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 struct symbol
*dso__find_symbol(struct dso
*self
, enum map_type type
, u64 addr
);
35 static int dso__load_kernel_sym(struct dso
*self
, struct map
*map
,
36 symbol_filter_t filter
);
37 unsigned int symbol__priv_size
;
38 static int vmlinux_path__nr_entries
;
39 static char **vmlinux_path
;
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);
155 strcpy(self
->name
, name
);
156 dso__set_long_name(self
, self
->name
);
157 self
->short_name
= self
->name
;
158 for (i
= 0; i
< MAP__NR_TYPES
; ++i
)
159 self
->symbols
[i
] = RB_ROOT
;
160 self
->find_symbol
= dso__find_symbol
;
161 self
->slen_calculated
= 0;
162 self
->origin
= DSO__ORIG_NOT_FOUND
;
164 self
->has_build_id
= 0;
170 static void symbols__delete(struct rb_root
*self
)
173 struct rb_node
*next
= rb_first(self
);
176 pos
= rb_entry(next
, struct symbol
, rb_node
);
177 next
= rb_next(&pos
->rb_node
);
178 rb_erase(&pos
->rb_node
, self
);
183 void dso__delete(struct dso
*self
)
186 for (i
= 0; i
< MAP__NR_TYPES
; ++i
)
187 symbols__delete(&self
->symbols
[i
]);
188 if (self
->long_name
!= self
->name
)
189 free(self
->long_name
);
193 void dso__set_build_id(struct dso
*self
, void *build_id
)
195 memcpy(self
->build_id
, build_id
, sizeof(self
->build_id
));
196 self
->has_build_id
= 1;
199 static void symbols__insert(struct rb_root
*self
, struct symbol
*sym
)
201 struct rb_node
**p
= &self
->rb_node
;
202 struct rb_node
*parent
= NULL
;
203 const u64 ip
= sym
->start
;
208 s
= rb_entry(parent
, struct symbol
, rb_node
);
214 rb_link_node(&sym
->rb_node
, parent
, p
);
215 rb_insert_color(&sym
->rb_node
, self
);
218 static struct symbol
*symbols__find(struct rb_root
*self
, u64 ip
)
228 struct symbol
*s
= rb_entry(n
, struct symbol
, rb_node
);
232 else if (ip
> s
->end
)
241 struct symbol
*dso__find_symbol(struct dso
*self
, enum map_type type
, u64 addr
)
243 return symbols__find(&self
->symbols
[type
], addr
);
246 int build_id__sprintf(u8
*self
, int len
, char *bf
)
252 for (i
= 0; i
< len
; ++i
) {
253 sprintf(bid
, "%02x", *raw
);
261 size_t dso__fprintf_buildid(struct dso
*self
, FILE *fp
)
263 char sbuild_id
[BUILD_ID_SIZE
* 2 + 1];
265 build_id__sprintf(self
->build_id
, sizeof(self
->build_id
), sbuild_id
);
266 return fprintf(fp
, "%s", sbuild_id
);
269 static const char * map_type__name
[MAP__NR_TYPES
] = {
270 [MAP__FUNCTION
] = "Functions",
273 size_t dso__fprintf(struct dso
*self
, FILE *fp
)
277 size_t ret
= fprintf(fp
, "dso: %s (", self
->short_name
);
279 ret
+= dso__fprintf_buildid(self
, fp
);
280 ret
+= fprintf(fp
, ")\n");
281 for (i
= 0; i
< MAP__NR_TYPES
; ++i
) {
282 ret
+= fprintf(fp
, "%s:\n", map_type__name
[i
]);
284 for (nd
= rb_first(&self
->symbols
[i
]); nd
; nd
= rb_next(nd
)) {
285 struct symbol
*pos
= rb_entry(nd
, struct symbol
, rb_node
);
286 ret
+= symbol__fprintf(pos
, fp
);
294 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
295 * so that we can in the next step set the symbol ->end address and then
296 * call kernel_maps__split_kallsyms.
298 static int dso__load_all_kallsyms(struct dso
*self
, struct map
*map
)
302 struct rb_root
*root
= &self
->symbols
[map
->type
];
303 FILE *file
= fopen("/proc/kallsyms", "r");
308 while (!feof(file
)) {
315 line_len
= getline(&line
, &n
, file
);
322 line
[--line_len
] = '\0'; /* \n */
324 len
= hex2u64(line
, &start
);
327 if (len
+ 2 >= line_len
)
330 symbol_type
= toupper(line
[len
]);
332 * We're interested only in code ('T'ext)
334 if (symbol_type
!= 'T' && symbol_type
!= 'W')
337 symbol_name
= line
+ len
+ 2;
339 * Will fix up the end later, when we have all symbols sorted.
341 sym
= symbol__new(start
, 0, symbol_name
);
344 goto out_delete_line
;
346 * We will pass the symbols to the filter later, in
347 * map__split_kallsyms, when we have split the maps per module
349 symbols__insert(root
, sym
);
364 * Split the symbols into maps, making sure there are no overlaps, i.e. the
365 * kernel range is broken in several maps, named [kernel].N, as we don't have
366 * the original ELF section names vmlinux have.
368 static int dso__split_kallsyms(struct dso
*self
, struct map
*map
,
369 symbol_filter_t filter
)
371 struct map
*curr_map
= map
;
374 struct rb_root
*root
= &self
->symbols
[map
->type
];
375 struct rb_node
*next
= rb_first(root
);
376 int kernel_range
= 0;
381 pos
= rb_entry(next
, struct symbol
, rb_node
);
382 next
= rb_next(&pos
->rb_node
);
384 module
= strchr(pos
->name
, '\t');
388 if (strcmp(self
->name
, module
)) {
389 curr_map
= kernel_maps__find_by_dso_name(module
);
390 if (curr_map
== NULL
) {
391 pr_err("/proc/{kallsyms,modules} "
397 * So that we look just like we get from .ko files,
398 * i.e. not prelinked, relative to map->start.
400 pos
->start
= curr_map
->map_ip(curr_map
, pos
->start
);
401 pos
->end
= curr_map
->map_ip(curr_map
, pos
->end
);
402 } else if (curr_map
!= map
) {
403 char dso_name
[PATH_MAX
];
406 snprintf(dso_name
, sizeof(dso_name
), "[kernel].%d",
409 dso
= dso__new(dso_name
);
413 curr_map
= map__new2(pos
->start
, dso
, map
->type
);
419 curr_map
->map_ip
= curr_map
->unmap_ip
= identity__map_ip
;
420 kernel_maps__insert(curr_map
);
424 if (filter
&& filter(curr_map
, pos
)) {
425 rb_erase(&pos
->rb_node
, root
);
428 if (curr_map
!= map
) {
429 rb_erase(&pos
->rb_node
, root
);
430 symbols__insert(&curr_map
->dso
->symbols
[curr_map
->type
], pos
);
440 static int dso__load_kallsyms(struct dso
*self
, struct map
*map
,
441 symbol_filter_t filter
)
443 if (dso__load_all_kallsyms(self
, map
) < 0)
446 symbols__fixup_end(&self
->symbols
[map
->type
]);
447 self
->origin
= DSO__ORIG_KERNEL
;
449 return dso__split_kallsyms(self
, map
, filter
);
452 size_t kernel_maps__fprintf(FILE *fp
)
454 size_t printed
= fprintf(fp
, "Kernel maps:\n");
457 for (nd
= rb_first(&kernel_maps__functions
); nd
; nd
= rb_next(nd
)) {
458 struct map
*pos
= rb_entry(nd
, struct map
, rb_node
);
460 printed
+= fprintf(fp
, "Map:");
461 printed
+= map__fprintf(pos
, fp
);
463 printed
+= dso__fprintf(pos
->dso
, fp
);
464 printed
+= fprintf(fp
, "--\n");
468 return printed
+ fprintf(fp
, "END kernel maps\n");
471 static int dso__load_perf_map(struct dso
*self
, struct map
*map
,
472 symbol_filter_t filter
)
479 file
= fopen(self
->long_name
, "r");
483 while (!feof(file
)) {
488 line_len
= getline(&line
, &n
, file
);
495 line
[--line_len
] = '\0'; /* \n */
497 len
= hex2u64(line
, &start
);
500 if (len
+ 2 >= line_len
)
503 len
+= hex2u64(line
+ len
, &size
);
506 if (len
+ 2 >= line_len
)
509 sym
= symbol__new(start
, size
, line
+ len
);
512 goto out_delete_line
;
514 if (filter
&& filter(map
, sym
))
517 symbols__insert(&self
->symbols
[map
->type
], sym
);
534 * elf_symtab__for_each_symbol - iterate thru all the symbols
536 * @self: struct elf_symtab instance to iterate
538 * @sym: GElf_Sym iterator
540 #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
541 for (idx = 0, gelf_getsym(syms, idx, &sym);\
543 idx++, gelf_getsym(syms, idx, &sym))
545 static inline uint8_t elf_sym__type(const GElf_Sym
*sym
)
547 return GELF_ST_TYPE(sym
->st_info
);
550 static inline int elf_sym__is_function(const GElf_Sym
*sym
)
552 return elf_sym__type(sym
) == STT_FUNC
&&
554 sym
->st_shndx
!= SHN_UNDEF
;
557 static inline int elf_sym__is_label(const GElf_Sym
*sym
)
559 return elf_sym__type(sym
) == STT_NOTYPE
&&
561 sym
->st_shndx
!= SHN_UNDEF
&&
562 sym
->st_shndx
!= SHN_ABS
;
565 static inline const char *elf_sec__name(const GElf_Shdr
*shdr
,
566 const Elf_Data
*secstrs
)
568 return secstrs
->d_buf
+ shdr
->sh_name
;
571 static inline int elf_sec__is_text(const GElf_Shdr
*shdr
,
572 const Elf_Data
*secstrs
)
574 return strstr(elf_sec__name(shdr
, secstrs
), "text") != NULL
;
577 static inline const char *elf_sym__name(const GElf_Sym
*sym
,
578 const Elf_Data
*symstrs
)
580 return symstrs
->d_buf
+ sym
->st_name
;
583 static Elf_Scn
*elf_section_by_name(Elf
*elf
, GElf_Ehdr
*ep
,
584 GElf_Shdr
*shp
, const char *name
,
590 while ((sec
= elf_nextscn(elf
, sec
)) != NULL
) {
593 gelf_getshdr(sec
, shp
);
594 str
= elf_strptr(elf
, ep
->e_shstrndx
, shp
->sh_name
);
595 if (!strcmp(name
, str
)) {
606 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
607 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
609 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
611 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
612 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
614 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
617 * We need to check if we have a .dynsym, so that we can handle the
618 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
619 * .dynsym or .symtab).
620 * And always look at the original dso, not at debuginfo packages, that
621 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
623 static int dso__synthesize_plt_symbols(struct dso
*self
, struct map
*map
,
624 symbol_filter_t filter
)
626 uint32_t nr_rel_entries
, idx
;
631 GElf_Shdr shdr_rel_plt
, shdr_dynsym
;
632 Elf_Data
*reldata
, *syms
, *symstrs
;
633 Elf_Scn
*scn_plt_rel
, *scn_symstrs
, *scn_dynsym
;
636 char sympltname
[1024];
638 int nr
= 0, symidx
, fd
, err
= 0;
640 fd
= open(self
->long_name
, O_RDONLY
);
644 elf
= elf_begin(fd
, PERF_ELF_C_READ_MMAP
, NULL
);
648 if (gelf_getehdr(elf
, &ehdr
) == NULL
)
651 scn_dynsym
= elf_section_by_name(elf
, &ehdr
, &shdr_dynsym
,
652 ".dynsym", &dynsym_idx
);
653 if (scn_dynsym
== NULL
)
656 scn_plt_rel
= elf_section_by_name(elf
, &ehdr
, &shdr_rel_plt
,
658 if (scn_plt_rel
== NULL
) {
659 scn_plt_rel
= elf_section_by_name(elf
, &ehdr
, &shdr_rel_plt
,
661 if (scn_plt_rel
== NULL
)
667 if (shdr_rel_plt
.sh_link
!= dynsym_idx
)
670 if (elf_section_by_name(elf
, &ehdr
, &shdr_plt
, ".plt", NULL
) == NULL
)
674 * Fetch the relocation section to find the idxes to the GOT
675 * and the symbols in the .dynsym they refer to.
677 reldata
= elf_getdata(scn_plt_rel
, NULL
);
681 syms
= elf_getdata(scn_dynsym
, NULL
);
685 scn_symstrs
= elf_getscn(elf
, shdr_dynsym
.sh_link
);
686 if (scn_symstrs
== NULL
)
689 symstrs
= elf_getdata(scn_symstrs
, NULL
);
693 nr_rel_entries
= shdr_rel_plt
.sh_size
/ shdr_rel_plt
.sh_entsize
;
694 plt_offset
= shdr_plt
.sh_offset
;
696 if (shdr_rel_plt
.sh_type
== SHT_RELA
) {
697 GElf_Rela pos_mem
, *pos
;
699 elf_section__for_each_rela(reldata
, pos
, pos_mem
, idx
,
701 symidx
= GELF_R_SYM(pos
->r_info
);
702 plt_offset
+= shdr_plt
.sh_entsize
;
703 gelf_getsym(syms
, symidx
, &sym
);
704 snprintf(sympltname
, sizeof(sympltname
),
705 "%s@plt", elf_sym__name(&sym
, symstrs
));
707 f
= symbol__new(plt_offset
, shdr_plt
.sh_entsize
,
712 if (filter
&& filter(map
, f
))
715 symbols__insert(&self
->symbols
[map
->type
], f
);
719 } else if (shdr_rel_plt
.sh_type
== SHT_REL
) {
720 GElf_Rel pos_mem
, *pos
;
721 elf_section__for_each_rel(reldata
, pos
, pos_mem
, idx
,
723 symidx
= GELF_R_SYM(pos
->r_info
);
724 plt_offset
+= shdr_plt
.sh_entsize
;
725 gelf_getsym(syms
, symidx
, &sym
);
726 snprintf(sympltname
, sizeof(sympltname
),
727 "%s@plt", elf_sym__name(&sym
, symstrs
));
729 f
= symbol__new(plt_offset
, shdr_plt
.sh_entsize
,
734 if (filter
&& filter(map
, f
))
737 symbols__insert(&self
->symbols
[map
->type
], f
);
752 pr_warning("%s: problems reading %s PLT info.\n",
753 __func__
, self
->long_name
);
757 static int dso__load_sym(struct dso
*self
, struct map
*map
, const char *name
,
758 int fd
, symbol_filter_t filter
, int kernel
,
761 struct map
*curr_map
= map
;
762 struct dso
*curr_dso
= self
;
763 size_t dso_name_len
= strlen(self
->short_name
);
764 Elf_Data
*symstrs
, *secstrs
;
772 Elf_Scn
*sec
, *sec_strndx
;
776 elf
= elf_begin(fd
, PERF_ELF_C_READ_MMAP
, NULL
);
778 pr_err("%s: cannot read %s ELF file.\n", __func__
, name
);
782 if (gelf_getehdr(elf
, &ehdr
) == NULL
) {
783 pr_err("%s: cannot get elf header.\n", __func__
);
787 sec
= elf_section_by_name(elf
, &ehdr
, &shdr
, ".symtab", NULL
);
789 sec
= elf_section_by_name(elf
, &ehdr
, &shdr
, ".dynsym", NULL
);
794 syms
= elf_getdata(sec
, NULL
);
798 sec
= elf_getscn(elf
, shdr
.sh_link
);
802 symstrs
= elf_getdata(sec
, NULL
);
806 sec_strndx
= elf_getscn(elf
, ehdr
.e_shstrndx
);
807 if (sec_strndx
== NULL
)
810 secstrs
= elf_getdata(sec_strndx
, NULL
);
814 nr_syms
= shdr
.sh_size
/ shdr
.sh_entsize
;
816 memset(&sym
, 0, sizeof(sym
));
818 self
->adjust_symbols
= (ehdr
.e_type
== ET_EXEC
||
819 elf_section_by_name(elf
, &ehdr
, &shdr
,
822 } else self
->adjust_symbols
= 0;
824 elf_symtab__for_each_symbol(syms
, nr_syms
, idx
, sym
) {
826 const char *elf_name
;
827 char *demangled
= NULL
;
828 int is_label
= elf_sym__is_label(&sym
);
829 const char *section_name
;
831 if (!is_label
&& !elf_sym__is_function(&sym
))
834 sec
= elf_getscn(elf
, sym
.st_shndx
);
838 gelf_getshdr(sec
, &shdr
);
840 if (is_label
&& !elf_sec__is_text(&shdr
, secstrs
))
843 elf_name
= elf_sym__name(&sym
, symstrs
);
844 section_name
= elf_sec__name(&shdr
, secstrs
);
846 if (kernel
|| kmodule
) {
847 char dso_name
[PATH_MAX
];
849 if (strcmp(section_name
,
850 curr_dso
->short_name
+ dso_name_len
) == 0)
853 if (strcmp(section_name
, ".text") == 0) {
859 snprintf(dso_name
, sizeof(dso_name
),
860 "%s%s", self
->short_name
, section_name
);
862 curr_map
= kernel_maps__find_by_dso_name(dso_name
);
863 if (curr_map
== NULL
) {
864 u64 start
= sym
.st_value
;
867 start
+= map
->start
+ shdr
.sh_offset
;
869 curr_dso
= dso__new(dso_name
);
870 if (curr_dso
== NULL
)
872 curr_map
= map__new2(start
, curr_dso
,
874 if (curr_map
== NULL
) {
875 dso__delete(curr_dso
);
878 curr_map
->map_ip
= identity__map_ip
;
879 curr_map
->unmap_ip
= identity__map_ip
;
880 curr_dso
->origin
= DSO__ORIG_KERNEL
;
881 kernel_maps__insert(curr_map
);
882 dsos__add(&dsos__kernel
, curr_dso
);
884 curr_dso
= curr_map
->dso
;
889 if (curr_dso
->adjust_symbols
) {
890 pr_debug2("adjusting symbol: st_value: %Lx sh_addr: "
891 "%Lx sh_offset: %Lx\n", (u64
)sym
.st_value
,
892 (u64
)shdr
.sh_addr
, (u64
)shdr
.sh_offset
);
893 sym
.st_value
-= shdr
.sh_addr
- shdr
.sh_offset
;
896 * We need to figure out if the object was created from C++ sources
897 * DWARF DW_compile_unit has this, but we don't always have access
900 demangled
= bfd_demangle(NULL
, elf_name
, DMGL_PARAMS
| DMGL_ANSI
);
901 if (demangled
!= NULL
)
902 elf_name
= demangled
;
904 f
= symbol__new(sym
.st_value
, sym
.st_size
, elf_name
);
909 if (filter
&& filter(curr_map
, f
))
912 symbols__insert(&curr_dso
->symbols
[curr_map
->type
], f
);
918 * For misannotated, zeroed, ASM function sizes.
921 symbols__fixup_end(&self
->symbols
[map
->type
]);
929 static bool dso__build_id_equal(const struct dso
*self
, u8
*build_id
)
931 return memcmp(self
->build_id
, build_id
, sizeof(self
->build_id
)) == 0;
934 static bool __dsos__read_build_ids(struct list_head
*head
)
936 bool have_build_id
= false;
939 list_for_each_entry(pos
, head
, node
)
940 if (filename__read_build_id(pos
->long_name
, pos
->build_id
,
941 sizeof(pos
->build_id
)) > 0) {
942 have_build_id
= true;
943 pos
->has_build_id
= true;
946 return have_build_id
;
949 bool dsos__read_build_ids(void)
951 return __dsos__read_build_ids(&dsos__kernel
) ||
952 __dsos__read_build_ids(&dsos__user
);
956 * Align offset to 4 bytes as needed for note name and descriptor data.
958 #define NOTE_ALIGN(n) (((n) + 3) & -4U)
960 int filename__read_build_id(const char *filename
, void *bf
, size_t size
)
971 if (size
< BUILD_ID_SIZE
)
974 fd
= open(filename
, O_RDONLY
);
978 elf
= elf_begin(fd
, PERF_ELF_C_READ_MMAP
, NULL
);
980 pr_debug2("%s: cannot read %s ELF file.\n", __func__
, filename
);
988 if (gelf_getehdr(elf
, &ehdr
) == NULL
) {
989 pr_err("%s: cannot get elf header.\n", __func__
);
993 sec
= elf_section_by_name(elf
, &ehdr
, &shdr
,
994 ".note.gnu.build-id", NULL
);
996 sec
= elf_section_by_name(elf
, &ehdr
, &shdr
,
1002 data
= elf_getdata(sec
, NULL
);
1007 while (ptr
< (data
->d_buf
+ data
->d_size
)) {
1008 GElf_Nhdr
*nhdr
= ptr
;
1009 int namesz
= NOTE_ALIGN(nhdr
->n_namesz
),
1010 descsz
= NOTE_ALIGN(nhdr
->n_descsz
);
1013 ptr
+= sizeof(*nhdr
);
1016 if (nhdr
->n_type
== NT_GNU_BUILD_ID
&&
1017 nhdr
->n_namesz
== sizeof("GNU")) {
1018 if (memcmp(name
, "GNU", sizeof("GNU")) == 0) {
1019 memcpy(bf
, ptr
, BUILD_ID_SIZE
);
1020 err
= BUILD_ID_SIZE
;
1034 int sysfs__read_build_id(const char *filename
, void *build_id
, size_t size
)
1038 if (size
< BUILD_ID_SIZE
)
1041 fd
= open(filename
, O_RDONLY
);
1050 if (read(fd
, &nhdr
, sizeof(nhdr
)) != sizeof(nhdr
))
1053 namesz
= NOTE_ALIGN(nhdr
.n_namesz
);
1054 descsz
= NOTE_ALIGN(nhdr
.n_descsz
);
1055 if (nhdr
.n_type
== NT_GNU_BUILD_ID
&&
1056 nhdr
.n_namesz
== sizeof("GNU")) {
1057 if (read(fd
, bf
, namesz
) != namesz
)
1059 if (memcmp(bf
, "GNU", sizeof("GNU")) == 0) {
1060 if (read(fd
, build_id
,
1061 BUILD_ID_SIZE
) == BUILD_ID_SIZE
) {
1065 } else if (read(fd
, bf
, descsz
) != descsz
)
1068 int n
= namesz
+ descsz
;
1069 if (read(fd
, bf
, n
) != n
)
1078 char dso__symtab_origin(const struct dso
*self
)
1080 static const char origin
[] = {
1081 [DSO__ORIG_KERNEL
] = 'k',
1082 [DSO__ORIG_JAVA_JIT
] = 'j',
1083 [DSO__ORIG_FEDORA
] = 'f',
1084 [DSO__ORIG_UBUNTU
] = 'u',
1085 [DSO__ORIG_BUILDID
] = 'b',
1086 [DSO__ORIG_DSO
] = 'd',
1087 [DSO__ORIG_KMODULE
] = 'K',
1090 if (self
== NULL
|| self
->origin
== DSO__ORIG_NOT_FOUND
)
1092 return origin
[self
->origin
];
1095 int dso__load(struct dso
*self
, struct map
*map
, symbol_filter_t filter
)
1097 int size
= PATH_MAX
;
1099 u8 build_id
[BUILD_ID_SIZE
];
1103 dso__set_loaded(self
, map
->type
);
1106 return dso__load_kernel_sym(self
, map
, filter
);
1108 name
= malloc(size
);
1112 self
->adjust_symbols
= 0;
1114 if (strncmp(self
->name
, "/tmp/perf-", 10) == 0) {
1115 ret
= dso__load_perf_map(self
, map
, filter
);
1116 self
->origin
= ret
> 0 ? DSO__ORIG_JAVA_JIT
:
1117 DSO__ORIG_NOT_FOUND
;
1121 self
->origin
= DSO__ORIG_FEDORA
- 1;
1126 switch (self
->origin
) {
1127 case DSO__ORIG_FEDORA
:
1128 snprintf(name
, size
, "/usr/lib/debug%s.debug",
1131 case DSO__ORIG_UBUNTU
:
1132 snprintf(name
, size
, "/usr/lib/debug%s",
1135 case DSO__ORIG_BUILDID
:
1136 if (filename__read_build_id(self
->long_name
, build_id
,
1137 sizeof(build_id
))) {
1138 char build_id_hex
[BUILD_ID_SIZE
* 2 + 1];
1140 build_id__sprintf(build_id
, sizeof(build_id
),
1142 snprintf(name
, size
,
1143 "/usr/lib/debug/.build-id/%.2s/%s.debug",
1144 build_id_hex
, build_id_hex
+ 2);
1145 if (self
->has_build_id
)
1146 goto compare_build_id
;
1152 snprintf(name
, size
, "%s", self
->long_name
);
1159 if (self
->has_build_id
) {
1160 if (filename__read_build_id(name
, build_id
,
1161 sizeof(build_id
)) < 0)
1164 if (!dso__build_id_equal(self
, build_id
))
1168 fd
= open(name
, O_RDONLY
);
1171 ret
= dso__load_sym(self
, map
, name
, fd
, filter
, 0, 0);
1175 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
1181 int nr_plt
= dso__synthesize_plt_symbols(self
, map
, filter
);
1187 if (ret
< 0 && strstr(self
->name
, " (deleted)") != NULL
)
1192 static void kernel_maps__insert(struct map
*map
)
1194 maps__insert(&kernel_maps__functions
, map
);
1197 struct symbol
*kernel_maps__find_function(u64 ip
, struct map
**mapp
,
1198 symbol_filter_t filter
)
1200 struct map
*map
= maps__find(&kernel_maps__functions
, ip
);
1206 ip
= map
->map_ip(map
, ip
);
1207 return map__find_symbol(map
, ip
, filter
);
1209 WARN_ONCE(RB_EMPTY_ROOT(&kernel_maps__functions
),
1210 "Empty kernel_maps, was symbol__init() called?\n");
1215 struct map
*kernel_maps__find_by_dso_name(const char *name
)
1219 for (nd
= rb_first(&kernel_maps__functions
); nd
; nd
= rb_next(nd
)) {
1220 struct map
*map
= rb_entry(nd
, struct map
, rb_node
);
1222 if (map
->dso
&& strcmp(map
->dso
->name
, name
) == 0)
1229 static int dsos__set_modules_path_dir(char *dirname
)
1231 struct dirent
*dent
;
1232 DIR *dir
= opendir(dirname
);
1235 pr_debug("%s: cannot open %s dir\n", __func__
, dirname
);
1239 while ((dent
= readdir(dir
)) != NULL
) {
1240 char path
[PATH_MAX
];
1242 if (dent
->d_type
== DT_DIR
) {
1243 if (!strcmp(dent
->d_name
, ".") ||
1244 !strcmp(dent
->d_name
, ".."))
1247 snprintf(path
, sizeof(path
), "%s/%s",
1248 dirname
, dent
->d_name
);
1249 if (dsos__set_modules_path_dir(path
) < 0)
1252 char *dot
= strrchr(dent
->d_name
, '.'),
1257 if (dot
== NULL
|| strcmp(dot
, ".ko"))
1259 snprintf(dso_name
, sizeof(dso_name
), "[%.*s]",
1260 (int)(dot
- dent
->d_name
), dent
->d_name
);
1262 strxfrchar(dso_name
, '-', '_');
1263 map
= kernel_maps__find_by_dso_name(dso_name
);
1267 snprintf(path
, sizeof(path
), "%s/%s",
1268 dirname
, dent
->d_name
);
1270 long_name
= strdup(path
);
1271 if (long_name
== NULL
)
1273 dso__set_long_name(map
->dso
, long_name
);
1283 static int dsos__set_modules_path(void)
1286 char modules_path
[PATH_MAX
];
1288 if (uname(&uts
) < 0)
1291 snprintf(modules_path
, sizeof(modules_path
), "/lib/modules/%s/kernel",
1294 return dsos__set_modules_path_dir(modules_path
);
1298 * Constructor variant for modules (where we know from /proc/modules where
1299 * they are loaded) and for vmlinux, where only after we load all the
1300 * symbols we'll know where it starts and ends.
1302 static struct map
*map__new2(u64 start
, struct dso
*dso
, enum map_type type
)
1304 struct map
*self
= malloc(sizeof(*self
));
1308 * ->end will be filled after we load all the symbols
1310 map__init(self
, type
, start
, 0, 0, dso
);
1316 static int kernel_maps__create_module_maps(void)
1320 FILE *file
= fopen("/proc/modules", "r");
1326 while (!feof(file
)) {
1327 char name
[PATH_MAX
];
1333 line_len
= getline(&line
, &n
, file
);
1340 line
[--line_len
] = '\0'; /* \n */
1342 sep
= strrchr(line
, 'x');
1346 hex2u64(sep
+ 1, &start
);
1348 sep
= strchr(line
, ' ');
1354 snprintf(name
, sizeof(name
), "[%s]", line
);
1355 dso
= dso__new(name
);
1358 goto out_delete_line
;
1360 map
= map__new2(start
, dso
, MAP__FUNCTION
);
1363 goto out_delete_line
;
1366 snprintf(name
, sizeof(name
),
1367 "/sys/module/%s/notes/.note.gnu.build-id", line
);
1368 if (sysfs__read_build_id(name
, dso
->build_id
,
1369 sizeof(dso
->build_id
)) == 0)
1370 dso
->has_build_id
= true;
1372 dso
->origin
= DSO__ORIG_KMODULE
;
1373 kernel_maps__insert(map
);
1374 dsos__add(&dsos__kernel
, dso
);
1380 return dsos__set_modules_path();
1388 static int dso__load_vmlinux(struct dso
*self
, struct map
*map
,
1389 const char *vmlinux
, symbol_filter_t filter
)
1393 if (self
->has_build_id
) {
1394 u8 build_id
[BUILD_ID_SIZE
];
1396 if (filename__read_build_id(vmlinux
, build_id
,
1397 sizeof(build_id
)) < 0) {
1398 pr_debug("No build_id in %s, ignoring it\n", vmlinux
);
1401 if (!dso__build_id_equal(self
, build_id
)) {
1402 char expected_build_id
[BUILD_ID_SIZE
* 2 + 1],
1403 vmlinux_build_id
[BUILD_ID_SIZE
* 2 + 1];
1405 build_id__sprintf(self
->build_id
,
1406 sizeof(self
->build_id
),
1408 build_id__sprintf(build_id
, sizeof(build_id
),
1410 pr_debug("build_id in %s is %s while expected is %s, "
1411 "ignoring it\n", vmlinux
, vmlinux_build_id
,
1417 fd
= open(vmlinux
, O_RDONLY
);
1421 dso__set_loaded(self
, map
->type
);
1422 err
= dso__load_sym(self
, map
, self
->long_name
, fd
, filter
, 1, 0);
1429 static int dso__load_kernel_sym(struct dso
*self
, struct map
*map
,
1430 symbol_filter_t filter
)
1435 if (vmlinux_path
!= NULL
) {
1437 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1438 vmlinux_path__nr_entries
);
1439 for (i
= 0; i
< vmlinux_path__nr_entries
; ++i
) {
1440 err
= dso__load_vmlinux(self
, map
, vmlinux_path
[i
],
1443 pr_debug("Using %s for symbols\n",
1445 dso__set_long_name(self
,
1446 strdup(vmlinux_path
[i
]));
1452 is_kallsyms
= self
->long_name
[0] == '[';
1456 err
= dso__load_vmlinux(self
, map
, self
->long_name
, filter
);
1458 pr_info("The file %s cannot be used, "
1459 "trying to use /proc/kallsyms...", self
->long_name
);
1461 err
= dso__load_kallsyms(self
, map
, filter
);
1462 if (err
> 0 && !is_kallsyms
)
1463 dso__set_long_name(self
, strdup("[kernel.kallsyms]"));
1468 map__fixup_start(map
);
1469 map__fixup_end(map
);
1475 LIST_HEAD(dsos__user
);
1476 LIST_HEAD(dsos__kernel
);
1479 static void dsos__add(struct list_head
*head
, struct dso
*dso
)
1481 list_add_tail(&dso
->node
, head
);
1484 static struct dso
*dsos__find(struct list_head
*head
, const char *name
)
1488 list_for_each_entry(pos
, head
, node
)
1489 if (strcmp(pos
->name
, name
) == 0)
1494 struct dso
*dsos__findnew(const char *name
)
1496 struct dso
*dso
= dsos__find(&dsos__user
, name
);
1499 dso
= dso__new(name
);
1501 dsos__add(&dsos__user
, dso
);
1502 dso__set_basename(dso
);
1509 static void __dsos__fprintf(struct list_head
*head
, FILE *fp
)
1513 list_for_each_entry(pos
, head
, node
)
1514 dso__fprintf(pos
, fp
);
1517 void dsos__fprintf(FILE *fp
)
1519 __dsos__fprintf(&dsos__kernel
, fp
);
1520 __dsos__fprintf(&dsos__user
, fp
);
1523 static size_t __dsos__fprintf_buildid(struct list_head
*head
, FILE *fp
)
1528 list_for_each_entry(pos
, head
, node
) {
1529 ret
+= dso__fprintf_buildid(pos
, fp
);
1530 ret
+= fprintf(fp
, " %s\n", pos
->long_name
);
1535 size_t dsos__fprintf_buildid(FILE *fp
)
1537 return (__dsos__fprintf_buildid(&dsos__kernel
, fp
) +
1538 __dsos__fprintf_buildid(&dsos__user
, fp
));
1541 static int kernel_maps__create_kernel_map(const struct symbol_conf
*conf
)
1544 struct dso
*kernel
= dso__new(conf
->vmlinux_name
?: "[kernel.kallsyms]");
1549 kmap
= map__new2(0, kernel
, MAP__FUNCTION
);
1551 goto out_delete_kernel_dso
;
1553 kmap
->map_ip
= kmap
->unmap_ip
= identity__map_ip
;
1554 kernel
->short_name
= "[kernel]";
1557 vdso
= dso__new("[vdso]");
1559 goto out_delete_kernel_map
;
1560 dso__set_loaded(vdso
, MAP__FUNCTION
);
1562 if (sysfs__read_build_id("/sys/kernel/notes", kernel
->build_id
,
1563 sizeof(kernel
->build_id
)) == 0)
1564 kernel
->has_build_id
= true;
1566 kernel_maps__insert(kmap
);
1567 dsos__add(&dsos__kernel
, kernel
);
1568 dsos__add(&dsos__user
, vdso
);
1572 out_delete_kernel_map
:
1574 out_delete_kernel_dso
:
1575 dso__delete(kernel
);
1579 static void vmlinux_path__exit(void)
1581 while (--vmlinux_path__nr_entries
>= 0) {
1582 free(vmlinux_path
[vmlinux_path__nr_entries
]);
1583 vmlinux_path
[vmlinux_path__nr_entries
] = NULL
;
1587 vmlinux_path
= NULL
;
1590 static int vmlinux_path__init(void)
1595 if (uname(&uts
) < 0)
1598 vmlinux_path
= malloc(sizeof(char *) * 5);
1599 if (vmlinux_path
== NULL
)
1602 vmlinux_path
[vmlinux_path__nr_entries
] = strdup("vmlinux");
1603 if (vmlinux_path
[vmlinux_path__nr_entries
] == NULL
)
1605 ++vmlinux_path__nr_entries
;
1606 vmlinux_path
[vmlinux_path__nr_entries
] = strdup("/boot/vmlinux");
1607 if (vmlinux_path
[vmlinux_path__nr_entries
] == NULL
)
1609 ++vmlinux_path__nr_entries
;
1610 snprintf(bf
, sizeof(bf
), "/boot/vmlinux-%s", uts
.release
);
1611 vmlinux_path
[vmlinux_path__nr_entries
] = strdup(bf
);
1612 if (vmlinux_path
[vmlinux_path__nr_entries
] == NULL
)
1614 ++vmlinux_path__nr_entries
;
1615 snprintf(bf
, sizeof(bf
), "/lib/modules/%s/build/vmlinux", uts
.release
);
1616 vmlinux_path
[vmlinux_path__nr_entries
] = strdup(bf
);
1617 if (vmlinux_path
[vmlinux_path__nr_entries
] == NULL
)
1619 ++vmlinux_path__nr_entries
;
1620 snprintf(bf
, sizeof(bf
), "/usr/lib/debug/lib/modules/%s/vmlinux",
1622 vmlinux_path
[vmlinux_path__nr_entries
] = strdup(bf
);
1623 if (vmlinux_path
[vmlinux_path__nr_entries
] == NULL
)
1625 ++vmlinux_path__nr_entries
;
1630 vmlinux_path__exit();
1634 static int kernel_maps__init(const struct symbol_conf
*conf
)
1636 const struct symbol_conf
*pconf
= conf
?: &symbol_conf__defaults
;
1638 symbol__priv_size
= pconf
->priv_size
;
1640 if (pconf
->try_vmlinux_path
&& vmlinux_path__init() < 0)
1643 if (kernel_maps__create_kernel_map(pconf
) < 0) {
1644 vmlinux_path__exit();
1648 if (pconf
->use_modules
&& kernel_maps__create_module_maps() < 0)
1649 pr_debug("Failed to load list of modules in use, "
1652 * Now that we have all the maps created, just set the ->end of them:
1654 kernel_maps__fixup_end();
1658 int symbol__init(struct symbol_conf
*conf
)
1660 elf_version(EV_CURRENT
);
1661 return kernel_maps__init(conf
);