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