-
Notifications
You must be signed in to change notification settings - Fork 0
/
watchpoint.c
164 lines (138 loc) · 4.01 KB
/
watchpoint.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <assert.h>
#include <pthread.h>
#include <signal.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/user.h>
#include <sys/wait.h>
#include <unistd.h>
#define fatal(_msg, ...) \
do { \
fprintf(stderr, "Fatal error: " _msg "\n", ## __VA_ARGS__); \
exit(1); \
} while (0)
#define USER_WORD_DR(x) offsetof(struct user, u_debugreg[x])
typedef enum {
TRAP_EXEC = 0x00, TRAP_WRITE = 0x01, TRAP_READWRITE = 0x03
} WatchType;
typedef enum {
BYTES_1 = 0x00, BYTES_2 = 0x01, BYTES_4 = 0x03, BYTES_8 = 0x02
} WatchBytes;
struct DebugControl {
uintptr_t dr0_local : 1;
uintptr_t dr0_global : 1;
uintptr_t dr1_local : 1;
uintptr_t dr1_global : 1;
uintptr_t dr2_local : 1;
uintptr_t dr2_global : 1;
uintptr_t dr3_local : 1;
uintptr_t dr3_global : 1;
uintptr_t ignored : 8;
WatchType dr0_type : 2;
WatchBytes dr0_len : 2;
WatchType dr1_type : 2;
WatchBytes dr1_len : 2;
WatchType dr2_type : 2;
WatchBytes dr2_len : 2;
WatchType dr3_type : 2;
WatchBytes dr3_len : 2;
};
static int mut = 0;
static int
read_mut(void)
{
return mut;
}
static void
write_mut(int x)
{
mut = x;
}
static void*
thread(void* unused)
{
read_mut();
write_mut(24);
return NULL;
}
static void
child(void)
{
pthread_t t;
if (ptrace(PTRACE_TRACEME, 0, 0, 0)) {
fatal("TRACEME in child");
}
kill(getpid(), SIGSTOP);
read_mut();
write_mut(42);
pthread_create(&t, NULL, thread, NULL);
pthread_join(t, NULL);
exit(0);
}
static void
watch(pid_t tid, void* addr, WatchType what, WatchBytes len)
{
struct DebugControl ctl = { 0 };
if (ptrace(PTRACE_POKEUSER, tid, USER_WORD_DR(6), 0)) {
fatal("Reset debug status");
}
if (ptrace(PTRACE_POKEUSER, tid, USER_WORD_DR(0), addr)) {
fatal("Set debug reg 0");
}
ctl.dr0_local = 1;
ctl.dr0_type = what;
ctl.dr0_len = len;
if (ptrace(PTRACE_POKEUSER, tid, USER_WORD_DR(7), ctl)) {
fatal("Set debug ctl reg");
}
}
int
main(void)
{
void* read_mut_fn = read_mut;
void* write_mut_fn = write_mut;
int ret, status;
struct user_regs_struct regs;
void* ip;
pid_t c = fork();
pid_t t;
if (0 == c) {
child();
}
ret = waitpid(c, &status, 0);
assert(c == ret && WIFSTOPPED(status) && SIGSTOP == WSTOPSIG(status));
ptrace(PTRACE_SETOPTIONS, c, NULL,
PTRACE_O_TRACECLONE | PTRACE_O_TRACESYSGOOD);
watch(c, &mut, TRAP_WRITE, BYTES_4);
ptrace(PTRACE_SYSCALL, c, NULL, NULL);
ret = waitpid(-1, &status, __WALL);
printf("task %d stopped with status %#x\n", ret, status);
assert(c == ret && WIFSTOPPED(status) && SIGTRAP == WSTOPSIG(status));
ptrace(PTRACE_GETREGS, c, 0, ®s);
ip = (void*)regs.eip;
printf(" -> hit write watchpoint at %p\n", ip);
assert(read_mut_fn < write_mut_fn && write_mut_fn <= ip);
ptrace(PTRACE_CONT, c, NULL, NULL);
ret = waitpid(-1, &status, __WALL);
printf("task %d stopped with status %#x\n", ret, status);
assert(c == ret && 0x3057f == status/*PTRACE_EVENT_CLONE*/);
ptrace(PTRACE_GETEVENTMSG, c, NULL, &t);
ret = waitpid(t, &status, __WALL);
printf("task %d stopped with status %#x\n", ret, status);
watch(c, &mut, TRAP_READWRITE, BYTES_4);
watch(t, &mut, TRAP_READWRITE, BYTES_4);
ptrace(PTRACE_CONT, c, NULL, NULL);
ptrace(PTRACE_CONT, t, NULL, NULL);
ret = waitpid(-1, &status, __WALL);
printf("task %d stopped with status %#x\n", ret, status);
assert(t == ret && WIFSTOPPED(status) && SIGTRAP == WSTOPSIG(status));
ptrace(PTRACE_GETREGS, t, 0, ®s);
ip = (void*)regs.eip;
printf(" -> hit read/write watchpoint at %p\n", ip);
assert(read_mut_fn <= ip && read_mut_fn < write_mut_fn);
return 0;
}