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 .demangle_kernel = false,
38 .cumulate_callchain = true,
39 .show_hist_headers = 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__OPENEMBEDDED_DEBUGINFO,
56 DSO_BINARY_TYPE__NOT_FOUND,
59 #define DSO_BINARY_TYPE__SYMTAB_CNT ARRAY_SIZE(binary_type_symtab)
61 bool symbol_type__is_a(char symbol_type, enum map_type map_type)
63 symbol_type = toupper(symbol_type);
67 return symbol_type == 'T' || symbol_type == 'W';
69 return symbol_type == 'D';
75 static int prefix_underscores_count(const char *str)
77 const char *tail = str;
88 static int choose_best_symbol(struct symbol *syma, struct symbol *symb)
94 /* Prefer a symbol with non zero length */
95 a = syma->end - syma->start;
96 b = symb->end - symb->start;
97 if ((b == 0) && (a > 0))
99 else if ((a == 0) && (b > 0))
102 /* Prefer a non weak symbol over a weak one */
103 a = syma->binding == STB_WEAK;
104 b = symb->binding == STB_WEAK;
110 /* Prefer a global symbol over a non global one */
111 a = syma->binding == STB_GLOBAL;
112 b = symb->binding == STB_GLOBAL;
118 /* Prefer a symbol with less underscores */
119 a = prefix_underscores_count(syma->name);
120 b = prefix_underscores_count(symb->name);
126 /* Choose the symbol with the longest name */
127 na = strlen(syma->name);
128 nb = strlen(symb->name);
134 /* Avoid "SyS" kernel syscall aliases */
135 if (na >= 3 && !strncmp(syma->name, "SyS", 3))
137 if (na >= 10 && !strncmp(syma->name, "compat_SyS", 10))
143 void symbols__fixup_duplicate(struct rb_root *symbols)
146 struct symbol *curr, *next;
148 nd = rb_first(symbols);
151 curr = rb_entry(nd, struct symbol, rb_node);
153 nd = rb_next(&curr->rb_node);
154 next = rb_entry(nd, struct symbol, rb_node);
159 if (curr->start != next->start)
162 if (choose_best_symbol(curr, next) == SYMBOL_A) {
163 rb_erase(&next->rb_node, symbols);
164 symbol__delete(next);
167 nd = rb_next(&curr->rb_node);
168 rb_erase(&curr->rb_node, symbols);
169 symbol__delete(curr);
174 void symbols__fixup_end(struct rb_root *symbols)
176 struct rb_node *nd, *prevnd = rb_first(symbols);
177 struct symbol *curr, *prev;
182 curr = rb_entry(prevnd, struct symbol, rb_node);
184 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
186 curr = rb_entry(nd, struct symbol, rb_node);
188 if (prev->end == prev->start && prev->end != curr->start)
189 prev->end = curr->start - 1;
193 if (curr->end == curr->start)
194 curr->end = roundup(curr->start, 4096);
197 void __map_groups__fixup_end(struct map_groups *mg, enum map_type type)
199 struct map *prev, *curr;
200 struct rb_node *nd, *prevnd = rb_first(&mg->maps[type]);
205 curr = rb_entry(prevnd, struct map, rb_node);
207 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
209 curr = rb_entry(nd, struct map, rb_node);
210 prev->end = curr->start - 1;
214 * We still haven't the actual symbols, so guess the
215 * last map final address.
220 struct symbol *symbol__new(u64 start, u64 len, u8 binding, const char *name)
222 size_t namelen = strlen(name) + 1;
223 struct symbol *sym = calloc(1, (symbol_conf.priv_size +
224 sizeof(*sym) + namelen));
228 if (symbol_conf.priv_size)
229 sym = ((void *)sym) + symbol_conf.priv_size;
232 sym->end = len ? start + len - 1 : start;
233 sym->binding = binding;
234 sym->namelen = namelen - 1;
236 pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n",
237 __func__, name, start, sym->end);
238 memcpy(sym->name, name, namelen);
243 void symbol__delete(struct symbol *sym)
245 free(((void *)sym) - symbol_conf.priv_size);
248 size_t symbol__fprintf(struct symbol *sym, FILE *fp)
250 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %c %s\n",
251 sym->start, sym->end,
252 sym->binding == STB_GLOBAL ? 'g' :
253 sym->binding == STB_LOCAL ? 'l' : 'w',
257 size_t symbol__fprintf_symname_offs(const struct symbol *sym,
258 const struct addr_location *al, FILE *fp)
260 unsigned long offset;
263 if (sym && sym->name) {
264 length = fprintf(fp, "%s", sym->name);
266 if (al->addr < sym->end)
267 offset = al->addr - sym->start;
269 offset = al->addr - al->map->start - sym->start;
270 length += fprintf(fp, "+0x%lx", offset);
274 return fprintf(fp, "[unknown]");
277 size_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp)
279 return symbol__fprintf_symname_offs(sym, NULL, fp);
282 void symbols__delete(struct rb_root *symbols)
285 struct rb_node *next = rb_first(symbols);
288 pos = rb_entry(next, struct symbol, rb_node);
289 next = rb_next(&pos->rb_node);
290 rb_erase(&pos->rb_node, symbols);
295 void symbols__insert(struct rb_root *symbols, struct symbol *sym)
297 struct rb_node **p = &symbols->rb_node;
298 struct rb_node *parent = NULL;
299 const u64 ip = sym->start;
304 s = rb_entry(parent, struct symbol, rb_node);
310 rb_link_node(&sym->rb_node, parent, p);
311 rb_insert_color(&sym->rb_node, symbols);
314 static struct symbol *symbols__find(struct rb_root *symbols, u64 ip)
321 n = symbols->rb_node;
324 struct symbol *s = rb_entry(n, struct symbol, rb_node);
328 else if (ip > s->end)
337 static struct symbol *symbols__first(struct rb_root *symbols)
339 struct rb_node *n = rb_first(symbols);
342 return rb_entry(n, struct symbol, rb_node);
347 static struct symbol *symbols__next(struct symbol *sym)
349 struct rb_node *n = rb_next(&sym->rb_node);
352 return rb_entry(n, struct symbol, rb_node);
357 struct symbol_name_rb_node {
358 struct rb_node rb_node;
362 static void symbols__insert_by_name(struct rb_root *symbols, struct symbol *sym)
364 struct rb_node **p = &symbols->rb_node;
365 struct rb_node *parent = NULL;
366 struct symbol_name_rb_node *symn, *s;
368 symn = container_of(sym, struct symbol_name_rb_node, sym);
372 s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
373 if (strcmp(sym->name, s->sym.name) < 0)
378 rb_link_node(&symn->rb_node, parent, p);
379 rb_insert_color(&symn->rb_node, symbols);
382 static void symbols__sort_by_name(struct rb_root *symbols,
383 struct rb_root *source)
387 for (nd = rb_first(source); nd; nd = rb_next(nd)) {
388 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
389 symbols__insert_by_name(symbols, pos);
393 static struct symbol *symbols__find_by_name(struct rb_root *symbols,
401 n = symbols->rb_node;
404 struct symbol_name_rb_node *s;
407 s = rb_entry(n, struct symbol_name_rb_node, rb_node);
408 cmp = strcmp(name, s->sym.name);
421 struct symbol *dso__find_symbol(struct dso *dso,
422 enum map_type type, u64 addr)
424 return symbols__find(&dso->symbols[type], addr);
427 struct symbol *dso__first_symbol(struct dso *dso, enum map_type type)
429 return symbols__first(&dso->symbols[type]);
432 struct symbol *dso__next_symbol(struct symbol *sym)
434 return symbols__next(sym);
437 struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
440 return symbols__find_by_name(&dso->symbol_names[type], name);
443 void dso__sort_by_name(struct dso *dso, enum map_type type)
445 dso__set_sorted_by_name(dso, type);
446 return symbols__sort_by_name(&dso->symbol_names[type],
447 &dso->symbols[type]);
450 size_t dso__fprintf_symbols_by_name(struct dso *dso,
451 enum map_type type, FILE *fp)
455 struct symbol_name_rb_node *pos;
457 for (nd = rb_first(&dso->symbol_names[type]); nd; nd = rb_next(nd)) {
458 pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
459 fprintf(fp, "%s\n", pos->sym.name);
465 int modules__parse(const char *filename, void *arg,
466 int (*process_module)(void *arg, const char *name,
474 file = fopen(filename, "r");
484 line_len = getline(&line, &n, file);
497 line[--line_len] = '\0'; /* \n */
499 sep = strrchr(line, 'x');
503 hex2u64(sep + 1, &start);
505 sep = strchr(line, ' ');
511 scnprintf(name, sizeof(name), "[%s]", line);
513 err = process_module(arg, name, start);
523 struct process_kallsyms_args {
529 * These are symbols in the kernel image, so make sure that
530 * sym is from a kernel DSO.
532 bool symbol__is_idle(struct symbol *sym)
534 const char * const idle_symbols[] = {
543 "mwait_idle_with_hints",
545 "ppc64_runlatch_off",
546 "pseries_dedicated_idle_sleep",
555 for (i = 0; idle_symbols[i]; i++) {
556 if (!strcmp(idle_symbols[i], sym->name))
563 static int map__process_kallsym_symbol(void *arg, const char *name,
564 char type, u64 start)
567 struct process_kallsyms_args *a = arg;
568 struct rb_root *root = &a->dso->symbols[a->map->type];
570 if (!symbol_type__is_a(type, a->map->type))
574 * module symbols are not sorted so we add all
575 * symbols, setting length to 0, and rely on
576 * symbols__fixup_end() to fix it up.
578 sym = symbol__new(start, 0, kallsyms2elf_type(type), name);
582 * We will pass the symbols to the filter later, in
583 * map__split_kallsyms, when we have split the maps per module
585 symbols__insert(root, sym);
591 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
592 * so that we can in the next step set the symbol ->end address and then
593 * call kernel_maps__split_kallsyms.
595 static int dso__load_all_kallsyms(struct dso *dso, const char *filename,
598 struct process_kallsyms_args args = { .map = map, .dso = dso, };
599 return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
602 static int dso__split_kallsyms_for_kcore(struct dso *dso, struct map *map,
603 symbol_filter_t filter)
605 struct map_groups *kmaps = map__kmap(map)->kmaps;
606 struct map *curr_map;
608 int count = 0, moved = 0;
609 struct rb_root *root = &dso->symbols[map->type];
610 struct rb_node *next = rb_first(root);
615 pos = rb_entry(next, struct symbol, rb_node);
616 next = rb_next(&pos->rb_node);
618 module = strchr(pos->name, '\t');
622 curr_map = map_groups__find(kmaps, map->type, pos->start);
624 if (!curr_map || (filter && filter(curr_map, pos))) {
625 rb_erase(&pos->rb_node, root);
628 pos->start -= curr_map->start - curr_map->pgoff;
630 pos->end -= curr_map->start - curr_map->pgoff;
631 if (curr_map != map) {
632 rb_erase(&pos->rb_node, root);
634 &curr_map->dso->symbols[curr_map->type],
643 /* Symbols have been adjusted */
644 dso->adjust_symbols = 1;
646 return count + moved;
650 * Split the symbols into maps, making sure there are no overlaps, i.e. the
651 * kernel range is broken in several maps, named [kernel].N, as we don't have
652 * the original ELF section names vmlinux have.
654 static int dso__split_kallsyms(struct dso *dso, struct map *map, u64 delta,
655 symbol_filter_t filter)
657 struct map_groups *kmaps = map__kmap(map)->kmaps;
658 struct machine *machine = kmaps->machine;
659 struct map *curr_map = map;
661 int count = 0, moved = 0;
662 struct rb_root *root = &dso->symbols[map->type];
663 struct rb_node *next = rb_first(root);
664 int kernel_range = 0;
669 pos = rb_entry(next, struct symbol, rb_node);
670 next = rb_next(&pos->rb_node);
672 module = strchr(pos->name, '\t');
674 if (!symbol_conf.use_modules)
679 if (strcmp(curr_map->dso->short_name, module)) {
680 if (curr_map != map &&
681 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
682 machine__is_default_guest(machine)) {
684 * We assume all symbols of a module are
685 * continuous in * kallsyms, so curr_map
686 * points to a module and all its
687 * symbols are in its kmap. Mark it as
690 dso__set_loaded(curr_map->dso,
694 curr_map = map_groups__find_by_name(kmaps,
696 if (curr_map == NULL) {
697 pr_debug("%s/proc/{kallsyms,modules} "
698 "inconsistency while looking "
699 "for \"%s\" module!\n",
700 machine->root_dir, module);
705 if (curr_map->dso->loaded &&
706 !machine__is_default_guest(machine))
710 * So that we look just like we get from .ko files,
711 * i.e. not prelinked, relative to map->start.
713 pos->start = curr_map->map_ip(curr_map, pos->start);
714 pos->end = curr_map->map_ip(curr_map, pos->end);
715 } else if (curr_map != map) {
716 char dso_name[PATH_MAX];
720 /* Kernel was relocated at boot time */
730 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
731 snprintf(dso_name, sizeof(dso_name),
735 snprintf(dso_name, sizeof(dso_name),
739 ndso = dso__new(dso_name);
743 ndso->kernel = dso->kernel;
745 curr_map = map__new2(pos->start, ndso, map->type);
746 if (curr_map == NULL) {
751 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
752 map_groups__insert(kmaps, curr_map);
755 /* Kernel was relocated at boot time */
760 if (filter && filter(curr_map, pos)) {
761 discard_symbol: rb_erase(&pos->rb_node, root);
764 if (curr_map != map) {
765 rb_erase(&pos->rb_node, root);
766 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
773 if (curr_map != map &&
774 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
775 machine__is_default_guest(kmaps->machine)) {
776 dso__set_loaded(curr_map->dso, curr_map->type);
779 return count + moved;
782 bool symbol__restricted_filename(const char *filename,
783 const char *restricted_filename)
785 bool restricted = false;
787 if (symbol_conf.kptr_restrict) {
788 char *r = realpath(filename, NULL);
791 restricted = strcmp(r, restricted_filename) == 0;
801 struct rb_node rb_node;
806 static void add_module(struct module_info *mi, struct rb_root *modules)
808 struct rb_node **p = &modules->rb_node;
809 struct rb_node *parent = NULL;
810 struct module_info *m;
814 m = rb_entry(parent, struct module_info, rb_node);
815 if (strcmp(mi->name, m->name) < 0)
820 rb_link_node(&mi->rb_node, parent, p);
821 rb_insert_color(&mi->rb_node, modules);
824 static void delete_modules(struct rb_root *modules)
826 struct module_info *mi;
827 struct rb_node *next = rb_first(modules);
830 mi = rb_entry(next, struct module_info, rb_node);
831 next = rb_next(&mi->rb_node);
832 rb_erase(&mi->rb_node, modules);
838 static struct module_info *find_module(const char *name,
839 struct rb_root *modules)
841 struct rb_node *n = modules->rb_node;
844 struct module_info *m;
847 m = rb_entry(n, struct module_info, rb_node);
848 cmp = strcmp(name, m->name);
860 static int __read_proc_modules(void *arg, const char *name, u64 start)
862 struct rb_root *modules = arg;
863 struct module_info *mi;
865 mi = zalloc(sizeof(struct module_info));
869 mi->name = strdup(name);
877 add_module(mi, modules);
882 static int read_proc_modules(const char *filename, struct rb_root *modules)
884 if (symbol__restricted_filename(filename, "/proc/modules"))
887 if (modules__parse(filename, modules, __read_proc_modules)) {
888 delete_modules(modules);
895 int compare_proc_modules(const char *from, const char *to)
897 struct rb_root from_modules = RB_ROOT;
898 struct rb_root to_modules = RB_ROOT;
899 struct rb_node *from_node, *to_node;
900 struct module_info *from_m, *to_m;
903 if (read_proc_modules(from, &from_modules))
906 if (read_proc_modules(to, &to_modules))
907 goto out_delete_from;
909 from_node = rb_first(&from_modules);
910 to_node = rb_first(&to_modules);
915 from_m = rb_entry(from_node, struct module_info, rb_node);
916 to_m = rb_entry(to_node, struct module_info, rb_node);
918 if (from_m->start != to_m->start ||
919 strcmp(from_m->name, to_m->name))
922 from_node = rb_next(from_node);
923 to_node = rb_next(to_node);
926 if (!from_node && !to_node)
929 delete_modules(&to_modules);
931 delete_modules(&from_modules);
936 static int do_validate_kcore_modules(const char *filename, struct map *map,
937 struct map_groups *kmaps)
939 struct rb_root modules = RB_ROOT;
943 err = read_proc_modules(filename, &modules);
947 old_map = map_groups__first(kmaps, map->type);
949 struct map *next = map_groups__next(old_map);
950 struct module_info *mi;
952 if (old_map == map || old_map->start == map->start) {
958 /* Module must be in memory at the same address */
959 mi = find_module(old_map->dso->short_name, &modules);
960 if (!mi || mi->start != old_map->start) {
968 delete_modules(&modules);
973 * If kallsyms is referenced by name then we look for filename in the same
976 static bool filename_from_kallsyms_filename(char *filename,
977 const char *base_name,
978 const char *kallsyms_filename)
982 strcpy(filename, kallsyms_filename);
983 name = strrchr(filename, '/');
989 if (!strcmp(name, "kallsyms")) {
990 strcpy(name, base_name);
997 static int validate_kcore_modules(const char *kallsyms_filename,
1000 struct map_groups *kmaps = map__kmap(map)->kmaps;
1001 char modules_filename[PATH_MAX];
1003 if (!filename_from_kallsyms_filename(modules_filename, "modules",
1007 if (do_validate_kcore_modules(modules_filename, map, kmaps))
1013 static int validate_kcore_addresses(const char *kallsyms_filename,
1016 struct kmap *kmap = map__kmap(map);
1018 if (kmap->ref_reloc_sym && kmap->ref_reloc_sym->name) {
1021 start = kallsyms__get_function_start(kallsyms_filename,
1022 kmap->ref_reloc_sym->name);
1023 if (start != kmap->ref_reloc_sym->addr)
1027 return validate_kcore_modules(kallsyms_filename, map);
1030 struct kcore_mapfn_data {
1033 struct list_head maps;
1036 static int kcore_mapfn(u64 start, u64 len, u64 pgoff, void *data)
1038 struct kcore_mapfn_data *md = data;
1041 map = map__new2(start, md->dso, md->type);
1045 map->end = map->start + len;
1048 list_add(&map->node, &md->maps);
1053 static int dso__load_kcore(struct dso *dso, struct map *map,
1054 const char *kallsyms_filename)
1056 struct map_groups *kmaps = map__kmap(map)->kmaps;
1057 struct machine *machine = kmaps->machine;
1058 struct kcore_mapfn_data md;
1059 struct map *old_map, *new_map, *replacement_map = NULL;
1062 char kcore_filename[PATH_MAX];
1065 /* This function requires that the map is the kernel map */
1066 if (map != machine->vmlinux_maps[map->type])
1069 if (!filename_from_kallsyms_filename(kcore_filename, "kcore",
1073 /* Modules and kernel must be present at their original addresses */
1074 if (validate_kcore_addresses(kallsyms_filename, map))
1078 md.type = map->type;
1079 INIT_LIST_HEAD(&md.maps);
1081 fd = open(kcore_filename, O_RDONLY);
1085 /* Read new maps into temporary lists */
1086 err = file__read_maps(fd, md.type == MAP__FUNCTION, kcore_mapfn, &md,
1090 dso->is_64_bit = is_64_bit;
1092 if (list_empty(&md.maps)) {
1097 /* Remove old maps */
1098 old_map = map_groups__first(kmaps, map->type);
1100 struct map *next = map_groups__next(old_map);
1103 map_groups__remove(kmaps, old_map);
1107 /* Find the kernel map using the first symbol */
1108 sym = dso__first_symbol(dso, map->type);
1109 list_for_each_entry(new_map, &md.maps, node) {
1110 if (sym && sym->start >= new_map->start &&
1111 sym->start < new_map->end) {
1112 replacement_map = new_map;
1117 if (!replacement_map)
1118 replacement_map = list_entry(md.maps.next, struct map, node);
1121 while (!list_empty(&md.maps)) {
1122 new_map = list_entry(md.maps.next, struct map, node);
1123 list_del(&new_map->node);
1124 if (new_map == replacement_map) {
1125 map->start = new_map->start;
1126 map->end = new_map->end;
1127 map->pgoff = new_map->pgoff;
1128 map->map_ip = new_map->map_ip;
1129 map->unmap_ip = new_map->unmap_ip;
1130 map__delete(new_map);
1131 /* Ensure maps are correctly ordered */
1132 map_groups__remove(kmaps, map);
1133 map_groups__insert(kmaps, map);
1135 map_groups__insert(kmaps, new_map);
1140 * Set the data type and long name so that kcore can be read via
1141 * dso__data_read_addr().
1143 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1144 dso->binary_type = DSO_BINARY_TYPE__GUEST_KCORE;
1146 dso->binary_type = DSO_BINARY_TYPE__KCORE;
1147 dso__set_long_name(dso, strdup(kcore_filename), true);
1151 if (map->type == MAP__FUNCTION)
1152 pr_debug("Using %s for kernel object code\n", kcore_filename);
1154 pr_debug("Using %s for kernel data\n", kcore_filename);
1159 while (!list_empty(&md.maps)) {
1160 map = list_entry(md.maps.next, struct map, node);
1161 list_del(&map->node);
1169 * If the kernel is relocated at boot time, kallsyms won't match. Compute the
1170 * delta based on the relocation reference symbol.
1172 static int kallsyms__delta(struct map *map, const char *filename, u64 *delta)
1174 struct kmap *kmap = map__kmap(map);
1177 if (!kmap->ref_reloc_sym || !kmap->ref_reloc_sym->name)
1180 addr = kallsyms__get_function_start(filename,
1181 kmap->ref_reloc_sym->name);
1185 *delta = addr - kmap->ref_reloc_sym->addr;
1189 int dso__load_kallsyms(struct dso *dso, const char *filename,
1190 struct map *map, symbol_filter_t filter)
1194 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
1197 if (dso__load_all_kallsyms(dso, filename, map) < 0)
1200 if (kallsyms__delta(map, filename, &delta))
1203 symbols__fixup_duplicate(&dso->symbols[map->type]);
1204 symbols__fixup_end(&dso->symbols[map->type]);
1206 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1207 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
1209 dso->symtab_type = DSO_BINARY_TYPE__KALLSYMS;
1211 if (!dso__load_kcore(dso, map, filename))
1212 return dso__split_kallsyms_for_kcore(dso, map, filter);
1214 return dso__split_kallsyms(dso, map, delta, filter);
1217 static int dso__load_perf_map(struct dso *dso, struct map *map,
1218 symbol_filter_t filter)
1225 file = fopen(dso->long_name, "r");
1229 while (!feof(file)) {
1234 line_len = getline(&line, &n, file);
1241 line[--line_len] = '\0'; /* \n */
1243 len = hex2u64(line, &start);
1246 if (len + 2 >= line_len)
1249 len += hex2u64(line + len, &size);
1252 if (len + 2 >= line_len)
1255 sym = symbol__new(start, size, STB_GLOBAL, line + len);
1258 goto out_delete_line;
1260 if (filter && filter(map, sym))
1261 symbol__delete(sym);
1263 symbols__insert(&dso->symbols[map->type], sym);
1279 static bool dso__is_compatible_symtab_type(struct dso *dso, bool kmod,
1280 enum dso_binary_type type)
1283 case DSO_BINARY_TYPE__JAVA_JIT:
1284 case DSO_BINARY_TYPE__DEBUGLINK:
1285 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
1286 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
1287 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
1288 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
1289 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
1290 return !kmod && dso->kernel == DSO_TYPE_USER;
1292 case DSO_BINARY_TYPE__KALLSYMS:
1293 case DSO_BINARY_TYPE__VMLINUX:
1294 case DSO_BINARY_TYPE__KCORE:
1295 return dso->kernel == DSO_TYPE_KERNEL;
1297 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
1298 case DSO_BINARY_TYPE__GUEST_VMLINUX:
1299 case DSO_BINARY_TYPE__GUEST_KCORE:
1300 return dso->kernel == DSO_TYPE_GUEST_KERNEL;
1302 case DSO_BINARY_TYPE__GUEST_KMODULE:
1303 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
1305 * kernel modules know their symtab type - it's set when
1306 * creating a module dso in machine__new_module().
1308 return kmod && dso->symtab_type == type;
1310 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
1313 case DSO_BINARY_TYPE__NOT_FOUND:
1319 int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter)
1324 struct machine *machine;
1325 char *root_dir = (char *) "";
1327 struct symsrc ss_[2];
1328 struct symsrc *syms_ss = NULL, *runtime_ss = NULL;
1331 dso__set_loaded(dso, map->type);
1333 if (dso->kernel == DSO_TYPE_KERNEL)
1334 return dso__load_kernel_sym(dso, map, filter);
1335 else if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1336 return dso__load_guest_kernel_sym(dso, map, filter);
1338 if (map->groups && map->groups->machine)
1339 machine = map->groups->machine;
1343 dso->adjust_symbols = 0;
1345 if (strncmp(dso->name, "/tmp/perf-", 10) == 0) {
1348 if (lstat(dso->name, &st) < 0)
1351 if (st.st_uid && (st.st_uid != geteuid())) {
1352 pr_warning("File %s not owned by current user or root, "
1353 "ignoring it.\n", dso->name);
1357 ret = dso__load_perf_map(dso, map, filter);
1358 dso->symtab_type = ret > 0 ? DSO_BINARY_TYPE__JAVA_JIT :
1359 DSO_BINARY_TYPE__NOT_FOUND;
1364 root_dir = machine->root_dir;
1366 name = malloc(PATH_MAX);
1370 kmod = dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
1371 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE;
1374 * Iterate over candidate debug images.
1375 * Keep track of "interesting" ones (those which have a symtab, dynsym,
1376 * and/or opd section) for processing.
1378 for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) {
1379 struct symsrc *ss = &ss_[ss_pos];
1380 bool next_slot = false;
1382 enum dso_binary_type symtab_type = binary_type_symtab[i];
1384 if (!dso__is_compatible_symtab_type(dso, kmod, symtab_type))
1387 if (dso__read_binary_type_filename(dso, symtab_type,
1388 root_dir, name, PATH_MAX))
1391 /* Name is now the name of the next image to try */
1392 if (symsrc__init(ss, dso, name, symtab_type) < 0)
1395 if (!syms_ss && symsrc__has_symtab(ss)) {
1398 if (!dso->symsrc_filename)
1399 dso->symsrc_filename = strdup(name);
1402 if (!runtime_ss && symsrc__possibly_runtime(ss)) {
1410 if (syms_ss && runtime_ss)
1413 symsrc__destroy(ss);
1418 if (!runtime_ss && !syms_ss)
1421 if (runtime_ss && !syms_ss) {
1422 syms_ss = runtime_ss;
1425 /* We'll have to hope for the best */
1426 if (!runtime_ss && syms_ss)
1427 runtime_ss = syms_ss;
1430 ret = dso__load_sym(dso, map, syms_ss, runtime_ss, filter, kmod);
1437 nr_plt = dso__synthesize_plt_symbols(dso, runtime_ss, map, filter);
1442 for (; ss_pos > 0; ss_pos--)
1443 symsrc__destroy(&ss_[ss_pos - 1]);
1446 if (ret < 0 && strstr(dso->name, " (deleted)") != NULL)
1451 struct map *map_groups__find_by_name(struct map_groups *mg,
1452 enum map_type type, const char *name)
1456 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
1457 struct map *map = rb_entry(nd, struct map, rb_node);
1459 if (map->dso && strcmp(map->dso->short_name, name) == 0)
1466 int dso__load_vmlinux(struct dso *dso, struct map *map,
1467 const char *vmlinux, bool vmlinux_allocated,
1468 symbol_filter_t filter)
1472 char symfs_vmlinux[PATH_MAX];
1473 enum dso_binary_type symtab_type;
1475 if (vmlinux[0] == '/')
1476 snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s", vmlinux);
1478 symbol__join_symfs(symfs_vmlinux, vmlinux);
1480 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1481 symtab_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
1483 symtab_type = DSO_BINARY_TYPE__VMLINUX;
1485 if (symsrc__init(&ss, dso, symfs_vmlinux, symtab_type))
1488 err = dso__load_sym(dso, map, &ss, &ss, filter, 0);
1489 symsrc__destroy(&ss);
1492 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1493 dso->binary_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
1495 dso->binary_type = DSO_BINARY_TYPE__VMLINUX;
1496 dso__set_long_name(dso, vmlinux, vmlinux_allocated);
1497 dso__set_loaded(dso, map->type);
1498 pr_debug("Using %s for symbols\n", symfs_vmlinux);
1504 int dso__load_vmlinux_path(struct dso *dso, struct map *map,
1505 symbol_filter_t filter)
1510 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1511 vmlinux_path__nr_entries + 1);
1513 filename = dso__build_id_filename(dso, NULL, 0);
1514 if (filename != NULL) {
1515 err = dso__load_vmlinux(dso, map, filename, true, filter);
1521 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
1522 err = dso__load_vmlinux(dso, map, vmlinux_path[i], false, filter);
1530 static int find_matching_kcore(struct map *map, char *dir, size_t dir_sz)
1532 char kallsyms_filename[PATH_MAX];
1533 struct dirent *dent;
1545 if (dent->d_type != DT_DIR)
1547 scnprintf(kallsyms_filename, sizeof(kallsyms_filename),
1548 "%s/%s/kallsyms", dir, dent->d_name);
1549 if (!validate_kcore_addresses(kallsyms_filename, map)) {
1550 strlcpy(dir, kallsyms_filename, dir_sz);
1561 static char *dso__find_kallsyms(struct dso *dso, struct map *map)
1563 u8 host_build_id[BUILD_ID_SIZE];
1564 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1565 bool is_host = false;
1566 char path[PATH_MAX];
1568 if (!dso->has_build_id) {
1570 * Last resort, if we don't have a build-id and couldn't find
1571 * any vmlinux file, try the running kernel kallsyms table.
1576 if (sysfs__read_build_id("/sys/kernel/notes", host_build_id,
1577 sizeof(host_build_id)) == 0)
1578 is_host = dso__build_id_equal(dso, host_build_id);
1580 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1582 scnprintf(path, sizeof(path), "%s/[kernel.kcore]/%s", buildid_dir,
1585 /* Use /proc/kallsyms if possible */
1590 /* If no cached kcore go with /proc/kallsyms */
1597 * Do not check the build-id cache, until we know we cannot use
1600 fd = open("/proc/kcore", O_RDONLY);
1603 /* If module maps match go with /proc/kallsyms */
1604 if (!validate_kcore_addresses("/proc/kallsyms", map))
1608 /* Find kallsyms in build-id cache with kcore */
1609 if (!find_matching_kcore(map, path, sizeof(path)))
1610 return strdup(path);
1615 /* Find kallsyms in build-id cache with kcore */
1616 if (!find_matching_kcore(map, path, sizeof(path)))
1617 return strdup(path);
1619 scnprintf(path, sizeof(path), "%s/[kernel.kallsyms]/%s",
1620 buildid_dir, sbuild_id);
1622 if (access(path, F_OK)) {
1623 pr_err("No kallsyms or vmlinux with build-id %s was found\n",
1628 return strdup(path);
1631 return strdup("/proc/kallsyms");
1634 static int dso__load_kernel_sym(struct dso *dso, struct map *map,
1635 symbol_filter_t filter)
1638 const char *kallsyms_filename = NULL;
1639 char *kallsyms_allocated_filename = NULL;
1641 * Step 1: if the user specified a kallsyms or vmlinux filename, use
1642 * it and only it, reporting errors to the user if it cannot be used.
1644 * For instance, try to analyse an ARM perf.data file _without_ a
1645 * build-id, or if the user specifies the wrong path to the right
1646 * vmlinux file, obviously we can't fallback to another vmlinux (a
1647 * x86_86 one, on the machine where analysis is being performed, say),
1648 * or worse, /proc/kallsyms.
1650 * If the specified file _has_ a build-id and there is a build-id
1651 * section in the perf.data file, we will still do the expected
1652 * validation in dso__load_vmlinux and will bail out if they don't
1655 if (symbol_conf.kallsyms_name != NULL) {
1656 kallsyms_filename = symbol_conf.kallsyms_name;
1660 if (!symbol_conf.ignore_vmlinux && symbol_conf.vmlinux_name != NULL) {
1661 return dso__load_vmlinux(dso, map, symbol_conf.vmlinux_name,
1665 if (!symbol_conf.ignore_vmlinux && vmlinux_path != NULL) {
1666 err = dso__load_vmlinux_path(dso, map, filter);
1671 /* do not try local files if a symfs was given */
1672 if (symbol_conf.symfs[0] != 0)
1675 kallsyms_allocated_filename = dso__find_kallsyms(dso, map);
1676 if (!kallsyms_allocated_filename)
1679 kallsyms_filename = kallsyms_allocated_filename;
1682 err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
1684 pr_debug("Using %s for symbols\n", kallsyms_filename);
1685 free(kallsyms_allocated_filename);
1687 if (err > 0 && !dso__is_kcore(dso)) {
1688 dso->binary_type = DSO_BINARY_TYPE__KALLSYMS;
1689 dso__set_long_name(dso, "[kernel.kallsyms]", false);
1690 map__fixup_start(map);
1691 map__fixup_end(map);
1697 static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
1698 symbol_filter_t filter)
1701 const char *kallsyms_filename = NULL;
1702 struct machine *machine;
1703 char path[PATH_MAX];
1706 pr_debug("Guest kernel map hasn't the point to groups\n");
1709 machine = map->groups->machine;
1711 if (machine__is_default_guest(machine)) {
1713 * if the user specified a vmlinux filename, use it and only
1714 * it, reporting errors to the user if it cannot be used.
1715 * Or use file guest_kallsyms inputted by user on commandline
1717 if (symbol_conf.default_guest_vmlinux_name != NULL) {
1718 err = dso__load_vmlinux(dso, map,
1719 symbol_conf.default_guest_vmlinux_name,
1724 kallsyms_filename = symbol_conf.default_guest_kallsyms;
1725 if (!kallsyms_filename)
1728 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
1729 kallsyms_filename = path;
1732 err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
1734 pr_debug("Using %s for symbols\n", kallsyms_filename);
1735 if (err > 0 && !dso__is_kcore(dso)) {
1736 dso->binary_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
1737 machine__mmap_name(machine, path, sizeof(path));
1738 dso__set_long_name(dso, strdup(path), true);
1739 map__fixup_start(map);
1740 map__fixup_end(map);
1746 static void vmlinux_path__exit(void)
1748 while (--vmlinux_path__nr_entries >= 0)
1749 zfree(&vmlinux_path[vmlinux_path__nr_entries]);
1751 zfree(&vmlinux_path);
1754 static int vmlinux_path__init(struct perf_session_env *env)
1758 char *kernel_version;
1760 vmlinux_path = malloc(sizeof(char *) * 6);
1761 if (vmlinux_path == NULL)
1764 vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
1765 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1767 ++vmlinux_path__nr_entries;
1768 vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
1769 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1771 ++vmlinux_path__nr_entries;
1773 /* only try kernel version if no symfs was given */
1774 if (symbol_conf.symfs[0] != 0)
1778 kernel_version = env->os_release;
1780 if (uname(&uts) < 0)
1783 kernel_version = uts.release;
1786 snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", kernel_version);
1787 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1788 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1790 ++vmlinux_path__nr_entries;
1791 snprintf(bf, sizeof(bf), "/usr/lib/debug/boot/vmlinux-%s",
1793 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1794 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1796 ++vmlinux_path__nr_entries;
1797 snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", kernel_version);
1798 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1799 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1801 ++vmlinux_path__nr_entries;
1802 snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
1804 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1805 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1807 ++vmlinux_path__nr_entries;
1812 vmlinux_path__exit();
1816 int setup_list(struct strlist **list, const char *list_str,
1817 const char *list_name)
1819 if (list_str == NULL)
1822 *list = strlist__new(true, list_str);
1824 pr_err("problems parsing %s list\n", list_name);
1830 static bool symbol__read_kptr_restrict(void)
1834 if (geteuid() != 0) {
1835 FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
1839 if (fgets(line, sizeof(line), fp) != NULL)
1840 value = atoi(line) != 0;
1849 int symbol__init(struct perf_session_env *env)
1853 if (symbol_conf.initialized)
1856 symbol_conf.priv_size = PERF_ALIGN(symbol_conf.priv_size, sizeof(u64));
1860 if (symbol_conf.sort_by_name)
1861 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
1862 sizeof(struct symbol));
1864 if (symbol_conf.try_vmlinux_path && vmlinux_path__init(env) < 0)
1867 if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
1868 pr_err("'.' is the only non valid --field-separator argument\n");
1872 if (setup_list(&symbol_conf.dso_list,
1873 symbol_conf.dso_list_str, "dso") < 0)
1876 if (setup_list(&symbol_conf.comm_list,
1877 symbol_conf.comm_list_str, "comm") < 0)
1878 goto out_free_dso_list;
1880 if (setup_list(&symbol_conf.sym_list,
1881 symbol_conf.sym_list_str, "symbol") < 0)
1882 goto out_free_comm_list;
1885 * A path to symbols of "/" is identical to ""
1886 * reset here for simplicity.
1888 symfs = realpath(symbol_conf.symfs, NULL);
1890 symfs = symbol_conf.symfs;
1891 if (strcmp(symfs, "/") == 0)
1892 symbol_conf.symfs = "";
1893 if (symfs != symbol_conf.symfs)
1894 free((void *)symfs);
1896 symbol_conf.kptr_restrict = symbol__read_kptr_restrict();
1898 symbol_conf.initialized = true;
1902 strlist__delete(symbol_conf.comm_list);
1904 strlist__delete(symbol_conf.dso_list);
1908 void symbol__exit(void)
1910 if (!symbol_conf.initialized)
1912 strlist__delete(symbol_conf.sym_list);
1913 strlist__delete(symbol_conf.dso_list);
1914 strlist__delete(symbol_conf.comm_list);
1915 vmlinux_path__exit();
1916 symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
1917 symbol_conf.initialized = false;