perf session: Make read_build_id routines look at the host_machine too
[pandora-kernel.git] / tools / perf / util / symbol.c
1 #define _GNU_SOURCE
2 #include <ctype.h>
3 #include <dirent.h>
4 #include <errno.h>
5 #include <libgen.h>
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <string.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <sys/param.h>
12 #include <fcntl.h>
13 #include <unistd.h>
14 #include "symbol.h"
15 #include "strlist.h"
16
17 #include <libelf.h>
18 #include <gelf.h>
19 #include <elf.h>
20 #include <limits.h>
21 #include <sys/utsname.h>
22
23 #ifndef NT_GNU_BUILD_ID
24 #define NT_GNU_BUILD_ID 3
25 #endif
26
27 static void dsos__add(struct list_head *head, struct dso *dso);
28 static struct map *map__new2(u64 start, struct dso *dso, enum map_type type);
29 static int dso__load_kernel_sym(struct dso *self, struct map *map,
30                                 symbol_filter_t filter);
31 static int dso__load_guest_kernel_sym(struct dso *self, struct map *map,
32                         symbol_filter_t filter);
33 static int vmlinux_path__nr_entries;
34 static char **vmlinux_path;
35
36 struct symbol_conf symbol_conf = {
37         .exclude_other    = true,
38         .use_modules      = true,
39         .try_vmlinux_path = true,
40 };
41
42 bool dso__loaded(const struct dso *self, enum map_type type)
43 {
44         return self->loaded & (1 << type);
45 }
46
47 bool dso__sorted_by_name(const struct dso *self, enum map_type type)
48 {
49         return self->sorted_by_name & (1 << type);
50 }
51
52 static void dso__set_sorted_by_name(struct dso *self, enum map_type type)
53 {
54         self->sorted_by_name |= (1 << type);
55 }
56
57 bool symbol_type__is_a(char symbol_type, enum map_type map_type)
58 {
59         switch (map_type) {
60         case MAP__FUNCTION:
61                 return symbol_type == 'T' || symbol_type == 'W';
62         case MAP__VARIABLE:
63                 return symbol_type == 'D' || symbol_type == 'd';
64         default:
65                 return false;
66         }
67 }
68
69 static void symbols__fixup_end(struct rb_root *self)
70 {
71         struct rb_node *nd, *prevnd = rb_first(self);
72         struct symbol *curr, *prev;
73
74         if (prevnd == NULL)
75                 return;
76
77         curr = rb_entry(prevnd, struct symbol, rb_node);
78
79         for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
80                 prev = curr;
81                 curr = rb_entry(nd, struct symbol, rb_node);
82
83                 if (prev->end == prev->start)
84                         prev->end = curr->start - 1;
85         }
86
87         /* Last entry */
88         if (curr->end == curr->start)
89                 curr->end = roundup(curr->start, 4096);
90 }
91
92 static void __map_groups__fixup_end(struct map_groups *self, enum map_type type)
93 {
94         struct map *prev, *curr;
95         struct rb_node *nd, *prevnd = rb_first(&self->maps[type]);
96
97         if (prevnd == NULL)
98                 return;
99
100         curr = rb_entry(prevnd, struct map, rb_node);
101
102         for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
103                 prev = curr;
104                 curr = rb_entry(nd, struct map, rb_node);
105                 prev->end = curr->start - 1;
106         }
107
108         /*
109          * We still haven't the actual symbols, so guess the
110          * last map final address.
111          */
112         curr->end = ~0UL;
113 }
114
115 static void map_groups__fixup_end(struct map_groups *self)
116 {
117         int i;
118         for (i = 0; i < MAP__NR_TYPES; ++i)
119                 __map_groups__fixup_end(self, i);
120 }
121
122 static struct symbol *symbol__new(u64 start, u64 len, const char *name)
123 {
124         size_t namelen = strlen(name) + 1;
125         struct symbol *self = calloc(1, (symbol_conf.priv_size +
126                                          sizeof(*self) + namelen));
127         if (self == NULL)
128                 return NULL;
129
130         if (symbol_conf.priv_size)
131                 self = ((void *)self) + symbol_conf.priv_size;
132
133         self->start   = start;
134         self->end     = len ? start + len - 1 : start;
135         self->namelen = namelen - 1;
136
137         pr_debug4("%s: %s %#Lx-%#Lx\n", __func__, name, start, self->end);
138
139         memcpy(self->name, name, namelen);
140
141         return self;
142 }
143
144 void symbol__delete(struct symbol *self)
145 {
146         free(((void *)self) - symbol_conf.priv_size);
147 }
148
149 static size_t symbol__fprintf(struct symbol *self, FILE *fp)
150 {
151         return fprintf(fp, " %llx-%llx %s\n",
152                        self->start, self->end, self->name);
153 }
154
155 void dso__set_long_name(struct dso *self, char *name)
156 {
157         if (name == NULL)
158                 return;
159         self->long_name = name;
160         self->long_name_len = strlen(name);
161 }
162
163 static void dso__set_short_name(struct dso *self, const char *name)
164 {
165         if (name == NULL)
166                 return;
167         self->short_name = name;
168         self->short_name_len = strlen(name);
169 }
170
171 static void dso__set_basename(struct dso *self)
172 {
173         dso__set_short_name(self, basename(self->long_name));
174 }
175
176 struct dso *dso__new(const char *name)
177 {
178         struct dso *self = calloc(1, sizeof(*self) + strlen(name) + 1);
179
180         if (self != NULL) {
181                 int i;
182                 strcpy(self->name, name);
183                 dso__set_long_name(self, self->name);
184                 dso__set_short_name(self, self->name);
185                 for (i = 0; i < MAP__NR_TYPES; ++i)
186                         self->symbols[i] = self->symbol_names[i] = RB_ROOT;
187                 self->slen_calculated = 0;
188                 self->origin = DSO__ORIG_NOT_FOUND;
189                 self->loaded = 0;
190                 self->sorted_by_name = 0;
191                 self->has_build_id = 0;
192                 self->kernel = DSO_TYPE_USER;
193                 INIT_LIST_HEAD(&self->node);
194         }
195
196         return self;
197 }
198
199 static void symbols__delete(struct rb_root *self)
200 {
201         struct symbol *pos;
202         struct rb_node *next = rb_first(self);
203
204         while (next) {
205                 pos = rb_entry(next, struct symbol, rb_node);
206                 next = rb_next(&pos->rb_node);
207                 rb_erase(&pos->rb_node, self);
208                 symbol__delete(pos);
209         }
210 }
211
212 void dso__delete(struct dso *self)
213 {
214         int i;
215         for (i = 0; i < MAP__NR_TYPES; ++i)
216                 symbols__delete(&self->symbols[i]);
217         if (self->long_name != self->name)
218                 free(self->long_name);
219         free(self);
220 }
221
222 void dso__set_build_id(struct dso *self, void *build_id)
223 {
224         memcpy(self->build_id, build_id, sizeof(self->build_id));
225         self->has_build_id = 1;
226 }
227
228 static void symbols__insert(struct rb_root *self, struct symbol *sym)
229 {
230         struct rb_node **p = &self->rb_node;
231         struct rb_node *parent = NULL;
232         const u64 ip = sym->start;
233         struct symbol *s;
234
235         while (*p != NULL) {
236                 parent = *p;
237                 s = rb_entry(parent, struct symbol, rb_node);
238                 if (ip < s->start)
239                         p = &(*p)->rb_left;
240                 else
241                         p = &(*p)->rb_right;
242         }
243         rb_link_node(&sym->rb_node, parent, p);
244         rb_insert_color(&sym->rb_node, self);
245 }
246
247 static struct symbol *symbols__find(struct rb_root *self, u64 ip)
248 {
249         struct rb_node *n;
250
251         if (self == NULL)
252                 return NULL;
253
254         n = self->rb_node;
255
256         while (n) {
257                 struct symbol *s = rb_entry(n, struct symbol, rb_node);
258
259                 if (ip < s->start)
260                         n = n->rb_left;
261                 else if (ip > s->end)
262                         n = n->rb_right;
263                 else
264                         return s;
265         }
266
267         return NULL;
268 }
269
270 struct symbol_name_rb_node {
271         struct rb_node  rb_node;
272         struct symbol   sym;
273 };
274
275 static void symbols__insert_by_name(struct rb_root *self, struct symbol *sym)
276 {
277         struct rb_node **p = &self->rb_node;
278         struct rb_node *parent = NULL;
279         struct symbol_name_rb_node *symn = ((void *)sym) - sizeof(*parent), *s;
280
281         while (*p != NULL) {
282                 parent = *p;
283                 s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
284                 if (strcmp(sym->name, s->sym.name) < 0)
285                         p = &(*p)->rb_left;
286                 else
287                         p = &(*p)->rb_right;
288         }
289         rb_link_node(&symn->rb_node, parent, p);
290         rb_insert_color(&symn->rb_node, self);
291 }
292
293 static void symbols__sort_by_name(struct rb_root *self, struct rb_root *source)
294 {
295         struct rb_node *nd;
296
297         for (nd = rb_first(source); nd; nd = rb_next(nd)) {
298                 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
299                 symbols__insert_by_name(self, pos);
300         }
301 }
302
303 static struct symbol *symbols__find_by_name(struct rb_root *self, const char *name)
304 {
305         struct rb_node *n;
306
307         if (self == NULL)
308                 return NULL;
309
310         n = self->rb_node;
311
312         while (n) {
313                 struct symbol_name_rb_node *s;
314                 int cmp;
315
316                 s = rb_entry(n, struct symbol_name_rb_node, rb_node);
317                 cmp = strcmp(name, s->sym.name);
318
319                 if (cmp < 0)
320                         n = n->rb_left;
321                 else if (cmp > 0)
322                         n = n->rb_right;
323                 else
324                         return &s->sym;
325         }
326
327         return NULL;
328 }
329
330 struct symbol *dso__find_symbol(struct dso *self,
331                                 enum map_type type, u64 addr)
332 {
333         return symbols__find(&self->symbols[type], addr);
334 }
335
336 struct symbol *dso__find_symbol_by_name(struct dso *self, enum map_type type,
337                                         const char *name)
338 {
339         return symbols__find_by_name(&self->symbol_names[type], name);
340 }
341
342 void dso__sort_by_name(struct dso *self, enum map_type type)
343 {
344         dso__set_sorted_by_name(self, type);
345         return symbols__sort_by_name(&self->symbol_names[type],
346                                      &self->symbols[type]);
347 }
348
349 int build_id__sprintf(const u8 *self, int len, char *bf)
350 {
351         char *bid = bf;
352         const u8 *raw = self;
353         int i;
354
355         for (i = 0; i < len; ++i) {
356                 sprintf(bid, "%02x", *raw);
357                 ++raw;
358                 bid += 2;
359         }
360
361         return raw - self;
362 }
363
364 size_t dso__fprintf_buildid(struct dso *self, FILE *fp)
365 {
366         char sbuild_id[BUILD_ID_SIZE * 2 + 1];
367
368         build_id__sprintf(self->build_id, sizeof(self->build_id), sbuild_id);
369         return fprintf(fp, "%s", sbuild_id);
370 }
371
372 size_t dso__fprintf(struct dso *self, enum map_type type, FILE *fp)
373 {
374         struct rb_node *nd;
375         size_t ret = fprintf(fp, "dso: %s (", self->short_name);
376
377         if (self->short_name != self->long_name)
378                 ret += fprintf(fp, "%s, ", self->long_name);
379         ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
380                        self->loaded ? "" : "NOT ");
381         ret += dso__fprintf_buildid(self, fp);
382         ret += fprintf(fp, ")\n");
383         for (nd = rb_first(&self->symbols[type]); nd; nd = rb_next(nd)) {
384                 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
385                 ret += symbol__fprintf(pos, fp);
386         }
387
388         return ret;
389 }
390
391 int kallsyms__parse(const char *filename, void *arg,
392                     int (*process_symbol)(void *arg, const char *name,
393                                                      char type, u64 start))
394 {
395         char *line = NULL;
396         size_t n;
397         int err = 0;
398         FILE *file = fopen(filename, "r");
399
400         if (file == NULL)
401                 goto out_failure;
402
403         while (!feof(file)) {
404                 u64 start;
405                 int line_len, len;
406                 char symbol_type;
407                 char *symbol_name;
408
409                 line_len = getline(&line, &n, file);
410                 if (line_len < 0 || !line)
411                         break;
412
413                 line[--line_len] = '\0'; /* \n */
414
415                 len = hex2u64(line, &start);
416
417                 len++;
418                 if (len + 2 >= line_len)
419                         continue;
420
421                 symbol_type = toupper(line[len]);
422                 symbol_name = line + len + 2;
423
424                 err = process_symbol(arg, symbol_name, symbol_type, start);
425                 if (err)
426                         break;
427         }
428
429         free(line);
430         fclose(file);
431         return err;
432
433 out_failure:
434         return -1;
435 }
436
437 struct process_kallsyms_args {
438         struct map *map;
439         struct dso *dso;
440 };
441
442 static int map__process_kallsym_symbol(void *arg, const char *name,
443                                        char type, u64 start)
444 {
445         struct symbol *sym;
446         struct process_kallsyms_args *a = arg;
447         struct rb_root *root = &a->dso->symbols[a->map->type];
448
449         if (!symbol_type__is_a(type, a->map->type))
450                 return 0;
451
452         /*
453          * Will fix up the end later, when we have all symbols sorted.
454          */
455         sym = symbol__new(start, 0, name);
456
457         if (sym == NULL)
458                 return -ENOMEM;
459         /*
460          * We will pass the symbols to the filter later, in
461          * map__split_kallsyms, when we have split the maps per module
462          */
463         symbols__insert(root, sym);
464
465         return 0;
466 }
467
468 /*
469  * Loads the function entries in /proc/kallsyms into kernel_map->dso,
470  * so that we can in the next step set the symbol ->end address and then
471  * call kernel_maps__split_kallsyms.
472  */
473 static int dso__load_all_kallsyms(struct dso *self, const char *filename,
474                                   struct map *map)
475 {
476         struct process_kallsyms_args args = { .map = map, .dso = self, };
477         return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
478 }
479
480 /*
481  * Split the symbols into maps, making sure there are no overlaps, i.e. the
482  * kernel range is broken in several maps, named [kernel].N, as we don't have
483  * the original ELF section names vmlinux have.
484  */
485 static int dso__split_kallsyms(struct dso *self, struct map *map,
486                                symbol_filter_t filter)
487 {
488         struct map_groups *kmaps = map__kmap(map)->kmaps;
489         struct machine *machine = kmaps->machine;
490         struct map *curr_map = map;
491         struct symbol *pos;
492         int count = 0;
493         struct rb_root *root = &self->symbols[map->type];
494         struct rb_node *next = rb_first(root);
495         int kernel_range = 0;
496
497         while (next) {
498                 char *module;
499
500                 pos = rb_entry(next, struct symbol, rb_node);
501                 next = rb_next(&pos->rb_node);
502
503                 module = strchr(pos->name, '\t');
504                 if (module) {
505                         if (!symbol_conf.use_modules)
506                                 goto discard_symbol;
507
508                         *module++ = '\0';
509
510                         if (strcmp(curr_map->dso->short_name, module)) {
511                                 if (curr_map != map &&
512                                     self->kernel == DSO_TYPE_GUEST_KERNEL &&
513                                     machine__is_default_guest(machine)) {
514                                         /*
515                                          * We assume all symbols of a module are
516                                          * continuous in * kallsyms, so curr_map
517                                          * points to a module and all its
518                                          * symbols are in its kmap. Mark it as
519                                          * loaded.
520                                          */
521                                         dso__set_loaded(curr_map->dso,
522                                                         curr_map->type);
523                                 }
524
525                                 curr_map = map_groups__find_by_name(kmaps,
526                                                         map->type, module);
527                                 if (curr_map == NULL) {
528                                         pr_debug("%s/proc/{kallsyms,modules} "
529                                                  "inconsistency while looking "
530                                                  "for \"%s\" module!\n",
531                                                  machine->root_dir, module);
532                                         curr_map = map;
533                                         goto discard_symbol;
534                                 }
535
536                                 if (curr_map->dso->loaded &&
537                                     !machine__is_default_guest(machine))
538                                         goto discard_symbol;
539                         }
540                         /*
541                          * So that we look just like we get from .ko files,
542                          * i.e. not prelinked, relative to map->start.
543                          */
544                         pos->start = curr_map->map_ip(curr_map, pos->start);
545                         pos->end   = curr_map->map_ip(curr_map, pos->end);
546                 } else if (curr_map != map) {
547                         char dso_name[PATH_MAX];
548                         struct dso *dso;
549
550                         if (self->kernel == DSO_TYPE_GUEST_KERNEL)
551                                 snprintf(dso_name, sizeof(dso_name),
552                                         "[guest.kernel].%d",
553                                         kernel_range++);
554                         else
555                                 snprintf(dso_name, sizeof(dso_name),
556                                         "[kernel].%d",
557                                         kernel_range++);
558
559                         dso = dso__new(dso_name);
560                         if (dso == NULL)
561                                 return -1;
562
563                         dso->kernel = self->kernel;
564
565                         curr_map = map__new2(pos->start, dso, map->type);
566                         if (curr_map == NULL) {
567                                 dso__delete(dso);
568                                 return -1;
569                         }
570
571                         curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
572                         map_groups__insert(kmaps, curr_map);
573                         ++kernel_range;
574                 }
575
576                 if (filter && filter(curr_map, pos)) {
577 discard_symbol:         rb_erase(&pos->rb_node, root);
578                         symbol__delete(pos);
579                 } else {
580                         if (curr_map != map) {
581                                 rb_erase(&pos->rb_node, root);
582                                 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
583                         }
584                         count++;
585                 }
586         }
587
588         if (curr_map != map &&
589             self->kernel == DSO_TYPE_GUEST_KERNEL &&
590             machine__is_default_guest(kmaps->machine)) {
591                 dso__set_loaded(curr_map->dso, curr_map->type);
592         }
593
594         return count;
595 }
596
597 int dso__load_kallsyms(struct dso *self, const char *filename,
598                        struct map *map, symbol_filter_t filter)
599 {
600         if (dso__load_all_kallsyms(self, filename, map) < 0)
601                 return -1;
602
603         symbols__fixup_end(&self->symbols[map->type]);
604         if (self->kernel == DSO_TYPE_GUEST_KERNEL)
605                 self->origin = DSO__ORIG_GUEST_KERNEL;
606         else
607                 self->origin = DSO__ORIG_KERNEL;
608
609         return dso__split_kallsyms(self, map, filter);
610 }
611
612 static int dso__load_perf_map(struct dso *self, struct map *map,
613                               symbol_filter_t filter)
614 {
615         char *line = NULL;
616         size_t n;
617         FILE *file;
618         int nr_syms = 0;
619
620         file = fopen(self->long_name, "r");
621         if (file == NULL)
622                 goto out_failure;
623
624         while (!feof(file)) {
625                 u64 start, size;
626                 struct symbol *sym;
627                 int line_len, len;
628
629                 line_len = getline(&line, &n, file);
630                 if (line_len < 0)
631                         break;
632
633                 if (!line)
634                         goto out_failure;
635
636                 line[--line_len] = '\0'; /* \n */
637
638                 len = hex2u64(line, &start);
639
640                 len++;
641                 if (len + 2 >= line_len)
642                         continue;
643
644                 len += hex2u64(line + len, &size);
645
646                 len++;
647                 if (len + 2 >= line_len)
648                         continue;
649
650                 sym = symbol__new(start, size, line + len);
651
652                 if (sym == NULL)
653                         goto out_delete_line;
654
655                 if (filter && filter(map, sym))
656                         symbol__delete(sym);
657                 else {
658                         symbols__insert(&self->symbols[map->type], sym);
659                         nr_syms++;
660                 }
661         }
662
663         free(line);
664         fclose(file);
665
666         return nr_syms;
667
668 out_delete_line:
669         free(line);
670 out_failure:
671         return -1;
672 }
673
674 /**
675  * elf_symtab__for_each_symbol - iterate thru all the symbols
676  *
677  * @self: struct elf_symtab instance to iterate
678  * @idx: uint32_t idx
679  * @sym: GElf_Sym iterator
680  */
681 #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
682         for (idx = 0, gelf_getsym(syms, idx, &sym);\
683              idx < nr_syms; \
684              idx++, gelf_getsym(syms, idx, &sym))
685
686 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
687 {
688         return GELF_ST_TYPE(sym->st_info);
689 }
690
691 static inline int elf_sym__is_function(const GElf_Sym *sym)
692 {
693         return elf_sym__type(sym) == STT_FUNC &&
694                sym->st_name != 0 &&
695                sym->st_shndx != SHN_UNDEF;
696 }
697
698 static inline bool elf_sym__is_object(const GElf_Sym *sym)
699 {
700         return elf_sym__type(sym) == STT_OBJECT &&
701                 sym->st_name != 0 &&
702                 sym->st_shndx != SHN_UNDEF;
703 }
704
705 static inline int elf_sym__is_label(const GElf_Sym *sym)
706 {
707         return elf_sym__type(sym) == STT_NOTYPE &&
708                 sym->st_name != 0 &&
709                 sym->st_shndx != SHN_UNDEF &&
710                 sym->st_shndx != SHN_ABS;
711 }
712
713 static inline const char *elf_sec__name(const GElf_Shdr *shdr,
714                                         const Elf_Data *secstrs)
715 {
716         return secstrs->d_buf + shdr->sh_name;
717 }
718
719 static inline int elf_sec__is_text(const GElf_Shdr *shdr,
720                                         const Elf_Data *secstrs)
721 {
722         return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
723 }
724
725 static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
726                                     const Elf_Data *secstrs)
727 {
728         return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
729 }
730
731 static inline const char *elf_sym__name(const GElf_Sym *sym,
732                                         const Elf_Data *symstrs)
733 {
734         return symstrs->d_buf + sym->st_name;
735 }
736
737 static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
738                                     GElf_Shdr *shp, const char *name,
739                                     size_t *idx)
740 {
741         Elf_Scn *sec = NULL;
742         size_t cnt = 1;
743
744         while ((sec = elf_nextscn(elf, sec)) != NULL) {
745                 char *str;
746
747                 gelf_getshdr(sec, shp);
748                 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
749                 if (!strcmp(name, str)) {
750                         if (idx)
751                                 *idx = cnt;
752                         break;
753                 }
754                 ++cnt;
755         }
756
757         return sec;
758 }
759
760 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
761         for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
762              idx < nr_entries; \
763              ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
764
765 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
766         for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
767              idx < nr_entries; \
768              ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
769
770 /*
771  * We need to check if we have a .dynsym, so that we can handle the
772  * .plt, synthesizing its symbols, that aren't on the symtabs (be it
773  * .dynsym or .symtab).
774  * And always look at the original dso, not at debuginfo packages, that
775  * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
776  */
777 static int dso__synthesize_plt_symbols(struct  dso *self, struct map *map,
778                                        symbol_filter_t filter)
779 {
780         uint32_t nr_rel_entries, idx;
781         GElf_Sym sym;
782         u64 plt_offset;
783         GElf_Shdr shdr_plt;
784         struct symbol *f;
785         GElf_Shdr shdr_rel_plt, shdr_dynsym;
786         Elf_Data *reldata, *syms, *symstrs;
787         Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
788         size_t dynsym_idx;
789         GElf_Ehdr ehdr;
790         char sympltname[1024];
791         Elf *elf;
792         int nr = 0, symidx, fd, err = 0;
793
794         fd = open(self->long_name, O_RDONLY);
795         if (fd < 0)
796                 goto out;
797
798         elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
799         if (elf == NULL)
800                 goto out_close;
801
802         if (gelf_getehdr(elf, &ehdr) == NULL)
803                 goto out_elf_end;
804
805         scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
806                                          ".dynsym", &dynsym_idx);
807         if (scn_dynsym == NULL)
808                 goto out_elf_end;
809
810         scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
811                                           ".rela.plt", NULL);
812         if (scn_plt_rel == NULL) {
813                 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
814                                                   ".rel.plt", NULL);
815                 if (scn_plt_rel == NULL)
816                         goto out_elf_end;
817         }
818
819         err = -1;
820
821         if (shdr_rel_plt.sh_link != dynsym_idx)
822                 goto out_elf_end;
823
824         if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
825                 goto out_elf_end;
826
827         /*
828          * Fetch the relocation section to find the idxes to the GOT
829          * and the symbols in the .dynsym they refer to.
830          */
831         reldata = elf_getdata(scn_plt_rel, NULL);
832         if (reldata == NULL)
833                 goto out_elf_end;
834
835         syms = elf_getdata(scn_dynsym, NULL);
836         if (syms == NULL)
837                 goto out_elf_end;
838
839         scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
840         if (scn_symstrs == NULL)
841                 goto out_elf_end;
842
843         symstrs = elf_getdata(scn_symstrs, NULL);
844         if (symstrs == NULL)
845                 goto out_elf_end;
846
847         nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
848         plt_offset = shdr_plt.sh_offset;
849
850         if (shdr_rel_plt.sh_type == SHT_RELA) {
851                 GElf_Rela pos_mem, *pos;
852
853                 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
854                                            nr_rel_entries) {
855                         symidx = GELF_R_SYM(pos->r_info);
856                         plt_offset += shdr_plt.sh_entsize;
857                         gelf_getsym(syms, symidx, &sym);
858                         snprintf(sympltname, sizeof(sympltname),
859                                  "%s@plt", elf_sym__name(&sym, symstrs));
860
861                         f = symbol__new(plt_offset, shdr_plt.sh_entsize,
862                                         sympltname);
863                         if (!f)
864                                 goto out_elf_end;
865
866                         if (filter && filter(map, f))
867                                 symbol__delete(f);
868                         else {
869                                 symbols__insert(&self->symbols[map->type], f);
870                                 ++nr;
871                         }
872                 }
873         } else if (shdr_rel_plt.sh_type == SHT_REL) {
874                 GElf_Rel pos_mem, *pos;
875                 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
876                                           nr_rel_entries) {
877                         symidx = GELF_R_SYM(pos->r_info);
878                         plt_offset += shdr_plt.sh_entsize;
879                         gelf_getsym(syms, symidx, &sym);
880                         snprintf(sympltname, sizeof(sympltname),
881                                  "%s@plt", elf_sym__name(&sym, symstrs));
882
883                         f = symbol__new(plt_offset, shdr_plt.sh_entsize,
884                                         sympltname);
885                         if (!f)
886                                 goto out_elf_end;
887
888                         if (filter && filter(map, f))
889                                 symbol__delete(f);
890                         else {
891                                 symbols__insert(&self->symbols[map->type], f);
892                                 ++nr;
893                         }
894                 }
895         }
896
897         err = 0;
898 out_elf_end:
899         elf_end(elf);
900 out_close:
901         close(fd);
902
903         if (err == 0)
904                 return nr;
905 out:
906         pr_debug("%s: problems reading %s PLT info.\n",
907                  __func__, self->long_name);
908         return 0;
909 }
910
911 static bool elf_sym__is_a(GElf_Sym *self, enum map_type type)
912 {
913         switch (type) {
914         case MAP__FUNCTION:
915                 return elf_sym__is_function(self);
916         case MAP__VARIABLE:
917                 return elf_sym__is_object(self);
918         default:
919                 return false;
920         }
921 }
922
923 static bool elf_sec__is_a(GElf_Shdr *self, Elf_Data *secstrs, enum map_type type)
924 {
925         switch (type) {
926         case MAP__FUNCTION:
927                 return elf_sec__is_text(self, secstrs);
928         case MAP__VARIABLE:
929                 return elf_sec__is_data(self, secstrs);
930         default:
931                 return false;
932         }
933 }
934
935 static int dso__load_sym(struct dso *self, struct map *map, const char *name,
936                          int fd, symbol_filter_t filter, int kmodule)
937 {
938         struct kmap *kmap = self->kernel ? map__kmap(map) : NULL;
939         struct map *curr_map = map;
940         struct dso *curr_dso = self;
941         Elf_Data *symstrs, *secstrs;
942         uint32_t nr_syms;
943         int err = -1;
944         uint32_t idx;
945         GElf_Ehdr ehdr;
946         GElf_Shdr shdr;
947         Elf_Data *syms;
948         GElf_Sym sym;
949         Elf_Scn *sec, *sec_strndx;
950         Elf *elf;
951         int nr = 0;
952
953         elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
954         if (elf == NULL) {
955                 pr_err("%s: cannot read %s ELF file.\n", __func__, name);
956                 goto out_close;
957         }
958
959         if (gelf_getehdr(elf, &ehdr) == NULL) {
960                 pr_err("%s: cannot get elf header.\n", __func__);
961                 goto out_elf_end;
962         }
963
964         sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
965         if (sec == NULL) {
966                 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
967                 if (sec == NULL)
968                         goto out_elf_end;
969         }
970
971         syms = elf_getdata(sec, NULL);
972         if (syms == NULL)
973                 goto out_elf_end;
974
975         sec = elf_getscn(elf, shdr.sh_link);
976         if (sec == NULL)
977                 goto out_elf_end;
978
979         symstrs = elf_getdata(sec, NULL);
980         if (symstrs == NULL)
981                 goto out_elf_end;
982
983         sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
984         if (sec_strndx == NULL)
985                 goto out_elf_end;
986
987         secstrs = elf_getdata(sec_strndx, NULL);
988         if (secstrs == NULL)
989                 goto out_elf_end;
990
991         nr_syms = shdr.sh_size / shdr.sh_entsize;
992
993         memset(&sym, 0, sizeof(sym));
994         if (self->kernel == DSO_TYPE_USER) {
995                 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
996                                 elf_section_by_name(elf, &ehdr, &shdr,
997                                                      ".gnu.prelink_undo",
998                                                      NULL) != NULL);
999         } else self->adjust_symbols = 0;
1000
1001         elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
1002                 struct symbol *f;
1003                 const char *elf_name = elf_sym__name(&sym, symstrs);
1004                 char *demangled = NULL;
1005                 int is_label = elf_sym__is_label(&sym);
1006                 const char *section_name;
1007
1008                 if (kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name &&
1009                     strcmp(elf_name, kmap->ref_reloc_sym->name) == 0)
1010                         kmap->ref_reloc_sym->unrelocated_addr = sym.st_value;
1011
1012                 if (!is_label && !elf_sym__is_a(&sym, map->type))
1013                         continue;
1014
1015                 sec = elf_getscn(elf, sym.st_shndx);
1016                 if (!sec)
1017                         goto out_elf_end;
1018
1019                 gelf_getshdr(sec, &shdr);
1020
1021                 if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type))
1022                         continue;
1023
1024                 section_name = elf_sec__name(&shdr, secstrs);
1025
1026                 if (self->kernel != DSO_TYPE_USER || kmodule) {
1027                         char dso_name[PATH_MAX];
1028
1029                         if (strcmp(section_name,
1030                                    (curr_dso->short_name +
1031                                     self->short_name_len)) == 0)
1032                                 goto new_symbol;
1033
1034                         if (strcmp(section_name, ".text") == 0) {
1035                                 curr_map = map;
1036                                 curr_dso = self;
1037                                 goto new_symbol;
1038                         }
1039
1040                         snprintf(dso_name, sizeof(dso_name),
1041                                  "%s%s", self->short_name, section_name);
1042
1043                         curr_map = map_groups__find_by_name(kmap->kmaps, map->type, dso_name);
1044                         if (curr_map == NULL) {
1045                                 u64 start = sym.st_value;
1046
1047                                 if (kmodule)
1048                                         start += map->start + shdr.sh_offset;
1049
1050                                 curr_dso = dso__new(dso_name);
1051                                 if (curr_dso == NULL)
1052                                         goto out_elf_end;
1053                                 curr_dso->kernel = self->kernel;
1054                                 curr_map = map__new2(start, curr_dso,
1055                                                      map->type);
1056                                 if (curr_map == NULL) {
1057                                         dso__delete(curr_dso);
1058                                         goto out_elf_end;
1059                                 }
1060                                 curr_map->map_ip = identity__map_ip;
1061                                 curr_map->unmap_ip = identity__map_ip;
1062                                 curr_dso->origin = self->origin;
1063                                 map_groups__insert(kmap->kmaps, curr_map);
1064                                 dsos__add(&self->node, curr_dso);
1065                                 dso__set_loaded(curr_dso, map->type);
1066                         } else
1067                                 curr_dso = curr_map->dso;
1068
1069                         goto new_symbol;
1070                 }
1071
1072                 if (curr_dso->adjust_symbols) {
1073                         pr_debug4("%s: adjusting symbol: st_value: %#Lx "
1074                                   "sh_addr: %#Lx sh_offset: %#Lx\n", __func__,
1075                                   (u64)sym.st_value, (u64)shdr.sh_addr,
1076                                   (u64)shdr.sh_offset);
1077                         sym.st_value -= shdr.sh_addr - shdr.sh_offset;
1078                 }
1079                 /*
1080                  * We need to figure out if the object was created from C++ sources
1081                  * DWARF DW_compile_unit has this, but we don't always have access
1082                  * to it...
1083                  */
1084                 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
1085                 if (demangled != NULL)
1086                         elf_name = demangled;
1087 new_symbol:
1088                 f = symbol__new(sym.st_value, sym.st_size, elf_name);
1089                 free(demangled);
1090                 if (!f)
1091                         goto out_elf_end;
1092
1093                 if (filter && filter(curr_map, f))
1094                         symbol__delete(f);
1095                 else {
1096                         symbols__insert(&curr_dso->symbols[curr_map->type], f);
1097                         nr++;
1098                 }
1099         }
1100
1101         /*
1102          * For misannotated, zeroed, ASM function sizes.
1103          */
1104         if (nr > 0) {
1105                 symbols__fixup_end(&self->symbols[map->type]);
1106                 if (kmap) {
1107                         /*
1108                          * We need to fixup this here too because we create new
1109                          * maps here, for things like vsyscall sections.
1110                          */
1111                         __map_groups__fixup_end(kmap->kmaps, map->type);
1112                 }
1113         }
1114         err = nr;
1115 out_elf_end:
1116         elf_end(elf);
1117 out_close:
1118         return err;
1119 }
1120
1121 static bool dso__build_id_equal(const struct dso *self, u8 *build_id)
1122 {
1123         return memcmp(self->build_id, build_id, sizeof(self->build_id)) == 0;
1124 }
1125
1126 bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
1127 {
1128         bool have_build_id = false;
1129         struct dso *pos;
1130
1131         list_for_each_entry(pos, head, node) {
1132                 if (with_hits && !pos->hit)
1133                         continue;
1134                 if (pos->has_build_id) {
1135                         have_build_id = true;
1136                         continue;
1137                 }
1138                 if (filename__read_build_id(pos->long_name, pos->build_id,
1139                                             sizeof(pos->build_id)) > 0) {
1140                         have_build_id     = true;
1141                         pos->has_build_id = true;
1142                 }
1143         }
1144
1145         return have_build_id;
1146 }
1147
1148 /*
1149  * Align offset to 4 bytes as needed for note name and descriptor data.
1150  */
1151 #define NOTE_ALIGN(n) (((n) + 3) & -4U)
1152
1153 int filename__read_build_id(const char *filename, void *bf, size_t size)
1154 {
1155         int fd, err = -1;
1156         GElf_Ehdr ehdr;
1157         GElf_Shdr shdr;
1158         Elf_Data *data;
1159         Elf_Scn *sec;
1160         Elf_Kind ek;
1161         void *ptr;
1162         Elf *elf;
1163
1164         if (size < BUILD_ID_SIZE)
1165                 goto out;
1166
1167         fd = open(filename, O_RDONLY);
1168         if (fd < 0)
1169                 goto out;
1170
1171         elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1172         if (elf == NULL) {
1173                 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
1174                 goto out_close;
1175         }
1176
1177         ek = elf_kind(elf);
1178         if (ek != ELF_K_ELF)
1179                 goto out_elf_end;
1180
1181         if (gelf_getehdr(elf, &ehdr) == NULL) {
1182                 pr_err("%s: cannot get elf header.\n", __func__);
1183                 goto out_elf_end;
1184         }
1185
1186         sec = elf_section_by_name(elf, &ehdr, &shdr,
1187                                   ".note.gnu.build-id", NULL);
1188         if (sec == NULL) {
1189                 sec = elf_section_by_name(elf, &ehdr, &shdr,
1190                                           ".notes", NULL);
1191                 if (sec == NULL)
1192                         goto out_elf_end;
1193         }
1194
1195         data = elf_getdata(sec, NULL);
1196         if (data == NULL)
1197                 goto out_elf_end;
1198
1199         ptr = data->d_buf;
1200         while (ptr < (data->d_buf + data->d_size)) {
1201                 GElf_Nhdr *nhdr = ptr;
1202                 int namesz = NOTE_ALIGN(nhdr->n_namesz),
1203                     descsz = NOTE_ALIGN(nhdr->n_descsz);
1204                 const char *name;
1205
1206                 ptr += sizeof(*nhdr);
1207                 name = ptr;
1208                 ptr += namesz;
1209                 if (nhdr->n_type == NT_GNU_BUILD_ID &&
1210                     nhdr->n_namesz == sizeof("GNU")) {
1211                         if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
1212                                 memcpy(bf, ptr, BUILD_ID_SIZE);
1213                                 err = BUILD_ID_SIZE;
1214                                 break;
1215                         }
1216                 }
1217                 ptr += descsz;
1218         }
1219 out_elf_end:
1220         elf_end(elf);
1221 out_close:
1222         close(fd);
1223 out:
1224         return err;
1225 }
1226
1227 int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
1228 {
1229         int fd, err = -1;
1230
1231         if (size < BUILD_ID_SIZE)
1232                 goto out;
1233
1234         fd = open(filename, O_RDONLY);
1235         if (fd < 0)
1236                 goto out;
1237
1238         while (1) {
1239                 char bf[BUFSIZ];
1240                 GElf_Nhdr nhdr;
1241                 int namesz, descsz;
1242
1243                 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
1244                         break;
1245
1246                 namesz = NOTE_ALIGN(nhdr.n_namesz);
1247                 descsz = NOTE_ALIGN(nhdr.n_descsz);
1248                 if (nhdr.n_type == NT_GNU_BUILD_ID &&
1249                     nhdr.n_namesz == sizeof("GNU")) {
1250                         if (read(fd, bf, namesz) != namesz)
1251                                 break;
1252                         if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
1253                                 if (read(fd, build_id,
1254                                     BUILD_ID_SIZE) == BUILD_ID_SIZE) {
1255                                         err = 0;
1256                                         break;
1257                                 }
1258                         } else if (read(fd, bf, descsz) != descsz)
1259                                 break;
1260                 } else {
1261                         int n = namesz + descsz;
1262                         if (read(fd, bf, n) != n)
1263                                 break;
1264                 }
1265         }
1266         close(fd);
1267 out:
1268         return err;
1269 }
1270
1271 char dso__symtab_origin(const struct dso *self)
1272 {
1273         static const char origin[] = {
1274                 [DSO__ORIG_KERNEL] =   'k',
1275                 [DSO__ORIG_JAVA_JIT] = 'j',
1276                 [DSO__ORIG_BUILD_ID_CACHE] = 'B',
1277                 [DSO__ORIG_FEDORA] =   'f',
1278                 [DSO__ORIG_UBUNTU] =   'u',
1279                 [DSO__ORIG_BUILDID] =  'b',
1280                 [DSO__ORIG_DSO] =      'd',
1281                 [DSO__ORIG_KMODULE] =  'K',
1282                 [DSO__ORIG_GUEST_KERNEL] =  'g',
1283                 [DSO__ORIG_GUEST_KMODULE] =  'G',
1284         };
1285
1286         if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
1287                 return '!';
1288         return origin[self->origin];
1289 }
1290
1291 int dso__load(struct dso *self, struct map *map, symbol_filter_t filter)
1292 {
1293         int size = PATH_MAX;
1294         char *name;
1295         u8 build_id[BUILD_ID_SIZE];
1296         char build_id_hex[BUILD_ID_SIZE * 2 + 1];
1297         int ret = -1;
1298         int fd;
1299         struct machine *machine;
1300         const char *root_dir;
1301
1302         dso__set_loaded(self, map->type);
1303
1304         if (self->kernel == DSO_TYPE_KERNEL)
1305                 return dso__load_kernel_sym(self, map, filter);
1306         else if (self->kernel == DSO_TYPE_GUEST_KERNEL)
1307                 return dso__load_guest_kernel_sym(self, map, filter);
1308
1309         if (map->groups && map->groups->machine)
1310                 machine = map->groups->machine;
1311         else
1312                 machine = NULL;
1313
1314         name = malloc(size);
1315         if (!name)
1316                 return -1;
1317
1318         self->adjust_symbols = 0;
1319
1320         if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
1321                 ret = dso__load_perf_map(self, map, filter);
1322                 self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
1323                                          DSO__ORIG_NOT_FOUND;
1324                 return ret;
1325         }
1326
1327         self->origin = DSO__ORIG_BUILD_ID_CACHE;
1328
1329         if (self->has_build_id) {
1330                 build_id__sprintf(self->build_id, sizeof(self->build_id),
1331                                   build_id_hex);
1332                 snprintf(name, size, "%s/%s/.build-id/%.2s/%s",
1333                          getenv("HOME"), DEBUG_CACHE_DIR,
1334                          build_id_hex, build_id_hex + 2);
1335                 goto open_file;
1336         }
1337 more:
1338         do {
1339                 self->origin++;
1340                 switch (self->origin) {
1341                 case DSO__ORIG_FEDORA:
1342                         snprintf(name, size, "/usr/lib/debug%s.debug",
1343                                  self->long_name);
1344                         break;
1345                 case DSO__ORIG_UBUNTU:
1346                         snprintf(name, size, "/usr/lib/debug%s",
1347                                  self->long_name);
1348                         break;
1349                 case DSO__ORIG_BUILDID:
1350                         if (filename__read_build_id(self->long_name, build_id,
1351                                                     sizeof(build_id))) {
1352                                 build_id__sprintf(build_id, sizeof(build_id),
1353                                                   build_id_hex);
1354                                 snprintf(name, size,
1355                                          "/usr/lib/debug/.build-id/%.2s/%s.debug",
1356                                         build_id_hex, build_id_hex + 2);
1357                                 if (self->has_build_id)
1358                                         goto compare_build_id;
1359                                 break;
1360                         }
1361                         self->origin++;
1362                         /* Fall thru */
1363                 case DSO__ORIG_DSO:
1364                         snprintf(name, size, "%s", self->long_name);
1365                         break;
1366                 case DSO__ORIG_GUEST_KMODULE:
1367                         if (map->groups && map->groups->machine)
1368                                 root_dir = map->groups->machine->root_dir;
1369                         else
1370                                 root_dir = "";
1371                         snprintf(name, size, "%s%s", root_dir, self->long_name);
1372                         break;
1373
1374                 default:
1375                         goto out;
1376                 }
1377
1378                 if (self->has_build_id) {
1379                         if (filename__read_build_id(name, build_id,
1380                                                     sizeof(build_id)) < 0)
1381                                 goto more;
1382 compare_build_id:
1383                         if (!dso__build_id_equal(self, build_id))
1384                                 goto more;
1385                 }
1386 open_file:
1387                 fd = open(name, O_RDONLY);
1388         } while (fd < 0);
1389
1390         ret = dso__load_sym(self, map, name, fd, filter, 0);
1391         close(fd);
1392
1393         /*
1394          * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
1395          */
1396         if (!ret)
1397                 goto more;
1398
1399         if (ret > 0) {
1400                 int nr_plt = dso__synthesize_plt_symbols(self, map, filter);
1401                 if (nr_plt > 0)
1402                         ret += nr_plt;
1403         }
1404 out:
1405         free(name);
1406         if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
1407                 return 0;
1408         return ret;
1409 }
1410
1411 struct map *map_groups__find_by_name(struct map_groups *self,
1412                                      enum map_type type, const char *name)
1413 {
1414         struct rb_node *nd;
1415
1416         for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) {
1417                 struct map *map = rb_entry(nd, struct map, rb_node);
1418
1419                 if (map->dso && strcmp(map->dso->short_name, name) == 0)
1420                         return map;
1421         }
1422
1423         return NULL;
1424 }
1425
1426 static int dso__kernel_module_get_build_id(struct dso *self,
1427                                 const char *root_dir)
1428 {
1429         char filename[PATH_MAX];
1430         /*
1431          * kernel module short names are of the form "[module]" and
1432          * we need just "module" here.
1433          */
1434         const char *name = self->short_name + 1;
1435
1436         snprintf(filename, sizeof(filename),
1437                  "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1438                  root_dir, (int)strlen(name) - 1, name);
1439
1440         if (sysfs__read_build_id(filename, self->build_id,
1441                                  sizeof(self->build_id)) == 0)
1442                 self->has_build_id = true;
1443
1444         return 0;
1445 }
1446
1447 static int map_groups__set_modules_path_dir(struct map_groups *self,
1448                                 const char *dir_name)
1449 {
1450         struct dirent *dent;
1451         DIR *dir = opendir(dir_name);
1452
1453         if (!dir) {
1454                 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
1455                 return -1;
1456         }
1457
1458         while ((dent = readdir(dir)) != NULL) {
1459                 char path[PATH_MAX];
1460                 struct stat st;
1461
1462                 /*sshfs might return bad dent->d_type, so we have to stat*/
1463                 sprintf(path, "%s/%s", dir_name, dent->d_name);
1464                 if (stat(path, &st))
1465                         continue;
1466
1467                 if (S_ISDIR(st.st_mode)) {
1468                         if (!strcmp(dent->d_name, ".") ||
1469                             !strcmp(dent->d_name, ".."))
1470                                 continue;
1471
1472                         snprintf(path, sizeof(path), "%s/%s",
1473                                  dir_name, dent->d_name);
1474                         if (map_groups__set_modules_path_dir(self, path) < 0)
1475                                 goto failure;
1476                 } else {
1477                         char *dot = strrchr(dent->d_name, '.'),
1478                              dso_name[PATH_MAX];
1479                         struct map *map;
1480                         char *long_name;
1481
1482                         if (dot == NULL || strcmp(dot, ".ko"))
1483                                 continue;
1484                         snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1485                                  (int)(dot - dent->d_name), dent->d_name);
1486
1487                         strxfrchar(dso_name, '-', '_');
1488                         map = map_groups__find_by_name(self, MAP__FUNCTION, dso_name);
1489                         if (map == NULL)
1490                                 continue;
1491
1492                         snprintf(path, sizeof(path), "%s/%s",
1493                                  dir_name, dent->d_name);
1494
1495                         long_name = strdup(path);
1496                         if (long_name == NULL)
1497                                 goto failure;
1498                         dso__set_long_name(map->dso, long_name);
1499                         dso__kernel_module_get_build_id(map->dso, "");
1500                 }
1501         }
1502
1503         return 0;
1504 failure:
1505         closedir(dir);
1506         return -1;
1507 }
1508
1509 static char *get_kernel_version(const char *root_dir)
1510 {
1511         char version[PATH_MAX];
1512         FILE *file;
1513         char *name, *tmp;
1514         const char *prefix = "Linux version ";
1515
1516         sprintf(version, "%s/proc/version", root_dir);
1517         file = fopen(version, "r");
1518         if (!file)
1519                 return NULL;
1520
1521         version[0] = '\0';
1522         tmp = fgets(version, sizeof(version), file);
1523         fclose(file);
1524
1525         name = strstr(version, prefix);
1526         if (!name)
1527                 return NULL;
1528         name += strlen(prefix);
1529         tmp = strchr(name, ' ');
1530         if (tmp)
1531                 *tmp = '\0';
1532
1533         return strdup(name);
1534 }
1535
1536 static int machine__set_modules_path(struct machine *self)
1537 {
1538         char *version;
1539         char modules_path[PATH_MAX];
1540
1541         version = get_kernel_version(self->root_dir);
1542         if (!version)
1543                 return -1;
1544
1545         snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s/kernel",
1546                  self->root_dir, version);
1547         free(version);
1548
1549         return map_groups__set_modules_path_dir(&self->kmaps, modules_path);
1550 }
1551
1552 /*
1553  * Constructor variant for modules (where we know from /proc/modules where
1554  * they are loaded) and for vmlinux, where only after we load all the
1555  * symbols we'll know where it starts and ends.
1556  */
1557 static struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
1558 {
1559         struct map *self = calloc(1, (sizeof(*self) +
1560                                       (dso->kernel ? sizeof(struct kmap) : 0)));
1561         if (self != NULL) {
1562                 /*
1563                  * ->end will be filled after we load all the symbols
1564                  */
1565                 map__init(self, type, start, 0, 0, dso);
1566         }
1567
1568         return self;
1569 }
1570
1571 struct map *machine__new_module(struct machine *self, u64 start,
1572                                 const char *filename)
1573 {
1574         struct map *map;
1575         struct dso *dso = __dsos__findnew(&self->kernel_dsos, filename);
1576
1577         if (dso == NULL)
1578                 return NULL;
1579
1580         map = map__new2(start, dso, MAP__FUNCTION);
1581         if (map == NULL)
1582                 return NULL;
1583
1584         if (machine__is_host(self))
1585                 dso->origin = DSO__ORIG_KMODULE;
1586         else
1587                 dso->origin = DSO__ORIG_GUEST_KMODULE;
1588         map_groups__insert(&self->kmaps, map);
1589         return map;
1590 }
1591
1592 static int machine__create_modules(struct machine *self)
1593 {
1594         char *line = NULL;
1595         size_t n;
1596         FILE *file;
1597         struct map *map;
1598         const char *modules;
1599         char path[PATH_MAX];
1600
1601         if (machine__is_default_guest(self))
1602                 modules = symbol_conf.default_guest_modules;
1603         else {
1604                 sprintf(path, "%s/proc/modules", self->root_dir);
1605                 modules = path;
1606         }
1607
1608         file = fopen(modules, "r");
1609         if (file == NULL)
1610                 return -1;
1611
1612         while (!feof(file)) {
1613                 char name[PATH_MAX];
1614                 u64 start;
1615                 char *sep;
1616                 int line_len;
1617
1618                 line_len = getline(&line, &n, file);
1619                 if (line_len < 0)
1620                         break;
1621
1622                 if (!line)
1623                         goto out_failure;
1624
1625                 line[--line_len] = '\0'; /* \n */
1626
1627                 sep = strrchr(line, 'x');
1628                 if (sep == NULL)
1629                         continue;
1630
1631                 hex2u64(sep + 1, &start);
1632
1633                 sep = strchr(line, ' ');
1634                 if (sep == NULL)
1635                         continue;
1636
1637                 *sep = '\0';
1638
1639                 snprintf(name, sizeof(name), "[%s]", line);
1640                 map = machine__new_module(self, start, name);
1641                 if (map == NULL)
1642                         goto out_delete_line;
1643                 dso__kernel_module_get_build_id(map->dso, self->root_dir);
1644         }
1645
1646         free(line);
1647         fclose(file);
1648
1649         return machine__set_modules_path(self);
1650
1651 out_delete_line:
1652         free(line);
1653 out_failure:
1654         return -1;
1655 }
1656
1657 static int dso__load_vmlinux(struct dso *self, struct map *map,
1658                              const char *vmlinux, symbol_filter_t filter)
1659 {
1660         int err = -1, fd;
1661
1662         if (self->has_build_id) {
1663                 u8 build_id[BUILD_ID_SIZE];
1664
1665                 if (filename__read_build_id(vmlinux, build_id,
1666                                             sizeof(build_id)) < 0) {
1667                         pr_debug("No build_id in %s, ignoring it\n", vmlinux);
1668                         return -1;
1669                 }
1670                 if (!dso__build_id_equal(self, build_id)) {
1671                         char expected_build_id[BUILD_ID_SIZE * 2 + 1],
1672                              vmlinux_build_id[BUILD_ID_SIZE * 2 + 1];
1673
1674                         build_id__sprintf(self->build_id,
1675                                           sizeof(self->build_id),
1676                                           expected_build_id);
1677                         build_id__sprintf(build_id, sizeof(build_id),
1678                                           vmlinux_build_id);
1679                         pr_debug("build_id in %s is %s while expected is %s, "
1680                                  "ignoring it\n", vmlinux, vmlinux_build_id,
1681                                  expected_build_id);
1682                         return -1;
1683                 }
1684         }
1685
1686         fd = open(vmlinux, O_RDONLY);
1687         if (fd < 0)
1688                 return -1;
1689
1690         dso__set_loaded(self, map->type);
1691         err = dso__load_sym(self, map, vmlinux, fd, filter, 0);
1692         close(fd);
1693
1694         if (err > 0)
1695                 pr_debug("Using %s for symbols\n", vmlinux);
1696
1697         return err;
1698 }
1699
1700 int dso__load_vmlinux_path(struct dso *self, struct map *map,
1701                            symbol_filter_t filter)
1702 {
1703         int i, err = 0;
1704
1705         pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1706                  vmlinux_path__nr_entries);
1707
1708         for (i = 0; i < vmlinux_path__nr_entries; ++i) {
1709                 err = dso__load_vmlinux(self, map, vmlinux_path[i], filter);
1710                 if (err > 0) {
1711                         dso__set_long_name(self, strdup(vmlinux_path[i]));
1712                         break;
1713                 }
1714         }
1715
1716         return err;
1717 }
1718
1719 static int dso__load_kernel_sym(struct dso *self, struct map *map,
1720                                 symbol_filter_t filter)
1721 {
1722         int err;
1723         const char *kallsyms_filename = NULL;
1724         char *kallsyms_allocated_filename = NULL;
1725         /*
1726          * Step 1: if the user specified a vmlinux filename, use it and only
1727          * it, reporting errors to the user if it cannot be used.
1728          *
1729          * For instance, try to analyse an ARM perf.data file _without_ a
1730          * build-id, or if the user specifies the wrong path to the right
1731          * vmlinux file, obviously we can't fallback to another vmlinux (a
1732          * x86_86 one, on the machine where analysis is being performed, say),
1733          * or worse, /proc/kallsyms.
1734          *
1735          * If the specified file _has_ a build-id and there is a build-id
1736          * section in the perf.data file, we will still do the expected
1737          * validation in dso__load_vmlinux and will bail out if they don't
1738          * match.
1739          */
1740         if (symbol_conf.vmlinux_name != NULL) {
1741                 err = dso__load_vmlinux(self, map,
1742                                         symbol_conf.vmlinux_name, filter);
1743                 goto out_try_fixup;
1744         }
1745
1746         if (vmlinux_path != NULL) {
1747                 err = dso__load_vmlinux_path(self, map, filter);
1748                 if (err > 0)
1749                         goto out_fixup;
1750         }
1751
1752         /*
1753          * Say the kernel DSO was created when processing the build-id header table,
1754          * we have a build-id, so check if it is the same as the running kernel,
1755          * using it if it is.
1756          */
1757         if (self->has_build_id) {
1758                 u8 kallsyms_build_id[BUILD_ID_SIZE];
1759                 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1760
1761                 if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id,
1762                                          sizeof(kallsyms_build_id)) == 0) {
1763                         if (dso__build_id_equal(self, kallsyms_build_id)) {
1764                                 kallsyms_filename = "/proc/kallsyms";
1765                                 goto do_kallsyms;
1766                         }
1767                 }
1768                 /*
1769                  * Now look if we have it on the build-id cache in
1770                  * $HOME/.debug/[kernel.kallsyms].
1771                  */
1772                 build_id__sprintf(self->build_id, sizeof(self->build_id),
1773                                   sbuild_id);
1774
1775                 if (asprintf(&kallsyms_allocated_filename,
1776                              "%s/.debug/[kernel.kallsyms]/%s",
1777                              getenv("HOME"), sbuild_id) == -1) {
1778                         pr_err("Not enough memory for kallsyms file lookup\n");
1779                         return -1;
1780                 }
1781
1782                 kallsyms_filename = kallsyms_allocated_filename;
1783
1784                 if (access(kallsyms_filename, F_OK)) {
1785                         pr_err("No kallsyms or vmlinux with build-id %s "
1786                                "was found\n", sbuild_id);
1787                         free(kallsyms_allocated_filename);
1788                         return -1;
1789                 }
1790         } else {
1791                 /*
1792                  * Last resort, if we don't have a build-id and couldn't find
1793                  * any vmlinux file, try the running kernel kallsyms table.
1794                  */
1795                 kallsyms_filename = "/proc/kallsyms";
1796         }
1797
1798 do_kallsyms:
1799         err = dso__load_kallsyms(self, kallsyms_filename, map, filter);
1800         if (err > 0)
1801                 pr_debug("Using %s for symbols\n", kallsyms_filename);
1802         free(kallsyms_allocated_filename);
1803
1804 out_try_fixup:
1805         if (err > 0) {
1806 out_fixup:
1807                 if (kallsyms_filename != NULL)
1808                         dso__set_long_name(self, strdup("[kernel.kallsyms]"));
1809                 map__fixup_start(map);
1810                 map__fixup_end(map);
1811         }
1812
1813         return err;
1814 }
1815
1816 static int dso__load_guest_kernel_sym(struct dso *self, struct map *map,
1817                                 symbol_filter_t filter)
1818 {
1819         int err;
1820         const char *kallsyms_filename = NULL;
1821         struct machine *machine;
1822         char path[PATH_MAX];
1823
1824         if (!map->groups) {
1825                 pr_debug("Guest kernel map hasn't the point to groups\n");
1826                 return -1;
1827         }
1828         machine = map->groups->machine;
1829
1830         if (machine__is_default_guest(machine)) {
1831                 /*
1832                  * if the user specified a vmlinux filename, use it and only
1833                  * it, reporting errors to the user if it cannot be used.
1834                  * Or use file guest_kallsyms inputted by user on commandline
1835                  */
1836                 if (symbol_conf.default_guest_vmlinux_name != NULL) {
1837                         err = dso__load_vmlinux(self, map,
1838                                 symbol_conf.default_guest_vmlinux_name, filter);
1839                         goto out_try_fixup;
1840                 }
1841
1842                 kallsyms_filename = symbol_conf.default_guest_kallsyms;
1843                 if (!kallsyms_filename)
1844                         return -1;
1845         } else {
1846                 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
1847                 kallsyms_filename = path;
1848         }
1849
1850         err = dso__load_kallsyms(self, kallsyms_filename, map, filter);
1851         if (err > 0)
1852                 pr_debug("Using %s for symbols\n", kallsyms_filename);
1853
1854 out_try_fixup:
1855         if (err > 0) {
1856                 if (kallsyms_filename != NULL) {
1857                         machine__mmap_name(machine, path, sizeof(path));
1858                         dso__set_long_name(self, strdup(path));
1859                 }
1860                 map__fixup_start(map);
1861                 map__fixup_end(map);
1862         }
1863
1864         return err;
1865 }
1866
1867 static void dsos__add(struct list_head *head, struct dso *dso)
1868 {
1869         list_add_tail(&dso->node, head);
1870 }
1871
1872 static struct dso *dsos__find(struct list_head *head, const char *name)
1873 {
1874         struct dso *pos;
1875
1876         list_for_each_entry(pos, head, node)
1877                 if (strcmp(pos->long_name, name) == 0)
1878                         return pos;
1879         return NULL;
1880 }
1881
1882 struct dso *__dsos__findnew(struct list_head *head, const char *name)
1883 {
1884         struct dso *dso = dsos__find(head, name);
1885
1886         if (!dso) {
1887                 dso = dso__new(name);
1888                 if (dso != NULL) {
1889                         dsos__add(head, dso);
1890                         dso__set_basename(dso);
1891                 }
1892         }
1893
1894         return dso;
1895 }
1896
1897 size_t __dsos__fprintf(struct list_head *head, FILE *fp)
1898 {
1899         struct dso *pos;
1900         size_t ret = 0;
1901
1902         list_for_each_entry(pos, head, node) {
1903                 int i;
1904                 for (i = 0; i < MAP__NR_TYPES; ++i)
1905                         ret += dso__fprintf(pos, i, fp);
1906         }
1907
1908         return ret;
1909 }
1910
1911 size_t machines__fprintf_dsos(struct rb_root *self, FILE *fp)
1912 {
1913         struct rb_node *nd;
1914         size_t ret = 0;
1915
1916         for (nd = rb_first(self); nd; nd = rb_next(nd)) {
1917                 struct machine *pos = rb_entry(nd, struct machine, rb_node);
1918                 ret += __dsos__fprintf(&pos->kernel_dsos, fp);
1919                 ret += __dsos__fprintf(&pos->user_dsos, fp);
1920         }
1921
1922         return ret;
1923 }
1924
1925 static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
1926                                       bool with_hits)
1927 {
1928         struct dso *pos;
1929         size_t ret = 0;
1930
1931         list_for_each_entry(pos, head, node) {
1932                 if (with_hits && !pos->hit)
1933                         continue;
1934                 ret += dso__fprintf_buildid(pos, fp);
1935                 ret += fprintf(fp, " %s\n", pos->long_name);
1936         }
1937         return ret;
1938 }
1939
1940 size_t machine__fprintf_dsos_buildid(struct machine *self, FILE *fp, bool with_hits)
1941 {
1942         return __dsos__fprintf_buildid(&self->kernel_dsos, fp, with_hits) +
1943                __dsos__fprintf_buildid(&self->user_dsos, fp, with_hits);
1944 }
1945
1946 size_t machines__fprintf_dsos_buildid(struct rb_root *self, FILE *fp, bool with_hits)
1947 {
1948         struct rb_node *nd;
1949         size_t ret = 0;
1950
1951         for (nd = rb_first(self); nd; nd = rb_next(nd)) {
1952                 struct machine *pos = rb_entry(nd, struct machine, rb_node);
1953                 ret += machine__fprintf_dsos_buildid(pos, fp, with_hits);
1954         }
1955         return ret;
1956 }
1957
1958 struct dso *dso__new_kernel(const char *name)
1959 {
1960         struct dso *self = dso__new(name ?: "[kernel.kallsyms]");
1961
1962         if (self != NULL) {
1963                 dso__set_short_name(self, "[kernel]");
1964                 self->kernel = DSO_TYPE_KERNEL;
1965         }
1966
1967         return self;
1968 }
1969
1970 static struct dso *dso__new_guest_kernel(struct machine *machine,
1971                                         const char *name)
1972 {
1973         char bf[PATH_MAX];
1974         struct dso *self = dso__new(name ?: machine__mmap_name(machine, bf, sizeof(bf)));
1975
1976         if (self != NULL) {
1977                 dso__set_short_name(self, "[guest.kernel]");
1978                 self->kernel = DSO_TYPE_GUEST_KERNEL;
1979         }
1980
1981         return self;
1982 }
1983
1984 void dso__read_running_kernel_build_id(struct dso *self, struct machine *machine)
1985 {
1986         char path[PATH_MAX];
1987
1988         if (machine__is_default_guest(machine))
1989                 return;
1990         sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
1991         if (sysfs__read_build_id(path, self->build_id,
1992                                  sizeof(self->build_id)) == 0)
1993                 self->has_build_id = true;
1994 }
1995
1996 static struct dso *machine__create_kernel(struct machine *self)
1997 {
1998         const char *vmlinux_name = NULL;
1999         struct dso *kernel;
2000
2001         if (machine__is_host(self)) {
2002                 vmlinux_name = symbol_conf.vmlinux_name;
2003                 kernel = dso__new_kernel(vmlinux_name);
2004         } else {
2005                 if (machine__is_default_guest(self))
2006                         vmlinux_name = symbol_conf.default_guest_vmlinux_name;
2007                 kernel = dso__new_guest_kernel(self, vmlinux_name);
2008         }
2009
2010         if (kernel != NULL) {
2011                 dso__read_running_kernel_build_id(kernel, self);
2012                 dsos__add(&self->kernel_dsos, kernel);
2013         }
2014         return kernel;
2015 }
2016
2017 int __machine__create_kernel_maps(struct machine *self, struct dso *kernel)
2018 {
2019         enum map_type type;
2020
2021         for (type = 0; type < MAP__NR_TYPES; ++type) {
2022                 struct kmap *kmap;
2023
2024                 self->vmlinux_maps[type] = map__new2(0, kernel, type);
2025                 if (self->vmlinux_maps[type] == NULL)
2026                         return -1;
2027
2028                 self->vmlinux_maps[type]->map_ip =
2029                         self->vmlinux_maps[type]->unmap_ip = identity__map_ip;
2030
2031                 kmap = map__kmap(self->vmlinux_maps[type]);
2032                 kmap->kmaps = &self->kmaps;
2033                 map_groups__insert(&self->kmaps, self->vmlinux_maps[type]);
2034         }
2035
2036         return 0;
2037 }
2038
2039 int machine__create_kernel_maps(struct machine *self)
2040 {
2041         struct dso *kernel = machine__create_kernel(self);
2042
2043         if (kernel == NULL ||
2044             __machine__create_kernel_maps(self, kernel) < 0)
2045                 return -1;
2046
2047         if (symbol_conf.use_modules && machine__create_modules(self) < 0)
2048                 pr_debug("Problems creating module maps, continuing anyway...\n");
2049         /*
2050          * Now that we have all the maps created, just set the ->end of them:
2051          */
2052         map_groups__fixup_end(&self->kmaps);
2053         return 0;
2054 }
2055
2056 static void vmlinux_path__exit(void)
2057 {
2058         while (--vmlinux_path__nr_entries >= 0) {
2059                 free(vmlinux_path[vmlinux_path__nr_entries]);
2060                 vmlinux_path[vmlinux_path__nr_entries] = NULL;
2061         }
2062
2063         free(vmlinux_path);
2064         vmlinux_path = NULL;
2065 }
2066
2067 static int vmlinux_path__init(void)
2068 {
2069         struct utsname uts;
2070         char bf[PATH_MAX];
2071
2072         if (uname(&uts) < 0)
2073                 return -1;
2074
2075         vmlinux_path = malloc(sizeof(char *) * 5);
2076         if (vmlinux_path == NULL)
2077                 return -1;
2078
2079         vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
2080         if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2081                 goto out_fail;
2082         ++vmlinux_path__nr_entries;
2083         vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
2084         if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2085                 goto out_fail;
2086         ++vmlinux_path__nr_entries;
2087         snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
2088         vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
2089         if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2090                 goto out_fail;
2091         ++vmlinux_path__nr_entries;
2092         snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
2093         vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
2094         if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2095                 goto out_fail;
2096         ++vmlinux_path__nr_entries;
2097         snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
2098                  uts.release);
2099         vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
2100         if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2101                 goto out_fail;
2102         ++vmlinux_path__nr_entries;
2103
2104         return 0;
2105
2106 out_fail:
2107         vmlinux_path__exit();
2108         return -1;
2109 }
2110
2111 size_t vmlinux_path__fprintf(FILE *fp)
2112 {
2113         int i;
2114         size_t printed = 0;
2115
2116         for (i = 0; i < vmlinux_path__nr_entries; ++i)
2117                 printed += fprintf(fp, "[%d] %s\n", i, vmlinux_path[i]);
2118
2119         return printed;
2120 }
2121
2122 static int setup_list(struct strlist **list, const char *list_str,
2123                       const char *list_name)
2124 {
2125         if (list_str == NULL)
2126                 return 0;
2127
2128         *list = strlist__new(true, list_str);
2129         if (!*list) {
2130                 pr_err("problems parsing %s list\n", list_name);
2131                 return -1;
2132         }
2133         return 0;
2134 }
2135
2136 int symbol__init(void)
2137 {
2138         elf_version(EV_CURRENT);
2139         if (symbol_conf.sort_by_name)
2140                 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
2141                                           sizeof(struct symbol));
2142
2143         if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
2144                 return -1;
2145
2146         if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
2147                 pr_err("'.' is the only non valid --field-separator argument\n");
2148                 return -1;
2149         }
2150
2151         if (setup_list(&symbol_conf.dso_list,
2152                        symbol_conf.dso_list_str, "dso") < 0)
2153                 return -1;
2154
2155         if (setup_list(&symbol_conf.comm_list,
2156                        symbol_conf.comm_list_str, "comm") < 0)
2157                 goto out_free_dso_list;
2158
2159         if (setup_list(&symbol_conf.sym_list,
2160                        symbol_conf.sym_list_str, "symbol") < 0)
2161                 goto out_free_comm_list;
2162
2163         return 0;
2164
2165 out_free_dso_list:
2166         strlist__delete(symbol_conf.dso_list);
2167 out_free_comm_list:
2168         strlist__delete(symbol_conf.comm_list);
2169         return -1;
2170 }
2171
2172 int machines__create_kernel_maps(struct rb_root *self, pid_t pid)
2173 {
2174         struct machine *machine = machines__findnew(self, pid);
2175
2176         if (machine == NULL)
2177                 return -1;
2178
2179         return machine__create_kernel_maps(machine);
2180 }
2181
2182 static int hex(char ch)
2183 {
2184         if ((ch >= '0') && (ch <= '9'))
2185                 return ch - '0';
2186         if ((ch >= 'a') && (ch <= 'f'))
2187                 return ch - 'a' + 10;
2188         if ((ch >= 'A') && (ch <= 'F'))
2189                 return ch - 'A' + 10;
2190         return -1;
2191 }
2192
2193 /*
2194  * While we find nice hex chars, build a long_val.
2195  * Return number of chars processed.
2196  */
2197 int hex2u64(const char *ptr, u64 *long_val)
2198 {
2199         const char *p = ptr;
2200         *long_val = 0;
2201
2202         while (*p) {
2203                 const int hex_val = hex(*p);
2204
2205                 if (hex_val < 0)
2206                         break;
2207
2208                 *long_val = (*long_val << 4) | hex_val;
2209                 p++;
2210         }
2211
2212         return p - ptr;
2213 }
2214
2215 char *strxfrchar(char *s, char from, char to)
2216 {
2217         char *p = s;
2218
2219         while ((p = strchr(p, from)) != NULL)
2220                 *p++ = to;
2221
2222         return s;
2223 }
2224
2225 int machines__create_guest_kernel_maps(struct rb_root *self)
2226 {
2227         int ret = 0;
2228         struct dirent **namelist = NULL;
2229         int i, items = 0;
2230         char path[PATH_MAX];
2231         pid_t pid;
2232
2233         if (symbol_conf.default_guest_vmlinux_name ||
2234             symbol_conf.default_guest_modules ||
2235             symbol_conf.default_guest_kallsyms) {
2236                 machines__create_kernel_maps(self, DEFAULT_GUEST_KERNEL_ID);
2237         }
2238
2239         if (symbol_conf.guestmount) {
2240                 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
2241                 if (items <= 0)
2242                         return -ENOENT;
2243                 for (i = 0; i < items; i++) {
2244                         if (!isdigit(namelist[i]->d_name[0])) {
2245                                 /* Filter out . and .. */
2246                                 continue;
2247                         }
2248                         pid = atoi(namelist[i]->d_name);
2249                         sprintf(path, "%s/%s/proc/kallsyms",
2250                                 symbol_conf.guestmount,
2251                                 namelist[i]->d_name);
2252                         ret = access(path, R_OK);
2253                         if (ret) {
2254                                 pr_debug("Can't access file %s\n", path);
2255                                 goto failure;
2256                         }
2257                         machines__create_kernel_maps(self, pid);
2258                 }
2259 failure:
2260                 free(namelist);
2261         }
2262
2263         return ret;
2264 }
2265
2266 int machine__load_kallsyms(struct machine *self, const char *filename,
2267                            enum map_type type, symbol_filter_t filter)
2268 {
2269         struct map *map = self->vmlinux_maps[type];
2270         int ret = dso__load_kallsyms(map->dso, filename, map, filter);
2271
2272         if (ret > 0) {
2273                 dso__set_loaded(map->dso, type);
2274                 /*
2275                  * Since /proc/kallsyms will have multiple sessions for the
2276                  * kernel, with modules between them, fixup the end of all
2277                  * sections.
2278                  */
2279                 __map_groups__fixup_end(&self->kmaps, type);
2280         }
2281
2282         return ret;
2283 }
2284
2285 int machine__load_vmlinux_path(struct machine *self, enum map_type type,
2286                                symbol_filter_t filter)
2287 {
2288         struct map *map = self->vmlinux_maps[type];
2289         int ret = dso__load_vmlinux_path(map->dso, map, filter);
2290
2291         if (ret > 0) {
2292                 dso__set_loaded(map->dso, type);
2293                 map__reloc_vmlinux(map);
2294         }
2295
2296         return ret;
2297 }