14 #include <sys/utsname.h>
16 #ifndef NT_GNU_BUILD_ID
17 #define NT_GNU_BUILD_ID 3
31 static void dsos__add(struct list_head *head, struct dso *dso);
32 static struct map *kernel_maps__find_by_dso_name(const char *name);
33 static struct map *map__new2(u64 start, struct dso *dso, enum map_type type);
34 static void kernel_maps__insert(struct map *map);
35 struct symbol *dso__find_symbol(struct dso *self, enum map_type type, u64 addr);
36 static int dso__load_kernel_sym(struct dso *self, struct map *map,
37 symbol_filter_t filter);
38 unsigned int symbol__priv_size;
39 static int vmlinux_path__nr_entries;
40 static char **vmlinux_path;
42 static struct symbol_conf symbol_conf__defaults = {
44 .try_vmlinux_path = true,
47 static struct rb_root kernel_maps[MAP__NR_TYPES];
49 bool dso__loaded(const struct dso *self, enum map_type type)
51 return self->loaded & (1 << type);
54 static void dso__set_loaded(struct dso *self, enum map_type type)
56 self->loaded |= (1 << type);
59 static void symbols__fixup_end(struct rb_root *self)
61 struct rb_node *nd, *prevnd = rb_first(self);
62 struct symbol *curr, *prev;
67 curr = rb_entry(prevnd, struct symbol, rb_node);
69 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
71 curr = rb_entry(nd, struct symbol, rb_node);
73 if (prev->end == prev->start)
74 prev->end = curr->start - 1;
78 if (curr->end == curr->start)
79 curr->end = roundup(curr->start, 4096);
82 static void __kernel_maps__fixup_end(struct rb_root *root)
84 struct map *prev, *curr;
85 struct rb_node *nd, *prevnd = rb_first(root);
90 curr = rb_entry(prevnd, struct map, rb_node);
92 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
94 curr = rb_entry(nd, struct map, rb_node);
95 prev->end = curr->start - 1;
99 * We still haven't the actual symbols, so guess the
100 * last map final address.
105 static void kernel_maps__fixup_end(void)
108 for (i = 0; i < MAP__NR_TYPES; ++i)
109 __kernel_maps__fixup_end(&kernel_maps[i]);
112 static struct symbol *symbol__new(u64 start, u64 len, const char *name)
114 size_t namelen = strlen(name) + 1;
115 struct symbol *self = zalloc(symbol__priv_size +
116 sizeof(*self) + namelen);
120 if (symbol__priv_size)
121 self = ((void *)self) + symbol__priv_size;
124 self->end = len ? start + len - 1 : start;
126 pr_debug3("%s: %s %#Lx-%#Lx\n", __func__, name, start, self->end);
128 memcpy(self->name, name, namelen);
133 static void symbol__delete(struct symbol *self)
135 free(((void *)self) - symbol__priv_size);
138 static size_t symbol__fprintf(struct symbol *self, FILE *fp)
140 return fprintf(fp, " %llx-%llx %s\n",
141 self->start, self->end, self->name);
144 static void dso__set_long_name(struct dso *self, char *name)
148 self->long_name = name;
149 self->long_name_len = strlen(name);
152 static void dso__set_basename(struct dso *self)
154 self->short_name = basename(self->long_name);
157 struct dso *dso__new(const char *name)
159 struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
163 strcpy(self->name, name);
164 dso__set_long_name(self, self->name);
165 self->short_name = self->name;
166 for (i = 0; i < MAP__NR_TYPES; ++i)
167 self->symbols[i] = RB_ROOT;
168 self->find_symbol = dso__find_symbol;
169 self->slen_calculated = 0;
170 self->origin = DSO__ORIG_NOT_FOUND;
172 self->has_build_id = 0;
178 static void symbols__delete(struct rb_root *self)
181 struct rb_node *next = rb_first(self);
184 pos = rb_entry(next, struct symbol, rb_node);
185 next = rb_next(&pos->rb_node);
186 rb_erase(&pos->rb_node, self);
191 void dso__delete(struct dso *self)
194 for (i = 0; i < MAP__NR_TYPES; ++i)
195 symbols__delete(&self->symbols[i]);
196 if (self->long_name != self->name)
197 free(self->long_name);
201 void dso__set_build_id(struct dso *self, void *build_id)
203 memcpy(self->build_id, build_id, sizeof(self->build_id));
204 self->has_build_id = 1;
207 static void symbols__insert(struct rb_root *self, struct symbol *sym)
209 struct rb_node **p = &self->rb_node;
210 struct rb_node *parent = NULL;
211 const u64 ip = sym->start;
216 s = rb_entry(parent, struct symbol, rb_node);
222 rb_link_node(&sym->rb_node, parent, p);
223 rb_insert_color(&sym->rb_node, self);
226 static struct symbol *symbols__find(struct rb_root *self, u64 ip)
236 struct symbol *s = rb_entry(n, struct symbol, rb_node);
240 else if (ip > s->end)
249 struct symbol *dso__find_symbol(struct dso *self, enum map_type type, u64 addr)
251 return symbols__find(&self->symbols[type], addr);
254 int build_id__sprintf(u8 *self, int len, char *bf)
260 for (i = 0; i < len; ++i) {
261 sprintf(bid, "%02x", *raw);
269 size_t dso__fprintf_buildid(struct dso *self, FILE *fp)
271 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
273 build_id__sprintf(self->build_id, sizeof(self->build_id), sbuild_id);
274 return fprintf(fp, "%s", sbuild_id);
277 static const char * map_type__name[MAP__NR_TYPES] = {
278 [MAP__FUNCTION] = "Functions",
281 size_t dso__fprintf(struct dso *self, FILE *fp)
285 size_t ret = fprintf(fp, "dso: %s (", self->short_name);
287 ret += dso__fprintf_buildid(self, fp);
288 ret += fprintf(fp, ")\n");
289 for (i = 0; i < MAP__NR_TYPES; ++i) {
290 ret += fprintf(fp, "%s:\n", map_type__name[i]);
292 for (nd = rb_first(&self->symbols[i]); nd; nd = rb_next(nd)) {
293 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
294 ret += symbol__fprintf(pos, fp);
302 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
303 * so that we can in the next step set the symbol ->end address and then
304 * call kernel_maps__split_kallsyms.
306 static int dso__load_all_kallsyms(struct dso *self, struct map *map)
310 struct rb_root *root = &self->symbols[map->type];
311 FILE *file = fopen("/proc/kallsyms", "r");
316 while (!feof(file)) {
323 line_len = getline(&line, &n, file);
330 line[--line_len] = '\0'; /* \n */
332 len = hex2u64(line, &start);
335 if (len + 2 >= line_len)
338 symbol_type = toupper(line[len]);
340 * We're interested only in code ('T'ext)
342 if (symbol_type != 'T' && symbol_type != 'W')
345 symbol_name = line + len + 2;
347 * Will fix up the end later, when we have all symbols sorted.
349 sym = symbol__new(start, 0, symbol_name);
352 goto out_delete_line;
354 * We will pass the symbols to the filter later, in
355 * map__split_kallsyms, when we have split the maps per module
357 symbols__insert(root, sym);
372 * Split the symbols into maps, making sure there are no overlaps, i.e. the
373 * kernel range is broken in several maps, named [kernel].N, as we don't have
374 * the original ELF section names vmlinux have.
376 static int dso__split_kallsyms(struct dso *self, struct map *map,
377 symbol_filter_t filter)
379 struct map *curr_map = map;
382 struct rb_root *root = &self->symbols[map->type];
383 struct rb_node *next = rb_first(root);
384 int kernel_range = 0;
389 pos = rb_entry(next, struct symbol, rb_node);
390 next = rb_next(&pos->rb_node);
392 module = strchr(pos->name, '\t');
396 if (strcmp(self->name, module)) {
397 curr_map = kernel_maps__find_by_dso_name(module);
398 if (curr_map == NULL) {
399 pr_err("/proc/{kallsyms,modules} "
405 * So that we look just like we get from .ko files,
406 * i.e. not prelinked, relative to map->start.
408 pos->start = curr_map->map_ip(curr_map, pos->start);
409 pos->end = curr_map->map_ip(curr_map, pos->end);
410 } else if (curr_map != map) {
411 char dso_name[PATH_MAX];
414 snprintf(dso_name, sizeof(dso_name), "[kernel].%d",
417 dso = dso__new(dso_name);
421 curr_map = map__new2(pos->start, dso, map->type);
427 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
428 kernel_maps__insert(curr_map);
432 if (filter && filter(curr_map, pos)) {
433 rb_erase(&pos->rb_node, root);
436 if (curr_map != map) {
437 rb_erase(&pos->rb_node, root);
438 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
448 static int dso__load_kallsyms(struct dso *self, struct map *map,
449 symbol_filter_t filter)
451 if (dso__load_all_kallsyms(self, map) < 0)
454 symbols__fixup_end(&self->symbols[map->type]);
455 self->origin = DSO__ORIG_KERNEL;
457 return dso__split_kallsyms(self, map, filter);
460 static size_t __kernel_maps__fprintf(enum map_type type, FILE *fp)
462 size_t printed = fprintf(fp, "%s:\n", map_type__name[type]);
465 for (nd = rb_first(&kernel_maps[type]); nd; nd = rb_next(nd)) {
466 struct map *pos = rb_entry(nd, struct map, rb_node);
468 printed += fprintf(fp, "Map:");
469 printed += map__fprintf(pos, fp);
471 printed += dso__fprintf(pos->dso, fp);
472 printed += fprintf(fp, "--\n");
479 size_t kernel_maps__fprintf(FILE *fp)
481 size_t printed = fprintf(fp, "Kernel maps:\n");
483 for (i = 0; i < MAP__NR_TYPES; ++i)
484 printed += __kernel_maps__fprintf(i, fp);
486 return printed + fprintf(fp, "END kernel maps\n");
489 static int dso__load_perf_map(struct dso *self, struct map *map,
490 symbol_filter_t filter)
497 file = fopen(self->long_name, "r");
501 while (!feof(file)) {
506 line_len = getline(&line, &n, file);
513 line[--line_len] = '\0'; /* \n */
515 len = hex2u64(line, &start);
518 if (len + 2 >= line_len)
521 len += hex2u64(line + len, &size);
524 if (len + 2 >= line_len)
527 sym = symbol__new(start, size, line + len);
530 goto out_delete_line;
532 if (filter && filter(map, sym))
535 symbols__insert(&self->symbols[map->type], sym);
552 * elf_symtab__for_each_symbol - iterate thru all the symbols
554 * @self: struct elf_symtab instance to iterate
556 * @sym: GElf_Sym iterator
558 #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
559 for (idx = 0, gelf_getsym(syms, idx, &sym);\
561 idx++, gelf_getsym(syms, idx, &sym))
563 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
565 return GELF_ST_TYPE(sym->st_info);
568 static inline int elf_sym__is_function(const GElf_Sym *sym)
570 return elf_sym__type(sym) == STT_FUNC &&
572 sym->st_shndx != SHN_UNDEF;
575 static inline int elf_sym__is_label(const GElf_Sym *sym)
577 return elf_sym__type(sym) == STT_NOTYPE &&
579 sym->st_shndx != SHN_UNDEF &&
580 sym->st_shndx != SHN_ABS;
583 static inline const char *elf_sec__name(const GElf_Shdr *shdr,
584 const Elf_Data *secstrs)
586 return secstrs->d_buf + shdr->sh_name;
589 static inline int elf_sec__is_text(const GElf_Shdr *shdr,
590 const Elf_Data *secstrs)
592 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
595 static inline const char *elf_sym__name(const GElf_Sym *sym,
596 const Elf_Data *symstrs)
598 return symstrs->d_buf + sym->st_name;
601 static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
602 GElf_Shdr *shp, const char *name,
608 while ((sec = elf_nextscn(elf, sec)) != NULL) {
611 gelf_getshdr(sec, shp);
612 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
613 if (!strcmp(name, str)) {
624 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
625 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
627 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
629 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
630 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
632 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
635 * We need to check if we have a .dynsym, so that we can handle the
636 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
637 * .dynsym or .symtab).
638 * And always look at the original dso, not at debuginfo packages, that
639 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
641 static int dso__synthesize_plt_symbols(struct dso *self, struct map *map,
642 symbol_filter_t filter)
644 uint32_t nr_rel_entries, idx;
649 GElf_Shdr shdr_rel_plt, shdr_dynsym;
650 Elf_Data *reldata, *syms, *symstrs;
651 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
654 char sympltname[1024];
656 int nr = 0, symidx, fd, err = 0;
658 fd = open(self->long_name, O_RDONLY);
662 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
666 if (gelf_getehdr(elf, &ehdr) == NULL)
669 scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
670 ".dynsym", &dynsym_idx);
671 if (scn_dynsym == NULL)
674 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
676 if (scn_plt_rel == NULL) {
677 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
679 if (scn_plt_rel == NULL)
685 if (shdr_rel_plt.sh_link != dynsym_idx)
688 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
692 * Fetch the relocation section to find the idxes to the GOT
693 * and the symbols in the .dynsym they refer to.
695 reldata = elf_getdata(scn_plt_rel, NULL);
699 syms = elf_getdata(scn_dynsym, NULL);
703 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
704 if (scn_symstrs == NULL)
707 symstrs = elf_getdata(scn_symstrs, NULL);
711 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
712 plt_offset = shdr_plt.sh_offset;
714 if (shdr_rel_plt.sh_type == SHT_RELA) {
715 GElf_Rela pos_mem, *pos;
717 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
719 symidx = GELF_R_SYM(pos->r_info);
720 plt_offset += shdr_plt.sh_entsize;
721 gelf_getsym(syms, symidx, &sym);
722 snprintf(sympltname, sizeof(sympltname),
723 "%s@plt", elf_sym__name(&sym, symstrs));
725 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
730 if (filter && filter(map, f))
733 symbols__insert(&self->symbols[map->type], f);
737 } else if (shdr_rel_plt.sh_type == SHT_REL) {
738 GElf_Rel pos_mem, *pos;
739 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
741 symidx = GELF_R_SYM(pos->r_info);
742 plt_offset += shdr_plt.sh_entsize;
743 gelf_getsym(syms, symidx, &sym);
744 snprintf(sympltname, sizeof(sympltname),
745 "%s@plt", elf_sym__name(&sym, symstrs));
747 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
752 if (filter && filter(map, f))
755 symbols__insert(&self->symbols[map->type], f);
770 pr_warning("%s: problems reading %s PLT info.\n",
771 __func__, self->long_name);
775 static int dso__load_sym(struct dso *self, struct map *map, const char *name,
776 int fd, symbol_filter_t filter, int kernel,
779 struct map *curr_map = map;
780 struct dso *curr_dso = self;
781 size_t dso_name_len = strlen(self->short_name);
782 Elf_Data *symstrs, *secstrs;
790 Elf_Scn *sec, *sec_strndx;
794 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
796 pr_err("%s: cannot read %s ELF file.\n", __func__, name);
800 if (gelf_getehdr(elf, &ehdr) == NULL) {
801 pr_err("%s: cannot get elf header.\n", __func__);
805 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
807 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
812 syms = elf_getdata(sec, NULL);
816 sec = elf_getscn(elf, shdr.sh_link);
820 symstrs = elf_getdata(sec, NULL);
824 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
825 if (sec_strndx == NULL)
828 secstrs = elf_getdata(sec_strndx, NULL);
832 nr_syms = shdr.sh_size / shdr.sh_entsize;
834 memset(&sym, 0, sizeof(sym));
836 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
837 elf_section_by_name(elf, &ehdr, &shdr,
840 } else self->adjust_symbols = 0;
842 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
844 const char *elf_name;
845 char *demangled = NULL;
846 int is_label = elf_sym__is_label(&sym);
847 const char *section_name;
849 if (!is_label && !elf_sym__is_function(&sym))
852 sec = elf_getscn(elf, sym.st_shndx);
856 gelf_getshdr(sec, &shdr);
858 if (is_label && !elf_sec__is_text(&shdr, secstrs))
861 elf_name = elf_sym__name(&sym, symstrs);
862 section_name = elf_sec__name(&shdr, secstrs);
864 if (kernel || kmodule) {
865 char dso_name[PATH_MAX];
867 if (strcmp(section_name,
868 curr_dso->short_name + dso_name_len) == 0)
871 if (strcmp(section_name, ".text") == 0) {
877 snprintf(dso_name, sizeof(dso_name),
878 "%s%s", self->short_name, section_name);
880 curr_map = kernel_maps__find_by_dso_name(dso_name);
881 if (curr_map == NULL) {
882 u64 start = sym.st_value;
885 start += map->start + shdr.sh_offset;
887 curr_dso = dso__new(dso_name);
888 if (curr_dso == NULL)
890 curr_map = map__new2(start, curr_dso,
892 if (curr_map == NULL) {
893 dso__delete(curr_dso);
896 curr_map->map_ip = identity__map_ip;
897 curr_map->unmap_ip = identity__map_ip;
898 curr_dso->origin = DSO__ORIG_KERNEL;
899 kernel_maps__insert(curr_map);
900 dsos__add(&dsos__kernel, curr_dso);
902 curr_dso = curr_map->dso;
907 if (curr_dso->adjust_symbols) {
908 pr_debug2("adjusting symbol: st_value: %Lx sh_addr: "
909 "%Lx sh_offset: %Lx\n", (u64)sym.st_value,
910 (u64)shdr.sh_addr, (u64)shdr.sh_offset);
911 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
914 * We need to figure out if the object was created from C++ sources
915 * DWARF DW_compile_unit has this, but we don't always have access
918 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
919 if (demangled != NULL)
920 elf_name = demangled;
922 f = symbol__new(sym.st_value, sym.st_size, elf_name);
927 if (filter && filter(curr_map, f))
930 symbols__insert(&curr_dso->symbols[curr_map->type], f);
936 * For misannotated, zeroed, ASM function sizes.
939 symbols__fixup_end(&self->symbols[map->type]);
947 static bool dso__build_id_equal(const struct dso *self, u8 *build_id)
949 return memcmp(self->build_id, build_id, sizeof(self->build_id)) == 0;
952 static bool __dsos__read_build_ids(struct list_head *head)
954 bool have_build_id = false;
957 list_for_each_entry(pos, head, node)
958 if (filename__read_build_id(pos->long_name, pos->build_id,
959 sizeof(pos->build_id)) > 0) {
960 have_build_id = true;
961 pos->has_build_id = true;
964 return have_build_id;
967 bool dsos__read_build_ids(void)
969 return __dsos__read_build_ids(&dsos__kernel) ||
970 __dsos__read_build_ids(&dsos__user);
974 * Align offset to 4 bytes as needed for note name and descriptor data.
976 #define NOTE_ALIGN(n) (((n) + 3) & -4U)
978 int filename__read_build_id(const char *filename, void *bf, size_t size)
989 if (size < BUILD_ID_SIZE)
992 fd = open(filename, O_RDONLY);
996 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
998 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
1003 if (ek != ELF_K_ELF)
1006 if (gelf_getehdr(elf, &ehdr) == NULL) {
1007 pr_err("%s: cannot get elf header.\n", __func__);
1011 sec = elf_section_by_name(elf, &ehdr, &shdr,
1012 ".note.gnu.build-id", NULL);
1014 sec = elf_section_by_name(elf, &ehdr, &shdr,
1020 data = elf_getdata(sec, NULL);
1025 while (ptr < (data->d_buf + data->d_size)) {
1026 GElf_Nhdr *nhdr = ptr;
1027 int namesz = NOTE_ALIGN(nhdr->n_namesz),
1028 descsz = NOTE_ALIGN(nhdr->n_descsz);
1031 ptr += sizeof(*nhdr);
1034 if (nhdr->n_type == NT_GNU_BUILD_ID &&
1035 nhdr->n_namesz == sizeof("GNU")) {
1036 if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
1037 memcpy(bf, ptr, BUILD_ID_SIZE);
1038 err = BUILD_ID_SIZE;
1052 int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
1056 if (size < BUILD_ID_SIZE)
1059 fd = open(filename, O_RDONLY);
1068 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
1071 namesz = NOTE_ALIGN(nhdr.n_namesz);
1072 descsz = NOTE_ALIGN(nhdr.n_descsz);
1073 if (nhdr.n_type == NT_GNU_BUILD_ID &&
1074 nhdr.n_namesz == sizeof("GNU")) {
1075 if (read(fd, bf, namesz) != namesz)
1077 if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
1078 if (read(fd, build_id,
1079 BUILD_ID_SIZE) == BUILD_ID_SIZE) {
1083 } else if (read(fd, bf, descsz) != descsz)
1086 int n = namesz + descsz;
1087 if (read(fd, bf, n) != n)
1096 char dso__symtab_origin(const struct dso *self)
1098 static const char origin[] = {
1099 [DSO__ORIG_KERNEL] = 'k',
1100 [DSO__ORIG_JAVA_JIT] = 'j',
1101 [DSO__ORIG_FEDORA] = 'f',
1102 [DSO__ORIG_UBUNTU] = 'u',
1103 [DSO__ORIG_BUILDID] = 'b',
1104 [DSO__ORIG_DSO] = 'd',
1105 [DSO__ORIG_KMODULE] = 'K',
1108 if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
1110 return origin[self->origin];
1113 int dso__load(struct dso *self, struct map *map, symbol_filter_t filter)
1115 int size = PATH_MAX;
1117 u8 build_id[BUILD_ID_SIZE];
1121 dso__set_loaded(self, map->type);
1124 return dso__load_kernel_sym(self, map, filter);
1126 name = malloc(size);
1130 self->adjust_symbols = 0;
1132 if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
1133 ret = dso__load_perf_map(self, map, filter);
1134 self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
1135 DSO__ORIG_NOT_FOUND;
1139 self->origin = DSO__ORIG_FEDORA - 1;
1144 switch (self->origin) {
1145 case DSO__ORIG_FEDORA:
1146 snprintf(name, size, "/usr/lib/debug%s.debug",
1149 case DSO__ORIG_UBUNTU:
1150 snprintf(name, size, "/usr/lib/debug%s",
1153 case DSO__ORIG_BUILDID:
1154 if (filename__read_build_id(self->long_name, build_id,
1155 sizeof(build_id))) {
1156 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
1158 build_id__sprintf(build_id, sizeof(build_id),
1160 snprintf(name, size,
1161 "/usr/lib/debug/.build-id/%.2s/%s.debug",
1162 build_id_hex, build_id_hex + 2);
1163 if (self->has_build_id)
1164 goto compare_build_id;
1170 snprintf(name, size, "%s", self->long_name);
1177 if (self->has_build_id) {
1178 if (filename__read_build_id(name, build_id,
1179 sizeof(build_id)) < 0)
1182 if (!dso__build_id_equal(self, build_id))
1186 fd = open(name, O_RDONLY);
1189 ret = dso__load_sym(self, map, name, fd, filter, 0, 0);
1193 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
1199 int nr_plt = dso__synthesize_plt_symbols(self, map, filter);
1205 if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
1210 static void kernel_maps__insert(struct map *map)
1212 maps__insert(&kernel_maps[map->type], map);
1215 static struct symbol *kernel_maps__find_symbol(u64 ip, enum map_type type,
1217 symbol_filter_t filter)
1219 struct map *map = maps__find(&kernel_maps[type], ip);
1225 ip = map->map_ip(map, ip);
1226 return map__find_symbol(map, ip, filter);
1228 WARN_ONCE(RB_EMPTY_ROOT(&kernel_maps[type]),
1229 "Empty kernel_maps, was symbol__init() called?\n");
1234 struct symbol *kernel_maps__find_function(u64 ip, struct map **mapp,
1235 symbol_filter_t filter)
1237 return kernel_maps__find_symbol(ip, MAP__FUNCTION, mapp, filter);
1240 static struct map *kernel_maps__find_by_dso_name(const char *name)
1244 for (nd = rb_first(&kernel_maps[MAP__FUNCTION]); nd; nd = rb_next(nd)) {
1245 struct map *map = rb_entry(nd, struct map, rb_node);
1247 if (map->dso && strcmp(map->dso->name, name) == 0)
1254 static int dsos__set_modules_path_dir(char *dirname)
1256 struct dirent *dent;
1257 DIR *dir = opendir(dirname);
1260 pr_debug("%s: cannot open %s dir\n", __func__, dirname);
1264 while ((dent = readdir(dir)) != NULL) {
1265 char path[PATH_MAX];
1267 if (dent->d_type == DT_DIR) {
1268 if (!strcmp(dent->d_name, ".") ||
1269 !strcmp(dent->d_name, ".."))
1272 snprintf(path, sizeof(path), "%s/%s",
1273 dirname, dent->d_name);
1274 if (dsos__set_modules_path_dir(path) < 0)
1277 char *dot = strrchr(dent->d_name, '.'),
1282 if (dot == NULL || strcmp(dot, ".ko"))
1284 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1285 (int)(dot - dent->d_name), dent->d_name);
1287 strxfrchar(dso_name, '-', '_');
1288 map = kernel_maps__find_by_dso_name(dso_name);
1292 snprintf(path, sizeof(path), "%s/%s",
1293 dirname, dent->d_name);
1295 long_name = strdup(path);
1296 if (long_name == NULL)
1298 dso__set_long_name(map->dso, long_name);
1308 static int dsos__set_modules_path(void)
1311 char modules_path[PATH_MAX];
1313 if (uname(&uts) < 0)
1316 snprintf(modules_path, sizeof(modules_path), "/lib/modules/%s/kernel",
1319 return dsos__set_modules_path_dir(modules_path);
1323 * Constructor variant for modules (where we know from /proc/modules where
1324 * they are loaded) and for vmlinux, where only after we load all the
1325 * symbols we'll know where it starts and ends.
1327 static struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
1329 struct map *self = malloc(sizeof(*self));
1333 * ->end will be filled after we load all the symbols
1335 map__init(self, type, start, 0, 0, dso);
1341 static int kernel_maps__create_module_maps(void)
1345 FILE *file = fopen("/proc/modules", "r");
1351 while (!feof(file)) {
1352 char name[PATH_MAX];
1358 line_len = getline(&line, &n, file);
1365 line[--line_len] = '\0'; /* \n */
1367 sep = strrchr(line, 'x');
1371 hex2u64(sep + 1, &start);
1373 sep = strchr(line, ' ');
1379 snprintf(name, sizeof(name), "[%s]", line);
1380 dso = dso__new(name);
1383 goto out_delete_line;
1385 map = map__new2(start, dso, MAP__FUNCTION);
1388 goto out_delete_line;
1391 snprintf(name, sizeof(name),
1392 "/sys/module/%s/notes/.note.gnu.build-id", line);
1393 if (sysfs__read_build_id(name, dso->build_id,
1394 sizeof(dso->build_id)) == 0)
1395 dso->has_build_id = true;
1397 dso->origin = DSO__ORIG_KMODULE;
1398 kernel_maps__insert(map);
1399 dsos__add(&dsos__kernel, dso);
1405 return dsos__set_modules_path();
1413 static int dso__load_vmlinux(struct dso *self, struct map *map,
1414 const char *vmlinux, symbol_filter_t filter)
1418 if (self->has_build_id) {
1419 u8 build_id[BUILD_ID_SIZE];
1421 if (filename__read_build_id(vmlinux, build_id,
1422 sizeof(build_id)) < 0) {
1423 pr_debug("No build_id in %s, ignoring it\n", vmlinux);
1426 if (!dso__build_id_equal(self, build_id)) {
1427 char expected_build_id[BUILD_ID_SIZE * 2 + 1],
1428 vmlinux_build_id[BUILD_ID_SIZE * 2 + 1];
1430 build_id__sprintf(self->build_id,
1431 sizeof(self->build_id),
1433 build_id__sprintf(build_id, sizeof(build_id),
1435 pr_debug("build_id in %s is %s while expected is %s, "
1436 "ignoring it\n", vmlinux, vmlinux_build_id,
1442 fd = open(vmlinux, O_RDONLY);
1446 dso__set_loaded(self, map->type);
1447 err = dso__load_sym(self, map, self->long_name, fd, filter, 1, 0);
1454 static int dso__load_kernel_sym(struct dso *self, struct map *map,
1455 symbol_filter_t filter)
1460 if (vmlinux_path != NULL) {
1462 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1463 vmlinux_path__nr_entries);
1464 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
1465 err = dso__load_vmlinux(self, map, vmlinux_path[i],
1468 pr_debug("Using %s for symbols\n",
1470 dso__set_long_name(self,
1471 strdup(vmlinux_path[i]));
1477 is_kallsyms = self->long_name[0] == '[';
1481 err = dso__load_vmlinux(self, map, self->long_name, filter);
1483 pr_info("The file %s cannot be used, "
1484 "trying to use /proc/kallsyms...", self->long_name);
1486 err = dso__load_kallsyms(self, map, filter);
1487 if (err > 0 && !is_kallsyms)
1488 dso__set_long_name(self, strdup("[kernel.kallsyms]"));
1493 map__fixup_start(map);
1494 map__fixup_end(map);
1500 LIST_HEAD(dsos__user);
1501 LIST_HEAD(dsos__kernel);
1504 static void dsos__add(struct list_head *head, struct dso *dso)
1506 list_add_tail(&dso->node, head);
1509 static struct dso *dsos__find(struct list_head *head, const char *name)
1513 list_for_each_entry(pos, head, node)
1514 if (strcmp(pos->name, name) == 0)
1519 struct dso *dsos__findnew(const char *name)
1521 struct dso *dso = dsos__find(&dsos__user, name);
1524 dso = dso__new(name);
1526 dsos__add(&dsos__user, dso);
1527 dso__set_basename(dso);
1534 static void __dsos__fprintf(struct list_head *head, FILE *fp)
1538 list_for_each_entry(pos, head, node)
1539 dso__fprintf(pos, fp);
1542 void dsos__fprintf(FILE *fp)
1544 __dsos__fprintf(&dsos__kernel, fp);
1545 __dsos__fprintf(&dsos__user, fp);
1548 static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp)
1553 list_for_each_entry(pos, head, node) {
1554 ret += dso__fprintf_buildid(pos, fp);
1555 ret += fprintf(fp, " %s\n", pos->long_name);
1560 size_t dsos__fprintf_buildid(FILE *fp)
1562 return (__dsos__fprintf_buildid(&dsos__kernel, fp) +
1563 __dsos__fprintf_buildid(&dsos__user, fp));
1566 static int kernel_maps__create_kernel_map(const struct symbol_conf *conf)
1569 struct dso *kernel = dso__new(conf->vmlinux_name ?: "[kernel.kallsyms]");
1574 kmap = map__new2(0, kernel, MAP__FUNCTION);
1576 goto out_delete_kernel_dso;
1578 kmap->map_ip = kmap->unmap_ip = identity__map_ip;
1579 kernel->short_name = "[kernel]";
1582 vdso = dso__new("[vdso]");
1584 goto out_delete_kernel_map;
1585 dso__set_loaded(vdso, MAP__FUNCTION);
1587 if (sysfs__read_build_id("/sys/kernel/notes", kernel->build_id,
1588 sizeof(kernel->build_id)) == 0)
1589 kernel->has_build_id = true;
1591 kernel_maps__insert(kmap);
1592 dsos__add(&dsos__kernel, kernel);
1593 dsos__add(&dsos__user, vdso);
1597 out_delete_kernel_map:
1599 out_delete_kernel_dso:
1600 dso__delete(kernel);
1604 static void vmlinux_path__exit(void)
1606 while (--vmlinux_path__nr_entries >= 0) {
1607 free(vmlinux_path[vmlinux_path__nr_entries]);
1608 vmlinux_path[vmlinux_path__nr_entries] = NULL;
1612 vmlinux_path = NULL;
1615 static int vmlinux_path__init(void)
1620 if (uname(&uts) < 0)
1623 vmlinux_path = malloc(sizeof(char *) * 5);
1624 if (vmlinux_path == NULL)
1627 vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
1628 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1630 ++vmlinux_path__nr_entries;
1631 vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
1632 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1634 ++vmlinux_path__nr_entries;
1635 snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
1636 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1637 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1639 ++vmlinux_path__nr_entries;
1640 snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
1641 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1642 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1644 ++vmlinux_path__nr_entries;
1645 snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
1647 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1648 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1650 ++vmlinux_path__nr_entries;
1655 vmlinux_path__exit();
1659 static int kernel_maps__init(const struct symbol_conf *conf)
1661 const struct symbol_conf *pconf = conf ?: &symbol_conf__defaults;
1663 symbol__priv_size = pconf->priv_size;
1665 if (pconf->try_vmlinux_path && vmlinux_path__init() < 0)
1668 if (kernel_maps__create_kernel_map(pconf) < 0) {
1669 vmlinux_path__exit();
1673 if (pconf->use_modules && kernel_maps__create_module_maps() < 0)
1674 pr_debug("Failed to load list of modules in use, "
1677 * Now that we have all the maps created, just set the ->end of them:
1679 kernel_maps__fixup_end();
1683 int symbol__init(struct symbol_conf *conf)
1685 elf_version(EV_CURRENT);
1686 return kernel_maps__init(conf);