24 #include <symbol/kallsyms.h>
25 #include <sys/utsname.h>
27 static int dso__load_kernel_sym(struct dso *dso, struct map *map);
28 static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map);
29 static bool symbol__is_idle(const char *name);
31 int vmlinux_path__nr_entries;
34 struct symbol_conf symbol_conf = {
36 .try_vmlinux_path = true,
39 .demangle_kernel = false,
40 .cumulate_callchain = true,
41 .show_hist_headers = true,
46 static enum dso_binary_type binary_type_symtab[] = {
47 DSO_BINARY_TYPE__KALLSYMS,
48 DSO_BINARY_TYPE__GUEST_KALLSYMS,
49 DSO_BINARY_TYPE__JAVA_JIT,
50 DSO_BINARY_TYPE__DEBUGLINK,
51 DSO_BINARY_TYPE__BUILD_ID_CACHE,
52 DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
53 DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
54 DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
55 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
56 DSO_BINARY_TYPE__GUEST_KMODULE,
57 DSO_BINARY_TYPE__GUEST_KMODULE_COMP,
58 DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE,
59 DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP,
60 DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO,
61 DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO,
62 DSO_BINARY_TYPE__NOT_FOUND,
65 #define DSO_BINARY_TYPE__SYMTAB_CNT ARRAY_SIZE(binary_type_symtab)
67 bool symbol_type__is_a(char symbol_type, enum map_type map_type)
69 symbol_type = toupper(symbol_type);
73 return symbol_type == 'T' || symbol_type == 'W';
75 return symbol_type == 'D';
81 static int prefix_underscores_count(const char *str)
83 const char *tail = str;
91 int __weak arch__choose_best_symbol(struct symbol *syma,
92 struct symbol *symb __maybe_unused)
94 /* Avoid "SyS" kernel syscall aliases */
95 if (strlen(syma->name) >= 3 && !strncmp(syma->name, "SyS", 3))
97 if (strlen(syma->name) >= 10 && !strncmp(syma->name, "compat_SyS", 10))
103 static int choose_best_symbol(struct symbol *syma, struct symbol *symb)
109 /* Prefer a symbol with non zero length */
110 a = syma->end - syma->start;
111 b = symb->end - symb->start;
112 if ((b == 0) && (a > 0))
114 else if ((a == 0) && (b > 0))
117 /* Prefer a non weak symbol over a weak one */
118 a = syma->binding == STB_WEAK;
119 b = symb->binding == STB_WEAK;
125 /* Prefer a global symbol over a non global one */
126 a = syma->binding == STB_GLOBAL;
127 b = symb->binding == STB_GLOBAL;
133 /* Prefer a symbol with less underscores */
134 a = prefix_underscores_count(syma->name);
135 b = prefix_underscores_count(symb->name);
141 /* Choose the symbol with the longest name */
142 na = strlen(syma->name);
143 nb = strlen(symb->name);
149 return arch__choose_best_symbol(syma, symb);
152 void symbols__fixup_duplicate(struct rb_root *symbols)
155 struct symbol *curr, *next;
157 if (symbol_conf.allow_aliases)
160 nd = rb_first(symbols);
163 curr = rb_entry(nd, struct symbol, rb_node);
165 nd = rb_next(&curr->rb_node);
166 next = rb_entry(nd, struct symbol, rb_node);
171 if (curr->start != next->start)
174 if (choose_best_symbol(curr, next) == SYMBOL_A) {
175 rb_erase(&next->rb_node, symbols);
176 symbol__delete(next);
179 nd = rb_next(&curr->rb_node);
180 rb_erase(&curr->rb_node, symbols);
181 symbol__delete(curr);
186 void symbols__fixup_end(struct rb_root *symbols)
188 struct rb_node *nd, *prevnd = rb_first(symbols);
189 struct symbol *curr, *prev;
194 curr = rb_entry(prevnd, struct symbol, rb_node);
196 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
198 curr = rb_entry(nd, struct symbol, rb_node);
200 if (prev->end == prev->start && prev->end != curr->start)
201 prev->end = curr->start;
205 if (curr->end == curr->start)
206 curr->end = roundup(curr->start, 4096) + 4096;
209 void __map_groups__fixup_end(struct map_groups *mg, enum map_type type)
211 struct maps *maps = &mg->maps[type];
212 struct map *next, *curr;
214 pthread_rwlock_wrlock(&maps->lock);
216 curr = maps__first(maps);
220 for (next = map__next(curr); next; next = map__next(curr)) {
222 curr->end = next->start;
227 * We still haven't the actual symbols, so guess the
228 * 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,
518 u64 start, u64 size))
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 size = strtoul(sep + 1, &endptr, 0);
565 if (*endptr != ' ' && *endptr != '\t')
568 err = process_module(arg, name, start, size);
578 struct process_kallsyms_args {
584 * These are symbols in the kernel image, so make sure that
585 * sym is from a kernel DSO.
587 static bool symbol__is_idle(const char *name)
589 const char * const idle_symbols[] = {
598 "mwait_idle_with_hints",
600 "ppc64_runlatch_off",
601 "pseries_dedicated_idle_sleep",
606 for (i = 0; idle_symbols[i]; i++) {
607 if (!strcmp(idle_symbols[i], name))
614 static int map__process_kallsym_symbol(void *arg, const char *name,
615 char type, u64 start)
618 struct process_kallsyms_args *a = arg;
619 struct rb_root *root = &a->dso->symbols[a->map->type];
621 if (!symbol_type__is_a(type, a->map->type))
625 * module symbols are not sorted so we add all
626 * symbols, setting length to 0, and rely on
627 * symbols__fixup_end() to fix it up.
629 sym = symbol__new(start, 0, kallsyms2elf_binding(type), name);
633 * We will pass the symbols to the filter later, in
634 * map__split_kallsyms, when we have split the maps per module
636 __symbols__insert(root, sym, !strchr(name, '['));
642 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
643 * so that we can in the next step set the symbol ->end address and then
644 * call kernel_maps__split_kallsyms.
646 static int dso__load_all_kallsyms(struct dso *dso, const char *filename,
649 struct process_kallsyms_args args = { .map = map, .dso = dso, };
650 return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
653 static int dso__split_kallsyms_for_kcore(struct dso *dso, struct map *map)
655 struct map_groups *kmaps = map__kmaps(map);
656 struct map *curr_map;
659 struct rb_root old_root = dso->symbols[map->type];
660 struct rb_root *root = &dso->symbols[map->type];
661 struct rb_node *next = rb_first(root);
671 pos = rb_entry(next, struct symbol, rb_node);
672 next = rb_next(&pos->rb_node);
674 rb_erase_init(&pos->rb_node, &old_root);
676 module = strchr(pos->name, '\t');
680 curr_map = map_groups__find(kmaps, map->type, pos->start);
687 pos->start -= curr_map->start - curr_map->pgoff;
689 pos->end -= curr_map->start - curr_map->pgoff;
690 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
694 /* Symbols have been adjusted */
695 dso->adjust_symbols = 1;
701 * Split the symbols into maps, making sure there are no overlaps, i.e. the
702 * kernel range is broken in several maps, named [kernel].N, as we don't have
703 * the original ELF section names vmlinux have.
705 static int dso__split_kallsyms(struct dso *dso, struct map *map, u64 delta)
707 struct map_groups *kmaps = map__kmaps(map);
708 struct machine *machine;
709 struct map *curr_map = map;
711 int count = 0, moved = 0;
712 struct rb_root *root = &dso->symbols[map->type];
713 struct rb_node *next = rb_first(root);
714 int kernel_range = 0;
719 machine = kmaps->machine;
724 pos = rb_entry(next, struct symbol, rb_node);
725 next = rb_next(&pos->rb_node);
727 module = strchr(pos->name, '\t');
729 if (!symbol_conf.use_modules)
734 if (strcmp(curr_map->dso->short_name, module)) {
735 if (curr_map != map &&
736 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
737 machine__is_default_guest(machine)) {
739 * We assume all symbols of a module are
740 * continuous in * kallsyms, so curr_map
741 * points to a module and all its
742 * symbols are in its kmap. Mark it as
745 dso__set_loaded(curr_map->dso,
749 curr_map = map_groups__find_by_name(kmaps,
751 if (curr_map == NULL) {
752 pr_debug("%s/proc/{kallsyms,modules} "
753 "inconsistency while looking "
754 "for \"%s\" module!\n",
755 machine->root_dir, module);
760 if (curr_map->dso->loaded &&
761 !machine__is_default_guest(machine))
765 * So that we look just like we get from .ko files,
766 * i.e. not prelinked, relative to map->start.
768 pos->start = curr_map->map_ip(curr_map, pos->start);
769 pos->end = curr_map->map_ip(curr_map, pos->end);
770 } else if (curr_map != map) {
771 char dso_name[PATH_MAX];
775 /* Kernel was relocated at boot time */
785 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
786 snprintf(dso_name, sizeof(dso_name),
790 snprintf(dso_name, sizeof(dso_name),
794 ndso = dso__new(dso_name);
798 ndso->kernel = dso->kernel;
800 curr_map = map__new2(pos->start, ndso, map->type);
801 if (curr_map == NULL) {
806 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
807 map_groups__insert(kmaps, curr_map);
810 /* Kernel was relocated at boot time */
815 if (curr_map != map) {
816 rb_erase(&pos->rb_node, root);
817 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
824 rb_erase(&pos->rb_node, root);
828 if (curr_map != map &&
829 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
830 machine__is_default_guest(kmaps->machine)) {
831 dso__set_loaded(curr_map->dso, curr_map->type);
834 return count + moved;
837 bool symbol__restricted_filename(const char *filename,
838 const char *restricted_filename)
840 bool restricted = false;
842 if (symbol_conf.kptr_restrict) {
843 char *r = realpath(filename, NULL);
846 restricted = strcmp(r, restricted_filename) == 0;
856 struct rb_node rb_node;
861 static void add_module(struct module_info *mi, struct rb_root *modules)
863 struct rb_node **p = &modules->rb_node;
864 struct rb_node *parent = NULL;
865 struct module_info *m;
869 m = rb_entry(parent, struct module_info, rb_node);
870 if (strcmp(mi->name, m->name) < 0)
875 rb_link_node(&mi->rb_node, parent, p);
876 rb_insert_color(&mi->rb_node, modules);
879 static void delete_modules(struct rb_root *modules)
881 struct module_info *mi;
882 struct rb_node *next = rb_first(modules);
885 mi = rb_entry(next, struct module_info, rb_node);
886 next = rb_next(&mi->rb_node);
887 rb_erase(&mi->rb_node, modules);
893 static struct module_info *find_module(const char *name,
894 struct rb_root *modules)
896 struct rb_node *n = modules->rb_node;
899 struct module_info *m;
902 m = rb_entry(n, struct module_info, rb_node);
903 cmp = strcmp(name, m->name);
915 static int __read_proc_modules(void *arg, const char *name, u64 start,
916 u64 size __maybe_unused)
918 struct rb_root *modules = arg;
919 struct module_info *mi;
921 mi = zalloc(sizeof(struct module_info));
925 mi->name = strdup(name);
933 add_module(mi, modules);
938 static int read_proc_modules(const char *filename, struct rb_root *modules)
940 if (symbol__restricted_filename(filename, "/proc/modules"))
943 if (modules__parse(filename, modules, __read_proc_modules)) {
944 delete_modules(modules);
951 int compare_proc_modules(const char *from, const char *to)
953 struct rb_root from_modules = RB_ROOT;
954 struct rb_root to_modules = RB_ROOT;
955 struct rb_node *from_node, *to_node;
956 struct module_info *from_m, *to_m;
959 if (read_proc_modules(from, &from_modules))
962 if (read_proc_modules(to, &to_modules))
963 goto out_delete_from;
965 from_node = rb_first(&from_modules);
966 to_node = rb_first(&to_modules);
971 from_m = rb_entry(from_node, struct module_info, rb_node);
972 to_m = rb_entry(to_node, struct module_info, rb_node);
974 if (from_m->start != to_m->start ||
975 strcmp(from_m->name, to_m->name))
978 from_node = rb_next(from_node);
979 to_node = rb_next(to_node);
982 if (!from_node && !to_node)
985 delete_modules(&to_modules);
987 delete_modules(&from_modules);
992 static int do_validate_kcore_modules(const char *filename, struct map *map,
993 struct map_groups *kmaps)
995 struct rb_root modules = RB_ROOT;
999 err = read_proc_modules(filename, &modules);
1003 old_map = map_groups__first(kmaps, map->type);
1005 struct map *next = map_groups__next(old_map);
1006 struct module_info *mi;
1008 if (old_map == map || old_map->start == map->start) {
1009 /* The kernel map */
1014 /* Module must be in memory at the same address */
1015 mi = find_module(old_map->dso->short_name, &modules);
1016 if (!mi || mi->start != old_map->start) {
1024 delete_modules(&modules);
1029 * If kallsyms is referenced by name then we look for filename in the same
1032 static bool filename_from_kallsyms_filename(char *filename,
1033 const char *base_name,
1034 const char *kallsyms_filename)
1038 strcpy(filename, kallsyms_filename);
1039 name = strrchr(filename, '/');
1045 if (!strcmp(name, "kallsyms")) {
1046 strcpy(name, base_name);
1053 static int validate_kcore_modules(const char *kallsyms_filename,
1056 struct map_groups *kmaps = map__kmaps(map);
1057 char modules_filename[PATH_MAX];
1062 if (!filename_from_kallsyms_filename(modules_filename, "modules",
1066 if (do_validate_kcore_modules(modules_filename, map, kmaps))
1072 static int validate_kcore_addresses(const char *kallsyms_filename,
1075 struct kmap *kmap = map__kmap(map);
1080 if (kmap->ref_reloc_sym && kmap->ref_reloc_sym->name) {
1083 start = kallsyms__get_function_start(kallsyms_filename,
1084 kmap->ref_reloc_sym->name);
1085 if (start != kmap->ref_reloc_sym->addr)
1089 return validate_kcore_modules(kallsyms_filename, map);
1092 struct kcore_mapfn_data {
1095 struct list_head maps;
1098 static int kcore_mapfn(u64 start, u64 len, u64 pgoff, void *data)
1100 struct kcore_mapfn_data *md = data;
1103 map = map__new2(start, md->dso, md->type);
1107 map->end = map->start + len;
1110 list_add(&map->node, &md->maps);
1115 static int dso__load_kcore(struct dso *dso, struct map *map,
1116 const char *kallsyms_filename)
1118 struct map_groups *kmaps = map__kmaps(map);
1119 struct machine *machine;
1120 struct kcore_mapfn_data md;
1121 struct map *old_map, *new_map, *replacement_map = NULL;
1124 char kcore_filename[PATH_MAX];
1130 machine = kmaps->machine;
1132 /* This function requires that the map is the kernel map */
1133 if (map != machine->vmlinux_maps[map->type])
1136 if (!filename_from_kallsyms_filename(kcore_filename, "kcore",
1140 /* Modules and kernel must be present at their original addresses */
1141 if (validate_kcore_addresses(kallsyms_filename, map))
1145 md.type = map->type;
1146 INIT_LIST_HEAD(&md.maps);
1148 fd = open(kcore_filename, O_RDONLY);
1150 pr_debug("Failed to open %s. Note /proc/kcore requires CAP_SYS_RAWIO capability to access.\n",
1155 /* Read new maps into temporary lists */
1156 err = file__read_maps(fd, md.type == MAP__FUNCTION, kcore_mapfn, &md,
1160 dso->is_64_bit = is_64_bit;
1162 if (list_empty(&md.maps)) {
1167 /* Remove old maps */
1168 old_map = map_groups__first(kmaps, map->type);
1170 struct map *next = map_groups__next(old_map);
1173 map_groups__remove(kmaps, old_map);
1177 /* Find the kernel map using the first symbol */
1178 sym = dso__first_symbol(dso, map->type);
1179 list_for_each_entry(new_map, &md.maps, node) {
1180 if (sym && sym->start >= new_map->start &&
1181 sym->start < new_map->end) {
1182 replacement_map = new_map;
1187 if (!replacement_map)
1188 replacement_map = list_entry(md.maps.next, struct map, node);
1191 while (!list_empty(&md.maps)) {
1192 new_map = list_entry(md.maps.next, struct map, node);
1193 list_del_init(&new_map->node);
1194 if (new_map == replacement_map) {
1195 map->start = new_map->start;
1196 map->end = new_map->end;
1197 map->pgoff = new_map->pgoff;
1198 map->map_ip = new_map->map_ip;
1199 map->unmap_ip = new_map->unmap_ip;
1200 /* Ensure maps are correctly ordered */
1202 map_groups__remove(kmaps, map);
1203 map_groups__insert(kmaps, map);
1206 map_groups__insert(kmaps, new_map);
1213 * Set the data type and long name so that kcore can be read via
1214 * dso__data_read_addr().
1216 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1217 dso->binary_type = DSO_BINARY_TYPE__GUEST_KCORE;
1219 dso->binary_type = DSO_BINARY_TYPE__KCORE;
1220 dso__set_long_name(dso, strdup(kcore_filename), true);
1224 if (map->type == MAP__FUNCTION)
1225 pr_debug("Using %s for kernel object code\n", kcore_filename);
1227 pr_debug("Using %s for kernel data\n", kcore_filename);
1232 while (!list_empty(&md.maps)) {
1233 map = list_entry(md.maps.next, struct map, node);
1234 list_del_init(&map->node);
1242 * If the kernel is relocated at boot time, kallsyms won't match. Compute the
1243 * delta based on the relocation reference symbol.
1245 static int kallsyms__delta(struct map *map, const char *filename, u64 *delta)
1247 struct kmap *kmap = map__kmap(map);
1253 if (!kmap->ref_reloc_sym || !kmap->ref_reloc_sym->name)
1256 addr = kallsyms__get_function_start(filename,
1257 kmap->ref_reloc_sym->name);
1261 *delta = addr - kmap->ref_reloc_sym->addr;
1265 int __dso__load_kallsyms(struct dso *dso, const char *filename,
1266 struct map *map, bool no_kcore)
1270 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
1273 if (dso__load_all_kallsyms(dso, filename, map) < 0)
1276 if (kallsyms__delta(map, filename, &delta))
1279 symbols__fixup_end(&dso->symbols[map->type]);
1280 symbols__fixup_duplicate(&dso->symbols[map->type]);
1282 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1283 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
1285 dso->symtab_type = DSO_BINARY_TYPE__KALLSYMS;
1287 if (!no_kcore && !dso__load_kcore(dso, map, filename))
1288 return dso__split_kallsyms_for_kcore(dso, map);
1290 return dso__split_kallsyms(dso, map, delta);
1293 int dso__load_kallsyms(struct dso *dso, const char *filename,
1296 return __dso__load_kallsyms(dso, filename, map, false);
1299 static int dso__load_perf_map(struct dso *dso, struct map *map)
1306 file = fopen(dso->long_name, "r");
1310 while (!feof(file)) {
1315 line_len = getline(&line, &n, file);
1322 line[--line_len] = '\0'; /* \n */
1324 len = hex2u64(line, &start);
1327 if (len + 2 >= line_len)
1330 len += hex2u64(line + len, &size);
1333 if (len + 2 >= line_len)
1336 sym = symbol__new(start, size, STB_GLOBAL, line + len);
1339 goto out_delete_line;
1341 symbols__insert(&dso->symbols[map->type], sym);
1356 static bool dso__is_compatible_symtab_type(struct dso *dso, bool kmod,
1357 enum dso_binary_type type)
1360 case DSO_BINARY_TYPE__JAVA_JIT:
1361 case DSO_BINARY_TYPE__DEBUGLINK:
1362 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
1363 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
1364 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
1365 case DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO:
1366 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
1367 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
1368 return !kmod && dso->kernel == DSO_TYPE_USER;
1370 case DSO_BINARY_TYPE__KALLSYMS:
1371 case DSO_BINARY_TYPE__VMLINUX:
1372 case DSO_BINARY_TYPE__KCORE:
1373 return dso->kernel == DSO_TYPE_KERNEL;
1375 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
1376 case DSO_BINARY_TYPE__GUEST_VMLINUX:
1377 case DSO_BINARY_TYPE__GUEST_KCORE:
1378 return dso->kernel == DSO_TYPE_GUEST_KERNEL;
1380 case DSO_BINARY_TYPE__GUEST_KMODULE:
1381 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
1382 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
1383 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
1385 * kernel modules know their symtab type - it's set when
1386 * creating a module dso in machine__findnew_module_map().
1388 return kmod && dso->symtab_type == type;
1390 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
1393 case DSO_BINARY_TYPE__NOT_FOUND:
1399 int dso__load(struct dso *dso, struct map *map)
1404 struct machine *machine;
1405 char *root_dir = (char *) "";
1407 struct symsrc ss_[2];
1408 struct symsrc *syms_ss = NULL, *runtime_ss = NULL;
1410 unsigned char build_id[BUILD_ID_SIZE];
1412 pthread_mutex_lock(&dso->lock);
1414 /* check again under the dso->lock */
1415 if (dso__loaded(dso, map->type)) {
1421 if (dso->kernel == DSO_TYPE_KERNEL)
1422 ret = dso__load_kernel_sym(dso, map);
1423 else if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1424 ret = dso__load_guest_kernel_sym(dso, map);
1429 if (map->groups && map->groups->machine)
1430 machine = map->groups->machine;
1434 dso->adjust_symbols = 0;
1436 if (strncmp(dso->name, "/tmp/perf-", 10) == 0) {
1439 if (lstat(dso->name, &st) < 0)
1442 if (!symbol_conf.force && st.st_uid && (st.st_uid != geteuid())) {
1443 pr_warning("File %s not owned by current user or root, "
1444 "ignoring it (use -f to override).\n", dso->name);
1448 ret = dso__load_perf_map(dso, map);
1449 dso->symtab_type = ret > 0 ? DSO_BINARY_TYPE__JAVA_JIT :
1450 DSO_BINARY_TYPE__NOT_FOUND;
1455 root_dir = machine->root_dir;
1457 name = malloc(PATH_MAX);
1461 kmod = dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
1462 dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
1463 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE ||
1464 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
1468 * Read the build id if possible. This is required for
1469 * DSO_BINARY_TYPE__BUILDID_DEBUGINFO to work
1471 if (!dso->has_build_id &&
1472 is_regular_file(dso->long_name) &&
1473 filename__read_build_id(dso->long_name, build_id, BUILD_ID_SIZE) > 0)
1474 dso__set_build_id(dso, build_id);
1477 * Iterate over candidate debug images.
1478 * Keep track of "interesting" ones (those which have a symtab, dynsym,
1479 * and/or opd section) for processing.
1481 for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) {
1482 struct symsrc *ss = &ss_[ss_pos];
1483 bool next_slot = false;
1485 enum dso_binary_type symtab_type = binary_type_symtab[i];
1487 if (!dso__is_compatible_symtab_type(dso, kmod, symtab_type))
1490 if (dso__read_binary_type_filename(dso, symtab_type,
1491 root_dir, name, PATH_MAX))
1494 if (!is_regular_file(name))
1497 /* Name is now the name of the next image to try */
1498 if (symsrc__init(ss, dso, name, symtab_type) < 0)
1501 if (!syms_ss && symsrc__has_symtab(ss)) {
1504 if (!dso->symsrc_filename)
1505 dso->symsrc_filename = strdup(name);
1508 if (!runtime_ss && symsrc__possibly_runtime(ss)) {
1516 if (syms_ss && runtime_ss)
1519 symsrc__destroy(ss);
1524 if (!runtime_ss && !syms_ss)
1527 if (runtime_ss && !syms_ss) {
1528 syms_ss = runtime_ss;
1531 /* We'll have to hope for the best */
1532 if (!runtime_ss && syms_ss)
1533 runtime_ss = syms_ss;
1535 if (syms_ss && syms_ss->type == DSO_BINARY_TYPE__BUILD_ID_CACHE)
1536 if (dso__build_id_is_kmod(dso, name, PATH_MAX))
1540 ret = dso__load_sym(dso, map, syms_ss, runtime_ss, kmod);
1547 nr_plt = dso__synthesize_plt_symbols(dso, runtime_ss, map);
1552 for (; ss_pos > 0; ss_pos--)
1553 symsrc__destroy(&ss_[ss_pos - 1]);
1556 if (ret < 0 && strstr(dso->name, " (deleted)") != NULL)
1559 dso__set_loaded(dso, map->type);
1560 pthread_mutex_unlock(&dso->lock);
1565 struct map *map_groups__find_by_name(struct map_groups *mg,
1566 enum map_type type, const char *name)
1568 struct maps *maps = &mg->maps[type];
1571 pthread_rwlock_rdlock(&maps->lock);
1573 for (map = maps__first(maps); map; map = map__next(map)) {
1574 if (map->dso && strcmp(map->dso->short_name, name) == 0)
1581 pthread_rwlock_unlock(&maps->lock);
1585 int dso__load_vmlinux(struct dso *dso, struct map *map,
1586 const char *vmlinux, bool vmlinux_allocated)
1590 char symfs_vmlinux[PATH_MAX];
1591 enum dso_binary_type symtab_type;
1593 if (vmlinux[0] == '/')
1594 snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s", vmlinux);
1596 symbol__join_symfs(symfs_vmlinux, vmlinux);
1598 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1599 symtab_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
1601 symtab_type = DSO_BINARY_TYPE__VMLINUX;
1603 if (symsrc__init(&ss, dso, symfs_vmlinux, symtab_type))
1606 err = dso__load_sym(dso, map, &ss, &ss, 0);
1607 symsrc__destroy(&ss);
1610 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1611 dso->binary_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
1613 dso->binary_type = DSO_BINARY_TYPE__VMLINUX;
1614 dso__set_long_name(dso, vmlinux, vmlinux_allocated);
1615 dso__set_loaded(dso, map->type);
1616 pr_debug("Using %s for symbols\n", symfs_vmlinux);
1622 int dso__load_vmlinux_path(struct dso *dso, struct map *map)
1625 char *filename = NULL;
1627 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1628 vmlinux_path__nr_entries + 1);
1630 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
1631 err = dso__load_vmlinux(dso, map, vmlinux_path[i], false);
1636 if (!symbol_conf.ignore_vmlinux_buildid)
1637 filename = dso__build_id_filename(dso, NULL, 0);
1638 if (filename != NULL) {
1639 err = dso__load_vmlinux(dso, map, filename, true);
1648 static bool visible_dir_filter(const char *name, struct dirent *d)
1650 if (d->d_type != DT_DIR)
1652 return lsdir_no_dot_filter(name, d);
1655 static int find_matching_kcore(struct map *map, char *dir, size_t dir_sz)
1657 char kallsyms_filename[PATH_MAX];
1659 struct strlist *dirs;
1660 struct str_node *nd;
1662 dirs = lsdir(dir, visible_dir_filter);
1666 strlist__for_each_entry(nd, dirs) {
1667 scnprintf(kallsyms_filename, sizeof(kallsyms_filename),
1668 "%s/%s/kallsyms", dir, nd->s);
1669 if (!validate_kcore_addresses(kallsyms_filename, map)) {
1670 strlcpy(dir, kallsyms_filename, dir_sz);
1676 strlist__delete(dirs);
1682 * Use open(O_RDONLY) to check readability directly instead of access(R_OK)
1683 * since access(R_OK) only checks with real UID/GID but open() use effective
1684 * UID/GID and actual capabilities (e.g. /proc/kcore requires CAP_SYS_RAWIO).
1686 static bool filename__readable(const char *file)
1688 int fd = open(file, O_RDONLY);
1695 static char *dso__find_kallsyms(struct dso *dso, struct map *map)
1697 u8 host_build_id[BUILD_ID_SIZE];
1698 char sbuild_id[SBUILD_ID_SIZE];
1699 bool is_host = false;
1700 char path[PATH_MAX];
1702 if (!dso->has_build_id) {
1704 * Last resort, if we don't have a build-id and couldn't find
1705 * any vmlinux file, try the running kernel kallsyms table.
1710 if (sysfs__read_build_id("/sys/kernel/notes", host_build_id,
1711 sizeof(host_build_id)) == 0)
1712 is_host = dso__build_id_equal(dso, host_build_id);
1714 /* Try a fast path for /proc/kallsyms if possible */
1717 * Do not check the build-id cache, unless we know we cannot use
1718 * /proc/kcore or module maps don't match to /proc/kallsyms.
1719 * To check readability of /proc/kcore, do not use access(R_OK)
1720 * since /proc/kcore requires CAP_SYS_RAWIO to read and access
1723 if (filename__readable("/proc/kcore") &&
1724 !validate_kcore_addresses("/proc/kallsyms", map))
1728 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1730 /* Find kallsyms in build-id cache with kcore */
1731 scnprintf(path, sizeof(path), "%s/%s/%s",
1732 buildid_dir, DSO__NAME_KCORE, sbuild_id);
1734 if (!find_matching_kcore(map, path, sizeof(path)))
1735 return strdup(path);
1737 /* Use current /proc/kallsyms if possible */
1740 return strdup("/proc/kallsyms");
1743 /* Finally, find a cache of kallsyms */
1744 if (!build_id_cache__kallsyms_path(sbuild_id, path, sizeof(path))) {
1745 pr_err("No kallsyms or vmlinux with build-id %s was found\n",
1750 return strdup(path);
1753 static int dso__load_kernel_sym(struct dso *dso, struct map *map)
1756 const char *kallsyms_filename = NULL;
1757 char *kallsyms_allocated_filename = NULL;
1759 * Step 1: if the user specified a kallsyms or vmlinux filename, use
1760 * it and only it, reporting errors to the user if it cannot be used.
1762 * For instance, try to analyse an ARM perf.data file _without_ a
1763 * build-id, or if the user specifies the wrong path to the right
1764 * vmlinux file, obviously we can't fallback to another vmlinux (a
1765 * x86_86 one, on the machine where analysis is being performed, say),
1766 * or worse, /proc/kallsyms.
1768 * If the specified file _has_ a build-id and there is a build-id
1769 * section in the perf.data file, we will still do the expected
1770 * validation in dso__load_vmlinux and will bail out if they don't
1773 if (symbol_conf.kallsyms_name != NULL) {
1774 kallsyms_filename = symbol_conf.kallsyms_name;
1778 if (!symbol_conf.ignore_vmlinux && symbol_conf.vmlinux_name != NULL) {
1779 return dso__load_vmlinux(dso, map, symbol_conf.vmlinux_name, false);
1782 if (!symbol_conf.ignore_vmlinux && vmlinux_path != NULL) {
1783 err = dso__load_vmlinux_path(dso, map);
1788 /* do not try local files if a symfs was given */
1789 if (symbol_conf.symfs[0] != 0)
1792 kallsyms_allocated_filename = dso__find_kallsyms(dso, map);
1793 if (!kallsyms_allocated_filename)
1796 kallsyms_filename = kallsyms_allocated_filename;
1799 err = dso__load_kallsyms(dso, kallsyms_filename, map);
1801 pr_debug("Using %s for symbols\n", kallsyms_filename);
1802 free(kallsyms_allocated_filename);
1804 if (err > 0 && !dso__is_kcore(dso)) {
1805 dso->binary_type = DSO_BINARY_TYPE__KALLSYMS;
1806 dso__set_long_name(dso, DSO__NAME_KALLSYMS, false);
1807 map__fixup_start(map);
1808 map__fixup_end(map);
1814 static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map)
1817 const char *kallsyms_filename = NULL;
1818 struct machine *machine;
1819 char path[PATH_MAX];
1822 pr_debug("Guest kernel map hasn't the point to groups\n");
1825 machine = map->groups->machine;
1827 if (machine__is_default_guest(machine)) {
1829 * if the user specified a vmlinux filename, use it and only
1830 * it, reporting errors to the user if it cannot be used.
1831 * Or use file guest_kallsyms inputted by user on commandline
1833 if (symbol_conf.default_guest_vmlinux_name != NULL) {
1834 err = dso__load_vmlinux(dso, map,
1835 symbol_conf.default_guest_vmlinux_name,
1840 kallsyms_filename = symbol_conf.default_guest_kallsyms;
1841 if (!kallsyms_filename)
1844 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
1845 kallsyms_filename = path;
1848 err = dso__load_kallsyms(dso, kallsyms_filename, map);
1850 pr_debug("Using %s for symbols\n", kallsyms_filename);
1851 if (err > 0 && !dso__is_kcore(dso)) {
1852 dso->binary_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
1853 machine__mmap_name(machine, path, sizeof(path));
1854 dso__set_long_name(dso, strdup(path), true);
1855 map__fixup_start(map);
1856 map__fixup_end(map);
1862 static void vmlinux_path__exit(void)
1864 while (--vmlinux_path__nr_entries >= 0)
1865 zfree(&vmlinux_path[vmlinux_path__nr_entries]);
1866 vmlinux_path__nr_entries = 0;
1868 zfree(&vmlinux_path);
1871 static const char * const vmlinux_paths[] = {
1876 static const char * const vmlinux_paths_upd[] = {
1878 "/usr/lib/debug/boot/vmlinux-%s",
1879 "/lib/modules/%s/build/vmlinux",
1880 "/usr/lib/debug/lib/modules/%s/vmlinux",
1881 "/usr/lib/debug/boot/vmlinux-%s.debug"
1884 static int vmlinux_path__add(const char *new_entry)
1886 vmlinux_path[vmlinux_path__nr_entries] = strdup(new_entry);
1887 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1889 ++vmlinux_path__nr_entries;
1894 static int vmlinux_path__init(struct perf_env *env)
1898 char *kernel_version;
1901 vmlinux_path = malloc(sizeof(char *) * (ARRAY_SIZE(vmlinux_paths) +
1902 ARRAY_SIZE(vmlinux_paths_upd)));
1903 if (vmlinux_path == NULL)
1906 for (i = 0; i < ARRAY_SIZE(vmlinux_paths); i++)
1907 if (vmlinux_path__add(vmlinux_paths[i]) < 0)
1910 /* only try kernel version if no symfs was given */
1911 if (symbol_conf.symfs[0] != 0)
1915 kernel_version = env->os_release;
1917 if (uname(&uts) < 0)
1920 kernel_version = uts.release;
1923 for (i = 0; i < ARRAY_SIZE(vmlinux_paths_upd); i++) {
1924 snprintf(bf, sizeof(bf), vmlinux_paths_upd[i], kernel_version);
1925 if (vmlinux_path__add(bf) < 0)
1932 vmlinux_path__exit();
1936 int setup_list(struct strlist **list, const char *list_str,
1937 const char *list_name)
1939 if (list_str == NULL)
1942 *list = strlist__new(list_str, NULL);
1944 pr_err("problems parsing %s list\n", list_name);
1948 symbol_conf.has_filter = true;
1952 int setup_intlist(struct intlist **list, const char *list_str,
1953 const char *list_name)
1955 if (list_str == NULL)
1958 *list = intlist__new(list_str);
1960 pr_err("problems parsing %s list\n", list_name);
1966 static bool symbol__read_kptr_restrict(void)
1969 FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
1974 if (fgets(line, sizeof(line), fp) != NULL)
1975 value = (geteuid() != 0) ?
1985 int symbol__annotation_init(void)
1987 if (symbol_conf.initialized) {
1988 pr_err("Annotation needs to be init before symbol__init()\n");
1992 if (symbol_conf.init_annotation) {
1993 pr_warning("Annotation being initialized multiple times\n");
1997 symbol_conf.priv_size += sizeof(struct annotation);
1998 symbol_conf.init_annotation = true;
2002 int symbol__init(struct perf_env *env)
2006 if (symbol_conf.initialized)
2009 symbol_conf.priv_size = PERF_ALIGN(symbol_conf.priv_size, sizeof(u64));
2013 if (symbol_conf.sort_by_name)
2014 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
2015 sizeof(struct symbol));
2017 if (symbol_conf.try_vmlinux_path && vmlinux_path__init(env) < 0)
2020 if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
2021 pr_err("'.' is the only non valid --field-separator argument\n");
2025 if (setup_list(&symbol_conf.dso_list,
2026 symbol_conf.dso_list_str, "dso") < 0)
2029 if (setup_list(&symbol_conf.comm_list,
2030 symbol_conf.comm_list_str, "comm") < 0)
2031 goto out_free_dso_list;
2033 if (setup_intlist(&symbol_conf.pid_list,
2034 symbol_conf.pid_list_str, "pid") < 0)
2035 goto out_free_comm_list;
2037 if (setup_intlist(&symbol_conf.tid_list,
2038 symbol_conf.tid_list_str, "tid") < 0)
2039 goto out_free_pid_list;
2041 if (setup_list(&symbol_conf.sym_list,
2042 symbol_conf.sym_list_str, "symbol") < 0)
2043 goto out_free_tid_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 intlist__delete(symbol_conf.tid_list);
2065 intlist__delete(symbol_conf.pid_list);
2067 strlist__delete(symbol_conf.comm_list);
2069 strlist__delete(symbol_conf.dso_list);
2073 void symbol__exit(void)
2075 if (!symbol_conf.initialized)
2077 strlist__delete(symbol_conf.sym_list);
2078 strlist__delete(symbol_conf.dso_list);
2079 strlist__delete(symbol_conf.comm_list);
2080 intlist__delete(symbol_conf.tid_list);
2081 intlist__delete(symbol_conf.pid_list);
2082 vmlinux_path__exit();
2083 symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
2084 symbol_conf.initialized = false;
2087 int symbol__config_symfs(const struct option *opt __maybe_unused,
2088 const char *dir, int unset __maybe_unused)
2093 symbol_conf.symfs = strdup(dir);
2094 if (symbol_conf.symfs == NULL)
2097 /* skip the locally configured cache if a symfs is given, and
2098 * config buildid dir to symfs/.debug
2100 ret = asprintf(&bf, "%s/%s", dir, ".debug");
2104 set_buildid_dir(bf);