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