perf symbols: Use the buildids if present
[linux-2.6.git] / tools / perf / util / symbol.c
bloba2e95ce1f223c26da40e21c730d6cc9192c789e6
1 #include "util.h"
2 #include "../perf.h"
3 #include "string.h"
4 #include "symbol.h"
5 #include "thread.h"
7 #include "debug.h"
9 #include <libelf.h>
10 #include <gelf.h>
11 #include <elf.h>
12 #include <sys/utsname.h>
14 enum dso_origin {
15 DSO__ORIG_KERNEL = 0,
16 DSO__ORIG_JAVA_JIT,
17 DSO__ORIG_FEDORA,
18 DSO__ORIG_UBUNTU,
19 DSO__ORIG_BUILDID,
20 DSO__ORIG_DSO,
21 DSO__ORIG_KMODULE,
22 DSO__ORIG_NOT_FOUND,
25 static void dsos__add(struct dso *dso);
26 static struct dso *dsos__find(const char *name);
27 static struct map *map__new2(u64 start, struct dso *dso);
28 static void kernel_maps__insert(struct map *map);
29 unsigned int symbol__priv_size;
31 static struct rb_root kernel_maps;
33 static void dso__fixup_sym_end(struct dso *self)
35 struct rb_node *nd, *prevnd = rb_first(&self->syms);
36 struct symbol *curr, *prev;
38 if (prevnd == NULL)
39 return;
41 curr = rb_entry(prevnd, struct symbol, rb_node);
43 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
44 prev = curr;
45 curr = rb_entry(nd, struct symbol, rb_node);
47 if (prev->end == prev->start)
48 prev->end = curr->start - 1;
51 /* Last entry */
52 if (curr->end == curr->start)
53 curr->end = roundup(curr->start, 4096);
56 static void kernel_maps__fixup_end(void)
58 struct map *prev, *curr;
59 struct rb_node *nd, *prevnd = rb_first(&kernel_maps);
61 if (prevnd == NULL)
62 return;
64 curr = rb_entry(prevnd, struct map, rb_node);
66 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
67 prev = curr;
68 curr = rb_entry(nd, struct map, rb_node);
69 prev->end = curr->start - 1;
72 nd = rb_last(&curr->dso->syms);
73 if (nd) {
74 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
75 curr->end = sym->end;
79 static struct symbol *symbol__new(u64 start, u64 len, const char *name)
81 size_t namelen = strlen(name) + 1;
82 struct symbol *self = calloc(1, (symbol__priv_size +
83 sizeof(*self) + namelen));
84 if (!self)
85 return NULL;
87 if (symbol__priv_size) {
88 memset(self, 0, symbol__priv_size);
89 self = ((void *)self) + symbol__priv_size;
91 self->start = start;
92 self->end = len ? start + len - 1 : start;
94 pr_debug3("%s: %s %#Lx-%#Lx\n", __func__, name, start, self->end);
96 memcpy(self->name, name, namelen);
98 return self;
101 static void symbol__delete(struct symbol *self)
103 free(((void *)self) - symbol__priv_size);
106 static size_t symbol__fprintf(struct symbol *self, FILE *fp)
108 return fprintf(fp, " %llx-%llx %s\n",
109 self->start, self->end, self->name);
112 struct dso *dso__new(const char *name)
114 struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
116 if (self != NULL) {
117 strcpy(self->name, name);
118 self->long_name = self->name;
119 self->short_name = self->name;
120 self->syms = RB_ROOT;
121 self->find_symbol = dso__find_symbol;
122 self->slen_calculated = 0;
123 self->origin = DSO__ORIG_NOT_FOUND;
124 self->loaded = 0;
125 self->has_build_id = 0;
128 return self;
131 static void dso__delete_symbols(struct dso *self)
133 struct symbol *pos;
134 struct rb_node *next = rb_first(&self->syms);
136 while (next) {
137 pos = rb_entry(next, struct symbol, rb_node);
138 next = rb_next(&pos->rb_node);
139 rb_erase(&pos->rb_node, &self->syms);
140 symbol__delete(pos);
144 void dso__delete(struct dso *self)
146 dso__delete_symbols(self);
147 if (self->long_name != self->name)
148 free(self->long_name);
149 free(self);
152 void dso__set_build_id(struct dso *self, void *build_id)
154 memcpy(self->build_id, build_id, sizeof(self->build_id));
155 self->has_build_id = 1;
158 static void dso__insert_symbol(struct dso *self, struct symbol *sym)
160 struct rb_node **p = &self->syms.rb_node;
161 struct rb_node *parent = NULL;
162 const u64 ip = sym->start;
163 struct symbol *s;
165 while (*p != NULL) {
166 parent = *p;
167 s = rb_entry(parent, struct symbol, rb_node);
168 if (ip < s->start)
169 p = &(*p)->rb_left;
170 else
171 p = &(*p)->rb_right;
173 rb_link_node(&sym->rb_node, parent, p);
174 rb_insert_color(&sym->rb_node, &self->syms);
177 struct symbol *dso__find_symbol(struct dso *self, u64 ip)
179 struct rb_node *n;
181 if (self == NULL)
182 return NULL;
184 n = self->syms.rb_node;
186 while (n) {
187 struct symbol *s = rb_entry(n, struct symbol, rb_node);
189 if (ip < s->start)
190 n = n->rb_left;
191 else if (ip > s->end)
192 n = n->rb_right;
193 else
194 return s;
197 return NULL;
200 int build_id__sprintf(u8 *self, int len, char *bf)
202 char *bid = bf;
203 u8 *raw = self;
204 int i;
206 for (i = 0; i < len; ++i) {
207 sprintf(bid, "%02x", *raw);
208 ++raw;
209 bid += 2;
212 return raw - self;
215 size_t dso__fprintf(struct dso *self, FILE *fp)
217 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
218 struct rb_node *nd;
219 size_t ret;
221 build_id__sprintf(self->build_id, sizeof(self->build_id), sbuild_id);
222 ret = fprintf(fp, "dso: %s (%s)\n", self->short_name, sbuild_id);
224 for (nd = rb_first(&self->syms); nd; nd = rb_next(nd)) {
225 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
226 ret += symbol__fprintf(pos, fp);
229 return ret;
233 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
234 * so that we can in the next step set the symbol ->end address and then
235 * call kernel_maps__split_kallsyms.
237 static int kernel_maps__load_all_kallsyms(void)
239 char *line = NULL;
240 size_t n;
241 FILE *file = fopen("/proc/kallsyms", "r");
243 if (file == NULL)
244 goto out_failure;
246 while (!feof(file)) {
247 u64 start;
248 struct symbol *sym;
249 int line_len, len;
250 char symbol_type;
251 char *symbol_name;
253 line_len = getline(&line, &n, file);
254 if (line_len < 0)
255 break;
257 if (!line)
258 goto out_failure;
260 line[--line_len] = '\0'; /* \n */
262 len = hex2u64(line, &start);
264 len++;
265 if (len + 2 >= line_len)
266 continue;
268 symbol_type = toupper(line[len]);
270 * We're interested only in code ('T'ext)
272 if (symbol_type != 'T' && symbol_type != 'W')
273 continue;
275 symbol_name = line + len + 2;
277 * Will fix up the end later, when we have all symbols sorted.
279 sym = symbol__new(start, 0, symbol_name);
281 if (sym == NULL)
282 goto out_delete_line;
284 dso__insert_symbol(kernel_map->dso, sym);
287 free(line);
288 fclose(file);
290 return 0;
292 out_delete_line:
293 free(line);
294 out_failure:
295 return -1;
299 * Split the symbols into maps, making sure there are no overlaps, i.e. the
300 * kernel range is broken in several maps, named [kernel].N, as we don't have
301 * the original ELF section names vmlinux have.
303 static int kernel_maps__split_kallsyms(symbol_filter_t filter, int use_modules)
305 struct map *map = kernel_map;
306 struct symbol *pos;
307 int count = 0;
308 struct rb_node *next = rb_first(&kernel_map->dso->syms);
309 int kernel_range = 0;
311 while (next) {
312 char *module;
314 pos = rb_entry(next, struct symbol, rb_node);
315 next = rb_next(&pos->rb_node);
317 module = strchr(pos->name, '\t');
318 if (module) {
319 if (!use_modules)
320 goto delete_symbol;
322 *module++ = '\0';
324 if (strcmp(map->dso->name, module)) {
325 map = kernel_maps__find_by_dso_name(module);
326 if (!map) {
327 pr_err("/proc/{kallsyms,modules} "
328 "inconsistency!\n");
329 return -1;
333 * So that we look just like we get from .ko files,
334 * i.e. not prelinked, relative to map->start.
336 pos->start = map->map_ip(map, pos->start);
337 pos->end = map->map_ip(map, pos->end);
338 } else if (map != kernel_map) {
339 char dso_name[PATH_MAX];
340 struct dso *dso;
342 snprintf(dso_name, sizeof(dso_name), "[kernel].%d",
343 kernel_range++);
345 dso = dso__new(dso_name);
346 if (dso == NULL)
347 return -1;
349 map = map__new2(pos->start, dso);
350 if (map == NULL) {
351 dso__delete(dso);
352 return -1;
355 map->map_ip = map->unmap_ip = identity__map_ip;
356 kernel_maps__insert(map);
357 ++kernel_range;
360 if (filter && filter(map, pos)) {
361 delete_symbol:
362 rb_erase(&pos->rb_node, &kernel_map->dso->syms);
363 symbol__delete(pos);
364 } else {
365 if (map != kernel_map) {
366 rb_erase(&pos->rb_node, &kernel_map->dso->syms);
367 dso__insert_symbol(map->dso, pos);
369 count++;
373 return count;
377 static int kernel_maps__load_kallsyms(symbol_filter_t filter, int use_modules)
379 if (kernel_maps__load_all_kallsyms())
380 return -1;
382 dso__fixup_sym_end(kernel_map->dso);
384 return kernel_maps__split_kallsyms(filter, use_modules);
387 static size_t kernel_maps__fprintf(FILE *fp)
389 size_t printed = fprintf(fp, "Kernel maps:\n");
390 struct rb_node *nd;
392 for (nd = rb_first(&kernel_maps); nd; nd = rb_next(nd)) {
393 struct map *pos = rb_entry(nd, struct map, rb_node);
395 printed += fprintf(fp, "Map:");
396 printed += map__fprintf(pos, fp);
397 if (verbose > 1) {
398 printed += dso__fprintf(pos->dso, fp);
399 printed += fprintf(fp, "--\n");
403 return printed + fprintf(fp, "END kernel maps\n");
406 static int dso__load_perf_map(struct dso *self, struct map *map,
407 symbol_filter_t filter)
409 char *line = NULL;
410 size_t n;
411 FILE *file;
412 int nr_syms = 0;
414 file = fopen(self->long_name, "r");
415 if (file == NULL)
416 goto out_failure;
418 while (!feof(file)) {
419 u64 start, size;
420 struct symbol *sym;
421 int line_len, len;
423 line_len = getline(&line, &n, file);
424 if (line_len < 0)
425 break;
427 if (!line)
428 goto out_failure;
430 line[--line_len] = '\0'; /* \n */
432 len = hex2u64(line, &start);
434 len++;
435 if (len + 2 >= line_len)
436 continue;
438 len += hex2u64(line + len, &size);
440 len++;
441 if (len + 2 >= line_len)
442 continue;
444 sym = symbol__new(start, size, line + len);
446 if (sym == NULL)
447 goto out_delete_line;
449 if (filter && filter(map, sym))
450 symbol__delete(sym);
451 else {
452 dso__insert_symbol(self, sym);
453 nr_syms++;
457 free(line);
458 fclose(file);
460 return nr_syms;
462 out_delete_line:
463 free(line);
464 out_failure:
465 return -1;
469 * elf_symtab__for_each_symbol - iterate thru all the symbols
471 * @self: struct elf_symtab instance to iterate
472 * @idx: uint32_t idx
473 * @sym: GElf_Sym iterator
475 #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
476 for (idx = 0, gelf_getsym(syms, idx, &sym);\
477 idx < nr_syms; \
478 idx++, gelf_getsym(syms, idx, &sym))
480 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
482 return GELF_ST_TYPE(sym->st_info);
485 static inline int elf_sym__is_function(const GElf_Sym *sym)
487 return elf_sym__type(sym) == STT_FUNC &&
488 sym->st_name != 0 &&
489 sym->st_shndx != SHN_UNDEF;
492 static inline int elf_sym__is_label(const GElf_Sym *sym)
494 return elf_sym__type(sym) == STT_NOTYPE &&
495 sym->st_name != 0 &&
496 sym->st_shndx != SHN_UNDEF &&
497 sym->st_shndx != SHN_ABS;
500 static inline const char *elf_sec__name(const GElf_Shdr *shdr,
501 const Elf_Data *secstrs)
503 return secstrs->d_buf + shdr->sh_name;
506 static inline int elf_sec__is_text(const GElf_Shdr *shdr,
507 const Elf_Data *secstrs)
509 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
512 static inline const char *elf_sym__name(const GElf_Sym *sym,
513 const Elf_Data *symstrs)
515 return symstrs->d_buf + sym->st_name;
518 static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
519 GElf_Shdr *shp, const char *name,
520 size_t *idx)
522 Elf_Scn *sec = NULL;
523 size_t cnt = 1;
525 while ((sec = elf_nextscn(elf, sec)) != NULL) {
526 char *str;
528 gelf_getshdr(sec, shp);
529 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
530 if (!strcmp(name, str)) {
531 if (idx)
532 *idx = cnt;
533 break;
535 ++cnt;
538 return sec;
541 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
542 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
543 idx < nr_entries; \
544 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
546 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
547 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
548 idx < nr_entries; \
549 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
552 * We need to check if we have a .dynsym, so that we can handle the
553 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
554 * .dynsym or .symtab).
555 * And always look at the original dso, not at debuginfo packages, that
556 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
558 static int dso__synthesize_plt_symbols(struct dso *self)
560 uint32_t nr_rel_entries, idx;
561 GElf_Sym sym;
562 u64 plt_offset;
563 GElf_Shdr shdr_plt;
564 struct symbol *f;
565 GElf_Shdr shdr_rel_plt, shdr_dynsym;
566 Elf_Data *reldata, *syms, *symstrs;
567 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
568 size_t dynsym_idx;
569 GElf_Ehdr ehdr;
570 char sympltname[1024];
571 Elf *elf;
572 int nr = 0, symidx, fd, err = 0;
574 fd = open(self->long_name, O_RDONLY);
575 if (fd < 0)
576 goto out;
578 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
579 if (elf == NULL)
580 goto out_close;
582 if (gelf_getehdr(elf, &ehdr) == NULL)
583 goto out_elf_end;
585 scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
586 ".dynsym", &dynsym_idx);
587 if (scn_dynsym == NULL)
588 goto out_elf_end;
590 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
591 ".rela.plt", NULL);
592 if (scn_plt_rel == NULL) {
593 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
594 ".rel.plt", NULL);
595 if (scn_plt_rel == NULL)
596 goto out_elf_end;
599 err = -1;
601 if (shdr_rel_plt.sh_link != dynsym_idx)
602 goto out_elf_end;
604 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
605 goto out_elf_end;
608 * Fetch the relocation section to find the idxes to the GOT
609 * and the symbols in the .dynsym they refer to.
611 reldata = elf_getdata(scn_plt_rel, NULL);
612 if (reldata == NULL)
613 goto out_elf_end;
615 syms = elf_getdata(scn_dynsym, NULL);
616 if (syms == NULL)
617 goto out_elf_end;
619 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
620 if (scn_symstrs == NULL)
621 goto out_elf_end;
623 symstrs = elf_getdata(scn_symstrs, NULL);
624 if (symstrs == NULL)
625 goto out_elf_end;
627 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
628 plt_offset = shdr_plt.sh_offset;
630 if (shdr_rel_plt.sh_type == SHT_RELA) {
631 GElf_Rela pos_mem, *pos;
633 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
634 nr_rel_entries) {
635 symidx = GELF_R_SYM(pos->r_info);
636 plt_offset += shdr_plt.sh_entsize;
637 gelf_getsym(syms, symidx, &sym);
638 snprintf(sympltname, sizeof(sympltname),
639 "%s@plt", elf_sym__name(&sym, symstrs));
641 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
642 sympltname);
643 if (!f)
644 goto out_elf_end;
646 dso__insert_symbol(self, f);
647 ++nr;
649 } else if (shdr_rel_plt.sh_type == SHT_REL) {
650 GElf_Rel pos_mem, *pos;
651 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
652 nr_rel_entries) {
653 symidx = GELF_R_SYM(pos->r_info);
654 plt_offset += shdr_plt.sh_entsize;
655 gelf_getsym(syms, symidx, &sym);
656 snprintf(sympltname, sizeof(sympltname),
657 "%s@plt", elf_sym__name(&sym, symstrs));
659 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
660 sympltname);
661 if (!f)
662 goto out_elf_end;
664 dso__insert_symbol(self, f);
665 ++nr;
669 err = 0;
670 out_elf_end:
671 elf_end(elf);
672 out_close:
673 close(fd);
675 if (err == 0)
676 return nr;
677 out:
678 pr_warning("%s: problems reading %s PLT info.\n",
679 __func__, self->long_name);
680 return 0;
683 static int dso__load_sym(struct dso *self, struct map *map, const char *name,
684 int fd, symbol_filter_t filter, int kernel,
685 int kmodule)
687 struct map *curr_map = map;
688 struct dso *curr_dso = self;
689 size_t dso_name_len = strlen(self->short_name);
690 Elf_Data *symstrs, *secstrs;
691 uint32_t nr_syms;
692 int err = -1;
693 uint32_t idx;
694 GElf_Ehdr ehdr;
695 GElf_Shdr shdr;
696 Elf_Data *syms;
697 GElf_Sym sym;
698 Elf_Scn *sec, *sec_strndx;
699 Elf *elf;
700 int nr = 0;
702 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
703 if (elf == NULL) {
704 pr_err("%s: cannot read %s ELF file.\n", __func__, name);
705 goto out_close;
708 if (gelf_getehdr(elf, &ehdr) == NULL) {
709 pr_err("%s: cannot get elf header.\n", __func__);
710 goto out_elf_end;
713 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
714 if (sec == NULL) {
715 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
716 if (sec == NULL)
717 goto out_elf_end;
720 syms = elf_getdata(sec, NULL);
721 if (syms == NULL)
722 goto out_elf_end;
724 sec = elf_getscn(elf, shdr.sh_link);
725 if (sec == NULL)
726 goto out_elf_end;
728 symstrs = elf_getdata(sec, NULL);
729 if (symstrs == NULL)
730 goto out_elf_end;
732 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
733 if (sec_strndx == NULL)
734 goto out_elf_end;
736 secstrs = elf_getdata(sec_strndx, NULL);
737 if (secstrs == NULL)
738 goto out_elf_end;
740 nr_syms = shdr.sh_size / shdr.sh_entsize;
742 memset(&sym, 0, sizeof(sym));
743 if (!kernel) {
744 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
745 elf_section_by_name(elf, &ehdr, &shdr,
746 ".gnu.prelink_undo",
747 NULL) != NULL);
748 } else self->adjust_symbols = 0;
750 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
751 struct symbol *f;
752 const char *elf_name;
753 char *demangled = NULL;
754 int is_label = elf_sym__is_label(&sym);
755 const char *section_name;
757 if (!is_label && !elf_sym__is_function(&sym))
758 continue;
760 sec = elf_getscn(elf, sym.st_shndx);
761 if (!sec)
762 goto out_elf_end;
764 gelf_getshdr(sec, &shdr);
766 if (is_label && !elf_sec__is_text(&shdr, secstrs))
767 continue;
769 elf_name = elf_sym__name(&sym, symstrs);
770 section_name = elf_sec__name(&shdr, secstrs);
772 if (kernel || kmodule) {
773 char dso_name[PATH_MAX];
775 if (strcmp(section_name,
776 curr_dso->short_name + dso_name_len) == 0)
777 goto new_symbol;
779 if (strcmp(section_name, ".text") == 0) {
780 curr_map = map;
781 curr_dso = self;
782 goto new_symbol;
785 snprintf(dso_name, sizeof(dso_name),
786 "%s%s", self->short_name, section_name);
788 curr_map = kernel_maps__find_by_dso_name(dso_name);
789 if (curr_map == NULL) {
790 u64 start = sym.st_value;
792 if (kmodule)
793 start += map->start + shdr.sh_offset;
795 curr_dso = dso__new(dso_name);
796 if (curr_dso == NULL)
797 goto out_elf_end;
798 curr_map = map__new2(start, curr_dso);
799 if (curr_map == NULL) {
800 dso__delete(curr_dso);
801 goto out_elf_end;
803 curr_map->map_ip = identity__map_ip;
804 curr_map->unmap_ip = identity__map_ip;
805 curr_dso->origin = DSO__ORIG_KERNEL;
806 kernel_maps__insert(curr_map);
807 dsos__add(curr_dso);
808 } else
809 curr_dso = curr_map->dso;
811 goto new_symbol;
814 if (curr_dso->adjust_symbols) {
815 pr_debug2("adjusting symbol: st_value: %Lx sh_addr: "
816 "%Lx sh_offset: %Lx\n", (u64)sym.st_value,
817 (u64)shdr.sh_addr, (u64)shdr.sh_offset);
818 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
821 * We need to figure out if the object was created from C++ sources
822 * DWARF DW_compile_unit has this, but we don't always have access
823 * to it...
825 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
826 if (demangled != NULL)
827 elf_name = demangled;
828 new_symbol:
829 f = symbol__new(sym.st_value, sym.st_size, elf_name);
830 free(demangled);
831 if (!f)
832 goto out_elf_end;
834 if (filter && filter(curr_map, f))
835 symbol__delete(f);
836 else {
837 dso__insert_symbol(curr_dso, f);
838 nr++;
843 * For misannotated, zeroed, ASM function sizes.
845 if (nr > 0)
846 dso__fixup_sym_end(self);
847 err = nr;
848 out_elf_end:
849 elf_end(elf);
850 out_close:
851 return err;
854 int filename__read_build_id(const char *filename, void *bf, size_t size)
856 int fd, err = -1;
857 GElf_Ehdr ehdr;
858 GElf_Shdr shdr;
859 Elf_Data *build_id_data;
860 Elf_Scn *sec;
861 Elf *elf;
863 if (size < BUILD_ID_SIZE)
864 goto out;
866 fd = open(filename, O_RDONLY);
867 if (fd < 0)
868 goto out;
870 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
871 if (elf == NULL) {
872 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
873 goto out_close;
876 if (gelf_getehdr(elf, &ehdr) == NULL) {
877 pr_err("%s: cannot get elf header.\n", __func__);
878 goto out_elf_end;
881 sec = elf_section_by_name(elf, &ehdr, &shdr,
882 ".note.gnu.build-id", NULL);
883 if (sec == NULL)
884 goto out_elf_end;
886 build_id_data = elf_getdata(sec, NULL);
887 if (build_id_data == NULL)
888 goto out_elf_end;
889 memcpy(bf, build_id_data->d_buf + 16, BUILD_ID_SIZE);
890 err = BUILD_ID_SIZE;
891 out_elf_end:
892 elf_end(elf);
893 out_close:
894 close(fd);
895 out:
896 return err;
899 static char *dso__read_build_id(struct dso *self)
901 int len;
902 char *build_id = NULL;
903 unsigned char rawbf[BUILD_ID_SIZE];
905 len = filename__read_build_id(self->long_name, rawbf, sizeof(rawbf));
906 if (len < 0)
907 goto out;
909 build_id = malloc(len * 2 + 1);
910 if (build_id == NULL)
911 goto out;
913 build_id__sprintf(rawbf, len, build_id);
914 out:
915 return build_id;
918 char dso__symtab_origin(const struct dso *self)
920 static const char origin[] = {
921 [DSO__ORIG_KERNEL] = 'k',
922 [DSO__ORIG_JAVA_JIT] = 'j',
923 [DSO__ORIG_FEDORA] = 'f',
924 [DSO__ORIG_UBUNTU] = 'u',
925 [DSO__ORIG_BUILDID] = 'b',
926 [DSO__ORIG_DSO] = 'd',
927 [DSO__ORIG_KMODULE] = 'K',
930 if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
931 return '!';
932 return origin[self->origin];
935 int dso__load(struct dso *self, struct map *map, symbol_filter_t filter)
937 int size = PATH_MAX;
938 char *name = malloc(size), *build_id = NULL;
939 int ret = -1;
940 int fd;
942 self->loaded = 1;
944 if (!name)
945 return -1;
947 self->adjust_symbols = 0;
949 if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
950 ret = dso__load_perf_map(self, map, filter);
951 self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
952 DSO__ORIG_NOT_FOUND;
953 return ret;
956 self->origin = DSO__ORIG_FEDORA - 1;
958 more:
959 do {
960 int berr = 0;
962 self->origin++;
963 switch (self->origin) {
964 case DSO__ORIG_FEDORA:
965 snprintf(name, size, "/usr/lib/debug%s.debug",
966 self->long_name);
967 break;
968 case DSO__ORIG_UBUNTU:
969 snprintf(name, size, "/usr/lib/debug%s",
970 self->long_name);
971 break;
972 case DSO__ORIG_BUILDID:
973 build_id = dso__read_build_id(self);
974 if (build_id != NULL) {
975 snprintf(name, size,
976 "/usr/lib/debug/.build-id/%.2s/%s.debug",
977 build_id, build_id + 2);
978 goto compare_build_id;
980 self->origin++;
981 /* Fall thru */
982 case DSO__ORIG_DSO:
983 snprintf(name, size, "%s", self->long_name);
984 break;
986 default:
987 goto out;
990 if (self->has_build_id) {
991 bool match;
992 build_id = malloc(BUILD_ID_SIZE);
993 if (build_id == NULL)
994 goto more;
995 berr = filename__read_build_id(name, build_id,
996 BUILD_ID_SIZE);
997 compare_build_id:
998 match = berr > 0 && memcmp(build_id, self->build_id,
999 sizeof(self->build_id)) == 0;
1000 free(build_id);
1001 build_id = NULL;
1002 if (!match)
1003 goto more;
1006 fd = open(name, O_RDONLY);
1007 } while (fd < 0);
1009 ret = dso__load_sym(self, map, name, fd, filter, 0, 0);
1010 close(fd);
1013 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
1015 if (!ret)
1016 goto more;
1018 if (ret > 0) {
1019 int nr_plt = dso__synthesize_plt_symbols(self);
1020 if (nr_plt > 0)
1021 ret += nr_plt;
1023 out:
1024 free(name);
1025 if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
1026 return 0;
1027 return ret;
1030 struct map *kernel_map;
1032 static void kernel_maps__insert(struct map *map)
1034 maps__insert(&kernel_maps, map);
1037 struct symbol *kernel_maps__find_symbol(u64 ip, struct map **mapp)
1039 struct map *map = maps__find(&kernel_maps, ip);
1041 if (mapp)
1042 *mapp = map;
1044 if (map) {
1045 ip = map->map_ip(map, ip);
1046 return map->dso->find_symbol(map->dso, ip);
1049 return NULL;
1052 struct map *kernel_maps__find_by_dso_name(const char *name)
1054 struct rb_node *nd;
1056 for (nd = rb_first(&kernel_maps); nd; nd = rb_next(nd)) {
1057 struct map *map = rb_entry(nd, struct map, rb_node);
1059 if (map->dso && strcmp(map->dso->name, name) == 0)
1060 return map;
1063 return NULL;
1066 static int dso__load_module_sym(struct dso *self, struct map *map,
1067 symbol_filter_t filter)
1069 int err = 0, fd = open(self->long_name, O_RDONLY);
1071 self->loaded = 1;
1073 if (fd < 0) {
1074 pr_err("%s: cannot open %s\n", __func__, self->long_name);
1075 return err;
1078 err = dso__load_sym(self, map, self->long_name, fd, filter, 0, 1);
1079 close(fd);
1081 return err;
1084 static int dsos__load_modules_sym_dir(char *dirname, symbol_filter_t filter)
1086 struct dirent *dent;
1087 int nr_symbols = 0, err;
1088 DIR *dir = opendir(dirname);
1090 if (!dir) {
1091 pr_err("%s: cannot open %s dir\n", __func__, dirname);
1092 return -1;
1095 while ((dent = readdir(dir)) != NULL) {
1096 char path[PATH_MAX];
1098 if (dent->d_type == DT_DIR) {
1099 if (!strcmp(dent->d_name, ".") ||
1100 !strcmp(dent->d_name, ".."))
1101 continue;
1103 snprintf(path, sizeof(path), "%s/%s",
1104 dirname, dent->d_name);
1105 err = dsos__load_modules_sym_dir(path, filter);
1106 if (err < 0)
1107 goto failure;
1108 } else {
1109 char *dot = strrchr(dent->d_name, '.'),
1110 dso_name[PATH_MAX];
1111 struct map *map;
1112 struct rb_node *last;
1114 if (dot == NULL || strcmp(dot, ".ko"))
1115 continue;
1116 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1117 (int)(dot - dent->d_name), dent->d_name);
1119 strxfrchar(dso_name, '-', '_');
1120 map = kernel_maps__find_by_dso_name(dso_name);
1121 if (map == NULL)
1122 continue;
1124 snprintf(path, sizeof(path), "%s/%s",
1125 dirname, dent->d_name);
1127 map->dso->long_name = strdup(path);
1128 if (map->dso->long_name == NULL)
1129 goto failure;
1131 err = dso__load_module_sym(map->dso, map, filter);
1132 if (err < 0)
1133 goto failure;
1134 last = rb_last(&map->dso->syms);
1135 if (last) {
1136 struct symbol *sym;
1138 * We do this here as well, even having the
1139 * symbol size found in the symtab because
1140 * misannotated ASM symbols may have the size
1141 * set to zero.
1143 dso__fixup_sym_end(map->dso);
1145 sym = rb_entry(last, struct symbol, rb_node);
1146 map->end = map->start + sym->end;
1149 nr_symbols += err;
1152 return nr_symbols;
1153 failure:
1154 closedir(dir);
1155 return -1;
1158 static int dsos__load_modules_sym(symbol_filter_t filter)
1160 struct utsname uts;
1161 char modules_path[PATH_MAX];
1163 if (uname(&uts) < 0)
1164 return -1;
1166 snprintf(modules_path, sizeof(modules_path), "/lib/modules/%s/kernel",
1167 uts.release);
1169 return dsos__load_modules_sym_dir(modules_path, filter);
1173 * Constructor variant for modules (where we know from /proc/modules where
1174 * they are loaded) and for vmlinux, where only after we load all the
1175 * symbols we'll know where it starts and ends.
1177 static struct map *map__new2(u64 start, struct dso *dso)
1179 struct map *self = malloc(sizeof(*self));
1181 if (self != NULL) {
1183 * ->end will be filled after we load all the symbols
1185 map__init(self, start, 0, 0, dso);
1188 return self;
1191 static int dsos__load_modules(void)
1193 char *line = NULL;
1194 size_t n;
1195 FILE *file = fopen("/proc/modules", "r");
1196 struct map *map;
1198 if (file == NULL)
1199 return -1;
1201 while (!feof(file)) {
1202 char name[PATH_MAX];
1203 u64 start;
1204 struct dso *dso;
1205 char *sep;
1206 int line_len;
1208 line_len = getline(&line, &n, file);
1209 if (line_len < 0)
1210 break;
1212 if (!line)
1213 goto out_failure;
1215 line[--line_len] = '\0'; /* \n */
1217 sep = strrchr(line, 'x');
1218 if (sep == NULL)
1219 continue;
1221 hex2u64(sep + 1, &start);
1223 sep = strchr(line, ' ');
1224 if (sep == NULL)
1225 continue;
1227 *sep = '\0';
1229 snprintf(name, sizeof(name), "[%s]", line);
1230 dso = dso__new(name);
1232 if (dso == NULL)
1233 goto out_delete_line;
1235 map = map__new2(start, dso);
1236 if (map == NULL) {
1237 dso__delete(dso);
1238 goto out_delete_line;
1241 dso->origin = DSO__ORIG_KMODULE;
1242 kernel_maps__insert(map);
1243 dsos__add(dso);
1246 free(line);
1247 fclose(file);
1249 return 0;
1251 out_delete_line:
1252 free(line);
1253 out_failure:
1254 return -1;
1257 static int dso__load_vmlinux(struct dso *self, struct map *map,
1258 const char *vmlinux, symbol_filter_t filter)
1260 int err, fd = open(vmlinux, O_RDONLY);
1262 self->loaded = 1;
1264 if (fd < 0)
1265 return -1;
1267 err = dso__load_sym(self, map, self->long_name, fd, filter, 1, 0);
1269 close(fd);
1271 return err;
1274 int dsos__load_kernel(const char *vmlinux, symbol_filter_t filter,
1275 int use_modules)
1277 int err = -1;
1278 struct dso *dso = dso__new(vmlinux);
1280 if (dso == NULL)
1281 return -1;
1283 dso->short_name = "[kernel]";
1284 kernel_map = map__new2(0, dso);
1285 if (kernel_map == NULL)
1286 goto out_delete_dso;
1288 kernel_map->map_ip = kernel_map->unmap_ip = identity__map_ip;
1290 if (use_modules && dsos__load_modules() < 0) {
1291 pr_warning("Failed to load list of modules in use! "
1292 "Continuing...\n");
1293 use_modules = 0;
1296 if (vmlinux) {
1297 err = dso__load_vmlinux(dso, kernel_map, vmlinux, filter);
1298 if (err > 0 && use_modules) {
1299 int syms = dsos__load_modules_sym(filter);
1301 if (syms < 0)
1302 pr_warning("Failed to read module symbols!"
1303 " Continuing...\n");
1304 else
1305 err += syms;
1309 if (err <= 0)
1310 err = kernel_maps__load_kallsyms(filter, use_modules);
1312 if (err > 0) {
1313 struct rb_node *node = rb_first(&dso->syms);
1314 struct symbol *sym = rb_entry(node, struct symbol, rb_node);
1316 kernel_map->start = sym->start;
1317 node = rb_last(&dso->syms);
1318 sym = rb_entry(node, struct symbol, rb_node);
1319 kernel_map->end = sym->end;
1321 dso->origin = DSO__ORIG_KERNEL;
1322 kernel_maps__insert(kernel_map);
1324 * Now that we have all sorted out, just set the ->end of all
1325 * maps:
1327 kernel_maps__fixup_end();
1328 dsos__add(dso);
1330 if (verbose)
1331 kernel_maps__fprintf(stderr);
1334 return err;
1336 out_delete_dso:
1337 dso__delete(dso);
1338 return -1;
1341 LIST_HEAD(dsos);
1342 struct dso *vdso;
1344 const char *vmlinux_name = "vmlinux";
1345 int modules;
1347 static void dsos__add(struct dso *dso)
1349 list_add_tail(&dso->node, &dsos);
1352 static struct dso *dsos__find(const char *name)
1354 struct dso *pos;
1356 list_for_each_entry(pos, &dsos, node)
1357 if (strcmp(pos->name, name) == 0)
1358 return pos;
1359 return NULL;
1362 struct dso *dsos__findnew(const char *name)
1364 struct dso *dso = dsos__find(name);
1366 if (!dso) {
1367 dso = dso__new(name);
1368 if (dso != NULL)
1369 dsos__add(dso);
1372 return dso;
1375 void dsos__fprintf(FILE *fp)
1377 struct dso *pos;
1379 list_for_each_entry(pos, &dsos, node)
1380 dso__fprintf(pos, fp);
1383 int load_kernel(symbol_filter_t filter)
1385 if (dsos__load_kernel(vmlinux_name, filter, modules) <= 0)
1386 return -1;
1388 vdso = dso__new("[vdso]");
1389 if (!vdso)
1390 return -1;
1392 dsos__add(vdso);
1394 return 0;
1397 void symbol__init(unsigned int priv_size)
1399 elf_version(EV_CURRENT);
1400 symbol__priv_size = priv_size;