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