Skip to content
Newer
Older
100644 274 lines (227 sloc) 7.8 KB
43b260e @nelhage Import stub ptrace infrastructure.
authored Jan 16, 2011
1 #include <sys/ptrace.h>
84114a0 @ingramj Fix a compiler error in ptrace.c
ingramj authored Jan 22, 2011
2 #include <sys/types.h>
43b260e @nelhage Import stub ptrace infrastructure.
authored Jan 17, 2011
3 #include <sys/user.h>
4 #include <sys/wait.h>
5 #include <unistd.h>
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <errno.h>
9 #include <string.h>
10 #include <sys/syscall.h>
11 #include <sys/mman.h>
12 #include <assert.h>
988d4d1 @nelhage Get offsetof from stddef.h.
authored Jan 22, 2011
13 #include <stddef.h>
43b260e @nelhage Import stub ptrace infrastructure.
authored Jan 17, 2011
14
648ff26 @nelhage Start to separate ptrace.c into a library.
authored Jan 17, 2011
15 #include "ptrace.h"
16
da5c7fa @nelhage Define some ptrace constants on platforms where ptrace.h is missing t…
authored Jan 24, 2011
17 /*
18 * RHEL 5's kernel supports these flags, but their libc doesn't ship a ptrace.h
19 * that defines them. Define them here, and if our kernel doesn't support them,
20 * we'll find out when PTRACE_SETOPTIONS fails.
21 */
22 #ifndef PTRACE_O_TRACESYSGOOD
23 #define PTRACE_O_TRACESYSGOOD 0x00000001
24 #endif
25
26 #ifndef PTRACE_O_TRACEFORK
27 #define PTRACE_O_TRACEFORK 0x00000002
28 #endif
29
30 #ifndef PTRACE_EVENT_FORK
31 #define PTRACE_EVENT_FORK 1
32 #endif
33
35d826e @nelhage Implement memcpy to/from child.
authored Jan 17, 2011
34 #define min(x, y) ({ \
35 typeof(x) _min1 = (x); \
36 typeof(y) _min2 = (y); \
37 _min1 < _min2 ? _min1 : _min2; })
43b260e @nelhage Import stub ptrace infrastructure.
authored Jan 17, 2011
38
39
c2c4701 @nelhage ptrace: Make error handling consistent.
authored Jan 21, 2011
40 static long __ptrace_command(struct ptrace_child *child, enum __ptrace_request req,
41 void *, void*);
42
43 #define ptrace_command(cld, req, ...) _ptrace_command(cld, req, ## __VA_ARGS__, NULL, NULL)
44 #define _ptrace_command(cld, req, addr, data, ...) __ptrace_command((cld), (req), (void*)(addr), (void*)(data))
45
1fb6ce8 @nelhage Clean up ptrace infrastructure slightly.
authored Jan 17, 2011
46 int ptrace_wait(struct ptrace_child *child);
43b260e @nelhage Import stub ptrace infrastructure.
authored Jan 17, 2011
47
1fb6ce8 @nelhage Clean up ptrace infrastructure slightly.
authored Jan 17, 2011
48 int ptrace_attach_child(struct ptrace_child *child, pid_t pid) {
c2c4701 @nelhage ptrace: Make error handling consistent.
authored Jan 22, 2011
49 memset(child, 0, sizeof child);
50 child->pid = pid;
51 if (ptrace_command(child, PTRACE_ATTACH) < 0)
52 return -1;
9e91662 @nelhage Checkpoint working version.
authored Jan 17, 2011
53
54 return ptrace_finish_attach(child, pid);
55 }
56
57 int ptrace_finish_attach(struct ptrace_child *child, pid_t pid) {
1fb6ce8 @nelhage Clean up ptrace infrastructure slightly.
authored Jan 17, 2011
58 memset(child, 0, sizeof child);
59 child->pid = pid;
60
c2c4701 @nelhage ptrace: Make error handling consistent.
authored Jan 22, 2011
61 if (ptrace_wait(child) < 0)
62 goto detach;
1fb6ce8 @nelhage Clean up ptrace infrastructure slightly.
authored Jan 17, 2011
63
c2c4701 @nelhage ptrace: Make error handling consistent.
authored Jan 22, 2011
64 if (ptrace_command(child, PTRACE_SETOPTIONS, 0,
65 PTRACE_O_TRACESYSGOOD|PTRACE_O_TRACEFORK) < 0)
1fb6ce8 @nelhage Clean up ptrace infrastructure slightly.
authored Jan 17, 2011
66 goto detach;
67
c2c4701 @nelhage ptrace: Make error handling consistent.
authored Jan 22, 2011
68 return 0;
1fb6ce8 @nelhage Clean up ptrace infrastructure slightly.
authored Jan 17, 2011
69
70 detach:
c2c4701 @nelhage ptrace: Make error handling consistent.
authored Jan 22, 2011
71 /* Don't clobber child->error */
72 ptrace(PTRACE_DETACH, child->pid, 0, 0);
73 return -1;
1fb6ce8 @nelhage Clean up ptrace infrastructure slightly.
authored Jan 17, 2011
74 }
75
76 int ptrace_detach_child(struct ptrace_child *child) {
c2c4701 @nelhage ptrace: Make error handling consistent.
authored Jan 22, 2011
77 if (ptrace_command(child, PTRACE_DETACH, 0, 0) < 0)
78 return -1;
79 child->state = ptrace_detached;
80 return 0;
1fb6ce8 @nelhage Clean up ptrace infrastructure slightly.
authored Jan 17, 2011
81 }
82
83 int ptrace_wait(struct ptrace_child *child) {
c2c4701 @nelhage ptrace: Make error handling consistent.
authored Jan 22, 2011
84 if (waitpid(child->pid, &child->status, 0) < 0) {
85 child->error = errno;
86 return -1;
87 }
1fb6ce8 @nelhage Clean up ptrace infrastructure slightly.
authored Jan 17, 2011
88 if (WIFEXITED(child->status) || WIFSIGNALED(child->status)) {
89 child->state = ptrace_exited;
90 } else if(WIFSTOPPED(child->status)) {
9e91662 @nelhage Checkpoint working version.
authored Jan 18, 2011
91 int sig = WSTOPSIG(child->status);
92 if (sig & 0x80) {
1fb6ce8 @nelhage Clean up ptrace infrastructure slightly.
authored Jan 17, 2011
93 child->state = (child->state == ptrace_at_syscall) ?
94 ptrace_after_syscall : ptrace_at_syscall;
95 } else {
9e91662 @nelhage Checkpoint working version.
authored Jan 18, 2011
96 if (sig == SIGTRAP && (((child->status >> 8) & PTRACE_EVENT_FORK) == PTRACE_EVENT_FORK))
c2c4701 @nelhage ptrace: Make error handling consistent.
authored Jan 22, 2011
97 ptrace_command(child, PTRACE_GETEVENTMSG, 0, &child->forked_pid);
9e91662 @nelhage Checkpoint working version.
authored Jan 18, 2011
98 if (child->state != ptrace_at_syscall)
99 child->state = ptrace_stopped;
43b260e @nelhage Import stub ptrace infrastructure.
authored Jan 17, 2011
100 }
1fb6ce8 @nelhage Clean up ptrace infrastructure slightly.
authored Jan 17, 2011
101 } else {
c2c4701 @nelhage ptrace: Make error handling consistent.
authored Jan 22, 2011
102 child->error = EINVAL;
1fb6ce8 @nelhage Clean up ptrace infrastructure slightly.
authored Jan 17, 2011
103 return -1;
104 }
105 return 0;
106 }
107
108 int ptrace_advance_to_state(struct ptrace_child *child,
109 enum child_state desired) {
110 int err;
111 while(child->state != desired) {
112 switch(desired) {
113 case ptrace_after_syscall:
0046dc7 @nelhage ptrace.c: Improve the wait / advance_to_state logic somewhat.
authored Jan 25, 2011
114 case ptrace_at_syscall:
115 if (WIFSTOPPED(child->status) && WSTOPSIG(child->status) == SIGSEGV) {
116 child->error = EAGAIN;
117 return -1;
118 }
c2c4701 @nelhage ptrace: Make error handling consistent.
authored Jan 22, 2011
119 err = ptrace_command(child, PTRACE_SYSCALL, 0, 0);
1fb6ce8 @nelhage Clean up ptrace infrastructure slightly.
authored Jan 17, 2011
120 break;
121 case ptrace_running:
c2c4701 @nelhage ptrace: Make error handling consistent.
authored Jan 22, 2011
122 return ptrace_command(child, PTRACE_CONT, 0, 0);
1fb6ce8 @nelhage Clean up ptrace infrastructure slightly.
authored Jan 17, 2011
123 case ptrace_stopped:
124 err = kill(child->pid, SIGSTOP);
c2c4701 @nelhage ptrace: Make error handling consistent.
authored Jan 22, 2011
125 if (err < 0)
126 child->error = errno;
43b260e @nelhage Import stub ptrace infrastructure.
authored Jan 17, 2011
127 break;
1fb6ce8 @nelhage Clean up ptrace infrastructure slightly.
authored Jan 17, 2011
128 default:
c2c4701 @nelhage ptrace: Make error handling consistent.
authored Jan 22, 2011
129 child->error = EINVAL;
1fb6ce8 @nelhage Clean up ptrace infrastructure slightly.
authored Jan 17, 2011
130 return -1;
131 }
132 if (err < 0)
133 return err;
c2c4701 @nelhage ptrace: Make error handling consistent.
authored Jan 22, 2011
134 if(ptrace_wait(child) < 0)
135 return -1;
43b260e @nelhage Import stub ptrace infrastructure.
authored Jan 17, 2011
136 }
1fb6ce8 @nelhage Clean up ptrace infrastructure slightly.
authored Jan 17, 2011
137 return 0;
43b260e @nelhage Import stub ptrace infrastructure.
authored Jan 17, 2011
138 }
139
35d826e @nelhage Implement memcpy to/from child.
authored Jan 18, 2011
140
141 static void reset_user_struct(struct user *user) {
142 user->regs.reg_ip -= 2;
143 user->regs.reg_ax = user->regs.orig_ax;
144 }
145
1fb6ce8 @nelhage Clean up ptrace infrastructure slightly.
authored Jan 17, 2011
146 int ptrace_save_regs(struct ptrace_child *child) {
c2c4701 @nelhage ptrace: Make error handling consistent.
authored Jan 22, 2011
147 if (ptrace_advance_to_state(child, ptrace_at_syscall) < 0)
148 return -1;
149 if (ptrace_command(child, PTRACE_GETREGS, 0, &child->user) < 0)
150 return -1;
151 reset_user_struct(&child->user);
152 return 0;
1fb6ce8 @nelhage Clean up ptrace infrastructure slightly.
authored Jan 17, 2011
153 }
154
155 int ptrace_restore_regs(struct ptrace_child *child) {
c2c4701 @nelhage ptrace: Make error handling consistent.
authored Jan 22, 2011
156 return ptrace_command(child, PTRACE_SETREGS, 0, &child->user);
1fb6ce8 @nelhage Clean up ptrace infrastructure slightly.
authored Jan 17, 2011
157 }
158
159 unsigned long ptrace_remote_syscall(struct ptrace_child *child,
160 unsigned long sysno,
161 unsigned long p0, unsigned long p1,
162 unsigned long p2, unsigned long p3,
163 unsigned long p4, unsigned long p5) {
35d826e @nelhage Implement memcpy to/from child.
authored Jan 18, 2011
164 unsigned long rv;
c2c4701 @nelhage ptrace: Make error handling consistent.
authored Jan 22, 2011
165 if (ptrace_advance_to_state(child, ptrace_at_syscall) < 0)
166 return -1;
43b260e @nelhage Import stub ptrace infrastructure.
authored Jan 17, 2011
167
168 #define setreg(r, v) do { \
c2c4701 @nelhage ptrace: Make error handling consistent.
authored Jan 22, 2011
169 if (ptrace_command(child, PTRACE_POKEUSER, \
170 offsetof(struct user, regs.r), \
171 (v)) < 0) \
172 return -1; \
43b260e @nelhage Import stub ptrace infrastructure.
authored Jan 17, 2011
173 } while(0)
174
175 setreg(orig_ax, sysno);
176 setreg(syscall_arg0, p0);
177 setreg(syscall_arg1, p1);
178 setreg(syscall_arg2, p2);
179 setreg(syscall_arg3, p3);
180 setreg(syscall_arg4, p4);
181 setreg(syscall_arg5, p5);
182
c2c4701 @nelhage ptrace: Make error handling consistent.
authored Jan 22, 2011
183 if (ptrace_advance_to_state(child, ptrace_after_syscall) < 0)
184 return -1;
43b260e @nelhage Import stub ptrace infrastructure.
authored Jan 17, 2011
185
c2c4701 @nelhage ptrace: Make error handling consistent.
authored Jan 22, 2011
186 rv = ptrace_command(child, PTRACE_PEEKUSER, offsetof(struct user, regs.reg_ax));
187 if (child->error)
188 return -1;
35d826e @nelhage Implement memcpy to/from child.
authored Jan 18, 2011
189
190 setreg(reg_ax, child->user.regs.orig_ax);
191 setreg(reg_ip, child->user.regs.reg_ip);
192
193 #undef setreg
194
195 return rv;
43b260e @nelhage Import stub ptrace infrastructure.
authored Jan 17, 2011
196 }
197
fff1db5 @nelhage const fix for memcpy_to_child
authored Jan 17, 2011
198 int ptrace_memcpy_to_child(struct ptrace_child *child, child_addr_t dst, const void *src, size_t n) {
35d826e @nelhage Implement memcpy to/from child.
authored Jan 18, 2011
199 unsigned long scratch;
200
201 while (n >= sizeof(unsigned long)) {
c2c4701 @nelhage ptrace: Make error handling consistent.
authored Jan 22, 2011
202 if (ptrace_command(child, PTRACE_POKEDATA, dst, *((unsigned long*)src)) < 0)
203 return -1;
35d826e @nelhage Implement memcpy to/from child.
authored Jan 18, 2011
204 dst += sizeof(unsigned long);
205 src += sizeof(unsigned long);
206 n -= sizeof(unsigned long);
207 }
208
209 if (n) {
c2c4701 @nelhage ptrace: Make error handling consistent.
authored Jan 22, 2011
210 scratch = ptrace_command(child, PTRACE_PEEKDATA, dst);
211 if (child->error)
35d826e @nelhage Implement memcpy to/from child.
authored Jan 18, 2011
212 return -1;
213 memcpy(&scratch, src, n);
c2c4701 @nelhage ptrace: Make error handling consistent.
authored Jan 22, 2011
214 if (ptrace_command(child, PTRACE_POKEDATA, dst, scratch) < 0)
215 return -1;
35d826e @nelhage Implement memcpy to/from child.
authored Jan 18, 2011
216 }
217
218 return 0;
43b260e @nelhage Import stub ptrace infrastructure.
authored Jan 17, 2011
219 }
220
35d826e @nelhage Implement memcpy to/from child.
authored Jan 18, 2011
221 int ptrace_memcpy_from_child(struct ptrace_child *child, void *dst, child_addr_t src, size_t n) {
222 unsigned long scratch;
223
224 while (n) {
c2c4701 @nelhage ptrace: Make error handling consistent.
authored Jan 22, 2011
225 scratch = ptrace_command(child, PTRACE_PEEKDATA, src);
226 if (child->error) return -1;
35d826e @nelhage Implement memcpy to/from child.
authored Jan 18, 2011
227 memcpy(dst, &scratch, min(n, sizeof(unsigned long)));
228
229 dst += sizeof(unsigned long);
230 src += sizeof(unsigned long);
231 if (n >= sizeof(unsigned long))
232 n -= sizeof(unsigned long);
233 else
234 n = 0;
235 }
236 return 0;
237 }
238
c2c4701 @nelhage ptrace: Make error handling consistent.
authored Jan 22, 2011
239 static long __ptrace_command(struct ptrace_child *child, enum __ptrace_request req,
240 void *addr, void *data) {
241 long rv;
242 errno = 0;
243 rv = ptrace(req, child->pid, addr, data);
244 child->error = errno;
245 return rv;
246 }
247
35d826e @nelhage Implement memcpy to/from child.
authored Jan 18, 2011
248
648ff26 @nelhage Start to separate ptrace.c into a library.
authored Jan 17, 2011
249 #ifdef BUILD_PTRACE_MAIN
43b260e @nelhage Import stub ptrace infrastructure.
authored Jan 17, 2011
250 int main(int argc, char **argv) {
1fb6ce8 @nelhage Clean up ptrace infrastructure slightly.
authored Jan 17, 2011
251 struct ptrace_child child;
43b260e @nelhage Import stub ptrace infrastructure.
authored Jan 17, 2011
252 pid_t pid;
253
254 if (argc < 2) {
255 printf("Usage: %s pid\n", argv[0]);
256 return 1;
257 }
258 pid = atoi(argv[1]);
259
1fb6ce8 @nelhage Clean up ptrace infrastructure slightly.
authored Jan 17, 2011
260 assert(!ptrace_attach_child(&child, pid));
261 assert(!ptrace_save_regs(&child));
43b260e @nelhage Import stub ptrace infrastructure.
authored Jan 17, 2011
262
1fb6ce8 @nelhage Clean up ptrace infrastructure slightly.
authored Jan 17, 2011
263 printf("mmap = %lx\n", ptrace_remote_syscall(&child, mmap_syscall, 0,
264 4096, PROT_READ|PROT_WRITE,
265 MAP_ANONYMOUS|MAP_PRIVATE, 0, 0));
43b260e @nelhage Import stub ptrace infrastructure.
authored Jan 17, 2011
266
1fb6ce8 @nelhage Clean up ptrace infrastructure slightly.
authored Jan 17, 2011
267 reset_user_struct(&child.user);
268 assert(!ptrace_restore_regs(&child));
269 assert(!ptrace_detach_child(&child));
43b260e @nelhage Import stub ptrace infrastructure.
authored Jan 17, 2011
270
271 return 0;
272 }
648ff26 @nelhage Start to separate ptrace.c into a library.
authored Jan 17, 2011
273 #endif
Something went wrong with that request. Please try again.