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
);
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
;
40 static struct symbol_conf symbol_conf__defaults
= {
42 .try_vmlinux_path
= true,
45 static struct rb_root kernel_maps__functions
;
47 static void symbols__fixup_end(struct rb_root
*self
)
49 struct rb_node
*nd
, *prevnd
= rb_first(self
);
50 struct symbol
*curr
, *prev
;
55 curr
= rb_entry(prevnd
, struct symbol
, rb_node
);
57 for (nd
= rb_next(prevnd
); nd
; nd
= rb_next(nd
)) {
59 curr
= rb_entry(nd
, struct symbol
, rb_node
);
61 if (prev
->end
== prev
->start
)
62 prev
->end
= curr
->start
- 1;
66 if (curr
->end
== curr
->start
)
67 curr
->end
= roundup(curr
->start
, 4096);
70 static void kernel_maps__fixup_end(void)
72 struct map
*prev
, *curr
;
73 struct rb_node
*nd
, *prevnd
= rb_first(&kernel_maps__functions
);
78 curr
= rb_entry(prevnd
, struct map
, rb_node
);
80 for (nd
= rb_next(prevnd
); nd
; nd
= rb_next(nd
)) {
82 curr
= rb_entry(nd
, struct map
, rb_node
);
83 prev
->end
= curr
->start
- 1;
87 * We still haven't the actual symbols, so guess the
88 * last map final address.
93 static struct symbol
*symbol__new(u64 start
, u64 len
, const char *name
)
95 size_t namelen
= strlen(name
) + 1;
96 struct symbol
*self
= zalloc(symbol__priv_size
+
97 sizeof(*self
) + namelen
);
101 if (symbol__priv_size
)
102 self
= ((void *)self
) + symbol__priv_size
;
105 self
->end
= len
? start
+ len
- 1 : start
;
107 pr_debug3("%s: %s %#Lx-%#Lx\n", __func__
, name
, start
, self
->end
);
109 memcpy(self
->name
, name
, namelen
);
114 static void symbol__delete(struct symbol
*self
)
116 free(((void *)self
) - symbol__priv_size
);
119 static size_t symbol__fprintf(struct symbol
*self
, FILE *fp
)
121 return fprintf(fp
, " %llx-%llx %s\n",
122 self
->start
, self
->end
, self
->name
);
125 static void dso__set_long_name(struct dso
*self
, char *name
)
129 self
->long_name
= name
;
130 self
->long_name_len
= strlen(name
);
133 static void dso__set_basename(struct dso
*self
)
135 self
->short_name
= basename(self
->long_name
);
138 struct dso
*dso__new(const char *name
)
140 struct dso
*self
= malloc(sizeof(*self
) + strlen(name
) + 1);
143 strcpy(self
->name
, name
);
144 dso__set_long_name(self
, self
->name
);
145 self
->short_name
= self
->name
;
146 self
->functions
= RB_ROOT
;
147 self
->find_function
= dso__find_function
;
148 self
->slen_calculated
= 0;
149 self
->origin
= DSO__ORIG_NOT_FOUND
;
151 self
->has_build_id
= 0;
157 static void symbols__delete(struct rb_root
*self
)
160 struct rb_node
*next
= rb_first(self
);
163 pos
= rb_entry(next
, struct symbol
, rb_node
);
164 next
= rb_next(&pos
->rb_node
);
165 rb_erase(&pos
->rb_node
, self
);
170 void dso__delete(struct dso
*self
)
172 symbols__delete(&self
->functions
);
173 if (self
->long_name
!= self
->name
)
174 free(self
->long_name
);
178 void dso__set_build_id(struct dso
*self
, void *build_id
)
180 memcpy(self
->build_id
, build_id
, sizeof(self
->build_id
));
181 self
->has_build_id
= 1;
184 static void symbols__insert(struct rb_root
*self
, struct symbol
*sym
)
186 struct rb_node
**p
= &self
->rb_node
;
187 struct rb_node
*parent
= NULL
;
188 const u64 ip
= sym
->start
;
193 s
= rb_entry(parent
, struct symbol
, rb_node
);
199 rb_link_node(&sym
->rb_node
, parent
, p
);
200 rb_insert_color(&sym
->rb_node
, self
);
203 static struct symbol
*symbols__find(struct rb_root
*self
, u64 ip
)
213 struct symbol
*s
= rb_entry(n
, struct symbol
, rb_node
);
217 else if (ip
> s
->end
)
226 struct symbol
*dso__find_function(struct dso
*self
, u64 ip
)
228 return symbols__find(&self
->functions
, ip
);
231 int build_id__sprintf(u8
*self
, int len
, char *bf
)
237 for (i
= 0; i
< len
; ++i
) {
238 sprintf(bid
, "%02x", *raw
);
246 size_t dso__fprintf_buildid(struct dso
*self
, FILE *fp
)
248 char sbuild_id
[BUILD_ID_SIZE
* 2 + 1];
250 build_id__sprintf(self
->build_id
, sizeof(self
->build_id
), sbuild_id
);
251 return fprintf(fp
, "%s", sbuild_id
);
254 size_t dso__fprintf(struct dso
*self
, FILE *fp
)
257 size_t ret
= fprintf(fp
, "dso: %s (", self
->short_name
);
259 ret
+= dso__fprintf_buildid(self
, fp
);
260 ret
+= fprintf(fp
, ")\nFunctions:\n");
262 for (nd
= rb_first(&self
->functions
); nd
; nd
= rb_next(nd
)) {
263 struct symbol
*pos
= rb_entry(nd
, struct symbol
, rb_node
);
264 ret
+= symbol__fprintf(pos
, fp
);
271 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
272 * so that we can in the next step set the symbol ->end address and then
273 * call kernel_maps__split_kallsyms.
275 static int kernel_maps__load_all_kallsyms(void)
279 FILE *file
= fopen("/proc/kallsyms", "r");
284 while (!feof(file
)) {
291 line_len
= getline(&line
, &n
, file
);
298 line
[--line_len
] = '\0'; /* \n */
300 len
= hex2u64(line
, &start
);
303 if (len
+ 2 >= line_len
)
306 symbol_type
= toupper(line
[len
]);
308 * We're interested only in code ('T'ext)
310 if (symbol_type
!= 'T' && symbol_type
!= 'W')
313 symbol_name
= line
+ len
+ 2;
315 * Will fix up the end later, when we have all symbols sorted.
317 sym
= symbol__new(start
, 0, symbol_name
);
320 goto out_delete_line
;
323 * We will pass the symbols to the filter later, in
324 * kernel_maps__split_kallsyms, when we have split the
327 symbols__insert(&kernel_map__functions
->dso
->functions
, sym
);
342 * Split the symbols into maps, making sure there are no overlaps, i.e. the
343 * kernel range is broken in several maps, named [kernel].N, as we don't have
344 * the original ELF section names vmlinux have.
346 static int kernel_maps__split_kallsyms(symbol_filter_t filter
)
348 struct map
*map
= kernel_map__functions
;
351 struct rb_node
*next
= rb_first(&kernel_map__functions
->dso
->functions
);
352 int kernel_range
= 0;
357 pos
= rb_entry(next
, struct symbol
, rb_node
);
358 next
= rb_next(&pos
->rb_node
);
360 module
= strchr(pos
->name
, '\t');
364 if (strcmp(map
->dso
->name
, module
)) {
365 map
= kernel_maps__find_by_dso_name(module
);
367 pr_err("/proc/{kallsyms,modules} "
373 * So that we look just like we get from .ko files,
374 * i.e. not prelinked, relative to map->start.
376 pos
->start
= map
->map_ip(map
, pos
->start
);
377 pos
->end
= map
->map_ip(map
, pos
->end
);
378 } else if (map
!= kernel_map__functions
) {
379 char dso_name
[PATH_MAX
];
382 snprintf(dso_name
, sizeof(dso_name
), "[kernel].%d",
385 dso
= dso__new(dso_name
);
389 map
= map__new2(pos
->start
, dso
);
395 map
->map_ip
= map
->unmap_ip
= identity__map_ip
;
396 kernel_maps__insert(map
);
400 if (filter
&& filter(map
, pos
)) {
401 rb_erase(&pos
->rb_node
, &kernel_map__functions
->dso
->functions
);
404 if (map
!= kernel_map__functions
) {
405 rb_erase(&pos
->rb_node
,
406 &kernel_map__functions
->dso
->functions
);
407 symbols__insert(&map
->dso
->functions
, pos
);
417 static int kernel_maps__load_kallsyms(symbol_filter_t filter
)
419 if (kernel_maps__load_all_kallsyms())
422 symbols__fixup_end(&kernel_map__functions
->dso
->functions
);
423 kernel_map__functions
->dso
->origin
= DSO__ORIG_KERNEL
;
425 return kernel_maps__split_kallsyms(filter
);
428 size_t kernel_maps__fprintf(FILE *fp
)
430 size_t printed
= fprintf(fp
, "Kernel maps:\n");
433 for (nd
= rb_first(&kernel_maps__functions
); nd
; nd
= rb_next(nd
)) {
434 struct map
*pos
= rb_entry(nd
, struct map
, rb_node
);
436 printed
+= fprintf(fp
, "Map:");
437 printed
+= map__fprintf(pos
, fp
);
439 printed
+= dso__fprintf(pos
->dso
, fp
);
440 printed
+= fprintf(fp
, "--\n");
444 return printed
+ fprintf(fp
, "END kernel maps\n");
447 static int dso__load_perf_map(struct dso
*self
, struct map
*map
,
448 symbol_filter_t filter
)
455 file
= fopen(self
->long_name
, "r");
459 while (!feof(file
)) {
464 line_len
= getline(&line
, &n
, file
);
471 line
[--line_len
] = '\0'; /* \n */
473 len
= hex2u64(line
, &start
);
476 if (len
+ 2 >= line_len
)
479 len
+= hex2u64(line
+ len
, &size
);
482 if (len
+ 2 >= line_len
)
485 sym
= symbol__new(start
, size
, line
+ len
);
488 goto out_delete_line
;
490 if (filter
&& filter(map
, sym
))
493 symbols__insert(&self
->functions
, sym
);
510 * elf_symtab__for_each_symbol - iterate thru all the symbols
512 * @self: struct elf_symtab instance to iterate
514 * @sym: GElf_Sym iterator
516 #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
517 for (idx = 0, gelf_getsym(syms, idx, &sym);\
519 idx++, gelf_getsym(syms, idx, &sym))
521 static inline uint8_t elf_sym__type(const GElf_Sym
*sym
)
523 return GELF_ST_TYPE(sym
->st_info
);
526 static inline int elf_sym__is_function(const GElf_Sym
*sym
)
528 return elf_sym__type(sym
) == STT_FUNC
&&
530 sym
->st_shndx
!= SHN_UNDEF
;
533 static inline int elf_sym__is_label(const GElf_Sym
*sym
)
535 return elf_sym__type(sym
) == STT_NOTYPE
&&
537 sym
->st_shndx
!= SHN_UNDEF
&&
538 sym
->st_shndx
!= SHN_ABS
;
541 static inline const char *elf_sec__name(const GElf_Shdr
*shdr
,
542 const Elf_Data
*secstrs
)
544 return secstrs
->d_buf
+ shdr
->sh_name
;
547 static inline int elf_sec__is_text(const GElf_Shdr
*shdr
,
548 const Elf_Data
*secstrs
)
550 return strstr(elf_sec__name(shdr
, secstrs
), "text") != NULL
;
553 static inline const char *elf_sym__name(const GElf_Sym
*sym
,
554 const Elf_Data
*symstrs
)
556 return symstrs
->d_buf
+ sym
->st_name
;
559 static Elf_Scn
*elf_section_by_name(Elf
*elf
, GElf_Ehdr
*ep
,
560 GElf_Shdr
*shp
, const char *name
,
566 while ((sec
= elf_nextscn(elf
, sec
)) != NULL
) {
569 gelf_getshdr(sec
, shp
);
570 str
= elf_strptr(elf
, ep
->e_shstrndx
, shp
->sh_name
);
571 if (!strcmp(name
, str
)) {
582 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
583 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
585 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
587 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
588 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
590 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
593 * We need to check if we have a .dynsym, so that we can handle the
594 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
595 * .dynsym or .symtab).
596 * And always look at the original dso, not at debuginfo packages, that
597 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
599 static int dso__synthesize_plt_symbols(struct dso
*self
, struct map
*map
,
600 symbol_filter_t filter
)
602 uint32_t nr_rel_entries
, idx
;
607 GElf_Shdr shdr_rel_plt
, shdr_dynsym
;
608 Elf_Data
*reldata
, *syms
, *symstrs
;
609 Elf_Scn
*scn_plt_rel
, *scn_symstrs
, *scn_dynsym
;
612 char sympltname
[1024];
614 int nr
= 0, symidx
, fd
, err
= 0;
616 fd
= open(self
->long_name
, O_RDONLY
);
620 elf
= elf_begin(fd
, PERF_ELF_C_READ_MMAP
, NULL
);
624 if (gelf_getehdr(elf
, &ehdr
) == NULL
)
627 scn_dynsym
= elf_section_by_name(elf
, &ehdr
, &shdr_dynsym
,
628 ".dynsym", &dynsym_idx
);
629 if (scn_dynsym
== NULL
)
632 scn_plt_rel
= elf_section_by_name(elf
, &ehdr
, &shdr_rel_plt
,
634 if (scn_plt_rel
== NULL
) {
635 scn_plt_rel
= elf_section_by_name(elf
, &ehdr
, &shdr_rel_plt
,
637 if (scn_plt_rel
== NULL
)
643 if (shdr_rel_plt
.sh_link
!= dynsym_idx
)
646 if (elf_section_by_name(elf
, &ehdr
, &shdr_plt
, ".plt", NULL
) == NULL
)
650 * Fetch the relocation section to find the idxes to the GOT
651 * and the symbols in the .dynsym they refer to.
653 reldata
= elf_getdata(scn_plt_rel
, NULL
);
657 syms
= elf_getdata(scn_dynsym
, NULL
);
661 scn_symstrs
= elf_getscn(elf
, shdr_dynsym
.sh_link
);
662 if (scn_symstrs
== NULL
)
665 symstrs
= elf_getdata(scn_symstrs
, NULL
);
669 nr_rel_entries
= shdr_rel_plt
.sh_size
/ shdr_rel_plt
.sh_entsize
;
670 plt_offset
= shdr_plt
.sh_offset
;
672 if (shdr_rel_plt
.sh_type
== SHT_RELA
) {
673 GElf_Rela pos_mem
, *pos
;
675 elf_section__for_each_rela(reldata
, pos
, pos_mem
, idx
,
677 symidx
= GELF_R_SYM(pos
->r_info
);
678 plt_offset
+= shdr_plt
.sh_entsize
;
679 gelf_getsym(syms
, symidx
, &sym
);
680 snprintf(sympltname
, sizeof(sympltname
),
681 "%s@plt", elf_sym__name(&sym
, symstrs
));
683 f
= symbol__new(plt_offset
, shdr_plt
.sh_entsize
,
688 if (filter
&& filter(map
, f
))
691 symbols__insert(&self
->functions
, f
);
695 } else if (shdr_rel_plt
.sh_type
== SHT_REL
) {
696 GElf_Rel pos_mem
, *pos
;
697 elf_section__for_each_rel(reldata
, pos
, pos_mem
, idx
,
699 symidx
= GELF_R_SYM(pos
->r_info
);
700 plt_offset
+= shdr_plt
.sh_entsize
;
701 gelf_getsym(syms
, symidx
, &sym
);
702 snprintf(sympltname
, sizeof(sympltname
),
703 "%s@plt", elf_sym__name(&sym
, symstrs
));
705 f
= symbol__new(plt_offset
, shdr_plt
.sh_entsize
,
710 if (filter
&& filter(map
, f
))
713 symbols__insert(&self
->functions
, f
);
728 pr_warning("%s: problems reading %s PLT info.\n",
729 __func__
, self
->long_name
);
733 static int dso__load_sym(struct dso
*self
, struct map
*map
, const char *name
,
734 int fd
, symbol_filter_t filter
, int kernel
,
737 struct map
*curr_map
= map
;
738 struct dso
*curr_dso
= self
;
739 size_t dso_name_len
= strlen(self
->short_name
);
740 Elf_Data
*symstrs
, *secstrs
;
748 Elf_Scn
*sec
, *sec_strndx
;
752 elf
= elf_begin(fd
, PERF_ELF_C_READ_MMAP
, NULL
);
754 pr_err("%s: cannot read %s ELF file.\n", __func__
, name
);
758 if (gelf_getehdr(elf
, &ehdr
) == NULL
) {
759 pr_err("%s: cannot get elf header.\n", __func__
);
763 sec
= elf_section_by_name(elf
, &ehdr
, &shdr
, ".symtab", NULL
);
765 sec
= elf_section_by_name(elf
, &ehdr
, &shdr
, ".dynsym", NULL
);
770 syms
= elf_getdata(sec
, NULL
);
774 sec
= elf_getscn(elf
, shdr
.sh_link
);
778 symstrs
= elf_getdata(sec
, NULL
);
782 sec_strndx
= elf_getscn(elf
, ehdr
.e_shstrndx
);
783 if (sec_strndx
== NULL
)
786 secstrs
= elf_getdata(sec_strndx
, NULL
);
790 nr_syms
= shdr
.sh_size
/ shdr
.sh_entsize
;
792 memset(&sym
, 0, sizeof(sym
));
794 self
->adjust_symbols
= (ehdr
.e_type
== ET_EXEC
||
795 elf_section_by_name(elf
, &ehdr
, &shdr
,
798 } else self
->adjust_symbols
= 0;
800 elf_symtab__for_each_symbol(syms
, nr_syms
, idx
, sym
) {
802 const char *elf_name
;
803 char *demangled
= NULL
;
804 int is_label
= elf_sym__is_label(&sym
);
805 const char *section_name
;
807 if (!is_label
&& !elf_sym__is_function(&sym
))
810 sec
= elf_getscn(elf
, sym
.st_shndx
);
814 gelf_getshdr(sec
, &shdr
);
816 if (is_label
&& !elf_sec__is_text(&shdr
, secstrs
))
819 elf_name
= elf_sym__name(&sym
, symstrs
);
820 section_name
= elf_sec__name(&shdr
, secstrs
);
822 if (kernel
|| kmodule
) {
823 char dso_name
[PATH_MAX
];
825 if (strcmp(section_name
,
826 curr_dso
->short_name
+ dso_name_len
) == 0)
829 if (strcmp(section_name
, ".text") == 0) {
835 snprintf(dso_name
, sizeof(dso_name
),
836 "%s%s", self
->short_name
, section_name
);
838 curr_map
= kernel_maps__find_by_dso_name(dso_name
);
839 if (curr_map
== NULL
) {
840 u64 start
= sym
.st_value
;
843 start
+= map
->start
+ shdr
.sh_offset
;
845 curr_dso
= dso__new(dso_name
);
846 if (curr_dso
== NULL
)
848 curr_map
= map__new2(start
, curr_dso
);
849 if (curr_map
== NULL
) {
850 dso__delete(curr_dso
);
853 curr_map
->map_ip
= identity__map_ip
;
854 curr_map
->unmap_ip
= identity__map_ip
;
855 curr_dso
->origin
= DSO__ORIG_KERNEL
;
856 kernel_maps__insert(curr_map
);
857 dsos__add(&dsos__kernel
, curr_dso
);
859 curr_dso
= curr_map
->dso
;
864 if (curr_dso
->adjust_symbols
) {
865 pr_debug2("adjusting symbol: st_value: %Lx sh_addr: "
866 "%Lx sh_offset: %Lx\n", (u64
)sym
.st_value
,
867 (u64
)shdr
.sh_addr
, (u64
)shdr
.sh_offset
);
868 sym
.st_value
-= shdr
.sh_addr
- shdr
.sh_offset
;
871 * We need to figure out if the object was created from C++ sources
872 * DWARF DW_compile_unit has this, but we don't always have access
875 demangled
= bfd_demangle(NULL
, elf_name
, DMGL_PARAMS
| DMGL_ANSI
);
876 if (demangled
!= NULL
)
877 elf_name
= demangled
;
879 f
= symbol__new(sym
.st_value
, sym
.st_size
, elf_name
);
884 if (filter
&& filter(curr_map
, f
))
887 symbols__insert(&curr_dso
->functions
, f
);
893 * For misannotated, zeroed, ASM function sizes.
896 symbols__fixup_end(&self
->functions
);
904 static bool dso__build_id_equal(const struct dso
*self
, u8
*build_id
)
906 return memcmp(self
->build_id
, build_id
, sizeof(self
->build_id
)) == 0;
909 static bool __dsos__read_build_ids(struct list_head
*head
)
911 bool have_build_id
= false;
914 list_for_each_entry(pos
, head
, node
)
915 if (filename__read_build_id(pos
->long_name
, pos
->build_id
,
916 sizeof(pos
->build_id
)) > 0) {
917 have_build_id
= true;
918 pos
->has_build_id
= true;
921 return have_build_id
;
924 bool dsos__read_build_ids(void)
926 return __dsos__read_build_ids(&dsos__kernel
) ||
927 __dsos__read_build_ids(&dsos__user
);
931 * Align offset to 4 bytes as needed for note name and descriptor data.
933 #define NOTE_ALIGN(n) (((n) + 3) & -4U)
935 int filename__read_build_id(const char *filename
, void *bf
, size_t size
)
946 if (size
< BUILD_ID_SIZE
)
949 fd
= open(filename
, O_RDONLY
);
953 elf
= elf_begin(fd
, PERF_ELF_C_READ_MMAP
, NULL
);
955 pr_debug2("%s: cannot read %s ELF file.\n", __func__
, filename
);
963 if (gelf_getehdr(elf
, &ehdr
) == NULL
) {
964 pr_err("%s: cannot get elf header.\n", __func__
);
968 sec
= elf_section_by_name(elf
, &ehdr
, &shdr
,
969 ".note.gnu.build-id", NULL
);
971 sec
= elf_section_by_name(elf
, &ehdr
, &shdr
,
977 data
= elf_getdata(sec
, NULL
);
982 while (ptr
< (data
->d_buf
+ data
->d_size
)) {
983 GElf_Nhdr
*nhdr
= ptr
;
984 int namesz
= NOTE_ALIGN(nhdr
->n_namesz
),
985 descsz
= NOTE_ALIGN(nhdr
->n_descsz
);
988 ptr
+= sizeof(*nhdr
);
991 if (nhdr
->n_type
== NT_GNU_BUILD_ID
&&
992 nhdr
->n_namesz
== sizeof("GNU")) {
993 if (memcmp(name
, "GNU", sizeof("GNU")) == 0) {
994 memcpy(bf
, ptr
, BUILD_ID_SIZE
);
1009 int sysfs__read_build_id(const char *filename
, void *build_id
, size_t size
)
1013 if (size
< BUILD_ID_SIZE
)
1016 fd
= open(filename
, O_RDONLY
);
1025 if (read(fd
, &nhdr
, sizeof(nhdr
)) != sizeof(nhdr
))
1028 namesz
= NOTE_ALIGN(nhdr
.n_namesz
);
1029 descsz
= NOTE_ALIGN(nhdr
.n_descsz
);
1030 if (nhdr
.n_type
== NT_GNU_BUILD_ID
&&
1031 nhdr
.n_namesz
== sizeof("GNU")) {
1032 if (read(fd
, bf
, namesz
) != namesz
)
1034 if (memcmp(bf
, "GNU", sizeof("GNU")) == 0) {
1035 if (read(fd
, build_id
,
1036 BUILD_ID_SIZE
) == BUILD_ID_SIZE
) {
1040 } else if (read(fd
, bf
, descsz
) != descsz
)
1043 int n
= namesz
+ descsz
;
1044 if (read(fd
, bf
, n
) != n
)
1053 char dso__symtab_origin(const struct dso
*self
)
1055 static const char origin
[] = {
1056 [DSO__ORIG_KERNEL
] = 'k',
1057 [DSO__ORIG_JAVA_JIT
] = 'j',
1058 [DSO__ORIG_FEDORA
] = 'f',
1059 [DSO__ORIG_UBUNTU
] = 'u',
1060 [DSO__ORIG_BUILDID
] = 'b',
1061 [DSO__ORIG_DSO
] = 'd',
1062 [DSO__ORIG_KMODULE
] = 'K',
1065 if (self
== NULL
|| self
->origin
== DSO__ORIG_NOT_FOUND
)
1067 return origin
[self
->origin
];
1070 int dso__load(struct dso
*self
, struct map
*map
, symbol_filter_t filter
)
1072 int size
= PATH_MAX
;
1074 u8 build_id
[BUILD_ID_SIZE
];
1081 return dso__load_kernel_sym(self
, map
, filter
);
1083 name
= malloc(size
);
1087 self
->adjust_symbols
= 0;
1089 if (strncmp(self
->name
, "/tmp/perf-", 10) == 0) {
1090 ret
= dso__load_perf_map(self
, map
, filter
);
1091 self
->origin
= ret
> 0 ? DSO__ORIG_JAVA_JIT
:
1092 DSO__ORIG_NOT_FOUND
;
1096 self
->origin
= DSO__ORIG_FEDORA
- 1;
1101 switch (self
->origin
) {
1102 case DSO__ORIG_FEDORA
:
1103 snprintf(name
, size
, "/usr/lib/debug%s.debug",
1106 case DSO__ORIG_UBUNTU
:
1107 snprintf(name
, size
, "/usr/lib/debug%s",
1110 case DSO__ORIG_BUILDID
:
1111 if (filename__read_build_id(self
->long_name
, build_id
,
1112 sizeof(build_id
))) {
1113 char build_id_hex
[BUILD_ID_SIZE
* 2 + 1];
1115 build_id__sprintf(build_id
, sizeof(build_id
),
1117 snprintf(name
, size
,
1118 "/usr/lib/debug/.build-id/%.2s/%s.debug",
1119 build_id_hex
, build_id_hex
+ 2);
1120 if (self
->has_build_id
)
1121 goto compare_build_id
;
1127 snprintf(name
, size
, "%s", self
->long_name
);
1134 if (self
->has_build_id
) {
1135 if (filename__read_build_id(name
, build_id
,
1136 sizeof(build_id
)) < 0)
1139 if (!dso__build_id_equal(self
, build_id
))
1143 fd
= open(name
, O_RDONLY
);
1146 ret
= dso__load_sym(self
, map
, name
, fd
, filter
, 0, 0);
1150 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
1156 int nr_plt
= dso__synthesize_plt_symbols(self
, map
, filter
);
1162 if (ret
< 0 && strstr(self
->name
, " (deleted)") != NULL
)
1167 struct map
*kernel_map__functions
;
1169 static void kernel_maps__insert(struct map
*map
)
1171 maps__insert(&kernel_maps__functions
, map
);
1174 struct symbol
*kernel_maps__find_function(u64 ip
, struct map
**mapp
,
1175 symbol_filter_t filter
)
1177 struct map
*map
= maps__find(&kernel_maps__functions
, ip
);
1183 ip
= map
->map_ip(map
, ip
);
1184 return map__find_function(map
, ip
, filter
);
1186 WARN_ONCE(RB_EMPTY_ROOT(&kernel_maps__functions
),
1187 "Empty kernel_maps, was symbol__init() called?\n");
1192 struct map
*kernel_maps__find_by_dso_name(const char *name
)
1196 for (nd
= rb_first(&kernel_maps__functions
); nd
; nd
= rb_next(nd
)) {
1197 struct map
*map
= rb_entry(nd
, struct map
, rb_node
);
1199 if (map
->dso
&& strcmp(map
->dso
->name
, name
) == 0)
1206 static int dsos__set_modules_path_dir(char *dirname
)
1208 struct dirent
*dent
;
1209 DIR *dir
= opendir(dirname
);
1212 pr_debug("%s: cannot open %s dir\n", __func__
, dirname
);
1216 while ((dent
= readdir(dir
)) != NULL
) {
1217 char path
[PATH_MAX
];
1219 if (dent
->d_type
== DT_DIR
) {
1220 if (!strcmp(dent
->d_name
, ".") ||
1221 !strcmp(dent
->d_name
, ".."))
1224 snprintf(path
, sizeof(path
), "%s/%s",
1225 dirname
, dent
->d_name
);
1226 if (dsos__set_modules_path_dir(path
) < 0)
1229 char *dot
= strrchr(dent
->d_name
, '.'),
1234 if (dot
== NULL
|| strcmp(dot
, ".ko"))
1236 snprintf(dso_name
, sizeof(dso_name
), "[%.*s]",
1237 (int)(dot
- dent
->d_name
), dent
->d_name
);
1239 strxfrchar(dso_name
, '-', '_');
1240 map
= kernel_maps__find_by_dso_name(dso_name
);
1244 snprintf(path
, sizeof(path
), "%s/%s",
1245 dirname
, dent
->d_name
);
1247 long_name
= strdup(path
);
1248 if (long_name
== NULL
)
1250 dso__set_long_name(map
->dso
, long_name
);
1260 static int dsos__set_modules_path(void)
1263 char modules_path
[PATH_MAX
];
1265 if (uname(&uts
) < 0)
1268 snprintf(modules_path
, sizeof(modules_path
), "/lib/modules/%s/kernel",
1271 return dsos__set_modules_path_dir(modules_path
);
1275 * Constructor variant for modules (where we know from /proc/modules where
1276 * they are loaded) and for vmlinux, where only after we load all the
1277 * symbols we'll know where it starts and ends.
1279 static struct map
*map__new2(u64 start
, struct dso
*dso
)
1281 struct map
*self
= malloc(sizeof(*self
));
1285 * ->end will be filled after we load all the symbols
1287 map__init(self
, start
, 0, 0, dso
);
1293 static int kernel_maps__create_module_maps(void)
1297 FILE *file
= fopen("/proc/modules", "r");
1303 while (!feof(file
)) {
1304 char name
[PATH_MAX
];
1310 line_len
= getline(&line
, &n
, file
);
1317 line
[--line_len
] = '\0'; /* \n */
1319 sep
= strrchr(line
, 'x');
1323 hex2u64(sep
+ 1, &start
);
1325 sep
= strchr(line
, ' ');
1331 snprintf(name
, sizeof(name
), "[%s]", line
);
1332 dso
= dso__new(name
);
1335 goto out_delete_line
;
1337 map
= map__new2(start
, dso
);
1340 goto out_delete_line
;
1343 snprintf(name
, sizeof(name
),
1344 "/sys/module/%s/notes/.note.gnu.build-id", line
);
1345 if (sysfs__read_build_id(name
, dso
->build_id
,
1346 sizeof(dso
->build_id
)) == 0)
1347 dso
->has_build_id
= true;
1349 dso
->origin
= DSO__ORIG_KMODULE
;
1350 kernel_maps__insert(map
);
1351 dsos__add(&dsos__kernel
, dso
);
1357 return dsos__set_modules_path();
1365 static int dso__load_vmlinux(struct dso
*self
, struct map
*map
,
1366 const char *vmlinux
, symbol_filter_t filter
)
1370 if (self
->has_build_id
) {
1371 u8 build_id
[BUILD_ID_SIZE
];
1373 if (filename__read_build_id(vmlinux
, build_id
,
1374 sizeof(build_id
)) < 0) {
1375 pr_debug("No build_id in %s, ignoring it\n", vmlinux
);
1378 if (!dso__build_id_equal(self
, build_id
)) {
1379 char expected_build_id
[BUILD_ID_SIZE
* 2 + 1],
1380 vmlinux_build_id
[BUILD_ID_SIZE
* 2 + 1];
1382 build_id__sprintf(self
->build_id
,
1383 sizeof(self
->build_id
),
1385 build_id__sprintf(build_id
, sizeof(build_id
),
1387 pr_debug("build_id in %s is %s while expected is %s, "
1388 "ignoring it\n", vmlinux
, vmlinux_build_id
,
1394 fd
= open(vmlinux
, O_RDONLY
);
1399 err
= dso__load_sym(self
, map
, self
->long_name
, fd
, filter
, 1, 0);
1406 static int dso__load_kernel_sym(struct dso
*self
, struct map
*map
,
1407 symbol_filter_t filter
)
1412 if (vmlinux_path
!= NULL
) {
1414 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1415 vmlinux_path__nr_entries
);
1416 for (i
= 0; i
< vmlinux_path__nr_entries
; ++i
) {
1417 err
= dso__load_vmlinux(self
, map
, vmlinux_path
[i
],
1420 pr_debug("Using %s for symbols\n",
1422 dso__set_long_name(self
,
1423 strdup(vmlinux_path
[i
]));
1429 is_kallsyms
= self
->long_name
[0] == '[';
1433 err
= dso__load_vmlinux(self
, map
, self
->long_name
, filter
);
1435 pr_info("The file %s cannot be used, "
1436 "trying to use /proc/kallsyms...", self
->long_name
);
1439 err
= kernel_maps__load_kallsyms(filter
);
1440 if (err
> 0 && !is_kallsyms
)
1441 dso__set_long_name(self
, strdup("[kernel.kallsyms]"));
1446 map__fixup_start(map
, &map
->dso
->functions
);
1447 map__fixup_end(map
, &map
->dso
->functions
);
1453 LIST_HEAD(dsos__user
);
1454 LIST_HEAD(dsos__kernel
);
1457 static void dsos__add(struct list_head
*head
, struct dso
*dso
)
1459 list_add_tail(&dso
->node
, head
);
1462 static struct dso
*dsos__find(struct list_head
*head
, const char *name
)
1466 list_for_each_entry(pos
, head
, node
)
1467 if (strcmp(pos
->name
, name
) == 0)
1472 struct dso
*dsos__findnew(const char *name
)
1474 struct dso
*dso
= dsos__find(&dsos__user
, name
);
1477 dso
= dso__new(name
);
1479 dsos__add(&dsos__user
, dso
);
1480 dso__set_basename(dso
);
1487 static void __dsos__fprintf(struct list_head
*head
, FILE *fp
)
1491 list_for_each_entry(pos
, head
, node
)
1492 dso__fprintf(pos
, fp
);
1495 void dsos__fprintf(FILE *fp
)
1497 __dsos__fprintf(&dsos__kernel
, fp
);
1498 __dsos__fprintf(&dsos__user
, fp
);
1501 static size_t __dsos__fprintf_buildid(struct list_head
*head
, FILE *fp
)
1506 list_for_each_entry(pos
, head
, node
) {
1507 ret
+= dso__fprintf_buildid(pos
, fp
);
1508 ret
+= fprintf(fp
, " %s\n", pos
->long_name
);
1513 size_t dsos__fprintf_buildid(FILE *fp
)
1515 return (__dsos__fprintf_buildid(&dsos__kernel
, fp
) +
1516 __dsos__fprintf_buildid(&dsos__user
, fp
));
1519 static int kernel_maps__create_kernel_map(const struct symbol_conf
*conf
)
1521 struct dso
*kernel
= dso__new(conf
->vmlinux_name
?: "[kernel.kallsyms]");
1526 kernel_map__functions
= map__new2(0, kernel
);
1527 if (kernel_map__functions
== NULL
)
1528 goto out_delete_kernel_dso
;
1530 kernel_map__functions
->map_ip
= kernel_map__functions
->unmap_ip
= identity__map_ip
;
1531 kernel
->short_name
= "[kernel]";
1534 vdso
= dso__new("[vdso]");
1536 goto out_delete_kernel_map
;
1539 if (sysfs__read_build_id("/sys/kernel/notes", kernel
->build_id
,
1540 sizeof(kernel
->build_id
)) == 0)
1541 kernel
->has_build_id
= true;
1543 kernel_maps__insert(kernel_map__functions
);
1544 dsos__add(&dsos__kernel
, kernel
);
1545 dsos__add(&dsos__user
, vdso
);
1549 out_delete_kernel_map
:
1550 map__delete(kernel_map__functions
);
1551 kernel_map__functions
= NULL
;
1552 out_delete_kernel_dso
:
1553 dso__delete(kernel
);
1557 static void vmlinux_path__exit(void)
1559 while (--vmlinux_path__nr_entries
>= 0) {
1560 free(vmlinux_path
[vmlinux_path__nr_entries
]);
1561 vmlinux_path
[vmlinux_path__nr_entries
] = NULL
;
1565 vmlinux_path
= NULL
;
1568 static int vmlinux_path__init(void)
1573 if (uname(&uts
) < 0)
1576 vmlinux_path
= malloc(sizeof(char *) * 5);
1577 if (vmlinux_path
== NULL
)
1580 vmlinux_path
[vmlinux_path__nr_entries
] = strdup("vmlinux");
1581 if (vmlinux_path
[vmlinux_path__nr_entries
] == NULL
)
1583 ++vmlinux_path__nr_entries
;
1584 vmlinux_path
[vmlinux_path__nr_entries
] = strdup("/boot/vmlinux");
1585 if (vmlinux_path
[vmlinux_path__nr_entries
] == NULL
)
1587 ++vmlinux_path__nr_entries
;
1588 snprintf(bf
, sizeof(bf
), "/boot/vmlinux-%s", uts
.release
);
1589 vmlinux_path
[vmlinux_path__nr_entries
] = strdup(bf
);
1590 if (vmlinux_path
[vmlinux_path__nr_entries
] == NULL
)
1592 ++vmlinux_path__nr_entries
;
1593 snprintf(bf
, sizeof(bf
), "/lib/modules/%s/build/vmlinux", uts
.release
);
1594 vmlinux_path
[vmlinux_path__nr_entries
] = strdup(bf
);
1595 if (vmlinux_path
[vmlinux_path__nr_entries
] == NULL
)
1597 ++vmlinux_path__nr_entries
;
1598 snprintf(bf
, sizeof(bf
), "/usr/lib/debug/lib/modules/%s/vmlinux",
1600 vmlinux_path
[vmlinux_path__nr_entries
] = strdup(bf
);
1601 if (vmlinux_path
[vmlinux_path__nr_entries
] == NULL
)
1603 ++vmlinux_path__nr_entries
;
1608 vmlinux_path__exit();
1612 static int kernel_maps__init(const struct symbol_conf
*conf
)
1614 const struct symbol_conf
*pconf
= conf
?: &symbol_conf__defaults
;
1616 symbol__priv_size
= pconf
->priv_size
;
1618 if (pconf
->try_vmlinux_path
&& vmlinux_path__init() < 0)
1621 if (kernel_maps__create_kernel_map(pconf
) < 0) {
1622 vmlinux_path__exit();
1626 if (pconf
->use_modules
&& kernel_maps__create_module_maps() < 0)
1627 pr_debug("Failed to load list of modules in use, "
1630 * Now that we have all the maps created, just set the ->end of them:
1632 kernel_maps__fixup_end();
1636 int symbol__init(struct symbol_conf
*conf
)
1638 elf_version(EV_CURRENT
);
1639 return kernel_maps__init(conf
);