11 #include <sys/param.h>
23 #include <sys/utsname.h>
25 #ifndef NT_GNU_BUILD_ID
26 #define NT_GNU_BUILD_ID 3
29 static bool dso__build_id_equal(const struct dso *self, u8 *build_id);
30 static int elf_read_build_id(Elf *elf, void *bf, size_t size);
31 static void dsos__add(struct list_head *head, struct dso *dso);
32 static struct map *map__new2(u64 start, struct dso *dso, enum map_type type);
33 static int dso__load_kernel_sym(struct dso *self, struct map *map,
34 symbol_filter_t filter);
35 static int dso__load_guest_kernel_sym(struct dso *self, struct map *map,
36 symbol_filter_t filter);
37 static int vmlinux_path__nr_entries;
38 static char **vmlinux_path;
40 struct symbol_conf symbol_conf = {
41 .exclude_other = true,
43 .try_vmlinux_path = true,
46 int dso__name_len(const struct dso *self)
49 return self->long_name_len;
51 return self->short_name_len;
54 bool dso__loaded(const struct dso *self, enum map_type type)
56 return self->loaded & (1 << type);
59 bool dso__sorted_by_name(const struct dso *self, enum map_type type)
61 return self->sorted_by_name & (1 << type);
64 static void dso__set_sorted_by_name(struct dso *self, enum map_type type)
66 self->sorted_by_name |= (1 << type);
69 bool symbol_type__is_a(char symbol_type, enum map_type map_type)
73 return symbol_type == 'T' || symbol_type == 'W';
75 return symbol_type == 'D' || symbol_type == 'd';
81 static void symbols__fixup_end(struct rb_root *self)
83 struct rb_node *nd, *prevnd = rb_first(self);
84 struct symbol *curr, *prev;
89 curr = rb_entry(prevnd, struct symbol, rb_node);
91 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
93 curr = rb_entry(nd, struct symbol, rb_node);
95 if (prev->end == prev->start)
96 prev->end = curr->start - 1;
100 if (curr->end == curr->start)
101 curr->end = roundup(curr->start, 4096);
104 static void __map_groups__fixup_end(struct map_groups *self, enum map_type type)
106 struct map *prev, *curr;
107 struct rb_node *nd, *prevnd = rb_first(&self->maps[type]);
112 curr = rb_entry(prevnd, struct map, rb_node);
114 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
116 curr = rb_entry(nd, struct map, rb_node);
117 prev->end = curr->start - 1;
121 * We still haven't the actual symbols, so guess the
122 * last map final address.
127 static void map_groups__fixup_end(struct map_groups *self)
130 for (i = 0; i < MAP__NR_TYPES; ++i)
131 __map_groups__fixup_end(self, i);
134 static struct symbol *symbol__new(u64 start, u64 len, const char *name)
136 size_t namelen = strlen(name) + 1;
137 struct symbol *self = calloc(1, (symbol_conf.priv_size +
138 sizeof(*self) + namelen));
142 if (symbol_conf.priv_size)
143 self = ((void *)self) + symbol_conf.priv_size;
146 self->end = len ? start + len - 1 : start;
147 self->namelen = namelen - 1;
149 pr_debug4("%s: %s %#Lx-%#Lx\n", __func__, name, start, self->end);
151 memcpy(self->name, name, namelen);
156 void symbol__delete(struct symbol *self)
158 free(((void *)self) - symbol_conf.priv_size);
161 static size_t symbol__fprintf(struct symbol *self, FILE *fp)
163 return fprintf(fp, " %llx-%llx %s\n",
164 self->start, self->end, self->name);
167 void dso__set_long_name(struct dso *self, char *name)
171 self->long_name = name;
172 self->long_name_len = strlen(name);
175 static void dso__set_short_name(struct dso *self, const char *name)
179 self->short_name = name;
180 self->short_name_len = strlen(name);
183 static void dso__set_basename(struct dso *self)
185 dso__set_short_name(self, basename(self->long_name));
188 struct dso *dso__new(const char *name)
190 struct dso *self = calloc(1, sizeof(*self) + strlen(name) + 1);
194 strcpy(self->name, name);
195 dso__set_long_name(self, self->name);
196 dso__set_short_name(self, self->name);
197 for (i = 0; i < MAP__NR_TYPES; ++i)
198 self->symbols[i] = self->symbol_names[i] = RB_ROOT;
199 self->slen_calculated = 0;
200 self->origin = DSO__ORIG_NOT_FOUND;
202 self->sorted_by_name = 0;
203 self->has_build_id = 0;
204 self->kernel = DSO_TYPE_USER;
205 INIT_LIST_HEAD(&self->node);
211 static void symbols__delete(struct rb_root *self)
214 struct rb_node *next = rb_first(self);
217 pos = rb_entry(next, struct symbol, rb_node);
218 next = rb_next(&pos->rb_node);
219 rb_erase(&pos->rb_node, self);
224 void dso__delete(struct dso *self)
227 for (i = 0; i < MAP__NR_TYPES; ++i)
228 symbols__delete(&self->symbols[i]);
229 if (self->sname_alloc)
230 free((char *)self->short_name);
231 if (self->lname_alloc)
232 free(self->long_name);
236 void dso__set_build_id(struct dso *self, void *build_id)
238 memcpy(self->build_id, build_id, sizeof(self->build_id));
239 self->has_build_id = 1;
242 static void symbols__insert(struct rb_root *self, struct symbol *sym)
244 struct rb_node **p = &self->rb_node;
245 struct rb_node *parent = NULL;
246 const u64 ip = sym->start;
251 s = rb_entry(parent, struct symbol, rb_node);
257 rb_link_node(&sym->rb_node, parent, p);
258 rb_insert_color(&sym->rb_node, self);
261 static struct symbol *symbols__find(struct rb_root *self, u64 ip)
271 struct symbol *s = rb_entry(n, struct symbol, rb_node);
275 else if (ip > s->end)
284 struct symbol_name_rb_node {
285 struct rb_node rb_node;
289 static void symbols__insert_by_name(struct rb_root *self, struct symbol *sym)
291 struct rb_node **p = &self->rb_node;
292 struct rb_node *parent = NULL;
293 struct symbol_name_rb_node *symn = ((void *)sym) - sizeof(*parent), *s;
297 s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
298 if (strcmp(sym->name, s->sym.name) < 0)
303 rb_link_node(&symn->rb_node, parent, p);
304 rb_insert_color(&symn->rb_node, self);
307 static void symbols__sort_by_name(struct rb_root *self, struct rb_root *source)
311 for (nd = rb_first(source); nd; nd = rb_next(nd)) {
312 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
313 symbols__insert_by_name(self, pos);
317 static struct symbol *symbols__find_by_name(struct rb_root *self, const char *name)
327 struct symbol_name_rb_node *s;
330 s = rb_entry(n, struct symbol_name_rb_node, rb_node);
331 cmp = strcmp(name, s->sym.name);
344 struct symbol *dso__find_symbol(struct dso *self,
345 enum map_type type, u64 addr)
347 return symbols__find(&self->symbols[type], addr);
350 struct symbol *dso__find_symbol_by_name(struct dso *self, enum map_type type,
353 return symbols__find_by_name(&self->symbol_names[type], name);
356 void dso__sort_by_name(struct dso *self, enum map_type type)
358 dso__set_sorted_by_name(self, type);
359 return symbols__sort_by_name(&self->symbol_names[type],
360 &self->symbols[type]);
363 int build_id__sprintf(const u8 *self, int len, char *bf)
366 const u8 *raw = self;
369 for (i = 0; i < len; ++i) {
370 sprintf(bid, "%02x", *raw);
378 size_t dso__fprintf_buildid(struct dso *self, FILE *fp)
380 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
382 build_id__sprintf(self->build_id, sizeof(self->build_id), sbuild_id);
383 return fprintf(fp, "%s", sbuild_id);
386 size_t dso__fprintf(struct dso *self, enum map_type type, FILE *fp)
389 size_t ret = fprintf(fp, "dso: %s (", self->short_name);
391 if (self->short_name != self->long_name)
392 ret += fprintf(fp, "%s, ", self->long_name);
393 ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
394 self->loaded ? "" : "NOT ");
395 ret += dso__fprintf_buildid(self, fp);
396 ret += fprintf(fp, ")\n");
397 for (nd = rb_first(&self->symbols[type]); nd; nd = rb_next(nd)) {
398 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
399 ret += symbol__fprintf(pos, fp);
405 int kallsyms__parse(const char *filename, void *arg,
406 int (*process_symbol)(void *arg, const char *name,
407 char type, u64 start))
412 FILE *file = fopen(filename, "r");
417 while (!feof(file)) {
423 line_len = getline(&line, &n, file);
424 if (line_len < 0 || !line)
427 line[--line_len] = '\0'; /* \n */
429 len = hex2u64(line, &start);
432 if (len + 2 >= line_len)
435 symbol_type = toupper(line[len]);
436 symbol_name = line + len + 2;
438 err = process_symbol(arg, symbol_name, symbol_type, start);
451 struct process_kallsyms_args {
456 static int map__process_kallsym_symbol(void *arg, const char *name,
457 char type, u64 start)
460 struct process_kallsyms_args *a = arg;
461 struct rb_root *root = &a->dso->symbols[a->map->type];
463 if (!symbol_type__is_a(type, a->map->type))
467 * Will fix up the end later, when we have all symbols sorted.
469 sym = symbol__new(start, 0, name);
474 * We will pass the symbols to the filter later, in
475 * map__split_kallsyms, when we have split the maps per module
477 symbols__insert(root, sym);
483 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
484 * so that we can in the next step set the symbol ->end address and then
485 * call kernel_maps__split_kallsyms.
487 static int dso__load_all_kallsyms(struct dso *self, const char *filename,
490 struct process_kallsyms_args args = { .map = map, .dso = self, };
491 return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
495 * Split the symbols into maps, making sure there are no overlaps, i.e. the
496 * kernel range is broken in several maps, named [kernel].N, as we don't have
497 * the original ELF section names vmlinux have.
499 static int dso__split_kallsyms(struct dso *self, struct map *map,
500 symbol_filter_t filter)
502 struct map_groups *kmaps = map__kmap(map)->kmaps;
503 struct machine *machine = kmaps->machine;
504 struct map *curr_map = map;
507 struct rb_root *root = &self->symbols[map->type];
508 struct rb_node *next = rb_first(root);
509 int kernel_range = 0;
514 pos = rb_entry(next, struct symbol, rb_node);
515 next = rb_next(&pos->rb_node);
517 module = strchr(pos->name, '\t');
519 if (!symbol_conf.use_modules)
524 if (strcmp(curr_map->dso->short_name, module)) {
525 if (curr_map != map &&
526 self->kernel == DSO_TYPE_GUEST_KERNEL &&
527 machine__is_default_guest(machine)) {
529 * We assume all symbols of a module are
530 * continuous in * kallsyms, so curr_map
531 * points to a module and all its
532 * symbols are in its kmap. Mark it as
535 dso__set_loaded(curr_map->dso,
539 curr_map = map_groups__find_by_name(kmaps,
541 if (curr_map == NULL) {
542 pr_debug("%s/proc/{kallsyms,modules} "
543 "inconsistency while looking "
544 "for \"%s\" module!\n",
545 machine->root_dir, module);
550 if (curr_map->dso->loaded &&
551 !machine__is_default_guest(machine))
555 * So that we look just like we get from .ko files,
556 * i.e. not prelinked, relative to map->start.
558 pos->start = curr_map->map_ip(curr_map, pos->start);
559 pos->end = curr_map->map_ip(curr_map, pos->end);
560 } else if (curr_map != map) {
561 char dso_name[PATH_MAX];
564 if (self->kernel == DSO_TYPE_GUEST_KERNEL)
565 snprintf(dso_name, sizeof(dso_name),
569 snprintf(dso_name, sizeof(dso_name),
573 dso = dso__new(dso_name);
577 dso->kernel = self->kernel;
579 curr_map = map__new2(pos->start, dso, map->type);
580 if (curr_map == NULL) {
585 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
586 map_groups__insert(kmaps, curr_map);
590 if (filter && filter(curr_map, pos)) {
591 discard_symbol: rb_erase(&pos->rb_node, root);
594 if (curr_map != map) {
595 rb_erase(&pos->rb_node, root);
596 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
602 if (curr_map != map &&
603 self->kernel == DSO_TYPE_GUEST_KERNEL &&
604 machine__is_default_guest(kmaps->machine)) {
605 dso__set_loaded(curr_map->dso, curr_map->type);
611 int dso__load_kallsyms(struct dso *self, const char *filename,
612 struct map *map, symbol_filter_t filter)
614 if (dso__load_all_kallsyms(self, filename, map) < 0)
617 symbols__fixup_end(&self->symbols[map->type]);
618 if (self->kernel == DSO_TYPE_GUEST_KERNEL)
619 self->origin = DSO__ORIG_GUEST_KERNEL;
621 self->origin = DSO__ORIG_KERNEL;
623 return dso__split_kallsyms(self, map, filter);
626 static int dso__load_perf_map(struct dso *self, struct map *map,
627 symbol_filter_t filter)
634 file = fopen(self->long_name, "r");
638 while (!feof(file)) {
643 line_len = getline(&line, &n, file);
650 line[--line_len] = '\0'; /* \n */
652 len = hex2u64(line, &start);
655 if (len + 2 >= line_len)
658 len += hex2u64(line + len, &size);
661 if (len + 2 >= line_len)
664 sym = symbol__new(start, size, line + len);
667 goto out_delete_line;
669 if (filter && filter(map, sym))
672 symbols__insert(&self->symbols[map->type], sym);
689 * elf_symtab__for_each_symbol - iterate thru all the symbols
691 * @self: struct elf_symtab instance to iterate
693 * @sym: GElf_Sym iterator
695 #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
696 for (idx = 0, gelf_getsym(syms, idx, &sym);\
698 idx++, gelf_getsym(syms, idx, &sym))
700 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
702 return GELF_ST_TYPE(sym->st_info);
705 static inline int elf_sym__is_function(const GElf_Sym *sym)
707 return elf_sym__type(sym) == STT_FUNC &&
709 sym->st_shndx != SHN_UNDEF;
712 static inline bool elf_sym__is_object(const GElf_Sym *sym)
714 return elf_sym__type(sym) == STT_OBJECT &&
716 sym->st_shndx != SHN_UNDEF;
719 static inline int elf_sym__is_label(const GElf_Sym *sym)
721 return elf_sym__type(sym) == STT_NOTYPE &&
723 sym->st_shndx != SHN_UNDEF &&
724 sym->st_shndx != SHN_ABS;
727 static inline const char *elf_sec__name(const GElf_Shdr *shdr,
728 const Elf_Data *secstrs)
730 return secstrs->d_buf + shdr->sh_name;
733 static inline int elf_sec__is_text(const GElf_Shdr *shdr,
734 const Elf_Data *secstrs)
736 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
739 static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
740 const Elf_Data *secstrs)
742 return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
745 static inline const char *elf_sym__name(const GElf_Sym *sym,
746 const Elf_Data *symstrs)
748 return symstrs->d_buf + sym->st_name;
751 static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
752 GElf_Shdr *shp, const char *name,
758 while ((sec = elf_nextscn(elf, sec)) != NULL) {
761 gelf_getshdr(sec, shp);
762 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
763 if (!strcmp(name, str)) {
774 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
775 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
777 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
779 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
780 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
782 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
785 * We need to check if we have a .dynsym, so that we can handle the
786 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
787 * .dynsym or .symtab).
788 * And always look at the original dso, not at debuginfo packages, that
789 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
791 static int dso__synthesize_plt_symbols(struct dso *self, struct map *map,
792 symbol_filter_t filter)
794 uint32_t nr_rel_entries, idx;
799 GElf_Shdr shdr_rel_plt, shdr_dynsym;
800 Elf_Data *reldata, *syms, *symstrs;
801 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
804 char sympltname[1024];
806 int nr = 0, symidx, fd, err = 0;
808 fd = open(self->long_name, O_RDONLY);
812 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
816 if (gelf_getehdr(elf, &ehdr) == NULL)
819 scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
820 ".dynsym", &dynsym_idx);
821 if (scn_dynsym == NULL)
824 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
826 if (scn_plt_rel == NULL) {
827 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
829 if (scn_plt_rel == NULL)
835 if (shdr_rel_plt.sh_link != dynsym_idx)
838 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
842 * Fetch the relocation section to find the idxes to the GOT
843 * and the symbols in the .dynsym they refer to.
845 reldata = elf_getdata(scn_plt_rel, NULL);
849 syms = elf_getdata(scn_dynsym, NULL);
853 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
854 if (scn_symstrs == NULL)
857 symstrs = elf_getdata(scn_symstrs, NULL);
861 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
862 plt_offset = shdr_plt.sh_offset;
864 if (shdr_rel_plt.sh_type == SHT_RELA) {
865 GElf_Rela pos_mem, *pos;
867 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
869 symidx = GELF_R_SYM(pos->r_info);
870 plt_offset += shdr_plt.sh_entsize;
871 gelf_getsym(syms, symidx, &sym);
872 snprintf(sympltname, sizeof(sympltname),
873 "%s@plt", elf_sym__name(&sym, symstrs));
875 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
880 if (filter && filter(map, f))
883 symbols__insert(&self->symbols[map->type], f);
887 } else if (shdr_rel_plt.sh_type == SHT_REL) {
888 GElf_Rel pos_mem, *pos;
889 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
891 symidx = GELF_R_SYM(pos->r_info);
892 plt_offset += shdr_plt.sh_entsize;
893 gelf_getsym(syms, symidx, &sym);
894 snprintf(sympltname, sizeof(sympltname),
895 "%s@plt", elf_sym__name(&sym, symstrs));
897 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
902 if (filter && filter(map, f))
905 symbols__insert(&self->symbols[map->type], f);
920 pr_debug("%s: problems reading %s PLT info.\n",
921 __func__, self->long_name);
925 static bool elf_sym__is_a(GElf_Sym *self, enum map_type type)
929 return elf_sym__is_function(self);
931 return elf_sym__is_object(self);
937 static bool elf_sec__is_a(GElf_Shdr *self, Elf_Data *secstrs, enum map_type type)
941 return elf_sec__is_text(self, secstrs);
943 return elf_sec__is_data(self, secstrs);
949 static size_t elf_addr_to_index(Elf *elf, GElf_Addr addr)
955 while ((sec = elf_nextscn(elf, sec)) != NULL) {
956 gelf_getshdr(sec, &shdr);
958 if ((addr >= shdr.sh_addr) &&
959 (addr < (shdr.sh_addr + shdr.sh_size)))
968 static int dso__load_sym(struct dso *self, struct map *map, const char *name,
969 int fd, symbol_filter_t filter, int kmodule)
971 struct kmap *kmap = self->kernel ? map__kmap(map) : NULL;
972 struct map *curr_map = map;
973 struct dso *curr_dso = self;
974 Elf_Data *symstrs, *secstrs;
979 GElf_Shdr shdr, opdshdr;
980 Elf_Data *syms, *opddata = NULL;
982 Elf_Scn *sec, *sec_strndx, *opdsec;
987 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
989 pr_err("%s: cannot read %s ELF file.\n", __func__, name);
993 if (gelf_getehdr(elf, &ehdr) == NULL) {
994 pr_err("%s: cannot get elf header.\n", __func__);
998 if (self->has_build_id) {
999 u8 build_id[BUILD_ID_SIZE];
1001 if (elf_read_build_id(elf, build_id,
1002 BUILD_ID_SIZE) != BUILD_ID_SIZE)
1005 if (!dso__build_id_equal(self, build_id))
1009 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
1011 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
1016 opdsec = elf_section_by_name(elf, &ehdr, &opdshdr, ".opd", &opdidx);
1018 opddata = elf_rawdata(opdsec, NULL);
1020 syms = elf_getdata(sec, NULL);
1024 sec = elf_getscn(elf, shdr.sh_link);
1028 symstrs = elf_getdata(sec, NULL);
1029 if (symstrs == NULL)
1032 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
1033 if (sec_strndx == NULL)
1036 secstrs = elf_getdata(sec_strndx, NULL);
1037 if (secstrs == NULL)
1040 nr_syms = shdr.sh_size / shdr.sh_entsize;
1042 memset(&sym, 0, sizeof(sym));
1043 if (self->kernel == DSO_TYPE_USER) {
1044 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
1045 elf_section_by_name(elf, &ehdr, &shdr,
1046 ".gnu.prelink_undo",
1048 } else self->adjust_symbols = 0;
1050 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
1052 const char *elf_name = elf_sym__name(&sym, symstrs);
1053 char *demangled = NULL;
1054 int is_label = elf_sym__is_label(&sym);
1055 const char *section_name;
1057 if (kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name &&
1058 strcmp(elf_name, kmap->ref_reloc_sym->name) == 0)
1059 kmap->ref_reloc_sym->unrelocated_addr = sym.st_value;
1061 if (!is_label && !elf_sym__is_a(&sym, map->type))
1064 if (opdsec && sym.st_shndx == opdidx) {
1065 u32 offset = sym.st_value - opdshdr.sh_addr;
1066 u64 *opd = opddata->d_buf + offset;
1067 sym.st_value = *opd;
1068 sym.st_shndx = elf_addr_to_index(elf, sym.st_value);
1071 sec = elf_getscn(elf, sym.st_shndx);
1075 gelf_getshdr(sec, &shdr);
1077 if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type))
1080 section_name = elf_sec__name(&shdr, secstrs);
1082 if (self->kernel != DSO_TYPE_USER || kmodule) {
1083 char dso_name[PATH_MAX];
1085 if (strcmp(section_name,
1086 (curr_dso->short_name +
1087 self->short_name_len)) == 0)
1090 if (strcmp(section_name, ".text") == 0) {
1096 snprintf(dso_name, sizeof(dso_name),
1097 "%s%s", self->short_name, section_name);
1099 curr_map = map_groups__find_by_name(kmap->kmaps, map->type, dso_name);
1100 if (curr_map == NULL) {
1101 u64 start = sym.st_value;
1104 start += map->start + shdr.sh_offset;
1106 curr_dso = dso__new(dso_name);
1107 if (curr_dso == NULL)
1109 curr_dso->kernel = self->kernel;
1110 curr_map = map__new2(start, curr_dso,
1112 if (curr_map == NULL) {
1113 dso__delete(curr_dso);
1116 curr_map->map_ip = identity__map_ip;
1117 curr_map->unmap_ip = identity__map_ip;
1118 curr_dso->origin = self->origin;
1119 map_groups__insert(kmap->kmaps, curr_map);
1120 dsos__add(&self->node, curr_dso);
1121 dso__set_loaded(curr_dso, map->type);
1123 curr_dso = curr_map->dso;
1128 if (curr_dso->adjust_symbols) {
1129 pr_debug4("%s: adjusting symbol: st_value: %#Lx "
1130 "sh_addr: %#Lx sh_offset: %#Lx\n", __func__,
1131 (u64)sym.st_value, (u64)shdr.sh_addr,
1132 (u64)shdr.sh_offset);
1133 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
1136 * We need to figure out if the object was created from C++ sources
1137 * DWARF DW_compile_unit has this, but we don't always have access
1140 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
1141 if (demangled != NULL)
1142 elf_name = demangled;
1144 f = symbol__new(sym.st_value, sym.st_size, elf_name);
1149 if (filter && filter(curr_map, f))
1152 symbols__insert(&curr_dso->symbols[curr_map->type], f);
1158 * For misannotated, zeroed, ASM function sizes.
1161 symbols__fixup_end(&self->symbols[map->type]);
1164 * We need to fixup this here too because we create new
1165 * maps here, for things like vsyscall sections.
1167 __map_groups__fixup_end(kmap->kmaps, map->type);
1177 static bool dso__build_id_equal(const struct dso *self, u8 *build_id)
1179 return memcmp(self->build_id, build_id, sizeof(self->build_id)) == 0;
1182 bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
1184 bool have_build_id = false;
1187 list_for_each_entry(pos, head, node) {
1188 if (with_hits && !pos->hit)
1190 if (pos->has_build_id) {
1191 have_build_id = true;
1194 if (filename__read_build_id(pos->long_name, pos->build_id,
1195 sizeof(pos->build_id)) > 0) {
1196 have_build_id = true;
1197 pos->has_build_id = true;
1201 return have_build_id;
1205 * Align offset to 4 bytes as needed for note name and descriptor data.
1207 #define NOTE_ALIGN(n) (((n) + 3) & -4U)
1209 static int elf_read_build_id(Elf *elf, void *bf, size_t size)
1219 if (size < BUILD_ID_SIZE)
1223 if (ek != ELF_K_ELF)
1226 if (gelf_getehdr(elf, &ehdr) == NULL) {
1227 pr_err("%s: cannot get elf header.\n", __func__);
1231 sec = elf_section_by_name(elf, &ehdr, &shdr,
1232 ".note.gnu.build-id", NULL);
1234 sec = elf_section_by_name(elf, &ehdr, &shdr,
1240 data = elf_getdata(sec, NULL);
1245 while (ptr < (data->d_buf + data->d_size)) {
1246 GElf_Nhdr *nhdr = ptr;
1247 int namesz = NOTE_ALIGN(nhdr->n_namesz),
1248 descsz = NOTE_ALIGN(nhdr->n_descsz);
1251 ptr += sizeof(*nhdr);
1254 if (nhdr->n_type == NT_GNU_BUILD_ID &&
1255 nhdr->n_namesz == sizeof("GNU")) {
1256 if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
1257 memcpy(bf, ptr, BUILD_ID_SIZE);
1258 err = BUILD_ID_SIZE;
1269 int filename__read_build_id(const char *filename, void *bf, size_t size)
1274 if (size < BUILD_ID_SIZE)
1277 fd = open(filename, O_RDONLY);
1281 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1283 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
1287 err = elf_read_build_id(elf, bf, size);
1296 int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
1300 if (size < BUILD_ID_SIZE)
1303 fd = open(filename, O_RDONLY);
1312 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
1315 namesz = NOTE_ALIGN(nhdr.n_namesz);
1316 descsz = NOTE_ALIGN(nhdr.n_descsz);
1317 if (nhdr.n_type == NT_GNU_BUILD_ID &&
1318 nhdr.n_namesz == sizeof("GNU")) {
1319 if (read(fd, bf, namesz) != namesz)
1321 if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
1322 if (read(fd, build_id,
1323 BUILD_ID_SIZE) == BUILD_ID_SIZE) {
1327 } else if (read(fd, bf, descsz) != descsz)
1330 int n = namesz + descsz;
1331 if (read(fd, bf, n) != n)
1340 char dso__symtab_origin(const struct dso *self)
1342 static const char origin[] = {
1343 [DSO__ORIG_KERNEL] = 'k',
1344 [DSO__ORIG_JAVA_JIT] = 'j',
1345 [DSO__ORIG_BUILD_ID_CACHE] = 'B',
1346 [DSO__ORIG_FEDORA] = 'f',
1347 [DSO__ORIG_UBUNTU] = 'u',
1348 [DSO__ORIG_BUILDID] = 'b',
1349 [DSO__ORIG_DSO] = 'd',
1350 [DSO__ORIG_KMODULE] = 'K',
1351 [DSO__ORIG_GUEST_KERNEL] = 'g',
1352 [DSO__ORIG_GUEST_KMODULE] = 'G',
1355 if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
1357 return origin[self->origin];
1360 int dso__load(struct dso *self, struct map *map, symbol_filter_t filter)
1362 int size = PATH_MAX;
1366 struct machine *machine;
1367 const char *root_dir;
1369 dso__set_loaded(self, map->type);
1371 if (self->kernel == DSO_TYPE_KERNEL)
1372 return dso__load_kernel_sym(self, map, filter);
1373 else if (self->kernel == DSO_TYPE_GUEST_KERNEL)
1374 return dso__load_guest_kernel_sym(self, map, filter);
1376 if (map->groups && map->groups->machine)
1377 machine = map->groups->machine;
1381 name = malloc(size);
1385 self->adjust_symbols = 0;
1387 if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
1388 ret = dso__load_perf_map(self, map, filter);
1389 self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
1390 DSO__ORIG_NOT_FOUND;
1394 self->origin = DSO__ORIG_BUILD_ID_CACHE;
1395 if (dso__build_id_filename(self, name, size) != NULL)
1400 switch (self->origin) {
1401 case DSO__ORIG_FEDORA:
1402 snprintf(name, size, "/usr/lib/debug%s.debug",
1405 case DSO__ORIG_UBUNTU:
1406 snprintf(name, size, "/usr/lib/debug%s",
1409 case DSO__ORIG_BUILDID:
1410 if (self->has_build_id) {
1411 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
1412 build_id__sprintf(self->build_id,
1413 sizeof(self->build_id),
1415 snprintf(name, size,
1416 "/usr/lib/debug/.build-id/%.2s/%s.debug",
1417 build_id_hex, build_id_hex + 2);
1423 snprintf(name, size, "%s", self->long_name);
1425 case DSO__ORIG_GUEST_KMODULE:
1426 if (map->groups && map->groups->machine)
1427 root_dir = map->groups->machine->root_dir;
1430 snprintf(name, size, "%s%s", root_dir, self->long_name);
1437 fd = open(name, O_RDONLY);
1440 ret = dso__load_sym(self, map, name, fd, filter, 0);
1444 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
1450 int nr_plt = dso__synthesize_plt_symbols(self, map, filter);
1456 if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
1461 struct map *map_groups__find_by_name(struct map_groups *self,
1462 enum map_type type, const char *name)
1466 for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) {
1467 struct map *map = rb_entry(nd, struct map, rb_node);
1469 if (map->dso && strcmp(map->dso->short_name, name) == 0)
1476 static int dso__kernel_module_get_build_id(struct dso *self,
1477 const char *root_dir)
1479 char filename[PATH_MAX];
1481 * kernel module short names are of the form "[module]" and
1482 * we need just "module" here.
1484 const char *name = self->short_name + 1;
1486 snprintf(filename, sizeof(filename),
1487 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1488 root_dir, (int)strlen(name) - 1, name);
1490 if (sysfs__read_build_id(filename, self->build_id,
1491 sizeof(self->build_id)) == 0)
1492 self->has_build_id = true;
1497 static int map_groups__set_modules_path_dir(struct map_groups *self,
1498 const char *dir_name)
1500 struct dirent *dent;
1501 DIR *dir = opendir(dir_name);
1504 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
1508 while ((dent = readdir(dir)) != NULL) {
1509 char path[PATH_MAX];
1512 /*sshfs might return bad dent->d_type, so we have to stat*/
1513 sprintf(path, "%s/%s", dir_name, dent->d_name);
1514 if (stat(path, &st))
1517 if (S_ISDIR(st.st_mode)) {
1518 if (!strcmp(dent->d_name, ".") ||
1519 !strcmp(dent->d_name, ".."))
1522 snprintf(path, sizeof(path), "%s/%s",
1523 dir_name, dent->d_name);
1524 if (map_groups__set_modules_path_dir(self, path) < 0)
1527 char *dot = strrchr(dent->d_name, '.'),
1532 if (dot == NULL || strcmp(dot, ".ko"))
1534 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1535 (int)(dot - dent->d_name), dent->d_name);
1537 strxfrchar(dso_name, '-', '_');
1538 map = map_groups__find_by_name(self, MAP__FUNCTION, dso_name);
1542 snprintf(path, sizeof(path), "%s/%s",
1543 dir_name, dent->d_name);
1545 long_name = strdup(path);
1546 if (long_name == NULL)
1548 dso__set_long_name(map->dso, long_name);
1549 map->dso->lname_alloc = 1;
1550 dso__kernel_module_get_build_id(map->dso, "");
1560 static char *get_kernel_version(const char *root_dir)
1562 char version[PATH_MAX];
1565 const char *prefix = "Linux version ";
1567 sprintf(version, "%s/proc/version", root_dir);
1568 file = fopen(version, "r");
1573 tmp = fgets(version, sizeof(version), file);
1576 name = strstr(version, prefix);
1579 name += strlen(prefix);
1580 tmp = strchr(name, ' ');
1584 return strdup(name);
1587 static int machine__set_modules_path(struct machine *self)
1590 char modules_path[PATH_MAX];
1592 version = get_kernel_version(self->root_dir);
1596 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s/kernel",
1597 self->root_dir, version);
1600 return map_groups__set_modules_path_dir(&self->kmaps, modules_path);
1604 * Constructor variant for modules (where we know from /proc/modules where
1605 * they are loaded) and for vmlinux, where only after we load all the
1606 * symbols we'll know where it starts and ends.
1608 static struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
1610 struct map *self = calloc(1, (sizeof(*self) +
1611 (dso->kernel ? sizeof(struct kmap) : 0)));
1614 * ->end will be filled after we load all the symbols
1616 map__init(self, type, start, 0, 0, dso);
1622 struct map *machine__new_module(struct machine *self, u64 start,
1623 const char *filename)
1626 struct dso *dso = __dsos__findnew(&self->kernel_dsos, filename);
1631 map = map__new2(start, dso, MAP__FUNCTION);
1635 if (machine__is_host(self))
1636 dso->origin = DSO__ORIG_KMODULE;
1638 dso->origin = DSO__ORIG_GUEST_KMODULE;
1639 map_groups__insert(&self->kmaps, map);
1643 static int machine__create_modules(struct machine *self)
1649 const char *modules;
1650 char path[PATH_MAX];
1652 if (machine__is_default_guest(self))
1653 modules = symbol_conf.default_guest_modules;
1655 sprintf(path, "%s/proc/modules", self->root_dir);
1659 file = fopen(modules, "r");
1663 while (!feof(file)) {
1664 char name[PATH_MAX];
1669 line_len = getline(&line, &n, file);
1676 line[--line_len] = '\0'; /* \n */
1678 sep = strrchr(line, 'x');
1682 hex2u64(sep + 1, &start);
1684 sep = strchr(line, ' ');
1690 snprintf(name, sizeof(name), "[%s]", line);
1691 map = machine__new_module(self, start, name);
1693 goto out_delete_line;
1694 dso__kernel_module_get_build_id(map->dso, self->root_dir);
1700 return machine__set_modules_path(self);
1708 static int dso__load_vmlinux(struct dso *self, struct map *map,
1709 const char *vmlinux, symbol_filter_t filter)
1713 if (self->has_build_id) {
1714 u8 build_id[BUILD_ID_SIZE];
1716 if (filename__read_build_id(vmlinux, build_id,
1717 sizeof(build_id)) < 0) {
1718 pr_debug("No build_id in %s, ignoring it\n", vmlinux);
1721 if (!dso__build_id_equal(self, build_id)) {
1722 char expected_build_id[BUILD_ID_SIZE * 2 + 1],
1723 vmlinux_build_id[BUILD_ID_SIZE * 2 + 1];
1725 build_id__sprintf(self->build_id,
1726 sizeof(self->build_id),
1728 build_id__sprintf(build_id, sizeof(build_id),
1730 pr_debug("build_id in %s is %s while expected is %s, "
1731 "ignoring it\n", vmlinux, vmlinux_build_id,
1737 fd = open(vmlinux, O_RDONLY);
1741 dso__set_loaded(self, map->type);
1742 err = dso__load_sym(self, map, vmlinux, fd, filter, 0);
1746 pr_debug("Using %s for symbols\n", vmlinux);
1751 int dso__load_vmlinux_path(struct dso *self, struct map *map,
1752 symbol_filter_t filter)
1757 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1758 vmlinux_path__nr_entries + 1);
1760 filename = dso__build_id_filename(self, NULL, 0);
1761 if (filename != NULL) {
1762 err = dso__load_vmlinux(self, map, filename, filter);
1764 dso__set_long_name(self, filename);
1770 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
1771 err = dso__load_vmlinux(self, map, vmlinux_path[i], filter);
1773 dso__set_long_name(self, strdup(vmlinux_path[i]));
1781 static int dso__load_kernel_sym(struct dso *self, struct map *map,
1782 symbol_filter_t filter)
1785 const char *kallsyms_filename = NULL;
1786 char *kallsyms_allocated_filename = NULL;
1788 * Step 1: if the user specified a vmlinux filename, use it and only
1789 * it, reporting errors to the user if it cannot be used.
1791 * For instance, try to analyse an ARM perf.data file _without_ a
1792 * build-id, or if the user specifies the wrong path to the right
1793 * vmlinux file, obviously we can't fallback to another vmlinux (a
1794 * x86_86 one, on the machine where analysis is being performed, say),
1795 * or worse, /proc/kallsyms.
1797 * If the specified file _has_ a build-id and there is a build-id
1798 * section in the perf.data file, we will still do the expected
1799 * validation in dso__load_vmlinux and will bail out if they don't
1802 if (symbol_conf.vmlinux_name != NULL) {
1803 err = dso__load_vmlinux(self, map,
1804 symbol_conf.vmlinux_name, filter);
1806 dso__set_long_name(self,
1807 strdup(symbol_conf.vmlinux_name));
1813 if (vmlinux_path != NULL) {
1814 err = dso__load_vmlinux_path(self, map, filter);
1820 * Say the kernel DSO was created when processing the build-id header table,
1821 * we have a build-id, so check if it is the same as the running kernel,
1822 * using it if it is.
1824 if (self->has_build_id) {
1825 u8 kallsyms_build_id[BUILD_ID_SIZE];
1826 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1828 if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id,
1829 sizeof(kallsyms_build_id)) == 0) {
1830 if (dso__build_id_equal(self, kallsyms_build_id)) {
1831 kallsyms_filename = "/proc/kallsyms";
1836 * Now look if we have it on the build-id cache in
1837 * $HOME/.debug/[kernel.kallsyms].
1839 build_id__sprintf(self->build_id, sizeof(self->build_id),
1842 if (asprintf(&kallsyms_allocated_filename,
1843 "%s/.debug/[kernel.kallsyms]/%s",
1844 getenv("HOME"), sbuild_id) == -1) {
1845 pr_err("Not enough memory for kallsyms file lookup\n");
1849 kallsyms_filename = kallsyms_allocated_filename;
1851 if (access(kallsyms_filename, F_OK)) {
1852 pr_err("No kallsyms or vmlinux with build-id %s "
1853 "was found\n", sbuild_id);
1854 free(kallsyms_allocated_filename);
1859 * Last resort, if we don't have a build-id and couldn't find
1860 * any vmlinux file, try the running kernel kallsyms table.
1862 kallsyms_filename = "/proc/kallsyms";
1866 err = dso__load_kallsyms(self, kallsyms_filename, map, filter);
1868 pr_debug("Using %s for symbols\n", kallsyms_filename);
1869 free(kallsyms_allocated_filename);
1873 if (kallsyms_filename != NULL)
1874 dso__set_long_name(self, strdup("[kernel.kallsyms]"));
1875 map__fixup_start(map);
1876 map__fixup_end(map);
1882 static int dso__load_guest_kernel_sym(struct dso *self, struct map *map,
1883 symbol_filter_t filter)
1886 const char *kallsyms_filename = NULL;
1887 struct machine *machine;
1888 char path[PATH_MAX];
1891 pr_debug("Guest kernel map hasn't the point to groups\n");
1894 machine = map->groups->machine;
1896 if (machine__is_default_guest(machine)) {
1898 * if the user specified a vmlinux filename, use it and only
1899 * it, reporting errors to the user if it cannot be used.
1900 * Or use file guest_kallsyms inputted by user on commandline
1902 if (symbol_conf.default_guest_vmlinux_name != NULL) {
1903 err = dso__load_vmlinux(self, map,
1904 symbol_conf.default_guest_vmlinux_name, filter);
1908 kallsyms_filename = symbol_conf.default_guest_kallsyms;
1909 if (!kallsyms_filename)
1912 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
1913 kallsyms_filename = path;
1916 err = dso__load_kallsyms(self, kallsyms_filename, map, filter);
1918 pr_debug("Using %s for symbols\n", kallsyms_filename);
1922 if (kallsyms_filename != NULL) {
1923 machine__mmap_name(machine, path, sizeof(path));
1924 dso__set_long_name(self, strdup(path));
1926 map__fixup_start(map);
1927 map__fixup_end(map);
1933 static void dsos__add(struct list_head *head, struct dso *dso)
1935 list_add_tail(&dso->node, head);
1938 static struct dso *dsos__find(struct list_head *head, const char *name)
1942 list_for_each_entry(pos, head, node)
1943 if (strcmp(pos->long_name, name) == 0)
1948 struct dso *__dsos__findnew(struct list_head *head, const char *name)
1950 struct dso *dso = dsos__find(head, name);
1953 dso = dso__new(name);
1955 dsos__add(head, dso);
1956 dso__set_basename(dso);
1963 size_t __dsos__fprintf(struct list_head *head, FILE *fp)
1968 list_for_each_entry(pos, head, node) {
1970 for (i = 0; i < MAP__NR_TYPES; ++i)
1971 ret += dso__fprintf(pos, i, fp);
1977 size_t machines__fprintf_dsos(struct rb_root *self, FILE *fp)
1982 for (nd = rb_first(self); nd; nd = rb_next(nd)) {
1983 struct machine *pos = rb_entry(nd, struct machine, rb_node);
1984 ret += __dsos__fprintf(&pos->kernel_dsos, fp);
1985 ret += __dsos__fprintf(&pos->user_dsos, fp);
1991 static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
1997 list_for_each_entry(pos, head, node) {
1998 if (with_hits && !pos->hit)
2000 ret += dso__fprintf_buildid(pos, fp);
2001 ret += fprintf(fp, " %s\n", pos->long_name);
2006 size_t machine__fprintf_dsos_buildid(struct machine *self, FILE *fp, bool with_hits)
2008 return __dsos__fprintf_buildid(&self->kernel_dsos, fp, with_hits) +
2009 __dsos__fprintf_buildid(&self->user_dsos, fp, with_hits);
2012 size_t machines__fprintf_dsos_buildid(struct rb_root *self, FILE *fp, bool with_hits)
2017 for (nd = rb_first(self); nd; nd = rb_next(nd)) {
2018 struct machine *pos = rb_entry(nd, struct machine, rb_node);
2019 ret += machine__fprintf_dsos_buildid(pos, fp, with_hits);
2024 struct dso *dso__new_kernel(const char *name)
2026 struct dso *self = dso__new(name ?: "[kernel.kallsyms]");
2029 dso__set_short_name(self, "[kernel]");
2030 self->kernel = DSO_TYPE_KERNEL;
2036 static struct dso *dso__new_guest_kernel(struct machine *machine,
2040 struct dso *self = dso__new(name ?: machine__mmap_name(machine, bf, sizeof(bf)));
2043 dso__set_short_name(self, "[guest.kernel]");
2044 self->kernel = DSO_TYPE_GUEST_KERNEL;
2050 void dso__read_running_kernel_build_id(struct dso *self, struct machine *machine)
2052 char path[PATH_MAX];
2054 if (machine__is_default_guest(machine))
2056 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
2057 if (sysfs__read_build_id(path, self->build_id,
2058 sizeof(self->build_id)) == 0)
2059 self->has_build_id = true;
2062 static struct dso *machine__create_kernel(struct machine *self)
2064 const char *vmlinux_name = NULL;
2067 if (machine__is_host(self)) {
2068 vmlinux_name = symbol_conf.vmlinux_name;
2069 kernel = dso__new_kernel(vmlinux_name);
2071 if (machine__is_default_guest(self))
2072 vmlinux_name = symbol_conf.default_guest_vmlinux_name;
2073 kernel = dso__new_guest_kernel(self, vmlinux_name);
2076 if (kernel != NULL) {
2077 dso__read_running_kernel_build_id(kernel, self);
2078 dsos__add(&self->kernel_dsos, kernel);
2083 int __machine__create_kernel_maps(struct machine *self, struct dso *kernel)
2087 for (type = 0; type < MAP__NR_TYPES; ++type) {
2090 self->vmlinux_maps[type] = map__new2(0, kernel, type);
2091 if (self->vmlinux_maps[type] == NULL)
2094 self->vmlinux_maps[type]->map_ip =
2095 self->vmlinux_maps[type]->unmap_ip = identity__map_ip;
2097 kmap = map__kmap(self->vmlinux_maps[type]);
2098 kmap->kmaps = &self->kmaps;
2099 map_groups__insert(&self->kmaps, self->vmlinux_maps[type]);
2105 int machine__create_kernel_maps(struct machine *self)
2107 struct dso *kernel = machine__create_kernel(self);
2109 if (kernel == NULL ||
2110 __machine__create_kernel_maps(self, kernel) < 0)
2113 if (symbol_conf.use_modules && machine__create_modules(self) < 0)
2114 pr_debug("Problems creating module maps, continuing anyway...\n");
2116 * Now that we have all the maps created, just set the ->end of them:
2118 map_groups__fixup_end(&self->kmaps);
2122 static void vmlinux_path__exit(void)
2124 while (--vmlinux_path__nr_entries >= 0) {
2125 free(vmlinux_path[vmlinux_path__nr_entries]);
2126 vmlinux_path[vmlinux_path__nr_entries] = NULL;
2130 vmlinux_path = NULL;
2133 static int vmlinux_path__init(void)
2138 if (uname(&uts) < 0)
2141 vmlinux_path = malloc(sizeof(char *) * 5);
2142 if (vmlinux_path == NULL)
2145 vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
2146 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2148 ++vmlinux_path__nr_entries;
2149 vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
2150 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2152 ++vmlinux_path__nr_entries;
2153 snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
2154 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
2155 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2157 ++vmlinux_path__nr_entries;
2158 snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
2159 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
2160 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2162 ++vmlinux_path__nr_entries;
2163 snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
2165 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
2166 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2168 ++vmlinux_path__nr_entries;
2173 vmlinux_path__exit();
2177 size_t machine__fprintf_vmlinux_path(struct machine *self, FILE *fp)
2181 struct dso *kdso = self->vmlinux_maps[MAP__FUNCTION]->dso;
2183 if (kdso->has_build_id) {
2184 char filename[PATH_MAX];
2185 if (dso__build_id_filename(kdso, filename, sizeof(filename)))
2186 printed += fprintf(fp, "[0] %s\n", filename);
2189 for (i = 0; i < vmlinux_path__nr_entries; ++i)
2190 printed += fprintf(fp, "[%d] %s\n",
2191 i + kdso->has_build_id, vmlinux_path[i]);
2196 static int setup_list(struct strlist **list, const char *list_str,
2197 const char *list_name)
2199 if (list_str == NULL)
2202 *list = strlist__new(true, list_str);
2204 pr_err("problems parsing %s list\n", list_name);
2210 int symbol__init(void)
2212 elf_version(EV_CURRENT);
2213 if (symbol_conf.sort_by_name)
2214 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
2215 sizeof(struct symbol));
2217 if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
2220 if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
2221 pr_err("'.' is the only non valid --field-separator argument\n");
2225 if (setup_list(&symbol_conf.dso_list,
2226 symbol_conf.dso_list_str, "dso") < 0)
2229 if (setup_list(&symbol_conf.comm_list,
2230 symbol_conf.comm_list_str, "comm") < 0)
2231 goto out_free_dso_list;
2233 if (setup_list(&symbol_conf.sym_list,
2234 symbol_conf.sym_list_str, "symbol") < 0)
2235 goto out_free_comm_list;
2240 strlist__delete(symbol_conf.dso_list);
2242 strlist__delete(symbol_conf.comm_list);
2246 int machines__create_kernel_maps(struct rb_root *self, pid_t pid)
2248 struct machine *machine = machines__findnew(self, pid);
2250 if (machine == NULL)
2253 return machine__create_kernel_maps(machine);
2256 static int hex(char ch)
2258 if ((ch >= '0') && (ch <= '9'))
2260 if ((ch >= 'a') && (ch <= 'f'))
2261 return ch - 'a' + 10;
2262 if ((ch >= 'A') && (ch <= 'F'))
2263 return ch - 'A' + 10;
2268 * While we find nice hex chars, build a long_val.
2269 * Return number of chars processed.
2271 int hex2u64(const char *ptr, u64 *long_val)
2273 const char *p = ptr;
2277 const int hex_val = hex(*p);
2282 *long_val = (*long_val << 4) | hex_val;
2289 char *strxfrchar(char *s, char from, char to)
2293 while ((p = strchr(p, from)) != NULL)
2299 int machines__create_guest_kernel_maps(struct rb_root *self)
2302 struct dirent **namelist = NULL;
2304 char path[PATH_MAX];
2307 if (symbol_conf.default_guest_vmlinux_name ||
2308 symbol_conf.default_guest_modules ||
2309 symbol_conf.default_guest_kallsyms) {
2310 machines__create_kernel_maps(self, DEFAULT_GUEST_KERNEL_ID);
2313 if (symbol_conf.guestmount) {
2314 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
2317 for (i = 0; i < items; i++) {
2318 if (!isdigit(namelist[i]->d_name[0])) {
2319 /* Filter out . and .. */
2322 pid = atoi(namelist[i]->d_name);
2323 sprintf(path, "%s/%s/proc/kallsyms",
2324 symbol_conf.guestmount,
2325 namelist[i]->d_name);
2326 ret = access(path, R_OK);
2328 pr_debug("Can't access file %s\n", path);
2331 machines__create_kernel_maps(self, pid);
2340 int machine__load_kallsyms(struct machine *self, const char *filename,
2341 enum map_type type, symbol_filter_t filter)
2343 struct map *map = self->vmlinux_maps[type];
2344 int ret = dso__load_kallsyms(map->dso, filename, map, filter);
2347 dso__set_loaded(map->dso, type);
2349 * Since /proc/kallsyms will have multiple sessions for the
2350 * kernel, with modules between them, fixup the end of all
2353 __map_groups__fixup_end(&self->kmaps, type);
2359 int machine__load_vmlinux_path(struct machine *self, enum map_type type,
2360 symbol_filter_t filter)
2362 struct map *map = self->vmlinux_maps[type];
2363 int ret = dso__load_vmlinux_path(map->dso, map, filter);
2366 dso__set_loaded(map->dso, type);
2367 map__reloc_vmlinux(map);