ipv6: drop unused "dev" arg of icmpv6_send()
[linux-2.6/next.git] / tools / perf / util / symbol.c
blobab92763edb03f757e2941f95040133f96f84da02
1 #include "util.h"
2 #include "../perf.h"
3 #include "session.h"
4 #include "sort.h"
5 #include "string.h"
6 #include "symbol.h"
7 #include "thread.h"
9 #include "debug.h"
11 #include <asm/bug.h>
12 #include <libelf.h>
13 #include <gelf.h>
14 #include <elf.h>
15 #include <limits.h>
16 #include <sys/utsname.h>
18 #ifndef NT_GNU_BUILD_ID
19 #define NT_GNU_BUILD_ID 3
20 #endif
22 enum dso_origin {
23 DSO__ORIG_KERNEL = 0,
24 DSO__ORIG_JAVA_JIT,
25 DSO__ORIG_FEDORA,
26 DSO__ORIG_UBUNTU,
27 DSO__ORIG_BUILDID,
28 DSO__ORIG_DSO,
29 DSO__ORIG_KMODULE,
30 DSO__ORIG_NOT_FOUND,
33 static void dsos__add(struct list_head *head, struct dso *dso);
34 static struct map *map__new2(u64 start, struct dso *dso, enum map_type type);
35 static int dso__load_kernel_sym(struct dso *self, struct map *map,
36 struct perf_session *session, symbol_filter_t filter);
37 static int vmlinux_path__nr_entries;
38 static char **vmlinux_path;
40 struct symbol_conf symbol_conf = {
41 .exclude_other = true,
42 .use_modules = true,
43 .try_vmlinux_path = true,
46 bool dso__loaded(const struct dso *self, enum map_type type)
48 return self->loaded & (1 << type);
51 bool dso__sorted_by_name(const struct dso *self, enum map_type type)
53 return self->sorted_by_name & (1 << type);
56 static void dso__set_loaded(struct dso *self, enum map_type type)
58 self->loaded |= (1 << type);
61 static void dso__set_sorted_by_name(struct dso *self, enum map_type type)
63 self->sorted_by_name |= (1 << type);
66 static bool symbol_type__is_a(char symbol_type, enum map_type map_type)
68 switch (map_type) {
69 case MAP__FUNCTION:
70 return symbol_type == 'T' || symbol_type == 'W';
71 case MAP__VARIABLE:
72 return symbol_type == 'D' || symbol_type == 'd';
73 default:
74 return false;
78 static void symbols__fixup_end(struct rb_root *self)
80 struct rb_node *nd, *prevnd = rb_first(self);
81 struct symbol *curr, *prev;
83 if (prevnd == NULL)
84 return;
86 curr = rb_entry(prevnd, struct symbol, rb_node);
88 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
89 prev = curr;
90 curr = rb_entry(nd, struct symbol, rb_node);
92 if (prev->end == prev->start)
93 prev->end = curr->start - 1;
96 /* Last entry */
97 if (curr->end == curr->start)
98 curr->end = roundup(curr->start, 4096);
101 static void __map_groups__fixup_end(struct map_groups *self, enum map_type type)
103 struct map *prev, *curr;
104 struct rb_node *nd, *prevnd = rb_first(&self->maps[type]);
106 if (prevnd == NULL)
107 return;
109 curr = rb_entry(prevnd, struct map, rb_node);
111 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
112 prev = curr;
113 curr = rb_entry(nd, struct map, rb_node);
114 prev->end = curr->start - 1;
118 * We still haven't the actual symbols, so guess the
119 * last map final address.
121 curr->end = ~0UL;
124 static void map_groups__fixup_end(struct map_groups *self)
126 int i;
127 for (i = 0; i < MAP__NR_TYPES; ++i)
128 __map_groups__fixup_end(self, i);
131 static struct symbol *symbol__new(u64 start, u64 len, const char *name)
133 size_t namelen = strlen(name) + 1;
134 struct symbol *self = zalloc(symbol_conf.priv_size +
135 sizeof(*self) + namelen);
136 if (self == NULL)
137 return NULL;
139 if (symbol_conf.priv_size)
140 self = ((void *)self) + symbol_conf.priv_size;
142 self->start = start;
143 self->end = len ? start + len - 1 : start;
145 pr_debug3("%s: %s %#Lx-%#Lx\n", __func__, name, start, self->end);
147 memcpy(self->name, name, namelen);
149 return self;
152 static void symbol__delete(struct symbol *self)
154 free(((void *)self) - symbol_conf.priv_size);
157 static size_t symbol__fprintf(struct symbol *self, FILE *fp)
159 return fprintf(fp, " %llx-%llx %s\n",
160 self->start, self->end, self->name);
163 static void dso__set_long_name(struct dso *self, char *name)
165 if (name == NULL)
166 return;
167 self->long_name = name;
168 self->long_name_len = strlen(name);
171 static void dso__set_basename(struct dso *self)
173 self->short_name = basename(self->long_name);
176 struct dso *dso__new(const char *name)
178 struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
180 if (self != NULL) {
181 int i;
182 strcpy(self->name, name);
183 dso__set_long_name(self, self->name);
184 self->short_name = self->name;
185 for (i = 0; i < MAP__NR_TYPES; ++i)
186 self->symbols[i] = self->symbol_names[i] = RB_ROOT;
187 self->slen_calculated = 0;
188 self->origin = DSO__ORIG_NOT_FOUND;
189 self->loaded = 0;
190 self->sorted_by_name = 0;
191 self->has_build_id = 0;
194 return self;
197 static void symbols__delete(struct rb_root *self)
199 struct symbol *pos;
200 struct rb_node *next = rb_first(self);
202 while (next) {
203 pos = rb_entry(next, struct symbol, rb_node);
204 next = rb_next(&pos->rb_node);
205 rb_erase(&pos->rb_node, self);
206 symbol__delete(pos);
210 void dso__delete(struct dso *self)
212 int i;
213 for (i = 0; i < MAP__NR_TYPES; ++i)
214 symbols__delete(&self->symbols[i]);
215 if (self->long_name != self->name)
216 free(self->long_name);
217 free(self);
220 void dso__set_build_id(struct dso *self, void *build_id)
222 memcpy(self->build_id, build_id, sizeof(self->build_id));
223 self->has_build_id = 1;
226 static void symbols__insert(struct rb_root *self, struct symbol *sym)
228 struct rb_node **p = &self->rb_node;
229 struct rb_node *parent = NULL;
230 const u64 ip = sym->start;
231 struct symbol *s;
233 while (*p != NULL) {
234 parent = *p;
235 s = rb_entry(parent, struct symbol, rb_node);
236 if (ip < s->start)
237 p = &(*p)->rb_left;
238 else
239 p = &(*p)->rb_right;
241 rb_link_node(&sym->rb_node, parent, p);
242 rb_insert_color(&sym->rb_node, self);
245 static struct symbol *symbols__find(struct rb_root *self, u64 ip)
247 struct rb_node *n;
249 if (self == NULL)
250 return NULL;
252 n = self->rb_node;
254 while (n) {
255 struct symbol *s = rb_entry(n, struct symbol, rb_node);
257 if (ip < s->start)
258 n = n->rb_left;
259 else if (ip > s->end)
260 n = n->rb_right;
261 else
262 return s;
265 return NULL;
268 struct symbol_name_rb_node {
269 struct rb_node rb_node;
270 struct symbol sym;
273 static void symbols__insert_by_name(struct rb_root *self, struct symbol *sym)
275 struct rb_node **p = &self->rb_node;
276 struct rb_node *parent = NULL;
277 struct symbol_name_rb_node *symn = ((void *)sym) - sizeof(*parent), *s;
279 while (*p != NULL) {
280 parent = *p;
281 s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
282 if (strcmp(sym->name, s->sym.name) < 0)
283 p = &(*p)->rb_left;
284 else
285 p = &(*p)->rb_right;
287 rb_link_node(&symn->rb_node, parent, p);
288 rb_insert_color(&symn->rb_node, self);
291 static void symbols__sort_by_name(struct rb_root *self, struct rb_root *source)
293 struct rb_node *nd;
295 for (nd = rb_first(source); nd; nd = rb_next(nd)) {
296 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
297 symbols__insert_by_name(self, pos);
301 static struct symbol *symbols__find_by_name(struct rb_root *self, const char *name)
303 struct rb_node *n;
305 if (self == NULL)
306 return NULL;
308 n = self->rb_node;
310 while (n) {
311 struct symbol_name_rb_node *s;
312 int cmp;
314 s = rb_entry(n, struct symbol_name_rb_node, rb_node);
315 cmp = strcmp(name, s->sym.name);
317 if (cmp < 0)
318 n = n->rb_left;
319 else if (cmp > 0)
320 n = n->rb_right;
321 else
322 return &s->sym;
325 return NULL;
328 struct symbol *dso__find_symbol(struct dso *self,
329 enum map_type type, u64 addr)
331 return symbols__find(&self->symbols[type], addr);
334 struct symbol *dso__find_symbol_by_name(struct dso *self, enum map_type type,
335 const char *name)
337 return symbols__find_by_name(&self->symbol_names[type], name);
340 void dso__sort_by_name(struct dso *self, enum map_type type)
342 dso__set_sorted_by_name(self, type);
343 return symbols__sort_by_name(&self->symbol_names[type],
344 &self->symbols[type]);
347 int build_id__sprintf(u8 *self, int len, char *bf)
349 char *bid = bf;
350 u8 *raw = self;
351 int i;
353 for (i = 0; i < len; ++i) {
354 sprintf(bid, "%02x", *raw);
355 ++raw;
356 bid += 2;
359 return raw - self;
362 size_t dso__fprintf_buildid(struct dso *self, FILE *fp)
364 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
366 build_id__sprintf(self->build_id, sizeof(self->build_id), sbuild_id);
367 return fprintf(fp, "%s", sbuild_id);
370 size_t dso__fprintf(struct dso *self, enum map_type type, FILE *fp)
372 struct rb_node *nd;
373 size_t ret = fprintf(fp, "dso: %s (", self->short_name);
375 ret += dso__fprintf_buildid(self, fp);
376 ret += fprintf(fp, ")\n");
377 for (nd = rb_first(&self->symbols[type]); nd; nd = rb_next(nd)) {
378 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
379 ret += symbol__fprintf(pos, fp);
382 return ret;
386 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
387 * so that we can in the next step set the symbol ->end address and then
388 * call kernel_maps__split_kallsyms.
390 static int dso__load_all_kallsyms(struct dso *self, struct map *map)
392 char *line = NULL;
393 size_t n;
394 struct rb_root *root = &self->symbols[map->type];
395 FILE *file = fopen("/proc/kallsyms", "r");
397 if (file == NULL)
398 goto out_failure;
400 while (!feof(file)) {
401 u64 start;
402 struct symbol *sym;
403 int line_len, len;
404 char symbol_type;
405 char *symbol_name;
407 line_len = getline(&line, &n, file);
408 if (line_len < 0)
409 break;
411 if (!line)
412 goto out_failure;
414 line[--line_len] = '\0'; /* \n */
416 len = hex2u64(line, &start);
418 len++;
419 if (len + 2 >= line_len)
420 continue;
422 symbol_type = toupper(line[len]);
423 if (!symbol_type__is_a(symbol_type, map->type))
424 continue;
426 symbol_name = line + len + 2;
428 * Will fix up the end later, when we have all symbols sorted.
430 sym = symbol__new(start, 0, symbol_name);
432 if (sym == NULL)
433 goto out_delete_line;
435 * We will pass the symbols to the filter later, in
436 * map__split_kallsyms, when we have split the maps per module
438 symbols__insert(root, sym);
441 free(line);
442 fclose(file);
444 return 0;
446 out_delete_line:
447 free(line);
448 out_failure:
449 return -1;
453 * Split the symbols into maps, making sure there are no overlaps, i.e. the
454 * kernel range is broken in several maps, named [kernel].N, as we don't have
455 * the original ELF section names vmlinux have.
457 static int dso__split_kallsyms(struct dso *self, struct map *map,
458 struct perf_session *session, symbol_filter_t filter)
460 struct map *curr_map = map;
461 struct symbol *pos;
462 int count = 0;
463 struct rb_root *root = &self->symbols[map->type];
464 struct rb_node *next = rb_first(root);
465 int kernel_range = 0;
467 while (next) {
468 char *module;
470 pos = rb_entry(next, struct symbol, rb_node);
471 next = rb_next(&pos->rb_node);
473 module = strchr(pos->name, '\t');
474 if (module) {
475 if (!symbol_conf.use_modules)
476 goto discard_symbol;
478 *module++ = '\0';
480 if (strcmp(self->name, module)) {
481 curr_map = map_groups__find_by_name(&session->kmaps, map->type, module);
482 if (curr_map == NULL) {
483 pr_debug("/proc/{kallsyms,modules} "
484 "inconsistency!\n");
485 return -1;
489 * So that we look just like we get from .ko files,
490 * i.e. not prelinked, relative to map->start.
492 pos->start = curr_map->map_ip(curr_map, pos->start);
493 pos->end = curr_map->map_ip(curr_map, pos->end);
494 } else if (curr_map != map) {
495 char dso_name[PATH_MAX];
496 struct dso *dso;
498 snprintf(dso_name, sizeof(dso_name), "[kernel].%d",
499 kernel_range++);
501 dso = dso__new(dso_name);
502 if (dso == NULL)
503 return -1;
505 curr_map = map__new2(pos->start, dso, map->type);
506 if (map == NULL) {
507 dso__delete(dso);
508 return -1;
511 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
512 map_groups__insert(&session->kmaps, curr_map);
513 ++kernel_range;
516 if (filter && filter(curr_map, pos)) {
517 discard_symbol: rb_erase(&pos->rb_node, root);
518 symbol__delete(pos);
519 } else {
520 if (curr_map != map) {
521 rb_erase(&pos->rb_node, root);
522 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
524 count++;
528 return count;
532 static int dso__load_kallsyms(struct dso *self, struct map *map,
533 struct perf_session *session, symbol_filter_t filter)
535 if (dso__load_all_kallsyms(self, map) < 0)
536 return -1;
538 symbols__fixup_end(&self->symbols[map->type]);
539 self->origin = DSO__ORIG_KERNEL;
541 return dso__split_kallsyms(self, map, session, filter);
544 static int dso__load_perf_map(struct dso *self, struct map *map,
545 symbol_filter_t filter)
547 char *line = NULL;
548 size_t n;
549 FILE *file;
550 int nr_syms = 0;
552 file = fopen(self->long_name, "r");
553 if (file == NULL)
554 goto out_failure;
556 while (!feof(file)) {
557 u64 start, size;
558 struct symbol *sym;
559 int line_len, len;
561 line_len = getline(&line, &n, file);
562 if (line_len < 0)
563 break;
565 if (!line)
566 goto out_failure;
568 line[--line_len] = '\0'; /* \n */
570 len = hex2u64(line, &start);
572 len++;
573 if (len + 2 >= line_len)
574 continue;
576 len += hex2u64(line + len, &size);
578 len++;
579 if (len + 2 >= line_len)
580 continue;
582 sym = symbol__new(start, size, line + len);
584 if (sym == NULL)
585 goto out_delete_line;
587 if (filter && filter(map, sym))
588 symbol__delete(sym);
589 else {
590 symbols__insert(&self->symbols[map->type], sym);
591 nr_syms++;
595 free(line);
596 fclose(file);
598 return nr_syms;
600 out_delete_line:
601 free(line);
602 out_failure:
603 return -1;
607 * elf_symtab__for_each_symbol - iterate thru all the symbols
609 * @self: struct elf_symtab instance to iterate
610 * @idx: uint32_t idx
611 * @sym: GElf_Sym iterator
613 #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
614 for (idx = 0, gelf_getsym(syms, idx, &sym);\
615 idx < nr_syms; \
616 idx++, gelf_getsym(syms, idx, &sym))
618 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
620 return GELF_ST_TYPE(sym->st_info);
623 static inline int elf_sym__is_function(const GElf_Sym *sym)
625 return elf_sym__type(sym) == STT_FUNC &&
626 sym->st_name != 0 &&
627 sym->st_shndx != SHN_UNDEF;
630 static inline bool elf_sym__is_object(const GElf_Sym *sym)
632 return elf_sym__type(sym) == STT_OBJECT &&
633 sym->st_name != 0 &&
634 sym->st_shndx != SHN_UNDEF;
637 static inline int elf_sym__is_label(const GElf_Sym *sym)
639 return elf_sym__type(sym) == STT_NOTYPE &&
640 sym->st_name != 0 &&
641 sym->st_shndx != SHN_UNDEF &&
642 sym->st_shndx != SHN_ABS;
645 static inline const char *elf_sec__name(const GElf_Shdr *shdr,
646 const Elf_Data *secstrs)
648 return secstrs->d_buf + shdr->sh_name;
651 static inline int elf_sec__is_text(const GElf_Shdr *shdr,
652 const Elf_Data *secstrs)
654 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
657 static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
658 const Elf_Data *secstrs)
660 return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
663 static inline const char *elf_sym__name(const GElf_Sym *sym,
664 const Elf_Data *symstrs)
666 return symstrs->d_buf + sym->st_name;
669 static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
670 GElf_Shdr *shp, const char *name,
671 size_t *idx)
673 Elf_Scn *sec = NULL;
674 size_t cnt = 1;
676 while ((sec = elf_nextscn(elf, sec)) != NULL) {
677 char *str;
679 gelf_getshdr(sec, shp);
680 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
681 if (!strcmp(name, str)) {
682 if (idx)
683 *idx = cnt;
684 break;
686 ++cnt;
689 return sec;
692 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
693 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
694 idx < nr_entries; \
695 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
697 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
698 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
699 idx < nr_entries; \
700 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
703 * We need to check if we have a .dynsym, so that we can handle the
704 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
705 * .dynsym or .symtab).
706 * And always look at the original dso, not at debuginfo packages, that
707 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
709 static int dso__synthesize_plt_symbols(struct dso *self, struct map *map,
710 symbol_filter_t filter)
712 uint32_t nr_rel_entries, idx;
713 GElf_Sym sym;
714 u64 plt_offset;
715 GElf_Shdr shdr_plt;
716 struct symbol *f;
717 GElf_Shdr shdr_rel_plt, shdr_dynsym;
718 Elf_Data *reldata, *syms, *symstrs;
719 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
720 size_t dynsym_idx;
721 GElf_Ehdr ehdr;
722 char sympltname[1024];
723 Elf *elf;
724 int nr = 0, symidx, fd, err = 0;
726 fd = open(self->long_name, O_RDONLY);
727 if (fd < 0)
728 goto out;
730 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
731 if (elf == NULL)
732 goto out_close;
734 if (gelf_getehdr(elf, &ehdr) == NULL)
735 goto out_elf_end;
737 scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
738 ".dynsym", &dynsym_idx);
739 if (scn_dynsym == NULL)
740 goto out_elf_end;
742 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
743 ".rela.plt", NULL);
744 if (scn_plt_rel == NULL) {
745 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
746 ".rel.plt", NULL);
747 if (scn_plt_rel == NULL)
748 goto out_elf_end;
751 err = -1;
753 if (shdr_rel_plt.sh_link != dynsym_idx)
754 goto out_elf_end;
756 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
757 goto out_elf_end;
760 * Fetch the relocation section to find the idxes to the GOT
761 * and the symbols in the .dynsym they refer to.
763 reldata = elf_getdata(scn_plt_rel, NULL);
764 if (reldata == NULL)
765 goto out_elf_end;
767 syms = elf_getdata(scn_dynsym, NULL);
768 if (syms == NULL)
769 goto out_elf_end;
771 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
772 if (scn_symstrs == NULL)
773 goto out_elf_end;
775 symstrs = elf_getdata(scn_symstrs, NULL);
776 if (symstrs == NULL)
777 goto out_elf_end;
779 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
780 plt_offset = shdr_plt.sh_offset;
782 if (shdr_rel_plt.sh_type == SHT_RELA) {
783 GElf_Rela pos_mem, *pos;
785 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
786 nr_rel_entries) {
787 symidx = GELF_R_SYM(pos->r_info);
788 plt_offset += shdr_plt.sh_entsize;
789 gelf_getsym(syms, symidx, &sym);
790 snprintf(sympltname, sizeof(sympltname),
791 "%s@plt", elf_sym__name(&sym, symstrs));
793 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
794 sympltname);
795 if (!f)
796 goto out_elf_end;
798 if (filter && filter(map, f))
799 symbol__delete(f);
800 else {
801 symbols__insert(&self->symbols[map->type], f);
802 ++nr;
805 } else if (shdr_rel_plt.sh_type == SHT_REL) {
806 GElf_Rel pos_mem, *pos;
807 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
808 nr_rel_entries) {
809 symidx = GELF_R_SYM(pos->r_info);
810 plt_offset += shdr_plt.sh_entsize;
811 gelf_getsym(syms, symidx, &sym);
812 snprintf(sympltname, sizeof(sympltname),
813 "%s@plt", elf_sym__name(&sym, symstrs));
815 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
816 sympltname);
817 if (!f)
818 goto out_elf_end;
820 if (filter && filter(map, f))
821 symbol__delete(f);
822 else {
823 symbols__insert(&self->symbols[map->type], f);
824 ++nr;
829 err = 0;
830 out_elf_end:
831 elf_end(elf);
832 out_close:
833 close(fd);
835 if (err == 0)
836 return nr;
837 out:
838 pr_warning("%s: problems reading %s PLT info.\n",
839 __func__, self->long_name);
840 return 0;
843 static bool elf_sym__is_a(GElf_Sym *self, enum map_type type)
845 switch (type) {
846 case MAP__FUNCTION:
847 return elf_sym__is_function(self);
848 case MAP__VARIABLE:
849 return elf_sym__is_object(self);
850 default:
851 return false;
855 static bool elf_sec__is_a(GElf_Shdr *self, Elf_Data *secstrs, enum map_type type)
857 switch (type) {
858 case MAP__FUNCTION:
859 return elf_sec__is_text(self, secstrs);
860 case MAP__VARIABLE:
861 return elf_sec__is_data(self, secstrs);
862 default:
863 return false;
867 static int dso__load_sym(struct dso *self, struct map *map,
868 struct perf_session *session, const char *name, int fd,
869 symbol_filter_t filter, int kernel, int kmodule)
871 struct map *curr_map = map;
872 struct dso *curr_dso = self;
873 size_t dso_name_len = strlen(self->short_name);
874 Elf_Data *symstrs, *secstrs;
875 uint32_t nr_syms;
876 int err = -1;
877 uint32_t idx;
878 GElf_Ehdr ehdr;
879 GElf_Shdr shdr;
880 Elf_Data *syms;
881 GElf_Sym sym;
882 Elf_Scn *sec, *sec_strndx;
883 Elf *elf;
884 int nr = 0;
886 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
887 if (elf == NULL) {
888 pr_err("%s: cannot read %s ELF file.\n", __func__, name);
889 goto out_close;
892 if (gelf_getehdr(elf, &ehdr) == NULL) {
893 pr_err("%s: cannot get elf header.\n", __func__);
894 goto out_elf_end;
897 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
898 if (sec == NULL) {
899 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
900 if (sec == NULL)
901 goto out_elf_end;
904 syms = elf_getdata(sec, NULL);
905 if (syms == NULL)
906 goto out_elf_end;
908 sec = elf_getscn(elf, shdr.sh_link);
909 if (sec == NULL)
910 goto out_elf_end;
912 symstrs = elf_getdata(sec, NULL);
913 if (symstrs == NULL)
914 goto out_elf_end;
916 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
917 if (sec_strndx == NULL)
918 goto out_elf_end;
920 secstrs = elf_getdata(sec_strndx, NULL);
921 if (secstrs == NULL)
922 goto out_elf_end;
924 nr_syms = shdr.sh_size / shdr.sh_entsize;
926 memset(&sym, 0, sizeof(sym));
927 if (!kernel) {
928 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
929 elf_section_by_name(elf, &ehdr, &shdr,
930 ".gnu.prelink_undo",
931 NULL) != NULL);
932 } else self->adjust_symbols = 0;
934 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
935 struct symbol *f;
936 const char *elf_name;
937 char *demangled = NULL;
938 int is_label = elf_sym__is_label(&sym);
939 const char *section_name;
941 if (!is_label && !elf_sym__is_a(&sym, map->type))
942 continue;
944 sec = elf_getscn(elf, sym.st_shndx);
945 if (!sec)
946 goto out_elf_end;
948 gelf_getshdr(sec, &shdr);
950 if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type))
951 continue;
953 elf_name = elf_sym__name(&sym, symstrs);
954 section_name = elf_sec__name(&shdr, secstrs);
956 if (kernel || kmodule) {
957 char dso_name[PATH_MAX];
959 if (strcmp(section_name,
960 curr_dso->short_name + dso_name_len) == 0)
961 goto new_symbol;
963 if (strcmp(section_name, ".text") == 0) {
964 curr_map = map;
965 curr_dso = self;
966 goto new_symbol;
969 snprintf(dso_name, sizeof(dso_name),
970 "%s%s", self->short_name, section_name);
972 curr_map = map_groups__find_by_name(&session->kmaps, map->type, dso_name);
973 if (curr_map == NULL) {
974 u64 start = sym.st_value;
976 if (kmodule)
977 start += map->start + shdr.sh_offset;
979 curr_dso = dso__new(dso_name);
980 if (curr_dso == NULL)
981 goto out_elf_end;
982 curr_map = map__new2(start, curr_dso,
983 MAP__FUNCTION);
984 if (curr_map == NULL) {
985 dso__delete(curr_dso);
986 goto out_elf_end;
988 curr_map->map_ip = identity__map_ip;
989 curr_map->unmap_ip = identity__map_ip;
990 curr_dso->origin = DSO__ORIG_KERNEL;
991 map_groups__insert(&session->kmaps, curr_map);
992 dsos__add(&dsos__kernel, curr_dso);
993 } else
994 curr_dso = curr_map->dso;
996 goto new_symbol;
999 if (curr_dso->adjust_symbols) {
1000 pr_debug2("adjusting symbol: st_value: %Lx sh_addr: "
1001 "%Lx sh_offset: %Lx\n", (u64)sym.st_value,
1002 (u64)shdr.sh_addr, (u64)shdr.sh_offset);
1003 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
1006 * We need to figure out if the object was created from C++ sources
1007 * DWARF DW_compile_unit has this, but we don't always have access
1008 * to it...
1010 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
1011 if (demangled != NULL)
1012 elf_name = demangled;
1013 new_symbol:
1014 f = symbol__new(sym.st_value, sym.st_size, elf_name);
1015 free(demangled);
1016 if (!f)
1017 goto out_elf_end;
1019 if (filter && filter(curr_map, f))
1020 symbol__delete(f);
1021 else {
1022 symbols__insert(&curr_dso->symbols[curr_map->type], f);
1023 nr++;
1028 * For misannotated, zeroed, ASM function sizes.
1030 if (nr > 0)
1031 symbols__fixup_end(&self->symbols[map->type]);
1032 err = nr;
1033 out_elf_end:
1034 elf_end(elf);
1035 out_close:
1036 return err;
1039 static bool dso__build_id_equal(const struct dso *self, u8 *build_id)
1041 return memcmp(self->build_id, build_id, sizeof(self->build_id)) == 0;
1044 static bool __dsos__read_build_ids(struct list_head *head)
1046 bool have_build_id = false;
1047 struct dso *pos;
1049 list_for_each_entry(pos, head, node)
1050 if (filename__read_build_id(pos->long_name, pos->build_id,
1051 sizeof(pos->build_id)) > 0) {
1052 have_build_id = true;
1053 pos->has_build_id = true;
1056 return have_build_id;
1059 bool dsos__read_build_ids(void)
1061 bool kbuildids = __dsos__read_build_ids(&dsos__kernel),
1062 ubuildids = __dsos__read_build_ids(&dsos__user);
1063 return kbuildids || ubuildids;
1067 * Align offset to 4 bytes as needed for note name and descriptor data.
1069 #define NOTE_ALIGN(n) (((n) + 3) & -4U)
1071 int filename__read_build_id(const char *filename, void *bf, size_t size)
1073 int fd, err = -1;
1074 GElf_Ehdr ehdr;
1075 GElf_Shdr shdr;
1076 Elf_Data *data;
1077 Elf_Scn *sec;
1078 Elf_Kind ek;
1079 void *ptr;
1080 Elf *elf;
1082 if (size < BUILD_ID_SIZE)
1083 goto out;
1085 fd = open(filename, O_RDONLY);
1086 if (fd < 0)
1087 goto out;
1089 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1090 if (elf == NULL) {
1091 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
1092 goto out_close;
1095 ek = elf_kind(elf);
1096 if (ek != ELF_K_ELF)
1097 goto out_elf_end;
1099 if (gelf_getehdr(elf, &ehdr) == NULL) {
1100 pr_err("%s: cannot get elf header.\n", __func__);
1101 goto out_elf_end;
1104 sec = elf_section_by_name(elf, &ehdr, &shdr,
1105 ".note.gnu.build-id", NULL);
1106 if (sec == NULL) {
1107 sec = elf_section_by_name(elf, &ehdr, &shdr,
1108 ".notes", NULL);
1109 if (sec == NULL)
1110 goto out_elf_end;
1113 data = elf_getdata(sec, NULL);
1114 if (data == NULL)
1115 goto out_elf_end;
1117 ptr = data->d_buf;
1118 while (ptr < (data->d_buf + data->d_size)) {
1119 GElf_Nhdr *nhdr = ptr;
1120 int namesz = NOTE_ALIGN(nhdr->n_namesz),
1121 descsz = NOTE_ALIGN(nhdr->n_descsz);
1122 const char *name;
1124 ptr += sizeof(*nhdr);
1125 name = ptr;
1126 ptr += namesz;
1127 if (nhdr->n_type == NT_GNU_BUILD_ID &&
1128 nhdr->n_namesz == sizeof("GNU")) {
1129 if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
1130 memcpy(bf, ptr, BUILD_ID_SIZE);
1131 err = BUILD_ID_SIZE;
1132 break;
1135 ptr += descsz;
1137 out_elf_end:
1138 elf_end(elf);
1139 out_close:
1140 close(fd);
1141 out:
1142 return err;
1145 int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
1147 int fd, err = -1;
1149 if (size < BUILD_ID_SIZE)
1150 goto out;
1152 fd = open(filename, O_RDONLY);
1153 if (fd < 0)
1154 goto out;
1156 while (1) {
1157 char bf[BUFSIZ];
1158 GElf_Nhdr nhdr;
1159 int namesz, descsz;
1161 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
1162 break;
1164 namesz = NOTE_ALIGN(nhdr.n_namesz);
1165 descsz = NOTE_ALIGN(nhdr.n_descsz);
1166 if (nhdr.n_type == NT_GNU_BUILD_ID &&
1167 nhdr.n_namesz == sizeof("GNU")) {
1168 if (read(fd, bf, namesz) != namesz)
1169 break;
1170 if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
1171 if (read(fd, build_id,
1172 BUILD_ID_SIZE) == BUILD_ID_SIZE) {
1173 err = 0;
1174 break;
1176 } else if (read(fd, bf, descsz) != descsz)
1177 break;
1178 } else {
1179 int n = namesz + descsz;
1180 if (read(fd, bf, n) != n)
1181 break;
1184 close(fd);
1185 out:
1186 return err;
1189 char dso__symtab_origin(const struct dso *self)
1191 static const char origin[] = {
1192 [DSO__ORIG_KERNEL] = 'k',
1193 [DSO__ORIG_JAVA_JIT] = 'j',
1194 [DSO__ORIG_FEDORA] = 'f',
1195 [DSO__ORIG_UBUNTU] = 'u',
1196 [DSO__ORIG_BUILDID] = 'b',
1197 [DSO__ORIG_DSO] = 'd',
1198 [DSO__ORIG_KMODULE] = 'K',
1201 if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
1202 return '!';
1203 return origin[self->origin];
1206 int dso__load(struct dso *self, struct map *map, struct perf_session *session,
1207 symbol_filter_t filter)
1209 int size = PATH_MAX;
1210 char *name;
1211 u8 build_id[BUILD_ID_SIZE];
1212 int ret = -1;
1213 int fd;
1215 dso__set_loaded(self, map->type);
1217 if (self->kernel)
1218 return dso__load_kernel_sym(self, map, session, filter);
1220 name = malloc(size);
1221 if (!name)
1222 return -1;
1224 self->adjust_symbols = 0;
1226 if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
1227 ret = dso__load_perf_map(self, map, filter);
1228 self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
1229 DSO__ORIG_NOT_FOUND;
1230 return ret;
1233 self->origin = DSO__ORIG_FEDORA - 1;
1235 more:
1236 do {
1237 self->origin++;
1238 switch (self->origin) {
1239 case DSO__ORIG_FEDORA:
1240 snprintf(name, size, "/usr/lib/debug%s.debug",
1241 self->long_name);
1242 break;
1243 case DSO__ORIG_UBUNTU:
1244 snprintf(name, size, "/usr/lib/debug%s",
1245 self->long_name);
1246 break;
1247 case DSO__ORIG_BUILDID:
1248 if (filename__read_build_id(self->long_name, build_id,
1249 sizeof(build_id))) {
1250 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
1252 build_id__sprintf(build_id, sizeof(build_id),
1253 build_id_hex);
1254 snprintf(name, size,
1255 "/usr/lib/debug/.build-id/%.2s/%s.debug",
1256 build_id_hex, build_id_hex + 2);
1257 if (self->has_build_id)
1258 goto compare_build_id;
1259 break;
1261 self->origin++;
1262 /* Fall thru */
1263 case DSO__ORIG_DSO:
1264 snprintf(name, size, "%s", self->long_name);
1265 break;
1267 default:
1268 goto out;
1271 if (self->has_build_id) {
1272 if (filename__read_build_id(name, build_id,
1273 sizeof(build_id)) < 0)
1274 goto more;
1275 compare_build_id:
1276 if (!dso__build_id_equal(self, build_id))
1277 goto more;
1280 fd = open(name, O_RDONLY);
1281 } while (fd < 0);
1283 ret = dso__load_sym(self, map, NULL, name, fd, filter, 0, 0);
1284 close(fd);
1287 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
1289 if (!ret)
1290 goto more;
1292 if (ret > 0) {
1293 int nr_plt = dso__synthesize_plt_symbols(self, map, filter);
1294 if (nr_plt > 0)
1295 ret += nr_plt;
1297 out:
1298 free(name);
1299 if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
1300 return 0;
1301 return ret;
1304 struct map *map_groups__find_by_name(struct map_groups *self,
1305 enum map_type type, const char *name)
1307 struct rb_node *nd;
1309 for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) {
1310 struct map *map = rb_entry(nd, struct map, rb_node);
1312 if (map->dso && strcmp(map->dso->name, name) == 0)
1313 return map;
1316 return NULL;
1319 static int perf_session__set_modules_path_dir(struct perf_session *self, char *dirname)
1321 struct dirent *dent;
1322 DIR *dir = opendir(dirname);
1324 if (!dir) {
1325 pr_debug("%s: cannot open %s dir\n", __func__, dirname);
1326 return -1;
1329 while ((dent = readdir(dir)) != NULL) {
1330 char path[PATH_MAX];
1332 if (dent->d_type == DT_DIR) {
1333 if (!strcmp(dent->d_name, ".") ||
1334 !strcmp(dent->d_name, ".."))
1335 continue;
1337 snprintf(path, sizeof(path), "%s/%s",
1338 dirname, dent->d_name);
1339 if (perf_session__set_modules_path_dir(self, path) < 0)
1340 goto failure;
1341 } else {
1342 char *dot = strrchr(dent->d_name, '.'),
1343 dso_name[PATH_MAX];
1344 struct map *map;
1345 char *long_name;
1347 if (dot == NULL || strcmp(dot, ".ko"))
1348 continue;
1349 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1350 (int)(dot - dent->d_name), dent->d_name);
1352 strxfrchar(dso_name, '-', '_');
1353 map = map_groups__find_by_name(&self->kmaps, MAP__FUNCTION, dso_name);
1354 if (map == NULL)
1355 continue;
1357 snprintf(path, sizeof(path), "%s/%s",
1358 dirname, dent->d_name);
1360 long_name = strdup(path);
1361 if (long_name == NULL)
1362 goto failure;
1363 dso__set_long_name(map->dso, long_name);
1367 return 0;
1368 failure:
1369 closedir(dir);
1370 return -1;
1373 static int perf_session__set_modules_path(struct perf_session *self)
1375 struct utsname uts;
1376 char modules_path[PATH_MAX];
1378 if (uname(&uts) < 0)
1379 return -1;
1381 snprintf(modules_path, sizeof(modules_path), "/lib/modules/%s/kernel",
1382 uts.release);
1384 return perf_session__set_modules_path_dir(self, modules_path);
1388 * Constructor variant for modules (where we know from /proc/modules where
1389 * they are loaded) and for vmlinux, where only after we load all the
1390 * symbols we'll know where it starts and ends.
1392 static struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
1394 struct map *self = malloc(sizeof(*self));
1396 if (self != NULL) {
1398 * ->end will be filled after we load all the symbols
1400 map__init(self, type, start, 0, 0, dso);
1403 return self;
1406 static int perf_session__create_module_maps(struct perf_session *self)
1408 char *line = NULL;
1409 size_t n;
1410 FILE *file = fopen("/proc/modules", "r");
1411 struct map *map;
1413 if (file == NULL)
1414 return -1;
1416 while (!feof(file)) {
1417 char name[PATH_MAX];
1418 u64 start;
1419 struct dso *dso;
1420 char *sep;
1421 int line_len;
1423 line_len = getline(&line, &n, file);
1424 if (line_len < 0)
1425 break;
1427 if (!line)
1428 goto out_failure;
1430 line[--line_len] = '\0'; /* \n */
1432 sep = strrchr(line, 'x');
1433 if (sep == NULL)
1434 continue;
1436 hex2u64(sep + 1, &start);
1438 sep = strchr(line, ' ');
1439 if (sep == NULL)
1440 continue;
1442 *sep = '\0';
1444 snprintf(name, sizeof(name), "[%s]", line);
1445 dso = dso__new(name);
1447 if (dso == NULL)
1448 goto out_delete_line;
1450 map = map__new2(start, dso, MAP__FUNCTION);
1451 if (map == NULL) {
1452 dso__delete(dso);
1453 goto out_delete_line;
1456 snprintf(name, sizeof(name),
1457 "/sys/module/%s/notes/.note.gnu.build-id", line);
1458 if (sysfs__read_build_id(name, dso->build_id,
1459 sizeof(dso->build_id)) == 0)
1460 dso->has_build_id = true;
1462 dso->origin = DSO__ORIG_KMODULE;
1463 map_groups__insert(&self->kmaps, map);
1464 dsos__add(&dsos__kernel, dso);
1467 free(line);
1468 fclose(file);
1470 return perf_session__set_modules_path(self);
1472 out_delete_line:
1473 free(line);
1474 out_failure:
1475 return -1;
1478 static int dso__load_vmlinux(struct dso *self, struct map *map,
1479 struct perf_session *session,
1480 const char *vmlinux, symbol_filter_t filter)
1482 int err = -1, fd;
1484 if (self->has_build_id) {
1485 u8 build_id[BUILD_ID_SIZE];
1487 if (filename__read_build_id(vmlinux, build_id,
1488 sizeof(build_id)) < 0) {
1489 pr_debug("No build_id in %s, ignoring it\n", vmlinux);
1490 return -1;
1492 if (!dso__build_id_equal(self, build_id)) {
1493 char expected_build_id[BUILD_ID_SIZE * 2 + 1],
1494 vmlinux_build_id[BUILD_ID_SIZE * 2 + 1];
1496 build_id__sprintf(self->build_id,
1497 sizeof(self->build_id),
1498 expected_build_id);
1499 build_id__sprintf(build_id, sizeof(build_id),
1500 vmlinux_build_id);
1501 pr_debug("build_id in %s is %s while expected is %s, "
1502 "ignoring it\n", vmlinux, vmlinux_build_id,
1503 expected_build_id);
1504 return -1;
1508 fd = open(vmlinux, O_RDONLY);
1509 if (fd < 0)
1510 return -1;
1512 dso__set_loaded(self, map->type);
1513 err = dso__load_sym(self, map, session, self->long_name, fd, filter, 1, 0);
1514 close(fd);
1516 return err;
1519 static int dso__load_kernel_sym(struct dso *self, struct map *map,
1520 struct perf_session *session, symbol_filter_t filter)
1522 int err;
1523 bool is_kallsyms;
1525 if (vmlinux_path != NULL) {
1526 int i;
1527 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1528 vmlinux_path__nr_entries);
1529 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
1530 err = dso__load_vmlinux(self, map, session,
1531 vmlinux_path[i], filter);
1532 if (err > 0) {
1533 pr_debug("Using %s for symbols\n",
1534 vmlinux_path[i]);
1535 dso__set_long_name(self,
1536 strdup(vmlinux_path[i]));
1537 goto out_fixup;
1542 is_kallsyms = self->long_name[0] == '[';
1543 if (is_kallsyms)
1544 goto do_kallsyms;
1546 err = dso__load_vmlinux(self, map, session, self->long_name, filter);
1547 if (err <= 0) {
1548 pr_info("The file %s cannot be used, "
1549 "trying to use /proc/kallsyms...", self->long_name);
1550 do_kallsyms:
1551 err = dso__load_kallsyms(self, map, session, filter);
1552 if (err > 0 && !is_kallsyms)
1553 dso__set_long_name(self, strdup("[kernel.kallsyms]"));
1556 if (err > 0) {
1557 out_fixup:
1558 map__fixup_start(map);
1559 map__fixup_end(map);
1562 return err;
1565 LIST_HEAD(dsos__user);
1566 LIST_HEAD(dsos__kernel);
1567 struct dso *vdso;
1569 static void dsos__add(struct list_head *head, struct dso *dso)
1571 list_add_tail(&dso->node, head);
1574 static struct dso *dsos__find(struct list_head *head, const char *name)
1576 struct dso *pos;
1578 list_for_each_entry(pos, head, node)
1579 if (strcmp(pos->name, name) == 0)
1580 return pos;
1581 return NULL;
1584 struct dso *dsos__findnew(const char *name)
1586 struct dso *dso = dsos__find(&dsos__user, name);
1588 if (!dso) {
1589 dso = dso__new(name);
1590 if (dso != NULL) {
1591 dsos__add(&dsos__user, dso);
1592 dso__set_basename(dso);
1596 return dso;
1599 static void __dsos__fprintf(struct list_head *head, FILE *fp)
1601 struct dso *pos;
1603 list_for_each_entry(pos, head, node) {
1604 int i;
1605 for (i = 0; i < MAP__NR_TYPES; ++i)
1606 dso__fprintf(pos, i, fp);
1610 void dsos__fprintf(FILE *fp)
1612 __dsos__fprintf(&dsos__kernel, fp);
1613 __dsos__fprintf(&dsos__user, fp);
1616 static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp)
1618 struct dso *pos;
1619 size_t ret = 0;
1621 list_for_each_entry(pos, head, node) {
1622 ret += dso__fprintf_buildid(pos, fp);
1623 ret += fprintf(fp, " %s\n", pos->long_name);
1625 return ret;
1628 size_t dsos__fprintf_buildid(FILE *fp)
1630 return (__dsos__fprintf_buildid(&dsos__kernel, fp) +
1631 __dsos__fprintf_buildid(&dsos__user, fp));
1634 static struct dso *dsos__create_kernel( const char *vmlinux)
1636 struct dso *kernel = dso__new(vmlinux ?: "[kernel.kallsyms]");
1638 if (kernel == NULL)
1639 return NULL;
1641 kernel->short_name = "[kernel]";
1642 kernel->kernel = 1;
1644 vdso = dso__new("[vdso]");
1645 if (vdso == NULL)
1646 goto out_delete_kernel_dso;
1647 dso__set_loaded(vdso, MAP__FUNCTION);
1649 if (sysfs__read_build_id("/sys/kernel/notes", kernel->build_id,
1650 sizeof(kernel->build_id)) == 0)
1651 kernel->has_build_id = true;
1653 dsos__add(&dsos__kernel, kernel);
1654 dsos__add(&dsos__user, vdso);
1656 return kernel;
1658 out_delete_kernel_dso:
1659 dso__delete(kernel);
1660 return NULL;
1663 static int map_groups__create_kernel_maps(struct map_groups *self, const char *vmlinux)
1665 struct map *functions, *variables;
1666 struct dso *kernel = dsos__create_kernel(vmlinux);
1668 if (kernel == NULL)
1669 return -1;
1671 functions = map__new2(0, kernel, MAP__FUNCTION);
1672 if (functions == NULL)
1673 return -1;
1675 variables = map__new2(0, kernel, MAP__VARIABLE);
1676 if (variables == NULL) {
1677 map__delete(functions);
1678 return -1;
1681 functions->map_ip = functions->unmap_ip =
1682 variables->map_ip = variables->unmap_ip = identity__map_ip;
1683 map_groups__insert(self, functions);
1684 map_groups__insert(self, variables);
1686 return 0;
1689 static void vmlinux_path__exit(void)
1691 while (--vmlinux_path__nr_entries >= 0) {
1692 free(vmlinux_path[vmlinux_path__nr_entries]);
1693 vmlinux_path[vmlinux_path__nr_entries] = NULL;
1696 free(vmlinux_path);
1697 vmlinux_path = NULL;
1700 static int vmlinux_path__init(void)
1702 struct utsname uts;
1703 char bf[PATH_MAX];
1705 if (uname(&uts) < 0)
1706 return -1;
1708 vmlinux_path = malloc(sizeof(char *) * 5);
1709 if (vmlinux_path == NULL)
1710 return -1;
1712 vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
1713 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1714 goto out_fail;
1715 ++vmlinux_path__nr_entries;
1716 vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
1717 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1718 goto out_fail;
1719 ++vmlinux_path__nr_entries;
1720 snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
1721 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1722 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1723 goto out_fail;
1724 ++vmlinux_path__nr_entries;
1725 snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
1726 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1727 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1728 goto out_fail;
1729 ++vmlinux_path__nr_entries;
1730 snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
1731 uts.release);
1732 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1733 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1734 goto out_fail;
1735 ++vmlinux_path__nr_entries;
1737 return 0;
1739 out_fail:
1740 vmlinux_path__exit();
1741 return -1;
1744 static int setup_list(struct strlist **list, const char *list_str,
1745 const char *list_name)
1747 if (list_str == NULL)
1748 return 0;
1750 *list = strlist__new(true, list_str);
1751 if (!*list) {
1752 pr_err("problems parsing %s list\n", list_name);
1753 return -1;
1755 return 0;
1758 int symbol__init(void)
1760 elf_version(EV_CURRENT);
1761 if (symbol_conf.sort_by_name)
1762 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
1763 sizeof(struct symbol));
1765 if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
1766 return -1;
1768 if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
1769 pr_err("'.' is the only non valid --field-separator argument\n");
1770 return -1;
1773 if (setup_list(&symbol_conf.dso_list,
1774 symbol_conf.dso_list_str, "dso") < 0)
1775 return -1;
1777 if (setup_list(&symbol_conf.comm_list,
1778 symbol_conf.comm_list_str, "comm") < 0)
1779 goto out_free_dso_list;
1781 if (setup_list(&symbol_conf.sym_list,
1782 symbol_conf.sym_list_str, "symbol") < 0)
1783 goto out_free_comm_list;
1785 return 0;
1787 out_free_dso_list:
1788 strlist__delete(symbol_conf.dso_list);
1789 out_free_comm_list:
1790 strlist__delete(symbol_conf.comm_list);
1791 return -1;
1794 int perf_session__create_kernel_maps(struct perf_session *self)
1796 if (map_groups__create_kernel_maps(&self->kmaps,
1797 symbol_conf.vmlinux_name) < 0)
1798 return -1;
1800 if (symbol_conf.use_modules &&
1801 perf_session__create_module_maps(self) < 0)
1802 pr_debug("Failed to load list of modules for session %s, "
1803 "continuing...\n", self->filename);
1805 * Now that we have all the maps created, just set the ->end of them:
1807 map_groups__fixup_end(&self->kmaps);
1808 return 0;