dm thin metadata: fix __udivdi3 undefined on 32-bit
[linux/fpc-iii.git] / tools / perf / tests / dwarf-unwind.c
blob07221793a3acec65d083f68548d4bf5e287b4628
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"
14 #if defined (__x86_64__) || defined (__i386__)
15 #include "arch-tests.h"
16 #endif
18 /* For bsearch. We try to unwind functions in shared object. */
19 #include <stdlib.h>
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;
32 pid_t pid = getpid();
34 return perf_event__synthesize_mmap_events(NULL, &event, pid, pid,
35 mmap_handler, machine, true, 500);
38 #define MAX_STACK 8
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",
46 "unwind_thread",
47 "compare",
48 "bsearch",
49 "krava_3",
50 "krava_2",
51 "krava_1",
52 "test__dwarf_unwind"
55 if (*cnt >= MAX_STACK) {
56 pr_debug("failed: crossed the max stack value %d\n", MAX_STACK);
57 return -1;
60 if (!symbol) {
61 pr_debug("failed: got unresolved address 0x%" PRIx64 "\n",
62 entry->ip);
63 return -1;
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;
75 int err = -1;
77 memset(&sample, 0, sizeof(sample));
79 if (test__arch_unwind_sample(&sample, thread)) {
80 pr_debug("failed to get unwind sample\n");
81 goto out;
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;
94 out:
95 free(sample.user_stack.data);
96 free(sample.user_regs.regs);
97 return err;
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);
111 return p1 - p2;
114 __attribute__ ((noinline))
115 static int krava_3(struct thread *thread)
117 struct thread *array[2] = {thread, thread};
118 void *fp = &bsearch;
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 *));
128 _bsearch = fp;
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;
150 int err = -1;
152 machines__init(&machines);
154 machine = machines__find(&machines, HOST_KERNEL_ID);
155 if (!machine) {
156 pr_err("Could not get machine\n");
157 return -1;
160 callchain_param.record_mode = CALLCHAIN_DWARF;
162 if (init_live_machine(machine)) {
163 pr_err("Could not init machine\n");
164 goto out;
167 if (verbose > 1)
168 machine__fprintf(machine, stderr);
170 thread = machine__find_thread(machine, getpid(), getpid());
171 if (!thread) {
172 pr_err("Could not get thread\n");
173 goto out;
176 err = krava_1(thread);
177 thread__put(thread);
179 out:
180 machine__delete_threads(machine);
181 machine__exit(machine);
182 machines__exit(&machines);
183 return err;