12 #include <sys/utsname.h>
25 static void dsos__add(struct dso *dso);
26 static struct dso *dsos__find(const char *name);
27 static struct map *map__new2(u64 start, struct dso *dso);
28 static void kernel_maps__insert(struct map *map);
29 unsigned int symbol__priv_size;
31 static struct rb_root kernel_maps;
33 static void dso__fixup_sym_end(struct dso *self)
35 struct rb_node *nd, *prevnd = rb_first(&self->syms);
36 struct symbol *curr, *prev;
41 curr = rb_entry(prevnd, struct symbol, rb_node);
43 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
45 curr = rb_entry(nd, struct symbol, rb_node);
47 if (prev->end == prev->start)
48 prev->end = curr->start - 1;
52 if (curr->end == curr->start)
53 curr->end = roundup(curr->start, 4096);
56 static void kernel_maps__fixup_end(void)
58 struct map *prev, *curr;
59 struct rb_node *nd, *prevnd = rb_first(&kernel_maps);
64 curr = rb_entry(prevnd, struct map, rb_node);
66 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
68 curr = rb_entry(nd, struct map, rb_node);
69 prev->end = curr->start - 1;
72 nd = rb_last(&curr->dso->syms);
74 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
79 static struct symbol *symbol__new(u64 start, u64 len, const char *name)
81 size_t namelen = strlen(name) + 1;
82 struct symbol *self = calloc(1, (symbol__priv_size +
83 sizeof(*self) + namelen));
87 if (symbol__priv_size) {
88 memset(self, 0, symbol__priv_size);
89 self = ((void *)self) + symbol__priv_size;
92 self->end = len ? start + len - 1 : start;
94 pr_debug3("%s: %s %#Lx-%#Lx\n", __func__, name, start, self->end);
96 memcpy(self->name, name, namelen);
101 static void symbol__delete(struct symbol *self)
103 free(((void *)self) - symbol__priv_size);
106 static size_t symbol__fprintf(struct symbol *self, FILE *fp)
108 return fprintf(fp, " %llx-%llx %s\n",
109 self->start, self->end, self->name);
112 struct dso *dso__new(const char *name)
114 struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
117 strcpy(self->name, name);
118 self->long_name = self->name;
119 self->short_name = self->name;
120 self->syms = RB_ROOT;
121 self->find_symbol = dso__find_symbol;
122 self->slen_calculated = 0;
123 self->origin = DSO__ORIG_NOT_FOUND;
125 self->has_build_id = 0;
131 static void dso__delete_symbols(struct dso *self)
134 struct rb_node *next = rb_first(&self->syms);
137 pos = rb_entry(next, struct symbol, rb_node);
138 next = rb_next(&pos->rb_node);
139 rb_erase(&pos->rb_node, &self->syms);
144 void dso__delete(struct dso *self)
146 dso__delete_symbols(self);
147 if (self->long_name != self->name)
148 free(self->long_name);
152 void dso__set_build_id(struct dso *self, void *build_id)
154 memcpy(self->build_id, build_id, sizeof(self->build_id));
155 self->has_build_id = 1;
158 static void dso__insert_symbol(struct dso *self, struct symbol *sym)
160 struct rb_node **p = &self->syms.rb_node;
161 struct rb_node *parent = NULL;
162 const u64 ip = sym->start;
167 s = rb_entry(parent, struct symbol, rb_node);
173 rb_link_node(&sym->rb_node, parent, p);
174 rb_insert_color(&sym->rb_node, &self->syms);
177 struct symbol *dso__find_symbol(struct dso *self, u64 ip)
184 n = self->syms.rb_node;
187 struct symbol *s = rb_entry(n, struct symbol, rb_node);
191 else if (ip > s->end)
200 int build_id__sprintf(u8 *self, int len, char *bf)
206 for (i = 0; i < len; ++i) {
207 sprintf(bid, "%02x", *raw);
215 size_t dso__fprintf(struct dso *self, FILE *fp)
217 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
221 build_id__sprintf(self->build_id, sizeof(self->build_id), sbuild_id);
222 ret = fprintf(fp, "dso: %s (%s)\n", self->short_name, sbuild_id);
224 for (nd = rb_first(&self->syms); nd; nd = rb_next(nd)) {
225 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
226 ret += symbol__fprintf(pos, fp);
233 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
234 * so that we can in the next step set the symbol ->end address and then
235 * call kernel_maps__split_kallsyms.
237 static int kernel_maps__load_all_kallsyms(void)
241 FILE *file = fopen("/proc/kallsyms", "r");
246 while (!feof(file)) {
253 line_len = getline(&line, &n, file);
260 line[--line_len] = '\0'; /* \n */
262 len = hex2u64(line, &start);
265 if (len + 2 >= line_len)
268 symbol_type = toupper(line[len]);
270 * We're interested only in code ('T'ext)
272 if (symbol_type != 'T' && symbol_type != 'W')
275 symbol_name = line + len + 2;
277 * Will fix up the end later, when we have all symbols sorted.
279 sym = symbol__new(start, 0, symbol_name);
282 goto out_delete_line;
284 dso__insert_symbol(kernel_map->dso, sym);
299 * Split the symbols into maps, making sure there are no overlaps, i.e. the
300 * kernel range is broken in several maps, named [kernel].N, as we don't have
301 * the original ELF section names vmlinux have.
303 static int kernel_maps__split_kallsyms(symbol_filter_t filter, int use_modules)
305 struct map *map = kernel_map;
308 struct rb_node *next = rb_first(&kernel_map->dso->syms);
309 int kernel_range = 0;
314 pos = rb_entry(next, struct symbol, rb_node);
315 next = rb_next(&pos->rb_node);
317 module = strchr(pos->name, '\t');
324 if (strcmp(map->dso->name, module)) {
325 map = kernel_maps__find_by_dso_name(module);
327 pr_err("/proc/{kallsyms,modules} "
333 * So that we look just like we get from .ko files,
334 * i.e. not prelinked, relative to map->start.
336 pos->start = map->map_ip(map, pos->start);
337 pos->end = map->map_ip(map, pos->end);
338 } else if (map != kernel_map) {
339 char dso_name[PATH_MAX];
342 snprintf(dso_name, sizeof(dso_name), "[kernel].%d",
345 dso = dso__new(dso_name);
349 map = map__new2(pos->start, dso);
355 map->map_ip = map->unmap_ip = identity__map_ip;
356 kernel_maps__insert(map);
360 if (filter && filter(map, pos)) {
362 rb_erase(&pos->rb_node, &kernel_map->dso->syms);
365 if (map != kernel_map) {
366 rb_erase(&pos->rb_node, &kernel_map->dso->syms);
367 dso__insert_symbol(map->dso, pos);
377 static int kernel_maps__load_kallsyms(symbol_filter_t filter, int use_modules)
379 if (kernel_maps__load_all_kallsyms())
382 dso__fixup_sym_end(kernel_map->dso);
384 return kernel_maps__split_kallsyms(filter, use_modules);
387 static size_t kernel_maps__fprintf(FILE *fp)
389 size_t printed = fprintf(fp, "Kernel maps:\n");
392 for (nd = rb_first(&kernel_maps); nd; nd = rb_next(nd)) {
393 struct map *pos = rb_entry(nd, struct map, rb_node);
395 printed += fprintf(fp, "Map:");
396 printed += map__fprintf(pos, fp);
398 printed += dso__fprintf(pos->dso, fp);
399 printed += fprintf(fp, "--\n");
403 return printed + fprintf(fp, "END kernel maps\n");
406 static int dso__load_perf_map(struct dso *self, struct map *map,
407 symbol_filter_t filter)
414 file = fopen(self->long_name, "r");
418 while (!feof(file)) {
423 line_len = getline(&line, &n, file);
430 line[--line_len] = '\0'; /* \n */
432 len = hex2u64(line, &start);
435 if (len + 2 >= line_len)
438 len += hex2u64(line + len, &size);
441 if (len + 2 >= line_len)
444 sym = symbol__new(start, size, line + len);
447 goto out_delete_line;
449 if (filter && filter(map, sym))
452 dso__insert_symbol(self, sym);
469 * elf_symtab__for_each_symbol - iterate thru all the symbols
471 * @self: struct elf_symtab instance to iterate
473 * @sym: GElf_Sym iterator
475 #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
476 for (idx = 0, gelf_getsym(syms, idx, &sym);\
478 idx++, gelf_getsym(syms, idx, &sym))
480 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
482 return GELF_ST_TYPE(sym->st_info);
485 static inline int elf_sym__is_function(const GElf_Sym *sym)
487 return elf_sym__type(sym) == STT_FUNC &&
489 sym->st_shndx != SHN_UNDEF;
492 static inline int elf_sym__is_label(const GElf_Sym *sym)
494 return elf_sym__type(sym) == STT_NOTYPE &&
496 sym->st_shndx != SHN_UNDEF &&
497 sym->st_shndx != SHN_ABS;
500 static inline const char *elf_sec__name(const GElf_Shdr *shdr,
501 const Elf_Data *secstrs)
503 return secstrs->d_buf + shdr->sh_name;
506 static inline int elf_sec__is_text(const GElf_Shdr *shdr,
507 const Elf_Data *secstrs)
509 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
512 static inline const char *elf_sym__name(const GElf_Sym *sym,
513 const Elf_Data *symstrs)
515 return symstrs->d_buf + sym->st_name;
518 static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
519 GElf_Shdr *shp, const char *name,
525 while ((sec = elf_nextscn(elf, sec)) != NULL) {
528 gelf_getshdr(sec, shp);
529 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
530 if (!strcmp(name, str)) {
541 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
542 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
544 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
546 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
547 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
549 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
552 * We need to check if we have a .dynsym, so that we can handle the
553 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
554 * .dynsym or .symtab).
555 * And always look at the original dso, not at debuginfo packages, that
556 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
558 static int dso__synthesize_plt_symbols(struct dso *self)
560 uint32_t nr_rel_entries, idx;
565 GElf_Shdr shdr_rel_plt, shdr_dynsym;
566 Elf_Data *reldata, *syms, *symstrs;
567 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
570 char sympltname[1024];
572 int nr = 0, symidx, fd, err = 0;
574 fd = open(self->long_name, O_RDONLY);
578 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
582 if (gelf_getehdr(elf, &ehdr) == NULL)
585 scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
586 ".dynsym", &dynsym_idx);
587 if (scn_dynsym == NULL)
590 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
592 if (scn_plt_rel == NULL) {
593 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
595 if (scn_plt_rel == NULL)
601 if (shdr_rel_plt.sh_link != dynsym_idx)
604 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
608 * Fetch the relocation section to find the idxes to the GOT
609 * and the symbols in the .dynsym they refer to.
611 reldata = elf_getdata(scn_plt_rel, NULL);
615 syms = elf_getdata(scn_dynsym, NULL);
619 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
620 if (scn_symstrs == NULL)
623 symstrs = elf_getdata(scn_symstrs, NULL);
627 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
628 plt_offset = shdr_plt.sh_offset;
630 if (shdr_rel_plt.sh_type == SHT_RELA) {
631 GElf_Rela pos_mem, *pos;
633 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
635 symidx = GELF_R_SYM(pos->r_info);
636 plt_offset += shdr_plt.sh_entsize;
637 gelf_getsym(syms, symidx, &sym);
638 snprintf(sympltname, sizeof(sympltname),
639 "%s@plt", elf_sym__name(&sym, symstrs));
641 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
646 dso__insert_symbol(self, f);
649 } else if (shdr_rel_plt.sh_type == SHT_REL) {
650 GElf_Rel pos_mem, *pos;
651 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
653 symidx = GELF_R_SYM(pos->r_info);
654 plt_offset += shdr_plt.sh_entsize;
655 gelf_getsym(syms, symidx, &sym);
656 snprintf(sympltname, sizeof(sympltname),
657 "%s@plt", elf_sym__name(&sym, symstrs));
659 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
664 dso__insert_symbol(self, f);
678 pr_warning("%s: problems reading %s PLT info.\n",
679 __func__, self->long_name);
683 static int dso__load_sym(struct dso *self, struct map *map, const char *name,
684 int fd, symbol_filter_t filter, int kernel,
687 struct map *curr_map = map;
688 struct dso *curr_dso = self;
689 size_t dso_name_len = strlen(self->short_name);
690 Elf_Data *symstrs, *secstrs;
698 Elf_Scn *sec, *sec_strndx;
702 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
704 pr_err("%s: cannot read %s ELF file.\n", __func__, name);
708 if (gelf_getehdr(elf, &ehdr) == NULL) {
709 pr_err("%s: cannot get elf header.\n", __func__);
713 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
715 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
720 syms = elf_getdata(sec, NULL);
724 sec = elf_getscn(elf, shdr.sh_link);
728 symstrs = elf_getdata(sec, NULL);
732 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
733 if (sec_strndx == NULL)
736 secstrs = elf_getdata(sec_strndx, NULL);
740 nr_syms = shdr.sh_size / shdr.sh_entsize;
742 memset(&sym, 0, sizeof(sym));
744 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
745 elf_section_by_name(elf, &ehdr, &shdr,
748 } else self->adjust_symbols = 0;
750 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
752 const char *elf_name;
753 char *demangled = NULL;
754 int is_label = elf_sym__is_label(&sym);
755 const char *section_name;
757 if (!is_label && !elf_sym__is_function(&sym))
760 sec = elf_getscn(elf, sym.st_shndx);
764 gelf_getshdr(sec, &shdr);
766 if (is_label && !elf_sec__is_text(&shdr, secstrs))
769 elf_name = elf_sym__name(&sym, symstrs);
770 section_name = elf_sec__name(&shdr, secstrs);
772 if (kernel || kmodule) {
773 char dso_name[PATH_MAX];
775 if (strcmp(section_name,
776 curr_dso->short_name + dso_name_len) == 0)
779 if (strcmp(section_name, ".text") == 0) {
785 snprintf(dso_name, sizeof(dso_name),
786 "%s%s", self->short_name, section_name);
788 curr_map = kernel_maps__find_by_dso_name(dso_name);
789 if (curr_map == NULL) {
790 u64 start = sym.st_value;
793 start += map->start + shdr.sh_offset;
795 curr_dso = dso__new(dso_name);
796 if (curr_dso == NULL)
798 curr_map = map__new2(start, curr_dso);
799 if (curr_map == NULL) {
800 dso__delete(curr_dso);
803 curr_map->map_ip = identity__map_ip;
804 curr_map->unmap_ip = identity__map_ip;
805 curr_dso->origin = DSO__ORIG_KERNEL;
806 kernel_maps__insert(curr_map);
809 curr_dso = curr_map->dso;
814 if (curr_dso->adjust_symbols) {
815 pr_debug2("adjusting symbol: st_value: %Lx sh_addr: "
816 "%Lx sh_offset: %Lx\n", (u64)sym.st_value,
817 (u64)shdr.sh_addr, (u64)shdr.sh_offset);
818 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
821 * We need to figure out if the object was created from C++ sources
822 * DWARF DW_compile_unit has this, but we don't always have access
825 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
826 if (demangled != NULL)
827 elf_name = demangled;
829 f = symbol__new(sym.st_value, sym.st_size, elf_name);
834 if (filter && filter(curr_map, f))
837 dso__insert_symbol(curr_dso, f);
843 * For misannotated, zeroed, ASM function sizes.
846 dso__fixup_sym_end(self);
854 int filename__read_build_id(const char *filename, void *bf, size_t size)
859 Elf_Data *build_id_data;
863 if (size < BUILD_ID_SIZE)
866 fd = open(filename, O_RDONLY);
870 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
872 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
876 if (gelf_getehdr(elf, &ehdr) == NULL) {
877 pr_err("%s: cannot get elf header.\n", __func__);
881 sec = elf_section_by_name(elf, &ehdr, &shdr,
882 ".note.gnu.build-id", NULL);
886 build_id_data = elf_getdata(sec, NULL);
887 if (build_id_data == NULL)
889 memcpy(bf, build_id_data->d_buf + 16, BUILD_ID_SIZE);
899 static char *dso__read_build_id(struct dso *self)
902 char *build_id = NULL;
903 unsigned char rawbf[BUILD_ID_SIZE];
905 len = filename__read_build_id(self->long_name, rawbf, sizeof(rawbf));
909 build_id = malloc(len * 2 + 1);
910 if (build_id == NULL)
913 build_id__sprintf(rawbf, len, build_id);
918 char dso__symtab_origin(const struct dso *self)
920 static const char origin[] = {
921 [DSO__ORIG_KERNEL] = 'k',
922 [DSO__ORIG_JAVA_JIT] = 'j',
923 [DSO__ORIG_FEDORA] = 'f',
924 [DSO__ORIG_UBUNTU] = 'u',
925 [DSO__ORIG_BUILDID] = 'b',
926 [DSO__ORIG_DSO] = 'd',
927 [DSO__ORIG_KMODULE] = 'K',
930 if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
932 return origin[self->origin];
935 int dso__load(struct dso *self, struct map *map, symbol_filter_t filter)
938 char *name = malloc(size), *build_id = NULL;
947 self->adjust_symbols = 0;
949 if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
950 ret = dso__load_perf_map(self, map, filter);
951 self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
956 self->origin = DSO__ORIG_FEDORA - 1;
963 switch (self->origin) {
964 case DSO__ORIG_FEDORA:
965 snprintf(name, size, "/usr/lib/debug%s.debug",
968 case DSO__ORIG_UBUNTU:
969 snprintf(name, size, "/usr/lib/debug%s",
972 case DSO__ORIG_BUILDID:
973 build_id = dso__read_build_id(self);
974 if (build_id != NULL) {
976 "/usr/lib/debug/.build-id/%.2s/%s.debug",
977 build_id, build_id + 2);
978 goto compare_build_id;
983 snprintf(name, size, "%s", self->long_name);
990 if (self->has_build_id) {
992 build_id = malloc(BUILD_ID_SIZE);
993 if (build_id == NULL)
995 berr = filename__read_build_id(name, build_id,
998 match = berr > 0 && memcmp(build_id, self->build_id,
999 sizeof(self->build_id)) == 0;
1006 fd = open(name, O_RDONLY);
1009 ret = dso__load_sym(self, map, name, fd, filter, 0, 0);
1013 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
1019 int nr_plt = dso__synthesize_plt_symbols(self);
1025 if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
1030 struct map *kernel_map;
1032 static void kernel_maps__insert(struct map *map)
1034 maps__insert(&kernel_maps, map);
1037 struct symbol *kernel_maps__find_symbol(u64 ip, struct map **mapp)
1039 struct map *map = maps__find(&kernel_maps, ip);
1045 ip = map->map_ip(map, ip);
1046 return map->dso->find_symbol(map->dso, ip);
1052 struct map *kernel_maps__find_by_dso_name(const char *name)
1056 for (nd = rb_first(&kernel_maps); nd; nd = rb_next(nd)) {
1057 struct map *map = rb_entry(nd, struct map, rb_node);
1059 if (map->dso && strcmp(map->dso->name, name) == 0)
1066 static int dso__load_module_sym(struct dso *self, struct map *map,
1067 symbol_filter_t filter)
1069 int err = 0, fd = open(self->long_name, O_RDONLY);
1074 pr_err("%s: cannot open %s\n", __func__, self->long_name);
1078 err = dso__load_sym(self, map, self->long_name, fd, filter, 0, 1);
1084 static int dsos__load_modules_sym_dir(char *dirname, symbol_filter_t filter)
1086 struct dirent *dent;
1087 int nr_symbols = 0, err;
1088 DIR *dir = opendir(dirname);
1091 pr_err("%s: cannot open %s dir\n", __func__, dirname);
1095 while ((dent = readdir(dir)) != NULL) {
1096 char path[PATH_MAX];
1098 if (dent->d_type == DT_DIR) {
1099 if (!strcmp(dent->d_name, ".") ||
1100 !strcmp(dent->d_name, ".."))
1103 snprintf(path, sizeof(path), "%s/%s",
1104 dirname, dent->d_name);
1105 err = dsos__load_modules_sym_dir(path, filter);
1109 char *dot = strrchr(dent->d_name, '.'),
1112 struct rb_node *last;
1114 if (dot == NULL || strcmp(dot, ".ko"))
1116 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1117 (int)(dot - dent->d_name), dent->d_name);
1119 strxfrchar(dso_name, '-', '_');
1120 map = kernel_maps__find_by_dso_name(dso_name);
1124 snprintf(path, sizeof(path), "%s/%s",
1125 dirname, dent->d_name);
1127 map->dso->long_name = strdup(path);
1128 if (map->dso->long_name == NULL)
1131 err = dso__load_module_sym(map->dso, map, filter);
1134 last = rb_last(&map->dso->syms);
1138 * We do this here as well, even having the
1139 * symbol size found in the symtab because
1140 * misannotated ASM symbols may have the size
1143 dso__fixup_sym_end(map->dso);
1145 sym = rb_entry(last, struct symbol, rb_node);
1146 map->end = map->start + sym->end;
1158 static int dsos__load_modules_sym(symbol_filter_t filter)
1161 char modules_path[PATH_MAX];
1163 if (uname(&uts) < 0)
1166 snprintf(modules_path, sizeof(modules_path), "/lib/modules/%s/kernel",
1169 return dsos__load_modules_sym_dir(modules_path, filter);
1173 * Constructor variant for modules (where we know from /proc/modules where
1174 * they are loaded) and for vmlinux, where only after we load all the
1175 * symbols we'll know where it starts and ends.
1177 static struct map *map__new2(u64 start, struct dso *dso)
1179 struct map *self = malloc(sizeof(*self));
1183 * ->end will be filled after we load all the symbols
1185 map__init(self, start, 0, 0, dso);
1191 static int dsos__load_modules(void)
1195 FILE *file = fopen("/proc/modules", "r");
1201 while (!feof(file)) {
1202 char name[PATH_MAX];
1208 line_len = getline(&line, &n, file);
1215 line[--line_len] = '\0'; /* \n */
1217 sep = strrchr(line, 'x');
1221 hex2u64(sep + 1, &start);
1223 sep = strchr(line, ' ');
1229 snprintf(name, sizeof(name), "[%s]", line);
1230 dso = dso__new(name);
1233 goto out_delete_line;
1235 map = map__new2(start, dso);
1238 goto out_delete_line;
1241 dso->origin = DSO__ORIG_KMODULE;
1242 kernel_maps__insert(map);
1257 static int dso__load_vmlinux(struct dso *self, struct map *map,
1258 const char *vmlinux, symbol_filter_t filter)
1260 int err, fd = open(vmlinux, O_RDONLY);
1267 err = dso__load_sym(self, map, self->long_name, fd, filter, 1, 0);
1274 int dsos__load_kernel(const char *vmlinux, symbol_filter_t filter,
1278 struct dso *dso = dso__new(vmlinux);
1283 dso->short_name = "[kernel]";
1284 kernel_map = map__new2(0, dso);
1285 if (kernel_map == NULL)
1286 goto out_delete_dso;
1288 kernel_map->map_ip = kernel_map->unmap_ip = identity__map_ip;
1290 if (use_modules && dsos__load_modules() < 0) {
1291 pr_warning("Failed to load list of modules in use! "
1297 err = dso__load_vmlinux(dso, kernel_map, vmlinux, filter);
1298 if (err > 0 && use_modules) {
1299 int syms = dsos__load_modules_sym(filter);
1302 pr_warning("Failed to read module symbols!"
1303 " Continuing...\n");
1310 err = kernel_maps__load_kallsyms(filter, use_modules);
1313 struct rb_node *node = rb_first(&dso->syms);
1314 struct symbol *sym = rb_entry(node, struct symbol, rb_node);
1316 kernel_map->start = sym->start;
1317 node = rb_last(&dso->syms);
1318 sym = rb_entry(node, struct symbol, rb_node);
1319 kernel_map->end = sym->end;
1321 dso->origin = DSO__ORIG_KERNEL;
1322 kernel_maps__insert(kernel_map);
1324 * Now that we have all sorted out, just set the ->end of all
1327 kernel_maps__fixup_end();
1331 kernel_maps__fprintf(stderr);
1344 const char *vmlinux_name = "vmlinux";
1347 static void dsos__add(struct dso *dso)
1349 list_add_tail(&dso->node, &dsos);
1352 static struct dso *dsos__find(const char *name)
1356 list_for_each_entry(pos, &dsos, node)
1357 if (strcmp(pos->name, name) == 0)
1362 struct dso *dsos__findnew(const char *name)
1364 struct dso *dso = dsos__find(name);
1367 dso = dso__new(name);
1375 void dsos__fprintf(FILE *fp)
1379 list_for_each_entry(pos, &dsos, node)
1380 dso__fprintf(pos, fp);
1383 int load_kernel(symbol_filter_t filter)
1385 if (dsos__load_kernel(vmlinux_name, filter, modules) <= 0)
1388 vdso = dso__new("[vdso]");
1397 void symbol__init(unsigned int priv_size)
1399 elf_version(EV_CURRENT);
1400 symbol__priv_size = priv_size;