15 #include <sys/utsname.h>
17 #ifndef NT_GNU_BUILD_ID
18 #define NT_GNU_BUILD_ID 3
32 static void dsos__add(struct list_head *head, struct dso *dso);
33 static struct map *map__new2(u64 start, struct dso *dso, enum map_type type);
34 static int dso__load_kernel_sym(struct dso *self, struct map *map,
35 struct perf_session *session, symbol_filter_t filter);
36 static int vmlinux_path__nr_entries;
37 static char **vmlinux_path;
39 struct symbol_conf symbol_conf = {
41 .try_vmlinux_path = true,
44 bool dso__loaded(const struct dso *self, enum map_type type)
46 return self->loaded & (1 << type);
49 bool dso__sorted_by_name(const struct dso *self, enum map_type type)
51 return self->sorted_by_name & (1 << type);
54 static void dso__set_loaded(struct dso *self, enum map_type type)
56 self->loaded |= (1 << type);
59 static void dso__set_sorted_by_name(struct dso *self, enum map_type type)
61 self->sorted_by_name |= (1 << type);
64 static bool symbol_type__is_a(char symbol_type, enum map_type map_type)
68 return symbol_type == 'T' || symbol_type == 'W';
70 return symbol_type == 'D' || symbol_type == 'd';
76 static void symbols__fixup_end(struct rb_root *self)
78 struct rb_node *nd, *prevnd = rb_first(self);
79 struct symbol *curr, *prev;
84 curr = rb_entry(prevnd, struct symbol, rb_node);
86 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
88 curr = rb_entry(nd, struct symbol, rb_node);
90 if (prev->end == prev->start)
91 prev->end = curr->start - 1;
95 if (curr->end == curr->start)
96 curr->end = roundup(curr->start, 4096);
99 static void __map_groups__fixup_end(struct map_groups *self, enum map_type type)
101 struct map *prev, *curr;
102 struct rb_node *nd, *prevnd = rb_first(&self->maps[type]);
107 curr = rb_entry(prevnd, struct map, rb_node);
109 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
111 curr = rb_entry(nd, struct map, rb_node);
112 prev->end = curr->start - 1;
116 * We still haven't the actual symbols, so guess the
117 * last map final address.
122 static void map_groups__fixup_end(struct map_groups *self)
125 for (i = 0; i < MAP__NR_TYPES; ++i)
126 __map_groups__fixup_end(self, i);
129 static struct symbol *symbol__new(u64 start, u64 len, const char *name)
131 size_t namelen = strlen(name) + 1;
132 struct symbol *self = zalloc(symbol_conf.priv_size +
133 sizeof(*self) + namelen);
137 if (symbol_conf.priv_size)
138 self = ((void *)self) + symbol_conf.priv_size;
141 self->end = len ? start + len - 1 : start;
143 pr_debug3("%s: %s %#Lx-%#Lx\n", __func__, name, start, self->end);
145 memcpy(self->name, name, namelen);
150 static void symbol__delete(struct symbol *self)
152 free(((void *)self) - symbol_conf.priv_size);
155 static size_t symbol__fprintf(struct symbol *self, FILE *fp)
157 return fprintf(fp, " %llx-%llx %s\n",
158 self->start, self->end, self->name);
161 static void dso__set_long_name(struct dso *self, char *name)
165 self->long_name = name;
166 self->long_name_len = strlen(name);
169 static void dso__set_basename(struct dso *self)
171 self->short_name = basename(self->long_name);
174 struct dso *dso__new(const char *name)
176 struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
180 strcpy(self->name, name);
181 dso__set_long_name(self, self->name);
182 self->short_name = self->name;
183 for (i = 0; i < MAP__NR_TYPES; ++i)
184 self->symbols[i] = self->symbol_names[i] = RB_ROOT;
185 self->slen_calculated = 0;
186 self->origin = DSO__ORIG_NOT_FOUND;
188 self->sorted_by_name = 0;
189 self->has_build_id = 0;
195 static void symbols__delete(struct rb_root *self)
198 struct rb_node *next = rb_first(self);
201 pos = rb_entry(next, struct symbol, rb_node);
202 next = rb_next(&pos->rb_node);
203 rb_erase(&pos->rb_node, self);
208 void dso__delete(struct dso *self)
211 for (i = 0; i < MAP__NR_TYPES; ++i)
212 symbols__delete(&self->symbols[i]);
213 if (self->long_name != self->name)
214 free(self->long_name);
218 void dso__set_build_id(struct dso *self, void *build_id)
220 memcpy(self->build_id, build_id, sizeof(self->build_id));
221 self->has_build_id = 1;
224 static void symbols__insert(struct rb_root *self, struct symbol *sym)
226 struct rb_node **p = &self->rb_node;
227 struct rb_node *parent = NULL;
228 const u64 ip = sym->start;
233 s = rb_entry(parent, struct symbol, rb_node);
239 rb_link_node(&sym->rb_node, parent, p);
240 rb_insert_color(&sym->rb_node, self);
243 static struct symbol *symbols__find(struct rb_root *self, u64 ip)
253 struct symbol *s = rb_entry(n, struct symbol, rb_node);
257 else if (ip > s->end)
266 struct symbol_name_rb_node {
267 struct rb_node rb_node;
271 static void symbols__insert_by_name(struct rb_root *self, struct symbol *sym)
273 struct rb_node **p = &self->rb_node;
274 struct rb_node *parent = NULL;
275 struct symbol_name_rb_node *symn = ((void *)sym) - sizeof(*parent), *s;
279 s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
280 if (strcmp(sym->name, s->sym.name) < 0)
285 rb_link_node(&symn->rb_node, parent, p);
286 rb_insert_color(&symn->rb_node, self);
289 static void symbols__sort_by_name(struct rb_root *self, struct rb_root *source)
293 for (nd = rb_first(source); nd; nd = rb_next(nd)) {
294 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
295 symbols__insert_by_name(self, pos);
299 static struct symbol *symbols__find_by_name(struct rb_root *self, const char *name)
309 struct symbol_name_rb_node *s;
312 s = rb_entry(n, struct symbol_name_rb_node, rb_node);
313 cmp = strcmp(name, s->sym.name);
326 struct symbol *dso__find_symbol(struct dso *self,
327 enum map_type type, u64 addr)
329 return symbols__find(&self->symbols[type], addr);
332 struct symbol *dso__find_symbol_by_name(struct dso *self, enum map_type type,
335 return symbols__find_by_name(&self->symbol_names[type], name);
338 void dso__sort_by_name(struct dso *self, enum map_type type)
340 dso__set_sorted_by_name(self, type);
341 return symbols__sort_by_name(&self->symbol_names[type],
342 &self->symbols[type]);
345 int build_id__sprintf(u8 *self, int len, char *bf)
351 for (i = 0; i < len; ++i) {
352 sprintf(bid, "%02x", *raw);
360 size_t dso__fprintf_buildid(struct dso *self, FILE *fp)
362 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
364 build_id__sprintf(self->build_id, sizeof(self->build_id), sbuild_id);
365 return fprintf(fp, "%s", sbuild_id);
368 size_t dso__fprintf(struct dso *self, enum map_type type, FILE *fp)
371 size_t ret = fprintf(fp, "dso: %s (", self->short_name);
373 ret += dso__fprintf_buildid(self, fp);
374 ret += fprintf(fp, ")\n");
375 for (nd = rb_first(&self->symbols[type]); nd; nd = rb_next(nd)) {
376 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
377 ret += symbol__fprintf(pos, fp);
384 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
385 * so that we can in the next step set the symbol ->end address and then
386 * call kernel_maps__split_kallsyms.
388 static int dso__load_all_kallsyms(struct dso *self, struct map *map)
392 struct rb_root *root = &self->symbols[map->type];
393 FILE *file = fopen("/proc/kallsyms", "r");
398 while (!feof(file)) {
405 line_len = getline(&line, &n, file);
412 line[--line_len] = '\0'; /* \n */
414 len = hex2u64(line, &start);
417 if (len + 2 >= line_len)
420 symbol_type = toupper(line[len]);
421 if (!symbol_type__is_a(symbol_type, map->type))
424 symbol_name = line + len + 2;
426 * Will fix up the end later, when we have all symbols sorted.
428 sym = symbol__new(start, 0, symbol_name);
431 goto out_delete_line;
433 * We will pass the symbols to the filter later, in
434 * map__split_kallsyms, when we have split the maps per module
436 symbols__insert(root, sym);
451 * Split the symbols into maps, making sure there are no overlaps, i.e. the
452 * kernel range is broken in several maps, named [kernel].N, as we don't have
453 * the original ELF section names vmlinux have.
455 static int dso__split_kallsyms(struct dso *self, struct map *map,
456 struct perf_session *session, symbol_filter_t filter)
458 struct map *curr_map = map;
461 struct rb_root *root = &self->symbols[map->type];
462 struct rb_node *next = rb_first(root);
463 int kernel_range = 0;
468 pos = rb_entry(next, struct symbol, rb_node);
469 next = rb_next(&pos->rb_node);
471 module = strchr(pos->name, '\t');
473 if (!symbol_conf.use_modules)
478 if (strcmp(self->name, module)) {
479 curr_map = map_groups__find_by_name(&session->kmaps, map->type, module);
480 if (curr_map == NULL) {
481 pr_debug("/proc/{kallsyms,modules} "
487 * So that we look just like we get from .ko files,
488 * i.e. not prelinked, relative to map->start.
490 pos->start = curr_map->map_ip(curr_map, pos->start);
491 pos->end = curr_map->map_ip(curr_map, pos->end);
492 } else if (curr_map != map) {
493 char dso_name[PATH_MAX];
496 snprintf(dso_name, sizeof(dso_name), "[kernel].%d",
499 dso = dso__new(dso_name);
503 curr_map = map__new2(pos->start, dso, map->type);
509 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
510 map_groups__insert(&session->kmaps, curr_map);
514 if (filter && filter(curr_map, pos)) {
515 discard_symbol: rb_erase(&pos->rb_node, root);
518 if (curr_map != map) {
519 rb_erase(&pos->rb_node, root);
520 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
530 static int dso__load_kallsyms(struct dso *self, struct map *map,
531 struct perf_session *session, symbol_filter_t filter)
533 if (dso__load_all_kallsyms(self, map) < 0)
536 symbols__fixup_end(&self->symbols[map->type]);
537 self->origin = DSO__ORIG_KERNEL;
539 return dso__split_kallsyms(self, map, session, filter);
542 static int dso__load_perf_map(struct dso *self, struct map *map,
543 symbol_filter_t filter)
550 file = fopen(self->long_name, "r");
554 while (!feof(file)) {
559 line_len = getline(&line, &n, file);
566 line[--line_len] = '\0'; /* \n */
568 len = hex2u64(line, &start);
571 if (len + 2 >= line_len)
574 len += hex2u64(line + len, &size);
577 if (len + 2 >= line_len)
580 sym = symbol__new(start, size, line + len);
583 goto out_delete_line;
585 if (filter && filter(map, sym))
588 symbols__insert(&self->symbols[map->type], sym);
605 * elf_symtab__for_each_symbol - iterate thru all the symbols
607 * @self: struct elf_symtab instance to iterate
609 * @sym: GElf_Sym iterator
611 #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
612 for (idx = 0, gelf_getsym(syms, idx, &sym);\
614 idx++, gelf_getsym(syms, idx, &sym))
616 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
618 return GELF_ST_TYPE(sym->st_info);
621 static inline int elf_sym__is_function(const GElf_Sym *sym)
623 return elf_sym__type(sym) == STT_FUNC &&
625 sym->st_shndx != SHN_UNDEF;
628 static inline bool elf_sym__is_object(const GElf_Sym *sym)
630 return elf_sym__type(sym) == STT_OBJECT &&
632 sym->st_shndx != SHN_UNDEF;
635 static inline int elf_sym__is_label(const GElf_Sym *sym)
637 return elf_sym__type(sym) == STT_NOTYPE &&
639 sym->st_shndx != SHN_UNDEF &&
640 sym->st_shndx != SHN_ABS;
643 static inline const char *elf_sec__name(const GElf_Shdr *shdr,
644 const Elf_Data *secstrs)
646 return secstrs->d_buf + shdr->sh_name;
649 static inline int elf_sec__is_text(const GElf_Shdr *shdr,
650 const Elf_Data *secstrs)
652 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
655 static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
656 const Elf_Data *secstrs)
658 return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
661 static inline const char *elf_sym__name(const GElf_Sym *sym,
662 const Elf_Data *symstrs)
664 return symstrs->d_buf + sym->st_name;
667 static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
668 GElf_Shdr *shp, const char *name,
674 while ((sec = elf_nextscn(elf, sec)) != NULL) {
677 gelf_getshdr(sec, shp);
678 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
679 if (!strcmp(name, str)) {
690 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
691 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
693 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
695 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
696 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
698 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
701 * We need to check if we have a .dynsym, so that we can handle the
702 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
703 * .dynsym or .symtab).
704 * And always look at the original dso, not at debuginfo packages, that
705 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
707 static int dso__synthesize_plt_symbols(struct dso *self, struct map *map,
708 symbol_filter_t filter)
710 uint32_t nr_rel_entries, idx;
715 GElf_Shdr shdr_rel_plt, shdr_dynsym;
716 Elf_Data *reldata, *syms, *symstrs;
717 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
720 char sympltname[1024];
722 int nr = 0, symidx, fd, err = 0;
724 fd = open(self->long_name, O_RDONLY);
728 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
732 if (gelf_getehdr(elf, &ehdr) == NULL)
735 scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
736 ".dynsym", &dynsym_idx);
737 if (scn_dynsym == NULL)
740 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
742 if (scn_plt_rel == NULL) {
743 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
745 if (scn_plt_rel == NULL)
751 if (shdr_rel_plt.sh_link != dynsym_idx)
754 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
758 * Fetch the relocation section to find the idxes to the GOT
759 * and the symbols in the .dynsym they refer to.
761 reldata = elf_getdata(scn_plt_rel, NULL);
765 syms = elf_getdata(scn_dynsym, NULL);
769 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
770 if (scn_symstrs == NULL)
773 symstrs = elf_getdata(scn_symstrs, NULL);
777 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
778 plt_offset = shdr_plt.sh_offset;
780 if (shdr_rel_plt.sh_type == SHT_RELA) {
781 GElf_Rela pos_mem, *pos;
783 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
785 symidx = GELF_R_SYM(pos->r_info);
786 plt_offset += shdr_plt.sh_entsize;
787 gelf_getsym(syms, symidx, &sym);
788 snprintf(sympltname, sizeof(sympltname),
789 "%s@plt", elf_sym__name(&sym, symstrs));
791 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
796 if (filter && filter(map, f))
799 symbols__insert(&self->symbols[map->type], f);
803 } else if (shdr_rel_plt.sh_type == SHT_REL) {
804 GElf_Rel pos_mem, *pos;
805 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
807 symidx = GELF_R_SYM(pos->r_info);
808 plt_offset += shdr_plt.sh_entsize;
809 gelf_getsym(syms, symidx, &sym);
810 snprintf(sympltname, sizeof(sympltname),
811 "%s@plt", elf_sym__name(&sym, symstrs));
813 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
818 if (filter && filter(map, f))
821 symbols__insert(&self->symbols[map->type], f);
836 pr_warning("%s: problems reading %s PLT info.\n",
837 __func__, self->long_name);
841 static bool elf_sym__is_a(GElf_Sym *self, enum map_type type)
845 return elf_sym__is_function(self);
847 return elf_sym__is_object(self);
853 static bool elf_sec__is_a(GElf_Shdr *self, Elf_Data *secstrs, enum map_type type)
857 return elf_sec__is_text(self, secstrs);
859 return elf_sec__is_data(self, secstrs);
865 static int dso__load_sym(struct dso *self, struct map *map,
866 struct perf_session *session, const char *name, int fd,
867 symbol_filter_t filter, int kernel, int kmodule)
869 struct map *curr_map = map;
870 struct dso *curr_dso = self;
871 size_t dso_name_len = strlen(self->short_name);
872 Elf_Data *symstrs, *secstrs;
880 Elf_Scn *sec, *sec_strndx;
884 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
886 pr_err("%s: cannot read %s ELF file.\n", __func__, name);
890 if (gelf_getehdr(elf, &ehdr) == NULL) {
891 pr_err("%s: cannot get elf header.\n", __func__);
895 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
897 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
902 syms = elf_getdata(sec, NULL);
906 sec = elf_getscn(elf, shdr.sh_link);
910 symstrs = elf_getdata(sec, NULL);
914 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
915 if (sec_strndx == NULL)
918 secstrs = elf_getdata(sec_strndx, NULL);
922 nr_syms = shdr.sh_size / shdr.sh_entsize;
924 memset(&sym, 0, sizeof(sym));
926 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
927 elf_section_by_name(elf, &ehdr, &shdr,
930 } else self->adjust_symbols = 0;
932 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
934 const char *elf_name;
935 char *demangled = NULL;
936 int is_label = elf_sym__is_label(&sym);
937 const char *section_name;
939 if (!is_label && !elf_sym__is_a(&sym, map->type))
942 sec = elf_getscn(elf, sym.st_shndx);
946 gelf_getshdr(sec, &shdr);
948 if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type))
951 elf_name = elf_sym__name(&sym, symstrs);
952 section_name = elf_sec__name(&shdr, secstrs);
954 if (kernel || kmodule) {
955 char dso_name[PATH_MAX];
957 if (strcmp(section_name,
958 curr_dso->short_name + dso_name_len) == 0)
961 if (strcmp(section_name, ".text") == 0) {
967 snprintf(dso_name, sizeof(dso_name),
968 "%s%s", self->short_name, section_name);
970 curr_map = map_groups__find_by_name(&session->kmaps, map->type, dso_name);
971 if (curr_map == NULL) {
972 u64 start = sym.st_value;
975 start += map->start + shdr.sh_offset;
977 curr_dso = dso__new(dso_name);
978 if (curr_dso == NULL)
980 curr_map = map__new2(start, curr_dso,
982 if (curr_map == NULL) {
983 dso__delete(curr_dso);
986 curr_map->map_ip = identity__map_ip;
987 curr_map->unmap_ip = identity__map_ip;
988 curr_dso->origin = DSO__ORIG_KERNEL;
989 map_groups__insert(&session->kmaps, curr_map);
990 dsos__add(&dsos__kernel, curr_dso);
992 curr_dso = curr_map->dso;
997 if (curr_dso->adjust_symbols) {
998 pr_debug2("adjusting symbol: st_value: %Lx sh_addr: "
999 "%Lx sh_offset: %Lx\n", (u64)sym.st_value,
1000 (u64)shdr.sh_addr, (u64)shdr.sh_offset);
1001 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
1004 * We need to figure out if the object was created from C++ sources
1005 * DWARF DW_compile_unit has this, but we don't always have access
1008 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
1009 if (demangled != NULL)
1010 elf_name = demangled;
1012 f = symbol__new(sym.st_value, sym.st_size, elf_name);
1017 if (filter && filter(curr_map, f))
1020 symbols__insert(&curr_dso->symbols[curr_map->type], f);
1026 * For misannotated, zeroed, ASM function sizes.
1029 symbols__fixup_end(&self->symbols[map->type]);
1037 static bool dso__build_id_equal(const struct dso *self, u8 *build_id)
1039 return memcmp(self->build_id, build_id, sizeof(self->build_id)) == 0;
1042 static bool __dsos__read_build_ids(struct list_head *head)
1044 bool have_build_id = false;
1047 list_for_each_entry(pos, head, node)
1048 if (filename__read_build_id(pos->long_name, pos->build_id,
1049 sizeof(pos->build_id)) > 0) {
1050 have_build_id = true;
1051 pos->has_build_id = true;
1054 return have_build_id;
1057 bool dsos__read_build_ids(void)
1059 bool kbuildids = __dsos__read_build_ids(&dsos__kernel),
1060 ubuildids = __dsos__read_build_ids(&dsos__user);
1061 return kbuildids || ubuildids;
1065 * Align offset to 4 bytes as needed for note name and descriptor data.
1067 #define NOTE_ALIGN(n) (((n) + 3) & -4U)
1069 int filename__read_build_id(const char *filename, void *bf, size_t size)
1080 if (size < BUILD_ID_SIZE)
1083 fd = open(filename, O_RDONLY);
1087 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1089 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
1094 if (ek != ELF_K_ELF)
1097 if (gelf_getehdr(elf, &ehdr) == NULL) {
1098 pr_err("%s: cannot get elf header.\n", __func__);
1102 sec = elf_section_by_name(elf, &ehdr, &shdr,
1103 ".note.gnu.build-id", NULL);
1105 sec = elf_section_by_name(elf, &ehdr, &shdr,
1111 data = elf_getdata(sec, NULL);
1116 while (ptr < (data->d_buf + data->d_size)) {
1117 GElf_Nhdr *nhdr = ptr;
1118 int namesz = NOTE_ALIGN(nhdr->n_namesz),
1119 descsz = NOTE_ALIGN(nhdr->n_descsz);
1122 ptr += sizeof(*nhdr);
1125 if (nhdr->n_type == NT_GNU_BUILD_ID &&
1126 nhdr->n_namesz == sizeof("GNU")) {
1127 if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
1128 memcpy(bf, ptr, BUILD_ID_SIZE);
1129 err = BUILD_ID_SIZE;
1143 int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
1147 if (size < BUILD_ID_SIZE)
1150 fd = open(filename, O_RDONLY);
1159 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
1162 namesz = NOTE_ALIGN(nhdr.n_namesz);
1163 descsz = NOTE_ALIGN(nhdr.n_descsz);
1164 if (nhdr.n_type == NT_GNU_BUILD_ID &&
1165 nhdr.n_namesz == sizeof("GNU")) {
1166 if (read(fd, bf, namesz) != namesz)
1168 if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
1169 if (read(fd, build_id,
1170 BUILD_ID_SIZE) == BUILD_ID_SIZE) {
1174 } else if (read(fd, bf, descsz) != descsz)
1177 int n = namesz + descsz;
1178 if (read(fd, bf, n) != n)
1187 char dso__symtab_origin(const struct dso *self)
1189 static const char origin[] = {
1190 [DSO__ORIG_KERNEL] = 'k',
1191 [DSO__ORIG_JAVA_JIT] = 'j',
1192 [DSO__ORIG_FEDORA] = 'f',
1193 [DSO__ORIG_UBUNTU] = 'u',
1194 [DSO__ORIG_BUILDID] = 'b',
1195 [DSO__ORIG_DSO] = 'd',
1196 [DSO__ORIG_KMODULE] = 'K',
1199 if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
1201 return origin[self->origin];
1204 int dso__load(struct dso *self, struct map *map, struct perf_session *session,
1205 symbol_filter_t filter)
1207 int size = PATH_MAX;
1209 u8 build_id[BUILD_ID_SIZE];
1213 dso__set_loaded(self, map->type);
1216 return dso__load_kernel_sym(self, map, session, filter);
1218 name = malloc(size);
1222 self->adjust_symbols = 0;
1224 if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
1225 ret = dso__load_perf_map(self, map, filter);
1226 self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
1227 DSO__ORIG_NOT_FOUND;
1231 self->origin = DSO__ORIG_FEDORA - 1;
1236 switch (self->origin) {
1237 case DSO__ORIG_FEDORA:
1238 snprintf(name, size, "/usr/lib/debug%s.debug",
1241 case DSO__ORIG_UBUNTU:
1242 snprintf(name, size, "/usr/lib/debug%s",
1245 case DSO__ORIG_BUILDID:
1246 if (filename__read_build_id(self->long_name, build_id,
1247 sizeof(build_id))) {
1248 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
1250 build_id__sprintf(build_id, sizeof(build_id),
1252 snprintf(name, size,
1253 "/usr/lib/debug/.build-id/%.2s/%s.debug",
1254 build_id_hex, build_id_hex + 2);
1255 if (self->has_build_id)
1256 goto compare_build_id;
1262 snprintf(name, size, "%s", self->long_name);
1269 if (self->has_build_id) {
1270 if (filename__read_build_id(name, build_id,
1271 sizeof(build_id)) < 0)
1274 if (!dso__build_id_equal(self, build_id))
1278 fd = open(name, O_RDONLY);
1281 ret = dso__load_sym(self, map, NULL, name, fd, filter, 0, 0);
1285 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
1291 int nr_plt = dso__synthesize_plt_symbols(self, map, filter);
1297 if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
1302 struct map *map_groups__find_by_name(struct map_groups *self,
1303 enum map_type type, const char *name)
1307 for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) {
1308 struct map *map = rb_entry(nd, struct map, rb_node);
1310 if (map->dso && strcmp(map->dso->name, name) == 0)
1317 static int perf_session__set_modules_path_dir(struct perf_session *self, char *dirname)
1319 struct dirent *dent;
1320 DIR *dir = opendir(dirname);
1323 pr_debug("%s: cannot open %s dir\n", __func__, dirname);
1327 while ((dent = readdir(dir)) != NULL) {
1328 char path[PATH_MAX];
1330 if (dent->d_type == DT_DIR) {
1331 if (!strcmp(dent->d_name, ".") ||
1332 !strcmp(dent->d_name, ".."))
1335 snprintf(path, sizeof(path), "%s/%s",
1336 dirname, dent->d_name);
1337 if (perf_session__set_modules_path_dir(self, path) < 0)
1340 char *dot = strrchr(dent->d_name, '.'),
1345 if (dot == NULL || strcmp(dot, ".ko"))
1347 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1348 (int)(dot - dent->d_name), dent->d_name);
1350 strxfrchar(dso_name, '-', '_');
1351 map = map_groups__find_by_name(&self->kmaps, MAP__FUNCTION, dso_name);
1355 snprintf(path, sizeof(path), "%s/%s",
1356 dirname, dent->d_name);
1358 long_name = strdup(path);
1359 if (long_name == NULL)
1361 dso__set_long_name(map->dso, long_name);
1371 static int perf_session__set_modules_path(struct perf_session *self)
1374 char modules_path[PATH_MAX];
1376 if (uname(&uts) < 0)
1379 snprintf(modules_path, sizeof(modules_path), "/lib/modules/%s/kernel",
1382 return perf_session__set_modules_path_dir(self, modules_path);
1386 * Constructor variant for modules (where we know from /proc/modules where
1387 * they are loaded) and for vmlinux, where only after we load all the
1388 * symbols we'll know where it starts and ends.
1390 static struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
1392 struct map *self = malloc(sizeof(*self));
1396 * ->end will be filled after we load all the symbols
1398 map__init(self, type, start, 0, 0, dso);
1404 static int perf_session__create_module_maps(struct perf_session *self)
1408 FILE *file = fopen("/proc/modules", "r");
1414 while (!feof(file)) {
1415 char name[PATH_MAX];
1421 line_len = getline(&line, &n, file);
1428 line[--line_len] = '\0'; /* \n */
1430 sep = strrchr(line, 'x');
1434 hex2u64(sep + 1, &start);
1436 sep = strchr(line, ' ');
1442 snprintf(name, sizeof(name), "[%s]", line);
1443 dso = dso__new(name);
1446 goto out_delete_line;
1448 map = map__new2(start, dso, MAP__FUNCTION);
1451 goto out_delete_line;
1454 snprintf(name, sizeof(name),
1455 "/sys/module/%s/notes/.note.gnu.build-id", line);
1456 if (sysfs__read_build_id(name, dso->build_id,
1457 sizeof(dso->build_id)) == 0)
1458 dso->has_build_id = true;
1460 dso->origin = DSO__ORIG_KMODULE;
1461 map_groups__insert(&self->kmaps, map);
1462 dsos__add(&dsos__kernel, dso);
1468 return perf_session__set_modules_path(self);
1476 static int dso__load_vmlinux(struct dso *self, struct map *map,
1477 struct perf_session *session,
1478 const char *vmlinux, symbol_filter_t filter)
1482 if (self->has_build_id) {
1483 u8 build_id[BUILD_ID_SIZE];
1485 if (filename__read_build_id(vmlinux, build_id,
1486 sizeof(build_id)) < 0) {
1487 pr_debug("No build_id in %s, ignoring it\n", vmlinux);
1490 if (!dso__build_id_equal(self, build_id)) {
1491 char expected_build_id[BUILD_ID_SIZE * 2 + 1],
1492 vmlinux_build_id[BUILD_ID_SIZE * 2 + 1];
1494 build_id__sprintf(self->build_id,
1495 sizeof(self->build_id),
1497 build_id__sprintf(build_id, sizeof(build_id),
1499 pr_debug("build_id in %s is %s while expected is %s, "
1500 "ignoring it\n", vmlinux, vmlinux_build_id,
1506 fd = open(vmlinux, O_RDONLY);
1510 dso__set_loaded(self, map->type);
1511 err = dso__load_sym(self, map, session, self->long_name, fd, filter, 1, 0);
1517 static int dso__load_kernel_sym(struct dso *self, struct map *map,
1518 struct perf_session *session, symbol_filter_t filter)
1523 if (vmlinux_path != NULL) {
1525 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1526 vmlinux_path__nr_entries);
1527 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
1528 err = dso__load_vmlinux(self, map, session,
1529 vmlinux_path[i], filter);
1531 pr_debug("Using %s for symbols\n",
1533 dso__set_long_name(self,
1534 strdup(vmlinux_path[i]));
1540 is_kallsyms = self->long_name[0] == '[';
1544 err = dso__load_vmlinux(self, map, session, self->long_name, filter);
1546 pr_info("The file %s cannot be used, "
1547 "trying to use /proc/kallsyms...", self->long_name);
1549 err = dso__load_kallsyms(self, map, session, filter);
1550 if (err > 0 && !is_kallsyms)
1551 dso__set_long_name(self, strdup("[kernel.kallsyms]"));
1556 map__fixup_start(map);
1557 map__fixup_end(map);
1563 LIST_HEAD(dsos__user);
1564 LIST_HEAD(dsos__kernel);
1567 static void dsos__add(struct list_head *head, struct dso *dso)
1569 list_add_tail(&dso->node, head);
1572 static struct dso *dsos__find(struct list_head *head, const char *name)
1576 list_for_each_entry(pos, head, node)
1577 if (strcmp(pos->name, name) == 0)
1582 struct dso *dsos__findnew(const char *name)
1584 struct dso *dso = dsos__find(&dsos__user, name);
1587 dso = dso__new(name);
1589 dsos__add(&dsos__user, dso);
1590 dso__set_basename(dso);
1597 static void __dsos__fprintf(struct list_head *head, FILE *fp)
1601 list_for_each_entry(pos, head, node) {
1603 for (i = 0; i < MAP__NR_TYPES; ++i)
1604 dso__fprintf(pos, i, fp);
1608 void dsos__fprintf(FILE *fp)
1610 __dsos__fprintf(&dsos__kernel, fp);
1611 __dsos__fprintf(&dsos__user, fp);
1614 static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp)
1619 list_for_each_entry(pos, head, node) {
1620 ret += dso__fprintf_buildid(pos, fp);
1621 ret += fprintf(fp, " %s\n", pos->long_name);
1626 size_t dsos__fprintf_buildid(FILE *fp)
1628 return (__dsos__fprintf_buildid(&dsos__kernel, fp) +
1629 __dsos__fprintf_buildid(&dsos__user, fp));
1632 static struct dso *dsos__create_kernel( const char *vmlinux)
1634 struct dso *kernel = dso__new(vmlinux ?: "[kernel.kallsyms]");
1639 kernel->short_name = "[kernel]";
1642 vdso = dso__new("[vdso]");
1644 goto out_delete_kernel_dso;
1645 dso__set_loaded(vdso, MAP__FUNCTION);
1647 if (sysfs__read_build_id("/sys/kernel/notes", kernel->build_id,
1648 sizeof(kernel->build_id)) == 0)
1649 kernel->has_build_id = true;
1651 dsos__add(&dsos__kernel, kernel);
1652 dsos__add(&dsos__user, vdso);
1656 out_delete_kernel_dso:
1657 dso__delete(kernel);
1661 static int map_groups__create_kernel_maps(struct map_groups *self, const char *vmlinux)
1663 struct map *functions, *variables;
1664 struct dso *kernel = dsos__create_kernel(vmlinux);
1669 functions = map__new2(0, kernel, MAP__FUNCTION);
1670 if (functions == NULL)
1673 variables = map__new2(0, kernel, MAP__VARIABLE);
1674 if (variables == NULL) {
1675 map__delete(functions);
1679 functions->map_ip = functions->unmap_ip =
1680 variables->map_ip = variables->unmap_ip = identity__map_ip;
1681 map_groups__insert(self, functions);
1682 map_groups__insert(self, variables);
1687 static void vmlinux_path__exit(void)
1689 while (--vmlinux_path__nr_entries >= 0) {
1690 free(vmlinux_path[vmlinux_path__nr_entries]);
1691 vmlinux_path[vmlinux_path__nr_entries] = NULL;
1695 vmlinux_path = NULL;
1698 static int vmlinux_path__init(void)
1703 if (uname(&uts) < 0)
1706 vmlinux_path = malloc(sizeof(char *) * 5);
1707 if (vmlinux_path == NULL)
1710 vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
1711 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1713 ++vmlinux_path__nr_entries;
1714 vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
1715 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1717 ++vmlinux_path__nr_entries;
1718 snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
1719 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1720 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1722 ++vmlinux_path__nr_entries;
1723 snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
1724 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1725 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1727 ++vmlinux_path__nr_entries;
1728 snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
1730 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1731 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1733 ++vmlinux_path__nr_entries;
1738 vmlinux_path__exit();
1742 int symbol__init(void)
1744 elf_version(EV_CURRENT);
1745 if (symbol_conf.sort_by_name)
1746 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
1747 sizeof(struct symbol));
1749 if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
1755 int perf_session__create_kernel_maps(struct perf_session *self)
1757 if (map_groups__create_kernel_maps(&self->kmaps,
1758 symbol_conf.vmlinux_name) < 0)
1761 if (symbol_conf.use_modules &&
1762 perf_session__create_module_maps(self) < 0)
1763 pr_debug("Failed to load list of modules for session %s, "
1764 "continuing...\n", self->filename);
1766 * Now that we have all the maps created, just set the ->end of them:
1768 map_groups__fixup_end(&self->kmaps);