xref: /linux/tools/perf/tests/dwarf-unwind.c (revision e78f70bad29c5ae1e1076698b690b15794e9b81e)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/compiler.h>
3 #include <linux/types.h>
4 #include <linux/zalloc.h>
5 #include <inttypes.h>
6 #include <limits.h>
7 #include <unistd.h>
8 #include "tests.h"
9 #include "debug.h"
10 #include "machine.h"
11 #include "event.h"
12 #include "../util/unwind.h"
13 #include "perf_regs.h"
14 #include "map.h"
15 #include "symbol.h"
16 #include "thread.h"
17 #include "callchain.h"
18 
19 /* For bsearch. We try to unwind functions in shared object. */
20 #include <stdlib.h>
21 
22 /*
23  * The test will assert frames are on the stack but tail call optimizations lose
24  * the frame of the caller. Clang can disable this optimization on a called
25  * function but GCC currently (11/2020) lacks this attribute. The barrier is
26  * used to inhibit tail calls in these cases.
27  */
28 #ifdef __has_attribute
29 #if __has_attribute(disable_tail_calls)
30 #define NO_TAIL_CALL_ATTRIBUTE __attribute__((disable_tail_calls))
31 #define NO_TAIL_CALL_BARRIER
32 #endif
33 #endif
34 #ifndef NO_TAIL_CALL_ATTRIBUTE
35 #define NO_TAIL_CALL_ATTRIBUTE
36 #define NO_TAIL_CALL_BARRIER __asm__ __volatile__("" : : : "memory");
37 #endif
38 
39 /*
40  * We need to keep these functions global, despite the
41  * fact that they are used only locally in this object,
42  * in order to keep them around even if the binary is
43  * stripped. If they are gone, the unwind check for
44  * symbol fails.
45  */
46 int test_dwarf_unwind__thread(struct thread *thread);
47 int test_dwarf_unwind__compare(void *p1, void *p2);
48 int test_dwarf_unwind__krava_3(struct thread *thread);
49 int test_dwarf_unwind__krava_2(struct thread *thread);
50 int test_dwarf_unwind__krava_1(struct thread *thread);
51 int test__dwarf_unwind(struct test_suite *test, int subtest);
52 
53 #define MAX_STACK 8
54 
55 static int unwind_entry(struct unwind_entry *entry, void *arg)
56 {
57 	unsigned long *cnt = (unsigned long *) arg;
58 	char *symbol = entry->ms.sym ? entry->ms.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",
63 		"bsearch",
64 		"test_dwarf_unwind__krava_3",
65 		"test_dwarf_unwind__krava_2",
66 		"test_dwarf_unwind__krava_1",
67 		"test__dwarf_unwind"
68 	};
69 	/*
70 	 * The funcs[MAX_STACK] array index, based on the
71 	 * callchain order setup.
72 	 */
73 	int idx = callchain_param.order == ORDER_CALLER ?
74 		  MAX_STACK - *cnt - 1 : *cnt;
75 
76 	if (*cnt >= MAX_STACK) {
77 		pr_debug("failed: crossed the max stack value %d\n", MAX_STACK);
78 		return -1;
79 	}
80 
81 	if (!symbol) {
82 		pr_debug("failed: got unresolved address 0x%" PRIx64 "\n",
83 			 entry->ip);
84 		return -1;
85 	}
86 
87 	(*cnt)++;
88 	pr_debug("got: %s 0x%" PRIx64 ", expecting %s\n",
89 		 symbol, entry->ip, funcs[idx]);
90 	return strcmp((const char *) symbol, funcs[idx]);
91 }
92 
93 NO_TAIL_CALL_ATTRIBUTE noinline int test_dwarf_unwind__thread(struct thread *thread)
94 {
95 	struct perf_sample sample;
96 	unsigned long cnt = 0;
97 	int err = -1;
98 
99 	perf_sample__init(&sample, /*all=*/true);
100 	if (test__arch_unwind_sample(&sample, thread)) {
101 		pr_debug("failed to get unwind sample\n");
102 		goto out;
103 	}
104 
105 	err = unwind__get_entries(unwind_entry, &cnt, thread,
106 				  &sample, MAX_STACK, false);
107 	if (err)
108 		pr_debug("unwind failed\n");
109 	else if (cnt != MAX_STACK) {
110 		pr_debug("got wrong number of stack entries %lu != %d\n",
111 			 cnt, MAX_STACK);
112 		err = -1;
113 	}
114 
115  out:
116 	zfree(&sample.user_stack.data);
117 	zfree(&sample.user_regs->regs);
118 	perf_sample__exit(&sample);
119 	return err;
120 }
121 
122 static int global_unwind_retval = -INT_MAX;
123 
124 NO_TAIL_CALL_ATTRIBUTE noinline int test_dwarf_unwind__compare(void *p1, void *p2)
125 {
126 	/* Any possible value should be 'thread' */
127 	struct thread *thread = *(struct thread **)p1;
128 
129 	if (global_unwind_retval == -INT_MAX) {
130 		/* Call unwinder twice for both callchain orders. */
131 		callchain_param.order = ORDER_CALLER;
132 
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);
137 		}
138 	}
139 
140 	return p1 - p2;
141 }
142 
143 NO_TAIL_CALL_ATTRIBUTE noinline int test_dwarf_unwind__krava_3(struct thread *thread)
144 {
145 	struct thread *array[2] = {thread, thread};
146 	void *fp = &bsearch;
147 	/*
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.
152 	 */
153 	void *(*volatile _bsearch)(void *, void *, size_t,
154 			size_t, int (*)(void *, void *));
155 
156 	_bsearch = fp;
157 	_bsearch(array, &thread, 2, sizeof(struct thread **),
158 		 test_dwarf_unwind__compare);
159 	return global_unwind_retval;
160 }
161 
162 NO_TAIL_CALL_ATTRIBUTE noinline int test_dwarf_unwind__krava_2(struct thread *thread)
163 {
164 	int ret;
165 
166 	ret =  test_dwarf_unwind__krava_3(thread);
167 	NO_TAIL_CALL_BARRIER;
168 	return ret;
169 }
170 
171 NO_TAIL_CALL_ATTRIBUTE noinline int test_dwarf_unwind__krava_1(struct thread *thread)
172 {
173 	int ret;
174 
175 	ret =  test_dwarf_unwind__krava_2(thread);
176 	NO_TAIL_CALL_BARRIER;
177 	return ret;
178 }
179 
180 noinline int test__dwarf_unwind(struct test_suite *test __maybe_unused,
181 				int subtest __maybe_unused)
182 {
183 	struct machine *machine;
184 	struct thread *thread;
185 	int err = -1;
186 	pid_t pid = getpid();
187 
188 	callchain_param.record_mode = CALLCHAIN_DWARF;
189 	dwarf_callchain_users = true;
190 
191 	machine = machine__new_live(/*kernel_maps=*/true, pid);
192 	if (!machine) {
193 		pr_err("Could not get machine\n");
194 		return -1;
195 	}
196 
197 	if (machine__create_kernel_maps(machine)) {
198 		pr_err("Failed to create kernel maps\n");
199 		return -1;
200 	}
201 
202 	if (verbose > 1)
203 		machine__fprintf(machine, stderr);
204 
205 	thread = machine__find_thread(machine, pid, pid);
206 	if (!thread) {
207 		pr_err("Could not get thread\n");
208 		goto out;
209 	}
210 
211 	err = test_dwarf_unwind__krava_1(thread);
212 	thread__put(thread);
213 
214  out:
215 	machine__delete(machine);
216 	return err;
217 }
218 
219 DEFINE_SUITE("Test dwarf unwind", dwarf_unwind);
220