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