1 #include <linux/compiler.h>
2 #include <linux/types.h>
3 #include <unistd.h>
4 #include "tests.h"
5 #include "debug.h"
6 #include "machine.h"
7 #include "event.h"
8 #include "unwind.h"
9 #include "perf_regs.h"
10 #include "map.h"
11 #include "thread.h"
12 #include "callchain.h"
13
14 #if defined (__x86_64__) || defined (__i386__)
15 #include "arch-tests.h"
16 #endif
17
18 /* For bsearch. We try to unwind functions in shared object. */
19 #include <stdlib.h>
20
mmap_handler(struct perf_tool * tool __maybe_unused,union perf_event * event,struct perf_sample * sample __maybe_unused,struct machine * machine)21 static int mmap_handler(struct perf_tool *tool __maybe_unused,
22 union perf_event *event,
23 struct perf_sample *sample __maybe_unused,
24 struct machine *machine)
25 {
26 return machine__process_mmap2_event(machine, event, NULL);
27 }
28
init_live_machine(struct machine * machine)29 static int init_live_machine(struct machine *machine)
30 {
31 union perf_event event;
32 pid_t pid = getpid();
33
34 return perf_event__synthesize_mmap_events(NULL, &event, pid, pid,
35 mmap_handler, machine, true, 500);
36 }
37
38 #define MAX_STACK 8
39
unwind_entry(struct unwind_entry * entry,void * arg)40 static int unwind_entry(struct unwind_entry *entry, void *arg)
41 {
42 unsigned long *cnt = (unsigned long *) arg;
43 char *symbol = entry->sym ? entry->sym->name : NULL;
44 static const char *funcs[MAX_STACK] = {
45 "test__arch_unwind_sample",
46 "unwind_thread",
47 "compare",
48 "bsearch",
49 "krava_3",
50 "krava_2",
51 "krava_1",
52 "test__dwarf_unwind"
53 };
54
55 if (*cnt >= MAX_STACK) {
56 pr_debug("failed: crossed the max stack value %d\n", MAX_STACK);
57 return -1;
58 }
59
60 if (!symbol) {
61 pr_debug("failed: got unresolved address 0x%" PRIx64 "\n",
62 entry->ip);
63 return -1;
64 }
65
66 pr_debug("got: %s 0x%" PRIx64 "\n", symbol, entry->ip);
67 return strcmp((const char *) symbol, funcs[(*cnt)++]);
68 }
69
70 __attribute__ ((noinline))
unwind_thread(struct thread * thread)71 static int unwind_thread(struct thread *thread)
72 {
73 struct perf_sample sample;
74 unsigned long cnt = 0;
75 int err = -1;
76
77 memset(&sample, 0, sizeof(sample));
78
79 if (test__arch_unwind_sample(&sample, thread)) {
80 pr_debug("failed to get unwind sample\n");
81 goto out;
82 }
83
84 err = unwind__get_entries(unwind_entry, &cnt, thread,
85 &sample, MAX_STACK);
86 if (err)
87 pr_debug("unwind failed\n");
88 else if (cnt != MAX_STACK) {
89 pr_debug("got wrong number of stack entries %lu != %d\n",
90 cnt, MAX_STACK);
91 err = -1;
92 }
93
94 out:
95 free(sample.user_stack.data);
96 free(sample.user_regs.regs);
97 return err;
98 }
99
100 static int global_unwind_retval = -INT_MAX;
101
102 __attribute__ ((noinline))
compare(void * p1,void * p2)103 static int compare(void *p1, void *p2)
104 {
105 /* Any possible value should be 'thread' */
106 struct thread *thread = *(struct thread **)p1;
107
108 if (global_unwind_retval == -INT_MAX)
109 global_unwind_retval = unwind_thread(thread);
110
111 return p1 - p2;
112 }
113
114 __attribute__ ((noinline))
krava_3(struct thread * thread)115 static int krava_3(struct thread *thread)
116 {
117 struct thread *array[2] = {thread, thread};
118 void *fp = &bsearch;
119 /*
120 * make _bsearch a volatile function pointer to
121 * prevent potential optimization, which may expand
122 * bsearch and call compare directly from this function,
123 * instead of libc shared object.
124 */
125 void *(*volatile _bsearch)(void *, void *, size_t,
126 size_t, int (*)(void *, void *));
127
128 _bsearch = fp;
129 _bsearch(array, &thread, 2, sizeof(struct thread **), compare);
130 return global_unwind_retval;
131 }
132
133 __attribute__ ((noinline))
krava_2(struct thread * thread)134 static int krava_2(struct thread *thread)
135 {
136 return krava_3(thread);
137 }
138
139 __attribute__ ((noinline))
krava_1(struct thread * thread)140 static int krava_1(struct thread *thread)
141 {
142 return krava_2(thread);
143 }
144
test__dwarf_unwind(void)145 int test__dwarf_unwind(void)
146 {
147 struct machines machines;
148 struct machine *machine;
149 struct thread *thread;
150 int err = -1;
151
152 machines__init(&machines);
153
154 machine = machines__find(&machines, HOST_KERNEL_ID);
155 if (!machine) {
156 pr_err("Could not get machine\n");
157 return -1;
158 }
159
160 callchain_param.record_mode = CALLCHAIN_DWARF;
161
162 if (init_live_machine(machine)) {
163 pr_err("Could not init machine\n");
164 goto out;
165 }
166
167 if (verbose > 1)
168 machine__fprintf(machine, stderr);
169
170 thread = machine__find_thread(machine, getpid(), getpid());
171 if (!thread) {
172 pr_err("Could not get thread\n");
173 goto out;
174 }
175
176 err = krava_1(thread);
177 thread__put(thread);
178
179 out:
180 machine__delete_threads(machine);
181 machine__exit(machine);
182 machines__exit(&machines);
183 return err;
184 }
185