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 static bool dso__is_compatible_symtab_type(struct dso *dso, bool kmod, 1593 enum dso_binary_type type) 1594 { 1595 switch (type) { 1596 case DSO_BINARY_TYPE__JAVA_JIT: 1597 case DSO_BINARY_TYPE__DEBUGLINK: 1598 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO: 1599 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO: 1600 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO: 1601 case DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO: 1602 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO: 1603 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO: 1604 case DSO_BINARY_TYPE__GNU_DEBUGDATA: 1605 return !kmod && dso__kernel(dso) == DSO_SPACE__USER; 1606 1607 case DSO_BINARY_TYPE__KALLSYMS: 1608 case DSO_BINARY_TYPE__VMLINUX: 1609 case DSO_BINARY_TYPE__KCORE: 1610 return dso__kernel(dso) == DSO_SPACE__KERNEL; 1611 1612 case DSO_BINARY_TYPE__GUEST_KALLSYMS: 1613 case DSO_BINARY_TYPE__GUEST_VMLINUX: 1614 case DSO_BINARY_TYPE__GUEST_KCORE: 1615 return dso__kernel(dso) == DSO_SPACE__KERNEL_GUEST; 1616 1617 case DSO_BINARY_TYPE__GUEST_KMODULE: 1618 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP: 1619 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE: 1620 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP: 1621 /* 1622 * kernel modules know their symtab type - it's set when 1623 * creating a module dso in machine__addnew_module_map(). 1624 */ 1625 return kmod && dso__symtab_type(dso) == type; 1626 1627 case DSO_BINARY_TYPE__BUILD_ID_CACHE: 1628 case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO: 1629 return true; 1630 1631 case DSO_BINARY_TYPE__BPF_PROG_INFO: 1632 case DSO_BINARY_TYPE__BPF_IMAGE: 1633 case DSO_BINARY_TYPE__OOL: 1634 case DSO_BINARY_TYPE__NOT_FOUND: 1635 default: 1636 return false; 1637 } 1638 } 1639 1640 /* Checks for the existence of the perf-<pid>.map file in two different 1641 * locations. First, if the process is a separate mount namespace, check in 1642 * that namespace using the pid of the innermost pid namespace. If's not in a 1643 * namespace, or the file can't be found there, try in the mount namespace of 1644 * the tracing process using our view of its pid. 1645 */ 1646 static int dso__find_perf_map(char *filebuf, size_t bufsz, 1647 struct nsinfo **nsip) 1648 { 1649 struct nscookie nsc; 1650 struct nsinfo *nsi; 1651 struct nsinfo *nnsi; 1652 int rc = -1; 1653 1654 nsi = *nsip; 1655 1656 if (nsinfo__need_setns(nsi)) { 1657 snprintf(filebuf, bufsz, "/tmp/perf-%d.map", nsinfo__nstgid(nsi)); 1658 nsinfo__mountns_enter(nsi, &nsc); 1659 rc = access(filebuf, R_OK); 1660 nsinfo__mountns_exit(&nsc); 1661 if (rc == 0) 1662 return rc; 1663 } 1664 1665 nnsi = nsinfo__copy(nsi); 1666 if (nnsi) { 1667 nsinfo__put(nsi); 1668 1669 nsinfo__clear_need_setns(nnsi); 1670 snprintf(filebuf, bufsz, "/tmp/perf-%d.map", nsinfo__tgid(nnsi)); 1671 *nsip = nnsi; 1672 rc = 0; 1673 } 1674 1675 return rc; 1676 } 1677 1678 int dso__load(struct dso *dso, struct map *map) 1679 { 1680 char *name; 1681 int ret = -1; 1682 u_int i; 1683 struct machine *machine = NULL; 1684 char *root_dir = (char *) ""; 1685 int ss_pos = 0; 1686 struct symsrc ss_[2]; 1687 struct symsrc *syms_ss = NULL, *runtime_ss = NULL; 1688 bool kmod; 1689 bool perfmap; 1690 struct nscookie nsc; 1691 char newmapname[PATH_MAX]; 1692 const char *map_path = dso__long_name(dso); 1693 1694 mutex_lock(dso__lock(dso)); 1695 perfmap = is_perf_pid_map_name(map_path); 1696 1697 if (perfmap) { 1698 if (dso__nsinfo(dso) && 1699 (dso__find_perf_map(newmapname, sizeof(newmapname), 1700 dso__nsinfo_ptr(dso)) == 0)) { 1701 map_path = newmapname; 1702 } 1703 } 1704 1705 nsinfo__mountns_enter(dso__nsinfo(dso), &nsc); 1706 1707 /* check again under the dso->lock */ 1708 if (dso__loaded(dso)) { 1709 ret = 1; 1710 goto out; 1711 } 1712 1713 kmod = dso__is_kmod(dso); 1714 1715 if (dso__kernel(dso) && !kmod) { 1716 if (dso__kernel(dso) == DSO_SPACE__KERNEL) 1717 ret = dso__load_kernel_sym(dso, map); 1718 else if (dso__kernel(dso) == DSO_SPACE__KERNEL_GUEST) 1719 ret = dso__load_guest_kernel_sym(dso, map); 1720 1721 machine = maps__machine(map__kmaps(map)); 1722 if (machine__is(machine, "x86_64")) 1723 machine__map_x86_64_entry_trampolines(machine, dso); 1724 goto out; 1725 } 1726 1727 dso__set_adjust_symbols(dso, false); 1728 1729 if (perfmap) { 1730 ret = dso__load_perf_map(map_path, dso); 1731 dso__set_symtab_type(dso, ret > 0 1732 ? DSO_BINARY_TYPE__JAVA_JIT 1733 : DSO_BINARY_TYPE__NOT_FOUND); 1734 goto out; 1735 } 1736 1737 if (machine) 1738 root_dir = machine->root_dir; 1739 1740 name = malloc(PATH_MAX); 1741 if (!name) 1742 goto out; 1743 1744 /* 1745 * Read the build id if possible. This is required for 1746 * DSO_BINARY_TYPE__BUILDID_DEBUGINFO to work. Don't block in case path 1747 * isn't for a regular file. 1748 */ 1749 if (!dso__has_build_id(dso)) { 1750 struct build_id bid = { .size = 0, }; 1751 1752 __symbol__join_symfs(name, PATH_MAX, dso__long_name(dso)); 1753 if (filename__read_build_id(name, &bid, /*block=*/false) > 0) 1754 dso__set_build_id(dso, &bid); 1755 } 1756 1757 /* 1758 * Iterate over candidate debug images. 1759 * Keep track of "interesting" ones (those which have a symtab, dynsym, 1760 * and/or opd section) for processing. 1761 */ 1762 for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) { 1763 struct symsrc *ss = &ss_[ss_pos]; 1764 bool next_slot = false; 1765 bool is_reg; 1766 bool nsexit; 1767 int bfdrc = -1; 1768 int sirc = -1; 1769 1770 enum dso_binary_type symtab_type = binary_type_symtab[i]; 1771 1772 nsexit = (symtab_type == DSO_BINARY_TYPE__BUILD_ID_CACHE || 1773 symtab_type == DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO); 1774 1775 if (!dso__is_compatible_symtab_type(dso, kmod, symtab_type)) 1776 continue; 1777 1778 if (dso__read_binary_type_filename(dso, symtab_type, 1779 root_dir, name, PATH_MAX)) 1780 continue; 1781 1782 if (nsexit) 1783 nsinfo__mountns_exit(&nsc); 1784 1785 is_reg = is_regular_file(name); 1786 if (!is_reg && errno == ENOENT && dso__nsinfo(dso)) { 1787 char *new_name = dso__filename_with_chroot(dso, name); 1788 if (new_name) { 1789 is_reg = is_regular_file(new_name); 1790 strlcpy(name, new_name, PATH_MAX); 1791 free(new_name); 1792 } 1793 } 1794 1795 #ifdef HAVE_LIBBFD_SUPPORT 1796 if (is_reg) 1797 bfdrc = dso__load_bfd_symbols(dso, name); 1798 #endif 1799 if (is_reg && bfdrc < 0) 1800 sirc = symsrc__init(ss, dso, name, symtab_type); 1801 1802 if (nsexit) 1803 nsinfo__mountns_enter(dso__nsinfo(dso), &nsc); 1804 1805 if (bfdrc == 0) { 1806 ret = 0; 1807 break; 1808 } 1809 1810 if (!is_reg || sirc < 0) 1811 continue; 1812 1813 if (!syms_ss && symsrc__has_symtab(ss)) { 1814 syms_ss = ss; 1815 next_slot = true; 1816 if (!dso__symsrc_filename(dso)) 1817 dso__set_symsrc_filename(dso, strdup(name)); 1818 } 1819 1820 if (!runtime_ss && symsrc__possibly_runtime(ss)) { 1821 runtime_ss = ss; 1822 next_slot = true; 1823 } 1824 1825 if (next_slot) { 1826 ss_pos++; 1827 1828 if (dso__binary_type(dso) == DSO_BINARY_TYPE__NOT_FOUND) 1829 dso__set_binary_type(dso, symtab_type); 1830 1831 if (syms_ss && runtime_ss) 1832 break; 1833 } else { 1834 symsrc__destroy(ss); 1835 } 1836 1837 } 1838 1839 if (!runtime_ss && !syms_ss) 1840 goto out_free; 1841 1842 if (runtime_ss && !syms_ss) { 1843 syms_ss = runtime_ss; 1844 } 1845 1846 /* We'll have to hope for the best */ 1847 if (!runtime_ss && syms_ss) 1848 runtime_ss = syms_ss; 1849 1850 if (syms_ss) 1851 ret = dso__load_sym(dso, map, syms_ss, runtime_ss, kmod); 1852 else 1853 ret = -1; 1854 1855 if (ret > 0) { 1856 int nr_plt; 1857 1858 nr_plt = dso__synthesize_plt_symbols(dso, runtime_ss); 1859 if (nr_plt > 0) 1860 ret += nr_plt; 1861 } 1862 1863 for (; ss_pos > 0; ss_pos--) 1864 symsrc__destroy(&ss_[ss_pos - 1]); 1865 out_free: 1866 free(name); 1867 if (ret < 0 && strstr(dso__name(dso), " (deleted)") != NULL) 1868 ret = 0; 1869 out: 1870 dso__set_loaded(dso); 1871 mutex_unlock(dso__lock(dso)); 1872 nsinfo__mountns_exit(&nsc); 1873 1874 return ret; 1875 } 1876 1877 /* 1878 * Always takes ownership of vmlinux when vmlinux_allocated == true, even if 1879 * it returns an error. 1880 */ 1881 int dso__load_vmlinux(struct dso *dso, struct map *map, 1882 const char *vmlinux, bool vmlinux_allocated) 1883 { 1884 int err = -1; 1885 struct symsrc ss; 1886 char symfs_vmlinux[PATH_MAX]; 1887 enum dso_binary_type symtab_type; 1888 1889 if (vmlinux[0] == '/') 1890 snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s", vmlinux); 1891 else 1892 symbol__join_symfs(symfs_vmlinux, vmlinux); 1893 1894 if (dso__kernel(dso) == DSO_SPACE__KERNEL_GUEST) 1895 symtab_type = DSO_BINARY_TYPE__GUEST_VMLINUX; 1896 else 1897 symtab_type = DSO_BINARY_TYPE__VMLINUX; 1898 1899 if (symsrc__init(&ss, dso, symfs_vmlinux, symtab_type)) { 1900 if (vmlinux_allocated) 1901 free((char *) vmlinux); 1902 return -1; 1903 } 1904 1905 /* 1906 * dso__load_sym() may copy 'dso' which will result in the copies having 1907 * an incorrect long name unless we set it here first. 1908 */ 1909 dso__set_long_name(dso, vmlinux, vmlinux_allocated); 1910 if (dso__kernel(dso) == DSO_SPACE__KERNEL_GUEST) 1911 dso__set_binary_type(dso, DSO_BINARY_TYPE__GUEST_VMLINUX); 1912 else 1913 dso__set_binary_type(dso, DSO_BINARY_TYPE__VMLINUX); 1914 1915 err = dso__load_sym(dso, map, &ss, &ss, 0); 1916 symsrc__destroy(&ss); 1917 1918 if (err > 0) { 1919 dso__set_loaded(dso); 1920 pr_debug("Using %s for symbols\n", symfs_vmlinux); 1921 } 1922 1923 return err; 1924 } 1925 1926 int dso__load_vmlinux_path(struct dso *dso, struct map *map) 1927 { 1928 int i, err = 0; 1929 char *filename = NULL; 1930 1931 pr_debug("Looking at the vmlinux_path (%d entries long)\n", 1932 vmlinux_path__nr_entries + 1); 1933 1934 for (i = 0; i < vmlinux_path__nr_entries; ++i) { 1935 err = dso__load_vmlinux(dso, map, vmlinux_path[i], false); 1936 if (err > 0) 1937 goto out; 1938 } 1939 1940 if (!symbol_conf.ignore_vmlinux_buildid) 1941 filename = dso__build_id_filename(dso, NULL, 0, false); 1942 if (filename != NULL) { 1943 err = dso__load_vmlinux(dso, map, filename, true); 1944 if (err > 0) 1945 goto out; 1946 } 1947 out: 1948 return err; 1949 } 1950 1951 static bool visible_dir_filter(const char *name, struct dirent *d) 1952 { 1953 if (d->d_type != DT_DIR) 1954 return false; 1955 return lsdir_no_dot_filter(name, d); 1956 } 1957 1958 static int find_matching_kcore(struct map *map, char *dir, size_t dir_sz) 1959 { 1960 char kallsyms_filename[PATH_MAX]; 1961 int ret = -1; 1962 struct strlist *dirs; 1963 struct str_node *nd; 1964 1965 dirs = lsdir(dir, visible_dir_filter); 1966 if (!dirs) 1967 return -1; 1968 1969 strlist__for_each_entry(nd, dirs) { 1970 scnprintf(kallsyms_filename, sizeof(kallsyms_filename), 1971 "%s/%s/kallsyms", dir, nd->s); 1972 if (!validate_kcore_addresses(kallsyms_filename, map)) { 1973 strlcpy(dir, kallsyms_filename, dir_sz); 1974 ret = 0; 1975 break; 1976 } 1977 } 1978 1979 strlist__delete(dirs); 1980 1981 return ret; 1982 } 1983 1984 /* 1985 * Use open(O_RDONLY) to check readability directly instead of access(R_OK) 1986 * since access(R_OK) only checks with real UID/GID but open() use effective 1987 * UID/GID and actual capabilities (e.g. /proc/kcore requires CAP_SYS_RAWIO). 1988 */ 1989 static bool filename__readable(const char *file) 1990 { 1991 int fd = open(file, O_RDONLY); 1992 if (fd < 0) 1993 return false; 1994 close(fd); 1995 return true; 1996 } 1997 1998 static char *dso__find_kallsyms(struct dso *dso, struct map *map) 1999 { 2000 struct build_id bid = { .size = 0, }; 2001 char sbuild_id[SBUILD_ID_SIZE]; 2002 bool is_host = false; 2003 char path[PATH_MAX]; 2004 2005 if (!dso__has_build_id(dso)) { 2006 /* 2007 * Last resort, if we don't have a build-id and couldn't find 2008 * any vmlinux file, try the running kernel kallsyms table. 2009 */ 2010 goto proc_kallsyms; 2011 } 2012 2013 if (sysfs__read_build_id("/sys/kernel/notes", &bid) == 0) 2014 is_host = dso__build_id_equal(dso, &bid); 2015 2016 /* Try a fast path for /proc/kallsyms if possible */ 2017 if (is_host) { 2018 /* 2019 * Do not check the build-id cache, unless we know we cannot use 2020 * /proc/kcore or module maps don't match to /proc/kallsyms. 2021 * To check readability of /proc/kcore, do not use access(R_OK) 2022 * since /proc/kcore requires CAP_SYS_RAWIO to read and access 2023 * can't check it. 2024 */ 2025 if (filename__readable("/proc/kcore") && 2026 !validate_kcore_addresses("/proc/kallsyms", map)) 2027 goto proc_kallsyms; 2028 } 2029 2030 build_id__snprintf(dso__bid(dso), sbuild_id, sizeof(sbuild_id)); 2031 2032 /* Find kallsyms in build-id cache with kcore */ 2033 scnprintf(path, sizeof(path), "%s/%s/%s", 2034 buildid_dir, DSO__NAME_KCORE, sbuild_id); 2035 2036 if (!find_matching_kcore(map, path, sizeof(path))) 2037 return strdup(path); 2038 2039 /* Use current /proc/kallsyms if possible */ 2040 if (is_host) { 2041 proc_kallsyms: 2042 return strdup("/proc/kallsyms"); 2043 } 2044 2045 /* Finally, find a cache of kallsyms */ 2046 if (!build_id_cache__kallsyms_path(sbuild_id, path, sizeof(path))) { 2047 pr_err("No kallsyms or vmlinux with build-id %s was found\n", 2048 sbuild_id); 2049 return NULL; 2050 } 2051 2052 return strdup(path); 2053 } 2054 2055 static int dso__load_kernel_sym(struct dso *dso, struct map *map) 2056 { 2057 int err; 2058 const char *kallsyms_filename = NULL; 2059 char *kallsyms_allocated_filename = NULL; 2060 char *filename = NULL; 2061 2062 /* 2063 * Step 1: if the user specified a kallsyms or vmlinux filename, use 2064 * it and only it, reporting errors to the user if it cannot be used. 2065 * 2066 * For instance, try to analyse an ARM perf.data file _without_ a 2067 * build-id, or if the user specifies the wrong path to the right 2068 * vmlinux file, obviously we can't fallback to another vmlinux (a 2069 * x86_86 one, on the machine where analysis is being performed, say), 2070 * or worse, /proc/kallsyms. 2071 * 2072 * If the specified file _has_ a build-id and there is a build-id 2073 * section in the perf.data file, we will still do the expected 2074 * validation in dso__load_vmlinux and will bail out if they don't 2075 * match. 2076 */ 2077 if (symbol_conf.kallsyms_name != NULL) { 2078 kallsyms_filename = symbol_conf.kallsyms_name; 2079 goto do_kallsyms; 2080 } 2081 2082 if (!symbol_conf.ignore_vmlinux && symbol_conf.vmlinux_name != NULL) { 2083 return dso__load_vmlinux(dso, map, symbol_conf.vmlinux_name, false); 2084 } 2085 2086 /* 2087 * Before checking on common vmlinux locations, check if it's 2088 * stored as standard build id binary (not kallsyms) under 2089 * .debug cache. 2090 */ 2091 if (!symbol_conf.ignore_vmlinux_buildid) 2092 filename = __dso__build_id_filename(dso, NULL, 0, false, false); 2093 if (filename != NULL) { 2094 err = dso__load_vmlinux(dso, map, filename, true); 2095 if (err > 0) 2096 return err; 2097 } 2098 2099 if (!symbol_conf.ignore_vmlinux && vmlinux_path != NULL) { 2100 err = dso__load_vmlinux_path(dso, map); 2101 if (err > 0) 2102 return err; 2103 } 2104 2105 /* do not try local files if a symfs was given */ 2106 if (symbol_conf.symfs[0] != 0) 2107 return -1; 2108 2109 kallsyms_allocated_filename = dso__find_kallsyms(dso, map); 2110 if (!kallsyms_allocated_filename) 2111 return -1; 2112 2113 kallsyms_filename = kallsyms_allocated_filename; 2114 2115 do_kallsyms: 2116 err = dso__load_kallsyms(dso, kallsyms_filename, map); 2117 if (err > 0) 2118 pr_debug("Using %s for symbols\n", kallsyms_filename); 2119 free(kallsyms_allocated_filename); 2120 2121 if (err > 0 && !dso__is_kcore(dso)) { 2122 dso__set_binary_type(dso, DSO_BINARY_TYPE__KALLSYMS); 2123 dso__set_long_name(dso, DSO__NAME_KALLSYMS, false); 2124 map__fixup_start(map); 2125 map__fixup_end(map); 2126 } 2127 2128 return err; 2129 } 2130 2131 static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map) 2132 { 2133 int err; 2134 const char *kallsyms_filename; 2135 struct machine *machine = maps__machine(map__kmaps(map)); 2136 char path[PATH_MAX]; 2137 2138 if (machine->kallsyms_filename) { 2139 kallsyms_filename = machine->kallsyms_filename; 2140 } else if (machine__is_default_guest(machine)) { 2141 /* 2142 * if the user specified a vmlinux filename, use it and only 2143 * it, reporting errors to the user if it cannot be used. 2144 * Or use file guest_kallsyms inputted by user on commandline 2145 */ 2146 if (symbol_conf.default_guest_vmlinux_name != NULL) { 2147 err = dso__load_vmlinux(dso, map, 2148 symbol_conf.default_guest_vmlinux_name, 2149 false); 2150 return err; 2151 } 2152 2153 kallsyms_filename = symbol_conf.default_guest_kallsyms; 2154 if (!kallsyms_filename) 2155 return -1; 2156 } else { 2157 sprintf(path, "%s/proc/kallsyms", machine->root_dir); 2158 kallsyms_filename = path; 2159 } 2160 2161 err = dso__load_kallsyms(dso, kallsyms_filename, map); 2162 if (err > 0) 2163 pr_debug("Using %s for symbols\n", kallsyms_filename); 2164 if (err > 0 && !dso__is_kcore(dso)) { 2165 dso__set_binary_type(dso, DSO_BINARY_TYPE__GUEST_KALLSYMS); 2166 dso__set_long_name(dso, machine->mmap_name, false); 2167 map__fixup_start(map); 2168 map__fixup_end(map); 2169 } 2170 2171 return err; 2172 } 2173 2174 static void vmlinux_path__exit(void) 2175 { 2176 while (--vmlinux_path__nr_entries >= 0) 2177 zfree(&vmlinux_path[vmlinux_path__nr_entries]); 2178 vmlinux_path__nr_entries = 0; 2179 2180 zfree(&vmlinux_path); 2181 } 2182 2183 static const char * const vmlinux_paths[] = { 2184 "vmlinux", 2185 "/boot/vmlinux" 2186 }; 2187 2188 static const char * const vmlinux_paths_upd[] = { 2189 "/boot/vmlinux-%s", 2190 "/usr/lib/debug/boot/vmlinux-%s", 2191 "/lib/modules/%s/build/vmlinux", 2192 "/usr/lib/debug/lib/modules/%s/vmlinux", 2193 "/usr/lib/debug/boot/vmlinux-%s.debug" 2194 }; 2195 2196 static int vmlinux_path__add(const char *new_entry) 2197 { 2198 vmlinux_path[vmlinux_path__nr_entries] = strdup(new_entry); 2199 if (vmlinux_path[vmlinux_path__nr_entries] == NULL) 2200 return -1; 2201 ++vmlinux_path__nr_entries; 2202 2203 return 0; 2204 } 2205 2206 static int vmlinux_path__init(struct perf_env *env) 2207 { 2208 struct utsname uts; 2209 char bf[PATH_MAX]; 2210 char *kernel_version; 2211 unsigned int i; 2212 2213 vmlinux_path = malloc(sizeof(char *) * (ARRAY_SIZE(vmlinux_paths) + 2214 ARRAY_SIZE(vmlinux_paths_upd))); 2215 if (vmlinux_path == NULL) 2216 return -1; 2217 2218 for (i = 0; i < ARRAY_SIZE(vmlinux_paths); i++) 2219 if (vmlinux_path__add(vmlinux_paths[i]) < 0) 2220 goto out_fail; 2221 2222 /* only try kernel version if no symfs was given */ 2223 if (symbol_conf.symfs[0] != 0) 2224 return 0; 2225 2226 if (env) { 2227 kernel_version = env->os_release; 2228 } else { 2229 if (uname(&uts) < 0) 2230 goto out_fail; 2231 2232 kernel_version = uts.release; 2233 } 2234 2235 for (i = 0; i < ARRAY_SIZE(vmlinux_paths_upd); i++) { 2236 snprintf(bf, sizeof(bf), vmlinux_paths_upd[i], kernel_version); 2237 if (vmlinux_path__add(bf) < 0) 2238 goto out_fail; 2239 } 2240 2241 return 0; 2242 2243 out_fail: 2244 vmlinux_path__exit(); 2245 return -1; 2246 } 2247 2248 int setup_list(struct strlist **list, const char *list_str, 2249 const char *list_name) 2250 { 2251 if (list_str == NULL) 2252 return 0; 2253 2254 *list = strlist__new(list_str, NULL); 2255 if (!*list) { 2256 pr_err("problems parsing %s list\n", list_name); 2257 return -1; 2258 } 2259 2260 symbol_conf.has_filter = true; 2261 return 0; 2262 } 2263 2264 int setup_intlist(struct intlist **list, const char *list_str, 2265 const char *list_name) 2266 { 2267 if (list_str == NULL) 2268 return 0; 2269 2270 *list = intlist__new(list_str); 2271 if (!*list) { 2272 pr_err("problems parsing %s list\n", list_name); 2273 return -1; 2274 } 2275 return 0; 2276 } 2277 2278 static int setup_addrlist(struct intlist **addr_list, struct strlist *sym_list) 2279 { 2280 struct str_node *pos, *tmp; 2281 unsigned long val; 2282 char *sep; 2283 const char *end; 2284 int i = 0, err; 2285 2286 *addr_list = intlist__new(NULL); 2287 if (!*addr_list) 2288 return -1; 2289 2290 strlist__for_each_entry_safe(pos, tmp, sym_list) { 2291 errno = 0; 2292 val = strtoul(pos->s, &sep, 16); 2293 if (errno || (sep == pos->s)) 2294 continue; 2295 2296 if (*sep != '\0') { 2297 end = pos->s + strlen(pos->s) - 1; 2298 while (end >= sep && isspace(*end)) 2299 end--; 2300 2301 if (end >= sep) 2302 continue; 2303 } 2304 2305 err = intlist__add(*addr_list, val); 2306 if (err) 2307 break; 2308 2309 strlist__remove(sym_list, pos); 2310 i++; 2311 } 2312 2313 if (i == 0) { 2314 intlist__delete(*addr_list); 2315 *addr_list = NULL; 2316 } 2317 2318 return 0; 2319 } 2320 2321 static bool symbol__read_kptr_restrict(void) 2322 { 2323 bool value = false; 2324 FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r"); 2325 bool used_root; 2326 bool cap_syslog = perf_cap__capable(CAP_SYSLOG, &used_root); 2327 2328 if (fp != NULL) { 2329 char line[8]; 2330 2331 if (fgets(line, sizeof(line), fp) != NULL) 2332 value = cap_syslog ? (atoi(line) >= 2) : (atoi(line) != 0); 2333 2334 fclose(fp); 2335 } 2336 2337 /* Per kernel/kallsyms.c: 2338 * we also restrict when perf_event_paranoid > 1 w/o CAP_SYSLOG 2339 */ 2340 if (perf_event_paranoid() > 1 && !cap_syslog) 2341 value = true; 2342 2343 return value; 2344 } 2345 2346 int symbol__annotation_init(void) 2347 { 2348 if (symbol_conf.init_annotation) 2349 return 0; 2350 2351 if (symbol_conf.initialized) { 2352 pr_err("Annotation needs to be init before symbol__init()\n"); 2353 return -1; 2354 } 2355 2356 symbol_conf.priv_size += sizeof(struct annotation); 2357 symbol_conf.init_annotation = true; 2358 return 0; 2359 } 2360 2361 static int setup_parallelism_bitmap(void) 2362 { 2363 struct perf_cpu_map *map; 2364 struct perf_cpu cpu; 2365 int i, err = -1; 2366 2367 if (symbol_conf.parallelism_list_str == NULL) 2368 return 0; 2369 2370 map = perf_cpu_map__new(symbol_conf.parallelism_list_str); 2371 if (map == NULL) { 2372 pr_err("failed to parse parallelism filter list\n"); 2373 return -1; 2374 } 2375 2376 bitmap_fill(symbol_conf.parallelism_filter, MAX_NR_CPUS + 1); 2377 perf_cpu_map__for_each_cpu(cpu, i, map) { 2378 if (cpu.cpu <= 0 || cpu.cpu > MAX_NR_CPUS) { 2379 pr_err("Requested parallelism level %d is invalid.\n", cpu.cpu); 2380 goto out_delete_map; 2381 } 2382 __clear_bit(cpu.cpu, symbol_conf.parallelism_filter); 2383 } 2384 2385 err = 0; 2386 out_delete_map: 2387 perf_cpu_map__put(map); 2388 return err; 2389 } 2390 2391 int symbol__init(struct perf_env *env) 2392 { 2393 const char *symfs; 2394 2395 if (symbol_conf.initialized) 2396 return 0; 2397 2398 symbol_conf.priv_size = PERF_ALIGN(symbol_conf.priv_size, sizeof(u64)); 2399 2400 symbol__elf_init(); 2401 2402 if (symbol_conf.try_vmlinux_path && vmlinux_path__init(env) < 0) 2403 return -1; 2404 2405 if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') { 2406 pr_err("'.' is the only non valid --field-separator argument\n"); 2407 return -1; 2408 } 2409 2410 if (setup_parallelism_bitmap()) 2411 return -1; 2412 2413 if (setup_list(&symbol_conf.dso_list, 2414 symbol_conf.dso_list_str, "dso") < 0) 2415 return -1; 2416 2417 if (setup_list(&symbol_conf.comm_list, 2418 symbol_conf.comm_list_str, "comm") < 0) 2419 goto out_free_dso_list; 2420 2421 if (setup_intlist(&symbol_conf.pid_list, 2422 symbol_conf.pid_list_str, "pid") < 0) 2423 goto out_free_comm_list; 2424 2425 if (setup_intlist(&symbol_conf.tid_list, 2426 symbol_conf.tid_list_str, "tid") < 0) 2427 goto out_free_pid_list; 2428 2429 if (setup_list(&symbol_conf.sym_list, 2430 symbol_conf.sym_list_str, "symbol") < 0) 2431 goto out_free_tid_list; 2432 2433 if (symbol_conf.sym_list && 2434 setup_addrlist(&symbol_conf.addr_list, symbol_conf.sym_list) < 0) 2435 goto out_free_sym_list; 2436 2437 if (setup_list(&symbol_conf.bt_stop_list, 2438 symbol_conf.bt_stop_list_str, "symbol") < 0) 2439 goto out_free_sym_list; 2440 2441 /* 2442 * A path to symbols of "/" is identical to "" 2443 * reset here for simplicity. 2444 */ 2445 symfs = realpath(symbol_conf.symfs, NULL); 2446 if (symfs == NULL) 2447 symfs = symbol_conf.symfs; 2448 if (strcmp(symfs, "/") == 0) 2449 symbol_conf.symfs = ""; 2450 if (symfs != symbol_conf.symfs) 2451 free((void *)symfs); 2452 2453 symbol_conf.kptr_restrict = symbol__read_kptr_restrict(); 2454 2455 symbol_conf.initialized = true; 2456 return 0; 2457 2458 out_free_sym_list: 2459 strlist__delete(symbol_conf.sym_list); 2460 intlist__delete(symbol_conf.addr_list); 2461 out_free_tid_list: 2462 intlist__delete(symbol_conf.tid_list); 2463 out_free_pid_list: 2464 intlist__delete(symbol_conf.pid_list); 2465 out_free_comm_list: 2466 strlist__delete(symbol_conf.comm_list); 2467 out_free_dso_list: 2468 strlist__delete(symbol_conf.dso_list); 2469 return -1; 2470 } 2471 2472 void symbol__exit(void) 2473 { 2474 if (!symbol_conf.initialized) 2475 return; 2476 strlist__delete(symbol_conf.bt_stop_list); 2477 strlist__delete(symbol_conf.sym_list); 2478 strlist__delete(symbol_conf.dso_list); 2479 strlist__delete(symbol_conf.comm_list); 2480 intlist__delete(symbol_conf.tid_list); 2481 intlist__delete(symbol_conf.pid_list); 2482 intlist__delete(symbol_conf.addr_list); 2483 vmlinux_path__exit(); 2484 symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL; 2485 symbol_conf.bt_stop_list = NULL; 2486 symbol_conf.initialized = false; 2487 } 2488 2489 int symbol__config_symfs(const struct option *opt __maybe_unused, 2490 const char *dir, int unset __maybe_unused) 2491 { 2492 char *bf = NULL; 2493 int ret; 2494 2495 symbol_conf.symfs = strdup(dir); 2496 if (symbol_conf.symfs == NULL) 2497 return -ENOMEM; 2498 2499 /* skip the locally configured cache if a symfs is given, and 2500 * config buildid dir to symfs/.debug 2501 */ 2502 ret = asprintf(&bf, "%s/%s", dir, ".debug"); 2503 if (ret < 0) 2504 return -ENOMEM; 2505 2506 set_buildid_dir(bf); 2507 2508 free(bf); 2509 return 0; 2510 } 2511 2512 /* 2513 * Checks that user supplied symbol kernel files are accessible because 2514 * the default mechanism for accessing elf files fails silently. i.e. if 2515 * debug syms for a build ID aren't found perf carries on normally. When 2516 * they are user supplied we should assume that the user doesn't want to 2517 * silently fail. 2518 */ 2519 int symbol__validate_sym_arguments(void) 2520 { 2521 if (symbol_conf.vmlinux_name && 2522 access(symbol_conf.vmlinux_name, R_OK)) { 2523 pr_err("Invalid file: %s\n", symbol_conf.vmlinux_name); 2524 return -EINVAL; 2525 } 2526 if (symbol_conf.kallsyms_name && 2527 access(symbol_conf.kallsyms_name, R_OK)) { 2528 pr_err("Invalid file: %s\n", symbol_conf.kallsyms_name); 2529 return -EINVAL; 2530 } 2531 return 0; 2532 } 2533 2534 static bool want_demangle(bool is_kernel_sym) 2535 { 2536 return is_kernel_sym ? symbol_conf.demangle_kernel : symbol_conf.demangle; 2537 } 2538 2539 /* 2540 * Demangle C++ function signature, typically replaced by demangle-cxx.cpp 2541 * version. 2542 */ 2543 #ifndef HAVE_CXA_DEMANGLE_SUPPORT 2544 char *cxx_demangle_sym(const char *str __maybe_unused, bool params __maybe_unused, 2545 bool modifiers __maybe_unused) 2546 { 2547 #ifdef HAVE_LIBBFD_SUPPORT 2548 int flags = (params ? DMGL_PARAMS : 0) | (modifiers ? DMGL_ANSI : 0); 2549 2550 return bfd_demangle(NULL, str, flags); 2551 #elif defined(HAVE_CPLUS_DEMANGLE_SUPPORT) 2552 int flags = (params ? DMGL_PARAMS : 0) | (modifiers ? DMGL_ANSI : 0); 2553 2554 return cplus_demangle(str, flags); 2555 #else 2556 return NULL; 2557 #endif 2558 } 2559 #endif /* !HAVE_CXA_DEMANGLE_SUPPORT */ 2560 2561 char *dso__demangle_sym(struct dso *dso, int kmodule, const char *elf_name) 2562 { 2563 struct demangle rust_demangle = { 2564 .style = DemangleStyleUnknown, 2565 }; 2566 char *demangled = NULL; 2567 2568 /* 2569 * We need to figure out if the object was created from C++ sources 2570 * DWARF DW_compile_unit has this, but we don't always have access 2571 * to it... 2572 */ 2573 if (!want_demangle((dso && dso__kernel(dso)) || kmodule)) 2574 return demangled; 2575 2576 rust_demangle_demangle(elf_name, &rust_demangle); 2577 if (rust_demangle_is_known(&rust_demangle)) { 2578 /* A rust mangled name. */ 2579 if (rust_demangle.mangled_len == 0) 2580 return demangled; 2581 2582 for (size_t buf_len = roundup_pow_of_two(rust_demangle.mangled_len * 2); 2583 buf_len < 1024 * 1024; buf_len += 32) { 2584 char *tmp = realloc(demangled, buf_len); 2585 2586 if (!tmp) { 2587 /* Failure to grow output buffer, return what is there. */ 2588 return demangled; 2589 } 2590 demangled = tmp; 2591 if (rust_demangle_display_demangle(&rust_demangle, demangled, buf_len, 2592 /*alternate=*/true) == OverflowOk) 2593 return demangled; 2594 } 2595 /* Buffer exceeded sensible bounds, return what is there. */ 2596 return demangled; 2597 } 2598 2599 demangled = cxx_demangle_sym(elf_name, verbose > 0, verbose > 0); 2600 if (demangled) 2601 return demangled; 2602 2603 demangled = ocaml_demangle_sym(elf_name); 2604 if (demangled) 2605 return demangled; 2606 2607 return java_demangle_sym(elf_name, JAVA_DEMANGLE_NORET); 2608 } 2609