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 static bool dso__build_id_equal(const struct dso *self, u8 *build_id)
889 return memcmp(self->build_id, build_id, sizeof(self->build_id)) == 0;
892 bool dsos__read_build_ids(void)
894 bool have_build_id = false;
897 list_for_each_entry(pos, &dsos, node)
898 if (filename__read_build_id(pos->long_name, pos->build_id,
899 sizeof(pos->build_id)) > 0) {
900 have_build_id = true;
901 pos->has_build_id = true;
904 return have_build_id;
908 * Align offset to 4 bytes as needed for note name and descriptor data.
910 #define NOTE_ALIGN(n) (((n) + 3) & -4U)
912 int filename__read_build_id(const char *filename, void *bf, size_t size)
922 if (size < BUILD_ID_SIZE)
925 fd = open(filename, O_RDONLY);
929 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
931 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
935 if (gelf_getehdr(elf, &ehdr) == NULL) {
936 pr_err("%s: cannot get elf header.\n", __func__);
940 sec = elf_section_by_name(elf, &ehdr, &shdr,
941 ".note.gnu.build-id", NULL);
943 sec = elf_section_by_name(elf, &ehdr, &shdr,
949 data = elf_getdata(sec, NULL);
954 while (ptr < (data->d_buf + data->d_size)) {
955 GElf_Nhdr *nhdr = ptr;
956 int namesz = NOTE_ALIGN(nhdr->n_namesz),
957 descsz = NOTE_ALIGN(nhdr->n_descsz);
960 ptr += sizeof(*nhdr);
963 if (nhdr->n_type == NT_GNU_BUILD_ID &&
964 nhdr->n_namesz == sizeof("GNU")) {
965 if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
966 memcpy(bf, ptr, BUILD_ID_SIZE);
981 int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
985 if (size < BUILD_ID_SIZE)
988 fd = open(filename, O_RDONLY);
997 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
1000 namesz = NOTE_ALIGN(nhdr.n_namesz);
1001 descsz = NOTE_ALIGN(nhdr.n_descsz);
1002 if (nhdr.n_type == NT_GNU_BUILD_ID &&
1003 nhdr.n_namesz == sizeof("GNU")) {
1004 if (read(fd, bf, namesz) != namesz)
1006 if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
1007 if (read(fd, build_id,
1008 BUILD_ID_SIZE) == BUILD_ID_SIZE) {
1012 } else if (read(fd, bf, descsz) != descsz)
1015 int n = namesz + descsz;
1016 if (read(fd, bf, n) != n)
1025 char dso__symtab_origin(const struct dso *self)
1027 static const char origin[] = {
1028 [DSO__ORIG_KERNEL] = 'k',
1029 [DSO__ORIG_JAVA_JIT] = 'j',
1030 [DSO__ORIG_FEDORA] = 'f',
1031 [DSO__ORIG_UBUNTU] = 'u',
1032 [DSO__ORIG_BUILDID] = 'b',
1033 [DSO__ORIG_DSO] = 'd',
1034 [DSO__ORIG_KMODULE] = 'K',
1037 if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
1039 return origin[self->origin];
1042 int dso__load(struct dso *self, struct map *map, symbol_filter_t filter)
1044 int size = PATH_MAX;
1045 char *name = malloc(size);
1046 u8 build_id[BUILD_ID_SIZE];
1055 self->adjust_symbols = 0;
1057 if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
1058 ret = dso__load_perf_map(self, map, filter);
1059 self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
1060 DSO__ORIG_NOT_FOUND;
1064 self->origin = DSO__ORIG_FEDORA - 1;
1069 switch (self->origin) {
1070 case DSO__ORIG_FEDORA:
1071 snprintf(name, size, "/usr/lib/debug%s.debug",
1074 case DSO__ORIG_UBUNTU:
1075 snprintf(name, size, "/usr/lib/debug%s",
1078 case DSO__ORIG_BUILDID:
1079 if (filename__read_build_id(self->long_name, build_id,
1080 sizeof(build_id))) {
1081 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
1083 build_id__sprintf(build_id, sizeof(build_id),
1085 snprintf(name, size,
1086 "/usr/lib/debug/.build-id/%.2s/%s.debug",
1087 build_id_hex, build_id_hex + 2);
1088 if (self->has_build_id)
1089 goto compare_build_id;
1095 snprintf(name, size, "%s", self->long_name);
1102 if (self->has_build_id) {
1103 if (filename__read_build_id(name, build_id,
1104 sizeof(build_id)) < 0)
1107 if (!dso__build_id_equal(self, build_id))
1111 fd = open(name, O_RDONLY);
1114 ret = dso__load_sym(self, map, name, fd, filter, 0, 0);
1118 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
1124 int nr_plt = dso__synthesize_plt_symbols(self, map, filter);
1130 if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
1135 struct map *kernel_map;
1137 static void kernel_maps__insert(struct map *map)
1139 maps__insert(&kernel_maps, map);
1142 struct symbol *kernel_maps__find_symbol(u64 ip, struct map **mapp)
1144 struct map *map = maps__find(&kernel_maps, ip);
1150 ip = map->map_ip(map, ip);
1151 return map->dso->find_symbol(map->dso, ip);
1157 struct map *kernel_maps__find_by_dso_name(const char *name)
1161 for (nd = rb_first(&kernel_maps); nd; nd = rb_next(nd)) {
1162 struct map *map = rb_entry(nd, struct map, rb_node);
1164 if (map->dso && strcmp(map->dso->name, name) == 0)
1171 static int dso__load_module_sym(struct dso *self, struct map *map,
1172 symbol_filter_t filter)
1174 int err = 0, fd = open(self->long_name, O_RDONLY);
1179 pr_err("%s: cannot open %s\n", __func__, self->long_name);
1183 err = dso__load_sym(self, map, self->long_name, fd, filter, 0, 1);
1189 static int dsos__load_modules_sym_dir(char *dirname, symbol_filter_t filter)
1191 struct dirent *dent;
1192 int nr_symbols = 0, err;
1193 DIR *dir = opendir(dirname);
1196 pr_err("%s: cannot open %s dir\n", __func__, dirname);
1200 while ((dent = readdir(dir)) != NULL) {
1201 char path[PATH_MAX];
1203 if (dent->d_type == DT_DIR) {
1204 if (!strcmp(dent->d_name, ".") ||
1205 !strcmp(dent->d_name, ".."))
1208 snprintf(path, sizeof(path), "%s/%s",
1209 dirname, dent->d_name);
1210 err = dsos__load_modules_sym_dir(path, filter);
1214 char *dot = strrchr(dent->d_name, '.'),
1217 struct rb_node *last;
1220 if (dot == NULL || strcmp(dot, ".ko"))
1222 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1223 (int)(dot - dent->d_name), dent->d_name);
1225 strxfrchar(dso_name, '-', '_');
1226 map = kernel_maps__find_by_dso_name(dso_name);
1230 snprintf(path, sizeof(path), "%s/%s",
1231 dirname, dent->d_name);
1233 long_name = strdup(path);
1234 if (long_name == NULL)
1236 dso__set_long_name(map->dso, long_name);
1237 dso__set_basename(map->dso);
1239 err = dso__load_module_sym(map->dso, map, filter);
1242 last = rb_last(&map->dso->syms);
1246 * We do this here as well, even having the
1247 * symbol size found in the symtab because
1248 * misannotated ASM symbols may have the size
1251 dso__fixup_sym_end(map->dso);
1253 sym = rb_entry(last, struct symbol, rb_node);
1254 map->end = map->start + sym->end;
1266 int dsos__load_modules_sym(symbol_filter_t filter)
1269 char modules_path[PATH_MAX];
1271 if (uname(&uts) < 0)
1274 snprintf(modules_path, sizeof(modules_path), "/lib/modules/%s/kernel",
1277 return dsos__load_modules_sym_dir(modules_path, filter);
1281 * Constructor variant for modules (where we know from /proc/modules where
1282 * they are loaded) and for vmlinux, where only after we load all the
1283 * symbols we'll know where it starts and ends.
1285 static struct map *map__new2(u64 start, struct dso *dso)
1287 struct map *self = malloc(sizeof(*self));
1291 * ->end will be filled after we load all the symbols
1293 map__init(self, start, 0, 0, dso);
1299 int dsos__load_modules(void)
1303 FILE *file = fopen("/proc/modules", "r");
1309 while (!feof(file)) {
1310 char name[PATH_MAX];
1316 line_len = getline(&line, &n, file);
1323 line[--line_len] = '\0'; /* \n */
1325 sep = strrchr(line, 'x');
1329 hex2u64(sep + 1, &start);
1331 sep = strchr(line, ' ');
1337 snprintf(name, sizeof(name), "[%s]", line);
1338 dso = dso__new(name);
1341 goto out_delete_line;
1343 map = map__new2(start, dso);
1346 goto out_delete_line;
1349 snprintf(name, sizeof(name),
1350 "/sys/module/%s/notes/.note.gnu.build-id", line);
1351 if (sysfs__read_build_id(name, dso->build_id,
1352 sizeof(dso->build_id)) == 0)
1353 dso->has_build_id = true;
1355 dso->origin = DSO__ORIG_KMODULE;
1356 kernel_maps__insert(map);
1371 static int dso__load_vmlinux(struct dso *self, struct map *map,
1372 const char *vmlinux, symbol_filter_t filter)
1374 int err, fd = open(vmlinux, O_RDONLY);
1381 err = dso__load_sym(self, map, self->long_name, fd, filter, 1, 0);
1388 int dso__load_kernel_sym(struct dso *self, symbol_filter_t filter,
1393 kernel_map = map__new2(0, self);
1394 if (kernel_map == NULL)
1397 kernel_map->map_ip = kernel_map->unmap_ip = identity__map_ip;
1399 err = dso__load_vmlinux(self, kernel_map, self->name, filter);
1401 err = kernel_maps__load_kallsyms(filter, use_modules);
1404 struct rb_node *node = rb_first(&self->syms);
1405 struct symbol *sym = rb_entry(node, struct symbol, rb_node);
1407 kernel_map->start = sym->start;
1408 node = rb_last(&self->syms);
1409 sym = rb_entry(node, struct symbol, rb_node);
1410 kernel_map->end = sym->end;
1412 self->origin = DSO__ORIG_KERNEL;
1413 kernel_maps__insert(kernel_map);
1415 * Now that we have all sorted out, just set the ->end of all
1418 kernel_maps__fixup_end();
1421 kernel_maps__fprintf(stderr);
1430 const char *vmlinux_name = "vmlinux";
1432 static void dsos__add(struct dso *dso)
1434 list_add_tail(&dso->node, &dsos);
1437 static struct dso *dsos__find(const char *name)
1441 list_for_each_entry(pos, &dsos, node)
1442 if (strcmp(pos->name, name) == 0)
1447 struct dso *dsos__findnew(const char *name)
1449 struct dso *dso = dsos__find(name);
1452 dso = dso__new(name);
1455 dso__set_basename(dso);
1462 void dsos__fprintf(FILE *fp)
1466 list_for_each_entry(pos, &dsos, node)
1467 dso__fprintf(pos, fp);
1470 size_t dsos__fprintf_buildid(FILE *fp)
1475 list_for_each_entry(pos, &dsos, node) {
1476 ret += dso__fprintf_buildid(pos, fp);
1477 ret += fprintf(fp, " %s\n", pos->long_name);
1482 struct dso *dsos__load_kernel(void)
1484 struct dso *kernel = dso__new(vmlinux_name);
1489 kernel->short_name = "[kernel]";
1490 vdso = dso__new("[vdso]");
1494 if (sysfs__read_build_id("/sys/kernel/notes", kernel->build_id,
1495 sizeof(kernel->build_id)) == 0)
1496 kernel->has_build_id = true;
1504 int load_kernel(symbol_filter_t filter, bool use_modules)
1506 struct dso *kernel = dsos__load_kernel();
1512 if (dsos__load_modules() < 0)
1513 pr_warning("Failed to load list of modules in use, "
1515 else if (dsos__load_modules_sym(filter) < 0)
1516 pr_warning("Failed to read module symbols, "
1520 if (dso__load_kernel_sym(kernel, filter, use_modules) < 0)
1521 pr_warning("Failed to read kernel symbols, continuing...\n");
1526 void symbol__init(unsigned int priv_size)
1528 elf_version(EV_CURRENT);
1529 symbol__priv_size = priv_size;