1 // SPDX-License-Identifier: GPL-2.0 2 #include <dirent.h> 3 #include <errno.h> 4 #include <stdlib.h> 5 #include <stdio.h> 6 #include <string.h> 7 #include <linux/capability.h> 8 #include <linux/kernel.h> 9 #include <linux/mman.h> 10 #include <linux/string.h> 11 #include <linux/time64.h> 12 #include <sys/types.h> 13 #include <sys/stat.h> 14 #include <sys/param.h> 15 #include <fcntl.h> 16 #include <unistd.h> 17 #include <inttypes.h> 18 #include "annotate.h" 19 #include "build-id.h" 20 #include "cap.h" 21 #include "cpumap.h" 22 #include "debug.h" 23 #include "demangle-cxx.h" 24 #include "demangle-java.h" 25 #include "demangle-ocaml.h" 26 #include "demangle-rust-v0.h" 27 #include "dso.h" 28 #include "util.h" // lsdir() 29 #include "debug.h" 30 #include "event.h" 31 #include "machine.h" 32 #include "map.h" 33 #include "symbol.h" 34 #include "map_symbol.h" 35 #include "mem-events.h" 36 #include "mem-info.h" 37 #include "symsrc.h" 38 #include "strlist.h" 39 #include "intlist.h" 40 #include "namespaces.h" 41 #include "header.h" 42 #include "path.h" 43 #include <linux/ctype.h> 44 #include <linux/log2.h> 45 #include <linux/zalloc.h> 46 47 #include <elf.h> 48 #include <limits.h> 49 #include <symbol/kallsyms.h> 50 #include <sys/utsname.h> 51 52 static int dso__load_kernel_sym(struct dso *dso, struct map *map); 53 static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map); 54 static bool symbol__is_idle(const char *name); 55 56 int vmlinux_path__nr_entries; 57 char **vmlinux_path; 58 59 struct symbol_conf symbol_conf = { 60 .nanosecs = false, 61 .use_modules = true, 62 .try_vmlinux_path = true, 63 .demangle = true, 64 .demangle_kernel = false, 65 .cumulate_callchain = true, 66 .time_quantum = 100 * NSEC_PER_MSEC, /* 100ms */ 67 .show_hist_headers = true, 68 .symfs = "", 69 .event_group = true, 70 .inline_name = true, 71 .res_sample = 0, 72 }; 73 74 struct map_list_node { 75 struct list_head node; 76 struct map *map; 77 }; 78 79 static struct map_list_node *map_list_node__new(void) 80 { 81 return malloc(sizeof(struct map_list_node)); 82 } 83 84 static enum dso_binary_type binary_type_symtab[] = { 85 DSO_BINARY_TYPE__KALLSYMS, 86 DSO_BINARY_TYPE__GUEST_KALLSYMS, 87 DSO_BINARY_TYPE__JAVA_JIT, 88 DSO_BINARY_TYPE__DEBUGLINK, 89 DSO_BINARY_TYPE__BUILD_ID_CACHE, 90 DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO, 91 DSO_BINARY_TYPE__FEDORA_DEBUGINFO, 92 DSO_BINARY_TYPE__UBUNTU_DEBUGINFO, 93 DSO_BINARY_TYPE__BUILDID_DEBUGINFO, 94 DSO_BINARY_TYPE__GNU_DEBUGDATA, 95 DSO_BINARY_TYPE__SYSTEM_PATH_DSO, 96 DSO_BINARY_TYPE__GUEST_KMODULE, 97 DSO_BINARY_TYPE__GUEST_KMODULE_COMP, 98 DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE, 99 DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP, 100 DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO, 101 DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO, 102 DSO_BINARY_TYPE__NOT_FOUND, 103 }; 104 105 #define DSO_BINARY_TYPE__SYMTAB_CNT ARRAY_SIZE(binary_type_symtab) 106 107 static bool symbol_type__filter(char __symbol_type) 108 { 109 // Since 'U' == undefined and 'u' == unique global symbol, we can't use toupper there 110 char symbol_type = toupper(__symbol_type); 111 return symbol_type == 'T' || symbol_type == 'W' || symbol_type == 'D' || symbol_type == 'B' || 112 __symbol_type == 'u' || __symbol_type == 'l'; 113 } 114 115 static int prefix_underscores_count(const char *str) 116 { 117 const char *tail = str; 118 119 while (*tail == '_') 120 tail++; 121 122 return tail - str; 123 } 124 125 const char * __weak arch__normalize_symbol_name(const char *name) 126 { 127 return name; 128 } 129 130 int __weak arch__compare_symbol_names(const char *namea, const char *nameb) 131 { 132 return strcmp(namea, nameb); 133 } 134 135 int __weak arch__compare_symbol_names_n(const char *namea, const char *nameb, 136 unsigned int n) 137 { 138 return strncmp(namea, nameb, n); 139 } 140 141 int __weak arch__choose_best_symbol(struct symbol *syma, 142 struct symbol *symb __maybe_unused) 143 { 144 /* Avoid "SyS" kernel syscall aliases */ 145 if (strlen(syma->name) >= 3 && !strncmp(syma->name, "SyS", 3)) 146 return SYMBOL_B; 147 if (strlen(syma->name) >= 10 && !strncmp(syma->name, "compat_SyS", 10)) 148 return SYMBOL_B; 149 150 return SYMBOL_A; 151 } 152 153 static int choose_best_symbol(struct symbol *syma, struct symbol *symb) 154 { 155 s64 a; 156 s64 b; 157 size_t na, nb; 158 159 /* Prefer a symbol with non zero length */ 160 a = syma->end - syma->start; 161 b = symb->end - symb->start; 162 if ((b == 0) && (a > 0)) 163 return SYMBOL_A; 164 else if ((a == 0) && (b > 0)) 165 return SYMBOL_B; 166 167 if (syma->type != symb->type) { 168 if (syma->type == STT_NOTYPE) 169 return SYMBOL_B; 170 if (symb->type == STT_NOTYPE) 171 return SYMBOL_A; 172 } 173 174 /* Prefer a non weak symbol over a weak one */ 175 a = syma->binding == STB_WEAK; 176 b = symb->binding == STB_WEAK; 177 if (b && !a) 178 return SYMBOL_A; 179 if (a && !b) 180 return SYMBOL_B; 181 182 /* Prefer a global symbol over a non global one */ 183 a = syma->binding == STB_GLOBAL; 184 b = symb->binding == STB_GLOBAL; 185 if (a && !b) 186 return SYMBOL_A; 187 if (b && !a) 188 return SYMBOL_B; 189 190 /* Prefer a symbol with less underscores */ 191 a = prefix_underscores_count(syma->name); 192 b = prefix_underscores_count(symb->name); 193 if (b > a) 194 return SYMBOL_A; 195 else if (a > b) 196 return SYMBOL_B; 197 198 /* Choose the symbol with the longest name */ 199 na = strlen(syma->name); 200 nb = strlen(symb->name); 201 if (na > nb) 202 return SYMBOL_A; 203 else if (na < nb) 204 return SYMBOL_B; 205 206 return arch__choose_best_symbol(syma, symb); 207 } 208 209 void symbols__fixup_duplicate(struct rb_root_cached *symbols) 210 { 211 struct rb_node *nd; 212 struct symbol *curr, *next; 213 214 if (symbol_conf.allow_aliases) 215 return; 216 217 nd = rb_first_cached(symbols); 218 219 while (nd) { 220 curr = rb_entry(nd, struct symbol, rb_node); 221 again: 222 nd = rb_next(&curr->rb_node); 223 if (!nd) 224 break; 225 226 next = rb_entry(nd, struct symbol, rb_node); 227 if (curr->start != next->start) 228 continue; 229 230 if (choose_best_symbol(curr, next) == SYMBOL_A) { 231 if (next->type == STT_GNU_IFUNC) 232 curr->ifunc_alias = true; 233 rb_erase_cached(&next->rb_node, symbols); 234 symbol__delete(next); 235 goto again; 236 } else { 237 if (curr->type == STT_GNU_IFUNC) 238 next->ifunc_alias = true; 239 nd = rb_next(&curr->rb_node); 240 rb_erase_cached(&curr->rb_node, symbols); 241 symbol__delete(curr); 242 } 243 } 244 } 245 246 /* Update zero-sized symbols using the address of the next symbol */ 247 void symbols__fixup_end(struct rb_root_cached *symbols, bool is_kallsyms) 248 { 249 struct rb_node *nd, *prevnd = rb_first_cached(symbols); 250 struct symbol *curr, *prev; 251 252 if (prevnd == NULL) 253 return; 254 255 curr = rb_entry(prevnd, struct symbol, rb_node); 256 257 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) { 258 prev = curr; 259 curr = rb_entry(nd, struct symbol, rb_node); 260 261 /* 262 * On some architecture kernel text segment start is located at 263 * some low memory address, while modules are located at high 264 * memory addresses (or vice versa). The gap between end of 265 * kernel text segment and beginning of first module's text 266 * segment is very big. Therefore do not fill this gap and do 267 * not assign it to the kernel dso map (kallsyms). 268 * 269 * Also BPF code can be allocated separately from text segments 270 * and modules. So the last entry in a module should not fill 271 * the gap too. 272 * 273 * In kallsyms, it determines module symbols using '[' character 274 * like in: 275 * ffffffffc1937000 T hdmi_driver_init [snd_hda_codec_hdmi] 276 */ 277 if (prev->end == prev->start) { 278 const char *prev_mod; 279 const char *curr_mod; 280 281 if (!is_kallsyms) { 282 prev->end = curr->start; 283 continue; 284 } 285 286 prev_mod = strchr(prev->name, '['); 287 curr_mod = strchr(curr->name, '['); 288 289 /* Last kernel/module symbol mapped to end of page */ 290 if (!prev_mod != !curr_mod) 291 prev->end = roundup(prev->end + 4096, 4096); 292 /* Last symbol in the previous module */ 293 else if (prev_mod && strcmp(prev_mod, curr_mod)) 294 prev->end = roundup(prev->end + 4096, 4096); 295 else 296 prev->end = curr->start; 297 298 pr_debug4("%s sym:%s end:%#" PRIx64 "\n", 299 __func__, prev->name, prev->end); 300 } 301 } 302 303 /* Last entry */ 304 if (curr->end == curr->start) 305 curr->end = roundup(curr->start, 4096) + 4096; 306 } 307 308 struct symbol *symbol__new(u64 start, u64 len, u8 binding, u8 type, const char *name) 309 { 310 size_t namelen = strlen(name) + 1; 311 struct symbol *sym = calloc(1, (symbol_conf.priv_size + 312 sizeof(*sym) + namelen)); 313 if (sym == NULL) 314 return NULL; 315 316 if (symbol_conf.priv_size) { 317 if (symbol_conf.init_annotation) { 318 struct annotation *notes = (void *)sym; 319 annotation__init(notes); 320 } 321 sym = ((void *)sym) + symbol_conf.priv_size; 322 } 323 324 sym->start = start; 325 sym->end = len ? start + len : start; 326 sym->type = type; 327 sym->binding = binding; 328 sym->namelen = namelen - 1; 329 330 pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n", 331 __func__, name, start, sym->end); 332 memcpy(sym->name, name, namelen); 333 334 return sym; 335 } 336 337 void symbol__delete(struct symbol *sym) 338 { 339 if (symbol_conf.priv_size) { 340 if (symbol_conf.init_annotation) { 341 struct annotation *notes = symbol__annotation(sym); 342 343 annotation__exit(notes); 344 } 345 } 346 free(((void *)sym) - symbol_conf.priv_size); 347 } 348 349 void symbols__delete(struct rb_root_cached *symbols) 350 { 351 struct symbol *pos; 352 struct rb_node *next = rb_first_cached(symbols); 353 354 while (next) { 355 pos = rb_entry(next, struct symbol, rb_node); 356 next = rb_next(&pos->rb_node); 357 rb_erase_cached(&pos->rb_node, symbols); 358 symbol__delete(pos); 359 } 360 } 361 362 void __symbols__insert(struct rb_root_cached *symbols, 363 struct symbol *sym, bool kernel) 364 { 365 struct rb_node **p = &symbols->rb_root.rb_node; 366 struct rb_node *parent = NULL; 367 const u64 ip = sym->start; 368 struct symbol *s; 369 bool leftmost = true; 370 371 if (kernel) { 372 const char *name = sym->name; 373 /* 374 * ppc64 uses function descriptors and appends a '.' to the 375 * start of every instruction address. Remove it. 376 */ 377 if (name[0] == '.') 378 name++; 379 sym->idle = symbol__is_idle(name); 380 } 381 382 while (*p != NULL) { 383 parent = *p; 384 s = rb_entry(parent, struct symbol, rb_node); 385 if (ip < s->start) 386 p = &(*p)->rb_left; 387 else { 388 p = &(*p)->rb_right; 389 leftmost = false; 390 } 391 } 392 rb_link_node(&sym->rb_node, parent, p); 393 rb_insert_color_cached(&sym->rb_node, symbols, leftmost); 394 } 395 396 void symbols__insert(struct rb_root_cached *symbols, struct symbol *sym) 397 { 398 __symbols__insert(symbols, sym, false); 399 } 400 401 static struct symbol *symbols__find(struct rb_root_cached *symbols, u64 ip) 402 { 403 struct rb_node *n; 404 405 if (symbols == NULL) 406 return NULL; 407 408 n = symbols->rb_root.rb_node; 409 410 while (n) { 411 struct symbol *s = rb_entry(n, struct symbol, rb_node); 412 413 if (ip < s->start) 414 n = n->rb_left; 415 else if (ip > s->end || (ip == s->end && ip != s->start)) 416 n = n->rb_right; 417 else 418 return s; 419 } 420 421 return NULL; 422 } 423 424 static struct symbol *symbols__first(struct rb_root_cached *symbols) 425 { 426 struct rb_node *n = rb_first_cached(symbols); 427 428 if (n) 429 return rb_entry(n, struct symbol, rb_node); 430 431 return NULL; 432 } 433 434 static struct symbol *symbols__last(struct rb_root_cached *symbols) 435 { 436 struct rb_node *n = rb_last(&symbols->rb_root); 437 438 if (n) 439 return rb_entry(n, struct symbol, rb_node); 440 441 return NULL; 442 } 443 444 static struct symbol *symbols__next(struct symbol *sym) 445 { 446 struct rb_node *n = rb_next(&sym->rb_node); 447 448 if (n) 449 return rb_entry(n, struct symbol, rb_node); 450 451 return NULL; 452 } 453 454 static int symbols__sort_name_cmp(const void *vlhs, const void *vrhs) 455 { 456 const struct symbol *lhs = *((const struct symbol **)vlhs); 457 const struct symbol *rhs = *((const struct symbol **)vrhs); 458 459 return strcmp(lhs->name, rhs->name); 460 } 461 462 static struct symbol **symbols__sort_by_name(struct rb_root_cached *source, size_t *len) 463 { 464 struct rb_node *nd; 465 struct symbol **result; 466 size_t i = 0, size = 0; 467 468 for (nd = rb_first_cached(source); nd; nd = rb_next(nd)) 469 size++; 470 471 result = malloc(sizeof(*result) * size); 472 if (!result) 473 return NULL; 474 475 for (nd = rb_first_cached(source); nd; nd = rb_next(nd)) { 476 struct symbol *pos = rb_entry(nd, struct symbol, rb_node); 477 478 result[i++] = pos; 479 } 480 qsort(result, size, sizeof(*result), symbols__sort_name_cmp); 481 *len = size; 482 return result; 483 } 484 485 int symbol__match_symbol_name(const char *name, const char *str, 486 enum symbol_tag_include includes) 487 { 488 const char *versioning; 489 490 if (includes == SYMBOL_TAG_INCLUDE__DEFAULT_ONLY && 491 (versioning = strstr(name, "@@"))) { 492 int len = strlen(str); 493 494 if (len < versioning - name) 495 len = versioning - name; 496 497 return arch__compare_symbol_names_n(name, str, len); 498 } else 499 return arch__compare_symbol_names(name, str); 500 } 501 502 static struct symbol *symbols__find_by_name(struct symbol *symbols[], 503 size_t symbols_len, 504 const char *name, 505 enum symbol_tag_include includes, 506 size_t *found_idx) 507 { 508 size_t i, lower = 0, upper = symbols_len; 509 struct symbol *s = NULL; 510 511 if (found_idx) 512 *found_idx = SIZE_MAX; 513 514 if (!symbols_len) 515 return NULL; 516 517 while (lower < upper) { 518 int cmp; 519 520 i = (lower + upper) / 2; 521 cmp = symbol__match_symbol_name(symbols[i]->name, name, includes); 522 523 if (cmp > 0) 524 upper = i; 525 else if (cmp < 0) 526 lower = i + 1; 527 else { 528 if (found_idx) 529 *found_idx = i; 530 s = symbols[i]; 531 break; 532 } 533 } 534 if (s && includes != SYMBOL_TAG_INCLUDE__DEFAULT_ONLY) { 535 /* return first symbol that has same name (if any) */ 536 for (; i > 0; i--) { 537 struct symbol *tmp = symbols[i - 1]; 538 539 if (!arch__compare_symbol_names(tmp->name, s->name)) { 540 if (found_idx) 541 *found_idx = i - 1; 542 s = tmp; 543 } else 544 break; 545 } 546 } 547 assert(!found_idx || !s || s == symbols[*found_idx]); 548 return s; 549 } 550 551 void dso__reset_find_symbol_cache(struct dso *dso) 552 { 553 dso__set_last_find_result_addr(dso, 0); 554 dso__set_last_find_result_symbol(dso, NULL); 555 } 556 557 void dso__insert_symbol(struct dso *dso, struct symbol *sym) 558 { 559 __symbols__insert(dso__symbols(dso), sym, dso__kernel(dso)); 560 561 /* update the symbol cache if necessary */ 562 if (dso__last_find_result_addr(dso) >= sym->start && 563 (dso__last_find_result_addr(dso) < sym->end || 564 sym->start == sym->end)) { 565 dso__set_last_find_result_symbol(dso, sym); 566 } 567 } 568 569 void dso__delete_symbol(struct dso *dso, struct symbol *sym) 570 { 571 rb_erase_cached(&sym->rb_node, dso__symbols(dso)); 572 symbol__delete(sym); 573 dso__reset_find_symbol_cache(dso); 574 } 575 576 struct symbol *dso__find_symbol(struct dso *dso, u64 addr) 577 { 578 if (dso__last_find_result_addr(dso) != addr || dso__last_find_result_symbol(dso) == NULL) { 579 dso__set_last_find_result_addr(dso, addr); 580 dso__set_last_find_result_symbol(dso, symbols__find(dso__symbols(dso), addr)); 581 } 582 583 return dso__last_find_result_symbol(dso); 584 } 585 586 struct symbol *dso__find_symbol_nocache(struct dso *dso, u64 addr) 587 { 588 return symbols__find(dso__symbols(dso), addr); 589 } 590 591 struct symbol *dso__first_symbol(struct dso *dso) 592 { 593 return symbols__first(dso__symbols(dso)); 594 } 595 596 struct symbol *dso__last_symbol(struct dso *dso) 597 { 598 return symbols__last(dso__symbols(dso)); 599 } 600 601 struct symbol *dso__next_symbol(struct symbol *sym) 602 { 603 return symbols__next(sym); 604 } 605 606 struct symbol *dso__next_symbol_by_name(struct dso *dso, size_t *idx) 607 { 608 if (*idx + 1 >= dso__symbol_names_len(dso)) 609 return NULL; 610 611 ++*idx; 612 return dso__symbol_names(dso)[*idx]; 613 } 614 615 /* 616 * Returns first symbol that matched with @name. 617 */ 618 struct symbol *dso__find_symbol_by_name(struct dso *dso, const char *name, size_t *idx) 619 { 620 struct symbol *s = symbols__find_by_name(dso__symbol_names(dso), 621 dso__symbol_names_len(dso), 622 name, SYMBOL_TAG_INCLUDE__NONE, idx); 623 if (!s) { 624 s = symbols__find_by_name(dso__symbol_names(dso), dso__symbol_names_len(dso), 625 name, SYMBOL_TAG_INCLUDE__DEFAULT_ONLY, idx); 626 } 627 return s; 628 } 629 630 void dso__sort_by_name(struct dso *dso) 631 { 632 mutex_lock(dso__lock(dso)); 633 if (!dso__sorted_by_name(dso)) { 634 size_t len; 635 636 dso__set_symbol_names(dso, symbols__sort_by_name(dso__symbols(dso), &len)); 637 if (dso__symbol_names(dso)) { 638 dso__set_symbol_names_len(dso, len); 639 dso__set_sorted_by_name(dso); 640 } 641 } 642 mutex_unlock(dso__lock(dso)); 643 } 644 645 /* 646 * While we find nice hex chars, build a long_val. 647 * Return number of chars processed. 648 */ 649 static int hex2u64(const char *ptr, u64 *long_val) 650 { 651 char *p; 652 653 *long_val = strtoull(ptr, &p, 16); 654 655 return p - ptr; 656 } 657 658 659 int modules__parse(const char *filename, void *arg, 660 int (*process_module)(void *arg, const char *name, 661 u64 start, u64 size)) 662 { 663 char *line = NULL; 664 size_t n; 665 FILE *file; 666 int err = 0; 667 668 file = fopen(filename, "r"); 669 if (file == NULL) 670 return -1; 671 672 while (1) { 673 char name[PATH_MAX]; 674 u64 start, size; 675 char *sep, *endptr; 676 ssize_t line_len; 677 678 line_len = getline(&line, &n, file); 679 if (line_len < 0) { 680 if (feof(file)) 681 break; 682 err = -1; 683 goto out; 684 } 685 686 if (!line) { 687 err = -1; 688 goto out; 689 } 690 691 line[--line_len] = '\0'; /* \n */ 692 693 sep = strrchr(line, 'x'); 694 if (sep == NULL) 695 continue; 696 697 hex2u64(sep + 1, &start); 698 699 sep = strchr(line, ' '); 700 if (sep == NULL) 701 continue; 702 703 *sep = '\0'; 704 705 scnprintf(name, sizeof(name), "[%s]", line); 706 707 size = strtoul(sep + 1, &endptr, 0); 708 if (*endptr != ' ' && *endptr != '\t') 709 continue; 710 711 err = process_module(arg, name, start, size); 712 if (err) 713 break; 714 } 715 out: 716 free(line); 717 fclose(file); 718 return err; 719 } 720 721 /* 722 * These are symbols in the kernel image, so make sure that 723 * sym is from a kernel DSO. 724 */ 725 static bool symbol__is_idle(const char *name) 726 { 727 const char * const idle_symbols[] = { 728 "acpi_idle_do_entry", 729 "acpi_processor_ffh_cstate_enter", 730 "arch_cpu_idle", 731 "cpu_idle", 732 "cpu_startup_entry", 733 "idle_cpu", 734 "intel_idle", 735 "intel_idle_ibrs", 736 "default_idle", 737 "native_safe_halt", 738 "enter_idle", 739 "exit_idle", 740 "mwait_idle", 741 "mwait_idle_with_hints", 742 "mwait_idle_with_hints.constprop.0", 743 "poll_idle", 744 "ppc64_runlatch_off", 745 "pseries_dedicated_idle_sleep", 746 "psw_idle", 747 "psw_idle_exit", 748 NULL 749 }; 750 int i; 751 static struct strlist *idle_symbols_list; 752 753 if (idle_symbols_list) 754 return strlist__has_entry(idle_symbols_list, name); 755 756 idle_symbols_list = strlist__new(NULL, NULL); 757 758 for (i = 0; idle_symbols[i]; i++) 759 strlist__add(idle_symbols_list, idle_symbols[i]); 760 761 return strlist__has_entry(idle_symbols_list, name); 762 } 763 764 static int map__process_kallsym_symbol(void *arg, const char *name, 765 char type, u64 start) 766 { 767 struct symbol *sym; 768 struct dso *dso = arg; 769 struct rb_root_cached *root = dso__symbols(dso); 770 771 if (!symbol_type__filter(type)) 772 return 0; 773 774 /* Ignore local symbols for ARM modules */ 775 if (name[0] == '$') 776 return 0; 777 778 /* 779 * module symbols are not sorted so we add all 780 * symbols, setting length to 0, and rely on 781 * symbols__fixup_end() to fix it up. 782 */ 783 sym = symbol__new(start, 0, kallsyms2elf_binding(type), kallsyms2elf_type(type), name); 784 if (sym == NULL) 785 return -ENOMEM; 786 /* 787 * We will pass the symbols to the filter later, in 788 * map__split_kallsyms, when we have split the maps per module 789 */ 790 __symbols__insert(root, sym, !strchr(name, '[')); 791 792 return 0; 793 } 794 795 /* 796 * Loads the function entries in /proc/kallsyms into kernel_map->dso, 797 * so that we can in the next step set the symbol ->end address and then 798 * call kernel_maps__split_kallsyms. 799 */ 800 static int dso__load_all_kallsyms(struct dso *dso, const char *filename) 801 { 802 return kallsyms__parse(filename, dso, map__process_kallsym_symbol); 803 } 804 805 static int maps__split_kallsyms_for_kcore(struct maps *kmaps, struct dso *dso) 806 { 807 struct symbol *pos; 808 int count = 0; 809 struct rb_root_cached *root = dso__symbols(dso); 810 struct rb_root_cached old_root = *root; 811 struct rb_node *next = rb_first_cached(root); 812 813 if (!kmaps) 814 return -1; 815 816 *root = RB_ROOT_CACHED; 817 818 while (next) { 819 struct map *curr_map; 820 struct dso *curr_map_dso; 821 char *module; 822 823 pos = rb_entry(next, struct symbol, rb_node); 824 next = rb_next(&pos->rb_node); 825 826 rb_erase_cached(&pos->rb_node, &old_root); 827 RB_CLEAR_NODE(&pos->rb_node); 828 module = strchr(pos->name, '\t'); 829 if (module) 830 *module = '\0'; 831 832 curr_map = maps__find(kmaps, pos->start); 833 834 if (!curr_map) { 835 symbol__delete(pos); 836 continue; 837 } 838 curr_map_dso = map__dso(curr_map); 839 pos->start -= map__start(curr_map) - map__pgoff(curr_map); 840 if (pos->end > map__end(curr_map)) 841 pos->end = map__end(curr_map); 842 if (pos->end) 843 pos->end -= map__start(curr_map) - map__pgoff(curr_map); 844 symbols__insert(dso__symbols(curr_map_dso), pos); 845 ++count; 846 map__put(curr_map); 847 } 848 849 /* Symbols have been adjusted */ 850 dso__set_adjust_symbols(dso, true); 851 852 return count; 853 } 854 855 /* 856 * Split the symbols into maps, making sure there are no overlaps, i.e. the 857 * kernel range is broken in several maps, named [kernel].N, as we don't have 858 * the original ELF section names vmlinux have. 859 */ 860 static int maps__split_kallsyms(struct maps *kmaps, struct dso *dso, u64 delta, 861 struct map *initial_map) 862 { 863 struct machine *machine; 864 struct map *curr_map = map__get(initial_map); 865 struct symbol *pos; 866 int count = 0, moved = 0; 867 struct rb_root_cached *root = dso__symbols(dso); 868 struct rb_node *next = rb_first_cached(root); 869 int kernel_range = 0; 870 bool x86_64; 871 872 if (!kmaps) 873 return -1; 874 875 machine = maps__machine(kmaps); 876 877 x86_64 = machine__is(machine, "x86_64"); 878 879 while (next) { 880 char *module; 881 882 pos = rb_entry(next, struct symbol, rb_node); 883 next = rb_next(&pos->rb_node); 884 885 module = strchr(pos->name, '\t'); 886 if (module) { 887 struct dso *curr_map_dso; 888 889 if (!symbol_conf.use_modules) 890 goto discard_symbol; 891 892 *module++ = '\0'; 893 curr_map_dso = map__dso(curr_map); 894 if (strcmp(dso__short_name(curr_map_dso), module)) { 895 if (!RC_CHK_EQUAL(curr_map, initial_map) && 896 dso__kernel(dso) == DSO_SPACE__KERNEL_GUEST && 897 machine__is_default_guest(machine)) { 898 /* 899 * We assume all symbols of a module are 900 * continuous in * kallsyms, so curr_map 901 * points to a module and all its 902 * symbols are in its kmap. Mark it as 903 * loaded. 904 */ 905 dso__set_loaded(curr_map_dso); 906 } 907 908 map__zput(curr_map); 909 curr_map = maps__find_by_name(kmaps, module); 910 if (curr_map == NULL) { 911 pr_debug("%s/proc/{kallsyms,modules} " 912 "inconsistency while looking " 913 "for \"%s\" module!\n", 914 machine->root_dir, module); 915 curr_map = map__get(initial_map); 916 goto discard_symbol; 917 } 918 curr_map_dso = map__dso(curr_map); 919 if (dso__loaded(curr_map_dso) && 920 !machine__is_default_guest(machine)) 921 goto discard_symbol; 922 } 923 /* 924 * So that we look just like we get from .ko files, 925 * i.e. not prelinked, relative to initial_map->start. 926 */ 927 pos->start = map__map_ip(curr_map, pos->start); 928 pos->end = map__map_ip(curr_map, pos->end); 929 } else if (x86_64 && is_entry_trampoline(pos->name)) { 930 /* 931 * These symbols are not needed anymore since the 932 * trampoline maps refer to the text section and it's 933 * symbols instead. Avoid having to deal with 934 * relocations, and the assumption that the first symbol 935 * is the start of kernel text, by simply removing the 936 * symbols at this point. 937 */ 938 goto discard_symbol; 939 } else if (!RC_CHK_EQUAL(curr_map, initial_map)) { 940 char dso_name[PATH_MAX]; 941 struct dso *ndso; 942 943 if (delta) { 944 /* Kernel was relocated at boot time */ 945 pos->start -= delta; 946 pos->end -= delta; 947 } 948 949 if (count == 0) { 950 map__zput(curr_map); 951 curr_map = map__get(initial_map); 952 goto add_symbol; 953 } 954 955 if (dso__kernel(dso) == DSO_SPACE__KERNEL_GUEST) 956 snprintf(dso_name, sizeof(dso_name), 957 "[guest.kernel].%d", 958 kernel_range++); 959 else 960 snprintf(dso_name, sizeof(dso_name), 961 "[kernel].%d", 962 kernel_range++); 963 964 ndso = dso__new(dso_name); 965 map__zput(curr_map); 966 if (ndso == NULL) 967 return -1; 968 969 dso__set_kernel(ndso, dso__kernel(dso)); 970 971 curr_map = map__new2(pos->start, ndso); 972 if (curr_map == NULL) { 973 dso__put(ndso); 974 return -1; 975 } 976 977 map__set_mapping_type(curr_map, MAPPING_TYPE__IDENTITY); 978 if (maps__insert(kmaps, curr_map)) { 979 map__zput(curr_map); 980 dso__put(ndso); 981 return -1; 982 } 983 ++kernel_range; 984 } else if (delta) { 985 /* Kernel was relocated at boot time */ 986 pos->start -= delta; 987 pos->end -= delta; 988 } 989 add_symbol: 990 if (!RC_CHK_EQUAL(curr_map, initial_map)) { 991 struct dso *curr_map_dso = map__dso(curr_map); 992 993 rb_erase_cached(&pos->rb_node, root); 994 symbols__insert(dso__symbols(curr_map_dso), pos); 995 ++moved; 996 } else 997 ++count; 998 999 continue; 1000 discard_symbol: 1001 rb_erase_cached(&pos->rb_node, root); 1002 symbol__delete(pos); 1003 } 1004 1005 if (!RC_CHK_EQUAL(curr_map, initial_map) && 1006 dso__kernel(dso) == DSO_SPACE__KERNEL_GUEST && 1007 machine__is_default_guest(maps__machine(kmaps))) { 1008 dso__set_loaded(map__dso(curr_map)); 1009 } 1010 map__put(curr_map); 1011 return count + moved; 1012 } 1013 1014 bool symbol__restricted_filename(const char *filename, 1015 const char *restricted_filename) 1016 { 1017 bool restricted = false; 1018 1019 if (symbol_conf.kptr_restrict) { 1020 char *r = realpath(filename, NULL); 1021 1022 if (r != NULL) { 1023 restricted = strcmp(r, restricted_filename) == 0; 1024 free(r); 1025 return restricted; 1026 } 1027 } 1028 1029 return restricted; 1030 } 1031 1032 struct module_info { 1033 struct rb_node rb_node; 1034 char *name; 1035 u64 start; 1036 }; 1037 1038 static void add_module(struct module_info *mi, struct rb_root *modules) 1039 { 1040 struct rb_node **p = &modules->rb_node; 1041 struct rb_node *parent = NULL; 1042 struct module_info *m; 1043 1044 while (*p != NULL) { 1045 parent = *p; 1046 m = rb_entry(parent, struct module_info, rb_node); 1047 if (strcmp(mi->name, m->name) < 0) 1048 p = &(*p)->rb_left; 1049 else 1050 p = &(*p)->rb_right; 1051 } 1052 rb_link_node(&mi->rb_node, parent, p); 1053 rb_insert_color(&mi->rb_node, modules); 1054 } 1055 1056 static void delete_modules(struct rb_root *modules) 1057 { 1058 struct module_info *mi; 1059 struct rb_node *next = rb_first(modules); 1060 1061 while (next) { 1062 mi = rb_entry(next, struct module_info, rb_node); 1063 next = rb_next(&mi->rb_node); 1064 rb_erase(&mi->rb_node, modules); 1065 zfree(&mi->name); 1066 free(mi); 1067 } 1068 } 1069 1070 static struct module_info *find_module(const char *name, 1071 struct rb_root *modules) 1072 { 1073 struct rb_node *n = modules->rb_node; 1074 1075 while (n) { 1076 struct module_info *m; 1077 int cmp; 1078 1079 m = rb_entry(n, struct module_info, rb_node); 1080 cmp = strcmp(name, m->name); 1081 if (cmp < 0) 1082 n = n->rb_left; 1083 else if (cmp > 0) 1084 n = n->rb_right; 1085 else 1086 return m; 1087 } 1088 1089 return NULL; 1090 } 1091 1092 static int __read_proc_modules(void *arg, const char *name, u64 start, 1093 u64 size __maybe_unused) 1094 { 1095 struct rb_root *modules = arg; 1096 struct module_info *mi; 1097 1098 mi = zalloc(sizeof(struct module_info)); 1099 if (!mi) 1100 return -ENOMEM; 1101 1102 mi->name = strdup(name); 1103 mi->start = start; 1104 1105 if (!mi->name) { 1106 free(mi); 1107 return -ENOMEM; 1108 } 1109 1110 add_module(mi, modules); 1111 1112 return 0; 1113 } 1114 1115 static int read_proc_modules(const char *filename, struct rb_root *modules) 1116 { 1117 if (symbol__restricted_filename(filename, "/proc/modules")) 1118 return -1; 1119 1120 if (modules__parse(filename, modules, __read_proc_modules)) { 1121 delete_modules(modules); 1122 return -1; 1123 } 1124 1125 return 0; 1126 } 1127 1128 int compare_proc_modules(const char *from, const char *to) 1129 { 1130 struct rb_root from_modules = RB_ROOT; 1131 struct rb_root to_modules = RB_ROOT; 1132 struct rb_node *from_node, *to_node; 1133 struct module_info *from_m, *to_m; 1134 int ret = -1; 1135 1136 if (read_proc_modules(from, &from_modules)) 1137 return -1; 1138 1139 if (read_proc_modules(to, &to_modules)) 1140 goto out_delete_from; 1141 1142 from_node = rb_first(&from_modules); 1143 to_node = rb_first(&to_modules); 1144 while (from_node) { 1145 if (!to_node) 1146 break; 1147 1148 from_m = rb_entry(from_node, struct module_info, rb_node); 1149 to_m = rb_entry(to_node, struct module_info, rb_node); 1150 1151 if (from_m->start != to_m->start || 1152 strcmp(from_m->name, to_m->name)) 1153 break; 1154 1155 from_node = rb_next(from_node); 1156 to_node = rb_next(to_node); 1157 } 1158 1159 if (!from_node && !to_node) 1160 ret = 0; 1161 1162 delete_modules(&to_modules); 1163 out_delete_from: 1164 delete_modules(&from_modules); 1165 1166 return ret; 1167 } 1168 1169 static int do_validate_kcore_modules_cb(struct map *old_map, void *data) 1170 { 1171 struct rb_root *modules = data; 1172 struct module_info *mi; 1173 struct dso *dso; 1174 1175 if (!__map__is_kmodule(old_map)) 1176 return 0; 1177 1178 dso = map__dso(old_map); 1179 /* Module must be in memory at the same address */ 1180 mi = find_module(dso__short_name(dso), modules); 1181 if (!mi || mi->start != map__start(old_map)) 1182 return -EINVAL; 1183 1184 return 0; 1185 } 1186 1187 static int do_validate_kcore_modules(const char *filename, struct maps *kmaps) 1188 { 1189 struct rb_root modules = RB_ROOT; 1190 int err; 1191 1192 err = read_proc_modules(filename, &modules); 1193 if (err) 1194 return err; 1195 1196 err = maps__for_each_map(kmaps, do_validate_kcore_modules_cb, &modules); 1197 1198 delete_modules(&modules); 1199 return err; 1200 } 1201 1202 /* 1203 * If kallsyms is referenced by name then we look for filename in the same 1204 * directory. 1205 */ 1206 static bool filename_from_kallsyms_filename(char *filename, 1207 const char *base_name, 1208 const char *kallsyms_filename) 1209 { 1210 char *name; 1211 1212 strcpy(filename, kallsyms_filename); 1213 name = strrchr(filename, '/'); 1214 if (!name) 1215 return false; 1216 1217 name += 1; 1218 1219 if (!strcmp(name, "kallsyms")) { 1220 strcpy(name, base_name); 1221 return true; 1222 } 1223 1224 return false; 1225 } 1226 1227 static int validate_kcore_modules(const char *kallsyms_filename, 1228 struct map *map) 1229 { 1230 struct maps *kmaps = map__kmaps(map); 1231 char modules_filename[PATH_MAX]; 1232 1233 if (!kmaps) 1234 return -EINVAL; 1235 1236 if (!filename_from_kallsyms_filename(modules_filename, "modules", 1237 kallsyms_filename)) 1238 return -EINVAL; 1239 1240 if (do_validate_kcore_modules(modules_filename, kmaps)) 1241 return -EINVAL; 1242 1243 return 0; 1244 } 1245 1246 static int validate_kcore_addresses(const char *kallsyms_filename, 1247 struct map *map) 1248 { 1249 struct kmap *kmap = map__kmap(map); 1250 1251 if (!kmap) 1252 return -EINVAL; 1253 1254 if (kmap->ref_reloc_sym && kmap->ref_reloc_sym->name) { 1255 u64 start; 1256 1257 if (kallsyms__get_function_start(kallsyms_filename, 1258 kmap->ref_reloc_sym->name, &start)) 1259 return -ENOENT; 1260 if (start != kmap->ref_reloc_sym->addr) 1261 return -EINVAL; 1262 } 1263 1264 return validate_kcore_modules(kallsyms_filename, map); 1265 } 1266 1267 struct kcore_mapfn_data { 1268 struct dso *dso; 1269 struct list_head maps; 1270 }; 1271 1272 static int kcore_mapfn(u64 start, u64 len, u64 pgoff, void *data) 1273 { 1274 struct kcore_mapfn_data *md = data; 1275 struct map_list_node *list_node = map_list_node__new(); 1276 1277 if (!list_node) 1278 return -ENOMEM; 1279 1280 list_node->map = map__new2(start, md->dso); 1281 if (!list_node->map) { 1282 free(list_node); 1283 return -ENOMEM; 1284 } 1285 1286 map__set_end(list_node->map, map__start(list_node->map) + len); 1287 map__set_pgoff(list_node->map, pgoff); 1288 1289 list_add(&list_node->node, &md->maps); 1290 1291 return 0; 1292 } 1293 1294 static bool remove_old_maps(struct map *map, void *data) 1295 { 1296 const struct map *map_to_save = data; 1297 1298 /* 1299 * We need to preserve eBPF maps even if they are covered by kcore, 1300 * because we need to access eBPF dso for source data. 1301 */ 1302 return !RC_CHK_EQUAL(map, map_to_save) && !__map__is_bpf_prog(map); 1303 } 1304 1305 static int dso__load_kcore(struct dso *dso, struct map *map, 1306 const char *kallsyms_filename) 1307 { 1308 struct maps *kmaps = map__kmaps(map); 1309 struct kcore_mapfn_data md; 1310 struct map *map_ref, *replacement_map = NULL; 1311 struct machine *machine; 1312 bool is_64_bit; 1313 int err, fd; 1314 char kcore_filename[PATH_MAX]; 1315 u64 stext; 1316 1317 if (!kmaps) 1318 return -EINVAL; 1319 1320 machine = maps__machine(kmaps); 1321 1322 /* This function requires that the map is the kernel map */ 1323 if (!__map__is_kernel(map)) 1324 return -EINVAL; 1325 1326 if (!filename_from_kallsyms_filename(kcore_filename, "kcore", 1327 kallsyms_filename)) 1328 return -EINVAL; 1329 1330 /* Modules and kernel must be present at their original addresses */ 1331 if (validate_kcore_addresses(kallsyms_filename, map)) 1332 return -EINVAL; 1333 1334 md.dso = dso; 1335 INIT_LIST_HEAD(&md.maps); 1336 1337 fd = open(kcore_filename, O_RDONLY); 1338 if (fd < 0) { 1339 pr_debug("Failed to open %s. Note /proc/kcore requires CAP_SYS_RAWIO capability to access.\n", 1340 kcore_filename); 1341 return -EINVAL; 1342 } 1343 1344 /* Read new maps into temporary lists */ 1345 err = file__read_maps(fd, map__prot(map) & PROT_EXEC, kcore_mapfn, &md, 1346 &is_64_bit); 1347 if (err) 1348 goto out_err; 1349 dso__set_is_64_bit(dso, is_64_bit); 1350 1351 if (list_empty(&md.maps)) { 1352 err = -EINVAL; 1353 goto out_err; 1354 } 1355 1356 /* Remove old maps */ 1357 maps__remove_maps(kmaps, remove_old_maps, map); 1358 machine->trampolines_mapped = false; 1359 1360 /* Find the kernel map using the '_stext' symbol */ 1361 if (!kallsyms__get_function_start(kallsyms_filename, "_stext", &stext)) { 1362 u64 replacement_size = 0; 1363 struct map_list_node *new_node; 1364 1365 list_for_each_entry(new_node, &md.maps, node) { 1366 struct map *new_map = new_node->map; 1367 u64 new_size = map__size(new_map); 1368 1369 if (!(stext >= map__start(new_map) && stext < map__end(new_map))) 1370 continue; 1371 1372 /* 1373 * On some architectures, ARM64 for example, the kernel 1374 * text can get allocated inside of the vmalloc segment. 1375 * Select the smallest matching segment, in case stext 1376 * falls within more than one in the list. 1377 */ 1378 if (!replacement_map || new_size < replacement_size) { 1379 replacement_map = new_map; 1380 replacement_size = new_size; 1381 } 1382 } 1383 } 1384 1385 if (!replacement_map) 1386 replacement_map = list_entry(md.maps.next, struct map_list_node, node)->map; 1387 1388 /* 1389 * Update addresses of vmlinux map. Re-insert it to ensure maps are 1390 * correctly ordered. Do this before using maps__merge_in() for the 1391 * remaining maps so vmlinux gets split if necessary. 1392 */ 1393 map_ref = map__get(map); 1394 maps__remove(kmaps, map_ref); 1395 1396 map__set_start(map_ref, map__start(replacement_map)); 1397 map__set_end(map_ref, map__end(replacement_map)); 1398 map__set_pgoff(map_ref, map__pgoff(replacement_map)); 1399 map__set_mapping_type(map_ref, map__mapping_type(replacement_map)); 1400 1401 err = maps__insert(kmaps, map_ref); 1402 map__put(map_ref); 1403 if (err) 1404 goto out_err; 1405 1406 /* Add new maps */ 1407 while (!list_empty(&md.maps)) { 1408 struct map_list_node *new_node = list_entry(md.maps.next, struct map_list_node, node); 1409 struct map *new_map = new_node->map; 1410 1411 list_del_init(&new_node->node); 1412 1413 /* skip if replacement_map, already inserted above */ 1414 if (!RC_CHK_EQUAL(new_map, replacement_map)) { 1415 /* 1416 * Merge kcore map into existing maps, 1417 * and ensure that current maps (eBPF) 1418 * stay intact. 1419 */ 1420 if (maps__merge_in(kmaps, new_map)) { 1421 err = -EINVAL; 1422 goto out_err; 1423 } 1424 } 1425 free(new_node); 1426 } 1427 1428 if (machine__is(machine, "x86_64")) { 1429 u64 addr; 1430 1431 /* 1432 * If one of the corresponding symbols is there, assume the 1433 * entry trampoline maps are too. 1434 */ 1435 if (!kallsyms__get_function_start(kallsyms_filename, 1436 ENTRY_TRAMPOLINE_NAME, 1437 &addr)) 1438 machine->trampolines_mapped = true; 1439 } 1440 1441 /* 1442 * Set the data type and long name so that kcore can be read via 1443 * dso__data_read_addr(). 1444 */ 1445 if (dso__kernel(dso) == DSO_SPACE__KERNEL_GUEST) 1446 dso__set_binary_type(dso, DSO_BINARY_TYPE__GUEST_KCORE); 1447 else 1448 dso__set_binary_type(dso, DSO_BINARY_TYPE__KCORE); 1449 dso__set_long_name(dso, strdup(kcore_filename), true); 1450 1451 close(fd); 1452 1453 if (map__prot(map) & PROT_EXEC) 1454 pr_debug("Using %s for kernel object code\n", kcore_filename); 1455 else 1456 pr_debug("Using %s for kernel data\n", kcore_filename); 1457 1458 return 0; 1459 1460 out_err: 1461 while (!list_empty(&md.maps)) { 1462 struct map_list_node *list_node; 1463 1464 list_node = list_entry(md.maps.next, struct map_list_node, node); 1465 list_del_init(&list_node->node); 1466 map__zput(list_node->map); 1467 free(list_node); 1468 } 1469 close(fd); 1470 return err; 1471 } 1472 1473 /* 1474 * If the kernel is relocated at boot time, kallsyms won't match. Compute the 1475 * delta based on the relocation reference symbol. 1476 */ 1477 static int kallsyms__delta(struct kmap *kmap, const char *filename, u64 *delta) 1478 { 1479 u64 addr; 1480 1481 if (!kmap->ref_reloc_sym || !kmap->ref_reloc_sym->name) 1482 return 0; 1483 1484 if (kallsyms__get_function_start(filename, kmap->ref_reloc_sym->name, &addr)) 1485 return -1; 1486 1487 *delta = addr - kmap->ref_reloc_sym->addr; 1488 return 0; 1489 } 1490 1491 int __dso__load_kallsyms(struct dso *dso, const char *filename, 1492 struct map *map, bool no_kcore) 1493 { 1494 struct kmap *kmap = map__kmap(map); 1495 u64 delta = 0; 1496 1497 if (symbol__restricted_filename(filename, "/proc/kallsyms")) 1498 return -1; 1499 1500 if (!kmap || !kmap->kmaps) 1501 return -1; 1502 1503 if (dso__load_all_kallsyms(dso, filename) < 0) 1504 return -1; 1505 1506 if (kallsyms__delta(kmap, filename, &delta)) 1507 return -1; 1508 1509 symbols__fixup_end(dso__symbols(dso), true); 1510 symbols__fixup_duplicate(dso__symbols(dso)); 1511 1512 if (dso__kernel(dso) == DSO_SPACE__KERNEL_GUEST) 1513 dso__set_symtab_type(dso, DSO_BINARY_TYPE__GUEST_KALLSYMS); 1514 else 1515 dso__set_symtab_type(dso, DSO_BINARY_TYPE__KALLSYMS); 1516 1517 if (!no_kcore && !dso__load_kcore(dso, map, filename)) 1518 return maps__split_kallsyms_for_kcore(kmap->kmaps, dso); 1519 else 1520 return maps__split_kallsyms(kmap->kmaps, dso, delta, map); 1521 } 1522 1523 int dso__load_kallsyms(struct dso *dso, const char *filename, 1524 struct map *map) 1525 { 1526 return __dso__load_kallsyms(dso, filename, map, false); 1527 } 1528 1529 static int dso__load_perf_map(const char *map_path, struct dso *dso) 1530 { 1531 char *line = NULL; 1532 size_t n; 1533 FILE *file; 1534 int nr_syms = 0; 1535 1536 file = fopen(map_path, "r"); 1537 if (file == NULL) 1538 goto out_failure; 1539 1540 while (!feof(file)) { 1541 u64 start, size; 1542 struct symbol *sym; 1543 int line_len, len; 1544 1545 line_len = getline(&line, &n, file); 1546 if (line_len < 0) 1547 break; 1548 1549 if (!line) 1550 goto out_failure; 1551 1552 line[--line_len] = '\0'; /* \n */ 1553 1554 len = hex2u64(line, &start); 1555 1556 len++; 1557 if (len + 2 >= line_len) 1558 continue; 1559 1560 len += hex2u64(line + len, &size); 1561 1562 len++; 1563 if (len + 2 >= line_len) 1564 continue; 1565 1566 sym = symbol__new(start, size, STB_GLOBAL, STT_FUNC, line + len); 1567 1568 if (sym == NULL) 1569 goto out_delete_line; 1570 1571 symbols__insert(dso__symbols(dso), sym); 1572 nr_syms++; 1573 } 1574 1575 free(line); 1576 fclose(file); 1577 1578 return nr_syms; 1579 1580 out_delete_line: 1581 free(line); 1582 out_failure: 1583 return -1; 1584 } 1585 1586 #ifdef HAVE_LIBBFD_SUPPORT 1587 #define PACKAGE 'perf' 1588 #include <bfd.h> 1589 1590 static int bfd_symbols__cmpvalue(const void *a, const void *b) 1591 { 1592 const asymbol *as = *(const asymbol **)a, *bs = *(const asymbol **)b; 1593 1594 if (bfd_asymbol_value(as) != bfd_asymbol_value(bs)) 1595 return bfd_asymbol_value(as) - bfd_asymbol_value(bs); 1596 1597 return bfd_asymbol_name(as)[0] - bfd_asymbol_name(bs)[0]; 1598 } 1599 1600 static int bfd2elf_binding(asymbol *symbol) 1601 { 1602 if (symbol->flags & BSF_WEAK) 1603 return STB_WEAK; 1604 if (symbol->flags & BSF_GLOBAL) 1605 return STB_GLOBAL; 1606 if (symbol->flags & BSF_LOCAL) 1607 return STB_LOCAL; 1608 return -1; 1609 } 1610 1611 int dso__load_bfd_symbols(struct dso *dso, const char *debugfile) 1612 { 1613 int err = -1; 1614 long symbols_size, symbols_count, i; 1615 asection *section; 1616 asymbol **symbols, *sym; 1617 struct symbol *symbol; 1618 bfd *abfd; 1619 u64 start, len; 1620 1621 abfd = bfd_openr(debugfile, NULL); 1622 if (!abfd) 1623 return -1; 1624 1625 if (!bfd_check_format(abfd, bfd_object)) { 1626 pr_debug2("%s: cannot read %s bfd file.\n", __func__, 1627 dso__long_name(dso)); 1628 goto out_close; 1629 } 1630 1631 if (bfd_get_flavour(abfd) == bfd_target_elf_flavour) 1632 goto out_close; 1633 1634 symbols_size = bfd_get_symtab_upper_bound(abfd); 1635 if (symbols_size == 0) { 1636 bfd_close(abfd); 1637 return 0; 1638 } 1639 1640 if (symbols_size < 0) 1641 goto out_close; 1642 1643 symbols = malloc(symbols_size); 1644 if (!symbols) 1645 goto out_close; 1646 1647 symbols_count = bfd_canonicalize_symtab(abfd, symbols); 1648 if (symbols_count < 0) 1649 goto out_free; 1650 1651 section = bfd_get_section_by_name(abfd, ".text"); 1652 if (section) { 1653 for (i = 0; i < symbols_count; ++i) { 1654 if (!strcmp(bfd_asymbol_name(symbols[i]), "__ImageBase") || 1655 !strcmp(bfd_asymbol_name(symbols[i]), "__image_base__")) 1656 break; 1657 } 1658 if (i < symbols_count) { 1659 /* PE symbols can only have 4 bytes, so use .text high bits */ 1660 u64 text_offset = (section->vma - (u32)section->vma) 1661 + (u32)bfd_asymbol_value(symbols[i]); 1662 dso__set_text_offset(dso, text_offset); 1663 dso__set_text_end(dso, (section->vma - text_offset) + section->size); 1664 } else { 1665 dso__set_text_offset(dso, section->vma - section->filepos); 1666 dso__set_text_end(dso, section->filepos + section->size); 1667 } 1668 } 1669 1670 qsort(symbols, symbols_count, sizeof(asymbol *), bfd_symbols__cmpvalue); 1671 1672 #ifdef bfd_get_section 1673 #define bfd_asymbol_section bfd_get_section 1674 #endif 1675 for (i = 0; i < symbols_count; ++i) { 1676 sym = symbols[i]; 1677 section = bfd_asymbol_section(sym); 1678 if (bfd2elf_binding(sym) < 0) 1679 continue; 1680 1681 while (i + 1 < symbols_count && 1682 bfd_asymbol_section(symbols[i + 1]) == section && 1683 bfd2elf_binding(symbols[i + 1]) < 0) 1684 i++; 1685 1686 if (i + 1 < symbols_count && 1687 bfd_asymbol_section(symbols[i + 1]) == section) 1688 len = symbols[i + 1]->value - sym->value; 1689 else 1690 len = section->size - sym->value; 1691 1692 start = bfd_asymbol_value(sym) - dso__text_offset(dso); 1693 symbol = symbol__new(start, len, bfd2elf_binding(sym), STT_FUNC, 1694 bfd_asymbol_name(sym)); 1695 if (!symbol) 1696 goto out_free; 1697 1698 symbols__insert(dso__symbols(dso), symbol); 1699 } 1700 #ifdef bfd_get_section 1701 #undef bfd_asymbol_section 1702 #endif 1703 1704 symbols__fixup_end(dso__symbols(dso), false); 1705 symbols__fixup_duplicate(dso__symbols(dso)); 1706 dso__set_adjust_symbols(dso, true); 1707 1708 err = 0; 1709 out_free: 1710 free(symbols); 1711 out_close: 1712 bfd_close(abfd); 1713 return err; 1714 } 1715 #endif 1716 1717 static bool dso__is_compatible_symtab_type(struct dso *dso, bool kmod, 1718 enum dso_binary_type type) 1719 { 1720 switch (type) { 1721 case DSO_BINARY_TYPE__JAVA_JIT: 1722 case DSO_BINARY_TYPE__DEBUGLINK: 1723 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO: 1724 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO: 1725 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO: 1726 case DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO: 1727 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO: 1728 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO: 1729 case DSO_BINARY_TYPE__GNU_DEBUGDATA: 1730 return !kmod && dso__kernel(dso) == DSO_SPACE__USER; 1731 1732 case DSO_BINARY_TYPE__KALLSYMS: 1733 case DSO_BINARY_TYPE__VMLINUX: 1734 case DSO_BINARY_TYPE__KCORE: 1735 return dso__kernel(dso) == DSO_SPACE__KERNEL; 1736 1737 case DSO_BINARY_TYPE__GUEST_KALLSYMS: 1738 case DSO_BINARY_TYPE__GUEST_VMLINUX: 1739 case DSO_BINARY_TYPE__GUEST_KCORE: 1740 return dso__kernel(dso) == DSO_SPACE__KERNEL_GUEST; 1741 1742 case DSO_BINARY_TYPE__GUEST_KMODULE: 1743 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP: 1744 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE: 1745 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP: 1746 /* 1747 * kernel modules know their symtab type - it's set when 1748 * creating a module dso in machine__addnew_module_map(). 1749 */ 1750 return kmod && dso__symtab_type(dso) == type; 1751 1752 case DSO_BINARY_TYPE__BUILD_ID_CACHE: 1753 case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO: 1754 return true; 1755 1756 case DSO_BINARY_TYPE__BPF_PROG_INFO: 1757 case DSO_BINARY_TYPE__BPF_IMAGE: 1758 case DSO_BINARY_TYPE__OOL: 1759 case DSO_BINARY_TYPE__NOT_FOUND: 1760 default: 1761 return false; 1762 } 1763 } 1764 1765 /* Checks for the existence of the perf-<pid>.map file in two different 1766 * locations. First, if the process is a separate mount namespace, check in 1767 * that namespace using the pid of the innermost pid namespace. If's not in a 1768 * namespace, or the file can't be found there, try in the mount namespace of 1769 * the tracing process using our view of its pid. 1770 */ 1771 static int dso__find_perf_map(char *filebuf, size_t bufsz, 1772 struct nsinfo **nsip) 1773 { 1774 struct nscookie nsc; 1775 struct nsinfo *nsi; 1776 struct nsinfo *nnsi; 1777 int rc = -1; 1778 1779 nsi = *nsip; 1780 1781 if (nsinfo__need_setns(nsi)) { 1782 snprintf(filebuf, bufsz, "/tmp/perf-%d.map", nsinfo__nstgid(nsi)); 1783 nsinfo__mountns_enter(nsi, &nsc); 1784 rc = access(filebuf, R_OK); 1785 nsinfo__mountns_exit(&nsc); 1786 if (rc == 0) 1787 return rc; 1788 } 1789 1790 nnsi = nsinfo__copy(nsi); 1791 if (nnsi) { 1792 nsinfo__put(nsi); 1793 1794 nsinfo__clear_need_setns(nnsi); 1795 snprintf(filebuf, bufsz, "/tmp/perf-%d.map", nsinfo__tgid(nnsi)); 1796 *nsip = nnsi; 1797 rc = 0; 1798 } 1799 1800 return rc; 1801 } 1802 1803 int dso__load(struct dso *dso, struct map *map) 1804 { 1805 char *name; 1806 int ret = -1; 1807 u_int i; 1808 struct machine *machine = NULL; 1809 char *root_dir = (char *) ""; 1810 int ss_pos = 0; 1811 struct symsrc ss_[2]; 1812 struct symsrc *syms_ss = NULL, *runtime_ss = NULL; 1813 bool kmod; 1814 bool perfmap; 1815 struct build_id bid; 1816 struct nscookie nsc; 1817 char newmapname[PATH_MAX]; 1818 const char *map_path = dso__long_name(dso); 1819 1820 mutex_lock(dso__lock(dso)); 1821 perfmap = is_perf_pid_map_name(map_path); 1822 1823 if (perfmap) { 1824 if (dso__nsinfo(dso) && 1825 (dso__find_perf_map(newmapname, sizeof(newmapname), 1826 dso__nsinfo_ptr(dso)) == 0)) { 1827 map_path = newmapname; 1828 } 1829 } 1830 1831 nsinfo__mountns_enter(dso__nsinfo(dso), &nsc); 1832 1833 /* check again under the dso->lock */ 1834 if (dso__loaded(dso)) { 1835 ret = 1; 1836 goto out; 1837 } 1838 1839 kmod = dso__is_kmod(dso); 1840 1841 if (dso__kernel(dso) && !kmod) { 1842 if (dso__kernel(dso) == DSO_SPACE__KERNEL) 1843 ret = dso__load_kernel_sym(dso, map); 1844 else if (dso__kernel(dso) == DSO_SPACE__KERNEL_GUEST) 1845 ret = dso__load_guest_kernel_sym(dso, map); 1846 1847 machine = maps__machine(map__kmaps(map)); 1848 if (machine__is(machine, "x86_64")) 1849 machine__map_x86_64_entry_trampolines(machine, dso); 1850 goto out; 1851 } 1852 1853 dso__set_adjust_symbols(dso, false); 1854 1855 if (perfmap) { 1856 ret = dso__load_perf_map(map_path, dso); 1857 dso__set_symtab_type(dso, ret > 0 1858 ? DSO_BINARY_TYPE__JAVA_JIT 1859 : DSO_BINARY_TYPE__NOT_FOUND); 1860 goto out; 1861 } 1862 1863 if (machine) 1864 root_dir = machine->root_dir; 1865 1866 name = malloc(PATH_MAX); 1867 if (!name) 1868 goto out; 1869 1870 /* 1871 * Read the build id if possible. This is required for 1872 * DSO_BINARY_TYPE__BUILDID_DEBUGINFO to work 1873 */ 1874 if (!dso__has_build_id(dso) && 1875 is_regular_file(dso__long_name(dso))) { 1876 __symbol__join_symfs(name, PATH_MAX, dso__long_name(dso)); 1877 if (filename__read_build_id(name, &bid) > 0) 1878 dso__set_build_id(dso, &bid); 1879 } 1880 1881 /* 1882 * Iterate over candidate debug images. 1883 * Keep track of "interesting" ones (those which have a symtab, dynsym, 1884 * and/or opd section) for processing. 1885 */ 1886 for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) { 1887 struct symsrc *ss = &ss_[ss_pos]; 1888 bool next_slot = false; 1889 bool is_reg; 1890 bool nsexit; 1891 int bfdrc = -1; 1892 int sirc = -1; 1893 1894 enum dso_binary_type symtab_type = binary_type_symtab[i]; 1895 1896 nsexit = (symtab_type == DSO_BINARY_TYPE__BUILD_ID_CACHE || 1897 symtab_type == DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO); 1898 1899 if (!dso__is_compatible_symtab_type(dso, kmod, symtab_type)) 1900 continue; 1901 1902 if (dso__read_binary_type_filename(dso, symtab_type, 1903 root_dir, name, PATH_MAX)) 1904 continue; 1905 1906 if (nsexit) 1907 nsinfo__mountns_exit(&nsc); 1908 1909 is_reg = is_regular_file(name); 1910 if (!is_reg && errno == ENOENT && dso__nsinfo(dso)) { 1911 char *new_name = dso__filename_with_chroot(dso, name); 1912 if (new_name) { 1913 is_reg = is_regular_file(new_name); 1914 strlcpy(name, new_name, PATH_MAX); 1915 free(new_name); 1916 } 1917 } 1918 1919 #ifdef HAVE_LIBBFD_SUPPORT 1920 if (is_reg) 1921 bfdrc = dso__load_bfd_symbols(dso, name); 1922 #endif 1923 if (is_reg && bfdrc < 0) 1924 sirc = symsrc__init(ss, dso, name, symtab_type); 1925 1926 if (nsexit) 1927 nsinfo__mountns_enter(dso__nsinfo(dso), &nsc); 1928 1929 if (bfdrc == 0) { 1930 ret = 0; 1931 break; 1932 } 1933 1934 if (!is_reg || sirc < 0) 1935 continue; 1936 1937 if (!syms_ss && symsrc__has_symtab(ss)) { 1938 syms_ss = ss; 1939 next_slot = true; 1940 if (!dso__symsrc_filename(dso)) 1941 dso__set_symsrc_filename(dso, strdup(name)); 1942 } 1943 1944 if (!runtime_ss && symsrc__possibly_runtime(ss)) { 1945 runtime_ss = ss; 1946 next_slot = true; 1947 } 1948 1949 if (next_slot) { 1950 ss_pos++; 1951 1952 if (dso__binary_type(dso) == DSO_BINARY_TYPE__NOT_FOUND) 1953 dso__set_binary_type(dso, symtab_type); 1954 1955 if (syms_ss && runtime_ss) 1956 break; 1957 } else { 1958 symsrc__destroy(ss); 1959 } 1960 1961 } 1962 1963 if (!runtime_ss && !syms_ss) 1964 goto out_free; 1965 1966 if (runtime_ss && !syms_ss) { 1967 syms_ss = runtime_ss; 1968 } 1969 1970 /* We'll have to hope for the best */ 1971 if (!runtime_ss && syms_ss) 1972 runtime_ss = syms_ss; 1973 1974 if (syms_ss) 1975 ret = dso__load_sym(dso, map, syms_ss, runtime_ss, kmod); 1976 else 1977 ret = -1; 1978 1979 if (ret > 0) { 1980 int nr_plt; 1981 1982 nr_plt = dso__synthesize_plt_symbols(dso, runtime_ss); 1983 if (nr_plt > 0) 1984 ret += nr_plt; 1985 } 1986 1987 for (; ss_pos > 0; ss_pos--) 1988 symsrc__destroy(&ss_[ss_pos - 1]); 1989 out_free: 1990 free(name); 1991 if (ret < 0 && strstr(dso__name(dso), " (deleted)") != NULL) 1992 ret = 0; 1993 out: 1994 dso__set_loaded(dso); 1995 mutex_unlock(dso__lock(dso)); 1996 nsinfo__mountns_exit(&nsc); 1997 1998 return ret; 1999 } 2000 2001 /* 2002 * Always takes ownership of vmlinux when vmlinux_allocated == true, even if 2003 * it returns an error. 2004 */ 2005 int dso__load_vmlinux(struct dso *dso, struct map *map, 2006 const char *vmlinux, bool vmlinux_allocated) 2007 { 2008 int err = -1; 2009 struct symsrc ss; 2010 char symfs_vmlinux[PATH_MAX]; 2011 enum dso_binary_type symtab_type; 2012 2013 if (vmlinux[0] == '/') 2014 snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s", vmlinux); 2015 else 2016 symbol__join_symfs(symfs_vmlinux, vmlinux); 2017 2018 if (dso__kernel(dso) == DSO_SPACE__KERNEL_GUEST) 2019 symtab_type = DSO_BINARY_TYPE__GUEST_VMLINUX; 2020 else 2021 symtab_type = DSO_BINARY_TYPE__VMLINUX; 2022 2023 if (symsrc__init(&ss, dso, symfs_vmlinux, symtab_type)) { 2024 if (vmlinux_allocated) 2025 free((char *) vmlinux); 2026 return -1; 2027 } 2028 2029 /* 2030 * dso__load_sym() may copy 'dso' which will result in the copies having 2031 * an incorrect long name unless we set it here first. 2032 */ 2033 dso__set_long_name(dso, vmlinux, vmlinux_allocated); 2034 if (dso__kernel(dso) == DSO_SPACE__KERNEL_GUEST) 2035 dso__set_binary_type(dso, DSO_BINARY_TYPE__GUEST_VMLINUX); 2036 else 2037 dso__set_binary_type(dso, DSO_BINARY_TYPE__VMLINUX); 2038 2039 err = dso__load_sym(dso, map, &ss, &ss, 0); 2040 symsrc__destroy(&ss); 2041 2042 if (err > 0) { 2043 dso__set_loaded(dso); 2044 pr_debug("Using %s for symbols\n", symfs_vmlinux); 2045 } 2046 2047 return err; 2048 } 2049 2050 int dso__load_vmlinux_path(struct dso *dso, struct map *map) 2051 { 2052 int i, err = 0; 2053 char *filename = NULL; 2054 2055 pr_debug("Looking at the vmlinux_path (%d entries long)\n", 2056 vmlinux_path__nr_entries + 1); 2057 2058 for (i = 0; i < vmlinux_path__nr_entries; ++i) { 2059 err = dso__load_vmlinux(dso, map, vmlinux_path[i], false); 2060 if (err > 0) 2061 goto out; 2062 } 2063 2064 if (!symbol_conf.ignore_vmlinux_buildid) 2065 filename = dso__build_id_filename(dso, NULL, 0, false); 2066 if (filename != NULL) { 2067 err = dso__load_vmlinux(dso, map, filename, true); 2068 if (err > 0) 2069 goto out; 2070 } 2071 out: 2072 return err; 2073 } 2074 2075 static bool visible_dir_filter(const char *name, struct dirent *d) 2076 { 2077 if (d->d_type != DT_DIR) 2078 return false; 2079 return lsdir_no_dot_filter(name, d); 2080 } 2081 2082 static int find_matching_kcore(struct map *map, char *dir, size_t dir_sz) 2083 { 2084 char kallsyms_filename[PATH_MAX]; 2085 int ret = -1; 2086 struct strlist *dirs; 2087 struct str_node *nd; 2088 2089 dirs = lsdir(dir, visible_dir_filter); 2090 if (!dirs) 2091 return -1; 2092 2093 strlist__for_each_entry(nd, dirs) { 2094 scnprintf(kallsyms_filename, sizeof(kallsyms_filename), 2095 "%s/%s/kallsyms", dir, nd->s); 2096 if (!validate_kcore_addresses(kallsyms_filename, map)) { 2097 strlcpy(dir, kallsyms_filename, dir_sz); 2098 ret = 0; 2099 break; 2100 } 2101 } 2102 2103 strlist__delete(dirs); 2104 2105 return ret; 2106 } 2107 2108 /* 2109 * Use open(O_RDONLY) to check readability directly instead of access(R_OK) 2110 * since access(R_OK) only checks with real UID/GID but open() use effective 2111 * UID/GID and actual capabilities (e.g. /proc/kcore requires CAP_SYS_RAWIO). 2112 */ 2113 static bool filename__readable(const char *file) 2114 { 2115 int fd = open(file, O_RDONLY); 2116 if (fd < 0) 2117 return false; 2118 close(fd); 2119 return true; 2120 } 2121 2122 static char *dso__find_kallsyms(struct dso *dso, struct map *map) 2123 { 2124 struct build_id bid; 2125 char sbuild_id[SBUILD_ID_SIZE]; 2126 bool is_host = false; 2127 char path[PATH_MAX]; 2128 2129 if (!dso__has_build_id(dso)) { 2130 /* 2131 * Last resort, if we don't have a build-id and couldn't find 2132 * any vmlinux file, try the running kernel kallsyms table. 2133 */ 2134 goto proc_kallsyms; 2135 } 2136 2137 if (sysfs__read_build_id("/sys/kernel/notes", &bid) == 0) 2138 is_host = dso__build_id_equal(dso, &bid); 2139 2140 /* Try a fast path for /proc/kallsyms if possible */ 2141 if (is_host) { 2142 /* 2143 * Do not check the build-id cache, unless we know we cannot use 2144 * /proc/kcore or module maps don't match to /proc/kallsyms. 2145 * To check readability of /proc/kcore, do not use access(R_OK) 2146 * since /proc/kcore requires CAP_SYS_RAWIO to read and access 2147 * can't check it. 2148 */ 2149 if (filename__readable("/proc/kcore") && 2150 !validate_kcore_addresses("/proc/kallsyms", map)) 2151 goto proc_kallsyms; 2152 } 2153 2154 build_id__sprintf(dso__bid(dso), sbuild_id); 2155 2156 /* Find kallsyms in build-id cache with kcore */ 2157 scnprintf(path, sizeof(path), "%s/%s/%s", 2158 buildid_dir, DSO__NAME_KCORE, sbuild_id); 2159 2160 if (!find_matching_kcore(map, path, sizeof(path))) 2161 return strdup(path); 2162 2163 /* Use current /proc/kallsyms if possible */ 2164 if (is_host) { 2165 proc_kallsyms: 2166 return strdup("/proc/kallsyms"); 2167 } 2168 2169 /* Finally, find a cache of kallsyms */ 2170 if (!build_id_cache__kallsyms_path(sbuild_id, path, sizeof(path))) { 2171 pr_err("No kallsyms or vmlinux with build-id %s was found\n", 2172 sbuild_id); 2173 return NULL; 2174 } 2175 2176 return strdup(path); 2177 } 2178 2179 static int dso__load_kernel_sym(struct dso *dso, struct map *map) 2180 { 2181 int err; 2182 const char *kallsyms_filename = NULL; 2183 char *kallsyms_allocated_filename = NULL; 2184 char *filename = NULL; 2185 2186 /* 2187 * Step 1: if the user specified a kallsyms or vmlinux filename, use 2188 * it and only it, reporting errors to the user if it cannot be used. 2189 * 2190 * For instance, try to analyse an ARM perf.data file _without_ a 2191 * build-id, or if the user specifies the wrong path to the right 2192 * vmlinux file, obviously we can't fallback to another vmlinux (a 2193 * x86_86 one, on the machine where analysis is being performed, say), 2194 * or worse, /proc/kallsyms. 2195 * 2196 * If the specified file _has_ a build-id and there is a build-id 2197 * section in the perf.data file, we will still do the expected 2198 * validation in dso__load_vmlinux and will bail out if they don't 2199 * match. 2200 */ 2201 if (symbol_conf.kallsyms_name != NULL) { 2202 kallsyms_filename = symbol_conf.kallsyms_name; 2203 goto do_kallsyms; 2204 } 2205 2206 if (!symbol_conf.ignore_vmlinux && symbol_conf.vmlinux_name != NULL) { 2207 return dso__load_vmlinux(dso, map, symbol_conf.vmlinux_name, false); 2208 } 2209 2210 /* 2211 * Before checking on common vmlinux locations, check if it's 2212 * stored as standard build id binary (not kallsyms) under 2213 * .debug cache. 2214 */ 2215 if (!symbol_conf.ignore_vmlinux_buildid) 2216 filename = __dso__build_id_filename(dso, NULL, 0, false, false); 2217 if (filename != NULL) { 2218 err = dso__load_vmlinux(dso, map, filename, true); 2219 if (err > 0) 2220 return err; 2221 } 2222 2223 if (!symbol_conf.ignore_vmlinux && vmlinux_path != NULL) { 2224 err = dso__load_vmlinux_path(dso, map); 2225 if (err > 0) 2226 return err; 2227 } 2228 2229 /* do not try local files if a symfs was given */ 2230 if (symbol_conf.symfs[0] != 0) 2231 return -1; 2232 2233 kallsyms_allocated_filename = dso__find_kallsyms(dso, map); 2234 if (!kallsyms_allocated_filename) 2235 return -1; 2236 2237 kallsyms_filename = kallsyms_allocated_filename; 2238 2239 do_kallsyms: 2240 err = dso__load_kallsyms(dso, kallsyms_filename, map); 2241 if (err > 0) 2242 pr_debug("Using %s for symbols\n", kallsyms_filename); 2243 free(kallsyms_allocated_filename); 2244 2245 if (err > 0 && !dso__is_kcore(dso)) { 2246 dso__set_binary_type(dso, DSO_BINARY_TYPE__KALLSYMS); 2247 dso__set_long_name(dso, DSO__NAME_KALLSYMS, false); 2248 map__fixup_start(map); 2249 map__fixup_end(map); 2250 } 2251 2252 return err; 2253 } 2254 2255 static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map) 2256 { 2257 int err; 2258 const char *kallsyms_filename; 2259 struct machine *machine = maps__machine(map__kmaps(map)); 2260 char path[PATH_MAX]; 2261 2262 if (machine->kallsyms_filename) { 2263 kallsyms_filename = machine->kallsyms_filename; 2264 } else if (machine__is_default_guest(machine)) { 2265 /* 2266 * if the user specified a vmlinux filename, use it and only 2267 * it, reporting errors to the user if it cannot be used. 2268 * Or use file guest_kallsyms inputted by user on commandline 2269 */ 2270 if (symbol_conf.default_guest_vmlinux_name != NULL) { 2271 err = dso__load_vmlinux(dso, map, 2272 symbol_conf.default_guest_vmlinux_name, 2273 false); 2274 return err; 2275 } 2276 2277 kallsyms_filename = symbol_conf.default_guest_kallsyms; 2278 if (!kallsyms_filename) 2279 return -1; 2280 } else { 2281 sprintf(path, "%s/proc/kallsyms", machine->root_dir); 2282 kallsyms_filename = path; 2283 } 2284 2285 err = dso__load_kallsyms(dso, kallsyms_filename, map); 2286 if (err > 0) 2287 pr_debug("Using %s for symbols\n", kallsyms_filename); 2288 if (err > 0 && !dso__is_kcore(dso)) { 2289 dso__set_binary_type(dso, DSO_BINARY_TYPE__GUEST_KALLSYMS); 2290 dso__set_long_name(dso, machine->mmap_name, false); 2291 map__fixup_start(map); 2292 map__fixup_end(map); 2293 } 2294 2295 return err; 2296 } 2297 2298 static void vmlinux_path__exit(void) 2299 { 2300 while (--vmlinux_path__nr_entries >= 0) 2301 zfree(&vmlinux_path[vmlinux_path__nr_entries]); 2302 vmlinux_path__nr_entries = 0; 2303 2304 zfree(&vmlinux_path); 2305 } 2306 2307 static const char * const vmlinux_paths[] = { 2308 "vmlinux", 2309 "/boot/vmlinux" 2310 }; 2311 2312 static const char * const vmlinux_paths_upd[] = { 2313 "/boot/vmlinux-%s", 2314 "/usr/lib/debug/boot/vmlinux-%s", 2315 "/lib/modules/%s/build/vmlinux", 2316 "/usr/lib/debug/lib/modules/%s/vmlinux", 2317 "/usr/lib/debug/boot/vmlinux-%s.debug" 2318 }; 2319 2320 static int vmlinux_path__add(const char *new_entry) 2321 { 2322 vmlinux_path[vmlinux_path__nr_entries] = strdup(new_entry); 2323 if (vmlinux_path[vmlinux_path__nr_entries] == NULL) 2324 return -1; 2325 ++vmlinux_path__nr_entries; 2326 2327 return 0; 2328 } 2329 2330 static int vmlinux_path__init(struct perf_env *env) 2331 { 2332 struct utsname uts; 2333 char bf[PATH_MAX]; 2334 char *kernel_version; 2335 unsigned int i; 2336 2337 vmlinux_path = malloc(sizeof(char *) * (ARRAY_SIZE(vmlinux_paths) + 2338 ARRAY_SIZE(vmlinux_paths_upd))); 2339 if (vmlinux_path == NULL) 2340 return -1; 2341 2342 for (i = 0; i < ARRAY_SIZE(vmlinux_paths); i++) 2343 if (vmlinux_path__add(vmlinux_paths[i]) < 0) 2344 goto out_fail; 2345 2346 /* only try kernel version if no symfs was given */ 2347 if (symbol_conf.symfs[0] != 0) 2348 return 0; 2349 2350 if (env) { 2351 kernel_version = env->os_release; 2352 } else { 2353 if (uname(&uts) < 0) 2354 goto out_fail; 2355 2356 kernel_version = uts.release; 2357 } 2358 2359 for (i = 0; i < ARRAY_SIZE(vmlinux_paths_upd); i++) { 2360 snprintf(bf, sizeof(bf), vmlinux_paths_upd[i], kernel_version); 2361 if (vmlinux_path__add(bf) < 0) 2362 goto out_fail; 2363 } 2364 2365 return 0; 2366 2367 out_fail: 2368 vmlinux_path__exit(); 2369 return -1; 2370 } 2371 2372 int setup_list(struct strlist **list, const char *list_str, 2373 const char *list_name) 2374 { 2375 if (list_str == NULL) 2376 return 0; 2377 2378 *list = strlist__new(list_str, NULL); 2379 if (!*list) { 2380 pr_err("problems parsing %s list\n", list_name); 2381 return -1; 2382 } 2383 2384 symbol_conf.has_filter = true; 2385 return 0; 2386 } 2387 2388 int setup_intlist(struct intlist **list, const char *list_str, 2389 const char *list_name) 2390 { 2391 if (list_str == NULL) 2392 return 0; 2393 2394 *list = intlist__new(list_str); 2395 if (!*list) { 2396 pr_err("problems parsing %s list\n", list_name); 2397 return -1; 2398 } 2399 return 0; 2400 } 2401 2402 static int setup_addrlist(struct intlist **addr_list, struct strlist *sym_list) 2403 { 2404 struct str_node *pos, *tmp; 2405 unsigned long val; 2406 char *sep; 2407 const char *end; 2408 int i = 0, err; 2409 2410 *addr_list = intlist__new(NULL); 2411 if (!*addr_list) 2412 return -1; 2413 2414 strlist__for_each_entry_safe(pos, tmp, sym_list) { 2415 errno = 0; 2416 val = strtoul(pos->s, &sep, 16); 2417 if (errno || (sep == pos->s)) 2418 continue; 2419 2420 if (*sep != '\0') { 2421 end = pos->s + strlen(pos->s) - 1; 2422 while (end >= sep && isspace(*end)) 2423 end--; 2424 2425 if (end >= sep) 2426 continue; 2427 } 2428 2429 err = intlist__add(*addr_list, val); 2430 if (err) 2431 break; 2432 2433 strlist__remove(sym_list, pos); 2434 i++; 2435 } 2436 2437 if (i == 0) { 2438 intlist__delete(*addr_list); 2439 *addr_list = NULL; 2440 } 2441 2442 return 0; 2443 } 2444 2445 static bool symbol__read_kptr_restrict(void) 2446 { 2447 bool value = false; 2448 FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r"); 2449 bool used_root; 2450 bool cap_syslog = perf_cap__capable(CAP_SYSLOG, &used_root); 2451 2452 if (fp != NULL) { 2453 char line[8]; 2454 2455 if (fgets(line, sizeof(line), fp) != NULL) 2456 value = cap_syslog ? (atoi(line) >= 2) : (atoi(line) != 0); 2457 2458 fclose(fp); 2459 } 2460 2461 /* Per kernel/kallsyms.c: 2462 * we also restrict when perf_event_paranoid > 1 w/o CAP_SYSLOG 2463 */ 2464 if (perf_event_paranoid() > 1 && !cap_syslog) 2465 value = true; 2466 2467 return value; 2468 } 2469 2470 int symbol__annotation_init(void) 2471 { 2472 if (symbol_conf.init_annotation) 2473 return 0; 2474 2475 if (symbol_conf.initialized) { 2476 pr_err("Annotation needs to be init before symbol__init()\n"); 2477 return -1; 2478 } 2479 2480 symbol_conf.priv_size += sizeof(struct annotation); 2481 symbol_conf.init_annotation = true; 2482 return 0; 2483 } 2484 2485 static int setup_parallelism_bitmap(void) 2486 { 2487 struct perf_cpu_map *map; 2488 struct perf_cpu cpu; 2489 int i, err = -1; 2490 2491 if (symbol_conf.parallelism_list_str == NULL) 2492 return 0; 2493 2494 map = perf_cpu_map__new(symbol_conf.parallelism_list_str); 2495 if (map == NULL) { 2496 pr_err("failed to parse parallelism filter list\n"); 2497 return -1; 2498 } 2499 2500 bitmap_fill(symbol_conf.parallelism_filter, MAX_NR_CPUS + 1); 2501 perf_cpu_map__for_each_cpu(cpu, i, map) { 2502 if (cpu.cpu <= 0 || cpu.cpu > MAX_NR_CPUS) { 2503 pr_err("Requested parallelism level %d is invalid.\n", cpu.cpu); 2504 goto out_delete_map; 2505 } 2506 __clear_bit(cpu.cpu, symbol_conf.parallelism_filter); 2507 } 2508 2509 err = 0; 2510 out_delete_map: 2511 perf_cpu_map__put(map); 2512 return err; 2513 } 2514 2515 int symbol__init(struct perf_env *env) 2516 { 2517 const char *symfs; 2518 2519 if (symbol_conf.initialized) 2520 return 0; 2521 2522 symbol_conf.priv_size = PERF_ALIGN(symbol_conf.priv_size, sizeof(u64)); 2523 2524 symbol__elf_init(); 2525 2526 if (symbol_conf.try_vmlinux_path && vmlinux_path__init(env) < 0) 2527 return -1; 2528 2529 if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') { 2530 pr_err("'.' is the only non valid --field-separator argument\n"); 2531 return -1; 2532 } 2533 2534 if (setup_parallelism_bitmap()) 2535 return -1; 2536 2537 if (setup_list(&symbol_conf.dso_list, 2538 symbol_conf.dso_list_str, "dso") < 0) 2539 return -1; 2540 2541 if (setup_list(&symbol_conf.comm_list, 2542 symbol_conf.comm_list_str, "comm") < 0) 2543 goto out_free_dso_list; 2544 2545 if (setup_intlist(&symbol_conf.pid_list, 2546 symbol_conf.pid_list_str, "pid") < 0) 2547 goto out_free_comm_list; 2548 2549 if (setup_intlist(&symbol_conf.tid_list, 2550 symbol_conf.tid_list_str, "tid") < 0) 2551 goto out_free_pid_list; 2552 2553 if (setup_list(&symbol_conf.sym_list, 2554 symbol_conf.sym_list_str, "symbol") < 0) 2555 goto out_free_tid_list; 2556 2557 if (symbol_conf.sym_list && 2558 setup_addrlist(&symbol_conf.addr_list, symbol_conf.sym_list) < 0) 2559 goto out_free_sym_list; 2560 2561 if (setup_list(&symbol_conf.bt_stop_list, 2562 symbol_conf.bt_stop_list_str, "symbol") < 0) 2563 goto out_free_sym_list; 2564 2565 /* 2566 * A path to symbols of "/" is identical to "" 2567 * reset here for simplicity. 2568 */ 2569 symfs = realpath(symbol_conf.symfs, NULL); 2570 if (symfs == NULL) 2571 symfs = symbol_conf.symfs; 2572 if (strcmp(symfs, "/") == 0) 2573 symbol_conf.symfs = ""; 2574 if (symfs != symbol_conf.symfs) 2575 free((void *)symfs); 2576 2577 symbol_conf.kptr_restrict = symbol__read_kptr_restrict(); 2578 2579 symbol_conf.initialized = true; 2580 return 0; 2581 2582 out_free_sym_list: 2583 strlist__delete(symbol_conf.sym_list); 2584 intlist__delete(symbol_conf.addr_list); 2585 out_free_tid_list: 2586 intlist__delete(symbol_conf.tid_list); 2587 out_free_pid_list: 2588 intlist__delete(symbol_conf.pid_list); 2589 out_free_comm_list: 2590 strlist__delete(symbol_conf.comm_list); 2591 out_free_dso_list: 2592 strlist__delete(symbol_conf.dso_list); 2593 return -1; 2594 } 2595 2596 void symbol__exit(void) 2597 { 2598 if (!symbol_conf.initialized) 2599 return; 2600 strlist__delete(symbol_conf.bt_stop_list); 2601 strlist__delete(symbol_conf.sym_list); 2602 strlist__delete(symbol_conf.dso_list); 2603 strlist__delete(symbol_conf.comm_list); 2604 intlist__delete(symbol_conf.tid_list); 2605 intlist__delete(symbol_conf.pid_list); 2606 intlist__delete(symbol_conf.addr_list); 2607 vmlinux_path__exit(); 2608 symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL; 2609 symbol_conf.bt_stop_list = NULL; 2610 symbol_conf.initialized = false; 2611 } 2612 2613 int symbol__config_symfs(const struct option *opt __maybe_unused, 2614 const char *dir, int unset __maybe_unused) 2615 { 2616 char *bf = NULL; 2617 int ret; 2618 2619 symbol_conf.symfs = strdup(dir); 2620 if (symbol_conf.symfs == NULL) 2621 return -ENOMEM; 2622 2623 /* skip the locally configured cache if a symfs is given, and 2624 * config buildid dir to symfs/.debug 2625 */ 2626 ret = asprintf(&bf, "%s/%s", dir, ".debug"); 2627 if (ret < 0) 2628 return -ENOMEM; 2629 2630 set_buildid_dir(bf); 2631 2632 free(bf); 2633 return 0; 2634 } 2635 2636 /* 2637 * Checks that user supplied symbol kernel files are accessible because 2638 * the default mechanism for accessing elf files fails silently. i.e. if 2639 * debug syms for a build ID aren't found perf carries on normally. When 2640 * they are user supplied we should assume that the user doesn't want to 2641 * silently fail. 2642 */ 2643 int symbol__validate_sym_arguments(void) 2644 { 2645 if (symbol_conf.vmlinux_name && 2646 access(symbol_conf.vmlinux_name, R_OK)) { 2647 pr_err("Invalid file: %s\n", symbol_conf.vmlinux_name); 2648 return -EINVAL; 2649 } 2650 if (symbol_conf.kallsyms_name && 2651 access(symbol_conf.kallsyms_name, R_OK)) { 2652 pr_err("Invalid file: %s\n", symbol_conf.kallsyms_name); 2653 return -EINVAL; 2654 } 2655 return 0; 2656 } 2657 2658 static bool want_demangle(bool is_kernel_sym) 2659 { 2660 return is_kernel_sym ? symbol_conf.demangle_kernel : symbol_conf.demangle; 2661 } 2662 2663 /* 2664 * Demangle C++ function signature, typically replaced by demangle-cxx.cpp 2665 * version. 2666 */ 2667 #ifndef HAVE_CXA_DEMANGLE_SUPPORT 2668 char *cxx_demangle_sym(const char *str __maybe_unused, bool params __maybe_unused, 2669 bool modifiers __maybe_unused) 2670 { 2671 #ifdef HAVE_LIBBFD_SUPPORT 2672 int flags = (params ? DMGL_PARAMS : 0) | (modifiers ? DMGL_ANSI : 0); 2673 2674 return bfd_demangle(NULL, str, flags); 2675 #elif defined(HAVE_CPLUS_DEMANGLE_SUPPORT) 2676 int flags = (params ? DMGL_PARAMS : 0) | (modifiers ? DMGL_ANSI : 0); 2677 2678 return cplus_demangle(str, flags); 2679 #else 2680 return NULL; 2681 #endif 2682 } 2683 #endif /* !HAVE_CXA_DEMANGLE_SUPPORT */ 2684 2685 char *dso__demangle_sym(struct dso *dso, int kmodule, const char *elf_name) 2686 { 2687 struct demangle rust_demangle = { 2688 .style = DemangleStyleUnknown, 2689 }; 2690 char *demangled = NULL; 2691 2692 /* 2693 * We need to figure out if the object was created from C++ sources 2694 * DWARF DW_compile_unit has this, but we don't always have access 2695 * to it... 2696 */ 2697 if (!want_demangle((dso && dso__kernel(dso)) || kmodule)) 2698 return demangled; 2699 2700 rust_demangle_demangle(elf_name, &rust_demangle); 2701 if (rust_demangle_is_known(&rust_demangle)) { 2702 /* A rust mangled name. */ 2703 if (rust_demangle.mangled_len == 0) 2704 return demangled; 2705 2706 for (size_t buf_len = roundup_pow_of_two(rust_demangle.mangled_len * 2); 2707 buf_len < 1024 * 1024; buf_len += 32) { 2708 char *tmp = realloc(demangled, buf_len); 2709 2710 if (!tmp) { 2711 /* Failure to grow output buffer, return what is there. */ 2712 return demangled; 2713 } 2714 demangled = tmp; 2715 if (rust_demangle_display_demangle(&rust_demangle, demangled, buf_len, 2716 /*alternate=*/true) == OverflowOk) 2717 return demangled; 2718 } 2719 /* Buffer exceeded sensible bounds, return what is there. */ 2720 return demangled; 2721 } 2722 2723 demangled = cxx_demangle_sym(elf_name, verbose > 0, verbose > 0); 2724 if (demangled) 2725 return demangled; 2726 2727 demangled = ocaml_demangle_sym(elf_name); 2728 if (demangled) 2729 return demangled; 2730 2731 return java_demangle_sym(elf_name, JAVA_DEMANGLE_NORET); 2732 } 2733