xref: /openbmc/linux/tools/perf/tests/dwarf-unwind.c (revision feac8c8b)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/compiler.h>
3 #include <linux/types.h>
4 #include <inttypes.h>
5 #include <unistd.h>
6 #include "tests.h"
7 #include "debug.h"
8 #include "machine.h"
9 #include "event.h"
10 #include "../util/unwind.h"
11 #include "perf_regs.h"
12 #include "map.h"
13 #include "thread.h"
14 #include "callchain.h"
15 
16 #if defined (__x86_64__) || defined (__i386__) || defined (__powerpc__)
17 #include "arch-tests.h"
18 #endif
19 
20 /* For bsearch. We try to unwind functions in shared object. */
21 #include <stdlib.h>
22 
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)
27 {
28 	return machine__process_mmap2_event(machine, event, sample);
29 }
30 
31 static int init_live_machine(struct machine *machine)
32 {
33 	union perf_event event;
34 	pid_t pid = getpid();
35 
36 	return perf_event__synthesize_mmap_events(NULL, &event, pid, pid,
37 						  mmap_handler, machine, true, 500);
38 }
39 
40 #define MAX_STACK 8
41 
42 static int unwind_entry(struct unwind_entry *entry, void *arg)
43 {
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",
48 		"unwind_thread",
49 		"compare",
50 		"bsearch",
51 		"krava_3",
52 		"krava_2",
53 		"krava_1",
54 		"test__dwarf_unwind"
55 	};
56 	/*
57 	 * The funcs[MAX_STACK] array index, based on the
58 	 * callchain order setup.
59 	 */
60 	int idx = callchain_param.order == ORDER_CALLER ?
61 		  MAX_STACK - *cnt - 1 : *cnt;
62 
63 	if (*cnt >= MAX_STACK) {
64 		pr_debug("failed: crossed the max stack value %d\n", MAX_STACK);
65 		return -1;
66 	}
67 
68 	if (!symbol) {
69 		pr_debug("failed: got unresolved address 0x%" PRIx64 "\n",
70 			 entry->ip);
71 		return -1;
72 	}
73 
74 	(*cnt)++;
75 	pr_debug("got: %s 0x%" PRIx64 ", expecting %s\n",
76 		 symbol, entry->ip, funcs[idx]);
77 	return strcmp((const char *) symbol, funcs[idx]);
78 }
79 
80 static noinline int unwind_thread(struct thread *thread)
81 {
82 	struct perf_sample sample;
83 	unsigned long cnt = 0;
84 	int err = -1;
85 
86 	memset(&sample, 0, sizeof(sample));
87 
88 	if (test__arch_unwind_sample(&sample, thread)) {
89 		pr_debug("failed to get unwind sample\n");
90 		goto out;
91 	}
92 
93 	err = unwind__get_entries(unwind_entry, &cnt, thread,
94 				  &sample, MAX_STACK);
95 	if (err)
96 		pr_debug("unwind failed\n");
97 	else if (cnt != MAX_STACK) {
98 		pr_debug("got wrong number of stack entries %lu != %d\n",
99 			 cnt, MAX_STACK);
100 		err = -1;
101 	}
102 
103  out:
104 	free(sample.user_stack.data);
105 	free(sample.user_regs.regs);
106 	return err;
107 }
108 
109 static int global_unwind_retval = -INT_MAX;
110 
111 static noinline int compare(void *p1, void *p2)
112 {
113 	/* Any possible value should be 'thread' */
114 	struct thread *thread = *(struct thread **)p1;
115 
116 	if (global_unwind_retval == -INT_MAX) {
117 		/* Call unwinder twice for both callchain orders. */
118 		callchain_param.order = ORDER_CALLER;
119 
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);
124 		}
125 	}
126 
127 	return p1 - p2;
128 }
129 
130 static noinline int krava_3(struct thread *thread)
131 {
132 	struct thread *array[2] = {thread, thread};
133 	void *fp = &bsearch;
134 	/*
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.
139 	 */
140 	void *(*volatile _bsearch)(void *, void *, size_t,
141 			size_t, int (*)(void *, void *));
142 
143 	_bsearch = fp;
144 	_bsearch(array, &thread, 2, sizeof(struct thread **), compare);
145 	return global_unwind_retval;
146 }
147 
148 static noinline int krava_2(struct thread *thread)
149 {
150 	return krava_3(thread);
151 }
152 
153 static noinline int krava_1(struct thread *thread)
154 {
155 	return krava_2(thread);
156 }
157 
158 int test__dwarf_unwind(struct test *test __maybe_unused, int subtest __maybe_unused)
159 {
160 	struct machine *machine;
161 	struct thread *thread;
162 	int err = -1;
163 
164 	machine = machine__new_host();
165 	if (!machine) {
166 		pr_err("Could not get machine\n");
167 		return -1;
168 	}
169 
170 	if (machine__create_kernel_maps(machine)) {
171 		pr_err("Failed to create kernel maps\n");
172 		return -1;
173 	}
174 
175 	callchain_param.record_mode = CALLCHAIN_DWARF;
176 	dwarf_callchain_users = true;
177 
178 	if (init_live_machine(machine)) {
179 		pr_err("Could not init machine\n");
180 		goto out;
181 	}
182 
183 	if (verbose > 1)
184 		machine__fprintf(machine, stderr);
185 
186 	thread = machine__find_thread(machine, getpid(), getpid());
187 	if (!thread) {
188 		pr_err("Could not get thread\n");
189 		goto out;
190 	}
191 
192 	err = krava_1(thread);
193 	thread__put(thread);
194 
195  out:
196 	machine__delete_threads(machine);
197 	machine__delete(machine);
198 	return err;
199 }
200