20 #include <sys/utsname.h>
23 #define KSYM_NAME_LEN 256
26 static void dso_cache__free(struct rb_root *root);
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 static enum dso_binary_type binary_type_data[] = {
60 DSO_BINARY_TYPE__BUILD_ID_CACHE,
61 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
62 DSO_BINARY_TYPE__NOT_FOUND,
65 #define DSO_BINARY_TYPE__DATA_CNT ARRAY_SIZE(binary_type_data)
67 int dso__name_len(const struct dso *dso)
70 return strlen("[unknown]");
72 return dso->long_name_len;
74 return dso->short_name_len;
77 bool dso__loaded(const struct dso *dso, enum map_type type)
79 return dso->loaded & (1 << type);
82 bool dso__sorted_by_name(const struct dso *dso, enum map_type type)
84 return dso->sorted_by_name & (1 << type);
87 static void dso__set_sorted_by_name(struct dso *dso, enum map_type type)
89 dso->sorted_by_name |= (1 << type);
92 bool symbol_type__is_a(char symbol_type, enum map_type map_type)
94 symbol_type = toupper(symbol_type);
98 return symbol_type == 'T' || symbol_type == 'W';
100 return symbol_type == 'D';
106 static int prefix_underscores_count(const char *str)
108 const char *tail = str;
119 static int choose_best_symbol(struct symbol *syma, struct symbol *symb)
124 /* Prefer a symbol with non zero length */
125 a = syma->end - syma->start;
126 b = symb->end - symb->start;
127 if ((b == 0) && (a > 0))
129 else if ((a == 0) && (b > 0))
132 /* Prefer a non weak symbol over a weak one */
133 a = syma->binding == STB_WEAK;
134 b = symb->binding == STB_WEAK;
140 /* Prefer a global symbol over a non global one */
141 a = syma->binding == STB_GLOBAL;
142 b = symb->binding == STB_GLOBAL;
148 /* Prefer a symbol with less underscores */
149 a = prefix_underscores_count(syma->name);
150 b = prefix_underscores_count(symb->name);
156 /* If all else fails, choose the symbol with the longest name */
157 if (strlen(syma->name) >= strlen(symb->name))
163 void symbols__fixup_duplicate(struct rb_root *symbols)
166 struct symbol *curr, *next;
168 nd = rb_first(symbols);
171 curr = rb_entry(nd, struct symbol, rb_node);
173 nd = rb_next(&curr->rb_node);
174 next = rb_entry(nd, struct symbol, rb_node);
179 if (curr->start != next->start)
182 if (choose_best_symbol(curr, next) == SYMBOL_A) {
183 rb_erase(&next->rb_node, symbols);
186 nd = rb_next(&curr->rb_node);
187 rb_erase(&curr->rb_node, symbols);
192 void symbols__fixup_end(struct rb_root *symbols)
194 struct rb_node *nd, *prevnd = rb_first(symbols);
195 struct symbol *curr, *prev;
200 curr = rb_entry(prevnd, struct symbol, rb_node);
202 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
204 curr = rb_entry(nd, struct symbol, rb_node);
206 if (prev->end == prev->start && prev->end != curr->start)
207 prev->end = curr->start - 1;
211 if (curr->end == curr->start)
212 curr->end = roundup(curr->start, 4096);
215 void __map_groups__fixup_end(struct map_groups *mg, enum map_type type)
217 struct map *prev, *curr;
218 struct rb_node *nd, *prevnd = rb_first(&mg->maps[type]);
223 curr = rb_entry(prevnd, struct map, rb_node);
225 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
227 curr = rb_entry(nd, struct map, rb_node);
228 prev->end = curr->start - 1;
232 * We still haven't the actual symbols, so guess the
233 * last map final address.
238 static void map_groups__fixup_end(struct map_groups *mg)
241 for (i = 0; i < MAP__NR_TYPES; ++i)
242 __map_groups__fixup_end(mg, i);
245 struct symbol *symbol__new(u64 start, u64 len, u8 binding, const char *name)
247 size_t namelen = strlen(name) + 1;
248 struct symbol *sym = calloc(1, (symbol_conf.priv_size +
249 sizeof(*sym) + namelen));
253 if (symbol_conf.priv_size)
254 sym = ((void *)sym) + symbol_conf.priv_size;
257 sym->end = len ? start + len - 1 : start;
258 sym->binding = binding;
259 sym->namelen = namelen - 1;
261 pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n",
262 __func__, name, start, sym->end);
263 memcpy(sym->name, name, namelen);
268 void symbol__delete(struct symbol *sym)
270 free(((void *)sym) - symbol_conf.priv_size);
273 static size_t symbol__fprintf(struct symbol *sym, FILE *fp)
275 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %c %s\n",
276 sym->start, sym->end,
277 sym->binding == STB_GLOBAL ? 'g' :
278 sym->binding == STB_LOCAL ? 'l' : 'w',
282 size_t symbol__fprintf_symname_offs(const struct symbol *sym,
283 const struct addr_location *al, FILE *fp)
285 unsigned long offset;
288 if (sym && sym->name) {
289 length = fprintf(fp, "%s", sym->name);
291 offset = al->addr - sym->start;
292 length += fprintf(fp, "+0x%lx", offset);
296 return fprintf(fp, "[unknown]");
299 size_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp)
301 return symbol__fprintf_symname_offs(sym, NULL, fp);
304 void dso__set_long_name(struct dso *dso, char *name)
308 dso->long_name = name;
309 dso->long_name_len = strlen(name);
312 static void dso__set_short_name(struct dso *dso, const char *name)
316 dso->short_name = name;
317 dso->short_name_len = strlen(name);
320 static void dso__set_basename(struct dso *dso)
322 dso__set_short_name(dso, basename(dso->long_name));
325 struct dso *dso__new(const char *name)
327 struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
331 strcpy(dso->name, name);
332 dso__set_long_name(dso, dso->name);
333 dso__set_short_name(dso, dso->name);
334 for (i = 0; i < MAP__NR_TYPES; ++i)
335 dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
336 dso->cache = RB_ROOT;
337 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
338 dso->data_type = DSO_BINARY_TYPE__NOT_FOUND;
340 dso->sorted_by_name = 0;
341 dso->has_build_id = 0;
342 dso->kernel = DSO_TYPE_USER;
343 dso->needs_swap = DSO_SWAP__UNSET;
344 INIT_LIST_HEAD(&dso->node);
350 static void symbols__delete(struct rb_root *symbols)
353 struct rb_node *next = rb_first(symbols);
356 pos = rb_entry(next, struct symbol, rb_node);
357 next = rb_next(&pos->rb_node);
358 rb_erase(&pos->rb_node, symbols);
363 void dso__delete(struct dso *dso)
366 for (i = 0; i < MAP__NR_TYPES; ++i)
367 symbols__delete(&dso->symbols[i]);
368 if (dso->sname_alloc)
369 free((char *)dso->short_name);
370 if (dso->lname_alloc)
371 free(dso->long_name);
372 dso_cache__free(&dso->cache);
376 void dso__set_build_id(struct dso *dso, void *build_id)
378 memcpy(dso->build_id, build_id, sizeof(dso->build_id));
379 dso->has_build_id = 1;
382 void symbols__insert(struct rb_root *symbols, struct symbol *sym)
384 struct rb_node **p = &symbols->rb_node;
385 struct rb_node *parent = NULL;
386 const u64 ip = sym->start;
391 s = rb_entry(parent, struct symbol, rb_node);
397 rb_link_node(&sym->rb_node, parent, p);
398 rb_insert_color(&sym->rb_node, symbols);
401 static struct symbol *symbols__find(struct rb_root *symbols, u64 ip)
408 n = symbols->rb_node;
411 struct symbol *s = rb_entry(n, struct symbol, rb_node);
415 else if (ip > s->end)
424 struct symbol_name_rb_node {
425 struct rb_node rb_node;
429 static void symbols__insert_by_name(struct rb_root *symbols, struct symbol *sym)
431 struct rb_node **p = &symbols->rb_node;
432 struct rb_node *parent = NULL;
433 struct symbol_name_rb_node *symn, *s;
435 symn = container_of(sym, struct symbol_name_rb_node, sym);
439 s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
440 if (strcmp(sym->name, s->sym.name) < 0)
445 rb_link_node(&symn->rb_node, parent, p);
446 rb_insert_color(&symn->rb_node, symbols);
449 static void symbols__sort_by_name(struct rb_root *symbols,
450 struct rb_root *source)
454 for (nd = rb_first(source); nd; nd = rb_next(nd)) {
455 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
456 symbols__insert_by_name(symbols, pos);
460 static struct symbol *symbols__find_by_name(struct rb_root *symbols,
468 n = symbols->rb_node;
471 struct symbol_name_rb_node *s;
474 s = rb_entry(n, struct symbol_name_rb_node, rb_node);
475 cmp = strcmp(name, s->sym.name);
488 struct symbol *dso__find_symbol(struct dso *dso,
489 enum map_type type, u64 addr)
491 return symbols__find(&dso->symbols[type], addr);
494 struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
497 return symbols__find_by_name(&dso->symbol_names[type], name);
500 void dso__sort_by_name(struct dso *dso, enum map_type type)
502 dso__set_sorted_by_name(dso, type);
503 return symbols__sort_by_name(&dso->symbol_names[type],
504 &dso->symbols[type]);
507 int build_id__sprintf(const u8 *build_id, int len, char *bf)
510 const u8 *raw = build_id;
513 for (i = 0; i < len; ++i) {
514 sprintf(bid, "%02x", *raw);
519 return raw - build_id;
522 size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
524 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
526 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
527 return fprintf(fp, "%s", sbuild_id);
530 size_t dso__fprintf_symbols_by_name(struct dso *dso,
531 enum map_type type, FILE *fp)
535 struct symbol_name_rb_node *pos;
537 for (nd = rb_first(&dso->symbol_names[type]); nd; nd = rb_next(nd)) {
538 pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
539 fprintf(fp, "%s\n", pos->sym.name);
545 size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
548 size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
550 if (dso->short_name != dso->long_name)
551 ret += fprintf(fp, "%s, ", dso->long_name);
552 ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
553 dso->loaded ? "" : "NOT ");
554 ret += dso__fprintf_buildid(dso, fp);
555 ret += fprintf(fp, ")\n");
556 for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) {
557 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
558 ret += symbol__fprintf(pos, fp);
564 int kallsyms__parse(const char *filename, void *arg,
565 int (*process_symbol)(void *arg, const char *name,
566 char type, u64 start))
571 FILE *file = fopen(filename, "r");
578 while (!feof(file)) {
584 line_len = getline(&line, &n, file);
585 if (line_len < 0 || !line)
588 line[--line_len] = '\0'; /* \n */
590 len = hex2u64(line, &start);
593 if (len + 2 >= line_len)
596 symbol_type = line[len];
598 symbol_name = line + len;
599 len = line_len - len;
601 if (len >= KSYM_NAME_LEN) {
606 err = process_symbol(arg, symbol_name,
620 struct process_kallsyms_args {
625 static u8 kallsyms2elf_type(char type)
630 return isupper(type) ? STB_GLOBAL : STB_LOCAL;
633 static int map__process_kallsym_symbol(void *arg, const char *name,
634 char type, u64 start)
637 struct process_kallsyms_args *a = arg;
638 struct rb_root *root = &a->dso->symbols[a->map->type];
640 if (!symbol_type__is_a(type, a->map->type))
644 * module symbols are not sorted so we add all
645 * symbols, setting length to 0, and rely on
646 * symbols__fixup_end() to fix it up.
648 sym = symbol__new(start, 0, kallsyms2elf_type(type), name);
652 * We will pass the symbols to the filter later, in
653 * map__split_kallsyms, when we have split the maps per module
655 symbols__insert(root, sym);
661 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
662 * so that we can in the next step set the symbol ->end address and then
663 * call kernel_maps__split_kallsyms.
665 static int dso__load_all_kallsyms(struct dso *dso, const char *filename,
668 struct process_kallsyms_args args = { .map = map, .dso = dso, };
669 return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
673 * Split the symbols into maps, making sure there are no overlaps, i.e. the
674 * kernel range is broken in several maps, named [kernel].N, as we don't have
675 * the original ELF section names vmlinux have.
677 static int dso__split_kallsyms(struct dso *dso, struct map *map,
678 symbol_filter_t filter)
680 struct map_groups *kmaps = map__kmap(map)->kmaps;
681 struct machine *machine = kmaps->machine;
682 struct map *curr_map = map;
684 int count = 0, moved = 0;
685 struct rb_root *root = &dso->symbols[map->type];
686 struct rb_node *next = rb_first(root);
687 int kernel_range = 0;
692 pos = rb_entry(next, struct symbol, rb_node);
693 next = rb_next(&pos->rb_node);
695 module = strchr(pos->name, '\t');
697 if (!symbol_conf.use_modules)
702 if (strcmp(curr_map->dso->short_name, module)) {
703 if (curr_map != map &&
704 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
705 machine__is_default_guest(machine)) {
707 * We assume all symbols of a module are
708 * continuous in * kallsyms, so curr_map
709 * points to a module and all its
710 * symbols are in its kmap. Mark it as
713 dso__set_loaded(curr_map->dso,
717 curr_map = map_groups__find_by_name(kmaps,
719 if (curr_map == NULL) {
720 pr_debug("%s/proc/{kallsyms,modules} "
721 "inconsistency while looking "
722 "for \"%s\" module!\n",
723 machine->root_dir, module);
728 if (curr_map->dso->loaded &&
729 !machine__is_default_guest(machine))
733 * So that we look just like we get from .ko files,
734 * i.e. not prelinked, relative to map->start.
736 pos->start = curr_map->map_ip(curr_map, pos->start);
737 pos->end = curr_map->map_ip(curr_map, pos->end);
738 } else if (curr_map != map) {
739 char dso_name[PATH_MAX];
747 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
748 snprintf(dso_name, sizeof(dso_name),
752 snprintf(dso_name, sizeof(dso_name),
756 ndso = dso__new(dso_name);
760 ndso->kernel = dso->kernel;
762 curr_map = map__new2(pos->start, ndso, map->type);
763 if (curr_map == NULL) {
768 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
769 map_groups__insert(kmaps, curr_map);
773 if (filter && filter(curr_map, pos)) {
774 discard_symbol: rb_erase(&pos->rb_node, root);
777 if (curr_map != map) {
778 rb_erase(&pos->rb_node, root);
779 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
786 if (curr_map != map &&
787 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
788 machine__is_default_guest(kmaps->machine)) {
789 dso__set_loaded(curr_map->dso, curr_map->type);
792 return count + moved;
795 static bool symbol__restricted_filename(const char *filename,
796 const char *restricted_filename)
798 bool restricted = false;
800 if (symbol_conf.kptr_restrict) {
801 char *r = realpath(filename, NULL);
804 restricted = strcmp(r, restricted_filename) == 0;
813 int dso__load_kallsyms(struct dso *dso, const char *filename,
814 struct map *map, symbol_filter_t filter)
816 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
819 if (dso__load_all_kallsyms(dso, filename, map) < 0)
822 symbols__fixup_duplicate(&dso->symbols[map->type]);
823 symbols__fixup_end(&dso->symbols[map->type]);
825 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
826 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
828 dso->symtab_type = DSO_BINARY_TYPE__KALLSYMS;
830 return dso__split_kallsyms(dso, map, filter);
833 static int dso__load_perf_map(struct dso *dso, struct map *map,
834 symbol_filter_t filter)
841 file = fopen(dso->long_name, "r");
845 while (!feof(file)) {
850 line_len = getline(&line, &n, file);
857 line[--line_len] = '\0'; /* \n */
859 len = hex2u64(line, &start);
862 if (len + 2 >= line_len)
865 len += hex2u64(line + len, &size);
868 if (len + 2 >= line_len)
871 sym = symbol__new(start, size, STB_GLOBAL, line + len);
874 goto out_delete_line;
876 if (filter && filter(map, sym))
879 symbols__insert(&dso->symbols[map->type], sym);
895 bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
897 return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
900 bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
902 bool have_build_id = false;
905 list_for_each_entry(pos, head, node) {
906 if (with_hits && !pos->hit)
908 if (pos->has_build_id) {
909 have_build_id = true;
912 if (filename__read_build_id(pos->long_name, pos->build_id,
913 sizeof(pos->build_id)) > 0) {
914 have_build_id = true;
915 pos->has_build_id = true;
919 return have_build_id;
922 char dso__symtab_origin(const struct dso *dso)
924 static const char origin[] = {
925 [DSO_BINARY_TYPE__KALLSYMS] = 'k',
926 [DSO_BINARY_TYPE__VMLINUX] = 'v',
927 [DSO_BINARY_TYPE__JAVA_JIT] = 'j',
928 [DSO_BINARY_TYPE__DEBUGLINK] = 'l',
929 [DSO_BINARY_TYPE__BUILD_ID_CACHE] = 'B',
930 [DSO_BINARY_TYPE__FEDORA_DEBUGINFO] = 'f',
931 [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO] = 'u',
932 [DSO_BINARY_TYPE__BUILDID_DEBUGINFO] = 'b',
933 [DSO_BINARY_TYPE__SYSTEM_PATH_DSO] = 'd',
934 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE] = 'K',
935 [DSO_BINARY_TYPE__GUEST_KALLSYMS] = 'g',
936 [DSO_BINARY_TYPE__GUEST_KMODULE] = 'G',
937 [DSO_BINARY_TYPE__GUEST_VMLINUX] = 'V',
940 if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND)
942 return origin[dso->symtab_type];
945 int dso__binary_type_file(struct dso *dso, enum dso_binary_type type,
946 char *root_dir, char *file, size_t size)
948 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
952 case DSO_BINARY_TYPE__DEBUGLINK: {
955 strncpy(file, dso->long_name, size);
956 debuglink = file + dso->long_name_len;
957 while (debuglink != file && *debuglink != '/')
959 if (*debuglink == '/')
961 filename__read_debuglink(dso->long_name, debuglink,
962 size - (debuglink - file));
965 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
966 /* skip the locally configured cache if a symfs is given */
967 if (symbol_conf.symfs[0] ||
968 (dso__build_id_filename(dso, file, size) == NULL))
972 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
973 snprintf(file, size, "%s/usr/lib/debug%s.debug",
974 symbol_conf.symfs, dso->long_name);
977 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
978 snprintf(file, size, "%s/usr/lib/debug%s",
979 symbol_conf.symfs, dso->long_name);
982 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
983 if (!dso->has_build_id) {
988 build_id__sprintf(dso->build_id,
989 sizeof(dso->build_id),
992 "%s/usr/lib/debug/.build-id/%.2s/%s.debug",
993 symbol_conf.symfs, build_id_hex, build_id_hex + 2);
996 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
997 snprintf(file, size, "%s%s",
998 symbol_conf.symfs, dso->long_name);
1001 case DSO_BINARY_TYPE__GUEST_KMODULE:
1002 snprintf(file, size, "%s%s%s", symbol_conf.symfs,
1003 root_dir, dso->long_name);
1006 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
1007 snprintf(file, size, "%s%s", symbol_conf.symfs,
1012 case DSO_BINARY_TYPE__KALLSYMS:
1013 case DSO_BINARY_TYPE__VMLINUX:
1014 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
1015 case DSO_BINARY_TYPE__GUEST_VMLINUX:
1016 case DSO_BINARY_TYPE__JAVA_JIT:
1017 case DSO_BINARY_TYPE__NOT_FOUND:
1025 int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter)
1030 struct machine *machine;
1031 char *root_dir = (char *) "";
1033 struct symsrc ss_[2];
1034 struct symsrc *syms_ss = NULL, *runtime_ss = NULL;
1036 dso__set_loaded(dso, map->type);
1038 if (dso->kernel == DSO_TYPE_KERNEL)
1039 return dso__load_kernel_sym(dso, map, filter);
1040 else if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1041 return dso__load_guest_kernel_sym(dso, map, filter);
1043 if (map->groups && map->groups->machine)
1044 machine = map->groups->machine;
1048 name = malloc(PATH_MAX);
1052 dso->adjust_symbols = 0;
1054 if (strncmp(dso->name, "/tmp/perf-", 10) == 0) {
1057 if (lstat(dso->name, &st) < 0)
1060 if (st.st_uid && (st.st_uid != geteuid())) {
1061 pr_warning("File %s not owned by current user or root, "
1062 "ignoring it.\n", dso->name);
1066 ret = dso__load_perf_map(dso, map, filter);
1067 dso->symtab_type = ret > 0 ? DSO_BINARY_TYPE__JAVA_JIT :
1068 DSO_BINARY_TYPE__NOT_FOUND;
1073 root_dir = machine->root_dir;
1075 /* Iterate over candidate debug images.
1076 * Keep track of "interesting" ones (those which have a symtab, dynsym,
1077 * and/or opd section) for processing.
1079 for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) {
1080 struct symsrc *ss = &ss_[ss_pos];
1081 bool next_slot = false;
1083 enum dso_binary_type symtab_type = binary_type_symtab[i];
1085 if (dso__binary_type_file(dso, symtab_type,
1086 root_dir, name, PATH_MAX))
1089 /* Name is now the name of the next image to try */
1090 if (symsrc__init(ss, dso, name, symtab_type) < 0)
1093 if (!syms_ss && symsrc__has_symtab(ss)) {
1098 if (!runtime_ss && symsrc__possibly_runtime(ss)) {
1106 if (syms_ss && runtime_ss)
1112 if (!runtime_ss && !syms_ss)
1115 if (runtime_ss && !syms_ss) {
1116 syms_ss = runtime_ss;
1119 /* We'll have to hope for the best */
1120 if (!runtime_ss && syms_ss)
1121 runtime_ss = syms_ss;
1124 ret = dso__load_sym(dso, map, syms_ss, runtime_ss, filter, 0);
1131 nr_plt = dso__synthesize_plt_symbols(dso, runtime_ss, map, filter);
1136 for (; ss_pos > 0; ss_pos--)
1137 symsrc__destroy(&ss_[ss_pos - 1]);
1140 if (ret < 0 && strstr(dso->name, " (deleted)") != NULL)
1145 struct map *map_groups__find_by_name(struct map_groups *mg,
1146 enum map_type type, const char *name)
1150 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
1151 struct map *map = rb_entry(nd, struct map, rb_node);
1153 if (map->dso && strcmp(map->dso->short_name, name) == 0)
1160 static int dso__kernel_module_get_build_id(struct dso *dso,
1161 const char *root_dir)
1163 char filename[PATH_MAX];
1165 * kernel module short names are of the form "[module]" and
1166 * we need just "module" here.
1168 const char *name = dso->short_name + 1;
1170 snprintf(filename, sizeof(filename),
1171 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1172 root_dir, (int)strlen(name) - 1, name);
1174 if (sysfs__read_build_id(filename, dso->build_id,
1175 sizeof(dso->build_id)) == 0)
1176 dso->has_build_id = true;
1181 static int map_groups__set_modules_path_dir(struct map_groups *mg,
1182 const char *dir_name)
1184 struct dirent *dent;
1185 DIR *dir = opendir(dir_name);
1189 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
1193 while ((dent = readdir(dir)) != NULL) {
1194 char path[PATH_MAX];
1197 /*sshfs might return bad dent->d_type, so we have to stat*/
1198 snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name);
1199 if (stat(path, &st))
1202 if (S_ISDIR(st.st_mode)) {
1203 if (!strcmp(dent->d_name, ".") ||
1204 !strcmp(dent->d_name, ".."))
1207 ret = map_groups__set_modules_path_dir(mg, path);
1211 char *dot = strrchr(dent->d_name, '.'),
1216 if (dot == NULL || strcmp(dot, ".ko"))
1218 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1219 (int)(dot - dent->d_name), dent->d_name);
1221 strxfrchar(dso_name, '-', '_');
1222 map = map_groups__find_by_name(mg, MAP__FUNCTION,
1227 long_name = strdup(path);
1228 if (long_name == NULL) {
1232 dso__set_long_name(map->dso, long_name);
1233 map->dso->lname_alloc = 1;
1234 dso__kernel_module_get_build_id(map->dso, "");
1243 static char *get_kernel_version(const char *root_dir)
1245 char version[PATH_MAX];
1248 const char *prefix = "Linux version ";
1250 sprintf(version, "%s/proc/version", root_dir);
1251 file = fopen(version, "r");
1256 tmp = fgets(version, sizeof(version), file);
1259 name = strstr(version, prefix);
1262 name += strlen(prefix);
1263 tmp = strchr(name, ' ');
1267 return strdup(name);
1270 static int machine__set_modules_path(struct machine *machine)
1273 char modules_path[PATH_MAX];
1275 version = get_kernel_version(machine->root_dir);
1279 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s/kernel",
1280 machine->root_dir, version);
1283 return map_groups__set_modules_path_dir(&machine->kmaps, modules_path);
1286 struct map *machine__new_module(struct machine *machine, u64 start,
1287 const char *filename)
1290 struct dso *dso = __dsos__findnew(&machine->kernel_dsos, filename);
1295 map = map__new2(start, dso, MAP__FUNCTION);
1299 if (machine__is_host(machine))
1300 dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
1302 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
1303 map_groups__insert(&machine->kmaps, map);
1307 static int machine__create_modules(struct machine *machine)
1313 const char *modules;
1314 char path[PATH_MAX];
1316 if (machine__is_default_guest(machine))
1317 modules = symbol_conf.default_guest_modules;
1319 sprintf(path, "%s/proc/modules", machine->root_dir);
1323 if (symbol__restricted_filename(path, "/proc/modules"))
1326 file = fopen(modules, "r");
1330 while (!feof(file)) {
1331 char name[PATH_MAX];
1336 line_len = getline(&line, &n, file);
1343 line[--line_len] = '\0'; /* \n */
1345 sep = strrchr(line, 'x');
1349 hex2u64(sep + 1, &start);
1351 sep = strchr(line, ' ');
1357 snprintf(name, sizeof(name), "[%s]", line);
1358 map = machine__new_module(machine, start, name);
1360 goto out_delete_line;
1361 dso__kernel_module_get_build_id(map->dso, machine->root_dir);
1367 return machine__set_modules_path(machine);
1375 int dso__load_vmlinux(struct dso *dso, struct map *map,
1376 const char *vmlinux, symbol_filter_t filter)
1380 char symfs_vmlinux[PATH_MAX];
1381 enum dso_binary_type symtab_type;
1383 snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s%s",
1384 symbol_conf.symfs, vmlinux);
1386 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1387 symtab_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
1389 symtab_type = DSO_BINARY_TYPE__VMLINUX;
1391 if (symsrc__init(&ss, dso, symfs_vmlinux, symtab_type))
1394 err = dso__load_sym(dso, map, &ss, &ss, filter, 0);
1395 symsrc__destroy(&ss);
1398 dso__set_long_name(dso, (char *)vmlinux);
1399 dso__set_loaded(dso, map->type);
1400 pr_debug("Using %s for symbols\n", symfs_vmlinux);
1406 int dso__load_vmlinux_path(struct dso *dso, struct map *map,
1407 symbol_filter_t filter)
1412 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1413 vmlinux_path__nr_entries + 1);
1415 filename = dso__build_id_filename(dso, NULL, 0);
1416 if (filename != NULL) {
1417 err = dso__load_vmlinux(dso, map, filename, filter);
1423 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
1424 err = dso__load_vmlinux(dso, map, vmlinux_path[i], filter);
1426 dso__set_long_name(dso, strdup(vmlinux_path[i]));
1434 static int dso__load_kernel_sym(struct dso *dso, struct map *map,
1435 symbol_filter_t filter)
1438 const char *kallsyms_filename = NULL;
1439 char *kallsyms_allocated_filename = NULL;
1441 * Step 1: if the user specified a kallsyms or vmlinux filename, use
1442 * it and only it, reporting errors to the user if it cannot be used.
1444 * For instance, try to analyse an ARM perf.data file _without_ a
1445 * build-id, or if the user specifies the wrong path to the right
1446 * vmlinux file, obviously we can't fallback to another vmlinux (a
1447 * x86_86 one, on the machine where analysis is being performed, say),
1448 * or worse, /proc/kallsyms.
1450 * If the specified file _has_ a build-id and there is a build-id
1451 * section in the perf.data file, we will still do the expected
1452 * validation in dso__load_vmlinux and will bail out if they don't
1455 if (symbol_conf.kallsyms_name != NULL) {
1456 kallsyms_filename = symbol_conf.kallsyms_name;
1460 if (symbol_conf.vmlinux_name != NULL) {
1461 err = dso__load_vmlinux(dso, map,
1462 symbol_conf.vmlinux_name, filter);
1464 dso__set_long_name(dso,
1465 strdup(symbol_conf.vmlinux_name));
1471 if (vmlinux_path != NULL) {
1472 err = dso__load_vmlinux_path(dso, map, filter);
1477 /* do not try local files if a symfs was given */
1478 if (symbol_conf.symfs[0] != 0)
1482 * Say the kernel DSO was created when processing the build-id header table,
1483 * we have a build-id, so check if it is the same as the running kernel,
1484 * using it if it is.
1486 if (dso->has_build_id) {
1487 u8 kallsyms_build_id[BUILD_ID_SIZE];
1488 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1490 if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id,
1491 sizeof(kallsyms_build_id)) == 0) {
1492 if (dso__build_id_equal(dso, kallsyms_build_id)) {
1493 kallsyms_filename = "/proc/kallsyms";
1498 * Now look if we have it on the build-id cache in
1499 * $HOME/.debug/[kernel.kallsyms].
1501 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1504 if (asprintf(&kallsyms_allocated_filename,
1505 "%s/.debug/[kernel.kallsyms]/%s",
1506 getenv("HOME"), sbuild_id) == -1) {
1507 pr_err("Not enough memory for kallsyms file lookup\n");
1511 kallsyms_filename = kallsyms_allocated_filename;
1513 if (access(kallsyms_filename, F_OK)) {
1514 pr_err("No kallsyms or vmlinux with build-id %s "
1515 "was found\n", sbuild_id);
1516 free(kallsyms_allocated_filename);
1521 * Last resort, if we don't have a build-id and couldn't find
1522 * any vmlinux file, try the running kernel kallsyms table.
1524 kallsyms_filename = "/proc/kallsyms";
1528 err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
1530 pr_debug("Using %s for symbols\n", kallsyms_filename);
1531 free(kallsyms_allocated_filename);
1534 dso__set_long_name(dso, strdup("[kernel.kallsyms]"));
1536 map__fixup_start(map);
1537 map__fixup_end(map);
1543 static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
1544 symbol_filter_t filter)
1547 const char *kallsyms_filename = NULL;
1548 struct machine *machine;
1549 char path[PATH_MAX];
1552 pr_debug("Guest kernel map hasn't the point to groups\n");
1555 machine = map->groups->machine;
1557 if (machine__is_default_guest(machine)) {
1559 * if the user specified a vmlinux filename, use it and only
1560 * it, reporting errors to the user if it cannot be used.
1561 * Or use file guest_kallsyms inputted by user on commandline
1563 if (symbol_conf.default_guest_vmlinux_name != NULL) {
1564 err = dso__load_vmlinux(dso, map,
1565 symbol_conf.default_guest_vmlinux_name, filter);
1569 kallsyms_filename = symbol_conf.default_guest_kallsyms;
1570 if (!kallsyms_filename)
1573 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
1574 kallsyms_filename = path;
1577 err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
1579 pr_debug("Using %s for symbols\n", kallsyms_filename);
1583 if (kallsyms_filename != NULL) {
1584 machine__mmap_name(machine, path, sizeof(path));
1585 dso__set_long_name(dso, strdup(path));
1587 map__fixup_start(map);
1588 map__fixup_end(map);
1594 void dsos__add(struct list_head *head, struct dso *dso)
1596 list_add_tail(&dso->node, head);
1599 struct dso *dsos__find(struct list_head *head, const char *name)
1603 list_for_each_entry(pos, head, node)
1604 if (strcmp(pos->long_name, name) == 0)
1609 struct dso *__dsos__findnew(struct list_head *head, const char *name)
1611 struct dso *dso = dsos__find(head, name);
1614 dso = dso__new(name);
1616 dsos__add(head, dso);
1617 dso__set_basename(dso);
1624 size_t __dsos__fprintf(struct list_head *head, FILE *fp)
1629 list_for_each_entry(pos, head, node) {
1631 for (i = 0; i < MAP__NR_TYPES; ++i)
1632 ret += dso__fprintf(pos, i, fp);
1638 size_t machines__fprintf_dsos(struct rb_root *machines, FILE *fp)
1643 for (nd = rb_first(machines); nd; nd = rb_next(nd)) {
1644 struct machine *pos = rb_entry(nd, struct machine, rb_node);
1645 ret += __dsos__fprintf(&pos->kernel_dsos, fp);
1646 ret += __dsos__fprintf(&pos->user_dsos, fp);
1652 static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
1658 list_for_each_entry(pos, head, node) {
1659 if (with_hits && !pos->hit)
1661 ret += dso__fprintf_buildid(pos, fp);
1662 ret += fprintf(fp, " %s\n", pos->long_name);
1667 size_t machine__fprintf_dsos_buildid(struct machine *machine, FILE *fp,
1670 return __dsos__fprintf_buildid(&machine->kernel_dsos, fp, with_hits) +
1671 __dsos__fprintf_buildid(&machine->user_dsos, fp, with_hits);
1674 size_t machines__fprintf_dsos_buildid(struct rb_root *machines,
1675 FILE *fp, bool with_hits)
1680 for (nd = rb_first(machines); nd; nd = rb_next(nd)) {
1681 struct machine *pos = rb_entry(nd, struct machine, rb_node);
1682 ret += machine__fprintf_dsos_buildid(pos, fp, with_hits);
1688 dso__kernel_findnew(struct machine *machine, const char *name,
1689 const char *short_name, int dso_type)
1692 * The kernel dso could be created by build_id processing.
1694 struct dso *dso = __dsos__findnew(&machine->kernel_dsos, name);
1697 * We need to run this in all cases, since during the build_id
1698 * processing we had no idea this was the kernel dso.
1701 dso__set_short_name(dso, short_name);
1702 dso->kernel = dso_type;
1708 void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
1710 char path[PATH_MAX];
1712 if (machine__is_default_guest(machine))
1714 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
1715 if (sysfs__read_build_id(path, dso->build_id,
1716 sizeof(dso->build_id)) == 0)
1717 dso->has_build_id = true;
1720 static struct dso *machine__get_kernel(struct machine *machine)
1722 const char *vmlinux_name = NULL;
1725 if (machine__is_host(machine)) {
1726 vmlinux_name = symbol_conf.vmlinux_name;
1728 vmlinux_name = "[kernel.kallsyms]";
1730 kernel = dso__kernel_findnew(machine, vmlinux_name,
1736 if (machine__is_default_guest(machine))
1737 vmlinux_name = symbol_conf.default_guest_vmlinux_name;
1739 vmlinux_name = machine__mmap_name(machine, bf,
1742 kernel = dso__kernel_findnew(machine, vmlinux_name,
1744 DSO_TYPE_GUEST_KERNEL);
1747 if (kernel != NULL && (!kernel->has_build_id))
1748 dso__read_running_kernel_build_id(kernel, machine);
1753 struct process_args {
1757 static int symbol__in_kernel(void *arg, const char *name,
1758 char type __maybe_unused, u64 start)
1760 struct process_args *args = arg;
1762 if (strchr(name, '['))
1765 args->start = start;
1769 /* Figure out the start address of kernel map from /proc/kallsyms */
1770 static u64 machine__get_kernel_start_addr(struct machine *machine)
1772 const char *filename;
1773 char path[PATH_MAX];
1774 struct process_args args;
1776 if (machine__is_host(machine)) {
1777 filename = "/proc/kallsyms";
1779 if (machine__is_default_guest(machine))
1780 filename = (char *)symbol_conf.default_guest_kallsyms;
1782 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
1787 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
1790 if (kallsyms__parse(filename, &args, symbol__in_kernel) <= 0)
1796 int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
1799 u64 start = machine__get_kernel_start_addr(machine);
1801 for (type = 0; type < MAP__NR_TYPES; ++type) {
1804 machine->vmlinux_maps[type] = map__new2(start, kernel, type);
1805 if (machine->vmlinux_maps[type] == NULL)
1808 machine->vmlinux_maps[type]->map_ip =
1809 machine->vmlinux_maps[type]->unmap_ip =
1811 kmap = map__kmap(machine->vmlinux_maps[type]);
1812 kmap->kmaps = &machine->kmaps;
1813 map_groups__insert(&machine->kmaps,
1814 machine->vmlinux_maps[type]);
1820 void machine__destroy_kernel_maps(struct machine *machine)
1824 for (type = 0; type < MAP__NR_TYPES; ++type) {
1827 if (machine->vmlinux_maps[type] == NULL)
1830 kmap = map__kmap(machine->vmlinux_maps[type]);
1831 map_groups__remove(&machine->kmaps,
1832 machine->vmlinux_maps[type]);
1833 if (kmap->ref_reloc_sym) {
1835 * ref_reloc_sym is shared among all maps, so free just
1838 if (type == MAP__FUNCTION) {
1839 free((char *)kmap->ref_reloc_sym->name);
1840 kmap->ref_reloc_sym->name = NULL;
1841 free(kmap->ref_reloc_sym);
1843 kmap->ref_reloc_sym = NULL;
1846 map__delete(machine->vmlinux_maps[type]);
1847 machine->vmlinux_maps[type] = NULL;
1851 int machine__create_kernel_maps(struct machine *machine)
1853 struct dso *kernel = machine__get_kernel(machine);
1855 if (kernel == NULL ||
1856 __machine__create_kernel_maps(machine, kernel) < 0)
1859 if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
1860 if (machine__is_host(machine))
1861 pr_debug("Problems creating module maps, "
1862 "continuing anyway...\n");
1864 pr_debug("Problems creating module maps for guest %d, "
1865 "continuing anyway...\n", machine->pid);
1869 * Now that we have all the maps created, just set the ->end of them:
1871 map_groups__fixup_end(&machine->kmaps);
1875 static void vmlinux_path__exit(void)
1877 while (--vmlinux_path__nr_entries >= 0) {
1878 free(vmlinux_path[vmlinux_path__nr_entries]);
1879 vmlinux_path[vmlinux_path__nr_entries] = NULL;
1883 vmlinux_path = NULL;
1886 static int vmlinux_path__init(void)
1891 vmlinux_path = malloc(sizeof(char *) * 5);
1892 if (vmlinux_path == NULL)
1895 vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
1896 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1898 ++vmlinux_path__nr_entries;
1899 vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
1900 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1902 ++vmlinux_path__nr_entries;
1904 /* only try running kernel version if no symfs was given */
1905 if (symbol_conf.symfs[0] != 0)
1908 if (uname(&uts) < 0)
1911 snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
1912 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1913 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1915 ++vmlinux_path__nr_entries;
1916 snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
1917 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1918 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1920 ++vmlinux_path__nr_entries;
1921 snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
1923 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1924 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1926 ++vmlinux_path__nr_entries;
1931 vmlinux_path__exit();
1935 size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
1939 struct dso *kdso = machine->vmlinux_maps[MAP__FUNCTION]->dso;
1941 if (kdso->has_build_id) {
1942 char filename[PATH_MAX];
1943 if (dso__build_id_filename(kdso, filename, sizeof(filename)))
1944 printed += fprintf(fp, "[0] %s\n", filename);
1947 for (i = 0; i < vmlinux_path__nr_entries; ++i)
1948 printed += fprintf(fp, "[%d] %s\n",
1949 i + kdso->has_build_id, vmlinux_path[i]);
1954 static int setup_list(struct strlist **list, const char *list_str,
1955 const char *list_name)
1957 if (list_str == NULL)
1960 *list = strlist__new(true, list_str);
1962 pr_err("problems parsing %s list\n", list_name);
1968 static bool symbol__read_kptr_restrict(void)
1972 if (geteuid() != 0) {
1973 FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
1977 if (fgets(line, sizeof(line), fp) != NULL)
1978 value = atoi(line) != 0;
1987 int symbol__init(void)
1991 if (symbol_conf.initialized)
1994 symbol_conf.priv_size = PERF_ALIGN(symbol_conf.priv_size, sizeof(u64));
1998 if (symbol_conf.sort_by_name)
1999 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
2000 sizeof(struct symbol));
2002 if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
2005 if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
2006 pr_err("'.' is the only non valid --field-separator argument\n");
2010 if (setup_list(&symbol_conf.dso_list,
2011 symbol_conf.dso_list_str, "dso") < 0)
2014 if (setup_list(&symbol_conf.comm_list,
2015 symbol_conf.comm_list_str, "comm") < 0)
2016 goto out_free_dso_list;
2018 if (setup_list(&symbol_conf.sym_list,
2019 symbol_conf.sym_list_str, "symbol") < 0)
2020 goto out_free_comm_list;
2023 * A path to symbols of "/" is identical to ""
2024 * reset here for simplicity.
2026 symfs = realpath(symbol_conf.symfs, NULL);
2028 symfs = symbol_conf.symfs;
2029 if (strcmp(symfs, "/") == 0)
2030 symbol_conf.symfs = "";
2031 if (symfs != symbol_conf.symfs)
2032 free((void *)symfs);
2034 symbol_conf.kptr_restrict = symbol__read_kptr_restrict();
2036 symbol_conf.initialized = true;
2040 strlist__delete(symbol_conf.comm_list);
2042 strlist__delete(symbol_conf.dso_list);
2046 void symbol__exit(void)
2048 if (!symbol_conf.initialized)
2050 strlist__delete(symbol_conf.sym_list);
2051 strlist__delete(symbol_conf.dso_list);
2052 strlist__delete(symbol_conf.comm_list);
2053 vmlinux_path__exit();
2054 symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
2055 symbol_conf.initialized = false;
2058 int machines__create_kernel_maps(struct rb_root *machines, pid_t pid)
2060 struct machine *machine = machines__findnew(machines, pid);
2062 if (machine == NULL)
2065 return machine__create_kernel_maps(machine);
2068 static int hex(char ch)
2070 if ((ch >= '0') && (ch <= '9'))
2072 if ((ch >= 'a') && (ch <= 'f'))
2073 return ch - 'a' + 10;
2074 if ((ch >= 'A') && (ch <= 'F'))
2075 return ch - 'A' + 10;
2080 * While we find nice hex chars, build a long_val.
2081 * Return number of chars processed.
2083 int hex2u64(const char *ptr, u64 *long_val)
2085 const char *p = ptr;
2089 const int hex_val = hex(*p);
2094 *long_val = (*long_val << 4) | hex_val;
2101 char *strxfrchar(char *s, char from, char to)
2105 while ((p = strchr(p, from)) != NULL)
2111 int machines__create_guest_kernel_maps(struct rb_root *machines)
2114 struct dirent **namelist = NULL;
2116 char path[PATH_MAX];
2120 if (symbol_conf.default_guest_vmlinux_name ||
2121 symbol_conf.default_guest_modules ||
2122 symbol_conf.default_guest_kallsyms) {
2123 machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
2126 if (symbol_conf.guestmount) {
2127 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
2130 for (i = 0; i < items; i++) {
2131 if (!isdigit(namelist[i]->d_name[0])) {
2132 /* Filter out . and .. */
2135 pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10);
2136 if ((*endp != '\0') ||
2137 (endp == namelist[i]->d_name) ||
2138 (errno == ERANGE)) {
2139 pr_debug("invalid directory (%s). Skipping.\n",
2140 namelist[i]->d_name);
2143 sprintf(path, "%s/%s/proc/kallsyms",
2144 symbol_conf.guestmount,
2145 namelist[i]->d_name);
2146 ret = access(path, R_OK);
2148 pr_debug("Can't access file %s\n", path);
2151 machines__create_kernel_maps(machines, pid);
2160 void machines__destroy_guest_kernel_maps(struct rb_root *machines)
2162 struct rb_node *next = rb_first(machines);
2165 struct machine *pos = rb_entry(next, struct machine, rb_node);
2167 next = rb_next(&pos->rb_node);
2168 rb_erase(&pos->rb_node, machines);
2169 machine__delete(pos);
2173 int machine__load_kallsyms(struct machine *machine, const char *filename,
2174 enum map_type type, symbol_filter_t filter)
2176 struct map *map = machine->vmlinux_maps[type];
2177 int ret = dso__load_kallsyms(map->dso, filename, map, filter);
2180 dso__set_loaded(map->dso, type);
2182 * Since /proc/kallsyms will have multiple sessions for the
2183 * kernel, with modules between them, fixup the end of all
2186 __map_groups__fixup_end(&machine->kmaps, type);
2192 int machine__load_vmlinux_path(struct machine *machine, enum map_type type,
2193 symbol_filter_t filter)
2195 struct map *map = machine->vmlinux_maps[type];
2196 int ret = dso__load_vmlinux_path(map->dso, map, filter);
2199 dso__set_loaded(map->dso, type);
2200 map__reloc_vmlinux(map);
2206 struct map *dso__new_map(const char *name)
2208 struct map *map = NULL;
2209 struct dso *dso = dso__new(name);
2212 map = map__new2(0, dso, MAP__FUNCTION);
2217 static int open_dso(struct dso *dso, struct machine *machine)
2219 char *root_dir = (char *) "";
2223 name = malloc(PATH_MAX);
2228 root_dir = machine->root_dir;
2230 if (dso__binary_type_file(dso, dso->data_type,
2231 root_dir, name, PATH_MAX)) {
2236 fd = open(name, O_RDONLY);
2241 int dso__data_fd(struct dso *dso, struct machine *machine)
2245 if (dso->data_type != DSO_BINARY_TYPE__NOT_FOUND)
2246 return open_dso(dso, machine);
2251 dso->data_type = binary_type_data[i++];
2253 fd = open_dso(dso, machine);
2257 } while (dso->data_type != DSO_BINARY_TYPE__NOT_FOUND);
2263 dso_cache__free(struct rb_root *root)
2265 struct rb_node *next = rb_first(root);
2268 struct dso_cache *cache;
2270 cache = rb_entry(next, struct dso_cache, rb_node);
2271 next = rb_next(&cache->rb_node);
2272 rb_erase(&cache->rb_node, root);
2277 static struct dso_cache*
2278 dso_cache__find(struct rb_root *root, u64 offset)
2280 struct rb_node **p = &root->rb_node;
2281 struct rb_node *parent = NULL;
2282 struct dso_cache *cache;
2284 while (*p != NULL) {
2288 cache = rb_entry(parent, struct dso_cache, rb_node);
2289 end = cache->offset + DSO__DATA_CACHE_SIZE;
2291 if (offset < cache->offset)
2293 else if (offset >= end)
2294 p = &(*p)->rb_right;
2302 dso_cache__insert(struct rb_root *root, struct dso_cache *new)
2304 struct rb_node **p = &root->rb_node;
2305 struct rb_node *parent = NULL;
2306 struct dso_cache *cache;
2307 u64 offset = new->offset;
2309 while (*p != NULL) {
2313 cache = rb_entry(parent, struct dso_cache, rb_node);
2314 end = cache->offset + DSO__DATA_CACHE_SIZE;
2316 if (offset < cache->offset)
2318 else if (offset >= end)
2319 p = &(*p)->rb_right;
2322 rb_link_node(&new->rb_node, parent, p);
2323 rb_insert_color(&new->rb_node, root);
2327 dso_cache__memcpy(struct dso_cache *cache, u64 offset,
2330 u64 cache_offset = offset - cache->offset;
2331 u64 cache_size = min(cache->size - cache_offset, size);
2333 memcpy(data, cache->data + cache_offset, cache_size);
2338 dso_cache__read(struct dso *dso, struct machine *machine,
2339 u64 offset, u8 *data, ssize_t size)
2341 struct dso_cache *cache;
2345 fd = dso__data_fd(dso, machine);
2354 cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
2358 cache_offset = offset & DSO__DATA_CACHE_MASK;
2361 if (-1 == lseek(fd, cache_offset, SEEK_SET))
2364 ret = read(fd, cache->data, DSO__DATA_CACHE_SIZE);
2368 cache->offset = cache_offset;
2370 dso_cache__insert(&dso->cache, cache);
2372 ret = dso_cache__memcpy(cache, offset, data, size);
2383 static ssize_t dso_cache_read(struct dso *dso, struct machine *machine,
2384 u64 offset, u8 *data, ssize_t size)
2386 struct dso_cache *cache;
2388 cache = dso_cache__find(&dso->cache, offset);
2390 return dso_cache__memcpy(cache, offset, data, size);
2392 return dso_cache__read(dso, machine, offset, data, size);
2395 ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
2396 u64 offset, u8 *data, ssize_t size)
2404 ret = dso_cache_read(dso, machine, offset, p, size);
2408 /* Reached EOF, return what we have. */
2424 ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
2425 struct machine *machine, u64 addr,
2426 u8 *data, ssize_t size)
2428 u64 offset = map->map_ip(map, addr);
2429 return dso__data_read_offset(dso, machine, offset, data, size);