David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2 | /* |
| 3 | * probe-event.c : perf-probe definition to probe_events format converter |
| 4 | * |
| 5 | * Written by Masami Hiramatsu <mhiramat@redhat.com> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <inttypes.h> |
| 9 | #include <sys/utsname.h> |
| 10 | #include <sys/types.h> |
| 11 | #include <sys/stat.h> |
| 12 | #include <fcntl.h> |
| 13 | #include <errno.h> |
| 14 | #include <stdio.h> |
| 15 | #include <unistd.h> |
| 16 | #include <stdlib.h> |
| 17 | #include <string.h> |
| 18 | #include <stdarg.h> |
| 19 | #include <limits.h> |
| 20 | #include <elf.h> |
| 21 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 22 | #include "build-id.h" |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 23 | #include "event.h" |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 24 | #include "namespaces.h" |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 25 | #include "strlist.h" |
| 26 | #include "strfilter.h" |
| 27 | #include "debug.h" |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 28 | #include "dso.h" |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 29 | #include "color.h" |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 30 | #include "map.h" |
| 31 | #include "map_groups.h" |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 32 | #include "symbol.h" |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 33 | #include <api/fs/fs.h> |
| 34 | #include "trace-event.h" /* For __maybe_unused */ |
| 35 | #include "probe-event.h" |
| 36 | #include "probe-finder.h" |
| 37 | #include "probe-file.h" |
| 38 | #include "session.h" |
| 39 | #include "string2.h" |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 40 | #include "strbuf.h" |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 41 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 42 | #include <subcmd/pager.h> |
| 43 | #include <linux/ctype.h> |
| 44 | #include <linux/zalloc.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 45 | |
| 46 | #define PERFPROBE_GROUP "probe" |
| 47 | |
| 48 | bool probe_event_dry_run; /* Dry run flag */ |
| 49 | struct probe_conf probe_conf; |
| 50 | |
| 51 | #define semantic_error(msg ...) pr_err("Semantic error :" msg) |
| 52 | |
| 53 | int e_snprintf(char *str, size_t size, const char *format, ...) |
| 54 | { |
| 55 | int ret; |
| 56 | va_list ap; |
| 57 | va_start(ap, format); |
| 58 | ret = vsnprintf(str, size, format, ap); |
| 59 | va_end(ap); |
| 60 | if (ret >= (int)size) |
| 61 | ret = -E2BIG; |
| 62 | return ret; |
| 63 | } |
| 64 | |
| 65 | static struct machine *host_machine; |
| 66 | |
| 67 | /* Initialize symbol maps and path of vmlinux/modules */ |
| 68 | int init_probe_symbol_maps(bool user_only) |
| 69 | { |
| 70 | int ret; |
| 71 | |
| 72 | symbol_conf.sort_by_name = true; |
| 73 | symbol_conf.allow_aliases = true; |
| 74 | ret = symbol__init(NULL); |
| 75 | if (ret < 0) { |
| 76 | pr_debug("Failed to init symbol map.\n"); |
| 77 | goto out; |
| 78 | } |
| 79 | |
| 80 | if (host_machine || user_only) /* already initialized */ |
| 81 | return 0; |
| 82 | |
| 83 | if (symbol_conf.vmlinux_name) |
| 84 | pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name); |
| 85 | |
| 86 | host_machine = machine__new_host(); |
| 87 | if (!host_machine) { |
| 88 | pr_debug("machine__new_host() failed.\n"); |
| 89 | symbol__exit(); |
| 90 | ret = -1; |
| 91 | } |
| 92 | out: |
| 93 | if (ret < 0) |
| 94 | pr_warning("Failed to init vmlinux path.\n"); |
| 95 | return ret; |
| 96 | } |
| 97 | |
| 98 | void exit_probe_symbol_maps(void) |
| 99 | { |
| 100 | machine__delete(host_machine); |
| 101 | host_machine = NULL; |
| 102 | symbol__exit(); |
| 103 | } |
| 104 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 105 | static struct ref_reloc_sym *kernel_get_ref_reloc_sym(struct map **pmap) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 106 | { |
| 107 | /* kmap->ref_reloc_sym should be set if host_machine is initialized */ |
| 108 | struct kmap *kmap; |
| 109 | struct map *map = machine__kernel_map(host_machine); |
| 110 | |
| 111 | if (map__load(map) < 0) |
| 112 | return NULL; |
| 113 | |
| 114 | kmap = map__kmap(map); |
| 115 | if (!kmap) |
| 116 | return NULL; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 117 | |
| 118 | if (pmap) |
| 119 | *pmap = map; |
| 120 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 121 | return kmap->ref_reloc_sym; |
| 122 | } |
| 123 | |
| 124 | static int kernel_get_symbol_address_by_name(const char *name, u64 *addr, |
| 125 | bool reloc, bool reladdr) |
| 126 | { |
| 127 | struct ref_reloc_sym *reloc_sym; |
| 128 | struct symbol *sym; |
| 129 | struct map *map; |
| 130 | |
| 131 | /* ref_reloc_sym is just a label. Need a special fix*/ |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 132 | reloc_sym = kernel_get_ref_reloc_sym(NULL); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 133 | if (reloc_sym && strcmp(name, reloc_sym->name) == 0) |
| 134 | *addr = (reloc) ? reloc_sym->addr : reloc_sym->unrelocated_addr; |
| 135 | else { |
| 136 | sym = machine__find_kernel_symbol_by_name(host_machine, name, &map); |
| 137 | if (!sym) |
| 138 | return -ENOENT; |
| 139 | *addr = map->unmap_ip(map, sym->start) - |
| 140 | ((reloc) ? 0 : map->reloc) - |
| 141 | ((reladdr) ? map->start : 0); |
| 142 | } |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | static struct map *kernel_get_module_map(const char *module) |
| 147 | { |
| 148 | struct maps *maps = machine__kernel_maps(host_machine); |
| 149 | struct map *pos; |
| 150 | |
| 151 | /* A file path -- this is an offline module */ |
| 152 | if (module && strchr(module, '/')) |
| 153 | return dso__new_map(module); |
| 154 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 155 | if (!module) { |
| 156 | pos = machine__kernel_map(host_machine); |
| 157 | return map__get(pos); |
| 158 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 159 | |
| 160 | for (pos = maps__first(maps); pos; pos = map__next(pos)) { |
| 161 | /* short_name is "[module]" */ |
| 162 | if (strncmp(pos->dso->short_name + 1, module, |
| 163 | pos->dso->short_name_len - 2) == 0 && |
| 164 | module[pos->dso->short_name_len - 2] == '\0') { |
| 165 | return map__get(pos); |
| 166 | } |
| 167 | } |
| 168 | return NULL; |
| 169 | } |
| 170 | |
| 171 | struct map *get_target_map(const char *target, struct nsinfo *nsi, bool user) |
| 172 | { |
| 173 | /* Init maps of given executable or kernel */ |
| 174 | if (user) { |
| 175 | struct map *map; |
| 176 | |
| 177 | map = dso__new_map(target); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 178 | if (map && map->dso) { |
| 179 | nsinfo__put(map->dso->nsinfo); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 180 | map->dso->nsinfo = nsinfo__get(nsi); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 181 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 182 | return map; |
| 183 | } else { |
| 184 | return kernel_get_module_map(target); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | static int convert_exec_to_group(const char *exec, char **result) |
| 189 | { |
| 190 | char *ptr1, *ptr2, *exec_copy; |
| 191 | char buf[64]; |
| 192 | int ret; |
| 193 | |
| 194 | exec_copy = strdup(exec); |
| 195 | if (!exec_copy) |
| 196 | return -ENOMEM; |
| 197 | |
| 198 | ptr1 = basename(exec_copy); |
| 199 | if (!ptr1) { |
| 200 | ret = -EINVAL; |
| 201 | goto out; |
| 202 | } |
| 203 | |
| 204 | for (ptr2 = ptr1; *ptr2 != '\0'; ptr2++) { |
| 205 | if (!isalnum(*ptr2) && *ptr2 != '_') { |
| 206 | *ptr2 = '\0'; |
| 207 | break; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1); |
| 212 | if (ret < 0) |
| 213 | goto out; |
| 214 | |
| 215 | *result = strdup(buf); |
| 216 | ret = *result ? 0 : -ENOMEM; |
| 217 | |
| 218 | out: |
| 219 | free(exec_copy); |
| 220 | return ret; |
| 221 | } |
| 222 | |
| 223 | static void clear_perf_probe_point(struct perf_probe_point *pp) |
| 224 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 225 | zfree(&pp->file); |
| 226 | zfree(&pp->function); |
| 227 | zfree(&pp->lazy_line); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs) |
| 231 | { |
| 232 | int i; |
| 233 | |
| 234 | for (i = 0; i < ntevs; i++) |
| 235 | clear_probe_trace_event(tevs + i); |
| 236 | } |
| 237 | |
| 238 | static bool kprobe_blacklist__listed(unsigned long address); |
| 239 | static bool kprobe_warn_out_range(const char *symbol, unsigned long address) |
| 240 | { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 241 | struct map *map; |
| 242 | bool ret = false; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 243 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 244 | map = kernel_get_module_map(NULL); |
| 245 | if (map) { |
| 246 | ret = address <= map->start || map->end < address; |
| 247 | if (ret) |
| 248 | pr_warning("%s is out of .text, skip it.\n", symbol); |
| 249 | map__put(map); |
| 250 | } |
| 251 | if (!ret && kprobe_blacklist__listed(address)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 252 | pr_warning("%s is blacklisted function, skip it.\n", symbol); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 253 | ret = true; |
| 254 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 255 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 256 | return ret; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | /* |
| 260 | * @module can be module name of module file path. In case of path, |
| 261 | * inspect elf and find out what is actual module name. |
| 262 | * Caller has to free mod_name after using it. |
| 263 | */ |
| 264 | static char *find_module_name(const char *module) |
| 265 | { |
| 266 | int fd; |
| 267 | Elf *elf; |
| 268 | GElf_Ehdr ehdr; |
| 269 | GElf_Shdr shdr; |
| 270 | Elf_Data *data; |
| 271 | Elf_Scn *sec; |
| 272 | char *mod_name = NULL; |
| 273 | int name_offset; |
| 274 | |
| 275 | fd = open(module, O_RDONLY); |
| 276 | if (fd < 0) |
| 277 | return NULL; |
| 278 | |
| 279 | elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); |
| 280 | if (elf == NULL) |
| 281 | goto elf_err; |
| 282 | |
| 283 | if (gelf_getehdr(elf, &ehdr) == NULL) |
| 284 | goto ret_err; |
| 285 | |
| 286 | sec = elf_section_by_name(elf, &ehdr, &shdr, |
| 287 | ".gnu.linkonce.this_module", NULL); |
| 288 | if (!sec) |
| 289 | goto ret_err; |
| 290 | |
| 291 | data = elf_getdata(sec, NULL); |
| 292 | if (!data || !data->d_buf) |
| 293 | goto ret_err; |
| 294 | |
| 295 | /* |
| 296 | * NOTE: |
| 297 | * '.gnu.linkonce.this_module' section of kernel module elf directly |
| 298 | * maps to 'struct module' from linux/module.h. This section contains |
| 299 | * actual module name which will be used by kernel after loading it. |
| 300 | * But, we cannot use 'struct module' here since linux/module.h is not |
| 301 | * exposed to user-space. Offset of 'name' has remained same from long |
| 302 | * time, so hardcoding it here. |
| 303 | */ |
| 304 | if (ehdr.e_ident[EI_CLASS] == ELFCLASS32) |
| 305 | name_offset = 12; |
| 306 | else /* expect ELFCLASS64 by default */ |
| 307 | name_offset = 24; |
| 308 | |
| 309 | mod_name = strdup((char *)data->d_buf + name_offset); |
| 310 | |
| 311 | ret_err: |
| 312 | elf_end(elf); |
| 313 | elf_err: |
| 314 | close(fd); |
| 315 | return mod_name; |
| 316 | } |
| 317 | |
| 318 | #ifdef HAVE_DWARF_SUPPORT |
| 319 | |
| 320 | static int kernel_get_module_dso(const char *module, struct dso **pdso) |
| 321 | { |
| 322 | struct dso *dso; |
| 323 | struct map *map; |
| 324 | const char *vmlinux_name; |
| 325 | int ret = 0; |
| 326 | |
| 327 | if (module) { |
| 328 | char module_name[128]; |
| 329 | |
| 330 | snprintf(module_name, sizeof(module_name), "[%s]", module); |
| 331 | map = map_groups__find_by_name(&host_machine->kmaps, module_name); |
| 332 | if (map) { |
| 333 | dso = map->dso; |
| 334 | goto found; |
| 335 | } |
| 336 | pr_debug("Failed to find module %s.\n", module); |
| 337 | return -ENOENT; |
| 338 | } |
| 339 | |
| 340 | map = machine__kernel_map(host_machine); |
| 341 | dso = map->dso; |
| 342 | |
| 343 | vmlinux_name = symbol_conf.vmlinux_name; |
| 344 | dso->load_errno = 0; |
| 345 | if (vmlinux_name) |
| 346 | ret = dso__load_vmlinux(dso, map, vmlinux_name, false); |
| 347 | else |
| 348 | ret = dso__load_vmlinux_path(dso, map); |
| 349 | found: |
| 350 | *pdso = dso; |
| 351 | return ret; |
| 352 | } |
| 353 | |
| 354 | /* |
| 355 | * Some binaries like glibc have special symbols which are on the symbol |
| 356 | * table, but not in the debuginfo. If we can find the address of the |
| 357 | * symbol from map, we can translate the address back to the probe point. |
| 358 | */ |
| 359 | static int find_alternative_probe_point(struct debuginfo *dinfo, |
| 360 | struct perf_probe_point *pp, |
| 361 | struct perf_probe_point *result, |
| 362 | const char *target, struct nsinfo *nsi, |
| 363 | bool uprobes) |
| 364 | { |
| 365 | struct map *map = NULL; |
| 366 | struct symbol *sym; |
| 367 | u64 address = 0; |
| 368 | int ret = -ENOENT; |
| 369 | |
| 370 | /* This can work only for function-name based one */ |
| 371 | if (!pp->function || pp->file) |
| 372 | return -ENOTSUP; |
| 373 | |
| 374 | map = get_target_map(target, nsi, uprobes); |
| 375 | if (!map) |
| 376 | return -EINVAL; |
| 377 | |
| 378 | /* Find the address of given function */ |
| 379 | map__for_each_symbol_by_name(map, pp->function, sym) { |
| 380 | if (uprobes) |
| 381 | address = sym->start; |
| 382 | else |
| 383 | address = map->unmap_ip(map, sym->start) - map->reloc; |
| 384 | break; |
| 385 | } |
| 386 | if (!address) { |
| 387 | ret = -ENOENT; |
| 388 | goto out; |
| 389 | } |
| 390 | pr_debug("Symbol %s address found : %" PRIx64 "\n", |
| 391 | pp->function, address); |
| 392 | |
| 393 | ret = debuginfo__find_probe_point(dinfo, (unsigned long)address, |
| 394 | result); |
| 395 | if (ret <= 0) |
| 396 | ret = (!ret) ? -ENOENT : ret; |
| 397 | else { |
| 398 | result->offset += pp->offset; |
| 399 | result->line += pp->line; |
| 400 | result->retprobe = pp->retprobe; |
| 401 | ret = 0; |
| 402 | } |
| 403 | |
| 404 | out: |
| 405 | map__put(map); |
| 406 | return ret; |
| 407 | |
| 408 | } |
| 409 | |
| 410 | static int get_alternative_probe_event(struct debuginfo *dinfo, |
| 411 | struct perf_probe_event *pev, |
| 412 | struct perf_probe_point *tmp) |
| 413 | { |
| 414 | int ret; |
| 415 | |
| 416 | memcpy(tmp, &pev->point, sizeof(*tmp)); |
| 417 | memset(&pev->point, 0, sizeof(pev->point)); |
| 418 | ret = find_alternative_probe_point(dinfo, tmp, &pev->point, pev->target, |
| 419 | pev->nsi, pev->uprobes); |
| 420 | if (ret < 0) |
| 421 | memcpy(&pev->point, tmp, sizeof(*tmp)); |
| 422 | |
| 423 | return ret; |
| 424 | } |
| 425 | |
| 426 | static int get_alternative_line_range(struct debuginfo *dinfo, |
| 427 | struct line_range *lr, |
| 428 | const char *target, bool user) |
| 429 | { |
| 430 | struct perf_probe_point pp = { .function = lr->function, |
| 431 | .file = lr->file, |
| 432 | .line = lr->start }; |
| 433 | struct perf_probe_point result; |
| 434 | int ret, len = 0; |
| 435 | |
| 436 | memset(&result, 0, sizeof(result)); |
| 437 | |
| 438 | if (lr->end != INT_MAX) |
| 439 | len = lr->end - lr->start; |
| 440 | ret = find_alternative_probe_point(dinfo, &pp, &result, |
| 441 | target, NULL, user); |
| 442 | if (!ret) { |
| 443 | lr->function = result.function; |
| 444 | lr->file = result.file; |
| 445 | lr->start = result.line; |
| 446 | if (lr->end != INT_MAX) |
| 447 | lr->end = lr->start + len; |
| 448 | clear_perf_probe_point(&pp); |
| 449 | } |
| 450 | return ret; |
| 451 | } |
| 452 | |
| 453 | /* Open new debuginfo of given module */ |
| 454 | static struct debuginfo *open_debuginfo(const char *module, struct nsinfo *nsi, |
| 455 | bool silent) |
| 456 | { |
| 457 | const char *path = module; |
| 458 | char reason[STRERR_BUFSIZE]; |
| 459 | struct debuginfo *ret = NULL; |
| 460 | struct dso *dso = NULL; |
| 461 | struct nscookie nsc; |
| 462 | int err; |
| 463 | |
| 464 | if (!module || !strchr(module, '/')) { |
| 465 | err = kernel_get_module_dso(module, &dso); |
| 466 | if (err < 0) { |
| 467 | if (!dso || dso->load_errno == 0) { |
| 468 | if (!str_error_r(-err, reason, STRERR_BUFSIZE)) |
| 469 | strcpy(reason, "(unknown)"); |
| 470 | } else |
| 471 | dso__strerror_load(dso, reason, STRERR_BUFSIZE); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 472 | if (!silent) { |
| 473 | if (module) |
| 474 | pr_err("Module %s is not loaded, please specify its full path name.\n", module); |
| 475 | else |
| 476 | pr_err("Failed to find the path for the kernel: %s\n", reason); |
| 477 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 478 | return NULL; |
| 479 | } |
| 480 | path = dso->long_name; |
| 481 | } |
| 482 | nsinfo__mountns_enter(nsi, &nsc); |
| 483 | ret = debuginfo__new(path); |
| 484 | if (!ret && !silent) { |
| 485 | pr_warning("The %s file has no debug information.\n", path); |
| 486 | if (!module || !strtailcmp(path, ".ko")) |
| 487 | pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, "); |
| 488 | else |
| 489 | pr_warning("Rebuild with -g, "); |
| 490 | pr_warning("or install an appropriate debuginfo package.\n"); |
| 491 | } |
| 492 | nsinfo__mountns_exit(&nsc); |
| 493 | return ret; |
| 494 | } |
| 495 | |
| 496 | /* For caching the last debuginfo */ |
| 497 | static struct debuginfo *debuginfo_cache; |
| 498 | static char *debuginfo_cache_path; |
| 499 | |
| 500 | static struct debuginfo *debuginfo_cache__open(const char *module, bool silent) |
| 501 | { |
| 502 | const char *path = module; |
| 503 | |
| 504 | /* If the module is NULL, it should be the kernel. */ |
| 505 | if (!module) |
| 506 | path = "kernel"; |
| 507 | |
| 508 | if (debuginfo_cache_path && !strcmp(debuginfo_cache_path, path)) |
| 509 | goto out; |
| 510 | |
| 511 | /* Copy module path */ |
| 512 | free(debuginfo_cache_path); |
| 513 | debuginfo_cache_path = strdup(path); |
| 514 | if (!debuginfo_cache_path) { |
| 515 | debuginfo__delete(debuginfo_cache); |
| 516 | debuginfo_cache = NULL; |
| 517 | goto out; |
| 518 | } |
| 519 | |
| 520 | debuginfo_cache = open_debuginfo(module, NULL, silent); |
| 521 | if (!debuginfo_cache) |
| 522 | zfree(&debuginfo_cache_path); |
| 523 | out: |
| 524 | return debuginfo_cache; |
| 525 | } |
| 526 | |
| 527 | static void debuginfo_cache__exit(void) |
| 528 | { |
| 529 | debuginfo__delete(debuginfo_cache); |
| 530 | debuginfo_cache = NULL; |
| 531 | zfree(&debuginfo_cache_path); |
| 532 | } |
| 533 | |
| 534 | |
| 535 | static int get_text_start_address(const char *exec, unsigned long *address, |
| 536 | struct nsinfo *nsi) |
| 537 | { |
| 538 | Elf *elf; |
| 539 | GElf_Ehdr ehdr; |
| 540 | GElf_Shdr shdr; |
| 541 | int fd, ret = -ENOENT; |
| 542 | struct nscookie nsc; |
| 543 | |
| 544 | nsinfo__mountns_enter(nsi, &nsc); |
| 545 | fd = open(exec, O_RDONLY); |
| 546 | nsinfo__mountns_exit(&nsc); |
| 547 | if (fd < 0) |
| 548 | return -errno; |
| 549 | |
| 550 | elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); |
| 551 | if (elf == NULL) { |
| 552 | ret = -EINVAL; |
| 553 | goto out_close; |
| 554 | } |
| 555 | |
| 556 | if (gelf_getehdr(elf, &ehdr) == NULL) |
| 557 | goto out; |
| 558 | |
| 559 | if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL)) |
| 560 | goto out; |
| 561 | |
| 562 | *address = shdr.sh_addr - shdr.sh_offset; |
| 563 | ret = 0; |
| 564 | out: |
| 565 | elf_end(elf); |
| 566 | out_close: |
| 567 | close(fd); |
| 568 | |
| 569 | return ret; |
| 570 | } |
| 571 | |
| 572 | /* |
| 573 | * Convert trace point to probe point with debuginfo |
| 574 | */ |
| 575 | static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp, |
| 576 | struct perf_probe_point *pp, |
| 577 | bool is_kprobe) |
| 578 | { |
| 579 | struct debuginfo *dinfo = NULL; |
| 580 | unsigned long stext = 0; |
| 581 | u64 addr = tp->address; |
| 582 | int ret = -ENOENT; |
| 583 | |
| 584 | /* convert the address to dwarf address */ |
| 585 | if (!is_kprobe) { |
| 586 | if (!addr) { |
| 587 | ret = -EINVAL; |
| 588 | goto error; |
| 589 | } |
| 590 | ret = get_text_start_address(tp->module, &stext, NULL); |
| 591 | if (ret < 0) |
| 592 | goto error; |
| 593 | addr += stext; |
| 594 | } else if (tp->symbol) { |
| 595 | /* If the module is given, this returns relative address */ |
| 596 | ret = kernel_get_symbol_address_by_name(tp->symbol, &addr, |
| 597 | false, !!tp->module); |
| 598 | if (ret != 0) |
| 599 | goto error; |
| 600 | addr += tp->offset; |
| 601 | } |
| 602 | |
| 603 | pr_debug("try to find information at %" PRIx64 " in %s\n", addr, |
| 604 | tp->module ? : "kernel"); |
| 605 | |
| 606 | dinfo = debuginfo_cache__open(tp->module, verbose <= 0); |
| 607 | if (dinfo) |
| 608 | ret = debuginfo__find_probe_point(dinfo, |
| 609 | (unsigned long)addr, pp); |
| 610 | else |
| 611 | ret = -ENOENT; |
| 612 | |
| 613 | if (ret > 0) { |
| 614 | pp->retprobe = tp->retprobe; |
| 615 | return 0; |
| 616 | } |
| 617 | error: |
| 618 | pr_debug("Failed to find corresponding probes from debuginfo.\n"); |
| 619 | return ret ? : -ENOENT; |
| 620 | } |
| 621 | |
| 622 | /* Adjust symbol name and address */ |
| 623 | static int post_process_probe_trace_point(struct probe_trace_point *tp, |
| 624 | struct map *map, unsigned long offs) |
| 625 | { |
| 626 | struct symbol *sym; |
| 627 | u64 addr = tp->address - offs; |
| 628 | |
| 629 | sym = map__find_symbol(map, addr); |
| 630 | if (!sym) |
| 631 | return -ENOENT; |
| 632 | |
| 633 | if (strcmp(sym->name, tp->symbol)) { |
| 634 | /* If we have no realname, use symbol for it */ |
| 635 | if (!tp->realname) |
| 636 | tp->realname = tp->symbol; |
| 637 | else |
| 638 | free(tp->symbol); |
| 639 | tp->symbol = strdup(sym->name); |
| 640 | if (!tp->symbol) |
| 641 | return -ENOMEM; |
| 642 | } |
| 643 | tp->offset = addr - sym->start; |
| 644 | tp->address -= offs; |
| 645 | |
| 646 | return 0; |
| 647 | } |
| 648 | |
| 649 | /* |
| 650 | * Rename DWARF symbols to ELF symbols -- gcc sometimes optimizes functions |
| 651 | * and generate new symbols with suffixes such as .constprop.N or .isra.N |
| 652 | * etc. Since those symbols are not recorded in DWARF, we have to find |
| 653 | * correct generated symbols from offline ELF binary. |
| 654 | * For online kernel or uprobes we don't need this because those are |
| 655 | * rebased on _text, or already a section relative address. |
| 656 | */ |
| 657 | static int |
| 658 | post_process_offline_probe_trace_events(struct probe_trace_event *tevs, |
| 659 | int ntevs, const char *pathname) |
| 660 | { |
| 661 | struct map *map; |
| 662 | unsigned long stext = 0; |
| 663 | int i, ret = 0; |
| 664 | |
| 665 | /* Prepare a map for offline binary */ |
| 666 | map = dso__new_map(pathname); |
| 667 | if (!map || get_text_start_address(pathname, &stext, NULL) < 0) { |
| 668 | pr_warning("Failed to get ELF symbols for %s\n", pathname); |
| 669 | return -EINVAL; |
| 670 | } |
| 671 | |
| 672 | for (i = 0; i < ntevs; i++) { |
| 673 | ret = post_process_probe_trace_point(&tevs[i].point, |
| 674 | map, stext); |
| 675 | if (ret < 0) |
| 676 | break; |
| 677 | } |
| 678 | map__put(map); |
| 679 | |
| 680 | return ret; |
| 681 | } |
| 682 | |
| 683 | static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs, |
| 684 | int ntevs, const char *exec, |
| 685 | struct nsinfo *nsi) |
| 686 | { |
| 687 | int i, ret = 0; |
| 688 | unsigned long stext = 0; |
| 689 | |
| 690 | if (!exec) |
| 691 | return 0; |
| 692 | |
| 693 | ret = get_text_start_address(exec, &stext, nsi); |
| 694 | if (ret < 0) |
| 695 | return ret; |
| 696 | |
| 697 | for (i = 0; i < ntevs && ret >= 0; i++) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 698 | /* point.address is the address of point.symbol + point.offset */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 699 | tevs[i].point.address -= stext; |
| 700 | tevs[i].point.module = strdup(exec); |
| 701 | if (!tevs[i].point.module) { |
| 702 | ret = -ENOMEM; |
| 703 | break; |
| 704 | } |
| 705 | tevs[i].uprobes = true; |
| 706 | } |
| 707 | |
| 708 | return ret; |
| 709 | } |
| 710 | |
| 711 | static int |
| 712 | post_process_module_probe_trace_events(struct probe_trace_event *tevs, |
| 713 | int ntevs, const char *module, |
| 714 | struct debuginfo *dinfo) |
| 715 | { |
| 716 | Dwarf_Addr text_offs = 0; |
| 717 | int i, ret = 0; |
| 718 | char *mod_name = NULL; |
| 719 | struct map *map; |
| 720 | |
| 721 | if (!module) |
| 722 | return 0; |
| 723 | |
| 724 | map = get_target_map(module, NULL, false); |
| 725 | if (!map || debuginfo__get_text_offset(dinfo, &text_offs, true) < 0) { |
| 726 | pr_warning("Failed to get ELF symbols for %s\n", module); |
| 727 | return -EINVAL; |
| 728 | } |
| 729 | |
| 730 | mod_name = find_module_name(module); |
| 731 | for (i = 0; i < ntevs; i++) { |
| 732 | ret = post_process_probe_trace_point(&tevs[i].point, |
| 733 | map, (unsigned long)text_offs); |
| 734 | if (ret < 0) |
| 735 | break; |
| 736 | tevs[i].point.module = |
| 737 | strdup(mod_name ? mod_name : module); |
| 738 | if (!tevs[i].point.module) { |
| 739 | ret = -ENOMEM; |
| 740 | break; |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | free(mod_name); |
| 745 | map__put(map); |
| 746 | |
| 747 | return ret; |
| 748 | } |
| 749 | |
| 750 | static int |
| 751 | post_process_kernel_probe_trace_events(struct probe_trace_event *tevs, |
| 752 | int ntevs) |
| 753 | { |
| 754 | struct ref_reloc_sym *reloc_sym; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 755 | struct map *map; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 756 | char *tmp; |
| 757 | int i, skipped = 0; |
| 758 | |
| 759 | /* Skip post process if the target is an offline kernel */ |
| 760 | if (symbol_conf.ignore_vmlinux_buildid) |
| 761 | return post_process_offline_probe_trace_events(tevs, ntevs, |
| 762 | symbol_conf.vmlinux_name); |
| 763 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 764 | reloc_sym = kernel_get_ref_reloc_sym(&map); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 765 | if (!reloc_sym) { |
| 766 | pr_warning("Relocated base symbol is not found!\n"); |
| 767 | return -EINVAL; |
| 768 | } |
| 769 | |
| 770 | for (i = 0; i < ntevs; i++) { |
| 771 | if (!tevs[i].point.address) |
| 772 | continue; |
| 773 | if (tevs[i].point.retprobe && !kretprobe_offset_is_supported()) |
| 774 | continue; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 775 | /* |
| 776 | * If we found a wrong one, mark it by NULL symbol. |
| 777 | * Since addresses in debuginfo is same as objdump, we need |
| 778 | * to convert it to addresses on memory. |
| 779 | */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 780 | if (kprobe_warn_out_range(tevs[i].point.symbol, |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 781 | map__objdump_2mem(map, tevs[i].point.address))) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 782 | tmp = NULL; |
| 783 | skipped++; |
| 784 | } else { |
| 785 | tmp = strdup(reloc_sym->name); |
| 786 | if (!tmp) |
| 787 | return -ENOMEM; |
| 788 | } |
| 789 | /* If we have no realname, use symbol for it */ |
| 790 | if (!tevs[i].point.realname) |
| 791 | tevs[i].point.realname = tevs[i].point.symbol; |
| 792 | else |
| 793 | free(tevs[i].point.symbol); |
| 794 | tevs[i].point.symbol = tmp; |
| 795 | tevs[i].point.offset = tevs[i].point.address - |
| 796 | reloc_sym->unrelocated_addr; |
| 797 | } |
| 798 | return skipped; |
| 799 | } |
| 800 | |
| 801 | void __weak |
| 802 | arch__post_process_probe_trace_events(struct perf_probe_event *pev __maybe_unused, |
| 803 | int ntevs __maybe_unused) |
| 804 | { |
| 805 | } |
| 806 | |
| 807 | /* Post processing the probe events */ |
| 808 | static int post_process_probe_trace_events(struct perf_probe_event *pev, |
| 809 | struct probe_trace_event *tevs, |
| 810 | int ntevs, const char *module, |
| 811 | bool uprobe, struct debuginfo *dinfo) |
| 812 | { |
| 813 | int ret; |
| 814 | |
| 815 | if (uprobe) |
| 816 | ret = add_exec_to_probe_trace_events(tevs, ntevs, module, |
| 817 | pev->nsi); |
| 818 | else if (module) |
| 819 | /* Currently ref_reloc_sym based probe is not for drivers */ |
| 820 | ret = post_process_module_probe_trace_events(tevs, ntevs, |
| 821 | module, dinfo); |
| 822 | else |
| 823 | ret = post_process_kernel_probe_trace_events(tevs, ntevs); |
| 824 | |
| 825 | if (ret >= 0) |
| 826 | arch__post_process_probe_trace_events(pev, ntevs); |
| 827 | |
| 828 | return ret; |
| 829 | } |
| 830 | |
| 831 | /* Try to find perf_probe_event with debuginfo */ |
| 832 | static int try_to_find_probe_trace_events(struct perf_probe_event *pev, |
| 833 | struct probe_trace_event **tevs) |
| 834 | { |
| 835 | bool need_dwarf = perf_probe_event_need_dwarf(pev); |
| 836 | struct perf_probe_point tmp; |
| 837 | struct debuginfo *dinfo; |
| 838 | int ntevs, ret = 0; |
| 839 | |
| 840 | dinfo = open_debuginfo(pev->target, pev->nsi, !need_dwarf); |
| 841 | if (!dinfo) { |
| 842 | if (need_dwarf) |
| 843 | return -ENOENT; |
| 844 | pr_debug("Could not open debuginfo. Try to use symbols.\n"); |
| 845 | return 0; |
| 846 | } |
| 847 | |
| 848 | pr_debug("Try to find probe point from debuginfo.\n"); |
| 849 | /* Searching trace events corresponding to a probe event */ |
| 850 | ntevs = debuginfo__find_trace_events(dinfo, pev, tevs); |
| 851 | |
| 852 | if (ntevs == 0) { /* Not found, retry with an alternative */ |
| 853 | ret = get_alternative_probe_event(dinfo, pev, &tmp); |
| 854 | if (!ret) { |
| 855 | ntevs = debuginfo__find_trace_events(dinfo, pev, tevs); |
| 856 | /* |
| 857 | * Write back to the original probe_event for |
| 858 | * setting appropriate (user given) event name |
| 859 | */ |
| 860 | clear_perf_probe_point(&pev->point); |
| 861 | memcpy(&pev->point, &tmp, sizeof(tmp)); |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | if (ntevs > 0) { /* Succeeded to find trace events */ |
| 866 | pr_debug("Found %d probe_trace_events.\n", ntevs); |
| 867 | ret = post_process_probe_trace_events(pev, *tevs, ntevs, |
| 868 | pev->target, pev->uprobes, dinfo); |
| 869 | if (ret < 0 || ret == ntevs) { |
| 870 | pr_debug("Post processing failed or all events are skipped. (%d)\n", ret); |
| 871 | clear_probe_trace_events(*tevs, ntevs); |
| 872 | zfree(tevs); |
| 873 | ntevs = 0; |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | debuginfo__delete(dinfo); |
| 878 | |
| 879 | if (ntevs == 0) { /* No error but failed to find probe point. */ |
| 880 | pr_warning("Probe point '%s' not found.\n", |
| 881 | synthesize_perf_probe_point(&pev->point)); |
| 882 | return -ENOENT; |
| 883 | } else if (ntevs < 0) { |
| 884 | /* Error path : ntevs < 0 */ |
| 885 | pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs); |
| 886 | if (ntevs == -EBADF) |
| 887 | pr_warning("Warning: No dwarf info found in the vmlinux - " |
| 888 | "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n"); |
| 889 | if (!need_dwarf) { |
| 890 | pr_debug("Trying to use symbols.\n"); |
| 891 | return 0; |
| 892 | } |
| 893 | } |
| 894 | return ntevs; |
| 895 | } |
| 896 | |
| 897 | #define LINEBUF_SIZE 256 |
| 898 | #define NR_ADDITIONAL_LINES 2 |
| 899 | |
| 900 | static int __show_one_line(FILE *fp, int l, bool skip, bool show_num) |
| 901 | { |
| 902 | char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE]; |
| 903 | const char *color = show_num ? "" : PERF_COLOR_BLUE; |
| 904 | const char *prefix = NULL; |
| 905 | |
| 906 | do { |
| 907 | if (fgets(buf, LINEBUF_SIZE, fp) == NULL) |
| 908 | goto error; |
| 909 | if (skip) |
| 910 | continue; |
| 911 | if (!prefix) { |
| 912 | prefix = show_num ? "%7d " : " "; |
| 913 | color_fprintf(stdout, color, prefix, l); |
| 914 | } |
| 915 | color_fprintf(stdout, color, "%s", buf); |
| 916 | |
| 917 | } while (strchr(buf, '\n') == NULL); |
| 918 | |
| 919 | return 1; |
| 920 | error: |
| 921 | if (ferror(fp)) { |
| 922 | pr_warning("File read error: %s\n", |
| 923 | str_error_r(errno, sbuf, sizeof(sbuf))); |
| 924 | return -1; |
| 925 | } |
| 926 | return 0; |
| 927 | } |
| 928 | |
| 929 | static int _show_one_line(FILE *fp, int l, bool skip, bool show_num) |
| 930 | { |
| 931 | int rv = __show_one_line(fp, l, skip, show_num); |
| 932 | if (rv == 0) { |
| 933 | pr_warning("Source file is shorter than expected.\n"); |
| 934 | rv = -1; |
| 935 | } |
| 936 | return rv; |
| 937 | } |
| 938 | |
| 939 | #define show_one_line_with_num(f,l) _show_one_line(f,l,false,true) |
| 940 | #define show_one_line(f,l) _show_one_line(f,l,false,false) |
| 941 | #define skip_one_line(f,l) _show_one_line(f,l,true,false) |
| 942 | #define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false) |
| 943 | |
| 944 | /* |
| 945 | * Show line-range always requires debuginfo to find source file and |
| 946 | * line number. |
| 947 | */ |
| 948 | static int __show_line_range(struct line_range *lr, const char *module, |
| 949 | bool user) |
| 950 | { |
| 951 | int l = 1; |
| 952 | struct int_node *ln; |
| 953 | struct debuginfo *dinfo; |
| 954 | FILE *fp; |
| 955 | int ret; |
| 956 | char *tmp; |
| 957 | char sbuf[STRERR_BUFSIZE]; |
| 958 | |
| 959 | /* Search a line range */ |
| 960 | dinfo = open_debuginfo(module, NULL, false); |
| 961 | if (!dinfo) |
| 962 | return -ENOENT; |
| 963 | |
| 964 | ret = debuginfo__find_line_range(dinfo, lr); |
| 965 | if (!ret) { /* Not found, retry with an alternative */ |
| 966 | ret = get_alternative_line_range(dinfo, lr, module, user); |
| 967 | if (!ret) |
| 968 | ret = debuginfo__find_line_range(dinfo, lr); |
| 969 | } |
| 970 | debuginfo__delete(dinfo); |
| 971 | if (ret == 0 || ret == -ENOENT) { |
| 972 | pr_warning("Specified source line is not found.\n"); |
| 973 | return -ENOENT; |
| 974 | } else if (ret < 0) { |
| 975 | pr_warning("Debuginfo analysis failed.\n"); |
| 976 | return ret; |
| 977 | } |
| 978 | |
| 979 | /* Convert source file path */ |
| 980 | tmp = lr->path; |
| 981 | ret = get_real_path(tmp, lr->comp_dir, &lr->path); |
| 982 | |
| 983 | /* Free old path when new path is assigned */ |
| 984 | if (tmp != lr->path) |
| 985 | free(tmp); |
| 986 | |
| 987 | if (ret < 0) { |
| 988 | pr_warning("Failed to find source file path.\n"); |
| 989 | return ret; |
| 990 | } |
| 991 | |
| 992 | setup_pager(); |
| 993 | |
| 994 | if (lr->function) |
| 995 | fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path, |
| 996 | lr->start - lr->offset); |
| 997 | else |
| 998 | fprintf(stdout, "<%s:%d>\n", lr->path, lr->start); |
| 999 | |
| 1000 | fp = fopen(lr->path, "r"); |
| 1001 | if (fp == NULL) { |
| 1002 | pr_warning("Failed to open %s: %s\n", lr->path, |
| 1003 | str_error_r(errno, sbuf, sizeof(sbuf))); |
| 1004 | return -errno; |
| 1005 | } |
| 1006 | /* Skip to starting line number */ |
| 1007 | while (l < lr->start) { |
| 1008 | ret = skip_one_line(fp, l++); |
| 1009 | if (ret < 0) |
| 1010 | goto end; |
| 1011 | } |
| 1012 | |
| 1013 | intlist__for_each_entry(ln, lr->line_list) { |
| 1014 | for (; ln->i > l; l++) { |
| 1015 | ret = show_one_line(fp, l - lr->offset); |
| 1016 | if (ret < 0) |
| 1017 | goto end; |
| 1018 | } |
| 1019 | ret = show_one_line_with_num(fp, l++ - lr->offset); |
| 1020 | if (ret < 0) |
| 1021 | goto end; |
| 1022 | } |
| 1023 | |
| 1024 | if (lr->end == INT_MAX) |
| 1025 | lr->end = l + NR_ADDITIONAL_LINES; |
| 1026 | while (l <= lr->end) { |
| 1027 | ret = show_one_line_or_eof(fp, l++ - lr->offset); |
| 1028 | if (ret <= 0) |
| 1029 | break; |
| 1030 | } |
| 1031 | end: |
| 1032 | fclose(fp); |
| 1033 | return ret; |
| 1034 | } |
| 1035 | |
| 1036 | int show_line_range(struct line_range *lr, const char *module, |
| 1037 | struct nsinfo *nsi, bool user) |
| 1038 | { |
| 1039 | int ret; |
| 1040 | struct nscookie nsc; |
| 1041 | |
| 1042 | ret = init_probe_symbol_maps(user); |
| 1043 | if (ret < 0) |
| 1044 | return ret; |
| 1045 | nsinfo__mountns_enter(nsi, &nsc); |
| 1046 | ret = __show_line_range(lr, module, user); |
| 1047 | nsinfo__mountns_exit(&nsc); |
| 1048 | exit_probe_symbol_maps(); |
| 1049 | |
| 1050 | return ret; |
| 1051 | } |
| 1052 | |
| 1053 | static int show_available_vars_at(struct debuginfo *dinfo, |
| 1054 | struct perf_probe_event *pev, |
| 1055 | struct strfilter *_filter) |
| 1056 | { |
| 1057 | char *buf; |
| 1058 | int ret, i, nvars; |
| 1059 | struct str_node *node; |
| 1060 | struct variable_list *vls = NULL, *vl; |
| 1061 | struct perf_probe_point tmp; |
| 1062 | const char *var; |
| 1063 | |
| 1064 | buf = synthesize_perf_probe_point(&pev->point); |
| 1065 | if (!buf) |
| 1066 | return -EINVAL; |
| 1067 | pr_debug("Searching variables at %s\n", buf); |
| 1068 | |
| 1069 | ret = debuginfo__find_available_vars_at(dinfo, pev, &vls); |
| 1070 | if (!ret) { /* Not found, retry with an alternative */ |
| 1071 | ret = get_alternative_probe_event(dinfo, pev, &tmp); |
| 1072 | if (!ret) { |
| 1073 | ret = debuginfo__find_available_vars_at(dinfo, pev, |
| 1074 | &vls); |
| 1075 | /* Release the old probe_point */ |
| 1076 | clear_perf_probe_point(&tmp); |
| 1077 | } |
| 1078 | } |
| 1079 | if (ret <= 0) { |
| 1080 | if (ret == 0 || ret == -ENOENT) { |
| 1081 | pr_err("Failed to find the address of %s\n", buf); |
| 1082 | ret = -ENOENT; |
| 1083 | } else |
| 1084 | pr_warning("Debuginfo analysis failed.\n"); |
| 1085 | goto end; |
| 1086 | } |
| 1087 | |
| 1088 | /* Some variables are found */ |
| 1089 | fprintf(stdout, "Available variables at %s\n", buf); |
| 1090 | for (i = 0; i < ret; i++) { |
| 1091 | vl = &vls[i]; |
| 1092 | /* |
| 1093 | * A probe point might be converted to |
| 1094 | * several trace points. |
| 1095 | */ |
| 1096 | fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol, |
| 1097 | vl->point.offset); |
| 1098 | zfree(&vl->point.symbol); |
| 1099 | nvars = 0; |
| 1100 | if (vl->vars) { |
| 1101 | strlist__for_each_entry(node, vl->vars) { |
| 1102 | var = strchr(node->s, '\t') + 1; |
| 1103 | if (strfilter__compare(_filter, var)) { |
| 1104 | fprintf(stdout, "\t\t%s\n", node->s); |
| 1105 | nvars++; |
| 1106 | } |
| 1107 | } |
| 1108 | strlist__delete(vl->vars); |
| 1109 | } |
| 1110 | if (nvars == 0) |
| 1111 | fprintf(stdout, "\t\t(No matched variables)\n"); |
| 1112 | } |
| 1113 | free(vls); |
| 1114 | end: |
| 1115 | free(buf); |
| 1116 | return ret; |
| 1117 | } |
| 1118 | |
| 1119 | /* Show available variables on given probe point */ |
| 1120 | int show_available_vars(struct perf_probe_event *pevs, int npevs, |
| 1121 | struct strfilter *_filter) |
| 1122 | { |
| 1123 | int i, ret = 0; |
| 1124 | struct debuginfo *dinfo; |
| 1125 | |
| 1126 | ret = init_probe_symbol_maps(pevs->uprobes); |
| 1127 | if (ret < 0) |
| 1128 | return ret; |
| 1129 | |
| 1130 | dinfo = open_debuginfo(pevs->target, pevs->nsi, false); |
| 1131 | if (!dinfo) { |
| 1132 | ret = -ENOENT; |
| 1133 | goto out; |
| 1134 | } |
| 1135 | |
| 1136 | setup_pager(); |
| 1137 | |
| 1138 | for (i = 0; i < npevs && ret >= 0; i++) |
| 1139 | ret = show_available_vars_at(dinfo, &pevs[i], _filter); |
| 1140 | |
| 1141 | debuginfo__delete(dinfo); |
| 1142 | out: |
| 1143 | exit_probe_symbol_maps(); |
| 1144 | return ret; |
| 1145 | } |
| 1146 | |
| 1147 | #else /* !HAVE_DWARF_SUPPORT */ |
| 1148 | |
| 1149 | static void debuginfo_cache__exit(void) |
| 1150 | { |
| 1151 | } |
| 1152 | |
| 1153 | static int |
| 1154 | find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused, |
| 1155 | struct perf_probe_point *pp __maybe_unused, |
| 1156 | bool is_kprobe __maybe_unused) |
| 1157 | { |
| 1158 | return -ENOSYS; |
| 1159 | } |
| 1160 | |
| 1161 | static int try_to_find_probe_trace_events(struct perf_probe_event *pev, |
| 1162 | struct probe_trace_event **tevs __maybe_unused) |
| 1163 | { |
| 1164 | if (perf_probe_event_need_dwarf(pev)) { |
| 1165 | pr_warning("Debuginfo-analysis is not supported.\n"); |
| 1166 | return -ENOSYS; |
| 1167 | } |
| 1168 | |
| 1169 | return 0; |
| 1170 | } |
| 1171 | |
| 1172 | int show_line_range(struct line_range *lr __maybe_unused, |
| 1173 | const char *module __maybe_unused, |
| 1174 | struct nsinfo *nsi __maybe_unused, |
| 1175 | bool user __maybe_unused) |
| 1176 | { |
| 1177 | pr_warning("Debuginfo-analysis is not supported.\n"); |
| 1178 | return -ENOSYS; |
| 1179 | } |
| 1180 | |
| 1181 | int show_available_vars(struct perf_probe_event *pevs __maybe_unused, |
| 1182 | int npevs __maybe_unused, |
| 1183 | struct strfilter *filter __maybe_unused) |
| 1184 | { |
| 1185 | pr_warning("Debuginfo-analysis is not supported.\n"); |
| 1186 | return -ENOSYS; |
| 1187 | } |
| 1188 | #endif |
| 1189 | |
| 1190 | void line_range__clear(struct line_range *lr) |
| 1191 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1192 | zfree(&lr->function); |
| 1193 | zfree(&lr->file); |
| 1194 | zfree(&lr->path); |
| 1195 | zfree(&lr->comp_dir); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1196 | intlist__delete(lr->line_list); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | int line_range__init(struct line_range *lr) |
| 1200 | { |
| 1201 | memset(lr, 0, sizeof(*lr)); |
| 1202 | lr->line_list = intlist__new(NULL); |
| 1203 | if (!lr->line_list) |
| 1204 | return -ENOMEM; |
| 1205 | else |
| 1206 | return 0; |
| 1207 | } |
| 1208 | |
| 1209 | static int parse_line_num(char **ptr, int *val, const char *what) |
| 1210 | { |
| 1211 | const char *start = *ptr; |
| 1212 | |
| 1213 | errno = 0; |
| 1214 | *val = strtol(*ptr, ptr, 0); |
| 1215 | if (errno || *ptr == start) { |
| 1216 | semantic_error("'%s' is not a valid number.\n", what); |
| 1217 | return -EINVAL; |
| 1218 | } |
| 1219 | return 0; |
| 1220 | } |
| 1221 | |
| 1222 | /* Check the name is good for event, group or function */ |
| 1223 | static bool is_c_func_name(const char *name) |
| 1224 | { |
| 1225 | if (!isalpha(*name) && *name != '_') |
| 1226 | return false; |
| 1227 | while (*++name != '\0') { |
| 1228 | if (!isalpha(*name) && !isdigit(*name) && *name != '_') |
| 1229 | return false; |
| 1230 | } |
| 1231 | return true; |
| 1232 | } |
| 1233 | |
| 1234 | /* |
| 1235 | * Stuff 'lr' according to the line range described by 'arg'. |
| 1236 | * The line range syntax is described by: |
| 1237 | * |
| 1238 | * SRC[:SLN[+NUM|-ELN]] |
| 1239 | * FNC[@SRC][:SLN[+NUM|-ELN]] |
| 1240 | */ |
| 1241 | int parse_line_range_desc(const char *arg, struct line_range *lr) |
| 1242 | { |
| 1243 | char *range, *file, *name = strdup(arg); |
| 1244 | int err; |
| 1245 | |
| 1246 | if (!name) |
| 1247 | return -ENOMEM; |
| 1248 | |
| 1249 | lr->start = 0; |
| 1250 | lr->end = INT_MAX; |
| 1251 | |
| 1252 | range = strchr(name, ':'); |
| 1253 | if (range) { |
| 1254 | *range++ = '\0'; |
| 1255 | |
| 1256 | err = parse_line_num(&range, &lr->start, "start line"); |
| 1257 | if (err) |
| 1258 | goto err; |
| 1259 | |
| 1260 | if (*range == '+' || *range == '-') { |
| 1261 | const char c = *range++; |
| 1262 | |
| 1263 | err = parse_line_num(&range, &lr->end, "end line"); |
| 1264 | if (err) |
| 1265 | goto err; |
| 1266 | |
| 1267 | if (c == '+') { |
| 1268 | lr->end += lr->start; |
| 1269 | /* |
| 1270 | * Adjust the number of lines here. |
| 1271 | * If the number of lines == 1, the |
| 1272 | * the end of line should be equal to |
| 1273 | * the start of line. |
| 1274 | */ |
| 1275 | lr->end--; |
| 1276 | } |
| 1277 | } |
| 1278 | |
| 1279 | pr_debug("Line range is %d to %d\n", lr->start, lr->end); |
| 1280 | |
| 1281 | err = -EINVAL; |
| 1282 | if (lr->start > lr->end) { |
| 1283 | semantic_error("Start line must be smaller" |
| 1284 | " than end line.\n"); |
| 1285 | goto err; |
| 1286 | } |
| 1287 | if (*range != '\0') { |
| 1288 | semantic_error("Tailing with invalid str '%s'.\n", range); |
| 1289 | goto err; |
| 1290 | } |
| 1291 | } |
| 1292 | |
| 1293 | file = strchr(name, '@'); |
| 1294 | if (file) { |
| 1295 | *file = '\0'; |
| 1296 | lr->file = strdup(++file); |
| 1297 | if (lr->file == NULL) { |
| 1298 | err = -ENOMEM; |
| 1299 | goto err; |
| 1300 | } |
| 1301 | lr->function = name; |
| 1302 | } else if (strchr(name, '/') || strchr(name, '.')) |
| 1303 | lr->file = name; |
| 1304 | else if (is_c_func_name(name))/* We reuse it for checking funcname */ |
| 1305 | lr->function = name; |
| 1306 | else { /* Invalid name */ |
| 1307 | semantic_error("'%s' is not a valid function name.\n", name); |
| 1308 | err = -EINVAL; |
| 1309 | goto err; |
| 1310 | } |
| 1311 | |
| 1312 | return 0; |
| 1313 | err: |
| 1314 | free(name); |
| 1315 | return err; |
| 1316 | } |
| 1317 | |
| 1318 | static int parse_perf_probe_event_name(char **arg, struct perf_probe_event *pev) |
| 1319 | { |
| 1320 | char *ptr; |
| 1321 | |
| 1322 | ptr = strpbrk_esc(*arg, ":"); |
| 1323 | if (ptr) { |
| 1324 | *ptr = '\0'; |
| 1325 | if (!pev->sdt && !is_c_func_name(*arg)) |
| 1326 | goto ng_name; |
| 1327 | pev->group = strdup_esc(*arg); |
| 1328 | if (!pev->group) |
| 1329 | return -ENOMEM; |
| 1330 | *arg = ptr + 1; |
| 1331 | } else |
| 1332 | pev->group = NULL; |
| 1333 | |
| 1334 | pev->event = strdup_esc(*arg); |
| 1335 | if (pev->event == NULL) |
| 1336 | return -ENOMEM; |
| 1337 | |
| 1338 | if (!pev->sdt && !is_c_func_name(pev->event)) { |
| 1339 | zfree(&pev->event); |
| 1340 | ng_name: |
| 1341 | zfree(&pev->group); |
| 1342 | semantic_error("%s is bad for event name -it must " |
| 1343 | "follow C symbol-naming rule.\n", *arg); |
| 1344 | return -EINVAL; |
| 1345 | } |
| 1346 | return 0; |
| 1347 | } |
| 1348 | |
| 1349 | /* Parse probepoint definition. */ |
| 1350 | static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev) |
| 1351 | { |
| 1352 | struct perf_probe_point *pp = &pev->point; |
| 1353 | char *ptr, *tmp; |
| 1354 | char c, nc = 0; |
| 1355 | bool file_spec = false; |
| 1356 | int ret; |
| 1357 | |
| 1358 | /* |
| 1359 | * <Syntax> |
| 1360 | * perf probe [GRP:][EVENT=]SRC[:LN|;PTN] |
| 1361 | * perf probe [GRP:][EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT] |
| 1362 | * perf probe %[GRP:]SDT_EVENT |
| 1363 | */ |
| 1364 | if (!arg) |
| 1365 | return -EINVAL; |
| 1366 | |
| 1367 | if (is_sdt_event(arg)) { |
| 1368 | pev->sdt = true; |
| 1369 | if (arg[0] == '%') |
| 1370 | arg++; |
| 1371 | } |
| 1372 | |
| 1373 | ptr = strpbrk_esc(arg, ";=@+%"); |
| 1374 | if (pev->sdt) { |
| 1375 | if (ptr) { |
| 1376 | if (*ptr != '@') { |
| 1377 | semantic_error("%s must be an SDT name.\n", |
| 1378 | arg); |
| 1379 | return -EINVAL; |
| 1380 | } |
| 1381 | /* This must be a target file name or build id */ |
| 1382 | tmp = build_id_cache__complement(ptr + 1); |
| 1383 | if (tmp) { |
| 1384 | pev->target = build_id_cache__origname(tmp); |
| 1385 | free(tmp); |
| 1386 | } else |
| 1387 | pev->target = strdup_esc(ptr + 1); |
| 1388 | if (!pev->target) |
| 1389 | return -ENOMEM; |
| 1390 | *ptr = '\0'; |
| 1391 | } |
| 1392 | ret = parse_perf_probe_event_name(&arg, pev); |
| 1393 | if (ret == 0) { |
| 1394 | if (asprintf(&pev->point.function, "%%%s", pev->event) < 0) |
| 1395 | ret = -errno; |
| 1396 | } |
| 1397 | return ret; |
| 1398 | } |
| 1399 | |
| 1400 | if (ptr && *ptr == '=') { /* Event name */ |
| 1401 | *ptr = '\0'; |
| 1402 | tmp = ptr + 1; |
| 1403 | ret = parse_perf_probe_event_name(&arg, pev); |
| 1404 | if (ret < 0) |
| 1405 | return ret; |
| 1406 | |
| 1407 | arg = tmp; |
| 1408 | } |
| 1409 | |
| 1410 | /* |
| 1411 | * Check arg is function or file name and copy it. |
| 1412 | * |
| 1413 | * We consider arg to be a file spec if and only if it satisfies |
| 1414 | * all of the below criteria:: |
| 1415 | * - it does not include any of "+@%", |
| 1416 | * - it includes one of ":;", and |
| 1417 | * - it has a period '.' in the name. |
| 1418 | * |
| 1419 | * Otherwise, we consider arg to be a function specification. |
| 1420 | */ |
| 1421 | if (!strpbrk_esc(arg, "+@%")) { |
| 1422 | ptr = strpbrk_esc(arg, ";:"); |
| 1423 | /* This is a file spec if it includes a '.' before ; or : */ |
| 1424 | if (ptr && memchr(arg, '.', ptr - arg)) |
| 1425 | file_spec = true; |
| 1426 | } |
| 1427 | |
| 1428 | ptr = strpbrk_esc(arg, ";:+@%"); |
| 1429 | if (ptr) { |
| 1430 | nc = *ptr; |
| 1431 | *ptr++ = '\0'; |
| 1432 | } |
| 1433 | |
| 1434 | if (arg[0] == '\0') |
| 1435 | tmp = NULL; |
| 1436 | else { |
| 1437 | tmp = strdup_esc(arg); |
| 1438 | if (tmp == NULL) |
| 1439 | return -ENOMEM; |
| 1440 | } |
| 1441 | |
| 1442 | if (file_spec) |
| 1443 | pp->file = tmp; |
| 1444 | else { |
| 1445 | pp->function = tmp; |
| 1446 | |
| 1447 | /* |
| 1448 | * Keep pp->function even if this is absolute address, |
| 1449 | * so it can mark whether abs_address is valid. |
| 1450 | * Which make 'perf probe lib.bin 0x0' possible. |
| 1451 | * |
| 1452 | * Note that checking length of tmp is not needed |
| 1453 | * because when we access tmp[1] we know tmp[0] is '0', |
| 1454 | * so tmp[1] should always valid (but could be '\0'). |
| 1455 | */ |
| 1456 | if (tmp && !strncmp(tmp, "0x", 2)) { |
| 1457 | pp->abs_address = strtoul(pp->function, &tmp, 0); |
| 1458 | if (*tmp != '\0') { |
| 1459 | semantic_error("Invalid absolute address.\n"); |
| 1460 | return -EINVAL; |
| 1461 | } |
| 1462 | } |
| 1463 | } |
| 1464 | |
| 1465 | /* Parse other options */ |
| 1466 | while (ptr) { |
| 1467 | arg = ptr; |
| 1468 | c = nc; |
| 1469 | if (c == ';') { /* Lazy pattern must be the last part */ |
| 1470 | pp->lazy_line = strdup(arg); /* let leave escapes */ |
| 1471 | if (pp->lazy_line == NULL) |
| 1472 | return -ENOMEM; |
| 1473 | break; |
| 1474 | } |
| 1475 | ptr = strpbrk_esc(arg, ";:+@%"); |
| 1476 | if (ptr) { |
| 1477 | nc = *ptr; |
| 1478 | *ptr++ = '\0'; |
| 1479 | } |
| 1480 | switch (c) { |
| 1481 | case ':': /* Line number */ |
| 1482 | pp->line = strtoul(arg, &tmp, 0); |
| 1483 | if (*tmp != '\0') { |
| 1484 | semantic_error("There is non-digit char" |
| 1485 | " in line number.\n"); |
| 1486 | return -EINVAL; |
| 1487 | } |
| 1488 | break; |
| 1489 | case '+': /* Byte offset from a symbol */ |
| 1490 | pp->offset = strtoul(arg, &tmp, 0); |
| 1491 | if (*tmp != '\0') { |
| 1492 | semantic_error("There is non-digit character" |
| 1493 | " in offset.\n"); |
| 1494 | return -EINVAL; |
| 1495 | } |
| 1496 | break; |
| 1497 | case '@': /* File name */ |
| 1498 | if (pp->file) { |
| 1499 | semantic_error("SRC@SRC is not allowed.\n"); |
| 1500 | return -EINVAL; |
| 1501 | } |
| 1502 | pp->file = strdup_esc(arg); |
| 1503 | if (pp->file == NULL) |
| 1504 | return -ENOMEM; |
| 1505 | break; |
| 1506 | case '%': /* Probe places */ |
| 1507 | if (strcmp(arg, "return") == 0) { |
| 1508 | pp->retprobe = 1; |
| 1509 | } else { /* Others not supported yet */ |
| 1510 | semantic_error("%%%s is not supported.\n", arg); |
| 1511 | return -ENOTSUP; |
| 1512 | } |
| 1513 | break; |
| 1514 | default: /* Buggy case */ |
| 1515 | pr_err("This program has a bug at %s:%d.\n", |
| 1516 | __FILE__, __LINE__); |
| 1517 | return -ENOTSUP; |
| 1518 | break; |
| 1519 | } |
| 1520 | } |
| 1521 | |
| 1522 | /* Exclusion check */ |
| 1523 | if (pp->lazy_line && pp->line) { |
| 1524 | semantic_error("Lazy pattern can't be used with" |
| 1525 | " line number.\n"); |
| 1526 | return -EINVAL; |
| 1527 | } |
| 1528 | |
| 1529 | if (pp->lazy_line && pp->offset) { |
| 1530 | semantic_error("Lazy pattern can't be used with offset.\n"); |
| 1531 | return -EINVAL; |
| 1532 | } |
| 1533 | |
| 1534 | if (pp->line && pp->offset) { |
| 1535 | semantic_error("Offset can't be used with line number.\n"); |
| 1536 | return -EINVAL; |
| 1537 | } |
| 1538 | |
| 1539 | if (!pp->line && !pp->lazy_line && pp->file && !pp->function) { |
| 1540 | semantic_error("File always requires line number or " |
| 1541 | "lazy pattern.\n"); |
| 1542 | return -EINVAL; |
| 1543 | } |
| 1544 | |
| 1545 | if (pp->offset && !pp->function) { |
| 1546 | semantic_error("Offset requires an entry function.\n"); |
| 1547 | return -EINVAL; |
| 1548 | } |
| 1549 | |
| 1550 | if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) { |
| 1551 | semantic_error("Offset/Line/Lazy pattern can't be used with " |
| 1552 | "return probe.\n"); |
| 1553 | return -EINVAL; |
| 1554 | } |
| 1555 | |
| 1556 | pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n", |
| 1557 | pp->function, pp->file, pp->line, pp->offset, pp->retprobe, |
| 1558 | pp->lazy_line); |
| 1559 | return 0; |
| 1560 | } |
| 1561 | |
| 1562 | /* Parse perf-probe event argument */ |
| 1563 | static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg) |
| 1564 | { |
| 1565 | char *tmp, *goodname; |
| 1566 | struct perf_probe_arg_field **fieldp; |
| 1567 | |
| 1568 | pr_debug("parsing arg: %s into ", str); |
| 1569 | |
| 1570 | tmp = strchr(str, '='); |
| 1571 | if (tmp) { |
| 1572 | arg->name = strndup(str, tmp - str); |
| 1573 | if (arg->name == NULL) |
| 1574 | return -ENOMEM; |
| 1575 | pr_debug("name:%s ", arg->name); |
| 1576 | str = tmp + 1; |
| 1577 | } |
| 1578 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1579 | tmp = strchr(str, '@'); |
| 1580 | if (tmp && tmp != str && strcmp(tmp + 1, "user")) { /* user attr */ |
| 1581 | if (!user_access_is_supported()) { |
| 1582 | semantic_error("ftrace does not support user access\n"); |
| 1583 | return -EINVAL; |
| 1584 | } |
| 1585 | *tmp = '\0'; |
| 1586 | arg->user_access = true; |
| 1587 | pr_debug("user_access "); |
| 1588 | } |
| 1589 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1590 | tmp = strchr(str, ':'); |
| 1591 | if (tmp) { /* Type setting */ |
| 1592 | *tmp = '\0'; |
| 1593 | arg->type = strdup(tmp + 1); |
| 1594 | if (arg->type == NULL) |
| 1595 | return -ENOMEM; |
| 1596 | pr_debug("type:%s ", arg->type); |
| 1597 | } |
| 1598 | |
| 1599 | tmp = strpbrk(str, "-.["); |
| 1600 | if (!is_c_varname(str) || !tmp) { |
| 1601 | /* A variable, register, symbol or special value */ |
| 1602 | arg->var = strdup(str); |
| 1603 | if (arg->var == NULL) |
| 1604 | return -ENOMEM; |
| 1605 | pr_debug("%s\n", arg->var); |
| 1606 | return 0; |
| 1607 | } |
| 1608 | |
| 1609 | /* Structure fields or array element */ |
| 1610 | arg->var = strndup(str, tmp - str); |
| 1611 | if (arg->var == NULL) |
| 1612 | return -ENOMEM; |
| 1613 | goodname = arg->var; |
| 1614 | pr_debug("%s, ", arg->var); |
| 1615 | fieldp = &arg->field; |
| 1616 | |
| 1617 | do { |
| 1618 | *fieldp = zalloc(sizeof(struct perf_probe_arg_field)); |
| 1619 | if (*fieldp == NULL) |
| 1620 | return -ENOMEM; |
| 1621 | if (*tmp == '[') { /* Array */ |
| 1622 | str = tmp; |
| 1623 | (*fieldp)->index = strtol(str + 1, &tmp, 0); |
| 1624 | (*fieldp)->ref = true; |
| 1625 | if (*tmp != ']' || tmp == str + 1) { |
| 1626 | semantic_error("Array index must be a" |
| 1627 | " number.\n"); |
| 1628 | return -EINVAL; |
| 1629 | } |
| 1630 | tmp++; |
| 1631 | if (*tmp == '\0') |
| 1632 | tmp = NULL; |
| 1633 | } else { /* Structure */ |
| 1634 | if (*tmp == '.') { |
| 1635 | str = tmp + 1; |
| 1636 | (*fieldp)->ref = false; |
| 1637 | } else if (tmp[1] == '>') { |
| 1638 | str = tmp + 2; |
| 1639 | (*fieldp)->ref = true; |
| 1640 | } else { |
| 1641 | semantic_error("Argument parse error: %s\n", |
| 1642 | str); |
| 1643 | return -EINVAL; |
| 1644 | } |
| 1645 | tmp = strpbrk(str, "-.["); |
| 1646 | } |
| 1647 | if (tmp) { |
| 1648 | (*fieldp)->name = strndup(str, tmp - str); |
| 1649 | if ((*fieldp)->name == NULL) |
| 1650 | return -ENOMEM; |
| 1651 | if (*str != '[') |
| 1652 | goodname = (*fieldp)->name; |
| 1653 | pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref); |
| 1654 | fieldp = &(*fieldp)->next; |
| 1655 | } |
| 1656 | } while (tmp); |
| 1657 | (*fieldp)->name = strdup(str); |
| 1658 | if ((*fieldp)->name == NULL) |
| 1659 | return -ENOMEM; |
| 1660 | if (*str != '[') |
| 1661 | goodname = (*fieldp)->name; |
| 1662 | pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref); |
| 1663 | |
| 1664 | /* If no name is specified, set the last field name (not array index)*/ |
| 1665 | if (!arg->name) { |
| 1666 | arg->name = strdup(goodname); |
| 1667 | if (arg->name == NULL) |
| 1668 | return -ENOMEM; |
| 1669 | } |
| 1670 | return 0; |
| 1671 | } |
| 1672 | |
| 1673 | /* Parse perf-probe event command */ |
| 1674 | int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev) |
| 1675 | { |
| 1676 | char **argv; |
| 1677 | int argc, i, ret = 0; |
| 1678 | |
| 1679 | argv = argv_split(cmd, &argc); |
| 1680 | if (!argv) { |
| 1681 | pr_debug("Failed to split arguments.\n"); |
| 1682 | return -ENOMEM; |
| 1683 | } |
| 1684 | if (argc - 1 > MAX_PROBE_ARGS) { |
| 1685 | semantic_error("Too many probe arguments (%d).\n", argc - 1); |
| 1686 | ret = -ERANGE; |
| 1687 | goto out; |
| 1688 | } |
| 1689 | /* Parse probe point */ |
| 1690 | ret = parse_perf_probe_point(argv[0], pev); |
| 1691 | if (ret < 0) |
| 1692 | goto out; |
| 1693 | |
| 1694 | /* Copy arguments and ensure return probe has no C argument */ |
| 1695 | pev->nargs = argc - 1; |
| 1696 | pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs); |
| 1697 | if (pev->args == NULL) { |
| 1698 | ret = -ENOMEM; |
| 1699 | goto out; |
| 1700 | } |
| 1701 | for (i = 0; i < pev->nargs && ret >= 0; i++) { |
| 1702 | ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]); |
| 1703 | if (ret >= 0 && |
| 1704 | is_c_varname(pev->args[i].var) && pev->point.retprobe) { |
| 1705 | semantic_error("You can't specify local variable for" |
| 1706 | " kretprobe.\n"); |
| 1707 | ret = -EINVAL; |
| 1708 | } |
| 1709 | } |
| 1710 | out: |
| 1711 | argv_free(argv); |
| 1712 | |
| 1713 | return ret; |
| 1714 | } |
| 1715 | |
| 1716 | /* Returns true if *any* ARG is either C variable, $params or $vars. */ |
| 1717 | bool perf_probe_with_var(struct perf_probe_event *pev) |
| 1718 | { |
| 1719 | int i = 0; |
| 1720 | |
| 1721 | for (i = 0; i < pev->nargs; i++) |
| 1722 | if (is_c_varname(pev->args[i].var) || |
| 1723 | !strcmp(pev->args[i].var, PROBE_ARG_PARAMS) || |
| 1724 | !strcmp(pev->args[i].var, PROBE_ARG_VARS)) |
| 1725 | return true; |
| 1726 | return false; |
| 1727 | } |
| 1728 | |
| 1729 | /* Return true if this perf_probe_event requires debuginfo */ |
| 1730 | bool perf_probe_event_need_dwarf(struct perf_probe_event *pev) |
| 1731 | { |
| 1732 | if (pev->point.file || pev->point.line || pev->point.lazy_line) |
| 1733 | return true; |
| 1734 | |
| 1735 | if (perf_probe_with_var(pev)) |
| 1736 | return true; |
| 1737 | |
| 1738 | return false; |
| 1739 | } |
| 1740 | |
| 1741 | /* Parse probe_events event into struct probe_point */ |
| 1742 | int parse_probe_trace_command(const char *cmd, struct probe_trace_event *tev) |
| 1743 | { |
| 1744 | struct probe_trace_point *tp = &tev->point; |
| 1745 | char pr; |
| 1746 | char *p; |
| 1747 | char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str; |
| 1748 | int ret, i, argc; |
| 1749 | char **argv; |
| 1750 | |
| 1751 | pr_debug("Parsing probe_events: %s\n", cmd); |
| 1752 | argv = argv_split(cmd, &argc); |
| 1753 | if (!argv) { |
| 1754 | pr_debug("Failed to split arguments.\n"); |
| 1755 | return -ENOMEM; |
| 1756 | } |
| 1757 | if (argc < 2) { |
| 1758 | semantic_error("Too few probe arguments.\n"); |
| 1759 | ret = -ERANGE; |
| 1760 | goto out; |
| 1761 | } |
| 1762 | |
| 1763 | /* Scan event and group name. */ |
| 1764 | argv0_str = strdup(argv[0]); |
| 1765 | if (argv0_str == NULL) { |
| 1766 | ret = -ENOMEM; |
| 1767 | goto out; |
| 1768 | } |
| 1769 | fmt1_str = strtok_r(argv0_str, ":", &fmt); |
| 1770 | fmt2_str = strtok_r(NULL, "/", &fmt); |
| 1771 | fmt3_str = strtok_r(NULL, " \t", &fmt); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1772 | if (fmt1_str == NULL || fmt2_str == NULL || fmt3_str == NULL) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1773 | semantic_error("Failed to parse event name: %s\n", argv[0]); |
| 1774 | ret = -EINVAL; |
| 1775 | goto out; |
| 1776 | } |
| 1777 | pr = fmt1_str[0]; |
| 1778 | tev->group = strdup(fmt2_str); |
| 1779 | tev->event = strdup(fmt3_str); |
| 1780 | if (tev->group == NULL || tev->event == NULL) { |
| 1781 | ret = -ENOMEM; |
| 1782 | goto out; |
| 1783 | } |
| 1784 | pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr); |
| 1785 | |
| 1786 | tp->retprobe = (pr == 'r'); |
| 1787 | |
| 1788 | /* Scan module name(if there), function name and offset */ |
| 1789 | p = strchr(argv[1], ':'); |
| 1790 | if (p) { |
| 1791 | tp->module = strndup(argv[1], p - argv[1]); |
| 1792 | if (!tp->module) { |
| 1793 | ret = -ENOMEM; |
| 1794 | goto out; |
| 1795 | } |
| 1796 | tev->uprobes = (tp->module[0] == '/'); |
| 1797 | p++; |
| 1798 | } else |
| 1799 | p = argv[1]; |
| 1800 | fmt1_str = strtok_r(p, "+", &fmt); |
| 1801 | /* only the address started with 0x */ |
| 1802 | if (fmt1_str[0] == '0') { |
| 1803 | /* |
| 1804 | * Fix a special case: |
| 1805 | * if address == 0, kernel reports something like: |
| 1806 | * p:probe_libc/abs_0 /lib/libc-2.18.so:0x (null) arg1=%ax |
| 1807 | * Newer kernel may fix that, but we want to |
| 1808 | * support old kernel also. |
| 1809 | */ |
| 1810 | if (strcmp(fmt1_str, "0x") == 0) { |
| 1811 | if (!argv[2] || strcmp(argv[2], "(null)")) { |
| 1812 | ret = -EINVAL; |
| 1813 | goto out; |
| 1814 | } |
| 1815 | tp->address = 0; |
| 1816 | |
| 1817 | free(argv[2]); |
| 1818 | for (i = 2; argv[i + 1] != NULL; i++) |
| 1819 | argv[i] = argv[i + 1]; |
| 1820 | |
| 1821 | argv[i] = NULL; |
| 1822 | argc -= 1; |
| 1823 | } else |
| 1824 | tp->address = strtoul(fmt1_str, NULL, 0); |
| 1825 | } else { |
| 1826 | /* Only the symbol-based probe has offset */ |
| 1827 | tp->symbol = strdup(fmt1_str); |
| 1828 | if (tp->symbol == NULL) { |
| 1829 | ret = -ENOMEM; |
| 1830 | goto out; |
| 1831 | } |
| 1832 | fmt2_str = strtok_r(NULL, "", &fmt); |
| 1833 | if (fmt2_str == NULL) |
| 1834 | tp->offset = 0; |
| 1835 | else |
| 1836 | tp->offset = strtoul(fmt2_str, NULL, 10); |
| 1837 | } |
| 1838 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1839 | if (tev->uprobes) { |
| 1840 | fmt2_str = strchr(p, '('); |
| 1841 | if (fmt2_str) |
| 1842 | tp->ref_ctr_offset = strtoul(fmt2_str + 1, NULL, 0); |
| 1843 | } |
| 1844 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1845 | tev->nargs = argc - 2; |
| 1846 | tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs); |
| 1847 | if (tev->args == NULL) { |
| 1848 | ret = -ENOMEM; |
| 1849 | goto out; |
| 1850 | } |
| 1851 | for (i = 0; i < tev->nargs; i++) { |
| 1852 | p = strchr(argv[i + 2], '='); |
| 1853 | if (p) /* We don't need which register is assigned. */ |
| 1854 | *p++ = '\0'; |
| 1855 | else |
| 1856 | p = argv[i + 2]; |
| 1857 | tev->args[i].name = strdup(argv[i + 2]); |
| 1858 | /* TODO: parse regs and offset */ |
| 1859 | tev->args[i].value = strdup(p); |
| 1860 | if (tev->args[i].name == NULL || tev->args[i].value == NULL) { |
| 1861 | ret = -ENOMEM; |
| 1862 | goto out; |
| 1863 | } |
| 1864 | } |
| 1865 | ret = 0; |
| 1866 | out: |
| 1867 | free(argv0_str); |
| 1868 | argv_free(argv); |
| 1869 | return ret; |
| 1870 | } |
| 1871 | |
| 1872 | /* Compose only probe arg */ |
| 1873 | char *synthesize_perf_probe_arg(struct perf_probe_arg *pa) |
| 1874 | { |
| 1875 | struct perf_probe_arg_field *field = pa->field; |
| 1876 | struct strbuf buf; |
| 1877 | char *ret = NULL; |
| 1878 | int err; |
| 1879 | |
| 1880 | if (strbuf_init(&buf, 64) < 0) |
| 1881 | return NULL; |
| 1882 | |
| 1883 | if (pa->name && pa->var) |
| 1884 | err = strbuf_addf(&buf, "%s=%s", pa->name, pa->var); |
| 1885 | else |
| 1886 | err = strbuf_addstr(&buf, pa->name ?: pa->var); |
| 1887 | if (err) |
| 1888 | goto out; |
| 1889 | |
| 1890 | while (field) { |
| 1891 | if (field->name[0] == '[') |
| 1892 | err = strbuf_addstr(&buf, field->name); |
| 1893 | else |
| 1894 | err = strbuf_addf(&buf, "%s%s", field->ref ? "->" : ".", |
| 1895 | field->name); |
| 1896 | field = field->next; |
| 1897 | if (err) |
| 1898 | goto out; |
| 1899 | } |
| 1900 | |
| 1901 | if (pa->type) |
| 1902 | if (strbuf_addf(&buf, ":%s", pa->type) < 0) |
| 1903 | goto out; |
| 1904 | |
| 1905 | ret = strbuf_detach(&buf, NULL); |
| 1906 | out: |
| 1907 | strbuf_release(&buf); |
| 1908 | return ret; |
| 1909 | } |
| 1910 | |
| 1911 | /* Compose only probe point (not argument) */ |
| 1912 | char *synthesize_perf_probe_point(struct perf_probe_point *pp) |
| 1913 | { |
| 1914 | struct strbuf buf; |
| 1915 | char *tmp, *ret = NULL; |
| 1916 | int len, err = 0; |
| 1917 | |
| 1918 | if (strbuf_init(&buf, 64) < 0) |
| 1919 | return NULL; |
| 1920 | |
| 1921 | if (pp->function) { |
| 1922 | if (strbuf_addstr(&buf, pp->function) < 0) |
| 1923 | goto out; |
| 1924 | if (pp->offset) |
| 1925 | err = strbuf_addf(&buf, "+%lu", pp->offset); |
| 1926 | else if (pp->line) |
| 1927 | err = strbuf_addf(&buf, ":%d", pp->line); |
| 1928 | else if (pp->retprobe) |
| 1929 | err = strbuf_addstr(&buf, "%return"); |
| 1930 | if (err) |
| 1931 | goto out; |
| 1932 | } |
| 1933 | if (pp->file) { |
| 1934 | tmp = pp->file; |
| 1935 | len = strlen(tmp); |
| 1936 | if (len > 30) { |
| 1937 | tmp = strchr(pp->file + len - 30, '/'); |
| 1938 | tmp = tmp ? tmp + 1 : pp->file + len - 30; |
| 1939 | } |
| 1940 | err = strbuf_addf(&buf, "@%s", tmp); |
| 1941 | if (!err && !pp->function && pp->line) |
| 1942 | err = strbuf_addf(&buf, ":%d", pp->line); |
| 1943 | } |
| 1944 | if (!err) |
| 1945 | ret = strbuf_detach(&buf, NULL); |
| 1946 | out: |
| 1947 | strbuf_release(&buf); |
| 1948 | return ret; |
| 1949 | } |
| 1950 | |
| 1951 | char *synthesize_perf_probe_command(struct perf_probe_event *pev) |
| 1952 | { |
| 1953 | struct strbuf buf; |
| 1954 | char *tmp, *ret = NULL; |
| 1955 | int i; |
| 1956 | |
| 1957 | if (strbuf_init(&buf, 64)) |
| 1958 | return NULL; |
| 1959 | if (pev->event) |
| 1960 | if (strbuf_addf(&buf, "%s:%s=", pev->group ?: PERFPROBE_GROUP, |
| 1961 | pev->event) < 0) |
| 1962 | goto out; |
| 1963 | |
| 1964 | tmp = synthesize_perf_probe_point(&pev->point); |
| 1965 | if (!tmp || strbuf_addstr(&buf, tmp) < 0) |
| 1966 | goto out; |
| 1967 | free(tmp); |
| 1968 | |
| 1969 | for (i = 0; i < pev->nargs; i++) { |
| 1970 | tmp = synthesize_perf_probe_arg(pev->args + i); |
| 1971 | if (!tmp || strbuf_addf(&buf, " %s", tmp) < 0) |
| 1972 | goto out; |
| 1973 | free(tmp); |
| 1974 | } |
| 1975 | |
| 1976 | ret = strbuf_detach(&buf, NULL); |
| 1977 | out: |
| 1978 | strbuf_release(&buf); |
| 1979 | return ret; |
| 1980 | } |
| 1981 | |
| 1982 | static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref, |
| 1983 | struct strbuf *buf, int depth) |
| 1984 | { |
| 1985 | int err; |
| 1986 | if (ref->next) { |
| 1987 | depth = __synthesize_probe_trace_arg_ref(ref->next, buf, |
| 1988 | depth + 1); |
| 1989 | if (depth < 0) |
| 1990 | return depth; |
| 1991 | } |
| 1992 | err = strbuf_addf(buf, "%+ld(", ref->offset); |
| 1993 | return (err < 0) ? err : depth; |
| 1994 | } |
| 1995 | |
| 1996 | static int synthesize_probe_trace_arg(struct probe_trace_arg *arg, |
| 1997 | struct strbuf *buf) |
| 1998 | { |
| 1999 | struct probe_trace_arg_ref *ref = arg->ref; |
| 2000 | int depth = 0, err; |
| 2001 | |
| 2002 | /* Argument name or separator */ |
| 2003 | if (arg->name) |
| 2004 | err = strbuf_addf(buf, " %s=", arg->name); |
| 2005 | else |
| 2006 | err = strbuf_addch(buf, ' '); |
| 2007 | if (err) |
| 2008 | return err; |
| 2009 | |
| 2010 | /* Special case: @XXX */ |
| 2011 | if (arg->value[0] == '@' && arg->ref) |
| 2012 | ref = ref->next; |
| 2013 | |
| 2014 | /* Dereferencing arguments */ |
| 2015 | if (ref) { |
| 2016 | depth = __synthesize_probe_trace_arg_ref(ref, buf, 1); |
| 2017 | if (depth < 0) |
| 2018 | return depth; |
| 2019 | } |
| 2020 | |
| 2021 | /* Print argument value */ |
| 2022 | if (arg->value[0] == '@' && arg->ref) |
| 2023 | err = strbuf_addf(buf, "%s%+ld", arg->value, arg->ref->offset); |
| 2024 | else |
| 2025 | err = strbuf_addstr(buf, arg->value); |
| 2026 | |
| 2027 | /* Closing */ |
| 2028 | while (!err && depth--) |
| 2029 | err = strbuf_addch(buf, ')'); |
| 2030 | |
| 2031 | /* Print argument type */ |
| 2032 | if (!err && arg->type) |
| 2033 | err = strbuf_addf(buf, ":%s", arg->type); |
| 2034 | |
| 2035 | return err; |
| 2036 | } |
| 2037 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2038 | static int |
| 2039 | synthesize_uprobe_trace_def(struct probe_trace_event *tev, struct strbuf *buf) |
| 2040 | { |
| 2041 | struct probe_trace_point *tp = &tev->point; |
| 2042 | int err; |
| 2043 | |
| 2044 | err = strbuf_addf(buf, "%s:0x%lx", tp->module, tp->address); |
| 2045 | |
| 2046 | if (err >= 0 && tp->ref_ctr_offset) { |
| 2047 | if (!uprobe_ref_ctr_is_supported()) |
| 2048 | return -1; |
| 2049 | err = strbuf_addf(buf, "(0x%lx)", tp->ref_ctr_offset); |
| 2050 | } |
| 2051 | return err >= 0 ? 0 : -1; |
| 2052 | } |
| 2053 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2054 | char *synthesize_probe_trace_command(struct probe_trace_event *tev) |
| 2055 | { |
| 2056 | struct probe_trace_point *tp = &tev->point; |
| 2057 | struct strbuf buf; |
| 2058 | char *ret = NULL; |
| 2059 | int i, err; |
| 2060 | |
| 2061 | /* Uprobes must have tp->module */ |
| 2062 | if (tev->uprobes && !tp->module) |
| 2063 | return NULL; |
| 2064 | |
| 2065 | if (strbuf_init(&buf, 32) < 0) |
| 2066 | return NULL; |
| 2067 | |
| 2068 | if (strbuf_addf(&buf, "%c:%s/%s ", tp->retprobe ? 'r' : 'p', |
| 2069 | tev->group, tev->event) < 0) |
| 2070 | goto error; |
| 2071 | /* |
| 2072 | * If tp->address == 0, then this point must be a |
| 2073 | * absolute address uprobe. |
| 2074 | * try_to_find_absolute_address() should have made |
| 2075 | * tp->symbol to "0x0". |
| 2076 | */ |
| 2077 | if (tev->uprobes && !tp->address) { |
| 2078 | if (!tp->symbol || strcmp(tp->symbol, "0x0")) |
| 2079 | goto error; |
| 2080 | } |
| 2081 | |
| 2082 | /* Use the tp->address for uprobes */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2083 | if (tev->uprobes) { |
| 2084 | err = synthesize_uprobe_trace_def(tev, &buf); |
| 2085 | } else if (!strncmp(tp->symbol, "0x", 2)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2086 | /* Absolute address. See try_to_find_absolute_address() */ |
| 2087 | err = strbuf_addf(&buf, "%s%s0x%lx", tp->module ?: "", |
| 2088 | tp->module ? ":" : "", tp->address); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2089 | } else { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2090 | err = strbuf_addf(&buf, "%s%s%s+%lu", tp->module ?: "", |
| 2091 | tp->module ? ":" : "", tp->symbol, tp->offset); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2092 | } |
| 2093 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2094 | if (err) |
| 2095 | goto error; |
| 2096 | |
| 2097 | for (i = 0; i < tev->nargs; i++) |
| 2098 | if (synthesize_probe_trace_arg(&tev->args[i], &buf) < 0) |
| 2099 | goto error; |
| 2100 | |
| 2101 | ret = strbuf_detach(&buf, NULL); |
| 2102 | error: |
| 2103 | strbuf_release(&buf); |
| 2104 | return ret; |
| 2105 | } |
| 2106 | |
| 2107 | static int find_perf_probe_point_from_map(struct probe_trace_point *tp, |
| 2108 | struct perf_probe_point *pp, |
| 2109 | bool is_kprobe) |
| 2110 | { |
| 2111 | struct symbol *sym = NULL; |
| 2112 | struct map *map = NULL; |
| 2113 | u64 addr = tp->address; |
| 2114 | int ret = -ENOENT; |
| 2115 | |
| 2116 | if (!is_kprobe) { |
| 2117 | map = dso__new_map(tp->module); |
| 2118 | if (!map) |
| 2119 | goto out; |
| 2120 | sym = map__find_symbol(map, addr); |
| 2121 | } else { |
| 2122 | if (tp->symbol && !addr) { |
| 2123 | if (kernel_get_symbol_address_by_name(tp->symbol, |
| 2124 | &addr, true, false) < 0) |
| 2125 | goto out; |
| 2126 | } |
| 2127 | if (addr) { |
| 2128 | addr += tp->offset; |
| 2129 | sym = machine__find_kernel_symbol(host_machine, addr, &map); |
| 2130 | } |
| 2131 | } |
| 2132 | |
| 2133 | if (!sym) |
| 2134 | goto out; |
| 2135 | |
| 2136 | pp->retprobe = tp->retprobe; |
| 2137 | pp->offset = addr - map->unmap_ip(map, sym->start); |
| 2138 | pp->function = strdup(sym->name); |
| 2139 | ret = pp->function ? 0 : -ENOMEM; |
| 2140 | |
| 2141 | out: |
| 2142 | if (map && !is_kprobe) { |
| 2143 | map__put(map); |
| 2144 | } |
| 2145 | |
| 2146 | return ret; |
| 2147 | } |
| 2148 | |
| 2149 | static int convert_to_perf_probe_point(struct probe_trace_point *tp, |
| 2150 | struct perf_probe_point *pp, |
| 2151 | bool is_kprobe) |
| 2152 | { |
| 2153 | char buf[128]; |
| 2154 | int ret; |
| 2155 | |
| 2156 | ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe); |
| 2157 | if (!ret) |
| 2158 | return 0; |
| 2159 | ret = find_perf_probe_point_from_map(tp, pp, is_kprobe); |
| 2160 | if (!ret) |
| 2161 | return 0; |
| 2162 | |
| 2163 | pr_debug("Failed to find probe point from both of dwarf and map.\n"); |
| 2164 | |
| 2165 | if (tp->symbol) { |
| 2166 | pp->function = strdup(tp->symbol); |
| 2167 | pp->offset = tp->offset; |
| 2168 | } else { |
| 2169 | ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address); |
| 2170 | if (ret < 0) |
| 2171 | return ret; |
| 2172 | pp->function = strdup(buf); |
| 2173 | pp->offset = 0; |
| 2174 | } |
| 2175 | if (pp->function == NULL) |
| 2176 | return -ENOMEM; |
| 2177 | |
| 2178 | pp->retprobe = tp->retprobe; |
| 2179 | |
| 2180 | return 0; |
| 2181 | } |
| 2182 | |
| 2183 | static int convert_to_perf_probe_event(struct probe_trace_event *tev, |
| 2184 | struct perf_probe_event *pev, bool is_kprobe) |
| 2185 | { |
| 2186 | struct strbuf buf = STRBUF_INIT; |
| 2187 | int i, ret; |
| 2188 | |
| 2189 | /* Convert event/group name */ |
| 2190 | pev->event = strdup(tev->event); |
| 2191 | pev->group = strdup(tev->group); |
| 2192 | if (pev->event == NULL || pev->group == NULL) |
| 2193 | return -ENOMEM; |
| 2194 | |
| 2195 | /* Convert trace_point to probe_point */ |
| 2196 | ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe); |
| 2197 | if (ret < 0) |
| 2198 | return ret; |
| 2199 | |
| 2200 | /* Convert trace_arg to probe_arg */ |
| 2201 | pev->nargs = tev->nargs; |
| 2202 | pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs); |
| 2203 | if (pev->args == NULL) |
| 2204 | return -ENOMEM; |
| 2205 | for (i = 0; i < tev->nargs && ret >= 0; i++) { |
| 2206 | if (tev->args[i].name) |
| 2207 | pev->args[i].name = strdup(tev->args[i].name); |
| 2208 | else { |
| 2209 | if ((ret = strbuf_init(&buf, 32)) < 0) |
| 2210 | goto error; |
| 2211 | ret = synthesize_probe_trace_arg(&tev->args[i], &buf); |
| 2212 | pev->args[i].name = strbuf_detach(&buf, NULL); |
| 2213 | } |
| 2214 | if (pev->args[i].name == NULL && ret >= 0) |
| 2215 | ret = -ENOMEM; |
| 2216 | } |
| 2217 | error: |
| 2218 | if (ret < 0) |
| 2219 | clear_perf_probe_event(pev); |
| 2220 | |
| 2221 | return ret; |
| 2222 | } |
| 2223 | |
| 2224 | void clear_perf_probe_event(struct perf_probe_event *pev) |
| 2225 | { |
| 2226 | struct perf_probe_arg_field *field, *next; |
| 2227 | int i; |
| 2228 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2229 | zfree(&pev->event); |
| 2230 | zfree(&pev->group); |
| 2231 | zfree(&pev->target); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2232 | clear_perf_probe_point(&pev->point); |
| 2233 | |
| 2234 | for (i = 0; i < pev->nargs; i++) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2235 | zfree(&pev->args[i].name); |
| 2236 | zfree(&pev->args[i].var); |
| 2237 | zfree(&pev->args[i].type); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2238 | field = pev->args[i].field; |
| 2239 | while (field) { |
| 2240 | next = field->next; |
| 2241 | zfree(&field->name); |
| 2242 | free(field); |
| 2243 | field = next; |
| 2244 | } |
| 2245 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2246 | pev->nargs = 0; |
| 2247 | zfree(&pev->args); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2248 | } |
| 2249 | |
| 2250 | #define strdup_or_goto(str, label) \ |
| 2251 | ({ char *__p = NULL; if (str && !(__p = strdup(str))) goto label; __p; }) |
| 2252 | |
| 2253 | static int perf_probe_point__copy(struct perf_probe_point *dst, |
| 2254 | struct perf_probe_point *src) |
| 2255 | { |
| 2256 | dst->file = strdup_or_goto(src->file, out_err); |
| 2257 | dst->function = strdup_or_goto(src->function, out_err); |
| 2258 | dst->lazy_line = strdup_or_goto(src->lazy_line, out_err); |
| 2259 | dst->line = src->line; |
| 2260 | dst->retprobe = src->retprobe; |
| 2261 | dst->offset = src->offset; |
| 2262 | return 0; |
| 2263 | |
| 2264 | out_err: |
| 2265 | clear_perf_probe_point(dst); |
| 2266 | return -ENOMEM; |
| 2267 | } |
| 2268 | |
| 2269 | static int perf_probe_arg__copy(struct perf_probe_arg *dst, |
| 2270 | struct perf_probe_arg *src) |
| 2271 | { |
| 2272 | struct perf_probe_arg_field *field, **ppfield; |
| 2273 | |
| 2274 | dst->name = strdup_or_goto(src->name, out_err); |
| 2275 | dst->var = strdup_or_goto(src->var, out_err); |
| 2276 | dst->type = strdup_or_goto(src->type, out_err); |
| 2277 | |
| 2278 | field = src->field; |
| 2279 | ppfield = &(dst->field); |
| 2280 | while (field) { |
| 2281 | *ppfield = zalloc(sizeof(*field)); |
| 2282 | if (!*ppfield) |
| 2283 | goto out_err; |
| 2284 | (*ppfield)->name = strdup_or_goto(field->name, out_err); |
| 2285 | (*ppfield)->index = field->index; |
| 2286 | (*ppfield)->ref = field->ref; |
| 2287 | field = field->next; |
| 2288 | ppfield = &((*ppfield)->next); |
| 2289 | } |
| 2290 | return 0; |
| 2291 | out_err: |
| 2292 | return -ENOMEM; |
| 2293 | } |
| 2294 | |
| 2295 | int perf_probe_event__copy(struct perf_probe_event *dst, |
| 2296 | struct perf_probe_event *src) |
| 2297 | { |
| 2298 | int i; |
| 2299 | |
| 2300 | dst->event = strdup_or_goto(src->event, out_err); |
| 2301 | dst->group = strdup_or_goto(src->group, out_err); |
| 2302 | dst->target = strdup_or_goto(src->target, out_err); |
| 2303 | dst->uprobes = src->uprobes; |
| 2304 | |
| 2305 | if (perf_probe_point__copy(&dst->point, &src->point) < 0) |
| 2306 | goto out_err; |
| 2307 | |
| 2308 | dst->args = zalloc(sizeof(struct perf_probe_arg) * src->nargs); |
| 2309 | if (!dst->args) |
| 2310 | goto out_err; |
| 2311 | dst->nargs = src->nargs; |
| 2312 | |
| 2313 | for (i = 0; i < src->nargs; i++) |
| 2314 | if (perf_probe_arg__copy(&dst->args[i], &src->args[i]) < 0) |
| 2315 | goto out_err; |
| 2316 | return 0; |
| 2317 | |
| 2318 | out_err: |
| 2319 | clear_perf_probe_event(dst); |
| 2320 | return -ENOMEM; |
| 2321 | } |
| 2322 | |
| 2323 | void clear_probe_trace_event(struct probe_trace_event *tev) |
| 2324 | { |
| 2325 | struct probe_trace_arg_ref *ref, *next; |
| 2326 | int i; |
| 2327 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2328 | zfree(&tev->event); |
| 2329 | zfree(&tev->group); |
| 2330 | zfree(&tev->point.symbol); |
| 2331 | zfree(&tev->point.realname); |
| 2332 | zfree(&tev->point.module); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2333 | for (i = 0; i < tev->nargs; i++) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2334 | zfree(&tev->args[i].name); |
| 2335 | zfree(&tev->args[i].value); |
| 2336 | zfree(&tev->args[i].type); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2337 | ref = tev->args[i].ref; |
| 2338 | while (ref) { |
| 2339 | next = ref->next; |
| 2340 | free(ref); |
| 2341 | ref = next; |
| 2342 | } |
| 2343 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2344 | zfree(&tev->args); |
| 2345 | tev->nargs = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2346 | } |
| 2347 | |
| 2348 | struct kprobe_blacklist_node { |
| 2349 | struct list_head list; |
| 2350 | unsigned long start; |
| 2351 | unsigned long end; |
| 2352 | char *symbol; |
| 2353 | }; |
| 2354 | |
| 2355 | static void kprobe_blacklist__delete(struct list_head *blacklist) |
| 2356 | { |
| 2357 | struct kprobe_blacklist_node *node; |
| 2358 | |
| 2359 | while (!list_empty(blacklist)) { |
| 2360 | node = list_first_entry(blacklist, |
| 2361 | struct kprobe_blacklist_node, list); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2362 | list_del_init(&node->list); |
| 2363 | zfree(&node->symbol); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2364 | free(node); |
| 2365 | } |
| 2366 | } |
| 2367 | |
| 2368 | static int kprobe_blacklist__load(struct list_head *blacklist) |
| 2369 | { |
| 2370 | struct kprobe_blacklist_node *node; |
| 2371 | const char *__debugfs = debugfs__mountpoint(); |
| 2372 | char buf[PATH_MAX], *p; |
| 2373 | FILE *fp; |
| 2374 | int ret; |
| 2375 | |
| 2376 | if (__debugfs == NULL) |
| 2377 | return -ENOTSUP; |
| 2378 | |
| 2379 | ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs); |
| 2380 | if (ret < 0) |
| 2381 | return ret; |
| 2382 | |
| 2383 | fp = fopen(buf, "r"); |
| 2384 | if (!fp) |
| 2385 | return -errno; |
| 2386 | |
| 2387 | ret = 0; |
| 2388 | while (fgets(buf, PATH_MAX, fp)) { |
| 2389 | node = zalloc(sizeof(*node)); |
| 2390 | if (!node) { |
| 2391 | ret = -ENOMEM; |
| 2392 | break; |
| 2393 | } |
| 2394 | INIT_LIST_HEAD(&node->list); |
| 2395 | list_add_tail(&node->list, blacklist); |
| 2396 | if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) { |
| 2397 | ret = -EINVAL; |
| 2398 | break; |
| 2399 | } |
| 2400 | p = strchr(buf, '\t'); |
| 2401 | if (p) { |
| 2402 | p++; |
| 2403 | if (p[strlen(p) - 1] == '\n') |
| 2404 | p[strlen(p) - 1] = '\0'; |
| 2405 | } else |
| 2406 | p = (char *)"unknown"; |
| 2407 | node->symbol = strdup(p); |
| 2408 | if (!node->symbol) { |
| 2409 | ret = -ENOMEM; |
| 2410 | break; |
| 2411 | } |
| 2412 | pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n", |
| 2413 | node->start, node->end, node->symbol); |
| 2414 | ret++; |
| 2415 | } |
| 2416 | if (ret < 0) |
| 2417 | kprobe_blacklist__delete(blacklist); |
| 2418 | fclose(fp); |
| 2419 | |
| 2420 | return ret; |
| 2421 | } |
| 2422 | |
| 2423 | static struct kprobe_blacklist_node * |
| 2424 | kprobe_blacklist__find_by_address(struct list_head *blacklist, |
| 2425 | unsigned long address) |
| 2426 | { |
| 2427 | struct kprobe_blacklist_node *node; |
| 2428 | |
| 2429 | list_for_each_entry(node, blacklist, list) { |
| 2430 | if (node->start <= address && address < node->end) |
| 2431 | return node; |
| 2432 | } |
| 2433 | |
| 2434 | return NULL; |
| 2435 | } |
| 2436 | |
| 2437 | static LIST_HEAD(kprobe_blacklist); |
| 2438 | |
| 2439 | static void kprobe_blacklist__init(void) |
| 2440 | { |
| 2441 | if (!list_empty(&kprobe_blacklist)) |
| 2442 | return; |
| 2443 | |
| 2444 | if (kprobe_blacklist__load(&kprobe_blacklist) < 0) |
| 2445 | pr_debug("No kprobe blacklist support, ignored\n"); |
| 2446 | } |
| 2447 | |
| 2448 | static void kprobe_blacklist__release(void) |
| 2449 | { |
| 2450 | kprobe_blacklist__delete(&kprobe_blacklist); |
| 2451 | } |
| 2452 | |
| 2453 | static bool kprobe_blacklist__listed(unsigned long address) |
| 2454 | { |
| 2455 | return !!kprobe_blacklist__find_by_address(&kprobe_blacklist, address); |
| 2456 | } |
| 2457 | |
| 2458 | static int perf_probe_event__sprintf(const char *group, const char *event, |
| 2459 | struct perf_probe_event *pev, |
| 2460 | const char *module, |
| 2461 | struct strbuf *result) |
| 2462 | { |
| 2463 | int i, ret; |
| 2464 | char *buf; |
| 2465 | |
| 2466 | if (asprintf(&buf, "%s:%s", group, event) < 0) |
| 2467 | return -errno; |
| 2468 | ret = strbuf_addf(result, " %-20s (on ", buf); |
| 2469 | free(buf); |
| 2470 | if (ret) |
| 2471 | return ret; |
| 2472 | |
| 2473 | /* Synthesize only event probe point */ |
| 2474 | buf = synthesize_perf_probe_point(&pev->point); |
| 2475 | if (!buf) |
| 2476 | return -ENOMEM; |
| 2477 | ret = strbuf_addstr(result, buf); |
| 2478 | free(buf); |
| 2479 | |
| 2480 | if (!ret && module) |
| 2481 | ret = strbuf_addf(result, " in %s", module); |
| 2482 | |
| 2483 | if (!ret && pev->nargs > 0) { |
| 2484 | ret = strbuf_add(result, " with", 5); |
| 2485 | for (i = 0; !ret && i < pev->nargs; i++) { |
| 2486 | buf = synthesize_perf_probe_arg(&pev->args[i]); |
| 2487 | if (!buf) |
| 2488 | return -ENOMEM; |
| 2489 | ret = strbuf_addf(result, " %s", buf); |
| 2490 | free(buf); |
| 2491 | } |
| 2492 | } |
| 2493 | if (!ret) |
| 2494 | ret = strbuf_addch(result, ')'); |
| 2495 | |
| 2496 | return ret; |
| 2497 | } |
| 2498 | |
| 2499 | /* Show an event */ |
| 2500 | int show_perf_probe_event(const char *group, const char *event, |
| 2501 | struct perf_probe_event *pev, |
| 2502 | const char *module, bool use_stdout) |
| 2503 | { |
| 2504 | struct strbuf buf = STRBUF_INIT; |
| 2505 | int ret; |
| 2506 | |
| 2507 | ret = perf_probe_event__sprintf(group, event, pev, module, &buf); |
| 2508 | if (ret >= 0) { |
| 2509 | if (use_stdout) |
| 2510 | printf("%s\n", buf.buf); |
| 2511 | else |
| 2512 | pr_info("%s\n", buf.buf); |
| 2513 | } |
| 2514 | strbuf_release(&buf); |
| 2515 | |
| 2516 | return ret; |
| 2517 | } |
| 2518 | |
| 2519 | static bool filter_probe_trace_event(struct probe_trace_event *tev, |
| 2520 | struct strfilter *filter) |
| 2521 | { |
| 2522 | char tmp[128]; |
| 2523 | |
| 2524 | /* At first, check the event name itself */ |
| 2525 | if (strfilter__compare(filter, tev->event)) |
| 2526 | return true; |
| 2527 | |
| 2528 | /* Next, check the combination of name and group */ |
| 2529 | if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0) |
| 2530 | return false; |
| 2531 | return strfilter__compare(filter, tmp); |
| 2532 | } |
| 2533 | |
| 2534 | static int __show_perf_probe_events(int fd, bool is_kprobe, |
| 2535 | struct strfilter *filter) |
| 2536 | { |
| 2537 | int ret = 0; |
| 2538 | struct probe_trace_event tev; |
| 2539 | struct perf_probe_event pev; |
| 2540 | struct strlist *rawlist; |
| 2541 | struct str_node *ent; |
| 2542 | |
| 2543 | memset(&tev, 0, sizeof(tev)); |
| 2544 | memset(&pev, 0, sizeof(pev)); |
| 2545 | |
| 2546 | rawlist = probe_file__get_rawlist(fd); |
| 2547 | if (!rawlist) |
| 2548 | return -ENOMEM; |
| 2549 | |
| 2550 | strlist__for_each_entry(ent, rawlist) { |
| 2551 | ret = parse_probe_trace_command(ent->s, &tev); |
| 2552 | if (ret >= 0) { |
| 2553 | if (!filter_probe_trace_event(&tev, filter)) |
| 2554 | goto next; |
| 2555 | ret = convert_to_perf_probe_event(&tev, &pev, |
| 2556 | is_kprobe); |
| 2557 | if (ret < 0) |
| 2558 | goto next; |
| 2559 | ret = show_perf_probe_event(pev.group, pev.event, |
| 2560 | &pev, tev.point.module, |
| 2561 | true); |
| 2562 | } |
| 2563 | next: |
| 2564 | clear_perf_probe_event(&pev); |
| 2565 | clear_probe_trace_event(&tev); |
| 2566 | if (ret < 0) |
| 2567 | break; |
| 2568 | } |
| 2569 | strlist__delete(rawlist); |
| 2570 | /* Cleanup cached debuginfo if needed */ |
| 2571 | debuginfo_cache__exit(); |
| 2572 | |
| 2573 | return ret; |
| 2574 | } |
| 2575 | |
| 2576 | /* List up current perf-probe events */ |
| 2577 | int show_perf_probe_events(struct strfilter *filter) |
| 2578 | { |
| 2579 | int kp_fd, up_fd, ret; |
| 2580 | |
| 2581 | setup_pager(); |
| 2582 | |
| 2583 | if (probe_conf.cache) |
| 2584 | return probe_cache__show_all_caches(filter); |
| 2585 | |
| 2586 | ret = init_probe_symbol_maps(false); |
| 2587 | if (ret < 0) |
| 2588 | return ret; |
| 2589 | |
| 2590 | ret = probe_file__open_both(&kp_fd, &up_fd, 0); |
| 2591 | if (ret < 0) |
| 2592 | return ret; |
| 2593 | |
| 2594 | if (kp_fd >= 0) |
| 2595 | ret = __show_perf_probe_events(kp_fd, true, filter); |
| 2596 | if (up_fd >= 0 && ret >= 0) |
| 2597 | ret = __show_perf_probe_events(up_fd, false, filter); |
| 2598 | if (kp_fd > 0) |
| 2599 | close(kp_fd); |
| 2600 | if (up_fd > 0) |
| 2601 | close(up_fd); |
| 2602 | exit_probe_symbol_maps(); |
| 2603 | |
| 2604 | return ret; |
| 2605 | } |
| 2606 | |
| 2607 | static int get_new_event_name(char *buf, size_t len, const char *base, |
| 2608 | struct strlist *namelist, bool ret_event, |
| 2609 | bool allow_suffix) |
| 2610 | { |
| 2611 | int i, ret; |
| 2612 | char *p, *nbase; |
| 2613 | |
| 2614 | if (*base == '.') |
| 2615 | base++; |
| 2616 | nbase = strdup(base); |
| 2617 | if (!nbase) |
| 2618 | return -ENOMEM; |
| 2619 | |
| 2620 | /* Cut off the dot suffixes (e.g. .const, .isra) and version suffixes */ |
| 2621 | p = strpbrk(nbase, ".@"); |
| 2622 | if (p && p != nbase) |
| 2623 | *p = '\0'; |
| 2624 | |
| 2625 | /* Try no suffix number */ |
| 2626 | ret = e_snprintf(buf, len, "%s%s", nbase, ret_event ? "__return" : ""); |
| 2627 | if (ret < 0) { |
| 2628 | pr_debug("snprintf() failed: %d\n", ret); |
| 2629 | goto out; |
| 2630 | } |
| 2631 | if (!strlist__has_entry(namelist, buf)) |
| 2632 | goto out; |
| 2633 | |
| 2634 | if (!allow_suffix) { |
| 2635 | pr_warning("Error: event \"%s\" already exists.\n" |
| 2636 | " Hint: Remove existing event by 'perf probe -d'\n" |
| 2637 | " or force duplicates by 'perf probe -f'\n" |
| 2638 | " or set 'force=yes' in BPF source.\n", |
| 2639 | buf); |
| 2640 | ret = -EEXIST; |
| 2641 | goto out; |
| 2642 | } |
| 2643 | |
| 2644 | /* Try to add suffix */ |
| 2645 | for (i = 1; i < MAX_EVENT_INDEX; i++) { |
| 2646 | ret = e_snprintf(buf, len, "%s_%d", nbase, i); |
| 2647 | if (ret < 0) { |
| 2648 | pr_debug("snprintf() failed: %d\n", ret); |
| 2649 | goto out; |
| 2650 | } |
| 2651 | if (!strlist__has_entry(namelist, buf)) |
| 2652 | break; |
| 2653 | } |
| 2654 | if (i == MAX_EVENT_INDEX) { |
| 2655 | pr_warning("Too many events are on the same function.\n"); |
| 2656 | ret = -ERANGE; |
| 2657 | } |
| 2658 | |
| 2659 | out: |
| 2660 | free(nbase); |
| 2661 | |
| 2662 | /* Final validation */ |
| 2663 | if (ret >= 0 && !is_c_func_name(buf)) { |
| 2664 | pr_warning("Internal error: \"%s\" is an invalid event name.\n", |
| 2665 | buf); |
| 2666 | ret = -EINVAL; |
| 2667 | } |
| 2668 | |
| 2669 | return ret; |
| 2670 | } |
| 2671 | |
| 2672 | /* Warn if the current kernel's uprobe implementation is old */ |
| 2673 | static void warn_uprobe_event_compat(struct probe_trace_event *tev) |
| 2674 | { |
| 2675 | int i; |
| 2676 | char *buf = synthesize_probe_trace_command(tev); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2677 | struct probe_trace_point *tp = &tev->point; |
| 2678 | |
| 2679 | if (tp->ref_ctr_offset && !uprobe_ref_ctr_is_supported()) { |
| 2680 | pr_warning("A semaphore is associated with %s:%s and " |
| 2681 | "seems your kernel doesn't support it.\n", |
| 2682 | tev->group, tev->event); |
| 2683 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2684 | |
| 2685 | /* Old uprobe event doesn't support memory dereference */ |
| 2686 | if (!tev->uprobes || tev->nargs == 0 || !buf) |
| 2687 | goto out; |
| 2688 | |
| 2689 | for (i = 0; i < tev->nargs; i++) |
| 2690 | if (strglobmatch(tev->args[i].value, "[$@+-]*")) { |
| 2691 | pr_warning("Please upgrade your kernel to at least " |
| 2692 | "3.14 to have access to feature %s\n", |
| 2693 | tev->args[i].value); |
| 2694 | break; |
| 2695 | } |
| 2696 | out: |
| 2697 | free(buf); |
| 2698 | } |
| 2699 | |
| 2700 | /* Set new name from original perf_probe_event and namelist */ |
| 2701 | static int probe_trace_event__set_name(struct probe_trace_event *tev, |
| 2702 | struct perf_probe_event *pev, |
| 2703 | struct strlist *namelist, |
| 2704 | bool allow_suffix) |
| 2705 | { |
| 2706 | const char *event, *group; |
| 2707 | char buf[64]; |
| 2708 | int ret; |
| 2709 | |
| 2710 | /* If probe_event or trace_event already have the name, reuse it */ |
| 2711 | if (pev->event && !pev->sdt) |
| 2712 | event = pev->event; |
| 2713 | else if (tev->event) |
| 2714 | event = tev->event; |
| 2715 | else { |
| 2716 | /* Or generate new one from probe point */ |
| 2717 | if (pev->point.function && |
| 2718 | (strncmp(pev->point.function, "0x", 2) != 0) && |
| 2719 | !strisglob(pev->point.function)) |
| 2720 | event = pev->point.function; |
| 2721 | else |
| 2722 | event = tev->point.realname; |
| 2723 | } |
| 2724 | if (pev->group && !pev->sdt) |
| 2725 | group = pev->group; |
| 2726 | else if (tev->group) |
| 2727 | group = tev->group; |
| 2728 | else |
| 2729 | group = PERFPROBE_GROUP; |
| 2730 | |
| 2731 | /* Get an unused new event name */ |
| 2732 | ret = get_new_event_name(buf, 64, event, namelist, |
| 2733 | tev->point.retprobe, allow_suffix); |
| 2734 | if (ret < 0) |
| 2735 | return ret; |
| 2736 | |
| 2737 | event = buf; |
| 2738 | |
| 2739 | tev->event = strdup(event); |
| 2740 | tev->group = strdup(group); |
| 2741 | if (tev->event == NULL || tev->group == NULL) |
| 2742 | return -ENOMEM; |
| 2743 | |
| 2744 | /* Add added event name to namelist */ |
| 2745 | strlist__add(namelist, event); |
| 2746 | return 0; |
| 2747 | } |
| 2748 | |
| 2749 | static int __open_probe_file_and_namelist(bool uprobe, |
| 2750 | struct strlist **namelist) |
| 2751 | { |
| 2752 | int fd; |
| 2753 | |
| 2754 | fd = probe_file__open(PF_FL_RW | (uprobe ? PF_FL_UPROBE : 0)); |
| 2755 | if (fd < 0) |
| 2756 | return fd; |
| 2757 | |
| 2758 | /* Get current event names */ |
| 2759 | *namelist = probe_file__get_namelist(fd); |
| 2760 | if (!(*namelist)) { |
| 2761 | pr_debug("Failed to get current event list.\n"); |
| 2762 | close(fd); |
| 2763 | return -ENOMEM; |
| 2764 | } |
| 2765 | return fd; |
| 2766 | } |
| 2767 | |
| 2768 | static int __add_probe_trace_events(struct perf_probe_event *pev, |
| 2769 | struct probe_trace_event *tevs, |
| 2770 | int ntevs, bool allow_suffix) |
| 2771 | { |
| 2772 | int i, fd[2] = {-1, -1}, up, ret; |
| 2773 | struct probe_trace_event *tev = NULL; |
| 2774 | struct probe_cache *cache = NULL; |
| 2775 | struct strlist *namelist[2] = {NULL, NULL}; |
| 2776 | struct nscookie nsc; |
| 2777 | |
| 2778 | up = pev->uprobes ? 1 : 0; |
| 2779 | fd[up] = __open_probe_file_and_namelist(up, &namelist[up]); |
| 2780 | if (fd[up] < 0) |
| 2781 | return fd[up]; |
| 2782 | |
| 2783 | ret = 0; |
| 2784 | for (i = 0; i < ntevs; i++) { |
| 2785 | tev = &tevs[i]; |
| 2786 | up = tev->uprobes ? 1 : 0; |
| 2787 | if (fd[up] == -1) { /* Open the kprobe/uprobe_events */ |
| 2788 | fd[up] = __open_probe_file_and_namelist(up, |
| 2789 | &namelist[up]); |
| 2790 | if (fd[up] < 0) |
| 2791 | goto close_out; |
| 2792 | } |
| 2793 | /* Skip if the symbol is out of .text or blacklisted */ |
| 2794 | if (!tev->point.symbol && !pev->uprobes) |
| 2795 | continue; |
| 2796 | |
| 2797 | /* Set new name for tev (and update namelist) */ |
| 2798 | ret = probe_trace_event__set_name(tev, pev, namelist[up], |
| 2799 | allow_suffix); |
| 2800 | if (ret < 0) |
| 2801 | break; |
| 2802 | |
| 2803 | nsinfo__mountns_enter(pev->nsi, &nsc); |
| 2804 | ret = probe_file__add_event(fd[up], tev); |
| 2805 | nsinfo__mountns_exit(&nsc); |
| 2806 | if (ret < 0) |
| 2807 | break; |
| 2808 | |
| 2809 | /* |
| 2810 | * Probes after the first probe which comes from same |
| 2811 | * user input are always allowed to add suffix, because |
| 2812 | * there might be several addresses corresponding to |
| 2813 | * one code line. |
| 2814 | */ |
| 2815 | allow_suffix = true; |
| 2816 | } |
| 2817 | if (ret == -EINVAL && pev->uprobes) |
| 2818 | warn_uprobe_event_compat(tev); |
| 2819 | if (ret == 0 && probe_conf.cache) { |
| 2820 | cache = probe_cache__new(pev->target, pev->nsi); |
| 2821 | if (!cache || |
| 2822 | probe_cache__add_entry(cache, pev, tevs, ntevs) < 0 || |
| 2823 | probe_cache__commit(cache) < 0) |
| 2824 | pr_warning("Failed to add event to probe cache\n"); |
| 2825 | probe_cache__delete(cache); |
| 2826 | } |
| 2827 | |
| 2828 | close_out: |
| 2829 | for (up = 0; up < 2; up++) { |
| 2830 | strlist__delete(namelist[up]); |
| 2831 | if (fd[up] >= 0) |
| 2832 | close(fd[up]); |
| 2833 | } |
| 2834 | return ret; |
| 2835 | } |
| 2836 | |
| 2837 | static int find_probe_functions(struct map *map, char *name, |
| 2838 | struct symbol **syms) |
| 2839 | { |
| 2840 | int found = 0; |
| 2841 | struct symbol *sym; |
| 2842 | struct rb_node *tmp; |
| 2843 | const char *norm, *ver; |
| 2844 | char *buf = NULL; |
| 2845 | bool cut_version = true; |
| 2846 | |
| 2847 | if (map__load(map) < 0) |
| 2848 | return 0; |
| 2849 | |
| 2850 | /* If user gives a version, don't cut off the version from symbols */ |
| 2851 | if (strchr(name, '@')) |
| 2852 | cut_version = false; |
| 2853 | |
| 2854 | map__for_each_symbol(map, sym, tmp) { |
| 2855 | norm = arch__normalize_symbol_name(sym->name); |
| 2856 | if (!norm) |
| 2857 | continue; |
| 2858 | |
| 2859 | if (cut_version) { |
| 2860 | /* We don't care about default symbol or not */ |
| 2861 | ver = strchr(norm, '@'); |
| 2862 | if (ver) { |
| 2863 | buf = strndup(norm, ver - norm); |
| 2864 | if (!buf) |
| 2865 | return -ENOMEM; |
| 2866 | norm = buf; |
| 2867 | } |
| 2868 | } |
| 2869 | |
| 2870 | if (strglobmatch(norm, name)) { |
| 2871 | found++; |
| 2872 | if (syms && found < probe_conf.max_probes) |
| 2873 | syms[found - 1] = sym; |
| 2874 | } |
| 2875 | if (buf) |
| 2876 | zfree(&buf); |
| 2877 | } |
| 2878 | |
| 2879 | return found; |
| 2880 | } |
| 2881 | |
| 2882 | void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused, |
| 2883 | struct probe_trace_event *tev __maybe_unused, |
| 2884 | struct map *map __maybe_unused, |
| 2885 | struct symbol *sym __maybe_unused) { } |
| 2886 | |
| 2887 | /* |
| 2888 | * Find probe function addresses from map. |
| 2889 | * Return an error or the number of found probe_trace_event |
| 2890 | */ |
| 2891 | static int find_probe_trace_events_from_map(struct perf_probe_event *pev, |
| 2892 | struct probe_trace_event **tevs) |
| 2893 | { |
| 2894 | struct map *map = NULL; |
| 2895 | struct ref_reloc_sym *reloc_sym = NULL; |
| 2896 | struct symbol *sym; |
| 2897 | struct symbol **syms = NULL; |
| 2898 | struct probe_trace_event *tev; |
| 2899 | struct perf_probe_point *pp = &pev->point; |
| 2900 | struct probe_trace_point *tp; |
| 2901 | int num_matched_functions; |
| 2902 | int ret, i, j, skipped = 0; |
| 2903 | char *mod_name; |
| 2904 | |
| 2905 | map = get_target_map(pev->target, pev->nsi, pev->uprobes); |
| 2906 | if (!map) { |
| 2907 | ret = -EINVAL; |
| 2908 | goto out; |
| 2909 | } |
| 2910 | |
| 2911 | syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes); |
| 2912 | if (!syms) { |
| 2913 | ret = -ENOMEM; |
| 2914 | goto out; |
| 2915 | } |
| 2916 | |
| 2917 | /* |
| 2918 | * Load matched symbols: Since the different local symbols may have |
| 2919 | * same name but different addresses, this lists all the symbols. |
| 2920 | */ |
| 2921 | num_matched_functions = find_probe_functions(map, pp->function, syms); |
| 2922 | if (num_matched_functions <= 0) { |
| 2923 | pr_err("Failed to find symbol %s in %s\n", pp->function, |
| 2924 | pev->target ? : "kernel"); |
| 2925 | ret = -ENOENT; |
| 2926 | goto out; |
| 2927 | } else if (num_matched_functions > probe_conf.max_probes) { |
| 2928 | pr_err("Too many functions matched in %s\n", |
| 2929 | pev->target ? : "kernel"); |
| 2930 | ret = -E2BIG; |
| 2931 | goto out; |
| 2932 | } |
| 2933 | |
| 2934 | /* Note that the symbols in the kmodule are not relocated */ |
| 2935 | if (!pev->uprobes && !pev->target && |
| 2936 | (!pp->retprobe || kretprobe_offset_is_supported())) { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 2937 | reloc_sym = kernel_get_ref_reloc_sym(NULL); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2938 | if (!reloc_sym) { |
| 2939 | pr_warning("Relocated base symbol is not found!\n"); |
| 2940 | ret = -EINVAL; |
| 2941 | goto out; |
| 2942 | } |
| 2943 | } |
| 2944 | |
| 2945 | /* Setup result trace-probe-events */ |
| 2946 | *tevs = zalloc(sizeof(*tev) * num_matched_functions); |
| 2947 | if (!*tevs) { |
| 2948 | ret = -ENOMEM; |
| 2949 | goto out; |
| 2950 | } |
| 2951 | |
| 2952 | ret = 0; |
| 2953 | |
| 2954 | for (j = 0; j < num_matched_functions; j++) { |
| 2955 | sym = syms[j]; |
| 2956 | |
| 2957 | tev = (*tevs) + ret; |
| 2958 | tp = &tev->point; |
| 2959 | if (ret == num_matched_functions) { |
| 2960 | pr_warning("Too many symbols are listed. Skip it.\n"); |
| 2961 | break; |
| 2962 | } |
| 2963 | ret++; |
| 2964 | |
| 2965 | if (pp->offset > sym->end - sym->start) { |
| 2966 | pr_warning("Offset %ld is bigger than the size of %s\n", |
| 2967 | pp->offset, sym->name); |
| 2968 | ret = -ENOENT; |
| 2969 | goto err_out; |
| 2970 | } |
| 2971 | /* Add one probe point */ |
| 2972 | tp->address = map->unmap_ip(map, sym->start) + pp->offset; |
| 2973 | |
| 2974 | /* Check the kprobe (not in module) is within .text */ |
| 2975 | if (!pev->uprobes && !pev->target && |
| 2976 | kprobe_warn_out_range(sym->name, tp->address)) { |
| 2977 | tp->symbol = NULL; /* Skip it */ |
| 2978 | skipped++; |
| 2979 | } else if (reloc_sym) { |
| 2980 | tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out); |
| 2981 | tp->offset = tp->address - reloc_sym->addr; |
| 2982 | } else { |
| 2983 | tp->symbol = strdup_or_goto(sym->name, nomem_out); |
| 2984 | tp->offset = pp->offset; |
| 2985 | } |
| 2986 | tp->realname = strdup_or_goto(sym->name, nomem_out); |
| 2987 | |
| 2988 | tp->retprobe = pp->retprobe; |
| 2989 | if (pev->target) { |
| 2990 | if (pev->uprobes) { |
| 2991 | tev->point.module = strdup_or_goto(pev->target, |
| 2992 | nomem_out); |
| 2993 | } else { |
| 2994 | mod_name = find_module_name(pev->target); |
| 2995 | tev->point.module = |
| 2996 | strdup(mod_name ? mod_name : pev->target); |
| 2997 | free(mod_name); |
| 2998 | if (!tev->point.module) |
| 2999 | goto nomem_out; |
| 3000 | } |
| 3001 | } |
| 3002 | tev->uprobes = pev->uprobes; |
| 3003 | tev->nargs = pev->nargs; |
| 3004 | if (tev->nargs) { |
| 3005 | tev->args = zalloc(sizeof(struct probe_trace_arg) * |
| 3006 | tev->nargs); |
| 3007 | if (tev->args == NULL) |
| 3008 | goto nomem_out; |
| 3009 | } |
| 3010 | for (i = 0; i < tev->nargs; i++) { |
| 3011 | if (pev->args[i].name) |
| 3012 | tev->args[i].name = |
| 3013 | strdup_or_goto(pev->args[i].name, |
| 3014 | nomem_out); |
| 3015 | |
| 3016 | tev->args[i].value = strdup_or_goto(pev->args[i].var, |
| 3017 | nomem_out); |
| 3018 | if (pev->args[i].type) |
| 3019 | tev->args[i].type = |
| 3020 | strdup_or_goto(pev->args[i].type, |
| 3021 | nomem_out); |
| 3022 | } |
| 3023 | arch__fix_tev_from_maps(pev, tev, map, sym); |
| 3024 | } |
| 3025 | if (ret == skipped) { |
| 3026 | ret = -ENOENT; |
| 3027 | goto err_out; |
| 3028 | } |
| 3029 | |
| 3030 | out: |
| 3031 | map__put(map); |
| 3032 | free(syms); |
| 3033 | return ret; |
| 3034 | |
| 3035 | nomem_out: |
| 3036 | ret = -ENOMEM; |
| 3037 | err_out: |
| 3038 | clear_probe_trace_events(*tevs, num_matched_functions); |
| 3039 | zfree(tevs); |
| 3040 | goto out; |
| 3041 | } |
| 3042 | |
| 3043 | static int try_to_find_absolute_address(struct perf_probe_event *pev, |
| 3044 | struct probe_trace_event **tevs) |
| 3045 | { |
| 3046 | struct perf_probe_point *pp = &pev->point; |
| 3047 | struct probe_trace_event *tev; |
| 3048 | struct probe_trace_point *tp; |
| 3049 | int i, err; |
| 3050 | |
| 3051 | if (!(pev->point.function && !strncmp(pev->point.function, "0x", 2))) |
| 3052 | return -EINVAL; |
| 3053 | if (perf_probe_event_need_dwarf(pev)) |
| 3054 | return -EINVAL; |
| 3055 | |
| 3056 | /* |
| 3057 | * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at |
| 3058 | * absolute address. |
| 3059 | * |
| 3060 | * Only one tev can be generated by this. |
| 3061 | */ |
| 3062 | *tevs = zalloc(sizeof(*tev)); |
| 3063 | if (!*tevs) |
| 3064 | return -ENOMEM; |
| 3065 | |
| 3066 | tev = *tevs; |
| 3067 | tp = &tev->point; |
| 3068 | |
| 3069 | /* |
| 3070 | * Don't use tp->offset, use address directly, because |
| 3071 | * in synthesize_probe_trace_command() address cannot be |
| 3072 | * zero. |
| 3073 | */ |
| 3074 | tp->address = pev->point.abs_address; |
| 3075 | tp->retprobe = pp->retprobe; |
| 3076 | tev->uprobes = pev->uprobes; |
| 3077 | |
| 3078 | err = -ENOMEM; |
| 3079 | /* |
| 3080 | * Give it a '0x' leading symbol name. |
| 3081 | * In __add_probe_trace_events, a NULL symbol is interpreted as |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3082 | * invalid. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3083 | */ |
| 3084 | if (asprintf(&tp->symbol, "0x%lx", tp->address) < 0) |
| 3085 | goto errout; |
| 3086 | |
| 3087 | /* For kprobe, check range */ |
| 3088 | if ((!tev->uprobes) && |
| 3089 | (kprobe_warn_out_range(tev->point.symbol, |
| 3090 | tev->point.address))) { |
| 3091 | err = -EACCES; |
| 3092 | goto errout; |
| 3093 | } |
| 3094 | |
| 3095 | if (asprintf(&tp->realname, "abs_%lx", tp->address) < 0) |
| 3096 | goto errout; |
| 3097 | |
| 3098 | if (pev->target) { |
| 3099 | tp->module = strdup(pev->target); |
| 3100 | if (!tp->module) |
| 3101 | goto errout; |
| 3102 | } |
| 3103 | |
| 3104 | if (tev->group) { |
| 3105 | tev->group = strdup(pev->group); |
| 3106 | if (!tev->group) |
| 3107 | goto errout; |
| 3108 | } |
| 3109 | |
| 3110 | if (pev->event) { |
| 3111 | tev->event = strdup(pev->event); |
| 3112 | if (!tev->event) |
| 3113 | goto errout; |
| 3114 | } |
| 3115 | |
| 3116 | tev->nargs = pev->nargs; |
| 3117 | tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs); |
| 3118 | if (!tev->args) |
| 3119 | goto errout; |
| 3120 | |
| 3121 | for (i = 0; i < tev->nargs; i++) |
| 3122 | copy_to_probe_trace_arg(&tev->args[i], &pev->args[i]); |
| 3123 | |
| 3124 | return 1; |
| 3125 | |
| 3126 | errout: |
| 3127 | clear_probe_trace_events(*tevs, 1); |
| 3128 | *tevs = NULL; |
| 3129 | return err; |
| 3130 | } |
| 3131 | |
| 3132 | /* Concatinate two arrays */ |
| 3133 | static void *memcat(void *a, size_t sz_a, void *b, size_t sz_b) |
| 3134 | { |
| 3135 | void *ret; |
| 3136 | |
| 3137 | ret = malloc(sz_a + sz_b); |
| 3138 | if (ret) { |
| 3139 | memcpy(ret, a, sz_a); |
| 3140 | memcpy(ret + sz_a, b, sz_b); |
| 3141 | } |
| 3142 | return ret; |
| 3143 | } |
| 3144 | |
| 3145 | static int |
| 3146 | concat_probe_trace_events(struct probe_trace_event **tevs, int *ntevs, |
| 3147 | struct probe_trace_event **tevs2, int ntevs2) |
| 3148 | { |
| 3149 | struct probe_trace_event *new_tevs; |
| 3150 | int ret = 0; |
| 3151 | |
| 3152 | if (*ntevs == 0) { |
| 3153 | *tevs = *tevs2; |
| 3154 | *ntevs = ntevs2; |
| 3155 | *tevs2 = NULL; |
| 3156 | return 0; |
| 3157 | } |
| 3158 | |
| 3159 | if (*ntevs + ntevs2 > probe_conf.max_probes) |
| 3160 | ret = -E2BIG; |
| 3161 | else { |
| 3162 | /* Concatinate the array of probe_trace_event */ |
| 3163 | new_tevs = memcat(*tevs, (*ntevs) * sizeof(**tevs), |
| 3164 | *tevs2, ntevs2 * sizeof(**tevs2)); |
| 3165 | if (!new_tevs) |
| 3166 | ret = -ENOMEM; |
| 3167 | else { |
| 3168 | free(*tevs); |
| 3169 | *tevs = new_tevs; |
| 3170 | *ntevs += ntevs2; |
| 3171 | } |
| 3172 | } |
| 3173 | if (ret < 0) |
| 3174 | clear_probe_trace_events(*tevs2, ntevs2); |
| 3175 | zfree(tevs2); |
| 3176 | |
| 3177 | return ret; |
| 3178 | } |
| 3179 | |
| 3180 | /* |
| 3181 | * Try to find probe_trace_event from given probe caches. Return the number |
| 3182 | * of cached events found, if an error occurs return the error. |
| 3183 | */ |
| 3184 | static int find_cached_events(struct perf_probe_event *pev, |
| 3185 | struct probe_trace_event **tevs, |
| 3186 | const char *target) |
| 3187 | { |
| 3188 | struct probe_cache *cache; |
| 3189 | struct probe_cache_entry *entry; |
| 3190 | struct probe_trace_event *tmp_tevs = NULL; |
| 3191 | int ntevs = 0; |
| 3192 | int ret = 0; |
| 3193 | |
| 3194 | cache = probe_cache__new(target, pev->nsi); |
| 3195 | /* Return 0 ("not found") if the target has no probe cache. */ |
| 3196 | if (!cache) |
| 3197 | return 0; |
| 3198 | |
| 3199 | for_each_probe_cache_entry(entry, cache) { |
| 3200 | /* Skip the cache entry which has no name */ |
| 3201 | if (!entry->pev.event || !entry->pev.group) |
| 3202 | continue; |
| 3203 | if ((!pev->group || strglobmatch(entry->pev.group, pev->group)) && |
| 3204 | strglobmatch(entry->pev.event, pev->event)) { |
| 3205 | ret = probe_cache_entry__get_event(entry, &tmp_tevs); |
| 3206 | if (ret > 0) |
| 3207 | ret = concat_probe_trace_events(tevs, &ntevs, |
| 3208 | &tmp_tevs, ret); |
| 3209 | if (ret < 0) |
| 3210 | break; |
| 3211 | } |
| 3212 | } |
| 3213 | probe_cache__delete(cache); |
| 3214 | if (ret < 0) { |
| 3215 | clear_probe_trace_events(*tevs, ntevs); |
| 3216 | zfree(tevs); |
| 3217 | } else { |
| 3218 | ret = ntevs; |
| 3219 | if (ntevs > 0 && target && target[0] == '/') |
| 3220 | pev->uprobes = true; |
| 3221 | } |
| 3222 | |
| 3223 | return ret; |
| 3224 | } |
| 3225 | |
| 3226 | /* Try to find probe_trace_event from all probe caches */ |
| 3227 | static int find_cached_events_all(struct perf_probe_event *pev, |
| 3228 | struct probe_trace_event **tevs) |
| 3229 | { |
| 3230 | struct probe_trace_event *tmp_tevs = NULL; |
| 3231 | struct strlist *bidlist; |
| 3232 | struct str_node *nd; |
| 3233 | char *pathname; |
| 3234 | int ntevs = 0; |
| 3235 | int ret; |
| 3236 | |
| 3237 | /* Get the buildid list of all valid caches */ |
| 3238 | bidlist = build_id_cache__list_all(true); |
| 3239 | if (!bidlist) { |
| 3240 | ret = -errno; |
| 3241 | pr_debug("Failed to get buildids: %d\n", ret); |
| 3242 | return ret; |
| 3243 | } |
| 3244 | |
| 3245 | ret = 0; |
| 3246 | strlist__for_each_entry(nd, bidlist) { |
| 3247 | pathname = build_id_cache__origname(nd->s); |
| 3248 | ret = find_cached_events(pev, &tmp_tevs, pathname); |
| 3249 | /* In the case of cnt == 0, we just skip it */ |
| 3250 | if (ret > 0) |
| 3251 | ret = concat_probe_trace_events(tevs, &ntevs, |
| 3252 | &tmp_tevs, ret); |
| 3253 | free(pathname); |
| 3254 | if (ret < 0) |
| 3255 | break; |
| 3256 | } |
| 3257 | strlist__delete(bidlist); |
| 3258 | |
| 3259 | if (ret < 0) { |
| 3260 | clear_probe_trace_events(*tevs, ntevs); |
| 3261 | zfree(tevs); |
| 3262 | } else |
| 3263 | ret = ntevs; |
| 3264 | |
| 3265 | return ret; |
| 3266 | } |
| 3267 | |
| 3268 | static int find_probe_trace_events_from_cache(struct perf_probe_event *pev, |
| 3269 | struct probe_trace_event **tevs) |
| 3270 | { |
| 3271 | struct probe_cache *cache; |
| 3272 | struct probe_cache_entry *entry; |
| 3273 | struct probe_trace_event *tev; |
| 3274 | struct str_node *node; |
| 3275 | int ret, i; |
| 3276 | |
| 3277 | if (pev->sdt) { |
| 3278 | /* For SDT/cached events, we use special search functions */ |
| 3279 | if (!pev->target) |
| 3280 | return find_cached_events_all(pev, tevs); |
| 3281 | else |
| 3282 | return find_cached_events(pev, tevs, pev->target); |
| 3283 | } |
| 3284 | cache = probe_cache__new(pev->target, pev->nsi); |
| 3285 | if (!cache) |
| 3286 | return 0; |
| 3287 | |
| 3288 | entry = probe_cache__find(cache, pev); |
| 3289 | if (!entry) { |
| 3290 | /* SDT must be in the cache */ |
| 3291 | ret = pev->sdt ? -ENOENT : 0; |
| 3292 | goto out; |
| 3293 | } |
| 3294 | |
| 3295 | ret = strlist__nr_entries(entry->tevlist); |
| 3296 | if (ret > probe_conf.max_probes) { |
| 3297 | pr_debug("Too many entries matched in the cache of %s\n", |
| 3298 | pev->target ? : "kernel"); |
| 3299 | ret = -E2BIG; |
| 3300 | goto out; |
| 3301 | } |
| 3302 | |
| 3303 | *tevs = zalloc(ret * sizeof(*tev)); |
| 3304 | if (!*tevs) { |
| 3305 | ret = -ENOMEM; |
| 3306 | goto out; |
| 3307 | } |
| 3308 | |
| 3309 | i = 0; |
| 3310 | strlist__for_each_entry(node, entry->tevlist) { |
| 3311 | tev = &(*tevs)[i++]; |
| 3312 | ret = parse_probe_trace_command(node->s, tev); |
| 3313 | if (ret < 0) |
| 3314 | goto out; |
| 3315 | /* Set the uprobes attribute as same as original */ |
| 3316 | tev->uprobes = pev->uprobes; |
| 3317 | } |
| 3318 | ret = i; |
| 3319 | |
| 3320 | out: |
| 3321 | probe_cache__delete(cache); |
| 3322 | return ret; |
| 3323 | } |
| 3324 | |
| 3325 | static int convert_to_probe_trace_events(struct perf_probe_event *pev, |
| 3326 | struct probe_trace_event **tevs) |
| 3327 | { |
| 3328 | int ret; |
| 3329 | |
| 3330 | if (!pev->group && !pev->sdt) { |
| 3331 | /* Set group name if not given */ |
| 3332 | if (!pev->uprobes) { |
| 3333 | pev->group = strdup(PERFPROBE_GROUP); |
| 3334 | ret = pev->group ? 0 : -ENOMEM; |
| 3335 | } else |
| 3336 | ret = convert_exec_to_group(pev->target, &pev->group); |
| 3337 | if (ret != 0) { |
| 3338 | pr_warning("Failed to make a group name.\n"); |
| 3339 | return ret; |
| 3340 | } |
| 3341 | } |
| 3342 | |
| 3343 | ret = try_to_find_absolute_address(pev, tevs); |
| 3344 | if (ret > 0) |
| 3345 | return ret; |
| 3346 | |
| 3347 | /* At first, we need to lookup cache entry */ |
| 3348 | ret = find_probe_trace_events_from_cache(pev, tevs); |
| 3349 | if (ret > 0 || pev->sdt) /* SDT can be found only in the cache */ |
| 3350 | return ret == 0 ? -ENOENT : ret; /* Found in probe cache */ |
| 3351 | |
| 3352 | /* Convert perf_probe_event with debuginfo */ |
| 3353 | ret = try_to_find_probe_trace_events(pev, tevs); |
| 3354 | if (ret != 0) |
| 3355 | return ret; /* Found in debuginfo or got an error */ |
| 3356 | |
| 3357 | return find_probe_trace_events_from_map(pev, tevs); |
| 3358 | } |
| 3359 | |
| 3360 | int convert_perf_probe_events(struct perf_probe_event *pevs, int npevs) |
| 3361 | { |
| 3362 | int i, ret; |
| 3363 | |
| 3364 | /* Loop 1: convert all events */ |
| 3365 | for (i = 0; i < npevs; i++) { |
| 3366 | /* Init kprobe blacklist if needed */ |
| 3367 | if (!pevs[i].uprobes) |
| 3368 | kprobe_blacklist__init(); |
| 3369 | /* Convert with or without debuginfo */ |
| 3370 | ret = convert_to_probe_trace_events(&pevs[i], &pevs[i].tevs); |
| 3371 | if (ret < 0) |
| 3372 | return ret; |
| 3373 | pevs[i].ntevs = ret; |
| 3374 | } |
| 3375 | /* This just release blacklist only if allocated */ |
| 3376 | kprobe_blacklist__release(); |
| 3377 | |
| 3378 | return 0; |
| 3379 | } |
| 3380 | |
| 3381 | static int show_probe_trace_event(struct probe_trace_event *tev) |
| 3382 | { |
| 3383 | char *buf = synthesize_probe_trace_command(tev); |
| 3384 | |
| 3385 | if (!buf) { |
| 3386 | pr_debug("Failed to synthesize probe trace event.\n"); |
| 3387 | return -EINVAL; |
| 3388 | } |
| 3389 | |
| 3390 | /* Showing definition always go stdout */ |
| 3391 | printf("%s\n", buf); |
| 3392 | free(buf); |
| 3393 | |
| 3394 | return 0; |
| 3395 | } |
| 3396 | |
| 3397 | int show_probe_trace_events(struct perf_probe_event *pevs, int npevs) |
| 3398 | { |
| 3399 | struct strlist *namelist = strlist__new(NULL, NULL); |
| 3400 | struct probe_trace_event *tev; |
| 3401 | struct perf_probe_event *pev; |
| 3402 | int i, j, ret = 0; |
| 3403 | |
| 3404 | if (!namelist) |
| 3405 | return -ENOMEM; |
| 3406 | |
| 3407 | for (j = 0; j < npevs && !ret; j++) { |
| 3408 | pev = &pevs[j]; |
| 3409 | for (i = 0; i < pev->ntevs && !ret; i++) { |
| 3410 | tev = &pev->tevs[i]; |
| 3411 | /* Skip if the symbol is out of .text or blacklisted */ |
| 3412 | if (!tev->point.symbol && !pev->uprobes) |
| 3413 | continue; |
| 3414 | |
| 3415 | /* Set new name for tev (and update namelist) */ |
| 3416 | ret = probe_trace_event__set_name(tev, pev, |
| 3417 | namelist, true); |
| 3418 | if (!ret) |
| 3419 | ret = show_probe_trace_event(tev); |
| 3420 | } |
| 3421 | } |
| 3422 | strlist__delete(namelist); |
| 3423 | |
| 3424 | return ret; |
| 3425 | } |
| 3426 | |
| 3427 | int apply_perf_probe_events(struct perf_probe_event *pevs, int npevs) |
| 3428 | { |
| 3429 | int i, ret = 0; |
| 3430 | |
| 3431 | /* Loop 2: add all events */ |
| 3432 | for (i = 0; i < npevs; i++) { |
| 3433 | ret = __add_probe_trace_events(&pevs[i], pevs[i].tevs, |
| 3434 | pevs[i].ntevs, |
| 3435 | probe_conf.force_add); |
| 3436 | if (ret < 0) |
| 3437 | break; |
| 3438 | } |
| 3439 | return ret; |
| 3440 | } |
| 3441 | |
| 3442 | void cleanup_perf_probe_events(struct perf_probe_event *pevs, int npevs) |
| 3443 | { |
| 3444 | int i, j; |
| 3445 | struct perf_probe_event *pev; |
| 3446 | |
| 3447 | /* Loop 3: cleanup and free trace events */ |
| 3448 | for (i = 0; i < npevs; i++) { |
| 3449 | pev = &pevs[i]; |
| 3450 | for (j = 0; j < pevs[i].ntevs; j++) |
| 3451 | clear_probe_trace_event(&pevs[i].tevs[j]); |
| 3452 | zfree(&pevs[i].tevs); |
| 3453 | pevs[i].ntevs = 0; |
| 3454 | nsinfo__zput(pev->nsi); |
| 3455 | clear_perf_probe_event(&pevs[i]); |
| 3456 | } |
| 3457 | } |
| 3458 | |
| 3459 | int add_perf_probe_events(struct perf_probe_event *pevs, int npevs) |
| 3460 | { |
| 3461 | int ret; |
| 3462 | |
| 3463 | ret = init_probe_symbol_maps(pevs->uprobes); |
| 3464 | if (ret < 0) |
| 3465 | return ret; |
| 3466 | |
| 3467 | ret = convert_perf_probe_events(pevs, npevs); |
| 3468 | if (ret == 0) |
| 3469 | ret = apply_perf_probe_events(pevs, npevs); |
| 3470 | |
| 3471 | cleanup_perf_probe_events(pevs, npevs); |
| 3472 | |
| 3473 | exit_probe_symbol_maps(); |
| 3474 | return ret; |
| 3475 | } |
| 3476 | |
| 3477 | int del_perf_probe_events(struct strfilter *filter) |
| 3478 | { |
| 3479 | int ret, ret2, ufd = -1, kfd = -1; |
| 3480 | char *str = strfilter__string(filter); |
| 3481 | |
| 3482 | if (!str) |
| 3483 | return -EINVAL; |
| 3484 | |
| 3485 | /* Get current event names */ |
| 3486 | ret = probe_file__open_both(&kfd, &ufd, PF_FL_RW); |
| 3487 | if (ret < 0) |
| 3488 | goto out; |
| 3489 | |
| 3490 | ret = probe_file__del_events(kfd, filter); |
| 3491 | if (ret < 0 && ret != -ENOENT) |
| 3492 | goto error; |
| 3493 | |
| 3494 | ret2 = probe_file__del_events(ufd, filter); |
| 3495 | if (ret2 < 0 && ret2 != -ENOENT) { |
| 3496 | ret = ret2; |
| 3497 | goto error; |
| 3498 | } |
| 3499 | ret = 0; |
| 3500 | |
| 3501 | error: |
| 3502 | if (kfd >= 0) |
| 3503 | close(kfd); |
| 3504 | if (ufd >= 0) |
| 3505 | close(ufd); |
| 3506 | out: |
| 3507 | free(str); |
| 3508 | |
| 3509 | return ret; |
| 3510 | } |
| 3511 | |
| 3512 | int show_available_funcs(const char *target, struct nsinfo *nsi, |
| 3513 | struct strfilter *_filter, bool user) |
| 3514 | { |
| 3515 | struct rb_node *nd; |
| 3516 | struct map *map; |
| 3517 | int ret; |
| 3518 | |
| 3519 | ret = init_probe_symbol_maps(user); |
| 3520 | if (ret < 0) |
| 3521 | return ret; |
| 3522 | |
| 3523 | /* Get a symbol map */ |
| 3524 | map = get_target_map(target, nsi, user); |
| 3525 | if (!map) { |
| 3526 | pr_err("Failed to get a map for %s\n", (target) ? : "kernel"); |
| 3527 | return -EINVAL; |
| 3528 | } |
| 3529 | |
| 3530 | ret = map__load(map); |
| 3531 | if (ret) { |
| 3532 | if (ret == -2) { |
| 3533 | char *str = strfilter__string(_filter); |
| 3534 | pr_err("Failed to find symbols matched to \"%s\"\n", |
| 3535 | str); |
| 3536 | free(str); |
| 3537 | } else |
| 3538 | pr_err("Failed to load symbols in %s\n", |
| 3539 | (target) ? : "kernel"); |
| 3540 | goto end; |
| 3541 | } |
| 3542 | if (!dso__sorted_by_name(map->dso)) |
| 3543 | dso__sort_by_name(map->dso); |
| 3544 | |
| 3545 | /* Show all (filtered) symbols */ |
| 3546 | setup_pager(); |
| 3547 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3548 | for (nd = rb_first_cached(&map->dso->symbol_names); nd; |
| 3549 | nd = rb_next(nd)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3550 | struct symbol_name_rb_node *pos = rb_entry(nd, struct symbol_name_rb_node, rb_node); |
| 3551 | |
| 3552 | if (strfilter__compare(_filter, pos->sym.name)) |
| 3553 | printf("%s\n", pos->sym.name); |
| 3554 | } |
| 3555 | end: |
| 3556 | map__put(map); |
| 3557 | exit_probe_symbol_maps(); |
| 3558 | |
| 3559 | return ret; |
| 3560 | } |
| 3561 | |
| 3562 | int copy_to_probe_trace_arg(struct probe_trace_arg *tvar, |
| 3563 | struct perf_probe_arg *pvar) |
| 3564 | { |
| 3565 | tvar->value = strdup(pvar->var); |
| 3566 | if (tvar->value == NULL) |
| 3567 | return -ENOMEM; |
| 3568 | if (pvar->type) { |
| 3569 | tvar->type = strdup(pvar->type); |
| 3570 | if (tvar->type == NULL) |
| 3571 | return -ENOMEM; |
| 3572 | } |
| 3573 | if (pvar->name) { |
| 3574 | tvar->name = strdup(pvar->name); |
| 3575 | if (tvar->name == NULL) |
| 3576 | return -ENOMEM; |
| 3577 | } else |
| 3578 | tvar->name = NULL; |
| 3579 | return 0; |
| 3580 | } |