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