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;
136 pr_debug4("%s: %s %#Lx-%#Lx\n", __func__, name, start, self->end);
138 memcpy(self->name, name, namelen);
143 void symbol__delete(struct symbol *self)
145 free(((void *)self) - symbol_conf.priv_size);
148 static size_t symbol__fprintf(struct symbol *self, FILE *fp)
150 return fprintf(fp, " %llx-%llx %s\n",
151 self->start, self->end, self->name);
154 void dso__set_long_name(struct dso *self, char *name)
158 self->long_name = name;
159 self->long_name_len = strlen(name);
162 static void dso__set_short_name(struct dso *self, const char *name)
166 self->short_name = name;
167 self->short_name_len = strlen(name);
170 static void dso__set_basename(struct dso *self)
172 dso__set_short_name(self, basename(self->long_name));
175 struct dso *dso__new(const char *name)
177 struct dso *self = calloc(1, sizeof(*self) + strlen(name) + 1);
181 strcpy(self->name, name);
182 dso__set_long_name(self, self->name);
183 dso__set_short_name(self, self->name);
184 for (i = 0; i < MAP__NR_TYPES; ++i)
185 self->symbols[i] = self->symbol_names[i] = RB_ROOT;
186 self->slen_calculated = 0;
187 self->origin = DSO__ORIG_NOT_FOUND;
189 self->sorted_by_name = 0;
190 self->has_build_id = 0;
191 self->kernel = DSO_TYPE_USER;
192 INIT_LIST_HEAD(&self->node);
198 static void symbols__delete(struct rb_root *self)
201 struct rb_node *next = rb_first(self);
204 pos = rb_entry(next, struct symbol, rb_node);
205 next = rb_next(&pos->rb_node);
206 rb_erase(&pos->rb_node, self);
211 void dso__delete(struct dso *self)
214 for (i = 0; i < MAP__NR_TYPES; ++i)
215 symbols__delete(&self->symbols[i]);
216 if (self->long_name != self->name)
217 free(self->long_name);
221 void dso__set_build_id(struct dso *self, void *build_id)
223 memcpy(self->build_id, build_id, sizeof(self->build_id));
224 self->has_build_id = 1;
227 static void symbols__insert(struct rb_root *self, struct symbol *sym)
229 struct rb_node **p = &self->rb_node;
230 struct rb_node *parent = NULL;
231 const u64 ip = sym->start;
236 s = rb_entry(parent, struct symbol, rb_node);
242 rb_link_node(&sym->rb_node, parent, p);
243 rb_insert_color(&sym->rb_node, self);
246 static struct symbol *symbols__find(struct rb_root *self, u64 ip)
256 struct symbol *s = rb_entry(n, struct symbol, rb_node);
260 else if (ip > s->end)
269 struct symbol_name_rb_node {
270 struct rb_node rb_node;
274 static void symbols__insert_by_name(struct rb_root *self, struct symbol *sym)
276 struct rb_node **p = &self->rb_node;
277 struct rb_node *parent = NULL;
278 struct symbol_name_rb_node *symn = ((void *)sym) - sizeof(*parent), *s;
282 s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
283 if (strcmp(sym->name, s->sym.name) < 0)
288 rb_link_node(&symn->rb_node, parent, p);
289 rb_insert_color(&symn->rb_node, self);
292 static void symbols__sort_by_name(struct rb_root *self, struct rb_root *source)
296 for (nd = rb_first(source); nd; nd = rb_next(nd)) {
297 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
298 symbols__insert_by_name(self, pos);
302 static struct symbol *symbols__find_by_name(struct rb_root *self, const char *name)
312 struct symbol_name_rb_node *s;
315 s = rb_entry(n, struct symbol_name_rb_node, rb_node);
316 cmp = strcmp(name, s->sym.name);
329 struct symbol *dso__find_symbol(struct dso *self,
330 enum map_type type, u64 addr)
332 return symbols__find(&self->symbols[type], addr);
335 struct symbol *dso__find_symbol_by_name(struct dso *self, enum map_type type,
338 return symbols__find_by_name(&self->symbol_names[type], name);
341 void dso__sort_by_name(struct dso *self, enum map_type type)
343 dso__set_sorted_by_name(self, type);
344 return symbols__sort_by_name(&self->symbol_names[type],
345 &self->symbols[type]);
348 int build_id__sprintf(const u8 *self, int len, char *bf)
351 const u8 *raw = self;
354 for (i = 0; i < len; ++i) {
355 sprintf(bid, "%02x", *raw);
363 size_t dso__fprintf_buildid(struct dso *self, FILE *fp)
365 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
367 build_id__sprintf(self->build_id, sizeof(self->build_id), sbuild_id);
368 return fprintf(fp, "%s", sbuild_id);
371 size_t dso__fprintf(struct dso *self, enum map_type type, FILE *fp)
374 size_t ret = fprintf(fp, "dso: %s (", self->short_name);
376 if (self->short_name != self->long_name)
377 ret += fprintf(fp, "%s, ", self->long_name);
378 ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
379 self->loaded ? "" : "NOT ");
380 ret += dso__fprintf_buildid(self, fp);
381 ret += fprintf(fp, ")\n");
382 for (nd = rb_first(&self->symbols[type]); nd; nd = rb_next(nd)) {
383 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
384 ret += symbol__fprintf(pos, fp);
390 int kallsyms__parse(const char *filename, void *arg,
391 int (*process_symbol)(void *arg, const char *name,
392 char type, u64 start))
397 FILE *file = fopen(filename, "r");
402 while (!feof(file)) {
408 line_len = getline(&line, &n, file);
409 if (line_len < 0 || !line)
412 line[--line_len] = '\0'; /* \n */
414 len = hex2u64(line, &start);
417 if (len + 2 >= line_len)
420 symbol_type = toupper(line[len]);
421 symbol_name = line + len + 2;
423 err = process_symbol(arg, symbol_name, symbol_type, start);
436 struct process_kallsyms_args {
441 static int map__process_kallsym_symbol(void *arg, const char *name,
442 char type, u64 start)
445 struct process_kallsyms_args *a = arg;
446 struct rb_root *root = &a->dso->symbols[a->map->type];
448 if (!symbol_type__is_a(type, a->map->type))
452 * Will fix up the end later, when we have all symbols sorted.
454 sym = symbol__new(start, 0, name);
459 * We will pass the symbols to the filter later, in
460 * map__split_kallsyms, when we have split the maps per module
462 symbols__insert(root, sym);
468 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
469 * so that we can in the next step set the symbol ->end address and then
470 * call kernel_maps__split_kallsyms.
472 static int dso__load_all_kallsyms(struct dso *self, const char *filename,
475 struct process_kallsyms_args args = { .map = map, .dso = self, };
476 return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
480 * Split the symbols into maps, making sure there are no overlaps, i.e. the
481 * kernel range is broken in several maps, named [kernel].N, as we don't have
482 * the original ELF section names vmlinux have.
484 static int dso__split_kallsyms(struct dso *self, struct map *map,
485 symbol_filter_t filter)
487 struct map_groups *kmaps = map__kmap(map)->kmaps;
488 struct machine *machine = kmaps->machine;
489 struct map *curr_map = map;
492 struct rb_root *root = &self->symbols[map->type];
493 struct rb_node *next = rb_first(root);
494 int kernel_range = 0;
499 pos = rb_entry(next, struct symbol, rb_node);
500 next = rb_next(&pos->rb_node);
502 module = strchr(pos->name, '\t');
504 if (!symbol_conf.use_modules)
509 if (strcmp(curr_map->dso->short_name, module)) {
510 if (curr_map != map &&
511 self->kernel == DSO_TYPE_GUEST_KERNEL &&
512 machine__is_default_guest(machine)) {
514 * We assume all symbols of a module are
515 * continuous in * kallsyms, so curr_map
516 * points to a module and all its
517 * symbols are in its kmap. Mark it as
520 dso__set_loaded(curr_map->dso,
524 curr_map = map_groups__find_by_name(kmaps,
526 if (curr_map == NULL) {
527 pr_err("%s/proc/{kallsyms,modules} "
528 "inconsistency while looking "
529 "for \"%s\" module!\n",
530 machine->root_dir, module);
535 if (curr_map->dso->loaded &&
536 !machine__is_default_guest(machine))
540 * So that we look just like we get from .ko files,
541 * i.e. not prelinked, relative to map->start.
543 pos->start = curr_map->map_ip(curr_map, pos->start);
544 pos->end = curr_map->map_ip(curr_map, pos->end);
545 } else if (curr_map != map) {
546 char dso_name[PATH_MAX];
549 if (self->kernel == DSO_TYPE_GUEST_KERNEL)
550 snprintf(dso_name, sizeof(dso_name),
554 snprintf(dso_name, sizeof(dso_name),
558 dso = dso__new(dso_name);
562 dso->kernel = self->kernel;
564 curr_map = map__new2(pos->start, dso, map->type);
565 if (curr_map == NULL) {
570 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
571 map_groups__insert(kmaps, curr_map);
575 if (filter && filter(curr_map, pos)) {
576 discard_symbol: rb_erase(&pos->rb_node, root);
579 if (curr_map != map) {
580 rb_erase(&pos->rb_node, root);
581 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
587 if (curr_map != map &&
588 self->kernel == DSO_TYPE_GUEST_KERNEL &&
589 machine__is_default_guest(kmaps->machine)) {
590 dso__set_loaded(curr_map->dso, curr_map->type);
596 int dso__load_kallsyms(struct dso *self, const char *filename,
597 struct map *map, symbol_filter_t filter)
599 if (dso__load_all_kallsyms(self, filename, map) < 0)
602 symbols__fixup_end(&self->symbols[map->type]);
603 if (self->kernel == DSO_TYPE_GUEST_KERNEL)
604 self->origin = DSO__ORIG_GUEST_KERNEL;
606 self->origin = DSO__ORIG_KERNEL;
608 return dso__split_kallsyms(self, map, filter);
611 static int dso__load_perf_map(struct dso *self, struct map *map,
612 symbol_filter_t filter)
619 file = fopen(self->long_name, "r");
623 while (!feof(file)) {
628 line_len = getline(&line, &n, file);
635 line[--line_len] = '\0'; /* \n */
637 len = hex2u64(line, &start);
640 if (len + 2 >= line_len)
643 len += hex2u64(line + len, &size);
646 if (len + 2 >= line_len)
649 sym = symbol__new(start, size, line + len);
652 goto out_delete_line;
654 if (filter && filter(map, sym))
657 symbols__insert(&self->symbols[map->type], sym);
674 * elf_symtab__for_each_symbol - iterate thru all the symbols
676 * @self: struct elf_symtab instance to iterate
678 * @sym: GElf_Sym iterator
680 #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
681 for (idx = 0, gelf_getsym(syms, idx, &sym);\
683 idx++, gelf_getsym(syms, idx, &sym))
685 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
687 return GELF_ST_TYPE(sym->st_info);
690 static inline int elf_sym__is_function(const GElf_Sym *sym)
692 return elf_sym__type(sym) == STT_FUNC &&
694 sym->st_shndx != SHN_UNDEF;
697 static inline bool elf_sym__is_object(const GElf_Sym *sym)
699 return elf_sym__type(sym) == STT_OBJECT &&
701 sym->st_shndx != SHN_UNDEF;
704 static inline int elf_sym__is_label(const GElf_Sym *sym)
706 return elf_sym__type(sym) == STT_NOTYPE &&
708 sym->st_shndx != SHN_UNDEF &&
709 sym->st_shndx != SHN_ABS;
712 static inline const char *elf_sec__name(const GElf_Shdr *shdr,
713 const Elf_Data *secstrs)
715 return secstrs->d_buf + shdr->sh_name;
718 static inline int elf_sec__is_text(const GElf_Shdr *shdr,
719 const Elf_Data *secstrs)
721 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
724 static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
725 const Elf_Data *secstrs)
727 return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
730 static inline const char *elf_sym__name(const GElf_Sym *sym,
731 const Elf_Data *symstrs)
733 return symstrs->d_buf + sym->st_name;
736 static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
737 GElf_Shdr *shp, const char *name,
743 while ((sec = elf_nextscn(elf, sec)) != NULL) {
746 gelf_getshdr(sec, shp);
747 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
748 if (!strcmp(name, str)) {
759 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
760 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
762 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
764 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
765 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
767 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
770 * We need to check if we have a .dynsym, so that we can handle the
771 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
772 * .dynsym or .symtab).
773 * And always look at the original dso, not at debuginfo packages, that
774 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
776 static int dso__synthesize_plt_symbols(struct dso *self, struct map *map,
777 symbol_filter_t filter)
779 uint32_t nr_rel_entries, idx;
784 GElf_Shdr shdr_rel_plt, shdr_dynsym;
785 Elf_Data *reldata, *syms, *symstrs;
786 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
789 char sympltname[1024];
791 int nr = 0, symidx, fd, err = 0;
793 fd = open(self->long_name, O_RDONLY);
797 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
801 if (gelf_getehdr(elf, &ehdr) == NULL)
804 scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
805 ".dynsym", &dynsym_idx);
806 if (scn_dynsym == NULL)
809 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
811 if (scn_plt_rel == NULL) {
812 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
814 if (scn_plt_rel == NULL)
820 if (shdr_rel_plt.sh_link != dynsym_idx)
823 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
827 * Fetch the relocation section to find the idxes to the GOT
828 * and the symbols in the .dynsym they refer to.
830 reldata = elf_getdata(scn_plt_rel, NULL);
834 syms = elf_getdata(scn_dynsym, NULL);
838 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
839 if (scn_symstrs == NULL)
842 symstrs = elf_getdata(scn_symstrs, NULL);
846 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
847 plt_offset = shdr_plt.sh_offset;
849 if (shdr_rel_plt.sh_type == SHT_RELA) {
850 GElf_Rela pos_mem, *pos;
852 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
854 symidx = GELF_R_SYM(pos->r_info);
855 plt_offset += shdr_plt.sh_entsize;
856 gelf_getsym(syms, symidx, &sym);
857 snprintf(sympltname, sizeof(sympltname),
858 "%s@plt", elf_sym__name(&sym, symstrs));
860 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
865 if (filter && filter(map, f))
868 symbols__insert(&self->symbols[map->type], f);
872 } else if (shdr_rel_plt.sh_type == SHT_REL) {
873 GElf_Rel pos_mem, *pos;
874 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
876 symidx = GELF_R_SYM(pos->r_info);
877 plt_offset += shdr_plt.sh_entsize;
878 gelf_getsym(syms, symidx, &sym);
879 snprintf(sympltname, sizeof(sympltname),
880 "%s@plt", elf_sym__name(&sym, symstrs));
882 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
887 if (filter && filter(map, f))
890 symbols__insert(&self->symbols[map->type], f);
905 pr_debug("%s: problems reading %s PLT info.\n",
906 __func__, self->long_name);
910 static bool elf_sym__is_a(GElf_Sym *self, enum map_type type)
914 return elf_sym__is_function(self);
916 return elf_sym__is_object(self);
922 static bool elf_sec__is_a(GElf_Shdr *self, Elf_Data *secstrs, enum map_type type)
926 return elf_sec__is_text(self, secstrs);
928 return elf_sec__is_data(self, secstrs);
934 static int dso__load_sym(struct dso *self, struct map *map, const char *name,
935 int fd, symbol_filter_t filter, int kmodule)
937 struct kmap *kmap = self->kernel ? map__kmap(map) : NULL;
938 struct map *curr_map = map;
939 struct dso *curr_dso = self;
940 Elf_Data *symstrs, *secstrs;
948 Elf_Scn *sec, *sec_strndx;
952 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
954 pr_err("%s: cannot read %s ELF file.\n", __func__, name);
958 if (gelf_getehdr(elf, &ehdr) == NULL) {
959 pr_err("%s: cannot get elf header.\n", __func__);
963 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
965 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
970 syms = elf_getdata(sec, NULL);
974 sec = elf_getscn(elf, shdr.sh_link);
978 symstrs = elf_getdata(sec, NULL);
982 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
983 if (sec_strndx == NULL)
986 secstrs = elf_getdata(sec_strndx, NULL);
990 nr_syms = shdr.sh_size / shdr.sh_entsize;
992 memset(&sym, 0, sizeof(sym));
993 if (self->kernel == DSO_TYPE_USER) {
994 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
995 elf_section_by_name(elf, &ehdr, &shdr,
998 } else self->adjust_symbols = 0;
1000 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
1002 const char *elf_name = elf_sym__name(&sym, symstrs);
1003 char *demangled = NULL;
1004 int is_label = elf_sym__is_label(&sym);
1005 const char *section_name;
1007 if (kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name &&
1008 strcmp(elf_name, kmap->ref_reloc_sym->name) == 0)
1009 kmap->ref_reloc_sym->unrelocated_addr = sym.st_value;
1011 if (!is_label && !elf_sym__is_a(&sym, map->type))
1014 sec = elf_getscn(elf, sym.st_shndx);
1018 gelf_getshdr(sec, &shdr);
1020 if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type))
1023 section_name = elf_sec__name(&shdr, secstrs);
1025 if (self->kernel != DSO_TYPE_USER || kmodule) {
1026 char dso_name[PATH_MAX];
1028 if (strcmp(section_name,
1029 (curr_dso->short_name +
1030 self->short_name_len)) == 0)
1033 if (strcmp(section_name, ".text") == 0) {
1039 snprintf(dso_name, sizeof(dso_name),
1040 "%s%s", self->short_name, section_name);
1042 curr_map = map_groups__find_by_name(kmap->kmaps, map->type, dso_name);
1043 if (curr_map == NULL) {
1044 u64 start = sym.st_value;
1047 start += map->start + shdr.sh_offset;
1049 curr_dso = dso__new(dso_name);
1050 if (curr_dso == NULL)
1052 curr_dso->kernel = self->kernel;
1053 curr_map = map__new2(start, curr_dso,
1055 if (curr_map == NULL) {
1056 dso__delete(curr_dso);
1059 curr_map->map_ip = identity__map_ip;
1060 curr_map->unmap_ip = identity__map_ip;
1061 curr_dso->origin = self->origin;
1062 map_groups__insert(kmap->kmaps, curr_map);
1063 dsos__add(&self->node, curr_dso);
1064 dso__set_loaded(curr_dso, map->type);
1066 curr_dso = curr_map->dso;
1071 if (curr_dso->adjust_symbols) {
1072 pr_debug4("%s: adjusting symbol: st_value: %#Lx "
1073 "sh_addr: %#Lx sh_offset: %#Lx\n", __func__,
1074 (u64)sym.st_value, (u64)shdr.sh_addr,
1075 (u64)shdr.sh_offset);
1076 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
1079 * We need to figure out if the object was created from C++ sources
1080 * DWARF DW_compile_unit has this, but we don't always have access
1083 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
1084 if (demangled != NULL)
1085 elf_name = demangled;
1087 f = symbol__new(sym.st_value, sym.st_size, elf_name);
1092 if (filter && filter(curr_map, f))
1095 symbols__insert(&curr_dso->symbols[curr_map->type], f);
1101 * For misannotated, zeroed, ASM function sizes.
1104 symbols__fixup_end(&self->symbols[map->type]);
1107 * We need to fixup this here too because we create new
1108 * maps here, for things like vsyscall sections.
1110 __map_groups__fixup_end(kmap->kmaps, map->type);
1120 static bool dso__build_id_equal(const struct dso *self, u8 *build_id)
1122 return memcmp(self->build_id, build_id, sizeof(self->build_id)) == 0;
1125 bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
1127 bool have_build_id = false;
1130 list_for_each_entry(pos, head, node) {
1131 if (with_hits && !pos->hit)
1133 if (filename__read_build_id(pos->long_name, pos->build_id,
1134 sizeof(pos->build_id)) > 0) {
1135 have_build_id = true;
1136 pos->has_build_id = true;
1140 return have_build_id;
1144 * Align offset to 4 bytes as needed for note name and descriptor data.
1146 #define NOTE_ALIGN(n) (((n) + 3) & -4U)
1148 int filename__read_build_id(const char *filename, void *bf, size_t size)
1159 if (size < BUILD_ID_SIZE)
1162 fd = open(filename, O_RDONLY);
1166 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1168 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
1173 if (ek != ELF_K_ELF)
1176 if (gelf_getehdr(elf, &ehdr) == NULL) {
1177 pr_err("%s: cannot get elf header.\n", __func__);
1181 sec = elf_section_by_name(elf, &ehdr, &shdr,
1182 ".note.gnu.build-id", NULL);
1184 sec = elf_section_by_name(elf, &ehdr, &shdr,
1190 data = elf_getdata(sec, NULL);
1195 while (ptr < (data->d_buf + data->d_size)) {
1196 GElf_Nhdr *nhdr = ptr;
1197 int namesz = NOTE_ALIGN(nhdr->n_namesz),
1198 descsz = NOTE_ALIGN(nhdr->n_descsz);
1201 ptr += sizeof(*nhdr);
1204 if (nhdr->n_type == NT_GNU_BUILD_ID &&
1205 nhdr->n_namesz == sizeof("GNU")) {
1206 if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
1207 memcpy(bf, ptr, BUILD_ID_SIZE);
1208 err = BUILD_ID_SIZE;
1222 int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
1226 if (size < BUILD_ID_SIZE)
1229 fd = open(filename, O_RDONLY);
1238 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
1241 namesz = NOTE_ALIGN(nhdr.n_namesz);
1242 descsz = NOTE_ALIGN(nhdr.n_descsz);
1243 if (nhdr.n_type == NT_GNU_BUILD_ID &&
1244 nhdr.n_namesz == sizeof("GNU")) {
1245 if (read(fd, bf, namesz) != namesz)
1247 if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
1248 if (read(fd, build_id,
1249 BUILD_ID_SIZE) == BUILD_ID_SIZE) {
1253 } else if (read(fd, bf, descsz) != descsz)
1256 int n = namesz + descsz;
1257 if (read(fd, bf, n) != n)
1266 char dso__symtab_origin(const struct dso *self)
1268 static const char origin[] = {
1269 [DSO__ORIG_KERNEL] = 'k',
1270 [DSO__ORIG_JAVA_JIT] = 'j',
1271 [DSO__ORIG_BUILD_ID_CACHE] = 'B',
1272 [DSO__ORIG_FEDORA] = 'f',
1273 [DSO__ORIG_UBUNTU] = 'u',
1274 [DSO__ORIG_BUILDID] = 'b',
1275 [DSO__ORIG_DSO] = 'd',
1276 [DSO__ORIG_KMODULE] = 'K',
1277 [DSO__ORIG_GUEST_KERNEL] = 'g',
1278 [DSO__ORIG_GUEST_KMODULE] = 'G',
1281 if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
1283 return origin[self->origin];
1286 int dso__load(struct dso *self, struct map *map, symbol_filter_t filter)
1288 int size = PATH_MAX;
1290 u8 build_id[BUILD_ID_SIZE];
1291 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
1294 struct machine *machine;
1295 const char *root_dir;
1297 dso__set_loaded(self, map->type);
1299 if (self->kernel == DSO_TYPE_KERNEL)
1300 return dso__load_kernel_sym(self, map, filter);
1301 else if (self->kernel == DSO_TYPE_GUEST_KERNEL)
1302 return dso__load_guest_kernel_sym(self, map, filter);
1304 if (map->groups && map->groups->machine)
1305 machine = map->groups->machine;
1309 name = malloc(size);
1313 self->adjust_symbols = 0;
1315 if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
1316 ret = dso__load_perf_map(self, map, filter);
1317 self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
1318 DSO__ORIG_NOT_FOUND;
1322 self->origin = DSO__ORIG_BUILD_ID_CACHE;
1324 if (self->has_build_id) {
1325 build_id__sprintf(self->build_id, sizeof(self->build_id),
1327 snprintf(name, size, "%s/%s/.build-id/%.2s/%s",
1328 getenv("HOME"), DEBUG_CACHE_DIR,
1329 build_id_hex, build_id_hex + 2);
1335 switch (self->origin) {
1336 case DSO__ORIG_FEDORA:
1337 snprintf(name, size, "/usr/lib/debug%s.debug",
1340 case DSO__ORIG_UBUNTU:
1341 snprintf(name, size, "/usr/lib/debug%s",
1344 case DSO__ORIG_BUILDID:
1345 if (filename__read_build_id(self->long_name, build_id,
1346 sizeof(build_id))) {
1347 build_id__sprintf(build_id, sizeof(build_id),
1349 snprintf(name, size,
1350 "/usr/lib/debug/.build-id/%.2s/%s.debug",
1351 build_id_hex, build_id_hex + 2);
1352 if (self->has_build_id)
1353 goto compare_build_id;
1359 snprintf(name, size, "%s", self->long_name);
1361 case DSO__ORIG_GUEST_KMODULE:
1362 if (map->groups && map->groups->machine)
1363 root_dir = map->groups->machine->root_dir;
1366 snprintf(name, size, "%s%s", root_dir, self->long_name);
1373 if (self->has_build_id) {
1374 if (filename__read_build_id(name, build_id,
1375 sizeof(build_id)) < 0)
1378 if (!dso__build_id_equal(self, build_id))
1382 fd = open(name, O_RDONLY);
1385 ret = dso__load_sym(self, map, name, fd, filter, 0);
1389 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
1395 int nr_plt = dso__synthesize_plt_symbols(self, map, filter);
1401 if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
1406 struct map *map_groups__find_by_name(struct map_groups *self,
1407 enum map_type type, const char *name)
1411 for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) {
1412 struct map *map = rb_entry(nd, struct map, rb_node);
1414 if (map->dso && strcmp(map->dso->short_name, name) == 0)
1421 static int dso__kernel_module_get_build_id(struct dso *self,
1422 const char *root_dir)
1424 char filename[PATH_MAX];
1426 * kernel module short names are of the form "[module]" and
1427 * we need just "module" here.
1429 const char *name = self->short_name + 1;
1431 snprintf(filename, sizeof(filename),
1432 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1433 root_dir, (int)strlen(name) - 1, name);
1435 if (sysfs__read_build_id(filename, self->build_id,
1436 sizeof(self->build_id)) == 0)
1437 self->has_build_id = true;
1442 static int map_groups__set_modules_path_dir(struct map_groups *self,
1443 const char *dir_name)
1445 struct dirent *dent;
1446 DIR *dir = opendir(dir_name);
1449 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
1453 while ((dent = readdir(dir)) != NULL) {
1454 char path[PATH_MAX];
1457 /*sshfs might return bad dent->d_type, so we have to stat*/
1458 sprintf(path, "%s/%s", dir_name, dent->d_name);
1459 if (stat(path, &st))
1462 if (S_ISDIR(st.st_mode)) {
1463 if (!strcmp(dent->d_name, ".") ||
1464 !strcmp(dent->d_name, ".."))
1467 snprintf(path, sizeof(path), "%s/%s",
1468 dir_name, dent->d_name);
1469 if (map_groups__set_modules_path_dir(self, path) < 0)
1472 char *dot = strrchr(dent->d_name, '.'),
1477 if (dot == NULL || strcmp(dot, ".ko"))
1479 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1480 (int)(dot - dent->d_name), dent->d_name);
1482 strxfrchar(dso_name, '-', '_');
1483 map = map_groups__find_by_name(self, MAP__FUNCTION, dso_name);
1487 snprintf(path, sizeof(path), "%s/%s",
1488 dir_name, dent->d_name);
1490 long_name = strdup(path);
1491 if (long_name == NULL)
1493 dso__set_long_name(map->dso, long_name);
1494 dso__kernel_module_get_build_id(map->dso, "");
1504 static char *get_kernel_version(const char *root_dir)
1506 char version[PATH_MAX];
1509 const char *prefix = "Linux version ";
1511 sprintf(version, "%s/proc/version", root_dir);
1512 file = fopen(version, "r");
1517 tmp = fgets(version, sizeof(version), file);
1520 name = strstr(version, prefix);
1523 name += strlen(prefix);
1524 tmp = strchr(name, ' ');
1528 return strdup(name);
1531 static int map_groups__set_modules_path(struct map_groups *self,
1532 const char *root_dir)
1535 char modules_path[PATH_MAX];
1537 version = get_kernel_version(root_dir);
1541 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s/kernel",
1545 return map_groups__set_modules_path_dir(self, 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 *map_groups__new_module(struct map_groups *self, u64 start,
1568 const char *filename,
1569 struct machine *machine)
1574 dso = __dsos__findnew(&machine->kernel_dsos, filename);
1578 map = map__new2(start, dso, MAP__FUNCTION);
1582 if (machine__is_host(machine))
1583 dso->origin = DSO__ORIG_KMODULE;
1585 dso->origin = DSO__ORIG_GUEST_KMODULE;
1586 map_groups__insert(self, map);
1590 static int map_groups__create_modules(struct machine *machine)
1596 const char *root_dir;
1597 const char *modules;
1598 char path[PATH_MAX];
1600 if (machine__is_default_guest(machine))
1601 modules = symbol_conf.default_guest_modules;
1603 sprintf(path, "%s/proc/modules", machine->root_dir);
1607 file = fopen(modules, "r");
1611 root_dir = machine->root_dir;
1613 while (!feof(file)) {
1614 char name[PATH_MAX];
1619 line_len = getline(&line, &n, file);
1626 line[--line_len] = '\0'; /* \n */
1628 sep = strrchr(line, 'x');
1632 hex2u64(sep + 1, &start);
1634 sep = strchr(line, ' ');
1640 snprintf(name, sizeof(name), "[%s]", line);
1641 map = map_groups__new_module(&machine->kmaps, start,
1644 goto out_delete_line;
1645 dso__kernel_module_get_build_id(map->dso, root_dir);
1651 return map_groups__set_modules_path(&machine->kmaps, root_dir);
1659 static int dso__load_vmlinux(struct dso *self, struct map *map,
1660 const char *vmlinux, symbol_filter_t filter)
1664 if (self->has_build_id) {
1665 u8 build_id[BUILD_ID_SIZE];
1667 if (filename__read_build_id(vmlinux, build_id,
1668 sizeof(build_id)) < 0) {
1669 pr_debug("No build_id in %s, ignoring it\n", vmlinux);
1672 if (!dso__build_id_equal(self, build_id)) {
1673 char expected_build_id[BUILD_ID_SIZE * 2 + 1],
1674 vmlinux_build_id[BUILD_ID_SIZE * 2 + 1];
1676 build_id__sprintf(self->build_id,
1677 sizeof(self->build_id),
1679 build_id__sprintf(build_id, sizeof(build_id),
1681 pr_debug("build_id in %s is %s while expected is %s, "
1682 "ignoring it\n", vmlinux, vmlinux_build_id,
1688 fd = open(vmlinux, O_RDONLY);
1692 dso__set_loaded(self, map->type);
1693 err = dso__load_sym(self, map, vmlinux, fd, filter, 0);
1697 pr_debug("Using %s for symbols\n", vmlinux);
1702 int dso__load_vmlinux_path(struct dso *self, struct map *map,
1703 symbol_filter_t filter)
1707 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1708 vmlinux_path__nr_entries);
1710 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
1711 err = dso__load_vmlinux(self, map, vmlinux_path[i], filter);
1713 dso__set_long_name(self, strdup(vmlinux_path[i]));
1721 static int dso__load_kernel_sym(struct dso *self, struct map *map,
1722 symbol_filter_t filter)
1725 const char *kallsyms_filename = NULL;
1726 char *kallsyms_allocated_filename = NULL;
1728 * Step 1: if the user specified a vmlinux filename, use it and only
1729 * it, reporting errors to the user if it cannot be used.
1731 * For instance, try to analyse an ARM perf.data file _without_ a
1732 * build-id, or if the user specifies the wrong path to the right
1733 * vmlinux file, obviously we can't fallback to another vmlinux (a
1734 * x86_86 one, on the machine where analysis is being performed, say),
1735 * or worse, /proc/kallsyms.
1737 * If the specified file _has_ a build-id and there is a build-id
1738 * section in the perf.data file, we will still do the expected
1739 * validation in dso__load_vmlinux and will bail out if they don't
1742 if (symbol_conf.vmlinux_name != NULL) {
1743 err = dso__load_vmlinux(self, map,
1744 symbol_conf.vmlinux_name, filter);
1748 if (vmlinux_path != NULL) {
1749 err = dso__load_vmlinux_path(self, map, filter);
1755 * Say the kernel DSO was created when processing the build-id header table,
1756 * we have a build-id, so check if it is the same as the running kernel,
1757 * using it if it is.
1759 if (self->has_build_id) {
1760 u8 kallsyms_build_id[BUILD_ID_SIZE];
1761 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1763 if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id,
1764 sizeof(kallsyms_build_id)) == 0) {
1765 if (dso__build_id_equal(self, kallsyms_build_id)) {
1766 kallsyms_filename = "/proc/kallsyms";
1771 * Now look if we have it on the build-id cache in
1772 * $HOME/.debug/[kernel.kallsyms].
1774 build_id__sprintf(self->build_id, sizeof(self->build_id),
1777 if (asprintf(&kallsyms_allocated_filename,
1778 "%s/.debug/[kernel.kallsyms]/%s",
1779 getenv("HOME"), sbuild_id) == -1) {
1780 pr_err("Not enough memory for kallsyms file lookup\n");
1784 kallsyms_filename = kallsyms_allocated_filename;
1786 if (access(kallsyms_filename, F_OK)) {
1787 pr_err("No kallsyms or vmlinux with build-id %s "
1788 "was found\n", sbuild_id);
1789 free(kallsyms_allocated_filename);
1794 * Last resort, if we don't have a build-id and couldn't find
1795 * any vmlinux file, try the running kernel kallsyms table.
1797 kallsyms_filename = "/proc/kallsyms";
1801 err = dso__load_kallsyms(self, kallsyms_filename, map, filter);
1803 pr_debug("Using %s for symbols\n", kallsyms_filename);
1804 free(kallsyms_allocated_filename);
1809 if (kallsyms_filename != NULL)
1810 dso__set_long_name(self, strdup("[kernel.kallsyms]"));
1811 map__fixup_start(map);
1812 map__fixup_end(map);
1818 static int dso__load_guest_kernel_sym(struct dso *self, struct map *map,
1819 symbol_filter_t filter)
1822 const char *kallsyms_filename = NULL;
1823 struct machine *machine;
1824 char path[PATH_MAX];
1827 pr_debug("Guest kernel map hasn't the point to groups\n");
1830 machine = map->groups->machine;
1832 if (machine__is_default_guest(machine)) {
1834 * if the user specified a vmlinux filename, use it and only
1835 * it, reporting errors to the user if it cannot be used.
1836 * Or use file guest_kallsyms inputted by user on commandline
1838 if (symbol_conf.default_guest_vmlinux_name != NULL) {
1839 err = dso__load_vmlinux(self, map,
1840 symbol_conf.default_guest_vmlinux_name, filter);
1844 kallsyms_filename = symbol_conf.default_guest_kallsyms;
1845 if (!kallsyms_filename)
1848 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
1849 kallsyms_filename = path;
1852 err = dso__load_kallsyms(self, kallsyms_filename, map, filter);
1854 pr_debug("Using %s for symbols\n", kallsyms_filename);
1858 if (kallsyms_filename != NULL) {
1859 machine__mmap_name(machine, path, sizeof(path));
1860 dso__set_long_name(self, strdup(path));
1862 map__fixup_start(map);
1863 map__fixup_end(map);
1869 static void dsos__add(struct list_head *head, struct dso *dso)
1871 list_add_tail(&dso->node, head);
1874 static struct dso *dsos__find(struct list_head *head, const char *name)
1878 list_for_each_entry(pos, head, node)
1879 if (strcmp(pos->long_name, name) == 0)
1884 struct dso *__dsos__findnew(struct list_head *head, const char *name)
1886 struct dso *dso = dsos__find(head, name);
1889 dso = dso__new(name);
1891 dsos__add(head, dso);
1892 dso__set_basename(dso);
1899 static void __dsos__fprintf(struct list_head *head, FILE *fp)
1903 list_for_each_entry(pos, head, node) {
1905 for (i = 0; i < MAP__NR_TYPES; ++i)
1906 dso__fprintf(pos, i, fp);
1910 void dsos__fprintf(struct rb_root *machines, FILE *fp)
1914 for (nd = rb_first(machines); nd; nd = rb_next(nd)) {
1915 struct machine *pos = rb_entry(nd, struct machine, rb_node);
1916 __dsos__fprintf(&pos->kernel_dsos, fp);
1917 __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 dsos__fprintf_buildid(struct rb_root *machines, FILE *fp, bool with_hits)
1941 for (nd = rb_first(machines); 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 *dsos__create_kernel(struct machine *machine)
1989 const char *vmlinux_name = NULL;
1992 if (machine__is_host(machine)) {
1993 vmlinux_name = symbol_conf.vmlinux_name;
1994 kernel = dso__new_kernel(vmlinux_name);
1996 if (machine__is_default_guest(machine))
1997 vmlinux_name = symbol_conf.default_guest_vmlinux_name;
1998 kernel = dso__new_guest_kernel(machine, vmlinux_name);
2001 if (kernel != NULL) {
2002 dso__read_running_kernel_build_id(kernel, machine);
2003 dsos__add(&machine->kernel_dsos, kernel);
2008 int __map_groups__create_kernel_maps(struct map_groups *self,
2009 struct map *vmlinux_maps[MAP__NR_TYPES],
2014 for (type = 0; type < MAP__NR_TYPES; ++type) {
2017 vmlinux_maps[type] = map__new2(0, kernel, type);
2018 if (vmlinux_maps[type] == NULL)
2021 vmlinux_maps[type]->map_ip =
2022 vmlinux_maps[type]->unmap_ip = identity__map_ip;
2024 kmap = map__kmap(vmlinux_maps[type]);
2026 map_groups__insert(self, vmlinux_maps[type]);
2032 static void vmlinux_path__exit(void)
2034 while (--vmlinux_path__nr_entries >= 0) {
2035 free(vmlinux_path[vmlinux_path__nr_entries]);
2036 vmlinux_path[vmlinux_path__nr_entries] = NULL;
2040 vmlinux_path = NULL;
2043 static int vmlinux_path__init(void)
2048 if (uname(&uts) < 0)
2051 vmlinux_path = malloc(sizeof(char *) * 5);
2052 if (vmlinux_path == NULL)
2055 vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
2056 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2058 ++vmlinux_path__nr_entries;
2059 vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
2060 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2062 ++vmlinux_path__nr_entries;
2063 snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
2064 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
2065 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2067 ++vmlinux_path__nr_entries;
2068 snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
2069 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
2070 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2072 ++vmlinux_path__nr_entries;
2073 snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
2075 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
2076 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2078 ++vmlinux_path__nr_entries;
2083 vmlinux_path__exit();
2087 size_t vmlinux_path__fprintf(FILE *fp)
2092 for (i = 0; i < vmlinux_path__nr_entries; ++i)
2093 printed += fprintf(fp, "[%d] %s\n", i, vmlinux_path[i]);
2098 static int setup_list(struct strlist **list, const char *list_str,
2099 const char *list_name)
2101 if (list_str == NULL)
2104 *list = strlist__new(true, list_str);
2106 pr_err("problems parsing %s list\n", list_name);
2112 int symbol__init(void)
2114 elf_version(EV_CURRENT);
2115 if (symbol_conf.sort_by_name)
2116 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
2117 sizeof(struct symbol));
2119 if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
2122 if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
2123 pr_err("'.' is the only non valid --field-separator argument\n");
2127 if (setup_list(&symbol_conf.dso_list,
2128 symbol_conf.dso_list_str, "dso") < 0)
2131 if (setup_list(&symbol_conf.comm_list,
2132 symbol_conf.comm_list_str, "comm") < 0)
2133 goto out_free_dso_list;
2135 if (setup_list(&symbol_conf.sym_list,
2136 symbol_conf.sym_list_str, "symbol") < 0)
2137 goto out_free_comm_list;
2142 strlist__delete(symbol_conf.dso_list);
2144 strlist__delete(symbol_conf.comm_list);
2148 int map_groups__create_kernel_maps(struct rb_root *machines, pid_t pid)
2151 struct machine *machine = machines__findnew(machines, pid);
2153 if (machine == NULL)
2155 kernel = dsos__create_kernel(machine);
2159 if (__map_groups__create_kernel_maps(&machine->kmaps,
2160 machine->vmlinux_maps, kernel) < 0)
2163 if (symbol_conf.use_modules &&
2164 map_groups__create_modules(machine) < 0)
2165 pr_debug("Problems creating module maps, continuing anyway...\n");
2167 * Now that we have all the maps created, just set the ->end of them:
2169 map_groups__fixup_end(&machine->kmaps);
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 map_groups__create_guest_kernel_maps(struct rb_root *machines)
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 map_groups__create_kernel_maps(machines, 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 map_groups__create_kernel_maps(machines, pid);