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