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