11 #include <sys/param.h>
24 #include <sys/utsname.h>
27 #define KSYM_NAME_LEN 128
30 #ifndef NT_GNU_BUILD_ID
31 #define NT_GNU_BUILD_ID 3
34 static bool dso__build_id_equal(const struct dso
*self
, u8
*build_id
);
35 static int elf_read_build_id(Elf
*elf
, void *bf
, size_t size
);
36 static void dsos__add(struct list_head
*head
, struct dso
*dso
);
37 static struct map
*map__new2(u64 start
, struct dso
*dso
, enum map_type type
);
38 static int dso__load_kernel_sym(struct dso
*self
, struct map
*map
,
39 symbol_filter_t filter
);
40 static int dso__load_guest_kernel_sym(struct dso
*self
, struct map
*map
,
41 symbol_filter_t filter
);
42 static int vmlinux_path__nr_entries
;
43 static char **vmlinux_path
;
45 struct symbol_conf symbol_conf
= {
46 .exclude_other
= true,
48 .try_vmlinux_path
= true,
52 int dso__name_len(const struct dso
*self
)
55 return self
->long_name_len
;
57 return self
->short_name_len
;
60 bool dso__loaded(const struct dso
*self
, enum map_type type
)
62 return self
->loaded
& (1 << type
);
65 bool dso__sorted_by_name(const struct dso
*self
, enum map_type type
)
67 return self
->sorted_by_name
& (1 << type
);
70 static void dso__set_sorted_by_name(struct dso
*self
, enum map_type type
)
72 self
->sorted_by_name
|= (1 << type
);
75 bool symbol_type__is_a(char symbol_type
, enum map_type map_type
)
79 return symbol_type
== 'T' || symbol_type
== 'W';
81 return symbol_type
== 'D' || symbol_type
== 'd';
87 static void symbols__fixup_end(struct rb_root
*self
)
89 struct rb_node
*nd
, *prevnd
= rb_first(self
);
90 struct symbol
*curr
, *prev
;
95 curr
= rb_entry(prevnd
, struct symbol
, rb_node
);
97 for (nd
= rb_next(prevnd
); nd
; nd
= rb_next(nd
)) {
99 curr
= rb_entry(nd
, struct symbol
, rb_node
);
101 if (prev
->end
== prev
->start
&& prev
->end
!= curr
->start
)
102 prev
->end
= curr
->start
- 1;
106 if (curr
->end
== curr
->start
)
107 curr
->end
= roundup(curr
->start
, 4096);
110 static void __map_groups__fixup_end(struct map_groups
*self
, enum map_type type
)
112 struct map
*prev
, *curr
;
113 struct rb_node
*nd
, *prevnd
= rb_first(&self
->maps
[type
]);
118 curr
= rb_entry(prevnd
, struct map
, rb_node
);
120 for (nd
= rb_next(prevnd
); nd
; nd
= rb_next(nd
)) {
122 curr
= rb_entry(nd
, struct map
, rb_node
);
123 prev
->end
= curr
->start
- 1;
127 * We still haven't the actual symbols, so guess the
128 * last map final address.
133 static void map_groups__fixup_end(struct map_groups
*self
)
136 for (i
= 0; i
< MAP__NR_TYPES
; ++i
)
137 __map_groups__fixup_end(self
, i
);
140 static struct symbol
*symbol__new(u64 start
, u64 len
, u8 binding
,
143 size_t namelen
= strlen(name
) + 1;
144 struct symbol
*self
= calloc(1, (symbol_conf
.priv_size
+
145 sizeof(*self
) + namelen
));
149 if (symbol_conf
.priv_size
)
150 self
= ((void *)self
) + symbol_conf
.priv_size
;
153 self
->end
= len
? start
+ len
- 1 : start
;
154 self
->binding
= binding
;
155 self
->namelen
= namelen
- 1;
157 pr_debug4("%s: %s %#" PRIx64
"-%#" PRIx64
"\n", __func__
, name
, start
, self
->end
);
159 memcpy(self
->name
, name
, namelen
);
164 void symbol__delete(struct symbol
*self
)
166 free(((void *)self
) - symbol_conf
.priv_size
);
169 static size_t symbol__fprintf(struct symbol
*self
, FILE *fp
)
171 return fprintf(fp
, " %" PRIx64
"-%" PRIx64
" %c %s\n",
172 self
->start
, self
->end
,
173 self
->binding
== STB_GLOBAL
? 'g' :
174 self
->binding
== STB_LOCAL
? 'l' : 'w',
178 void dso__set_long_name(struct dso
*self
, char *name
)
182 self
->long_name
= name
;
183 self
->long_name_len
= strlen(name
);
186 static void dso__set_short_name(struct dso
*self
, const char *name
)
190 self
->short_name
= name
;
191 self
->short_name_len
= strlen(name
);
194 static void dso__set_basename(struct dso
*self
)
196 dso__set_short_name(self
, basename(self
->long_name
));
199 struct dso
*dso__new(const char *name
)
201 struct dso
*self
= calloc(1, sizeof(*self
) + strlen(name
) + 1);
205 strcpy(self
->name
, name
);
206 dso__set_long_name(self
, self
->name
);
207 dso__set_short_name(self
, self
->name
);
208 for (i
= 0; i
< MAP__NR_TYPES
; ++i
)
209 self
->symbols
[i
] = self
->symbol_names
[i
] = RB_ROOT
;
210 self
->symtab_type
= SYMTAB__NOT_FOUND
;
212 self
->sorted_by_name
= 0;
213 self
->has_build_id
= 0;
214 self
->kernel
= DSO_TYPE_USER
;
215 INIT_LIST_HEAD(&self
->node
);
221 static void symbols__delete(struct rb_root
*self
)
224 struct rb_node
*next
= rb_first(self
);
227 pos
= rb_entry(next
, struct symbol
, rb_node
);
228 next
= rb_next(&pos
->rb_node
);
229 rb_erase(&pos
->rb_node
, self
);
234 void dso__delete(struct dso
*self
)
237 for (i
= 0; i
< MAP__NR_TYPES
; ++i
)
238 symbols__delete(&self
->symbols
[i
]);
239 if (self
->sname_alloc
)
240 free((char *)self
->short_name
);
241 if (self
->lname_alloc
)
242 free(self
->long_name
);
246 void dso__set_build_id(struct dso
*self
, void *build_id
)
248 memcpy(self
->build_id
, build_id
, sizeof(self
->build_id
));
249 self
->has_build_id
= 1;
252 static void symbols__insert(struct rb_root
*self
, struct symbol
*sym
)
254 struct rb_node
**p
= &self
->rb_node
;
255 struct rb_node
*parent
= NULL
;
256 const u64 ip
= sym
->start
;
261 s
= rb_entry(parent
, struct symbol
, rb_node
);
267 rb_link_node(&sym
->rb_node
, parent
, p
);
268 rb_insert_color(&sym
->rb_node
, self
);
271 static struct symbol
*symbols__find(struct rb_root
*self
, u64 ip
)
281 struct symbol
*s
= rb_entry(n
, struct symbol
, rb_node
);
285 else if (ip
> s
->end
)
294 struct symbol_name_rb_node
{
295 struct rb_node rb_node
;
299 static void symbols__insert_by_name(struct rb_root
*self
, struct symbol
*sym
)
301 struct rb_node
**p
= &self
->rb_node
;
302 struct rb_node
*parent
= NULL
;
303 struct symbol_name_rb_node
*symn
, *s
;
305 symn
= container_of(sym
, struct symbol_name_rb_node
, sym
);
309 s
= rb_entry(parent
, struct symbol_name_rb_node
, rb_node
);
310 if (strcmp(sym
->name
, s
->sym
.name
) < 0)
315 rb_link_node(&symn
->rb_node
, parent
, p
);
316 rb_insert_color(&symn
->rb_node
, self
);
319 static void symbols__sort_by_name(struct rb_root
*self
, struct rb_root
*source
)
323 for (nd
= rb_first(source
); nd
; nd
= rb_next(nd
)) {
324 struct symbol
*pos
= rb_entry(nd
, struct symbol
, rb_node
);
325 symbols__insert_by_name(self
, pos
);
329 static struct symbol
*symbols__find_by_name(struct rb_root
*self
, const char *name
)
339 struct symbol_name_rb_node
*s
;
342 s
= rb_entry(n
, struct symbol_name_rb_node
, rb_node
);
343 cmp
= strcmp(name
, s
->sym
.name
);
356 struct symbol
*dso__find_symbol(struct dso
*self
,
357 enum map_type type
, u64 addr
)
359 return symbols__find(&self
->symbols
[type
], addr
);
362 struct symbol
*dso__find_symbol_by_name(struct dso
*self
, enum map_type type
,
365 return symbols__find_by_name(&self
->symbol_names
[type
], name
);
368 void dso__sort_by_name(struct dso
*self
, enum map_type type
)
370 dso__set_sorted_by_name(self
, type
);
371 return symbols__sort_by_name(&self
->symbol_names
[type
],
372 &self
->symbols
[type
]);
375 int build_id__sprintf(const u8
*self
, int len
, char *bf
)
378 const u8
*raw
= self
;
381 for (i
= 0; i
< len
; ++i
) {
382 sprintf(bid
, "%02x", *raw
);
390 size_t dso__fprintf_buildid(struct dso
*self
, FILE *fp
)
392 char sbuild_id
[BUILD_ID_SIZE
* 2 + 1];
394 build_id__sprintf(self
->build_id
, sizeof(self
->build_id
), sbuild_id
);
395 return fprintf(fp
, "%s", sbuild_id
);
398 size_t dso__fprintf_symbols_by_name(struct dso
*self
, enum map_type type
, FILE *fp
)
402 struct symbol_name_rb_node
*pos
;
404 for (nd
= rb_first(&self
->symbol_names
[type
]); nd
; nd
= rb_next(nd
)) {
405 pos
= rb_entry(nd
, struct symbol_name_rb_node
, rb_node
);
406 fprintf(fp
, "%s\n", pos
->sym
.name
);
412 size_t dso__fprintf(struct dso
*self
, enum map_type type
, FILE *fp
)
415 size_t ret
= fprintf(fp
, "dso: %s (", self
->short_name
);
417 if (self
->short_name
!= self
->long_name
)
418 ret
+= fprintf(fp
, "%s, ", self
->long_name
);
419 ret
+= fprintf(fp
, "%s, %sloaded, ", map_type__name
[type
],
420 self
->loaded
? "" : "NOT ");
421 ret
+= dso__fprintf_buildid(self
, fp
);
422 ret
+= fprintf(fp
, ")\n");
423 for (nd
= rb_first(&self
->symbols
[type
]); nd
; nd
= rb_next(nd
)) {
424 struct symbol
*pos
= rb_entry(nd
, struct symbol
, rb_node
);
425 ret
+= symbol__fprintf(pos
, fp
);
431 int kallsyms__parse(const char *filename
, void *arg
,
432 int (*process_symbol
)(void *arg
, const char *name
,
433 char type
, u64 start
, u64 end
))
439 char prev_symbol_type
= 0;
440 char *prev_symbol_name
;
441 FILE *file
= fopen(filename
, "r");
446 prev_symbol_name
= malloc(KSYM_NAME_LEN
);
447 if (prev_symbol_name
== NULL
)
452 while (!feof(file
)) {
458 line_len
= getline(&line
, &n
, file
);
459 if (line_len
< 0 || !line
)
462 line
[--line_len
] = '\0'; /* \n */
464 len
= hex2u64(line
, &start
);
467 if (len
+ 2 >= line_len
)
470 symbol_type
= toupper(line
[len
]);
472 symbol_name
= line
+ len
;
473 len
= line_len
- len
;
475 if (len
>= KSYM_NAME_LEN
) {
480 if (prev_symbol_type
) {
482 if (end
!= prev_start
)
484 err
= process_symbol(arg
, prev_symbol_name
,
485 prev_symbol_type
, prev_start
, end
);
490 memcpy(prev_symbol_name
, symbol_name
, len
+ 1);
491 prev_symbol_type
= symbol_type
;
495 free(prev_symbol_name
);
505 struct process_kallsyms_args
{
510 static u8
kallsyms2elf_type(char type
)
515 return isupper(type
) ? STB_GLOBAL
: STB_LOCAL
;
518 static int map__process_kallsym_symbol(void *arg
, const char *name
,
519 char type
, u64 start
, u64 end
)
522 struct process_kallsyms_args
*a
= arg
;
523 struct rb_root
*root
= &a
->dso
->symbols
[a
->map
->type
];
525 if (!symbol_type__is_a(type
, a
->map
->type
))
528 sym
= symbol__new(start
, end
- start
+ 1,
529 kallsyms2elf_type(type
), name
);
533 * We will pass the symbols to the filter later, in
534 * map__split_kallsyms, when we have split the maps per module
536 symbols__insert(root
, sym
);
542 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
543 * so that we can in the next step set the symbol ->end address and then
544 * call kernel_maps__split_kallsyms.
546 static int dso__load_all_kallsyms(struct dso
*self
, const char *filename
,
549 struct process_kallsyms_args args
= { .map
= map
, .dso
= self
, };
550 return kallsyms__parse(filename
, &args
, map__process_kallsym_symbol
);
554 * Split the symbols into maps, making sure there are no overlaps, i.e. the
555 * kernel range is broken in several maps, named [kernel].N, as we don't have
556 * the original ELF section names vmlinux have.
558 static int dso__split_kallsyms(struct dso
*self
, struct map
*map
,
559 symbol_filter_t filter
)
561 struct map_groups
*kmaps
= map__kmap(map
)->kmaps
;
562 struct machine
*machine
= kmaps
->machine
;
563 struct map
*curr_map
= map
;
565 int count
= 0, moved
= 0;
566 struct rb_root
*root
= &self
->symbols
[map
->type
];
567 struct rb_node
*next
= rb_first(root
);
568 int kernel_range
= 0;
573 pos
= rb_entry(next
, struct symbol
, rb_node
);
574 next
= rb_next(&pos
->rb_node
);
576 module
= strchr(pos
->name
, '\t');
578 if (!symbol_conf
.use_modules
)
583 if (strcmp(curr_map
->dso
->short_name
, module
)) {
584 if (curr_map
!= map
&&
585 self
->kernel
== DSO_TYPE_GUEST_KERNEL
&&
586 machine__is_default_guest(machine
)) {
588 * We assume all symbols of a module are
589 * continuous in * kallsyms, so curr_map
590 * points to a module and all its
591 * symbols are in its kmap. Mark it as
594 dso__set_loaded(curr_map
->dso
,
598 curr_map
= map_groups__find_by_name(kmaps
,
600 if (curr_map
== NULL
) {
601 pr_debug("%s/proc/{kallsyms,modules} "
602 "inconsistency while looking "
603 "for \"%s\" module!\n",
604 machine
->root_dir
, module
);
609 if (curr_map
->dso
->loaded
&&
610 !machine__is_default_guest(machine
))
614 * So that we look just like we get from .ko files,
615 * i.e. not prelinked, relative to map->start.
617 pos
->start
= curr_map
->map_ip(curr_map
, pos
->start
);
618 pos
->end
= curr_map
->map_ip(curr_map
, pos
->end
);
619 } else if (curr_map
!= map
) {
620 char dso_name
[PATH_MAX
];
628 if (self
->kernel
== DSO_TYPE_GUEST_KERNEL
)
629 snprintf(dso_name
, sizeof(dso_name
),
633 snprintf(dso_name
, sizeof(dso_name
),
637 dso
= dso__new(dso_name
);
641 dso
->kernel
= self
->kernel
;
643 curr_map
= map__new2(pos
->start
, dso
, map
->type
);
644 if (curr_map
== NULL
) {
649 curr_map
->map_ip
= curr_map
->unmap_ip
= identity__map_ip
;
650 map_groups__insert(kmaps
, curr_map
);
654 if (filter
&& filter(curr_map
, pos
)) {
655 discard_symbol
: rb_erase(&pos
->rb_node
, root
);
658 if (curr_map
!= map
) {
659 rb_erase(&pos
->rb_node
, root
);
660 symbols__insert(&curr_map
->dso
->symbols
[curr_map
->type
], pos
);
667 if (curr_map
!= map
&&
668 self
->kernel
== DSO_TYPE_GUEST_KERNEL
&&
669 machine__is_default_guest(kmaps
->machine
)) {
670 dso__set_loaded(curr_map
->dso
, curr_map
->type
);
673 return count
+ moved
;
676 int dso__load_kallsyms(struct dso
*self
, const char *filename
,
677 struct map
*map
, symbol_filter_t filter
)
679 if (dso__load_all_kallsyms(self
, filename
, map
) < 0)
682 if (self
->kernel
== DSO_TYPE_GUEST_KERNEL
)
683 self
->symtab_type
= SYMTAB__GUEST_KALLSYMS
;
685 self
->symtab_type
= SYMTAB__KALLSYMS
;
687 return dso__split_kallsyms(self
, map
, filter
);
690 static int dso__load_perf_map(struct dso
*self
, struct map
*map
,
691 symbol_filter_t filter
)
698 file
= fopen(self
->long_name
, "r");
702 while (!feof(file
)) {
707 line_len
= getline(&line
, &n
, file
);
714 line
[--line_len
] = '\0'; /* \n */
716 len
= hex2u64(line
, &start
);
719 if (len
+ 2 >= line_len
)
722 len
+= hex2u64(line
+ len
, &size
);
725 if (len
+ 2 >= line_len
)
728 sym
= symbol__new(start
, size
, STB_GLOBAL
, line
+ len
);
731 goto out_delete_line
;
733 if (filter
&& filter(map
, sym
))
736 symbols__insert(&self
->symbols
[map
->type
], sym
);
753 * elf_symtab__for_each_symbol - iterate thru all the symbols
755 * @self: struct elf_symtab instance to iterate
757 * @sym: GElf_Sym iterator
759 #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
760 for (idx = 0, gelf_getsym(syms, idx, &sym);\
762 idx++, gelf_getsym(syms, idx, &sym))
764 static inline uint8_t elf_sym__type(const GElf_Sym
*sym
)
766 return GELF_ST_TYPE(sym
->st_info
);
769 static inline int elf_sym__is_function(const GElf_Sym
*sym
)
771 return elf_sym__type(sym
) == STT_FUNC
&&
773 sym
->st_shndx
!= SHN_UNDEF
;
776 static inline bool elf_sym__is_object(const GElf_Sym
*sym
)
778 return elf_sym__type(sym
) == STT_OBJECT
&&
780 sym
->st_shndx
!= SHN_UNDEF
;
783 static inline int elf_sym__is_label(const GElf_Sym
*sym
)
785 return elf_sym__type(sym
) == STT_NOTYPE
&&
787 sym
->st_shndx
!= SHN_UNDEF
&&
788 sym
->st_shndx
!= SHN_ABS
;
791 static inline const char *elf_sec__name(const GElf_Shdr
*shdr
,
792 const Elf_Data
*secstrs
)
794 return secstrs
->d_buf
+ shdr
->sh_name
;
797 static inline int elf_sec__is_text(const GElf_Shdr
*shdr
,
798 const Elf_Data
*secstrs
)
800 return strstr(elf_sec__name(shdr
, secstrs
), "text") != NULL
;
803 static inline bool elf_sec__is_data(const GElf_Shdr
*shdr
,
804 const Elf_Data
*secstrs
)
806 return strstr(elf_sec__name(shdr
, secstrs
), "data") != NULL
;
809 static inline const char *elf_sym__name(const GElf_Sym
*sym
,
810 const Elf_Data
*symstrs
)
812 return symstrs
->d_buf
+ sym
->st_name
;
815 static Elf_Scn
*elf_section_by_name(Elf
*elf
, GElf_Ehdr
*ep
,
816 GElf_Shdr
*shp
, const char *name
,
822 while ((sec
= elf_nextscn(elf
, sec
)) != NULL
) {
825 gelf_getshdr(sec
, shp
);
826 str
= elf_strptr(elf
, ep
->e_shstrndx
, shp
->sh_name
);
827 if (!strcmp(name
, str
)) {
838 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
839 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
841 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
843 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
844 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
846 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
849 * We need to check if we have a .dynsym, so that we can handle the
850 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
851 * .dynsym or .symtab).
852 * And always look at the original dso, not at debuginfo packages, that
853 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
855 static int dso__synthesize_plt_symbols(struct dso
*self
, struct map
*map
,
856 symbol_filter_t filter
)
858 uint32_t nr_rel_entries
, idx
;
863 GElf_Shdr shdr_rel_plt
, shdr_dynsym
;
864 Elf_Data
*reldata
, *syms
, *symstrs
;
865 Elf_Scn
*scn_plt_rel
, *scn_symstrs
, *scn_dynsym
;
868 char sympltname
[1024];
870 int nr
= 0, symidx
, fd
, err
= 0;
873 snprintf(name
, sizeof(name
), "%s%s",
874 symbol_conf
.symfs
, self
->long_name
);
875 fd
= open(name
, O_RDONLY
);
879 elf
= elf_begin(fd
, PERF_ELF_C_READ_MMAP
, NULL
);
883 if (gelf_getehdr(elf
, &ehdr
) == NULL
)
886 scn_dynsym
= elf_section_by_name(elf
, &ehdr
, &shdr_dynsym
,
887 ".dynsym", &dynsym_idx
);
888 if (scn_dynsym
== NULL
)
891 scn_plt_rel
= elf_section_by_name(elf
, &ehdr
, &shdr_rel_plt
,
893 if (scn_plt_rel
== NULL
) {
894 scn_plt_rel
= elf_section_by_name(elf
, &ehdr
, &shdr_rel_plt
,
896 if (scn_plt_rel
== NULL
)
902 if (shdr_rel_plt
.sh_link
!= dynsym_idx
)
905 if (elf_section_by_name(elf
, &ehdr
, &shdr_plt
, ".plt", NULL
) == NULL
)
909 * Fetch the relocation section to find the idxes to the GOT
910 * and the symbols in the .dynsym they refer to.
912 reldata
= elf_getdata(scn_plt_rel
, NULL
);
916 syms
= elf_getdata(scn_dynsym
, NULL
);
920 scn_symstrs
= elf_getscn(elf
, shdr_dynsym
.sh_link
);
921 if (scn_symstrs
== NULL
)
924 symstrs
= elf_getdata(scn_symstrs
, NULL
);
928 nr_rel_entries
= shdr_rel_plt
.sh_size
/ shdr_rel_plt
.sh_entsize
;
929 plt_offset
= shdr_plt
.sh_offset
;
931 if (shdr_rel_plt
.sh_type
== SHT_RELA
) {
932 GElf_Rela pos_mem
, *pos
;
934 elf_section__for_each_rela(reldata
, pos
, pos_mem
, idx
,
936 symidx
= GELF_R_SYM(pos
->r_info
);
937 plt_offset
+= shdr_plt
.sh_entsize
;
938 gelf_getsym(syms
, symidx
, &sym
);
939 snprintf(sympltname
, sizeof(sympltname
),
940 "%s@plt", elf_sym__name(&sym
, symstrs
));
942 f
= symbol__new(plt_offset
, shdr_plt
.sh_entsize
,
943 STB_GLOBAL
, sympltname
);
947 if (filter
&& filter(map
, f
))
950 symbols__insert(&self
->symbols
[map
->type
], f
);
954 } else if (shdr_rel_plt
.sh_type
== SHT_REL
) {
955 GElf_Rel pos_mem
, *pos
;
956 elf_section__for_each_rel(reldata
, pos
, pos_mem
, idx
,
958 symidx
= GELF_R_SYM(pos
->r_info
);
959 plt_offset
+= shdr_plt
.sh_entsize
;
960 gelf_getsym(syms
, symidx
, &sym
);
961 snprintf(sympltname
, sizeof(sympltname
),
962 "%s@plt", elf_sym__name(&sym
, symstrs
));
964 f
= symbol__new(plt_offset
, shdr_plt
.sh_entsize
,
965 STB_GLOBAL
, sympltname
);
969 if (filter
&& filter(map
, f
))
972 symbols__insert(&self
->symbols
[map
->type
], f
);
987 pr_debug("%s: problems reading %s PLT info.\n",
988 __func__
, self
->long_name
);
992 static bool elf_sym__is_a(GElf_Sym
*self
, enum map_type type
)
996 return elf_sym__is_function(self
);
998 return elf_sym__is_object(self
);
1004 static bool elf_sec__is_a(GElf_Shdr
*self
, Elf_Data
*secstrs
, enum map_type type
)
1008 return elf_sec__is_text(self
, secstrs
);
1010 return elf_sec__is_data(self
, secstrs
);
1016 static size_t elf_addr_to_index(Elf
*elf
, GElf_Addr addr
)
1018 Elf_Scn
*sec
= NULL
;
1022 while ((sec
= elf_nextscn(elf
, sec
)) != NULL
) {
1023 gelf_getshdr(sec
, &shdr
);
1025 if ((addr
>= shdr
.sh_addr
) &&
1026 (addr
< (shdr
.sh_addr
+ shdr
.sh_size
)))
1035 static int dso__load_sym(struct dso
*self
, struct map
*map
, const char *name
,
1036 int fd
, symbol_filter_t filter
, int kmodule
,
1039 struct kmap
*kmap
= self
->kernel
? map__kmap(map
) : NULL
;
1040 struct map
*curr_map
= map
;
1041 struct dso
*curr_dso
= self
;
1042 Elf_Data
*symstrs
, *secstrs
;
1047 GElf_Shdr shdr
, opdshdr
;
1048 Elf_Data
*syms
, *opddata
= NULL
;
1050 Elf_Scn
*sec
, *sec_strndx
, *opdsec
;
1055 elf
= elf_begin(fd
, PERF_ELF_C_READ_MMAP
, NULL
);
1057 pr_debug("%s: cannot read %s ELF file.\n", __func__
, name
);
1061 if (gelf_getehdr(elf
, &ehdr
) == NULL
) {
1062 pr_debug("%s: cannot get elf header.\n", __func__
);
1066 /* Always reject images with a mismatched build-id: */
1067 if (self
->has_build_id
) {
1068 u8 build_id
[BUILD_ID_SIZE
];
1070 if (elf_read_build_id(elf
, build_id
,
1071 BUILD_ID_SIZE
) != BUILD_ID_SIZE
)
1074 if (!dso__build_id_equal(self
, build_id
))
1078 sec
= elf_section_by_name(elf
, &ehdr
, &shdr
, ".symtab", NULL
);
1083 sec
= elf_section_by_name(elf
, &ehdr
, &shdr
, ".dynsym", NULL
);
1088 opdsec
= elf_section_by_name(elf
, &ehdr
, &opdshdr
, ".opd", &opdidx
);
1090 opddata
= elf_rawdata(opdsec
, NULL
);
1092 syms
= elf_getdata(sec
, NULL
);
1096 sec
= elf_getscn(elf
, shdr
.sh_link
);
1100 symstrs
= elf_getdata(sec
, NULL
);
1101 if (symstrs
== NULL
)
1104 sec_strndx
= elf_getscn(elf
, ehdr
.e_shstrndx
);
1105 if (sec_strndx
== NULL
)
1108 secstrs
= elf_getdata(sec_strndx
, NULL
);
1109 if (secstrs
== NULL
)
1112 nr_syms
= shdr
.sh_size
/ shdr
.sh_entsize
;
1114 memset(&sym
, 0, sizeof(sym
));
1115 if (self
->kernel
== DSO_TYPE_USER
) {
1116 self
->adjust_symbols
= (ehdr
.e_type
== ET_EXEC
||
1117 elf_section_by_name(elf
, &ehdr
, &shdr
,
1118 ".gnu.prelink_undo",
1120 } else self
->adjust_symbols
= 0;
1122 elf_symtab__for_each_symbol(syms
, nr_syms
, idx
, sym
) {
1124 const char *elf_name
= elf_sym__name(&sym
, symstrs
);
1125 char *demangled
= NULL
;
1126 int is_label
= elf_sym__is_label(&sym
);
1127 const char *section_name
;
1129 if (kmap
&& kmap
->ref_reloc_sym
&& kmap
->ref_reloc_sym
->name
&&
1130 strcmp(elf_name
, kmap
->ref_reloc_sym
->name
) == 0)
1131 kmap
->ref_reloc_sym
->unrelocated_addr
= sym
.st_value
;
1133 if (!is_label
&& !elf_sym__is_a(&sym
, map
->type
))
1136 /* Reject ARM ELF "mapping symbols": these aren't unique and
1137 * don't identify functions, so will confuse the profile
1139 if (ehdr
.e_machine
== EM_ARM
) {
1140 if (!strcmp(elf_name
, "$a") ||
1141 !strcmp(elf_name
, "$d") ||
1142 !strcmp(elf_name
, "$t"))
1146 if (opdsec
&& sym
.st_shndx
== opdidx
) {
1147 u32 offset
= sym
.st_value
- opdshdr
.sh_addr
;
1148 u64
*opd
= opddata
->d_buf
+ offset
;
1149 sym
.st_value
= *opd
;
1150 sym
.st_shndx
= elf_addr_to_index(elf
, sym
.st_value
);
1153 sec
= elf_getscn(elf
, sym
.st_shndx
);
1157 gelf_getshdr(sec
, &shdr
);
1159 if (is_label
&& !elf_sec__is_a(&shdr
, secstrs
, map
->type
))
1162 section_name
= elf_sec__name(&shdr
, secstrs
);
1164 /* On ARM, symbols for thumb functions have 1 added to
1165 * the symbol address as a flag - remove it */
1166 if ((ehdr
.e_machine
== EM_ARM
) &&
1167 (map
->type
== MAP__FUNCTION
) &&
1171 if (self
->kernel
!= DSO_TYPE_USER
|| kmodule
) {
1172 char dso_name
[PATH_MAX
];
1174 if (strcmp(section_name
,
1175 (curr_dso
->short_name
+
1176 self
->short_name_len
)) == 0)
1179 if (strcmp(section_name
, ".text") == 0) {
1185 snprintf(dso_name
, sizeof(dso_name
),
1186 "%s%s", self
->short_name
, section_name
);
1188 curr_map
= map_groups__find_by_name(kmap
->kmaps
, map
->type
, dso_name
);
1189 if (curr_map
== NULL
) {
1190 u64 start
= sym
.st_value
;
1193 start
+= map
->start
+ shdr
.sh_offset
;
1195 curr_dso
= dso__new(dso_name
);
1196 if (curr_dso
== NULL
)
1198 curr_dso
->kernel
= self
->kernel
;
1199 curr_dso
->long_name
= self
->long_name
;
1200 curr_dso
->long_name_len
= self
->long_name_len
;
1201 curr_map
= map__new2(start
, curr_dso
,
1203 if (curr_map
== NULL
) {
1204 dso__delete(curr_dso
);
1207 curr_map
->map_ip
= identity__map_ip
;
1208 curr_map
->unmap_ip
= identity__map_ip
;
1209 curr_dso
->symtab_type
= self
->symtab_type
;
1210 map_groups__insert(kmap
->kmaps
, curr_map
);
1211 dsos__add(&self
->node
, curr_dso
);
1212 dso__set_loaded(curr_dso
, map
->type
);
1214 curr_dso
= curr_map
->dso
;
1219 if (curr_dso
->adjust_symbols
) {
1220 pr_debug4("%s: adjusting symbol: st_value: %#" PRIx64
" "
1221 "sh_addr: %#" PRIx64
" sh_offset: %#" PRIx64
"\n", __func__
,
1222 (u64
)sym
.st_value
, (u64
)shdr
.sh_addr
,
1223 (u64
)shdr
.sh_offset
);
1224 sym
.st_value
-= shdr
.sh_addr
- shdr
.sh_offset
;
1227 * We need to figure out if the object was created from C++ sources
1228 * DWARF DW_compile_unit has this, but we don't always have access
1231 demangled
= bfd_demangle(NULL
, elf_name
, DMGL_PARAMS
| DMGL_ANSI
);
1232 if (demangled
!= NULL
)
1233 elf_name
= demangled
;
1235 f
= symbol__new(sym
.st_value
, sym
.st_size
,
1236 GELF_ST_BIND(sym
.st_info
), elf_name
);
1241 if (filter
&& filter(curr_map
, f
))
1244 symbols__insert(&curr_dso
->symbols
[curr_map
->type
], f
);
1250 * For misannotated, zeroed, ASM function sizes.
1253 symbols__fixup_end(&self
->symbols
[map
->type
]);
1256 * We need to fixup this here too because we create new
1257 * maps here, for things like vsyscall sections.
1259 __map_groups__fixup_end(kmap
->kmaps
, map
->type
);
1269 static bool dso__build_id_equal(const struct dso
*self
, u8
*build_id
)
1271 return memcmp(self
->build_id
, build_id
, sizeof(self
->build_id
)) == 0;
1274 bool __dsos__read_build_ids(struct list_head
*head
, bool with_hits
)
1276 bool have_build_id
= false;
1279 list_for_each_entry(pos
, head
, node
) {
1280 if (with_hits
&& !pos
->hit
)
1282 if (pos
->has_build_id
) {
1283 have_build_id
= true;
1286 if (filename__read_build_id(pos
->long_name
, pos
->build_id
,
1287 sizeof(pos
->build_id
)) > 0) {
1288 have_build_id
= true;
1289 pos
->has_build_id
= true;
1293 return have_build_id
;
1297 * Align offset to 4 bytes as needed for note name and descriptor data.
1299 #define NOTE_ALIGN(n) (((n) + 3) & -4U)
1301 static int elf_read_build_id(Elf
*elf
, void *bf
, size_t size
)
1311 if (size
< BUILD_ID_SIZE
)
1315 if (ek
!= ELF_K_ELF
)
1318 if (gelf_getehdr(elf
, &ehdr
) == NULL
) {
1319 pr_err("%s: cannot get elf header.\n", __func__
);
1323 sec
= elf_section_by_name(elf
, &ehdr
, &shdr
,
1324 ".note.gnu.build-id", NULL
);
1326 sec
= elf_section_by_name(elf
, &ehdr
, &shdr
,
1332 data
= elf_getdata(sec
, NULL
);
1337 while (ptr
< (data
->d_buf
+ data
->d_size
)) {
1338 GElf_Nhdr
*nhdr
= ptr
;
1339 int namesz
= NOTE_ALIGN(nhdr
->n_namesz
),
1340 descsz
= NOTE_ALIGN(nhdr
->n_descsz
);
1343 ptr
+= sizeof(*nhdr
);
1346 if (nhdr
->n_type
== NT_GNU_BUILD_ID
&&
1347 nhdr
->n_namesz
== sizeof("GNU")) {
1348 if (memcmp(name
, "GNU", sizeof("GNU")) == 0) {
1349 memcpy(bf
, ptr
, BUILD_ID_SIZE
);
1350 err
= BUILD_ID_SIZE
;
1361 int filename__read_build_id(const char *filename
, void *bf
, size_t size
)
1366 if (size
< BUILD_ID_SIZE
)
1369 fd
= open(filename
, O_RDONLY
);
1373 elf
= elf_begin(fd
, PERF_ELF_C_READ_MMAP
, NULL
);
1375 pr_debug2("%s: cannot read %s ELF file.\n", __func__
, filename
);
1379 err
= elf_read_build_id(elf
, bf
, size
);
1388 int sysfs__read_build_id(const char *filename
, void *build_id
, size_t size
)
1392 if (size
< BUILD_ID_SIZE
)
1395 fd
= open(filename
, O_RDONLY
);
1404 if (read(fd
, &nhdr
, sizeof(nhdr
)) != sizeof(nhdr
))
1407 namesz
= NOTE_ALIGN(nhdr
.n_namesz
);
1408 descsz
= NOTE_ALIGN(nhdr
.n_descsz
);
1409 if (nhdr
.n_type
== NT_GNU_BUILD_ID
&&
1410 nhdr
.n_namesz
== sizeof("GNU")) {
1411 if (read(fd
, bf
, namesz
) != namesz
)
1413 if (memcmp(bf
, "GNU", sizeof("GNU")) == 0) {
1414 if (read(fd
, build_id
,
1415 BUILD_ID_SIZE
) == BUILD_ID_SIZE
) {
1419 } else if (read(fd
, bf
, descsz
) != descsz
)
1422 int n
= namesz
+ descsz
;
1423 if (read(fd
, bf
, n
) != n
)
1432 char dso__symtab_origin(const struct dso
*self
)
1434 static const char origin
[] = {
1435 [SYMTAB__KALLSYMS
] = 'k',
1436 [SYMTAB__JAVA_JIT
] = 'j',
1437 [SYMTAB__BUILD_ID_CACHE
] = 'B',
1438 [SYMTAB__FEDORA_DEBUGINFO
] = 'f',
1439 [SYMTAB__UBUNTU_DEBUGINFO
] = 'u',
1440 [SYMTAB__BUILDID_DEBUGINFO
] = 'b',
1441 [SYMTAB__SYSTEM_PATH_DSO
] = 'd',
1442 [SYMTAB__SYSTEM_PATH_KMODULE
] = 'K',
1443 [SYMTAB__GUEST_KALLSYMS
] = 'g',
1444 [SYMTAB__GUEST_KMODULE
] = 'G',
1447 if (self
== NULL
|| self
->symtab_type
== SYMTAB__NOT_FOUND
)
1449 return origin
[self
->symtab_type
];
1452 int dso__load(struct dso
*self
, struct map
*map
, symbol_filter_t filter
)
1454 int size
= PATH_MAX
;
1458 struct machine
*machine
;
1459 const char *root_dir
;
1462 dso__set_loaded(self
, map
->type
);
1464 if (self
->kernel
== DSO_TYPE_KERNEL
)
1465 return dso__load_kernel_sym(self
, map
, filter
);
1466 else if (self
->kernel
== DSO_TYPE_GUEST_KERNEL
)
1467 return dso__load_guest_kernel_sym(self
, map
, filter
);
1469 if (map
->groups
&& map
->groups
->machine
)
1470 machine
= map
->groups
->machine
;
1474 name
= malloc(size
);
1478 self
->adjust_symbols
= 0;
1480 if (strncmp(self
->name
, "/tmp/perf-", 10) == 0) {
1481 ret
= dso__load_perf_map(self
, map
, filter
);
1482 self
->symtab_type
= ret
> 0 ? SYMTAB__JAVA_JIT
:
1487 /* Iterate over candidate debug images.
1488 * On the first pass, only load images if they have a full symtab.
1489 * Failing that, do a second pass where we accept .dynsym also
1493 for (self
->symtab_type
= SYMTAB__BUILD_ID_CACHE
;
1494 self
->symtab_type
!= SYMTAB__NOT_FOUND
;
1495 self
->symtab_type
++) {
1496 switch (self
->symtab_type
) {
1497 case SYMTAB__BUILD_ID_CACHE
:
1498 /* skip the locally configured cache if a symfs is given */
1499 if (symbol_conf
.symfs
[0] ||
1500 (dso__build_id_filename(self
, name
, size
) == NULL
)) {
1504 case SYMTAB__FEDORA_DEBUGINFO
:
1505 snprintf(name
, size
, "%s/usr/lib/debug%s.debug",
1506 symbol_conf
.symfs
, self
->long_name
);
1508 case SYMTAB__UBUNTU_DEBUGINFO
:
1509 snprintf(name
, size
, "%s/usr/lib/debug%s",
1510 symbol_conf
.symfs
, self
->long_name
);
1512 case SYMTAB__BUILDID_DEBUGINFO
: {
1513 char build_id_hex
[BUILD_ID_SIZE
* 2 + 1];
1515 if (!self
->has_build_id
)
1518 build_id__sprintf(self
->build_id
,
1519 sizeof(self
->build_id
),
1521 snprintf(name
, size
,
1522 "%s/usr/lib/debug/.build-id/%.2s/%s.debug",
1523 symbol_conf
.symfs
, build_id_hex
, build_id_hex
+ 2);
1526 case SYMTAB__SYSTEM_PATH_DSO
:
1527 snprintf(name
, size
, "%s%s",
1528 symbol_conf
.symfs
, self
->long_name
);
1530 case SYMTAB__GUEST_KMODULE
:
1531 if (map
->groups
&& machine
)
1532 root_dir
= machine
->root_dir
;
1535 snprintf(name
, size
, "%s%s%s", symbol_conf
.symfs
,
1536 root_dir
, self
->long_name
);
1539 case SYMTAB__SYSTEM_PATH_KMODULE
:
1540 snprintf(name
, size
, "%s%s", symbol_conf
.symfs
,
1546 /* Name is now the name of the next image to try */
1547 fd
= open(name
, O_RDONLY
);
1551 ret
= dso__load_sym(self
, map
, name
, fd
, filter
, 0,
1556 * Some people seem to have debuginfo files _WITHOUT_ debug
1563 int nr_plt
= dso__synthesize_plt_symbols(self
, map
, filter
);
1571 * If we wanted a full symtab but no image had one,
1572 * relax our requirements and repeat the search.
1574 if (ret
<= 0 && want_symtab
) {
1580 if (ret
< 0 && strstr(self
->name
, " (deleted)") != NULL
)
1585 struct map
*map_groups__find_by_name(struct map_groups
*self
,
1586 enum map_type type
, const char *name
)
1590 for (nd
= rb_first(&self
->maps
[type
]); nd
; nd
= rb_next(nd
)) {
1591 struct map
*map
= rb_entry(nd
, struct map
, rb_node
);
1593 if (map
->dso
&& strcmp(map
->dso
->short_name
, name
) == 0)
1600 static int dso__kernel_module_get_build_id(struct dso
*self
,
1601 const char *root_dir
)
1603 char filename
[PATH_MAX
];
1605 * kernel module short names are of the form "[module]" and
1606 * we need just "module" here.
1608 const char *name
= self
->short_name
+ 1;
1610 snprintf(filename
, sizeof(filename
),
1611 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1612 root_dir
, (int)strlen(name
) - 1, name
);
1614 if (sysfs__read_build_id(filename
, self
->build_id
,
1615 sizeof(self
->build_id
)) == 0)
1616 self
->has_build_id
= true;
1621 static int map_groups__set_modules_path_dir(struct map_groups
*self
,
1622 const char *dir_name
)
1624 struct dirent
*dent
;
1625 DIR *dir
= opendir(dir_name
);
1629 pr_debug("%s: cannot open %s dir\n", __func__
, dir_name
);
1633 while ((dent
= readdir(dir
)) != NULL
) {
1634 char path
[PATH_MAX
];
1637 /*sshfs might return bad dent->d_type, so we have to stat*/
1638 sprintf(path
, "%s/%s", dir_name
, dent
->d_name
);
1639 if (stat(path
, &st
))
1642 if (S_ISDIR(st
.st_mode
)) {
1643 if (!strcmp(dent
->d_name
, ".") ||
1644 !strcmp(dent
->d_name
, ".."))
1647 snprintf(path
, sizeof(path
), "%s/%s",
1648 dir_name
, dent
->d_name
);
1649 ret
= map_groups__set_modules_path_dir(self
, path
);
1653 char *dot
= strrchr(dent
->d_name
, '.'),
1658 if (dot
== NULL
|| strcmp(dot
, ".ko"))
1660 snprintf(dso_name
, sizeof(dso_name
), "[%.*s]",
1661 (int)(dot
- dent
->d_name
), dent
->d_name
);
1663 strxfrchar(dso_name
, '-', '_');
1664 map
= map_groups__find_by_name(self
, MAP__FUNCTION
, dso_name
);
1668 snprintf(path
, sizeof(path
), "%s/%s",
1669 dir_name
, dent
->d_name
);
1671 long_name
= strdup(path
);
1672 if (long_name
== NULL
) {
1676 dso__set_long_name(map
->dso
, long_name
);
1677 map
->dso
->lname_alloc
= 1;
1678 dso__kernel_module_get_build_id(map
->dso
, "");
1687 static char *get_kernel_version(const char *root_dir
)
1689 char version
[PATH_MAX
];
1692 const char *prefix
= "Linux version ";
1694 sprintf(version
, "%s/proc/version", root_dir
);
1695 file
= fopen(version
, "r");
1700 tmp
= fgets(version
, sizeof(version
), file
);
1703 name
= strstr(version
, prefix
);
1706 name
+= strlen(prefix
);
1707 tmp
= strchr(name
, ' ');
1711 return strdup(name
);
1714 static int machine__set_modules_path(struct machine
*self
)
1717 char modules_path
[PATH_MAX
];
1719 version
= get_kernel_version(self
->root_dir
);
1723 snprintf(modules_path
, sizeof(modules_path
), "%s/lib/modules/%s/kernel",
1724 self
->root_dir
, version
);
1727 return map_groups__set_modules_path_dir(&self
->kmaps
, modules_path
);
1731 * Constructor variant for modules (where we know from /proc/modules where
1732 * they are loaded) and for vmlinux, where only after we load all the
1733 * symbols we'll know where it starts and ends.
1735 static struct map
*map__new2(u64 start
, struct dso
*dso
, enum map_type type
)
1737 struct map
*self
= calloc(1, (sizeof(*self
) +
1738 (dso
->kernel
? sizeof(struct kmap
) : 0)));
1741 * ->end will be filled after we load all the symbols
1743 map__init(self
, type
, start
, 0, 0, dso
);
1749 struct map
*machine__new_module(struct machine
*self
, u64 start
,
1750 const char *filename
)
1753 struct dso
*dso
= __dsos__findnew(&self
->kernel_dsos
, filename
);
1758 map
= map__new2(start
, dso
, MAP__FUNCTION
);
1762 if (machine__is_host(self
))
1763 dso
->symtab_type
= SYMTAB__SYSTEM_PATH_KMODULE
;
1765 dso
->symtab_type
= SYMTAB__GUEST_KMODULE
;
1766 map_groups__insert(&self
->kmaps
, map
);
1770 static int machine__create_modules(struct machine
*self
)
1776 const char *modules
;
1777 char path
[PATH_MAX
];
1779 if (machine__is_default_guest(self
))
1780 modules
= symbol_conf
.default_guest_modules
;
1782 sprintf(path
, "%s/proc/modules", self
->root_dir
);
1786 file
= fopen(modules
, "r");
1790 while (!feof(file
)) {
1791 char name
[PATH_MAX
];
1796 line_len
= getline(&line
, &n
, file
);
1803 line
[--line_len
] = '\0'; /* \n */
1805 sep
= strrchr(line
, 'x');
1809 hex2u64(sep
+ 1, &start
);
1811 sep
= strchr(line
, ' ');
1817 snprintf(name
, sizeof(name
), "[%s]", line
);
1818 map
= machine__new_module(self
, start
, name
);
1820 goto out_delete_line
;
1821 dso__kernel_module_get_build_id(map
->dso
, self
->root_dir
);
1827 return machine__set_modules_path(self
);
1835 int dso__load_vmlinux(struct dso
*self
, struct map
*map
,
1836 const char *vmlinux
, symbol_filter_t filter
)
1839 char symfs_vmlinux
[PATH_MAX
];
1841 snprintf(symfs_vmlinux
, sizeof(symfs_vmlinux
), "%s%s",
1842 symbol_conf
.symfs
, vmlinux
);
1843 fd
= open(symfs_vmlinux
, O_RDONLY
);
1847 dso__set_long_name(self
, (char *)vmlinux
);
1848 dso__set_loaded(self
, map
->type
);
1849 err
= dso__load_sym(self
, map
, symfs_vmlinux
, fd
, filter
, 0, 0);
1853 pr_debug("Using %s for symbols\n", symfs_vmlinux
);
1858 int dso__load_vmlinux_path(struct dso
*self
, struct map
*map
,
1859 symbol_filter_t filter
)
1864 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1865 vmlinux_path__nr_entries
+ 1);
1867 filename
= dso__build_id_filename(self
, NULL
, 0);
1868 if (filename
!= NULL
) {
1869 err
= dso__load_vmlinux(self
, map
, filename
, filter
);
1871 dso__set_long_name(self
, filename
);
1877 for (i
= 0; i
< vmlinux_path__nr_entries
; ++i
) {
1878 err
= dso__load_vmlinux(self
, map
, vmlinux_path
[i
], filter
);
1880 dso__set_long_name(self
, strdup(vmlinux_path
[i
]));
1888 static int dso__load_kernel_sym(struct dso
*self
, struct map
*map
,
1889 symbol_filter_t filter
)
1892 const char *kallsyms_filename
= NULL
;
1893 char *kallsyms_allocated_filename
= NULL
;
1895 * Step 1: if the user specified a kallsyms or vmlinux filename, use
1896 * it and only it, reporting errors to the user if it cannot be used.
1898 * For instance, try to analyse an ARM perf.data file _without_ a
1899 * build-id, or if the user specifies the wrong path to the right
1900 * vmlinux file, obviously we can't fallback to another vmlinux (a
1901 * x86_86 one, on the machine where analysis is being performed, say),
1902 * or worse, /proc/kallsyms.
1904 * If the specified file _has_ a build-id and there is a build-id
1905 * section in the perf.data file, we will still do the expected
1906 * validation in dso__load_vmlinux and will bail out if they don't
1909 if (symbol_conf
.kallsyms_name
!= NULL
) {
1910 kallsyms_filename
= symbol_conf
.kallsyms_name
;
1914 if (symbol_conf
.vmlinux_name
!= NULL
) {
1915 err
= dso__load_vmlinux(self
, map
,
1916 symbol_conf
.vmlinux_name
, filter
);
1918 dso__set_long_name(self
,
1919 strdup(symbol_conf
.vmlinux_name
));
1925 if (vmlinux_path
!= NULL
) {
1926 err
= dso__load_vmlinux_path(self
, map
, filter
);
1931 /* do not try local files if a symfs was given */
1932 if (symbol_conf
.symfs
[0] != 0)
1936 * Say the kernel DSO was created when processing the build-id header table,
1937 * we have a build-id, so check if it is the same as the running kernel,
1938 * using it if it is.
1940 if (self
->has_build_id
) {
1941 u8 kallsyms_build_id
[BUILD_ID_SIZE
];
1942 char sbuild_id
[BUILD_ID_SIZE
* 2 + 1];
1944 if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id
,
1945 sizeof(kallsyms_build_id
)) == 0) {
1946 if (dso__build_id_equal(self
, kallsyms_build_id
)) {
1947 kallsyms_filename
= "/proc/kallsyms";
1952 * Now look if we have it on the build-id cache in
1953 * $HOME/.debug/[kernel.kallsyms].
1955 build_id__sprintf(self
->build_id
, sizeof(self
->build_id
),
1958 if (asprintf(&kallsyms_allocated_filename
,
1959 "%s/.debug/[kernel.kallsyms]/%s",
1960 getenv("HOME"), sbuild_id
) == -1) {
1961 pr_err("Not enough memory for kallsyms file lookup\n");
1965 kallsyms_filename
= kallsyms_allocated_filename
;
1967 if (access(kallsyms_filename
, F_OK
)) {
1968 pr_err("No kallsyms or vmlinux with build-id %s "
1969 "was found\n", sbuild_id
);
1970 free(kallsyms_allocated_filename
);
1975 * Last resort, if we don't have a build-id and couldn't find
1976 * any vmlinux file, try the running kernel kallsyms table.
1978 kallsyms_filename
= "/proc/kallsyms";
1982 err
= dso__load_kallsyms(self
, kallsyms_filename
, map
, filter
);
1984 pr_debug("Using %s for symbols\n", kallsyms_filename
);
1985 free(kallsyms_allocated_filename
);
1989 if (kallsyms_filename
!= NULL
)
1990 dso__set_long_name(self
, strdup("[kernel.kallsyms]"));
1991 map__fixup_start(map
);
1992 map__fixup_end(map
);
1998 static int dso__load_guest_kernel_sym(struct dso
*self
, struct map
*map
,
1999 symbol_filter_t filter
)
2002 const char *kallsyms_filename
= NULL
;
2003 struct machine
*machine
;
2004 char path
[PATH_MAX
];
2007 pr_debug("Guest kernel map hasn't the point to groups\n");
2010 machine
= map
->groups
->machine
;
2012 if (machine__is_default_guest(machine
)) {
2014 * if the user specified a vmlinux filename, use it and only
2015 * it, reporting errors to the user if it cannot be used.
2016 * Or use file guest_kallsyms inputted by user on commandline
2018 if (symbol_conf
.default_guest_vmlinux_name
!= NULL
) {
2019 err
= dso__load_vmlinux(self
, map
,
2020 symbol_conf
.default_guest_vmlinux_name
, filter
);
2024 kallsyms_filename
= symbol_conf
.default_guest_kallsyms
;
2025 if (!kallsyms_filename
)
2028 sprintf(path
, "%s/proc/kallsyms", machine
->root_dir
);
2029 kallsyms_filename
= path
;
2032 err
= dso__load_kallsyms(self
, kallsyms_filename
, map
, filter
);
2034 pr_debug("Using %s for symbols\n", kallsyms_filename
);
2038 if (kallsyms_filename
!= NULL
) {
2039 machine__mmap_name(machine
, path
, sizeof(path
));
2040 dso__set_long_name(self
, strdup(path
));
2042 map__fixup_start(map
);
2043 map__fixup_end(map
);
2049 static void dsos__add(struct list_head
*head
, struct dso
*dso
)
2051 list_add_tail(&dso
->node
, head
);
2054 static struct dso
*dsos__find(struct list_head
*head
, const char *name
)
2058 list_for_each_entry(pos
, head
, node
)
2059 if (strcmp(pos
->long_name
, name
) == 0)
2064 struct dso
*__dsos__findnew(struct list_head
*head
, const char *name
)
2066 struct dso
*dso
= dsos__find(head
, name
);
2069 dso
= dso__new(name
);
2071 dsos__add(head
, dso
);
2072 dso__set_basename(dso
);
2079 size_t __dsos__fprintf(struct list_head
*head
, FILE *fp
)
2084 list_for_each_entry(pos
, head
, node
) {
2086 for (i
= 0; i
< MAP__NR_TYPES
; ++i
)
2087 ret
+= dso__fprintf(pos
, i
, fp
);
2093 size_t machines__fprintf_dsos(struct rb_root
*self
, FILE *fp
)
2098 for (nd
= rb_first(self
); nd
; nd
= rb_next(nd
)) {
2099 struct machine
*pos
= rb_entry(nd
, struct machine
, rb_node
);
2100 ret
+= __dsos__fprintf(&pos
->kernel_dsos
, fp
);
2101 ret
+= __dsos__fprintf(&pos
->user_dsos
, fp
);
2107 static size_t __dsos__fprintf_buildid(struct list_head
*head
, FILE *fp
,
2113 list_for_each_entry(pos
, head
, node
) {
2114 if (with_hits
&& !pos
->hit
)
2116 ret
+= dso__fprintf_buildid(pos
, fp
);
2117 ret
+= fprintf(fp
, " %s\n", pos
->long_name
);
2122 size_t machine__fprintf_dsos_buildid(struct machine
*self
, FILE *fp
, bool with_hits
)
2124 return __dsos__fprintf_buildid(&self
->kernel_dsos
, fp
, with_hits
) +
2125 __dsos__fprintf_buildid(&self
->user_dsos
, fp
, with_hits
);
2128 size_t machines__fprintf_dsos_buildid(struct rb_root
*self
, FILE *fp
, bool with_hits
)
2133 for (nd
= rb_first(self
); nd
; nd
= rb_next(nd
)) {
2134 struct machine
*pos
= rb_entry(nd
, struct machine
, rb_node
);
2135 ret
+= machine__fprintf_dsos_buildid(pos
, fp
, with_hits
);
2140 struct dso
*dso__new_kernel(const char *name
)
2142 struct dso
*self
= dso__new(name
?: "[kernel.kallsyms]");
2145 dso__set_short_name(self
, "[kernel]");
2146 self
->kernel
= DSO_TYPE_KERNEL
;
2152 static struct dso
*dso__new_guest_kernel(struct machine
*machine
,
2156 struct dso
*self
= dso__new(name
?: machine__mmap_name(machine
, bf
, sizeof(bf
)));
2159 dso__set_short_name(self
, "[guest.kernel]");
2160 self
->kernel
= DSO_TYPE_GUEST_KERNEL
;
2166 void dso__read_running_kernel_build_id(struct dso
*self
, struct machine
*machine
)
2168 char path
[PATH_MAX
];
2170 if (machine__is_default_guest(machine
))
2172 sprintf(path
, "%s/sys/kernel/notes", machine
->root_dir
);
2173 if (sysfs__read_build_id(path
, self
->build_id
,
2174 sizeof(self
->build_id
)) == 0)
2175 self
->has_build_id
= true;
2178 static struct dso
*machine__create_kernel(struct machine
*self
)
2180 const char *vmlinux_name
= NULL
;
2183 if (machine__is_host(self
)) {
2184 vmlinux_name
= symbol_conf
.vmlinux_name
;
2185 kernel
= dso__new_kernel(vmlinux_name
);
2187 if (machine__is_default_guest(self
))
2188 vmlinux_name
= symbol_conf
.default_guest_vmlinux_name
;
2189 kernel
= dso__new_guest_kernel(self
, vmlinux_name
);
2192 if (kernel
!= NULL
) {
2193 dso__read_running_kernel_build_id(kernel
, self
);
2194 dsos__add(&self
->kernel_dsos
, kernel
);
2199 struct process_args
{
2203 static int symbol__in_kernel(void *arg
, const char *name
,
2204 char type __used
, u64 start
, u64 end __used
)
2206 struct process_args
*args
= arg
;
2208 if (strchr(name
, '['))
2211 args
->start
= start
;
2215 /* Figure out the start address of kernel map from /proc/kallsyms */
2216 static u64
machine__get_kernel_start_addr(struct machine
*machine
)
2218 const char *filename
;
2219 char path
[PATH_MAX
];
2220 struct process_args args
;
2222 if (machine__is_host(machine
)) {
2223 filename
= "/proc/kallsyms";
2225 if (machine__is_default_guest(machine
))
2226 filename
= (char *)symbol_conf
.default_guest_kallsyms
;
2228 sprintf(path
, "%s/proc/kallsyms", machine
->root_dir
);
2233 if (kallsyms__parse(filename
, &args
, symbol__in_kernel
) <= 0)
2239 int __machine__create_kernel_maps(struct machine
*self
, struct dso
*kernel
)
2242 u64 start
= machine__get_kernel_start_addr(self
);
2244 for (type
= 0; type
< MAP__NR_TYPES
; ++type
) {
2247 self
->vmlinux_maps
[type
] = map__new2(start
, kernel
, type
);
2248 if (self
->vmlinux_maps
[type
] == NULL
)
2251 self
->vmlinux_maps
[type
]->map_ip
=
2252 self
->vmlinux_maps
[type
]->unmap_ip
= identity__map_ip
;
2254 kmap
= map__kmap(self
->vmlinux_maps
[type
]);
2255 kmap
->kmaps
= &self
->kmaps
;
2256 map_groups__insert(&self
->kmaps
, self
->vmlinux_maps
[type
]);
2262 void machine__destroy_kernel_maps(struct machine
*self
)
2266 for (type
= 0; type
< MAP__NR_TYPES
; ++type
) {
2269 if (self
->vmlinux_maps
[type
] == NULL
)
2272 kmap
= map__kmap(self
->vmlinux_maps
[type
]);
2273 map_groups__remove(&self
->kmaps
, self
->vmlinux_maps
[type
]);
2274 if (kmap
->ref_reloc_sym
) {
2276 * ref_reloc_sym is shared among all maps, so free just
2279 if (type
== MAP__FUNCTION
) {
2280 free((char *)kmap
->ref_reloc_sym
->name
);
2281 kmap
->ref_reloc_sym
->name
= NULL
;
2282 free(kmap
->ref_reloc_sym
);
2284 kmap
->ref_reloc_sym
= NULL
;
2287 map__delete(self
->vmlinux_maps
[type
]);
2288 self
->vmlinux_maps
[type
] = NULL
;
2292 int machine__create_kernel_maps(struct machine
*self
)
2294 struct dso
*kernel
= machine__create_kernel(self
);
2296 if (kernel
== NULL
||
2297 __machine__create_kernel_maps(self
, kernel
) < 0)
2300 if (symbol_conf
.use_modules
&& machine__create_modules(self
) < 0)
2301 pr_debug("Problems creating module maps, continuing anyway...\n");
2303 * Now that we have all the maps created, just set the ->end of them:
2305 map_groups__fixup_end(&self
->kmaps
);
2309 static void vmlinux_path__exit(void)
2311 while (--vmlinux_path__nr_entries
>= 0) {
2312 free(vmlinux_path
[vmlinux_path__nr_entries
]);
2313 vmlinux_path
[vmlinux_path__nr_entries
] = NULL
;
2317 vmlinux_path
= NULL
;
2320 static int vmlinux_path__init(void)
2325 vmlinux_path
= malloc(sizeof(char *) * 5);
2326 if (vmlinux_path
== NULL
)
2329 vmlinux_path
[vmlinux_path__nr_entries
] = strdup("vmlinux");
2330 if (vmlinux_path
[vmlinux_path__nr_entries
] == NULL
)
2332 ++vmlinux_path__nr_entries
;
2333 vmlinux_path
[vmlinux_path__nr_entries
] = strdup("/boot/vmlinux");
2334 if (vmlinux_path
[vmlinux_path__nr_entries
] == NULL
)
2336 ++vmlinux_path__nr_entries
;
2338 /* only try running kernel version if no symfs was given */
2339 if (symbol_conf
.symfs
[0] != 0)
2342 if (uname(&uts
) < 0)
2345 snprintf(bf
, sizeof(bf
), "/boot/vmlinux-%s", uts
.release
);
2346 vmlinux_path
[vmlinux_path__nr_entries
] = strdup(bf
);
2347 if (vmlinux_path
[vmlinux_path__nr_entries
] == NULL
)
2349 ++vmlinux_path__nr_entries
;
2350 snprintf(bf
, sizeof(bf
), "/lib/modules/%s/build/vmlinux", uts
.release
);
2351 vmlinux_path
[vmlinux_path__nr_entries
] = strdup(bf
);
2352 if (vmlinux_path
[vmlinux_path__nr_entries
] == NULL
)
2354 ++vmlinux_path__nr_entries
;
2355 snprintf(bf
, sizeof(bf
), "/usr/lib/debug/lib/modules/%s/vmlinux",
2357 vmlinux_path
[vmlinux_path__nr_entries
] = strdup(bf
);
2358 if (vmlinux_path
[vmlinux_path__nr_entries
] == NULL
)
2360 ++vmlinux_path__nr_entries
;
2365 vmlinux_path__exit();
2369 size_t machine__fprintf_vmlinux_path(struct machine
*self
, FILE *fp
)
2373 struct dso
*kdso
= self
->vmlinux_maps
[MAP__FUNCTION
]->dso
;
2375 if (kdso
->has_build_id
) {
2376 char filename
[PATH_MAX
];
2377 if (dso__build_id_filename(kdso
, filename
, sizeof(filename
)))
2378 printed
+= fprintf(fp
, "[0] %s\n", filename
);
2381 for (i
= 0; i
< vmlinux_path__nr_entries
; ++i
)
2382 printed
+= fprintf(fp
, "[%d] %s\n",
2383 i
+ kdso
->has_build_id
, vmlinux_path
[i
]);
2388 static int setup_list(struct strlist
**list
, const char *list_str
,
2389 const char *list_name
)
2391 if (list_str
== NULL
)
2394 *list
= strlist__new(true, list_str
);
2396 pr_err("problems parsing %s list\n", list_name
);
2402 int symbol__init(void)
2406 if (symbol_conf
.initialized
)
2409 symbol_conf
.priv_size
= ALIGN(symbol_conf
.priv_size
, sizeof(u64
));
2411 elf_version(EV_CURRENT
);
2412 if (symbol_conf
.sort_by_name
)
2413 symbol_conf
.priv_size
+= (sizeof(struct symbol_name_rb_node
) -
2414 sizeof(struct symbol
));
2416 if (symbol_conf
.try_vmlinux_path
&& vmlinux_path__init() < 0)
2419 if (symbol_conf
.field_sep
&& *symbol_conf
.field_sep
== '.') {
2420 pr_err("'.' is the only non valid --field-separator argument\n");
2424 if (setup_list(&symbol_conf
.dso_list
,
2425 symbol_conf
.dso_list_str
, "dso") < 0)
2428 if (setup_list(&symbol_conf
.comm_list
,
2429 symbol_conf
.comm_list_str
, "comm") < 0)
2430 goto out_free_dso_list
;
2432 if (setup_list(&symbol_conf
.sym_list
,
2433 symbol_conf
.sym_list_str
, "symbol") < 0)
2434 goto out_free_comm_list
;
2437 * A path to symbols of "/" is identical to ""
2438 * reset here for simplicity.
2440 symfs
= realpath(symbol_conf
.symfs
, NULL
);
2442 symfs
= symbol_conf
.symfs
;
2443 if (strcmp(symfs
, "/") == 0)
2444 symbol_conf
.symfs
= "";
2445 if (symfs
!= symbol_conf
.symfs
)
2446 free((void *)symfs
);
2448 symbol_conf
.initialized
= true;
2452 strlist__delete(symbol_conf
.dso_list
);
2454 strlist__delete(symbol_conf
.comm_list
);
2458 void symbol__exit(void)
2460 if (!symbol_conf
.initialized
)
2462 strlist__delete(symbol_conf
.sym_list
);
2463 strlist__delete(symbol_conf
.dso_list
);
2464 strlist__delete(symbol_conf
.comm_list
);
2465 vmlinux_path__exit();
2466 symbol_conf
.sym_list
= symbol_conf
.dso_list
= symbol_conf
.comm_list
= NULL
;
2467 symbol_conf
.initialized
= false;
2470 int machines__create_kernel_maps(struct rb_root
*self
, pid_t pid
)
2472 struct machine
*machine
= machines__findnew(self
, pid
);
2474 if (machine
== NULL
)
2477 return machine__create_kernel_maps(machine
);
2480 static int hex(char ch
)
2482 if ((ch
>= '0') && (ch
<= '9'))
2484 if ((ch
>= 'a') && (ch
<= 'f'))
2485 return ch
- 'a' + 10;
2486 if ((ch
>= 'A') && (ch
<= 'F'))
2487 return ch
- 'A' + 10;
2492 * While we find nice hex chars, build a long_val.
2493 * Return number of chars processed.
2495 int hex2u64(const char *ptr
, u64
*long_val
)
2497 const char *p
= ptr
;
2501 const int hex_val
= hex(*p
);
2506 *long_val
= (*long_val
<< 4) | hex_val
;
2513 char *strxfrchar(char *s
, char from
, char to
)
2517 while ((p
= strchr(p
, from
)) != NULL
)
2523 int machines__create_guest_kernel_maps(struct rb_root
*self
)
2526 struct dirent
**namelist
= NULL
;
2528 char path
[PATH_MAX
];
2531 if (symbol_conf
.default_guest_vmlinux_name
||
2532 symbol_conf
.default_guest_modules
||
2533 symbol_conf
.default_guest_kallsyms
) {
2534 machines__create_kernel_maps(self
, DEFAULT_GUEST_KERNEL_ID
);
2537 if (symbol_conf
.guestmount
) {
2538 items
= scandir(symbol_conf
.guestmount
, &namelist
, NULL
, NULL
);
2541 for (i
= 0; i
< items
; i
++) {
2542 if (!isdigit(namelist
[i
]->d_name
[0])) {
2543 /* Filter out . and .. */
2546 pid
= atoi(namelist
[i
]->d_name
);
2547 sprintf(path
, "%s/%s/proc/kallsyms",
2548 symbol_conf
.guestmount
,
2549 namelist
[i
]->d_name
);
2550 ret
= access(path
, R_OK
);
2552 pr_debug("Can't access file %s\n", path
);
2555 machines__create_kernel_maps(self
, pid
);
2564 void machines__destroy_guest_kernel_maps(struct rb_root
*self
)
2566 struct rb_node
*next
= rb_first(self
);
2569 struct machine
*pos
= rb_entry(next
, struct machine
, rb_node
);
2571 next
= rb_next(&pos
->rb_node
);
2572 rb_erase(&pos
->rb_node
, self
);
2573 machine__delete(pos
);
2577 int machine__load_kallsyms(struct machine
*self
, const char *filename
,
2578 enum map_type type
, symbol_filter_t filter
)
2580 struct map
*map
= self
->vmlinux_maps
[type
];
2581 int ret
= dso__load_kallsyms(map
->dso
, filename
, map
, filter
);
2584 dso__set_loaded(map
->dso
, type
);
2586 * Since /proc/kallsyms will have multiple sessions for the
2587 * kernel, with modules between them, fixup the end of all
2590 __map_groups__fixup_end(&self
->kmaps
, type
);
2596 int machine__load_vmlinux_path(struct machine
*self
, enum map_type type
,
2597 symbol_filter_t filter
)
2599 struct map
*map
= self
->vmlinux_maps
[type
];
2600 int ret
= dso__load_vmlinux_path(map
->dso
, map
, filter
);
2603 dso__set_loaded(map
->dso
, type
);
2604 map__reloc_vmlinux(map
);