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);
165 nd = rb_next(&curr->rb_node);
166 rb_erase(&curr->rb_node, symbols);
171 void symbols__fixup_end(struct rb_root *symbols)
173 struct rb_node *nd, *prevnd = rb_first(symbols);
174 struct symbol *curr, *prev;
179 curr = rb_entry(prevnd, struct symbol, rb_node);
181 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
183 curr = rb_entry(nd, struct symbol, rb_node);
185 if (prev->end == prev->start && prev->end != curr->start)
186 prev->end = curr->start - 1;
190 if (curr->end == curr->start)
191 curr->end = roundup(curr->start, 4096);
194 void __map_groups__fixup_end(struct map_groups *mg, enum map_type type)
196 struct map *prev, *curr;
197 struct rb_node *nd, *prevnd = rb_first(&mg->maps[type]);
202 curr = rb_entry(prevnd, struct map, rb_node);
204 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
206 curr = rb_entry(nd, struct map, rb_node);
207 prev->end = curr->start - 1;
211 * We still haven't the actual symbols, so guess the
212 * last map final address.
217 struct symbol *symbol__new(u64 start, u64 len, u8 binding, const char *name)
219 size_t namelen = strlen(name) + 1;
220 struct symbol *sym = calloc(1, (symbol_conf.priv_size +
221 sizeof(*sym) + namelen));
225 if (symbol_conf.priv_size)
226 sym = ((void *)sym) + symbol_conf.priv_size;
229 sym->end = len ? start + len - 1 : start;
230 sym->binding = binding;
231 sym->namelen = namelen - 1;
233 pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n",
234 __func__, name, start, sym->end);
235 memcpy(sym->name, name, namelen);
240 void symbol__delete(struct symbol *sym)
242 free(((void *)sym) - symbol_conf.priv_size);
245 size_t symbol__fprintf(struct symbol *sym, FILE *fp)
247 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %c %s\n",
248 sym->start, sym->end,
249 sym->binding == STB_GLOBAL ? 'g' :
250 sym->binding == STB_LOCAL ? 'l' : 'w',
254 size_t symbol__fprintf_symname_offs(const struct symbol *sym,
255 const struct addr_location *al, FILE *fp)
257 unsigned long offset;
260 if (sym && sym->name) {
261 length = fprintf(fp, "%s", sym->name);
263 if (al->addr < sym->end)
264 offset = al->addr - sym->start;
266 offset = al->addr - al->map->start - sym->start;
267 length += fprintf(fp, "+0x%lx", offset);
271 return fprintf(fp, "[unknown]");
274 size_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp)
276 return symbol__fprintf_symname_offs(sym, NULL, fp);
279 void symbols__delete(struct rb_root *symbols)
282 struct rb_node *next = rb_first(symbols);
285 pos = rb_entry(next, struct symbol, rb_node);
286 next = rb_next(&pos->rb_node);
287 rb_erase(&pos->rb_node, symbols);
292 void symbols__insert(struct rb_root *symbols, struct symbol *sym)
294 struct rb_node **p = &symbols->rb_node;
295 struct rb_node *parent = NULL;
296 const u64 ip = sym->start;
301 s = rb_entry(parent, struct symbol, rb_node);
307 rb_link_node(&sym->rb_node, parent, p);
308 rb_insert_color(&sym->rb_node, symbols);
311 static struct symbol *symbols__find(struct rb_root *symbols, u64 ip)
318 n = symbols->rb_node;
321 struct symbol *s = rb_entry(n, struct symbol, rb_node);
325 else if (ip > s->end)
334 static struct symbol *symbols__first(struct rb_root *symbols)
336 struct rb_node *n = rb_first(symbols);
339 return rb_entry(n, struct symbol, rb_node);
344 struct symbol_name_rb_node {
345 struct rb_node rb_node;
349 static void symbols__insert_by_name(struct rb_root *symbols, struct symbol *sym)
351 struct rb_node **p = &symbols->rb_node;
352 struct rb_node *parent = NULL;
353 struct symbol_name_rb_node *symn, *s;
355 symn = container_of(sym, struct symbol_name_rb_node, sym);
359 s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
360 if (strcmp(sym->name, s->sym.name) < 0)
365 rb_link_node(&symn->rb_node, parent, p);
366 rb_insert_color(&symn->rb_node, symbols);
369 static void symbols__sort_by_name(struct rb_root *symbols,
370 struct rb_root *source)
374 for (nd = rb_first(source); nd; nd = rb_next(nd)) {
375 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
376 symbols__insert_by_name(symbols, pos);
380 static struct symbol *symbols__find_by_name(struct rb_root *symbols,
388 n = symbols->rb_node;
391 struct symbol_name_rb_node *s;
394 s = rb_entry(n, struct symbol_name_rb_node, rb_node);
395 cmp = strcmp(name, s->sym.name);
408 struct symbol *dso__find_symbol(struct dso *dso,
409 enum map_type type, u64 addr)
411 return symbols__find(&dso->symbols[type], addr);
414 struct symbol *dso__first_symbol(struct dso *dso, enum map_type type)
416 return symbols__first(&dso->symbols[type]);
419 struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
422 return symbols__find_by_name(&dso->symbol_names[type], name);
425 void dso__sort_by_name(struct dso *dso, enum map_type type)
427 dso__set_sorted_by_name(dso, type);
428 return symbols__sort_by_name(&dso->symbol_names[type],
429 &dso->symbols[type]);
432 size_t dso__fprintf_symbols_by_name(struct dso *dso,
433 enum map_type type, FILE *fp)
437 struct symbol_name_rb_node *pos;
439 for (nd = rb_first(&dso->symbol_names[type]); nd; nd = rb_next(nd)) {
440 pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
441 fprintf(fp, "%s\n", pos->sym.name);
447 int kallsyms__parse(const char *filename, void *arg,
448 int (*process_symbol)(void *arg, const char *name,
449 char type, u64 start))
454 FILE *file = fopen(filename, "r");
461 while (!feof(file)) {
467 line_len = getline(&line, &n, file);
468 if (line_len < 0 || !line)
471 line[--line_len] = '\0'; /* \n */
473 len = hex2u64(line, &start);
476 if (len + 2 >= line_len)
479 symbol_type = line[len];
481 symbol_name = line + len;
482 len = line_len - len;
484 if (len >= KSYM_NAME_LEN) {
489 err = process_symbol(arg, symbol_name,
503 int modules__parse(const char *filename, void *arg,
504 int (*process_module)(void *arg, const char *name,
512 file = fopen(filename, "r");
522 line_len = getline(&line, &n, file);
535 line[--line_len] = '\0'; /* \n */
537 sep = strrchr(line, 'x');
541 hex2u64(sep + 1, &start);
543 sep = strchr(line, ' ');
549 scnprintf(name, sizeof(name), "[%s]", line);
551 err = process_module(arg, name, start);
561 struct process_kallsyms_args {
566 static u8 kallsyms2elf_type(char type)
571 return isupper(type) ? STB_GLOBAL : STB_LOCAL;
574 static int map__process_kallsym_symbol(void *arg, const char *name,
575 char type, u64 start)
578 struct process_kallsyms_args *a = arg;
579 struct rb_root *root = &a->dso->symbols[a->map->type];
581 if (!symbol_type__is_a(type, a->map->type))
585 * module symbols are not sorted so we add all
586 * symbols, setting length to 0, and rely on
587 * symbols__fixup_end() to fix it up.
589 sym = symbol__new(start, 0, kallsyms2elf_type(type), name);
593 * We will pass the symbols to the filter later, in
594 * map__split_kallsyms, when we have split the maps per module
596 symbols__insert(root, sym);
602 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
603 * so that we can in the next step set the symbol ->end address and then
604 * call kernel_maps__split_kallsyms.
606 static int dso__load_all_kallsyms(struct dso *dso, const char *filename,
609 struct process_kallsyms_args args = { .map = map, .dso = dso, };
610 return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
613 static int dso__split_kallsyms_for_kcore(struct dso *dso, struct map *map,
614 symbol_filter_t filter)
616 struct map_groups *kmaps = map__kmap(map)->kmaps;
617 struct map *curr_map;
619 int count = 0, moved = 0;
620 struct rb_root *root = &dso->symbols[map->type];
621 struct rb_node *next = rb_first(root);
626 pos = rb_entry(next, struct symbol, rb_node);
627 next = rb_next(&pos->rb_node);
629 module = strchr(pos->name, '\t');
633 curr_map = map_groups__find(kmaps, map->type, pos->start);
635 if (!curr_map || (filter && filter(curr_map, pos))) {
636 rb_erase(&pos->rb_node, root);
639 pos->start -= curr_map->start - curr_map->pgoff;
641 pos->end -= curr_map->start - curr_map->pgoff;
642 if (curr_map != map) {
643 rb_erase(&pos->rb_node, root);
645 &curr_map->dso->symbols[curr_map->type],
654 /* Symbols have been adjusted */
655 dso->adjust_symbols = 1;
657 return count + moved;
661 * Split the symbols into maps, making sure there are no overlaps, i.e. the
662 * kernel range is broken in several maps, named [kernel].N, as we don't have
663 * the original ELF section names vmlinux have.
665 static int dso__split_kallsyms(struct dso *dso, struct map *map,
666 symbol_filter_t filter)
668 struct map_groups *kmaps = map__kmap(map)->kmaps;
669 struct machine *machine = kmaps->machine;
670 struct map *curr_map = map;
672 int count = 0, moved = 0;
673 struct rb_root *root = &dso->symbols[map->type];
674 struct rb_node *next = rb_first(root);
675 int kernel_range = 0;
680 pos = rb_entry(next, struct symbol, rb_node);
681 next = rb_next(&pos->rb_node);
683 module = strchr(pos->name, '\t');
685 if (!symbol_conf.use_modules)
690 if (strcmp(curr_map->dso->short_name, module)) {
691 if (curr_map != map &&
692 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
693 machine__is_default_guest(machine)) {
695 * We assume all symbols of a module are
696 * continuous in * kallsyms, so curr_map
697 * points to a module and all its
698 * symbols are in its kmap. Mark it as
701 dso__set_loaded(curr_map->dso,
705 curr_map = map_groups__find_by_name(kmaps,
707 if (curr_map == NULL) {
708 pr_debug("%s/proc/{kallsyms,modules} "
709 "inconsistency while looking "
710 "for \"%s\" module!\n",
711 machine->root_dir, module);
716 if (curr_map->dso->loaded &&
717 !machine__is_default_guest(machine))
721 * So that we look just like we get from .ko files,
722 * i.e. not prelinked, relative to map->start.
724 pos->start = curr_map->map_ip(curr_map, pos->start);
725 pos->end = curr_map->map_ip(curr_map, pos->end);
726 } else if (curr_map != map) {
727 char dso_name[PATH_MAX];
735 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
736 snprintf(dso_name, sizeof(dso_name),
740 snprintf(dso_name, sizeof(dso_name),
744 ndso = dso__new(dso_name);
748 ndso->kernel = dso->kernel;
750 curr_map = map__new2(pos->start, ndso, map->type);
751 if (curr_map == NULL) {
756 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
757 map_groups__insert(kmaps, curr_map);
761 if (filter && filter(curr_map, pos)) {
762 discard_symbol: rb_erase(&pos->rb_node, root);
765 if (curr_map != map) {
766 rb_erase(&pos->rb_node, root);
767 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
774 if (curr_map != map &&
775 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
776 machine__is_default_guest(kmaps->machine)) {
777 dso__set_loaded(curr_map->dso, curr_map->type);
780 return count + moved;
783 bool symbol__restricted_filename(const char *filename,
784 const char *restricted_filename)
786 bool restricted = false;
788 if (symbol_conf.kptr_restrict) {
789 char *r = realpath(filename, NULL);
792 restricted = strcmp(r, restricted_filename) == 0;
802 struct rb_node rb_node;
807 static void add_module(struct module_info *mi, struct rb_root *modules)
809 struct rb_node **p = &modules->rb_node;
810 struct rb_node *parent = NULL;
811 struct module_info *m;
815 m = rb_entry(parent, struct module_info, rb_node);
816 if (strcmp(mi->name, m->name) < 0)
821 rb_link_node(&mi->rb_node, parent, p);
822 rb_insert_color(&mi->rb_node, modules);
825 static void delete_modules(struct rb_root *modules)
827 struct module_info *mi;
828 struct rb_node *next = rb_first(modules);
831 mi = rb_entry(next, struct module_info, rb_node);
832 next = rb_next(&mi->rb_node);
833 rb_erase(&mi->rb_node, modules);
839 static struct module_info *find_module(const char *name,
840 struct rb_root *modules)
842 struct rb_node *n = modules->rb_node;
845 struct module_info *m;
848 m = rb_entry(n, struct module_info, rb_node);
849 cmp = strcmp(name, m->name);
861 static int __read_proc_modules(void *arg, const char *name, u64 start)
863 struct rb_root *modules = arg;
864 struct module_info *mi;
866 mi = zalloc(sizeof(struct module_info));
870 mi->name = strdup(name);
878 add_module(mi, modules);
883 static int read_proc_modules(const char *filename, struct rb_root *modules)
885 if (symbol__restricted_filename(filename, "/proc/modules"))
888 if (modules__parse(filename, modules, __read_proc_modules)) {
889 delete_modules(modules);
896 int compare_proc_modules(const char *from, const char *to)
898 struct rb_root from_modules = RB_ROOT;
899 struct rb_root to_modules = RB_ROOT;
900 struct rb_node *from_node, *to_node;
901 struct module_info *from_m, *to_m;
904 if (read_proc_modules(from, &from_modules))
907 if (read_proc_modules(to, &to_modules))
908 goto out_delete_from;
910 from_node = rb_first(&from_modules);
911 to_node = rb_first(&to_modules);
916 from_m = rb_entry(from_node, struct module_info, rb_node);
917 to_m = rb_entry(to_node, struct module_info, rb_node);
919 if (from_m->start != to_m->start ||
920 strcmp(from_m->name, to_m->name))
923 from_node = rb_next(from_node);
924 to_node = rb_next(to_node);
927 if (!from_node && !to_node)
930 delete_modules(&to_modules);
932 delete_modules(&from_modules);
937 static int do_validate_kcore_modules(const char *filename, struct map *map,
938 struct map_groups *kmaps)
940 struct rb_root modules = RB_ROOT;
944 err = read_proc_modules(filename, &modules);
948 old_map = map_groups__first(kmaps, map->type);
950 struct map *next = map_groups__next(old_map);
951 struct module_info *mi;
953 if (old_map == map || old_map->start == map->start) {
959 /* Module must be in memory at the same address */
960 mi = find_module(old_map->dso->short_name, &modules);
961 if (!mi || mi->start != old_map->start) {
969 delete_modules(&modules);
974 * If kallsyms is referenced by name then we look for filename in the same
977 static bool filename_from_kallsyms_filename(char *filename,
978 const char *base_name,
979 const char *kallsyms_filename)
983 strcpy(filename, kallsyms_filename);
984 name = strrchr(filename, '/');
990 if (!strcmp(name, "kallsyms")) {
991 strcpy(name, base_name);
998 static int validate_kcore_modules(const char *kallsyms_filename,
1001 struct map_groups *kmaps = map__kmap(map)->kmaps;
1002 char modules_filename[PATH_MAX];
1004 if (!filename_from_kallsyms_filename(modules_filename, "modules",
1008 if (do_validate_kcore_modules(modules_filename, map, kmaps))
1014 struct kcore_mapfn_data {
1017 struct list_head maps;
1020 static int kcore_mapfn(u64 start, u64 len, u64 pgoff, void *data)
1022 struct kcore_mapfn_data *md = data;
1025 map = map__new2(start, md->dso, md->type);
1029 map->end = map->start + len;
1032 list_add(&map->node, &md->maps);
1037 static int dso__load_kcore(struct dso *dso, struct map *map,
1038 const char *kallsyms_filename)
1040 struct map_groups *kmaps = map__kmap(map)->kmaps;
1041 struct machine *machine = kmaps->machine;
1042 struct kcore_mapfn_data md;
1043 struct map *old_map, *new_map, *replacement_map = NULL;
1046 char kcore_filename[PATH_MAX];
1049 /* This function requires that the map is the kernel map */
1050 if (map != machine->vmlinux_maps[map->type])
1053 if (!filename_from_kallsyms_filename(kcore_filename, "kcore",
1057 /* All modules must be present at their original addresses */
1058 if (validate_kcore_modules(kallsyms_filename, map))
1062 md.type = map->type;
1063 INIT_LIST_HEAD(&md.maps);
1065 fd = open(kcore_filename, O_RDONLY);
1069 /* Read new maps into temporary lists */
1070 err = file__read_maps(fd, md.type == MAP__FUNCTION, kcore_mapfn, &md,
1075 if (list_empty(&md.maps)) {
1080 /* Remove old maps */
1081 old_map = map_groups__first(kmaps, map->type);
1083 struct map *next = map_groups__next(old_map);
1086 map_groups__remove(kmaps, old_map);
1090 /* Find the kernel map using the first symbol */
1091 sym = dso__first_symbol(dso, map->type);
1092 list_for_each_entry(new_map, &md.maps, node) {
1093 if (sym && sym->start >= new_map->start &&
1094 sym->start < new_map->end) {
1095 replacement_map = new_map;
1100 if (!replacement_map)
1101 replacement_map = list_entry(md.maps.next, struct map, node);
1104 while (!list_empty(&md.maps)) {
1105 new_map = list_entry(md.maps.next, struct map, node);
1106 list_del(&new_map->node);
1107 if (new_map == replacement_map) {
1108 map->start = new_map->start;
1109 map->end = new_map->end;
1110 map->pgoff = new_map->pgoff;
1111 map->map_ip = new_map->map_ip;
1112 map->unmap_ip = new_map->unmap_ip;
1113 map__delete(new_map);
1114 /* Ensure maps are correctly ordered */
1115 map_groups__remove(kmaps, map);
1116 map_groups__insert(kmaps, map);
1118 map_groups__insert(kmaps, new_map);
1123 * Set the data type and long name so that kcore can be read via
1124 * dso__data_read_addr().
1126 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1127 dso->data_type = DSO_BINARY_TYPE__GUEST_KCORE;
1129 dso->data_type = DSO_BINARY_TYPE__KCORE;
1130 dso__set_long_name(dso, strdup(kcore_filename));
1134 if (map->type == MAP__FUNCTION)
1135 pr_debug("Using %s for kernel object code\n", kcore_filename);
1137 pr_debug("Using %s for kernel data\n", kcore_filename);
1142 while (!list_empty(&md.maps)) {
1143 map = list_entry(md.maps.next, struct map, node);
1144 list_del(&map->node);
1151 int dso__load_kallsyms(struct dso *dso, const char *filename,
1152 struct map *map, symbol_filter_t filter)
1154 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
1157 if (dso__load_all_kallsyms(dso, filename, map) < 0)
1160 symbols__fixup_duplicate(&dso->symbols[map->type]);
1161 symbols__fixup_end(&dso->symbols[map->type]);
1163 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1164 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
1166 dso->symtab_type = DSO_BINARY_TYPE__KALLSYMS;
1168 if (!dso__load_kcore(dso, map, filename))
1169 return dso__split_kallsyms_for_kcore(dso, map, filter);
1171 return dso__split_kallsyms(dso, map, filter);
1174 static int dso__load_perf_map(struct dso *dso, struct map *map,
1175 symbol_filter_t filter)
1182 file = fopen(dso->long_name, "r");
1186 while (!feof(file)) {
1191 line_len = getline(&line, &n, file);
1198 line[--line_len] = '\0'; /* \n */
1200 len = hex2u64(line, &start);
1203 if (len + 2 >= line_len)
1206 len += hex2u64(line + len, &size);
1209 if (len + 2 >= line_len)
1212 sym = symbol__new(start, size, STB_GLOBAL, line + len);
1215 goto out_delete_line;
1217 if (filter && filter(map, sym))
1218 symbol__delete(sym);
1220 symbols__insert(&dso->symbols[map->type], sym);
1236 int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter)
1241 struct machine *machine;
1242 char *root_dir = (char *) "";
1244 struct symsrc ss_[2];
1245 struct symsrc *syms_ss = NULL, *runtime_ss = NULL;
1247 dso__set_loaded(dso, map->type);
1249 if (dso->kernel == DSO_TYPE_KERNEL)
1250 return dso__load_kernel_sym(dso, map, filter);
1251 else if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1252 return dso__load_guest_kernel_sym(dso, map, filter);
1254 if (map->groups && map->groups->machine)
1255 machine = map->groups->machine;
1259 dso->adjust_symbols = 0;
1261 if (strncmp(dso->name, "/tmp/perf-", 10) == 0) {
1264 if (lstat(dso->name, &st) < 0)
1267 if (st.st_uid && (st.st_uid != geteuid())) {
1268 pr_warning("File %s not owned by current user or root, "
1269 "ignoring it.\n", dso->name);
1273 ret = dso__load_perf_map(dso, map, filter);
1274 dso->symtab_type = ret > 0 ? DSO_BINARY_TYPE__JAVA_JIT :
1275 DSO_BINARY_TYPE__NOT_FOUND;
1280 root_dir = machine->root_dir;
1282 name = malloc(PATH_MAX);
1286 /* Iterate over candidate debug images.
1287 * Keep track of "interesting" ones (those which have a symtab, dynsym,
1288 * and/or opd section) for processing.
1290 for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) {
1291 struct symsrc *ss = &ss_[ss_pos];
1292 bool next_slot = false;
1294 enum dso_binary_type symtab_type = binary_type_symtab[i];
1296 if (dso__binary_type_file(dso, symtab_type,
1297 root_dir, name, PATH_MAX))
1300 /* Name is now the name of the next image to try */
1301 if (symsrc__init(ss, dso, name, symtab_type) < 0)
1304 if (!syms_ss && symsrc__has_symtab(ss)) {
1309 if (!runtime_ss && symsrc__possibly_runtime(ss)) {
1317 if (syms_ss && runtime_ss)
1323 if (!runtime_ss && !syms_ss)
1326 if (runtime_ss && !syms_ss) {
1327 syms_ss = runtime_ss;
1330 /* We'll have to hope for the best */
1331 if (!runtime_ss && syms_ss)
1332 runtime_ss = syms_ss;
1337 km = dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
1338 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE;
1339 ret = dso__load_sym(dso, map, syms_ss, runtime_ss, filter, km);
1347 nr_plt = dso__synthesize_plt_symbols(dso, runtime_ss, map, filter);
1352 for (; ss_pos > 0; ss_pos--)
1353 symsrc__destroy(&ss_[ss_pos - 1]);
1356 if (ret < 0 && strstr(dso->name, " (deleted)") != NULL)
1361 struct map *map_groups__find_by_name(struct map_groups *mg,
1362 enum map_type type, const char *name)
1366 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
1367 struct map *map = rb_entry(nd, struct map, rb_node);
1369 if (map->dso && strcmp(map->dso->short_name, name) == 0)
1376 int dso__load_vmlinux(struct dso *dso, struct map *map,
1377 const char *vmlinux, symbol_filter_t filter)
1381 char symfs_vmlinux[PATH_MAX];
1382 enum dso_binary_type symtab_type;
1384 if (vmlinux[0] == '/')
1385 snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s", vmlinux);
1387 snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s%s",
1388 symbol_conf.symfs, vmlinux);
1390 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1391 symtab_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
1393 symtab_type = DSO_BINARY_TYPE__VMLINUX;
1395 if (symsrc__init(&ss, dso, symfs_vmlinux, symtab_type))
1398 err = dso__load_sym(dso, map, &ss, &ss, filter, 0);
1399 symsrc__destroy(&ss);
1402 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1403 dso->data_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
1405 dso->data_type = DSO_BINARY_TYPE__VMLINUX;
1406 dso__set_long_name(dso, (char *)vmlinux);
1407 dso__set_loaded(dso, map->type);
1408 pr_debug("Using %s for symbols\n", symfs_vmlinux);
1414 int dso__load_vmlinux_path(struct dso *dso, struct map *map,
1415 symbol_filter_t filter)
1420 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1421 vmlinux_path__nr_entries + 1);
1423 filename = dso__build_id_filename(dso, NULL, 0);
1424 if (filename != NULL) {
1425 err = dso__load_vmlinux(dso, map, filename, filter);
1427 dso->lname_alloc = 1;
1433 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
1434 err = dso__load_vmlinux(dso, map, vmlinux_path[i], filter);
1436 dso__set_long_name(dso, strdup(vmlinux_path[i]));
1437 dso->lname_alloc = 1;
1445 static int find_matching_kcore(struct map *map, char *dir, size_t dir_sz)
1447 char kallsyms_filename[PATH_MAX];
1448 struct dirent *dent;
1460 if (dent->d_type != DT_DIR)
1462 scnprintf(kallsyms_filename, sizeof(kallsyms_filename),
1463 "%s/%s/kallsyms", dir, dent->d_name);
1464 if (!validate_kcore_modules(kallsyms_filename, map)) {
1465 strlcpy(dir, kallsyms_filename, dir_sz);
1476 static char *dso__find_kallsyms(struct dso *dso, struct map *map)
1478 u8 host_build_id[BUILD_ID_SIZE];
1479 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1480 bool is_host = false;
1481 char path[PATH_MAX];
1483 if (!dso->has_build_id) {
1485 * Last resort, if we don't have a build-id and couldn't find
1486 * any vmlinux file, try the running kernel kallsyms table.
1491 if (sysfs__read_build_id("/sys/kernel/notes", host_build_id,
1492 sizeof(host_build_id)) == 0)
1493 is_host = dso__build_id_equal(dso, host_build_id);
1495 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1497 /* Use /proc/kallsyms if possible */
1502 /* If no cached kcore go with /proc/kallsyms */
1503 scnprintf(path, sizeof(path), "%s/[kernel.kcore]/%s",
1504 buildid_dir, sbuild_id);
1511 * Do not check the build-id cache, until we know we cannot use
1514 fd = open("/proc/kcore", O_RDONLY);
1517 /* If module maps match go with /proc/kallsyms */
1518 if (!validate_kcore_modules("/proc/kallsyms", map))
1522 /* Find kallsyms in build-id cache with kcore */
1523 if (!find_matching_kcore(map, path, sizeof(path)))
1524 return strdup(path);
1529 scnprintf(path, sizeof(path), "%s/[kernel.kallsyms]/%s",
1530 buildid_dir, sbuild_id);
1532 if (access(path, F_OK)) {
1533 pr_err("No kallsyms or vmlinux with build-id %s was found\n",
1538 return strdup(path);
1541 return strdup("/proc/kallsyms");
1544 static int dso__load_kernel_sym(struct dso *dso, struct map *map,
1545 symbol_filter_t filter)
1548 const char *kallsyms_filename = NULL;
1549 char *kallsyms_allocated_filename = NULL;
1551 * Step 1: if the user specified a kallsyms or vmlinux filename, use
1552 * it and only it, reporting errors to the user if it cannot be used.
1554 * For instance, try to analyse an ARM perf.data file _without_ a
1555 * build-id, or if the user specifies the wrong path to the right
1556 * vmlinux file, obviously we can't fallback to another vmlinux (a
1557 * x86_86 one, on the machine where analysis is being performed, say),
1558 * or worse, /proc/kallsyms.
1560 * If the specified file _has_ a build-id and there is a build-id
1561 * section in the perf.data file, we will still do the expected
1562 * validation in dso__load_vmlinux and will bail out if they don't
1565 if (symbol_conf.kallsyms_name != NULL) {
1566 kallsyms_filename = symbol_conf.kallsyms_name;
1570 if (!symbol_conf.ignore_vmlinux && symbol_conf.vmlinux_name != NULL) {
1571 err = dso__load_vmlinux(dso, map,
1572 symbol_conf.vmlinux_name, filter);
1574 dso__set_long_name(dso,
1575 strdup(symbol_conf.vmlinux_name));
1576 dso->lname_alloc = 1;
1582 if (!symbol_conf.ignore_vmlinux && vmlinux_path != NULL) {
1583 err = dso__load_vmlinux_path(dso, map, filter);
1588 /* do not try local files if a symfs was given */
1589 if (symbol_conf.symfs[0] != 0)
1592 kallsyms_allocated_filename = dso__find_kallsyms(dso, map);
1593 if (!kallsyms_allocated_filename)
1596 kallsyms_filename = kallsyms_allocated_filename;
1599 err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
1601 pr_debug("Using %s for symbols\n", kallsyms_filename);
1602 free(kallsyms_allocated_filename);
1604 if (err > 0 && !dso__is_kcore(dso)) {
1605 dso__set_long_name(dso, strdup("[kernel.kallsyms]"));
1606 map__fixup_start(map);
1607 map__fixup_end(map);
1613 static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
1614 symbol_filter_t filter)
1617 const char *kallsyms_filename = NULL;
1618 struct machine *machine;
1619 char path[PATH_MAX];
1622 pr_debug("Guest kernel map hasn't the point to groups\n");
1625 machine = map->groups->machine;
1627 if (machine__is_default_guest(machine)) {
1629 * if the user specified a vmlinux filename, use it and only
1630 * it, reporting errors to the user if it cannot be used.
1631 * Or use file guest_kallsyms inputted by user on commandline
1633 if (symbol_conf.default_guest_vmlinux_name != NULL) {
1634 err = dso__load_vmlinux(dso, map,
1635 symbol_conf.default_guest_vmlinux_name, filter);
1639 kallsyms_filename = symbol_conf.default_guest_kallsyms;
1640 if (!kallsyms_filename)
1643 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
1644 kallsyms_filename = path;
1647 err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
1649 pr_debug("Using %s for symbols\n", kallsyms_filename);
1650 if (err > 0 && !dso__is_kcore(dso)) {
1651 machine__mmap_name(machine, path, sizeof(path));
1652 dso__set_long_name(dso, strdup(path));
1653 map__fixup_start(map);
1654 map__fixup_end(map);
1660 static void vmlinux_path__exit(void)
1662 while (--vmlinux_path__nr_entries >= 0) {
1663 free(vmlinux_path[vmlinux_path__nr_entries]);
1664 vmlinux_path[vmlinux_path__nr_entries] = NULL;
1668 vmlinux_path = NULL;
1671 static int vmlinux_path__init(void)
1676 vmlinux_path = malloc(sizeof(char *) * 5);
1677 if (vmlinux_path == NULL)
1680 vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
1681 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1683 ++vmlinux_path__nr_entries;
1684 vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
1685 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1687 ++vmlinux_path__nr_entries;
1689 /* only try running kernel version if no symfs was given */
1690 if (symbol_conf.symfs[0] != 0)
1693 if (uname(&uts) < 0)
1696 snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
1697 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1698 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1700 ++vmlinux_path__nr_entries;
1701 snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
1702 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1703 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1705 ++vmlinux_path__nr_entries;
1706 snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
1708 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1709 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1711 ++vmlinux_path__nr_entries;
1716 vmlinux_path__exit();
1720 static int setup_list(struct strlist **list, const char *list_str,
1721 const char *list_name)
1723 if (list_str == NULL)
1726 *list = strlist__new(true, list_str);
1728 pr_err("problems parsing %s list\n", list_name);
1734 static bool symbol__read_kptr_restrict(void)
1738 if (geteuid() != 0) {
1739 FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
1743 if (fgets(line, sizeof(line), fp) != NULL)
1744 value = atoi(line) != 0;
1753 int symbol__init(void)
1757 if (symbol_conf.initialized)
1760 symbol_conf.priv_size = PERF_ALIGN(symbol_conf.priv_size, sizeof(u64));
1764 if (symbol_conf.sort_by_name)
1765 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
1766 sizeof(struct symbol));
1768 if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
1771 if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
1772 pr_err("'.' is the only non valid --field-separator argument\n");
1776 if (setup_list(&symbol_conf.dso_list,
1777 symbol_conf.dso_list_str, "dso") < 0)
1780 if (setup_list(&symbol_conf.comm_list,
1781 symbol_conf.comm_list_str, "comm") < 0)
1782 goto out_free_dso_list;
1784 if (setup_list(&symbol_conf.sym_list,
1785 symbol_conf.sym_list_str, "symbol") < 0)
1786 goto out_free_comm_list;
1789 * A path to symbols of "/" is identical to ""
1790 * reset here for simplicity.
1792 symfs = realpath(symbol_conf.symfs, NULL);
1794 symfs = symbol_conf.symfs;
1795 if (strcmp(symfs, "/") == 0)
1796 symbol_conf.symfs = "";
1797 if (symfs != symbol_conf.symfs)
1798 free((void *)symfs);
1800 symbol_conf.kptr_restrict = symbol__read_kptr_restrict();
1802 symbol_conf.initialized = true;
1806 strlist__delete(symbol_conf.comm_list);
1808 strlist__delete(symbol_conf.dso_list);
1812 void symbol__exit(void)
1814 if (!symbol_conf.initialized)
1816 strlist__delete(symbol_conf.sym_list);
1817 strlist__delete(symbol_conf.dso_list);
1818 strlist__delete(symbol_conf.comm_list);
1819 vmlinux_path__exit();
1820 symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
1821 symbol_conf.initialized = false;