1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/compiler.h>
3 #include <linux/types.h>
4 #include <linux/zalloc.h>
12 #include "../util/unwind.h"
13 #include "perf_regs.h"
17 #include "callchain.h"
18 #include "util/synthetic-events.h"
20 #if defined (__x86_64__) || defined (__i386__) || defined (__powerpc__)
21 #include "arch-tests.h"
24 /* For bsearch. We try to unwind functions in shared object. */
27 static int mmap_handler(struct perf_tool *tool __maybe_unused,
28 union perf_event *event,
29 struct perf_sample *sample,
30 struct machine *machine)
32 return machine__process_mmap2_event(machine, event, sample);
35 static int init_live_machine(struct machine *machine)
37 union perf_event event;
40 return perf_event__synthesize_mmap_events(NULL, &event, pid, pid,
41 mmap_handler, machine, true);
45 * We need to keep these functions global, despite the
46 * fact that they are used only locally in this object,
47 * in order to keep them around even if the binary is
48 * stripped. If they are gone, the unwind check for
51 int test_dwarf_unwind__thread(struct thread *thread);
52 int test_dwarf_unwind__compare(void *p1, void *p2);
53 int test_dwarf_unwind__krava_3(struct thread *thread);
54 int test_dwarf_unwind__krava_2(struct thread *thread);
55 int test_dwarf_unwind__krava_1(struct thread *thread);
59 static int unwind_entry(struct unwind_entry *entry, void *arg)
61 unsigned long *cnt = (unsigned long *) arg;
62 char *symbol = entry->sym ? entry->sym->name : NULL;
63 static const char *funcs[MAX_STACK] = {
64 "test__arch_unwind_sample",
65 "test_dwarf_unwind__thread",
66 "test_dwarf_unwind__compare",
68 "test_dwarf_unwind__krava_3",
69 "test_dwarf_unwind__krava_2",
70 "test_dwarf_unwind__krava_1",
74 * The funcs[MAX_STACK] array index, based on the
75 * callchain order setup.
77 int idx = callchain_param.order == ORDER_CALLER ?
78 MAX_STACK - *cnt - 1 : *cnt;
80 if (*cnt >= MAX_STACK) {
81 pr_debug("failed: crossed the max stack value %d\n", MAX_STACK);
86 pr_debug("failed: got unresolved address 0x%" PRIx64 "\n",
92 pr_debug("got: %s 0x%" PRIx64 ", expecting %s\n",
93 symbol, entry->ip, funcs[idx]);
94 return strcmp((const char *) symbol, funcs[idx]);
97 noinline int test_dwarf_unwind__thread(struct thread *thread)
99 struct perf_sample sample;
100 unsigned long cnt = 0;
103 memset(&sample, 0, sizeof(sample));
105 if (test__arch_unwind_sample(&sample, thread)) {
106 pr_debug("failed to get unwind sample\n");
110 err = unwind__get_entries(unwind_entry, &cnt, thread,
113 pr_debug("unwind failed\n");
114 else if (cnt != MAX_STACK) {
115 pr_debug("got wrong number of stack entries %lu != %d\n",
121 zfree(&sample.user_stack.data);
122 zfree(&sample.user_regs.regs);
126 static int global_unwind_retval = -INT_MAX;
128 noinline int test_dwarf_unwind__compare(void *p1, void *p2)
130 /* Any possible value should be 'thread' */
131 struct thread *thread = *(struct thread **)p1;
133 if (global_unwind_retval == -INT_MAX) {
134 /* Call unwinder twice for both callchain orders. */
135 callchain_param.order = ORDER_CALLER;
137 global_unwind_retval = test_dwarf_unwind__thread(thread);
138 if (!global_unwind_retval) {
139 callchain_param.order = ORDER_CALLEE;
140 global_unwind_retval = test_dwarf_unwind__thread(thread);
147 noinline int test_dwarf_unwind__krava_3(struct thread *thread)
149 struct thread *array[2] = {thread, thread};
152 * make _bsearch a volatile function pointer to
153 * prevent potential optimization, which may expand
154 * bsearch and call compare directly from this function,
155 * instead of libc shared object.
157 void *(*volatile _bsearch)(void *, void *, size_t,
158 size_t, int (*)(void *, void *));
161 _bsearch(array, &thread, 2, sizeof(struct thread **),
162 test_dwarf_unwind__compare);
163 return global_unwind_retval;
166 noinline int test_dwarf_unwind__krava_2(struct thread *thread)
168 return test_dwarf_unwind__krava_3(thread);
171 noinline int test_dwarf_unwind__krava_1(struct thread *thread)
173 return test_dwarf_unwind__krava_2(thread);
176 int test__dwarf_unwind(struct test *test __maybe_unused, int subtest __maybe_unused)
178 struct machine *machine;
179 struct thread *thread;
182 machine = machine__new_host();
184 pr_err("Could not get machine\n");
188 if (machine__create_kernel_maps(machine)) {
189 pr_err("Failed to create kernel maps\n");
193 callchain_param.record_mode = CALLCHAIN_DWARF;
194 dwarf_callchain_users = true;
196 if (init_live_machine(machine)) {
197 pr_err("Could not init machine\n");
202 machine__fprintf(machine, stderr);
204 thread = machine__find_thread(machine, getpid(), getpid());
206 pr_err("Could not get thread\n");
210 err = test_dwarf_unwind__krava_1(thread);
214 machine__delete_threads(machine);
215 machine__delete(machine);