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