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