perf buildid-cache: Apply force option to copying kcore
[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;
1068
1069 if (list_empty(&md.maps)) {
1070 err = -EINVAL;
1071 goto out_err;
1072 }
1073
1074 /* Remove old maps */
1075 old_map = map_groups__first(kmaps, map->type);
1076 while (old_map) {
1077 struct map *next = map_groups__next(old_map);
1078
1079 if (old_map != map)
1080 map_groups__remove(kmaps, old_map);
1081 old_map = next;
1082 }
1083
1084 /* Find the kernel map using the first symbol */
1085 sym = dso__first_symbol(dso, map->type);
1086 list_for_each_entry(new_map, &md.maps, node) {
1087 if (sym && sym->start >= new_map->start &&
1088 sym->start < new_map->end) {
1089 replacement_map = new_map;
1090 break;
1091 }
1092 }
1093
1094 if (!replacement_map)
1095 replacement_map = list_entry(md.maps.next, struct map, node);
1096
1097 /* Add new maps */
1098 while (!list_empty(&md.maps)) {
1099 new_map = list_entry(md.maps.next, struct map, node);
1100 list_del(&new_map->node);
1101 if (new_map == replacement_map) {
1102 map->start = new_map->start;
1103 map->end = new_map->end;
1104 map->pgoff = new_map->pgoff;
1105 map->map_ip = new_map->map_ip;
1106 map->unmap_ip = new_map->unmap_ip;
1107 map__delete(new_map);
1108 /* Ensure maps are correctly ordered */
1109 map_groups__remove(kmaps, map);
1110 map_groups__insert(kmaps, map);
1111 } else {
1112 map_groups__insert(kmaps, new_map);
1113 }
1114 }
1115
1116 /*
1117 * Set the data type and long name so that kcore can be read via
1118 * dso__data_read_addr().
1119 */
1120 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
5f70619d 1121 dso->binary_type = DSO_BINARY_TYPE__GUEST_KCORE;
8e0cf965 1122 else
5f70619d 1123 dso->binary_type = DSO_BINARY_TYPE__KCORE;
7e155d4d 1124 dso__set_long_name(dso, strdup(kcore_filename), true);
8e0cf965
AH
1125
1126 close(fd);
1127
1128 if (map->type == MAP__FUNCTION)
1129 pr_debug("Using %s for kernel object code\n", kcore_filename);
1130 else
1131 pr_debug("Using %s for kernel data\n", kcore_filename);
1132
1133 return 0;
1134
1135out_err:
1136 while (!list_empty(&md.maps)) {
1137 map = list_entry(md.maps.next, struct map, node);
1138 list_del(&map->node);
1139 map__delete(map);
1140 }
1141 close(fd);
1142 return -EINVAL;
1143}
1144
d9b62aba
AH
1145/*
1146 * If the kernel is relocated at boot time, kallsyms won't match. Compute the
1147 * delta based on the relocation reference symbol.
1148 */
1149static int kallsyms__delta(struct map *map, const char *filename, u64 *delta)
1150{
1151 struct kmap *kmap = map__kmap(map);
1152 u64 addr;
1153
1154 if (!kmap->ref_reloc_sym || !kmap->ref_reloc_sym->name)
1155 return 0;
1156
1157 addr = kallsyms__get_function_start(filename,
1158 kmap->ref_reloc_sym->name);
1159 if (!addr)
1160 return -1;
1161
1162 *delta = addr - kmap->ref_reloc_sym->addr;
1163 return 0;
1164}
1165
aeafcbaf 1166int dso__load_kallsyms(struct dso *dso, const char *filename,
9de89fe7 1167 struct map *map, symbol_filter_t filter)
2e538c4a 1168{
d9b62aba
AH
1169 u64 delta = 0;
1170
ec80fde7
ACM
1171 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
1172 return -1;
1173
aeafcbaf 1174 if (dso__load_all_kallsyms(dso, filename, map) < 0)
2e538c4a
ACM
1175 return -1;
1176
d9b62aba
AH
1177 if (kallsyms__delta(map, filename, &delta))
1178 return -1;
1179
694bf407 1180 symbols__fixup_duplicate(&dso->symbols[map->type]);
3f5a4272
AB
1181 symbols__fixup_end(&dso->symbols[map->type]);
1182
aeafcbaf 1183 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
44f24cb3 1184 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
a1645ce1 1185 else
44f24cb3 1186 dso->symtab_type = DSO_BINARY_TYPE__KALLSYMS;
2e538c4a 1187
8e0cf965
AH
1188 if (!dso__load_kcore(dso, map, filename))
1189 return dso__split_kallsyms_for_kcore(dso, map, filter);
1190 else
d9b62aba 1191 return dso__split_kallsyms(dso, map, delta, filter);
af427bf5
ACM
1192}
1193
aeafcbaf 1194static int dso__load_perf_map(struct dso *dso, struct map *map,
6beba7ad 1195 symbol_filter_t filter)
80d496be
PE
1196{
1197 char *line = NULL;
1198 size_t n;
1199 FILE *file;
1200 int nr_syms = 0;
1201
aeafcbaf 1202 file = fopen(dso->long_name, "r");
80d496be
PE
1203 if (file == NULL)
1204 goto out_failure;
1205
1206 while (!feof(file)) {
9cffa8d5 1207 u64 start, size;
80d496be
PE
1208 struct symbol *sym;
1209 int line_len, len;
1210
1211 line_len = getline(&line, &n, file);
1212 if (line_len < 0)
1213 break;
1214
1215 if (!line)
1216 goto out_failure;
1217
1218 line[--line_len] = '\0'; /* \n */
1219
1220 len = hex2u64(line, &start);
1221
1222 len++;
1223 if (len + 2 >= line_len)
1224 continue;
1225
1226 len += hex2u64(line + len, &size);
1227
1228 len++;
1229 if (len + 2 >= line_len)
1230 continue;
1231
c408fedf 1232 sym = symbol__new(start, size, STB_GLOBAL, line + len);
80d496be
PE
1233
1234 if (sym == NULL)
1235 goto out_delete_line;
1236
439d473b 1237 if (filter && filter(map, sym))
00a192b3 1238 symbol__delete(sym);
80d496be 1239 else {
aeafcbaf 1240 symbols__insert(&dso->symbols[map->type], sym);
80d496be
PE
1241 nr_syms++;
1242 }
1243 }
1244
1245 free(line);
1246 fclose(file);
1247
1248 return nr_syms;
1249
1250out_delete_line:
1251 free(line);
1252out_failure:
1253 return -1;
1254}
1255
1029f9fe
NK
1256static bool dso__is_compatible_symtab_type(struct dso *dso, bool kmod,
1257 enum dso_binary_type type)
1258{
1259 switch (type) {
1260 case DSO_BINARY_TYPE__JAVA_JIT:
1261 case DSO_BINARY_TYPE__DEBUGLINK:
1262 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
1263 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
1264 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
1265 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
1266 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
1267 return !kmod && dso->kernel == DSO_TYPE_USER;
1268
1269 case DSO_BINARY_TYPE__KALLSYMS:
1270 case DSO_BINARY_TYPE__VMLINUX:
1271 case DSO_BINARY_TYPE__KCORE:
1272 return dso->kernel == DSO_TYPE_KERNEL;
1273
1274 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
1275 case DSO_BINARY_TYPE__GUEST_VMLINUX:
1276 case DSO_BINARY_TYPE__GUEST_KCORE:
1277 return dso->kernel == DSO_TYPE_GUEST_KERNEL;
1278
1279 case DSO_BINARY_TYPE__GUEST_KMODULE:
1280 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
1281 /*
1282 * kernel modules know their symtab type - it's set when
1283 * creating a module dso in machine__new_module().
1284 */
1285 return kmod && dso->symtab_type == type;
1286
1287 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
1288 return true;
1289
1290 case DSO_BINARY_TYPE__NOT_FOUND:
1291 default:
1292 return false;
1293 }
1294}
1295
aeafcbaf 1296int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter)
a2928c42 1297{
c338aee8 1298 char *name;
a2928c42 1299 int ret = -1;
44f24cb3 1300 u_int i;
23346f21 1301 struct machine *machine;
44f24cb3 1302 char *root_dir = (char *) "";
3aafe5ae
CS
1303 int ss_pos = 0;
1304 struct symsrc ss_[2];
1305 struct symsrc *syms_ss = NULL, *runtime_ss = NULL;
1029f9fe 1306 bool kmod;
a2928c42 1307
aeafcbaf 1308 dso__set_loaded(dso, map->type);
66bd8424 1309
aeafcbaf
ACM
1310 if (dso->kernel == DSO_TYPE_KERNEL)
1311 return dso__load_kernel_sym(dso, map, filter);
1312 else if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1313 return dso__load_guest_kernel_sym(dso, map, filter);
a1645ce1 1314
23346f21
ACM
1315 if (map->groups && map->groups->machine)
1316 machine = map->groups->machine;
a1645ce1 1317 else
23346f21 1318 machine = NULL;
c338aee8 1319
aeafcbaf 1320 dso->adjust_symbols = 0;
f5812a7a 1321
aeafcbaf 1322 if (strncmp(dso->name, "/tmp/perf-", 10) == 0) {
981c1252
PE
1323 struct stat st;
1324
e9b52ef2 1325 if (lstat(dso->name, &st) < 0)
981c1252
PE
1326 return -1;
1327
1328 if (st.st_uid && (st.st_uid != geteuid())) {
1329 pr_warning("File %s not owned by current user or root, "
1330 "ignoring it.\n", dso->name);
1331 return -1;
1332 }
1333
aeafcbaf 1334 ret = dso__load_perf_map(dso, map, filter);
44f24cb3
JO
1335 dso->symtab_type = ret > 0 ? DSO_BINARY_TYPE__JAVA_JIT :
1336 DSO_BINARY_TYPE__NOT_FOUND;
94cb9e38
ACM
1337 return ret;
1338 }
1339
44f24cb3
JO
1340 if (machine)
1341 root_dir = machine->root_dir;
1342
164c800e
DA
1343 name = malloc(PATH_MAX);
1344 if (!name)
1345 return -1;
1346
1029f9fe
NK
1347 kmod = dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
1348 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE;
1349
1350 /*
1351 * Iterate over candidate debug images.
3aafe5ae
CS
1352 * Keep track of "interesting" ones (those which have a symtab, dynsym,
1353 * and/or opd section) for processing.
6da80ce8 1354 */
44f24cb3 1355 for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) {
3aafe5ae
CS
1356 struct symsrc *ss = &ss_[ss_pos];
1357 bool next_slot = false;
6da80ce8 1358
005f9294 1359 enum dso_binary_type symtab_type = binary_type_symtab[i];
ec5761ea 1360
1029f9fe
NK
1361 if (!dso__is_compatible_symtab_type(dso, kmod, symtab_type))
1362 continue;
1363
ee4e9625
ACM
1364 if (dso__read_binary_type_filename(dso, symtab_type,
1365 root_dir, name, PATH_MAX))
44f24cb3 1366 continue;
6da80ce8
DM
1367
1368 /* Name is now the name of the next image to try */
3aafe5ae 1369 if (symsrc__init(ss, dso, name, symtab_type) < 0)
6da80ce8 1370 continue;
a2928c42 1371
3aafe5ae
CS
1372 if (!syms_ss && symsrc__has_symtab(ss)) {
1373 syms_ss = ss;
1374 next_slot = true;
0058aef6
AH
1375 if (!dso->symsrc_filename)
1376 dso->symsrc_filename = strdup(name);
d26cd12b
CS
1377 }
1378
3aafe5ae
CS
1379 if (!runtime_ss && symsrc__possibly_runtime(ss)) {
1380 runtime_ss = ss;
1381 next_slot = true;
a44f605b 1382 }
a2928c42 1383
3aafe5ae
CS
1384 if (next_slot) {
1385 ss_pos++;
33ff581e 1386
3aafe5ae
CS
1387 if (syms_ss && runtime_ss)
1388 break;
98e9f03b
NK
1389 } else {
1390 symsrc__destroy(ss);
6da80ce8 1391 }
3aafe5ae 1392
a25e46c4 1393 }
6da80ce8 1394
3aafe5ae
CS
1395 if (!runtime_ss && !syms_ss)
1396 goto out_free;
1397
1398 if (runtime_ss && !syms_ss) {
1399 syms_ss = runtime_ss;
1400 }
1401
1402 /* We'll have to hope for the best */
1403 if (!runtime_ss && syms_ss)
1404 runtime_ss = syms_ss;
1405
1029f9fe
NK
1406 if (syms_ss)
1407 ret = dso__load_sym(dso, map, syms_ss, runtime_ss, filter, kmod);
1408 else
3aafe5ae
CS
1409 ret = -1;
1410
f47b58b7 1411 if (ret > 0) {
3aafe5ae
CS
1412 int nr_plt;
1413
1414 nr_plt = dso__synthesize_plt_symbols(dso, runtime_ss, map, filter);
1415 if (nr_plt > 0)
1416 ret += nr_plt;
60e4b10c
ACM
1417 }
1418
3aafe5ae
CS
1419 for (; ss_pos > 0; ss_pos--)
1420 symsrc__destroy(&ss_[ss_pos - 1]);
1421out_free:
a2928c42 1422 free(name);
aeafcbaf 1423 if (ret < 0 && strstr(dso->name, " (deleted)") != NULL)
1340e6bb 1424 return 0;
a2928c42
ACM
1425 return ret;
1426}
1427
aeafcbaf 1428struct map *map_groups__find_by_name(struct map_groups *mg,
79406cd7 1429 enum map_type type, const char *name)
439d473b
ACM
1430{
1431 struct rb_node *nd;
1432
aeafcbaf 1433 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
439d473b
ACM
1434 struct map *map = rb_entry(nd, struct map, rb_node);
1435
b7cece76 1436 if (map->dso && strcmp(map->dso->short_name, name) == 0)
439d473b
ACM
1437 return map;
1438 }
1439
1440 return NULL;
1441}
1442
aeafcbaf 1443int dso__load_vmlinux(struct dso *dso, struct map *map,
5230fb7d
ACM
1444 const char *vmlinux, bool vmlinux_allocated,
1445 symbol_filter_t filter)
a2928c42 1446{
b68e2f91
CS
1447 int err = -1;
1448 struct symsrc ss;
ec5761ea 1449 char symfs_vmlinux[PATH_MAX];
005f9294 1450 enum dso_binary_type symtab_type;
a2928c42 1451
5698d2c9
NK
1452 if (vmlinux[0] == '/')
1453 snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s", vmlinux);
1454 else
1455 snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s%s",
1456 symbol_conf.symfs, vmlinux);
a2928c42 1457
21ea4539 1458 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
005f9294 1459 symtab_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
21ea4539 1460 else
005f9294 1461 symtab_type = DSO_BINARY_TYPE__VMLINUX;
21ea4539 1462
005f9294 1463 if (symsrc__init(&ss, dso, symfs_vmlinux, symtab_type))
b68e2f91
CS
1464 return -1;
1465
261360b6 1466 err = dso__load_sym(dso, map, &ss, &ss, filter, 0);
b68e2f91 1467 symsrc__destroy(&ss);
a2928c42 1468
515850e4 1469 if (err > 0) {
39b12f78 1470 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
5f70619d 1471 dso->binary_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
39b12f78 1472 else
5f70619d 1473 dso->binary_type = DSO_BINARY_TYPE__VMLINUX;
bf4414ae 1474 dso__set_long_name(dso, vmlinux, vmlinux_allocated);
515850e4 1475 dso__set_loaded(dso, map->type);
ec5761ea 1476 pr_debug("Using %s for symbols\n", symfs_vmlinux);
515850e4 1477 }
3846df2e 1478
a2928c42
ACM
1479 return err;
1480}
1481
aeafcbaf 1482int dso__load_vmlinux_path(struct dso *dso, struct map *map,
9de89fe7 1483 symbol_filter_t filter)
a19afe46
ACM
1484{
1485 int i, err = 0;
5ad90e4e 1486 char *filename;
a19afe46
ACM
1487
1488 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
5ad90e4e
ACM
1489 vmlinux_path__nr_entries + 1);
1490
aeafcbaf 1491 filename = dso__build_id_filename(dso, NULL, 0);
5ad90e4e 1492 if (filename != NULL) {
5230fb7d
ACM
1493 err = dso__load_vmlinux(dso, map, filename, true, filter);
1494 if (err > 0)
5ad90e4e 1495 goto out;
5ad90e4e
ACM
1496 free(filename);
1497 }
a19afe46
ACM
1498
1499 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
5230fb7d
ACM
1500 err = dso__load_vmlinux(dso, map, vmlinux_path[i], false, filter);
1501 if (err > 0)
a19afe46 1502 break;
a19afe46 1503 }
5ad90e4e 1504out:
a19afe46
ACM
1505 return err;
1506}
1507
0544d422
AH
1508static int find_matching_kcore(struct map *map, char *dir, size_t dir_sz)
1509{
1510 char kallsyms_filename[PATH_MAX];
1511 struct dirent *dent;
1512 int ret = -1;
1513 DIR *d;
1514
1515 d = opendir(dir);
1516 if (!d)
1517 return -1;
1518
1519 while (1) {
1520 dent = readdir(d);
1521 if (!dent)
1522 break;
1523 if (dent->d_type != DT_DIR)
1524 continue;
1525 scnprintf(kallsyms_filename, sizeof(kallsyms_filename),
1526 "%s/%s/kallsyms", dir, dent->d_name);
a00d28cb 1527 if (!validate_kcore_addresses(kallsyms_filename, map)) {
0544d422
AH
1528 strlcpy(dir, kallsyms_filename, dir_sz);
1529 ret = 0;
1530 break;
1531 }
1532 }
1533
1534 closedir(d);
1535
1536 return ret;
1537}
1538
1539static char *dso__find_kallsyms(struct dso *dso, struct map *map)
1540{
1541 u8 host_build_id[BUILD_ID_SIZE];
1542 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1543 bool is_host = false;
1544 char path[PATH_MAX];
1545
1546 if (!dso->has_build_id) {
1547 /*
1548 * Last resort, if we don't have a build-id and couldn't find
1549 * any vmlinux file, try the running kernel kallsyms table.
1550 */
1551 goto proc_kallsyms;
1552 }
1553
1554 if (sysfs__read_build_id("/sys/kernel/notes", host_build_id,
1555 sizeof(host_build_id)) == 0)
1556 is_host = dso__build_id_equal(dso, host_build_id);
1557
1558 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1559
449867e3
AH
1560 scnprintf(path, sizeof(path), "%s/[kernel.kcore]/%s", buildid_dir,
1561 sbuild_id);
1562
0544d422
AH
1563 /* Use /proc/kallsyms if possible */
1564 if (is_host) {
1565 DIR *d;
1566 int fd;
1567
1568 /* If no cached kcore go with /proc/kallsyms */
0544d422
AH
1569 d = opendir(path);
1570 if (!d)
1571 goto proc_kallsyms;
1572 closedir(d);
1573
1574 /*
1575 * Do not check the build-id cache, until we know we cannot use
1576 * /proc/kcore.
1577 */
1578 fd = open("/proc/kcore", O_RDONLY);
1579 if (fd != -1) {
1580 close(fd);
1581 /* If module maps match go with /proc/kallsyms */
a00d28cb 1582 if (!validate_kcore_addresses("/proc/kallsyms", map))
0544d422
AH
1583 goto proc_kallsyms;
1584 }
1585
1586 /* Find kallsyms in build-id cache with kcore */
1587 if (!find_matching_kcore(map, path, sizeof(path)))
1588 return strdup(path);
1589
1590 goto proc_kallsyms;
1591 }
1592
449867e3
AH
1593 /* Find kallsyms in build-id cache with kcore */
1594 if (!find_matching_kcore(map, path, sizeof(path)))
1595 return strdup(path);
1596
0544d422
AH
1597 scnprintf(path, sizeof(path), "%s/[kernel.kallsyms]/%s",
1598 buildid_dir, sbuild_id);
1599
1600 if (access(path, F_OK)) {
1601 pr_err("No kallsyms or vmlinux with build-id %s was found\n",
1602 sbuild_id);
1603 return NULL;
1604 }
1605
1606 return strdup(path);
1607
1608proc_kallsyms:
1609 return strdup("/proc/kallsyms");
1610}
1611
aeafcbaf 1612static int dso__load_kernel_sym(struct dso *dso, struct map *map,
9de89fe7 1613 symbol_filter_t filter)
a827c875 1614{
cc612d81 1615 int err;
9e201442
ACM
1616 const char *kallsyms_filename = NULL;
1617 char *kallsyms_allocated_filename = NULL;
dc8d6ab2 1618 /*
b226a5a7
DA
1619 * Step 1: if the user specified a kallsyms or vmlinux filename, use
1620 * it and only it, reporting errors to the user if it cannot be used.
dc8d6ab2
ACM
1621 *
1622 * For instance, try to analyse an ARM perf.data file _without_ a
1623 * build-id, or if the user specifies the wrong path to the right
1624 * vmlinux file, obviously we can't fallback to another vmlinux (a
1625 * x86_86 one, on the machine where analysis is being performed, say),
1626 * or worse, /proc/kallsyms.
1627 *
1628 * If the specified file _has_ a build-id and there is a build-id
1629 * section in the perf.data file, we will still do the expected
1630 * validation in dso__load_vmlinux and will bail out if they don't
1631 * match.
1632 */
b226a5a7
DA
1633 if (symbol_conf.kallsyms_name != NULL) {
1634 kallsyms_filename = symbol_conf.kallsyms_name;
1635 goto do_kallsyms;
1636 }
1637
fc2be696 1638 if (!symbol_conf.ignore_vmlinux && symbol_conf.vmlinux_name != NULL) {
5230fb7d
ACM
1639 return dso__load_vmlinux(dso, map, symbol_conf.vmlinux_name,
1640 false, filter);
dc8d6ab2 1641 }
cc612d81 1642
fc2be696 1643 if (!symbol_conf.ignore_vmlinux && vmlinux_path != NULL) {
aeafcbaf 1644 err = dso__load_vmlinux_path(dso, map, filter);
a19afe46 1645 if (err > 0)
39b12f78 1646 return err;
cc612d81
ACM
1647 }
1648
ec5761ea
DA
1649 /* do not try local files if a symfs was given */
1650 if (symbol_conf.symfs[0] != 0)
1651 return -1;
1652
0544d422
AH
1653 kallsyms_allocated_filename = dso__find_kallsyms(dso, map);
1654 if (!kallsyms_allocated_filename)
1655 return -1;
19fc2ded 1656
0544d422 1657 kallsyms_filename = kallsyms_allocated_filename;
439d473b 1658
cc612d81 1659do_kallsyms:
aeafcbaf 1660 err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
3846df2e
ACM
1661 if (err > 0)
1662 pr_debug("Using %s for symbols\n", kallsyms_filename);
dc8d6ab2 1663 free(kallsyms_allocated_filename);
439d473b 1664
8e0cf965 1665 if (err > 0 && !dso__is_kcore(dso)) {
bf4414ae 1666 dso__set_long_name(dso, "[kernel.kallsyms]", false);
6a4694a4
ACM
1667 map__fixup_start(map);
1668 map__fixup_end(map);
439d473b 1669 }
94cb9e38 1670
a827c875
ACM
1671 return err;
1672}
1673
aeafcbaf
ACM
1674static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
1675 symbol_filter_t filter)
a1645ce1
ZY
1676{
1677 int err;
1678 const char *kallsyms_filename = NULL;
23346f21 1679 struct machine *machine;
a1645ce1
ZY
1680 char path[PATH_MAX];
1681
1682 if (!map->groups) {
1683 pr_debug("Guest kernel map hasn't the point to groups\n");
1684 return -1;
1685 }
23346f21 1686 machine = map->groups->machine;
a1645ce1 1687
23346f21 1688 if (machine__is_default_guest(machine)) {
a1645ce1
ZY
1689 /*
1690 * if the user specified a vmlinux filename, use it and only
1691 * it, reporting errors to the user if it cannot be used.
1692 * Or use file guest_kallsyms inputted by user on commandline
1693 */
1694 if (symbol_conf.default_guest_vmlinux_name != NULL) {
aeafcbaf 1695 err = dso__load_vmlinux(dso, map,
5230fb7d
ACM
1696 symbol_conf.default_guest_vmlinux_name,
1697 false, filter);
39b12f78 1698 return err;
a1645ce1
ZY
1699 }
1700
1701 kallsyms_filename = symbol_conf.default_guest_kallsyms;
1702 if (!kallsyms_filename)
1703 return -1;
1704 } else {
23346f21 1705 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
a1645ce1
ZY
1706 kallsyms_filename = path;
1707 }
1708
aeafcbaf 1709 err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
8e0cf965 1710 if (err > 0)
39b12f78 1711 pr_debug("Using %s for symbols\n", kallsyms_filename);
8e0cf965 1712 if (err > 0 && !dso__is_kcore(dso)) {
39b12f78 1713 machine__mmap_name(machine, path, sizeof(path));
7e155d4d 1714 dso__set_long_name(dso, strdup(path), true);
a1645ce1
ZY
1715 map__fixup_start(map);
1716 map__fixup_end(map);
1717 }
1718
1719 return err;
1720}
cd84c2ac 1721
cc612d81
ACM
1722static void vmlinux_path__exit(void)
1723{
04662523
ACM
1724 while (--vmlinux_path__nr_entries >= 0)
1725 zfree(&vmlinux_path[vmlinux_path__nr_entries]);
cc612d81 1726
04662523 1727 zfree(&vmlinux_path);
cc612d81
ACM
1728}
1729
1730static int vmlinux_path__init(void)
1731{
1732 struct utsname uts;
1733 char bf[PATH_MAX];
1734
cc612d81
ACM
1735 vmlinux_path = malloc(sizeof(char *) * 5);
1736 if (vmlinux_path == NULL)
1737 return -1;
1738
1739 vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
1740 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1741 goto out_fail;
1742 ++vmlinux_path__nr_entries;
1743 vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
1744 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1745 goto out_fail;
1746 ++vmlinux_path__nr_entries;
ec5761ea
DA
1747
1748 /* only try running kernel version if no symfs was given */
1749 if (symbol_conf.symfs[0] != 0)
1750 return 0;
1751
1752 if (uname(&uts) < 0)
1753 return -1;
1754
cc612d81
ACM
1755 snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
1756 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1757 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1758 goto out_fail;
1759 ++vmlinux_path__nr_entries;
1760 snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
1761 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1762 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1763 goto out_fail;
1764 ++vmlinux_path__nr_entries;
1765 snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
1766 uts.release);
1767 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1768 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1769 goto out_fail;
1770 ++vmlinux_path__nr_entries;
1771
1772 return 0;
1773
1774out_fail:
1775 vmlinux_path__exit();
1776 return -1;
1777}
1778
3bfe5f81 1779int setup_list(struct strlist **list, const char *list_str,
655000e7
ACM
1780 const char *list_name)
1781{
1782 if (list_str == NULL)
1783 return 0;
1784
1785 *list = strlist__new(true, list_str);
1786 if (!*list) {
1787 pr_err("problems parsing %s list\n", list_name);
1788 return -1;
1789 }
1790 return 0;
1791}
1792
ec80fde7
ACM
1793static bool symbol__read_kptr_restrict(void)
1794{
1795 bool value = false;
1796
1797 if (geteuid() != 0) {
1798 FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
1799 if (fp != NULL) {
1800 char line[8];
1801
1802 if (fgets(line, sizeof(line), fp) != NULL)
1803 value = atoi(line) != 0;
1804
1805 fclose(fp);
1806 }
1807 }
1808
1809 return value;
1810}
1811
75be6cf4 1812int symbol__init(void)
2446042c 1813{
ec5761ea
DA
1814 const char *symfs;
1815
85e00b55
JZ
1816 if (symbol_conf.initialized)
1817 return 0;
1818
9ac3e487 1819 symbol_conf.priv_size = PERF_ALIGN(symbol_conf.priv_size, sizeof(u64));
4d439517 1820
166ccc9c
NK
1821 symbol__elf_init();
1822
75be6cf4
ACM
1823 if (symbol_conf.sort_by_name)
1824 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
1825 sizeof(struct symbol));
b32d133a 1826
75be6cf4 1827 if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
2446042c
ACM
1828 return -1;
1829
c410a338
ACM
1830 if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
1831 pr_err("'.' is the only non valid --field-separator argument\n");
1832 return -1;
1833 }
1834
655000e7
ACM
1835 if (setup_list(&symbol_conf.dso_list,
1836 symbol_conf.dso_list_str, "dso") < 0)
1837 return -1;
1838
1839 if (setup_list(&symbol_conf.comm_list,
1840 symbol_conf.comm_list_str, "comm") < 0)
1841 goto out_free_dso_list;
1842
1843 if (setup_list(&symbol_conf.sym_list,
1844 symbol_conf.sym_list_str, "symbol") < 0)
1845 goto out_free_comm_list;
1846
ec5761ea
DA
1847 /*
1848 * A path to symbols of "/" is identical to ""
1849 * reset here for simplicity.
1850 */
1851 symfs = realpath(symbol_conf.symfs, NULL);
1852 if (symfs == NULL)
1853 symfs = symbol_conf.symfs;
1854 if (strcmp(symfs, "/") == 0)
1855 symbol_conf.symfs = "";
1856 if (symfs != symbol_conf.symfs)
1857 free((void *)symfs);
1858
ec80fde7
ACM
1859 symbol_conf.kptr_restrict = symbol__read_kptr_restrict();
1860
85e00b55 1861 symbol_conf.initialized = true;
4aa65636 1862 return 0;
655000e7 1863
655000e7
ACM
1864out_free_comm_list:
1865 strlist__delete(symbol_conf.comm_list);
d74c896b
NK
1866out_free_dso_list:
1867 strlist__delete(symbol_conf.dso_list);
655000e7 1868 return -1;
4aa65636
ACM
1869}
1870
d65a458b
ACM
1871void symbol__exit(void)
1872{
85e00b55
JZ
1873 if (!symbol_conf.initialized)
1874 return;
d65a458b
ACM
1875 strlist__delete(symbol_conf.sym_list);
1876 strlist__delete(symbol_conf.dso_list);
1877 strlist__delete(symbol_conf.comm_list);
1878 vmlinux_path__exit();
1879 symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
85e00b55 1880 symbol_conf.initialized = false;
d65a458b 1881}
This page took 0.286943 seconds and 5 git commands to generate.