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) { /* Type setting */ 1567 *tmp = '\0'; 1568 arg->type = strdup(tmp + 1); 1569 if (arg->type == NULL) 1570 return -ENOMEM; 1571 pr_debug("type:%s ", arg->type); 1572 } 1573 1574 tmp = strpbrk(str, "-.["); 1575 if (!is_c_varname(str) || !tmp) { 1576 /* A variable, register, symbol or special value */ 1577 arg->var = strdup(str); 1578 if (arg->var == NULL) 1579 return -ENOMEM; 1580 pr_debug("%s\n", arg->var); 1581 return 0; 1582 } 1583 1584 /* Structure fields or array element */ 1585 arg->var = strndup(str, tmp - str); 1586 if (arg->var == NULL) 1587 return -ENOMEM; 1588 goodname = arg->var; 1589 pr_debug("%s, ", arg->var); 1590 fieldp = &arg->field; 1591 1592 do { 1593 *fieldp = zalloc(sizeof(struct perf_probe_arg_field)); 1594 if (*fieldp == NULL) 1595 return -ENOMEM; 1596 if (*tmp == '[') { /* Array */ 1597 str = tmp; 1598 (*fieldp)->index = strtol(str + 1, &tmp, 0); 1599 (*fieldp)->ref = true; 1600 if (*tmp != ']' || tmp == str + 1) { 1601 semantic_error("Array index must be a" 1602 " number.\n"); 1603 return -EINVAL; 1604 } 1605 tmp++; 1606 if (*tmp == '\0') 1607 tmp = NULL; 1608 } else { /* Structure */ 1609 if (*tmp == '.') { 1610 str = tmp + 1; 1611 (*fieldp)->ref = false; 1612 } else if (tmp[1] == '>') { 1613 str = tmp + 2; 1614 (*fieldp)->ref = true; 1615 } else { 1616 semantic_error("Argument parse error: %s\n", 1617 str); 1618 return -EINVAL; 1619 } 1620 tmp = strpbrk(str, "-.["); 1621 } 1622 if (tmp) { 1623 (*fieldp)->name = strndup(str, tmp - str); 1624 if ((*fieldp)->name == NULL) 1625 return -ENOMEM; 1626 if (*str != '[') 1627 goodname = (*fieldp)->name; 1628 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref); 1629 fieldp = &(*fieldp)->next; 1630 } 1631 } while (tmp); 1632 (*fieldp)->name = strdup(str); 1633 if ((*fieldp)->name == NULL) 1634 return -ENOMEM; 1635 if (*str != '[') 1636 goodname = (*fieldp)->name; 1637 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref); 1638 1639 /* If no name is specified, set the last field name (not array index)*/ 1640 if (!arg->name) { 1641 arg->name = strdup(goodname); 1642 if (arg->name == NULL) 1643 return -ENOMEM; 1644 } 1645 return 0; 1646 } 1647 1648 /* Parse perf-probe event command */ 1649 int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev) 1650 { 1651 char **argv; 1652 int argc, i, ret = 0; 1653 1654 argv = argv_split(cmd, &argc); 1655 if (!argv) { 1656 pr_debug("Failed to split arguments.\n"); 1657 return -ENOMEM; 1658 } 1659 if (argc - 1 > MAX_PROBE_ARGS) { 1660 semantic_error("Too many probe arguments (%d).\n", argc - 1); 1661 ret = -ERANGE; 1662 goto out; 1663 } 1664 /* Parse probe point */ 1665 ret = parse_perf_probe_point(argv[0], pev); 1666 if (ret < 0) 1667 goto out; 1668 1669 /* Copy arguments and ensure return probe has no C argument */ 1670 pev->nargs = argc - 1; 1671 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs); 1672 if (pev->args == NULL) { 1673 ret = -ENOMEM; 1674 goto out; 1675 } 1676 for (i = 0; i < pev->nargs && ret >= 0; i++) { 1677 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]); 1678 if (ret >= 0 && 1679 is_c_varname(pev->args[i].var) && pev->point.retprobe) { 1680 semantic_error("You can't specify local variable for" 1681 " kretprobe.\n"); 1682 ret = -EINVAL; 1683 } 1684 } 1685 out: 1686 argv_free(argv); 1687 1688 return ret; 1689 } 1690 1691 /* Returns true if *any* ARG is either C variable, $params or $vars. */ 1692 bool perf_probe_with_var(struct perf_probe_event *pev) 1693 { 1694 int i = 0; 1695 1696 for (i = 0; i < pev->nargs; i++) 1697 if (is_c_varname(pev->args[i].var) || 1698 !strcmp(pev->args[i].var, PROBE_ARG_PARAMS) || 1699 !strcmp(pev->args[i].var, PROBE_ARG_VARS)) 1700 return true; 1701 return false; 1702 } 1703 1704 /* Return true if this perf_probe_event requires debuginfo */ 1705 bool perf_probe_event_need_dwarf(struct perf_probe_event *pev) 1706 { 1707 if (pev->point.file || pev->point.line || pev->point.lazy_line) 1708 return true; 1709 1710 if (perf_probe_with_var(pev)) 1711 return true; 1712 1713 return false; 1714 } 1715 1716 /* Parse probe_events event into struct probe_point */ 1717 int parse_probe_trace_command(const char *cmd, struct probe_trace_event *tev) 1718 { 1719 struct probe_trace_point *tp = &tev->point; 1720 char pr; 1721 char *p; 1722 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str; 1723 int ret, i, argc; 1724 char **argv; 1725 1726 pr_debug("Parsing probe_events: %s\n", cmd); 1727 argv = argv_split(cmd, &argc); 1728 if (!argv) { 1729 pr_debug("Failed to split arguments.\n"); 1730 return -ENOMEM; 1731 } 1732 if (argc < 2) { 1733 semantic_error("Too few probe arguments.\n"); 1734 ret = -ERANGE; 1735 goto out; 1736 } 1737 1738 /* Scan event and group name. */ 1739 argv0_str = strdup(argv[0]); 1740 if (argv0_str == NULL) { 1741 ret = -ENOMEM; 1742 goto out; 1743 } 1744 fmt1_str = strtok_r(argv0_str, ":", &fmt); 1745 fmt2_str = strtok_r(NULL, "/", &fmt); 1746 fmt3_str = strtok_r(NULL, " \t", &fmt); 1747 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL 1748 || fmt3_str == NULL) { 1749 semantic_error("Failed to parse event name: %s\n", argv[0]); 1750 ret = -EINVAL; 1751 goto out; 1752 } 1753 pr = fmt1_str[0]; 1754 tev->group = strdup(fmt2_str); 1755 tev->event = strdup(fmt3_str); 1756 if (tev->group == NULL || tev->event == NULL) { 1757 ret = -ENOMEM; 1758 goto out; 1759 } 1760 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr); 1761 1762 tp->retprobe = (pr == 'r'); 1763 1764 /* Scan module name(if there), function name and offset */ 1765 p = strchr(argv[1], ':'); 1766 if (p) { 1767 tp->module = strndup(argv[1], p - argv[1]); 1768 if (!tp->module) { 1769 ret = -ENOMEM; 1770 goto out; 1771 } 1772 tev->uprobes = (tp->module[0] == '/'); 1773 p++; 1774 } else 1775 p = argv[1]; 1776 fmt1_str = strtok_r(p, "+", &fmt); 1777 /* only the address started with 0x */ 1778 if (fmt1_str[0] == '0') { 1779 /* 1780 * Fix a special case: 1781 * if address == 0, kernel reports something like: 1782 * p:probe_libc/abs_0 /lib/libc-2.18.so:0x (null) arg1=%ax 1783 * Newer kernel may fix that, but we want to 1784 * support old kernel also. 1785 */ 1786 if (strcmp(fmt1_str, "0x") == 0) { 1787 if (!argv[2] || strcmp(argv[2], "(null)")) { 1788 ret = -EINVAL; 1789 goto out; 1790 } 1791 tp->address = 0; 1792 1793 free(argv[2]); 1794 for (i = 2; argv[i + 1] != NULL; i++) 1795 argv[i] = argv[i + 1]; 1796 1797 argv[i] = NULL; 1798 argc -= 1; 1799 } else 1800 tp->address = strtoul(fmt1_str, NULL, 0); 1801 } else { 1802 /* Only the symbol-based probe has offset */ 1803 tp->symbol = strdup(fmt1_str); 1804 if (tp->symbol == NULL) { 1805 ret = -ENOMEM; 1806 goto out; 1807 } 1808 fmt2_str = strtok_r(NULL, "", &fmt); 1809 if (fmt2_str == NULL) 1810 tp->offset = 0; 1811 else 1812 tp->offset = strtoul(fmt2_str, NULL, 10); 1813 } 1814 1815 if (tev->uprobes) { 1816 fmt2_str = strchr(p, '('); 1817 if (fmt2_str) 1818 tp->ref_ctr_offset = strtoul(fmt2_str + 1, NULL, 0); 1819 } 1820 1821 tev->nargs = argc - 2; 1822 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs); 1823 if (tev->args == NULL) { 1824 ret = -ENOMEM; 1825 goto out; 1826 } 1827 for (i = 0; i < tev->nargs; i++) { 1828 p = strchr(argv[i + 2], '='); 1829 if (p) /* We don't need which register is assigned. */ 1830 *p++ = '\0'; 1831 else 1832 p = argv[i + 2]; 1833 tev->args[i].name = strdup(argv[i + 2]); 1834 /* TODO: parse regs and offset */ 1835 tev->args[i].value = strdup(p); 1836 if (tev->args[i].name == NULL || tev->args[i].value == NULL) { 1837 ret = -ENOMEM; 1838 goto out; 1839 } 1840 } 1841 ret = 0; 1842 out: 1843 free(argv0_str); 1844 argv_free(argv); 1845 return ret; 1846 } 1847 1848 /* Compose only probe arg */ 1849 char *synthesize_perf_probe_arg(struct perf_probe_arg *pa) 1850 { 1851 struct perf_probe_arg_field *field = pa->field; 1852 struct strbuf buf; 1853 char *ret = NULL; 1854 int err; 1855 1856 if (strbuf_init(&buf, 64) < 0) 1857 return NULL; 1858 1859 if (pa->name && pa->var) 1860 err = strbuf_addf(&buf, "%s=%s", pa->name, pa->var); 1861 else 1862 err = strbuf_addstr(&buf, pa->name ?: pa->var); 1863 if (err) 1864 goto out; 1865 1866 while (field) { 1867 if (field->name[0] == '[') 1868 err = strbuf_addstr(&buf, field->name); 1869 else 1870 err = strbuf_addf(&buf, "%s%s", field->ref ? "->" : ".", 1871 field->name); 1872 field = field->next; 1873 if (err) 1874 goto out; 1875 } 1876 1877 if (pa->type) 1878 if (strbuf_addf(&buf, ":%s", pa->type) < 0) 1879 goto out; 1880 1881 ret = strbuf_detach(&buf, NULL); 1882 out: 1883 strbuf_release(&buf); 1884 return ret; 1885 } 1886 1887 /* Compose only probe point (not argument) */ 1888 char *synthesize_perf_probe_point(struct perf_probe_point *pp) 1889 { 1890 struct strbuf buf; 1891 char *tmp, *ret = NULL; 1892 int len, err = 0; 1893 1894 if (strbuf_init(&buf, 64) < 0) 1895 return NULL; 1896 1897 if (pp->function) { 1898 if (strbuf_addstr(&buf, pp->function) < 0) 1899 goto out; 1900 if (pp->offset) 1901 err = strbuf_addf(&buf, "+%lu", pp->offset); 1902 else if (pp->line) 1903 err = strbuf_addf(&buf, ":%d", pp->line); 1904 else if (pp->retprobe) 1905 err = strbuf_addstr(&buf, "%return"); 1906 if (err) 1907 goto out; 1908 } 1909 if (pp->file) { 1910 tmp = pp->file; 1911 len = strlen(tmp); 1912 if (len > 30) { 1913 tmp = strchr(pp->file + len - 30, '/'); 1914 tmp = tmp ? tmp + 1 : pp->file + len - 30; 1915 } 1916 err = strbuf_addf(&buf, "@%s", tmp); 1917 if (!err && !pp->function && pp->line) 1918 err = strbuf_addf(&buf, ":%d", pp->line); 1919 } 1920 if (!err) 1921 ret = strbuf_detach(&buf, NULL); 1922 out: 1923 strbuf_release(&buf); 1924 return ret; 1925 } 1926 1927 char *synthesize_perf_probe_command(struct perf_probe_event *pev) 1928 { 1929 struct strbuf buf; 1930 char *tmp, *ret = NULL; 1931 int i; 1932 1933 if (strbuf_init(&buf, 64)) 1934 return NULL; 1935 if (pev->event) 1936 if (strbuf_addf(&buf, "%s:%s=", pev->group ?: PERFPROBE_GROUP, 1937 pev->event) < 0) 1938 goto out; 1939 1940 tmp = synthesize_perf_probe_point(&pev->point); 1941 if (!tmp || strbuf_addstr(&buf, tmp) < 0) 1942 goto out; 1943 free(tmp); 1944 1945 for (i = 0; i < pev->nargs; i++) { 1946 tmp = synthesize_perf_probe_arg(pev->args + i); 1947 if (!tmp || strbuf_addf(&buf, " %s", tmp) < 0) 1948 goto out; 1949 free(tmp); 1950 } 1951 1952 ret = strbuf_detach(&buf, NULL); 1953 out: 1954 strbuf_release(&buf); 1955 return ret; 1956 } 1957 1958 static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref, 1959 struct strbuf *buf, int depth) 1960 { 1961 int err; 1962 if (ref->next) { 1963 depth = __synthesize_probe_trace_arg_ref(ref->next, buf, 1964 depth + 1); 1965 if (depth < 0) 1966 return depth; 1967 } 1968 err = strbuf_addf(buf, "%+ld(", ref->offset); 1969 return (err < 0) ? err : depth; 1970 } 1971 1972 static int synthesize_probe_trace_arg(struct probe_trace_arg *arg, 1973 struct strbuf *buf) 1974 { 1975 struct probe_trace_arg_ref *ref = arg->ref; 1976 int depth = 0, err; 1977 1978 /* Argument name or separator */ 1979 if (arg->name) 1980 err = strbuf_addf(buf, " %s=", arg->name); 1981 else 1982 err = strbuf_addch(buf, ' '); 1983 if (err) 1984 return err; 1985 1986 /* Special case: @XXX */ 1987 if (arg->value[0] == '@' && arg->ref) 1988 ref = ref->next; 1989 1990 /* Dereferencing arguments */ 1991 if (ref) { 1992 depth = __synthesize_probe_trace_arg_ref(ref, buf, 1); 1993 if (depth < 0) 1994 return depth; 1995 } 1996 1997 /* Print argument value */ 1998 if (arg->value[0] == '@' && arg->ref) 1999 err = strbuf_addf(buf, "%s%+ld", arg->value, arg->ref->offset); 2000 else 2001 err = strbuf_addstr(buf, arg->value); 2002 2003 /* Closing */ 2004 while (!err && depth--) 2005 err = strbuf_addch(buf, ')'); 2006 2007 /* Print argument type */ 2008 if (!err && arg->type) 2009 err = strbuf_addf(buf, ":%s", arg->type); 2010 2011 return err; 2012 } 2013 2014 static int 2015 synthesize_uprobe_trace_def(struct probe_trace_event *tev, struct strbuf *buf) 2016 { 2017 struct probe_trace_point *tp = &tev->point; 2018 int err; 2019 2020 err = strbuf_addf(buf, "%s:0x%lx", tp->module, tp->address); 2021 2022 if (err >= 0 && tp->ref_ctr_offset) { 2023 if (!uprobe_ref_ctr_is_supported()) 2024 return -1; 2025 err = strbuf_addf(buf, "(0x%lx)", tp->ref_ctr_offset); 2026 } 2027 return err >= 0 ? 0 : -1; 2028 } 2029 2030 char *synthesize_probe_trace_command(struct probe_trace_event *tev) 2031 { 2032 struct probe_trace_point *tp = &tev->point; 2033 struct strbuf buf; 2034 char *ret = NULL; 2035 int i, err; 2036 2037 /* Uprobes must have tp->module */ 2038 if (tev->uprobes && !tp->module) 2039 return NULL; 2040 2041 if (strbuf_init(&buf, 32) < 0) 2042 return NULL; 2043 2044 if (strbuf_addf(&buf, "%c:%s/%s ", tp->retprobe ? 'r' : 'p', 2045 tev->group, tev->event) < 0) 2046 goto error; 2047 /* 2048 * If tp->address == 0, then this point must be a 2049 * absolute address uprobe. 2050 * try_to_find_absolute_address() should have made 2051 * tp->symbol to "0x0". 2052 */ 2053 if (tev->uprobes && !tp->address) { 2054 if (!tp->symbol || strcmp(tp->symbol, "0x0")) 2055 goto error; 2056 } 2057 2058 /* Use the tp->address for uprobes */ 2059 if (tev->uprobes) { 2060 err = synthesize_uprobe_trace_def(tev, &buf); 2061 } else if (!strncmp(tp->symbol, "0x", 2)) { 2062 /* Absolute address. See try_to_find_absolute_address() */ 2063 err = strbuf_addf(&buf, "%s%s0x%lx", tp->module ?: "", 2064 tp->module ? ":" : "", tp->address); 2065 } else { 2066 err = strbuf_addf(&buf, "%s%s%s+%lu", tp->module ?: "", 2067 tp->module ? ":" : "", tp->symbol, tp->offset); 2068 } 2069 2070 if (err) 2071 goto error; 2072 2073 for (i = 0; i < tev->nargs; i++) 2074 if (synthesize_probe_trace_arg(&tev->args[i], &buf) < 0) 2075 goto error; 2076 2077 ret = strbuf_detach(&buf, NULL); 2078 error: 2079 strbuf_release(&buf); 2080 return ret; 2081 } 2082 2083 static int find_perf_probe_point_from_map(struct probe_trace_point *tp, 2084 struct perf_probe_point *pp, 2085 bool is_kprobe) 2086 { 2087 struct symbol *sym = NULL; 2088 struct map *map = NULL; 2089 u64 addr = tp->address; 2090 int ret = -ENOENT; 2091 2092 if (!is_kprobe) { 2093 map = dso__new_map(tp->module); 2094 if (!map) 2095 goto out; 2096 sym = map__find_symbol(map, addr); 2097 } else { 2098 if (tp->symbol && !addr) { 2099 if (kernel_get_symbol_address_by_name(tp->symbol, 2100 &addr, true, false) < 0) 2101 goto out; 2102 } 2103 if (addr) { 2104 addr += tp->offset; 2105 sym = machine__find_kernel_symbol(host_machine, addr, &map); 2106 } 2107 } 2108 2109 if (!sym) 2110 goto out; 2111 2112 pp->retprobe = tp->retprobe; 2113 pp->offset = addr - map->unmap_ip(map, sym->start); 2114 pp->function = strdup(sym->name); 2115 ret = pp->function ? 0 : -ENOMEM; 2116 2117 out: 2118 if (map && !is_kprobe) { 2119 map__put(map); 2120 } 2121 2122 return ret; 2123 } 2124 2125 static int convert_to_perf_probe_point(struct probe_trace_point *tp, 2126 struct perf_probe_point *pp, 2127 bool is_kprobe) 2128 { 2129 char buf[128]; 2130 int ret; 2131 2132 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe); 2133 if (!ret) 2134 return 0; 2135 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe); 2136 if (!ret) 2137 return 0; 2138 2139 pr_debug("Failed to find probe point from both of dwarf and map.\n"); 2140 2141 if (tp->symbol) { 2142 pp->function = strdup(tp->symbol); 2143 pp->offset = tp->offset; 2144 } else { 2145 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address); 2146 if (ret < 0) 2147 return ret; 2148 pp->function = strdup(buf); 2149 pp->offset = 0; 2150 } 2151 if (pp->function == NULL) 2152 return -ENOMEM; 2153 2154 pp->retprobe = tp->retprobe; 2155 2156 return 0; 2157 } 2158 2159 static int convert_to_perf_probe_event(struct probe_trace_event *tev, 2160 struct perf_probe_event *pev, bool is_kprobe) 2161 { 2162 struct strbuf buf = STRBUF_INIT; 2163 int i, ret; 2164 2165 /* Convert event/group name */ 2166 pev->event = strdup(tev->event); 2167 pev->group = strdup(tev->group); 2168 if (pev->event == NULL || pev->group == NULL) 2169 return -ENOMEM; 2170 2171 /* Convert trace_point to probe_point */ 2172 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe); 2173 if (ret < 0) 2174 return ret; 2175 2176 /* Convert trace_arg to probe_arg */ 2177 pev->nargs = tev->nargs; 2178 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs); 2179 if (pev->args == NULL) 2180 return -ENOMEM; 2181 for (i = 0; i < tev->nargs && ret >= 0; i++) { 2182 if (tev->args[i].name) 2183 pev->args[i].name = strdup(tev->args[i].name); 2184 else { 2185 if ((ret = strbuf_init(&buf, 32)) < 0) 2186 goto error; 2187 ret = synthesize_probe_trace_arg(&tev->args[i], &buf); 2188 pev->args[i].name = strbuf_detach(&buf, NULL); 2189 } 2190 if (pev->args[i].name == NULL && ret >= 0) 2191 ret = -ENOMEM; 2192 } 2193 error: 2194 if (ret < 0) 2195 clear_perf_probe_event(pev); 2196 2197 return ret; 2198 } 2199 2200 void clear_perf_probe_event(struct perf_probe_event *pev) 2201 { 2202 struct perf_probe_arg_field *field, *next; 2203 int i; 2204 2205 zfree(&pev->event); 2206 zfree(&pev->group); 2207 zfree(&pev->target); 2208 clear_perf_probe_point(&pev->point); 2209 2210 for (i = 0; i < pev->nargs; i++) { 2211 zfree(&pev->args[i].name); 2212 zfree(&pev->args[i].var); 2213 zfree(&pev->args[i].type); 2214 field = pev->args[i].field; 2215 while (field) { 2216 next = field->next; 2217 zfree(&field->name); 2218 free(field); 2219 field = next; 2220 } 2221 } 2222 zfree(&pev->args); 2223 } 2224 2225 #define strdup_or_goto(str, label) \ 2226 ({ char *__p = NULL; if (str && !(__p = strdup(str))) goto label; __p; }) 2227 2228 static int perf_probe_point__copy(struct perf_probe_point *dst, 2229 struct perf_probe_point *src) 2230 { 2231 dst->file = strdup_or_goto(src->file, out_err); 2232 dst->function = strdup_or_goto(src->function, out_err); 2233 dst->lazy_line = strdup_or_goto(src->lazy_line, out_err); 2234 dst->line = src->line; 2235 dst->retprobe = src->retprobe; 2236 dst->offset = src->offset; 2237 return 0; 2238 2239 out_err: 2240 clear_perf_probe_point(dst); 2241 return -ENOMEM; 2242 } 2243 2244 static int perf_probe_arg__copy(struct perf_probe_arg *dst, 2245 struct perf_probe_arg *src) 2246 { 2247 struct perf_probe_arg_field *field, **ppfield; 2248 2249 dst->name = strdup_or_goto(src->name, out_err); 2250 dst->var = strdup_or_goto(src->var, out_err); 2251 dst->type = strdup_or_goto(src->type, out_err); 2252 2253 field = src->field; 2254 ppfield = &(dst->field); 2255 while (field) { 2256 *ppfield = zalloc(sizeof(*field)); 2257 if (!*ppfield) 2258 goto out_err; 2259 (*ppfield)->name = strdup_or_goto(field->name, out_err); 2260 (*ppfield)->index = field->index; 2261 (*ppfield)->ref = field->ref; 2262 field = field->next; 2263 ppfield = &((*ppfield)->next); 2264 } 2265 return 0; 2266 out_err: 2267 return -ENOMEM; 2268 } 2269 2270 int perf_probe_event__copy(struct perf_probe_event *dst, 2271 struct perf_probe_event *src) 2272 { 2273 int i; 2274 2275 dst->event = strdup_or_goto(src->event, out_err); 2276 dst->group = strdup_or_goto(src->group, out_err); 2277 dst->target = strdup_or_goto(src->target, out_err); 2278 dst->uprobes = src->uprobes; 2279 2280 if (perf_probe_point__copy(&dst->point, &src->point) < 0) 2281 goto out_err; 2282 2283 dst->args = zalloc(sizeof(struct perf_probe_arg) * src->nargs); 2284 if (!dst->args) 2285 goto out_err; 2286 dst->nargs = src->nargs; 2287 2288 for (i = 0; i < src->nargs; i++) 2289 if (perf_probe_arg__copy(&dst->args[i], &src->args[i]) < 0) 2290 goto out_err; 2291 return 0; 2292 2293 out_err: 2294 clear_perf_probe_event(dst); 2295 return -ENOMEM; 2296 } 2297 2298 void clear_probe_trace_event(struct probe_trace_event *tev) 2299 { 2300 struct probe_trace_arg_ref *ref, *next; 2301 int i; 2302 2303 zfree(&tev->event); 2304 zfree(&tev->group); 2305 zfree(&tev->point.symbol); 2306 zfree(&tev->point.realname); 2307 zfree(&tev->point.module); 2308 for (i = 0; i < tev->nargs; i++) { 2309 zfree(&tev->args[i].name); 2310 zfree(&tev->args[i].value); 2311 zfree(&tev->args[i].type); 2312 ref = tev->args[i].ref; 2313 while (ref) { 2314 next = ref->next; 2315 free(ref); 2316 ref = next; 2317 } 2318 } 2319 zfree(&tev->args); 2320 } 2321 2322 struct kprobe_blacklist_node { 2323 struct list_head list; 2324 unsigned long start; 2325 unsigned long end; 2326 char *symbol; 2327 }; 2328 2329 static void kprobe_blacklist__delete(struct list_head *blacklist) 2330 { 2331 struct kprobe_blacklist_node *node; 2332 2333 while (!list_empty(blacklist)) { 2334 node = list_first_entry(blacklist, 2335 struct kprobe_blacklist_node, list); 2336 list_del_init(&node->list); 2337 zfree(&node->symbol); 2338 free(node); 2339 } 2340 } 2341 2342 static int kprobe_blacklist__load(struct list_head *blacklist) 2343 { 2344 struct kprobe_blacklist_node *node; 2345 const char *__debugfs = debugfs__mountpoint(); 2346 char buf[PATH_MAX], *p; 2347 FILE *fp; 2348 int ret; 2349 2350 if (__debugfs == NULL) 2351 return -ENOTSUP; 2352 2353 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs); 2354 if (ret < 0) 2355 return ret; 2356 2357 fp = fopen(buf, "r"); 2358 if (!fp) 2359 return -errno; 2360 2361 ret = 0; 2362 while (fgets(buf, PATH_MAX, fp)) { 2363 node = zalloc(sizeof(*node)); 2364 if (!node) { 2365 ret = -ENOMEM; 2366 break; 2367 } 2368 INIT_LIST_HEAD(&node->list); 2369 list_add_tail(&node->list, blacklist); 2370 if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) { 2371 ret = -EINVAL; 2372 break; 2373 } 2374 p = strchr(buf, '\t'); 2375 if (p) { 2376 p++; 2377 if (p[strlen(p) - 1] == '\n') 2378 p[strlen(p) - 1] = '\0'; 2379 } else 2380 p = (char *)"unknown"; 2381 node->symbol = strdup(p); 2382 if (!node->symbol) { 2383 ret = -ENOMEM; 2384 break; 2385 } 2386 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n", 2387 node->start, node->end, node->symbol); 2388 ret++; 2389 } 2390 if (ret < 0) 2391 kprobe_blacklist__delete(blacklist); 2392 fclose(fp); 2393 2394 return ret; 2395 } 2396 2397 static struct kprobe_blacklist_node * 2398 kprobe_blacklist__find_by_address(struct list_head *blacklist, 2399 unsigned long address) 2400 { 2401 struct kprobe_blacklist_node *node; 2402 2403 list_for_each_entry(node, blacklist, list) { 2404 if (node->start <= address && address < node->end) 2405 return node; 2406 } 2407 2408 return NULL; 2409 } 2410 2411 static LIST_HEAD(kprobe_blacklist); 2412 2413 static void kprobe_blacklist__init(void) 2414 { 2415 if (!list_empty(&kprobe_blacklist)) 2416 return; 2417 2418 if (kprobe_blacklist__load(&kprobe_blacklist) < 0) 2419 pr_debug("No kprobe blacklist support, ignored\n"); 2420 } 2421 2422 static void kprobe_blacklist__release(void) 2423 { 2424 kprobe_blacklist__delete(&kprobe_blacklist); 2425 } 2426 2427 static bool kprobe_blacklist__listed(unsigned long address) 2428 { 2429 return !!kprobe_blacklist__find_by_address(&kprobe_blacklist, address); 2430 } 2431 2432 static int perf_probe_event__sprintf(const char *group, const char *event, 2433 struct perf_probe_event *pev, 2434 const char *module, 2435 struct strbuf *result) 2436 { 2437 int i, ret; 2438 char *buf; 2439 2440 if (asprintf(&buf, "%s:%s", group, event) < 0) 2441 return -errno; 2442 ret = strbuf_addf(result, " %-20s (on ", buf); 2443 free(buf); 2444 if (ret) 2445 return ret; 2446 2447 /* Synthesize only event probe point */ 2448 buf = synthesize_perf_probe_point(&pev->point); 2449 if (!buf) 2450 return -ENOMEM; 2451 ret = strbuf_addstr(result, buf); 2452 free(buf); 2453 2454 if (!ret && module) 2455 ret = strbuf_addf(result, " in %s", module); 2456 2457 if (!ret && pev->nargs > 0) { 2458 ret = strbuf_add(result, " with", 5); 2459 for (i = 0; !ret && i < pev->nargs; i++) { 2460 buf = synthesize_perf_probe_arg(&pev->args[i]); 2461 if (!buf) 2462 return -ENOMEM; 2463 ret = strbuf_addf(result, " %s", buf); 2464 free(buf); 2465 } 2466 } 2467 if (!ret) 2468 ret = strbuf_addch(result, ')'); 2469 2470 return ret; 2471 } 2472 2473 /* Show an event */ 2474 int show_perf_probe_event(const char *group, const char *event, 2475 struct perf_probe_event *pev, 2476 const char *module, bool use_stdout) 2477 { 2478 struct strbuf buf = STRBUF_INIT; 2479 int ret; 2480 2481 ret = perf_probe_event__sprintf(group, event, pev, module, &buf); 2482 if (ret >= 0) { 2483 if (use_stdout) 2484 printf("%s\n", buf.buf); 2485 else 2486 pr_info("%s\n", buf.buf); 2487 } 2488 strbuf_release(&buf); 2489 2490 return ret; 2491 } 2492 2493 static bool filter_probe_trace_event(struct probe_trace_event *tev, 2494 struct strfilter *filter) 2495 { 2496 char tmp[128]; 2497 2498 /* At first, check the event name itself */ 2499 if (strfilter__compare(filter, tev->event)) 2500 return true; 2501 2502 /* Next, check the combination of name and group */ 2503 if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0) 2504 return false; 2505 return strfilter__compare(filter, tmp); 2506 } 2507 2508 static int __show_perf_probe_events(int fd, bool is_kprobe, 2509 struct strfilter *filter) 2510 { 2511 int ret = 0; 2512 struct probe_trace_event tev; 2513 struct perf_probe_event pev; 2514 struct strlist *rawlist; 2515 struct str_node *ent; 2516 2517 memset(&tev, 0, sizeof(tev)); 2518 memset(&pev, 0, sizeof(pev)); 2519 2520 rawlist = probe_file__get_rawlist(fd); 2521 if (!rawlist) 2522 return -ENOMEM; 2523 2524 strlist__for_each_entry(ent, rawlist) { 2525 ret = parse_probe_trace_command(ent->s, &tev); 2526 if (ret >= 0) { 2527 if (!filter_probe_trace_event(&tev, filter)) 2528 goto next; 2529 ret = convert_to_perf_probe_event(&tev, &pev, 2530 is_kprobe); 2531 if (ret < 0) 2532 goto next; 2533 ret = show_perf_probe_event(pev.group, pev.event, 2534 &pev, tev.point.module, 2535 true); 2536 } 2537 next: 2538 clear_perf_probe_event(&pev); 2539 clear_probe_trace_event(&tev); 2540 if (ret < 0) 2541 break; 2542 } 2543 strlist__delete(rawlist); 2544 /* Cleanup cached debuginfo if needed */ 2545 debuginfo_cache__exit(); 2546 2547 return ret; 2548 } 2549 2550 /* List up current perf-probe events */ 2551 int show_perf_probe_events(struct strfilter *filter) 2552 { 2553 int kp_fd, up_fd, ret; 2554 2555 setup_pager(); 2556 2557 if (probe_conf.cache) 2558 return probe_cache__show_all_caches(filter); 2559 2560 ret = init_probe_symbol_maps(false); 2561 if (ret < 0) 2562 return ret; 2563 2564 ret = probe_file__open_both(&kp_fd, &up_fd, 0); 2565 if (ret < 0) 2566 return ret; 2567 2568 if (kp_fd >= 0) 2569 ret = __show_perf_probe_events(kp_fd, true, filter); 2570 if (up_fd >= 0 && ret >= 0) 2571 ret = __show_perf_probe_events(up_fd, false, filter); 2572 if (kp_fd > 0) 2573 close(kp_fd); 2574 if (up_fd > 0) 2575 close(up_fd); 2576 exit_probe_symbol_maps(); 2577 2578 return ret; 2579 } 2580 2581 static int get_new_event_name(char *buf, size_t len, const char *base, 2582 struct strlist *namelist, bool ret_event, 2583 bool allow_suffix) 2584 { 2585 int i, ret; 2586 char *p, *nbase; 2587 2588 if (*base == '.') 2589 base++; 2590 nbase = strdup(base); 2591 if (!nbase) 2592 return -ENOMEM; 2593 2594 /* Cut off the dot suffixes (e.g. .const, .isra) and version suffixes */ 2595 p = strpbrk(nbase, ".@"); 2596 if (p && p != nbase) 2597 *p = '\0'; 2598 2599 /* Try no suffix number */ 2600 ret = e_snprintf(buf, len, "%s%s", nbase, ret_event ? "__return" : ""); 2601 if (ret < 0) { 2602 pr_debug("snprintf() failed: %d\n", ret); 2603 goto out; 2604 } 2605 if (!strlist__has_entry(namelist, buf)) 2606 goto out; 2607 2608 if (!allow_suffix) { 2609 pr_warning("Error: event \"%s\" already exists.\n" 2610 " Hint: Remove existing event by 'perf probe -d'\n" 2611 " or force duplicates by 'perf probe -f'\n" 2612 " or set 'force=yes' in BPF source.\n", 2613 buf); 2614 ret = -EEXIST; 2615 goto out; 2616 } 2617 2618 /* Try to add suffix */ 2619 for (i = 1; i < MAX_EVENT_INDEX; i++) { 2620 ret = e_snprintf(buf, len, "%s_%d", nbase, i); 2621 if (ret < 0) { 2622 pr_debug("snprintf() failed: %d\n", ret); 2623 goto out; 2624 } 2625 if (!strlist__has_entry(namelist, buf)) 2626 break; 2627 } 2628 if (i == MAX_EVENT_INDEX) { 2629 pr_warning("Too many events are on the same function.\n"); 2630 ret = -ERANGE; 2631 } 2632 2633 out: 2634 free(nbase); 2635 2636 /* Final validation */ 2637 if (ret >= 0 && !is_c_func_name(buf)) { 2638 pr_warning("Internal error: \"%s\" is an invalid event name.\n", 2639 buf); 2640 ret = -EINVAL; 2641 } 2642 2643 return ret; 2644 } 2645 2646 /* Warn if the current kernel's uprobe implementation is old */ 2647 static void warn_uprobe_event_compat(struct probe_trace_event *tev) 2648 { 2649 int i; 2650 char *buf = synthesize_probe_trace_command(tev); 2651 struct probe_trace_point *tp = &tev->point; 2652 2653 if (tp->ref_ctr_offset && !uprobe_ref_ctr_is_supported()) { 2654 pr_warning("A semaphore is associated with %s:%s and " 2655 "seems your kernel doesn't support it.\n", 2656 tev->group, tev->event); 2657 } 2658 2659 /* Old uprobe event doesn't support memory dereference */ 2660 if (!tev->uprobes || tev->nargs == 0 || !buf) 2661 goto out; 2662 2663 for (i = 0; i < tev->nargs; i++) 2664 if (strglobmatch(tev->args[i].value, "[$@+-]*")) { 2665 pr_warning("Please upgrade your kernel to at least " 2666 "3.14 to have access to feature %s\n", 2667 tev->args[i].value); 2668 break; 2669 } 2670 out: 2671 free(buf); 2672 } 2673 2674 /* Set new name from original perf_probe_event and namelist */ 2675 static int probe_trace_event__set_name(struct probe_trace_event *tev, 2676 struct perf_probe_event *pev, 2677 struct strlist *namelist, 2678 bool allow_suffix) 2679 { 2680 const char *event, *group; 2681 char buf[64]; 2682 int ret; 2683 2684 /* If probe_event or trace_event already have the name, reuse it */ 2685 if (pev->event && !pev->sdt) 2686 event = pev->event; 2687 else if (tev->event) 2688 event = tev->event; 2689 else { 2690 /* Or generate new one from probe point */ 2691 if (pev->point.function && 2692 (strncmp(pev->point.function, "0x", 2) != 0) && 2693 !strisglob(pev->point.function)) 2694 event = pev->point.function; 2695 else 2696 event = tev->point.realname; 2697 } 2698 if (pev->group && !pev->sdt) 2699 group = pev->group; 2700 else if (tev->group) 2701 group = tev->group; 2702 else 2703 group = PERFPROBE_GROUP; 2704 2705 /* Get an unused new event name */ 2706 ret = get_new_event_name(buf, 64, event, namelist, 2707 tev->point.retprobe, allow_suffix); 2708 if (ret < 0) 2709 return ret; 2710 2711 event = buf; 2712 2713 tev->event = strdup(event); 2714 tev->group = strdup(group); 2715 if (tev->event == NULL || tev->group == NULL) 2716 return -ENOMEM; 2717 2718 /* Add added event name to namelist */ 2719 strlist__add(namelist, event); 2720 return 0; 2721 } 2722 2723 static int __open_probe_file_and_namelist(bool uprobe, 2724 struct strlist **namelist) 2725 { 2726 int fd; 2727 2728 fd = probe_file__open(PF_FL_RW | (uprobe ? PF_FL_UPROBE : 0)); 2729 if (fd < 0) 2730 return fd; 2731 2732 /* Get current event names */ 2733 *namelist = probe_file__get_namelist(fd); 2734 if (!(*namelist)) { 2735 pr_debug("Failed to get current event list.\n"); 2736 close(fd); 2737 return -ENOMEM; 2738 } 2739 return fd; 2740 } 2741 2742 static int __add_probe_trace_events(struct perf_probe_event *pev, 2743 struct probe_trace_event *tevs, 2744 int ntevs, bool allow_suffix) 2745 { 2746 int i, fd[2] = {-1, -1}, up, ret; 2747 struct probe_trace_event *tev = NULL; 2748 struct probe_cache *cache = NULL; 2749 struct strlist *namelist[2] = {NULL, NULL}; 2750 struct nscookie nsc; 2751 2752 up = pev->uprobes ? 1 : 0; 2753 fd[up] = __open_probe_file_and_namelist(up, &namelist[up]); 2754 if (fd[up] < 0) 2755 return fd[up]; 2756 2757 ret = 0; 2758 for (i = 0; i < ntevs; i++) { 2759 tev = &tevs[i]; 2760 up = tev->uprobes ? 1 : 0; 2761 if (fd[up] == -1) { /* Open the kprobe/uprobe_events */ 2762 fd[up] = __open_probe_file_and_namelist(up, 2763 &namelist[up]); 2764 if (fd[up] < 0) 2765 goto close_out; 2766 } 2767 /* Skip if the symbol is out of .text or blacklisted */ 2768 if (!tev->point.symbol && !pev->uprobes) 2769 continue; 2770 2771 /* Set new name for tev (and update namelist) */ 2772 ret = probe_trace_event__set_name(tev, pev, namelist[up], 2773 allow_suffix); 2774 if (ret < 0) 2775 break; 2776 2777 nsinfo__mountns_enter(pev->nsi, &nsc); 2778 ret = probe_file__add_event(fd[up], tev); 2779 nsinfo__mountns_exit(&nsc); 2780 if (ret < 0) 2781 break; 2782 2783 /* 2784 * Probes after the first probe which comes from same 2785 * user input are always allowed to add suffix, because 2786 * there might be several addresses corresponding to 2787 * one code line. 2788 */ 2789 allow_suffix = true; 2790 } 2791 if (ret == -EINVAL && pev->uprobes) 2792 warn_uprobe_event_compat(tev); 2793 if (ret == 0 && probe_conf.cache) { 2794 cache = probe_cache__new(pev->target, pev->nsi); 2795 if (!cache || 2796 probe_cache__add_entry(cache, pev, tevs, ntevs) < 0 || 2797 probe_cache__commit(cache) < 0) 2798 pr_warning("Failed to add event to probe cache\n"); 2799 probe_cache__delete(cache); 2800 } 2801 2802 close_out: 2803 for (up = 0; up < 2; up++) { 2804 strlist__delete(namelist[up]); 2805 if (fd[up] >= 0) 2806 close(fd[up]); 2807 } 2808 return ret; 2809 } 2810 2811 static int find_probe_functions(struct map *map, char *name, 2812 struct symbol **syms) 2813 { 2814 int found = 0; 2815 struct symbol *sym; 2816 struct rb_node *tmp; 2817 const char *norm, *ver; 2818 char *buf = NULL; 2819 bool cut_version = true; 2820 2821 if (map__load(map) < 0) 2822 return 0; 2823 2824 /* If user gives a version, don't cut off the version from symbols */ 2825 if (strchr(name, '@')) 2826 cut_version = false; 2827 2828 map__for_each_symbol(map, sym, tmp) { 2829 norm = arch__normalize_symbol_name(sym->name); 2830 if (!norm) 2831 continue; 2832 2833 if (cut_version) { 2834 /* We don't care about default symbol or not */ 2835 ver = strchr(norm, '@'); 2836 if (ver) { 2837 buf = strndup(norm, ver - norm); 2838 if (!buf) 2839 return -ENOMEM; 2840 norm = buf; 2841 } 2842 } 2843 2844 if (strglobmatch(norm, name)) { 2845 found++; 2846 if (syms && found < probe_conf.max_probes) 2847 syms[found - 1] = sym; 2848 } 2849 if (buf) 2850 zfree(&buf); 2851 } 2852 2853 return found; 2854 } 2855 2856 void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused, 2857 struct probe_trace_event *tev __maybe_unused, 2858 struct map *map __maybe_unused, 2859 struct symbol *sym __maybe_unused) { } 2860 2861 /* 2862 * Find probe function addresses from map. 2863 * Return an error or the number of found probe_trace_event 2864 */ 2865 static int find_probe_trace_events_from_map(struct perf_probe_event *pev, 2866 struct probe_trace_event **tevs) 2867 { 2868 struct map *map = NULL; 2869 struct ref_reloc_sym *reloc_sym = NULL; 2870 struct symbol *sym; 2871 struct symbol **syms = NULL; 2872 struct probe_trace_event *tev; 2873 struct perf_probe_point *pp = &pev->point; 2874 struct probe_trace_point *tp; 2875 int num_matched_functions; 2876 int ret, i, j, skipped = 0; 2877 char *mod_name; 2878 2879 map = get_target_map(pev->target, pev->nsi, pev->uprobes); 2880 if (!map) { 2881 ret = -EINVAL; 2882 goto out; 2883 } 2884 2885 syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes); 2886 if (!syms) { 2887 ret = -ENOMEM; 2888 goto out; 2889 } 2890 2891 /* 2892 * Load matched symbols: Since the different local symbols may have 2893 * same name but different addresses, this lists all the symbols. 2894 */ 2895 num_matched_functions = find_probe_functions(map, pp->function, syms); 2896 if (num_matched_functions <= 0) { 2897 pr_err("Failed to find symbol %s in %s\n", pp->function, 2898 pev->target ? : "kernel"); 2899 ret = -ENOENT; 2900 goto out; 2901 } else if (num_matched_functions > probe_conf.max_probes) { 2902 pr_err("Too many functions matched in %s\n", 2903 pev->target ? : "kernel"); 2904 ret = -E2BIG; 2905 goto out; 2906 } 2907 2908 /* Note that the symbols in the kmodule are not relocated */ 2909 if (!pev->uprobes && !pev->target && 2910 (!pp->retprobe || kretprobe_offset_is_supported())) { 2911 reloc_sym = kernel_get_ref_reloc_sym(); 2912 if (!reloc_sym) { 2913 pr_warning("Relocated base symbol is not found!\n"); 2914 ret = -EINVAL; 2915 goto out; 2916 } 2917 } 2918 2919 /* Setup result trace-probe-events */ 2920 *tevs = zalloc(sizeof(*tev) * num_matched_functions); 2921 if (!*tevs) { 2922 ret = -ENOMEM; 2923 goto out; 2924 } 2925 2926 ret = 0; 2927 2928 for (j = 0; j < num_matched_functions; j++) { 2929 sym = syms[j]; 2930 2931 tev = (*tevs) + ret; 2932 tp = &tev->point; 2933 if (ret == num_matched_functions) { 2934 pr_warning("Too many symbols are listed. Skip it.\n"); 2935 break; 2936 } 2937 ret++; 2938 2939 if (pp->offset > sym->end - sym->start) { 2940 pr_warning("Offset %ld is bigger than the size of %s\n", 2941 pp->offset, sym->name); 2942 ret = -ENOENT; 2943 goto err_out; 2944 } 2945 /* Add one probe point */ 2946 tp->address = map->unmap_ip(map, sym->start) + pp->offset; 2947 2948 /* Check the kprobe (not in module) is within .text */ 2949 if (!pev->uprobes && !pev->target && 2950 kprobe_warn_out_range(sym->name, tp->address)) { 2951 tp->symbol = NULL; /* Skip it */ 2952 skipped++; 2953 } else if (reloc_sym) { 2954 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out); 2955 tp->offset = tp->address - reloc_sym->addr; 2956 } else { 2957 tp->symbol = strdup_or_goto(sym->name, nomem_out); 2958 tp->offset = pp->offset; 2959 } 2960 tp->realname = strdup_or_goto(sym->name, nomem_out); 2961 2962 tp->retprobe = pp->retprobe; 2963 if (pev->target) { 2964 if (pev->uprobes) { 2965 tev->point.module = strdup_or_goto(pev->target, 2966 nomem_out); 2967 } else { 2968 mod_name = find_module_name(pev->target); 2969 tev->point.module = 2970 strdup(mod_name ? mod_name : pev->target); 2971 free(mod_name); 2972 if (!tev->point.module) 2973 goto nomem_out; 2974 } 2975 } 2976 tev->uprobes = pev->uprobes; 2977 tev->nargs = pev->nargs; 2978 if (tev->nargs) { 2979 tev->args = zalloc(sizeof(struct probe_trace_arg) * 2980 tev->nargs); 2981 if (tev->args == NULL) 2982 goto nomem_out; 2983 } 2984 for (i = 0; i < tev->nargs; i++) { 2985 if (pev->args[i].name) 2986 tev->args[i].name = 2987 strdup_or_goto(pev->args[i].name, 2988 nomem_out); 2989 2990 tev->args[i].value = strdup_or_goto(pev->args[i].var, 2991 nomem_out); 2992 if (pev->args[i].type) 2993 tev->args[i].type = 2994 strdup_or_goto(pev->args[i].type, 2995 nomem_out); 2996 } 2997 arch__fix_tev_from_maps(pev, tev, map, sym); 2998 } 2999 if (ret == skipped) { 3000 ret = -ENOENT; 3001 goto err_out; 3002 } 3003 3004 out: 3005 map__put(map); 3006 free(syms); 3007 return ret; 3008 3009 nomem_out: 3010 ret = -ENOMEM; 3011 err_out: 3012 clear_probe_trace_events(*tevs, num_matched_functions); 3013 zfree(tevs); 3014 goto out; 3015 } 3016 3017 static int try_to_find_absolute_address(struct perf_probe_event *pev, 3018 struct probe_trace_event **tevs) 3019 { 3020 struct perf_probe_point *pp = &pev->point; 3021 struct probe_trace_event *tev; 3022 struct probe_trace_point *tp; 3023 int i, err; 3024 3025 if (!(pev->point.function && !strncmp(pev->point.function, "0x", 2))) 3026 return -EINVAL; 3027 if (perf_probe_event_need_dwarf(pev)) 3028 return -EINVAL; 3029 3030 /* 3031 * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at 3032 * absolute address. 3033 * 3034 * Only one tev can be generated by this. 3035 */ 3036 *tevs = zalloc(sizeof(*tev)); 3037 if (!*tevs) 3038 return -ENOMEM; 3039 3040 tev = *tevs; 3041 tp = &tev->point; 3042 3043 /* 3044 * Don't use tp->offset, use address directly, because 3045 * in synthesize_probe_trace_command() address cannot be 3046 * zero. 3047 */ 3048 tp->address = pev->point.abs_address; 3049 tp->retprobe = pp->retprobe; 3050 tev->uprobes = pev->uprobes; 3051 3052 err = -ENOMEM; 3053 /* 3054 * Give it a '0x' leading symbol name. 3055 * In __add_probe_trace_events, a NULL symbol is interpreted as 3056 * invalid. 3057 */ 3058 if (asprintf(&tp->symbol, "0x%lx", tp->address) < 0) 3059 goto errout; 3060 3061 /* For kprobe, check range */ 3062 if ((!tev->uprobes) && 3063 (kprobe_warn_out_range(tev->point.symbol, 3064 tev->point.address))) { 3065 err = -EACCES; 3066 goto errout; 3067 } 3068 3069 if (asprintf(&tp->realname, "abs_%lx", tp->address) < 0) 3070 goto errout; 3071 3072 if (pev->target) { 3073 tp->module = strdup(pev->target); 3074 if (!tp->module) 3075 goto errout; 3076 } 3077 3078 if (tev->group) { 3079 tev->group = strdup(pev->group); 3080 if (!tev->group) 3081 goto errout; 3082 } 3083 3084 if (pev->event) { 3085 tev->event = strdup(pev->event); 3086 if (!tev->event) 3087 goto errout; 3088 } 3089 3090 tev->nargs = pev->nargs; 3091 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs); 3092 if (!tev->args) 3093 goto errout; 3094 3095 for (i = 0; i < tev->nargs; i++) 3096 copy_to_probe_trace_arg(&tev->args[i], &pev->args[i]); 3097 3098 return 1; 3099 3100 errout: 3101 clear_probe_trace_events(*tevs, 1); 3102 *tevs = NULL; 3103 return err; 3104 } 3105 3106 /* Concatinate two arrays */ 3107 static void *memcat(void *a, size_t sz_a, void *b, size_t sz_b) 3108 { 3109 void *ret; 3110 3111 ret = malloc(sz_a + sz_b); 3112 if (ret) { 3113 memcpy(ret, a, sz_a); 3114 memcpy(ret + sz_a, b, sz_b); 3115 } 3116 return ret; 3117 } 3118 3119 static int 3120 concat_probe_trace_events(struct probe_trace_event **tevs, int *ntevs, 3121 struct probe_trace_event **tevs2, int ntevs2) 3122 { 3123 struct probe_trace_event *new_tevs; 3124 int ret = 0; 3125 3126 if (*ntevs == 0) { 3127 *tevs = *tevs2; 3128 *ntevs = ntevs2; 3129 *tevs2 = NULL; 3130 return 0; 3131 } 3132 3133 if (*ntevs + ntevs2 > probe_conf.max_probes) 3134 ret = -E2BIG; 3135 else { 3136 /* Concatinate the array of probe_trace_event */ 3137 new_tevs = memcat(*tevs, (*ntevs) * sizeof(**tevs), 3138 *tevs2, ntevs2 * sizeof(**tevs2)); 3139 if (!new_tevs) 3140 ret = -ENOMEM; 3141 else { 3142 free(*tevs); 3143 *tevs = new_tevs; 3144 *ntevs += ntevs2; 3145 } 3146 } 3147 if (ret < 0) 3148 clear_probe_trace_events(*tevs2, ntevs2); 3149 zfree(tevs2); 3150 3151 return ret; 3152 } 3153 3154 /* 3155 * Try to find probe_trace_event from given probe caches. Return the number 3156 * of cached events found, if an error occurs return the error. 3157 */ 3158 static int find_cached_events(struct perf_probe_event *pev, 3159 struct probe_trace_event **tevs, 3160 const char *target) 3161 { 3162 struct probe_cache *cache; 3163 struct probe_cache_entry *entry; 3164 struct probe_trace_event *tmp_tevs = NULL; 3165 int ntevs = 0; 3166 int ret = 0; 3167 3168 cache = probe_cache__new(target, pev->nsi); 3169 /* Return 0 ("not found") if the target has no probe cache. */ 3170 if (!cache) 3171 return 0; 3172 3173 for_each_probe_cache_entry(entry, cache) { 3174 /* Skip the cache entry which has no name */ 3175 if (!entry->pev.event || !entry->pev.group) 3176 continue; 3177 if ((!pev->group || strglobmatch(entry->pev.group, pev->group)) && 3178 strglobmatch(entry->pev.event, pev->event)) { 3179 ret = probe_cache_entry__get_event(entry, &tmp_tevs); 3180 if (ret > 0) 3181 ret = concat_probe_trace_events(tevs, &ntevs, 3182 &tmp_tevs, ret); 3183 if (ret < 0) 3184 break; 3185 } 3186 } 3187 probe_cache__delete(cache); 3188 if (ret < 0) { 3189 clear_probe_trace_events(*tevs, ntevs); 3190 zfree(tevs); 3191 } else { 3192 ret = ntevs; 3193 if (ntevs > 0 && target && target[0] == '/') 3194 pev->uprobes = true; 3195 } 3196 3197 return ret; 3198 } 3199 3200 /* Try to find probe_trace_event from all probe caches */ 3201 static int find_cached_events_all(struct perf_probe_event *pev, 3202 struct probe_trace_event **tevs) 3203 { 3204 struct probe_trace_event *tmp_tevs = NULL; 3205 struct strlist *bidlist; 3206 struct str_node *nd; 3207 char *pathname; 3208 int ntevs = 0; 3209 int ret; 3210 3211 /* Get the buildid list of all valid caches */ 3212 bidlist = build_id_cache__list_all(true); 3213 if (!bidlist) { 3214 ret = -errno; 3215 pr_debug("Failed to get buildids: %d\n", ret); 3216 return ret; 3217 } 3218 3219 ret = 0; 3220 strlist__for_each_entry(nd, bidlist) { 3221 pathname = build_id_cache__origname(nd->s); 3222 ret = find_cached_events(pev, &tmp_tevs, pathname); 3223 /* In the case of cnt == 0, we just skip it */ 3224 if (ret > 0) 3225 ret = concat_probe_trace_events(tevs, &ntevs, 3226 &tmp_tevs, ret); 3227 free(pathname); 3228 if (ret < 0) 3229 break; 3230 } 3231 strlist__delete(bidlist); 3232 3233 if (ret < 0) { 3234 clear_probe_trace_events(*tevs, ntevs); 3235 zfree(tevs); 3236 } else 3237 ret = ntevs; 3238 3239 return ret; 3240 } 3241 3242 static int find_probe_trace_events_from_cache(struct perf_probe_event *pev, 3243 struct probe_trace_event **tevs) 3244 { 3245 struct probe_cache *cache; 3246 struct probe_cache_entry *entry; 3247 struct probe_trace_event *tev; 3248 struct str_node *node; 3249 int ret, i; 3250 3251 if (pev->sdt) { 3252 /* For SDT/cached events, we use special search functions */ 3253 if (!pev->target) 3254 return find_cached_events_all(pev, tevs); 3255 else 3256 return find_cached_events(pev, tevs, pev->target); 3257 } 3258 cache = probe_cache__new(pev->target, pev->nsi); 3259 if (!cache) 3260 return 0; 3261 3262 entry = probe_cache__find(cache, pev); 3263 if (!entry) { 3264 /* SDT must be in the cache */ 3265 ret = pev->sdt ? -ENOENT : 0; 3266 goto out; 3267 } 3268 3269 ret = strlist__nr_entries(entry->tevlist); 3270 if (ret > probe_conf.max_probes) { 3271 pr_debug("Too many entries matched in the cache of %s\n", 3272 pev->target ? : "kernel"); 3273 ret = -E2BIG; 3274 goto out; 3275 } 3276 3277 *tevs = zalloc(ret * sizeof(*tev)); 3278 if (!*tevs) { 3279 ret = -ENOMEM; 3280 goto out; 3281 } 3282 3283 i = 0; 3284 strlist__for_each_entry(node, entry->tevlist) { 3285 tev = &(*tevs)[i++]; 3286 ret = parse_probe_trace_command(node->s, tev); 3287 if (ret < 0) 3288 goto out; 3289 /* Set the uprobes attribute as same as original */ 3290 tev->uprobes = pev->uprobes; 3291 } 3292 ret = i; 3293 3294 out: 3295 probe_cache__delete(cache); 3296 return ret; 3297 } 3298 3299 static int convert_to_probe_trace_events(struct perf_probe_event *pev, 3300 struct probe_trace_event **tevs) 3301 { 3302 int ret; 3303 3304 if (!pev->group && !pev->sdt) { 3305 /* Set group name if not given */ 3306 if (!pev->uprobes) { 3307 pev->group = strdup(PERFPROBE_GROUP); 3308 ret = pev->group ? 0 : -ENOMEM; 3309 } else 3310 ret = convert_exec_to_group(pev->target, &pev->group); 3311 if (ret != 0) { 3312 pr_warning("Failed to make a group name.\n"); 3313 return ret; 3314 } 3315 } 3316 3317 ret = try_to_find_absolute_address(pev, tevs); 3318 if (ret > 0) 3319 return ret; 3320 3321 /* At first, we need to lookup cache entry */ 3322 ret = find_probe_trace_events_from_cache(pev, tevs); 3323 if (ret > 0 || pev->sdt) /* SDT can be found only in the cache */ 3324 return ret == 0 ? -ENOENT : ret; /* Found in probe cache */ 3325 3326 /* Convert perf_probe_event with debuginfo */ 3327 ret = try_to_find_probe_trace_events(pev, tevs); 3328 if (ret != 0) 3329 return ret; /* Found in debuginfo or got an error */ 3330 3331 return find_probe_trace_events_from_map(pev, tevs); 3332 } 3333 3334 int convert_perf_probe_events(struct perf_probe_event *pevs, int npevs) 3335 { 3336 int i, ret; 3337 3338 /* Loop 1: convert all events */ 3339 for (i = 0; i < npevs; i++) { 3340 /* Init kprobe blacklist if needed */ 3341 if (!pevs[i].uprobes) 3342 kprobe_blacklist__init(); 3343 /* Convert with or without debuginfo */ 3344 ret = convert_to_probe_trace_events(&pevs[i], &pevs[i].tevs); 3345 if (ret < 0) 3346 return ret; 3347 pevs[i].ntevs = ret; 3348 } 3349 /* This just release blacklist only if allocated */ 3350 kprobe_blacklist__release(); 3351 3352 return 0; 3353 } 3354 3355 static int show_probe_trace_event(struct probe_trace_event *tev) 3356 { 3357 char *buf = synthesize_probe_trace_command(tev); 3358 3359 if (!buf) { 3360 pr_debug("Failed to synthesize probe trace event.\n"); 3361 return -EINVAL; 3362 } 3363 3364 /* Showing definition always go stdout */ 3365 printf("%s\n", buf); 3366 free(buf); 3367 3368 return 0; 3369 } 3370 3371 int show_probe_trace_events(struct perf_probe_event *pevs, int npevs) 3372 { 3373 struct strlist *namelist = strlist__new(NULL, NULL); 3374 struct probe_trace_event *tev; 3375 struct perf_probe_event *pev; 3376 int i, j, ret = 0; 3377 3378 if (!namelist) 3379 return -ENOMEM; 3380 3381 for (j = 0; j < npevs && !ret; j++) { 3382 pev = &pevs[j]; 3383 for (i = 0; i < pev->ntevs && !ret; i++) { 3384 tev = &pev->tevs[i]; 3385 /* Skip if the symbol is out of .text or blacklisted */ 3386 if (!tev->point.symbol && !pev->uprobes) 3387 continue; 3388 3389 /* Set new name for tev (and update namelist) */ 3390 ret = probe_trace_event__set_name(tev, pev, 3391 namelist, true); 3392 if (!ret) 3393 ret = show_probe_trace_event(tev); 3394 } 3395 } 3396 strlist__delete(namelist); 3397 3398 return ret; 3399 } 3400 3401 int apply_perf_probe_events(struct perf_probe_event *pevs, int npevs) 3402 { 3403 int i, ret = 0; 3404 3405 /* Loop 2: add all events */ 3406 for (i = 0; i < npevs; i++) { 3407 ret = __add_probe_trace_events(&pevs[i], pevs[i].tevs, 3408 pevs[i].ntevs, 3409 probe_conf.force_add); 3410 if (ret < 0) 3411 break; 3412 } 3413 return ret; 3414 } 3415 3416 void cleanup_perf_probe_events(struct perf_probe_event *pevs, int npevs) 3417 { 3418 int i, j; 3419 struct perf_probe_event *pev; 3420 3421 /* Loop 3: cleanup and free trace events */ 3422 for (i = 0; i < npevs; i++) { 3423 pev = &pevs[i]; 3424 for (j = 0; j < pevs[i].ntevs; j++) 3425 clear_probe_trace_event(&pevs[i].tevs[j]); 3426 zfree(&pevs[i].tevs); 3427 pevs[i].ntevs = 0; 3428 nsinfo__zput(pev->nsi); 3429 clear_perf_probe_event(&pevs[i]); 3430 } 3431 } 3432 3433 int add_perf_probe_events(struct perf_probe_event *pevs, int npevs) 3434 { 3435 int ret; 3436 3437 ret = init_probe_symbol_maps(pevs->uprobes); 3438 if (ret < 0) 3439 return ret; 3440 3441 ret = convert_perf_probe_events(pevs, npevs); 3442 if (ret == 0) 3443 ret = apply_perf_probe_events(pevs, npevs); 3444 3445 cleanup_perf_probe_events(pevs, npevs); 3446 3447 exit_probe_symbol_maps(); 3448 return ret; 3449 } 3450 3451 int del_perf_probe_events(struct strfilter *filter) 3452 { 3453 int ret, ret2, ufd = -1, kfd = -1; 3454 char *str = strfilter__string(filter); 3455 3456 if (!str) 3457 return -EINVAL; 3458 3459 /* Get current event names */ 3460 ret = probe_file__open_both(&kfd, &ufd, PF_FL_RW); 3461 if (ret < 0) 3462 goto out; 3463 3464 ret = probe_file__del_events(kfd, filter); 3465 if (ret < 0 && ret != -ENOENT) 3466 goto error; 3467 3468 ret2 = probe_file__del_events(ufd, filter); 3469 if (ret2 < 0 && ret2 != -ENOENT) { 3470 ret = ret2; 3471 goto error; 3472 } 3473 ret = 0; 3474 3475 error: 3476 if (kfd >= 0) 3477 close(kfd); 3478 if (ufd >= 0) 3479 close(ufd); 3480 out: 3481 free(str); 3482 3483 return ret; 3484 } 3485 3486 int show_available_funcs(const char *target, struct nsinfo *nsi, 3487 struct strfilter *_filter, bool user) 3488 { 3489 struct rb_node *nd; 3490 struct map *map; 3491 int ret; 3492 3493 ret = init_probe_symbol_maps(user); 3494 if (ret < 0) 3495 return ret; 3496 3497 /* Get a symbol map */ 3498 map = get_target_map(target, nsi, user); 3499 if (!map) { 3500 pr_err("Failed to get a map for %s\n", (target) ? : "kernel"); 3501 return -EINVAL; 3502 } 3503 3504 ret = map__load(map); 3505 if (ret) { 3506 if (ret == -2) { 3507 char *str = strfilter__string(_filter); 3508 pr_err("Failed to find symbols matched to \"%s\"\n", 3509 str); 3510 free(str); 3511 } else 3512 pr_err("Failed to load symbols in %s\n", 3513 (target) ? : "kernel"); 3514 goto end; 3515 } 3516 if (!dso__sorted_by_name(map->dso)) 3517 dso__sort_by_name(map->dso); 3518 3519 /* Show all (filtered) symbols */ 3520 setup_pager(); 3521 3522 for (nd = rb_first_cached(&map->dso->symbol_names); nd; 3523 nd = rb_next(nd)) { 3524 struct symbol_name_rb_node *pos = rb_entry(nd, struct symbol_name_rb_node, rb_node); 3525 3526 if (strfilter__compare(_filter, pos->sym.name)) 3527 printf("%s\n", pos->sym.name); 3528 } 3529 end: 3530 map__put(map); 3531 exit_probe_symbol_maps(); 3532 3533 return ret; 3534 } 3535 3536 int copy_to_probe_trace_arg(struct probe_trace_arg *tvar, 3537 struct perf_probe_arg *pvar) 3538 { 3539 tvar->value = strdup(pvar->var); 3540 if (tvar->value == NULL) 3541 return -ENOMEM; 3542 if (pvar->type) { 3543 tvar->type = strdup(pvar->type); 3544 if (tvar->type == NULL) 3545 return -ENOMEM; 3546 } 3547 if (pvar->name) { 3548 tvar->name = strdup(pvar->name); 3549 if (tvar->name == NULL) 3550 return -ENOMEM; 3551 } else 3552 tvar->name = NULL; 3553 return 0; 3554 } 3555