13 #include <sys/utsname.h>
26 static void dsos__add(struct dso *dso);
27 static struct dso *dsos__find(const char *name);
28 static struct map *map__new2(u64 start, struct dso *dso);
29 static void kernel_maps__insert(struct map *map);
30 unsigned int symbol__priv_size;
32 static struct rb_root kernel_maps;
34 static void dso__fixup_sym_end(struct dso *self)
36 struct rb_node *nd, *prevnd = rb_first(&self->syms);
37 struct symbol *curr, *prev;
42 curr = rb_entry(prevnd, struct symbol, rb_node);
44 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
46 curr = rb_entry(nd, struct symbol, rb_node);
48 if (prev->end == prev->start)
49 prev->end = curr->start - 1;
53 if (curr->end == curr->start)
54 curr->end = roundup(curr->start, 4096);
57 static void kernel_maps__fixup_end(void)
59 struct map *prev, *curr;
60 struct rb_node *nd, *prevnd = rb_first(&kernel_maps);
65 curr = rb_entry(prevnd, struct map, rb_node);
67 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
69 curr = rb_entry(nd, struct map, rb_node);
70 prev->end = curr->start - 1;
73 nd = rb_last(&curr->dso->syms);
75 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
80 static struct symbol *symbol__new(u64 start, u64 len, const char *name)
82 size_t namelen = strlen(name) + 1;
83 struct symbol *self = calloc(1, (symbol__priv_size +
84 sizeof(*self) + namelen));
88 if (symbol__priv_size) {
89 memset(self, 0, symbol__priv_size);
90 self = ((void *)self) + symbol__priv_size;
93 self->end = len ? start + len - 1 : start;
95 pr_debug3("%s: %s %#Lx-%#Lx\n", __func__, name, start, self->end);
97 memcpy(self->name, name, namelen);
102 static void symbol__delete(struct symbol *self)
104 free(((void *)self) - symbol__priv_size);
107 static size_t symbol__fprintf(struct symbol *self, FILE *fp)
109 return fprintf(fp, " %llx-%llx %s\n",
110 self->start, self->end, self->name);
113 static void dso__set_long_name(struct dso *self, char *name)
115 self->long_name = name;
116 self->long_name_len = strlen(name);
119 static void dso__set_basename(struct dso *self)
121 self->short_name = basename(self->long_name);
124 struct dso *dso__new(const char *name)
126 struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
129 strcpy(self->name, name);
130 dso__set_long_name(self, self->name);
131 self->short_name = self->name;
132 self->syms = RB_ROOT;
133 self->find_symbol = dso__find_symbol;
134 self->slen_calculated = 0;
135 self->origin = DSO__ORIG_NOT_FOUND;
137 self->has_build_id = 0;
143 static void dso__delete_symbols(struct dso *self)
146 struct rb_node *next = rb_first(&self->syms);
149 pos = rb_entry(next, struct symbol, rb_node);
150 next = rb_next(&pos->rb_node);
151 rb_erase(&pos->rb_node, &self->syms);
156 void dso__delete(struct dso *self)
158 dso__delete_symbols(self);
159 if (self->long_name != self->name)
160 free(self->long_name);
164 void dso__set_build_id(struct dso *self, void *build_id)
166 memcpy(self->build_id, build_id, sizeof(self->build_id));
167 self->has_build_id = 1;
170 static void dso__insert_symbol(struct dso *self, struct symbol *sym)
172 struct rb_node **p = &self->syms.rb_node;
173 struct rb_node *parent = NULL;
174 const u64 ip = sym->start;
179 s = rb_entry(parent, struct symbol, rb_node);
185 rb_link_node(&sym->rb_node, parent, p);
186 rb_insert_color(&sym->rb_node, &self->syms);
189 struct symbol *dso__find_symbol(struct dso *self, u64 ip)
196 n = self->syms.rb_node;
199 struct symbol *s = rb_entry(n, struct symbol, rb_node);
203 else if (ip > s->end)
212 int build_id__sprintf(u8 *self, int len, char *bf)
218 for (i = 0; i < len; ++i) {
219 sprintf(bid, "%02x", *raw);
227 size_t dso__fprintf_buildid(struct dso *self, FILE *fp)
229 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
231 build_id__sprintf(self->build_id, sizeof(self->build_id), sbuild_id);
232 return fprintf(fp, "%s", sbuild_id);
235 size_t dso__fprintf(struct dso *self, FILE *fp)
238 size_t ret = fprintf(fp, "dso: %s (", self->short_name);
240 ret += dso__fprintf_buildid(self, fp);
241 ret += fprintf(fp, ")\n");
243 for (nd = rb_first(&self->syms); nd; nd = rb_next(nd)) {
244 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
245 ret += symbol__fprintf(pos, fp);
252 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
253 * so that we can in the next step set the symbol ->end address and then
254 * call kernel_maps__split_kallsyms.
256 static int kernel_maps__load_all_kallsyms(void)
260 FILE *file = fopen("/proc/kallsyms", "r");
265 while (!feof(file)) {
272 line_len = getline(&line, &n, file);
279 line[--line_len] = '\0'; /* \n */
281 len = hex2u64(line, &start);
284 if (len + 2 >= line_len)
287 symbol_type = toupper(line[len]);
289 * We're interested only in code ('T'ext)
291 if (symbol_type != 'T' && symbol_type != 'W')
294 symbol_name = line + len + 2;
296 * Will fix up the end later, when we have all symbols sorted.
298 sym = symbol__new(start, 0, symbol_name);
301 goto out_delete_line;
304 * We will pass the symbols to the filter later, in
305 * kernel_maps__split_kallsyms, when we have split the
308 dso__insert_symbol(kernel_map->dso, sym);
323 * Split the symbols into maps, making sure there are no overlaps, i.e. the
324 * kernel range is broken in several maps, named [kernel].N, as we don't have
325 * the original ELF section names vmlinux have.
327 static int kernel_maps__split_kallsyms(symbol_filter_t filter, int use_modules)
329 struct map *map = kernel_map;
332 struct rb_node *next = rb_first(&kernel_map->dso->syms);
333 int kernel_range = 0;
338 pos = rb_entry(next, struct symbol, rb_node);
339 next = rb_next(&pos->rb_node);
341 module = strchr(pos->name, '\t');
348 if (strcmp(map->dso->name, module)) {
349 map = kernel_maps__find_by_dso_name(module);
351 pr_err("/proc/{kallsyms,modules} "
357 * So that we look just like we get from .ko files,
358 * i.e. not prelinked, relative to map->start.
360 pos->start = map->map_ip(map, pos->start);
361 pos->end = map->map_ip(map, pos->end);
362 } else if (map != kernel_map) {
363 char dso_name[PATH_MAX];
366 snprintf(dso_name, sizeof(dso_name), "[kernel].%d",
369 dso = dso__new(dso_name);
373 map = map__new2(pos->start, dso);
379 map->map_ip = map->unmap_ip = identity__map_ip;
380 kernel_maps__insert(map);
384 if (filter && filter(map, pos)) {
386 rb_erase(&pos->rb_node, &kernel_map->dso->syms);
389 if (map != kernel_map) {
390 rb_erase(&pos->rb_node, &kernel_map->dso->syms);
391 dso__insert_symbol(map->dso, pos);
401 static int kernel_maps__load_kallsyms(symbol_filter_t filter, int use_modules)
403 if (kernel_maps__load_all_kallsyms())
406 dso__fixup_sym_end(kernel_map->dso);
408 return kernel_maps__split_kallsyms(filter, use_modules);
411 static size_t kernel_maps__fprintf(FILE *fp)
413 size_t printed = fprintf(fp, "Kernel maps:\n");
416 for (nd = rb_first(&kernel_maps); nd; nd = rb_next(nd)) {
417 struct map *pos = rb_entry(nd, struct map, rb_node);
419 printed += fprintf(fp, "Map:");
420 printed += map__fprintf(pos, fp);
422 printed += dso__fprintf(pos->dso, fp);
423 printed += fprintf(fp, "--\n");
427 return printed + fprintf(fp, "END kernel maps\n");
430 static int dso__load_perf_map(struct dso *self, struct map *map,
431 symbol_filter_t filter)
438 file = fopen(self->long_name, "r");
442 while (!feof(file)) {
447 line_len = getline(&line, &n, file);
454 line[--line_len] = '\0'; /* \n */
456 len = hex2u64(line, &start);
459 if (len + 2 >= line_len)
462 len += hex2u64(line + len, &size);
465 if (len + 2 >= line_len)
468 sym = symbol__new(start, size, line + len);
471 goto out_delete_line;
473 if (filter && filter(map, sym))
476 dso__insert_symbol(self, sym);
493 * elf_symtab__for_each_symbol - iterate thru all the symbols
495 * @self: struct elf_symtab instance to iterate
497 * @sym: GElf_Sym iterator
499 #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
500 for (idx = 0, gelf_getsym(syms, idx, &sym);\
502 idx++, gelf_getsym(syms, idx, &sym))
504 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
506 return GELF_ST_TYPE(sym->st_info);
509 static inline int elf_sym__is_function(const GElf_Sym *sym)
511 return elf_sym__type(sym) == STT_FUNC &&
513 sym->st_shndx != SHN_UNDEF;
516 static inline int elf_sym__is_label(const GElf_Sym *sym)
518 return elf_sym__type(sym) == STT_NOTYPE &&
520 sym->st_shndx != SHN_UNDEF &&
521 sym->st_shndx != SHN_ABS;
524 static inline const char *elf_sec__name(const GElf_Shdr *shdr,
525 const Elf_Data *secstrs)
527 return secstrs->d_buf + shdr->sh_name;
530 static inline int elf_sec__is_text(const GElf_Shdr *shdr,
531 const Elf_Data *secstrs)
533 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
536 static inline const char *elf_sym__name(const GElf_Sym *sym,
537 const Elf_Data *symstrs)
539 return symstrs->d_buf + sym->st_name;
542 static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
543 GElf_Shdr *shp, const char *name,
549 while ((sec = elf_nextscn(elf, sec)) != NULL) {
552 gelf_getshdr(sec, shp);
553 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
554 if (!strcmp(name, str)) {
565 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
566 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
568 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
570 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
571 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
573 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
576 * We need to check if we have a .dynsym, so that we can handle the
577 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
578 * .dynsym or .symtab).
579 * And always look at the original dso, not at debuginfo packages, that
580 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
582 static int dso__synthesize_plt_symbols(struct dso *self, struct map *map,
583 symbol_filter_t filter)
585 uint32_t nr_rel_entries, idx;
590 GElf_Shdr shdr_rel_plt, shdr_dynsym;
591 Elf_Data *reldata, *syms, *symstrs;
592 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
595 char sympltname[1024];
597 int nr = 0, symidx, fd, err = 0;
599 fd = open(self->long_name, O_RDONLY);
603 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
607 if (gelf_getehdr(elf, &ehdr) == NULL)
610 scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
611 ".dynsym", &dynsym_idx);
612 if (scn_dynsym == NULL)
615 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
617 if (scn_plt_rel == NULL) {
618 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
620 if (scn_plt_rel == NULL)
626 if (shdr_rel_plt.sh_link != dynsym_idx)
629 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
633 * Fetch the relocation section to find the idxes to the GOT
634 * and the symbols in the .dynsym they refer to.
636 reldata = elf_getdata(scn_plt_rel, NULL);
640 syms = elf_getdata(scn_dynsym, NULL);
644 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
645 if (scn_symstrs == NULL)
648 symstrs = elf_getdata(scn_symstrs, NULL);
652 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
653 plt_offset = shdr_plt.sh_offset;
655 if (shdr_rel_plt.sh_type == SHT_RELA) {
656 GElf_Rela pos_mem, *pos;
658 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
660 symidx = GELF_R_SYM(pos->r_info);
661 plt_offset += shdr_plt.sh_entsize;
662 gelf_getsym(syms, symidx, &sym);
663 snprintf(sympltname, sizeof(sympltname),
664 "%s@plt", elf_sym__name(&sym, symstrs));
666 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
671 if (filter && filter(map, f))
674 dso__insert_symbol(self, f);
678 } else if (shdr_rel_plt.sh_type == SHT_REL) {
679 GElf_Rel pos_mem, *pos;
680 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
682 symidx = GELF_R_SYM(pos->r_info);
683 plt_offset += shdr_plt.sh_entsize;
684 gelf_getsym(syms, symidx, &sym);
685 snprintf(sympltname, sizeof(sympltname),
686 "%s@plt", elf_sym__name(&sym, symstrs));
688 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
693 if (filter && filter(map, f))
696 dso__insert_symbol(self, f);
711 pr_warning("%s: problems reading %s PLT info.\n",
712 __func__, self->long_name);
716 static int dso__load_sym(struct dso *self, struct map *map, const char *name,
717 int fd, symbol_filter_t filter, int kernel,
720 struct map *curr_map = map;
721 struct dso *curr_dso = self;
722 size_t dso_name_len = strlen(self->short_name);
723 Elf_Data *symstrs, *secstrs;
731 Elf_Scn *sec, *sec_strndx;
735 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
737 pr_err("%s: cannot read %s ELF file.\n", __func__, name);
741 if (gelf_getehdr(elf, &ehdr) == NULL) {
742 pr_err("%s: cannot get elf header.\n", __func__);
746 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
748 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
753 syms = elf_getdata(sec, NULL);
757 sec = elf_getscn(elf, shdr.sh_link);
761 symstrs = elf_getdata(sec, NULL);
765 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
766 if (sec_strndx == NULL)
769 secstrs = elf_getdata(sec_strndx, NULL);
773 nr_syms = shdr.sh_size / shdr.sh_entsize;
775 memset(&sym, 0, sizeof(sym));
777 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
778 elf_section_by_name(elf, &ehdr, &shdr,
781 } else self->adjust_symbols = 0;
783 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
785 const char *elf_name;
786 char *demangled = NULL;
787 int is_label = elf_sym__is_label(&sym);
788 const char *section_name;
790 if (!is_label && !elf_sym__is_function(&sym))
793 sec = elf_getscn(elf, sym.st_shndx);
797 gelf_getshdr(sec, &shdr);
799 if (is_label && !elf_sec__is_text(&shdr, secstrs))
802 elf_name = elf_sym__name(&sym, symstrs);
803 section_name = elf_sec__name(&shdr, secstrs);
805 if (kernel || kmodule) {
806 char dso_name[PATH_MAX];
808 if (strcmp(section_name,
809 curr_dso->short_name + dso_name_len) == 0)
812 if (strcmp(section_name, ".text") == 0) {
818 snprintf(dso_name, sizeof(dso_name),
819 "%s%s", self->short_name, section_name);
821 curr_map = kernel_maps__find_by_dso_name(dso_name);
822 if (curr_map == NULL) {
823 u64 start = sym.st_value;
826 start += map->start + shdr.sh_offset;
828 curr_dso = dso__new(dso_name);
829 if (curr_dso == NULL)
831 curr_map = map__new2(start, curr_dso);
832 if (curr_map == NULL) {
833 dso__delete(curr_dso);
836 curr_map->map_ip = identity__map_ip;
837 curr_map->unmap_ip = identity__map_ip;
838 curr_dso->origin = DSO__ORIG_KERNEL;
839 kernel_maps__insert(curr_map);
842 curr_dso = curr_map->dso;
847 if (curr_dso->adjust_symbols) {
848 pr_debug2("adjusting symbol: st_value: %Lx sh_addr: "
849 "%Lx sh_offset: %Lx\n", (u64)sym.st_value,
850 (u64)shdr.sh_addr, (u64)shdr.sh_offset);
851 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
854 * We need to figure out if the object was created from C++ sources
855 * DWARF DW_compile_unit has this, but we don't always have access
858 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
859 if (demangled != NULL)
860 elf_name = demangled;
862 f = symbol__new(sym.st_value, sym.st_size, elf_name);
867 if (filter && filter(curr_map, f))
870 dso__insert_symbol(curr_dso, f);
876 * For misannotated, zeroed, ASM function sizes.
879 dso__fixup_sym_end(self);
887 bool dsos__read_build_ids(void)
889 bool have_build_id = false;
892 list_for_each_entry(pos, &dsos, node)
893 if (filename__read_build_id(pos->long_name, pos->build_id,
894 sizeof(pos->build_id)) > 0) {
895 have_build_id = true;
896 pos->has_build_id = true;
899 return have_build_id;
902 int filename__read_build_id(const char *filename, void *bf, size_t size)
907 Elf_Data *build_id_data;
911 if (size < BUILD_ID_SIZE)
914 fd = open(filename, O_RDONLY);
918 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
920 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
924 if (gelf_getehdr(elf, &ehdr) == NULL) {
925 pr_err("%s: cannot get elf header.\n", __func__);
929 sec = elf_section_by_name(elf, &ehdr, &shdr,
930 ".note.gnu.build-id", NULL);
934 build_id_data = elf_getdata(sec, NULL);
935 if (build_id_data == NULL)
937 memcpy(bf, build_id_data->d_buf + 16, BUILD_ID_SIZE);
947 int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
951 if (size < BUILD_ID_SIZE)
954 fd = open(filename, O_RDONLY);
963 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
966 namesz = (nhdr.n_namesz + 3) & -4U;
967 descsz = (nhdr.n_descsz + 3) & -4U;
968 if (nhdr.n_type == NT_GNU_BUILD_ID &&
969 nhdr.n_namesz == sizeof("GNU")) {
970 if (read(fd, bf, namesz) != namesz)
972 if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
973 if (read(fd, build_id,
974 BUILD_ID_SIZE) == BUILD_ID_SIZE) {
978 } else if (read(fd, bf, descsz) != descsz)
981 int n = namesz + descsz;
982 if (read(fd, bf, n) != n)
991 char dso__symtab_origin(const struct dso *self)
993 static const char origin[] = {
994 [DSO__ORIG_KERNEL] = 'k',
995 [DSO__ORIG_JAVA_JIT] = 'j',
996 [DSO__ORIG_FEDORA] = 'f',
997 [DSO__ORIG_UBUNTU] = 'u',
998 [DSO__ORIG_BUILDID] = 'b',
999 [DSO__ORIG_DSO] = 'd',
1000 [DSO__ORIG_KMODULE] = 'K',
1003 if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
1005 return origin[self->origin];
1008 int dso__load(struct dso *self, struct map *map, symbol_filter_t filter)
1010 int size = PATH_MAX;
1011 char *name = malloc(size);
1012 u8 build_id[BUILD_ID_SIZE];
1021 self->adjust_symbols = 0;
1023 if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
1024 ret = dso__load_perf_map(self, map, filter);
1025 self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
1026 DSO__ORIG_NOT_FOUND;
1030 self->origin = DSO__ORIG_FEDORA - 1;
1035 switch (self->origin) {
1036 case DSO__ORIG_FEDORA:
1037 snprintf(name, size, "/usr/lib/debug%s.debug",
1040 case DSO__ORIG_UBUNTU:
1041 snprintf(name, size, "/usr/lib/debug%s",
1044 case DSO__ORIG_BUILDID:
1045 if (filename__read_build_id(self->long_name, build_id,
1046 sizeof(build_id))) {
1047 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
1049 build_id__sprintf(build_id, sizeof(build_id),
1051 snprintf(name, size,
1052 "/usr/lib/debug/.build-id/%.2s/%s.debug",
1053 build_id_hex, build_id_hex + 2);
1054 if (self->has_build_id)
1055 goto compare_build_id;
1061 snprintf(name, size, "%s", self->long_name);
1068 if (self->has_build_id) {
1069 if (filename__read_build_id(name, build_id,
1070 sizeof(build_id)) < 0)
1073 if (memcmp(build_id, self->build_id,
1074 sizeof(self->build_id)) != 0)
1078 fd = open(name, O_RDONLY);
1081 ret = dso__load_sym(self, map, name, fd, filter, 0, 0);
1085 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
1091 int nr_plt = dso__synthesize_plt_symbols(self, map, filter);
1097 if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
1102 struct map *kernel_map;
1104 static void kernel_maps__insert(struct map *map)
1106 maps__insert(&kernel_maps, map);
1109 struct symbol *kernel_maps__find_symbol(u64 ip, struct map **mapp)
1111 struct map *map = maps__find(&kernel_maps, ip);
1117 ip = map->map_ip(map, ip);
1118 return map->dso->find_symbol(map->dso, ip);
1124 struct map *kernel_maps__find_by_dso_name(const char *name)
1128 for (nd = rb_first(&kernel_maps); nd; nd = rb_next(nd)) {
1129 struct map *map = rb_entry(nd, struct map, rb_node);
1131 if (map->dso && strcmp(map->dso->name, name) == 0)
1138 static int dso__load_module_sym(struct dso *self, struct map *map,
1139 symbol_filter_t filter)
1141 int err = 0, fd = open(self->long_name, O_RDONLY);
1146 pr_err("%s: cannot open %s\n", __func__, self->long_name);
1150 err = dso__load_sym(self, map, self->long_name, fd, filter, 0, 1);
1156 static int dsos__load_modules_sym_dir(char *dirname, symbol_filter_t filter)
1158 struct dirent *dent;
1159 int nr_symbols = 0, err;
1160 DIR *dir = opendir(dirname);
1163 pr_err("%s: cannot open %s dir\n", __func__, dirname);
1167 while ((dent = readdir(dir)) != NULL) {
1168 char path[PATH_MAX];
1170 if (dent->d_type == DT_DIR) {
1171 if (!strcmp(dent->d_name, ".") ||
1172 !strcmp(dent->d_name, ".."))
1175 snprintf(path, sizeof(path), "%s/%s",
1176 dirname, dent->d_name);
1177 err = dsos__load_modules_sym_dir(path, filter);
1181 char *dot = strrchr(dent->d_name, '.'),
1184 struct rb_node *last;
1187 if (dot == NULL || strcmp(dot, ".ko"))
1189 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1190 (int)(dot - dent->d_name), dent->d_name);
1192 strxfrchar(dso_name, '-', '_');
1193 map = kernel_maps__find_by_dso_name(dso_name);
1197 snprintf(path, sizeof(path), "%s/%s",
1198 dirname, dent->d_name);
1200 long_name = strdup(path);
1201 if (long_name == NULL)
1203 dso__set_long_name(map->dso, long_name);
1204 dso__set_basename(map->dso);
1206 err = dso__load_module_sym(map->dso, map, filter);
1209 last = rb_last(&map->dso->syms);
1213 * We do this here as well, even having the
1214 * symbol size found in the symtab because
1215 * misannotated ASM symbols may have the size
1218 dso__fixup_sym_end(map->dso);
1220 sym = rb_entry(last, struct symbol, rb_node);
1221 map->end = map->start + sym->end;
1233 int dsos__load_modules_sym(symbol_filter_t filter)
1236 char modules_path[PATH_MAX];
1238 if (uname(&uts) < 0)
1241 snprintf(modules_path, sizeof(modules_path), "/lib/modules/%s/kernel",
1244 return dsos__load_modules_sym_dir(modules_path, filter);
1248 * Constructor variant for modules (where we know from /proc/modules where
1249 * they are loaded) and for vmlinux, where only after we load all the
1250 * symbols we'll know where it starts and ends.
1252 static struct map *map__new2(u64 start, struct dso *dso)
1254 struct map *self = malloc(sizeof(*self));
1258 * ->end will be filled after we load all the symbols
1260 map__init(self, start, 0, 0, dso);
1266 int dsos__load_modules(void)
1270 FILE *file = fopen("/proc/modules", "r");
1276 while (!feof(file)) {
1277 char name[PATH_MAX];
1283 line_len = getline(&line, &n, file);
1290 line[--line_len] = '\0'; /* \n */
1292 sep = strrchr(line, 'x');
1296 hex2u64(sep + 1, &start);
1298 sep = strchr(line, ' ');
1304 snprintf(name, sizeof(name), "[%s]", line);
1305 dso = dso__new(name);
1308 goto out_delete_line;
1310 map = map__new2(start, dso);
1313 goto out_delete_line;
1316 snprintf(name, sizeof(name),
1317 "/sys/module/%s/notes/.note.gnu.build-id", line);
1318 if (sysfs__read_build_id(name, dso->build_id,
1319 sizeof(dso->build_id)) == 0)
1320 dso->has_build_id = true;
1322 dso->origin = DSO__ORIG_KMODULE;
1323 kernel_maps__insert(map);
1338 static int dso__load_vmlinux(struct dso *self, struct map *map,
1339 const char *vmlinux, symbol_filter_t filter)
1341 int err, fd = open(vmlinux, O_RDONLY);
1348 err = dso__load_sym(self, map, self->long_name, fd, filter, 1, 0);
1355 int dso__load_kernel_sym(struct dso *self, symbol_filter_t filter,
1360 kernel_map = map__new2(0, self);
1361 if (kernel_map == NULL)
1364 kernel_map->map_ip = kernel_map->unmap_ip = identity__map_ip;
1366 err = dso__load_vmlinux(self, kernel_map, self->name, filter);
1368 err = kernel_maps__load_kallsyms(filter, use_modules);
1371 struct rb_node *node = rb_first(&self->syms);
1372 struct symbol *sym = rb_entry(node, struct symbol, rb_node);
1374 kernel_map->start = sym->start;
1375 node = rb_last(&self->syms);
1376 sym = rb_entry(node, struct symbol, rb_node);
1377 kernel_map->end = sym->end;
1379 self->origin = DSO__ORIG_KERNEL;
1380 kernel_maps__insert(kernel_map);
1382 * Now that we have all sorted out, just set the ->end of all
1385 kernel_maps__fixup_end();
1388 kernel_maps__fprintf(stderr);
1397 const char *vmlinux_name = "vmlinux";
1399 static void dsos__add(struct dso *dso)
1401 list_add_tail(&dso->node, &dsos);
1404 static struct dso *dsos__find(const char *name)
1408 list_for_each_entry(pos, &dsos, node)
1409 if (strcmp(pos->name, name) == 0)
1414 struct dso *dsos__findnew(const char *name)
1416 struct dso *dso = dsos__find(name);
1419 dso = dso__new(name);
1422 dso__set_basename(dso);
1429 void dsos__fprintf(FILE *fp)
1433 list_for_each_entry(pos, &dsos, node)
1434 dso__fprintf(pos, fp);
1437 size_t dsos__fprintf_buildid(FILE *fp)
1442 list_for_each_entry(pos, &dsos, node) {
1443 ret += dso__fprintf_buildid(pos, fp);
1444 ret += fprintf(fp, " %s\n", pos->long_name);
1449 struct dso *dsos__load_kernel(void)
1451 struct dso *kernel = dso__new(vmlinux_name);
1456 kernel->short_name = "[kernel]";
1457 vdso = dso__new("[vdso]");
1461 if (sysfs__read_build_id("/sys/kernel/notes", kernel->build_id,
1462 sizeof(kernel->build_id)) == 0)
1463 kernel->has_build_id = true;
1471 int load_kernel(symbol_filter_t filter, bool use_modules)
1473 struct dso *kernel = dsos__load_kernel();
1479 if (dsos__load_modules() < 0)
1480 pr_warning("Failed to load list of modules in use, "
1482 else if (dsos__load_modules_sym(filter) < 0)
1483 pr_warning("Failed to read module symbols, "
1487 if (dso__load_kernel_sym(kernel, filter, use_modules) < 0)
1488 pr_warning("Failed to read kernel symbols, continuing...\n");
1493 void symbol__init(unsigned int priv_size)
1495 elf_version(EV_CURRENT);
1496 symbol__priv_size = priv_size;