12 #include <sys/utsname.h>
14 const char *sym_hist_filter;
27 static void dsos__add(struct dso *dso);
28 static struct dso *dsos__find(const char *name);
30 static struct symbol *symbol__new(u64 start, u64 len, const char *name,
31 unsigned int priv_size, int v)
33 size_t namelen = strlen(name) + 1;
34 struct symbol *self = calloc(1, priv_size + sizeof(*self) + namelen);
40 printf("new symbol: %016Lx [%08lx]: %s, hist: %p\n",
41 start, (unsigned long)len, name, self->hist);
46 if (sym_hist_filter && !strcmp(name, sym_hist_filter))
47 self->hist = calloc(sizeof(u64), len);
50 memset(self, 0, priv_size);
51 self = ((void *)self) + priv_size;
54 self->end = len ? start + len - 1 : start;
55 memcpy(self->name, name, namelen);
60 static void symbol__delete(struct symbol *self, unsigned int priv_size)
62 free(((void *)self) - priv_size);
65 static size_t symbol__fprintf(struct symbol *self, FILE *fp)
67 return fprintf(fp, " %llx-%llx %s\n",
68 self->start, self->end, self->name);
71 struct dso *dso__new(const char *name, unsigned int sym_priv_size)
73 struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
76 strcpy(self->name, name);
77 self->long_name = self->name;
78 self->short_name = self->name;
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;
89 static void dso__delete_symbols(struct dso *self)
92 struct rb_node *next = rb_first(&self->syms);
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);
102 void dso__delete(struct dso *self)
104 dso__delete_symbols(self);
105 if (self->long_name != self->name)
106 free(self->long_name);
110 static void dso__insert_symbol(struct dso *self, struct symbol *sym)
112 struct rb_node **p = &self->syms.rb_node;
113 struct rb_node *parent = NULL;
114 const u64 ip = sym->start;
119 s = rb_entry(parent, struct symbol, rb_node);
125 rb_link_node(&sym->rb_node, parent, p);
126 rb_insert_color(&sym->rb_node, &self->syms);
129 struct symbol *dso__find_symbol(struct dso *self, u64 ip)
136 n = self->syms.rb_node;
139 struct symbol *s = rb_entry(n, struct symbol, rb_node);
143 else if (ip > s->end)
152 size_t dso__fprintf(struct dso *self, FILE *fp)
154 size_t ret = fprintf(fp, "dso: %s\n", self->long_name);
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);
165 static int dso__load_kallsyms(struct dso *self, struct map *map,
166 symbol_filter_t filter, int v)
168 struct rb_node *nd, *prevnd;
171 FILE *file = fopen("/proc/kallsyms", "r");
177 while (!feof(file)) {
183 line_len = getline(&line, &n, file);
190 line[--line_len] = '\0'; /* \n */
192 len = hex2u64(line, &start);
195 if (len + 2 >= line_len)
198 symbol_type = toupper(line[len]);
200 * We're interested only in code ('T'ext)
202 if (symbol_type != 'T' && symbol_type != 'W')
205 * Well fix up the end later, when we have all sorted.
207 sym = symbol__new(start, 0xdead, line + len + 2,
208 self->sym_priv_size, v);
211 goto out_delete_line;
213 if (filter && filter(map, sym))
214 symbol__delete(sym, self->sym_priv_size);
216 dso__insert_symbol(self, sym);
222 * Now that we have all sorted out, just set the ->end of all
225 prevnd = rb_first(&self->syms);
228 goto out_delete_line;
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);
234 prev->end = curr->start - 1;
249 static int dso__load_perf_map(struct dso *self, struct map *map,
250 symbol_filter_t filter, int v)
257 file = fopen(self->long_name, "r");
261 while (!feof(file)) {
266 line_len = getline(&line, &n, file);
273 line[--line_len] = '\0'; /* \n */
275 len = hex2u64(line, &start);
278 if (len + 2 >= line_len)
281 len += hex2u64(line + len, &size);
284 if (len + 2 >= line_len)
287 sym = symbol__new(start, size, line + len,
288 self->sym_priv_size, v);
291 goto out_delete_line;
293 if (filter && filter(map, sym))
294 symbol__delete(sym, self->sym_priv_size);
296 dso__insert_symbol(self, sym);
313 * elf_symtab__for_each_symbol - iterate thru all the symbols
315 * @self: struct elf_symtab instance to iterate
317 * @sym: GElf_Sym iterator
319 #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
320 for (idx = 0, gelf_getsym(syms, idx, &sym);\
322 idx++, gelf_getsym(syms, idx, &sym))
324 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
326 return GELF_ST_TYPE(sym->st_info);
329 static inline int elf_sym__is_function(const GElf_Sym *sym)
331 return elf_sym__type(sym) == STT_FUNC &&
333 sym->st_shndx != SHN_UNDEF &&
337 static inline int elf_sym__is_label(const GElf_Sym *sym)
339 return elf_sym__type(sym) == STT_NOTYPE &&
341 sym->st_shndx != SHN_UNDEF &&
342 sym->st_shndx != SHN_ABS;
345 static inline const char *elf_sec__name(const GElf_Shdr *shdr,
346 const Elf_Data *secstrs)
348 return secstrs->d_buf + shdr->sh_name;
351 static inline int elf_sec__is_text(const GElf_Shdr *shdr,
352 const Elf_Data *secstrs)
354 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
357 static inline const char *elf_sym__name(const GElf_Sym *sym,
358 const Elf_Data *symstrs)
360 return symstrs->d_buf + sym->st_name;
363 static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
364 GElf_Shdr *shp, const char *name,
370 while ((sec = elf_nextscn(elf, sec)) != NULL) {
373 gelf_getshdr(sec, shp);
374 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
375 if (!strcmp(name, str)) {
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); \
389 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
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); \
394 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
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).
403 static int dso__synthesize_plt_symbols(struct dso *self, int v)
405 uint32_t nr_rel_entries, idx;
410 GElf_Shdr shdr_rel_plt, shdr_dynsym;
411 Elf_Data *reldata, *syms, *symstrs;
412 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
415 char sympltname[1024];
417 int nr = 0, symidx, fd, err = 0;
419 fd = open(self->long_name, O_RDONLY);
423 elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
427 if (gelf_getehdr(elf, &ehdr) == NULL)
430 scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
431 ".dynsym", &dynsym_idx);
432 if (scn_dynsym == NULL)
435 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
437 if (scn_plt_rel == NULL) {
438 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
440 if (scn_plt_rel == NULL)
446 if (shdr_rel_plt.sh_link != dynsym_idx)
449 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
453 * Fetch the relocation section to find the idxes to the GOT
454 * and the symbols in the .dynsym they refer to.
456 reldata = elf_getdata(scn_plt_rel, NULL);
460 syms = elf_getdata(scn_dynsym, NULL);
464 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
465 if (scn_symstrs == NULL)
468 symstrs = elf_getdata(scn_symstrs, NULL);
472 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
473 plt_offset = shdr_plt.sh_offset;
475 if (shdr_rel_plt.sh_type == SHT_RELA) {
476 GElf_Rela pos_mem, *pos;
478 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
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));
486 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
487 sympltname, self->sym_priv_size, v);
491 dso__insert_symbol(self, f);
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,
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));
504 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
505 sympltname, self->sym_priv_size, v);
509 dso__insert_symbol(self, f);
523 fprintf(stderr, "%s: problems reading %s PLT info.\n",
524 __func__, self->long_name);
528 static int dso__load_sym(struct dso *self, struct map *map, const char *name,
529 int fd, symbol_filter_t filter, int kernel,
532 Elf_Data *symstrs, *secstrs;
540 Elf_Scn *sec, *sec_strndx;
544 elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
547 fprintf(stderr, "%s: cannot read %s ELF file.\n",
552 if (gelf_getehdr(elf, &ehdr) == NULL) {
554 fprintf(stderr, "%s: cannot get elf header.\n", __func__);
558 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
560 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
565 syms = elf_getdata(sec, NULL);
569 sec = elf_getscn(elf, shdr.sh_link);
573 symstrs = elf_getdata(sec, NULL);
577 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
578 if (sec_strndx == NULL)
581 secstrs = elf_getdata(sec_strndx, NULL);
585 nr_syms = shdr.sh_size / shdr.sh_entsize;
587 memset(&sym, 0, sizeof(sym));
589 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
590 elf_section_by_name(elf, &ehdr, &shdr,
593 } else self->adjust_symbols = 0;
595 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
597 const char *elf_name;
599 int is_label = elf_sym__is_label(&sym);
600 const char *section_name;
602 if (!is_label && !elf_sym__is_function(&sym))
605 sec = elf_getscn(elf, sym.st_shndx);
609 gelf_getshdr(sec, &shdr);
611 if (is_label && !elf_sec__is_text(&shdr, secstrs))
614 section_name = elf_sec__name(&shdr, secstrs);
616 if (self->adjust_symbols) {
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);
621 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
623 sym.st_value += shdr.sh_offset;
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
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;
634 f = symbol__new(sym.st_value, sym.st_size, elf_name,
635 self->sym_priv_size, v);
640 if (filter && filter(map, f))
641 symbol__delete(f, self->sym_priv_size);
643 dso__insert_symbol(self, f);
655 #define BUILD_ID_SIZE 128
657 static char *dso__read_build_id(struct dso *self, int v)
662 Elf_Data *build_id_data;
664 char *build_id = NULL, *bid;
667 int fd = open(self->long_name, O_RDONLY);
672 elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
675 fprintf(stderr, "%s: cannot read %s ELF file.\n",
676 __func__, self->long_name);
680 if (gelf_getehdr(elf, &ehdr) == NULL) {
682 fprintf(stderr, "%s: cannot get elf header.\n", __func__);
686 sec = elf_section_by_name(elf, &ehdr, &shdr, ".note.gnu.build-id", NULL);
690 build_id_data = elf_getdata(sec, NULL);
691 if (build_id_data == NULL)
693 build_id = malloc(BUILD_ID_SIZE);
694 if (build_id == NULL)
696 raw = build_id_data->d_buf + 16;
699 for (i = 0; i < 20; ++i) {
700 sprintf(bid, "%02x", *raw);
705 printf("%s(%s): %s\n", __func__, self->long_name, build_id);
714 char dso__symtab_origin(const struct dso *self)
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',
726 if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
728 return origin[self->origin];
731 int dso__load(struct dso *self, struct map *map, symbol_filter_t filter, int v)
734 char *name = malloc(size), *build_id = NULL;
741 self->adjust_symbols = 0;
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 :
750 self->origin = DSO__ORIG_FEDORA - 1;
755 switch (self->origin) {
756 case DSO__ORIG_FEDORA:
757 snprintf(name, size, "/usr/lib/debug%s.debug",
760 case DSO__ORIG_UBUNTU:
761 snprintf(name, size, "/usr/lib/debug%s",
764 case DSO__ORIG_BUILDID:
765 build_id = dso__read_build_id(self, v);
766 if (build_id != NULL) {
768 "/usr/lib/debug/.build-id/%.2s/%s.debug",
769 build_id, build_id + 2);
776 snprintf(name, size, "%s", self->long_name);
783 fd = open(name, O_RDONLY);
786 ret = dso__load_sym(self, map, name, fd, filter, 0, 0, v);
790 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
796 int nr_plt = dso__synthesize_plt_symbols(self, v);
802 if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
807 static struct rb_root kernel_maps;
808 struct map *kernel_map;
810 static void kernel_maps__insert(struct map *map)
812 maps__insert(&kernel_maps, map);
815 struct symbol *kernel_maps__find_symbol(u64 ip, struct map **mapp)
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.
823 struct map *map = maps__find(&kernel_maps, ip);
827 ip = map->map_ip(map, ip);
828 sym = map->dso->find_symbol(map->dso, ip);
831 sym = map->dso->find_symbol(map->dso, ip);
840 struct map *kernel_maps__find_by_dso_name(const char *name)
844 for (nd = rb_first(&kernel_maps); nd; nd = rb_next(nd)) {
845 struct map *map = rb_entry(nd, struct map, rb_node);
847 if (map->dso && strcmp(map->dso->name, name) == 0)
854 static int dso__load_module_sym(struct dso *self, struct map *map,
855 symbol_filter_t filter, int v)
857 int err = 0, fd = open(self->long_name, O_RDONLY);
861 fprintf(stderr, "%s: cannot open %s\n",
862 __func__, self->long_name);
866 err = dso__load_sym(self, map, self->long_name, fd, filter, 0, 1, v);
872 static int dsos__load_modules_sym_dir(char *dirname,
873 symbol_filter_t filter, int v)
876 int nr_symbols = 0, err;
877 DIR *dir = opendir(dirname);
881 fprintf(stderr, "%s: cannot open %s dir\n", __func__,
886 while ((dent = readdir(dir)) != NULL) {
889 if (dent->d_type == DT_DIR) {
890 if (!strcmp(dent->d_name, ".") ||
891 !strcmp(dent->d_name, ".."))
894 snprintf(path, sizeof(path), "%s/%s",
895 dirname, dent->d_name);
896 err = dsos__load_modules_sym_dir(path, filter, v);
900 char *dot = strrchr(dent->d_name, '.'),
903 struct rb_node *last;
905 if (dot == NULL || strcmp(dot, ".ko"))
907 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
908 (int)(dot - dent->d_name), dent->d_name);
910 map = kernel_maps__find_by_dso_name(dso_name);
914 snprintf(path, sizeof(path), "%s/%s",
915 dirname, dent->d_name);
917 map->dso->long_name = strdup(path);
918 if (map->dso->long_name == NULL)
921 err = dso__load_module_sym(map->dso, map, filter, v);
924 last = rb_last(&map->dso->syms);
927 sym = rb_entry(last, struct symbol, rb_node);
928 map->end = map->start + sym->end;
940 static int dsos__load_modules_sym(symbol_filter_t filter, int v)
943 char modules_path[PATH_MAX];
948 snprintf(modules_path, sizeof(modules_path), "/lib/modules/%s/kernel",
951 return dsos__load_modules_sym_dir(modules_path, filter, v);
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.
959 static struct map *map__new2(u64 start, struct dso *dso)
961 struct map *self = malloc(sizeof(*self));
966 * Will be filled after we load all the symbols
972 self->map_ip = map__map_ip;
973 RB_CLEAR_NODE(&self->rb_node);
978 int dsos__load_modules(unsigned int sym_priv_size,
979 symbol_filter_t filter, int v)
983 FILE *file = fopen("/proc/modules", "r");
989 while (!feof(file)) {
996 line_len = getline(&line, &n, file);
1003 line[--line_len] = '\0'; /* \n */
1005 sep = strrchr(line, 'x');
1009 hex2u64(sep + 1, &start);
1011 sep = strchr(line, ' ');
1017 snprintf(name, sizeof(name), "[%s]", line);
1018 dso = dso__new(name, sym_priv_size);
1021 goto out_delete_line;
1023 map = map__new2(start, dso);
1026 goto out_delete_line;
1029 dso->origin = DSO__ORIG_KMODULE;
1030 kernel_maps__insert(map);
1038 return dsos__load_modules_sym(filter, v);
1046 static int dso__load_vmlinux(struct dso *self, struct map *map,
1047 const char *vmlinux,
1048 symbol_filter_t filter, int v)
1050 int err, fd = open(vmlinux, O_RDONLY);
1055 err = dso__load_sym(self, map, self->long_name, fd, filter, 1, 0, v);
1062 int dsos__load_kernel(const char *vmlinux, unsigned int sym_priv_size,
1063 symbol_filter_t filter, int v, int use_modules)
1066 struct dso *dso = dso__new(vmlinux, sym_priv_size);
1071 dso->short_name = "[kernel]";
1072 kernel_map = map__new2(0, dso);
1073 if (kernel_map == NULL)
1074 goto out_delete_dso;
1076 kernel_map->map_ip = vdso__map_ip;
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);
1084 fprintf(stderr, "dsos__load_modules failed!\n");
1092 err = dso__load_kallsyms(dso, kernel_map, filter, v);
1095 struct rb_node *node = rb_first(&dso->syms);
1096 struct symbol *sym = rb_entry(node, struct symbol, rb_node);
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;
1103 dso->origin = DSO__ORIG_KERNEL;
1105 * XXX See kernel_maps__find_symbol comment
1106 * kernel_maps__insert(kernel_map)
1121 const char *vmlinux_name = "vmlinux";
1124 static void dsos__add(struct dso *dso)
1126 list_add_tail(&dso->node, &dsos);
1129 static struct dso *dsos__find(const char *name)
1133 list_for_each_entry(pos, &dsos, node)
1134 if (strcmp(pos->name, name) == 0)
1139 struct dso *dsos__findnew(const char *name)
1141 struct dso *dso = dsos__find(name);
1147 dso = dso__new(name, 0);
1149 goto out_delete_dso;
1151 nr = dso__load(dso, NULL, NULL, verbose);
1153 eprintf("Failed to open: %s\n", name);
1154 goto out_delete_dso;
1157 eprintf("No symbols found in: %s, maybe install a debug package?\n", name);
1168 void dsos__fprintf(FILE *fp)
1172 list_for_each_entry(pos, &dsos, node)
1173 dso__fprintf(pos, fp);
1176 int load_kernel(void)
1178 if (dsos__load_kernel(vmlinux_name, 0, NULL, verbose, modules) <= 0)
1181 vdso = dso__new("[vdso]", 0);
1190 void symbol__init(void)
1192 elf_version(EV_CURRENT);