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