22 #include <sys/utsname.h>
25 #define KSYM_NAME_LEN 256
28 #ifndef NT_GNU_BUILD_ID
29 #define NT_GNU_BUILD_ID 3
32 static void dso_cache__free(struct rb_root *root);
33 static bool dso__build_id_equal(const struct dso *dso, u8 *build_id);
34 static int elf_read_build_id(Elf *elf, void *bf, size_t size);
35 static void dsos__add(struct list_head *head, struct dso *dso);
36 static struct map *map__new2(u64 start, struct dso *dso, enum map_type type);
37 static int dso__load_kernel_sym(struct dso *dso, struct map *map,
38 symbol_filter_t filter);
39 static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
40 symbol_filter_t filter);
41 static int vmlinux_path__nr_entries;
42 static char **vmlinux_path;
44 struct symbol_conf symbol_conf = {
45 .exclude_other = true,
47 .try_vmlinux_path = true,
52 static enum dso_binary_type binary_type_symtab[] = {
53 DSO_BINARY_TYPE__KALLSYMS,
54 DSO_BINARY_TYPE__GUEST_KALLSYMS,
55 DSO_BINARY_TYPE__JAVA_JIT,
56 DSO_BINARY_TYPE__DEBUGLINK,
57 DSO_BINARY_TYPE__BUILD_ID_CACHE,
58 DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
59 DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
60 DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
61 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
62 DSO_BINARY_TYPE__GUEST_KMODULE,
63 DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE,
64 DSO_BINARY_TYPE__NOT_FOUND,
67 #define DSO_BINARY_TYPE__SYMTAB_CNT ARRAY_SIZE(binary_type_symtab)
69 static enum dso_binary_type binary_type_data[] = {
70 DSO_BINARY_TYPE__BUILD_ID_CACHE,
71 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
72 DSO_BINARY_TYPE__NOT_FOUND,
75 #define DSO_BINARY_TYPE__DATA_CNT ARRAY_SIZE(binary_type_data)
77 int dso__name_len(const struct dso *dso)
80 return strlen("[unknown]");
82 return dso->long_name_len;
84 return dso->short_name_len;
87 bool dso__loaded(const struct dso *dso, enum map_type type)
89 return dso->loaded & (1 << type);
92 bool dso__sorted_by_name(const struct dso *dso, enum map_type type)
94 return dso->sorted_by_name & (1 << type);
97 static void dso__set_sorted_by_name(struct dso *dso, enum map_type type)
99 dso->sorted_by_name |= (1 << type);
102 bool symbol_type__is_a(char symbol_type, enum map_type map_type)
104 symbol_type = toupper(symbol_type);
108 return symbol_type == 'T' || symbol_type == 'W';
110 return symbol_type == 'D';
116 static int prefix_underscores_count(const char *str)
118 const char *tail = str;
129 static int choose_best_symbol(struct symbol *syma, struct symbol *symb)
134 /* Prefer a symbol with non zero length */
135 a = syma->end - syma->start;
136 b = symb->end - symb->start;
137 if ((b == 0) && (a > 0))
139 else if ((a == 0) && (b > 0))
142 /* Prefer a non weak symbol over a weak one */
143 a = syma->binding == STB_WEAK;
144 b = symb->binding == STB_WEAK;
150 /* Prefer a global symbol over a non global one */
151 a = syma->binding == STB_GLOBAL;
152 b = symb->binding == STB_GLOBAL;
158 /* Prefer a symbol with less underscores */
159 a = prefix_underscores_count(syma->name);
160 b = prefix_underscores_count(symb->name);
166 /* If all else fails, choose the symbol with the longest name */
167 if (strlen(syma->name) >= strlen(symb->name))
173 static void symbols__fixup_duplicate(struct rb_root *symbols)
176 struct symbol *curr, *next;
178 nd = rb_first(symbols);
181 curr = rb_entry(nd, struct symbol, rb_node);
183 nd = rb_next(&curr->rb_node);
184 next = rb_entry(nd, struct symbol, rb_node);
189 if (curr->start != next->start)
192 if (choose_best_symbol(curr, next) == SYMBOL_A) {
193 rb_erase(&next->rb_node, symbols);
196 nd = rb_next(&curr->rb_node);
197 rb_erase(&curr->rb_node, symbols);
202 static void symbols__fixup_end(struct rb_root *symbols)
204 struct rb_node *nd, *prevnd = rb_first(symbols);
205 struct symbol *curr, *prev;
210 curr = rb_entry(prevnd, struct symbol, rb_node);
212 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
214 curr = rb_entry(nd, struct symbol, rb_node);
216 if (prev->end == prev->start && prev->end != curr->start)
217 prev->end = curr->start - 1;
221 if (curr->end == curr->start)
222 curr->end = roundup(curr->start, 4096);
225 static void __map_groups__fixup_end(struct map_groups *mg, enum map_type type)
227 struct map *prev, *curr;
228 struct rb_node *nd, *prevnd = rb_first(&mg->maps[type]);
233 curr = rb_entry(prevnd, struct map, rb_node);
235 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
237 curr = rb_entry(nd, struct map, rb_node);
238 prev->end = curr->start - 1;
242 * We still haven't the actual symbols, so guess the
243 * last map final address.
248 static void map_groups__fixup_end(struct map_groups *mg)
251 for (i = 0; i < MAP__NR_TYPES; ++i)
252 __map_groups__fixup_end(mg, i);
255 static struct symbol *symbol__new(u64 start, u64 len, u8 binding,
258 size_t namelen = strlen(name) + 1;
259 struct symbol *sym = calloc(1, (symbol_conf.priv_size +
260 sizeof(*sym) + namelen));
264 if (symbol_conf.priv_size)
265 sym = ((void *)sym) + symbol_conf.priv_size;
268 sym->end = len ? start + len - 1 : start;
269 sym->binding = binding;
270 sym->namelen = namelen - 1;
272 pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n",
273 __func__, name, start, sym->end);
274 memcpy(sym->name, name, namelen);
279 void symbol__delete(struct symbol *sym)
281 free(((void *)sym) - symbol_conf.priv_size);
284 static size_t symbol__fprintf(struct symbol *sym, FILE *fp)
286 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %c %s\n",
287 sym->start, sym->end,
288 sym->binding == STB_GLOBAL ? 'g' :
289 sym->binding == STB_LOCAL ? 'l' : 'w',
293 size_t symbol__fprintf_symname_offs(const struct symbol *sym,
294 const struct addr_location *al, FILE *fp)
296 unsigned long offset;
299 if (sym && sym->name) {
300 length = fprintf(fp, "%s", sym->name);
302 offset = al->addr - sym->start;
303 length += fprintf(fp, "+0x%lx", offset);
307 return fprintf(fp, "[unknown]");
310 size_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp)
312 return symbol__fprintf_symname_offs(sym, NULL, fp);
315 void dso__set_long_name(struct dso *dso, char *name)
319 dso->long_name = name;
320 dso->long_name_len = strlen(name);
323 static void dso__set_short_name(struct dso *dso, const char *name)
327 dso->short_name = name;
328 dso->short_name_len = strlen(name);
331 static void dso__set_basename(struct dso *dso)
333 dso__set_short_name(dso, basename(dso->long_name));
336 struct dso *dso__new(const char *name)
338 struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
342 strcpy(dso->name, name);
343 dso__set_long_name(dso, dso->name);
344 dso__set_short_name(dso, dso->name);
345 for (i = 0; i < MAP__NR_TYPES; ++i)
346 dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
347 dso->cache = RB_ROOT;
348 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
349 dso->data_type = DSO_BINARY_TYPE__NOT_FOUND;
351 dso->sorted_by_name = 0;
352 dso->has_build_id = 0;
353 dso->kernel = DSO_TYPE_USER;
354 dso->needs_swap = DSO_SWAP__UNSET;
355 INIT_LIST_HEAD(&dso->node);
361 static void symbols__delete(struct rb_root *symbols)
364 struct rb_node *next = rb_first(symbols);
367 pos = rb_entry(next, struct symbol, rb_node);
368 next = rb_next(&pos->rb_node);
369 rb_erase(&pos->rb_node, symbols);
374 void dso__delete(struct dso *dso)
377 for (i = 0; i < MAP__NR_TYPES; ++i)
378 symbols__delete(&dso->symbols[i]);
379 if (dso->sname_alloc)
380 free((char *)dso->short_name);
381 if (dso->lname_alloc)
382 free(dso->long_name);
383 dso_cache__free(&dso->cache);
387 void dso__set_build_id(struct dso *dso, void *build_id)
389 memcpy(dso->build_id, build_id, sizeof(dso->build_id));
390 dso->has_build_id = 1;
393 static void symbols__insert(struct rb_root *symbols, struct symbol *sym)
395 struct rb_node **p = &symbols->rb_node;
396 struct rb_node *parent = NULL;
397 const u64 ip = sym->start;
402 s = rb_entry(parent, struct symbol, rb_node);
408 rb_link_node(&sym->rb_node, parent, p);
409 rb_insert_color(&sym->rb_node, symbols);
412 static struct symbol *symbols__find(struct rb_root *symbols, u64 ip)
419 n = symbols->rb_node;
422 struct symbol *s = rb_entry(n, struct symbol, rb_node);
426 else if (ip > s->end)
435 struct symbol_name_rb_node {
436 struct rb_node rb_node;
440 static void symbols__insert_by_name(struct rb_root *symbols, struct symbol *sym)
442 struct rb_node **p = &symbols->rb_node;
443 struct rb_node *parent = NULL;
444 struct symbol_name_rb_node *symn, *s;
446 symn = container_of(sym, struct symbol_name_rb_node, sym);
450 s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
451 if (strcmp(sym->name, s->sym.name) < 0)
456 rb_link_node(&symn->rb_node, parent, p);
457 rb_insert_color(&symn->rb_node, symbols);
460 static void symbols__sort_by_name(struct rb_root *symbols,
461 struct rb_root *source)
465 for (nd = rb_first(source); nd; nd = rb_next(nd)) {
466 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
467 symbols__insert_by_name(symbols, pos);
471 static struct symbol *symbols__find_by_name(struct rb_root *symbols,
479 n = symbols->rb_node;
482 struct symbol_name_rb_node *s;
485 s = rb_entry(n, struct symbol_name_rb_node, rb_node);
486 cmp = strcmp(name, s->sym.name);
499 struct symbol *dso__find_symbol(struct dso *dso,
500 enum map_type type, u64 addr)
502 return symbols__find(&dso->symbols[type], addr);
505 struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
508 return symbols__find_by_name(&dso->symbol_names[type], name);
511 void dso__sort_by_name(struct dso *dso, enum map_type type)
513 dso__set_sorted_by_name(dso, type);
514 return symbols__sort_by_name(&dso->symbol_names[type],
515 &dso->symbols[type]);
518 int build_id__sprintf(const u8 *build_id, int len, char *bf)
521 const u8 *raw = build_id;
524 for (i = 0; i < len; ++i) {
525 sprintf(bid, "%02x", *raw);
530 return raw - build_id;
533 size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
535 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
537 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
538 return fprintf(fp, "%s", sbuild_id);
541 size_t dso__fprintf_symbols_by_name(struct dso *dso,
542 enum map_type type, FILE *fp)
546 struct symbol_name_rb_node *pos;
548 for (nd = rb_first(&dso->symbol_names[type]); nd; nd = rb_next(nd)) {
549 pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
550 fprintf(fp, "%s\n", pos->sym.name);
556 size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
559 size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
561 if (dso->short_name != dso->long_name)
562 ret += fprintf(fp, "%s, ", dso->long_name);
563 ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
564 dso->loaded ? "" : "NOT ");
565 ret += dso__fprintf_buildid(dso, fp);
566 ret += fprintf(fp, ")\n");
567 for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) {
568 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
569 ret += symbol__fprintf(pos, fp);
575 int kallsyms__parse(const char *filename, void *arg,
576 int (*process_symbol)(void *arg, const char *name,
577 char type, u64 start, u64 end))
582 FILE *file = fopen(filename, "r");
589 while (!feof(file)) {
595 line_len = getline(&line, &n, file);
596 if (line_len < 0 || !line)
599 line[--line_len] = '\0'; /* \n */
601 len = hex2u64(line, &start);
604 if (len + 2 >= line_len)
607 symbol_type = line[len];
609 symbol_name = line + len;
610 len = line_len - len;
612 if (len >= KSYM_NAME_LEN) {
618 * module symbols are not sorted so we add all
619 * symbols with zero length and rely on
620 * symbols__fixup_end() to fix it up.
622 err = process_symbol(arg, symbol_name,
623 symbol_type, start, start);
636 struct process_kallsyms_args {
641 static u8 kallsyms2elf_type(char type)
646 return isupper(type) ? STB_GLOBAL : STB_LOCAL;
649 static int map__process_kallsym_symbol(void *arg, const char *name,
650 char type, u64 start, u64 end)
653 struct process_kallsyms_args *a = arg;
654 struct rb_root *root = &a->dso->symbols[a->map->type];
656 if (!symbol_type__is_a(type, a->map->type))
659 sym = symbol__new(start, end - start + 1,
660 kallsyms2elf_type(type), name);
664 * We will pass the symbols to the filter later, in
665 * map__split_kallsyms, when we have split the maps per module
667 symbols__insert(root, sym);
673 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
674 * so that we can in the next step set the symbol ->end address and then
675 * call kernel_maps__split_kallsyms.
677 static int dso__load_all_kallsyms(struct dso *dso, const char *filename,
680 struct process_kallsyms_args args = { .map = map, .dso = dso, };
681 return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
685 * Split the symbols into maps, making sure there are no overlaps, i.e. the
686 * kernel range is broken in several maps, named [kernel].N, as we don't have
687 * the original ELF section names vmlinux have.
689 static int dso__split_kallsyms(struct dso *dso, struct map *map,
690 symbol_filter_t filter)
692 struct map_groups *kmaps = map__kmap(map)->kmaps;
693 struct machine *machine = kmaps->machine;
694 struct map *curr_map = map;
696 int count = 0, moved = 0;
697 struct rb_root *root = &dso->symbols[map->type];
698 struct rb_node *next = rb_first(root);
699 int kernel_range = 0;
704 pos = rb_entry(next, struct symbol, rb_node);
705 next = rb_next(&pos->rb_node);
707 module = strchr(pos->name, '\t');
709 if (!symbol_conf.use_modules)
714 if (strcmp(curr_map->dso->short_name, module)) {
715 if (curr_map != map &&
716 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
717 machine__is_default_guest(machine)) {
719 * We assume all symbols of a module are
720 * continuous in * kallsyms, so curr_map
721 * points to a module and all its
722 * symbols are in its kmap. Mark it as
725 dso__set_loaded(curr_map->dso,
729 curr_map = map_groups__find_by_name(kmaps,
731 if (curr_map == NULL) {
732 pr_debug("%s/proc/{kallsyms,modules} "
733 "inconsistency while looking "
734 "for \"%s\" module!\n",
735 machine->root_dir, module);
740 if (curr_map->dso->loaded &&
741 !machine__is_default_guest(machine))
745 * So that we look just like we get from .ko files,
746 * i.e. not prelinked, relative to map->start.
748 pos->start = curr_map->map_ip(curr_map, pos->start);
749 pos->end = curr_map->map_ip(curr_map, pos->end);
750 } else if (curr_map != map) {
751 char dso_name[PATH_MAX];
759 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
760 snprintf(dso_name, sizeof(dso_name),
764 snprintf(dso_name, sizeof(dso_name),
768 ndso = dso__new(dso_name);
772 ndso->kernel = dso->kernel;
774 curr_map = map__new2(pos->start, ndso, map->type);
775 if (curr_map == NULL) {
780 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
781 map_groups__insert(kmaps, curr_map);
785 if (filter && filter(curr_map, pos)) {
786 discard_symbol: rb_erase(&pos->rb_node, root);
789 if (curr_map != map) {
790 rb_erase(&pos->rb_node, root);
791 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
798 if (curr_map != map &&
799 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
800 machine__is_default_guest(kmaps->machine)) {
801 dso__set_loaded(curr_map->dso, curr_map->type);
804 return count + moved;
807 static bool symbol__restricted_filename(const char *filename,
808 const char *restricted_filename)
810 bool restricted = false;
812 if (symbol_conf.kptr_restrict) {
813 char *r = realpath(filename, NULL);
816 restricted = strcmp(r, restricted_filename) == 0;
825 int dso__load_kallsyms(struct dso *dso, const char *filename,
826 struct map *map, symbol_filter_t filter)
828 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
831 if (dso__load_all_kallsyms(dso, filename, map) < 0)
834 symbols__fixup_duplicate(&dso->symbols[map->type]);
835 symbols__fixup_end(&dso->symbols[map->type]);
837 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
838 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
840 dso->symtab_type = DSO_BINARY_TYPE__KALLSYMS;
842 return dso__split_kallsyms(dso, map, filter);
845 static int dso__load_perf_map(struct dso *dso, struct map *map,
846 symbol_filter_t filter)
853 file = fopen(dso->long_name, "r");
857 while (!feof(file)) {
862 line_len = getline(&line, &n, file);
869 line[--line_len] = '\0'; /* \n */
871 len = hex2u64(line, &start);
874 if (len + 2 >= line_len)
877 len += hex2u64(line + len, &size);
880 if (len + 2 >= line_len)
883 sym = symbol__new(start, size, STB_GLOBAL, line + len);
886 goto out_delete_line;
888 if (filter && filter(map, sym))
891 symbols__insert(&dso->symbols[map->type], sym);
908 * elf_symtab__for_each_symbol - iterate thru all the symbols
910 * @syms: struct elf_symtab instance to iterate
912 * @sym: GElf_Sym iterator
914 #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
915 for (idx = 0, gelf_getsym(syms, idx, &sym);\
917 idx++, gelf_getsym(syms, idx, &sym))
919 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
921 return GELF_ST_TYPE(sym->st_info);
924 static inline int elf_sym__is_function(const GElf_Sym *sym)
926 return elf_sym__type(sym) == STT_FUNC &&
928 sym->st_shndx != SHN_UNDEF;
931 static inline bool elf_sym__is_object(const GElf_Sym *sym)
933 return elf_sym__type(sym) == STT_OBJECT &&
935 sym->st_shndx != SHN_UNDEF;
938 static inline int elf_sym__is_label(const GElf_Sym *sym)
940 return elf_sym__type(sym) == STT_NOTYPE &&
942 sym->st_shndx != SHN_UNDEF &&
943 sym->st_shndx != SHN_ABS;
946 static inline const char *elf_sec__name(const GElf_Shdr *shdr,
947 const Elf_Data *secstrs)
949 return secstrs->d_buf + shdr->sh_name;
952 static inline int elf_sec__is_text(const GElf_Shdr *shdr,
953 const Elf_Data *secstrs)
955 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
958 static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
959 const Elf_Data *secstrs)
961 return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
964 static inline const char *elf_sym__name(const GElf_Sym *sym,
965 const Elf_Data *symstrs)
967 return symstrs->d_buf + sym->st_name;
970 static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
971 GElf_Shdr *shp, const char *name,
977 while ((sec = elf_nextscn(elf, sec)) != NULL) {
980 gelf_getshdr(sec, shp);
981 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
982 if (!strcmp(name, str)) {
993 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
994 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
996 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
998 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
999 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
1001 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
1004 * We need to check if we have a .dynsym, so that we can handle the
1005 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
1006 * .dynsym or .symtab).
1007 * And always look at the original dso, not at debuginfo packages, that
1008 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
1011 dso__synthesize_plt_symbols(struct dso *dso, char *name, struct map *map,
1012 symbol_filter_t filter)
1014 uint32_t nr_rel_entries, idx;
1019 GElf_Shdr shdr_rel_plt, shdr_dynsym;
1020 Elf_Data *reldata, *syms, *symstrs;
1021 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
1024 char sympltname[1024];
1026 int nr = 0, symidx, fd, err = 0;
1028 fd = open(name, O_RDONLY);
1032 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1036 if (gelf_getehdr(elf, &ehdr) == NULL)
1039 scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
1040 ".dynsym", &dynsym_idx);
1041 if (scn_dynsym == NULL)
1044 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
1046 if (scn_plt_rel == NULL) {
1047 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
1049 if (scn_plt_rel == NULL)
1055 if (shdr_rel_plt.sh_link != dynsym_idx)
1058 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
1062 * Fetch the relocation section to find the idxes to the GOT
1063 * and the symbols in the .dynsym they refer to.
1065 reldata = elf_getdata(scn_plt_rel, NULL);
1066 if (reldata == NULL)
1069 syms = elf_getdata(scn_dynsym, NULL);
1073 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
1074 if (scn_symstrs == NULL)
1077 symstrs = elf_getdata(scn_symstrs, NULL);
1078 if (symstrs == NULL)
1081 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
1082 plt_offset = shdr_plt.sh_offset;
1084 if (shdr_rel_plt.sh_type == SHT_RELA) {
1085 GElf_Rela pos_mem, *pos;
1087 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
1089 symidx = GELF_R_SYM(pos->r_info);
1090 plt_offset += shdr_plt.sh_entsize;
1091 gelf_getsym(syms, symidx, &sym);
1092 snprintf(sympltname, sizeof(sympltname),
1093 "%s@plt", elf_sym__name(&sym, symstrs));
1095 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
1096 STB_GLOBAL, sympltname);
1100 if (filter && filter(map, f))
1103 symbols__insert(&dso->symbols[map->type], f);
1107 } else if (shdr_rel_plt.sh_type == SHT_REL) {
1108 GElf_Rel pos_mem, *pos;
1109 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
1111 symidx = GELF_R_SYM(pos->r_info);
1112 plt_offset += shdr_plt.sh_entsize;
1113 gelf_getsym(syms, symidx, &sym);
1114 snprintf(sympltname, sizeof(sympltname),
1115 "%s@plt", elf_sym__name(&sym, symstrs));
1117 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
1118 STB_GLOBAL, sympltname);
1122 if (filter && filter(map, f))
1125 symbols__insert(&dso->symbols[map->type], f);
1140 pr_debug("%s: problems reading %s PLT info.\n",
1141 __func__, dso->long_name);
1145 static bool elf_sym__is_a(GElf_Sym *sym, enum map_type type)
1149 return elf_sym__is_function(sym);
1151 return elf_sym__is_object(sym);
1157 static bool elf_sec__is_a(GElf_Shdr *shdr, Elf_Data *secstrs,
1162 return elf_sec__is_text(shdr, secstrs);
1164 return elf_sec__is_data(shdr, secstrs);
1170 static size_t elf_addr_to_index(Elf *elf, GElf_Addr addr)
1172 Elf_Scn *sec = NULL;
1176 while ((sec = elf_nextscn(elf, sec)) != NULL) {
1177 gelf_getshdr(sec, &shdr);
1179 if ((addr >= shdr.sh_addr) &&
1180 (addr < (shdr.sh_addr + shdr.sh_size)))
1189 static int dso__swap_init(struct dso *dso, unsigned char eidata)
1191 static unsigned int const endian = 1;
1193 dso->needs_swap = DSO_SWAP__NO;
1197 /* We are big endian, DSO is little endian. */
1198 if (*(unsigned char const *)&endian != 1)
1199 dso->needs_swap = DSO_SWAP__YES;
1203 /* We are little endian, DSO is big endian. */
1204 if (*(unsigned char const *)&endian != 0)
1205 dso->needs_swap = DSO_SWAP__YES;
1209 pr_err("unrecognized DSO data encoding %d\n", eidata);
1216 static int dso__load_sym(struct dso *dso, struct map *map, const char *name,
1217 int fd, symbol_filter_t filter, int kmodule,
1220 struct kmap *kmap = dso->kernel ? map__kmap(map) : NULL;
1221 struct map *curr_map = map;
1222 struct dso *curr_dso = dso;
1223 Elf_Data *symstrs, *secstrs;
1228 GElf_Shdr shdr, opdshdr;
1229 Elf_Data *syms, *opddata = NULL;
1231 Elf_Scn *sec, *sec_strndx, *opdsec;
1236 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1238 pr_debug("%s: cannot read %s ELF file.\n", __func__, name);
1242 if (gelf_getehdr(elf, &ehdr) == NULL) {
1243 pr_debug("%s: cannot get elf header.\n", __func__);
1247 if (dso__swap_init(dso, ehdr.e_ident[EI_DATA]))
1250 /* Always reject images with a mismatched build-id: */
1251 if (dso->has_build_id) {
1252 u8 build_id[BUILD_ID_SIZE];
1254 if (elf_read_build_id(elf, build_id, BUILD_ID_SIZE) < 0)
1257 if (!dso__build_id_equal(dso, build_id))
1261 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
1266 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
1271 opdsec = elf_section_by_name(elf, &ehdr, &opdshdr, ".opd", &opdidx);
1272 if (opdshdr.sh_type != SHT_PROGBITS)
1275 opddata = elf_rawdata(opdsec, NULL);
1277 syms = elf_getdata(sec, NULL);
1281 sec = elf_getscn(elf, shdr.sh_link);
1285 symstrs = elf_getdata(sec, NULL);
1286 if (symstrs == NULL)
1289 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
1290 if (sec_strndx == NULL)
1293 secstrs = elf_getdata(sec_strndx, NULL);
1294 if (secstrs == NULL)
1297 nr_syms = shdr.sh_size / shdr.sh_entsize;
1299 memset(&sym, 0, sizeof(sym));
1300 if (dso->kernel == DSO_TYPE_USER) {
1301 dso->adjust_symbols = (ehdr.e_type == ET_EXEC ||
1302 elf_section_by_name(elf, &ehdr, &shdr,
1303 ".gnu.prelink_undo",
1306 dso->adjust_symbols = 0;
1308 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
1310 const char *elf_name = elf_sym__name(&sym, symstrs);
1311 char *demangled = NULL;
1312 int is_label = elf_sym__is_label(&sym);
1313 const char *section_name;
1315 if (kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name &&
1316 strcmp(elf_name, kmap->ref_reloc_sym->name) == 0)
1317 kmap->ref_reloc_sym->unrelocated_addr = sym.st_value;
1319 if (!is_label && !elf_sym__is_a(&sym, map->type))
1322 /* Reject ARM ELF "mapping symbols": these aren't unique and
1323 * don't identify functions, so will confuse the profile
1325 if (ehdr.e_machine == EM_ARM) {
1326 if (!strcmp(elf_name, "$a") ||
1327 !strcmp(elf_name, "$d") ||
1328 !strcmp(elf_name, "$t"))
1332 if (opdsec && sym.st_shndx == opdidx) {
1333 u32 offset = sym.st_value - opdshdr.sh_addr;
1334 u64 *opd = opddata->d_buf + offset;
1335 sym.st_value = DSO__SWAP(dso, u64, *opd);
1336 sym.st_shndx = elf_addr_to_index(elf, sym.st_value);
1339 sec = elf_getscn(elf, sym.st_shndx);
1343 gelf_getshdr(sec, &shdr);
1345 if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type))
1348 section_name = elf_sec__name(&shdr, secstrs);
1350 /* On ARM, symbols for thumb functions have 1 added to
1351 * the symbol address as a flag - remove it */
1352 if ((ehdr.e_machine == EM_ARM) &&
1353 (map->type == MAP__FUNCTION) &&
1357 if (dso->kernel != DSO_TYPE_USER || kmodule) {
1358 char dso_name[PATH_MAX];
1360 if (strcmp(section_name,
1361 (curr_dso->short_name +
1362 dso->short_name_len)) == 0)
1365 if (strcmp(section_name, ".text") == 0) {
1371 snprintf(dso_name, sizeof(dso_name),
1372 "%s%s", dso->short_name, section_name);
1374 curr_map = map_groups__find_by_name(kmap->kmaps, map->type, dso_name);
1375 if (curr_map == NULL) {
1376 u64 start = sym.st_value;
1379 start += map->start + shdr.sh_offset;
1381 curr_dso = dso__new(dso_name);
1382 if (curr_dso == NULL)
1384 curr_dso->kernel = dso->kernel;
1385 curr_dso->long_name = dso->long_name;
1386 curr_dso->long_name_len = dso->long_name_len;
1387 curr_map = map__new2(start, curr_dso,
1389 if (curr_map == NULL) {
1390 dso__delete(curr_dso);
1393 curr_map->map_ip = identity__map_ip;
1394 curr_map->unmap_ip = identity__map_ip;
1395 curr_dso->symtab_type = dso->symtab_type;
1396 map_groups__insert(kmap->kmaps, curr_map);
1397 dsos__add(&dso->node, curr_dso);
1398 dso__set_loaded(curr_dso, map->type);
1400 curr_dso = curr_map->dso;
1405 if (curr_dso->adjust_symbols) {
1406 pr_debug4("%s: adjusting symbol: st_value: %#" PRIx64 " "
1407 "sh_addr: %#" PRIx64 " sh_offset: %#" PRIx64 "\n", __func__,
1408 (u64)sym.st_value, (u64)shdr.sh_addr,
1409 (u64)shdr.sh_offset);
1410 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
1413 * We need to figure out if the object was created from C++ sources
1414 * DWARF DW_compile_unit has this, but we don't always have access
1417 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
1418 if (demangled != NULL)
1419 elf_name = demangled;
1421 f = symbol__new(sym.st_value, sym.st_size,
1422 GELF_ST_BIND(sym.st_info), elf_name);
1427 if (filter && filter(curr_map, f))
1430 symbols__insert(&curr_dso->symbols[curr_map->type], f);
1436 * For misannotated, zeroed, ASM function sizes.
1439 symbols__fixup_duplicate(&dso->symbols[map->type]);
1440 symbols__fixup_end(&dso->symbols[map->type]);
1443 * We need to fixup this here too because we create new
1444 * maps here, for things like vsyscall sections.
1446 __map_groups__fixup_end(kmap->kmaps, map->type);
1456 static bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
1458 return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
1461 bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
1463 bool have_build_id = false;
1466 list_for_each_entry(pos, head, node) {
1467 if (with_hits && !pos->hit)
1469 if (pos->has_build_id) {
1470 have_build_id = true;
1473 if (filename__read_build_id(pos->long_name, pos->build_id,
1474 sizeof(pos->build_id)) > 0) {
1475 have_build_id = true;
1476 pos->has_build_id = true;
1480 return have_build_id;
1484 * Align offset to 4 bytes as needed for note name and descriptor data.
1486 #define NOTE_ALIGN(n) (((n) + 3) & -4U)
1488 static int elf_read_build_id(Elf *elf, void *bf, size_t size)
1498 if (size < BUILD_ID_SIZE)
1502 if (ek != ELF_K_ELF)
1505 if (gelf_getehdr(elf, &ehdr) == NULL) {
1506 pr_err("%s: cannot get elf header.\n", __func__);
1511 * Check following sections for notes:
1512 * '.note.gnu.build-id'
1514 * '.note' (VDSO specific)
1517 sec = elf_section_by_name(elf, &ehdr, &shdr,
1518 ".note.gnu.build-id", NULL);
1522 sec = elf_section_by_name(elf, &ehdr, &shdr,
1527 sec = elf_section_by_name(elf, &ehdr, &shdr,
1536 data = elf_getdata(sec, NULL);
1541 while (ptr < (data->d_buf + data->d_size)) {
1542 GElf_Nhdr *nhdr = ptr;
1543 size_t namesz = NOTE_ALIGN(nhdr->n_namesz),
1544 descsz = NOTE_ALIGN(nhdr->n_descsz);
1547 ptr += sizeof(*nhdr);
1550 if (nhdr->n_type == NT_GNU_BUILD_ID &&
1551 nhdr->n_namesz == sizeof("GNU")) {
1552 if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
1553 size_t sz = min(size, descsz);
1554 memcpy(bf, ptr, sz);
1555 memset(bf + sz, 0, size - sz);
1567 int filename__read_build_id(const char *filename, void *bf, size_t size)
1572 if (size < BUILD_ID_SIZE)
1575 fd = open(filename, O_RDONLY);
1579 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1581 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
1585 err = elf_read_build_id(elf, bf, size);
1594 int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
1598 if (size < BUILD_ID_SIZE)
1601 fd = open(filename, O_RDONLY);
1608 size_t namesz, descsz;
1610 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
1613 namesz = NOTE_ALIGN(nhdr.n_namesz);
1614 descsz = NOTE_ALIGN(nhdr.n_descsz);
1615 if (nhdr.n_type == NT_GNU_BUILD_ID &&
1616 nhdr.n_namesz == sizeof("GNU")) {
1617 if (read(fd, bf, namesz) != (ssize_t)namesz)
1619 if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
1620 size_t sz = min(descsz, size);
1621 if (read(fd, build_id, sz) == (ssize_t)sz) {
1622 memset(build_id + sz, 0, size - sz);
1626 } else if (read(fd, bf, descsz) != (ssize_t)descsz)
1629 int n = namesz + descsz;
1630 if (read(fd, bf, n) != n)
1639 static int filename__read_debuglink(const char *filename,
1640 char *debuglink, size_t size)
1650 fd = open(filename, O_RDONLY);
1654 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1656 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
1661 if (ek != ELF_K_ELF)
1664 if (gelf_getehdr(elf, &ehdr) == NULL) {
1665 pr_err("%s: cannot get elf header.\n", __func__);
1669 sec = elf_section_by_name(elf, &ehdr, &shdr,
1670 ".gnu_debuglink", NULL);
1674 data = elf_getdata(sec, NULL);
1678 /* the start of this section is a zero-terminated string */
1679 strncpy(debuglink, data->d_buf, size);
1689 char dso__symtab_origin(const struct dso *dso)
1691 static const char origin[] = {
1692 [DSO_BINARY_TYPE__KALLSYMS] = 'k',
1693 [DSO_BINARY_TYPE__JAVA_JIT] = 'j',
1694 [DSO_BINARY_TYPE__DEBUGLINK] = 'l',
1695 [DSO_BINARY_TYPE__BUILD_ID_CACHE] = 'B',
1696 [DSO_BINARY_TYPE__FEDORA_DEBUGINFO] = 'f',
1697 [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO] = 'u',
1698 [DSO_BINARY_TYPE__BUILDID_DEBUGINFO] = 'b',
1699 [DSO_BINARY_TYPE__SYSTEM_PATH_DSO] = 'd',
1700 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE] = 'K',
1701 [DSO_BINARY_TYPE__GUEST_KALLSYMS] = 'g',
1702 [DSO_BINARY_TYPE__GUEST_KMODULE] = 'G',
1705 if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND)
1707 return origin[dso->symtab_type];
1710 int dso__binary_type_file(struct dso *dso, enum dso_binary_type type,
1711 char *root_dir, char *file, size_t size)
1713 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
1717 case DSO_BINARY_TYPE__DEBUGLINK: {
1720 strncpy(file, dso->long_name, size);
1721 debuglink = file + dso->long_name_len;
1722 while (debuglink != file && *debuglink != '/')
1724 if (*debuglink == '/')
1726 filename__read_debuglink(dso->long_name, debuglink,
1727 size - (debuglink - file));
1730 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
1731 /* skip the locally configured cache if a symfs is given */
1732 if (symbol_conf.symfs[0] ||
1733 (dso__build_id_filename(dso, file, size) == NULL))
1737 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
1738 snprintf(file, size, "%s/usr/lib/debug%s.debug",
1739 symbol_conf.symfs, dso->long_name);
1742 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
1743 snprintf(file, size, "%s/usr/lib/debug%s",
1744 symbol_conf.symfs, dso->long_name);
1747 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
1748 if (!dso->has_build_id) {
1753 build_id__sprintf(dso->build_id,
1754 sizeof(dso->build_id),
1756 snprintf(file, size,
1757 "%s/usr/lib/debug/.build-id/%.2s/%s.debug",
1758 symbol_conf.symfs, build_id_hex, build_id_hex + 2);
1761 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
1762 snprintf(file, size, "%s%s",
1763 symbol_conf.symfs, dso->long_name);
1766 case DSO_BINARY_TYPE__GUEST_KMODULE:
1767 snprintf(file, size, "%s%s%s", symbol_conf.symfs,
1768 root_dir, dso->long_name);
1771 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
1772 snprintf(file, size, "%s%s", symbol_conf.symfs,
1777 case DSO_BINARY_TYPE__KALLSYMS:
1778 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
1779 case DSO_BINARY_TYPE__JAVA_JIT:
1780 case DSO_BINARY_TYPE__NOT_FOUND:
1788 int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter)
1794 struct machine *machine;
1795 char *root_dir = (char *) "";
1798 dso__set_loaded(dso, map->type);
1800 if (dso->kernel == DSO_TYPE_KERNEL)
1801 return dso__load_kernel_sym(dso, map, filter);
1802 else if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1803 return dso__load_guest_kernel_sym(dso, map, filter);
1805 if (map->groups && map->groups->machine)
1806 machine = map->groups->machine;
1810 name = malloc(PATH_MAX);
1814 dso->adjust_symbols = 0;
1816 if (strncmp(dso->name, "/tmp/perf-", 10) == 0) {
1819 if (lstat(dso->name, &st) < 0)
1822 if (st.st_uid && (st.st_uid != geteuid())) {
1823 pr_warning("File %s not owned by current user or root, "
1824 "ignoring it.\n", dso->name);
1828 ret = dso__load_perf_map(dso, map, filter);
1829 dso->symtab_type = ret > 0 ? DSO_BINARY_TYPE__JAVA_JIT :
1830 DSO_BINARY_TYPE__NOT_FOUND;
1835 root_dir = machine->root_dir;
1837 /* Iterate over candidate debug images.
1838 * On the first pass, only load images if they have a full symtab.
1839 * Failing that, do a second pass where we accept .dynsym also
1843 for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) {
1845 dso->symtab_type = binary_type_symtab[i];
1847 if (dso__binary_type_file(dso, dso->symtab_type,
1848 root_dir, name, PATH_MAX))
1851 /* Name is now the name of the next image to try */
1852 fd = open(name, O_RDONLY);
1856 ret = dso__load_sym(dso, map, name, fd, filter, 0,
1861 * Some people seem to have debuginfo files _WITHOUT_ debug
1870 nr_plt = dso__synthesize_plt_symbols(dso, name, map, filter);
1878 * If we wanted a full symtab but no image had one,
1879 * relax our requirements and repeat the search.
1881 if (ret <= 0 && want_symtab) {
1887 if (ret < 0 && strstr(dso->name, " (deleted)") != NULL)
1892 struct map *map_groups__find_by_name(struct map_groups *mg,
1893 enum map_type type, const char *name)
1897 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
1898 struct map *map = rb_entry(nd, struct map, rb_node);
1900 if (map->dso && strcmp(map->dso->short_name, name) == 0)
1907 static int dso__kernel_module_get_build_id(struct dso *dso,
1908 const char *root_dir)
1910 char filename[PATH_MAX];
1912 * kernel module short names are of the form "[module]" and
1913 * we need just "module" here.
1915 const char *name = dso->short_name + 1;
1917 snprintf(filename, sizeof(filename),
1918 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1919 root_dir, (int)strlen(name) - 1, name);
1921 if (sysfs__read_build_id(filename, dso->build_id,
1922 sizeof(dso->build_id)) == 0)
1923 dso->has_build_id = true;
1928 static int map_groups__set_modules_path_dir(struct map_groups *mg,
1929 const char *dir_name)
1931 struct dirent *dent;
1932 DIR *dir = opendir(dir_name);
1936 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
1940 while ((dent = readdir(dir)) != NULL) {
1941 char path[PATH_MAX];
1944 /*sshfs might return bad dent->d_type, so we have to stat*/
1945 snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name);
1946 if (stat(path, &st))
1949 if (S_ISDIR(st.st_mode)) {
1950 if (!strcmp(dent->d_name, ".") ||
1951 !strcmp(dent->d_name, ".."))
1954 ret = map_groups__set_modules_path_dir(mg, path);
1958 char *dot = strrchr(dent->d_name, '.'),
1963 if (dot == NULL || strcmp(dot, ".ko"))
1965 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1966 (int)(dot - dent->d_name), dent->d_name);
1968 strxfrchar(dso_name, '-', '_');
1969 map = map_groups__find_by_name(mg, MAP__FUNCTION,
1974 long_name = strdup(path);
1975 if (long_name == NULL) {
1979 dso__set_long_name(map->dso, long_name);
1980 map->dso->lname_alloc = 1;
1981 dso__kernel_module_get_build_id(map->dso, "");
1990 static char *get_kernel_version(const char *root_dir)
1992 char version[PATH_MAX];
1995 const char *prefix = "Linux version ";
1997 sprintf(version, "%s/proc/version", root_dir);
1998 file = fopen(version, "r");
2003 tmp = fgets(version, sizeof(version), file);
2006 name = strstr(version, prefix);
2009 name += strlen(prefix);
2010 tmp = strchr(name, ' ');
2014 return strdup(name);
2017 static int machine__set_modules_path(struct machine *machine)
2020 char modules_path[PATH_MAX];
2022 version = get_kernel_version(machine->root_dir);
2026 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s/kernel",
2027 machine->root_dir, version);
2030 return map_groups__set_modules_path_dir(&machine->kmaps, modules_path);
2034 * Constructor variant for modules (where we know from /proc/modules where
2035 * they are loaded) and for vmlinux, where only after we load all the
2036 * symbols we'll know where it starts and ends.
2038 static struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
2040 struct map *map = calloc(1, (sizeof(*map) +
2041 (dso->kernel ? sizeof(struct kmap) : 0)));
2044 * ->end will be filled after we load all the symbols
2046 map__init(map, type, start, 0, 0, dso);
2052 struct map *machine__new_module(struct machine *machine, u64 start,
2053 const char *filename)
2056 struct dso *dso = __dsos__findnew(&machine->kernel_dsos, filename);
2061 map = map__new2(start, dso, MAP__FUNCTION);
2065 if (machine__is_host(machine))
2066 dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
2068 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
2069 map_groups__insert(&machine->kmaps, map);
2073 static int machine__create_modules(struct machine *machine)
2079 const char *modules;
2080 char path[PATH_MAX];
2082 if (machine__is_default_guest(machine))
2083 modules = symbol_conf.default_guest_modules;
2085 sprintf(path, "%s/proc/modules", machine->root_dir);
2089 if (symbol__restricted_filename(path, "/proc/modules"))
2092 file = fopen(modules, "r");
2096 while (!feof(file)) {
2097 char name[PATH_MAX];
2102 line_len = getline(&line, &n, file);
2109 line[--line_len] = '\0'; /* \n */
2111 sep = strrchr(line, 'x');
2115 hex2u64(sep + 1, &start);
2117 sep = strchr(line, ' ');
2123 snprintf(name, sizeof(name), "[%s]", line);
2124 map = machine__new_module(machine, start, name);
2126 goto out_delete_line;
2127 dso__kernel_module_get_build_id(map->dso, machine->root_dir);
2133 return machine__set_modules_path(machine);
2141 int dso__load_vmlinux(struct dso *dso, struct map *map,
2142 const char *vmlinux, symbol_filter_t filter)
2145 char symfs_vmlinux[PATH_MAX];
2147 snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s%s",
2148 symbol_conf.symfs, vmlinux);
2149 fd = open(symfs_vmlinux, O_RDONLY);
2153 dso__set_long_name(dso, (char *)vmlinux);
2154 dso__set_loaded(dso, map->type);
2155 err = dso__load_sym(dso, map, symfs_vmlinux, fd, filter, 0, 0);
2159 pr_debug("Using %s for symbols\n", symfs_vmlinux);
2164 int dso__load_vmlinux_path(struct dso *dso, struct map *map,
2165 symbol_filter_t filter)
2170 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
2171 vmlinux_path__nr_entries + 1);
2173 filename = dso__build_id_filename(dso, NULL, 0);
2174 if (filename != NULL) {
2175 err = dso__load_vmlinux(dso, map, filename, filter);
2177 dso__set_long_name(dso, filename);
2183 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
2184 err = dso__load_vmlinux(dso, map, vmlinux_path[i], filter);
2186 dso__set_long_name(dso, strdup(vmlinux_path[i]));
2194 static int dso__load_kernel_sym(struct dso *dso, struct map *map,
2195 symbol_filter_t filter)
2198 const char *kallsyms_filename = NULL;
2199 char *kallsyms_allocated_filename = NULL;
2201 * Step 1: if the user specified a kallsyms or vmlinux filename, use
2202 * it and only it, reporting errors to the user if it cannot be used.
2204 * For instance, try to analyse an ARM perf.data file _without_ a
2205 * build-id, or if the user specifies the wrong path to the right
2206 * vmlinux file, obviously we can't fallback to another vmlinux (a
2207 * x86_86 one, on the machine where analysis is being performed, say),
2208 * or worse, /proc/kallsyms.
2210 * If the specified file _has_ a build-id and there is a build-id
2211 * section in the perf.data file, we will still do the expected
2212 * validation in dso__load_vmlinux and will bail out if they don't
2215 if (symbol_conf.kallsyms_name != NULL) {
2216 kallsyms_filename = symbol_conf.kallsyms_name;
2220 if (symbol_conf.vmlinux_name != NULL) {
2221 err = dso__load_vmlinux(dso, map,
2222 symbol_conf.vmlinux_name, filter);
2224 dso__set_long_name(dso,
2225 strdup(symbol_conf.vmlinux_name));
2231 if (vmlinux_path != NULL) {
2232 err = dso__load_vmlinux_path(dso, map, filter);
2237 /* do not try local files if a symfs was given */
2238 if (symbol_conf.symfs[0] != 0)
2242 * Say the kernel DSO was created when processing the build-id header table,
2243 * we have a build-id, so check if it is the same as the running kernel,
2244 * using it if it is.
2246 if (dso->has_build_id) {
2247 u8 kallsyms_build_id[BUILD_ID_SIZE];
2248 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
2250 if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id,
2251 sizeof(kallsyms_build_id)) == 0) {
2252 if (dso__build_id_equal(dso, kallsyms_build_id)) {
2253 kallsyms_filename = "/proc/kallsyms";
2258 * Now look if we have it on the build-id cache in
2259 * $HOME/.debug/[kernel.kallsyms].
2261 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
2264 if (asprintf(&kallsyms_allocated_filename,
2265 "%s/.debug/[kernel.kallsyms]/%s",
2266 getenv("HOME"), sbuild_id) == -1) {
2267 pr_err("Not enough memory for kallsyms file lookup\n");
2271 kallsyms_filename = kallsyms_allocated_filename;
2273 if (access(kallsyms_filename, F_OK)) {
2274 pr_err("No kallsyms or vmlinux with build-id %s "
2275 "was found\n", sbuild_id);
2276 free(kallsyms_allocated_filename);
2281 * Last resort, if we don't have a build-id and couldn't find
2282 * any vmlinux file, try the running kernel kallsyms table.
2284 kallsyms_filename = "/proc/kallsyms";
2288 err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
2290 pr_debug("Using %s for symbols\n", kallsyms_filename);
2291 free(kallsyms_allocated_filename);
2295 if (kallsyms_filename != NULL)
2296 dso__set_long_name(dso, strdup("[kernel.kallsyms]"));
2297 map__fixup_start(map);
2298 map__fixup_end(map);
2304 static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
2305 symbol_filter_t filter)
2308 const char *kallsyms_filename = NULL;
2309 struct machine *machine;
2310 char path[PATH_MAX];
2313 pr_debug("Guest kernel map hasn't the point to groups\n");
2316 machine = map->groups->machine;
2318 if (machine__is_default_guest(machine)) {
2320 * if the user specified a vmlinux filename, use it and only
2321 * it, reporting errors to the user if it cannot be used.
2322 * Or use file guest_kallsyms inputted by user on commandline
2324 if (symbol_conf.default_guest_vmlinux_name != NULL) {
2325 err = dso__load_vmlinux(dso, map,
2326 symbol_conf.default_guest_vmlinux_name, filter);
2330 kallsyms_filename = symbol_conf.default_guest_kallsyms;
2331 if (!kallsyms_filename)
2334 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
2335 kallsyms_filename = path;
2338 err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
2340 pr_debug("Using %s for symbols\n", kallsyms_filename);
2344 if (kallsyms_filename != NULL) {
2345 machine__mmap_name(machine, path, sizeof(path));
2346 dso__set_long_name(dso, strdup(path));
2348 map__fixup_start(map);
2349 map__fixup_end(map);
2355 static void dsos__add(struct list_head *head, struct dso *dso)
2357 list_add_tail(&dso->node, head);
2360 static struct dso *dsos__find(struct list_head *head, const char *name)
2364 list_for_each_entry(pos, head, node)
2365 if (strcmp(pos->long_name, name) == 0)
2370 struct dso *__dsos__findnew(struct list_head *head, const char *name)
2372 struct dso *dso = dsos__find(head, name);
2375 dso = dso__new(name);
2377 dsos__add(head, dso);
2378 dso__set_basename(dso);
2385 size_t __dsos__fprintf(struct list_head *head, FILE *fp)
2390 list_for_each_entry(pos, head, node) {
2392 for (i = 0; i < MAP__NR_TYPES; ++i)
2393 ret += dso__fprintf(pos, i, fp);
2399 size_t machines__fprintf_dsos(struct rb_root *machines, FILE *fp)
2404 for (nd = rb_first(machines); nd; nd = rb_next(nd)) {
2405 struct machine *pos = rb_entry(nd, struct machine, rb_node);
2406 ret += __dsos__fprintf(&pos->kernel_dsos, fp);
2407 ret += __dsos__fprintf(&pos->user_dsos, fp);
2413 static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
2419 list_for_each_entry(pos, head, node) {
2420 if (with_hits && !pos->hit)
2422 ret += dso__fprintf_buildid(pos, fp);
2423 ret += fprintf(fp, " %s\n", pos->long_name);
2428 size_t machine__fprintf_dsos_buildid(struct machine *machine, FILE *fp,
2431 return __dsos__fprintf_buildid(&machine->kernel_dsos, fp, with_hits) +
2432 __dsos__fprintf_buildid(&machine->user_dsos, fp, with_hits);
2435 size_t machines__fprintf_dsos_buildid(struct rb_root *machines,
2436 FILE *fp, bool with_hits)
2441 for (nd = rb_first(machines); nd; nd = rb_next(nd)) {
2442 struct machine *pos = rb_entry(nd, struct machine, rb_node);
2443 ret += machine__fprintf_dsos_buildid(pos, fp, with_hits);
2449 dso__kernel_findnew(struct machine *machine, const char *name,
2450 const char *short_name, int dso_type)
2453 * The kernel dso could be created by build_id processing.
2455 struct dso *dso = __dsos__findnew(&machine->kernel_dsos, name);
2458 * We need to run this in all cases, since during the build_id
2459 * processing we had no idea this was the kernel dso.
2462 dso__set_short_name(dso, short_name);
2463 dso->kernel = dso_type;
2469 void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
2471 char path[PATH_MAX];
2473 if (machine__is_default_guest(machine))
2475 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
2476 if (sysfs__read_build_id(path, dso->build_id,
2477 sizeof(dso->build_id)) == 0)
2478 dso->has_build_id = true;
2481 static struct dso *machine__get_kernel(struct machine *machine)
2483 const char *vmlinux_name = NULL;
2486 if (machine__is_host(machine)) {
2487 vmlinux_name = symbol_conf.vmlinux_name;
2489 vmlinux_name = "[kernel.kallsyms]";
2491 kernel = dso__kernel_findnew(machine, vmlinux_name,
2497 if (machine__is_default_guest(machine))
2498 vmlinux_name = symbol_conf.default_guest_vmlinux_name;
2500 vmlinux_name = machine__mmap_name(machine, bf,
2503 kernel = dso__kernel_findnew(machine, vmlinux_name,
2505 DSO_TYPE_GUEST_KERNEL);
2508 if (kernel != NULL && (!kernel->has_build_id))
2509 dso__read_running_kernel_build_id(kernel, machine);
2514 struct process_args {
2518 static int symbol__in_kernel(void *arg, const char *name,
2519 char type __used, u64 start, u64 end __used)
2521 struct process_args *args = arg;
2523 if (strchr(name, '['))
2526 args->start = start;
2530 /* Figure out the start address of kernel map from /proc/kallsyms */
2531 static u64 machine__get_kernel_start_addr(struct machine *machine)
2533 const char *filename;
2534 char path[PATH_MAX];
2535 struct process_args args;
2537 if (machine__is_host(machine)) {
2538 filename = "/proc/kallsyms";
2540 if (machine__is_default_guest(machine))
2541 filename = (char *)symbol_conf.default_guest_kallsyms;
2543 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
2548 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
2551 if (kallsyms__parse(filename, &args, symbol__in_kernel) <= 0)
2557 int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
2560 u64 start = machine__get_kernel_start_addr(machine);
2562 for (type = 0; type < MAP__NR_TYPES; ++type) {
2565 machine->vmlinux_maps[type] = map__new2(start, kernel, type);
2566 if (machine->vmlinux_maps[type] == NULL)
2569 machine->vmlinux_maps[type]->map_ip =
2570 machine->vmlinux_maps[type]->unmap_ip =
2572 kmap = map__kmap(machine->vmlinux_maps[type]);
2573 kmap->kmaps = &machine->kmaps;
2574 map_groups__insert(&machine->kmaps,
2575 machine->vmlinux_maps[type]);
2581 void machine__destroy_kernel_maps(struct machine *machine)
2585 for (type = 0; type < MAP__NR_TYPES; ++type) {
2588 if (machine->vmlinux_maps[type] == NULL)
2591 kmap = map__kmap(machine->vmlinux_maps[type]);
2592 map_groups__remove(&machine->kmaps,
2593 machine->vmlinux_maps[type]);
2594 if (kmap->ref_reloc_sym) {
2596 * ref_reloc_sym is shared among all maps, so free just
2599 if (type == MAP__FUNCTION) {
2600 free((char *)kmap->ref_reloc_sym->name);
2601 kmap->ref_reloc_sym->name = NULL;
2602 free(kmap->ref_reloc_sym);
2604 kmap->ref_reloc_sym = NULL;
2607 map__delete(machine->vmlinux_maps[type]);
2608 machine->vmlinux_maps[type] = NULL;
2612 int machine__create_kernel_maps(struct machine *machine)
2614 struct dso *kernel = machine__get_kernel(machine);
2616 if (kernel == NULL ||
2617 __machine__create_kernel_maps(machine, kernel) < 0)
2620 if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
2621 if (machine__is_host(machine))
2622 pr_debug("Problems creating module maps, "
2623 "continuing anyway...\n");
2625 pr_debug("Problems creating module maps for guest %d, "
2626 "continuing anyway...\n", machine->pid);
2630 * Now that we have all the maps created, just set the ->end of them:
2632 map_groups__fixup_end(&machine->kmaps);
2636 static void vmlinux_path__exit(void)
2638 while (--vmlinux_path__nr_entries >= 0) {
2639 free(vmlinux_path[vmlinux_path__nr_entries]);
2640 vmlinux_path[vmlinux_path__nr_entries] = NULL;
2644 vmlinux_path = NULL;
2647 static int vmlinux_path__init(void)
2652 vmlinux_path = malloc(sizeof(char *) * 5);
2653 if (vmlinux_path == NULL)
2656 vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
2657 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2659 ++vmlinux_path__nr_entries;
2660 vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
2661 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2663 ++vmlinux_path__nr_entries;
2665 /* only try running kernel version if no symfs was given */
2666 if (symbol_conf.symfs[0] != 0)
2669 if (uname(&uts) < 0)
2672 snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
2673 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
2674 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2676 ++vmlinux_path__nr_entries;
2677 snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
2678 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
2679 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2681 ++vmlinux_path__nr_entries;
2682 snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
2684 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
2685 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2687 ++vmlinux_path__nr_entries;
2692 vmlinux_path__exit();
2696 size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
2700 struct dso *kdso = machine->vmlinux_maps[MAP__FUNCTION]->dso;
2702 if (kdso->has_build_id) {
2703 char filename[PATH_MAX];
2704 if (dso__build_id_filename(kdso, filename, sizeof(filename)))
2705 printed += fprintf(fp, "[0] %s\n", filename);
2708 for (i = 0; i < vmlinux_path__nr_entries; ++i)
2709 printed += fprintf(fp, "[%d] %s\n",
2710 i + kdso->has_build_id, vmlinux_path[i]);
2715 static int setup_list(struct strlist **list, const char *list_str,
2716 const char *list_name)
2718 if (list_str == NULL)
2721 *list = strlist__new(true, list_str);
2723 pr_err("problems parsing %s list\n", list_name);
2729 static bool symbol__read_kptr_restrict(void)
2733 if (geteuid() != 0) {
2734 FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
2738 if (fgets(line, sizeof(line), fp) != NULL)
2739 value = atoi(line) != 0;
2748 int symbol__init(void)
2752 if (symbol_conf.initialized)
2755 symbol_conf.priv_size = ALIGN(symbol_conf.priv_size, sizeof(u64));
2757 elf_version(EV_CURRENT);
2758 if (symbol_conf.sort_by_name)
2759 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
2760 sizeof(struct symbol));
2762 if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
2765 if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
2766 pr_err("'.' is the only non valid --field-separator argument\n");
2770 if (setup_list(&symbol_conf.dso_list,
2771 symbol_conf.dso_list_str, "dso") < 0)
2774 if (setup_list(&symbol_conf.comm_list,
2775 symbol_conf.comm_list_str, "comm") < 0)
2776 goto out_free_dso_list;
2778 if (setup_list(&symbol_conf.sym_list,
2779 symbol_conf.sym_list_str, "symbol") < 0)
2780 goto out_free_comm_list;
2783 * A path to symbols of "/" is identical to ""
2784 * reset here for simplicity.
2786 symfs = realpath(symbol_conf.symfs, NULL);
2788 symfs = symbol_conf.symfs;
2789 if (strcmp(symfs, "/") == 0)
2790 symbol_conf.symfs = "";
2791 if (symfs != symbol_conf.symfs)
2792 free((void *)symfs);
2794 symbol_conf.kptr_restrict = symbol__read_kptr_restrict();
2796 symbol_conf.initialized = true;
2800 strlist__delete(symbol_conf.comm_list);
2802 strlist__delete(symbol_conf.dso_list);
2806 void symbol__exit(void)
2808 if (!symbol_conf.initialized)
2810 strlist__delete(symbol_conf.sym_list);
2811 strlist__delete(symbol_conf.dso_list);
2812 strlist__delete(symbol_conf.comm_list);
2813 vmlinux_path__exit();
2814 symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
2815 symbol_conf.initialized = false;
2818 int machines__create_kernel_maps(struct rb_root *machines, pid_t pid)
2820 struct machine *machine = machines__findnew(machines, pid);
2822 if (machine == NULL)
2825 return machine__create_kernel_maps(machine);
2828 static int hex(char ch)
2830 if ((ch >= '0') && (ch <= '9'))
2832 if ((ch >= 'a') && (ch <= 'f'))
2833 return ch - 'a' + 10;
2834 if ((ch >= 'A') && (ch <= 'F'))
2835 return ch - 'A' + 10;
2840 * While we find nice hex chars, build a long_val.
2841 * Return number of chars processed.
2843 int hex2u64(const char *ptr, u64 *long_val)
2845 const char *p = ptr;
2849 const int hex_val = hex(*p);
2854 *long_val = (*long_val << 4) | hex_val;
2861 char *strxfrchar(char *s, char from, char to)
2865 while ((p = strchr(p, from)) != NULL)
2871 int machines__create_guest_kernel_maps(struct rb_root *machines)
2874 struct dirent **namelist = NULL;
2876 char path[PATH_MAX];
2880 if (symbol_conf.default_guest_vmlinux_name ||
2881 symbol_conf.default_guest_modules ||
2882 symbol_conf.default_guest_kallsyms) {
2883 machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
2886 if (symbol_conf.guestmount) {
2887 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
2890 for (i = 0; i < items; i++) {
2891 if (!isdigit(namelist[i]->d_name[0])) {
2892 /* Filter out . and .. */
2895 pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10);
2896 if ((*endp != '\0') ||
2897 (endp == namelist[i]->d_name) ||
2898 (errno == ERANGE)) {
2899 pr_debug("invalid directory (%s). Skipping.\n",
2900 namelist[i]->d_name);
2903 sprintf(path, "%s/%s/proc/kallsyms",
2904 symbol_conf.guestmount,
2905 namelist[i]->d_name);
2906 ret = access(path, R_OK);
2908 pr_debug("Can't access file %s\n", path);
2911 machines__create_kernel_maps(machines, pid);
2920 void machines__destroy_guest_kernel_maps(struct rb_root *machines)
2922 struct rb_node *next = rb_first(machines);
2925 struct machine *pos = rb_entry(next, struct machine, rb_node);
2927 next = rb_next(&pos->rb_node);
2928 rb_erase(&pos->rb_node, machines);
2929 machine__delete(pos);
2933 int machine__load_kallsyms(struct machine *machine, const char *filename,
2934 enum map_type type, symbol_filter_t filter)
2936 struct map *map = machine->vmlinux_maps[type];
2937 int ret = dso__load_kallsyms(map->dso, filename, map, filter);
2940 dso__set_loaded(map->dso, type);
2942 * Since /proc/kallsyms will have multiple sessions for the
2943 * kernel, with modules between them, fixup the end of all
2946 __map_groups__fixup_end(&machine->kmaps, type);
2952 int machine__load_vmlinux_path(struct machine *machine, enum map_type type,
2953 symbol_filter_t filter)
2955 struct map *map = machine->vmlinux_maps[type];
2956 int ret = dso__load_vmlinux_path(map->dso, map, filter);
2959 dso__set_loaded(map->dso, type);
2960 map__reloc_vmlinux(map);
2966 struct map *dso__new_map(const char *name)
2968 struct map *map = NULL;
2969 struct dso *dso = dso__new(name);
2972 map = map__new2(0, dso, MAP__FUNCTION);
2977 static int open_dso(struct dso *dso, struct machine *machine)
2979 char *root_dir = (char *) "";
2983 name = malloc(PATH_MAX);
2988 root_dir = machine->root_dir;
2990 if (dso__binary_type_file(dso, dso->data_type,
2991 root_dir, name, PATH_MAX)) {
2996 fd = open(name, O_RDONLY);
3001 int dso__data_fd(struct dso *dso, struct machine *machine)
3005 if (dso->data_type != DSO_BINARY_TYPE__NOT_FOUND)
3006 return open_dso(dso, machine);
3011 dso->data_type = binary_type_data[i++];
3013 fd = open_dso(dso, machine);
3017 } while (dso->data_type != DSO_BINARY_TYPE__NOT_FOUND);
3023 dso_cache__free(struct rb_root *root)
3025 struct rb_node *next = rb_first(root);
3028 struct dso_cache *cache;
3030 cache = rb_entry(next, struct dso_cache, rb_node);
3031 next = rb_next(&cache->rb_node);
3032 rb_erase(&cache->rb_node, root);
3037 static struct dso_cache*
3038 dso_cache__find(struct rb_root *root, u64 offset)
3040 struct rb_node **p = &root->rb_node;
3041 struct rb_node *parent = NULL;
3042 struct dso_cache *cache;
3044 while (*p != NULL) {
3048 cache = rb_entry(parent, struct dso_cache, rb_node);
3049 end = cache->offset + DSO__DATA_CACHE_SIZE;
3051 if (offset < cache->offset)
3053 else if (offset >= end)
3054 p = &(*p)->rb_right;
3062 dso_cache__insert(struct rb_root *root, struct dso_cache *new)
3064 struct rb_node **p = &root->rb_node;
3065 struct rb_node *parent = NULL;
3066 struct dso_cache *cache;
3067 u64 offset = new->offset;
3069 while (*p != NULL) {
3073 cache = rb_entry(parent, struct dso_cache, rb_node);
3074 end = cache->offset + DSO__DATA_CACHE_SIZE;
3076 if (offset < cache->offset)
3078 else if (offset >= end)
3079 p = &(*p)->rb_right;
3082 rb_link_node(&new->rb_node, parent, p);
3083 rb_insert_color(&new->rb_node, root);
3087 dso_cache__memcpy(struct dso_cache *cache, u64 offset,
3090 u64 cache_offset = offset - cache->offset;
3091 u64 cache_size = min(cache->size - cache_offset, size);
3093 memcpy(data, cache->data + cache_offset, cache_size);
3098 dso_cache__read(struct dso *dso, struct machine *machine,
3099 u64 offset, u8 *data, ssize_t size)
3101 struct dso_cache *cache;
3105 fd = dso__data_fd(dso, machine);
3114 cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
3118 cache_offset = offset & DSO__DATA_CACHE_MASK;
3121 if (-1 == lseek(fd, cache_offset, SEEK_SET))
3124 ret = read(fd, cache->data, DSO__DATA_CACHE_SIZE);
3128 cache->offset = cache_offset;
3130 dso_cache__insert(&dso->cache, cache);
3132 ret = dso_cache__memcpy(cache, offset, data, size);
3143 static ssize_t dso_cache_read(struct dso *dso, struct machine *machine,
3144 u64 offset, u8 *data, ssize_t size)
3146 struct dso_cache *cache;
3148 cache = dso_cache__find(&dso->cache, offset);
3150 return dso_cache__memcpy(cache, offset, data, size);
3152 return dso_cache__read(dso, machine, offset, data, size);
3155 ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
3156 u64 offset, u8 *data, ssize_t size)
3164 ret = dso_cache_read(dso, machine, offset, p, size);
3168 /* Reached EOF, return what we have. */
3184 ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
3185 struct machine *machine, u64 addr,
3186 u8 *data, ssize_t size)
3188 u64 offset = map->map_ip(map, addr);
3189 return dso__data_read_offset(dso, machine, offset, data, size);