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