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 <inttypes.h> 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 #include <elf.h> 35 36 #include "util.h" 37 #include "event.h" 38 #include "strlist.h" 39 #include "strfilter.h" 40 #include "debug.h" 41 #include "cache.h" 42 #include "color.h" 43 #include "symbol.h" 44 #include "thread.h" 45 #include <api/fs/fs.h> 46 #include "trace-event.h" /* For __maybe_unused */ 47 #include "probe-event.h" 48 #include "probe-finder.h" 49 #include "probe-file.h" 50 #include "session.h" 51 #include "string2.h" 52 53 #include "sane_ctype.h" 54 55 #define PERFPROBE_GROUP "probe" 56 57 bool probe_event_dry_run; /* Dry run flag */ 58 struct probe_conf probe_conf; 59 60 #define semantic_error(msg ...) pr_err("Semantic error :" msg) 61 62 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 struct machine *host_machine; 75 76 /* Initialize symbol maps and path of vmlinux/modules */ 77 int init_probe_symbol_maps(bool user_only) 78 { 79 int ret; 80 81 symbol_conf.sort_by_name = true; 82 symbol_conf.allow_aliases = true; 83 ret = symbol__init(NULL); 84 if (ret < 0) { 85 pr_debug("Failed to init symbol map.\n"); 86 goto out; 87 } 88 89 if (host_machine || user_only) /* already initialized */ 90 return 0; 91 92 if (symbol_conf.vmlinux_name) 93 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name); 94 95 host_machine = machine__new_host(); 96 if (!host_machine) { 97 pr_debug("machine__new_host() failed.\n"); 98 symbol__exit(); 99 ret = -1; 100 } 101 out: 102 if (ret < 0) 103 pr_warning("Failed to init vmlinux path.\n"); 104 return ret; 105 } 106 107 void exit_probe_symbol_maps(void) 108 { 109 machine__delete(host_machine); 110 host_machine = NULL; 111 symbol__exit(); 112 } 113 114 static struct ref_reloc_sym *kernel_get_ref_reloc_sym(void) 115 { 116 /* kmap->ref_reloc_sym should be set if host_machine is initialized */ 117 struct kmap *kmap; 118 struct map *map = machine__kernel_map(host_machine); 119 120 if (map__load(map) < 0) 121 return NULL; 122 123 kmap = map__kmap(map); 124 if (!kmap) 125 return NULL; 126 return kmap->ref_reloc_sym; 127 } 128 129 static int kernel_get_symbol_address_by_name(const char *name, u64 *addr, 130 bool reloc, bool reladdr) 131 { 132 struct ref_reloc_sym *reloc_sym; 133 struct symbol *sym; 134 struct map *map; 135 136 /* ref_reloc_sym is just a label. Need a special fix*/ 137 reloc_sym = kernel_get_ref_reloc_sym(); 138 if (reloc_sym && strcmp(name, reloc_sym->name) == 0) 139 *addr = (reloc) ? reloc_sym->addr : reloc_sym->unrelocated_addr; 140 else { 141 sym = machine__find_kernel_symbol_by_name(host_machine, name, &map); 142 if (!sym) 143 return -ENOENT; 144 *addr = map->unmap_ip(map, sym->start) - 145 ((reloc) ? 0 : map->reloc) - 146 ((reladdr) ? map->start : 0); 147 } 148 return 0; 149 } 150 151 static struct map *kernel_get_module_map(const char *module) 152 { 153 struct maps *maps = machine__kernel_maps(host_machine); 154 struct map *pos; 155 156 /* A file path -- this is an offline module */ 157 if (module && strchr(module, '/')) 158 return dso__new_map(module); 159 160 if (!module) 161 module = "kernel"; 162 163 for (pos = maps__first(maps); pos; pos = map__next(pos)) { 164 /* short_name is "[module]" */ 165 if (strncmp(pos->dso->short_name + 1, module, 166 pos->dso->short_name_len - 2) == 0 && 167 module[pos->dso->short_name_len - 2] == '\0') { 168 map__get(pos); 169 return pos; 170 } 171 } 172 return NULL; 173 } 174 175 struct map *get_target_map(const char *target, struct nsinfo *nsi, bool user) 176 { 177 /* Init maps of given executable or kernel */ 178 if (user) { 179 struct map *map; 180 181 map = dso__new_map(target); 182 if (map && map->dso) 183 map->dso->nsinfo = nsinfo__get(nsi); 184 return map; 185 } else { 186 return kernel_get_module_map(target); 187 } 188 } 189 190 static int convert_exec_to_group(const char *exec, char **result) 191 { 192 char *ptr1, *ptr2, *exec_copy; 193 char buf[64]; 194 int ret; 195 196 exec_copy = strdup(exec); 197 if (!exec_copy) 198 return -ENOMEM; 199 200 ptr1 = basename(exec_copy); 201 if (!ptr1) { 202 ret = -EINVAL; 203 goto out; 204 } 205 206 for (ptr2 = ptr1; *ptr2 != '\0'; ptr2++) { 207 if (!isalnum(*ptr2) && *ptr2 != '_') { 208 *ptr2 = '\0'; 209 break; 210 } 211 } 212 213 ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1); 214 if (ret < 0) 215 goto out; 216 217 *result = strdup(buf); 218 ret = *result ? 0 : -ENOMEM; 219 220 out: 221 free(exec_copy); 222 return ret; 223 } 224 225 static void clear_perf_probe_point(struct perf_probe_point *pp) 226 { 227 free(pp->file); 228 free(pp->function); 229 free(pp->lazy_line); 230 } 231 232 static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs) 233 { 234 int i; 235 236 for (i = 0; i < ntevs; i++) 237 clear_probe_trace_event(tevs + i); 238 } 239 240 static bool kprobe_blacklist__listed(unsigned long address); 241 static bool kprobe_warn_out_range(const char *symbol, unsigned long address) 242 { 243 u64 etext_addr = 0; 244 int ret; 245 246 /* Get the address of _etext for checking non-probable text symbol */ 247 ret = kernel_get_symbol_address_by_name("_etext", &etext_addr, 248 false, false); 249 250 if (ret == 0 && etext_addr < address) 251 pr_warning("%s is out of .text, skip it.\n", symbol); 252 else if (kprobe_blacklist__listed(address)) 253 pr_warning("%s is blacklisted function, skip it.\n", symbol); 254 else 255 return false; 256 257 return true; 258 } 259 260 /* 261 * @module can be module name of module file path. In case of path, 262 * inspect elf and find out what is actual module name. 263 * Caller has to free mod_name after using it. 264 */ 265 static char *find_module_name(const char *module) 266 { 267 int fd; 268 Elf *elf; 269 GElf_Ehdr ehdr; 270 GElf_Shdr shdr; 271 Elf_Data *data; 272 Elf_Scn *sec; 273 char *mod_name = NULL; 274 int name_offset; 275 276 fd = open(module, O_RDONLY); 277 if (fd < 0) 278 return NULL; 279 280 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); 281 if (elf == NULL) 282 goto elf_err; 283 284 if (gelf_getehdr(elf, &ehdr) == NULL) 285 goto ret_err; 286 287 sec = elf_section_by_name(elf, &ehdr, &shdr, 288 ".gnu.linkonce.this_module", NULL); 289 if (!sec) 290 goto ret_err; 291 292 data = elf_getdata(sec, NULL); 293 if (!data || !data->d_buf) 294 goto ret_err; 295 296 /* 297 * NOTE: 298 * '.gnu.linkonce.this_module' section of kernel module elf directly 299 * maps to 'struct module' from linux/module.h. This section contains 300 * actual module name which will be used by kernel after loading it. 301 * But, we cannot use 'struct module' here since linux/module.h is not 302 * exposed to user-space. Offset of 'name' has remained same from long 303 * time, so hardcoding it here. 304 */ 305 if (ehdr.e_ident[EI_CLASS] == ELFCLASS32) 306 name_offset = 12; 307 else /* expect ELFCLASS64 by default */ 308 name_offset = 24; 309 310 mod_name = strdup((char *)data->d_buf + name_offset); 311 312 ret_err: 313 elf_end(elf); 314 elf_err: 315 close(fd); 316 return mod_name; 317 } 318 319 #ifdef HAVE_DWARF_SUPPORT 320 321 static int kernel_get_module_dso(const char *module, struct dso **pdso) 322 { 323 struct dso *dso; 324 struct map *map; 325 const char *vmlinux_name; 326 int ret = 0; 327 328 if (module) { 329 char module_name[128]; 330 331 snprintf(module_name, sizeof(module_name), "[%s]", module); 332 map = map_groups__find_by_name(&host_machine->kmaps, module_name); 333 if (map) { 334 dso = map->dso; 335 goto found; 336 } 337 pr_debug("Failed to find module %s.\n", module); 338 return -ENOENT; 339 } 340 341 map = machine__kernel_map(host_machine); 342 dso = map->dso; 343 344 vmlinux_name = symbol_conf.vmlinux_name; 345 dso->load_errno = 0; 346 if (vmlinux_name) 347 ret = dso__load_vmlinux(dso, map, vmlinux_name, false); 348 else 349 ret = dso__load_vmlinux_path(dso, map); 350 found: 351 *pdso = dso; 352 return ret; 353 } 354 355 /* 356 * Some binaries like glibc have special symbols which are on the symbol 357 * table, but not in the debuginfo. If we can find the address of the 358 * symbol from map, we can translate the address back to the probe point. 359 */ 360 static int find_alternative_probe_point(struct debuginfo *dinfo, 361 struct perf_probe_point *pp, 362 struct perf_probe_point *result, 363 const char *target, struct nsinfo *nsi, 364 bool uprobes) 365 { 366 struct map *map = NULL; 367 struct symbol *sym; 368 u64 address = 0; 369 int ret = -ENOENT; 370 371 /* This can work only for function-name based one */ 372 if (!pp->function || pp->file) 373 return -ENOTSUP; 374 375 map = get_target_map(target, nsi, uprobes); 376 if (!map) 377 return -EINVAL; 378 379 /* Find the address of given function */ 380 map__for_each_symbol_by_name(map, pp->function, sym) { 381 if (uprobes) 382 address = sym->start; 383 else 384 address = map->unmap_ip(map, sym->start) - map->reloc; 385 break; 386 } 387 if (!address) { 388 ret = -ENOENT; 389 goto out; 390 } 391 pr_debug("Symbol %s address found : %" PRIx64 "\n", 392 pp->function, address); 393 394 ret = debuginfo__find_probe_point(dinfo, (unsigned long)address, 395 result); 396 if (ret <= 0) 397 ret = (!ret) ? -ENOENT : ret; 398 else { 399 result->offset += pp->offset; 400 result->line += pp->line; 401 result->retprobe = pp->retprobe; 402 ret = 0; 403 } 404 405 out: 406 map__put(map); 407 return ret; 408 409 } 410 411 static int get_alternative_probe_event(struct debuginfo *dinfo, 412 struct perf_probe_event *pev, 413 struct perf_probe_point *tmp) 414 { 415 int ret; 416 417 memcpy(tmp, &pev->point, sizeof(*tmp)); 418 memset(&pev->point, 0, sizeof(pev->point)); 419 ret = find_alternative_probe_point(dinfo, tmp, &pev->point, pev->target, 420 pev->nsi, pev->uprobes); 421 if (ret < 0) 422 memcpy(&pev->point, tmp, sizeof(*tmp)); 423 424 return ret; 425 } 426 427 static int get_alternative_line_range(struct debuginfo *dinfo, 428 struct line_range *lr, 429 const char *target, bool user) 430 { 431 struct perf_probe_point pp = { .function = lr->function, 432 .file = lr->file, 433 .line = lr->start }; 434 struct perf_probe_point result; 435 int ret, len = 0; 436 437 memset(&result, 0, sizeof(result)); 438 439 if (lr->end != INT_MAX) 440 len = lr->end - lr->start; 441 ret = find_alternative_probe_point(dinfo, &pp, &result, 442 target, NULL, user); 443 if (!ret) { 444 lr->function = result.function; 445 lr->file = result.file; 446 lr->start = result.line; 447 if (lr->end != INT_MAX) 448 lr->end = lr->start + len; 449 clear_perf_probe_point(&pp); 450 } 451 return ret; 452 } 453 454 /* Open new debuginfo of given module */ 455 static struct debuginfo *open_debuginfo(const char *module, struct nsinfo *nsi, 456 bool silent) 457 { 458 const char *path = module; 459 char reason[STRERR_BUFSIZE]; 460 struct debuginfo *ret = NULL; 461 struct dso *dso = NULL; 462 struct nscookie nsc; 463 int err; 464 465 if (!module || !strchr(module, '/')) { 466 err = kernel_get_module_dso(module, &dso); 467 if (err < 0) { 468 if (!dso || dso->load_errno == 0) { 469 if (!str_error_r(-err, reason, STRERR_BUFSIZE)) 470 strcpy(reason, "(unknown)"); 471 } else 472 dso__strerror_load(dso, reason, STRERR_BUFSIZE); 473 if (!silent) 474 pr_err("Failed to find the path for %s: %s\n", 475 module ?: "kernel", reason); 476 return NULL; 477 } 478 path = dso->long_name; 479 } 480 nsinfo__mountns_enter(nsi, &nsc); 481 ret = debuginfo__new(path); 482 if (!ret && !silent) { 483 pr_warning("The %s file has no debug information.\n", path); 484 if (!module || !strtailcmp(path, ".ko")) 485 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, "); 486 else 487 pr_warning("Rebuild with -g, "); 488 pr_warning("or install an appropriate debuginfo package.\n"); 489 } 490 nsinfo__mountns_exit(&nsc); 491 return ret; 492 } 493 494 /* For caching the last debuginfo */ 495 static struct debuginfo *debuginfo_cache; 496 static char *debuginfo_cache_path; 497 498 static struct debuginfo *debuginfo_cache__open(const char *module, bool silent) 499 { 500 const char *path = module; 501 502 /* If the module is NULL, it should be the kernel. */ 503 if (!module) 504 path = "kernel"; 505 506 if (debuginfo_cache_path && !strcmp(debuginfo_cache_path, path)) 507 goto out; 508 509 /* Copy module path */ 510 free(debuginfo_cache_path); 511 debuginfo_cache_path = strdup(path); 512 if (!debuginfo_cache_path) { 513 debuginfo__delete(debuginfo_cache); 514 debuginfo_cache = NULL; 515 goto out; 516 } 517 518 debuginfo_cache = open_debuginfo(module, NULL, silent); 519 if (!debuginfo_cache) 520 zfree(&debuginfo_cache_path); 521 out: 522 return debuginfo_cache; 523 } 524 525 static void debuginfo_cache__exit(void) 526 { 527 debuginfo__delete(debuginfo_cache); 528 debuginfo_cache = NULL; 529 zfree(&debuginfo_cache_path); 530 } 531 532 533 static int get_text_start_address(const char *exec, unsigned long *address, 534 struct nsinfo *nsi) 535 { 536 Elf *elf; 537 GElf_Ehdr ehdr; 538 GElf_Shdr shdr; 539 int fd, ret = -ENOENT; 540 struct nscookie nsc; 541 542 nsinfo__mountns_enter(nsi, &nsc); 543 fd = open(exec, O_RDONLY); 544 nsinfo__mountns_exit(&nsc); 545 if (fd < 0) 546 return -errno; 547 548 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); 549 if (elf == NULL) { 550 ret = -EINVAL; 551 goto out_close; 552 } 553 554 if (gelf_getehdr(elf, &ehdr) == NULL) 555 goto out; 556 557 if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL)) 558 goto out; 559 560 *address = shdr.sh_addr - shdr.sh_offset; 561 ret = 0; 562 out: 563 elf_end(elf); 564 out_close: 565 close(fd); 566 567 return ret; 568 } 569 570 /* 571 * Convert trace point to probe point with debuginfo 572 */ 573 static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp, 574 struct perf_probe_point *pp, 575 bool is_kprobe) 576 { 577 struct debuginfo *dinfo = NULL; 578 unsigned long stext = 0; 579 u64 addr = tp->address; 580 int ret = -ENOENT; 581 582 /* convert the address to dwarf address */ 583 if (!is_kprobe) { 584 if (!addr) { 585 ret = -EINVAL; 586 goto error; 587 } 588 ret = get_text_start_address(tp->module, &stext, NULL); 589 if (ret < 0) 590 goto error; 591 addr += stext; 592 } else if (tp->symbol) { 593 /* If the module is given, this returns relative address */ 594 ret = kernel_get_symbol_address_by_name(tp->symbol, &addr, 595 false, !!tp->module); 596 if (ret != 0) 597 goto error; 598 addr += tp->offset; 599 } 600 601 pr_debug("try to find information at %" PRIx64 " in %s\n", addr, 602 tp->module ? : "kernel"); 603 604 dinfo = debuginfo_cache__open(tp->module, verbose <= 0); 605 if (dinfo) 606 ret = debuginfo__find_probe_point(dinfo, 607 (unsigned long)addr, pp); 608 else 609 ret = -ENOENT; 610 611 if (ret > 0) { 612 pp->retprobe = tp->retprobe; 613 return 0; 614 } 615 error: 616 pr_debug("Failed to find corresponding probes from debuginfo.\n"); 617 return ret ? : -ENOENT; 618 } 619 620 /* Adjust symbol name and address */ 621 static int post_process_probe_trace_point(struct probe_trace_point *tp, 622 struct map *map, unsigned long offs) 623 { 624 struct symbol *sym; 625 u64 addr = tp->address - offs; 626 627 sym = map__find_symbol(map, addr); 628 if (!sym) 629 return -ENOENT; 630 631 if (strcmp(sym->name, tp->symbol)) { 632 /* If we have no realname, use symbol for it */ 633 if (!tp->realname) 634 tp->realname = tp->symbol; 635 else 636 free(tp->symbol); 637 tp->symbol = strdup(sym->name); 638 if (!tp->symbol) 639 return -ENOMEM; 640 } 641 tp->offset = addr - sym->start; 642 tp->address -= offs; 643 644 return 0; 645 } 646 647 /* 648 * Rename DWARF symbols to ELF symbols -- gcc sometimes optimizes functions 649 * and generate new symbols with suffixes such as .constprop.N or .isra.N 650 * etc. Since those symbols are not recorded in DWARF, we have to find 651 * correct generated symbols from offline ELF binary. 652 * For online kernel or uprobes we don't need this because those are 653 * rebased on _text, or already a section relative address. 654 */ 655 static int 656 post_process_offline_probe_trace_events(struct probe_trace_event *tevs, 657 int ntevs, const char *pathname) 658 { 659 struct map *map; 660 unsigned long stext = 0; 661 int i, ret = 0; 662 663 /* Prepare a map for offline binary */ 664 map = dso__new_map(pathname); 665 if (!map || get_text_start_address(pathname, &stext, NULL) < 0) { 666 pr_warning("Failed to get ELF symbols for %s\n", pathname); 667 return -EINVAL; 668 } 669 670 for (i = 0; i < ntevs; i++) { 671 ret = post_process_probe_trace_point(&tevs[i].point, 672 map, stext); 673 if (ret < 0) 674 break; 675 } 676 map__put(map); 677 678 return ret; 679 } 680 681 static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs, 682 int ntevs, const char *exec, 683 struct nsinfo *nsi) 684 { 685 int i, ret = 0; 686 unsigned long stext = 0; 687 688 if (!exec) 689 return 0; 690 691 ret = get_text_start_address(exec, &stext, nsi); 692 if (ret < 0) 693 return ret; 694 695 for (i = 0; i < ntevs && ret >= 0; i++) { 696 /* point.address is the addres of point.symbol + point.offset */ 697 tevs[i].point.address -= stext; 698 tevs[i].point.module = strdup(exec); 699 if (!tevs[i].point.module) { 700 ret = -ENOMEM; 701 break; 702 } 703 tevs[i].uprobes = true; 704 } 705 706 return ret; 707 } 708 709 static int 710 post_process_module_probe_trace_events(struct probe_trace_event *tevs, 711 int ntevs, const char *module, 712 struct debuginfo *dinfo) 713 { 714 Dwarf_Addr text_offs = 0; 715 int i, ret = 0; 716 char *mod_name = NULL; 717 struct map *map; 718 719 if (!module) 720 return 0; 721 722 map = get_target_map(module, NULL, false); 723 if (!map || debuginfo__get_text_offset(dinfo, &text_offs, true) < 0) { 724 pr_warning("Failed to get ELF symbols for %s\n", module); 725 return -EINVAL; 726 } 727 728 mod_name = find_module_name(module); 729 for (i = 0; i < ntevs; i++) { 730 ret = post_process_probe_trace_point(&tevs[i].point, 731 map, (unsigned long)text_offs); 732 if (ret < 0) 733 break; 734 tevs[i].point.module = 735 strdup(mod_name ? mod_name : module); 736 if (!tevs[i].point.module) { 737 ret = -ENOMEM; 738 break; 739 } 740 } 741 742 free(mod_name); 743 map__put(map); 744 745 return ret; 746 } 747 748 static int 749 post_process_kernel_probe_trace_events(struct probe_trace_event *tevs, 750 int ntevs) 751 { 752 struct ref_reloc_sym *reloc_sym; 753 char *tmp; 754 int i, skipped = 0; 755 756 /* Skip post process if the target is an offline kernel */ 757 if (symbol_conf.ignore_vmlinux_buildid) 758 return post_process_offline_probe_trace_events(tevs, ntevs, 759 symbol_conf.vmlinux_name); 760 761 reloc_sym = kernel_get_ref_reloc_sym(); 762 if (!reloc_sym) { 763 pr_warning("Relocated base symbol is not found!\n"); 764 return -EINVAL; 765 } 766 767 for (i = 0; i < ntevs; i++) { 768 if (!tevs[i].point.address) 769 continue; 770 if (tevs[i].point.retprobe && !kretprobe_offset_is_supported()) 771 continue; 772 /* If we found a wrong one, mark it by NULL symbol */ 773 if (kprobe_warn_out_range(tevs[i].point.symbol, 774 tevs[i].point.address)) { 775 tmp = NULL; 776 skipped++; 777 } else { 778 tmp = strdup(reloc_sym->name); 779 if (!tmp) 780 return -ENOMEM; 781 } 782 /* If we have no realname, use symbol for it */ 783 if (!tevs[i].point.realname) 784 tevs[i].point.realname = tevs[i].point.symbol; 785 else 786 free(tevs[i].point.symbol); 787 tevs[i].point.symbol = tmp; 788 tevs[i].point.offset = tevs[i].point.address - 789 reloc_sym->unrelocated_addr; 790 } 791 return skipped; 792 } 793 794 void __weak 795 arch__post_process_probe_trace_events(struct perf_probe_event *pev __maybe_unused, 796 int ntevs __maybe_unused) 797 { 798 } 799 800 /* Post processing the probe events */ 801 static int post_process_probe_trace_events(struct perf_probe_event *pev, 802 struct probe_trace_event *tevs, 803 int ntevs, const char *module, 804 bool uprobe, struct debuginfo *dinfo) 805 { 806 int ret; 807 808 if (uprobe) 809 ret = add_exec_to_probe_trace_events(tevs, ntevs, module, 810 pev->nsi); 811 else if (module) 812 /* Currently ref_reloc_sym based probe is not for drivers */ 813 ret = post_process_module_probe_trace_events(tevs, ntevs, 814 module, dinfo); 815 else 816 ret = post_process_kernel_probe_trace_events(tevs, ntevs); 817 818 if (ret >= 0) 819 arch__post_process_probe_trace_events(pev, ntevs); 820 821 return ret; 822 } 823 824 /* Try to find perf_probe_event with debuginfo */ 825 static int try_to_find_probe_trace_events(struct perf_probe_event *pev, 826 struct probe_trace_event **tevs) 827 { 828 bool need_dwarf = perf_probe_event_need_dwarf(pev); 829 struct perf_probe_point tmp; 830 struct debuginfo *dinfo; 831 int ntevs, ret = 0; 832 833 dinfo = open_debuginfo(pev->target, pev->nsi, !need_dwarf); 834 if (!dinfo) { 835 if (need_dwarf) 836 return -ENOENT; 837 pr_debug("Could not open debuginfo. Try to use symbols.\n"); 838 return 0; 839 } 840 841 pr_debug("Try to find probe point from debuginfo.\n"); 842 /* Searching trace events corresponding to a probe event */ 843 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs); 844 845 if (ntevs == 0) { /* Not found, retry with an alternative */ 846 ret = get_alternative_probe_event(dinfo, pev, &tmp); 847 if (!ret) { 848 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs); 849 /* 850 * Write back to the original probe_event for 851 * setting appropriate (user given) event name 852 */ 853 clear_perf_probe_point(&pev->point); 854 memcpy(&pev->point, &tmp, sizeof(tmp)); 855 } 856 } 857 858 if (ntevs > 0) { /* Succeeded to find trace events */ 859 pr_debug("Found %d probe_trace_events.\n", ntevs); 860 ret = post_process_probe_trace_events(pev, *tevs, ntevs, 861 pev->target, pev->uprobes, dinfo); 862 if (ret < 0 || ret == ntevs) { 863 pr_debug("Post processing failed or all events are skipped. (%d)\n", ret); 864 clear_probe_trace_events(*tevs, ntevs); 865 zfree(tevs); 866 ntevs = 0; 867 } 868 } 869 870 debuginfo__delete(dinfo); 871 872 if (ntevs == 0) { /* No error but failed to find probe point. */ 873 pr_warning("Probe point '%s' not found.\n", 874 synthesize_perf_probe_point(&pev->point)); 875 return -ENOENT; 876 } else if (ntevs < 0) { 877 /* Error path : ntevs < 0 */ 878 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs); 879 if (ntevs == -EBADF) 880 pr_warning("Warning: No dwarf info found in the vmlinux - " 881 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n"); 882 if (!need_dwarf) { 883 pr_debug("Trying to use symbols.\n"); 884 return 0; 885 } 886 } 887 return ntevs; 888 } 889 890 #define LINEBUF_SIZE 256 891 #define NR_ADDITIONAL_LINES 2 892 893 static int __show_one_line(FILE *fp, int l, bool skip, bool show_num) 894 { 895 char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE]; 896 const char *color = show_num ? "" : PERF_COLOR_BLUE; 897 const char *prefix = NULL; 898 899 do { 900 if (fgets(buf, LINEBUF_SIZE, fp) == NULL) 901 goto error; 902 if (skip) 903 continue; 904 if (!prefix) { 905 prefix = show_num ? "%7d " : " "; 906 color_fprintf(stdout, color, prefix, l); 907 } 908 color_fprintf(stdout, color, "%s", buf); 909 910 } while (strchr(buf, '\n') == NULL); 911 912 return 1; 913 error: 914 if (ferror(fp)) { 915 pr_warning("File read error: %s\n", 916 str_error_r(errno, sbuf, sizeof(sbuf))); 917 return -1; 918 } 919 return 0; 920 } 921 922 static int _show_one_line(FILE *fp, int l, bool skip, bool show_num) 923 { 924 int rv = __show_one_line(fp, l, skip, show_num); 925 if (rv == 0) { 926 pr_warning("Source file is shorter than expected.\n"); 927 rv = -1; 928 } 929 return rv; 930 } 931 932 #define show_one_line_with_num(f,l) _show_one_line(f,l,false,true) 933 #define show_one_line(f,l) _show_one_line(f,l,false,false) 934 #define skip_one_line(f,l) _show_one_line(f,l,true,false) 935 #define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false) 936 937 /* 938 * Show line-range always requires debuginfo to find source file and 939 * line number. 940 */ 941 static int __show_line_range(struct line_range *lr, const char *module, 942 bool user) 943 { 944 int l = 1; 945 struct int_node *ln; 946 struct debuginfo *dinfo; 947 FILE *fp; 948 int ret; 949 char *tmp; 950 char sbuf[STRERR_BUFSIZE]; 951 952 /* Search a line range */ 953 dinfo = open_debuginfo(module, NULL, false); 954 if (!dinfo) 955 return -ENOENT; 956 957 ret = debuginfo__find_line_range(dinfo, lr); 958 if (!ret) { /* Not found, retry with an alternative */ 959 ret = get_alternative_line_range(dinfo, lr, module, user); 960 if (!ret) 961 ret = debuginfo__find_line_range(dinfo, lr); 962 } 963 debuginfo__delete(dinfo); 964 if (ret == 0 || ret == -ENOENT) { 965 pr_warning("Specified source line is not found.\n"); 966 return -ENOENT; 967 } else if (ret < 0) { 968 pr_warning("Debuginfo analysis failed.\n"); 969 return ret; 970 } 971 972 /* Convert source file path */ 973 tmp = lr->path; 974 ret = get_real_path(tmp, lr->comp_dir, &lr->path); 975 976 /* Free old path when new path is assigned */ 977 if (tmp != lr->path) 978 free(tmp); 979 980 if (ret < 0) { 981 pr_warning("Failed to find source file path.\n"); 982 return ret; 983 } 984 985 setup_pager(); 986 987 if (lr->function) 988 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path, 989 lr->start - lr->offset); 990 else 991 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start); 992 993 fp = fopen(lr->path, "r"); 994 if (fp == NULL) { 995 pr_warning("Failed to open %s: %s\n", lr->path, 996 str_error_r(errno, sbuf, sizeof(sbuf))); 997 return -errno; 998 } 999 /* Skip to starting line number */ 1000 while (l < lr->start) { 1001 ret = skip_one_line(fp, l++); 1002 if (ret < 0) 1003 goto end; 1004 } 1005 1006 intlist__for_each_entry(ln, lr->line_list) { 1007 for (; ln->i > l; l++) { 1008 ret = show_one_line(fp, l - lr->offset); 1009 if (ret < 0) 1010 goto end; 1011 } 1012 ret = show_one_line_with_num(fp, l++ - lr->offset); 1013 if (ret < 0) 1014 goto end; 1015 } 1016 1017 if (lr->end == INT_MAX) 1018 lr->end = l + NR_ADDITIONAL_LINES; 1019 while (l <= lr->end) { 1020 ret = show_one_line_or_eof(fp, l++ - lr->offset); 1021 if (ret <= 0) 1022 break; 1023 } 1024 end: 1025 fclose(fp); 1026 return ret; 1027 } 1028 1029 int show_line_range(struct line_range *lr, const char *module, 1030 struct nsinfo *nsi, bool user) 1031 { 1032 int ret; 1033 struct nscookie nsc; 1034 1035 ret = init_probe_symbol_maps(user); 1036 if (ret < 0) 1037 return ret; 1038 nsinfo__mountns_enter(nsi, &nsc); 1039 ret = __show_line_range(lr, module, user); 1040 nsinfo__mountns_exit(&nsc); 1041 exit_probe_symbol_maps(); 1042 1043 return ret; 1044 } 1045 1046 static int show_available_vars_at(struct debuginfo *dinfo, 1047 struct perf_probe_event *pev, 1048 struct strfilter *_filter) 1049 { 1050 char *buf; 1051 int ret, i, nvars; 1052 struct str_node *node; 1053 struct variable_list *vls = NULL, *vl; 1054 struct perf_probe_point tmp; 1055 const char *var; 1056 1057 buf = synthesize_perf_probe_point(&pev->point); 1058 if (!buf) 1059 return -EINVAL; 1060 pr_debug("Searching variables at %s\n", buf); 1061 1062 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls); 1063 if (!ret) { /* Not found, retry with an alternative */ 1064 ret = get_alternative_probe_event(dinfo, pev, &tmp); 1065 if (!ret) { 1066 ret = debuginfo__find_available_vars_at(dinfo, pev, 1067 &vls); 1068 /* Release the old probe_point */ 1069 clear_perf_probe_point(&tmp); 1070 } 1071 } 1072 if (ret <= 0) { 1073 if (ret == 0 || ret == -ENOENT) { 1074 pr_err("Failed to find the address of %s\n", buf); 1075 ret = -ENOENT; 1076 } else 1077 pr_warning("Debuginfo analysis failed.\n"); 1078 goto end; 1079 } 1080 1081 /* Some variables are found */ 1082 fprintf(stdout, "Available variables at %s\n", buf); 1083 for (i = 0; i < ret; i++) { 1084 vl = &vls[i]; 1085 /* 1086 * A probe point might be converted to 1087 * several trace points. 1088 */ 1089 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol, 1090 vl->point.offset); 1091 zfree(&vl->point.symbol); 1092 nvars = 0; 1093 if (vl->vars) { 1094 strlist__for_each_entry(node, vl->vars) { 1095 var = strchr(node->s, '\t') + 1; 1096 if (strfilter__compare(_filter, var)) { 1097 fprintf(stdout, "\t\t%s\n", node->s); 1098 nvars++; 1099 } 1100 } 1101 strlist__delete(vl->vars); 1102 } 1103 if (nvars == 0) 1104 fprintf(stdout, "\t\t(No matched variables)\n"); 1105 } 1106 free(vls); 1107 end: 1108 free(buf); 1109 return ret; 1110 } 1111 1112 /* Show available variables on given probe point */ 1113 int show_available_vars(struct perf_probe_event *pevs, int npevs, 1114 struct strfilter *_filter) 1115 { 1116 int i, ret = 0; 1117 struct debuginfo *dinfo; 1118 1119 ret = init_probe_symbol_maps(pevs->uprobes); 1120 if (ret < 0) 1121 return ret; 1122 1123 dinfo = open_debuginfo(pevs->target, pevs->nsi, false); 1124 if (!dinfo) { 1125 ret = -ENOENT; 1126 goto out; 1127 } 1128 1129 setup_pager(); 1130 1131 for (i = 0; i < npevs && ret >= 0; i++) 1132 ret = show_available_vars_at(dinfo, &pevs[i], _filter); 1133 1134 debuginfo__delete(dinfo); 1135 out: 1136 exit_probe_symbol_maps(); 1137 return ret; 1138 } 1139 1140 #else /* !HAVE_DWARF_SUPPORT */ 1141 1142 static void debuginfo_cache__exit(void) 1143 { 1144 } 1145 1146 static int 1147 find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused, 1148 struct perf_probe_point *pp __maybe_unused, 1149 bool is_kprobe __maybe_unused) 1150 { 1151 return -ENOSYS; 1152 } 1153 1154 static int try_to_find_probe_trace_events(struct perf_probe_event *pev, 1155 struct probe_trace_event **tevs __maybe_unused) 1156 { 1157 if (perf_probe_event_need_dwarf(pev)) { 1158 pr_warning("Debuginfo-analysis is not supported.\n"); 1159 return -ENOSYS; 1160 } 1161 1162 return 0; 1163 } 1164 1165 int show_line_range(struct line_range *lr __maybe_unused, 1166 const char *module __maybe_unused, 1167 struct nsinfo *nsi __maybe_unused, 1168 bool user __maybe_unused) 1169 { 1170 pr_warning("Debuginfo-analysis is not supported.\n"); 1171 return -ENOSYS; 1172 } 1173 1174 int show_available_vars(struct perf_probe_event *pevs __maybe_unused, 1175 int npevs __maybe_unused, 1176 struct strfilter *filter __maybe_unused) 1177 { 1178 pr_warning("Debuginfo-analysis is not supported.\n"); 1179 return -ENOSYS; 1180 } 1181 #endif 1182 1183 void line_range__clear(struct line_range *lr) 1184 { 1185 free(lr->function); 1186 free(lr->file); 1187 free(lr->path); 1188 free(lr->comp_dir); 1189 intlist__delete(lr->line_list); 1190 memset(lr, 0, sizeof(*lr)); 1191 } 1192 1193 int line_range__init(struct line_range *lr) 1194 { 1195 memset(lr, 0, sizeof(*lr)); 1196 lr->line_list = intlist__new(NULL); 1197 if (!lr->line_list) 1198 return -ENOMEM; 1199 else 1200 return 0; 1201 } 1202 1203 static int parse_line_num(char **ptr, int *val, const char *what) 1204 { 1205 const char *start = *ptr; 1206 1207 errno = 0; 1208 *val = strtol(*ptr, ptr, 0); 1209 if (errno || *ptr == start) { 1210 semantic_error("'%s' is not a valid number.\n", what); 1211 return -EINVAL; 1212 } 1213 return 0; 1214 } 1215 1216 /* Check the name is good for event, group or function */ 1217 static bool is_c_func_name(const char *name) 1218 { 1219 if (!isalpha(*name) && *name != '_') 1220 return false; 1221 while (*++name != '\0') { 1222 if (!isalpha(*name) && !isdigit(*name) && *name != '_') 1223 return false; 1224 } 1225 return true; 1226 } 1227 1228 /* 1229 * Stuff 'lr' according to the line range described by 'arg'. 1230 * The line range syntax is described by: 1231 * 1232 * SRC[:SLN[+NUM|-ELN]] 1233 * FNC[@SRC][:SLN[+NUM|-ELN]] 1234 */ 1235 int parse_line_range_desc(const char *arg, struct line_range *lr) 1236 { 1237 char *range, *file, *name = strdup(arg); 1238 int err; 1239 1240 if (!name) 1241 return -ENOMEM; 1242 1243 lr->start = 0; 1244 lr->end = INT_MAX; 1245 1246 range = strchr(name, ':'); 1247 if (range) { 1248 *range++ = '\0'; 1249 1250 err = parse_line_num(&range, &lr->start, "start line"); 1251 if (err) 1252 goto err; 1253 1254 if (*range == '+' || *range == '-') { 1255 const char c = *range++; 1256 1257 err = parse_line_num(&range, &lr->end, "end line"); 1258 if (err) 1259 goto err; 1260 1261 if (c == '+') { 1262 lr->end += lr->start; 1263 /* 1264 * Adjust the number of lines here. 1265 * If the number of lines == 1, the 1266 * the end of line should be equal to 1267 * the start of line. 1268 */ 1269 lr->end--; 1270 } 1271 } 1272 1273 pr_debug("Line range is %d to %d\n", lr->start, lr->end); 1274 1275 err = -EINVAL; 1276 if (lr->start > lr->end) { 1277 semantic_error("Start line must be smaller" 1278 " than end line.\n"); 1279 goto err; 1280 } 1281 if (*range != '\0') { 1282 semantic_error("Tailing with invalid str '%s'.\n", range); 1283 goto err; 1284 } 1285 } 1286 1287 file = strchr(name, '@'); 1288 if (file) { 1289 *file = '\0'; 1290 lr->file = strdup(++file); 1291 if (lr->file == NULL) { 1292 err = -ENOMEM; 1293 goto err; 1294 } 1295 lr->function = name; 1296 } else if (strchr(name, '/') || strchr(name, '.')) 1297 lr->file = name; 1298 else if (is_c_func_name(name))/* We reuse it for checking funcname */ 1299 lr->function = name; 1300 else { /* Invalid name */ 1301 semantic_error("'%s' is not a valid function name.\n", name); 1302 err = -EINVAL; 1303 goto err; 1304 } 1305 1306 return 0; 1307 err: 1308 free(name); 1309 return err; 1310 } 1311 1312 static int parse_perf_probe_event_name(char **arg, struct perf_probe_event *pev) 1313 { 1314 char *ptr; 1315 1316 ptr = strpbrk_esc(*arg, ":"); 1317 if (ptr) { 1318 *ptr = '\0'; 1319 if (!pev->sdt && !is_c_func_name(*arg)) 1320 goto ng_name; 1321 pev->group = strdup_esc(*arg); 1322 if (!pev->group) 1323 return -ENOMEM; 1324 *arg = ptr + 1; 1325 } else 1326 pev->group = NULL; 1327 1328 pev->event = strdup_esc(*arg); 1329 if (pev->event == NULL) 1330 return -ENOMEM; 1331 1332 if (!pev->sdt && !is_c_func_name(pev->event)) { 1333 zfree(&pev->event); 1334 ng_name: 1335 zfree(&pev->group); 1336 semantic_error("%s is bad for event name -it must " 1337 "follow C symbol-naming rule.\n", *arg); 1338 return -EINVAL; 1339 } 1340 return 0; 1341 } 1342 1343 /* Parse probepoint definition. */ 1344 static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev) 1345 { 1346 struct perf_probe_point *pp = &pev->point; 1347 char *ptr, *tmp; 1348 char c, nc = 0; 1349 bool file_spec = false; 1350 int ret; 1351 1352 /* 1353 * <Syntax> 1354 * perf probe [GRP:][EVENT=]SRC[:LN|;PTN] 1355 * perf probe [GRP:][EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT] 1356 * perf probe %[GRP:]SDT_EVENT 1357 */ 1358 if (!arg) 1359 return -EINVAL; 1360 1361 if (is_sdt_event(arg)) { 1362 pev->sdt = true; 1363 if (arg[0] == '%') 1364 arg++; 1365 } 1366 1367 ptr = strpbrk_esc(arg, ";=@+%"); 1368 if (pev->sdt) { 1369 if (ptr) { 1370 if (*ptr != '@') { 1371 semantic_error("%s must be an SDT name.\n", 1372 arg); 1373 return -EINVAL; 1374 } 1375 /* This must be a target file name or build id */ 1376 tmp = build_id_cache__complement(ptr + 1); 1377 if (tmp) { 1378 pev->target = build_id_cache__origname(tmp); 1379 free(tmp); 1380 } else 1381 pev->target = strdup_esc(ptr + 1); 1382 if (!pev->target) 1383 return -ENOMEM; 1384 *ptr = '\0'; 1385 } 1386 ret = parse_perf_probe_event_name(&arg, pev); 1387 if (ret == 0) { 1388 if (asprintf(&pev->point.function, "%%%s", pev->event) < 0) 1389 ret = -errno; 1390 } 1391 return ret; 1392 } 1393 1394 if (ptr && *ptr == '=') { /* Event name */ 1395 *ptr = '\0'; 1396 tmp = ptr + 1; 1397 ret = parse_perf_probe_event_name(&arg, pev); 1398 if (ret < 0) 1399 return ret; 1400 1401 arg = tmp; 1402 } 1403 1404 /* 1405 * Check arg is function or file name and copy it. 1406 * 1407 * We consider arg to be a file spec if and only if it satisfies 1408 * all of the below criteria:: 1409 * - it does not include any of "+@%", 1410 * - it includes one of ":;", and 1411 * - it has a period '.' in the name. 1412 * 1413 * Otherwise, we consider arg to be a function specification. 1414 */ 1415 if (!strpbrk_esc(arg, "+@%")) { 1416 ptr = strpbrk_esc(arg, ";:"); 1417 /* This is a file spec if it includes a '.' before ; or : */ 1418 if (ptr && memchr(arg, '.', ptr - arg)) 1419 file_spec = true; 1420 } 1421 1422 ptr = strpbrk_esc(arg, ";:+@%"); 1423 if (ptr) { 1424 nc = *ptr; 1425 *ptr++ = '\0'; 1426 } 1427 1428 if (arg[0] == '\0') 1429 tmp = NULL; 1430 else { 1431 tmp = strdup_esc(arg); 1432 if (tmp == NULL) 1433 return -ENOMEM; 1434 } 1435 1436 if (file_spec) 1437 pp->file = tmp; 1438 else { 1439 pp->function = tmp; 1440 1441 /* 1442 * Keep pp->function even if this is absolute address, 1443 * so it can mark whether abs_address is valid. 1444 * Which make 'perf probe lib.bin 0x0' possible. 1445 * 1446 * Note that checking length of tmp is not needed 1447 * because when we access tmp[1] we know tmp[0] is '0', 1448 * so tmp[1] should always valid (but could be '\0'). 1449 */ 1450 if (tmp && !strncmp(tmp, "0x", 2)) { 1451 pp->abs_address = strtoul(pp->function, &tmp, 0); 1452 if (*tmp != '\0') { 1453 semantic_error("Invalid absolute address.\n"); 1454 return -EINVAL; 1455 } 1456 } 1457 } 1458 1459 /* Parse other options */ 1460 while (ptr) { 1461 arg = ptr; 1462 c = nc; 1463 if (c == ';') { /* Lazy pattern must be the last part */ 1464 pp->lazy_line = strdup(arg); /* let leave escapes */ 1465 if (pp->lazy_line == NULL) 1466 return -ENOMEM; 1467 break; 1468 } 1469 ptr = strpbrk_esc(arg, ";:+@%"); 1470 if (ptr) { 1471 nc = *ptr; 1472 *ptr++ = '\0'; 1473 } 1474 switch (c) { 1475 case ':': /* Line number */ 1476 pp->line = strtoul(arg, &tmp, 0); 1477 if (*tmp != '\0') { 1478 semantic_error("There is non-digit char" 1479 " in line number.\n"); 1480 return -EINVAL; 1481 } 1482 break; 1483 case '+': /* Byte offset from a symbol */ 1484 pp->offset = strtoul(arg, &tmp, 0); 1485 if (*tmp != '\0') { 1486 semantic_error("There is non-digit character" 1487 " in offset.\n"); 1488 return -EINVAL; 1489 } 1490 break; 1491 case '@': /* File name */ 1492 if (pp->file) { 1493 semantic_error("SRC@SRC is not allowed.\n"); 1494 return -EINVAL; 1495 } 1496 pp->file = strdup_esc(arg); 1497 if (pp->file == NULL) 1498 return -ENOMEM; 1499 break; 1500 case '%': /* Probe places */ 1501 if (strcmp(arg, "return") == 0) { 1502 pp->retprobe = 1; 1503 } else { /* Others not supported yet */ 1504 semantic_error("%%%s is not supported.\n", arg); 1505 return -ENOTSUP; 1506 } 1507 break; 1508 default: /* Buggy case */ 1509 pr_err("This program has a bug at %s:%d.\n", 1510 __FILE__, __LINE__); 1511 return -ENOTSUP; 1512 break; 1513 } 1514 } 1515 1516 /* Exclusion check */ 1517 if (pp->lazy_line && pp->line) { 1518 semantic_error("Lazy pattern can't be used with" 1519 " line number.\n"); 1520 return -EINVAL; 1521 } 1522 1523 if (pp->lazy_line && pp->offset) { 1524 semantic_error("Lazy pattern can't be used with offset.\n"); 1525 return -EINVAL; 1526 } 1527 1528 if (pp->line && pp->offset) { 1529 semantic_error("Offset can't be used with line number.\n"); 1530 return -EINVAL; 1531 } 1532 1533 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) { 1534 semantic_error("File always requires line number or " 1535 "lazy pattern.\n"); 1536 return -EINVAL; 1537 } 1538 1539 if (pp->offset && !pp->function) { 1540 semantic_error("Offset requires an entry function.\n"); 1541 return -EINVAL; 1542 } 1543 1544 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) { 1545 semantic_error("Offset/Line/Lazy pattern can't be used with " 1546 "return probe.\n"); 1547 return -EINVAL; 1548 } 1549 1550 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n", 1551 pp->function, pp->file, pp->line, pp->offset, pp->retprobe, 1552 pp->lazy_line); 1553 return 0; 1554 } 1555 1556 /* Parse perf-probe event argument */ 1557 static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg) 1558 { 1559 char *tmp, *goodname; 1560 struct perf_probe_arg_field **fieldp; 1561 1562 pr_debug("parsing arg: %s into ", str); 1563 1564 tmp = strchr(str, '='); 1565 if (tmp) { 1566 arg->name = strndup(str, tmp - str); 1567 if (arg->name == NULL) 1568 return -ENOMEM; 1569 pr_debug("name:%s ", arg->name); 1570 str = tmp + 1; 1571 } 1572 1573 tmp = strchr(str, ':'); 1574 if (tmp) { /* Type setting */ 1575 *tmp = '\0'; 1576 arg->type = strdup(tmp + 1); 1577 if (arg->type == NULL) 1578 return -ENOMEM; 1579 pr_debug("type:%s ", arg->type); 1580 } 1581 1582 tmp = strpbrk(str, "-.["); 1583 if (!is_c_varname(str) || !tmp) { 1584 /* A variable, register, symbol or special value */ 1585 arg->var = strdup(str); 1586 if (arg->var == NULL) 1587 return -ENOMEM; 1588 pr_debug("%s\n", arg->var); 1589 return 0; 1590 } 1591 1592 /* Structure fields or array element */ 1593 arg->var = strndup(str, tmp - str); 1594 if (arg->var == NULL) 1595 return -ENOMEM; 1596 goodname = arg->var; 1597 pr_debug("%s, ", arg->var); 1598 fieldp = &arg->field; 1599 1600 do { 1601 *fieldp = zalloc(sizeof(struct perf_probe_arg_field)); 1602 if (*fieldp == NULL) 1603 return -ENOMEM; 1604 if (*tmp == '[') { /* Array */ 1605 str = tmp; 1606 (*fieldp)->index = strtol(str + 1, &tmp, 0); 1607 (*fieldp)->ref = true; 1608 if (*tmp != ']' || tmp == str + 1) { 1609 semantic_error("Array index must be a" 1610 " number.\n"); 1611 return -EINVAL; 1612 } 1613 tmp++; 1614 if (*tmp == '\0') 1615 tmp = NULL; 1616 } else { /* Structure */ 1617 if (*tmp == '.') { 1618 str = tmp + 1; 1619 (*fieldp)->ref = false; 1620 } else if (tmp[1] == '>') { 1621 str = tmp + 2; 1622 (*fieldp)->ref = true; 1623 } else { 1624 semantic_error("Argument parse error: %s\n", 1625 str); 1626 return -EINVAL; 1627 } 1628 tmp = strpbrk(str, "-.["); 1629 } 1630 if (tmp) { 1631 (*fieldp)->name = strndup(str, tmp - str); 1632 if ((*fieldp)->name == NULL) 1633 return -ENOMEM; 1634 if (*str != '[') 1635 goodname = (*fieldp)->name; 1636 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref); 1637 fieldp = &(*fieldp)->next; 1638 } 1639 } while (tmp); 1640 (*fieldp)->name = strdup(str); 1641 if ((*fieldp)->name == NULL) 1642 return -ENOMEM; 1643 if (*str != '[') 1644 goodname = (*fieldp)->name; 1645 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref); 1646 1647 /* If no name is specified, set the last field name (not array index)*/ 1648 if (!arg->name) { 1649 arg->name = strdup(goodname); 1650 if (arg->name == NULL) 1651 return -ENOMEM; 1652 } 1653 return 0; 1654 } 1655 1656 /* Parse perf-probe event command */ 1657 int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev) 1658 { 1659 char **argv; 1660 int argc, i, ret = 0; 1661 1662 argv = argv_split(cmd, &argc); 1663 if (!argv) { 1664 pr_debug("Failed to split arguments.\n"); 1665 return -ENOMEM; 1666 } 1667 if (argc - 1 > MAX_PROBE_ARGS) { 1668 semantic_error("Too many probe arguments (%d).\n", argc - 1); 1669 ret = -ERANGE; 1670 goto out; 1671 } 1672 /* Parse probe point */ 1673 ret = parse_perf_probe_point(argv[0], pev); 1674 if (ret < 0) 1675 goto out; 1676 1677 /* Copy arguments and ensure return probe has no C argument */ 1678 pev->nargs = argc - 1; 1679 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs); 1680 if (pev->args == NULL) { 1681 ret = -ENOMEM; 1682 goto out; 1683 } 1684 for (i = 0; i < pev->nargs && ret >= 0; i++) { 1685 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]); 1686 if (ret >= 0 && 1687 is_c_varname(pev->args[i].var) && pev->point.retprobe) { 1688 semantic_error("You can't specify local variable for" 1689 " kretprobe.\n"); 1690 ret = -EINVAL; 1691 } 1692 } 1693 out: 1694 argv_free(argv); 1695 1696 return ret; 1697 } 1698 1699 /* Returns true if *any* ARG is either C variable, $params or $vars. */ 1700 bool perf_probe_with_var(struct perf_probe_event *pev) 1701 { 1702 int i = 0; 1703 1704 for (i = 0; i < pev->nargs; i++) 1705 if (is_c_varname(pev->args[i].var) || 1706 !strcmp(pev->args[i].var, PROBE_ARG_PARAMS) || 1707 !strcmp(pev->args[i].var, PROBE_ARG_VARS)) 1708 return true; 1709 return false; 1710 } 1711 1712 /* Return true if this perf_probe_event requires debuginfo */ 1713 bool perf_probe_event_need_dwarf(struct perf_probe_event *pev) 1714 { 1715 if (pev->point.file || pev->point.line || pev->point.lazy_line) 1716 return true; 1717 1718 if (perf_probe_with_var(pev)) 1719 return true; 1720 1721 return false; 1722 } 1723 1724 /* Parse probe_events event into struct probe_point */ 1725 int parse_probe_trace_command(const char *cmd, struct probe_trace_event *tev) 1726 { 1727 struct probe_trace_point *tp = &tev->point; 1728 char pr; 1729 char *p; 1730 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str; 1731 int ret, i, argc; 1732 char **argv; 1733 1734 pr_debug("Parsing probe_events: %s\n", cmd); 1735 argv = argv_split(cmd, &argc); 1736 if (!argv) { 1737 pr_debug("Failed to split arguments.\n"); 1738 return -ENOMEM; 1739 } 1740 if (argc < 2) { 1741 semantic_error("Too few probe arguments.\n"); 1742 ret = -ERANGE; 1743 goto out; 1744 } 1745 1746 /* Scan event and group name. */ 1747 argv0_str = strdup(argv[0]); 1748 if (argv0_str == NULL) { 1749 ret = -ENOMEM; 1750 goto out; 1751 } 1752 fmt1_str = strtok_r(argv0_str, ":", &fmt); 1753 fmt2_str = strtok_r(NULL, "/", &fmt); 1754 fmt3_str = strtok_r(NULL, " \t", &fmt); 1755 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL 1756 || fmt3_str == NULL) { 1757 semantic_error("Failed to parse event name: %s\n", argv[0]); 1758 ret = -EINVAL; 1759 goto out; 1760 } 1761 pr = fmt1_str[0]; 1762 tev->group = strdup(fmt2_str); 1763 tev->event = strdup(fmt3_str); 1764 if (tev->group == NULL || tev->event == NULL) { 1765 ret = -ENOMEM; 1766 goto out; 1767 } 1768 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr); 1769 1770 tp->retprobe = (pr == 'r'); 1771 1772 /* Scan module name(if there), function name and offset */ 1773 p = strchr(argv[1], ':'); 1774 if (p) { 1775 tp->module = strndup(argv[1], p - argv[1]); 1776 if (!tp->module) { 1777 ret = -ENOMEM; 1778 goto out; 1779 } 1780 tev->uprobes = (tp->module[0] == '/'); 1781 p++; 1782 } else 1783 p = argv[1]; 1784 fmt1_str = strtok_r(p, "+", &fmt); 1785 /* only the address started with 0x */ 1786 if (fmt1_str[0] == '0') { 1787 /* 1788 * Fix a special case: 1789 * if address == 0, kernel reports something like: 1790 * p:probe_libc/abs_0 /lib/libc-2.18.so:0x (null) arg1=%ax 1791 * Newer kernel may fix that, but we want to 1792 * support old kernel also. 1793 */ 1794 if (strcmp(fmt1_str, "0x") == 0) { 1795 if (!argv[2] || strcmp(argv[2], "(null)")) { 1796 ret = -EINVAL; 1797 goto out; 1798 } 1799 tp->address = 0; 1800 1801 free(argv[2]); 1802 for (i = 2; argv[i + 1] != NULL; i++) 1803 argv[i] = argv[i + 1]; 1804 1805 argv[i] = NULL; 1806 argc -= 1; 1807 } else 1808 tp->address = strtoul(fmt1_str, NULL, 0); 1809 } else { 1810 /* Only the symbol-based probe has offset */ 1811 tp->symbol = strdup(fmt1_str); 1812 if (tp->symbol == NULL) { 1813 ret = -ENOMEM; 1814 goto out; 1815 } 1816 fmt2_str = strtok_r(NULL, "", &fmt); 1817 if (fmt2_str == NULL) 1818 tp->offset = 0; 1819 else 1820 tp->offset = strtoul(fmt2_str, NULL, 10); 1821 } 1822 1823 tev->nargs = argc - 2; 1824 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs); 1825 if (tev->args == NULL) { 1826 ret = -ENOMEM; 1827 goto out; 1828 } 1829 for (i = 0; i < tev->nargs; i++) { 1830 p = strchr(argv[i + 2], '='); 1831 if (p) /* We don't need which register is assigned. */ 1832 *p++ = '\0'; 1833 else 1834 p = argv[i + 2]; 1835 tev->args[i].name = strdup(argv[i + 2]); 1836 /* TODO: parse regs and offset */ 1837 tev->args[i].value = strdup(p); 1838 if (tev->args[i].name == NULL || tev->args[i].value == NULL) { 1839 ret = -ENOMEM; 1840 goto out; 1841 } 1842 } 1843 ret = 0; 1844 out: 1845 free(argv0_str); 1846 argv_free(argv); 1847 return ret; 1848 } 1849 1850 /* Compose only probe arg */ 1851 char *synthesize_perf_probe_arg(struct perf_probe_arg *pa) 1852 { 1853 struct perf_probe_arg_field *field = pa->field; 1854 struct strbuf buf; 1855 char *ret = NULL; 1856 int err; 1857 1858 if (strbuf_init(&buf, 64) < 0) 1859 return NULL; 1860 1861 if (pa->name && pa->var) 1862 err = strbuf_addf(&buf, "%s=%s", pa->name, pa->var); 1863 else 1864 err = strbuf_addstr(&buf, pa->name ?: pa->var); 1865 if (err) 1866 goto out; 1867 1868 while (field) { 1869 if (field->name[0] == '[') 1870 err = strbuf_addstr(&buf, field->name); 1871 else 1872 err = strbuf_addf(&buf, "%s%s", field->ref ? "->" : ".", 1873 field->name); 1874 field = field->next; 1875 if (err) 1876 goto out; 1877 } 1878 1879 if (pa->type) 1880 if (strbuf_addf(&buf, ":%s", pa->type) < 0) 1881 goto out; 1882 1883 ret = strbuf_detach(&buf, NULL); 1884 out: 1885 strbuf_release(&buf); 1886 return ret; 1887 } 1888 1889 /* Compose only probe point (not argument) */ 1890 char *synthesize_perf_probe_point(struct perf_probe_point *pp) 1891 { 1892 struct strbuf buf; 1893 char *tmp, *ret = NULL; 1894 int len, err = 0; 1895 1896 if (strbuf_init(&buf, 64) < 0) 1897 return NULL; 1898 1899 if (pp->function) { 1900 if (strbuf_addstr(&buf, pp->function) < 0) 1901 goto out; 1902 if (pp->offset) 1903 err = strbuf_addf(&buf, "+%lu", pp->offset); 1904 else if (pp->line) 1905 err = strbuf_addf(&buf, ":%d", pp->line); 1906 else if (pp->retprobe) 1907 err = strbuf_addstr(&buf, "%return"); 1908 if (err) 1909 goto out; 1910 } 1911 if (pp->file) { 1912 tmp = pp->file; 1913 len = strlen(tmp); 1914 if (len > 30) { 1915 tmp = strchr(pp->file + len - 30, '/'); 1916 tmp = tmp ? tmp + 1 : pp->file + len - 30; 1917 } 1918 err = strbuf_addf(&buf, "@%s", tmp); 1919 if (!err && !pp->function && pp->line) 1920 err = strbuf_addf(&buf, ":%d", pp->line); 1921 } 1922 if (!err) 1923 ret = strbuf_detach(&buf, NULL); 1924 out: 1925 strbuf_release(&buf); 1926 return ret; 1927 } 1928 1929 char *synthesize_perf_probe_command(struct perf_probe_event *pev) 1930 { 1931 struct strbuf buf; 1932 char *tmp, *ret = NULL; 1933 int i; 1934 1935 if (strbuf_init(&buf, 64)) 1936 return NULL; 1937 if (pev->event) 1938 if (strbuf_addf(&buf, "%s:%s=", pev->group ?: PERFPROBE_GROUP, 1939 pev->event) < 0) 1940 goto out; 1941 1942 tmp = synthesize_perf_probe_point(&pev->point); 1943 if (!tmp || strbuf_addstr(&buf, tmp) < 0) 1944 goto out; 1945 free(tmp); 1946 1947 for (i = 0; i < pev->nargs; i++) { 1948 tmp = synthesize_perf_probe_arg(pev->args + i); 1949 if (!tmp || strbuf_addf(&buf, " %s", tmp) < 0) 1950 goto out; 1951 free(tmp); 1952 } 1953 1954 ret = strbuf_detach(&buf, NULL); 1955 out: 1956 strbuf_release(&buf); 1957 return ret; 1958 } 1959 1960 static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref, 1961 struct strbuf *buf, int depth) 1962 { 1963 int err; 1964 if (ref->next) { 1965 depth = __synthesize_probe_trace_arg_ref(ref->next, buf, 1966 depth + 1); 1967 if (depth < 0) 1968 return depth; 1969 } 1970 err = strbuf_addf(buf, "%+ld(", ref->offset); 1971 return (err < 0) ? err : depth; 1972 } 1973 1974 static int synthesize_probe_trace_arg(struct probe_trace_arg *arg, 1975 struct strbuf *buf) 1976 { 1977 struct probe_trace_arg_ref *ref = arg->ref; 1978 int depth = 0, err; 1979 1980 /* Argument name or separator */ 1981 if (arg->name) 1982 err = strbuf_addf(buf, " %s=", arg->name); 1983 else 1984 err = strbuf_addch(buf, ' '); 1985 if (err) 1986 return err; 1987 1988 /* Special case: @XXX */ 1989 if (arg->value[0] == '@' && arg->ref) 1990 ref = ref->next; 1991 1992 /* Dereferencing arguments */ 1993 if (ref) { 1994 depth = __synthesize_probe_trace_arg_ref(ref, buf, 1); 1995 if (depth < 0) 1996 return depth; 1997 } 1998 1999 /* Print argument value */ 2000 if (arg->value[0] == '@' && arg->ref) 2001 err = strbuf_addf(buf, "%s%+ld", arg->value, arg->ref->offset); 2002 else 2003 err = strbuf_addstr(buf, arg->value); 2004 2005 /* Closing */ 2006 while (!err && depth--) 2007 err = strbuf_addch(buf, ')'); 2008 2009 /* Print argument type */ 2010 if (!err && arg->type) 2011 err = strbuf_addf(buf, ":%s", arg->type); 2012 2013 return err; 2014 } 2015 2016 char *synthesize_probe_trace_command(struct probe_trace_event *tev) 2017 { 2018 struct probe_trace_point *tp = &tev->point; 2019 struct strbuf buf; 2020 char *ret = NULL; 2021 int i, err; 2022 2023 /* Uprobes must have tp->module */ 2024 if (tev->uprobes && !tp->module) 2025 return NULL; 2026 2027 if (strbuf_init(&buf, 32) < 0) 2028 return NULL; 2029 2030 if (strbuf_addf(&buf, "%c:%s/%s ", tp->retprobe ? 'r' : 'p', 2031 tev->group, tev->event) < 0) 2032 goto error; 2033 /* 2034 * If tp->address == 0, then this point must be a 2035 * absolute address uprobe. 2036 * try_to_find_absolute_address() should have made 2037 * tp->symbol to "0x0". 2038 */ 2039 if (tev->uprobes && !tp->address) { 2040 if (!tp->symbol || strcmp(tp->symbol, "0x0")) 2041 goto error; 2042 } 2043 2044 /* Use the tp->address for uprobes */ 2045 if (tev->uprobes) 2046 err = strbuf_addf(&buf, "%s:0x%lx", tp->module, tp->address); 2047 else if (!strncmp(tp->symbol, "0x", 2)) 2048 /* Absolute address. See try_to_find_absolute_address() */ 2049 err = strbuf_addf(&buf, "%s%s0x%lx", tp->module ?: "", 2050 tp->module ? ":" : "", tp->address); 2051 else 2052 err = strbuf_addf(&buf, "%s%s%s+%lu", tp->module ?: "", 2053 tp->module ? ":" : "", tp->symbol, tp->offset); 2054 if (err) 2055 goto error; 2056 2057 for (i = 0; i < tev->nargs; i++) 2058 if (synthesize_probe_trace_arg(&tev->args[i], &buf) < 0) 2059 goto error; 2060 2061 ret = strbuf_detach(&buf, NULL); 2062 error: 2063 strbuf_release(&buf); 2064 return ret; 2065 } 2066 2067 static int find_perf_probe_point_from_map(struct probe_trace_point *tp, 2068 struct perf_probe_point *pp, 2069 bool is_kprobe) 2070 { 2071 struct symbol *sym = NULL; 2072 struct map *map = NULL; 2073 u64 addr = tp->address; 2074 int ret = -ENOENT; 2075 2076 if (!is_kprobe) { 2077 map = dso__new_map(tp->module); 2078 if (!map) 2079 goto out; 2080 sym = map__find_symbol(map, addr); 2081 } else { 2082 if (tp->symbol && !addr) { 2083 if (kernel_get_symbol_address_by_name(tp->symbol, 2084 &addr, true, false) < 0) 2085 goto out; 2086 } 2087 if (addr) { 2088 addr += tp->offset; 2089 sym = machine__find_kernel_symbol(host_machine, addr, &map); 2090 } 2091 } 2092 2093 if (!sym) 2094 goto out; 2095 2096 pp->retprobe = tp->retprobe; 2097 pp->offset = addr - map->unmap_ip(map, sym->start); 2098 pp->function = strdup(sym->name); 2099 ret = pp->function ? 0 : -ENOMEM; 2100 2101 out: 2102 if (map && !is_kprobe) { 2103 map__put(map); 2104 } 2105 2106 return ret; 2107 } 2108 2109 static int convert_to_perf_probe_point(struct probe_trace_point *tp, 2110 struct perf_probe_point *pp, 2111 bool is_kprobe) 2112 { 2113 char buf[128]; 2114 int ret; 2115 2116 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe); 2117 if (!ret) 2118 return 0; 2119 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe); 2120 if (!ret) 2121 return 0; 2122 2123 pr_debug("Failed to find probe point from both of dwarf and map.\n"); 2124 2125 if (tp->symbol) { 2126 pp->function = strdup(tp->symbol); 2127 pp->offset = tp->offset; 2128 } else { 2129 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address); 2130 if (ret < 0) 2131 return ret; 2132 pp->function = strdup(buf); 2133 pp->offset = 0; 2134 } 2135 if (pp->function == NULL) 2136 return -ENOMEM; 2137 2138 pp->retprobe = tp->retprobe; 2139 2140 return 0; 2141 } 2142 2143 static int convert_to_perf_probe_event(struct probe_trace_event *tev, 2144 struct perf_probe_event *pev, bool is_kprobe) 2145 { 2146 struct strbuf buf = STRBUF_INIT; 2147 int i, ret; 2148 2149 /* Convert event/group name */ 2150 pev->event = strdup(tev->event); 2151 pev->group = strdup(tev->group); 2152 if (pev->event == NULL || pev->group == NULL) 2153 return -ENOMEM; 2154 2155 /* Convert trace_point to probe_point */ 2156 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe); 2157 if (ret < 0) 2158 return ret; 2159 2160 /* Convert trace_arg to probe_arg */ 2161 pev->nargs = tev->nargs; 2162 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs); 2163 if (pev->args == NULL) 2164 return -ENOMEM; 2165 for (i = 0; i < tev->nargs && ret >= 0; i++) { 2166 if (tev->args[i].name) 2167 pev->args[i].name = strdup(tev->args[i].name); 2168 else { 2169 if ((ret = strbuf_init(&buf, 32)) < 0) 2170 goto error; 2171 ret = synthesize_probe_trace_arg(&tev->args[i], &buf); 2172 pev->args[i].name = strbuf_detach(&buf, NULL); 2173 } 2174 if (pev->args[i].name == NULL && ret >= 0) 2175 ret = -ENOMEM; 2176 } 2177 error: 2178 if (ret < 0) 2179 clear_perf_probe_event(pev); 2180 2181 return ret; 2182 } 2183 2184 void clear_perf_probe_event(struct perf_probe_event *pev) 2185 { 2186 struct perf_probe_arg_field *field, *next; 2187 int i; 2188 2189 free(pev->event); 2190 free(pev->group); 2191 free(pev->target); 2192 clear_perf_probe_point(&pev->point); 2193 2194 for (i = 0; i < pev->nargs; i++) { 2195 free(pev->args[i].name); 2196 free(pev->args[i].var); 2197 free(pev->args[i].type); 2198 field = pev->args[i].field; 2199 while (field) { 2200 next = field->next; 2201 zfree(&field->name); 2202 free(field); 2203 field = next; 2204 } 2205 } 2206 free(pev->args); 2207 memset(pev, 0, sizeof(*pev)); 2208 } 2209 2210 #define strdup_or_goto(str, label) \ 2211 ({ char *__p = NULL; if (str && !(__p = strdup(str))) goto label; __p; }) 2212 2213 static int perf_probe_point__copy(struct perf_probe_point *dst, 2214 struct perf_probe_point *src) 2215 { 2216 dst->file = strdup_or_goto(src->file, out_err); 2217 dst->function = strdup_or_goto(src->function, out_err); 2218 dst->lazy_line = strdup_or_goto(src->lazy_line, out_err); 2219 dst->line = src->line; 2220 dst->retprobe = src->retprobe; 2221 dst->offset = src->offset; 2222 return 0; 2223 2224 out_err: 2225 clear_perf_probe_point(dst); 2226 return -ENOMEM; 2227 } 2228 2229 static int perf_probe_arg__copy(struct perf_probe_arg *dst, 2230 struct perf_probe_arg *src) 2231 { 2232 struct perf_probe_arg_field *field, **ppfield; 2233 2234 dst->name = strdup_or_goto(src->name, out_err); 2235 dst->var = strdup_or_goto(src->var, out_err); 2236 dst->type = strdup_or_goto(src->type, out_err); 2237 2238 field = src->field; 2239 ppfield = &(dst->field); 2240 while (field) { 2241 *ppfield = zalloc(sizeof(*field)); 2242 if (!*ppfield) 2243 goto out_err; 2244 (*ppfield)->name = strdup_or_goto(field->name, out_err); 2245 (*ppfield)->index = field->index; 2246 (*ppfield)->ref = field->ref; 2247 field = field->next; 2248 ppfield = &((*ppfield)->next); 2249 } 2250 return 0; 2251 out_err: 2252 return -ENOMEM; 2253 } 2254 2255 int perf_probe_event__copy(struct perf_probe_event *dst, 2256 struct perf_probe_event *src) 2257 { 2258 int i; 2259 2260 dst->event = strdup_or_goto(src->event, out_err); 2261 dst->group = strdup_or_goto(src->group, out_err); 2262 dst->target = strdup_or_goto(src->target, out_err); 2263 dst->uprobes = src->uprobes; 2264 2265 if (perf_probe_point__copy(&dst->point, &src->point) < 0) 2266 goto out_err; 2267 2268 dst->args = zalloc(sizeof(struct perf_probe_arg) * src->nargs); 2269 if (!dst->args) 2270 goto out_err; 2271 dst->nargs = src->nargs; 2272 2273 for (i = 0; i < src->nargs; i++) 2274 if (perf_probe_arg__copy(&dst->args[i], &src->args[i]) < 0) 2275 goto out_err; 2276 return 0; 2277 2278 out_err: 2279 clear_perf_probe_event(dst); 2280 return -ENOMEM; 2281 } 2282 2283 void clear_probe_trace_event(struct probe_trace_event *tev) 2284 { 2285 struct probe_trace_arg_ref *ref, *next; 2286 int i; 2287 2288 free(tev->event); 2289 free(tev->group); 2290 free(tev->point.symbol); 2291 free(tev->point.realname); 2292 free(tev->point.module); 2293 for (i = 0; i < tev->nargs; i++) { 2294 free(tev->args[i].name); 2295 free(tev->args[i].value); 2296 free(tev->args[i].type); 2297 ref = tev->args[i].ref; 2298 while (ref) { 2299 next = ref->next; 2300 free(ref); 2301 ref = next; 2302 } 2303 } 2304 free(tev->args); 2305 memset(tev, 0, sizeof(*tev)); 2306 } 2307 2308 struct kprobe_blacklist_node { 2309 struct list_head list; 2310 unsigned long start; 2311 unsigned long end; 2312 char *symbol; 2313 }; 2314 2315 static void kprobe_blacklist__delete(struct list_head *blacklist) 2316 { 2317 struct kprobe_blacklist_node *node; 2318 2319 while (!list_empty(blacklist)) { 2320 node = list_first_entry(blacklist, 2321 struct kprobe_blacklist_node, list); 2322 list_del(&node->list); 2323 free(node->symbol); 2324 free(node); 2325 } 2326 } 2327 2328 static int kprobe_blacklist__load(struct list_head *blacklist) 2329 { 2330 struct kprobe_blacklist_node *node; 2331 const char *__debugfs = debugfs__mountpoint(); 2332 char buf[PATH_MAX], *p; 2333 FILE *fp; 2334 int ret; 2335 2336 if (__debugfs == NULL) 2337 return -ENOTSUP; 2338 2339 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs); 2340 if (ret < 0) 2341 return ret; 2342 2343 fp = fopen(buf, "r"); 2344 if (!fp) 2345 return -errno; 2346 2347 ret = 0; 2348 while (fgets(buf, PATH_MAX, fp)) { 2349 node = zalloc(sizeof(*node)); 2350 if (!node) { 2351 ret = -ENOMEM; 2352 break; 2353 } 2354 INIT_LIST_HEAD(&node->list); 2355 list_add_tail(&node->list, blacklist); 2356 if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) { 2357 ret = -EINVAL; 2358 break; 2359 } 2360 p = strchr(buf, '\t'); 2361 if (p) { 2362 p++; 2363 if (p[strlen(p) - 1] == '\n') 2364 p[strlen(p) - 1] = '\0'; 2365 } else 2366 p = (char *)"unknown"; 2367 node->symbol = strdup(p); 2368 if (!node->symbol) { 2369 ret = -ENOMEM; 2370 break; 2371 } 2372 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n", 2373 node->start, node->end, node->symbol); 2374 ret++; 2375 } 2376 if (ret < 0) 2377 kprobe_blacklist__delete(blacklist); 2378 fclose(fp); 2379 2380 return ret; 2381 } 2382 2383 static struct kprobe_blacklist_node * 2384 kprobe_blacklist__find_by_address(struct list_head *blacklist, 2385 unsigned long address) 2386 { 2387 struct kprobe_blacklist_node *node; 2388 2389 list_for_each_entry(node, blacklist, list) { 2390 if (node->start <= address && address < node->end) 2391 return node; 2392 } 2393 2394 return NULL; 2395 } 2396 2397 static LIST_HEAD(kprobe_blacklist); 2398 2399 static void kprobe_blacklist__init(void) 2400 { 2401 if (!list_empty(&kprobe_blacklist)) 2402 return; 2403 2404 if (kprobe_blacklist__load(&kprobe_blacklist) < 0) 2405 pr_debug("No kprobe blacklist support, ignored\n"); 2406 } 2407 2408 static void kprobe_blacklist__release(void) 2409 { 2410 kprobe_blacklist__delete(&kprobe_blacklist); 2411 } 2412 2413 static bool kprobe_blacklist__listed(unsigned long address) 2414 { 2415 return !!kprobe_blacklist__find_by_address(&kprobe_blacklist, address); 2416 } 2417 2418 static int perf_probe_event__sprintf(const char *group, const char *event, 2419 struct perf_probe_event *pev, 2420 const char *module, 2421 struct strbuf *result) 2422 { 2423 int i, ret; 2424 char *buf; 2425 2426 if (asprintf(&buf, "%s:%s", group, event) < 0) 2427 return -errno; 2428 ret = strbuf_addf(result, " %-20s (on ", buf); 2429 free(buf); 2430 if (ret) 2431 return ret; 2432 2433 /* Synthesize only event probe point */ 2434 buf = synthesize_perf_probe_point(&pev->point); 2435 if (!buf) 2436 return -ENOMEM; 2437 ret = strbuf_addstr(result, buf); 2438 free(buf); 2439 2440 if (!ret && module) 2441 ret = strbuf_addf(result, " in %s", module); 2442 2443 if (!ret && pev->nargs > 0) { 2444 ret = strbuf_add(result, " with", 5); 2445 for (i = 0; !ret && i < pev->nargs; i++) { 2446 buf = synthesize_perf_probe_arg(&pev->args[i]); 2447 if (!buf) 2448 return -ENOMEM; 2449 ret = strbuf_addf(result, " %s", buf); 2450 free(buf); 2451 } 2452 } 2453 if (!ret) 2454 ret = strbuf_addch(result, ')'); 2455 2456 return ret; 2457 } 2458 2459 /* Show an event */ 2460 int show_perf_probe_event(const char *group, const char *event, 2461 struct perf_probe_event *pev, 2462 const char *module, bool use_stdout) 2463 { 2464 struct strbuf buf = STRBUF_INIT; 2465 int ret; 2466 2467 ret = perf_probe_event__sprintf(group, event, pev, module, &buf); 2468 if (ret >= 0) { 2469 if (use_stdout) 2470 printf("%s\n", buf.buf); 2471 else 2472 pr_info("%s\n", buf.buf); 2473 } 2474 strbuf_release(&buf); 2475 2476 return ret; 2477 } 2478 2479 static bool filter_probe_trace_event(struct probe_trace_event *tev, 2480 struct strfilter *filter) 2481 { 2482 char tmp[128]; 2483 2484 /* At first, check the event name itself */ 2485 if (strfilter__compare(filter, tev->event)) 2486 return true; 2487 2488 /* Next, check the combination of name and group */ 2489 if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0) 2490 return false; 2491 return strfilter__compare(filter, tmp); 2492 } 2493 2494 static int __show_perf_probe_events(int fd, bool is_kprobe, 2495 struct strfilter *filter) 2496 { 2497 int ret = 0; 2498 struct probe_trace_event tev; 2499 struct perf_probe_event pev; 2500 struct strlist *rawlist; 2501 struct str_node *ent; 2502 2503 memset(&tev, 0, sizeof(tev)); 2504 memset(&pev, 0, sizeof(pev)); 2505 2506 rawlist = probe_file__get_rawlist(fd); 2507 if (!rawlist) 2508 return -ENOMEM; 2509 2510 strlist__for_each_entry(ent, rawlist) { 2511 ret = parse_probe_trace_command(ent->s, &tev); 2512 if (ret >= 0) { 2513 if (!filter_probe_trace_event(&tev, filter)) 2514 goto next; 2515 ret = convert_to_perf_probe_event(&tev, &pev, 2516 is_kprobe); 2517 if (ret < 0) 2518 goto next; 2519 ret = show_perf_probe_event(pev.group, pev.event, 2520 &pev, tev.point.module, 2521 true); 2522 } 2523 next: 2524 clear_perf_probe_event(&pev); 2525 clear_probe_trace_event(&tev); 2526 if (ret < 0) 2527 break; 2528 } 2529 strlist__delete(rawlist); 2530 /* Cleanup cached debuginfo if needed */ 2531 debuginfo_cache__exit(); 2532 2533 return ret; 2534 } 2535 2536 /* List up current perf-probe events */ 2537 int show_perf_probe_events(struct strfilter *filter) 2538 { 2539 int kp_fd, up_fd, ret; 2540 2541 setup_pager(); 2542 2543 if (probe_conf.cache) 2544 return probe_cache__show_all_caches(filter); 2545 2546 ret = init_probe_symbol_maps(false); 2547 if (ret < 0) 2548 return ret; 2549 2550 ret = probe_file__open_both(&kp_fd, &up_fd, 0); 2551 if (ret < 0) 2552 return ret; 2553 2554 if (kp_fd >= 0) 2555 ret = __show_perf_probe_events(kp_fd, true, filter); 2556 if (up_fd >= 0 && ret >= 0) 2557 ret = __show_perf_probe_events(up_fd, false, filter); 2558 if (kp_fd > 0) 2559 close(kp_fd); 2560 if (up_fd > 0) 2561 close(up_fd); 2562 exit_probe_symbol_maps(); 2563 2564 return ret; 2565 } 2566 2567 static int get_new_event_name(char *buf, size_t len, const char *base, 2568 struct strlist *namelist, bool ret_event, 2569 bool allow_suffix) 2570 { 2571 int i, ret; 2572 char *p, *nbase; 2573 2574 if (*base == '.') 2575 base++; 2576 nbase = strdup(base); 2577 if (!nbase) 2578 return -ENOMEM; 2579 2580 /* Cut off the dot suffixes (e.g. .const, .isra) and version suffixes */ 2581 p = strpbrk(nbase, ".@"); 2582 if (p && p != nbase) 2583 *p = '\0'; 2584 2585 /* Try no suffix number */ 2586 ret = e_snprintf(buf, len, "%s%s", nbase, ret_event ? "__return" : ""); 2587 if (ret < 0) { 2588 pr_debug("snprintf() failed: %d\n", ret); 2589 goto out; 2590 } 2591 if (!strlist__has_entry(namelist, buf)) 2592 goto out; 2593 2594 if (!allow_suffix) { 2595 pr_warning("Error: event \"%s\" already exists.\n" 2596 " Hint: Remove existing event by 'perf probe -d'\n" 2597 " or force duplicates by 'perf probe -f'\n" 2598 " or set 'force=yes' in BPF source.\n", 2599 buf); 2600 ret = -EEXIST; 2601 goto out; 2602 } 2603 2604 /* Try to add suffix */ 2605 for (i = 1; i < MAX_EVENT_INDEX; i++) { 2606 ret = e_snprintf(buf, len, "%s_%d", nbase, i); 2607 if (ret < 0) { 2608 pr_debug("snprintf() failed: %d\n", ret); 2609 goto out; 2610 } 2611 if (!strlist__has_entry(namelist, buf)) 2612 break; 2613 } 2614 if (i == MAX_EVENT_INDEX) { 2615 pr_warning("Too many events are on the same function.\n"); 2616 ret = -ERANGE; 2617 } 2618 2619 out: 2620 free(nbase); 2621 2622 /* Final validation */ 2623 if (ret >= 0 && !is_c_func_name(buf)) { 2624 pr_warning("Internal error: \"%s\" is an invalid event name.\n", 2625 buf); 2626 ret = -EINVAL; 2627 } 2628 2629 return ret; 2630 } 2631 2632 /* Warn if the current kernel's uprobe implementation is old */ 2633 static void warn_uprobe_event_compat(struct probe_trace_event *tev) 2634 { 2635 int i; 2636 char *buf = synthesize_probe_trace_command(tev); 2637 2638 /* Old uprobe event doesn't support memory dereference */ 2639 if (!tev->uprobes || tev->nargs == 0 || !buf) 2640 goto out; 2641 2642 for (i = 0; i < tev->nargs; i++) 2643 if (strglobmatch(tev->args[i].value, "[$@+-]*")) { 2644 pr_warning("Please upgrade your kernel to at least " 2645 "3.14 to have access to feature %s\n", 2646 tev->args[i].value); 2647 break; 2648 } 2649 out: 2650 free(buf); 2651 } 2652 2653 /* Set new name from original perf_probe_event and namelist */ 2654 static int probe_trace_event__set_name(struct probe_trace_event *tev, 2655 struct perf_probe_event *pev, 2656 struct strlist *namelist, 2657 bool allow_suffix) 2658 { 2659 const char *event, *group; 2660 char buf[64]; 2661 int ret; 2662 2663 /* If probe_event or trace_event already have the name, reuse it */ 2664 if (pev->event && !pev->sdt) 2665 event = pev->event; 2666 else if (tev->event) 2667 event = tev->event; 2668 else { 2669 /* Or generate new one from probe point */ 2670 if (pev->point.function && 2671 (strncmp(pev->point.function, "0x", 2) != 0) && 2672 !strisglob(pev->point.function)) 2673 event = pev->point.function; 2674 else 2675 event = tev->point.realname; 2676 } 2677 if (pev->group && !pev->sdt) 2678 group = pev->group; 2679 else if (tev->group) 2680 group = tev->group; 2681 else 2682 group = PERFPROBE_GROUP; 2683 2684 /* Get an unused new event name */ 2685 ret = get_new_event_name(buf, 64, event, namelist, 2686 tev->point.retprobe, allow_suffix); 2687 if (ret < 0) 2688 return ret; 2689 2690 event = buf; 2691 2692 tev->event = strdup(event); 2693 tev->group = strdup(group); 2694 if (tev->event == NULL || tev->group == NULL) 2695 return -ENOMEM; 2696 2697 /* Add added event name to namelist */ 2698 strlist__add(namelist, event); 2699 return 0; 2700 } 2701 2702 static int __open_probe_file_and_namelist(bool uprobe, 2703 struct strlist **namelist) 2704 { 2705 int fd; 2706 2707 fd = probe_file__open(PF_FL_RW | (uprobe ? PF_FL_UPROBE : 0)); 2708 if (fd < 0) 2709 return fd; 2710 2711 /* Get current event names */ 2712 *namelist = probe_file__get_namelist(fd); 2713 if (!(*namelist)) { 2714 pr_debug("Failed to get current event list.\n"); 2715 close(fd); 2716 return -ENOMEM; 2717 } 2718 return fd; 2719 } 2720 2721 static int __add_probe_trace_events(struct perf_probe_event *pev, 2722 struct probe_trace_event *tevs, 2723 int ntevs, bool allow_suffix) 2724 { 2725 int i, fd[2] = {-1, -1}, up, ret; 2726 struct probe_trace_event *tev = NULL; 2727 struct probe_cache *cache = NULL; 2728 struct strlist *namelist[2] = {NULL, NULL}; 2729 struct nscookie nsc; 2730 2731 up = pev->uprobes ? 1 : 0; 2732 fd[up] = __open_probe_file_and_namelist(up, &namelist[up]); 2733 if (fd[up] < 0) 2734 return fd[up]; 2735 2736 ret = 0; 2737 for (i = 0; i < ntevs; i++) { 2738 tev = &tevs[i]; 2739 up = tev->uprobes ? 1 : 0; 2740 if (fd[up] == -1) { /* Open the kprobe/uprobe_events */ 2741 fd[up] = __open_probe_file_and_namelist(up, 2742 &namelist[up]); 2743 if (fd[up] < 0) 2744 goto close_out; 2745 } 2746 /* Skip if the symbol is out of .text or blacklisted */ 2747 if (!tev->point.symbol && !pev->uprobes) 2748 continue; 2749 2750 /* Set new name for tev (and update namelist) */ 2751 ret = probe_trace_event__set_name(tev, pev, namelist[up], 2752 allow_suffix); 2753 if (ret < 0) 2754 break; 2755 2756 nsinfo__mountns_enter(pev->nsi, &nsc); 2757 ret = probe_file__add_event(fd[up], tev); 2758 nsinfo__mountns_exit(&nsc); 2759 if (ret < 0) 2760 break; 2761 2762 /* 2763 * Probes after the first probe which comes from same 2764 * user input are always allowed to add suffix, because 2765 * there might be several addresses corresponding to 2766 * one code line. 2767 */ 2768 allow_suffix = true; 2769 } 2770 if (ret == -EINVAL && pev->uprobes) 2771 warn_uprobe_event_compat(tev); 2772 if (ret == 0 && probe_conf.cache) { 2773 cache = probe_cache__new(pev->target, pev->nsi); 2774 if (!cache || 2775 probe_cache__add_entry(cache, pev, tevs, ntevs) < 0 || 2776 probe_cache__commit(cache) < 0) 2777 pr_warning("Failed to add event to probe cache\n"); 2778 probe_cache__delete(cache); 2779 } 2780 2781 close_out: 2782 for (up = 0; up < 2; up++) { 2783 strlist__delete(namelist[up]); 2784 if (fd[up] >= 0) 2785 close(fd[up]); 2786 } 2787 return ret; 2788 } 2789 2790 static int find_probe_functions(struct map *map, char *name, 2791 struct symbol **syms) 2792 { 2793 int found = 0; 2794 struct symbol *sym; 2795 struct rb_node *tmp; 2796 const char *norm, *ver; 2797 char *buf = NULL; 2798 bool cut_version = true; 2799 2800 if (map__load(map) < 0) 2801 return 0; 2802 2803 /* If user gives a version, don't cut off the version from symbols */ 2804 if (strchr(name, '@')) 2805 cut_version = false; 2806 2807 map__for_each_symbol(map, sym, tmp) { 2808 norm = arch__normalize_symbol_name(sym->name); 2809 if (!norm) 2810 continue; 2811 2812 if (cut_version) { 2813 /* We don't care about default symbol or not */ 2814 ver = strchr(norm, '@'); 2815 if (ver) { 2816 buf = strndup(norm, ver - norm); 2817 if (!buf) 2818 return -ENOMEM; 2819 norm = buf; 2820 } 2821 } 2822 2823 if (strglobmatch(norm, name)) { 2824 found++; 2825 if (syms && found < probe_conf.max_probes) 2826 syms[found - 1] = sym; 2827 } 2828 if (buf) 2829 zfree(&buf); 2830 } 2831 2832 return found; 2833 } 2834 2835 void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused, 2836 struct probe_trace_event *tev __maybe_unused, 2837 struct map *map __maybe_unused, 2838 struct symbol *sym __maybe_unused) { } 2839 2840 /* 2841 * Find probe function addresses from map. 2842 * Return an error or the number of found probe_trace_event 2843 */ 2844 static int find_probe_trace_events_from_map(struct perf_probe_event *pev, 2845 struct probe_trace_event **tevs) 2846 { 2847 struct map *map = NULL; 2848 struct ref_reloc_sym *reloc_sym = NULL; 2849 struct symbol *sym; 2850 struct symbol **syms = NULL; 2851 struct probe_trace_event *tev; 2852 struct perf_probe_point *pp = &pev->point; 2853 struct probe_trace_point *tp; 2854 int num_matched_functions; 2855 int ret, i, j, skipped = 0; 2856 char *mod_name; 2857 2858 map = get_target_map(pev->target, pev->nsi, pev->uprobes); 2859 if (!map) { 2860 ret = -EINVAL; 2861 goto out; 2862 } 2863 2864 syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes); 2865 if (!syms) { 2866 ret = -ENOMEM; 2867 goto out; 2868 } 2869 2870 /* 2871 * Load matched symbols: Since the different local symbols may have 2872 * same name but different addresses, this lists all the symbols. 2873 */ 2874 num_matched_functions = find_probe_functions(map, pp->function, syms); 2875 if (num_matched_functions <= 0) { 2876 pr_err("Failed to find symbol %s in %s\n", pp->function, 2877 pev->target ? : "kernel"); 2878 ret = -ENOENT; 2879 goto out; 2880 } else if (num_matched_functions > probe_conf.max_probes) { 2881 pr_err("Too many functions matched in %s\n", 2882 pev->target ? : "kernel"); 2883 ret = -E2BIG; 2884 goto out; 2885 } 2886 2887 /* Note that the symbols in the kmodule are not relocated */ 2888 if (!pev->uprobes && !pev->target && 2889 (!pp->retprobe || kretprobe_offset_is_supported())) { 2890 reloc_sym = kernel_get_ref_reloc_sym(); 2891 if (!reloc_sym) { 2892 pr_warning("Relocated base symbol is not found!\n"); 2893 ret = -EINVAL; 2894 goto out; 2895 } 2896 } 2897 2898 /* Setup result trace-probe-events */ 2899 *tevs = zalloc(sizeof(*tev) * num_matched_functions); 2900 if (!*tevs) { 2901 ret = -ENOMEM; 2902 goto out; 2903 } 2904 2905 ret = 0; 2906 2907 for (j = 0; j < num_matched_functions; j++) { 2908 sym = syms[j]; 2909 2910 tev = (*tevs) + ret; 2911 tp = &tev->point; 2912 if (ret == num_matched_functions) { 2913 pr_warning("Too many symbols are listed. Skip it.\n"); 2914 break; 2915 } 2916 ret++; 2917 2918 if (pp->offset > sym->end - sym->start) { 2919 pr_warning("Offset %ld is bigger than the size of %s\n", 2920 pp->offset, sym->name); 2921 ret = -ENOENT; 2922 goto err_out; 2923 } 2924 /* Add one probe point */ 2925 tp->address = map->unmap_ip(map, sym->start) + pp->offset; 2926 2927 /* Check the kprobe (not in module) is within .text */ 2928 if (!pev->uprobes && !pev->target && 2929 kprobe_warn_out_range(sym->name, tp->address)) { 2930 tp->symbol = NULL; /* Skip it */ 2931 skipped++; 2932 } else if (reloc_sym) { 2933 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out); 2934 tp->offset = tp->address - reloc_sym->addr; 2935 } else { 2936 tp->symbol = strdup_or_goto(sym->name, nomem_out); 2937 tp->offset = pp->offset; 2938 } 2939 tp->realname = strdup_or_goto(sym->name, nomem_out); 2940 2941 tp->retprobe = pp->retprobe; 2942 if (pev->target) { 2943 if (pev->uprobes) { 2944 tev->point.module = strdup_or_goto(pev->target, 2945 nomem_out); 2946 } else { 2947 mod_name = find_module_name(pev->target); 2948 tev->point.module = 2949 strdup(mod_name ? mod_name : pev->target); 2950 free(mod_name); 2951 if (!tev->point.module) 2952 goto nomem_out; 2953 } 2954 } 2955 tev->uprobes = pev->uprobes; 2956 tev->nargs = pev->nargs; 2957 if (tev->nargs) { 2958 tev->args = zalloc(sizeof(struct probe_trace_arg) * 2959 tev->nargs); 2960 if (tev->args == NULL) 2961 goto nomem_out; 2962 } 2963 for (i = 0; i < tev->nargs; i++) { 2964 if (pev->args[i].name) 2965 tev->args[i].name = 2966 strdup_or_goto(pev->args[i].name, 2967 nomem_out); 2968 2969 tev->args[i].value = strdup_or_goto(pev->args[i].var, 2970 nomem_out); 2971 if (pev->args[i].type) 2972 tev->args[i].type = 2973 strdup_or_goto(pev->args[i].type, 2974 nomem_out); 2975 } 2976 arch__fix_tev_from_maps(pev, tev, map, sym); 2977 } 2978 if (ret == skipped) { 2979 ret = -ENOENT; 2980 goto err_out; 2981 } 2982 2983 out: 2984 map__put(map); 2985 free(syms); 2986 return ret; 2987 2988 nomem_out: 2989 ret = -ENOMEM; 2990 err_out: 2991 clear_probe_trace_events(*tevs, num_matched_functions); 2992 zfree(tevs); 2993 goto out; 2994 } 2995 2996 static int try_to_find_absolute_address(struct perf_probe_event *pev, 2997 struct probe_trace_event **tevs) 2998 { 2999 struct perf_probe_point *pp = &pev->point; 3000 struct probe_trace_event *tev; 3001 struct probe_trace_point *tp; 3002 int i, err; 3003 3004 if (!(pev->point.function && !strncmp(pev->point.function, "0x", 2))) 3005 return -EINVAL; 3006 if (perf_probe_event_need_dwarf(pev)) 3007 return -EINVAL; 3008 3009 /* 3010 * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at 3011 * absolute address. 3012 * 3013 * Only one tev can be generated by this. 3014 */ 3015 *tevs = zalloc(sizeof(*tev)); 3016 if (!*tevs) 3017 return -ENOMEM; 3018 3019 tev = *tevs; 3020 tp = &tev->point; 3021 3022 /* 3023 * Don't use tp->offset, use address directly, because 3024 * in synthesize_probe_trace_command() address cannot be 3025 * zero. 3026 */ 3027 tp->address = pev->point.abs_address; 3028 tp->retprobe = pp->retprobe; 3029 tev->uprobes = pev->uprobes; 3030 3031 err = -ENOMEM; 3032 /* 3033 * Give it a '0x' leading symbol name. 3034 * In __add_probe_trace_events, a NULL symbol is interpreted as 3035 * invalud. 3036 */ 3037 if (asprintf(&tp->symbol, "0x%lx", tp->address) < 0) 3038 goto errout; 3039 3040 /* For kprobe, check range */ 3041 if ((!tev->uprobes) && 3042 (kprobe_warn_out_range(tev->point.symbol, 3043 tev->point.address))) { 3044 err = -EACCES; 3045 goto errout; 3046 } 3047 3048 if (asprintf(&tp->realname, "abs_%lx", tp->address) < 0) 3049 goto errout; 3050 3051 if (pev->target) { 3052 tp->module = strdup(pev->target); 3053 if (!tp->module) 3054 goto errout; 3055 } 3056 3057 if (tev->group) { 3058 tev->group = strdup(pev->group); 3059 if (!tev->group) 3060 goto errout; 3061 } 3062 3063 if (pev->event) { 3064 tev->event = strdup(pev->event); 3065 if (!tev->event) 3066 goto errout; 3067 } 3068 3069 tev->nargs = pev->nargs; 3070 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs); 3071 if (!tev->args) 3072 goto errout; 3073 3074 for (i = 0; i < tev->nargs; i++) 3075 copy_to_probe_trace_arg(&tev->args[i], &pev->args[i]); 3076 3077 return 1; 3078 3079 errout: 3080 clear_probe_trace_events(*tevs, 1); 3081 *tevs = NULL; 3082 return err; 3083 } 3084 3085 /* Concatinate two arrays */ 3086 static void *memcat(void *a, size_t sz_a, void *b, size_t sz_b) 3087 { 3088 void *ret; 3089 3090 ret = malloc(sz_a + sz_b); 3091 if (ret) { 3092 memcpy(ret, a, sz_a); 3093 memcpy(ret + sz_a, b, sz_b); 3094 } 3095 return ret; 3096 } 3097 3098 static int 3099 concat_probe_trace_events(struct probe_trace_event **tevs, int *ntevs, 3100 struct probe_trace_event **tevs2, int ntevs2) 3101 { 3102 struct probe_trace_event *new_tevs; 3103 int ret = 0; 3104 3105 if (*ntevs == 0) { 3106 *tevs = *tevs2; 3107 *ntevs = ntevs2; 3108 *tevs2 = NULL; 3109 return 0; 3110 } 3111 3112 if (*ntevs + ntevs2 > probe_conf.max_probes) 3113 ret = -E2BIG; 3114 else { 3115 /* Concatinate the array of probe_trace_event */ 3116 new_tevs = memcat(*tevs, (*ntevs) * sizeof(**tevs), 3117 *tevs2, ntevs2 * sizeof(**tevs2)); 3118 if (!new_tevs) 3119 ret = -ENOMEM; 3120 else { 3121 free(*tevs); 3122 *tevs = new_tevs; 3123 *ntevs += ntevs2; 3124 } 3125 } 3126 if (ret < 0) 3127 clear_probe_trace_events(*tevs2, ntevs2); 3128 zfree(tevs2); 3129 3130 return ret; 3131 } 3132 3133 /* 3134 * Try to find probe_trace_event from given probe caches. Return the number 3135 * of cached events found, if an error occurs return the error. 3136 */ 3137 static int find_cached_events(struct perf_probe_event *pev, 3138 struct probe_trace_event **tevs, 3139 const char *target) 3140 { 3141 struct probe_cache *cache; 3142 struct probe_cache_entry *entry; 3143 struct probe_trace_event *tmp_tevs = NULL; 3144 int ntevs = 0; 3145 int ret = 0; 3146 3147 cache = probe_cache__new(target, pev->nsi); 3148 /* Return 0 ("not found") if the target has no probe cache. */ 3149 if (!cache) 3150 return 0; 3151 3152 for_each_probe_cache_entry(entry, cache) { 3153 /* Skip the cache entry which has no name */ 3154 if (!entry->pev.event || !entry->pev.group) 3155 continue; 3156 if ((!pev->group || strglobmatch(entry->pev.group, pev->group)) && 3157 strglobmatch(entry->pev.event, pev->event)) { 3158 ret = probe_cache_entry__get_event(entry, &tmp_tevs); 3159 if (ret > 0) 3160 ret = concat_probe_trace_events(tevs, &ntevs, 3161 &tmp_tevs, ret); 3162 if (ret < 0) 3163 break; 3164 } 3165 } 3166 probe_cache__delete(cache); 3167 if (ret < 0) { 3168 clear_probe_trace_events(*tevs, ntevs); 3169 zfree(tevs); 3170 } else { 3171 ret = ntevs; 3172 if (ntevs > 0 && target && target[0] == '/') 3173 pev->uprobes = true; 3174 } 3175 3176 return ret; 3177 } 3178 3179 /* Try to find probe_trace_event from all probe caches */ 3180 static int find_cached_events_all(struct perf_probe_event *pev, 3181 struct probe_trace_event **tevs) 3182 { 3183 struct probe_trace_event *tmp_tevs = NULL; 3184 struct strlist *bidlist; 3185 struct str_node *nd; 3186 char *pathname; 3187 int ntevs = 0; 3188 int ret; 3189 3190 /* Get the buildid list of all valid caches */ 3191 bidlist = build_id_cache__list_all(true); 3192 if (!bidlist) { 3193 ret = -errno; 3194 pr_debug("Failed to get buildids: %d\n", ret); 3195 return ret; 3196 } 3197 3198 ret = 0; 3199 strlist__for_each_entry(nd, bidlist) { 3200 pathname = build_id_cache__origname(nd->s); 3201 ret = find_cached_events(pev, &tmp_tevs, pathname); 3202 /* In the case of cnt == 0, we just skip it */ 3203 if (ret > 0) 3204 ret = concat_probe_trace_events(tevs, &ntevs, 3205 &tmp_tevs, ret); 3206 free(pathname); 3207 if (ret < 0) 3208 break; 3209 } 3210 strlist__delete(bidlist); 3211 3212 if (ret < 0) { 3213 clear_probe_trace_events(*tevs, ntevs); 3214 zfree(tevs); 3215 } else 3216 ret = ntevs; 3217 3218 return ret; 3219 } 3220 3221 static int find_probe_trace_events_from_cache(struct perf_probe_event *pev, 3222 struct probe_trace_event **tevs) 3223 { 3224 struct probe_cache *cache; 3225 struct probe_cache_entry *entry; 3226 struct probe_trace_event *tev; 3227 struct str_node *node; 3228 int ret, i; 3229 3230 if (pev->sdt) { 3231 /* For SDT/cached events, we use special search functions */ 3232 if (!pev->target) 3233 return find_cached_events_all(pev, tevs); 3234 else 3235 return find_cached_events(pev, tevs, pev->target); 3236 } 3237 cache = probe_cache__new(pev->target, pev->nsi); 3238 if (!cache) 3239 return 0; 3240 3241 entry = probe_cache__find(cache, pev); 3242 if (!entry) { 3243 /* SDT must be in the cache */ 3244 ret = pev->sdt ? -ENOENT : 0; 3245 goto out; 3246 } 3247 3248 ret = strlist__nr_entries(entry->tevlist); 3249 if (ret > probe_conf.max_probes) { 3250 pr_debug("Too many entries matched in the cache of %s\n", 3251 pev->target ? : "kernel"); 3252 ret = -E2BIG; 3253 goto out; 3254 } 3255 3256 *tevs = zalloc(ret * sizeof(*tev)); 3257 if (!*tevs) { 3258 ret = -ENOMEM; 3259 goto out; 3260 } 3261 3262 i = 0; 3263 strlist__for_each_entry(node, entry->tevlist) { 3264 tev = &(*tevs)[i++]; 3265 ret = parse_probe_trace_command(node->s, tev); 3266 if (ret < 0) 3267 goto out; 3268 /* Set the uprobes attribute as same as original */ 3269 tev->uprobes = pev->uprobes; 3270 } 3271 ret = i; 3272 3273 out: 3274 probe_cache__delete(cache); 3275 return ret; 3276 } 3277 3278 static int convert_to_probe_trace_events(struct perf_probe_event *pev, 3279 struct probe_trace_event **tevs) 3280 { 3281 int ret; 3282 3283 if (!pev->group && !pev->sdt) { 3284 /* Set group name if not given */ 3285 if (!pev->uprobes) { 3286 pev->group = strdup(PERFPROBE_GROUP); 3287 ret = pev->group ? 0 : -ENOMEM; 3288 } else 3289 ret = convert_exec_to_group(pev->target, &pev->group); 3290 if (ret != 0) { 3291 pr_warning("Failed to make a group name.\n"); 3292 return ret; 3293 } 3294 } 3295 3296 ret = try_to_find_absolute_address(pev, tevs); 3297 if (ret > 0) 3298 return ret; 3299 3300 /* At first, we need to lookup cache entry */ 3301 ret = find_probe_trace_events_from_cache(pev, tevs); 3302 if (ret > 0 || pev->sdt) /* SDT can be found only in the cache */ 3303 return ret == 0 ? -ENOENT : ret; /* Found in probe cache */ 3304 3305 /* Convert perf_probe_event with debuginfo */ 3306 ret = try_to_find_probe_trace_events(pev, tevs); 3307 if (ret != 0) 3308 return ret; /* Found in debuginfo or got an error */ 3309 3310 return find_probe_trace_events_from_map(pev, tevs); 3311 } 3312 3313 int convert_perf_probe_events(struct perf_probe_event *pevs, int npevs) 3314 { 3315 int i, ret; 3316 3317 /* Loop 1: convert all events */ 3318 for (i = 0; i < npevs; i++) { 3319 /* Init kprobe blacklist if needed */ 3320 if (!pevs[i].uprobes) 3321 kprobe_blacklist__init(); 3322 /* Convert with or without debuginfo */ 3323 ret = convert_to_probe_trace_events(&pevs[i], &pevs[i].tevs); 3324 if (ret < 0) 3325 return ret; 3326 pevs[i].ntevs = ret; 3327 } 3328 /* This just release blacklist only if allocated */ 3329 kprobe_blacklist__release(); 3330 3331 return 0; 3332 } 3333 3334 static int show_probe_trace_event(struct probe_trace_event *tev) 3335 { 3336 char *buf = synthesize_probe_trace_command(tev); 3337 3338 if (!buf) { 3339 pr_debug("Failed to synthesize probe trace event.\n"); 3340 return -EINVAL; 3341 } 3342 3343 /* Showing definition always go stdout */ 3344 printf("%s\n", buf); 3345 free(buf); 3346 3347 return 0; 3348 } 3349 3350 int show_probe_trace_events(struct perf_probe_event *pevs, int npevs) 3351 { 3352 struct strlist *namelist = strlist__new(NULL, NULL); 3353 struct probe_trace_event *tev; 3354 struct perf_probe_event *pev; 3355 int i, j, ret = 0; 3356 3357 if (!namelist) 3358 return -ENOMEM; 3359 3360 for (j = 0; j < npevs && !ret; j++) { 3361 pev = &pevs[j]; 3362 for (i = 0; i < pev->ntevs && !ret; i++) { 3363 tev = &pev->tevs[i]; 3364 /* Skip if the symbol is out of .text or blacklisted */ 3365 if (!tev->point.symbol && !pev->uprobes) 3366 continue; 3367 3368 /* Set new name for tev (and update namelist) */ 3369 ret = probe_trace_event__set_name(tev, pev, 3370 namelist, true); 3371 if (!ret) 3372 ret = show_probe_trace_event(tev); 3373 } 3374 } 3375 strlist__delete(namelist); 3376 3377 return ret; 3378 } 3379 3380 int apply_perf_probe_events(struct perf_probe_event *pevs, int npevs) 3381 { 3382 int i, ret = 0; 3383 3384 /* Loop 2: add all events */ 3385 for (i = 0; i < npevs; i++) { 3386 ret = __add_probe_trace_events(&pevs[i], pevs[i].tevs, 3387 pevs[i].ntevs, 3388 probe_conf.force_add); 3389 if (ret < 0) 3390 break; 3391 } 3392 return ret; 3393 } 3394 3395 void cleanup_perf_probe_events(struct perf_probe_event *pevs, int npevs) 3396 { 3397 int i, j; 3398 struct perf_probe_event *pev; 3399 3400 /* Loop 3: cleanup and free trace events */ 3401 for (i = 0; i < npevs; i++) { 3402 pev = &pevs[i]; 3403 for (j = 0; j < pevs[i].ntevs; j++) 3404 clear_probe_trace_event(&pevs[i].tevs[j]); 3405 zfree(&pevs[i].tevs); 3406 pevs[i].ntevs = 0; 3407 nsinfo__zput(pev->nsi); 3408 clear_perf_probe_event(&pevs[i]); 3409 } 3410 } 3411 3412 int add_perf_probe_events(struct perf_probe_event *pevs, int npevs) 3413 { 3414 int ret; 3415 3416 ret = init_probe_symbol_maps(pevs->uprobes); 3417 if (ret < 0) 3418 return ret; 3419 3420 ret = convert_perf_probe_events(pevs, npevs); 3421 if (ret == 0) 3422 ret = apply_perf_probe_events(pevs, npevs); 3423 3424 cleanup_perf_probe_events(pevs, npevs); 3425 3426 exit_probe_symbol_maps(); 3427 return ret; 3428 } 3429 3430 int del_perf_probe_events(struct strfilter *filter) 3431 { 3432 int ret, ret2, ufd = -1, kfd = -1; 3433 char *str = strfilter__string(filter); 3434 3435 if (!str) 3436 return -EINVAL; 3437 3438 /* Get current event names */ 3439 ret = probe_file__open_both(&kfd, &ufd, PF_FL_RW); 3440 if (ret < 0) 3441 goto out; 3442 3443 ret = probe_file__del_events(kfd, filter); 3444 if (ret < 0 && ret != -ENOENT) 3445 goto error; 3446 3447 ret2 = probe_file__del_events(ufd, filter); 3448 if (ret2 < 0 && ret2 != -ENOENT) { 3449 ret = ret2; 3450 goto error; 3451 } 3452 ret = 0; 3453 3454 error: 3455 if (kfd >= 0) 3456 close(kfd); 3457 if (ufd >= 0) 3458 close(ufd); 3459 out: 3460 free(str); 3461 3462 return ret; 3463 } 3464 3465 int show_available_funcs(const char *target, struct nsinfo *nsi, 3466 struct strfilter *_filter, bool user) 3467 { 3468 struct rb_node *nd; 3469 struct map *map; 3470 int ret; 3471 3472 ret = init_probe_symbol_maps(user); 3473 if (ret < 0) 3474 return ret; 3475 3476 /* Get a symbol map */ 3477 map = get_target_map(target, nsi, user); 3478 if (!map) { 3479 pr_err("Failed to get a map for %s\n", (target) ? : "kernel"); 3480 return -EINVAL; 3481 } 3482 3483 ret = map__load(map); 3484 if (ret) { 3485 if (ret == -2) { 3486 char *str = strfilter__string(_filter); 3487 pr_err("Failed to find symbols matched to \"%s\"\n", 3488 str); 3489 free(str); 3490 } else 3491 pr_err("Failed to load symbols in %s\n", 3492 (target) ? : "kernel"); 3493 goto end; 3494 } 3495 if (!dso__sorted_by_name(map->dso)) 3496 dso__sort_by_name(map->dso); 3497 3498 /* Show all (filtered) symbols */ 3499 setup_pager(); 3500 3501 for (nd = rb_first(&map->dso->symbol_names); nd; nd = rb_next(nd)) { 3502 struct symbol_name_rb_node *pos = rb_entry(nd, struct symbol_name_rb_node, rb_node); 3503 3504 if (strfilter__compare(_filter, pos->sym.name)) 3505 printf("%s\n", pos->sym.name); 3506 } 3507 end: 3508 map__put(map); 3509 exit_probe_symbol_maps(); 3510 3511 return ret; 3512 } 3513 3514 int copy_to_probe_trace_arg(struct probe_trace_arg *tvar, 3515 struct perf_probe_arg *pvar) 3516 { 3517 tvar->value = strdup(pvar->var); 3518 if (tvar->value == NULL) 3519 return -ENOMEM; 3520 if (pvar->type) { 3521 tvar->type = strdup(pvar->type); 3522 if (tvar->type == NULL) 3523 return -ENOMEM; 3524 } 3525 if (pvar->name) { 3526 tvar->name = strdup(pvar->name); 3527 if (tvar->name == NULL) 3528 return -ENOMEM; 3529 } else 3530 tvar->name = NULL; 3531 return 0; 3532 } 3533