e882968994700d870474c553b86d26a192345cae
[pandora-kernel.git] / tools / perf / util / symbol.c
1 #include "util.h"
2 #include "../perf.h"
3 #include "string.h"
4 #include "symbol.h"
5 #include "thread.h"
6
7 #include "debug.h"
8
9 #include <libelf.h>
10 #include <gelf.h>
11 #include <elf.h>
12 #include <sys/utsname.h>
13
14 const char *sym_hist_filter;
15
16 enum dso_origin {
17         DSO__ORIG_KERNEL = 0,
18         DSO__ORIG_JAVA_JIT,
19         DSO__ORIG_FEDORA,
20         DSO__ORIG_UBUNTU,
21         DSO__ORIG_BUILDID,
22         DSO__ORIG_DSO,
23         DSO__ORIG_KMODULE,
24         DSO__ORIG_NOT_FOUND,
25 };
26
27 static void dsos__add(struct dso *dso);
28 static struct dso *dsos__find(const char *name);
29
30 static struct symbol *symbol__new(u64 start, u64 len, const char *name,
31                                   unsigned int priv_size, int v)
32 {
33         size_t namelen = strlen(name) + 1;
34         struct symbol *self = calloc(1, priv_size + sizeof(*self) + namelen);
35
36         if (!self)
37                 return NULL;
38
39         if (v >= 2)
40                 printf("new symbol: %016Lx [%08lx]: %s, hist: %p\n",
41                         start, (unsigned long)len, name, self->hist);
42
43         self->hist = NULL;
44         self->hist_sum = 0;
45
46         if (sym_hist_filter && !strcmp(name, sym_hist_filter))
47                 self->hist = calloc(sizeof(u64), len);
48
49         if (priv_size) {
50                 memset(self, 0, priv_size);
51                 self = ((void *)self) + priv_size;
52         }
53         self->start = start;
54         self->end   = len ? start + len - 1 : start;
55         memcpy(self->name, name, namelen);
56
57         return self;
58 }
59
60 static void symbol__delete(struct symbol *self, unsigned int priv_size)
61 {
62         free(((void *)self) - priv_size);
63 }
64
65 static size_t symbol__fprintf(struct symbol *self, FILE *fp)
66 {
67         return fprintf(fp, " %llx-%llx %s\n",
68                        self->start, self->end, self->name);
69 }
70
71 struct dso *dso__new(const char *name, unsigned int sym_priv_size)
72 {
73         struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
74
75         if (self != NULL) {
76                 strcpy(self->name, name);
77                 self->long_name = self->name;
78                 self->short_name = self->name;
79                 self->syms = RB_ROOT;
80                 self->sym_priv_size = sym_priv_size;
81                 self->find_symbol = dso__find_symbol;
82                 self->slen_calculated = 0;
83                 self->origin = DSO__ORIG_NOT_FOUND;
84         }
85
86         return self;
87 }
88
89 static void dso__delete_symbols(struct dso *self)
90 {
91         struct symbol *pos;
92         struct rb_node *next = rb_first(&self->syms);
93
94         while (next) {
95                 pos = rb_entry(next, struct symbol, rb_node);
96                 next = rb_next(&pos->rb_node);
97                 rb_erase(&pos->rb_node, &self->syms);
98                 symbol__delete(pos, self->sym_priv_size);
99         }
100 }
101
102 void dso__delete(struct dso *self)
103 {
104         dso__delete_symbols(self);
105         if (self->long_name != self->name)
106                 free(self->long_name);
107         free(self);
108 }
109
110 static void dso__insert_symbol(struct dso *self, struct symbol *sym)
111 {
112         struct rb_node **p = &self->syms.rb_node;
113         struct rb_node *parent = NULL;
114         const u64 ip = sym->start;
115         struct symbol *s;
116
117         while (*p != NULL) {
118                 parent = *p;
119                 s = rb_entry(parent, struct symbol, rb_node);
120                 if (ip < s->start)
121                         p = &(*p)->rb_left;
122                 else
123                         p = &(*p)->rb_right;
124         }
125         rb_link_node(&sym->rb_node, parent, p);
126         rb_insert_color(&sym->rb_node, &self->syms);
127 }
128
129 struct symbol *dso__find_symbol(struct dso *self, u64 ip)
130 {
131         struct rb_node *n;
132
133         if (self == NULL)
134                 return NULL;
135
136         n = self->syms.rb_node;
137
138         while (n) {
139                 struct symbol *s = rb_entry(n, struct symbol, rb_node);
140
141                 if (ip < s->start)
142                         n = n->rb_left;
143                 else if (ip > s->end)
144                         n = n->rb_right;
145                 else
146                         return s;
147         }
148
149         return NULL;
150 }
151
152 size_t dso__fprintf(struct dso *self, FILE *fp)
153 {
154         size_t ret = fprintf(fp, "dso: %s\n", self->long_name);
155
156         struct rb_node *nd;
157         for (nd = rb_first(&self->syms); nd; nd = rb_next(nd)) {
158                 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
159                 ret += symbol__fprintf(pos, fp);
160         }
161
162         return ret;
163 }
164
165 static int dso__load_kallsyms(struct dso *self, struct map *map,
166                               symbol_filter_t filter, int v)
167 {
168         struct rb_node *nd, *prevnd;
169         char *line = NULL;
170         size_t n;
171         FILE *file = fopen("/proc/kallsyms", "r");
172         int count = 0;
173
174         if (file == NULL)
175                 goto out_failure;
176
177         while (!feof(file)) {
178                 u64 start;
179                 struct symbol *sym;
180                 int line_len, len;
181                 char symbol_type;
182
183                 line_len = getline(&line, &n, file);
184                 if (line_len < 0)
185                         break;
186
187                 if (!line)
188                         goto out_failure;
189
190                 line[--line_len] = '\0'; /* \n */
191
192                 len = hex2u64(line, &start);
193
194                 len++;
195                 if (len + 2 >= line_len)
196                         continue;
197
198                 symbol_type = toupper(line[len]);
199                 /*
200                  * We're interested only in code ('T'ext)
201                  */
202                 if (symbol_type != 'T' && symbol_type != 'W')
203                         continue;
204                 /*
205                  * Well fix up the end later, when we have all sorted.
206                  */
207                 sym = symbol__new(start, 0xdead, line + len + 2,
208                                   self->sym_priv_size, v);
209
210                 if (sym == NULL)
211                         goto out_delete_line;
212
213                 if (filter && filter(map, sym))
214                         symbol__delete(sym, self->sym_priv_size);
215                 else {
216                         dso__insert_symbol(self, sym);
217                         count++;
218                 }
219         }
220
221         /*
222          * Now that we have all sorted out, just set the ->end of all
223          * symbols
224          */
225         prevnd = rb_first(&self->syms);
226
227         if (prevnd == NULL)
228                 goto out_delete_line;
229
230         for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
231                 struct symbol *prev = rb_entry(prevnd, struct symbol, rb_node),
232                               *curr = rb_entry(nd, struct symbol, rb_node);
233
234                 prev->end = curr->start - 1;
235                 prevnd = nd;
236         }
237
238         free(line);
239         fclose(file);
240
241         return count;
242
243 out_delete_line:
244         free(line);
245 out_failure:
246         return -1;
247 }
248
249 static int dso__load_perf_map(struct dso *self, struct map *map,
250                               symbol_filter_t filter, int v)
251 {
252         char *line = NULL;
253         size_t n;
254         FILE *file;
255         int nr_syms = 0;
256
257         file = fopen(self->long_name, "r");
258         if (file == NULL)
259                 goto out_failure;
260
261         while (!feof(file)) {
262                 u64 start, size;
263                 struct symbol *sym;
264                 int line_len, len;
265
266                 line_len = getline(&line, &n, file);
267                 if (line_len < 0)
268                         break;
269
270                 if (!line)
271                         goto out_failure;
272
273                 line[--line_len] = '\0'; /* \n */
274
275                 len = hex2u64(line, &start);
276
277                 len++;
278                 if (len + 2 >= line_len)
279                         continue;
280
281                 len += hex2u64(line + len, &size);
282
283                 len++;
284                 if (len + 2 >= line_len)
285                         continue;
286
287                 sym = symbol__new(start, size, line + len,
288                                   self->sym_priv_size, v);
289
290                 if (sym == NULL)
291                         goto out_delete_line;
292
293                 if (filter && filter(map, sym))
294                         symbol__delete(sym, self->sym_priv_size);
295                 else {
296                         dso__insert_symbol(self, sym);
297                         nr_syms++;
298                 }
299         }
300
301         free(line);
302         fclose(file);
303
304         return nr_syms;
305
306 out_delete_line:
307         free(line);
308 out_failure:
309         return -1;
310 }
311
312 /**
313  * elf_symtab__for_each_symbol - iterate thru all the symbols
314  *
315  * @self: struct elf_symtab instance to iterate
316  * @idx: uint32_t idx
317  * @sym: GElf_Sym iterator
318  */
319 #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
320         for (idx = 0, gelf_getsym(syms, idx, &sym);\
321              idx < nr_syms; \
322              idx++, gelf_getsym(syms, idx, &sym))
323
324 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
325 {
326         return GELF_ST_TYPE(sym->st_info);
327 }
328
329 static inline int elf_sym__is_function(const GElf_Sym *sym)
330 {
331         return elf_sym__type(sym) == STT_FUNC &&
332                sym->st_name != 0 &&
333                sym->st_shndx != SHN_UNDEF &&
334                sym->st_size != 0;
335 }
336
337 static inline int elf_sym__is_label(const GElf_Sym *sym)
338 {
339         return elf_sym__type(sym) == STT_NOTYPE &&
340                 sym->st_name != 0 &&
341                 sym->st_shndx != SHN_UNDEF &&
342                 sym->st_shndx != SHN_ABS;
343 }
344
345 static inline const char *elf_sec__name(const GElf_Shdr *shdr,
346                                         const Elf_Data *secstrs)
347 {
348         return secstrs->d_buf + shdr->sh_name;
349 }
350
351 static inline int elf_sec__is_text(const GElf_Shdr *shdr,
352                                         const Elf_Data *secstrs)
353 {
354         return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
355 }
356
357 static inline const char *elf_sym__name(const GElf_Sym *sym,
358                                         const Elf_Data *symstrs)
359 {
360         return symstrs->d_buf + sym->st_name;
361 }
362
363 static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
364                                     GElf_Shdr *shp, const char *name,
365                                     size_t *idx)
366 {
367         Elf_Scn *sec = NULL;
368         size_t cnt = 1;
369
370         while ((sec = elf_nextscn(elf, sec)) != NULL) {
371                 char *str;
372
373                 gelf_getshdr(sec, shp);
374                 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
375                 if (!strcmp(name, str)) {
376                         if (idx)
377                                 *idx = cnt;
378                         break;
379                 }
380                 ++cnt;
381         }
382
383         return sec;
384 }
385
386 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
387         for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
388              idx < nr_entries; \
389              ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
390
391 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
392         for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
393              idx < nr_entries; \
394              ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
395
396 /*
397  * We need to check if we have a .dynsym, so that we can handle the
398  * .plt, synthesizing its symbols, that aren't on the symtabs (be it
399  * .dynsym or .symtab).
400  * And always look at the original dso, not at debuginfo packages, that
401  * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
402  */
403 static int dso__synthesize_plt_symbols(struct  dso *self, int v)
404 {
405         uint32_t nr_rel_entries, idx;
406         GElf_Sym sym;
407         u64 plt_offset;
408         GElf_Shdr shdr_plt;
409         struct symbol *f;
410         GElf_Shdr shdr_rel_plt, shdr_dynsym;
411         Elf_Data *reldata, *syms, *symstrs;
412         Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
413         size_t dynsym_idx;
414         GElf_Ehdr ehdr;
415         char sympltname[1024];
416         Elf *elf;
417         int nr = 0, symidx, fd, err = 0;
418
419         fd = open(self->long_name, O_RDONLY);
420         if (fd < 0)
421                 goto out;
422
423         elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
424         if (elf == NULL)
425                 goto out_close;
426
427         if (gelf_getehdr(elf, &ehdr) == NULL)
428                 goto out_elf_end;
429
430         scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
431                                          ".dynsym", &dynsym_idx);
432         if (scn_dynsym == NULL)
433                 goto out_elf_end;
434
435         scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
436                                           ".rela.plt", NULL);
437         if (scn_plt_rel == NULL) {
438                 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
439                                                   ".rel.plt", NULL);
440                 if (scn_plt_rel == NULL)
441                         goto out_elf_end;
442         }
443
444         err = -1;
445
446         if (shdr_rel_plt.sh_link != dynsym_idx)
447                 goto out_elf_end;
448
449         if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
450                 goto out_elf_end;
451
452         /*
453          * Fetch the relocation section to find the idxes to the GOT
454          * and the symbols in the .dynsym they refer to.
455          */
456         reldata = elf_getdata(scn_plt_rel, NULL);
457         if (reldata == NULL)
458                 goto out_elf_end;
459
460         syms = elf_getdata(scn_dynsym, NULL);
461         if (syms == NULL)
462                 goto out_elf_end;
463
464         scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
465         if (scn_symstrs == NULL)
466                 goto out_elf_end;
467
468         symstrs = elf_getdata(scn_symstrs, NULL);
469         if (symstrs == NULL)
470                 goto out_elf_end;
471
472         nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
473         plt_offset = shdr_plt.sh_offset;
474
475         if (shdr_rel_plt.sh_type == SHT_RELA) {
476                 GElf_Rela pos_mem, *pos;
477
478                 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
479                                            nr_rel_entries) {
480                         symidx = GELF_R_SYM(pos->r_info);
481                         plt_offset += shdr_plt.sh_entsize;
482                         gelf_getsym(syms, symidx, &sym);
483                         snprintf(sympltname, sizeof(sympltname),
484                                  "%s@plt", elf_sym__name(&sym, symstrs));
485
486                         f = symbol__new(plt_offset, shdr_plt.sh_entsize,
487                                         sympltname, self->sym_priv_size, v);
488                         if (!f)
489                                 goto out_elf_end;
490
491                         dso__insert_symbol(self, f);
492                         ++nr;
493                 }
494         } else if (shdr_rel_plt.sh_type == SHT_REL) {
495                 GElf_Rel pos_mem, *pos;
496                 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
497                                           nr_rel_entries) {
498                         symidx = GELF_R_SYM(pos->r_info);
499                         plt_offset += shdr_plt.sh_entsize;
500                         gelf_getsym(syms, symidx, &sym);
501                         snprintf(sympltname, sizeof(sympltname),
502                                  "%s@plt", elf_sym__name(&sym, symstrs));
503
504                         f = symbol__new(plt_offset, shdr_plt.sh_entsize,
505                                         sympltname, self->sym_priv_size, v);
506                         if (!f)
507                                 goto out_elf_end;
508
509                         dso__insert_symbol(self, f);
510                         ++nr;
511                 }
512         }
513
514         err = 0;
515 out_elf_end:
516         elf_end(elf);
517 out_close:
518         close(fd);
519
520         if (err == 0)
521                 return nr;
522 out:
523         fprintf(stderr, "%s: problems reading %s PLT info.\n",
524                 __func__, self->long_name);
525         return 0;
526 }
527
528 static int dso__load_sym(struct dso *self, struct map *map, const char *name,
529                          int fd, symbol_filter_t filter, int kernel,
530                          int kmodule, int v)
531 {
532         Elf_Data *symstrs, *secstrs;
533         uint32_t nr_syms;
534         int err = -1;
535         uint32_t idx;
536         GElf_Ehdr ehdr;
537         GElf_Shdr shdr;
538         Elf_Data *syms;
539         GElf_Sym sym;
540         Elf_Scn *sec, *sec_strndx;
541         Elf *elf;
542         int nr = 0;
543
544         elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
545         if (elf == NULL) {
546                 if (v)
547                         fprintf(stderr, "%s: cannot read %s ELF file.\n",
548                                 __func__, name);
549                 goto out_close;
550         }
551
552         if (gelf_getehdr(elf, &ehdr) == NULL) {
553                 if (v)
554                         fprintf(stderr, "%s: cannot get elf header.\n", __func__);
555                 goto out_elf_end;
556         }
557
558         sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
559         if (sec == NULL) {
560                 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
561                 if (sec == NULL)
562                         goto out_elf_end;
563         }
564
565         syms = elf_getdata(sec, NULL);
566         if (syms == NULL)
567                 goto out_elf_end;
568
569         sec = elf_getscn(elf, shdr.sh_link);
570         if (sec == NULL)
571                 goto out_elf_end;
572
573         symstrs = elf_getdata(sec, NULL);
574         if (symstrs == NULL)
575                 goto out_elf_end;
576
577         sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
578         if (sec_strndx == NULL)
579                 goto out_elf_end;
580
581         secstrs = elf_getdata(sec_strndx, NULL);
582         if (secstrs == NULL)
583                 goto out_elf_end;
584
585         nr_syms = shdr.sh_size / shdr.sh_entsize;
586
587         memset(&sym, 0, sizeof(sym));
588         if (!kernel) {
589                 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
590                                 elf_section_by_name(elf, &ehdr, &shdr,
591                                                      ".gnu.prelink_undo",
592                                                      NULL) != NULL);
593         } else self->adjust_symbols = 0;
594
595         elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
596                 struct symbol *f;
597                 const char *elf_name;
598                 char *demangled;
599                 int is_label = elf_sym__is_label(&sym);
600                 const char *section_name;
601
602                 if (!is_label && !elf_sym__is_function(&sym))
603                         continue;
604
605                 sec = elf_getscn(elf, sym.st_shndx);
606                 if (!sec)
607                         goto out_elf_end;
608
609                 gelf_getshdr(sec, &shdr);
610
611                 if (is_label && !elf_sec__is_text(&shdr, secstrs))
612                         continue;
613
614                 section_name = elf_sec__name(&shdr, secstrs);
615
616                 if (self->adjust_symbols) {
617                         if (v >= 2)
618                                 printf("adjusting symbol: st_value: %Lx sh_addr: %Lx sh_offset: %Lx\n",
619                                         (u64)sym.st_value, (u64)shdr.sh_addr, (u64)shdr.sh_offset);
620
621                         sym.st_value -= shdr.sh_addr - shdr.sh_offset;
622                 } else if (kmodule)
623                         sym.st_value += shdr.sh_offset;
624                 /*
625                  * We need to figure out if the object was created from C++ sources
626                  * DWARF DW_compile_unit has this, but we don't always have access
627                  * to it...
628                  */
629                 elf_name = elf_sym__name(&sym, symstrs);
630                 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
631                 if (demangled != NULL)
632                         elf_name = demangled;
633
634                 f = symbol__new(sym.st_value, sym.st_size, elf_name,
635                                 self->sym_priv_size, v);
636                 free(demangled);
637                 if (!f)
638                         goto out_elf_end;
639
640                 if (filter && filter(map, f))
641                         symbol__delete(f, self->sym_priv_size);
642                 else {
643                         dso__insert_symbol(self, f);
644                         nr++;
645                 }
646         }
647
648         err = nr;
649 out_elf_end:
650         elf_end(elf);
651 out_close:
652         return err;
653 }
654
655 #define BUILD_ID_SIZE 128
656
657 static char *dso__read_build_id(struct dso *self, int v)
658 {
659         int i;
660         GElf_Ehdr ehdr;
661         GElf_Shdr shdr;
662         Elf_Data *build_id_data;
663         Elf_Scn *sec;
664         char *build_id = NULL, *bid;
665         unsigned char *raw;
666         Elf *elf;
667         int fd = open(self->long_name, O_RDONLY);
668
669         if (fd < 0)
670                 goto out;
671
672         elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
673         if (elf == NULL) {
674                 if (v)
675                         fprintf(stderr, "%s: cannot read %s ELF file.\n",
676                                 __func__, self->long_name);
677                 goto out_close;
678         }
679
680         if (gelf_getehdr(elf, &ehdr) == NULL) {
681                 if (v)
682                         fprintf(stderr, "%s: cannot get elf header.\n", __func__);
683                 goto out_elf_end;
684         }
685
686         sec = elf_section_by_name(elf, &ehdr, &shdr, ".note.gnu.build-id", NULL);
687         if (sec == NULL)
688                 goto out_elf_end;
689
690         build_id_data = elf_getdata(sec, NULL);
691         if (build_id_data == NULL)
692                 goto out_elf_end;
693         build_id = malloc(BUILD_ID_SIZE);
694         if (build_id == NULL)
695                 goto out_elf_end;
696         raw = build_id_data->d_buf + 16;
697         bid = build_id;
698
699         for (i = 0; i < 20; ++i) {
700                 sprintf(bid, "%02x", *raw);
701                 ++raw;
702                 bid += 2;
703         }
704         if (v >= 2)
705                 printf("%s(%s): %s\n", __func__, self->long_name, build_id);
706 out_elf_end:
707         elf_end(elf);
708 out_close:
709         close(fd);
710 out:
711         return build_id;
712 }
713
714 char dso__symtab_origin(const struct dso *self)
715 {
716         static const char origin[] = {
717                 [DSO__ORIG_KERNEL] =   'k',
718                 [DSO__ORIG_JAVA_JIT] = 'j',
719                 [DSO__ORIG_FEDORA] =   'f',
720                 [DSO__ORIG_UBUNTU] =   'u',
721                 [DSO__ORIG_BUILDID] =  'b',
722                 [DSO__ORIG_DSO] =      'd',
723                 [DSO__ORIG_KMODULE] =  'K',
724         };
725
726         if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
727                 return '!';
728         return origin[self->origin];
729 }
730
731 int dso__load(struct dso *self, struct map *map, symbol_filter_t filter, int v)
732 {
733         int size = PATH_MAX;
734         char *name = malloc(size), *build_id = NULL;
735         int ret = -1;
736         int fd;
737
738         if (!name)
739                 return -1;
740
741         self->adjust_symbols = 0;
742
743         if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
744                 ret = dso__load_perf_map(self, map, filter, v);
745                 self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
746                                          DSO__ORIG_NOT_FOUND;
747                 return ret;
748         }
749
750         self->origin = DSO__ORIG_FEDORA - 1;
751
752 more:
753         do {
754                 self->origin++;
755                 switch (self->origin) {
756                 case DSO__ORIG_FEDORA:
757                         snprintf(name, size, "/usr/lib/debug%s.debug",
758                                  self->long_name);
759                         break;
760                 case DSO__ORIG_UBUNTU:
761                         snprintf(name, size, "/usr/lib/debug%s",
762                                  self->long_name);
763                         break;
764                 case DSO__ORIG_BUILDID:
765                         build_id = dso__read_build_id(self, v);
766                         if (build_id != NULL) {
767                                 snprintf(name, size,
768                                          "/usr/lib/debug/.build-id/%.2s/%s.debug",
769                                         build_id, build_id + 2);
770                                 free(build_id);
771                                 break;
772                         }
773                         self->origin++;
774                         /* Fall thru */
775                 case DSO__ORIG_DSO:
776                         snprintf(name, size, "%s", self->long_name);
777                         break;
778
779                 default:
780                         goto out;
781                 }
782
783                 fd = open(name, O_RDONLY);
784         } while (fd < 0);
785
786         ret = dso__load_sym(self, map, name, fd, filter, 0, 0, v);
787         close(fd);
788
789         /*
790          * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
791          */
792         if (!ret)
793                 goto more;
794
795         if (ret > 0) {
796                 int nr_plt = dso__synthesize_plt_symbols(self, v);
797                 if (nr_plt > 0)
798                         ret += nr_plt;
799         }
800 out:
801         free(name);
802         if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
803                 return 0;
804         return ret;
805 }
806
807 static struct rb_root kernel_maps;
808 struct map *kernel_map;
809
810 static void kernel_maps__insert(struct map *map)
811 {
812         maps__insert(&kernel_maps, map);
813 }
814
815 struct symbol *kernel_maps__find_symbol(u64 ip, struct map **mapp)
816 {
817         /*
818          * We can't have kernel_map in kernel_maps because it spans an address
819          * space that includes the modules. The right way to fix this is to
820          * create several maps, so that we don't have overlapping ranges with
821          * modules. For now lets look first on the kernel dso.
822          */
823         struct map *map = maps__find(&kernel_maps, ip);
824         struct symbol *sym;
825
826         if (map) {
827                 ip = map->map_ip(map, ip);
828                 sym = map->dso->find_symbol(map->dso, ip);
829         } else {
830                 map = kernel_map;
831                 sym = map->dso->find_symbol(map->dso, ip);
832         }
833
834         if (mapp)
835                 *mapp = map;
836
837         return sym;
838 }
839
840 struct map *kernel_maps__find_by_dso_name(const char *name)
841 {
842         struct rb_node *nd;
843
844         for (nd = rb_first(&kernel_maps); nd; nd = rb_next(nd)) {
845                 struct map *map = rb_entry(nd, struct map, rb_node);
846
847                 if (map->dso && strcmp(map->dso->name, name) == 0)
848                         return map;
849         }
850
851         return NULL;
852 }
853
854 static int dso__load_module_sym(struct dso *self, struct map *map,
855                                 symbol_filter_t filter, int v)
856 {
857         int err = 0, fd = open(self->long_name, O_RDONLY);
858
859         if (fd < 0) {
860                 if (v)
861                         fprintf(stderr, "%s: cannot open %s\n",
862                                 __func__, self->long_name);
863                 return err;
864         }
865
866         err = dso__load_sym(self, map, self->long_name, fd, filter, 0, 1, v);
867         close(fd);
868
869         return err;
870 }
871
872 static int dsos__load_modules_sym_dir(char *dirname,
873                                       symbol_filter_t filter, int v)
874 {
875         struct dirent *dent;
876         int nr_symbols = 0, err;
877         DIR *dir = opendir(dirname);
878
879         if (!dir) {
880                 if (v)
881                         fprintf(stderr, "%s: cannot open %s dir\n", __func__,
882                                 dirname);
883                 return -1;
884         }
885
886         while ((dent = readdir(dir)) != NULL) {
887                 char path[PATH_MAX];
888
889                 if (dent->d_type == DT_DIR) {
890                         if (!strcmp(dent->d_name, ".") ||
891                             !strcmp(dent->d_name, ".."))
892                                 continue;
893
894                         snprintf(path, sizeof(path), "%s/%s",
895                                  dirname, dent->d_name);
896                         err = dsos__load_modules_sym_dir(path, filter, v);
897                         if (err < 0)
898                                 goto failure;
899                 } else {
900                         char *dot = strrchr(dent->d_name, '.'),
901                              dso_name[PATH_MAX];
902                         struct map *map;
903                         struct rb_node *last;
904
905                         if (dot == NULL || strcmp(dot, ".ko"))
906                                 continue;
907                         snprintf(dso_name, sizeof(dso_name), "[%.*s]",
908                                  (int)(dot - dent->d_name), dent->d_name);
909
910                         map = kernel_maps__find_by_dso_name(dso_name);
911                         if (map == NULL)
912                                 continue;
913
914                         snprintf(path, sizeof(path), "%s/%s",
915                                  dirname, dent->d_name);
916
917                         map->dso->long_name = strdup(path);
918                         if (map->dso->long_name == NULL)
919                                 goto failure;
920
921                         err = dso__load_module_sym(map->dso, map, filter, v);
922                         if (err < 0)
923                                 goto failure;
924                         last = rb_last(&map->dso->syms);
925                         if (last) {
926                                 struct symbol *sym;
927                                 sym = rb_entry(last, struct symbol, rb_node);
928                                 map->end = map->start + sym->end;
929                         }
930                 }
931                 nr_symbols += err;
932         }
933
934         return nr_symbols;
935 failure:
936         closedir(dir);
937         return -1;
938 }
939
940 static int dsos__load_modules_sym(symbol_filter_t filter, int v)
941 {
942         struct utsname uts;
943         char modules_path[PATH_MAX];
944
945         if (uname(&uts) < 0)
946                 return -1;
947
948         snprintf(modules_path, sizeof(modules_path), "/lib/modules/%s/kernel",
949                  uts.release);
950
951         return dsos__load_modules_sym_dir(modules_path, filter, v);
952 }
953
954 /*
955  * Constructor variant for modules (where we know from /proc/modules where
956  * they are loaded) and for vmlinux, where only after we load all the
957  * symbols we'll know where it starts and ends.
958  */
959 static struct map *map__new2(u64 start, struct dso *dso)
960 {
961         struct map *self = malloc(sizeof(*self));
962
963         if (self != NULL) {
964                 self->start = start;
965                 /*
966                  * Will be filled after we load all the symbols
967                  */
968                 self->end = 0;
969
970                 self->pgoff = 0;
971                 self->dso = dso;
972                 self->map_ip = map__map_ip;
973                 RB_CLEAR_NODE(&self->rb_node);
974         }
975         return self;
976 }
977
978 int dsos__load_modules(unsigned int sym_priv_size,
979                        symbol_filter_t filter, int v)
980 {
981         char *line = NULL;
982         size_t n;
983         FILE *file = fopen("/proc/modules", "r");
984         struct map *map;
985
986         if (file == NULL)
987                 return -1;
988
989         while (!feof(file)) {
990                 char name[PATH_MAX];
991                 u64 start;
992                 struct dso *dso;
993                 char *sep;
994                 int line_len;
995
996                 line_len = getline(&line, &n, file);
997                 if (line_len < 0)
998                         break;
999
1000                 if (!line)
1001                         goto out_failure;
1002
1003                 line[--line_len] = '\0'; /* \n */
1004
1005                 sep = strrchr(line, 'x');
1006                 if (sep == NULL)
1007                         continue;
1008
1009                 hex2u64(sep + 1, &start);
1010
1011                 sep = strchr(line, ' ');
1012                 if (sep == NULL)
1013                         continue;
1014
1015                 *sep = '\0';
1016
1017                 snprintf(name, sizeof(name), "[%s]", line);
1018                 dso = dso__new(name, sym_priv_size);
1019
1020                 if (dso == NULL)
1021                         goto out_delete_line;
1022
1023                 map = map__new2(start, dso);
1024                 if (map == NULL) {
1025                         dso__delete(dso);
1026                         goto out_delete_line;
1027                 }
1028
1029                 dso->origin = DSO__ORIG_KMODULE;
1030                 kernel_maps__insert(map);
1031                 dsos__add(dso);
1032         }
1033
1034         free(line);
1035         fclose(file);
1036
1037         v = 1;
1038         return dsos__load_modules_sym(filter, v);
1039
1040 out_delete_line:
1041         free(line);
1042 out_failure:
1043         return -1;
1044 }
1045
1046 static int dso__load_vmlinux(struct dso *self, struct map *map,
1047                              const char *vmlinux,
1048                              symbol_filter_t filter, int v)
1049 {
1050         int err, fd = open(vmlinux, O_RDONLY);
1051
1052         if (fd < 0)
1053                 return -1;
1054
1055         err = dso__load_sym(self, map, self->long_name, fd, filter, 1, 0, v);
1056
1057         close(fd);
1058
1059         return err;
1060 }
1061
1062 int dsos__load_kernel(const char *vmlinux, unsigned int sym_priv_size,
1063                       symbol_filter_t filter, int v, int use_modules)
1064 {
1065         int err = -1;
1066         struct dso *dso = dso__new(vmlinux, sym_priv_size);
1067
1068         if (dso == NULL)
1069                 return -1;
1070
1071         dso->short_name = "[kernel]";
1072         kernel_map = map__new2(0, dso);
1073         if (kernel_map == NULL)
1074                 goto out_delete_dso;
1075
1076         kernel_map->map_ip = vdso__map_ip;
1077
1078         if (vmlinux) {
1079                 err = dso__load_vmlinux(dso, kernel_map, vmlinux, filter, v);
1080                 if (err > 0 && use_modules) {
1081                         int syms = dsos__load_modules(sym_priv_size, filter, v);
1082
1083                         if (syms < 0) {
1084                                 fprintf(stderr, "dsos__load_modules failed!\n");
1085                                 return syms;
1086                         }
1087                         err += syms;
1088                 }
1089         }
1090
1091         if (err <= 0)
1092                 err = dso__load_kallsyms(dso, kernel_map, filter, v);
1093
1094         if (err > 0) {
1095                 struct rb_node *node = rb_first(&dso->syms);
1096                 struct symbol *sym = rb_entry(node, struct symbol, rb_node);
1097
1098                 kernel_map->start = sym->start;
1099                 node = rb_last(&dso->syms);
1100                 sym = rb_entry(node, struct symbol, rb_node);
1101                 kernel_map->end = sym->end;
1102
1103                 dso->origin = DSO__ORIG_KERNEL;
1104                 /*
1105                  * XXX See kernel_maps__find_symbol comment
1106                  * kernel_maps__insert(kernel_map)
1107                  */
1108                 dsos__add(dso);
1109         }
1110
1111         return err;
1112
1113 out_delete_dso:
1114         dso__delete(dso);
1115         return -1;
1116 }
1117
1118 LIST_HEAD(dsos);
1119 struct dso      *vdso;
1120
1121 const char      *vmlinux_name = "vmlinux";
1122 int             modules;
1123
1124 static void dsos__add(struct dso *dso)
1125 {
1126         list_add_tail(&dso->node, &dsos);
1127 }
1128
1129 static struct dso *dsos__find(const char *name)
1130 {
1131         struct dso *pos;
1132
1133         list_for_each_entry(pos, &dsos, node)
1134                 if (strcmp(pos->name, name) == 0)
1135                         return pos;
1136         return NULL;
1137 }
1138
1139 struct dso *dsos__findnew(const char *name)
1140 {
1141         struct dso *dso = dsos__find(name);
1142         int nr;
1143
1144         if (dso)
1145                 return dso;
1146
1147         dso = dso__new(name, 0);
1148         if (!dso)
1149                 goto out_delete_dso;
1150
1151         nr = dso__load(dso, NULL, NULL, verbose);
1152         if (nr < 0) {
1153                 eprintf("Failed to open: %s\n", name);
1154                 goto out_delete_dso;
1155         }
1156         if (!nr)
1157                 eprintf("No symbols found in: %s, maybe install a debug package?\n", name);
1158
1159         dsos__add(dso);
1160
1161         return dso;
1162
1163 out_delete_dso:
1164         dso__delete(dso);
1165         return NULL;
1166 }
1167
1168 void dsos__fprintf(FILE *fp)
1169 {
1170         struct dso *pos;
1171
1172         list_for_each_entry(pos, &dsos, node)
1173                 dso__fprintf(pos, fp);
1174 }
1175
1176 int load_kernel(void)
1177 {
1178         if (dsos__load_kernel(vmlinux_name, 0, NULL, verbose, modules) <= 0)
1179                 return -1;
1180
1181         vdso = dso__new("[vdso]", 0);
1182         if (!vdso)
1183                 return -1;
1184
1185         dsos__add(vdso);
1186
1187         return 0;
1188 }
1189
1190 void symbol__init(void)
1191 {
1192         elf_version(EV_CURRENT);
1193 }