16 #include <sys/utsname.h>
18 #ifndef NT_GNU_BUILD_ID
19 #define NT_GNU_BUILD_ID 3
25 DSO__ORIG_BUILD_ID_CACHE,
34 static void dsos__add(struct list_head *head, struct dso *dso);
35 static struct map *map__new2(u64 start, struct dso *dso, enum map_type type);
36 static int dso__load_kernel_sym(struct dso *self, struct map *map,
37 struct perf_session *session, symbol_filter_t filter);
38 static int vmlinux_path__nr_entries;
39 static char **vmlinux_path;
41 struct symbol_conf symbol_conf = {
42 .exclude_other = true,
44 .try_vmlinux_path = true,
47 bool dso__loaded(const struct dso *self, enum map_type type)
49 return self->loaded & (1 << type);
52 bool dso__sorted_by_name(const struct dso *self, enum map_type type)
54 return self->sorted_by_name & (1 << type);
57 static void dso__set_loaded(struct dso *self, enum map_type type)
59 self->loaded |= (1 << type);
62 static void dso__set_sorted_by_name(struct dso *self, enum map_type type)
64 self->sorted_by_name |= (1 << type);
67 bool symbol_type__is_a(char symbol_type, enum map_type map_type)
71 return symbol_type == 'T' || symbol_type == 'W';
73 return symbol_type == 'D' || symbol_type == 'd';
79 static void symbols__fixup_end(struct rb_root *self)
81 struct rb_node *nd, *prevnd = rb_first(self);
82 struct symbol *curr, *prev;
87 curr = rb_entry(prevnd, struct symbol, rb_node);
89 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
91 curr = rb_entry(nd, struct symbol, rb_node);
93 if (prev->end == prev->start)
94 prev->end = curr->start - 1;
98 if (curr->end == curr->start)
99 curr->end = roundup(curr->start, 4096);
102 static void __map_groups__fixup_end(struct map_groups *self, enum map_type type)
104 struct map *prev, *curr;
105 struct rb_node *nd, *prevnd = rb_first(&self->maps[type]);
110 curr = rb_entry(prevnd, struct map, rb_node);
112 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
114 curr = rb_entry(nd, struct map, rb_node);
115 prev->end = curr->start - 1;
119 * We still haven't the actual symbols, so guess the
120 * last map final address.
125 static void map_groups__fixup_end(struct map_groups *self)
128 for (i = 0; i < MAP__NR_TYPES; ++i)
129 __map_groups__fixup_end(self, i);
132 static struct symbol *symbol__new(u64 start, u64 len, const char *name)
134 size_t namelen = strlen(name) + 1;
135 struct symbol *self = zalloc(symbol_conf.priv_size +
136 sizeof(*self) + namelen);
140 if (symbol_conf.priv_size)
141 self = ((void *)self) + symbol_conf.priv_size;
144 self->end = len ? start + len - 1 : start;
146 pr_debug3("%s: %s %#Lx-%#Lx\n", __func__, name, start, self->end);
148 memcpy(self->name, name, namelen);
153 static void symbol__delete(struct symbol *self)
155 free(((void *)self) - symbol_conf.priv_size);
158 static size_t symbol__fprintf(struct symbol *self, FILE *fp)
160 return fprintf(fp, " %llx-%llx %s\n",
161 self->start, self->end, self->name);
164 void dso__set_long_name(struct dso *self, char *name)
168 self->long_name = name;
169 self->long_name_len = strlen(name);
172 static void dso__set_basename(struct dso *self)
174 self->short_name = basename(self->long_name);
177 struct dso *dso__new(const char *name)
179 struct dso *self = zalloc(sizeof(*self) + strlen(name) + 1);
183 strcpy(self->name, name);
184 dso__set_long_name(self, self->name);
185 self->short_name = self->name;
186 for (i = 0; i < MAP__NR_TYPES; ++i)
187 self->symbols[i] = self->symbol_names[i] = RB_ROOT;
188 self->slen_calculated = 0;
189 self->origin = DSO__ORIG_NOT_FOUND;
191 self->sorted_by_name = 0;
192 self->has_build_id = 0;
198 static void symbols__delete(struct rb_root *self)
201 struct rb_node *next = rb_first(self);
204 pos = rb_entry(next, struct symbol, rb_node);
205 next = rb_next(&pos->rb_node);
206 rb_erase(&pos->rb_node, self);
211 void dso__delete(struct dso *self)
214 for (i = 0; i < MAP__NR_TYPES; ++i)
215 symbols__delete(&self->symbols[i]);
216 if (self->long_name != self->name)
217 free(self->long_name);
221 void dso__set_build_id(struct dso *self, void *build_id)
223 memcpy(self->build_id, build_id, sizeof(self->build_id));
224 self->has_build_id = 1;
227 static void symbols__insert(struct rb_root *self, struct symbol *sym)
229 struct rb_node **p = &self->rb_node;
230 struct rb_node *parent = NULL;
231 const u64 ip = sym->start;
236 s = rb_entry(parent, struct symbol, rb_node);
242 rb_link_node(&sym->rb_node, parent, p);
243 rb_insert_color(&sym->rb_node, self);
246 static struct symbol *symbols__find(struct rb_root *self, u64 ip)
256 struct symbol *s = rb_entry(n, struct symbol, rb_node);
260 else if (ip > s->end)
269 struct symbol_name_rb_node {
270 struct rb_node rb_node;
274 static void symbols__insert_by_name(struct rb_root *self, struct symbol *sym)
276 struct rb_node **p = &self->rb_node;
277 struct rb_node *parent = NULL;
278 struct symbol_name_rb_node *symn = ((void *)sym) - sizeof(*parent), *s;
282 s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
283 if (strcmp(sym->name, s->sym.name) < 0)
288 rb_link_node(&symn->rb_node, parent, p);
289 rb_insert_color(&symn->rb_node, self);
292 static void symbols__sort_by_name(struct rb_root *self, struct rb_root *source)
296 for (nd = rb_first(source); nd; nd = rb_next(nd)) {
297 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
298 symbols__insert_by_name(self, pos);
302 static struct symbol *symbols__find_by_name(struct rb_root *self, const char *name)
312 struct symbol_name_rb_node *s;
315 s = rb_entry(n, struct symbol_name_rb_node, rb_node);
316 cmp = strcmp(name, s->sym.name);
329 struct symbol *dso__find_symbol(struct dso *self,
330 enum map_type type, u64 addr)
332 return symbols__find(&self->symbols[type], addr);
335 struct symbol *dso__find_symbol_by_name(struct dso *self, enum map_type type,
338 return symbols__find_by_name(&self->symbol_names[type], name);
341 void dso__sort_by_name(struct dso *self, enum map_type type)
343 dso__set_sorted_by_name(self, type);
344 return symbols__sort_by_name(&self->symbol_names[type],
345 &self->symbols[type]);
348 int build_id__sprintf(const u8 *self, int len, char *bf)
351 const u8 *raw = self;
354 for (i = 0; i < len; ++i) {
355 sprintf(bid, "%02x", *raw);
363 size_t dso__fprintf_buildid(struct dso *self, FILE *fp)
365 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
367 build_id__sprintf(self->build_id, sizeof(self->build_id), sbuild_id);
368 return fprintf(fp, "%s", sbuild_id);
371 size_t dso__fprintf(struct dso *self, enum map_type type, FILE *fp)
374 size_t ret = fprintf(fp, "dso: %s (", self->short_name);
376 ret += dso__fprintf_buildid(self, fp);
377 ret += fprintf(fp, ")\n");
378 for (nd = rb_first(&self->symbols[type]); nd; nd = rb_next(nd)) {
379 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
380 ret += symbol__fprintf(pos, fp);
386 int kallsyms__parse(const char *filename, void *arg,
387 int (*process_symbol)(void *arg, const char *name,
388 char type, u64 start))
393 FILE *file = fopen(filename, "r");
398 while (!feof(file)) {
404 line_len = getline(&line, &n, file);
411 line[--line_len] = '\0'; /* \n */
413 len = hex2u64(line, &start);
416 if (len + 2 >= line_len)
419 symbol_type = toupper(line[len]);
420 symbol_name = line + len + 2;
422 err = process_symbol(arg, symbol_name, symbol_type, start);
435 struct process_kallsyms_args {
440 static int map__process_kallsym_symbol(void *arg, const char *name,
441 char type, u64 start)
444 struct process_kallsyms_args *a = arg;
445 struct rb_root *root = &a->dso->symbols[a->map->type];
447 if (!symbol_type__is_a(type, a->map->type))
451 * Will fix up the end later, when we have all symbols sorted.
453 sym = symbol__new(start, 0, name);
458 * We will pass the symbols to the filter later, in
459 * map__split_kallsyms, when we have split the maps per module
461 symbols__insert(root, sym);
466 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
467 * so that we can in the next step set the symbol ->end address and then
468 * call kernel_maps__split_kallsyms.
470 static int dso__load_all_kallsyms(struct dso *self, const char *filename,
473 struct process_kallsyms_args args = { .map = map, .dso = self, };
474 return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
478 * Split the symbols into maps, making sure there are no overlaps, i.e. the
479 * kernel range is broken in several maps, named [kernel].N, as we don't have
480 * the original ELF section names vmlinux have.
482 static int dso__split_kallsyms(struct dso *self, struct map *map,
483 struct perf_session *session, symbol_filter_t filter)
485 struct map *curr_map = map;
488 struct rb_root *root = &self->symbols[map->type];
489 struct rb_node *next = rb_first(root);
490 int kernel_range = 0;
495 pos = rb_entry(next, struct symbol, rb_node);
496 next = rb_next(&pos->rb_node);
498 module = strchr(pos->name, '\t');
500 if (!symbol_conf.use_modules)
505 if (strcmp(curr_map->dso->short_name, module)) {
506 curr_map = map_groups__find_by_name(&session->kmaps, map->type, module);
507 if (curr_map == NULL) {
508 pr_debug("/proc/{kallsyms,modules} "
509 "inconsistency while looking "
510 "for \"%s\" module!\n", module);
514 if (curr_map->dso->loaded)
518 * So that we look just like we get from .ko files,
519 * i.e. not prelinked, relative to map->start.
521 pos->start = curr_map->map_ip(curr_map, pos->start);
522 pos->end = curr_map->map_ip(curr_map, pos->end);
523 } else if (curr_map != map) {
524 char dso_name[PATH_MAX];
527 snprintf(dso_name, sizeof(dso_name), "[kernel].%d",
530 dso = dso__new(dso_name);
534 curr_map = map__new2(pos->start, dso, map->type);
540 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
541 map_groups__insert(&session->kmaps, curr_map);
545 if (filter && filter(curr_map, pos)) {
546 discard_symbol: rb_erase(&pos->rb_node, root);
549 if (curr_map != map) {
550 rb_erase(&pos->rb_node, root);
551 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
561 static int dso__load_kallsyms(struct dso *self, const char *filename, struct map *map,
562 struct perf_session *session, symbol_filter_t filter)
564 if (dso__load_all_kallsyms(self, filename, map) < 0)
567 symbols__fixup_end(&self->symbols[map->type]);
568 self->origin = DSO__ORIG_KERNEL;
570 return dso__split_kallsyms(self, map, session, filter);
573 static int dso__load_perf_map(struct dso *self, struct map *map,
574 symbol_filter_t filter)
581 file = fopen(self->long_name, "r");
585 while (!feof(file)) {
590 line_len = getline(&line, &n, file);
597 line[--line_len] = '\0'; /* \n */
599 len = hex2u64(line, &start);
602 if (len + 2 >= line_len)
605 len += hex2u64(line + len, &size);
608 if (len + 2 >= line_len)
611 sym = symbol__new(start, size, line + len);
614 goto out_delete_line;
616 if (filter && filter(map, sym))
619 symbols__insert(&self->symbols[map->type], sym);
636 * elf_symtab__for_each_symbol - iterate thru all the symbols
638 * @self: struct elf_symtab instance to iterate
640 * @sym: GElf_Sym iterator
642 #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
643 for (idx = 0, gelf_getsym(syms, idx, &sym);\
645 idx++, gelf_getsym(syms, idx, &sym))
647 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
649 return GELF_ST_TYPE(sym->st_info);
652 static inline int elf_sym__is_function(const GElf_Sym *sym)
654 return elf_sym__type(sym) == STT_FUNC &&
656 sym->st_shndx != SHN_UNDEF;
659 static inline bool elf_sym__is_object(const GElf_Sym *sym)
661 return elf_sym__type(sym) == STT_OBJECT &&
663 sym->st_shndx != SHN_UNDEF;
666 static inline int elf_sym__is_label(const GElf_Sym *sym)
668 return elf_sym__type(sym) == STT_NOTYPE &&
670 sym->st_shndx != SHN_UNDEF &&
671 sym->st_shndx != SHN_ABS;
674 static inline const char *elf_sec__name(const GElf_Shdr *shdr,
675 const Elf_Data *secstrs)
677 return secstrs->d_buf + shdr->sh_name;
680 static inline int elf_sec__is_text(const GElf_Shdr *shdr,
681 const Elf_Data *secstrs)
683 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
686 static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
687 const Elf_Data *secstrs)
689 return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
692 static inline const char *elf_sym__name(const GElf_Sym *sym,
693 const Elf_Data *symstrs)
695 return symstrs->d_buf + sym->st_name;
698 static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
699 GElf_Shdr *shp, const char *name,
705 while ((sec = elf_nextscn(elf, sec)) != NULL) {
708 gelf_getshdr(sec, shp);
709 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
710 if (!strcmp(name, str)) {
721 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
722 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
724 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
726 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
727 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
729 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
732 * We need to check if we have a .dynsym, so that we can handle the
733 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
734 * .dynsym or .symtab).
735 * And always look at the original dso, not at debuginfo packages, that
736 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
738 static int dso__synthesize_plt_symbols(struct dso *self, struct map *map,
739 symbol_filter_t filter)
741 uint32_t nr_rel_entries, idx;
746 GElf_Shdr shdr_rel_plt, shdr_dynsym;
747 Elf_Data *reldata, *syms, *symstrs;
748 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
751 char sympltname[1024];
753 int nr = 0, symidx, fd, err = 0;
755 fd = open(self->long_name, O_RDONLY);
759 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
763 if (gelf_getehdr(elf, &ehdr) == NULL)
766 scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
767 ".dynsym", &dynsym_idx);
768 if (scn_dynsym == NULL)
771 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
773 if (scn_plt_rel == NULL) {
774 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
776 if (scn_plt_rel == NULL)
782 if (shdr_rel_plt.sh_link != dynsym_idx)
785 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
789 * Fetch the relocation section to find the idxes to the GOT
790 * and the symbols in the .dynsym they refer to.
792 reldata = elf_getdata(scn_plt_rel, NULL);
796 syms = elf_getdata(scn_dynsym, NULL);
800 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
801 if (scn_symstrs == NULL)
804 symstrs = elf_getdata(scn_symstrs, NULL);
808 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
809 plt_offset = shdr_plt.sh_offset;
811 if (shdr_rel_plt.sh_type == SHT_RELA) {
812 GElf_Rela pos_mem, *pos;
814 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
816 symidx = GELF_R_SYM(pos->r_info);
817 plt_offset += shdr_plt.sh_entsize;
818 gelf_getsym(syms, symidx, &sym);
819 snprintf(sympltname, sizeof(sympltname),
820 "%s@plt", elf_sym__name(&sym, symstrs));
822 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
827 if (filter && filter(map, f))
830 symbols__insert(&self->symbols[map->type], f);
834 } else if (shdr_rel_plt.sh_type == SHT_REL) {
835 GElf_Rel pos_mem, *pos;
836 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
838 symidx = GELF_R_SYM(pos->r_info);
839 plt_offset += shdr_plt.sh_entsize;
840 gelf_getsym(syms, symidx, &sym);
841 snprintf(sympltname, sizeof(sympltname),
842 "%s@plt", elf_sym__name(&sym, symstrs));
844 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
849 if (filter && filter(map, f))
852 symbols__insert(&self->symbols[map->type], f);
867 pr_warning("%s: problems reading %s PLT info.\n",
868 __func__, self->long_name);
872 static bool elf_sym__is_a(GElf_Sym *self, enum map_type type)
876 return elf_sym__is_function(self);
878 return elf_sym__is_object(self);
884 static bool elf_sec__is_a(GElf_Shdr *self, Elf_Data *secstrs, enum map_type type)
888 return elf_sec__is_text(self, secstrs);
890 return elf_sec__is_data(self, secstrs);
896 static int dso__load_sym(struct dso *self, struct map *map,
897 struct perf_session *session, const char *name, int fd,
898 symbol_filter_t filter, int kernel, int kmodule)
900 struct map *curr_map = map;
901 struct dso *curr_dso = self;
902 size_t dso_name_len = strlen(self->short_name);
903 Elf_Data *symstrs, *secstrs;
911 Elf_Scn *sec, *sec_strndx;
915 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
917 pr_err("%s: cannot read %s ELF file.\n", __func__, name);
921 if (gelf_getehdr(elf, &ehdr) == NULL) {
922 pr_err("%s: cannot get elf header.\n", __func__);
926 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
928 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
933 syms = elf_getdata(sec, NULL);
937 sec = elf_getscn(elf, shdr.sh_link);
941 symstrs = elf_getdata(sec, NULL);
945 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
946 if (sec_strndx == NULL)
949 secstrs = elf_getdata(sec_strndx, NULL);
953 nr_syms = shdr.sh_size / shdr.sh_entsize;
955 memset(&sym, 0, sizeof(sym));
957 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
958 elf_section_by_name(elf, &ehdr, &shdr,
961 } else self->adjust_symbols = 0;
963 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
965 const char *elf_name = elf_sym__name(&sym, symstrs);
966 char *demangled = NULL;
967 int is_label = elf_sym__is_label(&sym);
968 const char *section_name;
970 if (kernel && session->ref_reloc_sym.name != NULL &&
971 strcmp(elf_name, session->ref_reloc_sym.name) == 0)
972 perf_session__reloc_vmlinux_maps(session, sym.st_value);
974 if (!is_label && !elf_sym__is_a(&sym, map->type))
977 sec = elf_getscn(elf, sym.st_shndx);
981 gelf_getshdr(sec, &shdr);
983 if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type))
986 section_name = elf_sec__name(&shdr, secstrs);
988 if (kernel || kmodule) {
989 char dso_name[PATH_MAX];
991 if (strcmp(section_name,
992 curr_dso->short_name + dso_name_len) == 0)
995 if (strcmp(section_name, ".text") == 0) {
1001 snprintf(dso_name, sizeof(dso_name),
1002 "%s%s", self->short_name, section_name);
1004 curr_map = map_groups__find_by_name(&session->kmaps, map->type, dso_name);
1005 if (curr_map == NULL) {
1006 u64 start = sym.st_value;
1009 start += map->start + shdr.sh_offset;
1011 curr_dso = dso__new(dso_name);
1012 if (curr_dso == NULL)
1014 curr_map = map__new2(start, curr_dso,
1016 if (curr_map == NULL) {
1017 dso__delete(curr_dso);
1020 curr_map->map_ip = identity__map_ip;
1021 curr_map->unmap_ip = identity__map_ip;
1022 curr_dso->origin = DSO__ORIG_KERNEL;
1023 map_groups__insert(&session->kmaps, curr_map);
1024 dsos__add(&dsos__kernel, curr_dso);
1026 curr_dso = curr_map->dso;
1031 if (curr_dso->adjust_symbols) {
1032 pr_debug2("adjusting symbol: st_value: %Lx sh_addr: "
1033 "%Lx sh_offset: %Lx\n", (u64)sym.st_value,
1034 (u64)shdr.sh_addr, (u64)shdr.sh_offset);
1035 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
1038 * We need to figure out if the object was created from C++ sources
1039 * DWARF DW_compile_unit has this, but we don't always have access
1042 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
1043 if (demangled != NULL)
1044 elf_name = demangled;
1046 f = symbol__new(sym.st_value, sym.st_size, elf_name);
1051 if (filter && filter(curr_map, f))
1054 symbols__insert(&curr_dso->symbols[curr_map->type], f);
1060 * For misannotated, zeroed, ASM function sizes.
1063 symbols__fixup_end(&self->symbols[map->type]);
1071 static bool dso__build_id_equal(const struct dso *self, u8 *build_id)
1073 return memcmp(self->build_id, build_id, sizeof(self->build_id)) == 0;
1076 static bool __dsos__read_build_ids(struct list_head *head)
1078 bool have_build_id = false;
1081 list_for_each_entry(pos, head, node)
1082 if (filename__read_build_id(pos->long_name, pos->build_id,
1083 sizeof(pos->build_id)) > 0) {
1084 have_build_id = true;
1085 pos->has_build_id = true;
1088 return have_build_id;
1091 bool dsos__read_build_ids(void)
1093 bool kbuildids = __dsos__read_build_ids(&dsos__kernel),
1094 ubuildids = __dsos__read_build_ids(&dsos__user);
1095 return kbuildids || ubuildids;
1099 * Align offset to 4 bytes as needed for note name and descriptor data.
1101 #define NOTE_ALIGN(n) (((n) + 3) & -4U)
1103 int filename__read_build_id(const char *filename, void *bf, size_t size)
1114 if (size < BUILD_ID_SIZE)
1117 fd = open(filename, O_RDONLY);
1121 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1123 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
1128 if (ek != ELF_K_ELF)
1131 if (gelf_getehdr(elf, &ehdr) == NULL) {
1132 pr_err("%s: cannot get elf header.\n", __func__);
1136 sec = elf_section_by_name(elf, &ehdr, &shdr,
1137 ".note.gnu.build-id", NULL);
1139 sec = elf_section_by_name(elf, &ehdr, &shdr,
1145 data = elf_getdata(sec, NULL);
1150 while (ptr < (data->d_buf + data->d_size)) {
1151 GElf_Nhdr *nhdr = ptr;
1152 int namesz = NOTE_ALIGN(nhdr->n_namesz),
1153 descsz = NOTE_ALIGN(nhdr->n_descsz);
1156 ptr += sizeof(*nhdr);
1159 if (nhdr->n_type == NT_GNU_BUILD_ID &&
1160 nhdr->n_namesz == sizeof("GNU")) {
1161 if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
1162 memcpy(bf, ptr, BUILD_ID_SIZE);
1163 err = BUILD_ID_SIZE;
1177 int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
1181 if (size < BUILD_ID_SIZE)
1184 fd = open(filename, O_RDONLY);
1193 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
1196 namesz = NOTE_ALIGN(nhdr.n_namesz);
1197 descsz = NOTE_ALIGN(nhdr.n_descsz);
1198 if (nhdr.n_type == NT_GNU_BUILD_ID &&
1199 nhdr.n_namesz == sizeof("GNU")) {
1200 if (read(fd, bf, namesz) != namesz)
1202 if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
1203 if (read(fd, build_id,
1204 BUILD_ID_SIZE) == BUILD_ID_SIZE) {
1208 } else if (read(fd, bf, descsz) != descsz)
1211 int n = namesz + descsz;
1212 if (read(fd, bf, n) != n)
1221 char dso__symtab_origin(const struct dso *self)
1223 static const char origin[] = {
1224 [DSO__ORIG_KERNEL] = 'k',
1225 [DSO__ORIG_JAVA_JIT] = 'j',
1226 [DSO__ORIG_BUILD_ID_CACHE] = 'B',
1227 [DSO__ORIG_FEDORA] = 'f',
1228 [DSO__ORIG_UBUNTU] = 'u',
1229 [DSO__ORIG_BUILDID] = 'b',
1230 [DSO__ORIG_DSO] = 'd',
1231 [DSO__ORIG_KMODULE] = 'K',
1234 if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
1236 return origin[self->origin];
1239 int dso__load(struct dso *self, struct map *map, struct perf_session *session,
1240 symbol_filter_t filter)
1242 int size = PATH_MAX;
1244 u8 build_id[BUILD_ID_SIZE];
1245 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
1249 dso__set_loaded(self, map->type);
1252 return dso__load_kernel_sym(self, map, session, filter);
1254 name = malloc(size);
1258 self->adjust_symbols = 0;
1260 if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
1261 ret = dso__load_perf_map(self, map, filter);
1262 self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
1263 DSO__ORIG_NOT_FOUND;
1267 self->origin = DSO__ORIG_BUILD_ID_CACHE;
1269 if (self->has_build_id) {
1270 build_id__sprintf(self->build_id, sizeof(self->build_id),
1272 snprintf(name, size, "%s/%s/.build-id/%.2s/%s",
1273 getenv("HOME"), DEBUG_CACHE_DIR,
1274 build_id_hex, build_id_hex + 2);
1280 switch (self->origin) {
1281 case DSO__ORIG_FEDORA:
1282 snprintf(name, size, "/usr/lib/debug%s.debug",
1285 case DSO__ORIG_UBUNTU:
1286 snprintf(name, size, "/usr/lib/debug%s",
1289 case DSO__ORIG_BUILDID:
1290 if (filename__read_build_id(self->long_name, build_id,
1291 sizeof(build_id))) {
1292 build_id__sprintf(build_id, sizeof(build_id),
1294 snprintf(name, size,
1295 "/usr/lib/debug/.build-id/%.2s/%s.debug",
1296 build_id_hex, build_id_hex + 2);
1297 if (self->has_build_id)
1298 goto compare_build_id;
1304 snprintf(name, size, "%s", self->long_name);
1311 if (self->has_build_id) {
1312 if (filename__read_build_id(name, build_id,
1313 sizeof(build_id)) < 0)
1316 if (!dso__build_id_equal(self, build_id))
1320 fd = open(name, O_RDONLY);
1323 ret = dso__load_sym(self, map, NULL, name, fd, filter, 0, 0);
1327 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
1333 int nr_plt = dso__synthesize_plt_symbols(self, map, filter);
1339 if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
1344 struct map *map_groups__find_by_name(struct map_groups *self,
1345 enum map_type type, const char *name)
1349 for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) {
1350 struct map *map = rb_entry(nd, struct map, rb_node);
1352 if (map->dso && strcmp(map->dso->short_name, name) == 0)
1359 static int dso__kernel_module_get_build_id(struct dso *self)
1361 char filename[PATH_MAX];
1363 * kernel module short names are of the form "[module]" and
1364 * we need just "module" here.
1366 const char *name = self->short_name + 1;
1368 snprintf(filename, sizeof(filename),
1369 "/sys/module/%.*s/notes/.note.gnu.build-id",
1370 (int)strlen(name - 1), name);
1372 if (sysfs__read_build_id(filename, self->build_id,
1373 sizeof(self->build_id)) == 0)
1374 self->has_build_id = true;
1379 static int perf_session__set_modules_path_dir(struct perf_session *self, char *dirname)
1381 struct dirent *dent;
1382 DIR *dir = opendir(dirname);
1385 pr_debug("%s: cannot open %s dir\n", __func__, dirname);
1389 while ((dent = readdir(dir)) != NULL) {
1390 char path[PATH_MAX];
1392 if (dent->d_type == DT_DIR) {
1393 if (!strcmp(dent->d_name, ".") ||
1394 !strcmp(dent->d_name, ".."))
1397 snprintf(path, sizeof(path), "%s/%s",
1398 dirname, dent->d_name);
1399 if (perf_session__set_modules_path_dir(self, path) < 0)
1402 char *dot = strrchr(dent->d_name, '.'),
1407 if (dot == NULL || strcmp(dot, ".ko"))
1409 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1410 (int)(dot - dent->d_name), dent->d_name);
1412 strxfrchar(dso_name, '-', '_');
1413 map = map_groups__find_by_name(&self->kmaps, MAP__FUNCTION, dso_name);
1417 snprintf(path, sizeof(path), "%s/%s",
1418 dirname, dent->d_name);
1420 long_name = strdup(path);
1421 if (long_name == NULL)
1423 dso__set_long_name(map->dso, long_name);
1424 dso__kernel_module_get_build_id(map->dso);
1434 static int perf_session__set_modules_path(struct perf_session *self)
1437 char modules_path[PATH_MAX];
1439 if (uname(&uts) < 0)
1442 snprintf(modules_path, sizeof(modules_path), "/lib/modules/%s/kernel",
1445 return perf_session__set_modules_path_dir(self, modules_path);
1449 * Constructor variant for modules (where we know from /proc/modules where
1450 * they are loaded) and for vmlinux, where only after we load all the
1451 * symbols we'll know where it starts and ends.
1453 static struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
1455 struct map *self = malloc(sizeof(*self));
1459 * ->end will be filled after we load all the symbols
1461 map__init(self, type, start, 0, 0, dso);
1467 struct map *perf_session__new_module_map(struct perf_session *self, u64 start,
1468 const char *filename)
1471 struct dso *dso = __dsos__findnew(&dsos__kernel, filename);
1476 map = map__new2(start, dso, MAP__FUNCTION);
1480 dso->origin = DSO__ORIG_KMODULE;
1481 map_groups__insert(&self->kmaps, map);
1485 static int perf_session__create_module_maps(struct perf_session *self)
1489 FILE *file = fopen("/proc/modules", "r");
1495 while (!feof(file)) {
1496 char name[PATH_MAX];
1501 line_len = getline(&line, &n, file);
1508 line[--line_len] = '\0'; /* \n */
1510 sep = strrchr(line, 'x');
1514 hex2u64(sep + 1, &start);
1516 sep = strchr(line, ' ');
1522 snprintf(name, sizeof(name), "[%s]", line);
1523 map = perf_session__new_module_map(self, start, name);
1525 goto out_delete_line;
1526 dso__kernel_module_get_build_id(map->dso);
1532 return perf_session__set_modules_path(self);
1540 static int dso__load_vmlinux(struct dso *self, struct map *map,
1541 struct perf_session *session,
1542 const char *vmlinux, symbol_filter_t filter)
1546 if (self->has_build_id) {
1547 u8 build_id[BUILD_ID_SIZE];
1549 if (filename__read_build_id(vmlinux, build_id,
1550 sizeof(build_id)) < 0) {
1551 pr_debug("No build_id in %s, ignoring it\n", vmlinux);
1554 if (!dso__build_id_equal(self, build_id)) {
1555 char expected_build_id[BUILD_ID_SIZE * 2 + 1],
1556 vmlinux_build_id[BUILD_ID_SIZE * 2 + 1];
1558 build_id__sprintf(self->build_id,
1559 sizeof(self->build_id),
1561 build_id__sprintf(build_id, sizeof(build_id),
1563 pr_debug("build_id in %s is %s while expected is %s, "
1564 "ignoring it\n", vmlinux, vmlinux_build_id,
1570 fd = open(vmlinux, O_RDONLY);
1574 dso__set_loaded(self, map->type);
1575 err = dso__load_sym(self, map, session, vmlinux, fd, filter, 1, 0);
1581 static int dso__load_kernel_sym(struct dso *self, struct map *map,
1582 struct perf_session *session, symbol_filter_t filter)
1585 const char *kallsyms_filename = NULL;
1586 char *kallsyms_allocated_filename = NULL;
1588 * Step 1: if the user specified a vmlinux filename, use it and only
1589 * it, reporting errors to the user if it cannot be used.
1591 * For instance, try to analyse an ARM perf.data file _without_ a
1592 * build-id, or if the user specifies the wrong path to the right
1593 * vmlinux file, obviously we can't fallback to another vmlinux (a
1594 * x86_86 one, on the machine where analysis is being performed, say),
1595 * or worse, /proc/kallsyms.
1597 * If the specified file _has_ a build-id and there is a build-id
1598 * section in the perf.data file, we will still do the expected
1599 * validation in dso__load_vmlinux and will bail out if they don't
1602 if (symbol_conf.vmlinux_name != NULL) {
1603 err = dso__load_vmlinux(self, map, session,
1604 symbol_conf.vmlinux_name, filter);
1608 if (vmlinux_path != NULL) {
1610 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1611 vmlinux_path__nr_entries);
1612 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
1613 err = dso__load_vmlinux(self, map, session,
1614 vmlinux_path[i], filter);
1616 pr_debug("Using %s for symbols\n",
1618 dso__set_long_name(self,
1619 strdup(vmlinux_path[i]));
1626 * Say the kernel DSO was created when processing the build-id header table,
1627 * we have a build-id, so check if it is the same as the running kernel,
1628 * using it if it is.
1630 if (self->has_build_id) {
1631 u8 kallsyms_build_id[BUILD_ID_SIZE];
1632 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1634 if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id,
1635 sizeof(kallsyms_build_id)) == 0) {
1636 if (dso__build_id_equal(self, kallsyms_build_id)) {
1637 kallsyms_filename = "/proc/kallsyms";
1642 * Now look if we have it on the build-id cache in
1643 * $HOME/.debug/[kernel.kallsyms].
1645 build_id__sprintf(self->build_id, sizeof(self->build_id),
1648 if (asprintf(&kallsyms_allocated_filename,
1649 "%s/.debug/[kernel.kallsyms]/%s",
1650 getenv("HOME"), sbuild_id) == -1)
1653 if (access(kallsyms_filename, F_OK)) {
1654 free(kallsyms_allocated_filename);
1658 kallsyms_filename = kallsyms_allocated_filename;
1661 * Last resort, if we don't have a build-id and couldn't find
1662 * any vmlinux file, try the running kernel kallsyms table.
1664 kallsyms_filename = "/proc/kallsyms";
1668 err = dso__load_kallsyms(self, kallsyms_filename, map, session, filter);
1669 free(kallsyms_allocated_filename);
1674 if (kallsyms_filename != NULL)
1675 dso__set_long_name(self, strdup("[kernel.kallsyms]"));
1676 map__fixup_start(map);
1677 map__fixup_end(map);
1683 LIST_HEAD(dsos__user);
1684 LIST_HEAD(dsos__kernel);
1687 static void dsos__add(struct list_head *head, struct dso *dso)
1689 list_add_tail(&dso->node, head);
1692 static struct dso *dsos__find(struct list_head *head, const char *name)
1696 list_for_each_entry(pos, head, node)
1697 if (strcmp(pos->long_name, name) == 0)
1702 struct dso *__dsos__findnew(struct list_head *head, const char *name)
1704 struct dso *dso = dsos__find(head, name);
1707 dso = dso__new(name);
1709 dsos__add(head, dso);
1710 dso__set_basename(dso);
1717 static void __dsos__fprintf(struct list_head *head, FILE *fp)
1721 list_for_each_entry(pos, head, node) {
1723 for (i = 0; i < MAP__NR_TYPES; ++i)
1724 dso__fprintf(pos, i, fp);
1728 void dsos__fprintf(FILE *fp)
1730 __dsos__fprintf(&dsos__kernel, fp);
1731 __dsos__fprintf(&dsos__user, fp);
1734 static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
1740 list_for_each_entry(pos, head, node) {
1741 if (with_hits && !pos->hit)
1743 ret += dso__fprintf_buildid(pos, fp);
1744 ret += fprintf(fp, " %s\n", pos->long_name);
1749 size_t dsos__fprintf_buildid(FILE *fp, bool with_hits)
1751 return (__dsos__fprintf_buildid(&dsos__kernel, fp, with_hits) +
1752 __dsos__fprintf_buildid(&dsos__user, fp, with_hits));
1755 static struct dso *dsos__create_kernel(const char *vmlinux)
1757 struct dso *kernel = dso__new(vmlinux ?: "[kernel.kallsyms]");
1762 kernel->short_name = "[kernel]";
1765 vdso = dso__new("[vdso]");
1767 goto out_delete_kernel_dso;
1768 dso__set_loaded(vdso, MAP__FUNCTION);
1770 if (sysfs__read_build_id("/sys/kernel/notes", kernel->build_id,
1771 sizeof(kernel->build_id)) == 0)
1772 kernel->has_build_id = true;
1774 dsos__add(&dsos__kernel, kernel);
1775 dsos__add(&dsos__user, vdso);
1779 out_delete_kernel_dso:
1780 dso__delete(kernel);
1784 int __map_groups__create_kernel_maps(struct map_groups *self,
1785 struct map *vmlinux_maps[MAP__NR_TYPES],
1790 for (type = 0; type < MAP__NR_TYPES; ++type) {
1791 vmlinux_maps[type] = map__new2(0, kernel, type);
1792 if (vmlinux_maps[type] == NULL)
1795 vmlinux_maps[type]->map_ip =
1796 vmlinux_maps[type]->unmap_ip = identity__map_ip;
1797 map_groups__insert(self, vmlinux_maps[type]);
1803 static int map_groups__create_kernel_maps(struct map_groups *self,
1804 struct map *vmlinux_maps[MAP__NR_TYPES],
1805 const char *vmlinux)
1807 struct dso *kernel = dsos__create_kernel(vmlinux);
1812 return __map_groups__create_kernel_maps(self, vmlinux_maps, kernel);
1815 static void vmlinux_path__exit(void)
1817 while (--vmlinux_path__nr_entries >= 0) {
1818 free(vmlinux_path[vmlinux_path__nr_entries]);
1819 vmlinux_path[vmlinux_path__nr_entries] = NULL;
1823 vmlinux_path = NULL;
1826 static int vmlinux_path__init(void)
1831 if (uname(&uts) < 0)
1834 vmlinux_path = malloc(sizeof(char *) * 5);
1835 if (vmlinux_path == NULL)
1838 vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
1839 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1841 ++vmlinux_path__nr_entries;
1842 vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
1843 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1845 ++vmlinux_path__nr_entries;
1846 snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
1847 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1848 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1850 ++vmlinux_path__nr_entries;
1851 snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
1852 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1853 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1855 ++vmlinux_path__nr_entries;
1856 snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
1858 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1859 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1861 ++vmlinux_path__nr_entries;
1866 vmlinux_path__exit();
1870 static int setup_list(struct strlist **list, const char *list_str,
1871 const char *list_name)
1873 if (list_str == NULL)
1876 *list = strlist__new(true, list_str);
1878 pr_err("problems parsing %s list\n", list_name);
1884 int symbol__init(void)
1886 elf_version(EV_CURRENT);
1887 if (symbol_conf.sort_by_name)
1888 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
1889 sizeof(struct symbol));
1891 if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
1894 if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
1895 pr_err("'.' is the only non valid --field-separator argument\n");
1899 if (setup_list(&symbol_conf.dso_list,
1900 symbol_conf.dso_list_str, "dso") < 0)
1903 if (setup_list(&symbol_conf.comm_list,
1904 symbol_conf.comm_list_str, "comm") < 0)
1905 goto out_free_dso_list;
1907 if (setup_list(&symbol_conf.sym_list,
1908 symbol_conf.sym_list_str, "symbol") < 0)
1909 goto out_free_comm_list;
1914 strlist__delete(symbol_conf.dso_list);
1916 strlist__delete(symbol_conf.comm_list);
1920 int perf_session__create_kernel_maps(struct perf_session *self)
1922 if (map_groups__create_kernel_maps(&self->kmaps, self->vmlinux_maps,
1923 symbol_conf.vmlinux_name) < 0)
1926 if (symbol_conf.use_modules &&
1927 perf_session__create_module_maps(self) < 0)
1928 pr_debug("Failed to load list of modules for session %s, "
1929 "continuing...\n", self->filename);
1931 * Now that we have all the maps created, just set the ->end of them:
1933 map_groups__fixup_end(&self->kmaps);