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