blob: e89a5479b361303e8b323182dd5509a18e5ead1b [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001// SPDX-License-Identifier: GPL-2.0
2#include "unwind.h"
David Brazdil0f672f62019-12-10 10:32:29 +00003#include "dso.h"
4#include "map.h"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005#include "thread.h"
6#include "session.h"
7#include "debug.h"
8#include "env.h"
David Brazdil0f672f62019-12-10 10:32:29 +00009#include "callchain.h"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000010
11struct unwind_libunwind_ops __weak *local_unwind_libunwind_ops;
12struct unwind_libunwind_ops __weak *x86_32_unwind_libunwind_ops;
13struct unwind_libunwind_ops __weak *arm64_unwind_libunwind_ops;
14
Olivier Deprez157378f2022-04-04 15:47:50 +020015static void unwind__register_ops(struct maps *maps, struct unwind_libunwind_ops *ops)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000016{
Olivier Deprez157378f2022-04-04 15:47:50 +020017 maps->unwind_libunwind_ops = ops;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000018}
19
Olivier Deprez157378f2022-04-04 15:47:50 +020020int unwind__prepare_access(struct maps *maps, struct map *map, bool *initialized)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000021{
22 const char *arch;
23 enum dso_type dso_type;
24 struct unwind_libunwind_ops *ops = local_unwind_libunwind_ops;
25 int err;
26
David Brazdil0f672f62019-12-10 10:32:29 +000027 if (!dwarf_callchain_users)
28 return 0;
29
Olivier Deprez157378f2022-04-04 15:47:50 +020030 if (maps->addr_space) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000031 pr_debug("unwind: thread map already set, dso=%s\n",
32 map->dso->name);
33 if (initialized)
34 *initialized = true;
35 return 0;
36 }
37
38 /* env->arch is NULL for live-mode (i.e. perf top) */
Olivier Deprez157378f2022-04-04 15:47:50 +020039 if (!maps->machine->env || !maps->machine->env->arch)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000040 goto out_register;
41
Olivier Deprez157378f2022-04-04 15:47:50 +020042 dso_type = dso__type(map->dso, maps->machine);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000043 if (dso_type == DSO__TYPE_UNKNOWN)
44 return 0;
45
Olivier Deprez157378f2022-04-04 15:47:50 +020046 arch = perf_env__arch(maps->machine->env);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000047
48 if (!strcmp(arch, "x86")) {
49 if (dso_type != DSO__TYPE_64BIT)
50 ops = x86_32_unwind_libunwind_ops;
51 } else if (!strcmp(arch, "arm64") || !strcmp(arch, "arm")) {
52 if (dso_type == DSO__TYPE_64BIT)
53 ops = arm64_unwind_libunwind_ops;
54 }
55
56 if (!ops) {
57 pr_err("unwind: target platform=%s is not supported\n", arch);
58 return 0;
59 }
60out_register:
Olivier Deprez157378f2022-04-04 15:47:50 +020061 unwind__register_ops(maps, ops);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000062
Olivier Deprez157378f2022-04-04 15:47:50 +020063 err = maps->unwind_libunwind_ops->prepare_access(maps);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000064 if (initialized)
65 *initialized = err ? false : true;
66 return err;
67}
68
Olivier Deprez157378f2022-04-04 15:47:50 +020069void unwind__flush_access(struct maps *maps)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000070{
Olivier Deprez157378f2022-04-04 15:47:50 +020071 if (maps->unwind_libunwind_ops)
72 maps->unwind_libunwind_ops->flush_access(maps);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000073}
74
Olivier Deprez157378f2022-04-04 15:47:50 +020075void unwind__finish_access(struct maps *maps)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000076{
Olivier Deprez157378f2022-04-04 15:47:50 +020077 if (maps->unwind_libunwind_ops)
78 maps->unwind_libunwind_ops->finish_access(maps);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000079}
80
81int unwind__get_entries(unwind_entry_cb_t cb, void *arg,
82 struct thread *thread,
83 struct perf_sample *data, int max_stack)
84{
Olivier Deprez157378f2022-04-04 15:47:50 +020085 if (thread->maps->unwind_libunwind_ops)
86 return thread->maps->unwind_libunwind_ops->get_entries(cb, arg, thread, data, max_stack);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000087 return 0;
88}