1 // 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/capability.h>
8 #include <linux/kernel.h>
9 #include <linux/mman.h>
10 #include <linux/string.h>
11 #include <linux/time64.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <sys/param.h>
15 #include <fcntl.h>
16 #include <unistd.h>
17 #include <inttypes.h>
18 #include "annotate.h"
19 #include "build-id.h"
20 #include "cap.h"
21 #include "cpumap.h"
22 #include "debug.h"
23 #include "demangle-cxx.h"
24 #include "demangle-java.h"
25 #include "demangle-ocaml.h"
26 #include "demangle-rust-v0.h"
27 #include "dso.h"
28 #include "util.h" // lsdir()
29 #include "debug.h"
30 #include "event.h"
31 #include "machine.h"
32 #include "map.h"
33 #include "symbol.h"
34 #include "map_symbol.h"
35 #include "mem-events.h"
36 #include "mem-info.h"
37 #include "symsrc.h"
38 #include "strlist.h"
39 #include "intlist.h"
40 #include "namespaces.h"
41 #include "header.h"
42 #include "path.h"
43 #include <linux/ctype.h>
44 #include <linux/log2.h>
45 #include <linux/zalloc.h>
46
47 #include <elf.h>
48 #include <limits.h>
49 #include <symbol/kallsyms.h>
50 #include <sys/utsname.h>
51
52 static int dso__load_kernel_sym(struct dso *dso, struct map *map);
53 static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map);
54 static bool symbol__is_idle(const char *name);
55
56 int vmlinux_path__nr_entries;
57 char **vmlinux_path;
58
59 struct symbol_conf symbol_conf = {
60 .nanosecs = false,
61 .use_modules = true,
62 .try_vmlinux_path = true,
63 .demangle = true,
64 .demangle_kernel = false,
65 .cumulate_callchain = true,
66 .time_quantum = 100 * NSEC_PER_MSEC, /* 100ms */
67 .show_hist_headers = true,
68 .symfs = "",
69 .event_group = true,
70 .inline_name = true,
71 .res_sample = 0,
72 };
73
74 struct map_list_node {
75 struct list_head node;
76 struct map *map;
77 };
78
map_list_node__new(void)79 static struct map_list_node *map_list_node__new(void)
80 {
81 return malloc(sizeof(struct map_list_node));
82 }
83
84 static enum dso_binary_type binary_type_symtab[] = {
85 DSO_BINARY_TYPE__KALLSYMS,
86 DSO_BINARY_TYPE__GUEST_KALLSYMS,
87 DSO_BINARY_TYPE__JAVA_JIT,
88 DSO_BINARY_TYPE__DEBUGLINK,
89 DSO_BINARY_TYPE__BUILD_ID_CACHE,
90 DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO,
91 DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
92 DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
93 DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
94 DSO_BINARY_TYPE__GNU_DEBUGDATA,
95 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
96 DSO_BINARY_TYPE__GUEST_KMODULE,
97 DSO_BINARY_TYPE__GUEST_KMODULE_COMP,
98 DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE,
99 DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP,
100 DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO,
101 DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO,
102 DSO_BINARY_TYPE__NOT_FOUND,
103 };
104
105 #define DSO_BINARY_TYPE__SYMTAB_CNT ARRAY_SIZE(binary_type_symtab)
106
symbol_type__filter(char __symbol_type)107 static bool symbol_type__filter(char __symbol_type)
108 {
109 // Since 'U' == undefined and 'u' == unique global symbol, we can't use toupper there
110 char symbol_type = toupper(__symbol_type);
111 return symbol_type == 'T' || symbol_type == 'W' || symbol_type == 'D' || symbol_type == 'B' ||
112 __symbol_type == 'u' || __symbol_type == 'l';
113 }
114
prefix_underscores_count(const char * str)115 static int prefix_underscores_count(const char *str)
116 {
117 const char *tail = str;
118
119 while (*tail == '_')
120 tail++;
121
122 return tail - str;
123 }
124
arch__normalize_symbol_name(const char * name)125 const char * __weak arch__normalize_symbol_name(const char *name)
126 {
127 return name;
128 }
129
arch__compare_symbol_names(const char * namea,const char * nameb)130 int __weak arch__compare_symbol_names(const char *namea, const char *nameb)
131 {
132 return strcmp(namea, nameb);
133 }
134
arch__compare_symbol_names_n(const char * namea,const char * nameb,unsigned int n)135 int __weak arch__compare_symbol_names_n(const char *namea, const char *nameb,
136 unsigned int n)
137 {
138 return strncmp(namea, nameb, n);
139 }
140
arch__choose_best_symbol(struct symbol * syma,struct symbol * symb __maybe_unused)141 int __weak arch__choose_best_symbol(struct symbol *syma,
142 struct symbol *symb __maybe_unused)
143 {
144 /* Avoid "SyS" kernel syscall aliases */
145 if (strlen(syma->name) >= 3 && !strncmp(syma->name, "SyS", 3))
146 return SYMBOL_B;
147 if (strlen(syma->name) >= 10 && !strncmp(syma->name, "compat_SyS", 10))
148 return SYMBOL_B;
149
150 return SYMBOL_A;
151 }
152
choose_best_symbol(struct symbol * syma,struct symbol * symb)153 static int choose_best_symbol(struct symbol *syma, struct symbol *symb)
154 {
155 s64 a;
156 s64 b;
157 size_t na, nb;
158
159 /* Prefer a symbol with non zero length */
160 a = syma->end - syma->start;
161 b = symb->end - symb->start;
162 if ((b == 0) && (a > 0))
163 return SYMBOL_A;
164 else if ((a == 0) && (b > 0))
165 return SYMBOL_B;
166
167 if (syma->type != symb->type) {
168 if (syma->type == STT_NOTYPE)
169 return SYMBOL_B;
170 if (symb->type == STT_NOTYPE)
171 return SYMBOL_A;
172 }
173
174 /* Prefer a non weak symbol over a weak one */
175 a = syma->binding == STB_WEAK;
176 b = symb->binding == STB_WEAK;
177 if (b && !a)
178 return SYMBOL_A;
179 if (a && !b)
180 return SYMBOL_B;
181
182 /* Prefer a global symbol over a non global one */
183 a = syma->binding == STB_GLOBAL;
184 b = symb->binding == STB_GLOBAL;
185 if (a && !b)
186 return SYMBOL_A;
187 if (b && !a)
188 return SYMBOL_B;
189
190 /* Prefer a symbol with less underscores */
191 a = prefix_underscores_count(syma->name);
192 b = prefix_underscores_count(symb->name);
193 if (b > a)
194 return SYMBOL_A;
195 else if (a > b)
196 return SYMBOL_B;
197
198 /* Choose the symbol with the longest name */
199 na = strlen(syma->name);
200 nb = strlen(symb->name);
201 if (na > nb)
202 return SYMBOL_A;
203 else if (na < nb)
204 return SYMBOL_B;
205
206 return arch__choose_best_symbol(syma, symb);
207 }
208
symbols__fixup_duplicate(struct rb_root_cached * symbols)209 void symbols__fixup_duplicate(struct rb_root_cached *symbols)
210 {
211 struct rb_node *nd;
212 struct symbol *curr, *next;
213
214 if (symbol_conf.allow_aliases)
215 return;
216
217 nd = rb_first_cached(symbols);
218
219 while (nd) {
220 curr = rb_entry(nd, struct symbol, rb_node);
221 again:
222 nd = rb_next(&curr->rb_node);
223 if (!nd)
224 break;
225
226 next = rb_entry(nd, struct symbol, rb_node);
227 if (curr->start != next->start)
228 continue;
229
230 if (choose_best_symbol(curr, next) == SYMBOL_A) {
231 if (next->type == STT_GNU_IFUNC)
232 curr->ifunc_alias = true;
233 rb_erase_cached(&next->rb_node, symbols);
234 symbol__delete(next);
235 goto again;
236 } else {
237 if (curr->type == STT_GNU_IFUNC)
238 next->ifunc_alias = true;
239 nd = rb_next(&curr->rb_node);
240 rb_erase_cached(&curr->rb_node, symbols);
241 symbol__delete(curr);
242 }
243 }
244 }
245
246 /* Update zero-sized symbols using the address of the next symbol */
symbols__fixup_end(struct rb_root_cached * symbols,bool is_kallsyms)247 void symbols__fixup_end(struct rb_root_cached *symbols, bool is_kallsyms)
248 {
249 struct rb_node *nd, *prevnd = rb_first_cached(symbols);
250 struct symbol *curr, *prev;
251
252 if (prevnd == NULL)
253 return;
254
255 curr = rb_entry(prevnd, struct symbol, rb_node);
256
257 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
258 prev = curr;
259 curr = rb_entry(nd, struct symbol, rb_node);
260
261 /*
262 * On some architecture kernel text segment start is located at
263 * some low memory address, while modules are located at high
264 * memory addresses (or vice versa). The gap between end of
265 * kernel text segment and beginning of first module's text
266 * segment is very big. Therefore do not fill this gap and do
267 * not assign it to the kernel dso map (kallsyms).
268 *
269 * Also BPF code can be allocated separately from text segments
270 * and modules. So the last entry in a module should not fill
271 * the gap too.
272 *
273 * In kallsyms, it determines module symbols using '[' character
274 * like in:
275 * ffffffffc1937000 T hdmi_driver_init [snd_hda_codec_hdmi]
276 */
277 if (prev->end == prev->start) {
278 const char *prev_mod;
279 const char *curr_mod;
280
281 if (!is_kallsyms) {
282 prev->end = curr->start;
283 continue;
284 }
285
286 prev_mod = strchr(prev->name, '[');
287 curr_mod = strchr(curr->name, '[');
288
289 /* Last kernel/module symbol mapped to end of page */
290 if (!prev_mod != !curr_mod)
291 prev->end = roundup(prev->end + 4096, 4096);
292 /* Last symbol in the previous module */
293 else if (prev_mod && strcmp(prev_mod, curr_mod))
294 prev->end = roundup(prev->end + 4096, 4096);
295 else
296 prev->end = curr->start;
297
298 pr_debug4("%s sym:%s end:%#" PRIx64 "\n",
299 __func__, prev->name, prev->end);
300 }
301 }
302
303 /* Last entry */
304 if (curr->end == curr->start)
305 curr->end = roundup(curr->start, 4096) + 4096;
306 }
307
symbol__new(u64 start,u64 len,u8 binding,u8 type,const char * name)308 struct symbol *symbol__new(u64 start, u64 len, u8 binding, u8 type, const char *name)
309 {
310 size_t namelen = strlen(name) + 1;
311 struct symbol *sym = calloc(1, (symbol_conf.priv_size +
312 sizeof(*sym) + namelen));
313 if (sym == NULL)
314 return NULL;
315
316 if (symbol_conf.priv_size) {
317 if (symbol_conf.init_annotation) {
318 struct annotation *notes = (void *)sym;
319 annotation__init(notes);
320 }
321 sym = ((void *)sym) + symbol_conf.priv_size;
322 }
323
324 sym->start = start;
325 sym->end = len ? start + len : start;
326 sym->type = type;
327 sym->binding = binding;
328 sym->namelen = namelen - 1;
329
330 pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n",
331 __func__, name, start, sym->end);
332 memcpy(sym->name, name, namelen);
333
334 return sym;
335 }
336
symbol__delete(struct symbol * sym)337 void symbol__delete(struct symbol *sym)
338 {
339 if (symbol_conf.priv_size) {
340 if (symbol_conf.init_annotation) {
341 struct annotation *notes = symbol__annotation(sym);
342
343 annotation__exit(notes);
344 }
345 }
346 free(((void *)sym) - symbol_conf.priv_size);
347 }
348
symbols__delete(struct rb_root_cached * symbols)349 void symbols__delete(struct rb_root_cached *symbols)
350 {
351 struct symbol *pos;
352 struct rb_node *next = rb_first_cached(symbols);
353
354 while (next) {
355 pos = rb_entry(next, struct symbol, rb_node);
356 next = rb_next(&pos->rb_node);
357 rb_erase_cached(&pos->rb_node, symbols);
358 symbol__delete(pos);
359 }
360 }
361
__symbols__insert(struct rb_root_cached * symbols,struct symbol * sym,bool kernel)362 void __symbols__insert(struct rb_root_cached *symbols,
363 struct symbol *sym, bool kernel)
364 {
365 struct rb_node **p = &symbols->rb_root.rb_node;
366 struct rb_node *parent = NULL;
367 const u64 ip = sym->start;
368 struct symbol *s;
369 bool leftmost = true;
370
371 if (kernel) {
372 const char *name = sym->name;
373 /*
374 * ppc64 uses function descriptors and appends a '.' to the
375 * start of every instruction address. Remove it.
376 */
377 if (name[0] == '.')
378 name++;
379 sym->idle = symbol__is_idle(name);
380 }
381
382 while (*p != NULL) {
383 parent = *p;
384 s = rb_entry(parent, struct symbol, rb_node);
385 if (ip < s->start)
386 p = &(*p)->rb_left;
387 else {
388 p = &(*p)->rb_right;
389 leftmost = false;
390 }
391 }
392 rb_link_node(&sym->rb_node, parent, p);
393 rb_insert_color_cached(&sym->rb_node, symbols, leftmost);
394 }
395
symbols__insert(struct rb_root_cached * symbols,struct symbol * sym)396 void symbols__insert(struct rb_root_cached *symbols, struct symbol *sym)
397 {
398 __symbols__insert(symbols, sym, false);
399 }
400
symbols__find(struct rb_root_cached * symbols,u64 ip)401 static struct symbol *symbols__find(struct rb_root_cached *symbols, u64 ip)
402 {
403 struct rb_node *n;
404
405 if (symbols == NULL)
406 return NULL;
407
408 n = symbols->rb_root.rb_node;
409
410 while (n) {
411 struct symbol *s = rb_entry(n, struct symbol, rb_node);
412
413 if (ip < s->start)
414 n = n->rb_left;
415 else if (ip > s->end || (ip == s->end && ip != s->start))
416 n = n->rb_right;
417 else
418 return s;
419 }
420
421 return NULL;
422 }
423
symbols__first(struct rb_root_cached * symbols)424 static struct symbol *symbols__first(struct rb_root_cached *symbols)
425 {
426 struct rb_node *n = rb_first_cached(symbols);
427
428 if (n)
429 return rb_entry(n, struct symbol, rb_node);
430
431 return NULL;
432 }
433
symbols__last(struct rb_root_cached * symbols)434 static struct symbol *symbols__last(struct rb_root_cached *symbols)
435 {
436 struct rb_node *n = rb_last(&symbols->rb_root);
437
438 if (n)
439 return rb_entry(n, struct symbol, rb_node);
440
441 return NULL;
442 }
443
symbols__next(struct symbol * sym)444 static struct symbol *symbols__next(struct symbol *sym)
445 {
446 struct rb_node *n = rb_next(&sym->rb_node);
447
448 if (n)
449 return rb_entry(n, struct symbol, rb_node);
450
451 return NULL;
452 }
453
symbols__sort_name_cmp(const void * vlhs,const void * vrhs)454 static int symbols__sort_name_cmp(const void *vlhs, const void *vrhs)
455 {
456 const struct symbol *lhs = *((const struct symbol **)vlhs);
457 const struct symbol *rhs = *((const struct symbol **)vrhs);
458
459 return strcmp(lhs->name, rhs->name);
460 }
461
symbols__sort_by_name(struct rb_root_cached * source,size_t * len)462 static struct symbol **symbols__sort_by_name(struct rb_root_cached *source, size_t *len)
463 {
464 struct rb_node *nd;
465 struct symbol **result;
466 size_t i = 0, size = 0;
467
468 for (nd = rb_first_cached(source); nd; nd = rb_next(nd))
469 size++;
470
471 result = malloc(sizeof(*result) * size);
472 if (!result)
473 return NULL;
474
475 for (nd = rb_first_cached(source); nd; nd = rb_next(nd)) {
476 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
477
478 result[i++] = pos;
479 }
480 qsort(result, size, sizeof(*result), symbols__sort_name_cmp);
481 *len = size;
482 return result;
483 }
484
symbol__match_symbol_name(const char * name,const char * str,enum symbol_tag_include includes)485 int symbol__match_symbol_name(const char *name, const char *str,
486 enum symbol_tag_include includes)
487 {
488 const char *versioning;
489
490 if (includes == SYMBOL_TAG_INCLUDE__DEFAULT_ONLY &&
491 (versioning = strstr(name, "@@"))) {
492 int len = strlen(str);
493
494 if (len < versioning - name)
495 len = versioning - name;
496
497 return arch__compare_symbol_names_n(name, str, len);
498 } else
499 return arch__compare_symbol_names(name, str);
500 }
501
symbols__find_by_name(struct symbol * symbols[],size_t symbols_len,const char * name,enum symbol_tag_include includes,size_t * found_idx)502 static struct symbol *symbols__find_by_name(struct symbol *symbols[],
503 size_t symbols_len,
504 const char *name,
505 enum symbol_tag_include includes,
506 size_t *found_idx)
507 {
508 size_t i, lower = 0, upper = symbols_len;
509 struct symbol *s = NULL;
510
511 if (found_idx)
512 *found_idx = SIZE_MAX;
513
514 if (!symbols_len)
515 return NULL;
516
517 while (lower < upper) {
518 int cmp;
519
520 i = (lower + upper) / 2;
521 cmp = symbol__match_symbol_name(symbols[i]->name, name, includes);
522
523 if (cmp > 0)
524 upper = i;
525 else if (cmp < 0)
526 lower = i + 1;
527 else {
528 if (found_idx)
529 *found_idx = i;
530 s = symbols[i];
531 break;
532 }
533 }
534 if (s && includes != SYMBOL_TAG_INCLUDE__DEFAULT_ONLY) {
535 /* return first symbol that has same name (if any) */
536 for (; i > 0; i--) {
537 struct symbol *tmp = symbols[i - 1];
538
539 if (!arch__compare_symbol_names(tmp->name, s->name)) {
540 if (found_idx)
541 *found_idx = i - 1;
542 s = tmp;
543 } else
544 break;
545 }
546 }
547 assert(!found_idx || !s || s == symbols[*found_idx]);
548 return s;
549 }
550
dso__reset_find_symbol_cache(struct dso * dso)551 void dso__reset_find_symbol_cache(struct dso *dso)
552 {
553 dso__set_last_find_result_addr(dso, 0);
554 dso__set_last_find_result_symbol(dso, NULL);
555 }
556
dso__insert_symbol(struct dso * dso,struct symbol * sym)557 void dso__insert_symbol(struct dso *dso, struct symbol *sym)
558 {
559 __symbols__insert(dso__symbols(dso), sym, dso__kernel(dso));
560
561 /* update the symbol cache if necessary */
562 if (dso__last_find_result_addr(dso) >= sym->start &&
563 (dso__last_find_result_addr(dso) < sym->end ||
564 sym->start == sym->end)) {
565 dso__set_last_find_result_symbol(dso, sym);
566 }
567 }
568
dso__delete_symbol(struct dso * dso,struct symbol * sym)569 void dso__delete_symbol(struct dso *dso, struct symbol *sym)
570 {
571 rb_erase_cached(&sym->rb_node, dso__symbols(dso));
572 symbol__delete(sym);
573 dso__reset_find_symbol_cache(dso);
574 }
575
dso__find_symbol(struct dso * dso,u64 addr)576 struct symbol *dso__find_symbol(struct dso *dso, u64 addr)
577 {
578 if (dso__last_find_result_addr(dso) != addr || dso__last_find_result_symbol(dso) == NULL) {
579 dso__set_last_find_result_addr(dso, addr);
580 dso__set_last_find_result_symbol(dso, symbols__find(dso__symbols(dso), addr));
581 }
582
583 return dso__last_find_result_symbol(dso);
584 }
585
dso__find_symbol_nocache(struct dso * dso,u64 addr)586 struct symbol *dso__find_symbol_nocache(struct dso *dso, u64 addr)
587 {
588 return symbols__find(dso__symbols(dso), addr);
589 }
590
dso__first_symbol(struct dso * dso)591 struct symbol *dso__first_symbol(struct dso *dso)
592 {
593 return symbols__first(dso__symbols(dso));
594 }
595
dso__last_symbol(struct dso * dso)596 struct symbol *dso__last_symbol(struct dso *dso)
597 {
598 return symbols__last(dso__symbols(dso));
599 }
600
dso__next_symbol(struct symbol * sym)601 struct symbol *dso__next_symbol(struct symbol *sym)
602 {
603 return symbols__next(sym);
604 }
605
dso__next_symbol_by_name(struct dso * dso,size_t * idx)606 struct symbol *dso__next_symbol_by_name(struct dso *dso, size_t *idx)
607 {
608 if (*idx + 1 >= dso__symbol_names_len(dso))
609 return NULL;
610
611 ++*idx;
612 return dso__symbol_names(dso)[*idx];
613 }
614
615 /*
616 * Returns first symbol that matched with @name.
617 */
dso__find_symbol_by_name(struct dso * dso,const char * name,size_t * idx)618 struct symbol *dso__find_symbol_by_name(struct dso *dso, const char *name, size_t *idx)
619 {
620 struct symbol *s = symbols__find_by_name(dso__symbol_names(dso),
621 dso__symbol_names_len(dso),
622 name, SYMBOL_TAG_INCLUDE__NONE, idx);
623 if (!s) {
624 s = symbols__find_by_name(dso__symbol_names(dso), dso__symbol_names_len(dso),
625 name, SYMBOL_TAG_INCLUDE__DEFAULT_ONLY, idx);
626 }
627 return s;
628 }
629
dso__sort_by_name(struct dso * dso)630 void dso__sort_by_name(struct dso *dso)
631 {
632 mutex_lock(dso__lock(dso));
633 if (!dso__sorted_by_name(dso)) {
634 size_t len = 0;
635
636 dso__set_symbol_names(dso, symbols__sort_by_name(dso__symbols(dso), &len));
637 if (dso__symbol_names(dso)) {
638 dso__set_symbol_names_len(dso, len);
639 dso__set_sorted_by_name(dso);
640 }
641 }
642 mutex_unlock(dso__lock(dso));
643 }
644
645 /*
646 * While we find nice hex chars, build a long_val.
647 * Return number of chars processed.
648 */
hex2u64(const char * ptr,u64 * long_val)649 static int hex2u64(const char *ptr, u64 *long_val)
650 {
651 char *p;
652
653 *long_val = strtoull(ptr, &p, 16);
654
655 return p - ptr;
656 }
657
658
modules__parse(const char * filename,void * arg,int (* process_module)(void * arg,const char * name,u64 start,u64 size))659 int modules__parse(const char *filename, void *arg,
660 int (*process_module)(void *arg, const char *name,
661 u64 start, u64 size))
662 {
663 char *line = NULL;
664 size_t n;
665 FILE *file;
666 int err = 0;
667
668 file = fopen(filename, "r");
669 if (file == NULL)
670 return -1;
671
672 while (1) {
673 char name[PATH_MAX];
674 u64 start, size;
675 char *sep, *endptr;
676 ssize_t line_len;
677
678 line_len = getline(&line, &n, file);
679 if (line_len < 0) {
680 if (feof(file))
681 break;
682 err = -1;
683 goto out;
684 }
685
686 if (!line) {
687 err = -1;
688 goto out;
689 }
690
691 line[--line_len] = '\0'; /* \n */
692
693 sep = strrchr(line, 'x');
694 if (sep == NULL)
695 continue;
696
697 hex2u64(sep + 1, &start);
698
699 sep = strchr(line, ' ');
700 if (sep == NULL)
701 continue;
702
703 *sep = '\0';
704
705 scnprintf(name, sizeof(name), "[%s]", line);
706
707 size = strtoul(sep + 1, &endptr, 0);
708 if (*endptr != ' ' && *endptr != '\t')
709 continue;
710
711 err = process_module(arg, name, start, size);
712 if (err)
713 break;
714 }
715 out:
716 free(line);
717 fclose(file);
718 return err;
719 }
720
721 /*
722 * These are symbols in the kernel image, so make sure that
723 * sym is from a kernel DSO.
724 */
symbol__is_idle(const char * name)725 static bool symbol__is_idle(const char *name)
726 {
727 const char * const idle_symbols[] = {
728 "acpi_idle_do_entry",
729 "acpi_processor_ffh_cstate_enter",
730 "arch_cpu_idle",
731 "cpu_idle",
732 "cpu_startup_entry",
733 "idle_cpu",
734 "intel_idle",
735 "intel_idle_ibrs",
736 "default_idle",
737 "native_safe_halt",
738 "enter_idle",
739 "exit_idle",
740 "mwait_idle",
741 "mwait_idle_with_hints",
742 "mwait_idle_with_hints.constprop.0",
743 "poll_idle",
744 "ppc64_runlatch_off",
745 "pseries_dedicated_idle_sleep",
746 "psw_idle",
747 "psw_idle_exit",
748 NULL
749 };
750 int i;
751 static struct strlist *idle_symbols_list;
752
753 if (idle_symbols_list)
754 return strlist__has_entry(idle_symbols_list, name);
755
756 idle_symbols_list = strlist__new(NULL, NULL);
757
758 for (i = 0; idle_symbols[i]; i++)
759 strlist__add(idle_symbols_list, idle_symbols[i]);
760
761 return strlist__has_entry(idle_symbols_list, name);
762 }
763
map__process_kallsym_symbol(void * arg,const char * name,char type,u64 start)764 static int map__process_kallsym_symbol(void *arg, const char *name,
765 char type, u64 start)
766 {
767 struct symbol *sym;
768 struct dso *dso = arg;
769 struct rb_root_cached *root = dso__symbols(dso);
770
771 if (!symbol_type__filter(type))
772 return 0;
773
774 /* Ignore local symbols for ARM modules */
775 if (name[0] == '$')
776 return 0;
777
778 /*
779 * module symbols are not sorted so we add all
780 * symbols, setting length to 0, and rely on
781 * symbols__fixup_end() to fix it up.
782 */
783 sym = symbol__new(start, 0, kallsyms2elf_binding(type), kallsyms2elf_type(type), name);
784 if (sym == NULL)
785 return -ENOMEM;
786 /*
787 * We will pass the symbols to the filter later, in
788 * map__split_kallsyms, when we have split the maps per module
789 */
790 __symbols__insert(root, sym, !strchr(name, '['));
791
792 return 0;
793 }
794
795 /*
796 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
797 * so that we can in the next step set the symbol ->end address and then
798 * call kernel_maps__split_kallsyms.
799 */
dso__load_all_kallsyms(struct dso * dso,const char * filename)800 static int dso__load_all_kallsyms(struct dso *dso, const char *filename)
801 {
802 return kallsyms__parse(filename, dso, map__process_kallsym_symbol);
803 }
804
maps__split_kallsyms_for_kcore(struct maps * kmaps,struct dso * dso)805 static int maps__split_kallsyms_for_kcore(struct maps *kmaps, struct dso *dso)
806 {
807 struct symbol *pos;
808 int count = 0;
809 struct rb_root_cached *root = dso__symbols(dso);
810 struct rb_root_cached old_root = *root;
811 struct rb_node *next = rb_first_cached(root);
812
813 if (!kmaps)
814 return -1;
815
816 *root = RB_ROOT_CACHED;
817
818 while (next) {
819 struct map *curr_map;
820 struct dso *curr_map_dso;
821 char *module;
822
823 pos = rb_entry(next, struct symbol, rb_node);
824 next = rb_next(&pos->rb_node);
825
826 rb_erase_cached(&pos->rb_node, &old_root);
827 RB_CLEAR_NODE(&pos->rb_node);
828 module = strchr(pos->name, '\t');
829 if (module)
830 *module = '\0';
831
832 curr_map = maps__find(kmaps, pos->start);
833
834 if (!curr_map) {
835 symbol__delete(pos);
836 continue;
837 }
838 curr_map_dso = map__dso(curr_map);
839 pos->start -= map__start(curr_map) - map__pgoff(curr_map);
840 if (pos->end > map__end(curr_map))
841 pos->end = map__end(curr_map);
842 if (pos->end)
843 pos->end -= map__start(curr_map) - map__pgoff(curr_map);
844 symbols__insert(dso__symbols(curr_map_dso), pos);
845 ++count;
846 map__put(curr_map);
847 }
848
849 /* Symbols have been adjusted */
850 dso__set_adjust_symbols(dso, true);
851
852 return count;
853 }
854
855 /*
856 * Split the symbols into maps, making sure there are no overlaps, i.e. the
857 * kernel range is broken in several maps, named [kernel].N, as we don't have
858 * the original ELF section names vmlinux have.
859 */
maps__split_kallsyms(struct maps * kmaps,struct dso * dso,u64 delta,struct map * initial_map)860 static int maps__split_kallsyms(struct maps *kmaps, struct dso *dso, u64 delta,
861 struct map *initial_map)
862 {
863 struct machine *machine;
864 struct map *curr_map = map__get(initial_map);
865 struct symbol *pos;
866 int count = 0, moved = 0;
867 struct rb_root_cached *root = dso__symbols(dso);
868 struct rb_node *next = rb_first_cached(root);
869 int kernel_range = 0;
870 bool x86_64;
871
872 if (!kmaps)
873 return -1;
874
875 machine = maps__machine(kmaps);
876
877 x86_64 = machine__is(machine, "x86_64");
878
879 while (next) {
880 char *module;
881
882 pos = rb_entry(next, struct symbol, rb_node);
883 next = rb_next(&pos->rb_node);
884
885 module = strchr(pos->name, '\t');
886 if (module) {
887 struct dso *curr_map_dso;
888
889 if (!symbol_conf.use_modules)
890 goto discard_symbol;
891
892 *module++ = '\0';
893 curr_map_dso = map__dso(curr_map);
894 if (strcmp(dso__short_name(curr_map_dso), module)) {
895 if (!RC_CHK_EQUAL(curr_map, initial_map) &&
896 dso__kernel(dso) == DSO_SPACE__KERNEL_GUEST &&
897 machine__is_default_guest(machine)) {
898 /*
899 * We assume all symbols of a module are
900 * continuous in * kallsyms, so curr_map
901 * points to a module and all its
902 * symbols are in its kmap. Mark it as
903 * loaded.
904 */
905 dso__set_loaded(curr_map_dso);
906 }
907
908 map__zput(curr_map);
909 curr_map = maps__find_by_name(kmaps, module);
910 if (curr_map == NULL) {
911 pr_debug("%s/proc/{kallsyms,modules} "
912 "inconsistency while looking "
913 "for \"%s\" module!\n",
914 machine->root_dir, module);
915 curr_map = map__get(initial_map);
916 goto discard_symbol;
917 }
918 curr_map_dso = map__dso(curr_map);
919 if (dso__loaded(curr_map_dso) &&
920 !machine__is_default_guest(machine))
921 goto discard_symbol;
922 }
923 /*
924 * So that we look just like we get from .ko files,
925 * i.e. not prelinked, relative to initial_map->start.
926 */
927 pos->start = map__map_ip(curr_map, pos->start);
928 pos->end = map__map_ip(curr_map, pos->end);
929 } else if (x86_64 && is_entry_trampoline(pos->name)) {
930 /*
931 * These symbols are not needed anymore since the
932 * trampoline maps refer to the text section and it's
933 * symbols instead. Avoid having to deal with
934 * relocations, and the assumption that the first symbol
935 * is the start of kernel text, by simply removing the
936 * symbols at this point.
937 */
938 goto discard_symbol;
939 } else if (!RC_CHK_EQUAL(curr_map, initial_map)) {
940 char dso_name[PATH_MAX];
941 struct dso *ndso;
942
943 if (delta) {
944 /* Kernel was relocated at boot time */
945 pos->start -= delta;
946 pos->end -= delta;
947 }
948
949 if (count == 0) {
950 map__zput(curr_map);
951 curr_map = map__get(initial_map);
952 goto add_symbol;
953 }
954
955 if (dso__kernel(dso) == DSO_SPACE__KERNEL_GUEST)
956 snprintf(dso_name, sizeof(dso_name),
957 "[guest.kernel].%d",
958 kernel_range++);
959 else
960 snprintf(dso_name, sizeof(dso_name),
961 "[kernel].%d",
962 kernel_range++);
963
964 ndso = dso__new(dso_name);
965 map__zput(curr_map);
966 if (ndso == NULL)
967 return -1;
968
969 dso__set_kernel(ndso, dso__kernel(dso));
970
971 curr_map = map__new2(pos->start, ndso);
972 if (curr_map == NULL) {
973 dso__put(ndso);
974 return -1;
975 }
976
977 map__set_mapping_type(curr_map, MAPPING_TYPE__IDENTITY);
978 if (maps__insert(kmaps, curr_map)) {
979 map__zput(curr_map);
980 dso__put(ndso);
981 return -1;
982 }
983 ++kernel_range;
984 } else if (delta) {
985 /* Kernel was relocated at boot time */
986 pos->start -= delta;
987 pos->end -= delta;
988 }
989 add_symbol:
990 if (!RC_CHK_EQUAL(curr_map, initial_map)) {
991 struct dso *curr_map_dso = map__dso(curr_map);
992
993 rb_erase_cached(&pos->rb_node, root);
994 symbols__insert(dso__symbols(curr_map_dso), pos);
995 ++moved;
996 } else
997 ++count;
998
999 continue;
1000 discard_symbol:
1001 rb_erase_cached(&pos->rb_node, root);
1002 symbol__delete(pos);
1003 }
1004
1005 if (!RC_CHK_EQUAL(curr_map, initial_map) &&
1006 dso__kernel(dso) == DSO_SPACE__KERNEL_GUEST &&
1007 machine__is_default_guest(maps__machine(kmaps))) {
1008 dso__set_loaded(map__dso(curr_map));
1009 }
1010 map__put(curr_map);
1011 return count + moved;
1012 }
1013
symbol__restricted_filename(const char * filename,const char * restricted_filename)1014 bool symbol__restricted_filename(const char *filename,
1015 const char *restricted_filename)
1016 {
1017 bool restricted = false;
1018
1019 if (symbol_conf.kptr_restrict) {
1020 char *r = realpath(filename, NULL);
1021
1022 if (r != NULL) {
1023 restricted = strcmp(r, restricted_filename) == 0;
1024 free(r);
1025 return restricted;
1026 }
1027 }
1028
1029 return restricted;
1030 }
1031
1032 struct module_info {
1033 struct rb_node rb_node;
1034 char *name;
1035 u64 start;
1036 };
1037
add_module(struct module_info * mi,struct rb_root * modules)1038 static void add_module(struct module_info *mi, struct rb_root *modules)
1039 {
1040 struct rb_node **p = &modules->rb_node;
1041 struct rb_node *parent = NULL;
1042 struct module_info *m;
1043
1044 while (*p != NULL) {
1045 parent = *p;
1046 m = rb_entry(parent, struct module_info, rb_node);
1047 if (strcmp(mi->name, m->name) < 0)
1048 p = &(*p)->rb_left;
1049 else
1050 p = &(*p)->rb_right;
1051 }
1052 rb_link_node(&mi->rb_node, parent, p);
1053 rb_insert_color(&mi->rb_node, modules);
1054 }
1055
delete_modules(struct rb_root * modules)1056 static void delete_modules(struct rb_root *modules)
1057 {
1058 struct module_info *mi;
1059 struct rb_node *next = rb_first(modules);
1060
1061 while (next) {
1062 mi = rb_entry(next, struct module_info, rb_node);
1063 next = rb_next(&mi->rb_node);
1064 rb_erase(&mi->rb_node, modules);
1065 zfree(&mi->name);
1066 free(mi);
1067 }
1068 }
1069
find_module(const char * name,struct rb_root * modules)1070 static struct module_info *find_module(const char *name,
1071 struct rb_root *modules)
1072 {
1073 struct rb_node *n = modules->rb_node;
1074
1075 while (n) {
1076 struct module_info *m;
1077 int cmp;
1078
1079 m = rb_entry(n, struct module_info, rb_node);
1080 cmp = strcmp(name, m->name);
1081 if (cmp < 0)
1082 n = n->rb_left;
1083 else if (cmp > 0)
1084 n = n->rb_right;
1085 else
1086 return m;
1087 }
1088
1089 return NULL;
1090 }
1091
__read_proc_modules(void * arg,const char * name,u64 start,u64 size __maybe_unused)1092 static int __read_proc_modules(void *arg, const char *name, u64 start,
1093 u64 size __maybe_unused)
1094 {
1095 struct rb_root *modules = arg;
1096 struct module_info *mi;
1097
1098 mi = zalloc(sizeof(struct module_info));
1099 if (!mi)
1100 return -ENOMEM;
1101
1102 mi->name = strdup(name);
1103 mi->start = start;
1104
1105 if (!mi->name) {
1106 free(mi);
1107 return -ENOMEM;
1108 }
1109
1110 add_module(mi, modules);
1111
1112 return 0;
1113 }
1114
read_proc_modules(const char * filename,struct rb_root * modules)1115 static int read_proc_modules(const char *filename, struct rb_root *modules)
1116 {
1117 if (symbol__restricted_filename(filename, "/proc/modules"))
1118 return -1;
1119
1120 if (modules__parse(filename, modules, __read_proc_modules)) {
1121 delete_modules(modules);
1122 return -1;
1123 }
1124
1125 return 0;
1126 }
1127
compare_proc_modules(const char * from,const char * to)1128 int compare_proc_modules(const char *from, const char *to)
1129 {
1130 struct rb_root from_modules = RB_ROOT;
1131 struct rb_root to_modules = RB_ROOT;
1132 struct rb_node *from_node, *to_node;
1133 struct module_info *from_m, *to_m;
1134 int ret = -1;
1135
1136 if (read_proc_modules(from, &from_modules))
1137 return -1;
1138
1139 if (read_proc_modules(to, &to_modules))
1140 goto out_delete_from;
1141
1142 from_node = rb_first(&from_modules);
1143 to_node = rb_first(&to_modules);
1144 while (from_node) {
1145 if (!to_node)
1146 break;
1147
1148 from_m = rb_entry(from_node, struct module_info, rb_node);
1149 to_m = rb_entry(to_node, struct module_info, rb_node);
1150
1151 if (from_m->start != to_m->start ||
1152 strcmp(from_m->name, to_m->name))
1153 break;
1154
1155 from_node = rb_next(from_node);
1156 to_node = rb_next(to_node);
1157 }
1158
1159 if (!from_node && !to_node)
1160 ret = 0;
1161
1162 delete_modules(&to_modules);
1163 out_delete_from:
1164 delete_modules(&from_modules);
1165
1166 return ret;
1167 }
1168
do_validate_kcore_modules_cb(struct map * old_map,void * data)1169 static int do_validate_kcore_modules_cb(struct map *old_map, void *data)
1170 {
1171 struct rb_root *modules = data;
1172 struct module_info *mi;
1173 struct dso *dso;
1174
1175 if (!__map__is_kmodule(old_map))
1176 return 0;
1177
1178 dso = map__dso(old_map);
1179 /* Module must be in memory at the same address */
1180 mi = find_module(dso__short_name(dso), modules);
1181 if (!mi || mi->start != map__start(old_map))
1182 return -EINVAL;
1183
1184 return 0;
1185 }
1186
do_validate_kcore_modules(const char * filename,struct maps * kmaps)1187 static int do_validate_kcore_modules(const char *filename, struct maps *kmaps)
1188 {
1189 struct rb_root modules = RB_ROOT;
1190 int err;
1191
1192 err = read_proc_modules(filename, &modules);
1193 if (err)
1194 return err;
1195
1196 err = maps__for_each_map(kmaps, do_validate_kcore_modules_cb, &modules);
1197
1198 delete_modules(&modules);
1199 return err;
1200 }
1201
1202 /*
1203 * If kallsyms is referenced by name then we look for filename in the same
1204 * directory.
1205 */
filename_from_kallsyms_filename(char * filename,const char * base_name,const char * kallsyms_filename)1206 static bool filename_from_kallsyms_filename(char *filename,
1207 const char *base_name,
1208 const char *kallsyms_filename)
1209 {
1210 char *name;
1211
1212 strcpy(filename, kallsyms_filename);
1213 name = strrchr(filename, '/');
1214 if (!name)
1215 return false;
1216
1217 name += 1;
1218
1219 if (!strcmp(name, "kallsyms")) {
1220 strcpy(name, base_name);
1221 return true;
1222 }
1223
1224 return false;
1225 }
1226
validate_kcore_modules(const char * kallsyms_filename,struct map * map)1227 static int validate_kcore_modules(const char *kallsyms_filename,
1228 struct map *map)
1229 {
1230 struct maps *kmaps = map__kmaps(map);
1231 char modules_filename[PATH_MAX];
1232
1233 if (!kmaps)
1234 return -EINVAL;
1235
1236 if (!filename_from_kallsyms_filename(modules_filename, "modules",
1237 kallsyms_filename))
1238 return -EINVAL;
1239
1240 if (do_validate_kcore_modules(modules_filename, kmaps))
1241 return -EINVAL;
1242
1243 return 0;
1244 }
1245
validate_kcore_addresses(const char * kallsyms_filename,struct map * map)1246 static int validate_kcore_addresses(const char *kallsyms_filename,
1247 struct map *map)
1248 {
1249 struct kmap *kmap = map__kmap(map);
1250
1251 if (!kmap)
1252 return -EINVAL;
1253
1254 if (kmap->ref_reloc_sym && kmap->ref_reloc_sym->name) {
1255 u64 start;
1256
1257 if (kallsyms__get_function_start(kallsyms_filename,
1258 kmap->ref_reloc_sym->name, &start))
1259 return -ENOENT;
1260 if (start != kmap->ref_reloc_sym->addr)
1261 return -EINVAL;
1262 }
1263
1264 return validate_kcore_modules(kallsyms_filename, map);
1265 }
1266
1267 struct kcore_mapfn_data {
1268 struct dso *dso;
1269 struct list_head maps;
1270 };
1271
kcore_mapfn(u64 start,u64 len,u64 pgoff,void * data)1272 static int kcore_mapfn(u64 start, u64 len, u64 pgoff, void *data)
1273 {
1274 struct kcore_mapfn_data *md = data;
1275 struct map_list_node *list_node = map_list_node__new();
1276
1277 if (!list_node)
1278 return -ENOMEM;
1279
1280 list_node->map = map__new2(start, md->dso);
1281 if (!list_node->map) {
1282 free(list_node);
1283 return -ENOMEM;
1284 }
1285
1286 map__set_end(list_node->map, map__start(list_node->map) + len);
1287 map__set_pgoff(list_node->map, pgoff);
1288
1289 list_add(&list_node->node, &md->maps);
1290
1291 return 0;
1292 }
1293
remove_old_maps(struct map * map,void * data)1294 static bool remove_old_maps(struct map *map, void *data)
1295 {
1296 const struct map *map_to_save = data;
1297
1298 /*
1299 * We need to preserve eBPF maps even if they are covered by kcore,
1300 * because we need to access eBPF dso for source data.
1301 */
1302 return !RC_CHK_EQUAL(map, map_to_save) && !__map__is_bpf_prog(map);
1303 }
1304
dso__load_kcore(struct dso * dso,struct map * map,const char * kallsyms_filename)1305 static int dso__load_kcore(struct dso *dso, struct map *map,
1306 const char *kallsyms_filename)
1307 {
1308 struct maps *kmaps = map__kmaps(map);
1309 struct kcore_mapfn_data md;
1310 struct map *map_ref, *replacement_map = NULL;
1311 struct machine *machine;
1312 bool is_64_bit;
1313 int err, fd;
1314 char kcore_filename[PATH_MAX];
1315 u64 stext;
1316
1317 if (!kmaps)
1318 return -EINVAL;
1319
1320 machine = maps__machine(kmaps);
1321
1322 /* This function requires that the map is the kernel map */
1323 if (!__map__is_kernel(map))
1324 return -EINVAL;
1325
1326 if (!filename_from_kallsyms_filename(kcore_filename, "kcore",
1327 kallsyms_filename))
1328 return -EINVAL;
1329
1330 /* Modules and kernel must be present at their original addresses */
1331 if (validate_kcore_addresses(kallsyms_filename, map))
1332 return -EINVAL;
1333
1334 md.dso = dso;
1335 INIT_LIST_HEAD(&md.maps);
1336
1337 fd = open(kcore_filename, O_RDONLY);
1338 if (fd < 0) {
1339 pr_debug("Failed to open %s. Note /proc/kcore requires CAP_SYS_RAWIO capability to access.\n",
1340 kcore_filename);
1341 return -EINVAL;
1342 }
1343
1344 /* Read new maps into temporary lists */
1345 err = file__read_maps(fd, map__prot(map) & PROT_EXEC, kcore_mapfn, &md,
1346 &is_64_bit);
1347 if (err)
1348 goto out_err;
1349 dso__set_is_64_bit(dso, is_64_bit);
1350
1351 if (list_empty(&md.maps)) {
1352 err = -EINVAL;
1353 goto out_err;
1354 }
1355
1356 /* Remove old maps */
1357 maps__remove_maps(kmaps, remove_old_maps, map);
1358 machine->trampolines_mapped = false;
1359
1360 /* Find the kernel map using the '_stext' symbol */
1361 if (!kallsyms__get_function_start(kallsyms_filename, "_stext", &stext)) {
1362 u64 replacement_size = 0;
1363 struct map_list_node *new_node;
1364
1365 list_for_each_entry(new_node, &md.maps, node) {
1366 struct map *new_map = new_node->map;
1367 u64 new_size = map__size(new_map);
1368
1369 if (!(stext >= map__start(new_map) && stext < map__end(new_map)))
1370 continue;
1371
1372 /*
1373 * On some architectures, ARM64 for example, the kernel
1374 * text can get allocated inside of the vmalloc segment.
1375 * Select the smallest matching segment, in case stext
1376 * falls within more than one in the list.
1377 */
1378 if (!replacement_map || new_size < replacement_size) {
1379 replacement_map = new_map;
1380 replacement_size = new_size;
1381 }
1382 }
1383 }
1384
1385 if (!replacement_map)
1386 replacement_map = list_entry(md.maps.next, struct map_list_node, node)->map;
1387
1388 /*
1389 * Update addresses of vmlinux map. Re-insert it to ensure maps are
1390 * correctly ordered. Do this before using maps__merge_in() for the
1391 * remaining maps so vmlinux gets split if necessary.
1392 */
1393 map_ref = map__get(map);
1394 maps__remove(kmaps, map_ref);
1395
1396 map__set_start(map_ref, map__start(replacement_map));
1397 map__set_end(map_ref, map__end(replacement_map));
1398 map__set_pgoff(map_ref, map__pgoff(replacement_map));
1399 map__set_mapping_type(map_ref, map__mapping_type(replacement_map));
1400
1401 err = maps__insert(kmaps, map_ref);
1402 map__put(map_ref);
1403 if (err)
1404 goto out_err;
1405
1406 /* Add new maps */
1407 while (!list_empty(&md.maps)) {
1408 struct map_list_node *new_node = list_entry(md.maps.next, struct map_list_node, node);
1409 struct map *new_map = new_node->map;
1410
1411 list_del_init(&new_node->node);
1412
1413 /* skip if replacement_map, already inserted above */
1414 if (!RC_CHK_EQUAL(new_map, replacement_map)) {
1415 /*
1416 * Merge kcore map into existing maps,
1417 * and ensure that current maps (eBPF)
1418 * stay intact.
1419 */
1420 if (maps__merge_in(kmaps, new_map)) {
1421 err = -EINVAL;
1422 goto out_err;
1423 }
1424 }
1425 map__zput(new_node->map);
1426 free(new_node);
1427 }
1428
1429 if (machine__is(machine, "x86_64")) {
1430 u64 addr;
1431
1432 /*
1433 * If one of the corresponding symbols is there, assume the
1434 * entry trampoline maps are too.
1435 */
1436 if (!kallsyms__get_function_start(kallsyms_filename,
1437 ENTRY_TRAMPOLINE_NAME,
1438 &addr))
1439 machine->trampolines_mapped = true;
1440 }
1441
1442 /*
1443 * Set the data type and long name so that kcore can be read via
1444 * dso__data_read_addr().
1445 */
1446 if (dso__kernel(dso) == DSO_SPACE__KERNEL_GUEST)
1447 dso__set_binary_type(dso, DSO_BINARY_TYPE__GUEST_KCORE);
1448 else
1449 dso__set_binary_type(dso, DSO_BINARY_TYPE__KCORE);
1450 dso__set_long_name(dso, strdup(kcore_filename), true);
1451
1452 close(fd);
1453
1454 if (map__prot(map) & PROT_EXEC)
1455 pr_debug("Using %s for kernel object code\n", kcore_filename);
1456 else
1457 pr_debug("Using %s for kernel data\n", kcore_filename);
1458
1459 return 0;
1460
1461 out_err:
1462 while (!list_empty(&md.maps)) {
1463 struct map_list_node *list_node;
1464
1465 list_node = list_entry(md.maps.next, struct map_list_node, node);
1466 list_del_init(&list_node->node);
1467 map__zput(list_node->map);
1468 free(list_node);
1469 }
1470 close(fd);
1471 return err;
1472 }
1473
1474 /*
1475 * If the kernel is relocated at boot time, kallsyms won't match. Compute the
1476 * delta based on the relocation reference symbol.
1477 */
kallsyms__delta(struct kmap * kmap,const char * filename,u64 * delta)1478 static int kallsyms__delta(struct kmap *kmap, const char *filename, u64 *delta)
1479 {
1480 u64 addr;
1481
1482 if (!kmap->ref_reloc_sym || !kmap->ref_reloc_sym->name)
1483 return 0;
1484
1485 if (kallsyms__get_function_start(filename, kmap->ref_reloc_sym->name, &addr))
1486 return -1;
1487
1488 *delta = addr - kmap->ref_reloc_sym->addr;
1489 return 0;
1490 }
1491
__dso__load_kallsyms(struct dso * dso,const char * filename,struct map * map,bool no_kcore)1492 int __dso__load_kallsyms(struct dso *dso, const char *filename,
1493 struct map *map, bool no_kcore)
1494 {
1495 struct kmap *kmap = map__kmap(map);
1496 u64 delta = 0;
1497
1498 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
1499 return -1;
1500
1501 if (!kmap || !kmap->kmaps)
1502 return -1;
1503
1504 if (dso__load_all_kallsyms(dso, filename) < 0)
1505 return -1;
1506
1507 if (kallsyms__delta(kmap, filename, &delta))
1508 return -1;
1509
1510 symbols__fixup_end(dso__symbols(dso), true);
1511 symbols__fixup_duplicate(dso__symbols(dso));
1512
1513 if (dso__kernel(dso) == DSO_SPACE__KERNEL_GUEST)
1514 dso__set_symtab_type(dso, DSO_BINARY_TYPE__GUEST_KALLSYMS);
1515 else
1516 dso__set_symtab_type(dso, DSO_BINARY_TYPE__KALLSYMS);
1517
1518 if (!no_kcore && !dso__load_kcore(dso, map, filename))
1519 return maps__split_kallsyms_for_kcore(kmap->kmaps, dso);
1520 else
1521 return maps__split_kallsyms(kmap->kmaps, dso, delta, map);
1522 }
1523
dso__load_kallsyms(struct dso * dso,const char * filename,struct map * map)1524 int dso__load_kallsyms(struct dso *dso, const char *filename,
1525 struct map *map)
1526 {
1527 return __dso__load_kallsyms(dso, filename, map, false);
1528 }
1529
dso__load_perf_map(const char * map_path,struct dso * dso)1530 static int dso__load_perf_map(const char *map_path, struct dso *dso)
1531 {
1532 char *line = NULL;
1533 size_t n;
1534 FILE *file;
1535 int nr_syms = 0;
1536
1537 file = fopen(map_path, "r");
1538 if (file == NULL)
1539 goto out_failure;
1540
1541 while (!feof(file)) {
1542 u64 start, size;
1543 struct symbol *sym;
1544 int line_len, len;
1545
1546 line_len = getline(&line, &n, file);
1547 if (line_len < 0)
1548 break;
1549
1550 if (!line)
1551 goto out_failure;
1552
1553 line[--line_len] = '\0'; /* \n */
1554
1555 len = hex2u64(line, &start);
1556
1557 len++;
1558 if (len + 2 >= line_len)
1559 continue;
1560
1561 len += hex2u64(line + len, &size);
1562
1563 len++;
1564 if (len + 2 >= line_len)
1565 continue;
1566
1567 sym = symbol__new(start, size, STB_GLOBAL, STT_FUNC, line + len);
1568
1569 if (sym == NULL)
1570 goto out_delete_line;
1571
1572 symbols__insert(dso__symbols(dso), sym);
1573 nr_syms++;
1574 }
1575
1576 free(line);
1577 fclose(file);
1578
1579 return nr_syms;
1580
1581 out_delete_line:
1582 free(line);
1583 out_failure:
1584 return -1;
1585 }
1586
1587 #ifdef HAVE_LIBBFD_SUPPORT
1588 #define PACKAGE 'perf'
1589 #include <bfd.h>
1590
bfd_symbols__cmpvalue(const void * a,const void * b)1591 static int bfd_symbols__cmpvalue(const void *a, const void *b)
1592 {
1593 const asymbol *as = *(const asymbol **)a, *bs = *(const asymbol **)b;
1594
1595 if (bfd_asymbol_value(as) != bfd_asymbol_value(bs))
1596 return bfd_asymbol_value(as) - bfd_asymbol_value(bs);
1597
1598 return bfd_asymbol_name(as)[0] - bfd_asymbol_name(bs)[0];
1599 }
1600
bfd2elf_binding(asymbol * symbol)1601 static int bfd2elf_binding(asymbol *symbol)
1602 {
1603 if (symbol->flags & BSF_WEAK)
1604 return STB_WEAK;
1605 if (symbol->flags & BSF_GLOBAL)
1606 return STB_GLOBAL;
1607 if (symbol->flags & BSF_LOCAL)
1608 return STB_LOCAL;
1609 return -1;
1610 }
1611
dso__load_bfd_symbols(struct dso * dso,const char * debugfile)1612 int dso__load_bfd_symbols(struct dso *dso, const char *debugfile)
1613 {
1614 int err = -1;
1615 long symbols_size, symbols_count, i;
1616 asection *section;
1617 asymbol **symbols, *sym;
1618 struct symbol *symbol;
1619 bfd *abfd;
1620 u64 start, len;
1621
1622 abfd = bfd_openr(debugfile, NULL);
1623 if (!abfd)
1624 return -1;
1625
1626 if (!bfd_check_format(abfd, bfd_object)) {
1627 pr_debug2("%s: cannot read %s bfd file.\n", __func__,
1628 dso__long_name(dso));
1629 goto out_close;
1630 }
1631
1632 if (bfd_get_flavour(abfd) == bfd_target_elf_flavour)
1633 goto out_close;
1634
1635 symbols_size = bfd_get_symtab_upper_bound(abfd);
1636 if (symbols_size == 0) {
1637 bfd_close(abfd);
1638 return 0;
1639 }
1640
1641 if (symbols_size < 0)
1642 goto out_close;
1643
1644 symbols = malloc(symbols_size);
1645 if (!symbols)
1646 goto out_close;
1647
1648 symbols_count = bfd_canonicalize_symtab(abfd, symbols);
1649 if (symbols_count < 0)
1650 goto out_free;
1651
1652 section = bfd_get_section_by_name(abfd, ".text");
1653 if (section) {
1654 for (i = 0; i < symbols_count; ++i) {
1655 if (!strcmp(bfd_asymbol_name(symbols[i]), "__ImageBase") ||
1656 !strcmp(bfd_asymbol_name(symbols[i]), "__image_base__"))
1657 break;
1658 }
1659 if (i < symbols_count) {
1660 /* PE symbols can only have 4 bytes, so use .text high bits */
1661 u64 text_offset = (section->vma - (u32)section->vma)
1662 + (u32)bfd_asymbol_value(symbols[i]);
1663 dso__set_text_offset(dso, text_offset);
1664 dso__set_text_end(dso, (section->vma - text_offset) + section->size);
1665 } else {
1666 dso__set_text_offset(dso, section->vma - section->filepos);
1667 dso__set_text_end(dso, section->filepos + section->size);
1668 }
1669 }
1670
1671 qsort(symbols, symbols_count, sizeof(asymbol *), bfd_symbols__cmpvalue);
1672
1673 #ifdef bfd_get_section
1674 #define bfd_asymbol_section bfd_get_section
1675 #endif
1676 for (i = 0; i < symbols_count; ++i) {
1677 sym = symbols[i];
1678 section = bfd_asymbol_section(sym);
1679 if (bfd2elf_binding(sym) < 0)
1680 continue;
1681
1682 while (i + 1 < symbols_count &&
1683 bfd_asymbol_section(symbols[i + 1]) == section &&
1684 bfd2elf_binding(symbols[i + 1]) < 0)
1685 i++;
1686
1687 if (i + 1 < symbols_count &&
1688 bfd_asymbol_section(symbols[i + 1]) == section)
1689 len = symbols[i + 1]->value - sym->value;
1690 else
1691 len = section->size - sym->value;
1692
1693 start = bfd_asymbol_value(sym) - dso__text_offset(dso);
1694 symbol = symbol__new(start, len, bfd2elf_binding(sym), STT_FUNC,
1695 bfd_asymbol_name(sym));
1696 if (!symbol)
1697 goto out_free;
1698
1699 symbols__insert(dso__symbols(dso), symbol);
1700 }
1701 #ifdef bfd_get_section
1702 #undef bfd_asymbol_section
1703 #endif
1704
1705 symbols__fixup_end(dso__symbols(dso), false);
1706 symbols__fixup_duplicate(dso__symbols(dso));
1707 dso__set_adjust_symbols(dso, true);
1708
1709 err = 0;
1710 out_free:
1711 free(symbols);
1712 out_close:
1713 bfd_close(abfd);
1714 return err;
1715 }
1716 #endif
1717
dso__is_compatible_symtab_type(struct dso * dso,bool kmod,enum dso_binary_type type)1718 static bool dso__is_compatible_symtab_type(struct dso *dso, bool kmod,
1719 enum dso_binary_type type)
1720 {
1721 switch (type) {
1722 case DSO_BINARY_TYPE__JAVA_JIT:
1723 case DSO_BINARY_TYPE__DEBUGLINK:
1724 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
1725 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
1726 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
1727 case DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO:
1728 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
1729 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
1730 case DSO_BINARY_TYPE__GNU_DEBUGDATA:
1731 return !kmod && dso__kernel(dso) == DSO_SPACE__USER;
1732
1733 case DSO_BINARY_TYPE__KALLSYMS:
1734 case DSO_BINARY_TYPE__VMLINUX:
1735 case DSO_BINARY_TYPE__KCORE:
1736 return dso__kernel(dso) == DSO_SPACE__KERNEL;
1737
1738 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
1739 case DSO_BINARY_TYPE__GUEST_VMLINUX:
1740 case DSO_BINARY_TYPE__GUEST_KCORE:
1741 return dso__kernel(dso) == DSO_SPACE__KERNEL_GUEST;
1742
1743 case DSO_BINARY_TYPE__GUEST_KMODULE:
1744 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
1745 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
1746 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
1747 /*
1748 * kernel modules know their symtab type - it's set when
1749 * creating a module dso in machine__addnew_module_map().
1750 */
1751 return kmod && dso__symtab_type(dso) == type;
1752
1753 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
1754 case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO:
1755 return true;
1756
1757 case DSO_BINARY_TYPE__BPF_PROG_INFO:
1758 case DSO_BINARY_TYPE__BPF_IMAGE:
1759 case DSO_BINARY_TYPE__OOL:
1760 case DSO_BINARY_TYPE__NOT_FOUND:
1761 default:
1762 return false;
1763 }
1764 }
1765
1766 /* Checks for the existence of the perf-<pid>.map file in two different
1767 * locations. First, if the process is a separate mount namespace, check in
1768 * that namespace using the pid of the innermost pid namespace. If's not in a
1769 * namespace, or the file can't be found there, try in the mount namespace of
1770 * the tracing process using our view of its pid.
1771 */
dso__find_perf_map(char * filebuf,size_t bufsz,struct nsinfo ** nsip)1772 static int dso__find_perf_map(char *filebuf, size_t bufsz,
1773 struct nsinfo **nsip)
1774 {
1775 struct nscookie nsc;
1776 struct nsinfo *nsi;
1777 struct nsinfo *nnsi;
1778 int rc = -1;
1779
1780 nsi = *nsip;
1781
1782 if (nsinfo__need_setns(nsi)) {
1783 snprintf(filebuf, bufsz, "/tmp/perf-%d.map", nsinfo__nstgid(nsi));
1784 nsinfo__mountns_enter(nsi, &nsc);
1785 rc = access(filebuf, R_OK);
1786 nsinfo__mountns_exit(&nsc);
1787 if (rc == 0)
1788 return rc;
1789 }
1790
1791 nnsi = nsinfo__copy(nsi);
1792 if (nnsi) {
1793 nsinfo__put(nsi);
1794
1795 nsinfo__clear_need_setns(nnsi);
1796 snprintf(filebuf, bufsz, "/tmp/perf-%d.map", nsinfo__tgid(nnsi));
1797 *nsip = nnsi;
1798 rc = 0;
1799 }
1800
1801 return rc;
1802 }
1803
dso__load(struct dso * dso,struct map * map)1804 int dso__load(struct dso *dso, struct map *map)
1805 {
1806 char *name;
1807 int ret = -1;
1808 u_int i;
1809 struct machine *machine = NULL;
1810 char *root_dir = (char *) "";
1811 int ss_pos = 0;
1812 struct symsrc ss_[2];
1813 struct symsrc *syms_ss = NULL, *runtime_ss = NULL;
1814 bool kmod;
1815 bool perfmap;
1816 struct nscookie nsc;
1817 char newmapname[PATH_MAX];
1818 const char *map_path = dso__long_name(dso);
1819
1820 mutex_lock(dso__lock(dso));
1821 perfmap = is_perf_pid_map_name(map_path);
1822
1823 if (perfmap) {
1824 if (dso__nsinfo(dso) &&
1825 (dso__find_perf_map(newmapname, sizeof(newmapname),
1826 dso__nsinfo_ptr(dso)) == 0)) {
1827 map_path = newmapname;
1828 }
1829 }
1830
1831 nsinfo__mountns_enter(dso__nsinfo(dso), &nsc);
1832
1833 /* check again under the dso->lock */
1834 if (dso__loaded(dso)) {
1835 ret = 1;
1836 goto out;
1837 }
1838
1839 kmod = dso__is_kmod(dso);
1840
1841 if (dso__kernel(dso) && !kmod) {
1842 if (dso__kernel(dso) == DSO_SPACE__KERNEL)
1843 ret = dso__load_kernel_sym(dso, map);
1844 else if (dso__kernel(dso) == DSO_SPACE__KERNEL_GUEST)
1845 ret = dso__load_guest_kernel_sym(dso, map);
1846
1847 machine = maps__machine(map__kmaps(map));
1848 if (machine__is(machine, "x86_64"))
1849 machine__map_x86_64_entry_trampolines(machine, dso);
1850 goto out;
1851 }
1852
1853 dso__set_adjust_symbols(dso, false);
1854
1855 if (perfmap) {
1856 ret = dso__load_perf_map(map_path, dso);
1857 dso__set_symtab_type(dso, ret > 0
1858 ? DSO_BINARY_TYPE__JAVA_JIT
1859 : DSO_BINARY_TYPE__NOT_FOUND);
1860 goto out;
1861 }
1862
1863 if (machine)
1864 root_dir = machine->root_dir;
1865
1866 name = malloc(PATH_MAX);
1867 if (!name)
1868 goto out;
1869
1870 /*
1871 * Read the build id if possible. This is required for
1872 * DSO_BINARY_TYPE__BUILDID_DEBUGINFO to work
1873 */
1874 if (!dso__has_build_id(dso) &&
1875 is_regular_file(dso__long_name(dso))) {
1876 struct build_id bid = { .size = 0, };
1877
1878 __symbol__join_symfs(name, PATH_MAX, dso__long_name(dso));
1879 if (filename__read_build_id(name, &bid) > 0)
1880 dso__set_build_id(dso, &bid);
1881 }
1882
1883 /*
1884 * Iterate over candidate debug images.
1885 * Keep track of "interesting" ones (those which have a symtab, dynsym,
1886 * and/or opd section) for processing.
1887 */
1888 for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) {
1889 struct symsrc *ss = &ss_[ss_pos];
1890 bool next_slot = false;
1891 bool is_reg;
1892 bool nsexit;
1893 int bfdrc = -1;
1894 int sirc = -1;
1895
1896 enum dso_binary_type symtab_type = binary_type_symtab[i];
1897
1898 nsexit = (symtab_type == DSO_BINARY_TYPE__BUILD_ID_CACHE ||
1899 symtab_type == DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO);
1900
1901 if (!dso__is_compatible_symtab_type(dso, kmod, symtab_type))
1902 continue;
1903
1904 if (dso__read_binary_type_filename(dso, symtab_type,
1905 root_dir, name, PATH_MAX))
1906 continue;
1907
1908 if (nsexit)
1909 nsinfo__mountns_exit(&nsc);
1910
1911 is_reg = is_regular_file(name);
1912 if (!is_reg && errno == ENOENT && dso__nsinfo(dso)) {
1913 char *new_name = dso__filename_with_chroot(dso, name);
1914 if (new_name) {
1915 is_reg = is_regular_file(new_name);
1916 strlcpy(name, new_name, PATH_MAX);
1917 free(new_name);
1918 }
1919 }
1920
1921 #ifdef HAVE_LIBBFD_SUPPORT
1922 if (is_reg)
1923 bfdrc = dso__load_bfd_symbols(dso, name);
1924 #endif
1925 if (is_reg && bfdrc < 0)
1926 sirc = symsrc__init(ss, dso, name, symtab_type);
1927
1928 if (nsexit)
1929 nsinfo__mountns_enter(dso__nsinfo(dso), &nsc);
1930
1931 if (bfdrc == 0) {
1932 ret = 0;
1933 break;
1934 }
1935
1936 if (!is_reg || sirc < 0)
1937 continue;
1938
1939 if (!syms_ss && symsrc__has_symtab(ss)) {
1940 syms_ss = ss;
1941 next_slot = true;
1942 if (!dso__symsrc_filename(dso))
1943 dso__set_symsrc_filename(dso, strdup(name));
1944 }
1945
1946 if (!runtime_ss && symsrc__possibly_runtime(ss)) {
1947 runtime_ss = ss;
1948 next_slot = true;
1949 }
1950
1951 if (next_slot) {
1952 ss_pos++;
1953
1954 if (dso__binary_type(dso) == DSO_BINARY_TYPE__NOT_FOUND)
1955 dso__set_binary_type(dso, symtab_type);
1956
1957 if (syms_ss && runtime_ss)
1958 break;
1959 } else {
1960 symsrc__destroy(ss);
1961 }
1962
1963 }
1964
1965 if (!runtime_ss && !syms_ss)
1966 goto out_free;
1967
1968 if (runtime_ss && !syms_ss) {
1969 syms_ss = runtime_ss;
1970 }
1971
1972 /* We'll have to hope for the best */
1973 if (!runtime_ss && syms_ss)
1974 runtime_ss = syms_ss;
1975
1976 if (syms_ss)
1977 ret = dso__load_sym(dso, map, syms_ss, runtime_ss, kmod);
1978 else
1979 ret = -1;
1980
1981 if (ret > 0) {
1982 int nr_plt;
1983
1984 nr_plt = dso__synthesize_plt_symbols(dso, runtime_ss);
1985 if (nr_plt > 0)
1986 ret += nr_plt;
1987 }
1988
1989 for (; ss_pos > 0; ss_pos--)
1990 symsrc__destroy(&ss_[ss_pos - 1]);
1991 out_free:
1992 free(name);
1993 if (ret < 0 && strstr(dso__name(dso), " (deleted)") != NULL)
1994 ret = 0;
1995 out:
1996 dso__set_loaded(dso);
1997 mutex_unlock(dso__lock(dso));
1998 nsinfo__mountns_exit(&nsc);
1999
2000 return ret;
2001 }
2002
2003 /*
2004 * Always takes ownership of vmlinux when vmlinux_allocated == true, even if
2005 * it returns an error.
2006 */
dso__load_vmlinux(struct dso * dso,struct map * map,const char * vmlinux,bool vmlinux_allocated)2007 int dso__load_vmlinux(struct dso *dso, struct map *map,
2008 const char *vmlinux, bool vmlinux_allocated)
2009 {
2010 int err = -1;
2011 struct symsrc ss;
2012 char symfs_vmlinux[PATH_MAX];
2013 enum dso_binary_type symtab_type;
2014
2015 if (vmlinux[0] == '/')
2016 snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s", vmlinux);
2017 else
2018 symbol__join_symfs(symfs_vmlinux, vmlinux);
2019
2020 if (dso__kernel(dso) == DSO_SPACE__KERNEL_GUEST)
2021 symtab_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
2022 else
2023 symtab_type = DSO_BINARY_TYPE__VMLINUX;
2024
2025 if (symsrc__init(&ss, dso, symfs_vmlinux, symtab_type)) {
2026 if (vmlinux_allocated)
2027 free((char *) vmlinux);
2028 return -1;
2029 }
2030
2031 /*
2032 * dso__load_sym() may copy 'dso' which will result in the copies having
2033 * an incorrect long name unless we set it here first.
2034 */
2035 dso__set_long_name(dso, vmlinux, vmlinux_allocated);
2036 if (dso__kernel(dso) == DSO_SPACE__KERNEL_GUEST)
2037 dso__set_binary_type(dso, DSO_BINARY_TYPE__GUEST_VMLINUX);
2038 else
2039 dso__set_binary_type(dso, DSO_BINARY_TYPE__VMLINUX);
2040
2041 err = dso__load_sym(dso, map, &ss, &ss, 0);
2042 symsrc__destroy(&ss);
2043
2044 if (err > 0) {
2045 dso__set_loaded(dso);
2046 pr_debug("Using %s for symbols\n", symfs_vmlinux);
2047 }
2048
2049 return err;
2050 }
2051
dso__load_vmlinux_path(struct dso * dso,struct map * map)2052 int dso__load_vmlinux_path(struct dso *dso, struct map *map)
2053 {
2054 int i, err = 0;
2055 char *filename = NULL;
2056
2057 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
2058 vmlinux_path__nr_entries + 1);
2059
2060 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
2061 err = dso__load_vmlinux(dso, map, vmlinux_path[i], false);
2062 if (err > 0)
2063 goto out;
2064 }
2065
2066 if (!symbol_conf.ignore_vmlinux_buildid)
2067 filename = dso__build_id_filename(dso, NULL, 0, false);
2068 if (filename != NULL) {
2069 err = dso__load_vmlinux(dso, map, filename, true);
2070 if (err > 0)
2071 goto out;
2072 }
2073 out:
2074 return err;
2075 }
2076
visible_dir_filter(const char * name,struct dirent * d)2077 static bool visible_dir_filter(const char *name, struct dirent *d)
2078 {
2079 if (d->d_type != DT_DIR)
2080 return false;
2081 return lsdir_no_dot_filter(name, d);
2082 }
2083
find_matching_kcore(struct map * map,char * dir,size_t dir_sz)2084 static int find_matching_kcore(struct map *map, char *dir, size_t dir_sz)
2085 {
2086 char kallsyms_filename[PATH_MAX];
2087 int ret = -1;
2088 struct strlist *dirs;
2089 struct str_node *nd;
2090
2091 dirs = lsdir(dir, visible_dir_filter);
2092 if (!dirs)
2093 return -1;
2094
2095 strlist__for_each_entry(nd, dirs) {
2096 scnprintf(kallsyms_filename, sizeof(kallsyms_filename),
2097 "%s/%s/kallsyms", dir, nd->s);
2098 if (!validate_kcore_addresses(kallsyms_filename, map)) {
2099 strlcpy(dir, kallsyms_filename, dir_sz);
2100 ret = 0;
2101 break;
2102 }
2103 }
2104
2105 strlist__delete(dirs);
2106
2107 return ret;
2108 }
2109
2110 /*
2111 * Use open(O_RDONLY) to check readability directly instead of access(R_OK)
2112 * since access(R_OK) only checks with real UID/GID but open() use effective
2113 * UID/GID and actual capabilities (e.g. /proc/kcore requires CAP_SYS_RAWIO).
2114 */
filename__readable(const char * file)2115 static bool filename__readable(const char *file)
2116 {
2117 int fd = open(file, O_RDONLY);
2118 if (fd < 0)
2119 return false;
2120 close(fd);
2121 return true;
2122 }
2123
dso__find_kallsyms(struct dso * dso,struct map * map)2124 static char *dso__find_kallsyms(struct dso *dso, struct map *map)
2125 {
2126 struct build_id bid = { .size = 0, };
2127 char sbuild_id[SBUILD_ID_SIZE];
2128 bool is_host = false;
2129 char path[PATH_MAX];
2130
2131 if (!dso__has_build_id(dso)) {
2132 /*
2133 * Last resort, if we don't have a build-id and couldn't find
2134 * any vmlinux file, try the running kernel kallsyms table.
2135 */
2136 goto proc_kallsyms;
2137 }
2138
2139 if (sysfs__read_build_id("/sys/kernel/notes", &bid) == 0)
2140 is_host = dso__build_id_equal(dso, &bid);
2141
2142 /* Try a fast path for /proc/kallsyms if possible */
2143 if (is_host) {
2144 /*
2145 * Do not check the build-id cache, unless we know we cannot use
2146 * /proc/kcore or module maps don't match to /proc/kallsyms.
2147 * To check readability of /proc/kcore, do not use access(R_OK)
2148 * since /proc/kcore requires CAP_SYS_RAWIO to read and access
2149 * can't check it.
2150 */
2151 if (filename__readable("/proc/kcore") &&
2152 !validate_kcore_addresses("/proc/kallsyms", map))
2153 goto proc_kallsyms;
2154 }
2155
2156 build_id__snprintf(dso__bid(dso), sbuild_id, sizeof(sbuild_id));
2157
2158 /* Find kallsyms in build-id cache with kcore */
2159 scnprintf(path, sizeof(path), "%s/%s/%s",
2160 buildid_dir, DSO__NAME_KCORE, sbuild_id);
2161
2162 if (!find_matching_kcore(map, path, sizeof(path)))
2163 return strdup(path);
2164
2165 /* Use current /proc/kallsyms if possible */
2166 if (is_host) {
2167 proc_kallsyms:
2168 return strdup("/proc/kallsyms");
2169 }
2170
2171 /* Finally, find a cache of kallsyms */
2172 if (!build_id_cache__kallsyms_path(sbuild_id, path, sizeof(path))) {
2173 pr_err("No kallsyms or vmlinux with build-id %s was found\n",
2174 sbuild_id);
2175 return NULL;
2176 }
2177
2178 return strdup(path);
2179 }
2180
dso__load_kernel_sym(struct dso * dso,struct map * map)2181 static int dso__load_kernel_sym(struct dso *dso, struct map *map)
2182 {
2183 int err;
2184 const char *kallsyms_filename = NULL;
2185 char *kallsyms_allocated_filename = NULL;
2186 char *filename = NULL;
2187
2188 /*
2189 * Step 1: if the user specified a kallsyms or vmlinux filename, use
2190 * it and only it, reporting errors to the user if it cannot be used.
2191 *
2192 * For instance, try to analyse an ARM perf.data file _without_ a
2193 * build-id, or if the user specifies the wrong path to the right
2194 * vmlinux file, obviously we can't fallback to another vmlinux (a
2195 * x86_86 one, on the machine where analysis is being performed, say),
2196 * or worse, /proc/kallsyms.
2197 *
2198 * If the specified file _has_ a build-id and there is a build-id
2199 * section in the perf.data file, we will still do the expected
2200 * validation in dso__load_vmlinux and will bail out if they don't
2201 * match.
2202 */
2203 if (symbol_conf.kallsyms_name != NULL) {
2204 kallsyms_filename = symbol_conf.kallsyms_name;
2205 goto do_kallsyms;
2206 }
2207
2208 if (!symbol_conf.ignore_vmlinux && symbol_conf.vmlinux_name != NULL) {
2209 return dso__load_vmlinux(dso, map, symbol_conf.vmlinux_name, false);
2210 }
2211
2212 /*
2213 * Before checking on common vmlinux locations, check if it's
2214 * stored as standard build id binary (not kallsyms) under
2215 * .debug cache.
2216 */
2217 if (!symbol_conf.ignore_vmlinux_buildid)
2218 filename = __dso__build_id_filename(dso, NULL, 0, false, false);
2219 if (filename != NULL) {
2220 err = dso__load_vmlinux(dso, map, filename, true);
2221 if (err > 0)
2222 return err;
2223 }
2224
2225 if (!symbol_conf.ignore_vmlinux && vmlinux_path != NULL) {
2226 err = dso__load_vmlinux_path(dso, map);
2227 if (err > 0)
2228 return err;
2229 }
2230
2231 /* do not try local files if a symfs was given */
2232 if (symbol_conf.symfs[0] != 0)
2233 return -1;
2234
2235 kallsyms_allocated_filename = dso__find_kallsyms(dso, map);
2236 if (!kallsyms_allocated_filename)
2237 return -1;
2238
2239 kallsyms_filename = kallsyms_allocated_filename;
2240
2241 do_kallsyms:
2242 err = dso__load_kallsyms(dso, kallsyms_filename, map);
2243 if (err > 0)
2244 pr_debug("Using %s for symbols\n", kallsyms_filename);
2245 free(kallsyms_allocated_filename);
2246
2247 if (err > 0 && !dso__is_kcore(dso)) {
2248 dso__set_binary_type(dso, DSO_BINARY_TYPE__KALLSYMS);
2249 dso__set_long_name(dso, DSO__NAME_KALLSYMS, false);
2250 map__fixup_start(map);
2251 map__fixup_end(map);
2252 }
2253
2254 return err;
2255 }
2256
dso__load_guest_kernel_sym(struct dso * dso,struct map * map)2257 static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map)
2258 {
2259 int err;
2260 const char *kallsyms_filename;
2261 struct machine *machine = maps__machine(map__kmaps(map));
2262 char path[PATH_MAX];
2263
2264 if (machine->kallsyms_filename) {
2265 kallsyms_filename = machine->kallsyms_filename;
2266 } else if (machine__is_default_guest(machine)) {
2267 /*
2268 * if the user specified a vmlinux filename, use it and only
2269 * it, reporting errors to the user if it cannot be used.
2270 * Or use file guest_kallsyms inputted by user on commandline
2271 */
2272 if (symbol_conf.default_guest_vmlinux_name != NULL) {
2273 err = dso__load_vmlinux(dso, map,
2274 symbol_conf.default_guest_vmlinux_name,
2275 false);
2276 return err;
2277 }
2278
2279 kallsyms_filename = symbol_conf.default_guest_kallsyms;
2280 if (!kallsyms_filename)
2281 return -1;
2282 } else {
2283 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
2284 kallsyms_filename = path;
2285 }
2286
2287 err = dso__load_kallsyms(dso, kallsyms_filename, map);
2288 if (err > 0)
2289 pr_debug("Using %s for symbols\n", kallsyms_filename);
2290 if (err > 0 && !dso__is_kcore(dso)) {
2291 dso__set_binary_type(dso, DSO_BINARY_TYPE__GUEST_KALLSYMS);
2292 dso__set_long_name(dso, machine->mmap_name, false);
2293 map__fixup_start(map);
2294 map__fixup_end(map);
2295 }
2296
2297 return err;
2298 }
2299
vmlinux_path__exit(void)2300 static void vmlinux_path__exit(void)
2301 {
2302 while (--vmlinux_path__nr_entries >= 0)
2303 zfree(&vmlinux_path[vmlinux_path__nr_entries]);
2304 vmlinux_path__nr_entries = 0;
2305
2306 zfree(&vmlinux_path);
2307 }
2308
2309 static const char * const vmlinux_paths[] = {
2310 "vmlinux",
2311 "/boot/vmlinux"
2312 };
2313
2314 static const char * const vmlinux_paths_upd[] = {
2315 "/boot/vmlinux-%s",
2316 "/usr/lib/debug/boot/vmlinux-%s",
2317 "/lib/modules/%s/build/vmlinux",
2318 "/usr/lib/debug/lib/modules/%s/vmlinux",
2319 "/usr/lib/debug/boot/vmlinux-%s.debug"
2320 };
2321
vmlinux_path__add(const char * new_entry)2322 static int vmlinux_path__add(const char *new_entry)
2323 {
2324 vmlinux_path[vmlinux_path__nr_entries] = strdup(new_entry);
2325 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2326 return -1;
2327 ++vmlinux_path__nr_entries;
2328
2329 return 0;
2330 }
2331
vmlinux_path__init(struct perf_env * env)2332 static int vmlinux_path__init(struct perf_env *env)
2333 {
2334 struct utsname uts;
2335 char bf[PATH_MAX];
2336 char *kernel_version;
2337 unsigned int i;
2338
2339 vmlinux_path = malloc(sizeof(char *) * (ARRAY_SIZE(vmlinux_paths) +
2340 ARRAY_SIZE(vmlinux_paths_upd)));
2341 if (vmlinux_path == NULL)
2342 return -1;
2343
2344 for (i = 0; i < ARRAY_SIZE(vmlinux_paths); i++)
2345 if (vmlinux_path__add(vmlinux_paths[i]) < 0)
2346 goto out_fail;
2347
2348 /* only try kernel version if no symfs was given */
2349 if (symbol_conf.symfs[0] != 0)
2350 return 0;
2351
2352 if (env) {
2353 kernel_version = env->os_release;
2354 } else {
2355 if (uname(&uts) < 0)
2356 goto out_fail;
2357
2358 kernel_version = uts.release;
2359 }
2360
2361 for (i = 0; i < ARRAY_SIZE(vmlinux_paths_upd); i++) {
2362 snprintf(bf, sizeof(bf), vmlinux_paths_upd[i], kernel_version);
2363 if (vmlinux_path__add(bf) < 0)
2364 goto out_fail;
2365 }
2366
2367 return 0;
2368
2369 out_fail:
2370 vmlinux_path__exit();
2371 return -1;
2372 }
2373
setup_list(struct strlist ** list,const char * list_str,const char * list_name)2374 int setup_list(struct strlist **list, const char *list_str,
2375 const char *list_name)
2376 {
2377 if (list_str == NULL)
2378 return 0;
2379
2380 *list = strlist__new(list_str, NULL);
2381 if (!*list) {
2382 pr_err("problems parsing %s list\n", list_name);
2383 return -1;
2384 }
2385
2386 symbol_conf.has_filter = true;
2387 return 0;
2388 }
2389
setup_intlist(struct intlist ** list,const char * list_str,const char * list_name)2390 int setup_intlist(struct intlist **list, const char *list_str,
2391 const char *list_name)
2392 {
2393 if (list_str == NULL)
2394 return 0;
2395
2396 *list = intlist__new(list_str);
2397 if (!*list) {
2398 pr_err("problems parsing %s list\n", list_name);
2399 return -1;
2400 }
2401 return 0;
2402 }
2403
setup_addrlist(struct intlist ** addr_list,struct strlist * sym_list)2404 static int setup_addrlist(struct intlist **addr_list, struct strlist *sym_list)
2405 {
2406 struct str_node *pos, *tmp;
2407 unsigned long val;
2408 char *sep;
2409 const char *end;
2410 int i = 0, err;
2411
2412 *addr_list = intlist__new(NULL);
2413 if (!*addr_list)
2414 return -1;
2415
2416 strlist__for_each_entry_safe(pos, tmp, sym_list) {
2417 errno = 0;
2418 val = strtoul(pos->s, &sep, 16);
2419 if (errno || (sep == pos->s))
2420 continue;
2421
2422 if (*sep != '\0') {
2423 end = pos->s + strlen(pos->s) - 1;
2424 while (end >= sep && isspace(*end))
2425 end--;
2426
2427 if (end >= sep)
2428 continue;
2429 }
2430
2431 err = intlist__add(*addr_list, val);
2432 if (err)
2433 break;
2434
2435 strlist__remove(sym_list, pos);
2436 i++;
2437 }
2438
2439 if (i == 0) {
2440 intlist__delete(*addr_list);
2441 *addr_list = NULL;
2442 }
2443
2444 return 0;
2445 }
2446
symbol__read_kptr_restrict(void)2447 static bool symbol__read_kptr_restrict(void)
2448 {
2449 bool value = false;
2450 FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
2451 bool used_root;
2452 bool cap_syslog = perf_cap__capable(CAP_SYSLOG, &used_root);
2453
2454 if (fp != NULL) {
2455 char line[8];
2456
2457 if (fgets(line, sizeof(line), fp) != NULL)
2458 value = cap_syslog ? (atoi(line) >= 2) : (atoi(line) != 0);
2459
2460 fclose(fp);
2461 }
2462
2463 /* Per kernel/kallsyms.c:
2464 * we also restrict when perf_event_paranoid > 1 w/o CAP_SYSLOG
2465 */
2466 if (perf_event_paranoid() > 1 && !cap_syslog)
2467 value = true;
2468
2469 return value;
2470 }
2471
symbol__annotation_init(void)2472 int symbol__annotation_init(void)
2473 {
2474 if (symbol_conf.init_annotation)
2475 return 0;
2476
2477 if (symbol_conf.initialized) {
2478 pr_err("Annotation needs to be init before symbol__init()\n");
2479 return -1;
2480 }
2481
2482 symbol_conf.priv_size += sizeof(struct annotation);
2483 symbol_conf.init_annotation = true;
2484 return 0;
2485 }
2486
setup_parallelism_bitmap(void)2487 static int setup_parallelism_bitmap(void)
2488 {
2489 struct perf_cpu_map *map;
2490 struct perf_cpu cpu;
2491 int i, err = -1;
2492
2493 if (symbol_conf.parallelism_list_str == NULL)
2494 return 0;
2495
2496 map = perf_cpu_map__new(symbol_conf.parallelism_list_str);
2497 if (map == NULL) {
2498 pr_err("failed to parse parallelism filter list\n");
2499 return -1;
2500 }
2501
2502 bitmap_fill(symbol_conf.parallelism_filter, MAX_NR_CPUS + 1);
2503 perf_cpu_map__for_each_cpu(cpu, i, map) {
2504 if (cpu.cpu <= 0 || cpu.cpu > MAX_NR_CPUS) {
2505 pr_err("Requested parallelism level %d is invalid.\n", cpu.cpu);
2506 goto out_delete_map;
2507 }
2508 __clear_bit(cpu.cpu, symbol_conf.parallelism_filter);
2509 }
2510
2511 err = 0;
2512 out_delete_map:
2513 perf_cpu_map__put(map);
2514 return err;
2515 }
2516
symbol__init(struct perf_env * env)2517 int symbol__init(struct perf_env *env)
2518 {
2519 const char *symfs;
2520
2521 if (symbol_conf.initialized)
2522 return 0;
2523
2524 symbol_conf.priv_size = PERF_ALIGN(symbol_conf.priv_size, sizeof(u64));
2525
2526 symbol__elf_init();
2527
2528 if (symbol_conf.try_vmlinux_path && vmlinux_path__init(env) < 0)
2529 return -1;
2530
2531 if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
2532 pr_err("'.' is the only non valid --field-separator argument\n");
2533 return -1;
2534 }
2535
2536 if (setup_parallelism_bitmap())
2537 return -1;
2538
2539 if (setup_list(&symbol_conf.dso_list,
2540 symbol_conf.dso_list_str, "dso") < 0)
2541 return -1;
2542
2543 if (setup_list(&symbol_conf.comm_list,
2544 symbol_conf.comm_list_str, "comm") < 0)
2545 goto out_free_dso_list;
2546
2547 if (setup_intlist(&symbol_conf.pid_list,
2548 symbol_conf.pid_list_str, "pid") < 0)
2549 goto out_free_comm_list;
2550
2551 if (setup_intlist(&symbol_conf.tid_list,
2552 symbol_conf.tid_list_str, "tid") < 0)
2553 goto out_free_pid_list;
2554
2555 if (setup_list(&symbol_conf.sym_list,
2556 symbol_conf.sym_list_str, "symbol") < 0)
2557 goto out_free_tid_list;
2558
2559 if (symbol_conf.sym_list &&
2560 setup_addrlist(&symbol_conf.addr_list, symbol_conf.sym_list) < 0)
2561 goto out_free_sym_list;
2562
2563 if (setup_list(&symbol_conf.bt_stop_list,
2564 symbol_conf.bt_stop_list_str, "symbol") < 0)
2565 goto out_free_sym_list;
2566
2567 /*
2568 * A path to symbols of "/" is identical to ""
2569 * reset here for simplicity.
2570 */
2571 symfs = realpath(symbol_conf.symfs, NULL);
2572 if (symfs == NULL)
2573 symfs = symbol_conf.symfs;
2574 if (strcmp(symfs, "/") == 0)
2575 symbol_conf.symfs = "";
2576 if (symfs != symbol_conf.symfs)
2577 free((void *)symfs);
2578
2579 symbol_conf.kptr_restrict = symbol__read_kptr_restrict();
2580
2581 symbol_conf.initialized = true;
2582 return 0;
2583
2584 out_free_sym_list:
2585 strlist__delete(symbol_conf.sym_list);
2586 intlist__delete(symbol_conf.addr_list);
2587 out_free_tid_list:
2588 intlist__delete(symbol_conf.tid_list);
2589 out_free_pid_list:
2590 intlist__delete(symbol_conf.pid_list);
2591 out_free_comm_list:
2592 strlist__delete(symbol_conf.comm_list);
2593 out_free_dso_list:
2594 strlist__delete(symbol_conf.dso_list);
2595 return -1;
2596 }
2597
symbol__exit(void)2598 void symbol__exit(void)
2599 {
2600 if (!symbol_conf.initialized)
2601 return;
2602 strlist__delete(symbol_conf.bt_stop_list);
2603 strlist__delete(symbol_conf.sym_list);
2604 strlist__delete(symbol_conf.dso_list);
2605 strlist__delete(symbol_conf.comm_list);
2606 intlist__delete(symbol_conf.tid_list);
2607 intlist__delete(symbol_conf.pid_list);
2608 intlist__delete(symbol_conf.addr_list);
2609 vmlinux_path__exit();
2610 symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
2611 symbol_conf.bt_stop_list = NULL;
2612 symbol_conf.initialized = false;
2613 }
2614
symbol__config_symfs(const struct option * opt __maybe_unused,const char * dir,int unset __maybe_unused)2615 int symbol__config_symfs(const struct option *opt __maybe_unused,
2616 const char *dir, int unset __maybe_unused)
2617 {
2618 char *bf = NULL;
2619 int ret;
2620
2621 symbol_conf.symfs = strdup(dir);
2622 if (symbol_conf.symfs == NULL)
2623 return -ENOMEM;
2624
2625 /* skip the locally configured cache if a symfs is given, and
2626 * config buildid dir to symfs/.debug
2627 */
2628 ret = asprintf(&bf, "%s/%s", dir, ".debug");
2629 if (ret < 0)
2630 return -ENOMEM;
2631
2632 set_buildid_dir(bf);
2633
2634 free(bf);
2635 return 0;
2636 }
2637
2638 /*
2639 * Checks that user supplied symbol kernel files are accessible because
2640 * the default mechanism for accessing elf files fails silently. i.e. if
2641 * debug syms for a build ID aren't found perf carries on normally. When
2642 * they are user supplied we should assume that the user doesn't want to
2643 * silently fail.
2644 */
symbol__validate_sym_arguments(void)2645 int symbol__validate_sym_arguments(void)
2646 {
2647 if (symbol_conf.vmlinux_name &&
2648 access(symbol_conf.vmlinux_name, R_OK)) {
2649 pr_err("Invalid file: %s\n", symbol_conf.vmlinux_name);
2650 return -EINVAL;
2651 }
2652 if (symbol_conf.kallsyms_name &&
2653 access(symbol_conf.kallsyms_name, R_OK)) {
2654 pr_err("Invalid file: %s\n", symbol_conf.kallsyms_name);
2655 return -EINVAL;
2656 }
2657 return 0;
2658 }
2659
want_demangle(bool is_kernel_sym)2660 static bool want_demangle(bool is_kernel_sym)
2661 {
2662 return is_kernel_sym ? symbol_conf.demangle_kernel : symbol_conf.demangle;
2663 }
2664
2665 /*
2666 * Demangle C++ function signature, typically replaced by demangle-cxx.cpp
2667 * version.
2668 */
2669 #ifndef HAVE_CXA_DEMANGLE_SUPPORT
cxx_demangle_sym(const char * str __maybe_unused,bool params __maybe_unused,bool modifiers __maybe_unused)2670 char *cxx_demangle_sym(const char *str __maybe_unused, bool params __maybe_unused,
2671 bool modifiers __maybe_unused)
2672 {
2673 #ifdef HAVE_LIBBFD_SUPPORT
2674 int flags = (params ? DMGL_PARAMS : 0) | (modifiers ? DMGL_ANSI : 0);
2675
2676 return bfd_demangle(NULL, str, flags);
2677 #elif defined(HAVE_CPLUS_DEMANGLE_SUPPORT)
2678 int flags = (params ? DMGL_PARAMS : 0) | (modifiers ? DMGL_ANSI : 0);
2679
2680 return cplus_demangle(str, flags);
2681 #else
2682 return NULL;
2683 #endif
2684 }
2685 #endif /* !HAVE_CXA_DEMANGLE_SUPPORT */
2686
dso__demangle_sym(struct dso * dso,int kmodule,const char * elf_name)2687 char *dso__demangle_sym(struct dso *dso, int kmodule, const char *elf_name)
2688 {
2689 struct demangle rust_demangle = {
2690 .style = DemangleStyleUnknown,
2691 };
2692 char *demangled = NULL;
2693
2694 /*
2695 * We need to figure out if the object was created from C++ sources
2696 * DWARF DW_compile_unit has this, but we don't always have access
2697 * to it...
2698 */
2699 if (!want_demangle((dso && dso__kernel(dso)) || kmodule))
2700 return demangled;
2701
2702 rust_demangle_demangle(elf_name, &rust_demangle);
2703 if (rust_demangle_is_known(&rust_demangle)) {
2704 /* A rust mangled name. */
2705 if (rust_demangle.mangled_len == 0)
2706 return demangled;
2707
2708 for (size_t buf_len = roundup_pow_of_two(rust_demangle.mangled_len * 2);
2709 buf_len < 1024 * 1024; buf_len += 32) {
2710 char *tmp = realloc(demangled, buf_len);
2711
2712 if (!tmp) {
2713 /* Failure to grow output buffer, return what is there. */
2714 return demangled;
2715 }
2716 demangled = tmp;
2717 if (rust_demangle_display_demangle(&rust_demangle, demangled, buf_len,
2718 /*alternate=*/true) == OverflowOk)
2719 return demangled;
2720 }
2721 /* Buffer exceeded sensible bounds, return what is there. */
2722 return demangled;
2723 }
2724
2725 demangled = cxx_demangle_sym(elf_name, verbose > 0, verbose > 0);
2726 if (demangled)
2727 return demangled;
2728
2729 demangled = ocaml_demangle_sym(elf_name);
2730 if (demangled)
2731 return demangled;
2732
2733 return java_demangle_sym(elf_name, JAVA_DEMANGLE_NORET);
2734 }
2735