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