21 #include <sys/utsname.h>
24 #define KSYM_NAME_LEN 256
27 static int dso__load_kernel_sym(struct dso *dso, struct map *map,
28 symbol_filter_t filter);
29 static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
30 symbol_filter_t filter);
31 int vmlinux_path__nr_entries;
34 struct symbol_conf symbol_conf = {
36 .try_vmlinux_path = true,
42 static enum dso_binary_type binary_type_symtab[] = {
43 DSO_BINARY_TYPE__KALLSYMS,
44 DSO_BINARY_TYPE__GUEST_KALLSYMS,
45 DSO_BINARY_TYPE__JAVA_JIT,
46 DSO_BINARY_TYPE__DEBUGLINK,
47 DSO_BINARY_TYPE__BUILD_ID_CACHE,
48 DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
49 DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
50 DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
51 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
52 DSO_BINARY_TYPE__GUEST_KMODULE,
53 DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE,
54 DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO,
55 DSO_BINARY_TYPE__NOT_FOUND,
58 #define DSO_BINARY_TYPE__SYMTAB_CNT ARRAY_SIZE(binary_type_symtab)
60 bool symbol_type__is_a(char symbol_type, enum map_type map_type)
62 symbol_type = toupper(symbol_type);
66 return symbol_type == 'T' || symbol_type == 'W';
68 return symbol_type == 'D';
74 static int prefix_underscores_count(const char *str)
76 const char *tail = str;
87 static int choose_best_symbol(struct symbol *syma, struct symbol *symb)
93 /* Prefer a symbol with non zero length */
94 a = syma->end - syma->start;
95 b = symb->end - symb->start;
96 if ((b == 0) && (a > 0))
98 else if ((a == 0) && (b > 0))
101 /* Prefer a non weak symbol over a weak one */
102 a = syma->binding == STB_WEAK;
103 b = symb->binding == STB_WEAK;
109 /* Prefer a global symbol over a non global one */
110 a = syma->binding == STB_GLOBAL;
111 b = symb->binding == STB_GLOBAL;
117 /* Prefer a symbol with less underscores */
118 a = prefix_underscores_count(syma->name);
119 b = prefix_underscores_count(symb->name);
125 /* Choose the symbol with the longest name */
126 na = strlen(syma->name);
127 nb = strlen(symb->name);
133 /* Avoid "SyS" kernel syscall aliases */
134 if (na >= 3 && !strncmp(syma->name, "SyS", 3))
136 if (na >= 10 && !strncmp(syma->name, "compat_SyS", 10))
142 void symbols__fixup_duplicate(struct rb_root *symbols)
145 struct symbol *curr, *next;
147 nd = rb_first(symbols);
150 curr = rb_entry(nd, struct symbol, rb_node);
152 nd = rb_next(&curr->rb_node);
153 next = rb_entry(nd, struct symbol, rb_node);
158 if (curr->start != next->start)
161 if (choose_best_symbol(curr, next) == SYMBOL_A) {
162 rb_erase(&next->rb_node, symbols);
163 symbol__delete(next);
166 nd = rb_next(&curr->rb_node);
167 rb_erase(&curr->rb_node, symbols);
168 symbol__delete(curr);
173 void symbols__fixup_end(struct rb_root *symbols)
175 struct rb_node *nd, *prevnd = rb_first(symbols);
176 struct symbol *curr, *prev;
181 curr = rb_entry(prevnd, struct symbol, rb_node);
183 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
185 curr = rb_entry(nd, struct symbol, rb_node);
187 if (prev->end == prev->start && prev->end != curr->start)
188 prev->end = curr->start - 1;
192 if (curr->end == curr->start)
193 curr->end = roundup(curr->start, 4096);
196 void __map_groups__fixup_end(struct map_groups *mg, enum map_type type)
198 struct map *prev, *curr;
199 struct rb_node *nd, *prevnd = rb_first(&mg->maps[type]);
204 curr = rb_entry(prevnd, struct map, rb_node);
206 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
208 curr = rb_entry(nd, struct map, rb_node);
209 prev->end = curr->start - 1;
213 * We still haven't the actual symbols, so guess the
214 * last map final address.
219 struct symbol *symbol__new(u64 start, u64 len, u8 binding, const char *name)
221 size_t namelen = strlen(name) + 1;
222 struct symbol *sym = calloc(1, (symbol_conf.priv_size +
223 sizeof(*sym) + namelen));
227 if (symbol_conf.priv_size)
228 sym = ((void *)sym) + symbol_conf.priv_size;
231 sym->end = len ? start + len - 1 : start;
232 sym->binding = binding;
233 sym->namelen = namelen - 1;
235 pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n",
236 __func__, name, start, sym->end);
237 memcpy(sym->name, name, namelen);
242 void symbol__delete(struct symbol *sym)
244 free(((void *)sym) - symbol_conf.priv_size);
247 size_t symbol__fprintf(struct symbol *sym, FILE *fp)
249 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %c %s\n",
250 sym->start, sym->end,
251 sym->binding == STB_GLOBAL ? 'g' :
252 sym->binding == STB_LOCAL ? 'l' : 'w',
256 size_t symbol__fprintf_symname_offs(const struct symbol *sym,
257 const struct addr_location *al, FILE *fp)
259 unsigned long offset;
262 if (sym && sym->name) {
263 length = fprintf(fp, "%s", sym->name);
265 if (al->addr < sym->end)
266 offset = al->addr - sym->start;
268 offset = al->addr - al->map->start - sym->start;
269 length += fprintf(fp, "+0x%lx", offset);
273 return fprintf(fp, "[unknown]");
276 size_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp)
278 return symbol__fprintf_symname_offs(sym, NULL, fp);
281 void symbols__delete(struct rb_root *symbols)
284 struct rb_node *next = rb_first(symbols);
287 pos = rb_entry(next, struct symbol, rb_node);
288 next = rb_next(&pos->rb_node);
289 rb_erase(&pos->rb_node, symbols);
294 void symbols__insert(struct rb_root *symbols, struct symbol *sym)
296 struct rb_node **p = &symbols->rb_node;
297 struct rb_node *parent = NULL;
298 const u64 ip = sym->start;
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 static struct symbol *symbols__find(struct rb_root *symbols, u64 ip)
320 n = symbols->rb_node;
323 struct symbol *s = rb_entry(n, struct symbol, rb_node);
327 else if (ip > s->end)
336 static struct symbol *symbols__first(struct rb_root *symbols)
338 struct rb_node *n = rb_first(symbols);
341 return rb_entry(n, struct symbol, rb_node);
346 struct symbol_name_rb_node {
347 struct rb_node rb_node;
351 static void symbols__insert_by_name(struct rb_root *symbols, struct symbol *sym)
353 struct rb_node **p = &symbols->rb_node;
354 struct rb_node *parent = NULL;
355 struct symbol_name_rb_node *symn, *s;
357 symn = container_of(sym, struct symbol_name_rb_node, sym);
361 s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
362 if (strcmp(sym->name, s->sym.name) < 0)
367 rb_link_node(&symn->rb_node, parent, p);
368 rb_insert_color(&symn->rb_node, symbols);
371 static void symbols__sort_by_name(struct rb_root *symbols,
372 struct rb_root *source)
376 for (nd = rb_first(source); nd; nd = rb_next(nd)) {
377 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
378 symbols__insert_by_name(symbols, pos);
382 static struct symbol *symbols__find_by_name(struct rb_root *symbols,
390 n = symbols->rb_node;
393 struct symbol_name_rb_node *s;
396 s = rb_entry(n, struct symbol_name_rb_node, rb_node);
397 cmp = strcmp(name, s->sym.name);
410 struct symbol *dso__find_symbol(struct dso *dso,
411 enum map_type type, u64 addr)
413 return symbols__find(&dso->symbols[type], addr);
416 struct symbol *dso__first_symbol(struct dso *dso, enum map_type type)
418 return symbols__first(&dso->symbols[type]);
421 struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
424 return symbols__find_by_name(&dso->symbol_names[type], name);
427 void dso__sort_by_name(struct dso *dso, enum map_type type)
429 dso__set_sorted_by_name(dso, type);
430 return symbols__sort_by_name(&dso->symbol_names[type],
431 &dso->symbols[type]);
434 size_t dso__fprintf_symbols_by_name(struct dso *dso,
435 enum map_type type, FILE *fp)
439 struct symbol_name_rb_node *pos;
441 for (nd = rb_first(&dso->symbol_names[type]); nd; nd = rb_next(nd)) {
442 pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
443 fprintf(fp, "%s\n", pos->sym.name);
449 int kallsyms__parse(const char *filename, void *arg,
450 int (*process_symbol)(void *arg, const char *name,
451 char type, u64 start))
456 FILE *file = fopen(filename, "r");
463 while (!feof(file)) {
469 line_len = getline(&line, &n, file);
470 if (line_len < 0 || !line)
473 line[--line_len] = '\0'; /* \n */
475 len = hex2u64(line, &start);
478 if (len + 2 >= line_len)
481 symbol_type = line[len];
483 symbol_name = line + len;
484 len = line_len - len;
486 if (len >= KSYM_NAME_LEN) {
491 err = process_symbol(arg, symbol_name,
505 int modules__parse(const char *filename, void *arg,
506 int (*process_module)(void *arg, const char *name,
514 file = fopen(filename, "r");
524 line_len = getline(&line, &n, file);
537 line[--line_len] = '\0'; /* \n */
539 sep = strrchr(line, 'x');
543 hex2u64(sep + 1, &start);
545 sep = strchr(line, ' ');
551 scnprintf(name, sizeof(name), "[%s]", line);
553 err = process_module(arg, name, start);
563 struct process_kallsyms_args {
568 static u8 kallsyms2elf_type(char type)
573 return isupper(type) ? STB_GLOBAL : STB_LOCAL;
576 static int map__process_kallsym_symbol(void *arg, const char *name,
577 char type, u64 start)
580 struct process_kallsyms_args *a = arg;
581 struct rb_root *root = &a->dso->symbols[a->map->type];
583 if (!symbol_type__is_a(type, a->map->type))
587 * module symbols are not sorted so we add all
588 * symbols, setting length to 0, and rely on
589 * symbols__fixup_end() to fix it up.
591 sym = symbol__new(start, 0, kallsyms2elf_type(type), name);
595 * We will pass the symbols to the filter later, in
596 * map__split_kallsyms, when we have split the maps per module
598 symbols__insert(root, sym);
604 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
605 * so that we can in the next step set the symbol ->end address and then
606 * call kernel_maps__split_kallsyms.
608 static int dso__load_all_kallsyms(struct dso *dso, const char *filename,
611 struct process_kallsyms_args args = { .map = map, .dso = dso, };
612 return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
615 static int dso__split_kallsyms_for_kcore(struct dso *dso, struct map *map,
616 symbol_filter_t filter)
618 struct map_groups *kmaps = map__kmap(map)->kmaps;
619 struct map *curr_map;
621 int count = 0, moved = 0;
622 struct rb_root *root = &dso->symbols[map->type];
623 struct rb_node *next = rb_first(root);
628 pos = rb_entry(next, struct symbol, rb_node);
629 next = rb_next(&pos->rb_node);
631 module = strchr(pos->name, '\t');
635 curr_map = map_groups__find(kmaps, map->type, pos->start);
637 if (!curr_map || (filter && filter(curr_map, pos))) {
638 rb_erase(&pos->rb_node, root);
641 pos->start -= curr_map->start - curr_map->pgoff;
643 pos->end -= curr_map->start - curr_map->pgoff;
644 if (curr_map != map) {
645 rb_erase(&pos->rb_node, root);
647 &curr_map->dso->symbols[curr_map->type],
656 /* Symbols have been adjusted */
657 dso->adjust_symbols = 1;
659 return count + moved;
663 * Split the symbols into maps, making sure there are no overlaps, i.e. the
664 * kernel range is broken in several maps, named [kernel].N, as we don't have
665 * the original ELF section names vmlinux have.
667 static int dso__split_kallsyms(struct dso *dso, struct map *map,
668 symbol_filter_t filter)
670 struct map_groups *kmaps = map__kmap(map)->kmaps;
671 struct machine *machine = kmaps->machine;
672 struct map *curr_map = map;
674 int count = 0, moved = 0;
675 struct rb_root *root = &dso->symbols[map->type];
676 struct rb_node *next = rb_first(root);
677 int kernel_range = 0;
682 pos = rb_entry(next, struct symbol, rb_node);
683 next = rb_next(&pos->rb_node);
685 module = strchr(pos->name, '\t');
687 if (!symbol_conf.use_modules)
692 if (strcmp(curr_map->dso->short_name, module)) {
693 if (curr_map != map &&
694 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
695 machine__is_default_guest(machine)) {
697 * We assume all symbols of a module are
698 * continuous in * kallsyms, so curr_map
699 * points to a module and all its
700 * symbols are in its kmap. Mark it as
703 dso__set_loaded(curr_map->dso,
707 curr_map = map_groups__find_by_name(kmaps,
709 if (curr_map == NULL) {
710 pr_debug("%s/proc/{kallsyms,modules} "
711 "inconsistency while looking "
712 "for \"%s\" module!\n",
713 machine->root_dir, module);
718 if (curr_map->dso->loaded &&
719 !machine__is_default_guest(machine))
723 * So that we look just like we get from .ko files,
724 * i.e. not prelinked, relative to map->start.
726 pos->start = curr_map->map_ip(curr_map, pos->start);
727 pos->end = curr_map->map_ip(curr_map, pos->end);
728 } else if (curr_map != map) {
729 char dso_name[PATH_MAX];
737 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
738 snprintf(dso_name, sizeof(dso_name),
742 snprintf(dso_name, sizeof(dso_name),
746 ndso = dso__new(dso_name);
750 ndso->kernel = dso->kernel;
752 curr_map = map__new2(pos->start, ndso, map->type);
753 if (curr_map == NULL) {
758 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
759 map_groups__insert(kmaps, curr_map);
763 if (filter && filter(curr_map, pos)) {
764 discard_symbol: rb_erase(&pos->rb_node, root);
767 if (curr_map != map) {
768 rb_erase(&pos->rb_node, root);
769 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
776 if (curr_map != map &&
777 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
778 machine__is_default_guest(kmaps->machine)) {
779 dso__set_loaded(curr_map->dso, curr_map->type);
782 return count + moved;
785 bool symbol__restricted_filename(const char *filename,
786 const char *restricted_filename)
788 bool restricted = false;
790 if (symbol_conf.kptr_restrict) {
791 char *r = realpath(filename, NULL);
794 restricted = strcmp(r, restricted_filename) == 0;
804 struct rb_node rb_node;
809 static void add_module(struct module_info *mi, struct rb_root *modules)
811 struct rb_node **p = &modules->rb_node;
812 struct rb_node *parent = NULL;
813 struct module_info *m;
817 m = rb_entry(parent, struct module_info, rb_node);
818 if (strcmp(mi->name, m->name) < 0)
823 rb_link_node(&mi->rb_node, parent, p);
824 rb_insert_color(&mi->rb_node, modules);
827 static void delete_modules(struct rb_root *modules)
829 struct module_info *mi;
830 struct rb_node *next = rb_first(modules);
833 mi = rb_entry(next, struct module_info, rb_node);
834 next = rb_next(&mi->rb_node);
835 rb_erase(&mi->rb_node, modules);
841 static struct module_info *find_module(const char *name,
842 struct rb_root *modules)
844 struct rb_node *n = modules->rb_node;
847 struct module_info *m;
850 m = rb_entry(n, struct module_info, rb_node);
851 cmp = strcmp(name, m->name);
863 static int __read_proc_modules(void *arg, const char *name, u64 start)
865 struct rb_root *modules = arg;
866 struct module_info *mi;
868 mi = zalloc(sizeof(struct module_info));
872 mi->name = strdup(name);
880 add_module(mi, modules);
885 static int read_proc_modules(const char *filename, struct rb_root *modules)
887 if (symbol__restricted_filename(filename, "/proc/modules"))
890 if (modules__parse(filename, modules, __read_proc_modules)) {
891 delete_modules(modules);
898 int compare_proc_modules(const char *from, const char *to)
900 struct rb_root from_modules = RB_ROOT;
901 struct rb_root to_modules = RB_ROOT;
902 struct rb_node *from_node, *to_node;
903 struct module_info *from_m, *to_m;
906 if (read_proc_modules(from, &from_modules))
909 if (read_proc_modules(to, &to_modules))
910 goto out_delete_from;
912 from_node = rb_first(&from_modules);
913 to_node = rb_first(&to_modules);
918 from_m = rb_entry(from_node, struct module_info, rb_node);
919 to_m = rb_entry(to_node, struct module_info, rb_node);
921 if (from_m->start != to_m->start ||
922 strcmp(from_m->name, to_m->name))
925 from_node = rb_next(from_node);
926 to_node = rb_next(to_node);
929 if (!from_node && !to_node)
932 delete_modules(&to_modules);
934 delete_modules(&from_modules);
939 static int do_validate_kcore_modules(const char *filename, struct map *map,
940 struct map_groups *kmaps)
942 struct rb_root modules = RB_ROOT;
946 err = read_proc_modules(filename, &modules);
950 old_map = map_groups__first(kmaps, map->type);
952 struct map *next = map_groups__next(old_map);
953 struct module_info *mi;
955 if (old_map == map || old_map->start == map->start) {
961 /* Module must be in memory at the same address */
962 mi = find_module(old_map->dso->short_name, &modules);
963 if (!mi || mi->start != old_map->start) {
971 delete_modules(&modules);
976 * If kallsyms is referenced by name then we look for filename in the same
979 static bool filename_from_kallsyms_filename(char *filename,
980 const char *base_name,
981 const char *kallsyms_filename)
985 strcpy(filename, kallsyms_filename);
986 name = strrchr(filename, '/');
992 if (!strcmp(name, "kallsyms")) {
993 strcpy(name, base_name);
1000 static int validate_kcore_modules(const char *kallsyms_filename,
1003 struct map_groups *kmaps = map__kmap(map)->kmaps;
1004 char modules_filename[PATH_MAX];
1006 if (!filename_from_kallsyms_filename(modules_filename, "modules",
1010 if (do_validate_kcore_modules(modules_filename, map, kmaps))
1016 struct kcore_mapfn_data {
1019 struct list_head maps;
1022 static int kcore_mapfn(u64 start, u64 len, u64 pgoff, void *data)
1024 struct kcore_mapfn_data *md = data;
1027 map = map__new2(start, md->dso, md->type);
1031 map->end = map->start + len;
1034 list_add(&map->node, &md->maps);
1039 static int dso__load_kcore(struct dso *dso, struct map *map,
1040 const char *kallsyms_filename)
1042 struct map_groups *kmaps = map__kmap(map)->kmaps;
1043 struct machine *machine = kmaps->machine;
1044 struct kcore_mapfn_data md;
1045 struct map *old_map, *new_map, *replacement_map = NULL;
1048 char kcore_filename[PATH_MAX];
1051 /* This function requires that the map is the kernel map */
1052 if (map != machine->vmlinux_maps[map->type])
1055 if (!filename_from_kallsyms_filename(kcore_filename, "kcore",
1059 /* All modules must be present at their original addresses */
1060 if (validate_kcore_modules(kallsyms_filename, map))
1064 md.type = map->type;
1065 INIT_LIST_HEAD(&md.maps);
1067 fd = open(kcore_filename, O_RDONLY);
1071 /* Read new maps into temporary lists */
1072 err = file__read_maps(fd, md.type == MAP__FUNCTION, kcore_mapfn, &md,
1077 if (list_empty(&md.maps)) {
1082 /* Remove old maps */
1083 old_map = map_groups__first(kmaps, map->type);
1085 struct map *next = map_groups__next(old_map);
1088 map_groups__remove(kmaps, old_map);
1092 /* Find the kernel map using the first symbol */
1093 sym = dso__first_symbol(dso, map->type);
1094 list_for_each_entry(new_map, &md.maps, node) {
1095 if (sym && sym->start >= new_map->start &&
1096 sym->start < new_map->end) {
1097 replacement_map = new_map;
1102 if (!replacement_map)
1103 replacement_map = list_entry(md.maps.next, struct map, node);
1106 while (!list_empty(&md.maps)) {
1107 new_map = list_entry(md.maps.next, struct map, node);
1108 list_del(&new_map->node);
1109 if (new_map == replacement_map) {
1110 map->start = new_map->start;
1111 map->end = new_map->end;
1112 map->pgoff = new_map->pgoff;
1113 map->map_ip = new_map->map_ip;
1114 map->unmap_ip = new_map->unmap_ip;
1115 map__delete(new_map);
1116 /* Ensure maps are correctly ordered */
1117 map_groups__remove(kmaps, map);
1118 map_groups__insert(kmaps, map);
1120 map_groups__insert(kmaps, new_map);
1125 * Set the data type and long name so that kcore can be read via
1126 * dso__data_read_addr().
1128 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1129 dso->data_type = DSO_BINARY_TYPE__GUEST_KCORE;
1131 dso->data_type = DSO_BINARY_TYPE__KCORE;
1132 dso__set_long_name(dso, strdup(kcore_filename));
1136 if (map->type == MAP__FUNCTION)
1137 pr_debug("Using %s for kernel object code\n", kcore_filename);
1139 pr_debug("Using %s for kernel data\n", kcore_filename);
1144 while (!list_empty(&md.maps)) {
1145 map = list_entry(md.maps.next, struct map, node);
1146 list_del(&map->node);
1153 int dso__load_kallsyms(struct dso *dso, const char *filename,
1154 struct map *map, symbol_filter_t filter)
1156 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
1159 if (dso__load_all_kallsyms(dso, filename, map) < 0)
1162 symbols__fixup_duplicate(&dso->symbols[map->type]);
1163 symbols__fixup_end(&dso->symbols[map->type]);
1165 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1166 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
1168 dso->symtab_type = DSO_BINARY_TYPE__KALLSYMS;
1170 if (!dso__load_kcore(dso, map, filename))
1171 return dso__split_kallsyms_for_kcore(dso, map, filter);
1173 return dso__split_kallsyms(dso, map, filter);
1176 static int dso__load_perf_map(struct dso *dso, struct map *map,
1177 symbol_filter_t filter)
1184 file = fopen(dso->long_name, "r");
1188 while (!feof(file)) {
1193 line_len = getline(&line, &n, file);
1200 line[--line_len] = '\0'; /* \n */
1202 len = hex2u64(line, &start);
1205 if (len + 2 >= line_len)
1208 len += hex2u64(line + len, &size);
1211 if (len + 2 >= line_len)
1214 sym = symbol__new(start, size, STB_GLOBAL, line + len);
1217 goto out_delete_line;
1219 if (filter && filter(map, sym))
1220 symbol__delete(sym);
1222 symbols__insert(&dso->symbols[map->type], sym);
1238 int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter)
1243 struct machine *machine;
1244 char *root_dir = (char *) "";
1246 struct symsrc ss_[2];
1247 struct symsrc *syms_ss = NULL, *runtime_ss = NULL;
1249 dso__set_loaded(dso, map->type);
1251 if (dso->kernel == DSO_TYPE_KERNEL)
1252 return dso__load_kernel_sym(dso, map, filter);
1253 else if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1254 return dso__load_guest_kernel_sym(dso, map, filter);
1256 if (map->groups && map->groups->machine)
1257 machine = map->groups->machine;
1261 dso->adjust_symbols = 0;
1263 if (strncmp(dso->name, "/tmp/perf-", 10) == 0) {
1266 if (lstat(dso->name, &st) < 0)
1269 if (st.st_uid && (st.st_uid != geteuid())) {
1270 pr_warning("File %s not owned by current user or root, "
1271 "ignoring it.\n", dso->name);
1275 ret = dso__load_perf_map(dso, map, filter);
1276 dso->symtab_type = ret > 0 ? DSO_BINARY_TYPE__JAVA_JIT :
1277 DSO_BINARY_TYPE__NOT_FOUND;
1282 root_dir = machine->root_dir;
1284 name = malloc(PATH_MAX);
1288 /* Iterate over candidate debug images.
1289 * Keep track of "interesting" ones (those which have a symtab, dynsym,
1290 * and/or opd section) for processing.
1292 for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) {
1293 struct symsrc *ss = &ss_[ss_pos];
1294 bool next_slot = false;
1296 enum dso_binary_type symtab_type = binary_type_symtab[i];
1298 if (dso__binary_type_file(dso, symtab_type,
1299 root_dir, name, PATH_MAX))
1302 /* Name is now the name of the next image to try */
1303 if (symsrc__init(ss, dso, name, symtab_type) < 0)
1306 if (!syms_ss && symsrc__has_symtab(ss)) {
1311 if (!runtime_ss && symsrc__possibly_runtime(ss)) {
1319 if (syms_ss && runtime_ss)
1325 if (!runtime_ss && !syms_ss)
1328 if (runtime_ss && !syms_ss) {
1329 syms_ss = runtime_ss;
1332 /* We'll have to hope for the best */
1333 if (!runtime_ss && syms_ss)
1334 runtime_ss = syms_ss;
1339 km = dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
1340 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE;
1341 ret = dso__load_sym(dso, map, syms_ss, runtime_ss, filter, km);
1349 nr_plt = dso__synthesize_plt_symbols(dso, runtime_ss, map, filter);
1354 for (; ss_pos > 0; ss_pos--)
1355 symsrc__destroy(&ss_[ss_pos - 1]);
1358 if (ret < 0 && strstr(dso->name, " (deleted)") != NULL)
1363 struct map *map_groups__find_by_name(struct map_groups *mg,
1364 enum map_type type, const char *name)
1368 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
1369 struct map *map = rb_entry(nd, struct map, rb_node);
1371 if (map->dso && strcmp(map->dso->short_name, name) == 0)
1378 int dso__load_vmlinux(struct dso *dso, struct map *map,
1379 const char *vmlinux, symbol_filter_t filter)
1383 char symfs_vmlinux[PATH_MAX];
1384 enum dso_binary_type symtab_type;
1386 if (vmlinux[0] == '/')
1387 snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s", vmlinux);
1389 snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s%s",
1390 symbol_conf.symfs, vmlinux);
1392 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1393 symtab_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
1395 symtab_type = DSO_BINARY_TYPE__VMLINUX;
1397 if (symsrc__init(&ss, dso, symfs_vmlinux, symtab_type))
1400 err = dso__load_sym(dso, map, &ss, &ss, filter, 0);
1401 symsrc__destroy(&ss);
1404 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1405 dso->data_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
1407 dso->data_type = DSO_BINARY_TYPE__VMLINUX;
1408 dso__set_long_name(dso, (char *)vmlinux);
1409 dso__set_loaded(dso, map->type);
1410 pr_debug("Using %s for symbols\n", symfs_vmlinux);
1416 int dso__load_vmlinux_path(struct dso *dso, struct map *map,
1417 symbol_filter_t filter)
1422 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1423 vmlinux_path__nr_entries + 1);
1425 filename = dso__build_id_filename(dso, NULL, 0);
1426 if (filename != NULL) {
1427 err = dso__load_vmlinux(dso, map, filename, filter);
1429 dso->lname_alloc = 1;
1435 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
1436 err = dso__load_vmlinux(dso, map, vmlinux_path[i], filter);
1438 dso__set_long_name(dso, strdup(vmlinux_path[i]));
1439 dso->lname_alloc = 1;
1447 static int find_matching_kcore(struct map *map, char *dir, size_t dir_sz)
1449 char kallsyms_filename[PATH_MAX];
1450 struct dirent *dent;
1462 if (dent->d_type != DT_DIR)
1464 scnprintf(kallsyms_filename, sizeof(kallsyms_filename),
1465 "%s/%s/kallsyms", dir, dent->d_name);
1466 if (!validate_kcore_modules(kallsyms_filename, map)) {
1467 strlcpy(dir, kallsyms_filename, dir_sz);
1478 static char *dso__find_kallsyms(struct dso *dso, struct map *map)
1480 u8 host_build_id[BUILD_ID_SIZE];
1481 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1482 bool is_host = false;
1483 char path[PATH_MAX];
1485 if (!dso->has_build_id) {
1487 * Last resort, if we don't have a build-id and couldn't find
1488 * any vmlinux file, try the running kernel kallsyms table.
1493 if (sysfs__read_build_id("/sys/kernel/notes", host_build_id,
1494 sizeof(host_build_id)) == 0)
1495 is_host = dso__build_id_equal(dso, host_build_id);
1497 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1499 /* Use /proc/kallsyms if possible */
1504 /* If no cached kcore go with /proc/kallsyms */
1505 scnprintf(path, sizeof(path), "%s/[kernel.kcore]/%s",
1506 buildid_dir, sbuild_id);
1513 * Do not check the build-id cache, until we know we cannot use
1516 fd = open("/proc/kcore", O_RDONLY);
1519 /* If module maps match go with /proc/kallsyms */
1520 if (!validate_kcore_modules("/proc/kallsyms", map))
1524 /* Find kallsyms in build-id cache with kcore */
1525 if (!find_matching_kcore(map, path, sizeof(path)))
1526 return strdup(path);
1531 scnprintf(path, sizeof(path), "%s/[kernel.kallsyms]/%s",
1532 buildid_dir, sbuild_id);
1534 if (access(path, F_OK)) {
1535 pr_err("No kallsyms or vmlinux with build-id %s was found\n",
1540 return strdup(path);
1543 return strdup("/proc/kallsyms");
1546 static int dso__load_kernel_sym(struct dso *dso, struct map *map,
1547 symbol_filter_t filter)
1550 const char *kallsyms_filename = NULL;
1551 char *kallsyms_allocated_filename = NULL;
1553 * Step 1: if the user specified a kallsyms or vmlinux filename, use
1554 * it and only it, reporting errors to the user if it cannot be used.
1556 * For instance, try to analyse an ARM perf.data file _without_ a
1557 * build-id, or if the user specifies the wrong path to the right
1558 * vmlinux file, obviously we can't fallback to another vmlinux (a
1559 * x86_86 one, on the machine where analysis is being performed, say),
1560 * or worse, /proc/kallsyms.
1562 * If the specified file _has_ a build-id and there is a build-id
1563 * section in the perf.data file, we will still do the expected
1564 * validation in dso__load_vmlinux and will bail out if they don't
1567 if (symbol_conf.kallsyms_name != NULL) {
1568 kallsyms_filename = symbol_conf.kallsyms_name;
1572 if (!symbol_conf.ignore_vmlinux && symbol_conf.vmlinux_name != NULL) {
1573 err = dso__load_vmlinux(dso, map,
1574 symbol_conf.vmlinux_name, filter);
1576 dso__set_long_name(dso,
1577 strdup(symbol_conf.vmlinux_name));
1578 dso->lname_alloc = 1;
1584 if (!symbol_conf.ignore_vmlinux && vmlinux_path != NULL) {
1585 err = dso__load_vmlinux_path(dso, map, filter);
1590 /* do not try local files if a symfs was given */
1591 if (symbol_conf.symfs[0] != 0)
1594 kallsyms_allocated_filename = dso__find_kallsyms(dso, map);
1595 if (!kallsyms_allocated_filename)
1598 kallsyms_filename = kallsyms_allocated_filename;
1601 err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
1603 pr_debug("Using %s for symbols\n", kallsyms_filename);
1604 free(kallsyms_allocated_filename);
1606 if (err > 0 && !dso__is_kcore(dso)) {
1607 dso__set_long_name(dso, strdup("[kernel.kallsyms]"));
1608 map__fixup_start(map);
1609 map__fixup_end(map);
1615 static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
1616 symbol_filter_t filter)
1619 const char *kallsyms_filename = NULL;
1620 struct machine *machine;
1621 char path[PATH_MAX];
1624 pr_debug("Guest kernel map hasn't the point to groups\n");
1627 machine = map->groups->machine;
1629 if (machine__is_default_guest(machine)) {
1631 * if the user specified a vmlinux filename, use it and only
1632 * it, reporting errors to the user if it cannot be used.
1633 * Or use file guest_kallsyms inputted by user on commandline
1635 if (symbol_conf.default_guest_vmlinux_name != NULL) {
1636 err = dso__load_vmlinux(dso, map,
1637 symbol_conf.default_guest_vmlinux_name, filter);
1641 kallsyms_filename = symbol_conf.default_guest_kallsyms;
1642 if (!kallsyms_filename)
1645 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
1646 kallsyms_filename = path;
1649 err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
1651 pr_debug("Using %s for symbols\n", kallsyms_filename);
1652 if (err > 0 && !dso__is_kcore(dso)) {
1653 machine__mmap_name(machine, path, sizeof(path));
1654 dso__set_long_name(dso, strdup(path));
1655 map__fixup_start(map);
1656 map__fixup_end(map);
1662 static void vmlinux_path__exit(void)
1664 while (--vmlinux_path__nr_entries >= 0) {
1665 free(vmlinux_path[vmlinux_path__nr_entries]);
1666 vmlinux_path[vmlinux_path__nr_entries] = NULL;
1670 vmlinux_path = NULL;
1673 static int vmlinux_path__init(void)
1678 vmlinux_path = malloc(sizeof(char *) * 5);
1679 if (vmlinux_path == NULL)
1682 vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
1683 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1685 ++vmlinux_path__nr_entries;
1686 vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
1687 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1689 ++vmlinux_path__nr_entries;
1691 /* only try running kernel version if no symfs was given */
1692 if (symbol_conf.symfs[0] != 0)
1695 if (uname(&uts) < 0)
1698 snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
1699 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1700 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1702 ++vmlinux_path__nr_entries;
1703 snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
1704 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1705 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1707 ++vmlinux_path__nr_entries;
1708 snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
1710 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1711 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1713 ++vmlinux_path__nr_entries;
1718 vmlinux_path__exit();
1722 static int setup_list(struct strlist **list, const char *list_str,
1723 const char *list_name)
1725 if (list_str == NULL)
1728 *list = strlist__new(true, list_str);
1730 pr_err("problems parsing %s list\n", list_name);
1736 static bool symbol__read_kptr_restrict(void)
1740 if (geteuid() != 0) {
1741 FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
1745 if (fgets(line, sizeof(line), fp) != NULL)
1746 value = atoi(line) != 0;
1755 int symbol__init(void)
1759 if (symbol_conf.initialized)
1762 symbol_conf.priv_size = PERF_ALIGN(symbol_conf.priv_size, sizeof(u64));
1766 if (symbol_conf.sort_by_name)
1767 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
1768 sizeof(struct symbol));
1770 if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
1773 if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
1774 pr_err("'.' is the only non valid --field-separator argument\n");
1778 if (setup_list(&symbol_conf.dso_list,
1779 symbol_conf.dso_list_str, "dso") < 0)
1782 if (setup_list(&symbol_conf.comm_list,
1783 symbol_conf.comm_list_str, "comm") < 0)
1784 goto out_free_dso_list;
1786 if (setup_list(&symbol_conf.sym_list,
1787 symbol_conf.sym_list_str, "symbol") < 0)
1788 goto out_free_comm_list;
1791 * A path to symbols of "/" is identical to ""
1792 * reset here for simplicity.
1794 symfs = realpath(symbol_conf.symfs, NULL);
1796 symfs = symbol_conf.symfs;
1797 if (strcmp(symfs, "/") == 0)
1798 symbol_conf.symfs = "";
1799 if (symfs != symbol_conf.symfs)
1800 free((void *)symfs);
1802 symbol_conf.kptr_restrict = symbol__read_kptr_restrict();
1804 symbol_conf.initialized = true;
1808 strlist__delete(symbol_conf.comm_list);
1810 strlist__delete(symbol_conf.dso_list);
1814 void symbol__exit(void)
1816 if (!symbol_conf.initialized)
1818 strlist__delete(symbol_conf.sym_list);
1819 strlist__delete(symbol_conf.dso_list);
1820 strlist__delete(symbol_conf.comm_list);
1821 vmlinux_path__exit();
1822 symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
1823 symbol_conf.initialized = false;