22 #include <symbol/kallsyms.h>
23 #include <sys/utsname.h>
25 static int dso__load_kernel_sym(struct dso *dso, struct map *map,
26 symbol_filter_t filter);
27 static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
28 symbol_filter_t filter);
29 int vmlinux_path__nr_entries;
32 struct symbol_conf symbol_conf = {
34 .try_vmlinux_path = true,
37 .cumulate_callchain = true,
38 .show_hist_headers = true,
42 static enum dso_binary_type binary_type_symtab[] = {
43 DSO_BINARY_TYPE__KALLSYMS,
44 DSO_BINARY_TYPE__GUEST_KALLSYMS,
45 DSO_BINARY_TYPE__JAVA_JIT,
46 DSO_BINARY_TYPE__DEBUGLINK,
47 DSO_BINARY_TYPE__BUILD_ID_CACHE,
48 DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
49 DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
50 DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
51 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
52 DSO_BINARY_TYPE__GUEST_KMODULE,
53 DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE,
54 DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO,
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)
93 /* Prefer a symbol with non zero length */
94 a = syma->end - syma->start;
95 b = symb->end - symb->start;
96 if ((b == 0) && (a > 0))
98 else if ((a == 0) && (b > 0))
101 /* Prefer a non weak symbol over a weak one */
102 a = syma->binding == STB_WEAK;
103 b = symb->binding == STB_WEAK;
109 /* Prefer a global symbol over a non global one */
110 a = syma->binding == STB_GLOBAL;
111 b = symb->binding == STB_GLOBAL;
117 /* Prefer a symbol with less underscores */
118 a = prefix_underscores_count(syma->name);
119 b = prefix_underscores_count(symb->name);
125 /* Choose the symbol with the longest name */
126 na = strlen(syma->name);
127 nb = strlen(symb->name);
133 /* Avoid "SyS" kernel syscall aliases */
134 if (na >= 3 && !strncmp(syma->name, "SyS", 3))
136 if (na >= 10 && !strncmp(syma->name, "compat_SyS", 10))
142 void symbols__fixup_duplicate(struct rb_root *symbols)
145 struct symbol *curr, *next;
147 nd = rb_first(symbols);
150 curr = rb_entry(nd, struct symbol, rb_node);
152 nd = rb_next(&curr->rb_node);
153 next = rb_entry(nd, struct symbol, rb_node);
158 if (curr->start != next->start)
161 if (choose_best_symbol(curr, next) == SYMBOL_A) {
162 rb_erase(&next->rb_node, symbols);
163 symbol__delete(next);
166 nd = rb_next(&curr->rb_node);
167 rb_erase(&curr->rb_node, symbols);
168 symbol__delete(curr);
173 void symbols__fixup_end(struct rb_root *symbols)
175 struct rb_node *nd, *prevnd = rb_first(symbols);
176 struct symbol *curr, *prev;
181 curr = rb_entry(prevnd, struct symbol, rb_node);
183 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
185 curr = rb_entry(nd, struct symbol, rb_node);
187 if (prev->end == prev->start && prev->end != curr->start)
188 prev->end = curr->start - 1;
192 if (curr->end == curr->start)
193 curr->end = roundup(curr->start, 4096);
196 void __map_groups__fixup_end(struct map_groups *mg, enum map_type type)
198 struct map *prev, *curr;
199 struct rb_node *nd, *prevnd = rb_first(&mg->maps[type]);
204 curr = rb_entry(prevnd, struct map, rb_node);
206 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
208 curr = rb_entry(nd, struct map, rb_node);
209 prev->end = curr->start - 1;
213 * We still haven't the actual symbols, so guess the
214 * last map final address.
219 struct symbol *symbol__new(u64 start, u64 len, u8 binding, const char *name)
221 size_t namelen = strlen(name) + 1;
222 struct symbol *sym = calloc(1, (symbol_conf.priv_size +
223 sizeof(*sym) + namelen));
227 if (symbol_conf.priv_size)
228 sym = ((void *)sym) + symbol_conf.priv_size;
231 sym->end = len ? start + len - 1 : start;
232 sym->binding = binding;
233 sym->namelen = namelen - 1;
235 pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n",
236 __func__, name, start, sym->end);
237 memcpy(sym->name, name, namelen);
242 void symbol__delete(struct symbol *sym)
244 free(((void *)sym) - symbol_conf.priv_size);
247 size_t symbol__fprintf(struct symbol *sym, FILE *fp)
249 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %c %s\n",
250 sym->start, sym->end,
251 sym->binding == STB_GLOBAL ? 'g' :
252 sym->binding == STB_LOCAL ? 'l' : 'w',
256 size_t symbol__fprintf_symname_offs(const struct symbol *sym,
257 const struct addr_location *al, FILE *fp)
259 unsigned long offset;
262 if (sym && sym->name) {
263 length = fprintf(fp, "%s", sym->name);
265 if (al->addr < sym->end)
266 offset = al->addr - sym->start;
268 offset = al->addr - al->map->start - sym->start;
269 length += fprintf(fp, "+0x%lx", offset);
273 return fprintf(fp, "[unknown]");
276 size_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp)
278 return symbol__fprintf_symname_offs(sym, NULL, fp);
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)
296 struct rb_node **p = &symbols->rb_node;
297 struct rb_node *parent = NULL;
298 const u64 ip = sym->start;
303 s = rb_entry(parent, struct symbol, rb_node);
309 rb_link_node(&sym->rb_node, parent, p);
310 rb_insert_color(&sym->rb_node, symbols);
313 static struct symbol *symbols__find(struct rb_root *symbols, u64 ip)
320 n = symbols->rb_node;
323 struct symbol *s = rb_entry(n, struct symbol, rb_node);
327 else if (ip > s->end)
336 static struct symbol *symbols__first(struct rb_root *symbols)
338 struct rb_node *n = rb_first(symbols);
341 return rb_entry(n, struct symbol, rb_node);
346 static struct symbol *symbols__next(struct symbol *sym)
348 struct rb_node *n = rb_next(&sym->rb_node);
351 return rb_entry(n, struct symbol, rb_node);
356 struct symbol_name_rb_node {
357 struct rb_node rb_node;
361 static void symbols__insert_by_name(struct rb_root *symbols, struct symbol *sym)
363 struct rb_node **p = &symbols->rb_node;
364 struct rb_node *parent = NULL;
365 struct symbol_name_rb_node *symn, *s;
367 symn = container_of(sym, struct symbol_name_rb_node, sym);
371 s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
372 if (strcmp(sym->name, s->sym.name) < 0)
377 rb_link_node(&symn->rb_node, parent, p);
378 rb_insert_color(&symn->rb_node, symbols);
381 static void symbols__sort_by_name(struct rb_root *symbols,
382 struct rb_root *source)
386 for (nd = rb_first(source); nd; nd = rb_next(nd)) {
387 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
388 symbols__insert_by_name(symbols, pos);
392 static struct symbol *symbols__find_by_name(struct rb_root *symbols,
400 n = symbols->rb_node;
403 struct symbol_name_rb_node *s;
406 s = rb_entry(n, struct symbol_name_rb_node, rb_node);
407 cmp = strcmp(name, s->sym.name);
420 struct symbol *dso__find_symbol(struct dso *dso,
421 enum map_type type, u64 addr)
423 return symbols__find(&dso->symbols[type], addr);
426 struct symbol *dso__first_symbol(struct dso *dso, enum map_type type)
428 return symbols__first(&dso->symbols[type]);
431 struct symbol *dso__next_symbol(struct symbol *sym)
433 return symbols__next(sym);
436 struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
439 return symbols__find_by_name(&dso->symbol_names[type], name);
442 void dso__sort_by_name(struct dso *dso, enum map_type type)
444 dso__set_sorted_by_name(dso, type);
445 return symbols__sort_by_name(&dso->symbol_names[type],
446 &dso->symbols[type]);
449 size_t dso__fprintf_symbols_by_name(struct dso *dso,
450 enum map_type type, FILE *fp)
454 struct symbol_name_rb_node *pos;
456 for (nd = rb_first(&dso->symbol_names[type]); nd; nd = rb_next(nd)) {
457 pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
458 fprintf(fp, "%s\n", pos->sym.name);
464 int modules__parse(const char *filename, void *arg,
465 int (*process_module)(void *arg, const char *name,
473 file = fopen(filename, "r");
483 line_len = getline(&line, &n, file);
496 line[--line_len] = '\0'; /* \n */
498 sep = strrchr(line, 'x');
502 hex2u64(sep + 1, &start);
504 sep = strchr(line, ' ');
510 scnprintf(name, sizeof(name), "[%s]", line);
512 err = process_module(arg, name, start);
522 struct process_kallsyms_args {
528 * These are symbols in the kernel image, so make sure that
529 * sym is from a kernel DSO.
531 bool symbol__is_idle(struct symbol *sym)
533 const char * const idle_symbols[] = {
542 "mwait_idle_with_hints",
544 "ppc64_runlatch_off",
545 "pseries_dedicated_idle_sleep",
554 for (i = 0; idle_symbols[i]; i++) {
555 if (!strcmp(idle_symbols[i], sym->name))
562 static int map__process_kallsym_symbol(void *arg, const char *name,
563 char type, u64 start)
566 struct process_kallsyms_args *a = arg;
567 struct rb_root *root = &a->dso->symbols[a->map->type];
569 if (!symbol_type__is_a(type, a->map->type))
573 * module symbols are not sorted so we add all
574 * symbols, setting length to 0, and rely on
575 * symbols__fixup_end() to fix it up.
577 sym = symbol__new(start, 0, kallsyms2elf_type(type), name);
581 * We will pass the symbols to the filter later, in
582 * map__split_kallsyms, when we have split the maps per module
584 symbols__insert(root, sym);
590 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
591 * so that we can in the next step set the symbol ->end address and then
592 * call kernel_maps__split_kallsyms.
594 static int dso__load_all_kallsyms(struct dso *dso, const char *filename,
597 struct process_kallsyms_args args = { .map = map, .dso = dso, };
598 return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
601 static int dso__split_kallsyms_for_kcore(struct dso *dso, struct map *map,
602 symbol_filter_t filter)
604 struct map_groups *kmaps = map__kmap(map)->kmaps;
605 struct map *curr_map;
607 int count = 0, moved = 0;
608 struct rb_root *root = &dso->symbols[map->type];
609 struct rb_node *next = rb_first(root);
614 pos = rb_entry(next, struct symbol, rb_node);
615 next = rb_next(&pos->rb_node);
617 module = strchr(pos->name, '\t');
621 curr_map = map_groups__find(kmaps, map->type, pos->start);
623 if (!curr_map || (filter && filter(curr_map, pos))) {
624 rb_erase(&pos->rb_node, root);
627 pos->start -= curr_map->start - curr_map->pgoff;
629 pos->end -= curr_map->start - curr_map->pgoff;
630 if (curr_map != map) {
631 rb_erase(&pos->rb_node, root);
633 &curr_map->dso->symbols[curr_map->type],
642 /* Symbols have been adjusted */
643 dso->adjust_symbols = 1;
645 return count + moved;
649 * Split the symbols into maps, making sure there are no overlaps, i.e. the
650 * kernel range is broken in several maps, named [kernel].N, as we don't have
651 * the original ELF section names vmlinux have.
653 static int dso__split_kallsyms(struct dso *dso, struct map *map, u64 delta,
654 symbol_filter_t filter)
656 struct map_groups *kmaps = map__kmap(map)->kmaps;
657 struct machine *machine = kmaps->machine;
658 struct map *curr_map = map;
660 int count = 0, moved = 0;
661 struct rb_root *root = &dso->symbols[map->type];
662 struct rb_node *next = rb_first(root);
663 int kernel_range = 0;
668 pos = rb_entry(next, struct symbol, rb_node);
669 next = rb_next(&pos->rb_node);
671 module = strchr(pos->name, '\t');
673 if (!symbol_conf.use_modules)
678 if (strcmp(curr_map->dso->short_name, module)) {
679 if (curr_map != map &&
680 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
681 machine__is_default_guest(machine)) {
683 * We assume all symbols of a module are
684 * continuous in * kallsyms, so curr_map
685 * points to a module and all its
686 * symbols are in its kmap. Mark it as
689 dso__set_loaded(curr_map->dso,
693 curr_map = map_groups__find_by_name(kmaps,
695 if (curr_map == NULL) {
696 pr_debug("%s/proc/{kallsyms,modules} "
697 "inconsistency while looking "
698 "for \"%s\" module!\n",
699 machine->root_dir, module);
704 if (curr_map->dso->loaded &&
705 !machine__is_default_guest(machine))
709 * So that we look just like we get from .ko files,
710 * i.e. not prelinked, relative to map->start.
712 pos->start = curr_map->map_ip(curr_map, pos->start);
713 pos->end = curr_map->map_ip(curr_map, pos->end);
714 } else if (curr_map != map) {
715 char dso_name[PATH_MAX];
719 /* Kernel was relocated at boot time */
729 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
730 snprintf(dso_name, sizeof(dso_name),
734 snprintf(dso_name, sizeof(dso_name),
738 ndso = dso__new(dso_name);
742 ndso->kernel = dso->kernel;
744 curr_map = map__new2(pos->start, ndso, map->type);
745 if (curr_map == NULL) {
750 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
751 map_groups__insert(kmaps, curr_map);
754 /* Kernel was relocated at boot time */
759 if (filter && filter(curr_map, pos)) {
760 discard_symbol: rb_erase(&pos->rb_node, root);
763 if (curr_map != map) {
764 rb_erase(&pos->rb_node, root);
765 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
772 if (curr_map != map &&
773 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
774 machine__is_default_guest(kmaps->machine)) {
775 dso__set_loaded(curr_map->dso, curr_map->type);
778 return count + moved;
781 bool symbol__restricted_filename(const char *filename,
782 const char *restricted_filename)
784 bool restricted = false;
786 if (symbol_conf.kptr_restrict) {
787 char *r = realpath(filename, NULL);
790 restricted = strcmp(r, restricted_filename) == 0;
800 struct rb_node rb_node;
805 static void add_module(struct module_info *mi, struct rb_root *modules)
807 struct rb_node **p = &modules->rb_node;
808 struct rb_node *parent = NULL;
809 struct module_info *m;
813 m = rb_entry(parent, struct module_info, rb_node);
814 if (strcmp(mi->name, m->name) < 0)
819 rb_link_node(&mi->rb_node, parent, p);
820 rb_insert_color(&mi->rb_node, modules);
823 static void delete_modules(struct rb_root *modules)
825 struct module_info *mi;
826 struct rb_node *next = rb_first(modules);
829 mi = rb_entry(next, struct module_info, rb_node);
830 next = rb_next(&mi->rb_node);
831 rb_erase(&mi->rb_node, modules);
837 static struct module_info *find_module(const char *name,
838 struct rb_root *modules)
840 struct rb_node *n = modules->rb_node;
843 struct module_info *m;
846 m = rb_entry(n, struct module_info, rb_node);
847 cmp = strcmp(name, m->name);
859 static int __read_proc_modules(void *arg, const char *name, u64 start)
861 struct rb_root *modules = arg;
862 struct module_info *mi;
864 mi = zalloc(sizeof(struct module_info));
868 mi->name = strdup(name);
876 add_module(mi, modules);
881 static int read_proc_modules(const char *filename, struct rb_root *modules)
883 if (symbol__restricted_filename(filename, "/proc/modules"))
886 if (modules__parse(filename, modules, __read_proc_modules)) {
887 delete_modules(modules);
894 int compare_proc_modules(const char *from, const char *to)
896 struct rb_root from_modules = RB_ROOT;
897 struct rb_root to_modules = RB_ROOT;
898 struct rb_node *from_node, *to_node;
899 struct module_info *from_m, *to_m;
902 if (read_proc_modules(from, &from_modules))
905 if (read_proc_modules(to, &to_modules))
906 goto out_delete_from;
908 from_node = rb_first(&from_modules);
909 to_node = rb_first(&to_modules);
914 from_m = rb_entry(from_node, struct module_info, rb_node);
915 to_m = rb_entry(to_node, struct module_info, rb_node);
917 if (from_m->start != to_m->start ||
918 strcmp(from_m->name, to_m->name))
921 from_node = rb_next(from_node);
922 to_node = rb_next(to_node);
925 if (!from_node && !to_node)
928 delete_modules(&to_modules);
930 delete_modules(&from_modules);
935 static int do_validate_kcore_modules(const char *filename, struct map *map,
936 struct map_groups *kmaps)
938 struct rb_root modules = RB_ROOT;
942 err = read_proc_modules(filename, &modules);
946 old_map = map_groups__first(kmaps, map->type);
948 struct map *next = map_groups__next(old_map);
949 struct module_info *mi;
951 if (old_map == map || old_map->start == map->start) {
957 /* Module must be in memory at the same address */
958 mi = find_module(old_map->dso->short_name, &modules);
959 if (!mi || mi->start != old_map->start) {
967 delete_modules(&modules);
972 * If kallsyms is referenced by name then we look for filename in the same
975 static bool filename_from_kallsyms_filename(char *filename,
976 const char *base_name,
977 const char *kallsyms_filename)
981 strcpy(filename, kallsyms_filename);
982 name = strrchr(filename, '/');
988 if (!strcmp(name, "kallsyms")) {
989 strcpy(name, base_name);
996 static int validate_kcore_modules(const char *kallsyms_filename,
999 struct map_groups *kmaps = map__kmap(map)->kmaps;
1000 char modules_filename[PATH_MAX];
1002 if (!filename_from_kallsyms_filename(modules_filename, "modules",
1006 if (do_validate_kcore_modules(modules_filename, map, kmaps))
1012 static int validate_kcore_addresses(const char *kallsyms_filename,
1015 struct kmap *kmap = map__kmap(map);
1017 if (kmap->ref_reloc_sym && kmap->ref_reloc_sym->name) {
1020 start = kallsyms__get_function_start(kallsyms_filename,
1021 kmap->ref_reloc_sym->name);
1022 if (start != kmap->ref_reloc_sym->addr)
1026 return validate_kcore_modules(kallsyms_filename, map);
1029 struct kcore_mapfn_data {
1032 struct list_head maps;
1035 static int kcore_mapfn(u64 start, u64 len, u64 pgoff, void *data)
1037 struct kcore_mapfn_data *md = data;
1040 map = map__new2(start, md->dso, md->type);
1044 map->end = map->start + len;
1047 list_add(&map->node, &md->maps);
1052 static int dso__load_kcore(struct dso *dso, struct map *map,
1053 const char *kallsyms_filename)
1055 struct map_groups *kmaps = map__kmap(map)->kmaps;
1056 struct machine *machine = kmaps->machine;
1057 struct kcore_mapfn_data md;
1058 struct map *old_map, *new_map, *replacement_map = NULL;
1061 char kcore_filename[PATH_MAX];
1064 /* This function requires that the map is the kernel map */
1065 if (map != machine->vmlinux_maps[map->type])
1068 if (!filename_from_kallsyms_filename(kcore_filename, "kcore",
1072 /* Modules and kernel must be present at their original addresses */
1073 if (validate_kcore_addresses(kallsyms_filename, map))
1077 md.type = map->type;
1078 INIT_LIST_HEAD(&md.maps);
1080 fd = open(kcore_filename, O_RDONLY);
1084 /* Read new maps into temporary lists */
1085 err = file__read_maps(fd, md.type == MAP__FUNCTION, kcore_mapfn, &md,
1089 dso->is_64_bit = is_64_bit;
1091 if (list_empty(&md.maps)) {
1096 /* Remove old maps */
1097 old_map = map_groups__first(kmaps, map->type);
1099 struct map *next = map_groups__next(old_map);
1102 map_groups__remove(kmaps, old_map);
1106 /* Find the kernel map using the first symbol */
1107 sym = dso__first_symbol(dso, map->type);
1108 list_for_each_entry(new_map, &md.maps, node) {
1109 if (sym && sym->start >= new_map->start &&
1110 sym->start < new_map->end) {
1111 replacement_map = new_map;
1116 if (!replacement_map)
1117 replacement_map = list_entry(md.maps.next, struct map, node);
1120 while (!list_empty(&md.maps)) {
1121 new_map = list_entry(md.maps.next, struct map, node);
1122 list_del(&new_map->node);
1123 if (new_map == replacement_map) {
1124 map->start = new_map->start;
1125 map->end = new_map->end;
1126 map->pgoff = new_map->pgoff;
1127 map->map_ip = new_map->map_ip;
1128 map->unmap_ip = new_map->unmap_ip;
1129 map__delete(new_map);
1130 /* Ensure maps are correctly ordered */
1131 map_groups__remove(kmaps, map);
1132 map_groups__insert(kmaps, map);
1134 map_groups__insert(kmaps, new_map);
1139 * Set the data type and long name so that kcore can be read via
1140 * dso__data_read_addr().
1142 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1143 dso->binary_type = DSO_BINARY_TYPE__GUEST_KCORE;
1145 dso->binary_type = DSO_BINARY_TYPE__KCORE;
1146 dso__set_long_name(dso, strdup(kcore_filename), true);
1150 if (map->type == MAP__FUNCTION)
1151 pr_debug("Using %s for kernel object code\n", kcore_filename);
1153 pr_debug("Using %s for kernel data\n", kcore_filename);
1158 while (!list_empty(&md.maps)) {
1159 map = list_entry(md.maps.next, struct map, node);
1160 list_del(&map->node);
1168 * If the kernel is relocated at boot time, kallsyms won't match. Compute the
1169 * delta based on the relocation reference symbol.
1171 static int kallsyms__delta(struct map *map, const char *filename, u64 *delta)
1173 struct kmap *kmap = map__kmap(map);
1176 if (!kmap->ref_reloc_sym || !kmap->ref_reloc_sym->name)
1179 addr = kallsyms__get_function_start(filename,
1180 kmap->ref_reloc_sym->name);
1184 *delta = addr - kmap->ref_reloc_sym->addr;
1188 int dso__load_kallsyms(struct dso *dso, const char *filename,
1189 struct map *map, symbol_filter_t filter)
1193 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
1196 if (dso__load_all_kallsyms(dso, filename, map) < 0)
1199 if (kallsyms__delta(map, filename, &delta))
1202 symbols__fixup_duplicate(&dso->symbols[map->type]);
1203 symbols__fixup_end(&dso->symbols[map->type]);
1205 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1206 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
1208 dso->symtab_type = DSO_BINARY_TYPE__KALLSYMS;
1210 if (!dso__load_kcore(dso, map, filename))
1211 return dso__split_kallsyms_for_kcore(dso, map, filter);
1213 return dso__split_kallsyms(dso, map, delta, filter);
1216 static int dso__load_perf_map(struct dso *dso, struct map *map,
1217 symbol_filter_t filter)
1224 file = fopen(dso->long_name, "r");
1228 while (!feof(file)) {
1233 line_len = getline(&line, &n, file);
1240 line[--line_len] = '\0'; /* \n */
1242 len = hex2u64(line, &start);
1245 if (len + 2 >= line_len)
1248 len += hex2u64(line + len, &size);
1251 if (len + 2 >= line_len)
1254 sym = symbol__new(start, size, STB_GLOBAL, line + len);
1257 goto out_delete_line;
1259 if (filter && filter(map, sym))
1260 symbol__delete(sym);
1262 symbols__insert(&dso->symbols[map->type], sym);
1278 static bool dso__is_compatible_symtab_type(struct dso *dso, bool kmod,
1279 enum dso_binary_type type)
1282 case DSO_BINARY_TYPE__JAVA_JIT:
1283 case DSO_BINARY_TYPE__DEBUGLINK:
1284 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
1285 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
1286 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
1287 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
1288 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
1289 return !kmod && dso->kernel == DSO_TYPE_USER;
1291 case DSO_BINARY_TYPE__KALLSYMS:
1292 case DSO_BINARY_TYPE__VMLINUX:
1293 case DSO_BINARY_TYPE__KCORE:
1294 return dso->kernel == DSO_TYPE_KERNEL;
1296 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
1297 case DSO_BINARY_TYPE__GUEST_VMLINUX:
1298 case DSO_BINARY_TYPE__GUEST_KCORE:
1299 return dso->kernel == DSO_TYPE_GUEST_KERNEL;
1301 case DSO_BINARY_TYPE__GUEST_KMODULE:
1302 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
1304 * kernel modules know their symtab type - it's set when
1305 * creating a module dso in machine__new_module().
1307 return kmod && dso->symtab_type == type;
1309 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
1312 case DSO_BINARY_TYPE__NOT_FOUND:
1318 int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter)
1323 struct machine *machine;
1324 char *root_dir = (char *) "";
1326 struct symsrc ss_[2];
1327 struct symsrc *syms_ss = NULL, *runtime_ss = NULL;
1330 dso__set_loaded(dso, map->type);
1332 if (dso->kernel == DSO_TYPE_KERNEL)
1333 return dso__load_kernel_sym(dso, map, filter);
1334 else if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1335 return dso__load_guest_kernel_sym(dso, map, filter);
1337 if (map->groups && map->groups->machine)
1338 machine = map->groups->machine;
1342 dso->adjust_symbols = 0;
1344 if (strncmp(dso->name, "/tmp/perf-", 10) == 0) {
1347 if (lstat(dso->name, &st) < 0)
1350 if (st.st_uid && (st.st_uid != geteuid())) {
1351 pr_warning("File %s not owned by current user or root, "
1352 "ignoring it.\n", dso->name);
1356 ret = dso__load_perf_map(dso, map, filter);
1357 dso->symtab_type = ret > 0 ? DSO_BINARY_TYPE__JAVA_JIT :
1358 DSO_BINARY_TYPE__NOT_FOUND;
1363 root_dir = machine->root_dir;
1365 name = malloc(PATH_MAX);
1369 kmod = dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
1370 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE;
1373 * Iterate over candidate debug images.
1374 * Keep track of "interesting" ones (those which have a symtab, dynsym,
1375 * and/or opd section) for processing.
1377 for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) {
1378 struct symsrc *ss = &ss_[ss_pos];
1379 bool next_slot = false;
1381 enum dso_binary_type symtab_type = binary_type_symtab[i];
1383 if (!dso__is_compatible_symtab_type(dso, kmod, symtab_type))
1386 if (dso__read_binary_type_filename(dso, symtab_type,
1387 root_dir, name, PATH_MAX))
1390 /* Name is now the name of the next image to try */
1391 if (symsrc__init(ss, dso, name, symtab_type) < 0)
1394 if (!syms_ss && symsrc__has_symtab(ss)) {
1397 if (!dso->symsrc_filename)
1398 dso->symsrc_filename = strdup(name);
1401 if (!runtime_ss && symsrc__possibly_runtime(ss)) {
1409 if (syms_ss && runtime_ss)
1412 symsrc__destroy(ss);
1417 if (!runtime_ss && !syms_ss)
1420 if (runtime_ss && !syms_ss) {
1421 syms_ss = runtime_ss;
1424 /* We'll have to hope for the best */
1425 if (!runtime_ss && syms_ss)
1426 runtime_ss = syms_ss;
1429 ret = dso__load_sym(dso, map, syms_ss, runtime_ss, filter, kmod);
1436 nr_plt = dso__synthesize_plt_symbols(dso, runtime_ss, map, filter);
1441 for (; ss_pos > 0; ss_pos--)
1442 symsrc__destroy(&ss_[ss_pos - 1]);
1445 if (ret < 0 && strstr(dso->name, " (deleted)") != NULL)
1450 struct map *map_groups__find_by_name(struct map_groups *mg,
1451 enum map_type type, const char *name)
1455 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
1456 struct map *map = rb_entry(nd, struct map, rb_node);
1458 if (map->dso && strcmp(map->dso->short_name, name) == 0)
1465 int dso__load_vmlinux(struct dso *dso, struct map *map,
1466 const char *vmlinux, bool vmlinux_allocated,
1467 symbol_filter_t filter)
1471 char symfs_vmlinux[PATH_MAX];
1472 enum dso_binary_type symtab_type;
1474 if (vmlinux[0] == '/')
1475 snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s", vmlinux);
1477 symbol__join_symfs(symfs_vmlinux, vmlinux);
1479 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1480 symtab_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
1482 symtab_type = DSO_BINARY_TYPE__VMLINUX;
1484 if (symsrc__init(&ss, dso, symfs_vmlinux, symtab_type))
1487 err = dso__load_sym(dso, map, &ss, &ss, filter, 0);
1488 symsrc__destroy(&ss);
1491 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1492 dso->binary_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
1494 dso->binary_type = DSO_BINARY_TYPE__VMLINUX;
1495 dso__set_long_name(dso, vmlinux, vmlinux_allocated);
1496 dso__set_loaded(dso, map->type);
1497 pr_debug("Using %s for symbols\n", symfs_vmlinux);
1503 int dso__load_vmlinux_path(struct dso *dso, struct map *map,
1504 symbol_filter_t filter)
1509 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1510 vmlinux_path__nr_entries + 1);
1512 filename = dso__build_id_filename(dso, NULL, 0);
1513 if (filename != NULL) {
1514 err = dso__load_vmlinux(dso, map, filename, true, filter);
1520 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
1521 err = dso__load_vmlinux(dso, map, vmlinux_path[i], false, filter);
1529 static int find_matching_kcore(struct map *map, char *dir, size_t dir_sz)
1531 char kallsyms_filename[PATH_MAX];
1532 struct dirent *dent;
1544 if (dent->d_type != DT_DIR)
1546 scnprintf(kallsyms_filename, sizeof(kallsyms_filename),
1547 "%s/%s/kallsyms", dir, dent->d_name);
1548 if (!validate_kcore_addresses(kallsyms_filename, map)) {
1549 strlcpy(dir, kallsyms_filename, dir_sz);
1560 static char *dso__find_kallsyms(struct dso *dso, struct map *map)
1562 u8 host_build_id[BUILD_ID_SIZE];
1563 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1564 bool is_host = false;
1565 char path[PATH_MAX];
1567 if (!dso->has_build_id) {
1569 * Last resort, if we don't have a build-id and couldn't find
1570 * any vmlinux file, try the running kernel kallsyms table.
1575 if (sysfs__read_build_id("/sys/kernel/notes", host_build_id,
1576 sizeof(host_build_id)) == 0)
1577 is_host = dso__build_id_equal(dso, host_build_id);
1579 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1581 scnprintf(path, sizeof(path), "%s/[kernel.kcore]/%s", buildid_dir,
1584 /* Use /proc/kallsyms if possible */
1589 /* If no cached kcore go with /proc/kallsyms */
1596 * Do not check the build-id cache, until we know we cannot use
1599 fd = open("/proc/kcore", O_RDONLY);
1602 /* If module maps match go with /proc/kallsyms */
1603 if (!validate_kcore_addresses("/proc/kallsyms", map))
1607 /* Find kallsyms in build-id cache with kcore */
1608 if (!find_matching_kcore(map, path, sizeof(path)))
1609 return strdup(path);
1614 /* Find kallsyms in build-id cache with kcore */
1615 if (!find_matching_kcore(map, path, sizeof(path)))
1616 return strdup(path);
1618 scnprintf(path, sizeof(path), "%s/[kernel.kallsyms]/%s",
1619 buildid_dir, sbuild_id);
1621 if (access(path, F_OK)) {
1622 pr_err("No kallsyms or vmlinux with build-id %s was found\n",
1627 return strdup(path);
1630 return strdup("/proc/kallsyms");
1633 static int dso__load_kernel_sym(struct dso *dso, struct map *map,
1634 symbol_filter_t filter)
1637 const char *kallsyms_filename = NULL;
1638 char *kallsyms_allocated_filename = NULL;
1640 * Step 1: if the user specified a kallsyms or vmlinux filename, use
1641 * it and only it, reporting errors to the user if it cannot be used.
1643 * For instance, try to analyse an ARM perf.data file _without_ a
1644 * build-id, or if the user specifies the wrong path to the right
1645 * vmlinux file, obviously we can't fallback to another vmlinux (a
1646 * x86_86 one, on the machine where analysis is being performed, say),
1647 * or worse, /proc/kallsyms.
1649 * If the specified file _has_ a build-id and there is a build-id
1650 * section in the perf.data file, we will still do the expected
1651 * validation in dso__load_vmlinux and will bail out if they don't
1654 if (symbol_conf.kallsyms_name != NULL) {
1655 kallsyms_filename = symbol_conf.kallsyms_name;
1659 if (!symbol_conf.ignore_vmlinux && symbol_conf.vmlinux_name != NULL) {
1660 return dso__load_vmlinux(dso, map, symbol_conf.vmlinux_name,
1664 if (!symbol_conf.ignore_vmlinux && vmlinux_path != NULL) {
1665 err = dso__load_vmlinux_path(dso, map, filter);
1670 /* do not try local files if a symfs was given */
1671 if (symbol_conf.symfs[0] != 0)
1674 kallsyms_allocated_filename = dso__find_kallsyms(dso, map);
1675 if (!kallsyms_allocated_filename)
1678 kallsyms_filename = kallsyms_allocated_filename;
1681 err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
1683 pr_debug("Using %s for symbols\n", kallsyms_filename);
1684 free(kallsyms_allocated_filename);
1686 if (err > 0 && !dso__is_kcore(dso)) {
1687 dso->binary_type = DSO_BINARY_TYPE__KALLSYMS;
1688 dso__set_long_name(dso, "[kernel.kallsyms]", false);
1689 map__fixup_start(map);
1690 map__fixup_end(map);
1696 static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
1697 symbol_filter_t filter)
1700 const char *kallsyms_filename = NULL;
1701 struct machine *machine;
1702 char path[PATH_MAX];
1705 pr_debug("Guest kernel map hasn't the point to groups\n");
1708 machine = map->groups->machine;
1710 if (machine__is_default_guest(machine)) {
1712 * if the user specified a vmlinux filename, use it and only
1713 * it, reporting errors to the user if it cannot be used.
1714 * Or use file guest_kallsyms inputted by user on commandline
1716 if (symbol_conf.default_guest_vmlinux_name != NULL) {
1717 err = dso__load_vmlinux(dso, map,
1718 symbol_conf.default_guest_vmlinux_name,
1723 kallsyms_filename = symbol_conf.default_guest_kallsyms;
1724 if (!kallsyms_filename)
1727 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
1728 kallsyms_filename = path;
1731 err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
1733 pr_debug("Using %s for symbols\n", kallsyms_filename);
1734 if (err > 0 && !dso__is_kcore(dso)) {
1735 dso->binary_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
1736 machine__mmap_name(machine, path, sizeof(path));
1737 dso__set_long_name(dso, strdup(path), true);
1738 map__fixup_start(map);
1739 map__fixup_end(map);
1745 static void vmlinux_path__exit(void)
1747 while (--vmlinux_path__nr_entries >= 0)
1748 zfree(&vmlinux_path[vmlinux_path__nr_entries]);
1750 zfree(&vmlinux_path);
1753 static int vmlinux_path__init(struct perf_session_env *env)
1757 char *kernel_version;
1759 vmlinux_path = malloc(sizeof(char *) * 5);
1760 if (vmlinux_path == NULL)
1763 vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
1764 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1766 ++vmlinux_path__nr_entries;
1767 vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
1768 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1770 ++vmlinux_path__nr_entries;
1772 /* only try kernel version if no symfs was given */
1773 if (symbol_conf.symfs[0] != 0)
1777 kernel_version = env->os_release;
1779 if (uname(&uts) < 0)
1782 kernel_version = uts.release;
1785 snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", kernel_version);
1786 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1787 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1789 ++vmlinux_path__nr_entries;
1790 snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", kernel_version);
1791 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1792 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1794 ++vmlinux_path__nr_entries;
1795 snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
1797 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1798 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1800 ++vmlinux_path__nr_entries;
1805 vmlinux_path__exit();
1809 int setup_list(struct strlist **list, const char *list_str,
1810 const char *list_name)
1812 if (list_str == NULL)
1815 *list = strlist__new(true, list_str);
1817 pr_err("problems parsing %s list\n", list_name);
1823 static bool symbol__read_kptr_restrict(void)
1827 if (geteuid() != 0) {
1828 FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
1832 if (fgets(line, sizeof(line), fp) != NULL)
1833 value = atoi(line) != 0;
1842 int symbol__init(struct perf_session_env *env)
1846 if (symbol_conf.initialized)
1849 symbol_conf.priv_size = PERF_ALIGN(symbol_conf.priv_size, sizeof(u64));
1853 if (symbol_conf.sort_by_name)
1854 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
1855 sizeof(struct symbol));
1857 if (symbol_conf.try_vmlinux_path && vmlinux_path__init(env) < 0)
1860 if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
1861 pr_err("'.' is the only non valid --field-separator argument\n");
1865 if (setup_list(&symbol_conf.dso_list,
1866 symbol_conf.dso_list_str, "dso") < 0)
1869 if (setup_list(&symbol_conf.comm_list,
1870 symbol_conf.comm_list_str, "comm") < 0)
1871 goto out_free_dso_list;
1873 if (setup_list(&symbol_conf.sym_list,
1874 symbol_conf.sym_list_str, "symbol") < 0)
1875 goto out_free_comm_list;
1878 * A path to symbols of "/" is identical to ""
1879 * reset here for simplicity.
1881 symfs = realpath(symbol_conf.symfs, NULL);
1883 symfs = symbol_conf.symfs;
1884 if (strcmp(symfs, "/") == 0)
1885 symbol_conf.symfs = "";
1886 if (symfs != symbol_conf.symfs)
1887 free((void *)symfs);
1889 symbol_conf.kptr_restrict = symbol__read_kptr_restrict();
1891 symbol_conf.initialized = true;
1895 strlist__delete(symbol_conf.comm_list);
1897 strlist__delete(symbol_conf.dso_list);
1901 void symbol__exit(void)
1903 if (!symbol_conf.initialized)
1905 strlist__delete(symbol_conf.sym_list);
1906 strlist__delete(symbol_conf.dso_list);
1907 strlist__delete(symbol_conf.comm_list);
1908 vmlinux_path__exit();
1909 symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
1910 symbol_conf.initialized = false;