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 "cpumap.h" 22 #include "debug.h" 23 #include "demangle-cxx.h" 24 #include "demangle-java.h" 25 #include "demangle-ocaml.h" 26 #include "demangle-rust-v0.h" 27 #include "dso.h" 28 #include "util.h" // lsdir() 29 #include "event.h" 30 #include "libbfd.h" 31 #include "machine.h" 32 #include "map.h" 33 #include "symbol.h" 34 #include "map_symbol.h" 35 #include "mem-events.h" 36 #include "mem-info.h" 37 #include "symsrc.h" 38 #include "strlist.h" 39 #include "intlist.h" 40 #include "namespaces.h" 41 #include "header.h" 42 #include "path.h" 43 #include <linux/ctype.h> 44 #include <linux/log2.h> 45 #include <linux/zalloc.h> 46 47 #include <elf.h> 48 #include <limits.h> 49 #include <symbol/kallsyms.h> 50 #include <sys/utsname.h> 51 52 static int map_fixup_cb(struct map *map, void *data __maybe_unused) 53 { 54 map__fixup_start(map); 55 map__fixup_end(map); 56 return 0; 57 } 58 59 static int dso__load_kernel_sym(struct dso *dso, struct map *map); 60 static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map); 61 62 int vmlinux_path__nr_entries; 63 char **vmlinux_path; 64 65 struct symbol_conf symbol_conf = { 66 .nanosecs = false, 67 .use_modules = true, 68 .try_vmlinux_path = true, 69 .demangle = true, 70 .demangle_kernel = false, 71 .cumulate_callchain = true, 72 .time_quantum = 100 * NSEC_PER_MSEC, /* 100ms */ 73 .show_hist_headers = true, 74 .symfs = "", 75 .symfs_layout_flat = false, 76 .event_group = true, 77 .hybrid_merge = false, 78 .inline_name = true, 79 .res_sample = 0, 80 .addr2line_timeout_ms = 5 * 1000, 81 }; 82 83 struct map_list_node { 84 struct list_head node; 85 struct map *map; 86 }; 87 88 static struct map_list_node *map_list_node__new(void) 89 { 90 return malloc(sizeof(struct map_list_node)); 91 } 92 93 static enum dso_binary_type binary_type_symtab[] = { 94 DSO_BINARY_TYPE__KALLSYMS, 95 DSO_BINARY_TYPE__GUEST_KALLSYMS, 96 DSO_BINARY_TYPE__JAVA_JIT, 97 DSO_BINARY_TYPE__DEBUGLINK, 98 DSO_BINARY_TYPE__BUILD_ID_CACHE, 99 DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO, 100 DSO_BINARY_TYPE__FEDORA_DEBUGINFO, 101 DSO_BINARY_TYPE__UBUNTU_DEBUGINFO, 102 DSO_BINARY_TYPE__BUILDID_DEBUGINFO, 103 DSO_BINARY_TYPE__GNU_DEBUGDATA, 104 DSO_BINARY_TYPE__SYSTEM_PATH_DSO, 105 DSO_BINARY_TYPE__GUEST_KMODULE, 106 DSO_BINARY_TYPE__GUEST_KMODULE_COMP, 107 DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE, 108 DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP, 109 DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO, 110 DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO, 111 DSO_BINARY_TYPE__NOT_FOUND, 112 }; 113 114 #define DSO_BINARY_TYPE__SYMTAB_CNT ARRAY_SIZE(binary_type_symtab) 115 116 static bool symbol_type__filter(char symbol_type) 117 { 118 symbol_type = toupper(symbol_type); 119 return symbol_type == 'T' || symbol_type == 'W' || symbol_type == 'D' || symbol_type == 'B'; 120 } 121 122 static int prefix_underscores_count(const char *str) 123 { 124 const char *tail = str; 125 126 while (*tail == '_') 127 tail++; 128 129 return tail - str; 130 } 131 132 const char * __weak arch__normalize_symbol_name(const char *name) 133 { 134 return name; 135 } 136 137 int __weak arch__compare_symbol_names(const char *namea, const char *nameb) 138 { 139 return strcmp(namea, nameb); 140 } 141 142 int __weak arch__compare_symbol_names_n(const char *namea, const char *nameb, 143 unsigned int n) 144 { 145 return strncmp(namea, nameb, n); 146 } 147 148 int __weak arch__choose_best_symbol(struct symbol *syma, 149 struct symbol *symb __maybe_unused) 150 { 151 /* Avoid "SyS" kernel syscall aliases */ 152 if (strlen(syma->name) >= 3 && !strncmp(syma->name, "SyS", 3)) 153 return SYMBOL_B; 154 if (strlen(syma->name) >= 10 && !strncmp(syma->name, "compat_SyS", 10)) 155 return SYMBOL_B; 156 157 return SYMBOL_A; 158 } 159 160 static int choose_best_symbol(struct symbol *syma, struct symbol *symb) 161 { 162 s64 a; 163 s64 b; 164 size_t na, nb; 165 166 /* Prefer a symbol with non zero length */ 167 a = syma->end - syma->start; 168 b = symb->end - symb->start; 169 if ((b == 0) && (a > 0)) 170 return SYMBOL_A; 171 else if ((a == 0) && (b > 0)) 172 return SYMBOL_B; 173 174 if (symbol__type(syma) != symbol__type(symb)) { 175 if (symbol__type(syma) == STT_NOTYPE) 176 return SYMBOL_B; 177 if (symbol__type(symb) == STT_NOTYPE) 178 return SYMBOL_A; 179 } 180 181 /* Prefer a non weak symbol over a weak one */ 182 a = symbol__binding(syma) == STB_WEAK; 183 b = symbol__binding(symb) == STB_WEAK; 184 if (b && !a) 185 return SYMBOL_A; 186 if (a && !b) 187 return SYMBOL_B; 188 189 /* Prefer a global symbol over a non global one */ 190 a = symbol__binding(syma) == STB_GLOBAL; 191 b = symbol__binding(symb) == STB_GLOBAL; 192 if (a && !b) 193 return SYMBOL_A; 194 if (b && !a) 195 return SYMBOL_B; 196 197 /* Prefer a symbol with less underscores */ 198 a = prefix_underscores_count(syma->name); 199 b = prefix_underscores_count(symb->name); 200 if (b > a) 201 return SYMBOL_A; 202 else if (a > b) 203 return SYMBOL_B; 204 205 /* Choose the symbol with the longest name */ 206 na = strlen(syma->name); 207 nb = strlen(symb->name); 208 if (na > nb) 209 return SYMBOL_A; 210 else if (na < nb) 211 return SYMBOL_B; 212 213 return arch__choose_best_symbol(syma, symb); 214 } 215 216 void symbols__fixup_duplicate(struct rb_root_cached *symbols) 217 { 218 struct rb_node *nd; 219 struct symbol *curr, *next; 220 221 if (symbol_conf.allow_aliases) 222 return; 223 224 nd = rb_first_cached(symbols); 225 226 while (nd) { 227 curr = rb_entry(nd, struct symbol, rb_node); 228 again: 229 nd = rb_next(&curr->rb_node); 230 if (!nd) 231 break; 232 233 next = rb_entry(nd, struct symbol, rb_node); 234 if (curr->start != next->start) 235 continue; 236 237 if (choose_best_symbol(curr, next) == SYMBOL_A) { 238 if (symbol__type(next) == STT_GNU_IFUNC) 239 symbol__set_ifunc_alias(curr, true); 240 rb_erase_cached(&next->rb_node, symbols); 241 symbol__delete(next); 242 goto again; 243 } else { 244 if (symbol__type(curr) == STT_GNU_IFUNC) 245 symbol__set_ifunc_alias(next, true); 246 nd = rb_next(&curr->rb_node); 247 rb_erase_cached(&curr->rb_node, symbols); 248 symbol__delete(curr); 249 } 250 } 251 } 252 253 /* Update zero-sized symbols using the address of the next symbol */ 254 void symbols__fixup_end(struct rb_root_cached *symbols, bool is_kallsyms) 255 { 256 struct rb_node *nd, *prevnd = rb_first_cached(symbols); 257 struct symbol *curr, *prev; 258 259 if (prevnd == NULL) 260 return; 261 262 curr = rb_entry(prevnd, struct symbol, rb_node); 263 264 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) { 265 prev = curr; 266 curr = rb_entry(nd, struct symbol, rb_node); 267 268 /* 269 * On some architecture kernel text segment start is located at 270 * some low memory address, while modules are located at high 271 * memory addresses (or vice versa). The gap between end of 272 * kernel text segment and beginning of first module's text 273 * segment is very big. Therefore do not fill this gap and do 274 * not assign it to the kernel dso map (kallsyms). 275 * 276 * Also BPF code can be allocated separately from text segments 277 * and modules. So the last entry in a module should not fill 278 * the gap too. 279 * 280 * In kallsyms, it determines module symbols using '[' character 281 * like in: 282 * ffffffffc1937000 T hdmi_driver_init [snd_hda_codec_hdmi] 283 */ 284 if (prev->end == prev->start) { 285 const char *prev_mod; 286 const char *curr_mod; 287 288 if (!is_kallsyms) { 289 prev->end = curr->start; 290 continue; 291 } 292 293 prev_mod = strchr(prev->name, '['); 294 curr_mod = strchr(curr->name, '['); 295 296 /* Last kernel/module symbol mapped to end of page */ 297 if (!prev_mod != !curr_mod) 298 prev->end = roundup(prev->end + 4096, 4096); 299 /* Last symbol in the previous module */ 300 else if (prev_mod && strcmp(prev_mod, curr_mod)) 301 prev->end = roundup(prev->end + 4096, 4096); 302 else 303 prev->end = curr->start; 304 305 /* The next module can start within that page */ 306 if (prev->end > curr->start) 307 prev->end = curr->start; 308 309 pr_debug4("%s sym:%s end:%#" PRIx64 "\n", 310 __func__, prev->name, prev->end); 311 } 312 } 313 314 /* Last entry */ 315 if (curr->end == curr->start) 316 curr->end = roundup(curr->start, 4096) + 4096; 317 } 318 319 struct symbol *symbol__new(u64 start, u64 len, u8 binding, u8 type, const char *name) 320 { 321 size_t namelen = strlen(name) + 1; 322 struct symbol *sym = calloc(1, (symbol_conf.priv_size + 323 sizeof(*sym) + namelen)); 324 if (sym == NULL) 325 return NULL; 326 327 if (symbol_conf.priv_size) { 328 if (symbol_conf.init_annotation) { 329 struct annotation *notes = (void *)sym; 330 annotation__init(notes); 331 } 332 sym = ((void *)sym) + symbol_conf.priv_size; 333 } 334 335 sym->start = start; 336 sym->end = len ? start + len : start; 337 atomic_init(&sym->flags, (type << SYMBOL_FLAG_TYPE_SHIFT) | 338 (binding << SYMBOL_FLAG_BINDING_SHIFT)); 339 sym->namelen = namelen - 1; 340 341 pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n", 342 __func__, name, start, sym->end); 343 memcpy(sym->name, name, namelen); 344 345 return sym; 346 } 347 348 void symbol__delete(struct symbol *sym) 349 { 350 if (symbol_conf.priv_size) { 351 if (symbol_conf.init_annotation) { 352 struct annotation *notes = symbol__annotation(sym); 353 354 annotation__exit(notes); 355 } 356 } 357 free(((void *)sym) - symbol_conf.priv_size); 358 } 359 360 void symbol__set_ignore(struct symbol *sym, bool ignore) 361 { 362 if (ignore) 363 atomic_fetch_or(&sym->flags, SYMBOL_FLAG_IGNORE); 364 else 365 atomic_fetch_and(&sym->flags, ~SYMBOL_FLAG_IGNORE); 366 } 367 368 void symbol__set_annotate2(struct symbol *sym, bool annotate2) 369 { 370 if (annotate2) 371 atomic_fetch_or(&sym->flags, SYMBOL_FLAG_ANNOTATE2); 372 else 373 atomic_fetch_and(&sym->flags, ~SYMBOL_FLAG_ANNOTATE2); 374 } 375 376 void symbol__set_inlined(struct symbol *sym, bool inlined) 377 { 378 if (inlined) 379 atomic_fetch_or(&sym->flags, SYMBOL_FLAG_INLINED); 380 else 381 atomic_fetch_and(&sym->flags, ~SYMBOL_FLAG_INLINED); 382 } 383 384 void symbol__set_ifunc_alias(struct symbol *sym, bool ifunc_alias) 385 { 386 if (ifunc_alias) 387 atomic_fetch_or(&sym->flags, SYMBOL_FLAG_IFUNC_ALIAS); 388 else 389 atomic_fetch_and(&sym->flags, ~SYMBOL_FLAG_IFUNC_ALIAS); 390 } 391 392 static void symbol__set_idle(struct symbol *sym, bool idle) 393 { 394 uint16_t old_flags = atomic_load_explicit(&sym->flags, memory_order_relaxed); 395 uint16_t new_flags; 396 uint16_t idle_val = idle ? SYMBOL_IDLE__IDLE : SYMBOL_IDLE__NOT_IDLE; 397 398 do { 399 new_flags = old_flags & ~SYMBOL_FLAG_IDLE_MASK; 400 new_flags |= (idle_val << SYMBOL_FLAG_IDLE_SHIFT); 401 } while (!atomic_compare_exchange_weak(&sym->flags, &old_flags, new_flags)); 402 } 403 void symbols__delete(struct rb_root_cached *symbols) 404 { 405 struct symbol *pos; 406 struct rb_node *next = rb_first_cached(symbols); 407 408 while (next) { 409 pos = rb_entry(next, struct symbol, rb_node); 410 next = rb_next(&pos->rb_node); 411 rb_erase_cached(&pos->rb_node, symbols); 412 symbol__delete(pos); 413 } 414 } 415 416 void __symbols__insert(struct rb_root_cached *symbols, struct symbol *sym) 417 { 418 struct rb_node **p = &symbols->rb_root.rb_node; 419 struct rb_node *parent = NULL; 420 const u64 ip = sym->start; 421 struct symbol *s; 422 bool leftmost = true; 423 424 while (*p != NULL) { 425 parent = *p; 426 s = rb_entry(parent, struct symbol, rb_node); 427 if (ip < s->start) 428 p = &(*p)->rb_left; 429 else { 430 p = &(*p)->rb_right; 431 leftmost = false; 432 } 433 } 434 rb_link_node(&sym->rb_node, parent, p); 435 rb_insert_color_cached(&sym->rb_node, symbols, leftmost); 436 } 437 438 void symbols__insert(struct rb_root_cached *symbols, struct symbol *sym) 439 { 440 __symbols__insert(symbols, sym); 441 } 442 443 static struct symbol *symbols__find(struct rb_root_cached *symbols, u64 ip) 444 { 445 struct rb_node *n; 446 447 if (symbols == NULL) 448 return NULL; 449 450 n = symbols->rb_root.rb_node; 451 452 while (n) { 453 struct symbol *s = rb_entry(n, struct symbol, rb_node); 454 455 if (ip < s->start) 456 n = n->rb_left; 457 else if (ip > s->end || (ip == s->end && ip != s->start)) 458 n = n->rb_right; 459 else 460 return s; 461 } 462 463 return NULL; 464 } 465 466 static struct symbol *symbols__first(struct rb_root_cached *symbols) 467 { 468 struct rb_node *n = rb_first_cached(symbols); 469 470 if (n) 471 return rb_entry(n, struct symbol, rb_node); 472 473 return NULL; 474 } 475 476 static struct symbol *symbols__last(struct rb_root_cached *symbols) 477 { 478 struct rb_node *n = rb_last(&symbols->rb_root); 479 480 if (n) 481 return rb_entry(n, struct symbol, rb_node); 482 483 return NULL; 484 } 485 486 static struct symbol *symbols__next(struct symbol *sym) 487 { 488 struct rb_node *n = rb_next(&sym->rb_node); 489 490 if (n) 491 return rb_entry(n, struct symbol, rb_node); 492 493 return NULL; 494 } 495 496 static int symbols__sort_name_cmp(const void *vlhs, const void *vrhs) 497 { 498 const struct symbol *lhs = *((const struct symbol **)vlhs); 499 const struct symbol *rhs = *((const struct symbol **)vrhs); 500 501 return strcmp(lhs->name, rhs->name); 502 } 503 504 static struct symbol **symbols__sort_by_name(struct rb_root_cached *source, size_t *len) 505 { 506 struct rb_node *nd; 507 struct symbol **result; 508 size_t i = 0, size = 0; 509 510 for (nd = rb_first_cached(source); nd; nd = rb_next(nd)) 511 size++; 512 513 result = malloc(sizeof(*result) * size); 514 if (!result) 515 return NULL; 516 517 for (nd = rb_first_cached(source); nd; nd = rb_next(nd)) { 518 struct symbol *pos = rb_entry(nd, struct symbol, rb_node); 519 520 result[i++] = pos; 521 } 522 qsort(result, size, sizeof(*result), symbols__sort_name_cmp); 523 *len = size; 524 return result; 525 } 526 527 int symbol__match_symbol_name(const char *name, const char *str, 528 enum symbol_tag_include includes) 529 { 530 const char *versioning; 531 532 if (includes == SYMBOL_TAG_INCLUDE__DEFAULT_ONLY && 533 (versioning = strstr(name, "@@"))) { 534 int len = strlen(str); 535 536 if (len < versioning - name) 537 len = versioning - name; 538 539 return arch__compare_symbol_names_n(name, str, len); 540 } else 541 return arch__compare_symbol_names(name, str); 542 } 543 544 static struct symbol *symbols__find_by_name(struct symbol *symbols[], 545 size_t symbols_len, 546 const char *name, 547 enum symbol_tag_include includes, 548 size_t *found_idx) 549 { 550 size_t i, lower = 0, upper = symbols_len; 551 struct symbol *s = NULL; 552 553 if (found_idx) 554 *found_idx = SIZE_MAX; 555 556 if (!symbols_len) 557 return NULL; 558 559 while (lower < upper) { 560 int cmp; 561 562 i = (lower + upper) / 2; 563 cmp = symbol__match_symbol_name(symbols[i]->name, name, includes); 564 565 if (cmp > 0) 566 upper = i; 567 else if (cmp < 0) 568 lower = i + 1; 569 else { 570 if (found_idx) 571 *found_idx = i; 572 s = symbols[i]; 573 break; 574 } 575 } 576 if (s && includes != SYMBOL_TAG_INCLUDE__DEFAULT_ONLY) { 577 /* return first symbol that has same name (if any) */ 578 for (; i > 0; i--) { 579 struct symbol *tmp = symbols[i - 1]; 580 581 if (!arch__compare_symbol_names(tmp->name, s->name)) { 582 if (found_idx) 583 *found_idx = i - 1; 584 s = tmp; 585 } else 586 break; 587 } 588 } 589 assert(!found_idx || !s || s == symbols[*found_idx]); 590 return s; 591 } 592 593 void dso__reset_find_symbol_cache(struct dso *dso) 594 { 595 dso__set_last_find_result_addr(dso, 0); 596 dso__set_last_find_result_symbol(dso, NULL); 597 } 598 599 void dso__insert_symbol(struct dso *dso, struct symbol *sym) 600 { 601 __symbols__insert(dso__symbols(dso), sym); 602 603 /* update the symbol cache if necessary */ 604 if (dso__last_find_result_addr(dso) >= sym->start && 605 (dso__last_find_result_addr(dso) < sym->end || 606 sym->start == sym->end)) { 607 dso__set_last_find_result_symbol(dso, sym); 608 } 609 } 610 611 void dso__delete_symbol(struct dso *dso, struct symbol *sym) 612 { 613 rb_erase_cached(&sym->rb_node, dso__symbols(dso)); 614 symbol__delete(sym); 615 dso__reset_find_symbol_cache(dso); 616 } 617 618 struct symbol *dso__find_symbol(struct dso *dso, u64 addr) 619 { 620 if (dso__last_find_result_addr(dso) != addr || dso__last_find_result_symbol(dso) == NULL) { 621 dso__set_last_find_result_addr(dso, addr); 622 dso__set_last_find_result_symbol(dso, symbols__find(dso__symbols(dso), addr)); 623 } 624 625 return dso__last_find_result_symbol(dso); 626 } 627 628 struct symbol *dso__find_symbol_nocache(struct dso *dso, u64 addr) 629 { 630 return symbols__find(dso__symbols(dso), addr); 631 } 632 633 struct symbol *dso__first_symbol(struct dso *dso) 634 { 635 return symbols__first(dso__symbols(dso)); 636 } 637 638 struct symbol *dso__last_symbol(struct dso *dso) 639 { 640 return symbols__last(dso__symbols(dso)); 641 } 642 643 struct symbol *dso__next_symbol(struct symbol *sym) 644 { 645 return symbols__next(sym); 646 } 647 648 struct symbol *dso__next_symbol_by_name(struct dso *dso, size_t *idx) 649 { 650 if (*idx + 1 >= dso__symbol_names_len(dso)) 651 return NULL; 652 653 ++*idx; 654 return dso__symbol_names(dso)[*idx]; 655 } 656 657 /* 658 * Returns first symbol that matched with @name. 659 */ 660 struct symbol *dso__find_symbol_by_name(struct dso *dso, const char *name, size_t *idx) 661 { 662 struct symbol *s = symbols__find_by_name(dso__symbol_names(dso), 663 dso__symbol_names_len(dso), 664 name, SYMBOL_TAG_INCLUDE__NONE, idx); 665 if (!s) { 666 s = symbols__find_by_name(dso__symbol_names(dso), dso__symbol_names_len(dso), 667 name, SYMBOL_TAG_INCLUDE__DEFAULT_ONLY, idx); 668 } 669 return s; 670 } 671 672 void dso__sort_by_name(struct dso *dso) 673 { 674 mutex_lock(dso__lock(dso)); 675 if (!dso__sorted_by_name(dso)) { 676 size_t len = 0; 677 678 dso__set_symbol_names(dso, symbols__sort_by_name(dso__symbols(dso), &len)); 679 if (dso__symbol_names(dso)) { 680 dso__set_symbol_names_len(dso, len); 681 dso__set_sorted_by_name(dso); 682 } 683 } 684 mutex_unlock(dso__lock(dso)); 685 } 686 687 /* 688 * While we find nice hex chars, build a long_val. 689 * Return number of chars processed. 690 */ 691 static int hex2u64(const char *ptr, u64 *long_val) 692 { 693 char *p; 694 695 *long_val = strtoull(ptr, &p, 16); 696 697 return p - ptr; 698 } 699 700 701 int modules__parse(const char *filename, void *arg, 702 int (*process_module)(void *arg, const char *name, 703 u64 start, u64 size)) 704 { 705 char *line = NULL; 706 size_t n; 707 FILE *file; 708 int err = 0; 709 710 file = fopen(filename, "r"); 711 if (file == NULL) 712 return -1; 713 714 while (1) { 715 char name[PATH_MAX]; 716 u64 start, size; 717 char *sep, *endptr; 718 ssize_t line_len; 719 720 line_len = getline(&line, &n, file); 721 if (line_len < 0) { 722 if (feof(file)) 723 break; 724 err = -1; 725 goto out; 726 } 727 728 if (!line) { 729 err = -1; 730 goto out; 731 } 732 733 line[--line_len] = '\0'; /* \n */ 734 735 sep = strrchr(line, 'x'); 736 if (sep == NULL) 737 continue; 738 739 hex2u64(sep + 1, &start); 740 741 sep = strchr(line, ' '); 742 if (sep == NULL) 743 continue; 744 745 *sep = '\0'; 746 747 scnprintf(name, sizeof(name), "[%s]", line); 748 749 size = strtoul(sep + 1, &endptr, 0); 750 if (*endptr != ' ' && *endptr != '\t') 751 continue; 752 753 err = process_module(arg, name, start, size); 754 if (err) 755 break; 756 } 757 out: 758 free(line); 759 fclose(file); 760 return err; 761 } 762 763 764 /* 765 * These are symbols in the kernel image, so make sure that 766 * sym is from a kernel DSO. 767 */ 768 static int sym_name_cmp(const void *a, const void *b) 769 { 770 const char *name = a; 771 const char *const *sym = b; 772 773 return strcmp(name, *sym); 774 } 775 776 static bool match_x86_idle_routine(const char *name, const char *base) 777 { 778 if (strstarts(name, base)) { 779 size_t len = strlen(base); 780 781 if (name[len] == '\0' || name[len] == '.') 782 return true; 783 } 784 return false; 785 } 786 787 bool symbol__is_idle(struct symbol *sym, const struct dso *dso, struct perf_env *env) 788 { 789 static const char * const idle_symbols[] = { 790 "acpi_idle_do_entry", 791 "acpi_processor_ffh_cstate_enter", 792 "arch_cpu_idle", 793 "cpu_idle", 794 "cpu_startup_entry", 795 "default_idle", 796 "enter_idle", 797 "exit_idle", 798 "idle_cpu", 799 "native_safe_halt", 800 "poll_idle", 801 "pseries_dedicated_idle_sleep", 802 }; 803 const char *name = sym->name; 804 uint16_t e_machine; 805 806 { 807 uint16_t flags = atomic_load_explicit(&sym->flags, memory_order_relaxed); 808 uint16_t idle_val = (flags & SYMBOL_FLAG_IDLE_MASK) >> SYMBOL_FLAG_IDLE_SHIFT; 809 810 if (idle_val != SYMBOL_IDLE__UNKNOWN) 811 return idle_val == SYMBOL_IDLE__IDLE; 812 } 813 814 if (!dso || dso__kernel(dso) == DSO_SPACE__USER) { 815 symbol__set_idle(sym, /*idle=*/false); 816 return false; 817 } 818 819 /* 820 * ppc64 uses function descriptors and appends a '.' to the 821 * start of every instruction address. Remove it. 822 */ 823 if (name[0] == '.') 824 name++; 825 826 if (bsearch(name, idle_symbols, ARRAY_SIZE(idle_symbols), 827 sizeof(idle_symbols[0]), sym_name_cmp)) { 828 symbol__set_idle(sym, /*idle=*/true); 829 return true; 830 } 831 832 e_machine = (env && env->arch) ? perf_env__e_machine(env, NULL) : EM_NONE; 833 if (e_machine == EM_NONE && dso) 834 e_machine = dso__e_machine((struct dso *)dso, NULL, NULL); 835 if (e_machine == EM_NONE && env) 836 e_machine = perf_env__e_machine(env, NULL); 837 838 if (e_machine == EM_386 || e_machine == EM_X86_64) { 839 if (match_x86_idle_routine(name, "intel_idle") || 840 match_x86_idle_routine(name, "intel_idle_irq") || 841 match_x86_idle_routine(name, "intel_idle_ibrs") || 842 match_x86_idle_routine(name, "mwait_idle") || 843 match_x86_idle_routine(name, "mwait_idle_with_hints")) { 844 symbol__set_idle(sym, /*idle=*/true); 845 return true; 846 } 847 } 848 849 if (e_machine == EM_PPC64 && !strcmp(name, "ppc64_runlatch_off")) { 850 symbol__set_idle(sym, /*idle=*/true); 851 return true; 852 } 853 854 if (e_machine == EM_S390 && strstarts(name, "psw_idle")) { 855 int major = 0, minor = 0; 856 const char *release = env ? perf_env__os_release(env) : NULL; 857 858 /* 859 * If we can't determine the release (e.g. unpopulated guest traces), 860 * default to idle. 861 */ 862 if (!release) { 863 symbol__set_idle(sym, /*idle=*/true); 864 return true; 865 } 866 867 /* Before v6.10, s390 used psw_idle. */ 868 if (sscanf(release, "%d.%d", &major, &minor) == 2 && 869 (major < 6 || (major == 6 && minor < 10))) { 870 symbol__set_idle(sym, /*idle=*/true); 871 return true; 872 } 873 } 874 875 symbol__set_idle(sym, /*idle=*/false); 876 return false; 877 } 878 879 static int map__process_kallsym_symbol(void *arg, const char *name, 880 char type, u64 start) 881 { 882 struct symbol *sym; 883 struct dso *dso = arg; 884 struct rb_root_cached *root = dso__symbols(dso); 885 886 if (!symbol_type__filter(type)) 887 return 0; 888 889 /* Ignore mapping and livepatch symbols in kallsyms */ 890 if (is_ignored_kernel_symbol(name) || is_livepatch_symbol(name)) 891 return 0; 892 893 /* 894 * module symbols are not sorted so we add all 895 * symbols, setting length to 0, and rely on 896 * symbols__fixup_end() to fix it up. 897 */ 898 sym = symbol__new(start, 0, kallsyms2elf_binding(type), kallsyms2elf_type(type), name); 899 if (sym == NULL) 900 return -ENOMEM; 901 /* 902 * We will pass the symbols to the filter later, in 903 * map__split_kallsyms, when we have split the maps per module 904 */ 905 __symbols__insert(root, sym); 906 907 return 0; 908 } 909 910 /* 911 * Loads the function entries in /proc/kallsyms into kernel_map->dso, 912 * so that we can in the next step set the symbol ->end address and then 913 * call kernel_maps__split_kallsyms. 914 */ 915 static int dso__load_all_kallsyms(struct dso *dso, const char *filename) 916 { 917 return kallsyms__parse(filename, dso, map__process_kallsym_symbol); 918 } 919 920 static int maps__split_kallsyms_for_kcore(struct maps *kmaps, struct dso *dso) 921 { 922 struct symbol *pos; 923 int count = 0; 924 struct rb_root_cached *root = dso__symbols(dso); 925 struct rb_root_cached old_root = *root; 926 struct rb_node *next = rb_first_cached(root); 927 928 if (!kmaps) 929 return -1; 930 931 *root = RB_ROOT_CACHED; 932 933 while (next) { 934 struct map *curr_map; 935 struct dso *curr_map_dso; 936 char *module; 937 938 pos = rb_entry(next, struct symbol, rb_node); 939 next = rb_next(&pos->rb_node); 940 941 rb_erase_cached(&pos->rb_node, &old_root); 942 RB_CLEAR_NODE(&pos->rb_node); 943 module = strchr(pos->name, '\t'); 944 if (module) 945 *module = '\0'; 946 947 curr_map = maps__find(kmaps, pos->start); 948 949 if (!curr_map) { 950 symbol__delete(pos); 951 continue; 952 } 953 curr_map_dso = map__dso(curr_map); 954 pos->start -= map__start(curr_map) - map__pgoff(curr_map); 955 if (pos->end > map__end(curr_map)) 956 pos->end = map__end(curr_map); 957 if (pos->end) 958 pos->end -= map__start(curr_map) - map__pgoff(curr_map); 959 symbols__insert(dso__symbols(curr_map_dso), pos); 960 ++count; 961 map__put(curr_map); 962 } 963 964 /* Symbols have been adjusted */ 965 dso__set_adjust_symbols(dso, true); 966 967 return count; 968 } 969 970 /* 971 * Split the symbols into maps, making sure there are no overlaps, i.e. the 972 * kernel range is broken in several maps, named [kernel].N, as we don't have 973 * the original ELF section names vmlinux have. 974 */ 975 static int maps__split_kallsyms(struct maps *kmaps, struct dso *dso, u64 delta, 976 struct map *initial_map) 977 { 978 struct machine *machine; 979 struct map *curr_map = map__get(initial_map); 980 struct symbol *pos; 981 int count = 0, moved = 0; 982 struct rb_root_cached *root = dso__symbols(dso); 983 struct rb_node *next = rb_first_cached(root); 984 int kernel_range = 0; 985 uint16_t e_machine = EM_NONE; 986 987 if (!kmaps) 988 return -1; 989 990 machine = maps__machine(kmaps); 991 e_machine = perf_env__e_machine(machine->env, /*e_flags=*/NULL); 992 993 while (next) { 994 char *module; 995 996 pos = rb_entry(next, struct symbol, rb_node); 997 next = rb_next(&pos->rb_node); 998 999 module = strchr(pos->name, '\t'); 1000 if (module) { 1001 struct dso *curr_map_dso; 1002 1003 if (!symbol_conf.use_modules) 1004 goto discard_symbol; 1005 1006 *module++ = '\0'; 1007 curr_map_dso = map__dso(curr_map); 1008 if (strcmp(dso__short_name(curr_map_dso), module)) { 1009 if (!RC_CHK_EQUAL(curr_map, initial_map) && 1010 dso__kernel(dso) == DSO_SPACE__KERNEL_GUEST && 1011 machine__is_default_guest(machine)) { 1012 /* 1013 * We assume all symbols of a module are 1014 * continuous in * kallsyms, so curr_map 1015 * points to a module and all its 1016 * symbols are in its kmap. Mark it as 1017 * loaded. 1018 */ 1019 dso__set_loaded(curr_map_dso); 1020 } 1021 1022 map__zput(curr_map); 1023 curr_map = maps__find_by_name(kmaps, module); 1024 if (curr_map == NULL) { 1025 pr_debug("%s/proc/{kallsyms,modules} " 1026 "inconsistency while looking " 1027 "for \"%s\" module!\n", 1028 machine->root_dir, module); 1029 curr_map = map__get(initial_map); 1030 goto discard_symbol; 1031 } 1032 curr_map_dso = map__dso(curr_map); 1033 if (dso__loaded(curr_map_dso) && 1034 !machine__is_default_guest(machine)) 1035 goto discard_symbol; 1036 } 1037 /* 1038 * So that we look just like we get from .ko files, 1039 * i.e. not prelinked, relative to initial_map->start. 1040 */ 1041 pos->start = map__map_ip(curr_map, pos->start); 1042 pos->end = map__map_ip(curr_map, pos->end); 1043 } else if (e_machine == EM_X86_64 && is_entry_trampoline(pos->name)) { 1044 /* 1045 * These symbols are not needed anymore since the 1046 * trampoline maps refer to the text section and it's 1047 * symbols instead. Avoid having to deal with 1048 * relocations, and the assumption that the first symbol 1049 * is the start of kernel text, by simply removing the 1050 * symbols at this point. 1051 */ 1052 goto discard_symbol; 1053 } else if (!RC_CHK_EQUAL(curr_map, initial_map)) { 1054 char dso_name[PATH_MAX]; 1055 struct dso *ndso; 1056 1057 if (delta) { 1058 /* Kernel was relocated at boot time */ 1059 pos->start -= delta; 1060 pos->end -= delta; 1061 } 1062 1063 if (map__start(initial_map) <= (pos->start + delta) && 1064 (pos->start + delta) < map__end(initial_map)) { 1065 map__zput(curr_map); 1066 curr_map = map__get(initial_map); 1067 goto add_symbol; 1068 } 1069 1070 if (dso__kernel(dso) == DSO_SPACE__KERNEL_GUEST) 1071 snprintf(dso_name, sizeof(dso_name), 1072 "[guest.kernel].%d", 1073 kernel_range); 1074 else 1075 snprintf(dso_name, sizeof(dso_name), 1076 "[kernel].%d", 1077 kernel_range); 1078 1079 ndso = dso__new(dso_name); 1080 map__zput(curr_map); 1081 if (ndso == NULL) 1082 return -1; 1083 1084 dso__set_kernel(ndso, dso__kernel(dso)); 1085 if (dso__kernel(dso) == DSO_SPACE__KERNEL_GUEST) { 1086 dso__set_binary_type(ndso, DSO_BINARY_TYPE__GUEST_KALLSYMS); 1087 dso__set_symtab_type(ndso, DSO_BINARY_TYPE__GUEST_KALLSYMS); 1088 } else { 1089 dso__set_binary_type(ndso, DSO_BINARY_TYPE__KALLSYMS); 1090 dso__set_symtab_type(ndso, DSO_BINARY_TYPE__KALLSYMS); 1091 } 1092 dso__set_loaded(ndso); 1093 1094 curr_map = map__new2(pos->start, ndso); 1095 if (curr_map == NULL) { 1096 dso__put(ndso); 1097 return -1; 1098 } 1099 1100 map__set_mapping_type(curr_map, MAPPING_TYPE__IDENTITY); 1101 if (maps__insert(kmaps, curr_map)) { 1102 map__zput(curr_map); 1103 dso__put(ndso); 1104 return -1; 1105 } 1106 dso__put(ndso); 1107 ++kernel_range; 1108 } else if (delta) { 1109 /* Kernel was relocated at boot time */ 1110 pos->start -= delta; 1111 pos->end -= delta; 1112 } 1113 add_symbol: 1114 if (!RC_CHK_EQUAL(curr_map, initial_map)) { 1115 struct dso *curr_map_dso = map__dso(curr_map); 1116 1117 rb_erase_cached(&pos->rb_node, root); 1118 symbols__insert(dso__symbols(curr_map_dso), pos); 1119 ++moved; 1120 } else 1121 ++count; 1122 1123 continue; 1124 discard_symbol: 1125 rb_erase_cached(&pos->rb_node, root); 1126 symbol__delete(pos); 1127 } 1128 1129 if (!RC_CHK_EQUAL(curr_map, initial_map) && 1130 dso__kernel(dso) == DSO_SPACE__KERNEL_GUEST && 1131 machine__is_default_guest(maps__machine(kmaps))) { 1132 dso__set_loaded(map__dso(curr_map)); 1133 } 1134 map__put(curr_map); 1135 return count + moved; 1136 } 1137 1138 bool symbol__restricted_filename(const char *filename, 1139 const char *restricted_filename) 1140 { 1141 bool restricted = false; 1142 1143 if (symbol_conf.kptr_restrict) { 1144 char *r = realpath(filename, NULL); 1145 1146 if (r != NULL) { 1147 restricted = strcmp(r, restricted_filename) == 0; 1148 free(r); 1149 return restricted; 1150 } 1151 } 1152 1153 return restricted; 1154 } 1155 1156 struct module_info { 1157 struct rb_node rb_node; 1158 char *name; 1159 u64 start; 1160 }; 1161 1162 static void add_module(struct module_info *mi, struct rb_root *modules) 1163 { 1164 struct rb_node **p = &modules->rb_node; 1165 struct rb_node *parent = NULL; 1166 struct module_info *m; 1167 1168 while (*p != NULL) { 1169 parent = *p; 1170 m = rb_entry(parent, struct module_info, rb_node); 1171 if (strcmp(mi->name, m->name) < 0) 1172 p = &(*p)->rb_left; 1173 else 1174 p = &(*p)->rb_right; 1175 } 1176 rb_link_node(&mi->rb_node, parent, p); 1177 rb_insert_color(&mi->rb_node, modules); 1178 } 1179 1180 static void delete_modules(struct rb_root *modules) 1181 { 1182 struct module_info *mi; 1183 struct rb_node *next = rb_first(modules); 1184 1185 while (next) { 1186 mi = rb_entry(next, struct module_info, rb_node); 1187 next = rb_next(&mi->rb_node); 1188 rb_erase(&mi->rb_node, modules); 1189 zfree(&mi->name); 1190 free(mi); 1191 } 1192 } 1193 1194 static struct module_info *find_module(const char *name, 1195 struct rb_root *modules) 1196 { 1197 struct rb_node *n = modules->rb_node; 1198 1199 while (n) { 1200 struct module_info *m; 1201 int cmp; 1202 1203 m = rb_entry(n, struct module_info, rb_node); 1204 cmp = strcmp(name, m->name); 1205 if (cmp < 0) 1206 n = n->rb_left; 1207 else if (cmp > 0) 1208 n = n->rb_right; 1209 else 1210 return m; 1211 } 1212 1213 return NULL; 1214 } 1215 1216 static int __read_proc_modules(void *arg, const char *name, u64 start, 1217 u64 size __maybe_unused) 1218 { 1219 struct rb_root *modules = arg; 1220 struct module_info *mi; 1221 1222 mi = zalloc(sizeof(struct module_info)); 1223 if (!mi) 1224 return -ENOMEM; 1225 1226 mi->name = strdup(name); 1227 mi->start = start; 1228 1229 if (!mi->name) { 1230 free(mi); 1231 return -ENOMEM; 1232 } 1233 1234 add_module(mi, modules); 1235 1236 return 0; 1237 } 1238 1239 static int read_proc_modules(const char *filename, struct rb_root *modules) 1240 { 1241 if (symbol__restricted_filename(filename, "/proc/modules")) 1242 return -1; 1243 1244 if (modules__parse(filename, modules, __read_proc_modules)) { 1245 delete_modules(modules); 1246 return -1; 1247 } 1248 1249 return 0; 1250 } 1251 1252 int compare_proc_modules(const char *from, const char *to) 1253 { 1254 struct rb_root from_modules = RB_ROOT; 1255 struct rb_root to_modules = RB_ROOT; 1256 struct rb_node *from_node, *to_node; 1257 struct module_info *from_m, *to_m; 1258 int ret = -1; 1259 1260 if (read_proc_modules(from, &from_modules)) 1261 return -1; 1262 1263 if (read_proc_modules(to, &to_modules)) 1264 goto out_delete_from; 1265 1266 from_node = rb_first(&from_modules); 1267 to_node = rb_first(&to_modules); 1268 while (from_node) { 1269 if (!to_node) 1270 break; 1271 1272 from_m = rb_entry(from_node, struct module_info, rb_node); 1273 to_m = rb_entry(to_node, struct module_info, rb_node); 1274 1275 if (from_m->start != to_m->start || 1276 strcmp(from_m->name, to_m->name)) 1277 break; 1278 1279 from_node = rb_next(from_node); 1280 to_node = rb_next(to_node); 1281 } 1282 1283 if (!from_node && !to_node) 1284 ret = 0; 1285 1286 delete_modules(&to_modules); 1287 out_delete_from: 1288 delete_modules(&from_modules); 1289 1290 return ret; 1291 } 1292 1293 static int do_validate_kcore_modules_cb(struct map *old_map, void *data) 1294 { 1295 struct rb_root *modules = data; 1296 struct module_info *mi; 1297 struct dso *dso; 1298 1299 if (!__map__is_kmodule(old_map)) 1300 return 0; 1301 1302 dso = map__dso(old_map); 1303 /* Module must be in memory at the same address */ 1304 mi = find_module(dso__short_name(dso), modules); 1305 if (!mi || mi->start != map__start(old_map)) 1306 return -EINVAL; 1307 1308 return 0; 1309 } 1310 1311 static int do_validate_kcore_modules(const char *filename, struct maps *kmaps) 1312 { 1313 struct rb_root modules = RB_ROOT; 1314 int err; 1315 1316 err = read_proc_modules(filename, &modules); 1317 if (err) 1318 return err; 1319 1320 err = maps__for_each_map(kmaps, do_validate_kcore_modules_cb, &modules); 1321 1322 delete_modules(&modules); 1323 return err; 1324 } 1325 1326 /* 1327 * If kallsyms is referenced by name then we look for filename in the same 1328 * directory. 1329 */ 1330 static bool filename_from_kallsyms_filename(char *filename, 1331 const char *base_name, 1332 const char *kallsyms_filename) 1333 { 1334 char *name; 1335 1336 strcpy(filename, kallsyms_filename); 1337 name = strrchr(filename, '/'); 1338 if (!name) 1339 return false; 1340 1341 name += 1; 1342 1343 if (!strcmp(name, "kallsyms")) { 1344 strcpy(name, base_name); 1345 return true; 1346 } 1347 1348 return false; 1349 } 1350 1351 static int validate_kcore_modules(const char *kallsyms_filename, 1352 struct map *map) 1353 { 1354 struct maps *kmaps = map__kmaps(map); 1355 char modules_filename[PATH_MAX]; 1356 1357 if (!kmaps) 1358 return -EINVAL; 1359 1360 if (!filename_from_kallsyms_filename(modules_filename, "modules", 1361 kallsyms_filename)) 1362 return -EINVAL; 1363 1364 if (do_validate_kcore_modules(modules_filename, kmaps)) 1365 return -EINVAL; 1366 1367 return 0; 1368 } 1369 1370 static int validate_kcore_addresses(const char *kallsyms_filename, 1371 struct map *map) 1372 { 1373 struct kmap *kmap = map__kmap(map); 1374 1375 if (!kmap) 1376 return -EINVAL; 1377 1378 if (kmap->ref_reloc_sym && kmap->ref_reloc_sym->name) { 1379 u64 start; 1380 1381 if (kallsyms__get_function_start(kallsyms_filename, 1382 kmap->ref_reloc_sym->name, &start)) 1383 return -ENOENT; 1384 if (start != kmap->ref_reloc_sym->addr) 1385 return -EINVAL; 1386 } 1387 1388 return validate_kcore_modules(kallsyms_filename, map); 1389 } 1390 1391 struct kcore_mapfn_data { 1392 struct dso *dso; 1393 struct list_head maps; 1394 }; 1395 1396 static int kcore_mapfn(u64 start, u64 len, u64 pgoff, void *data) 1397 { 1398 struct kcore_mapfn_data *md = data; 1399 struct map_list_node *list_node = map_list_node__new(); 1400 1401 if (!list_node) 1402 return -ENOMEM; 1403 1404 list_node->map = map__new2(start, md->dso); 1405 if (!list_node->map) { 1406 free(list_node); 1407 return -ENOMEM; 1408 } 1409 1410 map__set_end(list_node->map, map__start(list_node->map) + len); 1411 map__set_pgoff(list_node->map, pgoff); 1412 1413 list_add(&list_node->node, &md->maps); 1414 1415 return 0; 1416 } 1417 1418 static bool remove_old_maps(struct map *map, void *data) 1419 { 1420 const struct map *map_to_save = data; 1421 1422 /* 1423 * We need to preserve eBPF maps even if they are covered by kcore, 1424 * because we need to access eBPF dso for source data. 1425 */ 1426 return !RC_CHK_EQUAL(map, map_to_save) && !__map__is_bpf_prog(map); 1427 } 1428 1429 static int dso__load_kcore(struct dso *dso, struct map *map, 1430 const char *kallsyms_filename) 1431 { 1432 struct maps *kmaps = map__kmaps(map); 1433 struct kcore_mapfn_data md; 1434 struct map *map_ref, *replacement_map = NULL; 1435 struct machine *machine; 1436 bool is_64_bit; 1437 int err, fd; 1438 char kcore_filename[PATH_MAX]; 1439 u64 stext; 1440 1441 if (!kmaps) 1442 return -EINVAL; 1443 1444 machine = maps__machine(kmaps); 1445 1446 /* This function requires that the map is the kernel map */ 1447 if (!__map__is_kernel(map)) 1448 return -EINVAL; 1449 1450 if (!filename_from_kallsyms_filename(kcore_filename, "kcore", 1451 kallsyms_filename)) 1452 return -EINVAL; 1453 1454 /* Modules and kernel must be present at their original addresses */ 1455 if (validate_kcore_addresses(kallsyms_filename, map)) 1456 return -EINVAL; 1457 1458 md.dso = dso; 1459 INIT_LIST_HEAD(&md.maps); 1460 1461 fd = open(kcore_filename, O_RDONLY); 1462 if (fd < 0) { 1463 pr_debug("Failed to open %s. Note /proc/kcore requires CAP_SYS_RAWIO capability to access.\n", 1464 kcore_filename); 1465 return -EINVAL; 1466 } 1467 1468 /* Read new maps into temporary lists */ 1469 err = file__read_maps(fd, map__prot(map) & PROT_EXEC, kcore_mapfn, &md, 1470 &is_64_bit); 1471 if (err) 1472 goto out_err; 1473 dso__set_is_64_bit(dso, is_64_bit); 1474 1475 if (list_empty(&md.maps)) { 1476 err = -EINVAL; 1477 goto out_err; 1478 } 1479 1480 /* Remove old maps */ 1481 maps__remove_maps(kmaps, remove_old_maps, map); 1482 machine->trampolines_mapped = false; 1483 1484 /* Find the kernel map using the '_stext' symbol */ 1485 if (!kallsyms__get_function_start(kallsyms_filename, "_stext", &stext)) { 1486 u64 replacement_size = 0; 1487 struct map_list_node *new_node; 1488 1489 list_for_each_entry(new_node, &md.maps, node) { 1490 struct map *new_map = new_node->map; 1491 u64 new_size = map__size(new_map); 1492 1493 if (!(stext >= map__start(new_map) && stext < map__end(new_map))) 1494 continue; 1495 1496 /* 1497 * On some architectures, ARM64 for example, the kernel 1498 * text can get allocated inside of the vmalloc segment. 1499 * Select the smallest matching segment, in case stext 1500 * falls within more than one in the list. 1501 */ 1502 if (!replacement_map || new_size < replacement_size) { 1503 replacement_map = new_map; 1504 replacement_size = new_size; 1505 } 1506 } 1507 } 1508 1509 if (!replacement_map) 1510 replacement_map = list_entry(md.maps.next, struct map_list_node, node)->map; 1511 1512 /* 1513 * Update addresses of vmlinux map. Re-insert it to ensure maps are 1514 * correctly ordered. Do this before using maps__merge_in() for the 1515 * remaining maps so vmlinux gets split if necessary. 1516 */ 1517 map_ref = map__get(map); 1518 maps__remove(kmaps, map_ref); 1519 1520 map__set_start(map_ref, map__start(replacement_map)); 1521 map__set_end(map_ref, map__end(replacement_map)); 1522 map__set_pgoff(map_ref, map__pgoff(replacement_map)); 1523 map__set_mapping_type(map_ref, map__mapping_type(replacement_map)); 1524 1525 err = maps__insert(kmaps, map_ref); 1526 map__put(map_ref); 1527 if (err) 1528 goto out_err; 1529 1530 /* Add new maps */ 1531 while (!list_empty(&md.maps)) { 1532 struct map_list_node *new_node = list_entry(md.maps.next, struct map_list_node, node); 1533 struct map *new_map = new_node->map; 1534 1535 list_del_init(&new_node->node); 1536 1537 /* skip if replacement_map, already inserted above */ 1538 if (!RC_CHK_EQUAL(new_map, replacement_map)) { 1539 /* 1540 * Merge kcore map into existing maps, 1541 * and ensure that current maps (eBPF) 1542 * stay intact. 1543 */ 1544 if (maps__merge_in(kmaps, new_map)) { 1545 err = -EINVAL; 1546 goto out_err; 1547 } 1548 } 1549 map__zput(new_node->map); 1550 free(new_node); 1551 } 1552 1553 if (perf_env__e_machine(machine->env, /*e_flags=*/NULL) == EM_X86_64) { 1554 u64 addr; 1555 1556 /* 1557 * If one of the corresponding symbols is there, assume the 1558 * entry trampoline maps are too. 1559 */ 1560 if (!kallsyms__get_function_start(kallsyms_filename, 1561 ENTRY_TRAMPOLINE_NAME, 1562 &addr)) 1563 machine->trampolines_mapped = true; 1564 } 1565 1566 /* 1567 * Set the data type and long name so that kcore can be read via 1568 * dso__data_read_addr(). 1569 */ 1570 if (dso__kernel(dso) == DSO_SPACE__KERNEL_GUEST) 1571 dso__set_binary_type(dso, DSO_BINARY_TYPE__GUEST_KCORE); 1572 else 1573 dso__set_binary_type(dso, DSO_BINARY_TYPE__KCORE); 1574 dso__set_long_name(dso, strdup(kcore_filename), true); 1575 1576 close(fd); 1577 1578 if (map__prot(map) & PROT_EXEC) 1579 pr_debug("Using %s for kernel object code\n", kcore_filename); 1580 else 1581 pr_debug("Using %s for kernel data\n", kcore_filename); 1582 1583 return 0; 1584 1585 out_err: 1586 while (!list_empty(&md.maps)) { 1587 struct map_list_node *list_node; 1588 1589 list_node = list_entry(md.maps.next, struct map_list_node, node); 1590 list_del_init(&list_node->node); 1591 map__zput(list_node->map); 1592 free(list_node); 1593 } 1594 close(fd); 1595 return err; 1596 } 1597 1598 /* 1599 * If the kernel is relocated at boot time, kallsyms won't match. Compute the 1600 * delta based on the relocation reference symbol. 1601 */ 1602 static int kallsyms__delta(struct kmap *kmap, const char *filename, u64 *delta) 1603 { 1604 u64 addr; 1605 1606 if (!kmap->ref_reloc_sym || !kmap->ref_reloc_sym->name) 1607 return 0; 1608 1609 if (kallsyms__get_function_start(filename, kmap->ref_reloc_sym->name, &addr)) 1610 return -1; 1611 1612 *delta = addr - kmap->ref_reloc_sym->addr; 1613 return 0; 1614 } 1615 1616 int __dso__load_kallsyms(struct dso *dso, const char *filename, 1617 struct map *map, bool no_kcore) 1618 { 1619 struct kmap *kmap = map__kmap(map); 1620 u64 delta = 0; 1621 1622 if (symbol__restricted_filename(filename, "/proc/kallsyms")) 1623 return -1; 1624 1625 if (!kmap || !kmap->kmaps) 1626 return -1; 1627 1628 if (dso__load_all_kallsyms(dso, filename) < 0) 1629 return -1; 1630 1631 if (kallsyms__delta(kmap, filename, &delta)) 1632 return -1; 1633 1634 symbols__fixup_end(dso__symbols(dso), true); 1635 symbols__fixup_duplicate(dso__symbols(dso)); 1636 1637 if (dso__kernel(dso) == DSO_SPACE__KERNEL_GUEST) 1638 dso__set_symtab_type(dso, DSO_BINARY_TYPE__GUEST_KALLSYMS); 1639 else 1640 dso__set_symtab_type(dso, DSO_BINARY_TYPE__KALLSYMS); 1641 1642 if (!no_kcore && !dso__load_kcore(dso, map, filename)) 1643 return maps__split_kallsyms_for_kcore(kmap->kmaps, dso); 1644 else 1645 return maps__split_kallsyms(kmap->kmaps, dso, delta, map); 1646 } 1647 1648 int dso__load_kallsyms(struct dso *dso, const char *filename, 1649 struct map *map) 1650 { 1651 return __dso__load_kallsyms(dso, filename, map, false); 1652 } 1653 1654 static int dso__load_perf_map(const char *map_path, struct dso *dso) 1655 { 1656 char *line = NULL; 1657 size_t n; 1658 FILE *file; 1659 int nr_syms = 0; 1660 1661 file = fopen(map_path, "r"); 1662 if (file == NULL) 1663 goto out_failure; 1664 1665 while (!feof(file)) { 1666 u64 start, size; 1667 struct symbol *sym; 1668 int line_len, len; 1669 1670 line_len = getline(&line, &n, file); 1671 if (line_len < 0) 1672 break; 1673 1674 if (!line) 1675 goto out_failure; 1676 1677 line[--line_len] = '\0'; /* \n */ 1678 1679 len = hex2u64(line, &start); 1680 1681 len++; 1682 if (len + 2 >= line_len) 1683 continue; 1684 1685 len += hex2u64(line + len, &size); 1686 1687 len++; 1688 if (len + 2 >= line_len) 1689 continue; 1690 1691 sym = symbol__new(start, size, STB_GLOBAL, STT_FUNC, line + len); 1692 1693 if (sym == NULL) 1694 goto out_delete_line; 1695 1696 symbols__insert(dso__symbols(dso), sym); 1697 nr_syms++; 1698 } 1699 1700 free(line); 1701 fclose(file); 1702 1703 return nr_syms; 1704 1705 out_delete_line: 1706 free(line); 1707 out_failure: 1708 return -1; 1709 } 1710 1711 static bool dso__is_compatible_symtab_type(struct dso *dso, bool kmod, 1712 enum dso_binary_type type) 1713 { 1714 switch (type) { 1715 case DSO_BINARY_TYPE__JAVA_JIT: 1716 case DSO_BINARY_TYPE__DEBUGLINK: 1717 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO: 1718 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO: 1719 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO: 1720 case DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO: 1721 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO: 1722 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO: 1723 case DSO_BINARY_TYPE__GNU_DEBUGDATA: 1724 return !kmod && dso__kernel(dso) == DSO_SPACE__USER; 1725 1726 case DSO_BINARY_TYPE__KALLSYMS: 1727 case DSO_BINARY_TYPE__VMLINUX: 1728 case DSO_BINARY_TYPE__KCORE: 1729 return dso__kernel(dso) == DSO_SPACE__KERNEL; 1730 1731 case DSO_BINARY_TYPE__GUEST_KALLSYMS: 1732 case DSO_BINARY_TYPE__GUEST_VMLINUX: 1733 case DSO_BINARY_TYPE__GUEST_KCORE: 1734 return dso__kernel(dso) == DSO_SPACE__KERNEL_GUEST; 1735 1736 case DSO_BINARY_TYPE__GUEST_KMODULE: 1737 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP: 1738 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE: 1739 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP: 1740 /* 1741 * kernel modules know their symtab type - it's set when 1742 * creating a module dso in machine__addnew_module_map(). 1743 */ 1744 return kmod && dso__symtab_type(dso) == type; 1745 1746 case DSO_BINARY_TYPE__BUILD_ID_CACHE: 1747 case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO: 1748 return true; 1749 1750 case DSO_BINARY_TYPE__BPF_PROG_INFO: 1751 case DSO_BINARY_TYPE__BPF_IMAGE: 1752 case DSO_BINARY_TYPE__OOL: 1753 case DSO_BINARY_TYPE__NOT_FOUND: 1754 default: 1755 return false; 1756 } 1757 } 1758 1759 /* Checks for the existence of the perf-<pid>.map file in two different 1760 * locations. First, if the process is a separate mount namespace, check in 1761 * that namespace using the pid of the innermost pid namespace. If's not in a 1762 * namespace, or the file can't be found there, try in the mount namespace of 1763 * the tracing process using our view of its pid. 1764 */ 1765 static int dso__find_perf_map(char *filebuf, size_t bufsz, 1766 struct nsinfo **nsip) 1767 { 1768 struct nscookie nsc; 1769 struct nsinfo *nsi; 1770 struct nsinfo *nnsi; 1771 int rc = -1; 1772 1773 nsi = *nsip; 1774 1775 if (nsinfo__need_setns(nsi)) { 1776 snprintf(filebuf, bufsz, "/tmp/perf-%d.map", nsinfo__nstgid(nsi)); 1777 nsinfo__mountns_enter(nsi, &nsc); 1778 rc = access(filebuf, R_OK); 1779 nsinfo__mountns_exit(&nsc); 1780 if (rc == 0) 1781 return rc; 1782 } 1783 1784 nnsi = nsinfo__copy(nsi); 1785 if (nnsi) { 1786 nsinfo__put(nsi); 1787 1788 nsinfo__clear_need_setns(nnsi); 1789 snprintf(filebuf, bufsz, "/tmp/perf-%d.map", nsinfo__tgid(nnsi)); 1790 *nsip = nnsi; 1791 rc = 0; 1792 } 1793 1794 return rc; 1795 } 1796 1797 int dso__load(struct dso *dso, struct map *map) 1798 { 1799 char *name; 1800 int ret = -1; 1801 u_int i; 1802 struct machine *machine = NULL; 1803 char *root_dir = (char *) ""; 1804 int ss_pos = 0; 1805 struct symsrc ss_[2]; 1806 struct symsrc *syms_ss = NULL, *runtime_ss = NULL; 1807 bool kmod; 1808 bool perfmap; 1809 struct nscookie nsc; 1810 char newmapname[PATH_MAX]; 1811 const char *map_path = dso__long_name(dso); 1812 1813 mutex_lock(dso__lock(dso)); 1814 perfmap = is_perf_pid_map_name(map_path); 1815 1816 if (perfmap) { 1817 if (dso__nsinfo(dso) && 1818 (dso__find_perf_map(newmapname, sizeof(newmapname), 1819 dso__nsinfo_ptr(dso)) == 0)) { 1820 map_path = newmapname; 1821 } 1822 } 1823 1824 nsinfo__mountns_enter(dso__nsinfo(dso), &nsc); 1825 1826 /* check again under the dso->lock */ 1827 if (dso__loaded(dso)) { 1828 ret = 1; 1829 goto out; 1830 } 1831 1832 kmod = dso__is_kmod(dso); 1833 1834 if (dso__kernel(dso) && !kmod) { 1835 if (dso__kernel(dso) == DSO_SPACE__KERNEL) 1836 ret = dso__load_kernel_sym(dso, map); 1837 else if (dso__kernel(dso) == DSO_SPACE__KERNEL_GUEST) 1838 ret = dso__load_guest_kernel_sym(dso, map); 1839 1840 machine = maps__machine(map__kmaps(map)); 1841 if (perf_env__e_machine(machine->env, /*e_flags=*/NULL) == EM_X86_64) 1842 machine__map_x86_64_entry_trampolines(machine, dso); 1843 goto out; 1844 } 1845 1846 dso__set_adjust_symbols(dso, false); 1847 1848 if (perfmap) { 1849 ret = dso__load_perf_map(map_path, dso); 1850 dso__set_binary_type(dso, DSO_BINARY_TYPE__JAVA_JIT); 1851 dso__set_symtab_type(dso, ret > 0 1852 ? DSO_BINARY_TYPE__JAVA_JIT 1853 : DSO_BINARY_TYPE__NOT_FOUND); 1854 goto out; 1855 } 1856 1857 if (machine) 1858 root_dir = machine->root_dir; 1859 1860 name = malloc(PATH_MAX); 1861 if (!name) 1862 goto out; 1863 1864 /* 1865 * Read the build id if possible. This is required for 1866 * DSO_BINARY_TYPE__BUILDID_DEBUGINFO to work. 1867 */ 1868 if (!dso__has_build_id(dso)) { 1869 struct build_id bid = { .size = 0, }; 1870 1871 __symbol__join_symfs(name, PATH_MAX, dso__long_name(dso)); 1872 if (filename__read_build_id(name, &bid) > 0) 1873 dso__set_build_id(dso, &bid); 1874 } 1875 1876 /* 1877 * Iterate over candidate debug images. 1878 * Keep track of "interesting" ones (those which have a symtab, dynsym, 1879 * and/or opd section) for processing. 1880 */ 1881 for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) { 1882 struct symsrc *ss = &ss_[ss_pos]; 1883 bool next_slot = false; 1884 bool is_reg; 1885 bool nsexit; 1886 int bfdrc = -1; 1887 int sirc = -1; 1888 1889 enum dso_binary_type symtab_type = binary_type_symtab[i]; 1890 1891 nsexit = (symtab_type == DSO_BINARY_TYPE__BUILD_ID_CACHE || 1892 symtab_type == DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO); 1893 1894 if (!dso__is_compatible_symtab_type(dso, kmod, symtab_type)) 1895 continue; 1896 1897 if (dso__read_binary_type_filename(dso, symtab_type, 1898 root_dir, name, PATH_MAX)) 1899 continue; 1900 1901 if (nsexit) 1902 nsinfo__mountns_exit(&nsc); 1903 1904 is_reg = is_regular_file(name); 1905 if (!is_reg && errno == ENOENT && dso__nsinfo(dso)) { 1906 char *new_name = dso__filename_with_chroot(dso, name); 1907 if (new_name) { 1908 is_reg = is_regular_file(new_name); 1909 strlcpy(name, new_name, PATH_MAX); 1910 free(new_name); 1911 } 1912 } 1913 1914 #ifdef HAVE_LIBBFD_SUPPORT 1915 if (is_reg) 1916 bfdrc = dso__load_bfd_symbols(dso, name); 1917 #endif 1918 if (is_reg && bfdrc < 0) 1919 sirc = symsrc__init(ss, dso, name, symtab_type); 1920 1921 if (nsexit) 1922 nsinfo__mountns_enter(dso__nsinfo(dso), &nsc); 1923 1924 if (bfdrc > 0) { 1925 dso__set_symtab_type(dso, symtab_type); 1926 ret = 1; 1927 goto out_destroy; 1928 } 1929 1930 if (!is_reg || sirc < 0) 1931 continue; 1932 1933 if (!syms_ss && symsrc__has_symtab(ss)) { 1934 syms_ss = ss; 1935 next_slot = true; 1936 if (!dso__symsrc_filename(dso)) 1937 dso__set_symsrc_filename(dso, strdup(name)); 1938 } 1939 1940 if (!runtime_ss && symsrc__possibly_runtime(ss)) { 1941 runtime_ss = ss; 1942 next_slot = true; 1943 } 1944 1945 if (next_slot) { 1946 ss_pos++; 1947 if (syms_ss && runtime_ss) 1948 break; 1949 } else { 1950 symsrc__destroy(ss); 1951 } 1952 1953 } 1954 1955 if (!runtime_ss && !syms_ss) 1956 goto out_free; 1957 1958 if (runtime_ss && !syms_ss) { 1959 syms_ss = runtime_ss; 1960 } 1961 1962 /* We'll have to hope for the best */ 1963 if (!runtime_ss && syms_ss) 1964 runtime_ss = syms_ss; 1965 1966 if (syms_ss) 1967 ret = dso__load_sym(dso, map, syms_ss, runtime_ss, kmod); 1968 else 1969 ret = -1; 1970 1971 if (ret > 0) { 1972 int nr_plt; 1973 1974 nr_plt = dso__synthesize_plt_symbols(dso, runtime_ss); 1975 if (nr_plt > 0) 1976 ret += nr_plt; 1977 } 1978 1979 out_destroy: 1980 for (; ss_pos > 0; ss_pos--) 1981 symsrc__destroy(&ss_[ss_pos - 1]); 1982 out_free: 1983 free(name); 1984 if (ret < 0 && strstr(dso__name(dso), " (deleted)") != NULL) 1985 ret = 0; 1986 out: 1987 dso__set_loaded(dso); 1988 mutex_unlock(dso__lock(dso)); 1989 nsinfo__mountns_exit(&nsc); 1990 1991 return ret; 1992 } 1993 1994 /* 1995 * Always takes ownership of vmlinux when vmlinux_allocated == true, even if 1996 * it returns an error. 1997 */ 1998 int dso__load_vmlinux(struct dso *dso, struct map *map, 1999 const char *vmlinux, bool vmlinux_allocated) 2000 { 2001 int err = -1; 2002 struct symsrc ss; 2003 char symfs_vmlinux[PATH_MAX]; 2004 enum dso_binary_type symtab_type; 2005 2006 if (vmlinux[0] == '/') 2007 snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s", vmlinux); 2008 else 2009 symbol__join_symfs(symfs_vmlinux, vmlinux); 2010 2011 if (dso__kernel(dso) == DSO_SPACE__KERNEL_GUEST) 2012 symtab_type = DSO_BINARY_TYPE__GUEST_VMLINUX; 2013 else 2014 symtab_type = DSO_BINARY_TYPE__VMLINUX; 2015 2016 if (symsrc__init(&ss, dso, symfs_vmlinux, symtab_type)) { 2017 if (vmlinux_allocated) 2018 free((char *) vmlinux); 2019 return -1; 2020 } 2021 2022 /* 2023 * dso__load_sym() may copy 'dso' which will result in the copies having 2024 * an incorrect long name unless we set it here first. 2025 */ 2026 dso__set_long_name(dso, vmlinux, vmlinux_allocated); 2027 if (dso__kernel(dso) == DSO_SPACE__KERNEL_GUEST) 2028 dso__set_binary_type(dso, DSO_BINARY_TYPE__GUEST_VMLINUX); 2029 else 2030 dso__set_binary_type(dso, DSO_BINARY_TYPE__VMLINUX); 2031 2032 err = dso__load_sym(dso, map, &ss, &ss, 0); 2033 symsrc__destroy(&ss); 2034 2035 if (err > 0) { 2036 dso__set_loaded(dso); 2037 pr_debug("Using %s for symbols\n", symfs_vmlinux); 2038 } 2039 2040 return err; 2041 } 2042 2043 int dso__load_vmlinux_path(struct dso *dso, struct map *map) 2044 { 2045 int i, err = 0; 2046 char *filename = NULL; 2047 2048 pr_debug("Looking at the vmlinux_path (%d entries long)\n", 2049 vmlinux_path__nr_entries + 1); 2050 2051 for (i = 0; i < vmlinux_path__nr_entries; ++i) { 2052 err = dso__load_vmlinux(dso, map, vmlinux_path[i], false); 2053 if (err > 0) 2054 goto out; 2055 } 2056 2057 if (!symbol_conf.ignore_vmlinux_buildid) 2058 filename = dso__build_id_filename(dso, NULL, 0, false); 2059 if (filename != NULL) { 2060 err = dso__load_vmlinux(dso, map, filename, true); 2061 if (err > 0) 2062 dso__set_symtab_type(dso, DSO_BINARY_TYPE__BUILD_ID_CACHE); 2063 } 2064 out: 2065 return err; 2066 } 2067 2068 static bool visible_dir_filter(const char *name, struct dirent *d) 2069 { 2070 if (d->d_type != DT_DIR) 2071 return false; 2072 return lsdir_no_dot_filter(name, d); 2073 } 2074 2075 static int find_matching_kcore(struct map *map, char *dir, size_t dir_sz) 2076 { 2077 char kallsyms_filename[PATH_MAX]; 2078 int ret = -1; 2079 struct strlist *dirs; 2080 struct str_node *nd; 2081 2082 dirs = lsdir(dir, visible_dir_filter); 2083 if (!dirs) 2084 return -1; 2085 2086 strlist__for_each_entry(nd, dirs) { 2087 scnprintf(kallsyms_filename, sizeof(kallsyms_filename), 2088 "%s/%s/kallsyms", dir, nd->s); 2089 if (!validate_kcore_addresses(kallsyms_filename, map)) { 2090 strlcpy(dir, kallsyms_filename, dir_sz); 2091 ret = 0; 2092 break; 2093 } 2094 } 2095 2096 strlist__delete(dirs); 2097 2098 return ret; 2099 } 2100 2101 /* 2102 * Use open(O_RDONLY) to check readability directly instead of access(R_OK) 2103 * since access(R_OK) only checks with real UID/GID but open() use effective 2104 * UID/GID and actual capabilities (e.g. /proc/kcore requires CAP_SYS_RAWIO). 2105 */ 2106 static bool filename__readable(const char *file) 2107 { 2108 int fd = open(file, O_RDONLY); 2109 if (fd < 0) 2110 return false; 2111 close(fd); 2112 return true; 2113 } 2114 2115 static char *dso__find_kallsyms(struct dso *dso, struct map *map) 2116 { 2117 struct build_id bid = { .size = 0, }; 2118 char sbuild_id[SBUILD_ID_SIZE]; 2119 bool is_host = false; 2120 char path[PATH_MAX]; 2121 struct maps *kmaps = map__kmaps(map); 2122 2123 if (!dso__has_build_id(dso)) { 2124 /* 2125 * Last resort, if we don't have a build-id and couldn't find 2126 * any vmlinux file, try the running kernel kallsyms table. 2127 */ 2128 goto proc_kallsyms; 2129 } 2130 2131 if (sysfs__read_build_id("/sys/kernel/notes", &bid) == 0) 2132 is_host = dso__build_id_equal(dso, &bid); 2133 2134 /* Try a fast path for /proc/kallsyms if possible */ 2135 if (is_host) { 2136 /* 2137 * Do not check the build-id cache, unless we know we cannot use 2138 * /proc/kcore or module maps don't match to /proc/kallsyms. 2139 * To check readability of /proc/kcore, do not use access(R_OK) 2140 * since /proc/kcore requires CAP_SYS_RAWIO to read and access 2141 * can't check it. 2142 */ 2143 if (filename__readable("/proc/kcore") && 2144 !validate_kcore_addresses("/proc/kallsyms", map)) 2145 goto proc_kallsyms; 2146 } 2147 2148 build_id__snprintf(dso__bid(dso), sbuild_id, sizeof(sbuild_id)); 2149 2150 /* Find kallsyms in build-id cache with kcore */ 2151 scnprintf(path, sizeof(path), "%s/%s/%s", 2152 buildid_dir, DSO__NAME_KCORE, sbuild_id); 2153 2154 if (!find_matching_kcore(map, path, sizeof(path))) 2155 return strdup(path); 2156 2157 /* Use current /proc/kallsyms if possible */ 2158 proc_kallsyms: 2159 if (kmaps) { 2160 struct machine *machine = maps__machine(kmaps); 2161 2162 scnprintf(path, sizeof(path), "%s/proc/kallsyms", machine->root_dir); 2163 return strdup(path); 2164 } else if (is_host) { 2165 return strdup("/proc/kallsyms"); 2166 } 2167 2168 /* Finally, find a cache of kallsyms */ 2169 if (!build_id_cache__kallsyms_path(sbuild_id, path, sizeof(path))) { 2170 pr_err("No kallsyms or vmlinux with build-id %s was found\n", 2171 sbuild_id); 2172 return NULL; 2173 } 2174 2175 return strdup(path); 2176 } 2177 2178 static int dso__load_kernel_sym(struct dso *dso, struct map *map) 2179 { 2180 int err; 2181 const char *kallsyms_filename = NULL; 2182 char *kallsyms_allocated_filename = NULL; 2183 char *filename = NULL; 2184 2185 /* 2186 * Step 1: if the user specified a kallsyms or vmlinux filename, use 2187 * it and only it, reporting errors to the user if it cannot be used. 2188 * 2189 * For instance, try to analyse an ARM perf.data file _without_ a 2190 * build-id, or if the user specifies the wrong path to the right 2191 * vmlinux file, obviously we can't fallback to another vmlinux (a 2192 * x86_86 one, on the machine where analysis is being performed, say), 2193 * or worse, /proc/kallsyms. 2194 * 2195 * If the specified file _has_ a build-id and there is a build-id 2196 * section in the perf.data file, we will still do the expected 2197 * validation in dso__load_vmlinux and will bail out if they don't 2198 * match. 2199 */ 2200 if (symbol_conf.kallsyms_name != NULL) { 2201 kallsyms_filename = symbol_conf.kallsyms_name; 2202 goto do_kallsyms; 2203 } 2204 2205 if (!symbol_conf.ignore_vmlinux && symbol_conf.vmlinux_name != NULL) { 2206 return dso__load_vmlinux(dso, map, symbol_conf.vmlinux_name, false); 2207 } 2208 2209 /* 2210 * Before checking on common vmlinux locations, check if it's 2211 * stored as standard build id binary (not kallsyms) under 2212 * .debug cache. 2213 */ 2214 if (!symbol_conf.ignore_vmlinux_buildid) 2215 filename = __dso__build_id_filename(dso, NULL, 0, false, false); 2216 if (filename != NULL) { 2217 err = dso__load_vmlinux(dso, map, filename, true); 2218 if (err > 0) { 2219 dso__set_symtab_type(dso, DSO_BINARY_TYPE__BUILD_ID_CACHE); 2220 return err; 2221 } 2222 } 2223 2224 if (!symbol_conf.ignore_vmlinux && vmlinux_path != NULL) { 2225 err = dso__load_vmlinux_path(dso, map); 2226 if (err > 0) 2227 return err; 2228 } 2229 2230 /* do not try local files if a symfs was given */ 2231 if (symbol_conf.symfs[0] != 0) 2232 return -1; 2233 2234 kallsyms_allocated_filename = dso__find_kallsyms(dso, map); 2235 if (!kallsyms_allocated_filename) 2236 return -1; 2237 2238 kallsyms_filename = kallsyms_allocated_filename; 2239 2240 do_kallsyms: 2241 err = dso__load_kallsyms(dso, kallsyms_filename, map); 2242 if (err > 0) 2243 pr_debug("Using %s for symbols\n", kallsyms_filename); 2244 free(kallsyms_allocated_filename); 2245 2246 if (err > 0 && !dso__is_kcore(dso)) { 2247 struct maps *kmaps = map__kmaps(map); 2248 2249 dso__set_binary_type(dso, DSO_BINARY_TYPE__KALLSYMS); 2250 dso__set_long_name(dso, DSO__NAME_KALLSYMS, false); 2251 maps__mutate_mapping(kmaps, map, map_fixup_cb, NULL); 2252 } 2253 2254 return err; 2255 } 2256 2257 static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map) 2258 { 2259 int err; 2260 const char *kallsyms_filename; 2261 struct machine *machine = maps__machine(map__kmaps(map)); 2262 char path[PATH_MAX]; 2263 2264 if (machine->kallsyms_filename) { 2265 kallsyms_filename = machine->kallsyms_filename; 2266 } else if (machine__is_default_guest(machine)) { 2267 /* 2268 * if the user specified a vmlinux filename, use it and only 2269 * it, reporting errors to the user if it cannot be used. 2270 * Or use file guest_kallsyms inputted by user on commandline 2271 */ 2272 if (symbol_conf.default_guest_vmlinux_name != NULL) { 2273 err = dso__load_vmlinux(dso, map, 2274 symbol_conf.default_guest_vmlinux_name, 2275 false); 2276 return err; 2277 } 2278 2279 kallsyms_filename = symbol_conf.default_guest_kallsyms; 2280 if (!kallsyms_filename) 2281 return -1; 2282 } else { 2283 snprintf(path, sizeof(path), "%s/proc/kallsyms", machine->root_dir); 2284 kallsyms_filename = path; 2285 } 2286 2287 err = dso__load_kallsyms(dso, kallsyms_filename, map); 2288 if (err > 0) 2289 pr_debug("Using %s for symbols\n", kallsyms_filename); 2290 if (err > 0 && !dso__is_kcore(dso)) { 2291 struct maps *kmaps = map__kmaps(map); 2292 2293 dso__set_binary_type(dso, DSO_BINARY_TYPE__GUEST_KALLSYMS); 2294 dso__set_long_name(dso, machine->mmap_name, false); 2295 maps__mutate_mapping(kmaps, map, map_fixup_cb, NULL); 2296 } 2297 2298 return err; 2299 } 2300 2301 static void vmlinux_path__exit(void) 2302 { 2303 while (--vmlinux_path__nr_entries >= 0) 2304 zfree(&vmlinux_path[vmlinux_path__nr_entries]); 2305 vmlinux_path__nr_entries = 0; 2306 2307 zfree(&vmlinux_path); 2308 } 2309 2310 static const char * const vmlinux_paths[] = { 2311 "vmlinux", 2312 "/boot/vmlinux" 2313 }; 2314 2315 static const char * const vmlinux_paths_upd[] = { 2316 "/boot/vmlinux-%s", 2317 "/usr/lib/debug/boot/vmlinux-%s", 2318 "/lib/modules/%s/build/vmlinux", 2319 "/usr/lib/debug/lib/modules/%s/vmlinux", 2320 "/usr/lib/debug/boot/vmlinux-%s.debug" 2321 }; 2322 2323 static int vmlinux_path__add(const char *new_entry) 2324 { 2325 vmlinux_path[vmlinux_path__nr_entries] = strdup(new_entry); 2326 if (vmlinux_path[vmlinux_path__nr_entries] == NULL) 2327 return -1; 2328 ++vmlinux_path__nr_entries; 2329 2330 return 0; 2331 } 2332 2333 static int vmlinux_path__init(struct perf_env *env) 2334 { 2335 struct utsname uts; 2336 char bf[PATH_MAX]; 2337 const char *kernel_version; 2338 unsigned int i; 2339 2340 vmlinux_path = malloc(sizeof(char *) * (ARRAY_SIZE(vmlinux_paths) + 2341 ARRAY_SIZE(vmlinux_paths_upd))); 2342 if (vmlinux_path == NULL) 2343 return -1; 2344 2345 for (i = 0; i < ARRAY_SIZE(vmlinux_paths); i++) 2346 if (vmlinux_path__add(vmlinux_paths[i]) < 0) 2347 goto out_fail; 2348 2349 /* only try kernel version if no symfs was given */ 2350 if (symbol_conf.symfs[0] != 0) 2351 return 0; 2352 2353 if (env) { 2354 kernel_version = perf_env__os_release(env); 2355 } else { 2356 if (uname(&uts) < 0) 2357 goto out_fail; 2358 2359 kernel_version = uts.release; 2360 } 2361 2362 for (i = 0; i < ARRAY_SIZE(vmlinux_paths_upd); i++) { 2363 snprintf(bf, sizeof(bf), vmlinux_paths_upd[i], kernel_version); 2364 if (vmlinux_path__add(bf) < 0) 2365 goto out_fail; 2366 } 2367 2368 return 0; 2369 2370 out_fail: 2371 vmlinux_path__exit(); 2372 return -1; 2373 } 2374 2375 int setup_list(struct strlist **list, const char *list_str, 2376 const char *list_name) 2377 { 2378 if (list_str == NULL) 2379 return 0; 2380 2381 *list = strlist__new(list_str, NULL); 2382 if (!*list) { 2383 pr_err("problems parsing %s list\n", list_name); 2384 return -1; 2385 } 2386 2387 symbol_conf.has_filter = true; 2388 return 0; 2389 } 2390 2391 int setup_intlist(struct intlist **list, const char *list_str, 2392 const char *list_name) 2393 { 2394 if (list_str == NULL) 2395 return 0; 2396 2397 *list = intlist__new(list_str); 2398 if (!*list) { 2399 pr_err("problems parsing %s list\n", list_name); 2400 return -1; 2401 } 2402 return 0; 2403 } 2404 2405 static int setup_addrlist(struct intlist **addr_list, struct strlist *sym_list) 2406 { 2407 struct str_node *pos, *tmp; 2408 unsigned long val; 2409 char *sep; 2410 const char *end; 2411 int i = 0, err; 2412 2413 *addr_list = intlist__new(NULL); 2414 if (!*addr_list) 2415 return -1; 2416 2417 strlist__for_each_entry_safe(pos, tmp, sym_list) { 2418 errno = 0; 2419 val = strtoul(pos->s, &sep, 16); 2420 if (errno || (sep == pos->s)) 2421 continue; 2422 2423 if (*sep != '\0') { 2424 end = pos->s + strlen(pos->s) - 1; 2425 while (end >= sep && isspace(*end)) 2426 end--; 2427 2428 if (end >= sep) 2429 continue; 2430 } 2431 2432 err = intlist__add(*addr_list, val); 2433 if (err) 2434 break; 2435 2436 strlist__remove(sym_list, pos); 2437 i++; 2438 } 2439 2440 if (i == 0) { 2441 intlist__delete(*addr_list); 2442 *addr_list = NULL; 2443 } 2444 2445 return 0; 2446 } 2447 2448 static bool symbol__read_kptr_restrict(void) 2449 { 2450 bool value = false; 2451 FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r"); 2452 bool cap_syslog = perf_cap__capable(CAP_SYSLOG); 2453 2454 if (fp != NULL) { 2455 char line[8]; 2456 2457 if (fgets(line, sizeof(line), fp) != NULL) 2458 value = cap_syslog ? (atoi(line) >= 2) : (atoi(line) != 0); 2459 2460 fclose(fp); 2461 } 2462 2463 /* Per kernel/kallsyms.c: 2464 * we also restrict when perf_event_paranoid > 1 w/o CAP_SYSLOG 2465 */ 2466 if (perf_event_paranoid() > 1 && !cap_syslog) 2467 value = true; 2468 2469 return value; 2470 } 2471 2472 int symbol__annotation_init(void) 2473 { 2474 if (symbol_conf.init_annotation) 2475 return 0; 2476 2477 if (symbol_conf.initialized) { 2478 pr_err("Annotation needs to be init before symbol__init()\n"); 2479 return -1; 2480 } 2481 2482 symbol_conf.priv_size += sizeof(struct annotation); 2483 symbol_conf.init_annotation = true; 2484 return 0; 2485 } 2486 2487 static int setup_parallelism_bitmap(void) 2488 { 2489 struct perf_cpu_map *map; 2490 struct perf_cpu cpu; 2491 unsigned int i; 2492 int err = -1; 2493 2494 if (symbol_conf.parallelism_list_str == NULL) 2495 return 0; 2496 2497 map = perf_cpu_map__new(symbol_conf.parallelism_list_str); 2498 if (map == NULL) { 2499 pr_err("failed to parse parallelism filter list\n"); 2500 return -1; 2501 } 2502 2503 bitmap_fill(symbol_conf.parallelism_filter, MAX_NR_CPUS + 1); 2504 perf_cpu_map__for_each_cpu(cpu, i, map) { 2505 if (cpu.cpu <= 0 || cpu.cpu > MAX_NR_CPUS) { 2506 pr_err("Requested parallelism level %d is invalid.\n", cpu.cpu); 2507 goto out_delete_map; 2508 } 2509 __clear_bit(cpu.cpu, symbol_conf.parallelism_filter); 2510 } 2511 2512 err = 0; 2513 out_delete_map: 2514 perf_cpu_map__put(map); 2515 return err; 2516 } 2517 2518 int symbol__init(struct perf_env *env) 2519 { 2520 const char *symfs; 2521 2522 if (symbol_conf.initialized) 2523 return 0; 2524 2525 symbol_conf.priv_size = PERF_ALIGN(symbol_conf.priv_size, sizeof(u64)); 2526 2527 symbol__elf_init(); 2528 2529 if (symbol_conf.try_vmlinux_path && vmlinux_path__init(env) < 0) 2530 return -1; 2531 2532 if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') { 2533 pr_err("'.' is the only non valid --field-separator argument\n"); 2534 return -1; 2535 } 2536 2537 if (setup_parallelism_bitmap()) 2538 return -1; 2539 2540 if (setup_list(&symbol_conf.dso_list, 2541 symbol_conf.dso_list_str, "dso") < 0) 2542 return -1; 2543 2544 if (setup_list(&symbol_conf.comm_list, 2545 symbol_conf.comm_list_str, "comm") < 0) 2546 goto out_free_dso_list; 2547 2548 if (setup_intlist(&symbol_conf.pid_list, 2549 symbol_conf.pid_list_str, "pid") < 0) 2550 goto out_free_comm_list; 2551 2552 if (setup_intlist(&symbol_conf.tid_list, 2553 symbol_conf.tid_list_str, "tid") < 0) 2554 goto out_free_pid_list; 2555 2556 if (setup_list(&symbol_conf.sym_list, 2557 symbol_conf.sym_list_str, "symbol") < 0) 2558 goto out_free_tid_list; 2559 2560 if (symbol_conf.sym_list && 2561 setup_addrlist(&symbol_conf.addr_list, symbol_conf.sym_list) < 0) 2562 goto out_free_sym_list; 2563 2564 if (setup_list(&symbol_conf.bt_stop_list, 2565 symbol_conf.bt_stop_list_str, "symbol") < 0) 2566 goto out_free_sym_list; 2567 2568 /* 2569 * A path to symbols of "/" is identical to "" 2570 * reset here for simplicity. 2571 */ 2572 symfs = realpath(symbol_conf.symfs, NULL); 2573 if (symfs == NULL) 2574 symfs = symbol_conf.symfs; 2575 if (strcmp(symfs, "/") == 0) 2576 symbol_conf.symfs = ""; 2577 if (symfs != symbol_conf.symfs) 2578 free((void *)symfs); 2579 2580 symbol_conf.kptr_restrict = symbol__read_kptr_restrict(); 2581 2582 symbol_conf.initialized = true; 2583 return 0; 2584 2585 out_free_sym_list: 2586 strlist__delete(symbol_conf.sym_list); 2587 intlist__delete(symbol_conf.addr_list); 2588 out_free_tid_list: 2589 intlist__delete(symbol_conf.tid_list); 2590 out_free_pid_list: 2591 intlist__delete(symbol_conf.pid_list); 2592 out_free_comm_list: 2593 strlist__delete(symbol_conf.comm_list); 2594 out_free_dso_list: 2595 strlist__delete(symbol_conf.dso_list); 2596 return -1; 2597 } 2598 2599 void symbol__exit(void) 2600 { 2601 if (!symbol_conf.initialized) 2602 return; 2603 2604 strlist__delete(symbol_conf.bt_stop_list); 2605 strlist__delete(symbol_conf.sym_list); 2606 strlist__delete(symbol_conf.dso_list); 2607 strlist__delete(symbol_conf.comm_list); 2608 intlist__delete(symbol_conf.tid_list); 2609 intlist__delete(symbol_conf.pid_list); 2610 intlist__delete(symbol_conf.addr_list); 2611 vmlinux_path__exit(); 2612 symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL; 2613 symbol_conf.bt_stop_list = NULL; 2614 symbol_conf.initialized = false; 2615 } 2616 2617 int symbol__config_symfs(const struct option *opt __maybe_unused, 2618 const char *dir, int unset __maybe_unused) 2619 { 2620 char *bf = NULL; 2621 const char *layout_str; 2622 char *dir_copy; 2623 int ret; 2624 2625 layout_str = strrchr(dir, ','); 2626 if (layout_str) { 2627 size_t dir_len = layout_str - dir; 2628 2629 dir_copy = strndup(dir, dir_len); 2630 if (dir_copy == NULL) 2631 return -ENOMEM; 2632 2633 symbol_conf.symfs = dir_copy; 2634 2635 layout_str++; 2636 if (!strcmp(layout_str, "flat")) 2637 symbol_conf.symfs_layout_flat = true; 2638 else if (!strcmp(layout_str, "hierarchy")) 2639 symbol_conf.symfs_layout_flat = false; 2640 else { 2641 pr_err("Invalid layout: '%s', use 'hierarchy' or 'flat'\n", 2642 layout_str); 2643 free(dir_copy); 2644 return -EINVAL; 2645 } 2646 } else { 2647 symbol_conf.symfs = strdup(dir); 2648 if (symbol_conf.symfs == NULL) 2649 return -ENOMEM; 2650 symbol_conf.symfs_layout_flat = false; 2651 } 2652 2653 /* skip the locally configured cache if a symfs is given, and 2654 * config buildid dir to symfs/.debug 2655 */ 2656 ret = asprintf(&bf, "%s/%s", symbol_conf.symfs, ".debug"); 2657 if (ret < 0) 2658 return -ENOMEM; 2659 2660 set_buildid_dir(bf); 2661 2662 free(bf); 2663 return 0; 2664 } 2665 2666 /* 2667 * Checks that user supplied symbol kernel files are accessible because 2668 * the default mechanism for accessing elf files fails silently. i.e. if 2669 * debug syms for a build ID aren't found perf carries on normally. When 2670 * they are user supplied we should assume that the user doesn't want to 2671 * silently fail. 2672 */ 2673 int symbol__validate_sym_arguments(void) 2674 { 2675 if (symbol_conf.vmlinux_name && 2676 access(symbol_conf.vmlinux_name, R_OK)) { 2677 pr_err("Invalid file: %s\n", symbol_conf.vmlinux_name); 2678 return -EINVAL; 2679 } 2680 if (symbol_conf.kallsyms_name && 2681 access(symbol_conf.kallsyms_name, R_OK)) { 2682 pr_err("Invalid file: %s\n", symbol_conf.kallsyms_name); 2683 return -EINVAL; 2684 } 2685 return 0; 2686 } 2687 2688 static bool want_demangle(bool is_kernel_sym) 2689 { 2690 return is_kernel_sym ? symbol_conf.demangle_kernel : symbol_conf.demangle; 2691 } 2692 2693 /* 2694 * Demangle C++ function signature, typically replaced by demangle-cxx.cpp 2695 * version. 2696 */ 2697 #ifndef HAVE_CXA_DEMANGLE_SUPPORT 2698 #ifdef HAVE_CPLUS_DEMANGLE_SUPPORT 2699 /* 2700 * Declarations from libiberty's demangle.h, the header isn't installed by 2701 * every binutils package. 2702 */ 2703 #ifndef DMGL_PARAMS 2704 #define DMGL_PARAMS (1 << 0) /* Include function arguments. */ 2705 #define DMGL_ANSI (1 << 1) /* Include const, volatile, etc. */ 2706 #endif 2707 2708 char *cplus_demangle(const char *mangled, int options); 2709 #endif 2710 2711 char *cxx_demangle_sym(const char *str __maybe_unused, bool params __maybe_unused, 2712 bool modifiers __maybe_unused) 2713 { 2714 #ifdef HAVE_LIBBFD_SUPPORT 2715 return libbfd__demangle_sym(str, params, modifiers); 2716 #elif defined(HAVE_CPLUS_DEMANGLE_SUPPORT) 2717 int flags = (params ? DMGL_PARAMS : 0) | (modifiers ? DMGL_ANSI : 0); 2718 2719 return cplus_demangle(str, flags); 2720 #else 2721 return NULL; 2722 #endif 2723 } 2724 #endif /* !HAVE_CXA_DEMANGLE_SUPPORT */ 2725 2726 char *dso__demangle_sym(struct dso *dso, int kmodule, const char *elf_name) 2727 { 2728 struct demangle rust_demangle = { 2729 .style = DemangleStyleUnknown, 2730 }; 2731 char *demangled = NULL; 2732 2733 /* 2734 * We need to figure out if the object was created from C++ sources 2735 * DWARF DW_compile_unit has this, but we don't always have access 2736 * to it... 2737 */ 2738 if (!want_demangle((dso && dso__kernel(dso)) || kmodule)) 2739 return demangled; 2740 2741 rust_demangle_demangle(elf_name, &rust_demangle); 2742 if (rust_demangle_is_known(&rust_demangle)) { 2743 /* A rust mangled name. */ 2744 if (rust_demangle.mangled_len == 0) 2745 return demangled; 2746 2747 for (size_t buf_len = roundup_pow_of_two(rust_demangle.mangled_len * 2); 2748 buf_len < 1024 * 1024; buf_len += 32) { 2749 char *tmp = realloc(demangled, buf_len); 2750 2751 if (!tmp) 2752 break; 2753 demangled = tmp; 2754 if (rust_demangle_display_demangle(&rust_demangle, demangled, buf_len, 2755 /*alternate=*/true) == OverflowOk) 2756 return demangled; 2757 } 2758 /* Failure to grow output buffer or buffer exceeded sensible bounds. */ 2759 free(demangled); 2760 return NULL; 2761 } 2762 2763 demangled = cxx_demangle_sym(elf_name, verbose > 0, verbose > 0); 2764 if (demangled) 2765 return demangled; 2766 2767 demangled = ocaml_demangle_sym(elf_name); 2768 if (demangled) 2769 return demangled; 2770 2771 return java_demangle_sym(elf_name, JAVA_DEMANGLE_NORET); 2772 } 2773