Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 274 lines (227 sloc) 7.985 kB
43b260e @nelhage Import stub ptrace infrastructure.
authored
1 #include <sys/ptrace.h>
84114a0 @ingramj Fix a compiler error in ptrace.c
ingramj authored
2 #include <sys/types.h>
43b260e @nelhage Import stub ptrace infrastructure.
authored
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
13 #include <stddef.h>
43b260e @nelhage Import stub ptrace infrastructure.
authored
14
648ff26 @nelhage Start to separate ptrace.c into a library.
authored
15 #include "ptrace.h"
16
da5c7fa @nelhage Define some ptrace constants on platforms where ptrace.h is missing t…
authored
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
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
38
39
c2c4701 @nelhage ptrace: Make error handling consistent.
authored
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
46 int ptrace_wait(struct ptrace_child *child);
43b260e @nelhage Import stub ptrace infrastructure.
authored
47
1fb6ce8 @nelhage Clean up ptrace infrastructure slightly.
authored
48 int ptrace_attach_child(struct ptrace_child *child, pid_t pid) {
c2c4701 @nelhage ptrace: Make error handling consistent.
authored
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
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
58 memset(child, 0, sizeof child);
59 child->pid = pid;
60
c2c4701 @nelhage ptrace: Make error handling consistent.
authored
61 if (ptrace_wait(child) < 0)
62 goto detach;
1fb6ce8 @nelhage Clean up ptrace infrastructure slightly.
authored
63
c2c4701 @nelhage ptrace: Make error handling consistent.
authored
64 if (ptrace_command(child, PTRACE_SETOPTIONS, 0,
65 PTRACE_O_TRACESYSGOOD|PTRACE_O_TRACEFORK) < 0)
1fb6ce8 @nelhage Clean up ptrace infrastructure slightly.
authored
66 goto detach;
67
c2c4701 @nelhage ptrace: Make error handling consistent.
authored
68 return 0;
1fb6ce8 @nelhage Clean up ptrace infrastructure slightly.
authored
69
70 detach:
c2c4701 @nelhage ptrace: Make error handling consistent.
authored
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
74 }
75
76 int ptrace_detach_child(struct ptrace_child *child) {
c2c4701 @nelhage ptrace: Make error handling consistent.
authored
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
81 }
82
83 int ptrace_wait(struct ptrace_child *child) {
c2c4701 @nelhage ptrace: Make error handling consistent.
authored
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
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
91 int sig = WSTOPSIG(child->status);
92 if (sig & 0x80) {
1fb6ce8 @nelhage Clean up ptrace infrastructure slightly.
authored
93 child->state = (child->state == ptrace_at_syscall) ?
94 ptrace_after_syscall : ptrace_at_syscall;
95 } else {
9e91662 @nelhage Checkpoint working version.
authored
96 if (sig == SIGTRAP && (((child->status >> 8) & PTRACE_EVENT_FORK) == PTRACE_EVENT_FORK))
c2c4701 @nelhage ptrace: Make error handling consistent.
authored
97 ptrace_command(child, PTRACE_GETEVENTMSG, 0, &child->forked_pid);
9e91662 @nelhage Checkpoint working version.
authored
98 if (child->state != ptrace_at_syscall)
99 child->state = ptrace_stopped;
065cb54 @nelhage Handle attaching to processes that are already session leaders.
authored
100 if (sig != SIGSTOP && sig != SIGTRAP && sig != SIGCHLD && sig != SIGHUP && sig != SIGCONT) {
c2c4701 @nelhage ptrace: Make error handling consistent.
authored
101 child->error = EAGAIN;
95e1d51 @nelhage ptrace.c: Don't spin forever if the child gets a weird signal.
authored
102 return -1;
c2c4701 @nelhage ptrace: Make error handling consistent.
authored
103 }
43b260e @nelhage Import stub ptrace infrastructure.
authored
104 }
1fb6ce8 @nelhage Clean up ptrace infrastructure slightly.
authored
105 } else {
c2c4701 @nelhage ptrace: Make error handling consistent.
authored
106 child->error = EINVAL;
1fb6ce8 @nelhage Clean up ptrace infrastructure slightly.
authored
107 return -1;
108 }
109 return 0;
110 }
111
112 int ptrace_advance_to_state(struct ptrace_child *child,
113 enum child_state desired) {
114 int err;
115 while(child->state != desired) {
116 switch(desired) {
117 case ptrace_at_syscall:
118 case ptrace_after_syscall:
c2c4701 @nelhage ptrace: Make error handling consistent.
authored
119 err = ptrace_command(child, PTRACE_SYSCALL, 0, 0);
1fb6ce8 @nelhage Clean up ptrace infrastructure slightly.
authored
120 break;
121 case ptrace_running:
c2c4701 @nelhage ptrace: Make error handling consistent.
authored
122 return ptrace_command(child, PTRACE_CONT, 0, 0);
1fb6ce8 @nelhage Clean up ptrace infrastructure slightly.
authored
123 case ptrace_stopped:
124 err = kill(child->pid, SIGSTOP);
c2c4701 @nelhage ptrace: Make error handling consistent.
authored
125 if (err < 0)
126 child->error = errno;
43b260e @nelhage Import stub ptrace infrastructure.
authored
127 break;
1fb6ce8 @nelhage Clean up ptrace infrastructure slightly.
authored
128 default:
c2c4701 @nelhage ptrace: Make error handling consistent.
authored
129 child->error = EINVAL;
1fb6ce8 @nelhage Clean up ptrace infrastructure slightly.
authored
130 return -1;
131 }
132 if (err < 0)
133 return err;
c2c4701 @nelhage ptrace: Make error handling consistent.
authored
134 if(ptrace_wait(child) < 0)
135 return -1;
43b260e @nelhage Import stub ptrace infrastructure.
authored
136 }
1fb6ce8 @nelhage Clean up ptrace infrastructure slightly.
authored
137 return 0;
43b260e @nelhage Import stub ptrace infrastructure.
authored
138 }
139
35d826e @nelhage Implement memcpy to/from child.
authored
140
141 static void reset_user_struct(struct user *user) {
c84bdba @nelhage Refactor architecture-dependent headers slightly.
authored
142 arch_fixup_ip(user);
35d826e @nelhage Implement memcpy to/from child.
authored
143 user->regs.reg_ax = user->regs.orig_ax;
144 }
145
1fb6ce8 @nelhage Clean up ptrace infrastructure slightly.
authored
146 int ptrace_save_regs(struct ptrace_child *child) {
c2c4701 @nelhage ptrace: Make error handling consistent.
authored
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
153 }
154
155 int ptrace_restore_regs(struct ptrace_child *child) {
c2c4701 @nelhage ptrace: Make error handling consistent.
authored
156 return ptrace_command(child, PTRACE_SETREGS, 0, &child->user);
1fb6ce8 @nelhage Clean up ptrace infrastructure slightly.
authored
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
164 unsigned long rv;
c2c4701 @nelhage ptrace: Make error handling consistent.
authored
165 if (ptrace_advance_to_state(child, ptrace_at_syscall) < 0)
166 return -1;
43b260e @nelhage Import stub ptrace infrastructure.
authored
167
168 #define setreg(r, v) do { \
c2c4701 @nelhage ptrace: Make error handling consistent.
authored
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
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
183 if (ptrace_advance_to_state(child, ptrace_after_syscall) < 0)
184 return -1;
43b260e @nelhage Import stub ptrace infrastructure.
authored
185
c84bdba @nelhage Refactor architecture-dependent headers slightly.
authored
186 rv = ptrace_command(child, PTRACE_PEEKUSER,
187 offsetof(struct user, regs.syscall_rv));
c2c4701 @nelhage ptrace: Make error handling consistent.
authored
188 if (child->error)
189 return -1;
35d826e @nelhage Implement memcpy to/from child.
authored
190
191 setreg(reg_ip, child->user.regs.reg_ip);
192
193 #undef setreg
194
195 return rv;
43b260e @nelhage Import stub ptrace infrastructure.
authored
196 }
197
fff1db5 @nelhage const fix for memcpy_to_child
authored
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
199 unsigned long scratch;
200
201 while (n >= sizeof(unsigned long)) {
c2c4701 @nelhage ptrace: Make error handling consistent.
authored
202 if (ptrace_command(child, PTRACE_POKEDATA, dst, *((unsigned long*)src)) < 0)
203 return -1;
35d826e @nelhage Implement memcpy to/from child.
authored
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
210 scratch = ptrace_command(child, PTRACE_PEEKDATA, dst);
211 if (child->error)
35d826e @nelhage Implement memcpy to/from child.
authored
212 return -1;
213 memcpy(&scratch, src, n);
c2c4701 @nelhage ptrace: Make error handling consistent.
authored
214 if (ptrace_command(child, PTRACE_POKEDATA, dst, scratch) < 0)
215 return -1;
35d826e @nelhage Implement memcpy to/from child.
authored
216 }
217
218 return 0;
43b260e @nelhage Import stub ptrace infrastructure.
authored
219 }
220
35d826e @nelhage Implement memcpy to/from child.
authored
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
225 scratch = ptrace_command(child, PTRACE_PEEKDATA, src);
226 if (child->error) return -1;
35d826e @nelhage Implement memcpy to/from child.
authored
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
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
248
648ff26 @nelhage Start to separate ptrace.c into a library.
authored
249 #ifdef BUILD_PTRACE_MAIN
43b260e @nelhage Import stub ptrace infrastructure.
authored
250 int main(int argc, char **argv) {
1fb6ce8 @nelhage Clean up ptrace infrastructure slightly.
authored
251 struct ptrace_child child;
43b260e @nelhage Import stub ptrace infrastructure.
authored
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
260 assert(!ptrace_attach_child(&child, pid));
261 assert(!ptrace_save_regs(&child));
43b260e @nelhage Import stub ptrace infrastructure.
authored
262
1fb6ce8 @nelhage Clean up ptrace infrastructure slightly.
authored
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
266
1fb6ce8 @nelhage Clean up ptrace infrastructure slightly.
authored
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
270
271 return 0;
272 }
648ff26 @nelhage Start to separate ptrace.c into a library.
authored
273 #endif
Something went wrong with that request. Please try again.