14 #include <sys/utsname.h>
16 #ifndef NT_GNU_BUILD_ID
17 #define NT_GNU_BUILD_ID 3
31 static void dsos__add(struct list_head *head, struct dso *dso);
32 static struct map *map_groups__find_by_name(struct map_groups *self, char *name);
33 static struct map *map__new2(u64 start, struct dso *dso, enum map_type type);
34 struct symbol *dso__find_symbol(struct dso *self, enum map_type type, u64 addr);
35 static int dso__load_kernel_sym(struct dso *self, struct map *map,
36 struct map_groups *mg, symbol_filter_t filter);
37 unsigned int symbol__priv_size;
38 static int vmlinux_path__nr_entries;
39 static char **vmlinux_path;
41 static struct symbol_conf symbol_conf__defaults = {
43 .try_vmlinux_path = true,
46 static struct map_groups kmaps_mem;
47 struct map_groups *kmaps = &kmaps_mem;
49 bool dso__loaded(const struct dso *self, enum map_type type)
51 return self->loaded & (1 << type);
54 static void dso__set_loaded(struct dso *self, enum map_type type)
56 self->loaded |= (1 << type);
59 static bool symbol_type__is_a(char symbol_type, enum map_type map_type)
63 return symbol_type == 'T' || symbol_type == 'W';
65 return symbol_type == 'D' || symbol_type == 'd';
71 static void symbols__fixup_end(struct rb_root *self)
73 struct rb_node *nd, *prevnd = rb_first(self);
74 struct symbol *curr, *prev;
79 curr = rb_entry(prevnd, struct symbol, rb_node);
81 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
83 curr = rb_entry(nd, struct symbol, rb_node);
85 if (prev->end == prev->start)
86 prev->end = curr->start - 1;
90 if (curr->end == curr->start)
91 curr->end = roundup(curr->start, 4096);
94 static void __map_groups__fixup_end(struct map_groups *self, enum map_type type)
96 struct map *prev, *curr;
97 struct rb_node *nd, *prevnd = rb_first(&self->maps[type]);
102 curr = rb_entry(prevnd, struct map, rb_node);
104 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
106 curr = rb_entry(nd, struct map, rb_node);
107 prev->end = curr->start - 1;
111 * We still haven't the actual symbols, so guess the
112 * last map final address.
117 static void map_groups__fixup_end(struct map_groups *self)
120 for (i = 0; i < MAP__NR_TYPES; ++i)
121 __map_groups__fixup_end(self, i);
124 static struct symbol *symbol__new(u64 start, u64 len, const char *name)
126 size_t namelen = strlen(name) + 1;
127 struct symbol *self = zalloc(symbol__priv_size +
128 sizeof(*self) + namelen);
132 if (symbol__priv_size)
133 self = ((void *)self) + symbol__priv_size;
136 self->end = len ? start + len - 1 : start;
138 pr_debug3("%s: %s %#Lx-%#Lx\n", __func__, name, start, self->end);
140 memcpy(self->name, name, namelen);
145 static void symbol__delete(struct symbol *self)
147 free(((void *)self) - symbol__priv_size);
150 static size_t symbol__fprintf(struct symbol *self, FILE *fp)
152 return fprintf(fp, " %llx-%llx %s\n",
153 self->start, self->end, self->name);
156 static void dso__set_long_name(struct dso *self, char *name)
160 self->long_name = name;
161 self->long_name_len = strlen(name);
164 static void dso__set_basename(struct dso *self)
166 self->short_name = basename(self->long_name);
169 struct dso *dso__new(const char *name)
171 struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
175 strcpy(self->name, name);
176 dso__set_long_name(self, self->name);
177 self->short_name = self->name;
178 for (i = 0; i < MAP__NR_TYPES; ++i)
179 self->symbols[i] = RB_ROOT;
180 self->find_symbol = dso__find_symbol;
181 self->slen_calculated = 0;
182 self->origin = DSO__ORIG_NOT_FOUND;
184 self->has_build_id = 0;
190 static void symbols__delete(struct rb_root *self)
193 struct rb_node *next = rb_first(self);
196 pos = rb_entry(next, struct symbol, rb_node);
197 next = rb_next(&pos->rb_node);
198 rb_erase(&pos->rb_node, self);
203 void dso__delete(struct dso *self)
206 for (i = 0; i < MAP__NR_TYPES; ++i)
207 symbols__delete(&self->symbols[i]);
208 if (self->long_name != self->name)
209 free(self->long_name);
213 void dso__set_build_id(struct dso *self, void *build_id)
215 memcpy(self->build_id, build_id, sizeof(self->build_id));
216 self->has_build_id = 1;
219 static void symbols__insert(struct rb_root *self, struct symbol *sym)
221 struct rb_node **p = &self->rb_node;
222 struct rb_node *parent = NULL;
223 const u64 ip = sym->start;
228 s = rb_entry(parent, struct symbol, rb_node);
234 rb_link_node(&sym->rb_node, parent, p);
235 rb_insert_color(&sym->rb_node, self);
238 static struct symbol *symbols__find(struct rb_root *self, u64 ip)
248 struct symbol *s = rb_entry(n, struct symbol, rb_node);
252 else if (ip > s->end)
261 struct symbol *dso__find_symbol(struct dso *self, enum map_type type, u64 addr)
263 return symbols__find(&self->symbols[type], addr);
266 int build_id__sprintf(u8 *self, int len, char *bf)
272 for (i = 0; i < len; ++i) {
273 sprintf(bid, "%02x", *raw);
281 size_t dso__fprintf_buildid(struct dso *self, FILE *fp)
283 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
285 build_id__sprintf(self->build_id, sizeof(self->build_id), sbuild_id);
286 return fprintf(fp, "%s", sbuild_id);
289 size_t dso__fprintf(struct dso *self, enum map_type type, FILE *fp)
292 size_t ret = fprintf(fp, "dso: %s (", self->short_name);
294 ret += dso__fprintf_buildid(self, fp);
295 ret += fprintf(fp, ")\n");
296 for (nd = rb_first(&self->symbols[type]); nd; nd = rb_next(nd)) {
297 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
298 ret += symbol__fprintf(pos, fp);
305 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
306 * so that we can in the next step set the symbol ->end address and then
307 * call kernel_maps__split_kallsyms.
309 static int dso__load_all_kallsyms(struct dso *self, struct map *map)
313 struct rb_root *root = &self->symbols[map->type];
314 FILE *file = fopen("/proc/kallsyms", "r");
319 while (!feof(file)) {
326 line_len = getline(&line, &n, file);
333 line[--line_len] = '\0'; /* \n */
335 len = hex2u64(line, &start);
338 if (len + 2 >= line_len)
341 symbol_type = toupper(line[len]);
342 if (!symbol_type__is_a(symbol_type, map->type))
345 symbol_name = line + len + 2;
347 * Will fix up the end later, when we have all symbols sorted.
349 sym = symbol__new(start, 0, symbol_name);
352 goto out_delete_line;
354 * We will pass the symbols to the filter later, in
355 * map__split_kallsyms, when we have split the maps per module
357 symbols__insert(root, sym);
372 * Split the symbols into maps, making sure there are no overlaps, i.e. the
373 * kernel range is broken in several maps, named [kernel].N, as we don't have
374 * the original ELF section names vmlinux have.
376 static int dso__split_kallsyms(struct dso *self, struct map *map,
377 struct map_groups *mg, symbol_filter_t filter)
379 struct map *curr_map = map;
382 struct rb_root *root = &self->symbols[map->type];
383 struct rb_node *next = rb_first(root);
384 int kernel_range = 0;
389 pos = rb_entry(next, struct symbol, rb_node);
390 next = rb_next(&pos->rb_node);
392 module = strchr(pos->name, '\t');
394 if (!mg->use_modules)
399 if (strcmp(self->name, module)) {
400 curr_map = map_groups__find_by_name(mg, module);
401 if (curr_map == NULL) {
402 pr_debug("/proc/{kallsyms,modules} "
408 * So that we look just like we get from .ko files,
409 * i.e. not prelinked, relative to map->start.
411 pos->start = curr_map->map_ip(curr_map, pos->start);
412 pos->end = curr_map->map_ip(curr_map, pos->end);
413 } else if (curr_map != map) {
414 char dso_name[PATH_MAX];
417 snprintf(dso_name, sizeof(dso_name), "[kernel].%d",
420 dso = dso__new(dso_name);
424 curr_map = map__new2(pos->start, dso, map->type);
430 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
431 map_groups__insert(mg, curr_map);
435 if (filter && filter(curr_map, pos)) {
436 discard_symbol: rb_erase(&pos->rb_node, root);
439 if (curr_map != map) {
440 rb_erase(&pos->rb_node, root);
441 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
451 static int dso__load_kallsyms(struct dso *self, struct map *map,
452 struct map_groups *mg, symbol_filter_t filter)
454 if (dso__load_all_kallsyms(self, map) < 0)
457 symbols__fixup_end(&self->symbols[map->type]);
458 self->origin = DSO__ORIG_KERNEL;
460 return dso__split_kallsyms(self, map, mg, filter);
463 size_t kernel_maps__fprintf(FILE *fp)
465 size_t printed = fprintf(fp, "Kernel maps:\n");
466 printed += map_groups__fprintf_maps(kmaps, fp);
467 return printed + fprintf(fp, "END kernel maps\n");
470 static int dso__load_perf_map(struct dso *self, struct map *map,
471 symbol_filter_t filter)
478 file = fopen(self->long_name, "r");
482 while (!feof(file)) {
487 line_len = getline(&line, &n, file);
494 line[--line_len] = '\0'; /* \n */
496 len = hex2u64(line, &start);
499 if (len + 2 >= line_len)
502 len += hex2u64(line + len, &size);
505 if (len + 2 >= line_len)
508 sym = symbol__new(start, size, line + len);
511 goto out_delete_line;
513 if (filter && filter(map, sym))
516 symbols__insert(&self->symbols[map->type], sym);
533 * elf_symtab__for_each_symbol - iterate thru all the symbols
535 * @self: struct elf_symtab instance to iterate
537 * @sym: GElf_Sym iterator
539 #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
540 for (idx = 0, gelf_getsym(syms, idx, &sym);\
542 idx++, gelf_getsym(syms, idx, &sym))
544 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
546 return GELF_ST_TYPE(sym->st_info);
549 static inline int elf_sym__is_function(const GElf_Sym *sym)
551 return elf_sym__type(sym) == STT_FUNC &&
553 sym->st_shndx != SHN_UNDEF;
556 static inline bool elf_sym__is_object(const GElf_Sym *sym)
558 return elf_sym__type(sym) == STT_OBJECT &&
560 sym->st_shndx != SHN_UNDEF;
563 static inline int elf_sym__is_label(const GElf_Sym *sym)
565 return elf_sym__type(sym) == STT_NOTYPE &&
567 sym->st_shndx != SHN_UNDEF &&
568 sym->st_shndx != SHN_ABS;
571 static inline const char *elf_sec__name(const GElf_Shdr *shdr,
572 const Elf_Data *secstrs)
574 return secstrs->d_buf + shdr->sh_name;
577 static inline int elf_sec__is_text(const GElf_Shdr *shdr,
578 const Elf_Data *secstrs)
580 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
583 static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
584 const Elf_Data *secstrs)
586 return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
589 static inline const char *elf_sym__name(const GElf_Sym *sym,
590 const Elf_Data *symstrs)
592 return symstrs->d_buf + sym->st_name;
595 static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
596 GElf_Shdr *shp, const char *name,
602 while ((sec = elf_nextscn(elf, sec)) != NULL) {
605 gelf_getshdr(sec, shp);
606 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
607 if (!strcmp(name, str)) {
618 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
619 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
621 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
623 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
624 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
626 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
629 * We need to check if we have a .dynsym, so that we can handle the
630 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
631 * .dynsym or .symtab).
632 * And always look at the original dso, not at debuginfo packages, that
633 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
635 static int dso__synthesize_plt_symbols(struct dso *self, struct map *map,
636 symbol_filter_t filter)
638 uint32_t nr_rel_entries, idx;
643 GElf_Shdr shdr_rel_plt, shdr_dynsym;
644 Elf_Data *reldata, *syms, *symstrs;
645 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
648 char sympltname[1024];
650 int nr = 0, symidx, fd, err = 0;
652 fd = open(self->long_name, O_RDONLY);
656 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
660 if (gelf_getehdr(elf, &ehdr) == NULL)
663 scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
664 ".dynsym", &dynsym_idx);
665 if (scn_dynsym == NULL)
668 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
670 if (scn_plt_rel == NULL) {
671 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
673 if (scn_plt_rel == NULL)
679 if (shdr_rel_plt.sh_link != dynsym_idx)
682 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
686 * Fetch the relocation section to find the idxes to the GOT
687 * and the symbols in the .dynsym they refer to.
689 reldata = elf_getdata(scn_plt_rel, NULL);
693 syms = elf_getdata(scn_dynsym, NULL);
697 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
698 if (scn_symstrs == NULL)
701 symstrs = elf_getdata(scn_symstrs, NULL);
705 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
706 plt_offset = shdr_plt.sh_offset;
708 if (shdr_rel_plt.sh_type == SHT_RELA) {
709 GElf_Rela pos_mem, *pos;
711 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
713 symidx = GELF_R_SYM(pos->r_info);
714 plt_offset += shdr_plt.sh_entsize;
715 gelf_getsym(syms, symidx, &sym);
716 snprintf(sympltname, sizeof(sympltname),
717 "%s@plt", elf_sym__name(&sym, symstrs));
719 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
724 if (filter && filter(map, f))
727 symbols__insert(&self->symbols[map->type], f);
731 } else if (shdr_rel_plt.sh_type == SHT_REL) {
732 GElf_Rel pos_mem, *pos;
733 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
735 symidx = GELF_R_SYM(pos->r_info);
736 plt_offset += shdr_plt.sh_entsize;
737 gelf_getsym(syms, symidx, &sym);
738 snprintf(sympltname, sizeof(sympltname),
739 "%s@plt", elf_sym__name(&sym, symstrs));
741 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
746 if (filter && filter(map, f))
749 symbols__insert(&self->symbols[map->type], f);
764 pr_warning("%s: problems reading %s PLT info.\n",
765 __func__, self->long_name);
769 static bool elf_sym__is_a(GElf_Sym *self, enum map_type type)
773 return elf_sym__is_function(self);
775 return elf_sym__is_object(self);
781 static bool elf_sec__is_a(GElf_Shdr *self, Elf_Data *secstrs, enum map_type type)
785 return elf_sec__is_text(self, secstrs);
787 return elf_sec__is_data(self, secstrs);
793 static int dso__load_sym(struct dso *self, struct map *map,
794 struct map_groups *mg, const char *name, int fd,
795 symbol_filter_t filter, int kernel, int kmodule)
797 struct map *curr_map = map;
798 struct dso *curr_dso = self;
799 size_t dso_name_len = strlen(self->short_name);
800 Elf_Data *symstrs, *secstrs;
808 Elf_Scn *sec, *sec_strndx;
812 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
814 pr_err("%s: cannot read %s ELF file.\n", __func__, name);
818 if (gelf_getehdr(elf, &ehdr) == NULL) {
819 pr_err("%s: cannot get elf header.\n", __func__);
823 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
825 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
830 syms = elf_getdata(sec, NULL);
834 sec = elf_getscn(elf, shdr.sh_link);
838 symstrs = elf_getdata(sec, NULL);
842 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
843 if (sec_strndx == NULL)
846 secstrs = elf_getdata(sec_strndx, NULL);
850 nr_syms = shdr.sh_size / shdr.sh_entsize;
852 memset(&sym, 0, sizeof(sym));
854 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
855 elf_section_by_name(elf, &ehdr, &shdr,
858 } else self->adjust_symbols = 0;
860 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
862 const char *elf_name;
863 char *demangled = NULL;
864 int is_label = elf_sym__is_label(&sym);
865 const char *section_name;
867 if (!is_label && !elf_sym__is_a(&sym, map->type))
870 sec = elf_getscn(elf, sym.st_shndx);
874 gelf_getshdr(sec, &shdr);
876 if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type))
879 elf_name = elf_sym__name(&sym, symstrs);
880 section_name = elf_sec__name(&shdr, secstrs);
882 if (kernel || kmodule) {
883 char dso_name[PATH_MAX];
885 if (strcmp(section_name,
886 curr_dso->short_name + dso_name_len) == 0)
889 if (strcmp(section_name, ".text") == 0) {
895 snprintf(dso_name, sizeof(dso_name),
896 "%s%s", self->short_name, section_name);
898 curr_map = map_groups__find_by_name(mg, dso_name);
899 if (curr_map == NULL) {
900 u64 start = sym.st_value;
903 start += map->start + shdr.sh_offset;
905 curr_dso = dso__new(dso_name);
906 if (curr_dso == NULL)
908 curr_map = map__new2(start, curr_dso,
910 if (curr_map == NULL) {
911 dso__delete(curr_dso);
914 curr_map->map_ip = identity__map_ip;
915 curr_map->unmap_ip = identity__map_ip;
916 curr_dso->origin = DSO__ORIG_KERNEL;
917 map_groups__insert(kmaps, curr_map);
918 dsos__add(&dsos__kernel, curr_dso);
920 curr_dso = curr_map->dso;
925 if (curr_dso->adjust_symbols) {
926 pr_debug2("adjusting symbol: st_value: %Lx sh_addr: "
927 "%Lx sh_offset: %Lx\n", (u64)sym.st_value,
928 (u64)shdr.sh_addr, (u64)shdr.sh_offset);
929 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
932 * We need to figure out if the object was created from C++ sources
933 * DWARF DW_compile_unit has this, but we don't always have access
936 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
937 if (demangled != NULL)
938 elf_name = demangled;
940 f = symbol__new(sym.st_value, sym.st_size, elf_name);
945 if (filter && filter(curr_map, f))
948 symbols__insert(&curr_dso->symbols[curr_map->type], f);
954 * For misannotated, zeroed, ASM function sizes.
957 symbols__fixup_end(&self->symbols[map->type]);
965 static bool dso__build_id_equal(const struct dso *self, u8 *build_id)
967 return memcmp(self->build_id, build_id, sizeof(self->build_id)) == 0;
970 static bool __dsos__read_build_ids(struct list_head *head)
972 bool have_build_id = false;
975 list_for_each_entry(pos, head, node)
976 if (filename__read_build_id(pos->long_name, pos->build_id,
977 sizeof(pos->build_id)) > 0) {
978 have_build_id = true;
979 pos->has_build_id = true;
982 return have_build_id;
985 bool dsos__read_build_ids(void)
987 bool kbuildids = __dsos__read_build_ids(&dsos__kernel),
988 ubuildids = __dsos__read_build_ids(&dsos__user);
989 return kbuildids || ubuildids;
993 * Align offset to 4 bytes as needed for note name and descriptor data.
995 #define NOTE_ALIGN(n) (((n) + 3) & -4U)
997 int filename__read_build_id(const char *filename, void *bf, size_t size)
1008 if (size < BUILD_ID_SIZE)
1011 fd = open(filename, O_RDONLY);
1015 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1017 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
1022 if (ek != ELF_K_ELF)
1025 if (gelf_getehdr(elf, &ehdr) == NULL) {
1026 pr_err("%s: cannot get elf header.\n", __func__);
1030 sec = elf_section_by_name(elf, &ehdr, &shdr,
1031 ".note.gnu.build-id", NULL);
1033 sec = elf_section_by_name(elf, &ehdr, &shdr,
1039 data = elf_getdata(sec, NULL);
1044 while (ptr < (data->d_buf + data->d_size)) {
1045 GElf_Nhdr *nhdr = ptr;
1046 int namesz = NOTE_ALIGN(nhdr->n_namesz),
1047 descsz = NOTE_ALIGN(nhdr->n_descsz);
1050 ptr += sizeof(*nhdr);
1053 if (nhdr->n_type == NT_GNU_BUILD_ID &&
1054 nhdr->n_namesz == sizeof("GNU")) {
1055 if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
1056 memcpy(bf, ptr, BUILD_ID_SIZE);
1057 err = BUILD_ID_SIZE;
1071 int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
1075 if (size < BUILD_ID_SIZE)
1078 fd = open(filename, O_RDONLY);
1087 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
1090 namesz = NOTE_ALIGN(nhdr.n_namesz);
1091 descsz = NOTE_ALIGN(nhdr.n_descsz);
1092 if (nhdr.n_type == NT_GNU_BUILD_ID &&
1093 nhdr.n_namesz == sizeof("GNU")) {
1094 if (read(fd, bf, namesz) != namesz)
1096 if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
1097 if (read(fd, build_id,
1098 BUILD_ID_SIZE) == BUILD_ID_SIZE) {
1102 } else if (read(fd, bf, descsz) != descsz)
1105 int n = namesz + descsz;
1106 if (read(fd, bf, n) != n)
1115 char dso__symtab_origin(const struct dso *self)
1117 static const char origin[] = {
1118 [DSO__ORIG_KERNEL] = 'k',
1119 [DSO__ORIG_JAVA_JIT] = 'j',
1120 [DSO__ORIG_FEDORA] = 'f',
1121 [DSO__ORIG_UBUNTU] = 'u',
1122 [DSO__ORIG_BUILDID] = 'b',
1123 [DSO__ORIG_DSO] = 'd',
1124 [DSO__ORIG_KMODULE] = 'K',
1127 if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
1129 return origin[self->origin];
1132 int dso__load(struct dso *self, struct map *map, symbol_filter_t filter)
1134 int size = PATH_MAX;
1136 u8 build_id[BUILD_ID_SIZE];
1140 dso__set_loaded(self, map->type);
1143 return dso__load_kernel_sym(self, map, kmaps, filter);
1145 name = malloc(size);
1149 self->adjust_symbols = 0;
1151 if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
1152 ret = dso__load_perf_map(self, map, filter);
1153 self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
1154 DSO__ORIG_NOT_FOUND;
1158 self->origin = DSO__ORIG_FEDORA - 1;
1163 switch (self->origin) {
1164 case DSO__ORIG_FEDORA:
1165 snprintf(name, size, "/usr/lib/debug%s.debug",
1168 case DSO__ORIG_UBUNTU:
1169 snprintf(name, size, "/usr/lib/debug%s",
1172 case DSO__ORIG_BUILDID:
1173 if (filename__read_build_id(self->long_name, build_id,
1174 sizeof(build_id))) {
1175 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
1177 build_id__sprintf(build_id, sizeof(build_id),
1179 snprintf(name, size,
1180 "/usr/lib/debug/.build-id/%.2s/%s.debug",
1181 build_id_hex, build_id_hex + 2);
1182 if (self->has_build_id)
1183 goto compare_build_id;
1189 snprintf(name, size, "%s", self->long_name);
1196 if (self->has_build_id) {
1197 if (filename__read_build_id(name, build_id,
1198 sizeof(build_id)) < 0)
1201 if (!dso__build_id_equal(self, build_id))
1205 fd = open(name, O_RDONLY);
1208 ret = dso__load_sym(self, map, NULL, name, fd, filter, 0, 0);
1212 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
1218 int nr_plt = dso__synthesize_plt_symbols(self, map, filter);
1224 if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
1229 static struct map *map_groups__find_by_name(struct map_groups *self, char *name)
1233 for (nd = rb_first(&self->maps[MAP__FUNCTION]); nd; nd = rb_next(nd)) {
1234 struct map *map = rb_entry(nd, struct map, rb_node);
1236 if (map->dso && strcmp(map->dso->name, name) == 0)
1243 static int dsos__set_modules_path_dir(char *dirname)
1245 struct dirent *dent;
1246 DIR *dir = opendir(dirname);
1249 pr_debug("%s: cannot open %s dir\n", __func__, dirname);
1253 while ((dent = readdir(dir)) != NULL) {
1254 char path[PATH_MAX];
1256 if (dent->d_type == DT_DIR) {
1257 if (!strcmp(dent->d_name, ".") ||
1258 !strcmp(dent->d_name, ".."))
1261 snprintf(path, sizeof(path), "%s/%s",
1262 dirname, dent->d_name);
1263 if (dsos__set_modules_path_dir(path) < 0)
1266 char *dot = strrchr(dent->d_name, '.'),
1271 if (dot == NULL || strcmp(dot, ".ko"))
1273 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1274 (int)(dot - dent->d_name), dent->d_name);
1276 strxfrchar(dso_name, '-', '_');
1277 map = map_groups__find_by_name(kmaps, dso_name);
1281 snprintf(path, sizeof(path), "%s/%s",
1282 dirname, dent->d_name);
1284 long_name = strdup(path);
1285 if (long_name == NULL)
1287 dso__set_long_name(map->dso, long_name);
1297 static int dsos__set_modules_path(void)
1300 char modules_path[PATH_MAX];
1302 if (uname(&uts) < 0)
1305 snprintf(modules_path, sizeof(modules_path), "/lib/modules/%s/kernel",
1308 return dsos__set_modules_path_dir(modules_path);
1312 * Constructor variant for modules (where we know from /proc/modules where
1313 * they are loaded) and for vmlinux, where only after we load all the
1314 * symbols we'll know where it starts and ends.
1316 static struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
1318 struct map *self = malloc(sizeof(*self));
1322 * ->end will be filled after we load all the symbols
1324 map__init(self, type, start, 0, 0, dso);
1330 static int map_groups__create_module_maps(struct map_groups *self)
1334 FILE *file = fopen("/proc/modules", "r");
1340 while (!feof(file)) {
1341 char name[PATH_MAX];
1347 line_len = getline(&line, &n, file);
1354 line[--line_len] = '\0'; /* \n */
1356 sep = strrchr(line, 'x');
1360 hex2u64(sep + 1, &start);
1362 sep = strchr(line, ' ');
1368 snprintf(name, sizeof(name), "[%s]", line);
1369 dso = dso__new(name);
1372 goto out_delete_line;
1374 map = map__new2(start, dso, MAP__FUNCTION);
1377 goto out_delete_line;
1380 snprintf(name, sizeof(name),
1381 "/sys/module/%s/notes/.note.gnu.build-id", line);
1382 if (sysfs__read_build_id(name, dso->build_id,
1383 sizeof(dso->build_id)) == 0)
1384 dso->has_build_id = true;
1386 dso->origin = DSO__ORIG_KMODULE;
1387 map_groups__insert(self, map);
1388 dsos__add(&dsos__kernel, dso);
1394 return dsos__set_modules_path();
1402 static int dso__load_vmlinux(struct dso *self, struct map *map,
1403 struct map_groups *mg,
1404 const char *vmlinux, symbol_filter_t filter)
1408 if (self->has_build_id) {
1409 u8 build_id[BUILD_ID_SIZE];
1411 if (filename__read_build_id(vmlinux, build_id,
1412 sizeof(build_id)) < 0) {
1413 pr_debug("No build_id in %s, ignoring it\n", vmlinux);
1416 if (!dso__build_id_equal(self, build_id)) {
1417 char expected_build_id[BUILD_ID_SIZE * 2 + 1],
1418 vmlinux_build_id[BUILD_ID_SIZE * 2 + 1];
1420 build_id__sprintf(self->build_id,
1421 sizeof(self->build_id),
1423 build_id__sprintf(build_id, sizeof(build_id),
1425 pr_debug("build_id in %s is %s while expected is %s, "
1426 "ignoring it\n", vmlinux, vmlinux_build_id,
1432 fd = open(vmlinux, O_RDONLY);
1436 dso__set_loaded(self, map->type);
1437 err = dso__load_sym(self, map, mg, self->long_name, fd, filter, 1, 0);
1443 static int dso__load_kernel_sym(struct dso *self, struct map *map,
1444 struct map_groups *mg, symbol_filter_t filter)
1449 if (vmlinux_path != NULL) {
1451 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1452 vmlinux_path__nr_entries);
1453 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
1454 err = dso__load_vmlinux(self, map, mg,
1455 vmlinux_path[i], filter);
1457 pr_debug("Using %s for symbols\n",
1459 dso__set_long_name(self,
1460 strdup(vmlinux_path[i]));
1466 is_kallsyms = self->long_name[0] == '[';
1470 err = dso__load_vmlinux(self, map, mg, self->long_name, filter);
1472 pr_info("The file %s cannot be used, "
1473 "trying to use /proc/kallsyms...", self->long_name);
1475 err = dso__load_kallsyms(self, map, mg, filter);
1476 if (err > 0 && !is_kallsyms)
1477 dso__set_long_name(self, strdup("[kernel.kallsyms]"));
1482 map__fixup_start(map);
1483 map__fixup_end(map);
1489 LIST_HEAD(dsos__user);
1490 LIST_HEAD(dsos__kernel);
1493 static void dsos__add(struct list_head *head, struct dso *dso)
1495 list_add_tail(&dso->node, head);
1498 static struct dso *dsos__find(struct list_head *head, const char *name)
1502 list_for_each_entry(pos, head, node)
1503 if (strcmp(pos->name, name) == 0)
1508 struct dso *dsos__findnew(const char *name)
1510 struct dso *dso = dsos__find(&dsos__user, name);
1513 dso = dso__new(name);
1515 dsos__add(&dsos__user, dso);
1516 dso__set_basename(dso);
1523 static void __dsos__fprintf(struct list_head *head, FILE *fp)
1527 list_for_each_entry(pos, head, node) {
1529 for (i = 0; i < MAP__NR_TYPES; ++i)
1530 dso__fprintf(pos, i, fp);
1534 void dsos__fprintf(FILE *fp)
1536 __dsos__fprintf(&dsos__kernel, fp);
1537 __dsos__fprintf(&dsos__user, fp);
1540 static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp)
1545 list_for_each_entry(pos, head, node) {
1546 ret += dso__fprintf_buildid(pos, fp);
1547 ret += fprintf(fp, " %s\n", pos->long_name);
1552 size_t dsos__fprintf_buildid(FILE *fp)
1554 return (__dsos__fprintf_buildid(&dsos__kernel, fp) +
1555 __dsos__fprintf_buildid(&dsos__user, fp));
1558 static struct dso *dsos__create_kernel( const char *vmlinux)
1560 struct dso *kernel = dso__new(vmlinux ?: "[kernel.kallsyms]");
1565 kernel->short_name = "[kernel]";
1568 vdso = dso__new("[vdso]");
1570 goto out_delete_kernel_dso;
1571 dso__set_loaded(vdso, MAP__FUNCTION);
1573 if (sysfs__read_build_id("/sys/kernel/notes", kernel->build_id,
1574 sizeof(kernel->build_id)) == 0)
1575 kernel->has_build_id = true;
1577 dsos__add(&dsos__kernel, kernel);
1578 dsos__add(&dsos__user, vdso);
1582 out_delete_kernel_dso:
1583 dso__delete(kernel);
1587 static int map_groups__create_kernel_maps(struct map_groups *self, const char *vmlinux)
1589 struct map *functions, *variables;
1590 struct dso *kernel = dsos__create_kernel(vmlinux);
1595 functions = map__new2(0, kernel, MAP__FUNCTION);
1596 if (functions == NULL)
1599 variables = map__new2(0, kernel, MAP__VARIABLE);
1600 if (variables == NULL) {
1601 map__delete(functions);
1605 functions->map_ip = functions->unmap_ip =
1606 variables->map_ip = variables->unmap_ip = identity__map_ip;
1607 map_groups__insert(self, functions);
1608 map_groups__insert(self, variables);
1613 static void vmlinux_path__exit(void)
1615 while (--vmlinux_path__nr_entries >= 0) {
1616 free(vmlinux_path[vmlinux_path__nr_entries]);
1617 vmlinux_path[vmlinux_path__nr_entries] = NULL;
1621 vmlinux_path = NULL;
1624 static int vmlinux_path__init(void)
1629 if (uname(&uts) < 0)
1632 vmlinux_path = malloc(sizeof(char *) * 5);
1633 if (vmlinux_path == NULL)
1636 vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
1637 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1639 ++vmlinux_path__nr_entries;
1640 vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
1641 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1643 ++vmlinux_path__nr_entries;
1644 snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
1645 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1646 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1648 ++vmlinux_path__nr_entries;
1649 snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
1650 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1651 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1653 ++vmlinux_path__nr_entries;
1654 snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
1656 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1657 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1659 ++vmlinux_path__nr_entries;
1664 vmlinux_path__exit();
1668 int symbol__init(struct symbol_conf *conf)
1670 const struct symbol_conf *pconf = conf ?: &symbol_conf__defaults;
1672 elf_version(EV_CURRENT);
1673 symbol__priv_size = pconf->priv_size;
1674 map_groups__init(kmaps);
1676 if (pconf->try_vmlinux_path && vmlinux_path__init() < 0)
1679 if (map_groups__create_kernel_maps(kmaps, pconf->vmlinux_name) < 0) {
1680 vmlinux_path__exit();
1684 kmaps->use_modules = pconf->use_modules;
1685 if (pconf->use_modules && map_groups__create_module_maps(kmaps) < 0)
1686 pr_debug("Failed to load list of modules in use, "
1689 * Now that we have all the maps created, just set the ->end of them:
1691 map_groups__fixup_end(kmaps);