perf record: Introduce a symtab cache
[linux-2.6.git] / tools / perf / util / symbol.c
1 #include "util.h"
2 #include "../perf.h"
3 #include "session.h"
4 #include "sort.h"
5 #include "string.h"
6 #include "symbol.h"
7 #include "thread.h"
8
9 #include "debug.h"
10
11 #include <asm/bug.h>
12 #include <libelf.h>
13 #include <gelf.h>
14 #include <elf.h>
15 #include <limits.h>
16 #include <sys/utsname.h>
17
18 #ifndef NT_GNU_BUILD_ID
19 #define NT_GNU_BUILD_ID 3
20 #endif
21
22 enum dso_origin {
23         DSO__ORIG_KERNEL = 0,
24         DSO__ORIG_JAVA_JIT,
25         DSO__ORIG_BUILD_ID_CACHE,
26         DSO__ORIG_FEDORA,
27         DSO__ORIG_UBUNTU,
28         DSO__ORIG_BUILDID,
29         DSO__ORIG_DSO,
30         DSO__ORIG_KMODULE,
31         DSO__ORIG_NOT_FOUND,
32 };
33
34 static void dsos__add(struct list_head *head, struct dso *dso);
35 static struct map *map__new2(u64 start, struct dso *dso, enum map_type type);
36 static int dso__load_kernel_sym(struct dso *self, struct map *map,
37                                 struct perf_session *session, symbol_filter_t filter);
38 static int vmlinux_path__nr_entries;
39 static char **vmlinux_path;
40
41 struct symbol_conf symbol_conf = {
42         .exclude_other    = true,
43         .use_modules      = true,
44         .try_vmlinux_path = true,
45 };
46
47 bool dso__loaded(const struct dso *self, enum map_type type)
48 {
49         return self->loaded & (1 << type);
50 }
51
52 bool dso__sorted_by_name(const struct dso *self, enum map_type type)
53 {
54         return self->sorted_by_name & (1 << type);
55 }
56
57 static void dso__set_loaded(struct dso *self, enum map_type type)
58 {
59         self->loaded |= (1 << type);
60 }
61
62 static void dso__set_sorted_by_name(struct dso *self, enum map_type type)
63 {
64         self->sorted_by_name |= (1 << type);
65 }
66
67 static bool symbol_type__is_a(char symbol_type, enum map_type map_type)
68 {
69         switch (map_type) {
70         case MAP__FUNCTION:
71                 return symbol_type == 'T' || symbol_type == 'W';
72         case MAP__VARIABLE:
73                 return symbol_type == 'D' || symbol_type == 'd';
74         default:
75                 return false;
76         }
77 }
78
79 static void symbols__fixup_end(struct rb_root *self)
80 {
81         struct rb_node *nd, *prevnd = rb_first(self);
82         struct symbol *curr, *prev;
83
84         if (prevnd == NULL)
85                 return;
86
87         curr = rb_entry(prevnd, struct symbol, rb_node);
88
89         for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
90                 prev = curr;
91                 curr = rb_entry(nd, struct symbol, rb_node);
92
93                 if (prev->end == prev->start)
94                         prev->end = curr->start - 1;
95         }
96
97         /* Last entry */
98         if (curr->end == curr->start)
99                 curr->end = roundup(curr->start, 4096);
100 }
101
102 static void __map_groups__fixup_end(struct map_groups *self, enum map_type type)
103 {
104         struct map *prev, *curr;
105         struct rb_node *nd, *prevnd = rb_first(&self->maps[type]);
106
107         if (prevnd == NULL)
108                 return;
109
110         curr = rb_entry(prevnd, struct map, rb_node);
111
112         for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
113                 prev = curr;
114                 curr = rb_entry(nd, struct map, rb_node);
115                 prev->end = curr->start - 1;
116         }
117
118         /*
119          * We still haven't the actual symbols, so guess the
120          * last map final address.
121          */
122         curr->end = ~0UL;
123 }
124
125 static void map_groups__fixup_end(struct map_groups *self)
126 {
127         int i;
128         for (i = 0; i < MAP__NR_TYPES; ++i)
129                 __map_groups__fixup_end(self, i);
130 }
131
132 static struct symbol *symbol__new(u64 start, u64 len, const char *name)
133 {
134         size_t namelen = strlen(name) + 1;
135         struct symbol *self = zalloc(symbol_conf.priv_size +
136                                      sizeof(*self) + namelen);
137         if (self == NULL)
138                 return NULL;
139
140         if (symbol_conf.priv_size)
141                 self = ((void *)self) + symbol_conf.priv_size;
142
143         self->start = start;
144         self->end   = len ? start + len - 1 : start;
145
146         pr_debug3("%s: %s %#Lx-%#Lx\n", __func__, name, start, self->end);
147
148         memcpy(self->name, name, namelen);
149
150         return self;
151 }
152
153 static void symbol__delete(struct symbol *self)
154 {
155         free(((void *)self) - symbol_conf.priv_size);
156 }
157
158 static size_t symbol__fprintf(struct symbol *self, FILE *fp)
159 {
160         return fprintf(fp, " %llx-%llx %s\n",
161                        self->start, self->end, self->name);
162 }
163
164 static void dso__set_long_name(struct dso *self, char *name)
165 {
166         if (name == NULL)
167                 return;
168         self->long_name = name;
169         self->long_name_len = strlen(name);
170 }
171
172 static void dso__set_basename(struct dso *self)
173 {
174         self->short_name = basename(self->long_name);
175 }
176
177 struct dso *dso__new(const char *name)
178 {
179         struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
180
181         if (self != NULL) {
182                 int i;
183                 strcpy(self->name, name);
184                 dso__set_long_name(self, self->name);
185                 self->short_name = self->name;
186                 for (i = 0; i < MAP__NR_TYPES; ++i)
187                         self->symbols[i] = self->symbol_names[i] = RB_ROOT;
188                 self->slen_calculated = 0;
189                 self->origin = DSO__ORIG_NOT_FOUND;
190                 self->loaded = 0;
191                 self->sorted_by_name = 0;
192                 self->has_build_id = 0;
193         }
194
195         return self;
196 }
197
198 static void symbols__delete(struct rb_root *self)
199 {
200         struct symbol *pos;
201         struct rb_node *next = rb_first(self);
202
203         while (next) {
204                 pos = rb_entry(next, struct symbol, rb_node);
205                 next = rb_next(&pos->rb_node);
206                 rb_erase(&pos->rb_node, self);
207                 symbol__delete(pos);
208         }
209 }
210
211 void dso__delete(struct dso *self)
212 {
213         int i;
214         for (i = 0; i < MAP__NR_TYPES; ++i)
215                 symbols__delete(&self->symbols[i]);
216         if (self->long_name != self->name)
217                 free(self->long_name);
218         free(self);
219 }
220
221 void dso__set_build_id(struct dso *self, void *build_id)
222 {
223         memcpy(self->build_id, build_id, sizeof(self->build_id));
224         self->has_build_id = 1;
225 }
226
227 static void symbols__insert(struct rb_root *self, struct symbol *sym)
228 {
229         struct rb_node **p = &self->rb_node;
230         struct rb_node *parent = NULL;
231         const u64 ip = sym->start;
232         struct symbol *s;
233
234         while (*p != NULL) {
235                 parent = *p;
236                 s = rb_entry(parent, struct symbol, rb_node);
237                 if (ip < s->start)
238                         p = &(*p)->rb_left;
239                 else
240                         p = &(*p)->rb_right;
241         }
242         rb_link_node(&sym->rb_node, parent, p);
243         rb_insert_color(&sym->rb_node, self);
244 }
245
246 static struct symbol *symbols__find(struct rb_root *self, u64 ip)
247 {
248         struct rb_node *n;
249
250         if (self == NULL)
251                 return NULL;
252
253         n = self->rb_node;
254
255         while (n) {
256                 struct symbol *s = rb_entry(n, struct symbol, rb_node);
257
258                 if (ip < s->start)
259                         n = n->rb_left;
260                 else if (ip > s->end)
261                         n = n->rb_right;
262                 else
263                         return s;
264         }
265
266         return NULL;
267 }
268
269 struct symbol_name_rb_node {
270         struct rb_node  rb_node;
271         struct symbol   sym;
272 };
273
274 static void symbols__insert_by_name(struct rb_root *self, struct symbol *sym)
275 {
276         struct rb_node **p = &self->rb_node;
277         struct rb_node *parent = NULL;
278         struct symbol_name_rb_node *symn = ((void *)sym) - sizeof(*parent), *s;
279
280         while (*p != NULL) {
281                 parent = *p;
282                 s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
283                 if (strcmp(sym->name, s->sym.name) < 0)
284                         p = &(*p)->rb_left;
285                 else
286                         p = &(*p)->rb_right;
287         }
288         rb_link_node(&symn->rb_node, parent, p);
289         rb_insert_color(&symn->rb_node, self);
290 }
291
292 static void symbols__sort_by_name(struct rb_root *self, struct rb_root *source)
293 {
294         struct rb_node *nd;
295
296         for (nd = rb_first(source); nd; nd = rb_next(nd)) {
297                 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
298                 symbols__insert_by_name(self, pos);
299         }
300 }
301
302 static struct symbol *symbols__find_by_name(struct rb_root *self, const char *name)
303 {
304         struct rb_node *n;
305
306         if (self == NULL)
307                 return NULL;
308
309         n = self->rb_node;
310
311         while (n) {
312                 struct symbol_name_rb_node *s;
313                 int cmp;
314
315                 s = rb_entry(n, struct symbol_name_rb_node, rb_node);
316                 cmp = strcmp(name, s->sym.name);
317
318                 if (cmp < 0)
319                         n = n->rb_left;
320                 else if (cmp > 0)
321                         n = n->rb_right;
322                 else
323                         return &s->sym;
324         }
325
326         return NULL;
327 }
328
329 struct symbol *dso__find_symbol(struct dso *self,
330                                 enum map_type type, u64 addr)
331 {
332         return symbols__find(&self->symbols[type], addr);
333 }
334
335 struct symbol *dso__find_symbol_by_name(struct dso *self, enum map_type type,
336                                         const char *name)
337 {
338         return symbols__find_by_name(&self->symbol_names[type], name);
339 }
340
341 void dso__sort_by_name(struct dso *self, enum map_type type)
342 {
343         dso__set_sorted_by_name(self, type);
344         return symbols__sort_by_name(&self->symbol_names[type],
345                                      &self->symbols[type]);
346 }
347
348 int build_id__sprintf(u8 *self, int len, char *bf)
349 {
350         char *bid = bf;
351         u8 *raw = self;
352         int i;
353
354         for (i = 0; i < len; ++i) {
355                 sprintf(bid, "%02x", *raw);
356                 ++raw;
357                 bid += 2;
358         }
359
360         return raw - self;
361 }
362
363 size_t dso__fprintf_buildid(struct dso *self, FILE *fp)
364 {
365         char sbuild_id[BUILD_ID_SIZE * 2 + 1];
366
367         build_id__sprintf(self->build_id, sizeof(self->build_id), sbuild_id);
368         return fprintf(fp, "%s", sbuild_id);
369 }
370
371 size_t dso__fprintf(struct dso *self, enum map_type type, FILE *fp)
372 {
373         struct rb_node *nd;
374         size_t ret = fprintf(fp, "dso: %s (", self->short_name);
375
376         ret += dso__fprintf_buildid(self, fp);
377         ret += fprintf(fp, ")\n");
378         for (nd = rb_first(&self->symbols[type]); nd; nd = rb_next(nd)) {
379                 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
380                 ret += symbol__fprintf(pos, fp);
381         }
382
383         return ret;
384 }
385
386 /*
387  * Loads the function entries in /proc/kallsyms into kernel_map->dso,
388  * so that we can in the next step set the symbol ->end address and then
389  * call kernel_maps__split_kallsyms.
390  */
391 static int dso__load_all_kallsyms(struct dso *self, struct map *map)
392 {
393         char *line = NULL;
394         size_t n;
395         struct rb_root *root = &self->symbols[map->type];
396         FILE *file = fopen("/proc/kallsyms", "r");
397
398         if (file == NULL)
399                 goto out_failure;
400
401         while (!feof(file)) {
402                 u64 start;
403                 struct symbol *sym;
404                 int line_len, len;
405                 char symbol_type;
406                 char *symbol_name;
407
408                 line_len = getline(&line, &n, file);
409                 if (line_len < 0)
410                         break;
411
412                 if (!line)
413                         goto out_failure;
414
415                 line[--line_len] = '\0'; /* \n */
416
417                 len = hex2u64(line, &start);
418
419                 len++;
420                 if (len + 2 >= line_len)
421                         continue;
422
423                 symbol_type = toupper(line[len]);
424                 if (!symbol_type__is_a(symbol_type, map->type))
425                         continue;
426
427                 symbol_name = line + len + 2;
428                 /*
429                  * Will fix up the end later, when we have all symbols sorted.
430                  */
431                 sym = symbol__new(start, 0, symbol_name);
432
433                 if (sym == NULL)
434                         goto out_delete_line;
435                 /*
436                  * We will pass the symbols to the filter later, in
437                  * map__split_kallsyms, when we have split the maps per module
438                  */
439                 symbols__insert(root, sym);
440         }
441
442         free(line);
443         fclose(file);
444
445         return 0;
446
447 out_delete_line:
448         free(line);
449 out_failure:
450         return -1;
451 }
452
453 /*
454  * Split the symbols into maps, making sure there are no overlaps, i.e. the
455  * kernel range is broken in several maps, named [kernel].N, as we don't have
456  * the original ELF section names vmlinux have.
457  */
458 static int dso__split_kallsyms(struct dso *self, struct map *map,
459                                struct perf_session *session, symbol_filter_t filter)
460 {
461         struct map *curr_map = map;
462         struct symbol *pos;
463         int count = 0;
464         struct rb_root *root = &self->symbols[map->type];
465         struct rb_node *next = rb_first(root);
466         int kernel_range = 0;
467
468         while (next) {
469                 char *module;
470
471                 pos = rb_entry(next, struct symbol, rb_node);
472                 next = rb_next(&pos->rb_node);
473
474                 module = strchr(pos->name, '\t');
475                 if (module) {
476                         if (!symbol_conf.use_modules)
477                                 goto discard_symbol;
478
479                         *module++ = '\0';
480
481                         if (strcmp(self->name, module)) {
482                                 curr_map = map_groups__find_by_name(&session->kmaps, map->type, module);
483                                 if (curr_map == NULL) {
484                                         pr_debug("/proc/{kallsyms,modules} "
485                                                  "inconsistency!\n");
486                                         return -1;
487                                 }
488                         }
489                         /*
490                          * So that we look just like we get from .ko files,
491                          * i.e. not prelinked, relative to map->start.
492                          */
493                         pos->start = curr_map->map_ip(curr_map, pos->start);
494                         pos->end   = curr_map->map_ip(curr_map, pos->end);
495                 } else if (curr_map != map) {
496                         char dso_name[PATH_MAX];
497                         struct dso *dso;
498
499                         snprintf(dso_name, sizeof(dso_name), "[kernel].%d",
500                                  kernel_range++);
501
502                         dso = dso__new(dso_name);
503                         if (dso == NULL)
504                                 return -1;
505
506                         curr_map = map__new2(pos->start, dso, map->type);
507                         if (map == NULL) {
508                                 dso__delete(dso);
509                                 return -1;
510                         }
511
512                         curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
513                         map_groups__insert(&session->kmaps, curr_map);
514                         ++kernel_range;
515                 }
516
517                 if (filter && filter(curr_map, pos)) {
518 discard_symbol:         rb_erase(&pos->rb_node, root);
519                         symbol__delete(pos);
520                 } else {
521                         if (curr_map != map) {
522                                 rb_erase(&pos->rb_node, root);
523                                 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
524                         }
525                         count++;
526                 }
527         }
528
529         return count;
530 }
531
532
533 static int dso__load_kallsyms(struct dso *self, struct map *map,
534                               struct perf_session *session, symbol_filter_t filter)
535 {
536         if (dso__load_all_kallsyms(self, map) < 0)
537                 return -1;
538
539         symbols__fixup_end(&self->symbols[map->type]);
540         self->origin = DSO__ORIG_KERNEL;
541
542         return dso__split_kallsyms(self, map, session, filter);
543 }
544
545 static int dso__load_perf_map(struct dso *self, struct map *map,
546                               symbol_filter_t filter)
547 {
548         char *line = NULL;
549         size_t n;
550         FILE *file;
551         int nr_syms = 0;
552
553         file = fopen(self->long_name, "r");
554         if (file == NULL)
555                 goto out_failure;
556
557         while (!feof(file)) {
558                 u64 start, size;
559                 struct symbol *sym;
560                 int line_len, len;
561
562                 line_len = getline(&line, &n, file);
563                 if (line_len < 0)
564                         break;
565
566                 if (!line)
567                         goto out_failure;
568
569                 line[--line_len] = '\0'; /* \n */
570
571                 len = hex2u64(line, &start);
572
573                 len++;
574                 if (len + 2 >= line_len)
575                         continue;
576
577                 len += hex2u64(line + len, &size);
578
579                 len++;
580                 if (len + 2 >= line_len)
581                         continue;
582
583                 sym = symbol__new(start, size, line + len);
584
585                 if (sym == NULL)
586                         goto out_delete_line;
587
588                 if (filter && filter(map, sym))
589                         symbol__delete(sym);
590                 else {
591                         symbols__insert(&self->symbols[map->type], sym);
592                         nr_syms++;
593                 }
594         }
595
596         free(line);
597         fclose(file);
598
599         return nr_syms;
600
601 out_delete_line:
602         free(line);
603 out_failure:
604         return -1;
605 }
606
607 /**
608  * elf_symtab__for_each_symbol - iterate thru all the symbols
609  *
610  * @self: struct elf_symtab instance to iterate
611  * @idx: uint32_t idx
612  * @sym: GElf_Sym iterator
613  */
614 #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
615         for (idx = 0, gelf_getsym(syms, idx, &sym);\
616              idx < nr_syms; \
617              idx++, gelf_getsym(syms, idx, &sym))
618
619 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
620 {
621         return GELF_ST_TYPE(sym->st_info);
622 }
623
624 static inline int elf_sym__is_function(const GElf_Sym *sym)
625 {
626         return elf_sym__type(sym) == STT_FUNC &&
627                sym->st_name != 0 &&
628                sym->st_shndx != SHN_UNDEF;
629 }
630
631 static inline bool elf_sym__is_object(const GElf_Sym *sym)
632 {
633         return elf_sym__type(sym) == STT_OBJECT &&
634                 sym->st_name != 0 &&
635                 sym->st_shndx != SHN_UNDEF;
636 }
637
638 static inline int elf_sym__is_label(const GElf_Sym *sym)
639 {
640         return elf_sym__type(sym) == STT_NOTYPE &&
641                 sym->st_name != 0 &&
642                 sym->st_shndx != SHN_UNDEF &&
643                 sym->st_shndx != SHN_ABS;
644 }
645
646 static inline const char *elf_sec__name(const GElf_Shdr *shdr,
647                                         const Elf_Data *secstrs)
648 {
649         return secstrs->d_buf + shdr->sh_name;
650 }
651
652 static inline int elf_sec__is_text(const GElf_Shdr *shdr,
653                                         const Elf_Data *secstrs)
654 {
655         return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
656 }
657
658 static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
659                                     const Elf_Data *secstrs)
660 {
661         return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
662 }
663
664 static inline const char *elf_sym__name(const GElf_Sym *sym,
665                                         const Elf_Data *symstrs)
666 {
667         return symstrs->d_buf + sym->st_name;
668 }
669
670 static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
671                                     GElf_Shdr *shp, const char *name,
672                                     size_t *idx)
673 {
674         Elf_Scn *sec = NULL;
675         size_t cnt = 1;
676
677         while ((sec = elf_nextscn(elf, sec)) != NULL) {
678                 char *str;
679
680                 gelf_getshdr(sec, shp);
681                 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
682                 if (!strcmp(name, str)) {
683                         if (idx)
684                                 *idx = cnt;
685                         break;
686                 }
687                 ++cnt;
688         }
689
690         return sec;
691 }
692
693 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
694         for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
695              idx < nr_entries; \
696              ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
697
698 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
699         for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
700              idx < nr_entries; \
701              ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
702
703 /*
704  * We need to check if we have a .dynsym, so that we can handle the
705  * .plt, synthesizing its symbols, that aren't on the symtabs (be it
706  * .dynsym or .symtab).
707  * And always look at the original dso, not at debuginfo packages, that
708  * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
709  */
710 static int dso__synthesize_plt_symbols(struct  dso *self, struct map *map,
711                                        symbol_filter_t filter)
712 {
713         uint32_t nr_rel_entries, idx;
714         GElf_Sym sym;
715         u64 plt_offset;
716         GElf_Shdr shdr_plt;
717         struct symbol *f;
718         GElf_Shdr shdr_rel_plt, shdr_dynsym;
719         Elf_Data *reldata, *syms, *symstrs;
720         Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
721         size_t dynsym_idx;
722         GElf_Ehdr ehdr;
723         char sympltname[1024];
724         Elf *elf;
725         int nr = 0, symidx, fd, err = 0;
726
727         fd = open(self->long_name, O_RDONLY);
728         if (fd < 0)
729                 goto out;
730
731         elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
732         if (elf == NULL)
733                 goto out_close;
734
735         if (gelf_getehdr(elf, &ehdr) == NULL)
736                 goto out_elf_end;
737
738         scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
739                                          ".dynsym", &dynsym_idx);
740         if (scn_dynsym == NULL)
741                 goto out_elf_end;
742
743         scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
744                                           ".rela.plt", NULL);
745         if (scn_plt_rel == NULL) {
746                 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
747                                                   ".rel.plt", NULL);
748                 if (scn_plt_rel == NULL)
749                         goto out_elf_end;
750         }
751
752         err = -1;
753
754         if (shdr_rel_plt.sh_link != dynsym_idx)
755                 goto out_elf_end;
756
757         if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
758                 goto out_elf_end;
759
760         /*
761          * Fetch the relocation section to find the idxes to the GOT
762          * and the symbols in the .dynsym they refer to.
763          */
764         reldata = elf_getdata(scn_plt_rel, NULL);
765         if (reldata == NULL)
766                 goto out_elf_end;
767
768         syms = elf_getdata(scn_dynsym, NULL);
769         if (syms == NULL)
770                 goto out_elf_end;
771
772         scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
773         if (scn_symstrs == NULL)
774                 goto out_elf_end;
775
776         symstrs = elf_getdata(scn_symstrs, NULL);
777         if (symstrs == NULL)
778                 goto out_elf_end;
779
780         nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
781         plt_offset = shdr_plt.sh_offset;
782
783         if (shdr_rel_plt.sh_type == SHT_RELA) {
784                 GElf_Rela pos_mem, *pos;
785
786                 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
787                                            nr_rel_entries) {
788                         symidx = GELF_R_SYM(pos->r_info);
789                         plt_offset += shdr_plt.sh_entsize;
790                         gelf_getsym(syms, symidx, &sym);
791                         snprintf(sympltname, sizeof(sympltname),
792                                  "%s@plt", elf_sym__name(&sym, symstrs));
793
794                         f = symbol__new(plt_offset, shdr_plt.sh_entsize,
795                                         sympltname);
796                         if (!f)
797                                 goto out_elf_end;
798
799                         if (filter && filter(map, f))
800                                 symbol__delete(f);
801                         else {
802                                 symbols__insert(&self->symbols[map->type], f);
803                                 ++nr;
804                         }
805                 }
806         } else if (shdr_rel_plt.sh_type == SHT_REL) {
807                 GElf_Rel pos_mem, *pos;
808                 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
809                                           nr_rel_entries) {
810                         symidx = GELF_R_SYM(pos->r_info);
811                         plt_offset += shdr_plt.sh_entsize;
812                         gelf_getsym(syms, symidx, &sym);
813                         snprintf(sympltname, sizeof(sympltname),
814                                  "%s@plt", elf_sym__name(&sym, symstrs));
815
816                         f = symbol__new(plt_offset, shdr_plt.sh_entsize,
817                                         sympltname);
818                         if (!f)
819                                 goto out_elf_end;
820
821                         if (filter && filter(map, f))
822                                 symbol__delete(f);
823                         else {
824                                 symbols__insert(&self->symbols[map->type], f);
825                                 ++nr;
826                         }
827                 }
828         }
829
830         err = 0;
831 out_elf_end:
832         elf_end(elf);
833 out_close:
834         close(fd);
835
836         if (err == 0)
837                 return nr;
838 out:
839         pr_warning("%s: problems reading %s PLT info.\n",
840                    __func__, self->long_name);
841         return 0;
842 }
843
844 static bool elf_sym__is_a(GElf_Sym *self, enum map_type type)
845 {
846         switch (type) {
847         case MAP__FUNCTION:
848                 return elf_sym__is_function(self);
849         case MAP__VARIABLE:
850                 return elf_sym__is_object(self);
851         default:
852                 return false;
853         }
854 }
855
856 static bool elf_sec__is_a(GElf_Shdr *self, Elf_Data *secstrs, enum map_type type)
857 {
858         switch (type) {
859         case MAP__FUNCTION:
860                 return elf_sec__is_text(self, secstrs);
861         case MAP__VARIABLE:
862                 return elf_sec__is_data(self, secstrs);
863         default:
864                 return false;
865         }
866 }
867
868 static int dso__load_sym(struct dso *self, struct map *map,
869                          struct perf_session *session, const char *name, int fd,
870                          symbol_filter_t filter, int kernel, int kmodule)
871 {
872         struct map *curr_map = map;
873         struct dso *curr_dso = self;
874         size_t dso_name_len = strlen(self->short_name);
875         Elf_Data *symstrs, *secstrs;
876         uint32_t nr_syms;
877         int err = -1;
878         uint32_t idx;
879         GElf_Ehdr ehdr;
880         GElf_Shdr shdr;
881         Elf_Data *syms;
882         GElf_Sym sym;
883         Elf_Scn *sec, *sec_strndx;
884         Elf *elf;
885         int nr = 0;
886
887         elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
888         if (elf == NULL) {
889                 pr_err("%s: cannot read %s ELF file.\n", __func__, name);
890                 goto out_close;
891         }
892
893         if (gelf_getehdr(elf, &ehdr) == NULL) {
894                 pr_err("%s: cannot get elf header.\n", __func__);
895                 goto out_elf_end;
896         }
897
898         sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
899         if (sec == NULL) {
900                 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
901                 if (sec == NULL)
902                         goto out_elf_end;
903         }
904
905         syms = elf_getdata(sec, NULL);
906         if (syms == NULL)
907                 goto out_elf_end;
908
909         sec = elf_getscn(elf, shdr.sh_link);
910         if (sec == NULL)
911                 goto out_elf_end;
912
913         symstrs = elf_getdata(sec, NULL);
914         if (symstrs == NULL)
915                 goto out_elf_end;
916
917         sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
918         if (sec_strndx == NULL)
919                 goto out_elf_end;
920
921         secstrs = elf_getdata(sec_strndx, NULL);
922         if (secstrs == NULL)
923                 goto out_elf_end;
924
925         nr_syms = shdr.sh_size / shdr.sh_entsize;
926
927         memset(&sym, 0, sizeof(sym));
928         if (!kernel) {
929                 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
930                                 elf_section_by_name(elf, &ehdr, &shdr,
931                                                      ".gnu.prelink_undo",
932                                                      NULL) != NULL);
933         } else self->adjust_symbols = 0;
934
935         elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
936                 struct symbol *f;
937                 const char *elf_name;
938                 char *demangled = NULL;
939                 int is_label = elf_sym__is_label(&sym);
940                 const char *section_name;
941
942                 if (!is_label && !elf_sym__is_a(&sym, map->type))
943                         continue;
944
945                 sec = elf_getscn(elf, sym.st_shndx);
946                 if (!sec)
947                         goto out_elf_end;
948
949                 gelf_getshdr(sec, &shdr);
950
951                 if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type))
952                         continue;
953
954                 elf_name = elf_sym__name(&sym, symstrs);
955                 section_name = elf_sec__name(&shdr, secstrs);
956
957                 if (kernel || kmodule) {
958                         char dso_name[PATH_MAX];
959
960                         if (strcmp(section_name,
961                                    curr_dso->short_name + dso_name_len) == 0)
962                                 goto new_symbol;
963
964                         if (strcmp(section_name, ".text") == 0) {
965                                 curr_map = map;
966                                 curr_dso = self;
967                                 goto new_symbol;
968                         }
969
970                         snprintf(dso_name, sizeof(dso_name),
971                                  "%s%s", self->short_name, section_name);
972
973                         curr_map = map_groups__find_by_name(&session->kmaps, map->type, dso_name);
974                         if (curr_map == NULL) {
975                                 u64 start = sym.st_value;
976
977                                 if (kmodule)
978                                         start += map->start + shdr.sh_offset;
979
980                                 curr_dso = dso__new(dso_name);
981                                 if (curr_dso == NULL)
982                                         goto out_elf_end;
983                                 curr_map = map__new2(start, curr_dso,
984                                                      MAP__FUNCTION);
985                                 if (curr_map == NULL) {
986                                         dso__delete(curr_dso);
987                                         goto out_elf_end;
988                                 }
989                                 curr_map->map_ip = identity__map_ip;
990                                 curr_map->unmap_ip = identity__map_ip;
991                                 curr_dso->origin = DSO__ORIG_KERNEL;
992                                 map_groups__insert(&session->kmaps, curr_map);
993                                 dsos__add(&dsos__kernel, curr_dso);
994                         } else
995                                 curr_dso = curr_map->dso;
996
997                         goto new_symbol;
998                 }
999
1000                 if (curr_dso->adjust_symbols) {
1001                         pr_debug2("adjusting symbol: st_value: %Lx sh_addr: "
1002                                   "%Lx sh_offset: %Lx\n", (u64)sym.st_value,
1003                                   (u64)shdr.sh_addr, (u64)shdr.sh_offset);
1004                         sym.st_value -= shdr.sh_addr - shdr.sh_offset;
1005                 }
1006                 /*
1007                  * We need to figure out if the object was created from C++ sources
1008                  * DWARF DW_compile_unit has this, but we don't always have access
1009                  * to it...
1010                  */
1011                 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
1012                 if (demangled != NULL)
1013                         elf_name = demangled;
1014 new_symbol:
1015                 f = symbol__new(sym.st_value, sym.st_size, elf_name);
1016                 free(demangled);
1017                 if (!f)
1018                         goto out_elf_end;
1019
1020                 if (filter && filter(curr_map, f))
1021                         symbol__delete(f);
1022                 else {
1023                         symbols__insert(&curr_dso->symbols[curr_map->type], f);
1024                         nr++;
1025                 }
1026         }
1027
1028         /*
1029          * For misannotated, zeroed, ASM function sizes.
1030          */
1031         if (nr > 0)
1032                 symbols__fixup_end(&self->symbols[map->type]);
1033         err = nr;
1034 out_elf_end:
1035         elf_end(elf);
1036 out_close:
1037         return err;
1038 }
1039
1040 static bool dso__build_id_equal(const struct dso *self, u8 *build_id)
1041 {
1042         return memcmp(self->build_id, build_id, sizeof(self->build_id)) == 0;
1043 }
1044
1045 static bool __dsos__read_build_ids(struct list_head *head)
1046 {
1047         bool have_build_id = false;
1048         struct dso *pos;
1049
1050         list_for_each_entry(pos, head, node)
1051                 if (filename__read_build_id(pos->long_name, pos->build_id,
1052                                             sizeof(pos->build_id)) > 0) {
1053                         have_build_id     = true;
1054                         pos->has_build_id = true;
1055                 }
1056
1057         return have_build_id;
1058 }
1059
1060 bool dsos__read_build_ids(void)
1061 {
1062         bool kbuildids = __dsos__read_build_ids(&dsos__kernel),
1063              ubuildids = __dsos__read_build_ids(&dsos__user);
1064         return kbuildids || ubuildids;
1065 }
1066
1067 /*
1068  * Align offset to 4 bytes as needed for note name and descriptor data.
1069  */
1070 #define NOTE_ALIGN(n) (((n) + 3) & -4U)
1071
1072 int filename__read_build_id(const char *filename, void *bf, size_t size)
1073 {
1074         int fd, err = -1;
1075         GElf_Ehdr ehdr;
1076         GElf_Shdr shdr;
1077         Elf_Data *data;
1078         Elf_Scn *sec;
1079         Elf_Kind ek;
1080         void *ptr;
1081         Elf *elf;
1082
1083         if (size < BUILD_ID_SIZE)
1084                 goto out;
1085
1086         fd = open(filename, O_RDONLY);
1087         if (fd < 0)
1088                 goto out;
1089
1090         elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1091         if (elf == NULL) {
1092                 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
1093                 goto out_close;
1094         }
1095
1096         ek = elf_kind(elf);
1097         if (ek != ELF_K_ELF)
1098                 goto out_elf_end;
1099
1100         if (gelf_getehdr(elf, &ehdr) == NULL) {
1101                 pr_err("%s: cannot get elf header.\n", __func__);
1102                 goto out_elf_end;
1103         }
1104
1105         sec = elf_section_by_name(elf, &ehdr, &shdr,
1106                                   ".note.gnu.build-id", NULL);
1107         if (sec == NULL) {
1108                 sec = elf_section_by_name(elf, &ehdr, &shdr,
1109                                           ".notes", NULL);
1110                 if (sec == NULL)
1111                         goto out_elf_end;
1112         }
1113
1114         data = elf_getdata(sec, NULL);
1115         if (data == NULL)
1116                 goto out_elf_end;
1117
1118         ptr = data->d_buf;
1119         while (ptr < (data->d_buf + data->d_size)) {
1120                 GElf_Nhdr *nhdr = ptr;
1121                 int namesz = NOTE_ALIGN(nhdr->n_namesz),
1122                     descsz = NOTE_ALIGN(nhdr->n_descsz);
1123                 const char *name;
1124
1125                 ptr += sizeof(*nhdr);
1126                 name = ptr;
1127                 ptr += namesz;
1128                 if (nhdr->n_type == NT_GNU_BUILD_ID &&
1129                     nhdr->n_namesz == sizeof("GNU")) {
1130                         if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
1131                                 memcpy(bf, ptr, BUILD_ID_SIZE);
1132                                 err = BUILD_ID_SIZE;
1133                                 break;
1134                         }
1135                 }
1136                 ptr += descsz;
1137         }
1138 out_elf_end:
1139         elf_end(elf);
1140 out_close:
1141         close(fd);
1142 out:
1143         return err;
1144 }
1145
1146 int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
1147 {
1148         int fd, err = -1;
1149
1150         if (size < BUILD_ID_SIZE)
1151                 goto out;
1152
1153         fd = open(filename, O_RDONLY);
1154         if (fd < 0)
1155                 goto out;
1156
1157         while (1) {
1158                 char bf[BUFSIZ];
1159                 GElf_Nhdr nhdr;
1160                 int namesz, descsz;
1161
1162                 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
1163                         break;
1164
1165                 namesz = NOTE_ALIGN(nhdr.n_namesz);
1166                 descsz = NOTE_ALIGN(nhdr.n_descsz);
1167                 if (nhdr.n_type == NT_GNU_BUILD_ID &&
1168                     nhdr.n_namesz == sizeof("GNU")) {
1169                         if (read(fd, bf, namesz) != namesz)
1170                                 break;
1171                         if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
1172                                 if (read(fd, build_id,
1173                                     BUILD_ID_SIZE) == BUILD_ID_SIZE) {
1174                                         err = 0;
1175                                         break;
1176                                 }
1177                         } else if (read(fd, bf, descsz) != descsz)
1178                                 break;
1179                 } else {
1180                         int n = namesz + descsz;
1181                         if (read(fd, bf, n) != n)
1182                                 break;
1183                 }
1184         }
1185         close(fd);
1186 out:
1187         return err;
1188 }
1189
1190 char dso__symtab_origin(const struct dso *self)
1191 {
1192         static const char origin[] = {
1193                 [DSO__ORIG_KERNEL] =   'k',
1194                 [DSO__ORIG_JAVA_JIT] = 'j',
1195                 [DSO__ORIG_BUILD_ID_CACHE] = 'B',
1196                 [DSO__ORIG_FEDORA] =   'f',
1197                 [DSO__ORIG_UBUNTU] =   'u',
1198                 [DSO__ORIG_BUILDID] =  'b',
1199                 [DSO__ORIG_DSO] =      'd',
1200                 [DSO__ORIG_KMODULE] =  'K',
1201         };
1202
1203         if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
1204                 return '!';
1205         return origin[self->origin];
1206 }
1207
1208 int dso__load(struct dso *self, struct map *map, struct perf_session *session,
1209               symbol_filter_t filter)
1210 {
1211         int size = PATH_MAX;
1212         char *name;
1213         u8 build_id[BUILD_ID_SIZE];
1214         char build_id_hex[BUILD_ID_SIZE * 2 + 1];
1215         int ret = -1;
1216         int fd;
1217
1218         dso__set_loaded(self, map->type);
1219
1220         if (self->kernel)
1221                 return dso__load_kernel_sym(self, map, session, filter);
1222
1223         name = malloc(size);
1224         if (!name)
1225                 return -1;
1226
1227         self->adjust_symbols = 0;
1228
1229         if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
1230                 ret = dso__load_perf_map(self, map, filter);
1231                 self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
1232                                          DSO__ORIG_NOT_FOUND;
1233                 return ret;
1234         }
1235
1236         self->origin = DSO__ORIG_BUILD_ID_CACHE;
1237
1238         if (self->has_build_id) {
1239                 build_id__sprintf(self->build_id, sizeof(self->build_id),
1240                                   build_id_hex);
1241                 snprintf(name, size, "%s/%s/.build-id/%.2s/%s",
1242                          getenv("HOME"), DEBUG_CACHE_DIR,
1243                          build_id_hex, build_id_hex + 2);
1244                 goto open_file;
1245         }
1246 more:
1247         do {
1248                 self->origin++;
1249                 switch (self->origin) {
1250                 case DSO__ORIG_FEDORA:
1251                         snprintf(name, size, "/usr/lib/debug%s.debug",
1252                                  self->long_name);
1253                         break;
1254                 case DSO__ORIG_UBUNTU:
1255                         snprintf(name, size, "/usr/lib/debug%s",
1256                                  self->long_name);
1257                         break;
1258                 case DSO__ORIG_BUILDID:
1259                         if (filename__read_build_id(self->long_name, build_id,
1260                                                     sizeof(build_id))) {
1261                                 build_id__sprintf(build_id, sizeof(build_id),
1262                                                   build_id_hex);
1263                                 snprintf(name, size,
1264                                          "/usr/lib/debug/.build-id/%.2s/%s.debug",
1265                                         build_id_hex, build_id_hex + 2);
1266                                 if (self->has_build_id)
1267                                         goto compare_build_id;
1268                                 break;
1269                         }
1270                         self->origin++;
1271                         /* Fall thru */
1272                 case DSO__ORIG_DSO:
1273                         snprintf(name, size, "%s", self->long_name);
1274                         break;
1275
1276                 default:
1277                         goto out;
1278                 }
1279
1280                 if (self->has_build_id) {
1281                         if (filename__read_build_id(name, build_id,
1282                                                     sizeof(build_id)) < 0)
1283                                 goto more;
1284 compare_build_id:
1285                         if (!dso__build_id_equal(self, build_id))
1286                                 goto more;
1287                 }
1288 open_file:
1289                 fd = open(name, O_RDONLY);
1290         } while (fd < 0);
1291
1292         ret = dso__load_sym(self, map, NULL, name, fd, filter, 0, 0);
1293         close(fd);
1294
1295         /*
1296          * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
1297          */
1298         if (!ret)
1299                 goto more;
1300
1301         if (ret > 0) {
1302                 int nr_plt = dso__synthesize_plt_symbols(self, map, filter);
1303                 if (nr_plt > 0)
1304                         ret += nr_plt;
1305         }
1306 out:
1307         free(name);
1308         if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
1309                 return 0;
1310         return ret;
1311 }
1312
1313 struct map *map_groups__find_by_name(struct map_groups *self,
1314                                      enum map_type type, const char *name)
1315 {
1316         struct rb_node *nd;
1317
1318         for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) {
1319                 struct map *map = rb_entry(nd, struct map, rb_node);
1320
1321                 if (map->dso && strcmp(map->dso->name, name) == 0)
1322                         return map;
1323         }
1324
1325         return NULL;
1326 }
1327
1328 static int perf_session__set_modules_path_dir(struct perf_session *self, char *dirname)
1329 {
1330         struct dirent *dent;
1331         DIR *dir = opendir(dirname);
1332
1333         if (!dir) {
1334                 pr_debug("%s: cannot open %s dir\n", __func__, dirname);
1335                 return -1;
1336         }
1337
1338         while ((dent = readdir(dir)) != NULL) {
1339                 char path[PATH_MAX];
1340
1341                 if (dent->d_type == DT_DIR) {
1342                         if (!strcmp(dent->d_name, ".") ||
1343                             !strcmp(dent->d_name, ".."))
1344                                 continue;
1345
1346                         snprintf(path, sizeof(path), "%s/%s",
1347                                  dirname, dent->d_name);
1348                         if (perf_session__set_modules_path_dir(self, path) < 0)
1349                                 goto failure;
1350                 } else {
1351                         char *dot = strrchr(dent->d_name, '.'),
1352                              dso_name[PATH_MAX];
1353                         struct map *map;
1354                         char *long_name;
1355
1356                         if (dot == NULL || strcmp(dot, ".ko"))
1357                                 continue;
1358                         snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1359                                  (int)(dot - dent->d_name), dent->d_name);
1360
1361                         strxfrchar(dso_name, '-', '_');
1362                         map = map_groups__find_by_name(&self->kmaps, MAP__FUNCTION, dso_name);
1363                         if (map == NULL)
1364                                 continue;
1365
1366                         snprintf(path, sizeof(path), "%s/%s",
1367                                  dirname, dent->d_name);
1368
1369                         long_name = strdup(path);
1370                         if (long_name == NULL)
1371                                 goto failure;
1372                         dso__set_long_name(map->dso, long_name);
1373                 }
1374         }
1375
1376         return 0;
1377 failure:
1378         closedir(dir);
1379         return -1;
1380 }
1381
1382 static int perf_session__set_modules_path(struct perf_session *self)
1383 {
1384         struct utsname uts;
1385         char modules_path[PATH_MAX];
1386
1387         if (uname(&uts) < 0)
1388                 return -1;
1389
1390         snprintf(modules_path, sizeof(modules_path), "/lib/modules/%s/kernel",
1391                  uts.release);
1392
1393         return perf_session__set_modules_path_dir(self, modules_path);
1394 }
1395
1396 /*
1397  * Constructor variant for modules (where we know from /proc/modules where
1398  * they are loaded) and for vmlinux, where only after we load all the
1399  * symbols we'll know where it starts and ends.
1400  */
1401 static struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
1402 {
1403         struct map *self = malloc(sizeof(*self));
1404
1405         if (self != NULL) {
1406                 /*
1407                  * ->end will be filled after we load all the symbols
1408                  */
1409                 map__init(self, type, start, 0, 0, dso);
1410         }
1411
1412         return self;
1413 }
1414
1415 static int perf_session__create_module_maps(struct perf_session *self)
1416 {
1417         char *line = NULL;
1418         size_t n;
1419         FILE *file = fopen("/proc/modules", "r");
1420         struct map *map;
1421
1422         if (file == NULL)
1423                 return -1;
1424
1425         while (!feof(file)) {
1426                 char name[PATH_MAX];
1427                 u64 start;
1428                 struct dso *dso;
1429                 char *sep;
1430                 int line_len;
1431
1432                 line_len = getline(&line, &n, file);
1433                 if (line_len < 0)
1434                         break;
1435
1436                 if (!line)
1437                         goto out_failure;
1438
1439                 line[--line_len] = '\0'; /* \n */
1440
1441                 sep = strrchr(line, 'x');
1442                 if (sep == NULL)
1443                         continue;
1444
1445                 hex2u64(sep + 1, &start);
1446
1447                 sep = strchr(line, ' ');
1448                 if (sep == NULL)
1449                         continue;
1450
1451                 *sep = '\0';
1452
1453                 snprintf(name, sizeof(name), "[%s]", line);
1454                 dso = dso__new(name);
1455
1456                 if (dso == NULL)
1457                         goto out_delete_line;
1458
1459                 map = map__new2(start, dso, MAP__FUNCTION);
1460                 if (map == NULL) {
1461                         dso__delete(dso);
1462                         goto out_delete_line;
1463                 }
1464
1465                 snprintf(name, sizeof(name),
1466                          "/sys/module/%s/notes/.note.gnu.build-id", line);
1467                 if (sysfs__read_build_id(name, dso->build_id,
1468                                          sizeof(dso->build_id)) == 0)
1469                         dso->has_build_id = true;
1470
1471                 dso->origin = DSO__ORIG_KMODULE;
1472                 map_groups__insert(&self->kmaps, map);
1473                 dsos__add(&dsos__kernel, dso);
1474         }
1475
1476         free(line);
1477         fclose(file);
1478
1479         return perf_session__set_modules_path(self);
1480
1481 out_delete_line:
1482         free(line);
1483 out_failure:
1484         return -1;
1485 }
1486
1487 static int dso__load_vmlinux(struct dso *self, struct map *map,
1488                              struct perf_session *session,
1489                              const char *vmlinux, symbol_filter_t filter)
1490 {
1491         int err = -1, fd;
1492
1493         if (self->has_build_id) {
1494                 u8 build_id[BUILD_ID_SIZE];
1495
1496                 if (filename__read_build_id(vmlinux, build_id,
1497                                             sizeof(build_id)) < 0) {
1498                         pr_debug("No build_id in %s, ignoring it\n", vmlinux);
1499                         return -1;
1500                 }
1501                 if (!dso__build_id_equal(self, build_id)) {
1502                         char expected_build_id[BUILD_ID_SIZE * 2 + 1],
1503                              vmlinux_build_id[BUILD_ID_SIZE * 2 + 1];
1504
1505                         build_id__sprintf(self->build_id,
1506                                           sizeof(self->build_id),
1507                                           expected_build_id);
1508                         build_id__sprintf(build_id, sizeof(build_id),
1509                                           vmlinux_build_id);
1510                         pr_debug("build_id in %s is %s while expected is %s, "
1511                                  "ignoring it\n", vmlinux, vmlinux_build_id,
1512                                  expected_build_id);
1513                         return -1;
1514                 }
1515         }
1516
1517         fd = open(vmlinux, O_RDONLY);
1518         if (fd < 0)
1519                 return -1;
1520
1521         dso__set_loaded(self, map->type);
1522         err = dso__load_sym(self, map, session, self->long_name, fd, filter, 1, 0);
1523         close(fd);
1524
1525         return err;
1526 }
1527
1528 static int dso__load_kernel_sym(struct dso *self, struct map *map,
1529                                 struct perf_session *session, symbol_filter_t filter)
1530 {
1531         int err;
1532         bool is_kallsyms;
1533
1534         if (vmlinux_path != NULL) {
1535                 int i;
1536                 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1537                          vmlinux_path__nr_entries);
1538                 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
1539                         err = dso__load_vmlinux(self, map, session,
1540                                                 vmlinux_path[i], filter);
1541                         if (err > 0) {
1542                                 pr_debug("Using %s for symbols\n",
1543                                          vmlinux_path[i]);
1544                                 dso__set_long_name(self,
1545                                                    strdup(vmlinux_path[i]));
1546                                 goto out_fixup;
1547                         }
1548                 }
1549         }
1550
1551         is_kallsyms = self->long_name[0] == '[';
1552         if (is_kallsyms)
1553                 goto do_kallsyms;
1554
1555         err = dso__load_vmlinux(self, map, session, self->long_name, filter);
1556         if (err <= 0) {
1557                 pr_info("The file %s cannot be used, "
1558                         "trying to use /proc/kallsyms...", self->long_name);
1559 do_kallsyms:
1560                 err = dso__load_kallsyms(self, map, session, filter);
1561                 if (err > 0 && !is_kallsyms)
1562                         dso__set_long_name(self, strdup("[kernel.kallsyms]"));
1563         }
1564
1565         if (err > 0) {
1566 out_fixup:
1567                 map__fixup_start(map);
1568                 map__fixup_end(map);
1569         }
1570
1571         return err;
1572 }
1573
1574 LIST_HEAD(dsos__user);
1575 LIST_HEAD(dsos__kernel);
1576 struct dso *vdso;
1577
1578 static void dsos__add(struct list_head *head, struct dso *dso)
1579 {
1580         list_add_tail(&dso->node, head);
1581 }
1582
1583 static struct dso *dsos__find(struct list_head *head, const char *name)
1584 {
1585         struct dso *pos;
1586
1587         list_for_each_entry(pos, head, node)
1588                 if (strcmp(pos->name, name) == 0)
1589                         return pos;
1590         return NULL;
1591 }
1592
1593 struct dso *dsos__findnew(const char *name)
1594 {
1595         struct dso *dso = dsos__find(&dsos__user, name);
1596
1597         if (!dso) {
1598                 dso = dso__new(name);
1599                 if (dso != NULL) {
1600                         dsos__add(&dsos__user, dso);
1601                         dso__set_basename(dso);
1602                 }
1603         }
1604
1605         return dso;
1606 }
1607
1608 static void __dsos__fprintf(struct list_head *head, FILE *fp)
1609 {
1610         struct dso *pos;
1611
1612         list_for_each_entry(pos, head, node) {
1613                 int i;
1614                 for (i = 0; i < MAP__NR_TYPES; ++i)
1615                         dso__fprintf(pos, i, fp);
1616         }
1617 }
1618
1619 void dsos__fprintf(FILE *fp)
1620 {
1621         __dsos__fprintf(&dsos__kernel, fp);
1622         __dsos__fprintf(&dsos__user, fp);
1623 }
1624
1625 static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp)
1626 {
1627         struct dso *pos;
1628         size_t ret = 0;
1629
1630         list_for_each_entry(pos, head, node) {
1631                 ret += dso__fprintf_buildid(pos, fp);
1632                 ret += fprintf(fp, " %s\n", pos->long_name);
1633         }
1634         return ret;
1635 }
1636
1637 size_t dsos__fprintf_buildid(FILE *fp)
1638 {
1639         return (__dsos__fprintf_buildid(&dsos__kernel, fp) +
1640                 __dsos__fprintf_buildid(&dsos__user, fp));
1641 }
1642
1643 static struct dso *dsos__create_kernel( const char *vmlinux)
1644 {
1645         struct dso *kernel = dso__new(vmlinux ?: "[kernel.kallsyms]");
1646
1647         if (kernel == NULL)
1648                 return NULL;
1649
1650         kernel->short_name = "[kernel]";
1651         kernel->kernel     = 1;
1652
1653         vdso = dso__new("[vdso]");
1654         if (vdso == NULL)
1655                 goto out_delete_kernel_dso;
1656         dso__set_loaded(vdso, MAP__FUNCTION);
1657
1658         if (sysfs__read_build_id("/sys/kernel/notes", kernel->build_id,
1659                                  sizeof(kernel->build_id)) == 0)
1660                 kernel->has_build_id = true;
1661
1662         dsos__add(&dsos__kernel, kernel);
1663         dsos__add(&dsos__user, vdso);
1664
1665         return kernel;
1666
1667 out_delete_kernel_dso:
1668         dso__delete(kernel);
1669         return NULL;
1670 }
1671
1672 static int map_groups__create_kernel_maps(struct map_groups *self, const char *vmlinux)
1673 {
1674         struct map *functions, *variables;
1675         struct dso *kernel = dsos__create_kernel(vmlinux);
1676
1677         if (kernel == NULL)
1678                 return -1;
1679
1680         functions = map__new2(0, kernel, MAP__FUNCTION);
1681         if (functions == NULL)
1682                 return -1;
1683
1684         variables = map__new2(0, kernel, MAP__VARIABLE);
1685         if (variables == NULL) {
1686                 map__delete(functions);
1687                 return -1;
1688         }
1689
1690         functions->map_ip = functions->unmap_ip =
1691                 variables->map_ip = variables->unmap_ip = identity__map_ip;
1692         map_groups__insert(self, functions);
1693         map_groups__insert(self, variables);
1694
1695         return 0;
1696 }
1697
1698 static void vmlinux_path__exit(void)
1699 {
1700         while (--vmlinux_path__nr_entries >= 0) {
1701                 free(vmlinux_path[vmlinux_path__nr_entries]);
1702                 vmlinux_path[vmlinux_path__nr_entries] = NULL;
1703         }
1704
1705         free(vmlinux_path);
1706         vmlinux_path = NULL;
1707 }
1708
1709 static int vmlinux_path__init(void)
1710 {
1711         struct utsname uts;
1712         char bf[PATH_MAX];
1713
1714         if (uname(&uts) < 0)
1715                 return -1;
1716
1717         vmlinux_path = malloc(sizeof(char *) * 5);
1718         if (vmlinux_path == NULL)
1719                 return -1;
1720
1721         vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
1722         if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1723                 goto out_fail;
1724         ++vmlinux_path__nr_entries;
1725         vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
1726         if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1727                 goto out_fail;
1728         ++vmlinux_path__nr_entries;
1729         snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
1730         vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1731         if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1732                 goto out_fail;
1733         ++vmlinux_path__nr_entries;
1734         snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
1735         vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1736         if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1737                 goto out_fail;
1738         ++vmlinux_path__nr_entries;
1739         snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
1740                  uts.release);
1741         vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1742         if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1743                 goto out_fail;
1744         ++vmlinux_path__nr_entries;
1745
1746         return 0;
1747
1748 out_fail:
1749         vmlinux_path__exit();
1750         return -1;
1751 }
1752
1753 static int setup_list(struct strlist **list, const char *list_str,
1754                       const char *list_name)
1755 {
1756         if (list_str == NULL)
1757                 return 0;
1758
1759         *list = strlist__new(true, list_str);
1760         if (!*list) {
1761                 pr_err("problems parsing %s list\n", list_name);
1762                 return -1;
1763         }
1764         return 0;
1765 }
1766
1767 int symbol__init(void)
1768 {
1769         elf_version(EV_CURRENT);
1770         if (symbol_conf.sort_by_name)
1771                 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
1772                                           sizeof(struct symbol));
1773
1774         if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
1775                 return -1;
1776
1777         if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
1778                 pr_err("'.' is the only non valid --field-separator argument\n");
1779                 return -1;
1780         }
1781
1782         if (setup_list(&symbol_conf.dso_list,
1783                        symbol_conf.dso_list_str, "dso") < 0)
1784                 return -1;
1785
1786         if (setup_list(&symbol_conf.comm_list,
1787                        symbol_conf.comm_list_str, "comm") < 0)
1788                 goto out_free_dso_list;
1789
1790         if (setup_list(&symbol_conf.sym_list,
1791                        symbol_conf.sym_list_str, "symbol") < 0)
1792                 goto out_free_comm_list;
1793
1794         return 0;
1795
1796 out_free_dso_list:
1797         strlist__delete(symbol_conf.dso_list);
1798 out_free_comm_list:
1799         strlist__delete(symbol_conf.comm_list);
1800         return -1;
1801 }
1802
1803 int perf_session__create_kernel_maps(struct perf_session *self)
1804 {
1805         if (map_groups__create_kernel_maps(&self->kmaps,
1806                                            symbol_conf.vmlinux_name) < 0)
1807                 return -1;
1808
1809         if (symbol_conf.use_modules &&
1810             perf_session__create_module_maps(self) < 0)
1811                 pr_debug("Failed to load list of modules for session %s, "
1812                          "continuing...\n", self->filename);
1813         /*
1814          * Now that we have all the maps created, just set the ->end of them:
1815          */
1816         map_groups__fixup_end(&self->kmaps);
1817         return 0;
1818 }