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