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 <libgen.h> 15 #include <stdio.h> 16 #include <unistd.h> 17 #include <stdlib.h> 18 #include <string.h> 19 #include <stdarg.h> 20 #include <limits.h> 21 #include <elf.h> 22 23 #include "build-id.h" 24 #include "event.h" 25 #include "namespaces.h" 26 #include "strlist.h" 27 #include "strfilter.h" 28 #include "debug.h" 29 #include "dso.h" 30 #include "color.h" 31 #include "map.h" 32 #include "maps.h" 33 #include "mutex.h" 34 #include "symbol.h" 35 #include <api/fs/fs.h> 36 #include "trace-event.h" /* For __maybe_unused */ 37 #include "probe-event.h" 38 #include "probe-finder.h" 39 #include "probe-file.h" 40 #include "session.h" 41 #include "string2.h" 42 #include "strbuf.h" 43 44 #include <subcmd/pager.h> 45 #include <linux/ctype.h> 46 #include <linux/zalloc.h> 47 48 #ifdef HAVE_DEBUGINFOD_SUPPORT 49 #include <elfutils/debuginfod.h> 50 #endif 51 52 #define PERFPROBE_GROUP "probe" 53 54 bool probe_event_dry_run; /* Dry run flag */ 55 struct probe_conf probe_conf = { .magic_num = DEFAULT_PROBE_MAGIC_NUM }; 56 57 static char *synthesize_perf_probe_point(struct perf_probe_point *pp); 58 59 #define semantic_error(msg ...) pr_err("Semantic error :" msg) 60 61 int e_snprintf(char *str, size_t size, const char *format, ...) 62 { 63 int ret; 64 va_list ap; 65 va_start(ap, format); 66 ret = vsnprintf(str, size, format, ap); 67 va_end(ap); 68 if (ret >= (int)size) 69 ret = -E2BIG; 70 return ret; 71 } 72 73 static struct machine *host_machine; 74 75 /* Initialize symbol maps and path of vmlinux/modules */ 76 int init_probe_symbol_maps(bool user_only) 77 { 78 int ret; 79 80 symbol_conf.allow_aliases = true; 81 ret = symbol__init(NULL); 82 if (ret < 0) { 83 pr_debug("Failed to init symbol map.\n"); 84 goto out; 85 } 86 87 if (host_machine || user_only) /* already initialized */ 88 return 0; 89 90 if (symbol_conf.vmlinux_name) 91 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name); 92 93 host_machine = machine__new_host(); 94 if (!host_machine) { 95 pr_debug("machine__new_host() failed.\n"); 96 symbol__exit(); 97 ret = -1; 98 } 99 out: 100 if (ret < 0) 101 pr_warning("Failed to init vmlinux path.\n"); 102 return ret; 103 } 104 105 void exit_probe_symbol_maps(void) 106 { 107 machine__delete(host_machine); 108 host_machine = NULL; 109 symbol__exit(); 110 } 111 112 static struct ref_reloc_sym *kernel_get_ref_reloc_sym(struct map **pmap) 113 { 114 struct kmap *kmap; 115 struct map *map = machine__kernel_map(host_machine); 116 117 if (map__load(map) < 0) 118 return NULL; 119 120 kmap = map__kmap(map); 121 if (!kmap) 122 return NULL; 123 124 if (pmap) 125 *pmap = map; 126 127 return kmap->ref_reloc_sym; 128 } 129 130 static int kernel_get_symbol_address_by_name(const char *name, u64 *addr, 131 bool reloc, bool reladdr) 132 { 133 struct ref_reloc_sym *reloc_sym; 134 struct symbol *sym; 135 struct map *map; 136 137 /* ref_reloc_sym is just a label. Need a special fix*/ 138 reloc_sym = kernel_get_ref_reloc_sym(&map); 139 if (reloc_sym && strcmp(name, reloc_sym->name) == 0) 140 *addr = (!map__reloc(map) || reloc) ? reloc_sym->addr : 141 reloc_sym->unrelocated_addr; 142 else { 143 sym = machine__find_kernel_symbol_by_name(host_machine, name, &map); 144 if (!sym) 145 return -ENOENT; 146 *addr = map__unmap_ip(map, sym->start) - 147 ((reloc) ? 0 : map__reloc(map)) - 148 ((reladdr) ? map__start(map) : 0); 149 } 150 return 0; 151 } 152 153 struct kernel_get_module_map_cb_args { 154 const char *module; 155 struct map *result; 156 }; 157 158 static int kernel_get_module_map_cb(struct map *map, void *data) 159 { 160 struct kernel_get_module_map_cb_args *args = data; 161 struct dso *dso = map__dso(map); 162 const char *short_name = dso__short_name(dso); 163 u16 short_name_len = dso__short_name_len(dso); 164 165 if (strncmp(short_name + 1, args->module, short_name_len - 2) == 0 && 166 args->module[short_name_len - 2] == '\0') { 167 args->result = map__get(map); 168 return 1; 169 } 170 return 0; 171 } 172 173 static struct map *kernel_get_module_map(const char *module) 174 { 175 struct kernel_get_module_map_cb_args args = { 176 .module = module, 177 .result = NULL, 178 }; 179 180 /* A file path -- this is an offline module */ 181 if (module && strchr(module, '/')) 182 return dso__new_map(module); 183 184 if (!module) { 185 struct map *map = machine__kernel_map(host_machine); 186 187 return map__get(map); 188 } 189 190 maps__for_each_map(machine__kernel_maps(host_machine), kernel_get_module_map_cb, &args); 191 192 return args.result; 193 } 194 195 struct map *get_target_map(const char *target, struct nsinfo *nsi, bool user) 196 { 197 /* Init maps of given executable or kernel */ 198 if (user) { 199 struct map *map; 200 struct dso *dso; 201 202 map = dso__new_map(target); 203 dso = map ? map__dso(map) : NULL; 204 if (dso) { 205 mutex_lock(dso__lock(dso)); 206 dso__set_nsinfo(dso, nsinfo__get(nsi)); 207 mutex_unlock(dso__lock(dso)); 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, sizeof(buf), "%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(dso)) 371 dso__read_running_kernel_build_id(dso, host_machine); 372 373 vmlinux_name = symbol_conf.vmlinux_name; 374 *dso__load_errno(dso) = 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(dso), 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(dso) == 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(dso); 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 map__put(map); 2278 2279 return ret; 2280 } 2281 2282 static int convert_to_perf_probe_point(struct probe_trace_point *tp, 2283 struct perf_probe_point *pp, 2284 bool is_kprobe) 2285 { 2286 char buf[128]; 2287 int ret; 2288 2289 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe); 2290 if (!ret) 2291 return 0; 2292 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe); 2293 if (!ret) 2294 return 0; 2295 2296 pr_debug("Failed to find probe point from both of dwarf and map.\n"); 2297 2298 if (tp->symbol) { 2299 pp->function = strdup(tp->symbol); 2300 pp->offset = tp->offset; 2301 } else { 2302 ret = e_snprintf(buf, 128, "0x%" PRIx64, tp->address); 2303 if (ret < 0) 2304 return ret; 2305 pp->function = strdup(buf); 2306 pp->offset = 0; 2307 } 2308 if (pp->function == NULL) 2309 return -ENOMEM; 2310 2311 pp->retprobe = tp->retprobe; 2312 2313 return 0; 2314 } 2315 2316 static int convert_to_perf_probe_event(struct probe_trace_event *tev, 2317 struct perf_probe_event *pev, bool is_kprobe) 2318 { 2319 struct strbuf buf = STRBUF_INIT; 2320 int i, ret; 2321 2322 /* Convert event/group name */ 2323 pev->event = strdup(tev->event); 2324 pev->group = strdup(tev->group); 2325 if (pev->event == NULL || pev->group == NULL) 2326 return -ENOMEM; 2327 2328 /* Convert trace_point to probe_point */ 2329 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe); 2330 if (ret < 0) 2331 return ret; 2332 2333 /* Convert trace_arg to probe_arg */ 2334 pev->nargs = tev->nargs; 2335 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs); 2336 if (pev->args == NULL) 2337 return -ENOMEM; 2338 for (i = 0; i < tev->nargs && ret >= 0; i++) { 2339 if (tev->args[i].name) 2340 pev->args[i].name = strdup(tev->args[i].name); 2341 else { 2342 if ((ret = strbuf_init(&buf, 32)) < 0) 2343 goto error; 2344 ret = synthesize_probe_trace_arg(&tev->args[i], &buf); 2345 pev->args[i].name = strbuf_detach(&buf, NULL); 2346 } 2347 if (pev->args[i].name == NULL && ret >= 0) 2348 ret = -ENOMEM; 2349 } 2350 error: 2351 if (ret < 0) 2352 clear_perf_probe_event(pev); 2353 2354 return ret; 2355 } 2356 2357 void clear_perf_probe_event(struct perf_probe_event *pev) 2358 { 2359 struct perf_probe_arg_field *field, *next; 2360 int i; 2361 2362 zfree(&pev->event); 2363 zfree(&pev->group); 2364 zfree(&pev->target); 2365 clear_perf_probe_point(&pev->point); 2366 2367 for (i = 0; i < pev->nargs; i++) { 2368 zfree(&pev->args[i].name); 2369 zfree(&pev->args[i].var); 2370 zfree(&pev->args[i].type); 2371 field = pev->args[i].field; 2372 while (field) { 2373 next = field->next; 2374 zfree(&field->name); 2375 free(field); 2376 field = next; 2377 } 2378 } 2379 pev->nargs = 0; 2380 zfree(&pev->args); 2381 } 2382 2383 #define strdup_or_goto(str, label) \ 2384 ({ char *__p = NULL; if (str && !(__p = strdup(str))) goto label; __p; }) 2385 2386 static int perf_probe_point__copy(struct perf_probe_point *dst, 2387 struct perf_probe_point *src) 2388 { 2389 dst->file = strdup_or_goto(src->file, out_err); 2390 dst->function = strdup_or_goto(src->function, out_err); 2391 dst->lazy_line = strdup_or_goto(src->lazy_line, out_err); 2392 dst->line = src->line; 2393 dst->retprobe = src->retprobe; 2394 dst->offset = src->offset; 2395 return 0; 2396 2397 out_err: 2398 clear_perf_probe_point(dst); 2399 return -ENOMEM; 2400 } 2401 2402 static int perf_probe_arg__copy(struct perf_probe_arg *dst, 2403 struct perf_probe_arg *src) 2404 { 2405 struct perf_probe_arg_field *field, **ppfield; 2406 2407 dst->name = strdup_or_goto(src->name, out_err); 2408 dst->var = strdup_or_goto(src->var, out_err); 2409 dst->type = strdup_or_goto(src->type, out_err); 2410 2411 field = src->field; 2412 ppfield = &(dst->field); 2413 while (field) { 2414 *ppfield = zalloc(sizeof(*field)); 2415 if (!*ppfield) 2416 goto out_err; 2417 (*ppfield)->name = strdup_or_goto(field->name, out_err); 2418 (*ppfield)->index = field->index; 2419 (*ppfield)->ref = field->ref; 2420 field = field->next; 2421 ppfield = &((*ppfield)->next); 2422 } 2423 return 0; 2424 out_err: 2425 return -ENOMEM; 2426 } 2427 2428 int perf_probe_event__copy(struct perf_probe_event *dst, 2429 struct perf_probe_event *src) 2430 { 2431 int i; 2432 2433 dst->event = strdup_or_goto(src->event, out_err); 2434 dst->group = strdup_or_goto(src->group, out_err); 2435 dst->target = strdup_or_goto(src->target, out_err); 2436 dst->uprobes = src->uprobes; 2437 2438 if (perf_probe_point__copy(&dst->point, &src->point) < 0) 2439 goto out_err; 2440 2441 dst->args = zalloc(sizeof(struct perf_probe_arg) * src->nargs); 2442 if (!dst->args) 2443 goto out_err; 2444 dst->nargs = src->nargs; 2445 2446 for (i = 0; i < src->nargs; i++) 2447 if (perf_probe_arg__copy(&dst->args[i], &src->args[i]) < 0) 2448 goto out_err; 2449 return 0; 2450 2451 out_err: 2452 clear_perf_probe_event(dst); 2453 return -ENOMEM; 2454 } 2455 2456 void clear_probe_trace_event(struct probe_trace_event *tev) 2457 { 2458 struct probe_trace_arg_ref *ref, *next; 2459 int i; 2460 2461 zfree(&tev->event); 2462 zfree(&tev->group); 2463 zfree(&tev->point.symbol); 2464 zfree(&tev->point.realname); 2465 zfree(&tev->point.module); 2466 for (i = 0; i < tev->nargs; i++) { 2467 zfree(&tev->args[i].name); 2468 zfree(&tev->args[i].value); 2469 zfree(&tev->args[i].type); 2470 ref = tev->args[i].ref; 2471 while (ref) { 2472 next = ref->next; 2473 free(ref); 2474 ref = next; 2475 } 2476 } 2477 zfree(&tev->args); 2478 tev->nargs = 0; 2479 } 2480 2481 struct kprobe_blacklist_node { 2482 struct list_head list; 2483 u64 start; 2484 u64 end; 2485 char *symbol; 2486 }; 2487 2488 static void kprobe_blacklist__delete(struct list_head *blacklist) 2489 { 2490 struct kprobe_blacklist_node *node; 2491 2492 while (!list_empty(blacklist)) { 2493 node = list_first_entry(blacklist, 2494 struct kprobe_blacklist_node, list); 2495 list_del_init(&node->list); 2496 zfree(&node->symbol); 2497 free(node); 2498 } 2499 } 2500 2501 static int kprobe_blacklist__load(struct list_head *blacklist) 2502 { 2503 struct kprobe_blacklist_node *node; 2504 const char *__debugfs = debugfs__mountpoint(); 2505 char buf[PATH_MAX], *p; 2506 FILE *fp; 2507 int ret; 2508 2509 if (__debugfs == NULL) 2510 return -ENOTSUP; 2511 2512 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs); 2513 if (ret < 0) 2514 return ret; 2515 2516 fp = fopen(buf, "r"); 2517 if (!fp) 2518 return -errno; 2519 2520 ret = 0; 2521 while (fgets(buf, PATH_MAX, fp)) { 2522 node = zalloc(sizeof(*node)); 2523 if (!node) { 2524 ret = -ENOMEM; 2525 break; 2526 } 2527 INIT_LIST_HEAD(&node->list); 2528 list_add_tail(&node->list, blacklist); 2529 if (sscanf(buf, "0x%" PRIx64 "-0x%" PRIx64, &node->start, &node->end) != 2) { 2530 ret = -EINVAL; 2531 break; 2532 } 2533 p = strchr(buf, '\t'); 2534 if (p) { 2535 p++; 2536 if (p[strlen(p) - 1] == '\n') 2537 p[strlen(p) - 1] = '\0'; 2538 } else 2539 p = (char *)"unknown"; 2540 node->symbol = strdup(p); 2541 if (!node->symbol) { 2542 ret = -ENOMEM; 2543 break; 2544 } 2545 pr_debug2("Blacklist: 0x%" PRIx64 "-0x%" PRIx64 ", %s\n", 2546 node->start, node->end, node->symbol); 2547 ret++; 2548 } 2549 if (ret < 0) 2550 kprobe_blacklist__delete(blacklist); 2551 fclose(fp); 2552 2553 return ret; 2554 } 2555 2556 static struct kprobe_blacklist_node * 2557 kprobe_blacklist__find_by_address(struct list_head *blacklist, u64 address) 2558 { 2559 struct kprobe_blacklist_node *node; 2560 2561 list_for_each_entry(node, blacklist, list) { 2562 if (node->start <= address && address < node->end) 2563 return node; 2564 } 2565 2566 return NULL; 2567 } 2568 2569 static LIST_HEAD(kprobe_blacklist); 2570 2571 static void kprobe_blacklist__init(void) 2572 { 2573 if (!list_empty(&kprobe_blacklist)) 2574 return; 2575 2576 if (kprobe_blacklist__load(&kprobe_blacklist) < 0) 2577 pr_debug("No kprobe blacklist support, ignored\n"); 2578 } 2579 2580 static void kprobe_blacklist__release(void) 2581 { 2582 kprobe_blacklist__delete(&kprobe_blacklist); 2583 } 2584 2585 static bool kprobe_blacklist__listed(u64 address) 2586 { 2587 return !!kprobe_blacklist__find_by_address(&kprobe_blacklist, address); 2588 } 2589 2590 static int perf_probe_event__sprintf(const char *group, const char *event, 2591 struct perf_probe_event *pev, 2592 const char *module, 2593 struct strbuf *result) 2594 { 2595 int i, ret; 2596 char *buf; 2597 2598 if (asprintf(&buf, "%s:%s", group, event) < 0) 2599 return -errno; 2600 ret = strbuf_addf(result, " %-20s (on ", buf); 2601 free(buf); 2602 if (ret) 2603 return ret; 2604 2605 /* Synthesize only event probe point */ 2606 buf = synthesize_perf_probe_point(&pev->point); 2607 if (!buf) 2608 return -ENOMEM; 2609 ret = strbuf_addstr(result, buf); 2610 free(buf); 2611 2612 if (!ret && module) 2613 ret = strbuf_addf(result, " in %s", module); 2614 2615 if (!ret && pev->nargs > 0) { 2616 ret = strbuf_add(result, " with", 5); 2617 for (i = 0; !ret && i < pev->nargs; i++) { 2618 buf = synthesize_perf_probe_arg(&pev->args[i]); 2619 if (!buf) 2620 return -ENOMEM; 2621 ret = strbuf_addf(result, " %s", buf); 2622 free(buf); 2623 } 2624 } 2625 if (!ret) 2626 ret = strbuf_addch(result, ')'); 2627 2628 return ret; 2629 } 2630 2631 /* Show an event */ 2632 int show_perf_probe_event(const char *group, const char *event, 2633 struct perf_probe_event *pev, 2634 const char *module, bool use_stdout) 2635 { 2636 struct strbuf buf = STRBUF_INIT; 2637 int ret; 2638 2639 ret = perf_probe_event__sprintf(group, event, pev, module, &buf); 2640 if (ret >= 0) { 2641 if (use_stdout) 2642 printf("%s\n", buf.buf); 2643 else 2644 pr_info("%s\n", buf.buf); 2645 } 2646 strbuf_release(&buf); 2647 2648 return ret; 2649 } 2650 2651 static bool filter_probe_trace_event(struct probe_trace_event *tev, 2652 struct strfilter *filter) 2653 { 2654 char tmp[128]; 2655 2656 /* At first, check the event name itself */ 2657 if (strfilter__compare(filter, tev->event)) 2658 return true; 2659 2660 /* Next, check the combination of name and group */ 2661 if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0) 2662 return false; 2663 return strfilter__compare(filter, tmp); 2664 } 2665 2666 static int __show_perf_probe_events(int fd, bool is_kprobe, 2667 struct strfilter *filter) 2668 { 2669 int ret = 0; 2670 struct probe_trace_event tev; 2671 struct perf_probe_event pev; 2672 struct strlist *rawlist; 2673 struct str_node *ent; 2674 2675 memset(&tev, 0, sizeof(tev)); 2676 memset(&pev, 0, sizeof(pev)); 2677 2678 rawlist = probe_file__get_rawlist(fd); 2679 if (!rawlist) 2680 return -ENOMEM; 2681 2682 strlist__for_each_entry(ent, rawlist) { 2683 ret = parse_probe_trace_command(ent->s, &tev); 2684 if (ret >= 0) { 2685 if (!filter_probe_trace_event(&tev, filter)) 2686 goto next; 2687 ret = convert_to_perf_probe_event(&tev, &pev, 2688 is_kprobe); 2689 if (ret < 0) 2690 goto next; 2691 ret = show_perf_probe_event(pev.group, pev.event, 2692 &pev, tev.point.module, 2693 true); 2694 } 2695 next: 2696 clear_perf_probe_event(&pev); 2697 clear_probe_trace_event(&tev); 2698 if (ret < 0) 2699 break; 2700 } 2701 strlist__delete(rawlist); 2702 /* Cleanup cached debuginfo if needed */ 2703 debuginfo_cache__exit(); 2704 2705 return ret; 2706 } 2707 2708 /* List up current perf-probe events */ 2709 int show_perf_probe_events(struct strfilter *filter) 2710 { 2711 int kp_fd, up_fd, ret; 2712 2713 setup_pager(); 2714 2715 if (probe_conf.cache) 2716 return probe_cache__show_all_caches(filter); 2717 2718 ret = init_probe_symbol_maps(false); 2719 if (ret < 0) 2720 return ret; 2721 2722 ret = probe_file__open_both(&kp_fd, &up_fd, 0); 2723 if (ret < 0) 2724 return ret; 2725 2726 if (kp_fd >= 0) 2727 ret = __show_perf_probe_events(kp_fd, true, filter); 2728 if (up_fd >= 0 && ret >= 0) 2729 ret = __show_perf_probe_events(up_fd, false, filter); 2730 if (kp_fd > 0) 2731 close(kp_fd); 2732 if (up_fd > 0) 2733 close(up_fd); 2734 exit_probe_symbol_maps(); 2735 2736 return ret; 2737 } 2738 2739 static int get_new_event_name(char *buf, size_t len, const char *base, 2740 struct strlist *namelist, bool ret_event, 2741 bool allow_suffix) 2742 { 2743 int i, ret; 2744 char *p, *nbase; 2745 2746 if (*base == '.') 2747 base++; 2748 nbase = strdup(base); 2749 if (!nbase) 2750 return -ENOMEM; 2751 2752 /* Cut off the dot suffixes (e.g. .const, .isra) and version suffixes */ 2753 p = strpbrk(nbase, ".@"); 2754 if (p && p != nbase) 2755 *p = '\0'; 2756 2757 /* Try no suffix number */ 2758 ret = e_snprintf(buf, len, "%s%s", nbase, ret_event ? "__return" : ""); 2759 if (ret < 0) { 2760 pr_warning("snprintf() failed: %d; the event name nbase='%s' is too long\n", ret, nbase); 2761 goto out; 2762 } 2763 if (!strlist__has_entry(namelist, buf)) 2764 goto out; 2765 2766 if (!allow_suffix) { 2767 pr_warning("Error: event \"%s\" already exists.\n" 2768 " Hint: Remove existing event by 'perf probe -d'\n" 2769 " or force duplicates by 'perf probe -f'\n" 2770 " or set 'force=yes' in BPF source.\n", 2771 buf); 2772 ret = -EEXIST; 2773 goto out; 2774 } 2775 2776 /* Try to add suffix */ 2777 for (i = 1; i < MAX_EVENT_INDEX; i++) { 2778 ret = e_snprintf(buf, len, "%s_%d", nbase, i); 2779 if (ret < 0) { 2780 pr_debug("snprintf() failed: %d\n", ret); 2781 goto out; 2782 } 2783 if (!strlist__has_entry(namelist, buf)) 2784 break; 2785 } 2786 if (i == MAX_EVENT_INDEX) { 2787 pr_warning("Too many events are on the same function.\n"); 2788 ret = -ERANGE; 2789 } 2790 2791 out: 2792 free(nbase); 2793 2794 /* Final validation */ 2795 if (ret >= 0 && !is_c_func_name(buf)) { 2796 pr_warning("Internal error: \"%s\" is an invalid event name.\n", 2797 buf); 2798 ret = -EINVAL; 2799 } 2800 2801 return ret; 2802 } 2803 2804 /* Warn if the current kernel's uprobe implementation is old */ 2805 static void warn_uprobe_event_compat(struct probe_trace_event *tev) 2806 { 2807 int i; 2808 char *buf = synthesize_probe_trace_command(tev); 2809 struct probe_trace_point *tp = &tev->point; 2810 2811 if (tp->ref_ctr_offset && !uprobe_ref_ctr_is_supported()) { 2812 pr_warning("A semaphore is associated with %s:%s and " 2813 "seems your kernel doesn't support it.\n", 2814 tev->group, tev->event); 2815 } 2816 2817 /* Old uprobe event doesn't support memory dereference */ 2818 if (!tev->uprobes || tev->nargs == 0 || !buf) 2819 goto out; 2820 2821 for (i = 0; i < tev->nargs; i++) { 2822 if (strchr(tev->args[i].value, '@')) { 2823 pr_warning("%s accesses a variable by symbol name, but that is not supported for user application probe.\n", 2824 tev->args[i].value); 2825 break; 2826 } 2827 if (strglobmatch(tev->args[i].value, "[$+-]*")) { 2828 pr_warning("Please upgrade your kernel to at least 3.14 to have access to feature %s\n", 2829 tev->args[i].value); 2830 break; 2831 } 2832 } 2833 out: 2834 free(buf); 2835 } 2836 2837 /* Set new name from original perf_probe_event and namelist */ 2838 static int probe_trace_event__set_name(struct probe_trace_event *tev, 2839 struct perf_probe_event *pev, 2840 struct strlist *namelist, 2841 bool allow_suffix) 2842 { 2843 const char *event, *group; 2844 char buf[64]; 2845 int ret; 2846 2847 /* If probe_event or trace_event already have the name, reuse it */ 2848 if (pev->event && !pev->sdt) 2849 event = pev->event; 2850 else if (tev->event) 2851 event = tev->event; 2852 else { 2853 /* Or generate new one from probe point */ 2854 if (pev->point.function && 2855 (strncmp(pev->point.function, "0x", 2) != 0) && 2856 !strisglob(pev->point.function)) 2857 event = pev->point.function; 2858 else 2859 event = tev->point.realname; 2860 } 2861 if (pev->group && !pev->sdt) 2862 group = pev->group; 2863 else if (tev->group) 2864 group = tev->group; 2865 else 2866 group = PERFPROBE_GROUP; 2867 2868 /* Get an unused new event name */ 2869 ret = get_new_event_name(buf, sizeof(buf), event, namelist, 2870 tev->point.retprobe, allow_suffix); 2871 if (ret < 0) 2872 return ret; 2873 2874 event = buf; 2875 2876 tev->event = strdup(event); 2877 tev->group = strdup(group); 2878 if (tev->event == NULL || tev->group == NULL) 2879 return -ENOMEM; 2880 2881 /* 2882 * Add new event name to namelist if multiprobe event is NOT 2883 * supported, since we have to use new event name for following 2884 * probes in that case. 2885 */ 2886 if (!multiprobe_event_is_supported()) 2887 strlist__add(namelist, event); 2888 return 0; 2889 } 2890 2891 static int __open_probe_file_and_namelist(bool uprobe, 2892 struct strlist **namelist) 2893 { 2894 int fd; 2895 2896 fd = probe_file__open(PF_FL_RW | (uprobe ? PF_FL_UPROBE : 0)); 2897 if (fd < 0) 2898 return fd; 2899 2900 /* Get current event names */ 2901 *namelist = probe_file__get_namelist(fd); 2902 if (!(*namelist)) { 2903 pr_debug("Failed to get current event list.\n"); 2904 close(fd); 2905 return -ENOMEM; 2906 } 2907 return fd; 2908 } 2909 2910 static int __add_probe_trace_events(struct perf_probe_event *pev, 2911 struct probe_trace_event *tevs, 2912 int ntevs, bool allow_suffix) 2913 { 2914 int i, fd[2] = {-1, -1}, up, ret; 2915 struct probe_trace_event *tev = NULL; 2916 struct probe_cache *cache = NULL; 2917 struct strlist *namelist[2] = {NULL, NULL}; 2918 struct nscookie nsc; 2919 2920 up = pev->uprobes ? 1 : 0; 2921 fd[up] = __open_probe_file_and_namelist(up, &namelist[up]); 2922 if (fd[up] < 0) 2923 return fd[up]; 2924 2925 ret = 0; 2926 for (i = 0; i < ntevs; i++) { 2927 tev = &tevs[i]; 2928 up = tev->uprobes ? 1 : 0; 2929 if (fd[up] == -1) { /* Open the kprobe/uprobe_events */ 2930 fd[up] = __open_probe_file_and_namelist(up, 2931 &namelist[up]); 2932 if (fd[up] < 0) 2933 goto close_out; 2934 } 2935 /* Skip if the symbol is out of .text or blacklisted */ 2936 if (!tev->point.symbol && !pev->uprobes) 2937 continue; 2938 2939 /* Set new name for tev (and update namelist) */ 2940 ret = probe_trace_event__set_name(tev, pev, namelist[up], 2941 allow_suffix); 2942 if (ret < 0) 2943 break; 2944 2945 nsinfo__mountns_enter(pev->nsi, &nsc); 2946 ret = probe_file__add_event(fd[up], tev); 2947 nsinfo__mountns_exit(&nsc); 2948 if (ret < 0) 2949 break; 2950 2951 /* 2952 * Probes after the first probe which comes from same 2953 * user input are always allowed to add suffix, because 2954 * there might be several addresses corresponding to 2955 * one code line. 2956 */ 2957 allow_suffix = true; 2958 } 2959 if (ret == -EINVAL && pev->uprobes) 2960 warn_uprobe_event_compat(tev); 2961 if (ret == 0 && probe_conf.cache) { 2962 cache = probe_cache__new(pev->target, pev->nsi); 2963 if (!cache || 2964 probe_cache__add_entry(cache, pev, tevs, ntevs) < 0 || 2965 probe_cache__commit(cache) < 0) 2966 pr_warning("Failed to add event to probe cache\n"); 2967 probe_cache__delete(cache); 2968 } 2969 2970 close_out: 2971 for (up = 0; up < 2; up++) { 2972 strlist__delete(namelist[up]); 2973 if (fd[up] >= 0) 2974 close(fd[up]); 2975 } 2976 return ret; 2977 } 2978 2979 static int find_probe_functions(struct map *map, char *name, 2980 struct symbol **syms) 2981 { 2982 int found = 0; 2983 struct symbol *sym; 2984 struct rb_node *tmp; 2985 const char *norm, *ver; 2986 char *buf = NULL; 2987 bool cut_version = true; 2988 2989 if (map__load(map) < 0) 2990 return -EACCES; /* Possible permission error to load symbols */ 2991 2992 /* If user gives a version, don't cut off the version from symbols */ 2993 if (strchr(name, '@')) 2994 cut_version = false; 2995 2996 map__for_each_symbol(map, sym, tmp) { 2997 norm = arch__normalize_symbol_name(sym->name); 2998 if (!norm) 2999 continue; 3000 3001 if (cut_version) { 3002 /* We don't care about default symbol or not */ 3003 ver = strchr(norm, '@'); 3004 if (ver) { 3005 buf = strndup(norm, ver - norm); 3006 if (!buf) 3007 return -ENOMEM; 3008 norm = buf; 3009 } 3010 } 3011 3012 if (strglobmatch(norm, name)) { 3013 found++; 3014 if (syms && found < probe_conf.max_probes) 3015 syms[found - 1] = sym; 3016 } 3017 if (buf) 3018 zfree(&buf); 3019 } 3020 3021 return found; 3022 } 3023 3024 void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused, 3025 struct probe_trace_event *tev __maybe_unused, 3026 struct map *map __maybe_unused, 3027 struct symbol *sym __maybe_unused) { } 3028 3029 3030 static void pr_kallsyms_access_error(void) 3031 { 3032 pr_err("Please ensure you can read the /proc/kallsyms symbol addresses.\n" 3033 "If /proc/sys/kernel/kptr_restrict is '2', you can not read\n" 3034 "kernel symbol addresses even if you are a superuser. Please change\n" 3035 "it to '1'. If kptr_restrict is '1', the superuser can read the\n" 3036 "symbol addresses.\n" 3037 "In that case, please run this command again with sudo.\n"); 3038 } 3039 3040 /* 3041 * Find probe function addresses from map. 3042 * Return an error or the number of found probe_trace_event 3043 */ 3044 static int find_probe_trace_events_from_map(struct perf_probe_event *pev, 3045 struct probe_trace_event **tevs) 3046 { 3047 struct map *map = NULL; 3048 struct ref_reloc_sym *reloc_sym = NULL; 3049 struct symbol *sym; 3050 struct symbol **syms = NULL; 3051 struct probe_trace_event *tev; 3052 struct perf_probe_point *pp = &pev->point; 3053 struct probe_trace_point *tp; 3054 int num_matched_functions; 3055 int ret, i, j, skipped = 0; 3056 char *mod_name; 3057 3058 map = get_target_map(pev->target, pev->nsi, pev->uprobes); 3059 if (!map) { 3060 ret = -EINVAL; 3061 goto out; 3062 } 3063 3064 syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes); 3065 if (!syms) { 3066 ret = -ENOMEM; 3067 goto out; 3068 } 3069 3070 /* 3071 * Load matched symbols: Since the different local symbols may have 3072 * same name but different addresses, this lists all the symbols. 3073 */ 3074 num_matched_functions = find_probe_functions(map, pp->function, syms); 3075 if (num_matched_functions <= 0) { 3076 if (num_matched_functions == -EACCES) { 3077 pr_err("Failed to load symbols from %s\n", 3078 pev->target ?: "/proc/kallsyms"); 3079 if (pev->target) 3080 pr_err("Please ensure the file is not stripped.\n"); 3081 else 3082 pr_kallsyms_access_error(); 3083 } else 3084 pr_err("Failed to find symbol %s in %s\n", pp->function, 3085 pev->target ? : "kernel"); 3086 ret = -ENOENT; 3087 goto out; 3088 } else if (num_matched_functions > probe_conf.max_probes) { 3089 pr_err("Too many functions matched in %s\n", 3090 pev->target ? : "kernel"); 3091 ret = -E2BIG; 3092 goto out; 3093 } 3094 3095 /* Note that the symbols in the kmodule are not relocated */ 3096 if (!pev->uprobes && !pev->target && 3097 (!pp->retprobe || kretprobe_offset_is_supported())) { 3098 reloc_sym = kernel_get_ref_reloc_sym(NULL); 3099 if (!reloc_sym) { 3100 pr_warning("Relocated base symbol is not found! " 3101 "Check /proc/sys/kernel/kptr_restrict\n" 3102 "and /proc/sys/kernel/perf_event_paranoid. " 3103 "Or run as privileged perf user.\n\n"); 3104 ret = -EINVAL; 3105 goto out; 3106 } 3107 } 3108 3109 /* Setup result trace-probe-events */ 3110 *tevs = zalloc(sizeof(*tev) * num_matched_functions); 3111 if (!*tevs) { 3112 ret = -ENOMEM; 3113 goto out; 3114 } 3115 3116 ret = 0; 3117 3118 for (j = 0; j < num_matched_functions; j++) { 3119 sym = syms[j]; 3120 3121 if (sym->type != STT_FUNC) 3122 continue; 3123 3124 /* There can be duplicated symbols in the map */ 3125 for (i = 0; i < j; i++) 3126 if (sym->start == syms[i]->start) { 3127 pr_debug("Found duplicated symbol %s @ %" PRIx64 "\n", 3128 sym->name, sym->start); 3129 break; 3130 } 3131 if (i != j) 3132 continue; 3133 3134 tev = (*tevs) + ret; 3135 tp = &tev->point; 3136 if (ret == num_matched_functions) { 3137 pr_warning("Too many symbols are listed. Skip it.\n"); 3138 break; 3139 } 3140 ret++; 3141 3142 if (pp->offset > sym->end - sym->start) { 3143 pr_warning("Offset %ld is bigger than the size of %s\n", 3144 pp->offset, sym->name); 3145 ret = -ENOENT; 3146 goto err_out; 3147 } 3148 /* Add one probe point */ 3149 tp->address = map__unmap_ip(map, sym->start) + pp->offset; 3150 3151 /* Check the kprobe (not in module) is within .text */ 3152 if (!pev->uprobes && !pev->target && 3153 kprobe_warn_out_range(sym->name, tp->address)) { 3154 tp->symbol = NULL; /* Skip it */ 3155 skipped++; 3156 } else if (reloc_sym) { 3157 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out); 3158 tp->offset = tp->address - reloc_sym->addr; 3159 } else { 3160 tp->symbol = strdup_or_goto(sym->name, nomem_out); 3161 tp->offset = pp->offset; 3162 } 3163 tp->realname = strdup_or_goto(sym->name, nomem_out); 3164 3165 tp->retprobe = pp->retprobe; 3166 if (pev->target) { 3167 if (pev->uprobes) { 3168 tev->point.module = strdup_or_goto(pev->target, 3169 nomem_out); 3170 } else { 3171 mod_name = find_module_name(pev->target); 3172 tev->point.module = 3173 strdup(mod_name ? mod_name : pev->target); 3174 free(mod_name); 3175 if (!tev->point.module) 3176 goto nomem_out; 3177 } 3178 } 3179 tev->uprobes = pev->uprobes; 3180 tev->nargs = pev->nargs; 3181 if (tev->nargs) { 3182 tev->args = zalloc(sizeof(struct probe_trace_arg) * 3183 tev->nargs); 3184 if (tev->args == NULL) 3185 goto nomem_out; 3186 } 3187 for (i = 0; i < tev->nargs; i++) { 3188 if (pev->args[i].name) 3189 tev->args[i].name = 3190 strdup_or_goto(pev->args[i].name, 3191 nomem_out); 3192 3193 tev->args[i].value = strdup_or_goto(pev->args[i].var, 3194 nomem_out); 3195 if (pev->args[i].type) 3196 tev->args[i].type = 3197 strdup_or_goto(pev->args[i].type, 3198 nomem_out); 3199 } 3200 arch__fix_tev_from_maps(pev, tev, map, sym); 3201 } 3202 if (ret == skipped) { 3203 ret = -ENOENT; 3204 goto err_out; 3205 } 3206 3207 out: 3208 map__put(map); 3209 free(syms); 3210 return ret; 3211 3212 nomem_out: 3213 ret = -ENOMEM; 3214 err_out: 3215 clear_probe_trace_events(*tevs, num_matched_functions); 3216 zfree(tevs); 3217 goto out; 3218 } 3219 3220 static int try_to_find_absolute_address(struct perf_probe_event *pev, 3221 struct probe_trace_event **tevs) 3222 { 3223 struct perf_probe_point *pp = &pev->point; 3224 struct probe_trace_event *tev; 3225 struct probe_trace_point *tp; 3226 int i, err; 3227 3228 if (!(pev->point.function && !strncmp(pev->point.function, "0x", 2))) 3229 return -EINVAL; 3230 if (perf_probe_event_need_dwarf(pev)) 3231 return -EINVAL; 3232 3233 /* 3234 * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at 3235 * absolute address. 3236 * 3237 * Only one tev can be generated by this. 3238 */ 3239 *tevs = zalloc(sizeof(*tev)); 3240 if (!*tevs) 3241 return -ENOMEM; 3242 3243 tev = *tevs; 3244 tp = &tev->point; 3245 3246 /* 3247 * Don't use tp->offset, use address directly, because 3248 * in synthesize_probe_trace_command() address cannot be 3249 * zero. 3250 */ 3251 tp->address = pev->point.abs_address; 3252 tp->retprobe = pp->retprobe; 3253 tev->uprobes = pev->uprobes; 3254 3255 err = -ENOMEM; 3256 /* 3257 * Give it a '0x' leading symbol name. 3258 * In __add_probe_trace_events, a NULL symbol is interpreted as 3259 * invalid. 3260 */ 3261 if (asprintf(&tp->symbol, "0x%" PRIx64, tp->address) < 0) 3262 goto errout; 3263 3264 /* For kprobe, check range */ 3265 if ((!tev->uprobes) && 3266 (kprobe_warn_out_range(tev->point.symbol, 3267 tev->point.address))) { 3268 err = -EACCES; 3269 goto errout; 3270 } 3271 3272 if (asprintf(&tp->realname, "abs_%" PRIx64, tp->address) < 0) 3273 goto errout; 3274 3275 if (pev->target) { 3276 tp->module = strdup(pev->target); 3277 if (!tp->module) 3278 goto errout; 3279 } 3280 3281 if (tev->group) { 3282 tev->group = strdup(pev->group); 3283 if (!tev->group) 3284 goto errout; 3285 } 3286 3287 if (pev->event) { 3288 tev->event = strdup(pev->event); 3289 if (!tev->event) 3290 goto errout; 3291 } 3292 3293 tev->nargs = pev->nargs; 3294 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs); 3295 if (!tev->args) 3296 goto errout; 3297 3298 for (i = 0; i < tev->nargs; i++) 3299 copy_to_probe_trace_arg(&tev->args[i], &pev->args[i]); 3300 3301 return 1; 3302 3303 errout: 3304 clear_probe_trace_events(*tevs, 1); 3305 *tevs = NULL; 3306 return err; 3307 } 3308 3309 /* Concatenate two arrays */ 3310 static void *memcat(void *a, size_t sz_a, void *b, size_t sz_b) 3311 { 3312 void *ret; 3313 3314 ret = malloc(sz_a + sz_b); 3315 if (ret) { 3316 memcpy(ret, a, sz_a); 3317 memcpy(ret + sz_a, b, sz_b); 3318 } 3319 return ret; 3320 } 3321 3322 static int 3323 concat_probe_trace_events(struct probe_trace_event **tevs, int *ntevs, 3324 struct probe_trace_event **tevs2, int ntevs2) 3325 { 3326 struct probe_trace_event *new_tevs; 3327 int ret = 0; 3328 3329 if (*ntevs == 0) { 3330 *tevs = *tevs2; 3331 *ntevs = ntevs2; 3332 *tevs2 = NULL; 3333 return 0; 3334 } 3335 3336 if (*ntevs + ntevs2 > probe_conf.max_probes) 3337 ret = -E2BIG; 3338 else { 3339 /* Concatenate the array of probe_trace_event */ 3340 new_tevs = memcat(*tevs, (*ntevs) * sizeof(**tevs), 3341 *tevs2, ntevs2 * sizeof(**tevs2)); 3342 if (!new_tevs) 3343 ret = -ENOMEM; 3344 else { 3345 free(*tevs); 3346 *tevs = new_tevs; 3347 *ntevs += ntevs2; 3348 } 3349 } 3350 if (ret < 0) 3351 clear_probe_trace_events(*tevs2, ntevs2); 3352 zfree(tevs2); 3353 3354 return ret; 3355 } 3356 3357 /* 3358 * Try to find probe_trace_event from given probe caches. Return the number 3359 * of cached events found, if an error occurs return the error. 3360 */ 3361 static int find_cached_events(struct perf_probe_event *pev, 3362 struct probe_trace_event **tevs, 3363 const char *target) 3364 { 3365 struct probe_cache *cache; 3366 struct probe_cache_entry *entry; 3367 struct probe_trace_event *tmp_tevs = NULL; 3368 int ntevs = 0; 3369 int ret = 0; 3370 3371 cache = probe_cache__new(target, pev->nsi); 3372 /* Return 0 ("not found") if the target has no probe cache. */ 3373 if (!cache) 3374 return 0; 3375 3376 for_each_probe_cache_entry(entry, cache) { 3377 /* Skip the cache entry which has no name */ 3378 if (!entry->pev.event || !entry->pev.group) 3379 continue; 3380 if ((!pev->group || strglobmatch(entry->pev.group, pev->group)) && 3381 strglobmatch(entry->pev.event, pev->event)) { 3382 ret = probe_cache_entry__get_event(entry, &tmp_tevs); 3383 if (ret > 0) 3384 ret = concat_probe_trace_events(tevs, &ntevs, 3385 &tmp_tevs, ret); 3386 if (ret < 0) 3387 break; 3388 } 3389 } 3390 probe_cache__delete(cache); 3391 if (ret < 0) { 3392 clear_probe_trace_events(*tevs, ntevs); 3393 zfree(tevs); 3394 } else { 3395 ret = ntevs; 3396 if (ntevs > 0 && target && target[0] == '/') 3397 pev->uprobes = true; 3398 } 3399 3400 return ret; 3401 } 3402 3403 /* Try to find probe_trace_event from all probe caches */ 3404 static int find_cached_events_all(struct perf_probe_event *pev, 3405 struct probe_trace_event **tevs) 3406 { 3407 struct probe_trace_event *tmp_tevs = NULL; 3408 struct strlist *bidlist; 3409 struct str_node *nd; 3410 char *pathname; 3411 int ntevs = 0; 3412 int ret; 3413 3414 /* Get the buildid list of all valid caches */ 3415 bidlist = build_id_cache__list_all(true); 3416 if (!bidlist) { 3417 ret = -errno; 3418 pr_debug("Failed to get buildids: %d\n", ret); 3419 return ret; 3420 } 3421 3422 ret = 0; 3423 strlist__for_each_entry(nd, bidlist) { 3424 pathname = build_id_cache__origname(nd->s); 3425 ret = find_cached_events(pev, &tmp_tevs, pathname); 3426 /* In the case of cnt == 0, we just skip it */ 3427 if (ret > 0) 3428 ret = concat_probe_trace_events(tevs, &ntevs, 3429 &tmp_tevs, ret); 3430 free(pathname); 3431 if (ret < 0) 3432 break; 3433 } 3434 strlist__delete(bidlist); 3435 3436 if (ret < 0) { 3437 clear_probe_trace_events(*tevs, ntevs); 3438 zfree(tevs); 3439 } else 3440 ret = ntevs; 3441 3442 return ret; 3443 } 3444 3445 static int find_probe_trace_events_from_cache(struct perf_probe_event *pev, 3446 struct probe_trace_event **tevs) 3447 { 3448 struct probe_cache *cache; 3449 struct probe_cache_entry *entry; 3450 struct probe_trace_event *tev; 3451 struct str_node *node; 3452 int ret, i; 3453 3454 if (pev->sdt) { 3455 /* For SDT/cached events, we use special search functions */ 3456 if (!pev->target) 3457 return find_cached_events_all(pev, tevs); 3458 else 3459 return find_cached_events(pev, tevs, pev->target); 3460 } 3461 cache = probe_cache__new(pev->target, pev->nsi); 3462 if (!cache) 3463 return 0; 3464 3465 entry = probe_cache__find(cache, pev); 3466 if (!entry) { 3467 /* SDT must be in the cache */ 3468 ret = pev->sdt ? -ENOENT : 0; 3469 goto out; 3470 } 3471 3472 ret = strlist__nr_entries(entry->tevlist); 3473 if (ret > probe_conf.max_probes) { 3474 pr_debug("Too many entries matched in the cache of %s\n", 3475 pev->target ? : "kernel"); 3476 ret = -E2BIG; 3477 goto out; 3478 } 3479 3480 *tevs = zalloc(ret * sizeof(*tev)); 3481 if (!*tevs) { 3482 ret = -ENOMEM; 3483 goto out; 3484 } 3485 3486 i = 0; 3487 strlist__for_each_entry(node, entry->tevlist) { 3488 tev = &(*tevs)[i++]; 3489 ret = parse_probe_trace_command(node->s, tev); 3490 if (ret < 0) 3491 goto out; 3492 /* Set the uprobes attribute as same as original */ 3493 tev->uprobes = pev->uprobes; 3494 } 3495 ret = i; 3496 3497 out: 3498 probe_cache__delete(cache); 3499 return ret; 3500 } 3501 3502 static int convert_to_probe_trace_events(struct perf_probe_event *pev, 3503 struct probe_trace_event **tevs) 3504 { 3505 int ret; 3506 3507 if (!pev->group && !pev->sdt) { 3508 /* Set group name if not given */ 3509 if (!pev->uprobes) { 3510 pev->group = strdup(PERFPROBE_GROUP); 3511 ret = pev->group ? 0 : -ENOMEM; 3512 } else 3513 ret = convert_exec_to_group(pev->target, &pev->group); 3514 if (ret != 0) { 3515 pr_warning("Failed to make a group name.\n"); 3516 return ret; 3517 } 3518 } 3519 3520 ret = try_to_find_absolute_address(pev, tevs); 3521 if (ret > 0) 3522 return ret; 3523 3524 /* At first, we need to lookup cache entry */ 3525 ret = find_probe_trace_events_from_cache(pev, tevs); 3526 if (ret > 0 || pev->sdt) /* SDT can be found only in the cache */ 3527 return ret == 0 ? -ENOENT : ret; /* Found in probe cache */ 3528 3529 /* Convert perf_probe_event with debuginfo */ 3530 ret = try_to_find_probe_trace_events(pev, tevs); 3531 if (ret != 0) 3532 return ret; /* Found in debuginfo or got an error */ 3533 3534 return find_probe_trace_events_from_map(pev, tevs); 3535 } 3536 3537 int convert_perf_probe_events(struct perf_probe_event *pevs, int npevs) 3538 { 3539 int i, ret; 3540 3541 /* Loop 1: convert all events */ 3542 for (i = 0; i < npevs; i++) { 3543 /* Init kprobe blacklist if needed */ 3544 if (!pevs[i].uprobes) 3545 kprobe_blacklist__init(); 3546 /* Convert with or without debuginfo */ 3547 ret = convert_to_probe_trace_events(&pevs[i], &pevs[i].tevs); 3548 if (ret < 0) 3549 return ret; 3550 pevs[i].ntevs = ret; 3551 } 3552 /* This just release blacklist only if allocated */ 3553 kprobe_blacklist__release(); 3554 3555 return 0; 3556 } 3557 3558 static int show_probe_trace_event(struct probe_trace_event *tev) 3559 { 3560 char *buf = synthesize_probe_trace_command(tev); 3561 3562 if (!buf) { 3563 pr_debug("Failed to synthesize probe trace event.\n"); 3564 return -EINVAL; 3565 } 3566 3567 /* Showing definition always go stdout */ 3568 printf("%s\n", buf); 3569 free(buf); 3570 3571 return 0; 3572 } 3573 3574 int show_probe_trace_events(struct perf_probe_event *pevs, int npevs) 3575 { 3576 struct strlist *namelist = strlist__new(NULL, NULL); 3577 struct probe_trace_event *tev; 3578 struct perf_probe_event *pev; 3579 int i, j, ret = 0; 3580 3581 if (!namelist) 3582 return -ENOMEM; 3583 3584 for (j = 0; j < npevs && !ret; j++) { 3585 pev = &pevs[j]; 3586 for (i = 0; i < pev->ntevs && !ret; i++) { 3587 tev = &pev->tevs[i]; 3588 /* Skip if the symbol is out of .text or blacklisted */ 3589 if (!tev->point.symbol && !pev->uprobes) 3590 continue; 3591 3592 /* Set new name for tev (and update namelist) */ 3593 ret = probe_trace_event__set_name(tev, pev, 3594 namelist, true); 3595 if (!ret) 3596 ret = show_probe_trace_event(tev); 3597 } 3598 } 3599 strlist__delete(namelist); 3600 3601 return ret; 3602 } 3603 3604 static int show_bootconfig_event(struct probe_trace_event *tev) 3605 { 3606 struct probe_trace_point *tp = &tev->point; 3607 struct strbuf buf; 3608 char *ret = NULL; 3609 int err; 3610 3611 if (strbuf_init(&buf, 32) < 0) 3612 return -ENOMEM; 3613 3614 err = synthesize_kprobe_trace_def(tp, &buf); 3615 if (err >= 0) 3616 err = synthesize_probe_trace_args(tev, &buf); 3617 if (err >= 0) 3618 ret = strbuf_detach(&buf, NULL); 3619 strbuf_release(&buf); 3620 3621 if (ret) { 3622 printf("'%s'", ret); 3623 free(ret); 3624 } 3625 3626 return err; 3627 } 3628 3629 int show_bootconfig_events(struct perf_probe_event *pevs, int npevs) 3630 { 3631 struct strlist *namelist = strlist__new(NULL, NULL); 3632 struct probe_trace_event *tev; 3633 struct perf_probe_event *pev; 3634 char *cur_name = NULL; 3635 int i, j, ret = 0; 3636 3637 if (!namelist) 3638 return -ENOMEM; 3639 3640 for (j = 0; j < npevs && !ret; j++) { 3641 pev = &pevs[j]; 3642 if (pev->group && strcmp(pev->group, "probe")) 3643 pr_warning("WARN: Group name %s is ignored\n", pev->group); 3644 if (pev->uprobes) { 3645 pr_warning("ERROR: Bootconfig doesn't support uprobes\n"); 3646 ret = -EINVAL; 3647 break; 3648 } 3649 for (i = 0; i < pev->ntevs && !ret; i++) { 3650 tev = &pev->tevs[i]; 3651 /* Skip if the symbol is out of .text or blacklisted */ 3652 if (!tev->point.symbol && !pev->uprobes) 3653 continue; 3654 3655 /* Set new name for tev (and update namelist) */ 3656 ret = probe_trace_event__set_name(tev, pev, 3657 namelist, true); 3658 if (ret) 3659 break; 3660 3661 if (!cur_name || strcmp(cur_name, tev->event)) { 3662 printf("%sftrace.event.kprobes.%s.probe = ", 3663 cur_name ? "\n" : "", tev->event); 3664 cur_name = tev->event; 3665 } else 3666 printf(", "); 3667 ret = show_bootconfig_event(tev); 3668 } 3669 } 3670 printf("\n"); 3671 strlist__delete(namelist); 3672 3673 return ret; 3674 } 3675 3676 int apply_perf_probe_events(struct perf_probe_event *pevs, int npevs) 3677 { 3678 int i, ret = 0; 3679 3680 /* Loop 2: add all events */ 3681 for (i = 0; i < npevs; i++) { 3682 ret = __add_probe_trace_events(&pevs[i], pevs[i].tevs, 3683 pevs[i].ntevs, 3684 probe_conf.force_add); 3685 if (ret < 0) 3686 break; 3687 } 3688 return ret; 3689 } 3690 3691 void cleanup_perf_probe_events(struct perf_probe_event *pevs, int npevs) 3692 { 3693 int i, j; 3694 struct perf_probe_event *pev; 3695 3696 /* Loop 3: cleanup and free trace events */ 3697 for (i = 0; i < npevs; i++) { 3698 pev = &pevs[i]; 3699 for (j = 0; j < pevs[i].ntevs; j++) 3700 clear_probe_trace_event(&pevs[i].tevs[j]); 3701 zfree(&pevs[i].tevs); 3702 pevs[i].ntevs = 0; 3703 nsinfo__zput(pev->nsi); 3704 clear_perf_probe_event(&pevs[i]); 3705 } 3706 } 3707 3708 int add_perf_probe_events(struct perf_probe_event *pevs, int npevs) 3709 { 3710 int ret; 3711 3712 ret = init_probe_symbol_maps(pevs->uprobes); 3713 if (ret < 0) 3714 return ret; 3715 3716 ret = convert_perf_probe_events(pevs, npevs); 3717 if (ret == 0) 3718 ret = apply_perf_probe_events(pevs, npevs); 3719 3720 cleanup_perf_probe_events(pevs, npevs); 3721 3722 exit_probe_symbol_maps(); 3723 return ret; 3724 } 3725 3726 int del_perf_probe_events(struct strfilter *filter) 3727 { 3728 int ret, ret2, ufd = -1, kfd = -1; 3729 char *str = strfilter__string(filter); 3730 3731 if (!str) 3732 return -EINVAL; 3733 3734 /* Get current event names */ 3735 ret = probe_file__open_both(&kfd, &ufd, PF_FL_RW); 3736 if (ret < 0) 3737 goto out; 3738 3739 ret = probe_file__del_events(kfd, filter); 3740 if (ret < 0 && ret != -ENOENT) 3741 goto error; 3742 3743 ret2 = probe_file__del_events(ufd, filter); 3744 if (ret2 < 0 && ret2 != -ENOENT) { 3745 ret = ret2; 3746 goto error; 3747 } 3748 ret = 0; 3749 3750 error: 3751 if (kfd >= 0) 3752 close(kfd); 3753 if (ufd >= 0) 3754 close(ufd); 3755 out: 3756 free(str); 3757 3758 return ret; 3759 } 3760 3761 int show_available_funcs(const char *target, struct nsinfo *nsi, 3762 struct strfilter *_filter, bool user) 3763 { 3764 struct map *map; 3765 struct dso *dso; 3766 int ret; 3767 3768 ret = init_probe_symbol_maps(user); 3769 if (ret < 0) 3770 return ret; 3771 3772 /* Get a symbol map */ 3773 map = get_target_map(target, nsi, user); 3774 if (!map) { 3775 pr_err("Failed to get a map for %s\n", (target) ? : "kernel"); 3776 return -EINVAL; 3777 } 3778 3779 ret = map__load(map); 3780 if (ret) { 3781 if (ret == -2) { 3782 char *str = strfilter__string(_filter); 3783 pr_err("Failed to find symbols matched to \"%s\"\n", 3784 str); 3785 free(str); 3786 } else 3787 pr_err("Failed to load symbols in %s\n", 3788 (target) ? : "kernel"); 3789 goto end; 3790 } 3791 dso = map__dso(map); 3792 dso__sort_by_name(dso); 3793 3794 /* Show all (filtered) symbols */ 3795 setup_pager(); 3796 3797 for (size_t i = 0; i < dso__symbol_names_len(dso); i++) { 3798 struct symbol *pos = dso__symbol_names(dso)[i]; 3799 3800 if (strfilter__compare(_filter, pos->name)) 3801 printf("%s\n", pos->name); 3802 } 3803 end: 3804 map__put(map); 3805 exit_probe_symbol_maps(); 3806 3807 return ret; 3808 } 3809 3810 int copy_to_probe_trace_arg(struct probe_trace_arg *tvar, 3811 struct perf_probe_arg *pvar) 3812 { 3813 tvar->value = strdup(pvar->var); 3814 if (tvar->value == NULL) 3815 return -ENOMEM; 3816 if (pvar->type) { 3817 tvar->type = strdup(pvar->type); 3818 if (tvar->type == NULL) 3819 return -ENOMEM; 3820 } 3821 if (pvar->name) { 3822 tvar->name = strdup(pvar->name); 3823 if (tvar->name == NULL) 3824 return -ENOMEM; 3825 } else 3826 tvar->name = NULL; 3827 return 0; 3828 } 3829