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 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 bool dso__loaded(const struct dso *self, enum map_type type)
47 return self->loaded & (1 << type);
50 bool dso__sorted_by_name(const struct dso *self, enum map_type type)
52 return self->sorted_by_name & (1 << type);
55 static void dso__set_loaded(struct dso *self, enum map_type type)
57 self->loaded |= (1 << type);
60 static void dso__set_sorted_by_name(struct dso *self, enum map_type type)
62 self->sorted_by_name |= (1 << type);
65 static bool symbol_type__is_a(char symbol_type, enum map_type map_type)
69 return symbol_type == 'T' || symbol_type == 'W';
71 return symbol_type == 'D' || symbol_type == 'd';
77 static void symbols__fixup_end(struct rb_root *self)
79 struct rb_node *nd, *prevnd = rb_first(self);
80 struct symbol *curr, *prev;
85 curr = rb_entry(prevnd, struct symbol, rb_node);
87 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
89 curr = rb_entry(nd, struct symbol, rb_node);
91 if (prev->end == prev->start)
92 prev->end = curr->start - 1;
96 if (curr->end == curr->start)
97 curr->end = roundup(curr->start, 4096);
100 static void __map_groups__fixup_end(struct map_groups *self, enum map_type type)
102 struct map *prev, *curr;
103 struct rb_node *nd, *prevnd = rb_first(&self->maps[type]);
108 curr = rb_entry(prevnd, struct map, rb_node);
110 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
112 curr = rb_entry(nd, struct map, rb_node);
113 prev->end = curr->start - 1;
117 * We still haven't the actual symbols, so guess the
118 * last map final address.
123 static void map_groups__fixup_end(struct map_groups *self)
126 for (i = 0; i < MAP__NR_TYPES; ++i)
127 __map_groups__fixup_end(self, i);
130 static struct symbol *symbol__new(u64 start, u64 len, const char *name)
132 size_t namelen = strlen(name) + 1;
133 struct symbol *self = zalloc(symbol__priv_size +
134 sizeof(*self) + namelen);
138 if (symbol__priv_size)
139 self = ((void *)self) + symbol__priv_size;
142 self->end = len ? start + len - 1 : start;
144 pr_debug3("%s: %s %#Lx-%#Lx\n", __func__, name, start, self->end);
146 memcpy(self->name, name, namelen);
151 static void symbol__delete(struct symbol *self)
153 free(((void *)self) - symbol__priv_size);
156 static size_t symbol__fprintf(struct symbol *self, FILE *fp)
158 return fprintf(fp, " %llx-%llx %s\n",
159 self->start, self->end, self->name);
162 static void dso__set_long_name(struct dso *self, char *name)
166 self->long_name = name;
167 self->long_name_len = strlen(name);
170 static void dso__set_basename(struct dso *self)
172 self->short_name = basename(self->long_name);
175 struct dso *dso__new(const char *name)
177 struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
181 strcpy(self->name, name);
182 dso__set_long_name(self, self->name);
183 self->short_name = self->name;
184 for (i = 0; i < MAP__NR_TYPES; ++i)
185 self->symbols[i] = self->symbol_names[i] = RB_ROOT;
186 self->slen_calculated = 0;
187 self->origin = DSO__ORIG_NOT_FOUND;
189 self->sorted_by_name = 0;
190 self->has_build_id = 0;
196 static void symbols__delete(struct rb_root *self)
199 struct rb_node *next = rb_first(self);
202 pos = rb_entry(next, struct symbol, rb_node);
203 next = rb_next(&pos->rb_node);
204 rb_erase(&pos->rb_node, self);
209 void dso__delete(struct dso *self)
212 for (i = 0; i < MAP__NR_TYPES; ++i)
213 symbols__delete(&self->symbols[i]);
214 if (self->long_name != self->name)
215 free(self->long_name);
219 void dso__set_build_id(struct dso *self, void *build_id)
221 memcpy(self->build_id, build_id, sizeof(self->build_id));
222 self->has_build_id = 1;
225 static void symbols__insert(struct rb_root *self, struct symbol *sym)
227 struct rb_node **p = &self->rb_node;
228 struct rb_node *parent = NULL;
229 const u64 ip = sym->start;
234 s = rb_entry(parent, struct symbol, rb_node);
240 rb_link_node(&sym->rb_node, parent, p);
241 rb_insert_color(&sym->rb_node, self);
244 static struct symbol *symbols__find(struct rb_root *self, u64 ip)
254 struct symbol *s = rb_entry(n, struct symbol, rb_node);
258 else if (ip > s->end)
267 struct symbol_name_rb_node {
268 struct rb_node rb_node;
272 static void symbols__insert_by_name(struct rb_root *self, struct symbol *sym)
274 struct rb_node **p = &self->rb_node;
275 struct rb_node *parent = NULL;
276 struct symbol_name_rb_node *symn = ((void *)sym) - sizeof(*parent), *s;
280 s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
281 if (strcmp(sym->name, s->sym.name) < 0)
286 rb_link_node(&symn->rb_node, parent, p);
287 rb_insert_color(&symn->rb_node, self);
290 static void symbols__sort_by_name(struct rb_root *self, struct rb_root *source)
294 for (nd = rb_first(source); nd; nd = rb_next(nd)) {
295 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
296 symbols__insert_by_name(self, pos);
300 static struct symbol *symbols__find_by_name(struct rb_root *self, const char *name)
310 struct symbol_name_rb_node *s;
313 s = rb_entry(n, struct symbol_name_rb_node, rb_node);
314 cmp = strcmp(name, s->sym.name);
327 struct symbol *dso__find_symbol(struct dso *self,
328 enum map_type type, u64 addr)
330 return symbols__find(&self->symbols[type], addr);
333 struct symbol *dso__find_symbol_by_name(struct dso *self, enum map_type type,
336 return symbols__find_by_name(&self->symbol_names[type], name);
339 void dso__sort_by_name(struct dso *self, enum map_type type)
341 dso__set_sorted_by_name(self, type);
342 return symbols__sort_by_name(&self->symbol_names[type],
343 &self->symbols[type]);
346 int build_id__sprintf(u8 *self, int len, char *bf)
352 for (i = 0; i < len; ++i) {
353 sprintf(bid, "%02x", *raw);
361 size_t dso__fprintf_buildid(struct dso *self, FILE *fp)
363 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
365 build_id__sprintf(self->build_id, sizeof(self->build_id), sbuild_id);
366 return fprintf(fp, "%s", sbuild_id);
369 size_t dso__fprintf(struct dso *self, enum map_type type, FILE *fp)
372 size_t ret = fprintf(fp, "dso: %s (", self->short_name);
374 ret += dso__fprintf_buildid(self, fp);
375 ret += fprintf(fp, ")\n");
376 for (nd = rb_first(&self->symbols[type]); nd; nd = rb_next(nd)) {
377 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
378 ret += symbol__fprintf(pos, fp);
385 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
386 * so that we can in the next step set the symbol ->end address and then
387 * call kernel_maps__split_kallsyms.
389 static int dso__load_all_kallsyms(struct dso *self, struct map *map)
393 struct rb_root *root = &self->symbols[map->type];
394 FILE *file = fopen("/proc/kallsyms", "r");
399 while (!feof(file)) {
406 line_len = getline(&line, &n, file);
413 line[--line_len] = '\0'; /* \n */
415 len = hex2u64(line, &start);
418 if (len + 2 >= line_len)
421 symbol_type = toupper(line[len]);
422 if (!symbol_type__is_a(symbol_type, map->type))
425 symbol_name = line + len + 2;
427 * Will fix up the end later, when we have all symbols sorted.
429 sym = symbol__new(start, 0, symbol_name);
432 goto out_delete_line;
434 * We will pass the symbols to the filter later, in
435 * map__split_kallsyms, when we have split the maps per module
437 symbols__insert(root, sym);
452 * Split the symbols into maps, making sure there are no overlaps, i.e. the
453 * kernel range is broken in several maps, named [kernel].N, as we don't have
454 * the original ELF section names vmlinux have.
456 static int dso__split_kallsyms(struct dso *self, struct map *map,
457 struct perf_session *session, symbol_filter_t filter)
459 struct map *curr_map = map;
462 struct rb_root *root = &self->symbols[map->type];
463 struct rb_node *next = rb_first(root);
464 int kernel_range = 0;
469 pos = rb_entry(next, struct symbol, rb_node);
470 next = rb_next(&pos->rb_node);
472 module = strchr(pos->name, '\t');
474 if (!session->use_modules)
479 if (strcmp(self->name, module)) {
480 curr_map = map_groups__find_by_name(&session->kmaps, map->type, module);
481 if (curr_map == NULL) {
482 pr_debug("/proc/{kallsyms,modules} "
488 * So that we look just like we get from .ko files,
489 * i.e. not prelinked, relative to map->start.
491 pos->start = curr_map->map_ip(curr_map, pos->start);
492 pos->end = curr_map->map_ip(curr_map, pos->end);
493 } else if (curr_map != map) {
494 char dso_name[PATH_MAX];
497 snprintf(dso_name, sizeof(dso_name), "[kernel].%d",
500 dso = dso__new(dso_name);
504 curr_map = map__new2(pos->start, dso, map->type);
510 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
511 map_groups__insert(&session->kmaps, curr_map);
515 if (filter && filter(curr_map, pos)) {
516 discard_symbol: rb_erase(&pos->rb_node, root);
519 if (curr_map != map) {
520 rb_erase(&pos->rb_node, root);
521 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
531 static int dso__load_kallsyms(struct dso *self, struct map *map,
532 struct perf_session *session, symbol_filter_t filter)
534 if (dso__load_all_kallsyms(self, map) < 0)
537 symbols__fixup_end(&self->symbols[map->type]);
538 self->origin = DSO__ORIG_KERNEL;
540 return dso__split_kallsyms(self, map, session, filter);
543 static int dso__load_perf_map(struct dso *self, struct map *map,
544 symbol_filter_t filter)
551 file = fopen(self->long_name, "r");
555 while (!feof(file)) {
560 line_len = getline(&line, &n, file);
567 line[--line_len] = '\0'; /* \n */
569 len = hex2u64(line, &start);
572 if (len + 2 >= line_len)
575 len += hex2u64(line + len, &size);
578 if (len + 2 >= line_len)
581 sym = symbol__new(start, size, line + len);
584 goto out_delete_line;
586 if (filter && filter(map, sym))
589 symbols__insert(&self->symbols[map->type], sym);
606 * elf_symtab__for_each_symbol - iterate thru all the symbols
608 * @self: struct elf_symtab instance to iterate
610 * @sym: GElf_Sym iterator
612 #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
613 for (idx = 0, gelf_getsym(syms, idx, &sym);\
615 idx++, gelf_getsym(syms, idx, &sym))
617 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
619 return GELF_ST_TYPE(sym->st_info);
622 static inline int elf_sym__is_function(const GElf_Sym *sym)
624 return elf_sym__type(sym) == STT_FUNC &&
626 sym->st_shndx != SHN_UNDEF;
629 static inline bool elf_sym__is_object(const GElf_Sym *sym)
631 return elf_sym__type(sym) == STT_OBJECT &&
633 sym->st_shndx != SHN_UNDEF;
636 static inline int elf_sym__is_label(const GElf_Sym *sym)
638 return elf_sym__type(sym) == STT_NOTYPE &&
640 sym->st_shndx != SHN_UNDEF &&
641 sym->st_shndx != SHN_ABS;
644 static inline const char *elf_sec__name(const GElf_Shdr *shdr,
645 const Elf_Data *secstrs)
647 return secstrs->d_buf + shdr->sh_name;
650 static inline int elf_sec__is_text(const GElf_Shdr *shdr,
651 const Elf_Data *secstrs)
653 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
656 static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
657 const Elf_Data *secstrs)
659 return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
662 static inline const char *elf_sym__name(const GElf_Sym *sym,
663 const Elf_Data *symstrs)
665 return symstrs->d_buf + sym->st_name;
668 static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
669 GElf_Shdr *shp, const char *name,
675 while ((sec = elf_nextscn(elf, sec)) != NULL) {
678 gelf_getshdr(sec, shp);
679 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
680 if (!strcmp(name, str)) {
691 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
692 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
694 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
696 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
697 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
699 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
702 * We need to check if we have a .dynsym, so that we can handle the
703 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
704 * .dynsym or .symtab).
705 * And always look at the original dso, not at debuginfo packages, that
706 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
708 static int dso__synthesize_plt_symbols(struct dso *self, struct map *map,
709 symbol_filter_t filter)
711 uint32_t nr_rel_entries, idx;
716 GElf_Shdr shdr_rel_plt, shdr_dynsym;
717 Elf_Data *reldata, *syms, *symstrs;
718 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
721 char sympltname[1024];
723 int nr = 0, symidx, fd, err = 0;
725 fd = open(self->long_name, O_RDONLY);
729 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
733 if (gelf_getehdr(elf, &ehdr) == NULL)
736 scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
737 ".dynsym", &dynsym_idx);
738 if (scn_dynsym == NULL)
741 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
743 if (scn_plt_rel == NULL) {
744 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
746 if (scn_plt_rel == NULL)
752 if (shdr_rel_plt.sh_link != dynsym_idx)
755 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
759 * Fetch the relocation section to find the idxes to the GOT
760 * and the symbols in the .dynsym they refer to.
762 reldata = elf_getdata(scn_plt_rel, NULL);
766 syms = elf_getdata(scn_dynsym, NULL);
770 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
771 if (scn_symstrs == NULL)
774 symstrs = elf_getdata(scn_symstrs, NULL);
778 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
779 plt_offset = shdr_plt.sh_offset;
781 if (shdr_rel_plt.sh_type == SHT_RELA) {
782 GElf_Rela pos_mem, *pos;
784 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
786 symidx = GELF_R_SYM(pos->r_info);
787 plt_offset += shdr_plt.sh_entsize;
788 gelf_getsym(syms, symidx, &sym);
789 snprintf(sympltname, sizeof(sympltname),
790 "%s@plt", elf_sym__name(&sym, symstrs));
792 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
797 if (filter && filter(map, f))
800 symbols__insert(&self->symbols[map->type], f);
804 } else if (shdr_rel_plt.sh_type == SHT_REL) {
805 GElf_Rel pos_mem, *pos;
806 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
808 symidx = GELF_R_SYM(pos->r_info);
809 plt_offset += shdr_plt.sh_entsize;
810 gelf_getsym(syms, symidx, &sym);
811 snprintf(sympltname, sizeof(sympltname),
812 "%s@plt", elf_sym__name(&sym, symstrs));
814 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
819 if (filter && filter(map, f))
822 symbols__insert(&self->symbols[map->type], f);
837 pr_warning("%s: problems reading %s PLT info.\n",
838 __func__, self->long_name);
842 static bool elf_sym__is_a(GElf_Sym *self, enum map_type type)
846 return elf_sym__is_function(self);
848 return elf_sym__is_object(self);
854 static bool elf_sec__is_a(GElf_Shdr *self, Elf_Data *secstrs, enum map_type type)
858 return elf_sec__is_text(self, secstrs);
860 return elf_sec__is_data(self, secstrs);
866 static int dso__load_sym(struct dso *self, struct map *map,
867 struct perf_session *session, const char *name, int fd,
868 symbol_filter_t filter, int kernel, int kmodule)
870 struct map *curr_map = map;
871 struct dso *curr_dso = self;
872 size_t dso_name_len = strlen(self->short_name);
873 Elf_Data *symstrs, *secstrs;
881 Elf_Scn *sec, *sec_strndx;
885 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
887 pr_err("%s: cannot read %s ELF file.\n", __func__, name);
891 if (gelf_getehdr(elf, &ehdr) == NULL) {
892 pr_err("%s: cannot get elf header.\n", __func__);
896 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
898 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
903 syms = elf_getdata(sec, NULL);
907 sec = elf_getscn(elf, shdr.sh_link);
911 symstrs = elf_getdata(sec, NULL);
915 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
916 if (sec_strndx == NULL)
919 secstrs = elf_getdata(sec_strndx, NULL);
923 nr_syms = shdr.sh_size / shdr.sh_entsize;
925 memset(&sym, 0, sizeof(sym));
927 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
928 elf_section_by_name(elf, &ehdr, &shdr,
931 } else self->adjust_symbols = 0;
933 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
935 const char *elf_name;
936 char *demangled = NULL;
937 int is_label = elf_sym__is_label(&sym);
938 const char *section_name;
940 if (!is_label && !elf_sym__is_a(&sym, map->type))
943 sec = elf_getscn(elf, sym.st_shndx);
947 gelf_getshdr(sec, &shdr);
949 if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type))
952 elf_name = elf_sym__name(&sym, symstrs);
953 section_name = elf_sec__name(&shdr, secstrs);
955 if (kernel || kmodule) {
956 char dso_name[PATH_MAX];
958 if (strcmp(section_name,
959 curr_dso->short_name + dso_name_len) == 0)
962 if (strcmp(section_name, ".text") == 0) {
968 snprintf(dso_name, sizeof(dso_name),
969 "%s%s", self->short_name, section_name);
971 curr_map = map_groups__find_by_name(&session->kmaps, map->type, dso_name);
972 if (curr_map == NULL) {
973 u64 start = sym.st_value;
976 start += map->start + shdr.sh_offset;
978 curr_dso = dso__new(dso_name);
979 if (curr_dso == NULL)
981 curr_map = map__new2(start, curr_dso,
983 if (curr_map == NULL) {
984 dso__delete(curr_dso);
987 curr_map->map_ip = identity__map_ip;
988 curr_map->unmap_ip = identity__map_ip;
989 curr_dso->origin = DSO__ORIG_KERNEL;
990 map_groups__insert(&session->kmaps, curr_map);
991 dsos__add(&dsos__kernel, curr_dso);
993 curr_dso = curr_map->dso;
998 if (curr_dso->adjust_symbols) {
999 pr_debug2("adjusting symbol: st_value: %Lx sh_addr: "
1000 "%Lx sh_offset: %Lx\n", (u64)sym.st_value,
1001 (u64)shdr.sh_addr, (u64)shdr.sh_offset);
1002 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
1005 * We need to figure out if the object was created from C++ sources
1006 * DWARF DW_compile_unit has this, but we don't always have access
1009 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
1010 if (demangled != NULL)
1011 elf_name = demangled;
1013 f = symbol__new(sym.st_value, sym.st_size, elf_name);
1018 if (filter && filter(curr_map, f))
1021 symbols__insert(&curr_dso->symbols[curr_map->type], f);
1027 * For misannotated, zeroed, ASM function sizes.
1030 symbols__fixup_end(&self->symbols[map->type]);
1038 static bool dso__build_id_equal(const struct dso *self, u8 *build_id)
1040 return memcmp(self->build_id, build_id, sizeof(self->build_id)) == 0;
1043 static bool __dsos__read_build_ids(struct list_head *head)
1045 bool have_build_id = false;
1048 list_for_each_entry(pos, head, node)
1049 if (filename__read_build_id(pos->long_name, pos->build_id,
1050 sizeof(pos->build_id)) > 0) {
1051 have_build_id = true;
1052 pos->has_build_id = true;
1055 return have_build_id;
1058 bool dsos__read_build_ids(void)
1060 bool kbuildids = __dsos__read_build_ids(&dsos__kernel),
1061 ubuildids = __dsos__read_build_ids(&dsos__user);
1062 return kbuildids || ubuildids;
1066 * Align offset to 4 bytes as needed for note name and descriptor data.
1068 #define NOTE_ALIGN(n) (((n) + 3) & -4U)
1070 int filename__read_build_id(const char *filename, void *bf, size_t size)
1081 if (size < BUILD_ID_SIZE)
1084 fd = open(filename, O_RDONLY);
1088 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1090 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
1095 if (ek != ELF_K_ELF)
1098 if (gelf_getehdr(elf, &ehdr) == NULL) {
1099 pr_err("%s: cannot get elf header.\n", __func__);
1103 sec = elf_section_by_name(elf, &ehdr, &shdr,
1104 ".note.gnu.build-id", NULL);
1106 sec = elf_section_by_name(elf, &ehdr, &shdr,
1112 data = elf_getdata(sec, NULL);
1117 while (ptr < (data->d_buf + data->d_size)) {
1118 GElf_Nhdr *nhdr = ptr;
1119 int namesz = NOTE_ALIGN(nhdr->n_namesz),
1120 descsz = NOTE_ALIGN(nhdr->n_descsz);
1123 ptr += sizeof(*nhdr);
1126 if (nhdr->n_type == NT_GNU_BUILD_ID &&
1127 nhdr->n_namesz == sizeof("GNU")) {
1128 if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
1129 memcpy(bf, ptr, BUILD_ID_SIZE);
1130 err = BUILD_ID_SIZE;
1144 int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
1148 if (size < BUILD_ID_SIZE)
1151 fd = open(filename, O_RDONLY);
1160 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
1163 namesz = NOTE_ALIGN(nhdr.n_namesz);
1164 descsz = NOTE_ALIGN(nhdr.n_descsz);
1165 if (nhdr.n_type == NT_GNU_BUILD_ID &&
1166 nhdr.n_namesz == sizeof("GNU")) {
1167 if (read(fd, bf, namesz) != namesz)
1169 if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
1170 if (read(fd, build_id,
1171 BUILD_ID_SIZE) == BUILD_ID_SIZE) {
1175 } else if (read(fd, bf, descsz) != descsz)
1178 int n = namesz + descsz;
1179 if (read(fd, bf, n) != n)
1188 char dso__symtab_origin(const struct dso *self)
1190 static const char origin[] = {
1191 [DSO__ORIG_KERNEL] = 'k',
1192 [DSO__ORIG_JAVA_JIT] = 'j',
1193 [DSO__ORIG_FEDORA] = 'f',
1194 [DSO__ORIG_UBUNTU] = 'u',
1195 [DSO__ORIG_BUILDID] = 'b',
1196 [DSO__ORIG_DSO] = 'd',
1197 [DSO__ORIG_KMODULE] = 'K',
1200 if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
1202 return origin[self->origin];
1205 int dso__load(struct dso *self, struct map *map, struct perf_session *session,
1206 symbol_filter_t filter)
1208 int size = PATH_MAX;
1210 u8 build_id[BUILD_ID_SIZE];
1214 dso__set_loaded(self, map->type);
1217 return dso__load_kernel_sym(self, map, session, filter);
1219 name = malloc(size);
1223 self->adjust_symbols = 0;
1225 if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
1226 ret = dso__load_perf_map(self, map, filter);
1227 self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
1228 DSO__ORIG_NOT_FOUND;
1232 self->origin = DSO__ORIG_FEDORA - 1;
1237 switch (self->origin) {
1238 case DSO__ORIG_FEDORA:
1239 snprintf(name, size, "/usr/lib/debug%s.debug",
1242 case DSO__ORIG_UBUNTU:
1243 snprintf(name, size, "/usr/lib/debug%s",
1246 case DSO__ORIG_BUILDID:
1247 if (filename__read_build_id(self->long_name, build_id,
1248 sizeof(build_id))) {
1249 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
1251 build_id__sprintf(build_id, sizeof(build_id),
1253 snprintf(name, size,
1254 "/usr/lib/debug/.build-id/%.2s/%s.debug",
1255 build_id_hex, build_id_hex + 2);
1256 if (self->has_build_id)
1257 goto compare_build_id;
1263 snprintf(name, size, "%s", self->long_name);
1270 if (self->has_build_id) {
1271 if (filename__read_build_id(name, build_id,
1272 sizeof(build_id)) < 0)
1275 if (!dso__build_id_equal(self, build_id))
1279 fd = open(name, O_RDONLY);
1282 ret = dso__load_sym(self, map, NULL, name, fd, filter, 0, 0);
1286 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
1292 int nr_plt = dso__synthesize_plt_symbols(self, map, filter);
1298 if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
1303 struct map *map_groups__find_by_name(struct map_groups *self,
1304 enum map_type type, const char *name)
1308 for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) {
1309 struct map *map = rb_entry(nd, struct map, rb_node);
1311 if (map->dso && strcmp(map->dso->name, name) == 0)
1318 static int perf_session__set_modules_path_dir(struct perf_session *self, char *dirname)
1320 struct dirent *dent;
1321 DIR *dir = opendir(dirname);
1324 pr_debug("%s: cannot open %s dir\n", __func__, dirname);
1328 while ((dent = readdir(dir)) != NULL) {
1329 char path[PATH_MAX];
1331 if (dent->d_type == DT_DIR) {
1332 if (!strcmp(dent->d_name, ".") ||
1333 !strcmp(dent->d_name, ".."))
1336 snprintf(path, sizeof(path), "%s/%s",
1337 dirname, dent->d_name);
1338 if (perf_session__set_modules_path_dir(self, path) < 0)
1341 char *dot = strrchr(dent->d_name, '.'),
1346 if (dot == NULL || strcmp(dot, ".ko"))
1348 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1349 (int)(dot - dent->d_name), dent->d_name);
1351 strxfrchar(dso_name, '-', '_');
1352 map = map_groups__find_by_name(&self->kmaps, MAP__FUNCTION, dso_name);
1356 snprintf(path, sizeof(path), "%s/%s",
1357 dirname, dent->d_name);
1359 long_name = strdup(path);
1360 if (long_name == NULL)
1362 dso__set_long_name(map->dso, long_name);
1372 static int perf_session__set_modules_path(struct perf_session *self)
1375 char modules_path[PATH_MAX];
1377 if (uname(&uts) < 0)
1380 snprintf(modules_path, sizeof(modules_path), "/lib/modules/%s/kernel",
1383 return perf_session__set_modules_path_dir(self, modules_path);
1387 * Constructor variant for modules (where we know from /proc/modules where
1388 * they are loaded) and for vmlinux, where only after we load all the
1389 * symbols we'll know where it starts and ends.
1391 static struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
1393 struct map *self = malloc(sizeof(*self));
1397 * ->end will be filled after we load all the symbols
1399 map__init(self, type, start, 0, 0, dso);
1405 static int perf_session__create_module_maps(struct perf_session *self)
1409 FILE *file = fopen("/proc/modules", "r");
1415 while (!feof(file)) {
1416 char name[PATH_MAX];
1422 line_len = getline(&line, &n, file);
1429 line[--line_len] = '\0'; /* \n */
1431 sep = strrchr(line, 'x');
1435 hex2u64(sep + 1, &start);
1437 sep = strchr(line, ' ');
1443 snprintf(name, sizeof(name), "[%s]", line);
1444 dso = dso__new(name);
1447 goto out_delete_line;
1449 map = map__new2(start, dso, MAP__FUNCTION);
1452 goto out_delete_line;
1455 snprintf(name, sizeof(name),
1456 "/sys/module/%s/notes/.note.gnu.build-id", line);
1457 if (sysfs__read_build_id(name, dso->build_id,
1458 sizeof(dso->build_id)) == 0)
1459 dso->has_build_id = true;
1461 dso->origin = DSO__ORIG_KMODULE;
1462 map_groups__insert(&self->kmaps, map);
1463 dsos__add(&dsos__kernel, dso);
1469 return perf_session__set_modules_path(self);
1477 static int dso__load_vmlinux(struct dso *self, struct map *map,
1478 struct perf_session *session,
1479 const char *vmlinux, symbol_filter_t filter)
1483 if (self->has_build_id) {
1484 u8 build_id[BUILD_ID_SIZE];
1486 if (filename__read_build_id(vmlinux, build_id,
1487 sizeof(build_id)) < 0) {
1488 pr_debug("No build_id in %s, ignoring it\n", vmlinux);
1491 if (!dso__build_id_equal(self, build_id)) {
1492 char expected_build_id[BUILD_ID_SIZE * 2 + 1],
1493 vmlinux_build_id[BUILD_ID_SIZE * 2 + 1];
1495 build_id__sprintf(self->build_id,
1496 sizeof(self->build_id),
1498 build_id__sprintf(build_id, sizeof(build_id),
1500 pr_debug("build_id in %s is %s while expected is %s, "
1501 "ignoring it\n", vmlinux, vmlinux_build_id,
1507 fd = open(vmlinux, O_RDONLY);
1511 dso__set_loaded(self, map->type);
1512 err = dso__load_sym(self, map, session, self->long_name, fd, filter, 1, 0);
1518 static int dso__load_kernel_sym(struct dso *self, struct map *map,
1519 struct perf_session *session, symbol_filter_t filter)
1524 if (vmlinux_path != NULL) {
1526 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1527 vmlinux_path__nr_entries);
1528 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
1529 err = dso__load_vmlinux(self, map, session,
1530 vmlinux_path[i], filter);
1532 pr_debug("Using %s for symbols\n",
1534 dso__set_long_name(self,
1535 strdup(vmlinux_path[i]));
1541 is_kallsyms = self->long_name[0] == '[';
1545 err = dso__load_vmlinux(self, map, session, self->long_name, filter);
1547 pr_info("The file %s cannot be used, "
1548 "trying to use /proc/kallsyms...", self->long_name);
1550 err = dso__load_kallsyms(self, map, session, filter);
1551 if (err > 0 && !is_kallsyms)
1552 dso__set_long_name(self, strdup("[kernel.kallsyms]"));
1557 map__fixup_start(map);
1558 map__fixup_end(map);
1564 LIST_HEAD(dsos__user);
1565 LIST_HEAD(dsos__kernel);
1568 static void dsos__add(struct list_head *head, struct dso *dso)
1570 list_add_tail(&dso->node, head);
1573 static struct dso *dsos__find(struct list_head *head, const char *name)
1577 list_for_each_entry(pos, head, node)
1578 if (strcmp(pos->name, name) == 0)
1583 struct dso *dsos__findnew(const char *name)
1585 struct dso *dso = dsos__find(&dsos__user, name);
1588 dso = dso__new(name);
1590 dsos__add(&dsos__user, dso);
1591 dso__set_basename(dso);
1598 static void __dsos__fprintf(struct list_head *head, FILE *fp)
1602 list_for_each_entry(pos, head, node) {
1604 for (i = 0; i < MAP__NR_TYPES; ++i)
1605 dso__fprintf(pos, i, fp);
1609 void dsos__fprintf(FILE *fp)
1611 __dsos__fprintf(&dsos__kernel, fp);
1612 __dsos__fprintf(&dsos__user, fp);
1615 static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp)
1620 list_for_each_entry(pos, head, node) {
1621 ret += dso__fprintf_buildid(pos, fp);
1622 ret += fprintf(fp, " %s\n", pos->long_name);
1627 size_t dsos__fprintf_buildid(FILE *fp)
1629 return (__dsos__fprintf_buildid(&dsos__kernel, fp) +
1630 __dsos__fprintf_buildid(&dsos__user, fp));
1633 static struct dso *dsos__create_kernel( const char *vmlinux)
1635 struct dso *kernel = dso__new(vmlinux ?: "[kernel.kallsyms]");
1640 kernel->short_name = "[kernel]";
1643 vdso = dso__new("[vdso]");
1645 goto out_delete_kernel_dso;
1646 dso__set_loaded(vdso, MAP__FUNCTION);
1648 if (sysfs__read_build_id("/sys/kernel/notes", kernel->build_id,
1649 sizeof(kernel->build_id)) == 0)
1650 kernel->has_build_id = true;
1652 dsos__add(&dsos__kernel, kernel);
1653 dsos__add(&dsos__user, vdso);
1657 out_delete_kernel_dso:
1658 dso__delete(kernel);
1662 static int map_groups__create_kernel_maps(struct map_groups *self, const char *vmlinux)
1664 struct map *functions, *variables;
1665 struct dso *kernel = dsos__create_kernel(vmlinux);
1670 functions = map__new2(0, kernel, MAP__FUNCTION);
1671 if (functions == NULL)
1674 variables = map__new2(0, kernel, MAP__VARIABLE);
1675 if (variables == NULL) {
1676 map__delete(functions);
1680 functions->map_ip = functions->unmap_ip =
1681 variables->map_ip = variables->unmap_ip = identity__map_ip;
1682 map_groups__insert(self, functions);
1683 map_groups__insert(self, variables);
1688 static void vmlinux_path__exit(void)
1690 while (--vmlinux_path__nr_entries >= 0) {
1691 free(vmlinux_path[vmlinux_path__nr_entries]);
1692 vmlinux_path[vmlinux_path__nr_entries] = NULL;
1696 vmlinux_path = NULL;
1699 static int vmlinux_path__init(void)
1704 if (uname(&uts) < 0)
1707 vmlinux_path = malloc(sizeof(char *) * 5);
1708 if (vmlinux_path == NULL)
1711 vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
1712 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1714 ++vmlinux_path__nr_entries;
1715 vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
1716 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1718 ++vmlinux_path__nr_entries;
1719 snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
1720 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1721 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1723 ++vmlinux_path__nr_entries;
1724 snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
1725 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1726 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1728 ++vmlinux_path__nr_entries;
1729 snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
1731 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1732 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1734 ++vmlinux_path__nr_entries;
1739 vmlinux_path__exit();
1743 int symbol__init(struct symbol_conf *conf)
1745 const struct symbol_conf *pconf = conf ?: &symbol_conf__defaults;
1747 elf_version(EV_CURRENT);
1748 symbol__priv_size = pconf->priv_size;
1749 if (pconf->sort_by_name)
1750 symbol__priv_size += (sizeof(struct symbol_name_rb_node) -
1751 sizeof(struct symbol));
1753 if (pconf->try_vmlinux_path && vmlinux_path__init() < 0)
1759 int perf_session__create_kernel_maps(struct perf_session *self,
1760 struct symbol_conf *conf)
1762 const struct symbol_conf *pconf = conf ?: &symbol_conf__defaults;
1764 if (map_groups__create_kernel_maps(&self->kmaps,
1765 pconf->vmlinux_name) < 0)
1768 self->use_modules = pconf->use_modules;
1770 if (pconf->use_modules && perf_session__create_module_maps(self) < 0)
1771 pr_debug("Failed to load list of modules for session %s, "
1772 "continuing...\n", self->filename);
1774 * Now that we have all the maps created, just set the ->end of them:
1776 map_groups__fixup_end(&self->kmaps);