perf: Enable more compiler warnings
[linux-2.6/btrfs-unstable.git] / tools / perf / util / symbol.c
blob3159d47ae1cc7478ee52fc45b8c40e086ef7bfa1
1 #include "util.h"
2 #include "../perf.h"
3 #include "string.h"
4 #include "symbol.h"
6 #include <libelf.h>
7 #include <gelf.h>
8 #include <elf.h>
10 const char *sym_hist_filter;
12 enum dso_origin {
13 DSO__ORIG_KERNEL = 0,
14 DSO__ORIG_JAVA_JIT,
15 DSO__ORIG_FEDORA,
16 DSO__ORIG_UBUNTU,
17 DSO__ORIG_BUILDID,
18 DSO__ORIG_DSO,
19 DSO__ORIG_NOT_FOUND,
22 static struct symbol *symbol__new(u64 start, u64 len,
23 const char *name, unsigned int priv_size,
24 u64 obj_start, int v)
26 size_t namelen = strlen(name) + 1;
27 struct symbol *self = calloc(1, priv_size + sizeof(*self) + namelen);
29 if (!self)
30 return NULL;
32 if (v >= 2)
33 printf("new symbol: %016Lx [%08lx]: %s, hist: %p, obj_start: %p\n",
34 (u64)start, (unsigned long)len, name, self->hist, (void *)(unsigned long)obj_start);
36 self->obj_start= obj_start;
37 self->hist = NULL;
38 self->hist_sum = 0;
40 if (sym_hist_filter && !strcmp(name, sym_hist_filter))
41 self->hist = calloc(sizeof(u64), len);
43 if (priv_size) {
44 memset(self, 0, priv_size);
45 self = ((void *)self) + priv_size;
47 self->start = start;
48 self->end = len ? start + len - 1 : start;
49 memcpy(self->name, name, namelen);
51 return self;
54 static void symbol__delete(struct symbol *self, unsigned int priv_size)
56 free(((void *)self) - priv_size);
59 static size_t symbol__fprintf(struct symbol *self, FILE *fp)
61 if (!self->module)
62 return fprintf(fp, " %llx-%llx %s\n",
63 self->start, self->end, self->name);
64 else
65 return fprintf(fp, " %llx-%llx %s \t[%s]\n",
66 self->start, self->end, self->name, self->module->name);
69 struct dso *dso__new(const char *name, unsigned int sym_priv_size)
71 struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
73 if (self != NULL) {
74 strcpy(self->name, name);
75 self->syms = RB_ROOT;
76 self->sym_priv_size = sym_priv_size;
77 self->find_symbol = dso__find_symbol;
78 self->slen_calculated = 0;
79 self->origin = DSO__ORIG_NOT_FOUND;
82 return self;
85 static void dso__delete_symbols(struct dso *self)
87 struct symbol *pos;
88 struct rb_node *next = rb_first(&self->syms);
90 while (next) {
91 pos = rb_entry(next, struct symbol, rb_node);
92 next = rb_next(&pos->rb_node);
93 rb_erase(&pos->rb_node, &self->syms);
94 symbol__delete(pos, self->sym_priv_size);
98 void dso__delete(struct dso *self)
100 dso__delete_symbols(self);
101 free(self);
104 static void dso__insert_symbol(struct dso *self, struct symbol *sym)
106 struct rb_node **p = &self->syms.rb_node;
107 struct rb_node *parent = NULL;
108 const u64 ip = sym->start;
109 struct symbol *s;
111 while (*p != NULL) {
112 parent = *p;
113 s = rb_entry(parent, struct symbol, rb_node);
114 if (ip < s->start)
115 p = &(*p)->rb_left;
116 else
117 p = &(*p)->rb_right;
119 rb_link_node(&sym->rb_node, parent, p);
120 rb_insert_color(&sym->rb_node, &self->syms);
123 struct symbol *dso__find_symbol(struct dso *self, u64 ip)
125 struct rb_node *n;
127 if (self == NULL)
128 return NULL;
130 n = self->syms.rb_node;
132 while (n) {
133 struct symbol *s = rb_entry(n, struct symbol, rb_node);
135 if (ip < s->start)
136 n = n->rb_left;
137 else if (ip > s->end)
138 n = n->rb_right;
139 else
140 return s;
143 return NULL;
146 size_t dso__fprintf(struct dso *self, FILE *fp)
148 size_t ret = fprintf(fp, "dso: %s\n", self->name);
150 struct rb_node *nd;
151 for (nd = rb_first(&self->syms); nd; nd = rb_next(nd)) {
152 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
153 ret += symbol__fprintf(pos, fp);
156 return ret;
159 static int dso__load_kallsyms(struct dso *self, symbol_filter_t filter, int v)
161 struct rb_node *nd, *prevnd;
162 char *line = NULL;
163 size_t n;
164 FILE *file = fopen("/proc/kallsyms", "r");
165 int count = 0;
167 if (file == NULL)
168 goto out_failure;
170 while (!feof(file)) {
171 u64 start;
172 struct symbol *sym;
173 int line_len, len;
174 char symbol_type;
176 line_len = getline(&line, &n, file);
177 if (line_len < 0)
178 break;
180 if (!line)
181 goto out_failure;
183 line[--line_len] = '\0'; /* \n */
185 len = hex2u64(line, &start);
187 len++;
188 if (len + 2 >= line_len)
189 continue;
191 symbol_type = toupper(line[len]);
193 * We're interested only in code ('T'ext)
195 if (symbol_type != 'T' && symbol_type != 'W')
196 continue;
198 * Well fix up the end later, when we have all sorted.
200 sym = symbol__new(start, 0xdead, line + len + 2,
201 self->sym_priv_size, 0, v);
203 if (sym == NULL)
204 goto out_delete_line;
206 if (filter && filter(self, sym))
207 symbol__delete(sym, self->sym_priv_size);
208 else {
209 dso__insert_symbol(self, sym);
210 count++;
215 * Now that we have all sorted out, just set the ->end of all
216 * symbols
218 prevnd = rb_first(&self->syms);
220 if (prevnd == NULL)
221 goto out_delete_line;
223 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
224 struct symbol *prev = rb_entry(prevnd, struct symbol, rb_node),
225 *curr = rb_entry(nd, struct symbol, rb_node);
227 prev->end = curr->start - 1;
228 prevnd = nd;
231 free(line);
232 fclose(file);
234 return count;
236 out_delete_line:
237 free(line);
238 out_failure:
239 return -1;
242 static int dso__load_perf_map(struct dso *self, symbol_filter_t filter, int v)
244 char *line = NULL;
245 size_t n;
246 FILE *file;
247 int nr_syms = 0;
249 file = fopen(self->name, "r");
250 if (file == NULL)
251 goto out_failure;
253 while (!feof(file)) {
254 u64 start, size;
255 struct symbol *sym;
256 int line_len, len;
258 line_len = getline(&line, &n, file);
259 if (line_len < 0)
260 break;
262 if (!line)
263 goto out_failure;
265 line[--line_len] = '\0'; /* \n */
267 len = hex2u64(line, &start);
269 len++;
270 if (len + 2 >= line_len)
271 continue;
273 len += hex2u64(line + len, &size);
275 len++;
276 if (len + 2 >= line_len)
277 continue;
279 sym = symbol__new(start, size, line + len,
280 self->sym_priv_size, start, v);
282 if (sym == NULL)
283 goto out_delete_line;
285 if (filter && filter(self, sym))
286 symbol__delete(sym, self->sym_priv_size);
287 else {
288 dso__insert_symbol(self, sym);
289 nr_syms++;
293 free(line);
294 fclose(file);
296 return nr_syms;
298 out_delete_line:
299 free(line);
300 out_failure:
301 return -1;
305 * elf_symtab__for_each_symbol - iterate thru all the symbols
307 * @self: struct elf_symtab instance to iterate
308 * @idx: uint32_t idx
309 * @sym: GElf_Sym iterator
311 #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
312 for (idx = 0, gelf_getsym(syms, idx, &sym);\
313 idx < nr_syms; \
314 idx++, gelf_getsym(syms, idx, &sym))
316 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
318 return GELF_ST_TYPE(sym->st_info);
321 static inline int elf_sym__is_function(const GElf_Sym *sym)
323 return elf_sym__type(sym) == STT_FUNC &&
324 sym->st_name != 0 &&
325 sym->st_shndx != SHN_UNDEF &&
326 sym->st_size != 0;
329 static inline int elf_sym__is_label(const GElf_Sym *sym)
331 return elf_sym__type(sym) == STT_NOTYPE &&
332 sym->st_name != 0 &&
333 sym->st_shndx != SHN_UNDEF &&
334 sym->st_shndx != SHN_ABS;
337 static inline const char *elf_sec__name(const GElf_Shdr *shdr,
338 const Elf_Data *secstrs)
340 return secstrs->d_buf + shdr->sh_name;
343 static inline int elf_sec__is_text(const GElf_Shdr *shdr,
344 const Elf_Data *secstrs)
346 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
349 static inline const char *elf_sym__name(const GElf_Sym *sym,
350 const Elf_Data *symstrs)
352 return symstrs->d_buf + sym->st_name;
355 static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
356 GElf_Shdr *shp, const char *name,
357 size_t *idx)
359 Elf_Scn *sec = NULL;
360 size_t cnt = 1;
362 while ((sec = elf_nextscn(elf, sec)) != NULL) {
363 char *str;
365 gelf_getshdr(sec, shp);
366 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
367 if (!strcmp(name, str)) {
368 if (idx)
369 *idx = cnt;
370 break;
372 ++cnt;
375 return sec;
378 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
379 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
380 idx < nr_entries; \
381 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
383 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
384 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
385 idx < nr_entries; \
386 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
389 * We need to check if we have a .dynsym, so that we can handle the
390 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
391 * .dynsym or .symtab).
392 * And always look at the original dso, not at debuginfo packages, that
393 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
395 static int dso__synthesize_plt_symbols(struct dso *self, int v)
397 uint32_t nr_rel_entries, idx;
398 GElf_Sym sym;
399 u64 plt_offset;
400 GElf_Shdr shdr_plt;
401 struct symbol *f;
402 GElf_Shdr shdr_rel_plt, shdr_dynsym;
403 Elf_Data *reldata, *syms, *symstrs;
404 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
405 size_t dynsym_idx;
406 GElf_Ehdr ehdr;
407 char sympltname[1024];
408 Elf *elf;
409 int nr = 0, symidx, fd, err = 0;
411 fd = open(self->name, O_RDONLY);
412 if (fd < 0)
413 goto out;
415 elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
416 if (elf == NULL)
417 goto out_close;
419 if (gelf_getehdr(elf, &ehdr) == NULL)
420 goto out_elf_end;
422 scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
423 ".dynsym", &dynsym_idx);
424 if (scn_dynsym == NULL)
425 goto out_elf_end;
427 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
428 ".rela.plt", NULL);
429 if (scn_plt_rel == NULL) {
430 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
431 ".rel.plt", NULL);
432 if (scn_plt_rel == NULL)
433 goto out_elf_end;
436 err = -1;
438 if (shdr_rel_plt.sh_link != dynsym_idx)
439 goto out_elf_end;
441 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
442 goto out_elf_end;
445 * Fetch the relocation section to find the idxes to the GOT
446 * and the symbols in the .dynsym they refer to.
448 reldata = elf_getdata(scn_plt_rel, NULL);
449 if (reldata == NULL)
450 goto out_elf_end;
452 syms = elf_getdata(scn_dynsym, NULL);
453 if (syms == NULL)
454 goto out_elf_end;
456 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
457 if (scn_symstrs == NULL)
458 goto out_elf_end;
460 symstrs = elf_getdata(scn_symstrs, NULL);
461 if (symstrs == NULL)
462 goto out_elf_end;
464 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
465 plt_offset = shdr_plt.sh_offset;
467 if (shdr_rel_plt.sh_type == SHT_RELA) {
468 GElf_Rela pos_mem, *pos;
470 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
471 nr_rel_entries) {
472 symidx = GELF_R_SYM(pos->r_info);
473 plt_offset += shdr_plt.sh_entsize;
474 gelf_getsym(syms, symidx, &sym);
475 snprintf(sympltname, sizeof(sympltname),
476 "%s@plt", elf_sym__name(&sym, symstrs));
478 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
479 sympltname, self->sym_priv_size, 0, v);
480 if (!f)
481 goto out_elf_end;
483 dso__insert_symbol(self, f);
484 ++nr;
486 } else if (shdr_rel_plt.sh_type == SHT_REL) {
487 GElf_Rel pos_mem, *pos;
488 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
489 nr_rel_entries) {
490 symidx = GELF_R_SYM(pos->r_info);
491 plt_offset += shdr_plt.sh_entsize;
492 gelf_getsym(syms, symidx, &sym);
493 snprintf(sympltname, sizeof(sympltname),
494 "%s@plt", elf_sym__name(&sym, symstrs));
496 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
497 sympltname, self->sym_priv_size, 0, v);
498 if (!f)
499 goto out_elf_end;
501 dso__insert_symbol(self, f);
502 ++nr;
506 err = 0;
507 out_elf_end:
508 elf_end(elf);
509 out_close:
510 close(fd);
512 if (err == 0)
513 return nr;
514 out:
515 fprintf(stderr, "%s: problems reading %s PLT info.\n",
516 __func__, self->name);
517 return 0;
520 static int dso__load_sym(struct dso *self, int fd, const char *name,
521 symbol_filter_t filter, int v, struct module *mod)
523 Elf_Data *symstrs, *secstrs;
524 uint32_t nr_syms;
525 int err = -1;
526 uint32_t idx;
527 GElf_Ehdr ehdr;
528 GElf_Shdr shdr;
529 Elf_Data *syms;
530 GElf_Sym sym;
531 Elf_Scn *sec, *sec_strndx;
532 Elf *elf;
533 int nr = 0, kernel = !strcmp("[kernel]", self->name);
535 elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
536 if (elf == NULL) {
537 if (v)
538 fprintf(stderr, "%s: cannot read %s ELF file.\n",
539 __func__, name);
540 goto out_close;
543 if (gelf_getehdr(elf, &ehdr) == NULL) {
544 if (v)
545 fprintf(stderr, "%s: cannot get elf header.\n", __func__);
546 goto out_elf_end;
549 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
550 if (sec == NULL) {
551 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
552 if (sec == NULL)
553 goto out_elf_end;
556 syms = elf_getdata(sec, NULL);
557 if (syms == NULL)
558 goto out_elf_end;
560 sec = elf_getscn(elf, shdr.sh_link);
561 if (sec == NULL)
562 goto out_elf_end;
564 symstrs = elf_getdata(sec, NULL);
565 if (symstrs == NULL)
566 goto out_elf_end;
568 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
569 if (sec_strndx == NULL)
570 goto out_elf_end;
572 secstrs = elf_getdata(sec_strndx, NULL);
573 if (secstrs == NULL)
574 goto out_elf_end;
576 nr_syms = shdr.sh_size / shdr.sh_entsize;
578 memset(&sym, 0, sizeof(sym));
579 if (!kernel) {
580 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
581 elf_section_by_name(elf, &ehdr, &shdr,
582 ".gnu.prelink_undo",
583 NULL) != NULL);
584 } else self->adjust_symbols = 0;
586 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
587 struct symbol *f;
588 const char *elf_name;
589 char *demangled;
590 u64 obj_start;
591 struct section *section = NULL;
592 int is_label = elf_sym__is_label(&sym);
593 const char *section_name;
595 if (!is_label && !elf_sym__is_function(&sym))
596 continue;
598 sec = elf_getscn(elf, sym.st_shndx);
599 if (!sec)
600 goto out_elf_end;
602 gelf_getshdr(sec, &shdr);
604 if (is_label && !elf_sec__is_text(&shdr, secstrs))
605 continue;
607 section_name = elf_sec__name(&shdr, secstrs);
608 obj_start = sym.st_value;
610 if (self->adjust_symbols) {
611 if (v >= 2)
612 printf("adjusting symbol: st_value: %Lx sh_addr: %Lx sh_offset: %Lx\n",
613 (u64)sym.st_value, (u64)shdr.sh_addr, (u64)shdr.sh_offset);
615 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
618 if (mod) {
619 section = mod->sections->find_section(mod->sections, section_name);
620 if (section)
621 sym.st_value += section->vma;
622 else {
623 fprintf(stderr, "dso__load_sym() module %s lookup of %s failed\n",
624 mod->name, section_name);
625 goto out_elf_end;
629 * We need to figure out if the object was created from C++ sources
630 * DWARF DW_compile_unit has this, but we don't always have access
631 * to it...
633 elf_name = elf_sym__name(&sym, symstrs);
634 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
635 if (demangled != NULL)
636 elf_name = demangled;
638 f = symbol__new(sym.st_value, sym.st_size, elf_name,
639 self->sym_priv_size, obj_start, v);
640 free(demangled);
641 if (!f)
642 goto out_elf_end;
644 if (filter && filter(self, f))
645 symbol__delete(f, self->sym_priv_size);
646 else {
647 f->module = mod;
648 dso__insert_symbol(self, f);
649 nr++;
653 err = nr;
654 out_elf_end:
655 elf_end(elf);
656 out_close:
657 return err;
660 #define BUILD_ID_SIZE 128
662 static char *dso__read_build_id(struct dso *self, int v)
664 int i;
665 GElf_Ehdr ehdr;
666 GElf_Shdr shdr;
667 Elf_Data *build_id_data;
668 Elf_Scn *sec;
669 char *build_id = NULL, *bid;
670 unsigned char *raw;
671 Elf *elf;
672 int fd = open(self->name, O_RDONLY);
674 if (fd < 0)
675 goto out;
677 elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
678 if (elf == NULL) {
679 if (v)
680 fprintf(stderr, "%s: cannot read %s ELF file.\n",
681 __func__, self->name);
682 goto out_close;
685 if (gelf_getehdr(elf, &ehdr) == NULL) {
686 if (v)
687 fprintf(stderr, "%s: cannot get elf header.\n", __func__);
688 goto out_elf_end;
691 sec = elf_section_by_name(elf, &ehdr, &shdr, ".note.gnu.build-id", NULL);
692 if (sec == NULL)
693 goto out_elf_end;
695 build_id_data = elf_getdata(sec, NULL);
696 if (build_id_data == NULL)
697 goto out_elf_end;
698 build_id = malloc(BUILD_ID_SIZE);
699 if (build_id == NULL)
700 goto out_elf_end;
701 raw = build_id_data->d_buf + 16;
702 bid = build_id;
704 for (i = 0; i < 20; ++i) {
705 sprintf(bid, "%02x", *raw);
706 ++raw;
707 bid += 2;
709 if (v >= 2)
710 printf("%s(%s): %s\n", __func__, self->name, build_id);
711 out_elf_end:
712 elf_end(elf);
713 out_close:
714 close(fd);
715 out:
716 return build_id;
719 char dso__symtab_origin(const struct dso *self)
721 static const char origin[] = {
722 [DSO__ORIG_KERNEL] = 'k',
723 [DSO__ORIG_JAVA_JIT] = 'j',
724 [DSO__ORIG_FEDORA] = 'f',
725 [DSO__ORIG_UBUNTU] = 'u',
726 [DSO__ORIG_BUILDID] = 'b',
727 [DSO__ORIG_DSO] = 'd',
730 if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
731 return '!';
732 return origin[self->origin];
735 int dso__load(struct dso *self, symbol_filter_t filter, int v)
737 int size = PATH_MAX;
738 char *name = malloc(size), *build_id = NULL;
739 int ret = -1;
740 int fd;
742 if (!name)
743 return -1;
745 self->adjust_symbols = 0;
747 if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
748 ret = dso__load_perf_map(self, filter, v);
749 self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
750 DSO__ORIG_NOT_FOUND;
751 return ret;
754 self->origin = DSO__ORIG_FEDORA - 1;
756 more:
757 do {
758 self->origin++;
759 switch (self->origin) {
760 case DSO__ORIG_FEDORA:
761 snprintf(name, size, "/usr/lib/debug%s.debug", self->name);
762 break;
763 case DSO__ORIG_UBUNTU:
764 snprintf(name, size, "/usr/lib/debug%s", self->name);
765 break;
766 case DSO__ORIG_BUILDID:
767 build_id = dso__read_build_id(self, v);
768 if (build_id != NULL) {
769 snprintf(name, size,
770 "/usr/lib/debug/.build-id/%.2s/%s.debug",
771 build_id, build_id + 2);
772 free(build_id);
773 break;
775 self->origin++;
776 /* Fall thru */
777 case DSO__ORIG_DSO:
778 snprintf(name, size, "%s", self->name);
779 break;
781 default:
782 goto out;
785 fd = open(name, O_RDONLY);
786 } while (fd < 0);
788 ret = dso__load_sym(self, fd, name, filter, v, NULL);
789 close(fd);
792 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
794 if (!ret)
795 goto more;
797 if (ret > 0) {
798 int nr_plt = dso__synthesize_plt_symbols(self, v);
799 if (nr_plt > 0)
800 ret += nr_plt;
802 out:
803 free(name);
804 if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
805 return 0;
806 return ret;
809 static int dso__load_module(struct dso *self, struct mod_dso *mods, const char *name,
810 symbol_filter_t filter, int v)
812 struct module *mod = mod_dso__find_module(mods, name);
813 int err = 0, fd;
815 if (mod == NULL || !mod->active)
816 return err;
818 fd = open(mod->path, O_RDONLY);
820 if (fd < 0)
821 return err;
823 err = dso__load_sym(self, fd, name, filter, v, mod);
824 close(fd);
826 return err;
829 int dso__load_modules(struct dso *self, symbol_filter_t filter, int v)
831 struct mod_dso *mods = mod_dso__new_dso("modules");
832 struct module *pos;
833 struct rb_node *next;
834 int err;
836 err = mod_dso__load_modules(mods);
838 if (err <= 0)
839 return err;
842 * Iterate over modules, and load active symbols.
844 next = rb_first(&mods->mods);
845 while (next) {
846 pos = rb_entry(next, struct module, rb_node);
847 err = dso__load_module(self, mods, pos->name, filter, v);
849 if (err < 0)
850 break;
852 next = rb_next(&pos->rb_node);
855 if (err < 0) {
856 mod_dso__delete_modules(mods);
857 mod_dso__delete_self(mods);
860 return err;
863 static inline void dso__fill_symbol_holes(struct dso *self)
865 struct symbol *prev = NULL;
866 struct rb_node *nd;
868 for (nd = rb_last(&self->syms); nd; nd = rb_prev(nd)) {
869 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
871 if (prev) {
872 u64 hole = 0;
873 int alias = pos->start == prev->start;
875 if (!alias)
876 hole = prev->start - pos->end - 1;
878 if (hole || alias) {
879 if (alias)
880 pos->end = prev->end;
881 else if (hole)
882 pos->end = prev->start - 1;
885 prev = pos;
889 static int dso__load_vmlinux(struct dso *self, const char *vmlinux,
890 symbol_filter_t filter, int v)
892 int err, fd = open(vmlinux, O_RDONLY);
894 if (fd < 0)
895 return -1;
897 err = dso__load_sym(self, fd, vmlinux, filter, v, NULL);
899 if (err > 0)
900 dso__fill_symbol_holes(self);
902 close(fd);
904 return err;
907 int dso__load_kernel(struct dso *self, const char *vmlinux,
908 symbol_filter_t filter, int v, int use_modules)
910 int err = -1;
912 if (vmlinux) {
913 err = dso__load_vmlinux(self, vmlinux, filter, v);
914 if (err > 0 && use_modules)
915 err = dso__load_modules(self, filter, v);
918 if (err <= 0)
919 err = dso__load_kallsyms(self, filter, v);
921 if (err > 0)
922 self->origin = DSO__ORIG_KERNEL;
924 return err;
927 LIST_HEAD(dsos);
928 struct dso *kernel_dso;
929 struct dso *vdso;
930 struct dso *hypervisor_dso;
932 const char *vmlinux_name = "vmlinux";
933 int modules;
935 static void dsos__add(struct dso *dso)
937 list_add_tail(&dso->node, &dsos);
940 static struct dso *dsos__find(const char *name)
942 struct dso *pos;
944 list_for_each_entry(pos, &dsos, node)
945 if (strcmp(pos->name, name) == 0)
946 return pos;
947 return NULL;
950 struct dso *dsos__findnew(const char *name)
952 struct dso *dso = dsos__find(name);
953 int nr;
955 if (dso)
956 return dso;
958 dso = dso__new(name, 0);
959 if (!dso)
960 goto out_delete_dso;
962 nr = dso__load(dso, NULL, verbose);
963 if (nr < 0) {
964 eprintf("Failed to open: %s\n", name);
965 goto out_delete_dso;
967 if (!nr)
968 eprintf("No symbols found in: %s, maybe install a debug package?\n", name);
970 dsos__add(dso);
972 return dso;
974 out_delete_dso:
975 dso__delete(dso);
976 return NULL;
979 void dsos__fprintf(FILE *fp)
981 struct dso *pos;
983 list_for_each_entry(pos, &dsos, node)
984 dso__fprintf(pos, fp);
987 static struct symbol *vdso__find_symbol(struct dso *dso, u64 ip)
989 return dso__find_symbol(dso, ip);
992 int load_kernel(void)
994 int err;
996 kernel_dso = dso__new("[kernel]", 0);
997 if (!kernel_dso)
998 return -1;
1000 err = dso__load_kernel(kernel_dso, vmlinux_name, NULL, verbose, modules);
1001 if (err <= 0) {
1002 dso__delete(kernel_dso);
1003 kernel_dso = NULL;
1004 } else
1005 dsos__add(kernel_dso);
1007 vdso = dso__new("[vdso]", 0);
1008 if (!vdso)
1009 return -1;
1011 vdso->find_symbol = vdso__find_symbol;
1013 dsos__add(vdso);
1015 hypervisor_dso = dso__new("[hypervisor]", 0);
1016 if (!hypervisor_dso)
1017 return -1;
1018 dsos__add(hypervisor_dso);
1020 return err;
1024 void symbol__init(void)
1026 elf_version(EV_CURRENT);