21 #include <sys/utsname.h>
24 #define KSYM_NAME_LEN 256
27 static int dso__load_kernel_sym(struct dso
*dso
, struct map
*map
,
28 symbol_filter_t filter
);
29 static int dso__load_guest_kernel_sym(struct dso
*dso
, struct map
*map
,
30 symbol_filter_t filter
);
31 int vmlinux_path__nr_entries
;
34 struct symbol_conf symbol_conf
= {
35 .exclude_other
= true,
37 .try_vmlinux_path
= true,
43 static enum dso_binary_type binary_type_symtab
[] = {
44 DSO_BINARY_TYPE__KALLSYMS
,
45 DSO_BINARY_TYPE__GUEST_KALLSYMS
,
46 DSO_BINARY_TYPE__JAVA_JIT
,
47 DSO_BINARY_TYPE__DEBUGLINK
,
48 DSO_BINARY_TYPE__BUILD_ID_CACHE
,
49 DSO_BINARY_TYPE__FEDORA_DEBUGINFO
,
50 DSO_BINARY_TYPE__UBUNTU_DEBUGINFO
,
51 DSO_BINARY_TYPE__BUILDID_DEBUGINFO
,
52 DSO_BINARY_TYPE__SYSTEM_PATH_DSO
,
53 DSO_BINARY_TYPE__GUEST_KMODULE
,
54 DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE
,
55 DSO_BINARY_TYPE__NOT_FOUND
,
58 #define DSO_BINARY_TYPE__SYMTAB_CNT ARRAY_SIZE(binary_type_symtab)
60 bool symbol_type__is_a(char symbol_type
, enum map_type map_type
)
62 symbol_type
= toupper(symbol_type
);
66 return symbol_type
== 'T' || symbol_type
== 'W';
68 return symbol_type
== 'D';
74 static int prefix_underscores_count(const char *str
)
76 const char *tail
= str
;
87 static int choose_best_symbol(struct symbol
*syma
, struct symbol
*symb
)
92 /* Prefer a symbol with non zero length */
93 a
= syma
->end
- syma
->start
;
94 b
= symb
->end
- symb
->start
;
95 if ((b
== 0) && (a
> 0))
97 else if ((a
== 0) && (b
> 0))
100 /* Prefer a non weak symbol over a weak one */
101 a
= syma
->binding
== STB_WEAK
;
102 b
= symb
->binding
== STB_WEAK
;
108 /* Prefer a global symbol over a non global one */
109 a
= syma
->binding
== STB_GLOBAL
;
110 b
= symb
->binding
== STB_GLOBAL
;
116 /* Prefer a symbol with less underscores */
117 a
= prefix_underscores_count(syma
->name
);
118 b
= prefix_underscores_count(symb
->name
);
124 /* If all else fails, choose the symbol with the longest name */
125 if (strlen(syma
->name
) >= strlen(symb
->name
))
131 void symbols__fixup_duplicate(struct rb_root
*symbols
)
134 struct symbol
*curr
, *next
;
136 nd
= rb_first(symbols
);
139 curr
= rb_entry(nd
, struct symbol
, rb_node
);
141 nd
= rb_next(&curr
->rb_node
);
142 next
= rb_entry(nd
, struct symbol
, rb_node
);
147 if (curr
->start
!= next
->start
)
150 if (choose_best_symbol(curr
, next
) == SYMBOL_A
) {
151 rb_erase(&next
->rb_node
, symbols
);
154 nd
= rb_next(&curr
->rb_node
);
155 rb_erase(&curr
->rb_node
, symbols
);
160 void symbols__fixup_end(struct rb_root
*symbols
)
162 struct rb_node
*nd
, *prevnd
= rb_first(symbols
);
163 struct symbol
*curr
, *prev
;
168 curr
= rb_entry(prevnd
, struct symbol
, rb_node
);
170 for (nd
= rb_next(prevnd
); nd
; nd
= rb_next(nd
)) {
172 curr
= rb_entry(nd
, struct symbol
, rb_node
);
174 if (prev
->end
== prev
->start
&& prev
->end
!= curr
->start
)
175 prev
->end
= curr
->start
- 1;
179 if (curr
->end
== curr
->start
)
180 curr
->end
= roundup(curr
->start
, 4096);
183 void __map_groups__fixup_end(struct map_groups
*mg
, enum map_type type
)
185 struct map
*prev
, *curr
;
186 struct rb_node
*nd
, *prevnd
= rb_first(&mg
->maps
[type
]);
191 curr
= rb_entry(prevnd
, struct map
, rb_node
);
193 for (nd
= rb_next(prevnd
); nd
; nd
= rb_next(nd
)) {
195 curr
= rb_entry(nd
, struct map
, rb_node
);
196 prev
->end
= curr
->start
- 1;
200 * We still haven't the actual symbols, so guess the
201 * last map final address.
206 struct symbol
*symbol__new(u64 start
, u64 len
, u8 binding
, const char *name
)
208 size_t namelen
= strlen(name
) + 1;
209 struct symbol
*sym
= calloc(1, (symbol_conf
.priv_size
+
210 sizeof(*sym
) + namelen
));
214 if (symbol_conf
.priv_size
)
215 sym
= ((void *)sym
) + symbol_conf
.priv_size
;
218 sym
->end
= len
? start
+ len
- 1 : start
;
219 sym
->binding
= binding
;
220 sym
->namelen
= namelen
- 1;
222 pr_debug4("%s: %s %#" PRIx64
"-%#" PRIx64
"\n",
223 __func__
, name
, start
, sym
->end
);
224 memcpy(sym
->name
, name
, namelen
);
229 void symbol__delete(struct symbol
*sym
)
231 free(((void *)sym
) - symbol_conf
.priv_size
);
234 size_t symbol__fprintf(struct symbol
*sym
, FILE *fp
)
236 return fprintf(fp
, " %" PRIx64
"-%" PRIx64
" %c %s\n",
237 sym
->start
, sym
->end
,
238 sym
->binding
== STB_GLOBAL
? 'g' :
239 sym
->binding
== STB_LOCAL
? 'l' : 'w',
243 size_t symbol__fprintf_symname_offs(const struct symbol
*sym
,
244 const struct addr_location
*al
, FILE *fp
)
246 unsigned long offset
;
249 if (sym
&& sym
->name
) {
250 length
= fprintf(fp
, "%s", sym
->name
);
252 offset
= al
->addr
- sym
->start
;
253 length
+= fprintf(fp
, "+0x%lx", offset
);
257 return fprintf(fp
, "[unknown]");
260 size_t symbol__fprintf_symname(const struct symbol
*sym
, FILE *fp
)
262 return symbol__fprintf_symname_offs(sym
, NULL
, fp
);
265 void symbols__delete(struct rb_root
*symbols
)
268 struct rb_node
*next
= rb_first(symbols
);
271 pos
= rb_entry(next
, struct symbol
, rb_node
);
272 next
= rb_next(&pos
->rb_node
);
273 rb_erase(&pos
->rb_node
, symbols
);
278 void symbols__insert(struct rb_root
*symbols
, struct symbol
*sym
)
280 struct rb_node
**p
= &symbols
->rb_node
;
281 struct rb_node
*parent
= NULL
;
282 const u64 ip
= sym
->start
;
287 s
= rb_entry(parent
, struct symbol
, rb_node
);
293 rb_link_node(&sym
->rb_node
, parent
, p
);
294 rb_insert_color(&sym
->rb_node
, symbols
);
297 static struct symbol
*symbols__find(struct rb_root
*symbols
, u64 ip
)
304 n
= symbols
->rb_node
;
307 struct symbol
*s
= rb_entry(n
, struct symbol
, rb_node
);
311 else if (ip
> s
->end
)
320 struct symbol_name_rb_node
{
321 struct rb_node rb_node
;
325 static void symbols__insert_by_name(struct rb_root
*symbols
, struct symbol
*sym
)
327 struct rb_node
**p
= &symbols
->rb_node
;
328 struct rb_node
*parent
= NULL
;
329 struct symbol_name_rb_node
*symn
, *s
;
331 symn
= container_of(sym
, struct symbol_name_rb_node
, sym
);
335 s
= rb_entry(parent
, struct symbol_name_rb_node
, rb_node
);
336 if (strcmp(sym
->name
, s
->sym
.name
) < 0)
341 rb_link_node(&symn
->rb_node
, parent
, p
);
342 rb_insert_color(&symn
->rb_node
, symbols
);
345 static void symbols__sort_by_name(struct rb_root
*symbols
,
346 struct rb_root
*source
)
350 for (nd
= rb_first(source
); nd
; nd
= rb_next(nd
)) {
351 struct symbol
*pos
= rb_entry(nd
, struct symbol
, rb_node
);
352 symbols__insert_by_name(symbols
, pos
);
356 static struct symbol
*symbols__find_by_name(struct rb_root
*symbols
,
364 n
= symbols
->rb_node
;
367 struct symbol_name_rb_node
*s
;
370 s
= rb_entry(n
, struct symbol_name_rb_node
, rb_node
);
371 cmp
= strcmp(name
, s
->sym
.name
);
384 struct symbol
*dso__find_symbol(struct dso
*dso
,
385 enum map_type type
, u64 addr
)
387 return symbols__find(&dso
->symbols
[type
], addr
);
390 struct symbol
*dso__find_symbol_by_name(struct dso
*dso
, enum map_type type
,
393 return symbols__find_by_name(&dso
->symbol_names
[type
], name
);
396 void dso__sort_by_name(struct dso
*dso
, enum map_type type
)
398 dso__set_sorted_by_name(dso
, type
);
399 return symbols__sort_by_name(&dso
->symbol_names
[type
],
400 &dso
->symbols
[type
]);
403 size_t dso__fprintf_symbols_by_name(struct dso
*dso
,
404 enum map_type type
, FILE *fp
)
408 struct symbol_name_rb_node
*pos
;
410 for (nd
= rb_first(&dso
->symbol_names
[type
]); nd
; nd
= rb_next(nd
)) {
411 pos
= rb_entry(nd
, struct symbol_name_rb_node
, rb_node
);
412 fprintf(fp
, "%s\n", pos
->sym
.name
);
418 int kallsyms__parse(const char *filename
, void *arg
,
419 int (*process_symbol
)(void *arg
, const char *name
,
420 char type
, u64 start
))
425 FILE *file
= fopen(filename
, "r");
432 while (!feof(file
)) {
438 line_len
= getline(&line
, &n
, file
);
439 if (line_len
< 0 || !line
)
442 line
[--line_len
] = '\0'; /* \n */
444 len
= hex2u64(line
, &start
);
447 if (len
+ 2 >= line_len
)
450 symbol_type
= line
[len
];
452 symbol_name
= line
+ len
;
453 len
= line_len
- len
;
455 if (len
>= KSYM_NAME_LEN
) {
460 err
= process_symbol(arg
, symbol_name
,
474 struct process_kallsyms_args
{
479 static u8
kallsyms2elf_type(char type
)
484 return isupper(type
) ? STB_GLOBAL
: STB_LOCAL
;
487 static int map__process_kallsym_symbol(void *arg
, const char *name
,
488 char type
, u64 start
)
491 struct process_kallsyms_args
*a
= arg
;
492 struct rb_root
*root
= &a
->dso
->symbols
[a
->map
->type
];
494 if (!symbol_type__is_a(type
, a
->map
->type
))
498 * module symbols are not sorted so we add all
499 * symbols, setting length to 0, and rely on
500 * symbols__fixup_end() to fix it up.
502 sym
= symbol__new(start
, 0, kallsyms2elf_type(type
), name
);
506 * We will pass the symbols to the filter later, in
507 * map__split_kallsyms, when we have split the maps per module
509 symbols__insert(root
, sym
);
515 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
516 * so that we can in the next step set the symbol ->end address and then
517 * call kernel_maps__split_kallsyms.
519 static int dso__load_all_kallsyms(struct dso
*dso
, const char *filename
,
522 struct process_kallsyms_args args
= { .map
= map
, .dso
= dso
, };
523 return kallsyms__parse(filename
, &args
, map__process_kallsym_symbol
);
527 * Split the symbols into maps, making sure there are no overlaps, i.e. the
528 * kernel range is broken in several maps, named [kernel].N, as we don't have
529 * the original ELF section names vmlinux have.
531 static int dso__split_kallsyms(struct dso
*dso
, struct map
*map
,
532 symbol_filter_t filter
)
534 struct map_groups
*kmaps
= map__kmap(map
)->kmaps
;
535 struct machine
*machine
= kmaps
->machine
;
536 struct map
*curr_map
= map
;
538 int count
= 0, moved
= 0;
539 struct rb_root
*root
= &dso
->symbols
[map
->type
];
540 struct rb_node
*next
= rb_first(root
);
541 int kernel_range
= 0;
546 pos
= rb_entry(next
, struct symbol
, rb_node
);
547 next
= rb_next(&pos
->rb_node
);
549 module
= strchr(pos
->name
, '\t');
551 if (!symbol_conf
.use_modules
)
556 if (strcmp(curr_map
->dso
->short_name
, module
)) {
557 if (curr_map
!= map
&&
558 dso
->kernel
== DSO_TYPE_GUEST_KERNEL
&&
559 machine__is_default_guest(machine
)) {
561 * We assume all symbols of a module are
562 * continuous in * kallsyms, so curr_map
563 * points to a module and all its
564 * symbols are in its kmap. Mark it as
567 dso__set_loaded(curr_map
->dso
,
571 curr_map
= map_groups__find_by_name(kmaps
,
573 if (curr_map
== NULL
) {
574 pr_debug("%s/proc/{kallsyms,modules} "
575 "inconsistency while looking "
576 "for \"%s\" module!\n",
577 machine
->root_dir
, module
);
582 if (curr_map
->dso
->loaded
&&
583 !machine__is_default_guest(machine
))
587 * So that we look just like we get from .ko files,
588 * i.e. not prelinked, relative to map->start.
590 pos
->start
= curr_map
->map_ip(curr_map
, pos
->start
);
591 pos
->end
= curr_map
->map_ip(curr_map
, pos
->end
);
592 } else if (curr_map
!= map
) {
593 char dso_name
[PATH_MAX
];
601 if (dso
->kernel
== DSO_TYPE_GUEST_KERNEL
)
602 snprintf(dso_name
, sizeof(dso_name
),
606 snprintf(dso_name
, sizeof(dso_name
),
610 ndso
= dso__new(dso_name
);
614 ndso
->kernel
= dso
->kernel
;
616 curr_map
= map__new2(pos
->start
, ndso
, map
->type
);
617 if (curr_map
== NULL
) {
622 curr_map
->map_ip
= curr_map
->unmap_ip
= identity__map_ip
;
623 map_groups__insert(kmaps
, curr_map
);
627 if (filter
&& filter(curr_map
, pos
)) {
628 discard_symbol
: rb_erase(&pos
->rb_node
, root
);
631 if (curr_map
!= map
) {
632 rb_erase(&pos
->rb_node
, root
);
633 symbols__insert(&curr_map
->dso
->symbols
[curr_map
->type
], pos
);
640 if (curr_map
!= map
&&
641 dso
->kernel
== DSO_TYPE_GUEST_KERNEL
&&
642 machine__is_default_guest(kmaps
->machine
)) {
643 dso__set_loaded(curr_map
->dso
, curr_map
->type
);
646 return count
+ moved
;
649 bool symbol__restricted_filename(const char *filename
,
650 const char *restricted_filename
)
652 bool restricted
= false;
654 if (symbol_conf
.kptr_restrict
) {
655 char *r
= realpath(filename
, NULL
);
658 restricted
= strcmp(r
, restricted_filename
) == 0;
667 int dso__load_kallsyms(struct dso
*dso
, const char *filename
,
668 struct map
*map
, symbol_filter_t filter
)
670 if (symbol__restricted_filename(filename
, "/proc/kallsyms"))
673 if (dso__load_all_kallsyms(dso
, filename
, map
) < 0)
676 symbols__fixup_end(&dso
->symbols
[map
->type
]);
677 symbols__fixup_duplicate(&dso
->symbols
[map
->type
]);
679 if (dso
->kernel
== DSO_TYPE_GUEST_KERNEL
)
680 dso
->symtab_type
= DSO_BINARY_TYPE__GUEST_KALLSYMS
;
682 dso
->symtab_type
= DSO_BINARY_TYPE__KALLSYMS
;
684 return dso__split_kallsyms(dso
, map
, filter
);
687 static int dso__load_perf_map(struct dso
*dso
, struct map
*map
,
688 symbol_filter_t filter
)
695 file
= fopen(dso
->long_name
, "r");
699 while (!feof(file
)) {
704 line_len
= getline(&line
, &n
, file
);
711 line
[--line_len
] = '\0'; /* \n */
713 len
= hex2u64(line
, &start
);
716 if (len
+ 2 >= line_len
)
719 len
+= hex2u64(line
+ len
, &size
);
722 if (len
+ 2 >= line_len
)
725 sym
= symbol__new(start
, size
, STB_GLOBAL
, line
+ len
);
728 goto out_delete_line
;
730 if (filter
&& filter(map
, sym
))
733 symbols__insert(&dso
->symbols
[map
->type
], sym
);
749 int dso__load(struct dso
*dso
, struct map
*map
, symbol_filter_t filter
)
754 struct machine
*machine
;
755 char *root_dir
= (char *) "";
757 struct symsrc ss_
[2];
758 struct symsrc
*syms_ss
= NULL
, *runtime_ss
= NULL
;
760 dso__set_loaded(dso
, map
->type
);
762 if (dso
->kernel
== DSO_TYPE_KERNEL
)
763 return dso__load_kernel_sym(dso
, map
, filter
);
764 else if (dso
->kernel
== DSO_TYPE_GUEST_KERNEL
)
765 return dso__load_guest_kernel_sym(dso
, map
, filter
);
767 if (map
->groups
&& map
->groups
->machine
)
768 machine
= map
->groups
->machine
;
772 dso
->adjust_symbols
= 0;
774 if (strncmp(dso
->name
, "/tmp/perf-", 10) == 0) {
777 if (lstat(dso
->name
, &st
) < 0)
780 if (st
.st_uid
&& (st
.st_uid
!= geteuid())) {
781 pr_warning("File %s not owned by current user or root, "
782 "ignoring it.\n", dso
->name
);
786 ret
= dso__load_perf_map(dso
, map
, filter
);
787 dso
->symtab_type
= ret
> 0 ? DSO_BINARY_TYPE__JAVA_JIT
:
788 DSO_BINARY_TYPE__NOT_FOUND
;
793 root_dir
= machine
->root_dir
;
795 name
= malloc(PATH_MAX
);
799 /* Iterate over candidate debug images.
800 * Keep track of "interesting" ones (those which have a symtab, dynsym,
801 * and/or opd section) for processing.
803 for (i
= 0; i
< DSO_BINARY_TYPE__SYMTAB_CNT
; i
++) {
804 struct symsrc
*ss
= &ss_
[ss_pos
];
805 bool next_slot
= false;
807 enum dso_binary_type symtab_type
= binary_type_symtab
[i
];
809 if (dso__binary_type_file(dso
, symtab_type
,
810 root_dir
, name
, PATH_MAX
))
813 /* Name is now the name of the next image to try */
814 if (symsrc__init(ss
, dso
, name
, symtab_type
) < 0)
817 if (!syms_ss
&& symsrc__has_symtab(ss
)) {
822 if (!runtime_ss
&& symsrc__possibly_runtime(ss
)) {
830 if (syms_ss
&& runtime_ss
)
836 if (!runtime_ss
&& !syms_ss
)
839 if (runtime_ss
&& !syms_ss
) {
840 syms_ss
= runtime_ss
;
843 /* We'll have to hope for the best */
844 if (!runtime_ss
&& syms_ss
)
845 runtime_ss
= syms_ss
;
848 ret
= dso__load_sym(dso
, map
, syms_ss
, runtime_ss
, filter
, 0);
855 nr_plt
= dso__synthesize_plt_symbols(dso
, runtime_ss
, map
, filter
);
860 for (; ss_pos
> 0; ss_pos
--)
861 symsrc__destroy(&ss_
[ss_pos
- 1]);
864 if (ret
< 0 && strstr(dso
->name
, " (deleted)") != NULL
)
869 struct map
*map_groups__find_by_name(struct map_groups
*mg
,
870 enum map_type type
, const char *name
)
874 for (nd
= rb_first(&mg
->maps
[type
]); nd
; nd
= rb_next(nd
)) {
875 struct map
*map
= rb_entry(nd
, struct map
, rb_node
);
877 if (map
->dso
&& strcmp(map
->dso
->short_name
, name
) == 0)
884 int dso__load_vmlinux(struct dso
*dso
, struct map
*map
,
885 const char *vmlinux
, symbol_filter_t filter
)
889 char symfs_vmlinux
[PATH_MAX
];
890 enum dso_binary_type symtab_type
;
892 snprintf(symfs_vmlinux
, sizeof(symfs_vmlinux
), "%s%s",
893 symbol_conf
.symfs
, vmlinux
);
895 if (dso
->kernel
== DSO_TYPE_GUEST_KERNEL
)
896 symtab_type
= DSO_BINARY_TYPE__GUEST_VMLINUX
;
898 symtab_type
= DSO_BINARY_TYPE__VMLINUX
;
900 if (symsrc__init(&ss
, dso
, symfs_vmlinux
, symtab_type
))
903 err
= dso__load_sym(dso
, map
, &ss
, &ss
, filter
, 0);
904 symsrc__destroy(&ss
);
907 dso__set_long_name(dso
, (char *)vmlinux
);
908 dso__set_loaded(dso
, map
->type
);
909 pr_debug("Using %s for symbols\n", symfs_vmlinux
);
915 int dso__load_vmlinux_path(struct dso
*dso
, struct map
*map
,
916 symbol_filter_t filter
)
921 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
922 vmlinux_path__nr_entries
+ 1);
924 filename
= dso__build_id_filename(dso
, NULL
, 0);
925 if (filename
!= NULL
) {
926 err
= dso__load_vmlinux(dso
, map
, filename
, filter
);
928 dso
->lname_alloc
= 1;
934 for (i
= 0; i
< vmlinux_path__nr_entries
; ++i
) {
935 err
= dso__load_vmlinux(dso
, map
, vmlinux_path
[i
], filter
);
937 dso__set_long_name(dso
, strdup(vmlinux_path
[i
]));
938 dso
->lname_alloc
= 1;
946 static int dso__load_kernel_sym(struct dso
*dso
, struct map
*map
,
947 symbol_filter_t filter
)
950 const char *kallsyms_filename
= NULL
;
951 char *kallsyms_allocated_filename
= NULL
;
953 * Step 1: if the user specified a kallsyms or vmlinux filename, use
954 * it and only it, reporting errors to the user if it cannot be used.
956 * For instance, try to analyse an ARM perf.data file _without_ a
957 * build-id, or if the user specifies the wrong path to the right
958 * vmlinux file, obviously we can't fallback to another vmlinux (a
959 * x86_86 one, on the machine where analysis is being performed, say),
960 * or worse, /proc/kallsyms.
962 * If the specified file _has_ a build-id and there is a build-id
963 * section in the perf.data file, we will still do the expected
964 * validation in dso__load_vmlinux and will bail out if they don't
967 if (symbol_conf
.kallsyms_name
!= NULL
) {
968 kallsyms_filename
= symbol_conf
.kallsyms_name
;
972 if (symbol_conf
.vmlinux_name
!= NULL
) {
973 err
= dso__load_vmlinux(dso
, map
,
974 symbol_conf
.vmlinux_name
, filter
);
976 dso__set_long_name(dso
,
977 strdup(symbol_conf
.vmlinux_name
));
978 dso
->lname_alloc
= 1;
984 if (vmlinux_path
!= NULL
) {
985 err
= dso__load_vmlinux_path(dso
, map
, filter
);
990 /* do not try local files if a symfs was given */
991 if (symbol_conf
.symfs
[0] != 0)
995 * Say the kernel DSO was created when processing the build-id header table,
996 * we have a build-id, so check if it is the same as the running kernel,
999 if (dso
->has_build_id
) {
1000 u8 kallsyms_build_id
[BUILD_ID_SIZE
];
1001 char sbuild_id
[BUILD_ID_SIZE
* 2 + 1];
1003 if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id
,
1004 sizeof(kallsyms_build_id
)) == 0) {
1005 if (dso__build_id_equal(dso
, kallsyms_build_id
)) {
1006 kallsyms_filename
= "/proc/kallsyms";
1011 * Now look if we have it on the build-id cache in
1012 * $HOME/.debug/[kernel.kallsyms].
1014 build_id__sprintf(dso
->build_id
, sizeof(dso
->build_id
),
1017 if (asprintf(&kallsyms_allocated_filename
,
1018 "%s/.debug/[kernel.kallsyms]/%s",
1019 getenv("HOME"), sbuild_id
) == -1) {
1020 pr_err("Not enough memory for kallsyms file lookup\n");
1024 kallsyms_filename
= kallsyms_allocated_filename
;
1026 if (access(kallsyms_filename
, F_OK
)) {
1027 pr_err("No kallsyms or vmlinux with build-id %s "
1028 "was found\n", sbuild_id
);
1029 free(kallsyms_allocated_filename
);
1034 * Last resort, if we don't have a build-id and couldn't find
1035 * any vmlinux file, try the running kernel kallsyms table.
1037 kallsyms_filename
= "/proc/kallsyms";
1041 err
= dso__load_kallsyms(dso
, kallsyms_filename
, map
, filter
);
1043 pr_debug("Using %s for symbols\n", kallsyms_filename
);
1044 free(kallsyms_allocated_filename
);
1047 dso__set_long_name(dso
, strdup("[kernel.kallsyms]"));
1049 map__fixup_start(map
);
1050 map__fixup_end(map
);
1056 static int dso__load_guest_kernel_sym(struct dso
*dso
, struct map
*map
,
1057 symbol_filter_t filter
)
1060 const char *kallsyms_filename
= NULL
;
1061 struct machine
*machine
;
1062 char path
[PATH_MAX
];
1065 pr_debug("Guest kernel map hasn't the point to groups\n");
1068 machine
= map
->groups
->machine
;
1070 if (machine__is_default_guest(machine
)) {
1072 * if the user specified a vmlinux filename, use it and only
1073 * it, reporting errors to the user if it cannot be used.
1074 * Or use file guest_kallsyms inputted by user on commandline
1076 if (symbol_conf
.default_guest_vmlinux_name
!= NULL
) {
1077 err
= dso__load_vmlinux(dso
, map
,
1078 symbol_conf
.default_guest_vmlinux_name
, filter
);
1082 kallsyms_filename
= symbol_conf
.default_guest_kallsyms
;
1083 if (!kallsyms_filename
)
1086 sprintf(path
, "%s/proc/kallsyms", machine
->root_dir
);
1087 kallsyms_filename
= path
;
1090 err
= dso__load_kallsyms(dso
, kallsyms_filename
, map
, filter
);
1092 pr_debug("Using %s for symbols\n", kallsyms_filename
);
1096 if (kallsyms_filename
!= NULL
) {
1097 machine__mmap_name(machine
, path
, sizeof(path
));
1098 dso__set_long_name(dso
, strdup(path
));
1100 map__fixup_start(map
);
1101 map__fixup_end(map
);
1107 static void vmlinux_path__exit(void)
1109 while (--vmlinux_path__nr_entries
>= 0) {
1110 free(vmlinux_path
[vmlinux_path__nr_entries
]);
1111 vmlinux_path
[vmlinux_path__nr_entries
] = NULL
;
1115 vmlinux_path
= NULL
;
1118 static int vmlinux_path__init(void)
1123 vmlinux_path
= malloc(sizeof(char *) * 5);
1124 if (vmlinux_path
== NULL
)
1127 vmlinux_path
[vmlinux_path__nr_entries
] = strdup("vmlinux");
1128 if (vmlinux_path
[vmlinux_path__nr_entries
] == NULL
)
1130 ++vmlinux_path__nr_entries
;
1131 vmlinux_path
[vmlinux_path__nr_entries
] = strdup("/boot/vmlinux");
1132 if (vmlinux_path
[vmlinux_path__nr_entries
] == NULL
)
1134 ++vmlinux_path__nr_entries
;
1136 /* only try running kernel version if no symfs was given */
1137 if (symbol_conf
.symfs
[0] != 0)
1140 if (uname(&uts
) < 0)
1143 snprintf(bf
, sizeof(bf
), "/boot/vmlinux-%s", uts
.release
);
1144 vmlinux_path
[vmlinux_path__nr_entries
] = strdup(bf
);
1145 if (vmlinux_path
[vmlinux_path__nr_entries
] == NULL
)
1147 ++vmlinux_path__nr_entries
;
1148 snprintf(bf
, sizeof(bf
), "/lib/modules/%s/build/vmlinux", uts
.release
);
1149 vmlinux_path
[vmlinux_path__nr_entries
] = strdup(bf
);
1150 if (vmlinux_path
[vmlinux_path__nr_entries
] == NULL
)
1152 ++vmlinux_path__nr_entries
;
1153 snprintf(bf
, sizeof(bf
), "/usr/lib/debug/lib/modules/%s/vmlinux",
1155 vmlinux_path
[vmlinux_path__nr_entries
] = strdup(bf
);
1156 if (vmlinux_path
[vmlinux_path__nr_entries
] == NULL
)
1158 ++vmlinux_path__nr_entries
;
1163 vmlinux_path__exit();
1167 static int setup_list(struct strlist
**list
, const char *list_str
,
1168 const char *list_name
)
1170 if (list_str
== NULL
)
1173 *list
= strlist__new(true, list_str
);
1175 pr_err("problems parsing %s list\n", list_name
);
1181 static bool symbol__read_kptr_restrict(void)
1185 if (geteuid() != 0) {
1186 FILE *fp
= fopen("/proc/sys/kernel/kptr_restrict", "r");
1190 if (fgets(line
, sizeof(line
), fp
) != NULL
)
1191 value
= atoi(line
) != 0;
1200 int symbol__init(void)
1204 if (symbol_conf
.initialized
)
1207 symbol_conf
.priv_size
= PERF_ALIGN(symbol_conf
.priv_size
, sizeof(u64
));
1211 if (symbol_conf
.sort_by_name
)
1212 symbol_conf
.priv_size
+= (sizeof(struct symbol_name_rb_node
) -
1213 sizeof(struct symbol
));
1215 if (symbol_conf
.try_vmlinux_path
&& vmlinux_path__init() < 0)
1218 if (symbol_conf
.field_sep
&& *symbol_conf
.field_sep
== '.') {
1219 pr_err("'.' is the only non valid --field-separator argument\n");
1223 if (setup_list(&symbol_conf
.dso_list
,
1224 symbol_conf
.dso_list_str
, "dso") < 0)
1227 if (setup_list(&symbol_conf
.comm_list
,
1228 symbol_conf
.comm_list_str
, "comm") < 0)
1229 goto out_free_dso_list
;
1231 if (setup_list(&symbol_conf
.sym_list
,
1232 symbol_conf
.sym_list_str
, "symbol") < 0)
1233 goto out_free_comm_list
;
1236 * A path to symbols of "/" is identical to ""
1237 * reset here for simplicity.
1239 symfs
= realpath(symbol_conf
.symfs
, NULL
);
1241 symfs
= symbol_conf
.symfs
;
1242 if (strcmp(symfs
, "/") == 0)
1243 symbol_conf
.symfs
= "";
1244 if (symfs
!= symbol_conf
.symfs
)
1245 free((void *)symfs
);
1247 symbol_conf
.kptr_restrict
= symbol__read_kptr_restrict();
1249 symbol_conf
.initialized
= true;
1253 strlist__delete(symbol_conf
.comm_list
);
1255 strlist__delete(symbol_conf
.dso_list
);
1259 void symbol__exit(void)
1261 if (!symbol_conf
.initialized
)
1263 strlist__delete(symbol_conf
.sym_list
);
1264 strlist__delete(symbol_conf
.dso_list
);
1265 strlist__delete(symbol_conf
.comm_list
);
1266 vmlinux_path__exit();
1267 symbol_conf
.sym_list
= symbol_conf
.dso_list
= symbol_conf
.comm_list
= NULL
;
1268 symbol_conf
.initialized
= false;