blob: 91404bacc3df81a6faddf606e807265dc7579757 [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001// SPDX-License-Identifier: GPL-2.0
2#include <dirent.h>
3#include <errno.h>
4#include <stdlib.h>
5#include <stdio.h>
6#include <string.h>
7#include <linux/kernel.h>
8#include <linux/mman.h>
9#include <sys/types.h>
10#include <sys/stat.h>
11#include <sys/param.h>
12#include <fcntl.h>
13#include <unistd.h>
14#include <inttypes.h>
15#include "annotate.h"
16#include "build-id.h"
17#include "util.h"
18#include "debug.h"
19#include "machine.h"
20#include "symbol.h"
21#include "strlist.h"
22#include "intlist.h"
23#include "namespaces.h"
24#include "header.h"
25#include "path.h"
26#include "sane_ctype.h"
27
28#include <elf.h>
29#include <limits.h>
30#include <symbol/kallsyms.h>
31#include <sys/utsname.h>
32
33static int dso__load_kernel_sym(struct dso *dso, struct map *map);
34static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map);
35static bool symbol__is_idle(const char *name);
36
37int vmlinux_path__nr_entries;
38char **vmlinux_path;
39
40struct symbol_conf symbol_conf = {
41 .use_modules = true,
42 .try_vmlinux_path = true,
43 .demangle = true,
44 .demangle_kernel = false,
45 .cumulate_callchain = true,
46 .show_hist_headers = true,
47 .symfs = "",
48 .event_group = true,
49 .inline_name = true,
50};
51
52static enum dso_binary_type binary_type_symtab[] = {
53 DSO_BINARY_TYPE__KALLSYMS,
54 DSO_BINARY_TYPE__GUEST_KALLSYMS,
55 DSO_BINARY_TYPE__JAVA_JIT,
56 DSO_BINARY_TYPE__DEBUGLINK,
57 DSO_BINARY_TYPE__BUILD_ID_CACHE,
58 DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO,
59 DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
60 DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
61 DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
62 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
63 DSO_BINARY_TYPE__GUEST_KMODULE,
64 DSO_BINARY_TYPE__GUEST_KMODULE_COMP,
65 DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE,
66 DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP,
67 DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO,
68 DSO_BINARY_TYPE__NOT_FOUND,
69};
70
71#define DSO_BINARY_TYPE__SYMTAB_CNT ARRAY_SIZE(binary_type_symtab)
72
73static bool symbol_type__filter(char symbol_type)
74{
75 symbol_type = toupper(symbol_type);
76 return symbol_type == 'T' || symbol_type == 'W' || symbol_type == 'D' || symbol_type == 'B';
77}
78
79static int prefix_underscores_count(const char *str)
80{
81 const char *tail = str;
82
83 while (*tail == '_')
84 tail++;
85
86 return tail - str;
87}
88
89void __weak arch__symbols__fixup_end(struct symbol *p, struct symbol *c)
90{
91 p->end = c->start;
92}
93
94const char * __weak arch__normalize_symbol_name(const char *name)
95{
96 return name;
97}
98
99int __weak arch__compare_symbol_names(const char *namea, const char *nameb)
100{
101 return strcmp(namea, nameb);
102}
103
104int __weak arch__compare_symbol_names_n(const char *namea, const char *nameb,
105 unsigned int n)
106{
107 return strncmp(namea, nameb, n);
108}
109
110int __weak arch__choose_best_symbol(struct symbol *syma,
111 struct symbol *symb __maybe_unused)
112{
113 /* Avoid "SyS" kernel syscall aliases */
114 if (strlen(syma->name) >= 3 && !strncmp(syma->name, "SyS", 3))
115 return SYMBOL_B;
116 if (strlen(syma->name) >= 10 && !strncmp(syma->name, "compat_SyS", 10))
117 return SYMBOL_B;
118
119 return SYMBOL_A;
120}
121
122static int choose_best_symbol(struct symbol *syma, struct symbol *symb)
123{
124 s64 a;
125 s64 b;
126 size_t na, nb;
127
128 /* Prefer a symbol with non zero length */
129 a = syma->end - syma->start;
130 b = symb->end - symb->start;
131 if ((b == 0) && (a > 0))
132 return SYMBOL_A;
133 else if ((a == 0) && (b > 0))
134 return SYMBOL_B;
135
136 /* Prefer a non weak symbol over a weak one */
137 a = syma->binding == STB_WEAK;
138 b = symb->binding == STB_WEAK;
139 if (b && !a)
140 return SYMBOL_A;
141 if (a && !b)
142 return SYMBOL_B;
143
144 /* Prefer a global symbol over a non global one */
145 a = syma->binding == STB_GLOBAL;
146 b = symb->binding == STB_GLOBAL;
147 if (a && !b)
148 return SYMBOL_A;
149 if (b && !a)
150 return SYMBOL_B;
151
152 /* Prefer a symbol with less underscores */
153 a = prefix_underscores_count(syma->name);
154 b = prefix_underscores_count(symb->name);
155 if (b > a)
156 return SYMBOL_A;
157 else if (a > b)
158 return SYMBOL_B;
159
160 /* Choose the symbol with the longest name */
161 na = strlen(syma->name);
162 nb = strlen(symb->name);
163 if (na > nb)
164 return SYMBOL_A;
165 else if (na < nb)
166 return SYMBOL_B;
167
168 return arch__choose_best_symbol(syma, symb);
169}
170
171void symbols__fixup_duplicate(struct rb_root *symbols)
172{
173 struct rb_node *nd;
174 struct symbol *curr, *next;
175
176 if (symbol_conf.allow_aliases)
177 return;
178
179 nd = rb_first(symbols);
180
181 while (nd) {
182 curr = rb_entry(nd, struct symbol, rb_node);
183again:
184 nd = rb_next(&curr->rb_node);
185 next = rb_entry(nd, struct symbol, rb_node);
186
187 if (!nd)
188 break;
189
190 if (curr->start != next->start)
191 continue;
192
193 if (choose_best_symbol(curr, next) == SYMBOL_A) {
194 rb_erase(&next->rb_node, symbols);
195 symbol__delete(next);
196 goto again;
197 } else {
198 nd = rb_next(&curr->rb_node);
199 rb_erase(&curr->rb_node, symbols);
200 symbol__delete(curr);
201 }
202 }
203}
204
205void symbols__fixup_end(struct rb_root *symbols)
206{
207 struct rb_node *nd, *prevnd = rb_first(symbols);
208 struct symbol *curr, *prev;
209
210 if (prevnd == NULL)
211 return;
212
213 curr = rb_entry(prevnd, struct symbol, rb_node);
214
215 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
216 prev = curr;
217 curr = rb_entry(nd, struct symbol, rb_node);
218
219 if (prev->end == prev->start && prev->end != curr->start)
220 arch__symbols__fixup_end(prev, curr);
221 }
222
223 /* Last entry */
224 if (curr->end == curr->start)
225 curr->end = roundup(curr->start, 4096) + 4096;
226}
227
228void map_groups__fixup_end(struct map_groups *mg)
229{
230 struct maps *maps = &mg->maps;
231 struct map *next, *curr;
232
233 down_write(&maps->lock);
234
235 curr = maps__first(maps);
236 if (curr == NULL)
237 goto out_unlock;
238
239 for (next = map__next(curr); next; next = map__next(curr)) {
240 if (!curr->end)
241 curr->end = next->start;
242 curr = next;
243 }
244
245 /*
246 * We still haven't the actual symbols, so guess the
247 * last map final address.
248 */
249 if (!curr->end)
250 curr->end = ~0ULL;
251
252out_unlock:
253 up_write(&maps->lock);
254}
255
256struct symbol *symbol__new(u64 start, u64 len, u8 binding, u8 type, const char *name)
257{
258 size_t namelen = strlen(name) + 1;
259 struct symbol *sym = calloc(1, (symbol_conf.priv_size +
260 sizeof(*sym) + namelen));
261 if (sym == NULL)
262 return NULL;
263
264 if (symbol_conf.priv_size) {
265 if (symbol_conf.init_annotation) {
266 struct annotation *notes = (void *)sym;
267 pthread_mutex_init(&notes->lock, NULL);
268 }
269 sym = ((void *)sym) + symbol_conf.priv_size;
270 }
271
272 sym->start = start;
273 sym->end = len ? start + len : start;
274 sym->type = type;
275 sym->binding = binding;
276 sym->namelen = namelen - 1;
277
278 pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n",
279 __func__, name, start, sym->end);
280 memcpy(sym->name, name, namelen);
281
282 return sym;
283}
284
285void symbol__delete(struct symbol *sym)
286{
287 free(((void *)sym) - symbol_conf.priv_size);
288}
289
290void symbols__delete(struct rb_root *symbols)
291{
292 struct symbol *pos;
293 struct rb_node *next = rb_first(symbols);
294
295 while (next) {
296 pos = rb_entry(next, struct symbol, rb_node);
297 next = rb_next(&pos->rb_node);
298 rb_erase(&pos->rb_node, symbols);
299 symbol__delete(pos);
300 }
301}
302
303void __symbols__insert(struct rb_root *symbols, struct symbol *sym, bool kernel)
304{
305 struct rb_node **p = &symbols->rb_node;
306 struct rb_node *parent = NULL;
307 const u64 ip = sym->start;
308 struct symbol *s;
309
310 if (kernel) {
311 const char *name = sym->name;
312 /*
313 * ppc64 uses function descriptors and appends a '.' to the
314 * start of every instruction address. Remove it.
315 */
316 if (name[0] == '.')
317 name++;
318 sym->idle = symbol__is_idle(name);
319 }
320
321 while (*p != NULL) {
322 parent = *p;
323 s = rb_entry(parent, struct symbol, rb_node);
324 if (ip < s->start)
325 p = &(*p)->rb_left;
326 else
327 p = &(*p)->rb_right;
328 }
329 rb_link_node(&sym->rb_node, parent, p);
330 rb_insert_color(&sym->rb_node, symbols);
331}
332
333void symbols__insert(struct rb_root *symbols, struct symbol *sym)
334{
335 __symbols__insert(symbols, sym, false);
336}
337
338static struct symbol *symbols__find(struct rb_root *symbols, u64 ip)
339{
340 struct rb_node *n;
341
342 if (symbols == NULL)
343 return NULL;
344
345 n = symbols->rb_node;
346
347 while (n) {
348 struct symbol *s = rb_entry(n, struct symbol, rb_node);
349
350 if (ip < s->start)
351 n = n->rb_left;
352 else if (ip > s->end || (ip == s->end && ip != s->start))
353 n = n->rb_right;
354 else
355 return s;
356 }
357
358 return NULL;
359}
360
361static struct symbol *symbols__first(struct rb_root *symbols)
362{
363 struct rb_node *n = rb_first(symbols);
364
365 if (n)
366 return rb_entry(n, struct symbol, rb_node);
367
368 return NULL;
369}
370
371static struct symbol *symbols__last(struct rb_root *symbols)
372{
373 struct rb_node *n = rb_last(symbols);
374
375 if (n)
376 return rb_entry(n, struct symbol, rb_node);
377
378 return NULL;
379}
380
381static struct symbol *symbols__next(struct symbol *sym)
382{
383 struct rb_node *n = rb_next(&sym->rb_node);
384
385 if (n)
386 return rb_entry(n, struct symbol, rb_node);
387
388 return NULL;
389}
390
391static void symbols__insert_by_name(struct rb_root *symbols, struct symbol *sym)
392{
393 struct rb_node **p = &symbols->rb_node;
394 struct rb_node *parent = NULL;
395 struct symbol_name_rb_node *symn, *s;
396
397 symn = container_of(sym, struct symbol_name_rb_node, sym);
398
399 while (*p != NULL) {
400 parent = *p;
401 s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
402 if (strcmp(sym->name, s->sym.name) < 0)
403 p = &(*p)->rb_left;
404 else
405 p = &(*p)->rb_right;
406 }
407 rb_link_node(&symn->rb_node, parent, p);
408 rb_insert_color(&symn->rb_node, symbols);
409}
410
411static void symbols__sort_by_name(struct rb_root *symbols,
412 struct rb_root *source)
413{
414 struct rb_node *nd;
415
416 for (nd = rb_first(source); nd; nd = rb_next(nd)) {
417 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
418 symbols__insert_by_name(symbols, pos);
419 }
420}
421
422int symbol__match_symbol_name(const char *name, const char *str,
423 enum symbol_tag_include includes)
424{
425 const char *versioning;
426
427 if (includes == SYMBOL_TAG_INCLUDE__DEFAULT_ONLY &&
428 (versioning = strstr(name, "@@"))) {
429 int len = strlen(str);
430
431 if (len < versioning - name)
432 len = versioning - name;
433
434 return arch__compare_symbol_names_n(name, str, len);
435 } else
436 return arch__compare_symbol_names(name, str);
437}
438
439static struct symbol *symbols__find_by_name(struct rb_root *symbols,
440 const char *name,
441 enum symbol_tag_include includes)
442{
443 struct rb_node *n;
444 struct symbol_name_rb_node *s = NULL;
445
446 if (symbols == NULL)
447 return NULL;
448
449 n = symbols->rb_node;
450
451 while (n) {
452 int cmp;
453
454 s = rb_entry(n, struct symbol_name_rb_node, rb_node);
455 cmp = symbol__match_symbol_name(s->sym.name, name, includes);
456
457 if (cmp > 0)
458 n = n->rb_left;
459 else if (cmp < 0)
460 n = n->rb_right;
461 else
462 break;
463 }
464
465 if (n == NULL)
466 return NULL;
467
468 if (includes != SYMBOL_TAG_INCLUDE__DEFAULT_ONLY)
469 /* return first symbol that has same name (if any) */
470 for (n = rb_prev(n); n; n = rb_prev(n)) {
471 struct symbol_name_rb_node *tmp;
472
473 tmp = rb_entry(n, struct symbol_name_rb_node, rb_node);
474 if (arch__compare_symbol_names(tmp->sym.name, s->sym.name))
475 break;
476
477 s = tmp;
478 }
479
480 return &s->sym;
481}
482
483void dso__reset_find_symbol_cache(struct dso *dso)
484{
485 dso->last_find_result.addr = 0;
486 dso->last_find_result.symbol = NULL;
487}
488
489void dso__insert_symbol(struct dso *dso, struct symbol *sym)
490{
491 __symbols__insert(&dso->symbols, sym, dso->kernel);
492
493 /* update the symbol cache if necessary */
494 if (dso->last_find_result.addr >= sym->start &&
495 (dso->last_find_result.addr < sym->end ||
496 sym->start == sym->end)) {
497 dso->last_find_result.symbol = sym;
498 }
499}
500
501struct symbol *dso__find_symbol(struct dso *dso, u64 addr)
502{
503 if (dso->last_find_result.addr != addr || dso->last_find_result.symbol == NULL) {
504 dso->last_find_result.addr = addr;
505 dso->last_find_result.symbol = symbols__find(&dso->symbols, addr);
506 }
507
508 return dso->last_find_result.symbol;
509}
510
511struct symbol *dso__first_symbol(struct dso *dso)
512{
513 return symbols__first(&dso->symbols);
514}
515
516struct symbol *dso__last_symbol(struct dso *dso)
517{
518 return symbols__last(&dso->symbols);
519}
520
521struct symbol *dso__next_symbol(struct symbol *sym)
522{
523 return symbols__next(sym);
524}
525
526struct symbol *symbol__next_by_name(struct symbol *sym)
527{
528 struct symbol_name_rb_node *s = container_of(sym, struct symbol_name_rb_node, sym);
529 struct rb_node *n = rb_next(&s->rb_node);
530
531 return n ? &rb_entry(n, struct symbol_name_rb_node, rb_node)->sym : NULL;
532}
533
534 /*
535 * Returns first symbol that matched with @name.
536 */
537struct symbol *dso__find_symbol_by_name(struct dso *dso, const char *name)
538{
539 struct symbol *s = symbols__find_by_name(&dso->symbol_names, name,
540 SYMBOL_TAG_INCLUDE__NONE);
541 if (!s)
542 s = symbols__find_by_name(&dso->symbol_names, name,
543 SYMBOL_TAG_INCLUDE__DEFAULT_ONLY);
544 return s;
545}
546
547void dso__sort_by_name(struct dso *dso)
548{
549 dso__set_sorted_by_name(dso);
550 return symbols__sort_by_name(&dso->symbol_names, &dso->symbols);
551}
552
553int modules__parse(const char *filename, void *arg,
554 int (*process_module)(void *arg, const char *name,
555 u64 start, u64 size))
556{
557 char *line = NULL;
558 size_t n;
559 FILE *file;
560 int err = 0;
561
562 file = fopen(filename, "r");
563 if (file == NULL)
564 return -1;
565
566 while (1) {
567 char name[PATH_MAX];
568 u64 start, size;
569 char *sep, *endptr;
570 ssize_t line_len;
571
572 line_len = getline(&line, &n, file);
573 if (line_len < 0) {
574 if (feof(file))
575 break;
576 err = -1;
577 goto out;
578 }
579
580 if (!line) {
581 err = -1;
582 goto out;
583 }
584
585 line[--line_len] = '\0'; /* \n */
586
587 sep = strrchr(line, 'x');
588 if (sep == NULL)
589 continue;
590
591 hex2u64(sep + 1, &start);
592
593 sep = strchr(line, ' ');
594 if (sep == NULL)
595 continue;
596
597 *sep = '\0';
598
599 scnprintf(name, sizeof(name), "[%s]", line);
600
601 size = strtoul(sep + 1, &endptr, 0);
602 if (*endptr != ' ' && *endptr != '\t')
603 continue;
604
605 err = process_module(arg, name, start, size);
606 if (err)
607 break;
608 }
609out:
610 free(line);
611 fclose(file);
612 return err;
613}
614
615/*
616 * These are symbols in the kernel image, so make sure that
617 * sym is from a kernel DSO.
618 */
619static bool symbol__is_idle(const char *name)
620{
621 const char * const idle_symbols[] = {
622 "cpu_idle",
623 "cpu_startup_entry",
624 "intel_idle",
625 "default_idle",
626 "native_safe_halt",
627 "enter_idle",
628 "exit_idle",
629 "mwait_idle",
630 "mwait_idle_with_hints",
631 "poll_idle",
632 "ppc64_runlatch_off",
633 "pseries_dedicated_idle_sleep",
634 NULL
635 };
636 int i;
637
638 for (i = 0; idle_symbols[i]; i++) {
639 if (!strcmp(idle_symbols[i], name))
640 return true;
641 }
642
643 return false;
644}
645
646static int map__process_kallsym_symbol(void *arg, const char *name,
647 char type, u64 start)
648{
649 struct symbol *sym;
650 struct dso *dso = arg;
651 struct rb_root *root = &dso->symbols;
652
653 if (!symbol_type__filter(type))
654 return 0;
655
656 /*
657 * module symbols are not sorted so we add all
658 * symbols, setting length to 0, and rely on
659 * symbols__fixup_end() to fix it up.
660 */
661 sym = symbol__new(start, 0, kallsyms2elf_binding(type), kallsyms2elf_type(type), name);
662 if (sym == NULL)
663 return -ENOMEM;
664 /*
665 * We will pass the symbols to the filter later, in
666 * map__split_kallsyms, when we have split the maps per module
667 */
668 __symbols__insert(root, sym, !strchr(name, '['));
669
670 return 0;
671}
672
673/*
674 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
675 * so that we can in the next step set the symbol ->end address and then
676 * call kernel_maps__split_kallsyms.
677 */
678static int dso__load_all_kallsyms(struct dso *dso, const char *filename)
679{
680 return kallsyms__parse(filename, dso, map__process_kallsym_symbol);
681}
682
683static int map_groups__split_kallsyms_for_kcore(struct map_groups *kmaps, struct dso *dso)
684{
685 struct map *curr_map;
686 struct symbol *pos;
687 int count = 0;
688 struct rb_root old_root = dso->symbols;
689 struct rb_root *root = &dso->symbols;
690 struct rb_node *next = rb_first(root);
691
692 if (!kmaps)
693 return -1;
694
695 *root = RB_ROOT;
696
697 while (next) {
698 char *module;
699
700 pos = rb_entry(next, struct symbol, rb_node);
701 next = rb_next(&pos->rb_node);
702
703 rb_erase_init(&pos->rb_node, &old_root);
704
705 module = strchr(pos->name, '\t');
706 if (module)
707 *module = '\0';
708
709 curr_map = map_groups__find(kmaps, pos->start);
710
711 if (!curr_map) {
712 symbol__delete(pos);
713 continue;
714 }
715
716 pos->start -= curr_map->start - curr_map->pgoff;
717 if (pos->end > curr_map->end)
718 pos->end = curr_map->end;
719 if (pos->end)
720 pos->end -= curr_map->start - curr_map->pgoff;
721 symbols__insert(&curr_map->dso->symbols, pos);
722 ++count;
723 }
724
725 /* Symbols have been adjusted */
726 dso->adjust_symbols = 1;
727
728 return count;
729}
730
731/*
732 * Split the symbols into maps, making sure there are no overlaps, i.e. the
733 * kernel range is broken in several maps, named [kernel].N, as we don't have
734 * the original ELF section names vmlinux have.
735 */
736static int map_groups__split_kallsyms(struct map_groups *kmaps, struct dso *dso, u64 delta,
737 struct map *initial_map)
738{
739 struct machine *machine;
740 struct map *curr_map = initial_map;
741 struct symbol *pos;
742 int count = 0, moved = 0;
743 struct rb_root *root = &dso->symbols;
744 struct rb_node *next = rb_first(root);
745 int kernel_range = 0;
746 bool x86_64;
747
748 if (!kmaps)
749 return -1;
750
751 machine = kmaps->machine;
752
753 x86_64 = machine__is(machine, "x86_64");
754
755 while (next) {
756 char *module;
757
758 pos = rb_entry(next, struct symbol, rb_node);
759 next = rb_next(&pos->rb_node);
760
761 module = strchr(pos->name, '\t');
762 if (module) {
763 if (!symbol_conf.use_modules)
764 goto discard_symbol;
765
766 *module++ = '\0';
767
768 if (strcmp(curr_map->dso->short_name, module)) {
769 if (curr_map != initial_map &&
770 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
771 machine__is_default_guest(machine)) {
772 /*
773 * We assume all symbols of a module are
774 * continuous in * kallsyms, so curr_map
775 * points to a module and all its
776 * symbols are in its kmap. Mark it as
777 * loaded.
778 */
779 dso__set_loaded(curr_map->dso);
780 }
781
782 curr_map = map_groups__find_by_name(kmaps, module);
783 if (curr_map == NULL) {
784 pr_debug("%s/proc/{kallsyms,modules} "
785 "inconsistency while looking "
786 "for \"%s\" module!\n",
787 machine->root_dir, module);
788 curr_map = initial_map;
789 goto discard_symbol;
790 }
791
792 if (curr_map->dso->loaded &&
793 !machine__is_default_guest(machine))
794 goto discard_symbol;
795 }
796 /*
797 * So that we look just like we get from .ko files,
798 * i.e. not prelinked, relative to initial_map->start.
799 */
800 pos->start = curr_map->map_ip(curr_map, pos->start);
801 pos->end = curr_map->map_ip(curr_map, pos->end);
802 } else if (x86_64 && is_entry_trampoline(pos->name)) {
803 /*
804 * These symbols are not needed anymore since the
805 * trampoline maps refer to the text section and it's
806 * symbols instead. Avoid having to deal with
807 * relocations, and the assumption that the first symbol
808 * is the start of kernel text, by simply removing the
809 * symbols at this point.
810 */
811 goto discard_symbol;
812 } else if (curr_map != initial_map) {
813 char dso_name[PATH_MAX];
814 struct dso *ndso;
815
816 if (delta) {
817 /* Kernel was relocated at boot time */
818 pos->start -= delta;
819 pos->end -= delta;
820 }
821
822 if (count == 0) {
823 curr_map = initial_map;
824 goto add_symbol;
825 }
826
827 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
828 snprintf(dso_name, sizeof(dso_name),
829 "[guest.kernel].%d",
830 kernel_range++);
831 else
832 snprintf(dso_name, sizeof(dso_name),
833 "[kernel].%d",
834 kernel_range++);
835
836 ndso = dso__new(dso_name);
837 if (ndso == NULL)
838 return -1;
839
840 ndso->kernel = dso->kernel;
841
842 curr_map = map__new2(pos->start, ndso);
843 if (curr_map == NULL) {
844 dso__put(ndso);
845 return -1;
846 }
847
848 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
849 map_groups__insert(kmaps, curr_map);
850 ++kernel_range;
851 } else if (delta) {
852 /* Kernel was relocated at boot time */
853 pos->start -= delta;
854 pos->end -= delta;
855 }
856add_symbol:
857 if (curr_map != initial_map) {
858 rb_erase(&pos->rb_node, root);
859 symbols__insert(&curr_map->dso->symbols, pos);
860 ++moved;
861 } else
862 ++count;
863
864 continue;
865discard_symbol:
866 rb_erase(&pos->rb_node, root);
867 symbol__delete(pos);
868 }
869
870 if (curr_map != initial_map &&
871 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
872 machine__is_default_guest(kmaps->machine)) {
873 dso__set_loaded(curr_map->dso);
874 }
875
876 return count + moved;
877}
878
879bool symbol__restricted_filename(const char *filename,
880 const char *restricted_filename)
881{
882 bool restricted = false;
883
884 if (symbol_conf.kptr_restrict) {
885 char *r = realpath(filename, NULL);
886
887 if (r != NULL) {
888 restricted = strcmp(r, restricted_filename) == 0;
889 free(r);
890 return restricted;
891 }
892 }
893
894 return restricted;
895}
896
897struct module_info {
898 struct rb_node rb_node;
899 char *name;
900 u64 start;
901};
902
903static void add_module(struct module_info *mi, struct rb_root *modules)
904{
905 struct rb_node **p = &modules->rb_node;
906 struct rb_node *parent = NULL;
907 struct module_info *m;
908
909 while (*p != NULL) {
910 parent = *p;
911 m = rb_entry(parent, struct module_info, rb_node);
912 if (strcmp(mi->name, m->name) < 0)
913 p = &(*p)->rb_left;
914 else
915 p = &(*p)->rb_right;
916 }
917 rb_link_node(&mi->rb_node, parent, p);
918 rb_insert_color(&mi->rb_node, modules);
919}
920
921static void delete_modules(struct rb_root *modules)
922{
923 struct module_info *mi;
924 struct rb_node *next = rb_first(modules);
925
926 while (next) {
927 mi = rb_entry(next, struct module_info, rb_node);
928 next = rb_next(&mi->rb_node);
929 rb_erase(&mi->rb_node, modules);
930 zfree(&mi->name);
931 free(mi);
932 }
933}
934
935static struct module_info *find_module(const char *name,
936 struct rb_root *modules)
937{
938 struct rb_node *n = modules->rb_node;
939
940 while (n) {
941 struct module_info *m;
942 int cmp;
943
944 m = rb_entry(n, struct module_info, rb_node);
945 cmp = strcmp(name, m->name);
946 if (cmp < 0)
947 n = n->rb_left;
948 else if (cmp > 0)
949 n = n->rb_right;
950 else
951 return m;
952 }
953
954 return NULL;
955}
956
957static int __read_proc_modules(void *arg, const char *name, u64 start,
958 u64 size __maybe_unused)
959{
960 struct rb_root *modules = arg;
961 struct module_info *mi;
962
963 mi = zalloc(sizeof(struct module_info));
964 if (!mi)
965 return -ENOMEM;
966
967 mi->name = strdup(name);
968 mi->start = start;
969
970 if (!mi->name) {
971 free(mi);
972 return -ENOMEM;
973 }
974
975 add_module(mi, modules);
976
977 return 0;
978}
979
980static int read_proc_modules(const char *filename, struct rb_root *modules)
981{
982 if (symbol__restricted_filename(filename, "/proc/modules"))
983 return -1;
984
985 if (modules__parse(filename, modules, __read_proc_modules)) {
986 delete_modules(modules);
987 return -1;
988 }
989
990 return 0;
991}
992
993int compare_proc_modules(const char *from, const char *to)
994{
995 struct rb_root from_modules = RB_ROOT;
996 struct rb_root to_modules = RB_ROOT;
997 struct rb_node *from_node, *to_node;
998 struct module_info *from_m, *to_m;
999 int ret = -1;
1000
1001 if (read_proc_modules(from, &from_modules))
1002 return -1;
1003
1004 if (read_proc_modules(to, &to_modules))
1005 goto out_delete_from;
1006
1007 from_node = rb_first(&from_modules);
1008 to_node = rb_first(&to_modules);
1009 while (from_node) {
1010 if (!to_node)
1011 break;
1012
1013 from_m = rb_entry(from_node, struct module_info, rb_node);
1014 to_m = rb_entry(to_node, struct module_info, rb_node);
1015
1016 if (from_m->start != to_m->start ||
1017 strcmp(from_m->name, to_m->name))
1018 break;
1019
1020 from_node = rb_next(from_node);
1021 to_node = rb_next(to_node);
1022 }
1023
1024 if (!from_node && !to_node)
1025 ret = 0;
1026
1027 delete_modules(&to_modules);
1028out_delete_from:
1029 delete_modules(&from_modules);
1030
1031 return ret;
1032}
1033
1034struct map *map_groups__first(struct map_groups *mg)
1035{
1036 return maps__first(&mg->maps);
1037}
1038
1039static int do_validate_kcore_modules(const char *filename,
1040 struct map_groups *kmaps)
1041{
1042 struct rb_root modules = RB_ROOT;
1043 struct map *old_map;
1044 int err;
1045
1046 err = read_proc_modules(filename, &modules);
1047 if (err)
1048 return err;
1049
1050 old_map = map_groups__first(kmaps);
1051 while (old_map) {
1052 struct map *next = map_groups__next(old_map);
1053 struct module_info *mi;
1054
1055 if (!__map__is_kmodule(old_map)) {
1056 old_map = next;
1057 continue;
1058 }
1059
1060 /* Module must be in memory at the same address */
1061 mi = find_module(old_map->dso->short_name, &modules);
1062 if (!mi || mi->start != old_map->start) {
1063 err = -EINVAL;
1064 goto out;
1065 }
1066
1067 old_map = next;
1068 }
1069out:
1070 delete_modules(&modules);
1071 return err;
1072}
1073
1074/*
1075 * If kallsyms is referenced by name then we look for filename in the same
1076 * directory.
1077 */
1078static bool filename_from_kallsyms_filename(char *filename,
1079 const char *base_name,
1080 const char *kallsyms_filename)
1081{
1082 char *name;
1083
1084 strcpy(filename, kallsyms_filename);
1085 name = strrchr(filename, '/');
1086 if (!name)
1087 return false;
1088
1089 name += 1;
1090
1091 if (!strcmp(name, "kallsyms")) {
1092 strcpy(name, base_name);
1093 return true;
1094 }
1095
1096 return false;
1097}
1098
1099static int validate_kcore_modules(const char *kallsyms_filename,
1100 struct map *map)
1101{
1102 struct map_groups *kmaps = map__kmaps(map);
1103 char modules_filename[PATH_MAX];
1104
1105 if (!kmaps)
1106 return -EINVAL;
1107
1108 if (!filename_from_kallsyms_filename(modules_filename, "modules",
1109 kallsyms_filename))
1110 return -EINVAL;
1111
1112 if (do_validate_kcore_modules(modules_filename, kmaps))
1113 return -EINVAL;
1114
1115 return 0;
1116}
1117
1118static int validate_kcore_addresses(const char *kallsyms_filename,
1119 struct map *map)
1120{
1121 struct kmap *kmap = map__kmap(map);
1122
1123 if (!kmap)
1124 return -EINVAL;
1125
1126 if (kmap->ref_reloc_sym && kmap->ref_reloc_sym->name) {
1127 u64 start;
1128
1129 if (kallsyms__get_function_start(kallsyms_filename,
1130 kmap->ref_reloc_sym->name, &start))
1131 return -ENOENT;
1132 if (start != kmap->ref_reloc_sym->addr)
1133 return -EINVAL;
1134 }
1135
1136 return validate_kcore_modules(kallsyms_filename, map);
1137}
1138
1139struct kcore_mapfn_data {
1140 struct dso *dso;
1141 struct list_head maps;
1142};
1143
1144static int kcore_mapfn(u64 start, u64 len, u64 pgoff, void *data)
1145{
1146 struct kcore_mapfn_data *md = data;
1147 struct map *map;
1148
1149 map = map__new2(start, md->dso);
1150 if (map == NULL)
1151 return -ENOMEM;
1152
1153 map->end = map->start + len;
1154 map->pgoff = pgoff;
1155
1156 list_add(&map->node, &md->maps);
1157
1158 return 0;
1159}
1160
1161static int dso__load_kcore(struct dso *dso, struct map *map,
1162 const char *kallsyms_filename)
1163{
1164 struct map_groups *kmaps = map__kmaps(map);
1165 struct kcore_mapfn_data md;
1166 struct map *old_map, *new_map, *replacement_map = NULL;
1167 struct machine *machine;
1168 bool is_64_bit;
1169 int err, fd;
1170 char kcore_filename[PATH_MAX];
1171 u64 stext;
1172
1173 if (!kmaps)
1174 return -EINVAL;
1175
1176 machine = kmaps->machine;
1177
1178 /* This function requires that the map is the kernel map */
1179 if (!__map__is_kernel(map))
1180 return -EINVAL;
1181
1182 if (!filename_from_kallsyms_filename(kcore_filename, "kcore",
1183 kallsyms_filename))
1184 return -EINVAL;
1185
1186 /* Modules and kernel must be present at their original addresses */
1187 if (validate_kcore_addresses(kallsyms_filename, map))
1188 return -EINVAL;
1189
1190 md.dso = dso;
1191 INIT_LIST_HEAD(&md.maps);
1192
1193 fd = open(kcore_filename, O_RDONLY);
1194 if (fd < 0) {
1195 pr_debug("Failed to open %s. Note /proc/kcore requires CAP_SYS_RAWIO capability to access.\n",
1196 kcore_filename);
1197 return -EINVAL;
1198 }
1199
1200 /* Read new maps into temporary lists */
1201 err = file__read_maps(fd, map->prot & PROT_EXEC, kcore_mapfn, &md,
1202 &is_64_bit);
1203 if (err)
1204 goto out_err;
1205 dso->is_64_bit = is_64_bit;
1206
1207 if (list_empty(&md.maps)) {
1208 err = -EINVAL;
1209 goto out_err;
1210 }
1211
1212 /* Remove old maps */
1213 old_map = map_groups__first(kmaps);
1214 while (old_map) {
1215 struct map *next = map_groups__next(old_map);
1216
1217 if (old_map != map)
1218 map_groups__remove(kmaps, old_map);
1219 old_map = next;
1220 }
1221 machine->trampolines_mapped = false;
1222
1223 /* Find the kernel map using the '_stext' symbol */
1224 if (!kallsyms__get_function_start(kallsyms_filename, "_stext", &stext)) {
1225 list_for_each_entry(new_map, &md.maps, node) {
1226 if (stext >= new_map->start && stext < new_map->end) {
1227 replacement_map = new_map;
1228 break;
1229 }
1230 }
1231 }
1232
1233 if (!replacement_map)
1234 replacement_map = list_entry(md.maps.next, struct map, node);
1235
1236 /* Add new maps */
1237 while (!list_empty(&md.maps)) {
1238 new_map = list_entry(md.maps.next, struct map, node);
1239 list_del_init(&new_map->node);
1240 if (new_map == replacement_map) {
1241 map->start = new_map->start;
1242 map->end = new_map->end;
1243 map->pgoff = new_map->pgoff;
1244 map->map_ip = new_map->map_ip;
1245 map->unmap_ip = new_map->unmap_ip;
1246 /* Ensure maps are correctly ordered */
1247 map__get(map);
1248 map_groups__remove(kmaps, map);
1249 map_groups__insert(kmaps, map);
1250 map__put(map);
1251 } else {
1252 map_groups__insert(kmaps, new_map);
1253 }
1254
1255 map__put(new_map);
1256 }
1257
1258 if (machine__is(machine, "x86_64")) {
1259 u64 addr;
1260
1261 /*
1262 * If one of the corresponding symbols is there, assume the
1263 * entry trampoline maps are too.
1264 */
1265 if (!kallsyms__get_function_start(kallsyms_filename,
1266 ENTRY_TRAMPOLINE_NAME,
1267 &addr))
1268 machine->trampolines_mapped = true;
1269 }
1270
1271 /*
1272 * Set the data type and long name so that kcore can be read via
1273 * dso__data_read_addr().
1274 */
1275 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1276 dso->binary_type = DSO_BINARY_TYPE__GUEST_KCORE;
1277 else
1278 dso->binary_type = DSO_BINARY_TYPE__KCORE;
1279 dso__set_long_name(dso, strdup(kcore_filename), true);
1280
1281 close(fd);
1282
1283 if (map->prot & PROT_EXEC)
1284 pr_debug("Using %s for kernel object code\n", kcore_filename);
1285 else
1286 pr_debug("Using %s for kernel data\n", kcore_filename);
1287
1288 return 0;
1289
1290out_err:
1291 while (!list_empty(&md.maps)) {
1292 map = list_entry(md.maps.next, struct map, node);
1293 list_del_init(&map->node);
1294 map__put(map);
1295 }
1296 close(fd);
1297 return -EINVAL;
1298}
1299
1300/*
1301 * If the kernel is relocated at boot time, kallsyms won't match. Compute the
1302 * delta based on the relocation reference symbol.
1303 */
1304static int kallsyms__delta(struct kmap *kmap, const char *filename, u64 *delta)
1305{
1306 u64 addr;
1307
1308 if (!kmap->ref_reloc_sym || !kmap->ref_reloc_sym->name)
1309 return 0;
1310
1311 if (kallsyms__get_function_start(filename, kmap->ref_reloc_sym->name, &addr))
1312 return -1;
1313
1314 *delta = addr - kmap->ref_reloc_sym->addr;
1315 return 0;
1316}
1317
1318int __dso__load_kallsyms(struct dso *dso, const char *filename,
1319 struct map *map, bool no_kcore)
1320{
1321 struct kmap *kmap = map__kmap(map);
1322 u64 delta = 0;
1323
1324 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
1325 return -1;
1326
1327 if (!kmap || !kmap->kmaps)
1328 return -1;
1329
1330 if (dso__load_all_kallsyms(dso, filename) < 0)
1331 return -1;
1332
1333 if (kallsyms__delta(kmap, filename, &delta))
1334 return -1;
1335
1336 symbols__fixup_end(&dso->symbols);
1337 symbols__fixup_duplicate(&dso->symbols);
1338
1339 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1340 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
1341 else
1342 dso->symtab_type = DSO_BINARY_TYPE__KALLSYMS;
1343
1344 if (!no_kcore && !dso__load_kcore(dso, map, filename))
1345 return map_groups__split_kallsyms_for_kcore(kmap->kmaps, dso);
1346 else
1347 return map_groups__split_kallsyms(kmap->kmaps, dso, delta, map);
1348}
1349
1350int dso__load_kallsyms(struct dso *dso, const char *filename,
1351 struct map *map)
1352{
1353 return __dso__load_kallsyms(dso, filename, map, false);
1354}
1355
1356static int dso__load_perf_map(const char *map_path, struct dso *dso)
1357{
1358 char *line = NULL;
1359 size_t n;
1360 FILE *file;
1361 int nr_syms = 0;
1362
1363 file = fopen(map_path, "r");
1364 if (file == NULL)
1365 goto out_failure;
1366
1367 while (!feof(file)) {
1368 u64 start, size;
1369 struct symbol *sym;
1370 int line_len, len;
1371
1372 line_len = getline(&line, &n, file);
1373 if (line_len < 0)
1374 break;
1375
1376 if (!line)
1377 goto out_failure;
1378
1379 line[--line_len] = '\0'; /* \n */
1380
1381 len = hex2u64(line, &start);
1382
1383 len++;
1384 if (len + 2 >= line_len)
1385 continue;
1386
1387 len += hex2u64(line + len, &size);
1388
1389 len++;
1390 if (len + 2 >= line_len)
1391 continue;
1392
1393 sym = symbol__new(start, size, STB_GLOBAL, STT_FUNC, line + len);
1394
1395 if (sym == NULL)
1396 goto out_delete_line;
1397
1398 symbols__insert(&dso->symbols, sym);
1399 nr_syms++;
1400 }
1401
1402 free(line);
1403 fclose(file);
1404
1405 return nr_syms;
1406
1407out_delete_line:
1408 free(line);
1409out_failure:
1410 return -1;
1411}
1412
1413static bool dso__is_compatible_symtab_type(struct dso *dso, bool kmod,
1414 enum dso_binary_type type)
1415{
1416 switch (type) {
1417 case DSO_BINARY_TYPE__JAVA_JIT:
1418 case DSO_BINARY_TYPE__DEBUGLINK:
1419 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
1420 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
1421 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
1422 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
1423 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
1424 return !kmod && dso->kernel == DSO_TYPE_USER;
1425
1426 case DSO_BINARY_TYPE__KALLSYMS:
1427 case DSO_BINARY_TYPE__VMLINUX:
1428 case DSO_BINARY_TYPE__KCORE:
1429 return dso->kernel == DSO_TYPE_KERNEL;
1430
1431 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
1432 case DSO_BINARY_TYPE__GUEST_VMLINUX:
1433 case DSO_BINARY_TYPE__GUEST_KCORE:
1434 return dso->kernel == DSO_TYPE_GUEST_KERNEL;
1435
1436 case DSO_BINARY_TYPE__GUEST_KMODULE:
1437 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
1438 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
1439 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
1440 /*
1441 * kernel modules know their symtab type - it's set when
1442 * creating a module dso in machine__findnew_module_map().
1443 */
1444 return kmod && dso->symtab_type == type;
1445
1446 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
1447 case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO:
1448 return true;
1449
1450 case DSO_BINARY_TYPE__NOT_FOUND:
1451 default:
1452 return false;
1453 }
1454}
1455
1456/* Checks for the existence of the perf-<pid>.map file in two different
1457 * locations. First, if the process is a separate mount namespace, check in
1458 * that namespace using the pid of the innermost pid namespace. If's not in a
1459 * namespace, or the file can't be found there, try in the mount namespace of
1460 * the tracing process using our view of its pid.
1461 */
1462static int dso__find_perf_map(char *filebuf, size_t bufsz,
1463 struct nsinfo **nsip)
1464{
1465 struct nscookie nsc;
1466 struct nsinfo *nsi;
1467 struct nsinfo *nnsi;
1468 int rc = -1;
1469
1470 nsi = *nsip;
1471
1472 if (nsi->need_setns) {
1473 snprintf(filebuf, bufsz, "/tmp/perf-%d.map", nsi->nstgid);
1474 nsinfo__mountns_enter(nsi, &nsc);
1475 rc = access(filebuf, R_OK);
1476 nsinfo__mountns_exit(&nsc);
1477 if (rc == 0)
1478 return rc;
1479 }
1480
1481 nnsi = nsinfo__copy(nsi);
1482 if (nnsi) {
1483 nsinfo__put(nsi);
1484
1485 nnsi->need_setns = false;
1486 snprintf(filebuf, bufsz, "/tmp/perf-%d.map", nnsi->tgid);
1487 *nsip = nnsi;
1488 rc = 0;
1489 }
1490
1491 return rc;
1492}
1493
1494int dso__load(struct dso *dso, struct map *map)
1495{
1496 char *name;
1497 int ret = -1;
1498 u_int i;
1499 struct machine *machine;
1500 char *root_dir = (char *) "";
1501 int ss_pos = 0;
1502 struct symsrc ss_[2];
1503 struct symsrc *syms_ss = NULL, *runtime_ss = NULL;
1504 bool kmod;
1505 bool perfmap;
1506 unsigned char build_id[BUILD_ID_SIZE];
1507 struct nscookie nsc;
1508 char newmapname[PATH_MAX];
1509 const char *map_path = dso->long_name;
1510
1511 perfmap = strncmp(dso->name, "/tmp/perf-", 10) == 0;
1512 if (perfmap) {
1513 if (dso->nsinfo && (dso__find_perf_map(newmapname,
1514 sizeof(newmapname), &dso->nsinfo) == 0)) {
1515 map_path = newmapname;
1516 }
1517 }
1518
1519 nsinfo__mountns_enter(dso->nsinfo, &nsc);
1520 pthread_mutex_lock(&dso->lock);
1521
1522 /* check again under the dso->lock */
1523 if (dso__loaded(dso)) {
1524 ret = 1;
1525 goto out;
1526 }
1527
1528 if (map->groups && map->groups->machine)
1529 machine = map->groups->machine;
1530 else
1531 machine = NULL;
1532
1533 if (dso->kernel) {
1534 if (dso->kernel == DSO_TYPE_KERNEL)
1535 ret = dso__load_kernel_sym(dso, map);
1536 else if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1537 ret = dso__load_guest_kernel_sym(dso, map);
1538
1539 if (machine__is(machine, "x86_64"))
1540 machine__map_x86_64_entry_trampolines(machine, dso);
1541 goto out;
1542 }
1543
1544 dso->adjust_symbols = 0;
1545
1546 if (perfmap) {
1547 struct stat st;
1548
1549 if (lstat(map_path, &st) < 0)
1550 goto out;
1551
1552 if (!symbol_conf.force && st.st_uid && (st.st_uid != geteuid())) {
1553 pr_warning("File %s not owned by current user or root, "
1554 "ignoring it (use -f to override).\n", map_path);
1555 goto out;
1556 }
1557
1558 ret = dso__load_perf_map(map_path, dso);
1559 dso->symtab_type = ret > 0 ? DSO_BINARY_TYPE__JAVA_JIT :
1560 DSO_BINARY_TYPE__NOT_FOUND;
1561 goto out;
1562 }
1563
1564 if (machine)
1565 root_dir = machine->root_dir;
1566
1567 name = malloc(PATH_MAX);
1568 if (!name)
1569 goto out;
1570
1571 kmod = dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
1572 dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
1573 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE ||
1574 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
1575
1576
1577 /*
1578 * Read the build id if possible. This is required for
1579 * DSO_BINARY_TYPE__BUILDID_DEBUGINFO to work
1580 */
1581 if (!dso->has_build_id &&
1582 is_regular_file(dso->long_name)) {
1583 __symbol__join_symfs(name, PATH_MAX, dso->long_name);
1584 if (filename__read_build_id(name, build_id, BUILD_ID_SIZE) > 0)
1585 dso__set_build_id(dso, build_id);
1586 }
1587
1588 /*
1589 * Iterate over candidate debug images.
1590 * Keep track of "interesting" ones (those which have a symtab, dynsym,
1591 * and/or opd section) for processing.
1592 */
1593 for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) {
1594 struct symsrc *ss = &ss_[ss_pos];
1595 bool next_slot = false;
1596 bool is_reg;
1597 bool nsexit;
1598 int sirc = -1;
1599
1600 enum dso_binary_type symtab_type = binary_type_symtab[i];
1601
1602 nsexit = (symtab_type == DSO_BINARY_TYPE__BUILD_ID_CACHE ||
1603 symtab_type == DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO);
1604
1605 if (!dso__is_compatible_symtab_type(dso, kmod, symtab_type))
1606 continue;
1607
1608 if (dso__read_binary_type_filename(dso, symtab_type,
1609 root_dir, name, PATH_MAX))
1610 continue;
1611
1612 if (nsexit)
1613 nsinfo__mountns_exit(&nsc);
1614
1615 is_reg = is_regular_file(name);
1616 if (is_reg)
1617 sirc = symsrc__init(ss, dso, name, symtab_type);
1618
1619 if (nsexit)
1620 nsinfo__mountns_enter(dso->nsinfo, &nsc);
1621
1622 if (!is_reg || sirc < 0)
1623 continue;
1624
1625 if (!syms_ss && symsrc__has_symtab(ss)) {
1626 syms_ss = ss;
1627 next_slot = true;
1628 if (!dso->symsrc_filename)
1629 dso->symsrc_filename = strdup(name);
1630 }
1631
1632 if (!runtime_ss && symsrc__possibly_runtime(ss)) {
1633 runtime_ss = ss;
1634 next_slot = true;
1635 }
1636
1637 if (next_slot) {
1638 ss_pos++;
1639
1640 if (syms_ss && runtime_ss)
1641 break;
1642 } else {
1643 symsrc__destroy(ss);
1644 }
1645
1646 }
1647
1648 if (!runtime_ss && !syms_ss)
1649 goto out_free;
1650
1651 if (runtime_ss && !syms_ss) {
1652 syms_ss = runtime_ss;
1653 }
1654
1655 /* We'll have to hope for the best */
1656 if (!runtime_ss && syms_ss)
1657 runtime_ss = syms_ss;
1658
1659 if (syms_ss)
1660 ret = dso__load_sym(dso, map, syms_ss, runtime_ss, kmod);
1661 else
1662 ret = -1;
1663
1664 if (ret > 0) {
1665 int nr_plt;
1666
1667 nr_plt = dso__synthesize_plt_symbols(dso, runtime_ss);
1668 if (nr_plt > 0)
1669 ret += nr_plt;
1670 }
1671
1672 for (; ss_pos > 0; ss_pos--)
1673 symsrc__destroy(&ss_[ss_pos - 1]);
1674out_free:
1675 free(name);
1676 if (ret < 0 && strstr(dso->name, " (deleted)") != NULL)
1677 ret = 0;
1678out:
1679 dso__set_loaded(dso);
1680 pthread_mutex_unlock(&dso->lock);
1681 nsinfo__mountns_exit(&nsc);
1682
1683 return ret;
1684}
1685
1686struct map *map_groups__find_by_name(struct map_groups *mg, const char *name)
1687{
1688 struct maps *maps = &mg->maps;
1689 struct map *map;
1690
1691 down_read(&maps->lock);
1692
1693 for (map = maps__first(maps); map; map = map__next(map)) {
1694 if (map->dso && strcmp(map->dso->short_name, name) == 0)
1695 goto out_unlock;
1696 }
1697
1698 map = NULL;
1699
1700out_unlock:
1701 up_read(&maps->lock);
1702 return map;
1703}
1704
1705int dso__load_vmlinux(struct dso *dso, struct map *map,
1706 const char *vmlinux, bool vmlinux_allocated)
1707{
1708 int err = -1;
1709 struct symsrc ss;
1710 char symfs_vmlinux[PATH_MAX];
1711 enum dso_binary_type symtab_type;
1712
1713 if (vmlinux[0] == '/')
1714 snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s", vmlinux);
1715 else
1716 symbol__join_symfs(symfs_vmlinux, vmlinux);
1717
1718 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1719 symtab_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
1720 else
1721 symtab_type = DSO_BINARY_TYPE__VMLINUX;
1722
1723 if (symsrc__init(&ss, dso, symfs_vmlinux, symtab_type))
1724 return -1;
1725
1726 err = dso__load_sym(dso, map, &ss, &ss, 0);
1727 symsrc__destroy(&ss);
1728
1729 if (err > 0) {
1730 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1731 dso->binary_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
1732 else
1733 dso->binary_type = DSO_BINARY_TYPE__VMLINUX;
1734 dso__set_long_name(dso, vmlinux, vmlinux_allocated);
1735 dso__set_loaded(dso);
1736 pr_debug("Using %s for symbols\n", symfs_vmlinux);
1737 }
1738
1739 return err;
1740}
1741
1742int dso__load_vmlinux_path(struct dso *dso, struct map *map)
1743{
1744 int i, err = 0;
1745 char *filename = NULL;
1746
1747 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1748 vmlinux_path__nr_entries + 1);
1749
1750 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
1751 err = dso__load_vmlinux(dso, map, vmlinux_path[i], false);
1752 if (err > 0)
1753 goto out;
1754 }
1755
1756 if (!symbol_conf.ignore_vmlinux_buildid)
1757 filename = dso__build_id_filename(dso, NULL, 0, false);
1758 if (filename != NULL) {
1759 err = dso__load_vmlinux(dso, map, filename, true);
1760 if (err > 0)
1761 goto out;
1762 free(filename);
1763 }
1764out:
1765 return err;
1766}
1767
1768static bool visible_dir_filter(const char *name, struct dirent *d)
1769{
1770 if (d->d_type != DT_DIR)
1771 return false;
1772 return lsdir_no_dot_filter(name, d);
1773}
1774
1775static int find_matching_kcore(struct map *map, char *dir, size_t dir_sz)
1776{
1777 char kallsyms_filename[PATH_MAX];
1778 int ret = -1;
1779 struct strlist *dirs;
1780 struct str_node *nd;
1781
1782 dirs = lsdir(dir, visible_dir_filter);
1783 if (!dirs)
1784 return -1;
1785
1786 strlist__for_each_entry(nd, dirs) {
1787 scnprintf(kallsyms_filename, sizeof(kallsyms_filename),
1788 "%s/%s/kallsyms", dir, nd->s);
1789 if (!validate_kcore_addresses(kallsyms_filename, map)) {
1790 strlcpy(dir, kallsyms_filename, dir_sz);
1791 ret = 0;
1792 break;
1793 }
1794 }
1795
1796 strlist__delete(dirs);
1797
1798 return ret;
1799}
1800
1801/*
1802 * Use open(O_RDONLY) to check readability directly instead of access(R_OK)
1803 * since access(R_OK) only checks with real UID/GID but open() use effective
1804 * UID/GID and actual capabilities (e.g. /proc/kcore requires CAP_SYS_RAWIO).
1805 */
1806static bool filename__readable(const char *file)
1807{
1808 int fd = open(file, O_RDONLY);
1809 if (fd < 0)
1810 return false;
1811 close(fd);
1812 return true;
1813}
1814
1815static char *dso__find_kallsyms(struct dso *dso, struct map *map)
1816{
1817 u8 host_build_id[BUILD_ID_SIZE];
1818 char sbuild_id[SBUILD_ID_SIZE];
1819 bool is_host = false;
1820 char path[PATH_MAX];
1821
1822 if (!dso->has_build_id) {
1823 /*
1824 * Last resort, if we don't have a build-id and couldn't find
1825 * any vmlinux file, try the running kernel kallsyms table.
1826 */
1827 goto proc_kallsyms;
1828 }
1829
1830 if (sysfs__read_build_id("/sys/kernel/notes", host_build_id,
1831 sizeof(host_build_id)) == 0)
1832 is_host = dso__build_id_equal(dso, host_build_id);
1833
1834 /* Try a fast path for /proc/kallsyms if possible */
1835 if (is_host) {
1836 /*
1837 * Do not check the build-id cache, unless we know we cannot use
1838 * /proc/kcore or module maps don't match to /proc/kallsyms.
1839 * To check readability of /proc/kcore, do not use access(R_OK)
1840 * since /proc/kcore requires CAP_SYS_RAWIO to read and access
1841 * can't check it.
1842 */
1843 if (filename__readable("/proc/kcore") &&
1844 !validate_kcore_addresses("/proc/kallsyms", map))
1845 goto proc_kallsyms;
1846 }
1847
1848 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1849
1850 /* Find kallsyms in build-id cache with kcore */
1851 scnprintf(path, sizeof(path), "%s/%s/%s",
1852 buildid_dir, DSO__NAME_KCORE, sbuild_id);
1853
1854 if (!find_matching_kcore(map, path, sizeof(path)))
1855 return strdup(path);
1856
1857 /* Use current /proc/kallsyms if possible */
1858 if (is_host) {
1859proc_kallsyms:
1860 return strdup("/proc/kallsyms");
1861 }
1862
1863 /* Finally, find a cache of kallsyms */
1864 if (!build_id_cache__kallsyms_path(sbuild_id, path, sizeof(path))) {
1865 pr_err("No kallsyms or vmlinux with build-id %s was found\n",
1866 sbuild_id);
1867 return NULL;
1868 }
1869
1870 return strdup(path);
1871}
1872
1873static int dso__load_kernel_sym(struct dso *dso, struct map *map)
1874{
1875 int err;
1876 const char *kallsyms_filename = NULL;
1877 char *kallsyms_allocated_filename = NULL;
1878 /*
1879 * Step 1: if the user specified a kallsyms or vmlinux filename, use
1880 * it and only it, reporting errors to the user if it cannot be used.
1881 *
1882 * For instance, try to analyse an ARM perf.data file _without_ a
1883 * build-id, or if the user specifies the wrong path to the right
1884 * vmlinux file, obviously we can't fallback to another vmlinux (a
1885 * x86_86 one, on the machine where analysis is being performed, say),
1886 * or worse, /proc/kallsyms.
1887 *
1888 * If the specified file _has_ a build-id and there is a build-id
1889 * section in the perf.data file, we will still do the expected
1890 * validation in dso__load_vmlinux and will bail out if they don't
1891 * match.
1892 */
1893 if (symbol_conf.kallsyms_name != NULL) {
1894 kallsyms_filename = symbol_conf.kallsyms_name;
1895 goto do_kallsyms;
1896 }
1897
1898 if (!symbol_conf.ignore_vmlinux && symbol_conf.vmlinux_name != NULL) {
1899 return dso__load_vmlinux(dso, map, symbol_conf.vmlinux_name, false);
1900 }
1901
1902 if (!symbol_conf.ignore_vmlinux && vmlinux_path != NULL) {
1903 err = dso__load_vmlinux_path(dso, map);
1904 if (err > 0)
1905 return err;
1906 }
1907
1908 /* do not try local files if a symfs was given */
1909 if (symbol_conf.symfs[0] != 0)
1910 return -1;
1911
1912 kallsyms_allocated_filename = dso__find_kallsyms(dso, map);
1913 if (!kallsyms_allocated_filename)
1914 return -1;
1915
1916 kallsyms_filename = kallsyms_allocated_filename;
1917
1918do_kallsyms:
1919 err = dso__load_kallsyms(dso, kallsyms_filename, map);
1920 if (err > 0)
1921 pr_debug("Using %s for symbols\n", kallsyms_filename);
1922 free(kallsyms_allocated_filename);
1923
1924 if (err > 0 && !dso__is_kcore(dso)) {
1925 dso->binary_type = DSO_BINARY_TYPE__KALLSYMS;
1926 dso__set_long_name(dso, DSO__NAME_KALLSYMS, false);
1927 map__fixup_start(map);
1928 map__fixup_end(map);
1929 }
1930
1931 return err;
1932}
1933
1934static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map)
1935{
1936 int err;
1937 const char *kallsyms_filename = NULL;
1938 struct machine *machine;
1939 char path[PATH_MAX];
1940
1941 if (!map->groups) {
1942 pr_debug("Guest kernel map hasn't the point to groups\n");
1943 return -1;
1944 }
1945 machine = map->groups->machine;
1946
1947 if (machine__is_default_guest(machine)) {
1948 /*
1949 * if the user specified a vmlinux filename, use it and only
1950 * it, reporting errors to the user if it cannot be used.
1951 * Or use file guest_kallsyms inputted by user on commandline
1952 */
1953 if (symbol_conf.default_guest_vmlinux_name != NULL) {
1954 err = dso__load_vmlinux(dso, map,
1955 symbol_conf.default_guest_vmlinux_name,
1956 false);
1957 return err;
1958 }
1959
1960 kallsyms_filename = symbol_conf.default_guest_kallsyms;
1961 if (!kallsyms_filename)
1962 return -1;
1963 } else {
1964 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
1965 kallsyms_filename = path;
1966 }
1967
1968 err = dso__load_kallsyms(dso, kallsyms_filename, map);
1969 if (err > 0)
1970 pr_debug("Using %s for symbols\n", kallsyms_filename);
1971 if (err > 0 && !dso__is_kcore(dso)) {
1972 dso->binary_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
1973 dso__set_long_name(dso, machine->mmap_name, false);
1974 map__fixup_start(map);
1975 map__fixup_end(map);
1976 }
1977
1978 return err;
1979}
1980
1981static void vmlinux_path__exit(void)
1982{
1983 while (--vmlinux_path__nr_entries >= 0)
1984 zfree(&vmlinux_path[vmlinux_path__nr_entries]);
1985 vmlinux_path__nr_entries = 0;
1986
1987 zfree(&vmlinux_path);
1988}
1989
1990static const char * const vmlinux_paths[] = {
1991 "vmlinux",
1992 "/boot/vmlinux"
1993};
1994
1995static const char * const vmlinux_paths_upd[] = {
1996 "/boot/vmlinux-%s",
1997 "/usr/lib/debug/boot/vmlinux-%s",
1998 "/lib/modules/%s/build/vmlinux",
1999 "/usr/lib/debug/lib/modules/%s/vmlinux",
2000 "/usr/lib/debug/boot/vmlinux-%s.debug"
2001};
2002
2003static int vmlinux_path__add(const char *new_entry)
2004{
2005 vmlinux_path[vmlinux_path__nr_entries] = strdup(new_entry);
2006 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2007 return -1;
2008 ++vmlinux_path__nr_entries;
2009
2010 return 0;
2011}
2012
2013static int vmlinux_path__init(struct perf_env *env)
2014{
2015 struct utsname uts;
2016 char bf[PATH_MAX];
2017 char *kernel_version;
2018 unsigned int i;
2019
2020 vmlinux_path = malloc(sizeof(char *) * (ARRAY_SIZE(vmlinux_paths) +
2021 ARRAY_SIZE(vmlinux_paths_upd)));
2022 if (vmlinux_path == NULL)
2023 return -1;
2024
2025 for (i = 0; i < ARRAY_SIZE(vmlinux_paths); i++)
2026 if (vmlinux_path__add(vmlinux_paths[i]) < 0)
2027 goto out_fail;
2028
2029 /* only try kernel version if no symfs was given */
2030 if (symbol_conf.symfs[0] != 0)
2031 return 0;
2032
2033 if (env) {
2034 kernel_version = env->os_release;
2035 } else {
2036 if (uname(&uts) < 0)
2037 goto out_fail;
2038
2039 kernel_version = uts.release;
2040 }
2041
2042 for (i = 0; i < ARRAY_SIZE(vmlinux_paths_upd); i++) {
2043 snprintf(bf, sizeof(bf), vmlinux_paths_upd[i], kernel_version);
2044 if (vmlinux_path__add(bf) < 0)
2045 goto out_fail;
2046 }
2047
2048 return 0;
2049
2050out_fail:
2051 vmlinux_path__exit();
2052 return -1;
2053}
2054
2055int setup_list(struct strlist **list, const char *list_str,
2056 const char *list_name)
2057{
2058 if (list_str == NULL)
2059 return 0;
2060
2061 *list = strlist__new(list_str, NULL);
2062 if (!*list) {
2063 pr_err("problems parsing %s list\n", list_name);
2064 return -1;
2065 }
2066
2067 symbol_conf.has_filter = true;
2068 return 0;
2069}
2070
2071int setup_intlist(struct intlist **list, const char *list_str,
2072 const char *list_name)
2073{
2074 if (list_str == NULL)
2075 return 0;
2076
2077 *list = intlist__new(list_str);
2078 if (!*list) {
2079 pr_err("problems parsing %s list\n", list_name);
2080 return -1;
2081 }
2082 return 0;
2083}
2084
2085static bool symbol__read_kptr_restrict(void)
2086{
2087 bool value = false;
2088 FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
2089
2090 if (fp != NULL) {
2091 char line[8];
2092
2093 if (fgets(line, sizeof(line), fp) != NULL)
2094 value = ((geteuid() != 0) || (getuid() != 0)) ?
2095 (atoi(line) != 0) :
2096 (atoi(line) == 2);
2097
2098 fclose(fp);
2099 }
2100
2101 return value;
2102}
2103
2104int symbol__annotation_init(void)
2105{
2106 if (symbol_conf.init_annotation)
2107 return 0;
2108
2109 if (symbol_conf.initialized) {
2110 pr_err("Annotation needs to be init before symbol__init()\n");
2111 return -1;
2112 }
2113
2114 symbol_conf.priv_size += sizeof(struct annotation);
2115 symbol_conf.init_annotation = true;
2116 return 0;
2117}
2118
2119int symbol__init(struct perf_env *env)
2120{
2121 const char *symfs;
2122
2123 if (symbol_conf.initialized)
2124 return 0;
2125
2126 symbol_conf.priv_size = PERF_ALIGN(symbol_conf.priv_size, sizeof(u64));
2127
2128 symbol__elf_init();
2129
2130 if (symbol_conf.sort_by_name)
2131 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
2132 sizeof(struct symbol));
2133
2134 if (symbol_conf.try_vmlinux_path && vmlinux_path__init(env) < 0)
2135 return -1;
2136
2137 if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
2138 pr_err("'.' is the only non valid --field-separator argument\n");
2139 return -1;
2140 }
2141
2142 if (setup_list(&symbol_conf.dso_list,
2143 symbol_conf.dso_list_str, "dso") < 0)
2144 return -1;
2145
2146 if (setup_list(&symbol_conf.comm_list,
2147 symbol_conf.comm_list_str, "comm") < 0)
2148 goto out_free_dso_list;
2149
2150 if (setup_intlist(&symbol_conf.pid_list,
2151 symbol_conf.pid_list_str, "pid") < 0)
2152 goto out_free_comm_list;
2153
2154 if (setup_intlist(&symbol_conf.tid_list,
2155 symbol_conf.tid_list_str, "tid") < 0)
2156 goto out_free_pid_list;
2157
2158 if (setup_list(&symbol_conf.sym_list,
2159 symbol_conf.sym_list_str, "symbol") < 0)
2160 goto out_free_tid_list;
2161
2162 if (setup_list(&symbol_conf.bt_stop_list,
2163 symbol_conf.bt_stop_list_str, "symbol") < 0)
2164 goto out_free_sym_list;
2165
2166 /*
2167 * A path to symbols of "/" is identical to ""
2168 * reset here for simplicity.
2169 */
2170 symfs = realpath(symbol_conf.symfs, NULL);
2171 if (symfs == NULL)
2172 symfs = symbol_conf.symfs;
2173 if (strcmp(symfs, "/") == 0)
2174 symbol_conf.symfs = "";
2175 if (symfs != symbol_conf.symfs)
2176 free((void *)symfs);
2177
2178 symbol_conf.kptr_restrict = symbol__read_kptr_restrict();
2179
2180 symbol_conf.initialized = true;
2181 return 0;
2182
2183out_free_sym_list:
2184 strlist__delete(symbol_conf.sym_list);
2185out_free_tid_list:
2186 intlist__delete(symbol_conf.tid_list);
2187out_free_pid_list:
2188 intlist__delete(symbol_conf.pid_list);
2189out_free_comm_list:
2190 strlist__delete(symbol_conf.comm_list);
2191out_free_dso_list:
2192 strlist__delete(symbol_conf.dso_list);
2193 return -1;
2194}
2195
2196void symbol__exit(void)
2197{
2198 if (!symbol_conf.initialized)
2199 return;
2200 strlist__delete(symbol_conf.bt_stop_list);
2201 strlist__delete(symbol_conf.sym_list);
2202 strlist__delete(symbol_conf.dso_list);
2203 strlist__delete(symbol_conf.comm_list);
2204 intlist__delete(symbol_conf.tid_list);
2205 intlist__delete(symbol_conf.pid_list);
2206 vmlinux_path__exit();
2207 symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
2208 symbol_conf.bt_stop_list = NULL;
2209 symbol_conf.initialized = false;
2210}
2211
2212int symbol__config_symfs(const struct option *opt __maybe_unused,
2213 const char *dir, int unset __maybe_unused)
2214{
2215 char *bf = NULL;
2216 int ret;
2217
2218 symbol_conf.symfs = strdup(dir);
2219 if (symbol_conf.symfs == NULL)
2220 return -ENOMEM;
2221
2222 /* skip the locally configured cache if a symfs is given, and
2223 * config buildid dir to symfs/.debug
2224 */
2225 ret = asprintf(&bf, "%s/%s", dir, ".debug");
2226 if (ret < 0)
2227 return -ENOMEM;
2228
2229 set_buildid_dir(bf);
2230
2231 free(bf);
2232 return 0;
2233}
2234
2235struct mem_info *mem_info__get(struct mem_info *mi)
2236{
2237 if (mi)
2238 refcount_inc(&mi->refcnt);
2239 return mi;
2240}
2241
2242void mem_info__put(struct mem_info *mi)
2243{
2244 if (mi && refcount_dec_and_test(&mi->refcnt))
2245 free(mi);
2246}
2247
2248struct mem_info *mem_info__new(void)
2249{
2250 struct mem_info *mi = zalloc(sizeof(*mi));
2251
2252 if (mi)
2253 refcount_set(&mi->refcnt, 1);
2254 return mi;
2255}