6 #include <linux/kernel.h>
23 #include "sane_ctype.h"
27 #include <symbol/kallsyms.h>
28 #include <sys/utsname.h>
30 static int dso__load_kernel_sym(struct dso *dso, struct map *map);
31 static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map);
32 static bool symbol__is_idle(const char *name);
34 int vmlinux_path__nr_entries;
37 struct symbol_conf symbol_conf = {
39 .try_vmlinux_path = true,
42 .demangle_kernel = false,
43 .cumulate_callchain = true,
44 .show_hist_headers = true,
49 static enum dso_binary_type binary_type_symtab[] = {
50 DSO_BINARY_TYPE__KALLSYMS,
51 DSO_BINARY_TYPE__GUEST_KALLSYMS,
52 DSO_BINARY_TYPE__JAVA_JIT,
53 DSO_BINARY_TYPE__DEBUGLINK,
54 DSO_BINARY_TYPE__BUILD_ID_CACHE,
55 DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
56 DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
57 DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
58 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
59 DSO_BINARY_TYPE__GUEST_KMODULE,
60 DSO_BINARY_TYPE__GUEST_KMODULE_COMP,
61 DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE,
62 DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP,
63 DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO,
64 DSO_BINARY_TYPE__NOT_FOUND,
67 #define DSO_BINARY_TYPE__SYMTAB_CNT ARRAY_SIZE(binary_type_symtab)
69 bool symbol_type__is_a(char symbol_type, enum map_type map_type)
71 symbol_type = toupper(symbol_type);
75 return symbol_type == 'T' || symbol_type == 'W';
77 return symbol_type == 'D';
83 static int prefix_underscores_count(const char *str)
85 const char *tail = str;
93 int __weak arch__choose_best_symbol(struct symbol *syma,
94 struct symbol *symb __maybe_unused)
96 /* Avoid "SyS" kernel syscall aliases */
97 if (strlen(syma->name) >= 3 && !strncmp(syma->name, "SyS", 3))
99 if (strlen(syma->name) >= 10 && !strncmp(syma->name, "compat_SyS", 10))
105 static int choose_best_symbol(struct symbol *syma, struct symbol *symb)
111 /* Prefer a symbol with non zero length */
112 a = syma->end - syma->start;
113 b = symb->end - symb->start;
114 if ((b == 0) && (a > 0))
116 else if ((a == 0) && (b > 0))
119 /* Prefer a non weak symbol over a weak one */
120 a = syma->binding == STB_WEAK;
121 b = symb->binding == STB_WEAK;
127 /* Prefer a global symbol over a non global one */
128 a = syma->binding == STB_GLOBAL;
129 b = symb->binding == STB_GLOBAL;
135 /* Prefer a symbol with less underscores */
136 a = prefix_underscores_count(syma->name);
137 b = prefix_underscores_count(symb->name);
143 /* Choose the symbol with the longest name */
144 na = strlen(syma->name);
145 nb = strlen(symb->name);
151 return arch__choose_best_symbol(syma, symb);
154 void symbols__fixup_duplicate(struct rb_root *symbols)
157 struct symbol *curr, *next;
159 if (symbol_conf.allow_aliases)
162 nd = rb_first(symbols);
165 curr = rb_entry(nd, struct symbol, rb_node);
167 nd = rb_next(&curr->rb_node);
168 next = rb_entry(nd, struct symbol, rb_node);
173 if (curr->start != next->start)
176 if (choose_best_symbol(curr, next) == SYMBOL_A) {
177 rb_erase(&next->rb_node, symbols);
178 symbol__delete(next);
181 nd = rb_next(&curr->rb_node);
182 rb_erase(&curr->rb_node, symbols);
183 symbol__delete(curr);
188 void symbols__fixup_end(struct rb_root *symbols)
190 struct rb_node *nd, *prevnd = rb_first(symbols);
191 struct symbol *curr, *prev;
196 curr = rb_entry(prevnd, struct symbol, rb_node);
198 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
200 curr = rb_entry(nd, struct symbol, rb_node);
202 if (prev->end == prev->start && prev->end != curr->start)
203 prev->end = curr->start;
207 if (curr->end == curr->start)
208 curr->end = roundup(curr->start, 4096) + 4096;
211 void __map_groups__fixup_end(struct map_groups *mg, enum map_type type)
213 struct maps *maps = &mg->maps[type];
214 struct map *next, *curr;
216 pthread_rwlock_wrlock(&maps->lock);
218 curr = maps__first(maps);
222 for (next = map__next(curr); next; next = map__next(curr)) {
223 curr->end = next->start;
228 * We still haven't the actual symbols, so guess the
229 * last map final address.
234 pthread_rwlock_unlock(&maps->lock);
237 struct symbol *symbol__new(u64 start, u64 len, u8 binding, const char *name)
239 size_t namelen = strlen(name) + 1;
240 struct symbol *sym = calloc(1, (symbol_conf.priv_size +
241 sizeof(*sym) + namelen));
245 if (symbol_conf.priv_size) {
246 if (symbol_conf.init_annotation) {
247 struct annotation *notes = (void *)sym;
248 pthread_mutex_init(¬es->lock, NULL);
250 sym = ((void *)sym) + symbol_conf.priv_size;
254 sym->end = len ? start + len : start;
255 sym->binding = binding;
256 sym->namelen = namelen - 1;
258 pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n",
259 __func__, name, start, sym->end);
260 memcpy(sym->name, name, namelen);
265 void symbol__delete(struct symbol *sym)
267 free(((void *)sym) - symbol_conf.priv_size);
270 void symbols__delete(struct rb_root *symbols)
273 struct rb_node *next = rb_first(symbols);
276 pos = rb_entry(next, struct symbol, rb_node);
277 next = rb_next(&pos->rb_node);
278 rb_erase(&pos->rb_node, symbols);
283 void __symbols__insert(struct rb_root *symbols, struct symbol *sym, bool kernel)
285 struct rb_node **p = &symbols->rb_node;
286 struct rb_node *parent = NULL;
287 const u64 ip = sym->start;
291 const char *name = sym->name;
293 * ppc64 uses function descriptors and appends a '.' to the
294 * start of every instruction address. Remove it.
298 sym->idle = symbol__is_idle(name);
303 s = rb_entry(parent, struct symbol, rb_node);
309 rb_link_node(&sym->rb_node, parent, p);
310 rb_insert_color(&sym->rb_node, symbols);
313 void symbols__insert(struct rb_root *symbols, struct symbol *sym)
315 __symbols__insert(symbols, sym, false);
318 static struct symbol *symbols__find(struct rb_root *symbols, u64 ip)
325 n = symbols->rb_node;
328 struct symbol *s = rb_entry(n, struct symbol, rb_node);
332 else if (ip > s->end || (ip == s->end && ip != s->start))
341 static struct symbol *symbols__first(struct rb_root *symbols)
343 struct rb_node *n = rb_first(symbols);
346 return rb_entry(n, struct symbol, rb_node);
351 static struct symbol *symbols__last(struct rb_root *symbols)
353 struct rb_node *n = rb_last(symbols);
356 return rb_entry(n, struct symbol, rb_node);
361 static struct symbol *symbols__next(struct symbol *sym)
363 struct rb_node *n = rb_next(&sym->rb_node);
366 return rb_entry(n, struct symbol, rb_node);
371 static void symbols__insert_by_name(struct rb_root *symbols, struct symbol *sym)
373 struct rb_node **p = &symbols->rb_node;
374 struct rb_node *parent = NULL;
375 struct symbol_name_rb_node *symn, *s;
377 symn = container_of(sym, struct symbol_name_rb_node, sym);
381 s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
382 if (strcmp(sym->name, s->sym.name) < 0)
387 rb_link_node(&symn->rb_node, parent, p);
388 rb_insert_color(&symn->rb_node, symbols);
391 static void symbols__sort_by_name(struct rb_root *symbols,
392 struct rb_root *source)
396 for (nd = rb_first(source); nd; nd = rb_next(nd)) {
397 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
398 symbols__insert_by_name(symbols, pos);
402 static struct symbol *symbols__find_by_name(struct rb_root *symbols,
406 struct symbol_name_rb_node *s = NULL;
411 n = symbols->rb_node;
416 s = rb_entry(n, struct symbol_name_rb_node, rb_node);
417 cmp = arch__compare_symbol_names(name, s->sym.name);
430 /* return first symbol that has same name (if any) */
431 for (n = rb_prev(n); n; n = rb_prev(n)) {
432 struct symbol_name_rb_node *tmp;
434 tmp = rb_entry(n, struct symbol_name_rb_node, rb_node);
435 if (arch__compare_symbol_names(tmp->sym.name, s->sym.name))
444 void dso__reset_find_symbol_cache(struct dso *dso)
448 for (type = MAP__FUNCTION; type <= MAP__VARIABLE; ++type) {
449 dso->last_find_result[type].addr = 0;
450 dso->last_find_result[type].symbol = NULL;
454 void dso__insert_symbol(struct dso *dso, enum map_type type, struct symbol *sym)
456 __symbols__insert(&dso->symbols[type], sym, dso->kernel);
458 /* update the symbol cache if necessary */
459 if (dso->last_find_result[type].addr >= sym->start &&
460 (dso->last_find_result[type].addr < sym->end ||
461 sym->start == sym->end)) {
462 dso->last_find_result[type].symbol = sym;
466 struct symbol *dso__find_symbol(struct dso *dso,
467 enum map_type type, u64 addr)
469 if (dso->last_find_result[type].addr != addr) {
470 dso->last_find_result[type].addr = addr;
471 dso->last_find_result[type].symbol = symbols__find(&dso->symbols[type], addr);
474 return dso->last_find_result[type].symbol;
477 struct symbol *dso__first_symbol(struct dso *dso, enum map_type type)
479 return symbols__first(&dso->symbols[type]);
482 struct symbol *dso__last_symbol(struct dso *dso, enum map_type type)
484 return symbols__last(&dso->symbols[type]);
487 struct symbol *dso__next_symbol(struct symbol *sym)
489 return symbols__next(sym);
492 struct symbol *symbol__next_by_name(struct symbol *sym)
494 struct symbol_name_rb_node *s = container_of(sym, struct symbol_name_rb_node, sym);
495 struct rb_node *n = rb_next(&s->rb_node);
497 return n ? &rb_entry(n, struct symbol_name_rb_node, rb_node)->sym : NULL;
501 * Teturns first symbol that matched with @name.
503 struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
506 return symbols__find_by_name(&dso->symbol_names[type], name);
509 void dso__sort_by_name(struct dso *dso, enum map_type type)
511 dso__set_sorted_by_name(dso, type);
512 return symbols__sort_by_name(&dso->symbol_names[type],
513 &dso->symbols[type]);
516 int modules__parse(const char *filename, void *arg,
517 int (*process_module)(void *arg, const char *name,
525 file = fopen(filename, "r");
535 line_len = getline(&line, &n, file);
548 line[--line_len] = '\0'; /* \n */
550 sep = strrchr(line, 'x');
554 hex2u64(sep + 1, &start);
556 sep = strchr(line, ' ');
562 scnprintf(name, sizeof(name), "[%s]", line);
564 err = process_module(arg, name, start);
574 struct process_kallsyms_args {
580 * These are symbols in the kernel image, so make sure that
581 * sym is from a kernel DSO.
583 static bool symbol__is_idle(const char *name)
585 const char * const idle_symbols[] = {
594 "mwait_idle_with_hints",
596 "ppc64_runlatch_off",
597 "pseries_dedicated_idle_sleep",
602 for (i = 0; idle_symbols[i]; i++) {
603 if (!strcmp(idle_symbols[i], name))
610 static int map__process_kallsym_symbol(void *arg, const char *name,
611 char type, u64 start)
614 struct process_kallsyms_args *a = arg;
615 struct rb_root *root = &a->dso->symbols[a->map->type];
617 if (!symbol_type__is_a(type, a->map->type))
621 * module symbols are not sorted so we add all
622 * symbols, setting length to 0, and rely on
623 * symbols__fixup_end() to fix it up.
625 sym = symbol__new(start, 0, kallsyms2elf_binding(type), name);
629 * We will pass the symbols to the filter later, in
630 * map__split_kallsyms, when we have split the maps per module
632 __symbols__insert(root, sym, !strchr(name, '['));
638 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
639 * so that we can in the next step set the symbol ->end address and then
640 * call kernel_maps__split_kallsyms.
642 static int dso__load_all_kallsyms(struct dso *dso, const char *filename,
645 struct process_kallsyms_args args = { .map = map, .dso = dso, };
646 return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
649 static int dso__split_kallsyms_for_kcore(struct dso *dso, struct map *map)
651 struct map_groups *kmaps = map__kmaps(map);
652 struct map *curr_map;
655 struct rb_root old_root = dso->symbols[map->type];
656 struct rb_root *root = &dso->symbols[map->type];
657 struct rb_node *next = rb_first(root);
667 pos = rb_entry(next, struct symbol, rb_node);
668 next = rb_next(&pos->rb_node);
670 rb_erase_init(&pos->rb_node, &old_root);
672 module = strchr(pos->name, '\t');
676 curr_map = map_groups__find(kmaps, map->type, pos->start);
683 pos->start -= curr_map->start - curr_map->pgoff;
685 pos->end -= curr_map->start - curr_map->pgoff;
686 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
690 /* Symbols have been adjusted */
691 dso->adjust_symbols = 1;
697 * Split the symbols into maps, making sure there are no overlaps, i.e. the
698 * kernel range is broken in several maps, named [kernel].N, as we don't have
699 * the original ELF section names vmlinux have.
701 static int dso__split_kallsyms(struct dso *dso, struct map *map, u64 delta)
703 struct map_groups *kmaps = map__kmaps(map);
704 struct machine *machine;
705 struct map *curr_map = map;
707 int count = 0, moved = 0;
708 struct rb_root *root = &dso->symbols[map->type];
709 struct rb_node *next = rb_first(root);
710 int kernel_range = 0;
715 machine = kmaps->machine;
720 pos = rb_entry(next, struct symbol, rb_node);
721 next = rb_next(&pos->rb_node);
723 module = strchr(pos->name, '\t');
725 if (!symbol_conf.use_modules)
730 if (strcmp(curr_map->dso->short_name, module)) {
731 if (curr_map != map &&
732 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
733 machine__is_default_guest(machine)) {
735 * We assume all symbols of a module are
736 * continuous in * kallsyms, so curr_map
737 * points to a module and all its
738 * symbols are in its kmap. Mark it as
741 dso__set_loaded(curr_map->dso,
745 curr_map = map_groups__find_by_name(kmaps,
747 if (curr_map == NULL) {
748 pr_debug("%s/proc/{kallsyms,modules} "
749 "inconsistency while looking "
750 "for \"%s\" module!\n",
751 machine->root_dir, module);
756 if (curr_map->dso->loaded &&
757 !machine__is_default_guest(machine))
761 * So that we look just like we get from .ko files,
762 * i.e. not prelinked, relative to map->start.
764 pos->start = curr_map->map_ip(curr_map, pos->start);
765 pos->end = curr_map->map_ip(curr_map, pos->end);
766 } else if (curr_map != map) {
767 char dso_name[PATH_MAX];
771 /* Kernel was relocated at boot time */
781 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
782 snprintf(dso_name, sizeof(dso_name),
786 snprintf(dso_name, sizeof(dso_name),
790 ndso = dso__new(dso_name);
794 ndso->kernel = dso->kernel;
796 curr_map = map__new2(pos->start, ndso, map->type);
797 if (curr_map == NULL) {
802 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
803 map_groups__insert(kmaps, curr_map);
806 /* Kernel was relocated at boot time */
811 if (curr_map != map) {
812 rb_erase(&pos->rb_node, root);
813 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
820 rb_erase(&pos->rb_node, root);
824 if (curr_map != map &&
825 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
826 machine__is_default_guest(kmaps->machine)) {
827 dso__set_loaded(curr_map->dso, curr_map->type);
830 return count + moved;
833 bool symbol__restricted_filename(const char *filename,
834 const char *restricted_filename)
836 bool restricted = false;
838 if (symbol_conf.kptr_restrict) {
839 char *r = realpath(filename, NULL);
842 restricted = strcmp(r, restricted_filename) == 0;
852 struct rb_node rb_node;
857 static void add_module(struct module_info *mi, struct rb_root *modules)
859 struct rb_node **p = &modules->rb_node;
860 struct rb_node *parent = NULL;
861 struct module_info *m;
865 m = rb_entry(parent, struct module_info, rb_node);
866 if (strcmp(mi->name, m->name) < 0)
871 rb_link_node(&mi->rb_node, parent, p);
872 rb_insert_color(&mi->rb_node, modules);
875 static void delete_modules(struct rb_root *modules)
877 struct module_info *mi;
878 struct rb_node *next = rb_first(modules);
881 mi = rb_entry(next, struct module_info, rb_node);
882 next = rb_next(&mi->rb_node);
883 rb_erase(&mi->rb_node, modules);
889 static struct module_info *find_module(const char *name,
890 struct rb_root *modules)
892 struct rb_node *n = modules->rb_node;
895 struct module_info *m;
898 m = rb_entry(n, struct module_info, rb_node);
899 cmp = strcmp(name, m->name);
911 static int __read_proc_modules(void *arg, const char *name, u64 start)
913 struct rb_root *modules = arg;
914 struct module_info *mi;
916 mi = zalloc(sizeof(struct module_info));
920 mi->name = strdup(name);
928 add_module(mi, modules);
933 static int read_proc_modules(const char *filename, struct rb_root *modules)
935 if (symbol__restricted_filename(filename, "/proc/modules"))
938 if (modules__parse(filename, modules, __read_proc_modules)) {
939 delete_modules(modules);
946 int compare_proc_modules(const char *from, const char *to)
948 struct rb_root from_modules = RB_ROOT;
949 struct rb_root to_modules = RB_ROOT;
950 struct rb_node *from_node, *to_node;
951 struct module_info *from_m, *to_m;
954 if (read_proc_modules(from, &from_modules))
957 if (read_proc_modules(to, &to_modules))
958 goto out_delete_from;
960 from_node = rb_first(&from_modules);
961 to_node = rb_first(&to_modules);
966 from_m = rb_entry(from_node, struct module_info, rb_node);
967 to_m = rb_entry(to_node, struct module_info, rb_node);
969 if (from_m->start != to_m->start ||
970 strcmp(from_m->name, to_m->name))
973 from_node = rb_next(from_node);
974 to_node = rb_next(to_node);
977 if (!from_node && !to_node)
980 delete_modules(&to_modules);
982 delete_modules(&from_modules);
987 static int do_validate_kcore_modules(const char *filename, struct map *map,
988 struct map_groups *kmaps)
990 struct rb_root modules = RB_ROOT;
994 err = read_proc_modules(filename, &modules);
998 old_map = map_groups__first(kmaps, map->type);
1000 struct map *next = map_groups__next(old_map);
1001 struct module_info *mi;
1003 if (old_map == map || old_map->start == map->start) {
1004 /* The kernel map */
1009 /* Module must be in memory at the same address */
1010 mi = find_module(old_map->dso->short_name, &modules);
1011 if (!mi || mi->start != old_map->start) {
1019 delete_modules(&modules);
1024 * If kallsyms is referenced by name then we look for filename in the same
1027 static bool filename_from_kallsyms_filename(char *filename,
1028 const char *base_name,
1029 const char *kallsyms_filename)
1033 strcpy(filename, kallsyms_filename);
1034 name = strrchr(filename, '/');
1040 if (!strcmp(name, "kallsyms")) {
1041 strcpy(name, base_name);
1048 static int validate_kcore_modules(const char *kallsyms_filename,
1051 struct map_groups *kmaps = map__kmaps(map);
1052 char modules_filename[PATH_MAX];
1057 if (!filename_from_kallsyms_filename(modules_filename, "modules",
1061 if (do_validate_kcore_modules(modules_filename, map, kmaps))
1067 static int validate_kcore_addresses(const char *kallsyms_filename,
1070 struct kmap *kmap = map__kmap(map);
1075 if (kmap->ref_reloc_sym && kmap->ref_reloc_sym->name) {
1078 start = kallsyms__get_function_start(kallsyms_filename,
1079 kmap->ref_reloc_sym->name);
1080 if (start != kmap->ref_reloc_sym->addr)
1084 return validate_kcore_modules(kallsyms_filename, map);
1087 struct kcore_mapfn_data {
1090 struct list_head maps;
1093 static int kcore_mapfn(u64 start, u64 len, u64 pgoff, void *data)
1095 struct kcore_mapfn_data *md = data;
1098 map = map__new2(start, md->dso, md->type);
1102 map->end = map->start + len;
1105 list_add(&map->node, &md->maps);
1110 static int dso__load_kcore(struct dso *dso, struct map *map,
1111 const char *kallsyms_filename)
1113 struct map_groups *kmaps = map__kmaps(map);
1114 struct machine *machine;
1115 struct kcore_mapfn_data md;
1116 struct map *old_map, *new_map, *replacement_map = NULL;
1119 char kcore_filename[PATH_MAX];
1125 machine = kmaps->machine;
1127 /* This function requires that the map is the kernel map */
1128 if (map != machine->vmlinux_maps[map->type])
1131 if (!filename_from_kallsyms_filename(kcore_filename, "kcore",
1135 /* Modules and kernel must be present at their original addresses */
1136 if (validate_kcore_addresses(kallsyms_filename, map))
1140 md.type = map->type;
1141 INIT_LIST_HEAD(&md.maps);
1143 fd = open(kcore_filename, O_RDONLY);
1145 pr_debug("Failed to open %s. Note /proc/kcore requires CAP_SYS_RAWIO capability to access.\n",
1150 /* Read new maps into temporary lists */
1151 err = file__read_maps(fd, md.type == MAP__FUNCTION, kcore_mapfn, &md,
1155 dso->is_64_bit = is_64_bit;
1157 if (list_empty(&md.maps)) {
1162 /* Remove old maps */
1163 old_map = map_groups__first(kmaps, map->type);
1165 struct map *next = map_groups__next(old_map);
1168 map_groups__remove(kmaps, old_map);
1172 /* Find the kernel map using the first symbol */
1173 sym = dso__first_symbol(dso, map->type);
1174 list_for_each_entry(new_map, &md.maps, node) {
1175 if (sym && sym->start >= new_map->start &&
1176 sym->start < new_map->end) {
1177 replacement_map = new_map;
1182 if (!replacement_map)
1183 replacement_map = list_entry(md.maps.next, struct map, node);
1186 while (!list_empty(&md.maps)) {
1187 new_map = list_entry(md.maps.next, struct map, node);
1188 list_del_init(&new_map->node);
1189 if (new_map == replacement_map) {
1190 map->start = new_map->start;
1191 map->end = new_map->end;
1192 map->pgoff = new_map->pgoff;
1193 map->map_ip = new_map->map_ip;
1194 map->unmap_ip = new_map->unmap_ip;
1195 /* Ensure maps are correctly ordered */
1197 map_groups__remove(kmaps, map);
1198 map_groups__insert(kmaps, map);
1201 map_groups__insert(kmaps, new_map);
1208 * Set the data type and long name so that kcore can be read via
1209 * dso__data_read_addr().
1211 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1212 dso->binary_type = DSO_BINARY_TYPE__GUEST_KCORE;
1214 dso->binary_type = DSO_BINARY_TYPE__KCORE;
1215 dso__set_long_name(dso, strdup(kcore_filename), true);
1219 if (map->type == MAP__FUNCTION)
1220 pr_debug("Using %s for kernel object code\n", kcore_filename);
1222 pr_debug("Using %s for kernel data\n", kcore_filename);
1227 while (!list_empty(&md.maps)) {
1228 map = list_entry(md.maps.next, struct map, node);
1229 list_del_init(&map->node);
1237 * If the kernel is relocated at boot time, kallsyms won't match. Compute the
1238 * delta based on the relocation reference symbol.
1240 static int kallsyms__delta(struct map *map, const char *filename, u64 *delta)
1242 struct kmap *kmap = map__kmap(map);
1248 if (!kmap->ref_reloc_sym || !kmap->ref_reloc_sym->name)
1251 addr = kallsyms__get_function_start(filename,
1252 kmap->ref_reloc_sym->name);
1256 *delta = addr - kmap->ref_reloc_sym->addr;
1260 int __dso__load_kallsyms(struct dso *dso, const char *filename,
1261 struct map *map, bool no_kcore)
1265 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
1268 if (dso__load_all_kallsyms(dso, filename, map) < 0)
1271 if (kallsyms__delta(map, filename, &delta))
1274 symbols__fixup_end(&dso->symbols[map->type]);
1275 symbols__fixup_duplicate(&dso->symbols[map->type]);
1277 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1278 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
1280 dso->symtab_type = DSO_BINARY_TYPE__KALLSYMS;
1282 if (!no_kcore && !dso__load_kcore(dso, map, filename))
1283 return dso__split_kallsyms_for_kcore(dso, map);
1285 return dso__split_kallsyms(dso, map, delta);
1288 int dso__load_kallsyms(struct dso *dso, const char *filename,
1291 return __dso__load_kallsyms(dso, filename, map, false);
1294 static int dso__load_perf_map(struct dso *dso, struct map *map)
1301 file = fopen(dso->long_name, "r");
1305 while (!feof(file)) {
1310 line_len = getline(&line, &n, file);
1317 line[--line_len] = '\0'; /* \n */
1319 len = hex2u64(line, &start);
1322 if (len + 2 >= line_len)
1325 len += hex2u64(line + len, &size);
1328 if (len + 2 >= line_len)
1331 sym = symbol__new(start, size, STB_GLOBAL, line + len);
1334 goto out_delete_line;
1336 symbols__insert(&dso->symbols[map->type], sym);
1351 static bool dso__is_compatible_symtab_type(struct dso *dso, bool kmod,
1352 enum dso_binary_type type)
1355 case DSO_BINARY_TYPE__JAVA_JIT:
1356 case DSO_BINARY_TYPE__DEBUGLINK:
1357 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
1358 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
1359 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
1360 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
1361 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
1362 return !kmod && dso->kernel == DSO_TYPE_USER;
1364 case DSO_BINARY_TYPE__KALLSYMS:
1365 case DSO_BINARY_TYPE__VMLINUX:
1366 case DSO_BINARY_TYPE__KCORE:
1367 return dso->kernel == DSO_TYPE_KERNEL;
1369 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
1370 case DSO_BINARY_TYPE__GUEST_VMLINUX:
1371 case DSO_BINARY_TYPE__GUEST_KCORE:
1372 return dso->kernel == DSO_TYPE_GUEST_KERNEL;
1374 case DSO_BINARY_TYPE__GUEST_KMODULE:
1375 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
1376 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
1377 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
1379 * kernel modules know their symtab type - it's set when
1380 * creating a module dso in machine__findnew_module_map().
1382 return kmod && dso->symtab_type == type;
1384 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
1387 case DSO_BINARY_TYPE__NOT_FOUND:
1393 int dso__load(struct dso *dso, struct map *map)
1398 struct machine *machine;
1399 char *root_dir = (char *) "";
1401 struct symsrc ss_[2];
1402 struct symsrc *syms_ss = NULL, *runtime_ss = NULL;
1404 unsigned char build_id[BUILD_ID_SIZE];
1406 pthread_mutex_lock(&dso->lock);
1408 /* check again under the dso->lock */
1409 if (dso__loaded(dso, map->type)) {
1415 if (dso->kernel == DSO_TYPE_KERNEL)
1416 ret = dso__load_kernel_sym(dso, map);
1417 else if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1418 ret = dso__load_guest_kernel_sym(dso, map);
1423 if (map->groups && map->groups->machine)
1424 machine = map->groups->machine;
1428 dso->adjust_symbols = 0;
1430 if (strncmp(dso->name, "/tmp/perf-", 10) == 0) {
1433 if (lstat(dso->name, &st) < 0)
1436 if (!symbol_conf.force && st.st_uid && (st.st_uid != geteuid())) {
1437 pr_warning("File %s not owned by current user or root, "
1438 "ignoring it (use -f to override).\n", dso->name);
1442 ret = dso__load_perf_map(dso, map);
1443 dso->symtab_type = ret > 0 ? DSO_BINARY_TYPE__JAVA_JIT :
1444 DSO_BINARY_TYPE__NOT_FOUND;
1449 root_dir = machine->root_dir;
1451 name = malloc(PATH_MAX);
1455 kmod = dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
1456 dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
1457 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE ||
1458 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
1462 * Read the build id if possible. This is required for
1463 * DSO_BINARY_TYPE__BUILDID_DEBUGINFO to work
1465 if (!dso->has_build_id &&
1466 is_regular_file(dso->long_name)) {
1467 __symbol__join_symfs(name, PATH_MAX, dso->long_name);
1468 if (filename__read_build_id(name, build_id, BUILD_ID_SIZE) > 0)
1469 dso__set_build_id(dso, build_id);
1473 * Iterate over candidate debug images.
1474 * Keep track of "interesting" ones (those which have a symtab, dynsym,
1475 * and/or opd section) for processing.
1477 for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) {
1478 struct symsrc *ss = &ss_[ss_pos];
1479 bool next_slot = false;
1481 enum dso_binary_type symtab_type = binary_type_symtab[i];
1483 if (!dso__is_compatible_symtab_type(dso, kmod, symtab_type))
1486 if (dso__read_binary_type_filename(dso, symtab_type,
1487 root_dir, name, PATH_MAX))
1490 if (!is_regular_file(name))
1493 /* Name is now the name of the next image to try */
1494 if (symsrc__init(ss, dso, name, symtab_type) < 0)
1497 if (!syms_ss && symsrc__has_symtab(ss)) {
1500 if (!dso->symsrc_filename)
1501 dso->symsrc_filename = strdup(name);
1504 if (!runtime_ss && symsrc__possibly_runtime(ss)) {
1512 if (syms_ss && runtime_ss)
1515 symsrc__destroy(ss);
1520 if (!runtime_ss && !syms_ss)
1523 if (runtime_ss && !syms_ss) {
1524 syms_ss = runtime_ss;
1527 /* We'll have to hope for the best */
1528 if (!runtime_ss && syms_ss)
1529 runtime_ss = syms_ss;
1531 if (syms_ss && syms_ss->type == DSO_BINARY_TYPE__BUILD_ID_CACHE)
1532 if (dso__build_id_is_kmod(dso, name, PATH_MAX))
1536 ret = dso__load_sym(dso, map, syms_ss, runtime_ss, kmod);
1543 nr_plt = dso__synthesize_plt_symbols(dso, runtime_ss, map);
1548 for (; ss_pos > 0; ss_pos--)
1549 symsrc__destroy(&ss_[ss_pos - 1]);
1552 if (ret < 0 && strstr(dso->name, " (deleted)") != NULL)
1555 dso__set_loaded(dso, map->type);
1556 pthread_mutex_unlock(&dso->lock);
1561 struct map *map_groups__find_by_name(struct map_groups *mg,
1562 enum map_type type, const char *name)
1564 struct maps *maps = &mg->maps[type];
1567 pthread_rwlock_rdlock(&maps->lock);
1569 for (map = maps__first(maps); map; map = map__next(map)) {
1570 if (map->dso && strcmp(map->dso->short_name, name) == 0)
1577 pthread_rwlock_unlock(&maps->lock);
1581 int dso__load_vmlinux(struct dso *dso, struct map *map,
1582 const char *vmlinux, bool vmlinux_allocated)
1586 char symfs_vmlinux[PATH_MAX];
1587 enum dso_binary_type symtab_type;
1589 if (vmlinux[0] == '/')
1590 snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s", vmlinux);
1592 symbol__join_symfs(symfs_vmlinux, vmlinux);
1594 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1595 symtab_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
1597 symtab_type = DSO_BINARY_TYPE__VMLINUX;
1599 if (symsrc__init(&ss, dso, symfs_vmlinux, symtab_type))
1602 err = dso__load_sym(dso, map, &ss, &ss, 0);
1603 symsrc__destroy(&ss);
1606 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1607 dso->binary_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
1609 dso->binary_type = DSO_BINARY_TYPE__VMLINUX;
1610 dso__set_long_name(dso, vmlinux, vmlinux_allocated);
1611 dso__set_loaded(dso, map->type);
1612 pr_debug("Using %s for symbols\n", symfs_vmlinux);
1618 int dso__load_vmlinux_path(struct dso *dso, struct map *map)
1621 char *filename = NULL;
1623 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1624 vmlinux_path__nr_entries + 1);
1626 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
1627 err = dso__load_vmlinux(dso, map, vmlinux_path[i], false);
1632 if (!symbol_conf.ignore_vmlinux_buildid)
1633 filename = dso__build_id_filename(dso, NULL, 0);
1634 if (filename != NULL) {
1635 err = dso__load_vmlinux(dso, map, filename, true);
1644 static bool visible_dir_filter(const char *name, struct dirent *d)
1646 if (d->d_type != DT_DIR)
1648 return lsdir_no_dot_filter(name, d);
1651 static int find_matching_kcore(struct map *map, char *dir, size_t dir_sz)
1653 char kallsyms_filename[PATH_MAX];
1655 struct strlist *dirs;
1656 struct str_node *nd;
1658 dirs = lsdir(dir, visible_dir_filter);
1662 strlist__for_each_entry(nd, dirs) {
1663 scnprintf(kallsyms_filename, sizeof(kallsyms_filename),
1664 "%s/%s/kallsyms", dir, nd->s);
1665 if (!validate_kcore_addresses(kallsyms_filename, map)) {
1666 strlcpy(dir, kallsyms_filename, dir_sz);
1672 strlist__delete(dirs);
1678 * Use open(O_RDONLY) to check readability directly instead of access(R_OK)
1679 * since access(R_OK) only checks with real UID/GID but open() use effective
1680 * UID/GID and actual capabilities (e.g. /proc/kcore requires CAP_SYS_RAWIO).
1682 static bool filename__readable(const char *file)
1684 int fd = open(file, O_RDONLY);
1691 static char *dso__find_kallsyms(struct dso *dso, struct map *map)
1693 u8 host_build_id[BUILD_ID_SIZE];
1694 char sbuild_id[SBUILD_ID_SIZE];
1695 bool is_host = false;
1696 char path[PATH_MAX];
1698 if (!dso->has_build_id) {
1700 * Last resort, if we don't have a build-id and couldn't find
1701 * any vmlinux file, try the running kernel kallsyms table.
1706 if (sysfs__read_build_id("/sys/kernel/notes", host_build_id,
1707 sizeof(host_build_id)) == 0)
1708 is_host = dso__build_id_equal(dso, host_build_id);
1710 /* Try a fast path for /proc/kallsyms if possible */
1713 * Do not check the build-id cache, unless we know we cannot use
1714 * /proc/kcore or module maps don't match to /proc/kallsyms.
1715 * To check readability of /proc/kcore, do not use access(R_OK)
1716 * since /proc/kcore requires CAP_SYS_RAWIO to read and access
1719 if (filename__readable("/proc/kcore") &&
1720 !validate_kcore_addresses("/proc/kallsyms", map))
1724 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1726 /* Find kallsyms in build-id cache with kcore */
1727 scnprintf(path, sizeof(path), "%s/%s/%s",
1728 buildid_dir, DSO__NAME_KCORE, sbuild_id);
1730 if (!find_matching_kcore(map, path, sizeof(path)))
1731 return strdup(path);
1733 /* Use current /proc/kallsyms if possible */
1736 return strdup("/proc/kallsyms");
1739 /* Finally, find a cache of kallsyms */
1740 if (!build_id_cache__kallsyms_path(sbuild_id, path, sizeof(path))) {
1741 pr_err("No kallsyms or vmlinux with build-id %s was found\n",
1746 return strdup(path);
1749 static int dso__load_kernel_sym(struct dso *dso, struct map *map)
1752 const char *kallsyms_filename = NULL;
1753 char *kallsyms_allocated_filename = NULL;
1755 * Step 1: if the user specified a kallsyms or vmlinux filename, use
1756 * it and only it, reporting errors to the user if it cannot be used.
1758 * For instance, try to analyse an ARM perf.data file _without_ a
1759 * build-id, or if the user specifies the wrong path to the right
1760 * vmlinux file, obviously we can't fallback to another vmlinux (a
1761 * x86_86 one, on the machine where analysis is being performed, say),
1762 * or worse, /proc/kallsyms.
1764 * If the specified file _has_ a build-id and there is a build-id
1765 * section in the perf.data file, we will still do the expected
1766 * validation in dso__load_vmlinux and will bail out if they don't
1769 if (symbol_conf.kallsyms_name != NULL) {
1770 kallsyms_filename = symbol_conf.kallsyms_name;
1774 if (!symbol_conf.ignore_vmlinux && symbol_conf.vmlinux_name != NULL) {
1775 return dso__load_vmlinux(dso, map, symbol_conf.vmlinux_name, false);
1778 if (!symbol_conf.ignore_vmlinux && vmlinux_path != NULL) {
1779 err = dso__load_vmlinux_path(dso, map);
1784 /* do not try local files if a symfs was given */
1785 if (symbol_conf.symfs[0] != 0)
1788 kallsyms_allocated_filename = dso__find_kallsyms(dso, map);
1789 if (!kallsyms_allocated_filename)
1792 kallsyms_filename = kallsyms_allocated_filename;
1795 err = dso__load_kallsyms(dso, kallsyms_filename, map);
1797 pr_debug("Using %s for symbols\n", kallsyms_filename);
1798 free(kallsyms_allocated_filename);
1800 if (err > 0 && !dso__is_kcore(dso)) {
1801 dso->binary_type = DSO_BINARY_TYPE__KALLSYMS;
1802 dso__set_long_name(dso, DSO__NAME_KALLSYMS, false);
1803 map__fixup_start(map);
1804 map__fixup_end(map);
1810 static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map)
1813 const char *kallsyms_filename = NULL;
1814 struct machine *machine;
1815 char path[PATH_MAX];
1818 pr_debug("Guest kernel map hasn't the point to groups\n");
1821 machine = map->groups->machine;
1823 if (machine__is_default_guest(machine)) {
1825 * if the user specified a vmlinux filename, use it and only
1826 * it, reporting errors to the user if it cannot be used.
1827 * Or use file guest_kallsyms inputted by user on commandline
1829 if (symbol_conf.default_guest_vmlinux_name != NULL) {
1830 err = dso__load_vmlinux(dso, map,
1831 symbol_conf.default_guest_vmlinux_name,
1836 kallsyms_filename = symbol_conf.default_guest_kallsyms;
1837 if (!kallsyms_filename)
1840 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
1841 kallsyms_filename = path;
1844 err = dso__load_kallsyms(dso, kallsyms_filename, map);
1846 pr_debug("Using %s for symbols\n", kallsyms_filename);
1847 if (err > 0 && !dso__is_kcore(dso)) {
1848 dso->binary_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
1849 machine__mmap_name(machine, path, sizeof(path));
1850 dso__set_long_name(dso, strdup(path), true);
1851 map__fixup_start(map);
1852 map__fixup_end(map);
1858 static void vmlinux_path__exit(void)
1860 while (--vmlinux_path__nr_entries >= 0)
1861 zfree(&vmlinux_path[vmlinux_path__nr_entries]);
1862 vmlinux_path__nr_entries = 0;
1864 zfree(&vmlinux_path);
1867 static const char * const vmlinux_paths[] = {
1872 static const char * const vmlinux_paths_upd[] = {
1874 "/usr/lib/debug/boot/vmlinux-%s",
1875 "/lib/modules/%s/build/vmlinux",
1876 "/usr/lib/debug/lib/modules/%s/vmlinux",
1877 "/usr/lib/debug/boot/vmlinux-%s.debug"
1880 static int vmlinux_path__add(const char *new_entry)
1882 vmlinux_path[vmlinux_path__nr_entries] = strdup(new_entry);
1883 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1885 ++vmlinux_path__nr_entries;
1890 static int vmlinux_path__init(struct perf_env *env)
1894 char *kernel_version;
1897 vmlinux_path = malloc(sizeof(char *) * (ARRAY_SIZE(vmlinux_paths) +
1898 ARRAY_SIZE(vmlinux_paths_upd)));
1899 if (vmlinux_path == NULL)
1902 for (i = 0; i < ARRAY_SIZE(vmlinux_paths); i++)
1903 if (vmlinux_path__add(vmlinux_paths[i]) < 0)
1906 /* only try kernel version if no symfs was given */
1907 if (symbol_conf.symfs[0] != 0)
1911 kernel_version = env->os_release;
1913 if (uname(&uts) < 0)
1916 kernel_version = uts.release;
1919 for (i = 0; i < ARRAY_SIZE(vmlinux_paths_upd); i++) {
1920 snprintf(bf, sizeof(bf), vmlinux_paths_upd[i], kernel_version);
1921 if (vmlinux_path__add(bf) < 0)
1928 vmlinux_path__exit();
1932 int setup_list(struct strlist **list, const char *list_str,
1933 const char *list_name)
1935 if (list_str == NULL)
1938 *list = strlist__new(list_str, NULL);
1940 pr_err("problems parsing %s list\n", list_name);
1944 symbol_conf.has_filter = true;
1948 int setup_intlist(struct intlist **list, const char *list_str,
1949 const char *list_name)
1951 if (list_str == NULL)
1954 *list = intlist__new(list_str);
1956 pr_err("problems parsing %s list\n", list_name);
1962 static bool symbol__read_kptr_restrict(void)
1965 FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
1970 if (fgets(line, sizeof(line), fp) != NULL)
1971 value = ((geteuid() != 0) || (getuid() != 0)) ?
1981 int symbol__annotation_init(void)
1983 if (symbol_conf.initialized) {
1984 pr_err("Annotation needs to be init before symbol__init()\n");
1988 if (symbol_conf.init_annotation) {
1989 pr_warning("Annotation being initialized multiple times\n");
1993 symbol_conf.priv_size += sizeof(struct annotation);
1994 symbol_conf.init_annotation = true;
1998 int symbol__init(struct perf_env *env)
2002 if (symbol_conf.initialized)
2005 symbol_conf.priv_size = PERF_ALIGN(symbol_conf.priv_size, sizeof(u64));
2009 if (symbol_conf.sort_by_name)
2010 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
2011 sizeof(struct symbol));
2013 if (symbol_conf.try_vmlinux_path && vmlinux_path__init(env) < 0)
2016 if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
2017 pr_err("'.' is the only non valid --field-separator argument\n");
2021 if (setup_list(&symbol_conf.dso_list,
2022 symbol_conf.dso_list_str, "dso") < 0)
2025 if (setup_list(&symbol_conf.comm_list,
2026 symbol_conf.comm_list_str, "comm") < 0)
2027 goto out_free_dso_list;
2029 if (setup_intlist(&symbol_conf.pid_list,
2030 symbol_conf.pid_list_str, "pid") < 0)
2031 goto out_free_comm_list;
2033 if (setup_intlist(&symbol_conf.tid_list,
2034 symbol_conf.tid_list_str, "tid") < 0)
2035 goto out_free_pid_list;
2037 if (setup_list(&symbol_conf.sym_list,
2038 symbol_conf.sym_list_str, "symbol") < 0)
2039 goto out_free_tid_list;
2041 if (setup_list(&symbol_conf.bt_stop_list,
2042 symbol_conf.bt_stop_list_str, "symbol") < 0)
2043 goto out_free_sym_list;
2046 * A path to symbols of "/" is identical to ""
2047 * reset here for simplicity.
2049 symfs = realpath(symbol_conf.symfs, NULL);
2051 symfs = symbol_conf.symfs;
2052 if (strcmp(symfs, "/") == 0)
2053 symbol_conf.symfs = "";
2054 if (symfs != symbol_conf.symfs)
2055 free((void *)symfs);
2057 symbol_conf.kptr_restrict = symbol__read_kptr_restrict();
2059 symbol_conf.initialized = true;
2063 strlist__delete(symbol_conf.sym_list);
2065 intlist__delete(symbol_conf.tid_list);
2067 intlist__delete(symbol_conf.pid_list);
2069 strlist__delete(symbol_conf.comm_list);
2071 strlist__delete(symbol_conf.dso_list);
2075 void symbol__exit(void)
2077 if (!symbol_conf.initialized)
2079 strlist__delete(symbol_conf.bt_stop_list);
2080 strlist__delete(symbol_conf.sym_list);
2081 strlist__delete(symbol_conf.dso_list);
2082 strlist__delete(symbol_conf.comm_list);
2083 intlist__delete(symbol_conf.tid_list);
2084 intlist__delete(symbol_conf.pid_list);
2085 vmlinux_path__exit();
2086 symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
2087 symbol_conf.bt_stop_list = NULL;
2088 symbol_conf.initialized = false;
2091 int symbol__config_symfs(const struct option *opt __maybe_unused,
2092 const char *dir, int unset __maybe_unused)
2097 symbol_conf.symfs = strdup(dir);
2098 if (symbol_conf.symfs == NULL)
2101 /* skip the locally configured cache if a symfs is given, and
2102 * config buildid dir to symfs/.debug
2104 ret = asprintf(&bf, "%s/%s", dir, ".debug");
2108 set_buildid_dir(bf);