2 * Copyright (C) 2011 Red Hat, Inc., Frederic Weisbecker <fweisbec@redhat.com>
4 * Licensed under the terms of the GNU GPL License version 2
6 * Selftests for breakpoints (and more generally the do_debug() path) in x86.
10 #include <sys/ptrace.h>
17 #include <sys/types.h>
21 /* Breakpoint access modes */
28 static pid_t child_pid
;
31 * Ensures the child and parent are always "talking" about
32 * the same test sequence. (ie: that we haven't forgotten
33 * to call check_trapped() somewhere).
37 static void set_breakpoint_addr(void *addr
, int n
)
41 ret
= ptrace(PTRACE_POKEUSER
, child_pid
,
42 offsetof(struct user
, u_debugreg
[n
]), addr
);
44 perror("Can't set breakpoint addr\n");
49 static void toggle_breakpoint(int n
, int type
, int len
,
50 int local
, int global
, int set
)
55 unsigned long vdr7
, dr7
;
84 dr7
= ptrace(PTRACE_PEEKUSER
, child_pid
,
85 offsetof(struct user
, u_debugreg
[7]), 0);
87 vdr7
= (xlen
| xtype
) << 16;
104 ret
= ptrace(PTRACE_POKEUSER
, child_pid
,
105 offsetof(struct user
, u_debugreg
[7]), dr7
);
107 perror("Can't set dr7");
112 /* Dummy variables to test read/write accesses */
113 static unsigned long long dummy_var
[4];
115 /* Dummy functions to test execution accesses */
116 static void dummy_func(void) { }
117 static void dummy_func1(void) { }
118 static void dummy_func2(void) { }
119 static void dummy_func3(void) { }
121 static void (*dummy_funcs
[])(void) = {
130 static void check_trapped(void)
133 * If we haven't trapped, wake up the parent
134 * so that it notices the failure.
137 kill(getpid(), SIGUSR1
);
143 static void write_var(int len
)
145 char *pcval
; short *psval
; int *pival
; long long *plval
;
148 for (i
= 0; i
< 4; i
++) {
151 pcval
= (char *)&dummy_var
[i
];
155 psval
= (short *)&dummy_var
[i
];
159 pival
= (int *)&dummy_var
[i
];
163 plval
= (long long *)&dummy_var
[i
];
164 *plval
= 0xffffffffffffffffLL
;
171 static void read_var(int len
)
173 char cval
; short sval
; int ival
; long long lval
;
176 for (i
= 0; i
< 4; i
++) {
179 cval
= *(char *)&dummy_var
[i
];
182 sval
= *(short *)&dummy_var
[i
];
185 ival
= *(int *)&dummy_var
[i
];
188 lval
= *(long long *)&dummy_var
[i
];
196 * Do the r/w/x accesses to trigger the breakpoints. And run
199 static void trigger_tests(void)
201 int len
, local
, global
, i
;
205 ret
= ptrace(PTRACE_TRACEME
, 0, NULL
, 0);
207 perror("Can't be traced?\n");
211 /* Wake up father so that it sets up the first test */
212 kill(getpid(), SIGUSR1
);
214 /* Test instruction breakpoints */
215 for (local
= 0; local
< 2; local
++) {
216 for (global
= 0; global
< 2; global
++) {
217 if (!local
&& !global
)
220 for (i
= 0; i
< 4; i
++) {
227 /* Test write watchpoints */
228 for (len
= 1; len
<= sizeof(long); len
<<= 1) {
229 for (local
= 0; local
< 2; local
++) {
230 for (global
= 0; global
< 2; global
++) {
231 if (!local
&& !global
)
238 /* Test read/write watchpoints (on read accesses) */
239 for (len
= 1; len
<= sizeof(long); len
<<= 1) {
240 for (local
= 0; local
< 2; local
++) {
241 for (global
= 0; global
< 2; global
++) {
242 if (!local
&& !global
)
257 kill(getpid(), SIGUSR1
);
260 static void check_success(const char *msg
)
266 /* Wait for the child to SIGTRAP */
271 if (WSTOPSIG(status
) == SIGTRAP
) {
272 child_nr_tests
= ptrace(PTRACE_PEEKDATA
, child_pid
,
274 if (child_nr_tests
== nr_tests
)
276 if (ptrace(PTRACE_POKEDATA
, child_pid
, &trapped
, 1)) {
277 perror("Can't poke\n");
284 printf("%s [%s]\n", msg
, msg2
);
287 static void launch_instruction_breakpoints(char *buf
, int local
, int global
)
291 for (i
= 0; i
< 4; i
++) {
292 set_breakpoint_addr(dummy_funcs
[i
], i
);
293 toggle_breakpoint(i
, BP_X
, 1, local
, global
, 1);
294 ptrace(PTRACE_CONT
, child_pid
, NULL
, 0);
295 sprintf(buf
, "Test breakpoint %d with local: %d global: %d",
298 toggle_breakpoint(i
, BP_X
, 1, local
, global
, 0);
302 static void launch_watchpoints(char *buf
, int mode
, int len
,
303 int local
, int global
)
305 const char *mode_str
;
313 for (i
= 0; i
< 4; i
++) {
314 set_breakpoint_addr(&dummy_var
[i
], i
);
315 toggle_breakpoint(i
, mode
, len
, local
, global
, 1);
316 ptrace(PTRACE_CONT
, child_pid
, NULL
, 0);
317 sprintf(buf
, "Test %s watchpoint %d with len: %d local: "
318 "%d global: %d", mode_str
, i
, len
, local
, global
);
320 toggle_breakpoint(i
, mode
, len
, local
, global
, 0);
324 /* Set the breakpoints and check the child successfully trigger them */
325 static void launch_tests(void)
328 int len
, local
, global
, i
;
330 /* Instruction breakpoints */
331 for (local
= 0; local
< 2; local
++) {
332 for (global
= 0; global
< 2; global
++) {
333 if (!local
&& !global
)
335 launch_instruction_breakpoints(buf
, local
, global
);
339 /* Write watchpoint */
340 for (len
= 1; len
<= sizeof(long); len
<<= 1) {
341 for (local
= 0; local
< 2; local
++) {
342 for (global
= 0; global
< 2; global
++) {
343 if (!local
&& !global
)
345 launch_watchpoints(buf
, BP_W
, len
,
351 /* Read-Write watchpoint */
352 for (len
= 1; len
<= sizeof(long); len
<<= 1) {
353 for (local
= 0; local
< 2; local
++) {
354 for (global
= 0; global
< 2; global
++) {
355 if (!local
&& !global
)
357 launch_watchpoints(buf
, BP_RW
, len
,
364 ptrace(PTRACE_CONT
, child_pid
, NULL
, 0);
365 check_success("Test icebp");
368 ptrace(PTRACE_CONT
, child_pid
, NULL
, 0);
369 check_success("Test int 3 trap");
371 ptrace(PTRACE_CONT
, child_pid
, NULL
, 0);
374 int main(int argc
, char **argv
)