xref: /linux/tools/perf/util/unwind-libunwind.c (revision b8d312aa075f33282565467662c4628dae0a2aff)
1 // SPDX-License-Identifier: GPL-2.0
2 #include "unwind.h"
3 #include "map.h"
4 #include "thread.h"
5 #include "session.h"
6 #include "debug.h"
7 #include "env.h"
8 #include "callchain.h"
9 
10 struct unwind_libunwind_ops __weak *local_unwind_libunwind_ops;
11 struct unwind_libunwind_ops __weak *x86_32_unwind_libunwind_ops;
12 struct unwind_libunwind_ops __weak *arm64_unwind_libunwind_ops;
13 
14 static void unwind__register_ops(struct thread *thread,
15 			  struct unwind_libunwind_ops *ops)
16 {
17 	thread->unwind_libunwind_ops = ops;
18 }
19 
20 int unwind__prepare_access(struct thread *thread, struct map *map,
21 			   bool *initialized)
22 {
23 	const char *arch;
24 	enum dso_type dso_type;
25 	struct unwind_libunwind_ops *ops = local_unwind_libunwind_ops;
26 	int err;
27 
28 	if (!dwarf_callchain_users)
29 		return 0;
30 
31 	if (thread->addr_space) {
32 		pr_debug("unwind: thread map already set, dso=%s\n",
33 			 map->dso->name);
34 		if (initialized)
35 			*initialized = true;
36 		return 0;
37 	}
38 
39 	/* env->arch is NULL for live-mode (i.e. perf top) */
40 	if (!thread->mg->machine->env || !thread->mg->machine->env->arch)
41 		goto out_register;
42 
43 	dso_type = dso__type(map->dso, thread->mg->machine);
44 	if (dso_type == DSO__TYPE_UNKNOWN)
45 		return 0;
46 
47 	arch = perf_env__arch(thread->mg->machine->env);
48 
49 	if (!strcmp(arch, "x86")) {
50 		if (dso_type != DSO__TYPE_64BIT)
51 			ops = x86_32_unwind_libunwind_ops;
52 	} else if (!strcmp(arch, "arm64") || !strcmp(arch, "arm")) {
53 		if (dso_type == DSO__TYPE_64BIT)
54 			ops = arm64_unwind_libunwind_ops;
55 	}
56 
57 	if (!ops) {
58 		pr_err("unwind: target platform=%s is not supported\n", arch);
59 		return 0;
60 	}
61 out_register:
62 	unwind__register_ops(thread, ops);
63 
64 	err = thread->unwind_libunwind_ops->prepare_access(thread);
65 	if (initialized)
66 		*initialized = err ? false : true;
67 	return err;
68 }
69 
70 void unwind__flush_access(struct thread *thread)
71 {
72 	if (!dwarf_callchain_users)
73 		return;
74 
75 	if (thread->unwind_libunwind_ops)
76 		thread->unwind_libunwind_ops->flush_access(thread);
77 }
78 
79 void unwind__finish_access(struct thread *thread)
80 {
81 	if (!dwarf_callchain_users)
82 		return;
83 
84 	if (thread->unwind_libunwind_ops)
85 		thread->unwind_libunwind_ops->finish_access(thread);
86 }
87 
88 int unwind__get_entries(unwind_entry_cb_t cb, void *arg,
89 			 struct thread *thread,
90 			 struct perf_sample *data, int max_stack)
91 {
92 	if (thread->unwind_libunwind_ops)
93 		return thread->unwind_libunwind_ops->get_entries(cb, arg, thread, data, max_stack);
94 	return 0;
95 }
96