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__GUEST_KMODULE_COMP,
55 DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE,
56 DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP,
57 DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO,
58 DSO_BINARY_TYPE__NOT_FOUND,
61 #define DSO_BINARY_TYPE__SYMTAB_CNT ARRAY_SIZE(binary_type_symtab)
63 bool symbol_type__is_a(char symbol_type, enum map_type map_type)
65 symbol_type = toupper(symbol_type);
69 return symbol_type == 'T' || symbol_type == 'W';
71 return symbol_type == 'D';
77 static int prefix_underscores_count(const char *str)
79 const char *tail = str;
90 static int choose_best_symbol(struct symbol *syma, struct symbol *symb)
96 /* Prefer a symbol with non zero length */
97 a = syma->end - syma->start;
98 b = symb->end - symb->start;
99 if ((b == 0) && (a > 0))
101 else if ((a == 0) && (b > 0))
104 /* Prefer a non weak symbol over a weak one */
105 a = syma->binding == STB_WEAK;
106 b = symb->binding == STB_WEAK;
112 /* Prefer a global symbol over a non global one */
113 a = syma->binding == STB_GLOBAL;
114 b = symb->binding == STB_GLOBAL;
120 /* Prefer a symbol with less underscores */
121 a = prefix_underscores_count(syma->name);
122 b = prefix_underscores_count(symb->name);
128 /* Choose the symbol with the longest name */
129 na = strlen(syma->name);
130 nb = strlen(symb->name);
136 /* Avoid "SyS" kernel syscall aliases */
137 if (na >= 3 && !strncmp(syma->name, "SyS", 3))
139 if (na >= 10 && !strncmp(syma->name, "compat_SyS", 10))
145 void symbols__fixup_duplicate(struct rb_root *symbols)
148 struct symbol *curr, *next;
150 nd = rb_first(symbols);
153 curr = rb_entry(nd, struct symbol, rb_node);
155 nd = rb_next(&curr->rb_node);
156 next = rb_entry(nd, struct symbol, rb_node);
161 if (curr->start != next->start)
164 if (choose_best_symbol(curr, next) == SYMBOL_A) {
165 rb_erase(&next->rb_node, symbols);
166 symbol__delete(next);
169 nd = rb_next(&curr->rb_node);
170 rb_erase(&curr->rb_node, symbols);
171 symbol__delete(curr);
176 void symbols__fixup_end(struct rb_root *symbols)
178 struct rb_node *nd, *prevnd = rb_first(symbols);
179 struct symbol *curr, *prev;
184 curr = rb_entry(prevnd, struct symbol, rb_node);
186 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
188 curr = rb_entry(nd, struct symbol, rb_node);
190 if (prev->end == prev->start && prev->end != curr->start)
191 prev->end = curr->start;
195 if (curr->end == curr->start)
196 curr->end = roundup(curr->start, 4096);
199 void __map_groups__fixup_end(struct map_groups *mg, enum map_type type)
201 struct map *prev, *curr;
202 struct rb_node *nd, *prevnd = rb_first(&mg->maps[type]);
207 curr = rb_entry(prevnd, struct map, rb_node);
209 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
211 curr = rb_entry(nd, struct map, rb_node);
212 prev->end = curr->start;
216 * We still haven't the actual symbols, so guess the
217 * last map final address.
222 struct symbol *symbol__new(u64 start, u64 len, u8 binding, const char *name)
224 size_t namelen = strlen(name) + 1;
225 struct symbol *sym = calloc(1, (symbol_conf.priv_size +
226 sizeof(*sym) + namelen));
230 if (symbol_conf.priv_size)
231 sym = ((void *)sym) + symbol_conf.priv_size;
234 sym->end = len ? start + len : start;
235 sym->binding = binding;
236 sym->namelen = namelen - 1;
238 pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n",
239 __func__, name, start, sym->end);
240 memcpy(sym->name, name, namelen);
245 void symbol__delete(struct symbol *sym)
247 free(((void *)sym) - symbol_conf.priv_size);
250 size_t symbol__fprintf(struct symbol *sym, FILE *fp)
252 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %c %s\n",
253 sym->start, sym->end,
254 sym->binding == STB_GLOBAL ? 'g' :
255 sym->binding == STB_LOCAL ? 'l' : 'w',
259 size_t symbol__fprintf_symname_offs(const struct symbol *sym,
260 const struct addr_location *al, FILE *fp)
262 unsigned long offset;
265 if (sym && sym->name) {
266 length = fprintf(fp, "%s", sym->name);
268 if (al->addr < sym->end)
269 offset = al->addr - sym->start;
271 offset = al->addr - al->map->start - sym->start;
272 length += fprintf(fp, "+0x%lx", offset);
276 return fprintf(fp, "[unknown]");
279 size_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp)
281 return symbol__fprintf_symname_offs(sym, NULL, fp);
284 void symbols__delete(struct rb_root *symbols)
287 struct rb_node *next = rb_first(symbols);
290 pos = rb_entry(next, struct symbol, rb_node);
291 next = rb_next(&pos->rb_node);
292 rb_erase(&pos->rb_node, symbols);
297 void symbols__insert(struct rb_root *symbols, struct symbol *sym)
299 struct rb_node **p = &symbols->rb_node;
300 struct rb_node *parent = NULL;
301 const u64 ip = sym->start;
306 s = rb_entry(parent, struct symbol, rb_node);
312 rb_link_node(&sym->rb_node, parent, p);
313 rb_insert_color(&sym->rb_node, symbols);
316 static struct symbol *symbols__find(struct rb_root *symbols, u64 ip)
323 n = symbols->rb_node;
326 struct symbol *s = rb_entry(n, struct symbol, rb_node);
330 else if (ip >= s->end)
339 static struct symbol *symbols__first(struct rb_root *symbols)
341 struct rb_node *n = rb_first(symbols);
344 return rb_entry(n, struct symbol, rb_node);
349 static struct symbol *symbols__next(struct symbol *sym)
351 struct rb_node *n = rb_next(&sym->rb_node);
354 return rb_entry(n, struct symbol, rb_node);
359 struct symbol_name_rb_node {
360 struct rb_node rb_node;
364 static void symbols__insert_by_name(struct rb_root *symbols, struct symbol *sym)
366 struct rb_node **p = &symbols->rb_node;
367 struct rb_node *parent = NULL;
368 struct symbol_name_rb_node *symn, *s;
370 symn = container_of(sym, struct symbol_name_rb_node, sym);
374 s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
375 if (strcmp(sym->name, s->sym.name) < 0)
380 rb_link_node(&symn->rb_node, parent, p);
381 rb_insert_color(&symn->rb_node, symbols);
384 static void symbols__sort_by_name(struct rb_root *symbols,
385 struct rb_root *source)
389 for (nd = rb_first(source); nd; nd = rb_next(nd)) {
390 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
391 symbols__insert_by_name(symbols, pos);
395 static struct symbol *symbols__find_by_name(struct rb_root *symbols,
403 n = symbols->rb_node;
406 struct symbol_name_rb_node *s;
409 s = rb_entry(n, struct symbol_name_rb_node, rb_node);
410 cmp = strcmp(name, s->sym.name);
423 struct symbol *dso__find_symbol(struct dso *dso,
424 enum map_type type, u64 addr)
426 return symbols__find(&dso->symbols[type], addr);
429 struct symbol *dso__first_symbol(struct dso *dso, enum map_type type)
431 return symbols__first(&dso->symbols[type]);
434 struct symbol *dso__next_symbol(struct symbol *sym)
436 return symbols__next(sym);
439 struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
442 return symbols__find_by_name(&dso->symbol_names[type], name);
445 void dso__sort_by_name(struct dso *dso, enum map_type type)
447 dso__set_sorted_by_name(dso, type);
448 return symbols__sort_by_name(&dso->symbol_names[type],
449 &dso->symbols[type]);
452 size_t dso__fprintf_symbols_by_name(struct dso *dso,
453 enum map_type type, FILE *fp)
457 struct symbol_name_rb_node *pos;
459 for (nd = rb_first(&dso->symbol_names[type]); nd; nd = rb_next(nd)) {
460 pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
461 fprintf(fp, "%s\n", pos->sym.name);
467 int modules__parse(const char *filename, void *arg,
468 int (*process_module)(void *arg, const char *name,
476 file = fopen(filename, "r");
486 line_len = getline(&line, &n, file);
499 line[--line_len] = '\0'; /* \n */
501 sep = strrchr(line, 'x');
505 hex2u64(sep + 1, &start);
507 sep = strchr(line, ' ');
513 scnprintf(name, sizeof(name), "[%s]", line);
515 err = process_module(arg, name, start);
525 struct process_kallsyms_args {
531 * These are symbols in the kernel image, so make sure that
532 * sym is from a kernel DSO.
534 bool symbol__is_idle(struct symbol *sym)
536 const char * const idle_symbols[] = {
545 "mwait_idle_with_hints",
547 "ppc64_runlatch_off",
548 "pseries_dedicated_idle_sleep",
557 for (i = 0; idle_symbols[i]; i++) {
558 if (!strcmp(idle_symbols[i], sym->name))
565 static int map__process_kallsym_symbol(void *arg, const char *name,
566 char type, u64 start)
569 struct process_kallsyms_args *a = arg;
570 struct rb_root *root = &a->dso->symbols[a->map->type];
572 if (!symbol_type__is_a(type, a->map->type))
576 * module symbols are not sorted so we add all
577 * symbols, setting length to 0, and rely on
578 * symbols__fixup_end() to fix it up.
580 sym = symbol__new(start, 0, kallsyms2elf_type(type), name);
584 * We will pass the symbols to the filter later, in
585 * map__split_kallsyms, when we have split the maps per module
587 symbols__insert(root, sym);
593 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
594 * so that we can in the next step set the symbol ->end address and then
595 * call kernel_maps__split_kallsyms.
597 static int dso__load_all_kallsyms(struct dso *dso, const char *filename,
600 struct process_kallsyms_args args = { .map = map, .dso = dso, };
601 return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
604 static int dso__split_kallsyms_for_kcore(struct dso *dso, struct map *map,
605 symbol_filter_t filter)
607 struct map_groups *kmaps = map__kmap(map)->kmaps;
608 struct map *curr_map;
610 int count = 0, moved = 0;
611 struct rb_root *root = &dso->symbols[map->type];
612 struct rb_node *next = rb_first(root);
617 pos = rb_entry(next, struct symbol, rb_node);
618 next = rb_next(&pos->rb_node);
620 module = strchr(pos->name, '\t');
624 curr_map = map_groups__find(kmaps, map->type, pos->start);
626 if (!curr_map || (filter && filter(curr_map, pos))) {
627 rb_erase(&pos->rb_node, root);
630 pos->start -= curr_map->start - curr_map->pgoff;
632 pos->end -= curr_map->start - curr_map->pgoff;
633 if (curr_map != map) {
634 rb_erase(&pos->rb_node, root);
636 &curr_map->dso->symbols[curr_map->type],
645 /* Symbols have been adjusted */
646 dso->adjust_symbols = 1;
648 return count + moved;
652 * Split the symbols into maps, making sure there are no overlaps, i.e. the
653 * kernel range is broken in several maps, named [kernel].N, as we don't have
654 * the original ELF section names vmlinux have.
656 static int dso__split_kallsyms(struct dso *dso, struct map *map, u64 delta,
657 symbol_filter_t filter)
659 struct map_groups *kmaps = map__kmap(map)->kmaps;
660 struct machine *machine = kmaps->machine;
661 struct map *curr_map = map;
663 int count = 0, moved = 0;
664 struct rb_root *root = &dso->symbols[map->type];
665 struct rb_node *next = rb_first(root);
666 int kernel_range = 0;
671 pos = rb_entry(next, struct symbol, rb_node);
672 next = rb_next(&pos->rb_node);
674 module = strchr(pos->name, '\t');
676 if (!symbol_conf.use_modules)
681 if (strcmp(curr_map->dso->short_name, module)) {
682 if (curr_map != map &&
683 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
684 machine__is_default_guest(machine)) {
686 * We assume all symbols of a module are
687 * continuous in * kallsyms, so curr_map
688 * points to a module and all its
689 * symbols are in its kmap. Mark it as
692 dso__set_loaded(curr_map->dso,
696 curr_map = map_groups__find_by_name(kmaps,
698 if (curr_map == NULL) {
699 pr_debug("%s/proc/{kallsyms,modules} "
700 "inconsistency while looking "
701 "for \"%s\" module!\n",
702 machine->root_dir, module);
707 if (curr_map->dso->loaded &&
708 !machine__is_default_guest(machine))
712 * So that we look just like we get from .ko files,
713 * i.e. not prelinked, relative to map->start.
715 pos->start = curr_map->map_ip(curr_map, pos->start);
716 pos->end = curr_map->map_ip(curr_map, pos->end);
717 } else if (curr_map != map) {
718 char dso_name[PATH_MAX];
722 /* Kernel was relocated at boot time */
732 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
733 snprintf(dso_name, sizeof(dso_name),
737 snprintf(dso_name, sizeof(dso_name),
741 ndso = dso__new(dso_name);
745 ndso->kernel = dso->kernel;
747 curr_map = map__new2(pos->start, ndso, map->type);
748 if (curr_map == NULL) {
753 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
754 map_groups__insert(kmaps, curr_map);
757 /* Kernel was relocated at boot time */
762 if (filter && filter(curr_map, pos)) {
763 discard_symbol: rb_erase(&pos->rb_node, root);
766 if (curr_map != map) {
767 rb_erase(&pos->rb_node, root);
768 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
775 if (curr_map != map &&
776 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
777 machine__is_default_guest(kmaps->machine)) {
778 dso__set_loaded(curr_map->dso, curr_map->type);
781 return count + moved;
784 bool symbol__restricted_filename(const char *filename,
785 const char *restricted_filename)
787 bool restricted = false;
789 if (symbol_conf.kptr_restrict) {
790 char *r = realpath(filename, NULL);
793 restricted = strcmp(r, restricted_filename) == 0;
803 struct rb_node rb_node;
808 static void add_module(struct module_info *mi, struct rb_root *modules)
810 struct rb_node **p = &modules->rb_node;
811 struct rb_node *parent = NULL;
812 struct module_info *m;
816 m = rb_entry(parent, struct module_info, rb_node);
817 if (strcmp(mi->name, m->name) < 0)
822 rb_link_node(&mi->rb_node, parent, p);
823 rb_insert_color(&mi->rb_node, modules);
826 static void delete_modules(struct rb_root *modules)
828 struct module_info *mi;
829 struct rb_node *next = rb_first(modules);
832 mi = rb_entry(next, struct module_info, rb_node);
833 next = rb_next(&mi->rb_node);
834 rb_erase(&mi->rb_node, modules);
840 static struct module_info *find_module(const char *name,
841 struct rb_root *modules)
843 struct rb_node *n = modules->rb_node;
846 struct module_info *m;
849 m = rb_entry(n, struct module_info, rb_node);
850 cmp = strcmp(name, m->name);
862 static int __read_proc_modules(void *arg, const char *name, u64 start)
864 struct rb_root *modules = arg;
865 struct module_info *mi;
867 mi = zalloc(sizeof(struct module_info));
871 mi->name = strdup(name);
879 add_module(mi, modules);
884 static int read_proc_modules(const char *filename, struct rb_root *modules)
886 if (symbol__restricted_filename(filename, "/proc/modules"))
889 if (modules__parse(filename, modules, __read_proc_modules)) {
890 delete_modules(modules);
897 int compare_proc_modules(const char *from, const char *to)
899 struct rb_root from_modules = RB_ROOT;
900 struct rb_root to_modules = RB_ROOT;
901 struct rb_node *from_node, *to_node;
902 struct module_info *from_m, *to_m;
905 if (read_proc_modules(from, &from_modules))
908 if (read_proc_modules(to, &to_modules))
909 goto out_delete_from;
911 from_node = rb_first(&from_modules);
912 to_node = rb_first(&to_modules);
917 from_m = rb_entry(from_node, struct module_info, rb_node);
918 to_m = rb_entry(to_node, struct module_info, rb_node);
920 if (from_m->start != to_m->start ||
921 strcmp(from_m->name, to_m->name))
924 from_node = rb_next(from_node);
925 to_node = rb_next(to_node);
928 if (!from_node && !to_node)
931 delete_modules(&to_modules);
933 delete_modules(&from_modules);
938 static int do_validate_kcore_modules(const char *filename, struct map *map,
939 struct map_groups *kmaps)
941 struct rb_root modules = RB_ROOT;
945 err = read_proc_modules(filename, &modules);
949 old_map = map_groups__first(kmaps, map->type);
951 struct map *next = map_groups__next(old_map);
952 struct module_info *mi;
954 if (old_map == map || old_map->start == map->start) {
960 /* Module must be in memory at the same address */
961 mi = find_module(old_map->dso->short_name, &modules);
962 if (!mi || mi->start != old_map->start) {
970 delete_modules(&modules);
975 * If kallsyms is referenced by name then we look for filename in the same
978 static bool filename_from_kallsyms_filename(char *filename,
979 const char *base_name,
980 const char *kallsyms_filename)
984 strcpy(filename, kallsyms_filename);
985 name = strrchr(filename, '/');
991 if (!strcmp(name, "kallsyms")) {
992 strcpy(name, base_name);
999 static int validate_kcore_modules(const char *kallsyms_filename,
1002 struct map_groups *kmaps = map__kmap(map)->kmaps;
1003 char modules_filename[PATH_MAX];
1005 if (!filename_from_kallsyms_filename(modules_filename, "modules",
1009 if (do_validate_kcore_modules(modules_filename, map, kmaps))
1015 static int validate_kcore_addresses(const char *kallsyms_filename,
1018 struct kmap *kmap = map__kmap(map);
1020 if (kmap->ref_reloc_sym && kmap->ref_reloc_sym->name) {
1023 start = kallsyms__get_function_start(kallsyms_filename,
1024 kmap->ref_reloc_sym->name);
1025 if (start != kmap->ref_reloc_sym->addr)
1029 return validate_kcore_modules(kallsyms_filename, map);
1032 struct kcore_mapfn_data {
1035 struct list_head maps;
1038 static int kcore_mapfn(u64 start, u64 len, u64 pgoff, void *data)
1040 struct kcore_mapfn_data *md = data;
1043 map = map__new2(start, md->dso, md->type);
1047 map->end = map->start + len;
1050 list_add(&map->node, &md->maps);
1055 static int dso__load_kcore(struct dso *dso, struct map *map,
1056 const char *kallsyms_filename)
1058 struct map_groups *kmaps = map__kmap(map)->kmaps;
1059 struct machine *machine = kmaps->machine;
1060 struct kcore_mapfn_data md;
1061 struct map *old_map, *new_map, *replacement_map = NULL;
1064 char kcore_filename[PATH_MAX];
1067 /* This function requires that the map is the kernel map */
1068 if (map != machine->vmlinux_maps[map->type])
1071 if (!filename_from_kallsyms_filename(kcore_filename, "kcore",
1075 /* Modules and kernel must be present at their original addresses */
1076 if (validate_kcore_addresses(kallsyms_filename, map))
1080 md.type = map->type;
1081 INIT_LIST_HEAD(&md.maps);
1083 fd = open(kcore_filename, O_RDONLY);
1087 /* Read new maps into temporary lists */
1088 err = file__read_maps(fd, md.type == MAP__FUNCTION, kcore_mapfn, &md,
1092 dso->is_64_bit = is_64_bit;
1094 if (list_empty(&md.maps)) {
1099 /* Remove old maps */
1100 old_map = map_groups__first(kmaps, map->type);
1102 struct map *next = map_groups__next(old_map);
1105 map_groups__remove(kmaps, old_map);
1109 /* Find the kernel map using the first symbol */
1110 sym = dso__first_symbol(dso, map->type);
1111 list_for_each_entry(new_map, &md.maps, node) {
1112 if (sym && sym->start >= new_map->start &&
1113 sym->start < new_map->end) {
1114 replacement_map = new_map;
1119 if (!replacement_map)
1120 replacement_map = list_entry(md.maps.next, struct map, node);
1123 while (!list_empty(&md.maps)) {
1124 new_map = list_entry(md.maps.next, struct map, node);
1125 list_del(&new_map->node);
1126 if (new_map == replacement_map) {
1127 map->start = new_map->start;
1128 map->end = new_map->end;
1129 map->pgoff = new_map->pgoff;
1130 map->map_ip = new_map->map_ip;
1131 map->unmap_ip = new_map->unmap_ip;
1132 map__delete(new_map);
1133 /* Ensure maps are correctly ordered */
1134 map_groups__remove(kmaps, map);
1135 map_groups__insert(kmaps, map);
1137 map_groups__insert(kmaps, new_map);
1142 * Set the data type and long name so that kcore can be read via
1143 * dso__data_read_addr().
1145 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1146 dso->binary_type = DSO_BINARY_TYPE__GUEST_KCORE;
1148 dso->binary_type = DSO_BINARY_TYPE__KCORE;
1149 dso__set_long_name(dso, strdup(kcore_filename), true);
1153 if (map->type == MAP__FUNCTION)
1154 pr_debug("Using %s for kernel object code\n", kcore_filename);
1156 pr_debug("Using %s for kernel data\n", kcore_filename);
1161 while (!list_empty(&md.maps)) {
1162 map = list_entry(md.maps.next, struct map, node);
1163 list_del(&map->node);
1171 * If the kernel is relocated at boot time, kallsyms won't match. Compute the
1172 * delta based on the relocation reference symbol.
1174 static int kallsyms__delta(struct map *map, const char *filename, u64 *delta)
1176 struct kmap *kmap = map__kmap(map);
1179 if (!kmap->ref_reloc_sym || !kmap->ref_reloc_sym->name)
1182 addr = kallsyms__get_function_start(filename,
1183 kmap->ref_reloc_sym->name);
1187 *delta = addr - kmap->ref_reloc_sym->addr;
1191 int dso__load_kallsyms(struct dso *dso, const char *filename,
1192 struct map *map, symbol_filter_t filter)
1196 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
1199 if (dso__load_all_kallsyms(dso, filename, map) < 0)
1202 if (kallsyms__delta(map, filename, &delta))
1205 symbols__fixup_duplicate(&dso->symbols[map->type]);
1206 symbols__fixup_end(&dso->symbols[map->type]);
1208 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1209 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
1211 dso->symtab_type = DSO_BINARY_TYPE__KALLSYMS;
1213 if (!dso__load_kcore(dso, map, filename))
1214 return dso__split_kallsyms_for_kcore(dso, map, filter);
1216 return dso__split_kallsyms(dso, map, delta, filter);
1219 static int dso__load_perf_map(struct dso *dso, struct map *map,
1220 symbol_filter_t filter)
1227 file = fopen(dso->long_name, "r");
1231 while (!feof(file)) {
1236 line_len = getline(&line, &n, file);
1243 line[--line_len] = '\0'; /* \n */
1245 len = hex2u64(line, &start);
1248 if (len + 2 >= line_len)
1251 len += hex2u64(line + len, &size);
1254 if (len + 2 >= line_len)
1257 sym = symbol__new(start, size, STB_GLOBAL, line + len);
1260 goto out_delete_line;
1262 if (filter && filter(map, sym))
1263 symbol__delete(sym);
1265 symbols__insert(&dso->symbols[map->type], sym);
1281 static bool dso__is_compatible_symtab_type(struct dso *dso, bool kmod,
1282 enum dso_binary_type type)
1285 case DSO_BINARY_TYPE__JAVA_JIT:
1286 case DSO_BINARY_TYPE__DEBUGLINK:
1287 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
1288 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
1289 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
1290 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
1291 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
1292 return !kmod && dso->kernel == DSO_TYPE_USER;
1294 case DSO_BINARY_TYPE__KALLSYMS:
1295 case DSO_BINARY_TYPE__VMLINUX:
1296 case DSO_BINARY_TYPE__KCORE:
1297 return dso->kernel == DSO_TYPE_KERNEL;
1299 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
1300 case DSO_BINARY_TYPE__GUEST_VMLINUX:
1301 case DSO_BINARY_TYPE__GUEST_KCORE:
1302 return dso->kernel == DSO_TYPE_GUEST_KERNEL;
1304 case DSO_BINARY_TYPE__GUEST_KMODULE:
1305 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
1306 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
1307 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
1309 * kernel modules know their symtab type - it's set when
1310 * creating a module dso in machine__new_module().
1312 return kmod && dso->symtab_type == type;
1314 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
1317 case DSO_BINARY_TYPE__NOT_FOUND:
1323 int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter)
1328 struct machine *machine;
1329 char *root_dir = (char *) "";
1331 struct symsrc ss_[2];
1332 struct symsrc *syms_ss = NULL, *runtime_ss = NULL;
1335 dso__set_loaded(dso, map->type);
1337 if (dso->kernel == DSO_TYPE_KERNEL)
1338 return dso__load_kernel_sym(dso, map, filter);
1339 else if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1340 return dso__load_guest_kernel_sym(dso, map, filter);
1342 if (map->groups && map->groups->machine)
1343 machine = map->groups->machine;
1347 dso->adjust_symbols = 0;
1349 if (strncmp(dso->name, "/tmp/perf-", 10) == 0) {
1352 if (lstat(dso->name, &st) < 0)
1355 if (st.st_uid && (st.st_uid != geteuid())) {
1356 pr_warning("File %s not owned by current user or root, "
1357 "ignoring it.\n", dso->name);
1361 ret = dso__load_perf_map(dso, map, filter);
1362 dso->symtab_type = ret > 0 ? DSO_BINARY_TYPE__JAVA_JIT :
1363 DSO_BINARY_TYPE__NOT_FOUND;
1368 root_dir = machine->root_dir;
1370 name = malloc(PATH_MAX);
1374 kmod = dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
1375 dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
1376 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE ||
1377 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
1380 * Iterate over candidate debug images.
1381 * Keep track of "interesting" ones (those which have a symtab, dynsym,
1382 * and/or opd section) for processing.
1384 for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) {
1385 struct symsrc *ss = &ss_[ss_pos];
1386 bool next_slot = false;
1388 enum dso_binary_type symtab_type = binary_type_symtab[i];
1390 if (!dso__is_compatible_symtab_type(dso, kmod, symtab_type))
1393 if (dso__read_binary_type_filename(dso, symtab_type,
1394 root_dir, name, PATH_MAX))
1397 /* Name is now the name of the next image to try */
1398 if (symsrc__init(ss, dso, name, symtab_type) < 0)
1401 if (!syms_ss && symsrc__has_symtab(ss)) {
1404 if (!dso->symsrc_filename)
1405 dso->symsrc_filename = strdup(name);
1408 if (!runtime_ss && symsrc__possibly_runtime(ss)) {
1416 if (syms_ss && runtime_ss)
1419 symsrc__destroy(ss);
1424 if (!runtime_ss && !syms_ss)
1427 if (runtime_ss && !syms_ss) {
1428 syms_ss = runtime_ss;
1431 /* We'll have to hope for the best */
1432 if (!runtime_ss && syms_ss)
1433 runtime_ss = syms_ss;
1436 ret = dso__load_sym(dso, map, syms_ss, runtime_ss, filter, kmod);
1443 nr_plt = dso__synthesize_plt_symbols(dso, runtime_ss, map, filter);
1448 for (; ss_pos > 0; ss_pos--)
1449 symsrc__destroy(&ss_[ss_pos - 1]);
1452 if (ret < 0 && strstr(dso->name, " (deleted)") != NULL)
1457 struct map *map_groups__find_by_name(struct map_groups *mg,
1458 enum map_type type, const char *name)
1462 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
1463 struct map *map = rb_entry(nd, struct map, rb_node);
1465 if (map->dso && strcmp(map->dso->short_name, name) == 0)
1472 int dso__load_vmlinux(struct dso *dso, struct map *map,
1473 const char *vmlinux, bool vmlinux_allocated,
1474 symbol_filter_t filter)
1478 char symfs_vmlinux[PATH_MAX];
1479 enum dso_binary_type symtab_type;
1481 if (vmlinux[0] == '/')
1482 snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s", vmlinux);
1484 symbol__join_symfs(symfs_vmlinux, vmlinux);
1486 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1487 symtab_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
1489 symtab_type = DSO_BINARY_TYPE__VMLINUX;
1491 if (symsrc__init(&ss, dso, symfs_vmlinux, symtab_type))
1494 err = dso__load_sym(dso, map, &ss, &ss, filter, 0);
1495 symsrc__destroy(&ss);
1498 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1499 dso->binary_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
1501 dso->binary_type = DSO_BINARY_TYPE__VMLINUX;
1502 dso__set_long_name(dso, vmlinux, vmlinux_allocated);
1503 dso__set_loaded(dso, map->type);
1504 pr_debug("Using %s for symbols\n", symfs_vmlinux);
1510 int dso__load_vmlinux_path(struct dso *dso, struct map *map,
1511 symbol_filter_t filter)
1514 char *filename = NULL;
1516 if (!symbol_conf.ignore_vmlinux_buildid)
1517 filename = dso__build_id_filename(dso, NULL, 0);
1518 if (filename != NULL) {
1519 err = dso__load_vmlinux(dso, map, filename, true, filter);
1525 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1526 vmlinux_path__nr_entries + 1);
1528 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
1529 err = dso__load_vmlinux(dso, map, vmlinux_path[i], false, filter);
1537 static int find_matching_kcore(struct map *map, char *dir, size_t dir_sz)
1539 char kallsyms_filename[PATH_MAX];
1540 struct dirent *dent;
1552 if (dent->d_type != DT_DIR)
1554 scnprintf(kallsyms_filename, sizeof(kallsyms_filename),
1555 "%s/%s/kallsyms", dir, dent->d_name);
1556 if (!validate_kcore_addresses(kallsyms_filename, map)) {
1557 strlcpy(dir, kallsyms_filename, dir_sz);
1568 static char *dso__find_kallsyms(struct dso *dso, struct map *map)
1570 u8 host_build_id[BUILD_ID_SIZE];
1571 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1572 bool is_host = false;
1573 char path[PATH_MAX];
1575 if (!dso->has_build_id) {
1577 * Last resort, if we don't have a build-id and couldn't find
1578 * any vmlinux file, try the running kernel kallsyms table.
1583 if (sysfs__read_build_id("/sys/kernel/notes", host_build_id,
1584 sizeof(host_build_id)) == 0)
1585 is_host = dso__build_id_equal(dso, host_build_id);
1587 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1589 scnprintf(path, sizeof(path), "%s/[kernel.kcore]/%s", buildid_dir,
1592 /* Use /proc/kallsyms if possible */
1597 /* If no cached kcore go with /proc/kallsyms */
1604 * Do not check the build-id cache, until we know we cannot use
1607 fd = open("/proc/kcore", O_RDONLY);
1610 /* If module maps match go with /proc/kallsyms */
1611 if (!validate_kcore_addresses("/proc/kallsyms", map))
1615 /* Find kallsyms in build-id cache with kcore */
1616 if (!find_matching_kcore(map, path, sizeof(path)))
1617 return strdup(path);
1622 /* Find kallsyms in build-id cache with kcore */
1623 if (!find_matching_kcore(map, path, sizeof(path)))
1624 return strdup(path);
1626 scnprintf(path, sizeof(path), "%s/[kernel.kallsyms]/%s",
1627 buildid_dir, sbuild_id);
1629 if (access(path, F_OK)) {
1630 pr_err("No kallsyms or vmlinux with build-id %s was found\n",
1635 return strdup(path);
1638 return strdup("/proc/kallsyms");
1641 static int dso__load_kernel_sym(struct dso *dso, struct map *map,
1642 symbol_filter_t filter)
1645 const char *kallsyms_filename = NULL;
1646 char *kallsyms_allocated_filename = NULL;
1648 * Step 1: if the user specified a kallsyms or vmlinux filename, use
1649 * it and only it, reporting errors to the user if it cannot be used.
1651 * For instance, try to analyse an ARM perf.data file _without_ a
1652 * build-id, or if the user specifies the wrong path to the right
1653 * vmlinux file, obviously we can't fallback to another vmlinux (a
1654 * x86_86 one, on the machine where analysis is being performed, say),
1655 * or worse, /proc/kallsyms.
1657 * If the specified file _has_ a build-id and there is a build-id
1658 * section in the perf.data file, we will still do the expected
1659 * validation in dso__load_vmlinux and will bail out if they don't
1662 if (symbol_conf.kallsyms_name != NULL) {
1663 kallsyms_filename = symbol_conf.kallsyms_name;
1667 if (!symbol_conf.ignore_vmlinux && symbol_conf.vmlinux_name != NULL) {
1668 return dso__load_vmlinux(dso, map, symbol_conf.vmlinux_name,
1672 if (!symbol_conf.ignore_vmlinux && vmlinux_path != NULL) {
1673 err = dso__load_vmlinux_path(dso, map, filter);
1678 /* do not try local files if a symfs was given */
1679 if (symbol_conf.symfs[0] != 0)
1682 kallsyms_allocated_filename = dso__find_kallsyms(dso, map);
1683 if (!kallsyms_allocated_filename)
1686 kallsyms_filename = kallsyms_allocated_filename;
1689 err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
1691 pr_debug("Using %s for symbols\n", kallsyms_filename);
1692 free(kallsyms_allocated_filename);
1694 if (err > 0 && !dso__is_kcore(dso)) {
1695 dso->binary_type = DSO_BINARY_TYPE__KALLSYMS;
1696 dso__set_long_name(dso, "[kernel.kallsyms]", false);
1697 map__fixup_start(map);
1698 map__fixup_end(map);
1704 static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
1705 symbol_filter_t filter)
1708 const char *kallsyms_filename = NULL;
1709 struct machine *machine;
1710 char path[PATH_MAX];
1713 pr_debug("Guest kernel map hasn't the point to groups\n");
1716 machine = map->groups->machine;
1718 if (machine__is_default_guest(machine)) {
1720 * if the user specified a vmlinux filename, use it and only
1721 * it, reporting errors to the user if it cannot be used.
1722 * Or use file guest_kallsyms inputted by user on commandline
1724 if (symbol_conf.default_guest_vmlinux_name != NULL) {
1725 err = dso__load_vmlinux(dso, map,
1726 symbol_conf.default_guest_vmlinux_name,
1731 kallsyms_filename = symbol_conf.default_guest_kallsyms;
1732 if (!kallsyms_filename)
1735 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
1736 kallsyms_filename = path;
1739 err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
1741 pr_debug("Using %s for symbols\n", kallsyms_filename);
1742 if (err > 0 && !dso__is_kcore(dso)) {
1743 dso->binary_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
1744 machine__mmap_name(machine, path, sizeof(path));
1745 dso__set_long_name(dso, strdup(path), true);
1746 map__fixup_start(map);
1747 map__fixup_end(map);
1753 static void vmlinux_path__exit(void)
1755 while (--vmlinux_path__nr_entries >= 0)
1756 zfree(&vmlinux_path[vmlinux_path__nr_entries]);
1758 zfree(&vmlinux_path);
1761 static int vmlinux_path__init(struct perf_session_env *env)
1765 char *kernel_version;
1767 vmlinux_path = malloc(sizeof(char *) * 6);
1768 if (vmlinux_path == NULL)
1771 vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
1772 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1774 ++vmlinux_path__nr_entries;
1775 vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
1776 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1778 ++vmlinux_path__nr_entries;
1780 /* only try kernel version if no symfs was given */
1781 if (symbol_conf.symfs[0] != 0)
1785 kernel_version = env->os_release;
1787 if (uname(&uts) < 0)
1790 kernel_version = uts.release;
1793 snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", kernel_version);
1794 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1795 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1797 ++vmlinux_path__nr_entries;
1798 snprintf(bf, sizeof(bf), "/usr/lib/debug/boot/vmlinux-%s",
1800 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1801 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1803 ++vmlinux_path__nr_entries;
1804 snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", kernel_version);
1805 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1806 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1808 ++vmlinux_path__nr_entries;
1809 snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
1811 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1812 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1814 ++vmlinux_path__nr_entries;
1819 vmlinux_path__exit();
1823 int setup_list(struct strlist **list, const char *list_str,
1824 const char *list_name)
1826 if (list_str == NULL)
1829 *list = strlist__new(true, list_str);
1831 pr_err("problems parsing %s list\n", list_name);
1837 static bool symbol__read_kptr_restrict(void)
1841 if (geteuid() != 0) {
1842 FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
1846 if (fgets(line, sizeof(line), fp) != NULL)
1847 value = atoi(line) != 0;
1856 int symbol__init(struct perf_session_env *env)
1860 if (symbol_conf.initialized)
1863 symbol_conf.priv_size = PERF_ALIGN(symbol_conf.priv_size, sizeof(u64));
1867 if (symbol_conf.sort_by_name)
1868 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
1869 sizeof(struct symbol));
1871 if (symbol_conf.try_vmlinux_path && vmlinux_path__init(env) < 0)
1874 if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
1875 pr_err("'.' is the only non valid --field-separator argument\n");
1879 if (setup_list(&symbol_conf.dso_list,
1880 symbol_conf.dso_list_str, "dso") < 0)
1883 if (setup_list(&symbol_conf.comm_list,
1884 symbol_conf.comm_list_str, "comm") < 0)
1885 goto out_free_dso_list;
1887 if (setup_list(&symbol_conf.sym_list,
1888 symbol_conf.sym_list_str, "symbol") < 0)
1889 goto out_free_comm_list;
1892 * A path to symbols of "/" is identical to ""
1893 * reset here for simplicity.
1895 symfs = realpath(symbol_conf.symfs, NULL);
1897 symfs = symbol_conf.symfs;
1898 if (strcmp(symfs, "/") == 0)
1899 symbol_conf.symfs = "";
1900 if (symfs != symbol_conf.symfs)
1901 free((void *)symfs);
1903 symbol_conf.kptr_restrict = symbol__read_kptr_restrict();
1905 symbol_conf.initialized = true;
1909 strlist__delete(symbol_conf.comm_list);
1911 strlist__delete(symbol_conf.dso_list);
1915 void symbol__exit(void)
1917 if (!symbol_conf.initialized)
1919 strlist__delete(symbol_conf.sym_list);
1920 strlist__delete(symbol_conf.dso_list);
1921 strlist__delete(symbol_conf.comm_list);
1922 vmlinux_path__exit();
1923 symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
1924 symbol_conf.initialized = false;