perf sort: Make column width code per hists instance
[deliverable/linux.git] / tools / perf / util / symbol.c
1 #define _GNU_SOURCE
2 #include <ctype.h>
3 #include <dirent.h>
4 #include <errno.h>
5 #include <libgen.h>
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <string.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <sys/param.h>
12 #include <fcntl.h>
13 #include <unistd.h>
14 #include "build-id.h"
15 #include "debug.h"
16 #include "symbol.h"
17 #include "strlist.h"
18
19 #include <libelf.h>
20 #include <gelf.h>
21 #include <elf.h>
22 #include <limits.h>
23 #include <sys/utsname.h>
24
25 #ifndef NT_GNU_BUILD_ID
26 #define NT_GNU_BUILD_ID 3
27 #endif
28
29 static void dsos__add(struct list_head *head, struct dso *dso);
30 static struct map *map__new2(u64 start, struct dso *dso, enum map_type type);
31 static int dso__load_kernel_sym(struct dso *self, struct map *map,
32 symbol_filter_t filter);
33 static int dso__load_guest_kernel_sym(struct dso *self, struct map *map,
34 symbol_filter_t filter);
35 static int vmlinux_path__nr_entries;
36 static char **vmlinux_path;
37
38 struct symbol_conf symbol_conf = {
39 .exclude_other = true,
40 .use_modules = true,
41 .try_vmlinux_path = true,
42 };
43
44 int dso__name_len(const struct dso *self)
45 {
46 if (verbose)
47 return self->long_name_len;
48
49 return self->short_name_len;
50 }
51
52 bool dso__loaded(const struct dso *self, enum map_type type)
53 {
54 return self->loaded & (1 << type);
55 }
56
57 bool dso__sorted_by_name(const struct dso *self, enum map_type type)
58 {
59 return self->sorted_by_name & (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 = calloc(1, (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 self->namelen = namelen - 1;
146
147 pr_debug4("%s: %s %#Lx-%#Lx\n", __func__, name, start, self->end);
148
149 memcpy(self->name, name, namelen);
150
151 return self;
152 }
153
154 void symbol__delete(struct symbol *self)
155 {
156 free(((void *)self) - symbol_conf.priv_size);
157 }
158
159 static size_t symbol__fprintf(struct symbol *self, FILE *fp)
160 {
161 return fprintf(fp, " %llx-%llx %s\n",
162 self->start, self->end, self->name);
163 }
164
165 void dso__set_long_name(struct dso *self, char *name)
166 {
167 if (name == NULL)
168 return;
169 self->long_name = name;
170 self->long_name_len = strlen(name);
171 }
172
173 static void dso__set_short_name(struct dso *self, const char *name)
174 {
175 if (name == NULL)
176 return;
177 self->short_name = name;
178 self->short_name_len = strlen(name);
179 }
180
181 static void dso__set_basename(struct dso *self)
182 {
183 dso__set_short_name(self, basename(self->long_name));
184 }
185
186 struct dso *dso__new(const char *name)
187 {
188 struct dso *self = calloc(1, sizeof(*self) + strlen(name) + 1);
189
190 if (self != NULL) {
191 int i;
192 strcpy(self->name, name);
193 dso__set_long_name(self, self->name);
194 dso__set_short_name(self, self->name);
195 for (i = 0; i < MAP__NR_TYPES; ++i)
196 self->symbols[i] = self->symbol_names[i] = RB_ROOT;
197 self->slen_calculated = 0;
198 self->origin = DSO__ORIG_NOT_FOUND;
199 self->loaded = 0;
200 self->sorted_by_name = 0;
201 self->has_build_id = 0;
202 self->kernel = DSO_TYPE_USER;
203 INIT_LIST_HEAD(&self->node);
204 }
205
206 return self;
207 }
208
209 static void symbols__delete(struct rb_root *self)
210 {
211 struct symbol *pos;
212 struct rb_node *next = rb_first(self);
213
214 while (next) {
215 pos = rb_entry(next, struct symbol, rb_node);
216 next = rb_next(&pos->rb_node);
217 rb_erase(&pos->rb_node, self);
218 symbol__delete(pos);
219 }
220 }
221
222 void dso__delete(struct dso *self)
223 {
224 int i;
225 for (i = 0; i < MAP__NR_TYPES; ++i)
226 symbols__delete(&self->symbols[i]);
227 if (self->long_name != self->name)
228 free(self->long_name);
229 free(self);
230 }
231
232 void dso__set_build_id(struct dso *self, void *build_id)
233 {
234 memcpy(self->build_id, build_id, sizeof(self->build_id));
235 self->has_build_id = 1;
236 }
237
238 static void symbols__insert(struct rb_root *self, struct symbol *sym)
239 {
240 struct rb_node **p = &self->rb_node;
241 struct rb_node *parent = NULL;
242 const u64 ip = sym->start;
243 struct symbol *s;
244
245 while (*p != NULL) {
246 parent = *p;
247 s = rb_entry(parent, struct symbol, rb_node);
248 if (ip < s->start)
249 p = &(*p)->rb_left;
250 else
251 p = &(*p)->rb_right;
252 }
253 rb_link_node(&sym->rb_node, parent, p);
254 rb_insert_color(&sym->rb_node, self);
255 }
256
257 static struct symbol *symbols__find(struct rb_root *self, u64 ip)
258 {
259 struct rb_node *n;
260
261 if (self == NULL)
262 return NULL;
263
264 n = self->rb_node;
265
266 while (n) {
267 struct symbol *s = rb_entry(n, struct symbol, rb_node);
268
269 if (ip < s->start)
270 n = n->rb_left;
271 else if (ip > s->end)
272 n = n->rb_right;
273 else
274 return s;
275 }
276
277 return NULL;
278 }
279
280 struct symbol_name_rb_node {
281 struct rb_node rb_node;
282 struct symbol sym;
283 };
284
285 static void symbols__insert_by_name(struct rb_root *self, struct symbol *sym)
286 {
287 struct rb_node **p = &self->rb_node;
288 struct rb_node *parent = NULL;
289 struct symbol_name_rb_node *symn = ((void *)sym) - sizeof(*parent), *s;
290
291 while (*p != NULL) {
292 parent = *p;
293 s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
294 if (strcmp(sym->name, s->sym.name) < 0)
295 p = &(*p)->rb_left;
296 else
297 p = &(*p)->rb_right;
298 }
299 rb_link_node(&symn->rb_node, parent, p);
300 rb_insert_color(&symn->rb_node, self);
301 }
302
303 static void symbols__sort_by_name(struct rb_root *self, struct rb_root *source)
304 {
305 struct rb_node *nd;
306
307 for (nd = rb_first(source); nd; nd = rb_next(nd)) {
308 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
309 symbols__insert_by_name(self, pos);
310 }
311 }
312
313 static struct symbol *symbols__find_by_name(struct rb_root *self, const char *name)
314 {
315 struct rb_node *n;
316
317 if (self == NULL)
318 return NULL;
319
320 n = self->rb_node;
321
322 while (n) {
323 struct symbol_name_rb_node *s;
324 int cmp;
325
326 s = rb_entry(n, struct symbol_name_rb_node, rb_node);
327 cmp = strcmp(name, s->sym.name);
328
329 if (cmp < 0)
330 n = n->rb_left;
331 else if (cmp > 0)
332 n = n->rb_right;
333 else
334 return &s->sym;
335 }
336
337 return NULL;
338 }
339
340 struct symbol *dso__find_symbol(struct dso *self,
341 enum map_type type, u64 addr)
342 {
343 return symbols__find(&self->symbols[type], addr);
344 }
345
346 struct symbol *dso__find_symbol_by_name(struct dso *self, enum map_type type,
347 const char *name)
348 {
349 return symbols__find_by_name(&self->symbol_names[type], name);
350 }
351
352 void dso__sort_by_name(struct dso *self, enum map_type type)
353 {
354 dso__set_sorted_by_name(self, type);
355 return symbols__sort_by_name(&self->symbol_names[type],
356 &self->symbols[type]);
357 }
358
359 int build_id__sprintf(const u8 *self, int len, char *bf)
360 {
361 char *bid = bf;
362 const u8 *raw = self;
363 int i;
364
365 for (i = 0; i < len; ++i) {
366 sprintf(bid, "%02x", *raw);
367 ++raw;
368 bid += 2;
369 }
370
371 return raw - self;
372 }
373
374 size_t dso__fprintf_buildid(struct dso *self, FILE *fp)
375 {
376 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
377
378 build_id__sprintf(self->build_id, sizeof(self->build_id), sbuild_id);
379 return fprintf(fp, "%s", sbuild_id);
380 }
381
382 size_t dso__fprintf(struct dso *self, enum map_type type, FILE *fp)
383 {
384 struct rb_node *nd;
385 size_t ret = fprintf(fp, "dso: %s (", self->short_name);
386
387 if (self->short_name != self->long_name)
388 ret += fprintf(fp, "%s, ", self->long_name);
389 ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
390 self->loaded ? "" : "NOT ");
391 ret += dso__fprintf_buildid(self, fp);
392 ret += fprintf(fp, ")\n");
393 for (nd = rb_first(&self->symbols[type]); nd; nd = rb_next(nd)) {
394 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
395 ret += symbol__fprintf(pos, fp);
396 }
397
398 return ret;
399 }
400
401 int kallsyms__parse(const char *filename, void *arg,
402 int (*process_symbol)(void *arg, const char *name,
403 char type, u64 start))
404 {
405 char *line = NULL;
406 size_t n;
407 int err = 0;
408 FILE *file = fopen(filename, "r");
409
410 if (file == NULL)
411 goto out_failure;
412
413 while (!feof(file)) {
414 u64 start;
415 int line_len, len;
416 char symbol_type;
417 char *symbol_name;
418
419 line_len = getline(&line, &n, file);
420 if (line_len < 0 || !line)
421 break;
422
423 line[--line_len] = '\0'; /* \n */
424
425 len = hex2u64(line, &start);
426
427 len++;
428 if (len + 2 >= line_len)
429 continue;
430
431 symbol_type = toupper(line[len]);
432 symbol_name = line + len + 2;
433
434 err = process_symbol(arg, symbol_name, symbol_type, start);
435 if (err)
436 break;
437 }
438
439 free(line);
440 fclose(file);
441 return err;
442
443 out_failure:
444 return -1;
445 }
446
447 struct process_kallsyms_args {
448 struct map *map;
449 struct dso *dso;
450 };
451
452 static int map__process_kallsym_symbol(void *arg, const char *name,
453 char type, u64 start)
454 {
455 struct symbol *sym;
456 struct process_kallsyms_args *a = arg;
457 struct rb_root *root = &a->dso->symbols[a->map->type];
458
459 if (!symbol_type__is_a(type, a->map->type))
460 return 0;
461
462 /*
463 * Will fix up the end later, when we have all symbols sorted.
464 */
465 sym = symbol__new(start, 0, name);
466
467 if (sym == NULL)
468 return -ENOMEM;
469 /*
470 * We will pass the symbols to the filter later, in
471 * map__split_kallsyms, when we have split the maps per module
472 */
473 symbols__insert(root, sym);
474
475 return 0;
476 }
477
478 /*
479 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
480 * so that we can in the next step set the symbol ->end address and then
481 * call kernel_maps__split_kallsyms.
482 */
483 static int dso__load_all_kallsyms(struct dso *self, const char *filename,
484 struct map *map)
485 {
486 struct process_kallsyms_args args = { .map = map, .dso = self, };
487 return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
488 }
489
490 /*
491 * Split the symbols into maps, making sure there are no overlaps, i.e. the
492 * kernel range is broken in several maps, named [kernel].N, as we don't have
493 * the original ELF section names vmlinux have.
494 */
495 static int dso__split_kallsyms(struct dso *self, struct map *map,
496 symbol_filter_t filter)
497 {
498 struct map_groups *kmaps = map__kmap(map)->kmaps;
499 struct machine *machine = kmaps->machine;
500 struct map *curr_map = map;
501 struct symbol *pos;
502 int count = 0;
503 struct rb_root *root = &self->symbols[map->type];
504 struct rb_node *next = rb_first(root);
505 int kernel_range = 0;
506
507 while (next) {
508 char *module;
509
510 pos = rb_entry(next, struct symbol, rb_node);
511 next = rb_next(&pos->rb_node);
512
513 module = strchr(pos->name, '\t');
514 if (module) {
515 if (!symbol_conf.use_modules)
516 goto discard_symbol;
517
518 *module++ = '\0';
519
520 if (strcmp(curr_map->dso->short_name, module)) {
521 if (curr_map != map &&
522 self->kernel == DSO_TYPE_GUEST_KERNEL &&
523 machine__is_default_guest(machine)) {
524 /*
525 * We assume all symbols of a module are
526 * continuous in * kallsyms, so curr_map
527 * points to a module and all its
528 * symbols are in its kmap. Mark it as
529 * loaded.
530 */
531 dso__set_loaded(curr_map->dso,
532 curr_map->type);
533 }
534
535 curr_map = map_groups__find_by_name(kmaps,
536 map->type, module);
537 if (curr_map == NULL) {
538 pr_debug("%s/proc/{kallsyms,modules} "
539 "inconsistency while looking "
540 "for \"%s\" module!\n",
541 machine->root_dir, module);
542 curr_map = map;
543 goto discard_symbol;
544 }
545
546 if (curr_map->dso->loaded &&
547 !machine__is_default_guest(machine))
548 goto discard_symbol;
549 }
550 /*
551 * So that we look just like we get from .ko files,
552 * i.e. not prelinked, relative to map->start.
553 */
554 pos->start = curr_map->map_ip(curr_map, pos->start);
555 pos->end = curr_map->map_ip(curr_map, pos->end);
556 } else if (curr_map != map) {
557 char dso_name[PATH_MAX];
558 struct dso *dso;
559
560 if (self->kernel == DSO_TYPE_GUEST_KERNEL)
561 snprintf(dso_name, sizeof(dso_name),
562 "[guest.kernel].%d",
563 kernel_range++);
564 else
565 snprintf(dso_name, sizeof(dso_name),
566 "[kernel].%d",
567 kernel_range++);
568
569 dso = dso__new(dso_name);
570 if (dso == NULL)
571 return -1;
572
573 dso->kernel = self->kernel;
574
575 curr_map = map__new2(pos->start, dso, map->type);
576 if (curr_map == NULL) {
577 dso__delete(dso);
578 return -1;
579 }
580
581 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
582 map_groups__insert(kmaps, curr_map);
583 ++kernel_range;
584 }
585
586 if (filter && filter(curr_map, pos)) {
587 discard_symbol: rb_erase(&pos->rb_node, root);
588 symbol__delete(pos);
589 } else {
590 if (curr_map != map) {
591 rb_erase(&pos->rb_node, root);
592 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
593 }
594 count++;
595 }
596 }
597
598 if (curr_map != map &&
599 self->kernel == DSO_TYPE_GUEST_KERNEL &&
600 machine__is_default_guest(kmaps->machine)) {
601 dso__set_loaded(curr_map->dso, curr_map->type);
602 }
603
604 return count;
605 }
606
607 int dso__load_kallsyms(struct dso *self, const char *filename,
608 struct map *map, symbol_filter_t filter)
609 {
610 if (dso__load_all_kallsyms(self, filename, map) < 0)
611 return -1;
612
613 symbols__fixup_end(&self->symbols[map->type]);
614 if (self->kernel == DSO_TYPE_GUEST_KERNEL)
615 self->origin = DSO__ORIG_GUEST_KERNEL;
616 else
617 self->origin = DSO__ORIG_KERNEL;
618
619 return dso__split_kallsyms(self, map, filter);
620 }
621
622 static int dso__load_perf_map(struct dso *self, struct map *map,
623 symbol_filter_t filter)
624 {
625 char *line = NULL;
626 size_t n;
627 FILE *file;
628 int nr_syms = 0;
629
630 file = fopen(self->long_name, "r");
631 if (file == NULL)
632 goto out_failure;
633
634 while (!feof(file)) {
635 u64 start, size;
636 struct symbol *sym;
637 int line_len, len;
638
639 line_len = getline(&line, &n, file);
640 if (line_len < 0)
641 break;
642
643 if (!line)
644 goto out_failure;
645
646 line[--line_len] = '\0'; /* \n */
647
648 len = hex2u64(line, &start);
649
650 len++;
651 if (len + 2 >= line_len)
652 continue;
653
654 len += hex2u64(line + len, &size);
655
656 len++;
657 if (len + 2 >= line_len)
658 continue;
659
660 sym = symbol__new(start, size, line + len);
661
662 if (sym == NULL)
663 goto out_delete_line;
664
665 if (filter && filter(map, sym))
666 symbol__delete(sym);
667 else {
668 symbols__insert(&self->symbols[map->type], sym);
669 nr_syms++;
670 }
671 }
672
673 free(line);
674 fclose(file);
675
676 return nr_syms;
677
678 out_delete_line:
679 free(line);
680 out_failure:
681 return -1;
682 }
683
684 /**
685 * elf_symtab__for_each_symbol - iterate thru all the symbols
686 *
687 * @self: struct elf_symtab instance to iterate
688 * @idx: uint32_t idx
689 * @sym: GElf_Sym iterator
690 */
691 #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
692 for (idx = 0, gelf_getsym(syms, idx, &sym);\
693 idx < nr_syms; \
694 idx++, gelf_getsym(syms, idx, &sym))
695
696 static inline uint8_t elf_sym__type(const GElf_Sym *sym)
697 {
698 return GELF_ST_TYPE(sym->st_info);
699 }
700
701 static inline int elf_sym__is_function(const GElf_Sym *sym)
702 {
703 return elf_sym__type(sym) == STT_FUNC &&
704 sym->st_name != 0 &&
705 sym->st_shndx != SHN_UNDEF;
706 }
707
708 static inline bool elf_sym__is_object(const GElf_Sym *sym)
709 {
710 return elf_sym__type(sym) == STT_OBJECT &&
711 sym->st_name != 0 &&
712 sym->st_shndx != SHN_UNDEF;
713 }
714
715 static inline int elf_sym__is_label(const GElf_Sym *sym)
716 {
717 return elf_sym__type(sym) == STT_NOTYPE &&
718 sym->st_name != 0 &&
719 sym->st_shndx != SHN_UNDEF &&
720 sym->st_shndx != SHN_ABS;
721 }
722
723 static inline const char *elf_sec__name(const GElf_Shdr *shdr,
724 const Elf_Data *secstrs)
725 {
726 return secstrs->d_buf + shdr->sh_name;
727 }
728
729 static inline int elf_sec__is_text(const GElf_Shdr *shdr,
730 const Elf_Data *secstrs)
731 {
732 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
733 }
734
735 static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
736 const Elf_Data *secstrs)
737 {
738 return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
739 }
740
741 static inline const char *elf_sym__name(const GElf_Sym *sym,
742 const Elf_Data *symstrs)
743 {
744 return symstrs->d_buf + sym->st_name;
745 }
746
747 static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
748 GElf_Shdr *shp, const char *name,
749 size_t *idx)
750 {
751 Elf_Scn *sec = NULL;
752 size_t cnt = 1;
753
754 while ((sec = elf_nextscn(elf, sec)) != NULL) {
755 char *str;
756
757 gelf_getshdr(sec, shp);
758 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
759 if (!strcmp(name, str)) {
760 if (idx)
761 *idx = cnt;
762 break;
763 }
764 ++cnt;
765 }
766
767 return sec;
768 }
769
770 #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
771 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
772 idx < nr_entries; \
773 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
774
775 #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
776 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
777 idx < nr_entries; \
778 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
779
780 /*
781 * We need to check if we have a .dynsym, so that we can handle the
782 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
783 * .dynsym or .symtab).
784 * And always look at the original dso, not at debuginfo packages, that
785 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
786 */
787 static int dso__synthesize_plt_symbols(struct dso *self, struct map *map,
788 symbol_filter_t filter)
789 {
790 uint32_t nr_rel_entries, idx;
791 GElf_Sym sym;
792 u64 plt_offset;
793 GElf_Shdr shdr_plt;
794 struct symbol *f;
795 GElf_Shdr shdr_rel_plt, shdr_dynsym;
796 Elf_Data *reldata, *syms, *symstrs;
797 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
798 size_t dynsym_idx;
799 GElf_Ehdr ehdr;
800 char sympltname[1024];
801 Elf *elf;
802 int nr = 0, symidx, fd, err = 0;
803
804 fd = open(self->long_name, O_RDONLY);
805 if (fd < 0)
806 goto out;
807
808 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
809 if (elf == NULL)
810 goto out_close;
811
812 if (gelf_getehdr(elf, &ehdr) == NULL)
813 goto out_elf_end;
814
815 scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
816 ".dynsym", &dynsym_idx);
817 if (scn_dynsym == NULL)
818 goto out_elf_end;
819
820 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
821 ".rela.plt", NULL);
822 if (scn_plt_rel == NULL) {
823 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
824 ".rel.plt", NULL);
825 if (scn_plt_rel == NULL)
826 goto out_elf_end;
827 }
828
829 err = -1;
830
831 if (shdr_rel_plt.sh_link != dynsym_idx)
832 goto out_elf_end;
833
834 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
835 goto out_elf_end;
836
837 /*
838 * Fetch the relocation section to find the idxes to the GOT
839 * and the symbols in the .dynsym they refer to.
840 */
841 reldata = elf_getdata(scn_plt_rel, NULL);
842 if (reldata == NULL)
843 goto out_elf_end;
844
845 syms = elf_getdata(scn_dynsym, NULL);
846 if (syms == NULL)
847 goto out_elf_end;
848
849 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
850 if (scn_symstrs == NULL)
851 goto out_elf_end;
852
853 symstrs = elf_getdata(scn_symstrs, NULL);
854 if (symstrs == NULL)
855 goto out_elf_end;
856
857 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
858 plt_offset = shdr_plt.sh_offset;
859
860 if (shdr_rel_plt.sh_type == SHT_RELA) {
861 GElf_Rela pos_mem, *pos;
862
863 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
864 nr_rel_entries) {
865 symidx = GELF_R_SYM(pos->r_info);
866 plt_offset += shdr_plt.sh_entsize;
867 gelf_getsym(syms, symidx, &sym);
868 snprintf(sympltname, sizeof(sympltname),
869 "%s@plt", elf_sym__name(&sym, symstrs));
870
871 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
872 sympltname);
873 if (!f)
874 goto out_elf_end;
875
876 if (filter && filter(map, f))
877 symbol__delete(f);
878 else {
879 symbols__insert(&self->symbols[map->type], f);
880 ++nr;
881 }
882 }
883 } else if (shdr_rel_plt.sh_type == SHT_REL) {
884 GElf_Rel pos_mem, *pos;
885 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
886 nr_rel_entries) {
887 symidx = GELF_R_SYM(pos->r_info);
888 plt_offset += shdr_plt.sh_entsize;
889 gelf_getsym(syms, symidx, &sym);
890 snprintf(sympltname, sizeof(sympltname),
891 "%s@plt", elf_sym__name(&sym, symstrs));
892
893 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
894 sympltname);
895 if (!f)
896 goto out_elf_end;
897
898 if (filter && filter(map, f))
899 symbol__delete(f);
900 else {
901 symbols__insert(&self->symbols[map->type], f);
902 ++nr;
903 }
904 }
905 }
906
907 err = 0;
908 out_elf_end:
909 elf_end(elf);
910 out_close:
911 close(fd);
912
913 if (err == 0)
914 return nr;
915 out:
916 pr_debug("%s: problems reading %s PLT info.\n",
917 __func__, self->long_name);
918 return 0;
919 }
920
921 static bool elf_sym__is_a(GElf_Sym *self, enum map_type type)
922 {
923 switch (type) {
924 case MAP__FUNCTION:
925 return elf_sym__is_function(self);
926 case MAP__VARIABLE:
927 return elf_sym__is_object(self);
928 default:
929 return false;
930 }
931 }
932
933 static bool elf_sec__is_a(GElf_Shdr *self, Elf_Data *secstrs, enum map_type type)
934 {
935 switch (type) {
936 case MAP__FUNCTION:
937 return elf_sec__is_text(self, secstrs);
938 case MAP__VARIABLE:
939 return elf_sec__is_data(self, secstrs);
940 default:
941 return false;
942 }
943 }
944
945 static size_t elf_addr_to_index(Elf *elf, GElf_Addr addr)
946 {
947 Elf_Scn *sec = NULL;
948 GElf_Shdr shdr;
949 size_t cnt = 1;
950
951 while ((sec = elf_nextscn(elf, sec)) != NULL) {
952 gelf_getshdr(sec, &shdr);
953
954 if ((addr >= shdr.sh_addr) &&
955 (addr < (shdr.sh_addr + shdr.sh_size)))
956 return cnt;
957
958 ++cnt;
959 }
960
961 return -1;
962 }
963
964 static int dso__load_sym(struct dso *self, struct map *map, const char *name,
965 int fd, symbol_filter_t filter, int kmodule)
966 {
967 struct kmap *kmap = self->kernel ? map__kmap(map) : NULL;
968 struct map *curr_map = map;
969 struct dso *curr_dso = self;
970 Elf_Data *symstrs, *secstrs;
971 uint32_t nr_syms;
972 int err = -1;
973 uint32_t idx;
974 GElf_Ehdr ehdr;
975 GElf_Shdr shdr, opdshdr;
976 Elf_Data *syms, *opddata = NULL;
977 GElf_Sym sym;
978 Elf_Scn *sec, *sec_strndx, *opdsec;
979 Elf *elf;
980 int nr = 0;
981 size_t opdidx = 0;
982
983 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
984 if (elf == NULL) {
985 pr_err("%s: cannot read %s ELF file.\n", __func__, name);
986 goto out_close;
987 }
988
989 if (gelf_getehdr(elf, &ehdr) == NULL) {
990 pr_err("%s: cannot get elf header.\n", __func__);
991 goto out_elf_end;
992 }
993
994 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
995 if (sec == NULL) {
996 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
997 if (sec == NULL)
998 goto out_elf_end;
999 }
1000
1001 opdsec = elf_section_by_name(elf, &ehdr, &opdshdr, ".opd", &opdidx);
1002 if (opdsec)
1003 opddata = elf_rawdata(opdsec, NULL);
1004
1005 syms = elf_getdata(sec, NULL);
1006 if (syms == NULL)
1007 goto out_elf_end;
1008
1009 sec = elf_getscn(elf, shdr.sh_link);
1010 if (sec == NULL)
1011 goto out_elf_end;
1012
1013 symstrs = elf_getdata(sec, NULL);
1014 if (symstrs == NULL)
1015 goto out_elf_end;
1016
1017 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
1018 if (sec_strndx == NULL)
1019 goto out_elf_end;
1020
1021 secstrs = elf_getdata(sec_strndx, NULL);
1022 if (secstrs == NULL)
1023 goto out_elf_end;
1024
1025 nr_syms = shdr.sh_size / shdr.sh_entsize;
1026
1027 memset(&sym, 0, sizeof(sym));
1028 if (self->kernel == DSO_TYPE_USER) {
1029 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
1030 elf_section_by_name(elf, &ehdr, &shdr,
1031 ".gnu.prelink_undo",
1032 NULL) != NULL);
1033 } else self->adjust_symbols = 0;
1034
1035 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
1036 struct symbol *f;
1037 const char *elf_name = elf_sym__name(&sym, symstrs);
1038 char *demangled = NULL;
1039 int is_label = elf_sym__is_label(&sym);
1040 const char *section_name;
1041
1042 if (kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name &&
1043 strcmp(elf_name, kmap->ref_reloc_sym->name) == 0)
1044 kmap->ref_reloc_sym->unrelocated_addr = sym.st_value;
1045
1046 if (!is_label && !elf_sym__is_a(&sym, map->type))
1047 continue;
1048
1049 if (opdsec && sym.st_shndx == opdidx) {
1050 u32 offset = sym.st_value - opdshdr.sh_addr;
1051 u64 *opd = opddata->d_buf + offset;
1052 sym.st_value = *opd;
1053 sym.st_shndx = elf_addr_to_index(elf, sym.st_value);
1054 }
1055
1056 sec = elf_getscn(elf, sym.st_shndx);
1057 if (!sec)
1058 goto out_elf_end;
1059
1060 gelf_getshdr(sec, &shdr);
1061
1062 if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type))
1063 continue;
1064
1065 section_name = elf_sec__name(&shdr, secstrs);
1066
1067 if (self->kernel != DSO_TYPE_USER || kmodule) {
1068 char dso_name[PATH_MAX];
1069
1070 if (strcmp(section_name,
1071 (curr_dso->short_name +
1072 self->short_name_len)) == 0)
1073 goto new_symbol;
1074
1075 if (strcmp(section_name, ".text") == 0) {
1076 curr_map = map;
1077 curr_dso = self;
1078 goto new_symbol;
1079 }
1080
1081 snprintf(dso_name, sizeof(dso_name),
1082 "%s%s", self->short_name, section_name);
1083
1084 curr_map = map_groups__find_by_name(kmap->kmaps, map->type, dso_name);
1085 if (curr_map == NULL) {
1086 u64 start = sym.st_value;
1087
1088 if (kmodule)
1089 start += map->start + shdr.sh_offset;
1090
1091 curr_dso = dso__new(dso_name);
1092 if (curr_dso == NULL)
1093 goto out_elf_end;
1094 curr_dso->kernel = self->kernel;
1095 curr_map = map__new2(start, curr_dso,
1096 map->type);
1097 if (curr_map == NULL) {
1098 dso__delete(curr_dso);
1099 goto out_elf_end;
1100 }
1101 curr_map->map_ip = identity__map_ip;
1102 curr_map->unmap_ip = identity__map_ip;
1103 curr_dso->origin = self->origin;
1104 map_groups__insert(kmap->kmaps, curr_map);
1105 dsos__add(&self->node, curr_dso);
1106 dso__set_loaded(curr_dso, map->type);
1107 } else
1108 curr_dso = curr_map->dso;
1109
1110 goto new_symbol;
1111 }
1112
1113 if (curr_dso->adjust_symbols) {
1114 pr_debug4("%s: adjusting symbol: st_value: %#Lx "
1115 "sh_addr: %#Lx sh_offset: %#Lx\n", __func__,
1116 (u64)sym.st_value, (u64)shdr.sh_addr,
1117 (u64)shdr.sh_offset);
1118 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
1119 }
1120 /*
1121 * We need to figure out if the object was created from C++ sources
1122 * DWARF DW_compile_unit has this, but we don't always have access
1123 * to it...
1124 */
1125 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
1126 if (demangled != NULL)
1127 elf_name = demangled;
1128 new_symbol:
1129 f = symbol__new(sym.st_value, sym.st_size, elf_name);
1130 free(demangled);
1131 if (!f)
1132 goto out_elf_end;
1133
1134 if (filter && filter(curr_map, f))
1135 symbol__delete(f);
1136 else {
1137 symbols__insert(&curr_dso->symbols[curr_map->type], f);
1138 nr++;
1139 }
1140 }
1141
1142 /*
1143 * For misannotated, zeroed, ASM function sizes.
1144 */
1145 if (nr > 0) {
1146 symbols__fixup_end(&self->symbols[map->type]);
1147 if (kmap) {
1148 /*
1149 * We need to fixup this here too because we create new
1150 * maps here, for things like vsyscall sections.
1151 */
1152 __map_groups__fixup_end(kmap->kmaps, map->type);
1153 }
1154 }
1155 err = nr;
1156 out_elf_end:
1157 elf_end(elf);
1158 out_close:
1159 return err;
1160 }
1161
1162 static bool dso__build_id_equal(const struct dso *self, u8 *build_id)
1163 {
1164 return memcmp(self->build_id, build_id, sizeof(self->build_id)) == 0;
1165 }
1166
1167 bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
1168 {
1169 bool have_build_id = false;
1170 struct dso *pos;
1171
1172 list_for_each_entry(pos, head, node) {
1173 if (with_hits && !pos->hit)
1174 continue;
1175 if (pos->has_build_id) {
1176 have_build_id = true;
1177 continue;
1178 }
1179 if (filename__read_build_id(pos->long_name, pos->build_id,
1180 sizeof(pos->build_id)) > 0) {
1181 have_build_id = true;
1182 pos->has_build_id = true;
1183 }
1184 }
1185
1186 return have_build_id;
1187 }
1188
1189 /*
1190 * Align offset to 4 bytes as needed for note name and descriptor data.
1191 */
1192 #define NOTE_ALIGN(n) (((n) + 3) & -4U)
1193
1194 int filename__read_build_id(const char *filename, void *bf, size_t size)
1195 {
1196 int fd, err = -1;
1197 GElf_Ehdr ehdr;
1198 GElf_Shdr shdr;
1199 Elf_Data *data;
1200 Elf_Scn *sec;
1201 Elf_Kind ek;
1202 void *ptr;
1203 Elf *elf;
1204
1205 if (size < BUILD_ID_SIZE)
1206 goto out;
1207
1208 fd = open(filename, O_RDONLY);
1209 if (fd < 0)
1210 goto out;
1211
1212 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1213 if (elf == NULL) {
1214 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
1215 goto out_close;
1216 }
1217
1218 ek = elf_kind(elf);
1219 if (ek != ELF_K_ELF)
1220 goto out_elf_end;
1221
1222 if (gelf_getehdr(elf, &ehdr) == NULL) {
1223 pr_err("%s: cannot get elf header.\n", __func__);
1224 goto out_elf_end;
1225 }
1226
1227 sec = elf_section_by_name(elf, &ehdr, &shdr,
1228 ".note.gnu.build-id", NULL);
1229 if (sec == NULL) {
1230 sec = elf_section_by_name(elf, &ehdr, &shdr,
1231 ".notes", NULL);
1232 if (sec == NULL)
1233 goto out_elf_end;
1234 }
1235
1236 data = elf_getdata(sec, NULL);
1237 if (data == NULL)
1238 goto out_elf_end;
1239
1240 ptr = data->d_buf;
1241 while (ptr < (data->d_buf + data->d_size)) {
1242 GElf_Nhdr *nhdr = ptr;
1243 int namesz = NOTE_ALIGN(nhdr->n_namesz),
1244 descsz = NOTE_ALIGN(nhdr->n_descsz);
1245 const char *name;
1246
1247 ptr += sizeof(*nhdr);
1248 name = ptr;
1249 ptr += namesz;
1250 if (nhdr->n_type == NT_GNU_BUILD_ID &&
1251 nhdr->n_namesz == sizeof("GNU")) {
1252 if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
1253 memcpy(bf, ptr, BUILD_ID_SIZE);
1254 err = BUILD_ID_SIZE;
1255 break;
1256 }
1257 }
1258 ptr += descsz;
1259 }
1260 out_elf_end:
1261 elf_end(elf);
1262 out_close:
1263 close(fd);
1264 out:
1265 return err;
1266 }
1267
1268 int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
1269 {
1270 int fd, err = -1;
1271
1272 if (size < BUILD_ID_SIZE)
1273 goto out;
1274
1275 fd = open(filename, O_RDONLY);
1276 if (fd < 0)
1277 goto out;
1278
1279 while (1) {
1280 char bf[BUFSIZ];
1281 GElf_Nhdr nhdr;
1282 int namesz, descsz;
1283
1284 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
1285 break;
1286
1287 namesz = NOTE_ALIGN(nhdr.n_namesz);
1288 descsz = NOTE_ALIGN(nhdr.n_descsz);
1289 if (nhdr.n_type == NT_GNU_BUILD_ID &&
1290 nhdr.n_namesz == sizeof("GNU")) {
1291 if (read(fd, bf, namesz) != namesz)
1292 break;
1293 if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
1294 if (read(fd, build_id,
1295 BUILD_ID_SIZE) == BUILD_ID_SIZE) {
1296 err = 0;
1297 break;
1298 }
1299 } else if (read(fd, bf, descsz) != descsz)
1300 break;
1301 } else {
1302 int n = namesz + descsz;
1303 if (read(fd, bf, n) != n)
1304 break;
1305 }
1306 }
1307 close(fd);
1308 out:
1309 return err;
1310 }
1311
1312 char dso__symtab_origin(const struct dso *self)
1313 {
1314 static const char origin[] = {
1315 [DSO__ORIG_KERNEL] = 'k',
1316 [DSO__ORIG_JAVA_JIT] = 'j',
1317 [DSO__ORIG_BUILD_ID_CACHE] = 'B',
1318 [DSO__ORIG_FEDORA] = 'f',
1319 [DSO__ORIG_UBUNTU] = 'u',
1320 [DSO__ORIG_BUILDID] = 'b',
1321 [DSO__ORIG_DSO] = 'd',
1322 [DSO__ORIG_KMODULE] = 'K',
1323 [DSO__ORIG_GUEST_KERNEL] = 'g',
1324 [DSO__ORIG_GUEST_KMODULE] = 'G',
1325 };
1326
1327 if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
1328 return '!';
1329 return origin[self->origin];
1330 }
1331
1332 int dso__load(struct dso *self, struct map *map, symbol_filter_t filter)
1333 {
1334 int size = PATH_MAX;
1335 char *name;
1336 u8 build_id[BUILD_ID_SIZE];
1337 int ret = -1;
1338 int fd;
1339 struct machine *machine;
1340 const char *root_dir;
1341
1342 dso__set_loaded(self, map->type);
1343
1344 if (self->kernel == DSO_TYPE_KERNEL)
1345 return dso__load_kernel_sym(self, map, filter);
1346 else if (self->kernel == DSO_TYPE_GUEST_KERNEL)
1347 return dso__load_guest_kernel_sym(self, map, filter);
1348
1349 if (map->groups && map->groups->machine)
1350 machine = map->groups->machine;
1351 else
1352 machine = NULL;
1353
1354 name = malloc(size);
1355 if (!name)
1356 return -1;
1357
1358 self->adjust_symbols = 0;
1359
1360 if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
1361 ret = dso__load_perf_map(self, map, filter);
1362 self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
1363 DSO__ORIG_NOT_FOUND;
1364 return ret;
1365 }
1366
1367 self->origin = DSO__ORIG_BUILD_ID_CACHE;
1368 if (dso__build_id_filename(self, name, size) != NULL)
1369 goto open_file;
1370 more:
1371 do {
1372 self->origin++;
1373 switch (self->origin) {
1374 case DSO__ORIG_FEDORA:
1375 snprintf(name, size, "/usr/lib/debug%s.debug",
1376 self->long_name);
1377 break;
1378 case DSO__ORIG_UBUNTU:
1379 snprintf(name, size, "/usr/lib/debug%s",
1380 self->long_name);
1381 break;
1382 case DSO__ORIG_BUILDID:
1383 if (filename__read_build_id(self->long_name, build_id,
1384 sizeof(build_id))) {
1385 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
1386 build_id__sprintf(build_id, sizeof(build_id),
1387 build_id_hex);
1388 snprintf(name, size,
1389 "/usr/lib/debug/.build-id/%.2s/%s.debug",
1390 build_id_hex, build_id_hex + 2);
1391 if (self->has_build_id)
1392 goto compare_build_id;
1393 break;
1394 }
1395 self->origin++;
1396 /* Fall thru */
1397 case DSO__ORIG_DSO:
1398 snprintf(name, size, "%s", self->long_name);
1399 break;
1400 case DSO__ORIG_GUEST_KMODULE:
1401 if (map->groups && map->groups->machine)
1402 root_dir = map->groups->machine->root_dir;
1403 else
1404 root_dir = "";
1405 snprintf(name, size, "%s%s", root_dir, self->long_name);
1406 break;
1407
1408 default:
1409 goto out;
1410 }
1411
1412 if (self->has_build_id) {
1413 if (filename__read_build_id(name, build_id,
1414 sizeof(build_id)) < 0)
1415 goto more;
1416 compare_build_id:
1417 if (!dso__build_id_equal(self, build_id))
1418 goto more;
1419 }
1420 open_file:
1421 fd = open(name, O_RDONLY);
1422 } while (fd < 0);
1423
1424 ret = dso__load_sym(self, map, name, fd, filter, 0);
1425 close(fd);
1426
1427 /*
1428 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
1429 */
1430 if (!ret)
1431 goto more;
1432
1433 if (ret > 0) {
1434 int nr_plt = dso__synthesize_plt_symbols(self, map, filter);
1435 if (nr_plt > 0)
1436 ret += nr_plt;
1437 }
1438 out:
1439 free(name);
1440 if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
1441 return 0;
1442 return ret;
1443 }
1444
1445 struct map *map_groups__find_by_name(struct map_groups *self,
1446 enum map_type type, const char *name)
1447 {
1448 struct rb_node *nd;
1449
1450 for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) {
1451 struct map *map = rb_entry(nd, struct map, rb_node);
1452
1453 if (map->dso && strcmp(map->dso->short_name, name) == 0)
1454 return map;
1455 }
1456
1457 return NULL;
1458 }
1459
1460 static int dso__kernel_module_get_build_id(struct dso *self,
1461 const char *root_dir)
1462 {
1463 char filename[PATH_MAX];
1464 /*
1465 * kernel module short names are of the form "[module]" and
1466 * we need just "module" here.
1467 */
1468 const char *name = self->short_name + 1;
1469
1470 snprintf(filename, sizeof(filename),
1471 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1472 root_dir, (int)strlen(name) - 1, name);
1473
1474 if (sysfs__read_build_id(filename, self->build_id,
1475 sizeof(self->build_id)) == 0)
1476 self->has_build_id = true;
1477
1478 return 0;
1479 }
1480
1481 static int map_groups__set_modules_path_dir(struct map_groups *self,
1482 const char *dir_name)
1483 {
1484 struct dirent *dent;
1485 DIR *dir = opendir(dir_name);
1486
1487 if (!dir) {
1488 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
1489 return -1;
1490 }
1491
1492 while ((dent = readdir(dir)) != NULL) {
1493 char path[PATH_MAX];
1494 struct stat st;
1495
1496 /*sshfs might return bad dent->d_type, so we have to stat*/
1497 sprintf(path, "%s/%s", dir_name, dent->d_name);
1498 if (stat(path, &st))
1499 continue;
1500
1501 if (S_ISDIR(st.st_mode)) {
1502 if (!strcmp(dent->d_name, ".") ||
1503 !strcmp(dent->d_name, ".."))
1504 continue;
1505
1506 snprintf(path, sizeof(path), "%s/%s",
1507 dir_name, dent->d_name);
1508 if (map_groups__set_modules_path_dir(self, path) < 0)
1509 goto failure;
1510 } else {
1511 char *dot = strrchr(dent->d_name, '.'),
1512 dso_name[PATH_MAX];
1513 struct map *map;
1514 char *long_name;
1515
1516 if (dot == NULL || strcmp(dot, ".ko"))
1517 continue;
1518 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1519 (int)(dot - dent->d_name), dent->d_name);
1520
1521 strxfrchar(dso_name, '-', '_');
1522 map = map_groups__find_by_name(self, MAP__FUNCTION, dso_name);
1523 if (map == NULL)
1524 continue;
1525
1526 snprintf(path, sizeof(path), "%s/%s",
1527 dir_name, dent->d_name);
1528
1529 long_name = strdup(path);
1530 if (long_name == NULL)
1531 goto failure;
1532 dso__set_long_name(map->dso, long_name);
1533 dso__kernel_module_get_build_id(map->dso, "");
1534 }
1535 }
1536
1537 return 0;
1538 failure:
1539 closedir(dir);
1540 return -1;
1541 }
1542
1543 static char *get_kernel_version(const char *root_dir)
1544 {
1545 char version[PATH_MAX];
1546 FILE *file;
1547 char *name, *tmp;
1548 const char *prefix = "Linux version ";
1549
1550 sprintf(version, "%s/proc/version", root_dir);
1551 file = fopen(version, "r");
1552 if (!file)
1553 return NULL;
1554
1555 version[0] = '\0';
1556 tmp = fgets(version, sizeof(version), file);
1557 fclose(file);
1558
1559 name = strstr(version, prefix);
1560 if (!name)
1561 return NULL;
1562 name += strlen(prefix);
1563 tmp = strchr(name, ' ');
1564 if (tmp)
1565 *tmp = '\0';
1566
1567 return strdup(name);
1568 }
1569
1570 static int machine__set_modules_path(struct machine *self)
1571 {
1572 char *version;
1573 char modules_path[PATH_MAX];
1574
1575 version = get_kernel_version(self->root_dir);
1576 if (!version)
1577 return -1;
1578
1579 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s/kernel",
1580 self->root_dir, version);
1581 free(version);
1582
1583 return map_groups__set_modules_path_dir(&self->kmaps, modules_path);
1584 }
1585
1586 /*
1587 * Constructor variant for modules (where we know from /proc/modules where
1588 * they are loaded) and for vmlinux, where only after we load all the
1589 * symbols we'll know where it starts and ends.
1590 */
1591 static struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
1592 {
1593 struct map *self = calloc(1, (sizeof(*self) +
1594 (dso->kernel ? sizeof(struct kmap) : 0)));
1595 if (self != NULL) {
1596 /*
1597 * ->end will be filled after we load all the symbols
1598 */
1599 map__init(self, type, start, 0, 0, dso);
1600 }
1601
1602 return self;
1603 }
1604
1605 struct map *machine__new_module(struct machine *self, u64 start,
1606 const char *filename)
1607 {
1608 struct map *map;
1609 struct dso *dso = __dsos__findnew(&self->kernel_dsos, filename);
1610
1611 if (dso == NULL)
1612 return NULL;
1613
1614 map = map__new2(start, dso, MAP__FUNCTION);
1615 if (map == NULL)
1616 return NULL;
1617
1618 if (machine__is_host(self))
1619 dso->origin = DSO__ORIG_KMODULE;
1620 else
1621 dso->origin = DSO__ORIG_GUEST_KMODULE;
1622 map_groups__insert(&self->kmaps, map);
1623 return map;
1624 }
1625
1626 static int machine__create_modules(struct machine *self)
1627 {
1628 char *line = NULL;
1629 size_t n;
1630 FILE *file;
1631 struct map *map;
1632 const char *modules;
1633 char path[PATH_MAX];
1634
1635 if (machine__is_default_guest(self))
1636 modules = symbol_conf.default_guest_modules;
1637 else {
1638 sprintf(path, "%s/proc/modules", self->root_dir);
1639 modules = path;
1640 }
1641
1642 file = fopen(modules, "r");
1643 if (file == NULL)
1644 return -1;
1645
1646 while (!feof(file)) {
1647 char name[PATH_MAX];
1648 u64 start;
1649 char *sep;
1650 int line_len;
1651
1652 line_len = getline(&line, &n, file);
1653 if (line_len < 0)
1654 break;
1655
1656 if (!line)
1657 goto out_failure;
1658
1659 line[--line_len] = '\0'; /* \n */
1660
1661 sep = strrchr(line, 'x');
1662 if (sep == NULL)
1663 continue;
1664
1665 hex2u64(sep + 1, &start);
1666
1667 sep = strchr(line, ' ');
1668 if (sep == NULL)
1669 continue;
1670
1671 *sep = '\0';
1672
1673 snprintf(name, sizeof(name), "[%s]", line);
1674 map = machine__new_module(self, start, name);
1675 if (map == NULL)
1676 goto out_delete_line;
1677 dso__kernel_module_get_build_id(map->dso, self->root_dir);
1678 }
1679
1680 free(line);
1681 fclose(file);
1682
1683 return machine__set_modules_path(self);
1684
1685 out_delete_line:
1686 free(line);
1687 out_failure:
1688 return -1;
1689 }
1690
1691 static int dso__load_vmlinux(struct dso *self, struct map *map,
1692 const char *vmlinux, symbol_filter_t filter)
1693 {
1694 int err = -1, fd;
1695
1696 if (self->has_build_id) {
1697 u8 build_id[BUILD_ID_SIZE];
1698
1699 if (filename__read_build_id(vmlinux, build_id,
1700 sizeof(build_id)) < 0) {
1701 pr_debug("No build_id in %s, ignoring it\n", vmlinux);
1702 return -1;
1703 }
1704 if (!dso__build_id_equal(self, build_id)) {
1705 char expected_build_id[BUILD_ID_SIZE * 2 + 1],
1706 vmlinux_build_id[BUILD_ID_SIZE * 2 + 1];
1707
1708 build_id__sprintf(self->build_id,
1709 sizeof(self->build_id),
1710 expected_build_id);
1711 build_id__sprintf(build_id, sizeof(build_id),
1712 vmlinux_build_id);
1713 pr_debug("build_id in %s is %s while expected is %s, "
1714 "ignoring it\n", vmlinux, vmlinux_build_id,
1715 expected_build_id);
1716 return -1;
1717 }
1718 }
1719
1720 fd = open(vmlinux, O_RDONLY);
1721 if (fd < 0)
1722 return -1;
1723
1724 dso__set_loaded(self, map->type);
1725 err = dso__load_sym(self, map, vmlinux, fd, filter, 0);
1726 close(fd);
1727
1728 if (err > 0)
1729 pr_debug("Using %s for symbols\n", vmlinux);
1730
1731 return err;
1732 }
1733
1734 int dso__load_vmlinux_path(struct dso *self, struct map *map,
1735 symbol_filter_t filter)
1736 {
1737 int i, err = 0;
1738 char *filename;
1739
1740 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1741 vmlinux_path__nr_entries + 1);
1742
1743 filename = dso__build_id_filename(self, NULL, 0);
1744 if (filename != NULL) {
1745 err = dso__load_vmlinux(self, map, filename, filter);
1746 if (err > 0) {
1747 dso__set_long_name(self, filename);
1748 goto out;
1749 }
1750 free(filename);
1751 }
1752
1753 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
1754 err = dso__load_vmlinux(self, map, vmlinux_path[i], filter);
1755 if (err > 0) {
1756 dso__set_long_name(self, strdup(vmlinux_path[i]));
1757 break;
1758 }
1759 }
1760 out:
1761 return err;
1762 }
1763
1764 static int dso__load_kernel_sym(struct dso *self, struct map *map,
1765 symbol_filter_t filter)
1766 {
1767 int err;
1768 const char *kallsyms_filename = NULL;
1769 char *kallsyms_allocated_filename = NULL;
1770 /*
1771 * Step 1: if the user specified a vmlinux filename, use it and only
1772 * it, reporting errors to the user if it cannot be used.
1773 *
1774 * For instance, try to analyse an ARM perf.data file _without_ a
1775 * build-id, or if the user specifies the wrong path to the right
1776 * vmlinux file, obviously we can't fallback to another vmlinux (a
1777 * x86_86 one, on the machine where analysis is being performed, say),
1778 * or worse, /proc/kallsyms.
1779 *
1780 * If the specified file _has_ a build-id and there is a build-id
1781 * section in the perf.data file, we will still do the expected
1782 * validation in dso__load_vmlinux and will bail out if they don't
1783 * match.
1784 */
1785 if (symbol_conf.vmlinux_name != NULL) {
1786 err = dso__load_vmlinux(self, map,
1787 symbol_conf.vmlinux_name, filter);
1788 if (err > 0) {
1789 dso__set_long_name(self,
1790 strdup(symbol_conf.vmlinux_name));
1791 goto out_fixup;
1792 }
1793 return err;
1794 }
1795
1796 if (vmlinux_path != NULL) {
1797 err = dso__load_vmlinux_path(self, map, filter);
1798 if (err > 0)
1799 goto out_fixup;
1800 }
1801
1802 /*
1803 * Say the kernel DSO was created when processing the build-id header table,
1804 * we have a build-id, so check if it is the same as the running kernel,
1805 * using it if it is.
1806 */
1807 if (self->has_build_id) {
1808 u8 kallsyms_build_id[BUILD_ID_SIZE];
1809 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1810
1811 if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id,
1812 sizeof(kallsyms_build_id)) == 0) {
1813 if (dso__build_id_equal(self, kallsyms_build_id)) {
1814 kallsyms_filename = "/proc/kallsyms";
1815 goto do_kallsyms;
1816 }
1817 }
1818 /*
1819 * Now look if we have it on the build-id cache in
1820 * $HOME/.debug/[kernel.kallsyms].
1821 */
1822 build_id__sprintf(self->build_id, sizeof(self->build_id),
1823 sbuild_id);
1824
1825 if (asprintf(&kallsyms_allocated_filename,
1826 "%s/.debug/[kernel.kallsyms]/%s",
1827 getenv("HOME"), sbuild_id) == -1) {
1828 pr_err("Not enough memory for kallsyms file lookup\n");
1829 return -1;
1830 }
1831
1832 kallsyms_filename = kallsyms_allocated_filename;
1833
1834 if (access(kallsyms_filename, F_OK)) {
1835 pr_err("No kallsyms or vmlinux with build-id %s "
1836 "was found\n", sbuild_id);
1837 free(kallsyms_allocated_filename);
1838 return -1;
1839 }
1840 } else {
1841 /*
1842 * Last resort, if we don't have a build-id and couldn't find
1843 * any vmlinux file, try the running kernel kallsyms table.
1844 */
1845 kallsyms_filename = "/proc/kallsyms";
1846 }
1847
1848 do_kallsyms:
1849 err = dso__load_kallsyms(self, kallsyms_filename, map, filter);
1850 if (err > 0)
1851 pr_debug("Using %s for symbols\n", kallsyms_filename);
1852 free(kallsyms_allocated_filename);
1853
1854 if (err > 0) {
1855 out_fixup:
1856 if (kallsyms_filename != NULL)
1857 dso__set_long_name(self, strdup("[kernel.kallsyms]"));
1858 map__fixup_start(map);
1859 map__fixup_end(map);
1860 }
1861
1862 return err;
1863 }
1864
1865 static int dso__load_guest_kernel_sym(struct dso *self, struct map *map,
1866 symbol_filter_t filter)
1867 {
1868 int err;
1869 const char *kallsyms_filename = NULL;
1870 struct machine *machine;
1871 char path[PATH_MAX];
1872
1873 if (!map->groups) {
1874 pr_debug("Guest kernel map hasn't the point to groups\n");
1875 return -1;
1876 }
1877 machine = map->groups->machine;
1878
1879 if (machine__is_default_guest(machine)) {
1880 /*
1881 * if the user specified a vmlinux filename, use it and only
1882 * it, reporting errors to the user if it cannot be used.
1883 * Or use file guest_kallsyms inputted by user on commandline
1884 */
1885 if (symbol_conf.default_guest_vmlinux_name != NULL) {
1886 err = dso__load_vmlinux(self, map,
1887 symbol_conf.default_guest_vmlinux_name, filter);
1888 goto out_try_fixup;
1889 }
1890
1891 kallsyms_filename = symbol_conf.default_guest_kallsyms;
1892 if (!kallsyms_filename)
1893 return -1;
1894 } else {
1895 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
1896 kallsyms_filename = path;
1897 }
1898
1899 err = dso__load_kallsyms(self, kallsyms_filename, map, filter);
1900 if (err > 0)
1901 pr_debug("Using %s for symbols\n", kallsyms_filename);
1902
1903 out_try_fixup:
1904 if (err > 0) {
1905 if (kallsyms_filename != NULL) {
1906 machine__mmap_name(machine, path, sizeof(path));
1907 dso__set_long_name(self, strdup(path));
1908 }
1909 map__fixup_start(map);
1910 map__fixup_end(map);
1911 }
1912
1913 return err;
1914 }
1915
1916 static void dsos__add(struct list_head *head, struct dso *dso)
1917 {
1918 list_add_tail(&dso->node, head);
1919 }
1920
1921 static struct dso *dsos__find(struct list_head *head, const char *name)
1922 {
1923 struct dso *pos;
1924
1925 list_for_each_entry(pos, head, node)
1926 if (strcmp(pos->long_name, name) == 0)
1927 return pos;
1928 return NULL;
1929 }
1930
1931 struct dso *__dsos__findnew(struct list_head *head, const char *name)
1932 {
1933 struct dso *dso = dsos__find(head, name);
1934
1935 if (!dso) {
1936 dso = dso__new(name);
1937 if (dso != NULL) {
1938 dsos__add(head, dso);
1939 dso__set_basename(dso);
1940 }
1941 }
1942
1943 return dso;
1944 }
1945
1946 size_t __dsos__fprintf(struct list_head *head, FILE *fp)
1947 {
1948 struct dso *pos;
1949 size_t ret = 0;
1950
1951 list_for_each_entry(pos, head, node) {
1952 int i;
1953 for (i = 0; i < MAP__NR_TYPES; ++i)
1954 ret += dso__fprintf(pos, i, fp);
1955 }
1956
1957 return ret;
1958 }
1959
1960 size_t machines__fprintf_dsos(struct rb_root *self, FILE *fp)
1961 {
1962 struct rb_node *nd;
1963 size_t ret = 0;
1964
1965 for (nd = rb_first(self); nd; nd = rb_next(nd)) {
1966 struct machine *pos = rb_entry(nd, struct machine, rb_node);
1967 ret += __dsos__fprintf(&pos->kernel_dsos, fp);
1968 ret += __dsos__fprintf(&pos->user_dsos, fp);
1969 }
1970
1971 return ret;
1972 }
1973
1974 static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
1975 bool with_hits)
1976 {
1977 struct dso *pos;
1978 size_t ret = 0;
1979
1980 list_for_each_entry(pos, head, node) {
1981 if (with_hits && !pos->hit)
1982 continue;
1983 ret += dso__fprintf_buildid(pos, fp);
1984 ret += fprintf(fp, " %s\n", pos->long_name);
1985 }
1986 return ret;
1987 }
1988
1989 size_t machine__fprintf_dsos_buildid(struct machine *self, FILE *fp, bool with_hits)
1990 {
1991 return __dsos__fprintf_buildid(&self->kernel_dsos, fp, with_hits) +
1992 __dsos__fprintf_buildid(&self->user_dsos, fp, with_hits);
1993 }
1994
1995 size_t machines__fprintf_dsos_buildid(struct rb_root *self, FILE *fp, bool with_hits)
1996 {
1997 struct rb_node *nd;
1998 size_t ret = 0;
1999
2000 for (nd = rb_first(self); nd; nd = rb_next(nd)) {
2001 struct machine *pos = rb_entry(nd, struct machine, rb_node);
2002 ret += machine__fprintf_dsos_buildid(pos, fp, with_hits);
2003 }
2004 return ret;
2005 }
2006
2007 struct dso *dso__new_kernel(const char *name)
2008 {
2009 struct dso *self = dso__new(name ?: "[kernel.kallsyms]");
2010
2011 if (self != NULL) {
2012 dso__set_short_name(self, "[kernel]");
2013 self->kernel = DSO_TYPE_KERNEL;
2014 }
2015
2016 return self;
2017 }
2018
2019 static struct dso *dso__new_guest_kernel(struct machine *machine,
2020 const char *name)
2021 {
2022 char bf[PATH_MAX];
2023 struct dso *self = dso__new(name ?: machine__mmap_name(machine, bf, sizeof(bf)));
2024
2025 if (self != NULL) {
2026 dso__set_short_name(self, "[guest.kernel]");
2027 self->kernel = DSO_TYPE_GUEST_KERNEL;
2028 }
2029
2030 return self;
2031 }
2032
2033 void dso__read_running_kernel_build_id(struct dso *self, struct machine *machine)
2034 {
2035 char path[PATH_MAX];
2036
2037 if (machine__is_default_guest(machine))
2038 return;
2039 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
2040 if (sysfs__read_build_id(path, self->build_id,
2041 sizeof(self->build_id)) == 0)
2042 self->has_build_id = true;
2043 }
2044
2045 static struct dso *machine__create_kernel(struct machine *self)
2046 {
2047 const char *vmlinux_name = NULL;
2048 struct dso *kernel;
2049
2050 if (machine__is_host(self)) {
2051 vmlinux_name = symbol_conf.vmlinux_name;
2052 kernel = dso__new_kernel(vmlinux_name);
2053 } else {
2054 if (machine__is_default_guest(self))
2055 vmlinux_name = symbol_conf.default_guest_vmlinux_name;
2056 kernel = dso__new_guest_kernel(self, vmlinux_name);
2057 }
2058
2059 if (kernel != NULL) {
2060 dso__read_running_kernel_build_id(kernel, self);
2061 dsos__add(&self->kernel_dsos, kernel);
2062 }
2063 return kernel;
2064 }
2065
2066 int __machine__create_kernel_maps(struct machine *self, struct dso *kernel)
2067 {
2068 enum map_type type;
2069
2070 for (type = 0; type < MAP__NR_TYPES; ++type) {
2071 struct kmap *kmap;
2072
2073 self->vmlinux_maps[type] = map__new2(0, kernel, type);
2074 if (self->vmlinux_maps[type] == NULL)
2075 return -1;
2076
2077 self->vmlinux_maps[type]->map_ip =
2078 self->vmlinux_maps[type]->unmap_ip = identity__map_ip;
2079
2080 kmap = map__kmap(self->vmlinux_maps[type]);
2081 kmap->kmaps = &self->kmaps;
2082 map_groups__insert(&self->kmaps, self->vmlinux_maps[type]);
2083 }
2084
2085 return 0;
2086 }
2087
2088 int machine__create_kernel_maps(struct machine *self)
2089 {
2090 struct dso *kernel = machine__create_kernel(self);
2091
2092 if (kernel == NULL ||
2093 __machine__create_kernel_maps(self, kernel) < 0)
2094 return -1;
2095
2096 if (symbol_conf.use_modules && machine__create_modules(self) < 0)
2097 pr_debug("Problems creating module maps, continuing anyway...\n");
2098 /*
2099 * Now that we have all the maps created, just set the ->end of them:
2100 */
2101 map_groups__fixup_end(&self->kmaps);
2102 return 0;
2103 }
2104
2105 static void vmlinux_path__exit(void)
2106 {
2107 while (--vmlinux_path__nr_entries >= 0) {
2108 free(vmlinux_path[vmlinux_path__nr_entries]);
2109 vmlinux_path[vmlinux_path__nr_entries] = NULL;
2110 }
2111
2112 free(vmlinux_path);
2113 vmlinux_path = NULL;
2114 }
2115
2116 static int vmlinux_path__init(void)
2117 {
2118 struct utsname uts;
2119 char bf[PATH_MAX];
2120
2121 if (uname(&uts) < 0)
2122 return -1;
2123
2124 vmlinux_path = malloc(sizeof(char *) * 5);
2125 if (vmlinux_path == NULL)
2126 return -1;
2127
2128 vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
2129 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2130 goto out_fail;
2131 ++vmlinux_path__nr_entries;
2132 vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
2133 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2134 goto out_fail;
2135 ++vmlinux_path__nr_entries;
2136 snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
2137 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
2138 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2139 goto out_fail;
2140 ++vmlinux_path__nr_entries;
2141 snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
2142 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
2143 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2144 goto out_fail;
2145 ++vmlinux_path__nr_entries;
2146 snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
2147 uts.release);
2148 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
2149 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2150 goto out_fail;
2151 ++vmlinux_path__nr_entries;
2152
2153 return 0;
2154
2155 out_fail:
2156 vmlinux_path__exit();
2157 return -1;
2158 }
2159
2160 size_t machine__fprintf_vmlinux_path(struct machine *self, FILE *fp)
2161 {
2162 int i;
2163 size_t printed = 0;
2164 struct dso *kdso = self->vmlinux_maps[MAP__FUNCTION]->dso;
2165
2166 if (kdso->has_build_id) {
2167 char filename[PATH_MAX];
2168 if (dso__build_id_filename(kdso, filename, sizeof(filename)))
2169 printed += fprintf(fp, "[0] %s\n", filename);
2170 }
2171
2172 for (i = 0; i < vmlinux_path__nr_entries; ++i)
2173 printed += fprintf(fp, "[%d] %s\n",
2174 i + kdso->has_build_id, vmlinux_path[i]);
2175
2176 return printed;
2177 }
2178
2179 static int setup_list(struct strlist **list, const char *list_str,
2180 const char *list_name)
2181 {
2182 if (list_str == NULL)
2183 return 0;
2184
2185 *list = strlist__new(true, list_str);
2186 if (!*list) {
2187 pr_err("problems parsing %s list\n", list_name);
2188 return -1;
2189 }
2190 return 0;
2191 }
2192
2193 int symbol__init(void)
2194 {
2195 elf_version(EV_CURRENT);
2196 if (symbol_conf.sort_by_name)
2197 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
2198 sizeof(struct symbol));
2199
2200 if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
2201 return -1;
2202
2203 if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
2204 pr_err("'.' is the only non valid --field-separator argument\n");
2205 return -1;
2206 }
2207
2208 if (setup_list(&symbol_conf.dso_list,
2209 symbol_conf.dso_list_str, "dso") < 0)
2210 return -1;
2211
2212 if (setup_list(&symbol_conf.comm_list,
2213 symbol_conf.comm_list_str, "comm") < 0)
2214 goto out_free_dso_list;
2215
2216 if (setup_list(&symbol_conf.sym_list,
2217 symbol_conf.sym_list_str, "symbol") < 0)
2218 goto out_free_comm_list;
2219
2220 return 0;
2221
2222 out_free_dso_list:
2223 strlist__delete(symbol_conf.dso_list);
2224 out_free_comm_list:
2225 strlist__delete(symbol_conf.comm_list);
2226 return -1;
2227 }
2228
2229 int machines__create_kernel_maps(struct rb_root *self, pid_t pid)
2230 {
2231 struct machine *machine = machines__findnew(self, pid);
2232
2233 if (machine == NULL)
2234 return -1;
2235
2236 return machine__create_kernel_maps(machine);
2237 }
2238
2239 static int hex(char ch)
2240 {
2241 if ((ch >= '0') && (ch <= '9'))
2242 return ch - '0';
2243 if ((ch >= 'a') && (ch <= 'f'))
2244 return ch - 'a' + 10;
2245 if ((ch >= 'A') && (ch <= 'F'))
2246 return ch - 'A' + 10;
2247 return -1;
2248 }
2249
2250 /*
2251 * While we find nice hex chars, build a long_val.
2252 * Return number of chars processed.
2253 */
2254 int hex2u64(const char *ptr, u64 *long_val)
2255 {
2256 const char *p = ptr;
2257 *long_val = 0;
2258
2259 while (*p) {
2260 const int hex_val = hex(*p);
2261
2262 if (hex_val < 0)
2263 break;
2264
2265 *long_val = (*long_val << 4) | hex_val;
2266 p++;
2267 }
2268
2269 return p - ptr;
2270 }
2271
2272 char *strxfrchar(char *s, char from, char to)
2273 {
2274 char *p = s;
2275
2276 while ((p = strchr(p, from)) != NULL)
2277 *p++ = to;
2278
2279 return s;
2280 }
2281
2282 int machines__create_guest_kernel_maps(struct rb_root *self)
2283 {
2284 int ret = 0;
2285 struct dirent **namelist = NULL;
2286 int i, items = 0;
2287 char path[PATH_MAX];
2288 pid_t pid;
2289
2290 if (symbol_conf.default_guest_vmlinux_name ||
2291 symbol_conf.default_guest_modules ||
2292 symbol_conf.default_guest_kallsyms) {
2293 machines__create_kernel_maps(self, DEFAULT_GUEST_KERNEL_ID);
2294 }
2295
2296 if (symbol_conf.guestmount) {
2297 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
2298 if (items <= 0)
2299 return -ENOENT;
2300 for (i = 0; i < items; i++) {
2301 if (!isdigit(namelist[i]->d_name[0])) {
2302 /* Filter out . and .. */
2303 continue;
2304 }
2305 pid = atoi(namelist[i]->d_name);
2306 sprintf(path, "%s/%s/proc/kallsyms",
2307 symbol_conf.guestmount,
2308 namelist[i]->d_name);
2309 ret = access(path, R_OK);
2310 if (ret) {
2311 pr_debug("Can't access file %s\n", path);
2312 goto failure;
2313 }
2314 machines__create_kernel_maps(self, pid);
2315 }
2316 failure:
2317 free(namelist);
2318 }
2319
2320 return ret;
2321 }
2322
2323 int machine__load_kallsyms(struct machine *self, const char *filename,
2324 enum map_type type, symbol_filter_t filter)
2325 {
2326 struct map *map = self->vmlinux_maps[type];
2327 int ret = dso__load_kallsyms(map->dso, filename, map, filter);
2328
2329 if (ret > 0) {
2330 dso__set_loaded(map->dso, type);
2331 /*
2332 * Since /proc/kallsyms will have multiple sessions for the
2333 * kernel, with modules between them, fixup the end of all
2334 * sections.
2335 */
2336 __map_groups__fixup_end(&self->kmaps, type);
2337 }
2338
2339 return ret;
2340 }
2341
2342 int machine__load_vmlinux_path(struct machine *self, enum map_type type,
2343 symbol_filter_t filter)
2344 {
2345 struct map *map = self->vmlinux_maps[type];
2346 int ret = dso__load_vmlinux_path(map->dso, map, filter);
2347
2348 if (ret > 0) {
2349 dso__set_loaded(map->dso, type);
2350 map__reloc_vmlinux(map);
2351 }
2352
2353 return ret;
2354 }
This page took 0.121612 seconds and 5 git commands to generate.