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