21 #include <symbol/kallsyms.h>
22 #include <sys/utsname.h>
24 static int dso__load_kernel_sym(struct dso *dso, struct map *map,
25 symbol_filter_t filter);
26 static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
27 symbol_filter_t filter);
28 int vmlinux_path__nr_entries;
31 struct symbol_conf symbol_conf = {
33 .try_vmlinux_path = true,
36 .cumulate_callchain = true,
37 .show_hist_headers = true,
41 static enum dso_binary_type binary_type_symtab[] = {
42 DSO_BINARY_TYPE__KALLSYMS,
43 DSO_BINARY_TYPE__GUEST_KALLSYMS,
44 DSO_BINARY_TYPE__JAVA_JIT,
45 DSO_BINARY_TYPE__DEBUGLINK,
46 DSO_BINARY_TYPE__BUILD_ID_CACHE,
47 DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
48 DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
49 DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
50 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
51 DSO_BINARY_TYPE__GUEST_KMODULE,
52 DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE,
53 DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO,
54 DSO_BINARY_TYPE__NOT_FOUND,
57 #define DSO_BINARY_TYPE__SYMTAB_CNT ARRAY_SIZE(binary_type_symtab)
59 bool symbol_type__is_a(char symbol_type, enum map_type map_type)
61 symbol_type = toupper(symbol_type);
65 return symbol_type == 'T' || symbol_type == 'W';
67 return symbol_type == 'D';
73 static int prefix_underscores_count(const char *str)
75 const char *tail = str;
86 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 /* Choose the symbol with the longest name */
125 na = strlen(syma->name);
126 nb = strlen(symb->name);
132 /* Avoid "SyS" kernel syscall aliases */
133 if (na >= 3 && !strncmp(syma->name, "SyS", 3))
135 if (na >= 10 && !strncmp(syma->name, "compat_SyS", 10))
141 void symbols__fixup_duplicate(struct rb_root *symbols)
144 struct symbol *curr, *next;
146 nd = rb_first(symbols);
149 curr = rb_entry(nd, struct symbol, rb_node);
151 nd = rb_next(&curr->rb_node);
152 next = rb_entry(nd, struct symbol, rb_node);
157 if (curr->start != next->start)
160 if (choose_best_symbol(curr, next) == SYMBOL_A) {
161 rb_erase(&next->rb_node, symbols);
162 symbol__delete(next);
165 nd = rb_next(&curr->rb_node);
166 rb_erase(&curr->rb_node, symbols);
167 symbol__delete(curr);
172 void symbols__fixup_end(struct rb_root *symbols)
174 struct rb_node *nd, *prevnd = rb_first(symbols);
175 struct symbol *curr, *prev;
180 curr = rb_entry(prevnd, struct symbol, rb_node);
182 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
184 curr = rb_entry(nd, struct symbol, rb_node);
186 if (prev->end == prev->start && prev->end != curr->start)
187 prev->end = curr->start - 1;
191 if (curr->end == curr->start)
192 curr->end = roundup(curr->start, 4096);
195 void __map_groups__fixup_end(struct map_groups *mg, enum map_type type)
197 struct map *prev, *curr;
198 struct rb_node *nd, *prevnd = rb_first(&mg->maps[type]);
203 curr = rb_entry(prevnd, struct map, rb_node);
205 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
207 curr = rb_entry(nd, struct map, rb_node);
208 prev->end = curr->start - 1;
212 * We still haven't the actual symbols, so guess the
213 * last map final address.
218 struct symbol *symbol__new(u64 start, u64 len, u8 binding, const char *name)
220 size_t namelen = strlen(name) + 1;
221 struct symbol *sym = calloc(1, (symbol_conf.priv_size +
222 sizeof(*sym) + namelen));
226 if (symbol_conf.priv_size)
227 sym = ((void *)sym) + symbol_conf.priv_size;
230 sym->end = len ? start + len - 1 : start;
231 sym->binding = binding;
232 sym->namelen = namelen - 1;
234 pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n",
235 __func__, name, start, sym->end);
236 memcpy(sym->name, name, namelen);
241 void symbol__delete(struct symbol *sym)
243 free(((void *)sym) - symbol_conf.priv_size);
246 size_t symbol__fprintf(struct symbol *sym, FILE *fp)
248 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %c %s\n",
249 sym->start, sym->end,
250 sym->binding == STB_GLOBAL ? 'g' :
251 sym->binding == STB_LOCAL ? 'l' : 'w',
255 size_t symbol__fprintf_symname_offs(const struct symbol *sym,
256 const struct addr_location *al, FILE *fp)
258 unsigned long offset;
261 if (sym && sym->name) {
262 length = fprintf(fp, "%s", sym->name);
264 if (al->addr < sym->end)
265 offset = al->addr - sym->start;
267 offset = al->addr - al->map->start - sym->start;
268 length += fprintf(fp, "+0x%lx", offset);
272 return fprintf(fp, "[unknown]");
275 size_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp)
277 return symbol__fprintf_symname_offs(sym, NULL, fp);
280 void symbols__delete(struct rb_root *symbols)
283 struct rb_node *next = rb_first(symbols);
286 pos = rb_entry(next, struct symbol, rb_node);
287 next = rb_next(&pos->rb_node);
288 rb_erase(&pos->rb_node, symbols);
293 void symbols__insert(struct rb_root *symbols, struct symbol *sym)
295 struct rb_node **p = &symbols->rb_node;
296 struct rb_node *parent = NULL;
297 const u64 ip = sym->start;
302 s = rb_entry(parent, struct symbol, rb_node);
308 rb_link_node(&sym->rb_node, parent, p);
309 rb_insert_color(&sym->rb_node, symbols);
312 static struct symbol *symbols__find(struct rb_root *symbols, u64 ip)
319 n = symbols->rb_node;
322 struct symbol *s = rb_entry(n, struct symbol, rb_node);
326 else if (ip > s->end)
335 static struct symbol *symbols__first(struct rb_root *symbols)
337 struct rb_node *n = rb_first(symbols);
340 return rb_entry(n, struct symbol, rb_node);
345 static struct symbol *symbols__next(struct symbol *sym)
347 struct rb_node *n = rb_next(&sym->rb_node);
350 return rb_entry(n, struct symbol, rb_node);
355 struct symbol_name_rb_node {
356 struct rb_node rb_node;
360 static void symbols__insert_by_name(struct rb_root *symbols, struct symbol *sym)
362 struct rb_node **p = &symbols->rb_node;
363 struct rb_node *parent = NULL;
364 struct symbol_name_rb_node *symn, *s;
366 symn = container_of(sym, struct symbol_name_rb_node, sym);
370 s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
371 if (strcmp(sym->name, s->sym.name) < 0)
376 rb_link_node(&symn->rb_node, parent, p);
377 rb_insert_color(&symn->rb_node, symbols);
380 static void symbols__sort_by_name(struct rb_root *symbols,
381 struct rb_root *source)
385 for (nd = rb_first(source); nd; nd = rb_next(nd)) {
386 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
387 symbols__insert_by_name(symbols, pos);
391 static struct symbol *symbols__find_by_name(struct rb_root *symbols,
399 n = symbols->rb_node;
402 struct symbol_name_rb_node *s;
405 s = rb_entry(n, struct symbol_name_rb_node, rb_node);
406 cmp = strcmp(name, s->sym.name);
419 struct symbol *dso__find_symbol(struct dso *dso,
420 enum map_type type, u64 addr)
422 return symbols__find(&dso->symbols[type], addr);
425 struct symbol *dso__first_symbol(struct dso *dso, enum map_type type)
427 return symbols__first(&dso->symbols[type]);
430 struct symbol *dso__next_symbol(struct symbol *sym)
432 return symbols__next(sym);
435 struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
438 return symbols__find_by_name(&dso->symbol_names[type], name);
441 void dso__sort_by_name(struct dso *dso, enum map_type type)
443 dso__set_sorted_by_name(dso, type);
444 return symbols__sort_by_name(&dso->symbol_names[type],
445 &dso->symbols[type]);
448 size_t dso__fprintf_symbols_by_name(struct dso *dso,
449 enum map_type type, FILE *fp)
453 struct symbol_name_rb_node *pos;
455 for (nd = rb_first(&dso->symbol_names[type]); nd; nd = rb_next(nd)) {
456 pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
457 fprintf(fp, "%s\n", pos->sym.name);
463 int modules__parse(const char *filename, void *arg,
464 int (*process_module)(void *arg, const char *name,
472 file = fopen(filename, "r");
482 line_len = getline(&line, &n, file);
495 line[--line_len] = '\0'; /* \n */
497 sep = strrchr(line, 'x');
501 hex2u64(sep + 1, &start);
503 sep = strchr(line, ' ');
509 scnprintf(name, sizeof(name), "[%s]", line);
511 err = process_module(arg, name, start);
521 struct process_kallsyms_args {
526 bool symbol__is_idle(struct symbol *sym)
528 const char * const idle_symbols[] = {
536 "mwait_idle_with_hints",
538 "ppc64_runlatch_off",
539 "pseries_dedicated_idle_sleep",
548 for (i = 0; idle_symbols[i]; i++) {
549 if (!strcmp(idle_symbols[i], sym->name))
556 static int map__process_kallsym_symbol(void *arg, const char *name,
557 char type, u64 start)
560 struct process_kallsyms_args *a = arg;
561 struct rb_root *root = &a->dso->symbols[a->map->type];
563 if (!symbol_type__is_a(type, a->map->type))
567 * module symbols are not sorted so we add all
568 * symbols, setting length to 0, and rely on
569 * symbols__fixup_end() to fix it up.
571 sym = symbol__new(start, 0, kallsyms2elf_type(type), name);
575 * We will pass the symbols to the filter later, in
576 * map__split_kallsyms, when we have split the maps per module
578 symbols__insert(root, sym);
584 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
585 * so that we can in the next step set the symbol ->end address and then
586 * call kernel_maps__split_kallsyms.
588 static int dso__load_all_kallsyms(struct dso *dso, const char *filename,
591 struct process_kallsyms_args args = { .map = map, .dso = dso, };
592 return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
595 static int dso__split_kallsyms_for_kcore(struct dso *dso, struct map *map,
596 symbol_filter_t filter)
598 struct map_groups *kmaps = map__kmap(map)->kmaps;
599 struct map *curr_map;
601 int count = 0, moved = 0;
602 struct rb_root *root = &dso->symbols[map->type];
603 struct rb_node *next = rb_first(root);
608 pos = rb_entry(next, struct symbol, rb_node);
609 next = rb_next(&pos->rb_node);
611 module = strchr(pos->name, '\t');
615 curr_map = map_groups__find(kmaps, map->type, pos->start);
617 if (!curr_map || (filter && filter(curr_map, pos))) {
618 rb_erase(&pos->rb_node, root);
621 pos->start -= curr_map->start - curr_map->pgoff;
623 pos->end -= curr_map->start - curr_map->pgoff;
624 if (curr_map != map) {
625 rb_erase(&pos->rb_node, root);
627 &curr_map->dso->symbols[curr_map->type],
636 /* Symbols have been adjusted */
637 dso->adjust_symbols = 1;
639 return count + moved;
643 * Split the symbols into maps, making sure there are no overlaps, i.e. the
644 * kernel range is broken in several maps, named [kernel].N, as we don't have
645 * the original ELF section names vmlinux have.
647 static int dso__split_kallsyms(struct dso *dso, struct map *map, u64 delta,
648 symbol_filter_t filter)
650 struct map_groups *kmaps = map__kmap(map)->kmaps;
651 struct machine *machine = kmaps->machine;
652 struct map *curr_map = map;
654 int count = 0, moved = 0;
655 struct rb_root *root = &dso->symbols[map->type];
656 struct rb_node *next = rb_first(root);
657 int kernel_range = 0;
662 pos = rb_entry(next, struct symbol, rb_node);
663 next = rb_next(&pos->rb_node);
665 module = strchr(pos->name, '\t');
667 if (!symbol_conf.use_modules)
672 if (strcmp(curr_map->dso->short_name, module)) {
673 if (curr_map != map &&
674 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
675 machine__is_default_guest(machine)) {
677 * We assume all symbols of a module are
678 * continuous in * kallsyms, so curr_map
679 * points to a module and all its
680 * symbols are in its kmap. Mark it as
683 dso__set_loaded(curr_map->dso,
687 curr_map = map_groups__find_by_name(kmaps,
689 if (curr_map == NULL) {
690 pr_debug("%s/proc/{kallsyms,modules} "
691 "inconsistency while looking "
692 "for \"%s\" module!\n",
693 machine->root_dir, module);
698 if (curr_map->dso->loaded &&
699 !machine__is_default_guest(machine))
703 * So that we look just like we get from .ko files,
704 * i.e. not prelinked, relative to map->start.
706 pos->start = curr_map->map_ip(curr_map, pos->start);
707 pos->end = curr_map->map_ip(curr_map, pos->end);
708 } else if (curr_map != map) {
709 char dso_name[PATH_MAX];
713 /* Kernel was relocated at boot time */
723 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
724 snprintf(dso_name, sizeof(dso_name),
728 snprintf(dso_name, sizeof(dso_name),
732 ndso = dso__new(dso_name);
736 ndso->kernel = dso->kernel;
738 curr_map = map__new2(pos->start, ndso, map->type);
739 if (curr_map == NULL) {
744 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
745 map_groups__insert(kmaps, curr_map);
748 /* Kernel was relocated at boot time */
753 if (filter && filter(curr_map, pos)) {
754 discard_symbol: rb_erase(&pos->rb_node, root);
757 if (curr_map != map) {
758 rb_erase(&pos->rb_node, root);
759 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
766 if (curr_map != map &&
767 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
768 machine__is_default_guest(kmaps->machine)) {
769 dso__set_loaded(curr_map->dso, curr_map->type);
772 return count + moved;
775 bool symbol__restricted_filename(const char *filename,
776 const char *restricted_filename)
778 bool restricted = false;
780 if (symbol_conf.kptr_restrict) {
781 char *r = realpath(filename, NULL);
784 restricted = strcmp(r, restricted_filename) == 0;
794 struct rb_node rb_node;
799 static void add_module(struct module_info *mi, struct rb_root *modules)
801 struct rb_node **p = &modules->rb_node;
802 struct rb_node *parent = NULL;
803 struct module_info *m;
807 m = rb_entry(parent, struct module_info, rb_node);
808 if (strcmp(mi->name, m->name) < 0)
813 rb_link_node(&mi->rb_node, parent, p);
814 rb_insert_color(&mi->rb_node, modules);
817 static void delete_modules(struct rb_root *modules)
819 struct module_info *mi;
820 struct rb_node *next = rb_first(modules);
823 mi = rb_entry(next, struct module_info, rb_node);
824 next = rb_next(&mi->rb_node);
825 rb_erase(&mi->rb_node, modules);
831 static struct module_info *find_module(const char *name,
832 struct rb_root *modules)
834 struct rb_node *n = modules->rb_node;
837 struct module_info *m;
840 m = rb_entry(n, struct module_info, rb_node);
841 cmp = strcmp(name, m->name);
853 static int __read_proc_modules(void *arg, const char *name, u64 start)
855 struct rb_root *modules = arg;
856 struct module_info *mi;
858 mi = zalloc(sizeof(struct module_info));
862 mi->name = strdup(name);
870 add_module(mi, modules);
875 static int read_proc_modules(const char *filename, struct rb_root *modules)
877 if (symbol__restricted_filename(filename, "/proc/modules"))
880 if (modules__parse(filename, modules, __read_proc_modules)) {
881 delete_modules(modules);
888 int compare_proc_modules(const char *from, const char *to)
890 struct rb_root from_modules = RB_ROOT;
891 struct rb_root to_modules = RB_ROOT;
892 struct rb_node *from_node, *to_node;
893 struct module_info *from_m, *to_m;
896 if (read_proc_modules(from, &from_modules))
899 if (read_proc_modules(to, &to_modules))
900 goto out_delete_from;
902 from_node = rb_first(&from_modules);
903 to_node = rb_first(&to_modules);
908 from_m = rb_entry(from_node, struct module_info, rb_node);
909 to_m = rb_entry(to_node, struct module_info, rb_node);
911 if (from_m->start != to_m->start ||
912 strcmp(from_m->name, to_m->name))
915 from_node = rb_next(from_node);
916 to_node = rb_next(to_node);
919 if (!from_node && !to_node)
922 delete_modules(&to_modules);
924 delete_modules(&from_modules);
929 static int do_validate_kcore_modules(const char *filename, struct map *map,
930 struct map_groups *kmaps)
932 struct rb_root modules = RB_ROOT;
936 err = read_proc_modules(filename, &modules);
940 old_map = map_groups__first(kmaps, map->type);
942 struct map *next = map_groups__next(old_map);
943 struct module_info *mi;
945 if (old_map == map || old_map->start == map->start) {
951 /* Module must be in memory at the same address */
952 mi = find_module(old_map->dso->short_name, &modules);
953 if (!mi || mi->start != old_map->start) {
961 delete_modules(&modules);
966 * If kallsyms is referenced by name then we look for filename in the same
969 static bool filename_from_kallsyms_filename(char *filename,
970 const char *base_name,
971 const char *kallsyms_filename)
975 strcpy(filename, kallsyms_filename);
976 name = strrchr(filename, '/');
982 if (!strcmp(name, "kallsyms")) {
983 strcpy(name, base_name);
990 static int validate_kcore_modules(const char *kallsyms_filename,
993 struct map_groups *kmaps = map__kmap(map)->kmaps;
994 char modules_filename[PATH_MAX];
996 if (!filename_from_kallsyms_filename(modules_filename, "modules",
1000 if (do_validate_kcore_modules(modules_filename, map, kmaps))
1006 static int validate_kcore_addresses(const char *kallsyms_filename,
1009 struct kmap *kmap = map__kmap(map);
1011 if (kmap->ref_reloc_sym && kmap->ref_reloc_sym->name) {
1014 start = kallsyms__get_function_start(kallsyms_filename,
1015 kmap->ref_reloc_sym->name);
1016 if (start != kmap->ref_reloc_sym->addr)
1020 return validate_kcore_modules(kallsyms_filename, map);
1023 struct kcore_mapfn_data {
1026 struct list_head maps;
1029 static int kcore_mapfn(u64 start, u64 len, u64 pgoff, void *data)
1031 struct kcore_mapfn_data *md = data;
1034 map = map__new2(start, md->dso, md->type);
1038 map->end = map->start + len;
1041 list_add(&map->node, &md->maps);
1046 static int dso__load_kcore(struct dso *dso, struct map *map,
1047 const char *kallsyms_filename)
1049 struct map_groups *kmaps = map__kmap(map)->kmaps;
1050 struct machine *machine = kmaps->machine;
1051 struct kcore_mapfn_data md;
1052 struct map *old_map, *new_map, *replacement_map = NULL;
1055 char kcore_filename[PATH_MAX];
1058 /* This function requires that the map is the kernel map */
1059 if (map != machine->vmlinux_maps[map->type])
1062 if (!filename_from_kallsyms_filename(kcore_filename, "kcore",
1066 /* Modules and kernel must be present at their original addresses */
1067 if (validate_kcore_addresses(kallsyms_filename, map))
1071 md.type = map->type;
1072 INIT_LIST_HEAD(&md.maps);
1074 fd = open(kcore_filename, O_RDONLY);
1078 /* Read new maps into temporary lists */
1079 err = file__read_maps(fd, md.type == MAP__FUNCTION, kcore_mapfn, &md,
1083 dso->is_64_bit = is_64_bit;
1085 if (list_empty(&md.maps)) {
1090 /* Remove old maps */
1091 old_map = map_groups__first(kmaps, map->type);
1093 struct map *next = map_groups__next(old_map);
1096 map_groups__remove(kmaps, old_map);
1100 /* Find the kernel map using the first symbol */
1101 sym = dso__first_symbol(dso, map->type);
1102 list_for_each_entry(new_map, &md.maps, node) {
1103 if (sym && sym->start >= new_map->start &&
1104 sym->start < new_map->end) {
1105 replacement_map = new_map;
1110 if (!replacement_map)
1111 replacement_map = list_entry(md.maps.next, struct map, node);
1114 while (!list_empty(&md.maps)) {
1115 new_map = list_entry(md.maps.next, struct map, node);
1116 list_del(&new_map->node);
1117 if (new_map == replacement_map) {
1118 map->start = new_map->start;
1119 map->end = new_map->end;
1120 map->pgoff = new_map->pgoff;
1121 map->map_ip = new_map->map_ip;
1122 map->unmap_ip = new_map->unmap_ip;
1123 map__delete(new_map);
1124 /* Ensure maps are correctly ordered */
1125 map_groups__remove(kmaps, map);
1126 map_groups__insert(kmaps, map);
1128 map_groups__insert(kmaps, new_map);
1133 * Set the data type and long name so that kcore can be read via
1134 * dso__data_read_addr().
1136 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1137 dso->binary_type = DSO_BINARY_TYPE__GUEST_KCORE;
1139 dso->binary_type = DSO_BINARY_TYPE__KCORE;
1140 dso__set_long_name(dso, strdup(kcore_filename), true);
1144 if (map->type == MAP__FUNCTION)
1145 pr_debug("Using %s for kernel object code\n", kcore_filename);
1147 pr_debug("Using %s for kernel data\n", kcore_filename);
1152 while (!list_empty(&md.maps)) {
1153 map = list_entry(md.maps.next, struct map, node);
1154 list_del(&map->node);
1162 * If the kernel is relocated at boot time, kallsyms won't match. Compute the
1163 * delta based on the relocation reference symbol.
1165 static int kallsyms__delta(struct map *map, const char *filename, u64 *delta)
1167 struct kmap *kmap = map__kmap(map);
1170 if (!kmap->ref_reloc_sym || !kmap->ref_reloc_sym->name)
1173 addr = kallsyms__get_function_start(filename,
1174 kmap->ref_reloc_sym->name);
1178 *delta = addr - kmap->ref_reloc_sym->addr;
1182 int dso__load_kallsyms(struct dso *dso, const char *filename,
1183 struct map *map, symbol_filter_t filter)
1187 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
1190 if (dso__load_all_kallsyms(dso, filename, map) < 0)
1193 if (kallsyms__delta(map, filename, &delta))
1196 symbols__fixup_duplicate(&dso->symbols[map->type]);
1197 symbols__fixup_end(&dso->symbols[map->type]);
1199 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1200 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
1202 dso->symtab_type = DSO_BINARY_TYPE__KALLSYMS;
1204 if (!dso__load_kcore(dso, map, filename))
1205 return dso__split_kallsyms_for_kcore(dso, map, filter);
1207 return dso__split_kallsyms(dso, map, delta, filter);
1210 static int dso__load_perf_map(struct dso *dso, struct map *map,
1211 symbol_filter_t filter)
1218 file = fopen(dso->long_name, "r");
1222 while (!feof(file)) {
1227 line_len = getline(&line, &n, file);
1234 line[--line_len] = '\0'; /* \n */
1236 len = hex2u64(line, &start);
1239 if (len + 2 >= line_len)
1242 len += hex2u64(line + len, &size);
1245 if (len + 2 >= line_len)
1248 sym = symbol__new(start, size, STB_GLOBAL, line + len);
1251 goto out_delete_line;
1253 if (filter && filter(map, sym))
1254 symbol__delete(sym);
1256 symbols__insert(&dso->symbols[map->type], sym);
1272 static bool dso__is_compatible_symtab_type(struct dso *dso, bool kmod,
1273 enum dso_binary_type type)
1276 case DSO_BINARY_TYPE__JAVA_JIT:
1277 case DSO_BINARY_TYPE__DEBUGLINK:
1278 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
1279 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
1280 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
1281 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
1282 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
1283 return !kmod && dso->kernel == DSO_TYPE_USER;
1285 case DSO_BINARY_TYPE__KALLSYMS:
1286 case DSO_BINARY_TYPE__VMLINUX:
1287 case DSO_BINARY_TYPE__KCORE:
1288 return dso->kernel == DSO_TYPE_KERNEL;
1290 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
1291 case DSO_BINARY_TYPE__GUEST_VMLINUX:
1292 case DSO_BINARY_TYPE__GUEST_KCORE:
1293 return dso->kernel == DSO_TYPE_GUEST_KERNEL;
1295 case DSO_BINARY_TYPE__GUEST_KMODULE:
1296 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
1298 * kernel modules know their symtab type - it's set when
1299 * creating a module dso in machine__new_module().
1301 return kmod && dso->symtab_type == type;
1303 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
1306 case DSO_BINARY_TYPE__NOT_FOUND:
1312 int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter)
1317 struct machine *machine;
1318 char *root_dir = (char *) "";
1320 struct symsrc ss_[2];
1321 struct symsrc *syms_ss = NULL, *runtime_ss = NULL;
1324 dso__set_loaded(dso, map->type);
1326 if (dso->kernel == DSO_TYPE_KERNEL)
1327 return dso__load_kernel_sym(dso, map, filter);
1328 else if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1329 return dso__load_guest_kernel_sym(dso, map, filter);
1331 if (map->groups && map->groups->machine)
1332 machine = map->groups->machine;
1336 dso->adjust_symbols = 0;
1338 if (strncmp(dso->name, "/tmp/perf-", 10) == 0) {
1341 if (lstat(dso->name, &st) < 0)
1344 if (st.st_uid && (st.st_uid != geteuid())) {
1345 pr_warning("File %s not owned by current user or root, "
1346 "ignoring it.\n", dso->name);
1350 ret = dso__load_perf_map(dso, map, filter);
1351 dso->symtab_type = ret > 0 ? DSO_BINARY_TYPE__JAVA_JIT :
1352 DSO_BINARY_TYPE__NOT_FOUND;
1357 root_dir = machine->root_dir;
1359 name = malloc(PATH_MAX);
1363 kmod = dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
1364 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE;
1367 * Iterate over candidate debug images.
1368 * Keep track of "interesting" ones (those which have a symtab, dynsym,
1369 * and/or opd section) for processing.
1371 for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) {
1372 struct symsrc *ss = &ss_[ss_pos];
1373 bool next_slot = false;
1375 enum dso_binary_type symtab_type = binary_type_symtab[i];
1377 if (!dso__is_compatible_symtab_type(dso, kmod, symtab_type))
1380 if (dso__read_binary_type_filename(dso, symtab_type,
1381 root_dir, name, PATH_MAX))
1384 /* Name is now the name of the next image to try */
1385 if (symsrc__init(ss, dso, name, symtab_type) < 0)
1388 if (!syms_ss && symsrc__has_symtab(ss)) {
1391 if (!dso->symsrc_filename)
1392 dso->symsrc_filename = strdup(name);
1395 if (!runtime_ss && symsrc__possibly_runtime(ss)) {
1403 if (syms_ss && runtime_ss)
1406 symsrc__destroy(ss);
1411 if (!runtime_ss && !syms_ss)
1414 if (runtime_ss && !syms_ss) {
1415 syms_ss = runtime_ss;
1418 /* We'll have to hope for the best */
1419 if (!runtime_ss && syms_ss)
1420 runtime_ss = syms_ss;
1423 ret = dso__load_sym(dso, map, syms_ss, runtime_ss, filter, kmod);
1430 nr_plt = dso__synthesize_plt_symbols(dso, runtime_ss, map, filter);
1435 for (; ss_pos > 0; ss_pos--)
1436 symsrc__destroy(&ss_[ss_pos - 1]);
1439 if (ret < 0 && strstr(dso->name, " (deleted)") != NULL)
1444 struct map *map_groups__find_by_name(struct map_groups *mg,
1445 enum map_type type, const char *name)
1449 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
1450 struct map *map = rb_entry(nd, struct map, rb_node);
1452 if (map->dso && strcmp(map->dso->short_name, name) == 0)
1459 int dso__load_vmlinux(struct dso *dso, struct map *map,
1460 const char *vmlinux, bool vmlinux_allocated,
1461 symbol_filter_t filter)
1465 char symfs_vmlinux[PATH_MAX];
1466 enum dso_binary_type symtab_type;
1468 if (vmlinux[0] == '/')
1469 snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s", vmlinux);
1471 snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s%s",
1472 symbol_conf.symfs, vmlinux);
1474 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1475 symtab_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
1477 symtab_type = DSO_BINARY_TYPE__VMLINUX;
1479 if (symsrc__init(&ss, dso, symfs_vmlinux, symtab_type))
1482 err = dso__load_sym(dso, map, &ss, &ss, filter, 0);
1483 symsrc__destroy(&ss);
1486 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1487 dso->binary_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
1489 dso->binary_type = DSO_BINARY_TYPE__VMLINUX;
1490 dso__set_long_name(dso, vmlinux, vmlinux_allocated);
1491 dso__set_loaded(dso, map->type);
1492 pr_debug("Using %s for symbols\n", symfs_vmlinux);
1498 int dso__load_vmlinux_path(struct dso *dso, struct map *map,
1499 symbol_filter_t filter)
1504 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1505 vmlinux_path__nr_entries + 1);
1507 filename = dso__build_id_filename(dso, NULL, 0);
1508 if (filename != NULL) {
1509 err = dso__load_vmlinux(dso, map, filename, true, filter);
1515 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
1516 err = dso__load_vmlinux(dso, map, vmlinux_path[i], false, filter);
1524 static int find_matching_kcore(struct map *map, char *dir, size_t dir_sz)
1526 char kallsyms_filename[PATH_MAX];
1527 struct dirent *dent;
1539 if (dent->d_type != DT_DIR)
1541 scnprintf(kallsyms_filename, sizeof(kallsyms_filename),
1542 "%s/%s/kallsyms", dir, dent->d_name);
1543 if (!validate_kcore_addresses(kallsyms_filename, map)) {
1544 strlcpy(dir, kallsyms_filename, dir_sz);
1555 static char *dso__find_kallsyms(struct dso *dso, struct map *map)
1557 u8 host_build_id[BUILD_ID_SIZE];
1558 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1559 bool is_host = false;
1560 char path[PATH_MAX];
1562 if (!dso->has_build_id) {
1564 * Last resort, if we don't have a build-id and couldn't find
1565 * any vmlinux file, try the running kernel kallsyms table.
1570 if (sysfs__read_build_id("/sys/kernel/notes", host_build_id,
1571 sizeof(host_build_id)) == 0)
1572 is_host = dso__build_id_equal(dso, host_build_id);
1574 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1576 scnprintf(path, sizeof(path), "%s/[kernel.kcore]/%s", buildid_dir,
1579 /* Use /proc/kallsyms if possible */
1584 /* If no cached kcore go with /proc/kallsyms */
1591 * Do not check the build-id cache, until we know we cannot use
1594 fd = open("/proc/kcore", O_RDONLY);
1597 /* If module maps match go with /proc/kallsyms */
1598 if (!validate_kcore_addresses("/proc/kallsyms", map))
1602 /* Find kallsyms in build-id cache with kcore */
1603 if (!find_matching_kcore(map, path, sizeof(path)))
1604 return strdup(path);
1609 /* Find kallsyms in build-id cache with kcore */
1610 if (!find_matching_kcore(map, path, sizeof(path)))
1611 return strdup(path);
1613 scnprintf(path, sizeof(path), "%s/[kernel.kallsyms]/%s",
1614 buildid_dir, sbuild_id);
1616 if (access(path, F_OK)) {
1617 pr_err("No kallsyms or vmlinux with build-id %s was found\n",
1622 return strdup(path);
1625 return strdup("/proc/kallsyms");
1628 static int dso__load_kernel_sym(struct dso *dso, struct map *map,
1629 symbol_filter_t filter)
1632 const char *kallsyms_filename = NULL;
1633 char *kallsyms_allocated_filename = NULL;
1635 * Step 1: if the user specified a kallsyms or vmlinux filename, use
1636 * it and only it, reporting errors to the user if it cannot be used.
1638 * For instance, try to analyse an ARM perf.data file _without_ a
1639 * build-id, or if the user specifies the wrong path to the right
1640 * vmlinux file, obviously we can't fallback to another vmlinux (a
1641 * x86_86 one, on the machine where analysis is being performed, say),
1642 * or worse, /proc/kallsyms.
1644 * If the specified file _has_ a build-id and there is a build-id
1645 * section in the perf.data file, we will still do the expected
1646 * validation in dso__load_vmlinux and will bail out if they don't
1649 if (symbol_conf.kallsyms_name != NULL) {
1650 kallsyms_filename = symbol_conf.kallsyms_name;
1654 if (!symbol_conf.ignore_vmlinux && symbol_conf.vmlinux_name != NULL) {
1655 return dso__load_vmlinux(dso, map, symbol_conf.vmlinux_name,
1659 if (!symbol_conf.ignore_vmlinux && vmlinux_path != NULL) {
1660 err = dso__load_vmlinux_path(dso, map, filter);
1665 /* do not try local files if a symfs was given */
1666 if (symbol_conf.symfs[0] != 0)
1669 kallsyms_allocated_filename = dso__find_kallsyms(dso, map);
1670 if (!kallsyms_allocated_filename)
1673 kallsyms_filename = kallsyms_allocated_filename;
1676 err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
1678 pr_debug("Using %s for symbols\n", kallsyms_filename);
1679 free(kallsyms_allocated_filename);
1681 if (err > 0 && !dso__is_kcore(dso)) {
1682 dso->binary_type = DSO_BINARY_TYPE__KALLSYMS;
1683 dso__set_long_name(dso, "[kernel.kallsyms]", false);
1684 map__fixup_start(map);
1685 map__fixup_end(map);
1691 static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
1692 symbol_filter_t filter)
1695 const char *kallsyms_filename = NULL;
1696 struct machine *machine;
1697 char path[PATH_MAX];
1700 pr_debug("Guest kernel map hasn't the point to groups\n");
1703 machine = map->groups->machine;
1705 if (machine__is_default_guest(machine)) {
1707 * if the user specified a vmlinux filename, use it and only
1708 * it, reporting errors to the user if it cannot be used.
1709 * Or use file guest_kallsyms inputted by user on commandline
1711 if (symbol_conf.default_guest_vmlinux_name != NULL) {
1712 err = dso__load_vmlinux(dso, map,
1713 symbol_conf.default_guest_vmlinux_name,
1718 kallsyms_filename = symbol_conf.default_guest_kallsyms;
1719 if (!kallsyms_filename)
1722 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
1723 kallsyms_filename = path;
1726 err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
1728 pr_debug("Using %s for symbols\n", kallsyms_filename);
1729 if (err > 0 && !dso__is_kcore(dso)) {
1730 dso->binary_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
1731 machine__mmap_name(machine, path, sizeof(path));
1732 dso__set_long_name(dso, strdup(path), true);
1733 map__fixup_start(map);
1734 map__fixup_end(map);
1740 static void vmlinux_path__exit(void)
1742 while (--vmlinux_path__nr_entries >= 0)
1743 zfree(&vmlinux_path[vmlinux_path__nr_entries]);
1745 zfree(&vmlinux_path);
1748 static int vmlinux_path__init(void)
1753 vmlinux_path = malloc(sizeof(char *) * 5);
1754 if (vmlinux_path == NULL)
1757 vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
1758 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1760 ++vmlinux_path__nr_entries;
1761 vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
1762 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1764 ++vmlinux_path__nr_entries;
1766 /* only try running kernel version if no symfs was given */
1767 if (symbol_conf.symfs[0] != 0)
1770 if (uname(&uts) < 0)
1773 snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
1774 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1775 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1777 ++vmlinux_path__nr_entries;
1778 snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
1779 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1780 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1782 ++vmlinux_path__nr_entries;
1783 snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
1785 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1786 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1788 ++vmlinux_path__nr_entries;
1793 vmlinux_path__exit();
1797 int setup_list(struct strlist **list, const char *list_str,
1798 const char *list_name)
1800 if (list_str == NULL)
1803 *list = strlist__new(true, list_str);
1805 pr_err("problems parsing %s list\n", list_name);
1811 static bool symbol__read_kptr_restrict(void)
1815 if (geteuid() != 0) {
1816 FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
1820 if (fgets(line, sizeof(line), fp) != NULL)
1821 value = atoi(line) != 0;
1830 int symbol__init(void)
1834 if (symbol_conf.initialized)
1837 symbol_conf.priv_size = PERF_ALIGN(symbol_conf.priv_size, sizeof(u64));
1841 if (symbol_conf.sort_by_name)
1842 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
1843 sizeof(struct symbol));
1845 if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
1848 if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
1849 pr_err("'.' is the only non valid --field-separator argument\n");
1853 if (setup_list(&symbol_conf.dso_list,
1854 symbol_conf.dso_list_str, "dso") < 0)
1857 if (setup_list(&symbol_conf.comm_list,
1858 symbol_conf.comm_list_str, "comm") < 0)
1859 goto out_free_dso_list;
1861 if (setup_list(&symbol_conf.sym_list,
1862 symbol_conf.sym_list_str, "symbol") < 0)
1863 goto out_free_comm_list;
1866 * A path to symbols of "/" is identical to ""
1867 * reset here for simplicity.
1869 symfs = realpath(symbol_conf.symfs, NULL);
1871 symfs = symbol_conf.symfs;
1872 if (strcmp(symfs, "/") == 0)
1873 symbol_conf.symfs = "";
1874 if (symfs != symbol_conf.symfs)
1875 free((void *)symfs);
1877 symbol_conf.kptr_restrict = symbol__read_kptr_restrict();
1879 symbol_conf.initialized = true;
1883 strlist__delete(symbol_conf.comm_list);
1885 strlist__delete(symbol_conf.dso_list);
1889 void symbol__exit(void)
1891 if (!symbol_conf.initialized)
1893 strlist__delete(symbol_conf.sym_list);
1894 strlist__delete(symbol_conf.dso_list);
1895 strlist__delete(symbol_conf.comm_list);
1896 vmlinux_path__exit();
1897 symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
1898 symbol_conf.initialized = false;