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 "string.h" 38 #include "strlist.h" 39 #include "debug.h" 40 #include "cache.h" 41 #include "color.h" 42 #include "symbol.h" 43 #include "thread.h" 44 #include "debugfs.h" 45 #include "trace-event.h" /* For __unused */ 46 #include "probe-event.h" 47 #include "probe-finder.h" 48 49 #define MAX_CMDLEN 256 50 #define MAX_PROBE_ARGS 128 51 #define PERFPROBE_GROUP "probe" 52 53 bool probe_event_dry_run; /* Dry run flag */ 54 55 #define semantic_error(msg ...) pr_err("Semantic error :" msg) 56 57 /* If there is no space to write, returns -E2BIG. */ 58 static int e_snprintf(char *str, size_t size, const char *format, ...) 59 __attribute__((format(printf, 3, 4))); 60 61 static int e_snprintf(char *str, size_t size, const char *format, ...) 62 { 63 int ret; 64 va_list ap; 65 va_start(ap, format); 66 ret = vsnprintf(str, size, format, ap); 67 va_end(ap); 68 if (ret >= (int)size) 69 ret = -E2BIG; 70 return ret; 71 } 72 73 static char *synthesize_perf_probe_point(struct perf_probe_point *pp); 74 static struct machine machine; 75 76 /* Initialize symbol maps and path of vmlinux/modules */ 77 static int init_vmlinux(void) 78 { 79 int ret; 80 81 symbol_conf.sort_by_name = true; 82 if (symbol_conf.vmlinux_name == NULL) 83 symbol_conf.try_vmlinux_path = true; 84 else 85 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name); 86 ret = symbol__init(); 87 if (ret < 0) { 88 pr_debug("Failed to init symbol map.\n"); 89 goto out; 90 } 91 92 ret = machine__init(&machine, "", HOST_KERNEL_ID); 93 if (ret < 0) 94 goto out; 95 96 if (machine__create_kernel_maps(&machine) < 0) { 97 pr_debug("machine__create_kernel_maps() failed.\n"); 98 goto out; 99 } 100 out: 101 if (ret < 0) 102 pr_warning("Failed to init vmlinux path.\n"); 103 return ret; 104 } 105 106 static struct symbol *__find_kernel_function_by_name(const char *name, 107 struct map **mapp) 108 { 109 return machine__find_kernel_function_by_name(&machine, name, mapp, 110 NULL); 111 } 112 113 static struct map *kernel_get_module_map(const char *module) 114 { 115 struct rb_node *nd; 116 struct map_groups *grp = &machine.kmaps; 117 118 /* A file path -- this is an offline module */ 119 if (module && strchr(module, '/')) 120 return machine__new_module(&machine, 0, module); 121 122 if (!module) 123 module = "kernel"; 124 125 for (nd = rb_first(&grp->maps[MAP__FUNCTION]); nd; nd = rb_next(nd)) { 126 struct map *pos = rb_entry(nd, struct map, rb_node); 127 if (strncmp(pos->dso->short_name + 1, module, 128 pos->dso->short_name_len - 2) == 0) { 129 return pos; 130 } 131 } 132 return NULL; 133 } 134 135 static struct dso *kernel_get_module_dso(const char *module) 136 { 137 struct dso *dso; 138 struct map *map; 139 const char *vmlinux_name; 140 141 if (module) { 142 list_for_each_entry(dso, &machine.kernel_dsos, node) { 143 if (strncmp(dso->short_name + 1, module, 144 dso->short_name_len - 2) == 0) 145 goto found; 146 } 147 pr_debug("Failed to find module %s.\n", module); 148 return NULL; 149 } 150 151 map = machine.vmlinux_maps[MAP__FUNCTION]; 152 dso = map->dso; 153 154 vmlinux_name = symbol_conf.vmlinux_name; 155 if (vmlinux_name) { 156 if (dso__load_vmlinux(dso, map, vmlinux_name, NULL) <= 0) 157 return NULL; 158 } else { 159 if (dso__load_vmlinux_path(dso, map, NULL) <= 0) { 160 pr_debug("Failed to load kernel map.\n"); 161 return NULL; 162 } 163 } 164 found: 165 return dso; 166 } 167 168 const char *kernel_get_module_path(const char *module) 169 { 170 struct dso *dso = kernel_get_module_dso(module); 171 return (dso) ? dso->long_name : NULL; 172 } 173 174 #ifdef DWARF_SUPPORT 175 /* Open new debuginfo of given module */ 176 static struct debuginfo *open_debuginfo(const char *module) 177 { 178 const char *path; 179 180 /* A file path -- this is an offline module */ 181 if (module && strchr(module, '/')) 182 path = module; 183 else { 184 path = kernel_get_module_path(module); 185 186 if (!path) { 187 pr_err("Failed to find path of %s module.\n", 188 module ?: "kernel"); 189 return NULL; 190 } 191 } 192 return debuginfo__new(path); 193 } 194 195 /* 196 * Convert trace point to probe point with debuginfo 197 * Currently only handles kprobes. 198 */ 199 static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp, 200 struct perf_probe_point *pp) 201 { 202 struct symbol *sym; 203 struct map *map; 204 u64 addr; 205 int ret = -ENOENT; 206 struct debuginfo *dinfo; 207 208 sym = __find_kernel_function_by_name(tp->symbol, &map); 209 if (sym) { 210 addr = map->unmap_ip(map, sym->start + tp->offset); 211 pr_debug("try to find %s+%ld@%" PRIx64 "\n", tp->symbol, 212 tp->offset, addr); 213 214 dinfo = debuginfo__new_online_kernel(addr); 215 if (dinfo) { 216 ret = debuginfo__find_probe_point(dinfo, 217 (unsigned long)addr, pp); 218 debuginfo__delete(dinfo); 219 } else { 220 pr_debug("Failed to open debuginfo at 0x%" PRIx64 "\n", 221 addr); 222 ret = -ENOENT; 223 } 224 } 225 if (ret <= 0) { 226 pr_debug("Failed to find corresponding probes from " 227 "debuginfo. Use kprobe event information.\n"); 228 pp->function = strdup(tp->symbol); 229 if (pp->function == NULL) 230 return -ENOMEM; 231 pp->offset = tp->offset; 232 } 233 pp->retprobe = tp->retprobe; 234 235 return 0; 236 } 237 238 static int add_module_to_probe_trace_events(struct probe_trace_event *tevs, 239 int ntevs, const char *module) 240 { 241 int i, ret = 0; 242 char *tmp; 243 244 if (!module) 245 return 0; 246 247 tmp = strrchr(module, '/'); 248 if (tmp) { 249 /* This is a module path -- get the module name */ 250 module = strdup(tmp + 1); 251 if (!module) 252 return -ENOMEM; 253 tmp = strchr(module, '.'); 254 if (tmp) 255 *tmp = '\0'; 256 tmp = (char *)module; /* For free() */ 257 } 258 259 for (i = 0; i < ntevs; i++) { 260 tevs[i].point.module = strdup(module); 261 if (!tevs[i].point.module) { 262 ret = -ENOMEM; 263 break; 264 } 265 } 266 267 if (tmp) 268 free(tmp); 269 270 return ret; 271 } 272 273 /* Try to find perf_probe_event with debuginfo */ 274 static int try_to_find_probe_trace_events(struct perf_probe_event *pev, 275 struct probe_trace_event **tevs, 276 int max_tevs, const char *module) 277 { 278 bool need_dwarf = perf_probe_event_need_dwarf(pev); 279 struct debuginfo *dinfo = open_debuginfo(module); 280 int ntevs, ret = 0; 281 282 if (!dinfo) { 283 if (need_dwarf) { 284 pr_warning("Failed to open debuginfo file.\n"); 285 return -ENOENT; 286 } 287 pr_debug("Could not open debuginfo. Try to use symbols.\n"); 288 return 0; 289 } 290 291 /* Searching trace events corresponding to a probe event */ 292 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs, max_tevs); 293 294 debuginfo__delete(dinfo); 295 296 if (ntevs > 0) { /* Succeeded to find trace events */ 297 pr_debug("find %d probe_trace_events.\n", ntevs); 298 if (module) 299 ret = add_module_to_probe_trace_events(*tevs, ntevs, 300 module); 301 return ret < 0 ? ret : ntevs; 302 } 303 304 if (ntevs == 0) { /* No error but failed to find probe point. */ 305 pr_warning("Probe point '%s' not found.\n", 306 synthesize_perf_probe_point(&pev->point)); 307 return -ENOENT; 308 } 309 /* Error path : ntevs < 0 */ 310 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs); 311 if (ntevs == -EBADF) { 312 pr_warning("Warning: No dwarf info found in the vmlinux - " 313 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n"); 314 if (!need_dwarf) { 315 pr_debug("Trying to use symbols.\n"); 316 return 0; 317 } 318 } 319 return ntevs; 320 } 321 322 /* 323 * Find a src file from a DWARF tag path. Prepend optional source path prefix 324 * and chop off leading directories that do not exist. Result is passed back as 325 * a newly allocated path on success. 326 * Return 0 if file was found and readable, -errno otherwise. 327 */ 328 static int get_real_path(const char *raw_path, const char *comp_dir, 329 char **new_path) 330 { 331 const char *prefix = symbol_conf.source_prefix; 332 333 if (!prefix) { 334 if (raw_path[0] != '/' && comp_dir) 335 /* If not an absolute path, try to use comp_dir */ 336 prefix = comp_dir; 337 else { 338 if (access(raw_path, R_OK) == 0) { 339 *new_path = strdup(raw_path); 340 return 0; 341 } else 342 return -errno; 343 } 344 } 345 346 *new_path = malloc((strlen(prefix) + strlen(raw_path) + 2)); 347 if (!*new_path) 348 return -ENOMEM; 349 350 for (;;) { 351 sprintf(*new_path, "%s/%s", prefix, raw_path); 352 353 if (access(*new_path, R_OK) == 0) 354 return 0; 355 356 if (!symbol_conf.source_prefix) 357 /* In case of searching comp_dir, don't retry */ 358 return -errno; 359 360 switch (errno) { 361 case ENAMETOOLONG: 362 case ENOENT: 363 case EROFS: 364 case EFAULT: 365 raw_path = strchr(++raw_path, '/'); 366 if (!raw_path) { 367 free(*new_path); 368 *new_path = NULL; 369 return -ENOENT; 370 } 371 continue; 372 373 default: 374 free(*new_path); 375 *new_path = NULL; 376 return -errno; 377 } 378 } 379 } 380 381 #define LINEBUF_SIZE 256 382 #define NR_ADDITIONAL_LINES 2 383 384 static int __show_one_line(FILE *fp, int l, bool skip, bool show_num) 385 { 386 char buf[LINEBUF_SIZE]; 387 const char *color = show_num ? "" : PERF_COLOR_BLUE; 388 const char *prefix = NULL; 389 390 do { 391 if (fgets(buf, LINEBUF_SIZE, fp) == NULL) 392 goto error; 393 if (skip) 394 continue; 395 if (!prefix) { 396 prefix = show_num ? "%7d " : " "; 397 color_fprintf(stdout, color, prefix, l); 398 } 399 color_fprintf(stdout, color, "%s", buf); 400 401 } while (strchr(buf, '\n') == NULL); 402 403 return 1; 404 error: 405 if (ferror(fp)) { 406 pr_warning("File read error: %s\n", strerror(errno)); 407 return -1; 408 } 409 return 0; 410 } 411 412 static int _show_one_line(FILE *fp, int l, bool skip, bool show_num) 413 { 414 int rv = __show_one_line(fp, l, skip, show_num); 415 if (rv == 0) { 416 pr_warning("Source file is shorter than expected.\n"); 417 rv = -1; 418 } 419 return rv; 420 } 421 422 #define show_one_line_with_num(f,l) _show_one_line(f,l,false,true) 423 #define show_one_line(f,l) _show_one_line(f,l,false,false) 424 #define skip_one_line(f,l) _show_one_line(f,l,true,false) 425 #define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false) 426 427 /* 428 * Show line-range always requires debuginfo to find source file and 429 * line number. 430 */ 431 int show_line_range(struct line_range *lr, const char *module) 432 { 433 int l = 1; 434 struct line_node *ln; 435 struct debuginfo *dinfo; 436 FILE *fp; 437 int ret; 438 char *tmp; 439 440 /* Search a line range */ 441 ret = init_vmlinux(); 442 if (ret < 0) 443 return ret; 444 445 dinfo = open_debuginfo(module); 446 if (!dinfo) { 447 pr_warning("Failed to open debuginfo file.\n"); 448 return -ENOENT; 449 } 450 451 ret = debuginfo__find_line_range(dinfo, lr); 452 debuginfo__delete(dinfo); 453 if (ret == 0) { 454 pr_warning("Specified source line is not found.\n"); 455 return -ENOENT; 456 } else if (ret < 0) { 457 pr_warning("Debuginfo analysis failed. (%d)\n", ret); 458 return ret; 459 } 460 461 /* Convert source file path */ 462 tmp = lr->path; 463 ret = get_real_path(tmp, lr->comp_dir, &lr->path); 464 free(tmp); /* Free old path */ 465 if (ret < 0) { 466 pr_warning("Failed to find source file. (%d)\n", ret); 467 return ret; 468 } 469 470 setup_pager(); 471 472 if (lr->function) 473 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path, 474 lr->start - lr->offset); 475 else 476 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start); 477 478 fp = fopen(lr->path, "r"); 479 if (fp == NULL) { 480 pr_warning("Failed to open %s: %s\n", lr->path, 481 strerror(errno)); 482 return -errno; 483 } 484 /* Skip to starting line number */ 485 while (l < lr->start) { 486 ret = skip_one_line(fp, l++); 487 if (ret < 0) 488 goto end; 489 } 490 491 list_for_each_entry(ln, &lr->line_list, list) { 492 for (; ln->line > l; l++) { 493 ret = show_one_line(fp, l - lr->offset); 494 if (ret < 0) 495 goto end; 496 } 497 ret = show_one_line_with_num(fp, l++ - lr->offset); 498 if (ret < 0) 499 goto end; 500 } 501 502 if (lr->end == INT_MAX) 503 lr->end = l + NR_ADDITIONAL_LINES; 504 while (l <= lr->end) { 505 ret = show_one_line_or_eof(fp, l++ - lr->offset); 506 if (ret <= 0) 507 break; 508 } 509 end: 510 fclose(fp); 511 return ret; 512 } 513 514 static int show_available_vars_at(struct debuginfo *dinfo, 515 struct perf_probe_event *pev, 516 int max_vls, struct strfilter *_filter, 517 bool externs) 518 { 519 char *buf; 520 int ret, i, nvars; 521 struct str_node *node; 522 struct variable_list *vls = NULL, *vl; 523 const char *var; 524 525 buf = synthesize_perf_probe_point(&pev->point); 526 if (!buf) 527 return -EINVAL; 528 pr_debug("Searching variables at %s\n", buf); 529 530 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls, 531 max_vls, externs); 532 if (ret <= 0) { 533 pr_err("Failed to find variables at %s (%d)\n", buf, ret); 534 goto end; 535 } 536 /* Some variables are found */ 537 fprintf(stdout, "Available variables at %s\n", buf); 538 for (i = 0; i < ret; i++) { 539 vl = &vls[i]; 540 /* 541 * A probe point might be converted to 542 * several trace points. 543 */ 544 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol, 545 vl->point.offset); 546 free(vl->point.symbol); 547 nvars = 0; 548 if (vl->vars) { 549 strlist__for_each(node, vl->vars) { 550 var = strchr(node->s, '\t') + 1; 551 if (strfilter__compare(_filter, var)) { 552 fprintf(stdout, "\t\t%s\n", node->s); 553 nvars++; 554 } 555 } 556 strlist__delete(vl->vars); 557 } 558 if (nvars == 0) 559 fprintf(stdout, "\t\t(No matched variables)\n"); 560 } 561 free(vls); 562 end: 563 free(buf); 564 return ret; 565 } 566 567 /* Show available variables on given probe point */ 568 int show_available_vars(struct perf_probe_event *pevs, int npevs, 569 int max_vls, const char *module, 570 struct strfilter *_filter, bool externs) 571 { 572 int i, ret = 0; 573 struct debuginfo *dinfo; 574 575 ret = init_vmlinux(); 576 if (ret < 0) 577 return ret; 578 579 dinfo = open_debuginfo(module); 580 if (!dinfo) { 581 pr_warning("Failed to open debuginfo file.\n"); 582 return -ENOENT; 583 } 584 585 setup_pager(); 586 587 for (i = 0; i < npevs && ret >= 0; i++) 588 ret = show_available_vars_at(dinfo, &pevs[i], max_vls, _filter, 589 externs); 590 591 debuginfo__delete(dinfo); 592 return ret; 593 } 594 595 #else /* !DWARF_SUPPORT */ 596 597 static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp, 598 struct perf_probe_point *pp) 599 { 600 struct symbol *sym; 601 602 sym = __find_kernel_function_by_name(tp->symbol, NULL); 603 if (!sym) { 604 pr_err("Failed to find symbol %s in kernel.\n", tp->symbol); 605 return -ENOENT; 606 } 607 pp->function = strdup(tp->symbol); 608 if (pp->function == NULL) 609 return -ENOMEM; 610 pp->offset = tp->offset; 611 pp->retprobe = tp->retprobe; 612 613 return 0; 614 } 615 616 static int try_to_find_probe_trace_events(struct perf_probe_event *pev, 617 struct probe_trace_event **tevs __unused, 618 int max_tevs __unused, const char *mod __unused) 619 { 620 if (perf_probe_event_need_dwarf(pev)) { 621 pr_warning("Debuginfo-analysis is not supported.\n"); 622 return -ENOSYS; 623 } 624 return 0; 625 } 626 627 int show_line_range(struct line_range *lr __unused, const char *module __unused) 628 { 629 pr_warning("Debuginfo-analysis is not supported.\n"); 630 return -ENOSYS; 631 } 632 633 int show_available_vars(struct perf_probe_event *pevs __unused, 634 int npevs __unused, int max_vls __unused, 635 const char *module __unused, 636 struct strfilter *filter __unused, 637 bool externs __unused) 638 { 639 pr_warning("Debuginfo-analysis is not supported.\n"); 640 return -ENOSYS; 641 } 642 #endif 643 644 static int parse_line_num(char **ptr, int *val, const char *what) 645 { 646 const char *start = *ptr; 647 648 errno = 0; 649 *val = strtol(*ptr, ptr, 0); 650 if (errno || *ptr == start) { 651 semantic_error("'%s' is not a valid number.\n", what); 652 return -EINVAL; 653 } 654 return 0; 655 } 656 657 /* 658 * Stuff 'lr' according to the line range described by 'arg'. 659 * The line range syntax is described by: 660 * 661 * SRC[:SLN[+NUM|-ELN]] 662 * FNC[@SRC][:SLN[+NUM|-ELN]] 663 */ 664 int parse_line_range_desc(const char *arg, struct line_range *lr) 665 { 666 char *range, *file, *name = strdup(arg); 667 int err; 668 669 if (!name) 670 return -ENOMEM; 671 672 lr->start = 0; 673 lr->end = INT_MAX; 674 675 range = strchr(name, ':'); 676 if (range) { 677 *range++ = '\0'; 678 679 err = parse_line_num(&range, &lr->start, "start line"); 680 if (err) 681 goto err; 682 683 if (*range == '+' || *range == '-') { 684 const char c = *range++; 685 686 err = parse_line_num(&range, &lr->end, "end line"); 687 if (err) 688 goto err; 689 690 if (c == '+') { 691 lr->end += lr->start; 692 /* 693 * Adjust the number of lines here. 694 * If the number of lines == 1, the 695 * the end of line should be equal to 696 * the start of line. 697 */ 698 lr->end--; 699 } 700 } 701 702 pr_debug("Line range is %d to %d\n", lr->start, lr->end); 703 704 err = -EINVAL; 705 if (lr->start > lr->end) { 706 semantic_error("Start line must be smaller" 707 " than end line.\n"); 708 goto err; 709 } 710 if (*range != '\0') { 711 semantic_error("Tailing with invalid str '%s'.\n", range); 712 goto err; 713 } 714 } 715 716 file = strchr(name, '@'); 717 if (file) { 718 *file = '\0'; 719 lr->file = strdup(++file); 720 if (lr->file == NULL) { 721 err = -ENOMEM; 722 goto err; 723 } 724 lr->function = name; 725 } else if (strchr(name, '.')) 726 lr->file = name; 727 else 728 lr->function = name; 729 730 return 0; 731 err: 732 free(name); 733 return err; 734 } 735 736 /* Check the name is good for event/group */ 737 static bool check_event_name(const char *name) 738 { 739 if (!isalpha(*name) && *name != '_') 740 return false; 741 while (*++name != '\0') { 742 if (!isalpha(*name) && !isdigit(*name) && *name != '_') 743 return false; 744 } 745 return true; 746 } 747 748 /* Parse probepoint definition. */ 749 static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev) 750 { 751 struct perf_probe_point *pp = &pev->point; 752 char *ptr, *tmp; 753 char c, nc = 0; 754 /* 755 * <Syntax> 756 * perf probe [EVENT=]SRC[:LN|;PTN] 757 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT] 758 * 759 * TODO:Group name support 760 */ 761 762 ptr = strpbrk(arg, ";=@+%"); 763 if (ptr && *ptr == '=') { /* Event name */ 764 *ptr = '\0'; 765 tmp = ptr + 1; 766 if (strchr(arg, ':')) { 767 semantic_error("Group name is not supported yet.\n"); 768 return -ENOTSUP; 769 } 770 if (!check_event_name(arg)) { 771 semantic_error("%s is bad for event name -it must " 772 "follow C symbol-naming rule.\n", arg); 773 return -EINVAL; 774 } 775 pev->event = strdup(arg); 776 if (pev->event == NULL) 777 return -ENOMEM; 778 pev->group = NULL; 779 arg = tmp; 780 } 781 782 ptr = strpbrk(arg, ";:+@%"); 783 if (ptr) { 784 nc = *ptr; 785 *ptr++ = '\0'; 786 } 787 788 tmp = strdup(arg); 789 if (tmp == NULL) 790 return -ENOMEM; 791 792 /* Check arg is function or file and copy it */ 793 if (strchr(tmp, '.')) /* File */ 794 pp->file = tmp; 795 else /* Function */ 796 pp->function = tmp; 797 798 /* Parse other options */ 799 while (ptr) { 800 arg = ptr; 801 c = nc; 802 if (c == ';') { /* Lazy pattern must be the last part */ 803 pp->lazy_line = strdup(arg); 804 if (pp->lazy_line == NULL) 805 return -ENOMEM; 806 break; 807 } 808 ptr = strpbrk(arg, ";:+@%"); 809 if (ptr) { 810 nc = *ptr; 811 *ptr++ = '\0'; 812 } 813 switch (c) { 814 case ':': /* Line number */ 815 pp->line = strtoul(arg, &tmp, 0); 816 if (*tmp != '\0') { 817 semantic_error("There is non-digit char" 818 " in line number.\n"); 819 return -EINVAL; 820 } 821 break; 822 case '+': /* Byte offset from a symbol */ 823 pp->offset = strtoul(arg, &tmp, 0); 824 if (*tmp != '\0') { 825 semantic_error("There is non-digit character" 826 " in offset.\n"); 827 return -EINVAL; 828 } 829 break; 830 case '@': /* File name */ 831 if (pp->file) { 832 semantic_error("SRC@SRC is not allowed.\n"); 833 return -EINVAL; 834 } 835 pp->file = strdup(arg); 836 if (pp->file == NULL) 837 return -ENOMEM; 838 break; 839 case '%': /* Probe places */ 840 if (strcmp(arg, "return") == 0) { 841 pp->retprobe = 1; 842 } else { /* Others not supported yet */ 843 semantic_error("%%%s is not supported.\n", arg); 844 return -ENOTSUP; 845 } 846 break; 847 default: /* Buggy case */ 848 pr_err("This program has a bug at %s:%d.\n", 849 __FILE__, __LINE__); 850 return -ENOTSUP; 851 break; 852 } 853 } 854 855 /* Exclusion check */ 856 if (pp->lazy_line && pp->line) { 857 semantic_error("Lazy pattern can't be used with" 858 " line number.\n"); 859 return -EINVAL; 860 } 861 862 if (pp->lazy_line && pp->offset) { 863 semantic_error("Lazy pattern can't be used with offset.\n"); 864 return -EINVAL; 865 } 866 867 if (pp->line && pp->offset) { 868 semantic_error("Offset can't be used with line number.\n"); 869 return -EINVAL; 870 } 871 872 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) { 873 semantic_error("File always requires line number or " 874 "lazy pattern.\n"); 875 return -EINVAL; 876 } 877 878 if (pp->offset && !pp->function) { 879 semantic_error("Offset requires an entry function.\n"); 880 return -EINVAL; 881 } 882 883 if (pp->retprobe && !pp->function) { 884 semantic_error("Return probe requires an entry function.\n"); 885 return -EINVAL; 886 } 887 888 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) { 889 semantic_error("Offset/Line/Lazy pattern can't be used with " 890 "return probe.\n"); 891 return -EINVAL; 892 } 893 894 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n", 895 pp->function, pp->file, pp->line, pp->offset, pp->retprobe, 896 pp->lazy_line); 897 return 0; 898 } 899 900 /* Parse perf-probe event argument */ 901 static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg) 902 { 903 char *tmp, *goodname; 904 struct perf_probe_arg_field **fieldp; 905 906 pr_debug("parsing arg: %s into ", str); 907 908 tmp = strchr(str, '='); 909 if (tmp) { 910 arg->name = strndup(str, tmp - str); 911 if (arg->name == NULL) 912 return -ENOMEM; 913 pr_debug("name:%s ", arg->name); 914 str = tmp + 1; 915 } 916 917 tmp = strchr(str, ':'); 918 if (tmp) { /* Type setting */ 919 *tmp = '\0'; 920 arg->type = strdup(tmp + 1); 921 if (arg->type == NULL) 922 return -ENOMEM; 923 pr_debug("type:%s ", arg->type); 924 } 925 926 tmp = strpbrk(str, "-.["); 927 if (!is_c_varname(str) || !tmp) { 928 /* A variable, register, symbol or special value */ 929 arg->var = strdup(str); 930 if (arg->var == NULL) 931 return -ENOMEM; 932 pr_debug("%s\n", arg->var); 933 return 0; 934 } 935 936 /* Structure fields or array element */ 937 arg->var = strndup(str, tmp - str); 938 if (arg->var == NULL) 939 return -ENOMEM; 940 goodname = arg->var; 941 pr_debug("%s, ", arg->var); 942 fieldp = &arg->field; 943 944 do { 945 *fieldp = zalloc(sizeof(struct perf_probe_arg_field)); 946 if (*fieldp == NULL) 947 return -ENOMEM; 948 if (*tmp == '[') { /* Array */ 949 str = tmp; 950 (*fieldp)->index = strtol(str + 1, &tmp, 0); 951 (*fieldp)->ref = true; 952 if (*tmp != ']' || tmp == str + 1) { 953 semantic_error("Array index must be a" 954 " number.\n"); 955 return -EINVAL; 956 } 957 tmp++; 958 if (*tmp == '\0') 959 tmp = NULL; 960 } else { /* Structure */ 961 if (*tmp == '.') { 962 str = tmp + 1; 963 (*fieldp)->ref = false; 964 } else if (tmp[1] == '>') { 965 str = tmp + 2; 966 (*fieldp)->ref = true; 967 } else { 968 semantic_error("Argument parse error: %s\n", 969 str); 970 return -EINVAL; 971 } 972 tmp = strpbrk(str, "-.["); 973 } 974 if (tmp) { 975 (*fieldp)->name = strndup(str, tmp - str); 976 if ((*fieldp)->name == NULL) 977 return -ENOMEM; 978 if (*str != '[') 979 goodname = (*fieldp)->name; 980 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref); 981 fieldp = &(*fieldp)->next; 982 } 983 } while (tmp); 984 (*fieldp)->name = strdup(str); 985 if ((*fieldp)->name == NULL) 986 return -ENOMEM; 987 if (*str != '[') 988 goodname = (*fieldp)->name; 989 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref); 990 991 /* If no name is specified, set the last field name (not array index)*/ 992 if (!arg->name) { 993 arg->name = strdup(goodname); 994 if (arg->name == NULL) 995 return -ENOMEM; 996 } 997 return 0; 998 } 999 1000 /* Parse perf-probe event command */ 1001 int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev) 1002 { 1003 char **argv; 1004 int argc, i, ret = 0; 1005 1006 argv = argv_split(cmd, &argc); 1007 if (!argv) { 1008 pr_debug("Failed to split arguments.\n"); 1009 return -ENOMEM; 1010 } 1011 if (argc - 1 > MAX_PROBE_ARGS) { 1012 semantic_error("Too many probe arguments (%d).\n", argc - 1); 1013 ret = -ERANGE; 1014 goto out; 1015 } 1016 /* Parse probe point */ 1017 ret = parse_perf_probe_point(argv[0], pev); 1018 if (ret < 0) 1019 goto out; 1020 1021 /* Copy arguments and ensure return probe has no C argument */ 1022 pev->nargs = argc - 1; 1023 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs); 1024 if (pev->args == NULL) { 1025 ret = -ENOMEM; 1026 goto out; 1027 } 1028 for (i = 0; i < pev->nargs && ret >= 0; i++) { 1029 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]); 1030 if (ret >= 0 && 1031 is_c_varname(pev->args[i].var) && pev->point.retprobe) { 1032 semantic_error("You can't specify local variable for" 1033 " kretprobe.\n"); 1034 ret = -EINVAL; 1035 } 1036 } 1037 out: 1038 argv_free(argv); 1039 1040 return ret; 1041 } 1042 1043 /* Return true if this perf_probe_event requires debuginfo */ 1044 bool perf_probe_event_need_dwarf(struct perf_probe_event *pev) 1045 { 1046 int i; 1047 1048 if (pev->point.file || pev->point.line || pev->point.lazy_line) 1049 return true; 1050 1051 for (i = 0; i < pev->nargs; i++) 1052 if (is_c_varname(pev->args[i].var)) 1053 return true; 1054 1055 return false; 1056 } 1057 1058 /* Parse probe_events event into struct probe_point */ 1059 static int parse_probe_trace_command(const char *cmd, 1060 struct probe_trace_event *tev) 1061 { 1062 struct probe_trace_point *tp = &tev->point; 1063 char pr; 1064 char *p; 1065 int ret, i, argc; 1066 char **argv; 1067 1068 pr_debug("Parsing probe_events: %s\n", cmd); 1069 argv = argv_split(cmd, &argc); 1070 if (!argv) { 1071 pr_debug("Failed to split arguments.\n"); 1072 return -ENOMEM; 1073 } 1074 if (argc < 2) { 1075 semantic_error("Too few probe arguments.\n"); 1076 ret = -ERANGE; 1077 goto out; 1078 } 1079 1080 /* Scan event and group name. */ 1081 ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]", 1082 &pr, (float *)(void *)&tev->group, 1083 (float *)(void *)&tev->event); 1084 if (ret != 3) { 1085 semantic_error("Failed to parse event name: %s\n", argv[0]); 1086 ret = -EINVAL; 1087 goto out; 1088 } 1089 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr); 1090 1091 tp->retprobe = (pr == 'r'); 1092 1093 /* Scan module name(if there), function name and offset */ 1094 p = strchr(argv[1], ':'); 1095 if (p) { 1096 tp->module = strndup(argv[1], p - argv[1]); 1097 p++; 1098 } else 1099 p = argv[1]; 1100 ret = sscanf(p, "%a[^+]+%lu", (float *)(void *)&tp->symbol, 1101 &tp->offset); 1102 if (ret == 1) 1103 tp->offset = 0; 1104 1105 tev->nargs = argc - 2; 1106 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs); 1107 if (tev->args == NULL) { 1108 ret = -ENOMEM; 1109 goto out; 1110 } 1111 for (i = 0; i < tev->nargs; i++) { 1112 p = strchr(argv[i + 2], '='); 1113 if (p) /* We don't need which register is assigned. */ 1114 *p++ = '\0'; 1115 else 1116 p = argv[i + 2]; 1117 tev->args[i].name = strdup(argv[i + 2]); 1118 /* TODO: parse regs and offset */ 1119 tev->args[i].value = strdup(p); 1120 if (tev->args[i].name == NULL || tev->args[i].value == NULL) { 1121 ret = -ENOMEM; 1122 goto out; 1123 } 1124 } 1125 ret = 0; 1126 out: 1127 argv_free(argv); 1128 return ret; 1129 } 1130 1131 /* Compose only probe arg */ 1132 int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len) 1133 { 1134 struct perf_probe_arg_field *field = pa->field; 1135 int ret; 1136 char *tmp = buf; 1137 1138 if (pa->name && pa->var) 1139 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var); 1140 else 1141 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var); 1142 if (ret <= 0) 1143 goto error; 1144 tmp += ret; 1145 len -= ret; 1146 1147 while (field) { 1148 if (field->name[0] == '[') 1149 ret = e_snprintf(tmp, len, "%s", field->name); 1150 else 1151 ret = e_snprintf(tmp, len, "%s%s", 1152 field->ref ? "->" : ".", field->name); 1153 if (ret <= 0) 1154 goto error; 1155 tmp += ret; 1156 len -= ret; 1157 field = field->next; 1158 } 1159 1160 if (pa->type) { 1161 ret = e_snprintf(tmp, len, ":%s", pa->type); 1162 if (ret <= 0) 1163 goto error; 1164 tmp += ret; 1165 len -= ret; 1166 } 1167 1168 return tmp - buf; 1169 error: 1170 pr_debug("Failed to synthesize perf probe argument: %s\n", 1171 strerror(-ret)); 1172 return ret; 1173 } 1174 1175 /* Compose only probe point (not argument) */ 1176 static char *synthesize_perf_probe_point(struct perf_probe_point *pp) 1177 { 1178 char *buf, *tmp; 1179 char offs[32] = "", line[32] = "", file[32] = ""; 1180 int ret, len; 1181 1182 buf = zalloc(MAX_CMDLEN); 1183 if (buf == NULL) { 1184 ret = -ENOMEM; 1185 goto error; 1186 } 1187 if (pp->offset) { 1188 ret = e_snprintf(offs, 32, "+%lu", pp->offset); 1189 if (ret <= 0) 1190 goto error; 1191 } 1192 if (pp->line) { 1193 ret = e_snprintf(line, 32, ":%d", pp->line); 1194 if (ret <= 0) 1195 goto error; 1196 } 1197 if (pp->file) { 1198 tmp = pp->file; 1199 len = strlen(tmp); 1200 if (len > 30) { 1201 tmp = strchr(pp->file + len - 30, '/'); 1202 tmp = tmp ? tmp + 1 : pp->file + len - 30; 1203 } 1204 ret = e_snprintf(file, 32, "@%s", tmp); 1205 if (ret <= 0) 1206 goto error; 1207 } 1208 1209 if (pp->function) 1210 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function, 1211 offs, pp->retprobe ? "%return" : "", line, 1212 file); 1213 else 1214 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line); 1215 if (ret <= 0) 1216 goto error; 1217 1218 return buf; 1219 error: 1220 pr_debug("Failed to synthesize perf probe point: %s\n", 1221 strerror(-ret)); 1222 if (buf) 1223 free(buf); 1224 return NULL; 1225 } 1226 1227 #if 0 1228 char *synthesize_perf_probe_command(struct perf_probe_event *pev) 1229 { 1230 char *buf; 1231 int i, len, ret; 1232 1233 buf = synthesize_perf_probe_point(&pev->point); 1234 if (!buf) 1235 return NULL; 1236 1237 len = strlen(buf); 1238 for (i = 0; i < pev->nargs; i++) { 1239 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s", 1240 pev->args[i].name); 1241 if (ret <= 0) { 1242 free(buf); 1243 return NULL; 1244 } 1245 len += ret; 1246 } 1247 1248 return buf; 1249 } 1250 #endif 1251 1252 static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref, 1253 char **buf, size_t *buflen, 1254 int depth) 1255 { 1256 int ret; 1257 if (ref->next) { 1258 depth = __synthesize_probe_trace_arg_ref(ref->next, buf, 1259 buflen, depth + 1); 1260 if (depth < 0) 1261 goto out; 1262 } 1263 1264 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset); 1265 if (ret < 0) 1266 depth = ret; 1267 else { 1268 *buf += ret; 1269 *buflen -= ret; 1270 } 1271 out: 1272 return depth; 1273 1274 } 1275 1276 static int synthesize_probe_trace_arg(struct probe_trace_arg *arg, 1277 char *buf, size_t buflen) 1278 { 1279 struct probe_trace_arg_ref *ref = arg->ref; 1280 int ret, depth = 0; 1281 char *tmp = buf; 1282 1283 /* Argument name or separator */ 1284 if (arg->name) 1285 ret = e_snprintf(buf, buflen, " %s=", arg->name); 1286 else 1287 ret = e_snprintf(buf, buflen, " "); 1288 if (ret < 0) 1289 return ret; 1290 buf += ret; 1291 buflen -= ret; 1292 1293 /* Special case: @XXX */ 1294 if (arg->value[0] == '@' && arg->ref) 1295 ref = ref->next; 1296 1297 /* Dereferencing arguments */ 1298 if (ref) { 1299 depth = __synthesize_probe_trace_arg_ref(ref, &buf, 1300 &buflen, 1); 1301 if (depth < 0) 1302 return depth; 1303 } 1304 1305 /* Print argument value */ 1306 if (arg->value[0] == '@' && arg->ref) 1307 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value, 1308 arg->ref->offset); 1309 else 1310 ret = e_snprintf(buf, buflen, "%s", arg->value); 1311 if (ret < 0) 1312 return ret; 1313 buf += ret; 1314 buflen -= ret; 1315 1316 /* Closing */ 1317 while (depth--) { 1318 ret = e_snprintf(buf, buflen, ")"); 1319 if (ret < 0) 1320 return ret; 1321 buf += ret; 1322 buflen -= ret; 1323 } 1324 /* Print argument type */ 1325 if (arg->type) { 1326 ret = e_snprintf(buf, buflen, ":%s", arg->type); 1327 if (ret <= 0) 1328 return ret; 1329 buf += ret; 1330 } 1331 1332 return buf - tmp; 1333 } 1334 1335 char *synthesize_probe_trace_command(struct probe_trace_event *tev) 1336 { 1337 struct probe_trace_point *tp = &tev->point; 1338 char *buf; 1339 int i, len, ret; 1340 1341 buf = zalloc(MAX_CMDLEN); 1342 if (buf == NULL) 1343 return NULL; 1344 1345 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s%s%s+%lu", 1346 tp->retprobe ? 'r' : 'p', 1347 tev->group, tev->event, 1348 tp->module ?: "", tp->module ? ":" : "", 1349 tp->symbol, tp->offset); 1350 if (len <= 0) 1351 goto error; 1352 1353 for (i = 0; i < tev->nargs; i++) { 1354 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len, 1355 MAX_CMDLEN - len); 1356 if (ret <= 0) 1357 goto error; 1358 len += ret; 1359 } 1360 1361 return buf; 1362 error: 1363 free(buf); 1364 return NULL; 1365 } 1366 1367 static int convert_to_perf_probe_event(struct probe_trace_event *tev, 1368 struct perf_probe_event *pev) 1369 { 1370 char buf[64] = ""; 1371 int i, ret; 1372 1373 /* Convert event/group name */ 1374 pev->event = strdup(tev->event); 1375 pev->group = strdup(tev->group); 1376 if (pev->event == NULL || pev->group == NULL) 1377 return -ENOMEM; 1378 1379 /* Convert trace_point to probe_point */ 1380 ret = kprobe_convert_to_perf_probe(&tev->point, &pev->point); 1381 if (ret < 0) 1382 return ret; 1383 1384 /* Convert trace_arg to probe_arg */ 1385 pev->nargs = tev->nargs; 1386 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs); 1387 if (pev->args == NULL) 1388 return -ENOMEM; 1389 for (i = 0; i < tev->nargs && ret >= 0; i++) { 1390 if (tev->args[i].name) 1391 pev->args[i].name = strdup(tev->args[i].name); 1392 else { 1393 ret = synthesize_probe_trace_arg(&tev->args[i], 1394 buf, 64); 1395 pev->args[i].name = strdup(buf); 1396 } 1397 if (pev->args[i].name == NULL && ret >= 0) 1398 ret = -ENOMEM; 1399 } 1400 1401 if (ret < 0) 1402 clear_perf_probe_event(pev); 1403 1404 return ret; 1405 } 1406 1407 void clear_perf_probe_event(struct perf_probe_event *pev) 1408 { 1409 struct perf_probe_point *pp = &pev->point; 1410 struct perf_probe_arg_field *field, *next; 1411 int i; 1412 1413 if (pev->event) 1414 free(pev->event); 1415 if (pev->group) 1416 free(pev->group); 1417 if (pp->file) 1418 free(pp->file); 1419 if (pp->function) 1420 free(pp->function); 1421 if (pp->lazy_line) 1422 free(pp->lazy_line); 1423 for (i = 0; i < pev->nargs; i++) { 1424 if (pev->args[i].name) 1425 free(pev->args[i].name); 1426 if (pev->args[i].var) 1427 free(pev->args[i].var); 1428 if (pev->args[i].type) 1429 free(pev->args[i].type); 1430 field = pev->args[i].field; 1431 while (field) { 1432 next = field->next; 1433 if (field->name) 1434 free(field->name); 1435 free(field); 1436 field = next; 1437 } 1438 } 1439 if (pev->args) 1440 free(pev->args); 1441 memset(pev, 0, sizeof(*pev)); 1442 } 1443 1444 static void clear_probe_trace_event(struct probe_trace_event *tev) 1445 { 1446 struct probe_trace_arg_ref *ref, *next; 1447 int i; 1448 1449 if (tev->event) 1450 free(tev->event); 1451 if (tev->group) 1452 free(tev->group); 1453 if (tev->point.symbol) 1454 free(tev->point.symbol); 1455 if (tev->point.module) 1456 free(tev->point.module); 1457 for (i = 0; i < tev->nargs; i++) { 1458 if (tev->args[i].name) 1459 free(tev->args[i].name); 1460 if (tev->args[i].value) 1461 free(tev->args[i].value); 1462 if (tev->args[i].type) 1463 free(tev->args[i].type); 1464 ref = tev->args[i].ref; 1465 while (ref) { 1466 next = ref->next; 1467 free(ref); 1468 ref = next; 1469 } 1470 } 1471 if (tev->args) 1472 free(tev->args); 1473 memset(tev, 0, sizeof(*tev)); 1474 } 1475 1476 static int open_kprobe_events(bool readwrite) 1477 { 1478 char buf[PATH_MAX]; 1479 const char *__debugfs; 1480 int ret; 1481 1482 __debugfs = debugfs_find_mountpoint(); 1483 if (__debugfs == NULL) { 1484 pr_warning("Debugfs is not mounted.\n"); 1485 return -ENOENT; 1486 } 1487 1488 ret = e_snprintf(buf, PATH_MAX, "%stracing/kprobe_events", __debugfs); 1489 if (ret >= 0) { 1490 pr_debug("Opening %s write=%d\n", buf, readwrite); 1491 if (readwrite && !probe_event_dry_run) 1492 ret = open(buf, O_RDWR, O_APPEND); 1493 else 1494 ret = open(buf, O_RDONLY, 0); 1495 } 1496 1497 if (ret < 0) { 1498 if (errno == ENOENT) 1499 pr_warning("kprobe_events file does not exist - please" 1500 " rebuild kernel with CONFIG_KPROBE_EVENT.\n"); 1501 else 1502 pr_warning("Failed to open kprobe_events file: %s\n", 1503 strerror(errno)); 1504 } 1505 return ret; 1506 } 1507 1508 /* Get raw string list of current kprobe_events */ 1509 static struct strlist *get_probe_trace_command_rawlist(int fd) 1510 { 1511 int ret, idx; 1512 FILE *fp; 1513 char buf[MAX_CMDLEN]; 1514 char *p; 1515 struct strlist *sl; 1516 1517 sl = strlist__new(true, NULL); 1518 1519 fp = fdopen(dup(fd), "r"); 1520 while (!feof(fp)) { 1521 p = fgets(buf, MAX_CMDLEN, fp); 1522 if (!p) 1523 break; 1524 1525 idx = strlen(p) - 1; 1526 if (p[idx] == '\n') 1527 p[idx] = '\0'; 1528 ret = strlist__add(sl, buf); 1529 if (ret < 0) { 1530 pr_debug("strlist__add failed: %s\n", strerror(-ret)); 1531 strlist__delete(sl); 1532 return NULL; 1533 } 1534 } 1535 fclose(fp); 1536 1537 return sl; 1538 } 1539 1540 /* Show an event */ 1541 static int show_perf_probe_event(struct perf_probe_event *pev) 1542 { 1543 int i, ret; 1544 char buf[128]; 1545 char *place; 1546 1547 /* Synthesize only event probe point */ 1548 place = synthesize_perf_probe_point(&pev->point); 1549 if (!place) 1550 return -EINVAL; 1551 1552 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event); 1553 if (ret < 0) 1554 return ret; 1555 1556 printf(" %-20s (on %s", buf, place); 1557 1558 if (pev->nargs > 0) { 1559 printf(" with"); 1560 for (i = 0; i < pev->nargs; i++) { 1561 ret = synthesize_perf_probe_arg(&pev->args[i], 1562 buf, 128); 1563 if (ret < 0) 1564 break; 1565 printf(" %s", buf); 1566 } 1567 } 1568 printf(")\n"); 1569 free(place); 1570 return ret; 1571 } 1572 1573 /* List up current perf-probe events */ 1574 int show_perf_probe_events(void) 1575 { 1576 int fd, ret; 1577 struct probe_trace_event tev; 1578 struct perf_probe_event pev; 1579 struct strlist *rawlist; 1580 struct str_node *ent; 1581 1582 setup_pager(); 1583 ret = init_vmlinux(); 1584 if (ret < 0) 1585 return ret; 1586 1587 memset(&tev, 0, sizeof(tev)); 1588 memset(&pev, 0, sizeof(pev)); 1589 1590 fd = open_kprobe_events(false); 1591 if (fd < 0) 1592 return fd; 1593 1594 rawlist = get_probe_trace_command_rawlist(fd); 1595 close(fd); 1596 if (!rawlist) 1597 return -ENOENT; 1598 1599 strlist__for_each(ent, rawlist) { 1600 ret = parse_probe_trace_command(ent->s, &tev); 1601 if (ret >= 0) { 1602 ret = convert_to_perf_probe_event(&tev, &pev); 1603 if (ret >= 0) 1604 ret = show_perf_probe_event(&pev); 1605 } 1606 clear_perf_probe_event(&pev); 1607 clear_probe_trace_event(&tev); 1608 if (ret < 0) 1609 break; 1610 } 1611 strlist__delete(rawlist); 1612 1613 return ret; 1614 } 1615 1616 /* Get current perf-probe event names */ 1617 static struct strlist *get_probe_trace_event_names(int fd, bool include_group) 1618 { 1619 char buf[128]; 1620 struct strlist *sl, *rawlist; 1621 struct str_node *ent; 1622 struct probe_trace_event tev; 1623 int ret = 0; 1624 1625 memset(&tev, 0, sizeof(tev)); 1626 rawlist = get_probe_trace_command_rawlist(fd); 1627 sl = strlist__new(true, NULL); 1628 strlist__for_each(ent, rawlist) { 1629 ret = parse_probe_trace_command(ent->s, &tev); 1630 if (ret < 0) 1631 break; 1632 if (include_group) { 1633 ret = e_snprintf(buf, 128, "%s:%s", tev.group, 1634 tev.event); 1635 if (ret >= 0) 1636 ret = strlist__add(sl, buf); 1637 } else 1638 ret = strlist__add(sl, tev.event); 1639 clear_probe_trace_event(&tev); 1640 if (ret < 0) 1641 break; 1642 } 1643 strlist__delete(rawlist); 1644 1645 if (ret < 0) { 1646 strlist__delete(sl); 1647 return NULL; 1648 } 1649 return sl; 1650 } 1651 1652 static int write_probe_trace_event(int fd, struct probe_trace_event *tev) 1653 { 1654 int ret = 0; 1655 char *buf = synthesize_probe_trace_command(tev); 1656 1657 if (!buf) { 1658 pr_debug("Failed to synthesize probe trace event.\n"); 1659 return -EINVAL; 1660 } 1661 1662 pr_debug("Writing event: %s\n", buf); 1663 if (!probe_event_dry_run) { 1664 ret = write(fd, buf, strlen(buf)); 1665 if (ret <= 0) 1666 pr_warning("Failed to write event: %s\n", 1667 strerror(errno)); 1668 } 1669 free(buf); 1670 return ret; 1671 } 1672 1673 static int get_new_event_name(char *buf, size_t len, const char *base, 1674 struct strlist *namelist, bool allow_suffix) 1675 { 1676 int i, ret; 1677 1678 /* Try no suffix */ 1679 ret = e_snprintf(buf, len, "%s", base); 1680 if (ret < 0) { 1681 pr_debug("snprintf() failed: %s\n", strerror(-ret)); 1682 return ret; 1683 } 1684 if (!strlist__has_entry(namelist, buf)) 1685 return 0; 1686 1687 if (!allow_suffix) { 1688 pr_warning("Error: event \"%s\" already exists. " 1689 "(Use -f to force duplicates.)\n", base); 1690 return -EEXIST; 1691 } 1692 1693 /* Try to add suffix */ 1694 for (i = 1; i < MAX_EVENT_INDEX; i++) { 1695 ret = e_snprintf(buf, len, "%s_%d", base, i); 1696 if (ret < 0) { 1697 pr_debug("snprintf() failed: %s\n", strerror(-ret)); 1698 return ret; 1699 } 1700 if (!strlist__has_entry(namelist, buf)) 1701 break; 1702 } 1703 if (i == MAX_EVENT_INDEX) { 1704 pr_warning("Too many events are on the same function.\n"); 1705 ret = -ERANGE; 1706 } 1707 1708 return ret; 1709 } 1710 1711 static int __add_probe_trace_events(struct perf_probe_event *pev, 1712 struct probe_trace_event *tevs, 1713 int ntevs, bool allow_suffix) 1714 { 1715 int i, fd, ret; 1716 struct probe_trace_event *tev = NULL; 1717 char buf[64]; 1718 const char *event, *group; 1719 struct strlist *namelist; 1720 1721 fd = open_kprobe_events(true); 1722 if (fd < 0) 1723 return fd; 1724 /* Get current event names */ 1725 namelist = get_probe_trace_event_names(fd, false); 1726 if (!namelist) { 1727 pr_debug("Failed to get current event list.\n"); 1728 return -EIO; 1729 } 1730 1731 ret = 0; 1732 printf("Add new event%s\n", (ntevs > 1) ? "s:" : ":"); 1733 for (i = 0; i < ntevs; i++) { 1734 tev = &tevs[i]; 1735 if (pev->event) 1736 event = pev->event; 1737 else 1738 if (pev->point.function) 1739 event = pev->point.function; 1740 else 1741 event = tev->point.symbol; 1742 if (pev->group) 1743 group = pev->group; 1744 else 1745 group = PERFPROBE_GROUP; 1746 1747 /* Get an unused new event name */ 1748 ret = get_new_event_name(buf, 64, event, 1749 namelist, allow_suffix); 1750 if (ret < 0) 1751 break; 1752 event = buf; 1753 1754 tev->event = strdup(event); 1755 tev->group = strdup(group); 1756 if (tev->event == NULL || tev->group == NULL) { 1757 ret = -ENOMEM; 1758 break; 1759 } 1760 ret = write_probe_trace_event(fd, tev); 1761 if (ret < 0) 1762 break; 1763 /* Add added event name to namelist */ 1764 strlist__add(namelist, event); 1765 1766 /* Trick here - save current event/group */ 1767 event = pev->event; 1768 group = pev->group; 1769 pev->event = tev->event; 1770 pev->group = tev->group; 1771 show_perf_probe_event(pev); 1772 /* Trick here - restore current event/group */ 1773 pev->event = (char *)event; 1774 pev->group = (char *)group; 1775 1776 /* 1777 * Probes after the first probe which comes from same 1778 * user input are always allowed to add suffix, because 1779 * there might be several addresses corresponding to 1780 * one code line. 1781 */ 1782 allow_suffix = true; 1783 } 1784 1785 if (ret >= 0) { 1786 /* Show how to use the event. */ 1787 printf("\nYou can now use it on all perf tools, such as:\n\n"); 1788 printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group, 1789 tev->event); 1790 } 1791 1792 strlist__delete(namelist); 1793 close(fd); 1794 return ret; 1795 } 1796 1797 static int convert_to_probe_trace_events(struct perf_probe_event *pev, 1798 struct probe_trace_event **tevs, 1799 int max_tevs, const char *module) 1800 { 1801 struct symbol *sym; 1802 int ret = 0, i; 1803 struct probe_trace_event *tev; 1804 1805 /* Convert perf_probe_event with debuginfo */ 1806 ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, module); 1807 if (ret != 0) 1808 return ret; /* Found in debuginfo or got an error */ 1809 1810 /* Allocate trace event buffer */ 1811 tev = *tevs = zalloc(sizeof(struct probe_trace_event)); 1812 if (tev == NULL) 1813 return -ENOMEM; 1814 1815 /* Copy parameters */ 1816 tev->point.symbol = strdup(pev->point.function); 1817 if (tev->point.symbol == NULL) { 1818 ret = -ENOMEM; 1819 goto error; 1820 } 1821 1822 if (module) { 1823 tev->point.module = strdup(module); 1824 if (tev->point.module == NULL) { 1825 ret = -ENOMEM; 1826 goto error; 1827 } 1828 } 1829 1830 tev->point.offset = pev->point.offset; 1831 tev->point.retprobe = pev->point.retprobe; 1832 tev->nargs = pev->nargs; 1833 if (tev->nargs) { 1834 tev->args = zalloc(sizeof(struct probe_trace_arg) 1835 * tev->nargs); 1836 if (tev->args == NULL) { 1837 ret = -ENOMEM; 1838 goto error; 1839 } 1840 for (i = 0; i < tev->nargs; i++) { 1841 if (pev->args[i].name) { 1842 tev->args[i].name = strdup(pev->args[i].name); 1843 if (tev->args[i].name == NULL) { 1844 ret = -ENOMEM; 1845 goto error; 1846 } 1847 } 1848 tev->args[i].value = strdup(pev->args[i].var); 1849 if (tev->args[i].value == NULL) { 1850 ret = -ENOMEM; 1851 goto error; 1852 } 1853 if (pev->args[i].type) { 1854 tev->args[i].type = strdup(pev->args[i].type); 1855 if (tev->args[i].type == NULL) { 1856 ret = -ENOMEM; 1857 goto error; 1858 } 1859 } 1860 } 1861 } 1862 1863 /* Currently just checking function name from symbol map */ 1864 sym = __find_kernel_function_by_name(tev->point.symbol, NULL); 1865 if (!sym) { 1866 pr_warning("Kernel symbol \'%s\' not found.\n", 1867 tev->point.symbol); 1868 ret = -ENOENT; 1869 goto error; 1870 } 1871 1872 return 1; 1873 error: 1874 clear_probe_trace_event(tev); 1875 free(tev); 1876 *tevs = NULL; 1877 return ret; 1878 } 1879 1880 struct __event_package { 1881 struct perf_probe_event *pev; 1882 struct probe_trace_event *tevs; 1883 int ntevs; 1884 }; 1885 1886 int add_perf_probe_events(struct perf_probe_event *pevs, int npevs, 1887 int max_tevs, const char *module, bool force_add) 1888 { 1889 int i, j, ret; 1890 struct __event_package *pkgs; 1891 1892 pkgs = zalloc(sizeof(struct __event_package) * npevs); 1893 if (pkgs == NULL) 1894 return -ENOMEM; 1895 1896 /* Init vmlinux path */ 1897 ret = init_vmlinux(); 1898 if (ret < 0) { 1899 free(pkgs); 1900 return ret; 1901 } 1902 1903 /* Loop 1: convert all events */ 1904 for (i = 0; i < npevs; i++) { 1905 pkgs[i].pev = &pevs[i]; 1906 /* Convert with or without debuginfo */ 1907 ret = convert_to_probe_trace_events(pkgs[i].pev, 1908 &pkgs[i].tevs, 1909 max_tevs, 1910 module); 1911 if (ret < 0) 1912 goto end; 1913 pkgs[i].ntevs = ret; 1914 } 1915 1916 /* Loop 2: add all events */ 1917 for (i = 0; i < npevs; i++) { 1918 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs, 1919 pkgs[i].ntevs, force_add); 1920 if (ret < 0) 1921 break; 1922 } 1923 end: 1924 /* Loop 3: cleanup and free trace events */ 1925 for (i = 0; i < npevs; i++) { 1926 for (j = 0; j < pkgs[i].ntevs; j++) 1927 clear_probe_trace_event(&pkgs[i].tevs[j]); 1928 free(pkgs[i].tevs); 1929 } 1930 free(pkgs); 1931 1932 return ret; 1933 } 1934 1935 static int __del_trace_probe_event(int fd, struct str_node *ent) 1936 { 1937 char *p; 1938 char buf[128]; 1939 int ret; 1940 1941 /* Convert from perf-probe event to trace-probe event */ 1942 ret = e_snprintf(buf, 128, "-:%s", ent->s); 1943 if (ret < 0) 1944 goto error; 1945 1946 p = strchr(buf + 2, ':'); 1947 if (!p) { 1948 pr_debug("Internal error: %s should have ':' but not.\n", 1949 ent->s); 1950 ret = -ENOTSUP; 1951 goto error; 1952 } 1953 *p = '/'; 1954 1955 pr_debug("Writing event: %s\n", buf); 1956 ret = write(fd, buf, strlen(buf)); 1957 if (ret < 0) { 1958 ret = -errno; 1959 goto error; 1960 } 1961 1962 printf("Remove event: %s\n", ent->s); 1963 return 0; 1964 error: 1965 pr_warning("Failed to delete event: %s\n", strerror(-ret)); 1966 return ret; 1967 } 1968 1969 static int del_trace_probe_event(int fd, const char *group, 1970 const char *event, struct strlist *namelist) 1971 { 1972 char buf[128]; 1973 struct str_node *ent, *n; 1974 int found = 0, ret = 0; 1975 1976 ret = e_snprintf(buf, 128, "%s:%s", group, event); 1977 if (ret < 0) { 1978 pr_err("Failed to copy event.\n"); 1979 return ret; 1980 } 1981 1982 if (strpbrk(buf, "*?")) { /* Glob-exp */ 1983 strlist__for_each_safe(ent, n, namelist) 1984 if (strglobmatch(ent->s, buf)) { 1985 found++; 1986 ret = __del_trace_probe_event(fd, ent); 1987 if (ret < 0) 1988 break; 1989 strlist__remove(namelist, ent); 1990 } 1991 } else { 1992 ent = strlist__find(namelist, buf); 1993 if (ent) { 1994 found++; 1995 ret = __del_trace_probe_event(fd, ent); 1996 if (ret >= 0) 1997 strlist__remove(namelist, ent); 1998 } 1999 } 2000 if (found == 0 && ret >= 0) 2001 pr_info("Info: Event \"%s\" does not exist.\n", buf); 2002 2003 return ret; 2004 } 2005 2006 int del_perf_probe_events(struct strlist *dellist) 2007 { 2008 int fd, ret = 0; 2009 const char *group, *event; 2010 char *p, *str; 2011 struct str_node *ent; 2012 struct strlist *namelist; 2013 2014 fd = open_kprobe_events(true); 2015 if (fd < 0) 2016 return fd; 2017 2018 /* Get current event names */ 2019 namelist = get_probe_trace_event_names(fd, true); 2020 if (namelist == NULL) 2021 return -EINVAL; 2022 2023 strlist__for_each(ent, dellist) { 2024 str = strdup(ent->s); 2025 if (str == NULL) { 2026 ret = -ENOMEM; 2027 break; 2028 } 2029 pr_debug("Parsing: %s\n", str); 2030 p = strchr(str, ':'); 2031 if (p) { 2032 group = str; 2033 *p = '\0'; 2034 event = p + 1; 2035 } else { 2036 group = "*"; 2037 event = str; 2038 } 2039 pr_debug("Group: %s, Event: %s\n", group, event); 2040 ret = del_trace_probe_event(fd, group, event, namelist); 2041 free(str); 2042 if (ret < 0) 2043 break; 2044 } 2045 strlist__delete(namelist); 2046 close(fd); 2047 2048 return ret; 2049 } 2050 /* TODO: don't use a global variable for filter ... */ 2051 static struct strfilter *available_func_filter; 2052 2053 /* 2054 * If a symbol corresponds to a function with global binding and 2055 * matches filter return 0. For all others return 1. 2056 */ 2057 static int filter_available_functions(struct map *map __unused, 2058 struct symbol *sym) 2059 { 2060 if (sym->binding == STB_GLOBAL && 2061 strfilter__compare(available_func_filter, sym->name)) 2062 return 0; 2063 return 1; 2064 } 2065 2066 int show_available_funcs(const char *module, struct strfilter *_filter) 2067 { 2068 struct map *map; 2069 int ret; 2070 2071 setup_pager(); 2072 2073 ret = init_vmlinux(); 2074 if (ret < 0) 2075 return ret; 2076 2077 map = kernel_get_module_map(module); 2078 if (!map) { 2079 pr_err("Failed to find %s map.\n", (module) ? : "kernel"); 2080 return -EINVAL; 2081 } 2082 available_func_filter = _filter; 2083 if (map__load(map, filter_available_functions)) { 2084 pr_err("Failed to load map.\n"); 2085 return -EINVAL; 2086 } 2087 if (!dso__sorted_by_name(map->dso, map->type)) 2088 dso__sort_by_name(map->dso, map->type); 2089 2090 dso__fprintf_symbols_by_name(map->dso, map->type, stdout); 2091 return 0; 2092 } 2093