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