14 #include <sys/utsname.h>
16 #ifndef NT_GNU_BUILD_ID
17 #define NT_GNU_BUILD_ID 3
31 static void dsos__add(struct list_head *head, struct dso *dso);
32 static struct map *map__new2(u64 start, struct dso *dso, enum map_type type);
33 static int dso__load_kernel_sym(struct dso *self, struct map *map,
34 struct map_groups *mg, symbol_filter_t filter);
35 unsigned int symbol__priv_size;
36 static int vmlinux_path__nr_entries;
37 static char **vmlinux_path;
39 static struct symbol_conf symbol_conf__defaults = {
41 .try_vmlinux_path = true,
44 static struct map_groups kmaps_mem;
45 struct map_groups *kmaps = &kmaps_mem;
47 bool dso__loaded(const struct dso *self, enum map_type type)
49 return self->loaded & (1 << type);
52 bool dso__sorted_by_name(const struct dso *self, enum map_type type)
54 return self->sorted_by_name & (1 << type);
57 static void dso__set_loaded(struct dso *self, enum map_type type)
59 self->loaded |= (1 << type);
62 static void dso__set_sorted_by_name(struct dso *self, enum map_type type)
64 self->sorted_by_name |= (1 << type);
67 static bool symbol_type__is_a(char symbol_type, enum map_type map_type)
71 return symbol_type == 'T' || symbol_type == 'W';
73 return symbol_type == 'D' || symbol_type == 'd';
79 static void symbols__fixup_end(struct rb_root *self)
81 struct rb_node *nd, *prevnd = rb_first(self);
82 struct symbol *curr, *prev;
87 curr = rb_entry(prevnd, struct symbol, rb_node);
89 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
91 curr = rb_entry(nd, struct symbol, rb_node);
93 if (prev->end == prev->start)
94 prev->end = curr->start - 1;
98 if (curr->end == curr->start)
99 curr->end = roundup(curr->start, 4096);
102 static void __map_groups__fixup_end(struct map_groups *self, enum map_type type)
104 struct map *prev, *curr;
105 struct rb_node *nd, *prevnd = rb_first(&self->maps[type]);
110 curr = rb_entry(prevnd, struct map, rb_node);
112 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
114 curr = rb_entry(nd, struct map, rb_node);
115 prev->end = curr->start - 1;
119 * We still haven't the actual symbols, so guess the
120 * last map final address.
125 static void map_groups__fixup_end(struct map_groups *self)
128 for (i = 0; i < MAP__NR_TYPES; ++i)
129 __map_groups__fixup_end(self, i);
132 static struct symbol *symbol__new(u64 start, u64 len, const char *name)
134 size_t namelen = strlen(name) + 1;
135 struct symbol *self = zalloc(symbol__priv_size +
136 sizeof(*self) + namelen);
140 if (symbol__priv_size)
141 self = ((void *)self) + symbol__priv_size;
144 self->end = len ? start + len - 1 : start;
146 pr_debug3("%s: %s %#Lx-%#Lx\n", __func__, name, start, self->end);
148 memcpy(self->name, name, namelen);
153 static void symbol__delete(struct symbol *self)
155 free(((void *)self) - symbol__priv_size);
158 static size_t symbol__fprintf(struct symbol *self, FILE *fp)
160 return fprintf(fp, " %llx-%llx %s\n",
161 self->start, self->end, self->name);
164 static void dso__set_long_name(struct dso *self, char *name)
168 self->long_name = name;
169 self->long_name_len = strlen(name);
172 static void dso__set_basename(struct dso *self)
174 self->short_name = basename(self->long_name);
177 struct dso *dso__new(const char *name)
179 struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
183 strcpy(self->name, name);
184 dso__set_long_name(self, self->name);
185 self->short_name = self->name;
186 for (i = 0; i < MAP__NR_TYPES; ++i)
187 self->symbols[i] = self->symbol_names[i] = RB_ROOT;
188 self->slen_calculated = 0;
189 self->origin = DSO__ORIG_NOT_FOUND;
191 self->sorted_by_name = 0;
192 self->has_build_id = 0;
198 static void symbols__delete(struct rb_root *self)
201 struct rb_node *next = rb_first(self);
204 pos = rb_entry(next, struct symbol, rb_node);
205 next = rb_next(&pos->rb_node);
206 rb_erase(&pos->rb_node, self);
211 void dso__delete(struct dso *self)
214 for (i = 0; i < MAP__NR_TYPES; ++i)
215 symbols__delete(&self->symbols[i]);
216 if (self->long_name != self->name)
217 free(self->long_name);
221 void dso__set_build_id(struct dso *self, void *build_id)
223 memcpy(self->build_id, build_id, sizeof(self->build_id));
224 self->has_build_id = 1;
227 static void symbols__insert(struct rb_root *self, struct symbol *sym)
229 struct rb_node **p = &self->rb_node;
230 struct rb_node *parent = NULL;
231 const u64 ip = sym->start;
236 s = rb_entry(parent, struct symbol, rb_node);
242 rb_link_node(&sym->rb_node, parent, p);
243 rb_insert_color(&sym->rb_node, self);
246 static struct symbol *symbols__find(struct rb_root *self, u64 ip)
256 struct symbol *s = rb_entry(n, struct symbol, rb_node);
260 else if (ip > s->end)
269 struct symbol_name_rb_node {
270 struct rb_node rb_node;
274 static void symbols__insert_by_name(struct rb_root *self, struct symbol *sym)
276 struct rb_node **p = &self->rb_node;
277 struct rb_node *parent = NULL;
278 struct symbol_name_rb_node *symn = ((void *)sym) - sizeof(*parent), *s;
282 s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
283 if (strcmp(sym->name, s->sym.name) < 0)
288 rb_link_node(&symn->rb_node, parent, p);
289 rb_insert_color(&symn->rb_node, self);
292 static void symbols__sort_by_name(struct rb_root *self, struct rb_root *source)
296 for (nd = rb_first(source); nd; nd = rb_next(nd)) {
297 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
298 symbols__insert_by_name(self, pos);
302 static struct symbol *symbols__find_by_name(struct rb_root *self, const char *name)
312 struct symbol_name_rb_node *s;
315 s = rb_entry(n, struct symbol_name_rb_node, rb_node);
316 cmp = strcmp(name, s->sym.name);
329 struct symbol *dso__find_symbol(struct dso *self,
330 enum map_type type, u64 addr)
332 return symbols__find(&self->symbols[type], addr);
335 struct symbol *dso__find_symbol_by_name(struct dso *self, enum map_type type,
338 return symbols__find_by_name(&self->symbol_names[type], name);
341 void dso__sort_by_name(struct dso *self, enum map_type type)
343 dso__set_sorted_by_name(self, type);
344 return symbols__sort_by_name(&self->symbol_names[type],
345 &self->symbols[type]);
348 int build_id__sprintf(u8 *self, int len, char *bf)
354 for (i = 0; i < len; ++i) {
355 sprintf(bid, "%02x", *raw);
363 size_t dso__fprintf_buildid(struct dso *self, FILE *fp)
365 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
367 build_id__sprintf(self->build_id, sizeof(self->build_id), sbuild_id);
368 return fprintf(fp, "%s", sbuild_id);
371 size_t dso__fprintf(struct dso *self, enum map_type type, FILE *fp)
374 size_t ret = fprintf(fp, "dso: %s (", self->short_name);
376 ret += dso__fprintf_buildid(self, fp);
377 ret += fprintf(fp, ")\n");
378 for (nd = rb_first(&self->symbols[type]); nd; nd = rb_next(nd)) {
379 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
380 ret += symbol__fprintf(pos, fp);
387 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
388 * so that we can in the next step set the symbol ->end address and then
389 * call kernel_maps__split_kallsyms.
391 static int dso__load_all_kallsyms(struct dso *self, struct map *map)
395 struct rb_root *root = &self->symbols[map->type];
396 FILE *file = fopen("/proc/kallsyms", "r");
401 while (!feof(file)) {
408 line_len = getline(&line, &n, file);
415 line[--line_len] = '\0'; /* \n */
417 len = hex2u64(line, &start);
420 if (len + 2 >= line_len)
423 symbol_type = toupper(line[len]);
424 if (!symbol_type__is_a(symbol_type, map->type))
427 symbol_name = line + len + 2;
429 * Will fix up the end later, when we have all symbols sorted.
431 sym = symbol__new(start, 0, symbol_name);
434 goto out_delete_line;
436 * We will pass the symbols to the filter later, in
437 * map__split_kallsyms, when we have split the maps per module
439 symbols__insert(root, sym);
454 * Split the symbols into maps, making sure there are no overlaps, i.e. the
455 * kernel range is broken in several maps, named [kernel].N, as we don't have
456 * the original ELF section names vmlinux have.
458 static int dso__split_kallsyms(struct dso *self, struct map *map,
459 struct map_groups *mg, symbol_filter_t filter)
461 struct map *curr_map = map;
464 struct rb_root *root = &self->symbols[map->type];
465 struct rb_node *next = rb_first(root);
466 int kernel_range = 0;
471 pos = rb_entry(next, struct symbol, rb_node);
472 next = rb_next(&pos->rb_node);
474 module = strchr(pos->name, '\t');
476 if (!mg->use_modules)
481 if (strcmp(self->name, module)) {
482 curr_map = map_groups__find_by_name(mg, map->type, module);
483 if (curr_map == NULL) {
484 pr_debug("/proc/{kallsyms,modules} "
490 * So that we look just like we get from .ko files,
491 * i.e. not prelinked, relative to map->start.
493 pos->start = curr_map->map_ip(curr_map, pos->start);
494 pos->end = curr_map->map_ip(curr_map, pos->end);
495 } else if (curr_map != map) {
496 char dso_name[PATH_MAX];
499 snprintf(dso_name, sizeof(dso_name), "[kernel].%d",
502 dso = dso__new(dso_name);
506 curr_map = map__new2(pos->start, dso, map->type);
512 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
513 map_groups__insert(mg, curr_map);
517 if (filter && filter(curr_map, pos)) {
518 discard_symbol: rb_erase(&pos->rb_node, root);
521 if (curr_map != map) {
522 rb_erase(&pos->rb_node, root);
523 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
533 static int dso__load_kallsyms(struct dso *self, struct map *map,
534 struct map_groups *mg, symbol_filter_t filter)
536 if (dso__load_all_kallsyms(self, map) < 0)
539 symbols__fixup_end(&self->symbols[map->type]);
540 self->origin = DSO__ORIG_KERNEL;
542 return dso__split_kallsyms(self, map, mg, filter);
545 size_t kernel_maps__fprintf(FILE *fp)
547 size_t printed = fprintf(fp, "Kernel maps:\n");
548 printed += map_groups__fprintf_maps(kmaps, fp);
549 return printed + fprintf(fp, "END kernel maps\n");
552 static int dso__load_perf_map(struct dso *self, struct map *map,
553 symbol_filter_t filter)
560 file = fopen(self->long_name, "r");
564 while (!feof(file)) {
569 line_len = getline(&line, &n, file);
576 line[--line_len] = '\0'; /* \n */
578 len = hex2u64(line, &start);
581 if (len + 2 >= line_len)
584 len += hex2u64(line + len, &size);
587 if (len + 2 >= line_len)
590 sym = symbol__new(start, size, line + len);
593 goto out_delete_line;
595 if (filter && filter(map, sym))
598 symbols__insert(&self->symbols[map->type], sym);
615 * elf_symtab__for_each_symbol - iterate thru all the symbols
617 * @self: struct elf_symtab instance to iterate
619 * @sym: GElf_Sym iterator
621 #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
622 for (idx = 0, gelf_getsym(syms, idx, &sym);\
624 idx++, gelf_getsym(syms, idx, &sym))
626 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
628 return GELF_ST_TYPE(sym->st_info);
631 static inline int elf_sym__is_function(const GElf_Sym *sym)
633 return elf_sym__type(sym) == STT_FUNC &&
635 sym->st_shndx != SHN_UNDEF;
638 static inline bool elf_sym__is_object(const GElf_Sym *sym)
640 return elf_sym__type(sym) == STT_OBJECT &&
642 sym->st_shndx != SHN_UNDEF;
645 static inline int elf_sym__is_label(const GElf_Sym *sym)
647 return elf_sym__type(sym) == STT_NOTYPE &&
649 sym->st_shndx != SHN_UNDEF &&
650 sym->st_shndx != SHN_ABS;
653 static inline const char *elf_sec__name(const GElf_Shdr *shdr,
654 const Elf_Data *secstrs)
656 return secstrs->d_buf + shdr->sh_name;
659 static inline int elf_sec__is_text(const GElf_Shdr *shdr,
660 const Elf_Data *secstrs)
662 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
665 static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
666 const Elf_Data *secstrs)
668 return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
671 static inline const char *elf_sym__name(const GElf_Sym *sym,
672 const Elf_Data *symstrs)
674 return symstrs->d_buf + sym->st_name;
677 static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
678 GElf_Shdr *shp, const char *name,
684 while ((sec = elf_nextscn(elf, sec)) != NULL) {
687 gelf_getshdr(sec, shp);
688 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
689 if (!strcmp(name, str)) {
700 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
701 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
703 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
705 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
706 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
708 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
711 * We need to check if we have a .dynsym, so that we can handle the
712 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
713 * .dynsym or .symtab).
714 * And always look at the original dso, not at debuginfo packages, that
715 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
717 static int dso__synthesize_plt_symbols(struct dso *self, struct map *map,
718 symbol_filter_t filter)
720 uint32_t nr_rel_entries, idx;
725 GElf_Shdr shdr_rel_plt, shdr_dynsym;
726 Elf_Data *reldata, *syms, *symstrs;
727 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
730 char sympltname[1024];
732 int nr = 0, symidx, fd, err = 0;
734 fd = open(self->long_name, O_RDONLY);
738 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
742 if (gelf_getehdr(elf, &ehdr) == NULL)
745 scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
746 ".dynsym", &dynsym_idx);
747 if (scn_dynsym == NULL)
750 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
752 if (scn_plt_rel == NULL) {
753 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
755 if (scn_plt_rel == NULL)
761 if (shdr_rel_plt.sh_link != dynsym_idx)
764 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
768 * Fetch the relocation section to find the idxes to the GOT
769 * and the symbols in the .dynsym they refer to.
771 reldata = elf_getdata(scn_plt_rel, NULL);
775 syms = elf_getdata(scn_dynsym, NULL);
779 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
780 if (scn_symstrs == NULL)
783 symstrs = elf_getdata(scn_symstrs, NULL);
787 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
788 plt_offset = shdr_plt.sh_offset;
790 if (shdr_rel_plt.sh_type == SHT_RELA) {
791 GElf_Rela pos_mem, *pos;
793 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
795 symidx = GELF_R_SYM(pos->r_info);
796 plt_offset += shdr_plt.sh_entsize;
797 gelf_getsym(syms, symidx, &sym);
798 snprintf(sympltname, sizeof(sympltname),
799 "%s@plt", elf_sym__name(&sym, symstrs));
801 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
806 if (filter && filter(map, f))
809 symbols__insert(&self->symbols[map->type], f);
813 } else if (shdr_rel_plt.sh_type == SHT_REL) {
814 GElf_Rel pos_mem, *pos;
815 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
817 symidx = GELF_R_SYM(pos->r_info);
818 plt_offset += shdr_plt.sh_entsize;
819 gelf_getsym(syms, symidx, &sym);
820 snprintf(sympltname, sizeof(sympltname),
821 "%s@plt", elf_sym__name(&sym, symstrs));
823 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
828 if (filter && filter(map, f))
831 symbols__insert(&self->symbols[map->type], f);
846 pr_warning("%s: problems reading %s PLT info.\n",
847 __func__, self->long_name);
851 static bool elf_sym__is_a(GElf_Sym *self, enum map_type type)
855 return elf_sym__is_function(self);
857 return elf_sym__is_object(self);
863 static bool elf_sec__is_a(GElf_Shdr *self, Elf_Data *secstrs, enum map_type type)
867 return elf_sec__is_text(self, secstrs);
869 return elf_sec__is_data(self, secstrs);
875 static int dso__load_sym(struct dso *self, struct map *map,
876 struct map_groups *mg, const char *name, int fd,
877 symbol_filter_t filter, int kernel, int kmodule)
879 struct map *curr_map = map;
880 struct dso *curr_dso = self;
881 size_t dso_name_len = strlen(self->short_name);
882 Elf_Data *symstrs, *secstrs;
890 Elf_Scn *sec, *sec_strndx;
894 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
896 pr_err("%s: cannot read %s ELF file.\n", __func__, name);
900 if (gelf_getehdr(elf, &ehdr) == NULL) {
901 pr_err("%s: cannot get elf header.\n", __func__);
905 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
907 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
912 syms = elf_getdata(sec, NULL);
916 sec = elf_getscn(elf, shdr.sh_link);
920 symstrs = elf_getdata(sec, NULL);
924 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
925 if (sec_strndx == NULL)
928 secstrs = elf_getdata(sec_strndx, NULL);
932 nr_syms = shdr.sh_size / shdr.sh_entsize;
934 memset(&sym, 0, sizeof(sym));
936 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
937 elf_section_by_name(elf, &ehdr, &shdr,
940 } else self->adjust_symbols = 0;
942 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
944 const char *elf_name;
945 char *demangled = NULL;
946 int is_label = elf_sym__is_label(&sym);
947 const char *section_name;
949 if (!is_label && !elf_sym__is_a(&sym, map->type))
952 sec = elf_getscn(elf, sym.st_shndx);
956 gelf_getshdr(sec, &shdr);
958 if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type))
961 elf_name = elf_sym__name(&sym, symstrs);
962 section_name = elf_sec__name(&shdr, secstrs);
964 if (kernel || kmodule) {
965 char dso_name[PATH_MAX];
967 if (strcmp(section_name,
968 curr_dso->short_name + dso_name_len) == 0)
971 if (strcmp(section_name, ".text") == 0) {
977 snprintf(dso_name, sizeof(dso_name),
978 "%s%s", self->short_name, section_name);
980 curr_map = map_groups__find_by_name(mg, map->type, dso_name);
981 if (curr_map == NULL) {
982 u64 start = sym.st_value;
985 start += map->start + shdr.sh_offset;
987 curr_dso = dso__new(dso_name);
988 if (curr_dso == NULL)
990 curr_map = map__new2(start, curr_dso,
992 if (curr_map == NULL) {
993 dso__delete(curr_dso);
996 curr_map->map_ip = identity__map_ip;
997 curr_map->unmap_ip = identity__map_ip;
998 curr_dso->origin = DSO__ORIG_KERNEL;
999 map_groups__insert(kmaps, curr_map);
1000 dsos__add(&dsos__kernel, curr_dso);
1002 curr_dso = curr_map->dso;
1007 if (curr_dso->adjust_symbols) {
1008 pr_debug2("adjusting symbol: st_value: %Lx sh_addr: "
1009 "%Lx sh_offset: %Lx\n", (u64)sym.st_value,
1010 (u64)shdr.sh_addr, (u64)shdr.sh_offset);
1011 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
1014 * We need to figure out if the object was created from C++ sources
1015 * DWARF DW_compile_unit has this, but we don't always have access
1018 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
1019 if (demangled != NULL)
1020 elf_name = demangled;
1022 f = symbol__new(sym.st_value, sym.st_size, elf_name);
1027 if (filter && filter(curr_map, f))
1030 symbols__insert(&curr_dso->symbols[curr_map->type], f);
1036 * For misannotated, zeroed, ASM function sizes.
1039 symbols__fixup_end(&self->symbols[map->type]);
1047 static bool dso__build_id_equal(const struct dso *self, u8 *build_id)
1049 return memcmp(self->build_id, build_id, sizeof(self->build_id)) == 0;
1052 static bool __dsos__read_build_ids(struct list_head *head)
1054 bool have_build_id = false;
1057 list_for_each_entry(pos, head, node)
1058 if (filename__read_build_id(pos->long_name, pos->build_id,
1059 sizeof(pos->build_id)) > 0) {
1060 have_build_id = true;
1061 pos->has_build_id = true;
1064 return have_build_id;
1067 bool dsos__read_build_ids(void)
1069 bool kbuildids = __dsos__read_build_ids(&dsos__kernel),
1070 ubuildids = __dsos__read_build_ids(&dsos__user);
1071 return kbuildids || ubuildids;
1075 * Align offset to 4 bytes as needed for note name and descriptor data.
1077 #define NOTE_ALIGN(n) (((n) + 3) & -4U)
1079 int filename__read_build_id(const char *filename, void *bf, size_t size)
1090 if (size < BUILD_ID_SIZE)
1093 fd = open(filename, O_RDONLY);
1097 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1099 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
1104 if (ek != ELF_K_ELF)
1107 if (gelf_getehdr(elf, &ehdr) == NULL) {
1108 pr_err("%s: cannot get elf header.\n", __func__);
1112 sec = elf_section_by_name(elf, &ehdr, &shdr,
1113 ".note.gnu.build-id", NULL);
1115 sec = elf_section_by_name(elf, &ehdr, &shdr,
1121 data = elf_getdata(sec, NULL);
1126 while (ptr < (data->d_buf + data->d_size)) {
1127 GElf_Nhdr *nhdr = ptr;
1128 int namesz = NOTE_ALIGN(nhdr->n_namesz),
1129 descsz = NOTE_ALIGN(nhdr->n_descsz);
1132 ptr += sizeof(*nhdr);
1135 if (nhdr->n_type == NT_GNU_BUILD_ID &&
1136 nhdr->n_namesz == sizeof("GNU")) {
1137 if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
1138 memcpy(bf, ptr, BUILD_ID_SIZE);
1139 err = BUILD_ID_SIZE;
1153 int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
1157 if (size < BUILD_ID_SIZE)
1160 fd = open(filename, O_RDONLY);
1169 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
1172 namesz = NOTE_ALIGN(nhdr.n_namesz);
1173 descsz = NOTE_ALIGN(nhdr.n_descsz);
1174 if (nhdr.n_type == NT_GNU_BUILD_ID &&
1175 nhdr.n_namesz == sizeof("GNU")) {
1176 if (read(fd, bf, namesz) != namesz)
1178 if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
1179 if (read(fd, build_id,
1180 BUILD_ID_SIZE) == BUILD_ID_SIZE) {
1184 } else if (read(fd, bf, descsz) != descsz)
1187 int n = namesz + descsz;
1188 if (read(fd, bf, n) != n)
1197 char dso__symtab_origin(const struct dso *self)
1199 static const char origin[] = {
1200 [DSO__ORIG_KERNEL] = 'k',
1201 [DSO__ORIG_JAVA_JIT] = 'j',
1202 [DSO__ORIG_FEDORA] = 'f',
1203 [DSO__ORIG_UBUNTU] = 'u',
1204 [DSO__ORIG_BUILDID] = 'b',
1205 [DSO__ORIG_DSO] = 'd',
1206 [DSO__ORIG_KMODULE] = 'K',
1209 if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
1211 return origin[self->origin];
1214 int dso__load(struct dso *self, struct map *map, symbol_filter_t filter)
1216 int size = PATH_MAX;
1218 u8 build_id[BUILD_ID_SIZE];
1222 dso__set_loaded(self, map->type);
1225 return dso__load_kernel_sym(self, map, kmaps, filter);
1227 name = malloc(size);
1231 self->adjust_symbols = 0;
1233 if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
1234 ret = dso__load_perf_map(self, map, filter);
1235 self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
1236 DSO__ORIG_NOT_FOUND;
1240 self->origin = DSO__ORIG_FEDORA - 1;
1245 switch (self->origin) {
1246 case DSO__ORIG_FEDORA:
1247 snprintf(name, size, "/usr/lib/debug%s.debug",
1250 case DSO__ORIG_UBUNTU:
1251 snprintf(name, size, "/usr/lib/debug%s",
1254 case DSO__ORIG_BUILDID:
1255 if (filename__read_build_id(self->long_name, build_id,
1256 sizeof(build_id))) {
1257 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
1259 build_id__sprintf(build_id, sizeof(build_id),
1261 snprintf(name, size,
1262 "/usr/lib/debug/.build-id/%.2s/%s.debug",
1263 build_id_hex, build_id_hex + 2);
1264 if (self->has_build_id)
1265 goto compare_build_id;
1271 snprintf(name, size, "%s", self->long_name);
1278 if (self->has_build_id) {
1279 if (filename__read_build_id(name, build_id,
1280 sizeof(build_id)) < 0)
1283 if (!dso__build_id_equal(self, build_id))
1287 fd = open(name, O_RDONLY);
1290 ret = dso__load_sym(self, map, NULL, name, fd, filter, 0, 0);
1294 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
1300 int nr_plt = dso__synthesize_plt_symbols(self, map, filter);
1306 if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
1311 struct map *map_groups__find_by_name(struct map_groups *self,
1312 enum map_type type, const char *name)
1316 for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) {
1317 struct map *map = rb_entry(nd, struct map, rb_node);
1319 if (map->dso && strcmp(map->dso->name, name) == 0)
1326 static int dsos__set_modules_path_dir(char *dirname)
1328 struct dirent *dent;
1329 DIR *dir = opendir(dirname);
1332 pr_debug("%s: cannot open %s dir\n", __func__, dirname);
1336 while ((dent = readdir(dir)) != NULL) {
1337 char path[PATH_MAX];
1339 if (dent->d_type == DT_DIR) {
1340 if (!strcmp(dent->d_name, ".") ||
1341 !strcmp(dent->d_name, ".."))
1344 snprintf(path, sizeof(path), "%s/%s",
1345 dirname, dent->d_name);
1346 if (dsos__set_modules_path_dir(path) < 0)
1349 char *dot = strrchr(dent->d_name, '.'),
1354 if (dot == NULL || strcmp(dot, ".ko"))
1356 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1357 (int)(dot - dent->d_name), dent->d_name);
1359 strxfrchar(dso_name, '-', '_');
1360 map = map_groups__find_by_name(kmaps, MAP__FUNCTION, dso_name);
1364 snprintf(path, sizeof(path), "%s/%s",
1365 dirname, dent->d_name);
1367 long_name = strdup(path);
1368 if (long_name == NULL)
1370 dso__set_long_name(map->dso, long_name);
1380 static int dsos__set_modules_path(void)
1383 char modules_path[PATH_MAX];
1385 if (uname(&uts) < 0)
1388 snprintf(modules_path, sizeof(modules_path), "/lib/modules/%s/kernel",
1391 return dsos__set_modules_path_dir(modules_path);
1395 * Constructor variant for modules (where we know from /proc/modules where
1396 * they are loaded) and for vmlinux, where only after we load all the
1397 * symbols we'll know where it starts and ends.
1399 static struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
1401 struct map *self = malloc(sizeof(*self));
1405 * ->end will be filled after we load all the symbols
1407 map__init(self, type, start, 0, 0, dso);
1413 static int map_groups__create_module_maps(struct map_groups *self)
1417 FILE *file = fopen("/proc/modules", "r");
1423 while (!feof(file)) {
1424 char name[PATH_MAX];
1430 line_len = getline(&line, &n, file);
1437 line[--line_len] = '\0'; /* \n */
1439 sep = strrchr(line, 'x');
1443 hex2u64(sep + 1, &start);
1445 sep = strchr(line, ' ');
1451 snprintf(name, sizeof(name), "[%s]", line);
1452 dso = dso__new(name);
1455 goto out_delete_line;
1457 map = map__new2(start, dso, MAP__FUNCTION);
1460 goto out_delete_line;
1463 snprintf(name, sizeof(name),
1464 "/sys/module/%s/notes/.note.gnu.build-id", line);
1465 if (sysfs__read_build_id(name, dso->build_id,
1466 sizeof(dso->build_id)) == 0)
1467 dso->has_build_id = true;
1469 dso->origin = DSO__ORIG_KMODULE;
1470 map_groups__insert(self, map);
1471 dsos__add(&dsos__kernel, dso);
1477 return dsos__set_modules_path();
1485 static int dso__load_vmlinux(struct dso *self, struct map *map,
1486 struct map_groups *mg,
1487 const char *vmlinux, symbol_filter_t filter)
1491 if (self->has_build_id) {
1492 u8 build_id[BUILD_ID_SIZE];
1494 if (filename__read_build_id(vmlinux, build_id,
1495 sizeof(build_id)) < 0) {
1496 pr_debug("No build_id in %s, ignoring it\n", vmlinux);
1499 if (!dso__build_id_equal(self, build_id)) {
1500 char expected_build_id[BUILD_ID_SIZE * 2 + 1],
1501 vmlinux_build_id[BUILD_ID_SIZE * 2 + 1];
1503 build_id__sprintf(self->build_id,
1504 sizeof(self->build_id),
1506 build_id__sprintf(build_id, sizeof(build_id),
1508 pr_debug("build_id in %s is %s while expected is %s, "
1509 "ignoring it\n", vmlinux, vmlinux_build_id,
1515 fd = open(vmlinux, O_RDONLY);
1519 dso__set_loaded(self, map->type);
1520 err = dso__load_sym(self, map, mg, self->long_name, fd, filter, 1, 0);
1526 static int dso__load_kernel_sym(struct dso *self, struct map *map,
1527 struct map_groups *mg, symbol_filter_t filter)
1532 if (vmlinux_path != NULL) {
1534 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1535 vmlinux_path__nr_entries);
1536 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
1537 err = dso__load_vmlinux(self, map, mg,
1538 vmlinux_path[i], filter);
1540 pr_debug("Using %s for symbols\n",
1542 dso__set_long_name(self,
1543 strdup(vmlinux_path[i]));
1549 is_kallsyms = self->long_name[0] == '[';
1553 err = dso__load_vmlinux(self, map, mg, self->long_name, filter);
1555 pr_info("The file %s cannot be used, "
1556 "trying to use /proc/kallsyms...", self->long_name);
1558 err = dso__load_kallsyms(self, map, mg, filter);
1559 if (err > 0 && !is_kallsyms)
1560 dso__set_long_name(self, strdup("[kernel.kallsyms]"));
1565 map__fixup_start(map);
1566 map__fixup_end(map);
1572 LIST_HEAD(dsos__user);
1573 LIST_HEAD(dsos__kernel);
1576 static void dsos__add(struct list_head *head, struct dso *dso)
1578 list_add_tail(&dso->node, head);
1581 static struct dso *dsos__find(struct list_head *head, const char *name)
1585 list_for_each_entry(pos, head, node)
1586 if (strcmp(pos->name, name) == 0)
1591 struct dso *dsos__findnew(const char *name)
1593 struct dso *dso = dsos__find(&dsos__user, name);
1596 dso = dso__new(name);
1598 dsos__add(&dsos__user, dso);
1599 dso__set_basename(dso);
1606 static void __dsos__fprintf(struct list_head *head, FILE *fp)
1610 list_for_each_entry(pos, head, node) {
1612 for (i = 0; i < MAP__NR_TYPES; ++i)
1613 dso__fprintf(pos, i, fp);
1617 void dsos__fprintf(FILE *fp)
1619 __dsos__fprintf(&dsos__kernel, fp);
1620 __dsos__fprintf(&dsos__user, fp);
1623 static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp)
1628 list_for_each_entry(pos, head, node) {
1629 ret += dso__fprintf_buildid(pos, fp);
1630 ret += fprintf(fp, " %s\n", pos->long_name);
1635 size_t dsos__fprintf_buildid(FILE *fp)
1637 return (__dsos__fprintf_buildid(&dsos__kernel, fp) +
1638 __dsos__fprintf_buildid(&dsos__user, fp));
1641 static struct dso *dsos__create_kernel( const char *vmlinux)
1643 struct dso *kernel = dso__new(vmlinux ?: "[kernel.kallsyms]");
1648 kernel->short_name = "[kernel]";
1651 vdso = dso__new("[vdso]");
1653 goto out_delete_kernel_dso;
1654 dso__set_loaded(vdso, MAP__FUNCTION);
1656 if (sysfs__read_build_id("/sys/kernel/notes", kernel->build_id,
1657 sizeof(kernel->build_id)) == 0)
1658 kernel->has_build_id = true;
1660 dsos__add(&dsos__kernel, kernel);
1661 dsos__add(&dsos__user, vdso);
1665 out_delete_kernel_dso:
1666 dso__delete(kernel);
1670 static int map_groups__create_kernel_maps(struct map_groups *self, const char *vmlinux)
1672 struct map *functions, *variables;
1673 struct dso *kernel = dsos__create_kernel(vmlinux);
1678 functions = map__new2(0, kernel, MAP__FUNCTION);
1679 if (functions == NULL)
1682 variables = map__new2(0, kernel, MAP__VARIABLE);
1683 if (variables == NULL) {
1684 map__delete(functions);
1688 functions->map_ip = functions->unmap_ip =
1689 variables->map_ip = variables->unmap_ip = identity__map_ip;
1690 map_groups__insert(self, functions);
1691 map_groups__insert(self, variables);
1696 static void vmlinux_path__exit(void)
1698 while (--vmlinux_path__nr_entries >= 0) {
1699 free(vmlinux_path[vmlinux_path__nr_entries]);
1700 vmlinux_path[vmlinux_path__nr_entries] = NULL;
1704 vmlinux_path = NULL;
1707 static int vmlinux_path__init(void)
1712 if (uname(&uts) < 0)
1715 vmlinux_path = malloc(sizeof(char *) * 5);
1716 if (vmlinux_path == NULL)
1719 vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
1720 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1722 ++vmlinux_path__nr_entries;
1723 vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
1724 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1726 ++vmlinux_path__nr_entries;
1727 snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
1728 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1729 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1731 ++vmlinux_path__nr_entries;
1732 snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
1733 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1734 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1736 ++vmlinux_path__nr_entries;
1737 snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
1739 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1740 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1742 ++vmlinux_path__nr_entries;
1747 vmlinux_path__exit();
1751 int symbol__init(struct symbol_conf *conf)
1753 const struct symbol_conf *pconf = conf ?: &symbol_conf__defaults;
1755 elf_version(EV_CURRENT);
1756 symbol__priv_size = pconf->priv_size;
1757 if (pconf->sort_by_name)
1758 symbol__priv_size += (sizeof(struct symbol_name_rb_node) -
1759 sizeof(struct symbol));
1760 map_groups__init(kmaps);
1762 if (pconf->try_vmlinux_path && vmlinux_path__init() < 0)
1765 if (map_groups__create_kernel_maps(kmaps, pconf->vmlinux_name) < 0) {
1766 vmlinux_path__exit();
1770 kmaps->use_modules = pconf->use_modules;
1771 if (pconf->use_modules && map_groups__create_module_maps(kmaps) < 0)
1772 pr_debug("Failed to load list of modules in use, "
1775 * Now that we have all the maps created, just set the ->end of them:
1777 map_groups__fixup_end(kmaps);