Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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