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 static void dso__set_long_name(struct dso *self, char *name)
114 self->long_name = name;
115 self->long_name_len = strlen(name);
118 static void dso__set_basename(struct dso *self)
120 self->short_name = basename(self->long_name);
123 struct dso *dso__new(const char *name)
125 struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
128 strcpy(self->name, name);
129 dso__set_long_name(self, self->name);
130 self->short_name = self->name;
131 self->syms = RB_ROOT;
132 self->find_symbol = dso__find_symbol;
133 self->slen_calculated = 0;
134 self->origin = DSO__ORIG_NOT_FOUND;
136 self->has_build_id = 0;
142 static void dso__delete_symbols(struct dso *self)
145 struct rb_node *next = rb_first(&self->syms);
148 pos = rb_entry(next, struct symbol, rb_node);
149 next = rb_next(&pos->rb_node);
150 rb_erase(&pos->rb_node, &self->syms);
155 void dso__delete(struct dso *self)
157 dso__delete_symbols(self);
158 if (self->long_name != self->name)
159 free(self->long_name);
163 void dso__set_build_id(struct dso *self, void *build_id)
165 memcpy(self->build_id, build_id, sizeof(self->build_id));
166 self->has_build_id = 1;
169 static void dso__insert_symbol(struct dso *self, struct symbol *sym)
171 struct rb_node **p = &self->syms.rb_node;
172 struct rb_node *parent = NULL;
173 const u64 ip = sym->start;
178 s = rb_entry(parent, struct symbol, rb_node);
184 rb_link_node(&sym->rb_node, parent, p);
185 rb_insert_color(&sym->rb_node, &self->syms);
188 struct symbol *dso__find_symbol(struct dso *self, u64 ip)
195 n = self->syms.rb_node;
198 struct symbol *s = rb_entry(n, struct symbol, rb_node);
202 else if (ip > s->end)
211 int build_id__sprintf(u8 *self, int len, char *bf)
217 for (i = 0; i < len; ++i) {
218 sprintf(bid, "%02x", *raw);
226 size_t dso__fprintf_buildid(struct dso *self, FILE *fp)
228 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
230 build_id__sprintf(self->build_id, sizeof(self->build_id), sbuild_id);
231 return fprintf(fp, "%s", sbuild_id);
234 size_t dso__fprintf(struct dso *self, FILE *fp)
237 size_t ret = fprintf(fp, "dso: %s (", self->short_name);
239 ret += dso__fprintf_buildid(self, fp);
240 ret += fprintf(fp, ")\n");
242 for (nd = rb_first(&self->syms); nd; nd = rb_next(nd)) {
243 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
244 ret += symbol__fprintf(pos, fp);
251 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
252 * so that we can in the next step set the symbol ->end address and then
253 * call kernel_maps__split_kallsyms.
255 static int kernel_maps__load_all_kallsyms(void)
259 FILE *file = fopen("/proc/kallsyms", "r");
264 while (!feof(file)) {
271 line_len = getline(&line, &n, file);
278 line[--line_len] = '\0'; /* \n */
280 len = hex2u64(line, &start);
283 if (len + 2 >= line_len)
286 symbol_type = toupper(line[len]);
288 * We're interested only in code ('T'ext)
290 if (symbol_type != 'T' && symbol_type != 'W')
293 symbol_name = line + len + 2;
295 * Will fix up the end later, when we have all symbols sorted.
297 sym = symbol__new(start, 0, symbol_name);
300 goto out_delete_line;
303 * We will pass the symbols to the filter later, in
304 * kernel_maps__split_kallsyms, when we have split the
307 dso__insert_symbol(kernel_map->dso, sym);
322 * Split the symbols into maps, making sure there are no overlaps, i.e. the
323 * kernel range is broken in several maps, named [kernel].N, as we don't have
324 * the original ELF section names vmlinux have.
326 static int kernel_maps__split_kallsyms(symbol_filter_t filter, int use_modules)
328 struct map *map = kernel_map;
331 struct rb_node *next = rb_first(&kernel_map->dso->syms);
332 int kernel_range = 0;
337 pos = rb_entry(next, struct symbol, rb_node);
338 next = rb_next(&pos->rb_node);
340 module = strchr(pos->name, '\t');
347 if (strcmp(map->dso->name, module)) {
348 map = kernel_maps__find_by_dso_name(module);
350 pr_err("/proc/{kallsyms,modules} "
356 * So that we look just like we get from .ko files,
357 * i.e. not prelinked, relative to map->start.
359 pos->start = map->map_ip(map, pos->start);
360 pos->end = map->map_ip(map, pos->end);
361 } else if (map != kernel_map) {
362 char dso_name[PATH_MAX];
365 snprintf(dso_name, sizeof(dso_name), "[kernel].%d",
368 dso = dso__new(dso_name);
372 map = map__new2(pos->start, dso);
378 map->map_ip = map->unmap_ip = identity__map_ip;
379 kernel_maps__insert(map);
383 if (filter && filter(map, pos)) {
385 rb_erase(&pos->rb_node, &kernel_map->dso->syms);
388 if (map != kernel_map) {
389 rb_erase(&pos->rb_node, &kernel_map->dso->syms);
390 dso__insert_symbol(map->dso, pos);
400 static int kernel_maps__load_kallsyms(symbol_filter_t filter, int use_modules)
402 if (kernel_maps__load_all_kallsyms())
405 dso__fixup_sym_end(kernel_map->dso);
407 return kernel_maps__split_kallsyms(filter, use_modules);
410 static size_t kernel_maps__fprintf(FILE *fp)
412 size_t printed = fprintf(fp, "Kernel maps:\n");
415 for (nd = rb_first(&kernel_maps); nd; nd = rb_next(nd)) {
416 struct map *pos = rb_entry(nd, struct map, rb_node);
418 printed += fprintf(fp, "Map:");
419 printed += map__fprintf(pos, fp);
421 printed += dso__fprintf(pos->dso, fp);
422 printed += fprintf(fp, "--\n");
426 return printed + fprintf(fp, "END kernel maps\n");
429 static int dso__load_perf_map(struct dso *self, struct map *map,
430 symbol_filter_t filter)
437 file = fopen(self->long_name, "r");
441 while (!feof(file)) {
446 line_len = getline(&line, &n, file);
453 line[--line_len] = '\0'; /* \n */
455 len = hex2u64(line, &start);
458 if (len + 2 >= line_len)
461 len += hex2u64(line + len, &size);
464 if (len + 2 >= line_len)
467 sym = symbol__new(start, size, line + len);
470 goto out_delete_line;
472 if (filter && filter(map, sym))
475 dso__insert_symbol(self, sym);
492 * elf_symtab__for_each_symbol - iterate thru all the symbols
494 * @self: struct elf_symtab instance to iterate
496 * @sym: GElf_Sym iterator
498 #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
499 for (idx = 0, gelf_getsym(syms, idx, &sym);\
501 idx++, gelf_getsym(syms, idx, &sym))
503 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
505 return GELF_ST_TYPE(sym->st_info);
508 static inline int elf_sym__is_function(const GElf_Sym *sym)
510 return elf_sym__type(sym) == STT_FUNC &&
512 sym->st_shndx != SHN_UNDEF;
515 static inline int elf_sym__is_label(const GElf_Sym *sym)
517 return elf_sym__type(sym) == STT_NOTYPE &&
519 sym->st_shndx != SHN_UNDEF &&
520 sym->st_shndx != SHN_ABS;
523 static inline const char *elf_sec__name(const GElf_Shdr *shdr,
524 const Elf_Data *secstrs)
526 return secstrs->d_buf + shdr->sh_name;
529 static inline int elf_sec__is_text(const GElf_Shdr *shdr,
530 const Elf_Data *secstrs)
532 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
535 static inline const char *elf_sym__name(const GElf_Sym *sym,
536 const Elf_Data *symstrs)
538 return symstrs->d_buf + sym->st_name;
541 static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
542 GElf_Shdr *shp, const char *name,
548 while ((sec = elf_nextscn(elf, sec)) != NULL) {
551 gelf_getshdr(sec, shp);
552 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
553 if (!strcmp(name, str)) {
564 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
565 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
567 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
569 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
570 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
572 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
575 * We need to check if we have a .dynsym, so that we can handle the
576 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
577 * .dynsym or .symtab).
578 * And always look at the original dso, not at debuginfo packages, that
579 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
581 static int dso__synthesize_plt_symbols(struct dso *self, struct map *map,
582 symbol_filter_t filter)
584 uint32_t nr_rel_entries, idx;
589 GElf_Shdr shdr_rel_plt, shdr_dynsym;
590 Elf_Data *reldata, *syms, *symstrs;
591 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
594 char sympltname[1024];
596 int nr = 0, symidx, fd, err = 0;
598 fd = open(self->long_name, O_RDONLY);
602 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
606 if (gelf_getehdr(elf, &ehdr) == NULL)
609 scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
610 ".dynsym", &dynsym_idx);
611 if (scn_dynsym == NULL)
614 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
616 if (scn_plt_rel == NULL) {
617 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
619 if (scn_plt_rel == NULL)
625 if (shdr_rel_plt.sh_link != dynsym_idx)
628 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
632 * Fetch the relocation section to find the idxes to the GOT
633 * and the symbols in the .dynsym they refer to.
635 reldata = elf_getdata(scn_plt_rel, NULL);
639 syms = elf_getdata(scn_dynsym, NULL);
643 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
644 if (scn_symstrs == NULL)
647 symstrs = elf_getdata(scn_symstrs, NULL);
651 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
652 plt_offset = shdr_plt.sh_offset;
654 if (shdr_rel_plt.sh_type == SHT_RELA) {
655 GElf_Rela pos_mem, *pos;
657 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
659 symidx = GELF_R_SYM(pos->r_info);
660 plt_offset += shdr_plt.sh_entsize;
661 gelf_getsym(syms, symidx, &sym);
662 snprintf(sympltname, sizeof(sympltname),
663 "%s@plt", elf_sym__name(&sym, symstrs));
665 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
670 if (filter && filter(map, f))
673 dso__insert_symbol(self, f);
677 } else if (shdr_rel_plt.sh_type == SHT_REL) {
678 GElf_Rel pos_mem, *pos;
679 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
681 symidx = GELF_R_SYM(pos->r_info);
682 plt_offset += shdr_plt.sh_entsize;
683 gelf_getsym(syms, symidx, &sym);
684 snprintf(sympltname, sizeof(sympltname),
685 "%s@plt", elf_sym__name(&sym, symstrs));
687 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
692 if (filter && filter(map, f))
695 dso__insert_symbol(self, f);
710 pr_warning("%s: problems reading %s PLT info.\n",
711 __func__, self->long_name);
715 static int dso__load_sym(struct dso *self, struct map *map, const char *name,
716 int fd, symbol_filter_t filter, int kernel,
719 struct map *curr_map = map;
720 struct dso *curr_dso = self;
721 size_t dso_name_len = strlen(self->short_name);
722 Elf_Data *symstrs, *secstrs;
730 Elf_Scn *sec, *sec_strndx;
734 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
736 pr_err("%s: cannot read %s ELF file.\n", __func__, name);
740 if (gelf_getehdr(elf, &ehdr) == NULL) {
741 pr_err("%s: cannot get elf header.\n", __func__);
745 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
747 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
752 syms = elf_getdata(sec, NULL);
756 sec = elf_getscn(elf, shdr.sh_link);
760 symstrs = elf_getdata(sec, NULL);
764 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
765 if (sec_strndx == NULL)
768 secstrs = elf_getdata(sec_strndx, NULL);
772 nr_syms = shdr.sh_size / shdr.sh_entsize;
774 memset(&sym, 0, sizeof(sym));
776 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
777 elf_section_by_name(elf, &ehdr, &shdr,
780 } else self->adjust_symbols = 0;
782 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
784 const char *elf_name;
785 char *demangled = NULL;
786 int is_label = elf_sym__is_label(&sym);
787 const char *section_name;
789 if (!is_label && !elf_sym__is_function(&sym))
792 sec = elf_getscn(elf, sym.st_shndx);
796 gelf_getshdr(sec, &shdr);
798 if (is_label && !elf_sec__is_text(&shdr, secstrs))
801 elf_name = elf_sym__name(&sym, symstrs);
802 section_name = elf_sec__name(&shdr, secstrs);
804 if (kernel || kmodule) {
805 char dso_name[PATH_MAX];
807 if (strcmp(section_name,
808 curr_dso->short_name + dso_name_len) == 0)
811 if (strcmp(section_name, ".text") == 0) {
817 snprintf(dso_name, sizeof(dso_name),
818 "%s%s", self->short_name, section_name);
820 curr_map = kernel_maps__find_by_dso_name(dso_name);
821 if (curr_map == NULL) {
822 u64 start = sym.st_value;
825 start += map->start + shdr.sh_offset;
827 curr_dso = dso__new(dso_name);
828 if (curr_dso == NULL)
830 curr_map = map__new2(start, curr_dso);
831 if (curr_map == NULL) {
832 dso__delete(curr_dso);
835 curr_map->map_ip = identity__map_ip;
836 curr_map->unmap_ip = identity__map_ip;
837 curr_dso->origin = DSO__ORIG_KERNEL;
838 kernel_maps__insert(curr_map);
841 curr_dso = curr_map->dso;
846 if (curr_dso->adjust_symbols) {
847 pr_debug2("adjusting symbol: st_value: %Lx sh_addr: "
848 "%Lx sh_offset: %Lx\n", (u64)sym.st_value,
849 (u64)shdr.sh_addr, (u64)shdr.sh_offset);
850 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
853 * We need to figure out if the object was created from C++ sources
854 * DWARF DW_compile_unit has this, but we don't always have access
857 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
858 if (demangled != NULL)
859 elf_name = demangled;
861 f = symbol__new(sym.st_value, sym.st_size, elf_name);
866 if (filter && filter(curr_map, f))
869 dso__insert_symbol(curr_dso, f);
875 * For misannotated, zeroed, ASM function sizes.
878 dso__fixup_sym_end(self);
886 bool dsos__read_build_ids(void)
888 bool have_build_id = false;
891 list_for_each_entry(pos, &dsos, node)
892 if (filename__read_build_id(pos->long_name, pos->build_id,
893 sizeof(pos->build_id)) > 0) {
894 have_build_id = true;
895 pos->has_build_id = true;
898 return have_build_id;
901 int filename__read_build_id(const char *filename, void *bf, size_t size)
906 Elf_Data *build_id_data;
910 if (size < BUILD_ID_SIZE)
913 fd = open(filename, O_RDONLY);
917 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
919 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
923 if (gelf_getehdr(elf, &ehdr) == NULL) {
924 pr_err("%s: cannot get elf header.\n", __func__);
928 sec = elf_section_by_name(elf, &ehdr, &shdr,
929 ".note.gnu.build-id", NULL);
933 build_id_data = elf_getdata(sec, NULL);
934 if (build_id_data == NULL)
936 memcpy(bf, build_id_data->d_buf + 16, BUILD_ID_SIZE);
946 char dso__symtab_origin(const struct dso *self)
948 static const char origin[] = {
949 [DSO__ORIG_KERNEL] = 'k',
950 [DSO__ORIG_JAVA_JIT] = 'j',
951 [DSO__ORIG_FEDORA] = 'f',
952 [DSO__ORIG_UBUNTU] = 'u',
953 [DSO__ORIG_BUILDID] = 'b',
954 [DSO__ORIG_DSO] = 'd',
955 [DSO__ORIG_KMODULE] = 'K',
958 if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
960 return origin[self->origin];
963 int dso__load(struct dso *self, struct map *map, symbol_filter_t filter)
966 char *name = malloc(size);
967 u8 build_id[BUILD_ID_SIZE];
976 self->adjust_symbols = 0;
978 if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
979 ret = dso__load_perf_map(self, map, filter);
980 self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
985 self->origin = DSO__ORIG_FEDORA - 1;
990 switch (self->origin) {
991 case DSO__ORIG_FEDORA:
992 snprintf(name, size, "/usr/lib/debug%s.debug",
995 case DSO__ORIG_UBUNTU:
996 snprintf(name, size, "/usr/lib/debug%s",
999 case DSO__ORIG_BUILDID:
1000 if (filename__read_build_id(self->long_name, build_id,
1001 sizeof(build_id))) {
1002 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
1004 build_id__sprintf(build_id, sizeof(build_id),
1006 snprintf(name, size,
1007 "/usr/lib/debug/.build-id/%.2s/%s.debug",
1008 build_id_hex, build_id_hex + 2);
1009 if (self->has_build_id)
1010 goto compare_build_id;
1016 snprintf(name, size, "%s", self->long_name);
1023 if (self->has_build_id) {
1024 if (filename__read_build_id(name, build_id,
1025 sizeof(build_id)) < 0)
1028 if (memcmp(build_id, self->build_id,
1029 sizeof(self->build_id)) != 0)
1033 fd = open(name, O_RDONLY);
1036 ret = dso__load_sym(self, map, name, fd, filter, 0, 0);
1040 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
1046 int nr_plt = dso__synthesize_plt_symbols(self, map, filter);
1052 if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
1057 struct map *kernel_map;
1059 static void kernel_maps__insert(struct map *map)
1061 maps__insert(&kernel_maps, map);
1064 struct symbol *kernel_maps__find_symbol(u64 ip, struct map **mapp)
1066 struct map *map = maps__find(&kernel_maps, ip);
1072 ip = map->map_ip(map, ip);
1073 return map->dso->find_symbol(map->dso, ip);
1079 struct map *kernel_maps__find_by_dso_name(const char *name)
1083 for (nd = rb_first(&kernel_maps); nd; nd = rb_next(nd)) {
1084 struct map *map = rb_entry(nd, struct map, rb_node);
1086 if (map->dso && strcmp(map->dso->name, name) == 0)
1093 static int dso__load_module_sym(struct dso *self, struct map *map,
1094 symbol_filter_t filter)
1096 int err = 0, fd = open(self->long_name, O_RDONLY);
1101 pr_err("%s: cannot open %s\n", __func__, self->long_name);
1105 err = dso__load_sym(self, map, self->long_name, fd, filter, 0, 1);
1111 static int dsos__load_modules_sym_dir(char *dirname, symbol_filter_t filter)
1113 struct dirent *dent;
1114 int nr_symbols = 0, err;
1115 DIR *dir = opendir(dirname);
1118 pr_err("%s: cannot open %s dir\n", __func__, dirname);
1122 while ((dent = readdir(dir)) != NULL) {
1123 char path[PATH_MAX];
1125 if (dent->d_type == DT_DIR) {
1126 if (!strcmp(dent->d_name, ".") ||
1127 !strcmp(dent->d_name, ".."))
1130 snprintf(path, sizeof(path), "%s/%s",
1131 dirname, dent->d_name);
1132 err = dsos__load_modules_sym_dir(path, filter);
1136 char *dot = strrchr(dent->d_name, '.'),
1139 struct rb_node *last;
1142 if (dot == NULL || strcmp(dot, ".ko"))
1144 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1145 (int)(dot - dent->d_name), dent->d_name);
1147 strxfrchar(dso_name, '-', '_');
1148 map = kernel_maps__find_by_dso_name(dso_name);
1152 snprintf(path, sizeof(path), "%s/%s",
1153 dirname, dent->d_name);
1155 long_name = strdup(path);
1156 if (long_name == NULL)
1158 dso__set_long_name(map->dso, long_name);
1159 dso__set_basename(map->dso);
1161 err = dso__load_module_sym(map->dso, map, filter);
1164 last = rb_last(&map->dso->syms);
1168 * We do this here as well, even having the
1169 * symbol size found in the symtab because
1170 * misannotated ASM symbols may have the size
1173 dso__fixup_sym_end(map->dso);
1175 sym = rb_entry(last, struct symbol, rb_node);
1176 map->end = map->start + sym->end;
1188 static int dsos__load_modules_sym(symbol_filter_t filter)
1191 char modules_path[PATH_MAX];
1193 if (uname(&uts) < 0)
1196 snprintf(modules_path, sizeof(modules_path), "/lib/modules/%s/kernel",
1199 return dsos__load_modules_sym_dir(modules_path, filter);
1203 * Constructor variant for modules (where we know from /proc/modules where
1204 * they are loaded) and for vmlinux, where only after we load all the
1205 * symbols we'll know where it starts and ends.
1207 static struct map *map__new2(u64 start, struct dso *dso)
1209 struct map *self = malloc(sizeof(*self));
1213 * ->end will be filled after we load all the symbols
1215 map__init(self, start, 0, 0, dso);
1221 static int dsos__load_modules(void)
1225 FILE *file = fopen("/proc/modules", "r");
1231 while (!feof(file)) {
1232 char name[PATH_MAX];
1238 line_len = getline(&line, &n, file);
1245 line[--line_len] = '\0'; /* \n */
1247 sep = strrchr(line, 'x');
1251 hex2u64(sep + 1, &start);
1253 sep = strchr(line, ' ');
1259 snprintf(name, sizeof(name), "[%s]", line);
1260 dso = dso__new(name);
1263 goto out_delete_line;
1265 map = map__new2(start, dso);
1268 goto out_delete_line;
1271 dso->origin = DSO__ORIG_KMODULE;
1272 kernel_maps__insert(map);
1287 static int dso__load_vmlinux(struct dso *self, struct map *map,
1288 const char *vmlinux, symbol_filter_t filter)
1290 int err, fd = open(vmlinux, O_RDONLY);
1297 err = dso__load_sym(self, map, self->long_name, fd, filter, 1, 0);
1304 int dsos__load_kernel(const char *vmlinux, symbol_filter_t filter,
1308 struct dso *dso = dso__new(vmlinux);
1313 dso->short_name = "[kernel]";
1314 kernel_map = map__new2(0, dso);
1315 if (kernel_map == NULL)
1316 goto out_delete_dso;
1318 kernel_map->map_ip = kernel_map->unmap_ip = identity__map_ip;
1320 if (use_modules && dsos__load_modules() < 0) {
1321 pr_warning("Failed to load list of modules in use! "
1327 err = dso__load_vmlinux(dso, kernel_map, vmlinux, filter);
1328 if (err > 0 && use_modules) {
1329 int syms = dsos__load_modules_sym(filter);
1332 pr_warning("Failed to read module symbols!"
1333 " Continuing...\n");
1340 err = kernel_maps__load_kallsyms(filter, use_modules);
1343 struct rb_node *node = rb_first(&dso->syms);
1344 struct symbol *sym = rb_entry(node, struct symbol, rb_node);
1346 kernel_map->start = sym->start;
1347 node = rb_last(&dso->syms);
1348 sym = rb_entry(node, struct symbol, rb_node);
1349 kernel_map->end = sym->end;
1351 dso->origin = DSO__ORIG_KERNEL;
1352 kernel_maps__insert(kernel_map);
1354 * Now that we have all sorted out, just set the ->end of all
1357 kernel_maps__fixup_end();
1361 kernel_maps__fprintf(stderr);
1374 const char *vmlinux_name = "vmlinux";
1377 static void dsos__add(struct dso *dso)
1379 list_add_tail(&dso->node, &dsos);
1382 static struct dso *dsos__find(const char *name)
1386 list_for_each_entry(pos, &dsos, node)
1387 if (strcmp(pos->name, name) == 0)
1392 struct dso *dsos__findnew(const char *name)
1394 struct dso *dso = dsos__find(name);
1397 dso = dso__new(name);
1400 dso__set_basename(dso);
1407 void dsos__fprintf(FILE *fp)
1411 list_for_each_entry(pos, &dsos, node)
1412 dso__fprintf(pos, fp);
1415 size_t dsos__fprintf_buildid(FILE *fp)
1420 list_for_each_entry(pos, &dsos, node) {
1421 ret += dso__fprintf_buildid(pos, fp);
1422 ret += fprintf(fp, " %s\n", pos->long_name);
1427 int load_kernel(symbol_filter_t filter)
1429 if (dsos__load_kernel(vmlinux_name, filter, modules) <= 0)
1432 vdso = dso__new("[vdso]");
1441 void symbol__init(unsigned int priv_size)
1443 elf_version(EV_CURRENT);
1444 symbol__priv_size = priv_size;