Merge tag 'locks-v3.16-2' of git://git.samba.org/jlayton/linux
[linux/fpc-iii.git] / tools / perf / tests / bp_signal.c
blobaba095489193bb6e224ff11f3f2ee03968df8711
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"
29 static int fd1;
30 static int fd2;
31 static int overflows;
33 __attribute__ ((noinline))
34 static int test_function(void)
36 return time(NULL);
39 static void sig_handler(int signum __maybe_unused,
40 siginfo_t *oh __maybe_unused,
41 void *uc __maybe_unused)
43 overflows++;
45 if (overflows > 10) {
47 * This should be executed only once during
48 * this test, if we are here for the 10th
49 * time, consider this the recursive issue.
51 * We can get out of here by disable events,
52 * so no new SIGIO is delivered.
54 ioctl(fd1, PERF_EVENT_IOC_DISABLE, 0);
55 ioctl(fd2, PERF_EVENT_IOC_DISABLE, 0);
59 static int bp_event(void *fn, int setup_signal)
61 struct perf_event_attr pe;
62 int fd;
64 memset(&pe, 0, sizeof(struct perf_event_attr));
65 pe.type = PERF_TYPE_BREAKPOINT;
66 pe.size = sizeof(struct perf_event_attr);
68 pe.config = 0;
69 pe.bp_type = HW_BREAKPOINT_X;
70 pe.bp_addr = (unsigned long) fn;
71 pe.bp_len = sizeof(long);
73 pe.sample_period = 1;
74 pe.sample_type = PERF_SAMPLE_IP;
75 pe.wakeup_events = 1;
77 pe.disabled = 1;
78 pe.exclude_kernel = 1;
79 pe.exclude_hv = 1;
81 fd = sys_perf_event_open(&pe, 0, -1, -1, 0);
82 if (fd < 0) {
83 pr_debug("failed opening event %llx\n", pe.config);
84 return TEST_FAIL;
87 if (setup_signal) {
88 fcntl(fd, F_SETFL, O_RDWR|O_NONBLOCK|O_ASYNC);
89 fcntl(fd, F_SETSIG, SIGIO);
90 fcntl(fd, F_SETOWN, getpid());
93 ioctl(fd, PERF_EVENT_IOC_RESET, 0);
95 return fd;
98 static long long bp_count(int fd)
100 long long count;
101 int ret;
103 ret = read(fd, &count, sizeof(long long));
104 if (ret != sizeof(long long)) {
105 pr_debug("failed to read: %d\n", ret);
106 return TEST_FAIL;
109 return count;
112 int test__bp_signal(void)
114 struct sigaction sa;
115 long long count1, count2;
117 /* setup SIGIO signal handler */
118 memset(&sa, 0, sizeof(struct sigaction));
119 sa.sa_sigaction = (void *) sig_handler;
120 sa.sa_flags = SA_SIGINFO;
122 if (sigaction(SIGIO, &sa, NULL) < 0) {
123 pr_debug("failed setting up signal handler\n");
124 return TEST_FAIL;
128 * We create following events:
130 * fd1 - breakpoint event on test_function with SIGIO
131 * signal configured. We should get signal
132 * notification each time the breakpoint is hit
134 * fd2 - breakpoint event on sig_handler without SIGIO
135 * configured.
137 * Following processing should happen:
138 * - execute test_function
139 * - fd1 event breakpoint hit -> count1 == 1
140 * - SIGIO is delivered -> overflows == 1
141 * - fd2 event breakpoint hit -> count2 == 1
143 * The test case check following error conditions:
144 * - we get stuck in signal handler because of debug
145 * exception being triggered receursively due to
146 * the wrong RF EFLAG management
148 * - we never trigger the sig_handler breakpoint due
149 * to the rong RF EFLAG management
153 fd1 = bp_event(test_function, 1);
154 fd2 = bp_event(sig_handler, 0);
156 ioctl(fd1, PERF_EVENT_IOC_ENABLE, 0);
157 ioctl(fd2, PERF_EVENT_IOC_ENABLE, 0);
160 * Kick off the test by trigering 'fd1'
161 * breakpoint.
163 test_function();
165 ioctl(fd1, PERF_EVENT_IOC_DISABLE, 0);
166 ioctl(fd2, PERF_EVENT_IOC_DISABLE, 0);
168 count1 = bp_count(fd1);
169 count2 = bp_count(fd2);
171 close(fd1);
172 close(fd2);
174 pr_debug("count1 %lld, count2 %lld, overflow %d\n",
175 count1, count2, overflows);
177 if (count1 != 1) {
178 if (count1 == 11)
179 pr_debug("failed: RF EFLAG recursion issue detected\n");
180 else
181 pr_debug("failed: wrong count for bp1%lld\n", count1);
184 if (overflows != 1)
185 pr_debug("failed: wrong overflow hit\n");
187 if (count2 != 1)
188 pr_debug("failed: wrong count for bp2\n");
190 return count1 == 1 && overflows == 1 && count2 == 1 ?
191 TEST_OK : TEST_FAIL;