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