1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/compiler.h>
3 #include <linux/types.h>
10 #include "../util/unwind.h"
11 #include "perf_regs.h"
14 #include "callchain.h"
16 #if defined (__x86_64__) || defined (__i386__) || defined (__powerpc__)
17 #include "arch-tests.h"
20 /* For bsearch. We try to unwind functions in shared object. */
23 static int mmap_handler(struct perf_tool
*tool __maybe_unused
,
24 union perf_event
*event
,
25 struct perf_sample
*sample
,
26 struct machine
*machine
)
28 return machine__process_mmap2_event(machine
, event
, sample
);
31 static int init_live_machine(struct machine
*machine
)
33 union perf_event event
;
36 return perf_event__synthesize_mmap_events(NULL
, &event
, pid
, pid
,
37 mmap_handler
, machine
, true, 500);
42 static int unwind_entry(struct unwind_entry
*entry
, void *arg
)
44 unsigned long *cnt
= (unsigned long *) arg
;
45 char *symbol
= entry
->sym
? entry
->sym
->name
: NULL
;
46 static const char *funcs
[MAX_STACK
] = {
47 "test__arch_unwind_sample",
57 * The funcs[MAX_STACK] array index, based on the
58 * callchain order setup.
60 int idx
= callchain_param
.order
== ORDER_CALLER
?
61 MAX_STACK
- *cnt
- 1 : *cnt
;
63 if (*cnt
>= MAX_STACK
) {
64 pr_debug("failed: crossed the max stack value %d\n", MAX_STACK
);
69 pr_debug("failed: got unresolved address 0x%" PRIx64
"\n",
75 pr_debug("got: %s 0x%" PRIx64
", expecting %s\n",
76 symbol
, entry
->ip
, funcs
[idx
]);
77 return strcmp((const char *) symbol
, funcs
[idx
]);
80 static noinline
int unwind_thread(struct thread
*thread
)
82 struct perf_sample sample
;
83 unsigned long cnt
= 0;
86 memset(&sample
, 0, sizeof(sample
));
88 if (test__arch_unwind_sample(&sample
, thread
)) {
89 pr_debug("failed to get unwind sample\n");
93 err
= unwind__get_entries(unwind_entry
, &cnt
, thread
,
96 pr_debug("unwind failed\n");
97 else if (cnt
!= MAX_STACK
) {
98 pr_debug("got wrong number of stack entries %lu != %d\n",
104 free(sample
.user_stack
.data
);
105 free(sample
.user_regs
.regs
);
109 static int global_unwind_retval
= -INT_MAX
;
111 static noinline
int compare(void *p1
, void *p2
)
113 /* Any possible value should be 'thread' */
114 struct thread
*thread
= *(struct thread
**)p1
;
116 if (global_unwind_retval
== -INT_MAX
) {
117 /* Call unwinder twice for both callchain orders. */
118 callchain_param
.order
= ORDER_CALLER
;
120 global_unwind_retval
= unwind_thread(thread
);
121 if (!global_unwind_retval
) {
122 callchain_param
.order
= ORDER_CALLEE
;
123 global_unwind_retval
= unwind_thread(thread
);
130 static noinline
int krava_3(struct thread
*thread
)
132 struct thread
*array
[2] = {thread
, thread
};
135 * make _bsearch a volatile function pointer to
136 * prevent potential optimization, which may expand
137 * bsearch and call compare directly from this function,
138 * instead of libc shared object.
140 void *(*volatile _bsearch
)(void *, void *, size_t,
141 size_t, int (*)(void *, void *));
144 _bsearch(array
, &thread
, 2, sizeof(struct thread
**), compare
);
145 return global_unwind_retval
;
148 static noinline
int krava_2(struct thread
*thread
)
150 return krava_3(thread
);
153 static noinline
int krava_1(struct thread
*thread
)
155 return krava_2(thread
);
158 int test__dwarf_unwind(struct test
*test __maybe_unused
, int subtest __maybe_unused
)
160 struct machine
*machine
;
161 struct thread
*thread
;
164 machine
= machine__new_host();
166 pr_err("Could not get machine\n");
170 if (machine__create_kernel_maps(machine
)) {
171 pr_err("Failed to create kernel maps\n");
175 callchain_param
.record_mode
= CALLCHAIN_DWARF
;
176 dwarf_callchain_users
= true;
178 if (init_live_machine(machine
)) {
179 pr_err("Could not init machine\n");
184 machine__fprintf(machine
, stderr
);
186 thread
= machine__find_thread(machine
, getpid(), getpid());
188 pr_err("Could not get thread\n");
192 err
= krava_1(thread
);
196 machine__delete_threads(machine
);
197 machine__delete(machine
);