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 struct symbol *dso__find_symbol(struct dso *self, enum map_type type, u64 addr);
34 static int dso__load_kernel_sym(struct dso *self, struct map *map,
35 struct map_groups *mg, symbol_filter_t filter);
36 unsigned int symbol__priv_size;
37 static int vmlinux_path__nr_entries;
38 static char **vmlinux_path;
40 static struct symbol_conf symbol_conf__defaults = {
42 .try_vmlinux_path = true,
45 static struct map_groups kmaps_mem;
46 struct map_groups *kmaps = &kmaps_mem;
48 bool dso__loaded(const struct dso *self, enum map_type type)
50 return self->loaded & (1 << type);
53 bool dso__sorted_by_name(const struct dso *self, enum map_type type)
55 return self->sorted_by_name & (1 << type);
58 static void dso__set_loaded(struct dso *self, enum map_type type)
60 self->loaded |= (1 << type);
63 static void dso__set_sorted_by_name(struct dso *self, enum map_type type)
65 self->sorted_by_name |= (1 << type);
68 static bool symbol_type__is_a(char symbol_type, enum map_type map_type)
72 return symbol_type == 'T' || symbol_type == 'W';
74 return symbol_type == 'D' || symbol_type == 'd';
80 static void symbols__fixup_end(struct rb_root *self)
82 struct rb_node *nd, *prevnd = rb_first(self);
83 struct symbol *curr, *prev;
88 curr = rb_entry(prevnd, struct symbol, rb_node);
90 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
92 curr = rb_entry(nd, struct symbol, rb_node);
94 if (prev->end == prev->start)
95 prev->end = curr->start - 1;
99 if (curr->end == curr->start)
100 curr->end = roundup(curr->start, 4096);
103 static void __map_groups__fixup_end(struct map_groups *self, enum map_type type)
105 struct map *prev, *curr;
106 struct rb_node *nd, *prevnd = rb_first(&self->maps[type]);
111 curr = rb_entry(prevnd, struct map, rb_node);
113 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
115 curr = rb_entry(nd, struct map, rb_node);
116 prev->end = curr->start - 1;
120 * We still haven't the actual symbols, so guess the
121 * last map final address.
126 static void map_groups__fixup_end(struct map_groups *self)
129 for (i = 0; i < MAP__NR_TYPES; ++i)
130 __map_groups__fixup_end(self, i);
133 static struct symbol *symbol__new(u64 start, u64 len, const char *name)
135 size_t namelen = strlen(name) + 1;
136 struct symbol *self = zalloc(symbol__priv_size +
137 sizeof(*self) + namelen);
141 if (symbol__priv_size)
142 self = ((void *)self) + symbol__priv_size;
145 self->end = len ? start + len - 1 : start;
147 pr_debug3("%s: %s %#Lx-%#Lx\n", __func__, name, start, self->end);
149 memcpy(self->name, name, namelen);
154 static void symbol__delete(struct symbol *self)
156 free(((void *)self) - symbol__priv_size);
159 static size_t symbol__fprintf(struct symbol *self, FILE *fp)
161 return fprintf(fp, " %llx-%llx %s\n",
162 self->start, self->end, self->name);
165 static void dso__set_long_name(struct dso *self, char *name)
169 self->long_name = name;
170 self->long_name_len = strlen(name);
173 static void dso__set_basename(struct dso *self)
175 self->short_name = basename(self->long_name);
178 struct dso *dso__new(const char *name)
180 struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
184 strcpy(self->name, name);
185 dso__set_long_name(self, self->name);
186 self->short_name = self->name;
187 for (i = 0; i < MAP__NR_TYPES; ++i)
188 self->symbols[i] = self->symbol_names[i] = RB_ROOT;
189 self->find_symbol = dso__find_symbol;
190 self->slen_calculated = 0;
191 self->origin = DSO__ORIG_NOT_FOUND;
193 self->sorted_by_name = 0;
194 self->has_build_id = 0;
200 static void symbols__delete(struct rb_root *self)
203 struct rb_node *next = rb_first(self);
206 pos = rb_entry(next, struct symbol, rb_node);
207 next = rb_next(&pos->rb_node);
208 rb_erase(&pos->rb_node, self);
213 void dso__delete(struct dso *self)
216 for (i = 0; i < MAP__NR_TYPES; ++i)
217 symbols__delete(&self->symbols[i]);
218 if (self->long_name != self->name)
219 free(self->long_name);
223 void dso__set_build_id(struct dso *self, void *build_id)
225 memcpy(self->build_id, build_id, sizeof(self->build_id));
226 self->has_build_id = 1;
229 static void symbols__insert(struct rb_root *self, struct symbol *sym)
231 struct rb_node **p = &self->rb_node;
232 struct rb_node *parent = NULL;
233 const u64 ip = sym->start;
238 s = rb_entry(parent, struct symbol, rb_node);
244 rb_link_node(&sym->rb_node, parent, p);
245 rb_insert_color(&sym->rb_node, self);
248 static struct symbol *symbols__find(struct rb_root *self, u64 ip)
258 struct symbol *s = rb_entry(n, struct symbol, rb_node);
262 else if (ip > s->end)
271 struct symbol_name_rb_node {
272 struct rb_node rb_node;
276 static void symbols__insert_by_name(struct rb_root *self, struct symbol *sym)
278 struct rb_node **p = &self->rb_node;
279 struct rb_node *parent = NULL;
280 struct symbol_name_rb_node *symn = ((void *)sym) - sizeof(*parent), *s;
284 s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
285 if (strcmp(sym->name, s->sym.name) < 0)
290 rb_link_node(&symn->rb_node, parent, p);
291 rb_insert_color(&symn->rb_node, self);
294 static void symbols__sort_by_name(struct rb_root *self, struct rb_root *source)
298 for (nd = rb_first(source); nd; nd = rb_next(nd)) {
299 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
300 symbols__insert_by_name(self, pos);
304 static struct symbol *symbols__find_by_name(struct rb_root *self, const char *name)
314 struct symbol_name_rb_node *s;
317 s = rb_entry(n, struct symbol_name_rb_node, rb_node);
318 cmp = strcmp(name, s->sym.name);
331 struct symbol *dso__find_symbol(struct dso *self,
332 enum map_type type, u64 addr)
334 return symbols__find(&self->symbols[type], addr);
337 struct symbol *dso__find_symbol_by_name(struct dso *self, enum map_type type,
340 return symbols__find_by_name(&self->symbol_names[type], name);
343 void dso__sort_by_name(struct dso *self, enum map_type type)
345 dso__set_sorted_by_name(self, type);
346 return symbols__sort_by_name(&self->symbol_names[type],
347 &self->symbols[type]);
350 int build_id__sprintf(u8 *self, int len, char *bf)
356 for (i = 0; i < len; ++i) {
357 sprintf(bid, "%02x", *raw);
365 size_t dso__fprintf_buildid(struct dso *self, FILE *fp)
367 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
369 build_id__sprintf(self->build_id, sizeof(self->build_id), sbuild_id);
370 return fprintf(fp, "%s", sbuild_id);
373 size_t dso__fprintf(struct dso *self, enum map_type type, FILE *fp)
376 size_t ret = fprintf(fp, "dso: %s (", self->short_name);
378 ret += dso__fprintf_buildid(self, fp);
379 ret += fprintf(fp, ")\n");
380 for (nd = rb_first(&self->symbols[type]); nd; nd = rb_next(nd)) {
381 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
382 ret += symbol__fprintf(pos, fp);
389 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
390 * so that we can in the next step set the symbol ->end address and then
391 * call kernel_maps__split_kallsyms.
393 static int dso__load_all_kallsyms(struct dso *self, struct map *map)
397 struct rb_root *root = &self->symbols[map->type];
398 FILE *file = fopen("/proc/kallsyms", "r");
403 while (!feof(file)) {
410 line_len = getline(&line, &n, file);
417 line[--line_len] = '\0'; /* \n */
419 len = hex2u64(line, &start);
422 if (len + 2 >= line_len)
425 symbol_type = toupper(line[len]);
426 if (!symbol_type__is_a(symbol_type, map->type))
429 symbol_name = line + len + 2;
431 * Will fix up the end later, when we have all symbols sorted.
433 sym = symbol__new(start, 0, symbol_name);
436 goto out_delete_line;
438 * We will pass the symbols to the filter later, in
439 * map__split_kallsyms, when we have split the maps per module
441 symbols__insert(root, sym);
456 * Split the symbols into maps, making sure there are no overlaps, i.e. the
457 * kernel range is broken in several maps, named [kernel].N, as we don't have
458 * the original ELF section names vmlinux have.
460 static int dso__split_kallsyms(struct dso *self, struct map *map,
461 struct map_groups *mg, symbol_filter_t filter)
463 struct map *curr_map = map;
466 struct rb_root *root = &self->symbols[map->type];
467 struct rb_node *next = rb_first(root);
468 int kernel_range = 0;
473 pos = rb_entry(next, struct symbol, rb_node);
474 next = rb_next(&pos->rb_node);
476 module = strchr(pos->name, '\t');
478 if (!mg->use_modules)
483 if (strcmp(self->name, module)) {
484 curr_map = map_groups__find_by_name(mg, map->type, module);
485 if (curr_map == NULL) {
486 pr_debug("/proc/{kallsyms,modules} "
492 * So that we look just like we get from .ko files,
493 * i.e. not prelinked, relative to map->start.
495 pos->start = curr_map->map_ip(curr_map, pos->start);
496 pos->end = curr_map->map_ip(curr_map, pos->end);
497 } else if (curr_map != map) {
498 char dso_name[PATH_MAX];
501 snprintf(dso_name, sizeof(dso_name), "[kernel].%d",
504 dso = dso__new(dso_name);
508 curr_map = map__new2(pos->start, dso, map->type);
514 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
515 map_groups__insert(mg, curr_map);
519 if (filter && filter(curr_map, pos)) {
520 discard_symbol: rb_erase(&pos->rb_node, root);
523 if (curr_map != map) {
524 rb_erase(&pos->rb_node, root);
525 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
535 static int dso__load_kallsyms(struct dso *self, struct map *map,
536 struct map_groups *mg, symbol_filter_t filter)
538 if (dso__load_all_kallsyms(self, map) < 0)
541 symbols__fixup_end(&self->symbols[map->type]);
542 self->origin = DSO__ORIG_KERNEL;
544 return dso__split_kallsyms(self, map, mg, filter);
547 size_t kernel_maps__fprintf(FILE *fp)
549 size_t printed = fprintf(fp, "Kernel maps:\n");
550 printed += map_groups__fprintf_maps(kmaps, fp);
551 return printed + fprintf(fp, "END kernel maps\n");
554 static int dso__load_perf_map(struct dso *self, struct map *map,
555 symbol_filter_t filter)
562 file = fopen(self->long_name, "r");
566 while (!feof(file)) {
571 line_len = getline(&line, &n, file);
578 line[--line_len] = '\0'; /* \n */
580 len = hex2u64(line, &start);
583 if (len + 2 >= line_len)
586 len += hex2u64(line + len, &size);
589 if (len + 2 >= line_len)
592 sym = symbol__new(start, size, line + len);
595 goto out_delete_line;
597 if (filter && filter(map, sym))
600 symbols__insert(&self->symbols[map->type], sym);
617 * elf_symtab__for_each_symbol - iterate thru all the symbols
619 * @self: struct elf_symtab instance to iterate
621 * @sym: GElf_Sym iterator
623 #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
624 for (idx = 0, gelf_getsym(syms, idx, &sym);\
626 idx++, gelf_getsym(syms, idx, &sym))
628 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
630 return GELF_ST_TYPE(sym->st_info);
633 static inline int elf_sym__is_function(const GElf_Sym *sym)
635 return elf_sym__type(sym) == STT_FUNC &&
637 sym->st_shndx != SHN_UNDEF;
640 static inline bool elf_sym__is_object(const GElf_Sym *sym)
642 return elf_sym__type(sym) == STT_OBJECT &&
644 sym->st_shndx != SHN_UNDEF;
647 static inline int elf_sym__is_label(const GElf_Sym *sym)
649 return elf_sym__type(sym) == STT_NOTYPE &&
651 sym->st_shndx != SHN_UNDEF &&
652 sym->st_shndx != SHN_ABS;
655 static inline const char *elf_sec__name(const GElf_Shdr *shdr,
656 const Elf_Data *secstrs)
658 return secstrs->d_buf + shdr->sh_name;
661 static inline int elf_sec__is_text(const GElf_Shdr *shdr,
662 const Elf_Data *secstrs)
664 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
667 static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
668 const Elf_Data *secstrs)
670 return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
673 static inline const char *elf_sym__name(const GElf_Sym *sym,
674 const Elf_Data *symstrs)
676 return symstrs->d_buf + sym->st_name;
679 static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
680 GElf_Shdr *shp, const char *name,
686 while ((sec = elf_nextscn(elf, sec)) != NULL) {
689 gelf_getshdr(sec, shp);
690 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
691 if (!strcmp(name, str)) {
702 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
703 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
705 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
707 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
708 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
710 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
713 * We need to check if we have a .dynsym, so that we can handle the
714 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
715 * .dynsym or .symtab).
716 * And always look at the original dso, not at debuginfo packages, that
717 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
719 static int dso__synthesize_plt_symbols(struct dso *self, struct map *map,
720 symbol_filter_t filter)
722 uint32_t nr_rel_entries, idx;
727 GElf_Shdr shdr_rel_plt, shdr_dynsym;
728 Elf_Data *reldata, *syms, *symstrs;
729 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
732 char sympltname[1024];
734 int nr = 0, symidx, fd, err = 0;
736 fd = open(self->long_name, O_RDONLY);
740 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
744 if (gelf_getehdr(elf, &ehdr) == NULL)
747 scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
748 ".dynsym", &dynsym_idx);
749 if (scn_dynsym == NULL)
752 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
754 if (scn_plt_rel == NULL) {
755 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
757 if (scn_plt_rel == NULL)
763 if (shdr_rel_plt.sh_link != dynsym_idx)
766 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
770 * Fetch the relocation section to find the idxes to the GOT
771 * and the symbols in the .dynsym they refer to.
773 reldata = elf_getdata(scn_plt_rel, NULL);
777 syms = elf_getdata(scn_dynsym, NULL);
781 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
782 if (scn_symstrs == NULL)
785 symstrs = elf_getdata(scn_symstrs, NULL);
789 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
790 plt_offset = shdr_plt.sh_offset;
792 if (shdr_rel_plt.sh_type == SHT_RELA) {
793 GElf_Rela pos_mem, *pos;
795 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
797 symidx = GELF_R_SYM(pos->r_info);
798 plt_offset += shdr_plt.sh_entsize;
799 gelf_getsym(syms, symidx, &sym);
800 snprintf(sympltname, sizeof(sympltname),
801 "%s@plt", elf_sym__name(&sym, symstrs));
803 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
808 if (filter && filter(map, f))
811 symbols__insert(&self->symbols[map->type], f);
815 } else if (shdr_rel_plt.sh_type == SHT_REL) {
816 GElf_Rel pos_mem, *pos;
817 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
819 symidx = GELF_R_SYM(pos->r_info);
820 plt_offset += shdr_plt.sh_entsize;
821 gelf_getsym(syms, symidx, &sym);
822 snprintf(sympltname, sizeof(sympltname),
823 "%s@plt", elf_sym__name(&sym, symstrs));
825 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
830 if (filter && filter(map, f))
833 symbols__insert(&self->symbols[map->type], f);
848 pr_warning("%s: problems reading %s PLT info.\n",
849 __func__, self->long_name);
853 static bool elf_sym__is_a(GElf_Sym *self, enum map_type type)
857 return elf_sym__is_function(self);
859 return elf_sym__is_object(self);
865 static bool elf_sec__is_a(GElf_Shdr *self, Elf_Data *secstrs, enum map_type type)
869 return elf_sec__is_text(self, secstrs);
871 return elf_sec__is_data(self, secstrs);
877 static int dso__load_sym(struct dso *self, struct map *map,
878 struct map_groups *mg, const char *name, int fd,
879 symbol_filter_t filter, int kernel, int kmodule)
881 struct map *curr_map = map;
882 struct dso *curr_dso = self;
883 size_t dso_name_len = strlen(self->short_name);
884 Elf_Data *symstrs, *secstrs;
892 Elf_Scn *sec, *sec_strndx;
896 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
898 pr_err("%s: cannot read %s ELF file.\n", __func__, name);
902 if (gelf_getehdr(elf, &ehdr) == NULL) {
903 pr_err("%s: cannot get elf header.\n", __func__);
907 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
909 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
914 syms = elf_getdata(sec, NULL);
918 sec = elf_getscn(elf, shdr.sh_link);
922 symstrs = elf_getdata(sec, NULL);
926 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
927 if (sec_strndx == NULL)
930 secstrs = elf_getdata(sec_strndx, NULL);
934 nr_syms = shdr.sh_size / shdr.sh_entsize;
936 memset(&sym, 0, sizeof(sym));
938 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
939 elf_section_by_name(elf, &ehdr, &shdr,
942 } else self->adjust_symbols = 0;
944 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
946 const char *elf_name;
947 char *demangled = NULL;
948 int is_label = elf_sym__is_label(&sym);
949 const char *section_name;
951 if (!is_label && !elf_sym__is_a(&sym, map->type))
954 sec = elf_getscn(elf, sym.st_shndx);
958 gelf_getshdr(sec, &shdr);
960 if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type))
963 elf_name = elf_sym__name(&sym, symstrs);
964 section_name = elf_sec__name(&shdr, secstrs);
966 if (kernel || kmodule) {
967 char dso_name[PATH_MAX];
969 if (strcmp(section_name,
970 curr_dso->short_name + dso_name_len) == 0)
973 if (strcmp(section_name, ".text") == 0) {
979 snprintf(dso_name, sizeof(dso_name),
980 "%s%s", self->short_name, section_name);
982 curr_map = map_groups__find_by_name(mg, map->type, dso_name);
983 if (curr_map == NULL) {
984 u64 start = sym.st_value;
987 start += map->start + shdr.sh_offset;
989 curr_dso = dso__new(dso_name);
990 if (curr_dso == NULL)
992 curr_map = map__new2(start, curr_dso,
994 if (curr_map == NULL) {
995 dso__delete(curr_dso);
998 curr_map->map_ip = identity__map_ip;
999 curr_map->unmap_ip = identity__map_ip;
1000 curr_dso->origin = DSO__ORIG_KERNEL;
1001 map_groups__insert(kmaps, curr_map);
1002 dsos__add(&dsos__kernel, curr_dso);
1004 curr_dso = curr_map->dso;
1009 if (curr_dso->adjust_symbols) {
1010 pr_debug2("adjusting symbol: st_value: %Lx sh_addr: "
1011 "%Lx sh_offset: %Lx\n", (u64)sym.st_value,
1012 (u64)shdr.sh_addr, (u64)shdr.sh_offset);
1013 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
1016 * We need to figure out if the object was created from C++ sources
1017 * DWARF DW_compile_unit has this, but we don't always have access
1020 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
1021 if (demangled != NULL)
1022 elf_name = demangled;
1024 f = symbol__new(sym.st_value, sym.st_size, elf_name);
1029 if (filter && filter(curr_map, f))
1032 symbols__insert(&curr_dso->symbols[curr_map->type], f);
1038 * For misannotated, zeroed, ASM function sizes.
1041 symbols__fixup_end(&self->symbols[map->type]);
1049 static bool dso__build_id_equal(const struct dso *self, u8 *build_id)
1051 return memcmp(self->build_id, build_id, sizeof(self->build_id)) == 0;
1054 static bool __dsos__read_build_ids(struct list_head *head)
1056 bool have_build_id = false;
1059 list_for_each_entry(pos, head, node)
1060 if (filename__read_build_id(pos->long_name, pos->build_id,
1061 sizeof(pos->build_id)) > 0) {
1062 have_build_id = true;
1063 pos->has_build_id = true;
1066 return have_build_id;
1069 bool dsos__read_build_ids(void)
1071 bool kbuildids = __dsos__read_build_ids(&dsos__kernel),
1072 ubuildids = __dsos__read_build_ids(&dsos__user);
1073 return kbuildids || ubuildids;
1077 * Align offset to 4 bytes as needed for note name and descriptor data.
1079 #define NOTE_ALIGN(n) (((n) + 3) & -4U)
1081 int filename__read_build_id(const char *filename, void *bf, size_t size)
1092 if (size < BUILD_ID_SIZE)
1095 fd = open(filename, O_RDONLY);
1099 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1101 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
1106 if (ek != ELF_K_ELF)
1109 if (gelf_getehdr(elf, &ehdr) == NULL) {
1110 pr_err("%s: cannot get elf header.\n", __func__);
1114 sec = elf_section_by_name(elf, &ehdr, &shdr,
1115 ".note.gnu.build-id", NULL);
1117 sec = elf_section_by_name(elf, &ehdr, &shdr,
1123 data = elf_getdata(sec, NULL);
1128 while (ptr < (data->d_buf + data->d_size)) {
1129 GElf_Nhdr *nhdr = ptr;
1130 int namesz = NOTE_ALIGN(nhdr->n_namesz),
1131 descsz = NOTE_ALIGN(nhdr->n_descsz);
1134 ptr += sizeof(*nhdr);
1137 if (nhdr->n_type == NT_GNU_BUILD_ID &&
1138 nhdr->n_namesz == sizeof("GNU")) {
1139 if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
1140 memcpy(bf, ptr, BUILD_ID_SIZE);
1141 err = BUILD_ID_SIZE;
1155 int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
1159 if (size < BUILD_ID_SIZE)
1162 fd = open(filename, O_RDONLY);
1171 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
1174 namesz = NOTE_ALIGN(nhdr.n_namesz);
1175 descsz = NOTE_ALIGN(nhdr.n_descsz);
1176 if (nhdr.n_type == NT_GNU_BUILD_ID &&
1177 nhdr.n_namesz == sizeof("GNU")) {
1178 if (read(fd, bf, namesz) != namesz)
1180 if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
1181 if (read(fd, build_id,
1182 BUILD_ID_SIZE) == BUILD_ID_SIZE) {
1186 } else if (read(fd, bf, descsz) != descsz)
1189 int n = namesz + descsz;
1190 if (read(fd, bf, n) != n)
1199 char dso__symtab_origin(const struct dso *self)
1201 static const char origin[] = {
1202 [DSO__ORIG_KERNEL] = 'k',
1203 [DSO__ORIG_JAVA_JIT] = 'j',
1204 [DSO__ORIG_FEDORA] = 'f',
1205 [DSO__ORIG_UBUNTU] = 'u',
1206 [DSO__ORIG_BUILDID] = 'b',
1207 [DSO__ORIG_DSO] = 'd',
1208 [DSO__ORIG_KMODULE] = 'K',
1211 if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
1213 return origin[self->origin];
1216 int dso__load(struct dso *self, struct map *map, symbol_filter_t filter)
1218 int size = PATH_MAX;
1220 u8 build_id[BUILD_ID_SIZE];
1224 dso__set_loaded(self, map->type);
1227 return dso__load_kernel_sym(self, map, kmaps, filter);
1229 name = malloc(size);
1233 self->adjust_symbols = 0;
1235 if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
1236 ret = dso__load_perf_map(self, map, filter);
1237 self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
1238 DSO__ORIG_NOT_FOUND;
1242 self->origin = DSO__ORIG_FEDORA - 1;
1247 switch (self->origin) {
1248 case DSO__ORIG_FEDORA:
1249 snprintf(name, size, "/usr/lib/debug%s.debug",
1252 case DSO__ORIG_UBUNTU:
1253 snprintf(name, size, "/usr/lib/debug%s",
1256 case DSO__ORIG_BUILDID:
1257 if (filename__read_build_id(self->long_name, build_id,
1258 sizeof(build_id))) {
1259 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
1261 build_id__sprintf(build_id, sizeof(build_id),
1263 snprintf(name, size,
1264 "/usr/lib/debug/.build-id/%.2s/%s.debug",
1265 build_id_hex, build_id_hex + 2);
1266 if (self->has_build_id)
1267 goto compare_build_id;
1273 snprintf(name, size, "%s", self->long_name);
1280 if (self->has_build_id) {
1281 if (filename__read_build_id(name, build_id,
1282 sizeof(build_id)) < 0)
1285 if (!dso__build_id_equal(self, build_id))
1289 fd = open(name, O_RDONLY);
1292 ret = dso__load_sym(self, map, NULL, name, fd, filter, 0, 0);
1296 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
1302 int nr_plt = dso__synthesize_plt_symbols(self, map, filter);
1308 if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
1313 struct map *map_groups__find_by_name(struct map_groups *self,
1314 enum map_type type, const char *name)
1318 for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) {
1319 struct map *map = rb_entry(nd, struct map, rb_node);
1321 if (map->dso && strcmp(map->dso->name, name) == 0)
1328 static int dsos__set_modules_path_dir(char *dirname)
1330 struct dirent *dent;
1331 DIR *dir = opendir(dirname);
1334 pr_debug("%s: cannot open %s dir\n", __func__, dirname);
1338 while ((dent = readdir(dir)) != NULL) {
1339 char path[PATH_MAX];
1341 if (dent->d_type == DT_DIR) {
1342 if (!strcmp(dent->d_name, ".") ||
1343 !strcmp(dent->d_name, ".."))
1346 snprintf(path, sizeof(path), "%s/%s",
1347 dirname, dent->d_name);
1348 if (dsos__set_modules_path_dir(path) < 0)
1351 char *dot = strrchr(dent->d_name, '.'),
1356 if (dot == NULL || strcmp(dot, ".ko"))
1358 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1359 (int)(dot - dent->d_name), dent->d_name);
1361 strxfrchar(dso_name, '-', '_');
1362 map = map_groups__find_by_name(kmaps, MAP__FUNCTION, dso_name);
1366 snprintf(path, sizeof(path), "%s/%s",
1367 dirname, dent->d_name);
1369 long_name = strdup(path);
1370 if (long_name == NULL)
1372 dso__set_long_name(map->dso, long_name);
1382 static int dsos__set_modules_path(void)
1385 char modules_path[PATH_MAX];
1387 if (uname(&uts) < 0)
1390 snprintf(modules_path, sizeof(modules_path), "/lib/modules/%s/kernel",
1393 return dsos__set_modules_path_dir(modules_path);
1397 * Constructor variant for modules (where we know from /proc/modules where
1398 * they are loaded) and for vmlinux, where only after we load all the
1399 * symbols we'll know where it starts and ends.
1401 static struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
1403 struct map *self = malloc(sizeof(*self));
1407 * ->end will be filled after we load all the symbols
1409 map__init(self, type, start, 0, 0, dso);
1415 static int map_groups__create_module_maps(struct map_groups *self)
1419 FILE *file = fopen("/proc/modules", "r");
1425 while (!feof(file)) {
1426 char name[PATH_MAX];
1432 line_len = getline(&line, &n, file);
1439 line[--line_len] = '\0'; /* \n */
1441 sep = strrchr(line, 'x');
1445 hex2u64(sep + 1, &start);
1447 sep = strchr(line, ' ');
1453 snprintf(name, sizeof(name), "[%s]", line);
1454 dso = dso__new(name);
1457 goto out_delete_line;
1459 map = map__new2(start, dso, MAP__FUNCTION);
1462 goto out_delete_line;
1465 snprintf(name, sizeof(name),
1466 "/sys/module/%s/notes/.note.gnu.build-id", line);
1467 if (sysfs__read_build_id(name, dso->build_id,
1468 sizeof(dso->build_id)) == 0)
1469 dso->has_build_id = true;
1471 dso->origin = DSO__ORIG_KMODULE;
1472 map_groups__insert(self, map);
1473 dsos__add(&dsos__kernel, dso);
1479 return dsos__set_modules_path();
1487 static int dso__load_vmlinux(struct dso *self, struct map *map,
1488 struct map_groups *mg,
1489 const char *vmlinux, symbol_filter_t filter)
1493 if (self->has_build_id) {
1494 u8 build_id[BUILD_ID_SIZE];
1496 if (filename__read_build_id(vmlinux, build_id,
1497 sizeof(build_id)) < 0) {
1498 pr_debug("No build_id in %s, ignoring it\n", vmlinux);
1501 if (!dso__build_id_equal(self, build_id)) {
1502 char expected_build_id[BUILD_ID_SIZE * 2 + 1],
1503 vmlinux_build_id[BUILD_ID_SIZE * 2 + 1];
1505 build_id__sprintf(self->build_id,
1506 sizeof(self->build_id),
1508 build_id__sprintf(build_id, sizeof(build_id),
1510 pr_debug("build_id in %s is %s while expected is %s, "
1511 "ignoring it\n", vmlinux, vmlinux_build_id,
1517 fd = open(vmlinux, O_RDONLY);
1521 dso__set_loaded(self, map->type);
1522 err = dso__load_sym(self, map, mg, self->long_name, fd, filter, 1, 0);
1528 static int dso__load_kernel_sym(struct dso *self, struct map *map,
1529 struct map_groups *mg, symbol_filter_t filter)
1534 if (vmlinux_path != NULL) {
1536 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1537 vmlinux_path__nr_entries);
1538 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
1539 err = dso__load_vmlinux(self, map, mg,
1540 vmlinux_path[i], filter);
1542 pr_debug("Using %s for symbols\n",
1544 dso__set_long_name(self,
1545 strdup(vmlinux_path[i]));
1551 is_kallsyms = self->long_name[0] == '[';
1555 err = dso__load_vmlinux(self, map, mg, self->long_name, filter);
1557 pr_info("The file %s cannot be used, "
1558 "trying to use /proc/kallsyms...", self->long_name);
1560 err = dso__load_kallsyms(self, map, mg, filter);
1561 if (err > 0 && !is_kallsyms)
1562 dso__set_long_name(self, strdup("[kernel.kallsyms]"));
1567 map__fixup_start(map);
1568 map__fixup_end(map);
1574 LIST_HEAD(dsos__user);
1575 LIST_HEAD(dsos__kernel);
1578 static void dsos__add(struct list_head *head, struct dso *dso)
1580 list_add_tail(&dso->node, head);
1583 static struct dso *dsos__find(struct list_head *head, const char *name)
1587 list_for_each_entry(pos, head, node)
1588 if (strcmp(pos->name, name) == 0)
1593 struct dso *dsos__findnew(const char *name)
1595 struct dso *dso = dsos__find(&dsos__user, name);
1598 dso = dso__new(name);
1600 dsos__add(&dsos__user, dso);
1601 dso__set_basename(dso);
1608 static void __dsos__fprintf(struct list_head *head, FILE *fp)
1612 list_for_each_entry(pos, head, node) {
1614 for (i = 0; i < MAP__NR_TYPES; ++i)
1615 dso__fprintf(pos, i, fp);
1619 void dsos__fprintf(FILE *fp)
1621 __dsos__fprintf(&dsos__kernel, fp);
1622 __dsos__fprintf(&dsos__user, fp);
1625 static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp)
1630 list_for_each_entry(pos, head, node) {
1631 ret += dso__fprintf_buildid(pos, fp);
1632 ret += fprintf(fp, " %s\n", pos->long_name);
1637 size_t dsos__fprintf_buildid(FILE *fp)
1639 return (__dsos__fprintf_buildid(&dsos__kernel, fp) +
1640 __dsos__fprintf_buildid(&dsos__user, fp));
1643 static struct dso *dsos__create_kernel( const char *vmlinux)
1645 struct dso *kernel = dso__new(vmlinux ?: "[kernel.kallsyms]");
1650 kernel->short_name = "[kernel]";
1653 vdso = dso__new("[vdso]");
1655 goto out_delete_kernel_dso;
1656 dso__set_loaded(vdso, MAP__FUNCTION);
1658 if (sysfs__read_build_id("/sys/kernel/notes", kernel->build_id,
1659 sizeof(kernel->build_id)) == 0)
1660 kernel->has_build_id = true;
1662 dsos__add(&dsos__kernel, kernel);
1663 dsos__add(&dsos__user, vdso);
1667 out_delete_kernel_dso:
1668 dso__delete(kernel);
1672 static int map_groups__create_kernel_maps(struct map_groups *self, const char *vmlinux)
1674 struct map *functions, *variables;
1675 struct dso *kernel = dsos__create_kernel(vmlinux);
1680 functions = map__new2(0, kernel, MAP__FUNCTION);
1681 if (functions == NULL)
1684 variables = map__new2(0, kernel, MAP__VARIABLE);
1685 if (variables == NULL) {
1686 map__delete(functions);
1690 functions->map_ip = functions->unmap_ip =
1691 variables->map_ip = variables->unmap_ip = identity__map_ip;
1692 map_groups__insert(self, functions);
1693 map_groups__insert(self, variables);
1698 static void vmlinux_path__exit(void)
1700 while (--vmlinux_path__nr_entries >= 0) {
1701 free(vmlinux_path[vmlinux_path__nr_entries]);
1702 vmlinux_path[vmlinux_path__nr_entries] = NULL;
1706 vmlinux_path = NULL;
1709 static int vmlinux_path__init(void)
1714 if (uname(&uts) < 0)
1717 vmlinux_path = malloc(sizeof(char *) * 5);
1718 if (vmlinux_path == NULL)
1721 vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
1722 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1724 ++vmlinux_path__nr_entries;
1725 vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
1726 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1728 ++vmlinux_path__nr_entries;
1729 snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
1730 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1731 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1733 ++vmlinux_path__nr_entries;
1734 snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
1735 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1736 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1738 ++vmlinux_path__nr_entries;
1739 snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
1741 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1742 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1744 ++vmlinux_path__nr_entries;
1749 vmlinux_path__exit();
1753 int symbol__init(struct symbol_conf *conf)
1755 const struct symbol_conf *pconf = conf ?: &symbol_conf__defaults;
1757 elf_version(EV_CURRENT);
1758 symbol__priv_size = pconf->priv_size;
1759 if (pconf->sort_by_name)
1760 symbol__priv_size += (sizeof(struct symbol_name_rb_node) -
1761 sizeof(struct symbol));
1762 map_groups__init(kmaps);
1764 if (pconf->try_vmlinux_path && vmlinux_path__init() < 0)
1767 if (map_groups__create_kernel_maps(kmaps, pconf->vmlinux_name) < 0) {
1768 vmlinux_path__exit();
1772 kmaps->use_modules = pconf->use_modules;
1773 if (pconf->use_modules && map_groups__create_module_maps(kmaps) < 0)
1774 pr_debug("Failed to load list of modules in use, "
1777 * Now that we have all the maps created, just set the ->end of them:
1779 map_groups__fixup_end(kmaps);