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