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