13 #include <sys/utsname.h>
15 #ifndef NT_GNU_BUILD_ID
16 #define NT_GNU_BUILD_ID 3
30 static void dsos__add(struct dso *dso);
31 static struct dso *dsos__find(const char *name);
32 static struct map *map__new2(u64 start, struct dso *dso);
33 static void kernel_maps__insert(struct map *map);
34 static int dso__load_kernel_sym(struct dso *self, struct map *map,
35 symbol_filter_t filter);
36 unsigned int symbol__priv_size;
38 static struct rb_root kernel_maps;
40 static void dso__fixup_sym_end(struct dso *self)
42 struct rb_node *nd, *prevnd = rb_first(&self->syms);
43 struct symbol *curr, *prev;
48 curr = rb_entry(prevnd, struct symbol, rb_node);
50 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
52 curr = rb_entry(nd, struct symbol, rb_node);
54 if (prev->end == prev->start)
55 prev->end = curr->start - 1;
59 if (curr->end == curr->start)
60 curr->end = roundup(curr->start, 4096);
63 static void kernel_maps__fixup_end(void)
65 struct map *prev, *curr;
66 struct rb_node *nd, *prevnd = rb_first(&kernel_maps);
71 curr = rb_entry(prevnd, struct map, rb_node);
73 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
75 curr = rb_entry(nd, struct map, rb_node);
76 prev->end = curr->start - 1;
80 * We still haven't the actual symbols, so guess the
81 * last map final address.
86 static struct symbol *symbol__new(u64 start, u64 len, const char *name)
88 size_t namelen = strlen(name) + 1;
89 struct symbol *self = calloc(1, (symbol__priv_size +
90 sizeof(*self) + namelen));
94 if (symbol__priv_size) {
95 memset(self, 0, symbol__priv_size);
96 self = ((void *)self) + symbol__priv_size;
99 self->end = len ? start + len - 1 : start;
101 pr_debug3("%s: %s %#Lx-%#Lx\n", __func__, name, start, self->end);
103 memcpy(self->name, name, namelen);
108 static void symbol__delete(struct symbol *self)
110 free(((void *)self) - symbol__priv_size);
113 static size_t symbol__fprintf(struct symbol *self, FILE *fp)
115 return fprintf(fp, " %llx-%llx %s\n",
116 self->start, self->end, self->name);
119 static void dso__set_long_name(struct dso *self, char *name)
123 self->long_name = name;
124 self->long_name_len = strlen(name);
127 static void dso__set_basename(struct dso *self)
129 self->short_name = basename(self->long_name);
132 struct dso *dso__new(const char *name)
134 struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
137 strcpy(self->name, name);
138 dso__set_long_name(self, self->name);
139 self->short_name = self->name;
140 self->syms = RB_ROOT;
141 self->find_symbol = dso__find_symbol;
142 self->slen_calculated = 0;
143 self->origin = DSO__ORIG_NOT_FOUND;
145 self->has_build_id = 0;
151 static void dso__delete_symbols(struct dso *self)
154 struct rb_node *next = rb_first(&self->syms);
157 pos = rb_entry(next, struct symbol, rb_node);
158 next = rb_next(&pos->rb_node);
159 rb_erase(&pos->rb_node, &self->syms);
164 void dso__delete(struct dso *self)
166 dso__delete_symbols(self);
167 if (self->long_name != self->name)
168 free(self->long_name);
172 void dso__set_build_id(struct dso *self, void *build_id)
174 memcpy(self->build_id, build_id, sizeof(self->build_id));
175 self->has_build_id = 1;
178 static void dso__insert_symbol(struct dso *self, struct symbol *sym)
180 struct rb_node **p = &self->syms.rb_node;
181 struct rb_node *parent = NULL;
182 const u64 ip = sym->start;
187 s = rb_entry(parent, struct symbol, rb_node);
193 rb_link_node(&sym->rb_node, parent, p);
194 rb_insert_color(&sym->rb_node, &self->syms);
197 struct symbol *dso__find_symbol(struct dso *self, u64 ip)
204 n = self->syms.rb_node;
207 struct symbol *s = rb_entry(n, struct symbol, rb_node);
211 else if (ip > s->end)
220 int build_id__sprintf(u8 *self, int len, char *bf)
226 for (i = 0; i < len; ++i) {
227 sprintf(bid, "%02x", *raw);
235 size_t dso__fprintf_buildid(struct dso *self, FILE *fp)
237 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
239 build_id__sprintf(self->build_id, sizeof(self->build_id), sbuild_id);
240 return fprintf(fp, "%s", sbuild_id);
243 size_t dso__fprintf(struct dso *self, FILE *fp)
246 size_t ret = fprintf(fp, "dso: %s (", self->short_name);
248 ret += dso__fprintf_buildid(self, fp);
249 ret += fprintf(fp, ")\n");
251 for (nd = rb_first(&self->syms); nd; nd = rb_next(nd)) {
252 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
253 ret += symbol__fprintf(pos, fp);
260 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
261 * so that we can in the next step set the symbol ->end address and then
262 * call kernel_maps__split_kallsyms.
264 static int kernel_maps__load_all_kallsyms(void)
268 FILE *file = fopen("/proc/kallsyms", "r");
273 while (!feof(file)) {
280 line_len = getline(&line, &n, file);
287 line[--line_len] = '\0'; /* \n */
289 len = hex2u64(line, &start);
292 if (len + 2 >= line_len)
295 symbol_type = toupper(line[len]);
297 * We're interested only in code ('T'ext)
299 if (symbol_type != 'T' && symbol_type != 'W')
302 symbol_name = line + len + 2;
304 * Will fix up the end later, when we have all symbols sorted.
306 sym = symbol__new(start, 0, symbol_name);
309 goto out_delete_line;
312 * We will pass the symbols to the filter later, in
313 * kernel_maps__split_kallsyms, when we have split the
316 dso__insert_symbol(kernel_map->dso, sym);
331 * Split the symbols into maps, making sure there are no overlaps, i.e. the
332 * kernel range is broken in several maps, named [kernel].N, as we don't have
333 * the original ELF section names vmlinux have.
335 static int kernel_maps__split_kallsyms(symbol_filter_t filter)
337 struct map *map = kernel_map;
340 struct rb_node *next = rb_first(&kernel_map->dso->syms);
341 int kernel_range = 0;
346 pos = rb_entry(next, struct symbol, rb_node);
347 next = rb_next(&pos->rb_node);
349 module = strchr(pos->name, '\t');
353 if (strcmp(map->dso->name, module)) {
354 map = kernel_maps__find_by_dso_name(module);
356 pr_err("/proc/{kallsyms,modules} "
362 * So that we look just like we get from .ko files,
363 * i.e. not prelinked, relative to map->start.
365 pos->start = map->map_ip(map, pos->start);
366 pos->end = map->map_ip(map, pos->end);
367 } else if (map != kernel_map) {
368 char dso_name[PATH_MAX];
371 snprintf(dso_name, sizeof(dso_name), "[kernel].%d",
374 dso = dso__new(dso_name);
378 map = map__new2(pos->start, dso);
384 map->map_ip = map->unmap_ip = identity__map_ip;
385 kernel_maps__insert(map);
389 if (filter && filter(map, pos)) {
390 rb_erase(&pos->rb_node, &kernel_map->dso->syms);
393 if (map != kernel_map) {
394 rb_erase(&pos->rb_node, &kernel_map->dso->syms);
395 dso__insert_symbol(map->dso, pos);
405 static int kernel_maps__load_kallsyms(symbol_filter_t filter)
407 if (kernel_maps__load_all_kallsyms())
410 dso__fixup_sym_end(kernel_map->dso);
411 kernel_map->dso->origin = DSO__ORIG_KERNEL;
413 return kernel_maps__split_kallsyms(filter);
416 size_t kernel_maps__fprintf(FILE *fp)
418 size_t printed = fprintf(fp, "Kernel maps:\n");
421 for (nd = rb_first(&kernel_maps); nd; nd = rb_next(nd)) {
422 struct map *pos = rb_entry(nd, struct map, rb_node);
424 printed += fprintf(fp, "Map:");
425 printed += map__fprintf(pos, fp);
427 printed += dso__fprintf(pos->dso, fp);
428 printed += fprintf(fp, "--\n");
432 return printed + fprintf(fp, "END kernel maps\n");
435 static int dso__load_perf_map(struct dso *self, struct map *map,
436 symbol_filter_t filter)
443 file = fopen(self->long_name, "r");
447 while (!feof(file)) {
452 line_len = getline(&line, &n, file);
459 line[--line_len] = '\0'; /* \n */
461 len = hex2u64(line, &start);
464 if (len + 2 >= line_len)
467 len += hex2u64(line + len, &size);
470 if (len + 2 >= line_len)
473 sym = symbol__new(start, size, line + len);
476 goto out_delete_line;
478 if (filter && filter(map, sym))
481 dso__insert_symbol(self, sym);
498 * elf_symtab__for_each_symbol - iterate thru all the symbols
500 * @self: struct elf_symtab instance to iterate
502 * @sym: GElf_Sym iterator
504 #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
505 for (idx = 0, gelf_getsym(syms, idx, &sym);\
507 idx++, gelf_getsym(syms, idx, &sym))
509 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
511 return GELF_ST_TYPE(sym->st_info);
514 static inline int elf_sym__is_function(const GElf_Sym *sym)
516 return elf_sym__type(sym) == STT_FUNC &&
518 sym->st_shndx != SHN_UNDEF;
521 static inline int elf_sym__is_label(const GElf_Sym *sym)
523 return elf_sym__type(sym) == STT_NOTYPE &&
525 sym->st_shndx != SHN_UNDEF &&
526 sym->st_shndx != SHN_ABS;
529 static inline const char *elf_sec__name(const GElf_Shdr *shdr,
530 const Elf_Data *secstrs)
532 return secstrs->d_buf + shdr->sh_name;
535 static inline int elf_sec__is_text(const GElf_Shdr *shdr,
536 const Elf_Data *secstrs)
538 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
541 static inline const char *elf_sym__name(const GElf_Sym *sym,
542 const Elf_Data *symstrs)
544 return symstrs->d_buf + sym->st_name;
547 static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
548 GElf_Shdr *shp, const char *name,
554 while ((sec = elf_nextscn(elf, sec)) != NULL) {
557 gelf_getshdr(sec, shp);
558 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
559 if (!strcmp(name, str)) {
570 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
571 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
573 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
575 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
576 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
578 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
581 * We need to check if we have a .dynsym, so that we can handle the
582 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
583 * .dynsym or .symtab).
584 * And always look at the original dso, not at debuginfo packages, that
585 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
587 static int dso__synthesize_plt_symbols(struct dso *self, struct map *map,
588 symbol_filter_t filter)
590 uint32_t nr_rel_entries, idx;
595 GElf_Shdr shdr_rel_plt, shdr_dynsym;
596 Elf_Data *reldata, *syms, *symstrs;
597 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
600 char sympltname[1024];
602 int nr = 0, symidx, fd, err = 0;
604 fd = open(self->long_name, O_RDONLY);
608 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
612 if (gelf_getehdr(elf, &ehdr) == NULL)
615 scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
616 ".dynsym", &dynsym_idx);
617 if (scn_dynsym == NULL)
620 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
622 if (scn_plt_rel == NULL) {
623 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
625 if (scn_plt_rel == NULL)
631 if (shdr_rel_plt.sh_link != dynsym_idx)
634 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
638 * Fetch the relocation section to find the idxes to the GOT
639 * and the symbols in the .dynsym they refer to.
641 reldata = elf_getdata(scn_plt_rel, NULL);
645 syms = elf_getdata(scn_dynsym, NULL);
649 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
650 if (scn_symstrs == NULL)
653 symstrs = elf_getdata(scn_symstrs, NULL);
657 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
658 plt_offset = shdr_plt.sh_offset;
660 if (shdr_rel_plt.sh_type == SHT_RELA) {
661 GElf_Rela pos_mem, *pos;
663 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
665 symidx = GELF_R_SYM(pos->r_info);
666 plt_offset += shdr_plt.sh_entsize;
667 gelf_getsym(syms, symidx, &sym);
668 snprintf(sympltname, sizeof(sympltname),
669 "%s@plt", elf_sym__name(&sym, symstrs));
671 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
676 if (filter && filter(map, f))
679 dso__insert_symbol(self, f);
683 } else if (shdr_rel_plt.sh_type == SHT_REL) {
684 GElf_Rel pos_mem, *pos;
685 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
687 symidx = GELF_R_SYM(pos->r_info);
688 plt_offset += shdr_plt.sh_entsize;
689 gelf_getsym(syms, symidx, &sym);
690 snprintf(sympltname, sizeof(sympltname),
691 "%s@plt", elf_sym__name(&sym, symstrs));
693 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
698 if (filter && filter(map, f))
701 dso__insert_symbol(self, f);
716 pr_warning("%s: problems reading %s PLT info.\n",
717 __func__, self->long_name);
721 static int dso__load_sym(struct dso *self, struct map *map, const char *name,
722 int fd, symbol_filter_t filter, int kernel,
725 struct map *curr_map = map;
726 struct dso *curr_dso = self;
727 size_t dso_name_len = strlen(self->short_name);
728 Elf_Data *symstrs, *secstrs;
736 Elf_Scn *sec, *sec_strndx;
740 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
742 pr_err("%s: cannot read %s ELF file.\n", __func__, name);
746 if (gelf_getehdr(elf, &ehdr) == NULL) {
747 pr_err("%s: cannot get elf header.\n", __func__);
751 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
753 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
758 syms = elf_getdata(sec, NULL);
762 sec = elf_getscn(elf, shdr.sh_link);
766 symstrs = elf_getdata(sec, NULL);
770 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
771 if (sec_strndx == NULL)
774 secstrs = elf_getdata(sec_strndx, NULL);
778 nr_syms = shdr.sh_size / shdr.sh_entsize;
780 memset(&sym, 0, sizeof(sym));
782 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
783 elf_section_by_name(elf, &ehdr, &shdr,
786 } else self->adjust_symbols = 0;
788 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
790 const char *elf_name;
791 char *demangled = NULL;
792 int is_label = elf_sym__is_label(&sym);
793 const char *section_name;
795 if (!is_label && !elf_sym__is_function(&sym))
798 sec = elf_getscn(elf, sym.st_shndx);
802 gelf_getshdr(sec, &shdr);
804 if (is_label && !elf_sec__is_text(&shdr, secstrs))
807 elf_name = elf_sym__name(&sym, symstrs);
808 section_name = elf_sec__name(&shdr, secstrs);
810 if (kernel || kmodule) {
811 char dso_name[PATH_MAX];
813 if (strcmp(section_name,
814 curr_dso->short_name + dso_name_len) == 0)
817 if (strcmp(section_name, ".text") == 0) {
823 snprintf(dso_name, sizeof(dso_name),
824 "%s%s", self->short_name, section_name);
826 curr_map = kernel_maps__find_by_dso_name(dso_name);
827 if (curr_map == NULL) {
828 u64 start = sym.st_value;
831 start += map->start + shdr.sh_offset;
833 curr_dso = dso__new(dso_name);
834 if (curr_dso == NULL)
836 curr_map = map__new2(start, curr_dso);
837 if (curr_map == NULL) {
838 dso__delete(curr_dso);
841 curr_map->map_ip = identity__map_ip;
842 curr_map->unmap_ip = identity__map_ip;
843 curr_dso->origin = DSO__ORIG_KERNEL;
844 kernel_maps__insert(curr_map);
847 curr_dso = curr_map->dso;
852 if (curr_dso->adjust_symbols) {
853 pr_debug2("adjusting symbol: st_value: %Lx sh_addr: "
854 "%Lx sh_offset: %Lx\n", (u64)sym.st_value,
855 (u64)shdr.sh_addr, (u64)shdr.sh_offset);
856 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
859 * We need to figure out if the object was created from C++ sources
860 * DWARF DW_compile_unit has this, but we don't always have access
863 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
864 if (demangled != NULL)
865 elf_name = demangled;
867 f = symbol__new(sym.st_value, sym.st_size, elf_name);
872 if (filter && filter(curr_map, f))
875 dso__insert_symbol(curr_dso, f);
881 * For misannotated, zeroed, ASM function sizes.
884 dso__fixup_sym_end(self);
892 static bool dso__build_id_equal(const struct dso *self, u8 *build_id)
894 return memcmp(self->build_id, build_id, sizeof(self->build_id)) == 0;
897 bool dsos__read_build_ids(void)
899 bool have_build_id = false;
902 list_for_each_entry(pos, &dsos, node)
903 if (filename__read_build_id(pos->long_name, pos->build_id,
904 sizeof(pos->build_id)) > 0) {
905 have_build_id = true;
906 pos->has_build_id = true;
909 return have_build_id;
913 * Align offset to 4 bytes as needed for note name and descriptor data.
915 #define NOTE_ALIGN(n) (((n) + 3) & -4U)
917 int filename__read_build_id(const char *filename, void *bf, size_t size)
928 if (size < BUILD_ID_SIZE)
931 fd = open(filename, O_RDONLY);
935 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
937 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
945 if (gelf_getehdr(elf, &ehdr) == NULL) {
946 pr_err("%s: cannot get elf header.\n", __func__);
950 sec = elf_section_by_name(elf, &ehdr, &shdr,
951 ".note.gnu.build-id", NULL);
953 sec = elf_section_by_name(elf, &ehdr, &shdr,
959 data = elf_getdata(sec, NULL);
964 while (ptr < (data->d_buf + data->d_size)) {
965 GElf_Nhdr *nhdr = ptr;
966 int namesz = NOTE_ALIGN(nhdr->n_namesz),
967 descsz = NOTE_ALIGN(nhdr->n_descsz);
970 ptr += sizeof(*nhdr);
973 if (nhdr->n_type == NT_GNU_BUILD_ID &&
974 nhdr->n_namesz == sizeof("GNU")) {
975 if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
976 memcpy(bf, ptr, BUILD_ID_SIZE);
991 int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
995 if (size < BUILD_ID_SIZE)
998 fd = open(filename, O_RDONLY);
1007 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
1010 namesz = NOTE_ALIGN(nhdr.n_namesz);
1011 descsz = NOTE_ALIGN(nhdr.n_descsz);
1012 if (nhdr.n_type == NT_GNU_BUILD_ID &&
1013 nhdr.n_namesz == sizeof("GNU")) {
1014 if (read(fd, bf, namesz) != namesz)
1016 if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
1017 if (read(fd, build_id,
1018 BUILD_ID_SIZE) == BUILD_ID_SIZE) {
1022 } else if (read(fd, bf, descsz) != descsz)
1025 int n = namesz + descsz;
1026 if (read(fd, bf, n) != n)
1035 char dso__symtab_origin(const struct dso *self)
1037 static const char origin[] = {
1038 [DSO__ORIG_KERNEL] = 'k',
1039 [DSO__ORIG_JAVA_JIT] = 'j',
1040 [DSO__ORIG_FEDORA] = 'f',
1041 [DSO__ORIG_UBUNTU] = 'u',
1042 [DSO__ORIG_BUILDID] = 'b',
1043 [DSO__ORIG_DSO] = 'd',
1044 [DSO__ORIG_KMODULE] = 'K',
1047 if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
1049 return origin[self->origin];
1052 int dso__load(struct dso *self, struct map *map, symbol_filter_t filter)
1054 int size = PATH_MAX;
1056 u8 build_id[BUILD_ID_SIZE];
1063 return dso__load_kernel_sym(self, map, filter);
1065 name = malloc(size);
1069 self->adjust_symbols = 0;
1071 if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
1072 ret = dso__load_perf_map(self, map, filter);
1073 self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
1074 DSO__ORIG_NOT_FOUND;
1078 self->origin = DSO__ORIG_FEDORA - 1;
1083 switch (self->origin) {
1084 case DSO__ORIG_FEDORA:
1085 snprintf(name, size, "/usr/lib/debug%s.debug",
1088 case DSO__ORIG_UBUNTU:
1089 snprintf(name, size, "/usr/lib/debug%s",
1092 case DSO__ORIG_BUILDID:
1093 if (filename__read_build_id(self->long_name, build_id,
1094 sizeof(build_id))) {
1095 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
1097 build_id__sprintf(build_id, sizeof(build_id),
1099 snprintf(name, size,
1100 "/usr/lib/debug/.build-id/%.2s/%s.debug",
1101 build_id_hex, build_id_hex + 2);
1102 if (self->has_build_id)
1103 goto compare_build_id;
1109 snprintf(name, size, "%s", self->long_name);
1116 if (self->has_build_id) {
1117 if (filename__read_build_id(name, build_id,
1118 sizeof(build_id)) < 0)
1121 if (!dso__build_id_equal(self, build_id))
1125 fd = open(name, O_RDONLY);
1128 ret = dso__load_sym(self, map, name, fd, filter, 0, 0);
1132 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
1138 int nr_plt = dso__synthesize_plt_symbols(self, map, filter);
1144 if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
1149 struct map *kernel_map;
1151 static void kernel_maps__insert(struct map *map)
1153 maps__insert(&kernel_maps, map);
1156 struct symbol *kernel_maps__find_symbol(u64 ip, struct map **mapp,
1157 symbol_filter_t filter)
1159 struct map *map = maps__find(&kernel_maps, ip);
1165 ip = map->map_ip(map, ip);
1166 return map__find_symbol(map, ip, filter);
1172 struct map *kernel_maps__find_by_dso_name(const char *name)
1176 for (nd = rb_first(&kernel_maps); nd; nd = rb_next(nd)) {
1177 struct map *map = rb_entry(nd, struct map, rb_node);
1179 if (map->dso && strcmp(map->dso->name, name) == 0)
1186 static int dsos__set_modules_path_dir(char *dirname)
1188 struct dirent *dent;
1189 DIR *dir = opendir(dirname);
1192 pr_err("%s: cannot open %s dir\n", __func__, dirname);
1196 while ((dent = readdir(dir)) != NULL) {
1197 char path[PATH_MAX];
1199 if (dent->d_type == DT_DIR) {
1200 if (!strcmp(dent->d_name, ".") ||
1201 !strcmp(dent->d_name, ".."))
1204 snprintf(path, sizeof(path), "%s/%s",
1205 dirname, dent->d_name);
1206 if (dsos__set_modules_path_dir(path) < 0)
1209 char *dot = strrchr(dent->d_name, '.'),
1214 if (dot == NULL || strcmp(dot, ".ko"))
1216 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1217 (int)(dot - dent->d_name), dent->d_name);
1219 strxfrchar(dso_name, '-', '_');
1220 map = kernel_maps__find_by_dso_name(dso_name);
1224 snprintf(path, sizeof(path), "%s/%s",
1225 dirname, dent->d_name);
1227 long_name = strdup(path);
1228 if (long_name == NULL)
1230 dso__set_long_name(map->dso, long_name);
1240 static int dsos__set_modules_path(void)
1243 char modules_path[PATH_MAX];
1245 if (uname(&uts) < 0)
1248 snprintf(modules_path, sizeof(modules_path), "/lib/modules/%s/kernel",
1251 return dsos__set_modules_path_dir(modules_path);
1255 * Constructor variant for modules (where we know from /proc/modules where
1256 * they are loaded) and for vmlinux, where only after we load all the
1257 * symbols we'll know where it starts and ends.
1259 static struct map *map__new2(u64 start, struct dso *dso)
1261 struct map *self = malloc(sizeof(*self));
1265 * ->end will be filled after we load all the symbols
1267 map__init(self, start, 0, 0, dso);
1273 static int kernel_maps__create_module_maps(void)
1277 FILE *file = fopen("/proc/modules", "r");
1283 while (!feof(file)) {
1284 char name[PATH_MAX];
1290 line_len = getline(&line, &n, file);
1297 line[--line_len] = '\0'; /* \n */
1299 sep = strrchr(line, 'x');
1303 hex2u64(sep + 1, &start);
1305 sep = strchr(line, ' ');
1311 snprintf(name, sizeof(name), "[%s]", line);
1312 dso = dso__new(name);
1315 goto out_delete_line;
1317 map = map__new2(start, dso);
1320 goto out_delete_line;
1323 snprintf(name, sizeof(name),
1324 "/sys/module/%s/notes/.note.gnu.build-id", line);
1325 if (sysfs__read_build_id(name, dso->build_id,
1326 sizeof(dso->build_id)) == 0)
1327 dso->has_build_id = true;
1329 dso->origin = DSO__ORIG_KMODULE;
1330 kernel_maps__insert(map);
1337 return dsos__set_modules_path();
1345 static int dso__load_vmlinux(struct dso *self, struct map *map,
1346 const char *vmlinux, symbol_filter_t filter)
1350 if (self->has_build_id) {
1351 u8 build_id[BUILD_ID_SIZE];
1353 if (filename__read_build_id(vmlinux, build_id,
1354 sizeof(build_id)) < 0) {
1355 pr_debug("No build_id in %s, ignoring it\n", vmlinux);
1358 if (!dso__build_id_equal(self, build_id)) {
1359 char expected_build_id[BUILD_ID_SIZE * 2 + 1],
1360 vmlinux_build_id[BUILD_ID_SIZE * 2 + 1];
1362 build_id__sprintf(self->build_id,
1363 sizeof(self->build_id),
1365 build_id__sprintf(build_id, sizeof(build_id),
1367 pr_debug("build_id in %s is %s while expected is %s, "
1368 "ignoring it\n", vmlinux, vmlinux_build_id,
1374 fd = open(vmlinux, O_RDONLY);
1379 err = dso__load_sym(self, map, self->long_name, fd, filter, 1, 0);
1386 static int dso__load_kernel_sym(struct dso *self, struct map *map,
1387 symbol_filter_t filter)
1389 int err = dso__load_vmlinux(self, map, self->name, filter);
1392 err = kernel_maps__load_kallsyms(filter);
1394 dso__set_long_name(self, strdup("[kernel.kallsyms]"));
1398 map__fixup_start(map);
1399 map__fixup_end(map);
1408 const char *vmlinux_name = "vmlinux";
1410 static void dsos__add(struct dso *dso)
1412 list_add_tail(&dso->node, &dsos);
1415 static struct dso *dsos__find(const char *name)
1419 list_for_each_entry(pos, &dsos, node)
1420 if (strcmp(pos->name, name) == 0)
1425 struct dso *dsos__findnew(const char *name)
1427 struct dso *dso = dsos__find(name);
1430 dso = dso__new(name);
1433 dso__set_basename(dso);
1440 void dsos__fprintf(FILE *fp)
1444 list_for_each_entry(pos, &dsos, node)
1445 dso__fprintf(pos, fp);
1448 size_t dsos__fprintf_buildid(FILE *fp)
1453 list_for_each_entry(pos, &dsos, node) {
1454 ret += dso__fprintf_buildid(pos, fp);
1455 ret += fprintf(fp, " %s\n", pos->long_name);
1460 static int kernel_maps__create_kernel_map(void)
1462 struct dso *kernel = dso__new(vmlinux_name);
1467 kernel_map = map__new2(0, kernel);
1468 if (kernel_map == NULL)
1469 goto out_delete_kernel_dso;
1471 kernel_map->map_ip = kernel_map->unmap_ip = identity__map_ip;
1473 kernel->short_name = "[kernel]";
1475 vdso = dso__new("[vdso]");
1477 goto out_delete_kernel_map;
1479 if (sysfs__read_build_id("/sys/kernel/notes", kernel->build_id,
1480 sizeof(kernel->build_id)) == 0)
1481 kernel->has_build_id = true;
1483 kernel_maps__insert(kernel_map);
1489 out_delete_kernel_map:
1490 map__delete(kernel_map);
1492 out_delete_kernel_dso:
1493 dso__delete(kernel);
1497 int kernel_maps__init(bool use_modules)
1499 if (kernel_maps__create_kernel_map() < 0)
1502 if (use_modules && kernel_maps__create_module_maps() < 0)
1503 pr_warning("Failed to load list of modules in use, "
1506 * Now that we have all the maps created, just set the ->end of them:
1508 kernel_maps__fixup_end();
1512 void symbol__init(unsigned int priv_size)
1514 elf_version(EV_CURRENT);
1515 symbol__priv_size = priv_size;