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 *map__new2(u64 start, struct dso *dso, enum map_type type);
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;
37 static int vmlinux_path__nr_entries;
38 static char **vmlinux_path;
39 static struct map *kernel_map__functions;
41 static struct symbol_conf symbol_conf__defaults = {
43 .try_vmlinux_path = true,
46 static struct rb_root kernel_maps__functions;
48 bool dso__loaded(const struct dso *self, enum map_type type)
50 return self->loaded & (1 << type);
53 static void dso__set_loaded(struct dso *self, enum map_type type)
55 self->loaded |= (1 << type);
58 static void symbols__fixup_end(struct rb_root *self)
60 struct rb_node *nd, *prevnd = rb_first(self);
61 struct symbol *curr, *prev;
66 curr = rb_entry(prevnd, struct symbol, rb_node);
68 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
70 curr = rb_entry(nd, struct symbol, rb_node);
72 if (prev->end == prev->start)
73 prev->end = curr->start - 1;
77 if (curr->end == curr->start)
78 curr->end = roundup(curr->start, 4096);
81 static void kernel_maps__fixup_end(void)
83 struct map *prev, *curr;
84 struct rb_node *nd, *prevnd = rb_first(&kernel_maps__functions);
89 curr = rb_entry(prevnd, struct map, rb_node);
91 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
93 curr = rb_entry(nd, struct map, rb_node);
94 prev->end = curr->start - 1;
98 * We still haven't the actual symbols, so guess the
99 * last map final address.
104 static struct symbol *symbol__new(u64 start, u64 len, const char *name)
106 size_t namelen = strlen(name) + 1;
107 struct symbol *self = zalloc(symbol__priv_size +
108 sizeof(*self) + namelen);
112 if (symbol__priv_size)
113 self = ((void *)self) + symbol__priv_size;
116 self->end = len ? start + len - 1 : start;
118 pr_debug3("%s: %s %#Lx-%#Lx\n", __func__, name, start, self->end);
120 memcpy(self->name, name, namelen);
125 static void symbol__delete(struct symbol *self)
127 free(((void *)self) - symbol__priv_size);
130 static size_t symbol__fprintf(struct symbol *self, FILE *fp)
132 return fprintf(fp, " %llx-%llx %s\n",
133 self->start, self->end, self->name);
136 static void dso__set_long_name(struct dso *self, char *name)
140 self->long_name = name;
141 self->long_name_len = strlen(name);
144 static void dso__set_basename(struct dso *self)
146 self->short_name = basename(self->long_name);
149 struct dso *dso__new(const char *name)
151 struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
154 strcpy(self->name, name);
155 dso__set_long_name(self, self->name);
156 self->short_name = self->name;
157 self->functions = RB_ROOT;
158 self->find_function = dso__find_function;
159 self->slen_calculated = 0;
160 self->origin = DSO__ORIG_NOT_FOUND;
162 self->has_build_id = 0;
168 static void symbols__delete(struct rb_root *self)
171 struct rb_node *next = rb_first(self);
174 pos = rb_entry(next, struct symbol, rb_node);
175 next = rb_next(&pos->rb_node);
176 rb_erase(&pos->rb_node, self);
181 void dso__delete(struct dso *self)
183 symbols__delete(&self->functions);
184 if (self->long_name != self->name)
185 free(self->long_name);
189 void dso__set_build_id(struct dso *self, void *build_id)
191 memcpy(self->build_id, build_id, sizeof(self->build_id));
192 self->has_build_id = 1;
195 static void symbols__insert(struct rb_root *self, struct symbol *sym)
197 struct rb_node **p = &self->rb_node;
198 struct rb_node *parent = NULL;
199 const u64 ip = sym->start;
204 s = rb_entry(parent, struct symbol, rb_node);
210 rb_link_node(&sym->rb_node, parent, p);
211 rb_insert_color(&sym->rb_node, self);
214 static struct symbol *symbols__find(struct rb_root *self, u64 ip)
224 struct symbol *s = rb_entry(n, struct symbol, rb_node);
228 else if (ip > s->end)
237 struct symbol *dso__find_function(struct dso *self, u64 ip)
239 return symbols__find(&self->functions, ip);
242 int build_id__sprintf(u8 *self, int len, char *bf)
248 for (i = 0; i < len; ++i) {
249 sprintf(bid, "%02x", *raw);
257 size_t dso__fprintf_buildid(struct dso *self, FILE *fp)
259 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
261 build_id__sprintf(self->build_id, sizeof(self->build_id), sbuild_id);
262 return fprintf(fp, "%s", sbuild_id);
265 size_t dso__fprintf(struct dso *self, FILE *fp)
268 size_t ret = fprintf(fp, "dso: %s (", self->short_name);
270 ret += dso__fprintf_buildid(self, fp);
271 ret += fprintf(fp, ")\nFunctions:\n");
273 for (nd = rb_first(&self->functions); nd; nd = rb_next(nd)) {
274 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
275 ret += symbol__fprintf(pos, fp);
282 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
283 * so that we can in the next step set the symbol ->end address and then
284 * call kernel_maps__split_kallsyms.
286 static int kernel_maps__load_all_kallsyms(void)
290 FILE *file = fopen("/proc/kallsyms", "r");
295 while (!feof(file)) {
302 line_len = getline(&line, &n, file);
309 line[--line_len] = '\0'; /* \n */
311 len = hex2u64(line, &start);
314 if (len + 2 >= line_len)
317 symbol_type = toupper(line[len]);
319 * We're interested only in code ('T'ext)
321 if (symbol_type != 'T' && symbol_type != 'W')
324 symbol_name = line + len + 2;
326 * Will fix up the end later, when we have all symbols sorted.
328 sym = symbol__new(start, 0, symbol_name);
331 goto out_delete_line;
334 * We will pass the symbols to the filter later, in
335 * kernel_maps__split_kallsyms, when we have split the
338 symbols__insert(&kernel_map__functions->dso->functions, sym);
353 * Split the symbols into maps, making sure there are no overlaps, i.e. the
354 * kernel range is broken in several maps, named [kernel].N, as we don't have
355 * the original ELF section names vmlinux have.
357 static int kernel_maps__split_kallsyms(symbol_filter_t filter)
359 struct map *map = kernel_map__functions;
362 struct rb_node *next = rb_first(&kernel_map__functions->dso->functions);
363 int kernel_range = 0;
368 pos = rb_entry(next, struct symbol, rb_node);
369 next = rb_next(&pos->rb_node);
371 module = strchr(pos->name, '\t');
375 if (strcmp(map->dso->name, module)) {
376 map = kernel_maps__find_by_dso_name(module);
378 pr_err("/proc/{kallsyms,modules} "
384 * So that we look just like we get from .ko files,
385 * i.e. not prelinked, relative to map->start.
387 pos->start = map->map_ip(map, pos->start);
388 pos->end = map->map_ip(map, pos->end);
389 } else if (map != kernel_map__functions) {
390 char dso_name[PATH_MAX];
393 snprintf(dso_name, sizeof(dso_name), "[kernel].%d",
396 dso = dso__new(dso_name);
400 map = map__new2(pos->start, dso, MAP__FUNCTION);
406 map->map_ip = map->unmap_ip = identity__map_ip;
407 kernel_maps__insert(map);
411 if (filter && filter(map, pos)) {
412 rb_erase(&pos->rb_node, &kernel_map__functions->dso->functions);
415 if (map != kernel_map__functions) {
416 rb_erase(&pos->rb_node,
417 &kernel_map__functions->dso->functions);
418 symbols__insert(&map->dso->functions, pos);
428 static int kernel_maps__load_kallsyms(symbol_filter_t filter)
430 if (kernel_maps__load_all_kallsyms())
433 symbols__fixup_end(&kernel_map__functions->dso->functions);
434 kernel_map__functions->dso->origin = DSO__ORIG_KERNEL;
436 return kernel_maps__split_kallsyms(filter);
439 size_t kernel_maps__fprintf(FILE *fp)
441 size_t printed = fprintf(fp, "Kernel maps:\n");
444 for (nd = rb_first(&kernel_maps__functions); nd; nd = rb_next(nd)) {
445 struct map *pos = rb_entry(nd, struct map, rb_node);
447 printed += fprintf(fp, "Map:");
448 printed += map__fprintf(pos, fp);
450 printed += dso__fprintf(pos->dso, fp);
451 printed += fprintf(fp, "--\n");
455 return printed + fprintf(fp, "END kernel maps\n");
458 static int dso__load_perf_map(struct dso *self, struct map *map,
459 symbol_filter_t filter)
466 file = fopen(self->long_name, "r");
470 while (!feof(file)) {
475 line_len = getline(&line, &n, file);
482 line[--line_len] = '\0'; /* \n */
484 len = hex2u64(line, &start);
487 if (len + 2 >= line_len)
490 len += hex2u64(line + len, &size);
493 if (len + 2 >= line_len)
496 sym = symbol__new(start, size, line + len);
499 goto out_delete_line;
501 if (filter && filter(map, sym))
504 symbols__insert(&self->functions, sym);
521 * elf_symtab__for_each_symbol - iterate thru all the symbols
523 * @self: struct elf_symtab instance to iterate
525 * @sym: GElf_Sym iterator
527 #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
528 for (idx = 0, gelf_getsym(syms, idx, &sym);\
530 idx++, gelf_getsym(syms, idx, &sym))
532 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
534 return GELF_ST_TYPE(sym->st_info);
537 static inline int elf_sym__is_function(const GElf_Sym *sym)
539 return elf_sym__type(sym) == STT_FUNC &&
541 sym->st_shndx != SHN_UNDEF;
544 static inline int elf_sym__is_label(const GElf_Sym *sym)
546 return elf_sym__type(sym) == STT_NOTYPE &&
548 sym->st_shndx != SHN_UNDEF &&
549 sym->st_shndx != SHN_ABS;
552 static inline const char *elf_sec__name(const GElf_Shdr *shdr,
553 const Elf_Data *secstrs)
555 return secstrs->d_buf + shdr->sh_name;
558 static inline int elf_sec__is_text(const GElf_Shdr *shdr,
559 const Elf_Data *secstrs)
561 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
564 static inline const char *elf_sym__name(const GElf_Sym *sym,
565 const Elf_Data *symstrs)
567 return symstrs->d_buf + sym->st_name;
570 static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
571 GElf_Shdr *shp, const char *name,
577 while ((sec = elf_nextscn(elf, sec)) != NULL) {
580 gelf_getshdr(sec, shp);
581 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
582 if (!strcmp(name, str)) {
593 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
594 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
596 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
598 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
599 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
601 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
604 * We need to check if we have a .dynsym, so that we can handle the
605 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
606 * .dynsym or .symtab).
607 * And always look at the original dso, not at debuginfo packages, that
608 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
610 static int dso__synthesize_plt_symbols(struct dso *self, struct map *map,
611 symbol_filter_t filter)
613 uint32_t nr_rel_entries, idx;
618 GElf_Shdr shdr_rel_plt, shdr_dynsym;
619 Elf_Data *reldata, *syms, *symstrs;
620 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
623 char sympltname[1024];
625 int nr = 0, symidx, fd, err = 0;
627 fd = open(self->long_name, O_RDONLY);
631 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
635 if (gelf_getehdr(elf, &ehdr) == NULL)
638 scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
639 ".dynsym", &dynsym_idx);
640 if (scn_dynsym == NULL)
643 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
645 if (scn_plt_rel == NULL) {
646 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
648 if (scn_plt_rel == NULL)
654 if (shdr_rel_plt.sh_link != dynsym_idx)
657 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
661 * Fetch the relocation section to find the idxes to the GOT
662 * and the symbols in the .dynsym they refer to.
664 reldata = elf_getdata(scn_plt_rel, NULL);
668 syms = elf_getdata(scn_dynsym, NULL);
672 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
673 if (scn_symstrs == NULL)
676 symstrs = elf_getdata(scn_symstrs, NULL);
680 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
681 plt_offset = shdr_plt.sh_offset;
683 if (shdr_rel_plt.sh_type == SHT_RELA) {
684 GElf_Rela pos_mem, *pos;
686 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
688 symidx = GELF_R_SYM(pos->r_info);
689 plt_offset += shdr_plt.sh_entsize;
690 gelf_getsym(syms, symidx, &sym);
691 snprintf(sympltname, sizeof(sympltname),
692 "%s@plt", elf_sym__name(&sym, symstrs));
694 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
699 if (filter && filter(map, f))
702 symbols__insert(&self->functions, f);
706 } else if (shdr_rel_plt.sh_type == SHT_REL) {
707 GElf_Rel pos_mem, *pos;
708 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
710 symidx = GELF_R_SYM(pos->r_info);
711 plt_offset += shdr_plt.sh_entsize;
712 gelf_getsym(syms, symidx, &sym);
713 snprintf(sympltname, sizeof(sympltname),
714 "%s@plt", elf_sym__name(&sym, symstrs));
716 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
721 if (filter && filter(map, f))
724 symbols__insert(&self->functions, f);
739 pr_warning("%s: problems reading %s PLT info.\n",
740 __func__, self->long_name);
744 static int dso__load_sym(struct dso *self, struct map *map, const char *name,
745 int fd, symbol_filter_t filter, int kernel,
748 struct map *curr_map = map;
749 struct dso *curr_dso = self;
750 size_t dso_name_len = strlen(self->short_name);
751 Elf_Data *symstrs, *secstrs;
759 Elf_Scn *sec, *sec_strndx;
763 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
765 pr_err("%s: cannot read %s ELF file.\n", __func__, name);
769 if (gelf_getehdr(elf, &ehdr) == NULL) {
770 pr_err("%s: cannot get elf header.\n", __func__);
774 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
776 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
781 syms = elf_getdata(sec, NULL);
785 sec = elf_getscn(elf, shdr.sh_link);
789 symstrs = elf_getdata(sec, NULL);
793 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
794 if (sec_strndx == NULL)
797 secstrs = elf_getdata(sec_strndx, NULL);
801 nr_syms = shdr.sh_size / shdr.sh_entsize;
803 memset(&sym, 0, sizeof(sym));
805 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
806 elf_section_by_name(elf, &ehdr, &shdr,
809 } else self->adjust_symbols = 0;
811 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
813 const char *elf_name;
814 char *demangled = NULL;
815 int is_label = elf_sym__is_label(&sym);
816 const char *section_name;
818 if (!is_label && !elf_sym__is_function(&sym))
821 sec = elf_getscn(elf, sym.st_shndx);
825 gelf_getshdr(sec, &shdr);
827 if (is_label && !elf_sec__is_text(&shdr, secstrs))
830 elf_name = elf_sym__name(&sym, symstrs);
831 section_name = elf_sec__name(&shdr, secstrs);
833 if (kernel || kmodule) {
834 char dso_name[PATH_MAX];
836 if (strcmp(section_name,
837 curr_dso->short_name + dso_name_len) == 0)
840 if (strcmp(section_name, ".text") == 0) {
846 snprintf(dso_name, sizeof(dso_name),
847 "%s%s", self->short_name, section_name);
849 curr_map = kernel_maps__find_by_dso_name(dso_name);
850 if (curr_map == NULL) {
851 u64 start = sym.st_value;
854 start += map->start + shdr.sh_offset;
856 curr_dso = dso__new(dso_name);
857 if (curr_dso == NULL)
859 curr_map = map__new2(start, curr_dso,
861 if (curr_map == NULL) {
862 dso__delete(curr_dso);
865 curr_map->map_ip = identity__map_ip;
866 curr_map->unmap_ip = identity__map_ip;
867 curr_dso->origin = DSO__ORIG_KERNEL;
868 kernel_maps__insert(curr_map);
869 dsos__add(&dsos__kernel, curr_dso);
871 curr_dso = curr_map->dso;
876 if (curr_dso->adjust_symbols) {
877 pr_debug2("adjusting symbol: st_value: %Lx sh_addr: "
878 "%Lx sh_offset: %Lx\n", (u64)sym.st_value,
879 (u64)shdr.sh_addr, (u64)shdr.sh_offset);
880 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
883 * We need to figure out if the object was created from C++ sources
884 * DWARF DW_compile_unit has this, but we don't always have access
887 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
888 if (demangled != NULL)
889 elf_name = demangled;
891 f = symbol__new(sym.st_value, sym.st_size, elf_name);
896 if (filter && filter(curr_map, f))
899 symbols__insert(&curr_dso->functions, f);
905 * For misannotated, zeroed, ASM function sizes.
908 symbols__fixup_end(&self->functions);
916 static bool dso__build_id_equal(const struct dso *self, u8 *build_id)
918 return memcmp(self->build_id, build_id, sizeof(self->build_id)) == 0;
921 static bool __dsos__read_build_ids(struct list_head *head)
923 bool have_build_id = false;
926 list_for_each_entry(pos, head, node)
927 if (filename__read_build_id(pos->long_name, pos->build_id,
928 sizeof(pos->build_id)) > 0) {
929 have_build_id = true;
930 pos->has_build_id = true;
933 return have_build_id;
936 bool dsos__read_build_ids(void)
938 return __dsos__read_build_ids(&dsos__kernel) ||
939 __dsos__read_build_ids(&dsos__user);
943 * Align offset to 4 bytes as needed for note name and descriptor data.
945 #define NOTE_ALIGN(n) (((n) + 3) & -4U)
947 int filename__read_build_id(const char *filename, void *bf, size_t size)
958 if (size < BUILD_ID_SIZE)
961 fd = open(filename, O_RDONLY);
965 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
967 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
975 if (gelf_getehdr(elf, &ehdr) == NULL) {
976 pr_err("%s: cannot get elf header.\n", __func__);
980 sec = elf_section_by_name(elf, &ehdr, &shdr,
981 ".note.gnu.build-id", NULL);
983 sec = elf_section_by_name(elf, &ehdr, &shdr,
989 data = elf_getdata(sec, NULL);
994 while (ptr < (data->d_buf + data->d_size)) {
995 GElf_Nhdr *nhdr = ptr;
996 int namesz = NOTE_ALIGN(nhdr->n_namesz),
997 descsz = NOTE_ALIGN(nhdr->n_descsz);
1000 ptr += sizeof(*nhdr);
1003 if (nhdr->n_type == NT_GNU_BUILD_ID &&
1004 nhdr->n_namesz == sizeof("GNU")) {
1005 if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
1006 memcpy(bf, ptr, BUILD_ID_SIZE);
1007 err = BUILD_ID_SIZE;
1021 int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
1025 if (size < BUILD_ID_SIZE)
1028 fd = open(filename, O_RDONLY);
1037 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
1040 namesz = NOTE_ALIGN(nhdr.n_namesz);
1041 descsz = NOTE_ALIGN(nhdr.n_descsz);
1042 if (nhdr.n_type == NT_GNU_BUILD_ID &&
1043 nhdr.n_namesz == sizeof("GNU")) {
1044 if (read(fd, bf, namesz) != namesz)
1046 if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
1047 if (read(fd, build_id,
1048 BUILD_ID_SIZE) == BUILD_ID_SIZE) {
1052 } else if (read(fd, bf, descsz) != descsz)
1055 int n = namesz + descsz;
1056 if (read(fd, bf, n) != n)
1065 char dso__symtab_origin(const struct dso *self)
1067 static const char origin[] = {
1068 [DSO__ORIG_KERNEL] = 'k',
1069 [DSO__ORIG_JAVA_JIT] = 'j',
1070 [DSO__ORIG_FEDORA] = 'f',
1071 [DSO__ORIG_UBUNTU] = 'u',
1072 [DSO__ORIG_BUILDID] = 'b',
1073 [DSO__ORIG_DSO] = 'd',
1074 [DSO__ORIG_KMODULE] = 'K',
1077 if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
1079 return origin[self->origin];
1082 int dso__load(struct dso *self, struct map *map, symbol_filter_t filter)
1084 int size = PATH_MAX;
1086 u8 build_id[BUILD_ID_SIZE];
1090 dso__set_loaded(self, map->type);
1093 return dso__load_kernel_sym(self, map, filter);
1095 name = malloc(size);
1099 self->adjust_symbols = 0;
1101 if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
1102 ret = dso__load_perf_map(self, map, filter);
1103 self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
1104 DSO__ORIG_NOT_FOUND;
1108 self->origin = DSO__ORIG_FEDORA - 1;
1113 switch (self->origin) {
1114 case DSO__ORIG_FEDORA:
1115 snprintf(name, size, "/usr/lib/debug%s.debug",
1118 case DSO__ORIG_UBUNTU:
1119 snprintf(name, size, "/usr/lib/debug%s",
1122 case DSO__ORIG_BUILDID:
1123 if (filename__read_build_id(self->long_name, build_id,
1124 sizeof(build_id))) {
1125 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
1127 build_id__sprintf(build_id, sizeof(build_id),
1129 snprintf(name, size,
1130 "/usr/lib/debug/.build-id/%.2s/%s.debug",
1131 build_id_hex, build_id_hex + 2);
1132 if (self->has_build_id)
1133 goto compare_build_id;
1139 snprintf(name, size, "%s", self->long_name);
1146 if (self->has_build_id) {
1147 if (filename__read_build_id(name, build_id,
1148 sizeof(build_id)) < 0)
1151 if (!dso__build_id_equal(self, build_id))
1155 fd = open(name, O_RDONLY);
1158 ret = dso__load_sym(self, map, name, fd, filter, 0, 0);
1162 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
1168 int nr_plt = dso__synthesize_plt_symbols(self, map, filter);
1174 if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
1179 static void kernel_maps__insert(struct map *map)
1181 maps__insert(&kernel_maps__functions, map);
1184 struct symbol *kernel_maps__find_function(u64 ip, struct map **mapp,
1185 symbol_filter_t filter)
1187 struct map *map = maps__find(&kernel_maps__functions, ip);
1193 ip = map->map_ip(map, ip);
1194 return map__find_function(map, ip, filter);
1196 WARN_ONCE(RB_EMPTY_ROOT(&kernel_maps__functions),
1197 "Empty kernel_maps, was symbol__init() called?\n");
1202 struct map *kernel_maps__find_by_dso_name(const char *name)
1206 for (nd = rb_first(&kernel_maps__functions); nd; nd = rb_next(nd)) {
1207 struct map *map = rb_entry(nd, struct map, rb_node);
1209 if (map->dso && strcmp(map->dso->name, name) == 0)
1216 static int dsos__set_modules_path_dir(char *dirname)
1218 struct dirent *dent;
1219 DIR *dir = opendir(dirname);
1222 pr_debug("%s: cannot open %s dir\n", __func__, dirname);
1226 while ((dent = readdir(dir)) != NULL) {
1227 char path[PATH_MAX];
1229 if (dent->d_type == DT_DIR) {
1230 if (!strcmp(dent->d_name, ".") ||
1231 !strcmp(dent->d_name, ".."))
1234 snprintf(path, sizeof(path), "%s/%s",
1235 dirname, dent->d_name);
1236 if (dsos__set_modules_path_dir(path) < 0)
1239 char *dot = strrchr(dent->d_name, '.'),
1244 if (dot == NULL || strcmp(dot, ".ko"))
1246 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1247 (int)(dot - dent->d_name), dent->d_name);
1249 strxfrchar(dso_name, '-', '_');
1250 map = kernel_maps__find_by_dso_name(dso_name);
1254 snprintf(path, sizeof(path), "%s/%s",
1255 dirname, dent->d_name);
1257 long_name = strdup(path);
1258 if (long_name == NULL)
1260 dso__set_long_name(map->dso, long_name);
1270 static int dsos__set_modules_path(void)
1273 char modules_path[PATH_MAX];
1275 if (uname(&uts) < 0)
1278 snprintf(modules_path, sizeof(modules_path), "/lib/modules/%s/kernel",
1281 return dsos__set_modules_path_dir(modules_path);
1285 * Constructor variant for modules (where we know from /proc/modules where
1286 * they are loaded) and for vmlinux, where only after we load all the
1287 * symbols we'll know where it starts and ends.
1289 static struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
1291 struct map *self = malloc(sizeof(*self));
1295 * ->end will be filled after we load all the symbols
1297 map__init(self, type, start, 0, 0, dso);
1303 static int kernel_maps__create_module_maps(void)
1307 FILE *file = fopen("/proc/modules", "r");
1313 while (!feof(file)) {
1314 char name[PATH_MAX];
1320 line_len = getline(&line, &n, file);
1327 line[--line_len] = '\0'; /* \n */
1329 sep = strrchr(line, 'x');
1333 hex2u64(sep + 1, &start);
1335 sep = strchr(line, ' ');
1341 snprintf(name, sizeof(name), "[%s]", line);
1342 dso = dso__new(name);
1345 goto out_delete_line;
1347 map = map__new2(start, dso, MAP__FUNCTION);
1350 goto out_delete_line;
1353 snprintf(name, sizeof(name),
1354 "/sys/module/%s/notes/.note.gnu.build-id", line);
1355 if (sysfs__read_build_id(name, dso->build_id,
1356 sizeof(dso->build_id)) == 0)
1357 dso->has_build_id = true;
1359 dso->origin = DSO__ORIG_KMODULE;
1360 kernel_maps__insert(map);
1361 dsos__add(&dsos__kernel, dso);
1367 return dsos__set_modules_path();
1375 static int dso__load_vmlinux(struct dso *self, struct map *map,
1376 const char *vmlinux, symbol_filter_t filter)
1380 if (self->has_build_id) {
1381 u8 build_id[BUILD_ID_SIZE];
1383 if (filename__read_build_id(vmlinux, build_id,
1384 sizeof(build_id)) < 0) {
1385 pr_debug("No build_id in %s, ignoring it\n", vmlinux);
1388 if (!dso__build_id_equal(self, build_id)) {
1389 char expected_build_id[BUILD_ID_SIZE * 2 + 1],
1390 vmlinux_build_id[BUILD_ID_SIZE * 2 + 1];
1392 build_id__sprintf(self->build_id,
1393 sizeof(self->build_id),
1395 build_id__sprintf(build_id, sizeof(build_id),
1397 pr_debug("build_id in %s is %s while expected is %s, "
1398 "ignoring it\n", vmlinux, vmlinux_build_id,
1404 fd = open(vmlinux, O_RDONLY);
1408 dso__set_loaded(self, map->type);
1409 err = dso__load_sym(self, map, self->long_name, fd, filter, 1, 0);
1416 static int dso__load_kernel_sym(struct dso *self, struct map *map,
1417 symbol_filter_t filter)
1422 if (vmlinux_path != NULL) {
1424 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1425 vmlinux_path__nr_entries);
1426 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
1427 err = dso__load_vmlinux(self, map, vmlinux_path[i],
1430 pr_debug("Using %s for symbols\n",
1432 dso__set_long_name(self,
1433 strdup(vmlinux_path[i]));
1439 is_kallsyms = self->long_name[0] == '[';
1443 err = dso__load_vmlinux(self, map, self->long_name, filter);
1445 pr_info("The file %s cannot be used, "
1446 "trying to use /proc/kallsyms...", self->long_name);
1449 err = kernel_maps__load_kallsyms(filter);
1450 if (err > 0 && !is_kallsyms)
1451 dso__set_long_name(self, strdup("[kernel.kallsyms]"));
1456 map__fixup_start(map, &map->dso->functions);
1457 map__fixup_end(map, &map->dso->functions);
1463 LIST_HEAD(dsos__user);
1464 LIST_HEAD(dsos__kernel);
1467 static void dsos__add(struct list_head *head, struct dso *dso)
1469 list_add_tail(&dso->node, head);
1472 static struct dso *dsos__find(struct list_head *head, const char *name)
1476 list_for_each_entry(pos, head, node)
1477 if (strcmp(pos->name, name) == 0)
1482 struct dso *dsos__findnew(const char *name)
1484 struct dso *dso = dsos__find(&dsos__user, name);
1487 dso = dso__new(name);
1489 dsos__add(&dsos__user, dso);
1490 dso__set_basename(dso);
1497 static void __dsos__fprintf(struct list_head *head, FILE *fp)
1501 list_for_each_entry(pos, head, node)
1502 dso__fprintf(pos, fp);
1505 void dsos__fprintf(FILE *fp)
1507 __dsos__fprintf(&dsos__kernel, fp);
1508 __dsos__fprintf(&dsos__user, fp);
1511 static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp)
1516 list_for_each_entry(pos, head, node) {
1517 ret += dso__fprintf_buildid(pos, fp);
1518 ret += fprintf(fp, " %s\n", pos->long_name);
1523 size_t dsos__fprintf_buildid(FILE *fp)
1525 return (__dsos__fprintf_buildid(&dsos__kernel, fp) +
1526 __dsos__fprintf_buildid(&dsos__user, fp));
1529 static int kernel_maps__create_kernel_map(const struct symbol_conf *conf)
1531 struct dso *kernel = dso__new(conf->vmlinux_name ?: "[kernel.kallsyms]");
1536 kernel_map__functions = map__new2(0, kernel, MAP__FUNCTION);
1537 if (kernel_map__functions == NULL)
1538 goto out_delete_kernel_dso;
1540 kernel_map__functions->map_ip = kernel_map__functions->unmap_ip = identity__map_ip;
1541 kernel->short_name = "[kernel]";
1544 vdso = dso__new("[vdso]");
1546 goto out_delete_kernel_map;
1547 dso__set_loaded(vdso, MAP__FUNCTION);
1549 if (sysfs__read_build_id("/sys/kernel/notes", kernel->build_id,
1550 sizeof(kernel->build_id)) == 0)
1551 kernel->has_build_id = true;
1553 kernel_maps__insert(kernel_map__functions);
1554 dsos__add(&dsos__kernel, kernel);
1555 dsos__add(&dsos__user, vdso);
1559 out_delete_kernel_map:
1560 map__delete(kernel_map__functions);
1561 kernel_map__functions = NULL;
1562 out_delete_kernel_dso:
1563 dso__delete(kernel);
1567 static void vmlinux_path__exit(void)
1569 while (--vmlinux_path__nr_entries >= 0) {
1570 free(vmlinux_path[vmlinux_path__nr_entries]);
1571 vmlinux_path[vmlinux_path__nr_entries] = NULL;
1575 vmlinux_path = NULL;
1578 static int vmlinux_path__init(void)
1583 if (uname(&uts) < 0)
1586 vmlinux_path = malloc(sizeof(char *) * 5);
1587 if (vmlinux_path == NULL)
1590 vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
1591 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1593 ++vmlinux_path__nr_entries;
1594 vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
1595 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1597 ++vmlinux_path__nr_entries;
1598 snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
1599 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1600 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1602 ++vmlinux_path__nr_entries;
1603 snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
1604 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1605 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1607 ++vmlinux_path__nr_entries;
1608 snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
1610 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1611 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1613 ++vmlinux_path__nr_entries;
1618 vmlinux_path__exit();
1622 static int kernel_maps__init(const struct symbol_conf *conf)
1624 const struct symbol_conf *pconf = conf ?: &symbol_conf__defaults;
1626 symbol__priv_size = pconf->priv_size;
1628 if (pconf->try_vmlinux_path && vmlinux_path__init() < 0)
1631 if (kernel_maps__create_kernel_map(pconf) < 0) {
1632 vmlinux_path__exit();
1636 if (pconf->use_modules && kernel_maps__create_module_maps() < 0)
1637 pr_debug("Failed to load list of modules in use, "
1640 * Now that we have all the maps created, just set the ->end of them:
1642 kernel_maps__fixup_end();
1646 int symbol__init(struct symbol_conf *conf)
1648 elf_version(EV_CURRENT);
1649 return kernel_maps__init(conf);