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 *thread__find_map_by_name(struct thread *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 thread *thread, 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 thread kthread_mem;
47 struct thread *kthread = &kthread_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 void symbols__fixup_end(struct rb_root *self)
61 struct rb_node *nd, *prevnd = rb_first(self);
62 struct symbol *curr, *prev;
67 curr = rb_entry(prevnd, struct symbol, rb_node);
69 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
71 curr = rb_entry(nd, struct symbol, rb_node);
73 if (prev->end == prev->start)
74 prev->end = curr->start - 1;
78 if (curr->end == curr->start)
79 curr->end = roundup(curr->start, 4096);
82 static void __thread__fixup_maps_end(struct thread *self, enum map_type type)
84 struct map *prev, *curr;
85 struct rb_node *nd, *prevnd = rb_first(&self->maps[type]);
90 curr = rb_entry(prevnd, struct map, rb_node);
92 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
94 curr = rb_entry(nd, struct map, rb_node);
95 prev->end = curr->start - 1;
99 * We still haven't the actual symbols, so guess the
100 * last map final address.
105 static void thread__fixup_maps_end(struct thread *self)
108 for (i = 0; i < MAP__NR_TYPES; ++i)
109 __thread__fixup_maps_end(self, i);
112 static struct symbol *symbol__new(u64 start, u64 len, const char *name)
114 size_t namelen = strlen(name) + 1;
115 struct symbol *self = zalloc(symbol__priv_size +
116 sizeof(*self) + namelen);
120 if (symbol__priv_size)
121 self = ((void *)self) + symbol__priv_size;
124 self->end = len ? start + len - 1 : start;
126 pr_debug3("%s: %s %#Lx-%#Lx\n", __func__, name, start, self->end);
128 memcpy(self->name, name, namelen);
133 static void symbol__delete(struct symbol *self)
135 free(((void *)self) - symbol__priv_size);
138 static size_t symbol__fprintf(struct symbol *self, FILE *fp)
140 return fprintf(fp, " %llx-%llx %s\n",
141 self->start, self->end, self->name);
144 static void dso__set_long_name(struct dso *self, char *name)
148 self->long_name = name;
149 self->long_name_len = strlen(name);
152 static void dso__set_basename(struct dso *self)
154 self->short_name = basename(self->long_name);
157 struct dso *dso__new(const char *name)
159 struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
163 strcpy(self->name, name);
164 dso__set_long_name(self, self->name);
165 self->short_name = self->name;
166 for (i = 0; i < MAP__NR_TYPES; ++i)
167 self->symbols[i] = RB_ROOT;
168 self->find_symbol = dso__find_symbol;
169 self->slen_calculated = 0;
170 self->origin = DSO__ORIG_NOT_FOUND;
172 self->has_build_id = 0;
178 static void symbols__delete(struct rb_root *self)
181 struct rb_node *next = rb_first(self);
184 pos = rb_entry(next, struct symbol, rb_node);
185 next = rb_next(&pos->rb_node);
186 rb_erase(&pos->rb_node, self);
191 void dso__delete(struct dso *self)
194 for (i = 0; i < MAP__NR_TYPES; ++i)
195 symbols__delete(&self->symbols[i]);
196 if (self->long_name != self->name)
197 free(self->long_name);
201 void dso__set_build_id(struct dso *self, void *build_id)
203 memcpy(self->build_id, build_id, sizeof(self->build_id));
204 self->has_build_id = 1;
207 static void symbols__insert(struct rb_root *self, struct symbol *sym)
209 struct rb_node **p = &self->rb_node;
210 struct rb_node *parent = NULL;
211 const u64 ip = sym->start;
216 s = rb_entry(parent, struct symbol, rb_node);
222 rb_link_node(&sym->rb_node, parent, p);
223 rb_insert_color(&sym->rb_node, self);
226 static struct symbol *symbols__find(struct rb_root *self, u64 ip)
236 struct symbol *s = rb_entry(n, struct symbol, rb_node);
240 else if (ip > s->end)
249 struct symbol *dso__find_symbol(struct dso *self, enum map_type type, u64 addr)
251 return symbols__find(&self->symbols[type], addr);
254 int build_id__sprintf(u8 *self, int len, char *bf)
260 for (i = 0; i < len; ++i) {
261 sprintf(bid, "%02x", *raw);
269 size_t dso__fprintf_buildid(struct dso *self, FILE *fp)
271 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
273 build_id__sprintf(self->build_id, sizeof(self->build_id), sbuild_id);
274 return fprintf(fp, "%s", sbuild_id);
277 size_t dso__fprintf(struct dso *self, enum map_type type, FILE *fp)
280 size_t ret = fprintf(fp, "dso: %s (", self->short_name);
282 ret += dso__fprintf_buildid(self, fp);
283 ret += fprintf(fp, ")\n");
284 for (nd = rb_first(&self->symbols[type]); nd; nd = rb_next(nd)) {
285 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
286 ret += symbol__fprintf(pos, fp);
293 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
294 * so that we can in the next step set the symbol ->end address and then
295 * call kernel_maps__split_kallsyms.
297 static int dso__load_all_kallsyms(struct dso *self, struct map *map)
301 struct rb_root *root = &self->symbols[map->type];
302 FILE *file = fopen("/proc/kallsyms", "r");
307 while (!feof(file)) {
314 line_len = getline(&line, &n, file);
321 line[--line_len] = '\0'; /* \n */
323 len = hex2u64(line, &start);
326 if (len + 2 >= line_len)
329 symbol_type = toupper(line[len]);
331 * We're interested only in code ('T'ext)
333 if (symbol_type != 'T' && symbol_type != 'W')
336 symbol_name = line + len + 2;
338 * Will fix up the end later, when we have all symbols sorted.
340 sym = symbol__new(start, 0, symbol_name);
343 goto out_delete_line;
345 * We will pass the symbols to the filter later, in
346 * map__split_kallsyms, when we have split the maps per module
348 symbols__insert(root, sym);
363 * Split the symbols into maps, making sure there are no overlaps, i.e. the
364 * kernel range is broken in several maps, named [kernel].N, as we don't have
365 * the original ELF section names vmlinux have.
367 static int dso__split_kallsyms(struct dso *self, struct map *map, struct thread *thread,
368 symbol_filter_t filter)
370 struct map *curr_map = map;
373 struct rb_root *root = &self->symbols[map->type];
374 struct rb_node *next = rb_first(root);
375 int kernel_range = 0;
380 pos = rb_entry(next, struct symbol, rb_node);
381 next = rb_next(&pos->rb_node);
383 module = strchr(pos->name, '\t');
385 if (!thread->use_modules)
390 if (strcmp(self->name, module)) {
391 curr_map = thread__find_map_by_name(thread, module);
392 if (curr_map == NULL) {
393 pr_debug("/proc/{kallsyms,modules} "
399 * So that we look just like we get from .ko files,
400 * i.e. not prelinked, relative to map->start.
402 pos->start = curr_map->map_ip(curr_map, pos->start);
403 pos->end = curr_map->map_ip(curr_map, pos->end);
404 } else if (curr_map != map) {
405 char dso_name[PATH_MAX];
408 snprintf(dso_name, sizeof(dso_name), "[kernel].%d",
411 dso = dso__new(dso_name);
415 curr_map = map__new2(pos->start, dso, map->type);
421 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
422 __thread__insert_map(thread, curr_map);
426 if (filter && filter(curr_map, pos)) {
427 discard_symbol: rb_erase(&pos->rb_node, root);
430 if (curr_map != map) {
431 rb_erase(&pos->rb_node, root);
432 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
442 static int dso__load_kallsyms(struct dso *self, struct map *map,
443 struct thread *thread, symbol_filter_t filter)
445 if (dso__load_all_kallsyms(self, map) < 0)
448 symbols__fixup_end(&self->symbols[map->type]);
449 self->origin = DSO__ORIG_KERNEL;
451 return dso__split_kallsyms(self, map, thread, filter);
454 size_t kernel_maps__fprintf(FILE *fp)
456 size_t printed = fprintf(fp, "Kernel maps:\n");
457 printed += thread__fprintf_maps(kthread, fp);
458 return printed + fprintf(fp, "END kernel maps\n");
461 static int dso__load_perf_map(struct dso *self, struct map *map,
462 symbol_filter_t filter)
469 file = fopen(self->long_name, "r");
473 while (!feof(file)) {
478 line_len = getline(&line, &n, file);
485 line[--line_len] = '\0'; /* \n */
487 len = hex2u64(line, &start);
490 if (len + 2 >= line_len)
493 len += hex2u64(line + len, &size);
496 if (len + 2 >= line_len)
499 sym = symbol__new(start, size, line + len);
502 goto out_delete_line;
504 if (filter && filter(map, sym))
507 symbols__insert(&self->symbols[map->type], sym);
524 * elf_symtab__for_each_symbol - iterate thru all the symbols
526 * @self: struct elf_symtab instance to iterate
528 * @sym: GElf_Sym iterator
530 #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
531 for (idx = 0, gelf_getsym(syms, idx, &sym);\
533 idx++, gelf_getsym(syms, idx, &sym))
535 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
537 return GELF_ST_TYPE(sym->st_info);
540 static inline int elf_sym__is_function(const GElf_Sym *sym)
542 return elf_sym__type(sym) == STT_FUNC &&
544 sym->st_shndx != SHN_UNDEF;
547 static inline int elf_sym__is_label(const GElf_Sym *sym)
549 return elf_sym__type(sym) == STT_NOTYPE &&
551 sym->st_shndx != SHN_UNDEF &&
552 sym->st_shndx != SHN_ABS;
555 static inline const char *elf_sec__name(const GElf_Shdr *shdr,
556 const Elf_Data *secstrs)
558 return secstrs->d_buf + shdr->sh_name;
561 static inline int elf_sec__is_text(const GElf_Shdr *shdr,
562 const Elf_Data *secstrs)
564 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
567 static inline const char *elf_sym__name(const GElf_Sym *sym,
568 const Elf_Data *symstrs)
570 return symstrs->d_buf + sym->st_name;
573 static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
574 GElf_Shdr *shp, const char *name,
580 while ((sec = elf_nextscn(elf, sec)) != NULL) {
583 gelf_getshdr(sec, shp);
584 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
585 if (!strcmp(name, str)) {
596 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
597 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
599 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
601 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
602 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
604 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
607 * We need to check if we have a .dynsym, so that we can handle the
608 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
609 * .dynsym or .symtab).
610 * And always look at the original dso, not at debuginfo packages, that
611 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
613 static int dso__synthesize_plt_symbols(struct dso *self, struct map *map,
614 symbol_filter_t filter)
616 uint32_t nr_rel_entries, idx;
621 GElf_Shdr shdr_rel_plt, shdr_dynsym;
622 Elf_Data *reldata, *syms, *symstrs;
623 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
626 char sympltname[1024];
628 int nr = 0, symidx, fd, err = 0;
630 fd = open(self->long_name, O_RDONLY);
634 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
638 if (gelf_getehdr(elf, &ehdr) == NULL)
641 scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
642 ".dynsym", &dynsym_idx);
643 if (scn_dynsym == NULL)
646 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
648 if (scn_plt_rel == NULL) {
649 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
651 if (scn_plt_rel == NULL)
657 if (shdr_rel_plt.sh_link != dynsym_idx)
660 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
664 * Fetch the relocation section to find the idxes to the GOT
665 * and the symbols in the .dynsym they refer to.
667 reldata = elf_getdata(scn_plt_rel, NULL);
671 syms = elf_getdata(scn_dynsym, NULL);
675 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
676 if (scn_symstrs == NULL)
679 symstrs = elf_getdata(scn_symstrs, NULL);
683 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
684 plt_offset = shdr_plt.sh_offset;
686 if (shdr_rel_plt.sh_type == SHT_RELA) {
687 GElf_Rela pos_mem, *pos;
689 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
691 symidx = GELF_R_SYM(pos->r_info);
692 plt_offset += shdr_plt.sh_entsize;
693 gelf_getsym(syms, symidx, &sym);
694 snprintf(sympltname, sizeof(sympltname),
695 "%s@plt", elf_sym__name(&sym, symstrs));
697 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
702 if (filter && filter(map, f))
705 symbols__insert(&self->symbols[map->type], f);
709 } else if (shdr_rel_plt.sh_type == SHT_REL) {
710 GElf_Rel pos_mem, *pos;
711 elf_section__for_each_rel(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);
742 pr_warning("%s: problems reading %s PLT info.\n",
743 __func__, self->long_name);
747 static int dso__load_sym(struct dso *self, struct map *map,
748 struct thread *thread, const char *name, int fd,
749 symbol_filter_t filter, int kernel, int kmodule)
751 struct map *curr_map = map;
752 struct dso *curr_dso = self;
753 size_t dso_name_len = strlen(self->short_name);
754 Elf_Data *symstrs, *secstrs;
762 Elf_Scn *sec, *sec_strndx;
766 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
768 pr_err("%s: cannot read %s ELF file.\n", __func__, name);
772 if (gelf_getehdr(elf, &ehdr) == NULL) {
773 pr_err("%s: cannot get elf header.\n", __func__);
777 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
779 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
784 syms = elf_getdata(sec, NULL);
788 sec = elf_getscn(elf, shdr.sh_link);
792 symstrs = elf_getdata(sec, NULL);
796 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
797 if (sec_strndx == NULL)
800 secstrs = elf_getdata(sec_strndx, NULL);
804 nr_syms = shdr.sh_size / shdr.sh_entsize;
806 memset(&sym, 0, sizeof(sym));
808 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
809 elf_section_by_name(elf, &ehdr, &shdr,
812 } else self->adjust_symbols = 0;
814 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
816 const char *elf_name;
817 char *demangled = NULL;
818 int is_label = elf_sym__is_label(&sym);
819 const char *section_name;
821 if (!is_label && !elf_sym__is_function(&sym))
824 sec = elf_getscn(elf, sym.st_shndx);
828 gelf_getshdr(sec, &shdr);
830 if (is_label && !elf_sec__is_text(&shdr, secstrs))
833 elf_name = elf_sym__name(&sym, symstrs);
834 section_name = elf_sec__name(&shdr, secstrs);
836 if (kernel || kmodule) {
837 char dso_name[PATH_MAX];
839 if (strcmp(section_name,
840 curr_dso->short_name + dso_name_len) == 0)
843 if (strcmp(section_name, ".text") == 0) {
849 snprintf(dso_name, sizeof(dso_name),
850 "%s%s", self->short_name, section_name);
852 curr_map = thread__find_map_by_name(thread, dso_name);
853 if (curr_map == NULL) {
854 u64 start = sym.st_value;
857 start += map->start + shdr.sh_offset;
859 curr_dso = dso__new(dso_name);
860 if (curr_dso == NULL)
862 curr_map = map__new2(start, curr_dso,
864 if (curr_map == NULL) {
865 dso__delete(curr_dso);
868 curr_map->map_ip = identity__map_ip;
869 curr_map->unmap_ip = identity__map_ip;
870 curr_dso->origin = DSO__ORIG_KERNEL;
871 __thread__insert_map(kthread, curr_map);
872 dsos__add(&dsos__kernel, curr_dso);
874 curr_dso = curr_map->dso;
879 if (curr_dso->adjust_symbols) {
880 pr_debug2("adjusting symbol: st_value: %Lx sh_addr: "
881 "%Lx sh_offset: %Lx\n", (u64)sym.st_value,
882 (u64)shdr.sh_addr, (u64)shdr.sh_offset);
883 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
886 * We need to figure out if the object was created from C++ sources
887 * DWARF DW_compile_unit has this, but we don't always have access
890 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
891 if (demangled != NULL)
892 elf_name = demangled;
894 f = symbol__new(sym.st_value, sym.st_size, elf_name);
899 if (filter && filter(curr_map, f))
902 symbols__insert(&curr_dso->symbols[curr_map->type], f);
908 * For misannotated, zeroed, ASM function sizes.
911 symbols__fixup_end(&self->symbols[map->type]);
919 static bool dso__build_id_equal(const struct dso *self, u8 *build_id)
921 return memcmp(self->build_id, build_id, sizeof(self->build_id)) == 0;
924 static bool __dsos__read_build_ids(struct list_head *head)
926 bool have_build_id = false;
929 list_for_each_entry(pos, head, node)
930 if (filename__read_build_id(pos->long_name, pos->build_id,
931 sizeof(pos->build_id)) > 0) {
932 have_build_id = true;
933 pos->has_build_id = true;
936 return have_build_id;
939 bool dsos__read_build_ids(void)
941 bool kbuildids = __dsos__read_build_ids(&dsos__kernel),
942 ubuildids = __dsos__read_build_ids(&dsos__user);
943 return kbuildids || ubuildids;
947 * Align offset to 4 bytes as needed for note name and descriptor data.
949 #define NOTE_ALIGN(n) (((n) + 3) & -4U)
951 int filename__read_build_id(const char *filename, void *bf, size_t size)
962 if (size < BUILD_ID_SIZE)
965 fd = open(filename, O_RDONLY);
969 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
971 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
979 if (gelf_getehdr(elf, &ehdr) == NULL) {
980 pr_err("%s: cannot get elf header.\n", __func__);
984 sec = elf_section_by_name(elf, &ehdr, &shdr,
985 ".note.gnu.build-id", NULL);
987 sec = elf_section_by_name(elf, &ehdr, &shdr,
993 data = elf_getdata(sec, NULL);
998 while (ptr < (data->d_buf + data->d_size)) {
999 GElf_Nhdr *nhdr = ptr;
1000 int namesz = NOTE_ALIGN(nhdr->n_namesz),
1001 descsz = NOTE_ALIGN(nhdr->n_descsz);
1004 ptr += sizeof(*nhdr);
1007 if (nhdr->n_type == NT_GNU_BUILD_ID &&
1008 nhdr->n_namesz == sizeof("GNU")) {
1009 if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
1010 memcpy(bf, ptr, BUILD_ID_SIZE);
1011 err = BUILD_ID_SIZE;
1025 int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
1029 if (size < BUILD_ID_SIZE)
1032 fd = open(filename, O_RDONLY);
1041 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
1044 namesz = NOTE_ALIGN(nhdr.n_namesz);
1045 descsz = NOTE_ALIGN(nhdr.n_descsz);
1046 if (nhdr.n_type == NT_GNU_BUILD_ID &&
1047 nhdr.n_namesz == sizeof("GNU")) {
1048 if (read(fd, bf, namesz) != namesz)
1050 if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
1051 if (read(fd, build_id,
1052 BUILD_ID_SIZE) == BUILD_ID_SIZE) {
1056 } else if (read(fd, bf, descsz) != descsz)
1059 int n = namesz + descsz;
1060 if (read(fd, bf, n) != n)
1069 char dso__symtab_origin(const struct dso *self)
1071 static const char origin[] = {
1072 [DSO__ORIG_KERNEL] = 'k',
1073 [DSO__ORIG_JAVA_JIT] = 'j',
1074 [DSO__ORIG_FEDORA] = 'f',
1075 [DSO__ORIG_UBUNTU] = 'u',
1076 [DSO__ORIG_BUILDID] = 'b',
1077 [DSO__ORIG_DSO] = 'd',
1078 [DSO__ORIG_KMODULE] = 'K',
1081 if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
1083 return origin[self->origin];
1086 int dso__load(struct dso *self, struct map *map, symbol_filter_t filter)
1088 int size = PATH_MAX;
1090 u8 build_id[BUILD_ID_SIZE];
1094 dso__set_loaded(self, map->type);
1097 return dso__load_kernel_sym(self, map, kthread, filter);
1099 name = malloc(size);
1103 self->adjust_symbols = 0;
1105 if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
1106 ret = dso__load_perf_map(self, map, filter);
1107 self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
1108 DSO__ORIG_NOT_FOUND;
1112 self->origin = DSO__ORIG_FEDORA - 1;
1117 switch (self->origin) {
1118 case DSO__ORIG_FEDORA:
1119 snprintf(name, size, "/usr/lib/debug%s.debug",
1122 case DSO__ORIG_UBUNTU:
1123 snprintf(name, size, "/usr/lib/debug%s",
1126 case DSO__ORIG_BUILDID:
1127 if (filename__read_build_id(self->long_name, build_id,
1128 sizeof(build_id))) {
1129 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
1131 build_id__sprintf(build_id, sizeof(build_id),
1133 snprintf(name, size,
1134 "/usr/lib/debug/.build-id/%.2s/%s.debug",
1135 build_id_hex, build_id_hex + 2);
1136 if (self->has_build_id)
1137 goto compare_build_id;
1143 snprintf(name, size, "%s", self->long_name);
1150 if (self->has_build_id) {
1151 if (filename__read_build_id(name, build_id,
1152 sizeof(build_id)) < 0)
1155 if (!dso__build_id_equal(self, build_id))
1159 fd = open(name, O_RDONLY);
1162 ret = dso__load_sym(self, map, NULL, name, fd, filter, 0, 0);
1166 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
1172 int nr_plt = dso__synthesize_plt_symbols(self, map, filter);
1178 if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
1183 static struct map *thread__find_map_by_name(struct thread *self, char *name)
1187 for (nd = rb_first(&self->maps[MAP__FUNCTION]); nd; nd = rb_next(nd)) {
1188 struct map *map = rb_entry(nd, struct map, rb_node);
1190 if (map->dso && strcmp(map->dso->name, name) == 0)
1197 static int dsos__set_modules_path_dir(char *dirname)
1199 struct dirent *dent;
1200 DIR *dir = opendir(dirname);
1203 pr_debug("%s: cannot open %s dir\n", __func__, dirname);
1207 while ((dent = readdir(dir)) != NULL) {
1208 char path[PATH_MAX];
1210 if (dent->d_type == DT_DIR) {
1211 if (!strcmp(dent->d_name, ".") ||
1212 !strcmp(dent->d_name, ".."))
1215 snprintf(path, sizeof(path), "%s/%s",
1216 dirname, dent->d_name);
1217 if (dsos__set_modules_path_dir(path) < 0)
1220 char *dot = strrchr(dent->d_name, '.'),
1225 if (dot == NULL || strcmp(dot, ".ko"))
1227 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1228 (int)(dot - dent->d_name), dent->d_name);
1230 strxfrchar(dso_name, '-', '_');
1231 map = thread__find_map_by_name(kthread, dso_name);
1235 snprintf(path, sizeof(path), "%s/%s",
1236 dirname, dent->d_name);
1238 long_name = strdup(path);
1239 if (long_name == NULL)
1241 dso__set_long_name(map->dso, long_name);
1251 static int dsos__set_modules_path(void)
1254 char modules_path[PATH_MAX];
1256 if (uname(&uts) < 0)
1259 snprintf(modules_path, sizeof(modules_path), "/lib/modules/%s/kernel",
1262 return dsos__set_modules_path_dir(modules_path);
1266 * Constructor variant for modules (where we know from /proc/modules where
1267 * they are loaded) and for vmlinux, where only after we load all the
1268 * symbols we'll know where it starts and ends.
1270 static struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
1272 struct map *self = malloc(sizeof(*self));
1276 * ->end will be filled after we load all the symbols
1278 map__init(self, type, start, 0, 0, dso);
1284 static int thread__create_module_maps(struct thread *self)
1288 FILE *file = fopen("/proc/modules", "r");
1294 while (!feof(file)) {
1295 char name[PATH_MAX];
1301 line_len = getline(&line, &n, file);
1308 line[--line_len] = '\0'; /* \n */
1310 sep = strrchr(line, 'x');
1314 hex2u64(sep + 1, &start);
1316 sep = strchr(line, ' ');
1322 snprintf(name, sizeof(name), "[%s]", line);
1323 dso = dso__new(name);
1326 goto out_delete_line;
1328 map = map__new2(start, dso, MAP__FUNCTION);
1331 goto out_delete_line;
1334 snprintf(name, sizeof(name),
1335 "/sys/module/%s/notes/.note.gnu.build-id", line);
1336 if (sysfs__read_build_id(name, dso->build_id,
1337 sizeof(dso->build_id)) == 0)
1338 dso->has_build_id = true;
1340 dso->origin = DSO__ORIG_KMODULE;
1341 __thread__insert_map(self, map);
1342 dsos__add(&dsos__kernel, dso);
1348 return dsos__set_modules_path();
1356 static int dso__load_vmlinux(struct dso *self, struct map *map, struct thread *thread,
1357 const char *vmlinux, symbol_filter_t filter)
1361 if (self->has_build_id) {
1362 u8 build_id[BUILD_ID_SIZE];
1364 if (filename__read_build_id(vmlinux, build_id,
1365 sizeof(build_id)) < 0) {
1366 pr_debug("No build_id in %s, ignoring it\n", vmlinux);
1369 if (!dso__build_id_equal(self, build_id)) {
1370 char expected_build_id[BUILD_ID_SIZE * 2 + 1],
1371 vmlinux_build_id[BUILD_ID_SIZE * 2 + 1];
1373 build_id__sprintf(self->build_id,
1374 sizeof(self->build_id),
1376 build_id__sprintf(build_id, sizeof(build_id),
1378 pr_debug("build_id in %s is %s while expected is %s, "
1379 "ignoring it\n", vmlinux, vmlinux_build_id,
1385 fd = open(vmlinux, O_RDONLY);
1389 dso__set_loaded(self, map->type);
1390 err = dso__load_sym(self, map, thread, self->long_name, fd, filter, 1, 0);
1396 static int dso__load_kernel_sym(struct dso *self, struct map *map,
1397 struct thread *thread, symbol_filter_t filter)
1402 if (vmlinux_path != NULL) {
1404 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1405 vmlinux_path__nr_entries);
1406 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
1407 err = dso__load_vmlinux(self, map, thread,
1408 vmlinux_path[i], filter);
1410 pr_debug("Using %s for symbols\n",
1412 dso__set_long_name(self,
1413 strdup(vmlinux_path[i]));
1419 is_kallsyms = self->long_name[0] == '[';
1423 err = dso__load_vmlinux(self, map, thread, self->long_name, filter);
1425 pr_info("The file %s cannot be used, "
1426 "trying to use /proc/kallsyms...", self->long_name);
1428 err = dso__load_kallsyms(self, map, thread, filter);
1429 if (err > 0 && !is_kallsyms)
1430 dso__set_long_name(self, strdup("[kernel.kallsyms]"));
1435 map__fixup_start(map);
1436 map__fixup_end(map);
1442 LIST_HEAD(dsos__user);
1443 LIST_HEAD(dsos__kernel);
1446 static void dsos__add(struct list_head *head, struct dso *dso)
1448 list_add_tail(&dso->node, head);
1451 static struct dso *dsos__find(struct list_head *head, const char *name)
1455 list_for_each_entry(pos, head, node)
1456 if (strcmp(pos->name, name) == 0)
1461 struct dso *dsos__findnew(const char *name)
1463 struct dso *dso = dsos__find(&dsos__user, name);
1466 dso = dso__new(name);
1468 dsos__add(&dsos__user, dso);
1469 dso__set_basename(dso);
1476 static void __dsos__fprintf(struct list_head *head, FILE *fp)
1480 list_for_each_entry(pos, head, node) {
1482 for (i = 0; i < MAP__NR_TYPES; ++i)
1483 dso__fprintf(pos, i, fp);
1487 void dsos__fprintf(FILE *fp)
1489 __dsos__fprintf(&dsos__kernel, fp);
1490 __dsos__fprintf(&dsos__user, fp);
1493 static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp)
1498 list_for_each_entry(pos, head, node) {
1499 ret += dso__fprintf_buildid(pos, fp);
1500 ret += fprintf(fp, " %s\n", pos->long_name);
1505 size_t dsos__fprintf_buildid(FILE *fp)
1507 return (__dsos__fprintf_buildid(&dsos__kernel, fp) +
1508 __dsos__fprintf_buildid(&dsos__user, fp));
1511 static int thread__create_kernel_map(struct thread *self, const char *vmlinux)
1514 struct dso *kernel = dso__new(vmlinux ?: "[kernel.kallsyms]");
1519 kmap = map__new2(0, kernel, MAP__FUNCTION);
1521 goto out_delete_kernel_dso;
1523 kmap->map_ip = kmap->unmap_ip = identity__map_ip;
1524 kernel->short_name = "[kernel]";
1527 vdso = dso__new("[vdso]");
1529 goto out_delete_kernel_map;
1530 dso__set_loaded(vdso, MAP__FUNCTION);
1532 if (sysfs__read_build_id("/sys/kernel/notes", kernel->build_id,
1533 sizeof(kernel->build_id)) == 0)
1534 kernel->has_build_id = true;
1536 __thread__insert_map(self, kmap);
1537 dsos__add(&dsos__kernel, kernel);
1538 dsos__add(&dsos__user, vdso);
1542 out_delete_kernel_map:
1544 out_delete_kernel_dso:
1545 dso__delete(kernel);
1549 static void vmlinux_path__exit(void)
1551 while (--vmlinux_path__nr_entries >= 0) {
1552 free(vmlinux_path[vmlinux_path__nr_entries]);
1553 vmlinux_path[vmlinux_path__nr_entries] = NULL;
1557 vmlinux_path = NULL;
1560 static int vmlinux_path__init(void)
1565 if (uname(&uts) < 0)
1568 vmlinux_path = malloc(sizeof(char *) * 5);
1569 if (vmlinux_path == NULL)
1572 vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
1573 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1575 ++vmlinux_path__nr_entries;
1576 vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
1577 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1579 ++vmlinux_path__nr_entries;
1580 snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
1581 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1582 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1584 ++vmlinux_path__nr_entries;
1585 snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
1586 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1587 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1589 ++vmlinux_path__nr_entries;
1590 snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
1592 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1593 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1595 ++vmlinux_path__nr_entries;
1600 vmlinux_path__exit();
1604 int symbol__init(struct symbol_conf *conf)
1606 const struct symbol_conf *pconf = conf ?: &symbol_conf__defaults;
1608 elf_version(EV_CURRENT);
1609 symbol__priv_size = pconf->priv_size;
1610 thread__init(kthread, 0);
1612 if (pconf->try_vmlinux_path && vmlinux_path__init() < 0)
1615 if (thread__create_kernel_map(kthread, pconf->vmlinux_name) < 0) {
1616 vmlinux_path__exit();
1620 kthread->use_modules = pconf->use_modules;
1621 if (pconf->use_modules && thread__create_module_maps(kthread) < 0)
1622 pr_debug("Failed to load list of modules in use, "
1625 * Now that we have all the maps created, just set the ->end of them:
1627 thread__fixup_maps_end(kthread);