perf top: Properly notify the user that vmlinux is missing
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / tools / perf / util / symbol.c
blob292f941555a8beb1efe34af8f33544c1871a7b4b
1 #include "util.h"
2 #include "../perf.h"
3 #include "sort.h"
4 #include "string.h"
5 #include "symbol.h"
6 #include "thread.h"
8 #include "debug.h"
10 #include <asm/bug.h>
11 #include <libelf.h>
12 #include <gelf.h>
13 #include <elf.h>
14 #include <limits.h>
15 #include <sys/utsname.h>
17 #ifndef NT_GNU_BUILD_ID
18 #define NT_GNU_BUILD_ID 3
19 #endif
21 static void dsos__add(struct list_head *head, struct dso *dso);
22 static struct map *map__new2(u64 start, struct dso *dso, enum map_type type);
23 static int dso__load_kernel_sym(struct dso *self, struct map *map,
24 symbol_filter_t filter);
25 static int vmlinux_path__nr_entries;
26 static char **vmlinux_path;
28 struct symbol_conf symbol_conf = {
29 .exclude_other = true,
30 .use_modules = true,
31 .try_vmlinux_path = true,
34 bool dso__loaded(const struct dso *self, enum map_type type)
36 return self->loaded & (1 << type);
39 bool dso__sorted_by_name(const struct dso *self, enum map_type type)
41 return self->sorted_by_name & (1 << type);
44 static void dso__set_sorted_by_name(struct dso *self, enum map_type type)
46 self->sorted_by_name |= (1 << type);
49 bool symbol_type__is_a(char symbol_type, enum map_type map_type)
51 switch (map_type) {
52 case MAP__FUNCTION:
53 return symbol_type == 'T' || symbol_type == 'W';
54 case MAP__VARIABLE:
55 return symbol_type == 'D' || symbol_type == 'd';
56 default:
57 return false;
61 static void symbols__fixup_end(struct rb_root *self)
63 struct rb_node *nd, *prevnd = rb_first(self);
64 struct symbol *curr, *prev;
66 if (prevnd == NULL)
67 return;
69 curr = rb_entry(prevnd, struct symbol, rb_node);
71 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
72 prev = curr;
73 curr = rb_entry(nd, struct symbol, rb_node);
75 if (prev->end == prev->start)
76 prev->end = curr->start - 1;
79 /* Last entry */
80 if (curr->end == curr->start)
81 curr->end = roundup(curr->start, 4096);
84 static void __map_groups__fixup_end(struct map_groups *self, enum map_type type)
86 struct map *prev, *curr;
87 struct rb_node *nd, *prevnd = rb_first(&self->maps[type]);
89 if (prevnd == NULL)
90 return;
92 curr = rb_entry(prevnd, struct map, rb_node);
94 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
95 prev = curr;
96 curr = rb_entry(nd, struct map, rb_node);
97 prev->end = curr->start - 1;
101 * We still haven't the actual symbols, so guess the
102 * last map final address.
104 curr->end = ~0UL;
107 static void map_groups__fixup_end(struct map_groups *self)
109 int i;
110 for (i = 0; i < MAP__NR_TYPES; ++i)
111 __map_groups__fixup_end(self, i);
114 static struct symbol *symbol__new(u64 start, u64 len, const char *name)
116 size_t namelen = strlen(name) + 1;
117 struct symbol *self = zalloc(symbol_conf.priv_size +
118 sizeof(*self) + namelen);
119 if (self == NULL)
120 return NULL;
122 if (symbol_conf.priv_size)
123 self = ((void *)self) + symbol_conf.priv_size;
125 self->start = start;
126 self->end = len ? start + len - 1 : start;
128 pr_debug4("%s: %s %#Lx-%#Lx\n", __func__, name, start, self->end);
130 memcpy(self->name, name, namelen);
132 return self;
135 void symbol__delete(struct symbol *self)
137 free(((void *)self) - symbol_conf.priv_size);
140 static size_t symbol__fprintf(struct symbol *self, FILE *fp)
142 return fprintf(fp, " %llx-%llx %s\n",
143 self->start, self->end, self->name);
146 void dso__set_long_name(struct dso *self, char *name)
148 if (name == NULL)
149 return;
150 self->long_name = name;
151 self->long_name_len = strlen(name);
154 static void dso__set_basename(struct dso *self)
156 self->short_name = basename(self->long_name);
159 struct dso *dso__new(const char *name)
161 struct dso *self = zalloc(sizeof(*self) + strlen(name) + 1);
163 if (self != NULL) {
164 int i;
165 strcpy(self->name, name);
166 dso__set_long_name(self, self->name);
167 self->short_name = self->name;
168 for (i = 0; i < MAP__NR_TYPES; ++i)
169 self->symbols[i] = self->symbol_names[i] = RB_ROOT;
170 self->slen_calculated = 0;
171 self->origin = DSO__ORIG_NOT_FOUND;
172 self->loaded = 0;
173 self->sorted_by_name = 0;
174 self->has_build_id = 0;
177 return self;
180 static void symbols__delete(struct rb_root *self)
182 struct symbol *pos;
183 struct rb_node *next = rb_first(self);
185 while (next) {
186 pos = rb_entry(next, struct symbol, rb_node);
187 next = rb_next(&pos->rb_node);
188 rb_erase(&pos->rb_node, self);
189 symbol__delete(pos);
193 void dso__delete(struct dso *self)
195 int i;
196 for (i = 0; i < MAP__NR_TYPES; ++i)
197 symbols__delete(&self->symbols[i]);
198 if (self->long_name != self->name)
199 free(self->long_name);
200 free(self);
203 void dso__set_build_id(struct dso *self, void *build_id)
205 memcpy(self->build_id, build_id, sizeof(self->build_id));
206 self->has_build_id = 1;
209 static void symbols__insert(struct rb_root *self, struct symbol *sym)
211 struct rb_node **p = &self->rb_node;
212 struct rb_node *parent = NULL;
213 const u64 ip = sym->start;
214 struct symbol *s;
216 while (*p != NULL) {
217 parent = *p;
218 s = rb_entry(parent, struct symbol, rb_node);
219 if (ip < s->start)
220 p = &(*p)->rb_left;
221 else
222 p = &(*p)->rb_right;
224 rb_link_node(&sym->rb_node, parent, p);
225 rb_insert_color(&sym->rb_node, self);
228 static struct symbol *symbols__find(struct rb_root *self, u64 ip)
230 struct rb_node *n;
232 if (self == NULL)
233 return NULL;
235 n = self->rb_node;
237 while (n) {
238 struct symbol *s = rb_entry(n, struct symbol, rb_node);
240 if (ip < s->start)
241 n = n->rb_left;
242 else if (ip > s->end)
243 n = n->rb_right;
244 else
245 return s;
248 return NULL;
251 struct symbol_name_rb_node {
252 struct rb_node rb_node;
253 struct symbol sym;
256 static void symbols__insert_by_name(struct rb_root *self, struct symbol *sym)
258 struct rb_node **p = &self->rb_node;
259 struct rb_node *parent = NULL;
260 struct symbol_name_rb_node *symn = ((void *)sym) - sizeof(*parent), *s;
262 while (*p != NULL) {
263 parent = *p;
264 s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
265 if (strcmp(sym->name, s->sym.name) < 0)
266 p = &(*p)->rb_left;
267 else
268 p = &(*p)->rb_right;
270 rb_link_node(&symn->rb_node, parent, p);
271 rb_insert_color(&symn->rb_node, self);
274 static void symbols__sort_by_name(struct rb_root *self, struct rb_root *source)
276 struct rb_node *nd;
278 for (nd = rb_first(source); nd; nd = rb_next(nd)) {
279 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
280 symbols__insert_by_name(self, pos);
284 static struct symbol *symbols__find_by_name(struct rb_root *self, const char *name)
286 struct rb_node *n;
288 if (self == NULL)
289 return NULL;
291 n = self->rb_node;
293 while (n) {
294 struct symbol_name_rb_node *s;
295 int cmp;
297 s = rb_entry(n, struct symbol_name_rb_node, rb_node);
298 cmp = strcmp(name, s->sym.name);
300 if (cmp < 0)
301 n = n->rb_left;
302 else if (cmp > 0)
303 n = n->rb_right;
304 else
305 return &s->sym;
308 return NULL;
311 struct symbol *dso__find_symbol(struct dso *self,
312 enum map_type type, u64 addr)
314 return symbols__find(&self->symbols[type], addr);
317 struct symbol *dso__find_symbol_by_name(struct dso *self, enum map_type type,
318 const char *name)
320 return symbols__find_by_name(&self->symbol_names[type], name);
323 void dso__sort_by_name(struct dso *self, enum map_type type)
325 dso__set_sorted_by_name(self, type);
326 return symbols__sort_by_name(&self->symbol_names[type],
327 &self->symbols[type]);
330 int build_id__sprintf(const u8 *self, int len, char *bf)
332 char *bid = bf;
333 const u8 *raw = self;
334 int i;
336 for (i = 0; i < len; ++i) {
337 sprintf(bid, "%02x", *raw);
338 ++raw;
339 bid += 2;
342 return raw - self;
345 size_t dso__fprintf_buildid(struct dso *self, FILE *fp)
347 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
349 build_id__sprintf(self->build_id, sizeof(self->build_id), sbuild_id);
350 return fprintf(fp, "%s", sbuild_id);
353 size_t dso__fprintf(struct dso *self, enum map_type type, FILE *fp)
355 struct rb_node *nd;
356 size_t ret = fprintf(fp, "dso: %s (", self->short_name);
358 if (self->short_name != self->long_name)
359 ret += fprintf(fp, "%s, ", self->long_name);
360 ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
361 self->loaded ? "" : "NOT ");
362 ret += dso__fprintf_buildid(self, fp);
363 ret += fprintf(fp, ")\n");
364 for (nd = rb_first(&self->symbols[type]); nd; nd = rb_next(nd)) {
365 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
366 ret += symbol__fprintf(pos, fp);
369 return ret;
372 int kallsyms__parse(const char *filename, void *arg,
373 int (*process_symbol)(void *arg, const char *name,
374 char type, u64 start))
376 char *line = NULL;
377 size_t n;
378 int err = 0;
379 FILE *file = fopen(filename, "r");
381 if (file == NULL)
382 goto out_failure;
384 while (!feof(file)) {
385 u64 start;
386 int line_len, len;
387 char symbol_type;
388 char *symbol_name;
390 line_len = getline(&line, &n, file);
391 if (line_len < 0)
392 break;
394 if (!line)
395 goto out_failure;
397 line[--line_len] = '\0'; /* \n */
399 len = hex2u64(line, &start);
401 len++;
402 if (len + 2 >= line_len)
403 continue;
405 symbol_type = toupper(line[len]);
406 symbol_name = line + len + 2;
408 err = process_symbol(arg, symbol_name, symbol_type, start);
409 if (err)
410 break;
413 free(line);
414 fclose(file);
415 return err;
417 out_failure:
418 return -1;
421 struct process_kallsyms_args {
422 struct map *map;
423 struct dso *dso;
426 static int map__process_kallsym_symbol(void *arg, const char *name,
427 char type, u64 start)
429 struct symbol *sym;
430 struct process_kallsyms_args *a = arg;
431 struct rb_root *root = &a->dso->symbols[a->map->type];
433 if (!symbol_type__is_a(type, a->map->type))
434 return 0;
437 * Will fix up the end later, when we have all symbols sorted.
439 sym = symbol__new(start, 0, name);
441 if (sym == NULL)
442 return -ENOMEM;
444 * We will pass the symbols to the filter later, in
445 * map__split_kallsyms, when we have split the maps per module
447 symbols__insert(root, sym);
448 return 0;
452 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
453 * so that we can in the next step set the symbol ->end address and then
454 * call kernel_maps__split_kallsyms.
456 static int dso__load_all_kallsyms(struct dso *self, const char *filename,
457 struct map *map)
459 struct process_kallsyms_args args = { .map = map, .dso = self, };
460 return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
464 * Split the symbols into maps, making sure there are no overlaps, i.e. the
465 * kernel range is broken in several maps, named [kernel].N, as we don't have
466 * the original ELF section names vmlinux have.
468 static int dso__split_kallsyms(struct dso *self, struct map *map,
469 symbol_filter_t filter)
471 struct map_groups *kmaps = map__kmap(map)->kmaps;
472 struct map *curr_map = map;
473 struct symbol *pos;
474 int count = 0;
475 struct rb_root *root = &self->symbols[map->type];
476 struct rb_node *next = rb_first(root);
477 int kernel_range = 0;
479 while (next) {
480 char *module;
482 pos = rb_entry(next, struct symbol, rb_node);
483 next = rb_next(&pos->rb_node);
485 module = strchr(pos->name, '\t');
486 if (module) {
487 if (!symbol_conf.use_modules)
488 goto discard_symbol;
490 *module++ = '\0';
492 if (strcmp(curr_map->dso->short_name, module)) {
493 curr_map = map_groups__find_by_name(kmaps, map->type, module);
494 if (curr_map == NULL) {
495 pr_debug("/proc/{kallsyms,modules} "
496 "inconsistency while looking "
497 "for \"%s\" module!\n", module);
498 return -1;
501 if (curr_map->dso->loaded)
502 goto discard_symbol;
505 * So that we look just like we get from .ko files,
506 * i.e. not prelinked, relative to map->start.
508 pos->start = curr_map->map_ip(curr_map, pos->start);
509 pos->end = curr_map->map_ip(curr_map, pos->end);
510 } else if (curr_map != map) {
511 char dso_name[PATH_MAX];
512 struct dso *dso;
514 snprintf(dso_name, sizeof(dso_name), "[kernel].%d",
515 kernel_range++);
517 dso = dso__new(dso_name);
518 if (dso == NULL)
519 return -1;
521 curr_map = map__new2(pos->start, dso, map->type);
522 if (curr_map == NULL) {
523 dso__delete(dso);
524 return -1;
527 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
528 map_groups__insert(kmaps, curr_map);
529 ++kernel_range;
532 if (filter && filter(curr_map, pos)) {
533 discard_symbol: rb_erase(&pos->rb_node, root);
534 symbol__delete(pos);
535 } else {
536 if (curr_map != map) {
537 rb_erase(&pos->rb_node, root);
538 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
540 count++;
544 return count;
547 int dso__load_kallsyms(struct dso *self, const char *filename,
548 struct map *map, symbol_filter_t filter)
550 if (dso__load_all_kallsyms(self, filename, map) < 0)
551 return -1;
553 symbols__fixup_end(&self->symbols[map->type]);
554 self->origin = DSO__ORIG_KERNEL;
556 return dso__split_kallsyms(self, map, filter);
559 static int dso__load_perf_map(struct dso *self, struct map *map,
560 symbol_filter_t filter)
562 char *line = NULL;
563 size_t n;
564 FILE *file;
565 int nr_syms = 0;
567 file = fopen(self->long_name, "r");
568 if (file == NULL)
569 goto out_failure;
571 while (!feof(file)) {
572 u64 start, size;
573 struct symbol *sym;
574 int line_len, len;
576 line_len = getline(&line, &n, file);
577 if (line_len < 0)
578 break;
580 if (!line)
581 goto out_failure;
583 line[--line_len] = '\0'; /* \n */
585 len = hex2u64(line, &start);
587 len++;
588 if (len + 2 >= line_len)
589 continue;
591 len += hex2u64(line + len, &size);
593 len++;
594 if (len + 2 >= line_len)
595 continue;
597 sym = symbol__new(start, size, line + len);
599 if (sym == NULL)
600 goto out_delete_line;
602 if (filter && filter(map, sym))
603 symbol__delete(sym);
604 else {
605 symbols__insert(&self->symbols[map->type], sym);
606 nr_syms++;
610 free(line);
611 fclose(file);
613 return nr_syms;
615 out_delete_line:
616 free(line);
617 out_failure:
618 return -1;
622 * elf_symtab__for_each_symbol - iterate thru all the symbols
624 * @self: struct elf_symtab instance to iterate
625 * @idx: uint32_t idx
626 * @sym: GElf_Sym iterator
628 #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
629 for (idx = 0, gelf_getsym(syms, idx, &sym);\
630 idx < nr_syms; \
631 idx++, gelf_getsym(syms, idx, &sym))
633 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
635 return GELF_ST_TYPE(sym->st_info);
638 static inline int elf_sym__is_function(const GElf_Sym *sym)
640 return elf_sym__type(sym) == STT_FUNC &&
641 sym->st_name != 0 &&
642 sym->st_shndx != SHN_UNDEF;
645 static inline bool elf_sym__is_object(const GElf_Sym *sym)
647 return elf_sym__type(sym) == STT_OBJECT &&
648 sym->st_name != 0 &&
649 sym->st_shndx != SHN_UNDEF;
652 static inline int elf_sym__is_label(const GElf_Sym *sym)
654 return elf_sym__type(sym) == STT_NOTYPE &&
655 sym->st_name != 0 &&
656 sym->st_shndx != SHN_UNDEF &&
657 sym->st_shndx != SHN_ABS;
660 static inline const char *elf_sec__name(const GElf_Shdr *shdr,
661 const Elf_Data *secstrs)
663 return secstrs->d_buf + shdr->sh_name;
666 static inline int elf_sec__is_text(const GElf_Shdr *shdr,
667 const Elf_Data *secstrs)
669 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
672 static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
673 const Elf_Data *secstrs)
675 return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
678 static inline const char *elf_sym__name(const GElf_Sym *sym,
679 const Elf_Data *symstrs)
681 return symstrs->d_buf + sym->st_name;
684 static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
685 GElf_Shdr *shp, const char *name,
686 size_t *idx)
688 Elf_Scn *sec = NULL;
689 size_t cnt = 1;
691 while ((sec = elf_nextscn(elf, sec)) != NULL) {
692 char *str;
694 gelf_getshdr(sec, shp);
695 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
696 if (!strcmp(name, str)) {
697 if (idx)
698 *idx = cnt;
699 break;
701 ++cnt;
704 return sec;
707 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
708 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
709 idx < nr_entries; \
710 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
712 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
713 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
714 idx < nr_entries; \
715 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
718 * We need to check if we have a .dynsym, so that we can handle the
719 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
720 * .dynsym or .symtab).
721 * And always look at the original dso, not at debuginfo packages, that
722 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
724 static int dso__synthesize_plt_symbols(struct dso *self, struct map *map,
725 symbol_filter_t filter)
727 uint32_t nr_rel_entries, idx;
728 GElf_Sym sym;
729 u64 plt_offset;
730 GElf_Shdr shdr_plt;
731 struct symbol *f;
732 GElf_Shdr shdr_rel_plt, shdr_dynsym;
733 Elf_Data *reldata, *syms, *symstrs;
734 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
735 size_t dynsym_idx;
736 GElf_Ehdr ehdr;
737 char sympltname[1024];
738 Elf *elf;
739 int nr = 0, symidx, fd, err = 0;
741 fd = open(self->long_name, O_RDONLY);
742 if (fd < 0)
743 goto out;
745 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
746 if (elf == NULL)
747 goto out_close;
749 if (gelf_getehdr(elf, &ehdr) == NULL)
750 goto out_elf_end;
752 scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
753 ".dynsym", &dynsym_idx);
754 if (scn_dynsym == NULL)
755 goto out_elf_end;
757 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
758 ".rela.plt", NULL);
759 if (scn_plt_rel == NULL) {
760 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
761 ".rel.plt", NULL);
762 if (scn_plt_rel == NULL)
763 goto out_elf_end;
766 err = -1;
768 if (shdr_rel_plt.sh_link != dynsym_idx)
769 goto out_elf_end;
771 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
772 goto out_elf_end;
775 * Fetch the relocation section to find the idxes to the GOT
776 * and the symbols in the .dynsym they refer to.
778 reldata = elf_getdata(scn_plt_rel, NULL);
779 if (reldata == NULL)
780 goto out_elf_end;
782 syms = elf_getdata(scn_dynsym, NULL);
783 if (syms == NULL)
784 goto out_elf_end;
786 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
787 if (scn_symstrs == NULL)
788 goto out_elf_end;
790 symstrs = elf_getdata(scn_symstrs, NULL);
791 if (symstrs == NULL)
792 goto out_elf_end;
794 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
795 plt_offset = shdr_plt.sh_offset;
797 if (shdr_rel_plt.sh_type == SHT_RELA) {
798 GElf_Rela pos_mem, *pos;
800 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
801 nr_rel_entries) {
802 symidx = GELF_R_SYM(pos->r_info);
803 plt_offset += shdr_plt.sh_entsize;
804 gelf_getsym(syms, symidx, &sym);
805 snprintf(sympltname, sizeof(sympltname),
806 "%s@plt", elf_sym__name(&sym, symstrs));
808 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
809 sympltname);
810 if (!f)
811 goto out_elf_end;
813 if (filter && filter(map, f))
814 symbol__delete(f);
815 else {
816 symbols__insert(&self->symbols[map->type], f);
817 ++nr;
820 } else if (shdr_rel_plt.sh_type == SHT_REL) {
821 GElf_Rel pos_mem, *pos;
822 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
823 nr_rel_entries) {
824 symidx = GELF_R_SYM(pos->r_info);
825 plt_offset += shdr_plt.sh_entsize;
826 gelf_getsym(syms, symidx, &sym);
827 snprintf(sympltname, sizeof(sympltname),
828 "%s@plt", elf_sym__name(&sym, symstrs));
830 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
831 sympltname);
832 if (!f)
833 goto out_elf_end;
835 if (filter && filter(map, f))
836 symbol__delete(f);
837 else {
838 symbols__insert(&self->symbols[map->type], f);
839 ++nr;
844 err = 0;
845 out_elf_end:
846 elf_end(elf);
847 out_close:
848 close(fd);
850 if (err == 0)
851 return nr;
852 out:
853 pr_debug("%s: problems reading %s PLT info.\n",
854 __func__, self->long_name);
855 return 0;
858 static bool elf_sym__is_a(GElf_Sym *self, enum map_type type)
860 switch (type) {
861 case MAP__FUNCTION:
862 return elf_sym__is_function(self);
863 case MAP__VARIABLE:
864 return elf_sym__is_object(self);
865 default:
866 return false;
870 static bool elf_sec__is_a(GElf_Shdr *self, Elf_Data *secstrs, enum map_type type)
872 switch (type) {
873 case MAP__FUNCTION:
874 return elf_sec__is_text(self, secstrs);
875 case MAP__VARIABLE:
876 return elf_sec__is_data(self, secstrs);
877 default:
878 return false;
882 static int dso__load_sym(struct dso *self, struct map *map, const char *name,
883 int fd, symbol_filter_t filter, int kmodule)
885 struct kmap *kmap = self->kernel ? map__kmap(map) : NULL;
886 struct map *curr_map = map;
887 struct dso *curr_dso = self;
888 size_t dso_name_len = strlen(self->short_name);
889 Elf_Data *symstrs, *secstrs;
890 uint32_t nr_syms;
891 int err = -1;
892 uint32_t idx;
893 GElf_Ehdr ehdr;
894 GElf_Shdr shdr;
895 Elf_Data *syms;
896 GElf_Sym sym;
897 Elf_Scn *sec, *sec_strndx;
898 Elf *elf;
899 int nr = 0;
901 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
902 if (elf == NULL) {
903 pr_err("%s: cannot read %s ELF file.\n", __func__, name);
904 goto out_close;
907 if (gelf_getehdr(elf, &ehdr) == NULL) {
908 pr_err("%s: cannot get elf header.\n", __func__);
909 goto out_elf_end;
912 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
913 if (sec == NULL) {
914 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
915 if (sec == NULL)
916 goto out_elf_end;
919 syms = elf_getdata(sec, NULL);
920 if (syms == NULL)
921 goto out_elf_end;
923 sec = elf_getscn(elf, shdr.sh_link);
924 if (sec == NULL)
925 goto out_elf_end;
927 symstrs = elf_getdata(sec, NULL);
928 if (symstrs == NULL)
929 goto out_elf_end;
931 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
932 if (sec_strndx == NULL)
933 goto out_elf_end;
935 secstrs = elf_getdata(sec_strndx, NULL);
936 if (secstrs == NULL)
937 goto out_elf_end;
939 nr_syms = shdr.sh_size / shdr.sh_entsize;
941 memset(&sym, 0, sizeof(sym));
942 if (!self->kernel) {
943 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
944 elf_section_by_name(elf, &ehdr, &shdr,
945 ".gnu.prelink_undo",
946 NULL) != NULL);
947 } else self->adjust_symbols = 0;
949 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
950 struct symbol *f;
951 const char *elf_name = elf_sym__name(&sym, symstrs);
952 char *demangled = NULL;
953 int is_label = elf_sym__is_label(&sym);
954 const char *section_name;
956 if (kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name &&
957 strcmp(elf_name, kmap->ref_reloc_sym->name) == 0)
958 kmap->ref_reloc_sym->unrelocated_addr = sym.st_value;
960 if (!is_label && !elf_sym__is_a(&sym, map->type))
961 continue;
963 sec = elf_getscn(elf, sym.st_shndx);
964 if (!sec)
965 goto out_elf_end;
967 gelf_getshdr(sec, &shdr);
969 if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type))
970 continue;
972 section_name = elf_sec__name(&shdr, secstrs);
974 if (self->kernel || kmodule) {
975 char dso_name[PATH_MAX];
977 if (strcmp(section_name,
978 curr_dso->short_name + dso_name_len) == 0)
979 goto new_symbol;
981 if (strcmp(section_name, ".text") == 0) {
982 curr_map = map;
983 curr_dso = self;
984 goto new_symbol;
987 snprintf(dso_name, sizeof(dso_name),
988 "%s%s", self->short_name, section_name);
990 curr_map = map_groups__find_by_name(kmap->kmaps, map->type, dso_name);
991 if (curr_map == NULL) {
992 u64 start = sym.st_value;
994 if (kmodule)
995 start += map->start + shdr.sh_offset;
997 curr_dso = dso__new(dso_name);
998 if (curr_dso == NULL)
999 goto out_elf_end;
1000 curr_map = map__new2(start, curr_dso,
1001 map->type);
1002 if (curr_map == NULL) {
1003 dso__delete(curr_dso);
1004 goto out_elf_end;
1006 curr_map->map_ip = identity__map_ip;
1007 curr_map->unmap_ip = identity__map_ip;
1008 curr_dso->origin = self->origin;
1009 map_groups__insert(kmap->kmaps, curr_map);
1010 dsos__add(&dsos__kernel, curr_dso);
1011 dso__set_loaded(curr_dso, map->type);
1012 } else
1013 curr_dso = curr_map->dso;
1015 goto new_symbol;
1018 if (curr_dso->adjust_symbols) {
1019 pr_debug4("%s: adjusting symbol: st_value: %#Lx "
1020 "sh_addr: %#Lx sh_offset: %#Lx\n", __func__,
1021 (u64)sym.st_value, (u64)shdr.sh_addr,
1022 (u64)shdr.sh_offset);
1023 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
1026 * We need to figure out if the object was created from C++ sources
1027 * DWARF DW_compile_unit has this, but we don't always have access
1028 * to it...
1030 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
1031 if (demangled != NULL)
1032 elf_name = demangled;
1033 new_symbol:
1034 f = symbol__new(sym.st_value, sym.st_size, elf_name);
1035 free(demangled);
1036 if (!f)
1037 goto out_elf_end;
1039 if (filter && filter(curr_map, f))
1040 symbol__delete(f);
1041 else {
1042 symbols__insert(&curr_dso->symbols[curr_map->type], f);
1043 nr++;
1048 * For misannotated, zeroed, ASM function sizes.
1050 if (nr > 0) {
1051 symbols__fixup_end(&self->symbols[map->type]);
1052 if (kmap) {
1054 * We need to fixup this here too because we create new
1055 * maps here, for things like vsyscall sections.
1057 __map_groups__fixup_end(kmap->kmaps, map->type);
1060 err = nr;
1061 out_elf_end:
1062 elf_end(elf);
1063 out_close:
1064 return err;
1067 static bool dso__build_id_equal(const struct dso *self, u8 *build_id)
1069 return memcmp(self->build_id, build_id, sizeof(self->build_id)) == 0;
1072 static bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
1074 bool have_build_id = false;
1075 struct dso *pos;
1077 list_for_each_entry(pos, head, node) {
1078 if (with_hits && !pos->hit)
1079 continue;
1080 if (filename__read_build_id(pos->long_name, pos->build_id,
1081 sizeof(pos->build_id)) > 0) {
1082 have_build_id = true;
1083 pos->has_build_id = true;
1087 return have_build_id;
1090 bool dsos__read_build_ids(bool with_hits)
1092 bool kbuildids = __dsos__read_build_ids(&dsos__kernel, with_hits),
1093 ubuildids = __dsos__read_build_ids(&dsos__user, with_hits);
1094 return kbuildids || ubuildids;
1098 * Align offset to 4 bytes as needed for note name and descriptor data.
1100 #define NOTE_ALIGN(n) (((n) + 3) & -4U)
1102 int filename__read_build_id(const char *filename, void *bf, size_t size)
1104 int fd, err = -1;
1105 GElf_Ehdr ehdr;
1106 GElf_Shdr shdr;
1107 Elf_Data *data;
1108 Elf_Scn *sec;
1109 Elf_Kind ek;
1110 void *ptr;
1111 Elf *elf;
1113 if (size < BUILD_ID_SIZE)
1114 goto out;
1116 fd = open(filename, O_RDONLY);
1117 if (fd < 0)
1118 goto out;
1120 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1121 if (elf == NULL) {
1122 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
1123 goto out_close;
1126 ek = elf_kind(elf);
1127 if (ek != ELF_K_ELF)
1128 goto out_elf_end;
1130 if (gelf_getehdr(elf, &ehdr) == NULL) {
1131 pr_err("%s: cannot get elf header.\n", __func__);
1132 goto out_elf_end;
1135 sec = elf_section_by_name(elf, &ehdr, &shdr,
1136 ".note.gnu.build-id", NULL);
1137 if (sec == NULL) {
1138 sec = elf_section_by_name(elf, &ehdr, &shdr,
1139 ".notes", NULL);
1140 if (sec == NULL)
1141 goto out_elf_end;
1144 data = elf_getdata(sec, NULL);
1145 if (data == NULL)
1146 goto out_elf_end;
1148 ptr = data->d_buf;
1149 while (ptr < (data->d_buf + data->d_size)) {
1150 GElf_Nhdr *nhdr = ptr;
1151 int namesz = NOTE_ALIGN(nhdr->n_namesz),
1152 descsz = NOTE_ALIGN(nhdr->n_descsz);
1153 const char *name;
1155 ptr += sizeof(*nhdr);
1156 name = ptr;
1157 ptr += namesz;
1158 if (nhdr->n_type == NT_GNU_BUILD_ID &&
1159 nhdr->n_namesz == sizeof("GNU")) {
1160 if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
1161 memcpy(bf, ptr, BUILD_ID_SIZE);
1162 err = BUILD_ID_SIZE;
1163 break;
1166 ptr += descsz;
1168 out_elf_end:
1169 elf_end(elf);
1170 out_close:
1171 close(fd);
1172 out:
1173 return err;
1176 int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
1178 int fd, err = -1;
1180 if (size < BUILD_ID_SIZE)
1181 goto out;
1183 fd = open(filename, O_RDONLY);
1184 if (fd < 0)
1185 goto out;
1187 while (1) {
1188 char bf[BUFSIZ];
1189 GElf_Nhdr nhdr;
1190 int namesz, descsz;
1192 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
1193 break;
1195 namesz = NOTE_ALIGN(nhdr.n_namesz);
1196 descsz = NOTE_ALIGN(nhdr.n_descsz);
1197 if (nhdr.n_type == NT_GNU_BUILD_ID &&
1198 nhdr.n_namesz == sizeof("GNU")) {
1199 if (read(fd, bf, namesz) != namesz)
1200 break;
1201 if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
1202 if (read(fd, build_id,
1203 BUILD_ID_SIZE) == BUILD_ID_SIZE) {
1204 err = 0;
1205 break;
1207 } else if (read(fd, bf, descsz) != descsz)
1208 break;
1209 } else {
1210 int n = namesz + descsz;
1211 if (read(fd, bf, n) != n)
1212 break;
1215 close(fd);
1216 out:
1217 return err;
1220 char dso__symtab_origin(const struct dso *self)
1222 static const char origin[] = {
1223 [DSO__ORIG_KERNEL] = 'k',
1224 [DSO__ORIG_JAVA_JIT] = 'j',
1225 [DSO__ORIG_BUILD_ID_CACHE] = 'B',
1226 [DSO__ORIG_FEDORA] = 'f',
1227 [DSO__ORIG_UBUNTU] = 'u',
1228 [DSO__ORIG_BUILDID] = 'b',
1229 [DSO__ORIG_DSO] = 'd',
1230 [DSO__ORIG_KMODULE] = 'K',
1233 if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
1234 return '!';
1235 return origin[self->origin];
1238 int dso__load(struct dso *self, struct map *map, symbol_filter_t filter)
1240 int size = PATH_MAX;
1241 char *name;
1242 u8 build_id[BUILD_ID_SIZE];
1243 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
1244 int ret = -1;
1245 int fd;
1247 dso__set_loaded(self, map->type);
1249 if (self->kernel)
1250 return dso__load_kernel_sym(self, map, filter);
1252 name = malloc(size);
1253 if (!name)
1254 return -1;
1256 self->adjust_symbols = 0;
1258 if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
1259 ret = dso__load_perf_map(self, map, filter);
1260 self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
1261 DSO__ORIG_NOT_FOUND;
1262 return ret;
1265 self->origin = DSO__ORIG_BUILD_ID_CACHE;
1267 if (self->has_build_id) {
1268 build_id__sprintf(self->build_id, sizeof(self->build_id),
1269 build_id_hex);
1270 snprintf(name, size, "%s/%s/.build-id/%.2s/%s",
1271 getenv("HOME"), DEBUG_CACHE_DIR,
1272 build_id_hex, build_id_hex + 2);
1273 goto open_file;
1275 more:
1276 do {
1277 self->origin++;
1278 switch (self->origin) {
1279 case DSO__ORIG_FEDORA:
1280 snprintf(name, size, "/usr/lib/debug%s.debug",
1281 self->long_name);
1282 break;
1283 case DSO__ORIG_UBUNTU:
1284 snprintf(name, size, "/usr/lib/debug%s",
1285 self->long_name);
1286 break;
1287 case DSO__ORIG_BUILDID:
1288 if (filename__read_build_id(self->long_name, build_id,
1289 sizeof(build_id))) {
1290 build_id__sprintf(build_id, sizeof(build_id),
1291 build_id_hex);
1292 snprintf(name, size,
1293 "/usr/lib/debug/.build-id/%.2s/%s.debug",
1294 build_id_hex, build_id_hex + 2);
1295 if (self->has_build_id)
1296 goto compare_build_id;
1297 break;
1299 self->origin++;
1300 /* Fall thru */
1301 case DSO__ORIG_DSO:
1302 snprintf(name, size, "%s", self->long_name);
1303 break;
1305 default:
1306 goto out;
1309 if (self->has_build_id) {
1310 if (filename__read_build_id(name, build_id,
1311 sizeof(build_id)) < 0)
1312 goto more;
1313 compare_build_id:
1314 if (!dso__build_id_equal(self, build_id))
1315 goto more;
1317 open_file:
1318 fd = open(name, O_RDONLY);
1319 } while (fd < 0);
1321 ret = dso__load_sym(self, map, name, fd, filter, 0);
1322 close(fd);
1325 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
1327 if (!ret)
1328 goto more;
1330 if (ret > 0) {
1331 int nr_plt = dso__synthesize_plt_symbols(self, map, filter);
1332 if (nr_plt > 0)
1333 ret += nr_plt;
1335 out:
1336 free(name);
1337 if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
1338 return 0;
1339 return ret;
1342 struct map *map_groups__find_by_name(struct map_groups *self,
1343 enum map_type type, const char *name)
1345 struct rb_node *nd;
1347 for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) {
1348 struct map *map = rb_entry(nd, struct map, rb_node);
1350 if (map->dso && strcmp(map->dso->short_name, name) == 0)
1351 return map;
1354 return NULL;
1357 static int dso__kernel_module_get_build_id(struct dso *self)
1359 char filename[PATH_MAX];
1361 * kernel module short names are of the form "[module]" and
1362 * we need just "module" here.
1364 const char *name = self->short_name + 1;
1366 snprintf(filename, sizeof(filename),
1367 "/sys/module/%.*s/notes/.note.gnu.build-id",
1368 (int)strlen(name - 1), name);
1370 if (sysfs__read_build_id(filename, self->build_id,
1371 sizeof(self->build_id)) == 0)
1372 self->has_build_id = true;
1374 return 0;
1377 static int map_groups__set_modules_path_dir(struct map_groups *self, char *dirname)
1379 struct dirent *dent;
1380 DIR *dir = opendir(dirname);
1382 if (!dir) {
1383 pr_debug("%s: cannot open %s dir\n", __func__, dirname);
1384 return -1;
1387 while ((dent = readdir(dir)) != NULL) {
1388 char path[PATH_MAX];
1390 if (dent->d_type == DT_DIR) {
1391 if (!strcmp(dent->d_name, ".") ||
1392 !strcmp(dent->d_name, ".."))
1393 continue;
1395 snprintf(path, sizeof(path), "%s/%s",
1396 dirname, dent->d_name);
1397 if (map_groups__set_modules_path_dir(self, path) < 0)
1398 goto failure;
1399 } else {
1400 char *dot = strrchr(dent->d_name, '.'),
1401 dso_name[PATH_MAX];
1402 struct map *map;
1403 char *long_name;
1405 if (dot == NULL || strcmp(dot, ".ko"))
1406 continue;
1407 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1408 (int)(dot - dent->d_name), dent->d_name);
1410 strxfrchar(dso_name, '-', '_');
1411 map = map_groups__find_by_name(self, MAP__FUNCTION, dso_name);
1412 if (map == NULL)
1413 continue;
1415 snprintf(path, sizeof(path), "%s/%s",
1416 dirname, dent->d_name);
1418 long_name = strdup(path);
1419 if (long_name == NULL)
1420 goto failure;
1421 dso__set_long_name(map->dso, long_name);
1422 dso__kernel_module_get_build_id(map->dso);
1426 return 0;
1427 failure:
1428 closedir(dir);
1429 return -1;
1432 static int map_groups__set_modules_path(struct map_groups *self)
1434 struct utsname uts;
1435 char modules_path[PATH_MAX];
1437 if (uname(&uts) < 0)
1438 return -1;
1440 snprintf(modules_path, sizeof(modules_path), "/lib/modules/%s/kernel",
1441 uts.release);
1443 return map_groups__set_modules_path_dir(self, modules_path);
1447 * Constructor variant for modules (where we know from /proc/modules where
1448 * they are loaded) and for vmlinux, where only after we load all the
1449 * symbols we'll know where it starts and ends.
1451 static struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
1453 struct map *self = zalloc(sizeof(*self) +
1454 (dso->kernel ? sizeof(struct kmap) : 0));
1455 if (self != NULL) {
1457 * ->end will be filled after we load all the symbols
1459 map__init(self, type, start, 0, 0, dso);
1462 return self;
1465 struct map *map_groups__new_module(struct map_groups *self, u64 start,
1466 const char *filename)
1468 struct map *map;
1469 struct dso *dso = __dsos__findnew(&dsos__kernel, filename);
1471 if (dso == NULL)
1472 return NULL;
1474 map = map__new2(start, dso, MAP__FUNCTION);
1475 if (map == NULL)
1476 return NULL;
1478 dso->origin = DSO__ORIG_KMODULE;
1479 map_groups__insert(self, map);
1480 return map;
1483 static int map_groups__create_modules(struct map_groups *self)
1485 char *line = NULL;
1486 size_t n;
1487 FILE *file = fopen("/proc/modules", "r");
1488 struct map *map;
1490 if (file == NULL)
1491 return -1;
1493 while (!feof(file)) {
1494 char name[PATH_MAX];
1495 u64 start;
1496 char *sep;
1497 int line_len;
1499 line_len = getline(&line, &n, file);
1500 if (line_len < 0)
1501 break;
1503 if (!line)
1504 goto out_failure;
1506 line[--line_len] = '\0'; /* \n */
1508 sep = strrchr(line, 'x');
1509 if (sep == NULL)
1510 continue;
1512 hex2u64(sep + 1, &start);
1514 sep = strchr(line, ' ');
1515 if (sep == NULL)
1516 continue;
1518 *sep = '\0';
1520 snprintf(name, sizeof(name), "[%s]", line);
1521 map = map_groups__new_module(self, start, name);
1522 if (map == NULL)
1523 goto out_delete_line;
1524 dso__kernel_module_get_build_id(map->dso);
1527 free(line);
1528 fclose(file);
1530 return map_groups__set_modules_path(self);
1532 out_delete_line:
1533 free(line);
1534 out_failure:
1535 return -1;
1538 static int dso__load_vmlinux(struct dso *self, struct map *map,
1539 const char *vmlinux, symbol_filter_t filter)
1541 int err = -1, fd;
1543 if (self->has_build_id) {
1544 u8 build_id[BUILD_ID_SIZE];
1546 if (filename__read_build_id(vmlinux, build_id,
1547 sizeof(build_id)) < 0) {
1548 pr_debug("No build_id in %s, ignoring it\n", vmlinux);
1549 return -1;
1551 if (!dso__build_id_equal(self, build_id)) {
1552 char expected_build_id[BUILD_ID_SIZE * 2 + 1],
1553 vmlinux_build_id[BUILD_ID_SIZE * 2 + 1];
1555 build_id__sprintf(self->build_id,
1556 sizeof(self->build_id),
1557 expected_build_id);
1558 build_id__sprintf(build_id, sizeof(build_id),
1559 vmlinux_build_id);
1560 pr_debug("build_id in %s is %s while expected is %s, "
1561 "ignoring it\n", vmlinux, vmlinux_build_id,
1562 expected_build_id);
1563 return -1;
1567 fd = open(vmlinux, O_RDONLY);
1568 if (fd < 0)
1569 return -1;
1571 dso__set_loaded(self, map->type);
1572 err = dso__load_sym(self, map, vmlinux, fd, filter, 0);
1573 close(fd);
1575 if (err > 0)
1576 pr_debug("Using %s for symbols\n", vmlinux);
1578 return err;
1581 int dso__load_vmlinux_path(struct dso *self, struct map *map,
1582 symbol_filter_t filter)
1584 int i, err = 0;
1586 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1587 vmlinux_path__nr_entries);
1589 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
1590 err = dso__load_vmlinux(self, map, vmlinux_path[i], filter);
1591 if (err > 0) {
1592 dso__set_long_name(self, strdup(vmlinux_path[i]));
1593 break;
1597 return err;
1600 static int dso__load_kernel_sym(struct dso *self, struct map *map,
1601 symbol_filter_t filter)
1603 int err;
1604 const char *kallsyms_filename = NULL;
1605 char *kallsyms_allocated_filename = NULL;
1607 * Step 1: if the user specified a vmlinux filename, use it and only
1608 * it, reporting errors to the user if it cannot be used.
1610 * For instance, try to analyse an ARM perf.data file _without_ a
1611 * build-id, or if the user specifies the wrong path to the right
1612 * vmlinux file, obviously we can't fallback to another vmlinux (a
1613 * x86_86 one, on the machine where analysis is being performed, say),
1614 * or worse, /proc/kallsyms.
1616 * If the specified file _has_ a build-id and there is a build-id
1617 * section in the perf.data file, we will still do the expected
1618 * validation in dso__load_vmlinux and will bail out if they don't
1619 * match.
1621 if (symbol_conf.vmlinux_name != NULL) {
1622 err = dso__load_vmlinux(self, map,
1623 symbol_conf.vmlinux_name, filter);
1624 goto out_try_fixup;
1627 if (vmlinux_path != NULL) {
1628 err = dso__load_vmlinux_path(self, map, filter);
1629 if (err > 0)
1630 goto out_fixup;
1634 * Say the kernel DSO was created when processing the build-id header table,
1635 * we have a build-id, so check if it is the same as the running kernel,
1636 * using it if it is.
1638 if (self->has_build_id) {
1639 u8 kallsyms_build_id[BUILD_ID_SIZE];
1640 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1642 if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id,
1643 sizeof(kallsyms_build_id)) == 0) {
1644 if (dso__build_id_equal(self, kallsyms_build_id)) {
1645 kallsyms_filename = "/proc/kallsyms";
1646 goto do_kallsyms;
1650 * Now look if we have it on the build-id cache in
1651 * $HOME/.debug/[kernel.kallsyms].
1653 build_id__sprintf(self->build_id, sizeof(self->build_id),
1654 sbuild_id);
1656 if (asprintf(&kallsyms_allocated_filename,
1657 "%s/.debug/[kernel.kallsyms]/%s",
1658 getenv("HOME"), sbuild_id) == -1) {
1659 pr_err("Not enough memory for kallsyms file lookup\n");
1660 return -1;
1663 kallsyms_filename = kallsyms_allocated_filename;
1665 if (access(kallsyms_filename, F_OK)) {
1666 pr_err("No kallsyms or vmlinux with build-id %s "
1667 "was found\n", sbuild_id);
1668 free(kallsyms_allocated_filename);
1669 return -1;
1671 } else {
1673 * Last resort, if we don't have a build-id and couldn't find
1674 * any vmlinux file, try the running kernel kallsyms table.
1676 kallsyms_filename = "/proc/kallsyms";
1679 do_kallsyms:
1680 err = dso__load_kallsyms(self, kallsyms_filename, map, filter);
1681 if (err > 0)
1682 pr_debug("Using %s for symbols\n", kallsyms_filename);
1683 free(kallsyms_allocated_filename);
1685 out_try_fixup:
1686 if (err > 0) {
1687 out_fixup:
1688 if (kallsyms_filename != NULL)
1689 dso__set_long_name(self, strdup("[kernel.kallsyms]"));
1690 map__fixup_start(map);
1691 map__fixup_end(map);
1694 return err;
1697 LIST_HEAD(dsos__user);
1698 LIST_HEAD(dsos__kernel);
1700 static void dsos__add(struct list_head *head, struct dso *dso)
1702 list_add_tail(&dso->node, head);
1705 static struct dso *dsos__find(struct list_head *head, const char *name)
1707 struct dso *pos;
1709 list_for_each_entry(pos, head, node)
1710 if (strcmp(pos->long_name, name) == 0)
1711 return pos;
1712 return NULL;
1715 struct dso *__dsos__findnew(struct list_head *head, const char *name)
1717 struct dso *dso = dsos__find(head, name);
1719 if (!dso) {
1720 dso = dso__new(name);
1721 if (dso != NULL) {
1722 dsos__add(head, dso);
1723 dso__set_basename(dso);
1727 return dso;
1730 static void __dsos__fprintf(struct list_head *head, FILE *fp)
1732 struct dso *pos;
1734 list_for_each_entry(pos, head, node) {
1735 int i;
1736 for (i = 0; i < MAP__NR_TYPES; ++i)
1737 dso__fprintf(pos, i, fp);
1741 void dsos__fprintf(FILE *fp)
1743 __dsos__fprintf(&dsos__kernel, fp);
1744 __dsos__fprintf(&dsos__user, fp);
1747 static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
1748 bool with_hits)
1750 struct dso *pos;
1751 size_t ret = 0;
1753 list_for_each_entry(pos, head, node) {
1754 if (with_hits && !pos->hit)
1755 continue;
1756 ret += dso__fprintf_buildid(pos, fp);
1757 ret += fprintf(fp, " %s\n", pos->long_name);
1759 return ret;
1762 size_t dsos__fprintf_buildid(FILE *fp, bool with_hits)
1764 return (__dsos__fprintf_buildid(&dsos__kernel, fp, with_hits) +
1765 __dsos__fprintf_buildid(&dsos__user, fp, with_hits));
1768 struct dso *dso__new_kernel(const char *name)
1770 struct dso *self = dso__new(name ?: "[kernel.kallsyms]");
1772 if (self != NULL) {
1773 self->short_name = "[kernel]";
1774 self->kernel = 1;
1777 return self;
1780 void dso__read_running_kernel_build_id(struct dso *self)
1782 if (sysfs__read_build_id("/sys/kernel/notes", self->build_id,
1783 sizeof(self->build_id)) == 0)
1784 self->has_build_id = true;
1787 static struct dso *dsos__create_kernel(const char *vmlinux)
1789 struct dso *kernel = dso__new_kernel(vmlinux);
1791 if (kernel != NULL) {
1792 dso__read_running_kernel_build_id(kernel);
1793 dsos__add(&dsos__kernel, kernel);
1796 return kernel;
1799 int __map_groups__create_kernel_maps(struct map_groups *self,
1800 struct map *vmlinux_maps[MAP__NR_TYPES],
1801 struct dso *kernel)
1803 enum map_type type;
1805 for (type = 0; type < MAP__NR_TYPES; ++type) {
1806 struct kmap *kmap;
1808 vmlinux_maps[type] = map__new2(0, kernel, type);
1809 if (vmlinux_maps[type] == NULL)
1810 return -1;
1812 vmlinux_maps[type]->map_ip =
1813 vmlinux_maps[type]->unmap_ip = identity__map_ip;
1815 kmap = map__kmap(vmlinux_maps[type]);
1816 kmap->kmaps = self;
1817 map_groups__insert(self, vmlinux_maps[type]);
1820 return 0;
1823 static void vmlinux_path__exit(void)
1825 while (--vmlinux_path__nr_entries >= 0) {
1826 free(vmlinux_path[vmlinux_path__nr_entries]);
1827 vmlinux_path[vmlinux_path__nr_entries] = NULL;
1830 free(vmlinux_path);
1831 vmlinux_path = NULL;
1834 static int vmlinux_path__init(void)
1836 struct utsname uts;
1837 char bf[PATH_MAX];
1839 if (uname(&uts) < 0)
1840 return -1;
1842 vmlinux_path = malloc(sizeof(char *) * 5);
1843 if (vmlinux_path == NULL)
1844 return -1;
1846 vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
1847 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1848 goto out_fail;
1849 ++vmlinux_path__nr_entries;
1850 vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
1851 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1852 goto out_fail;
1853 ++vmlinux_path__nr_entries;
1854 snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
1855 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1856 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1857 goto out_fail;
1858 ++vmlinux_path__nr_entries;
1859 snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
1860 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1861 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1862 goto out_fail;
1863 ++vmlinux_path__nr_entries;
1864 snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
1865 uts.release);
1866 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1867 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1868 goto out_fail;
1869 ++vmlinux_path__nr_entries;
1871 return 0;
1873 out_fail:
1874 vmlinux_path__exit();
1875 return -1;
1878 size_t vmlinux_path__fprintf(FILE *fp)
1880 int i;
1881 size_t printed = 0;
1883 for (i = 0; i < vmlinux_path__nr_entries; ++i)
1884 printed += fprintf(fp, "[%d] %s\n", i, vmlinux_path[i]);
1886 return printed;
1889 static int setup_list(struct strlist **list, const char *list_str,
1890 const char *list_name)
1892 if (list_str == NULL)
1893 return 0;
1895 *list = strlist__new(true, list_str);
1896 if (!*list) {
1897 pr_err("problems parsing %s list\n", list_name);
1898 return -1;
1900 return 0;
1903 int symbol__init(void)
1905 elf_version(EV_CURRENT);
1906 if (symbol_conf.sort_by_name)
1907 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
1908 sizeof(struct symbol));
1910 if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
1911 return -1;
1913 if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
1914 pr_err("'.' is the only non valid --field-separator argument\n");
1915 return -1;
1918 if (setup_list(&symbol_conf.dso_list,
1919 symbol_conf.dso_list_str, "dso") < 0)
1920 return -1;
1922 if (setup_list(&symbol_conf.comm_list,
1923 symbol_conf.comm_list_str, "comm") < 0)
1924 goto out_free_dso_list;
1926 if (setup_list(&symbol_conf.sym_list,
1927 symbol_conf.sym_list_str, "symbol") < 0)
1928 goto out_free_comm_list;
1930 return 0;
1932 out_free_dso_list:
1933 strlist__delete(symbol_conf.dso_list);
1934 out_free_comm_list:
1935 strlist__delete(symbol_conf.comm_list);
1936 return -1;
1939 int map_groups__create_kernel_maps(struct map_groups *self,
1940 struct map *vmlinux_maps[MAP__NR_TYPES])
1942 struct dso *kernel = dsos__create_kernel(symbol_conf.vmlinux_name);
1944 if (kernel == NULL)
1945 return -1;
1947 if (__map_groups__create_kernel_maps(self, vmlinux_maps, kernel) < 0)
1948 return -1;
1950 if (symbol_conf.use_modules && map_groups__create_modules(self) < 0)
1951 pr_debug("Problems creating module maps, continuing anyway...\n");
1953 * Now that we have all the maps created, just set the ->end of them:
1955 map_groups__fixup_end(self);
1956 return 0;