1 #include <linux/compiler.h>
2 #include <linux/types.h>
12 #include "callchain.h"
14 /* For bsearch. We try to unwind functions in shared object. */
17 static int mmap_handler(struct perf_tool
*tool __maybe_unused
,
18 union perf_event
*event
,
19 struct perf_sample
*sample __maybe_unused
,
20 struct machine
*machine
)
22 return machine__process_mmap2_event(machine
, event
, NULL
);
25 static int init_live_machine(struct machine
*machine
)
27 union perf_event event
;
30 return perf_event__synthesize_mmap_events(NULL
, &event
, pid
, pid
,
31 mmap_handler
, machine
, true);
36 static int unwind_entry(struct unwind_entry
*entry
, void *arg
)
38 unsigned long *cnt
= (unsigned long *) arg
;
39 char *symbol
= entry
->sym
? entry
->sym
->name
: NULL
;
40 static const char *funcs
[MAX_STACK
] = {
41 "test__arch_unwind_sample",
51 if (*cnt
>= MAX_STACK
) {
52 pr_debug("failed: crossed the max stack value %d\n", MAX_STACK
);
57 pr_debug("failed: got unresolved address 0x%" PRIx64
"\n",
62 pr_debug("got: %s 0x%" PRIx64
"\n", symbol
, entry
->ip
);
63 return strcmp((const char *) symbol
, funcs
[(*cnt
)++]);
66 __attribute__ ((noinline
))
67 static int unwind_thread(struct thread
*thread
)
69 struct perf_sample sample
;
70 unsigned long cnt
= 0;
73 memset(&sample
, 0, sizeof(sample
));
75 if (test__arch_unwind_sample(&sample
, thread
)) {
76 pr_debug("failed to get unwind sample\n");
80 err
= unwind__get_entries(unwind_entry
, &cnt
, thread
,
83 pr_debug("unwind failed\n");
84 else if (cnt
!= MAX_STACK
) {
85 pr_debug("got wrong number of stack entries %lu != %d\n",
91 free(sample
.user_stack
.data
);
92 free(sample
.user_regs
.regs
);
96 static int global_unwind_retval
= -INT_MAX
;
98 __attribute__ ((noinline
))
99 static int compare(void *p1
, void *p2
)
101 /* Any possible value should be 'thread' */
102 struct thread
*thread
= *(struct thread
**)p1
;
104 if (global_unwind_retval
== -INT_MAX
)
105 global_unwind_retval
= unwind_thread(thread
);
110 __attribute__ ((noinline
))
111 static int krava_3(struct thread
*thread
)
113 struct thread
*array
[2] = {thread
, thread
};
116 * make _bsearch a volatile function pointer to
117 * prevent potential optimization, which may expand
118 * bsearch and call compare directly from this function,
119 * instead of libc shared object.
121 void *(*volatile _bsearch
)(void *, void *, size_t,
122 size_t, int (*)(void *, void *));
125 _bsearch(array
, &thread
, 2, sizeof(struct thread
**), compare
);
126 return global_unwind_retval
;
129 __attribute__ ((noinline
))
130 static int krava_2(struct thread
*thread
)
132 return krava_3(thread
);
135 __attribute__ ((noinline
))
136 static int krava_1(struct thread
*thread
)
138 return krava_2(thread
);
141 int test__dwarf_unwind(void)
143 struct machines machines
;
144 struct machine
*machine
;
145 struct thread
*thread
;
148 machines__init(&machines
);
150 machine
= machines__find(&machines
, HOST_KERNEL_ID
);
152 pr_err("Could not get machine\n");
156 callchain_param
.record_mode
= CALLCHAIN_DWARF
;
158 if (init_live_machine(machine
)) {
159 pr_err("Could not init machine\n");
164 machine__fprintf(machine
, stderr
);
166 thread
= machine__find_thread(machine
, getpid(), getpid());
168 pr_err("Could not get thread\n");
172 err
= krava_1(thread
);
175 machine__delete_threads(machine
);
176 machine__exit(machine
);
177 machines__exit(&machines
);