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