21 #include <sys/utsname.h>
24 #define KSYM_NAME_LEN 256
27 static int dso__load_kernel_sym(struct dso *dso, struct map *map,
28 symbol_filter_t filter);
29 static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
30 symbol_filter_t filter);
31 int vmlinux_path__nr_entries;
34 struct symbol_conf symbol_conf = {
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 struct symbol *symbol__new(u64 start, u64 len, u8 binding, const char *name)
207 size_t namelen = strlen(name) + 1;
208 struct symbol *sym = calloc(1, (symbol_conf.priv_size +
209 sizeof(*sym) + namelen));
213 if (symbol_conf.priv_size)
214 sym = ((void *)sym) + symbol_conf.priv_size;
217 sym->end = len ? start + len - 1 : start;
218 sym->binding = binding;
219 sym->namelen = namelen - 1;
221 pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n",
222 __func__, name, start, sym->end);
223 memcpy(sym->name, name, namelen);
228 void symbol__delete(struct symbol *sym)
230 free(((void *)sym) - symbol_conf.priv_size);
233 size_t symbol__fprintf(struct symbol *sym, FILE *fp)
235 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %c %s\n",
236 sym->start, sym->end,
237 sym->binding == STB_GLOBAL ? 'g' :
238 sym->binding == STB_LOCAL ? 'l' : 'w',
242 size_t symbol__fprintf_symname_offs(const struct symbol *sym,
243 const struct addr_location *al, FILE *fp)
245 unsigned long offset;
248 if (sym && sym->name) {
249 length = fprintf(fp, "%s", sym->name);
251 offset = al->addr - sym->start;
252 length += fprintf(fp, "+0x%lx", offset);
256 return fprintf(fp, "[unknown]");
259 size_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp)
261 return symbol__fprintf_symname_offs(sym, NULL, fp);
264 void symbols__delete(struct rb_root *symbols)
267 struct rb_node *next = rb_first(symbols);
270 pos = rb_entry(next, struct symbol, rb_node);
271 next = rb_next(&pos->rb_node);
272 rb_erase(&pos->rb_node, symbols);
277 void symbols__insert(struct rb_root *symbols, struct symbol *sym)
279 struct rb_node **p = &symbols->rb_node;
280 struct rb_node *parent = NULL;
281 const u64 ip = sym->start;
286 s = rb_entry(parent, struct symbol, rb_node);
292 rb_link_node(&sym->rb_node, parent, p);
293 rb_insert_color(&sym->rb_node, symbols);
296 static struct symbol *symbols__find(struct rb_root *symbols, u64 ip)
303 n = symbols->rb_node;
306 struct symbol *s = rb_entry(n, struct symbol, rb_node);
310 else if (ip > s->end)
319 struct symbol_name_rb_node {
320 struct rb_node rb_node;
324 static void symbols__insert_by_name(struct rb_root *symbols, struct symbol *sym)
326 struct rb_node **p = &symbols->rb_node;
327 struct rb_node *parent = NULL;
328 struct symbol_name_rb_node *symn, *s;
330 symn = container_of(sym, struct symbol_name_rb_node, sym);
334 s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
335 if (strcmp(sym->name, s->sym.name) < 0)
340 rb_link_node(&symn->rb_node, parent, p);
341 rb_insert_color(&symn->rb_node, symbols);
344 static void symbols__sort_by_name(struct rb_root *symbols,
345 struct rb_root *source)
349 for (nd = rb_first(source); nd; nd = rb_next(nd)) {
350 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
351 symbols__insert_by_name(symbols, pos);
355 static struct symbol *symbols__find_by_name(struct rb_root *symbols,
363 n = symbols->rb_node;
366 struct symbol_name_rb_node *s;
369 s = rb_entry(n, struct symbol_name_rb_node, rb_node);
370 cmp = strcmp(name, s->sym.name);
383 struct symbol *dso__find_symbol(struct dso *dso,
384 enum map_type type, u64 addr)
386 return symbols__find(&dso->symbols[type], addr);
389 struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
392 return symbols__find_by_name(&dso->symbol_names[type], name);
395 void dso__sort_by_name(struct dso *dso, enum map_type type)
397 dso__set_sorted_by_name(dso, type);
398 return symbols__sort_by_name(&dso->symbol_names[type],
399 &dso->symbols[type]);
402 size_t dso__fprintf_symbols_by_name(struct dso *dso,
403 enum map_type type, FILE *fp)
407 struct symbol_name_rb_node *pos;
409 for (nd = rb_first(&dso->symbol_names[type]); nd; nd = rb_next(nd)) {
410 pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
411 fprintf(fp, "%s\n", pos->sym.name);
417 int kallsyms__parse(const char *filename, void *arg,
418 int (*process_symbol)(void *arg, const char *name,
419 char type, u64 start))
424 FILE *file = fopen(filename, "r");
431 while (!feof(file)) {
437 line_len = getline(&line, &n, file);
438 if (line_len < 0 || !line)
441 line[--line_len] = '\0'; /* \n */
443 len = hex2u64(line, &start);
446 if (len + 2 >= line_len)
449 symbol_type = line[len];
451 symbol_name = line + len;
452 len = line_len - len;
454 if (len >= KSYM_NAME_LEN) {
459 err = process_symbol(arg, symbol_name,
473 struct process_kallsyms_args {
478 static u8 kallsyms2elf_type(char type)
483 return isupper(type) ? STB_GLOBAL : STB_LOCAL;
486 static int map__process_kallsym_symbol(void *arg, const char *name,
487 char type, u64 start)
490 struct process_kallsyms_args *a = arg;
491 struct rb_root *root = &a->dso->symbols[a->map->type];
493 if (!symbol_type__is_a(type, a->map->type))
497 * module symbols are not sorted so we add all
498 * symbols, setting length to 0, and rely on
499 * symbols__fixup_end() to fix it up.
501 sym = symbol__new(start, 0, kallsyms2elf_type(type), name);
505 * We will pass the symbols to the filter later, in
506 * map__split_kallsyms, when we have split the maps per module
508 symbols__insert(root, sym);
514 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
515 * so that we can in the next step set the symbol ->end address and then
516 * call kernel_maps__split_kallsyms.
518 static int dso__load_all_kallsyms(struct dso *dso, const char *filename,
521 struct process_kallsyms_args args = { .map = map, .dso = dso, };
522 return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
526 * Split the symbols into maps, making sure there are no overlaps, i.e. the
527 * kernel range is broken in several maps, named [kernel].N, as we don't have
528 * the original ELF section names vmlinux have.
530 static int dso__split_kallsyms(struct dso *dso, struct map *map,
531 symbol_filter_t filter)
533 struct map_groups *kmaps = map__kmap(map)->kmaps;
534 struct machine *machine = kmaps->machine;
535 struct map *curr_map = map;
537 int count = 0, moved = 0;
538 struct rb_root *root = &dso->symbols[map->type];
539 struct rb_node *next = rb_first(root);
540 int kernel_range = 0;
545 pos = rb_entry(next, struct symbol, rb_node);
546 next = rb_next(&pos->rb_node);
548 module = strchr(pos->name, '\t');
550 if (!symbol_conf.use_modules)
555 if (strcmp(curr_map->dso->short_name, module)) {
556 if (curr_map != map &&
557 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
558 machine__is_default_guest(machine)) {
560 * We assume all symbols of a module are
561 * continuous in * kallsyms, so curr_map
562 * points to a module and all its
563 * symbols are in its kmap. Mark it as
566 dso__set_loaded(curr_map->dso,
570 curr_map = map_groups__find_by_name(kmaps,
572 if (curr_map == NULL) {
573 pr_debug("%s/proc/{kallsyms,modules} "
574 "inconsistency while looking "
575 "for \"%s\" module!\n",
576 machine->root_dir, module);
581 if (curr_map->dso->loaded &&
582 !machine__is_default_guest(machine))
586 * So that we look just like we get from .ko files,
587 * i.e. not prelinked, relative to map->start.
589 pos->start = curr_map->map_ip(curr_map, pos->start);
590 pos->end = curr_map->map_ip(curr_map, pos->end);
591 } else if (curr_map != map) {
592 char dso_name[PATH_MAX];
600 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
601 snprintf(dso_name, sizeof(dso_name),
605 snprintf(dso_name, sizeof(dso_name),
609 ndso = dso__new(dso_name);
613 ndso->kernel = dso->kernel;
615 curr_map = map__new2(pos->start, ndso, map->type);
616 if (curr_map == NULL) {
621 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
622 map_groups__insert(kmaps, curr_map);
626 if (filter && filter(curr_map, pos)) {
627 discard_symbol: rb_erase(&pos->rb_node, root);
630 if (curr_map != map) {
631 rb_erase(&pos->rb_node, root);
632 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
639 if (curr_map != map &&
640 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
641 machine__is_default_guest(kmaps->machine)) {
642 dso__set_loaded(curr_map->dso, curr_map->type);
645 return count + moved;
648 bool symbol__restricted_filename(const char *filename,
649 const char *restricted_filename)
651 bool restricted = false;
653 if (symbol_conf.kptr_restrict) {
654 char *r = realpath(filename, NULL);
657 restricted = strcmp(r, restricted_filename) == 0;
666 int dso__load_kallsyms(struct dso *dso, const char *filename,
667 struct map *map, symbol_filter_t filter)
669 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
672 if (dso__load_all_kallsyms(dso, filename, map) < 0)
675 symbols__fixup_duplicate(&dso->symbols[map->type]);
676 symbols__fixup_end(&dso->symbols[map->type]);
678 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
679 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
681 dso->symtab_type = DSO_BINARY_TYPE__KALLSYMS;
683 return dso__split_kallsyms(dso, map, filter);
686 static int dso__load_perf_map(struct dso *dso, struct map *map,
687 symbol_filter_t filter)
694 file = fopen(dso->long_name, "r");
698 while (!feof(file)) {
703 line_len = getline(&line, &n, file);
710 line[--line_len] = '\0'; /* \n */
712 len = hex2u64(line, &start);
715 if (len + 2 >= line_len)
718 len += hex2u64(line + len, &size);
721 if (len + 2 >= line_len)
724 sym = symbol__new(start, size, STB_GLOBAL, line + len);
727 goto out_delete_line;
729 if (filter && filter(map, sym))
732 symbols__insert(&dso->symbols[map->type], sym);
748 int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter)
753 struct machine *machine;
754 char *root_dir = (char *) "";
756 struct symsrc ss_[2];
757 struct symsrc *syms_ss = NULL, *runtime_ss = NULL;
759 dso__set_loaded(dso, map->type);
761 if (dso->kernel == DSO_TYPE_KERNEL)
762 return dso__load_kernel_sym(dso, map, filter);
763 else if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
764 return dso__load_guest_kernel_sym(dso, map, filter);
766 if (map->groups && map->groups->machine)
767 machine = map->groups->machine;
771 dso->adjust_symbols = 0;
773 if (strncmp(dso->name, "/tmp/perf-", 10) == 0) {
776 if (lstat(dso->name, &st) < 0)
779 if (st.st_uid && (st.st_uid != geteuid())) {
780 pr_warning("File %s not owned by current user or root, "
781 "ignoring it.\n", dso->name);
785 ret = dso__load_perf_map(dso, map, filter);
786 dso->symtab_type = ret > 0 ? DSO_BINARY_TYPE__JAVA_JIT :
787 DSO_BINARY_TYPE__NOT_FOUND;
792 root_dir = machine->root_dir;
794 name = malloc(PATH_MAX);
798 /* Iterate over candidate debug images.
799 * Keep track of "interesting" ones (those which have a symtab, dynsym,
800 * and/or opd section) for processing.
802 for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) {
803 struct symsrc *ss = &ss_[ss_pos];
804 bool next_slot = false;
806 enum dso_binary_type symtab_type = binary_type_symtab[i];
808 if (dso__binary_type_file(dso, symtab_type,
809 root_dir, name, PATH_MAX))
812 /* Name is now the name of the next image to try */
813 if (symsrc__init(ss, dso, name, symtab_type) < 0)
816 if (!syms_ss && symsrc__has_symtab(ss)) {
821 if (!runtime_ss && symsrc__possibly_runtime(ss)) {
829 if (syms_ss && runtime_ss)
835 if (!runtime_ss && !syms_ss)
838 if (runtime_ss && !syms_ss) {
839 syms_ss = runtime_ss;
842 /* We'll have to hope for the best */
843 if (!runtime_ss && syms_ss)
844 runtime_ss = syms_ss;
847 ret = dso__load_sym(dso, map, syms_ss, runtime_ss, filter, 0);
854 nr_plt = dso__synthesize_plt_symbols(dso, runtime_ss, map, filter);
859 for (; ss_pos > 0; ss_pos--)
860 symsrc__destroy(&ss_[ss_pos - 1]);
863 if (ret < 0 && strstr(dso->name, " (deleted)") != NULL)
868 struct map *map_groups__find_by_name(struct map_groups *mg,
869 enum map_type type, const char *name)
873 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
874 struct map *map = rb_entry(nd, struct map, rb_node);
876 if (map->dso && strcmp(map->dso->short_name, name) == 0)
883 int dso__load_vmlinux(struct dso *dso, struct map *map,
884 const char *vmlinux, symbol_filter_t filter)
888 char symfs_vmlinux[PATH_MAX];
889 enum dso_binary_type symtab_type;
891 snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s%s",
892 symbol_conf.symfs, vmlinux);
894 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
895 symtab_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
897 symtab_type = DSO_BINARY_TYPE__VMLINUX;
899 if (symsrc__init(&ss, dso, symfs_vmlinux, symtab_type))
902 err = dso__load_sym(dso, map, &ss, &ss, filter, 0);
903 symsrc__destroy(&ss);
906 dso__set_long_name(dso, (char *)vmlinux);
907 dso__set_loaded(dso, map->type);
908 pr_debug("Using %s for symbols\n", symfs_vmlinux);
914 int dso__load_vmlinux_path(struct dso *dso, struct map *map,
915 symbol_filter_t filter)
920 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
921 vmlinux_path__nr_entries + 1);
923 filename = dso__build_id_filename(dso, NULL, 0);
924 if (filename != NULL) {
925 err = dso__load_vmlinux(dso, map, filename, filter);
927 dso->lname_alloc = 1;
933 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
934 err = dso__load_vmlinux(dso, map, vmlinux_path[i], filter);
936 dso__set_long_name(dso, strdup(vmlinux_path[i]));
937 dso->lname_alloc = 1;
945 static int dso__load_kernel_sym(struct dso *dso, struct map *map,
946 symbol_filter_t filter)
949 const char *kallsyms_filename = NULL;
950 char *kallsyms_allocated_filename = NULL;
952 * Step 1: if the user specified a kallsyms or vmlinux filename, use
953 * it and only it, reporting errors to the user if it cannot be used.
955 * For instance, try to analyse an ARM perf.data file _without_ a
956 * build-id, or if the user specifies the wrong path to the right
957 * vmlinux file, obviously we can't fallback to another vmlinux (a
958 * x86_86 one, on the machine where analysis is being performed, say),
959 * or worse, /proc/kallsyms.
961 * If the specified file _has_ a build-id and there is a build-id
962 * section in the perf.data file, we will still do the expected
963 * validation in dso__load_vmlinux and will bail out if they don't
966 if (symbol_conf.kallsyms_name != NULL) {
967 kallsyms_filename = symbol_conf.kallsyms_name;
971 if (symbol_conf.vmlinux_name != NULL) {
972 err = dso__load_vmlinux(dso, map,
973 symbol_conf.vmlinux_name, filter);
975 dso__set_long_name(dso,
976 strdup(symbol_conf.vmlinux_name));
977 dso->lname_alloc = 1;
983 if (vmlinux_path != NULL) {
984 err = dso__load_vmlinux_path(dso, map, filter);
989 /* do not try local files if a symfs was given */
990 if (symbol_conf.symfs[0] != 0)
994 * Say the kernel DSO was created when processing the build-id header table,
995 * we have a build-id, so check if it is the same as the running kernel,
998 if (dso->has_build_id) {
999 u8 kallsyms_build_id[BUILD_ID_SIZE];
1000 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1002 if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id,
1003 sizeof(kallsyms_build_id)) == 0) {
1004 if (dso__build_id_equal(dso, kallsyms_build_id)) {
1005 kallsyms_filename = "/proc/kallsyms";
1010 * Now look if we have it on the build-id cache in
1011 * $HOME/.debug/[kernel.kallsyms].
1013 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1016 if (asprintf(&kallsyms_allocated_filename,
1017 "%s/.debug/[kernel.kallsyms]/%s",
1018 getenv("HOME"), sbuild_id) == -1) {
1019 pr_err("Not enough memory for kallsyms file lookup\n");
1023 kallsyms_filename = kallsyms_allocated_filename;
1025 if (access(kallsyms_filename, F_OK)) {
1026 pr_err("No kallsyms or vmlinux with build-id %s "
1027 "was found\n", sbuild_id);
1028 free(kallsyms_allocated_filename);
1033 * Last resort, if we don't have a build-id and couldn't find
1034 * any vmlinux file, try the running kernel kallsyms table.
1036 kallsyms_filename = "/proc/kallsyms";
1040 err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
1042 pr_debug("Using %s for symbols\n", kallsyms_filename);
1043 free(kallsyms_allocated_filename);
1046 dso__set_long_name(dso, strdup("[kernel.kallsyms]"));
1048 map__fixup_start(map);
1049 map__fixup_end(map);
1055 static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
1056 symbol_filter_t filter)
1059 const char *kallsyms_filename = NULL;
1060 struct machine *machine;
1061 char path[PATH_MAX];
1064 pr_debug("Guest kernel map hasn't the point to groups\n");
1067 machine = map->groups->machine;
1069 if (machine__is_default_guest(machine)) {
1071 * if the user specified a vmlinux filename, use it and only
1072 * it, reporting errors to the user if it cannot be used.
1073 * Or use file guest_kallsyms inputted by user on commandline
1075 if (symbol_conf.default_guest_vmlinux_name != NULL) {
1076 err = dso__load_vmlinux(dso, map,
1077 symbol_conf.default_guest_vmlinux_name, filter);
1081 kallsyms_filename = symbol_conf.default_guest_kallsyms;
1082 if (!kallsyms_filename)
1085 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
1086 kallsyms_filename = path;
1089 err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
1091 pr_debug("Using %s for symbols\n", kallsyms_filename);
1095 if (kallsyms_filename != NULL) {
1096 machine__mmap_name(machine, path, sizeof(path));
1097 dso__set_long_name(dso, strdup(path));
1099 map__fixup_start(map);
1100 map__fixup_end(map);
1106 static void vmlinux_path__exit(void)
1108 while (--vmlinux_path__nr_entries >= 0) {
1109 free(vmlinux_path[vmlinux_path__nr_entries]);
1110 vmlinux_path[vmlinux_path__nr_entries] = NULL;
1114 vmlinux_path = NULL;
1117 static int vmlinux_path__init(void)
1122 vmlinux_path = malloc(sizeof(char *) * 5);
1123 if (vmlinux_path == NULL)
1126 vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
1127 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1129 ++vmlinux_path__nr_entries;
1130 vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
1131 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1133 ++vmlinux_path__nr_entries;
1135 /* only try running kernel version if no symfs was given */
1136 if (symbol_conf.symfs[0] != 0)
1139 if (uname(&uts) < 0)
1142 snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
1143 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1144 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1146 ++vmlinux_path__nr_entries;
1147 snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
1148 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1149 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1151 ++vmlinux_path__nr_entries;
1152 snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
1154 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1155 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1157 ++vmlinux_path__nr_entries;
1162 vmlinux_path__exit();
1166 static int setup_list(struct strlist **list, const char *list_str,
1167 const char *list_name)
1169 if (list_str == NULL)
1172 *list = strlist__new(true, list_str);
1174 pr_err("problems parsing %s list\n", list_name);
1180 static bool symbol__read_kptr_restrict(void)
1184 if (geteuid() != 0) {
1185 FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
1189 if (fgets(line, sizeof(line), fp) != NULL)
1190 value = atoi(line) != 0;
1199 int symbol__init(void)
1203 if (symbol_conf.initialized)
1206 symbol_conf.priv_size = PERF_ALIGN(symbol_conf.priv_size, sizeof(u64));
1210 if (symbol_conf.sort_by_name)
1211 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
1212 sizeof(struct symbol));
1214 if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
1217 if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
1218 pr_err("'.' is the only non valid --field-separator argument\n");
1222 if (setup_list(&symbol_conf.dso_list,
1223 symbol_conf.dso_list_str, "dso") < 0)
1226 if (setup_list(&symbol_conf.comm_list,
1227 symbol_conf.comm_list_str, "comm") < 0)
1228 goto out_free_dso_list;
1230 if (setup_list(&symbol_conf.sym_list,
1231 symbol_conf.sym_list_str, "symbol") < 0)
1232 goto out_free_comm_list;
1235 * A path to symbols of "/" is identical to ""
1236 * reset here for simplicity.
1238 symfs = realpath(symbol_conf.symfs, NULL);
1240 symfs = symbol_conf.symfs;
1241 if (strcmp(symfs, "/") == 0)
1242 symbol_conf.symfs = "";
1243 if (symfs != symbol_conf.symfs)
1244 free((void *)symfs);
1246 symbol_conf.kptr_restrict = symbol__read_kptr_restrict();
1248 symbol_conf.initialized = true;
1252 strlist__delete(symbol_conf.comm_list);
1254 strlist__delete(symbol_conf.dso_list);
1258 void symbol__exit(void)
1260 if (!symbol_conf.initialized)
1262 strlist__delete(symbol_conf.sym_list);
1263 strlist__delete(symbol_conf.dso_list);
1264 strlist__delete(symbol_conf.comm_list);
1265 vmlinux_path__exit();
1266 symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
1267 symbol_conf.initialized = false;