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