15 #include <sys/utsname.h>
17 #ifndef NT_GNU_BUILD_ID
18 #define NT_GNU_BUILD_ID 3
24 DSO__ORIG_BUILD_ID_CACHE,
33 static void dsos__add(struct list_head *head, struct dso *dso);
34 static struct map *map__new2(u64 start, struct dso *dso, enum map_type type);
35 static int dso__load_kernel_sym(struct dso *self, struct map *map,
36 symbol_filter_t filter);
37 static int vmlinux_path__nr_entries;
38 static char **vmlinux_path;
40 struct symbol_conf symbol_conf = {
41 .exclude_other = true,
43 .try_vmlinux_path = true,
46 bool dso__loaded(const struct dso *self, enum map_type type)
48 return self->loaded & (1 << type);
51 bool dso__sorted_by_name(const struct dso *self, enum map_type type)
53 return self->sorted_by_name & (1 << type);
56 static void dso__set_sorted_by_name(struct dso *self, enum map_type type)
58 self->sorted_by_name |= (1 << type);
61 bool symbol_type__is_a(char symbol_type, enum map_type map_type)
65 return symbol_type == 'T' || symbol_type == 'W';
67 return symbol_type == 'D' || symbol_type == 'd';
73 static void symbols__fixup_end(struct rb_root *self)
75 struct rb_node *nd, *prevnd = rb_first(self);
76 struct symbol *curr, *prev;
81 curr = rb_entry(prevnd, struct symbol, rb_node);
83 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
85 curr = rb_entry(nd, struct symbol, rb_node);
87 if (prev->end == prev->start)
88 prev->end = curr->start - 1;
92 if (curr->end == curr->start)
93 curr->end = roundup(curr->start, 4096);
96 static void __map_groups__fixup_end(struct map_groups *self, enum map_type type)
98 struct map *prev, *curr;
99 struct rb_node *nd, *prevnd = rb_first(&self->maps[type]);
104 curr = rb_entry(prevnd, struct map, rb_node);
106 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
108 curr = rb_entry(nd, struct map, rb_node);
109 prev->end = curr->start - 1;
113 * We still haven't the actual symbols, so guess the
114 * last map final address.
119 static void map_groups__fixup_end(struct map_groups *self)
122 for (i = 0; i < MAP__NR_TYPES; ++i)
123 __map_groups__fixup_end(self, i);
126 static struct symbol *symbol__new(u64 start, u64 len, const char *name)
128 size_t namelen = strlen(name) + 1;
129 struct symbol *self = zalloc(symbol_conf.priv_size +
130 sizeof(*self) + namelen);
134 if (symbol_conf.priv_size)
135 self = ((void *)self) + symbol_conf.priv_size;
138 self->end = len ? start + len - 1 : start;
140 pr_debug3("%s: %s %#Lx-%#Lx\n", __func__, name, start, self->end);
142 memcpy(self->name, name, namelen);
147 static void symbol__delete(struct symbol *self)
149 free(((void *)self) - symbol_conf.priv_size);
152 static size_t symbol__fprintf(struct symbol *self, FILE *fp)
154 return fprintf(fp, " %llx-%llx %s\n",
155 self->start, self->end, self->name);
158 void dso__set_long_name(struct dso *self, char *name)
162 self->long_name = name;
163 self->long_name_len = strlen(name);
166 static void dso__set_basename(struct dso *self)
168 self->short_name = basename(self->long_name);
171 struct dso *dso__new(const char *name)
173 struct dso *self = zalloc(sizeof(*self) + strlen(name) + 1);
177 strcpy(self->name, name);
178 dso__set_long_name(self, self->name);
179 self->short_name = self->name;
180 for (i = 0; i < MAP__NR_TYPES; ++i)
181 self->symbols[i] = self->symbol_names[i] = RB_ROOT;
182 self->slen_calculated = 0;
183 self->origin = DSO__ORIG_NOT_FOUND;
185 self->sorted_by_name = 0;
186 self->has_build_id = 0;
192 static void symbols__delete(struct rb_root *self)
195 struct rb_node *next = rb_first(self);
198 pos = rb_entry(next, struct symbol, rb_node);
199 next = rb_next(&pos->rb_node);
200 rb_erase(&pos->rb_node, self);
205 void dso__delete(struct dso *self)
208 for (i = 0; i < MAP__NR_TYPES; ++i)
209 symbols__delete(&self->symbols[i]);
210 if (self->long_name != self->name)
211 free(self->long_name);
215 void dso__set_build_id(struct dso *self, void *build_id)
217 memcpy(self->build_id, build_id, sizeof(self->build_id));
218 self->has_build_id = 1;
221 static void symbols__insert(struct rb_root *self, struct symbol *sym)
223 struct rb_node **p = &self->rb_node;
224 struct rb_node *parent = NULL;
225 const u64 ip = sym->start;
230 s = rb_entry(parent, struct symbol, rb_node);
236 rb_link_node(&sym->rb_node, parent, p);
237 rb_insert_color(&sym->rb_node, self);
240 static struct symbol *symbols__find(struct rb_root *self, u64 ip)
250 struct symbol *s = rb_entry(n, struct symbol, rb_node);
254 else if (ip > s->end)
263 struct symbol_name_rb_node {
264 struct rb_node rb_node;
268 static void symbols__insert_by_name(struct rb_root *self, struct symbol *sym)
270 struct rb_node **p = &self->rb_node;
271 struct rb_node *parent = NULL;
272 struct symbol_name_rb_node *symn = ((void *)sym) - sizeof(*parent), *s;
276 s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
277 if (strcmp(sym->name, s->sym.name) < 0)
282 rb_link_node(&symn->rb_node, parent, p);
283 rb_insert_color(&symn->rb_node, self);
286 static void symbols__sort_by_name(struct rb_root *self, struct rb_root *source)
290 for (nd = rb_first(source); nd; nd = rb_next(nd)) {
291 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
292 symbols__insert_by_name(self, pos);
296 static struct symbol *symbols__find_by_name(struct rb_root *self, const char *name)
306 struct symbol_name_rb_node *s;
309 s = rb_entry(n, struct symbol_name_rb_node, rb_node);
310 cmp = strcmp(name, s->sym.name);
323 struct symbol *dso__find_symbol(struct dso *self,
324 enum map_type type, u64 addr)
326 return symbols__find(&self->symbols[type], addr);
329 struct symbol *dso__find_symbol_by_name(struct dso *self, enum map_type type,
332 return symbols__find_by_name(&self->symbol_names[type], name);
335 void dso__sort_by_name(struct dso *self, enum map_type type)
337 dso__set_sorted_by_name(self, type);
338 return symbols__sort_by_name(&self->symbol_names[type],
339 &self->symbols[type]);
342 int build_id__sprintf(const u8 *self, int len, char *bf)
345 const u8 *raw = self;
348 for (i = 0; i < len; ++i) {
349 sprintf(bid, "%02x", *raw);
357 size_t dso__fprintf_buildid(struct dso *self, FILE *fp)
359 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
361 build_id__sprintf(self->build_id, sizeof(self->build_id), sbuild_id);
362 return fprintf(fp, "%s", sbuild_id);
365 size_t dso__fprintf(struct dso *self, enum map_type type, FILE *fp)
368 size_t ret = fprintf(fp, "dso: %s (", self->short_name);
370 ret += dso__fprintf_buildid(self, fp);
371 ret += fprintf(fp, ")\n");
372 for (nd = rb_first(&self->symbols[type]); nd; nd = rb_next(nd)) {
373 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
374 ret += symbol__fprintf(pos, fp);
380 int kallsyms__parse(const char *filename, void *arg,
381 int (*process_symbol)(void *arg, const char *name,
382 char type, u64 start))
387 FILE *file = fopen(filename, "r");
392 while (!feof(file)) {
398 line_len = getline(&line, &n, file);
405 line[--line_len] = '\0'; /* \n */
407 len = hex2u64(line, &start);
410 if (len + 2 >= line_len)
413 symbol_type = toupper(line[len]);
414 symbol_name = line + len + 2;
416 err = process_symbol(arg, symbol_name, symbol_type, start);
429 struct process_kallsyms_args {
434 static int map__process_kallsym_symbol(void *arg, const char *name,
435 char type, u64 start)
438 struct process_kallsyms_args *a = arg;
439 struct rb_root *root = &a->dso->symbols[a->map->type];
441 if (!symbol_type__is_a(type, a->map->type))
445 * Will fix up the end later, when we have all symbols sorted.
447 sym = symbol__new(start, 0, name);
452 * We will pass the symbols to the filter later, in
453 * map__split_kallsyms, when we have split the maps per module
455 symbols__insert(root, sym);
460 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
461 * so that we can in the next step set the symbol ->end address and then
462 * call kernel_maps__split_kallsyms.
464 static int dso__load_all_kallsyms(struct dso *self, const char *filename,
467 struct process_kallsyms_args args = { .map = map, .dso = self, };
468 return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
472 * Split the symbols into maps, making sure there are no overlaps, i.e. the
473 * kernel range is broken in several maps, named [kernel].N, as we don't have
474 * the original ELF section names vmlinux have.
476 static int dso__split_kallsyms(struct dso *self, struct map *map,
477 symbol_filter_t filter)
479 struct map_groups *kmaps = map__kmap(map)->kmaps;
480 struct map *curr_map = map;
483 struct rb_root *root = &self->symbols[map->type];
484 struct rb_node *next = rb_first(root);
485 int kernel_range = 0;
490 pos = rb_entry(next, struct symbol, rb_node);
491 next = rb_next(&pos->rb_node);
493 module = strchr(pos->name, '\t');
495 if (!symbol_conf.use_modules)
500 if (strcmp(curr_map->dso->short_name, module)) {
501 curr_map = map_groups__find_by_name(kmaps, map->type, module);
502 if (curr_map == NULL) {
503 pr_debug("/proc/{kallsyms,modules} "
504 "inconsistency while looking "
505 "for \"%s\" module!\n", module);
509 if (curr_map->dso->loaded)
513 * So that we look just like we get from .ko files,
514 * i.e. not prelinked, relative to map->start.
516 pos->start = curr_map->map_ip(curr_map, pos->start);
517 pos->end = curr_map->map_ip(curr_map, pos->end);
518 } else if (curr_map != map) {
519 char dso_name[PATH_MAX];
522 snprintf(dso_name, sizeof(dso_name), "[kernel].%d",
525 dso = dso__new(dso_name);
529 curr_map = map__new2(pos->start, dso, map->type);
535 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
536 map_groups__insert(kmaps, curr_map);
540 if (filter && filter(curr_map, pos)) {
541 discard_symbol: rb_erase(&pos->rb_node, root);
544 if (curr_map != map) {
545 rb_erase(&pos->rb_node, root);
546 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
555 int dso__load_kallsyms(struct dso *self, const char *filename,
556 struct map *map, symbol_filter_t filter)
558 if (dso__load_all_kallsyms(self, filename, map) < 0)
561 symbols__fixup_end(&self->symbols[map->type]);
562 self->origin = DSO__ORIG_KERNEL;
564 return dso__split_kallsyms(self, map, filter);
567 static int dso__load_perf_map(struct dso *self, struct map *map,
568 symbol_filter_t filter)
575 file = fopen(self->long_name, "r");
579 while (!feof(file)) {
584 line_len = getline(&line, &n, file);
591 line[--line_len] = '\0'; /* \n */
593 len = hex2u64(line, &start);
596 if (len + 2 >= line_len)
599 len += hex2u64(line + len, &size);
602 if (len + 2 >= line_len)
605 sym = symbol__new(start, size, line + len);
608 goto out_delete_line;
610 if (filter && filter(map, sym))
613 symbols__insert(&self->symbols[map->type], sym);
630 * elf_symtab__for_each_symbol - iterate thru all the symbols
632 * @self: struct elf_symtab instance to iterate
634 * @sym: GElf_Sym iterator
636 #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
637 for (idx = 0, gelf_getsym(syms, idx, &sym);\
639 idx++, gelf_getsym(syms, idx, &sym))
641 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
643 return GELF_ST_TYPE(sym->st_info);
646 static inline int elf_sym__is_function(const GElf_Sym *sym)
648 return elf_sym__type(sym) == STT_FUNC &&
650 sym->st_shndx != SHN_UNDEF;
653 static inline bool elf_sym__is_object(const GElf_Sym *sym)
655 return elf_sym__type(sym) == STT_OBJECT &&
657 sym->st_shndx != SHN_UNDEF;
660 static inline int elf_sym__is_label(const GElf_Sym *sym)
662 return elf_sym__type(sym) == STT_NOTYPE &&
664 sym->st_shndx != SHN_UNDEF &&
665 sym->st_shndx != SHN_ABS;
668 static inline const char *elf_sec__name(const GElf_Shdr *shdr,
669 const Elf_Data *secstrs)
671 return secstrs->d_buf + shdr->sh_name;
674 static inline int elf_sec__is_text(const GElf_Shdr *shdr,
675 const Elf_Data *secstrs)
677 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
680 static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
681 const Elf_Data *secstrs)
683 return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
686 static inline const char *elf_sym__name(const GElf_Sym *sym,
687 const Elf_Data *symstrs)
689 return symstrs->d_buf + sym->st_name;
692 static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
693 GElf_Shdr *shp, const char *name,
699 while ((sec = elf_nextscn(elf, sec)) != NULL) {
702 gelf_getshdr(sec, shp);
703 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
704 if (!strcmp(name, str)) {
715 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
716 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
718 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
720 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
721 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
723 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
726 * We need to check if we have a .dynsym, so that we can handle the
727 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
728 * .dynsym or .symtab).
729 * And always look at the original dso, not at debuginfo packages, that
730 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
732 static int dso__synthesize_plt_symbols(struct dso *self, struct map *map,
733 symbol_filter_t filter)
735 uint32_t nr_rel_entries, idx;
740 GElf_Shdr shdr_rel_plt, shdr_dynsym;
741 Elf_Data *reldata, *syms, *symstrs;
742 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
745 char sympltname[1024];
747 int nr = 0, symidx, fd, err = 0;
749 fd = open(self->long_name, O_RDONLY);
753 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
757 if (gelf_getehdr(elf, &ehdr) == NULL)
760 scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
761 ".dynsym", &dynsym_idx);
762 if (scn_dynsym == NULL)
765 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
767 if (scn_plt_rel == NULL) {
768 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
770 if (scn_plt_rel == NULL)
776 if (shdr_rel_plt.sh_link != dynsym_idx)
779 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
783 * Fetch the relocation section to find the idxes to the GOT
784 * and the symbols in the .dynsym they refer to.
786 reldata = elf_getdata(scn_plt_rel, NULL);
790 syms = elf_getdata(scn_dynsym, NULL);
794 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
795 if (scn_symstrs == NULL)
798 symstrs = elf_getdata(scn_symstrs, NULL);
802 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
803 plt_offset = shdr_plt.sh_offset;
805 if (shdr_rel_plt.sh_type == SHT_RELA) {
806 GElf_Rela pos_mem, *pos;
808 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
810 symidx = GELF_R_SYM(pos->r_info);
811 plt_offset += shdr_plt.sh_entsize;
812 gelf_getsym(syms, symidx, &sym);
813 snprintf(sympltname, sizeof(sympltname),
814 "%s@plt", elf_sym__name(&sym, symstrs));
816 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
821 if (filter && filter(map, f))
824 symbols__insert(&self->symbols[map->type], f);
828 } else if (shdr_rel_plt.sh_type == SHT_REL) {
829 GElf_Rel pos_mem, *pos;
830 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
832 symidx = GELF_R_SYM(pos->r_info);
833 plt_offset += shdr_plt.sh_entsize;
834 gelf_getsym(syms, symidx, &sym);
835 snprintf(sympltname, sizeof(sympltname),
836 "%s@plt", elf_sym__name(&sym, symstrs));
838 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
843 if (filter && filter(map, f))
846 symbols__insert(&self->symbols[map->type], f);
861 pr_warning("%s: problems reading %s PLT info.\n",
862 __func__, self->long_name);
866 static bool elf_sym__is_a(GElf_Sym *self, enum map_type type)
870 return elf_sym__is_function(self);
872 return elf_sym__is_object(self);
878 static bool elf_sec__is_a(GElf_Shdr *self, Elf_Data *secstrs, enum map_type type)
882 return elf_sec__is_text(self, secstrs);
884 return elf_sec__is_data(self, secstrs);
890 static int dso__load_sym(struct dso *self, struct map *map, const char *name,
891 int fd, symbol_filter_t filter, int kmodule)
893 struct kmap *kmap = self->kernel ? map__kmap(map) : NULL;
894 struct map *curr_map = map;
895 struct dso *curr_dso = self;
896 size_t dso_name_len = strlen(self->short_name);
897 Elf_Data *symstrs, *secstrs;
905 Elf_Scn *sec, *sec_strndx;
909 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
911 pr_err("%s: cannot read %s ELF file.\n", __func__, name);
915 if (gelf_getehdr(elf, &ehdr) == NULL) {
916 pr_err("%s: cannot get elf header.\n", __func__);
920 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
922 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
927 syms = elf_getdata(sec, NULL);
931 sec = elf_getscn(elf, shdr.sh_link);
935 symstrs = elf_getdata(sec, NULL);
939 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
940 if (sec_strndx == NULL)
943 secstrs = elf_getdata(sec_strndx, NULL);
947 nr_syms = shdr.sh_size / shdr.sh_entsize;
949 memset(&sym, 0, sizeof(sym));
951 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
952 elf_section_by_name(elf, &ehdr, &shdr,
955 } else self->adjust_symbols = 0;
957 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
959 const char *elf_name = elf_sym__name(&sym, symstrs);
960 char *demangled = NULL;
961 int is_label = elf_sym__is_label(&sym);
962 const char *section_name;
964 if (kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name &&
965 strcmp(elf_name, kmap->ref_reloc_sym->name) == 0)
966 kmap->ref_reloc_sym->unrelocated_addr = sym.st_value;
968 if (!is_label && !elf_sym__is_a(&sym, map->type))
971 sec = elf_getscn(elf, sym.st_shndx);
975 gelf_getshdr(sec, &shdr);
977 if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type))
980 section_name = elf_sec__name(&shdr, secstrs);
982 if (self->kernel || kmodule) {
983 char dso_name[PATH_MAX];
985 if (strcmp(section_name,
986 curr_dso->short_name + dso_name_len) == 0)
989 if (strcmp(section_name, ".text") == 0) {
995 snprintf(dso_name, sizeof(dso_name),
996 "%s%s", self->short_name, section_name);
998 curr_map = map_groups__find_by_name(kmap->kmaps, map->type, dso_name);
999 if (curr_map == NULL) {
1000 u64 start = sym.st_value;
1003 start += map->start + shdr.sh_offset;
1005 curr_dso = dso__new(dso_name);
1006 if (curr_dso == NULL)
1008 curr_map = map__new2(start, curr_dso,
1010 if (curr_map == NULL) {
1011 dso__delete(curr_dso);
1014 curr_map->map_ip = identity__map_ip;
1015 curr_map->unmap_ip = identity__map_ip;
1016 curr_dso->origin = DSO__ORIG_KERNEL;
1017 map_groups__insert(kmap->kmaps, curr_map);
1018 dsos__add(&dsos__kernel, curr_dso);
1019 dso__set_loaded(curr_dso, map->type);
1021 curr_dso = curr_map->dso;
1026 if (curr_dso->adjust_symbols) {
1027 pr_debug2("adjusting symbol: st_value: %Lx sh_addr: "
1028 "%Lx sh_offset: %Lx\n", (u64)sym.st_value,
1029 (u64)shdr.sh_addr, (u64)shdr.sh_offset);
1030 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
1033 * We need to figure out if the object was created from C++ sources
1034 * DWARF DW_compile_unit has this, but we don't always have access
1037 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
1038 if (demangled != NULL)
1039 elf_name = demangled;
1041 f = symbol__new(sym.st_value, sym.st_size, elf_name);
1046 if (filter && filter(curr_map, f))
1049 symbols__insert(&curr_dso->symbols[curr_map->type], f);
1055 * For misannotated, zeroed, ASM function sizes.
1058 symbols__fixup_end(&self->symbols[map->type]);
1061 * We need to fixup this here too because we create new
1062 * maps here, for things like vsyscall sections.
1064 __map_groups__fixup_end(kmap->kmaps, map->type);
1074 static bool dso__build_id_equal(const struct dso *self, u8 *build_id)
1076 return memcmp(self->build_id, build_id, sizeof(self->build_id)) == 0;
1079 static bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
1081 bool have_build_id = false;
1084 list_for_each_entry(pos, head, node) {
1085 if (with_hits && !pos->hit)
1087 if (filename__read_build_id(pos->long_name, pos->build_id,
1088 sizeof(pos->build_id)) > 0) {
1089 have_build_id = true;
1090 pos->has_build_id = true;
1094 return have_build_id;
1097 bool dsos__read_build_ids(bool with_hits)
1099 bool kbuildids = __dsos__read_build_ids(&dsos__kernel, with_hits),
1100 ubuildids = __dsos__read_build_ids(&dsos__user, with_hits);
1101 return kbuildids || ubuildids;
1105 * Align offset to 4 bytes as needed for note name and descriptor data.
1107 #define NOTE_ALIGN(n) (((n) + 3) & -4U)
1109 int filename__read_build_id(const char *filename, void *bf, size_t size)
1120 if (size < BUILD_ID_SIZE)
1123 fd = open(filename, O_RDONLY);
1127 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1129 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
1134 if (ek != ELF_K_ELF)
1137 if (gelf_getehdr(elf, &ehdr) == NULL) {
1138 pr_err("%s: cannot get elf header.\n", __func__);
1142 sec = elf_section_by_name(elf, &ehdr, &shdr,
1143 ".note.gnu.build-id", NULL);
1145 sec = elf_section_by_name(elf, &ehdr, &shdr,
1151 data = elf_getdata(sec, NULL);
1156 while (ptr < (data->d_buf + data->d_size)) {
1157 GElf_Nhdr *nhdr = ptr;
1158 int namesz = NOTE_ALIGN(nhdr->n_namesz),
1159 descsz = NOTE_ALIGN(nhdr->n_descsz);
1162 ptr += sizeof(*nhdr);
1165 if (nhdr->n_type == NT_GNU_BUILD_ID &&
1166 nhdr->n_namesz == sizeof("GNU")) {
1167 if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
1168 memcpy(bf, ptr, BUILD_ID_SIZE);
1169 err = BUILD_ID_SIZE;
1183 int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
1187 if (size < BUILD_ID_SIZE)
1190 fd = open(filename, O_RDONLY);
1199 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
1202 namesz = NOTE_ALIGN(nhdr.n_namesz);
1203 descsz = NOTE_ALIGN(nhdr.n_descsz);
1204 if (nhdr.n_type == NT_GNU_BUILD_ID &&
1205 nhdr.n_namesz == sizeof("GNU")) {
1206 if (read(fd, bf, namesz) != namesz)
1208 if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
1209 if (read(fd, build_id,
1210 BUILD_ID_SIZE) == BUILD_ID_SIZE) {
1214 } else if (read(fd, bf, descsz) != descsz)
1217 int n = namesz + descsz;
1218 if (read(fd, bf, n) != n)
1227 char dso__symtab_origin(const struct dso *self)
1229 static const char origin[] = {
1230 [DSO__ORIG_KERNEL] = 'k',
1231 [DSO__ORIG_JAVA_JIT] = 'j',
1232 [DSO__ORIG_BUILD_ID_CACHE] = 'B',
1233 [DSO__ORIG_FEDORA] = 'f',
1234 [DSO__ORIG_UBUNTU] = 'u',
1235 [DSO__ORIG_BUILDID] = 'b',
1236 [DSO__ORIG_DSO] = 'd',
1237 [DSO__ORIG_KMODULE] = 'K',
1240 if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
1242 return origin[self->origin];
1245 int dso__load(struct dso *self, struct map *map, symbol_filter_t filter)
1247 int size = PATH_MAX;
1249 u8 build_id[BUILD_ID_SIZE];
1250 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
1254 dso__set_loaded(self, map->type);
1257 return dso__load_kernel_sym(self, map, filter);
1259 name = malloc(size);
1263 self->adjust_symbols = 0;
1265 if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
1266 ret = dso__load_perf_map(self, map, filter);
1267 self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
1268 DSO__ORIG_NOT_FOUND;
1272 self->origin = DSO__ORIG_BUILD_ID_CACHE;
1274 if (self->has_build_id) {
1275 build_id__sprintf(self->build_id, sizeof(self->build_id),
1277 snprintf(name, size, "%s/%s/.build-id/%.2s/%s",
1278 getenv("HOME"), DEBUG_CACHE_DIR,
1279 build_id_hex, build_id_hex + 2);
1285 switch (self->origin) {
1286 case DSO__ORIG_FEDORA:
1287 snprintf(name, size, "/usr/lib/debug%s.debug",
1290 case DSO__ORIG_UBUNTU:
1291 snprintf(name, size, "/usr/lib/debug%s",
1294 case DSO__ORIG_BUILDID:
1295 if (filename__read_build_id(self->long_name, build_id,
1296 sizeof(build_id))) {
1297 build_id__sprintf(build_id, sizeof(build_id),
1299 snprintf(name, size,
1300 "/usr/lib/debug/.build-id/%.2s/%s.debug",
1301 build_id_hex, build_id_hex + 2);
1302 if (self->has_build_id)
1303 goto compare_build_id;
1309 snprintf(name, size, "%s", self->long_name);
1316 if (self->has_build_id) {
1317 if (filename__read_build_id(name, build_id,
1318 sizeof(build_id)) < 0)
1321 if (!dso__build_id_equal(self, build_id))
1325 fd = open(name, O_RDONLY);
1328 ret = dso__load_sym(self, map, name, fd, filter, 0);
1332 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
1338 int nr_plt = dso__synthesize_plt_symbols(self, map, filter);
1344 if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
1349 struct map *map_groups__find_by_name(struct map_groups *self,
1350 enum map_type type, const char *name)
1354 for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) {
1355 struct map *map = rb_entry(nd, struct map, rb_node);
1357 if (map->dso && strcmp(map->dso->short_name, name) == 0)
1364 static int dso__kernel_module_get_build_id(struct dso *self)
1366 char filename[PATH_MAX];
1368 * kernel module short names are of the form "[module]" and
1369 * we need just "module" here.
1371 const char *name = self->short_name + 1;
1373 snprintf(filename, sizeof(filename),
1374 "/sys/module/%.*s/notes/.note.gnu.build-id",
1375 (int)strlen(name - 1), name);
1377 if (sysfs__read_build_id(filename, self->build_id,
1378 sizeof(self->build_id)) == 0)
1379 self->has_build_id = true;
1384 static int map_groups__set_modules_path_dir(struct map_groups *self, char *dirname)
1386 struct dirent *dent;
1387 DIR *dir = opendir(dirname);
1390 pr_debug("%s: cannot open %s dir\n", __func__, dirname);
1394 while ((dent = readdir(dir)) != NULL) {
1395 char path[PATH_MAX];
1397 if (dent->d_type == DT_DIR) {
1398 if (!strcmp(dent->d_name, ".") ||
1399 !strcmp(dent->d_name, ".."))
1402 snprintf(path, sizeof(path), "%s/%s",
1403 dirname, dent->d_name);
1404 if (map_groups__set_modules_path_dir(self, path) < 0)
1407 char *dot = strrchr(dent->d_name, '.'),
1412 if (dot == NULL || strcmp(dot, ".ko"))
1414 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1415 (int)(dot - dent->d_name), dent->d_name);
1417 strxfrchar(dso_name, '-', '_');
1418 map = map_groups__find_by_name(self, MAP__FUNCTION, dso_name);
1422 snprintf(path, sizeof(path), "%s/%s",
1423 dirname, dent->d_name);
1425 long_name = strdup(path);
1426 if (long_name == NULL)
1428 dso__set_long_name(map->dso, long_name);
1429 dso__kernel_module_get_build_id(map->dso);
1439 static int map_groups__set_modules_path(struct map_groups *self)
1442 char modules_path[PATH_MAX];
1444 if (uname(&uts) < 0)
1447 snprintf(modules_path, sizeof(modules_path), "/lib/modules/%s/kernel",
1450 return map_groups__set_modules_path_dir(self, modules_path);
1454 * Constructor variant for modules (where we know from /proc/modules where
1455 * they are loaded) and for vmlinux, where only after we load all the
1456 * symbols we'll know where it starts and ends.
1458 static struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
1460 struct map *self = zalloc(sizeof(*self) +
1461 (dso->kernel ? sizeof(struct kmap) : 0));
1464 * ->end will be filled after we load all the symbols
1466 map__init(self, type, start, 0, 0, dso);
1472 struct map *map_groups__new_module(struct map_groups *self, u64 start,
1473 const char *filename)
1476 struct dso *dso = __dsos__findnew(&dsos__kernel, filename);
1481 map = map__new2(start, dso, MAP__FUNCTION);
1485 dso->origin = DSO__ORIG_KMODULE;
1486 map_groups__insert(self, map);
1490 static int map_groups__create_modules(struct map_groups *self)
1494 FILE *file = fopen("/proc/modules", "r");
1500 while (!feof(file)) {
1501 char name[PATH_MAX];
1506 line_len = getline(&line, &n, file);
1513 line[--line_len] = '\0'; /* \n */
1515 sep = strrchr(line, 'x');
1519 hex2u64(sep + 1, &start);
1521 sep = strchr(line, ' ');
1527 snprintf(name, sizeof(name), "[%s]", line);
1528 map = map_groups__new_module(self, start, name);
1530 goto out_delete_line;
1531 dso__kernel_module_get_build_id(map->dso);
1537 return map_groups__set_modules_path(self);
1545 static int dso__load_vmlinux(struct dso *self, struct map *map,
1546 const char *vmlinux, symbol_filter_t filter)
1550 if (self->has_build_id) {
1551 u8 build_id[BUILD_ID_SIZE];
1553 if (filename__read_build_id(vmlinux, build_id,
1554 sizeof(build_id)) < 0) {
1555 pr_debug("No build_id in %s, ignoring it\n", vmlinux);
1558 if (!dso__build_id_equal(self, build_id)) {
1559 char expected_build_id[BUILD_ID_SIZE * 2 + 1],
1560 vmlinux_build_id[BUILD_ID_SIZE * 2 + 1];
1562 build_id__sprintf(self->build_id,
1563 sizeof(self->build_id),
1565 build_id__sprintf(build_id, sizeof(build_id),
1567 pr_debug("build_id in %s is %s while expected is %s, "
1568 "ignoring it\n", vmlinux, vmlinux_build_id,
1574 fd = open(vmlinux, O_RDONLY);
1578 dso__set_loaded(self, map->type);
1579 err = dso__load_sym(self, map, vmlinux, fd, filter, 0);
1585 int dso__load_vmlinux_path(struct dso *self, struct map *map,
1586 symbol_filter_t filter)
1590 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1591 vmlinux_path__nr_entries);
1593 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
1594 err = dso__load_vmlinux(self, map, vmlinux_path[i], filter);
1596 pr_debug("Using %s for symbols\n", vmlinux_path[i]);
1597 dso__set_long_name(self, strdup(vmlinux_path[i]));
1605 static int dso__load_kernel_sym(struct dso *self, struct map *map,
1606 symbol_filter_t filter)
1609 const char *kallsyms_filename = NULL;
1610 char *kallsyms_allocated_filename = NULL;
1612 * Step 1: if the user specified a vmlinux filename, use it and only
1613 * it, reporting errors to the user if it cannot be used.
1615 * For instance, try to analyse an ARM perf.data file _without_ a
1616 * build-id, or if the user specifies the wrong path to the right
1617 * vmlinux file, obviously we can't fallback to another vmlinux (a
1618 * x86_86 one, on the machine where analysis is being performed, say),
1619 * or worse, /proc/kallsyms.
1621 * If the specified file _has_ a build-id and there is a build-id
1622 * section in the perf.data file, we will still do the expected
1623 * validation in dso__load_vmlinux and will bail out if they don't
1626 if (symbol_conf.vmlinux_name != NULL) {
1627 err = dso__load_vmlinux(self, map,
1628 symbol_conf.vmlinux_name, filter);
1632 if (vmlinux_path != NULL) {
1633 err = dso__load_vmlinux_path(self, map, filter);
1639 * Say the kernel DSO was created when processing the build-id header table,
1640 * we have a build-id, so check if it is the same as the running kernel,
1641 * using it if it is.
1643 if (self->has_build_id) {
1644 u8 kallsyms_build_id[BUILD_ID_SIZE];
1645 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1647 if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id,
1648 sizeof(kallsyms_build_id)) == 0) {
1649 if (dso__build_id_equal(self, kallsyms_build_id)) {
1650 kallsyms_filename = "/proc/kallsyms";
1655 * Now look if we have it on the build-id cache in
1656 * $HOME/.debug/[kernel.kallsyms].
1658 build_id__sprintf(self->build_id, sizeof(self->build_id),
1661 if (asprintf(&kallsyms_allocated_filename,
1662 "%s/.debug/[kernel.kallsyms]/%s",
1663 getenv("HOME"), sbuild_id) == -1)
1666 kallsyms_filename = kallsyms_allocated_filename;
1668 if (access(kallsyms_filename, F_OK)) {
1669 free(kallsyms_allocated_filename);
1674 * Last resort, if we don't have a build-id and couldn't find
1675 * any vmlinux file, try the running kernel kallsyms table.
1677 kallsyms_filename = "/proc/kallsyms";
1681 err = dso__load_kallsyms(self, kallsyms_filename, map, filter);
1682 free(kallsyms_allocated_filename);
1687 if (kallsyms_filename != NULL)
1688 dso__set_long_name(self, strdup("[kernel.kallsyms]"));
1689 map__fixup_start(map);
1690 map__fixup_end(map);
1696 LIST_HEAD(dsos__user);
1697 LIST_HEAD(dsos__kernel);
1699 static void dsos__add(struct list_head *head, struct dso *dso)
1701 list_add_tail(&dso->node, head);
1704 static struct dso *dsos__find(struct list_head *head, const char *name)
1708 list_for_each_entry(pos, head, node)
1709 if (strcmp(pos->long_name, name) == 0)
1714 struct dso *__dsos__findnew(struct list_head *head, const char *name)
1716 struct dso *dso = dsos__find(head, name);
1719 dso = dso__new(name);
1721 dsos__add(head, dso);
1722 dso__set_basename(dso);
1729 static void __dsos__fprintf(struct list_head *head, FILE *fp)
1733 list_for_each_entry(pos, head, node) {
1735 for (i = 0; i < MAP__NR_TYPES; ++i)
1736 dso__fprintf(pos, i, fp);
1740 void dsos__fprintf(FILE *fp)
1742 __dsos__fprintf(&dsos__kernel, fp);
1743 __dsos__fprintf(&dsos__user, fp);
1746 static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
1752 list_for_each_entry(pos, head, node) {
1753 if (with_hits && !pos->hit)
1755 ret += dso__fprintf_buildid(pos, fp);
1756 ret += fprintf(fp, " %s\n", pos->long_name);
1761 size_t dsos__fprintf_buildid(FILE *fp, bool with_hits)
1763 return (__dsos__fprintf_buildid(&dsos__kernel, fp, with_hits) +
1764 __dsos__fprintf_buildid(&dsos__user, fp, with_hits));
1767 struct dso *dso__new_kernel(const char *name)
1769 struct dso *self = dso__new(name ?: "[kernel.kallsyms]");
1772 self->short_name = "[kernel]";
1779 void dso__read_running_kernel_build_id(struct dso *self)
1781 if (sysfs__read_build_id("/sys/kernel/notes", self->build_id,
1782 sizeof(self->build_id)) == 0)
1783 self->has_build_id = true;
1786 static struct dso *dsos__create_kernel(const char *vmlinux)
1788 struct dso *kernel = dso__new_kernel(vmlinux);
1790 if (kernel != NULL) {
1791 dso__read_running_kernel_build_id(kernel);
1792 dsos__add(&dsos__kernel, kernel);
1798 int __map_groups__create_kernel_maps(struct map_groups *self,
1799 struct map *vmlinux_maps[MAP__NR_TYPES],
1804 for (type = 0; type < MAP__NR_TYPES; ++type) {
1807 vmlinux_maps[type] = map__new2(0, kernel, type);
1808 if (vmlinux_maps[type] == NULL)
1811 vmlinux_maps[type]->map_ip =
1812 vmlinux_maps[type]->unmap_ip = identity__map_ip;
1814 kmap = map__kmap(vmlinux_maps[type]);
1816 map_groups__insert(self, vmlinux_maps[type]);
1822 static void vmlinux_path__exit(void)
1824 while (--vmlinux_path__nr_entries >= 0) {
1825 free(vmlinux_path[vmlinux_path__nr_entries]);
1826 vmlinux_path[vmlinux_path__nr_entries] = NULL;
1830 vmlinux_path = NULL;
1833 static int vmlinux_path__init(void)
1838 if (uname(&uts) < 0)
1841 vmlinux_path = malloc(sizeof(char *) * 5);
1842 if (vmlinux_path == NULL)
1845 vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
1846 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1848 ++vmlinux_path__nr_entries;
1849 vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
1850 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1852 ++vmlinux_path__nr_entries;
1853 snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
1854 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1855 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1857 ++vmlinux_path__nr_entries;
1858 snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
1859 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1860 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1862 ++vmlinux_path__nr_entries;
1863 snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
1865 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1866 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1868 ++vmlinux_path__nr_entries;
1873 vmlinux_path__exit();
1877 static int setup_list(struct strlist **list, const char *list_str,
1878 const char *list_name)
1880 if (list_str == NULL)
1883 *list = strlist__new(true, list_str);
1885 pr_err("problems parsing %s list\n", list_name);
1891 int symbol__init(void)
1893 elf_version(EV_CURRENT);
1894 if (symbol_conf.sort_by_name)
1895 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
1896 sizeof(struct symbol));
1898 if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
1901 if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
1902 pr_err("'.' is the only non valid --field-separator argument\n");
1906 if (setup_list(&symbol_conf.dso_list,
1907 symbol_conf.dso_list_str, "dso") < 0)
1910 if (setup_list(&symbol_conf.comm_list,
1911 symbol_conf.comm_list_str, "comm") < 0)
1912 goto out_free_dso_list;
1914 if (setup_list(&symbol_conf.sym_list,
1915 symbol_conf.sym_list_str, "symbol") < 0)
1916 goto out_free_comm_list;
1921 strlist__delete(symbol_conf.dso_list);
1923 strlist__delete(symbol_conf.comm_list);
1927 int map_groups__create_kernel_maps(struct map_groups *self,
1928 struct map *vmlinux_maps[MAP__NR_TYPES])
1930 struct dso *kernel = dsos__create_kernel(symbol_conf.vmlinux_name);
1935 if (__map_groups__create_kernel_maps(self, vmlinux_maps, kernel) < 0)
1938 if (symbol_conf.use_modules && map_groups__create_modules(self) < 0)
1941 * Now that we have all the maps created, just set the ->end of them:
1943 map_groups__fixup_end(self);