16 #include <sys/utsname.h>
18 #ifndef NT_GNU_BUILD_ID
19 #define NT_GNU_BUILD_ID 3
33 static void dsos__add(struct list_head
*head
, struct dso
*dso
);
34 static struct map
*map__new2(u64 start
, struct dso
*dso
, enum map_type type
);
35 static int dso__load_kernel_sym(struct dso
*self
, struct map
*map
,
36 struct perf_session
*session
, symbol_filter_t filter
);
37 static int vmlinux_path__nr_entries
;
38 static char **vmlinux_path
;
40 struct symbol_conf symbol_conf
= {
41 .exclude_other
= true,
43 .try_vmlinux_path
= true,
46 bool dso__loaded(const struct dso
*self
, enum map_type type
)
48 return self
->loaded
& (1 << type
);
51 bool dso__sorted_by_name(const struct dso
*self
, enum map_type type
)
53 return self
->sorted_by_name
& (1 << type
);
56 static void dso__set_loaded(struct dso
*self
, enum map_type type
)
58 self
->loaded
|= (1 << type
);
61 static void dso__set_sorted_by_name(struct dso
*self
, enum map_type type
)
63 self
->sorted_by_name
|= (1 << type
);
66 static bool symbol_type__is_a(char symbol_type
, enum map_type map_type
)
70 return symbol_type
== 'T' || symbol_type
== 'W';
72 return symbol_type
== 'D' || symbol_type
== 'd';
78 static void symbols__fixup_end(struct rb_root
*self
)
80 struct rb_node
*nd
, *prevnd
= rb_first(self
);
81 struct symbol
*curr
, *prev
;
86 curr
= rb_entry(prevnd
, struct symbol
, rb_node
);
88 for (nd
= rb_next(prevnd
); nd
; nd
= rb_next(nd
)) {
90 curr
= rb_entry(nd
, struct symbol
, rb_node
);
92 if (prev
->end
== prev
->start
)
93 prev
->end
= curr
->start
- 1;
97 if (curr
->end
== curr
->start
)
98 curr
->end
= roundup(curr
->start
, 4096);
101 static void __map_groups__fixup_end(struct map_groups
*self
, enum map_type type
)
103 struct map
*prev
, *curr
;
104 struct rb_node
*nd
, *prevnd
= rb_first(&self
->maps
[type
]);
109 curr
= rb_entry(prevnd
, struct map
, rb_node
);
111 for (nd
= rb_next(prevnd
); nd
; nd
= rb_next(nd
)) {
113 curr
= rb_entry(nd
, struct map
, rb_node
);
114 prev
->end
= curr
->start
- 1;
118 * We still haven't the actual symbols, so guess the
119 * last map final address.
124 static void map_groups__fixup_end(struct map_groups
*self
)
127 for (i
= 0; i
< MAP__NR_TYPES
; ++i
)
128 __map_groups__fixup_end(self
, i
);
131 static struct symbol
*symbol__new(u64 start
, u64 len
, const char *name
)
133 size_t namelen
= strlen(name
) + 1;
134 struct symbol
*self
= zalloc(symbol_conf
.priv_size
+
135 sizeof(*self
) + namelen
);
139 if (symbol_conf
.priv_size
)
140 self
= ((void *)self
) + symbol_conf
.priv_size
;
143 self
->end
= len
? start
+ len
- 1 : start
;
145 pr_debug3("%s: %s %#Lx-%#Lx\n", __func__
, name
, start
, self
->end
);
147 memcpy(self
->name
, name
, namelen
);
152 static void symbol__delete(struct symbol
*self
)
154 free(((void *)self
) - symbol_conf
.priv_size
);
157 static size_t symbol__fprintf(struct symbol
*self
, FILE *fp
)
159 return fprintf(fp
, " %llx-%llx %s\n",
160 self
->start
, self
->end
, self
->name
);
163 static void dso__set_long_name(struct dso
*self
, char *name
)
167 self
->long_name
= name
;
168 self
->long_name_len
= strlen(name
);
171 static void dso__set_basename(struct dso
*self
)
173 self
->short_name
= basename(self
->long_name
);
176 struct dso
*dso__new(const char *name
)
178 struct dso
*self
= malloc(sizeof(*self
) + strlen(name
) + 1);
182 strcpy(self
->name
, name
);
183 dso__set_long_name(self
, self
->name
);
184 self
->short_name
= self
->name
;
185 for (i
= 0; i
< MAP__NR_TYPES
; ++i
)
186 self
->symbols
[i
] = self
->symbol_names
[i
] = RB_ROOT
;
187 self
->slen_calculated
= 0;
188 self
->origin
= DSO__ORIG_NOT_FOUND
;
190 self
->sorted_by_name
= 0;
191 self
->has_build_id
= 0;
197 static void symbols__delete(struct rb_root
*self
)
200 struct rb_node
*next
= rb_first(self
);
203 pos
= rb_entry(next
, struct symbol
, rb_node
);
204 next
= rb_next(&pos
->rb_node
);
205 rb_erase(&pos
->rb_node
, self
);
210 void dso__delete(struct dso
*self
)
213 for (i
= 0; i
< MAP__NR_TYPES
; ++i
)
214 symbols__delete(&self
->symbols
[i
]);
215 if (self
->long_name
!= self
->name
)
216 free(self
->long_name
);
220 void dso__set_build_id(struct dso
*self
, void *build_id
)
222 memcpy(self
->build_id
, build_id
, sizeof(self
->build_id
));
223 self
->has_build_id
= 1;
226 static void symbols__insert(struct rb_root
*self
, struct symbol
*sym
)
228 struct rb_node
**p
= &self
->rb_node
;
229 struct rb_node
*parent
= NULL
;
230 const u64 ip
= sym
->start
;
235 s
= rb_entry(parent
, struct symbol
, rb_node
);
241 rb_link_node(&sym
->rb_node
, parent
, p
);
242 rb_insert_color(&sym
->rb_node
, self
);
245 static struct symbol
*symbols__find(struct rb_root
*self
, u64 ip
)
255 struct symbol
*s
= rb_entry(n
, struct symbol
, rb_node
);
259 else if (ip
> s
->end
)
268 struct symbol_name_rb_node
{
269 struct rb_node rb_node
;
273 static void symbols__insert_by_name(struct rb_root
*self
, struct symbol
*sym
)
275 struct rb_node
**p
= &self
->rb_node
;
276 struct rb_node
*parent
= NULL
;
277 struct symbol_name_rb_node
*symn
= ((void *)sym
) - sizeof(*parent
), *s
;
281 s
= rb_entry(parent
, struct symbol_name_rb_node
, rb_node
);
282 if (strcmp(sym
->name
, s
->sym
.name
) < 0)
287 rb_link_node(&symn
->rb_node
, parent
, p
);
288 rb_insert_color(&symn
->rb_node
, self
);
291 static void symbols__sort_by_name(struct rb_root
*self
, struct rb_root
*source
)
295 for (nd
= rb_first(source
); nd
; nd
= rb_next(nd
)) {
296 struct symbol
*pos
= rb_entry(nd
, struct symbol
, rb_node
);
297 symbols__insert_by_name(self
, pos
);
301 static struct symbol
*symbols__find_by_name(struct rb_root
*self
, const char *name
)
311 struct symbol_name_rb_node
*s
;
314 s
= rb_entry(n
, struct symbol_name_rb_node
, rb_node
);
315 cmp
= strcmp(name
, s
->sym
.name
);
328 struct symbol
*dso__find_symbol(struct dso
*self
,
329 enum map_type type
, u64 addr
)
331 return symbols__find(&self
->symbols
[type
], addr
);
334 struct symbol
*dso__find_symbol_by_name(struct dso
*self
, enum map_type type
,
337 return symbols__find_by_name(&self
->symbol_names
[type
], name
);
340 void dso__sort_by_name(struct dso
*self
, enum map_type type
)
342 dso__set_sorted_by_name(self
, type
);
343 return symbols__sort_by_name(&self
->symbol_names
[type
],
344 &self
->symbols
[type
]);
347 int build_id__sprintf(u8
*self
, int len
, char *bf
)
353 for (i
= 0; i
< len
; ++i
) {
354 sprintf(bid
, "%02x", *raw
);
362 size_t dso__fprintf_buildid(struct dso
*self
, FILE *fp
)
364 char sbuild_id
[BUILD_ID_SIZE
* 2 + 1];
366 build_id__sprintf(self
->build_id
, sizeof(self
->build_id
), sbuild_id
);
367 return fprintf(fp
, "%s", sbuild_id
);
370 size_t dso__fprintf(struct dso
*self
, enum map_type type
, FILE *fp
)
373 size_t ret
= fprintf(fp
, "dso: %s (", self
->short_name
);
375 ret
+= dso__fprintf_buildid(self
, fp
);
376 ret
+= fprintf(fp
, ")\n");
377 for (nd
= rb_first(&self
->symbols
[type
]); nd
; nd
= rb_next(nd
)) {
378 struct symbol
*pos
= rb_entry(nd
, struct symbol
, rb_node
);
379 ret
+= symbol__fprintf(pos
, fp
);
386 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
387 * so that we can in the next step set the symbol ->end address and then
388 * call kernel_maps__split_kallsyms.
390 static int dso__load_all_kallsyms(struct dso
*self
, struct map
*map
)
394 struct rb_root
*root
= &self
->symbols
[map
->type
];
395 FILE *file
= fopen("/proc/kallsyms", "r");
400 while (!feof(file
)) {
407 line_len
= getline(&line
, &n
, file
);
414 line
[--line_len
] = '\0'; /* \n */
416 len
= hex2u64(line
, &start
);
419 if (len
+ 2 >= line_len
)
422 symbol_type
= toupper(line
[len
]);
423 if (!symbol_type__is_a(symbol_type
, map
->type
))
426 symbol_name
= line
+ len
+ 2;
428 * Will fix up the end later, when we have all symbols sorted.
430 sym
= symbol__new(start
, 0, symbol_name
);
433 goto out_delete_line
;
435 * We will pass the symbols to the filter later, in
436 * map__split_kallsyms, when we have split the maps per module
438 symbols__insert(root
, sym
);
453 * Split the symbols into maps, making sure there are no overlaps, i.e. the
454 * kernel range is broken in several maps, named [kernel].N, as we don't have
455 * the original ELF section names vmlinux have.
457 static int dso__split_kallsyms(struct dso
*self
, struct map
*map
,
458 struct perf_session
*session
, symbol_filter_t filter
)
460 struct map
*curr_map
= map
;
463 struct rb_root
*root
= &self
->symbols
[map
->type
];
464 struct rb_node
*next
= rb_first(root
);
465 int kernel_range
= 0;
470 pos
= rb_entry(next
, struct symbol
, rb_node
);
471 next
= rb_next(&pos
->rb_node
);
473 module
= strchr(pos
->name
, '\t');
475 if (!symbol_conf
.use_modules
)
480 if (strcmp(self
->name
, module
)) {
481 curr_map
= map_groups__find_by_name(&session
->kmaps
, map
->type
, module
);
482 if (curr_map
== NULL
) {
483 pr_debug("/proc/{kallsyms,modules} "
489 * So that we look just like we get from .ko files,
490 * i.e. not prelinked, relative to map->start.
492 pos
->start
= curr_map
->map_ip(curr_map
, pos
->start
);
493 pos
->end
= curr_map
->map_ip(curr_map
, pos
->end
);
494 } else if (curr_map
!= map
) {
495 char dso_name
[PATH_MAX
];
498 snprintf(dso_name
, sizeof(dso_name
), "[kernel].%d",
501 dso
= dso__new(dso_name
);
505 curr_map
= map__new2(pos
->start
, dso
, map
->type
);
511 curr_map
->map_ip
= curr_map
->unmap_ip
= identity__map_ip
;
512 map_groups__insert(&session
->kmaps
, curr_map
);
516 if (filter
&& filter(curr_map
, pos
)) {
517 discard_symbol
: rb_erase(&pos
->rb_node
, root
);
520 if (curr_map
!= map
) {
521 rb_erase(&pos
->rb_node
, root
);
522 symbols__insert(&curr_map
->dso
->symbols
[curr_map
->type
], pos
);
532 static int dso__load_kallsyms(struct dso
*self
, struct map
*map
,
533 struct perf_session
*session
, symbol_filter_t filter
)
535 if (dso__load_all_kallsyms(self
, map
) < 0)
538 symbols__fixup_end(&self
->symbols
[map
->type
]);
539 self
->origin
= DSO__ORIG_KERNEL
;
541 return dso__split_kallsyms(self
, map
, session
, filter
);
544 static int dso__load_perf_map(struct dso
*self
, struct map
*map
,
545 symbol_filter_t filter
)
552 file
= fopen(self
->long_name
, "r");
556 while (!feof(file
)) {
561 line_len
= getline(&line
, &n
, file
);
568 line
[--line_len
] = '\0'; /* \n */
570 len
= hex2u64(line
, &start
);
573 if (len
+ 2 >= line_len
)
576 len
+= hex2u64(line
+ len
, &size
);
579 if (len
+ 2 >= line_len
)
582 sym
= symbol__new(start
, size
, line
+ len
);
585 goto out_delete_line
;
587 if (filter
&& filter(map
, sym
))
590 symbols__insert(&self
->symbols
[map
->type
], sym
);
607 * elf_symtab__for_each_symbol - iterate thru all the symbols
609 * @self: struct elf_symtab instance to iterate
611 * @sym: GElf_Sym iterator
613 #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
614 for (idx = 0, gelf_getsym(syms, idx, &sym);\
616 idx++, gelf_getsym(syms, idx, &sym))
618 static inline uint8_t elf_sym__type(const GElf_Sym
*sym
)
620 return GELF_ST_TYPE(sym
->st_info
);
623 static inline int elf_sym__is_function(const GElf_Sym
*sym
)
625 return elf_sym__type(sym
) == STT_FUNC
&&
627 sym
->st_shndx
!= SHN_UNDEF
;
630 static inline bool elf_sym__is_object(const GElf_Sym
*sym
)
632 return elf_sym__type(sym
) == STT_OBJECT
&&
634 sym
->st_shndx
!= SHN_UNDEF
;
637 static inline int elf_sym__is_label(const GElf_Sym
*sym
)
639 return elf_sym__type(sym
) == STT_NOTYPE
&&
641 sym
->st_shndx
!= SHN_UNDEF
&&
642 sym
->st_shndx
!= SHN_ABS
;
645 static inline const char *elf_sec__name(const GElf_Shdr
*shdr
,
646 const Elf_Data
*secstrs
)
648 return secstrs
->d_buf
+ shdr
->sh_name
;
651 static inline int elf_sec__is_text(const GElf_Shdr
*shdr
,
652 const Elf_Data
*secstrs
)
654 return strstr(elf_sec__name(shdr
, secstrs
), "text") != NULL
;
657 static inline bool elf_sec__is_data(const GElf_Shdr
*shdr
,
658 const Elf_Data
*secstrs
)
660 return strstr(elf_sec__name(shdr
, secstrs
), "data") != NULL
;
663 static inline const char *elf_sym__name(const GElf_Sym
*sym
,
664 const Elf_Data
*symstrs
)
666 return symstrs
->d_buf
+ sym
->st_name
;
669 static Elf_Scn
*elf_section_by_name(Elf
*elf
, GElf_Ehdr
*ep
,
670 GElf_Shdr
*shp
, const char *name
,
676 while ((sec
= elf_nextscn(elf
, sec
)) != NULL
) {
679 gelf_getshdr(sec
, shp
);
680 str
= elf_strptr(elf
, ep
->e_shstrndx
, shp
->sh_name
);
681 if (!strcmp(name
, str
)) {
692 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
693 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
695 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
697 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
698 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
700 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
703 * We need to check if we have a .dynsym, so that we can handle the
704 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
705 * .dynsym or .symtab).
706 * And always look at the original dso, not at debuginfo packages, that
707 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
709 static int dso__synthesize_plt_symbols(struct dso
*self
, struct map
*map
,
710 symbol_filter_t filter
)
712 uint32_t nr_rel_entries
, idx
;
717 GElf_Shdr shdr_rel_plt
, shdr_dynsym
;
718 Elf_Data
*reldata
, *syms
, *symstrs
;
719 Elf_Scn
*scn_plt_rel
, *scn_symstrs
, *scn_dynsym
;
722 char sympltname
[1024];
724 int nr
= 0, symidx
, fd
, err
= 0;
726 fd
= open(self
->long_name
, O_RDONLY
);
730 elf
= elf_begin(fd
, PERF_ELF_C_READ_MMAP
, NULL
);
734 if (gelf_getehdr(elf
, &ehdr
) == NULL
)
737 scn_dynsym
= elf_section_by_name(elf
, &ehdr
, &shdr_dynsym
,
738 ".dynsym", &dynsym_idx
);
739 if (scn_dynsym
== NULL
)
742 scn_plt_rel
= elf_section_by_name(elf
, &ehdr
, &shdr_rel_plt
,
744 if (scn_plt_rel
== NULL
) {
745 scn_plt_rel
= elf_section_by_name(elf
, &ehdr
, &shdr_rel_plt
,
747 if (scn_plt_rel
== NULL
)
753 if (shdr_rel_plt
.sh_link
!= dynsym_idx
)
756 if (elf_section_by_name(elf
, &ehdr
, &shdr_plt
, ".plt", NULL
) == NULL
)
760 * Fetch the relocation section to find the idxes to the GOT
761 * and the symbols in the .dynsym they refer to.
763 reldata
= elf_getdata(scn_plt_rel
, NULL
);
767 syms
= elf_getdata(scn_dynsym
, NULL
);
771 scn_symstrs
= elf_getscn(elf
, shdr_dynsym
.sh_link
);
772 if (scn_symstrs
== NULL
)
775 symstrs
= elf_getdata(scn_symstrs
, NULL
);
779 nr_rel_entries
= shdr_rel_plt
.sh_size
/ shdr_rel_plt
.sh_entsize
;
780 plt_offset
= shdr_plt
.sh_offset
;
782 if (shdr_rel_plt
.sh_type
== SHT_RELA
) {
783 GElf_Rela pos_mem
, *pos
;
785 elf_section__for_each_rela(reldata
, pos
, pos_mem
, idx
,
787 symidx
= GELF_R_SYM(pos
->r_info
);
788 plt_offset
+= shdr_plt
.sh_entsize
;
789 gelf_getsym(syms
, symidx
, &sym
);
790 snprintf(sympltname
, sizeof(sympltname
),
791 "%s@plt", elf_sym__name(&sym
, symstrs
));
793 f
= symbol__new(plt_offset
, shdr_plt
.sh_entsize
,
798 if (filter
&& filter(map
, f
))
801 symbols__insert(&self
->symbols
[map
->type
], f
);
805 } else if (shdr_rel_plt
.sh_type
== SHT_REL
) {
806 GElf_Rel pos_mem
, *pos
;
807 elf_section__for_each_rel(reldata
, pos
, pos_mem
, idx
,
809 symidx
= GELF_R_SYM(pos
->r_info
);
810 plt_offset
+= shdr_plt
.sh_entsize
;
811 gelf_getsym(syms
, symidx
, &sym
);
812 snprintf(sympltname
, sizeof(sympltname
),
813 "%s@plt", elf_sym__name(&sym
, symstrs
));
815 f
= symbol__new(plt_offset
, shdr_plt
.sh_entsize
,
820 if (filter
&& filter(map
, f
))
823 symbols__insert(&self
->symbols
[map
->type
], f
);
838 pr_warning("%s: problems reading %s PLT info.\n",
839 __func__
, self
->long_name
);
843 static bool elf_sym__is_a(GElf_Sym
*self
, enum map_type type
)
847 return elf_sym__is_function(self
);
849 return elf_sym__is_object(self
);
855 static bool elf_sec__is_a(GElf_Shdr
*self
, Elf_Data
*secstrs
, enum map_type type
)
859 return elf_sec__is_text(self
, secstrs
);
861 return elf_sec__is_data(self
, secstrs
);
867 static int dso__load_sym(struct dso
*self
, struct map
*map
,
868 struct perf_session
*session
, const char *name
, int fd
,
869 symbol_filter_t filter
, int kernel
, int kmodule
)
871 struct map
*curr_map
= map
;
872 struct dso
*curr_dso
= self
;
873 size_t dso_name_len
= strlen(self
->short_name
);
874 Elf_Data
*symstrs
, *secstrs
;
882 Elf_Scn
*sec
, *sec_strndx
;
886 elf
= elf_begin(fd
, PERF_ELF_C_READ_MMAP
, NULL
);
888 pr_err("%s: cannot read %s ELF file.\n", __func__
, name
);
892 if (gelf_getehdr(elf
, &ehdr
) == NULL
) {
893 pr_err("%s: cannot get elf header.\n", __func__
);
897 sec
= elf_section_by_name(elf
, &ehdr
, &shdr
, ".symtab", NULL
);
899 sec
= elf_section_by_name(elf
, &ehdr
, &shdr
, ".dynsym", NULL
);
904 syms
= elf_getdata(sec
, NULL
);
908 sec
= elf_getscn(elf
, shdr
.sh_link
);
912 symstrs
= elf_getdata(sec
, NULL
);
916 sec_strndx
= elf_getscn(elf
, ehdr
.e_shstrndx
);
917 if (sec_strndx
== NULL
)
920 secstrs
= elf_getdata(sec_strndx
, NULL
);
924 nr_syms
= shdr
.sh_size
/ shdr
.sh_entsize
;
926 memset(&sym
, 0, sizeof(sym
));
928 self
->adjust_symbols
= (ehdr
.e_type
== ET_EXEC
||
929 elf_section_by_name(elf
, &ehdr
, &shdr
,
932 } else self
->adjust_symbols
= 0;
934 elf_symtab__for_each_symbol(syms
, nr_syms
, idx
, sym
) {
936 const char *elf_name
;
937 char *demangled
= NULL
;
938 int is_label
= elf_sym__is_label(&sym
);
939 const char *section_name
;
941 if (!is_label
&& !elf_sym__is_a(&sym
, map
->type
))
944 sec
= elf_getscn(elf
, sym
.st_shndx
);
948 gelf_getshdr(sec
, &shdr
);
950 if (is_label
&& !elf_sec__is_a(&shdr
, secstrs
, map
->type
))
953 elf_name
= elf_sym__name(&sym
, symstrs
);
954 section_name
= elf_sec__name(&shdr
, secstrs
);
956 if (kernel
|| kmodule
) {
957 char dso_name
[PATH_MAX
];
959 if (strcmp(section_name
,
960 curr_dso
->short_name
+ dso_name_len
) == 0)
963 if (strcmp(section_name
, ".text") == 0) {
969 snprintf(dso_name
, sizeof(dso_name
),
970 "%s%s", self
->short_name
, section_name
);
972 curr_map
= map_groups__find_by_name(&session
->kmaps
, map
->type
, dso_name
);
973 if (curr_map
== NULL
) {
974 u64 start
= sym
.st_value
;
977 start
+= map
->start
+ shdr
.sh_offset
;
979 curr_dso
= dso__new(dso_name
);
980 if (curr_dso
== NULL
)
982 curr_map
= map__new2(start
, curr_dso
,
984 if (curr_map
== NULL
) {
985 dso__delete(curr_dso
);
988 curr_map
->map_ip
= identity__map_ip
;
989 curr_map
->unmap_ip
= identity__map_ip
;
990 curr_dso
->origin
= DSO__ORIG_KERNEL
;
991 map_groups__insert(&session
->kmaps
, curr_map
);
992 dsos__add(&dsos__kernel
, curr_dso
);
994 curr_dso
= curr_map
->dso
;
999 if (curr_dso
->adjust_symbols
) {
1000 pr_debug2("adjusting symbol: st_value: %Lx sh_addr: "
1001 "%Lx sh_offset: %Lx\n", (u64
)sym
.st_value
,
1002 (u64
)shdr
.sh_addr
, (u64
)shdr
.sh_offset
);
1003 sym
.st_value
-= shdr
.sh_addr
- shdr
.sh_offset
;
1006 * We need to figure out if the object was created from C++ sources
1007 * DWARF DW_compile_unit has this, but we don't always have access
1010 demangled
= bfd_demangle(NULL
, elf_name
, DMGL_PARAMS
| DMGL_ANSI
);
1011 if (demangled
!= NULL
)
1012 elf_name
= demangled
;
1014 f
= symbol__new(sym
.st_value
, sym
.st_size
, elf_name
);
1019 if (filter
&& filter(curr_map
, f
))
1022 symbols__insert(&curr_dso
->symbols
[curr_map
->type
], f
);
1028 * For misannotated, zeroed, ASM function sizes.
1031 symbols__fixup_end(&self
->symbols
[map
->type
]);
1039 static bool dso__build_id_equal(const struct dso
*self
, u8
*build_id
)
1041 return memcmp(self
->build_id
, build_id
, sizeof(self
->build_id
)) == 0;
1044 static bool __dsos__read_build_ids(struct list_head
*head
)
1046 bool have_build_id
= false;
1049 list_for_each_entry(pos
, head
, node
)
1050 if (filename__read_build_id(pos
->long_name
, pos
->build_id
,
1051 sizeof(pos
->build_id
)) > 0) {
1052 have_build_id
= true;
1053 pos
->has_build_id
= true;
1056 return have_build_id
;
1059 bool dsos__read_build_ids(void)
1061 bool kbuildids
= __dsos__read_build_ids(&dsos__kernel
),
1062 ubuildids
= __dsos__read_build_ids(&dsos__user
);
1063 return kbuildids
|| ubuildids
;
1067 * Align offset to 4 bytes as needed for note name and descriptor data.
1069 #define NOTE_ALIGN(n) (((n) + 3) & -4U)
1071 int filename__read_build_id(const char *filename
, void *bf
, size_t size
)
1082 if (size
< BUILD_ID_SIZE
)
1085 fd
= open(filename
, O_RDONLY
);
1089 elf
= elf_begin(fd
, PERF_ELF_C_READ_MMAP
, NULL
);
1091 pr_debug2("%s: cannot read %s ELF file.\n", __func__
, filename
);
1096 if (ek
!= ELF_K_ELF
)
1099 if (gelf_getehdr(elf
, &ehdr
) == NULL
) {
1100 pr_err("%s: cannot get elf header.\n", __func__
);
1104 sec
= elf_section_by_name(elf
, &ehdr
, &shdr
,
1105 ".note.gnu.build-id", NULL
);
1107 sec
= elf_section_by_name(elf
, &ehdr
, &shdr
,
1113 data
= elf_getdata(sec
, NULL
);
1118 while (ptr
< (data
->d_buf
+ data
->d_size
)) {
1119 GElf_Nhdr
*nhdr
= ptr
;
1120 int namesz
= NOTE_ALIGN(nhdr
->n_namesz
),
1121 descsz
= NOTE_ALIGN(nhdr
->n_descsz
);
1124 ptr
+= sizeof(*nhdr
);
1127 if (nhdr
->n_type
== NT_GNU_BUILD_ID
&&
1128 nhdr
->n_namesz
== sizeof("GNU")) {
1129 if (memcmp(name
, "GNU", sizeof("GNU")) == 0) {
1130 memcpy(bf
, ptr
, BUILD_ID_SIZE
);
1131 err
= BUILD_ID_SIZE
;
1145 int sysfs__read_build_id(const char *filename
, void *build_id
, size_t size
)
1149 if (size
< BUILD_ID_SIZE
)
1152 fd
= open(filename
, O_RDONLY
);
1161 if (read(fd
, &nhdr
, sizeof(nhdr
)) != sizeof(nhdr
))
1164 namesz
= NOTE_ALIGN(nhdr
.n_namesz
);
1165 descsz
= NOTE_ALIGN(nhdr
.n_descsz
);
1166 if (nhdr
.n_type
== NT_GNU_BUILD_ID
&&
1167 nhdr
.n_namesz
== sizeof("GNU")) {
1168 if (read(fd
, bf
, namesz
) != namesz
)
1170 if (memcmp(bf
, "GNU", sizeof("GNU")) == 0) {
1171 if (read(fd
, build_id
,
1172 BUILD_ID_SIZE
) == BUILD_ID_SIZE
) {
1176 } else if (read(fd
, bf
, descsz
) != descsz
)
1179 int n
= namesz
+ descsz
;
1180 if (read(fd
, bf
, n
) != n
)
1189 char dso__symtab_origin(const struct dso
*self
)
1191 static const char origin
[] = {
1192 [DSO__ORIG_KERNEL
] = 'k',
1193 [DSO__ORIG_JAVA_JIT
] = 'j',
1194 [DSO__ORIG_FEDORA
] = 'f',
1195 [DSO__ORIG_UBUNTU
] = 'u',
1196 [DSO__ORIG_BUILDID
] = 'b',
1197 [DSO__ORIG_DSO
] = 'd',
1198 [DSO__ORIG_KMODULE
] = 'K',
1201 if (self
== NULL
|| self
->origin
== DSO__ORIG_NOT_FOUND
)
1203 return origin
[self
->origin
];
1206 int dso__load(struct dso
*self
, struct map
*map
, struct perf_session
*session
,
1207 symbol_filter_t filter
)
1209 int size
= PATH_MAX
;
1211 u8 build_id
[BUILD_ID_SIZE
];
1215 dso__set_loaded(self
, map
->type
);
1218 return dso__load_kernel_sym(self
, map
, session
, filter
);
1220 name
= malloc(size
);
1224 self
->adjust_symbols
= 0;
1226 if (strncmp(self
->name
, "/tmp/perf-", 10) == 0) {
1227 ret
= dso__load_perf_map(self
, map
, filter
);
1228 self
->origin
= ret
> 0 ? DSO__ORIG_JAVA_JIT
:
1229 DSO__ORIG_NOT_FOUND
;
1233 self
->origin
= DSO__ORIG_FEDORA
- 1;
1238 switch (self
->origin
) {
1239 case DSO__ORIG_FEDORA
:
1240 snprintf(name
, size
, "/usr/lib/debug%s.debug",
1243 case DSO__ORIG_UBUNTU
:
1244 snprintf(name
, size
, "/usr/lib/debug%s",
1247 case DSO__ORIG_BUILDID
:
1248 if (filename__read_build_id(self
->long_name
, build_id
,
1249 sizeof(build_id
))) {
1250 char build_id_hex
[BUILD_ID_SIZE
* 2 + 1];
1252 build_id__sprintf(build_id
, sizeof(build_id
),
1254 snprintf(name
, size
,
1255 "/usr/lib/debug/.build-id/%.2s/%s.debug",
1256 build_id_hex
, build_id_hex
+ 2);
1257 if (self
->has_build_id
)
1258 goto compare_build_id
;
1264 snprintf(name
, size
, "%s", self
->long_name
);
1271 if (self
->has_build_id
) {
1272 if (filename__read_build_id(name
, build_id
,
1273 sizeof(build_id
)) < 0)
1276 if (!dso__build_id_equal(self
, build_id
))
1280 fd
= open(name
, O_RDONLY
);
1283 ret
= dso__load_sym(self
, map
, NULL
, name
, fd
, filter
, 0, 0);
1287 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
1293 int nr_plt
= dso__synthesize_plt_symbols(self
, map
, filter
);
1299 if (ret
< 0 && strstr(self
->name
, " (deleted)") != NULL
)
1304 struct map
*map_groups__find_by_name(struct map_groups
*self
,
1305 enum map_type type
, const char *name
)
1309 for (nd
= rb_first(&self
->maps
[type
]); nd
; nd
= rb_next(nd
)) {
1310 struct map
*map
= rb_entry(nd
, struct map
, rb_node
);
1312 if (map
->dso
&& strcmp(map
->dso
->name
, name
) == 0)
1319 static int perf_session__set_modules_path_dir(struct perf_session
*self
, char *dirname
)
1321 struct dirent
*dent
;
1322 DIR *dir
= opendir(dirname
);
1325 pr_debug("%s: cannot open %s dir\n", __func__
, dirname
);
1329 while ((dent
= readdir(dir
)) != NULL
) {
1330 char path
[PATH_MAX
];
1332 if (dent
->d_type
== DT_DIR
) {
1333 if (!strcmp(dent
->d_name
, ".") ||
1334 !strcmp(dent
->d_name
, ".."))
1337 snprintf(path
, sizeof(path
), "%s/%s",
1338 dirname
, dent
->d_name
);
1339 if (perf_session__set_modules_path_dir(self
, path
) < 0)
1342 char *dot
= strrchr(dent
->d_name
, '.'),
1347 if (dot
== NULL
|| strcmp(dot
, ".ko"))
1349 snprintf(dso_name
, sizeof(dso_name
), "[%.*s]",
1350 (int)(dot
- dent
->d_name
), dent
->d_name
);
1352 strxfrchar(dso_name
, '-', '_');
1353 map
= map_groups__find_by_name(&self
->kmaps
, MAP__FUNCTION
, dso_name
);
1357 snprintf(path
, sizeof(path
), "%s/%s",
1358 dirname
, dent
->d_name
);
1360 long_name
= strdup(path
);
1361 if (long_name
== NULL
)
1363 dso__set_long_name(map
->dso
, long_name
);
1373 static int perf_session__set_modules_path(struct perf_session
*self
)
1376 char modules_path
[PATH_MAX
];
1378 if (uname(&uts
) < 0)
1381 snprintf(modules_path
, sizeof(modules_path
), "/lib/modules/%s/kernel",
1384 return perf_session__set_modules_path_dir(self
, modules_path
);
1388 * Constructor variant for modules (where we know from /proc/modules where
1389 * they are loaded) and for vmlinux, where only after we load all the
1390 * symbols we'll know where it starts and ends.
1392 static struct map
*map__new2(u64 start
, struct dso
*dso
, enum map_type type
)
1394 struct map
*self
= malloc(sizeof(*self
));
1398 * ->end will be filled after we load all the symbols
1400 map__init(self
, type
, start
, 0, 0, dso
);
1406 static int perf_session__create_module_maps(struct perf_session
*self
)
1410 FILE *file
= fopen("/proc/modules", "r");
1416 while (!feof(file
)) {
1417 char name
[PATH_MAX
];
1423 line_len
= getline(&line
, &n
, file
);
1430 line
[--line_len
] = '\0'; /* \n */
1432 sep
= strrchr(line
, 'x');
1436 hex2u64(sep
+ 1, &start
);
1438 sep
= strchr(line
, ' ');
1444 snprintf(name
, sizeof(name
), "[%s]", line
);
1445 dso
= dso__new(name
);
1448 goto out_delete_line
;
1450 map
= map__new2(start
, dso
, MAP__FUNCTION
);
1453 goto out_delete_line
;
1456 snprintf(name
, sizeof(name
),
1457 "/sys/module/%s/notes/.note.gnu.build-id", line
);
1458 if (sysfs__read_build_id(name
, dso
->build_id
,
1459 sizeof(dso
->build_id
)) == 0)
1460 dso
->has_build_id
= true;
1462 dso
->origin
= DSO__ORIG_KMODULE
;
1463 map_groups__insert(&self
->kmaps
, map
);
1464 dsos__add(&dsos__kernel
, dso
);
1470 return perf_session__set_modules_path(self
);
1478 static int dso__load_vmlinux(struct dso
*self
, struct map
*map
,
1479 struct perf_session
*session
,
1480 const char *vmlinux
, symbol_filter_t filter
)
1484 if (self
->has_build_id
) {
1485 u8 build_id
[BUILD_ID_SIZE
];
1487 if (filename__read_build_id(vmlinux
, build_id
,
1488 sizeof(build_id
)) < 0) {
1489 pr_debug("No build_id in %s, ignoring it\n", vmlinux
);
1492 if (!dso__build_id_equal(self
, build_id
)) {
1493 char expected_build_id
[BUILD_ID_SIZE
* 2 + 1],
1494 vmlinux_build_id
[BUILD_ID_SIZE
* 2 + 1];
1496 build_id__sprintf(self
->build_id
,
1497 sizeof(self
->build_id
),
1499 build_id__sprintf(build_id
, sizeof(build_id
),
1501 pr_debug("build_id in %s is %s while expected is %s, "
1502 "ignoring it\n", vmlinux
, vmlinux_build_id
,
1508 fd
= open(vmlinux
, O_RDONLY
);
1512 dso__set_loaded(self
, map
->type
);
1513 err
= dso__load_sym(self
, map
, session
, self
->long_name
, fd
, filter
, 1, 0);
1519 static int dso__load_kernel_sym(struct dso
*self
, struct map
*map
,
1520 struct perf_session
*session
, symbol_filter_t filter
)
1525 if (vmlinux_path
!= NULL
) {
1527 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1528 vmlinux_path__nr_entries
);
1529 for (i
= 0; i
< vmlinux_path__nr_entries
; ++i
) {
1530 err
= dso__load_vmlinux(self
, map
, session
,
1531 vmlinux_path
[i
], filter
);
1533 pr_debug("Using %s for symbols\n",
1535 dso__set_long_name(self
,
1536 strdup(vmlinux_path
[i
]));
1542 is_kallsyms
= self
->long_name
[0] == '[';
1546 err
= dso__load_vmlinux(self
, map
, session
, self
->long_name
, filter
);
1548 pr_info("The file %s cannot be used, "
1549 "trying to use /proc/kallsyms...", self
->long_name
);
1551 err
= dso__load_kallsyms(self
, map
, session
, filter
);
1552 if (err
> 0 && !is_kallsyms
)
1553 dso__set_long_name(self
, strdup("[kernel.kallsyms]"));
1558 map__fixup_start(map
);
1559 map__fixup_end(map
);
1565 LIST_HEAD(dsos__user
);
1566 LIST_HEAD(dsos__kernel
);
1569 static void dsos__add(struct list_head
*head
, struct dso
*dso
)
1571 list_add_tail(&dso
->node
, head
);
1574 static struct dso
*dsos__find(struct list_head
*head
, const char *name
)
1578 list_for_each_entry(pos
, head
, node
)
1579 if (strcmp(pos
->name
, name
) == 0)
1584 struct dso
*dsos__findnew(const char *name
)
1586 struct dso
*dso
= dsos__find(&dsos__user
, name
);
1589 dso
= dso__new(name
);
1591 dsos__add(&dsos__user
, dso
);
1592 dso__set_basename(dso
);
1599 static void __dsos__fprintf(struct list_head
*head
, FILE *fp
)
1603 list_for_each_entry(pos
, head
, node
) {
1605 for (i
= 0; i
< MAP__NR_TYPES
; ++i
)
1606 dso__fprintf(pos
, i
, fp
);
1610 void dsos__fprintf(FILE *fp
)
1612 __dsos__fprintf(&dsos__kernel
, fp
);
1613 __dsos__fprintf(&dsos__user
, fp
);
1616 static size_t __dsos__fprintf_buildid(struct list_head
*head
, FILE *fp
)
1621 list_for_each_entry(pos
, head
, node
) {
1622 ret
+= dso__fprintf_buildid(pos
, fp
);
1623 ret
+= fprintf(fp
, " %s\n", pos
->long_name
);
1628 size_t dsos__fprintf_buildid(FILE *fp
)
1630 return (__dsos__fprintf_buildid(&dsos__kernel
, fp
) +
1631 __dsos__fprintf_buildid(&dsos__user
, fp
));
1634 static struct dso
*dsos__create_kernel( const char *vmlinux
)
1636 struct dso
*kernel
= dso__new(vmlinux
?: "[kernel.kallsyms]");
1641 kernel
->short_name
= "[kernel]";
1644 vdso
= dso__new("[vdso]");
1646 goto out_delete_kernel_dso
;
1647 dso__set_loaded(vdso
, MAP__FUNCTION
);
1649 if (sysfs__read_build_id("/sys/kernel/notes", kernel
->build_id
,
1650 sizeof(kernel
->build_id
)) == 0)
1651 kernel
->has_build_id
= true;
1653 dsos__add(&dsos__kernel
, kernel
);
1654 dsos__add(&dsos__user
, vdso
);
1658 out_delete_kernel_dso
:
1659 dso__delete(kernel
);
1663 static int map_groups__create_kernel_maps(struct map_groups
*self
, const char *vmlinux
)
1665 struct map
*functions
, *variables
;
1666 struct dso
*kernel
= dsos__create_kernel(vmlinux
);
1671 functions
= map__new2(0, kernel
, MAP__FUNCTION
);
1672 if (functions
== NULL
)
1675 variables
= map__new2(0, kernel
, MAP__VARIABLE
);
1676 if (variables
== NULL
) {
1677 map__delete(functions
);
1681 functions
->map_ip
= functions
->unmap_ip
=
1682 variables
->map_ip
= variables
->unmap_ip
= identity__map_ip
;
1683 map_groups__insert(self
, functions
);
1684 map_groups__insert(self
, variables
);
1689 static void vmlinux_path__exit(void)
1691 while (--vmlinux_path__nr_entries
>= 0) {
1692 free(vmlinux_path
[vmlinux_path__nr_entries
]);
1693 vmlinux_path
[vmlinux_path__nr_entries
] = NULL
;
1697 vmlinux_path
= NULL
;
1700 static int vmlinux_path__init(void)
1705 if (uname(&uts
) < 0)
1708 vmlinux_path
= malloc(sizeof(char *) * 5);
1709 if (vmlinux_path
== NULL
)
1712 vmlinux_path
[vmlinux_path__nr_entries
] = strdup("vmlinux");
1713 if (vmlinux_path
[vmlinux_path__nr_entries
] == NULL
)
1715 ++vmlinux_path__nr_entries
;
1716 vmlinux_path
[vmlinux_path__nr_entries
] = strdup("/boot/vmlinux");
1717 if (vmlinux_path
[vmlinux_path__nr_entries
] == NULL
)
1719 ++vmlinux_path__nr_entries
;
1720 snprintf(bf
, sizeof(bf
), "/boot/vmlinux-%s", uts
.release
);
1721 vmlinux_path
[vmlinux_path__nr_entries
] = strdup(bf
);
1722 if (vmlinux_path
[vmlinux_path__nr_entries
] == NULL
)
1724 ++vmlinux_path__nr_entries
;
1725 snprintf(bf
, sizeof(bf
), "/lib/modules/%s/build/vmlinux", uts
.release
);
1726 vmlinux_path
[vmlinux_path__nr_entries
] = strdup(bf
);
1727 if (vmlinux_path
[vmlinux_path__nr_entries
] == NULL
)
1729 ++vmlinux_path__nr_entries
;
1730 snprintf(bf
, sizeof(bf
), "/usr/lib/debug/lib/modules/%s/vmlinux",
1732 vmlinux_path
[vmlinux_path__nr_entries
] = strdup(bf
);
1733 if (vmlinux_path
[vmlinux_path__nr_entries
] == NULL
)
1735 ++vmlinux_path__nr_entries
;
1740 vmlinux_path__exit();
1744 static int setup_list(struct strlist
**list
, const char *list_str
,
1745 const char *list_name
)
1747 if (list_str
== NULL
)
1750 *list
= strlist__new(true, list_str
);
1752 pr_err("problems parsing %s list\n", list_name
);
1758 int symbol__init(void)
1760 elf_version(EV_CURRENT
);
1761 if (symbol_conf
.sort_by_name
)
1762 symbol_conf
.priv_size
+= (sizeof(struct symbol_name_rb_node
) -
1763 sizeof(struct symbol
));
1765 if (symbol_conf
.try_vmlinux_path
&& vmlinux_path__init() < 0)
1768 if (symbol_conf
.field_sep
&& *symbol_conf
.field_sep
== '.') {
1769 pr_err("'.' is the only non valid --field-separator argument\n");
1773 if (setup_list(&symbol_conf
.dso_list
,
1774 symbol_conf
.dso_list_str
, "dso") < 0)
1777 if (setup_list(&symbol_conf
.comm_list
,
1778 symbol_conf
.comm_list_str
, "comm") < 0)
1779 goto out_free_dso_list
;
1781 if (setup_list(&symbol_conf
.sym_list
,
1782 symbol_conf
.sym_list_str
, "symbol") < 0)
1783 goto out_free_comm_list
;
1788 strlist__delete(symbol_conf
.dso_list
);
1790 strlist__delete(symbol_conf
.comm_list
);
1794 int perf_session__create_kernel_maps(struct perf_session
*self
)
1796 if (map_groups__create_kernel_maps(&self
->kmaps
,
1797 symbol_conf
.vmlinux_name
) < 0)
1800 if (symbol_conf
.use_modules
&&
1801 perf_session__create_module_maps(self
) < 0)
1802 pr_debug("Failed to load list of modules for session %s, "
1803 "continuing...\n", self
->filename
);
1805 * Now that we have all the maps created, just set the ->end of them:
1807 map_groups__fixup_end(&self
->kmaps
);