1 // SPDX-License-Identifier: GPL-2.0
7 #include <linux/kernel.h>
8 #include <linux/mman.h>
9 #include <linux/time64.h>
10 #include <sys/types.h>
12 #include <sys/param.h>
25 #include "namespaces.h"
28 #include "sane_ctype.h"
32 #include <symbol/kallsyms.h>
33 #include <sys/utsname.h>
35 static int dso__load_kernel_sym(struct dso *dso, struct map *map);
36 static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map);
37 static bool symbol__is_idle(const char *name);
39 int vmlinux_path__nr_entries;
42 struct symbol_conf symbol_conf = {
45 .try_vmlinux_path = true,
47 .demangle_kernel = false,
48 .cumulate_callchain = true,
49 .time_quantum = 100 * NSEC_PER_MSEC, /* 100ms */
50 .show_hist_headers = true,
56 static enum dso_binary_type binary_type_symtab[] = {
57 DSO_BINARY_TYPE__KALLSYMS,
58 DSO_BINARY_TYPE__GUEST_KALLSYMS,
59 DSO_BINARY_TYPE__JAVA_JIT,
60 DSO_BINARY_TYPE__DEBUGLINK,
61 DSO_BINARY_TYPE__BUILD_ID_CACHE,
62 DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO,
63 DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
64 DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
65 DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
66 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
67 DSO_BINARY_TYPE__GUEST_KMODULE,
68 DSO_BINARY_TYPE__GUEST_KMODULE_COMP,
69 DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE,
70 DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP,
71 DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO,
72 DSO_BINARY_TYPE__NOT_FOUND,
75 #define DSO_BINARY_TYPE__SYMTAB_CNT ARRAY_SIZE(binary_type_symtab)
77 static bool symbol_type__filter(char symbol_type)
79 symbol_type = toupper(symbol_type);
80 return symbol_type == 'T' || symbol_type == 'W' || symbol_type == 'D' || symbol_type == 'B';
83 static int prefix_underscores_count(const char *str)
85 const char *tail = str;
93 const char * __weak arch__normalize_symbol_name(const char *name)
98 int __weak arch__compare_symbol_names(const char *namea, const char *nameb)
100 return strcmp(namea, nameb);
103 int __weak arch__compare_symbol_names_n(const char *namea, const char *nameb,
106 return strncmp(namea, nameb, n);
109 int __weak arch__choose_best_symbol(struct symbol *syma,
110 struct symbol *symb __maybe_unused)
112 /* Avoid "SyS" kernel syscall aliases */
113 if (strlen(syma->name) >= 3 && !strncmp(syma->name, "SyS", 3))
115 if (strlen(syma->name) >= 10 && !strncmp(syma->name, "compat_SyS", 10))
121 static int choose_best_symbol(struct symbol *syma, struct symbol *symb)
127 /* Prefer a symbol with non zero length */
128 a = syma->end - syma->start;
129 b = symb->end - symb->start;
130 if ((b == 0) && (a > 0))
132 else if ((a == 0) && (b > 0))
135 /* Prefer a non weak symbol over a weak one */
136 a = syma->binding == STB_WEAK;
137 b = symb->binding == STB_WEAK;
143 /* Prefer a global symbol over a non global one */
144 a = syma->binding == STB_GLOBAL;
145 b = symb->binding == STB_GLOBAL;
151 /* Prefer a symbol with less underscores */
152 a = prefix_underscores_count(syma->name);
153 b = prefix_underscores_count(symb->name);
159 /* Choose the symbol with the longest name */
160 na = strlen(syma->name);
161 nb = strlen(symb->name);
167 return arch__choose_best_symbol(syma, symb);
170 void symbols__fixup_duplicate(struct rb_root_cached *symbols)
173 struct symbol *curr, *next;
175 if (symbol_conf.allow_aliases)
178 nd = rb_first_cached(symbols);
181 curr = rb_entry(nd, struct symbol, rb_node);
183 nd = rb_next(&curr->rb_node);
184 next = rb_entry(nd, struct symbol, rb_node);
189 if (curr->start != next->start)
192 if (choose_best_symbol(curr, next) == SYMBOL_A) {
193 rb_erase_cached(&next->rb_node, symbols);
194 symbol__delete(next);
197 nd = rb_next(&curr->rb_node);
198 rb_erase_cached(&curr->rb_node, symbols);
199 symbol__delete(curr);
204 void symbols__fixup_end(struct rb_root_cached *symbols)
206 struct rb_node *nd, *prevnd = rb_first_cached(symbols);
207 struct symbol *curr, *prev;
212 curr = rb_entry(prevnd, struct symbol, rb_node);
214 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
216 curr = rb_entry(nd, struct symbol, rb_node);
218 if (prev->end == prev->start && prev->end != curr->start)
219 prev->end = curr->start;
223 if (curr->end == curr->start)
224 curr->end = roundup(curr->start, 4096) + 4096;
227 void map_groups__fixup_end(struct map_groups *mg)
229 struct maps *maps = &mg->maps;
230 struct map *next, *curr;
232 down_write(&maps->lock);
234 curr = maps__first(maps);
238 for (next = map__next(curr); next; next = map__next(curr)) {
240 curr->end = next->start;
245 * We still haven't the actual symbols, so guess the
246 * last map final address.
252 up_write(&maps->lock);
255 struct symbol *symbol__new(u64 start, u64 len, u8 binding, u8 type, const char *name)
257 size_t namelen = strlen(name) + 1;
258 struct symbol *sym = calloc(1, (symbol_conf.priv_size +
259 sizeof(*sym) + namelen));
263 if (symbol_conf.priv_size) {
264 if (symbol_conf.init_annotation) {
265 struct annotation *notes = (void *)sym;
266 pthread_mutex_init(¬es->lock, NULL);
268 sym = ((void *)sym) + symbol_conf.priv_size;
272 sym->end = len ? start + len : start;
274 sym->binding = binding;
275 sym->namelen = namelen - 1;
277 pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n",
278 __func__, name, start, sym->end);
279 memcpy(sym->name, name, namelen);
284 void symbol__delete(struct symbol *sym)
286 free(((void *)sym) - symbol_conf.priv_size);
289 void symbols__delete(struct rb_root_cached *symbols)
292 struct rb_node *next = rb_first_cached(symbols);
295 pos = rb_entry(next, struct symbol, rb_node);
296 next = rb_next(&pos->rb_node);
297 rb_erase_cached(&pos->rb_node, symbols);
302 void __symbols__insert(struct rb_root_cached *symbols,
303 struct symbol *sym, bool kernel)
305 struct rb_node **p = &symbols->rb_root.rb_node;
306 struct rb_node *parent = NULL;
307 const u64 ip = sym->start;
309 bool leftmost = true;
312 const char *name = sym->name;
314 * ppc64 uses function descriptors and appends a '.' to the
315 * start of every instruction address. Remove it.
319 sym->idle = symbol__is_idle(name);
324 s = rb_entry(parent, struct symbol, rb_node);
332 rb_link_node(&sym->rb_node, parent, p);
333 rb_insert_color_cached(&sym->rb_node, symbols, leftmost);
336 void symbols__insert(struct rb_root_cached *symbols, struct symbol *sym)
338 __symbols__insert(symbols, sym, false);
341 static struct symbol *symbols__find(struct rb_root_cached *symbols, u64 ip)
348 n = symbols->rb_root.rb_node;
351 struct symbol *s = rb_entry(n, struct symbol, rb_node);
355 else if (ip > s->end || (ip == s->end && ip != s->start))
364 static struct symbol *symbols__first(struct rb_root_cached *symbols)
366 struct rb_node *n = rb_first_cached(symbols);
369 return rb_entry(n, struct symbol, rb_node);
374 static struct symbol *symbols__last(struct rb_root_cached *symbols)
376 struct rb_node *n = rb_last(&symbols->rb_root);
379 return rb_entry(n, struct symbol, rb_node);
384 static struct symbol *symbols__next(struct symbol *sym)
386 struct rb_node *n = rb_next(&sym->rb_node);
389 return rb_entry(n, struct symbol, rb_node);
394 static void symbols__insert_by_name(struct rb_root_cached *symbols, struct symbol *sym)
396 struct rb_node **p = &symbols->rb_root.rb_node;
397 struct rb_node *parent = NULL;
398 struct symbol_name_rb_node *symn, *s;
399 bool leftmost = true;
401 symn = container_of(sym, struct symbol_name_rb_node, sym);
405 s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
406 if (strcmp(sym->name, s->sym.name) < 0)
413 rb_link_node(&symn->rb_node, parent, p);
414 rb_insert_color_cached(&symn->rb_node, symbols, leftmost);
417 static void symbols__sort_by_name(struct rb_root_cached *symbols,
418 struct rb_root_cached *source)
422 for (nd = rb_first_cached(source); nd; nd = rb_next(nd)) {
423 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
424 symbols__insert_by_name(symbols, pos);
428 int symbol__match_symbol_name(const char *name, const char *str,
429 enum symbol_tag_include includes)
431 const char *versioning;
433 if (includes == SYMBOL_TAG_INCLUDE__DEFAULT_ONLY &&
434 (versioning = strstr(name, "@@"))) {
435 int len = strlen(str);
437 if (len < versioning - name)
438 len = versioning - name;
440 return arch__compare_symbol_names_n(name, str, len);
442 return arch__compare_symbol_names(name, str);
445 static struct symbol *symbols__find_by_name(struct rb_root_cached *symbols,
447 enum symbol_tag_include includes)
450 struct symbol_name_rb_node *s = NULL;
455 n = symbols->rb_root.rb_node;
460 s = rb_entry(n, struct symbol_name_rb_node, rb_node);
461 cmp = symbol__match_symbol_name(s->sym.name, name, includes);
474 if (includes != SYMBOL_TAG_INCLUDE__DEFAULT_ONLY)
475 /* return first symbol that has same name (if any) */
476 for (n = rb_prev(n); n; n = rb_prev(n)) {
477 struct symbol_name_rb_node *tmp;
479 tmp = rb_entry(n, struct symbol_name_rb_node, rb_node);
480 if (arch__compare_symbol_names(tmp->sym.name, s->sym.name))
489 void dso__reset_find_symbol_cache(struct dso *dso)
491 dso->last_find_result.addr = 0;
492 dso->last_find_result.symbol = NULL;
495 void dso__insert_symbol(struct dso *dso, struct symbol *sym)
497 __symbols__insert(&dso->symbols, sym, dso->kernel);
499 /* update the symbol cache if necessary */
500 if (dso->last_find_result.addr >= sym->start &&
501 (dso->last_find_result.addr < sym->end ||
502 sym->start == sym->end)) {
503 dso->last_find_result.symbol = sym;
507 struct symbol *dso__find_symbol(struct dso *dso, u64 addr)
509 if (dso->last_find_result.addr != addr || dso->last_find_result.symbol == NULL) {
510 dso->last_find_result.addr = addr;
511 dso->last_find_result.symbol = symbols__find(&dso->symbols, addr);
514 return dso->last_find_result.symbol;
517 struct symbol *dso__first_symbol(struct dso *dso)
519 return symbols__first(&dso->symbols);
522 struct symbol *dso__last_symbol(struct dso *dso)
524 return symbols__last(&dso->symbols);
527 struct symbol *dso__next_symbol(struct symbol *sym)
529 return symbols__next(sym);
532 struct symbol *symbol__next_by_name(struct symbol *sym)
534 struct symbol_name_rb_node *s = container_of(sym, struct symbol_name_rb_node, sym);
535 struct rb_node *n = rb_next(&s->rb_node);
537 return n ? &rb_entry(n, struct symbol_name_rb_node, rb_node)->sym : NULL;
541 * Returns first symbol that matched with @name.
543 struct symbol *dso__find_symbol_by_name(struct dso *dso, const char *name)
545 struct symbol *s = symbols__find_by_name(&dso->symbol_names, name,
546 SYMBOL_TAG_INCLUDE__NONE);
548 s = symbols__find_by_name(&dso->symbol_names, name,
549 SYMBOL_TAG_INCLUDE__DEFAULT_ONLY);
553 void dso__sort_by_name(struct dso *dso)
555 dso__set_sorted_by_name(dso);
556 return symbols__sort_by_name(&dso->symbol_names, &dso->symbols);
559 int modules__parse(const char *filename, void *arg,
560 int (*process_module)(void *arg, const char *name,
561 u64 start, u64 size))
568 file = fopen(filename, "r");
578 line_len = getline(&line, &n, file);
591 line[--line_len] = '\0'; /* \n */
593 sep = strrchr(line, 'x');
597 hex2u64(sep + 1, &start);
599 sep = strchr(line, ' ');
605 scnprintf(name, sizeof(name), "[%s]", line);
607 size = strtoul(sep + 1, &endptr, 0);
608 if (*endptr != ' ' && *endptr != '\t')
611 err = process_module(arg, name, start, size);
622 * These are symbols in the kernel image, so make sure that
623 * sym is from a kernel DSO.
625 static bool symbol__is_idle(const char *name)
627 const char * const idle_symbols[] = {
637 "mwait_idle_with_hints",
639 "ppc64_runlatch_off",
640 "pseries_dedicated_idle_sleep",
645 for (i = 0; idle_symbols[i]; i++) {
646 if (!strcmp(idle_symbols[i], name))
653 static int map__process_kallsym_symbol(void *arg, const char *name,
654 char type, u64 start)
657 struct dso *dso = arg;
658 struct rb_root_cached *root = &dso->symbols;
660 if (!symbol_type__filter(type))
664 * module symbols are not sorted so we add all
665 * symbols, setting length to 0, and rely on
666 * symbols__fixup_end() to fix it up.
668 sym = symbol__new(start, 0, kallsyms2elf_binding(type), kallsyms2elf_type(type), name);
672 * We will pass the symbols to the filter later, in
673 * map__split_kallsyms, when we have split the maps per module
675 __symbols__insert(root, sym, !strchr(name, '['));
681 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
682 * so that we can in the next step set the symbol ->end address and then
683 * call kernel_maps__split_kallsyms.
685 static int dso__load_all_kallsyms(struct dso *dso, const char *filename)
687 return kallsyms__parse(filename, dso, map__process_kallsym_symbol);
690 static int map_groups__split_kallsyms_for_kcore(struct map_groups *kmaps, struct dso *dso)
692 struct map *curr_map;
695 struct rb_root_cached old_root = dso->symbols;
696 struct rb_root_cached *root = &dso->symbols;
697 struct rb_node *next = rb_first_cached(root);
702 *root = RB_ROOT_CACHED;
707 pos = rb_entry(next, struct symbol, rb_node);
708 next = rb_next(&pos->rb_node);
710 rb_erase_cached(&pos->rb_node, &old_root);
711 RB_CLEAR_NODE(&pos->rb_node);
712 module = strchr(pos->name, '\t');
716 curr_map = map_groups__find(kmaps, pos->start);
723 pos->start -= curr_map->start - curr_map->pgoff;
724 if (pos->end > curr_map->end)
725 pos->end = curr_map->end;
727 pos->end -= curr_map->start - curr_map->pgoff;
728 symbols__insert(&curr_map->dso->symbols, pos);
732 /* Symbols have been adjusted */
733 dso->adjust_symbols = 1;
739 * Split the symbols into maps, making sure there are no overlaps, i.e. the
740 * kernel range is broken in several maps, named [kernel].N, as we don't have
741 * the original ELF section names vmlinux have.
743 static int map_groups__split_kallsyms(struct map_groups *kmaps, struct dso *dso, u64 delta,
744 struct map *initial_map)
746 struct machine *machine;
747 struct map *curr_map = initial_map;
749 int count = 0, moved = 0;
750 struct rb_root_cached *root = &dso->symbols;
751 struct rb_node *next = rb_first_cached(root);
752 int kernel_range = 0;
758 machine = kmaps->machine;
760 x86_64 = machine__is(machine, "x86_64");
765 pos = rb_entry(next, struct symbol, rb_node);
766 next = rb_next(&pos->rb_node);
768 module = strchr(pos->name, '\t');
770 if (!symbol_conf.use_modules)
775 if (strcmp(curr_map->dso->short_name, module)) {
776 if (curr_map != initial_map &&
777 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
778 machine__is_default_guest(machine)) {
780 * We assume all symbols of a module are
781 * continuous in * kallsyms, so curr_map
782 * points to a module and all its
783 * symbols are in its kmap. Mark it as
786 dso__set_loaded(curr_map->dso);
789 curr_map = map_groups__find_by_name(kmaps, module);
790 if (curr_map == NULL) {
791 pr_debug("%s/proc/{kallsyms,modules} "
792 "inconsistency while looking "
793 "for \"%s\" module!\n",
794 machine->root_dir, module);
795 curr_map = initial_map;
799 if (curr_map->dso->loaded &&
800 !machine__is_default_guest(machine))
804 * So that we look just like we get from .ko files,
805 * i.e. not prelinked, relative to initial_map->start.
807 pos->start = curr_map->map_ip(curr_map, pos->start);
808 pos->end = curr_map->map_ip(curr_map, pos->end);
809 } else if (x86_64 && is_entry_trampoline(pos->name)) {
811 * These symbols are not needed anymore since the
812 * trampoline maps refer to the text section and it's
813 * symbols instead. Avoid having to deal with
814 * relocations, and the assumption that the first symbol
815 * is the start of kernel text, by simply removing the
816 * symbols at this point.
819 } else if (curr_map != initial_map) {
820 char dso_name[PATH_MAX];
824 /* Kernel was relocated at boot time */
830 curr_map = initial_map;
834 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
835 snprintf(dso_name, sizeof(dso_name),
839 snprintf(dso_name, sizeof(dso_name),
843 ndso = dso__new(dso_name);
847 ndso->kernel = dso->kernel;
849 curr_map = map__new2(pos->start, ndso);
850 if (curr_map == NULL) {
855 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
856 map_groups__insert(kmaps, curr_map);
859 /* Kernel was relocated at boot time */
864 if (curr_map != initial_map) {
865 rb_erase_cached(&pos->rb_node, root);
866 symbols__insert(&curr_map->dso->symbols, pos);
873 rb_erase_cached(&pos->rb_node, root);
877 if (curr_map != initial_map &&
878 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
879 machine__is_default_guest(kmaps->machine)) {
880 dso__set_loaded(curr_map->dso);
883 return count + moved;
886 bool symbol__restricted_filename(const char *filename,
887 const char *restricted_filename)
889 bool restricted = false;
891 if (symbol_conf.kptr_restrict) {
892 char *r = realpath(filename, NULL);
895 restricted = strcmp(r, restricted_filename) == 0;
905 struct rb_node rb_node;
910 static void add_module(struct module_info *mi, struct rb_root *modules)
912 struct rb_node **p = &modules->rb_node;
913 struct rb_node *parent = NULL;
914 struct module_info *m;
918 m = rb_entry(parent, struct module_info, rb_node);
919 if (strcmp(mi->name, m->name) < 0)
924 rb_link_node(&mi->rb_node, parent, p);
925 rb_insert_color(&mi->rb_node, modules);
928 static void delete_modules(struct rb_root *modules)
930 struct module_info *mi;
931 struct rb_node *next = rb_first(modules);
934 mi = rb_entry(next, struct module_info, rb_node);
935 next = rb_next(&mi->rb_node);
936 rb_erase(&mi->rb_node, modules);
942 static struct module_info *find_module(const char *name,
943 struct rb_root *modules)
945 struct rb_node *n = modules->rb_node;
948 struct module_info *m;
951 m = rb_entry(n, struct module_info, rb_node);
952 cmp = strcmp(name, m->name);
964 static int __read_proc_modules(void *arg, const char *name, u64 start,
965 u64 size __maybe_unused)
967 struct rb_root *modules = arg;
968 struct module_info *mi;
970 mi = zalloc(sizeof(struct module_info));
974 mi->name = strdup(name);
982 add_module(mi, modules);
987 static int read_proc_modules(const char *filename, struct rb_root *modules)
989 if (symbol__restricted_filename(filename, "/proc/modules"))
992 if (modules__parse(filename, modules, __read_proc_modules)) {
993 delete_modules(modules);
1000 int compare_proc_modules(const char *from, const char *to)
1002 struct rb_root from_modules = RB_ROOT;
1003 struct rb_root to_modules = RB_ROOT;
1004 struct rb_node *from_node, *to_node;
1005 struct module_info *from_m, *to_m;
1008 if (read_proc_modules(from, &from_modules))
1011 if (read_proc_modules(to, &to_modules))
1012 goto out_delete_from;
1014 from_node = rb_first(&from_modules);
1015 to_node = rb_first(&to_modules);
1020 from_m = rb_entry(from_node, struct module_info, rb_node);
1021 to_m = rb_entry(to_node, struct module_info, rb_node);
1023 if (from_m->start != to_m->start ||
1024 strcmp(from_m->name, to_m->name))
1027 from_node = rb_next(from_node);
1028 to_node = rb_next(to_node);
1031 if (!from_node && !to_node)
1034 delete_modules(&to_modules);
1036 delete_modules(&from_modules);
1041 struct map *map_groups__first(struct map_groups *mg)
1043 return maps__first(&mg->maps);
1046 static int do_validate_kcore_modules(const char *filename,
1047 struct map_groups *kmaps)
1049 struct rb_root modules = RB_ROOT;
1050 struct map *old_map;
1053 err = read_proc_modules(filename, &modules);
1057 old_map = map_groups__first(kmaps);
1059 struct map *next = map_groups__next(old_map);
1060 struct module_info *mi;
1062 if (!__map__is_kmodule(old_map)) {
1067 /* Module must be in memory at the same address */
1068 mi = find_module(old_map->dso->short_name, &modules);
1069 if (!mi || mi->start != old_map->start) {
1077 delete_modules(&modules);
1082 * If kallsyms is referenced by name then we look for filename in the same
1085 static bool filename_from_kallsyms_filename(char *filename,
1086 const char *base_name,
1087 const char *kallsyms_filename)
1091 strcpy(filename, kallsyms_filename);
1092 name = strrchr(filename, '/');
1098 if (!strcmp(name, "kallsyms")) {
1099 strcpy(name, base_name);
1106 static int validate_kcore_modules(const char *kallsyms_filename,
1109 struct map_groups *kmaps = map__kmaps(map);
1110 char modules_filename[PATH_MAX];
1115 if (!filename_from_kallsyms_filename(modules_filename, "modules",
1119 if (do_validate_kcore_modules(modules_filename, kmaps))
1125 static int validate_kcore_addresses(const char *kallsyms_filename,
1128 struct kmap *kmap = map__kmap(map);
1133 if (kmap->ref_reloc_sym && kmap->ref_reloc_sym->name) {
1136 if (kallsyms__get_function_start(kallsyms_filename,
1137 kmap->ref_reloc_sym->name, &start))
1139 if (start != kmap->ref_reloc_sym->addr)
1143 return validate_kcore_modules(kallsyms_filename, map);
1146 struct kcore_mapfn_data {
1148 struct list_head maps;
1151 static int kcore_mapfn(u64 start, u64 len, u64 pgoff, void *data)
1153 struct kcore_mapfn_data *md = data;
1156 map = map__new2(start, md->dso);
1160 map->end = map->start + len;
1163 list_add(&map->node, &md->maps);
1168 static int dso__load_kcore(struct dso *dso, struct map *map,
1169 const char *kallsyms_filename)
1171 struct map_groups *kmaps = map__kmaps(map);
1172 struct kcore_mapfn_data md;
1173 struct map *old_map, *new_map, *replacement_map = NULL;
1174 struct machine *machine;
1177 char kcore_filename[PATH_MAX];
1183 machine = kmaps->machine;
1185 /* This function requires that the map is the kernel map */
1186 if (!__map__is_kernel(map))
1189 if (!filename_from_kallsyms_filename(kcore_filename, "kcore",
1193 /* Modules and kernel must be present at their original addresses */
1194 if (validate_kcore_addresses(kallsyms_filename, map))
1198 INIT_LIST_HEAD(&md.maps);
1200 fd = open(kcore_filename, O_RDONLY);
1202 pr_debug("Failed to open %s. Note /proc/kcore requires CAP_SYS_RAWIO capability to access.\n",
1207 /* Read new maps into temporary lists */
1208 err = file__read_maps(fd, map->prot & PROT_EXEC, kcore_mapfn, &md,
1212 dso->is_64_bit = is_64_bit;
1214 if (list_empty(&md.maps)) {
1219 /* Remove old maps */
1220 old_map = map_groups__first(kmaps);
1222 struct map *next = map_groups__next(old_map);
1225 map_groups__remove(kmaps, old_map);
1228 machine->trampolines_mapped = false;
1230 /* Find the kernel map using the '_stext' symbol */
1231 if (!kallsyms__get_function_start(kallsyms_filename, "_stext", &stext)) {
1232 list_for_each_entry(new_map, &md.maps, node) {
1233 if (stext >= new_map->start && stext < new_map->end) {
1234 replacement_map = new_map;
1240 if (!replacement_map)
1241 replacement_map = list_entry(md.maps.next, struct map, node);
1244 while (!list_empty(&md.maps)) {
1245 new_map = list_entry(md.maps.next, struct map, node);
1246 list_del_init(&new_map->node);
1247 if (new_map == replacement_map) {
1248 map->start = new_map->start;
1249 map->end = new_map->end;
1250 map->pgoff = new_map->pgoff;
1251 map->map_ip = new_map->map_ip;
1252 map->unmap_ip = new_map->unmap_ip;
1253 /* Ensure maps are correctly ordered */
1255 map_groups__remove(kmaps, map);
1256 map_groups__insert(kmaps, map);
1259 map_groups__insert(kmaps, new_map);
1265 if (machine__is(machine, "x86_64")) {
1269 * If one of the corresponding symbols is there, assume the
1270 * entry trampoline maps are too.
1272 if (!kallsyms__get_function_start(kallsyms_filename,
1273 ENTRY_TRAMPOLINE_NAME,
1275 machine->trampolines_mapped = true;
1279 * Set the data type and long name so that kcore can be read via
1280 * dso__data_read_addr().
1282 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1283 dso->binary_type = DSO_BINARY_TYPE__GUEST_KCORE;
1285 dso->binary_type = DSO_BINARY_TYPE__KCORE;
1286 dso__set_long_name(dso, strdup(kcore_filename), true);
1290 if (map->prot & PROT_EXEC)
1291 pr_debug("Using %s for kernel object code\n", kcore_filename);
1293 pr_debug("Using %s for kernel data\n", kcore_filename);
1298 while (!list_empty(&md.maps)) {
1299 map = list_entry(md.maps.next, struct map, node);
1300 list_del_init(&map->node);
1308 * If the kernel is relocated at boot time, kallsyms won't match. Compute the
1309 * delta based on the relocation reference symbol.
1311 static int kallsyms__delta(struct kmap *kmap, const char *filename, u64 *delta)
1315 if (!kmap->ref_reloc_sym || !kmap->ref_reloc_sym->name)
1318 if (kallsyms__get_function_start(filename, kmap->ref_reloc_sym->name, &addr))
1321 *delta = addr - kmap->ref_reloc_sym->addr;
1325 int __dso__load_kallsyms(struct dso *dso, const char *filename,
1326 struct map *map, bool no_kcore)
1328 struct kmap *kmap = map__kmap(map);
1331 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
1334 if (!kmap || !kmap->kmaps)
1337 if (dso__load_all_kallsyms(dso, filename) < 0)
1340 if (kallsyms__delta(kmap, filename, &delta))
1343 symbols__fixup_end(&dso->symbols);
1344 symbols__fixup_duplicate(&dso->symbols);
1346 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1347 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
1349 dso->symtab_type = DSO_BINARY_TYPE__KALLSYMS;
1351 if (!no_kcore && !dso__load_kcore(dso, map, filename))
1352 return map_groups__split_kallsyms_for_kcore(kmap->kmaps, dso);
1354 return map_groups__split_kallsyms(kmap->kmaps, dso, delta, map);
1357 int dso__load_kallsyms(struct dso *dso, const char *filename,
1360 return __dso__load_kallsyms(dso, filename, map, false);
1363 static int dso__load_perf_map(const char *map_path, struct dso *dso)
1370 file = fopen(map_path, "r");
1374 while (!feof(file)) {
1379 line_len = getline(&line, &n, file);
1386 line[--line_len] = '\0'; /* \n */
1388 len = hex2u64(line, &start);
1391 if (len + 2 >= line_len)
1394 len += hex2u64(line + len, &size);
1397 if (len + 2 >= line_len)
1400 sym = symbol__new(start, size, STB_GLOBAL, STT_FUNC, line + len);
1403 goto out_delete_line;
1405 symbols__insert(&dso->symbols, sym);
1420 static bool dso__is_compatible_symtab_type(struct dso *dso, bool kmod,
1421 enum dso_binary_type type)
1424 case DSO_BINARY_TYPE__JAVA_JIT:
1425 case DSO_BINARY_TYPE__DEBUGLINK:
1426 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
1427 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
1428 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
1429 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
1430 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
1431 return !kmod && dso->kernel == DSO_TYPE_USER;
1433 case DSO_BINARY_TYPE__KALLSYMS:
1434 case DSO_BINARY_TYPE__VMLINUX:
1435 case DSO_BINARY_TYPE__KCORE:
1436 return dso->kernel == DSO_TYPE_KERNEL;
1438 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
1439 case DSO_BINARY_TYPE__GUEST_VMLINUX:
1440 case DSO_BINARY_TYPE__GUEST_KCORE:
1441 return dso->kernel == DSO_TYPE_GUEST_KERNEL;
1443 case DSO_BINARY_TYPE__GUEST_KMODULE:
1444 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
1445 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
1446 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
1448 * kernel modules know their symtab type - it's set when
1449 * creating a module dso in machine__findnew_module_map().
1451 return kmod && dso->symtab_type == type;
1453 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
1454 case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO:
1457 case DSO_BINARY_TYPE__NOT_FOUND:
1463 /* Checks for the existence of the perf-<pid>.map file in two different
1464 * locations. First, if the process is a separate mount namespace, check in
1465 * that namespace using the pid of the innermost pid namespace. If's not in a
1466 * namespace, or the file can't be found there, try in the mount namespace of
1467 * the tracing process using our view of its pid.
1469 static int dso__find_perf_map(char *filebuf, size_t bufsz,
1470 struct nsinfo **nsip)
1472 struct nscookie nsc;
1474 struct nsinfo *nnsi;
1479 if (nsi->need_setns) {
1480 snprintf(filebuf, bufsz, "/tmp/perf-%d.map", nsi->nstgid);
1481 nsinfo__mountns_enter(nsi, &nsc);
1482 rc = access(filebuf, R_OK);
1483 nsinfo__mountns_exit(&nsc);
1488 nnsi = nsinfo__copy(nsi);
1492 nnsi->need_setns = false;
1493 snprintf(filebuf, bufsz, "/tmp/perf-%d.map", nnsi->tgid);
1501 int dso__load(struct dso *dso, struct map *map)
1506 struct machine *machine;
1507 char *root_dir = (char *) "";
1509 struct symsrc ss_[2];
1510 struct symsrc *syms_ss = NULL, *runtime_ss = NULL;
1513 unsigned char build_id[BUILD_ID_SIZE];
1514 struct nscookie nsc;
1515 char newmapname[PATH_MAX];
1516 const char *map_path = dso->long_name;
1518 perfmap = strncmp(dso->name, "/tmp/perf-", 10) == 0;
1520 if (dso->nsinfo && (dso__find_perf_map(newmapname,
1521 sizeof(newmapname), &dso->nsinfo) == 0)) {
1522 map_path = newmapname;
1526 nsinfo__mountns_enter(dso->nsinfo, &nsc);
1527 pthread_mutex_lock(&dso->lock);
1529 /* check again under the dso->lock */
1530 if (dso__loaded(dso)) {
1535 if (map->groups && map->groups->machine)
1536 machine = map->groups->machine;
1541 if (dso->kernel == DSO_TYPE_KERNEL)
1542 ret = dso__load_kernel_sym(dso, map);
1543 else if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1544 ret = dso__load_guest_kernel_sym(dso, map);
1546 if (machine__is(machine, "x86_64"))
1547 machine__map_x86_64_entry_trampolines(machine, dso);
1551 dso->adjust_symbols = 0;
1554 ret = dso__load_perf_map(map_path, dso);
1555 dso->symtab_type = ret > 0 ? DSO_BINARY_TYPE__JAVA_JIT :
1556 DSO_BINARY_TYPE__NOT_FOUND;
1561 root_dir = machine->root_dir;
1563 name = malloc(PATH_MAX);
1567 kmod = dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
1568 dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
1569 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE ||
1570 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
1574 * Read the build id if possible. This is required for
1575 * DSO_BINARY_TYPE__BUILDID_DEBUGINFO to work
1577 if (!dso->has_build_id &&
1578 is_regular_file(dso->long_name)) {
1579 __symbol__join_symfs(name, PATH_MAX, dso->long_name);
1580 if (filename__read_build_id(name, build_id, BUILD_ID_SIZE) > 0)
1581 dso__set_build_id(dso, build_id);
1585 * Iterate over candidate debug images.
1586 * Keep track of "interesting" ones (those which have a symtab, dynsym,
1587 * and/or opd section) for processing.
1589 for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) {
1590 struct symsrc *ss = &ss_[ss_pos];
1591 bool next_slot = false;
1596 enum dso_binary_type symtab_type = binary_type_symtab[i];
1598 nsexit = (symtab_type == DSO_BINARY_TYPE__BUILD_ID_CACHE ||
1599 symtab_type == DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO);
1601 if (!dso__is_compatible_symtab_type(dso, kmod, symtab_type))
1604 if (dso__read_binary_type_filename(dso, symtab_type,
1605 root_dir, name, PATH_MAX))
1609 nsinfo__mountns_exit(&nsc);
1611 is_reg = is_regular_file(name);
1613 sirc = symsrc__init(ss, dso, name, symtab_type);
1616 nsinfo__mountns_enter(dso->nsinfo, &nsc);
1618 if (!is_reg || sirc < 0)
1621 if (!syms_ss && symsrc__has_symtab(ss)) {
1624 if (!dso->symsrc_filename)
1625 dso->symsrc_filename = strdup(name);
1628 if (!runtime_ss && symsrc__possibly_runtime(ss)) {
1636 if (syms_ss && runtime_ss)
1639 symsrc__destroy(ss);
1644 if (!runtime_ss && !syms_ss)
1647 if (runtime_ss && !syms_ss) {
1648 syms_ss = runtime_ss;
1651 /* We'll have to hope for the best */
1652 if (!runtime_ss && syms_ss)
1653 runtime_ss = syms_ss;
1656 ret = dso__load_sym(dso, map, syms_ss, runtime_ss, kmod);
1663 nr_plt = dso__synthesize_plt_symbols(dso, runtime_ss);
1668 for (; ss_pos > 0; ss_pos--)
1669 symsrc__destroy(&ss_[ss_pos - 1]);
1672 if (ret < 0 && strstr(dso->name, " (deleted)") != NULL)
1675 dso__set_loaded(dso);
1676 pthread_mutex_unlock(&dso->lock);
1677 nsinfo__mountns_exit(&nsc);
1682 struct map *map_groups__find_by_name(struct map_groups *mg, const char *name)
1684 struct maps *maps = &mg->maps;
1686 struct rb_node *node;
1688 down_read(&maps->lock);
1690 for (node = maps->names.rb_node; node; ) {
1693 map = rb_entry(node, struct map, rb_node_name);
1695 rc = strcmp(map->dso->short_name, name);
1697 node = node->rb_left;
1699 node = node->rb_right;
1708 up_read(&maps->lock);
1712 int dso__load_vmlinux(struct dso *dso, struct map *map,
1713 const char *vmlinux, bool vmlinux_allocated)
1717 char symfs_vmlinux[PATH_MAX];
1718 enum dso_binary_type symtab_type;
1720 if (vmlinux[0] == '/')
1721 snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s", vmlinux);
1723 symbol__join_symfs(symfs_vmlinux, vmlinux);
1725 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1726 symtab_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
1728 symtab_type = DSO_BINARY_TYPE__VMLINUX;
1730 if (symsrc__init(&ss, dso, symfs_vmlinux, symtab_type))
1733 err = dso__load_sym(dso, map, &ss, &ss, 0);
1734 symsrc__destroy(&ss);
1737 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1738 dso->binary_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
1740 dso->binary_type = DSO_BINARY_TYPE__VMLINUX;
1741 dso__set_long_name(dso, vmlinux, vmlinux_allocated);
1742 dso__set_loaded(dso);
1743 pr_debug("Using %s for symbols\n", symfs_vmlinux);
1749 int dso__load_vmlinux_path(struct dso *dso, struct map *map)
1752 char *filename = NULL;
1754 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1755 vmlinux_path__nr_entries + 1);
1757 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
1758 err = dso__load_vmlinux(dso, map, vmlinux_path[i], false);
1763 if (!symbol_conf.ignore_vmlinux_buildid)
1764 filename = dso__build_id_filename(dso, NULL, 0, false);
1765 if (filename != NULL) {
1766 err = dso__load_vmlinux(dso, map, filename, true);
1775 static bool visible_dir_filter(const char *name, struct dirent *d)
1777 if (d->d_type != DT_DIR)
1779 return lsdir_no_dot_filter(name, d);
1782 static int find_matching_kcore(struct map *map, char *dir, size_t dir_sz)
1784 char kallsyms_filename[PATH_MAX];
1786 struct strlist *dirs;
1787 struct str_node *nd;
1789 dirs = lsdir(dir, visible_dir_filter);
1793 strlist__for_each_entry(nd, dirs) {
1794 scnprintf(kallsyms_filename, sizeof(kallsyms_filename),
1795 "%s/%s/kallsyms", dir, nd->s);
1796 if (!validate_kcore_addresses(kallsyms_filename, map)) {
1797 strlcpy(dir, kallsyms_filename, dir_sz);
1803 strlist__delete(dirs);
1809 * Use open(O_RDONLY) to check readability directly instead of access(R_OK)
1810 * since access(R_OK) only checks with real UID/GID but open() use effective
1811 * UID/GID and actual capabilities (e.g. /proc/kcore requires CAP_SYS_RAWIO).
1813 static bool filename__readable(const char *file)
1815 int fd = open(file, O_RDONLY);
1822 static char *dso__find_kallsyms(struct dso *dso, struct map *map)
1824 u8 host_build_id[BUILD_ID_SIZE];
1825 char sbuild_id[SBUILD_ID_SIZE];
1826 bool is_host = false;
1827 char path[PATH_MAX];
1829 if (!dso->has_build_id) {
1831 * Last resort, if we don't have a build-id and couldn't find
1832 * any vmlinux file, try the running kernel kallsyms table.
1837 if (sysfs__read_build_id("/sys/kernel/notes", host_build_id,
1838 sizeof(host_build_id)) == 0)
1839 is_host = dso__build_id_equal(dso, host_build_id);
1841 /* Try a fast path for /proc/kallsyms if possible */
1844 * Do not check the build-id cache, unless we know we cannot use
1845 * /proc/kcore or module maps don't match to /proc/kallsyms.
1846 * To check readability of /proc/kcore, do not use access(R_OK)
1847 * since /proc/kcore requires CAP_SYS_RAWIO to read and access
1850 if (filename__readable("/proc/kcore") &&
1851 !validate_kcore_addresses("/proc/kallsyms", map))
1855 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1857 /* Find kallsyms in build-id cache with kcore */
1858 scnprintf(path, sizeof(path), "%s/%s/%s",
1859 buildid_dir, DSO__NAME_KCORE, sbuild_id);
1861 if (!find_matching_kcore(map, path, sizeof(path)))
1862 return strdup(path);
1864 /* Use current /proc/kallsyms if possible */
1867 return strdup("/proc/kallsyms");
1870 /* Finally, find a cache of kallsyms */
1871 if (!build_id_cache__kallsyms_path(sbuild_id, path, sizeof(path))) {
1872 pr_err("No kallsyms or vmlinux with build-id %s was found\n",
1877 return strdup(path);
1880 static int dso__load_kernel_sym(struct dso *dso, struct map *map)
1883 const char *kallsyms_filename = NULL;
1884 char *kallsyms_allocated_filename = NULL;
1886 * Step 1: if the user specified a kallsyms or vmlinux filename, use
1887 * it and only it, reporting errors to the user if it cannot be used.
1889 * For instance, try to analyse an ARM perf.data file _without_ a
1890 * build-id, or if the user specifies the wrong path to the right
1891 * vmlinux file, obviously we can't fallback to another vmlinux (a
1892 * x86_86 one, on the machine where analysis is being performed, say),
1893 * or worse, /proc/kallsyms.
1895 * If the specified file _has_ a build-id and there is a build-id
1896 * section in the perf.data file, we will still do the expected
1897 * validation in dso__load_vmlinux and will bail out if they don't
1900 if (symbol_conf.kallsyms_name != NULL) {
1901 kallsyms_filename = symbol_conf.kallsyms_name;
1905 if (!symbol_conf.ignore_vmlinux && symbol_conf.vmlinux_name != NULL) {
1906 return dso__load_vmlinux(dso, map, symbol_conf.vmlinux_name, false);
1909 if (!symbol_conf.ignore_vmlinux && vmlinux_path != NULL) {
1910 err = dso__load_vmlinux_path(dso, map);
1915 /* do not try local files if a symfs was given */
1916 if (symbol_conf.symfs[0] != 0)
1919 kallsyms_allocated_filename = dso__find_kallsyms(dso, map);
1920 if (!kallsyms_allocated_filename)
1923 kallsyms_filename = kallsyms_allocated_filename;
1926 err = dso__load_kallsyms(dso, kallsyms_filename, map);
1928 pr_debug("Using %s for symbols\n", kallsyms_filename);
1929 free(kallsyms_allocated_filename);
1931 if (err > 0 && !dso__is_kcore(dso)) {
1932 dso->binary_type = DSO_BINARY_TYPE__KALLSYMS;
1933 dso__set_long_name(dso, DSO__NAME_KALLSYMS, false);
1934 map__fixup_start(map);
1935 map__fixup_end(map);
1941 static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map)
1944 const char *kallsyms_filename = NULL;
1945 struct machine *machine;
1946 char path[PATH_MAX];
1949 pr_debug("Guest kernel map hasn't the point to groups\n");
1952 machine = map->groups->machine;
1954 if (machine__is_default_guest(machine)) {
1956 * if the user specified a vmlinux filename, use it and only
1957 * it, reporting errors to the user if it cannot be used.
1958 * Or use file guest_kallsyms inputted by user on commandline
1960 if (symbol_conf.default_guest_vmlinux_name != NULL) {
1961 err = dso__load_vmlinux(dso, map,
1962 symbol_conf.default_guest_vmlinux_name,
1967 kallsyms_filename = symbol_conf.default_guest_kallsyms;
1968 if (!kallsyms_filename)
1971 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
1972 kallsyms_filename = path;
1975 err = dso__load_kallsyms(dso, kallsyms_filename, map);
1977 pr_debug("Using %s for symbols\n", kallsyms_filename);
1978 if (err > 0 && !dso__is_kcore(dso)) {
1979 dso->binary_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
1980 dso__set_long_name(dso, machine->mmap_name, false);
1981 map__fixup_start(map);
1982 map__fixup_end(map);
1988 static void vmlinux_path__exit(void)
1990 while (--vmlinux_path__nr_entries >= 0)
1991 zfree(&vmlinux_path[vmlinux_path__nr_entries]);
1992 vmlinux_path__nr_entries = 0;
1994 zfree(&vmlinux_path);
1997 static const char * const vmlinux_paths[] = {
2002 static const char * const vmlinux_paths_upd[] = {
2004 "/usr/lib/debug/boot/vmlinux-%s",
2005 "/lib/modules/%s/build/vmlinux",
2006 "/usr/lib/debug/lib/modules/%s/vmlinux",
2007 "/usr/lib/debug/boot/vmlinux-%s.debug"
2010 static int vmlinux_path__add(const char *new_entry)
2012 vmlinux_path[vmlinux_path__nr_entries] = strdup(new_entry);
2013 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2015 ++vmlinux_path__nr_entries;
2020 static int vmlinux_path__init(struct perf_env *env)
2024 char *kernel_version;
2027 vmlinux_path = malloc(sizeof(char *) * (ARRAY_SIZE(vmlinux_paths) +
2028 ARRAY_SIZE(vmlinux_paths_upd)));
2029 if (vmlinux_path == NULL)
2032 for (i = 0; i < ARRAY_SIZE(vmlinux_paths); i++)
2033 if (vmlinux_path__add(vmlinux_paths[i]) < 0)
2036 /* only try kernel version if no symfs was given */
2037 if (symbol_conf.symfs[0] != 0)
2041 kernel_version = env->os_release;
2043 if (uname(&uts) < 0)
2046 kernel_version = uts.release;
2049 for (i = 0; i < ARRAY_SIZE(vmlinux_paths_upd); i++) {
2050 snprintf(bf, sizeof(bf), vmlinux_paths_upd[i], kernel_version);
2051 if (vmlinux_path__add(bf) < 0)
2058 vmlinux_path__exit();
2062 int setup_list(struct strlist **list, const char *list_str,
2063 const char *list_name)
2065 if (list_str == NULL)
2068 *list = strlist__new(list_str, NULL);
2070 pr_err("problems parsing %s list\n", list_name);
2074 symbol_conf.has_filter = true;
2078 int setup_intlist(struct intlist **list, const char *list_str,
2079 const char *list_name)
2081 if (list_str == NULL)
2084 *list = intlist__new(list_str);
2086 pr_err("problems parsing %s list\n", list_name);
2092 static bool symbol__read_kptr_restrict(void)
2095 FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
2100 if (fgets(line, sizeof(line), fp) != NULL)
2101 value = ((geteuid() != 0) || (getuid() != 0)) ?
2111 int symbol__annotation_init(void)
2113 if (symbol_conf.init_annotation)
2116 if (symbol_conf.initialized) {
2117 pr_err("Annotation needs to be init before symbol__init()\n");
2121 symbol_conf.priv_size += sizeof(struct annotation);
2122 symbol_conf.init_annotation = true;
2126 int symbol__init(struct perf_env *env)
2130 if (symbol_conf.initialized)
2133 symbol_conf.priv_size = PERF_ALIGN(symbol_conf.priv_size, sizeof(u64));
2137 if (symbol_conf.sort_by_name)
2138 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
2139 sizeof(struct symbol));
2141 if (symbol_conf.try_vmlinux_path && vmlinux_path__init(env) < 0)
2144 if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
2145 pr_err("'.' is the only non valid --field-separator argument\n");
2149 if (setup_list(&symbol_conf.dso_list,
2150 symbol_conf.dso_list_str, "dso") < 0)
2153 if (setup_list(&symbol_conf.comm_list,
2154 symbol_conf.comm_list_str, "comm") < 0)
2155 goto out_free_dso_list;
2157 if (setup_intlist(&symbol_conf.pid_list,
2158 symbol_conf.pid_list_str, "pid") < 0)
2159 goto out_free_comm_list;
2161 if (setup_intlist(&symbol_conf.tid_list,
2162 symbol_conf.tid_list_str, "tid") < 0)
2163 goto out_free_pid_list;
2165 if (setup_list(&symbol_conf.sym_list,
2166 symbol_conf.sym_list_str, "symbol") < 0)
2167 goto out_free_tid_list;
2169 if (setup_list(&symbol_conf.bt_stop_list,
2170 symbol_conf.bt_stop_list_str, "symbol") < 0)
2171 goto out_free_sym_list;
2174 * A path to symbols of "/" is identical to ""
2175 * reset here for simplicity.
2177 symfs = realpath(symbol_conf.symfs, NULL);
2179 symfs = symbol_conf.symfs;
2180 if (strcmp(symfs, "/") == 0)
2181 symbol_conf.symfs = "";
2182 if (symfs != symbol_conf.symfs)
2183 free((void *)symfs);
2185 symbol_conf.kptr_restrict = symbol__read_kptr_restrict();
2187 symbol_conf.initialized = true;
2191 strlist__delete(symbol_conf.sym_list);
2193 intlist__delete(symbol_conf.tid_list);
2195 intlist__delete(symbol_conf.pid_list);
2197 strlist__delete(symbol_conf.comm_list);
2199 strlist__delete(symbol_conf.dso_list);
2203 void symbol__exit(void)
2205 if (!symbol_conf.initialized)
2207 strlist__delete(symbol_conf.bt_stop_list);
2208 strlist__delete(symbol_conf.sym_list);
2209 strlist__delete(symbol_conf.dso_list);
2210 strlist__delete(symbol_conf.comm_list);
2211 intlist__delete(symbol_conf.tid_list);
2212 intlist__delete(symbol_conf.pid_list);
2213 vmlinux_path__exit();
2214 symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
2215 symbol_conf.bt_stop_list = NULL;
2216 symbol_conf.initialized = false;
2219 int symbol__config_symfs(const struct option *opt __maybe_unused,
2220 const char *dir, int unset __maybe_unused)
2225 symbol_conf.symfs = strdup(dir);
2226 if (symbol_conf.symfs == NULL)
2229 /* skip the locally configured cache if a symfs is given, and
2230 * config buildid dir to symfs/.debug
2232 ret = asprintf(&bf, "%s/%s", dir, ".debug");
2236 set_buildid_dir(bf);
2242 struct mem_info *mem_info__get(struct mem_info *mi)
2245 refcount_inc(&mi->refcnt);
2249 void mem_info__put(struct mem_info *mi)
2251 if (mi && refcount_dec_and_test(&mi->refcnt))
2255 struct mem_info *mem_info__new(void)
2257 struct mem_info *mi = zalloc(sizeof(*mi));
2260 refcount_set(&mi->refcnt, 1);