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