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