1 #include <linux/compiler.h>
2 #include <linux/types.h>
12 #include "callchain.h"
14 static int mmap_handler(struct perf_tool *tool __maybe_unused,
15 union perf_event *event,
16 struct perf_sample *sample __maybe_unused,
17 struct machine *machine)
19 return machine__process_mmap2_event(machine, event, NULL);
22 static int init_live_machine(struct machine *machine)
24 union perf_event event;
27 return perf_event__synthesize_mmap_events(NULL, &event, pid, pid,
28 mmap_handler, machine, true);
33 static int unwind_entry(struct unwind_entry *entry, void *arg)
35 unsigned long *cnt = (unsigned long *) arg;
36 char *symbol = entry->sym ? entry->sym->name : NULL;
37 static const char *funcs[MAX_STACK] = {
38 "test__arch_unwind_sample",
46 if (*cnt >= MAX_STACK) {
47 pr_debug("failed: crossed the max stack value %d\n", MAX_STACK);
52 pr_debug("failed: got unresolved address 0x%" PRIx64 "\n",
57 pr_debug("got: %s 0x%" PRIx64 "\n", symbol, entry->ip);
58 return strcmp((const char *) symbol, funcs[(*cnt)++]);
61 __attribute__ ((noinline))
62 static int unwind_thread(struct thread *thread)
64 struct perf_sample sample;
65 unsigned long cnt = 0;
68 memset(&sample, 0, sizeof(sample));
70 if (test__arch_unwind_sample(&sample, thread)) {
71 pr_debug("failed to get unwind sample\n");
75 err = unwind__get_entries(unwind_entry, &cnt, thread,
78 pr_debug("unwind failed\n");
79 else if (cnt != MAX_STACK) {
80 pr_debug("got wrong number of stack entries %lu != %d\n",
86 free(sample.user_stack.data);
87 free(sample.user_regs.regs);
91 __attribute__ ((noinline))
92 static int krava_3(struct thread *thread)
94 return unwind_thread(thread);
97 __attribute__ ((noinline))
98 static int krava_2(struct thread *thread)
100 return krava_3(thread);
103 __attribute__ ((noinline))
104 static int krava_1(struct thread *thread)
106 return krava_2(thread);
109 int test__dwarf_unwind(void)
111 struct machines machines;
112 struct machine *machine;
113 struct thread *thread;
116 machines__init(&machines);
118 machine = machines__find(&machines, HOST_KERNEL_ID);
120 pr_err("Could not get machine\n");
124 callchain_param.record_mode = CALLCHAIN_DWARF;
126 if (init_live_machine(machine)) {
127 pr_err("Could not init machine\n");
132 machine__fprintf(machine, stderr);
134 thread = machine__find_thread(machine, getpid(), getpid());
136 pr_err("Could not get thread\n");
140 err = krava_1(thread);
143 machine__delete_threads(machine);
144 machine__exit(machine);
145 machines__exit(&machines);