1 #include <linux/compiler.h>
2 #include <linux/types.h>
12 #include "callchain.h"
14 #if defined (__x86_64__) || defined (__i386__)
15 #include "arch-tests.h"
18 /* For bsearch. We try to unwind functions in shared object. */
21 static int mmap_handler(struct perf_tool
*tool __maybe_unused
,
22 union perf_event
*event
,
23 struct perf_sample
*sample
,
24 struct machine
*machine
)
26 return machine__process_mmap2_event(machine
, event
, sample
);
29 static int init_live_machine(struct machine
*machine
)
31 union perf_event event
;
34 return perf_event__synthesize_mmap_events(NULL
, &event
, pid
, pid
,
35 mmap_handler
, machine
, true, 500);
40 static int unwind_entry(struct unwind_entry
*entry
, void *arg
)
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",
55 * The funcs[MAX_STACK] array index, based on the
56 * callchain order setup.
58 int idx
= callchain_param
.order
== ORDER_CALLER
?
59 MAX_STACK
- *cnt
- 1 : *cnt
;
61 if (*cnt
>= MAX_STACK
) {
62 pr_debug("failed: crossed the max stack value %d\n", MAX_STACK
);
67 pr_debug("failed: got unresolved address 0x%" PRIx64
"\n",
73 pr_debug("got: %s 0x%" PRIx64
", expecting %s\n",
74 symbol
, entry
->ip
, funcs
[idx
]);
75 return strcmp((const char *) symbol
, funcs
[idx
]);
78 __attribute__ ((noinline
))
79 static int unwind_thread(struct thread
*thread
)
81 struct perf_sample sample
;
82 unsigned long cnt
= 0;
85 memset(&sample
, 0, sizeof(sample
));
87 if (test__arch_unwind_sample(&sample
, thread
)) {
88 pr_debug("failed to get unwind sample\n");
92 err
= unwind__get_entries(unwind_entry
, &cnt
, thread
,
95 pr_debug("unwind failed\n");
96 else if (cnt
!= MAX_STACK
) {
97 pr_debug("got wrong number of stack entries %lu != %d\n",
103 free(sample
.user_stack
.data
);
104 free(sample
.user_regs
.regs
);
108 static int global_unwind_retval
= -INT_MAX
;
110 __attribute__ ((noinline
))
111 static int compare(void *p1
, void *p2
)
113 /* Any possible value should be 'thread' */
114 struct thread
*thread
= *(struct thread
**)p1
;
116 if (global_unwind_retval
== -INT_MAX
) {
117 /* Call unwinder twice for both callchain orders. */
118 callchain_param
.order
= ORDER_CALLER
;
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
);
130 __attribute__ ((noinline
))
131 static int krava_3(struct thread
*thread
)
133 struct thread
*array
[2] = {thread
, thread
};
136 * make _bsearch a volatile function pointer to
137 * prevent potential optimization, which may expand
138 * bsearch and call compare directly from this function,
139 * instead of libc shared object.
141 void *(*volatile _bsearch
)(void *, void *, size_t,
142 size_t, int (*)(void *, void *));
145 _bsearch(array
, &thread
, 2, sizeof(struct thread
**), compare
);
146 return global_unwind_retval
;
149 __attribute__ ((noinline
))
150 static int krava_2(struct thread
*thread
)
152 return krava_3(thread
);
155 __attribute__ ((noinline
))
156 static int krava_1(struct thread
*thread
)
158 return krava_2(thread
);
161 int test__dwarf_unwind(int subtest __maybe_unused
)
163 struct machine
*machine
;
164 struct thread
*thread
;
167 machine
= machine__new_host();
169 pr_err("Could not get machine\n");
173 if (machine__create_kernel_maps(machine
)) {
174 pr_err("Failed to create kernel maps\n");
178 callchain_param
.record_mode
= CALLCHAIN_DWARF
;
180 if (init_live_machine(machine
)) {
181 pr_err("Could not init machine\n");
186 machine__fprintf(machine
, stderr
);
188 thread
= machine__find_thread(machine
, getpid(), getpid());
190 pr_err("Could not get thread\n");
194 err
= krava_1(thread
);
198 machine__delete_threads(machine
);
199 machine__delete(machine
);