6 #include <linux/kernel.h>
23 #include "sane_ctype.h"
27 #include <symbol/kallsyms.h>
28 #include <sys/utsname.h>
30 static int dso__load_kernel_sym(struct dso
*dso
, struct map
*map
);
31 static int dso__load_guest_kernel_sym(struct dso
*dso
, struct map
*map
);
32 static bool symbol__is_idle(const char *name
);
34 int vmlinux_path__nr_entries
;
37 struct symbol_conf symbol_conf
= {
39 .try_vmlinux_path
= true,
42 .demangle_kernel
= false,
43 .cumulate_callchain
= true,
44 .show_hist_headers
= true,
49 static enum dso_binary_type binary_type_symtab
[] = {
50 DSO_BINARY_TYPE__KALLSYMS
,
51 DSO_BINARY_TYPE__GUEST_KALLSYMS
,
52 DSO_BINARY_TYPE__JAVA_JIT
,
53 DSO_BINARY_TYPE__DEBUGLINK
,
54 DSO_BINARY_TYPE__BUILD_ID_CACHE
,
55 DSO_BINARY_TYPE__FEDORA_DEBUGINFO
,
56 DSO_BINARY_TYPE__UBUNTU_DEBUGINFO
,
57 DSO_BINARY_TYPE__BUILDID_DEBUGINFO
,
58 DSO_BINARY_TYPE__SYSTEM_PATH_DSO
,
59 DSO_BINARY_TYPE__GUEST_KMODULE
,
60 DSO_BINARY_TYPE__GUEST_KMODULE_COMP
,
61 DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE
,
62 DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP
,
63 DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO
,
64 DSO_BINARY_TYPE__NOT_FOUND
,
67 #define DSO_BINARY_TYPE__SYMTAB_CNT ARRAY_SIZE(binary_type_symtab)
69 bool symbol_type__is_a(char symbol_type
, enum map_type map_type
)
71 symbol_type
= toupper(symbol_type
);
75 return symbol_type
== 'T' || symbol_type
== 'W';
77 return symbol_type
== 'D';
83 static int prefix_underscores_count(const char *str
)
85 const char *tail
= str
;
93 int __weak
arch__compare_symbol_names(const char *namea
, const char *nameb
)
95 return strcmp(namea
, nameb
);
98 int __weak
arch__compare_symbol_names_n(const char *namea
, const char *nameb
,
101 return strncmp(namea
, nameb
, n
);
104 int __weak
arch__choose_best_symbol(struct symbol
*syma
,
105 struct symbol
*symb __maybe_unused
)
107 /* Avoid "SyS" kernel syscall aliases */
108 if (strlen(syma
->name
) >= 3 && !strncmp(syma
->name
, "SyS", 3))
110 if (strlen(syma
->name
) >= 10 && !strncmp(syma
->name
, "compat_SyS", 10))
116 static int choose_best_symbol(struct symbol
*syma
, struct symbol
*symb
)
122 /* Prefer a symbol with non zero length */
123 a
= syma
->end
- syma
->start
;
124 b
= symb
->end
- symb
->start
;
125 if ((b
== 0) && (a
> 0))
127 else if ((a
== 0) && (b
> 0))
130 /* Prefer a non weak symbol over a weak one */
131 a
= syma
->binding
== STB_WEAK
;
132 b
= symb
->binding
== STB_WEAK
;
138 /* Prefer a global symbol over a non global one */
139 a
= syma
->binding
== STB_GLOBAL
;
140 b
= symb
->binding
== STB_GLOBAL
;
146 /* Prefer a symbol with less underscores */
147 a
= prefix_underscores_count(syma
->name
);
148 b
= prefix_underscores_count(symb
->name
);
154 /* Choose the symbol with the longest name */
155 na
= strlen(syma
->name
);
156 nb
= strlen(symb
->name
);
162 return arch__choose_best_symbol(syma
, symb
);
165 void symbols__fixup_duplicate(struct rb_root
*symbols
)
168 struct symbol
*curr
, *next
;
170 if (symbol_conf
.allow_aliases
)
173 nd
= rb_first(symbols
);
176 curr
= rb_entry(nd
, struct symbol
, rb_node
);
178 nd
= rb_next(&curr
->rb_node
);
179 next
= rb_entry(nd
, struct symbol
, rb_node
);
184 if (curr
->start
!= next
->start
)
187 if (choose_best_symbol(curr
, next
) == SYMBOL_A
) {
188 rb_erase(&next
->rb_node
, symbols
);
189 symbol__delete(next
);
192 nd
= rb_next(&curr
->rb_node
);
193 rb_erase(&curr
->rb_node
, symbols
);
194 symbol__delete(curr
);
199 void symbols__fixup_end(struct rb_root
*symbols
)
201 struct rb_node
*nd
, *prevnd
= rb_first(symbols
);
202 struct symbol
*curr
, *prev
;
207 curr
= rb_entry(prevnd
, struct symbol
, rb_node
);
209 for (nd
= rb_next(prevnd
); nd
; nd
= rb_next(nd
)) {
211 curr
= rb_entry(nd
, struct symbol
, rb_node
);
213 if (prev
->end
== prev
->start
&& prev
->end
!= curr
->start
)
214 prev
->end
= curr
->start
;
218 if (curr
->end
== curr
->start
)
219 curr
->end
= roundup(curr
->start
, 4096) + 4096;
222 void __map_groups__fixup_end(struct map_groups
*mg
, enum map_type type
)
224 struct maps
*maps
= &mg
->maps
[type
];
225 struct map
*next
, *curr
;
227 pthread_rwlock_wrlock(&maps
->lock
);
229 curr
= maps__first(maps
);
233 for (next
= map__next(curr
); next
; next
= map__next(curr
)) {
234 curr
->end
= next
->start
;
239 * We still haven't the actual symbols, so guess the
240 * last map final address.
245 pthread_rwlock_unlock(&maps
->lock
);
248 struct symbol
*symbol__new(u64 start
, u64 len
, u8 binding
, const char *name
)
250 size_t namelen
= strlen(name
) + 1;
251 struct symbol
*sym
= calloc(1, (symbol_conf
.priv_size
+
252 sizeof(*sym
) + namelen
));
256 if (symbol_conf
.priv_size
) {
257 if (symbol_conf
.init_annotation
) {
258 struct annotation
*notes
= (void *)sym
;
259 pthread_mutex_init(¬es
->lock
, NULL
);
261 sym
= ((void *)sym
) + symbol_conf
.priv_size
;
265 sym
->end
= len
? start
+ len
: start
;
266 sym
->binding
= binding
;
267 sym
->namelen
= namelen
- 1;
269 pr_debug4("%s: %s %#" PRIx64
"-%#" PRIx64
"\n",
270 __func__
, name
, start
, sym
->end
);
271 memcpy(sym
->name
, name
, namelen
);
276 void symbol__delete(struct symbol
*sym
)
278 free(((void *)sym
) - symbol_conf
.priv_size
);
281 void symbols__delete(struct rb_root
*symbols
)
284 struct rb_node
*next
= rb_first(symbols
);
287 pos
= rb_entry(next
, struct symbol
, rb_node
);
288 next
= rb_next(&pos
->rb_node
);
289 rb_erase(&pos
->rb_node
, symbols
);
294 void __symbols__insert(struct rb_root
*symbols
, struct symbol
*sym
, bool kernel
)
296 struct rb_node
**p
= &symbols
->rb_node
;
297 struct rb_node
*parent
= NULL
;
298 const u64 ip
= sym
->start
;
302 const char *name
= sym
->name
;
304 * ppc64 uses function descriptors and appends a '.' to the
305 * start of every instruction address. Remove it.
309 sym
->idle
= symbol__is_idle(name
);
314 s
= rb_entry(parent
, struct symbol
, rb_node
);
320 rb_link_node(&sym
->rb_node
, parent
, p
);
321 rb_insert_color(&sym
->rb_node
, symbols
);
324 void symbols__insert(struct rb_root
*symbols
, struct symbol
*sym
)
326 __symbols__insert(symbols
, sym
, false);
329 static struct symbol
*symbols__find(struct rb_root
*symbols
, u64 ip
)
336 n
= symbols
->rb_node
;
339 struct symbol
*s
= rb_entry(n
, struct symbol
, rb_node
);
343 else if (ip
> s
->end
|| (ip
== s
->end
&& ip
!= s
->start
))
352 static struct symbol
*symbols__first(struct rb_root
*symbols
)
354 struct rb_node
*n
= rb_first(symbols
);
357 return rb_entry(n
, struct symbol
, rb_node
);
362 static struct symbol
*symbols__last(struct rb_root
*symbols
)
364 struct rb_node
*n
= rb_last(symbols
);
367 return rb_entry(n
, struct symbol
, rb_node
);
372 static struct symbol
*symbols__next(struct symbol
*sym
)
374 struct rb_node
*n
= rb_next(&sym
->rb_node
);
377 return rb_entry(n
, struct symbol
, rb_node
);
382 static void symbols__insert_by_name(struct rb_root
*symbols
, struct symbol
*sym
)
384 struct rb_node
**p
= &symbols
->rb_node
;
385 struct rb_node
*parent
= NULL
;
386 struct symbol_name_rb_node
*symn
, *s
;
388 symn
= container_of(sym
, struct symbol_name_rb_node
, sym
);
392 s
= rb_entry(parent
, struct symbol_name_rb_node
, rb_node
);
393 if (strcmp(sym
->name
, s
->sym
.name
) < 0)
398 rb_link_node(&symn
->rb_node
, parent
, p
);
399 rb_insert_color(&symn
->rb_node
, symbols
);
402 static void symbols__sort_by_name(struct rb_root
*symbols
,
403 struct rb_root
*source
)
407 for (nd
= rb_first(source
); nd
; nd
= rb_next(nd
)) {
408 struct symbol
*pos
= rb_entry(nd
, struct symbol
, rb_node
);
409 symbols__insert_by_name(symbols
, pos
);
413 int symbol__match_symbol_name(const char *name
, const char *str
,
414 enum symbol_tag_include includes
)
416 const char *versioning
;
418 if (includes
== SYMBOL_TAG_INCLUDE__DEFAULT_ONLY
&&
419 (versioning
= strstr(name
, "@@"))) {
420 int len
= strlen(str
);
422 if (len
< versioning
- name
)
423 len
= versioning
- name
;
425 return arch__compare_symbol_names_n(name
, str
, len
);
427 return arch__compare_symbol_names(name
, str
);
430 static struct symbol
*symbols__find_by_name(struct rb_root
*symbols
,
432 enum symbol_tag_include includes
)
435 struct symbol_name_rb_node
*s
= NULL
;
440 n
= symbols
->rb_node
;
445 s
= rb_entry(n
, struct symbol_name_rb_node
, rb_node
);
446 cmp
= symbol__match_symbol_name(s
->sym
.name
, name
, includes
);
459 if (includes
!= SYMBOL_TAG_INCLUDE__DEFAULT_ONLY
)
460 /* return first symbol that has same name (if any) */
461 for (n
= rb_prev(n
); n
; n
= rb_prev(n
)) {
462 struct symbol_name_rb_node
*tmp
;
464 tmp
= rb_entry(n
, struct symbol_name_rb_node
, rb_node
);
465 if (arch__compare_symbol_names(tmp
->sym
.name
, s
->sym
.name
))
474 void dso__reset_find_symbol_cache(struct dso
*dso
)
478 for (type
= MAP__FUNCTION
; type
<= MAP__VARIABLE
; ++type
) {
479 dso
->last_find_result
[type
].addr
= 0;
480 dso
->last_find_result
[type
].symbol
= NULL
;
484 void dso__insert_symbol(struct dso
*dso
, enum map_type type
, struct symbol
*sym
)
486 __symbols__insert(&dso
->symbols
[type
], sym
, dso
->kernel
);
488 /* update the symbol cache if necessary */
489 if (dso
->last_find_result
[type
].addr
>= sym
->start
&&
490 (dso
->last_find_result
[type
].addr
< sym
->end
||
491 sym
->start
== sym
->end
)) {
492 dso
->last_find_result
[type
].symbol
= sym
;
496 struct symbol
*dso__find_symbol(struct dso
*dso
,
497 enum map_type type
, u64 addr
)
499 if (dso
->last_find_result
[type
].addr
!= addr
|| dso
->last_find_result
[type
].symbol
== NULL
) {
500 dso
->last_find_result
[type
].addr
= addr
;
501 dso
->last_find_result
[type
].symbol
= symbols__find(&dso
->symbols
[type
], addr
);
504 return dso
->last_find_result
[type
].symbol
;
507 struct symbol
*dso__first_symbol(struct dso
*dso
, enum map_type type
)
509 return symbols__first(&dso
->symbols
[type
]);
512 struct symbol
*dso__last_symbol(struct dso
*dso
, enum map_type type
)
514 return symbols__last(&dso
->symbols
[type
]);
517 struct symbol
*dso__next_symbol(struct symbol
*sym
)
519 return symbols__next(sym
);
522 struct symbol
*symbol__next_by_name(struct symbol
*sym
)
524 struct symbol_name_rb_node
*s
= container_of(sym
, struct symbol_name_rb_node
, sym
);
525 struct rb_node
*n
= rb_next(&s
->rb_node
);
527 return n
? &rb_entry(n
, struct symbol_name_rb_node
, rb_node
)->sym
: NULL
;
531 * Teturns first symbol that matched with @name.
533 struct symbol
*dso__find_symbol_by_name(struct dso
*dso
, enum map_type type
,
536 struct symbol
*s
= symbols__find_by_name(&dso
->symbol_names
[type
], name
,
537 SYMBOL_TAG_INCLUDE__NONE
);
539 s
= symbols__find_by_name(&dso
->symbol_names
[type
], name
,
540 SYMBOL_TAG_INCLUDE__DEFAULT_ONLY
);
544 void dso__sort_by_name(struct dso
*dso
, enum map_type type
)
546 dso__set_sorted_by_name(dso
, type
);
547 return symbols__sort_by_name(&dso
->symbol_names
[type
],
548 &dso
->symbols
[type
]);
551 int modules__parse(const char *filename
, void *arg
,
552 int (*process_module
)(void *arg
, const char *name
,
560 file
= fopen(filename
, "r");
570 line_len
= getline(&line
, &n
, file
);
583 line
[--line_len
] = '\0'; /* \n */
585 sep
= strrchr(line
, 'x');
589 hex2u64(sep
+ 1, &start
);
591 sep
= strchr(line
, ' ');
597 scnprintf(name
, sizeof(name
), "[%s]", line
);
599 err
= process_module(arg
, name
, start
);
609 struct process_kallsyms_args
{
615 * These are symbols in the kernel image, so make sure that
616 * sym is from a kernel DSO.
618 static bool symbol__is_idle(const char *name
)
620 const char * const idle_symbols
[] = {
629 "mwait_idle_with_hints",
631 "ppc64_runlatch_off",
632 "pseries_dedicated_idle_sleep",
637 for (i
= 0; idle_symbols
[i
]; i
++) {
638 if (!strcmp(idle_symbols
[i
], name
))
645 static int map__process_kallsym_symbol(void *arg
, const char *name
,
646 char type
, u64 start
)
649 struct process_kallsyms_args
*a
= arg
;
650 struct rb_root
*root
= &a
->dso
->symbols
[a
->map
->type
];
652 if (!symbol_type__is_a(type
, a
->map
->type
))
656 * module symbols are not sorted so we add all
657 * symbols, setting length to 0, and rely on
658 * symbols__fixup_end() to fix it up.
660 sym
= symbol__new(start
, 0, kallsyms2elf_binding(type
), name
);
664 * We will pass the symbols to the filter later, in
665 * map__split_kallsyms, when we have split the maps per module
667 __symbols__insert(root
, sym
, !strchr(name
, '['));
673 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
674 * so that we can in the next step set the symbol ->end address and then
675 * call kernel_maps__split_kallsyms.
677 static int dso__load_all_kallsyms(struct dso
*dso
, const char *filename
,
680 struct process_kallsyms_args args
= { .map
= map
, .dso
= dso
, };
681 return kallsyms__parse(filename
, &args
, map__process_kallsym_symbol
);
684 static int dso__split_kallsyms_for_kcore(struct dso
*dso
, struct map
*map
)
686 struct map_groups
*kmaps
= map__kmaps(map
);
687 struct map
*curr_map
;
690 struct rb_root old_root
= dso
->symbols
[map
->type
];
691 struct rb_root
*root
= &dso
->symbols
[map
->type
];
692 struct rb_node
*next
= rb_first(root
);
702 pos
= rb_entry(next
, struct symbol
, rb_node
);
703 next
= rb_next(&pos
->rb_node
);
705 rb_erase_init(&pos
->rb_node
, &old_root
);
707 module
= strchr(pos
->name
, '\t');
711 curr_map
= map_groups__find(kmaps
, map
->type
, pos
->start
);
718 pos
->start
-= curr_map
->start
- curr_map
->pgoff
;
720 pos
->end
-= curr_map
->start
- curr_map
->pgoff
;
721 symbols__insert(&curr_map
->dso
->symbols
[curr_map
->type
], pos
);
725 /* Symbols have been adjusted */
726 dso
->adjust_symbols
= 1;
732 * Split the symbols into maps, making sure there are no overlaps, i.e. the
733 * kernel range is broken in several maps, named [kernel].N, as we don't have
734 * the original ELF section names vmlinux have.
736 static int dso__split_kallsyms(struct dso
*dso
, struct map
*map
, u64 delta
)
738 struct map_groups
*kmaps
= map__kmaps(map
);
739 struct machine
*machine
;
740 struct map
*curr_map
= map
;
742 int count
= 0, moved
= 0;
743 struct rb_root
*root
= &dso
->symbols
[map
->type
];
744 struct rb_node
*next
= rb_first(root
);
745 int kernel_range
= 0;
750 machine
= kmaps
->machine
;
755 pos
= rb_entry(next
, struct symbol
, rb_node
);
756 next
= rb_next(&pos
->rb_node
);
758 module
= strchr(pos
->name
, '\t');
760 if (!symbol_conf
.use_modules
)
765 if (strcmp(curr_map
->dso
->short_name
, module
)) {
766 if (curr_map
!= map
&&
767 dso
->kernel
== DSO_TYPE_GUEST_KERNEL
&&
768 machine__is_default_guest(machine
)) {
770 * We assume all symbols of a module are
771 * continuous in * kallsyms, so curr_map
772 * points to a module and all its
773 * symbols are in its kmap. Mark it as
776 dso__set_loaded(curr_map
->dso
,
780 curr_map
= map_groups__find_by_name(kmaps
,
782 if (curr_map
== NULL
) {
783 pr_debug("%s/proc/{kallsyms,modules} "
784 "inconsistency while looking "
785 "for \"%s\" module!\n",
786 machine
->root_dir
, module
);
791 if (curr_map
->dso
->loaded
&&
792 !machine__is_default_guest(machine
))
796 * So that we look just like we get from .ko files,
797 * i.e. not prelinked, relative to map->start.
799 pos
->start
= curr_map
->map_ip(curr_map
, pos
->start
);
800 pos
->end
= curr_map
->map_ip(curr_map
, pos
->end
);
801 } else if (curr_map
!= map
) {
802 char dso_name
[PATH_MAX
];
806 /* Kernel was relocated at boot time */
816 if (dso
->kernel
== DSO_TYPE_GUEST_KERNEL
)
817 snprintf(dso_name
, sizeof(dso_name
),
821 snprintf(dso_name
, sizeof(dso_name
),
825 ndso
= dso__new(dso_name
);
829 ndso
->kernel
= dso
->kernel
;
831 curr_map
= map__new2(pos
->start
, ndso
, map
->type
);
832 if (curr_map
== NULL
) {
837 curr_map
->map_ip
= curr_map
->unmap_ip
= identity__map_ip
;
838 map_groups__insert(kmaps
, curr_map
);
841 /* Kernel was relocated at boot time */
846 if (curr_map
!= map
) {
847 rb_erase(&pos
->rb_node
, root
);
848 symbols__insert(&curr_map
->dso
->symbols
[curr_map
->type
], pos
);
855 rb_erase(&pos
->rb_node
, root
);
859 if (curr_map
!= map
&&
860 dso
->kernel
== DSO_TYPE_GUEST_KERNEL
&&
861 machine__is_default_guest(kmaps
->machine
)) {
862 dso__set_loaded(curr_map
->dso
, curr_map
->type
);
865 return count
+ moved
;
868 bool symbol__restricted_filename(const char *filename
,
869 const char *restricted_filename
)
871 bool restricted
= false;
873 if (symbol_conf
.kptr_restrict
) {
874 char *r
= realpath(filename
, NULL
);
877 restricted
= strcmp(r
, restricted_filename
) == 0;
887 struct rb_node rb_node
;
892 static void add_module(struct module_info
*mi
, struct rb_root
*modules
)
894 struct rb_node
**p
= &modules
->rb_node
;
895 struct rb_node
*parent
= NULL
;
896 struct module_info
*m
;
900 m
= rb_entry(parent
, struct module_info
, rb_node
);
901 if (strcmp(mi
->name
, m
->name
) < 0)
906 rb_link_node(&mi
->rb_node
, parent
, p
);
907 rb_insert_color(&mi
->rb_node
, modules
);
910 static void delete_modules(struct rb_root
*modules
)
912 struct module_info
*mi
;
913 struct rb_node
*next
= rb_first(modules
);
916 mi
= rb_entry(next
, struct module_info
, rb_node
);
917 next
= rb_next(&mi
->rb_node
);
918 rb_erase(&mi
->rb_node
, modules
);
924 static struct module_info
*find_module(const char *name
,
925 struct rb_root
*modules
)
927 struct rb_node
*n
= modules
->rb_node
;
930 struct module_info
*m
;
933 m
= rb_entry(n
, struct module_info
, rb_node
);
934 cmp
= strcmp(name
, m
->name
);
946 static int __read_proc_modules(void *arg
, const char *name
, u64 start
)
948 struct rb_root
*modules
= arg
;
949 struct module_info
*mi
;
951 mi
= zalloc(sizeof(struct module_info
));
955 mi
->name
= strdup(name
);
963 add_module(mi
, modules
);
968 static int read_proc_modules(const char *filename
, struct rb_root
*modules
)
970 if (symbol__restricted_filename(filename
, "/proc/modules"))
973 if (modules__parse(filename
, modules
, __read_proc_modules
)) {
974 delete_modules(modules
);
981 int compare_proc_modules(const char *from
, const char *to
)
983 struct rb_root from_modules
= RB_ROOT
;
984 struct rb_root to_modules
= RB_ROOT
;
985 struct rb_node
*from_node
, *to_node
;
986 struct module_info
*from_m
, *to_m
;
989 if (read_proc_modules(from
, &from_modules
))
992 if (read_proc_modules(to
, &to_modules
))
993 goto out_delete_from
;
995 from_node
= rb_first(&from_modules
);
996 to_node
= rb_first(&to_modules
);
1001 from_m
= rb_entry(from_node
, struct module_info
, rb_node
);
1002 to_m
= rb_entry(to_node
, struct module_info
, rb_node
);
1004 if (from_m
->start
!= to_m
->start
||
1005 strcmp(from_m
->name
, to_m
->name
))
1008 from_node
= rb_next(from_node
);
1009 to_node
= rb_next(to_node
);
1012 if (!from_node
&& !to_node
)
1015 delete_modules(&to_modules
);
1017 delete_modules(&from_modules
);
1022 static int do_validate_kcore_modules(const char *filename
, struct map
*map
,
1023 struct map_groups
*kmaps
)
1025 struct rb_root modules
= RB_ROOT
;
1026 struct map
*old_map
;
1029 err
= read_proc_modules(filename
, &modules
);
1033 old_map
= map_groups__first(kmaps
, map
->type
);
1035 struct map
*next
= map_groups__next(old_map
);
1036 struct module_info
*mi
;
1038 if (old_map
== map
|| old_map
->start
== map
->start
) {
1039 /* The kernel map */
1044 /* Module must be in memory at the same address */
1045 mi
= find_module(old_map
->dso
->short_name
, &modules
);
1046 if (!mi
|| mi
->start
!= old_map
->start
) {
1054 delete_modules(&modules
);
1059 * If kallsyms is referenced by name then we look for filename in the same
1062 static bool filename_from_kallsyms_filename(char *filename
,
1063 const char *base_name
,
1064 const char *kallsyms_filename
)
1068 strcpy(filename
, kallsyms_filename
);
1069 name
= strrchr(filename
, '/');
1075 if (!strcmp(name
, "kallsyms")) {
1076 strcpy(name
, base_name
);
1083 static int validate_kcore_modules(const char *kallsyms_filename
,
1086 struct map_groups
*kmaps
= map__kmaps(map
);
1087 char modules_filename
[PATH_MAX
];
1092 if (!filename_from_kallsyms_filename(modules_filename
, "modules",
1096 if (do_validate_kcore_modules(modules_filename
, map
, kmaps
))
1102 static int validate_kcore_addresses(const char *kallsyms_filename
,
1105 struct kmap
*kmap
= map__kmap(map
);
1110 if (kmap
->ref_reloc_sym
&& kmap
->ref_reloc_sym
->name
) {
1113 if (kallsyms__get_function_start(kallsyms_filename
,
1114 kmap
->ref_reloc_sym
->name
, &start
))
1116 if (start
!= kmap
->ref_reloc_sym
->addr
)
1120 return validate_kcore_modules(kallsyms_filename
, map
);
1123 struct kcore_mapfn_data
{
1126 struct list_head maps
;
1129 static int kcore_mapfn(u64 start
, u64 len
, u64 pgoff
, void *data
)
1131 struct kcore_mapfn_data
*md
= data
;
1134 map
= map__new2(start
, md
->dso
, md
->type
);
1138 map
->end
= map
->start
+ len
;
1141 list_add(&map
->node
, &md
->maps
);
1146 static int dso__load_kcore(struct dso
*dso
, struct map
*map
,
1147 const char *kallsyms_filename
)
1149 struct map_groups
*kmaps
= map__kmaps(map
);
1150 struct machine
*machine
;
1151 struct kcore_mapfn_data md
;
1152 struct map
*old_map
, *new_map
, *replacement_map
= NULL
;
1155 char kcore_filename
[PATH_MAX
];
1161 machine
= kmaps
->machine
;
1163 /* This function requires that the map is the kernel map */
1164 if (map
!= machine
->vmlinux_maps
[map
->type
])
1167 if (!filename_from_kallsyms_filename(kcore_filename
, "kcore",
1171 /* Modules and kernel must be present at their original addresses */
1172 if (validate_kcore_addresses(kallsyms_filename
, map
))
1176 md
.type
= map
->type
;
1177 INIT_LIST_HEAD(&md
.maps
);
1179 fd
= open(kcore_filename
, O_RDONLY
);
1181 pr_debug("Failed to open %s. Note /proc/kcore requires CAP_SYS_RAWIO capability to access.\n",
1186 /* Read new maps into temporary lists */
1187 err
= file__read_maps(fd
, md
.type
== MAP__FUNCTION
, kcore_mapfn
, &md
,
1191 dso
->is_64_bit
= is_64_bit
;
1193 if (list_empty(&md
.maps
)) {
1198 /* Remove old maps */
1199 old_map
= map_groups__first(kmaps
, map
->type
);
1201 struct map
*next
= map_groups__next(old_map
);
1204 map_groups__remove(kmaps
, old_map
);
1208 /* Find the kernel map using the first symbol */
1209 sym
= dso__first_symbol(dso
, map
->type
);
1210 list_for_each_entry(new_map
, &md
.maps
, node
) {
1211 if (sym
&& sym
->start
>= new_map
->start
&&
1212 sym
->start
< new_map
->end
) {
1213 replacement_map
= new_map
;
1218 if (!replacement_map
)
1219 replacement_map
= list_entry(md
.maps
.next
, struct map
, node
);
1222 while (!list_empty(&md
.maps
)) {
1223 new_map
= list_entry(md
.maps
.next
, struct map
, node
);
1224 list_del_init(&new_map
->node
);
1225 if (new_map
== replacement_map
) {
1226 map
->start
= new_map
->start
;
1227 map
->end
= new_map
->end
;
1228 map
->pgoff
= new_map
->pgoff
;
1229 map
->map_ip
= new_map
->map_ip
;
1230 map
->unmap_ip
= new_map
->unmap_ip
;
1231 /* Ensure maps are correctly ordered */
1233 map_groups__remove(kmaps
, map
);
1234 map_groups__insert(kmaps
, map
);
1237 map_groups__insert(kmaps
, new_map
);
1244 * Set the data type and long name so that kcore can be read via
1245 * dso__data_read_addr().
1247 if (dso
->kernel
== DSO_TYPE_GUEST_KERNEL
)
1248 dso
->binary_type
= DSO_BINARY_TYPE__GUEST_KCORE
;
1250 dso
->binary_type
= DSO_BINARY_TYPE__KCORE
;
1251 dso__set_long_name(dso
, strdup(kcore_filename
), true);
1255 if (map
->type
== MAP__FUNCTION
)
1256 pr_debug("Using %s for kernel object code\n", kcore_filename
);
1258 pr_debug("Using %s for kernel data\n", kcore_filename
);
1263 while (!list_empty(&md
.maps
)) {
1264 map
= list_entry(md
.maps
.next
, struct map
, node
);
1265 list_del_init(&map
->node
);
1273 * If the kernel is relocated at boot time, kallsyms won't match. Compute the
1274 * delta based on the relocation reference symbol.
1276 static int kallsyms__delta(struct map
*map
, const char *filename
, u64
*delta
)
1278 struct kmap
*kmap
= map__kmap(map
);
1284 if (!kmap
->ref_reloc_sym
|| !kmap
->ref_reloc_sym
->name
)
1287 if (kallsyms__get_function_start(filename
, kmap
->ref_reloc_sym
->name
, &addr
))
1290 *delta
= addr
- kmap
->ref_reloc_sym
->addr
;
1294 int __dso__load_kallsyms(struct dso
*dso
, const char *filename
,
1295 struct map
*map
, bool no_kcore
)
1299 if (symbol__restricted_filename(filename
, "/proc/kallsyms"))
1302 if (dso__load_all_kallsyms(dso
, filename
, map
) < 0)
1305 if (kallsyms__delta(map
, filename
, &delta
))
1308 symbols__fixup_end(&dso
->symbols
[map
->type
]);
1309 symbols__fixup_duplicate(&dso
->symbols
[map
->type
]);
1311 if (dso
->kernel
== DSO_TYPE_GUEST_KERNEL
)
1312 dso
->symtab_type
= DSO_BINARY_TYPE__GUEST_KALLSYMS
;
1314 dso
->symtab_type
= DSO_BINARY_TYPE__KALLSYMS
;
1316 if (!no_kcore
&& !dso__load_kcore(dso
, map
, filename
))
1317 return dso__split_kallsyms_for_kcore(dso
, map
);
1319 return dso__split_kallsyms(dso
, map
, delta
);
1322 int dso__load_kallsyms(struct dso
*dso
, const char *filename
,
1325 return __dso__load_kallsyms(dso
, filename
, map
, false);
1328 static int dso__load_perf_map(struct dso
*dso
, struct map
*map
)
1335 file
= fopen(dso
->long_name
, "r");
1339 while (!feof(file
)) {
1344 line_len
= getline(&line
, &n
, file
);
1351 line
[--line_len
] = '\0'; /* \n */
1353 len
= hex2u64(line
, &start
);
1356 if (len
+ 2 >= line_len
)
1359 len
+= hex2u64(line
+ len
, &size
);
1362 if (len
+ 2 >= line_len
)
1365 sym
= symbol__new(start
, size
, STB_GLOBAL
, line
+ len
);
1368 goto out_delete_line
;
1370 symbols__insert(&dso
->symbols
[map
->type
], sym
);
1385 static bool dso__is_compatible_symtab_type(struct dso
*dso
, bool kmod
,
1386 enum dso_binary_type type
)
1389 case DSO_BINARY_TYPE__JAVA_JIT
:
1390 case DSO_BINARY_TYPE__DEBUGLINK
:
1391 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO
:
1392 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO
:
1393 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO
:
1394 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO
:
1395 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO
:
1396 return !kmod
&& dso
->kernel
== DSO_TYPE_USER
;
1398 case DSO_BINARY_TYPE__KALLSYMS
:
1399 case DSO_BINARY_TYPE__VMLINUX
:
1400 case DSO_BINARY_TYPE__KCORE
:
1401 return dso
->kernel
== DSO_TYPE_KERNEL
;
1403 case DSO_BINARY_TYPE__GUEST_KALLSYMS
:
1404 case DSO_BINARY_TYPE__GUEST_VMLINUX
:
1405 case DSO_BINARY_TYPE__GUEST_KCORE
:
1406 return dso
->kernel
== DSO_TYPE_GUEST_KERNEL
;
1408 case DSO_BINARY_TYPE__GUEST_KMODULE
:
1409 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP
:
1410 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE
:
1411 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP
:
1413 * kernel modules know their symtab type - it's set when
1414 * creating a module dso in machine__findnew_module_map().
1416 return kmod
&& dso
->symtab_type
== type
;
1418 case DSO_BINARY_TYPE__BUILD_ID_CACHE
:
1421 case DSO_BINARY_TYPE__NOT_FOUND
:
1427 int dso__load(struct dso
*dso
, struct map
*map
)
1432 struct machine
*machine
;
1433 char *root_dir
= (char *) "";
1435 struct symsrc ss_
[2];
1436 struct symsrc
*syms_ss
= NULL
, *runtime_ss
= NULL
;
1438 unsigned char build_id
[BUILD_ID_SIZE
];
1440 pthread_mutex_lock(&dso
->lock
);
1442 /* check again under the dso->lock */
1443 if (dso__loaded(dso
, map
->type
)) {
1449 if (dso
->kernel
== DSO_TYPE_KERNEL
)
1450 ret
= dso__load_kernel_sym(dso
, map
);
1451 else if (dso
->kernel
== DSO_TYPE_GUEST_KERNEL
)
1452 ret
= dso__load_guest_kernel_sym(dso
, map
);
1457 if (map
->groups
&& map
->groups
->machine
)
1458 machine
= map
->groups
->machine
;
1462 dso
->adjust_symbols
= 0;
1464 if (strncmp(dso
->name
, "/tmp/perf-", 10) == 0) {
1467 if (lstat(dso
->name
, &st
) < 0)
1470 if (!symbol_conf
.force
&& st
.st_uid
&& (st
.st_uid
!= geteuid())) {
1471 pr_warning("File %s not owned by current user or root, "
1472 "ignoring it (use -f to override).\n", dso
->name
);
1476 ret
= dso__load_perf_map(dso
, map
);
1477 dso
->symtab_type
= ret
> 0 ? DSO_BINARY_TYPE__JAVA_JIT
:
1478 DSO_BINARY_TYPE__NOT_FOUND
;
1483 root_dir
= machine
->root_dir
;
1485 name
= malloc(PATH_MAX
);
1489 kmod
= dso
->symtab_type
== DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE
||
1490 dso
->symtab_type
== DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP
||
1491 dso
->symtab_type
== DSO_BINARY_TYPE__GUEST_KMODULE
||
1492 dso
->symtab_type
== DSO_BINARY_TYPE__GUEST_KMODULE_COMP
;
1496 * Read the build id if possible. This is required for
1497 * DSO_BINARY_TYPE__BUILDID_DEBUGINFO to work
1499 if (!dso
->has_build_id
&&
1500 is_regular_file(dso
->long_name
)) {
1501 __symbol__join_symfs(name
, PATH_MAX
, dso
->long_name
);
1502 if (filename__read_build_id(name
, build_id
, BUILD_ID_SIZE
) > 0)
1503 dso__set_build_id(dso
, build_id
);
1507 * Iterate over candidate debug images.
1508 * Keep track of "interesting" ones (those which have a symtab, dynsym,
1509 * and/or opd section) for processing.
1511 for (i
= 0; i
< DSO_BINARY_TYPE__SYMTAB_CNT
; i
++) {
1512 struct symsrc
*ss
= &ss_
[ss_pos
];
1513 bool next_slot
= false;
1515 enum dso_binary_type symtab_type
= binary_type_symtab
[i
];
1517 if (!dso__is_compatible_symtab_type(dso
, kmod
, symtab_type
))
1520 if (dso__read_binary_type_filename(dso
, symtab_type
,
1521 root_dir
, name
, PATH_MAX
))
1524 if (!is_regular_file(name
))
1527 /* Name is now the name of the next image to try */
1528 if (symsrc__init(ss
, dso
, name
, symtab_type
) < 0)
1531 if (!syms_ss
&& symsrc__has_symtab(ss
)) {
1534 if (!dso
->symsrc_filename
)
1535 dso
->symsrc_filename
= strdup(name
);
1538 if (!runtime_ss
&& symsrc__possibly_runtime(ss
)) {
1546 if (syms_ss
&& runtime_ss
)
1549 symsrc__destroy(ss
);
1554 if (!runtime_ss
&& !syms_ss
)
1557 if (runtime_ss
&& !syms_ss
) {
1558 syms_ss
= runtime_ss
;
1561 /* We'll have to hope for the best */
1562 if (!runtime_ss
&& syms_ss
)
1563 runtime_ss
= syms_ss
;
1566 ret
= dso__load_sym(dso
, map
, syms_ss
, runtime_ss
, kmod
);
1573 nr_plt
= dso__synthesize_plt_symbols(dso
, runtime_ss
, map
);
1578 for (; ss_pos
> 0; ss_pos
--)
1579 symsrc__destroy(&ss_
[ss_pos
- 1]);
1582 if (ret
< 0 && strstr(dso
->name
, " (deleted)") != NULL
)
1585 dso__set_loaded(dso
, map
->type
);
1586 pthread_mutex_unlock(&dso
->lock
);
1591 struct map
*map_groups__find_by_name(struct map_groups
*mg
,
1592 enum map_type type
, const char *name
)
1594 struct maps
*maps
= &mg
->maps
[type
];
1597 pthread_rwlock_rdlock(&maps
->lock
);
1599 for (map
= maps__first(maps
); map
; map
= map__next(map
)) {
1600 if (map
->dso
&& strcmp(map
->dso
->short_name
, name
) == 0)
1607 pthread_rwlock_unlock(&maps
->lock
);
1611 int dso__load_vmlinux(struct dso
*dso
, struct map
*map
,
1612 const char *vmlinux
, bool vmlinux_allocated
)
1616 char symfs_vmlinux
[PATH_MAX
];
1617 enum dso_binary_type symtab_type
;
1619 if (vmlinux
[0] == '/')
1620 snprintf(symfs_vmlinux
, sizeof(symfs_vmlinux
), "%s", vmlinux
);
1622 symbol__join_symfs(symfs_vmlinux
, vmlinux
);
1624 if (dso
->kernel
== DSO_TYPE_GUEST_KERNEL
)
1625 symtab_type
= DSO_BINARY_TYPE__GUEST_VMLINUX
;
1627 symtab_type
= DSO_BINARY_TYPE__VMLINUX
;
1629 if (symsrc__init(&ss
, dso
, symfs_vmlinux
, symtab_type
))
1632 err
= dso__load_sym(dso
, map
, &ss
, &ss
, 0);
1633 symsrc__destroy(&ss
);
1636 if (dso
->kernel
== DSO_TYPE_GUEST_KERNEL
)
1637 dso
->binary_type
= DSO_BINARY_TYPE__GUEST_VMLINUX
;
1639 dso
->binary_type
= DSO_BINARY_TYPE__VMLINUX
;
1640 dso__set_long_name(dso
, vmlinux
, vmlinux_allocated
);
1641 dso__set_loaded(dso
, map
->type
);
1642 pr_debug("Using %s for symbols\n", symfs_vmlinux
);
1648 int dso__load_vmlinux_path(struct dso
*dso
, struct map
*map
)
1651 char *filename
= NULL
;
1653 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1654 vmlinux_path__nr_entries
+ 1);
1656 for (i
= 0; i
< vmlinux_path__nr_entries
; ++i
) {
1657 err
= dso__load_vmlinux(dso
, map
, vmlinux_path
[i
], false);
1662 if (!symbol_conf
.ignore_vmlinux_buildid
)
1663 filename
= dso__build_id_filename(dso
, NULL
, 0);
1664 if (filename
!= NULL
) {
1665 err
= dso__load_vmlinux(dso
, map
, filename
, true);
1674 static bool visible_dir_filter(const char *name
, struct dirent
*d
)
1676 if (d
->d_type
!= DT_DIR
)
1678 return lsdir_no_dot_filter(name
, d
);
1681 static int find_matching_kcore(struct map
*map
, char *dir
, size_t dir_sz
)
1683 char kallsyms_filename
[PATH_MAX
];
1685 struct strlist
*dirs
;
1686 struct str_node
*nd
;
1688 dirs
= lsdir(dir
, visible_dir_filter
);
1692 strlist__for_each_entry(nd
, dirs
) {
1693 scnprintf(kallsyms_filename
, sizeof(kallsyms_filename
),
1694 "%s/%s/kallsyms", dir
, nd
->s
);
1695 if (!validate_kcore_addresses(kallsyms_filename
, map
)) {
1696 strlcpy(dir
, kallsyms_filename
, dir_sz
);
1702 strlist__delete(dirs
);
1708 * Use open(O_RDONLY) to check readability directly instead of access(R_OK)
1709 * since access(R_OK) only checks with real UID/GID but open() use effective
1710 * UID/GID and actual capabilities (e.g. /proc/kcore requires CAP_SYS_RAWIO).
1712 static bool filename__readable(const char *file
)
1714 int fd
= open(file
, O_RDONLY
);
1721 static char *dso__find_kallsyms(struct dso
*dso
, struct map
*map
)
1723 u8 host_build_id
[BUILD_ID_SIZE
];
1724 char sbuild_id
[SBUILD_ID_SIZE
];
1725 bool is_host
= false;
1726 char path
[PATH_MAX
];
1728 if (!dso
->has_build_id
) {
1730 * Last resort, if we don't have a build-id and couldn't find
1731 * any vmlinux file, try the running kernel kallsyms table.
1736 if (sysfs__read_build_id("/sys/kernel/notes", host_build_id
,
1737 sizeof(host_build_id
)) == 0)
1738 is_host
= dso__build_id_equal(dso
, host_build_id
);
1740 /* Try a fast path for /proc/kallsyms if possible */
1743 * Do not check the build-id cache, unless we know we cannot use
1744 * /proc/kcore or module maps don't match to /proc/kallsyms.
1745 * To check readability of /proc/kcore, do not use access(R_OK)
1746 * since /proc/kcore requires CAP_SYS_RAWIO to read and access
1749 if (filename__readable("/proc/kcore") &&
1750 !validate_kcore_addresses("/proc/kallsyms", map
))
1754 build_id__sprintf(dso
->build_id
, sizeof(dso
->build_id
), sbuild_id
);
1756 /* Find kallsyms in build-id cache with kcore */
1757 scnprintf(path
, sizeof(path
), "%s/%s/%s",
1758 buildid_dir
, DSO__NAME_KCORE
, sbuild_id
);
1760 if (!find_matching_kcore(map
, path
, sizeof(path
)))
1761 return strdup(path
);
1763 /* Use current /proc/kallsyms if possible */
1766 return strdup("/proc/kallsyms");
1769 /* Finally, find a cache of kallsyms */
1770 if (!build_id_cache__kallsyms_path(sbuild_id
, path
, sizeof(path
))) {
1771 pr_err("No kallsyms or vmlinux with build-id %s was found\n",
1776 return strdup(path
);
1779 static int dso__load_kernel_sym(struct dso
*dso
, struct map
*map
)
1782 const char *kallsyms_filename
= NULL
;
1783 char *kallsyms_allocated_filename
= NULL
;
1785 * Step 1: if the user specified a kallsyms or vmlinux filename, use
1786 * it and only it, reporting errors to the user if it cannot be used.
1788 * For instance, try to analyse an ARM perf.data file _without_ a
1789 * build-id, or if the user specifies the wrong path to the right
1790 * vmlinux file, obviously we can't fallback to another vmlinux (a
1791 * x86_86 one, on the machine where analysis is being performed, say),
1792 * or worse, /proc/kallsyms.
1794 * If the specified file _has_ a build-id and there is a build-id
1795 * section in the perf.data file, we will still do the expected
1796 * validation in dso__load_vmlinux and will bail out if they don't
1799 if (symbol_conf
.kallsyms_name
!= NULL
) {
1800 kallsyms_filename
= symbol_conf
.kallsyms_name
;
1804 if (!symbol_conf
.ignore_vmlinux
&& symbol_conf
.vmlinux_name
!= NULL
) {
1805 return dso__load_vmlinux(dso
, map
, symbol_conf
.vmlinux_name
, false);
1808 if (!symbol_conf
.ignore_vmlinux
&& vmlinux_path
!= NULL
) {
1809 err
= dso__load_vmlinux_path(dso
, map
);
1814 /* do not try local files if a symfs was given */
1815 if (symbol_conf
.symfs
[0] != 0)
1818 kallsyms_allocated_filename
= dso__find_kallsyms(dso
, map
);
1819 if (!kallsyms_allocated_filename
)
1822 kallsyms_filename
= kallsyms_allocated_filename
;
1825 err
= dso__load_kallsyms(dso
, kallsyms_filename
, map
);
1827 pr_debug("Using %s for symbols\n", kallsyms_filename
);
1828 free(kallsyms_allocated_filename
);
1830 if (err
> 0 && !dso__is_kcore(dso
)) {
1831 dso
->binary_type
= DSO_BINARY_TYPE__KALLSYMS
;
1832 dso__set_long_name(dso
, DSO__NAME_KALLSYMS
, false);
1833 map__fixup_start(map
);
1834 map__fixup_end(map
);
1840 static int dso__load_guest_kernel_sym(struct dso
*dso
, struct map
*map
)
1843 const char *kallsyms_filename
= NULL
;
1844 struct machine
*machine
;
1845 char path
[PATH_MAX
];
1848 pr_debug("Guest kernel map hasn't the point to groups\n");
1851 machine
= map
->groups
->machine
;
1853 if (machine__is_default_guest(machine
)) {
1855 * if the user specified a vmlinux filename, use it and only
1856 * it, reporting errors to the user if it cannot be used.
1857 * Or use file guest_kallsyms inputted by user on commandline
1859 if (symbol_conf
.default_guest_vmlinux_name
!= NULL
) {
1860 err
= dso__load_vmlinux(dso
, map
,
1861 symbol_conf
.default_guest_vmlinux_name
,
1866 kallsyms_filename
= symbol_conf
.default_guest_kallsyms
;
1867 if (!kallsyms_filename
)
1870 sprintf(path
, "%s/proc/kallsyms", machine
->root_dir
);
1871 kallsyms_filename
= path
;
1874 err
= dso__load_kallsyms(dso
, kallsyms_filename
, map
);
1876 pr_debug("Using %s for symbols\n", kallsyms_filename
);
1877 if (err
> 0 && !dso__is_kcore(dso
)) {
1878 dso
->binary_type
= DSO_BINARY_TYPE__GUEST_KALLSYMS
;
1879 machine__mmap_name(machine
, path
, sizeof(path
));
1880 dso__set_long_name(dso
, strdup(path
), true);
1881 map__fixup_start(map
);
1882 map__fixup_end(map
);
1888 static void vmlinux_path__exit(void)
1890 while (--vmlinux_path__nr_entries
>= 0)
1891 zfree(&vmlinux_path
[vmlinux_path__nr_entries
]);
1892 vmlinux_path__nr_entries
= 0;
1894 zfree(&vmlinux_path
);
1897 static const char * const vmlinux_paths
[] = {
1902 static const char * const vmlinux_paths_upd
[] = {
1904 "/usr/lib/debug/boot/vmlinux-%s",
1905 "/lib/modules/%s/build/vmlinux",
1906 "/usr/lib/debug/lib/modules/%s/vmlinux",
1907 "/usr/lib/debug/boot/vmlinux-%s.debug"
1910 static int vmlinux_path__add(const char *new_entry
)
1912 vmlinux_path
[vmlinux_path__nr_entries
] = strdup(new_entry
);
1913 if (vmlinux_path
[vmlinux_path__nr_entries
] == NULL
)
1915 ++vmlinux_path__nr_entries
;
1920 static int vmlinux_path__init(struct perf_env
*env
)
1924 char *kernel_version
;
1927 vmlinux_path
= malloc(sizeof(char *) * (ARRAY_SIZE(vmlinux_paths
) +
1928 ARRAY_SIZE(vmlinux_paths_upd
)));
1929 if (vmlinux_path
== NULL
)
1932 for (i
= 0; i
< ARRAY_SIZE(vmlinux_paths
); i
++)
1933 if (vmlinux_path__add(vmlinux_paths
[i
]) < 0)
1936 /* only try kernel version if no symfs was given */
1937 if (symbol_conf
.symfs
[0] != 0)
1941 kernel_version
= env
->os_release
;
1943 if (uname(&uts
) < 0)
1946 kernel_version
= uts
.release
;
1949 for (i
= 0; i
< ARRAY_SIZE(vmlinux_paths_upd
); i
++) {
1950 snprintf(bf
, sizeof(bf
), vmlinux_paths_upd
[i
], kernel_version
);
1951 if (vmlinux_path__add(bf
) < 0)
1958 vmlinux_path__exit();
1962 int setup_list(struct strlist
**list
, const char *list_str
,
1963 const char *list_name
)
1965 if (list_str
== NULL
)
1968 *list
= strlist__new(list_str
, NULL
);
1970 pr_err("problems parsing %s list\n", list_name
);
1974 symbol_conf
.has_filter
= true;
1978 int setup_intlist(struct intlist
**list
, const char *list_str
,
1979 const char *list_name
)
1981 if (list_str
== NULL
)
1984 *list
= intlist__new(list_str
);
1986 pr_err("problems parsing %s list\n", list_name
);
1992 static bool symbol__read_kptr_restrict(void)
1995 FILE *fp
= fopen("/proc/sys/kernel/kptr_restrict", "r");
2000 if (fgets(line
, sizeof(line
), fp
) != NULL
)
2001 value
= ((geteuid() != 0) || (getuid() != 0)) ?
2011 int symbol__annotation_init(void)
2013 if (symbol_conf
.initialized
) {
2014 pr_err("Annotation needs to be init before symbol__init()\n");
2018 if (symbol_conf
.init_annotation
) {
2019 pr_warning("Annotation being initialized multiple times\n");
2023 symbol_conf
.priv_size
+= sizeof(struct annotation
);
2024 symbol_conf
.init_annotation
= true;
2028 int symbol__init(struct perf_env
*env
)
2032 if (symbol_conf
.initialized
)
2035 symbol_conf
.priv_size
= PERF_ALIGN(symbol_conf
.priv_size
, sizeof(u64
));
2039 if (symbol_conf
.sort_by_name
)
2040 symbol_conf
.priv_size
+= (sizeof(struct symbol_name_rb_node
) -
2041 sizeof(struct symbol
));
2043 if (symbol_conf
.try_vmlinux_path
&& vmlinux_path__init(env
) < 0)
2046 if (symbol_conf
.field_sep
&& *symbol_conf
.field_sep
== '.') {
2047 pr_err("'.' is the only non valid --field-separator argument\n");
2051 if (setup_list(&symbol_conf
.dso_list
,
2052 symbol_conf
.dso_list_str
, "dso") < 0)
2055 if (setup_list(&symbol_conf
.comm_list
,
2056 symbol_conf
.comm_list_str
, "comm") < 0)
2057 goto out_free_dso_list
;
2059 if (setup_intlist(&symbol_conf
.pid_list
,
2060 symbol_conf
.pid_list_str
, "pid") < 0)
2061 goto out_free_comm_list
;
2063 if (setup_intlist(&symbol_conf
.tid_list
,
2064 symbol_conf
.tid_list_str
, "tid") < 0)
2065 goto out_free_pid_list
;
2067 if (setup_list(&symbol_conf
.sym_list
,
2068 symbol_conf
.sym_list_str
, "symbol") < 0)
2069 goto out_free_tid_list
;
2071 if (setup_list(&symbol_conf
.bt_stop_list
,
2072 symbol_conf
.bt_stop_list_str
, "symbol") < 0)
2073 goto out_free_sym_list
;
2076 * A path to symbols of "/" is identical to ""
2077 * reset here for simplicity.
2079 symfs
= realpath(symbol_conf
.symfs
, NULL
);
2081 symfs
= symbol_conf
.symfs
;
2082 if (strcmp(symfs
, "/") == 0)
2083 symbol_conf
.symfs
= "";
2084 if (symfs
!= symbol_conf
.symfs
)
2085 free((void *)symfs
);
2087 symbol_conf
.kptr_restrict
= symbol__read_kptr_restrict();
2089 symbol_conf
.initialized
= true;
2093 strlist__delete(symbol_conf
.sym_list
);
2095 intlist__delete(symbol_conf
.tid_list
);
2097 intlist__delete(symbol_conf
.pid_list
);
2099 strlist__delete(symbol_conf
.comm_list
);
2101 strlist__delete(symbol_conf
.dso_list
);
2105 void symbol__exit(void)
2107 if (!symbol_conf
.initialized
)
2109 strlist__delete(symbol_conf
.bt_stop_list
);
2110 strlist__delete(symbol_conf
.sym_list
);
2111 strlist__delete(symbol_conf
.dso_list
);
2112 strlist__delete(symbol_conf
.comm_list
);
2113 intlist__delete(symbol_conf
.tid_list
);
2114 intlist__delete(symbol_conf
.pid_list
);
2115 vmlinux_path__exit();
2116 symbol_conf
.sym_list
= symbol_conf
.dso_list
= symbol_conf
.comm_list
= NULL
;
2117 symbol_conf
.bt_stop_list
= NULL
;
2118 symbol_conf
.initialized
= false;
2121 int symbol__config_symfs(const struct option
*opt __maybe_unused
,
2122 const char *dir
, int unset __maybe_unused
)
2127 symbol_conf
.symfs
= strdup(dir
);
2128 if (symbol_conf
.symfs
== NULL
)
2131 /* skip the locally configured cache if a symfs is given, and
2132 * config buildid dir to symfs/.debug
2134 ret
= asprintf(&bf
, "%s/%s", dir
, ".debug");
2138 set_buildid_dir(bf
);