11 #include <sys/param.h>
24 #include <sys/utsname.h>
27 #define KSYM_NAME_LEN 128
30 #ifndef NT_GNU_BUILD_ID
31 #define NT_GNU_BUILD_ID 3
34 static bool dso__build_id_equal(const struct dso *dso, u8 *build_id);
35 static int elf_read_build_id(Elf *elf, void *bf, size_t size);
36 static void dsos__add(struct list_head *head, struct dso *dso);
37 static struct map *map__new2(u64 start, struct dso *dso, enum map_type type);
38 static int dso__load_kernel_sym(struct dso *dso, struct map *map,
39 symbol_filter_t filter);
40 static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
41 symbol_filter_t filter);
42 static int vmlinux_path__nr_entries;
43 static char **vmlinux_path;
45 struct symbol_conf symbol_conf = {
46 .exclude_other = true,
48 .try_vmlinux_path = true,
52 int dso__name_len(const struct dso *dso)
55 return dso->long_name_len;
57 return dso->short_name_len;
60 bool dso__loaded(const struct dso *dso, enum map_type type)
62 return dso->loaded & (1 << type);
65 bool dso__sorted_by_name(const struct dso *dso, enum map_type type)
67 return dso->sorted_by_name & (1 << type);
70 static void dso__set_sorted_by_name(struct dso *dso, enum map_type type)
72 dso->sorted_by_name |= (1 << type);
75 bool symbol_type__is_a(char symbol_type, enum map_type map_type)
79 return symbol_type == 'T' || symbol_type == 'W';
81 return symbol_type == 'D' || symbol_type == 'd';
87 static void symbols__fixup_end(struct rb_root *symbols)
89 struct rb_node *nd, *prevnd = rb_first(symbols);
90 struct symbol *curr, *prev;
95 curr = rb_entry(prevnd, struct symbol, rb_node);
97 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
99 curr = rb_entry(nd, struct symbol, rb_node);
101 if (prev->end == prev->start && prev->end != curr->start)
102 prev->end = curr->start - 1;
106 if (curr->end == curr->start)
107 curr->end = roundup(curr->start, 4096);
110 static void __map_groups__fixup_end(struct map_groups *mg, enum map_type type)
112 struct map *prev, *curr;
113 struct rb_node *nd, *prevnd = rb_first(&mg->maps[type]);
118 curr = rb_entry(prevnd, struct map, rb_node);
120 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
122 curr = rb_entry(nd, struct map, rb_node);
123 prev->end = curr->start - 1;
127 * We still haven't the actual symbols, so guess the
128 * last map final address.
133 static void map_groups__fixup_end(struct map_groups *mg)
136 for (i = 0; i < MAP__NR_TYPES; ++i)
137 __map_groups__fixup_end(mg, i);
140 static struct symbol *symbol__new(u64 start, u64 len, u8 binding,
143 size_t namelen = strlen(name) + 1;
144 struct symbol *sym = calloc(1, (symbol_conf.priv_size +
145 sizeof(*sym) + namelen));
149 if (symbol_conf.priv_size)
150 sym = ((void *)sym) + symbol_conf.priv_size;
153 sym->end = len ? start + len - 1 : start;
154 sym->binding = binding;
155 sym->namelen = namelen - 1;
157 pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n",
158 __func__, name, start, sym->end);
159 memcpy(sym->name, name, namelen);
164 void symbol__delete(struct symbol *sym)
166 free(((void *)sym) - symbol_conf.priv_size);
169 static size_t symbol__fprintf(struct symbol *sym, FILE *fp)
171 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %c %s\n",
172 sym->start, sym->end,
173 sym->binding == STB_GLOBAL ? 'g' :
174 sym->binding == STB_LOCAL ? 'l' : 'w',
178 void dso__set_long_name(struct dso *dso, char *name)
182 dso->long_name = name;
183 dso->long_name_len = strlen(name);
186 static void dso__set_short_name(struct dso *dso, const char *name)
190 dso->short_name = name;
191 dso->short_name_len = strlen(name);
194 static void dso__set_basename(struct dso *dso)
196 dso__set_short_name(dso, basename(dso->long_name));
199 struct dso *dso__new(const char *name)
201 struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
205 strcpy(dso->name, name);
206 dso__set_long_name(dso, dso->name);
207 dso__set_short_name(dso, dso->name);
208 for (i = 0; i < MAP__NR_TYPES; ++i)
209 dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
210 dso->symtab_type = SYMTAB__NOT_FOUND;
212 dso->sorted_by_name = 0;
213 dso->has_build_id = 0;
214 dso->kernel = DSO_TYPE_USER;
215 INIT_LIST_HEAD(&dso->node);
221 static void symbols__delete(struct rb_root *symbols)
224 struct rb_node *next = rb_first(symbols);
227 pos = rb_entry(next, struct symbol, rb_node);
228 next = rb_next(&pos->rb_node);
229 rb_erase(&pos->rb_node, symbols);
234 void dso__delete(struct dso *dso)
237 for (i = 0; i < MAP__NR_TYPES; ++i)
238 symbols__delete(&dso->symbols[i]);
239 if (dso->sname_alloc)
240 free((char *)dso->short_name);
241 if (dso->lname_alloc)
242 free(dso->long_name);
246 void dso__set_build_id(struct dso *dso, void *build_id)
248 memcpy(dso->build_id, build_id, sizeof(dso->build_id));
249 dso->has_build_id = 1;
252 static void symbols__insert(struct rb_root *symbols, struct symbol *sym)
254 struct rb_node **p = &symbols->rb_node;
255 struct rb_node *parent = NULL;
256 const u64 ip = sym->start;
261 s = rb_entry(parent, struct symbol, rb_node);
267 rb_link_node(&sym->rb_node, parent, p);
268 rb_insert_color(&sym->rb_node, symbols);
271 static struct symbol *symbols__find(struct rb_root *symbols, u64 ip)
278 n = symbols->rb_node;
281 struct symbol *s = rb_entry(n, struct symbol, rb_node);
285 else if (ip > s->end)
294 struct symbol_name_rb_node {
295 struct rb_node rb_node;
299 static void symbols__insert_by_name(struct rb_root *symbols, struct symbol *sym)
301 struct rb_node **p = &symbols->rb_node;
302 struct rb_node *parent = NULL;
303 struct symbol_name_rb_node *symn, *s;
305 symn = container_of(sym, struct symbol_name_rb_node, sym);
309 s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
310 if (strcmp(sym->name, s->sym.name) < 0)
315 rb_link_node(&symn->rb_node, parent, p);
316 rb_insert_color(&symn->rb_node, symbols);
319 static void symbols__sort_by_name(struct rb_root *symbols,
320 struct rb_root *source)
324 for (nd = rb_first(source); nd; nd = rb_next(nd)) {
325 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
326 symbols__insert_by_name(symbols, pos);
330 static struct symbol *symbols__find_by_name(struct rb_root *symbols,
338 n = symbols->rb_node;
341 struct symbol_name_rb_node *s;
344 s = rb_entry(n, struct symbol_name_rb_node, rb_node);
345 cmp = strcmp(name, s->sym.name);
358 struct symbol *dso__find_symbol(struct dso *dso,
359 enum map_type type, u64 addr)
361 return symbols__find(&dso->symbols[type], addr);
364 struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
367 return symbols__find_by_name(&dso->symbol_names[type], name);
370 void dso__sort_by_name(struct dso *dso, enum map_type type)
372 dso__set_sorted_by_name(dso, type);
373 return symbols__sort_by_name(&dso->symbol_names[type],
374 &dso->symbols[type]);
377 int build_id__sprintf(const u8 *build_id, int len, char *bf)
380 const u8 *raw = build_id;
383 for (i = 0; i < len; ++i) {
384 sprintf(bid, "%02x", *raw);
389 return raw - build_id;
392 size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
394 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
396 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
397 return fprintf(fp, "%s", sbuild_id);
400 size_t dso__fprintf_symbols_by_name(struct dso *dso,
401 enum map_type type, FILE *fp)
405 struct symbol_name_rb_node *pos;
407 for (nd = rb_first(&dso->symbol_names[type]); nd; nd = rb_next(nd)) {
408 pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
409 fprintf(fp, "%s\n", pos->sym.name);
415 size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
418 size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
420 if (dso->short_name != dso->long_name)
421 ret += fprintf(fp, "%s, ", dso->long_name);
422 ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
423 dso->loaded ? "" : "NOT ");
424 ret += dso__fprintf_buildid(dso, fp);
425 ret += fprintf(fp, ")\n");
426 for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) {
427 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
428 ret += symbol__fprintf(pos, fp);
434 int kallsyms__parse(const char *filename, void *arg,
435 int (*process_symbol)(void *arg, const char *name,
436 char type, u64 start, u64 end))
442 char prev_symbol_type = 0;
443 char *prev_symbol_name;
444 FILE *file = fopen(filename, "r");
449 prev_symbol_name = malloc(KSYM_NAME_LEN);
450 if (prev_symbol_name == NULL)
455 while (!feof(file)) {
461 line_len = getline(&line, &n, file);
462 if (line_len < 0 || !line)
465 line[--line_len] = '\0'; /* \n */
467 len = hex2u64(line, &start);
470 if (len + 2 >= line_len)
473 symbol_type = toupper(line[len]);
475 symbol_name = line + len;
476 len = line_len - len;
478 if (len >= KSYM_NAME_LEN) {
483 if (prev_symbol_type) {
485 if (end != prev_start)
487 err = process_symbol(arg, prev_symbol_name,
488 prev_symbol_type, prev_start, end);
493 memcpy(prev_symbol_name, symbol_name, len + 1);
494 prev_symbol_type = symbol_type;
498 free(prev_symbol_name);
508 struct process_kallsyms_args {
513 static u8 kallsyms2elf_type(char type)
518 return isupper(type) ? STB_GLOBAL : STB_LOCAL;
521 static int map__process_kallsym_symbol(void *arg, const char *name,
522 char type, u64 start, u64 end)
525 struct process_kallsyms_args *a = arg;
526 struct rb_root *root = &a->dso->symbols[a->map->type];
528 if (!symbol_type__is_a(type, a->map->type))
531 sym = symbol__new(start, end - start + 1,
532 kallsyms2elf_type(type), name);
536 * We will pass the symbols to the filter later, in
537 * map__split_kallsyms, when we have split the maps per module
539 symbols__insert(root, sym);
545 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
546 * so that we can in the next step set the symbol ->end address and then
547 * call kernel_maps__split_kallsyms.
549 static int dso__load_all_kallsyms(struct dso *dso, const char *filename,
552 struct process_kallsyms_args args = { .map = map, .dso = dso, };
553 return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
557 * Split the symbols into maps, making sure there are no overlaps, i.e. the
558 * kernel range is broken in several maps, named [kernel].N, as we don't have
559 * the original ELF section names vmlinux have.
561 static int dso__split_kallsyms(struct dso *dso, struct map *map,
562 symbol_filter_t filter)
564 struct map_groups *kmaps = map__kmap(map)->kmaps;
565 struct machine *machine = kmaps->machine;
566 struct map *curr_map = map;
568 int count = 0, moved = 0;
569 struct rb_root *root = &dso->symbols[map->type];
570 struct rb_node *next = rb_first(root);
571 int kernel_range = 0;
576 pos = rb_entry(next, struct symbol, rb_node);
577 next = rb_next(&pos->rb_node);
579 module = strchr(pos->name, '\t');
581 if (!symbol_conf.use_modules)
586 if (strcmp(curr_map->dso->short_name, module)) {
587 if (curr_map != map &&
588 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
589 machine__is_default_guest(machine)) {
591 * We assume all symbols of a module are
592 * continuous in * kallsyms, so curr_map
593 * points to a module and all its
594 * symbols are in its kmap. Mark it as
597 dso__set_loaded(curr_map->dso,
601 curr_map = map_groups__find_by_name(kmaps,
603 if (curr_map == NULL) {
604 pr_debug("%s/proc/{kallsyms,modules} "
605 "inconsistency while looking "
606 "for \"%s\" module!\n",
607 machine->root_dir, module);
612 if (curr_map->dso->loaded &&
613 !machine__is_default_guest(machine))
617 * So that we look just like we get from .ko files,
618 * i.e. not prelinked, relative to map->start.
620 pos->start = curr_map->map_ip(curr_map, pos->start);
621 pos->end = curr_map->map_ip(curr_map, pos->end);
622 } else if (curr_map != map) {
623 char dso_name[PATH_MAX];
631 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
632 snprintf(dso_name, sizeof(dso_name),
636 snprintf(dso_name, sizeof(dso_name),
640 ndso = dso__new(dso_name);
644 ndso->kernel = dso->kernel;
646 curr_map = map__new2(pos->start, ndso, map->type);
647 if (curr_map == NULL) {
652 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
653 map_groups__insert(kmaps, curr_map);
657 if (filter && filter(curr_map, pos)) {
658 discard_symbol: rb_erase(&pos->rb_node, root);
661 if (curr_map != map) {
662 rb_erase(&pos->rb_node, root);
663 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
670 if (curr_map != map &&
671 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
672 machine__is_default_guest(kmaps->machine)) {
673 dso__set_loaded(curr_map->dso, curr_map->type);
676 return count + moved;
679 static bool symbol__restricted_filename(const char *filename,
680 const char *restricted_filename)
682 bool restricted = false;
684 if (symbol_conf.kptr_restrict) {
685 char *r = realpath(filename, NULL);
688 restricted = strcmp(r, restricted_filename) == 0;
697 int dso__load_kallsyms(struct dso *dso, const char *filename,
698 struct map *map, symbol_filter_t filter)
700 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
703 if (dso__load_all_kallsyms(dso, filename, map) < 0)
706 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
707 dso->symtab_type = SYMTAB__GUEST_KALLSYMS;
709 dso->symtab_type = SYMTAB__KALLSYMS;
711 return dso__split_kallsyms(dso, map, filter);
714 static int dso__load_perf_map(struct dso *dso, struct map *map,
715 symbol_filter_t filter)
722 file = fopen(dso->long_name, "r");
726 while (!feof(file)) {
731 line_len = getline(&line, &n, file);
738 line[--line_len] = '\0'; /* \n */
740 len = hex2u64(line, &start);
743 if (len + 2 >= line_len)
746 len += hex2u64(line + len, &size);
749 if (len + 2 >= line_len)
752 sym = symbol__new(start, size, STB_GLOBAL, line + len);
755 goto out_delete_line;
757 if (filter && filter(map, sym))
760 symbols__insert(&dso->symbols[map->type], sym);
777 * elf_symtab__for_each_symbol - iterate thru all the symbols
779 * @syms: struct elf_symtab instance to iterate
781 * @sym: GElf_Sym iterator
783 #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
784 for (idx = 0, gelf_getsym(syms, idx, &sym);\
786 idx++, gelf_getsym(syms, idx, &sym))
788 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
790 return GELF_ST_TYPE(sym->st_info);
793 static inline int elf_sym__is_function(const GElf_Sym *sym)
795 return elf_sym__type(sym) == STT_FUNC &&
797 sym->st_shndx != SHN_UNDEF;
800 static inline bool elf_sym__is_object(const GElf_Sym *sym)
802 return elf_sym__type(sym) == STT_OBJECT &&
804 sym->st_shndx != SHN_UNDEF;
807 static inline int elf_sym__is_label(const GElf_Sym *sym)
809 return elf_sym__type(sym) == STT_NOTYPE &&
811 sym->st_shndx != SHN_UNDEF &&
812 sym->st_shndx != SHN_ABS;
815 static inline const char *elf_sec__name(const GElf_Shdr *shdr,
816 const Elf_Data *secstrs)
818 return secstrs->d_buf + shdr->sh_name;
821 static inline int elf_sec__is_text(const GElf_Shdr *shdr,
822 const Elf_Data *secstrs)
824 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
827 static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
828 const Elf_Data *secstrs)
830 return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
833 static inline const char *elf_sym__name(const GElf_Sym *sym,
834 const Elf_Data *symstrs)
836 return symstrs->d_buf + sym->st_name;
839 static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
840 GElf_Shdr *shp, const char *name,
846 while ((sec = elf_nextscn(elf, sec)) != NULL) {
849 gelf_getshdr(sec, shp);
850 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
851 if (!strcmp(name, str)) {
862 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
863 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
865 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
867 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
868 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
870 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
873 * We need to check if we have a .dynsym, so that we can handle the
874 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
875 * .dynsym or .symtab).
876 * And always look at the original dso, not at debuginfo packages, that
877 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
879 static int dso__synthesize_plt_symbols(struct dso *dso, struct map *map,
880 symbol_filter_t filter)
882 uint32_t nr_rel_entries, idx;
887 GElf_Shdr shdr_rel_plt, shdr_dynsym;
888 Elf_Data *reldata, *syms, *symstrs;
889 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
892 char sympltname[1024];
894 int nr = 0, symidx, fd, err = 0;
897 snprintf(name, sizeof(name), "%s%s",
898 symbol_conf.symfs, dso->long_name);
899 fd = open(name, O_RDONLY);
903 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
907 if (gelf_getehdr(elf, &ehdr) == NULL)
910 scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
911 ".dynsym", &dynsym_idx);
912 if (scn_dynsym == NULL)
915 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
917 if (scn_plt_rel == NULL) {
918 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
920 if (scn_plt_rel == NULL)
926 if (shdr_rel_plt.sh_link != dynsym_idx)
929 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
933 * Fetch the relocation section to find the idxes to the GOT
934 * and the symbols in the .dynsym they refer to.
936 reldata = elf_getdata(scn_plt_rel, NULL);
940 syms = elf_getdata(scn_dynsym, NULL);
944 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
945 if (scn_symstrs == NULL)
948 symstrs = elf_getdata(scn_symstrs, NULL);
952 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
953 plt_offset = shdr_plt.sh_offset;
955 if (shdr_rel_plt.sh_type == SHT_RELA) {
956 GElf_Rela pos_mem, *pos;
958 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
960 symidx = GELF_R_SYM(pos->r_info);
961 plt_offset += shdr_plt.sh_entsize;
962 gelf_getsym(syms, symidx, &sym);
963 snprintf(sympltname, sizeof(sympltname),
964 "%s@plt", elf_sym__name(&sym, symstrs));
966 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
967 STB_GLOBAL, sympltname);
971 if (filter && filter(map, f))
974 symbols__insert(&dso->symbols[map->type], f);
978 } else if (shdr_rel_plt.sh_type == SHT_REL) {
979 GElf_Rel pos_mem, *pos;
980 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
982 symidx = GELF_R_SYM(pos->r_info);
983 plt_offset += shdr_plt.sh_entsize;
984 gelf_getsym(syms, symidx, &sym);
985 snprintf(sympltname, sizeof(sympltname),
986 "%s@plt", elf_sym__name(&sym, symstrs));
988 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
989 STB_GLOBAL, sympltname);
993 if (filter && filter(map, f))
996 symbols__insert(&dso->symbols[map->type], f);
1011 pr_debug("%s: problems reading %s PLT info.\n",
1012 __func__, dso->long_name);
1016 static bool elf_sym__is_a(GElf_Sym *sym, enum map_type type)
1020 return elf_sym__is_function(sym);
1022 return elf_sym__is_object(sym);
1028 static bool elf_sec__is_a(GElf_Shdr *shdr, Elf_Data *secstrs,
1033 return elf_sec__is_text(shdr, secstrs);
1035 return elf_sec__is_data(shdr, secstrs);
1041 static size_t elf_addr_to_index(Elf *elf, GElf_Addr addr)
1043 Elf_Scn *sec = NULL;
1047 while ((sec = elf_nextscn(elf, sec)) != NULL) {
1048 gelf_getshdr(sec, &shdr);
1050 if ((addr >= shdr.sh_addr) &&
1051 (addr < (shdr.sh_addr + shdr.sh_size)))
1060 static int dso__load_sym(struct dso *dso, struct map *map, const char *name,
1061 int fd, symbol_filter_t filter, int kmodule,
1064 struct kmap *kmap = dso->kernel ? map__kmap(map) : NULL;
1065 struct map *curr_map = map;
1066 struct dso *curr_dso = dso;
1067 Elf_Data *symstrs, *secstrs;
1072 GElf_Shdr shdr, opdshdr;
1073 Elf_Data *syms, *opddata = NULL;
1075 Elf_Scn *sec, *sec_strndx, *opdsec;
1080 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1082 pr_debug("%s: cannot read %s ELF file.\n", __func__, name);
1086 if (gelf_getehdr(elf, &ehdr) == NULL) {
1087 pr_debug("%s: cannot get elf header.\n", __func__);
1091 /* Always reject images with a mismatched build-id: */
1092 if (dso->has_build_id) {
1093 u8 build_id[BUILD_ID_SIZE];
1095 if (elf_read_build_id(elf, build_id,
1096 BUILD_ID_SIZE) != BUILD_ID_SIZE)
1099 if (!dso__build_id_equal(dso, build_id))
1103 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
1108 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
1113 opdsec = elf_section_by_name(elf, &ehdr, &opdshdr, ".opd", &opdidx);
1114 if (opdshdr.sh_type != SHT_PROGBITS)
1117 opddata = elf_rawdata(opdsec, NULL);
1119 syms = elf_getdata(sec, NULL);
1123 sec = elf_getscn(elf, shdr.sh_link);
1127 symstrs = elf_getdata(sec, NULL);
1128 if (symstrs == NULL)
1131 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
1132 if (sec_strndx == NULL)
1135 secstrs = elf_getdata(sec_strndx, NULL);
1136 if (secstrs == NULL)
1139 nr_syms = shdr.sh_size / shdr.sh_entsize;
1141 memset(&sym, 0, sizeof(sym));
1142 if (dso->kernel == DSO_TYPE_USER) {
1143 dso->adjust_symbols = (ehdr.e_type == ET_EXEC ||
1144 elf_section_by_name(elf, &ehdr, &shdr,
1145 ".gnu.prelink_undo",
1148 dso->adjust_symbols = 0;
1150 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
1152 const char *elf_name = elf_sym__name(&sym, symstrs);
1153 char *demangled = NULL;
1154 int is_label = elf_sym__is_label(&sym);
1155 const char *section_name;
1157 if (kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name &&
1158 strcmp(elf_name, kmap->ref_reloc_sym->name) == 0)
1159 kmap->ref_reloc_sym->unrelocated_addr = sym.st_value;
1161 if (!is_label && !elf_sym__is_a(&sym, map->type))
1164 /* Reject ARM ELF "mapping symbols": these aren't unique and
1165 * don't identify functions, so will confuse the profile
1167 if (ehdr.e_machine == EM_ARM) {
1168 if (!strcmp(elf_name, "$a") ||
1169 !strcmp(elf_name, "$d") ||
1170 !strcmp(elf_name, "$t"))
1174 if (opdsec && sym.st_shndx == opdidx) {
1175 u32 offset = sym.st_value - opdshdr.sh_addr;
1176 u64 *opd = opddata->d_buf + offset;
1177 sym.st_value = *opd;
1178 sym.st_shndx = elf_addr_to_index(elf, sym.st_value);
1181 sec = elf_getscn(elf, sym.st_shndx);
1185 gelf_getshdr(sec, &shdr);
1187 if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type))
1190 section_name = elf_sec__name(&shdr, secstrs);
1192 /* On ARM, symbols for thumb functions have 1 added to
1193 * the symbol address as a flag - remove it */
1194 if ((ehdr.e_machine == EM_ARM) &&
1195 (map->type == MAP__FUNCTION) &&
1199 if (dso->kernel != DSO_TYPE_USER || kmodule) {
1200 char dso_name[PATH_MAX];
1202 if (strcmp(section_name,
1203 (curr_dso->short_name +
1204 dso->short_name_len)) == 0)
1207 if (strcmp(section_name, ".text") == 0) {
1213 snprintf(dso_name, sizeof(dso_name),
1214 "%s%s", dso->short_name, section_name);
1216 curr_map = map_groups__find_by_name(kmap->kmaps, map->type, dso_name);
1217 if (curr_map == NULL) {
1218 u64 start = sym.st_value;
1221 start += map->start + shdr.sh_offset;
1223 curr_dso = dso__new(dso_name);
1224 if (curr_dso == NULL)
1226 curr_dso->kernel = dso->kernel;
1227 curr_dso->long_name = dso->long_name;
1228 curr_dso->long_name_len = dso->long_name_len;
1229 curr_map = map__new2(start, curr_dso,
1231 if (curr_map == NULL) {
1232 dso__delete(curr_dso);
1235 curr_map->map_ip = identity__map_ip;
1236 curr_map->unmap_ip = identity__map_ip;
1237 curr_dso->symtab_type = dso->symtab_type;
1238 map_groups__insert(kmap->kmaps, curr_map);
1239 dsos__add(&dso->node, curr_dso);
1240 dso__set_loaded(curr_dso, map->type);
1242 curr_dso = curr_map->dso;
1247 if (curr_dso->adjust_symbols) {
1248 pr_debug4("%s: adjusting symbol: st_value: %#" PRIx64 " "
1249 "sh_addr: %#" PRIx64 " sh_offset: %#" PRIx64 "\n", __func__,
1250 (u64)sym.st_value, (u64)shdr.sh_addr,
1251 (u64)shdr.sh_offset);
1252 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
1255 * We need to figure out if the object was created from C++ sources
1256 * DWARF DW_compile_unit has this, but we don't always have access
1259 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
1260 if (demangled != NULL)
1261 elf_name = demangled;
1263 f = symbol__new(sym.st_value, sym.st_size,
1264 GELF_ST_BIND(sym.st_info), elf_name);
1269 if (filter && filter(curr_map, f))
1272 symbols__insert(&curr_dso->symbols[curr_map->type], f);
1278 * For misannotated, zeroed, ASM function sizes.
1281 symbols__fixup_end(&dso->symbols[map->type]);
1284 * We need to fixup this here too because we create new
1285 * maps here, for things like vsyscall sections.
1287 __map_groups__fixup_end(kmap->kmaps, map->type);
1297 static bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
1299 return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
1302 bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
1304 bool have_build_id = false;
1307 list_for_each_entry(pos, head, node) {
1308 if (with_hits && !pos->hit)
1310 if (pos->has_build_id) {
1311 have_build_id = true;
1314 if (filename__read_build_id(pos->long_name, pos->build_id,
1315 sizeof(pos->build_id)) > 0) {
1316 have_build_id = true;
1317 pos->has_build_id = true;
1321 return have_build_id;
1325 * Align offset to 4 bytes as needed for note name and descriptor data.
1327 #define NOTE_ALIGN(n) (((n) + 3) & -4U)
1329 static int elf_read_build_id(Elf *elf, void *bf, size_t size)
1339 if (size < BUILD_ID_SIZE)
1343 if (ek != ELF_K_ELF)
1346 if (gelf_getehdr(elf, &ehdr) == NULL) {
1347 pr_err("%s: cannot get elf header.\n", __func__);
1351 sec = elf_section_by_name(elf, &ehdr, &shdr,
1352 ".note.gnu.build-id", NULL);
1354 sec = elf_section_by_name(elf, &ehdr, &shdr,
1360 data = elf_getdata(sec, NULL);
1365 while (ptr < (data->d_buf + data->d_size)) {
1366 GElf_Nhdr *nhdr = ptr;
1367 int namesz = NOTE_ALIGN(nhdr->n_namesz),
1368 descsz = NOTE_ALIGN(nhdr->n_descsz);
1371 ptr += sizeof(*nhdr);
1374 if (nhdr->n_type == NT_GNU_BUILD_ID &&
1375 nhdr->n_namesz == sizeof("GNU")) {
1376 if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
1377 memcpy(bf, ptr, BUILD_ID_SIZE);
1378 err = BUILD_ID_SIZE;
1389 int filename__read_build_id(const char *filename, void *bf, size_t size)
1394 if (size < BUILD_ID_SIZE)
1397 fd = open(filename, O_RDONLY);
1401 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1403 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
1407 err = elf_read_build_id(elf, bf, size);
1416 int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
1420 if (size < BUILD_ID_SIZE)
1423 fd = open(filename, O_RDONLY);
1432 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
1435 namesz = NOTE_ALIGN(nhdr.n_namesz);
1436 descsz = NOTE_ALIGN(nhdr.n_descsz);
1437 if (nhdr.n_type == NT_GNU_BUILD_ID &&
1438 nhdr.n_namesz == sizeof("GNU")) {
1439 if (read(fd, bf, namesz) != namesz)
1441 if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
1442 if (read(fd, build_id,
1443 BUILD_ID_SIZE) == BUILD_ID_SIZE) {
1447 } else if (read(fd, bf, descsz) != descsz)
1450 int n = namesz + descsz;
1451 if (read(fd, bf, n) != n)
1460 char dso__symtab_origin(const struct dso *dso)
1462 static const char origin[] = {
1463 [SYMTAB__KALLSYMS] = 'k',
1464 [SYMTAB__JAVA_JIT] = 'j',
1465 [SYMTAB__BUILD_ID_CACHE] = 'B',
1466 [SYMTAB__FEDORA_DEBUGINFO] = 'f',
1467 [SYMTAB__UBUNTU_DEBUGINFO] = 'u',
1468 [SYMTAB__BUILDID_DEBUGINFO] = 'b',
1469 [SYMTAB__SYSTEM_PATH_DSO] = 'd',
1470 [SYMTAB__SYSTEM_PATH_KMODULE] = 'K',
1471 [SYMTAB__GUEST_KALLSYMS] = 'g',
1472 [SYMTAB__GUEST_KMODULE] = 'G',
1475 if (dso == NULL || dso->symtab_type == SYMTAB__NOT_FOUND)
1477 return origin[dso->symtab_type];
1480 int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter)
1482 int size = PATH_MAX;
1486 struct machine *machine;
1487 const char *root_dir;
1490 dso__set_loaded(dso, map->type);
1492 if (dso->kernel == DSO_TYPE_KERNEL)
1493 return dso__load_kernel_sym(dso, map, filter);
1494 else if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1495 return dso__load_guest_kernel_sym(dso, map, filter);
1497 if (map->groups && map->groups->machine)
1498 machine = map->groups->machine;
1502 name = malloc(size);
1506 dso->adjust_symbols = 0;
1508 if (strncmp(dso->name, "/tmp/perf-", 10) == 0) {
1509 ret = dso__load_perf_map(dso, map, filter);
1510 dso->symtab_type = ret > 0 ? SYMTAB__JAVA_JIT :
1515 /* Iterate over candidate debug images.
1516 * On the first pass, only load images if they have a full symtab.
1517 * Failing that, do a second pass where we accept .dynsym also
1521 for (dso->symtab_type = SYMTAB__BUILD_ID_CACHE;
1522 dso->symtab_type != SYMTAB__NOT_FOUND;
1523 dso->symtab_type++) {
1524 switch (dso->symtab_type) {
1525 case SYMTAB__BUILD_ID_CACHE:
1526 /* skip the locally configured cache if a symfs is given */
1527 if (symbol_conf.symfs[0] ||
1528 (dso__build_id_filename(dso, name, size) == NULL)) {
1532 case SYMTAB__FEDORA_DEBUGINFO:
1533 snprintf(name, size, "%s/usr/lib/debug%s.debug",
1534 symbol_conf.symfs, dso->long_name);
1536 case SYMTAB__UBUNTU_DEBUGINFO:
1537 snprintf(name, size, "%s/usr/lib/debug%s",
1538 symbol_conf.symfs, dso->long_name);
1540 case SYMTAB__BUILDID_DEBUGINFO: {
1541 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
1543 if (!dso->has_build_id)
1546 build_id__sprintf(dso->build_id,
1547 sizeof(dso->build_id),
1549 snprintf(name, size,
1550 "%s/usr/lib/debug/.build-id/%.2s/%s.debug",
1551 symbol_conf.symfs, build_id_hex, build_id_hex + 2);
1554 case SYMTAB__SYSTEM_PATH_DSO:
1555 snprintf(name, size, "%s%s",
1556 symbol_conf.symfs, dso->long_name);
1558 case SYMTAB__GUEST_KMODULE:
1559 if (map->groups && machine)
1560 root_dir = machine->root_dir;
1563 snprintf(name, size, "%s%s%s", symbol_conf.symfs,
1564 root_dir, dso->long_name);
1567 case SYMTAB__SYSTEM_PATH_KMODULE:
1568 snprintf(name, size, "%s%s", symbol_conf.symfs,
1574 /* Name is now the name of the next image to try */
1575 fd = open(name, O_RDONLY);
1579 ret = dso__load_sym(dso, map, name, fd, filter, 0,
1584 * Some people seem to have debuginfo files _WITHOUT_ debug
1591 int nr_plt = dso__synthesize_plt_symbols(dso, map,
1600 * If we wanted a full symtab but no image had one,
1601 * relax our requirements and repeat the search.
1603 if (ret <= 0 && want_symtab) {
1609 if (ret < 0 && strstr(dso->name, " (deleted)") != NULL)
1614 struct map *map_groups__find_by_name(struct map_groups *mg,
1615 enum map_type type, const char *name)
1619 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
1620 struct map *map = rb_entry(nd, struct map, rb_node);
1622 if (map->dso && strcmp(map->dso->short_name, name) == 0)
1629 static int dso__kernel_module_get_build_id(struct dso *dso,
1630 const char *root_dir)
1632 char filename[PATH_MAX];
1634 * kernel module short names are of the form "[module]" and
1635 * we need just "module" here.
1637 const char *name = dso->short_name + 1;
1639 snprintf(filename, sizeof(filename),
1640 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1641 root_dir, (int)strlen(name) - 1, name);
1643 if (sysfs__read_build_id(filename, dso->build_id,
1644 sizeof(dso->build_id)) == 0)
1645 dso->has_build_id = true;
1650 static int map_groups__set_modules_path_dir(struct map_groups *mg,
1651 const char *dir_name)
1653 struct dirent *dent;
1654 DIR *dir = opendir(dir_name);
1658 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
1662 while ((dent = readdir(dir)) != NULL) {
1663 char path[PATH_MAX];
1666 /*sshfs might return bad dent->d_type, so we have to stat*/
1667 sprintf(path, "%s/%s", dir_name, dent->d_name);
1668 if (stat(path, &st))
1671 if (S_ISDIR(st.st_mode)) {
1672 if (!strcmp(dent->d_name, ".") ||
1673 !strcmp(dent->d_name, ".."))
1676 snprintf(path, sizeof(path), "%s/%s",
1677 dir_name, dent->d_name);
1678 ret = map_groups__set_modules_path_dir(mg, path);
1682 char *dot = strrchr(dent->d_name, '.'),
1687 if (dot == NULL || strcmp(dot, ".ko"))
1689 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1690 (int)(dot - dent->d_name), dent->d_name);
1692 strxfrchar(dso_name, '-', '_');
1693 map = map_groups__find_by_name(mg, MAP__FUNCTION,
1698 snprintf(path, sizeof(path), "%s/%s",
1699 dir_name, dent->d_name);
1701 long_name = strdup(path);
1702 if (long_name == NULL) {
1706 dso__set_long_name(map->dso, long_name);
1707 map->dso->lname_alloc = 1;
1708 dso__kernel_module_get_build_id(map->dso, "");
1717 static char *get_kernel_version(const char *root_dir)
1719 char version[PATH_MAX];
1722 const char *prefix = "Linux version ";
1724 sprintf(version, "%s/proc/version", root_dir);
1725 file = fopen(version, "r");
1730 tmp = fgets(version, sizeof(version), file);
1733 name = strstr(version, prefix);
1736 name += strlen(prefix);
1737 tmp = strchr(name, ' ');
1741 return strdup(name);
1744 static int machine__set_modules_path(struct machine *machine)
1747 char modules_path[PATH_MAX];
1749 version = get_kernel_version(machine->root_dir);
1753 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s/kernel",
1754 machine->root_dir, version);
1757 return map_groups__set_modules_path_dir(&machine->kmaps, modules_path);
1761 * Constructor variant for modules (where we know from /proc/modules where
1762 * they are loaded) and for vmlinux, where only after we load all the
1763 * symbols we'll know where it starts and ends.
1765 static struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
1767 struct map *map = calloc(1, (sizeof(*map) +
1768 (dso->kernel ? sizeof(struct kmap) : 0)));
1771 * ->end will be filled after we load all the symbols
1773 map__init(map, type, start, 0, 0, dso);
1779 struct map *machine__new_module(struct machine *machine, u64 start,
1780 const char *filename)
1783 struct dso *dso = __dsos__findnew(&machine->kernel_dsos, filename);
1788 map = map__new2(start, dso, MAP__FUNCTION);
1792 if (machine__is_host(machine))
1793 dso->symtab_type = SYMTAB__SYSTEM_PATH_KMODULE;
1795 dso->symtab_type = SYMTAB__GUEST_KMODULE;
1796 map_groups__insert(&machine->kmaps, map);
1800 static int machine__create_modules(struct machine *machine)
1806 const char *modules;
1807 char path[PATH_MAX];
1809 if (machine__is_default_guest(machine))
1810 modules = symbol_conf.default_guest_modules;
1812 sprintf(path, "%s/proc/modules", machine->root_dir);
1816 if (symbol__restricted_filename(path, "/proc/modules"))
1819 file = fopen(modules, "r");
1823 while (!feof(file)) {
1824 char name[PATH_MAX];
1829 line_len = getline(&line, &n, file);
1836 line[--line_len] = '\0'; /* \n */
1838 sep = strrchr(line, 'x');
1842 hex2u64(sep + 1, &start);
1844 sep = strchr(line, ' ');
1850 snprintf(name, sizeof(name), "[%s]", line);
1851 map = machine__new_module(machine, start, name);
1853 goto out_delete_line;
1854 dso__kernel_module_get_build_id(map->dso, machine->root_dir);
1860 return machine__set_modules_path(machine);
1868 int dso__load_vmlinux(struct dso *dso, struct map *map,
1869 const char *vmlinux, symbol_filter_t filter)
1872 char symfs_vmlinux[PATH_MAX];
1874 snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s%s",
1875 symbol_conf.symfs, vmlinux);
1876 fd = open(symfs_vmlinux, O_RDONLY);
1880 dso__set_long_name(dso, (char *)vmlinux);
1881 dso__set_loaded(dso, map->type);
1882 err = dso__load_sym(dso, map, symfs_vmlinux, fd, filter, 0, 0);
1886 pr_debug("Using %s for symbols\n", symfs_vmlinux);
1891 int dso__load_vmlinux_path(struct dso *dso, struct map *map,
1892 symbol_filter_t filter)
1897 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1898 vmlinux_path__nr_entries + 1);
1900 filename = dso__build_id_filename(dso, NULL, 0);
1901 if (filename != NULL) {
1902 err = dso__load_vmlinux(dso, map, filename, filter);
1904 dso__set_long_name(dso, filename);
1910 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
1911 err = dso__load_vmlinux(dso, map, vmlinux_path[i], filter);
1913 dso__set_long_name(dso, strdup(vmlinux_path[i]));
1921 static int dso__load_kernel_sym(struct dso *dso, struct map *map,
1922 symbol_filter_t filter)
1925 const char *kallsyms_filename = NULL;
1926 char *kallsyms_allocated_filename = NULL;
1928 * Step 1: if the user specified a kallsyms or vmlinux filename, use
1929 * it and only it, reporting errors to the user if it cannot be used.
1931 * For instance, try to analyse an ARM perf.data file _without_ a
1932 * build-id, or if the user specifies the wrong path to the right
1933 * vmlinux file, obviously we can't fallback to another vmlinux (a
1934 * x86_86 one, on the machine where analysis is being performed, say),
1935 * or worse, /proc/kallsyms.
1937 * If the specified file _has_ a build-id and there is a build-id
1938 * section in the perf.data file, we will still do the expected
1939 * validation in dso__load_vmlinux and will bail out if they don't
1942 if (symbol_conf.kallsyms_name != NULL) {
1943 kallsyms_filename = symbol_conf.kallsyms_name;
1947 if (symbol_conf.vmlinux_name != NULL) {
1948 err = dso__load_vmlinux(dso, map,
1949 symbol_conf.vmlinux_name, filter);
1951 dso__set_long_name(dso,
1952 strdup(symbol_conf.vmlinux_name));
1958 if (vmlinux_path != NULL) {
1959 err = dso__load_vmlinux_path(dso, map, filter);
1964 /* do not try local files if a symfs was given */
1965 if (symbol_conf.symfs[0] != 0)
1969 * Say the kernel DSO was created when processing the build-id header table,
1970 * we have a build-id, so check if it is the same as the running kernel,
1971 * using it if it is.
1973 if (dso->has_build_id) {
1974 u8 kallsyms_build_id[BUILD_ID_SIZE];
1975 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1977 if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id,
1978 sizeof(kallsyms_build_id)) == 0) {
1979 if (dso__build_id_equal(dso, kallsyms_build_id)) {
1980 kallsyms_filename = "/proc/kallsyms";
1985 * Now look if we have it on the build-id cache in
1986 * $HOME/.debug/[kernel.kallsyms].
1988 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1991 if (asprintf(&kallsyms_allocated_filename,
1992 "%s/.debug/[kernel.kallsyms]/%s",
1993 getenv("HOME"), sbuild_id) == -1) {
1994 pr_err("Not enough memory for kallsyms file lookup\n");
1998 kallsyms_filename = kallsyms_allocated_filename;
2000 if (access(kallsyms_filename, F_OK)) {
2001 pr_err("No kallsyms or vmlinux with build-id %s "
2002 "was found\n", sbuild_id);
2003 free(kallsyms_allocated_filename);
2008 * Last resort, if we don't have a build-id and couldn't find
2009 * any vmlinux file, try the running kernel kallsyms table.
2011 kallsyms_filename = "/proc/kallsyms";
2015 err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
2017 pr_debug("Using %s for symbols\n", kallsyms_filename);
2018 free(kallsyms_allocated_filename);
2022 if (kallsyms_filename != NULL)
2023 dso__set_long_name(dso, strdup("[kernel.kallsyms]"));
2024 map__fixup_start(map);
2025 map__fixup_end(map);
2031 static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
2032 symbol_filter_t filter)
2035 const char *kallsyms_filename = NULL;
2036 struct machine *machine;
2037 char path[PATH_MAX];
2040 pr_debug("Guest kernel map hasn't the point to groups\n");
2043 machine = map->groups->machine;
2045 if (machine__is_default_guest(machine)) {
2047 * if the user specified a vmlinux filename, use it and only
2048 * it, reporting errors to the user if it cannot be used.
2049 * Or use file guest_kallsyms inputted by user on commandline
2051 if (symbol_conf.default_guest_vmlinux_name != NULL) {
2052 err = dso__load_vmlinux(dso, map,
2053 symbol_conf.default_guest_vmlinux_name, filter);
2057 kallsyms_filename = symbol_conf.default_guest_kallsyms;
2058 if (!kallsyms_filename)
2061 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
2062 kallsyms_filename = path;
2065 err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
2067 pr_debug("Using %s for symbols\n", kallsyms_filename);
2071 if (kallsyms_filename != NULL) {
2072 machine__mmap_name(machine, path, sizeof(path));
2073 dso__set_long_name(dso, strdup(path));
2075 map__fixup_start(map);
2076 map__fixup_end(map);
2082 static void dsos__add(struct list_head *head, struct dso *dso)
2084 list_add_tail(&dso->node, head);
2087 static struct dso *dsos__find(struct list_head *head, const char *name)
2091 list_for_each_entry(pos, head, node)
2092 if (strcmp(pos->long_name, name) == 0)
2097 struct dso *__dsos__findnew(struct list_head *head, const char *name)
2099 struct dso *dso = dsos__find(head, name);
2102 dso = dso__new(name);
2104 dsos__add(head, dso);
2105 dso__set_basename(dso);
2112 size_t __dsos__fprintf(struct list_head *head, FILE *fp)
2117 list_for_each_entry(pos, head, node) {
2119 for (i = 0; i < MAP__NR_TYPES; ++i)
2120 ret += dso__fprintf(pos, i, fp);
2126 size_t machines__fprintf_dsos(struct rb_root *machines, FILE *fp)
2131 for (nd = rb_first(machines); nd; nd = rb_next(nd)) {
2132 struct machine *pos = rb_entry(nd, struct machine, rb_node);
2133 ret += __dsos__fprintf(&pos->kernel_dsos, fp);
2134 ret += __dsos__fprintf(&pos->user_dsos, fp);
2140 static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
2146 list_for_each_entry(pos, head, node) {
2147 if (with_hits && !pos->hit)
2149 ret += dso__fprintf_buildid(pos, fp);
2150 ret += fprintf(fp, " %s\n", pos->long_name);
2155 size_t machine__fprintf_dsos_buildid(struct machine *machine, FILE *fp,
2158 return __dsos__fprintf_buildid(&machine->kernel_dsos, fp, with_hits) +
2159 __dsos__fprintf_buildid(&machine->user_dsos, fp, with_hits);
2162 size_t machines__fprintf_dsos_buildid(struct rb_root *machines,
2163 FILE *fp, bool with_hits)
2168 for (nd = rb_first(machines); nd; nd = rb_next(nd)) {
2169 struct machine *pos = rb_entry(nd, struct machine, rb_node);
2170 ret += machine__fprintf_dsos_buildid(pos, fp, with_hits);
2175 struct dso *dso__new_kernel(const char *name)
2177 struct dso *dso = dso__new(name ?: "[kernel.kallsyms]");
2180 dso__set_short_name(dso, "[kernel]");
2181 dso->kernel = DSO_TYPE_KERNEL;
2187 static struct dso *dso__new_guest_kernel(struct machine *machine,
2191 struct dso *dso = dso__new(name ?: machine__mmap_name(machine, bf,
2194 dso__set_short_name(dso, "[guest.kernel]");
2195 dso->kernel = DSO_TYPE_GUEST_KERNEL;
2201 void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
2203 char path[PATH_MAX];
2205 if (machine__is_default_guest(machine))
2207 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
2208 if (sysfs__read_build_id(path, dso->build_id,
2209 sizeof(dso->build_id)) == 0)
2210 dso->has_build_id = true;
2213 static struct dso *machine__create_kernel(struct machine *machine)
2215 const char *vmlinux_name = NULL;
2218 if (machine__is_host(machine)) {
2219 vmlinux_name = symbol_conf.vmlinux_name;
2220 kernel = dso__new_kernel(vmlinux_name);
2222 if (machine__is_default_guest(machine))
2223 vmlinux_name = symbol_conf.default_guest_vmlinux_name;
2224 kernel = dso__new_guest_kernel(machine, vmlinux_name);
2227 if (kernel != NULL) {
2228 dso__read_running_kernel_build_id(kernel, machine);
2229 dsos__add(&machine->kernel_dsos, kernel);
2234 struct process_args {
2238 static int symbol__in_kernel(void *arg, const char *name,
2239 char type __used, u64 start, u64 end __used)
2241 struct process_args *args = arg;
2243 if (strchr(name, '['))
2246 args->start = start;
2250 /* Figure out the start address of kernel map from /proc/kallsyms */
2251 static u64 machine__get_kernel_start_addr(struct machine *machine)
2253 const char *filename;
2254 char path[PATH_MAX];
2255 struct process_args args;
2257 if (machine__is_host(machine)) {
2258 filename = "/proc/kallsyms";
2260 if (machine__is_default_guest(machine))
2261 filename = (char *)symbol_conf.default_guest_kallsyms;
2263 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
2268 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
2271 if (kallsyms__parse(filename, &args, symbol__in_kernel) <= 0)
2277 int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
2280 u64 start = machine__get_kernel_start_addr(machine);
2282 for (type = 0; type < MAP__NR_TYPES; ++type) {
2285 machine->vmlinux_maps[type] = map__new2(start, kernel, type);
2286 if (machine->vmlinux_maps[type] == NULL)
2289 machine->vmlinux_maps[type]->map_ip =
2290 machine->vmlinux_maps[type]->unmap_ip =
2292 kmap = map__kmap(machine->vmlinux_maps[type]);
2293 kmap->kmaps = &machine->kmaps;
2294 map_groups__insert(&machine->kmaps,
2295 machine->vmlinux_maps[type]);
2301 void machine__destroy_kernel_maps(struct machine *machine)
2305 for (type = 0; type < MAP__NR_TYPES; ++type) {
2308 if (machine->vmlinux_maps[type] == NULL)
2311 kmap = map__kmap(machine->vmlinux_maps[type]);
2312 map_groups__remove(&machine->kmaps,
2313 machine->vmlinux_maps[type]);
2314 if (kmap->ref_reloc_sym) {
2316 * ref_reloc_sym is shared among all maps, so free just
2319 if (type == MAP__FUNCTION) {
2320 free((char *)kmap->ref_reloc_sym->name);
2321 kmap->ref_reloc_sym->name = NULL;
2322 free(kmap->ref_reloc_sym);
2324 kmap->ref_reloc_sym = NULL;
2327 map__delete(machine->vmlinux_maps[type]);
2328 machine->vmlinux_maps[type] = NULL;
2332 int machine__create_kernel_maps(struct machine *machine)
2334 struct dso *kernel = machine__create_kernel(machine);
2336 if (kernel == NULL ||
2337 __machine__create_kernel_maps(machine, kernel) < 0)
2340 if (symbol_conf.use_modules && machine__create_modules(machine) < 0)
2341 pr_debug("Problems creating module maps, continuing anyway...\n");
2343 * Now that we have all the maps created, just set the ->end of them:
2345 map_groups__fixup_end(&machine->kmaps);
2349 static void vmlinux_path__exit(void)
2351 while (--vmlinux_path__nr_entries >= 0) {
2352 free(vmlinux_path[vmlinux_path__nr_entries]);
2353 vmlinux_path[vmlinux_path__nr_entries] = NULL;
2357 vmlinux_path = NULL;
2360 static int vmlinux_path__init(void)
2365 vmlinux_path = malloc(sizeof(char *) * 5);
2366 if (vmlinux_path == NULL)
2369 vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
2370 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2372 ++vmlinux_path__nr_entries;
2373 vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
2374 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2376 ++vmlinux_path__nr_entries;
2378 /* only try running kernel version if no symfs was given */
2379 if (symbol_conf.symfs[0] != 0)
2382 if (uname(&uts) < 0)
2385 snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
2386 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
2387 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2389 ++vmlinux_path__nr_entries;
2390 snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
2391 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
2392 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2394 ++vmlinux_path__nr_entries;
2395 snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
2397 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
2398 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2400 ++vmlinux_path__nr_entries;
2405 vmlinux_path__exit();
2409 size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
2413 struct dso *kdso = machine->vmlinux_maps[MAP__FUNCTION]->dso;
2415 if (kdso->has_build_id) {
2416 char filename[PATH_MAX];
2417 if (dso__build_id_filename(kdso, filename, sizeof(filename)))
2418 printed += fprintf(fp, "[0] %s\n", filename);
2421 for (i = 0; i < vmlinux_path__nr_entries; ++i)
2422 printed += fprintf(fp, "[%d] %s\n",
2423 i + kdso->has_build_id, vmlinux_path[i]);
2428 static int setup_list(struct strlist **list, const char *list_str,
2429 const char *list_name)
2431 if (list_str == NULL)
2434 *list = strlist__new(true, list_str);
2436 pr_err("problems parsing %s list\n", list_name);
2442 static bool symbol__read_kptr_restrict(void)
2446 if (geteuid() != 0) {
2447 FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
2451 if (fgets(line, sizeof(line), fp) != NULL)
2452 value = atoi(line) != 0;
2461 int symbol__init(void)
2465 if (symbol_conf.initialized)
2468 symbol_conf.priv_size = ALIGN(symbol_conf.priv_size, sizeof(u64));
2470 elf_version(EV_CURRENT);
2471 if (symbol_conf.sort_by_name)
2472 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
2473 sizeof(struct symbol));
2475 if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
2478 if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
2479 pr_err("'.' is the only non valid --field-separator argument\n");
2483 if (setup_list(&symbol_conf.dso_list,
2484 symbol_conf.dso_list_str, "dso") < 0)
2487 if (setup_list(&symbol_conf.comm_list,
2488 symbol_conf.comm_list_str, "comm") < 0)
2489 goto out_free_dso_list;
2491 if (setup_list(&symbol_conf.sym_list,
2492 symbol_conf.sym_list_str, "symbol") < 0)
2493 goto out_free_comm_list;
2496 * A path to symbols of "/" is identical to ""
2497 * reset here for simplicity.
2499 symfs = realpath(symbol_conf.symfs, NULL);
2501 symfs = symbol_conf.symfs;
2502 if (strcmp(symfs, "/") == 0)
2503 symbol_conf.symfs = "";
2504 if (symfs != symbol_conf.symfs)
2505 free((void *)symfs);
2507 symbol_conf.kptr_restrict = symbol__read_kptr_restrict();
2509 symbol_conf.initialized = true;
2513 strlist__delete(symbol_conf.dso_list);
2515 strlist__delete(symbol_conf.comm_list);
2519 void symbol__exit(void)
2521 if (!symbol_conf.initialized)
2523 strlist__delete(symbol_conf.sym_list);
2524 strlist__delete(symbol_conf.dso_list);
2525 strlist__delete(symbol_conf.comm_list);
2526 vmlinux_path__exit();
2527 symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
2528 symbol_conf.initialized = false;
2531 int machines__create_kernel_maps(struct rb_root *machines, pid_t pid)
2533 struct machine *machine = machines__findnew(machines, pid);
2535 if (machine == NULL)
2538 return machine__create_kernel_maps(machine);
2541 static int hex(char ch)
2543 if ((ch >= '0') && (ch <= '9'))
2545 if ((ch >= 'a') && (ch <= 'f'))
2546 return ch - 'a' + 10;
2547 if ((ch >= 'A') && (ch <= 'F'))
2548 return ch - 'A' + 10;
2553 * While we find nice hex chars, build a long_val.
2554 * Return number of chars processed.
2556 int hex2u64(const char *ptr, u64 *long_val)
2558 const char *p = ptr;
2562 const int hex_val = hex(*p);
2567 *long_val = (*long_val << 4) | hex_val;
2574 char *strxfrchar(char *s, char from, char to)
2578 while ((p = strchr(p, from)) != NULL)
2584 int machines__create_guest_kernel_maps(struct rb_root *machines)
2587 struct dirent **namelist = NULL;
2589 char path[PATH_MAX];
2592 if (symbol_conf.default_guest_vmlinux_name ||
2593 symbol_conf.default_guest_modules ||
2594 symbol_conf.default_guest_kallsyms) {
2595 machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
2598 if (symbol_conf.guestmount) {
2599 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
2602 for (i = 0; i < items; i++) {
2603 if (!isdigit(namelist[i]->d_name[0])) {
2604 /* Filter out . and .. */
2607 pid = atoi(namelist[i]->d_name);
2608 sprintf(path, "%s/%s/proc/kallsyms",
2609 symbol_conf.guestmount,
2610 namelist[i]->d_name);
2611 ret = access(path, R_OK);
2613 pr_debug("Can't access file %s\n", path);
2616 machines__create_kernel_maps(machines, pid);
2625 void machines__destroy_guest_kernel_maps(struct rb_root *machines)
2627 struct rb_node *next = rb_first(machines);
2630 struct machine *pos = rb_entry(next, struct machine, rb_node);
2632 next = rb_next(&pos->rb_node);
2633 rb_erase(&pos->rb_node, machines);
2634 machine__delete(pos);
2638 int machine__load_kallsyms(struct machine *machine, const char *filename,
2639 enum map_type type, symbol_filter_t filter)
2641 struct map *map = machine->vmlinux_maps[type];
2642 int ret = dso__load_kallsyms(map->dso, filename, map, filter);
2645 dso__set_loaded(map->dso, type);
2647 * Since /proc/kallsyms will have multiple sessions for the
2648 * kernel, with modules between them, fixup the end of all
2651 __map_groups__fixup_end(&machine->kmaps, type);
2657 int machine__load_vmlinux_path(struct machine *machine, enum map_type type,
2658 symbol_filter_t filter)
2660 struct map *map = machine->vmlinux_maps[type];
2661 int ret = dso__load_vmlinux_path(map->dso, map, filter);
2664 dso__set_loaded(map->dso, type);
2665 map__reloc_vmlinux(map);