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