perf stat: Update walltime_nsecs_stats in interval mode
[linux/fpc-iii.git] / tools / perf / tests / bp_signal.c
blob97937e1bc53a0c358bf34a9cc50a67f150a1b905
1 /*
2 * Inspired by breakpoint overflow test done by
3 * Vince Weaver <vincent.weaver@maine.edu> for perf_event_tests
4 * (git://github.com/deater/perf_event_tests)
5 */
7 /*
8 * Powerpc needs __SANE_USERSPACE_TYPES__ before <linux/types.h> to select
9 * 'int-ll64.h' and avoid compile warnings when printing __u64 with %llu.
11 #define __SANE_USERSPACE_TYPES__
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <unistd.h>
16 #include <string.h>
17 #include <sys/ioctl.h>
18 #include <time.h>
19 #include <fcntl.h>
20 #include <signal.h>
21 #include <sys/mman.h>
22 #include <linux/compiler.h>
23 #include <linux/hw_breakpoint.h>
25 #include "tests.h"
26 #include "debug.h"
27 #include "perf.h"
28 #include "cloexec.h"
30 static int fd1;
31 static int fd2;
32 static int fd3;
33 static int overflows;
34 static int overflows_2;
36 volatile long the_var;
40 * Use ASM to ensure watchpoint and breakpoint can be triggered
41 * at one instruction.
43 #if defined (__x86_64__)
44 extern void __test_function(volatile long *ptr);
45 asm (
46 ".globl __test_function\n"
47 "__test_function:\n"
48 "incq (%rdi)\n"
49 "ret\n");
50 #elif defined (__aarch64__)
51 extern void __test_function(volatile long *ptr);
52 asm (
53 ".globl __test_function\n"
54 "__test_function:\n"
55 "str x30, [x0]\n"
56 "ret\n");
58 #else
59 static void __test_function(volatile long *ptr)
61 *ptr = 0x1234;
63 #endif
65 static noinline int test_function(void)
67 __test_function(&the_var);
68 the_var++;
69 return time(NULL);
72 static void sig_handler_2(int signum __maybe_unused,
73 siginfo_t *oh __maybe_unused,
74 void *uc __maybe_unused)
76 overflows_2++;
77 if (overflows_2 > 10) {
78 ioctl(fd1, PERF_EVENT_IOC_DISABLE, 0);
79 ioctl(fd2, PERF_EVENT_IOC_DISABLE, 0);
80 ioctl(fd3, PERF_EVENT_IOC_DISABLE, 0);
84 static void sig_handler(int signum __maybe_unused,
85 siginfo_t *oh __maybe_unused,
86 void *uc __maybe_unused)
88 overflows++;
90 if (overflows > 10) {
92 * This should be executed only once during
93 * this test, if we are here for the 10th
94 * time, consider this the recursive issue.
96 * We can get out of here by disable events,
97 * so no new SIGIO is delivered.
99 ioctl(fd1, PERF_EVENT_IOC_DISABLE, 0);
100 ioctl(fd2, PERF_EVENT_IOC_DISABLE, 0);
101 ioctl(fd3, PERF_EVENT_IOC_DISABLE, 0);
105 static int __event(bool is_x, void *addr, int sig)
107 struct perf_event_attr pe;
108 int fd;
110 memset(&pe, 0, sizeof(struct perf_event_attr));
111 pe.type = PERF_TYPE_BREAKPOINT;
112 pe.size = sizeof(struct perf_event_attr);
114 pe.config = 0;
115 pe.bp_type = is_x ? HW_BREAKPOINT_X : HW_BREAKPOINT_W;
116 pe.bp_addr = (unsigned long) addr;
117 pe.bp_len = sizeof(long);
119 pe.sample_period = 1;
120 pe.sample_type = PERF_SAMPLE_IP;
121 pe.wakeup_events = 1;
123 pe.disabled = 1;
124 pe.exclude_kernel = 1;
125 pe.exclude_hv = 1;
127 fd = sys_perf_event_open(&pe, 0, -1, -1,
128 perf_event_open_cloexec_flag());
129 if (fd < 0) {
130 pr_debug("failed opening event %llx\n", pe.config);
131 return TEST_FAIL;
134 fcntl(fd, F_SETFL, O_RDWR|O_NONBLOCK|O_ASYNC);
135 fcntl(fd, F_SETSIG, sig);
136 fcntl(fd, F_SETOWN, getpid());
138 ioctl(fd, PERF_EVENT_IOC_RESET, 0);
140 return fd;
143 static int bp_event(void *addr, int sig)
145 return __event(true, addr, sig);
148 static int wp_event(void *addr, int sig)
150 return __event(false, addr, sig);
153 static long long bp_count(int fd)
155 long long count;
156 int ret;
158 ret = read(fd, &count, sizeof(long long));
159 if (ret != sizeof(long long)) {
160 pr_debug("failed to read: %d\n", ret);
161 return TEST_FAIL;
164 return count;
167 int test__bp_signal(struct test *test __maybe_unused, int subtest __maybe_unused)
169 struct sigaction sa;
170 long long count1, count2, count3;
172 /* setup SIGIO signal handler */
173 memset(&sa, 0, sizeof(struct sigaction));
174 sa.sa_sigaction = (void *) sig_handler;
175 sa.sa_flags = SA_SIGINFO;
177 if (sigaction(SIGIO, &sa, NULL) < 0) {
178 pr_debug("failed setting up signal handler\n");
179 return TEST_FAIL;
182 sa.sa_sigaction = (void *) sig_handler_2;
183 if (sigaction(SIGUSR1, &sa, NULL) < 0) {
184 pr_debug("failed setting up signal handler 2\n");
185 return TEST_FAIL;
189 * We create following events:
191 * fd1 - breakpoint event on __test_function with SIGIO
192 * signal configured. We should get signal
193 * notification each time the breakpoint is hit
195 * fd2 - breakpoint event on sig_handler with SIGUSR1
196 * configured. We should get SIGUSR1 each time when
197 * breakpoint is hit
199 * fd3 - watchpoint event on __test_function with SIGIO
200 * configured.
202 * Following processing should happen:
203 * Exec: Action: Result:
204 * incq (%rdi) - fd1 event breakpoint hit -> count1 == 1
205 * - SIGIO is delivered
206 * sig_handler - fd2 event breakpoint hit -> count2 == 1
207 * - SIGUSR1 is delivered
208 * sig_handler_2 -> overflows_2 == 1 (nested signal)
209 * sys_rt_sigreturn - return from sig_handler_2
210 * overflows++ -> overflows = 1
211 * sys_rt_sigreturn - return from sig_handler
212 * incq (%rdi) - fd3 event watchpoint hit -> count3 == 1 (wp and bp in one insn)
213 * - SIGIO is delivered
214 * sig_handler - fd2 event breakpoint hit -> count2 == 2
215 * - SIGUSR1 is delivered
216 * sig_handler_2 -> overflows_2 == 2 (nested signal)
217 * sys_rt_sigreturn - return from sig_handler_2
218 * overflows++ -> overflows = 2
219 * sys_rt_sigreturn - return from sig_handler
220 * the_var++ - fd3 event watchpoint hit -> count3 == 2 (standalone watchpoint)
221 * - SIGIO is delivered
222 * sig_handler - fd2 event breakpoint hit -> count2 == 3
223 * - SIGUSR1 is delivered
224 * sig_handler_2 -> overflows_2 == 3 (nested signal)
225 * sys_rt_sigreturn - return from sig_handler_2
226 * overflows++ -> overflows == 3
227 * sys_rt_sigreturn - return from sig_handler
229 * The test case check following error conditions:
230 * - we get stuck in signal handler because of debug
231 * exception being triggered receursively due to
232 * the wrong RF EFLAG management
234 * - we never trigger the sig_handler breakpoint due
235 * to the rong RF EFLAG management
239 fd1 = bp_event(__test_function, SIGIO);
240 fd2 = bp_event(sig_handler, SIGUSR1);
241 fd3 = wp_event((void *)&the_var, SIGIO);
243 ioctl(fd1, PERF_EVENT_IOC_ENABLE, 0);
244 ioctl(fd2, PERF_EVENT_IOC_ENABLE, 0);
245 ioctl(fd3, PERF_EVENT_IOC_ENABLE, 0);
248 * Kick off the test by trigering 'fd1'
249 * breakpoint.
251 test_function();
253 ioctl(fd1, PERF_EVENT_IOC_DISABLE, 0);
254 ioctl(fd2, PERF_EVENT_IOC_DISABLE, 0);
255 ioctl(fd3, PERF_EVENT_IOC_DISABLE, 0);
257 count1 = bp_count(fd1);
258 count2 = bp_count(fd2);
259 count3 = bp_count(fd3);
261 close(fd1);
262 close(fd2);
263 close(fd3);
265 pr_debug("count1 %lld, count2 %lld, count3 %lld, overflow %d, overflows_2 %d\n",
266 count1, count2, count3, overflows, overflows_2);
268 if (count1 != 1) {
269 if (count1 == 11)
270 pr_debug("failed: RF EFLAG recursion issue detected\n");
271 else
272 pr_debug("failed: wrong count for bp1%lld\n", count1);
275 if (overflows != 3)
276 pr_debug("failed: wrong overflow hit\n");
278 if (overflows_2 != 3)
279 pr_debug("failed: wrong overflow_2 hit\n");
281 if (count2 != 3)
282 pr_debug("failed: wrong count for bp2\n");
284 if (count3 != 2)
285 pr_debug("failed: wrong count for bp3\n");
287 return count1 == 1 && overflows == 3 && count2 == 3 && overflows_2 == 3 && count3 == 2 ?
288 TEST_OK : TEST_FAIL;
291 bool test__bp_signal_is_supported(void)
294 * The powerpc so far does not have support to even create
295 * instruction breakpoint using the perf event interface.
296 * Once it's there we can release this.
298 #ifdef __powerpc__
299 return false;
300 #else
301 return true;
302 #endif