]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - tools/perf/util/symbol.c
perf symbols: Make sym->end be the first address after the symbol range
[mirror_ubuntu-artful-kernel.git] / tools / perf / util / symbol.c
CommitLineData
5aab621b
ACM
1#include <dirent.h>
2#include <errno.h>
5aab621b
ACM
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>
9486aa38 11#include <inttypes.h>
b36f19d5 12#include "build-id.h"
e334c726 13#include "util.h"
8a6c5b26 14#include "debug.h"
69d2591a 15#include "machine.h"
a2928c42 16#include "symbol.h"
5aab621b 17#include "strlist.h"
0a7e6d1b 18#include "header.h"
a2928c42 19
a2928c42 20#include <elf.h>
f1617b40 21#include <limits.h>
c506c96b 22#include <symbol/kallsyms.h>
439d473b 23#include <sys/utsname.h>
2cdbc46d 24
aeafcbaf 25static int dso__load_kernel_sym(struct dso *dso, struct map *map,
9de89fe7 26 symbol_filter_t filter);
aeafcbaf 27static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
a1645ce1 28 symbol_filter_t filter);
3f067dca
ACM
29int vmlinux_path__nr_entries;
30char **vmlinux_path;
439d473b 31
75be6cf4 32struct symbol_conf symbol_conf = {
e511db5e
NK
33 .use_modules = true,
34 .try_vmlinux_path = true,
35 .annotate_src = true,
36 .demangle = true,
763122ad 37 .demangle_kernel = false,
e511db5e 38 .cumulate_callchain = true,
c8302367 39 .show_hist_headers = true,
e511db5e 40 .symfs = "",
b32d133a
ACM
41};
42
44f24cb3
JO
43static enum dso_binary_type binary_type_symtab[] = {
44 DSO_BINARY_TYPE__KALLSYMS,
45 DSO_BINARY_TYPE__GUEST_KALLSYMS,
46 DSO_BINARY_TYPE__JAVA_JIT,
47 DSO_BINARY_TYPE__DEBUGLINK,
48 DSO_BINARY_TYPE__BUILD_ID_CACHE,
49 DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
50 DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
51 DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
52 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
53 DSO_BINARY_TYPE__GUEST_KMODULE,
54 DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE,
9cd00941 55 DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO,
44f24cb3
JO
56 DSO_BINARY_TYPE__NOT_FOUND,
57};
58
028df767 59#define DSO_BINARY_TYPE__SYMTAB_CNT ARRAY_SIZE(binary_type_symtab)
44f24cb3 60
36a3e646 61bool symbol_type__is_a(char symbol_type, enum map_type map_type)
6893d4ee 62{
31877908
AB
63 symbol_type = toupper(symbol_type);
64
6893d4ee
ACM
65 switch (map_type) {
66 case MAP__FUNCTION:
67 return symbol_type == 'T' || symbol_type == 'W';
f1dfa0b1 68 case MAP__VARIABLE:
31877908 69 return symbol_type == 'D';
6893d4ee
ACM
70 default:
71 return false;
72 }
73}
74
694bf407
AB
75static int prefix_underscores_count(const char *str)
76{
77 const char *tail = str;
78
79 while (*tail == '_')
80 tail++;
81
82 return tail - str;
83}
84
85#define SYMBOL_A 0
86#define SYMBOL_B 1
87
88static int choose_best_symbol(struct symbol *syma, struct symbol *symb)
89{
90 s64 a;
91 s64 b;
3445432b 92 size_t na, nb;
694bf407
AB
93
94 /* Prefer a symbol with non zero length */
95 a = syma->end - syma->start;
96 b = symb->end - symb->start;
97 if ((b == 0) && (a > 0))
98 return SYMBOL_A;
99 else if ((a == 0) && (b > 0))
100 return SYMBOL_B;
101
102 /* Prefer a non weak symbol over a weak one */
103 a = syma->binding == STB_WEAK;
104 b = symb->binding == STB_WEAK;
105 if (b && !a)
106 return SYMBOL_A;
107 if (a && !b)
108 return SYMBOL_B;
109
110 /* Prefer a global symbol over a non global one */
111 a = syma->binding == STB_GLOBAL;
112 b = symb->binding == STB_GLOBAL;
113 if (a && !b)
114 return SYMBOL_A;
115 if (b && !a)
116 return SYMBOL_B;
117
118 /* Prefer a symbol with less underscores */
119 a = prefix_underscores_count(syma->name);
120 b = prefix_underscores_count(symb->name);
121 if (b > a)
122 return SYMBOL_A;
123 else if (a > b)
124 return SYMBOL_B;
125
3445432b
AH
126 /* Choose the symbol with the longest name */
127 na = strlen(syma->name);
128 nb = strlen(symb->name);
129 if (na > nb)
694bf407 130 return SYMBOL_A;
3445432b 131 else if (na < nb)
694bf407 132 return SYMBOL_B;
3445432b
AH
133
134 /* Avoid "SyS" kernel syscall aliases */
135 if (na >= 3 && !strncmp(syma->name, "SyS", 3))
136 return SYMBOL_B;
137 if (na >= 10 && !strncmp(syma->name, "compat_SyS", 10))
138 return SYMBOL_B;
139
140 return SYMBOL_A;
694bf407
AB
141}
142
e5a1845f 143void symbols__fixup_duplicate(struct rb_root *symbols)
694bf407
AB
144{
145 struct rb_node *nd;
146 struct symbol *curr, *next;
147
148 nd = rb_first(symbols);
149
150 while (nd) {
151 curr = rb_entry(nd, struct symbol, rb_node);
152again:
153 nd = rb_next(&curr->rb_node);
154 next = rb_entry(nd, struct symbol, rb_node);
155
156 if (!nd)
157 break;
158
159 if (curr->start != next->start)
160 continue;
161
162 if (choose_best_symbol(curr, next) == SYMBOL_A) {
163 rb_erase(&next->rb_node, symbols);
d4f74eb8 164 symbol__delete(next);
694bf407
AB
165 goto again;
166 } else {
167 nd = rb_next(&curr->rb_node);
168 rb_erase(&curr->rb_node, symbols);
d4f74eb8 169 symbol__delete(curr);
694bf407
AB
170 }
171 }
172}
173
e5a1845f 174void symbols__fixup_end(struct rb_root *symbols)
af427bf5 175{
aeafcbaf 176 struct rb_node *nd, *prevnd = rb_first(symbols);
2e538c4a 177 struct symbol *curr, *prev;
af427bf5
ACM
178
179 if (prevnd == NULL)
180 return;
181
2e538c4a
ACM
182 curr = rb_entry(prevnd, struct symbol, rb_node);
183
af427bf5 184 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
2e538c4a
ACM
185 prev = curr;
186 curr = rb_entry(nd, struct symbol, rb_node);
af427bf5 187
3b01a413 188 if (prev->end == prev->start && prev->end != curr->start)
2c241bd3 189 prev->end = curr->start;
af427bf5 190 }
2e538c4a
ACM
191
192 /* Last entry */
193 if (curr->end == curr->start)
194 curr->end = roundup(curr->start, 4096);
af427bf5
ACM
195}
196
e5a1845f 197void __map_groups__fixup_end(struct map_groups *mg, enum map_type type)
af427bf5
ACM
198{
199 struct map *prev, *curr;
aeafcbaf 200 struct rb_node *nd, *prevnd = rb_first(&mg->maps[type]);
af427bf5
ACM
201
202 if (prevnd == NULL)
203 return;
204
205 curr = rb_entry(prevnd, struct map, rb_node);
af427bf5
ACM
206
207 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
208 prev = curr;
209 curr = rb_entry(nd, struct map, rb_node);
eba85230 210 prev->end = curr->start;
2e538c4a 211 }
90c83218
ACM
212
213 /*
214 * We still haven't the actual symbols, so guess the
215 * last map final address.
216 */
9d1faba5 217 curr->end = ~0ULL;
af427bf5
ACM
218}
219
e5a1845f 220struct symbol *symbol__new(u64 start, u64 len, u8 binding, const char *name)
a2928c42 221{
0085c954 222 size_t namelen = strlen(name) + 1;
aeafcbaf
ACM
223 struct symbol *sym = calloc(1, (symbol_conf.priv_size +
224 sizeof(*sym) + namelen));
225 if (sym == NULL)
0b73da3f
IM
226 return NULL;
227
75be6cf4 228 if (symbol_conf.priv_size)
aeafcbaf 229 sym = ((void *)sym) + symbol_conf.priv_size;
e4204992 230
aeafcbaf 231 sym->start = start;
2c241bd3 232 sym->end = len ? start + len : start;
aeafcbaf
ACM
233 sym->binding = binding;
234 sym->namelen = namelen - 1;
e4204992 235
aeafcbaf
ACM
236 pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n",
237 __func__, name, start, sym->end);
238 memcpy(sym->name, name, namelen);
a2928c42 239
aeafcbaf 240 return sym;
a2928c42
ACM
241}
242
aeafcbaf 243void symbol__delete(struct symbol *sym)
a2928c42 244{
aeafcbaf 245 free(((void *)sym) - symbol_conf.priv_size);
a2928c42
ACM
246}
247
cdd059d7 248size_t symbol__fprintf(struct symbol *sym, FILE *fp)
a2928c42 249{
9486aa38 250 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %c %s\n",
aeafcbaf
ACM
251 sym->start, sym->end,
252 sym->binding == STB_GLOBAL ? 'g' :
253 sym->binding == STB_LOCAL ? 'l' : 'w',
254 sym->name);
a2928c42
ACM
255}
256
a978f2ab
AN
257size_t symbol__fprintf_symname_offs(const struct symbol *sym,
258 const struct addr_location *al, FILE *fp)
547a92e0 259{
a978f2ab
AN
260 unsigned long offset;
261 size_t length;
262
263 if (sym && sym->name) {
264 length = fprintf(fp, "%s", sym->name);
265 if (al) {
0b8c25d9
DA
266 if (al->addr < sym->end)
267 offset = al->addr - sym->start;
268 else
269 offset = al->addr - al->map->start - sym->start;
a978f2ab
AN
270 length += fprintf(fp, "+0x%lx", offset);
271 }
272 return length;
273 } else
274 return fprintf(fp, "[unknown]");
275}
547a92e0 276
a978f2ab
AN
277size_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp)
278{
279 return symbol__fprintf_symname_offs(sym, NULL, fp);
547a92e0
AN
280}
281
cdd059d7 282void symbols__delete(struct rb_root *symbols)
a2928c42
ACM
283{
284 struct symbol *pos;
aeafcbaf 285 struct rb_node *next = rb_first(symbols);
a2928c42
ACM
286
287 while (next) {
288 pos = rb_entry(next, struct symbol, rb_node);
289 next = rb_next(&pos->rb_node);
aeafcbaf 290 rb_erase(&pos->rb_node, symbols);
00a192b3 291 symbol__delete(pos);
a2928c42
ACM
292 }
293}
294
e5a1845f 295void symbols__insert(struct rb_root *symbols, struct symbol *sym)
a2928c42 296{
aeafcbaf 297 struct rb_node **p = &symbols->rb_node;
a2928c42 298 struct rb_node *parent = NULL;
9cffa8d5 299 const u64 ip = sym->start;
a2928c42
ACM
300 struct symbol *s;
301
302 while (*p != NULL) {
303 parent = *p;
304 s = rb_entry(parent, struct symbol, rb_node);
305 if (ip < s->start)
306 p = &(*p)->rb_left;
307 else
308 p = &(*p)->rb_right;
309 }
310 rb_link_node(&sym->rb_node, parent, p);
aeafcbaf 311 rb_insert_color(&sym->rb_node, symbols);
a2928c42
ACM
312}
313
aeafcbaf 314static struct symbol *symbols__find(struct rb_root *symbols, u64 ip)
a2928c42
ACM
315{
316 struct rb_node *n;
317
aeafcbaf 318 if (symbols == NULL)
a2928c42
ACM
319 return NULL;
320
aeafcbaf 321 n = symbols->rb_node;
a2928c42
ACM
322
323 while (n) {
324 struct symbol *s = rb_entry(n, struct symbol, rb_node);
325
326 if (ip < s->start)
327 n = n->rb_left;
2c241bd3 328 else if (ip >= s->end)
a2928c42
ACM
329 n = n->rb_right;
330 else
331 return s;
332 }
333
334 return NULL;
335}
336
8e0cf965
AH
337static struct symbol *symbols__first(struct rb_root *symbols)
338{
339 struct rb_node *n = rb_first(symbols);
340
341 if (n)
342 return rb_entry(n, struct symbol, rb_node);
343
344 return NULL;
345}
346
9c00a81b
AH
347static struct symbol *symbols__next(struct symbol *sym)
348{
349 struct rb_node *n = rb_next(&sym->rb_node);
350
351 if (n)
352 return rb_entry(n, struct symbol, rb_node);
353
354 return NULL;
355}
356
79406cd7
ACM
357struct symbol_name_rb_node {
358 struct rb_node rb_node;
359 struct symbol sym;
360};
361
aeafcbaf 362static void symbols__insert_by_name(struct rb_root *symbols, struct symbol *sym)
79406cd7 363{
aeafcbaf 364 struct rb_node **p = &symbols->rb_node;
79406cd7 365 struct rb_node *parent = NULL;
02a9d037
RV
366 struct symbol_name_rb_node *symn, *s;
367
368 symn = container_of(sym, struct symbol_name_rb_node, sym);
79406cd7
ACM
369
370 while (*p != NULL) {
371 parent = *p;
372 s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
373 if (strcmp(sym->name, s->sym.name) < 0)
374 p = &(*p)->rb_left;
375 else
376 p = &(*p)->rb_right;
377 }
378 rb_link_node(&symn->rb_node, parent, p);
aeafcbaf 379 rb_insert_color(&symn->rb_node, symbols);
79406cd7
ACM
380}
381
aeafcbaf
ACM
382static void symbols__sort_by_name(struct rb_root *symbols,
383 struct rb_root *source)
79406cd7
ACM
384{
385 struct rb_node *nd;
386
387 for (nd = rb_first(source); nd; nd = rb_next(nd)) {
388 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
aeafcbaf 389 symbols__insert_by_name(symbols, pos);
79406cd7
ACM
390 }
391}
392
aeafcbaf
ACM
393static struct symbol *symbols__find_by_name(struct rb_root *symbols,
394 const char *name)
79406cd7
ACM
395{
396 struct rb_node *n;
397
aeafcbaf 398 if (symbols == NULL)
79406cd7
ACM
399 return NULL;
400
aeafcbaf 401 n = symbols->rb_node;
79406cd7
ACM
402
403 while (n) {
404 struct symbol_name_rb_node *s;
405 int cmp;
406
407 s = rb_entry(n, struct symbol_name_rb_node, rb_node);
408 cmp = strcmp(name, s->sym.name);
409
410 if (cmp < 0)
411 n = n->rb_left;
412 else if (cmp > 0)
413 n = n->rb_right;
414 else
415 return &s->sym;
416 }
417
418 return NULL;
419}
420
aeafcbaf 421struct symbol *dso__find_symbol(struct dso *dso,
79406cd7 422 enum map_type type, u64 addr)
fcf1203a 423{
aeafcbaf 424 return symbols__find(&dso->symbols[type], addr);
fcf1203a
ACM
425}
426
9c00a81b 427struct symbol *dso__first_symbol(struct dso *dso, enum map_type type)
8e0cf965
AH
428{
429 return symbols__first(&dso->symbols[type]);
9c00a81b
AH
430}
431
432struct symbol *dso__next_symbol(struct symbol *sym)
433{
434 return symbols__next(sym);
8e0cf965
AH
435}
436
aeafcbaf 437struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
79406cd7
ACM
438 const char *name)
439{
aeafcbaf 440 return symbols__find_by_name(&dso->symbol_names[type], name);
79406cd7
ACM
441}
442
aeafcbaf 443void dso__sort_by_name(struct dso *dso, enum map_type type)
79406cd7 444{
aeafcbaf
ACM
445 dso__set_sorted_by_name(dso, type);
446 return symbols__sort_by_name(&dso->symbol_names[type],
447 &dso->symbols[type]);
79406cd7
ACM
448}
449
aeafcbaf
ACM
450size_t dso__fprintf_symbols_by_name(struct dso *dso,
451 enum map_type type, FILE *fp)
90f18e63
SD
452{
453 size_t ret = 0;
454 struct rb_node *nd;
455 struct symbol_name_rb_node *pos;
456
aeafcbaf 457 for (nd = rb_first(&dso->symbol_names[type]); nd; nd = rb_next(nd)) {
90f18e63
SD
458 pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
459 fprintf(fp, "%s\n", pos->sym.name);
460 }
461
462 return ret;
463}
464
316d70d6
AH
465int modules__parse(const char *filename, void *arg,
466 int (*process_module)(void *arg, const char *name,
467 u64 start))
468{
469 char *line = NULL;
470 size_t n;
471 FILE *file;
472 int err = 0;
473
474 file = fopen(filename, "r");
475 if (file == NULL)
476 return -1;
477
478 while (1) {
479 char name[PATH_MAX];
480 u64 start;
481 char *sep;
482 ssize_t line_len;
483
484 line_len = getline(&line, &n, file);
485 if (line_len < 0) {
486 if (feof(file))
487 break;
488 err = -1;
489 goto out;
490 }
491
492 if (!line) {
493 err = -1;
494 goto out;
495 }
496
497 line[--line_len] = '\0'; /* \n */
498
499 sep = strrchr(line, 'x');
500 if (sep == NULL)
501 continue;
502
503 hex2u64(sep + 1, &start);
504
505 sep = strchr(line, ' ');
506 if (sep == NULL)
507 continue;
508
509 *sep = '\0';
510
511 scnprintf(name, sizeof(name), "[%s]", line);
512
513 err = process_module(arg, name, start);
514 if (err)
515 break;
516 }
517out:
518 free(line);
519 fclose(file);
520 return err;
521}
522
682b335a
ACM
523struct process_kallsyms_args {
524 struct map *map;
525 struct dso *dso;
526};
527
e7110b9f
ACM
528/*
529 * These are symbols in the kernel image, so make sure that
530 * sym is from a kernel DSO.
531 */
82d1deb0
DA
532bool symbol__is_idle(struct symbol *sym)
533{
534 const char * const idle_symbols[] = {
535 "cpu_idle",
e0336ed6 536 "cpu_startup_entry",
82d1deb0
DA
537 "intel_idle",
538 "default_idle",
539 "native_safe_halt",
540 "enter_idle",
541 "exit_idle",
542 "mwait_idle",
543 "mwait_idle_with_hints",
544 "poll_idle",
545 "ppc64_runlatch_off",
546 "pseries_dedicated_idle_sleep",
547 NULL
548 };
549
550 int i;
551
552 if (!sym)
553 return false;
554
555 for (i = 0; idle_symbols[i]; i++) {
556 if (!strcmp(idle_symbols[i], sym->name))
557 return true;
558 }
559
560 return false;
561}
562
682b335a 563static int map__process_kallsym_symbol(void *arg, const char *name,
82151520 564 char type, u64 start)
682b335a
ACM
565{
566 struct symbol *sym;
567 struct process_kallsyms_args *a = arg;
568 struct rb_root *root = &a->dso->symbols[a->map->type];
569
570 if (!symbol_type__is_a(type, a->map->type))
571 return 0;
572
82151520
CS
573 /*
574 * module symbols are not sorted so we add all
575 * symbols, setting length to 0, and rely on
576 * symbols__fixup_end() to fix it up.
577 */
578 sym = symbol__new(start, 0, kallsyms2elf_type(type), name);
682b335a
ACM
579 if (sym == NULL)
580 return -ENOMEM;
581 /*
582 * We will pass the symbols to the filter later, in
583 * map__split_kallsyms, when we have split the maps per module
584 */
585 symbols__insert(root, sym);
a1645ce1 586
682b335a
ACM
587 return 0;
588}
589
590/*
591 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
592 * so that we can in the next step set the symbol ->end address and then
593 * call kernel_maps__split_kallsyms.
594 */
aeafcbaf 595static int dso__load_all_kallsyms(struct dso *dso, const char *filename,
9e201442 596 struct map *map)
682b335a 597{
aeafcbaf 598 struct process_kallsyms_args args = { .map = map, .dso = dso, };
9e201442 599 return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
682b335a
ACM
600}
601
8e0cf965
AH
602static int dso__split_kallsyms_for_kcore(struct dso *dso, struct map *map,
603 symbol_filter_t filter)
604{
605 struct map_groups *kmaps = map__kmap(map)->kmaps;
606 struct map *curr_map;
607 struct symbol *pos;
608 int count = 0, moved = 0;
609 struct rb_root *root = &dso->symbols[map->type];
610 struct rb_node *next = rb_first(root);
611
612 while (next) {
613 char *module;
614
615 pos = rb_entry(next, struct symbol, rb_node);
616 next = rb_next(&pos->rb_node);
617
618 module = strchr(pos->name, '\t');
619 if (module)
620 *module = '\0';
621
622 curr_map = map_groups__find(kmaps, map->type, pos->start);
623
624 if (!curr_map || (filter && filter(curr_map, pos))) {
625 rb_erase(&pos->rb_node, root);
626 symbol__delete(pos);
627 } else {
628 pos->start -= curr_map->start - curr_map->pgoff;
629 if (pos->end)
630 pos->end -= curr_map->start - curr_map->pgoff;
631 if (curr_map != map) {
632 rb_erase(&pos->rb_node, root);
633 symbols__insert(
634 &curr_map->dso->symbols[curr_map->type],
635 pos);
636 ++moved;
637 } else {
638 ++count;
639 }
640 }
641 }
642
643 /* Symbols have been adjusted */
644 dso->adjust_symbols = 1;
645
646 return count + moved;
647}
648
2e538c4a
ACM
649/*
650 * Split the symbols into maps, making sure there are no overlaps, i.e. the
651 * kernel range is broken in several maps, named [kernel].N, as we don't have
652 * the original ELF section names vmlinux have.
653 */
d9b62aba 654static int dso__split_kallsyms(struct dso *dso, struct map *map, u64 delta,
9de89fe7 655 symbol_filter_t filter)
2e538c4a 656{
9de89fe7 657 struct map_groups *kmaps = map__kmap(map)->kmaps;
23346f21 658 struct machine *machine = kmaps->machine;
4e06255f 659 struct map *curr_map = map;
2e538c4a 660 struct symbol *pos;
8a953312 661 int count = 0, moved = 0;
aeafcbaf 662 struct rb_root *root = &dso->symbols[map->type];
4e06255f 663 struct rb_node *next = rb_first(root);
2e538c4a
ACM
664 int kernel_range = 0;
665
666 while (next) {
667 char *module;
668
669 pos = rb_entry(next, struct symbol, rb_node);
670 next = rb_next(&pos->rb_node);
671
672 module = strchr(pos->name, '\t');
673 if (module) {
75be6cf4 674 if (!symbol_conf.use_modules)
1de8e245
ACM
675 goto discard_symbol;
676
2e538c4a
ACM
677 *module++ = '\0';
678
b7cece76 679 if (strcmp(curr_map->dso->short_name, module)) {
a1645ce1 680 if (curr_map != map &&
aeafcbaf 681 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
23346f21 682 machine__is_default_guest(machine)) {
a1645ce1
ZY
683 /*
684 * We assume all symbols of a module are
685 * continuous in * kallsyms, so curr_map
686 * points to a module and all its
687 * symbols are in its kmap. Mark it as
688 * loaded.
689 */
690 dso__set_loaded(curr_map->dso,
691 curr_map->type);
692 }
693
694 curr_map = map_groups__find_by_name(kmaps,
695 map->type, module);
4e06255f 696 if (curr_map == NULL) {
2f51903b 697 pr_debug("%s/proc/{kallsyms,modules} "
b7cece76 698 "inconsistency while looking "
a1645ce1 699 "for \"%s\" module!\n",
23346f21 700 machine->root_dir, module);
a1645ce1
ZY
701 curr_map = map;
702 goto discard_symbol;
af427bf5 703 }
b7cece76 704
a1645ce1 705 if (curr_map->dso->loaded &&
23346f21 706 !machine__is_default_guest(machine))
b7cece76 707 goto discard_symbol;
af427bf5 708 }
2e538c4a
ACM
709 /*
710 * So that we look just like we get from .ko files,
711 * i.e. not prelinked, relative to map->start.
712 */
4e06255f
ACM
713 pos->start = curr_map->map_ip(curr_map, pos->start);
714 pos->end = curr_map->map_ip(curr_map, pos->end);
715 } else if (curr_map != map) {
2e538c4a 716 char dso_name[PATH_MAX];
aeafcbaf 717 struct dso *ndso;
2e538c4a 718
d9b62aba
AH
719 if (delta) {
720 /* Kernel was relocated at boot time */
721 pos->start -= delta;
722 pos->end -= delta;
723 }
724
8a953312
ACM
725 if (count == 0) {
726 curr_map = map;
727 goto filter_symbol;
728 }
729
aeafcbaf 730 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
a1645ce1
ZY
731 snprintf(dso_name, sizeof(dso_name),
732 "[guest.kernel].%d",
733 kernel_range++);
734 else
735 snprintf(dso_name, sizeof(dso_name),
736 "[kernel].%d",
737 kernel_range++);
2e538c4a 738
aeafcbaf
ACM
739 ndso = dso__new(dso_name);
740 if (ndso == NULL)
2e538c4a
ACM
741 return -1;
742
aeafcbaf 743 ndso->kernel = dso->kernel;
a1645ce1 744
aeafcbaf 745 curr_map = map__new2(pos->start, ndso, map->type);
37fe5fcb 746 if (curr_map == NULL) {
aeafcbaf 747 dso__delete(ndso);
2e538c4a
ACM
748 return -1;
749 }
a2928c42 750
4e06255f 751 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
9de89fe7 752 map_groups__insert(kmaps, curr_map);
2e538c4a 753 ++kernel_range;
d9b62aba
AH
754 } else if (delta) {
755 /* Kernel was relocated at boot time */
756 pos->start -= delta;
757 pos->end -= delta;
2e538c4a 758 }
8a953312 759filter_symbol:
4e06255f 760 if (filter && filter(curr_map, pos)) {
1de8e245 761discard_symbol: rb_erase(&pos->rb_node, root);
00a192b3 762 symbol__delete(pos);
2e538c4a 763 } else {
4e06255f
ACM
764 if (curr_map != map) {
765 rb_erase(&pos->rb_node, root);
766 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
8a953312
ACM
767 ++moved;
768 } else
769 ++count;
9974f496 770 }
a2928c42
ACM
771 }
772
a1645ce1 773 if (curr_map != map &&
aeafcbaf 774 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
23346f21 775 machine__is_default_guest(kmaps->machine)) {
a1645ce1
ZY
776 dso__set_loaded(curr_map->dso, curr_map->type);
777 }
778
8a953312 779 return count + moved;
2e538c4a 780}
a2928c42 781
3f067dca
ACM
782bool symbol__restricted_filename(const char *filename,
783 const char *restricted_filename)
ec80fde7
ACM
784{
785 bool restricted = false;
786
787 if (symbol_conf.kptr_restrict) {
788 char *r = realpath(filename, NULL);
789
790 if (r != NULL) {
791 restricted = strcmp(r, restricted_filename) == 0;
792 free(r);
793 return restricted;
794 }
795 }
796
797 return restricted;
798}
799
52afdaf9
AH
800struct module_info {
801 struct rb_node rb_node;
802 char *name;
803 u64 start;
8e0cf965
AH
804};
805
52afdaf9 806static void add_module(struct module_info *mi, struct rb_root *modules)
8e0cf965 807{
52afdaf9
AH
808 struct rb_node **p = &modules->rb_node;
809 struct rb_node *parent = NULL;
810 struct module_info *m;
8e0cf965 811
52afdaf9
AH
812 while (*p != NULL) {
813 parent = *p;
814 m = rb_entry(parent, struct module_info, rb_node);
815 if (strcmp(mi->name, m->name) < 0)
816 p = &(*p)->rb_left;
817 else
818 p = &(*p)->rb_right;
819 }
820 rb_link_node(&mi->rb_node, parent, p);
821 rb_insert_color(&mi->rb_node, modules);
822}
823
824static void delete_modules(struct rb_root *modules)
825{
826 struct module_info *mi;
827 struct rb_node *next = rb_first(modules);
828
829 while (next) {
830 mi = rb_entry(next, struct module_info, rb_node);
831 next = rb_next(&mi->rb_node);
832 rb_erase(&mi->rb_node, modules);
74cf249d 833 zfree(&mi->name);
52afdaf9
AH
834 free(mi);
835 }
836}
837
838static struct module_info *find_module(const char *name,
839 struct rb_root *modules)
840{
841 struct rb_node *n = modules->rb_node;
842
843 while (n) {
844 struct module_info *m;
845 int cmp;
846
847 m = rb_entry(n, struct module_info, rb_node);
848 cmp = strcmp(name, m->name);
849 if (cmp < 0)
850 n = n->rb_left;
851 else if (cmp > 0)
852 n = n->rb_right;
853 else
854 return m;
855 }
856
857 return NULL;
858}
859
860static int __read_proc_modules(void *arg, const char *name, u64 start)
861{
862 struct rb_root *modules = arg;
863 struct module_info *mi;
864
865 mi = zalloc(sizeof(struct module_info));
866 if (!mi)
8e0cf965
AH
867 return -ENOMEM;
868
52afdaf9
AH
869 mi->name = strdup(name);
870 mi->start = start;
8e0cf965 871
52afdaf9
AH
872 if (!mi->name) {
873 free(mi);
874 return -ENOMEM;
875 }
876
877 add_module(mi, modules);
878
879 return 0;
880}
881
882static int read_proc_modules(const char *filename, struct rb_root *modules)
883{
884 if (symbol__restricted_filename(filename, "/proc/modules"))
885 return -1;
886
887 if (modules__parse(filename, modules, __read_proc_modules)) {
888 delete_modules(modules);
889 return -1;
890 }
8e0cf965
AH
891
892 return 0;
893}
894
fc1b691d
AH
895int compare_proc_modules(const char *from, const char *to)
896{
897 struct rb_root from_modules = RB_ROOT;
898 struct rb_root to_modules = RB_ROOT;
899 struct rb_node *from_node, *to_node;
900 struct module_info *from_m, *to_m;
901 int ret = -1;
902
903 if (read_proc_modules(from, &from_modules))
904 return -1;
905
906 if (read_proc_modules(to, &to_modules))
907 goto out_delete_from;
908
909 from_node = rb_first(&from_modules);
910 to_node = rb_first(&to_modules);
911 while (from_node) {
912 if (!to_node)
913 break;
914
915 from_m = rb_entry(from_node, struct module_info, rb_node);
916 to_m = rb_entry(to_node, struct module_info, rb_node);
917
918 if (from_m->start != to_m->start ||
919 strcmp(from_m->name, to_m->name))
920 break;
921
922 from_node = rb_next(from_node);
923 to_node = rb_next(to_node);
924 }
925
926 if (!from_node && !to_node)
927 ret = 0;
928
929 delete_modules(&to_modules);
930out_delete_from:
931 delete_modules(&from_modules);
932
933 return ret;
934}
935
52afdaf9
AH
936static int do_validate_kcore_modules(const char *filename, struct map *map,
937 struct map_groups *kmaps)
938{
939 struct rb_root modules = RB_ROOT;
940 struct map *old_map;
941 int err;
942
943 err = read_proc_modules(filename, &modules);
944 if (err)
945 return err;
946
947 old_map = map_groups__first(kmaps, map->type);
948 while (old_map) {
949 struct map *next = map_groups__next(old_map);
950 struct module_info *mi;
951
952 if (old_map == map || old_map->start == map->start) {
953 /* The kernel map */
954 old_map = next;
955 continue;
956 }
957
958 /* Module must be in memory at the same address */
959 mi = find_module(old_map->dso->short_name, &modules);
960 if (!mi || mi->start != old_map->start) {
961 err = -EINVAL;
962 goto out;
963 }
964
965 old_map = next;
966 }
967out:
968 delete_modules(&modules);
969 return err;
970}
971
8e0cf965 972/*
52afdaf9 973 * If kallsyms is referenced by name then we look for filename in the same
8e0cf965
AH
974 * directory.
975 */
52afdaf9
AH
976static bool filename_from_kallsyms_filename(char *filename,
977 const char *base_name,
978 const char *kallsyms_filename)
8e0cf965
AH
979{
980 char *name;
981
52afdaf9
AH
982 strcpy(filename, kallsyms_filename);
983 name = strrchr(filename, '/');
8e0cf965
AH
984 if (!name)
985 return false;
986
52afdaf9
AH
987 name += 1;
988
989 if (!strcmp(name, "kallsyms")) {
990 strcpy(name, base_name);
8e0cf965
AH
991 return true;
992 }
993
994 return false;
995}
996
52afdaf9
AH
997static int validate_kcore_modules(const char *kallsyms_filename,
998 struct map *map)
999{
1000 struct map_groups *kmaps = map__kmap(map)->kmaps;
1001 char modules_filename[PATH_MAX];
1002
1003 if (!filename_from_kallsyms_filename(modules_filename, "modules",
1004 kallsyms_filename))
1005 return -EINVAL;
1006
1007 if (do_validate_kcore_modules(modules_filename, map, kmaps))
1008 return -EINVAL;
1009
1010 return 0;
1011}
1012
a00d28cb
AH
1013static int validate_kcore_addresses(const char *kallsyms_filename,
1014 struct map *map)
1015{
1016 struct kmap *kmap = map__kmap(map);
1017
1018 if (kmap->ref_reloc_sym && kmap->ref_reloc_sym->name) {
1019 u64 start;
1020
1021 start = kallsyms__get_function_start(kallsyms_filename,
1022 kmap->ref_reloc_sym->name);
1023 if (start != kmap->ref_reloc_sym->addr)
1024 return -EINVAL;
1025 }
1026
1027 return validate_kcore_modules(kallsyms_filename, map);
1028}
1029
52afdaf9
AH
1030struct kcore_mapfn_data {
1031 struct dso *dso;
1032 enum map_type type;
1033 struct list_head maps;
1034};
1035
1036static int kcore_mapfn(u64 start, u64 len, u64 pgoff, void *data)
1037{
1038 struct kcore_mapfn_data *md = data;
1039 struct map *map;
1040
1041 map = map__new2(start, md->dso, md->type);
1042 if (map == NULL)
1043 return -ENOMEM;
1044
1045 map->end = map->start + len;
1046 map->pgoff = pgoff;
1047
1048 list_add(&map->node, &md->maps);
1049
1050 return 0;
1051}
1052
8e0cf965
AH
1053static int dso__load_kcore(struct dso *dso, struct map *map,
1054 const char *kallsyms_filename)
1055{
1056 struct map_groups *kmaps = map__kmap(map)->kmaps;
1057 struct machine *machine = kmaps->machine;
1058 struct kcore_mapfn_data md;
1059 struct map *old_map, *new_map, *replacement_map = NULL;
1060 bool is_64_bit;
1061 int err, fd;
1062 char kcore_filename[PATH_MAX];
1063 struct symbol *sym;
1064
1065 /* This function requires that the map is the kernel map */
1066 if (map != machine->vmlinux_maps[map->type])
1067 return -EINVAL;
1068
52afdaf9
AH
1069 if (!filename_from_kallsyms_filename(kcore_filename, "kcore",
1070 kallsyms_filename))
1071 return -EINVAL;
1072
a00d28cb
AH
1073 /* Modules and kernel must be present at their original addresses */
1074 if (validate_kcore_addresses(kallsyms_filename, map))
8e0cf965
AH
1075 return -EINVAL;
1076
1077 md.dso = dso;
1078 md.type = map->type;
1079 INIT_LIST_HEAD(&md.maps);
1080
1081 fd = open(kcore_filename, O_RDONLY);
1082 if (fd < 0)
1083 return -EINVAL;
1084
1085 /* Read new maps into temporary lists */
1086 err = file__read_maps(fd, md.type == MAP__FUNCTION, kcore_mapfn, &md,
1087 &is_64_bit);
1088 if (err)
1089 goto out_err;
c6d8f2a4 1090 dso->is_64_bit = is_64_bit;
8e0cf965
AH
1091
1092 if (list_empty(&md.maps)) {
1093 err = -EINVAL;
1094 goto out_err;
1095 }
1096
1097 /* Remove old maps */
1098 old_map = map_groups__first(kmaps, map->type);
1099 while (old_map) {
1100 struct map *next = map_groups__next(old_map);
1101
1102 if (old_map != map)
1103 map_groups__remove(kmaps, old_map);
1104 old_map = next;
1105 }
1106
1107 /* Find the kernel map using the first symbol */
1108 sym = dso__first_symbol(dso, map->type);
1109 list_for_each_entry(new_map, &md.maps, node) {
1110 if (sym && sym->start >= new_map->start &&
1111 sym->start < new_map->end) {
1112 replacement_map = new_map;
1113 break;
1114 }
1115 }
1116
1117 if (!replacement_map)
1118 replacement_map = list_entry(md.maps.next, struct map, node);
1119
1120 /* Add new maps */
1121 while (!list_empty(&md.maps)) {
1122 new_map = list_entry(md.maps.next, struct map, node);
1123 list_del(&new_map->node);
1124 if (new_map == replacement_map) {
1125 map->start = new_map->start;
1126 map->end = new_map->end;
1127 map->pgoff = new_map->pgoff;
1128 map->map_ip = new_map->map_ip;
1129 map->unmap_ip = new_map->unmap_ip;
1130 map__delete(new_map);
1131 /* Ensure maps are correctly ordered */
1132 map_groups__remove(kmaps, map);
1133 map_groups__insert(kmaps, map);
1134 } else {
1135 map_groups__insert(kmaps, new_map);
1136 }
1137 }
1138
1139 /*
1140 * Set the data type and long name so that kcore can be read via
1141 * dso__data_read_addr().
1142 */
1143 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
5f70619d 1144 dso->binary_type = DSO_BINARY_TYPE__GUEST_KCORE;
8e0cf965 1145 else
5f70619d 1146 dso->binary_type = DSO_BINARY_TYPE__KCORE;
7e155d4d 1147 dso__set_long_name(dso, strdup(kcore_filename), true);
8e0cf965
AH
1148
1149 close(fd);
1150
1151 if (map->type == MAP__FUNCTION)
1152 pr_debug("Using %s for kernel object code\n", kcore_filename);
1153 else
1154 pr_debug("Using %s for kernel data\n", kcore_filename);
1155
1156 return 0;
1157
1158out_err:
1159 while (!list_empty(&md.maps)) {
1160 map = list_entry(md.maps.next, struct map, node);
1161 list_del(&map->node);
1162 map__delete(map);
1163 }
1164 close(fd);
1165 return -EINVAL;
1166}
1167
d9b62aba
AH
1168/*
1169 * If the kernel is relocated at boot time, kallsyms won't match. Compute the
1170 * delta based on the relocation reference symbol.
1171 */
1172static int kallsyms__delta(struct map *map, const char *filename, u64 *delta)
1173{
1174 struct kmap *kmap = map__kmap(map);
1175 u64 addr;
1176
1177 if (!kmap->ref_reloc_sym || !kmap->ref_reloc_sym->name)
1178 return 0;
1179
1180 addr = kallsyms__get_function_start(filename,
1181 kmap->ref_reloc_sym->name);
1182 if (!addr)
1183 return -1;
1184
1185 *delta = addr - kmap->ref_reloc_sym->addr;
1186 return 0;
1187}
1188
aeafcbaf 1189int dso__load_kallsyms(struct dso *dso, const char *filename,
9de89fe7 1190 struct map *map, symbol_filter_t filter)
2e538c4a 1191{
d9b62aba
AH
1192 u64 delta = 0;
1193
ec80fde7
ACM
1194 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
1195 return -1;
1196
aeafcbaf 1197 if (dso__load_all_kallsyms(dso, filename, map) < 0)
2e538c4a
ACM
1198 return -1;
1199
d9b62aba
AH
1200 if (kallsyms__delta(map, filename, &delta))
1201 return -1;
1202
694bf407 1203 symbols__fixup_duplicate(&dso->symbols[map->type]);
3f5a4272
AB
1204 symbols__fixup_end(&dso->symbols[map->type]);
1205
aeafcbaf 1206 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
44f24cb3 1207 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
a1645ce1 1208 else
44f24cb3 1209 dso->symtab_type = DSO_BINARY_TYPE__KALLSYMS;
2e538c4a 1210
8e0cf965
AH
1211 if (!dso__load_kcore(dso, map, filename))
1212 return dso__split_kallsyms_for_kcore(dso, map, filter);
1213 else
d9b62aba 1214 return dso__split_kallsyms(dso, map, delta, filter);
af427bf5
ACM
1215}
1216
aeafcbaf 1217static int dso__load_perf_map(struct dso *dso, struct map *map,
6beba7ad 1218 symbol_filter_t filter)
80d496be
PE
1219{
1220 char *line = NULL;
1221 size_t n;
1222 FILE *file;
1223 int nr_syms = 0;
1224
aeafcbaf 1225 file = fopen(dso->long_name, "r");
80d496be
PE
1226 if (file == NULL)
1227 goto out_failure;
1228
1229 while (!feof(file)) {
9cffa8d5 1230 u64 start, size;
80d496be
PE
1231 struct symbol *sym;
1232 int line_len, len;
1233
1234 line_len = getline(&line, &n, file);
1235 if (line_len < 0)
1236 break;
1237
1238 if (!line)
1239 goto out_failure;
1240
1241 line[--line_len] = '\0'; /* \n */
1242
1243 len = hex2u64(line, &start);
1244
1245 len++;
1246 if (len + 2 >= line_len)
1247 continue;
1248
1249 len += hex2u64(line + len, &size);
1250
1251 len++;
1252 if (len + 2 >= line_len)
1253 continue;
1254
c408fedf 1255 sym = symbol__new(start, size, STB_GLOBAL, line + len);
80d496be
PE
1256
1257 if (sym == NULL)
1258 goto out_delete_line;
1259
439d473b 1260 if (filter && filter(map, sym))
00a192b3 1261 symbol__delete(sym);
80d496be 1262 else {
aeafcbaf 1263 symbols__insert(&dso->symbols[map->type], sym);
80d496be
PE
1264 nr_syms++;
1265 }
1266 }
1267
1268 free(line);
1269 fclose(file);
1270
1271 return nr_syms;
1272
1273out_delete_line:
1274 free(line);
1275out_failure:
1276 return -1;
1277}
1278
1029f9fe
NK
1279static bool dso__is_compatible_symtab_type(struct dso *dso, bool kmod,
1280 enum dso_binary_type type)
1281{
1282 switch (type) {
1283 case DSO_BINARY_TYPE__JAVA_JIT:
1284 case DSO_BINARY_TYPE__DEBUGLINK:
1285 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
1286 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
1287 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
1288 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
1289 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
1290 return !kmod && dso->kernel == DSO_TYPE_USER;
1291
1292 case DSO_BINARY_TYPE__KALLSYMS:
1293 case DSO_BINARY_TYPE__VMLINUX:
1294 case DSO_BINARY_TYPE__KCORE:
1295 return dso->kernel == DSO_TYPE_KERNEL;
1296
1297 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
1298 case DSO_BINARY_TYPE__GUEST_VMLINUX:
1299 case DSO_BINARY_TYPE__GUEST_KCORE:
1300 return dso->kernel == DSO_TYPE_GUEST_KERNEL;
1301
1302 case DSO_BINARY_TYPE__GUEST_KMODULE:
1303 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
1304 /*
1305 * kernel modules know their symtab type - it's set when
1306 * creating a module dso in machine__new_module().
1307 */
1308 return kmod && dso->symtab_type == type;
1309
1310 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
1311 return true;
1312
1313 case DSO_BINARY_TYPE__NOT_FOUND:
1314 default:
1315 return false;
1316 }
1317}
1318
aeafcbaf 1319int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter)
a2928c42 1320{
c338aee8 1321 char *name;
a2928c42 1322 int ret = -1;
44f24cb3 1323 u_int i;
23346f21 1324 struct machine *machine;
44f24cb3 1325 char *root_dir = (char *) "";
3aafe5ae
CS
1326 int ss_pos = 0;
1327 struct symsrc ss_[2];
1328 struct symsrc *syms_ss = NULL, *runtime_ss = NULL;
1029f9fe 1329 bool kmod;
a2928c42 1330
aeafcbaf 1331 dso__set_loaded(dso, map->type);
66bd8424 1332
aeafcbaf
ACM
1333 if (dso->kernel == DSO_TYPE_KERNEL)
1334 return dso__load_kernel_sym(dso, map, filter);
1335 else if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1336 return dso__load_guest_kernel_sym(dso, map, filter);
a1645ce1 1337
23346f21
ACM
1338 if (map->groups && map->groups->machine)
1339 machine = map->groups->machine;
a1645ce1 1340 else
23346f21 1341 machine = NULL;
c338aee8 1342
aeafcbaf 1343 dso->adjust_symbols = 0;
f5812a7a 1344
aeafcbaf 1345 if (strncmp(dso->name, "/tmp/perf-", 10) == 0) {
981c1252
PE
1346 struct stat st;
1347
e9b52ef2 1348 if (lstat(dso->name, &st) < 0)
981c1252
PE
1349 return -1;
1350
1351 if (st.st_uid && (st.st_uid != geteuid())) {
1352 pr_warning("File %s not owned by current user or root, "
1353 "ignoring it.\n", dso->name);
1354 return -1;
1355 }
1356
aeafcbaf 1357 ret = dso__load_perf_map(dso, map, filter);
44f24cb3
JO
1358 dso->symtab_type = ret > 0 ? DSO_BINARY_TYPE__JAVA_JIT :
1359 DSO_BINARY_TYPE__NOT_FOUND;
94cb9e38
ACM
1360 return ret;
1361 }
1362
44f24cb3
JO
1363 if (machine)
1364 root_dir = machine->root_dir;
1365
164c800e
DA
1366 name = malloc(PATH_MAX);
1367 if (!name)
1368 return -1;
1369
1029f9fe
NK
1370 kmod = dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
1371 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE;
1372
1373 /*
1374 * Iterate over candidate debug images.
3aafe5ae
CS
1375 * Keep track of "interesting" ones (those which have a symtab, dynsym,
1376 * and/or opd section) for processing.
6da80ce8 1377 */
44f24cb3 1378 for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) {
3aafe5ae
CS
1379 struct symsrc *ss = &ss_[ss_pos];
1380 bool next_slot = false;
6da80ce8 1381
005f9294 1382 enum dso_binary_type symtab_type = binary_type_symtab[i];
ec5761ea 1383
1029f9fe
NK
1384 if (!dso__is_compatible_symtab_type(dso, kmod, symtab_type))
1385 continue;
1386
ee4e9625
ACM
1387 if (dso__read_binary_type_filename(dso, symtab_type,
1388 root_dir, name, PATH_MAX))
44f24cb3 1389 continue;
6da80ce8
DM
1390
1391 /* Name is now the name of the next image to try */
3aafe5ae 1392 if (symsrc__init(ss, dso, name, symtab_type) < 0)
6da80ce8 1393 continue;
a2928c42 1394
3aafe5ae
CS
1395 if (!syms_ss && symsrc__has_symtab(ss)) {
1396 syms_ss = ss;
1397 next_slot = true;
0058aef6
AH
1398 if (!dso->symsrc_filename)
1399 dso->symsrc_filename = strdup(name);
d26cd12b
CS
1400 }
1401
3aafe5ae
CS
1402 if (!runtime_ss && symsrc__possibly_runtime(ss)) {
1403 runtime_ss = ss;
1404 next_slot = true;
a44f605b 1405 }
a2928c42 1406
3aafe5ae
CS
1407 if (next_slot) {
1408 ss_pos++;
33ff581e 1409
3aafe5ae
CS
1410 if (syms_ss && runtime_ss)
1411 break;
98e9f03b
NK
1412 } else {
1413 symsrc__destroy(ss);
6da80ce8 1414 }
3aafe5ae 1415
a25e46c4 1416 }
6da80ce8 1417
3aafe5ae
CS
1418 if (!runtime_ss && !syms_ss)
1419 goto out_free;
1420
1421 if (runtime_ss && !syms_ss) {
1422 syms_ss = runtime_ss;
1423 }
1424
1425 /* We'll have to hope for the best */
1426 if (!runtime_ss && syms_ss)
1427 runtime_ss = syms_ss;
1428
1029f9fe
NK
1429 if (syms_ss)
1430 ret = dso__load_sym(dso, map, syms_ss, runtime_ss, filter, kmod);
1431 else
3aafe5ae
CS
1432 ret = -1;
1433
f47b58b7 1434 if (ret > 0) {
3aafe5ae
CS
1435 int nr_plt;
1436
1437 nr_plt = dso__synthesize_plt_symbols(dso, runtime_ss, map, filter);
1438 if (nr_plt > 0)
1439 ret += nr_plt;
60e4b10c
ACM
1440 }
1441
3aafe5ae
CS
1442 for (; ss_pos > 0; ss_pos--)
1443 symsrc__destroy(&ss_[ss_pos - 1]);
1444out_free:
a2928c42 1445 free(name);
aeafcbaf 1446 if (ret < 0 && strstr(dso->name, " (deleted)") != NULL)
1340e6bb 1447 return 0;
a2928c42
ACM
1448 return ret;
1449}
1450
aeafcbaf 1451struct map *map_groups__find_by_name(struct map_groups *mg,
79406cd7 1452 enum map_type type, const char *name)
439d473b
ACM
1453{
1454 struct rb_node *nd;
1455
aeafcbaf 1456 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
439d473b
ACM
1457 struct map *map = rb_entry(nd, struct map, rb_node);
1458
b7cece76 1459 if (map->dso && strcmp(map->dso->short_name, name) == 0)
439d473b
ACM
1460 return map;
1461 }
1462
1463 return NULL;
1464}
1465
aeafcbaf 1466int dso__load_vmlinux(struct dso *dso, struct map *map,
5230fb7d
ACM
1467 const char *vmlinux, bool vmlinux_allocated,
1468 symbol_filter_t filter)
a2928c42 1469{
b68e2f91
CS
1470 int err = -1;
1471 struct symsrc ss;
ec5761ea 1472 char symfs_vmlinux[PATH_MAX];
005f9294 1473 enum dso_binary_type symtab_type;
a2928c42 1474
5698d2c9
NK
1475 if (vmlinux[0] == '/')
1476 snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s", vmlinux);
1477 else
972f393b 1478 symbol__join_symfs(symfs_vmlinux, vmlinux);
a2928c42 1479
21ea4539 1480 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
005f9294 1481 symtab_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
21ea4539 1482 else
005f9294 1483 symtab_type = DSO_BINARY_TYPE__VMLINUX;
21ea4539 1484
005f9294 1485 if (symsrc__init(&ss, dso, symfs_vmlinux, symtab_type))
b68e2f91
CS
1486 return -1;
1487
261360b6 1488 err = dso__load_sym(dso, map, &ss, &ss, filter, 0);
b68e2f91 1489 symsrc__destroy(&ss);
a2928c42 1490
515850e4 1491 if (err > 0) {
39b12f78 1492 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
5f70619d 1493 dso->binary_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
39b12f78 1494 else
5f70619d 1495 dso->binary_type = DSO_BINARY_TYPE__VMLINUX;
bf4414ae 1496 dso__set_long_name(dso, vmlinux, vmlinux_allocated);
515850e4 1497 dso__set_loaded(dso, map->type);
ec5761ea 1498 pr_debug("Using %s for symbols\n", symfs_vmlinux);
515850e4 1499 }
3846df2e 1500
a2928c42
ACM
1501 return err;
1502}
1503
aeafcbaf 1504int dso__load_vmlinux_path(struct dso *dso, struct map *map,
9de89fe7 1505 symbol_filter_t filter)
a19afe46
ACM
1506{
1507 int i, err = 0;
5ad90e4e 1508 char *filename;
a19afe46
ACM
1509
1510 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
5ad90e4e
ACM
1511 vmlinux_path__nr_entries + 1);
1512
aeafcbaf 1513 filename = dso__build_id_filename(dso, NULL, 0);
5ad90e4e 1514 if (filename != NULL) {
5230fb7d
ACM
1515 err = dso__load_vmlinux(dso, map, filename, true, filter);
1516 if (err > 0)
5ad90e4e 1517 goto out;
5ad90e4e
ACM
1518 free(filename);
1519 }
a19afe46
ACM
1520
1521 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
5230fb7d
ACM
1522 err = dso__load_vmlinux(dso, map, vmlinux_path[i], false, filter);
1523 if (err > 0)
a19afe46 1524 break;
a19afe46 1525 }
5ad90e4e 1526out:
a19afe46
ACM
1527 return err;
1528}
1529
0544d422
AH
1530static int find_matching_kcore(struct map *map, char *dir, size_t dir_sz)
1531{
1532 char kallsyms_filename[PATH_MAX];
1533 struct dirent *dent;
1534 int ret = -1;
1535 DIR *d;
1536
1537 d = opendir(dir);
1538 if (!d)
1539 return -1;
1540
1541 while (1) {
1542 dent = readdir(d);
1543 if (!dent)
1544 break;
1545 if (dent->d_type != DT_DIR)
1546 continue;
1547 scnprintf(kallsyms_filename, sizeof(kallsyms_filename),
1548 "%s/%s/kallsyms", dir, dent->d_name);
a00d28cb 1549 if (!validate_kcore_addresses(kallsyms_filename, map)) {
0544d422
AH
1550 strlcpy(dir, kallsyms_filename, dir_sz);
1551 ret = 0;
1552 break;
1553 }
1554 }
1555
1556 closedir(d);
1557
1558 return ret;
1559}
1560
1561static char *dso__find_kallsyms(struct dso *dso, struct map *map)
1562{
1563 u8 host_build_id[BUILD_ID_SIZE];
1564 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1565 bool is_host = false;
1566 char path[PATH_MAX];
1567
1568 if (!dso->has_build_id) {
1569 /*
1570 * Last resort, if we don't have a build-id and couldn't find
1571 * any vmlinux file, try the running kernel kallsyms table.
1572 */
1573 goto proc_kallsyms;
1574 }
1575
1576 if (sysfs__read_build_id("/sys/kernel/notes", host_build_id,
1577 sizeof(host_build_id)) == 0)
1578 is_host = dso__build_id_equal(dso, host_build_id);
1579
1580 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1581
449867e3
AH
1582 scnprintf(path, sizeof(path), "%s/[kernel.kcore]/%s", buildid_dir,
1583 sbuild_id);
1584
0544d422
AH
1585 /* Use /proc/kallsyms if possible */
1586 if (is_host) {
1587 DIR *d;
1588 int fd;
1589
1590 /* If no cached kcore go with /proc/kallsyms */
0544d422
AH
1591 d = opendir(path);
1592 if (!d)
1593 goto proc_kallsyms;
1594 closedir(d);
1595
1596 /*
1597 * Do not check the build-id cache, until we know we cannot use
1598 * /proc/kcore.
1599 */
1600 fd = open("/proc/kcore", O_RDONLY);
1601 if (fd != -1) {
1602 close(fd);
1603 /* If module maps match go with /proc/kallsyms */
a00d28cb 1604 if (!validate_kcore_addresses("/proc/kallsyms", map))
0544d422
AH
1605 goto proc_kallsyms;
1606 }
1607
1608 /* Find kallsyms in build-id cache with kcore */
1609 if (!find_matching_kcore(map, path, sizeof(path)))
1610 return strdup(path);
1611
1612 goto proc_kallsyms;
1613 }
1614
449867e3
AH
1615 /* Find kallsyms in build-id cache with kcore */
1616 if (!find_matching_kcore(map, path, sizeof(path)))
1617 return strdup(path);
1618
0544d422
AH
1619 scnprintf(path, sizeof(path), "%s/[kernel.kallsyms]/%s",
1620 buildid_dir, sbuild_id);
1621
1622 if (access(path, F_OK)) {
1623 pr_err("No kallsyms or vmlinux with build-id %s was found\n",
1624 sbuild_id);
1625 return NULL;
1626 }
1627
1628 return strdup(path);
1629
1630proc_kallsyms:
1631 return strdup("/proc/kallsyms");
1632}
1633
aeafcbaf 1634static int dso__load_kernel_sym(struct dso *dso, struct map *map,
9de89fe7 1635 symbol_filter_t filter)
a827c875 1636{
cc612d81 1637 int err;
9e201442
ACM
1638 const char *kallsyms_filename = NULL;
1639 char *kallsyms_allocated_filename = NULL;
dc8d6ab2 1640 /*
b226a5a7
DA
1641 * Step 1: if the user specified a kallsyms or vmlinux filename, use
1642 * it and only it, reporting errors to the user if it cannot be used.
dc8d6ab2
ACM
1643 *
1644 * For instance, try to analyse an ARM perf.data file _without_ a
1645 * build-id, or if the user specifies the wrong path to the right
1646 * vmlinux file, obviously we can't fallback to another vmlinux (a
1647 * x86_86 one, on the machine where analysis is being performed, say),
1648 * or worse, /proc/kallsyms.
1649 *
1650 * If the specified file _has_ a build-id and there is a build-id
1651 * section in the perf.data file, we will still do the expected
1652 * validation in dso__load_vmlinux and will bail out if they don't
1653 * match.
1654 */
b226a5a7
DA
1655 if (symbol_conf.kallsyms_name != NULL) {
1656 kallsyms_filename = symbol_conf.kallsyms_name;
1657 goto do_kallsyms;
1658 }
1659
fc2be696 1660 if (!symbol_conf.ignore_vmlinux && symbol_conf.vmlinux_name != NULL) {
5230fb7d
ACM
1661 return dso__load_vmlinux(dso, map, symbol_conf.vmlinux_name,
1662 false, filter);
dc8d6ab2 1663 }
cc612d81 1664
fc2be696 1665 if (!symbol_conf.ignore_vmlinux && vmlinux_path != NULL) {
aeafcbaf 1666 err = dso__load_vmlinux_path(dso, map, filter);
a19afe46 1667 if (err > 0)
39b12f78 1668 return err;
cc612d81
ACM
1669 }
1670
ec5761ea
DA
1671 /* do not try local files if a symfs was given */
1672 if (symbol_conf.symfs[0] != 0)
1673 return -1;
1674
0544d422
AH
1675 kallsyms_allocated_filename = dso__find_kallsyms(dso, map);
1676 if (!kallsyms_allocated_filename)
1677 return -1;
19fc2ded 1678
0544d422 1679 kallsyms_filename = kallsyms_allocated_filename;
439d473b 1680
cc612d81 1681do_kallsyms:
aeafcbaf 1682 err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
3846df2e
ACM
1683 if (err > 0)
1684 pr_debug("Using %s for symbols\n", kallsyms_filename);
dc8d6ab2 1685 free(kallsyms_allocated_filename);
439d473b 1686
8e0cf965 1687 if (err > 0 && !dso__is_kcore(dso)) {
bdac0bcf 1688 dso->binary_type = DSO_BINARY_TYPE__KALLSYMS;
bf4414ae 1689 dso__set_long_name(dso, "[kernel.kallsyms]", false);
6a4694a4
ACM
1690 map__fixup_start(map);
1691 map__fixup_end(map);
439d473b 1692 }
94cb9e38 1693
a827c875
ACM
1694 return err;
1695}
1696
aeafcbaf
ACM
1697static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
1698 symbol_filter_t filter)
a1645ce1
ZY
1699{
1700 int err;
1701 const char *kallsyms_filename = NULL;
23346f21 1702 struct machine *machine;
a1645ce1
ZY
1703 char path[PATH_MAX];
1704
1705 if (!map->groups) {
1706 pr_debug("Guest kernel map hasn't the point to groups\n");
1707 return -1;
1708 }
23346f21 1709 machine = map->groups->machine;
a1645ce1 1710
23346f21 1711 if (machine__is_default_guest(machine)) {
a1645ce1
ZY
1712 /*
1713 * if the user specified a vmlinux filename, use it and only
1714 * it, reporting errors to the user if it cannot be used.
1715 * Or use file guest_kallsyms inputted by user on commandline
1716 */
1717 if (symbol_conf.default_guest_vmlinux_name != NULL) {
aeafcbaf 1718 err = dso__load_vmlinux(dso, map,
5230fb7d
ACM
1719 symbol_conf.default_guest_vmlinux_name,
1720 false, filter);
39b12f78 1721 return err;
a1645ce1
ZY
1722 }
1723
1724 kallsyms_filename = symbol_conf.default_guest_kallsyms;
1725 if (!kallsyms_filename)
1726 return -1;
1727 } else {
23346f21 1728 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
a1645ce1
ZY
1729 kallsyms_filename = path;
1730 }
1731
aeafcbaf 1732 err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
8e0cf965 1733 if (err > 0)
39b12f78 1734 pr_debug("Using %s for symbols\n", kallsyms_filename);
8e0cf965 1735 if (err > 0 && !dso__is_kcore(dso)) {
bdac0bcf 1736 dso->binary_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
39b12f78 1737 machine__mmap_name(machine, path, sizeof(path));
7e155d4d 1738 dso__set_long_name(dso, strdup(path), true);
a1645ce1
ZY
1739 map__fixup_start(map);
1740 map__fixup_end(map);
1741 }
1742
1743 return err;
1744}
cd84c2ac 1745
cc612d81
ACM
1746static void vmlinux_path__exit(void)
1747{
04662523
ACM
1748 while (--vmlinux_path__nr_entries >= 0)
1749 zfree(&vmlinux_path[vmlinux_path__nr_entries]);
cc612d81 1750
04662523 1751 zfree(&vmlinux_path);
cc612d81
ACM
1752}
1753
0a7e6d1b 1754static int vmlinux_path__init(struct perf_session_env *env)
cc612d81
ACM
1755{
1756 struct utsname uts;
1757 char bf[PATH_MAX];
0a7e6d1b 1758 char *kernel_version;
cc612d81 1759
c657f423 1760 vmlinux_path = malloc(sizeof(char *) * 6);
cc612d81
ACM
1761 if (vmlinux_path == NULL)
1762 return -1;
1763
1764 vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
1765 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1766 goto out_fail;
1767 ++vmlinux_path__nr_entries;
1768 vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
1769 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1770 goto out_fail;
1771 ++vmlinux_path__nr_entries;
ec5761ea 1772
0a7e6d1b 1773 /* only try kernel version if no symfs was given */
ec5761ea
DA
1774 if (symbol_conf.symfs[0] != 0)
1775 return 0;
1776
0a7e6d1b
NK
1777 if (env) {
1778 kernel_version = env->os_release;
1779 } else {
1780 if (uname(&uts) < 0)
1781 goto out_fail;
1782
1783 kernel_version = uts.release;
1784 }
ec5761ea 1785
0a7e6d1b 1786 snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", kernel_version);
cc612d81
ACM
1787 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1788 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1789 goto out_fail;
1790 ++vmlinux_path__nr_entries;
c657f423
AB
1791 snprintf(bf, sizeof(bf), "/usr/lib/debug/boot/vmlinux-%s",
1792 kernel_version);
1793 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1794 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1795 goto out_fail;
1796 ++vmlinux_path__nr_entries;
0a7e6d1b 1797 snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", kernel_version);
cc612d81
ACM
1798 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1799 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1800 goto out_fail;
1801 ++vmlinux_path__nr_entries;
1802 snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
0a7e6d1b 1803 kernel_version);
cc612d81
ACM
1804 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1805 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1806 goto out_fail;
1807 ++vmlinux_path__nr_entries;
1808
1809 return 0;
1810
1811out_fail:
1812 vmlinux_path__exit();
1813 return -1;
1814}
1815
3bfe5f81 1816int setup_list(struct strlist **list, const char *list_str,
655000e7
ACM
1817 const char *list_name)
1818{
1819 if (list_str == NULL)
1820 return 0;
1821
1822 *list = strlist__new(true, list_str);
1823 if (!*list) {
1824 pr_err("problems parsing %s list\n", list_name);
1825 return -1;
1826 }
1827 return 0;
1828}
1829
ec80fde7
ACM
1830static bool symbol__read_kptr_restrict(void)
1831{
1832 bool value = false;
1833
1834 if (geteuid() != 0) {
1835 FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
1836 if (fp != NULL) {
1837 char line[8];
1838
1839 if (fgets(line, sizeof(line), fp) != NULL)
1840 value = atoi(line) != 0;
1841
1842 fclose(fp);
1843 }
1844 }
1845
1846 return value;
1847}
1848
0a7e6d1b 1849int symbol__init(struct perf_session_env *env)
2446042c 1850{
ec5761ea
DA
1851 const char *symfs;
1852
85e00b55
JZ
1853 if (symbol_conf.initialized)
1854 return 0;
1855
9ac3e487 1856 symbol_conf.priv_size = PERF_ALIGN(symbol_conf.priv_size, sizeof(u64));
4d439517 1857
166ccc9c
NK
1858 symbol__elf_init();
1859
75be6cf4
ACM
1860 if (symbol_conf.sort_by_name)
1861 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
1862 sizeof(struct symbol));
b32d133a 1863
0a7e6d1b 1864 if (symbol_conf.try_vmlinux_path && vmlinux_path__init(env) < 0)
2446042c
ACM
1865 return -1;
1866
c410a338
ACM
1867 if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
1868 pr_err("'.' is the only non valid --field-separator argument\n");
1869 return -1;
1870 }
1871
655000e7
ACM
1872 if (setup_list(&symbol_conf.dso_list,
1873 symbol_conf.dso_list_str, "dso") < 0)
1874 return -1;
1875
1876 if (setup_list(&symbol_conf.comm_list,
1877 symbol_conf.comm_list_str, "comm") < 0)
1878 goto out_free_dso_list;
1879
1880 if (setup_list(&symbol_conf.sym_list,
1881 symbol_conf.sym_list_str, "symbol") < 0)
1882 goto out_free_comm_list;
1883
ec5761ea
DA
1884 /*
1885 * A path to symbols of "/" is identical to ""
1886 * reset here for simplicity.
1887 */
1888 symfs = realpath(symbol_conf.symfs, NULL);
1889 if (symfs == NULL)
1890 symfs = symbol_conf.symfs;
1891 if (strcmp(symfs, "/") == 0)
1892 symbol_conf.symfs = "";
1893 if (symfs != symbol_conf.symfs)
1894 free((void *)symfs);
1895
ec80fde7
ACM
1896 symbol_conf.kptr_restrict = symbol__read_kptr_restrict();
1897
85e00b55 1898 symbol_conf.initialized = true;
4aa65636 1899 return 0;
655000e7 1900
655000e7
ACM
1901out_free_comm_list:
1902 strlist__delete(symbol_conf.comm_list);
d74c896b
NK
1903out_free_dso_list:
1904 strlist__delete(symbol_conf.dso_list);
655000e7 1905 return -1;
4aa65636
ACM
1906}
1907
d65a458b
ACM
1908void symbol__exit(void)
1909{
85e00b55
JZ
1910 if (!symbol_conf.initialized)
1911 return;
d65a458b
ACM
1912 strlist__delete(symbol_conf.sym_list);
1913 strlist__delete(symbol_conf.dso_list);
1914 strlist__delete(symbol_conf.comm_list);
1915 vmlinux_path__exit();
1916 symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
85e00b55 1917 symbol_conf.initialized = false;
d65a458b 1918}