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