Linux 5.1.15
[linux/fpc-iii.git] / tools / perf / util / unwind-libunwind.c
blob9778b3133b7780b0ec49918d2c83d54d43bf6415
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"
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;
13 static void unwind__register_ops(struct thread *thread,
14 struct unwind_libunwind_ops *ops)
16 thread->unwind_libunwind_ops = ops;
19 int unwind__prepare_access(struct thread *thread, struct map *map,
20 bool *initialized)
22 const char *arch;
23 enum dso_type dso_type;
24 struct unwind_libunwind_ops *ops = local_unwind_libunwind_ops;
25 int err;
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;
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;
39 dso_type = dso__type(map->dso, thread->mg->machine);
40 if (dso_type == DSO__TYPE_UNKNOWN)
41 return 0;
43 arch = perf_env__arch(thread->mg->machine->env);
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;
53 if (!ops) {
54 pr_err("unwind: target platform=%s is not supported\n", arch);
55 return 0;
57 out_register:
58 unwind__register_ops(thread, ops);
60 err = thread->unwind_libunwind_ops->prepare_access(thread);
61 if (initialized)
62 *initialized = err ? false : true;
63 return err;
66 void unwind__flush_access(struct thread *thread)
68 if (thread->unwind_libunwind_ops)
69 thread->unwind_libunwind_ops->flush_access(thread);
72 void unwind__finish_access(struct thread *thread)
74 if (thread->unwind_libunwind_ops)
75 thread->unwind_libunwind_ops->finish_access(thread);
78 int unwind__get_entries(unwind_entry_cb_t cb, void *arg,
79 struct thread *thread,
80 struct perf_sample *data, int max_stack)
82 if (thread->unwind_libunwind_ops)
83 return thread->unwind_libunwind_ops->get_entries(cb, arg, thread, data, max_stack);
84 return 0;