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/kernel.h> 8 #include <sys/types.h> 9 #include <sys/stat.h> 10 #include <sys/param.h> 11 #include <fcntl.h> 12 #include <unistd.h> 13 #include <inttypes.h> 14 #include "annotate.h" 15 #include "build-id.h" 16 #include "util.h" 17 #include "debug.h" 18 #include "machine.h" 19 #include "symbol.h" 20 #include "strlist.h" 21 #include "intlist.h" 22 #include "namespaces.h" 23 #include "header.h" 24 #include "path.h" 25 #include "sane_ctype.h" 26 27 #include <elf.h> 28 #include <limits.h> 29 #include <symbol/kallsyms.h> 30 #include <sys/utsname.h> 31 32 static int dso__load_kernel_sym(struct dso *dso, struct map *map); 33 static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map); 34 static bool symbol__is_idle(const char *name); 35 36 int vmlinux_path__nr_entries; 37 char **vmlinux_path; 38 39 struct symbol_conf symbol_conf = { 40 .use_modules = true, 41 .try_vmlinux_path = true, 42 .annotate_src = true, 43 .demangle = true, 44 .demangle_kernel = false, 45 .cumulate_callchain = true, 46 .show_hist_headers = true, 47 .symfs = "", 48 .event_group = true, 49 .inline_name = true, 50 }; 51 52 static enum dso_binary_type binary_type_symtab[] = { 53 DSO_BINARY_TYPE__KALLSYMS, 54 DSO_BINARY_TYPE__GUEST_KALLSYMS, 55 DSO_BINARY_TYPE__JAVA_JIT, 56 DSO_BINARY_TYPE__DEBUGLINK, 57 DSO_BINARY_TYPE__BUILD_ID_CACHE, 58 DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO, 59 DSO_BINARY_TYPE__FEDORA_DEBUGINFO, 60 DSO_BINARY_TYPE__UBUNTU_DEBUGINFO, 61 DSO_BINARY_TYPE__BUILDID_DEBUGINFO, 62 DSO_BINARY_TYPE__SYSTEM_PATH_DSO, 63 DSO_BINARY_TYPE__GUEST_KMODULE, 64 DSO_BINARY_TYPE__GUEST_KMODULE_COMP, 65 DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE, 66 DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP, 67 DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO, 68 DSO_BINARY_TYPE__NOT_FOUND, 69 }; 70 71 #define DSO_BINARY_TYPE__SYMTAB_CNT ARRAY_SIZE(binary_type_symtab) 72 73 bool symbol_type__is_a(char symbol_type, enum map_type map_type) 74 { 75 symbol_type = toupper(symbol_type); 76 77 switch (map_type) { 78 case MAP__FUNCTION: 79 return symbol_type == 'T' || symbol_type == 'W'; 80 case MAP__VARIABLE: 81 return symbol_type == 'D'; 82 default: 83 return false; 84 } 85 } 86 87 static int prefix_underscores_count(const char *str) 88 { 89 const char *tail = str; 90 91 while (*tail == '_') 92 tail++; 93 94 return tail - str; 95 } 96 97 int __weak arch__compare_symbol_names(const char *namea, const char *nameb) 98 { 99 return strcmp(namea, nameb); 100 } 101 102 int __weak arch__compare_symbol_names_n(const char *namea, const char *nameb, 103 unsigned int n) 104 { 105 return strncmp(namea, nameb, n); 106 } 107 108 int __weak arch__choose_best_symbol(struct symbol *syma, 109 struct symbol *symb __maybe_unused) 110 { 111 /* Avoid "SyS" kernel syscall aliases */ 112 if (strlen(syma->name) >= 3 && !strncmp(syma->name, "SyS", 3)) 113 return SYMBOL_B; 114 if (strlen(syma->name) >= 10 && !strncmp(syma->name, "compat_SyS", 10)) 115 return SYMBOL_B; 116 117 return SYMBOL_A; 118 } 119 120 static int choose_best_symbol(struct symbol *syma, struct symbol *symb) 121 { 122 s64 a; 123 s64 b; 124 size_t na, nb; 125 126 /* Prefer a symbol with non zero length */ 127 a = syma->end - syma->start; 128 b = symb->end - symb->start; 129 if ((b == 0) && (a > 0)) 130 return SYMBOL_A; 131 else if ((a == 0) && (b > 0)) 132 return SYMBOL_B; 133 134 /* Prefer a non weak symbol over a weak one */ 135 a = syma->binding == STB_WEAK; 136 b = symb->binding == STB_WEAK; 137 if (b && !a) 138 return SYMBOL_A; 139 if (a && !b) 140 return SYMBOL_B; 141 142 /* Prefer a global symbol over a non global one */ 143 a = syma->binding == STB_GLOBAL; 144 b = symb->binding == STB_GLOBAL; 145 if (a && !b) 146 return SYMBOL_A; 147 if (b && !a) 148 return SYMBOL_B; 149 150 /* Prefer a symbol with less underscores */ 151 a = prefix_underscores_count(syma->name); 152 b = prefix_underscores_count(symb->name); 153 if (b > a) 154 return SYMBOL_A; 155 else if (a > b) 156 return SYMBOL_B; 157 158 /* Choose the symbol with the longest name */ 159 na = strlen(syma->name); 160 nb = strlen(symb->name); 161 if (na > nb) 162 return SYMBOL_A; 163 else if (na < nb) 164 return SYMBOL_B; 165 166 return arch__choose_best_symbol(syma, symb); 167 } 168 169 void symbols__fixup_duplicate(struct rb_root *symbols) 170 { 171 struct rb_node *nd; 172 struct symbol *curr, *next; 173 174 if (symbol_conf.allow_aliases) 175 return; 176 177 nd = rb_first(symbols); 178 179 while (nd) { 180 curr = rb_entry(nd, struct symbol, rb_node); 181 again: 182 nd = rb_next(&curr->rb_node); 183 next = rb_entry(nd, struct symbol, rb_node); 184 185 if (!nd) 186 break; 187 188 if (curr->start != next->start) 189 continue; 190 191 if (choose_best_symbol(curr, next) == SYMBOL_A) { 192 rb_erase(&next->rb_node, symbols); 193 symbol__delete(next); 194 goto again; 195 } else { 196 nd = rb_next(&curr->rb_node); 197 rb_erase(&curr->rb_node, symbols); 198 symbol__delete(curr); 199 } 200 } 201 } 202 203 void symbols__fixup_end(struct rb_root *symbols) 204 { 205 struct rb_node *nd, *prevnd = rb_first(symbols); 206 struct symbol *curr, *prev; 207 208 if (prevnd == NULL) 209 return; 210 211 curr = rb_entry(prevnd, struct symbol, rb_node); 212 213 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) { 214 prev = curr; 215 curr = rb_entry(nd, struct symbol, rb_node); 216 217 if (prev->end == prev->start && prev->end != curr->start) 218 prev->end = curr->start; 219 } 220 221 /* Last entry */ 222 if (curr->end == curr->start) 223 curr->end = roundup(curr->start, 4096) + 4096; 224 } 225 226 void __map_groups__fixup_end(struct map_groups *mg, enum map_type type) 227 { 228 struct maps *maps = &mg->maps[type]; 229 struct map *next, *curr; 230 231 down_write(&maps->lock); 232 233 curr = maps__first(maps); 234 if (curr == NULL) 235 goto out_unlock; 236 237 for (next = map__next(curr); next; next = map__next(curr)) { 238 if (!curr->end) 239 curr->end = next->start; 240 curr = next; 241 } 242 243 /* 244 * We still haven't the actual symbols, so guess the 245 * last map final address. 246 */ 247 if (!curr->end) 248 curr->end = ~0ULL; 249 250 out_unlock: 251 up_write(&maps->lock); 252 } 253 254 struct symbol *symbol__new(u64 start, u64 len, u8 binding, const char *name) 255 { 256 size_t namelen = strlen(name) + 1; 257 struct symbol *sym = calloc(1, (symbol_conf.priv_size + 258 sizeof(*sym) + namelen)); 259 if (sym == NULL) 260 return NULL; 261 262 if (symbol_conf.priv_size) { 263 if (symbol_conf.init_annotation) { 264 struct annotation *notes = (void *)sym; 265 pthread_mutex_init(¬es->lock, NULL); 266 } 267 sym = ((void *)sym) + symbol_conf.priv_size; 268 } 269 270 sym->start = start; 271 sym->end = len ? start + len : start; 272 sym->binding = binding; 273 sym->namelen = namelen - 1; 274 275 pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n", 276 __func__, name, start, sym->end); 277 memcpy(sym->name, name, namelen); 278 279 return sym; 280 } 281 282 void symbol__delete(struct symbol *sym) 283 { 284 free(((void *)sym) - symbol_conf.priv_size); 285 } 286 287 void symbols__delete(struct rb_root *symbols) 288 { 289 struct symbol *pos; 290 struct rb_node *next = rb_first(symbols); 291 292 while (next) { 293 pos = rb_entry(next, struct symbol, rb_node); 294 next = rb_next(&pos->rb_node); 295 rb_erase(&pos->rb_node, symbols); 296 symbol__delete(pos); 297 } 298 } 299 300 void __symbols__insert(struct rb_root *symbols, struct symbol *sym, bool kernel) 301 { 302 struct rb_node **p = &symbols->rb_node; 303 struct rb_node *parent = NULL; 304 const u64 ip = sym->start; 305 struct symbol *s; 306 307 if (kernel) { 308 const char *name = sym->name; 309 /* 310 * ppc64 uses function descriptors and appends a '.' to the 311 * start of every instruction address. Remove it. 312 */ 313 if (name[0] == '.') 314 name++; 315 sym->idle = symbol__is_idle(name); 316 } 317 318 while (*p != NULL) { 319 parent = *p; 320 s = rb_entry(parent, struct symbol, rb_node); 321 if (ip < s->start) 322 p = &(*p)->rb_left; 323 else 324 p = &(*p)->rb_right; 325 } 326 rb_link_node(&sym->rb_node, parent, p); 327 rb_insert_color(&sym->rb_node, symbols); 328 } 329 330 void symbols__insert(struct rb_root *symbols, struct symbol *sym) 331 { 332 __symbols__insert(symbols, sym, false); 333 } 334 335 static struct symbol *symbols__find(struct rb_root *symbols, u64 ip) 336 { 337 struct rb_node *n; 338 339 if (symbols == NULL) 340 return NULL; 341 342 n = symbols->rb_node; 343 344 while (n) { 345 struct symbol *s = rb_entry(n, struct symbol, rb_node); 346 347 if (ip < s->start) 348 n = n->rb_left; 349 else if (ip > s->end || (ip == s->end && ip != s->start)) 350 n = n->rb_right; 351 else 352 return s; 353 } 354 355 return NULL; 356 } 357 358 static struct symbol *symbols__first(struct rb_root *symbols) 359 { 360 struct rb_node *n = rb_first(symbols); 361 362 if (n) 363 return rb_entry(n, struct symbol, rb_node); 364 365 return NULL; 366 } 367 368 static struct symbol *symbols__last(struct rb_root *symbols) 369 { 370 struct rb_node *n = rb_last(symbols); 371 372 if (n) 373 return rb_entry(n, struct symbol, rb_node); 374 375 return NULL; 376 } 377 378 static struct symbol *symbols__next(struct symbol *sym) 379 { 380 struct rb_node *n = rb_next(&sym->rb_node); 381 382 if (n) 383 return rb_entry(n, struct symbol, rb_node); 384 385 return NULL; 386 } 387 388 static void symbols__insert_by_name(struct rb_root *symbols, struct symbol *sym) 389 { 390 struct rb_node **p = &symbols->rb_node; 391 struct rb_node *parent = NULL; 392 struct symbol_name_rb_node *symn, *s; 393 394 symn = container_of(sym, struct symbol_name_rb_node, sym); 395 396 while (*p != NULL) { 397 parent = *p; 398 s = rb_entry(parent, struct symbol_name_rb_node, rb_node); 399 if (strcmp(sym->name, s->sym.name) < 0) 400 p = &(*p)->rb_left; 401 else 402 p = &(*p)->rb_right; 403 } 404 rb_link_node(&symn->rb_node, parent, p); 405 rb_insert_color(&symn->rb_node, symbols); 406 } 407 408 static void symbols__sort_by_name(struct rb_root *symbols, 409 struct rb_root *source) 410 { 411 struct rb_node *nd; 412 413 for (nd = rb_first(source); nd; nd = rb_next(nd)) { 414 struct symbol *pos = rb_entry(nd, struct symbol, rb_node); 415 symbols__insert_by_name(symbols, pos); 416 } 417 } 418 419 int symbol__match_symbol_name(const char *name, const char *str, 420 enum symbol_tag_include includes) 421 { 422 const char *versioning; 423 424 if (includes == SYMBOL_TAG_INCLUDE__DEFAULT_ONLY && 425 (versioning = strstr(name, "@@"))) { 426 int len = strlen(str); 427 428 if (len < versioning - name) 429 len = versioning - name; 430 431 return arch__compare_symbol_names_n(name, str, len); 432 } else 433 return arch__compare_symbol_names(name, str); 434 } 435 436 static struct symbol *symbols__find_by_name(struct rb_root *symbols, 437 const char *name, 438 enum symbol_tag_include includes) 439 { 440 struct rb_node *n; 441 struct symbol_name_rb_node *s = NULL; 442 443 if (symbols == NULL) 444 return NULL; 445 446 n = symbols->rb_node; 447 448 while (n) { 449 int cmp; 450 451 s = rb_entry(n, struct symbol_name_rb_node, rb_node); 452 cmp = symbol__match_symbol_name(s->sym.name, name, includes); 453 454 if (cmp > 0) 455 n = n->rb_left; 456 else if (cmp < 0) 457 n = n->rb_right; 458 else 459 break; 460 } 461 462 if (n == NULL) 463 return NULL; 464 465 if (includes != SYMBOL_TAG_INCLUDE__DEFAULT_ONLY) 466 /* return first symbol that has same name (if any) */ 467 for (n = rb_prev(n); n; n = rb_prev(n)) { 468 struct symbol_name_rb_node *tmp; 469 470 tmp = rb_entry(n, struct symbol_name_rb_node, rb_node); 471 if (arch__compare_symbol_names(tmp->sym.name, s->sym.name)) 472 break; 473 474 s = tmp; 475 } 476 477 return &s->sym; 478 } 479 480 void dso__reset_find_symbol_cache(struct dso *dso) 481 { 482 enum map_type type; 483 484 for (type = MAP__FUNCTION; type <= MAP__VARIABLE; ++type) { 485 dso->last_find_result[type].addr = 0; 486 dso->last_find_result[type].symbol = NULL; 487 } 488 } 489 490 void dso__insert_symbol(struct dso *dso, enum map_type type, struct symbol *sym) 491 { 492 __symbols__insert(&dso->symbols[type], sym, dso->kernel); 493 494 /* update the symbol cache if necessary */ 495 if (dso->last_find_result[type].addr >= sym->start && 496 (dso->last_find_result[type].addr < sym->end || 497 sym->start == sym->end)) { 498 dso->last_find_result[type].symbol = sym; 499 } 500 } 501 502 struct symbol *dso__find_symbol(struct dso *dso, 503 enum map_type type, u64 addr) 504 { 505 if (dso->last_find_result[type].addr != addr || dso->last_find_result[type].symbol == NULL) { 506 dso->last_find_result[type].addr = addr; 507 dso->last_find_result[type].symbol = symbols__find(&dso->symbols[type], addr); 508 } 509 510 return dso->last_find_result[type].symbol; 511 } 512 513 struct symbol *dso__first_symbol(struct dso *dso, enum map_type type) 514 { 515 return symbols__first(&dso->symbols[type]); 516 } 517 518 struct symbol *dso__last_symbol(struct dso *dso, enum map_type type) 519 { 520 return symbols__last(&dso->symbols[type]); 521 } 522 523 struct symbol *dso__next_symbol(struct symbol *sym) 524 { 525 return symbols__next(sym); 526 } 527 528 struct symbol *symbol__next_by_name(struct symbol *sym) 529 { 530 struct symbol_name_rb_node *s = container_of(sym, struct symbol_name_rb_node, sym); 531 struct rb_node *n = rb_next(&s->rb_node); 532 533 return n ? &rb_entry(n, struct symbol_name_rb_node, rb_node)->sym : NULL; 534 } 535 536 /* 537 * Teturns first symbol that matched with @name. 538 */ 539 struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type, 540 const char *name) 541 { 542 struct symbol *s = symbols__find_by_name(&dso->symbol_names[type], name, 543 SYMBOL_TAG_INCLUDE__NONE); 544 if (!s) 545 s = symbols__find_by_name(&dso->symbol_names[type], name, 546 SYMBOL_TAG_INCLUDE__DEFAULT_ONLY); 547 return s; 548 } 549 550 void dso__sort_by_name(struct dso *dso, enum map_type type) 551 { 552 dso__set_sorted_by_name(dso, type); 553 return symbols__sort_by_name(&dso->symbol_names[type], 554 &dso->symbols[type]); 555 } 556 557 int modules__parse(const char *filename, void *arg, 558 int (*process_module)(void *arg, const char *name, 559 u64 start, u64 size)) 560 { 561 char *line = NULL; 562 size_t n; 563 FILE *file; 564 int err = 0; 565 566 file = fopen(filename, "r"); 567 if (file == NULL) 568 return -1; 569 570 while (1) { 571 char name[PATH_MAX]; 572 u64 start, size; 573 char *sep, *endptr; 574 ssize_t line_len; 575 576 line_len = getline(&line, &n, file); 577 if (line_len < 0) { 578 if (feof(file)) 579 break; 580 err = -1; 581 goto out; 582 } 583 584 if (!line) { 585 err = -1; 586 goto out; 587 } 588 589 line[--line_len] = '\0'; /* \n */ 590 591 sep = strrchr(line, 'x'); 592 if (sep == NULL) 593 continue; 594 595 hex2u64(sep + 1, &start); 596 597 sep = strchr(line, ' '); 598 if (sep == NULL) 599 continue; 600 601 *sep = '\0'; 602 603 scnprintf(name, sizeof(name), "[%s]", line); 604 605 size = strtoul(sep + 1, &endptr, 0); 606 if (*endptr != ' ' && *endptr != '\t') 607 continue; 608 609 err = process_module(arg, name, start, size); 610 if (err) 611 break; 612 } 613 out: 614 free(line); 615 fclose(file); 616 return err; 617 } 618 619 struct process_kallsyms_args { 620 struct map *map; 621 struct dso *dso; 622 }; 623 624 /* 625 * These are symbols in the kernel image, so make sure that 626 * sym is from a kernel DSO. 627 */ 628 static bool symbol__is_idle(const char *name) 629 { 630 const char * const idle_symbols[] = { 631 "cpu_idle", 632 "cpu_startup_entry", 633 "intel_idle", 634 "default_idle", 635 "native_safe_halt", 636 "enter_idle", 637 "exit_idle", 638 "mwait_idle", 639 "mwait_idle_with_hints", 640 "poll_idle", 641 "ppc64_runlatch_off", 642 "pseries_dedicated_idle_sleep", 643 NULL 644 }; 645 int i; 646 647 for (i = 0; idle_symbols[i]; i++) { 648 if (!strcmp(idle_symbols[i], name)) 649 return true; 650 } 651 652 return false; 653 } 654 655 static int map__process_kallsym_symbol(void *arg, const char *name, 656 char type, u64 start) 657 { 658 struct symbol *sym; 659 struct process_kallsyms_args *a = arg; 660 struct rb_root *root = &a->dso->symbols[a->map->type]; 661 662 if (!symbol_type__is_a(type, a->map->type)) 663 return 0; 664 665 /* 666 * module symbols are not sorted so we add all 667 * symbols, setting length to 0, and rely on 668 * symbols__fixup_end() to fix it up. 669 */ 670 sym = symbol__new(start, 0, kallsyms2elf_binding(type), name); 671 if (sym == NULL) 672 return -ENOMEM; 673 /* 674 * We will pass the symbols to the filter later, in 675 * map__split_kallsyms, when we have split the maps per module 676 */ 677 __symbols__insert(root, sym, !strchr(name, '[')); 678 679 return 0; 680 } 681 682 /* 683 * Loads the function entries in /proc/kallsyms into kernel_map->dso, 684 * so that we can in the next step set the symbol ->end address and then 685 * call kernel_maps__split_kallsyms. 686 */ 687 static int dso__load_all_kallsyms(struct dso *dso, const char *filename, 688 struct map *map) 689 { 690 struct process_kallsyms_args args = { .map = map, .dso = dso, }; 691 return kallsyms__parse(filename, &args, map__process_kallsym_symbol); 692 } 693 694 static int dso__split_kallsyms_for_kcore(struct dso *dso, struct map *map) 695 { 696 struct map_groups *kmaps = map__kmaps(map); 697 struct map *curr_map; 698 struct symbol *pos; 699 int count = 0; 700 struct rb_root old_root = dso->symbols[map->type]; 701 struct rb_root *root = &dso->symbols[map->type]; 702 struct rb_node *next = rb_first(root); 703 704 if (!kmaps) 705 return -1; 706 707 *root = RB_ROOT; 708 709 while (next) { 710 char *module; 711 712 pos = rb_entry(next, struct symbol, rb_node); 713 next = rb_next(&pos->rb_node); 714 715 rb_erase_init(&pos->rb_node, &old_root); 716 717 module = strchr(pos->name, '\t'); 718 if (module) 719 *module = '\0'; 720 721 curr_map = map_groups__find(kmaps, map->type, pos->start); 722 723 if (!curr_map) { 724 symbol__delete(pos); 725 continue; 726 } 727 728 pos->start -= curr_map->start - curr_map->pgoff; 729 if (pos->end) 730 pos->end -= curr_map->start - curr_map->pgoff; 731 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos); 732 ++count; 733 } 734 735 /* Symbols have been adjusted */ 736 dso->adjust_symbols = 1; 737 738 return count; 739 } 740 741 /* 742 * Split the symbols into maps, making sure there are no overlaps, i.e. the 743 * kernel range is broken in several maps, named [kernel].N, as we don't have 744 * the original ELF section names vmlinux have. 745 */ 746 static int dso__split_kallsyms(struct dso *dso, struct map *map, u64 delta) 747 { 748 struct map_groups *kmaps = map__kmaps(map); 749 struct machine *machine; 750 struct map *curr_map = map; 751 struct symbol *pos; 752 int count = 0, moved = 0; 753 struct rb_root *root = &dso->symbols[map->type]; 754 struct rb_node *next = rb_first(root); 755 int kernel_range = 0; 756 757 if (!kmaps) 758 return -1; 759 760 machine = kmaps->machine; 761 762 while (next) { 763 char *module; 764 765 pos = rb_entry(next, struct symbol, rb_node); 766 next = rb_next(&pos->rb_node); 767 768 module = strchr(pos->name, '\t'); 769 if (module) { 770 if (!symbol_conf.use_modules) 771 goto discard_symbol; 772 773 *module++ = '\0'; 774 775 if (strcmp(curr_map->dso->short_name, module)) { 776 if (curr_map != map && 777 dso->kernel == DSO_TYPE_GUEST_KERNEL && 778 machine__is_default_guest(machine)) { 779 /* 780 * We assume all symbols of a module are 781 * continuous in * kallsyms, so curr_map 782 * points to a module and all its 783 * symbols are in its kmap. Mark it as 784 * loaded. 785 */ 786 dso__set_loaded(curr_map->dso, 787 curr_map->type); 788 } 789 790 curr_map = map_groups__find_by_name(kmaps, 791 map->type, module); 792 if (curr_map == NULL) { 793 pr_debug("%s/proc/{kallsyms,modules} " 794 "inconsistency while looking " 795 "for \"%s\" module!\n", 796 machine->root_dir, module); 797 curr_map = map; 798 goto discard_symbol; 799 } 800 801 if (curr_map->dso->loaded && 802 !machine__is_default_guest(machine)) 803 goto discard_symbol; 804 } 805 /* 806 * So that we look just like we get from .ko files, 807 * i.e. not prelinked, relative to map->start. 808 */ 809 pos->start = curr_map->map_ip(curr_map, pos->start); 810 pos->end = curr_map->map_ip(curr_map, pos->end); 811 } else if (curr_map != map) { 812 char dso_name[PATH_MAX]; 813 struct dso *ndso; 814 815 if (delta) { 816 /* Kernel was relocated at boot time */ 817 pos->start -= delta; 818 pos->end -= delta; 819 } 820 821 if (count == 0) { 822 curr_map = map; 823 goto add_symbol; 824 } 825 826 if (dso->kernel == DSO_TYPE_GUEST_KERNEL) 827 snprintf(dso_name, sizeof(dso_name), 828 "[guest.kernel].%d", 829 kernel_range++); 830 else 831 snprintf(dso_name, sizeof(dso_name), 832 "[kernel].%d", 833 kernel_range++); 834 835 ndso = dso__new(dso_name); 836 if (ndso == NULL) 837 return -1; 838 839 ndso->kernel = dso->kernel; 840 841 curr_map = map__new2(pos->start, ndso, map->type); 842 if (curr_map == NULL) { 843 dso__put(ndso); 844 return -1; 845 } 846 847 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip; 848 map_groups__insert(kmaps, curr_map); 849 ++kernel_range; 850 } else if (delta) { 851 /* Kernel was relocated at boot time */ 852 pos->start -= delta; 853 pos->end -= delta; 854 } 855 add_symbol: 856 if (curr_map != map) { 857 rb_erase(&pos->rb_node, root); 858 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos); 859 ++moved; 860 } else 861 ++count; 862 863 continue; 864 discard_symbol: 865 rb_erase(&pos->rb_node, root); 866 symbol__delete(pos); 867 } 868 869 if (curr_map != map && 870 dso->kernel == DSO_TYPE_GUEST_KERNEL && 871 machine__is_default_guest(kmaps->machine)) { 872 dso__set_loaded(curr_map->dso, curr_map->type); 873 } 874 875 return count + moved; 876 } 877 878 bool symbol__restricted_filename(const char *filename, 879 const char *restricted_filename) 880 { 881 bool restricted = false; 882 883 if (symbol_conf.kptr_restrict) { 884 char *r = realpath(filename, NULL); 885 886 if (r != NULL) { 887 restricted = strcmp(r, restricted_filename) == 0; 888 free(r); 889 return restricted; 890 } 891 } 892 893 return restricted; 894 } 895 896 struct module_info { 897 struct rb_node rb_node; 898 char *name; 899 u64 start; 900 }; 901 902 static void add_module(struct module_info *mi, struct rb_root *modules) 903 { 904 struct rb_node **p = &modules->rb_node; 905 struct rb_node *parent = NULL; 906 struct module_info *m; 907 908 while (*p != NULL) { 909 parent = *p; 910 m = rb_entry(parent, struct module_info, rb_node); 911 if (strcmp(mi->name, m->name) < 0) 912 p = &(*p)->rb_left; 913 else 914 p = &(*p)->rb_right; 915 } 916 rb_link_node(&mi->rb_node, parent, p); 917 rb_insert_color(&mi->rb_node, modules); 918 } 919 920 static void delete_modules(struct rb_root *modules) 921 { 922 struct module_info *mi; 923 struct rb_node *next = rb_first(modules); 924 925 while (next) { 926 mi = rb_entry(next, struct module_info, rb_node); 927 next = rb_next(&mi->rb_node); 928 rb_erase(&mi->rb_node, modules); 929 zfree(&mi->name); 930 free(mi); 931 } 932 } 933 934 static struct module_info *find_module(const char *name, 935 struct rb_root *modules) 936 { 937 struct rb_node *n = modules->rb_node; 938 939 while (n) { 940 struct module_info *m; 941 int cmp; 942 943 m = rb_entry(n, struct module_info, rb_node); 944 cmp = strcmp(name, m->name); 945 if (cmp < 0) 946 n = n->rb_left; 947 else if (cmp > 0) 948 n = n->rb_right; 949 else 950 return m; 951 } 952 953 return NULL; 954 } 955 956 static int __read_proc_modules(void *arg, const char *name, u64 start, 957 u64 size __maybe_unused) 958 { 959 struct rb_root *modules = arg; 960 struct module_info *mi; 961 962 mi = zalloc(sizeof(struct module_info)); 963 if (!mi) 964 return -ENOMEM; 965 966 mi->name = strdup(name); 967 mi->start = start; 968 969 if (!mi->name) { 970 free(mi); 971 return -ENOMEM; 972 } 973 974 add_module(mi, modules); 975 976 return 0; 977 } 978 979 static int read_proc_modules(const char *filename, struct rb_root *modules) 980 { 981 if (symbol__restricted_filename(filename, "/proc/modules")) 982 return -1; 983 984 if (modules__parse(filename, modules, __read_proc_modules)) { 985 delete_modules(modules); 986 return -1; 987 } 988 989 return 0; 990 } 991 992 int compare_proc_modules(const char *from, const char *to) 993 { 994 struct rb_root from_modules = RB_ROOT; 995 struct rb_root to_modules = RB_ROOT; 996 struct rb_node *from_node, *to_node; 997 struct module_info *from_m, *to_m; 998 int ret = -1; 999 1000 if (read_proc_modules(from, &from_modules)) 1001 return -1; 1002 1003 if (read_proc_modules(to, &to_modules)) 1004 goto out_delete_from; 1005 1006 from_node = rb_first(&from_modules); 1007 to_node = rb_first(&to_modules); 1008 while (from_node) { 1009 if (!to_node) 1010 break; 1011 1012 from_m = rb_entry(from_node, struct module_info, rb_node); 1013 to_m = rb_entry(to_node, struct module_info, rb_node); 1014 1015 if (from_m->start != to_m->start || 1016 strcmp(from_m->name, to_m->name)) 1017 break; 1018 1019 from_node = rb_next(from_node); 1020 to_node = rb_next(to_node); 1021 } 1022 1023 if (!from_node && !to_node) 1024 ret = 0; 1025 1026 delete_modules(&to_modules); 1027 out_delete_from: 1028 delete_modules(&from_modules); 1029 1030 return ret; 1031 } 1032 1033 static int do_validate_kcore_modules(const char *filename, struct map *map, 1034 struct map_groups *kmaps) 1035 { 1036 struct rb_root modules = RB_ROOT; 1037 struct map *old_map; 1038 int err; 1039 1040 err = read_proc_modules(filename, &modules); 1041 if (err) 1042 return err; 1043 1044 old_map = map_groups__first(kmaps, map->type); 1045 while (old_map) { 1046 struct map *next = map_groups__next(old_map); 1047 struct module_info *mi; 1048 1049 if (old_map == map || old_map->start == map->start) { 1050 /* The kernel map */ 1051 old_map = next; 1052 continue; 1053 } 1054 1055 /* Module must be in memory at the same address */ 1056 mi = find_module(old_map->dso->short_name, &modules); 1057 if (!mi || mi->start != old_map->start) { 1058 err = -EINVAL; 1059 goto out; 1060 } 1061 1062 old_map = next; 1063 } 1064 out: 1065 delete_modules(&modules); 1066 return err; 1067 } 1068 1069 /* 1070 * If kallsyms is referenced by name then we look for filename in the same 1071 * directory. 1072 */ 1073 static bool filename_from_kallsyms_filename(char *filename, 1074 const char *base_name, 1075 const char *kallsyms_filename) 1076 { 1077 char *name; 1078 1079 strcpy(filename, kallsyms_filename); 1080 name = strrchr(filename, '/'); 1081 if (!name) 1082 return false; 1083 1084 name += 1; 1085 1086 if (!strcmp(name, "kallsyms")) { 1087 strcpy(name, base_name); 1088 return true; 1089 } 1090 1091 return false; 1092 } 1093 1094 static int validate_kcore_modules(const char *kallsyms_filename, 1095 struct map *map) 1096 { 1097 struct map_groups *kmaps = map__kmaps(map); 1098 char modules_filename[PATH_MAX]; 1099 1100 if (!kmaps) 1101 return -EINVAL; 1102 1103 if (!filename_from_kallsyms_filename(modules_filename, "modules", 1104 kallsyms_filename)) 1105 return -EINVAL; 1106 1107 if (do_validate_kcore_modules(modules_filename, map, kmaps)) 1108 return -EINVAL; 1109 1110 return 0; 1111 } 1112 1113 static int validate_kcore_addresses(const char *kallsyms_filename, 1114 struct map *map) 1115 { 1116 struct kmap *kmap = map__kmap(map); 1117 1118 if (!kmap) 1119 return -EINVAL; 1120 1121 if (kmap->ref_reloc_sym && kmap->ref_reloc_sym->name) { 1122 u64 start; 1123 1124 if (kallsyms__get_function_start(kallsyms_filename, 1125 kmap->ref_reloc_sym->name, &start)) 1126 return -ENOENT; 1127 if (start != kmap->ref_reloc_sym->addr) 1128 return -EINVAL; 1129 } 1130 1131 return validate_kcore_modules(kallsyms_filename, map); 1132 } 1133 1134 struct kcore_mapfn_data { 1135 struct dso *dso; 1136 enum map_type type; 1137 struct list_head maps; 1138 }; 1139 1140 static int kcore_mapfn(u64 start, u64 len, u64 pgoff, void *data) 1141 { 1142 struct kcore_mapfn_data *md = data; 1143 struct map *map; 1144 1145 map = map__new2(start, md->dso, md->type); 1146 if (map == NULL) 1147 return -ENOMEM; 1148 1149 map->end = map->start + len; 1150 map->pgoff = pgoff; 1151 1152 list_add(&map->node, &md->maps); 1153 1154 return 0; 1155 } 1156 1157 static int dso__load_kcore(struct dso *dso, struct map *map, 1158 const char *kallsyms_filename) 1159 { 1160 struct map_groups *kmaps = map__kmaps(map); 1161 struct machine *machine; 1162 struct kcore_mapfn_data md; 1163 struct map *old_map, *new_map, *replacement_map = NULL; 1164 bool is_64_bit; 1165 int err, fd; 1166 char kcore_filename[PATH_MAX]; 1167 struct symbol *sym; 1168 1169 if (!kmaps) 1170 return -EINVAL; 1171 1172 machine = kmaps->machine; 1173 1174 /* This function requires that the map is the kernel map */ 1175 if (map != machine->vmlinux_maps[map->type]) 1176 return -EINVAL; 1177 1178 if (!filename_from_kallsyms_filename(kcore_filename, "kcore", 1179 kallsyms_filename)) 1180 return -EINVAL; 1181 1182 /* Modules and kernel must be present at their original addresses */ 1183 if (validate_kcore_addresses(kallsyms_filename, map)) 1184 return -EINVAL; 1185 1186 md.dso = dso; 1187 md.type = map->type; 1188 INIT_LIST_HEAD(&md.maps); 1189 1190 fd = open(kcore_filename, O_RDONLY); 1191 if (fd < 0) { 1192 pr_debug("Failed to open %s. Note /proc/kcore requires CAP_SYS_RAWIO capability to access.\n", 1193 kcore_filename); 1194 return -EINVAL; 1195 } 1196 1197 /* Read new maps into temporary lists */ 1198 err = file__read_maps(fd, md.type == MAP__FUNCTION, kcore_mapfn, &md, 1199 &is_64_bit); 1200 if (err) 1201 goto out_err; 1202 dso->is_64_bit = is_64_bit; 1203 1204 if (list_empty(&md.maps)) { 1205 err = -EINVAL; 1206 goto out_err; 1207 } 1208 1209 /* Remove old maps */ 1210 old_map = map_groups__first(kmaps, map->type); 1211 while (old_map) { 1212 struct map *next = map_groups__next(old_map); 1213 1214 if (old_map != map) 1215 map_groups__remove(kmaps, old_map); 1216 old_map = next; 1217 } 1218 1219 /* Find the kernel map using the first symbol */ 1220 sym = dso__first_symbol(dso, map->type); 1221 list_for_each_entry(new_map, &md.maps, node) { 1222 if (sym && sym->start >= new_map->start && 1223 sym->start < new_map->end) { 1224 replacement_map = new_map; 1225 break; 1226 } 1227 } 1228 1229 if (!replacement_map) 1230 replacement_map = list_entry(md.maps.next, struct map, node); 1231 1232 /* Add new maps */ 1233 while (!list_empty(&md.maps)) { 1234 new_map = list_entry(md.maps.next, struct map, node); 1235 list_del_init(&new_map->node); 1236 if (new_map == replacement_map) { 1237 map->start = new_map->start; 1238 map->end = new_map->end; 1239 map->pgoff = new_map->pgoff; 1240 map->map_ip = new_map->map_ip; 1241 map->unmap_ip = new_map->unmap_ip; 1242 /* Ensure maps are correctly ordered */ 1243 map__get(map); 1244 map_groups__remove(kmaps, map); 1245 map_groups__insert(kmaps, map); 1246 map__put(map); 1247 } else { 1248 map_groups__insert(kmaps, new_map); 1249 } 1250 1251 map__put(new_map); 1252 } 1253 1254 /* 1255 * Set the data type and long name so that kcore can be read via 1256 * dso__data_read_addr(). 1257 */ 1258 if (dso->kernel == DSO_TYPE_GUEST_KERNEL) 1259 dso->binary_type = DSO_BINARY_TYPE__GUEST_KCORE; 1260 else 1261 dso->binary_type = DSO_BINARY_TYPE__KCORE; 1262 dso__set_long_name(dso, strdup(kcore_filename), true); 1263 1264 close(fd); 1265 1266 if (map->type == MAP__FUNCTION) 1267 pr_debug("Using %s for kernel object code\n", kcore_filename); 1268 else 1269 pr_debug("Using %s for kernel data\n", kcore_filename); 1270 1271 return 0; 1272 1273 out_err: 1274 while (!list_empty(&md.maps)) { 1275 map = list_entry(md.maps.next, struct map, node); 1276 list_del_init(&map->node); 1277 map__put(map); 1278 } 1279 close(fd); 1280 return -EINVAL; 1281 } 1282 1283 /* 1284 * If the kernel is relocated at boot time, kallsyms won't match. Compute the 1285 * delta based on the relocation reference symbol. 1286 */ 1287 static int kallsyms__delta(struct map *map, const char *filename, u64 *delta) 1288 { 1289 struct kmap *kmap = map__kmap(map); 1290 u64 addr; 1291 1292 if (!kmap) 1293 return -1; 1294 1295 if (!kmap->ref_reloc_sym || !kmap->ref_reloc_sym->name) 1296 return 0; 1297 1298 if (kallsyms__get_function_start(filename, kmap->ref_reloc_sym->name, &addr)) 1299 return -1; 1300 1301 *delta = addr - kmap->ref_reloc_sym->addr; 1302 return 0; 1303 } 1304 1305 int __dso__load_kallsyms(struct dso *dso, const char *filename, 1306 struct map *map, bool no_kcore) 1307 { 1308 u64 delta = 0; 1309 1310 if (symbol__restricted_filename(filename, "/proc/kallsyms")) 1311 return -1; 1312 1313 if (dso__load_all_kallsyms(dso, filename, map) < 0) 1314 return -1; 1315 1316 if (kallsyms__delta(map, filename, &delta)) 1317 return -1; 1318 1319 symbols__fixup_end(&dso->symbols[map->type]); 1320 symbols__fixup_duplicate(&dso->symbols[map->type]); 1321 1322 if (dso->kernel == DSO_TYPE_GUEST_KERNEL) 1323 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KALLSYMS; 1324 else 1325 dso->symtab_type = DSO_BINARY_TYPE__KALLSYMS; 1326 1327 if (!no_kcore && !dso__load_kcore(dso, map, filename)) 1328 return dso__split_kallsyms_for_kcore(dso, map); 1329 else 1330 return dso__split_kallsyms(dso, map, delta); 1331 } 1332 1333 int dso__load_kallsyms(struct dso *dso, const char *filename, 1334 struct map *map) 1335 { 1336 return __dso__load_kallsyms(dso, filename, map, false); 1337 } 1338 1339 static int dso__load_perf_map(const char *map_path, struct dso *dso, 1340 struct map *map) 1341 { 1342 char *line = NULL; 1343 size_t n; 1344 FILE *file; 1345 int nr_syms = 0; 1346 1347 file = fopen(map_path, "r"); 1348 if (file == NULL) 1349 goto out_failure; 1350 1351 while (!feof(file)) { 1352 u64 start, size; 1353 struct symbol *sym; 1354 int line_len, len; 1355 1356 line_len = getline(&line, &n, file); 1357 if (line_len < 0) 1358 break; 1359 1360 if (!line) 1361 goto out_failure; 1362 1363 line[--line_len] = '\0'; /* \n */ 1364 1365 len = hex2u64(line, &start); 1366 1367 len++; 1368 if (len + 2 >= line_len) 1369 continue; 1370 1371 len += hex2u64(line + len, &size); 1372 1373 len++; 1374 if (len + 2 >= line_len) 1375 continue; 1376 1377 sym = symbol__new(start, size, STB_GLOBAL, line + len); 1378 1379 if (sym == NULL) 1380 goto out_delete_line; 1381 1382 symbols__insert(&dso->symbols[map->type], sym); 1383 nr_syms++; 1384 } 1385 1386 free(line); 1387 fclose(file); 1388 1389 return nr_syms; 1390 1391 out_delete_line: 1392 free(line); 1393 out_failure: 1394 return -1; 1395 } 1396 1397 static bool dso__is_compatible_symtab_type(struct dso *dso, bool kmod, 1398 enum dso_binary_type type) 1399 { 1400 switch (type) { 1401 case DSO_BINARY_TYPE__JAVA_JIT: 1402 case DSO_BINARY_TYPE__DEBUGLINK: 1403 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO: 1404 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO: 1405 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO: 1406 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO: 1407 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO: 1408 return !kmod && dso->kernel == DSO_TYPE_USER; 1409 1410 case DSO_BINARY_TYPE__KALLSYMS: 1411 case DSO_BINARY_TYPE__VMLINUX: 1412 case DSO_BINARY_TYPE__KCORE: 1413 return dso->kernel == DSO_TYPE_KERNEL; 1414 1415 case DSO_BINARY_TYPE__GUEST_KALLSYMS: 1416 case DSO_BINARY_TYPE__GUEST_VMLINUX: 1417 case DSO_BINARY_TYPE__GUEST_KCORE: 1418 return dso->kernel == DSO_TYPE_GUEST_KERNEL; 1419 1420 case DSO_BINARY_TYPE__GUEST_KMODULE: 1421 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP: 1422 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE: 1423 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP: 1424 /* 1425 * kernel modules know their symtab type - it's set when 1426 * creating a module dso in machine__findnew_module_map(). 1427 */ 1428 return kmod && dso->symtab_type == type; 1429 1430 case DSO_BINARY_TYPE__BUILD_ID_CACHE: 1431 case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO: 1432 return true; 1433 1434 case DSO_BINARY_TYPE__NOT_FOUND: 1435 default: 1436 return false; 1437 } 1438 } 1439 1440 /* Checks for the existence of the perf-<pid>.map file in two different 1441 * locations. First, if the process is a separate mount namespace, check in 1442 * that namespace using the pid of the innermost pid namespace. If's not in a 1443 * namespace, or the file can't be found there, try in the mount namespace of 1444 * the tracing process using our view of its pid. 1445 */ 1446 static int dso__find_perf_map(char *filebuf, size_t bufsz, 1447 struct nsinfo **nsip) 1448 { 1449 struct nscookie nsc; 1450 struct nsinfo *nsi; 1451 struct nsinfo *nnsi; 1452 int rc = -1; 1453 1454 nsi = *nsip; 1455 1456 if (nsi->need_setns) { 1457 snprintf(filebuf, bufsz, "/tmp/perf-%d.map", nsi->nstgid); 1458 nsinfo__mountns_enter(nsi, &nsc); 1459 rc = access(filebuf, R_OK); 1460 nsinfo__mountns_exit(&nsc); 1461 if (rc == 0) 1462 return rc; 1463 } 1464 1465 nnsi = nsinfo__copy(nsi); 1466 if (nnsi) { 1467 nsinfo__put(nsi); 1468 1469 nnsi->need_setns = false; 1470 snprintf(filebuf, bufsz, "/tmp/perf-%d.map", nnsi->tgid); 1471 *nsip = nnsi; 1472 rc = 0; 1473 } 1474 1475 return rc; 1476 } 1477 1478 int dso__load(struct dso *dso, struct map *map) 1479 { 1480 char *name; 1481 int ret = -1; 1482 u_int i; 1483 struct machine *machine; 1484 char *root_dir = (char *) ""; 1485 int ss_pos = 0; 1486 struct symsrc ss_[2]; 1487 struct symsrc *syms_ss = NULL, *runtime_ss = NULL; 1488 bool kmod; 1489 bool perfmap; 1490 unsigned char build_id[BUILD_ID_SIZE]; 1491 struct nscookie nsc; 1492 char newmapname[PATH_MAX]; 1493 const char *map_path = dso->long_name; 1494 1495 perfmap = strncmp(dso->name, "/tmp/perf-", 10) == 0; 1496 if (perfmap) { 1497 if (dso->nsinfo && (dso__find_perf_map(newmapname, 1498 sizeof(newmapname), &dso->nsinfo) == 0)) { 1499 map_path = newmapname; 1500 } 1501 } 1502 1503 nsinfo__mountns_enter(dso->nsinfo, &nsc); 1504 pthread_mutex_lock(&dso->lock); 1505 1506 /* check again under the dso->lock */ 1507 if (dso__loaded(dso, map->type)) { 1508 ret = 1; 1509 goto out; 1510 } 1511 1512 if (dso->kernel) { 1513 if (dso->kernel == DSO_TYPE_KERNEL) 1514 ret = dso__load_kernel_sym(dso, map); 1515 else if (dso->kernel == DSO_TYPE_GUEST_KERNEL) 1516 ret = dso__load_guest_kernel_sym(dso, map); 1517 1518 goto out; 1519 } 1520 1521 if (map->groups && map->groups->machine) 1522 machine = map->groups->machine; 1523 else 1524 machine = NULL; 1525 1526 dso->adjust_symbols = 0; 1527 1528 if (perfmap) { 1529 struct stat st; 1530 1531 if (lstat(map_path, &st) < 0) 1532 goto out; 1533 1534 if (!symbol_conf.force && st.st_uid && (st.st_uid != geteuid())) { 1535 pr_warning("File %s not owned by current user or root, " 1536 "ignoring it (use -f to override).\n", map_path); 1537 goto out; 1538 } 1539 1540 ret = dso__load_perf_map(map_path, dso, map); 1541 dso->symtab_type = ret > 0 ? DSO_BINARY_TYPE__JAVA_JIT : 1542 DSO_BINARY_TYPE__NOT_FOUND; 1543 goto out; 1544 } 1545 1546 if (machine) 1547 root_dir = machine->root_dir; 1548 1549 name = malloc(PATH_MAX); 1550 if (!name) 1551 goto out; 1552 1553 kmod = dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE || 1554 dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP || 1555 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE || 1556 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP; 1557 1558 1559 /* 1560 * Read the build id if possible. This is required for 1561 * DSO_BINARY_TYPE__BUILDID_DEBUGINFO to work 1562 */ 1563 if (!dso->has_build_id && 1564 is_regular_file(dso->long_name)) { 1565 __symbol__join_symfs(name, PATH_MAX, dso->long_name); 1566 if (filename__read_build_id(name, build_id, BUILD_ID_SIZE) > 0) 1567 dso__set_build_id(dso, build_id); 1568 } 1569 1570 /* 1571 * Iterate over candidate debug images. 1572 * Keep track of "interesting" ones (those which have a symtab, dynsym, 1573 * and/or opd section) for processing. 1574 */ 1575 for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) { 1576 struct symsrc *ss = &ss_[ss_pos]; 1577 bool next_slot = false; 1578 bool is_reg; 1579 bool nsexit; 1580 int sirc; 1581 1582 enum dso_binary_type symtab_type = binary_type_symtab[i]; 1583 1584 nsexit = (symtab_type == DSO_BINARY_TYPE__BUILD_ID_CACHE || 1585 symtab_type == DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO); 1586 1587 if (!dso__is_compatible_symtab_type(dso, kmod, symtab_type)) 1588 continue; 1589 1590 if (dso__read_binary_type_filename(dso, symtab_type, 1591 root_dir, name, PATH_MAX)) 1592 continue; 1593 1594 if (nsexit) 1595 nsinfo__mountns_exit(&nsc); 1596 1597 is_reg = is_regular_file(name); 1598 sirc = symsrc__init(ss, dso, name, symtab_type); 1599 1600 if (nsexit) 1601 nsinfo__mountns_enter(dso->nsinfo, &nsc); 1602 1603 if (!is_reg || sirc < 0) { 1604 if (sirc >= 0) 1605 symsrc__destroy(ss); 1606 continue; 1607 } 1608 1609 if (!syms_ss && symsrc__has_symtab(ss)) { 1610 syms_ss = ss; 1611 next_slot = true; 1612 if (!dso->symsrc_filename) 1613 dso->symsrc_filename = strdup(name); 1614 } 1615 1616 if (!runtime_ss && symsrc__possibly_runtime(ss)) { 1617 runtime_ss = ss; 1618 next_slot = true; 1619 } 1620 1621 if (next_slot) { 1622 ss_pos++; 1623 1624 if (syms_ss && runtime_ss) 1625 break; 1626 } else { 1627 symsrc__destroy(ss); 1628 } 1629 1630 } 1631 1632 if (!runtime_ss && !syms_ss) 1633 goto out_free; 1634 1635 if (runtime_ss && !syms_ss) { 1636 syms_ss = runtime_ss; 1637 } 1638 1639 /* We'll have to hope for the best */ 1640 if (!runtime_ss && syms_ss) 1641 runtime_ss = syms_ss; 1642 1643 if (syms_ss) 1644 ret = dso__load_sym(dso, map, syms_ss, runtime_ss, kmod); 1645 else 1646 ret = -1; 1647 1648 if (ret > 0) { 1649 int nr_plt; 1650 1651 nr_plt = dso__synthesize_plt_symbols(dso, runtime_ss, map); 1652 if (nr_plt > 0) 1653 ret += nr_plt; 1654 } 1655 1656 for (; ss_pos > 0; ss_pos--) 1657 symsrc__destroy(&ss_[ss_pos - 1]); 1658 out_free: 1659 free(name); 1660 if (ret < 0 && strstr(dso->name, " (deleted)") != NULL) 1661 ret = 0; 1662 out: 1663 dso__set_loaded(dso, map->type); 1664 pthread_mutex_unlock(&dso->lock); 1665 nsinfo__mountns_exit(&nsc); 1666 1667 return ret; 1668 } 1669 1670 struct map *map_groups__find_by_name(struct map_groups *mg, 1671 enum map_type type, const char *name) 1672 { 1673 struct maps *maps = &mg->maps[type]; 1674 struct map *map; 1675 1676 down_read(&maps->lock); 1677 1678 for (map = maps__first(maps); map; map = map__next(map)) { 1679 if (map->dso && strcmp(map->dso->short_name, name) == 0) 1680 goto out_unlock; 1681 } 1682 1683 map = NULL; 1684 1685 out_unlock: 1686 up_read(&maps->lock); 1687 return map; 1688 } 1689 1690 int dso__load_vmlinux(struct dso *dso, struct map *map, 1691 const char *vmlinux, bool vmlinux_allocated) 1692 { 1693 int err = -1; 1694 struct symsrc ss; 1695 char symfs_vmlinux[PATH_MAX]; 1696 enum dso_binary_type symtab_type; 1697 1698 if (vmlinux[0] == '/') 1699 snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s", vmlinux); 1700 else 1701 symbol__join_symfs(symfs_vmlinux, vmlinux); 1702 1703 if (dso->kernel == DSO_TYPE_GUEST_KERNEL) 1704 symtab_type = DSO_BINARY_TYPE__GUEST_VMLINUX; 1705 else 1706 symtab_type = DSO_BINARY_TYPE__VMLINUX; 1707 1708 if (symsrc__init(&ss, dso, symfs_vmlinux, symtab_type)) 1709 return -1; 1710 1711 err = dso__load_sym(dso, map, &ss, &ss, 0); 1712 symsrc__destroy(&ss); 1713 1714 if (err > 0) { 1715 if (dso->kernel == DSO_TYPE_GUEST_KERNEL) 1716 dso->binary_type = DSO_BINARY_TYPE__GUEST_VMLINUX; 1717 else 1718 dso->binary_type = DSO_BINARY_TYPE__VMLINUX; 1719 dso__set_long_name(dso, vmlinux, vmlinux_allocated); 1720 dso__set_loaded(dso, map->type); 1721 pr_debug("Using %s for symbols\n", symfs_vmlinux); 1722 } 1723 1724 return err; 1725 } 1726 1727 int dso__load_vmlinux_path(struct dso *dso, struct map *map) 1728 { 1729 int i, err = 0; 1730 char *filename = NULL; 1731 1732 pr_debug("Looking at the vmlinux_path (%d entries long)\n", 1733 vmlinux_path__nr_entries + 1); 1734 1735 for (i = 0; i < vmlinux_path__nr_entries; ++i) { 1736 err = dso__load_vmlinux(dso, map, vmlinux_path[i], false); 1737 if (err > 0) 1738 goto out; 1739 } 1740 1741 if (!symbol_conf.ignore_vmlinux_buildid) 1742 filename = dso__build_id_filename(dso, NULL, 0, false); 1743 if (filename != NULL) { 1744 err = dso__load_vmlinux(dso, map, filename, true); 1745 if (err > 0) 1746 goto out; 1747 free(filename); 1748 } 1749 out: 1750 return err; 1751 } 1752 1753 static bool visible_dir_filter(const char *name, struct dirent *d) 1754 { 1755 if (d->d_type != DT_DIR) 1756 return false; 1757 return lsdir_no_dot_filter(name, d); 1758 } 1759 1760 static int find_matching_kcore(struct map *map, char *dir, size_t dir_sz) 1761 { 1762 char kallsyms_filename[PATH_MAX]; 1763 int ret = -1; 1764 struct strlist *dirs; 1765 struct str_node *nd; 1766 1767 dirs = lsdir(dir, visible_dir_filter); 1768 if (!dirs) 1769 return -1; 1770 1771 strlist__for_each_entry(nd, dirs) { 1772 scnprintf(kallsyms_filename, sizeof(kallsyms_filename), 1773 "%s/%s/kallsyms", dir, nd->s); 1774 if (!validate_kcore_addresses(kallsyms_filename, map)) { 1775 strlcpy(dir, kallsyms_filename, dir_sz); 1776 ret = 0; 1777 break; 1778 } 1779 } 1780 1781 strlist__delete(dirs); 1782 1783 return ret; 1784 } 1785 1786 /* 1787 * Use open(O_RDONLY) to check readability directly instead of access(R_OK) 1788 * since access(R_OK) only checks with real UID/GID but open() use effective 1789 * UID/GID and actual capabilities (e.g. /proc/kcore requires CAP_SYS_RAWIO). 1790 */ 1791 static bool filename__readable(const char *file) 1792 { 1793 int fd = open(file, O_RDONLY); 1794 if (fd < 0) 1795 return false; 1796 close(fd); 1797 return true; 1798 } 1799 1800 static char *dso__find_kallsyms(struct dso *dso, struct map *map) 1801 { 1802 u8 host_build_id[BUILD_ID_SIZE]; 1803 char sbuild_id[SBUILD_ID_SIZE]; 1804 bool is_host = false; 1805 char path[PATH_MAX]; 1806 1807 if (!dso->has_build_id) { 1808 /* 1809 * Last resort, if we don't have a build-id and couldn't find 1810 * any vmlinux file, try the running kernel kallsyms table. 1811 */ 1812 goto proc_kallsyms; 1813 } 1814 1815 if (sysfs__read_build_id("/sys/kernel/notes", host_build_id, 1816 sizeof(host_build_id)) == 0) 1817 is_host = dso__build_id_equal(dso, host_build_id); 1818 1819 /* Try a fast path for /proc/kallsyms if possible */ 1820 if (is_host) { 1821 /* 1822 * Do not check the build-id cache, unless we know we cannot use 1823 * /proc/kcore or module maps don't match to /proc/kallsyms. 1824 * To check readability of /proc/kcore, do not use access(R_OK) 1825 * since /proc/kcore requires CAP_SYS_RAWIO to read and access 1826 * can't check it. 1827 */ 1828 if (filename__readable("/proc/kcore") && 1829 !validate_kcore_addresses("/proc/kallsyms", map)) 1830 goto proc_kallsyms; 1831 } 1832 1833 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id); 1834 1835 /* Find kallsyms in build-id cache with kcore */ 1836 scnprintf(path, sizeof(path), "%s/%s/%s", 1837 buildid_dir, DSO__NAME_KCORE, sbuild_id); 1838 1839 if (!find_matching_kcore(map, path, sizeof(path))) 1840 return strdup(path); 1841 1842 /* Use current /proc/kallsyms if possible */ 1843 if (is_host) { 1844 proc_kallsyms: 1845 return strdup("/proc/kallsyms"); 1846 } 1847 1848 /* Finally, find a cache of kallsyms */ 1849 if (!build_id_cache__kallsyms_path(sbuild_id, path, sizeof(path))) { 1850 pr_err("No kallsyms or vmlinux with build-id %s was found\n", 1851 sbuild_id); 1852 return NULL; 1853 } 1854 1855 return strdup(path); 1856 } 1857 1858 static int dso__load_kernel_sym(struct dso *dso, struct map *map) 1859 { 1860 int err; 1861 const char *kallsyms_filename = NULL; 1862 char *kallsyms_allocated_filename = NULL; 1863 /* 1864 * Step 1: if the user specified a kallsyms or vmlinux filename, use 1865 * it and only it, reporting errors to the user if it cannot be used. 1866 * 1867 * For instance, try to analyse an ARM perf.data file _without_ a 1868 * build-id, or if the user specifies the wrong path to the right 1869 * vmlinux file, obviously we can't fallback to another vmlinux (a 1870 * x86_86 one, on the machine where analysis is being performed, say), 1871 * or worse, /proc/kallsyms. 1872 * 1873 * If the specified file _has_ a build-id and there is a build-id 1874 * section in the perf.data file, we will still do the expected 1875 * validation in dso__load_vmlinux and will bail out if they don't 1876 * match. 1877 */ 1878 if (symbol_conf.kallsyms_name != NULL) { 1879 kallsyms_filename = symbol_conf.kallsyms_name; 1880 goto do_kallsyms; 1881 } 1882 1883 if (!symbol_conf.ignore_vmlinux && symbol_conf.vmlinux_name != NULL) { 1884 return dso__load_vmlinux(dso, map, symbol_conf.vmlinux_name, false); 1885 } 1886 1887 if (!symbol_conf.ignore_vmlinux && vmlinux_path != NULL) { 1888 err = dso__load_vmlinux_path(dso, map); 1889 if (err > 0) 1890 return err; 1891 } 1892 1893 /* do not try local files if a symfs was given */ 1894 if (symbol_conf.symfs[0] != 0) 1895 return -1; 1896 1897 kallsyms_allocated_filename = dso__find_kallsyms(dso, map); 1898 if (!kallsyms_allocated_filename) 1899 return -1; 1900 1901 kallsyms_filename = kallsyms_allocated_filename; 1902 1903 do_kallsyms: 1904 err = dso__load_kallsyms(dso, kallsyms_filename, map); 1905 if (err > 0) 1906 pr_debug("Using %s for symbols\n", kallsyms_filename); 1907 free(kallsyms_allocated_filename); 1908 1909 if (err > 0 && !dso__is_kcore(dso)) { 1910 dso->binary_type = DSO_BINARY_TYPE__KALLSYMS; 1911 dso__set_long_name(dso, DSO__NAME_KALLSYMS, false); 1912 map__fixup_start(map); 1913 map__fixup_end(map); 1914 } 1915 1916 return err; 1917 } 1918 1919 static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map) 1920 { 1921 int err; 1922 const char *kallsyms_filename = NULL; 1923 struct machine *machine; 1924 char path[PATH_MAX]; 1925 1926 if (!map->groups) { 1927 pr_debug("Guest kernel map hasn't the point to groups\n"); 1928 return -1; 1929 } 1930 machine = map->groups->machine; 1931 1932 if (machine__is_default_guest(machine)) { 1933 /* 1934 * if the user specified a vmlinux filename, use it and only 1935 * it, reporting errors to the user if it cannot be used. 1936 * Or use file guest_kallsyms inputted by user on commandline 1937 */ 1938 if (symbol_conf.default_guest_vmlinux_name != NULL) { 1939 err = dso__load_vmlinux(dso, map, 1940 symbol_conf.default_guest_vmlinux_name, 1941 false); 1942 return err; 1943 } 1944 1945 kallsyms_filename = symbol_conf.default_guest_kallsyms; 1946 if (!kallsyms_filename) 1947 return -1; 1948 } else { 1949 sprintf(path, "%s/proc/kallsyms", machine->root_dir); 1950 kallsyms_filename = path; 1951 } 1952 1953 err = dso__load_kallsyms(dso, kallsyms_filename, map); 1954 if (err > 0) 1955 pr_debug("Using %s for symbols\n", kallsyms_filename); 1956 if (err > 0 && !dso__is_kcore(dso)) { 1957 dso->binary_type = DSO_BINARY_TYPE__GUEST_KALLSYMS; 1958 machine__mmap_name(machine, path, sizeof(path)); 1959 dso__set_long_name(dso, strdup(path), true); 1960 map__fixup_start(map); 1961 map__fixup_end(map); 1962 } 1963 1964 return err; 1965 } 1966 1967 static void vmlinux_path__exit(void) 1968 { 1969 while (--vmlinux_path__nr_entries >= 0) 1970 zfree(&vmlinux_path[vmlinux_path__nr_entries]); 1971 vmlinux_path__nr_entries = 0; 1972 1973 zfree(&vmlinux_path); 1974 } 1975 1976 static const char * const vmlinux_paths[] = { 1977 "vmlinux", 1978 "/boot/vmlinux" 1979 }; 1980 1981 static const char * const vmlinux_paths_upd[] = { 1982 "/boot/vmlinux-%s", 1983 "/usr/lib/debug/boot/vmlinux-%s", 1984 "/lib/modules/%s/build/vmlinux", 1985 "/usr/lib/debug/lib/modules/%s/vmlinux", 1986 "/usr/lib/debug/boot/vmlinux-%s.debug" 1987 }; 1988 1989 static int vmlinux_path__add(const char *new_entry) 1990 { 1991 vmlinux_path[vmlinux_path__nr_entries] = strdup(new_entry); 1992 if (vmlinux_path[vmlinux_path__nr_entries] == NULL) 1993 return -1; 1994 ++vmlinux_path__nr_entries; 1995 1996 return 0; 1997 } 1998 1999 static int vmlinux_path__init(struct perf_env *env) 2000 { 2001 struct utsname uts; 2002 char bf[PATH_MAX]; 2003 char *kernel_version; 2004 unsigned int i; 2005 2006 vmlinux_path = malloc(sizeof(char *) * (ARRAY_SIZE(vmlinux_paths) + 2007 ARRAY_SIZE(vmlinux_paths_upd))); 2008 if (vmlinux_path == NULL) 2009 return -1; 2010 2011 for (i = 0; i < ARRAY_SIZE(vmlinux_paths); i++) 2012 if (vmlinux_path__add(vmlinux_paths[i]) < 0) 2013 goto out_fail; 2014 2015 /* only try kernel version if no symfs was given */ 2016 if (symbol_conf.symfs[0] != 0) 2017 return 0; 2018 2019 if (env) { 2020 kernel_version = env->os_release; 2021 } else { 2022 if (uname(&uts) < 0) 2023 goto out_fail; 2024 2025 kernel_version = uts.release; 2026 } 2027 2028 for (i = 0; i < ARRAY_SIZE(vmlinux_paths_upd); i++) { 2029 snprintf(bf, sizeof(bf), vmlinux_paths_upd[i], kernel_version); 2030 if (vmlinux_path__add(bf) < 0) 2031 goto out_fail; 2032 } 2033 2034 return 0; 2035 2036 out_fail: 2037 vmlinux_path__exit(); 2038 return -1; 2039 } 2040 2041 int setup_list(struct strlist **list, const char *list_str, 2042 const char *list_name) 2043 { 2044 if (list_str == NULL) 2045 return 0; 2046 2047 *list = strlist__new(list_str, NULL); 2048 if (!*list) { 2049 pr_err("problems parsing %s list\n", list_name); 2050 return -1; 2051 } 2052 2053 symbol_conf.has_filter = true; 2054 return 0; 2055 } 2056 2057 int setup_intlist(struct intlist **list, const char *list_str, 2058 const char *list_name) 2059 { 2060 if (list_str == NULL) 2061 return 0; 2062 2063 *list = intlist__new(list_str); 2064 if (!*list) { 2065 pr_err("problems parsing %s list\n", list_name); 2066 return -1; 2067 } 2068 return 0; 2069 } 2070 2071 static bool symbol__read_kptr_restrict(void) 2072 { 2073 bool value = false; 2074 FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r"); 2075 2076 if (fp != NULL) { 2077 char line[8]; 2078 2079 if (fgets(line, sizeof(line), fp) != NULL) 2080 value = ((geteuid() != 0) || (getuid() != 0)) ? 2081 (atoi(line) != 0) : 2082 (atoi(line) == 2); 2083 2084 fclose(fp); 2085 } 2086 2087 return value; 2088 } 2089 2090 int symbol__annotation_init(void) 2091 { 2092 if (symbol_conf.initialized) { 2093 pr_err("Annotation needs to be init before symbol__init()\n"); 2094 return -1; 2095 } 2096 2097 if (symbol_conf.init_annotation) { 2098 pr_warning("Annotation being initialized multiple times\n"); 2099 return 0; 2100 } 2101 2102 symbol_conf.priv_size += sizeof(struct annotation); 2103 symbol_conf.init_annotation = true; 2104 return 0; 2105 } 2106 2107 int symbol__init(struct perf_env *env) 2108 { 2109 const char *symfs; 2110 2111 if (symbol_conf.initialized) 2112 return 0; 2113 2114 symbol_conf.priv_size = PERF_ALIGN(symbol_conf.priv_size, sizeof(u64)); 2115 2116 symbol__elf_init(); 2117 2118 if (symbol_conf.sort_by_name) 2119 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) - 2120 sizeof(struct symbol)); 2121 2122 if (symbol_conf.try_vmlinux_path && vmlinux_path__init(env) < 0) 2123 return -1; 2124 2125 if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') { 2126 pr_err("'.' is the only non valid --field-separator argument\n"); 2127 return -1; 2128 } 2129 2130 if (setup_list(&symbol_conf.dso_list, 2131 symbol_conf.dso_list_str, "dso") < 0) 2132 return -1; 2133 2134 if (setup_list(&symbol_conf.comm_list, 2135 symbol_conf.comm_list_str, "comm") < 0) 2136 goto out_free_dso_list; 2137 2138 if (setup_intlist(&symbol_conf.pid_list, 2139 symbol_conf.pid_list_str, "pid") < 0) 2140 goto out_free_comm_list; 2141 2142 if (setup_intlist(&symbol_conf.tid_list, 2143 symbol_conf.tid_list_str, "tid") < 0) 2144 goto out_free_pid_list; 2145 2146 if (setup_list(&symbol_conf.sym_list, 2147 symbol_conf.sym_list_str, "symbol") < 0) 2148 goto out_free_tid_list; 2149 2150 if (setup_list(&symbol_conf.bt_stop_list, 2151 symbol_conf.bt_stop_list_str, "symbol") < 0) 2152 goto out_free_sym_list; 2153 2154 /* 2155 * A path to symbols of "/" is identical to "" 2156 * reset here for simplicity. 2157 */ 2158 symfs = realpath(symbol_conf.symfs, NULL); 2159 if (symfs == NULL) 2160 symfs = symbol_conf.symfs; 2161 if (strcmp(symfs, "/") == 0) 2162 symbol_conf.symfs = ""; 2163 if (symfs != symbol_conf.symfs) 2164 free((void *)symfs); 2165 2166 symbol_conf.kptr_restrict = symbol__read_kptr_restrict(); 2167 2168 symbol_conf.initialized = true; 2169 return 0; 2170 2171 out_free_sym_list: 2172 strlist__delete(symbol_conf.sym_list); 2173 out_free_tid_list: 2174 intlist__delete(symbol_conf.tid_list); 2175 out_free_pid_list: 2176 intlist__delete(symbol_conf.pid_list); 2177 out_free_comm_list: 2178 strlist__delete(symbol_conf.comm_list); 2179 out_free_dso_list: 2180 strlist__delete(symbol_conf.dso_list); 2181 return -1; 2182 } 2183 2184 void symbol__exit(void) 2185 { 2186 if (!symbol_conf.initialized) 2187 return; 2188 strlist__delete(symbol_conf.bt_stop_list); 2189 strlist__delete(symbol_conf.sym_list); 2190 strlist__delete(symbol_conf.dso_list); 2191 strlist__delete(symbol_conf.comm_list); 2192 intlist__delete(symbol_conf.tid_list); 2193 intlist__delete(symbol_conf.pid_list); 2194 vmlinux_path__exit(); 2195 symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL; 2196 symbol_conf.bt_stop_list = NULL; 2197 symbol_conf.initialized = false; 2198 } 2199 2200 int symbol__config_symfs(const struct option *opt __maybe_unused, 2201 const char *dir, int unset __maybe_unused) 2202 { 2203 char *bf = NULL; 2204 int ret; 2205 2206 symbol_conf.symfs = strdup(dir); 2207 if (symbol_conf.symfs == NULL) 2208 return -ENOMEM; 2209 2210 /* skip the locally configured cache if a symfs is given, and 2211 * config buildid dir to symfs/.debug 2212 */ 2213 ret = asprintf(&bf, "%s/%s", dir, ".debug"); 2214 if (ret < 0) 2215 return -ENOMEM; 2216 2217 set_buildid_dir(bf); 2218 2219 free(bf); 2220 return 0; 2221 } 2222