blob: 779ce280a0e9d330869e4e20a0e8791a2893756d [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Jiri Olsaaa16b812014-01-07 13:47:22 +01002#include <linux/compiler.h>
Borislav Petkovd944c4e2014-04-25 21:31:02 +02003#include <linux/types.h>
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -03004#include <linux/zalloc.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -03005#include <inttypes.h>
Arnaldo Carvalho de Melo4a3cec82019-08-30 11:11:01 -03006#include <limits.h>
Jiri Olsaaa16b812014-01-07 13:47:22 +01007#include <unistd.h>
8#include "tests.h"
9#include "debug.h"
10#include "machine.h"
11#include "event.h"
Milian Wolffdf90cc42017-09-06 17:02:09 +020012#include "../util/unwind.h"
Jiri Olsaaa16b812014-01-07 13:47:22 +010013#include "perf_regs.h"
14#include "map.h"
Arnaldo Carvalho de Melodaecf9e2019-01-28 00:03:34 +010015#include "symbol.h"
Jiri Olsaaa16b812014-01-07 13:47:22 +010016#include "thread.h"
Namhyung Kim0cdccac2014-10-06 09:45:59 +090017#include "callchain.h"
Arnaldo Carvalho de Meloea49e012019-09-18 11:36:13 -030018#include "util/synthetic-events.h"
Jiri Olsaaa16b812014-01-07 13:47:22 +010019
Ravi Bangoriad18019a2016-09-19 02:38:20 -040020#if defined (__x86_64__) || defined (__i386__) || defined (__powerpc__)
Matt Flemingd8b167f2015-10-05 15:40:20 +010021#include "arch-tests.h"
22#endif
23
Wang Nanb93b0962015-01-14 10:36:47 +080024/* For bsearch. We try to unwind functions in shared object. */
25#include <stdlib.h>
26
Jiri Olsaaa16b812014-01-07 13:47:22 +010027static int mmap_handler(struct perf_tool *tool __maybe_unused,
28 union perf_event *event,
Arnaldo Carvalho de Meloeb9f0322016-03-22 19:00:43 -030029 struct perf_sample *sample,
Jiri Olsaaa16b812014-01-07 13:47:22 +010030 struct machine *machine)
31{
Arnaldo Carvalho de Meloeb9f0322016-03-22 19:00:43 -030032 return machine__process_mmap2_event(machine, event, sample);
Jiri Olsaaa16b812014-01-07 13:47:22 +010033}
34
35static int init_live_machine(struct machine *machine)
36{
37 union perf_event event;
38 pid_t pid = getpid();
39
40 return perf_event__synthesize_mmap_events(NULL, &event, pid, pid,
Mark Drayton3fcb10e2018-12-04 12:34:20 -080041 mmap_handler, machine, true);
Jiri Olsaaa16b812014-01-07 13:47:22 +010042}
43
Jiri Olsafdf7c492018-02-06 19:18:12 +010044/*
45 * We need to keep these functions global, despite the
46 * fact that they are used only locally in this object,
47 * in order to keep them around even if the binary is
48 * stripped. If they are gone, the unwind check for
49 * symbol fails.
50 */
51int test_dwarf_unwind__thread(struct thread *thread);
52int test_dwarf_unwind__compare(void *p1, void *p2);
53int test_dwarf_unwind__krava_3(struct thread *thread);
54int test_dwarf_unwind__krava_2(struct thread *thread);
55int test_dwarf_unwind__krava_1(struct thread *thread);
56
Wang Nanb93b0962015-01-14 10:36:47 +080057#define MAX_STACK 8
Jiri Olsaaa16b812014-01-07 13:47:22 +010058
59static int unwind_entry(struct unwind_entry *entry, void *arg)
60{
61 unsigned long *cnt = (unsigned long *) arg;
Arnaldo Carvalho de Meloc1529732019-11-04 11:58:21 -030062 char *symbol = entry->ms.sym ? entry->ms.sym->name : NULL;
Jiri Olsaaa16b812014-01-07 13:47:22 +010063 static const char *funcs[MAX_STACK] = {
64 "test__arch_unwind_sample",
Jiri Olsafdf7c492018-02-06 19:18:12 +010065 "test_dwarf_unwind__thread",
66 "test_dwarf_unwind__compare",
Wang Nanb93b0962015-01-14 10:36:47 +080067 "bsearch",
Jiri Olsafdf7c492018-02-06 19:18:12 +010068 "test_dwarf_unwind__krava_3",
69 "test_dwarf_unwind__krava_2",
70 "test_dwarf_unwind__krava_1",
Jiri Olsaaa16b812014-01-07 13:47:22 +010071 "test__dwarf_unwind"
72 };
Jiri Olsa8dc05642015-11-17 16:05:39 +010073 /*
74 * The funcs[MAX_STACK] array index, based on the
75 * callchain order setup.
76 */
77 int idx = callchain_param.order == ORDER_CALLER ?
78 MAX_STACK - *cnt - 1 : *cnt;
Jiri Olsaaa16b812014-01-07 13:47:22 +010079
80 if (*cnt >= MAX_STACK) {
81 pr_debug("failed: crossed the max stack value %d\n", MAX_STACK);
82 return -1;
83 }
84
85 if (!symbol) {
86 pr_debug("failed: got unresolved address 0x%" PRIx64 "\n",
87 entry->ip);
88 return -1;
89 }
90
Jiri Olsa8dc05642015-11-17 16:05:39 +010091 (*cnt)++;
92 pr_debug("got: %s 0x%" PRIx64 ", expecting %s\n",
93 symbol, entry->ip, funcs[idx]);
94 return strcmp((const char *) symbol, funcs[idx]);
Jiri Olsaaa16b812014-01-07 13:47:22 +010095}
96
Jiri Olsafdf7c492018-02-06 19:18:12 +010097noinline int test_dwarf_unwind__thread(struct thread *thread)
Jiri Olsaaa16b812014-01-07 13:47:22 +010098{
99 struct perf_sample sample;
100 unsigned long cnt = 0;
101 int err = -1;
102
103 memset(&sample, 0, sizeof(sample));
104
105 if (test__arch_unwind_sample(&sample, thread)) {
106 pr_debug("failed to get unwind sample\n");
107 goto out;
108 }
109
Arnaldo Carvalho de Melodd8c17a2014-10-23 16:42:19 -0300110 err = unwind__get_entries(unwind_entry, &cnt, thread,
Jiri Olsa352ea452014-01-07 13:47:25 +0100111 &sample, MAX_STACK);
Jiri Olsaaa16b812014-01-07 13:47:22 +0100112 if (err)
113 pr_debug("unwind failed\n");
114 else if (cnt != MAX_STACK) {
115 pr_debug("got wrong number of stack entries %lu != %d\n",
116 cnt, MAX_STACK);
117 err = -1;
118 }
119
120 out:
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -0300121 zfree(&sample.user_stack.data);
122 zfree(&sample.user_regs.regs);
Jiri Olsaaa16b812014-01-07 13:47:22 +0100123 return err;
124}
125
Wang Nanb93b0962015-01-14 10:36:47 +0800126static int global_unwind_retval = -INT_MAX;
127
Jiri Olsafdf7c492018-02-06 19:18:12 +0100128noinline int test_dwarf_unwind__compare(void *p1, void *p2)
Wang Nanb93b0962015-01-14 10:36:47 +0800129{
130 /* Any possible value should be 'thread' */
131 struct thread *thread = *(struct thread **)p1;
132
Jiri Olsa8dc05642015-11-17 16:05:39 +0100133 if (global_unwind_retval == -INT_MAX) {
134 /* Call unwinder twice for both callchain orders. */
135 callchain_param.order = ORDER_CALLER;
136
Jiri Olsafdf7c492018-02-06 19:18:12 +0100137 global_unwind_retval = test_dwarf_unwind__thread(thread);
Jiri Olsa8dc05642015-11-17 16:05:39 +0100138 if (!global_unwind_retval) {
139 callchain_param.order = ORDER_CALLEE;
Jiri Olsafdf7c492018-02-06 19:18:12 +0100140 global_unwind_retval = test_dwarf_unwind__thread(thread);
Jiri Olsa8dc05642015-11-17 16:05:39 +0100141 }
142 }
Wang Nanb93b0962015-01-14 10:36:47 +0800143
144 return p1 - p2;
145}
146
Jiri Olsafdf7c492018-02-06 19:18:12 +0100147noinline int test_dwarf_unwind__krava_3(struct thread *thread)
Jiri Olsaaa16b812014-01-07 13:47:22 +0100148{
Wang Nanb93b0962015-01-14 10:36:47 +0800149 struct thread *array[2] = {thread, thread};
150 void *fp = &bsearch;
151 /*
152 * make _bsearch a volatile function pointer to
153 * prevent potential optimization, which may expand
154 * bsearch and call compare directly from this function,
155 * instead of libc shared object.
156 */
157 void *(*volatile _bsearch)(void *, void *, size_t,
158 size_t, int (*)(void *, void *));
159
160 _bsearch = fp;
Jiri Olsafdf7c492018-02-06 19:18:12 +0100161 _bsearch(array, &thread, 2, sizeof(struct thread **),
162 test_dwarf_unwind__compare);
Wang Nanb93b0962015-01-14 10:36:47 +0800163 return global_unwind_retval;
Jiri Olsaaa16b812014-01-07 13:47:22 +0100164}
165
Jiri Olsafdf7c492018-02-06 19:18:12 +0100166noinline int test_dwarf_unwind__krava_2(struct thread *thread)
Jiri Olsaaa16b812014-01-07 13:47:22 +0100167{
Jiri Olsafdf7c492018-02-06 19:18:12 +0100168 return test_dwarf_unwind__krava_3(thread);
Jiri Olsaaa16b812014-01-07 13:47:22 +0100169}
170
Jiri Olsafdf7c492018-02-06 19:18:12 +0100171noinline int test_dwarf_unwind__krava_1(struct thread *thread)
Jiri Olsaaa16b812014-01-07 13:47:22 +0100172{
Jiri Olsafdf7c492018-02-06 19:18:12 +0100173 return test_dwarf_unwind__krava_2(thread);
Jiri Olsaaa16b812014-01-07 13:47:22 +0100174}
175
Arnaldo Carvalho de Melo81f17c92017-08-03 15:16:31 -0300176int test__dwarf_unwind(struct test *test __maybe_unused, int subtest __maybe_unused)
Jiri Olsaaa16b812014-01-07 13:47:22 +0100177{
Jiri Olsaaa16b812014-01-07 13:47:22 +0100178 struct machine *machine;
179 struct thread *thread;
180 int err = -1;
181
Jiri Olsabdaba8a2015-12-03 09:34:12 +0100182 machine = machine__new_host();
Jiri Olsaaa16b812014-01-07 13:47:22 +0100183 if (!machine) {
184 pr_err("Could not get machine\n");
185 return -1;
186 }
187
Jiri Olsa9bdcede52015-11-27 09:21:21 +0100188 if (machine__create_kernel_maps(machine)) {
189 pr_err("Failed to create kernel maps\n");
190 return -1;
191 }
192
Namhyung Kim0cdccac2014-10-06 09:45:59 +0900193 callchain_param.record_mode = CALLCHAIN_DWARF;
Arnaldo Carvalho de Meloeabad8c2018-01-15 16:48:46 -0300194 dwarf_callchain_users = true;
Namhyung Kim0cdccac2014-10-06 09:45:59 +0900195
Jiri Olsaaa16b812014-01-07 13:47:22 +0100196 if (init_live_machine(machine)) {
197 pr_err("Could not init machine\n");
198 goto out;
199 }
200
201 if (verbose > 1)
202 machine__fprintf(machine, stderr);
203
Jiri Olsad75e6092014-03-14 15:00:03 +0100204 thread = machine__find_thread(machine, getpid(), getpid());
Jiri Olsaaa16b812014-01-07 13:47:22 +0100205 if (!thread) {
206 pr_err("Could not get thread\n");
207 goto out;
208 }
209
Jiri Olsafdf7c492018-02-06 19:18:12 +0100210 err = test_dwarf_unwind__krava_1(thread);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300211 thread__put(thread);
Jiri Olsaaa16b812014-01-07 13:47:22 +0100212
213 out:
214 machine__delete_threads(machine);
Jiri Olsabdaba8a2015-12-03 09:34:12 +0100215 machine__delete(machine);
Jiri Olsaaa16b812014-01-07 13:47:22 +0100216 return err;
217}