xref: /linux/tools/perf/tests/dwarf-unwind.c (revision d8f797c60661a90ee26ca9330cf85ede9aa2ec17)
1 #include <linux/compiler.h>
2 #include <linux/types.h>
3 #include <inttypes.h>
4 #include <unistd.h>
5 #include "tests.h"
6 #include "debug.h"
7 #include "machine.h"
8 #include "event.h"
9 #include "unwind.h"
10 #include "perf_regs.h"
11 #include "map.h"
12 #include "thread.h"
13 #include "callchain.h"
14 
15 #if defined (__x86_64__) || defined (__i386__) || defined (__powerpc__)
16 #include "arch-tests.h"
17 #endif
18 
19 /* For bsearch. We try to unwind functions in shared object. */
20 #include <stdlib.h>
21 
22 static int mmap_handler(struct perf_tool *tool __maybe_unused,
23 			union perf_event *event,
24 			struct perf_sample *sample,
25 			struct machine *machine)
26 {
27 	return machine__process_mmap2_event(machine, event, sample);
28 }
29 
30 static int init_live_machine(struct machine *machine)
31 {
32 	union perf_event event;
33 	pid_t pid = getpid();
34 
35 	return perf_event__synthesize_mmap_events(NULL, &event, pid, pid,
36 						  mmap_handler, machine, true, 500);
37 }
38 
39 #define MAX_STACK 8
40 
41 static int unwind_entry(struct unwind_entry *entry, void *arg)
42 {
43 	unsigned long *cnt = (unsigned long *) arg;
44 	char *symbol = entry->sym ? entry->sym->name : NULL;
45 	static const char *funcs[MAX_STACK] = {
46 		"test__arch_unwind_sample",
47 		"unwind_thread",
48 		"compare",
49 		"bsearch",
50 		"krava_3",
51 		"krava_2",
52 		"krava_1",
53 		"test__dwarf_unwind"
54 	};
55 	/*
56 	 * The funcs[MAX_STACK] array index, based on the
57 	 * callchain order setup.
58 	 */
59 	int idx = callchain_param.order == ORDER_CALLER ?
60 		  MAX_STACK - *cnt - 1 : *cnt;
61 
62 	if (*cnt >= MAX_STACK) {
63 		pr_debug("failed: crossed the max stack value %d\n", MAX_STACK);
64 		return -1;
65 	}
66 
67 	if (!symbol) {
68 		pr_debug("failed: got unresolved address 0x%" PRIx64 "\n",
69 			 entry->ip);
70 		return -1;
71 	}
72 
73 	(*cnt)++;
74 	pr_debug("got: %s 0x%" PRIx64 ", expecting %s\n",
75 		 symbol, entry->ip, funcs[idx]);
76 	return strcmp((const char *) symbol, funcs[idx]);
77 }
78 
79 __attribute__ ((noinline))
80 static 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 __attribute__ ((noinline))
112 static int compare(void *p1, void *p2)
113 {
114 	/* Any possible value should be 'thread' */
115 	struct thread *thread = *(struct thread **)p1;
116 
117 	if (global_unwind_retval == -INT_MAX) {
118 		/* Call unwinder twice for both callchain orders. */
119 		callchain_param.order = ORDER_CALLER;
120 
121 		global_unwind_retval = unwind_thread(thread);
122 		if (!global_unwind_retval) {
123 			callchain_param.order = ORDER_CALLEE;
124 			global_unwind_retval = unwind_thread(thread);
125 		}
126 	}
127 
128 	return p1 - p2;
129 }
130 
131 __attribute__ ((noinline))
132 static int krava_3(struct thread *thread)
133 {
134 	struct thread *array[2] = {thread, thread};
135 	void *fp = &bsearch;
136 	/*
137 	 * make _bsearch a volatile function pointer to
138 	 * prevent potential optimization, which may expand
139 	 * bsearch and call compare directly from this function,
140 	 * instead of libc shared object.
141 	 */
142 	void *(*volatile _bsearch)(void *, void *, size_t,
143 			size_t, int (*)(void *, void *));
144 
145 	_bsearch = fp;
146 	_bsearch(array, &thread, 2, sizeof(struct thread **), compare);
147 	return global_unwind_retval;
148 }
149 
150 __attribute__ ((noinline))
151 static int krava_2(struct thread *thread)
152 {
153 	return krava_3(thread);
154 }
155 
156 __attribute__ ((noinline))
157 static int krava_1(struct thread *thread)
158 {
159 	return krava_2(thread);
160 }
161 
162 int test__dwarf_unwind(int subtest __maybe_unused)
163 {
164 	struct machine *machine;
165 	struct thread *thread;
166 	int err = -1;
167 
168 	machine = machine__new_host();
169 	if (!machine) {
170 		pr_err("Could not get machine\n");
171 		return -1;
172 	}
173 
174 	if (machine__create_kernel_maps(machine)) {
175 		pr_err("Failed to create kernel maps\n");
176 		return -1;
177 	}
178 
179 	callchain_param.record_mode = CALLCHAIN_DWARF;
180 
181 	if (init_live_machine(machine)) {
182 		pr_err("Could not init machine\n");
183 		goto out;
184 	}
185 
186 	if (verbose > 1)
187 		machine__fprintf(machine, stderr);
188 
189 	thread = machine__find_thread(machine, getpid(), getpid());
190 	if (!thread) {
191 		pr_err("Could not get thread\n");
192 		goto out;
193 	}
194 
195 	err = krava_1(thread);
196 	thread__put(thread);
197 
198  out:
199 	machine__delete_threads(machine);
200 	machine__delete(machine);
201 	return err;
202 }
203