perf symbols: Factor out dso__load_vmlinux_path()
[pandora-kernel.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 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 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 = zalloc(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(const u8 *self, int len, char *bf)
349 {
350         char *bid = bf;
351         const 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 int kallsyms__parse(const char *filename, void *arg,
387                     int (*process_symbol)(void *arg, const char *name,
388                                                      char type, u64 start))
389 {
390         char *line = NULL;
391         size_t n;
392         int err = 0;
393         FILE *file = fopen(filename, "r");
394
395         if (file == NULL)
396                 goto out_failure;
397
398         while (!feof(file)) {
399                 u64 start;
400                 int line_len, len;
401                 char symbol_type;
402                 char *symbol_name;
403
404                 line_len = getline(&line, &n, file);
405                 if (line_len < 0)
406                         break;
407
408                 if (!line)
409                         goto out_failure;
410
411                 line[--line_len] = '\0'; /* \n */
412
413                 len = hex2u64(line, &start);
414
415                 len++;
416                 if (len + 2 >= line_len)
417                         continue;
418
419                 symbol_type = toupper(line[len]);
420                 symbol_name = line + len + 2;
421
422                 err = process_symbol(arg, symbol_name, symbol_type, start);
423                 if (err)
424                         break;
425         }
426
427         free(line);
428         fclose(file);
429         return err;
430
431 out_failure:
432         return -1;
433 }
434
435 struct process_kallsyms_args {
436         struct map *map;
437         struct dso *dso;
438 };
439
440 static int map__process_kallsym_symbol(void *arg, const char *name,
441                                        char type, u64 start)
442 {
443         struct symbol *sym;
444         struct process_kallsyms_args *a = arg;
445         struct rb_root *root = &a->dso->symbols[a->map->type];
446
447         if (!symbol_type__is_a(type, a->map->type))
448                 return 0;
449
450         /*
451          * Will fix up the end later, when we have all symbols sorted.
452          */
453         sym = symbol__new(start, 0, name);
454
455         if (sym == NULL)
456                 return -ENOMEM;
457         /*
458          * We will pass the symbols to the filter later, in
459          * map__split_kallsyms, when we have split the maps per module
460          */
461         symbols__insert(root, sym);
462         return 0;
463 }
464
465 /*
466  * Loads the function entries in /proc/kallsyms into kernel_map->dso,
467  * so that we can in the next step set the symbol ->end address and then
468  * call kernel_maps__split_kallsyms.
469  */
470 static int dso__load_all_kallsyms(struct dso *self, const char *filename,
471                                   struct map *map)
472 {
473         struct process_kallsyms_args args = { .map = map, .dso = self, };
474         return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
475 }
476
477 /*
478  * Split the symbols into maps, making sure there are no overlaps, i.e. the
479  * kernel range is broken in several maps, named [kernel].N, as we don't have
480  * the original ELF section names vmlinux have.
481  */
482 static int dso__split_kallsyms(struct dso *self, struct map *map,
483                                struct perf_session *session, symbol_filter_t filter)
484 {
485         struct map *curr_map = map;
486         struct symbol *pos;
487         int count = 0;
488         struct rb_root *root = &self->symbols[map->type];
489         struct rb_node *next = rb_first(root);
490         int kernel_range = 0;
491
492         while (next) {
493                 char *module;
494
495                 pos = rb_entry(next, struct symbol, rb_node);
496                 next = rb_next(&pos->rb_node);
497
498                 module = strchr(pos->name, '\t');
499                 if (module) {
500                         if (!symbol_conf.use_modules)
501                                 goto discard_symbol;
502
503                         *module++ = '\0';
504
505                         if (strcmp(curr_map->dso->short_name, module)) {
506                                 curr_map = map_groups__find_by_name(&session->kmaps, map->type, module);
507                                 if (curr_map == NULL) {
508                                         pr_debug("/proc/{kallsyms,modules} "
509                                                  "inconsistency while looking "
510                                                  "for \"%s\" module!\n", module);
511                                         return -1;
512                                 }
513
514                                 if (curr_map->dso->loaded)
515                                         goto discard_symbol;
516                         }
517                         /*
518                          * So that we look just like we get from .ko files,
519                          * i.e. not prelinked, relative to map->start.
520                          */
521                         pos->start = curr_map->map_ip(curr_map, pos->start);
522                         pos->end   = curr_map->map_ip(curr_map, pos->end);
523                 } else if (curr_map != map) {
524                         char dso_name[PATH_MAX];
525                         struct dso *dso;
526
527                         snprintf(dso_name, sizeof(dso_name), "[kernel].%d",
528                                  kernel_range++);
529
530                         dso = dso__new(dso_name);
531                         if (dso == NULL)
532                                 return -1;
533
534                         curr_map = map__new2(pos->start, dso, map->type);
535                         if (map == NULL) {
536                                 dso__delete(dso);
537                                 return -1;
538                         }
539
540                         curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
541                         map_groups__insert(&session->kmaps, curr_map);
542                         ++kernel_range;
543                 }
544
545                 if (filter && filter(curr_map, pos)) {
546 discard_symbol:         rb_erase(&pos->rb_node, root);
547                         symbol__delete(pos);
548                 } else {
549                         if (curr_map != map) {
550                                 rb_erase(&pos->rb_node, root);
551                                 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
552                         }
553                         count++;
554                 }
555         }
556
557         return count;
558 }
559
560
561 static int dso__load_kallsyms(struct dso *self, const char *filename, struct map *map,
562                               struct perf_session *session, symbol_filter_t filter)
563 {
564         if (dso__load_all_kallsyms(self, filename, map) < 0)
565                 return -1;
566
567         symbols__fixup_end(&self->symbols[map->type]);
568         self->origin = DSO__ORIG_KERNEL;
569
570         return dso__split_kallsyms(self, map, session, filter);
571 }
572
573 static int dso__load_perf_map(struct dso *self, struct map *map,
574                               symbol_filter_t filter)
575 {
576         char *line = NULL;
577         size_t n;
578         FILE *file;
579         int nr_syms = 0;
580
581         file = fopen(self->long_name, "r");
582         if (file == NULL)
583                 goto out_failure;
584
585         while (!feof(file)) {
586                 u64 start, size;
587                 struct symbol *sym;
588                 int line_len, len;
589
590                 line_len = getline(&line, &n, file);
591                 if (line_len < 0)
592                         break;
593
594                 if (!line)
595                         goto out_failure;
596
597                 line[--line_len] = '\0'; /* \n */
598
599                 len = hex2u64(line, &start);
600
601                 len++;
602                 if (len + 2 >= line_len)
603                         continue;
604
605                 len += hex2u64(line + len, &size);
606
607                 len++;
608                 if (len + 2 >= line_len)
609                         continue;
610
611                 sym = symbol__new(start, size, line + len);
612
613                 if (sym == NULL)
614                         goto out_delete_line;
615
616                 if (filter && filter(map, sym))
617                         symbol__delete(sym);
618                 else {
619                         symbols__insert(&self->symbols[map->type], sym);
620                         nr_syms++;
621                 }
622         }
623
624         free(line);
625         fclose(file);
626
627         return nr_syms;
628
629 out_delete_line:
630         free(line);
631 out_failure:
632         return -1;
633 }
634
635 /**
636  * elf_symtab__for_each_symbol - iterate thru all the symbols
637  *
638  * @self: struct elf_symtab instance to iterate
639  * @idx: uint32_t idx
640  * @sym: GElf_Sym iterator
641  */
642 #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
643         for (idx = 0, gelf_getsym(syms, idx, &sym);\
644              idx < nr_syms; \
645              idx++, gelf_getsym(syms, idx, &sym))
646
647 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
648 {
649         return GELF_ST_TYPE(sym->st_info);
650 }
651
652 static inline int elf_sym__is_function(const GElf_Sym *sym)
653 {
654         return elf_sym__type(sym) == STT_FUNC &&
655                sym->st_name != 0 &&
656                sym->st_shndx != SHN_UNDEF;
657 }
658
659 static inline bool elf_sym__is_object(const GElf_Sym *sym)
660 {
661         return elf_sym__type(sym) == STT_OBJECT &&
662                 sym->st_name != 0 &&
663                 sym->st_shndx != SHN_UNDEF;
664 }
665
666 static inline int elf_sym__is_label(const GElf_Sym *sym)
667 {
668         return elf_sym__type(sym) == STT_NOTYPE &&
669                 sym->st_name != 0 &&
670                 sym->st_shndx != SHN_UNDEF &&
671                 sym->st_shndx != SHN_ABS;
672 }
673
674 static inline const char *elf_sec__name(const GElf_Shdr *shdr,
675                                         const Elf_Data *secstrs)
676 {
677         return secstrs->d_buf + shdr->sh_name;
678 }
679
680 static inline int elf_sec__is_text(const GElf_Shdr *shdr,
681                                         const Elf_Data *secstrs)
682 {
683         return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
684 }
685
686 static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
687                                     const Elf_Data *secstrs)
688 {
689         return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
690 }
691
692 static inline const char *elf_sym__name(const GElf_Sym *sym,
693                                         const Elf_Data *symstrs)
694 {
695         return symstrs->d_buf + sym->st_name;
696 }
697
698 static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
699                                     GElf_Shdr *shp, const char *name,
700                                     size_t *idx)
701 {
702         Elf_Scn *sec = NULL;
703         size_t cnt = 1;
704
705         while ((sec = elf_nextscn(elf, sec)) != NULL) {
706                 char *str;
707
708                 gelf_getshdr(sec, shp);
709                 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
710                 if (!strcmp(name, str)) {
711                         if (idx)
712                                 *idx = cnt;
713                         break;
714                 }
715                 ++cnt;
716         }
717
718         return sec;
719 }
720
721 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
722         for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
723              idx < nr_entries; \
724              ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
725
726 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
727         for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
728              idx < nr_entries; \
729              ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
730
731 /*
732  * We need to check if we have a .dynsym, so that we can handle the
733  * .plt, synthesizing its symbols, that aren't on the symtabs (be it
734  * .dynsym or .symtab).
735  * And always look at the original dso, not at debuginfo packages, that
736  * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
737  */
738 static int dso__synthesize_plt_symbols(struct  dso *self, struct map *map,
739                                        symbol_filter_t filter)
740 {
741         uint32_t nr_rel_entries, idx;
742         GElf_Sym sym;
743         u64 plt_offset;
744         GElf_Shdr shdr_plt;
745         struct symbol *f;
746         GElf_Shdr shdr_rel_plt, shdr_dynsym;
747         Elf_Data *reldata, *syms, *symstrs;
748         Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
749         size_t dynsym_idx;
750         GElf_Ehdr ehdr;
751         char sympltname[1024];
752         Elf *elf;
753         int nr = 0, symidx, fd, err = 0;
754
755         fd = open(self->long_name, O_RDONLY);
756         if (fd < 0)
757                 goto out;
758
759         elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
760         if (elf == NULL)
761                 goto out_close;
762
763         if (gelf_getehdr(elf, &ehdr) == NULL)
764                 goto out_elf_end;
765
766         scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
767                                          ".dynsym", &dynsym_idx);
768         if (scn_dynsym == NULL)
769                 goto out_elf_end;
770
771         scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
772                                           ".rela.plt", NULL);
773         if (scn_plt_rel == NULL) {
774                 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
775                                                   ".rel.plt", NULL);
776                 if (scn_plt_rel == NULL)
777                         goto out_elf_end;
778         }
779
780         err = -1;
781
782         if (shdr_rel_plt.sh_link != dynsym_idx)
783                 goto out_elf_end;
784
785         if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
786                 goto out_elf_end;
787
788         /*
789          * Fetch the relocation section to find the idxes to the GOT
790          * and the symbols in the .dynsym they refer to.
791          */
792         reldata = elf_getdata(scn_plt_rel, NULL);
793         if (reldata == NULL)
794                 goto out_elf_end;
795
796         syms = elf_getdata(scn_dynsym, NULL);
797         if (syms == NULL)
798                 goto out_elf_end;
799
800         scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
801         if (scn_symstrs == NULL)
802                 goto out_elf_end;
803
804         symstrs = elf_getdata(scn_symstrs, NULL);
805         if (symstrs == NULL)
806                 goto out_elf_end;
807
808         nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
809         plt_offset = shdr_plt.sh_offset;
810
811         if (shdr_rel_plt.sh_type == SHT_RELA) {
812                 GElf_Rela pos_mem, *pos;
813
814                 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
815                                            nr_rel_entries) {
816                         symidx = GELF_R_SYM(pos->r_info);
817                         plt_offset += shdr_plt.sh_entsize;
818                         gelf_getsym(syms, symidx, &sym);
819                         snprintf(sympltname, sizeof(sympltname),
820                                  "%s@plt", elf_sym__name(&sym, symstrs));
821
822                         f = symbol__new(plt_offset, shdr_plt.sh_entsize,
823                                         sympltname);
824                         if (!f)
825                                 goto out_elf_end;
826
827                         if (filter && filter(map, f))
828                                 symbol__delete(f);
829                         else {
830                                 symbols__insert(&self->symbols[map->type], f);
831                                 ++nr;
832                         }
833                 }
834         } else if (shdr_rel_plt.sh_type == SHT_REL) {
835                 GElf_Rel pos_mem, *pos;
836                 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
837                                           nr_rel_entries) {
838                         symidx = GELF_R_SYM(pos->r_info);
839                         plt_offset += shdr_plt.sh_entsize;
840                         gelf_getsym(syms, symidx, &sym);
841                         snprintf(sympltname, sizeof(sympltname),
842                                  "%s@plt", elf_sym__name(&sym, symstrs));
843
844                         f = symbol__new(plt_offset, shdr_plt.sh_entsize,
845                                         sympltname);
846                         if (!f)
847                                 goto out_elf_end;
848
849                         if (filter && filter(map, f))
850                                 symbol__delete(f);
851                         else {
852                                 symbols__insert(&self->symbols[map->type], f);
853                                 ++nr;
854                         }
855                 }
856         }
857
858         err = 0;
859 out_elf_end:
860         elf_end(elf);
861 out_close:
862         close(fd);
863
864         if (err == 0)
865                 return nr;
866 out:
867         pr_warning("%s: problems reading %s PLT info.\n",
868                    __func__, self->long_name);
869         return 0;
870 }
871
872 static bool elf_sym__is_a(GElf_Sym *self, enum map_type type)
873 {
874         switch (type) {
875         case MAP__FUNCTION:
876                 return elf_sym__is_function(self);
877         case MAP__VARIABLE:
878                 return elf_sym__is_object(self);
879         default:
880                 return false;
881         }
882 }
883
884 static bool elf_sec__is_a(GElf_Shdr *self, Elf_Data *secstrs, enum map_type type)
885 {
886         switch (type) {
887         case MAP__FUNCTION:
888                 return elf_sec__is_text(self, secstrs);
889         case MAP__VARIABLE:
890                 return elf_sec__is_data(self, secstrs);
891         default:
892                 return false;
893         }
894 }
895
896 static int dso__load_sym(struct dso *self, struct map *map,
897                          struct perf_session *session, const char *name, int fd,
898                          symbol_filter_t filter, int kernel, int kmodule)
899 {
900         struct map *curr_map = map;
901         struct dso *curr_dso = self;
902         size_t dso_name_len = strlen(self->short_name);
903         Elf_Data *symstrs, *secstrs;
904         uint32_t nr_syms;
905         int err = -1;
906         uint32_t idx;
907         GElf_Ehdr ehdr;
908         GElf_Shdr shdr;
909         Elf_Data *syms;
910         GElf_Sym sym;
911         Elf_Scn *sec, *sec_strndx;
912         Elf *elf;
913         int nr = 0;
914
915         elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
916         if (elf == NULL) {
917                 pr_err("%s: cannot read %s ELF file.\n", __func__, name);
918                 goto out_close;
919         }
920
921         if (gelf_getehdr(elf, &ehdr) == NULL) {
922                 pr_err("%s: cannot get elf header.\n", __func__);
923                 goto out_elf_end;
924         }
925
926         sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
927         if (sec == NULL) {
928                 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
929                 if (sec == NULL)
930                         goto out_elf_end;
931         }
932
933         syms = elf_getdata(sec, NULL);
934         if (syms == NULL)
935                 goto out_elf_end;
936
937         sec = elf_getscn(elf, shdr.sh_link);
938         if (sec == NULL)
939                 goto out_elf_end;
940
941         symstrs = elf_getdata(sec, NULL);
942         if (symstrs == NULL)
943                 goto out_elf_end;
944
945         sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
946         if (sec_strndx == NULL)
947                 goto out_elf_end;
948
949         secstrs = elf_getdata(sec_strndx, NULL);
950         if (secstrs == NULL)
951                 goto out_elf_end;
952
953         nr_syms = shdr.sh_size / shdr.sh_entsize;
954
955         memset(&sym, 0, sizeof(sym));
956         if (!kernel) {
957                 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
958                                 elf_section_by_name(elf, &ehdr, &shdr,
959                                                      ".gnu.prelink_undo",
960                                                      NULL) != NULL);
961         } else self->adjust_symbols = 0;
962
963         elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
964                 struct symbol *f;
965                 const char *elf_name = elf_sym__name(&sym, symstrs);
966                 char *demangled = NULL;
967                 int is_label = elf_sym__is_label(&sym);
968                 const char *section_name;
969
970                 if (kernel && session->ref_reloc_sym.name != NULL &&
971                     strcmp(elf_name, session->ref_reloc_sym.name) == 0)
972                         perf_session__reloc_vmlinux_maps(session, sym.st_value);
973
974                 if (!is_label && !elf_sym__is_a(&sym, map->type))
975                         continue;
976
977                 sec = elf_getscn(elf, sym.st_shndx);
978                 if (!sec)
979                         goto out_elf_end;
980
981                 gelf_getshdr(sec, &shdr);
982
983                 if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type))
984                         continue;
985
986                 section_name = elf_sec__name(&shdr, secstrs);
987
988                 if (kernel || kmodule) {
989                         char dso_name[PATH_MAX];
990
991                         if (strcmp(section_name,
992                                    curr_dso->short_name + dso_name_len) == 0)
993                                 goto new_symbol;
994
995                         if (strcmp(section_name, ".text") == 0) {
996                                 curr_map = map;
997                                 curr_dso = self;
998                                 goto new_symbol;
999                         }
1000
1001                         snprintf(dso_name, sizeof(dso_name),
1002                                  "%s%s", self->short_name, section_name);
1003
1004                         curr_map = map_groups__find_by_name(&session->kmaps, map->type, dso_name);
1005                         if (curr_map == NULL) {
1006                                 u64 start = sym.st_value;
1007
1008                                 if (kmodule)
1009                                         start += map->start + shdr.sh_offset;
1010
1011                                 curr_dso = dso__new(dso_name);
1012                                 if (curr_dso == NULL)
1013                                         goto out_elf_end;
1014                                 curr_map = map__new2(start, curr_dso,
1015                                                      MAP__FUNCTION);
1016                                 if (curr_map == NULL) {
1017                                         dso__delete(curr_dso);
1018                                         goto out_elf_end;
1019                                 }
1020                                 curr_map->map_ip = identity__map_ip;
1021                                 curr_map->unmap_ip = identity__map_ip;
1022                                 curr_dso->origin = DSO__ORIG_KERNEL;
1023                                 map_groups__insert(&session->kmaps, curr_map);
1024                                 dsos__add(&dsos__kernel, curr_dso);
1025                         } else
1026                                 curr_dso = curr_map->dso;
1027
1028                         goto new_symbol;
1029                 }
1030
1031                 if (curr_dso->adjust_symbols) {
1032                         pr_debug2("adjusting symbol: st_value: %Lx sh_addr: "
1033                                   "%Lx sh_offset: %Lx\n", (u64)sym.st_value,
1034                                   (u64)shdr.sh_addr, (u64)shdr.sh_offset);
1035                         sym.st_value -= shdr.sh_addr - shdr.sh_offset;
1036                 }
1037                 /*
1038                  * We need to figure out if the object was created from C++ sources
1039                  * DWARF DW_compile_unit has this, but we don't always have access
1040                  * to it...
1041                  */
1042                 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
1043                 if (demangled != NULL)
1044                         elf_name = demangled;
1045 new_symbol:
1046                 f = symbol__new(sym.st_value, sym.st_size, elf_name);
1047                 free(demangled);
1048                 if (!f)
1049                         goto out_elf_end;
1050
1051                 if (filter && filter(curr_map, f))
1052                         symbol__delete(f);
1053                 else {
1054                         symbols__insert(&curr_dso->symbols[curr_map->type], f);
1055                         nr++;
1056                 }
1057         }
1058
1059         /*
1060          * For misannotated, zeroed, ASM function sizes.
1061          */
1062         if (nr > 0)
1063                 symbols__fixup_end(&self->symbols[map->type]);
1064         err = nr;
1065 out_elf_end:
1066         elf_end(elf);
1067 out_close:
1068         return err;
1069 }
1070
1071 static bool dso__build_id_equal(const struct dso *self, u8 *build_id)
1072 {
1073         return memcmp(self->build_id, build_id, sizeof(self->build_id)) == 0;
1074 }
1075
1076 static bool __dsos__read_build_ids(struct list_head *head)
1077 {
1078         bool have_build_id = false;
1079         struct dso *pos;
1080
1081         list_for_each_entry(pos, head, node)
1082                 if (filename__read_build_id(pos->long_name, pos->build_id,
1083                                             sizeof(pos->build_id)) > 0) {
1084                         have_build_id     = true;
1085                         pos->has_build_id = true;
1086                 }
1087
1088         return have_build_id;
1089 }
1090
1091 bool dsos__read_build_ids(void)
1092 {
1093         bool kbuildids = __dsos__read_build_ids(&dsos__kernel),
1094              ubuildids = __dsos__read_build_ids(&dsos__user);
1095         return kbuildids || ubuildids;
1096 }
1097
1098 /*
1099  * Align offset to 4 bytes as needed for note name and descriptor data.
1100  */
1101 #define NOTE_ALIGN(n) (((n) + 3) & -4U)
1102
1103 int filename__read_build_id(const char *filename, void *bf, size_t size)
1104 {
1105         int fd, err = -1;
1106         GElf_Ehdr ehdr;
1107         GElf_Shdr shdr;
1108         Elf_Data *data;
1109         Elf_Scn *sec;
1110         Elf_Kind ek;
1111         void *ptr;
1112         Elf *elf;
1113
1114         if (size < BUILD_ID_SIZE)
1115                 goto out;
1116
1117         fd = open(filename, O_RDONLY);
1118         if (fd < 0)
1119                 goto out;
1120
1121         elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1122         if (elf == NULL) {
1123                 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
1124                 goto out_close;
1125         }
1126
1127         ek = elf_kind(elf);
1128         if (ek != ELF_K_ELF)
1129                 goto out_elf_end;
1130
1131         if (gelf_getehdr(elf, &ehdr) == NULL) {
1132                 pr_err("%s: cannot get elf header.\n", __func__);
1133                 goto out_elf_end;
1134         }
1135
1136         sec = elf_section_by_name(elf, &ehdr, &shdr,
1137                                   ".note.gnu.build-id", NULL);
1138         if (sec == NULL) {
1139                 sec = elf_section_by_name(elf, &ehdr, &shdr,
1140                                           ".notes", NULL);
1141                 if (sec == NULL)
1142                         goto out_elf_end;
1143         }
1144
1145         data = elf_getdata(sec, NULL);
1146         if (data == NULL)
1147                 goto out_elf_end;
1148
1149         ptr = data->d_buf;
1150         while (ptr < (data->d_buf + data->d_size)) {
1151                 GElf_Nhdr *nhdr = ptr;
1152                 int namesz = NOTE_ALIGN(nhdr->n_namesz),
1153                     descsz = NOTE_ALIGN(nhdr->n_descsz);
1154                 const char *name;
1155
1156                 ptr += sizeof(*nhdr);
1157                 name = ptr;
1158                 ptr += namesz;
1159                 if (nhdr->n_type == NT_GNU_BUILD_ID &&
1160                     nhdr->n_namesz == sizeof("GNU")) {
1161                         if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
1162                                 memcpy(bf, ptr, BUILD_ID_SIZE);
1163                                 err = BUILD_ID_SIZE;
1164                                 break;
1165                         }
1166                 }
1167                 ptr += descsz;
1168         }
1169 out_elf_end:
1170         elf_end(elf);
1171 out_close:
1172         close(fd);
1173 out:
1174         return err;
1175 }
1176
1177 int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
1178 {
1179         int fd, err = -1;
1180
1181         if (size < BUILD_ID_SIZE)
1182                 goto out;
1183
1184         fd = open(filename, O_RDONLY);
1185         if (fd < 0)
1186                 goto out;
1187
1188         while (1) {
1189                 char bf[BUFSIZ];
1190                 GElf_Nhdr nhdr;
1191                 int namesz, descsz;
1192
1193                 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
1194                         break;
1195
1196                 namesz = NOTE_ALIGN(nhdr.n_namesz);
1197                 descsz = NOTE_ALIGN(nhdr.n_descsz);
1198                 if (nhdr.n_type == NT_GNU_BUILD_ID &&
1199                     nhdr.n_namesz == sizeof("GNU")) {
1200                         if (read(fd, bf, namesz) != namesz)
1201                                 break;
1202                         if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
1203                                 if (read(fd, build_id,
1204                                     BUILD_ID_SIZE) == BUILD_ID_SIZE) {
1205                                         err = 0;
1206                                         break;
1207                                 }
1208                         } else if (read(fd, bf, descsz) != descsz)
1209                                 break;
1210                 } else {
1211                         int n = namesz + descsz;
1212                         if (read(fd, bf, n) != n)
1213                                 break;
1214                 }
1215         }
1216         close(fd);
1217 out:
1218         return err;
1219 }
1220
1221 char dso__symtab_origin(const struct dso *self)
1222 {
1223         static const char origin[] = {
1224                 [DSO__ORIG_KERNEL] =   'k',
1225                 [DSO__ORIG_JAVA_JIT] = 'j',
1226                 [DSO__ORIG_BUILD_ID_CACHE] = 'B',
1227                 [DSO__ORIG_FEDORA] =   'f',
1228                 [DSO__ORIG_UBUNTU] =   'u',
1229                 [DSO__ORIG_BUILDID] =  'b',
1230                 [DSO__ORIG_DSO] =      'd',
1231                 [DSO__ORIG_KMODULE] =  'K',
1232         };
1233
1234         if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
1235                 return '!';
1236         return origin[self->origin];
1237 }
1238
1239 int dso__load(struct dso *self, struct map *map, struct perf_session *session,
1240               symbol_filter_t filter)
1241 {
1242         int size = PATH_MAX;
1243         char *name;
1244         u8 build_id[BUILD_ID_SIZE];
1245         char build_id_hex[BUILD_ID_SIZE * 2 + 1];
1246         int ret = -1;
1247         int fd;
1248
1249         dso__set_loaded(self, map->type);
1250
1251         if (self->kernel)
1252                 return dso__load_kernel_sym(self, map, session, filter);
1253
1254         name = malloc(size);
1255         if (!name)
1256                 return -1;
1257
1258         self->adjust_symbols = 0;
1259
1260         if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
1261                 ret = dso__load_perf_map(self, map, filter);
1262                 self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
1263                                          DSO__ORIG_NOT_FOUND;
1264                 return ret;
1265         }
1266
1267         self->origin = DSO__ORIG_BUILD_ID_CACHE;
1268
1269         if (self->has_build_id) {
1270                 build_id__sprintf(self->build_id, sizeof(self->build_id),
1271                                   build_id_hex);
1272                 snprintf(name, size, "%s/%s/.build-id/%.2s/%s",
1273                          getenv("HOME"), DEBUG_CACHE_DIR,
1274                          build_id_hex, build_id_hex + 2);
1275                 goto open_file;
1276         }
1277 more:
1278         do {
1279                 self->origin++;
1280                 switch (self->origin) {
1281                 case DSO__ORIG_FEDORA:
1282                         snprintf(name, size, "/usr/lib/debug%s.debug",
1283                                  self->long_name);
1284                         break;
1285                 case DSO__ORIG_UBUNTU:
1286                         snprintf(name, size, "/usr/lib/debug%s",
1287                                  self->long_name);
1288                         break;
1289                 case DSO__ORIG_BUILDID:
1290                         if (filename__read_build_id(self->long_name, build_id,
1291                                                     sizeof(build_id))) {
1292                                 build_id__sprintf(build_id, sizeof(build_id),
1293                                                   build_id_hex);
1294                                 snprintf(name, size,
1295                                          "/usr/lib/debug/.build-id/%.2s/%s.debug",
1296                                         build_id_hex, build_id_hex + 2);
1297                                 if (self->has_build_id)
1298                                         goto compare_build_id;
1299                                 break;
1300                         }
1301                         self->origin++;
1302                         /* Fall thru */
1303                 case DSO__ORIG_DSO:
1304                         snprintf(name, size, "%s", self->long_name);
1305                         break;
1306
1307                 default:
1308                         goto out;
1309                 }
1310
1311                 if (self->has_build_id) {
1312                         if (filename__read_build_id(name, build_id,
1313                                                     sizeof(build_id)) < 0)
1314                                 goto more;
1315 compare_build_id:
1316                         if (!dso__build_id_equal(self, build_id))
1317                                 goto more;
1318                 }
1319 open_file:
1320                 fd = open(name, O_RDONLY);
1321         } while (fd < 0);
1322
1323         ret = dso__load_sym(self, map, NULL, name, fd, filter, 0, 0);
1324         close(fd);
1325
1326         /*
1327          * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
1328          */
1329         if (!ret)
1330                 goto more;
1331
1332         if (ret > 0) {
1333                 int nr_plt = dso__synthesize_plt_symbols(self, map, filter);
1334                 if (nr_plt > 0)
1335                         ret += nr_plt;
1336         }
1337 out:
1338         free(name);
1339         if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
1340                 return 0;
1341         return ret;
1342 }
1343
1344 struct map *map_groups__find_by_name(struct map_groups *self,
1345                                      enum map_type type, const char *name)
1346 {
1347         struct rb_node *nd;
1348
1349         for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) {
1350                 struct map *map = rb_entry(nd, struct map, rb_node);
1351
1352                 if (map->dso && strcmp(map->dso->short_name, name) == 0)
1353                         return map;
1354         }
1355
1356         return NULL;
1357 }
1358
1359 static int dso__kernel_module_get_build_id(struct dso *self)
1360 {
1361         char filename[PATH_MAX];
1362         /*
1363          * kernel module short names are of the form "[module]" and
1364          * we need just "module" here.
1365          */
1366         const char *name = self->short_name + 1;
1367
1368         snprintf(filename, sizeof(filename),
1369                  "/sys/module/%.*s/notes/.note.gnu.build-id",
1370                  (int)strlen(name - 1), name);
1371
1372         if (sysfs__read_build_id(filename, self->build_id,
1373                                  sizeof(self->build_id)) == 0)
1374                 self->has_build_id = true;
1375
1376         return 0;
1377 }
1378
1379 static int perf_session__set_modules_path_dir(struct perf_session *self, char *dirname)
1380 {
1381         struct dirent *dent;
1382         DIR *dir = opendir(dirname);
1383
1384         if (!dir) {
1385                 pr_debug("%s: cannot open %s dir\n", __func__, dirname);
1386                 return -1;
1387         }
1388
1389         while ((dent = readdir(dir)) != NULL) {
1390                 char path[PATH_MAX];
1391
1392                 if (dent->d_type == DT_DIR) {
1393                         if (!strcmp(dent->d_name, ".") ||
1394                             !strcmp(dent->d_name, ".."))
1395                                 continue;
1396
1397                         snprintf(path, sizeof(path), "%s/%s",
1398                                  dirname, dent->d_name);
1399                         if (perf_session__set_modules_path_dir(self, path) < 0)
1400                                 goto failure;
1401                 } else {
1402                         char *dot = strrchr(dent->d_name, '.'),
1403                              dso_name[PATH_MAX];
1404                         struct map *map;
1405                         char *long_name;
1406
1407                         if (dot == NULL || strcmp(dot, ".ko"))
1408                                 continue;
1409                         snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1410                                  (int)(dot - dent->d_name), dent->d_name);
1411
1412                         strxfrchar(dso_name, '-', '_');
1413                         map = map_groups__find_by_name(&self->kmaps, MAP__FUNCTION, dso_name);
1414                         if (map == NULL)
1415                                 continue;
1416
1417                         snprintf(path, sizeof(path), "%s/%s",
1418                                  dirname, dent->d_name);
1419
1420                         long_name = strdup(path);
1421                         if (long_name == NULL)
1422                                 goto failure;
1423                         dso__set_long_name(map->dso, long_name);
1424                         dso__kernel_module_get_build_id(map->dso);
1425                 }
1426         }
1427
1428         return 0;
1429 failure:
1430         closedir(dir);
1431         return -1;
1432 }
1433
1434 static int perf_session__set_modules_path(struct perf_session *self)
1435 {
1436         struct utsname uts;
1437         char modules_path[PATH_MAX];
1438
1439         if (uname(&uts) < 0)
1440                 return -1;
1441
1442         snprintf(modules_path, sizeof(modules_path), "/lib/modules/%s/kernel",
1443                  uts.release);
1444
1445         return perf_session__set_modules_path_dir(self, modules_path);
1446 }
1447
1448 /*
1449  * Constructor variant for modules (where we know from /proc/modules where
1450  * they are loaded) and for vmlinux, where only after we load all the
1451  * symbols we'll know where it starts and ends.
1452  */
1453 static struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
1454 {
1455         struct map *self = malloc(sizeof(*self));
1456
1457         if (self != NULL) {
1458                 /*
1459                  * ->end will be filled after we load all the symbols
1460                  */
1461                 map__init(self, type, start, 0, 0, dso);
1462         }
1463
1464         return self;
1465 }
1466
1467 struct map *perf_session__new_module_map(struct perf_session *self, u64 start,
1468                                          const char *filename)
1469 {
1470         struct map *map;
1471         struct dso *dso = __dsos__findnew(&dsos__kernel, filename);
1472
1473         if (dso == NULL)
1474                 return NULL;
1475
1476         map = map__new2(start, dso, MAP__FUNCTION);
1477         if (map == NULL)
1478                 return NULL;
1479
1480         dso->origin = DSO__ORIG_KMODULE;
1481         map_groups__insert(&self->kmaps, map);
1482         return map;
1483 }
1484
1485 static int perf_session__create_module_maps(struct perf_session *self)
1486 {
1487         char *line = NULL;
1488         size_t n;
1489         FILE *file = fopen("/proc/modules", "r");
1490         struct map *map;
1491
1492         if (file == NULL)
1493                 return -1;
1494
1495         while (!feof(file)) {
1496                 char name[PATH_MAX];
1497                 u64 start;
1498                 char *sep;
1499                 int line_len;
1500
1501                 line_len = getline(&line, &n, file);
1502                 if (line_len < 0)
1503                         break;
1504
1505                 if (!line)
1506                         goto out_failure;
1507
1508                 line[--line_len] = '\0'; /* \n */
1509
1510                 sep = strrchr(line, 'x');
1511                 if (sep == NULL)
1512                         continue;
1513
1514                 hex2u64(sep + 1, &start);
1515
1516                 sep = strchr(line, ' ');
1517                 if (sep == NULL)
1518                         continue;
1519
1520                 *sep = '\0';
1521
1522                 snprintf(name, sizeof(name), "[%s]", line);
1523                 map = perf_session__new_module_map(self, start, name);
1524                 if (map == NULL)
1525                         goto out_delete_line;
1526                 dso__kernel_module_get_build_id(map->dso);
1527         }
1528
1529         free(line);
1530         fclose(file);
1531
1532         return perf_session__set_modules_path(self);
1533
1534 out_delete_line:
1535         free(line);
1536 out_failure:
1537         return -1;
1538 }
1539
1540 static int dso__load_vmlinux(struct dso *self, struct map *map,
1541                              struct perf_session *session,
1542                              const char *vmlinux, symbol_filter_t filter)
1543 {
1544         int err = -1, fd;
1545
1546         if (self->has_build_id) {
1547                 u8 build_id[BUILD_ID_SIZE];
1548
1549                 if (filename__read_build_id(vmlinux, build_id,
1550                                             sizeof(build_id)) < 0) {
1551                         pr_debug("No build_id in %s, ignoring it\n", vmlinux);
1552                         return -1;
1553                 }
1554                 if (!dso__build_id_equal(self, build_id)) {
1555                         char expected_build_id[BUILD_ID_SIZE * 2 + 1],
1556                              vmlinux_build_id[BUILD_ID_SIZE * 2 + 1];
1557
1558                         build_id__sprintf(self->build_id,
1559                                           sizeof(self->build_id),
1560                                           expected_build_id);
1561                         build_id__sprintf(build_id, sizeof(build_id),
1562                                           vmlinux_build_id);
1563                         pr_debug("build_id in %s is %s while expected is %s, "
1564                                  "ignoring it\n", vmlinux, vmlinux_build_id,
1565                                  expected_build_id);
1566                         return -1;
1567                 }
1568         }
1569
1570         fd = open(vmlinux, O_RDONLY);
1571         if (fd < 0)
1572                 return -1;
1573
1574         dso__set_loaded(self, map->type);
1575         err = dso__load_sym(self, map, session, vmlinux, fd, filter, 1, 0);
1576         close(fd);
1577
1578         return err;
1579 }
1580
1581 int dso__load_vmlinux_path(struct dso *self, struct map *map,
1582                            struct perf_session *session, symbol_filter_t filter)
1583 {
1584         int i, err = 0;
1585
1586         pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1587                  vmlinux_path__nr_entries);
1588
1589         for (i = 0; i < vmlinux_path__nr_entries; ++i) {
1590                 err = dso__load_vmlinux(self, map, session, vmlinux_path[i],
1591                                         filter);
1592                 if (err > 0) {
1593                         pr_debug("Using %s for symbols\n", vmlinux_path[i]);
1594                         dso__set_long_name(self, strdup(vmlinux_path[i]));
1595                         break;
1596                 }
1597         }
1598
1599         return err;
1600 }
1601
1602 static int dso__load_kernel_sym(struct dso *self, struct map *map,
1603                                 struct perf_session *session, symbol_filter_t filter)
1604 {
1605         int err;
1606         const char *kallsyms_filename = NULL;
1607         char *kallsyms_allocated_filename = NULL;
1608         /*
1609          * Step 1: if the user specified a vmlinux filename, use it and only
1610          * it, reporting errors to the user if it cannot be used.
1611          *
1612          * For instance, try to analyse an ARM perf.data file _without_ a
1613          * build-id, or if the user specifies the wrong path to the right
1614          * vmlinux file, obviously we can't fallback to another vmlinux (a
1615          * x86_86 one, on the machine where analysis is being performed, say),
1616          * or worse, /proc/kallsyms.
1617          *
1618          * If the specified file _has_ a build-id and there is a build-id
1619          * section in the perf.data file, we will still do the expected
1620          * validation in dso__load_vmlinux and will bail out if they don't
1621          * match.
1622          */
1623         if (symbol_conf.vmlinux_name != NULL) {
1624                 err = dso__load_vmlinux(self, map, session,
1625                                         symbol_conf.vmlinux_name, filter);
1626                 goto out_try_fixup;
1627         }
1628
1629         if (vmlinux_path != NULL) {
1630                 err = dso__load_vmlinux_path(self, map, session, filter);
1631                 if (err > 0)
1632                         goto out_fixup;
1633         }
1634
1635         /*
1636          * Say the kernel DSO was created when processing the build-id header table,
1637          * we have a build-id, so check if it is the same as the running kernel,
1638          * using it if it is.
1639          */
1640         if (self->has_build_id) {
1641                 u8 kallsyms_build_id[BUILD_ID_SIZE];
1642                 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1643
1644                 if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id,
1645                                          sizeof(kallsyms_build_id)) == 0) {
1646                         if (dso__build_id_equal(self, kallsyms_build_id)) {
1647                                 kallsyms_filename = "/proc/kallsyms";
1648                                 goto do_kallsyms;
1649                         }
1650                 }
1651                 /*
1652                  * Now look if we have it on the build-id cache in
1653                  * $HOME/.debug/[kernel.kallsyms].
1654                  */
1655                 build_id__sprintf(self->build_id, sizeof(self->build_id),
1656                                   sbuild_id);
1657
1658                 if (asprintf(&kallsyms_allocated_filename,
1659                              "%s/.debug/[kernel.kallsyms]/%s",
1660                              getenv("HOME"), sbuild_id) == -1)
1661                         return -1;
1662
1663                 kallsyms_filename = kallsyms_allocated_filename;
1664
1665                 if (access(kallsyms_filename, F_OK)) {
1666                         free(kallsyms_allocated_filename);
1667                         return -1;
1668                 }
1669         } else {
1670                 /*
1671                  * Last resort, if we don't have a build-id and couldn't find
1672                  * any vmlinux file, try the running kernel kallsyms table.
1673                  */
1674                 kallsyms_filename = "/proc/kallsyms";
1675         }
1676
1677 do_kallsyms:
1678         err = dso__load_kallsyms(self, kallsyms_filename, map, session, filter);
1679         free(kallsyms_allocated_filename);
1680
1681 out_try_fixup:
1682         if (err > 0) {
1683 out_fixup:
1684                 if (kallsyms_filename != NULL)
1685                         dso__set_long_name(self, strdup("[kernel.kallsyms]"));
1686                 map__fixup_start(map);
1687                 map__fixup_end(map);
1688         }
1689
1690         return err;
1691 }
1692
1693 LIST_HEAD(dsos__user);
1694 LIST_HEAD(dsos__kernel);
1695 struct dso *vdso;
1696
1697 static void dsos__add(struct list_head *head, struct dso *dso)
1698 {
1699         list_add_tail(&dso->node, head);
1700 }
1701
1702 static struct dso *dsos__find(struct list_head *head, const char *name)
1703 {
1704         struct dso *pos;
1705
1706         list_for_each_entry(pos, head, node)
1707                 if (strcmp(pos->long_name, name) == 0)
1708                         return pos;
1709         return NULL;
1710 }
1711
1712 struct dso *__dsos__findnew(struct list_head *head, const char *name)
1713 {
1714         struct dso *dso = dsos__find(head, name);
1715
1716         if (!dso) {
1717                 dso = dso__new(name);
1718                 if (dso != NULL) {
1719                         dsos__add(head, dso);
1720                         dso__set_basename(dso);
1721                 }
1722         }
1723
1724         return dso;
1725 }
1726
1727 static void __dsos__fprintf(struct list_head *head, FILE *fp)
1728 {
1729         struct dso *pos;
1730
1731         list_for_each_entry(pos, head, node) {
1732                 int i;
1733                 for (i = 0; i < MAP__NR_TYPES; ++i)
1734                         dso__fprintf(pos, i, fp);
1735         }
1736 }
1737
1738 void dsos__fprintf(FILE *fp)
1739 {
1740         __dsos__fprintf(&dsos__kernel, fp);
1741         __dsos__fprintf(&dsos__user, fp);
1742 }
1743
1744 static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
1745                                       bool with_hits)
1746 {
1747         struct dso *pos;
1748         size_t ret = 0;
1749
1750         list_for_each_entry(pos, head, node) {
1751                 if (with_hits && !pos->hit)
1752                         continue;
1753                 ret += dso__fprintf_buildid(pos, fp);
1754                 ret += fprintf(fp, " %s\n", pos->long_name);
1755         }
1756         return ret;
1757 }
1758
1759 size_t dsos__fprintf_buildid(FILE *fp, bool with_hits)
1760 {
1761         return (__dsos__fprintf_buildid(&dsos__kernel, fp, with_hits) +
1762                 __dsos__fprintf_buildid(&dsos__user, fp, with_hits));
1763 }
1764
1765 static struct dso *dsos__create_kernel(const char *vmlinux)
1766 {
1767         struct dso *kernel = dso__new(vmlinux ?: "[kernel.kallsyms]");
1768
1769         if (kernel == NULL)
1770                 return NULL;
1771
1772         kernel->short_name = "[kernel]";
1773         kernel->kernel     = 1;
1774
1775         vdso = dso__new("[vdso]");
1776         if (vdso == NULL)
1777                 goto out_delete_kernel_dso;
1778         dso__set_loaded(vdso, MAP__FUNCTION);
1779
1780         if (sysfs__read_build_id("/sys/kernel/notes", kernel->build_id,
1781                                  sizeof(kernel->build_id)) == 0)
1782                 kernel->has_build_id = true;
1783
1784         dsos__add(&dsos__kernel, kernel);
1785         dsos__add(&dsos__user, vdso);
1786
1787         return kernel;
1788
1789 out_delete_kernel_dso:
1790         dso__delete(kernel);
1791         return NULL;
1792 }
1793
1794 int __map_groups__create_kernel_maps(struct map_groups *self,
1795                                      struct map *vmlinux_maps[MAP__NR_TYPES],
1796                                      struct dso *kernel)
1797 {
1798         enum map_type type;
1799
1800         for (type = 0; type < MAP__NR_TYPES; ++type) {
1801                 vmlinux_maps[type] = map__new2(0, kernel, type);
1802                 if (vmlinux_maps[type] == NULL)
1803                         return -1;
1804
1805                 vmlinux_maps[type]->map_ip =
1806                         vmlinux_maps[type]->unmap_ip = identity__map_ip;
1807                 map_groups__insert(self, vmlinux_maps[type]);
1808         }
1809
1810         return 0;
1811 }
1812
1813 static int map_groups__create_kernel_maps(struct map_groups *self,
1814                                           struct map *vmlinux_maps[MAP__NR_TYPES],
1815                                           const char *vmlinux)
1816 {
1817         struct dso *kernel = dsos__create_kernel(vmlinux);
1818
1819         if (kernel == NULL)
1820                 return -1;
1821
1822         return __map_groups__create_kernel_maps(self, vmlinux_maps, kernel);
1823 }
1824
1825 static void vmlinux_path__exit(void)
1826 {
1827         while (--vmlinux_path__nr_entries >= 0) {
1828                 free(vmlinux_path[vmlinux_path__nr_entries]);
1829                 vmlinux_path[vmlinux_path__nr_entries] = NULL;
1830         }
1831
1832         free(vmlinux_path);
1833         vmlinux_path = NULL;
1834 }
1835
1836 static int vmlinux_path__init(void)
1837 {
1838         struct utsname uts;
1839         char bf[PATH_MAX];
1840
1841         if (uname(&uts) < 0)
1842                 return -1;
1843
1844         vmlinux_path = malloc(sizeof(char *) * 5);
1845         if (vmlinux_path == NULL)
1846                 return -1;
1847
1848         vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
1849         if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1850                 goto out_fail;
1851         ++vmlinux_path__nr_entries;
1852         vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
1853         if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1854                 goto out_fail;
1855         ++vmlinux_path__nr_entries;
1856         snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
1857         vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1858         if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1859                 goto out_fail;
1860         ++vmlinux_path__nr_entries;
1861         snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
1862         vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1863         if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1864                 goto out_fail;
1865         ++vmlinux_path__nr_entries;
1866         snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
1867                  uts.release);
1868         vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1869         if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1870                 goto out_fail;
1871         ++vmlinux_path__nr_entries;
1872
1873         return 0;
1874
1875 out_fail:
1876         vmlinux_path__exit();
1877         return -1;
1878 }
1879
1880 static int setup_list(struct strlist **list, const char *list_str,
1881                       const char *list_name)
1882 {
1883         if (list_str == NULL)
1884                 return 0;
1885
1886         *list = strlist__new(true, list_str);
1887         if (!*list) {
1888                 pr_err("problems parsing %s list\n", list_name);
1889                 return -1;
1890         }
1891         return 0;
1892 }
1893
1894 int symbol__init(void)
1895 {
1896         elf_version(EV_CURRENT);
1897         if (symbol_conf.sort_by_name)
1898                 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
1899                                           sizeof(struct symbol));
1900
1901         if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
1902                 return -1;
1903
1904         if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
1905                 pr_err("'.' is the only non valid --field-separator argument\n");
1906                 return -1;
1907         }
1908
1909         if (setup_list(&symbol_conf.dso_list,
1910                        symbol_conf.dso_list_str, "dso") < 0)
1911                 return -1;
1912
1913         if (setup_list(&symbol_conf.comm_list,
1914                        symbol_conf.comm_list_str, "comm") < 0)
1915                 goto out_free_dso_list;
1916
1917         if (setup_list(&symbol_conf.sym_list,
1918                        symbol_conf.sym_list_str, "symbol") < 0)
1919                 goto out_free_comm_list;
1920
1921         return 0;
1922
1923 out_free_dso_list:
1924         strlist__delete(symbol_conf.dso_list);
1925 out_free_comm_list:
1926         strlist__delete(symbol_conf.comm_list);
1927         return -1;
1928 }
1929
1930 int perf_session__create_kernel_maps(struct perf_session *self)
1931 {
1932         if (map_groups__create_kernel_maps(&self->kmaps, self->vmlinux_maps,
1933                                            symbol_conf.vmlinux_name) < 0)
1934                 return -1;
1935
1936         if (symbol_conf.use_modules &&
1937             perf_session__create_module_maps(self) < 0)
1938                 pr_debug("Failed to load list of modules for session %s, "
1939                          "continuing...\n", self->filename);
1940         /*
1941          * Now that we have all the maps created, just set the ->end of them:
1942          */
1943         map_groups__fixup_end(&self->kmaps);
1944         return 0;
1945 }