]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
aa16b81f | 2 | #include <linux/compiler.h> |
d944c4ee | 3 | #include <linux/types.h> |
fd20e811 | 4 | #include <inttypes.h> |
aa16b81f JO |
5 | #include <unistd.h> |
6 | #include "tests.h" | |
7 | #include "debug.h" | |
8 | #include "machine.h" | |
9 | #include "event.h" | |
df90cc41 | 10 | #include "../util/unwind.h" |
aa16b81f JO |
11 | #include "perf_regs.h" |
12 | #include "map.h" | |
13 | #include "thread.h" | |
0cdccac6 | 14 | #include "callchain.h" |
aa16b81f | 15 | |
d18019a5 | 16 | #if defined (__x86_64__) || defined (__i386__) || defined (__powerpc__) |
d8b167f9 MF |
17 | #include "arch-tests.h" |
18 | #endif | |
19 | ||
b93b0967 WN |
20 | /* For bsearch. We try to unwind functions in shared object. */ |
21 | #include <stdlib.h> | |
22 | ||
aa16b81f JO |
23 | static int mmap_handler(struct perf_tool *tool __maybe_unused, |
24 | union perf_event *event, | |
eb9f0323 | 25 | struct perf_sample *sample, |
aa16b81f JO |
26 | struct machine *machine) |
27 | { | |
eb9f0323 | 28 | return machine__process_mmap2_event(machine, event, sample); |
aa16b81f JO |
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, | |
9d9cad76 | 37 | mmap_handler, machine, true, 500); |
aa16b81f JO |
38 | } |
39 | ||
b93b0967 | 40 | #define MAX_STACK 8 |
aa16b81f JO |
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", | |
b93b0967 WN |
49 | "compare", |
50 | "bsearch", | |
aa16b81f JO |
51 | "krava_3", |
52 | "krava_2", | |
53 | "krava_1", | |
54 | "test__dwarf_unwind" | |
55 | }; | |
8dc0564d JO |
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; | |
aa16b81f JO |
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 | ||
8dc0564d JO |
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]); | |
aa16b81f JO |
78 | } |
79 | ||
9dd4ca47 | 80 | static noinline int unwind_thread(struct thread *thread) |
aa16b81f JO |
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 | ||
dd8c17a5 | 93 | err = unwind__get_entries(unwind_entry, &cnt, thread, |
352ea45a | 94 | &sample, MAX_STACK); |
aa16b81f JO |
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 | ||
b93b0967 WN |
109 | static int global_unwind_retval = -INT_MAX; |
110 | ||
9dd4ca47 | 111 | static noinline int compare(void *p1, void *p2) |
b93b0967 WN |
112 | { |
113 | /* Any possible value should be 'thread' */ | |
114 | struct thread *thread = *(struct thread **)p1; | |
115 | ||
8dc0564d JO |
116 | if (global_unwind_retval == -INT_MAX) { |
117 | /* Call unwinder twice for both callchain orders. */ | |
118 | callchain_param.order = ORDER_CALLER; | |
119 | ||
b93b0967 | 120 | global_unwind_retval = unwind_thread(thread); |
8dc0564d JO |
121 | if (!global_unwind_retval) { |
122 | callchain_param.order = ORDER_CALLEE; | |
123 | global_unwind_retval = unwind_thread(thread); | |
124 | } | |
125 | } | |
b93b0967 WN |
126 | |
127 | return p1 - p2; | |
128 | } | |
129 | ||
9dd4ca47 | 130 | static noinline int krava_3(struct thread *thread) |
aa16b81f | 131 | { |
b93b0967 WN |
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; | |
aa16b81f JO |
146 | } |
147 | ||
9dd4ca47 | 148 | static noinline int krava_2(struct thread *thread) |
aa16b81f | 149 | { |
dd8c17a5 | 150 | return krava_3(thread); |
aa16b81f JO |
151 | } |
152 | ||
9dd4ca47 | 153 | static noinline int krava_1(struct thread *thread) |
aa16b81f | 154 | { |
dd8c17a5 | 155 | return krava_2(thread); |
aa16b81f JO |
156 | } |
157 | ||
81f17c90 | 158 | int test__dwarf_unwind(struct test *test __maybe_unused, int subtest __maybe_unused) |
aa16b81f | 159 | { |
aa16b81f JO |
160 | struct machine *machine; |
161 | struct thread *thread; | |
162 | int err = -1; | |
163 | ||
bdaba8ae | 164 | machine = machine__new_host(); |
aa16b81f JO |
165 | if (!machine) { |
166 | pr_err("Could not get machine\n"); | |
167 | return -1; | |
168 | } | |
169 | ||
9bdcede5 JO |
170 | if (machine__create_kernel_maps(machine)) { |
171 | pr_err("Failed to create kernel maps\n"); | |
172 | return -1; | |
173 | } | |
174 | ||
0cdccac6 | 175 | callchain_param.record_mode = CALLCHAIN_DWARF; |
eabad8c6 | 176 | dwarf_callchain_users = true; |
0cdccac6 | 177 | |
aa16b81f JO |
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 | ||
d75e6097 | 186 | thread = machine__find_thread(machine, getpid(), getpid()); |
aa16b81f JO |
187 | if (!thread) { |
188 | pr_err("Could not get thread\n"); | |
189 | goto out; | |
190 | } | |
191 | ||
dd8c17a5 | 192 | err = krava_1(thread); |
b91fc39f | 193 | thread__put(thread); |
aa16b81f JO |
194 | |
195 | out: | |
196 | machine__delete_threads(machine); | |
bdaba8ae | 197 | machine__delete(machine); |
aa16b81f JO |
198 | return err; |
199 | } |