11 #include <sys/param.h>
21 #include <sys/utsname.h>
23 #ifndef NT_GNU_BUILD_ID
24 #define NT_GNU_BUILD_ID 3
27 static void dsos__add(struct list_head *head, struct dso *dso);
28 static struct map *map__new2(u64 start, struct dso *dso, enum map_type type);
29 static int dso__load_kernel_sym(struct dso *self, struct map *map,
30 symbol_filter_t filter);
31 static int dso__load_guest_kernel_sym(struct dso *self, struct map *map,
32 symbol_filter_t filter);
33 static int vmlinux_path__nr_entries;
34 static char **vmlinux_path;
36 struct symbol_conf symbol_conf = {
37 .exclude_other = true,
39 .try_vmlinux_path = true,
42 bool dso__loaded(const struct dso *self, enum map_type type)
44 return self->loaded & (1 << type);
47 bool dso__sorted_by_name(const struct dso *self, enum map_type type)
49 return self->sorted_by_name & (1 << type);
52 static void dso__set_sorted_by_name(struct dso *self, enum map_type type)
54 self->sorted_by_name |= (1 << type);
57 bool symbol_type__is_a(char symbol_type, enum map_type map_type)
61 return symbol_type == 'T' || symbol_type == 'W';
63 return symbol_type == 'D' || symbol_type == 'd';
69 static void symbols__fixup_end(struct rb_root *self)
71 struct rb_node *nd, *prevnd = rb_first(self);
72 struct symbol *curr, *prev;
77 curr = rb_entry(prevnd, struct symbol, rb_node);
79 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
81 curr = rb_entry(nd, struct symbol, rb_node);
83 if (prev->end == prev->start)
84 prev->end = curr->start - 1;
88 if (curr->end == curr->start)
89 curr->end = roundup(curr->start, 4096);
92 static void __map_groups__fixup_end(struct map_groups *self, enum map_type type)
94 struct map *prev, *curr;
95 struct rb_node *nd, *prevnd = rb_first(&self->maps[type]);
100 curr = rb_entry(prevnd, struct map, rb_node);
102 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
104 curr = rb_entry(nd, struct map, rb_node);
105 prev->end = curr->start - 1;
109 * We still haven't the actual symbols, so guess the
110 * last map final address.
115 static void map_groups__fixup_end(struct map_groups *self)
118 for (i = 0; i < MAP__NR_TYPES; ++i)
119 __map_groups__fixup_end(self, i);
122 static struct symbol *symbol__new(u64 start, u64 len, const char *name)
124 size_t namelen = strlen(name) + 1;
125 struct symbol *self = calloc(1, (symbol_conf.priv_size +
126 sizeof(*self) + namelen));
130 if (symbol_conf.priv_size)
131 self = ((void *)self) + symbol_conf.priv_size;
134 self->end = len ? start + len - 1 : start;
135 self->namelen = namelen - 1;
137 pr_debug4("%s: %s %#Lx-%#Lx\n", __func__, name, start, self->end);
139 memcpy(self->name, name, namelen);
144 void symbol__delete(struct symbol *self)
146 free(((void *)self) - symbol_conf.priv_size);
149 static size_t symbol__fprintf(struct symbol *self, FILE *fp)
151 return fprintf(fp, " %llx-%llx %s\n",
152 self->start, self->end, self->name);
155 void dso__set_long_name(struct dso *self, char *name)
159 self->long_name = name;
160 self->long_name_len = strlen(name);
163 static void dso__set_short_name(struct dso *self, const char *name)
167 self->short_name = name;
168 self->short_name_len = strlen(name);
171 static void dso__set_basename(struct dso *self)
173 dso__set_short_name(self, basename(self->long_name));
176 struct dso *dso__new(const char *name)
178 struct dso *self = calloc(1, sizeof(*self) + strlen(name) + 1);
182 strcpy(self->name, name);
183 dso__set_long_name(self, self->name);
184 dso__set_short_name(self, self->name);
185 for (i = 0; i < MAP__NR_TYPES; ++i)
186 self->symbols[i] = self->symbol_names[i] = RB_ROOT;
187 self->slen_calculated = 0;
188 self->origin = DSO__ORIG_NOT_FOUND;
190 self->sorted_by_name = 0;
191 self->has_build_id = 0;
192 self->kernel = DSO_TYPE_USER;
193 INIT_LIST_HEAD(&self->node);
199 static void symbols__delete(struct rb_root *self)
202 struct rb_node *next = rb_first(self);
205 pos = rb_entry(next, struct symbol, rb_node);
206 next = rb_next(&pos->rb_node);
207 rb_erase(&pos->rb_node, self);
212 void dso__delete(struct dso *self)
215 for (i = 0; i < MAP__NR_TYPES; ++i)
216 symbols__delete(&self->symbols[i]);
217 if (self->long_name != self->name)
218 free(self->long_name);
222 void dso__set_build_id(struct dso *self, void *build_id)
224 memcpy(self->build_id, build_id, sizeof(self->build_id));
225 self->has_build_id = 1;
228 static void symbols__insert(struct rb_root *self, struct symbol *sym)
230 struct rb_node **p = &self->rb_node;
231 struct rb_node *parent = NULL;
232 const u64 ip = sym->start;
237 s = rb_entry(parent, struct symbol, rb_node);
243 rb_link_node(&sym->rb_node, parent, p);
244 rb_insert_color(&sym->rb_node, self);
247 static struct symbol *symbols__find(struct rb_root *self, u64 ip)
257 struct symbol *s = rb_entry(n, struct symbol, rb_node);
261 else if (ip > s->end)
270 struct symbol_name_rb_node {
271 struct rb_node rb_node;
275 static void symbols__insert_by_name(struct rb_root *self, struct symbol *sym)
277 struct rb_node **p = &self->rb_node;
278 struct rb_node *parent = NULL;
279 struct symbol_name_rb_node *symn = ((void *)sym) - sizeof(*parent), *s;
283 s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
284 if (strcmp(sym->name, s->sym.name) < 0)
289 rb_link_node(&symn->rb_node, parent, p);
290 rb_insert_color(&symn->rb_node, self);
293 static void symbols__sort_by_name(struct rb_root *self, struct rb_root *source)
297 for (nd = rb_first(source); nd; nd = rb_next(nd)) {
298 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
299 symbols__insert_by_name(self, pos);
303 static struct symbol *symbols__find_by_name(struct rb_root *self, const char *name)
313 struct symbol_name_rb_node *s;
316 s = rb_entry(n, struct symbol_name_rb_node, rb_node);
317 cmp = strcmp(name, s->sym.name);
330 struct symbol *dso__find_symbol(struct dso *self,
331 enum map_type type, u64 addr)
333 return symbols__find(&self->symbols[type], addr);
336 struct symbol *dso__find_symbol_by_name(struct dso *self, enum map_type type,
339 return symbols__find_by_name(&self->symbol_names[type], name);
342 void dso__sort_by_name(struct dso *self, enum map_type type)
344 dso__set_sorted_by_name(self, type);
345 return symbols__sort_by_name(&self->symbol_names[type],
346 &self->symbols[type]);
349 int build_id__sprintf(const u8 *self, int len, char *bf)
352 const u8 *raw = self;
355 for (i = 0; i < len; ++i) {
356 sprintf(bid, "%02x", *raw);
364 size_t dso__fprintf_buildid(struct dso *self, FILE *fp)
366 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
368 build_id__sprintf(self->build_id, sizeof(self->build_id), sbuild_id);
369 return fprintf(fp, "%s", sbuild_id);
372 size_t dso__fprintf(struct dso *self, enum map_type type, FILE *fp)
375 size_t ret = fprintf(fp, "dso: %s (", self->short_name);
377 if (self->short_name != self->long_name)
378 ret += fprintf(fp, "%s, ", self->long_name);
379 ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
380 self->loaded ? "" : "NOT ");
381 ret += dso__fprintf_buildid(self, fp);
382 ret += fprintf(fp, ")\n");
383 for (nd = rb_first(&self->symbols[type]); nd; nd = rb_next(nd)) {
384 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
385 ret += symbol__fprintf(pos, fp);
391 int kallsyms__parse(const char *filename, void *arg,
392 int (*process_symbol)(void *arg, const char *name,
393 char type, u64 start))
398 FILE *file = fopen(filename, "r");
403 while (!feof(file)) {
409 line_len = getline(&line, &n, file);
410 if (line_len < 0 || !line)
413 line[--line_len] = '\0'; /* \n */
415 len = hex2u64(line, &start);
418 if (len + 2 >= line_len)
421 symbol_type = toupper(line[len]);
422 symbol_name = line + len + 2;
424 err = process_symbol(arg, symbol_name, symbol_type, start);
437 struct process_kallsyms_args {
442 static int map__process_kallsym_symbol(void *arg, const char *name,
443 char type, u64 start)
446 struct process_kallsyms_args *a = arg;
447 struct rb_root *root = &a->dso->symbols[a->map->type];
449 if (!symbol_type__is_a(type, a->map->type))
453 * Will fix up the end later, when we have all symbols sorted.
455 sym = symbol__new(start, 0, name);
460 * We will pass the symbols to the filter later, in
461 * map__split_kallsyms, when we have split the maps per module
463 symbols__insert(root, sym);
469 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
470 * so that we can in the next step set the symbol ->end address and then
471 * call kernel_maps__split_kallsyms.
473 static int dso__load_all_kallsyms(struct dso *self, const char *filename,
476 struct process_kallsyms_args args = { .map = map, .dso = self, };
477 return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
481 * Split the symbols into maps, making sure there are no overlaps, i.e. the
482 * kernel range is broken in several maps, named [kernel].N, as we don't have
483 * the original ELF section names vmlinux have.
485 static int dso__split_kallsyms(struct dso *self, struct map *map,
486 symbol_filter_t filter)
488 struct map_groups *kmaps = map__kmap(map)->kmaps;
489 struct machine *machine = kmaps->machine;
490 struct map *curr_map = map;
493 struct rb_root *root = &self->symbols[map->type];
494 struct rb_node *next = rb_first(root);
495 int kernel_range = 0;
500 pos = rb_entry(next, struct symbol, rb_node);
501 next = rb_next(&pos->rb_node);
503 module = strchr(pos->name, '\t');
505 if (!symbol_conf.use_modules)
510 if (strcmp(curr_map->dso->short_name, module)) {
511 if (curr_map != map &&
512 self->kernel == DSO_TYPE_GUEST_KERNEL &&
513 machine__is_default_guest(machine)) {
515 * We assume all symbols of a module are
516 * continuous in * kallsyms, so curr_map
517 * points to a module and all its
518 * symbols are in its kmap. Mark it as
521 dso__set_loaded(curr_map->dso,
525 curr_map = map_groups__find_by_name(kmaps,
527 if (curr_map == NULL) {
528 pr_err("%s/proc/{kallsyms,modules} "
529 "inconsistency while looking "
530 "for \"%s\" module!\n",
531 machine->root_dir, module);
536 if (curr_map->dso->loaded &&
537 !machine__is_default_guest(machine))
541 * So that we look just like we get from .ko files,
542 * i.e. not prelinked, relative to map->start.
544 pos->start = curr_map->map_ip(curr_map, pos->start);
545 pos->end = curr_map->map_ip(curr_map, pos->end);
546 } else if (curr_map != map) {
547 char dso_name[PATH_MAX];
550 if (self->kernel == DSO_TYPE_GUEST_KERNEL)
551 snprintf(dso_name, sizeof(dso_name),
555 snprintf(dso_name, sizeof(dso_name),
559 dso = dso__new(dso_name);
563 dso->kernel = self->kernel;
565 curr_map = map__new2(pos->start, dso, map->type);
566 if (curr_map == NULL) {
571 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
572 map_groups__insert(kmaps, curr_map);
576 if (filter && filter(curr_map, pos)) {
577 discard_symbol: rb_erase(&pos->rb_node, root);
580 if (curr_map != map) {
581 rb_erase(&pos->rb_node, root);
582 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
588 if (curr_map != map &&
589 self->kernel == DSO_TYPE_GUEST_KERNEL &&
590 machine__is_default_guest(kmaps->machine)) {
591 dso__set_loaded(curr_map->dso, curr_map->type);
597 int dso__load_kallsyms(struct dso *self, const char *filename,
598 struct map *map, symbol_filter_t filter)
600 if (dso__load_all_kallsyms(self, filename, map) < 0)
603 symbols__fixup_end(&self->symbols[map->type]);
604 if (self->kernel == DSO_TYPE_GUEST_KERNEL)
605 self->origin = DSO__ORIG_GUEST_KERNEL;
607 self->origin = DSO__ORIG_KERNEL;
609 return dso__split_kallsyms(self, map, filter);
612 static int dso__load_perf_map(struct dso *self, struct map *map,
613 symbol_filter_t filter)
620 file = fopen(self->long_name, "r");
624 while (!feof(file)) {
629 line_len = getline(&line, &n, file);
636 line[--line_len] = '\0'; /* \n */
638 len = hex2u64(line, &start);
641 if (len + 2 >= line_len)
644 len += hex2u64(line + len, &size);
647 if (len + 2 >= line_len)
650 sym = symbol__new(start, size, line + len);
653 goto out_delete_line;
655 if (filter && filter(map, sym))
658 symbols__insert(&self->symbols[map->type], sym);
675 * elf_symtab__for_each_symbol - iterate thru all the symbols
677 * @self: struct elf_symtab instance to iterate
679 * @sym: GElf_Sym iterator
681 #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
682 for (idx = 0, gelf_getsym(syms, idx, &sym);\
684 idx++, gelf_getsym(syms, idx, &sym))
686 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
688 return GELF_ST_TYPE(sym->st_info);
691 static inline int elf_sym__is_function(const GElf_Sym *sym)
693 return elf_sym__type(sym) == STT_FUNC &&
695 sym->st_shndx != SHN_UNDEF;
698 static inline bool elf_sym__is_object(const GElf_Sym *sym)
700 return elf_sym__type(sym) == STT_OBJECT &&
702 sym->st_shndx != SHN_UNDEF;
705 static inline int elf_sym__is_label(const GElf_Sym *sym)
707 return elf_sym__type(sym) == STT_NOTYPE &&
709 sym->st_shndx != SHN_UNDEF &&
710 sym->st_shndx != SHN_ABS;
713 static inline const char *elf_sec__name(const GElf_Shdr *shdr,
714 const Elf_Data *secstrs)
716 return secstrs->d_buf + shdr->sh_name;
719 static inline int elf_sec__is_text(const GElf_Shdr *shdr,
720 const Elf_Data *secstrs)
722 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
725 static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
726 const Elf_Data *secstrs)
728 return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
731 static inline const char *elf_sym__name(const GElf_Sym *sym,
732 const Elf_Data *symstrs)
734 return symstrs->d_buf + sym->st_name;
737 static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
738 GElf_Shdr *shp, const char *name,
744 while ((sec = elf_nextscn(elf, sec)) != NULL) {
747 gelf_getshdr(sec, shp);
748 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
749 if (!strcmp(name, str)) {
760 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
761 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
763 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
765 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
766 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
768 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
771 * We need to check if we have a .dynsym, so that we can handle the
772 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
773 * .dynsym or .symtab).
774 * And always look at the original dso, not at debuginfo packages, that
775 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
777 static int dso__synthesize_plt_symbols(struct dso *self, struct map *map,
778 symbol_filter_t filter)
780 uint32_t nr_rel_entries, idx;
785 GElf_Shdr shdr_rel_plt, shdr_dynsym;
786 Elf_Data *reldata, *syms, *symstrs;
787 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
790 char sympltname[1024];
792 int nr = 0, symidx, fd, err = 0;
794 fd = open(self->long_name, O_RDONLY);
798 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
802 if (gelf_getehdr(elf, &ehdr) == NULL)
805 scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
806 ".dynsym", &dynsym_idx);
807 if (scn_dynsym == NULL)
810 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
812 if (scn_plt_rel == NULL) {
813 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
815 if (scn_plt_rel == NULL)
821 if (shdr_rel_plt.sh_link != dynsym_idx)
824 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
828 * Fetch the relocation section to find the idxes to the GOT
829 * and the symbols in the .dynsym they refer to.
831 reldata = elf_getdata(scn_plt_rel, NULL);
835 syms = elf_getdata(scn_dynsym, NULL);
839 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
840 if (scn_symstrs == NULL)
843 symstrs = elf_getdata(scn_symstrs, NULL);
847 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
848 plt_offset = shdr_plt.sh_offset;
850 if (shdr_rel_plt.sh_type == SHT_RELA) {
851 GElf_Rela pos_mem, *pos;
853 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
855 symidx = GELF_R_SYM(pos->r_info);
856 plt_offset += shdr_plt.sh_entsize;
857 gelf_getsym(syms, symidx, &sym);
858 snprintf(sympltname, sizeof(sympltname),
859 "%s@plt", elf_sym__name(&sym, symstrs));
861 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
866 if (filter && filter(map, f))
869 symbols__insert(&self->symbols[map->type], f);
873 } else if (shdr_rel_plt.sh_type == SHT_REL) {
874 GElf_Rel pos_mem, *pos;
875 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
877 symidx = GELF_R_SYM(pos->r_info);
878 plt_offset += shdr_plt.sh_entsize;
879 gelf_getsym(syms, symidx, &sym);
880 snprintf(sympltname, sizeof(sympltname),
881 "%s@plt", elf_sym__name(&sym, symstrs));
883 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
888 if (filter && filter(map, f))
891 symbols__insert(&self->symbols[map->type], f);
906 pr_debug("%s: problems reading %s PLT info.\n",
907 __func__, self->long_name);
911 static bool elf_sym__is_a(GElf_Sym *self, enum map_type type)
915 return elf_sym__is_function(self);
917 return elf_sym__is_object(self);
923 static bool elf_sec__is_a(GElf_Shdr *self, Elf_Data *secstrs, enum map_type type)
927 return elf_sec__is_text(self, secstrs);
929 return elf_sec__is_data(self, secstrs);
935 static int dso__load_sym(struct dso *self, struct map *map, const char *name,
936 int fd, symbol_filter_t filter, int kmodule)
938 struct kmap *kmap = self->kernel ? map__kmap(map) : NULL;
939 struct map *curr_map = map;
940 struct dso *curr_dso = self;
941 Elf_Data *symstrs, *secstrs;
949 Elf_Scn *sec, *sec_strndx;
953 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
955 pr_err("%s: cannot read %s ELF file.\n", __func__, name);
959 if (gelf_getehdr(elf, &ehdr) == NULL) {
960 pr_err("%s: cannot get elf header.\n", __func__);
964 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
966 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
971 syms = elf_getdata(sec, NULL);
975 sec = elf_getscn(elf, shdr.sh_link);
979 symstrs = elf_getdata(sec, NULL);
983 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
984 if (sec_strndx == NULL)
987 secstrs = elf_getdata(sec_strndx, NULL);
991 nr_syms = shdr.sh_size / shdr.sh_entsize;
993 memset(&sym, 0, sizeof(sym));
994 if (self->kernel == DSO_TYPE_USER) {
995 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
996 elf_section_by_name(elf, &ehdr, &shdr,
999 } else self->adjust_symbols = 0;
1001 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
1003 const char *elf_name = elf_sym__name(&sym, symstrs);
1004 char *demangled = NULL;
1005 int is_label = elf_sym__is_label(&sym);
1006 const char *section_name;
1008 if (kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name &&
1009 strcmp(elf_name, kmap->ref_reloc_sym->name) == 0)
1010 kmap->ref_reloc_sym->unrelocated_addr = sym.st_value;
1012 if (!is_label && !elf_sym__is_a(&sym, map->type))
1015 sec = elf_getscn(elf, sym.st_shndx);
1019 gelf_getshdr(sec, &shdr);
1021 if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type))
1024 section_name = elf_sec__name(&shdr, secstrs);
1026 if (self->kernel != DSO_TYPE_USER || kmodule) {
1027 char dso_name[PATH_MAX];
1029 if (strcmp(section_name,
1030 (curr_dso->short_name +
1031 self->short_name_len)) == 0)
1034 if (strcmp(section_name, ".text") == 0) {
1040 snprintf(dso_name, sizeof(dso_name),
1041 "%s%s", self->short_name, section_name);
1043 curr_map = map_groups__find_by_name(kmap->kmaps, map->type, dso_name);
1044 if (curr_map == NULL) {
1045 u64 start = sym.st_value;
1048 start += map->start + shdr.sh_offset;
1050 curr_dso = dso__new(dso_name);
1051 if (curr_dso == NULL)
1053 curr_dso->kernel = self->kernel;
1054 curr_map = map__new2(start, curr_dso,
1056 if (curr_map == NULL) {
1057 dso__delete(curr_dso);
1060 curr_map->map_ip = identity__map_ip;
1061 curr_map->unmap_ip = identity__map_ip;
1062 curr_dso->origin = self->origin;
1063 map_groups__insert(kmap->kmaps, curr_map);
1064 dsos__add(&self->node, curr_dso);
1065 dso__set_loaded(curr_dso, map->type);
1067 curr_dso = curr_map->dso;
1072 if (curr_dso->adjust_symbols) {
1073 pr_debug4("%s: adjusting symbol: st_value: %#Lx "
1074 "sh_addr: %#Lx sh_offset: %#Lx\n", __func__,
1075 (u64)sym.st_value, (u64)shdr.sh_addr,
1076 (u64)shdr.sh_offset);
1077 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
1080 * We need to figure out if the object was created from C++ sources
1081 * DWARF DW_compile_unit has this, but we don't always have access
1084 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
1085 if (demangled != NULL)
1086 elf_name = demangled;
1088 f = symbol__new(sym.st_value, sym.st_size, elf_name);
1093 if (filter && filter(curr_map, f))
1096 symbols__insert(&curr_dso->symbols[curr_map->type], f);
1102 * For misannotated, zeroed, ASM function sizes.
1105 symbols__fixup_end(&self->symbols[map->type]);
1108 * We need to fixup this here too because we create new
1109 * maps here, for things like vsyscall sections.
1111 __map_groups__fixup_end(kmap->kmaps, map->type);
1121 static bool dso__build_id_equal(const struct dso *self, u8 *build_id)
1123 return memcmp(self->build_id, build_id, sizeof(self->build_id)) == 0;
1126 bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
1128 bool have_build_id = false;
1131 list_for_each_entry(pos, head, node) {
1132 if (with_hits && !pos->hit)
1134 if (filename__read_build_id(pos->long_name, pos->build_id,
1135 sizeof(pos->build_id)) > 0) {
1136 have_build_id = true;
1137 pos->has_build_id = true;
1141 return have_build_id;
1145 * Align offset to 4 bytes as needed for note name and descriptor data.
1147 #define NOTE_ALIGN(n) (((n) + 3) & -4U)
1149 int filename__read_build_id(const char *filename, void *bf, size_t size)
1160 if (size < BUILD_ID_SIZE)
1163 fd = open(filename, O_RDONLY);
1167 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1169 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
1174 if (ek != ELF_K_ELF)
1177 if (gelf_getehdr(elf, &ehdr) == NULL) {
1178 pr_err("%s: cannot get elf header.\n", __func__);
1182 sec = elf_section_by_name(elf, &ehdr, &shdr,
1183 ".note.gnu.build-id", NULL);
1185 sec = elf_section_by_name(elf, &ehdr, &shdr,
1191 data = elf_getdata(sec, NULL);
1196 while (ptr < (data->d_buf + data->d_size)) {
1197 GElf_Nhdr *nhdr = ptr;
1198 int namesz = NOTE_ALIGN(nhdr->n_namesz),
1199 descsz = NOTE_ALIGN(nhdr->n_descsz);
1202 ptr += sizeof(*nhdr);
1205 if (nhdr->n_type == NT_GNU_BUILD_ID &&
1206 nhdr->n_namesz == sizeof("GNU")) {
1207 if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
1208 memcpy(bf, ptr, BUILD_ID_SIZE);
1209 err = BUILD_ID_SIZE;
1223 int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
1227 if (size < BUILD_ID_SIZE)
1230 fd = open(filename, O_RDONLY);
1239 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
1242 namesz = NOTE_ALIGN(nhdr.n_namesz);
1243 descsz = NOTE_ALIGN(nhdr.n_descsz);
1244 if (nhdr.n_type == NT_GNU_BUILD_ID &&
1245 nhdr.n_namesz == sizeof("GNU")) {
1246 if (read(fd, bf, namesz) != namesz)
1248 if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
1249 if (read(fd, build_id,
1250 BUILD_ID_SIZE) == BUILD_ID_SIZE) {
1254 } else if (read(fd, bf, descsz) != descsz)
1257 int n = namesz + descsz;
1258 if (read(fd, bf, n) != n)
1267 char dso__symtab_origin(const struct dso *self)
1269 static const char origin[] = {
1270 [DSO__ORIG_KERNEL] = 'k',
1271 [DSO__ORIG_JAVA_JIT] = 'j',
1272 [DSO__ORIG_BUILD_ID_CACHE] = 'B',
1273 [DSO__ORIG_FEDORA] = 'f',
1274 [DSO__ORIG_UBUNTU] = 'u',
1275 [DSO__ORIG_BUILDID] = 'b',
1276 [DSO__ORIG_DSO] = 'd',
1277 [DSO__ORIG_KMODULE] = 'K',
1278 [DSO__ORIG_GUEST_KERNEL] = 'g',
1279 [DSO__ORIG_GUEST_KMODULE] = 'G',
1282 if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
1284 return origin[self->origin];
1287 int dso__load(struct dso *self, struct map *map, symbol_filter_t filter)
1289 int size = PATH_MAX;
1291 u8 build_id[BUILD_ID_SIZE];
1292 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
1295 struct machine *machine;
1296 const char *root_dir;
1298 dso__set_loaded(self, map->type);
1300 if (self->kernel == DSO_TYPE_KERNEL)
1301 return dso__load_kernel_sym(self, map, filter);
1302 else if (self->kernel == DSO_TYPE_GUEST_KERNEL)
1303 return dso__load_guest_kernel_sym(self, map, filter);
1305 if (map->groups && map->groups->machine)
1306 machine = map->groups->machine;
1310 name = malloc(size);
1314 self->adjust_symbols = 0;
1316 if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
1317 ret = dso__load_perf_map(self, map, filter);
1318 self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
1319 DSO__ORIG_NOT_FOUND;
1323 self->origin = DSO__ORIG_BUILD_ID_CACHE;
1325 if (self->has_build_id) {
1326 build_id__sprintf(self->build_id, sizeof(self->build_id),
1328 snprintf(name, size, "%s/%s/.build-id/%.2s/%s",
1329 getenv("HOME"), DEBUG_CACHE_DIR,
1330 build_id_hex, build_id_hex + 2);
1336 switch (self->origin) {
1337 case DSO__ORIG_FEDORA:
1338 snprintf(name, size, "/usr/lib/debug%s.debug",
1341 case DSO__ORIG_UBUNTU:
1342 snprintf(name, size, "/usr/lib/debug%s",
1345 case DSO__ORIG_BUILDID:
1346 if (filename__read_build_id(self->long_name, build_id,
1347 sizeof(build_id))) {
1348 build_id__sprintf(build_id, sizeof(build_id),
1350 snprintf(name, size,
1351 "/usr/lib/debug/.build-id/%.2s/%s.debug",
1352 build_id_hex, build_id_hex + 2);
1353 if (self->has_build_id)
1354 goto compare_build_id;
1360 snprintf(name, size, "%s", self->long_name);
1362 case DSO__ORIG_GUEST_KMODULE:
1363 if (map->groups && map->groups->machine)
1364 root_dir = map->groups->machine->root_dir;
1367 snprintf(name, size, "%s%s", root_dir, self->long_name);
1374 if (self->has_build_id) {
1375 if (filename__read_build_id(name, build_id,
1376 sizeof(build_id)) < 0)
1379 if (!dso__build_id_equal(self, build_id))
1383 fd = open(name, O_RDONLY);
1386 ret = dso__load_sym(self, map, name, fd, filter, 0);
1390 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
1396 int nr_plt = dso__synthesize_plt_symbols(self, map, filter);
1402 if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
1407 struct map *map_groups__find_by_name(struct map_groups *self,
1408 enum map_type type, const char *name)
1412 for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) {
1413 struct map *map = rb_entry(nd, struct map, rb_node);
1415 if (map->dso && strcmp(map->dso->short_name, name) == 0)
1422 static int dso__kernel_module_get_build_id(struct dso *self,
1423 const char *root_dir)
1425 char filename[PATH_MAX];
1427 * kernel module short names are of the form "[module]" and
1428 * we need just "module" here.
1430 const char *name = self->short_name + 1;
1432 snprintf(filename, sizeof(filename),
1433 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1434 root_dir, (int)strlen(name) - 1, name);
1436 if (sysfs__read_build_id(filename, self->build_id,
1437 sizeof(self->build_id)) == 0)
1438 self->has_build_id = true;
1443 static int map_groups__set_modules_path_dir(struct map_groups *self,
1444 const char *dir_name)
1446 struct dirent *dent;
1447 DIR *dir = opendir(dir_name);
1450 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
1454 while ((dent = readdir(dir)) != NULL) {
1455 char path[PATH_MAX];
1458 /*sshfs might return bad dent->d_type, so we have to stat*/
1459 sprintf(path, "%s/%s", dir_name, dent->d_name);
1460 if (stat(path, &st))
1463 if (S_ISDIR(st.st_mode)) {
1464 if (!strcmp(dent->d_name, ".") ||
1465 !strcmp(dent->d_name, ".."))
1468 snprintf(path, sizeof(path), "%s/%s",
1469 dir_name, dent->d_name);
1470 if (map_groups__set_modules_path_dir(self, path) < 0)
1473 char *dot = strrchr(dent->d_name, '.'),
1478 if (dot == NULL || strcmp(dot, ".ko"))
1480 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1481 (int)(dot - dent->d_name), dent->d_name);
1483 strxfrchar(dso_name, '-', '_');
1484 map = map_groups__find_by_name(self, MAP__FUNCTION, dso_name);
1488 snprintf(path, sizeof(path), "%s/%s",
1489 dir_name, dent->d_name);
1491 long_name = strdup(path);
1492 if (long_name == NULL)
1494 dso__set_long_name(map->dso, long_name);
1495 dso__kernel_module_get_build_id(map->dso, "");
1505 static char *get_kernel_version(const char *root_dir)
1507 char version[PATH_MAX];
1510 const char *prefix = "Linux version ";
1512 sprintf(version, "%s/proc/version", root_dir);
1513 file = fopen(version, "r");
1518 tmp = fgets(version, sizeof(version), file);
1521 name = strstr(version, prefix);
1524 name += strlen(prefix);
1525 tmp = strchr(name, ' ');
1529 return strdup(name);
1532 static int machine__set_modules_path(struct machine *self)
1535 char modules_path[PATH_MAX];
1537 version = get_kernel_version(self->root_dir);
1541 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s/kernel",
1542 self->root_dir, version);
1545 return map_groups__set_modules_path_dir(&self->kmaps, modules_path);
1549 * Constructor variant for modules (where we know from /proc/modules where
1550 * they are loaded) and for vmlinux, where only after we load all the
1551 * symbols we'll know where it starts and ends.
1553 static struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
1555 struct map *self = calloc(1, (sizeof(*self) +
1556 (dso->kernel ? sizeof(struct kmap) : 0)));
1559 * ->end will be filled after we load all the symbols
1561 map__init(self, type, start, 0, 0, dso);
1567 struct map *machine__new_module(struct machine *self, u64 start,
1568 const char *filename)
1571 struct dso *dso = __dsos__findnew(&self->kernel_dsos, filename);
1576 map = map__new2(start, dso, MAP__FUNCTION);
1580 if (machine__is_host(self))
1581 dso->origin = DSO__ORIG_KMODULE;
1583 dso->origin = DSO__ORIG_GUEST_KMODULE;
1584 map_groups__insert(&self->kmaps, map);
1588 static int machine__create_modules(struct machine *self)
1594 const char *modules;
1595 char path[PATH_MAX];
1597 if (machine__is_default_guest(self))
1598 modules = symbol_conf.default_guest_modules;
1600 sprintf(path, "%s/proc/modules", self->root_dir);
1604 file = fopen(modules, "r");
1608 while (!feof(file)) {
1609 char name[PATH_MAX];
1614 line_len = getline(&line, &n, file);
1621 line[--line_len] = '\0'; /* \n */
1623 sep = strrchr(line, 'x');
1627 hex2u64(sep + 1, &start);
1629 sep = strchr(line, ' ');
1635 snprintf(name, sizeof(name), "[%s]", line);
1636 map = machine__new_module(self, start, name);
1638 goto out_delete_line;
1639 dso__kernel_module_get_build_id(map->dso, self->root_dir);
1645 return machine__set_modules_path(self);
1653 static int dso__load_vmlinux(struct dso *self, struct map *map,
1654 const char *vmlinux, symbol_filter_t filter)
1658 if (self->has_build_id) {
1659 u8 build_id[BUILD_ID_SIZE];
1661 if (filename__read_build_id(vmlinux, build_id,
1662 sizeof(build_id)) < 0) {
1663 pr_debug("No build_id in %s, ignoring it\n", vmlinux);
1666 if (!dso__build_id_equal(self, build_id)) {
1667 char expected_build_id[BUILD_ID_SIZE * 2 + 1],
1668 vmlinux_build_id[BUILD_ID_SIZE * 2 + 1];
1670 build_id__sprintf(self->build_id,
1671 sizeof(self->build_id),
1673 build_id__sprintf(build_id, sizeof(build_id),
1675 pr_debug("build_id in %s is %s while expected is %s, "
1676 "ignoring it\n", vmlinux, vmlinux_build_id,
1682 fd = open(vmlinux, O_RDONLY);
1686 dso__set_loaded(self, map->type);
1687 err = dso__load_sym(self, map, vmlinux, fd, filter, 0);
1691 pr_debug("Using %s for symbols\n", vmlinux);
1696 int dso__load_vmlinux_path(struct dso *self, struct map *map,
1697 symbol_filter_t filter)
1701 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1702 vmlinux_path__nr_entries);
1704 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
1705 err = dso__load_vmlinux(self, map, vmlinux_path[i], filter);
1707 dso__set_long_name(self, strdup(vmlinux_path[i]));
1715 static int dso__load_kernel_sym(struct dso *self, struct map *map,
1716 symbol_filter_t filter)
1719 const char *kallsyms_filename = NULL;
1720 char *kallsyms_allocated_filename = NULL;
1722 * Step 1: if the user specified a vmlinux filename, use it and only
1723 * it, reporting errors to the user if it cannot be used.
1725 * For instance, try to analyse an ARM perf.data file _without_ a
1726 * build-id, or if the user specifies the wrong path to the right
1727 * vmlinux file, obviously we can't fallback to another vmlinux (a
1728 * x86_86 one, on the machine where analysis is being performed, say),
1729 * or worse, /proc/kallsyms.
1731 * If the specified file _has_ a build-id and there is a build-id
1732 * section in the perf.data file, we will still do the expected
1733 * validation in dso__load_vmlinux and will bail out if they don't
1736 if (symbol_conf.vmlinux_name != NULL) {
1737 err = dso__load_vmlinux(self, map,
1738 symbol_conf.vmlinux_name, filter);
1742 if (vmlinux_path != NULL) {
1743 err = dso__load_vmlinux_path(self, map, filter);
1749 * Say the kernel DSO was created when processing the build-id header table,
1750 * we have a build-id, so check if it is the same as the running kernel,
1751 * using it if it is.
1753 if (self->has_build_id) {
1754 u8 kallsyms_build_id[BUILD_ID_SIZE];
1755 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1757 if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id,
1758 sizeof(kallsyms_build_id)) == 0) {
1759 if (dso__build_id_equal(self, kallsyms_build_id)) {
1760 kallsyms_filename = "/proc/kallsyms";
1765 * Now look if we have it on the build-id cache in
1766 * $HOME/.debug/[kernel.kallsyms].
1768 build_id__sprintf(self->build_id, sizeof(self->build_id),
1771 if (asprintf(&kallsyms_allocated_filename,
1772 "%s/.debug/[kernel.kallsyms]/%s",
1773 getenv("HOME"), sbuild_id) == -1) {
1774 pr_err("Not enough memory for kallsyms file lookup\n");
1778 kallsyms_filename = kallsyms_allocated_filename;
1780 if (access(kallsyms_filename, F_OK)) {
1781 pr_err("No kallsyms or vmlinux with build-id %s "
1782 "was found\n", sbuild_id);
1783 free(kallsyms_allocated_filename);
1788 * Last resort, if we don't have a build-id and couldn't find
1789 * any vmlinux file, try the running kernel kallsyms table.
1791 kallsyms_filename = "/proc/kallsyms";
1795 err = dso__load_kallsyms(self, kallsyms_filename, map, filter);
1797 pr_debug("Using %s for symbols\n", kallsyms_filename);
1798 free(kallsyms_allocated_filename);
1803 if (kallsyms_filename != NULL)
1804 dso__set_long_name(self, strdup("[kernel.kallsyms]"));
1805 map__fixup_start(map);
1806 map__fixup_end(map);
1812 static int dso__load_guest_kernel_sym(struct dso *self, struct map *map,
1813 symbol_filter_t filter)
1816 const char *kallsyms_filename = NULL;
1817 struct machine *machine;
1818 char path[PATH_MAX];
1821 pr_debug("Guest kernel map hasn't the point to groups\n");
1824 machine = map->groups->machine;
1826 if (machine__is_default_guest(machine)) {
1828 * if the user specified a vmlinux filename, use it and only
1829 * it, reporting errors to the user if it cannot be used.
1830 * Or use file guest_kallsyms inputted by user on commandline
1832 if (symbol_conf.default_guest_vmlinux_name != NULL) {
1833 err = dso__load_vmlinux(self, map,
1834 symbol_conf.default_guest_vmlinux_name, filter);
1838 kallsyms_filename = symbol_conf.default_guest_kallsyms;
1839 if (!kallsyms_filename)
1842 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
1843 kallsyms_filename = path;
1846 err = dso__load_kallsyms(self, kallsyms_filename, map, filter);
1848 pr_debug("Using %s for symbols\n", kallsyms_filename);
1852 if (kallsyms_filename != NULL) {
1853 machine__mmap_name(machine, path, sizeof(path));
1854 dso__set_long_name(self, strdup(path));
1856 map__fixup_start(map);
1857 map__fixup_end(map);
1863 static void dsos__add(struct list_head *head, struct dso *dso)
1865 list_add_tail(&dso->node, head);
1868 static struct dso *dsos__find(struct list_head *head, const char *name)
1872 list_for_each_entry(pos, head, node)
1873 if (strcmp(pos->long_name, name) == 0)
1878 struct dso *__dsos__findnew(struct list_head *head, const char *name)
1880 struct dso *dso = dsos__find(head, name);
1883 dso = dso__new(name);
1885 dsos__add(head, dso);
1886 dso__set_basename(dso);
1893 size_t __dsos__fprintf(struct list_head *head, FILE *fp)
1898 list_for_each_entry(pos, head, node) {
1900 for (i = 0; i < MAP__NR_TYPES; ++i)
1901 ret += dso__fprintf(pos, i, fp);
1907 size_t machines__fprintf_dsos(struct rb_root *self, FILE *fp)
1912 for (nd = rb_first(self); nd; nd = rb_next(nd)) {
1913 struct machine *pos = rb_entry(nd, struct machine, rb_node);
1914 ret += __dsos__fprintf(&pos->kernel_dsos, fp);
1915 ret += __dsos__fprintf(&pos->user_dsos, fp);
1921 static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
1927 list_for_each_entry(pos, head, node) {
1928 if (with_hits && !pos->hit)
1930 ret += dso__fprintf_buildid(pos, fp);
1931 ret += fprintf(fp, " %s\n", pos->long_name);
1936 size_t machines__fprintf_dsos_buildid(struct rb_root *self, FILE *fp, bool with_hits)
1941 for (nd = rb_first(self); nd; nd = rb_next(nd)) {
1942 struct machine *pos = rb_entry(nd, struct machine, rb_node);
1943 ret += __dsos__fprintf_buildid(&pos->kernel_dsos, fp, with_hits);
1944 ret += __dsos__fprintf_buildid(&pos->user_dsos, fp, with_hits);
1949 struct dso *dso__new_kernel(const char *name)
1951 struct dso *self = dso__new(name ?: "[kernel.kallsyms]");
1954 dso__set_short_name(self, "[kernel]");
1955 self->kernel = DSO_TYPE_KERNEL;
1961 static struct dso *dso__new_guest_kernel(struct machine *machine,
1965 struct dso *self = dso__new(name ?: machine__mmap_name(machine, bf, sizeof(bf)));
1968 dso__set_short_name(self, "[guest.kernel]");
1969 self->kernel = DSO_TYPE_GUEST_KERNEL;
1975 void dso__read_running_kernel_build_id(struct dso *self, struct machine *machine)
1977 char path[PATH_MAX];
1979 if (machine__is_default_guest(machine))
1981 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
1982 if (sysfs__read_build_id(path, self->build_id,
1983 sizeof(self->build_id)) == 0)
1984 self->has_build_id = true;
1987 static struct dso *machine__create_kernel(struct machine *self)
1989 const char *vmlinux_name = NULL;
1992 if (machine__is_host(self)) {
1993 vmlinux_name = symbol_conf.vmlinux_name;
1994 kernel = dso__new_kernel(vmlinux_name);
1996 if (machine__is_default_guest(self))
1997 vmlinux_name = symbol_conf.default_guest_vmlinux_name;
1998 kernel = dso__new_guest_kernel(self, vmlinux_name);
2001 if (kernel != NULL) {
2002 dso__read_running_kernel_build_id(kernel, self);
2003 dsos__add(&self->kernel_dsos, kernel);
2008 int __machine__create_kernel_maps(struct machine *self, struct dso *kernel)
2012 for (type = 0; type < MAP__NR_TYPES; ++type) {
2015 self->vmlinux_maps[type] = map__new2(0, kernel, type);
2016 if (self->vmlinux_maps[type] == NULL)
2019 self->vmlinux_maps[type]->map_ip =
2020 self->vmlinux_maps[type]->unmap_ip = identity__map_ip;
2022 kmap = map__kmap(self->vmlinux_maps[type]);
2023 kmap->kmaps = &self->kmaps;
2024 map_groups__insert(&self->kmaps, self->vmlinux_maps[type]);
2030 int machine__create_kernel_maps(struct machine *self)
2032 struct dso *kernel = machine__create_kernel(self);
2034 if (kernel == NULL ||
2035 __machine__create_kernel_maps(self, kernel) < 0)
2038 if (symbol_conf.use_modules && machine__create_modules(self) < 0)
2039 pr_debug("Problems creating module maps, continuing anyway...\n");
2041 * Now that we have all the maps created, just set the ->end of them:
2043 map_groups__fixup_end(&self->kmaps);
2047 static void vmlinux_path__exit(void)
2049 while (--vmlinux_path__nr_entries >= 0) {
2050 free(vmlinux_path[vmlinux_path__nr_entries]);
2051 vmlinux_path[vmlinux_path__nr_entries] = NULL;
2055 vmlinux_path = NULL;
2058 static int vmlinux_path__init(void)
2063 if (uname(&uts) < 0)
2066 vmlinux_path = malloc(sizeof(char *) * 5);
2067 if (vmlinux_path == NULL)
2070 vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
2071 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2073 ++vmlinux_path__nr_entries;
2074 vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
2075 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2077 ++vmlinux_path__nr_entries;
2078 snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
2079 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
2080 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2082 ++vmlinux_path__nr_entries;
2083 snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
2084 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
2085 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2087 ++vmlinux_path__nr_entries;
2088 snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
2090 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
2091 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2093 ++vmlinux_path__nr_entries;
2098 vmlinux_path__exit();
2102 size_t vmlinux_path__fprintf(FILE *fp)
2107 for (i = 0; i < vmlinux_path__nr_entries; ++i)
2108 printed += fprintf(fp, "[%d] %s\n", i, vmlinux_path[i]);
2113 static int setup_list(struct strlist **list, const char *list_str,
2114 const char *list_name)
2116 if (list_str == NULL)
2119 *list = strlist__new(true, list_str);
2121 pr_err("problems parsing %s list\n", list_name);
2127 int symbol__init(void)
2129 elf_version(EV_CURRENT);
2130 if (symbol_conf.sort_by_name)
2131 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
2132 sizeof(struct symbol));
2134 if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
2137 if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
2138 pr_err("'.' is the only non valid --field-separator argument\n");
2142 if (setup_list(&symbol_conf.dso_list,
2143 symbol_conf.dso_list_str, "dso") < 0)
2146 if (setup_list(&symbol_conf.comm_list,
2147 symbol_conf.comm_list_str, "comm") < 0)
2148 goto out_free_dso_list;
2150 if (setup_list(&symbol_conf.sym_list,
2151 symbol_conf.sym_list_str, "symbol") < 0)
2152 goto out_free_comm_list;
2157 strlist__delete(symbol_conf.dso_list);
2159 strlist__delete(symbol_conf.comm_list);
2163 int machines__create_kernel_maps(struct rb_root *self, pid_t pid)
2165 struct machine *machine = machines__findnew(self, pid);
2167 if (machine == NULL)
2170 return machine__create_kernel_maps(machine);
2173 static int hex(char ch)
2175 if ((ch >= '0') && (ch <= '9'))
2177 if ((ch >= 'a') && (ch <= 'f'))
2178 return ch - 'a' + 10;
2179 if ((ch >= 'A') && (ch <= 'F'))
2180 return ch - 'A' + 10;
2185 * While we find nice hex chars, build a long_val.
2186 * Return number of chars processed.
2188 int hex2u64(const char *ptr, u64 *long_val)
2190 const char *p = ptr;
2194 const int hex_val = hex(*p);
2199 *long_val = (*long_val << 4) | hex_val;
2206 char *strxfrchar(char *s, char from, char to)
2210 while ((p = strchr(p, from)) != NULL)
2216 int machines__create_guest_kernel_maps(struct rb_root *self)
2219 struct dirent **namelist = NULL;
2221 char path[PATH_MAX];
2224 if (symbol_conf.default_guest_vmlinux_name ||
2225 symbol_conf.default_guest_modules ||
2226 symbol_conf.default_guest_kallsyms) {
2227 machines__create_kernel_maps(self, DEFAULT_GUEST_KERNEL_ID);
2230 if (symbol_conf.guestmount) {
2231 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
2234 for (i = 0; i < items; i++) {
2235 if (!isdigit(namelist[i]->d_name[0])) {
2236 /* Filter out . and .. */
2239 pid = atoi(namelist[i]->d_name);
2240 sprintf(path, "%s/%s/proc/kallsyms",
2241 symbol_conf.guestmount,
2242 namelist[i]->d_name);
2243 ret = access(path, R_OK);
2245 pr_debug("Can't access file %s\n", path);
2248 machines__create_kernel_maps(self, pid);
2257 int machine__load_kallsyms(struct machine *self, const char *filename,
2258 enum map_type type, symbol_filter_t filter)
2260 struct map *map = self->vmlinux_maps[type];
2261 int ret = dso__load_kallsyms(map->dso, filename, map, filter);
2264 dso__set_loaded(map->dso, type);
2266 * Since /proc/kallsyms will have multiple sessions for the
2267 * kernel, with modules between them, fixup the end of all
2270 __map_groups__fixup_end(&self->kmaps, type);
2276 int machine__load_vmlinux_path(struct machine *self, enum map_type type,
2277 symbol_filter_t filter)
2279 struct map *map = self->vmlinux_maps[type];
2280 int ret = dso__load_vmlinux_path(map->dso, map, filter);
2283 dso__set_loaded(map->dso, type);
2284 map__reloc_vmlinux(map);