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