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 /* For bsearch. We try to unwind functions in shared object. */
24 * The test will assert frames are on the stack but tail call optimizations lose
25 * the frame of the caller. Clang can disable this optimization on a called
26 * function but GCC currently (11/2020) lacks this attribute. The barrier is
27 * used to inhibit tail calls in these cases.
29 #ifdef __has_attribute
30 #if __has_attribute(disable_tail_calls)
31 #define NO_TAIL_CALL_ATTRIBUTE __attribute__((disable_tail_calls))
32 #define NO_TAIL_CALL_BARRIER
35 #ifndef NO_TAIL_CALL_ATTRIBUTE
36 #define NO_TAIL_CALL_ATTRIBUTE
37 #define NO_TAIL_CALL_BARRIER __asm__ __volatile__("" : : : "memory");
40 static int mmap_handler(struct perf_tool *tool __maybe_unused,
41 union perf_event *event,
42 struct perf_sample *sample,
43 struct machine *machine)
45 return machine__process_mmap2_event(machine, event, sample);
48 static int init_live_machine(struct machine *machine)
50 union perf_event event;
53 memset(&event, 0, sizeof(event));
54 return perf_event__synthesize_mmap_events(NULL, &event, pid, pid,
55 mmap_handler, machine, true);
59 * We need to keep these functions global, despite the
60 * fact that they are used only locally in this object,
61 * in order to keep them around even if the binary is
62 * stripped. If they are gone, the unwind check for
65 int test_dwarf_unwind__thread(struct thread *thread);
66 int test_dwarf_unwind__compare(void *p1, void *p2);
67 int test_dwarf_unwind__krava_3(struct thread *thread);
68 int test_dwarf_unwind__krava_2(struct thread *thread);
69 int test_dwarf_unwind__krava_1(struct thread *thread);
70 int test__dwarf_unwind(struct test_suite *test, int subtest);
74 static int unwind_entry(struct unwind_entry *entry, void *arg)
76 unsigned long *cnt = (unsigned long *) arg;
77 char *symbol = entry->ms.sym ? entry->ms.sym->name : NULL;
78 static const char *funcs[MAX_STACK] = {
79 "test__arch_unwind_sample",
80 "test_dwarf_unwind__thread",
81 "test_dwarf_unwind__compare",
83 "test_dwarf_unwind__krava_3",
84 "test_dwarf_unwind__krava_2",
85 "test_dwarf_unwind__krava_1",
89 * The funcs[MAX_STACK] array index, based on the
90 * callchain order setup.
92 int idx = callchain_param.order == ORDER_CALLER ?
93 MAX_STACK - *cnt - 1 : *cnt;
95 if (*cnt >= MAX_STACK) {
96 pr_debug("failed: crossed the max stack value %d\n", MAX_STACK);
101 pr_debug("failed: got unresolved address 0x%" PRIx64 "\n",
107 pr_debug("got: %s 0x%" PRIx64 ", expecting %s\n",
108 symbol, entry->ip, funcs[idx]);
109 return strcmp((const char *) symbol, funcs[idx]);
112 NO_TAIL_CALL_ATTRIBUTE noinline int test_dwarf_unwind__thread(struct thread *thread)
114 struct perf_sample sample;
115 unsigned long cnt = 0;
118 memset(&sample, 0, sizeof(sample));
120 if (test__arch_unwind_sample(&sample, thread)) {
121 pr_debug("failed to get unwind sample\n");
125 err = unwind__get_entries(unwind_entry, &cnt, thread,
126 &sample, MAX_STACK, false);
128 pr_debug("unwind failed\n");
129 else if (cnt != MAX_STACK) {
130 pr_debug("got wrong number of stack entries %lu != %d\n",
136 zfree(&sample.user_stack.data);
137 zfree(&sample.user_regs.regs);
141 static int global_unwind_retval = -INT_MAX;
143 NO_TAIL_CALL_ATTRIBUTE noinline int test_dwarf_unwind__compare(void *p1, void *p2)
145 /* Any possible value should be 'thread' */
146 struct thread *thread = *(struct thread **)p1;
148 if (global_unwind_retval == -INT_MAX) {
149 /* Call unwinder twice for both callchain orders. */
150 callchain_param.order = ORDER_CALLER;
152 global_unwind_retval = test_dwarf_unwind__thread(thread);
153 if (!global_unwind_retval) {
154 callchain_param.order = ORDER_CALLEE;
155 global_unwind_retval = test_dwarf_unwind__thread(thread);
162 NO_TAIL_CALL_ATTRIBUTE noinline int test_dwarf_unwind__krava_3(struct thread *thread)
164 struct thread *array[2] = {thread, thread};
167 * make _bsearch a volatile function pointer to
168 * prevent potential optimization, which may expand
169 * bsearch and call compare directly from this function,
170 * instead of libc shared object.
172 void *(*volatile _bsearch)(void *, void *, size_t,
173 size_t, int (*)(void *, void *));
176 _bsearch(array, &thread, 2, sizeof(struct thread **),
177 test_dwarf_unwind__compare);
178 return global_unwind_retval;
181 NO_TAIL_CALL_ATTRIBUTE noinline int test_dwarf_unwind__krava_2(struct thread *thread)
185 ret = test_dwarf_unwind__krava_3(thread);
186 NO_TAIL_CALL_BARRIER;
190 NO_TAIL_CALL_ATTRIBUTE noinline int test_dwarf_unwind__krava_1(struct thread *thread)
194 ret = test_dwarf_unwind__krava_2(thread);
195 NO_TAIL_CALL_BARRIER;
199 noinline int test__dwarf_unwind(struct test_suite *test __maybe_unused,
200 int subtest __maybe_unused)
202 struct machine *machine;
203 struct thread *thread;
206 machine = machine__new_host();
208 pr_err("Could not get machine\n");
212 if (machine__create_kernel_maps(machine)) {
213 pr_err("Failed to create kernel maps\n");
217 callchain_param.record_mode = CALLCHAIN_DWARF;
218 dwarf_callchain_users = true;
220 if (init_live_machine(machine)) {
221 pr_err("Could not init machine\n");
226 machine__fprintf(machine, stderr);
228 thread = machine__find_thread(machine, getpid(), getpid());
230 pr_err("Could not get thread\n");
234 err = test_dwarf_unwind__krava_1(thread);
238 machine__delete(machine);
242 DEFINE_SUITE("Test dwarf unwind", dwarf_unwind);