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