13 #include <sys/utsname.h>
15 #ifndef NT_GNU_BUILD_ID
16 #define NT_GNU_BUILD_ID 3
30 static void dsos__add(struct dso
*dso
);
31 static struct dso
*dsos__find(const char *name
);
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
;
38 static struct rb_root kernel_maps
;
40 static void dso__fixup_sym_end(struct dso
*self
)
42 struct rb_node
*nd
, *prevnd
= rb_first(&self
->syms
);
43 struct symbol
*curr
, *prev
;
48 curr
= rb_entry(prevnd
, struct symbol
, rb_node
);
50 for (nd
= rb_next(prevnd
); nd
; nd
= rb_next(nd
)) {
52 curr
= rb_entry(nd
, struct symbol
, rb_node
);
54 if (prev
->end
== prev
->start
)
55 prev
->end
= curr
->start
- 1;
59 if (curr
->end
== curr
->start
)
60 curr
->end
= roundup(curr
->start
, 4096);
63 static void kernel_maps__fixup_end(void)
65 struct map
*prev
, *curr
;
66 struct rb_node
*nd
, *prevnd
= rb_first(&kernel_maps
);
71 curr
= rb_entry(prevnd
, struct map
, rb_node
);
73 for (nd
= rb_next(prevnd
); nd
; nd
= rb_next(nd
)) {
75 curr
= rb_entry(nd
, struct map
, rb_node
);
76 prev
->end
= curr
->start
- 1;
80 * We still haven't the actual symbols, so guess the
81 * last map final address.
86 static struct symbol
*symbol__new(u64 start
, u64 len
, const char *name
)
88 size_t namelen
= strlen(name
) + 1;
89 struct symbol
*self
= calloc(1, (symbol__priv_size
+
90 sizeof(*self
) + namelen
));
94 if (symbol__priv_size
) {
95 memset(self
, 0, symbol__priv_size
);
96 self
= ((void *)self
) + symbol__priv_size
;
99 self
->end
= len
? start
+ len
- 1 : start
;
101 pr_debug3("%s: %s %#Lx-%#Lx\n", __func__
, name
, start
, self
->end
);
103 memcpy(self
->name
, name
, namelen
);
108 static void symbol__delete(struct symbol
*self
)
110 free(((void *)self
) - symbol__priv_size
);
113 static size_t symbol__fprintf(struct symbol
*self
, FILE *fp
)
115 return fprintf(fp
, " %llx-%llx %s\n",
116 self
->start
, self
->end
, self
->name
);
119 static void dso__set_long_name(struct dso
*self
, char *name
)
123 self
->long_name
= name
;
124 self
->long_name_len
= strlen(name
);
127 static void dso__set_basename(struct dso
*self
)
129 self
->short_name
= basename(self
->long_name
);
132 struct dso
*dso__new(const char *name
)
134 struct dso
*self
= malloc(sizeof(*self
) + strlen(name
) + 1);
137 strcpy(self
->name
, name
);
138 dso__set_long_name(self
, self
->name
);
139 self
->short_name
= self
->name
;
140 self
->syms
= RB_ROOT
;
141 self
->find_symbol
= dso__find_symbol
;
142 self
->slen_calculated
= 0;
143 self
->origin
= DSO__ORIG_NOT_FOUND
;
145 self
->has_build_id
= 0;
151 static void dso__delete_symbols(struct dso
*self
)
154 struct rb_node
*next
= rb_first(&self
->syms
);
157 pos
= rb_entry(next
, struct symbol
, rb_node
);
158 next
= rb_next(&pos
->rb_node
);
159 rb_erase(&pos
->rb_node
, &self
->syms
);
164 void dso__delete(struct dso
*self
)
166 dso__delete_symbols(self
);
167 if (self
->long_name
!= self
->name
)
168 free(self
->long_name
);
172 void dso__set_build_id(struct dso
*self
, void *build_id
)
174 memcpy(self
->build_id
, build_id
, sizeof(self
->build_id
));
175 self
->has_build_id
= 1;
178 static void dso__insert_symbol(struct dso
*self
, struct symbol
*sym
)
180 struct rb_node
**p
= &self
->syms
.rb_node
;
181 struct rb_node
*parent
= NULL
;
182 const u64 ip
= sym
->start
;
187 s
= rb_entry(parent
, struct symbol
, rb_node
);
193 rb_link_node(&sym
->rb_node
, parent
, p
);
194 rb_insert_color(&sym
->rb_node
, &self
->syms
);
197 struct symbol
*dso__find_symbol(struct dso
*self
, u64 ip
)
204 n
= self
->syms
.rb_node
;
207 struct symbol
*s
= rb_entry(n
, struct symbol
, rb_node
);
211 else if (ip
> s
->end
)
220 int build_id__sprintf(u8
*self
, int len
, char *bf
)
226 for (i
= 0; i
< len
; ++i
) {
227 sprintf(bid
, "%02x", *raw
);
235 size_t dso__fprintf_buildid(struct dso
*self
, FILE *fp
)
237 char sbuild_id
[BUILD_ID_SIZE
* 2 + 1];
239 build_id__sprintf(self
->build_id
, sizeof(self
->build_id
), sbuild_id
);
240 return fprintf(fp
, "%s", sbuild_id
);
243 size_t dso__fprintf(struct dso
*self
, FILE *fp
)
246 size_t ret
= fprintf(fp
, "dso: %s (", self
->short_name
);
248 ret
+= dso__fprintf_buildid(self
, fp
);
249 ret
+= fprintf(fp
, ")\n");
251 for (nd
= rb_first(&self
->syms
); nd
; nd
= rb_next(nd
)) {
252 struct symbol
*pos
= rb_entry(nd
, struct symbol
, rb_node
);
253 ret
+= symbol__fprintf(pos
, fp
);
260 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
261 * so that we can in the next step set the symbol ->end address and then
262 * call kernel_maps__split_kallsyms.
264 static int kernel_maps__load_all_kallsyms(void)
268 FILE *file
= fopen("/proc/kallsyms", "r");
273 while (!feof(file
)) {
280 line_len
= getline(&line
, &n
, file
);
287 line
[--line_len
] = '\0'; /* \n */
289 len
= hex2u64(line
, &start
);
292 if (len
+ 2 >= line_len
)
295 symbol_type
= toupper(line
[len
]);
297 * We're interested only in code ('T'ext)
299 if (symbol_type
!= 'T' && symbol_type
!= 'W')
302 symbol_name
= line
+ len
+ 2;
304 * Will fix up the end later, when we have all symbols sorted.
306 sym
= symbol__new(start
, 0, symbol_name
);
309 goto out_delete_line
;
312 * We will pass the symbols to the filter later, in
313 * kernel_maps__split_kallsyms, when we have split the
316 dso__insert_symbol(kernel_map
->dso
, sym
);
331 * Split the symbols into maps, making sure there are no overlaps, i.e. the
332 * kernel range is broken in several maps, named [kernel].N, as we don't have
333 * the original ELF section names vmlinux have.
335 static int kernel_maps__split_kallsyms(symbol_filter_t filter
)
337 struct map
*map
= kernel_map
;
340 struct rb_node
*next
= rb_first(&kernel_map
->dso
->syms
);
341 int kernel_range
= 0;
346 pos
= rb_entry(next
, struct symbol
, rb_node
);
347 next
= rb_next(&pos
->rb_node
);
349 module
= strchr(pos
->name
, '\t');
353 if (strcmp(map
->dso
->name
, module
)) {
354 map
= kernel_maps__find_by_dso_name(module
);
356 pr_err("/proc/{kallsyms,modules} "
362 * So that we look just like we get from .ko files,
363 * i.e. not prelinked, relative to map->start.
365 pos
->start
= map
->map_ip(map
, pos
->start
);
366 pos
->end
= map
->map_ip(map
, pos
->end
);
367 } else if (map
!= kernel_map
) {
368 char dso_name
[PATH_MAX
];
371 snprintf(dso_name
, sizeof(dso_name
), "[kernel].%d",
374 dso
= dso__new(dso_name
);
378 map
= map__new2(pos
->start
, dso
);
384 map
->map_ip
= map
->unmap_ip
= identity__map_ip
;
385 kernel_maps__insert(map
);
389 if (filter
&& filter(map
, pos
)) {
390 rb_erase(&pos
->rb_node
, &kernel_map
->dso
->syms
);
393 if (map
!= kernel_map
) {
394 rb_erase(&pos
->rb_node
, &kernel_map
->dso
->syms
);
395 dso__insert_symbol(map
->dso
, pos
);
405 static int kernel_maps__load_kallsyms(symbol_filter_t filter
)
407 if (kernel_maps__load_all_kallsyms())
410 dso__fixup_sym_end(kernel_map
->dso
);
411 kernel_map
->dso
->origin
= DSO__ORIG_KERNEL
;
413 return kernel_maps__split_kallsyms(filter
);
416 size_t kernel_maps__fprintf(FILE *fp
)
418 size_t printed
= fprintf(fp
, "Kernel maps:\n");
421 for (nd
= rb_first(&kernel_maps
); nd
; nd
= rb_next(nd
)) {
422 struct map
*pos
= rb_entry(nd
, struct map
, rb_node
);
424 printed
+= fprintf(fp
, "Map:");
425 printed
+= map__fprintf(pos
, fp
);
427 printed
+= dso__fprintf(pos
->dso
, fp
);
428 printed
+= fprintf(fp
, "--\n");
432 return printed
+ fprintf(fp
, "END kernel maps\n");
435 static int dso__load_perf_map(struct dso
*self
, struct map
*map
,
436 symbol_filter_t filter
)
443 file
= fopen(self
->long_name
, "r");
447 while (!feof(file
)) {
452 line_len
= getline(&line
, &n
, file
);
459 line
[--line_len
] = '\0'; /* \n */
461 len
= hex2u64(line
, &start
);
464 if (len
+ 2 >= line_len
)
467 len
+= hex2u64(line
+ len
, &size
);
470 if (len
+ 2 >= line_len
)
473 sym
= symbol__new(start
, size
, line
+ len
);
476 goto out_delete_line
;
478 if (filter
&& filter(map
, sym
))
481 dso__insert_symbol(self
, sym
);
498 * elf_symtab__for_each_symbol - iterate thru all the symbols
500 * @self: struct elf_symtab instance to iterate
502 * @sym: GElf_Sym iterator
504 #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
505 for (idx = 0, gelf_getsym(syms, idx, &sym);\
507 idx++, gelf_getsym(syms, idx, &sym))
509 static inline uint8_t elf_sym__type(const GElf_Sym
*sym
)
511 return GELF_ST_TYPE(sym
->st_info
);
514 static inline int elf_sym__is_function(const GElf_Sym
*sym
)
516 return elf_sym__type(sym
) == STT_FUNC
&&
518 sym
->st_shndx
!= SHN_UNDEF
;
521 static inline int elf_sym__is_label(const GElf_Sym
*sym
)
523 return elf_sym__type(sym
) == STT_NOTYPE
&&
525 sym
->st_shndx
!= SHN_UNDEF
&&
526 sym
->st_shndx
!= SHN_ABS
;
529 static inline const char *elf_sec__name(const GElf_Shdr
*shdr
,
530 const Elf_Data
*secstrs
)
532 return secstrs
->d_buf
+ shdr
->sh_name
;
535 static inline int elf_sec__is_text(const GElf_Shdr
*shdr
,
536 const Elf_Data
*secstrs
)
538 return strstr(elf_sec__name(shdr
, secstrs
), "text") != NULL
;
541 static inline const char *elf_sym__name(const GElf_Sym
*sym
,
542 const Elf_Data
*symstrs
)
544 return symstrs
->d_buf
+ sym
->st_name
;
547 static Elf_Scn
*elf_section_by_name(Elf
*elf
, GElf_Ehdr
*ep
,
548 GElf_Shdr
*shp
, const char *name
,
554 while ((sec
= elf_nextscn(elf
, sec
)) != NULL
) {
557 gelf_getshdr(sec
, shp
);
558 str
= elf_strptr(elf
, ep
->e_shstrndx
, shp
->sh_name
);
559 if (!strcmp(name
, str
)) {
570 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
571 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
573 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
575 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
576 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
578 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
581 * We need to check if we have a .dynsym, so that we can handle the
582 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
583 * .dynsym or .symtab).
584 * And always look at the original dso, not at debuginfo packages, that
585 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
587 static int dso__synthesize_plt_symbols(struct dso
*self
, struct map
*map
,
588 symbol_filter_t filter
)
590 uint32_t nr_rel_entries
, idx
;
595 GElf_Shdr shdr_rel_plt
, shdr_dynsym
;
596 Elf_Data
*reldata
, *syms
, *symstrs
;
597 Elf_Scn
*scn_plt_rel
, *scn_symstrs
, *scn_dynsym
;
600 char sympltname
[1024];
602 int nr
= 0, symidx
, fd
, err
= 0;
604 fd
= open(self
->long_name
, O_RDONLY
);
608 elf
= elf_begin(fd
, PERF_ELF_C_READ_MMAP
, NULL
);
612 if (gelf_getehdr(elf
, &ehdr
) == NULL
)
615 scn_dynsym
= elf_section_by_name(elf
, &ehdr
, &shdr_dynsym
,
616 ".dynsym", &dynsym_idx
);
617 if (scn_dynsym
== NULL
)
620 scn_plt_rel
= elf_section_by_name(elf
, &ehdr
, &shdr_rel_plt
,
622 if (scn_plt_rel
== NULL
) {
623 scn_plt_rel
= elf_section_by_name(elf
, &ehdr
, &shdr_rel_plt
,
625 if (scn_plt_rel
== NULL
)
631 if (shdr_rel_plt
.sh_link
!= dynsym_idx
)
634 if (elf_section_by_name(elf
, &ehdr
, &shdr_plt
, ".plt", NULL
) == NULL
)
638 * Fetch the relocation section to find the idxes to the GOT
639 * and the symbols in the .dynsym they refer to.
641 reldata
= elf_getdata(scn_plt_rel
, NULL
);
645 syms
= elf_getdata(scn_dynsym
, NULL
);
649 scn_symstrs
= elf_getscn(elf
, shdr_dynsym
.sh_link
);
650 if (scn_symstrs
== NULL
)
653 symstrs
= elf_getdata(scn_symstrs
, NULL
);
657 nr_rel_entries
= shdr_rel_plt
.sh_size
/ shdr_rel_plt
.sh_entsize
;
658 plt_offset
= shdr_plt
.sh_offset
;
660 if (shdr_rel_plt
.sh_type
== SHT_RELA
) {
661 GElf_Rela pos_mem
, *pos
;
663 elf_section__for_each_rela(reldata
, pos
, pos_mem
, idx
,
665 symidx
= GELF_R_SYM(pos
->r_info
);
666 plt_offset
+= shdr_plt
.sh_entsize
;
667 gelf_getsym(syms
, symidx
, &sym
);
668 snprintf(sympltname
, sizeof(sympltname
),
669 "%s@plt", elf_sym__name(&sym
, symstrs
));
671 f
= symbol__new(plt_offset
, shdr_plt
.sh_entsize
,
676 if (filter
&& filter(map
, f
))
679 dso__insert_symbol(self
, f
);
683 } else if (shdr_rel_plt
.sh_type
== SHT_REL
) {
684 GElf_Rel pos_mem
, *pos
;
685 elf_section__for_each_rel(reldata
, pos
, pos_mem
, idx
,
687 symidx
= GELF_R_SYM(pos
->r_info
);
688 plt_offset
+= shdr_plt
.sh_entsize
;
689 gelf_getsym(syms
, symidx
, &sym
);
690 snprintf(sympltname
, sizeof(sympltname
),
691 "%s@plt", elf_sym__name(&sym
, symstrs
));
693 f
= symbol__new(plt_offset
, shdr_plt
.sh_entsize
,
698 if (filter
&& filter(map
, f
))
701 dso__insert_symbol(self
, f
);
716 pr_warning("%s: problems reading %s PLT info.\n",
717 __func__
, self
->long_name
);
721 static int dso__load_sym(struct dso
*self
, struct map
*map
, const char *name
,
722 int fd
, symbol_filter_t filter
, int kernel
,
725 struct map
*curr_map
= map
;
726 struct dso
*curr_dso
= self
;
727 size_t dso_name_len
= strlen(self
->short_name
);
728 Elf_Data
*symstrs
, *secstrs
;
736 Elf_Scn
*sec
, *sec_strndx
;
740 elf
= elf_begin(fd
, PERF_ELF_C_READ_MMAP
, NULL
);
742 pr_err("%s: cannot read %s ELF file.\n", __func__
, name
);
746 if (gelf_getehdr(elf
, &ehdr
) == NULL
) {
747 pr_err("%s: cannot get elf header.\n", __func__
);
751 sec
= elf_section_by_name(elf
, &ehdr
, &shdr
, ".symtab", NULL
);
753 sec
= elf_section_by_name(elf
, &ehdr
, &shdr
, ".dynsym", NULL
);
758 syms
= elf_getdata(sec
, NULL
);
762 sec
= elf_getscn(elf
, shdr
.sh_link
);
766 symstrs
= elf_getdata(sec
, NULL
);
770 sec_strndx
= elf_getscn(elf
, ehdr
.e_shstrndx
);
771 if (sec_strndx
== NULL
)
774 secstrs
= elf_getdata(sec_strndx
, NULL
);
778 nr_syms
= shdr
.sh_size
/ shdr
.sh_entsize
;
780 memset(&sym
, 0, sizeof(sym
));
782 self
->adjust_symbols
= (ehdr
.e_type
== ET_EXEC
||
783 elf_section_by_name(elf
, &ehdr
, &shdr
,
786 } else self
->adjust_symbols
= 0;
788 elf_symtab__for_each_symbol(syms
, nr_syms
, idx
, sym
) {
790 const char *elf_name
;
791 char *demangled
= NULL
;
792 int is_label
= elf_sym__is_label(&sym
);
793 const char *section_name
;
795 if (!is_label
&& !elf_sym__is_function(&sym
))
798 sec
= elf_getscn(elf
, sym
.st_shndx
);
802 gelf_getshdr(sec
, &shdr
);
804 if (is_label
&& !elf_sec__is_text(&shdr
, secstrs
))
807 elf_name
= elf_sym__name(&sym
, symstrs
);
808 section_name
= elf_sec__name(&shdr
, secstrs
);
810 if (kernel
|| kmodule
) {
811 char dso_name
[PATH_MAX
];
813 if (strcmp(section_name
,
814 curr_dso
->short_name
+ dso_name_len
) == 0)
817 if (strcmp(section_name
, ".text") == 0) {
823 snprintf(dso_name
, sizeof(dso_name
),
824 "%s%s", self
->short_name
, section_name
);
826 curr_map
= kernel_maps__find_by_dso_name(dso_name
);
827 if (curr_map
== NULL
) {
828 u64 start
= sym
.st_value
;
831 start
+= map
->start
+ shdr
.sh_offset
;
833 curr_dso
= dso__new(dso_name
);
834 if (curr_dso
== NULL
)
836 curr_map
= map__new2(start
, curr_dso
);
837 if (curr_map
== NULL
) {
838 dso__delete(curr_dso
);
841 curr_map
->map_ip
= identity__map_ip
;
842 curr_map
->unmap_ip
= identity__map_ip
;
843 curr_dso
->origin
= DSO__ORIG_KERNEL
;
844 kernel_maps__insert(curr_map
);
847 curr_dso
= curr_map
->dso
;
852 if (curr_dso
->adjust_symbols
) {
853 pr_debug2("adjusting symbol: st_value: %Lx sh_addr: "
854 "%Lx sh_offset: %Lx\n", (u64
)sym
.st_value
,
855 (u64
)shdr
.sh_addr
, (u64
)shdr
.sh_offset
);
856 sym
.st_value
-= shdr
.sh_addr
- shdr
.sh_offset
;
859 * We need to figure out if the object was created from C++ sources
860 * DWARF DW_compile_unit has this, but we don't always have access
863 demangled
= bfd_demangle(NULL
, elf_name
, DMGL_PARAMS
| DMGL_ANSI
);
864 if (demangled
!= NULL
)
865 elf_name
= demangled
;
867 f
= symbol__new(sym
.st_value
, sym
.st_size
, elf_name
);
872 if (filter
&& filter(curr_map
, f
))
875 dso__insert_symbol(curr_dso
, f
);
881 * For misannotated, zeroed, ASM function sizes.
884 dso__fixup_sym_end(self
);
892 static bool dso__build_id_equal(const struct dso
*self
, u8
*build_id
)
894 return memcmp(self
->build_id
, build_id
, sizeof(self
->build_id
)) == 0;
897 bool dsos__read_build_ids(void)
899 bool have_build_id
= false;
902 list_for_each_entry(pos
, &dsos
, node
)
903 if (filename__read_build_id(pos
->long_name
, pos
->build_id
,
904 sizeof(pos
->build_id
)) > 0) {
905 have_build_id
= true;
906 pos
->has_build_id
= true;
909 return have_build_id
;
913 * Align offset to 4 bytes as needed for note name and descriptor data.
915 #define NOTE_ALIGN(n) (((n) + 3) & -4U)
917 int filename__read_build_id(const char *filename
, void *bf
, size_t size
)
927 if (size
< BUILD_ID_SIZE
)
930 fd
= open(filename
, O_RDONLY
);
934 elf
= elf_begin(fd
, PERF_ELF_C_READ_MMAP
, NULL
);
936 pr_debug2("%s: cannot read %s ELF file.\n", __func__
, filename
);
940 if (gelf_getehdr(elf
, &ehdr
) == NULL
) {
941 pr_err("%s: cannot get elf header.\n", __func__
);
945 sec
= elf_section_by_name(elf
, &ehdr
, &shdr
,
946 ".note.gnu.build-id", NULL
);
948 sec
= elf_section_by_name(elf
, &ehdr
, &shdr
,
954 data
= elf_getdata(sec
, NULL
);
959 while (ptr
< (data
->d_buf
+ data
->d_size
)) {
960 GElf_Nhdr
*nhdr
= ptr
;
961 int namesz
= NOTE_ALIGN(nhdr
->n_namesz
),
962 descsz
= NOTE_ALIGN(nhdr
->n_descsz
);
965 ptr
+= sizeof(*nhdr
);
968 if (nhdr
->n_type
== NT_GNU_BUILD_ID
&&
969 nhdr
->n_namesz
== sizeof("GNU")) {
970 if (memcmp(name
, "GNU", sizeof("GNU")) == 0) {
971 memcpy(bf
, ptr
, BUILD_ID_SIZE
);
986 int sysfs__read_build_id(const char *filename
, void *build_id
, size_t size
)
990 if (size
< BUILD_ID_SIZE
)
993 fd
= open(filename
, O_RDONLY
);
1002 if (read(fd
, &nhdr
, sizeof(nhdr
)) != sizeof(nhdr
))
1005 namesz
= NOTE_ALIGN(nhdr
.n_namesz
);
1006 descsz
= NOTE_ALIGN(nhdr
.n_descsz
);
1007 if (nhdr
.n_type
== NT_GNU_BUILD_ID
&&
1008 nhdr
.n_namesz
== sizeof("GNU")) {
1009 if (read(fd
, bf
, namesz
) != namesz
)
1011 if (memcmp(bf
, "GNU", sizeof("GNU")) == 0) {
1012 if (read(fd
, build_id
,
1013 BUILD_ID_SIZE
) == BUILD_ID_SIZE
) {
1017 } else if (read(fd
, bf
, descsz
) != descsz
)
1020 int n
= namesz
+ descsz
;
1021 if (read(fd
, bf
, n
) != n
)
1030 char dso__symtab_origin(const struct dso
*self
)
1032 static const char origin
[] = {
1033 [DSO__ORIG_KERNEL
] = 'k',
1034 [DSO__ORIG_JAVA_JIT
] = 'j',
1035 [DSO__ORIG_FEDORA
] = 'f',
1036 [DSO__ORIG_UBUNTU
] = 'u',
1037 [DSO__ORIG_BUILDID
] = 'b',
1038 [DSO__ORIG_DSO
] = 'd',
1039 [DSO__ORIG_KMODULE
] = 'K',
1042 if (self
== NULL
|| self
->origin
== DSO__ORIG_NOT_FOUND
)
1044 return origin
[self
->origin
];
1047 int dso__load(struct dso
*self
, struct map
*map
, symbol_filter_t filter
)
1049 int size
= PATH_MAX
;
1051 u8 build_id
[BUILD_ID_SIZE
];
1058 return dso__load_kernel_sym(self
, map
, filter
);
1060 name
= malloc(size
);
1064 self
->adjust_symbols
= 0;
1066 if (strncmp(self
->name
, "/tmp/perf-", 10) == 0) {
1067 ret
= dso__load_perf_map(self
, map
, filter
);
1068 self
->origin
= ret
> 0 ? DSO__ORIG_JAVA_JIT
:
1069 DSO__ORIG_NOT_FOUND
;
1073 self
->origin
= DSO__ORIG_FEDORA
- 1;
1078 switch (self
->origin
) {
1079 case DSO__ORIG_FEDORA
:
1080 snprintf(name
, size
, "/usr/lib/debug%s.debug",
1083 case DSO__ORIG_UBUNTU
:
1084 snprintf(name
, size
, "/usr/lib/debug%s",
1087 case DSO__ORIG_BUILDID
:
1088 if (filename__read_build_id(self
->long_name
, build_id
,
1089 sizeof(build_id
))) {
1090 char build_id_hex
[BUILD_ID_SIZE
* 2 + 1];
1092 build_id__sprintf(build_id
, sizeof(build_id
),
1094 snprintf(name
, size
,
1095 "/usr/lib/debug/.build-id/%.2s/%s.debug",
1096 build_id_hex
, build_id_hex
+ 2);
1097 if (self
->has_build_id
)
1098 goto compare_build_id
;
1104 snprintf(name
, size
, "%s", self
->long_name
);
1111 if (self
->has_build_id
) {
1112 if (filename__read_build_id(name
, build_id
,
1113 sizeof(build_id
)) < 0)
1116 if (!dso__build_id_equal(self
, build_id
))
1120 fd
= open(name
, O_RDONLY
);
1123 ret
= dso__load_sym(self
, map
, name
, fd
, filter
, 0, 0);
1127 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
1133 int nr_plt
= dso__synthesize_plt_symbols(self
, map
, filter
);
1139 if (ret
< 0 && strstr(self
->name
, " (deleted)") != NULL
)
1144 struct map
*kernel_map
;
1146 static void kernel_maps__insert(struct map
*map
)
1148 maps__insert(&kernel_maps
, map
);
1151 struct symbol
*kernel_maps__find_symbol(u64 ip
, struct map
**mapp
,
1152 symbol_filter_t filter
)
1154 struct map
*map
= maps__find(&kernel_maps
, ip
);
1160 ip
= map
->map_ip(map
, ip
);
1161 return map__find_symbol(map
, ip
, filter
);
1167 struct map
*kernel_maps__find_by_dso_name(const char *name
)
1171 for (nd
= rb_first(&kernel_maps
); nd
; nd
= rb_next(nd
)) {
1172 struct map
*map
= rb_entry(nd
, struct map
, rb_node
);
1174 if (map
->dso
&& strcmp(map
->dso
->name
, name
) == 0)
1181 static int dsos__set_modules_path_dir(char *dirname
)
1183 struct dirent
*dent
;
1184 DIR *dir
= opendir(dirname
);
1187 pr_err("%s: cannot open %s dir\n", __func__
, dirname
);
1191 while ((dent
= readdir(dir
)) != NULL
) {
1192 char path
[PATH_MAX
];
1194 if (dent
->d_type
== DT_DIR
) {
1195 if (!strcmp(dent
->d_name
, ".") ||
1196 !strcmp(dent
->d_name
, ".."))
1199 snprintf(path
, sizeof(path
), "%s/%s",
1200 dirname
, dent
->d_name
);
1201 if (dsos__set_modules_path_dir(path
) < 0)
1204 char *dot
= strrchr(dent
->d_name
, '.'),
1209 if (dot
== NULL
|| strcmp(dot
, ".ko"))
1211 snprintf(dso_name
, sizeof(dso_name
), "[%.*s]",
1212 (int)(dot
- dent
->d_name
), dent
->d_name
);
1214 strxfrchar(dso_name
, '-', '_');
1215 map
= kernel_maps__find_by_dso_name(dso_name
);
1219 snprintf(path
, sizeof(path
), "%s/%s",
1220 dirname
, dent
->d_name
);
1222 long_name
= strdup(path
);
1223 if (long_name
== NULL
)
1225 dso__set_long_name(map
->dso
, long_name
);
1235 static int dsos__set_modules_path(void)
1238 char modules_path
[PATH_MAX
];
1240 if (uname(&uts
) < 0)
1243 snprintf(modules_path
, sizeof(modules_path
), "/lib/modules/%s/kernel",
1246 return dsos__set_modules_path_dir(modules_path
);
1250 * Constructor variant for modules (where we know from /proc/modules where
1251 * they are loaded) and for vmlinux, where only after we load all the
1252 * symbols we'll know where it starts and ends.
1254 static struct map
*map__new2(u64 start
, struct dso
*dso
)
1256 struct map
*self
= malloc(sizeof(*self
));
1260 * ->end will be filled after we load all the symbols
1262 map__init(self
, start
, 0, 0, dso
);
1268 static int kernel_maps__create_module_maps(void)
1272 FILE *file
= fopen("/proc/modules", "r");
1278 while (!feof(file
)) {
1279 char name
[PATH_MAX
];
1285 line_len
= getline(&line
, &n
, file
);
1292 line
[--line_len
] = '\0'; /* \n */
1294 sep
= strrchr(line
, 'x');
1298 hex2u64(sep
+ 1, &start
);
1300 sep
= strchr(line
, ' ');
1306 snprintf(name
, sizeof(name
), "[%s]", line
);
1307 dso
= dso__new(name
);
1310 goto out_delete_line
;
1312 map
= map__new2(start
, dso
);
1315 goto out_delete_line
;
1318 snprintf(name
, sizeof(name
),
1319 "/sys/module/%s/notes/.note.gnu.build-id", line
);
1320 if (sysfs__read_build_id(name
, dso
->build_id
,
1321 sizeof(dso
->build_id
)) == 0)
1322 dso
->has_build_id
= true;
1324 dso
->origin
= DSO__ORIG_KMODULE
;
1325 kernel_maps__insert(map
);
1332 return dsos__set_modules_path();
1340 static int dso__load_vmlinux(struct dso
*self
, struct map
*map
,
1341 const char *vmlinux
, symbol_filter_t filter
)
1345 if (self
->has_build_id
) {
1346 u8 build_id
[BUILD_ID_SIZE
];
1348 if (filename__read_build_id(vmlinux
, build_id
,
1349 sizeof(build_id
)) < 0) {
1350 pr_debug("No build_id in %s, ignoring it\n", vmlinux
);
1353 if (!dso__build_id_equal(self
, build_id
)) {
1354 char expected_build_id
[BUILD_ID_SIZE
* 2 + 1],
1355 vmlinux_build_id
[BUILD_ID_SIZE
* 2 + 1];
1357 build_id__sprintf(self
->build_id
,
1358 sizeof(self
->build_id
),
1360 build_id__sprintf(build_id
, sizeof(build_id
),
1362 pr_debug("build_id in %s is %s while expected is %s, "
1363 "ignoring it\n", vmlinux
, vmlinux_build_id
,
1369 fd
= open(vmlinux
, O_RDONLY
);
1374 err
= dso__load_sym(self
, map
, self
->long_name
, fd
, filter
, 1, 0);
1381 static int dso__load_kernel_sym(struct dso
*self
, struct map
*map
,
1382 symbol_filter_t filter
)
1384 int err
= dso__load_vmlinux(self
, map
, self
->name
, filter
);
1387 err
= kernel_maps__load_kallsyms(filter
);
1389 dso__set_long_name(self
, strdup("[kernel.kallsyms]"));
1393 map__fixup_start(map
);
1394 map__fixup_end(map
);
1403 const char *vmlinux_name
= "vmlinux";
1405 static void dsos__add(struct dso
*dso
)
1407 list_add_tail(&dso
->node
, &dsos
);
1410 static struct dso
*dsos__find(const char *name
)
1414 list_for_each_entry(pos
, &dsos
, node
)
1415 if (strcmp(pos
->name
, name
) == 0)
1420 struct dso
*dsos__findnew(const char *name
)
1422 struct dso
*dso
= dsos__find(name
);
1425 dso
= dso__new(name
);
1428 dso__set_basename(dso
);
1435 void dsos__fprintf(FILE *fp
)
1439 list_for_each_entry(pos
, &dsos
, node
)
1440 dso__fprintf(pos
, fp
);
1443 size_t dsos__fprintf_buildid(FILE *fp
)
1448 list_for_each_entry(pos
, &dsos
, node
) {
1449 ret
+= dso__fprintf_buildid(pos
, fp
);
1450 ret
+= fprintf(fp
, " %s\n", pos
->long_name
);
1455 static int kernel_maps__create_kernel_map(void)
1457 struct dso
*kernel
= dso__new(vmlinux_name
);
1462 kernel_map
= map__new2(0, kernel
);
1463 if (kernel_map
== NULL
)
1464 goto out_delete_kernel_dso
;
1466 kernel_map
->map_ip
= kernel_map
->unmap_ip
= identity__map_ip
;
1468 kernel
->short_name
= "[kernel]";
1470 vdso
= dso__new("[vdso]");
1472 goto out_delete_kernel_map
;
1474 if (sysfs__read_build_id("/sys/kernel/notes", kernel
->build_id
,
1475 sizeof(kernel
->build_id
)) == 0)
1476 kernel
->has_build_id
= true;
1478 kernel_maps__insert(kernel_map
);
1484 out_delete_kernel_map
:
1485 map__delete(kernel_map
);
1487 out_delete_kernel_dso
:
1488 dso__delete(kernel
);
1492 int kernel_maps__init(bool use_modules
)
1494 if (kernel_maps__create_kernel_map() < 0)
1497 if (use_modules
&& kernel_maps__create_module_maps() < 0)
1498 pr_warning("Failed to load list of modules in use, "
1501 * Now that we have all the maps created, just set the ->end of them:
1503 kernel_maps__fixup_end();
1507 void symbol__init(unsigned int priv_size
)
1509 elf_version(EV_CURRENT
);
1510 symbol__priv_size
= priv_size
;
This page took 0.113608 seconds and 5 git commands to generate.