21 #include <symbol/kallsyms.h>
22 #include <sys/utsname.h>
24 static int dso__load_kernel_sym(struct dso
*dso
, struct map
*map
,
25 symbol_filter_t filter
);
26 static int dso__load_guest_kernel_sym(struct dso
*dso
, struct map
*map
,
27 symbol_filter_t filter
);
28 int vmlinux_path__nr_entries
;
31 struct symbol_conf symbol_conf
= {
33 .try_vmlinux_path
= true,
36 .cumulate_callchain
= true,
40 static enum dso_binary_type binary_type_symtab
[] = {
41 DSO_BINARY_TYPE__KALLSYMS
,
42 DSO_BINARY_TYPE__GUEST_KALLSYMS
,
43 DSO_BINARY_TYPE__JAVA_JIT
,
44 DSO_BINARY_TYPE__DEBUGLINK
,
45 DSO_BINARY_TYPE__BUILD_ID_CACHE
,
46 DSO_BINARY_TYPE__FEDORA_DEBUGINFO
,
47 DSO_BINARY_TYPE__UBUNTU_DEBUGINFO
,
48 DSO_BINARY_TYPE__BUILDID_DEBUGINFO
,
49 DSO_BINARY_TYPE__SYSTEM_PATH_DSO
,
50 DSO_BINARY_TYPE__GUEST_KMODULE
,
51 DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE
,
52 DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO
,
53 DSO_BINARY_TYPE__NOT_FOUND
,
56 #define DSO_BINARY_TYPE__SYMTAB_CNT ARRAY_SIZE(binary_type_symtab)
58 bool symbol_type__is_a(char symbol_type
, enum map_type map_type
)
60 symbol_type
= toupper(symbol_type
);
64 return symbol_type
== 'T' || symbol_type
== 'W';
66 return symbol_type
== 'D';
72 static int prefix_underscores_count(const char *str
)
74 const char *tail
= str
;
85 static int choose_best_symbol(struct symbol
*syma
, struct symbol
*symb
)
91 /* Prefer a symbol with non zero length */
92 a
= syma
->end
- syma
->start
;
93 b
= symb
->end
- symb
->start
;
94 if ((b
== 0) && (a
> 0))
96 else if ((a
== 0) && (b
> 0))
99 /* Prefer a non weak symbol over a weak one */
100 a
= syma
->binding
== STB_WEAK
;
101 b
= symb
->binding
== STB_WEAK
;
107 /* Prefer a global symbol over a non global one */
108 a
= syma
->binding
== STB_GLOBAL
;
109 b
= symb
->binding
== STB_GLOBAL
;
115 /* Prefer a symbol with less underscores */
116 a
= prefix_underscores_count(syma
->name
);
117 b
= prefix_underscores_count(symb
->name
);
123 /* Choose the symbol with the longest name */
124 na
= strlen(syma
->name
);
125 nb
= strlen(symb
->name
);
131 /* Avoid "SyS" kernel syscall aliases */
132 if (na
>= 3 && !strncmp(syma
->name
, "SyS", 3))
134 if (na
>= 10 && !strncmp(syma
->name
, "compat_SyS", 10))
140 void symbols__fixup_duplicate(struct rb_root
*symbols
)
143 struct symbol
*curr
, *next
;
145 nd
= rb_first(symbols
);
148 curr
= rb_entry(nd
, struct symbol
, rb_node
);
150 nd
= rb_next(&curr
->rb_node
);
151 next
= rb_entry(nd
, struct symbol
, rb_node
);
156 if (curr
->start
!= next
->start
)
159 if (choose_best_symbol(curr
, next
) == SYMBOL_A
) {
160 rb_erase(&next
->rb_node
, symbols
);
161 symbol__delete(next
);
164 nd
= rb_next(&curr
->rb_node
);
165 rb_erase(&curr
->rb_node
, symbols
);
166 symbol__delete(curr
);
171 void symbols__fixup_end(struct rb_root
*symbols
)
173 struct rb_node
*nd
, *prevnd
= rb_first(symbols
);
174 struct symbol
*curr
, *prev
;
179 curr
= rb_entry(prevnd
, struct symbol
, rb_node
);
181 for (nd
= rb_next(prevnd
); nd
; nd
= rb_next(nd
)) {
183 curr
= rb_entry(nd
, struct symbol
, rb_node
);
185 if (prev
->end
== prev
->start
&& prev
->end
!= curr
->start
)
186 prev
->end
= curr
->start
- 1;
190 if (curr
->end
== curr
->start
)
191 curr
->end
= roundup(curr
->start
, 4096);
194 void __map_groups__fixup_end(struct map_groups
*mg
, enum map_type type
)
196 struct map
*prev
, *curr
;
197 struct rb_node
*nd
, *prevnd
= rb_first(&mg
->maps
[type
]);
202 curr
= rb_entry(prevnd
, struct map
, rb_node
);
204 for (nd
= rb_next(prevnd
); nd
; nd
= rb_next(nd
)) {
206 curr
= rb_entry(nd
, struct map
, rb_node
);
207 prev
->end
= curr
->start
- 1;
211 * We still haven't the actual symbols, so guess the
212 * last map final address.
217 struct symbol
*symbol__new(u64 start
, u64 len
, u8 binding
, const char *name
)
219 size_t namelen
= strlen(name
) + 1;
220 struct symbol
*sym
= calloc(1, (symbol_conf
.priv_size
+
221 sizeof(*sym
) + namelen
));
225 if (symbol_conf
.priv_size
)
226 sym
= ((void *)sym
) + symbol_conf
.priv_size
;
229 sym
->end
= len
? start
+ len
- 1 : start
;
230 sym
->binding
= binding
;
231 sym
->namelen
= namelen
- 1;
233 pr_debug4("%s: %s %#" PRIx64
"-%#" PRIx64
"\n",
234 __func__
, name
, start
, sym
->end
);
235 memcpy(sym
->name
, name
, namelen
);
240 void symbol__delete(struct symbol
*sym
)
242 free(((void *)sym
) - symbol_conf
.priv_size
);
245 size_t symbol__fprintf(struct symbol
*sym
, FILE *fp
)
247 return fprintf(fp
, " %" PRIx64
"-%" PRIx64
" %c %s\n",
248 sym
->start
, sym
->end
,
249 sym
->binding
== STB_GLOBAL
? 'g' :
250 sym
->binding
== STB_LOCAL
? 'l' : 'w',
254 size_t symbol__fprintf_symname_offs(const struct symbol
*sym
,
255 const struct addr_location
*al
, FILE *fp
)
257 unsigned long offset
;
260 if (sym
&& sym
->name
) {
261 length
= fprintf(fp
, "%s", sym
->name
);
263 if (al
->addr
< sym
->end
)
264 offset
= al
->addr
- sym
->start
;
266 offset
= al
->addr
- al
->map
->start
- sym
->start
;
267 length
+= fprintf(fp
, "+0x%lx", offset
);
271 return fprintf(fp
, "[unknown]");
274 size_t symbol__fprintf_symname(const struct symbol
*sym
, FILE *fp
)
276 return symbol__fprintf_symname_offs(sym
, NULL
, fp
);
279 void symbols__delete(struct rb_root
*symbols
)
282 struct rb_node
*next
= rb_first(symbols
);
285 pos
= rb_entry(next
, struct symbol
, rb_node
);
286 next
= rb_next(&pos
->rb_node
);
287 rb_erase(&pos
->rb_node
, symbols
);
292 void symbols__insert(struct rb_root
*symbols
, struct symbol
*sym
)
294 struct rb_node
**p
= &symbols
->rb_node
;
295 struct rb_node
*parent
= NULL
;
296 const u64 ip
= sym
->start
;
301 s
= rb_entry(parent
, struct symbol
, rb_node
);
307 rb_link_node(&sym
->rb_node
, parent
, p
);
308 rb_insert_color(&sym
->rb_node
, symbols
);
311 static struct symbol
*symbols__find(struct rb_root
*symbols
, u64 ip
)
318 n
= symbols
->rb_node
;
321 struct symbol
*s
= rb_entry(n
, struct symbol
, rb_node
);
325 else if (ip
> s
->end
)
334 static struct symbol
*symbols__first(struct rb_root
*symbols
)
336 struct rb_node
*n
= rb_first(symbols
);
339 return rb_entry(n
, struct symbol
, rb_node
);
344 struct symbol_name_rb_node
{
345 struct rb_node rb_node
;
349 static void symbols__insert_by_name(struct rb_root
*symbols
, struct symbol
*sym
)
351 struct rb_node
**p
= &symbols
->rb_node
;
352 struct rb_node
*parent
= NULL
;
353 struct symbol_name_rb_node
*symn
, *s
;
355 symn
= container_of(sym
, struct symbol_name_rb_node
, sym
);
359 s
= rb_entry(parent
, struct symbol_name_rb_node
, rb_node
);
360 if (strcmp(sym
->name
, s
->sym
.name
) < 0)
365 rb_link_node(&symn
->rb_node
, parent
, p
);
366 rb_insert_color(&symn
->rb_node
, symbols
);
369 static void symbols__sort_by_name(struct rb_root
*symbols
,
370 struct rb_root
*source
)
374 for (nd
= rb_first(source
); nd
; nd
= rb_next(nd
)) {
375 struct symbol
*pos
= rb_entry(nd
, struct symbol
, rb_node
);
376 symbols__insert_by_name(symbols
, pos
);
380 static struct symbol
*symbols__find_by_name(struct rb_root
*symbols
,
388 n
= symbols
->rb_node
;
391 struct symbol_name_rb_node
*s
;
394 s
= rb_entry(n
, struct symbol_name_rb_node
, rb_node
);
395 cmp
= strcmp(name
, s
->sym
.name
);
408 struct symbol
*dso__find_symbol(struct dso
*dso
,
409 enum map_type type
, u64 addr
)
411 return symbols__find(&dso
->symbols
[type
], addr
);
414 static struct symbol
*dso__first_symbol(struct dso
*dso
, enum map_type type
)
416 return symbols__first(&dso
->symbols
[type
]);
419 struct symbol
*dso__find_symbol_by_name(struct dso
*dso
, enum map_type type
,
422 return symbols__find_by_name(&dso
->symbol_names
[type
], name
);
425 void dso__sort_by_name(struct dso
*dso
, enum map_type type
)
427 dso__set_sorted_by_name(dso
, type
);
428 return symbols__sort_by_name(&dso
->symbol_names
[type
],
429 &dso
->symbols
[type
]);
432 size_t dso__fprintf_symbols_by_name(struct dso
*dso
,
433 enum map_type type
, FILE *fp
)
437 struct symbol_name_rb_node
*pos
;
439 for (nd
= rb_first(&dso
->symbol_names
[type
]); nd
; nd
= rb_next(nd
)) {
440 pos
= rb_entry(nd
, struct symbol_name_rb_node
, rb_node
);
441 fprintf(fp
, "%s\n", pos
->sym
.name
);
447 int modules__parse(const char *filename
, void *arg
,
448 int (*process_module
)(void *arg
, const char *name
,
456 file
= fopen(filename
, "r");
466 line_len
= getline(&line
, &n
, file
);
479 line
[--line_len
] = '\0'; /* \n */
481 sep
= strrchr(line
, 'x');
485 hex2u64(sep
+ 1, &start
);
487 sep
= strchr(line
, ' ');
493 scnprintf(name
, sizeof(name
), "[%s]", line
);
495 err
= process_module(arg
, name
, start
);
505 struct process_kallsyms_args
{
510 bool symbol__is_idle(struct symbol
*sym
)
512 const char * const idle_symbols
[] = {
520 "mwait_idle_with_hints",
522 "ppc64_runlatch_off",
523 "pseries_dedicated_idle_sleep",
532 for (i
= 0; idle_symbols
[i
]; i
++) {
533 if (!strcmp(idle_symbols
[i
], sym
->name
))
540 static int map__process_kallsym_symbol(void *arg
, const char *name
,
541 char type
, u64 start
)
544 struct process_kallsyms_args
*a
= arg
;
545 struct rb_root
*root
= &a
->dso
->symbols
[a
->map
->type
];
547 if (!symbol_type__is_a(type
, a
->map
->type
))
551 * module symbols are not sorted so we add all
552 * symbols, setting length to 0, and rely on
553 * symbols__fixup_end() to fix it up.
555 sym
= symbol__new(start
, 0, kallsyms2elf_type(type
), name
);
559 * We will pass the symbols to the filter later, in
560 * map__split_kallsyms, when we have split the maps per module
562 symbols__insert(root
, sym
);
568 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
569 * so that we can in the next step set the symbol ->end address and then
570 * call kernel_maps__split_kallsyms.
572 static int dso__load_all_kallsyms(struct dso
*dso
, const char *filename
,
575 struct process_kallsyms_args args
= { .map
= map
, .dso
= dso
, };
576 return kallsyms__parse(filename
, &args
, map__process_kallsym_symbol
);
579 static int dso__split_kallsyms_for_kcore(struct dso
*dso
, struct map
*map
,
580 symbol_filter_t filter
)
582 struct map_groups
*kmaps
= map__kmap(map
)->kmaps
;
583 struct map
*curr_map
;
585 int count
= 0, moved
= 0;
586 struct rb_root
*root
= &dso
->symbols
[map
->type
];
587 struct rb_node
*next
= rb_first(root
);
592 pos
= rb_entry(next
, struct symbol
, rb_node
);
593 next
= rb_next(&pos
->rb_node
);
595 module
= strchr(pos
->name
, '\t');
599 curr_map
= map_groups__find(kmaps
, map
->type
, pos
->start
);
601 if (!curr_map
|| (filter
&& filter(curr_map
, pos
))) {
602 rb_erase(&pos
->rb_node
, root
);
605 pos
->start
-= curr_map
->start
- curr_map
->pgoff
;
607 pos
->end
-= curr_map
->start
- curr_map
->pgoff
;
608 if (curr_map
!= map
) {
609 rb_erase(&pos
->rb_node
, root
);
611 &curr_map
->dso
->symbols
[curr_map
->type
],
620 /* Symbols have been adjusted */
621 dso
->adjust_symbols
= 1;
623 return count
+ moved
;
627 * Split the symbols into maps, making sure there are no overlaps, i.e. the
628 * kernel range is broken in several maps, named [kernel].N, as we don't have
629 * the original ELF section names vmlinux have.
631 static int dso__split_kallsyms(struct dso
*dso
, struct map
*map
, u64 delta
,
632 symbol_filter_t filter
)
634 struct map_groups
*kmaps
= map__kmap(map
)->kmaps
;
635 struct machine
*machine
= kmaps
->machine
;
636 struct map
*curr_map
= map
;
638 int count
= 0, moved
= 0;
639 struct rb_root
*root
= &dso
->symbols
[map
->type
];
640 struct rb_node
*next
= rb_first(root
);
641 int kernel_range
= 0;
646 pos
= rb_entry(next
, struct symbol
, rb_node
);
647 next
= rb_next(&pos
->rb_node
);
649 module
= strchr(pos
->name
, '\t');
651 if (!symbol_conf
.use_modules
)
656 if (strcmp(curr_map
->dso
->short_name
, module
)) {
657 if (curr_map
!= map
&&
658 dso
->kernel
== DSO_TYPE_GUEST_KERNEL
&&
659 machine__is_default_guest(machine
)) {
661 * We assume all symbols of a module are
662 * continuous in * kallsyms, so curr_map
663 * points to a module and all its
664 * symbols are in its kmap. Mark it as
667 dso__set_loaded(curr_map
->dso
,
671 curr_map
= map_groups__find_by_name(kmaps
,
673 if (curr_map
== NULL
) {
674 pr_debug("%s/proc/{kallsyms,modules} "
675 "inconsistency while looking "
676 "for \"%s\" module!\n",
677 machine
->root_dir
, module
);
682 if (curr_map
->dso
->loaded
&&
683 !machine__is_default_guest(machine
))
687 * So that we look just like we get from .ko files,
688 * i.e. not prelinked, relative to map->start.
690 pos
->start
= curr_map
->map_ip(curr_map
, pos
->start
);
691 pos
->end
= curr_map
->map_ip(curr_map
, pos
->end
);
692 } else if (curr_map
!= map
) {
693 char dso_name
[PATH_MAX
];
697 /* Kernel was relocated at boot time */
707 if (dso
->kernel
== DSO_TYPE_GUEST_KERNEL
)
708 snprintf(dso_name
, sizeof(dso_name
),
712 snprintf(dso_name
, sizeof(dso_name
),
716 ndso
= dso__new(dso_name
);
720 ndso
->kernel
= dso
->kernel
;
722 curr_map
= map__new2(pos
->start
, ndso
, map
->type
);
723 if (curr_map
== NULL
) {
728 curr_map
->map_ip
= curr_map
->unmap_ip
= identity__map_ip
;
729 map_groups__insert(kmaps
, curr_map
);
732 /* Kernel was relocated at boot time */
737 if (filter
&& filter(curr_map
, pos
)) {
738 discard_symbol
: rb_erase(&pos
->rb_node
, root
);
741 if (curr_map
!= map
) {
742 rb_erase(&pos
->rb_node
, root
);
743 symbols__insert(&curr_map
->dso
->symbols
[curr_map
->type
], pos
);
750 if (curr_map
!= map
&&
751 dso
->kernel
== DSO_TYPE_GUEST_KERNEL
&&
752 machine__is_default_guest(kmaps
->machine
)) {
753 dso__set_loaded(curr_map
->dso
, curr_map
->type
);
756 return count
+ moved
;
759 bool symbol__restricted_filename(const char *filename
,
760 const char *restricted_filename
)
762 bool restricted
= false;
764 if (symbol_conf
.kptr_restrict
) {
765 char *r
= realpath(filename
, NULL
);
768 restricted
= strcmp(r
, restricted_filename
) == 0;
778 struct rb_node rb_node
;
783 static void add_module(struct module_info
*mi
, struct rb_root
*modules
)
785 struct rb_node
**p
= &modules
->rb_node
;
786 struct rb_node
*parent
= NULL
;
787 struct module_info
*m
;
791 m
= rb_entry(parent
, struct module_info
, rb_node
);
792 if (strcmp(mi
->name
, m
->name
) < 0)
797 rb_link_node(&mi
->rb_node
, parent
, p
);
798 rb_insert_color(&mi
->rb_node
, modules
);
801 static void delete_modules(struct rb_root
*modules
)
803 struct module_info
*mi
;
804 struct rb_node
*next
= rb_first(modules
);
807 mi
= rb_entry(next
, struct module_info
, rb_node
);
808 next
= rb_next(&mi
->rb_node
);
809 rb_erase(&mi
->rb_node
, modules
);
815 static struct module_info
*find_module(const char *name
,
816 struct rb_root
*modules
)
818 struct rb_node
*n
= modules
->rb_node
;
821 struct module_info
*m
;
824 m
= rb_entry(n
, struct module_info
, rb_node
);
825 cmp
= strcmp(name
, m
->name
);
837 static int __read_proc_modules(void *arg
, const char *name
, u64 start
)
839 struct rb_root
*modules
= arg
;
840 struct module_info
*mi
;
842 mi
= zalloc(sizeof(struct module_info
));
846 mi
->name
= strdup(name
);
854 add_module(mi
, modules
);
859 static int read_proc_modules(const char *filename
, struct rb_root
*modules
)
861 if (symbol__restricted_filename(filename
, "/proc/modules"))
864 if (modules__parse(filename
, modules
, __read_proc_modules
)) {
865 delete_modules(modules
);
872 int compare_proc_modules(const char *from
, const char *to
)
874 struct rb_root from_modules
= RB_ROOT
;
875 struct rb_root to_modules
= RB_ROOT
;
876 struct rb_node
*from_node
, *to_node
;
877 struct module_info
*from_m
, *to_m
;
880 if (read_proc_modules(from
, &from_modules
))
883 if (read_proc_modules(to
, &to_modules
))
884 goto out_delete_from
;
886 from_node
= rb_first(&from_modules
);
887 to_node
= rb_first(&to_modules
);
892 from_m
= rb_entry(from_node
, struct module_info
, rb_node
);
893 to_m
= rb_entry(to_node
, struct module_info
, rb_node
);
895 if (from_m
->start
!= to_m
->start
||
896 strcmp(from_m
->name
, to_m
->name
))
899 from_node
= rb_next(from_node
);
900 to_node
= rb_next(to_node
);
903 if (!from_node
&& !to_node
)
906 delete_modules(&to_modules
);
908 delete_modules(&from_modules
);
913 static int do_validate_kcore_modules(const char *filename
, struct map
*map
,
914 struct map_groups
*kmaps
)
916 struct rb_root modules
= RB_ROOT
;
920 err
= read_proc_modules(filename
, &modules
);
924 old_map
= map_groups__first(kmaps
, map
->type
);
926 struct map
*next
= map_groups__next(old_map
);
927 struct module_info
*mi
;
929 if (old_map
== map
|| old_map
->start
== map
->start
) {
935 /* Module must be in memory at the same address */
936 mi
= find_module(old_map
->dso
->short_name
, &modules
);
937 if (!mi
|| mi
->start
!= old_map
->start
) {
945 delete_modules(&modules
);
950 * If kallsyms is referenced by name then we look for filename in the same
953 static bool filename_from_kallsyms_filename(char *filename
,
954 const char *base_name
,
955 const char *kallsyms_filename
)
959 strcpy(filename
, kallsyms_filename
);
960 name
= strrchr(filename
, '/');
966 if (!strcmp(name
, "kallsyms")) {
967 strcpy(name
, base_name
);
974 static int validate_kcore_modules(const char *kallsyms_filename
,
977 struct map_groups
*kmaps
= map__kmap(map
)->kmaps
;
978 char modules_filename
[PATH_MAX
];
980 if (!filename_from_kallsyms_filename(modules_filename
, "modules",
984 if (do_validate_kcore_modules(modules_filename
, map
, kmaps
))
990 static int validate_kcore_addresses(const char *kallsyms_filename
,
993 struct kmap
*kmap
= map__kmap(map
);
995 if (kmap
->ref_reloc_sym
&& kmap
->ref_reloc_sym
->name
) {
998 start
= kallsyms__get_function_start(kallsyms_filename
,
999 kmap
->ref_reloc_sym
->name
);
1000 if (start
!= kmap
->ref_reloc_sym
->addr
)
1004 return validate_kcore_modules(kallsyms_filename
, map
);
1007 struct kcore_mapfn_data
{
1010 struct list_head maps
;
1013 static int kcore_mapfn(u64 start
, u64 len
, u64 pgoff
, void *data
)
1015 struct kcore_mapfn_data
*md
= data
;
1018 map
= map__new2(start
, md
->dso
, md
->type
);
1022 map
->end
= map
->start
+ len
;
1025 list_add(&map
->node
, &md
->maps
);
1030 static int dso__load_kcore(struct dso
*dso
, struct map
*map
,
1031 const char *kallsyms_filename
)
1033 struct map_groups
*kmaps
= map__kmap(map
)->kmaps
;
1034 struct machine
*machine
= kmaps
->machine
;
1035 struct kcore_mapfn_data md
;
1036 struct map
*old_map
, *new_map
, *replacement_map
= NULL
;
1039 char kcore_filename
[PATH_MAX
];
1042 /* This function requires that the map is the kernel map */
1043 if (map
!= machine
->vmlinux_maps
[map
->type
])
1046 if (!filename_from_kallsyms_filename(kcore_filename
, "kcore",
1050 /* Modules and kernel must be present at their original addresses */
1051 if (validate_kcore_addresses(kallsyms_filename
, map
))
1055 md
.type
= map
->type
;
1056 INIT_LIST_HEAD(&md
.maps
);
1058 fd
= open(kcore_filename
, O_RDONLY
);
1062 /* Read new maps into temporary lists */
1063 err
= file__read_maps(fd
, md
.type
== MAP__FUNCTION
, kcore_mapfn
, &md
,
1068 if (list_empty(&md
.maps
)) {
1073 /* Remove old maps */
1074 old_map
= map_groups__first(kmaps
, map
->type
);
1076 struct map
*next
= map_groups__next(old_map
);
1079 map_groups__remove(kmaps
, old_map
);
1083 /* Find the kernel map using the first symbol */
1084 sym
= dso__first_symbol(dso
, map
->type
);
1085 list_for_each_entry(new_map
, &md
.maps
, node
) {
1086 if (sym
&& sym
->start
>= new_map
->start
&&
1087 sym
->start
< new_map
->end
) {
1088 replacement_map
= new_map
;
1093 if (!replacement_map
)
1094 replacement_map
= list_entry(md
.maps
.next
, struct map
, node
);
1097 while (!list_empty(&md
.maps
)) {
1098 new_map
= list_entry(md
.maps
.next
, struct map
, node
);
1099 list_del(&new_map
->node
);
1100 if (new_map
== replacement_map
) {
1101 map
->start
= new_map
->start
;
1102 map
->end
= new_map
->end
;
1103 map
->pgoff
= new_map
->pgoff
;
1104 map
->map_ip
= new_map
->map_ip
;
1105 map
->unmap_ip
= new_map
->unmap_ip
;
1106 map__delete(new_map
);
1107 /* Ensure maps are correctly ordered */
1108 map_groups__remove(kmaps
, map
);
1109 map_groups__insert(kmaps
, map
);
1111 map_groups__insert(kmaps
, new_map
);
1116 * Set the data type and long name so that kcore can be read via
1117 * dso__data_read_addr().
1119 if (dso
->kernel
== DSO_TYPE_GUEST_KERNEL
)
1120 dso
->binary_type
= DSO_BINARY_TYPE__GUEST_KCORE
;
1122 dso
->binary_type
= DSO_BINARY_TYPE__KCORE
;
1123 dso__set_long_name(dso
, strdup(kcore_filename
), true);
1127 if (map
->type
== MAP__FUNCTION
)
1128 pr_debug("Using %s for kernel object code\n", kcore_filename
);
1130 pr_debug("Using %s for kernel data\n", kcore_filename
);
1135 while (!list_empty(&md
.maps
)) {
1136 map
= list_entry(md
.maps
.next
, struct map
, node
);
1137 list_del(&map
->node
);
1145 * If the kernel is relocated at boot time, kallsyms won't match. Compute the
1146 * delta based on the relocation reference symbol.
1148 static int kallsyms__delta(struct map
*map
, const char *filename
, u64
*delta
)
1150 struct kmap
*kmap
= map__kmap(map
);
1153 if (!kmap
->ref_reloc_sym
|| !kmap
->ref_reloc_sym
->name
)
1156 addr
= kallsyms__get_function_start(filename
,
1157 kmap
->ref_reloc_sym
->name
);
1161 *delta
= addr
- kmap
->ref_reloc_sym
->addr
;
1165 int dso__load_kallsyms(struct dso
*dso
, const char *filename
,
1166 struct map
*map
, symbol_filter_t filter
)
1170 if (symbol__restricted_filename(filename
, "/proc/kallsyms"))
1173 if (dso__load_all_kallsyms(dso
, filename
, map
) < 0)
1176 if (kallsyms__delta(map
, filename
, &delta
))
1179 symbols__fixup_duplicate(&dso
->symbols
[map
->type
]);
1180 symbols__fixup_end(&dso
->symbols
[map
->type
]);
1182 if (dso
->kernel
== DSO_TYPE_GUEST_KERNEL
)
1183 dso
->symtab_type
= DSO_BINARY_TYPE__GUEST_KALLSYMS
;
1185 dso
->symtab_type
= DSO_BINARY_TYPE__KALLSYMS
;
1187 if (!dso__load_kcore(dso
, map
, filename
))
1188 return dso__split_kallsyms_for_kcore(dso
, map
, filter
);
1190 return dso__split_kallsyms(dso
, map
, delta
, filter
);
1193 static int dso__load_perf_map(struct dso
*dso
, struct map
*map
,
1194 symbol_filter_t filter
)
1201 file
= fopen(dso
->long_name
, "r");
1205 while (!feof(file
)) {
1210 line_len
= getline(&line
, &n
, file
);
1217 line
[--line_len
] = '\0'; /* \n */
1219 len
= hex2u64(line
, &start
);
1222 if (len
+ 2 >= line_len
)
1225 len
+= hex2u64(line
+ len
, &size
);
1228 if (len
+ 2 >= line_len
)
1231 sym
= symbol__new(start
, size
, STB_GLOBAL
, line
+ len
);
1234 goto out_delete_line
;
1236 if (filter
&& filter(map
, sym
))
1237 symbol__delete(sym
);
1239 symbols__insert(&dso
->symbols
[map
->type
], sym
);
1255 static bool dso__is_compatible_symtab_type(struct dso
*dso
, bool kmod
,
1256 enum dso_binary_type type
)
1259 case DSO_BINARY_TYPE__JAVA_JIT
:
1260 case DSO_BINARY_TYPE__DEBUGLINK
:
1261 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO
:
1262 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO
:
1263 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO
:
1264 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO
:
1265 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO
:
1266 return !kmod
&& dso
->kernel
== DSO_TYPE_USER
;
1268 case DSO_BINARY_TYPE__KALLSYMS
:
1269 case DSO_BINARY_TYPE__VMLINUX
:
1270 case DSO_BINARY_TYPE__KCORE
:
1271 return dso
->kernel
== DSO_TYPE_KERNEL
;
1273 case DSO_BINARY_TYPE__GUEST_KALLSYMS
:
1274 case DSO_BINARY_TYPE__GUEST_VMLINUX
:
1275 case DSO_BINARY_TYPE__GUEST_KCORE
:
1276 return dso
->kernel
== DSO_TYPE_GUEST_KERNEL
;
1278 case DSO_BINARY_TYPE__GUEST_KMODULE
:
1279 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE
:
1281 * kernel modules know their symtab type - it's set when
1282 * creating a module dso in machine__new_module().
1284 return kmod
&& dso
->symtab_type
== type
;
1286 case DSO_BINARY_TYPE__BUILD_ID_CACHE
:
1289 case DSO_BINARY_TYPE__NOT_FOUND
:
1295 int dso__load(struct dso
*dso
, struct map
*map
, symbol_filter_t filter
)
1300 struct machine
*machine
;
1301 char *root_dir
= (char *) "";
1303 struct symsrc ss_
[2];
1304 struct symsrc
*syms_ss
= NULL
, *runtime_ss
= NULL
;
1307 dso__set_loaded(dso
, map
->type
);
1309 if (dso
->kernel
== DSO_TYPE_KERNEL
)
1310 return dso__load_kernel_sym(dso
, map
, filter
);
1311 else if (dso
->kernel
== DSO_TYPE_GUEST_KERNEL
)
1312 return dso__load_guest_kernel_sym(dso
, map
, filter
);
1314 if (map
->groups
&& map
->groups
->machine
)
1315 machine
= map
->groups
->machine
;
1319 dso
->adjust_symbols
= 0;
1321 if (strncmp(dso
->name
, "/tmp/perf-", 10) == 0) {
1324 if (lstat(dso
->name
, &st
) < 0)
1327 if (st
.st_uid
&& (st
.st_uid
!= geteuid())) {
1328 pr_warning("File %s not owned by current user or root, "
1329 "ignoring it.\n", dso
->name
);
1333 ret
= dso__load_perf_map(dso
, map
, filter
);
1334 dso
->symtab_type
= ret
> 0 ? DSO_BINARY_TYPE__JAVA_JIT
:
1335 DSO_BINARY_TYPE__NOT_FOUND
;
1340 root_dir
= machine
->root_dir
;
1342 name
= malloc(PATH_MAX
);
1346 kmod
= dso
->symtab_type
== DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE
||
1347 dso
->symtab_type
== DSO_BINARY_TYPE__GUEST_KMODULE
;
1350 * Iterate over candidate debug images.
1351 * Keep track of "interesting" ones (those which have a symtab, dynsym,
1352 * and/or opd section) for processing.
1354 for (i
= 0; i
< DSO_BINARY_TYPE__SYMTAB_CNT
; i
++) {
1355 struct symsrc
*ss
= &ss_
[ss_pos
];
1356 bool next_slot
= false;
1358 enum dso_binary_type symtab_type
= binary_type_symtab
[i
];
1360 if (!dso__is_compatible_symtab_type(dso
, kmod
, symtab_type
))
1363 if (dso__read_binary_type_filename(dso
, symtab_type
,
1364 root_dir
, name
, PATH_MAX
))
1367 /* Name is now the name of the next image to try */
1368 if (symsrc__init(ss
, dso
, name
, symtab_type
) < 0)
1371 if (!syms_ss
&& symsrc__has_symtab(ss
)) {
1374 if (!dso
->symsrc_filename
)
1375 dso
->symsrc_filename
= strdup(name
);
1378 if (!runtime_ss
&& symsrc__possibly_runtime(ss
)) {
1386 if (syms_ss
&& runtime_ss
)
1389 symsrc__destroy(ss
);
1394 if (!runtime_ss
&& !syms_ss
)
1397 if (runtime_ss
&& !syms_ss
) {
1398 syms_ss
= runtime_ss
;
1401 /* We'll have to hope for the best */
1402 if (!runtime_ss
&& syms_ss
)
1403 runtime_ss
= syms_ss
;
1406 ret
= dso__load_sym(dso
, map
, syms_ss
, runtime_ss
, filter
, kmod
);
1413 nr_plt
= dso__synthesize_plt_symbols(dso
, runtime_ss
, map
, filter
);
1418 for (; ss_pos
> 0; ss_pos
--)
1419 symsrc__destroy(&ss_
[ss_pos
- 1]);
1422 if (ret
< 0 && strstr(dso
->name
, " (deleted)") != NULL
)
1427 struct map
*map_groups__find_by_name(struct map_groups
*mg
,
1428 enum map_type type
, const char *name
)
1432 for (nd
= rb_first(&mg
->maps
[type
]); nd
; nd
= rb_next(nd
)) {
1433 struct map
*map
= rb_entry(nd
, struct map
, rb_node
);
1435 if (map
->dso
&& strcmp(map
->dso
->short_name
, name
) == 0)
1442 int dso__load_vmlinux(struct dso
*dso
, struct map
*map
,
1443 const char *vmlinux
, bool vmlinux_allocated
,
1444 symbol_filter_t filter
)
1448 char symfs_vmlinux
[PATH_MAX
];
1449 enum dso_binary_type symtab_type
;
1451 if (vmlinux
[0] == '/')
1452 snprintf(symfs_vmlinux
, sizeof(symfs_vmlinux
), "%s", vmlinux
);
1454 snprintf(symfs_vmlinux
, sizeof(symfs_vmlinux
), "%s%s",
1455 symbol_conf
.symfs
, vmlinux
);
1457 if (dso
->kernel
== DSO_TYPE_GUEST_KERNEL
)
1458 symtab_type
= DSO_BINARY_TYPE__GUEST_VMLINUX
;
1460 symtab_type
= DSO_BINARY_TYPE__VMLINUX
;
1462 if (symsrc__init(&ss
, dso
, symfs_vmlinux
, symtab_type
))
1465 err
= dso__load_sym(dso
, map
, &ss
, &ss
, filter
, 0);
1466 symsrc__destroy(&ss
);
1469 if (dso
->kernel
== DSO_TYPE_GUEST_KERNEL
)
1470 dso
->binary_type
= DSO_BINARY_TYPE__GUEST_VMLINUX
;
1472 dso
->binary_type
= DSO_BINARY_TYPE__VMLINUX
;
1473 dso__set_long_name(dso
, vmlinux
, vmlinux_allocated
);
1474 dso__set_loaded(dso
, map
->type
);
1475 pr_debug("Using %s for symbols\n", symfs_vmlinux
);
1481 int dso__load_vmlinux_path(struct dso
*dso
, struct map
*map
,
1482 symbol_filter_t filter
)
1487 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1488 vmlinux_path__nr_entries
+ 1);
1490 filename
= dso__build_id_filename(dso
, NULL
, 0);
1491 if (filename
!= NULL
) {
1492 err
= dso__load_vmlinux(dso
, map
, filename
, true, filter
);
1498 for (i
= 0; i
< vmlinux_path__nr_entries
; ++i
) {
1499 err
= dso__load_vmlinux(dso
, map
, vmlinux_path
[i
], false, filter
);
1507 static int find_matching_kcore(struct map
*map
, char *dir
, size_t dir_sz
)
1509 char kallsyms_filename
[PATH_MAX
];
1510 struct dirent
*dent
;
1522 if (dent
->d_type
!= DT_DIR
)
1524 scnprintf(kallsyms_filename
, sizeof(kallsyms_filename
),
1525 "%s/%s/kallsyms", dir
, dent
->d_name
);
1526 if (!validate_kcore_addresses(kallsyms_filename
, map
)) {
1527 strlcpy(dir
, kallsyms_filename
, dir_sz
);
1538 static char *dso__find_kallsyms(struct dso
*dso
, struct map
*map
)
1540 u8 host_build_id
[BUILD_ID_SIZE
];
1541 char sbuild_id
[BUILD_ID_SIZE
* 2 + 1];
1542 bool is_host
= false;
1543 char path
[PATH_MAX
];
1545 if (!dso
->has_build_id
) {
1547 * Last resort, if we don't have a build-id and couldn't find
1548 * any vmlinux file, try the running kernel kallsyms table.
1553 if (sysfs__read_build_id("/sys/kernel/notes", host_build_id
,
1554 sizeof(host_build_id
)) == 0)
1555 is_host
= dso__build_id_equal(dso
, host_build_id
);
1557 build_id__sprintf(dso
->build_id
, sizeof(dso
->build_id
), sbuild_id
);
1559 scnprintf(path
, sizeof(path
), "%s/[kernel.kcore]/%s", buildid_dir
,
1562 /* Use /proc/kallsyms if possible */
1567 /* If no cached kcore go with /proc/kallsyms */
1574 * Do not check the build-id cache, until we know we cannot use
1577 fd
= open("/proc/kcore", O_RDONLY
);
1580 /* If module maps match go with /proc/kallsyms */
1581 if (!validate_kcore_addresses("/proc/kallsyms", map
))
1585 /* Find kallsyms in build-id cache with kcore */
1586 if (!find_matching_kcore(map
, path
, sizeof(path
)))
1587 return strdup(path
);
1592 /* Find kallsyms in build-id cache with kcore */
1593 if (!find_matching_kcore(map
, path
, sizeof(path
)))
1594 return strdup(path
);
1596 scnprintf(path
, sizeof(path
), "%s/[kernel.kallsyms]/%s",
1597 buildid_dir
, sbuild_id
);
1599 if (access(path
, F_OK
)) {
1600 pr_err("No kallsyms or vmlinux with build-id %s was found\n",
1605 return strdup(path
);
1608 return strdup("/proc/kallsyms");
1611 static int dso__load_kernel_sym(struct dso
*dso
, struct map
*map
,
1612 symbol_filter_t filter
)
1615 const char *kallsyms_filename
= NULL
;
1616 char *kallsyms_allocated_filename
= NULL
;
1618 * Step 1: if the user specified a kallsyms or vmlinux filename, use
1619 * it and only it, reporting errors to the user if it cannot be used.
1621 * For instance, try to analyse an ARM perf.data file _without_ a
1622 * build-id, or if the user specifies the wrong path to the right
1623 * vmlinux file, obviously we can't fallback to another vmlinux (a
1624 * x86_86 one, on the machine where analysis is being performed, say),
1625 * or worse, /proc/kallsyms.
1627 * If the specified file _has_ a build-id and there is a build-id
1628 * section in the perf.data file, we will still do the expected
1629 * validation in dso__load_vmlinux and will bail out if they don't
1632 if (symbol_conf
.kallsyms_name
!= NULL
) {
1633 kallsyms_filename
= symbol_conf
.kallsyms_name
;
1637 if (!symbol_conf
.ignore_vmlinux
&& symbol_conf
.vmlinux_name
!= NULL
) {
1638 return dso__load_vmlinux(dso
, map
, symbol_conf
.vmlinux_name
,
1642 if (!symbol_conf
.ignore_vmlinux
&& vmlinux_path
!= NULL
) {
1643 err
= dso__load_vmlinux_path(dso
, map
, filter
);
1648 /* do not try local files if a symfs was given */
1649 if (symbol_conf
.symfs
[0] != 0)
1652 kallsyms_allocated_filename
= dso__find_kallsyms(dso
, map
);
1653 if (!kallsyms_allocated_filename
)
1656 kallsyms_filename
= kallsyms_allocated_filename
;
1659 err
= dso__load_kallsyms(dso
, kallsyms_filename
, map
, filter
);
1661 pr_debug("Using %s for symbols\n", kallsyms_filename
);
1662 free(kallsyms_allocated_filename
);
1664 if (err
> 0 && !dso__is_kcore(dso
)) {
1665 dso__set_long_name(dso
, "[kernel.kallsyms]", false);
1666 map__fixup_start(map
);
1667 map__fixup_end(map
);
1673 static int dso__load_guest_kernel_sym(struct dso
*dso
, struct map
*map
,
1674 symbol_filter_t filter
)
1677 const char *kallsyms_filename
= NULL
;
1678 struct machine
*machine
;
1679 char path
[PATH_MAX
];
1682 pr_debug("Guest kernel map hasn't the point to groups\n");
1685 machine
= map
->groups
->machine
;
1687 if (machine__is_default_guest(machine
)) {
1689 * if the user specified a vmlinux filename, use it and only
1690 * it, reporting errors to the user if it cannot be used.
1691 * Or use file guest_kallsyms inputted by user on commandline
1693 if (symbol_conf
.default_guest_vmlinux_name
!= NULL
) {
1694 err
= dso__load_vmlinux(dso
, map
,
1695 symbol_conf
.default_guest_vmlinux_name
,
1700 kallsyms_filename
= symbol_conf
.default_guest_kallsyms
;
1701 if (!kallsyms_filename
)
1704 sprintf(path
, "%s/proc/kallsyms", machine
->root_dir
);
1705 kallsyms_filename
= path
;
1708 err
= dso__load_kallsyms(dso
, kallsyms_filename
, map
, filter
);
1710 pr_debug("Using %s for symbols\n", kallsyms_filename
);
1711 if (err
> 0 && !dso__is_kcore(dso
)) {
1712 machine__mmap_name(machine
, path
, sizeof(path
));
1713 dso__set_long_name(dso
, strdup(path
), true);
1714 map__fixup_start(map
);
1715 map__fixup_end(map
);
1721 static void vmlinux_path__exit(void)
1723 while (--vmlinux_path__nr_entries
>= 0)
1724 zfree(&vmlinux_path
[vmlinux_path__nr_entries
]);
1726 zfree(&vmlinux_path
);
1729 static int vmlinux_path__init(void)
1734 vmlinux_path
= malloc(sizeof(char *) * 5);
1735 if (vmlinux_path
== NULL
)
1738 vmlinux_path
[vmlinux_path__nr_entries
] = strdup("vmlinux");
1739 if (vmlinux_path
[vmlinux_path__nr_entries
] == NULL
)
1741 ++vmlinux_path__nr_entries
;
1742 vmlinux_path
[vmlinux_path__nr_entries
] = strdup("/boot/vmlinux");
1743 if (vmlinux_path
[vmlinux_path__nr_entries
] == NULL
)
1745 ++vmlinux_path__nr_entries
;
1747 /* only try running kernel version if no symfs was given */
1748 if (symbol_conf
.symfs
[0] != 0)
1751 if (uname(&uts
) < 0)
1754 snprintf(bf
, sizeof(bf
), "/boot/vmlinux-%s", uts
.release
);
1755 vmlinux_path
[vmlinux_path__nr_entries
] = strdup(bf
);
1756 if (vmlinux_path
[vmlinux_path__nr_entries
] == NULL
)
1758 ++vmlinux_path__nr_entries
;
1759 snprintf(bf
, sizeof(bf
), "/lib/modules/%s/build/vmlinux", uts
.release
);
1760 vmlinux_path
[vmlinux_path__nr_entries
] = strdup(bf
);
1761 if (vmlinux_path
[vmlinux_path__nr_entries
] == NULL
)
1763 ++vmlinux_path__nr_entries
;
1764 snprintf(bf
, sizeof(bf
), "/usr/lib/debug/lib/modules/%s/vmlinux",
1766 vmlinux_path
[vmlinux_path__nr_entries
] = strdup(bf
);
1767 if (vmlinux_path
[vmlinux_path__nr_entries
] == NULL
)
1769 ++vmlinux_path__nr_entries
;
1774 vmlinux_path__exit();
1778 int setup_list(struct strlist
**list
, const char *list_str
,
1779 const char *list_name
)
1781 if (list_str
== NULL
)
1784 *list
= strlist__new(true, list_str
);
1786 pr_err("problems parsing %s list\n", list_name
);
1792 static bool symbol__read_kptr_restrict(void)
1796 if (geteuid() != 0) {
1797 FILE *fp
= fopen("/proc/sys/kernel/kptr_restrict", "r");
1801 if (fgets(line
, sizeof(line
), fp
) != NULL
)
1802 value
= atoi(line
) != 0;
1811 int symbol__init(void)
1815 if (symbol_conf
.initialized
)
1818 symbol_conf
.priv_size
= PERF_ALIGN(symbol_conf
.priv_size
, sizeof(u64
));
1822 if (symbol_conf
.sort_by_name
)
1823 symbol_conf
.priv_size
+= (sizeof(struct symbol_name_rb_node
) -
1824 sizeof(struct symbol
));
1826 if (symbol_conf
.try_vmlinux_path
&& vmlinux_path__init() < 0)
1829 if (symbol_conf
.field_sep
&& *symbol_conf
.field_sep
== '.') {
1830 pr_err("'.' is the only non valid --field-separator argument\n");
1834 if (setup_list(&symbol_conf
.dso_list
,
1835 symbol_conf
.dso_list_str
, "dso") < 0)
1838 if (setup_list(&symbol_conf
.comm_list
,
1839 symbol_conf
.comm_list_str
, "comm") < 0)
1840 goto out_free_dso_list
;
1842 if (setup_list(&symbol_conf
.sym_list
,
1843 symbol_conf
.sym_list_str
, "symbol") < 0)
1844 goto out_free_comm_list
;
1847 * A path to symbols of "/" is identical to ""
1848 * reset here for simplicity.
1850 symfs
= realpath(symbol_conf
.symfs
, NULL
);
1852 symfs
= symbol_conf
.symfs
;
1853 if (strcmp(symfs
, "/") == 0)
1854 symbol_conf
.symfs
= "";
1855 if (symfs
!= symbol_conf
.symfs
)
1856 free((void *)symfs
);
1858 symbol_conf
.kptr_restrict
= symbol__read_kptr_restrict();
1860 symbol_conf
.initialized
= true;
1864 strlist__delete(symbol_conf
.comm_list
);
1866 strlist__delete(symbol_conf
.dso_list
);
1870 void symbol__exit(void)
1872 if (!symbol_conf
.initialized
)
1874 strlist__delete(symbol_conf
.sym_list
);
1875 strlist__delete(symbol_conf
.dso_list
);
1876 strlist__delete(symbol_conf
.comm_list
);
1877 vmlinux_path__exit();
1878 symbol_conf
.sym_list
= symbol_conf
.dso_list
= symbol_conf
.comm_list
= NULL
;
1879 symbol_conf
.initialized
= false;