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 static int vmlinux_path__nr_entries;
32 static char **vmlinux_path;
34 struct symbol_conf symbol_conf = {
35 .exclude_other = true,
37 .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__NOT_FOUND,
57 #define DSO_BINARY_TYPE__SYMTAB_CNT ARRAY_SIZE(binary_type_symtab)
59 bool symbol_type__is_a(char symbol_type, enum map_type map_type)
61 symbol_type = toupper(symbol_type);
65 return symbol_type == 'T' || symbol_type == 'W';
67 return symbol_type == 'D';
73 static int prefix_underscores_count(const char *str)
75 const char *tail = str;
86 static int choose_best_symbol(struct symbol *syma, struct symbol *symb)
91 /* Prefer a symbol with non zero length */
92 a = syma->end - syma->start;
93 b = symb->end - symb->start;
94 if ((b == 0) && (a > 0))
96 else if ((a == 0) && (b > 0))
99 /* Prefer a non weak symbol over a weak one */
100 a = syma->binding == STB_WEAK;
101 b = symb->binding == STB_WEAK;
107 /* Prefer a global symbol over a non global one */
108 a = syma->binding == STB_GLOBAL;
109 b = symb->binding == STB_GLOBAL;
115 /* Prefer a symbol with less underscores */
116 a = prefix_underscores_count(syma->name);
117 b = prefix_underscores_count(symb->name);
123 /* If all else fails, choose the symbol with the longest name */
124 if (strlen(syma->name) >= strlen(symb->name))
130 void symbols__fixup_duplicate(struct rb_root *symbols)
133 struct symbol *curr, *next;
135 nd = rb_first(symbols);
138 curr = rb_entry(nd, struct symbol, rb_node);
140 nd = rb_next(&curr->rb_node);
141 next = rb_entry(nd, struct symbol, rb_node);
146 if (curr->start != next->start)
149 if (choose_best_symbol(curr, next) == SYMBOL_A) {
150 rb_erase(&next->rb_node, symbols);
153 nd = rb_next(&curr->rb_node);
154 rb_erase(&curr->rb_node, symbols);
159 void symbols__fixup_end(struct rb_root *symbols)
161 struct rb_node *nd, *prevnd = rb_first(symbols);
162 struct symbol *curr, *prev;
167 curr = rb_entry(prevnd, struct symbol, rb_node);
169 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
171 curr = rb_entry(nd, struct symbol, rb_node);
173 if (prev->end == prev->start && prev->end != curr->start)
174 prev->end = curr->start - 1;
178 if (curr->end == curr->start)
179 curr->end = roundup(curr->start, 4096);
182 void __map_groups__fixup_end(struct map_groups *mg, enum map_type type)
184 struct map *prev, *curr;
185 struct rb_node *nd, *prevnd = rb_first(&mg->maps[type]);
190 curr = rb_entry(prevnd, struct map, rb_node);
192 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
194 curr = rb_entry(nd, struct map, rb_node);
195 prev->end = curr->start - 1;
199 * We still haven't the actual symbols, so guess the
200 * last map final address.
205 static void map_groups__fixup_end(struct map_groups *mg)
208 for (i = 0; i < MAP__NR_TYPES; ++i)
209 __map_groups__fixup_end(mg, i);
212 struct symbol *symbol__new(u64 start, u64 len, u8 binding, const char *name)
214 size_t namelen = strlen(name) + 1;
215 struct symbol *sym = calloc(1, (symbol_conf.priv_size +
216 sizeof(*sym) + namelen));
220 if (symbol_conf.priv_size)
221 sym = ((void *)sym) + symbol_conf.priv_size;
224 sym->end = len ? start + len - 1 : start;
225 sym->binding = binding;
226 sym->namelen = namelen - 1;
228 pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n",
229 __func__, name, start, sym->end);
230 memcpy(sym->name, name, namelen);
235 void symbol__delete(struct symbol *sym)
237 free(((void *)sym) - symbol_conf.priv_size);
240 size_t symbol__fprintf(struct symbol *sym, FILE *fp)
242 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %c %s\n",
243 sym->start, sym->end,
244 sym->binding == STB_GLOBAL ? 'g' :
245 sym->binding == STB_LOCAL ? 'l' : 'w',
249 size_t symbol__fprintf_symname_offs(const struct symbol *sym,
250 const struct addr_location *al, FILE *fp)
252 unsigned long offset;
255 if (sym && sym->name) {
256 length = fprintf(fp, "%s", sym->name);
258 offset = al->addr - sym->start;
259 length += fprintf(fp, "+0x%lx", offset);
263 return fprintf(fp, "[unknown]");
266 size_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp)
268 return symbol__fprintf_symname_offs(sym, NULL, fp);
271 void symbols__delete(struct rb_root *symbols)
274 struct rb_node *next = rb_first(symbols);
277 pos = rb_entry(next, struct symbol, rb_node);
278 next = rb_next(&pos->rb_node);
279 rb_erase(&pos->rb_node, symbols);
284 void symbols__insert(struct rb_root *symbols, struct symbol *sym)
286 struct rb_node **p = &symbols->rb_node;
287 struct rb_node *parent = NULL;
288 const u64 ip = sym->start;
293 s = rb_entry(parent, struct symbol, rb_node);
299 rb_link_node(&sym->rb_node, parent, p);
300 rb_insert_color(&sym->rb_node, symbols);
303 static struct symbol *symbols__find(struct rb_root *symbols, u64 ip)
310 n = symbols->rb_node;
313 struct symbol *s = rb_entry(n, struct symbol, rb_node);
317 else if (ip > s->end)
326 struct symbol_name_rb_node {
327 struct rb_node rb_node;
331 static void symbols__insert_by_name(struct rb_root *symbols, struct symbol *sym)
333 struct rb_node **p = &symbols->rb_node;
334 struct rb_node *parent = NULL;
335 struct symbol_name_rb_node *symn, *s;
337 symn = container_of(sym, struct symbol_name_rb_node, sym);
341 s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
342 if (strcmp(sym->name, s->sym.name) < 0)
347 rb_link_node(&symn->rb_node, parent, p);
348 rb_insert_color(&symn->rb_node, symbols);
351 static void symbols__sort_by_name(struct rb_root *symbols,
352 struct rb_root *source)
356 for (nd = rb_first(source); nd; nd = rb_next(nd)) {
357 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
358 symbols__insert_by_name(symbols, pos);
362 static struct symbol *symbols__find_by_name(struct rb_root *symbols,
370 n = symbols->rb_node;
373 struct symbol_name_rb_node *s;
376 s = rb_entry(n, struct symbol_name_rb_node, rb_node);
377 cmp = strcmp(name, s->sym.name);
390 struct symbol *dso__find_symbol(struct dso *dso,
391 enum map_type type, u64 addr)
393 return symbols__find(&dso->symbols[type], addr);
396 struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
399 return symbols__find_by_name(&dso->symbol_names[type], name);
402 void dso__sort_by_name(struct dso *dso, enum map_type type)
404 dso__set_sorted_by_name(dso, type);
405 return symbols__sort_by_name(&dso->symbol_names[type],
406 &dso->symbols[type]);
409 size_t dso__fprintf_symbols_by_name(struct dso *dso,
410 enum map_type type, FILE *fp)
414 struct symbol_name_rb_node *pos;
416 for (nd = rb_first(&dso->symbol_names[type]); nd; nd = rb_next(nd)) {
417 pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
418 fprintf(fp, "%s\n", pos->sym.name);
424 int kallsyms__parse(const char *filename, void *arg,
425 int (*process_symbol)(void *arg, const char *name,
426 char type, u64 start))
431 FILE *file = fopen(filename, "r");
438 while (!feof(file)) {
444 line_len = getline(&line, &n, file);
445 if (line_len < 0 || !line)
448 line[--line_len] = '\0'; /* \n */
450 len = hex2u64(line, &start);
453 if (len + 2 >= line_len)
456 symbol_type = line[len];
458 symbol_name = line + len;
459 len = line_len - len;
461 if (len >= KSYM_NAME_LEN) {
466 err = process_symbol(arg, symbol_name,
480 struct process_kallsyms_args {
485 static u8 kallsyms2elf_type(char type)
490 return isupper(type) ? STB_GLOBAL : STB_LOCAL;
493 static int map__process_kallsym_symbol(void *arg, const char *name,
494 char type, u64 start)
497 struct process_kallsyms_args *a = arg;
498 struct rb_root *root = &a->dso->symbols[a->map->type];
500 if (!symbol_type__is_a(type, a->map->type))
504 * module symbols are not sorted so we add all
505 * symbols, setting length to 0, and rely on
506 * symbols__fixup_end() to fix it up.
508 sym = symbol__new(start, 0, kallsyms2elf_type(type), name);
512 * We will pass the symbols to the filter later, in
513 * map__split_kallsyms, when we have split the maps per module
515 symbols__insert(root, sym);
521 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
522 * so that we can in the next step set the symbol ->end address and then
523 * call kernel_maps__split_kallsyms.
525 static int dso__load_all_kallsyms(struct dso *dso, const char *filename,
528 struct process_kallsyms_args args = { .map = map, .dso = dso, };
529 return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
533 * Split the symbols into maps, making sure there are no overlaps, i.e. the
534 * kernel range is broken in several maps, named [kernel].N, as we don't have
535 * the original ELF section names vmlinux have.
537 static int dso__split_kallsyms(struct dso *dso, struct map *map,
538 symbol_filter_t filter)
540 struct map_groups *kmaps = map__kmap(map)->kmaps;
541 struct machine *machine = kmaps->machine;
542 struct map *curr_map = map;
544 int count = 0, moved = 0;
545 struct rb_root *root = &dso->symbols[map->type];
546 struct rb_node *next = rb_first(root);
547 int kernel_range = 0;
552 pos = rb_entry(next, struct symbol, rb_node);
553 next = rb_next(&pos->rb_node);
555 module = strchr(pos->name, '\t');
557 if (!symbol_conf.use_modules)
562 if (strcmp(curr_map->dso->short_name, module)) {
563 if (curr_map != map &&
564 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
565 machine__is_default_guest(machine)) {
567 * We assume all symbols of a module are
568 * continuous in * kallsyms, so curr_map
569 * points to a module and all its
570 * symbols are in its kmap. Mark it as
573 dso__set_loaded(curr_map->dso,
577 curr_map = map_groups__find_by_name(kmaps,
579 if (curr_map == NULL) {
580 pr_debug("%s/proc/{kallsyms,modules} "
581 "inconsistency while looking "
582 "for \"%s\" module!\n",
583 machine->root_dir, module);
588 if (curr_map->dso->loaded &&
589 !machine__is_default_guest(machine))
593 * So that we look just like we get from .ko files,
594 * i.e. not prelinked, relative to map->start.
596 pos->start = curr_map->map_ip(curr_map, pos->start);
597 pos->end = curr_map->map_ip(curr_map, pos->end);
598 } else if (curr_map != map) {
599 char dso_name[PATH_MAX];
607 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
608 snprintf(dso_name, sizeof(dso_name),
612 snprintf(dso_name, sizeof(dso_name),
616 ndso = dso__new(dso_name);
620 ndso->kernel = dso->kernel;
622 curr_map = map__new2(pos->start, ndso, map->type);
623 if (curr_map == NULL) {
628 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
629 map_groups__insert(kmaps, curr_map);
633 if (filter && filter(curr_map, pos)) {
634 discard_symbol: rb_erase(&pos->rb_node, root);
637 if (curr_map != map) {
638 rb_erase(&pos->rb_node, root);
639 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
646 if (curr_map != map &&
647 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
648 machine__is_default_guest(kmaps->machine)) {
649 dso__set_loaded(curr_map->dso, curr_map->type);
652 return count + moved;
655 static bool symbol__restricted_filename(const char *filename,
656 const char *restricted_filename)
658 bool restricted = false;
660 if (symbol_conf.kptr_restrict) {
661 char *r = realpath(filename, NULL);
664 restricted = strcmp(r, restricted_filename) == 0;
673 int dso__load_kallsyms(struct dso *dso, const char *filename,
674 struct map *map, symbol_filter_t filter)
676 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
679 if (dso__load_all_kallsyms(dso, filename, map) < 0)
682 symbols__fixup_duplicate(&dso->symbols[map->type]);
683 symbols__fixup_end(&dso->symbols[map->type]);
685 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
686 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
688 dso->symtab_type = DSO_BINARY_TYPE__KALLSYMS;
690 return dso__split_kallsyms(dso, map, filter);
693 static int dso__load_perf_map(struct dso *dso, struct map *map,
694 symbol_filter_t filter)
701 file = fopen(dso->long_name, "r");
705 while (!feof(file)) {
710 line_len = getline(&line, &n, file);
717 line[--line_len] = '\0'; /* \n */
719 len = hex2u64(line, &start);
722 if (len + 2 >= line_len)
725 len += hex2u64(line + len, &size);
728 if (len + 2 >= line_len)
731 sym = symbol__new(start, size, STB_GLOBAL, line + len);
734 goto out_delete_line;
736 if (filter && filter(map, sym))
739 symbols__insert(&dso->symbols[map->type], sym);
755 int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter)
760 struct machine *machine;
761 char *root_dir = (char *) "";
763 struct symsrc ss_[2];
764 struct symsrc *syms_ss = NULL, *runtime_ss = NULL;
766 dso__set_loaded(dso, map->type);
768 if (dso->kernel == DSO_TYPE_KERNEL)
769 return dso__load_kernel_sym(dso, map, filter);
770 else if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
771 return dso__load_guest_kernel_sym(dso, map, filter);
773 if (map->groups && map->groups->machine)
774 machine = map->groups->machine;
778 name = malloc(PATH_MAX);
782 dso->adjust_symbols = 0;
784 if (strncmp(dso->name, "/tmp/perf-", 10) == 0) {
787 if (lstat(dso->name, &st) < 0)
790 if (st.st_uid && (st.st_uid != geteuid())) {
791 pr_warning("File %s not owned by current user or root, "
792 "ignoring it.\n", dso->name);
796 ret = dso__load_perf_map(dso, map, filter);
797 dso->symtab_type = ret > 0 ? DSO_BINARY_TYPE__JAVA_JIT :
798 DSO_BINARY_TYPE__NOT_FOUND;
803 root_dir = machine->root_dir;
805 /* Iterate over candidate debug images.
806 * Keep track of "interesting" ones (those which have a symtab, dynsym,
807 * and/or opd section) for processing.
809 for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) {
810 struct symsrc *ss = &ss_[ss_pos];
811 bool next_slot = false;
813 enum dso_binary_type symtab_type = binary_type_symtab[i];
815 if (dso__binary_type_file(dso, symtab_type,
816 root_dir, name, PATH_MAX))
819 /* Name is now the name of the next image to try */
820 if (symsrc__init(ss, dso, name, symtab_type) < 0)
823 if (!syms_ss && symsrc__has_symtab(ss)) {
828 if (!runtime_ss && symsrc__possibly_runtime(ss)) {
836 if (syms_ss && runtime_ss)
842 if (!runtime_ss && !syms_ss)
845 if (runtime_ss && !syms_ss) {
846 syms_ss = runtime_ss;
849 /* We'll have to hope for the best */
850 if (!runtime_ss && syms_ss)
851 runtime_ss = syms_ss;
854 ret = dso__load_sym(dso, map, syms_ss, runtime_ss, filter, 0);
861 nr_plt = dso__synthesize_plt_symbols(dso, runtime_ss, map, filter);
866 for (; ss_pos > 0; ss_pos--)
867 symsrc__destroy(&ss_[ss_pos - 1]);
870 if (ret < 0 && strstr(dso->name, " (deleted)") != NULL)
875 struct map *map_groups__find_by_name(struct map_groups *mg,
876 enum map_type type, const char *name)
880 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
881 struct map *map = rb_entry(nd, struct map, rb_node);
883 if (map->dso && strcmp(map->dso->short_name, name) == 0)
890 static int map_groups__set_modules_path_dir(struct map_groups *mg,
891 const char *dir_name)
894 DIR *dir = opendir(dir_name);
898 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
902 while ((dent = readdir(dir)) != NULL) {
906 /*sshfs might return bad dent->d_type, so we have to stat*/
907 snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name);
911 if (S_ISDIR(st.st_mode)) {
912 if (!strcmp(dent->d_name, ".") ||
913 !strcmp(dent->d_name, ".."))
916 ret = map_groups__set_modules_path_dir(mg, path);
920 char *dot = strrchr(dent->d_name, '.'),
925 if (dot == NULL || strcmp(dot, ".ko"))
927 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
928 (int)(dot - dent->d_name), dent->d_name);
930 strxfrchar(dso_name, '-', '_');
931 map = map_groups__find_by_name(mg, MAP__FUNCTION,
936 long_name = strdup(path);
937 if (long_name == NULL) {
941 dso__set_long_name(map->dso, long_name);
942 map->dso->lname_alloc = 1;
943 dso__kernel_module_get_build_id(map->dso, "");
952 static char *get_kernel_version(const char *root_dir)
954 char version[PATH_MAX];
957 const char *prefix = "Linux version ";
959 sprintf(version, "%s/proc/version", root_dir);
960 file = fopen(version, "r");
965 tmp = fgets(version, sizeof(version), file);
968 name = strstr(version, prefix);
971 name += strlen(prefix);
972 tmp = strchr(name, ' ');
979 static int machine__set_modules_path(struct machine *machine)
982 char modules_path[PATH_MAX];
984 version = get_kernel_version(machine->root_dir);
988 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s/kernel",
989 machine->root_dir, version);
992 return map_groups__set_modules_path_dir(&machine->kmaps, modules_path);
995 struct map *machine__new_module(struct machine *machine, u64 start,
996 const char *filename)
999 struct dso *dso = __dsos__findnew(&machine->kernel_dsos, filename);
1004 map = map__new2(start, dso, MAP__FUNCTION);
1008 if (machine__is_host(machine))
1009 dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
1011 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
1012 map_groups__insert(&machine->kmaps, map);
1016 static int machine__create_modules(struct machine *machine)
1022 const char *modules;
1023 char path[PATH_MAX];
1025 if (machine__is_default_guest(machine))
1026 modules = symbol_conf.default_guest_modules;
1028 sprintf(path, "%s/proc/modules", machine->root_dir);
1032 if (symbol__restricted_filename(path, "/proc/modules"))
1035 file = fopen(modules, "r");
1039 while (!feof(file)) {
1040 char name[PATH_MAX];
1045 line_len = getline(&line, &n, file);
1052 line[--line_len] = '\0'; /* \n */
1054 sep = strrchr(line, 'x');
1058 hex2u64(sep + 1, &start);
1060 sep = strchr(line, ' ');
1066 snprintf(name, sizeof(name), "[%s]", line);
1067 map = machine__new_module(machine, start, name);
1069 goto out_delete_line;
1070 dso__kernel_module_get_build_id(map->dso, machine->root_dir);
1076 return machine__set_modules_path(machine);
1084 int dso__load_vmlinux(struct dso *dso, struct map *map,
1085 const char *vmlinux, symbol_filter_t filter)
1089 char symfs_vmlinux[PATH_MAX];
1090 enum dso_binary_type symtab_type;
1092 snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s%s",
1093 symbol_conf.symfs, vmlinux);
1095 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1096 symtab_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
1098 symtab_type = DSO_BINARY_TYPE__VMLINUX;
1100 if (symsrc__init(&ss, dso, symfs_vmlinux, symtab_type))
1103 err = dso__load_sym(dso, map, &ss, &ss, filter, 0);
1104 symsrc__destroy(&ss);
1107 dso__set_long_name(dso, (char *)vmlinux);
1108 dso__set_loaded(dso, map->type);
1109 pr_debug("Using %s for symbols\n", symfs_vmlinux);
1115 int dso__load_vmlinux_path(struct dso *dso, struct map *map,
1116 symbol_filter_t filter)
1121 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1122 vmlinux_path__nr_entries + 1);
1124 filename = dso__build_id_filename(dso, NULL, 0);
1125 if (filename != NULL) {
1126 err = dso__load_vmlinux(dso, map, filename, filter);
1132 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
1133 err = dso__load_vmlinux(dso, map, vmlinux_path[i], filter);
1135 dso__set_long_name(dso, strdup(vmlinux_path[i]));
1143 static int dso__load_kernel_sym(struct dso *dso, struct map *map,
1144 symbol_filter_t filter)
1147 const char *kallsyms_filename = NULL;
1148 char *kallsyms_allocated_filename = NULL;
1150 * Step 1: if the user specified a kallsyms or vmlinux filename, use
1151 * it and only it, reporting errors to the user if it cannot be used.
1153 * For instance, try to analyse an ARM perf.data file _without_ a
1154 * build-id, or if the user specifies the wrong path to the right
1155 * vmlinux file, obviously we can't fallback to another vmlinux (a
1156 * x86_86 one, on the machine where analysis is being performed, say),
1157 * or worse, /proc/kallsyms.
1159 * If the specified file _has_ a build-id and there is a build-id
1160 * section in the perf.data file, we will still do the expected
1161 * validation in dso__load_vmlinux and will bail out if they don't
1164 if (symbol_conf.kallsyms_name != NULL) {
1165 kallsyms_filename = symbol_conf.kallsyms_name;
1169 if (symbol_conf.vmlinux_name != NULL) {
1170 err = dso__load_vmlinux(dso, map,
1171 symbol_conf.vmlinux_name, filter);
1173 dso__set_long_name(dso,
1174 strdup(symbol_conf.vmlinux_name));
1180 if (vmlinux_path != NULL) {
1181 err = dso__load_vmlinux_path(dso, map, filter);
1186 /* do not try local files if a symfs was given */
1187 if (symbol_conf.symfs[0] != 0)
1191 * Say the kernel DSO was created when processing the build-id header table,
1192 * we have a build-id, so check if it is the same as the running kernel,
1193 * using it if it is.
1195 if (dso->has_build_id) {
1196 u8 kallsyms_build_id[BUILD_ID_SIZE];
1197 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1199 if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id,
1200 sizeof(kallsyms_build_id)) == 0) {
1201 if (dso__build_id_equal(dso, kallsyms_build_id)) {
1202 kallsyms_filename = "/proc/kallsyms";
1207 * Now look if we have it on the build-id cache in
1208 * $HOME/.debug/[kernel.kallsyms].
1210 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1213 if (asprintf(&kallsyms_allocated_filename,
1214 "%s/.debug/[kernel.kallsyms]/%s",
1215 getenv("HOME"), sbuild_id) == -1) {
1216 pr_err("Not enough memory for kallsyms file lookup\n");
1220 kallsyms_filename = kallsyms_allocated_filename;
1222 if (access(kallsyms_filename, F_OK)) {
1223 pr_err("No kallsyms or vmlinux with build-id %s "
1224 "was found\n", sbuild_id);
1225 free(kallsyms_allocated_filename);
1230 * Last resort, if we don't have a build-id and couldn't find
1231 * any vmlinux file, try the running kernel kallsyms table.
1233 kallsyms_filename = "/proc/kallsyms";
1237 err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
1239 pr_debug("Using %s for symbols\n", kallsyms_filename);
1240 free(kallsyms_allocated_filename);
1243 dso__set_long_name(dso, strdup("[kernel.kallsyms]"));
1245 map__fixup_start(map);
1246 map__fixup_end(map);
1252 static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
1253 symbol_filter_t filter)
1256 const char *kallsyms_filename = NULL;
1257 struct machine *machine;
1258 char path[PATH_MAX];
1261 pr_debug("Guest kernel map hasn't the point to groups\n");
1264 machine = map->groups->machine;
1266 if (machine__is_default_guest(machine)) {
1268 * if the user specified a vmlinux filename, use it and only
1269 * it, reporting errors to the user if it cannot be used.
1270 * Or use file guest_kallsyms inputted by user on commandline
1272 if (symbol_conf.default_guest_vmlinux_name != NULL) {
1273 err = dso__load_vmlinux(dso, map,
1274 symbol_conf.default_guest_vmlinux_name, filter);
1278 kallsyms_filename = symbol_conf.default_guest_kallsyms;
1279 if (!kallsyms_filename)
1282 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
1283 kallsyms_filename = path;
1286 err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
1288 pr_debug("Using %s for symbols\n", kallsyms_filename);
1292 if (kallsyms_filename != NULL) {
1293 machine__mmap_name(machine, path, sizeof(path));
1294 dso__set_long_name(dso, strdup(path));
1296 map__fixup_start(map);
1297 map__fixup_end(map);
1303 size_t machines__fprintf_dsos(struct rb_root *machines, FILE *fp)
1308 for (nd = rb_first(machines); nd; nd = rb_next(nd)) {
1309 struct machine *pos = rb_entry(nd, struct machine, rb_node);
1310 ret += __dsos__fprintf(&pos->kernel_dsos, fp);
1311 ret += __dsos__fprintf(&pos->user_dsos, fp);
1317 size_t machine__fprintf_dsos_buildid(struct machine *machine, FILE *fp,
1318 bool (skip)(struct dso *dso, int parm), int parm)
1320 return __dsos__fprintf_buildid(&machine->kernel_dsos, fp, skip, parm) +
1321 __dsos__fprintf_buildid(&machine->user_dsos, fp, skip, parm);
1324 size_t machines__fprintf_dsos_buildid(struct rb_root *machines, FILE *fp,
1325 bool (skip)(struct dso *dso, int parm), int parm)
1330 for (nd = rb_first(machines); nd; nd = rb_next(nd)) {
1331 struct machine *pos = rb_entry(nd, struct machine, rb_node);
1332 ret += machine__fprintf_dsos_buildid(pos, fp, skip, parm);
1337 static struct dso *machine__get_kernel(struct machine *machine)
1339 const char *vmlinux_name = NULL;
1342 if (machine__is_host(machine)) {
1343 vmlinux_name = symbol_conf.vmlinux_name;
1345 vmlinux_name = "[kernel.kallsyms]";
1347 kernel = dso__kernel_findnew(machine, vmlinux_name,
1353 if (machine__is_default_guest(machine))
1354 vmlinux_name = symbol_conf.default_guest_vmlinux_name;
1356 vmlinux_name = machine__mmap_name(machine, bf,
1359 kernel = dso__kernel_findnew(machine, vmlinux_name,
1361 DSO_TYPE_GUEST_KERNEL);
1364 if (kernel != NULL && (!kernel->has_build_id))
1365 dso__read_running_kernel_build_id(kernel, machine);
1370 struct process_args {
1374 static int symbol__in_kernel(void *arg, const char *name,
1375 char type __maybe_unused, u64 start)
1377 struct process_args *args = arg;
1379 if (strchr(name, '['))
1382 args->start = start;
1386 /* Figure out the start address of kernel map from /proc/kallsyms */
1387 static u64 machine__get_kernel_start_addr(struct machine *machine)
1389 const char *filename;
1390 char path[PATH_MAX];
1391 struct process_args args;
1393 if (machine__is_host(machine)) {
1394 filename = "/proc/kallsyms";
1396 if (machine__is_default_guest(machine))
1397 filename = (char *)symbol_conf.default_guest_kallsyms;
1399 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
1404 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
1407 if (kallsyms__parse(filename, &args, symbol__in_kernel) <= 0)
1413 int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
1416 u64 start = machine__get_kernel_start_addr(machine);
1418 for (type = 0; type < MAP__NR_TYPES; ++type) {
1421 machine->vmlinux_maps[type] = map__new2(start, kernel, type);
1422 if (machine->vmlinux_maps[type] == NULL)
1425 machine->vmlinux_maps[type]->map_ip =
1426 machine->vmlinux_maps[type]->unmap_ip =
1428 kmap = map__kmap(machine->vmlinux_maps[type]);
1429 kmap->kmaps = &machine->kmaps;
1430 map_groups__insert(&machine->kmaps,
1431 machine->vmlinux_maps[type]);
1437 void machine__destroy_kernel_maps(struct machine *machine)
1441 for (type = 0; type < MAP__NR_TYPES; ++type) {
1444 if (machine->vmlinux_maps[type] == NULL)
1447 kmap = map__kmap(machine->vmlinux_maps[type]);
1448 map_groups__remove(&machine->kmaps,
1449 machine->vmlinux_maps[type]);
1450 if (kmap->ref_reloc_sym) {
1452 * ref_reloc_sym is shared among all maps, so free just
1455 if (type == MAP__FUNCTION) {
1456 free((char *)kmap->ref_reloc_sym->name);
1457 kmap->ref_reloc_sym->name = NULL;
1458 free(kmap->ref_reloc_sym);
1460 kmap->ref_reloc_sym = NULL;
1463 map__delete(machine->vmlinux_maps[type]);
1464 machine->vmlinux_maps[type] = NULL;
1468 int machine__create_kernel_maps(struct machine *machine)
1470 struct dso *kernel = machine__get_kernel(machine);
1472 if (kernel == NULL ||
1473 __machine__create_kernel_maps(machine, kernel) < 0)
1476 if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
1477 if (machine__is_host(machine))
1478 pr_debug("Problems creating module maps, "
1479 "continuing anyway...\n");
1481 pr_debug("Problems creating module maps for guest %d, "
1482 "continuing anyway...\n", machine->pid);
1486 * Now that we have all the maps created, just set the ->end of them:
1488 map_groups__fixup_end(&machine->kmaps);
1492 static void vmlinux_path__exit(void)
1494 while (--vmlinux_path__nr_entries >= 0) {
1495 free(vmlinux_path[vmlinux_path__nr_entries]);
1496 vmlinux_path[vmlinux_path__nr_entries] = NULL;
1500 vmlinux_path = NULL;
1503 static int vmlinux_path__init(void)
1508 vmlinux_path = malloc(sizeof(char *) * 5);
1509 if (vmlinux_path == NULL)
1512 vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
1513 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1515 ++vmlinux_path__nr_entries;
1516 vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
1517 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1519 ++vmlinux_path__nr_entries;
1521 /* only try running kernel version if no symfs was given */
1522 if (symbol_conf.symfs[0] != 0)
1525 if (uname(&uts) < 0)
1528 snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
1529 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1530 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1532 ++vmlinux_path__nr_entries;
1533 snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
1534 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1535 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1537 ++vmlinux_path__nr_entries;
1538 snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
1540 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1541 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1543 ++vmlinux_path__nr_entries;
1548 vmlinux_path__exit();
1552 size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
1556 struct dso *kdso = machine->vmlinux_maps[MAP__FUNCTION]->dso;
1558 if (kdso->has_build_id) {
1559 char filename[PATH_MAX];
1560 if (dso__build_id_filename(kdso, filename, sizeof(filename)))
1561 printed += fprintf(fp, "[0] %s\n", filename);
1564 for (i = 0; i < vmlinux_path__nr_entries; ++i)
1565 printed += fprintf(fp, "[%d] %s\n",
1566 i + kdso->has_build_id, vmlinux_path[i]);
1571 static int setup_list(struct strlist **list, const char *list_str,
1572 const char *list_name)
1574 if (list_str == NULL)
1577 *list = strlist__new(true, list_str);
1579 pr_err("problems parsing %s list\n", list_name);
1585 static bool symbol__read_kptr_restrict(void)
1589 if (geteuid() != 0) {
1590 FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
1594 if (fgets(line, sizeof(line), fp) != NULL)
1595 value = atoi(line) != 0;
1604 int symbol__init(void)
1608 if (symbol_conf.initialized)
1611 symbol_conf.priv_size = PERF_ALIGN(symbol_conf.priv_size, sizeof(u64));
1615 if (symbol_conf.sort_by_name)
1616 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
1617 sizeof(struct symbol));
1619 if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
1622 if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
1623 pr_err("'.' is the only non valid --field-separator argument\n");
1627 if (setup_list(&symbol_conf.dso_list,
1628 symbol_conf.dso_list_str, "dso") < 0)
1631 if (setup_list(&symbol_conf.comm_list,
1632 symbol_conf.comm_list_str, "comm") < 0)
1633 goto out_free_dso_list;
1635 if (setup_list(&symbol_conf.sym_list,
1636 symbol_conf.sym_list_str, "symbol") < 0)
1637 goto out_free_comm_list;
1640 * A path to symbols of "/" is identical to ""
1641 * reset here for simplicity.
1643 symfs = realpath(symbol_conf.symfs, NULL);
1645 symfs = symbol_conf.symfs;
1646 if (strcmp(symfs, "/") == 0)
1647 symbol_conf.symfs = "";
1648 if (symfs != symbol_conf.symfs)
1649 free((void *)symfs);
1651 symbol_conf.kptr_restrict = symbol__read_kptr_restrict();
1653 symbol_conf.initialized = true;
1657 strlist__delete(symbol_conf.comm_list);
1659 strlist__delete(symbol_conf.dso_list);
1663 void symbol__exit(void)
1665 if (!symbol_conf.initialized)
1667 strlist__delete(symbol_conf.sym_list);
1668 strlist__delete(symbol_conf.dso_list);
1669 strlist__delete(symbol_conf.comm_list);
1670 vmlinux_path__exit();
1671 symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
1672 symbol_conf.initialized = false;
1675 int machines__create_kernel_maps(struct rb_root *machines, pid_t pid)
1677 struct machine *machine = machines__findnew(machines, pid);
1679 if (machine == NULL)
1682 return machine__create_kernel_maps(machine);
1685 int machines__create_guest_kernel_maps(struct rb_root *machines)
1688 struct dirent **namelist = NULL;
1690 char path[PATH_MAX];
1694 if (symbol_conf.default_guest_vmlinux_name ||
1695 symbol_conf.default_guest_modules ||
1696 symbol_conf.default_guest_kallsyms) {
1697 machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
1700 if (symbol_conf.guestmount) {
1701 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
1704 for (i = 0; i < items; i++) {
1705 if (!isdigit(namelist[i]->d_name[0])) {
1706 /* Filter out . and .. */
1709 pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10);
1710 if ((*endp != '\0') ||
1711 (endp == namelist[i]->d_name) ||
1712 (errno == ERANGE)) {
1713 pr_debug("invalid directory (%s). Skipping.\n",
1714 namelist[i]->d_name);
1717 sprintf(path, "%s/%s/proc/kallsyms",
1718 symbol_conf.guestmount,
1719 namelist[i]->d_name);
1720 ret = access(path, R_OK);
1722 pr_debug("Can't access file %s\n", path);
1725 machines__create_kernel_maps(machines, pid);
1734 void machines__destroy_guest_kernel_maps(struct rb_root *machines)
1736 struct rb_node *next = rb_first(machines);
1739 struct machine *pos = rb_entry(next, struct machine, rb_node);
1741 next = rb_next(&pos->rb_node);
1742 rb_erase(&pos->rb_node, machines);
1743 machine__delete(pos);
1747 int machine__load_kallsyms(struct machine *machine, const char *filename,
1748 enum map_type type, symbol_filter_t filter)
1750 struct map *map = machine->vmlinux_maps[type];
1751 int ret = dso__load_kallsyms(map->dso, filename, map, filter);
1754 dso__set_loaded(map->dso, type);
1756 * Since /proc/kallsyms will have multiple sessions for the
1757 * kernel, with modules between them, fixup the end of all
1760 __map_groups__fixup_end(&machine->kmaps, type);
1766 int machine__load_vmlinux_path(struct machine *machine, enum map_type type,
1767 symbol_filter_t filter)
1769 struct map *map = machine->vmlinux_maps[type];
1770 int ret = dso__load_vmlinux_path(map->dso, map, filter);
1773 dso__set_loaded(map->dso, type);
1774 map__reloc_vmlinux(map);