Debian Codegraph @

testsuite/systemtap.syscall/futex.c

raw
1 /* COVERAGE: futex */
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <sys/syscall.h>
7 #include <linux/futex.h>
8 #include <sys/time.h>
9 #include <sys/types.h>
10 #include <sys/wait.h>
11 #include <sys/mman.h>
12
13 static int *futex_addr;
14
15 static inline int
16 __futex(int *uaddr, int op, int val, const struct timespec *timeout,
17 int *uaddr2, int val3)
18 {
19 return syscall(SYS_futex, uaddr, op, val, timeout, uaddr2, val3);
20 }
21
22 void do_child()
23 {
24 *futex_addr = 0;
25 __futex(futex_addr, FUTEX_WAIT, 0, NULL, NULL, 0);
26 }
27
28 int main()
29 {
30 pid_t pid;
31 int status;
32 struct timespec t = {0,789};
33
34 // Create a mmapped area that both the parent and child processes
35 // can access.
36 futex_addr = mmap(NULL, sizeof(int), PROT_READ|PROT_WRITE,
37 MAP_SHARED|MAP_ANONYMOUS, -1, 0);
38
39 pid = fork();
40 if (pid == 0) { /* child */
41 alarm(30);
42 do_child();
43 exit(0);
44 }
45
46 // Wait for the child to run and wait on the futex.
47 sleep(1);
48
49 // Wake the child (which should immediately exit).
50 __futex(futex_addr, FUTEX_WAKE, 1, NULL, NULL, 0);
51 //staptest// futex (XXXX, FUTEX_WAKE, 1) = 1
52
53 // Clean up the child process and the mmapped area.
54 waitpid(pid, &status, 0);
55 munmap(futex_addr, sizeof(int));
56
57 /* Limit testing. */
58
59 __futex((int *)-1, FUTEX_WAKE, 1, NULL, NULL, 0);
60 #ifdef __s390__
61 //staptest// futex (0x[7]?[f]+, FUTEX_WAKE, 1) = NNNN
62 #else
63 //staptest// futex (0x[f]+, FUTEX_WAKE, 1) = NNNN
64 #endif
65 __futex(NULL, -1, 1, NULL, NULL, 0);
66 #ifdef __s390__
67 //staptest// futex (0x0, 0x[7]?[f]+, 1) = NNNN
68 #else
69 //staptest// futex (0x0, 0x[f]+, 1) = NNNN
70 #endif
71
72 __futex(NULL, FUTEX_CMP_REQUEUE, -1, NULL, NULL, 0);
73 //staptest// futex (0x0, FUTEX_CMP_REQUEUE, -1, 0, 0x0, 0) = NNNN
74
75 __futex(NULL, FUTEX_CMP_REQUEUE, 1, (struct timespec *)-1, NULL, 0);
76 #if __WORDSIZE == 64
77 //staptest// futex (0x0, FUTEX_CMP_REQUEUE, 1, 18446744073709551615, 0x0, 0) = NNNN
78 #else
79 #ifdef __s390__
80 //staptest// futex (0x0, FUTEX_CMP_REQUEUE, 1, [[[[2147483647!!!!4294967295]]]], 0x0, 0) = NNNN
81 #else
82 //staptest// futex (0x0, FUTEX_CMP_REQUEUE, 1, 4294967295, 0x0, 0) = NNNN
83 #endif
84 #endif
85
86 __futex(NULL, FUTEX_CMP_REQUEUE, 1, NULL, (int *)-1, 0);
87 #ifdef __s390__
88 //staptest// futex (0x0, FUTEX_CMP_REQUEUE, 1, 0, 0x[7]?[f]+, 0) = NNNN
89 #else
90 //staptest// futex (0x0, FUTEX_CMP_REQUEUE, 1, 0, 0x[f]+, 0) = NNNN
91 #endif
92
93 __futex(NULL, FUTEX_CMP_REQUEUE, 1, NULL, NULL, -1);
94 //staptest// futex (0x0, FUTEX_CMP_REQUEUE, 1, 0, 0x0, -1) = NNNN
95
96 /* Since the futex argstr is different based on the operation,
97 * test several variants. */
98
99 #ifdef FUTEX_WAKE_BITSET
100 __futex(NULL, FUTEX_WAKE_BITSET, 1, NULL, NULL, -1);
101 #ifdef __s390__
102 //staptest// futex (0x0, FUTEX_WAKE_BITSET, 1, 0x[7]?[f]+) = NNNN
103 #else
104 //staptest// futex (0x0, FUTEX_WAKE_BITSET, 1, 0x[f]+) = NNNN
105 #endif
106 #endif
107
108 __futex(NULL, FUTEX_WAIT, 1, &t, NULL, 0);
109 //staptest// futex (0x0, FUTEX_WAIT, 1, \[0.000000789\]) = NNNN
110
111 #ifdef FUTEX_WAIT_BITSET
112 __futex(NULL, FUTEX_WAIT_BITSET, 1, &t, NULL, 0xbeef);
113 //staptest// futex (0x0, FUTEX_WAIT_BITSET, 1, \[0.000000789\], 0xbeef) = NNNN
114 #endif
115
116 __futex(NULL, FUTEX_REQUEUE, 1, (struct timespec *)2, NULL, -1);
117 //staptest// futex (0x0, FUTEX_REQUEUE, 1, 2, 0x0) = NNNN
118
119 __futex(NULL, FUTEX_CMP_REQUEUE, 1, (struct timespec *)2, (int *)3, -1);
120 //staptest// futex (0x0, FUTEX_CMP_REQUEUE, 1, 2, 0x3, -1) = NNNN
121
122 __futex(NULL, FUTEX_WAKE_OP, 1, (struct timespec *)2, (int *)3,
123 FUTEX_OP(FUTEX_OP_SET, 2, FUTEX_OP_CMP_EQ, 1));
124 //staptest// futex (0x0, FUTEX_WAKE_OP, 1, 2, 0x3, {FUTEX_OP_SET, 2, FUTEX_OP_CMP_EQ, 1}) = NNNN
125
126 #ifdef FUTEX_WAIT_REQUEUE_PI
127 __futex(NULL, FUTEX_WAIT_REQUEUE_PI, 1, &t, NULL, 0);
128 //staptest// futex (0x0, FUTEX_WAIT_REQUEUE_PI, 1, \[0.000000789\], 0x0) = NNNN
129 #endif
130
131 return 0;
132 }