]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - tools/perf/util/symbol.c
perf tools: Handle relocatable kernels
[mirror_ubuntu-artful-kernel.git] / tools / perf / util / symbol.c
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"
8
9 #include "debug.h"
10
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>
17
18 #ifndef NT_GNU_BUILD_ID
19 #define NT_GNU_BUILD_ID 3
20 #endif
21
22 enum dso_origin {
23 DSO__ORIG_KERNEL = 0,
24 DSO__ORIG_JAVA_JIT,
25 DSO__ORIG_BUILD_ID_CACHE,
26 DSO__ORIG_FEDORA,
27 DSO__ORIG_UBUNTU,
28 DSO__ORIG_BUILDID,
29 DSO__ORIG_DSO,
30 DSO__ORIG_KMODULE,
31 DSO__ORIG_NOT_FOUND,
32 };
33
34 static void dsos__add(struct list_head *head, struct dso *dso);
35 static struct map *map__new2(u64 start, struct dso *dso, enum map_type type);
36 static int dso__load_kernel_sym(struct dso *self, struct map *map,
37 struct perf_session *session, symbol_filter_t filter);
38 static int vmlinux_path__nr_entries;
39 static char **vmlinux_path;
40
41 struct symbol_conf symbol_conf = {
42 .exclude_other = true,
43 .use_modules = true,
44 .try_vmlinux_path = true,
45 };
46
47 bool dso__loaded(const struct dso *self, enum map_type type)
48 {
49 return self->loaded & (1 << type);
50 }
51
52 bool dso__sorted_by_name(const struct dso *self, enum map_type type)
53 {
54 return self->sorted_by_name & (1 << type);
55 }
56
57 static void dso__set_loaded(struct dso *self, enum map_type type)
58 {
59 self->loaded |= (1 << type);
60 }
61
62 static void dso__set_sorted_by_name(struct dso *self, enum map_type type)
63 {
64 self->sorted_by_name |= (1 << type);
65 }
66
67 bool symbol_type__is_a(char symbol_type, enum map_type map_type)
68 {
69 switch (map_type) {
70 case MAP__FUNCTION:
71 return symbol_type == 'T' || symbol_type == 'W';
72 case MAP__VARIABLE:
73 return symbol_type == 'D' || symbol_type == 'd';
74 default:
75 return false;
76 }
77 }
78
79 static void symbols__fixup_end(struct rb_root *self)
80 {
81 struct rb_node *nd, *prevnd = rb_first(self);
82 struct symbol *curr, *prev;
83
84 if (prevnd == NULL)
85 return;
86
87 curr = rb_entry(prevnd, struct symbol, rb_node);
88
89 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
90 prev = curr;
91 curr = rb_entry(nd, struct symbol, rb_node);
92
93 if (prev->end == prev->start)
94 prev->end = curr->start - 1;
95 }
96
97 /* Last entry */
98 if (curr->end == curr->start)
99 curr->end = roundup(curr->start, 4096);
100 }
101
102 static void __map_groups__fixup_end(struct map_groups *self, enum map_type type)
103 {
104 struct map *prev, *curr;
105 struct rb_node *nd, *prevnd = rb_first(&self->maps[type]);
106
107 if (prevnd == NULL)
108 return;
109
110 curr = rb_entry(prevnd, struct map, rb_node);
111
112 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
113 prev = curr;
114 curr = rb_entry(nd, struct map, rb_node);
115 prev->end = curr->start - 1;
116 }
117
118 /*
119 * We still haven't the actual symbols, so guess the
120 * last map final address.
121 */
122 curr->end = ~0UL;
123 }
124
125 static void map_groups__fixup_end(struct map_groups *self)
126 {
127 int i;
128 for (i = 0; i < MAP__NR_TYPES; ++i)
129 __map_groups__fixup_end(self, i);
130 }
131
132 static struct symbol *symbol__new(u64 start, u64 len, const char *name)
133 {
134 size_t namelen = strlen(name) + 1;
135 struct symbol *self = zalloc(symbol_conf.priv_size +
136 sizeof(*self) + namelen);
137 if (self == NULL)
138 return NULL;
139
140 if (symbol_conf.priv_size)
141 self = ((void *)self) + symbol_conf.priv_size;
142
143 self->start = start;
144 self->end = len ? start + len - 1 : start;
145
146 pr_debug3("%s: %s %#Lx-%#Lx\n", __func__, name, start, self->end);
147
148 memcpy(self->name, name, namelen);
149
150 return self;
151 }
152
153 static void symbol__delete(struct symbol *self)
154 {
155 free(((void *)self) - symbol_conf.priv_size);
156 }
157
158 static size_t symbol__fprintf(struct symbol *self, FILE *fp)
159 {
160 return fprintf(fp, " %llx-%llx %s\n",
161 self->start, self->end, self->name);
162 }
163
164 static void dso__set_long_name(struct dso *self, char *name)
165 {
166 if (name == NULL)
167 return;
168 self->long_name = name;
169 self->long_name_len = strlen(name);
170 }
171
172 static void dso__set_basename(struct dso *self)
173 {
174 self->short_name = basename(self->long_name);
175 }
176
177 struct dso *dso__new(const char *name)
178 {
179 struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
180
181 if (self != NULL) {
182 int i;
183 strcpy(self->name, name);
184 dso__set_long_name(self, self->name);
185 self->short_name = self->name;
186 for (i = 0; i < MAP__NR_TYPES; ++i)
187 self->symbols[i] = self->symbol_names[i] = RB_ROOT;
188 self->slen_calculated = 0;
189 self->origin = DSO__ORIG_NOT_FOUND;
190 self->loaded = 0;
191 self->sorted_by_name = 0;
192 self->has_build_id = 0;
193 }
194
195 return self;
196 }
197
198 static void symbols__delete(struct rb_root *self)
199 {
200 struct symbol *pos;
201 struct rb_node *next = rb_first(self);
202
203 while (next) {
204 pos = rb_entry(next, struct symbol, rb_node);
205 next = rb_next(&pos->rb_node);
206 rb_erase(&pos->rb_node, self);
207 symbol__delete(pos);
208 }
209 }
210
211 void dso__delete(struct dso *self)
212 {
213 int i;
214 for (i = 0; i < MAP__NR_TYPES; ++i)
215 symbols__delete(&self->symbols[i]);
216 if (self->long_name != self->name)
217 free(self->long_name);
218 free(self);
219 }
220
221 void dso__set_build_id(struct dso *self, void *build_id)
222 {
223 memcpy(self->build_id, build_id, sizeof(self->build_id));
224 self->has_build_id = 1;
225 }
226
227 static void symbols__insert(struct rb_root *self, struct symbol *sym)
228 {
229 struct rb_node **p = &self->rb_node;
230 struct rb_node *parent = NULL;
231 const u64 ip = sym->start;
232 struct symbol *s;
233
234 while (*p != NULL) {
235 parent = *p;
236 s = rb_entry(parent, struct symbol, rb_node);
237 if (ip < s->start)
238 p = &(*p)->rb_left;
239 else
240 p = &(*p)->rb_right;
241 }
242 rb_link_node(&sym->rb_node, parent, p);
243 rb_insert_color(&sym->rb_node, self);
244 }
245
246 static struct symbol *symbols__find(struct rb_root *self, u64 ip)
247 {
248 struct rb_node *n;
249
250 if (self == NULL)
251 return NULL;
252
253 n = self->rb_node;
254
255 while (n) {
256 struct symbol *s = rb_entry(n, struct symbol, rb_node);
257
258 if (ip < s->start)
259 n = n->rb_left;
260 else if (ip > s->end)
261 n = n->rb_right;
262 else
263 return s;
264 }
265
266 return NULL;
267 }
268
269 struct symbol_name_rb_node {
270 struct rb_node rb_node;
271 struct symbol sym;
272 };
273
274 static void symbols__insert_by_name(struct rb_root *self, struct symbol *sym)
275 {
276 struct rb_node **p = &self->rb_node;
277 struct rb_node *parent = NULL;
278 struct symbol_name_rb_node *symn = ((void *)sym) - sizeof(*parent), *s;
279
280 while (*p != NULL) {
281 parent = *p;
282 s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
283 if (strcmp(sym->name, s->sym.name) < 0)
284 p = &(*p)->rb_left;
285 else
286 p = &(*p)->rb_right;
287 }
288 rb_link_node(&symn->rb_node, parent, p);
289 rb_insert_color(&symn->rb_node, self);
290 }
291
292 static void symbols__sort_by_name(struct rb_root *self, struct rb_root *source)
293 {
294 struct rb_node *nd;
295
296 for (nd = rb_first(source); nd; nd = rb_next(nd)) {
297 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
298 symbols__insert_by_name(self, pos);
299 }
300 }
301
302 static struct symbol *symbols__find_by_name(struct rb_root *self, const char *name)
303 {
304 struct rb_node *n;
305
306 if (self == NULL)
307 return NULL;
308
309 n = self->rb_node;
310
311 while (n) {
312 struct symbol_name_rb_node *s;
313 int cmp;
314
315 s = rb_entry(n, struct symbol_name_rb_node, rb_node);
316 cmp = strcmp(name, s->sym.name);
317
318 if (cmp < 0)
319 n = n->rb_left;
320 else if (cmp > 0)
321 n = n->rb_right;
322 else
323 return &s->sym;
324 }
325
326 return NULL;
327 }
328
329 struct symbol *dso__find_symbol(struct dso *self,
330 enum map_type type, u64 addr)
331 {
332 return symbols__find(&self->symbols[type], addr);
333 }
334
335 struct symbol *dso__find_symbol_by_name(struct dso *self, enum map_type type,
336 const char *name)
337 {
338 return symbols__find_by_name(&self->symbol_names[type], name);
339 }
340
341 void dso__sort_by_name(struct dso *self, enum map_type type)
342 {
343 dso__set_sorted_by_name(self, type);
344 return symbols__sort_by_name(&self->symbol_names[type],
345 &self->symbols[type]);
346 }
347
348 int build_id__sprintf(u8 *self, int len, char *bf)
349 {
350 char *bid = bf;
351 u8 *raw = self;
352 int i;
353
354 for (i = 0; i < len; ++i) {
355 sprintf(bid, "%02x", *raw);
356 ++raw;
357 bid += 2;
358 }
359
360 return raw - self;
361 }
362
363 size_t dso__fprintf_buildid(struct dso *self, FILE *fp)
364 {
365 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
366
367 build_id__sprintf(self->build_id, sizeof(self->build_id), sbuild_id);
368 return fprintf(fp, "%s", sbuild_id);
369 }
370
371 size_t dso__fprintf(struct dso *self, enum map_type type, FILE *fp)
372 {
373 struct rb_node *nd;
374 size_t ret = fprintf(fp, "dso: %s (", self->short_name);
375
376 ret += dso__fprintf_buildid(self, fp);
377 ret += fprintf(fp, ")\n");
378 for (nd = rb_first(&self->symbols[type]); nd; nd = rb_next(nd)) {
379 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
380 ret += symbol__fprintf(pos, fp);
381 }
382
383 return ret;
384 }
385
386 int kallsyms__parse(void *arg, int (*process_symbol)(void *arg, const char *name,
387 char type, u64 start))
388 {
389 char *line = NULL;
390 size_t n;
391 int err = 0;
392 FILE *file = fopen("/proc/kallsyms", "r");
393
394 if (file == NULL)
395 goto out_failure;
396
397 while (!feof(file)) {
398 u64 start;
399 int line_len, len;
400 char symbol_type;
401 char *symbol_name;
402
403 line_len = getline(&line, &n, file);
404 if (line_len < 0)
405 break;
406
407 if (!line)
408 goto out_failure;
409
410 line[--line_len] = '\0'; /* \n */
411
412 len = hex2u64(line, &start);
413
414 len++;
415 if (len + 2 >= line_len)
416 continue;
417
418 symbol_type = toupper(line[len]);
419 symbol_name = line + len + 2;
420
421 err = process_symbol(arg, symbol_name, symbol_type, start);
422 if (err)
423 break;
424 }
425
426 free(line);
427 fclose(file);
428 return err;
429
430 out_failure:
431 return -1;
432 }
433
434 struct process_kallsyms_args {
435 struct map *map;
436 struct dso *dso;
437 };
438
439 static int map__process_kallsym_symbol(void *arg, const char *name,
440 char type, u64 start)
441 {
442 struct symbol *sym;
443 struct process_kallsyms_args *a = arg;
444 struct rb_root *root = &a->dso->symbols[a->map->type];
445
446 if (!symbol_type__is_a(type, a->map->type))
447 return 0;
448
449 /*
450 * Will fix up the end later, when we have all symbols sorted.
451 */
452 sym = symbol__new(start, 0, name);
453
454 if (sym == NULL)
455 return -ENOMEM;
456 /*
457 * We will pass the symbols to the filter later, in
458 * map__split_kallsyms, when we have split the maps per module
459 */
460 symbols__insert(root, sym);
461 return 0;
462 }
463
464 /*
465 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
466 * so that we can in the next step set the symbol ->end address and then
467 * call kernel_maps__split_kallsyms.
468 */
469 static int dso__load_all_kallsyms(struct dso *self, struct map *map)
470 {
471 struct process_kallsyms_args args = { .map = map, .dso = self, };
472 return kallsyms__parse(&args, map__process_kallsym_symbol);
473 }
474
475 /*
476 * Split the symbols into maps, making sure there are no overlaps, i.e. the
477 * kernel range is broken in several maps, named [kernel].N, as we don't have
478 * the original ELF section names vmlinux have.
479 */
480 static int dso__split_kallsyms(struct dso *self, struct map *map,
481 struct perf_session *session, symbol_filter_t filter)
482 {
483 struct map *curr_map = map;
484 struct symbol *pos;
485 int count = 0;
486 struct rb_root *root = &self->symbols[map->type];
487 struct rb_node *next = rb_first(root);
488 int kernel_range = 0;
489
490 while (next) {
491 char *module;
492
493 pos = rb_entry(next, struct symbol, rb_node);
494 next = rb_next(&pos->rb_node);
495
496 module = strchr(pos->name, '\t');
497 if (module) {
498 if (!symbol_conf.use_modules)
499 goto discard_symbol;
500
501 *module++ = '\0';
502
503 if (strcmp(self->name, module)) {
504 curr_map = map_groups__find_by_name(&session->kmaps, map->type, module);
505 if (curr_map == NULL) {
506 pr_debug("/proc/{kallsyms,modules} "
507 "inconsistency!\n");
508 return -1;
509 }
510 }
511 /*
512 * So that we look just like we get from .ko files,
513 * i.e. not prelinked, relative to map->start.
514 */
515 pos->start = curr_map->map_ip(curr_map, pos->start);
516 pos->end = curr_map->map_ip(curr_map, pos->end);
517 } else if (curr_map != map) {
518 char dso_name[PATH_MAX];
519 struct dso *dso;
520
521 snprintf(dso_name, sizeof(dso_name), "[kernel].%d",
522 kernel_range++);
523
524 dso = dso__new(dso_name);
525 if (dso == NULL)
526 return -1;
527
528 curr_map = map__new2(pos->start, dso, map->type);
529 if (map == NULL) {
530 dso__delete(dso);
531 return -1;
532 }
533
534 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
535 map_groups__insert(&session->kmaps, curr_map);
536 ++kernel_range;
537 }
538
539 if (filter && filter(curr_map, pos)) {
540 discard_symbol: rb_erase(&pos->rb_node, root);
541 symbol__delete(pos);
542 } else {
543 if (curr_map != map) {
544 rb_erase(&pos->rb_node, root);
545 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
546 }
547 count++;
548 }
549 }
550
551 return count;
552 }
553
554
555 static int dso__load_kallsyms(struct dso *self, struct map *map,
556 struct perf_session *session, symbol_filter_t filter)
557 {
558 if (dso__load_all_kallsyms(self, map) < 0)
559 return -1;
560
561 symbols__fixup_end(&self->symbols[map->type]);
562 self->origin = DSO__ORIG_KERNEL;
563
564 return dso__split_kallsyms(self, map, session, filter);
565 }
566
567 static int dso__load_perf_map(struct dso *self, struct map *map,
568 symbol_filter_t filter)
569 {
570 char *line = NULL;
571 size_t n;
572 FILE *file;
573 int nr_syms = 0;
574
575 file = fopen(self->long_name, "r");
576 if (file == NULL)
577 goto out_failure;
578
579 while (!feof(file)) {
580 u64 start, size;
581 struct symbol *sym;
582 int line_len, len;
583
584 line_len = getline(&line, &n, file);
585 if (line_len < 0)
586 break;
587
588 if (!line)
589 goto out_failure;
590
591 line[--line_len] = '\0'; /* \n */
592
593 len = hex2u64(line, &start);
594
595 len++;
596 if (len + 2 >= line_len)
597 continue;
598
599 len += hex2u64(line + len, &size);
600
601 len++;
602 if (len + 2 >= line_len)
603 continue;
604
605 sym = symbol__new(start, size, line + len);
606
607 if (sym == NULL)
608 goto out_delete_line;
609
610 if (filter && filter(map, sym))
611 symbol__delete(sym);
612 else {
613 symbols__insert(&self->symbols[map->type], sym);
614 nr_syms++;
615 }
616 }
617
618 free(line);
619 fclose(file);
620
621 return nr_syms;
622
623 out_delete_line:
624 free(line);
625 out_failure:
626 return -1;
627 }
628
629 /**
630 * elf_symtab__for_each_symbol - iterate thru all the symbols
631 *
632 * @self: struct elf_symtab instance to iterate
633 * @idx: uint32_t idx
634 * @sym: GElf_Sym iterator
635 */
636 #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
637 for (idx = 0, gelf_getsym(syms, idx, &sym);\
638 idx < nr_syms; \
639 idx++, gelf_getsym(syms, idx, &sym))
640
641 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
642 {
643 return GELF_ST_TYPE(sym->st_info);
644 }
645
646 static inline int elf_sym__is_function(const GElf_Sym *sym)
647 {
648 return elf_sym__type(sym) == STT_FUNC &&
649 sym->st_name != 0 &&
650 sym->st_shndx != SHN_UNDEF;
651 }
652
653 static inline bool elf_sym__is_object(const GElf_Sym *sym)
654 {
655 return elf_sym__type(sym) == STT_OBJECT &&
656 sym->st_name != 0 &&
657 sym->st_shndx != SHN_UNDEF;
658 }
659
660 static inline int elf_sym__is_label(const GElf_Sym *sym)
661 {
662 return elf_sym__type(sym) == STT_NOTYPE &&
663 sym->st_name != 0 &&
664 sym->st_shndx != SHN_UNDEF &&
665 sym->st_shndx != SHN_ABS;
666 }
667
668 static inline const char *elf_sec__name(const GElf_Shdr *shdr,
669 const Elf_Data *secstrs)
670 {
671 return secstrs->d_buf + shdr->sh_name;
672 }
673
674 static inline int elf_sec__is_text(const GElf_Shdr *shdr,
675 const Elf_Data *secstrs)
676 {
677 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
678 }
679
680 static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
681 const Elf_Data *secstrs)
682 {
683 return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
684 }
685
686 static inline const char *elf_sym__name(const GElf_Sym *sym,
687 const Elf_Data *symstrs)
688 {
689 return symstrs->d_buf + sym->st_name;
690 }
691
692 static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
693 GElf_Shdr *shp, const char *name,
694 size_t *idx)
695 {
696 Elf_Scn *sec = NULL;
697 size_t cnt = 1;
698
699 while ((sec = elf_nextscn(elf, sec)) != NULL) {
700 char *str;
701
702 gelf_getshdr(sec, shp);
703 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
704 if (!strcmp(name, str)) {
705 if (idx)
706 *idx = cnt;
707 break;
708 }
709 ++cnt;
710 }
711
712 return sec;
713 }
714
715 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
716 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
717 idx < nr_entries; \
718 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
719
720 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
721 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
722 idx < nr_entries; \
723 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
724
725 /*
726 * We need to check if we have a .dynsym, so that we can handle the
727 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
728 * .dynsym or .symtab).
729 * And always look at the original dso, not at debuginfo packages, that
730 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
731 */
732 static int dso__synthesize_plt_symbols(struct dso *self, struct map *map,
733 symbol_filter_t filter)
734 {
735 uint32_t nr_rel_entries, idx;
736 GElf_Sym sym;
737 u64 plt_offset;
738 GElf_Shdr shdr_plt;
739 struct symbol *f;
740 GElf_Shdr shdr_rel_plt, shdr_dynsym;
741 Elf_Data *reldata, *syms, *symstrs;
742 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
743 size_t dynsym_idx;
744 GElf_Ehdr ehdr;
745 char sympltname[1024];
746 Elf *elf;
747 int nr = 0, symidx, fd, err = 0;
748
749 fd = open(self->long_name, O_RDONLY);
750 if (fd < 0)
751 goto out;
752
753 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
754 if (elf == NULL)
755 goto out_close;
756
757 if (gelf_getehdr(elf, &ehdr) == NULL)
758 goto out_elf_end;
759
760 scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
761 ".dynsym", &dynsym_idx);
762 if (scn_dynsym == NULL)
763 goto out_elf_end;
764
765 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
766 ".rela.plt", NULL);
767 if (scn_plt_rel == NULL) {
768 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
769 ".rel.plt", NULL);
770 if (scn_plt_rel == NULL)
771 goto out_elf_end;
772 }
773
774 err = -1;
775
776 if (shdr_rel_plt.sh_link != dynsym_idx)
777 goto out_elf_end;
778
779 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
780 goto out_elf_end;
781
782 /*
783 * Fetch the relocation section to find the idxes to the GOT
784 * and the symbols in the .dynsym they refer to.
785 */
786 reldata = elf_getdata(scn_plt_rel, NULL);
787 if (reldata == NULL)
788 goto out_elf_end;
789
790 syms = elf_getdata(scn_dynsym, NULL);
791 if (syms == NULL)
792 goto out_elf_end;
793
794 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
795 if (scn_symstrs == NULL)
796 goto out_elf_end;
797
798 symstrs = elf_getdata(scn_symstrs, NULL);
799 if (symstrs == NULL)
800 goto out_elf_end;
801
802 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
803 plt_offset = shdr_plt.sh_offset;
804
805 if (shdr_rel_plt.sh_type == SHT_RELA) {
806 GElf_Rela pos_mem, *pos;
807
808 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
809 nr_rel_entries) {
810 symidx = GELF_R_SYM(pos->r_info);
811 plt_offset += shdr_plt.sh_entsize;
812 gelf_getsym(syms, symidx, &sym);
813 snprintf(sympltname, sizeof(sympltname),
814 "%s@plt", elf_sym__name(&sym, symstrs));
815
816 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
817 sympltname);
818 if (!f)
819 goto out_elf_end;
820
821 if (filter && filter(map, f))
822 symbol__delete(f);
823 else {
824 symbols__insert(&self->symbols[map->type], f);
825 ++nr;
826 }
827 }
828 } else if (shdr_rel_plt.sh_type == SHT_REL) {
829 GElf_Rel pos_mem, *pos;
830 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
831 nr_rel_entries) {
832 symidx = GELF_R_SYM(pos->r_info);
833 plt_offset += shdr_plt.sh_entsize;
834 gelf_getsym(syms, symidx, &sym);
835 snprintf(sympltname, sizeof(sympltname),
836 "%s@plt", elf_sym__name(&sym, symstrs));
837
838 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
839 sympltname);
840 if (!f)
841 goto out_elf_end;
842
843 if (filter && filter(map, f))
844 symbol__delete(f);
845 else {
846 symbols__insert(&self->symbols[map->type], f);
847 ++nr;
848 }
849 }
850 }
851
852 err = 0;
853 out_elf_end:
854 elf_end(elf);
855 out_close:
856 close(fd);
857
858 if (err == 0)
859 return nr;
860 out:
861 pr_warning("%s: problems reading %s PLT info.\n",
862 __func__, self->long_name);
863 return 0;
864 }
865
866 static bool elf_sym__is_a(GElf_Sym *self, enum map_type type)
867 {
868 switch (type) {
869 case MAP__FUNCTION:
870 return elf_sym__is_function(self);
871 case MAP__VARIABLE:
872 return elf_sym__is_object(self);
873 default:
874 return false;
875 }
876 }
877
878 static bool elf_sec__is_a(GElf_Shdr *self, Elf_Data *secstrs, enum map_type type)
879 {
880 switch (type) {
881 case MAP__FUNCTION:
882 return elf_sec__is_text(self, secstrs);
883 case MAP__VARIABLE:
884 return elf_sec__is_data(self, secstrs);
885 default:
886 return false;
887 }
888 }
889
890 static int dso__load_sym(struct dso *self, struct map *map,
891 struct perf_session *session, const char *name, int fd,
892 symbol_filter_t filter, int kernel, int kmodule)
893 {
894 struct map *curr_map = map;
895 struct dso *curr_dso = self;
896 size_t dso_name_len = strlen(self->short_name);
897 Elf_Data *symstrs, *secstrs;
898 uint32_t nr_syms;
899 int err = -1;
900 uint32_t idx;
901 GElf_Ehdr ehdr;
902 GElf_Shdr shdr;
903 Elf_Data *syms;
904 GElf_Sym sym;
905 Elf_Scn *sec, *sec_strndx;
906 Elf *elf;
907 int nr = 0;
908
909 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
910 if (elf == NULL) {
911 pr_err("%s: cannot read %s ELF file.\n", __func__, name);
912 goto out_close;
913 }
914
915 if (gelf_getehdr(elf, &ehdr) == NULL) {
916 pr_err("%s: cannot get elf header.\n", __func__);
917 goto out_elf_end;
918 }
919
920 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
921 if (sec == NULL) {
922 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
923 if (sec == NULL)
924 goto out_elf_end;
925 }
926
927 syms = elf_getdata(sec, NULL);
928 if (syms == NULL)
929 goto out_elf_end;
930
931 sec = elf_getscn(elf, shdr.sh_link);
932 if (sec == NULL)
933 goto out_elf_end;
934
935 symstrs = elf_getdata(sec, NULL);
936 if (symstrs == NULL)
937 goto out_elf_end;
938
939 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
940 if (sec_strndx == NULL)
941 goto out_elf_end;
942
943 secstrs = elf_getdata(sec_strndx, NULL);
944 if (secstrs == NULL)
945 goto out_elf_end;
946
947 nr_syms = shdr.sh_size / shdr.sh_entsize;
948
949 memset(&sym, 0, sizeof(sym));
950 if (!kernel) {
951 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
952 elf_section_by_name(elf, &ehdr, &shdr,
953 ".gnu.prelink_undo",
954 NULL) != NULL);
955 } else self->adjust_symbols = 0;
956
957 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
958 struct symbol *f;
959 const char *elf_name = elf_sym__name(&sym, symstrs);
960 char *demangled = NULL;
961 int is_label = elf_sym__is_label(&sym);
962 const char *section_name;
963
964 if (kernel && session->ref_reloc_sym.name != NULL &&
965 strcmp(elf_name, session->ref_reloc_sym.name) == 0)
966 perf_session__reloc_vmlinux_maps(session, sym.st_value);
967
968 if (!is_label && !elf_sym__is_a(&sym, map->type))
969 continue;
970
971 sec = elf_getscn(elf, sym.st_shndx);
972 if (!sec)
973 goto out_elf_end;
974
975 gelf_getshdr(sec, &shdr);
976
977 if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type))
978 continue;
979
980 section_name = elf_sec__name(&shdr, secstrs);
981
982 if (kernel || kmodule) {
983 char dso_name[PATH_MAX];
984
985 if (strcmp(section_name,
986 curr_dso->short_name + dso_name_len) == 0)
987 goto new_symbol;
988
989 if (strcmp(section_name, ".text") == 0) {
990 curr_map = map;
991 curr_dso = self;
992 goto new_symbol;
993 }
994
995 snprintf(dso_name, sizeof(dso_name),
996 "%s%s", self->short_name, section_name);
997
998 curr_map = map_groups__find_by_name(&session->kmaps, map->type, dso_name);
999 if (curr_map == NULL) {
1000 u64 start = sym.st_value;
1001
1002 if (kmodule)
1003 start += map->start + shdr.sh_offset;
1004
1005 curr_dso = dso__new(dso_name);
1006 if (curr_dso == NULL)
1007 goto out_elf_end;
1008 curr_map = map__new2(start, curr_dso,
1009 MAP__FUNCTION);
1010 if (curr_map == NULL) {
1011 dso__delete(curr_dso);
1012 goto out_elf_end;
1013 }
1014 curr_map->map_ip = identity__map_ip;
1015 curr_map->unmap_ip = identity__map_ip;
1016 curr_dso->origin = DSO__ORIG_KERNEL;
1017 map_groups__insert(&session->kmaps, curr_map);
1018 dsos__add(&dsos__kernel, curr_dso);
1019 } else
1020 curr_dso = curr_map->dso;
1021
1022 goto new_symbol;
1023 }
1024
1025 if (curr_dso->adjust_symbols) {
1026 pr_debug2("adjusting symbol: st_value: %Lx sh_addr: "
1027 "%Lx sh_offset: %Lx\n", (u64)sym.st_value,
1028 (u64)shdr.sh_addr, (u64)shdr.sh_offset);
1029 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
1030 }
1031 /*
1032 * We need to figure out if the object was created from C++ sources
1033 * DWARF DW_compile_unit has this, but we don't always have access
1034 * to it...
1035 */
1036 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
1037 if (demangled != NULL)
1038 elf_name = demangled;
1039 new_symbol:
1040 f = symbol__new(sym.st_value, sym.st_size, elf_name);
1041 free(demangled);
1042 if (!f)
1043 goto out_elf_end;
1044
1045 if (filter && filter(curr_map, f))
1046 symbol__delete(f);
1047 else {
1048 symbols__insert(&curr_dso->symbols[curr_map->type], f);
1049 nr++;
1050 }
1051 }
1052
1053 /*
1054 * For misannotated, zeroed, ASM function sizes.
1055 */
1056 if (nr > 0)
1057 symbols__fixup_end(&self->symbols[map->type]);
1058 err = nr;
1059 out_elf_end:
1060 elf_end(elf);
1061 out_close:
1062 return err;
1063 }
1064
1065 static bool dso__build_id_equal(const struct dso *self, u8 *build_id)
1066 {
1067 return memcmp(self->build_id, build_id, sizeof(self->build_id)) == 0;
1068 }
1069
1070 static bool __dsos__read_build_ids(struct list_head *head)
1071 {
1072 bool have_build_id = false;
1073 struct dso *pos;
1074
1075 list_for_each_entry(pos, head, node)
1076 if (filename__read_build_id(pos->long_name, pos->build_id,
1077 sizeof(pos->build_id)) > 0) {
1078 have_build_id = true;
1079 pos->has_build_id = true;
1080 }
1081
1082 return have_build_id;
1083 }
1084
1085 bool dsos__read_build_ids(void)
1086 {
1087 bool kbuildids = __dsos__read_build_ids(&dsos__kernel),
1088 ubuildids = __dsos__read_build_ids(&dsos__user);
1089 return kbuildids || ubuildids;
1090 }
1091
1092 /*
1093 * Align offset to 4 bytes as needed for note name and descriptor data.
1094 */
1095 #define NOTE_ALIGN(n) (((n) + 3) & -4U)
1096
1097 int filename__read_build_id(const char *filename, void *bf, size_t size)
1098 {
1099 int fd, err = -1;
1100 GElf_Ehdr ehdr;
1101 GElf_Shdr shdr;
1102 Elf_Data *data;
1103 Elf_Scn *sec;
1104 Elf_Kind ek;
1105 void *ptr;
1106 Elf *elf;
1107
1108 if (size < BUILD_ID_SIZE)
1109 goto out;
1110
1111 fd = open(filename, O_RDONLY);
1112 if (fd < 0)
1113 goto out;
1114
1115 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1116 if (elf == NULL) {
1117 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
1118 goto out_close;
1119 }
1120
1121 ek = elf_kind(elf);
1122 if (ek != ELF_K_ELF)
1123 goto out_elf_end;
1124
1125 if (gelf_getehdr(elf, &ehdr) == NULL) {
1126 pr_err("%s: cannot get elf header.\n", __func__);
1127 goto out_elf_end;
1128 }
1129
1130 sec = elf_section_by_name(elf, &ehdr, &shdr,
1131 ".note.gnu.build-id", NULL);
1132 if (sec == NULL) {
1133 sec = elf_section_by_name(elf, &ehdr, &shdr,
1134 ".notes", NULL);
1135 if (sec == NULL)
1136 goto out_elf_end;
1137 }
1138
1139 data = elf_getdata(sec, NULL);
1140 if (data == NULL)
1141 goto out_elf_end;
1142
1143 ptr = data->d_buf;
1144 while (ptr < (data->d_buf + data->d_size)) {
1145 GElf_Nhdr *nhdr = ptr;
1146 int namesz = NOTE_ALIGN(nhdr->n_namesz),
1147 descsz = NOTE_ALIGN(nhdr->n_descsz);
1148 const char *name;
1149
1150 ptr += sizeof(*nhdr);
1151 name = ptr;
1152 ptr += namesz;
1153 if (nhdr->n_type == NT_GNU_BUILD_ID &&
1154 nhdr->n_namesz == sizeof("GNU")) {
1155 if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
1156 memcpy(bf, ptr, BUILD_ID_SIZE);
1157 err = BUILD_ID_SIZE;
1158 break;
1159 }
1160 }
1161 ptr += descsz;
1162 }
1163 out_elf_end:
1164 elf_end(elf);
1165 out_close:
1166 close(fd);
1167 out:
1168 return err;
1169 }
1170
1171 int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
1172 {
1173 int fd, err = -1;
1174
1175 if (size < BUILD_ID_SIZE)
1176 goto out;
1177
1178 fd = open(filename, O_RDONLY);
1179 if (fd < 0)
1180 goto out;
1181
1182 while (1) {
1183 char bf[BUFSIZ];
1184 GElf_Nhdr nhdr;
1185 int namesz, descsz;
1186
1187 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
1188 break;
1189
1190 namesz = NOTE_ALIGN(nhdr.n_namesz);
1191 descsz = NOTE_ALIGN(nhdr.n_descsz);
1192 if (nhdr.n_type == NT_GNU_BUILD_ID &&
1193 nhdr.n_namesz == sizeof("GNU")) {
1194 if (read(fd, bf, namesz) != namesz)
1195 break;
1196 if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
1197 if (read(fd, build_id,
1198 BUILD_ID_SIZE) == BUILD_ID_SIZE) {
1199 err = 0;
1200 break;
1201 }
1202 } else if (read(fd, bf, descsz) != descsz)
1203 break;
1204 } else {
1205 int n = namesz + descsz;
1206 if (read(fd, bf, n) != n)
1207 break;
1208 }
1209 }
1210 close(fd);
1211 out:
1212 return err;
1213 }
1214
1215 char dso__symtab_origin(const struct dso *self)
1216 {
1217 static const char origin[] = {
1218 [DSO__ORIG_KERNEL] = 'k',
1219 [DSO__ORIG_JAVA_JIT] = 'j',
1220 [DSO__ORIG_BUILD_ID_CACHE] = 'B',
1221 [DSO__ORIG_FEDORA] = 'f',
1222 [DSO__ORIG_UBUNTU] = 'u',
1223 [DSO__ORIG_BUILDID] = 'b',
1224 [DSO__ORIG_DSO] = 'd',
1225 [DSO__ORIG_KMODULE] = 'K',
1226 };
1227
1228 if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
1229 return '!';
1230 return origin[self->origin];
1231 }
1232
1233 int dso__load(struct dso *self, struct map *map, struct perf_session *session,
1234 symbol_filter_t filter)
1235 {
1236 int size = PATH_MAX;
1237 char *name;
1238 u8 build_id[BUILD_ID_SIZE];
1239 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
1240 int ret = -1;
1241 int fd;
1242
1243 dso__set_loaded(self, map->type);
1244
1245 if (self->kernel)
1246 return dso__load_kernel_sym(self, map, session, filter);
1247
1248 name = malloc(size);
1249 if (!name)
1250 return -1;
1251
1252 self->adjust_symbols = 0;
1253
1254 if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
1255 ret = dso__load_perf_map(self, map, filter);
1256 self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
1257 DSO__ORIG_NOT_FOUND;
1258 return ret;
1259 }
1260
1261 self->origin = DSO__ORIG_BUILD_ID_CACHE;
1262
1263 if (self->has_build_id) {
1264 build_id__sprintf(self->build_id, sizeof(self->build_id),
1265 build_id_hex);
1266 snprintf(name, size, "%s/%s/.build-id/%.2s/%s",
1267 getenv("HOME"), DEBUG_CACHE_DIR,
1268 build_id_hex, build_id_hex + 2);
1269 goto open_file;
1270 }
1271 more:
1272 do {
1273 self->origin++;
1274 switch (self->origin) {
1275 case DSO__ORIG_FEDORA:
1276 snprintf(name, size, "/usr/lib/debug%s.debug",
1277 self->long_name);
1278 break;
1279 case DSO__ORIG_UBUNTU:
1280 snprintf(name, size, "/usr/lib/debug%s",
1281 self->long_name);
1282 break;
1283 case DSO__ORIG_BUILDID:
1284 if (filename__read_build_id(self->long_name, build_id,
1285 sizeof(build_id))) {
1286 build_id__sprintf(build_id, sizeof(build_id),
1287 build_id_hex);
1288 snprintf(name, size,
1289 "/usr/lib/debug/.build-id/%.2s/%s.debug",
1290 build_id_hex, build_id_hex + 2);
1291 if (self->has_build_id)
1292 goto compare_build_id;
1293 break;
1294 }
1295 self->origin++;
1296 /* Fall thru */
1297 case DSO__ORIG_DSO:
1298 snprintf(name, size, "%s", self->long_name);
1299 break;
1300
1301 default:
1302 goto out;
1303 }
1304
1305 if (self->has_build_id) {
1306 if (filename__read_build_id(name, build_id,
1307 sizeof(build_id)) < 0)
1308 goto more;
1309 compare_build_id:
1310 if (!dso__build_id_equal(self, build_id))
1311 goto more;
1312 }
1313 open_file:
1314 fd = open(name, O_RDONLY);
1315 } while (fd < 0);
1316
1317 ret = dso__load_sym(self, map, NULL, name, fd, filter, 0, 0);
1318 close(fd);
1319
1320 /*
1321 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
1322 */
1323 if (!ret)
1324 goto more;
1325
1326 if (ret > 0) {
1327 int nr_plt = dso__synthesize_plt_symbols(self, map, filter);
1328 if (nr_plt > 0)
1329 ret += nr_plt;
1330 }
1331 out:
1332 free(name);
1333 if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
1334 return 0;
1335 return ret;
1336 }
1337
1338 struct map *map_groups__find_by_name(struct map_groups *self,
1339 enum map_type type, const char *name)
1340 {
1341 struct rb_node *nd;
1342
1343 for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) {
1344 struct map *map = rb_entry(nd, struct map, rb_node);
1345
1346 if (map->dso && strcmp(map->dso->name, name) == 0)
1347 return map;
1348 }
1349
1350 return NULL;
1351 }
1352
1353 static int perf_session__set_modules_path_dir(struct perf_session *self, char *dirname)
1354 {
1355 struct dirent *dent;
1356 DIR *dir = opendir(dirname);
1357
1358 if (!dir) {
1359 pr_debug("%s: cannot open %s dir\n", __func__, dirname);
1360 return -1;
1361 }
1362
1363 while ((dent = readdir(dir)) != NULL) {
1364 char path[PATH_MAX];
1365
1366 if (dent->d_type == DT_DIR) {
1367 if (!strcmp(dent->d_name, ".") ||
1368 !strcmp(dent->d_name, ".."))
1369 continue;
1370
1371 snprintf(path, sizeof(path), "%s/%s",
1372 dirname, dent->d_name);
1373 if (perf_session__set_modules_path_dir(self, path) < 0)
1374 goto failure;
1375 } else {
1376 char *dot = strrchr(dent->d_name, '.'),
1377 dso_name[PATH_MAX];
1378 struct map *map;
1379 char *long_name;
1380
1381 if (dot == NULL || strcmp(dot, ".ko"))
1382 continue;
1383 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1384 (int)(dot - dent->d_name), dent->d_name);
1385
1386 strxfrchar(dso_name, '-', '_');
1387 map = map_groups__find_by_name(&self->kmaps, MAP__FUNCTION, dso_name);
1388 if (map == NULL)
1389 continue;
1390
1391 snprintf(path, sizeof(path), "%s/%s",
1392 dirname, dent->d_name);
1393
1394 long_name = strdup(path);
1395 if (long_name == NULL)
1396 goto failure;
1397 dso__set_long_name(map->dso, long_name);
1398 }
1399 }
1400
1401 return 0;
1402 failure:
1403 closedir(dir);
1404 return -1;
1405 }
1406
1407 static int perf_session__set_modules_path(struct perf_session *self)
1408 {
1409 struct utsname uts;
1410 char modules_path[PATH_MAX];
1411
1412 if (uname(&uts) < 0)
1413 return -1;
1414
1415 snprintf(modules_path, sizeof(modules_path), "/lib/modules/%s/kernel",
1416 uts.release);
1417
1418 return perf_session__set_modules_path_dir(self, modules_path);
1419 }
1420
1421 /*
1422 * Constructor variant for modules (where we know from /proc/modules where
1423 * they are loaded) and for vmlinux, where only after we load all the
1424 * symbols we'll know where it starts and ends.
1425 */
1426 static struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
1427 {
1428 struct map *self = malloc(sizeof(*self));
1429
1430 if (self != NULL) {
1431 /*
1432 * ->end will be filled after we load all the symbols
1433 */
1434 map__init(self, type, start, 0, 0, dso);
1435 }
1436
1437 return self;
1438 }
1439
1440 static int perf_session__create_module_maps(struct perf_session *self)
1441 {
1442 char *line = NULL;
1443 size_t n;
1444 FILE *file = fopen("/proc/modules", "r");
1445 struct map *map;
1446
1447 if (file == NULL)
1448 return -1;
1449
1450 while (!feof(file)) {
1451 char name[PATH_MAX];
1452 u64 start;
1453 struct dso *dso;
1454 char *sep;
1455 int line_len;
1456
1457 line_len = getline(&line, &n, file);
1458 if (line_len < 0)
1459 break;
1460
1461 if (!line)
1462 goto out_failure;
1463
1464 line[--line_len] = '\0'; /* \n */
1465
1466 sep = strrchr(line, 'x');
1467 if (sep == NULL)
1468 continue;
1469
1470 hex2u64(sep + 1, &start);
1471
1472 sep = strchr(line, ' ');
1473 if (sep == NULL)
1474 continue;
1475
1476 *sep = '\0';
1477
1478 snprintf(name, sizeof(name), "[%s]", line);
1479 dso = dso__new(name);
1480
1481 if (dso == NULL)
1482 goto out_delete_line;
1483
1484 map = map__new2(start, dso, MAP__FUNCTION);
1485 if (map == NULL) {
1486 dso__delete(dso);
1487 goto out_delete_line;
1488 }
1489
1490 snprintf(name, sizeof(name),
1491 "/sys/module/%s/notes/.note.gnu.build-id", line);
1492 if (sysfs__read_build_id(name, dso->build_id,
1493 sizeof(dso->build_id)) == 0)
1494 dso->has_build_id = true;
1495
1496 dso->origin = DSO__ORIG_KMODULE;
1497 map_groups__insert(&self->kmaps, map);
1498 dsos__add(&dsos__kernel, dso);
1499 }
1500
1501 free(line);
1502 fclose(file);
1503
1504 return perf_session__set_modules_path(self);
1505
1506 out_delete_line:
1507 free(line);
1508 out_failure:
1509 return -1;
1510 }
1511
1512 static int dso__load_vmlinux(struct dso *self, struct map *map,
1513 struct perf_session *session,
1514 const char *vmlinux, symbol_filter_t filter)
1515 {
1516 int err = -1, fd;
1517
1518 if (self->has_build_id) {
1519 u8 build_id[BUILD_ID_SIZE];
1520
1521 if (filename__read_build_id(vmlinux, build_id,
1522 sizeof(build_id)) < 0) {
1523 pr_debug("No build_id in %s, ignoring it\n", vmlinux);
1524 return -1;
1525 }
1526 if (!dso__build_id_equal(self, build_id)) {
1527 char expected_build_id[BUILD_ID_SIZE * 2 + 1],
1528 vmlinux_build_id[BUILD_ID_SIZE * 2 + 1];
1529
1530 build_id__sprintf(self->build_id,
1531 sizeof(self->build_id),
1532 expected_build_id);
1533 build_id__sprintf(build_id, sizeof(build_id),
1534 vmlinux_build_id);
1535 pr_debug("build_id in %s is %s while expected is %s, "
1536 "ignoring it\n", vmlinux, vmlinux_build_id,
1537 expected_build_id);
1538 return -1;
1539 }
1540 }
1541
1542 fd = open(vmlinux, O_RDONLY);
1543 if (fd < 0)
1544 return -1;
1545
1546 dso__set_loaded(self, map->type);
1547 err = dso__load_sym(self, map, session, self->long_name, fd, filter, 1, 0);
1548 close(fd);
1549
1550 return err;
1551 }
1552
1553 static int dso__load_kernel_sym(struct dso *self, struct map *map,
1554 struct perf_session *session, symbol_filter_t filter)
1555 {
1556 int err;
1557 bool is_kallsyms;
1558
1559 if (vmlinux_path != NULL) {
1560 int i;
1561 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1562 vmlinux_path__nr_entries);
1563 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
1564 err = dso__load_vmlinux(self, map, session,
1565 vmlinux_path[i], filter);
1566 if (err > 0) {
1567 pr_debug("Using %s for symbols\n",
1568 vmlinux_path[i]);
1569 dso__set_long_name(self,
1570 strdup(vmlinux_path[i]));
1571 goto out_fixup;
1572 }
1573 }
1574 }
1575
1576 is_kallsyms = self->long_name[0] == '[';
1577 if (is_kallsyms)
1578 goto do_kallsyms;
1579
1580 err = dso__load_vmlinux(self, map, session, self->long_name, filter);
1581 if (err <= 0) {
1582 pr_info("The file %s cannot be used, "
1583 "trying to use /proc/kallsyms...", self->long_name);
1584 do_kallsyms:
1585 err = dso__load_kallsyms(self, map, session, filter);
1586 if (err > 0 && !is_kallsyms)
1587 dso__set_long_name(self, strdup("[kernel.kallsyms]"));
1588 }
1589
1590 if (err > 0) {
1591 out_fixup:
1592 map__fixup_start(map);
1593 map__fixup_end(map);
1594 }
1595
1596 return err;
1597 }
1598
1599 LIST_HEAD(dsos__user);
1600 LIST_HEAD(dsos__kernel);
1601 struct dso *vdso;
1602
1603 static void dsos__add(struct list_head *head, struct dso *dso)
1604 {
1605 list_add_tail(&dso->node, head);
1606 }
1607
1608 static struct dso *dsos__find(struct list_head *head, const char *name)
1609 {
1610 struct dso *pos;
1611
1612 list_for_each_entry(pos, head, node)
1613 if (strcmp(pos->name, name) == 0)
1614 return pos;
1615 return NULL;
1616 }
1617
1618 struct dso *dsos__findnew(const char *name)
1619 {
1620 struct dso *dso = dsos__find(&dsos__user, name);
1621
1622 if (!dso) {
1623 dso = dso__new(name);
1624 if (dso != NULL) {
1625 dsos__add(&dsos__user, dso);
1626 dso__set_basename(dso);
1627 }
1628 }
1629
1630 return dso;
1631 }
1632
1633 static void __dsos__fprintf(struct list_head *head, FILE *fp)
1634 {
1635 struct dso *pos;
1636
1637 list_for_each_entry(pos, head, node) {
1638 int i;
1639 for (i = 0; i < MAP__NR_TYPES; ++i)
1640 dso__fprintf(pos, i, fp);
1641 }
1642 }
1643
1644 void dsos__fprintf(FILE *fp)
1645 {
1646 __dsos__fprintf(&dsos__kernel, fp);
1647 __dsos__fprintf(&dsos__user, fp);
1648 }
1649
1650 static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp)
1651 {
1652 struct dso *pos;
1653 size_t ret = 0;
1654
1655 list_for_each_entry(pos, head, node) {
1656 ret += dso__fprintf_buildid(pos, fp);
1657 ret += fprintf(fp, " %s\n", pos->long_name);
1658 }
1659 return ret;
1660 }
1661
1662 size_t dsos__fprintf_buildid(FILE *fp)
1663 {
1664 return (__dsos__fprintf_buildid(&dsos__kernel, fp) +
1665 __dsos__fprintf_buildid(&dsos__user, fp));
1666 }
1667
1668 static struct dso *dsos__create_kernel(const char *vmlinux)
1669 {
1670 struct dso *kernel = dso__new(vmlinux ?: "[kernel.kallsyms]");
1671
1672 if (kernel == NULL)
1673 return NULL;
1674
1675 kernel->short_name = "[kernel]";
1676 kernel->kernel = 1;
1677
1678 vdso = dso__new("[vdso]");
1679 if (vdso == NULL)
1680 goto out_delete_kernel_dso;
1681 dso__set_loaded(vdso, MAP__FUNCTION);
1682
1683 if (sysfs__read_build_id("/sys/kernel/notes", kernel->build_id,
1684 sizeof(kernel->build_id)) == 0)
1685 kernel->has_build_id = true;
1686
1687 dsos__add(&dsos__kernel, kernel);
1688 dsos__add(&dsos__user, vdso);
1689
1690 return kernel;
1691
1692 out_delete_kernel_dso:
1693 dso__delete(kernel);
1694 return NULL;
1695 }
1696
1697 static int map_groups__create_kernel_maps(struct map_groups *self,
1698 struct map *vmlinux_maps[MAP__NR_TYPES],
1699 const char *vmlinux)
1700 {
1701 struct dso *kernel = dsos__create_kernel(vmlinux);
1702 enum map_type type;
1703
1704 if (kernel == NULL)
1705 return -1;
1706
1707 for (type = 0; type < MAP__NR_TYPES; ++type) {
1708 vmlinux_maps[type] = map__new2(0, kernel, type);
1709 if (vmlinux_maps[type] == NULL)
1710 return -1;
1711
1712 vmlinux_maps[type]->map_ip =
1713 vmlinux_maps[type]->unmap_ip = identity__map_ip;
1714 map_groups__insert(self, vmlinux_maps[type]);
1715 }
1716
1717 return 0;
1718 }
1719
1720 static void vmlinux_path__exit(void)
1721 {
1722 while (--vmlinux_path__nr_entries >= 0) {
1723 free(vmlinux_path[vmlinux_path__nr_entries]);
1724 vmlinux_path[vmlinux_path__nr_entries] = NULL;
1725 }
1726
1727 free(vmlinux_path);
1728 vmlinux_path = NULL;
1729 }
1730
1731 static int vmlinux_path__init(void)
1732 {
1733 struct utsname uts;
1734 char bf[PATH_MAX];
1735
1736 if (uname(&uts) < 0)
1737 return -1;
1738
1739 vmlinux_path = malloc(sizeof(char *) * 5);
1740 if (vmlinux_path == NULL)
1741 return -1;
1742
1743 vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
1744 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1745 goto out_fail;
1746 ++vmlinux_path__nr_entries;
1747 vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
1748 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1749 goto out_fail;
1750 ++vmlinux_path__nr_entries;
1751 snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
1752 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1753 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1754 goto out_fail;
1755 ++vmlinux_path__nr_entries;
1756 snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
1757 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1758 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1759 goto out_fail;
1760 ++vmlinux_path__nr_entries;
1761 snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
1762 uts.release);
1763 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1764 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1765 goto out_fail;
1766 ++vmlinux_path__nr_entries;
1767
1768 return 0;
1769
1770 out_fail:
1771 vmlinux_path__exit();
1772 return -1;
1773 }
1774
1775 static int setup_list(struct strlist **list, const char *list_str,
1776 const char *list_name)
1777 {
1778 if (list_str == NULL)
1779 return 0;
1780
1781 *list = strlist__new(true, list_str);
1782 if (!*list) {
1783 pr_err("problems parsing %s list\n", list_name);
1784 return -1;
1785 }
1786 return 0;
1787 }
1788
1789 int symbol__init(void)
1790 {
1791 elf_version(EV_CURRENT);
1792 if (symbol_conf.sort_by_name)
1793 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
1794 sizeof(struct symbol));
1795
1796 if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
1797 return -1;
1798
1799 if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
1800 pr_err("'.' is the only non valid --field-separator argument\n");
1801 return -1;
1802 }
1803
1804 if (setup_list(&symbol_conf.dso_list,
1805 symbol_conf.dso_list_str, "dso") < 0)
1806 return -1;
1807
1808 if (setup_list(&symbol_conf.comm_list,
1809 symbol_conf.comm_list_str, "comm") < 0)
1810 goto out_free_dso_list;
1811
1812 if (setup_list(&symbol_conf.sym_list,
1813 symbol_conf.sym_list_str, "symbol") < 0)
1814 goto out_free_comm_list;
1815
1816 return 0;
1817
1818 out_free_dso_list:
1819 strlist__delete(symbol_conf.dso_list);
1820 out_free_comm_list:
1821 strlist__delete(symbol_conf.comm_list);
1822 return -1;
1823 }
1824
1825 int perf_session__create_kernel_maps(struct perf_session *self)
1826 {
1827 if (map_groups__create_kernel_maps(&self->kmaps, self->vmlinux_maps,
1828 symbol_conf.vmlinux_name) < 0)
1829 return -1;
1830
1831 if (symbol_conf.use_modules &&
1832 perf_session__create_module_maps(self) < 0)
1833 pr_debug("Failed to load list of modules for session %s, "
1834 "continuing...\n", self->filename);
1835 /*
1836 * Now that we have all the maps created, just set the ->end of them:
1837 */
1838 map_groups__fixup_end(&self->kmaps);
1839 return 0;
1840 }