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);
41 * We need to keep these functions global, despite the
42 * fact that they are used only locally in this object,
43 * in order to keep them around even if the binary is
44 * stripped. If they are gone, the unwind check for
47 int test_dwarf_unwind__thread(struct thread *thread);
48 int test_dwarf_unwind__compare(void *p1, void *p2);
49 int test_dwarf_unwind__krava_3(struct thread *thread);
50 int test_dwarf_unwind__krava_2(struct thread *thread);
51 int test_dwarf_unwind__krava_1(struct thread *thread);
55 static int unwind_entry(struct unwind_entry *entry, void *arg)
57 unsigned long *cnt = (unsigned long *) arg;
58 char *symbol = entry->sym ? entry->sym->name : NULL;
59 static const char *funcs[MAX_STACK] = {
60 "test__arch_unwind_sample",
61 "test_dwarf_unwind__thread",
62 "test_dwarf_unwind__compare",
64 "test_dwarf_unwind__krava_3",
65 "test_dwarf_unwind__krava_2",
66 "test_dwarf_unwind__krava_1",
70 * The funcs[MAX_STACK] array index, based on the
71 * callchain order setup.
73 int idx = callchain_param.order == ORDER_CALLER ?
74 MAX_STACK - *cnt - 1 : *cnt;
76 if (*cnt >= MAX_STACK) {
77 pr_debug("failed: crossed the max stack value %d\n", MAX_STACK);
82 pr_debug("failed: got unresolved address 0x%" PRIx64 "\n",
88 pr_debug("got: %s 0x%" PRIx64 ", expecting %s\n",
89 symbol, entry->ip, funcs[idx]);
90 return strcmp((const char *) symbol, funcs[idx]);
93 noinline int test_dwarf_unwind__thread(struct thread *thread)
95 struct perf_sample sample;
96 unsigned long cnt = 0;
99 memset(&sample, 0, sizeof(sample));
101 if (test__arch_unwind_sample(&sample, thread)) {
102 pr_debug("failed to get unwind sample\n");
106 err = unwind__get_entries(unwind_entry, &cnt, thread,
109 pr_debug("unwind failed\n");
110 else if (cnt != MAX_STACK) {
111 pr_debug("got wrong number of stack entries %lu != %d\n",
117 free(sample.user_stack.data);
118 free(sample.user_regs.regs);
122 static int global_unwind_retval = -INT_MAX;
124 noinline int test_dwarf_unwind__compare(void *p1, void *p2)
126 /* Any possible value should be 'thread' */
127 struct thread *thread = *(struct thread **)p1;
129 if (global_unwind_retval == -INT_MAX) {
130 /* Call unwinder twice for both callchain orders. */
131 callchain_param.order = ORDER_CALLER;
133 global_unwind_retval = test_dwarf_unwind__thread(thread);
134 if (!global_unwind_retval) {
135 callchain_param.order = ORDER_CALLEE;
136 global_unwind_retval = test_dwarf_unwind__thread(thread);
143 noinline int test_dwarf_unwind__krava_3(struct thread *thread)
145 struct thread *array[2] = {thread, thread};
148 * make _bsearch a volatile function pointer to
149 * prevent potential optimization, which may expand
150 * bsearch and call compare directly from this function,
151 * instead of libc shared object.
153 void *(*volatile _bsearch)(void *, void *, size_t,
154 size_t, int (*)(void *, void *));
157 _bsearch(array, &thread, 2, sizeof(struct thread **),
158 test_dwarf_unwind__compare);
159 return global_unwind_retval;
162 noinline int test_dwarf_unwind__krava_2(struct thread *thread)
164 return test_dwarf_unwind__krava_3(thread);
167 noinline int test_dwarf_unwind__krava_1(struct thread *thread)
169 return test_dwarf_unwind__krava_2(thread);
172 int test__dwarf_unwind(struct test *test __maybe_unused, int subtest __maybe_unused)
174 struct machine *machine;
175 struct thread *thread;
178 machine = machine__new_host();
180 pr_err("Could not get machine\n");
184 if (machine__create_kernel_maps(machine)) {
185 pr_err("Failed to create kernel maps\n");
189 callchain_param.record_mode = CALLCHAIN_DWARF;
190 dwarf_callchain_users = true;
192 if (init_live_machine(machine)) {
193 pr_err("Could not init machine\n");
198 machine__fprintf(machine, stderr);
200 thread = machine__find_thread(machine, getpid(), getpid());
202 pr_err("Could not get thread\n");
206 err = test_dwarf_unwind__krava_1(thread);
210 machine__delete_threads(machine);
211 machine__delete(machine);