perf symbols: Correct comment wrt kallsyms loading
[linux-2.6.git] / tools / perf / util / symbol.c
blob9f181a86f3b2032be85cedafcf6b54c67983c793
1 #include <dirent.h>
2 #include <errno.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <sys/param.h>
9 #include <fcntl.h>
10 #include <unistd.h>
11 #include <inttypes.h>
12 #include "build-id.h"
13 #include "util.h"
14 #include "debug.h"
15 #include "symbol.h"
16 #include "strlist.h"
18 #include <elf.h>
19 #include <limits.h>
20 #include <sys/utsname.h>
22 #ifndef KSYM_NAME_LEN
23 #define KSYM_NAME_LEN 256
24 #endif
26 static void dso_cache__free(struct rb_root *root);
27 static int dso__load_kernel_sym(struct dso *dso, struct map *map,
28 symbol_filter_t filter);
29 static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
30 symbol_filter_t filter);
31 static int vmlinux_path__nr_entries;
32 static char **vmlinux_path;
34 struct symbol_conf symbol_conf = {
35 .exclude_other = true,
36 .use_modules = true,
37 .try_vmlinux_path = true,
38 .annotate_src = true,
39 .symfs = "",
42 static enum dso_binary_type binary_type_symtab[] = {
43 DSO_BINARY_TYPE__KALLSYMS,
44 DSO_BINARY_TYPE__GUEST_KALLSYMS,
45 DSO_BINARY_TYPE__JAVA_JIT,
46 DSO_BINARY_TYPE__DEBUGLINK,
47 DSO_BINARY_TYPE__BUILD_ID_CACHE,
48 DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
49 DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
50 DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
51 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
52 DSO_BINARY_TYPE__GUEST_KMODULE,
53 DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE,
54 DSO_BINARY_TYPE__NOT_FOUND,
57 #define DSO_BINARY_TYPE__SYMTAB_CNT ARRAY_SIZE(binary_type_symtab)
59 static enum dso_binary_type binary_type_data[] = {
60 DSO_BINARY_TYPE__BUILD_ID_CACHE,
61 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
62 DSO_BINARY_TYPE__NOT_FOUND,
65 #define DSO_BINARY_TYPE__DATA_CNT ARRAY_SIZE(binary_type_data)
67 int dso__name_len(const struct dso *dso)
69 if (!dso)
70 return strlen("[unknown]");
71 if (verbose)
72 return dso->long_name_len;
74 return dso->short_name_len;
77 bool dso__loaded(const struct dso *dso, enum map_type type)
79 return dso->loaded & (1 << type);
82 bool dso__sorted_by_name(const struct dso *dso, enum map_type type)
84 return dso->sorted_by_name & (1 << type);
87 static void dso__set_sorted_by_name(struct dso *dso, enum map_type type)
89 dso->sorted_by_name |= (1 << type);
92 bool symbol_type__is_a(char symbol_type, enum map_type map_type)
94 symbol_type = toupper(symbol_type);
96 switch (map_type) {
97 case MAP__FUNCTION:
98 return symbol_type == 'T' || symbol_type == 'W';
99 case MAP__VARIABLE:
100 return symbol_type == 'D';
101 default:
102 return false;
106 static int prefix_underscores_count(const char *str)
108 const char *tail = str;
110 while (*tail == '_')
111 tail++;
113 return tail - str;
116 #define SYMBOL_A 0
117 #define SYMBOL_B 1
119 static int choose_best_symbol(struct symbol *syma, struct symbol *symb)
121 s64 a;
122 s64 b;
124 /* Prefer a symbol with non zero length */
125 a = syma->end - syma->start;
126 b = symb->end - symb->start;
127 if ((b == 0) && (a > 0))
128 return SYMBOL_A;
129 else if ((a == 0) && (b > 0))
130 return SYMBOL_B;
132 /* Prefer a non weak symbol over a weak one */
133 a = syma->binding == STB_WEAK;
134 b = symb->binding == STB_WEAK;
135 if (b && !a)
136 return SYMBOL_A;
137 if (a && !b)
138 return SYMBOL_B;
140 /* Prefer a global symbol over a non global one */
141 a = syma->binding == STB_GLOBAL;
142 b = symb->binding == STB_GLOBAL;
143 if (a && !b)
144 return SYMBOL_A;
145 if (b && !a)
146 return SYMBOL_B;
148 /* Prefer a symbol with less underscores */
149 a = prefix_underscores_count(syma->name);
150 b = prefix_underscores_count(symb->name);
151 if (b > a)
152 return SYMBOL_A;
153 else if (a > b)
154 return SYMBOL_B;
156 /* If all else fails, choose the symbol with the longest name */
157 if (strlen(syma->name) >= strlen(symb->name))
158 return SYMBOL_A;
159 else
160 return SYMBOL_B;
163 void symbols__fixup_duplicate(struct rb_root *symbols)
165 struct rb_node *nd;
166 struct symbol *curr, *next;
168 nd = rb_first(symbols);
170 while (nd) {
171 curr = rb_entry(nd, struct symbol, rb_node);
172 again:
173 nd = rb_next(&curr->rb_node);
174 next = rb_entry(nd, struct symbol, rb_node);
176 if (!nd)
177 break;
179 if (curr->start != next->start)
180 continue;
182 if (choose_best_symbol(curr, next) == SYMBOL_A) {
183 rb_erase(&next->rb_node, symbols);
184 goto again;
185 } else {
186 nd = rb_next(&curr->rb_node);
187 rb_erase(&curr->rb_node, symbols);
192 void symbols__fixup_end(struct rb_root *symbols)
194 struct rb_node *nd, *prevnd = rb_first(symbols);
195 struct symbol *curr, *prev;
197 if (prevnd == NULL)
198 return;
200 curr = rb_entry(prevnd, struct symbol, rb_node);
202 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
203 prev = curr;
204 curr = rb_entry(nd, struct symbol, rb_node);
206 if (prev->end == prev->start && prev->end != curr->start)
207 prev->end = curr->start - 1;
210 /* Last entry */
211 if (curr->end == curr->start)
212 curr->end = roundup(curr->start, 4096);
215 void __map_groups__fixup_end(struct map_groups *mg, enum map_type type)
217 struct map *prev, *curr;
218 struct rb_node *nd, *prevnd = rb_first(&mg->maps[type]);
220 if (prevnd == NULL)
221 return;
223 curr = rb_entry(prevnd, struct map, rb_node);
225 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
226 prev = curr;
227 curr = rb_entry(nd, struct map, rb_node);
228 prev->end = curr->start - 1;
232 * We still haven't the actual symbols, so guess the
233 * last map final address.
235 curr->end = ~0ULL;
238 static void map_groups__fixup_end(struct map_groups *mg)
240 int i;
241 for (i = 0; i < MAP__NR_TYPES; ++i)
242 __map_groups__fixup_end(mg, i);
245 struct symbol *symbol__new(u64 start, u64 len, u8 binding, const char *name)
247 size_t namelen = strlen(name) + 1;
248 struct symbol *sym = calloc(1, (symbol_conf.priv_size +
249 sizeof(*sym) + namelen));
250 if (sym == NULL)
251 return NULL;
253 if (symbol_conf.priv_size)
254 sym = ((void *)sym) + symbol_conf.priv_size;
256 sym->start = start;
257 sym->end = len ? start + len - 1 : start;
258 sym->binding = binding;
259 sym->namelen = namelen - 1;
261 pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n",
262 __func__, name, start, sym->end);
263 memcpy(sym->name, name, namelen);
265 return sym;
268 void symbol__delete(struct symbol *sym)
270 free(((void *)sym) - symbol_conf.priv_size);
273 static size_t symbol__fprintf(struct symbol *sym, FILE *fp)
275 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %c %s\n",
276 sym->start, sym->end,
277 sym->binding == STB_GLOBAL ? 'g' :
278 sym->binding == STB_LOCAL ? 'l' : 'w',
279 sym->name);
282 size_t symbol__fprintf_symname_offs(const struct symbol *sym,
283 const struct addr_location *al, FILE *fp)
285 unsigned long offset;
286 size_t length;
288 if (sym && sym->name) {
289 length = fprintf(fp, "%s", sym->name);
290 if (al) {
291 offset = al->addr - sym->start;
292 length += fprintf(fp, "+0x%lx", offset);
294 return length;
295 } else
296 return fprintf(fp, "[unknown]");
299 size_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp)
301 return symbol__fprintf_symname_offs(sym, NULL, fp);
304 void dso__set_long_name(struct dso *dso, char *name)
306 if (name == NULL)
307 return;
308 dso->long_name = name;
309 dso->long_name_len = strlen(name);
312 static void dso__set_short_name(struct dso *dso, const char *name)
314 if (name == NULL)
315 return;
316 dso->short_name = name;
317 dso->short_name_len = strlen(name);
320 static void dso__set_basename(struct dso *dso)
322 dso__set_short_name(dso, basename(dso->long_name));
325 struct dso *dso__new(const char *name)
327 struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
329 if (dso != NULL) {
330 int i;
331 strcpy(dso->name, name);
332 dso__set_long_name(dso, dso->name);
333 dso__set_short_name(dso, dso->name);
334 for (i = 0; i < MAP__NR_TYPES; ++i)
335 dso->symbols[i] = dso->symbol_names[i] = RB_ROOT;
336 dso->cache = RB_ROOT;
337 dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
338 dso->data_type = DSO_BINARY_TYPE__NOT_FOUND;
339 dso->loaded = 0;
340 dso->sorted_by_name = 0;
341 dso->has_build_id = 0;
342 dso->kernel = DSO_TYPE_USER;
343 dso->needs_swap = DSO_SWAP__UNSET;
344 INIT_LIST_HEAD(&dso->node);
347 return dso;
350 static void symbols__delete(struct rb_root *symbols)
352 struct symbol *pos;
353 struct rb_node *next = rb_first(symbols);
355 while (next) {
356 pos = rb_entry(next, struct symbol, rb_node);
357 next = rb_next(&pos->rb_node);
358 rb_erase(&pos->rb_node, symbols);
359 symbol__delete(pos);
363 void dso__delete(struct dso *dso)
365 int i;
366 for (i = 0; i < MAP__NR_TYPES; ++i)
367 symbols__delete(&dso->symbols[i]);
368 if (dso->sname_alloc)
369 free((char *)dso->short_name);
370 if (dso->lname_alloc)
371 free(dso->long_name);
372 dso_cache__free(&dso->cache);
373 free(dso);
376 void dso__set_build_id(struct dso *dso, void *build_id)
378 memcpy(dso->build_id, build_id, sizeof(dso->build_id));
379 dso->has_build_id = 1;
382 void symbols__insert(struct rb_root *symbols, struct symbol *sym)
384 struct rb_node **p = &symbols->rb_node;
385 struct rb_node *parent = NULL;
386 const u64 ip = sym->start;
387 struct symbol *s;
389 while (*p != NULL) {
390 parent = *p;
391 s = rb_entry(parent, struct symbol, rb_node);
392 if (ip < s->start)
393 p = &(*p)->rb_left;
394 else
395 p = &(*p)->rb_right;
397 rb_link_node(&sym->rb_node, parent, p);
398 rb_insert_color(&sym->rb_node, symbols);
401 static struct symbol *symbols__find(struct rb_root *symbols, u64 ip)
403 struct rb_node *n;
405 if (symbols == NULL)
406 return NULL;
408 n = symbols->rb_node;
410 while (n) {
411 struct symbol *s = rb_entry(n, struct symbol, rb_node);
413 if (ip < s->start)
414 n = n->rb_left;
415 else if (ip > s->end)
416 n = n->rb_right;
417 else
418 return s;
421 return NULL;
424 struct symbol_name_rb_node {
425 struct rb_node rb_node;
426 struct symbol sym;
429 static void symbols__insert_by_name(struct rb_root *symbols, struct symbol *sym)
431 struct rb_node **p = &symbols->rb_node;
432 struct rb_node *parent = NULL;
433 struct symbol_name_rb_node *symn, *s;
435 symn = container_of(sym, struct symbol_name_rb_node, sym);
437 while (*p != NULL) {
438 parent = *p;
439 s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
440 if (strcmp(sym->name, s->sym.name) < 0)
441 p = &(*p)->rb_left;
442 else
443 p = &(*p)->rb_right;
445 rb_link_node(&symn->rb_node, parent, p);
446 rb_insert_color(&symn->rb_node, symbols);
449 static void symbols__sort_by_name(struct rb_root *symbols,
450 struct rb_root *source)
452 struct rb_node *nd;
454 for (nd = rb_first(source); nd; nd = rb_next(nd)) {
455 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
456 symbols__insert_by_name(symbols, pos);
460 static struct symbol *symbols__find_by_name(struct rb_root *symbols,
461 const char *name)
463 struct rb_node *n;
465 if (symbols == NULL)
466 return NULL;
468 n = symbols->rb_node;
470 while (n) {
471 struct symbol_name_rb_node *s;
472 int cmp;
474 s = rb_entry(n, struct symbol_name_rb_node, rb_node);
475 cmp = strcmp(name, s->sym.name);
477 if (cmp < 0)
478 n = n->rb_left;
479 else if (cmp > 0)
480 n = n->rb_right;
481 else
482 return &s->sym;
485 return NULL;
488 struct symbol *dso__find_symbol(struct dso *dso,
489 enum map_type type, u64 addr)
491 return symbols__find(&dso->symbols[type], addr);
494 struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
495 const char *name)
497 return symbols__find_by_name(&dso->symbol_names[type], name);
500 void dso__sort_by_name(struct dso *dso, enum map_type type)
502 dso__set_sorted_by_name(dso, type);
503 return symbols__sort_by_name(&dso->symbol_names[type],
504 &dso->symbols[type]);
507 int build_id__sprintf(const u8 *build_id, int len, char *bf)
509 char *bid = bf;
510 const u8 *raw = build_id;
511 int i;
513 for (i = 0; i < len; ++i) {
514 sprintf(bid, "%02x", *raw);
515 ++raw;
516 bid += 2;
519 return raw - build_id;
522 size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
524 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
526 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
527 return fprintf(fp, "%s", sbuild_id);
530 size_t dso__fprintf_symbols_by_name(struct dso *dso,
531 enum map_type type, FILE *fp)
533 size_t ret = 0;
534 struct rb_node *nd;
535 struct symbol_name_rb_node *pos;
537 for (nd = rb_first(&dso->symbol_names[type]); nd; nd = rb_next(nd)) {
538 pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
539 fprintf(fp, "%s\n", pos->sym.name);
542 return ret;
545 size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp)
547 struct rb_node *nd;
548 size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
550 if (dso->short_name != dso->long_name)
551 ret += fprintf(fp, "%s, ", dso->long_name);
552 ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
553 dso->loaded ? "" : "NOT ");
554 ret += dso__fprintf_buildid(dso, fp);
555 ret += fprintf(fp, ")\n");
556 for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) {
557 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
558 ret += symbol__fprintf(pos, fp);
561 return ret;
564 int kallsyms__parse(const char *filename, void *arg,
565 int (*process_symbol)(void *arg, const char *name,
566 char type, u64 start, u64 end))
568 char *line = NULL;
569 size_t n;
570 int err = -1;
571 FILE *file = fopen(filename, "r");
573 if (file == NULL)
574 goto out_failure;
576 err = 0;
578 while (!feof(file)) {
579 u64 start;
580 int line_len, len;
581 char symbol_type;
582 char *symbol_name;
584 line_len = getline(&line, &n, file);
585 if (line_len < 0 || !line)
586 break;
588 line[--line_len] = '\0'; /* \n */
590 len = hex2u64(line, &start);
592 len++;
593 if (len + 2 >= line_len)
594 continue;
596 symbol_type = line[len];
597 len += 2;
598 symbol_name = line + len;
599 len = line_len - len;
601 if (len >= KSYM_NAME_LEN) {
602 err = -1;
603 break;
607 * module symbols are not sorted so we add all
608 * symbols, setting length to 1, and rely on
609 * symbols__fixup_end() to fix it up.
611 err = process_symbol(arg, symbol_name,
612 symbol_type, start, start);
613 if (err)
614 break;
617 free(line);
618 fclose(file);
619 return err;
621 out_failure:
622 return -1;
625 struct process_kallsyms_args {
626 struct map *map;
627 struct dso *dso;
630 static u8 kallsyms2elf_type(char type)
632 if (type == 'W')
633 return STB_WEAK;
635 return isupper(type) ? STB_GLOBAL : STB_LOCAL;
638 static int map__process_kallsym_symbol(void *arg, const char *name,
639 char type, u64 start, u64 end)
641 struct symbol *sym;
642 struct process_kallsyms_args *a = arg;
643 struct rb_root *root = &a->dso->symbols[a->map->type];
645 if (!symbol_type__is_a(type, a->map->type))
646 return 0;
648 sym = symbol__new(start, end - start + 1,
649 kallsyms2elf_type(type), name);
650 if (sym == NULL)
651 return -ENOMEM;
653 * We will pass the symbols to the filter later, in
654 * map__split_kallsyms, when we have split the maps per module
656 symbols__insert(root, sym);
658 return 0;
662 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
663 * so that we can in the next step set the symbol ->end address and then
664 * call kernel_maps__split_kallsyms.
666 static int dso__load_all_kallsyms(struct dso *dso, const char *filename,
667 struct map *map)
669 struct process_kallsyms_args args = { .map = map, .dso = dso, };
670 return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
674 * Split the symbols into maps, making sure there are no overlaps, i.e. the
675 * kernel range is broken in several maps, named [kernel].N, as we don't have
676 * the original ELF section names vmlinux have.
678 static int dso__split_kallsyms(struct dso *dso, struct map *map,
679 symbol_filter_t filter)
681 struct map_groups *kmaps = map__kmap(map)->kmaps;
682 struct machine *machine = kmaps->machine;
683 struct map *curr_map = map;
684 struct symbol *pos;
685 int count = 0, moved = 0;
686 struct rb_root *root = &dso->symbols[map->type];
687 struct rb_node *next = rb_first(root);
688 int kernel_range = 0;
690 while (next) {
691 char *module;
693 pos = rb_entry(next, struct symbol, rb_node);
694 next = rb_next(&pos->rb_node);
696 module = strchr(pos->name, '\t');
697 if (module) {
698 if (!symbol_conf.use_modules)
699 goto discard_symbol;
701 *module++ = '\0';
703 if (strcmp(curr_map->dso->short_name, module)) {
704 if (curr_map != map &&
705 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
706 machine__is_default_guest(machine)) {
708 * We assume all symbols of a module are
709 * continuous in * kallsyms, so curr_map
710 * points to a module and all its
711 * symbols are in its kmap. Mark it as
712 * loaded.
714 dso__set_loaded(curr_map->dso,
715 curr_map->type);
718 curr_map = map_groups__find_by_name(kmaps,
719 map->type, module);
720 if (curr_map == NULL) {
721 pr_debug("%s/proc/{kallsyms,modules} "
722 "inconsistency while looking "
723 "for \"%s\" module!\n",
724 machine->root_dir, module);
725 curr_map = map;
726 goto discard_symbol;
729 if (curr_map->dso->loaded &&
730 !machine__is_default_guest(machine))
731 goto discard_symbol;
734 * So that we look just like we get from .ko files,
735 * i.e. not prelinked, relative to map->start.
737 pos->start = curr_map->map_ip(curr_map, pos->start);
738 pos->end = curr_map->map_ip(curr_map, pos->end);
739 } else if (curr_map != map) {
740 char dso_name[PATH_MAX];
741 struct dso *ndso;
743 if (count == 0) {
744 curr_map = map;
745 goto filter_symbol;
748 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
749 snprintf(dso_name, sizeof(dso_name),
750 "[guest.kernel].%d",
751 kernel_range++);
752 else
753 snprintf(dso_name, sizeof(dso_name),
754 "[kernel].%d",
755 kernel_range++);
757 ndso = dso__new(dso_name);
758 if (ndso == NULL)
759 return -1;
761 ndso->kernel = dso->kernel;
763 curr_map = map__new2(pos->start, ndso, map->type);
764 if (curr_map == NULL) {
765 dso__delete(ndso);
766 return -1;
769 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
770 map_groups__insert(kmaps, curr_map);
771 ++kernel_range;
773 filter_symbol:
774 if (filter && filter(curr_map, pos)) {
775 discard_symbol: rb_erase(&pos->rb_node, root);
776 symbol__delete(pos);
777 } else {
778 if (curr_map != map) {
779 rb_erase(&pos->rb_node, root);
780 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
781 ++moved;
782 } else
783 ++count;
787 if (curr_map != map &&
788 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
789 machine__is_default_guest(kmaps->machine)) {
790 dso__set_loaded(curr_map->dso, curr_map->type);
793 return count + moved;
796 static bool symbol__restricted_filename(const char *filename,
797 const char *restricted_filename)
799 bool restricted = false;
801 if (symbol_conf.kptr_restrict) {
802 char *r = realpath(filename, NULL);
804 if (r != NULL) {
805 restricted = strcmp(r, restricted_filename) == 0;
806 free(r);
807 return restricted;
811 return restricted;
814 int dso__load_kallsyms(struct dso *dso, const char *filename,
815 struct map *map, symbol_filter_t filter)
817 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
818 return -1;
820 if (dso__load_all_kallsyms(dso, filename, map) < 0)
821 return -1;
823 symbols__fixup_duplicate(&dso->symbols[map->type]);
824 symbols__fixup_end(&dso->symbols[map->type]);
826 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
827 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
828 else
829 dso->symtab_type = DSO_BINARY_TYPE__KALLSYMS;
831 return dso__split_kallsyms(dso, map, filter);
834 static int dso__load_perf_map(struct dso *dso, struct map *map,
835 symbol_filter_t filter)
837 char *line = NULL;
838 size_t n;
839 FILE *file;
840 int nr_syms = 0;
842 file = fopen(dso->long_name, "r");
843 if (file == NULL)
844 goto out_failure;
846 while (!feof(file)) {
847 u64 start, size;
848 struct symbol *sym;
849 int line_len, len;
851 line_len = getline(&line, &n, file);
852 if (line_len < 0)
853 break;
855 if (!line)
856 goto out_failure;
858 line[--line_len] = '\0'; /* \n */
860 len = hex2u64(line, &start);
862 len++;
863 if (len + 2 >= line_len)
864 continue;
866 len += hex2u64(line + len, &size);
868 len++;
869 if (len + 2 >= line_len)
870 continue;
872 sym = symbol__new(start, size, STB_GLOBAL, line + len);
874 if (sym == NULL)
875 goto out_delete_line;
877 if (filter && filter(map, sym))
878 symbol__delete(sym);
879 else {
880 symbols__insert(&dso->symbols[map->type], sym);
881 nr_syms++;
885 free(line);
886 fclose(file);
888 return nr_syms;
890 out_delete_line:
891 free(line);
892 out_failure:
893 return -1;
896 bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
898 return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
901 bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
903 bool have_build_id = false;
904 struct dso *pos;
906 list_for_each_entry(pos, head, node) {
907 if (with_hits && !pos->hit)
908 continue;
909 if (pos->has_build_id) {
910 have_build_id = true;
911 continue;
913 if (filename__read_build_id(pos->long_name, pos->build_id,
914 sizeof(pos->build_id)) > 0) {
915 have_build_id = true;
916 pos->has_build_id = true;
920 return have_build_id;
923 char dso__symtab_origin(const struct dso *dso)
925 static const char origin[] = {
926 [DSO_BINARY_TYPE__KALLSYMS] = 'k',
927 [DSO_BINARY_TYPE__JAVA_JIT] = 'j',
928 [DSO_BINARY_TYPE__DEBUGLINK] = 'l',
929 [DSO_BINARY_TYPE__BUILD_ID_CACHE] = 'B',
930 [DSO_BINARY_TYPE__FEDORA_DEBUGINFO] = 'f',
931 [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO] = 'u',
932 [DSO_BINARY_TYPE__BUILDID_DEBUGINFO] = 'b',
933 [DSO_BINARY_TYPE__SYSTEM_PATH_DSO] = 'd',
934 [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE] = 'K',
935 [DSO_BINARY_TYPE__GUEST_KALLSYMS] = 'g',
936 [DSO_BINARY_TYPE__GUEST_KMODULE] = 'G',
939 if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND)
940 return '!';
941 return origin[dso->symtab_type];
944 int dso__binary_type_file(struct dso *dso, enum dso_binary_type type,
945 char *root_dir, char *file, size_t size)
947 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
948 int ret = 0;
950 switch (type) {
951 case DSO_BINARY_TYPE__DEBUGLINK: {
952 char *debuglink;
954 strncpy(file, dso->long_name, size);
955 debuglink = file + dso->long_name_len;
956 while (debuglink != file && *debuglink != '/')
957 debuglink--;
958 if (*debuglink == '/')
959 debuglink++;
960 filename__read_debuglink(dso->long_name, debuglink,
961 size - (debuglink - file));
963 break;
964 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
965 /* skip the locally configured cache if a symfs is given */
966 if (symbol_conf.symfs[0] ||
967 (dso__build_id_filename(dso, file, size) == NULL))
968 ret = -1;
969 break;
971 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
972 snprintf(file, size, "%s/usr/lib/debug%s.debug",
973 symbol_conf.symfs, dso->long_name);
974 break;
976 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
977 snprintf(file, size, "%s/usr/lib/debug%s",
978 symbol_conf.symfs, dso->long_name);
979 break;
981 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
982 if (!dso->has_build_id) {
983 ret = -1;
984 break;
987 build_id__sprintf(dso->build_id,
988 sizeof(dso->build_id),
989 build_id_hex);
990 snprintf(file, size,
991 "%s/usr/lib/debug/.build-id/%.2s/%s.debug",
992 symbol_conf.symfs, build_id_hex, build_id_hex + 2);
993 break;
995 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
996 snprintf(file, size, "%s%s",
997 symbol_conf.symfs, dso->long_name);
998 break;
1000 case DSO_BINARY_TYPE__GUEST_KMODULE:
1001 snprintf(file, size, "%s%s%s", symbol_conf.symfs,
1002 root_dir, dso->long_name);
1003 break;
1005 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
1006 snprintf(file, size, "%s%s", symbol_conf.symfs,
1007 dso->long_name);
1008 break;
1010 default:
1011 case DSO_BINARY_TYPE__KALLSYMS:
1012 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
1013 case DSO_BINARY_TYPE__JAVA_JIT:
1014 case DSO_BINARY_TYPE__NOT_FOUND:
1015 ret = -1;
1016 break;
1019 return ret;
1022 int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter)
1024 char *name;
1025 int ret = -1;
1026 int fd;
1027 u_int i;
1028 struct machine *machine;
1029 char *root_dir = (char *) "";
1030 int want_symtab;
1032 dso__set_loaded(dso, map->type);
1034 if (dso->kernel == DSO_TYPE_KERNEL)
1035 return dso__load_kernel_sym(dso, map, filter);
1036 else if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1037 return dso__load_guest_kernel_sym(dso, map, filter);
1039 if (map->groups && map->groups->machine)
1040 machine = map->groups->machine;
1041 else
1042 machine = NULL;
1044 name = malloc(PATH_MAX);
1045 if (!name)
1046 return -1;
1048 dso->adjust_symbols = 0;
1050 if (strncmp(dso->name, "/tmp/perf-", 10) == 0) {
1051 struct stat st;
1053 if (lstat(dso->name, &st) < 0)
1054 return -1;
1056 if (st.st_uid && (st.st_uid != geteuid())) {
1057 pr_warning("File %s not owned by current user or root, "
1058 "ignoring it.\n", dso->name);
1059 return -1;
1062 ret = dso__load_perf_map(dso, map, filter);
1063 dso->symtab_type = ret > 0 ? DSO_BINARY_TYPE__JAVA_JIT :
1064 DSO_BINARY_TYPE__NOT_FOUND;
1065 return ret;
1068 if (machine)
1069 root_dir = machine->root_dir;
1071 /* Iterate over candidate debug images.
1072 * On the first pass, only load images if they have a full symtab.
1073 * Failing that, do a second pass where we accept .dynsym also
1075 want_symtab = 1;
1076 restart:
1077 for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) {
1079 dso->symtab_type = binary_type_symtab[i];
1081 if (dso__binary_type_file(dso, dso->symtab_type,
1082 root_dir, name, PATH_MAX))
1083 continue;
1085 /* Name is now the name of the next image to try */
1086 fd = open(name, O_RDONLY);
1087 if (fd < 0)
1088 continue;
1090 ret = dso__load_sym(dso, map, name, fd, filter, 0,
1091 want_symtab);
1092 close(fd);
1095 * Some people seem to have debuginfo files _WITHOUT_ debug
1096 * info!?!?
1098 if (!ret)
1099 continue;
1101 if (ret > 0) {
1102 int nr_plt;
1104 nr_plt = dso__synthesize_plt_symbols(dso, name, map, filter);
1105 if (nr_plt > 0)
1106 ret += nr_plt;
1107 break;
1112 * If we wanted a full symtab but no image had one,
1113 * relax our requirements and repeat the search.
1115 if (ret <= 0 && want_symtab) {
1116 want_symtab = 0;
1117 goto restart;
1120 free(name);
1121 if (ret < 0 && strstr(dso->name, " (deleted)") != NULL)
1122 return 0;
1123 return ret;
1126 struct map *map_groups__find_by_name(struct map_groups *mg,
1127 enum map_type type, const char *name)
1129 struct rb_node *nd;
1131 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
1132 struct map *map = rb_entry(nd, struct map, rb_node);
1134 if (map->dso && strcmp(map->dso->short_name, name) == 0)
1135 return map;
1138 return NULL;
1141 static int dso__kernel_module_get_build_id(struct dso *dso,
1142 const char *root_dir)
1144 char filename[PATH_MAX];
1146 * kernel module short names are of the form "[module]" and
1147 * we need just "module" here.
1149 const char *name = dso->short_name + 1;
1151 snprintf(filename, sizeof(filename),
1152 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1153 root_dir, (int)strlen(name) - 1, name);
1155 if (sysfs__read_build_id(filename, dso->build_id,
1156 sizeof(dso->build_id)) == 0)
1157 dso->has_build_id = true;
1159 return 0;
1162 static int map_groups__set_modules_path_dir(struct map_groups *mg,
1163 const char *dir_name)
1165 struct dirent *dent;
1166 DIR *dir = opendir(dir_name);
1167 int ret = 0;
1169 if (!dir) {
1170 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
1171 return -1;
1174 while ((dent = readdir(dir)) != NULL) {
1175 char path[PATH_MAX];
1176 struct stat st;
1178 /*sshfs might return bad dent->d_type, so we have to stat*/
1179 snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name);
1180 if (stat(path, &st))
1181 continue;
1183 if (S_ISDIR(st.st_mode)) {
1184 if (!strcmp(dent->d_name, ".") ||
1185 !strcmp(dent->d_name, ".."))
1186 continue;
1188 ret = map_groups__set_modules_path_dir(mg, path);
1189 if (ret < 0)
1190 goto out;
1191 } else {
1192 char *dot = strrchr(dent->d_name, '.'),
1193 dso_name[PATH_MAX];
1194 struct map *map;
1195 char *long_name;
1197 if (dot == NULL || strcmp(dot, ".ko"))
1198 continue;
1199 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1200 (int)(dot - dent->d_name), dent->d_name);
1202 strxfrchar(dso_name, '-', '_');
1203 map = map_groups__find_by_name(mg, MAP__FUNCTION,
1204 dso_name);
1205 if (map == NULL)
1206 continue;
1208 long_name = strdup(path);
1209 if (long_name == NULL) {
1210 ret = -1;
1211 goto out;
1213 dso__set_long_name(map->dso, long_name);
1214 map->dso->lname_alloc = 1;
1215 dso__kernel_module_get_build_id(map->dso, "");
1219 out:
1220 closedir(dir);
1221 return ret;
1224 static char *get_kernel_version(const char *root_dir)
1226 char version[PATH_MAX];
1227 FILE *file;
1228 char *name, *tmp;
1229 const char *prefix = "Linux version ";
1231 sprintf(version, "%s/proc/version", root_dir);
1232 file = fopen(version, "r");
1233 if (!file)
1234 return NULL;
1236 version[0] = '\0';
1237 tmp = fgets(version, sizeof(version), file);
1238 fclose(file);
1240 name = strstr(version, prefix);
1241 if (!name)
1242 return NULL;
1243 name += strlen(prefix);
1244 tmp = strchr(name, ' ');
1245 if (tmp)
1246 *tmp = '\0';
1248 return strdup(name);
1251 static int machine__set_modules_path(struct machine *machine)
1253 char *version;
1254 char modules_path[PATH_MAX];
1256 version = get_kernel_version(machine->root_dir);
1257 if (!version)
1258 return -1;
1260 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s/kernel",
1261 machine->root_dir, version);
1262 free(version);
1264 return map_groups__set_modules_path_dir(&machine->kmaps, modules_path);
1267 struct map *machine__new_module(struct machine *machine, u64 start,
1268 const char *filename)
1270 struct map *map;
1271 struct dso *dso = __dsos__findnew(&machine->kernel_dsos, filename);
1273 if (dso == NULL)
1274 return NULL;
1276 map = map__new2(start, dso, MAP__FUNCTION);
1277 if (map == NULL)
1278 return NULL;
1280 if (machine__is_host(machine))
1281 dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
1282 else
1283 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
1284 map_groups__insert(&machine->kmaps, map);
1285 return map;
1288 static int machine__create_modules(struct machine *machine)
1290 char *line = NULL;
1291 size_t n;
1292 FILE *file;
1293 struct map *map;
1294 const char *modules;
1295 char path[PATH_MAX];
1297 if (machine__is_default_guest(machine))
1298 modules = symbol_conf.default_guest_modules;
1299 else {
1300 sprintf(path, "%s/proc/modules", machine->root_dir);
1301 modules = path;
1304 if (symbol__restricted_filename(path, "/proc/modules"))
1305 return -1;
1307 file = fopen(modules, "r");
1308 if (file == NULL)
1309 return -1;
1311 while (!feof(file)) {
1312 char name[PATH_MAX];
1313 u64 start;
1314 char *sep;
1315 int line_len;
1317 line_len = getline(&line, &n, file);
1318 if (line_len < 0)
1319 break;
1321 if (!line)
1322 goto out_failure;
1324 line[--line_len] = '\0'; /* \n */
1326 sep = strrchr(line, 'x');
1327 if (sep == NULL)
1328 continue;
1330 hex2u64(sep + 1, &start);
1332 sep = strchr(line, ' ');
1333 if (sep == NULL)
1334 continue;
1336 *sep = '\0';
1338 snprintf(name, sizeof(name), "[%s]", line);
1339 map = machine__new_module(machine, start, name);
1340 if (map == NULL)
1341 goto out_delete_line;
1342 dso__kernel_module_get_build_id(map->dso, machine->root_dir);
1345 free(line);
1346 fclose(file);
1348 return machine__set_modules_path(machine);
1350 out_delete_line:
1351 free(line);
1352 out_failure:
1353 return -1;
1356 int dso__load_vmlinux(struct dso *dso, struct map *map,
1357 const char *vmlinux, symbol_filter_t filter)
1359 int err = -1, fd;
1360 char symfs_vmlinux[PATH_MAX];
1362 snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s%s",
1363 symbol_conf.symfs, vmlinux);
1364 fd = open(symfs_vmlinux, O_RDONLY);
1365 if (fd < 0)
1366 return -1;
1368 dso__set_long_name(dso, (char *)vmlinux);
1369 dso__set_loaded(dso, map->type);
1370 err = dso__load_sym(dso, map, symfs_vmlinux, fd, filter, 0, 0);
1371 close(fd);
1373 if (err > 0)
1374 pr_debug("Using %s for symbols\n", symfs_vmlinux);
1376 return err;
1379 int dso__load_vmlinux_path(struct dso *dso, struct map *map,
1380 symbol_filter_t filter)
1382 int i, err = 0;
1383 char *filename;
1385 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1386 vmlinux_path__nr_entries + 1);
1388 filename = dso__build_id_filename(dso, NULL, 0);
1389 if (filename != NULL) {
1390 err = dso__load_vmlinux(dso, map, filename, filter);
1391 if (err > 0)
1392 goto out;
1393 free(filename);
1396 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
1397 err = dso__load_vmlinux(dso, map, vmlinux_path[i], filter);
1398 if (err > 0) {
1399 dso__set_long_name(dso, strdup(vmlinux_path[i]));
1400 break;
1403 out:
1404 return err;
1407 static int dso__load_kernel_sym(struct dso *dso, struct map *map,
1408 symbol_filter_t filter)
1410 int err;
1411 const char *kallsyms_filename = NULL;
1412 char *kallsyms_allocated_filename = NULL;
1414 * Step 1: if the user specified a kallsyms or vmlinux filename, use
1415 * it and only it, reporting errors to the user if it cannot be used.
1417 * For instance, try to analyse an ARM perf.data file _without_ a
1418 * build-id, or if the user specifies the wrong path to the right
1419 * vmlinux file, obviously we can't fallback to another vmlinux (a
1420 * x86_86 one, on the machine where analysis is being performed, say),
1421 * or worse, /proc/kallsyms.
1423 * If the specified file _has_ a build-id and there is a build-id
1424 * section in the perf.data file, we will still do the expected
1425 * validation in dso__load_vmlinux and will bail out if they don't
1426 * match.
1428 if (symbol_conf.kallsyms_name != NULL) {
1429 kallsyms_filename = symbol_conf.kallsyms_name;
1430 goto do_kallsyms;
1433 if (symbol_conf.vmlinux_name != NULL) {
1434 err = dso__load_vmlinux(dso, map,
1435 symbol_conf.vmlinux_name, filter);
1436 if (err > 0) {
1437 dso__set_long_name(dso,
1438 strdup(symbol_conf.vmlinux_name));
1439 goto out_fixup;
1441 return err;
1444 if (vmlinux_path != NULL) {
1445 err = dso__load_vmlinux_path(dso, map, filter);
1446 if (err > 0)
1447 goto out_fixup;
1450 /* do not try local files if a symfs was given */
1451 if (symbol_conf.symfs[0] != 0)
1452 return -1;
1455 * Say the kernel DSO was created when processing the build-id header table,
1456 * we have a build-id, so check if it is the same as the running kernel,
1457 * using it if it is.
1459 if (dso->has_build_id) {
1460 u8 kallsyms_build_id[BUILD_ID_SIZE];
1461 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1463 if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id,
1464 sizeof(kallsyms_build_id)) == 0) {
1465 if (dso__build_id_equal(dso, kallsyms_build_id)) {
1466 kallsyms_filename = "/proc/kallsyms";
1467 goto do_kallsyms;
1471 * Now look if we have it on the build-id cache in
1472 * $HOME/.debug/[kernel.kallsyms].
1474 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1475 sbuild_id);
1477 if (asprintf(&kallsyms_allocated_filename,
1478 "%s/.debug/[kernel.kallsyms]/%s",
1479 getenv("HOME"), sbuild_id) == -1) {
1480 pr_err("Not enough memory for kallsyms file lookup\n");
1481 return -1;
1484 kallsyms_filename = kallsyms_allocated_filename;
1486 if (access(kallsyms_filename, F_OK)) {
1487 pr_err("No kallsyms or vmlinux with build-id %s "
1488 "was found\n", sbuild_id);
1489 free(kallsyms_allocated_filename);
1490 return -1;
1492 } else {
1494 * Last resort, if we don't have a build-id and couldn't find
1495 * any vmlinux file, try the running kernel kallsyms table.
1497 kallsyms_filename = "/proc/kallsyms";
1500 do_kallsyms:
1501 err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
1502 if (err > 0)
1503 pr_debug("Using %s for symbols\n", kallsyms_filename);
1504 free(kallsyms_allocated_filename);
1506 if (err > 0) {
1507 out_fixup:
1508 if (kallsyms_filename != NULL)
1509 dso__set_long_name(dso, strdup("[kernel.kallsyms]"));
1510 map__fixup_start(map);
1511 map__fixup_end(map);
1514 return err;
1517 static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
1518 symbol_filter_t filter)
1520 int err;
1521 const char *kallsyms_filename = NULL;
1522 struct machine *machine;
1523 char path[PATH_MAX];
1525 if (!map->groups) {
1526 pr_debug("Guest kernel map hasn't the point to groups\n");
1527 return -1;
1529 machine = map->groups->machine;
1531 if (machine__is_default_guest(machine)) {
1533 * if the user specified a vmlinux filename, use it and only
1534 * it, reporting errors to the user if it cannot be used.
1535 * Or use file guest_kallsyms inputted by user on commandline
1537 if (symbol_conf.default_guest_vmlinux_name != NULL) {
1538 err = dso__load_vmlinux(dso, map,
1539 symbol_conf.default_guest_vmlinux_name, filter);
1540 goto out_try_fixup;
1543 kallsyms_filename = symbol_conf.default_guest_kallsyms;
1544 if (!kallsyms_filename)
1545 return -1;
1546 } else {
1547 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
1548 kallsyms_filename = path;
1551 err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
1552 if (err > 0)
1553 pr_debug("Using %s for symbols\n", kallsyms_filename);
1555 out_try_fixup:
1556 if (err > 0) {
1557 if (kallsyms_filename != NULL) {
1558 machine__mmap_name(machine, path, sizeof(path));
1559 dso__set_long_name(dso, strdup(path));
1561 map__fixup_start(map);
1562 map__fixup_end(map);
1565 return err;
1568 void dsos__add(struct list_head *head, struct dso *dso)
1570 list_add_tail(&dso->node, head);
1573 static struct dso *dsos__find(struct list_head *head, const char *name)
1575 struct dso *pos;
1577 list_for_each_entry(pos, head, node)
1578 if (strcmp(pos->long_name, name) == 0)
1579 return pos;
1580 return NULL;
1583 struct dso *__dsos__findnew(struct list_head *head, const char *name)
1585 struct dso *dso = dsos__find(head, name);
1587 if (!dso) {
1588 dso = dso__new(name);
1589 if (dso != NULL) {
1590 dsos__add(head, dso);
1591 dso__set_basename(dso);
1595 return dso;
1598 size_t __dsos__fprintf(struct list_head *head, FILE *fp)
1600 struct dso *pos;
1601 size_t ret = 0;
1603 list_for_each_entry(pos, head, node) {
1604 int i;
1605 for (i = 0; i < MAP__NR_TYPES; ++i)
1606 ret += dso__fprintf(pos, i, fp);
1609 return ret;
1612 size_t machines__fprintf_dsos(struct rb_root *machines, FILE *fp)
1614 struct rb_node *nd;
1615 size_t ret = 0;
1617 for (nd = rb_first(machines); nd; nd = rb_next(nd)) {
1618 struct machine *pos = rb_entry(nd, struct machine, rb_node);
1619 ret += __dsos__fprintf(&pos->kernel_dsos, fp);
1620 ret += __dsos__fprintf(&pos->user_dsos, fp);
1623 return ret;
1626 static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
1627 bool with_hits)
1629 struct dso *pos;
1630 size_t ret = 0;
1632 list_for_each_entry(pos, head, node) {
1633 if (with_hits && !pos->hit)
1634 continue;
1635 ret += dso__fprintf_buildid(pos, fp);
1636 ret += fprintf(fp, " %s\n", pos->long_name);
1638 return ret;
1641 size_t machine__fprintf_dsos_buildid(struct machine *machine, FILE *fp,
1642 bool with_hits)
1644 return __dsos__fprintf_buildid(&machine->kernel_dsos, fp, with_hits) +
1645 __dsos__fprintf_buildid(&machine->user_dsos, fp, with_hits);
1648 size_t machines__fprintf_dsos_buildid(struct rb_root *machines,
1649 FILE *fp, bool with_hits)
1651 struct rb_node *nd;
1652 size_t ret = 0;
1654 for (nd = rb_first(machines); nd; nd = rb_next(nd)) {
1655 struct machine *pos = rb_entry(nd, struct machine, rb_node);
1656 ret += machine__fprintf_dsos_buildid(pos, fp, with_hits);
1658 return ret;
1661 static struct dso*
1662 dso__kernel_findnew(struct machine *machine, const char *name,
1663 const char *short_name, int dso_type)
1666 * The kernel dso could be created by build_id processing.
1668 struct dso *dso = __dsos__findnew(&machine->kernel_dsos, name);
1671 * We need to run this in all cases, since during the build_id
1672 * processing we had no idea this was the kernel dso.
1674 if (dso != NULL) {
1675 dso__set_short_name(dso, short_name);
1676 dso->kernel = dso_type;
1679 return dso;
1682 void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
1684 char path[PATH_MAX];
1686 if (machine__is_default_guest(machine))
1687 return;
1688 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
1689 if (sysfs__read_build_id(path, dso->build_id,
1690 sizeof(dso->build_id)) == 0)
1691 dso->has_build_id = true;
1694 static struct dso *machine__get_kernel(struct machine *machine)
1696 const char *vmlinux_name = NULL;
1697 struct dso *kernel;
1699 if (machine__is_host(machine)) {
1700 vmlinux_name = symbol_conf.vmlinux_name;
1701 if (!vmlinux_name)
1702 vmlinux_name = "[kernel.kallsyms]";
1704 kernel = dso__kernel_findnew(machine, vmlinux_name,
1705 "[kernel]",
1706 DSO_TYPE_KERNEL);
1707 } else {
1708 char bf[PATH_MAX];
1710 if (machine__is_default_guest(machine))
1711 vmlinux_name = symbol_conf.default_guest_vmlinux_name;
1712 if (!vmlinux_name)
1713 vmlinux_name = machine__mmap_name(machine, bf,
1714 sizeof(bf));
1716 kernel = dso__kernel_findnew(machine, vmlinux_name,
1717 "[guest.kernel]",
1718 DSO_TYPE_GUEST_KERNEL);
1721 if (kernel != NULL && (!kernel->has_build_id))
1722 dso__read_running_kernel_build_id(kernel, machine);
1724 return kernel;
1727 struct process_args {
1728 u64 start;
1731 static int symbol__in_kernel(void *arg, const char *name,
1732 char type __used, u64 start, u64 end __used)
1734 struct process_args *args = arg;
1736 if (strchr(name, '['))
1737 return 0;
1739 args->start = start;
1740 return 1;
1743 /* Figure out the start address of kernel map from /proc/kallsyms */
1744 static u64 machine__get_kernel_start_addr(struct machine *machine)
1746 const char *filename;
1747 char path[PATH_MAX];
1748 struct process_args args;
1750 if (machine__is_host(machine)) {
1751 filename = "/proc/kallsyms";
1752 } else {
1753 if (machine__is_default_guest(machine))
1754 filename = (char *)symbol_conf.default_guest_kallsyms;
1755 else {
1756 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
1757 filename = path;
1761 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
1762 return 0;
1764 if (kallsyms__parse(filename, &args, symbol__in_kernel) <= 0)
1765 return 0;
1767 return args.start;
1770 int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
1772 enum map_type type;
1773 u64 start = machine__get_kernel_start_addr(machine);
1775 for (type = 0; type < MAP__NR_TYPES; ++type) {
1776 struct kmap *kmap;
1778 machine->vmlinux_maps[type] = map__new2(start, kernel, type);
1779 if (machine->vmlinux_maps[type] == NULL)
1780 return -1;
1782 machine->vmlinux_maps[type]->map_ip =
1783 machine->vmlinux_maps[type]->unmap_ip =
1784 identity__map_ip;
1785 kmap = map__kmap(machine->vmlinux_maps[type]);
1786 kmap->kmaps = &machine->kmaps;
1787 map_groups__insert(&machine->kmaps,
1788 machine->vmlinux_maps[type]);
1791 return 0;
1794 void machine__destroy_kernel_maps(struct machine *machine)
1796 enum map_type type;
1798 for (type = 0; type < MAP__NR_TYPES; ++type) {
1799 struct kmap *kmap;
1801 if (machine->vmlinux_maps[type] == NULL)
1802 continue;
1804 kmap = map__kmap(machine->vmlinux_maps[type]);
1805 map_groups__remove(&machine->kmaps,
1806 machine->vmlinux_maps[type]);
1807 if (kmap->ref_reloc_sym) {
1809 * ref_reloc_sym is shared among all maps, so free just
1810 * on one of them.
1812 if (type == MAP__FUNCTION) {
1813 free((char *)kmap->ref_reloc_sym->name);
1814 kmap->ref_reloc_sym->name = NULL;
1815 free(kmap->ref_reloc_sym);
1817 kmap->ref_reloc_sym = NULL;
1820 map__delete(machine->vmlinux_maps[type]);
1821 machine->vmlinux_maps[type] = NULL;
1825 int machine__create_kernel_maps(struct machine *machine)
1827 struct dso *kernel = machine__get_kernel(machine);
1829 if (kernel == NULL ||
1830 __machine__create_kernel_maps(machine, kernel) < 0)
1831 return -1;
1833 if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
1834 if (machine__is_host(machine))
1835 pr_debug("Problems creating module maps, "
1836 "continuing anyway...\n");
1837 else
1838 pr_debug("Problems creating module maps for guest %d, "
1839 "continuing anyway...\n", machine->pid);
1843 * Now that we have all the maps created, just set the ->end of them:
1845 map_groups__fixup_end(&machine->kmaps);
1846 return 0;
1849 static void vmlinux_path__exit(void)
1851 while (--vmlinux_path__nr_entries >= 0) {
1852 free(vmlinux_path[vmlinux_path__nr_entries]);
1853 vmlinux_path[vmlinux_path__nr_entries] = NULL;
1856 free(vmlinux_path);
1857 vmlinux_path = NULL;
1860 static int vmlinux_path__init(void)
1862 struct utsname uts;
1863 char bf[PATH_MAX];
1865 vmlinux_path = malloc(sizeof(char *) * 5);
1866 if (vmlinux_path == NULL)
1867 return -1;
1869 vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
1870 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1871 goto out_fail;
1872 ++vmlinux_path__nr_entries;
1873 vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
1874 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1875 goto out_fail;
1876 ++vmlinux_path__nr_entries;
1878 /* only try running kernel version if no symfs was given */
1879 if (symbol_conf.symfs[0] != 0)
1880 return 0;
1882 if (uname(&uts) < 0)
1883 return -1;
1885 snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
1886 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1887 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1888 goto out_fail;
1889 ++vmlinux_path__nr_entries;
1890 snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
1891 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1892 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1893 goto out_fail;
1894 ++vmlinux_path__nr_entries;
1895 snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
1896 uts.release);
1897 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1898 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1899 goto out_fail;
1900 ++vmlinux_path__nr_entries;
1902 return 0;
1904 out_fail:
1905 vmlinux_path__exit();
1906 return -1;
1909 size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
1911 int i;
1912 size_t printed = 0;
1913 struct dso *kdso = machine->vmlinux_maps[MAP__FUNCTION]->dso;
1915 if (kdso->has_build_id) {
1916 char filename[PATH_MAX];
1917 if (dso__build_id_filename(kdso, filename, sizeof(filename)))
1918 printed += fprintf(fp, "[0] %s\n", filename);
1921 for (i = 0; i < vmlinux_path__nr_entries; ++i)
1922 printed += fprintf(fp, "[%d] %s\n",
1923 i + kdso->has_build_id, vmlinux_path[i]);
1925 return printed;
1928 static int setup_list(struct strlist **list, const char *list_str,
1929 const char *list_name)
1931 if (list_str == NULL)
1932 return 0;
1934 *list = strlist__new(true, list_str);
1935 if (!*list) {
1936 pr_err("problems parsing %s list\n", list_name);
1937 return -1;
1939 return 0;
1942 static bool symbol__read_kptr_restrict(void)
1944 bool value = false;
1946 if (geteuid() != 0) {
1947 FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
1948 if (fp != NULL) {
1949 char line[8];
1951 if (fgets(line, sizeof(line), fp) != NULL)
1952 value = atoi(line) != 0;
1954 fclose(fp);
1958 return value;
1961 int symbol__init(void)
1963 const char *symfs;
1965 if (symbol_conf.initialized)
1966 return 0;
1968 symbol_conf.priv_size = ALIGN(symbol_conf.priv_size, sizeof(u64));
1970 symbol__elf_init();
1972 if (symbol_conf.sort_by_name)
1973 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
1974 sizeof(struct symbol));
1976 if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
1977 return -1;
1979 if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
1980 pr_err("'.' is the only non valid --field-separator argument\n");
1981 return -1;
1984 if (setup_list(&symbol_conf.dso_list,
1985 symbol_conf.dso_list_str, "dso") < 0)
1986 return -1;
1988 if (setup_list(&symbol_conf.comm_list,
1989 symbol_conf.comm_list_str, "comm") < 0)
1990 goto out_free_dso_list;
1992 if (setup_list(&symbol_conf.sym_list,
1993 symbol_conf.sym_list_str, "symbol") < 0)
1994 goto out_free_comm_list;
1997 * A path to symbols of "/" is identical to ""
1998 * reset here for simplicity.
2000 symfs = realpath(symbol_conf.symfs, NULL);
2001 if (symfs == NULL)
2002 symfs = symbol_conf.symfs;
2003 if (strcmp(symfs, "/") == 0)
2004 symbol_conf.symfs = "";
2005 if (symfs != symbol_conf.symfs)
2006 free((void *)symfs);
2008 symbol_conf.kptr_restrict = symbol__read_kptr_restrict();
2010 symbol_conf.initialized = true;
2011 return 0;
2013 out_free_comm_list:
2014 strlist__delete(symbol_conf.comm_list);
2015 out_free_dso_list:
2016 strlist__delete(symbol_conf.dso_list);
2017 return -1;
2020 void symbol__exit(void)
2022 if (!symbol_conf.initialized)
2023 return;
2024 strlist__delete(symbol_conf.sym_list);
2025 strlist__delete(symbol_conf.dso_list);
2026 strlist__delete(symbol_conf.comm_list);
2027 vmlinux_path__exit();
2028 symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
2029 symbol_conf.initialized = false;
2032 int machines__create_kernel_maps(struct rb_root *machines, pid_t pid)
2034 struct machine *machine = machines__findnew(machines, pid);
2036 if (machine == NULL)
2037 return -1;
2039 return machine__create_kernel_maps(machine);
2042 static int hex(char ch)
2044 if ((ch >= '0') && (ch <= '9'))
2045 return ch - '0';
2046 if ((ch >= 'a') && (ch <= 'f'))
2047 return ch - 'a' + 10;
2048 if ((ch >= 'A') && (ch <= 'F'))
2049 return ch - 'A' + 10;
2050 return -1;
2054 * While we find nice hex chars, build a long_val.
2055 * Return number of chars processed.
2057 int hex2u64(const char *ptr, u64 *long_val)
2059 const char *p = ptr;
2060 *long_val = 0;
2062 while (*p) {
2063 const int hex_val = hex(*p);
2065 if (hex_val < 0)
2066 break;
2068 *long_val = (*long_val << 4) | hex_val;
2069 p++;
2072 return p - ptr;
2075 char *strxfrchar(char *s, char from, char to)
2077 char *p = s;
2079 while ((p = strchr(p, from)) != NULL)
2080 *p++ = to;
2082 return s;
2085 int machines__create_guest_kernel_maps(struct rb_root *machines)
2087 int ret = 0;
2088 struct dirent **namelist = NULL;
2089 int i, items = 0;
2090 char path[PATH_MAX];
2091 pid_t pid;
2092 char *endp;
2094 if (symbol_conf.default_guest_vmlinux_name ||
2095 symbol_conf.default_guest_modules ||
2096 symbol_conf.default_guest_kallsyms) {
2097 machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
2100 if (symbol_conf.guestmount) {
2101 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
2102 if (items <= 0)
2103 return -ENOENT;
2104 for (i = 0; i < items; i++) {
2105 if (!isdigit(namelist[i]->d_name[0])) {
2106 /* Filter out . and .. */
2107 continue;
2109 pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10);
2110 if ((*endp != '\0') ||
2111 (endp == namelist[i]->d_name) ||
2112 (errno == ERANGE)) {
2113 pr_debug("invalid directory (%s). Skipping.\n",
2114 namelist[i]->d_name);
2115 continue;
2117 sprintf(path, "%s/%s/proc/kallsyms",
2118 symbol_conf.guestmount,
2119 namelist[i]->d_name);
2120 ret = access(path, R_OK);
2121 if (ret) {
2122 pr_debug("Can't access file %s\n", path);
2123 goto failure;
2125 machines__create_kernel_maps(machines, pid);
2127 failure:
2128 free(namelist);
2131 return ret;
2134 void machines__destroy_guest_kernel_maps(struct rb_root *machines)
2136 struct rb_node *next = rb_first(machines);
2138 while (next) {
2139 struct machine *pos = rb_entry(next, struct machine, rb_node);
2141 next = rb_next(&pos->rb_node);
2142 rb_erase(&pos->rb_node, machines);
2143 machine__delete(pos);
2147 int machine__load_kallsyms(struct machine *machine, const char *filename,
2148 enum map_type type, symbol_filter_t filter)
2150 struct map *map = machine->vmlinux_maps[type];
2151 int ret = dso__load_kallsyms(map->dso, filename, map, filter);
2153 if (ret > 0) {
2154 dso__set_loaded(map->dso, type);
2156 * Since /proc/kallsyms will have multiple sessions for the
2157 * kernel, with modules between them, fixup the end of all
2158 * sections.
2160 __map_groups__fixup_end(&machine->kmaps, type);
2163 return ret;
2166 int machine__load_vmlinux_path(struct machine *machine, enum map_type type,
2167 symbol_filter_t filter)
2169 struct map *map = machine->vmlinux_maps[type];
2170 int ret = dso__load_vmlinux_path(map->dso, map, filter);
2172 if (ret > 0) {
2173 dso__set_loaded(map->dso, type);
2174 map__reloc_vmlinux(map);
2177 return ret;
2180 struct map *dso__new_map(const char *name)
2182 struct map *map = NULL;
2183 struct dso *dso = dso__new(name);
2185 if (dso)
2186 map = map__new2(0, dso, MAP__FUNCTION);
2188 return map;
2191 static int open_dso(struct dso *dso, struct machine *machine)
2193 char *root_dir = (char *) "";
2194 char *name;
2195 int fd;
2197 name = malloc(PATH_MAX);
2198 if (!name)
2199 return -ENOMEM;
2201 if (machine)
2202 root_dir = machine->root_dir;
2204 if (dso__binary_type_file(dso, dso->data_type,
2205 root_dir, name, PATH_MAX)) {
2206 free(name);
2207 return -EINVAL;
2210 fd = open(name, O_RDONLY);
2211 free(name);
2212 return fd;
2215 int dso__data_fd(struct dso *dso, struct machine *machine)
2217 int i = 0;
2219 if (dso->data_type != DSO_BINARY_TYPE__NOT_FOUND)
2220 return open_dso(dso, machine);
2222 do {
2223 int fd;
2225 dso->data_type = binary_type_data[i++];
2227 fd = open_dso(dso, machine);
2228 if (fd >= 0)
2229 return fd;
2231 } while (dso->data_type != DSO_BINARY_TYPE__NOT_FOUND);
2233 return -EINVAL;
2236 static void
2237 dso_cache__free(struct rb_root *root)
2239 struct rb_node *next = rb_first(root);
2241 while (next) {
2242 struct dso_cache *cache;
2244 cache = rb_entry(next, struct dso_cache, rb_node);
2245 next = rb_next(&cache->rb_node);
2246 rb_erase(&cache->rb_node, root);
2247 free(cache);
2251 static struct dso_cache*
2252 dso_cache__find(struct rb_root *root, u64 offset)
2254 struct rb_node **p = &root->rb_node;
2255 struct rb_node *parent = NULL;
2256 struct dso_cache *cache;
2258 while (*p != NULL) {
2259 u64 end;
2261 parent = *p;
2262 cache = rb_entry(parent, struct dso_cache, rb_node);
2263 end = cache->offset + DSO__DATA_CACHE_SIZE;
2265 if (offset < cache->offset)
2266 p = &(*p)->rb_left;
2267 else if (offset >= end)
2268 p = &(*p)->rb_right;
2269 else
2270 return cache;
2272 return NULL;
2275 static void
2276 dso_cache__insert(struct rb_root *root, struct dso_cache *new)
2278 struct rb_node **p = &root->rb_node;
2279 struct rb_node *parent = NULL;
2280 struct dso_cache *cache;
2281 u64 offset = new->offset;
2283 while (*p != NULL) {
2284 u64 end;
2286 parent = *p;
2287 cache = rb_entry(parent, struct dso_cache, rb_node);
2288 end = cache->offset + DSO__DATA_CACHE_SIZE;
2290 if (offset < cache->offset)
2291 p = &(*p)->rb_left;
2292 else if (offset >= end)
2293 p = &(*p)->rb_right;
2296 rb_link_node(&new->rb_node, parent, p);
2297 rb_insert_color(&new->rb_node, root);
2300 static ssize_t
2301 dso_cache__memcpy(struct dso_cache *cache, u64 offset,
2302 u8 *data, u64 size)
2304 u64 cache_offset = offset - cache->offset;
2305 u64 cache_size = min(cache->size - cache_offset, size);
2307 memcpy(data, cache->data + cache_offset, cache_size);
2308 return cache_size;
2311 static ssize_t
2312 dso_cache__read(struct dso *dso, struct machine *machine,
2313 u64 offset, u8 *data, ssize_t size)
2315 struct dso_cache *cache;
2316 ssize_t ret;
2317 int fd;
2319 fd = dso__data_fd(dso, machine);
2320 if (fd < 0)
2321 return -1;
2323 do {
2324 u64 cache_offset;
2326 ret = -ENOMEM;
2328 cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
2329 if (!cache)
2330 break;
2332 cache_offset = offset & DSO__DATA_CACHE_MASK;
2333 ret = -EINVAL;
2335 if (-1 == lseek(fd, cache_offset, SEEK_SET))
2336 break;
2338 ret = read(fd, cache->data, DSO__DATA_CACHE_SIZE);
2339 if (ret <= 0)
2340 break;
2342 cache->offset = cache_offset;
2343 cache->size = ret;
2344 dso_cache__insert(&dso->cache, cache);
2346 ret = dso_cache__memcpy(cache, offset, data, size);
2348 } while (0);
2350 if (ret <= 0)
2351 free(cache);
2353 close(fd);
2354 return ret;
2357 static ssize_t dso_cache_read(struct dso *dso, struct machine *machine,
2358 u64 offset, u8 *data, ssize_t size)
2360 struct dso_cache *cache;
2362 cache = dso_cache__find(&dso->cache, offset);
2363 if (cache)
2364 return dso_cache__memcpy(cache, offset, data, size);
2365 else
2366 return dso_cache__read(dso, machine, offset, data, size);
2369 ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
2370 u64 offset, u8 *data, ssize_t size)
2372 ssize_t r = 0;
2373 u8 *p = data;
2375 do {
2376 ssize_t ret;
2378 ret = dso_cache_read(dso, machine, offset, p, size);
2379 if (ret < 0)
2380 return ret;
2382 /* Reached EOF, return what we have. */
2383 if (!ret)
2384 break;
2386 BUG_ON(ret > size);
2388 r += ret;
2389 p += ret;
2390 offset += ret;
2391 size -= ret;
2393 } while (size);
2395 return r;
2398 ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
2399 struct machine *machine, u64 addr,
2400 u8 *data, ssize_t size)
2402 u64 offset = map->map_ip(map, addr);
2403 return dso__data_read_offset(dso, machine, offset, data, size);