1 #define _GNU_SOURCE 2 #include <ctype.h> 3 #include <dirent.h> 4 #include <errno.h> 5 #include <libgen.h> 6 #include <stdlib.h> 7 #include <stdio.h> 8 #include <string.h> 9 #include <sys/types.h> 10 #include <sys/stat.h> 11 #include <sys/param.h> 12 #include <fcntl.h> 13 #include <unistd.h> 14 #include <inttypes.h> 15 #include "build-id.h" 16 #include "debug.h" 17 #include "symbol.h" 18 #include "strlist.h" 19 20 #include <libelf.h> 21 #include <gelf.h> 22 #include <elf.h> 23 #include <limits.h> 24 #include <sys/utsname.h> 25 26 #ifndef KSYM_NAME_LEN 27 #define KSYM_NAME_LEN 128 28 #endif 29 30 #ifndef NT_GNU_BUILD_ID 31 #define NT_GNU_BUILD_ID 3 32 #endif 33 34 static bool dso__build_id_equal(const struct dso *self, u8 *build_id); 35 static int elf_read_build_id(Elf *elf, void *bf, size_t size); 36 static void dsos__add(struct list_head *head, struct dso *dso); 37 static struct map *map__new2(u64 start, struct dso *dso, enum map_type type); 38 static int dso__load_kernel_sym(struct dso *self, struct map *map, 39 symbol_filter_t filter); 40 static int dso__load_guest_kernel_sym(struct dso *self, struct map *map, 41 symbol_filter_t filter); 42 static int vmlinux_path__nr_entries; 43 static char **vmlinux_path; 44 45 struct symbol_conf symbol_conf = { 46 .exclude_other = true, 47 .use_modules = true, 48 .try_vmlinux_path = true, 49 .symfs = "", 50 }; 51 52 int dso__name_len(const struct dso *self) 53 { 54 if (verbose) 55 return self->long_name_len; 56 57 return self->short_name_len; 58 } 59 60 bool dso__loaded(const struct dso *self, enum map_type type) 61 { 62 return self->loaded & (1 << type); 63 } 64 65 bool dso__sorted_by_name(const struct dso *self, enum map_type type) 66 { 67 return self->sorted_by_name & (1 << type); 68 } 69 70 static void dso__set_sorted_by_name(struct dso *self, enum map_type type) 71 { 72 self->sorted_by_name |= (1 << type); 73 } 74 75 bool symbol_type__is_a(char symbol_type, enum map_type map_type) 76 { 77 switch (map_type) { 78 case MAP__FUNCTION: 79 return symbol_type == 'T' || symbol_type == 'W'; 80 case MAP__VARIABLE: 81 return symbol_type == 'D' || symbol_type == 'd'; 82 default: 83 return false; 84 } 85 } 86 87 static void symbols__fixup_end(struct rb_root *self) 88 { 89 struct rb_node *nd, *prevnd = rb_first(self); 90 struct symbol *curr, *prev; 91 92 if (prevnd == NULL) 93 return; 94 95 curr = rb_entry(prevnd, struct symbol, rb_node); 96 97 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) { 98 prev = curr; 99 curr = rb_entry(nd, struct symbol, rb_node); 100 101 if (prev->end == prev->start && prev->end != curr->start) 102 prev->end = curr->start - 1; 103 } 104 105 /* Last entry */ 106 if (curr->end == curr->start) 107 curr->end = roundup(curr->start, 4096); 108 } 109 110 static void __map_groups__fixup_end(struct map_groups *self, enum map_type type) 111 { 112 struct map *prev, *curr; 113 struct rb_node *nd, *prevnd = rb_first(&self->maps[type]); 114 115 if (prevnd == NULL) 116 return; 117 118 curr = rb_entry(prevnd, struct map, rb_node); 119 120 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) { 121 prev = curr; 122 curr = rb_entry(nd, struct map, rb_node); 123 prev->end = curr->start - 1; 124 } 125 126 /* 127 * We still haven't the actual symbols, so guess the 128 * last map final address. 129 */ 130 curr->end = ~0ULL; 131 } 132 133 static void map_groups__fixup_end(struct map_groups *self) 134 { 135 int i; 136 for (i = 0; i < MAP__NR_TYPES; ++i) 137 __map_groups__fixup_end(self, i); 138 } 139 140 static struct symbol *symbol__new(u64 start, u64 len, u8 binding, 141 const char *name) 142 { 143 size_t namelen = strlen(name) + 1; 144 struct symbol *self = calloc(1, (symbol_conf.priv_size + 145 sizeof(*self) + namelen)); 146 if (self == NULL) 147 return NULL; 148 149 if (symbol_conf.priv_size) 150 self = ((void *)self) + symbol_conf.priv_size; 151 152 self->start = start; 153 self->end = len ? start + len - 1 : start; 154 self->binding = binding; 155 self->namelen = namelen - 1; 156 157 pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n", __func__, name, start, self->end); 158 159 memcpy(self->name, name, namelen); 160 161 return self; 162 } 163 164 void symbol__delete(struct symbol *self) 165 { 166 free(((void *)self) - symbol_conf.priv_size); 167 } 168 169 static size_t symbol__fprintf(struct symbol *self, FILE *fp) 170 { 171 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %c %s\n", 172 self->start, self->end, 173 self->binding == STB_GLOBAL ? 'g' : 174 self->binding == STB_LOCAL ? 'l' : 'w', 175 self->name); 176 } 177 178 void dso__set_long_name(struct dso *self, char *name) 179 { 180 if (name == NULL) 181 return; 182 self->long_name = name; 183 self->long_name_len = strlen(name); 184 } 185 186 static void dso__set_short_name(struct dso *self, const char *name) 187 { 188 if (name == NULL) 189 return; 190 self->short_name = name; 191 self->short_name_len = strlen(name); 192 } 193 194 static void dso__set_basename(struct dso *self) 195 { 196 dso__set_short_name(self, basename(self->long_name)); 197 } 198 199 struct dso *dso__new(const char *name) 200 { 201 struct dso *self = calloc(1, sizeof(*self) + strlen(name) + 1); 202 203 if (self != NULL) { 204 int i; 205 strcpy(self->name, name); 206 dso__set_long_name(self, self->name); 207 dso__set_short_name(self, self->name); 208 for (i = 0; i < MAP__NR_TYPES; ++i) 209 self->symbols[i] = self->symbol_names[i] = RB_ROOT; 210 self->symtab_type = SYMTAB__NOT_FOUND; 211 self->loaded = 0; 212 self->sorted_by_name = 0; 213 self->has_build_id = 0; 214 self->kernel = DSO_TYPE_USER; 215 INIT_LIST_HEAD(&self->node); 216 } 217 218 return self; 219 } 220 221 static void symbols__delete(struct rb_root *self) 222 { 223 struct symbol *pos; 224 struct rb_node *next = rb_first(self); 225 226 while (next) { 227 pos = rb_entry(next, struct symbol, rb_node); 228 next = rb_next(&pos->rb_node); 229 rb_erase(&pos->rb_node, self); 230 symbol__delete(pos); 231 } 232 } 233 234 void dso__delete(struct dso *self) 235 { 236 int i; 237 for (i = 0; i < MAP__NR_TYPES; ++i) 238 symbols__delete(&self->symbols[i]); 239 if (self->sname_alloc) 240 free((char *)self->short_name); 241 if (self->lname_alloc) 242 free(self->long_name); 243 free(self); 244 } 245 246 void dso__set_build_id(struct dso *self, void *build_id) 247 { 248 memcpy(self->build_id, build_id, sizeof(self->build_id)); 249 self->has_build_id = 1; 250 } 251 252 static void symbols__insert(struct rb_root *self, struct symbol *sym) 253 { 254 struct rb_node **p = &self->rb_node; 255 struct rb_node *parent = NULL; 256 const u64 ip = sym->start; 257 struct symbol *s; 258 259 while (*p != NULL) { 260 parent = *p; 261 s = rb_entry(parent, struct symbol, rb_node); 262 if (ip < s->start) 263 p = &(*p)->rb_left; 264 else 265 p = &(*p)->rb_right; 266 } 267 rb_link_node(&sym->rb_node, parent, p); 268 rb_insert_color(&sym->rb_node, self); 269 } 270 271 static struct symbol *symbols__find(struct rb_root *self, u64 ip) 272 { 273 struct rb_node *n; 274 275 if (self == NULL) 276 return NULL; 277 278 n = self->rb_node; 279 280 while (n) { 281 struct symbol *s = rb_entry(n, struct symbol, rb_node); 282 283 if (ip < s->start) 284 n = n->rb_left; 285 else if (ip > s->end) 286 n = n->rb_right; 287 else 288 return s; 289 } 290 291 return NULL; 292 } 293 294 struct symbol_name_rb_node { 295 struct rb_node rb_node; 296 struct symbol sym; 297 }; 298 299 static void symbols__insert_by_name(struct rb_root *self, struct symbol *sym) 300 { 301 struct rb_node **p = &self->rb_node; 302 struct rb_node *parent = NULL; 303 struct symbol_name_rb_node *symn, *s; 304 305 symn = container_of(sym, struct symbol_name_rb_node, sym); 306 307 while (*p != NULL) { 308 parent = *p; 309 s = rb_entry(parent, struct symbol_name_rb_node, rb_node); 310 if (strcmp(sym->name, s->sym.name) < 0) 311 p = &(*p)->rb_left; 312 else 313 p = &(*p)->rb_right; 314 } 315 rb_link_node(&symn->rb_node, parent, p); 316 rb_insert_color(&symn->rb_node, self); 317 } 318 319 static void symbols__sort_by_name(struct rb_root *self, struct rb_root *source) 320 { 321 struct rb_node *nd; 322 323 for (nd = rb_first(source); nd; nd = rb_next(nd)) { 324 struct symbol *pos = rb_entry(nd, struct symbol, rb_node); 325 symbols__insert_by_name(self, pos); 326 } 327 } 328 329 static struct symbol *symbols__find_by_name(struct rb_root *self, const char *name) 330 { 331 struct rb_node *n; 332 333 if (self == NULL) 334 return NULL; 335 336 n = self->rb_node; 337 338 while (n) { 339 struct symbol_name_rb_node *s; 340 int cmp; 341 342 s = rb_entry(n, struct symbol_name_rb_node, rb_node); 343 cmp = strcmp(name, s->sym.name); 344 345 if (cmp < 0) 346 n = n->rb_left; 347 else if (cmp > 0) 348 n = n->rb_right; 349 else 350 return &s->sym; 351 } 352 353 return NULL; 354 } 355 356 struct symbol *dso__find_symbol(struct dso *self, 357 enum map_type type, u64 addr) 358 { 359 return symbols__find(&self->symbols[type], addr); 360 } 361 362 struct symbol *dso__find_symbol_by_name(struct dso *self, enum map_type type, 363 const char *name) 364 { 365 return symbols__find_by_name(&self->symbol_names[type], name); 366 } 367 368 void dso__sort_by_name(struct dso *self, enum map_type type) 369 { 370 dso__set_sorted_by_name(self, type); 371 return symbols__sort_by_name(&self->symbol_names[type], 372 &self->symbols[type]); 373 } 374 375 int build_id__sprintf(const u8 *self, int len, char *bf) 376 { 377 char *bid = bf; 378 const u8 *raw = self; 379 int i; 380 381 for (i = 0; i < len; ++i) { 382 sprintf(bid, "%02x", *raw); 383 ++raw; 384 bid += 2; 385 } 386 387 return raw - self; 388 } 389 390 size_t dso__fprintf_buildid(struct dso *self, FILE *fp) 391 { 392 char sbuild_id[BUILD_ID_SIZE * 2 + 1]; 393 394 build_id__sprintf(self->build_id, sizeof(self->build_id), sbuild_id); 395 return fprintf(fp, "%s", sbuild_id); 396 } 397 398 size_t dso__fprintf_symbols_by_name(struct dso *self, enum map_type type, FILE *fp) 399 { 400 size_t ret = 0; 401 struct rb_node *nd; 402 struct symbol_name_rb_node *pos; 403 404 for (nd = rb_first(&self->symbol_names[type]); nd; nd = rb_next(nd)) { 405 pos = rb_entry(nd, struct symbol_name_rb_node, rb_node); 406 fprintf(fp, "%s\n", pos->sym.name); 407 } 408 409 return ret; 410 } 411 412 size_t dso__fprintf(struct dso *self, enum map_type type, FILE *fp) 413 { 414 struct rb_node *nd; 415 size_t ret = fprintf(fp, "dso: %s (", self->short_name); 416 417 if (self->short_name != self->long_name) 418 ret += fprintf(fp, "%s, ", self->long_name); 419 ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type], 420 self->loaded ? "" : "NOT "); 421 ret += dso__fprintf_buildid(self, fp); 422 ret += fprintf(fp, ")\n"); 423 for (nd = rb_first(&self->symbols[type]); nd; nd = rb_next(nd)) { 424 struct symbol *pos = rb_entry(nd, struct symbol, rb_node); 425 ret += symbol__fprintf(pos, fp); 426 } 427 428 return ret; 429 } 430 431 int kallsyms__parse(const char *filename, void *arg, 432 int (*process_symbol)(void *arg, const char *name, 433 char type, u64 start, u64 end)) 434 { 435 char *line = NULL; 436 size_t n; 437 int err = -1; 438 u64 prev_start = 0; 439 char prev_symbol_type = 0; 440 char *prev_symbol_name; 441 FILE *file = fopen(filename, "r"); 442 443 if (file == NULL) 444 goto out_failure; 445 446 prev_symbol_name = malloc(KSYM_NAME_LEN); 447 if (prev_symbol_name == NULL) 448 goto out_close; 449 450 err = 0; 451 452 while (!feof(file)) { 453 u64 start; 454 int line_len, len; 455 char symbol_type; 456 char *symbol_name; 457 458 line_len = getline(&line, &n, file); 459 if (line_len < 0 || !line) 460 break; 461 462 line[--line_len] = '\0'; /* \n */ 463 464 len = hex2u64(line, &start); 465 466 len++; 467 if (len + 2 >= line_len) 468 continue; 469 470 symbol_type = toupper(line[len]); 471 len += 2; 472 symbol_name = line + len; 473 len = line_len - len; 474 475 if (len >= KSYM_NAME_LEN) { 476 err = -1; 477 break; 478 } 479 480 if (prev_symbol_type) { 481 u64 end = start; 482 if (end != prev_start) 483 --end; 484 err = process_symbol(arg, prev_symbol_name, 485 prev_symbol_type, prev_start, end); 486 if (err) 487 break; 488 } 489 490 memcpy(prev_symbol_name, symbol_name, len + 1); 491 prev_symbol_type = symbol_type; 492 prev_start = start; 493 } 494 495 free(prev_symbol_name); 496 free(line); 497 out_close: 498 fclose(file); 499 return err; 500 501 out_failure: 502 return -1; 503 } 504 505 struct process_kallsyms_args { 506 struct map *map; 507 struct dso *dso; 508 }; 509 510 static u8 kallsyms2elf_type(char type) 511 { 512 if (type == 'W') 513 return STB_WEAK; 514 515 return isupper(type) ? STB_GLOBAL : STB_LOCAL; 516 } 517 518 static int map__process_kallsym_symbol(void *arg, const char *name, 519 char type, u64 start, u64 end) 520 { 521 struct symbol *sym; 522 struct process_kallsyms_args *a = arg; 523 struct rb_root *root = &a->dso->symbols[a->map->type]; 524 525 if (!symbol_type__is_a(type, a->map->type)) 526 return 0; 527 528 sym = symbol__new(start, end - start + 1, 529 kallsyms2elf_type(type), name); 530 if (sym == NULL) 531 return -ENOMEM; 532 /* 533 * We will pass the symbols to the filter later, in 534 * map__split_kallsyms, when we have split the maps per module 535 */ 536 symbols__insert(root, sym); 537 538 return 0; 539 } 540 541 /* 542 * Loads the function entries in /proc/kallsyms into kernel_map->dso, 543 * so that we can in the next step set the symbol ->end address and then 544 * call kernel_maps__split_kallsyms. 545 */ 546 static int dso__load_all_kallsyms(struct dso *self, const char *filename, 547 struct map *map) 548 { 549 struct process_kallsyms_args args = { .map = map, .dso = self, }; 550 return kallsyms__parse(filename, &args, map__process_kallsym_symbol); 551 } 552 553 /* 554 * Split the symbols into maps, making sure there are no overlaps, i.e. the 555 * kernel range is broken in several maps, named [kernel].N, as we don't have 556 * the original ELF section names vmlinux have. 557 */ 558 static int dso__split_kallsyms(struct dso *self, struct map *map, 559 symbol_filter_t filter) 560 { 561 struct map_groups *kmaps = map__kmap(map)->kmaps; 562 struct machine *machine = kmaps->machine; 563 struct map *curr_map = map; 564 struct symbol *pos; 565 int count = 0, moved = 0; 566 struct rb_root *root = &self->symbols[map->type]; 567 struct rb_node *next = rb_first(root); 568 int kernel_range = 0; 569 570 while (next) { 571 char *module; 572 573 pos = rb_entry(next, struct symbol, rb_node); 574 next = rb_next(&pos->rb_node); 575 576 module = strchr(pos->name, '\t'); 577 if (module) { 578 if (!symbol_conf.use_modules) 579 goto discard_symbol; 580 581 *module++ = '\0'; 582 583 if (strcmp(curr_map->dso->short_name, module)) { 584 if (curr_map != map && 585 self->kernel == DSO_TYPE_GUEST_KERNEL && 586 machine__is_default_guest(machine)) { 587 /* 588 * We assume all symbols of a module are 589 * continuous in * kallsyms, so curr_map 590 * points to a module and all its 591 * symbols are in its kmap. Mark it as 592 * loaded. 593 */ 594 dso__set_loaded(curr_map->dso, 595 curr_map->type); 596 } 597 598 curr_map = map_groups__find_by_name(kmaps, 599 map->type, module); 600 if (curr_map == NULL) { 601 pr_debug("%s/proc/{kallsyms,modules} " 602 "inconsistency while looking " 603 "for \"%s\" module!\n", 604 machine->root_dir, module); 605 curr_map = map; 606 goto discard_symbol; 607 } 608 609 if (curr_map->dso->loaded && 610 !machine__is_default_guest(machine)) 611 goto discard_symbol; 612 } 613 /* 614 * So that we look just like we get from .ko files, 615 * i.e. not prelinked, relative to map->start. 616 */ 617 pos->start = curr_map->map_ip(curr_map, pos->start); 618 pos->end = curr_map->map_ip(curr_map, pos->end); 619 } else if (curr_map != map) { 620 char dso_name[PATH_MAX]; 621 struct dso *dso; 622 623 if (count == 0) { 624 curr_map = map; 625 goto filter_symbol; 626 } 627 628 if (self->kernel == DSO_TYPE_GUEST_KERNEL) 629 snprintf(dso_name, sizeof(dso_name), 630 "[guest.kernel].%d", 631 kernel_range++); 632 else 633 snprintf(dso_name, sizeof(dso_name), 634 "[kernel].%d", 635 kernel_range++); 636 637 dso = dso__new(dso_name); 638 if (dso == NULL) 639 return -1; 640 641 dso->kernel = self->kernel; 642 643 curr_map = map__new2(pos->start, dso, map->type); 644 if (curr_map == NULL) { 645 dso__delete(dso); 646 return -1; 647 } 648 649 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip; 650 map_groups__insert(kmaps, curr_map); 651 ++kernel_range; 652 } 653 filter_symbol: 654 if (filter && filter(curr_map, pos)) { 655 discard_symbol: rb_erase(&pos->rb_node, root); 656 symbol__delete(pos); 657 } else { 658 if (curr_map != map) { 659 rb_erase(&pos->rb_node, root); 660 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos); 661 ++moved; 662 } else 663 ++count; 664 } 665 } 666 667 if (curr_map != map && 668 self->kernel == DSO_TYPE_GUEST_KERNEL && 669 machine__is_default_guest(kmaps->machine)) { 670 dso__set_loaded(curr_map->dso, curr_map->type); 671 } 672 673 return count + moved; 674 } 675 676 int dso__load_kallsyms(struct dso *self, const char *filename, 677 struct map *map, symbol_filter_t filter) 678 { 679 if (dso__load_all_kallsyms(self, filename, map) < 0) 680 return -1; 681 682 if (self->kernel == DSO_TYPE_GUEST_KERNEL) 683 self->symtab_type = SYMTAB__GUEST_KALLSYMS; 684 else 685 self->symtab_type = SYMTAB__KALLSYMS; 686 687 return dso__split_kallsyms(self, map, filter); 688 } 689 690 static int dso__load_perf_map(struct dso *self, struct map *map, 691 symbol_filter_t filter) 692 { 693 char *line = NULL; 694 size_t n; 695 FILE *file; 696 int nr_syms = 0; 697 698 file = fopen(self->long_name, "r"); 699 if (file == NULL) 700 goto out_failure; 701 702 while (!feof(file)) { 703 u64 start, size; 704 struct symbol *sym; 705 int line_len, len; 706 707 line_len = getline(&line, &n, file); 708 if (line_len < 0) 709 break; 710 711 if (!line) 712 goto out_failure; 713 714 line[--line_len] = '\0'; /* \n */ 715 716 len = hex2u64(line, &start); 717 718 len++; 719 if (len + 2 >= line_len) 720 continue; 721 722 len += hex2u64(line + len, &size); 723 724 len++; 725 if (len + 2 >= line_len) 726 continue; 727 728 sym = symbol__new(start, size, STB_GLOBAL, line + len); 729 730 if (sym == NULL) 731 goto out_delete_line; 732 733 if (filter && filter(map, sym)) 734 symbol__delete(sym); 735 else { 736 symbols__insert(&self->symbols[map->type], sym); 737 nr_syms++; 738 } 739 } 740 741 free(line); 742 fclose(file); 743 744 return nr_syms; 745 746 out_delete_line: 747 free(line); 748 out_failure: 749 return -1; 750 } 751 752 /** 753 * elf_symtab__for_each_symbol - iterate thru all the symbols 754 * 755 * @self: struct elf_symtab instance to iterate 756 * @idx: uint32_t idx 757 * @sym: GElf_Sym iterator 758 */ 759 #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \ 760 for (idx = 0, gelf_getsym(syms, idx, &sym);\ 761 idx < nr_syms; \ 762 idx++, gelf_getsym(syms, idx, &sym)) 763 764 static inline uint8_t elf_sym__type(const GElf_Sym *sym) 765 { 766 return GELF_ST_TYPE(sym->st_info); 767 } 768 769 static inline int elf_sym__is_function(const GElf_Sym *sym) 770 { 771 return elf_sym__type(sym) == STT_FUNC && 772 sym->st_name != 0 && 773 sym->st_shndx != SHN_UNDEF; 774 } 775 776 static inline bool elf_sym__is_object(const GElf_Sym *sym) 777 { 778 return elf_sym__type(sym) == STT_OBJECT && 779 sym->st_name != 0 && 780 sym->st_shndx != SHN_UNDEF; 781 } 782 783 static inline int elf_sym__is_label(const GElf_Sym *sym) 784 { 785 return elf_sym__type(sym) == STT_NOTYPE && 786 sym->st_name != 0 && 787 sym->st_shndx != SHN_UNDEF && 788 sym->st_shndx != SHN_ABS; 789 } 790 791 static inline const char *elf_sec__name(const GElf_Shdr *shdr, 792 const Elf_Data *secstrs) 793 { 794 return secstrs->d_buf + shdr->sh_name; 795 } 796 797 static inline int elf_sec__is_text(const GElf_Shdr *shdr, 798 const Elf_Data *secstrs) 799 { 800 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL; 801 } 802 803 static inline bool elf_sec__is_data(const GElf_Shdr *shdr, 804 const Elf_Data *secstrs) 805 { 806 return strstr(elf_sec__name(shdr, secstrs), "data") != NULL; 807 } 808 809 static inline const char *elf_sym__name(const GElf_Sym *sym, 810 const Elf_Data *symstrs) 811 { 812 return symstrs->d_buf + sym->st_name; 813 } 814 815 static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep, 816 GElf_Shdr *shp, const char *name, 817 size_t *idx) 818 { 819 Elf_Scn *sec = NULL; 820 size_t cnt = 1; 821 822 while ((sec = elf_nextscn(elf, sec)) != NULL) { 823 char *str; 824 825 gelf_getshdr(sec, shp); 826 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name); 827 if (!strcmp(name, str)) { 828 if (idx) 829 *idx = cnt; 830 break; 831 } 832 ++cnt; 833 } 834 835 return sec; 836 } 837 838 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \ 839 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \ 840 idx < nr_entries; \ 841 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem)) 842 843 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \ 844 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \ 845 idx < nr_entries; \ 846 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem)) 847 848 /* 849 * We need to check if we have a .dynsym, so that we can handle the 850 * .plt, synthesizing its symbols, that aren't on the symtabs (be it 851 * .dynsym or .symtab). 852 * And always look at the original dso, not at debuginfo packages, that 853 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS). 854 */ 855 static int dso__synthesize_plt_symbols(struct dso *self, struct map *map, 856 symbol_filter_t filter) 857 { 858 uint32_t nr_rel_entries, idx; 859 GElf_Sym sym; 860 u64 plt_offset; 861 GElf_Shdr shdr_plt; 862 struct symbol *f; 863 GElf_Shdr shdr_rel_plt, shdr_dynsym; 864 Elf_Data *reldata, *syms, *symstrs; 865 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym; 866 size_t dynsym_idx; 867 GElf_Ehdr ehdr; 868 char sympltname[1024]; 869 Elf *elf; 870 int nr = 0, symidx, fd, err = 0; 871 char name[PATH_MAX]; 872 873 snprintf(name, sizeof(name), "%s%s", 874 symbol_conf.symfs, self->long_name); 875 fd = open(name, O_RDONLY); 876 if (fd < 0) 877 goto out; 878 879 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); 880 if (elf == NULL) 881 goto out_close; 882 883 if (gelf_getehdr(elf, &ehdr) == NULL) 884 goto out_elf_end; 885 886 scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym, 887 ".dynsym", &dynsym_idx); 888 if (scn_dynsym == NULL) 889 goto out_elf_end; 890 891 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt, 892 ".rela.plt", NULL); 893 if (scn_plt_rel == NULL) { 894 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt, 895 ".rel.plt", NULL); 896 if (scn_plt_rel == NULL) 897 goto out_elf_end; 898 } 899 900 err = -1; 901 902 if (shdr_rel_plt.sh_link != dynsym_idx) 903 goto out_elf_end; 904 905 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL) 906 goto out_elf_end; 907 908 /* 909 * Fetch the relocation section to find the idxes to the GOT 910 * and the symbols in the .dynsym they refer to. 911 */ 912 reldata = elf_getdata(scn_plt_rel, NULL); 913 if (reldata == NULL) 914 goto out_elf_end; 915 916 syms = elf_getdata(scn_dynsym, NULL); 917 if (syms == NULL) 918 goto out_elf_end; 919 920 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link); 921 if (scn_symstrs == NULL) 922 goto out_elf_end; 923 924 symstrs = elf_getdata(scn_symstrs, NULL); 925 if (symstrs == NULL) 926 goto out_elf_end; 927 928 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize; 929 plt_offset = shdr_plt.sh_offset; 930 931 if (shdr_rel_plt.sh_type == SHT_RELA) { 932 GElf_Rela pos_mem, *pos; 933 934 elf_section__for_each_rela(reldata, pos, pos_mem, idx, 935 nr_rel_entries) { 936 symidx = GELF_R_SYM(pos->r_info); 937 plt_offset += shdr_plt.sh_entsize; 938 gelf_getsym(syms, symidx, &sym); 939 snprintf(sympltname, sizeof(sympltname), 940 "%s@plt", elf_sym__name(&sym, symstrs)); 941 942 f = symbol__new(plt_offset, shdr_plt.sh_entsize, 943 STB_GLOBAL, sympltname); 944 if (!f) 945 goto out_elf_end; 946 947 if (filter && filter(map, f)) 948 symbol__delete(f); 949 else { 950 symbols__insert(&self->symbols[map->type], f); 951 ++nr; 952 } 953 } 954 } else if (shdr_rel_plt.sh_type == SHT_REL) { 955 GElf_Rel pos_mem, *pos; 956 elf_section__for_each_rel(reldata, pos, pos_mem, idx, 957 nr_rel_entries) { 958 symidx = GELF_R_SYM(pos->r_info); 959 plt_offset += shdr_plt.sh_entsize; 960 gelf_getsym(syms, symidx, &sym); 961 snprintf(sympltname, sizeof(sympltname), 962 "%s@plt", elf_sym__name(&sym, symstrs)); 963 964 f = symbol__new(plt_offset, shdr_plt.sh_entsize, 965 STB_GLOBAL, sympltname); 966 if (!f) 967 goto out_elf_end; 968 969 if (filter && filter(map, f)) 970 symbol__delete(f); 971 else { 972 symbols__insert(&self->symbols[map->type], f); 973 ++nr; 974 } 975 } 976 } 977 978 err = 0; 979 out_elf_end: 980 elf_end(elf); 981 out_close: 982 close(fd); 983 984 if (err == 0) 985 return nr; 986 out: 987 pr_debug("%s: problems reading %s PLT info.\n", 988 __func__, self->long_name); 989 return 0; 990 } 991 992 static bool elf_sym__is_a(GElf_Sym *self, enum map_type type) 993 { 994 switch (type) { 995 case MAP__FUNCTION: 996 return elf_sym__is_function(self); 997 case MAP__VARIABLE: 998 return elf_sym__is_object(self); 999 default: 1000 return false; 1001 } 1002 } 1003 1004 static bool elf_sec__is_a(GElf_Shdr *self, Elf_Data *secstrs, enum map_type type) 1005 { 1006 switch (type) { 1007 case MAP__FUNCTION: 1008 return elf_sec__is_text(self, secstrs); 1009 case MAP__VARIABLE: 1010 return elf_sec__is_data(self, secstrs); 1011 default: 1012 return false; 1013 } 1014 } 1015 1016 static size_t elf_addr_to_index(Elf *elf, GElf_Addr addr) 1017 { 1018 Elf_Scn *sec = NULL; 1019 GElf_Shdr shdr; 1020 size_t cnt = 1; 1021 1022 while ((sec = elf_nextscn(elf, sec)) != NULL) { 1023 gelf_getshdr(sec, &shdr); 1024 1025 if ((addr >= shdr.sh_addr) && 1026 (addr < (shdr.sh_addr + shdr.sh_size))) 1027 return cnt; 1028 1029 ++cnt; 1030 } 1031 1032 return -1; 1033 } 1034 1035 static int dso__load_sym(struct dso *self, struct map *map, const char *name, 1036 int fd, symbol_filter_t filter, int kmodule, 1037 int want_symtab) 1038 { 1039 struct kmap *kmap = self->kernel ? map__kmap(map) : NULL; 1040 struct map *curr_map = map; 1041 struct dso *curr_dso = self; 1042 Elf_Data *symstrs, *secstrs; 1043 uint32_t nr_syms; 1044 int err = -1; 1045 uint32_t idx; 1046 GElf_Ehdr ehdr; 1047 GElf_Shdr shdr, opdshdr; 1048 Elf_Data *syms, *opddata = NULL; 1049 GElf_Sym sym; 1050 Elf_Scn *sec, *sec_strndx, *opdsec; 1051 Elf *elf; 1052 int nr = 0; 1053 size_t opdidx = 0; 1054 1055 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); 1056 if (elf == NULL) { 1057 pr_debug("%s: cannot read %s ELF file.\n", __func__, name); 1058 goto out_close; 1059 } 1060 1061 if (gelf_getehdr(elf, &ehdr) == NULL) { 1062 pr_debug("%s: cannot get elf header.\n", __func__); 1063 goto out_elf_end; 1064 } 1065 1066 /* Always reject images with a mismatched build-id: */ 1067 if (self->has_build_id) { 1068 u8 build_id[BUILD_ID_SIZE]; 1069 1070 if (elf_read_build_id(elf, build_id, 1071 BUILD_ID_SIZE) != BUILD_ID_SIZE) 1072 goto out_elf_end; 1073 1074 if (!dso__build_id_equal(self, build_id)) 1075 goto out_elf_end; 1076 } 1077 1078 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL); 1079 if (sec == NULL) { 1080 if (want_symtab) 1081 goto out_elf_end; 1082 1083 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL); 1084 if (sec == NULL) 1085 goto out_elf_end; 1086 } 1087 1088 opdsec = elf_section_by_name(elf, &ehdr, &opdshdr, ".opd", &opdidx); 1089 if (opdsec) 1090 opddata = elf_rawdata(opdsec, NULL); 1091 1092 syms = elf_getdata(sec, NULL); 1093 if (syms == NULL) 1094 goto out_elf_end; 1095 1096 sec = elf_getscn(elf, shdr.sh_link); 1097 if (sec == NULL) 1098 goto out_elf_end; 1099 1100 symstrs = elf_getdata(sec, NULL); 1101 if (symstrs == NULL) 1102 goto out_elf_end; 1103 1104 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx); 1105 if (sec_strndx == NULL) 1106 goto out_elf_end; 1107 1108 secstrs = elf_getdata(sec_strndx, NULL); 1109 if (secstrs == NULL) 1110 goto out_elf_end; 1111 1112 nr_syms = shdr.sh_size / shdr.sh_entsize; 1113 1114 memset(&sym, 0, sizeof(sym)); 1115 if (self->kernel == DSO_TYPE_USER) { 1116 self->adjust_symbols = (ehdr.e_type == ET_EXEC || 1117 elf_section_by_name(elf, &ehdr, &shdr, 1118 ".gnu.prelink_undo", 1119 NULL) != NULL); 1120 } else self->adjust_symbols = 0; 1121 1122 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) { 1123 struct symbol *f; 1124 const char *elf_name = elf_sym__name(&sym, symstrs); 1125 char *demangled = NULL; 1126 int is_label = elf_sym__is_label(&sym); 1127 const char *section_name; 1128 1129 if (kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name && 1130 strcmp(elf_name, kmap->ref_reloc_sym->name) == 0) 1131 kmap->ref_reloc_sym->unrelocated_addr = sym.st_value; 1132 1133 if (!is_label && !elf_sym__is_a(&sym, map->type)) 1134 continue; 1135 1136 /* Reject ARM ELF "mapping symbols": these aren't unique and 1137 * don't identify functions, so will confuse the profile 1138 * output: */ 1139 if (ehdr.e_machine == EM_ARM) { 1140 if (!strcmp(elf_name, "$a") || 1141 !strcmp(elf_name, "$d") || 1142 !strcmp(elf_name, "$t")) 1143 continue; 1144 } 1145 1146 if (opdsec && sym.st_shndx == opdidx) { 1147 u32 offset = sym.st_value - opdshdr.sh_addr; 1148 u64 *opd = opddata->d_buf + offset; 1149 sym.st_value = *opd; 1150 sym.st_shndx = elf_addr_to_index(elf, sym.st_value); 1151 } 1152 1153 sec = elf_getscn(elf, sym.st_shndx); 1154 if (!sec) 1155 goto out_elf_end; 1156 1157 gelf_getshdr(sec, &shdr); 1158 1159 if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type)) 1160 continue; 1161 1162 section_name = elf_sec__name(&shdr, secstrs); 1163 1164 /* On ARM, symbols for thumb functions have 1 added to 1165 * the symbol address as a flag - remove it */ 1166 if ((ehdr.e_machine == EM_ARM) && 1167 (map->type == MAP__FUNCTION) && 1168 (sym.st_value & 1)) 1169 --sym.st_value; 1170 1171 if (self->kernel != DSO_TYPE_USER || kmodule) { 1172 char dso_name[PATH_MAX]; 1173 1174 if (strcmp(section_name, 1175 (curr_dso->short_name + 1176 self->short_name_len)) == 0) 1177 goto new_symbol; 1178 1179 if (strcmp(section_name, ".text") == 0) { 1180 curr_map = map; 1181 curr_dso = self; 1182 goto new_symbol; 1183 } 1184 1185 snprintf(dso_name, sizeof(dso_name), 1186 "%s%s", self->short_name, section_name); 1187 1188 curr_map = map_groups__find_by_name(kmap->kmaps, map->type, dso_name); 1189 if (curr_map == NULL) { 1190 u64 start = sym.st_value; 1191 1192 if (kmodule) 1193 start += map->start + shdr.sh_offset; 1194 1195 curr_dso = dso__new(dso_name); 1196 if (curr_dso == NULL) 1197 goto out_elf_end; 1198 curr_dso->kernel = self->kernel; 1199 curr_map = map__new2(start, curr_dso, 1200 map->type); 1201 if (curr_map == NULL) { 1202 dso__delete(curr_dso); 1203 goto out_elf_end; 1204 } 1205 curr_map->map_ip = identity__map_ip; 1206 curr_map->unmap_ip = identity__map_ip; 1207 curr_dso->symtab_type = self->symtab_type; 1208 map_groups__insert(kmap->kmaps, curr_map); 1209 dsos__add(&self->node, curr_dso); 1210 dso__set_loaded(curr_dso, map->type); 1211 } else 1212 curr_dso = curr_map->dso; 1213 1214 goto new_symbol; 1215 } 1216 1217 if (curr_dso->adjust_symbols) { 1218 pr_debug4("%s: adjusting symbol: st_value: %#" PRIx64 " " 1219 "sh_addr: %#" PRIx64 " sh_offset: %#" PRIx64 "\n", __func__, 1220 (u64)sym.st_value, (u64)shdr.sh_addr, 1221 (u64)shdr.sh_offset); 1222 sym.st_value -= shdr.sh_addr - shdr.sh_offset; 1223 } 1224 /* 1225 * We need to figure out if the object was created from C++ sources 1226 * DWARF DW_compile_unit has this, but we don't always have access 1227 * to it... 1228 */ 1229 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI); 1230 if (demangled != NULL) 1231 elf_name = demangled; 1232 new_symbol: 1233 f = symbol__new(sym.st_value, sym.st_size, 1234 GELF_ST_BIND(sym.st_info), elf_name); 1235 free(demangled); 1236 if (!f) 1237 goto out_elf_end; 1238 1239 if (filter && filter(curr_map, f)) 1240 symbol__delete(f); 1241 else { 1242 symbols__insert(&curr_dso->symbols[curr_map->type], f); 1243 nr++; 1244 } 1245 } 1246 1247 /* 1248 * For misannotated, zeroed, ASM function sizes. 1249 */ 1250 if (nr > 0) { 1251 symbols__fixup_end(&self->symbols[map->type]); 1252 if (kmap) { 1253 /* 1254 * We need to fixup this here too because we create new 1255 * maps here, for things like vsyscall sections. 1256 */ 1257 __map_groups__fixup_end(kmap->kmaps, map->type); 1258 } 1259 } 1260 err = nr; 1261 out_elf_end: 1262 elf_end(elf); 1263 out_close: 1264 return err; 1265 } 1266 1267 static bool dso__build_id_equal(const struct dso *self, u8 *build_id) 1268 { 1269 return memcmp(self->build_id, build_id, sizeof(self->build_id)) == 0; 1270 } 1271 1272 bool __dsos__read_build_ids(struct list_head *head, bool with_hits) 1273 { 1274 bool have_build_id = false; 1275 struct dso *pos; 1276 1277 list_for_each_entry(pos, head, node) { 1278 if (with_hits && !pos->hit) 1279 continue; 1280 if (pos->has_build_id) { 1281 have_build_id = true; 1282 continue; 1283 } 1284 if (filename__read_build_id(pos->long_name, pos->build_id, 1285 sizeof(pos->build_id)) > 0) { 1286 have_build_id = true; 1287 pos->has_build_id = true; 1288 } 1289 } 1290 1291 return have_build_id; 1292 } 1293 1294 /* 1295 * Align offset to 4 bytes as needed for note name and descriptor data. 1296 */ 1297 #define NOTE_ALIGN(n) (((n) + 3) & -4U) 1298 1299 static int elf_read_build_id(Elf *elf, void *bf, size_t size) 1300 { 1301 int err = -1; 1302 GElf_Ehdr ehdr; 1303 GElf_Shdr shdr; 1304 Elf_Data *data; 1305 Elf_Scn *sec; 1306 Elf_Kind ek; 1307 void *ptr; 1308 1309 if (size < BUILD_ID_SIZE) 1310 goto out; 1311 1312 ek = elf_kind(elf); 1313 if (ek != ELF_K_ELF) 1314 goto out; 1315 1316 if (gelf_getehdr(elf, &ehdr) == NULL) { 1317 pr_err("%s: cannot get elf header.\n", __func__); 1318 goto out; 1319 } 1320 1321 sec = elf_section_by_name(elf, &ehdr, &shdr, 1322 ".note.gnu.build-id", NULL); 1323 if (sec == NULL) { 1324 sec = elf_section_by_name(elf, &ehdr, &shdr, 1325 ".notes", NULL); 1326 if (sec == NULL) 1327 goto out; 1328 } 1329 1330 data = elf_getdata(sec, NULL); 1331 if (data == NULL) 1332 goto out; 1333 1334 ptr = data->d_buf; 1335 while (ptr < (data->d_buf + data->d_size)) { 1336 GElf_Nhdr *nhdr = ptr; 1337 int namesz = NOTE_ALIGN(nhdr->n_namesz), 1338 descsz = NOTE_ALIGN(nhdr->n_descsz); 1339 const char *name; 1340 1341 ptr += sizeof(*nhdr); 1342 name = ptr; 1343 ptr += namesz; 1344 if (nhdr->n_type == NT_GNU_BUILD_ID && 1345 nhdr->n_namesz == sizeof("GNU")) { 1346 if (memcmp(name, "GNU", sizeof("GNU")) == 0) { 1347 memcpy(bf, ptr, BUILD_ID_SIZE); 1348 err = BUILD_ID_SIZE; 1349 break; 1350 } 1351 } 1352 ptr += descsz; 1353 } 1354 1355 out: 1356 return err; 1357 } 1358 1359 int filename__read_build_id(const char *filename, void *bf, size_t size) 1360 { 1361 int fd, err = -1; 1362 Elf *elf; 1363 1364 if (size < BUILD_ID_SIZE) 1365 goto out; 1366 1367 fd = open(filename, O_RDONLY); 1368 if (fd < 0) 1369 goto out; 1370 1371 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); 1372 if (elf == NULL) { 1373 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename); 1374 goto out_close; 1375 } 1376 1377 err = elf_read_build_id(elf, bf, size); 1378 1379 elf_end(elf); 1380 out_close: 1381 close(fd); 1382 out: 1383 return err; 1384 } 1385 1386 int sysfs__read_build_id(const char *filename, void *build_id, size_t size) 1387 { 1388 int fd, err = -1; 1389 1390 if (size < BUILD_ID_SIZE) 1391 goto out; 1392 1393 fd = open(filename, O_RDONLY); 1394 if (fd < 0) 1395 goto out; 1396 1397 while (1) { 1398 char bf[BUFSIZ]; 1399 GElf_Nhdr nhdr; 1400 int namesz, descsz; 1401 1402 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr)) 1403 break; 1404 1405 namesz = NOTE_ALIGN(nhdr.n_namesz); 1406 descsz = NOTE_ALIGN(nhdr.n_descsz); 1407 if (nhdr.n_type == NT_GNU_BUILD_ID && 1408 nhdr.n_namesz == sizeof("GNU")) { 1409 if (read(fd, bf, namesz) != namesz) 1410 break; 1411 if (memcmp(bf, "GNU", sizeof("GNU")) == 0) { 1412 if (read(fd, build_id, 1413 BUILD_ID_SIZE) == BUILD_ID_SIZE) { 1414 err = 0; 1415 break; 1416 } 1417 } else if (read(fd, bf, descsz) != descsz) 1418 break; 1419 } else { 1420 int n = namesz + descsz; 1421 if (read(fd, bf, n) != n) 1422 break; 1423 } 1424 } 1425 close(fd); 1426 out: 1427 return err; 1428 } 1429 1430 char dso__symtab_origin(const struct dso *self) 1431 { 1432 static const char origin[] = { 1433 [SYMTAB__KALLSYMS] = 'k', 1434 [SYMTAB__JAVA_JIT] = 'j', 1435 [SYMTAB__BUILD_ID_CACHE] = 'B', 1436 [SYMTAB__FEDORA_DEBUGINFO] = 'f', 1437 [SYMTAB__UBUNTU_DEBUGINFO] = 'u', 1438 [SYMTAB__BUILDID_DEBUGINFO] = 'b', 1439 [SYMTAB__SYSTEM_PATH_DSO] = 'd', 1440 [SYMTAB__SYSTEM_PATH_KMODULE] = 'K', 1441 [SYMTAB__GUEST_KALLSYMS] = 'g', 1442 [SYMTAB__GUEST_KMODULE] = 'G', 1443 }; 1444 1445 if (self == NULL || self->symtab_type == SYMTAB__NOT_FOUND) 1446 return '!'; 1447 return origin[self->symtab_type]; 1448 } 1449 1450 int dso__load(struct dso *self, struct map *map, symbol_filter_t filter) 1451 { 1452 int size = PATH_MAX; 1453 char *name; 1454 int ret = -1; 1455 int fd; 1456 struct machine *machine; 1457 const char *root_dir; 1458 int want_symtab; 1459 1460 dso__set_loaded(self, map->type); 1461 1462 if (self->kernel == DSO_TYPE_KERNEL) 1463 return dso__load_kernel_sym(self, map, filter); 1464 else if (self->kernel == DSO_TYPE_GUEST_KERNEL) 1465 return dso__load_guest_kernel_sym(self, map, filter); 1466 1467 if (map->groups && map->groups->machine) 1468 machine = map->groups->machine; 1469 else 1470 machine = NULL; 1471 1472 name = malloc(size); 1473 if (!name) 1474 return -1; 1475 1476 self->adjust_symbols = 0; 1477 1478 if (strncmp(self->name, "/tmp/perf-", 10) == 0) { 1479 ret = dso__load_perf_map(self, map, filter); 1480 self->symtab_type = ret > 0 ? SYMTAB__JAVA_JIT : 1481 SYMTAB__NOT_FOUND; 1482 return ret; 1483 } 1484 1485 /* Iterate over candidate debug images. 1486 * On the first pass, only load images if they have a full symtab. 1487 * Failing that, do a second pass where we accept .dynsym also 1488 */ 1489 for (self->symtab_type = SYMTAB__BUILD_ID_CACHE, want_symtab = 1; 1490 self->symtab_type != SYMTAB__NOT_FOUND; 1491 self->symtab_type++) { 1492 switch (self->symtab_type) { 1493 case SYMTAB__BUILD_ID_CACHE: 1494 /* skip the locally configured cache if a symfs is given */ 1495 if (symbol_conf.symfs[0] || 1496 (dso__build_id_filename(self, name, size) == NULL)) { 1497 continue; 1498 } 1499 break; 1500 case SYMTAB__FEDORA_DEBUGINFO: 1501 snprintf(name, size, "%s/usr/lib/debug%s.debug", 1502 symbol_conf.symfs, self->long_name); 1503 break; 1504 case SYMTAB__UBUNTU_DEBUGINFO: 1505 snprintf(name, size, "%s/usr/lib/debug%s", 1506 symbol_conf.symfs, self->long_name); 1507 break; 1508 case SYMTAB__BUILDID_DEBUGINFO: { 1509 char build_id_hex[BUILD_ID_SIZE * 2 + 1]; 1510 1511 if (!self->has_build_id) 1512 continue; 1513 1514 build_id__sprintf(self->build_id, 1515 sizeof(self->build_id), 1516 build_id_hex); 1517 snprintf(name, size, 1518 "%s/usr/lib/debug/.build-id/%.2s/%s.debug", 1519 symbol_conf.symfs, build_id_hex, build_id_hex + 2); 1520 } 1521 break; 1522 case SYMTAB__SYSTEM_PATH_DSO: 1523 snprintf(name, size, "%s%s", 1524 symbol_conf.symfs, self->long_name); 1525 break; 1526 case SYMTAB__GUEST_KMODULE: 1527 if (map->groups && machine) 1528 root_dir = machine->root_dir; 1529 else 1530 root_dir = ""; 1531 snprintf(name, size, "%s%s%s", symbol_conf.symfs, 1532 root_dir, self->long_name); 1533 break; 1534 1535 case SYMTAB__SYSTEM_PATH_KMODULE: 1536 snprintf(name, size, "%s%s", symbol_conf.symfs, 1537 self->long_name); 1538 break; 1539 1540 default: 1541 /* 1542 * If we wanted a full symtab but no image had one, 1543 * relax our requirements and repeat the search. 1544 */ 1545 if (want_symtab) { 1546 want_symtab = 0; 1547 self->symtab_type = SYMTAB__BUILD_ID_CACHE; 1548 } else 1549 continue; 1550 } 1551 1552 /* Name is now the name of the next image to try */ 1553 fd = open(name, O_RDONLY); 1554 if (fd < 0) 1555 continue; 1556 1557 ret = dso__load_sym(self, map, name, fd, filter, 0, 1558 want_symtab); 1559 close(fd); 1560 1561 /* 1562 * Some people seem to have debuginfo files _WITHOUT_ debug 1563 * info!?!? 1564 */ 1565 if (!ret) 1566 continue; 1567 1568 if (ret > 0) { 1569 int nr_plt = dso__synthesize_plt_symbols(self, map, filter); 1570 if (nr_plt > 0) 1571 ret += nr_plt; 1572 break; 1573 } 1574 } 1575 1576 free(name); 1577 if (ret < 0 && strstr(self->name, " (deleted)") != NULL) 1578 return 0; 1579 return ret; 1580 } 1581 1582 struct map *map_groups__find_by_name(struct map_groups *self, 1583 enum map_type type, const char *name) 1584 { 1585 struct rb_node *nd; 1586 1587 for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) { 1588 struct map *map = rb_entry(nd, struct map, rb_node); 1589 1590 if (map->dso && strcmp(map->dso->short_name, name) == 0) 1591 return map; 1592 } 1593 1594 return NULL; 1595 } 1596 1597 static int dso__kernel_module_get_build_id(struct dso *self, 1598 const char *root_dir) 1599 { 1600 char filename[PATH_MAX]; 1601 /* 1602 * kernel module short names are of the form "[module]" and 1603 * we need just "module" here. 1604 */ 1605 const char *name = self->short_name + 1; 1606 1607 snprintf(filename, sizeof(filename), 1608 "%s/sys/module/%.*s/notes/.note.gnu.build-id", 1609 root_dir, (int)strlen(name) - 1, name); 1610 1611 if (sysfs__read_build_id(filename, self->build_id, 1612 sizeof(self->build_id)) == 0) 1613 self->has_build_id = true; 1614 1615 return 0; 1616 } 1617 1618 static int map_groups__set_modules_path_dir(struct map_groups *self, 1619 const char *dir_name) 1620 { 1621 struct dirent *dent; 1622 DIR *dir = opendir(dir_name); 1623 int ret = 0; 1624 1625 if (!dir) { 1626 pr_debug("%s: cannot open %s dir\n", __func__, dir_name); 1627 return -1; 1628 } 1629 1630 while ((dent = readdir(dir)) != NULL) { 1631 char path[PATH_MAX]; 1632 struct stat st; 1633 1634 /*sshfs might return bad dent->d_type, so we have to stat*/ 1635 sprintf(path, "%s/%s", dir_name, dent->d_name); 1636 if (stat(path, &st)) 1637 continue; 1638 1639 if (S_ISDIR(st.st_mode)) { 1640 if (!strcmp(dent->d_name, ".") || 1641 !strcmp(dent->d_name, "..")) 1642 continue; 1643 1644 snprintf(path, sizeof(path), "%s/%s", 1645 dir_name, dent->d_name); 1646 ret = map_groups__set_modules_path_dir(self, path); 1647 if (ret < 0) 1648 goto out; 1649 } else { 1650 char *dot = strrchr(dent->d_name, '.'), 1651 dso_name[PATH_MAX]; 1652 struct map *map; 1653 char *long_name; 1654 1655 if (dot == NULL || strcmp(dot, ".ko")) 1656 continue; 1657 snprintf(dso_name, sizeof(dso_name), "[%.*s]", 1658 (int)(dot - dent->d_name), dent->d_name); 1659 1660 strxfrchar(dso_name, '-', '_'); 1661 map = map_groups__find_by_name(self, MAP__FUNCTION, dso_name); 1662 if (map == NULL) 1663 continue; 1664 1665 snprintf(path, sizeof(path), "%s/%s", 1666 dir_name, dent->d_name); 1667 1668 long_name = strdup(path); 1669 if (long_name == NULL) { 1670 ret = -1; 1671 goto out; 1672 } 1673 dso__set_long_name(map->dso, long_name); 1674 map->dso->lname_alloc = 1; 1675 dso__kernel_module_get_build_id(map->dso, ""); 1676 } 1677 } 1678 1679 out: 1680 closedir(dir); 1681 return ret; 1682 } 1683 1684 static char *get_kernel_version(const char *root_dir) 1685 { 1686 char version[PATH_MAX]; 1687 FILE *file; 1688 char *name, *tmp; 1689 const char *prefix = "Linux version "; 1690 1691 sprintf(version, "%s/proc/version", root_dir); 1692 file = fopen(version, "r"); 1693 if (!file) 1694 return NULL; 1695 1696 version[0] = '\0'; 1697 tmp = fgets(version, sizeof(version), file); 1698 fclose(file); 1699 1700 name = strstr(version, prefix); 1701 if (!name) 1702 return NULL; 1703 name += strlen(prefix); 1704 tmp = strchr(name, ' '); 1705 if (tmp) 1706 *tmp = '\0'; 1707 1708 return strdup(name); 1709 } 1710 1711 static int machine__set_modules_path(struct machine *self) 1712 { 1713 char *version; 1714 char modules_path[PATH_MAX]; 1715 1716 version = get_kernel_version(self->root_dir); 1717 if (!version) 1718 return -1; 1719 1720 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s/kernel", 1721 self->root_dir, version); 1722 free(version); 1723 1724 return map_groups__set_modules_path_dir(&self->kmaps, modules_path); 1725 } 1726 1727 /* 1728 * Constructor variant for modules (where we know from /proc/modules where 1729 * they are loaded) and for vmlinux, where only after we load all the 1730 * symbols we'll know where it starts and ends. 1731 */ 1732 static struct map *map__new2(u64 start, struct dso *dso, enum map_type type) 1733 { 1734 struct map *self = calloc(1, (sizeof(*self) + 1735 (dso->kernel ? sizeof(struct kmap) : 0))); 1736 if (self != NULL) { 1737 /* 1738 * ->end will be filled after we load all the symbols 1739 */ 1740 map__init(self, type, start, 0, 0, dso); 1741 } 1742 1743 return self; 1744 } 1745 1746 struct map *machine__new_module(struct machine *self, u64 start, 1747 const char *filename) 1748 { 1749 struct map *map; 1750 struct dso *dso = __dsos__findnew(&self->kernel_dsos, filename); 1751 1752 if (dso == NULL) 1753 return NULL; 1754 1755 map = map__new2(start, dso, MAP__FUNCTION); 1756 if (map == NULL) 1757 return NULL; 1758 1759 if (machine__is_host(self)) 1760 dso->symtab_type = SYMTAB__SYSTEM_PATH_KMODULE; 1761 else 1762 dso->symtab_type = SYMTAB__GUEST_KMODULE; 1763 map_groups__insert(&self->kmaps, map); 1764 return map; 1765 } 1766 1767 static int machine__create_modules(struct machine *self) 1768 { 1769 char *line = NULL; 1770 size_t n; 1771 FILE *file; 1772 struct map *map; 1773 const char *modules; 1774 char path[PATH_MAX]; 1775 1776 if (machine__is_default_guest(self)) 1777 modules = symbol_conf.default_guest_modules; 1778 else { 1779 sprintf(path, "%s/proc/modules", self->root_dir); 1780 modules = path; 1781 } 1782 1783 file = fopen(modules, "r"); 1784 if (file == NULL) 1785 return -1; 1786 1787 while (!feof(file)) { 1788 char name[PATH_MAX]; 1789 u64 start; 1790 char *sep; 1791 int line_len; 1792 1793 line_len = getline(&line, &n, file); 1794 if (line_len < 0) 1795 break; 1796 1797 if (!line) 1798 goto out_failure; 1799 1800 line[--line_len] = '\0'; /* \n */ 1801 1802 sep = strrchr(line, 'x'); 1803 if (sep == NULL) 1804 continue; 1805 1806 hex2u64(sep + 1, &start); 1807 1808 sep = strchr(line, ' '); 1809 if (sep == NULL) 1810 continue; 1811 1812 *sep = '\0'; 1813 1814 snprintf(name, sizeof(name), "[%s]", line); 1815 map = machine__new_module(self, start, name); 1816 if (map == NULL) 1817 goto out_delete_line; 1818 dso__kernel_module_get_build_id(map->dso, self->root_dir); 1819 } 1820 1821 free(line); 1822 fclose(file); 1823 1824 return machine__set_modules_path(self); 1825 1826 out_delete_line: 1827 free(line); 1828 out_failure: 1829 return -1; 1830 } 1831 1832 int dso__load_vmlinux(struct dso *self, struct map *map, 1833 const char *vmlinux, symbol_filter_t filter) 1834 { 1835 int err = -1, fd; 1836 char symfs_vmlinux[PATH_MAX]; 1837 1838 snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s%s", 1839 symbol_conf.symfs, vmlinux); 1840 fd = open(symfs_vmlinux, O_RDONLY); 1841 if (fd < 0) 1842 return -1; 1843 1844 dso__set_loaded(self, map->type); 1845 err = dso__load_sym(self, map, symfs_vmlinux, fd, filter, 0, 0); 1846 close(fd); 1847 1848 if (err > 0) 1849 pr_debug("Using %s for symbols\n", symfs_vmlinux); 1850 1851 return err; 1852 } 1853 1854 int dso__load_vmlinux_path(struct dso *self, struct map *map, 1855 symbol_filter_t filter) 1856 { 1857 int i, err = 0; 1858 char *filename; 1859 1860 pr_debug("Looking at the vmlinux_path (%d entries long)\n", 1861 vmlinux_path__nr_entries + 1); 1862 1863 filename = dso__build_id_filename(self, NULL, 0); 1864 if (filename != NULL) { 1865 err = dso__load_vmlinux(self, map, filename, filter); 1866 if (err > 0) { 1867 dso__set_long_name(self, filename); 1868 goto out; 1869 } 1870 free(filename); 1871 } 1872 1873 for (i = 0; i < vmlinux_path__nr_entries; ++i) { 1874 err = dso__load_vmlinux(self, map, vmlinux_path[i], filter); 1875 if (err > 0) { 1876 dso__set_long_name(self, strdup(vmlinux_path[i])); 1877 break; 1878 } 1879 } 1880 out: 1881 return err; 1882 } 1883 1884 static int dso__load_kernel_sym(struct dso *self, struct map *map, 1885 symbol_filter_t filter) 1886 { 1887 int err; 1888 const char *kallsyms_filename = NULL; 1889 char *kallsyms_allocated_filename = NULL; 1890 /* 1891 * Step 1: if the user specified a kallsyms or vmlinux filename, use 1892 * it and only it, reporting errors to the user if it cannot be used. 1893 * 1894 * For instance, try to analyse an ARM perf.data file _without_ a 1895 * build-id, or if the user specifies the wrong path to the right 1896 * vmlinux file, obviously we can't fallback to another vmlinux (a 1897 * x86_86 one, on the machine where analysis is being performed, say), 1898 * or worse, /proc/kallsyms. 1899 * 1900 * If the specified file _has_ a build-id and there is a build-id 1901 * section in the perf.data file, we will still do the expected 1902 * validation in dso__load_vmlinux and will bail out if they don't 1903 * match. 1904 */ 1905 if (symbol_conf.kallsyms_name != NULL) { 1906 kallsyms_filename = symbol_conf.kallsyms_name; 1907 goto do_kallsyms; 1908 } 1909 1910 if (symbol_conf.vmlinux_name != NULL) { 1911 err = dso__load_vmlinux(self, map, 1912 symbol_conf.vmlinux_name, filter); 1913 if (err > 0) { 1914 dso__set_long_name(self, 1915 strdup(symbol_conf.vmlinux_name)); 1916 goto out_fixup; 1917 } 1918 return err; 1919 } 1920 1921 if (vmlinux_path != NULL) { 1922 err = dso__load_vmlinux_path(self, map, filter); 1923 if (err > 0) 1924 goto out_fixup; 1925 } 1926 1927 /* do not try local files if a symfs was given */ 1928 if (symbol_conf.symfs[0] != 0) 1929 return -1; 1930 1931 /* 1932 * Say the kernel DSO was created when processing the build-id header table, 1933 * we have a build-id, so check if it is the same as the running kernel, 1934 * using it if it is. 1935 */ 1936 if (self->has_build_id) { 1937 u8 kallsyms_build_id[BUILD_ID_SIZE]; 1938 char sbuild_id[BUILD_ID_SIZE * 2 + 1]; 1939 1940 if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id, 1941 sizeof(kallsyms_build_id)) == 0) { 1942 if (dso__build_id_equal(self, kallsyms_build_id)) { 1943 kallsyms_filename = "/proc/kallsyms"; 1944 goto do_kallsyms; 1945 } 1946 } 1947 /* 1948 * Now look if we have it on the build-id cache in 1949 * $HOME/.debug/[kernel.kallsyms]. 1950 */ 1951 build_id__sprintf(self->build_id, sizeof(self->build_id), 1952 sbuild_id); 1953 1954 if (asprintf(&kallsyms_allocated_filename, 1955 "%s/.debug/[kernel.kallsyms]/%s", 1956 getenv("HOME"), sbuild_id) == -1) { 1957 pr_err("Not enough memory for kallsyms file lookup\n"); 1958 return -1; 1959 } 1960 1961 kallsyms_filename = kallsyms_allocated_filename; 1962 1963 if (access(kallsyms_filename, F_OK)) { 1964 pr_err("No kallsyms or vmlinux with build-id %s " 1965 "was found\n", sbuild_id); 1966 free(kallsyms_allocated_filename); 1967 return -1; 1968 } 1969 } else { 1970 /* 1971 * Last resort, if we don't have a build-id and couldn't find 1972 * any vmlinux file, try the running kernel kallsyms table. 1973 */ 1974 kallsyms_filename = "/proc/kallsyms"; 1975 } 1976 1977 do_kallsyms: 1978 err = dso__load_kallsyms(self, kallsyms_filename, map, filter); 1979 if (err > 0) 1980 pr_debug("Using %s for symbols\n", kallsyms_filename); 1981 free(kallsyms_allocated_filename); 1982 1983 if (err > 0) { 1984 out_fixup: 1985 if (kallsyms_filename != NULL) 1986 dso__set_long_name(self, strdup("[kernel.kallsyms]")); 1987 map__fixup_start(map); 1988 map__fixup_end(map); 1989 } 1990 1991 return err; 1992 } 1993 1994 static int dso__load_guest_kernel_sym(struct dso *self, struct map *map, 1995 symbol_filter_t filter) 1996 { 1997 int err; 1998 const char *kallsyms_filename = NULL; 1999 struct machine *machine; 2000 char path[PATH_MAX]; 2001 2002 if (!map->groups) { 2003 pr_debug("Guest kernel map hasn't the point to groups\n"); 2004 return -1; 2005 } 2006 machine = map->groups->machine; 2007 2008 if (machine__is_default_guest(machine)) { 2009 /* 2010 * if the user specified a vmlinux filename, use it and only 2011 * it, reporting errors to the user if it cannot be used. 2012 * Or use file guest_kallsyms inputted by user on commandline 2013 */ 2014 if (symbol_conf.default_guest_vmlinux_name != NULL) { 2015 err = dso__load_vmlinux(self, map, 2016 symbol_conf.default_guest_vmlinux_name, filter); 2017 goto out_try_fixup; 2018 } 2019 2020 kallsyms_filename = symbol_conf.default_guest_kallsyms; 2021 if (!kallsyms_filename) 2022 return -1; 2023 } else { 2024 sprintf(path, "%s/proc/kallsyms", machine->root_dir); 2025 kallsyms_filename = path; 2026 } 2027 2028 err = dso__load_kallsyms(self, kallsyms_filename, map, filter); 2029 if (err > 0) 2030 pr_debug("Using %s for symbols\n", kallsyms_filename); 2031 2032 out_try_fixup: 2033 if (err > 0) { 2034 if (kallsyms_filename != NULL) { 2035 machine__mmap_name(machine, path, sizeof(path)); 2036 dso__set_long_name(self, strdup(path)); 2037 } 2038 map__fixup_start(map); 2039 map__fixup_end(map); 2040 } 2041 2042 return err; 2043 } 2044 2045 static void dsos__add(struct list_head *head, struct dso *dso) 2046 { 2047 list_add_tail(&dso->node, head); 2048 } 2049 2050 static struct dso *dsos__find(struct list_head *head, const char *name) 2051 { 2052 struct dso *pos; 2053 2054 list_for_each_entry(pos, head, node) 2055 if (strcmp(pos->long_name, name) == 0) 2056 return pos; 2057 return NULL; 2058 } 2059 2060 struct dso *__dsos__findnew(struct list_head *head, const char *name) 2061 { 2062 struct dso *dso = dsos__find(head, name); 2063 2064 if (!dso) { 2065 dso = dso__new(name); 2066 if (dso != NULL) { 2067 dsos__add(head, dso); 2068 dso__set_basename(dso); 2069 } 2070 } 2071 2072 return dso; 2073 } 2074 2075 size_t __dsos__fprintf(struct list_head *head, FILE *fp) 2076 { 2077 struct dso *pos; 2078 size_t ret = 0; 2079 2080 list_for_each_entry(pos, head, node) { 2081 int i; 2082 for (i = 0; i < MAP__NR_TYPES; ++i) 2083 ret += dso__fprintf(pos, i, fp); 2084 } 2085 2086 return ret; 2087 } 2088 2089 size_t machines__fprintf_dsos(struct rb_root *self, FILE *fp) 2090 { 2091 struct rb_node *nd; 2092 size_t ret = 0; 2093 2094 for (nd = rb_first(self); nd; nd = rb_next(nd)) { 2095 struct machine *pos = rb_entry(nd, struct machine, rb_node); 2096 ret += __dsos__fprintf(&pos->kernel_dsos, fp); 2097 ret += __dsos__fprintf(&pos->user_dsos, fp); 2098 } 2099 2100 return ret; 2101 } 2102 2103 static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp, 2104 bool with_hits) 2105 { 2106 struct dso *pos; 2107 size_t ret = 0; 2108 2109 list_for_each_entry(pos, head, node) { 2110 if (with_hits && !pos->hit) 2111 continue; 2112 ret += dso__fprintf_buildid(pos, fp); 2113 ret += fprintf(fp, " %s\n", pos->long_name); 2114 } 2115 return ret; 2116 } 2117 2118 size_t machine__fprintf_dsos_buildid(struct machine *self, FILE *fp, bool with_hits) 2119 { 2120 return __dsos__fprintf_buildid(&self->kernel_dsos, fp, with_hits) + 2121 __dsos__fprintf_buildid(&self->user_dsos, fp, with_hits); 2122 } 2123 2124 size_t machines__fprintf_dsos_buildid(struct rb_root *self, FILE *fp, bool with_hits) 2125 { 2126 struct rb_node *nd; 2127 size_t ret = 0; 2128 2129 for (nd = rb_first(self); nd; nd = rb_next(nd)) { 2130 struct machine *pos = rb_entry(nd, struct machine, rb_node); 2131 ret += machine__fprintf_dsos_buildid(pos, fp, with_hits); 2132 } 2133 return ret; 2134 } 2135 2136 struct dso *dso__new_kernel(const char *name) 2137 { 2138 struct dso *self = dso__new(name ?: "[kernel.kallsyms]"); 2139 2140 if (self != NULL) { 2141 dso__set_short_name(self, "[kernel]"); 2142 self->kernel = DSO_TYPE_KERNEL; 2143 } 2144 2145 return self; 2146 } 2147 2148 static struct dso *dso__new_guest_kernel(struct machine *machine, 2149 const char *name) 2150 { 2151 char bf[PATH_MAX]; 2152 struct dso *self = dso__new(name ?: machine__mmap_name(machine, bf, sizeof(bf))); 2153 2154 if (self != NULL) { 2155 dso__set_short_name(self, "[guest.kernel]"); 2156 self->kernel = DSO_TYPE_GUEST_KERNEL; 2157 } 2158 2159 return self; 2160 } 2161 2162 void dso__read_running_kernel_build_id(struct dso *self, struct machine *machine) 2163 { 2164 char path[PATH_MAX]; 2165 2166 if (machine__is_default_guest(machine)) 2167 return; 2168 sprintf(path, "%s/sys/kernel/notes", machine->root_dir); 2169 if (sysfs__read_build_id(path, self->build_id, 2170 sizeof(self->build_id)) == 0) 2171 self->has_build_id = true; 2172 } 2173 2174 static struct dso *machine__create_kernel(struct machine *self) 2175 { 2176 const char *vmlinux_name = NULL; 2177 struct dso *kernel; 2178 2179 if (machine__is_host(self)) { 2180 vmlinux_name = symbol_conf.vmlinux_name; 2181 kernel = dso__new_kernel(vmlinux_name); 2182 } else { 2183 if (machine__is_default_guest(self)) 2184 vmlinux_name = symbol_conf.default_guest_vmlinux_name; 2185 kernel = dso__new_guest_kernel(self, vmlinux_name); 2186 } 2187 2188 if (kernel != NULL) { 2189 dso__read_running_kernel_build_id(kernel, self); 2190 dsos__add(&self->kernel_dsos, kernel); 2191 } 2192 return kernel; 2193 } 2194 2195 struct process_args { 2196 u64 start; 2197 }; 2198 2199 static int symbol__in_kernel(void *arg, const char *name, 2200 char type __used, u64 start, u64 end __used) 2201 { 2202 struct process_args *args = arg; 2203 2204 if (strchr(name, '[')) 2205 return 0; 2206 2207 args->start = start; 2208 return 1; 2209 } 2210 2211 /* Figure out the start address of kernel map from /proc/kallsyms */ 2212 static u64 machine__get_kernel_start_addr(struct machine *machine) 2213 { 2214 const char *filename; 2215 char path[PATH_MAX]; 2216 struct process_args args; 2217 2218 if (machine__is_host(machine)) { 2219 filename = "/proc/kallsyms"; 2220 } else { 2221 if (machine__is_default_guest(machine)) 2222 filename = (char *)symbol_conf.default_guest_kallsyms; 2223 else { 2224 sprintf(path, "%s/proc/kallsyms", machine->root_dir); 2225 filename = path; 2226 } 2227 } 2228 2229 if (kallsyms__parse(filename, &args, symbol__in_kernel) <= 0) 2230 return 0; 2231 2232 return args.start; 2233 } 2234 2235 int __machine__create_kernel_maps(struct machine *self, struct dso *kernel) 2236 { 2237 enum map_type type; 2238 u64 start = machine__get_kernel_start_addr(self); 2239 2240 for (type = 0; type < MAP__NR_TYPES; ++type) { 2241 struct kmap *kmap; 2242 2243 self->vmlinux_maps[type] = map__new2(start, kernel, type); 2244 if (self->vmlinux_maps[type] == NULL) 2245 return -1; 2246 2247 self->vmlinux_maps[type]->map_ip = 2248 self->vmlinux_maps[type]->unmap_ip = identity__map_ip; 2249 2250 kmap = map__kmap(self->vmlinux_maps[type]); 2251 kmap->kmaps = &self->kmaps; 2252 map_groups__insert(&self->kmaps, self->vmlinux_maps[type]); 2253 } 2254 2255 return 0; 2256 } 2257 2258 void machine__destroy_kernel_maps(struct machine *self) 2259 { 2260 enum map_type type; 2261 2262 for (type = 0; type < MAP__NR_TYPES; ++type) { 2263 struct kmap *kmap; 2264 2265 if (self->vmlinux_maps[type] == NULL) 2266 continue; 2267 2268 kmap = map__kmap(self->vmlinux_maps[type]); 2269 map_groups__remove(&self->kmaps, self->vmlinux_maps[type]); 2270 if (kmap->ref_reloc_sym) { 2271 /* 2272 * ref_reloc_sym is shared among all maps, so free just 2273 * on one of them. 2274 */ 2275 if (type == MAP__FUNCTION) { 2276 free((char *)kmap->ref_reloc_sym->name); 2277 kmap->ref_reloc_sym->name = NULL; 2278 free(kmap->ref_reloc_sym); 2279 } 2280 kmap->ref_reloc_sym = NULL; 2281 } 2282 2283 map__delete(self->vmlinux_maps[type]); 2284 self->vmlinux_maps[type] = NULL; 2285 } 2286 } 2287 2288 int machine__create_kernel_maps(struct machine *self) 2289 { 2290 struct dso *kernel = machine__create_kernel(self); 2291 2292 if (kernel == NULL || 2293 __machine__create_kernel_maps(self, kernel) < 0) 2294 return -1; 2295 2296 if (symbol_conf.use_modules && machine__create_modules(self) < 0) 2297 pr_debug("Problems creating module maps, continuing anyway...\n"); 2298 /* 2299 * Now that we have all the maps created, just set the ->end of them: 2300 */ 2301 map_groups__fixup_end(&self->kmaps); 2302 return 0; 2303 } 2304 2305 static void vmlinux_path__exit(void) 2306 { 2307 while (--vmlinux_path__nr_entries >= 0) { 2308 free(vmlinux_path[vmlinux_path__nr_entries]); 2309 vmlinux_path[vmlinux_path__nr_entries] = NULL; 2310 } 2311 2312 free(vmlinux_path); 2313 vmlinux_path = NULL; 2314 } 2315 2316 static int vmlinux_path__init(void) 2317 { 2318 struct utsname uts; 2319 char bf[PATH_MAX]; 2320 2321 vmlinux_path = malloc(sizeof(char *) * 5); 2322 if (vmlinux_path == NULL) 2323 return -1; 2324 2325 vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux"); 2326 if (vmlinux_path[vmlinux_path__nr_entries] == NULL) 2327 goto out_fail; 2328 ++vmlinux_path__nr_entries; 2329 vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux"); 2330 if (vmlinux_path[vmlinux_path__nr_entries] == NULL) 2331 goto out_fail; 2332 ++vmlinux_path__nr_entries; 2333 2334 /* only try running kernel version if no symfs was given */ 2335 if (symbol_conf.symfs[0] != 0) 2336 return 0; 2337 2338 if (uname(&uts) < 0) 2339 return -1; 2340 2341 snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release); 2342 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf); 2343 if (vmlinux_path[vmlinux_path__nr_entries] == NULL) 2344 goto out_fail; 2345 ++vmlinux_path__nr_entries; 2346 snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release); 2347 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf); 2348 if (vmlinux_path[vmlinux_path__nr_entries] == NULL) 2349 goto out_fail; 2350 ++vmlinux_path__nr_entries; 2351 snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux", 2352 uts.release); 2353 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf); 2354 if (vmlinux_path[vmlinux_path__nr_entries] == NULL) 2355 goto out_fail; 2356 ++vmlinux_path__nr_entries; 2357 2358 return 0; 2359 2360 out_fail: 2361 vmlinux_path__exit(); 2362 return -1; 2363 } 2364 2365 size_t machine__fprintf_vmlinux_path(struct machine *self, FILE *fp) 2366 { 2367 int i; 2368 size_t printed = 0; 2369 struct dso *kdso = self->vmlinux_maps[MAP__FUNCTION]->dso; 2370 2371 if (kdso->has_build_id) { 2372 char filename[PATH_MAX]; 2373 if (dso__build_id_filename(kdso, filename, sizeof(filename))) 2374 printed += fprintf(fp, "[0] %s\n", filename); 2375 } 2376 2377 for (i = 0; i < vmlinux_path__nr_entries; ++i) 2378 printed += fprintf(fp, "[%d] %s\n", 2379 i + kdso->has_build_id, vmlinux_path[i]); 2380 2381 return printed; 2382 } 2383 2384 static int setup_list(struct strlist **list, const char *list_str, 2385 const char *list_name) 2386 { 2387 if (list_str == NULL) 2388 return 0; 2389 2390 *list = strlist__new(true, list_str); 2391 if (!*list) { 2392 pr_err("problems parsing %s list\n", list_name); 2393 return -1; 2394 } 2395 return 0; 2396 } 2397 2398 int symbol__init(void) 2399 { 2400 const char *symfs; 2401 2402 if (symbol_conf.initialized) 2403 return 0; 2404 2405 elf_version(EV_CURRENT); 2406 if (symbol_conf.sort_by_name) 2407 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) - 2408 sizeof(struct symbol)); 2409 2410 if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0) 2411 return -1; 2412 2413 if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') { 2414 pr_err("'.' is the only non valid --field-separator argument\n"); 2415 return -1; 2416 } 2417 2418 if (setup_list(&symbol_conf.dso_list, 2419 symbol_conf.dso_list_str, "dso") < 0) 2420 return -1; 2421 2422 if (setup_list(&symbol_conf.comm_list, 2423 symbol_conf.comm_list_str, "comm") < 0) 2424 goto out_free_dso_list; 2425 2426 if (setup_list(&symbol_conf.sym_list, 2427 symbol_conf.sym_list_str, "symbol") < 0) 2428 goto out_free_comm_list; 2429 2430 /* 2431 * A path to symbols of "/" is identical to "" 2432 * reset here for simplicity. 2433 */ 2434 symfs = realpath(symbol_conf.symfs, NULL); 2435 if (symfs == NULL) 2436 symfs = symbol_conf.symfs; 2437 if (strcmp(symfs, "/") == 0) 2438 symbol_conf.symfs = ""; 2439 if (symfs != symbol_conf.symfs) 2440 free((void *)symfs); 2441 2442 symbol_conf.initialized = true; 2443 return 0; 2444 2445 out_free_dso_list: 2446 strlist__delete(symbol_conf.dso_list); 2447 out_free_comm_list: 2448 strlist__delete(symbol_conf.comm_list); 2449 return -1; 2450 } 2451 2452 void symbol__exit(void) 2453 { 2454 if (!symbol_conf.initialized) 2455 return; 2456 strlist__delete(symbol_conf.sym_list); 2457 strlist__delete(symbol_conf.dso_list); 2458 strlist__delete(symbol_conf.comm_list); 2459 vmlinux_path__exit(); 2460 symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL; 2461 symbol_conf.initialized = false; 2462 } 2463 2464 int machines__create_kernel_maps(struct rb_root *self, pid_t pid) 2465 { 2466 struct machine *machine = machines__findnew(self, pid); 2467 2468 if (machine == NULL) 2469 return -1; 2470 2471 return machine__create_kernel_maps(machine); 2472 } 2473 2474 static int hex(char ch) 2475 { 2476 if ((ch >= '0') && (ch <= '9')) 2477 return ch - '0'; 2478 if ((ch >= 'a') && (ch <= 'f')) 2479 return ch - 'a' + 10; 2480 if ((ch >= 'A') && (ch <= 'F')) 2481 return ch - 'A' + 10; 2482 return -1; 2483 } 2484 2485 /* 2486 * While we find nice hex chars, build a long_val. 2487 * Return number of chars processed. 2488 */ 2489 int hex2u64(const char *ptr, u64 *long_val) 2490 { 2491 const char *p = ptr; 2492 *long_val = 0; 2493 2494 while (*p) { 2495 const int hex_val = hex(*p); 2496 2497 if (hex_val < 0) 2498 break; 2499 2500 *long_val = (*long_val << 4) | hex_val; 2501 p++; 2502 } 2503 2504 return p - ptr; 2505 } 2506 2507 char *strxfrchar(char *s, char from, char to) 2508 { 2509 char *p = s; 2510 2511 while ((p = strchr(p, from)) != NULL) 2512 *p++ = to; 2513 2514 return s; 2515 } 2516 2517 int machines__create_guest_kernel_maps(struct rb_root *self) 2518 { 2519 int ret = 0; 2520 struct dirent **namelist = NULL; 2521 int i, items = 0; 2522 char path[PATH_MAX]; 2523 pid_t pid; 2524 2525 if (symbol_conf.default_guest_vmlinux_name || 2526 symbol_conf.default_guest_modules || 2527 symbol_conf.default_guest_kallsyms) { 2528 machines__create_kernel_maps(self, DEFAULT_GUEST_KERNEL_ID); 2529 } 2530 2531 if (symbol_conf.guestmount) { 2532 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL); 2533 if (items <= 0) 2534 return -ENOENT; 2535 for (i = 0; i < items; i++) { 2536 if (!isdigit(namelist[i]->d_name[0])) { 2537 /* Filter out . and .. */ 2538 continue; 2539 } 2540 pid = atoi(namelist[i]->d_name); 2541 sprintf(path, "%s/%s/proc/kallsyms", 2542 symbol_conf.guestmount, 2543 namelist[i]->d_name); 2544 ret = access(path, R_OK); 2545 if (ret) { 2546 pr_debug("Can't access file %s\n", path); 2547 goto failure; 2548 } 2549 machines__create_kernel_maps(self, pid); 2550 } 2551 failure: 2552 free(namelist); 2553 } 2554 2555 return ret; 2556 } 2557 2558 void machines__destroy_guest_kernel_maps(struct rb_root *self) 2559 { 2560 struct rb_node *next = rb_first(self); 2561 2562 while (next) { 2563 struct machine *pos = rb_entry(next, struct machine, rb_node); 2564 2565 next = rb_next(&pos->rb_node); 2566 rb_erase(&pos->rb_node, self); 2567 machine__delete(pos); 2568 } 2569 } 2570 2571 int machine__load_kallsyms(struct machine *self, const char *filename, 2572 enum map_type type, symbol_filter_t filter) 2573 { 2574 struct map *map = self->vmlinux_maps[type]; 2575 int ret = dso__load_kallsyms(map->dso, filename, map, filter); 2576 2577 if (ret > 0) { 2578 dso__set_loaded(map->dso, type); 2579 /* 2580 * Since /proc/kallsyms will have multiple sessions for the 2581 * kernel, with modules between them, fixup the end of all 2582 * sections. 2583 */ 2584 __map_groups__fixup_end(&self->kmaps, type); 2585 } 2586 2587 return ret; 2588 } 2589 2590 int machine__load_vmlinux_path(struct machine *self, enum map_type type, 2591 symbol_filter_t filter) 2592 { 2593 struct map *map = self->vmlinux_maps[type]; 2594 int ret = dso__load_vmlinux_path(map->dso, map, filter); 2595 2596 if (ret > 0) { 2597 dso__set_loaded(map->dso, type); 2598 map__reloc_vmlinux(map); 2599 } 2600 2601 return ret; 2602 } 2603