Merge tag 'locks-v3.16-2' of git://git.samba.org/jlayton/linux
[linux/fpc-iii.git] / tools / perf / util / symbol.c
blob7b9096f29cdbf7f8c2afabb041e2563b0e70c0da
1 #include <dirent.h>
2 #include <errno.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <sys/param.h>
9 #include <fcntl.h>
10 #include <unistd.h>
11 #include <inttypes.h>
12 #include "build-id.h"
13 #include "util.h"
14 #include "debug.h"
15 #include "machine.h"
16 #include "symbol.h"
17 #include "strlist.h"
19 #include <elf.h>
20 #include <limits.h>
21 #include <symbol/kallsyms.h>
22 #include <sys/utsname.h>
24 static int dso__load_kernel_sym(struct dso *dso, struct map *map,
25 symbol_filter_t filter);
26 static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
27 symbol_filter_t filter);
28 int vmlinux_path__nr_entries;
29 char **vmlinux_path;
31 struct symbol_conf symbol_conf = {
32 .use_modules = true,
33 .try_vmlinux_path = true,
34 .annotate_src = true,
35 .demangle = true,
36 .cumulate_callchain = true,
37 .symfs = "",
40 static enum dso_binary_type binary_type_symtab[] = {
41 DSO_BINARY_TYPE__KALLSYMS,
42 DSO_BINARY_TYPE__GUEST_KALLSYMS,
43 DSO_BINARY_TYPE__JAVA_JIT,
44 DSO_BINARY_TYPE__DEBUGLINK,
45 DSO_BINARY_TYPE__BUILD_ID_CACHE,
46 DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
47 DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
48 DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
49 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
50 DSO_BINARY_TYPE__GUEST_KMODULE,
51 DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE,
52 DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO,
53 DSO_BINARY_TYPE__NOT_FOUND,
56 #define DSO_BINARY_TYPE__SYMTAB_CNT ARRAY_SIZE(binary_type_symtab)
58 bool symbol_type__is_a(char symbol_type, enum map_type map_type)
60 symbol_type = toupper(symbol_type);
62 switch (map_type) {
63 case MAP__FUNCTION:
64 return symbol_type == 'T' || symbol_type == 'W';
65 case MAP__VARIABLE:
66 return symbol_type == 'D';
67 default:
68 return false;
72 static int prefix_underscores_count(const char *str)
74 const char *tail = str;
76 while (*tail == '_')
77 tail++;
79 return tail - str;
82 #define SYMBOL_A 0
83 #define SYMBOL_B 1
85 static int choose_best_symbol(struct symbol *syma, struct symbol *symb)
87 s64 a;
88 s64 b;
89 size_t na, nb;
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))
95 return SYMBOL_A;
96 else if ((a == 0) && (b > 0))
97 return SYMBOL_B;
99 /* Prefer a non weak symbol over a weak one */
100 a = syma->binding == STB_WEAK;
101 b = symb->binding == STB_WEAK;
102 if (b && !a)
103 return SYMBOL_A;
104 if (a && !b)
105 return SYMBOL_B;
107 /* Prefer a global symbol over a non global one */
108 a = syma->binding == STB_GLOBAL;
109 b = symb->binding == STB_GLOBAL;
110 if (a && !b)
111 return SYMBOL_A;
112 if (b && !a)
113 return SYMBOL_B;
115 /* Prefer a symbol with less underscores */
116 a = prefix_underscores_count(syma->name);
117 b = prefix_underscores_count(symb->name);
118 if (b > a)
119 return SYMBOL_A;
120 else if (a > b)
121 return SYMBOL_B;
123 /* Choose the symbol with the longest name */
124 na = strlen(syma->name);
125 nb = strlen(symb->name);
126 if (na > nb)
127 return SYMBOL_A;
128 else if (na < nb)
129 return SYMBOL_B;
131 /* Avoid "SyS" kernel syscall aliases */
132 if (na >= 3 && !strncmp(syma->name, "SyS", 3))
133 return SYMBOL_B;
134 if (na >= 10 && !strncmp(syma->name, "compat_SyS", 10))
135 return SYMBOL_B;
137 return SYMBOL_A;
140 void symbols__fixup_duplicate(struct rb_root *symbols)
142 struct rb_node *nd;
143 struct symbol *curr, *next;
145 nd = rb_first(symbols);
147 while (nd) {
148 curr = rb_entry(nd, struct symbol, rb_node);
149 again:
150 nd = rb_next(&curr->rb_node);
151 next = rb_entry(nd, struct symbol, rb_node);
153 if (!nd)
154 break;
156 if (curr->start != next->start)
157 continue;
159 if (choose_best_symbol(curr, next) == SYMBOL_A) {
160 rb_erase(&next->rb_node, symbols);
161 symbol__delete(next);
162 goto again;
163 } else {
164 nd = rb_next(&curr->rb_node);
165 rb_erase(&curr->rb_node, symbols);
166 symbol__delete(curr);
171 void symbols__fixup_end(struct rb_root *symbols)
173 struct rb_node *nd, *prevnd = rb_first(symbols);
174 struct symbol *curr, *prev;
176 if (prevnd == NULL)
177 return;
179 curr = rb_entry(prevnd, struct symbol, rb_node);
181 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
182 prev = curr;
183 curr = rb_entry(nd, struct symbol, rb_node);
185 if (prev->end == prev->start && prev->end != curr->start)
186 prev->end = curr->start - 1;
189 /* Last entry */
190 if (curr->end == curr->start)
191 curr->end = roundup(curr->start, 4096);
194 void __map_groups__fixup_end(struct map_groups *mg, enum map_type type)
196 struct map *prev, *curr;
197 struct rb_node *nd, *prevnd = rb_first(&mg->maps[type]);
199 if (prevnd == NULL)
200 return;
202 curr = rb_entry(prevnd, struct map, rb_node);
204 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
205 prev = curr;
206 curr = rb_entry(nd, struct map, rb_node);
207 prev->end = curr->start - 1;
211 * We still haven't the actual symbols, so guess the
212 * last map final address.
214 curr->end = ~0ULL;
217 struct symbol *symbol__new(u64 start, u64 len, u8 binding, const char *name)
219 size_t namelen = strlen(name) + 1;
220 struct symbol *sym = calloc(1, (symbol_conf.priv_size +
221 sizeof(*sym) + namelen));
222 if (sym == NULL)
223 return NULL;
225 if (symbol_conf.priv_size)
226 sym = ((void *)sym) + symbol_conf.priv_size;
228 sym->start = start;
229 sym->end = len ? start + len - 1 : start;
230 sym->binding = binding;
231 sym->namelen = namelen - 1;
233 pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n",
234 __func__, name, start, sym->end);
235 memcpy(sym->name, name, namelen);
237 return sym;
240 void symbol__delete(struct symbol *sym)
242 free(((void *)sym) - symbol_conf.priv_size);
245 size_t symbol__fprintf(struct symbol *sym, FILE *fp)
247 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %c %s\n",
248 sym->start, sym->end,
249 sym->binding == STB_GLOBAL ? 'g' :
250 sym->binding == STB_LOCAL ? 'l' : 'w',
251 sym->name);
254 size_t symbol__fprintf_symname_offs(const struct symbol *sym,
255 const struct addr_location *al, FILE *fp)
257 unsigned long offset;
258 size_t length;
260 if (sym && sym->name) {
261 length = fprintf(fp, "%s", sym->name);
262 if (al) {
263 if (al->addr < sym->end)
264 offset = al->addr - sym->start;
265 else
266 offset = al->addr - al->map->start - sym->start;
267 length += fprintf(fp, "+0x%lx", offset);
269 return length;
270 } else
271 return fprintf(fp, "[unknown]");
274 size_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp)
276 return symbol__fprintf_symname_offs(sym, NULL, fp);
279 void symbols__delete(struct rb_root *symbols)
281 struct symbol *pos;
282 struct rb_node *next = rb_first(symbols);
284 while (next) {
285 pos = rb_entry(next, struct symbol, rb_node);
286 next = rb_next(&pos->rb_node);
287 rb_erase(&pos->rb_node, symbols);
288 symbol__delete(pos);
292 void symbols__insert(struct rb_root *symbols, struct symbol *sym)
294 struct rb_node **p = &symbols->rb_node;
295 struct rb_node *parent = NULL;
296 const u64 ip = sym->start;
297 struct symbol *s;
299 while (*p != NULL) {
300 parent = *p;
301 s = rb_entry(parent, struct symbol, rb_node);
302 if (ip < s->start)
303 p = &(*p)->rb_left;
304 else
305 p = &(*p)->rb_right;
307 rb_link_node(&sym->rb_node, parent, p);
308 rb_insert_color(&sym->rb_node, symbols);
311 static struct symbol *symbols__find(struct rb_root *symbols, u64 ip)
313 struct rb_node *n;
315 if (symbols == NULL)
316 return NULL;
318 n = symbols->rb_node;
320 while (n) {
321 struct symbol *s = rb_entry(n, struct symbol, rb_node);
323 if (ip < s->start)
324 n = n->rb_left;
325 else if (ip > s->end)
326 n = n->rb_right;
327 else
328 return s;
331 return NULL;
334 static struct symbol *symbols__first(struct rb_root *symbols)
336 struct rb_node *n = rb_first(symbols);
338 if (n)
339 return rb_entry(n, struct symbol, rb_node);
341 return NULL;
344 struct symbol_name_rb_node {
345 struct rb_node rb_node;
346 struct symbol sym;
349 static void symbols__insert_by_name(struct rb_root *symbols, struct symbol *sym)
351 struct rb_node **p = &symbols->rb_node;
352 struct rb_node *parent = NULL;
353 struct symbol_name_rb_node *symn, *s;
355 symn = container_of(sym, struct symbol_name_rb_node, sym);
357 while (*p != NULL) {
358 parent = *p;
359 s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
360 if (strcmp(sym->name, s->sym.name) < 0)
361 p = &(*p)->rb_left;
362 else
363 p = &(*p)->rb_right;
365 rb_link_node(&symn->rb_node, parent, p);
366 rb_insert_color(&symn->rb_node, symbols);
369 static void symbols__sort_by_name(struct rb_root *symbols,
370 struct rb_root *source)
372 struct rb_node *nd;
374 for (nd = rb_first(source); nd; nd = rb_next(nd)) {
375 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
376 symbols__insert_by_name(symbols, pos);
380 static struct symbol *symbols__find_by_name(struct rb_root *symbols,
381 const char *name)
383 struct rb_node *n;
385 if (symbols == NULL)
386 return NULL;
388 n = symbols->rb_node;
390 while (n) {
391 struct symbol_name_rb_node *s;
392 int cmp;
394 s = rb_entry(n, struct symbol_name_rb_node, rb_node);
395 cmp = strcmp(name, s->sym.name);
397 if (cmp < 0)
398 n = n->rb_left;
399 else if (cmp > 0)
400 n = n->rb_right;
401 else
402 return &s->sym;
405 return NULL;
408 struct symbol *dso__find_symbol(struct dso *dso,
409 enum map_type type, u64 addr)
411 return symbols__find(&dso->symbols[type], addr);
414 static struct symbol *dso__first_symbol(struct dso *dso, enum map_type type)
416 return symbols__first(&dso->symbols[type]);
419 struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
420 const char *name)
422 return symbols__find_by_name(&dso->symbol_names[type], name);
425 void dso__sort_by_name(struct dso *dso, enum map_type type)
427 dso__set_sorted_by_name(dso, type);
428 return symbols__sort_by_name(&dso->symbol_names[type],
429 &dso->symbols[type]);
432 size_t dso__fprintf_symbols_by_name(struct dso *dso,
433 enum map_type type, FILE *fp)
435 size_t ret = 0;
436 struct rb_node *nd;
437 struct symbol_name_rb_node *pos;
439 for (nd = rb_first(&dso->symbol_names[type]); nd; nd = rb_next(nd)) {
440 pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
441 fprintf(fp, "%s\n", pos->sym.name);
444 return ret;
447 int modules__parse(const char *filename, void *arg,
448 int (*process_module)(void *arg, const char *name,
449 u64 start))
451 char *line = NULL;
452 size_t n;
453 FILE *file;
454 int err = 0;
456 file = fopen(filename, "r");
457 if (file == NULL)
458 return -1;
460 while (1) {
461 char name[PATH_MAX];
462 u64 start;
463 char *sep;
464 ssize_t line_len;
466 line_len = getline(&line, &n, file);
467 if (line_len < 0) {
468 if (feof(file))
469 break;
470 err = -1;
471 goto out;
474 if (!line) {
475 err = -1;
476 goto out;
479 line[--line_len] = '\0'; /* \n */
481 sep = strrchr(line, 'x');
482 if (sep == NULL)
483 continue;
485 hex2u64(sep + 1, &start);
487 sep = strchr(line, ' ');
488 if (sep == NULL)
489 continue;
491 *sep = '\0';
493 scnprintf(name, sizeof(name), "[%s]", line);
495 err = process_module(arg, name, start);
496 if (err)
497 break;
499 out:
500 free(line);
501 fclose(file);
502 return err;
505 struct process_kallsyms_args {
506 struct map *map;
507 struct dso *dso;
510 bool symbol__is_idle(struct symbol *sym)
512 const char * const idle_symbols[] = {
513 "cpu_idle",
514 "intel_idle",
515 "default_idle",
516 "native_safe_halt",
517 "enter_idle",
518 "exit_idle",
519 "mwait_idle",
520 "mwait_idle_with_hints",
521 "poll_idle",
522 "ppc64_runlatch_off",
523 "pseries_dedicated_idle_sleep",
524 NULL
527 int i;
529 if (!sym)
530 return false;
532 for (i = 0; idle_symbols[i]; i++) {
533 if (!strcmp(idle_symbols[i], sym->name))
534 return true;
537 return false;
540 static int map__process_kallsym_symbol(void *arg, const char *name,
541 char type, u64 start)
543 struct symbol *sym;
544 struct process_kallsyms_args *a = arg;
545 struct rb_root *root = &a->dso->symbols[a->map->type];
547 if (!symbol_type__is_a(type, a->map->type))
548 return 0;
551 * module symbols are not sorted so we add all
552 * symbols, setting length to 0, and rely on
553 * symbols__fixup_end() to fix it up.
555 sym = symbol__new(start, 0, kallsyms2elf_type(type), name);
556 if (sym == NULL)
557 return -ENOMEM;
559 * We will pass the symbols to the filter later, in
560 * map__split_kallsyms, when we have split the maps per module
562 symbols__insert(root, sym);
564 return 0;
568 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
569 * so that we can in the next step set the symbol ->end address and then
570 * call kernel_maps__split_kallsyms.
572 static int dso__load_all_kallsyms(struct dso *dso, const char *filename,
573 struct map *map)
575 struct process_kallsyms_args args = { .map = map, .dso = dso, };
576 return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
579 static int dso__split_kallsyms_for_kcore(struct dso *dso, struct map *map,
580 symbol_filter_t filter)
582 struct map_groups *kmaps = map__kmap(map)->kmaps;
583 struct map *curr_map;
584 struct symbol *pos;
585 int count = 0, moved = 0;
586 struct rb_root *root = &dso->symbols[map->type];
587 struct rb_node *next = rb_first(root);
589 while (next) {
590 char *module;
592 pos = rb_entry(next, struct symbol, rb_node);
593 next = rb_next(&pos->rb_node);
595 module = strchr(pos->name, '\t');
596 if (module)
597 *module = '\0';
599 curr_map = map_groups__find(kmaps, map->type, pos->start);
601 if (!curr_map || (filter && filter(curr_map, pos))) {
602 rb_erase(&pos->rb_node, root);
603 symbol__delete(pos);
604 } else {
605 pos->start -= curr_map->start - curr_map->pgoff;
606 if (pos->end)
607 pos->end -= curr_map->start - curr_map->pgoff;
608 if (curr_map != map) {
609 rb_erase(&pos->rb_node, root);
610 symbols__insert(
611 &curr_map->dso->symbols[curr_map->type],
612 pos);
613 ++moved;
614 } else {
615 ++count;
620 /* Symbols have been adjusted */
621 dso->adjust_symbols = 1;
623 return count + moved;
627 * Split the symbols into maps, making sure there are no overlaps, i.e. the
628 * kernel range is broken in several maps, named [kernel].N, as we don't have
629 * the original ELF section names vmlinux have.
631 static int dso__split_kallsyms(struct dso *dso, struct map *map, u64 delta,
632 symbol_filter_t filter)
634 struct map_groups *kmaps = map__kmap(map)->kmaps;
635 struct machine *machine = kmaps->machine;
636 struct map *curr_map = map;
637 struct symbol *pos;
638 int count = 0, moved = 0;
639 struct rb_root *root = &dso->symbols[map->type];
640 struct rb_node *next = rb_first(root);
641 int kernel_range = 0;
643 while (next) {
644 char *module;
646 pos = rb_entry(next, struct symbol, rb_node);
647 next = rb_next(&pos->rb_node);
649 module = strchr(pos->name, '\t');
650 if (module) {
651 if (!symbol_conf.use_modules)
652 goto discard_symbol;
654 *module++ = '\0';
656 if (strcmp(curr_map->dso->short_name, module)) {
657 if (curr_map != map &&
658 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
659 machine__is_default_guest(machine)) {
661 * We assume all symbols of a module are
662 * continuous in * kallsyms, so curr_map
663 * points to a module and all its
664 * symbols are in its kmap. Mark it as
665 * loaded.
667 dso__set_loaded(curr_map->dso,
668 curr_map->type);
671 curr_map = map_groups__find_by_name(kmaps,
672 map->type, module);
673 if (curr_map == NULL) {
674 pr_debug("%s/proc/{kallsyms,modules} "
675 "inconsistency while looking "
676 "for \"%s\" module!\n",
677 machine->root_dir, module);
678 curr_map = map;
679 goto discard_symbol;
682 if (curr_map->dso->loaded &&
683 !machine__is_default_guest(machine))
684 goto discard_symbol;
687 * So that we look just like we get from .ko files,
688 * i.e. not prelinked, relative to map->start.
690 pos->start = curr_map->map_ip(curr_map, pos->start);
691 pos->end = curr_map->map_ip(curr_map, pos->end);
692 } else if (curr_map != map) {
693 char dso_name[PATH_MAX];
694 struct dso *ndso;
696 if (delta) {
697 /* Kernel was relocated at boot time */
698 pos->start -= delta;
699 pos->end -= delta;
702 if (count == 0) {
703 curr_map = map;
704 goto filter_symbol;
707 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
708 snprintf(dso_name, sizeof(dso_name),
709 "[guest.kernel].%d",
710 kernel_range++);
711 else
712 snprintf(dso_name, sizeof(dso_name),
713 "[kernel].%d",
714 kernel_range++);
716 ndso = dso__new(dso_name);
717 if (ndso == NULL)
718 return -1;
720 ndso->kernel = dso->kernel;
722 curr_map = map__new2(pos->start, ndso, map->type);
723 if (curr_map == NULL) {
724 dso__delete(ndso);
725 return -1;
728 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
729 map_groups__insert(kmaps, curr_map);
730 ++kernel_range;
731 } else if (delta) {
732 /* Kernel was relocated at boot time */
733 pos->start -= delta;
734 pos->end -= delta;
736 filter_symbol:
737 if (filter && filter(curr_map, pos)) {
738 discard_symbol: rb_erase(&pos->rb_node, root);
739 symbol__delete(pos);
740 } else {
741 if (curr_map != map) {
742 rb_erase(&pos->rb_node, root);
743 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
744 ++moved;
745 } else
746 ++count;
750 if (curr_map != map &&
751 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
752 machine__is_default_guest(kmaps->machine)) {
753 dso__set_loaded(curr_map->dso, curr_map->type);
756 return count + moved;
759 bool symbol__restricted_filename(const char *filename,
760 const char *restricted_filename)
762 bool restricted = false;
764 if (symbol_conf.kptr_restrict) {
765 char *r = realpath(filename, NULL);
767 if (r != NULL) {
768 restricted = strcmp(r, restricted_filename) == 0;
769 free(r);
770 return restricted;
774 return restricted;
777 struct module_info {
778 struct rb_node rb_node;
779 char *name;
780 u64 start;
783 static void add_module(struct module_info *mi, struct rb_root *modules)
785 struct rb_node **p = &modules->rb_node;
786 struct rb_node *parent = NULL;
787 struct module_info *m;
789 while (*p != NULL) {
790 parent = *p;
791 m = rb_entry(parent, struct module_info, rb_node);
792 if (strcmp(mi->name, m->name) < 0)
793 p = &(*p)->rb_left;
794 else
795 p = &(*p)->rb_right;
797 rb_link_node(&mi->rb_node, parent, p);
798 rb_insert_color(&mi->rb_node, modules);
801 static void delete_modules(struct rb_root *modules)
803 struct module_info *mi;
804 struct rb_node *next = rb_first(modules);
806 while (next) {
807 mi = rb_entry(next, struct module_info, rb_node);
808 next = rb_next(&mi->rb_node);
809 rb_erase(&mi->rb_node, modules);
810 zfree(&mi->name);
811 free(mi);
815 static struct module_info *find_module(const char *name,
816 struct rb_root *modules)
818 struct rb_node *n = modules->rb_node;
820 while (n) {
821 struct module_info *m;
822 int cmp;
824 m = rb_entry(n, struct module_info, rb_node);
825 cmp = strcmp(name, m->name);
826 if (cmp < 0)
827 n = n->rb_left;
828 else if (cmp > 0)
829 n = n->rb_right;
830 else
831 return m;
834 return NULL;
837 static int __read_proc_modules(void *arg, const char *name, u64 start)
839 struct rb_root *modules = arg;
840 struct module_info *mi;
842 mi = zalloc(sizeof(struct module_info));
843 if (!mi)
844 return -ENOMEM;
846 mi->name = strdup(name);
847 mi->start = start;
849 if (!mi->name) {
850 free(mi);
851 return -ENOMEM;
854 add_module(mi, modules);
856 return 0;
859 static int read_proc_modules(const char *filename, struct rb_root *modules)
861 if (symbol__restricted_filename(filename, "/proc/modules"))
862 return -1;
864 if (modules__parse(filename, modules, __read_proc_modules)) {
865 delete_modules(modules);
866 return -1;
869 return 0;
872 int compare_proc_modules(const char *from, const char *to)
874 struct rb_root from_modules = RB_ROOT;
875 struct rb_root to_modules = RB_ROOT;
876 struct rb_node *from_node, *to_node;
877 struct module_info *from_m, *to_m;
878 int ret = -1;
880 if (read_proc_modules(from, &from_modules))
881 return -1;
883 if (read_proc_modules(to, &to_modules))
884 goto out_delete_from;
886 from_node = rb_first(&from_modules);
887 to_node = rb_first(&to_modules);
888 while (from_node) {
889 if (!to_node)
890 break;
892 from_m = rb_entry(from_node, struct module_info, rb_node);
893 to_m = rb_entry(to_node, struct module_info, rb_node);
895 if (from_m->start != to_m->start ||
896 strcmp(from_m->name, to_m->name))
897 break;
899 from_node = rb_next(from_node);
900 to_node = rb_next(to_node);
903 if (!from_node && !to_node)
904 ret = 0;
906 delete_modules(&to_modules);
907 out_delete_from:
908 delete_modules(&from_modules);
910 return ret;
913 static int do_validate_kcore_modules(const char *filename, struct map *map,
914 struct map_groups *kmaps)
916 struct rb_root modules = RB_ROOT;
917 struct map *old_map;
918 int err;
920 err = read_proc_modules(filename, &modules);
921 if (err)
922 return err;
924 old_map = map_groups__first(kmaps, map->type);
925 while (old_map) {
926 struct map *next = map_groups__next(old_map);
927 struct module_info *mi;
929 if (old_map == map || old_map->start == map->start) {
930 /* The kernel map */
931 old_map = next;
932 continue;
935 /* Module must be in memory at the same address */
936 mi = find_module(old_map->dso->short_name, &modules);
937 if (!mi || mi->start != old_map->start) {
938 err = -EINVAL;
939 goto out;
942 old_map = next;
944 out:
945 delete_modules(&modules);
946 return err;
950 * If kallsyms is referenced by name then we look for filename in the same
951 * directory.
953 static bool filename_from_kallsyms_filename(char *filename,
954 const char *base_name,
955 const char *kallsyms_filename)
957 char *name;
959 strcpy(filename, kallsyms_filename);
960 name = strrchr(filename, '/');
961 if (!name)
962 return false;
964 name += 1;
966 if (!strcmp(name, "kallsyms")) {
967 strcpy(name, base_name);
968 return true;
971 return false;
974 static int validate_kcore_modules(const char *kallsyms_filename,
975 struct map *map)
977 struct map_groups *kmaps = map__kmap(map)->kmaps;
978 char modules_filename[PATH_MAX];
980 if (!filename_from_kallsyms_filename(modules_filename, "modules",
981 kallsyms_filename))
982 return -EINVAL;
984 if (do_validate_kcore_modules(modules_filename, map, kmaps))
985 return -EINVAL;
987 return 0;
990 static int validate_kcore_addresses(const char *kallsyms_filename,
991 struct map *map)
993 struct kmap *kmap = map__kmap(map);
995 if (kmap->ref_reloc_sym && kmap->ref_reloc_sym->name) {
996 u64 start;
998 start = kallsyms__get_function_start(kallsyms_filename,
999 kmap->ref_reloc_sym->name);
1000 if (start != kmap->ref_reloc_sym->addr)
1001 return -EINVAL;
1004 return validate_kcore_modules(kallsyms_filename, map);
1007 struct kcore_mapfn_data {
1008 struct dso *dso;
1009 enum map_type type;
1010 struct list_head maps;
1013 static int kcore_mapfn(u64 start, u64 len, u64 pgoff, void *data)
1015 struct kcore_mapfn_data *md = data;
1016 struct map *map;
1018 map = map__new2(start, md->dso, md->type);
1019 if (map == NULL)
1020 return -ENOMEM;
1022 map->end = map->start + len;
1023 map->pgoff = pgoff;
1025 list_add(&map->node, &md->maps);
1027 return 0;
1030 static int dso__load_kcore(struct dso *dso, struct map *map,
1031 const char *kallsyms_filename)
1033 struct map_groups *kmaps = map__kmap(map)->kmaps;
1034 struct machine *machine = kmaps->machine;
1035 struct kcore_mapfn_data md;
1036 struct map *old_map, *new_map, *replacement_map = NULL;
1037 bool is_64_bit;
1038 int err, fd;
1039 char kcore_filename[PATH_MAX];
1040 struct symbol *sym;
1042 /* This function requires that the map is the kernel map */
1043 if (map != machine->vmlinux_maps[map->type])
1044 return -EINVAL;
1046 if (!filename_from_kallsyms_filename(kcore_filename, "kcore",
1047 kallsyms_filename))
1048 return -EINVAL;
1050 /* Modules and kernel must be present at their original addresses */
1051 if (validate_kcore_addresses(kallsyms_filename, map))
1052 return -EINVAL;
1054 md.dso = dso;
1055 md.type = map->type;
1056 INIT_LIST_HEAD(&md.maps);
1058 fd = open(kcore_filename, O_RDONLY);
1059 if (fd < 0)
1060 return -EINVAL;
1062 /* Read new maps into temporary lists */
1063 err = file__read_maps(fd, md.type == MAP__FUNCTION, kcore_mapfn, &md,
1064 &is_64_bit);
1065 if (err)
1066 goto out_err;
1068 if (list_empty(&md.maps)) {
1069 err = -EINVAL;
1070 goto out_err;
1073 /* Remove old maps */
1074 old_map = map_groups__first(kmaps, map->type);
1075 while (old_map) {
1076 struct map *next = map_groups__next(old_map);
1078 if (old_map != map)
1079 map_groups__remove(kmaps, old_map);
1080 old_map = next;
1083 /* Find the kernel map using the first symbol */
1084 sym = dso__first_symbol(dso, map->type);
1085 list_for_each_entry(new_map, &md.maps, node) {
1086 if (sym && sym->start >= new_map->start &&
1087 sym->start < new_map->end) {
1088 replacement_map = new_map;
1089 break;
1093 if (!replacement_map)
1094 replacement_map = list_entry(md.maps.next, struct map, node);
1096 /* Add new maps */
1097 while (!list_empty(&md.maps)) {
1098 new_map = list_entry(md.maps.next, struct map, node);
1099 list_del(&new_map->node);
1100 if (new_map == replacement_map) {
1101 map->start = new_map->start;
1102 map->end = new_map->end;
1103 map->pgoff = new_map->pgoff;
1104 map->map_ip = new_map->map_ip;
1105 map->unmap_ip = new_map->unmap_ip;
1106 map__delete(new_map);
1107 /* Ensure maps are correctly ordered */
1108 map_groups__remove(kmaps, map);
1109 map_groups__insert(kmaps, map);
1110 } else {
1111 map_groups__insert(kmaps, new_map);
1116 * Set the data type and long name so that kcore can be read via
1117 * dso__data_read_addr().
1119 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1120 dso->binary_type = DSO_BINARY_TYPE__GUEST_KCORE;
1121 else
1122 dso->binary_type = DSO_BINARY_TYPE__KCORE;
1123 dso__set_long_name(dso, strdup(kcore_filename), true);
1125 close(fd);
1127 if (map->type == MAP__FUNCTION)
1128 pr_debug("Using %s for kernel object code\n", kcore_filename);
1129 else
1130 pr_debug("Using %s for kernel data\n", kcore_filename);
1132 return 0;
1134 out_err:
1135 while (!list_empty(&md.maps)) {
1136 map = list_entry(md.maps.next, struct map, node);
1137 list_del(&map->node);
1138 map__delete(map);
1140 close(fd);
1141 return -EINVAL;
1145 * If the kernel is relocated at boot time, kallsyms won't match. Compute the
1146 * delta based on the relocation reference symbol.
1148 static int kallsyms__delta(struct map *map, const char *filename, u64 *delta)
1150 struct kmap *kmap = map__kmap(map);
1151 u64 addr;
1153 if (!kmap->ref_reloc_sym || !kmap->ref_reloc_sym->name)
1154 return 0;
1156 addr = kallsyms__get_function_start(filename,
1157 kmap->ref_reloc_sym->name);
1158 if (!addr)
1159 return -1;
1161 *delta = addr - kmap->ref_reloc_sym->addr;
1162 return 0;
1165 int dso__load_kallsyms(struct dso *dso, const char *filename,
1166 struct map *map, symbol_filter_t filter)
1168 u64 delta = 0;
1170 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
1171 return -1;
1173 if (dso__load_all_kallsyms(dso, filename, map) < 0)
1174 return -1;
1176 if (kallsyms__delta(map, filename, &delta))
1177 return -1;
1179 symbols__fixup_duplicate(&dso->symbols[map->type]);
1180 symbols__fixup_end(&dso->symbols[map->type]);
1182 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1183 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
1184 else
1185 dso->symtab_type = DSO_BINARY_TYPE__KALLSYMS;
1187 if (!dso__load_kcore(dso, map, filename))
1188 return dso__split_kallsyms_for_kcore(dso, map, filter);
1189 else
1190 return dso__split_kallsyms(dso, map, delta, filter);
1193 static int dso__load_perf_map(struct dso *dso, struct map *map,
1194 symbol_filter_t filter)
1196 char *line = NULL;
1197 size_t n;
1198 FILE *file;
1199 int nr_syms = 0;
1201 file = fopen(dso->long_name, "r");
1202 if (file == NULL)
1203 goto out_failure;
1205 while (!feof(file)) {
1206 u64 start, size;
1207 struct symbol *sym;
1208 int line_len, len;
1210 line_len = getline(&line, &n, file);
1211 if (line_len < 0)
1212 break;
1214 if (!line)
1215 goto out_failure;
1217 line[--line_len] = '\0'; /* \n */
1219 len = hex2u64(line, &start);
1221 len++;
1222 if (len + 2 >= line_len)
1223 continue;
1225 len += hex2u64(line + len, &size);
1227 len++;
1228 if (len + 2 >= line_len)
1229 continue;
1231 sym = symbol__new(start, size, STB_GLOBAL, line + len);
1233 if (sym == NULL)
1234 goto out_delete_line;
1236 if (filter && filter(map, sym))
1237 symbol__delete(sym);
1238 else {
1239 symbols__insert(&dso->symbols[map->type], sym);
1240 nr_syms++;
1244 free(line);
1245 fclose(file);
1247 return nr_syms;
1249 out_delete_line:
1250 free(line);
1251 out_failure:
1252 return -1;
1255 static bool dso__is_compatible_symtab_type(struct dso *dso, bool kmod,
1256 enum dso_binary_type type)
1258 switch (type) {
1259 case DSO_BINARY_TYPE__JAVA_JIT:
1260 case DSO_BINARY_TYPE__DEBUGLINK:
1261 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
1262 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
1263 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
1264 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
1265 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
1266 return !kmod && dso->kernel == DSO_TYPE_USER;
1268 case DSO_BINARY_TYPE__KALLSYMS:
1269 case DSO_BINARY_TYPE__VMLINUX:
1270 case DSO_BINARY_TYPE__KCORE:
1271 return dso->kernel == DSO_TYPE_KERNEL;
1273 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
1274 case DSO_BINARY_TYPE__GUEST_VMLINUX:
1275 case DSO_BINARY_TYPE__GUEST_KCORE:
1276 return dso->kernel == DSO_TYPE_GUEST_KERNEL;
1278 case DSO_BINARY_TYPE__GUEST_KMODULE:
1279 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
1281 * kernel modules know their symtab type - it's set when
1282 * creating a module dso in machine__new_module().
1284 return kmod && dso->symtab_type == type;
1286 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
1287 return true;
1289 case DSO_BINARY_TYPE__NOT_FOUND:
1290 default:
1291 return false;
1295 int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter)
1297 char *name;
1298 int ret = -1;
1299 u_int i;
1300 struct machine *machine;
1301 char *root_dir = (char *) "";
1302 int ss_pos = 0;
1303 struct symsrc ss_[2];
1304 struct symsrc *syms_ss = NULL, *runtime_ss = NULL;
1305 bool kmod;
1307 dso__set_loaded(dso, map->type);
1309 if (dso->kernel == DSO_TYPE_KERNEL)
1310 return dso__load_kernel_sym(dso, map, filter);
1311 else if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1312 return dso__load_guest_kernel_sym(dso, map, filter);
1314 if (map->groups && map->groups->machine)
1315 machine = map->groups->machine;
1316 else
1317 machine = NULL;
1319 dso->adjust_symbols = 0;
1321 if (strncmp(dso->name, "/tmp/perf-", 10) == 0) {
1322 struct stat st;
1324 if (lstat(dso->name, &st) < 0)
1325 return -1;
1327 if (st.st_uid && (st.st_uid != geteuid())) {
1328 pr_warning("File %s not owned by current user or root, "
1329 "ignoring it.\n", dso->name);
1330 return -1;
1333 ret = dso__load_perf_map(dso, map, filter);
1334 dso->symtab_type = ret > 0 ? DSO_BINARY_TYPE__JAVA_JIT :
1335 DSO_BINARY_TYPE__NOT_FOUND;
1336 return ret;
1339 if (machine)
1340 root_dir = machine->root_dir;
1342 name = malloc(PATH_MAX);
1343 if (!name)
1344 return -1;
1346 kmod = dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
1347 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE;
1350 * Iterate over candidate debug images.
1351 * Keep track of "interesting" ones (those which have a symtab, dynsym,
1352 * and/or opd section) for processing.
1354 for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) {
1355 struct symsrc *ss = &ss_[ss_pos];
1356 bool next_slot = false;
1358 enum dso_binary_type symtab_type = binary_type_symtab[i];
1360 if (!dso__is_compatible_symtab_type(dso, kmod, symtab_type))
1361 continue;
1363 if (dso__read_binary_type_filename(dso, symtab_type,
1364 root_dir, name, PATH_MAX))
1365 continue;
1367 /* Name is now the name of the next image to try */
1368 if (symsrc__init(ss, dso, name, symtab_type) < 0)
1369 continue;
1371 if (!syms_ss && symsrc__has_symtab(ss)) {
1372 syms_ss = ss;
1373 next_slot = true;
1374 if (!dso->symsrc_filename)
1375 dso->symsrc_filename = strdup(name);
1378 if (!runtime_ss && symsrc__possibly_runtime(ss)) {
1379 runtime_ss = ss;
1380 next_slot = true;
1383 if (next_slot) {
1384 ss_pos++;
1386 if (syms_ss && runtime_ss)
1387 break;
1388 } else {
1389 symsrc__destroy(ss);
1394 if (!runtime_ss && !syms_ss)
1395 goto out_free;
1397 if (runtime_ss && !syms_ss) {
1398 syms_ss = runtime_ss;
1401 /* We'll have to hope for the best */
1402 if (!runtime_ss && syms_ss)
1403 runtime_ss = syms_ss;
1405 if (syms_ss)
1406 ret = dso__load_sym(dso, map, syms_ss, runtime_ss, filter, kmod);
1407 else
1408 ret = -1;
1410 if (ret > 0) {
1411 int nr_plt;
1413 nr_plt = dso__synthesize_plt_symbols(dso, runtime_ss, map, filter);
1414 if (nr_plt > 0)
1415 ret += nr_plt;
1418 for (; ss_pos > 0; ss_pos--)
1419 symsrc__destroy(&ss_[ss_pos - 1]);
1420 out_free:
1421 free(name);
1422 if (ret < 0 && strstr(dso->name, " (deleted)") != NULL)
1423 return 0;
1424 return ret;
1427 struct map *map_groups__find_by_name(struct map_groups *mg,
1428 enum map_type type, const char *name)
1430 struct rb_node *nd;
1432 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
1433 struct map *map = rb_entry(nd, struct map, rb_node);
1435 if (map->dso && strcmp(map->dso->short_name, name) == 0)
1436 return map;
1439 return NULL;
1442 int dso__load_vmlinux(struct dso *dso, struct map *map,
1443 const char *vmlinux, bool vmlinux_allocated,
1444 symbol_filter_t filter)
1446 int err = -1;
1447 struct symsrc ss;
1448 char symfs_vmlinux[PATH_MAX];
1449 enum dso_binary_type symtab_type;
1451 if (vmlinux[0] == '/')
1452 snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s", vmlinux);
1453 else
1454 snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s%s",
1455 symbol_conf.symfs, vmlinux);
1457 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1458 symtab_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
1459 else
1460 symtab_type = DSO_BINARY_TYPE__VMLINUX;
1462 if (symsrc__init(&ss, dso, symfs_vmlinux, symtab_type))
1463 return -1;
1465 err = dso__load_sym(dso, map, &ss, &ss, filter, 0);
1466 symsrc__destroy(&ss);
1468 if (err > 0) {
1469 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1470 dso->binary_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
1471 else
1472 dso->binary_type = DSO_BINARY_TYPE__VMLINUX;
1473 dso__set_long_name(dso, vmlinux, vmlinux_allocated);
1474 dso__set_loaded(dso, map->type);
1475 pr_debug("Using %s for symbols\n", symfs_vmlinux);
1478 return err;
1481 int dso__load_vmlinux_path(struct dso *dso, struct map *map,
1482 symbol_filter_t filter)
1484 int i, err = 0;
1485 char *filename;
1487 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1488 vmlinux_path__nr_entries + 1);
1490 filename = dso__build_id_filename(dso, NULL, 0);
1491 if (filename != NULL) {
1492 err = dso__load_vmlinux(dso, map, filename, true, filter);
1493 if (err > 0)
1494 goto out;
1495 free(filename);
1498 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
1499 err = dso__load_vmlinux(dso, map, vmlinux_path[i], false, filter);
1500 if (err > 0)
1501 break;
1503 out:
1504 return err;
1507 static int find_matching_kcore(struct map *map, char *dir, size_t dir_sz)
1509 char kallsyms_filename[PATH_MAX];
1510 struct dirent *dent;
1511 int ret = -1;
1512 DIR *d;
1514 d = opendir(dir);
1515 if (!d)
1516 return -1;
1518 while (1) {
1519 dent = readdir(d);
1520 if (!dent)
1521 break;
1522 if (dent->d_type != DT_DIR)
1523 continue;
1524 scnprintf(kallsyms_filename, sizeof(kallsyms_filename),
1525 "%s/%s/kallsyms", dir, dent->d_name);
1526 if (!validate_kcore_addresses(kallsyms_filename, map)) {
1527 strlcpy(dir, kallsyms_filename, dir_sz);
1528 ret = 0;
1529 break;
1533 closedir(d);
1535 return ret;
1538 static char *dso__find_kallsyms(struct dso *dso, struct map *map)
1540 u8 host_build_id[BUILD_ID_SIZE];
1541 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1542 bool is_host = false;
1543 char path[PATH_MAX];
1545 if (!dso->has_build_id) {
1547 * Last resort, if we don't have a build-id and couldn't find
1548 * any vmlinux file, try the running kernel kallsyms table.
1550 goto proc_kallsyms;
1553 if (sysfs__read_build_id("/sys/kernel/notes", host_build_id,
1554 sizeof(host_build_id)) == 0)
1555 is_host = dso__build_id_equal(dso, host_build_id);
1557 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1559 scnprintf(path, sizeof(path), "%s/[kernel.kcore]/%s", buildid_dir,
1560 sbuild_id);
1562 /* Use /proc/kallsyms if possible */
1563 if (is_host) {
1564 DIR *d;
1565 int fd;
1567 /* If no cached kcore go with /proc/kallsyms */
1568 d = opendir(path);
1569 if (!d)
1570 goto proc_kallsyms;
1571 closedir(d);
1574 * Do not check the build-id cache, until we know we cannot use
1575 * /proc/kcore.
1577 fd = open("/proc/kcore", O_RDONLY);
1578 if (fd != -1) {
1579 close(fd);
1580 /* If module maps match go with /proc/kallsyms */
1581 if (!validate_kcore_addresses("/proc/kallsyms", map))
1582 goto proc_kallsyms;
1585 /* Find kallsyms in build-id cache with kcore */
1586 if (!find_matching_kcore(map, path, sizeof(path)))
1587 return strdup(path);
1589 goto proc_kallsyms;
1592 /* Find kallsyms in build-id cache with kcore */
1593 if (!find_matching_kcore(map, path, sizeof(path)))
1594 return strdup(path);
1596 scnprintf(path, sizeof(path), "%s/[kernel.kallsyms]/%s",
1597 buildid_dir, sbuild_id);
1599 if (access(path, F_OK)) {
1600 pr_err("No kallsyms or vmlinux with build-id %s was found\n",
1601 sbuild_id);
1602 return NULL;
1605 return strdup(path);
1607 proc_kallsyms:
1608 return strdup("/proc/kallsyms");
1611 static int dso__load_kernel_sym(struct dso *dso, struct map *map,
1612 symbol_filter_t filter)
1614 int err;
1615 const char *kallsyms_filename = NULL;
1616 char *kallsyms_allocated_filename = NULL;
1618 * Step 1: if the user specified a kallsyms or vmlinux filename, use
1619 * it and only it, reporting errors to the user if it cannot be used.
1621 * For instance, try to analyse an ARM perf.data file _without_ a
1622 * build-id, or if the user specifies the wrong path to the right
1623 * vmlinux file, obviously we can't fallback to another vmlinux (a
1624 * x86_86 one, on the machine where analysis is being performed, say),
1625 * or worse, /proc/kallsyms.
1627 * If the specified file _has_ a build-id and there is a build-id
1628 * section in the perf.data file, we will still do the expected
1629 * validation in dso__load_vmlinux and will bail out if they don't
1630 * match.
1632 if (symbol_conf.kallsyms_name != NULL) {
1633 kallsyms_filename = symbol_conf.kallsyms_name;
1634 goto do_kallsyms;
1637 if (!symbol_conf.ignore_vmlinux && symbol_conf.vmlinux_name != NULL) {
1638 return dso__load_vmlinux(dso, map, symbol_conf.vmlinux_name,
1639 false, filter);
1642 if (!symbol_conf.ignore_vmlinux && vmlinux_path != NULL) {
1643 err = dso__load_vmlinux_path(dso, map, filter);
1644 if (err > 0)
1645 return err;
1648 /* do not try local files if a symfs was given */
1649 if (symbol_conf.symfs[0] != 0)
1650 return -1;
1652 kallsyms_allocated_filename = dso__find_kallsyms(dso, map);
1653 if (!kallsyms_allocated_filename)
1654 return -1;
1656 kallsyms_filename = kallsyms_allocated_filename;
1658 do_kallsyms:
1659 err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
1660 if (err > 0)
1661 pr_debug("Using %s for symbols\n", kallsyms_filename);
1662 free(kallsyms_allocated_filename);
1664 if (err > 0 && !dso__is_kcore(dso)) {
1665 dso__set_long_name(dso, "[kernel.kallsyms]", false);
1666 map__fixup_start(map);
1667 map__fixup_end(map);
1670 return err;
1673 static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
1674 symbol_filter_t filter)
1676 int err;
1677 const char *kallsyms_filename = NULL;
1678 struct machine *machine;
1679 char path[PATH_MAX];
1681 if (!map->groups) {
1682 pr_debug("Guest kernel map hasn't the point to groups\n");
1683 return -1;
1685 machine = map->groups->machine;
1687 if (machine__is_default_guest(machine)) {
1689 * if the user specified a vmlinux filename, use it and only
1690 * it, reporting errors to the user if it cannot be used.
1691 * Or use file guest_kallsyms inputted by user on commandline
1693 if (symbol_conf.default_guest_vmlinux_name != NULL) {
1694 err = dso__load_vmlinux(dso, map,
1695 symbol_conf.default_guest_vmlinux_name,
1696 false, filter);
1697 return err;
1700 kallsyms_filename = symbol_conf.default_guest_kallsyms;
1701 if (!kallsyms_filename)
1702 return -1;
1703 } else {
1704 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
1705 kallsyms_filename = path;
1708 err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
1709 if (err > 0)
1710 pr_debug("Using %s for symbols\n", kallsyms_filename);
1711 if (err > 0 && !dso__is_kcore(dso)) {
1712 machine__mmap_name(machine, path, sizeof(path));
1713 dso__set_long_name(dso, strdup(path), true);
1714 map__fixup_start(map);
1715 map__fixup_end(map);
1718 return err;
1721 static void vmlinux_path__exit(void)
1723 while (--vmlinux_path__nr_entries >= 0)
1724 zfree(&vmlinux_path[vmlinux_path__nr_entries]);
1726 zfree(&vmlinux_path);
1729 static int vmlinux_path__init(void)
1731 struct utsname uts;
1732 char bf[PATH_MAX];
1734 vmlinux_path = malloc(sizeof(char *) * 5);
1735 if (vmlinux_path == NULL)
1736 return -1;
1738 vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
1739 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1740 goto out_fail;
1741 ++vmlinux_path__nr_entries;
1742 vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
1743 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1744 goto out_fail;
1745 ++vmlinux_path__nr_entries;
1747 /* only try running kernel version if no symfs was given */
1748 if (symbol_conf.symfs[0] != 0)
1749 return 0;
1751 if (uname(&uts) < 0)
1752 return -1;
1754 snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
1755 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1756 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1757 goto out_fail;
1758 ++vmlinux_path__nr_entries;
1759 snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
1760 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1761 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1762 goto out_fail;
1763 ++vmlinux_path__nr_entries;
1764 snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
1765 uts.release);
1766 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1767 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1768 goto out_fail;
1769 ++vmlinux_path__nr_entries;
1771 return 0;
1773 out_fail:
1774 vmlinux_path__exit();
1775 return -1;
1778 int setup_list(struct strlist **list, const char *list_str,
1779 const char *list_name)
1781 if (list_str == NULL)
1782 return 0;
1784 *list = strlist__new(true, list_str);
1785 if (!*list) {
1786 pr_err("problems parsing %s list\n", list_name);
1787 return -1;
1789 return 0;
1792 static bool symbol__read_kptr_restrict(void)
1794 bool value = false;
1796 if (geteuid() != 0) {
1797 FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
1798 if (fp != NULL) {
1799 char line[8];
1801 if (fgets(line, sizeof(line), fp) != NULL)
1802 value = atoi(line) != 0;
1804 fclose(fp);
1808 return value;
1811 int symbol__init(void)
1813 const char *symfs;
1815 if (symbol_conf.initialized)
1816 return 0;
1818 symbol_conf.priv_size = PERF_ALIGN(symbol_conf.priv_size, sizeof(u64));
1820 symbol__elf_init();
1822 if (symbol_conf.sort_by_name)
1823 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
1824 sizeof(struct symbol));
1826 if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
1827 return -1;
1829 if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
1830 pr_err("'.' is the only non valid --field-separator argument\n");
1831 return -1;
1834 if (setup_list(&symbol_conf.dso_list,
1835 symbol_conf.dso_list_str, "dso") < 0)
1836 return -1;
1838 if (setup_list(&symbol_conf.comm_list,
1839 symbol_conf.comm_list_str, "comm") < 0)
1840 goto out_free_dso_list;
1842 if (setup_list(&symbol_conf.sym_list,
1843 symbol_conf.sym_list_str, "symbol") < 0)
1844 goto out_free_comm_list;
1847 * A path to symbols of "/" is identical to ""
1848 * reset here for simplicity.
1850 symfs = realpath(symbol_conf.symfs, NULL);
1851 if (symfs == NULL)
1852 symfs = symbol_conf.symfs;
1853 if (strcmp(symfs, "/") == 0)
1854 symbol_conf.symfs = "";
1855 if (symfs != symbol_conf.symfs)
1856 free((void *)symfs);
1858 symbol_conf.kptr_restrict = symbol__read_kptr_restrict();
1860 symbol_conf.initialized = true;
1861 return 0;
1863 out_free_comm_list:
1864 strlist__delete(symbol_conf.comm_list);
1865 out_free_dso_list:
1866 strlist__delete(symbol_conf.dso_list);
1867 return -1;
1870 void symbol__exit(void)
1872 if (!symbol_conf.initialized)
1873 return;
1874 strlist__delete(symbol_conf.sym_list);
1875 strlist__delete(symbol_conf.dso_list);
1876 strlist__delete(symbol_conf.comm_list);
1877 vmlinux_path__exit();
1878 symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
1879 symbol_conf.initialized = false;