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 __maybe_unused
,
24 struct machine
*machine
)
26 return machine__process_mmap2_event(machine
, event
, NULL
);
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 if (*cnt
>= MAX_STACK
) {
56 pr_debug("failed: crossed the max stack value %d\n", MAX_STACK
);
61 pr_debug("failed: got unresolved address 0x%" PRIx64
"\n",
66 pr_debug("got: %s 0x%" PRIx64
"\n", symbol
, entry
->ip
);
67 return strcmp((const char *) symbol
, funcs
[(*cnt
)++]);
70 __attribute__ ((noinline
))
71 static int unwind_thread(struct thread
*thread
)
73 struct perf_sample sample
;
74 unsigned long cnt
= 0;
77 memset(&sample
, 0, sizeof(sample
));
79 if (test__arch_unwind_sample(&sample
, thread
)) {
80 pr_debug("failed to get unwind sample\n");
84 err
= unwind__get_entries(unwind_entry
, &cnt
, thread
,
87 pr_debug("unwind failed\n");
88 else if (cnt
!= MAX_STACK
) {
89 pr_debug("got wrong number of stack entries %lu != %d\n",
95 free(sample
.user_stack
.data
);
96 free(sample
.user_regs
.regs
);
100 static int global_unwind_retval
= -INT_MAX
;
102 __attribute__ ((noinline
))
103 static int compare(void *p1
, void *p2
)
105 /* Any possible value should be 'thread' */
106 struct thread
*thread
= *(struct thread
**)p1
;
108 if (global_unwind_retval
== -INT_MAX
)
109 global_unwind_retval
= unwind_thread(thread
);
114 __attribute__ ((noinline
))
115 static int krava_3(struct thread
*thread
)
117 struct thread
*array
[2] = {thread
, thread
};
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.
125 void *(*volatile _bsearch
)(void *, void *, size_t,
126 size_t, int (*)(void *, void *));
129 _bsearch(array
, &thread
, 2, sizeof(struct thread
**), compare
);
130 return global_unwind_retval
;
133 __attribute__ ((noinline
))
134 static int krava_2(struct thread
*thread
)
136 return krava_3(thread
);
139 __attribute__ ((noinline
))
140 static int krava_1(struct thread
*thread
)
142 return krava_2(thread
);
145 int test__dwarf_unwind(void)
147 struct machines machines
;
148 struct machine
*machine
;
149 struct thread
*thread
;
152 machines__init(&machines
);
154 machine
= machines__find(&machines
, HOST_KERNEL_ID
);
156 pr_err("Could not get machine\n");
160 callchain_param
.record_mode
= CALLCHAIN_DWARF
;
162 if (init_live_machine(machine
)) {
163 pr_err("Could not init machine\n");
168 machine__fprintf(machine
, stderr
);
170 thread
= machine__find_thread(machine
, getpid(), getpid());
172 pr_err("Could not get thread\n");
176 err
= krava_1(thread
);
180 machine__delete_threads(machine
);
181 machine__exit(machine
);
182 machines__exit(&machines
);