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