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