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
;
37 static int vmlinux_path__nr_entries
;
38 static char **vmlinux_path
;
40 static struct rb_root kernel_maps
;
42 static void dso__fixup_sym_end(struct dso
*self
)
44 struct rb_node
*nd
, *prevnd
= rb_first(&self
->syms
);
45 struct symbol
*curr
, *prev
;
50 curr
= rb_entry(prevnd
, struct symbol
, rb_node
);
52 for (nd
= rb_next(prevnd
); nd
; nd
= rb_next(nd
)) {
54 curr
= rb_entry(nd
, struct symbol
, rb_node
);
56 if (prev
->end
== prev
->start
)
57 prev
->end
= curr
->start
- 1;
61 if (curr
->end
== curr
->start
)
62 curr
->end
= roundup(curr
->start
, 4096);
65 static void kernel_maps__fixup_end(void)
67 struct map
*prev
, *curr
;
68 struct rb_node
*nd
, *prevnd
= rb_first(&kernel_maps
);
73 curr
= rb_entry(prevnd
, struct map
, rb_node
);
75 for (nd
= rb_next(prevnd
); nd
; nd
= rb_next(nd
)) {
77 curr
= rb_entry(nd
, struct map
, rb_node
);
78 prev
->end
= curr
->start
- 1;
82 * We still haven't the actual symbols, so guess the
83 * last map final address.
88 static struct symbol
*symbol__new(u64 start
, u64 len
, const char *name
)
90 size_t namelen
= strlen(name
) + 1;
91 struct symbol
*self
= calloc(1, (symbol__priv_size
+
92 sizeof(*self
) + namelen
));
96 if (symbol__priv_size
) {
97 memset(self
, 0, symbol__priv_size
);
98 self
= ((void *)self
) + symbol__priv_size
;
101 self
->end
= len
? start
+ len
- 1 : start
;
103 pr_debug3("%s: %s %#Lx-%#Lx\n", __func__
, name
, start
, self
->end
);
105 memcpy(self
->name
, name
, namelen
);
110 static void symbol__delete(struct symbol
*self
)
112 free(((void *)self
) - symbol__priv_size
);
115 static size_t symbol__fprintf(struct symbol
*self
, FILE *fp
)
117 return fprintf(fp
, " %llx-%llx %s\n",
118 self
->start
, self
->end
, self
->name
);
121 static void dso__set_long_name(struct dso
*self
, char *name
)
125 self
->long_name
= name
;
126 self
->long_name_len
= strlen(name
);
129 static void dso__set_basename(struct dso
*self
)
131 self
->short_name
= basename(self
->long_name
);
134 struct dso
*dso__new(const char *name
)
136 struct dso
*self
= malloc(sizeof(*self
) + strlen(name
) + 1);
139 strcpy(self
->name
, name
);
140 dso__set_long_name(self
, self
->name
);
141 self
->short_name
= self
->name
;
142 self
->syms
= RB_ROOT
;
143 self
->find_symbol
= dso__find_symbol
;
144 self
->slen_calculated
= 0;
145 self
->origin
= DSO__ORIG_NOT_FOUND
;
147 self
->has_build_id
= 0;
153 static void dso__delete_symbols(struct dso
*self
)
156 struct rb_node
*next
= rb_first(&self
->syms
);
159 pos
= rb_entry(next
, struct symbol
, rb_node
);
160 next
= rb_next(&pos
->rb_node
);
161 rb_erase(&pos
->rb_node
, &self
->syms
);
166 void dso__delete(struct dso
*self
)
168 dso__delete_symbols(self
);
169 if (self
->long_name
!= self
->name
)
170 free(self
->long_name
);
174 void dso__set_build_id(struct dso
*self
, void *build_id
)
176 memcpy(self
->build_id
, build_id
, sizeof(self
->build_id
));
177 self
->has_build_id
= 1;
180 static void dso__insert_symbol(struct dso
*self
, struct symbol
*sym
)
182 struct rb_node
**p
= &self
->syms
.rb_node
;
183 struct rb_node
*parent
= NULL
;
184 const u64 ip
= sym
->start
;
189 s
= rb_entry(parent
, struct symbol
, rb_node
);
195 rb_link_node(&sym
->rb_node
, parent
, p
);
196 rb_insert_color(&sym
->rb_node
, &self
->syms
);
199 struct symbol
*dso__find_symbol(struct dso
*self
, u64 ip
)
206 n
= self
->syms
.rb_node
;
209 struct symbol
*s
= rb_entry(n
, struct symbol
, rb_node
);
213 else if (ip
> s
->end
)
222 int build_id__sprintf(u8
*self
, int len
, char *bf
)
228 for (i
= 0; i
< len
; ++i
) {
229 sprintf(bid
, "%02x", *raw
);
237 size_t dso__fprintf_buildid(struct dso
*self
, FILE *fp
)
239 char sbuild_id
[BUILD_ID_SIZE
* 2 + 1];
241 build_id__sprintf(self
->build_id
, sizeof(self
->build_id
), sbuild_id
);
242 return fprintf(fp
, "%s", sbuild_id
);
245 size_t dso__fprintf(struct dso
*self
, FILE *fp
)
248 size_t ret
= fprintf(fp
, "dso: %s (", self
->short_name
);
250 ret
+= dso__fprintf_buildid(self
, fp
);
251 ret
+= fprintf(fp
, ")\n");
253 for (nd
= rb_first(&self
->syms
); nd
; nd
= rb_next(nd
)) {
254 struct symbol
*pos
= rb_entry(nd
, struct symbol
, rb_node
);
255 ret
+= symbol__fprintf(pos
, fp
);
262 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
263 * so that we can in the next step set the symbol ->end address and then
264 * call kernel_maps__split_kallsyms.
266 static int kernel_maps__load_all_kallsyms(void)
270 FILE *file
= fopen("/proc/kallsyms", "r");
275 while (!feof(file
)) {
282 line_len
= getline(&line
, &n
, file
);
289 line
[--line_len
] = '\0'; /* \n */
291 len
= hex2u64(line
, &start
);
294 if (len
+ 2 >= line_len
)
297 symbol_type
= toupper(line
[len
]);
299 * We're interested only in code ('T'ext)
301 if (symbol_type
!= 'T' && symbol_type
!= 'W')
304 symbol_name
= line
+ len
+ 2;
306 * Will fix up the end later, when we have all symbols sorted.
308 sym
= symbol__new(start
, 0, symbol_name
);
311 goto out_delete_line
;
314 * We will pass the symbols to the filter later, in
315 * kernel_maps__split_kallsyms, when we have split the
318 dso__insert_symbol(kernel_map
->dso
, sym
);
333 * Split the symbols into maps, making sure there are no overlaps, i.e. the
334 * kernel range is broken in several maps, named [kernel].N, as we don't have
335 * the original ELF section names vmlinux have.
337 static int kernel_maps__split_kallsyms(symbol_filter_t filter
)
339 struct map
*map
= kernel_map
;
342 struct rb_node
*next
= rb_first(&kernel_map
->dso
->syms
);
343 int kernel_range
= 0;
348 pos
= rb_entry(next
, struct symbol
, rb_node
);
349 next
= rb_next(&pos
->rb_node
);
351 module
= strchr(pos
->name
, '\t');
355 if (strcmp(map
->dso
->name
, module
)) {
356 map
= kernel_maps__find_by_dso_name(module
);
358 pr_err("/proc/{kallsyms,modules} "
364 * So that we look just like we get from .ko files,
365 * i.e. not prelinked, relative to map->start.
367 pos
->start
= map
->map_ip(map
, pos
->start
);
368 pos
->end
= map
->map_ip(map
, pos
->end
);
369 } else if (map
!= kernel_map
) {
370 char dso_name
[PATH_MAX
];
373 snprintf(dso_name
, sizeof(dso_name
), "[kernel].%d",
376 dso
= dso__new(dso_name
);
380 map
= map__new2(pos
->start
, dso
);
386 map
->map_ip
= map
->unmap_ip
= identity__map_ip
;
387 kernel_maps__insert(map
);
391 if (filter
&& filter(map
, pos
)) {
392 rb_erase(&pos
->rb_node
, &kernel_map
->dso
->syms
);
395 if (map
!= kernel_map
) {
396 rb_erase(&pos
->rb_node
, &kernel_map
->dso
->syms
);
397 dso__insert_symbol(map
->dso
, pos
);
407 static int kernel_maps__load_kallsyms(symbol_filter_t filter
)
409 if (kernel_maps__load_all_kallsyms())
412 dso__fixup_sym_end(kernel_map
->dso
);
413 kernel_map
->dso
->origin
= DSO__ORIG_KERNEL
;
415 return kernel_maps__split_kallsyms(filter
);
418 size_t kernel_maps__fprintf(FILE *fp
)
420 size_t printed
= fprintf(fp
, "Kernel maps:\n");
423 for (nd
= rb_first(&kernel_maps
); nd
; nd
= rb_next(nd
)) {
424 struct map
*pos
= rb_entry(nd
, struct map
, rb_node
);
426 printed
+= fprintf(fp
, "Map:");
427 printed
+= map__fprintf(pos
, fp
);
429 printed
+= dso__fprintf(pos
->dso
, fp
);
430 printed
+= fprintf(fp
, "--\n");
434 return printed
+ fprintf(fp
, "END kernel maps\n");
437 static int dso__load_perf_map(struct dso
*self
, struct map
*map
,
438 symbol_filter_t filter
)
445 file
= fopen(self
->long_name
, "r");
449 while (!feof(file
)) {
454 line_len
= getline(&line
, &n
, file
);
461 line
[--line_len
] = '\0'; /* \n */
463 len
= hex2u64(line
, &start
);
466 if (len
+ 2 >= line_len
)
469 len
+= hex2u64(line
+ len
, &size
);
472 if (len
+ 2 >= line_len
)
475 sym
= symbol__new(start
, size
, line
+ len
);
478 goto out_delete_line
;
480 if (filter
&& filter(map
, sym
))
483 dso__insert_symbol(self
, sym
);
500 * elf_symtab__for_each_symbol - iterate thru all the symbols
502 * @self: struct elf_symtab instance to iterate
504 * @sym: GElf_Sym iterator
506 #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
507 for (idx = 0, gelf_getsym(syms, idx, &sym);\
509 idx++, gelf_getsym(syms, idx, &sym))
511 static inline uint8_t elf_sym__type(const GElf_Sym
*sym
)
513 return GELF_ST_TYPE(sym
->st_info
);
516 static inline int elf_sym__is_function(const GElf_Sym
*sym
)
518 return elf_sym__type(sym
) == STT_FUNC
&&
520 sym
->st_shndx
!= SHN_UNDEF
;
523 static inline int elf_sym__is_label(const GElf_Sym
*sym
)
525 return elf_sym__type(sym
) == STT_NOTYPE
&&
527 sym
->st_shndx
!= SHN_UNDEF
&&
528 sym
->st_shndx
!= SHN_ABS
;
531 static inline const char *elf_sec__name(const GElf_Shdr
*shdr
,
532 const Elf_Data
*secstrs
)
534 return secstrs
->d_buf
+ shdr
->sh_name
;
537 static inline int elf_sec__is_text(const GElf_Shdr
*shdr
,
538 const Elf_Data
*secstrs
)
540 return strstr(elf_sec__name(shdr
, secstrs
), "text") != NULL
;
543 static inline const char *elf_sym__name(const GElf_Sym
*sym
,
544 const Elf_Data
*symstrs
)
546 return symstrs
->d_buf
+ sym
->st_name
;
549 static Elf_Scn
*elf_section_by_name(Elf
*elf
, GElf_Ehdr
*ep
,
550 GElf_Shdr
*shp
, const char *name
,
556 while ((sec
= elf_nextscn(elf
, sec
)) != NULL
) {
559 gelf_getshdr(sec
, shp
);
560 str
= elf_strptr(elf
, ep
->e_shstrndx
, shp
->sh_name
);
561 if (!strcmp(name
, str
)) {
572 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
573 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
575 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
577 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
578 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
580 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
583 * We need to check if we have a .dynsym, so that we can handle the
584 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
585 * .dynsym or .symtab).
586 * And always look at the original dso, not at debuginfo packages, that
587 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
589 static int dso__synthesize_plt_symbols(struct dso
*self
, struct map
*map
,
590 symbol_filter_t filter
)
592 uint32_t nr_rel_entries
, idx
;
597 GElf_Shdr shdr_rel_plt
, shdr_dynsym
;
598 Elf_Data
*reldata
, *syms
, *symstrs
;
599 Elf_Scn
*scn_plt_rel
, *scn_symstrs
, *scn_dynsym
;
602 char sympltname
[1024];
604 int nr
= 0, symidx
, fd
, err
= 0;
606 fd
= open(self
->long_name
, O_RDONLY
);
610 elf
= elf_begin(fd
, PERF_ELF_C_READ_MMAP
, NULL
);
614 if (gelf_getehdr(elf
, &ehdr
) == NULL
)
617 scn_dynsym
= elf_section_by_name(elf
, &ehdr
, &shdr_dynsym
,
618 ".dynsym", &dynsym_idx
);
619 if (scn_dynsym
== NULL
)
622 scn_plt_rel
= elf_section_by_name(elf
, &ehdr
, &shdr_rel_plt
,
624 if (scn_plt_rel
== NULL
) {
625 scn_plt_rel
= elf_section_by_name(elf
, &ehdr
, &shdr_rel_plt
,
627 if (scn_plt_rel
== NULL
)
633 if (shdr_rel_plt
.sh_link
!= dynsym_idx
)
636 if (elf_section_by_name(elf
, &ehdr
, &shdr_plt
, ".plt", NULL
) == NULL
)
640 * Fetch the relocation section to find the idxes to the GOT
641 * and the symbols in the .dynsym they refer to.
643 reldata
= elf_getdata(scn_plt_rel
, NULL
);
647 syms
= elf_getdata(scn_dynsym
, NULL
);
651 scn_symstrs
= elf_getscn(elf
, shdr_dynsym
.sh_link
);
652 if (scn_symstrs
== NULL
)
655 symstrs
= elf_getdata(scn_symstrs
, NULL
);
659 nr_rel_entries
= shdr_rel_plt
.sh_size
/ shdr_rel_plt
.sh_entsize
;
660 plt_offset
= shdr_plt
.sh_offset
;
662 if (shdr_rel_plt
.sh_type
== SHT_RELA
) {
663 GElf_Rela pos_mem
, *pos
;
665 elf_section__for_each_rela(reldata
, pos
, pos_mem
, idx
,
667 symidx
= GELF_R_SYM(pos
->r_info
);
668 plt_offset
+= shdr_plt
.sh_entsize
;
669 gelf_getsym(syms
, symidx
, &sym
);
670 snprintf(sympltname
, sizeof(sympltname
),
671 "%s@plt", elf_sym__name(&sym
, symstrs
));
673 f
= symbol__new(plt_offset
, shdr_plt
.sh_entsize
,
678 if (filter
&& filter(map
, f
))
681 dso__insert_symbol(self
, f
);
685 } else if (shdr_rel_plt
.sh_type
== SHT_REL
) {
686 GElf_Rel pos_mem
, *pos
;
687 elf_section__for_each_rel(reldata
, pos
, pos_mem
, idx
,
689 symidx
= GELF_R_SYM(pos
->r_info
);
690 plt_offset
+= shdr_plt
.sh_entsize
;
691 gelf_getsym(syms
, symidx
, &sym
);
692 snprintf(sympltname
, sizeof(sympltname
),
693 "%s@plt", elf_sym__name(&sym
, symstrs
));
695 f
= symbol__new(plt_offset
, shdr_plt
.sh_entsize
,
700 if (filter
&& filter(map
, f
))
703 dso__insert_symbol(self
, f
);
718 pr_warning("%s: problems reading %s PLT info.\n",
719 __func__
, self
->long_name
);
723 static int dso__load_sym(struct dso
*self
, struct map
*map
, const char *name
,
724 int fd
, symbol_filter_t filter
, int kernel
,
727 struct map
*curr_map
= map
;
728 struct dso
*curr_dso
= self
;
729 size_t dso_name_len
= strlen(self
->short_name
);
730 Elf_Data
*symstrs
, *secstrs
;
738 Elf_Scn
*sec
, *sec_strndx
;
742 elf
= elf_begin(fd
, PERF_ELF_C_READ_MMAP
, NULL
);
744 pr_err("%s: cannot read %s ELF file.\n", __func__
, name
);
748 if (gelf_getehdr(elf
, &ehdr
) == NULL
) {
749 pr_err("%s: cannot get elf header.\n", __func__
);
753 sec
= elf_section_by_name(elf
, &ehdr
, &shdr
, ".symtab", NULL
);
755 sec
= elf_section_by_name(elf
, &ehdr
, &shdr
, ".dynsym", NULL
);
760 syms
= elf_getdata(sec
, NULL
);
764 sec
= elf_getscn(elf
, shdr
.sh_link
);
768 symstrs
= elf_getdata(sec
, NULL
);
772 sec_strndx
= elf_getscn(elf
, ehdr
.e_shstrndx
);
773 if (sec_strndx
== NULL
)
776 secstrs
= elf_getdata(sec_strndx
, NULL
);
780 nr_syms
= shdr
.sh_size
/ shdr
.sh_entsize
;
782 memset(&sym
, 0, sizeof(sym
));
784 self
->adjust_symbols
= (ehdr
.e_type
== ET_EXEC
||
785 elf_section_by_name(elf
, &ehdr
, &shdr
,
788 } else self
->adjust_symbols
= 0;
790 elf_symtab__for_each_symbol(syms
, nr_syms
, idx
, sym
) {
792 const char *elf_name
;
793 char *demangled
= NULL
;
794 int is_label
= elf_sym__is_label(&sym
);
795 const char *section_name
;
797 if (!is_label
&& !elf_sym__is_function(&sym
))
800 sec
= elf_getscn(elf
, sym
.st_shndx
);
804 gelf_getshdr(sec
, &shdr
);
806 if (is_label
&& !elf_sec__is_text(&shdr
, secstrs
))
809 elf_name
= elf_sym__name(&sym
, symstrs
);
810 section_name
= elf_sec__name(&shdr
, secstrs
);
812 if (kernel
|| kmodule
) {
813 char dso_name
[PATH_MAX
];
815 if (strcmp(section_name
,
816 curr_dso
->short_name
+ dso_name_len
) == 0)
819 if (strcmp(section_name
, ".text") == 0) {
825 snprintf(dso_name
, sizeof(dso_name
),
826 "%s%s", self
->short_name
, section_name
);
828 curr_map
= kernel_maps__find_by_dso_name(dso_name
);
829 if (curr_map
== NULL
) {
830 u64 start
= sym
.st_value
;
833 start
+= map
->start
+ shdr
.sh_offset
;
835 curr_dso
= dso__new(dso_name
);
836 if (curr_dso
== NULL
)
838 curr_map
= map__new2(start
, curr_dso
);
839 if (curr_map
== NULL
) {
840 dso__delete(curr_dso
);
843 curr_map
->map_ip
= identity__map_ip
;
844 curr_map
->unmap_ip
= identity__map_ip
;
845 curr_dso
->origin
= DSO__ORIG_KERNEL
;
846 kernel_maps__insert(curr_map
);
849 curr_dso
= curr_map
->dso
;
854 if (curr_dso
->adjust_symbols
) {
855 pr_debug2("adjusting symbol: st_value: %Lx sh_addr: "
856 "%Lx sh_offset: %Lx\n", (u64
)sym
.st_value
,
857 (u64
)shdr
.sh_addr
, (u64
)shdr
.sh_offset
);
858 sym
.st_value
-= shdr
.sh_addr
- shdr
.sh_offset
;
861 * We need to figure out if the object was created from C++ sources
862 * DWARF DW_compile_unit has this, but we don't always have access
865 demangled
= bfd_demangle(NULL
, elf_name
, DMGL_PARAMS
| DMGL_ANSI
);
866 if (demangled
!= NULL
)
867 elf_name
= demangled
;
869 f
= symbol__new(sym
.st_value
, sym
.st_size
, elf_name
);
874 if (filter
&& filter(curr_map
, f
))
877 dso__insert_symbol(curr_dso
, f
);
883 * For misannotated, zeroed, ASM function sizes.
886 dso__fixup_sym_end(self
);
894 static bool dso__build_id_equal(const struct dso
*self
, u8
*build_id
)
896 return memcmp(self
->build_id
, build_id
, sizeof(self
->build_id
)) == 0;
899 bool dsos__read_build_ids(void)
901 bool have_build_id
= false;
904 list_for_each_entry(pos
, &dsos
, node
)
905 if (filename__read_build_id(pos
->long_name
, pos
->build_id
,
906 sizeof(pos
->build_id
)) > 0) {
907 have_build_id
= true;
908 pos
->has_build_id
= true;
911 return have_build_id
;
915 * Align offset to 4 bytes as needed for note name and descriptor data.
917 #define NOTE_ALIGN(n) (((n) + 3) & -4U)
919 int filename__read_build_id(const char *filename
, void *bf
, size_t size
)
930 if (size
< BUILD_ID_SIZE
)
933 fd
= open(filename
, O_RDONLY
);
937 elf
= elf_begin(fd
, PERF_ELF_C_READ_MMAP
, NULL
);
939 pr_debug2("%s: cannot read %s ELF file.\n", __func__
, filename
);
947 if (gelf_getehdr(elf
, &ehdr
) == NULL
) {
948 pr_err("%s: cannot get elf header.\n", __func__
);
952 sec
= elf_section_by_name(elf
, &ehdr
, &shdr
,
953 ".note.gnu.build-id", NULL
);
955 sec
= elf_section_by_name(elf
, &ehdr
, &shdr
,
961 data
= elf_getdata(sec
, NULL
);
966 while (ptr
< (data
->d_buf
+ data
->d_size
)) {
967 GElf_Nhdr
*nhdr
= ptr
;
968 int namesz
= NOTE_ALIGN(nhdr
->n_namesz
),
969 descsz
= NOTE_ALIGN(nhdr
->n_descsz
);
972 ptr
+= sizeof(*nhdr
);
975 if (nhdr
->n_type
== NT_GNU_BUILD_ID
&&
976 nhdr
->n_namesz
== sizeof("GNU")) {
977 if (memcmp(name
, "GNU", sizeof("GNU")) == 0) {
978 memcpy(bf
, ptr
, BUILD_ID_SIZE
);
993 int sysfs__read_build_id(const char *filename
, void *build_id
, size_t size
)
997 if (size
< BUILD_ID_SIZE
)
1000 fd
= open(filename
, O_RDONLY
);
1009 if (read(fd
, &nhdr
, sizeof(nhdr
)) != sizeof(nhdr
))
1012 namesz
= NOTE_ALIGN(nhdr
.n_namesz
);
1013 descsz
= NOTE_ALIGN(nhdr
.n_descsz
);
1014 if (nhdr
.n_type
== NT_GNU_BUILD_ID
&&
1015 nhdr
.n_namesz
== sizeof("GNU")) {
1016 if (read(fd
, bf
, namesz
) != namesz
)
1018 if (memcmp(bf
, "GNU", sizeof("GNU")) == 0) {
1019 if (read(fd
, build_id
,
1020 BUILD_ID_SIZE
) == BUILD_ID_SIZE
) {
1024 } else if (read(fd
, bf
, descsz
) != descsz
)
1027 int n
= namesz
+ descsz
;
1028 if (read(fd
, bf
, n
) != n
)
1037 char dso__symtab_origin(const struct dso
*self
)
1039 static const char origin
[] = {
1040 [DSO__ORIG_KERNEL
] = 'k',
1041 [DSO__ORIG_JAVA_JIT
] = 'j',
1042 [DSO__ORIG_FEDORA
] = 'f',
1043 [DSO__ORIG_UBUNTU
] = 'u',
1044 [DSO__ORIG_BUILDID
] = 'b',
1045 [DSO__ORIG_DSO
] = 'd',
1046 [DSO__ORIG_KMODULE
] = 'K',
1049 if (self
== NULL
|| self
->origin
== DSO__ORIG_NOT_FOUND
)
1051 return origin
[self
->origin
];
1054 int dso__load(struct dso
*self
, struct map
*map
, symbol_filter_t filter
)
1056 int size
= PATH_MAX
;
1058 u8 build_id
[BUILD_ID_SIZE
];
1065 return dso__load_kernel_sym(self
, map
, filter
);
1067 name
= malloc(size
);
1071 self
->adjust_symbols
= 0;
1073 if (strncmp(self
->name
, "/tmp/perf-", 10) == 0) {
1074 ret
= dso__load_perf_map(self
, map
, filter
);
1075 self
->origin
= ret
> 0 ? DSO__ORIG_JAVA_JIT
:
1076 DSO__ORIG_NOT_FOUND
;
1080 self
->origin
= DSO__ORIG_FEDORA
- 1;
1085 switch (self
->origin
) {
1086 case DSO__ORIG_FEDORA
:
1087 snprintf(name
, size
, "/usr/lib/debug%s.debug",
1090 case DSO__ORIG_UBUNTU
:
1091 snprintf(name
, size
, "/usr/lib/debug%s",
1094 case DSO__ORIG_BUILDID
:
1095 if (filename__read_build_id(self
->long_name
, build_id
,
1096 sizeof(build_id
))) {
1097 char build_id_hex
[BUILD_ID_SIZE
* 2 + 1];
1099 build_id__sprintf(build_id
, sizeof(build_id
),
1101 snprintf(name
, size
,
1102 "/usr/lib/debug/.build-id/%.2s/%s.debug",
1103 build_id_hex
, build_id_hex
+ 2);
1104 if (self
->has_build_id
)
1105 goto compare_build_id
;
1111 snprintf(name
, size
, "%s", self
->long_name
);
1118 if (self
->has_build_id
) {
1119 if (filename__read_build_id(name
, build_id
,
1120 sizeof(build_id
)) < 0)
1123 if (!dso__build_id_equal(self
, build_id
))
1127 fd
= open(name
, O_RDONLY
);
1130 ret
= dso__load_sym(self
, map
, name
, fd
, filter
, 0, 0);
1134 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
1140 int nr_plt
= dso__synthesize_plt_symbols(self
, map
, filter
);
1146 if (ret
< 0 && strstr(self
->name
, " (deleted)") != NULL
)
1151 struct map
*kernel_map
;
1153 static void kernel_maps__insert(struct map
*map
)
1155 maps__insert(&kernel_maps
, map
);
1158 struct symbol
*kernel_maps__find_symbol(u64 ip
, struct map
**mapp
,
1159 symbol_filter_t filter
)
1161 struct map
*map
= maps__find(&kernel_maps
, ip
);
1167 ip
= map
->map_ip(map
, ip
);
1168 return map__find_symbol(map
, ip
, filter
);
1174 struct map
*kernel_maps__find_by_dso_name(const char *name
)
1178 for (nd
= rb_first(&kernel_maps
); nd
; nd
= rb_next(nd
)) {
1179 struct map
*map
= rb_entry(nd
, struct map
, rb_node
);
1181 if (map
->dso
&& strcmp(map
->dso
->name
, name
) == 0)
1188 static int dsos__set_modules_path_dir(char *dirname
)
1190 struct dirent
*dent
;
1191 DIR *dir
= opendir(dirname
);
1194 pr_debug("%s: cannot open %s dir\n", __func__
, dirname
);
1198 while ((dent
= readdir(dir
)) != NULL
) {
1199 char path
[PATH_MAX
];
1201 if (dent
->d_type
== DT_DIR
) {
1202 if (!strcmp(dent
->d_name
, ".") ||
1203 !strcmp(dent
->d_name
, ".."))
1206 snprintf(path
, sizeof(path
), "%s/%s",
1207 dirname
, dent
->d_name
);
1208 if (dsos__set_modules_path_dir(path
) < 0)
1211 char *dot
= strrchr(dent
->d_name
, '.'),
1216 if (dot
== NULL
|| strcmp(dot
, ".ko"))
1218 snprintf(dso_name
, sizeof(dso_name
), "[%.*s]",
1219 (int)(dot
- dent
->d_name
), dent
->d_name
);
1221 strxfrchar(dso_name
, '-', '_');
1222 map
= kernel_maps__find_by_dso_name(dso_name
);
1226 snprintf(path
, sizeof(path
), "%s/%s",
1227 dirname
, dent
->d_name
);
1229 long_name
= strdup(path
);
1230 if (long_name
== NULL
)
1232 dso__set_long_name(map
->dso
, long_name
);
1242 static int dsos__set_modules_path(void)
1245 char modules_path
[PATH_MAX
];
1247 if (uname(&uts
) < 0)
1250 snprintf(modules_path
, sizeof(modules_path
), "/lib/modules/%s/kernel",
1253 return dsos__set_modules_path_dir(modules_path
);
1257 * Constructor variant for modules (where we know from /proc/modules where
1258 * they are loaded) and for vmlinux, where only after we load all the
1259 * symbols we'll know where it starts and ends.
1261 static struct map
*map__new2(u64 start
, struct dso
*dso
)
1263 struct map
*self
= malloc(sizeof(*self
));
1267 * ->end will be filled after we load all the symbols
1269 map__init(self
, start
, 0, 0, dso
);
1275 static int kernel_maps__create_module_maps(void)
1279 FILE *file
= fopen("/proc/modules", "r");
1285 while (!feof(file
)) {
1286 char name
[PATH_MAX
];
1292 line_len
= getline(&line
, &n
, file
);
1299 line
[--line_len
] = '\0'; /* \n */
1301 sep
= strrchr(line
, 'x');
1305 hex2u64(sep
+ 1, &start
);
1307 sep
= strchr(line
, ' ');
1313 snprintf(name
, sizeof(name
), "[%s]", line
);
1314 dso
= dso__new(name
);
1317 goto out_delete_line
;
1319 map
= map__new2(start
, dso
);
1322 goto out_delete_line
;
1325 snprintf(name
, sizeof(name
),
1326 "/sys/module/%s/notes/.note.gnu.build-id", line
);
1327 if (sysfs__read_build_id(name
, dso
->build_id
,
1328 sizeof(dso
->build_id
)) == 0)
1329 dso
->has_build_id
= true;
1331 dso
->origin
= DSO__ORIG_KMODULE
;
1332 kernel_maps__insert(map
);
1339 return dsos__set_modules_path();
1347 static int dso__load_vmlinux(struct dso
*self
, struct map
*map
,
1348 const char *vmlinux
, symbol_filter_t filter
)
1352 if (self
->has_build_id
) {
1353 u8 build_id
[BUILD_ID_SIZE
];
1355 if (filename__read_build_id(vmlinux
, build_id
,
1356 sizeof(build_id
)) < 0) {
1357 pr_debug("No build_id in %s, ignoring it\n", vmlinux
);
1360 if (!dso__build_id_equal(self
, build_id
)) {
1361 char expected_build_id
[BUILD_ID_SIZE
* 2 + 1],
1362 vmlinux_build_id
[BUILD_ID_SIZE
* 2 + 1];
1364 build_id__sprintf(self
->build_id
,
1365 sizeof(self
->build_id
),
1367 build_id__sprintf(build_id
, sizeof(build_id
),
1369 pr_debug("build_id in %s is %s while expected is %s, "
1370 "ignoring it\n", vmlinux
, vmlinux_build_id
,
1376 fd
= open(vmlinux
, O_RDONLY
);
1381 err
= dso__load_sym(self
, map
, self
->long_name
, fd
, filter
, 1, 0);
1388 static int dso__load_kernel_sym(struct dso
*self
, struct map
*map
,
1389 symbol_filter_t filter
)
1394 if (vmlinux_path
!= NULL
) {
1396 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1397 vmlinux_path__nr_entries
);
1398 for (i
= 0; i
< vmlinux_path__nr_entries
; ++i
) {
1399 err
= dso__load_vmlinux(self
, map
, vmlinux_path
[i
],
1402 pr_debug("Using %s for symbols\n",
1404 dso__set_long_name(self
,
1405 strdup(vmlinux_path
[i
]));
1411 is_kallsyms
= self
->long_name
[0] == '[';
1415 err
= dso__load_vmlinux(self
, map
, self
->long_name
, filter
);
1417 pr_info("The file %s cannot be used, "
1418 "trying to use /proc/kallsyms...", self
->long_name
);
1421 err
= kernel_maps__load_kallsyms(filter
);
1422 if (err
> 0 && !is_kallsyms
)
1423 dso__set_long_name(self
, strdup("[kernel.kallsyms]"));
1428 map__fixup_start(map
);
1429 map__fixup_end(map
);
1438 static void dsos__add(struct dso
*dso
)
1440 list_add_tail(&dso
->node
, &dsos
);
1443 static struct dso
*dsos__find(const char *name
)
1447 list_for_each_entry(pos
, &dsos
, node
)
1448 if (strcmp(pos
->name
, name
) == 0)
1453 struct dso
*dsos__findnew(const char *name
)
1455 struct dso
*dso
= dsos__find(name
);
1458 dso
= dso__new(name
);
1461 dso__set_basename(dso
);
1468 void dsos__fprintf(FILE *fp
)
1472 list_for_each_entry(pos
, &dsos
, node
)
1473 dso__fprintf(pos
, fp
);
1476 size_t dsos__fprintf_buildid(FILE *fp
)
1481 list_for_each_entry(pos
, &dsos
, node
) {
1482 ret
+= dso__fprintf_buildid(pos
, fp
);
1483 ret
+= fprintf(fp
, " %s\n", pos
->long_name
);
1488 static int kernel_maps__create_kernel_map(const char *vmlinux_name
)
1490 struct dso
*kernel
= dso__new(vmlinux_name
?: "[kernel.kallsyms]");
1495 kernel_map
= map__new2(0, kernel
);
1496 if (kernel_map
== NULL
)
1497 goto out_delete_kernel_dso
;
1499 kernel_map
->map_ip
= kernel_map
->unmap_ip
= identity__map_ip
;
1500 kernel
->short_name
= "[kernel]";
1503 vdso
= dso__new("[vdso]");
1505 goto out_delete_kernel_map
;
1507 if (sysfs__read_build_id("/sys/kernel/notes", kernel
->build_id
,
1508 sizeof(kernel
->build_id
)) == 0)
1509 kernel
->has_build_id
= true;
1511 kernel_maps__insert(kernel_map
);
1517 out_delete_kernel_map
:
1518 map__delete(kernel_map
);
1520 out_delete_kernel_dso
:
1521 dso__delete(kernel
);
1525 static void vmlinux_path__exit(void)
1527 while (--vmlinux_path__nr_entries
>= 0) {
1528 free(vmlinux_path
[vmlinux_path__nr_entries
]);
1529 vmlinux_path
[vmlinux_path__nr_entries
] = NULL
;
1533 vmlinux_path
= NULL
;
1536 static int vmlinux_path__init(void)
1541 if (uname(&uts
) < 0)
1544 vmlinux_path
= malloc(sizeof(char *) * 5);
1545 if (vmlinux_path
== NULL
)
1548 vmlinux_path
[vmlinux_path__nr_entries
] = strdup("vmlinux");
1549 if (vmlinux_path
[vmlinux_path__nr_entries
] == NULL
)
1551 ++vmlinux_path__nr_entries
;
1552 vmlinux_path
[vmlinux_path__nr_entries
] = strdup("/boot/vmlinux");
1553 if (vmlinux_path
[vmlinux_path__nr_entries
] == NULL
)
1555 ++vmlinux_path__nr_entries
;
1556 snprintf(bf
, sizeof(bf
), "/boot/vmlinux-%s", uts
.release
);
1557 vmlinux_path
[vmlinux_path__nr_entries
] = strdup(bf
);
1558 if (vmlinux_path
[vmlinux_path__nr_entries
] == NULL
)
1560 ++vmlinux_path__nr_entries
;
1561 snprintf(bf
, sizeof(bf
), "/lib/modules/%s/build/vmlinux", uts
.release
);
1562 vmlinux_path
[vmlinux_path__nr_entries
] = strdup(bf
);
1563 if (vmlinux_path
[vmlinux_path__nr_entries
] == NULL
)
1565 ++vmlinux_path__nr_entries
;
1566 snprintf(bf
, sizeof(bf
), "/usr/lib/debug/lib/modules/%s/vmlinux",
1568 vmlinux_path
[vmlinux_path__nr_entries
] = strdup(bf
);
1569 if (vmlinux_path
[vmlinux_path__nr_entries
] == NULL
)
1571 ++vmlinux_path__nr_entries
;
1576 vmlinux_path__exit();
1580 int kernel_maps__init(const char *vmlinux_name
, bool try_vmlinux_path
,
1583 if (try_vmlinux_path
&& vmlinux_path__init() < 0)
1586 if (kernel_maps__create_kernel_map(vmlinux_name
) < 0) {
1587 vmlinux_path__exit();
1591 if (use_modules
&& kernel_maps__create_module_maps() < 0)
1592 pr_debug("Failed to load list of modules in use, "
1595 * Now that we have all the maps created, just set the ->end of them:
1597 kernel_maps__fixup_end();
1601 void symbol__init(unsigned int priv_size
)
1603 elf_version(EV_CURRENT
);
1604 symbol__priv_size
= priv_size
;
This page took 0.259275 seconds and 5 git commands to generate.