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