blob: fc25e57f4a5d2bdf5455c9db75298b4700b34c04 [file] [log] [blame]
Jiri Olsaaa16b812014-01-07 13:47:22 +01001#include <linux/compiler.h>
Borislav Petkovd944c4e2014-04-25 21:31:02 +02002#include <linux/types.h>
Jiri Olsaaa16b812014-01-07 13:47:22 +01003#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"
Namhyung Kim0cdccac2014-10-06 09:45:59 +090012#include "callchain.h"
Jiri Olsaaa16b812014-01-07 13:47:22 +010013
14static 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)
18{
Don Zickusa5a5ba72014-05-30 10:49:42 -040019 return machine__process_mmap2_event(machine, event, NULL);
Jiri Olsaaa16b812014-01-07 13:47:22 +010020}
21
22static int init_live_machine(struct machine *machine)
23{
24 union perf_event event;
25 pid_t pid = getpid();
26
27 return perf_event__synthesize_mmap_events(NULL, &event, pid, pid,
28 mmap_handler, machine, true);
29}
30
31#define MAX_STACK 6
32
33static int unwind_entry(struct unwind_entry *entry, void *arg)
34{
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",
39 "unwind_thread",
40 "krava_3",
41 "krava_2",
42 "krava_1",
43 "test__dwarf_unwind"
44 };
45
46 if (*cnt >= MAX_STACK) {
47 pr_debug("failed: crossed the max stack value %d\n", MAX_STACK);
48 return -1;
49 }
50
51 if (!symbol) {
52 pr_debug("failed: got unresolved address 0x%" PRIx64 "\n",
53 entry->ip);
54 return -1;
55 }
56
57 pr_debug("got: %s 0x%" PRIx64 "\n", symbol, entry->ip);
58 return strcmp((const char *) symbol, funcs[(*cnt)++]);
59}
60
61__attribute__ ((noinline))
62static int unwind_thread(struct thread *thread, struct machine *machine)
63{
64 struct perf_sample sample;
65 unsigned long cnt = 0;
66 int err = -1;
67
68 memset(&sample, 0, sizeof(sample));
69
70 if (test__arch_unwind_sample(&sample, thread)) {
71 pr_debug("failed to get unwind sample\n");
72 goto out;
73 }
74
75 err = unwind__get_entries(unwind_entry, &cnt, machine, thread,
Jiri Olsa352ea452014-01-07 13:47:25 +010076 &sample, MAX_STACK);
Jiri Olsaaa16b812014-01-07 13:47:22 +010077 if (err)
78 pr_debug("unwind failed\n");
79 else if (cnt != MAX_STACK) {
80 pr_debug("got wrong number of stack entries %lu != %d\n",
81 cnt, MAX_STACK);
82 err = -1;
83 }
84
85 out:
86 free(sample.user_stack.data);
87 free(sample.user_regs.regs);
88 return err;
89}
90
91__attribute__ ((noinline))
92static int krava_3(struct thread *thread, struct machine *machine)
93{
94 return unwind_thread(thread, machine);
95}
96
97__attribute__ ((noinline))
98static int krava_2(struct thread *thread, struct machine *machine)
99{
100 return krava_3(thread, machine);
101}
102
103__attribute__ ((noinline))
104static int krava_1(struct thread *thread, struct machine *machine)
105{
106 return krava_2(thread, machine);
107}
108
109int test__dwarf_unwind(void)
110{
111 struct machines machines;
112 struct machine *machine;
113 struct thread *thread;
114 int err = -1;
115
116 machines__init(&machines);
117
118 machine = machines__find(&machines, HOST_KERNEL_ID);
119 if (!machine) {
120 pr_err("Could not get machine\n");
121 return -1;
122 }
123
Namhyung Kim0cdccac2014-10-06 09:45:59 +0900124 callchain_param.record_mode = CALLCHAIN_DWARF;
125
Jiri Olsaaa16b812014-01-07 13:47:22 +0100126 if (init_live_machine(machine)) {
127 pr_err("Could not init machine\n");
128 goto out;
129 }
130
131 if (verbose > 1)
132 machine__fprintf(machine, stderr);
133
Jiri Olsad75e6092014-03-14 15:00:03 +0100134 thread = machine__find_thread(machine, getpid(), getpid());
Jiri Olsaaa16b812014-01-07 13:47:22 +0100135 if (!thread) {
136 pr_err("Could not get thread\n");
137 goto out;
138 }
139
140 err = krava_1(thread, machine);
141
142 out:
143 machine__delete_threads(machine);
144 machine__exit(machine);
145 machines__exit(&machines);
146 return err;
147}