20 #include <sys/utsname.h>
23 #define KSYM_NAME_LEN 256
26 static int dso__load_kernel_sym(struct dso *dso, struct map *map,
27 symbol_filter_t filter);
28 static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
29 symbol_filter_t filter);
30 static int vmlinux_path__nr_entries;
31 static char **vmlinux_path;
33 struct symbol_conf symbol_conf = {
34 .exclude_other = true,
36 .try_vmlinux_path = true,
41 static enum dso_binary_type binary_type_symtab[] = {
42 DSO_BINARY_TYPE__KALLSYMS,
43 DSO_BINARY_TYPE__GUEST_KALLSYMS,
44 DSO_BINARY_TYPE__JAVA_JIT,
45 DSO_BINARY_TYPE__DEBUGLINK,
46 DSO_BINARY_TYPE__BUILD_ID_CACHE,
47 DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
48 DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
49 DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
50 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
51 DSO_BINARY_TYPE__GUEST_KMODULE,
52 DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE,
53 DSO_BINARY_TYPE__NOT_FOUND,
56 #define DSO_BINARY_TYPE__SYMTAB_CNT ARRAY_SIZE(binary_type_symtab)
58 bool symbol_type__is_a(char symbol_type, enum map_type map_type)
60 symbol_type = toupper(symbol_type);
64 return symbol_type == 'T' || symbol_type == 'W';
66 return symbol_type == 'D';
72 static int prefix_underscores_count(const char *str)
74 const char *tail = str;
85 static int choose_best_symbol(struct symbol *syma, struct symbol *symb)
90 /* Prefer a symbol with non zero length */
91 a = syma->end - syma->start;
92 b = symb->end - symb->start;
93 if ((b == 0) && (a > 0))
95 else if ((a == 0) && (b > 0))
98 /* Prefer a non weak symbol over a weak one */
99 a = syma->binding == STB_WEAK;
100 b = symb->binding == STB_WEAK;
106 /* Prefer a global symbol over a non global one */
107 a = syma->binding == STB_GLOBAL;
108 b = symb->binding == STB_GLOBAL;
114 /* Prefer a symbol with less underscores */
115 a = prefix_underscores_count(syma->name);
116 b = prefix_underscores_count(symb->name);
122 /* If all else fails, choose the symbol with the longest name */
123 if (strlen(syma->name) >= strlen(symb->name))
129 void symbols__fixup_duplicate(struct rb_root *symbols)
132 struct symbol *curr, *next;
134 nd = rb_first(symbols);
137 curr = rb_entry(nd, struct symbol, rb_node);
139 nd = rb_next(&curr->rb_node);
140 next = rb_entry(nd, struct symbol, rb_node);
145 if (curr->start != next->start)
148 if (choose_best_symbol(curr, next) == SYMBOL_A) {
149 rb_erase(&next->rb_node, symbols);
152 nd = rb_next(&curr->rb_node);
153 rb_erase(&curr->rb_node, symbols);
158 void symbols__fixup_end(struct rb_root *symbols)
160 struct rb_node *nd, *prevnd = rb_first(symbols);
161 struct symbol *curr, *prev;
166 curr = rb_entry(prevnd, struct symbol, rb_node);
168 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
170 curr = rb_entry(nd, struct symbol, rb_node);
172 if (prev->end == prev->start && prev->end != curr->start)
173 prev->end = curr->start - 1;
177 if (curr->end == curr->start)
178 curr->end = roundup(curr->start, 4096);
181 void __map_groups__fixup_end(struct map_groups *mg, enum map_type type)
183 struct map *prev, *curr;
184 struct rb_node *nd, *prevnd = rb_first(&mg->maps[type]);
189 curr = rb_entry(prevnd, struct map, rb_node);
191 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
193 curr = rb_entry(nd, struct map, rb_node);
194 prev->end = curr->start - 1;
198 * We still haven't the actual symbols, so guess the
199 * last map final address.
204 static void map_groups__fixup_end(struct map_groups *mg)
207 for (i = 0; i < MAP__NR_TYPES; ++i)
208 __map_groups__fixup_end(mg, i);
211 struct symbol *symbol__new(u64 start, u64 len, u8 binding, const char *name)
213 size_t namelen = strlen(name) + 1;
214 struct symbol *sym = calloc(1, (symbol_conf.priv_size +
215 sizeof(*sym) + namelen));
219 if (symbol_conf.priv_size)
220 sym = ((void *)sym) + symbol_conf.priv_size;
223 sym->end = len ? start + len - 1 : start;
224 sym->binding = binding;
225 sym->namelen = namelen - 1;
227 pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n",
228 __func__, name, start, sym->end);
229 memcpy(sym->name, name, namelen);
234 void symbol__delete(struct symbol *sym)
236 free(((void *)sym) - symbol_conf.priv_size);
239 size_t symbol__fprintf(struct symbol *sym, FILE *fp)
241 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %c %s\n",
242 sym->start, sym->end,
243 sym->binding == STB_GLOBAL ? 'g' :
244 sym->binding == STB_LOCAL ? 'l' : 'w',
248 size_t symbol__fprintf_symname_offs(const struct symbol *sym,
249 const struct addr_location *al, FILE *fp)
251 unsigned long offset;
254 if (sym && sym->name) {
255 length = fprintf(fp, "%s", sym->name);
257 offset = al->addr - sym->start;
258 length += fprintf(fp, "+0x%lx", offset);
262 return fprintf(fp, "[unknown]");
265 size_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp)
267 return symbol__fprintf_symname_offs(sym, NULL, fp);
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)
285 struct rb_node **p = &symbols->rb_node;
286 struct rb_node *parent = NULL;
287 const u64 ip = sym->start;
292 s = rb_entry(parent, struct symbol, rb_node);
298 rb_link_node(&sym->rb_node, parent, p);
299 rb_insert_color(&sym->rb_node, symbols);
302 static struct symbol *symbols__find(struct rb_root *symbols, u64 ip)
309 n = symbols->rb_node;
312 struct symbol *s = rb_entry(n, struct symbol, rb_node);
316 else if (ip > s->end)
325 struct symbol_name_rb_node {
326 struct rb_node rb_node;
330 static void symbols__insert_by_name(struct rb_root *symbols, struct symbol *sym)
332 struct rb_node **p = &symbols->rb_node;
333 struct rb_node *parent = NULL;
334 struct symbol_name_rb_node *symn, *s;
336 symn = container_of(sym, struct symbol_name_rb_node, sym);
340 s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
341 if (strcmp(sym->name, s->sym.name) < 0)
346 rb_link_node(&symn->rb_node, parent, p);
347 rb_insert_color(&symn->rb_node, symbols);
350 static void symbols__sort_by_name(struct rb_root *symbols,
351 struct rb_root *source)
355 for (nd = rb_first(source); nd; nd = rb_next(nd)) {
356 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
357 symbols__insert_by_name(symbols, pos);
361 static struct symbol *symbols__find_by_name(struct rb_root *symbols,
369 n = symbols->rb_node;
372 struct symbol_name_rb_node *s;
375 s = rb_entry(n, struct symbol_name_rb_node, rb_node);
376 cmp = strcmp(name, s->sym.name);
389 struct symbol *dso__find_symbol(struct dso *dso,
390 enum map_type type, u64 addr)
392 return symbols__find(&dso->symbols[type], addr);
395 struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
398 return symbols__find_by_name(&dso->symbol_names[type], name);
401 void dso__sort_by_name(struct dso *dso, enum map_type type)
403 dso__set_sorted_by_name(dso, type);
404 return symbols__sort_by_name(&dso->symbol_names[type],
405 &dso->symbols[type]);
408 size_t dso__fprintf_symbols_by_name(struct dso *dso,
409 enum map_type type, FILE *fp)
413 struct symbol_name_rb_node *pos;
415 for (nd = rb_first(&dso->symbol_names[type]); nd; nd = rb_next(nd)) {
416 pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
417 fprintf(fp, "%s\n", pos->sym.name);
423 int kallsyms__parse(const char *filename, void *arg,
424 int (*process_symbol)(void *arg, const char *name,
425 char type, u64 start))
430 FILE *file = fopen(filename, "r");
437 while (!feof(file)) {
443 line_len = getline(&line, &n, file);
444 if (line_len < 0 || !line)
447 line[--line_len] = '\0'; /* \n */
449 len = hex2u64(line, &start);
452 if (len + 2 >= line_len)
455 symbol_type = line[len];
457 symbol_name = line + len;
458 len = line_len - len;
460 if (len >= KSYM_NAME_LEN) {
465 err = process_symbol(arg, symbol_name,
479 struct process_kallsyms_args {
484 static u8 kallsyms2elf_type(char type)
489 return isupper(type) ? STB_GLOBAL : STB_LOCAL;
492 static int map__process_kallsym_symbol(void *arg, const char *name,
493 char type, u64 start)
496 struct process_kallsyms_args *a = arg;
497 struct rb_root *root = &a->dso->symbols[a->map->type];
499 if (!symbol_type__is_a(type, a->map->type))
503 * module symbols are not sorted so we add all
504 * symbols, setting length to 0, and rely on
505 * symbols__fixup_end() to fix it up.
507 sym = symbol__new(start, 0, kallsyms2elf_type(type), name);
511 * We will pass the symbols to the filter later, in
512 * map__split_kallsyms, when we have split the maps per module
514 symbols__insert(root, sym);
520 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
521 * so that we can in the next step set the symbol ->end address and then
522 * call kernel_maps__split_kallsyms.
524 static int dso__load_all_kallsyms(struct dso *dso, const char *filename,
527 struct process_kallsyms_args args = { .map = map, .dso = dso, };
528 return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
532 * Split the symbols into maps, making sure there are no overlaps, i.e. the
533 * kernel range is broken in several maps, named [kernel].N, as we don't have
534 * the original ELF section names vmlinux have.
536 static int dso__split_kallsyms(struct dso *dso, struct map *map,
537 symbol_filter_t filter)
539 struct map_groups *kmaps = map__kmap(map)->kmaps;
540 struct machine *machine = kmaps->machine;
541 struct map *curr_map = map;
543 int count = 0, moved = 0;
544 struct rb_root *root = &dso->symbols[map->type];
545 struct rb_node *next = rb_first(root);
546 int kernel_range = 0;
551 pos = rb_entry(next, struct symbol, rb_node);
552 next = rb_next(&pos->rb_node);
554 module = strchr(pos->name, '\t');
556 if (!symbol_conf.use_modules)
561 if (strcmp(curr_map->dso->short_name, module)) {
562 if (curr_map != map &&
563 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
564 machine__is_default_guest(machine)) {
566 * We assume all symbols of a module are
567 * continuous in * kallsyms, so curr_map
568 * points to a module and all its
569 * symbols are in its kmap. Mark it as
572 dso__set_loaded(curr_map->dso,
576 curr_map = map_groups__find_by_name(kmaps,
578 if (curr_map == NULL) {
579 pr_debug("%s/proc/{kallsyms,modules} "
580 "inconsistency while looking "
581 "for \"%s\" module!\n",
582 machine->root_dir, module);
587 if (curr_map->dso->loaded &&
588 !machine__is_default_guest(machine))
592 * So that we look just like we get from .ko files,
593 * i.e. not prelinked, relative to map->start.
595 pos->start = curr_map->map_ip(curr_map, pos->start);
596 pos->end = curr_map->map_ip(curr_map, pos->end);
597 } else if (curr_map != map) {
598 char dso_name[PATH_MAX];
606 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
607 snprintf(dso_name, sizeof(dso_name),
611 snprintf(dso_name, sizeof(dso_name),
615 ndso = dso__new(dso_name);
619 ndso->kernel = dso->kernel;
621 curr_map = map__new2(pos->start, ndso, map->type);
622 if (curr_map == NULL) {
627 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
628 map_groups__insert(kmaps, curr_map);
632 if (filter && filter(curr_map, pos)) {
633 discard_symbol: rb_erase(&pos->rb_node, root);
636 if (curr_map != map) {
637 rb_erase(&pos->rb_node, root);
638 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
645 if (curr_map != map &&
646 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
647 machine__is_default_guest(kmaps->machine)) {
648 dso__set_loaded(curr_map->dso, curr_map->type);
651 return count + moved;
654 static bool symbol__restricted_filename(const char *filename,
655 const char *restricted_filename)
657 bool restricted = false;
659 if (symbol_conf.kptr_restrict) {
660 char *r = realpath(filename, NULL);
663 restricted = strcmp(r, restricted_filename) == 0;
672 int dso__load_kallsyms(struct dso *dso, const char *filename,
673 struct map *map, symbol_filter_t filter)
675 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
678 if (dso__load_all_kallsyms(dso, filename, map) < 0)
681 symbols__fixup_duplicate(&dso->symbols[map->type]);
682 symbols__fixup_end(&dso->symbols[map->type]);
684 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
685 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
687 dso->symtab_type = DSO_BINARY_TYPE__KALLSYMS;
689 return dso__split_kallsyms(dso, map, filter);
692 static int dso__load_perf_map(struct dso *dso, struct map *map,
693 symbol_filter_t filter)
700 file = fopen(dso->long_name, "r");
704 while (!feof(file)) {
709 line_len = getline(&line, &n, file);
716 line[--line_len] = '\0'; /* \n */
718 len = hex2u64(line, &start);
721 if (len + 2 >= line_len)
724 len += hex2u64(line + len, &size);
727 if (len + 2 >= line_len)
730 sym = symbol__new(start, size, STB_GLOBAL, line + len);
733 goto out_delete_line;
735 if (filter && filter(map, sym))
738 symbols__insert(&dso->symbols[map->type], sym);
754 int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter)
759 struct machine *machine;
760 char *root_dir = (char *) "";
762 struct symsrc ss_[2];
763 struct symsrc *syms_ss = NULL, *runtime_ss = NULL;
765 dso__set_loaded(dso, map->type);
767 if (dso->kernel == DSO_TYPE_KERNEL)
768 return dso__load_kernel_sym(dso, map, filter);
769 else if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
770 return dso__load_guest_kernel_sym(dso, map, filter);
772 if (map->groups && map->groups->machine)
773 machine = map->groups->machine;
777 name = malloc(PATH_MAX);
781 dso->adjust_symbols = 0;
783 if (strncmp(dso->name, "/tmp/perf-", 10) == 0) {
786 if (lstat(dso->name, &st) < 0)
789 if (st.st_uid && (st.st_uid != geteuid())) {
790 pr_warning("File %s not owned by current user or root, "
791 "ignoring it.\n", dso->name);
795 ret = dso__load_perf_map(dso, map, filter);
796 dso->symtab_type = ret > 0 ? DSO_BINARY_TYPE__JAVA_JIT :
797 DSO_BINARY_TYPE__NOT_FOUND;
802 root_dir = machine->root_dir;
804 /* Iterate over candidate debug images.
805 * Keep track of "interesting" ones (those which have a symtab, dynsym,
806 * and/or opd section) for processing.
808 for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) {
809 struct symsrc *ss = &ss_[ss_pos];
810 bool next_slot = false;
812 enum dso_binary_type symtab_type = binary_type_symtab[i];
814 if (dso__binary_type_file(dso, symtab_type,
815 root_dir, name, PATH_MAX))
818 /* Name is now the name of the next image to try */
819 if (symsrc__init(ss, dso, name, symtab_type) < 0)
822 if (!syms_ss && symsrc__has_symtab(ss)) {
827 if (!runtime_ss && symsrc__possibly_runtime(ss)) {
835 if (syms_ss && runtime_ss)
841 if (!runtime_ss && !syms_ss)
844 if (runtime_ss && !syms_ss) {
845 syms_ss = runtime_ss;
848 /* We'll have to hope for the best */
849 if (!runtime_ss && syms_ss)
850 runtime_ss = syms_ss;
853 ret = dso__load_sym(dso, map, syms_ss, runtime_ss, filter, 0);
860 nr_plt = dso__synthesize_plt_symbols(dso, runtime_ss, map, filter);
865 for (; ss_pos > 0; ss_pos--)
866 symsrc__destroy(&ss_[ss_pos - 1]);
869 if (ret < 0 && strstr(dso->name, " (deleted)") != NULL)
874 struct map *map_groups__find_by_name(struct map_groups *mg,
875 enum map_type type, const char *name)
879 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
880 struct map *map = rb_entry(nd, struct map, rb_node);
882 if (map->dso && strcmp(map->dso->short_name, name) == 0)
889 static int map_groups__set_modules_path_dir(struct map_groups *mg,
890 const char *dir_name)
893 DIR *dir = opendir(dir_name);
897 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
901 while ((dent = readdir(dir)) != NULL) {
905 /*sshfs might return bad dent->d_type, so we have to stat*/
906 snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name);
910 if (S_ISDIR(st.st_mode)) {
911 if (!strcmp(dent->d_name, ".") ||
912 !strcmp(dent->d_name, ".."))
915 ret = map_groups__set_modules_path_dir(mg, path);
919 char *dot = strrchr(dent->d_name, '.'),
924 if (dot == NULL || strcmp(dot, ".ko"))
926 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
927 (int)(dot - dent->d_name), dent->d_name);
929 strxfrchar(dso_name, '-', '_');
930 map = map_groups__find_by_name(mg, MAP__FUNCTION,
935 long_name = strdup(path);
936 if (long_name == NULL) {
940 dso__set_long_name(map->dso, long_name);
941 map->dso->lname_alloc = 1;
942 dso__kernel_module_get_build_id(map->dso, "");
951 static char *get_kernel_version(const char *root_dir)
953 char version[PATH_MAX];
956 const char *prefix = "Linux version ";
958 sprintf(version, "%s/proc/version", root_dir);
959 file = fopen(version, "r");
964 tmp = fgets(version, sizeof(version), file);
967 name = strstr(version, prefix);
970 name += strlen(prefix);
971 tmp = strchr(name, ' ');
978 static int machine__set_modules_path(struct machine *machine)
981 char modules_path[PATH_MAX];
983 version = get_kernel_version(machine->root_dir);
987 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s/kernel",
988 machine->root_dir, version);
991 return map_groups__set_modules_path_dir(&machine->kmaps, modules_path);
994 struct map *machine__new_module(struct machine *machine, u64 start,
995 const char *filename)
998 struct dso *dso = __dsos__findnew(&machine->kernel_dsos, filename);
1003 map = map__new2(start, dso, MAP__FUNCTION);
1007 if (machine__is_host(machine))
1008 dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
1010 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
1011 map_groups__insert(&machine->kmaps, map);
1015 static int machine__create_modules(struct machine *machine)
1021 const char *modules;
1022 char path[PATH_MAX];
1024 if (machine__is_default_guest(machine))
1025 modules = symbol_conf.default_guest_modules;
1027 sprintf(path, "%s/proc/modules", machine->root_dir);
1031 if (symbol__restricted_filename(path, "/proc/modules"))
1034 file = fopen(modules, "r");
1038 while (!feof(file)) {
1039 char name[PATH_MAX];
1044 line_len = getline(&line, &n, file);
1051 line[--line_len] = '\0'; /* \n */
1053 sep = strrchr(line, 'x');
1057 hex2u64(sep + 1, &start);
1059 sep = strchr(line, ' ');
1065 snprintf(name, sizeof(name), "[%s]", line);
1066 map = machine__new_module(machine, start, name);
1068 goto out_delete_line;
1069 dso__kernel_module_get_build_id(map->dso, machine->root_dir);
1075 return machine__set_modules_path(machine);
1083 int dso__load_vmlinux(struct dso *dso, struct map *map,
1084 const char *vmlinux, symbol_filter_t filter)
1088 char symfs_vmlinux[PATH_MAX];
1089 enum dso_binary_type symtab_type;
1091 snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s%s",
1092 symbol_conf.symfs, vmlinux);
1094 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1095 symtab_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
1097 symtab_type = DSO_BINARY_TYPE__VMLINUX;
1099 if (symsrc__init(&ss, dso, symfs_vmlinux, symtab_type))
1102 err = dso__load_sym(dso, map, &ss, &ss, filter, 0);
1103 symsrc__destroy(&ss);
1106 dso__set_long_name(dso, (char *)vmlinux);
1107 dso__set_loaded(dso, map->type);
1108 pr_debug("Using %s for symbols\n", symfs_vmlinux);
1114 int dso__load_vmlinux_path(struct dso *dso, struct map *map,
1115 symbol_filter_t filter)
1120 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1121 vmlinux_path__nr_entries + 1);
1123 filename = dso__build_id_filename(dso, NULL, 0);
1124 if (filename != NULL) {
1125 err = dso__load_vmlinux(dso, map, filename, filter);
1131 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
1132 err = dso__load_vmlinux(dso, map, vmlinux_path[i], filter);
1134 dso__set_long_name(dso, strdup(vmlinux_path[i]));
1142 static int dso__load_kernel_sym(struct dso *dso, struct map *map,
1143 symbol_filter_t filter)
1146 const char *kallsyms_filename = NULL;
1147 char *kallsyms_allocated_filename = NULL;
1149 * Step 1: if the user specified a kallsyms or vmlinux filename, use
1150 * it and only it, reporting errors to the user if it cannot be used.
1152 * For instance, try to analyse an ARM perf.data file _without_ a
1153 * build-id, or if the user specifies the wrong path to the right
1154 * vmlinux file, obviously we can't fallback to another vmlinux (a
1155 * x86_86 one, on the machine where analysis is being performed, say),
1156 * or worse, /proc/kallsyms.
1158 * If the specified file _has_ a build-id and there is a build-id
1159 * section in the perf.data file, we will still do the expected
1160 * validation in dso__load_vmlinux and will bail out if they don't
1163 if (symbol_conf.kallsyms_name != NULL) {
1164 kallsyms_filename = symbol_conf.kallsyms_name;
1168 if (symbol_conf.vmlinux_name != NULL) {
1169 err = dso__load_vmlinux(dso, map,
1170 symbol_conf.vmlinux_name, filter);
1172 dso__set_long_name(dso,
1173 strdup(symbol_conf.vmlinux_name));
1179 if (vmlinux_path != NULL) {
1180 err = dso__load_vmlinux_path(dso, map, filter);
1185 /* do not try local files if a symfs was given */
1186 if (symbol_conf.symfs[0] != 0)
1190 * Say the kernel DSO was created when processing the build-id header table,
1191 * we have a build-id, so check if it is the same as the running kernel,
1192 * using it if it is.
1194 if (dso->has_build_id) {
1195 u8 kallsyms_build_id[BUILD_ID_SIZE];
1196 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1198 if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id,
1199 sizeof(kallsyms_build_id)) == 0) {
1200 if (dso__build_id_equal(dso, kallsyms_build_id)) {
1201 kallsyms_filename = "/proc/kallsyms";
1206 * Now look if we have it on the build-id cache in
1207 * $HOME/.debug/[kernel.kallsyms].
1209 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1212 if (asprintf(&kallsyms_allocated_filename,
1213 "%s/.debug/[kernel.kallsyms]/%s",
1214 getenv("HOME"), sbuild_id) == -1) {
1215 pr_err("Not enough memory for kallsyms file lookup\n");
1219 kallsyms_filename = kallsyms_allocated_filename;
1221 if (access(kallsyms_filename, F_OK)) {
1222 pr_err("No kallsyms or vmlinux with build-id %s "
1223 "was found\n", sbuild_id);
1224 free(kallsyms_allocated_filename);
1229 * Last resort, if we don't have a build-id and couldn't find
1230 * any vmlinux file, try the running kernel kallsyms table.
1232 kallsyms_filename = "/proc/kallsyms";
1236 err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
1238 pr_debug("Using %s for symbols\n", kallsyms_filename);
1239 free(kallsyms_allocated_filename);
1242 dso__set_long_name(dso, strdup("[kernel.kallsyms]"));
1244 map__fixup_start(map);
1245 map__fixup_end(map);
1251 static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
1252 symbol_filter_t filter)
1255 const char *kallsyms_filename = NULL;
1256 struct machine *machine;
1257 char path[PATH_MAX];
1260 pr_debug("Guest kernel map hasn't the point to groups\n");
1263 machine = map->groups->machine;
1265 if (machine__is_default_guest(machine)) {
1267 * if the user specified a vmlinux filename, use it and only
1268 * it, reporting errors to the user if it cannot be used.
1269 * Or use file guest_kallsyms inputted by user on commandline
1271 if (symbol_conf.default_guest_vmlinux_name != NULL) {
1272 err = dso__load_vmlinux(dso, map,
1273 symbol_conf.default_guest_vmlinux_name, filter);
1277 kallsyms_filename = symbol_conf.default_guest_kallsyms;
1278 if (!kallsyms_filename)
1281 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
1282 kallsyms_filename = path;
1285 err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
1287 pr_debug("Using %s for symbols\n", kallsyms_filename);
1291 if (kallsyms_filename != NULL) {
1292 machine__mmap_name(machine, path, sizeof(path));
1293 dso__set_long_name(dso, strdup(path));
1295 map__fixup_start(map);
1296 map__fixup_end(map);
1302 size_t machines__fprintf_dsos(struct rb_root *machines, FILE *fp)
1307 for (nd = rb_first(machines); nd; nd = rb_next(nd)) {
1308 struct machine *pos = rb_entry(nd, struct machine, rb_node);
1309 ret += __dsos__fprintf(&pos->kernel_dsos, fp);
1310 ret += __dsos__fprintf(&pos->user_dsos, fp);
1316 size_t machine__fprintf_dsos_buildid(struct machine *machine, FILE *fp,
1319 return __dsos__fprintf_buildid(&machine->kernel_dsos, fp, with_hits) +
1320 __dsos__fprintf_buildid(&machine->user_dsos, fp, with_hits);
1323 size_t machines__fprintf_dsos_buildid(struct rb_root *machines,
1324 FILE *fp, bool with_hits)
1329 for (nd = rb_first(machines); nd; nd = rb_next(nd)) {
1330 struct machine *pos = rb_entry(nd, struct machine, rb_node);
1331 ret += machine__fprintf_dsos_buildid(pos, fp, with_hits);
1336 static struct dso *machine__get_kernel(struct machine *machine)
1338 const char *vmlinux_name = NULL;
1341 if (machine__is_host(machine)) {
1342 vmlinux_name = symbol_conf.vmlinux_name;
1344 vmlinux_name = "[kernel.kallsyms]";
1346 kernel = dso__kernel_findnew(machine, vmlinux_name,
1352 if (machine__is_default_guest(machine))
1353 vmlinux_name = symbol_conf.default_guest_vmlinux_name;
1355 vmlinux_name = machine__mmap_name(machine, bf,
1358 kernel = dso__kernel_findnew(machine, vmlinux_name,
1360 DSO_TYPE_GUEST_KERNEL);
1363 if (kernel != NULL && (!kernel->has_build_id))
1364 dso__read_running_kernel_build_id(kernel, machine);
1369 struct process_args {
1373 static int symbol__in_kernel(void *arg, const char *name,
1374 char type __maybe_unused, u64 start)
1376 struct process_args *args = arg;
1378 if (strchr(name, '['))
1381 args->start = start;
1385 /* Figure out the start address of kernel map from /proc/kallsyms */
1386 static u64 machine__get_kernel_start_addr(struct machine *machine)
1388 const char *filename;
1389 char path[PATH_MAX];
1390 struct process_args args;
1392 if (machine__is_host(machine)) {
1393 filename = "/proc/kallsyms";
1395 if (machine__is_default_guest(machine))
1396 filename = (char *)symbol_conf.default_guest_kallsyms;
1398 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
1403 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
1406 if (kallsyms__parse(filename, &args, symbol__in_kernel) <= 0)
1412 int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
1415 u64 start = machine__get_kernel_start_addr(machine);
1417 for (type = 0; type < MAP__NR_TYPES; ++type) {
1420 machine->vmlinux_maps[type] = map__new2(start, kernel, type);
1421 if (machine->vmlinux_maps[type] == NULL)
1424 machine->vmlinux_maps[type]->map_ip =
1425 machine->vmlinux_maps[type]->unmap_ip =
1427 kmap = map__kmap(machine->vmlinux_maps[type]);
1428 kmap->kmaps = &machine->kmaps;
1429 map_groups__insert(&machine->kmaps,
1430 machine->vmlinux_maps[type]);
1436 void machine__destroy_kernel_maps(struct machine *machine)
1440 for (type = 0; type < MAP__NR_TYPES; ++type) {
1443 if (machine->vmlinux_maps[type] == NULL)
1446 kmap = map__kmap(machine->vmlinux_maps[type]);
1447 map_groups__remove(&machine->kmaps,
1448 machine->vmlinux_maps[type]);
1449 if (kmap->ref_reloc_sym) {
1451 * ref_reloc_sym is shared among all maps, so free just
1454 if (type == MAP__FUNCTION) {
1455 free((char *)kmap->ref_reloc_sym->name);
1456 kmap->ref_reloc_sym->name = NULL;
1457 free(kmap->ref_reloc_sym);
1459 kmap->ref_reloc_sym = NULL;
1462 map__delete(machine->vmlinux_maps[type]);
1463 machine->vmlinux_maps[type] = NULL;
1467 int machine__create_kernel_maps(struct machine *machine)
1469 struct dso *kernel = machine__get_kernel(machine);
1471 if (kernel == NULL ||
1472 __machine__create_kernel_maps(machine, kernel) < 0)
1475 if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
1476 if (machine__is_host(machine))
1477 pr_debug("Problems creating module maps, "
1478 "continuing anyway...\n");
1480 pr_debug("Problems creating module maps for guest %d, "
1481 "continuing anyway...\n", machine->pid);
1485 * Now that we have all the maps created, just set the ->end of them:
1487 map_groups__fixup_end(&machine->kmaps);
1491 static void vmlinux_path__exit(void)
1493 while (--vmlinux_path__nr_entries >= 0) {
1494 free(vmlinux_path[vmlinux_path__nr_entries]);
1495 vmlinux_path[vmlinux_path__nr_entries] = NULL;
1499 vmlinux_path = NULL;
1502 static int vmlinux_path__init(void)
1507 vmlinux_path = malloc(sizeof(char *) * 5);
1508 if (vmlinux_path == NULL)
1511 vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
1512 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1514 ++vmlinux_path__nr_entries;
1515 vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
1516 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1518 ++vmlinux_path__nr_entries;
1520 /* only try running kernel version if no symfs was given */
1521 if (symbol_conf.symfs[0] != 0)
1524 if (uname(&uts) < 0)
1527 snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
1528 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1529 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1531 ++vmlinux_path__nr_entries;
1532 snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
1533 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1534 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1536 ++vmlinux_path__nr_entries;
1537 snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
1539 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1540 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1542 ++vmlinux_path__nr_entries;
1547 vmlinux_path__exit();
1551 size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
1555 struct dso *kdso = machine->vmlinux_maps[MAP__FUNCTION]->dso;
1557 if (kdso->has_build_id) {
1558 char filename[PATH_MAX];
1559 if (dso__build_id_filename(kdso, filename, sizeof(filename)))
1560 printed += fprintf(fp, "[0] %s\n", filename);
1563 for (i = 0; i < vmlinux_path__nr_entries; ++i)
1564 printed += fprintf(fp, "[%d] %s\n",
1565 i + kdso->has_build_id, vmlinux_path[i]);
1570 static int setup_list(struct strlist **list, const char *list_str,
1571 const char *list_name)
1573 if (list_str == NULL)
1576 *list = strlist__new(true, list_str);
1578 pr_err("problems parsing %s list\n", list_name);
1584 static bool symbol__read_kptr_restrict(void)
1588 if (geteuid() != 0) {
1589 FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
1593 if (fgets(line, sizeof(line), fp) != NULL)
1594 value = atoi(line) != 0;
1603 int symbol__init(void)
1607 if (symbol_conf.initialized)
1610 symbol_conf.priv_size = PERF_ALIGN(symbol_conf.priv_size, sizeof(u64));
1614 if (symbol_conf.sort_by_name)
1615 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
1616 sizeof(struct symbol));
1618 if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
1621 if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
1622 pr_err("'.' is the only non valid --field-separator argument\n");
1626 if (setup_list(&symbol_conf.dso_list,
1627 symbol_conf.dso_list_str, "dso") < 0)
1630 if (setup_list(&symbol_conf.comm_list,
1631 symbol_conf.comm_list_str, "comm") < 0)
1632 goto out_free_dso_list;
1634 if (setup_list(&symbol_conf.sym_list,
1635 symbol_conf.sym_list_str, "symbol") < 0)
1636 goto out_free_comm_list;
1639 * A path to symbols of "/" is identical to ""
1640 * reset here for simplicity.
1642 symfs = realpath(symbol_conf.symfs, NULL);
1644 symfs = symbol_conf.symfs;
1645 if (strcmp(symfs, "/") == 0)
1646 symbol_conf.symfs = "";
1647 if (symfs != symbol_conf.symfs)
1648 free((void *)symfs);
1650 symbol_conf.kptr_restrict = symbol__read_kptr_restrict();
1652 symbol_conf.initialized = true;
1656 strlist__delete(symbol_conf.comm_list);
1658 strlist__delete(symbol_conf.dso_list);
1662 void symbol__exit(void)
1664 if (!symbol_conf.initialized)
1666 strlist__delete(symbol_conf.sym_list);
1667 strlist__delete(symbol_conf.dso_list);
1668 strlist__delete(symbol_conf.comm_list);
1669 vmlinux_path__exit();
1670 symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
1671 symbol_conf.initialized = false;
1674 int machines__create_kernel_maps(struct rb_root *machines, pid_t pid)
1676 struct machine *machine = machines__findnew(machines, pid);
1678 if (machine == NULL)
1681 return machine__create_kernel_maps(machine);
1684 int machines__create_guest_kernel_maps(struct rb_root *machines)
1687 struct dirent **namelist = NULL;
1689 char path[PATH_MAX];
1693 if (symbol_conf.default_guest_vmlinux_name ||
1694 symbol_conf.default_guest_modules ||
1695 symbol_conf.default_guest_kallsyms) {
1696 machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
1699 if (symbol_conf.guestmount) {
1700 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
1703 for (i = 0; i < items; i++) {
1704 if (!isdigit(namelist[i]->d_name[0])) {
1705 /* Filter out . and .. */
1708 pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10);
1709 if ((*endp != '\0') ||
1710 (endp == namelist[i]->d_name) ||
1711 (errno == ERANGE)) {
1712 pr_debug("invalid directory (%s). Skipping.\n",
1713 namelist[i]->d_name);
1716 sprintf(path, "%s/%s/proc/kallsyms",
1717 symbol_conf.guestmount,
1718 namelist[i]->d_name);
1719 ret = access(path, R_OK);
1721 pr_debug("Can't access file %s\n", path);
1724 machines__create_kernel_maps(machines, pid);
1733 void machines__destroy_guest_kernel_maps(struct rb_root *machines)
1735 struct rb_node *next = rb_first(machines);
1738 struct machine *pos = rb_entry(next, struct machine, rb_node);
1740 next = rb_next(&pos->rb_node);
1741 rb_erase(&pos->rb_node, machines);
1742 machine__delete(pos);
1746 int machine__load_kallsyms(struct machine *machine, const char *filename,
1747 enum map_type type, symbol_filter_t filter)
1749 struct map *map = machine->vmlinux_maps[type];
1750 int ret = dso__load_kallsyms(map->dso, filename, map, filter);
1753 dso__set_loaded(map->dso, type);
1755 * Since /proc/kallsyms will have multiple sessions for the
1756 * kernel, with modules between them, fixup the end of all
1759 __map_groups__fixup_end(&machine->kmaps, type);
1765 int machine__load_vmlinux_path(struct machine *machine, enum map_type type,
1766 symbol_filter_t filter)
1768 struct map *map = machine->vmlinux_maps[type];
1769 int ret = dso__load_vmlinux_path(map->dso, map, filter);
1772 dso__set_loaded(map->dso, type);
1773 map__reloc_vmlinux(map);