c69915c9d5bc202b699793e6833860f172a75093
[platform/kernel/linux-rpi.git] / tools / perf / util / symbol.c
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"
18 #include "header.h"
19
20 #include <elf.h>
21 #include <limits.h>
22 #include <symbol/kallsyms.h>
23 #include <sys/utsname.h>
24
25 static int dso__load_kernel_sym(struct dso *dso, struct map *map,
26                                 symbol_filter_t filter);
27 static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
28                         symbol_filter_t filter);
29 int vmlinux_path__nr_entries;
30 char **vmlinux_path;
31
32 struct symbol_conf symbol_conf = {
33         .use_modules            = true,
34         .try_vmlinux_path       = true,
35         .annotate_src           = true,
36         .demangle               = true,
37         .demangle_kernel        = false,
38         .cumulate_callchain     = true,
39         .show_hist_headers      = true,
40         .symfs                  = "",
41 };
42
43 static enum dso_binary_type binary_type_symtab[] = {
44         DSO_BINARY_TYPE__KALLSYMS,
45         DSO_BINARY_TYPE__GUEST_KALLSYMS,
46         DSO_BINARY_TYPE__JAVA_JIT,
47         DSO_BINARY_TYPE__DEBUGLINK,
48         DSO_BINARY_TYPE__BUILD_ID_CACHE,
49         DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
50         DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
51         DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
52         DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
53         DSO_BINARY_TYPE__GUEST_KMODULE,
54         DSO_BINARY_TYPE__GUEST_KMODULE_COMP,
55         DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE,
56         DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP,
57         DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO,
58         DSO_BINARY_TYPE__NOT_FOUND,
59 };
60
61 #define DSO_BINARY_TYPE__SYMTAB_CNT ARRAY_SIZE(binary_type_symtab)
62
63 bool symbol_type__is_a(char symbol_type, enum map_type map_type)
64 {
65         symbol_type = toupper(symbol_type);
66
67         switch (map_type) {
68         case MAP__FUNCTION:
69                 return symbol_type == 'T' || symbol_type == 'W';
70         case MAP__VARIABLE:
71                 return symbol_type == 'D';
72         default:
73                 return false;
74         }
75 }
76
77 static int prefix_underscores_count(const char *str)
78 {
79         const char *tail = str;
80
81         while (*tail == '_')
82                 tail++;
83
84         return tail - str;
85 }
86
87 #define SYMBOL_A 0
88 #define SYMBOL_B 1
89
90 static int choose_best_symbol(struct symbol *syma, struct symbol *symb)
91 {
92         s64 a;
93         s64 b;
94         size_t na, nb;
95
96         /* Prefer a symbol with non zero length */
97         a = syma->end - syma->start;
98         b = symb->end - symb->start;
99         if ((b == 0) && (a > 0))
100                 return SYMBOL_A;
101         else if ((a == 0) && (b > 0))
102                 return SYMBOL_B;
103
104         /* Prefer a non weak symbol over a weak one */
105         a = syma->binding == STB_WEAK;
106         b = symb->binding == STB_WEAK;
107         if (b && !a)
108                 return SYMBOL_A;
109         if (a && !b)
110                 return SYMBOL_B;
111
112         /* Prefer a global symbol over a non global one */
113         a = syma->binding == STB_GLOBAL;
114         b = symb->binding == STB_GLOBAL;
115         if (a && !b)
116                 return SYMBOL_A;
117         if (b && !a)
118                 return SYMBOL_B;
119
120         /* Prefer a symbol with less underscores */
121         a = prefix_underscores_count(syma->name);
122         b = prefix_underscores_count(symb->name);
123         if (b > a)
124                 return SYMBOL_A;
125         else if (a > b)
126                 return SYMBOL_B;
127
128         /* Choose the symbol with the longest name */
129         na = strlen(syma->name);
130         nb = strlen(symb->name);
131         if (na > nb)
132                 return SYMBOL_A;
133         else if (na < nb)
134                 return SYMBOL_B;
135
136         /* Avoid "SyS" kernel syscall aliases */
137         if (na >= 3 && !strncmp(syma->name, "SyS", 3))
138                 return SYMBOL_B;
139         if (na >= 10 && !strncmp(syma->name, "compat_SyS", 10))
140                 return SYMBOL_B;
141
142         return SYMBOL_A;
143 }
144
145 void symbols__fixup_duplicate(struct rb_root *symbols)
146 {
147         struct rb_node *nd;
148         struct symbol *curr, *next;
149
150         nd = rb_first(symbols);
151
152         while (nd) {
153                 curr = rb_entry(nd, struct symbol, rb_node);
154 again:
155                 nd = rb_next(&curr->rb_node);
156                 next = rb_entry(nd, struct symbol, rb_node);
157
158                 if (!nd)
159                         break;
160
161                 if (curr->start != next->start)
162                         continue;
163
164                 if (choose_best_symbol(curr, next) == SYMBOL_A) {
165                         rb_erase(&next->rb_node, symbols);
166                         symbol__delete(next);
167                         goto again;
168                 } else {
169                         nd = rb_next(&curr->rb_node);
170                         rb_erase(&curr->rb_node, symbols);
171                         symbol__delete(curr);
172                 }
173         }
174 }
175
176 void symbols__fixup_end(struct rb_root *symbols)
177 {
178         struct rb_node *nd, *prevnd = rb_first(symbols);
179         struct symbol *curr, *prev;
180
181         if (prevnd == NULL)
182                 return;
183
184         curr = rb_entry(prevnd, struct symbol, rb_node);
185
186         for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
187                 prev = curr;
188                 curr = rb_entry(nd, struct symbol, rb_node);
189
190                 if (prev->end == prev->start && prev->end != curr->start)
191                         prev->end = curr->start;
192         }
193
194         /* Last entry */
195         if (curr->end == curr->start)
196                 curr->end = roundup(curr->start, 4096);
197 }
198
199 void __map_groups__fixup_end(struct map_groups *mg, enum map_type type)
200 {
201         struct map *prev, *curr;
202         struct rb_node *nd, *prevnd = rb_first(&mg->maps[type]);
203
204         if (prevnd == NULL)
205                 return;
206
207         curr = rb_entry(prevnd, struct map, rb_node);
208
209         for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
210                 prev = curr;
211                 curr = rb_entry(nd, struct map, rb_node);
212                 prev->end = curr->start;
213         }
214
215         /*
216          * We still haven't the actual symbols, so guess the
217          * last map final address.
218          */
219         curr->end = ~0ULL;
220 }
221
222 struct symbol *symbol__new(u64 start, u64 len, u8 binding, const char *name)
223 {
224         size_t namelen = strlen(name) + 1;
225         struct symbol *sym = calloc(1, (symbol_conf.priv_size +
226                                         sizeof(*sym) + namelen));
227         if (sym == NULL)
228                 return NULL;
229
230         if (symbol_conf.priv_size)
231                 sym = ((void *)sym) + symbol_conf.priv_size;
232
233         sym->start   = start;
234         sym->end     = len ? start + len : start;
235         sym->binding = binding;
236         sym->namelen = namelen - 1;
237
238         pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n",
239                   __func__, name, start, sym->end);
240         memcpy(sym->name, name, namelen);
241
242         return sym;
243 }
244
245 void symbol__delete(struct symbol *sym)
246 {
247         free(((void *)sym) - symbol_conf.priv_size);
248 }
249
250 size_t symbol__fprintf(struct symbol *sym, FILE *fp)
251 {
252         return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %c %s\n",
253                        sym->start, sym->end,
254                        sym->binding == STB_GLOBAL ? 'g' :
255                        sym->binding == STB_LOCAL  ? 'l' : 'w',
256                        sym->name);
257 }
258
259 size_t symbol__fprintf_symname_offs(const struct symbol *sym,
260                                     const struct addr_location *al, FILE *fp)
261 {
262         unsigned long offset;
263         size_t length;
264
265         if (sym && sym->name) {
266                 length = fprintf(fp, "%s", sym->name);
267                 if (al) {
268                         if (al->addr < sym->end)
269                                 offset = al->addr - sym->start;
270                         else
271                                 offset = al->addr - al->map->start - sym->start;
272                         length += fprintf(fp, "+0x%lx", offset);
273                 }
274                 return length;
275         } else
276                 return fprintf(fp, "[unknown]");
277 }
278
279 size_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp)
280 {
281         return symbol__fprintf_symname_offs(sym, NULL, fp);
282 }
283
284 void symbols__delete(struct rb_root *symbols)
285 {
286         struct symbol *pos;
287         struct rb_node *next = rb_first(symbols);
288
289         while (next) {
290                 pos = rb_entry(next, struct symbol, rb_node);
291                 next = rb_next(&pos->rb_node);
292                 rb_erase(&pos->rb_node, symbols);
293                 symbol__delete(pos);
294         }
295 }
296
297 void symbols__insert(struct rb_root *symbols, struct symbol *sym)
298 {
299         struct rb_node **p = &symbols->rb_node;
300         struct rb_node *parent = NULL;
301         const u64 ip = sym->start;
302         struct symbol *s;
303
304         while (*p != NULL) {
305                 parent = *p;
306                 s = rb_entry(parent, struct symbol, rb_node);
307                 if (ip < s->start)
308                         p = &(*p)->rb_left;
309                 else
310                         p = &(*p)->rb_right;
311         }
312         rb_link_node(&sym->rb_node, parent, p);
313         rb_insert_color(&sym->rb_node, symbols);
314 }
315
316 static struct symbol *symbols__find(struct rb_root *symbols, u64 ip)
317 {
318         struct rb_node *n;
319
320         if (symbols == NULL)
321                 return NULL;
322
323         n = symbols->rb_node;
324
325         while (n) {
326                 struct symbol *s = rb_entry(n, struct symbol, rb_node);
327
328                 if (ip < s->start)
329                         n = n->rb_left;
330                 else if (ip >= s->end)
331                         n = n->rb_right;
332                 else
333                         return s;
334         }
335
336         return NULL;
337 }
338
339 static struct symbol *symbols__first(struct rb_root *symbols)
340 {
341         struct rb_node *n = rb_first(symbols);
342
343         if (n)
344                 return rb_entry(n, struct symbol, rb_node);
345
346         return NULL;
347 }
348
349 static struct symbol *symbols__next(struct symbol *sym)
350 {
351         struct rb_node *n = rb_next(&sym->rb_node);
352
353         if (n)
354                 return rb_entry(n, struct symbol, rb_node);
355
356         return NULL;
357 }
358
359 struct symbol_name_rb_node {
360         struct rb_node  rb_node;
361         struct symbol   sym;
362 };
363
364 static void symbols__insert_by_name(struct rb_root *symbols, struct symbol *sym)
365 {
366         struct rb_node **p = &symbols->rb_node;
367         struct rb_node *parent = NULL;
368         struct symbol_name_rb_node *symn, *s;
369
370         symn = container_of(sym, struct symbol_name_rb_node, sym);
371
372         while (*p != NULL) {
373                 parent = *p;
374                 s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
375                 if (strcmp(sym->name, s->sym.name) < 0)
376                         p = &(*p)->rb_left;
377                 else
378                         p = &(*p)->rb_right;
379         }
380         rb_link_node(&symn->rb_node, parent, p);
381         rb_insert_color(&symn->rb_node, symbols);
382 }
383
384 static void symbols__sort_by_name(struct rb_root *symbols,
385                                   struct rb_root *source)
386 {
387         struct rb_node *nd;
388
389         for (nd = rb_first(source); nd; nd = rb_next(nd)) {
390                 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
391                 symbols__insert_by_name(symbols, pos);
392         }
393 }
394
395 static struct symbol *symbols__find_by_name(struct rb_root *symbols,
396                                             const char *name)
397 {
398         struct rb_node *n;
399
400         if (symbols == NULL)
401                 return NULL;
402
403         n = symbols->rb_node;
404
405         while (n) {
406                 struct symbol_name_rb_node *s;
407                 int cmp;
408
409                 s = rb_entry(n, struct symbol_name_rb_node, rb_node);
410                 cmp = strcmp(name, s->sym.name);
411
412                 if (cmp < 0)
413                         n = n->rb_left;
414                 else if (cmp > 0)
415                         n = n->rb_right;
416                 else
417                         return &s->sym;
418         }
419
420         return NULL;
421 }
422
423 struct symbol *dso__find_symbol(struct dso *dso,
424                                 enum map_type type, u64 addr)
425 {
426         return symbols__find(&dso->symbols[type], addr);
427 }
428
429 struct symbol *dso__first_symbol(struct dso *dso, enum map_type type)
430 {
431         return symbols__first(&dso->symbols[type]);
432 }
433
434 struct symbol *dso__next_symbol(struct symbol *sym)
435 {
436         return symbols__next(sym);
437 }
438
439 struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
440                                         const char *name)
441 {
442         return symbols__find_by_name(&dso->symbol_names[type], name);
443 }
444
445 void dso__sort_by_name(struct dso *dso, enum map_type type)
446 {
447         dso__set_sorted_by_name(dso, type);
448         return symbols__sort_by_name(&dso->symbol_names[type],
449                                      &dso->symbols[type]);
450 }
451
452 size_t dso__fprintf_symbols_by_name(struct dso *dso,
453                                     enum map_type type, FILE *fp)
454 {
455         size_t ret = 0;
456         struct rb_node *nd;
457         struct symbol_name_rb_node *pos;
458
459         for (nd = rb_first(&dso->symbol_names[type]); nd; nd = rb_next(nd)) {
460                 pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
461                 fprintf(fp, "%s\n", pos->sym.name);
462         }
463
464         return ret;
465 }
466
467 int modules__parse(const char *filename, void *arg,
468                    int (*process_module)(void *arg, const char *name,
469                                          u64 start))
470 {
471         char *line = NULL;
472         size_t n;
473         FILE *file;
474         int err = 0;
475
476         file = fopen(filename, "r");
477         if (file == NULL)
478                 return -1;
479
480         while (1) {
481                 char name[PATH_MAX];
482                 u64 start;
483                 char *sep;
484                 ssize_t line_len;
485
486                 line_len = getline(&line, &n, file);
487                 if (line_len < 0) {
488                         if (feof(file))
489                                 break;
490                         err = -1;
491                         goto out;
492                 }
493
494                 if (!line) {
495                         err = -1;
496                         goto out;
497                 }
498
499                 line[--line_len] = '\0'; /* \n */
500
501                 sep = strrchr(line, 'x');
502                 if (sep == NULL)
503                         continue;
504
505                 hex2u64(sep + 1, &start);
506
507                 sep = strchr(line, ' ');
508                 if (sep == NULL)
509                         continue;
510
511                 *sep = '\0';
512
513                 scnprintf(name, sizeof(name), "[%s]", line);
514
515                 err = process_module(arg, name, start);
516                 if (err)
517                         break;
518         }
519 out:
520         free(line);
521         fclose(file);
522         return err;
523 }
524
525 struct process_kallsyms_args {
526         struct map *map;
527         struct dso *dso;
528 };
529
530 /*
531  * These are symbols in the kernel image, so make sure that
532  * sym is from a kernel DSO.
533  */
534 bool symbol__is_idle(struct symbol *sym)
535 {
536         const char * const idle_symbols[] = {
537                 "cpu_idle",
538                 "cpu_startup_entry",
539                 "intel_idle",
540                 "default_idle",
541                 "native_safe_halt",
542                 "enter_idle",
543                 "exit_idle",
544                 "mwait_idle",
545                 "mwait_idle_with_hints",
546                 "poll_idle",
547                 "ppc64_runlatch_off",
548                 "pseries_dedicated_idle_sleep",
549                 NULL
550         };
551
552         int i;
553
554         if (!sym)
555                 return false;
556
557         for (i = 0; idle_symbols[i]; i++) {
558                 if (!strcmp(idle_symbols[i], sym->name))
559                         return true;
560         }
561
562         return false;
563 }
564
565 static int map__process_kallsym_symbol(void *arg, const char *name,
566                                        char type, u64 start)
567 {
568         struct symbol *sym;
569         struct process_kallsyms_args *a = arg;
570         struct rb_root *root = &a->dso->symbols[a->map->type];
571
572         if (!symbol_type__is_a(type, a->map->type))
573                 return 0;
574
575         /*
576          * module symbols are not sorted so we add all
577          * symbols, setting length to 0, and rely on
578          * symbols__fixup_end() to fix it up.
579          */
580         sym = symbol__new(start, 0, kallsyms2elf_type(type), name);
581         if (sym == NULL)
582                 return -ENOMEM;
583         /*
584          * We will pass the symbols to the filter later, in
585          * map__split_kallsyms, when we have split the maps per module
586          */
587         symbols__insert(root, sym);
588
589         return 0;
590 }
591
592 /*
593  * Loads the function entries in /proc/kallsyms into kernel_map->dso,
594  * so that we can in the next step set the symbol ->end address and then
595  * call kernel_maps__split_kallsyms.
596  */
597 static int dso__load_all_kallsyms(struct dso *dso, const char *filename,
598                                   struct map *map)
599 {
600         struct process_kallsyms_args args = { .map = map, .dso = dso, };
601         return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
602 }
603
604 static int dso__split_kallsyms_for_kcore(struct dso *dso, struct map *map,
605                                          symbol_filter_t filter)
606 {
607         struct map_groups *kmaps = map__kmap(map)->kmaps;
608         struct map *curr_map;
609         struct symbol *pos;
610         int count = 0, moved = 0;
611         struct rb_root *root = &dso->symbols[map->type];
612         struct rb_node *next = rb_first(root);
613
614         while (next) {
615                 char *module;
616
617                 pos = rb_entry(next, struct symbol, rb_node);
618                 next = rb_next(&pos->rb_node);
619
620                 module = strchr(pos->name, '\t');
621                 if (module)
622                         *module = '\0';
623
624                 curr_map = map_groups__find(kmaps, map->type, pos->start);
625
626                 if (!curr_map || (filter && filter(curr_map, pos))) {
627                         rb_erase(&pos->rb_node, root);
628                         symbol__delete(pos);
629                 } else {
630                         pos->start -= curr_map->start - curr_map->pgoff;
631                         if (pos->end)
632                                 pos->end -= curr_map->start - curr_map->pgoff;
633                         if (curr_map != map) {
634                                 rb_erase(&pos->rb_node, root);
635                                 symbols__insert(
636                                         &curr_map->dso->symbols[curr_map->type],
637                                         pos);
638                                 ++moved;
639                         } else {
640                                 ++count;
641                         }
642                 }
643         }
644
645         /* Symbols have been adjusted */
646         dso->adjust_symbols = 1;
647
648         return count + moved;
649 }
650
651 /*
652  * Split the symbols into maps, making sure there are no overlaps, i.e. the
653  * kernel range is broken in several maps, named [kernel].N, as we don't have
654  * the original ELF section names vmlinux have.
655  */
656 static int dso__split_kallsyms(struct dso *dso, struct map *map, u64 delta,
657                                symbol_filter_t filter)
658 {
659         struct map_groups *kmaps = map__kmap(map)->kmaps;
660         struct machine *machine = kmaps->machine;
661         struct map *curr_map = map;
662         struct symbol *pos;
663         int count = 0, moved = 0;       
664         struct rb_root *root = &dso->symbols[map->type];
665         struct rb_node *next = rb_first(root);
666         int kernel_range = 0;
667
668         while (next) {
669                 char *module;
670
671                 pos = rb_entry(next, struct symbol, rb_node);
672                 next = rb_next(&pos->rb_node);
673
674                 module = strchr(pos->name, '\t');
675                 if (module) {
676                         if (!symbol_conf.use_modules)
677                                 goto discard_symbol;
678
679                         *module++ = '\0';
680
681                         if (strcmp(curr_map->dso->short_name, module)) {
682                                 if (curr_map != map &&
683                                     dso->kernel == DSO_TYPE_GUEST_KERNEL &&
684                                     machine__is_default_guest(machine)) {
685                                         /*
686                                          * We assume all symbols of a module are
687                                          * continuous in * kallsyms, so curr_map
688                                          * points to a module and all its
689                                          * symbols are in its kmap. Mark it as
690                                          * loaded.
691                                          */
692                                         dso__set_loaded(curr_map->dso,
693                                                         curr_map->type);
694                                 }
695
696                                 curr_map = map_groups__find_by_name(kmaps,
697                                                         map->type, module);
698                                 if (curr_map == NULL) {
699                                         pr_debug("%s/proc/{kallsyms,modules} "
700                                                  "inconsistency while looking "
701                                                  "for \"%s\" module!\n",
702                                                  machine->root_dir, module);
703                                         curr_map = map;
704                                         goto discard_symbol;
705                                 }
706
707                                 if (curr_map->dso->loaded &&
708                                     !machine__is_default_guest(machine))
709                                         goto discard_symbol;
710                         }
711                         /*
712                          * So that we look just like we get from .ko files,
713                          * i.e. not prelinked, relative to map->start.
714                          */
715                         pos->start = curr_map->map_ip(curr_map, pos->start);
716                         pos->end   = curr_map->map_ip(curr_map, pos->end);
717                 } else if (curr_map != map) {
718                         char dso_name[PATH_MAX];
719                         struct dso *ndso;
720
721                         if (delta) {
722                                 /* Kernel was relocated at boot time */
723                                 pos->start -= delta;
724                                 pos->end -= delta;
725                         }
726
727                         if (count == 0) {
728                                 curr_map = map;
729                                 goto filter_symbol;
730                         }
731
732                         if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
733                                 snprintf(dso_name, sizeof(dso_name),
734                                         "[guest.kernel].%d",
735                                         kernel_range++);
736                         else
737                                 snprintf(dso_name, sizeof(dso_name),
738                                         "[kernel].%d",
739                                         kernel_range++);
740
741                         ndso = dso__new(dso_name);
742                         if (ndso == NULL)
743                                 return -1;
744
745                         ndso->kernel = dso->kernel;
746
747                         curr_map = map__new2(pos->start, ndso, map->type);
748                         if (curr_map == NULL) {
749                                 dso__delete(ndso);
750                                 return -1;
751                         }
752
753                         curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
754                         map_groups__insert(kmaps, curr_map);
755                         ++kernel_range;
756                 } else if (delta) {
757                         /* Kernel was relocated at boot time */
758                         pos->start -= delta;
759                         pos->end -= delta;
760                 }
761 filter_symbol:
762                 if (filter && filter(curr_map, pos)) {
763 discard_symbol:         rb_erase(&pos->rb_node, root);
764                         symbol__delete(pos);
765                 } else {
766                         if (curr_map != map) {
767                                 rb_erase(&pos->rb_node, root);
768                                 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
769                                 ++moved;
770                         } else
771                                 ++count;
772                 }
773         }
774
775         if (curr_map != map &&
776             dso->kernel == DSO_TYPE_GUEST_KERNEL &&
777             machine__is_default_guest(kmaps->machine)) {
778                 dso__set_loaded(curr_map->dso, curr_map->type);
779         }
780
781         return count + moved;
782 }
783
784 bool symbol__restricted_filename(const char *filename,
785                                  const char *restricted_filename)
786 {
787         bool restricted = false;
788
789         if (symbol_conf.kptr_restrict) {
790                 char *r = realpath(filename, NULL);
791
792                 if (r != NULL) {
793                         restricted = strcmp(r, restricted_filename) == 0;
794                         free(r);
795                         return restricted;
796                 }
797         }
798
799         return restricted;
800 }
801
802 struct module_info {
803         struct rb_node rb_node;
804         char *name;
805         u64 start;
806 };
807
808 static void add_module(struct module_info *mi, struct rb_root *modules)
809 {
810         struct rb_node **p = &modules->rb_node;
811         struct rb_node *parent = NULL;
812         struct module_info *m;
813
814         while (*p != NULL) {
815                 parent = *p;
816                 m = rb_entry(parent, struct module_info, rb_node);
817                 if (strcmp(mi->name, m->name) < 0)
818                         p = &(*p)->rb_left;
819                 else
820                         p = &(*p)->rb_right;
821         }
822         rb_link_node(&mi->rb_node, parent, p);
823         rb_insert_color(&mi->rb_node, modules);
824 }
825
826 static void delete_modules(struct rb_root *modules)
827 {
828         struct module_info *mi;
829         struct rb_node *next = rb_first(modules);
830
831         while (next) {
832                 mi = rb_entry(next, struct module_info, rb_node);
833                 next = rb_next(&mi->rb_node);
834                 rb_erase(&mi->rb_node, modules);
835                 zfree(&mi->name);
836                 free(mi);
837         }
838 }
839
840 static struct module_info *find_module(const char *name,
841                                        struct rb_root *modules)
842 {
843         struct rb_node *n = modules->rb_node;
844
845         while (n) {
846                 struct module_info *m;
847                 int cmp;
848
849                 m = rb_entry(n, struct module_info, rb_node);
850                 cmp = strcmp(name, m->name);
851                 if (cmp < 0)
852                         n = n->rb_left;
853                 else if (cmp > 0)
854                         n = n->rb_right;
855                 else
856                         return m;
857         }
858
859         return NULL;
860 }
861
862 static int __read_proc_modules(void *arg, const char *name, u64 start)
863 {
864         struct rb_root *modules = arg;
865         struct module_info *mi;
866
867         mi = zalloc(sizeof(struct module_info));
868         if (!mi)
869                 return -ENOMEM;
870
871         mi->name = strdup(name);
872         mi->start = start;
873
874         if (!mi->name) {
875                 free(mi);
876                 return -ENOMEM;
877         }
878
879         add_module(mi, modules);
880
881         return 0;
882 }
883
884 static int read_proc_modules(const char *filename, struct rb_root *modules)
885 {
886         if (symbol__restricted_filename(filename, "/proc/modules"))
887                 return -1;
888
889         if (modules__parse(filename, modules, __read_proc_modules)) {
890                 delete_modules(modules);
891                 return -1;
892         }
893
894         return 0;
895 }
896
897 int compare_proc_modules(const char *from, const char *to)
898 {
899         struct rb_root from_modules = RB_ROOT;
900         struct rb_root to_modules = RB_ROOT;
901         struct rb_node *from_node, *to_node;
902         struct module_info *from_m, *to_m;
903         int ret = -1;
904
905         if (read_proc_modules(from, &from_modules))
906                 return -1;
907
908         if (read_proc_modules(to, &to_modules))
909                 goto out_delete_from;
910
911         from_node = rb_first(&from_modules);
912         to_node = rb_first(&to_modules);
913         while (from_node) {
914                 if (!to_node)
915                         break;
916
917                 from_m = rb_entry(from_node, struct module_info, rb_node);
918                 to_m = rb_entry(to_node, struct module_info, rb_node);
919
920                 if (from_m->start != to_m->start ||
921                     strcmp(from_m->name, to_m->name))
922                         break;
923
924                 from_node = rb_next(from_node);
925                 to_node = rb_next(to_node);
926         }
927
928         if (!from_node && !to_node)
929                 ret = 0;
930
931         delete_modules(&to_modules);
932 out_delete_from:
933         delete_modules(&from_modules);
934
935         return ret;
936 }
937
938 static int do_validate_kcore_modules(const char *filename, struct map *map,
939                                   struct map_groups *kmaps)
940 {
941         struct rb_root modules = RB_ROOT;
942         struct map *old_map;
943         int err;
944
945         err = read_proc_modules(filename, &modules);
946         if (err)
947                 return err;
948
949         old_map = map_groups__first(kmaps, map->type);
950         while (old_map) {
951                 struct map *next = map_groups__next(old_map);
952                 struct module_info *mi;
953
954                 if (old_map == map || old_map->start == map->start) {
955                         /* The kernel map */
956                         old_map = next;
957                         continue;
958                 }
959
960                 /* Module must be in memory at the same address */
961                 mi = find_module(old_map->dso->short_name, &modules);
962                 if (!mi || mi->start != old_map->start) {
963                         err = -EINVAL;
964                         goto out;
965                 }
966
967                 old_map = next;
968         }
969 out:
970         delete_modules(&modules);
971         return err;
972 }
973
974 /*
975  * If kallsyms is referenced by name then we look for filename in the same
976  * directory.
977  */
978 static bool filename_from_kallsyms_filename(char *filename,
979                                             const char *base_name,
980                                             const char *kallsyms_filename)
981 {
982         char *name;
983
984         strcpy(filename, kallsyms_filename);
985         name = strrchr(filename, '/');
986         if (!name)
987                 return false;
988
989         name += 1;
990
991         if (!strcmp(name, "kallsyms")) {
992                 strcpy(name, base_name);
993                 return true;
994         }
995
996         return false;
997 }
998
999 static int validate_kcore_modules(const char *kallsyms_filename,
1000                                   struct map *map)
1001 {
1002         struct map_groups *kmaps = map__kmap(map)->kmaps;
1003         char modules_filename[PATH_MAX];
1004
1005         if (!filename_from_kallsyms_filename(modules_filename, "modules",
1006                                              kallsyms_filename))
1007                 return -EINVAL;
1008
1009         if (do_validate_kcore_modules(modules_filename, map, kmaps))
1010                 return -EINVAL;
1011
1012         return 0;
1013 }
1014
1015 static int validate_kcore_addresses(const char *kallsyms_filename,
1016                                     struct map *map)
1017 {
1018         struct kmap *kmap = map__kmap(map);
1019
1020         if (kmap->ref_reloc_sym && kmap->ref_reloc_sym->name) {
1021                 u64 start;
1022
1023                 start = kallsyms__get_function_start(kallsyms_filename,
1024                                                      kmap->ref_reloc_sym->name);
1025                 if (start != kmap->ref_reloc_sym->addr)
1026                         return -EINVAL;
1027         }
1028
1029         return validate_kcore_modules(kallsyms_filename, map);
1030 }
1031
1032 struct kcore_mapfn_data {
1033         struct dso *dso;
1034         enum map_type type;
1035         struct list_head maps;
1036 };
1037
1038 static int kcore_mapfn(u64 start, u64 len, u64 pgoff, void *data)
1039 {
1040         struct kcore_mapfn_data *md = data;
1041         struct map *map;
1042
1043         map = map__new2(start, md->dso, md->type);
1044         if (map == NULL)
1045                 return -ENOMEM;
1046
1047         map->end = map->start + len;
1048         map->pgoff = pgoff;
1049
1050         list_add(&map->node, &md->maps);
1051
1052         return 0;
1053 }
1054
1055 static int dso__load_kcore(struct dso *dso, struct map *map,
1056                            const char *kallsyms_filename)
1057 {
1058         struct map_groups *kmaps = map__kmap(map)->kmaps;
1059         struct machine *machine = kmaps->machine;
1060         struct kcore_mapfn_data md;
1061         struct map *old_map, *new_map, *replacement_map = NULL;
1062         bool is_64_bit;
1063         int err, fd;
1064         char kcore_filename[PATH_MAX];
1065         struct symbol *sym;
1066
1067         /* This function requires that the map is the kernel map */
1068         if (map != machine->vmlinux_maps[map->type])
1069                 return -EINVAL;
1070
1071         if (!filename_from_kallsyms_filename(kcore_filename, "kcore",
1072                                              kallsyms_filename))
1073                 return -EINVAL;
1074
1075         /* Modules and kernel must be present at their original addresses */
1076         if (validate_kcore_addresses(kallsyms_filename, map))
1077                 return -EINVAL;
1078
1079         md.dso = dso;
1080         md.type = map->type;
1081         INIT_LIST_HEAD(&md.maps);
1082
1083         fd = open(kcore_filename, O_RDONLY);
1084         if (fd < 0)
1085                 return -EINVAL;
1086
1087         /* Read new maps into temporary lists */
1088         err = file__read_maps(fd, md.type == MAP__FUNCTION, kcore_mapfn, &md,
1089                               &is_64_bit);
1090         if (err)
1091                 goto out_err;
1092         dso->is_64_bit = is_64_bit;
1093
1094         if (list_empty(&md.maps)) {
1095                 err = -EINVAL;
1096                 goto out_err;
1097         }
1098
1099         /* Remove old maps */
1100         old_map = map_groups__first(kmaps, map->type);
1101         while (old_map) {
1102                 struct map *next = map_groups__next(old_map);
1103
1104                 if (old_map != map)
1105                         map_groups__remove(kmaps, old_map);
1106                 old_map = next;
1107         }
1108
1109         /* Find the kernel map using the first symbol */
1110         sym = dso__first_symbol(dso, map->type);
1111         list_for_each_entry(new_map, &md.maps, node) {
1112                 if (sym && sym->start >= new_map->start &&
1113                     sym->start < new_map->end) {
1114                         replacement_map = new_map;
1115                         break;
1116                 }
1117         }
1118
1119         if (!replacement_map)
1120                 replacement_map = list_entry(md.maps.next, struct map, node);
1121
1122         /* Add new maps */
1123         while (!list_empty(&md.maps)) {
1124                 new_map = list_entry(md.maps.next, struct map, node);
1125                 list_del(&new_map->node);
1126                 if (new_map == replacement_map) {
1127                         map->start      = new_map->start;
1128                         map->end        = new_map->end;
1129                         map->pgoff      = new_map->pgoff;
1130                         map->map_ip     = new_map->map_ip;
1131                         map->unmap_ip   = new_map->unmap_ip;
1132                         map__delete(new_map);
1133                         /* Ensure maps are correctly ordered */
1134                         map_groups__remove(kmaps, map);
1135                         map_groups__insert(kmaps, map);
1136                 } else {
1137                         map_groups__insert(kmaps, new_map);
1138                 }
1139         }
1140
1141         /*
1142          * Set the data type and long name so that kcore can be read via
1143          * dso__data_read_addr().
1144          */
1145         if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1146                 dso->binary_type = DSO_BINARY_TYPE__GUEST_KCORE;
1147         else
1148                 dso->binary_type = DSO_BINARY_TYPE__KCORE;
1149         dso__set_long_name(dso, strdup(kcore_filename), true);
1150
1151         close(fd);
1152
1153         if (map->type == MAP__FUNCTION)
1154                 pr_debug("Using %s for kernel object code\n", kcore_filename);
1155         else
1156                 pr_debug("Using %s for kernel data\n", kcore_filename);
1157
1158         return 0;
1159
1160 out_err:
1161         while (!list_empty(&md.maps)) {
1162                 map = list_entry(md.maps.next, struct map, node);
1163                 list_del(&map->node);
1164                 map__delete(map);
1165         }
1166         close(fd);
1167         return -EINVAL;
1168 }
1169
1170 /*
1171  * If the kernel is relocated at boot time, kallsyms won't match.  Compute the
1172  * delta based on the relocation reference symbol.
1173  */
1174 static int kallsyms__delta(struct map *map, const char *filename, u64 *delta)
1175 {
1176         struct kmap *kmap = map__kmap(map);
1177         u64 addr;
1178
1179         if (!kmap->ref_reloc_sym || !kmap->ref_reloc_sym->name)
1180                 return 0;
1181
1182         addr = kallsyms__get_function_start(filename,
1183                                             kmap->ref_reloc_sym->name);
1184         if (!addr)
1185                 return -1;
1186
1187         *delta = addr - kmap->ref_reloc_sym->addr;
1188         return 0;
1189 }
1190
1191 int dso__load_kallsyms(struct dso *dso, const char *filename,
1192                        struct map *map, symbol_filter_t filter)
1193 {
1194         u64 delta = 0;
1195
1196         if (symbol__restricted_filename(filename, "/proc/kallsyms"))
1197                 return -1;
1198
1199         if (dso__load_all_kallsyms(dso, filename, map) < 0)
1200                 return -1;
1201
1202         if (kallsyms__delta(map, filename, &delta))
1203                 return -1;
1204
1205         symbols__fixup_duplicate(&dso->symbols[map->type]);
1206         symbols__fixup_end(&dso->symbols[map->type]);
1207
1208         if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1209                 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
1210         else
1211                 dso->symtab_type = DSO_BINARY_TYPE__KALLSYMS;
1212
1213         if (!dso__load_kcore(dso, map, filename))
1214                 return dso__split_kallsyms_for_kcore(dso, map, filter);
1215         else
1216                 return dso__split_kallsyms(dso, map, delta, filter);
1217 }
1218
1219 static int dso__load_perf_map(struct dso *dso, struct map *map,
1220                               symbol_filter_t filter)
1221 {
1222         char *line = NULL;
1223         size_t n;
1224         FILE *file;
1225         int nr_syms = 0;
1226
1227         file = fopen(dso->long_name, "r");
1228         if (file == NULL)
1229                 goto out_failure;
1230
1231         while (!feof(file)) {
1232                 u64 start, size;
1233                 struct symbol *sym;
1234                 int line_len, len;
1235
1236                 line_len = getline(&line, &n, file);
1237                 if (line_len < 0)
1238                         break;
1239
1240                 if (!line)
1241                         goto out_failure;
1242
1243                 line[--line_len] = '\0'; /* \n */
1244
1245                 len = hex2u64(line, &start);
1246
1247                 len++;
1248                 if (len + 2 >= line_len)
1249                         continue;
1250
1251                 len += hex2u64(line + len, &size);
1252
1253                 len++;
1254                 if (len + 2 >= line_len)
1255                         continue;
1256
1257                 sym = symbol__new(start, size, STB_GLOBAL, line + len);
1258
1259                 if (sym == NULL)
1260                         goto out_delete_line;
1261
1262                 if (filter && filter(map, sym))
1263                         symbol__delete(sym);
1264                 else {
1265                         symbols__insert(&dso->symbols[map->type], sym);
1266                         nr_syms++;
1267                 }
1268         }
1269
1270         free(line);
1271         fclose(file);
1272
1273         return nr_syms;
1274
1275 out_delete_line:
1276         free(line);
1277 out_failure:
1278         return -1;
1279 }
1280
1281 static bool dso__is_compatible_symtab_type(struct dso *dso, bool kmod,
1282                                            enum dso_binary_type type)
1283 {
1284         switch (type) {
1285         case DSO_BINARY_TYPE__JAVA_JIT:
1286         case DSO_BINARY_TYPE__DEBUGLINK:
1287         case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
1288         case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
1289         case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
1290         case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
1291         case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
1292                 return !kmod && dso->kernel == DSO_TYPE_USER;
1293
1294         case DSO_BINARY_TYPE__KALLSYMS:
1295         case DSO_BINARY_TYPE__VMLINUX:
1296         case DSO_BINARY_TYPE__KCORE:
1297                 return dso->kernel == DSO_TYPE_KERNEL;
1298
1299         case DSO_BINARY_TYPE__GUEST_KALLSYMS:
1300         case DSO_BINARY_TYPE__GUEST_VMLINUX:
1301         case DSO_BINARY_TYPE__GUEST_KCORE:
1302                 return dso->kernel == DSO_TYPE_GUEST_KERNEL;
1303
1304         case DSO_BINARY_TYPE__GUEST_KMODULE:
1305         case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
1306         case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
1307         case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
1308                 /*
1309                  * kernel modules know their symtab type - it's set when
1310                  * creating a module dso in machine__new_module().
1311                  */
1312                 return kmod && dso->symtab_type == type;
1313
1314         case DSO_BINARY_TYPE__BUILD_ID_CACHE:
1315                 return true;
1316
1317         case DSO_BINARY_TYPE__NOT_FOUND:
1318         default:
1319                 return false;
1320         }
1321 }
1322
1323 int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter)
1324 {
1325         char *name;
1326         int ret = -1;
1327         u_int i;
1328         struct machine *machine;
1329         char *root_dir = (char *) "";
1330         int ss_pos = 0;
1331         struct symsrc ss_[2];
1332         struct symsrc *syms_ss = NULL, *runtime_ss = NULL;
1333         bool kmod;
1334
1335         dso__set_loaded(dso, map->type);
1336
1337         if (dso->kernel == DSO_TYPE_KERNEL)
1338                 return dso__load_kernel_sym(dso, map, filter);
1339         else if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1340                 return dso__load_guest_kernel_sym(dso, map, filter);
1341
1342         if (map->groups && map->groups->machine)
1343                 machine = map->groups->machine;
1344         else
1345                 machine = NULL;
1346
1347         dso->adjust_symbols = 0;
1348
1349         if (strncmp(dso->name, "/tmp/perf-", 10) == 0) {
1350                 struct stat st;
1351
1352                 if (lstat(dso->name, &st) < 0)
1353                         return -1;
1354
1355                 if (st.st_uid && (st.st_uid != geteuid())) {
1356                         pr_warning("File %s not owned by current user or root, "
1357                                 "ignoring it.\n", dso->name);
1358                         return -1;
1359                 }
1360
1361                 ret = dso__load_perf_map(dso, map, filter);
1362                 dso->symtab_type = ret > 0 ? DSO_BINARY_TYPE__JAVA_JIT :
1363                                              DSO_BINARY_TYPE__NOT_FOUND;
1364                 return ret;
1365         }
1366
1367         if (machine)
1368                 root_dir = machine->root_dir;
1369
1370         name = malloc(PATH_MAX);
1371         if (!name)
1372                 return -1;
1373
1374         kmod = dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
1375                 dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
1376                 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE ||
1377                 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
1378
1379         /*
1380          * Iterate over candidate debug images.
1381          * Keep track of "interesting" ones (those which have a symtab, dynsym,
1382          * and/or opd section) for processing.
1383          */
1384         for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) {
1385                 struct symsrc *ss = &ss_[ss_pos];
1386                 bool next_slot = false;
1387
1388                 enum dso_binary_type symtab_type = binary_type_symtab[i];
1389
1390                 if (!dso__is_compatible_symtab_type(dso, kmod, symtab_type))
1391                         continue;
1392
1393                 if (dso__read_binary_type_filename(dso, symtab_type,
1394                                                    root_dir, name, PATH_MAX))
1395                         continue;
1396
1397                 /* Name is now the name of the next image to try */
1398                 if (symsrc__init(ss, dso, name, symtab_type) < 0)
1399                         continue;
1400
1401                 if (!syms_ss && symsrc__has_symtab(ss)) {
1402                         syms_ss = ss;
1403                         next_slot = true;
1404                         if (!dso->symsrc_filename)
1405                                 dso->symsrc_filename = strdup(name);
1406                 }
1407
1408                 if (!runtime_ss && symsrc__possibly_runtime(ss)) {
1409                         runtime_ss = ss;
1410                         next_slot = true;
1411                 }
1412
1413                 if (next_slot) {
1414                         ss_pos++;
1415
1416                         if (syms_ss && runtime_ss)
1417                                 break;
1418                 } else {
1419                         symsrc__destroy(ss);
1420                 }
1421
1422         }
1423
1424         if (!runtime_ss && !syms_ss)
1425                 goto out_free;
1426
1427         if (runtime_ss && !syms_ss) {
1428                 syms_ss = runtime_ss;
1429         }
1430
1431         /* We'll have to hope for the best */
1432         if (!runtime_ss && syms_ss)
1433                 runtime_ss = syms_ss;
1434
1435         if (syms_ss)
1436                 ret = dso__load_sym(dso, map, syms_ss, runtime_ss, filter, kmod);
1437         else
1438                 ret = -1;
1439
1440         if (ret > 0) {
1441                 int nr_plt;
1442
1443                 nr_plt = dso__synthesize_plt_symbols(dso, runtime_ss, map, filter);
1444                 if (nr_plt > 0)
1445                         ret += nr_plt;
1446         }
1447
1448         for (; ss_pos > 0; ss_pos--)
1449                 symsrc__destroy(&ss_[ss_pos - 1]);
1450 out_free:
1451         free(name);
1452         if (ret < 0 && strstr(dso->name, " (deleted)") != NULL)
1453                 return 0;
1454         return ret;
1455 }
1456
1457 struct map *map_groups__find_by_name(struct map_groups *mg,
1458                                      enum map_type type, const char *name)
1459 {
1460         struct rb_node *nd;
1461
1462         for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
1463                 struct map *map = rb_entry(nd, struct map, rb_node);
1464
1465                 if (map->dso && strcmp(map->dso->short_name, name) == 0)
1466                         return map;
1467         }
1468
1469         return NULL;
1470 }
1471
1472 int dso__load_vmlinux(struct dso *dso, struct map *map,
1473                       const char *vmlinux, bool vmlinux_allocated,
1474                       symbol_filter_t filter)
1475 {
1476         int err = -1;
1477         struct symsrc ss;
1478         char symfs_vmlinux[PATH_MAX];
1479         enum dso_binary_type symtab_type;
1480
1481         if (vmlinux[0] == '/')
1482                 snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s", vmlinux);
1483         else
1484                 symbol__join_symfs(symfs_vmlinux, vmlinux);
1485
1486         if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1487                 symtab_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
1488         else
1489                 symtab_type = DSO_BINARY_TYPE__VMLINUX;
1490
1491         if (symsrc__init(&ss, dso, symfs_vmlinux, symtab_type))
1492                 return -1;
1493
1494         err = dso__load_sym(dso, map, &ss, &ss, filter, 0);
1495         symsrc__destroy(&ss);
1496
1497         if (err > 0) {
1498                 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1499                         dso->binary_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
1500                 else
1501                         dso->binary_type = DSO_BINARY_TYPE__VMLINUX;
1502                 dso__set_long_name(dso, vmlinux, vmlinux_allocated);
1503                 dso__set_loaded(dso, map->type);
1504                 pr_debug("Using %s for symbols\n", symfs_vmlinux);
1505         }
1506
1507         return err;
1508 }
1509
1510 int dso__load_vmlinux_path(struct dso *dso, struct map *map,
1511                            symbol_filter_t filter)
1512 {
1513         int i, err = 0;
1514         char *filename;
1515
1516         pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1517                  vmlinux_path__nr_entries + 1);
1518
1519         filename = dso__build_id_filename(dso, NULL, 0);
1520         if (filename != NULL) {
1521                 err = dso__load_vmlinux(dso, map, filename, true, filter);
1522                 if (err > 0)
1523                         goto out;
1524                 free(filename);
1525         }
1526
1527         for (i = 0; i < vmlinux_path__nr_entries; ++i) {
1528                 err = dso__load_vmlinux(dso, map, vmlinux_path[i], false, filter);
1529                 if (err > 0)
1530                         break;
1531         }
1532 out:
1533         return err;
1534 }
1535
1536 static int find_matching_kcore(struct map *map, char *dir, size_t dir_sz)
1537 {
1538         char kallsyms_filename[PATH_MAX];
1539         struct dirent *dent;
1540         int ret = -1;
1541         DIR *d;
1542
1543         d = opendir(dir);
1544         if (!d)
1545                 return -1;
1546
1547         while (1) {
1548                 dent = readdir(d);
1549                 if (!dent)
1550                         break;
1551                 if (dent->d_type != DT_DIR)
1552                         continue;
1553                 scnprintf(kallsyms_filename, sizeof(kallsyms_filename),
1554                           "%s/%s/kallsyms", dir, dent->d_name);
1555                 if (!validate_kcore_addresses(kallsyms_filename, map)) {
1556                         strlcpy(dir, kallsyms_filename, dir_sz);
1557                         ret = 0;
1558                         break;
1559                 }
1560         }
1561
1562         closedir(d);
1563
1564         return ret;
1565 }
1566
1567 static char *dso__find_kallsyms(struct dso *dso, struct map *map)
1568 {
1569         u8 host_build_id[BUILD_ID_SIZE];
1570         char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1571         bool is_host = false;
1572         char path[PATH_MAX];
1573
1574         if (!dso->has_build_id) {
1575                 /*
1576                  * Last resort, if we don't have a build-id and couldn't find
1577                  * any vmlinux file, try the running kernel kallsyms table.
1578                  */
1579                 goto proc_kallsyms;
1580         }
1581
1582         if (sysfs__read_build_id("/sys/kernel/notes", host_build_id,
1583                                  sizeof(host_build_id)) == 0)
1584                 is_host = dso__build_id_equal(dso, host_build_id);
1585
1586         build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1587
1588         scnprintf(path, sizeof(path), "%s/[kernel.kcore]/%s", buildid_dir,
1589                   sbuild_id);
1590
1591         /* Use /proc/kallsyms if possible */
1592         if (is_host) {
1593                 DIR *d;
1594                 int fd;
1595
1596                 /* If no cached kcore go with /proc/kallsyms */
1597                 d = opendir(path);
1598                 if (!d)
1599                         goto proc_kallsyms;
1600                 closedir(d);
1601
1602                 /*
1603                  * Do not check the build-id cache, until we know we cannot use
1604                  * /proc/kcore.
1605                  */
1606                 fd = open("/proc/kcore", O_RDONLY);
1607                 if (fd != -1) {
1608                         close(fd);
1609                         /* If module maps match go with /proc/kallsyms */
1610                         if (!validate_kcore_addresses("/proc/kallsyms", map))
1611                                 goto proc_kallsyms;
1612                 }
1613
1614                 /* Find kallsyms in build-id cache with kcore */
1615                 if (!find_matching_kcore(map, path, sizeof(path)))
1616                         return strdup(path);
1617
1618                 goto proc_kallsyms;
1619         }
1620
1621         /* Find kallsyms in build-id cache with kcore */
1622         if (!find_matching_kcore(map, path, sizeof(path)))
1623                 return strdup(path);
1624
1625         scnprintf(path, sizeof(path), "%s/[kernel.kallsyms]/%s",
1626                   buildid_dir, sbuild_id);
1627
1628         if (access(path, F_OK)) {
1629                 pr_err("No kallsyms or vmlinux with build-id %s was found\n",
1630                        sbuild_id);
1631                 return NULL;
1632         }
1633
1634         return strdup(path);
1635
1636 proc_kallsyms:
1637         return strdup("/proc/kallsyms");
1638 }
1639
1640 static int dso__load_kernel_sym(struct dso *dso, struct map *map,
1641                                 symbol_filter_t filter)
1642 {
1643         int err;
1644         const char *kallsyms_filename = NULL;
1645         char *kallsyms_allocated_filename = NULL;
1646         /*
1647          * Step 1: if the user specified a kallsyms or vmlinux filename, use
1648          * it and only it, reporting errors to the user if it cannot be used.
1649          *
1650          * For instance, try to analyse an ARM perf.data file _without_ a
1651          * build-id, or if the user specifies the wrong path to the right
1652          * vmlinux file, obviously we can't fallback to another vmlinux (a
1653          * x86_86 one, on the machine where analysis is being performed, say),
1654          * or worse, /proc/kallsyms.
1655          *
1656          * If the specified file _has_ a build-id and there is a build-id
1657          * section in the perf.data file, we will still do the expected
1658          * validation in dso__load_vmlinux and will bail out if they don't
1659          * match.
1660          */
1661         if (symbol_conf.kallsyms_name != NULL) {
1662                 kallsyms_filename = symbol_conf.kallsyms_name;
1663                 goto do_kallsyms;
1664         }
1665
1666         if (!symbol_conf.ignore_vmlinux && symbol_conf.vmlinux_name != NULL) {
1667                 return dso__load_vmlinux(dso, map, symbol_conf.vmlinux_name,
1668                                          false, filter);
1669         }
1670
1671         if (!symbol_conf.ignore_vmlinux && vmlinux_path != NULL) {
1672                 err = dso__load_vmlinux_path(dso, map, filter);
1673                 if (err > 0)
1674                         return err;
1675         }
1676
1677         /* do not try local files if a symfs was given */
1678         if (symbol_conf.symfs[0] != 0)
1679                 return -1;
1680
1681         kallsyms_allocated_filename = dso__find_kallsyms(dso, map);
1682         if (!kallsyms_allocated_filename)
1683                 return -1;
1684
1685         kallsyms_filename = kallsyms_allocated_filename;
1686
1687 do_kallsyms:
1688         err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
1689         if (err > 0)
1690                 pr_debug("Using %s for symbols\n", kallsyms_filename);
1691         free(kallsyms_allocated_filename);
1692
1693         if (err > 0 && !dso__is_kcore(dso)) {
1694                 dso->binary_type = DSO_BINARY_TYPE__KALLSYMS;
1695                 dso__set_long_name(dso, "[kernel.kallsyms]", false);
1696                 map__fixup_start(map);
1697                 map__fixup_end(map);
1698         }
1699
1700         return err;
1701 }
1702
1703 static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
1704                                       symbol_filter_t filter)
1705 {
1706         int err;
1707         const char *kallsyms_filename = NULL;
1708         struct machine *machine;
1709         char path[PATH_MAX];
1710
1711         if (!map->groups) {
1712                 pr_debug("Guest kernel map hasn't the point to groups\n");
1713                 return -1;
1714         }
1715         machine = map->groups->machine;
1716
1717         if (machine__is_default_guest(machine)) {
1718                 /*
1719                  * if the user specified a vmlinux filename, use it and only
1720                  * it, reporting errors to the user if it cannot be used.
1721                  * Or use file guest_kallsyms inputted by user on commandline
1722                  */
1723                 if (symbol_conf.default_guest_vmlinux_name != NULL) {
1724                         err = dso__load_vmlinux(dso, map,
1725                                                 symbol_conf.default_guest_vmlinux_name,
1726                                                 false, filter);
1727                         return err;
1728                 }
1729
1730                 kallsyms_filename = symbol_conf.default_guest_kallsyms;
1731                 if (!kallsyms_filename)
1732                         return -1;
1733         } else {
1734                 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
1735                 kallsyms_filename = path;
1736         }
1737
1738         err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
1739         if (err > 0)
1740                 pr_debug("Using %s for symbols\n", kallsyms_filename);
1741         if (err > 0 && !dso__is_kcore(dso)) {
1742                 dso->binary_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
1743                 machine__mmap_name(machine, path, sizeof(path));
1744                 dso__set_long_name(dso, strdup(path), true);
1745                 map__fixup_start(map);
1746                 map__fixup_end(map);
1747         }
1748
1749         return err;
1750 }
1751
1752 static void vmlinux_path__exit(void)
1753 {
1754         while (--vmlinux_path__nr_entries >= 0)
1755                 zfree(&vmlinux_path[vmlinux_path__nr_entries]);
1756
1757         zfree(&vmlinux_path);
1758 }
1759
1760 static int vmlinux_path__init(struct perf_session_env *env)
1761 {
1762         struct utsname uts;
1763         char bf[PATH_MAX];
1764         char *kernel_version;
1765
1766         vmlinux_path = malloc(sizeof(char *) * 6);
1767         if (vmlinux_path == NULL)
1768                 return -1;
1769
1770         vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
1771         if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1772                 goto out_fail;
1773         ++vmlinux_path__nr_entries;
1774         vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
1775         if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1776                 goto out_fail;
1777         ++vmlinux_path__nr_entries;
1778
1779         /* only try kernel version if no symfs was given */
1780         if (symbol_conf.symfs[0] != 0)
1781                 return 0;
1782
1783         if (env) {
1784                 kernel_version = env->os_release;
1785         } else {
1786                 if (uname(&uts) < 0)
1787                         goto out_fail;
1788
1789                 kernel_version = uts.release;
1790         }
1791
1792         snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", kernel_version);
1793         vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1794         if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1795                 goto out_fail;
1796         ++vmlinux_path__nr_entries;
1797         snprintf(bf, sizeof(bf), "/usr/lib/debug/boot/vmlinux-%s",
1798                  kernel_version);
1799         vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1800         if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1801                 goto out_fail;
1802         ++vmlinux_path__nr_entries;
1803         snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", kernel_version);
1804         vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1805         if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1806                 goto out_fail;
1807         ++vmlinux_path__nr_entries;
1808         snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
1809                  kernel_version);
1810         vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1811         if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1812                 goto out_fail;
1813         ++vmlinux_path__nr_entries;
1814
1815         return 0;
1816
1817 out_fail:
1818         vmlinux_path__exit();
1819         return -1;
1820 }
1821
1822 int setup_list(struct strlist **list, const char *list_str,
1823                       const char *list_name)
1824 {
1825         if (list_str == NULL)
1826                 return 0;
1827
1828         *list = strlist__new(true, list_str);
1829         if (!*list) {
1830                 pr_err("problems parsing %s list\n", list_name);
1831                 return -1;
1832         }
1833         return 0;
1834 }
1835
1836 static bool symbol__read_kptr_restrict(void)
1837 {
1838         bool value = false;
1839
1840         if (geteuid() != 0) {
1841                 FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
1842                 if (fp != NULL) {
1843                         char line[8];
1844
1845                         if (fgets(line, sizeof(line), fp) != NULL)
1846                                 value = atoi(line) != 0;
1847
1848                         fclose(fp);
1849                 }
1850         }
1851
1852         return value;
1853 }
1854
1855 int symbol__init(struct perf_session_env *env)
1856 {
1857         const char *symfs;
1858
1859         if (symbol_conf.initialized)
1860                 return 0;
1861
1862         symbol_conf.priv_size = PERF_ALIGN(symbol_conf.priv_size, sizeof(u64));
1863
1864         symbol__elf_init();
1865
1866         if (symbol_conf.sort_by_name)
1867                 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
1868                                           sizeof(struct symbol));
1869
1870         if (symbol_conf.try_vmlinux_path && vmlinux_path__init(env) < 0)
1871                 return -1;
1872
1873         if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
1874                 pr_err("'.' is the only non valid --field-separator argument\n");
1875                 return -1;
1876         }
1877
1878         if (setup_list(&symbol_conf.dso_list,
1879                        symbol_conf.dso_list_str, "dso") < 0)
1880                 return -1;
1881
1882         if (setup_list(&symbol_conf.comm_list,
1883                        symbol_conf.comm_list_str, "comm") < 0)
1884                 goto out_free_dso_list;
1885
1886         if (setup_list(&symbol_conf.sym_list,
1887                        symbol_conf.sym_list_str, "symbol") < 0)
1888                 goto out_free_comm_list;
1889
1890         /*
1891          * A path to symbols of "/" is identical to ""
1892          * reset here for simplicity.
1893          */
1894         symfs = realpath(symbol_conf.symfs, NULL);
1895         if (symfs == NULL)
1896                 symfs = symbol_conf.symfs;
1897         if (strcmp(symfs, "/") == 0)
1898                 symbol_conf.symfs = "";
1899         if (symfs != symbol_conf.symfs)
1900                 free((void *)symfs);
1901
1902         symbol_conf.kptr_restrict = symbol__read_kptr_restrict();
1903
1904         symbol_conf.initialized = true;
1905         return 0;
1906
1907 out_free_comm_list:
1908         strlist__delete(symbol_conf.comm_list);
1909 out_free_dso_list:
1910         strlist__delete(symbol_conf.dso_list);
1911         return -1;
1912 }
1913
1914 void symbol__exit(void)
1915 {
1916         if (!symbol_conf.initialized)
1917                 return;
1918         strlist__delete(symbol_conf.sym_list);
1919         strlist__delete(symbol_conf.dso_list);
1920         strlist__delete(symbol_conf.comm_list);
1921         vmlinux_path__exit();
1922         symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
1923         symbol_conf.initialized = false;
1924 }