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