-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathptrace.c
256 lines (209 loc) · 5.3 KB
/
ptrace.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
* Copyright (C) 2011-2012 IITiS PAN Gliwice <http://www.iitis.pl/>
* Author: Paweł Foremski <[email protected]>
* Licensed under GNU GPL v. 3
*/
#include "tracedump.h"
/** ptrace wrapper
* @retval >=0 success
* @retval <0 error, return value is (-1 * errno)
*/
static long _run_ptrace(enum __ptrace_request request, struct pid *sp,
void *addr, void *data,
const char *func)
{
long rc;
int pid = sp ? sp->pid : 0;
rc = ptrace(request, pid, addr, data);
/* rc equal -1 means error */
if (rc == -1) {
rc = -errno;
/* skip errors on some conditions */
if (errno != 0) {
if (request == PTRACE_DETACH)
goto ret;
} else {
if (request == PTRACE_PEEKDATA || request == PTRACE_PEEKUSER)
goto ret;
}
dbg(1, "%s(req %d, pid %d): %s\n", func, request, pid, strerror(errno));
} else {
dbg(5, "%s(req %d, pid %d)\n", func, request, pid);
}
ret:
return rc;
}
#define run_ptrace(a, b, c, d) _run_ptrace(a, b, ((void *) (c)), ((void *) (d)), __func__)
int ptrace_attach_pid(struct pid *sp, void (*cb)(struct pid *sp))
{
DIR *dh;
char buf[128];
struct dirent *de;
int pid;
struct pid *sp2;
snprintf(buf, sizeof buf, "/proc/%d/task", sp->pid);
dh = opendir(buf);
if (dh) {
while ((de = readdir(dh))) {
if (!isdigit(de->d_name[0]))
continue;
pid = atoi(de->d_name);
if (pid <= 0)
continue;
sp2 = pid_get(sp->td, pid);
run_ptrace(PTRACE_ATTACH, sp2, NULL, NULL);
if (ptrace_attach_child(sp2, cb) < 0)
return -1;
}
closedir(dh);
} else {
run_ptrace(PTRACE_ATTACH, sp, NULL, NULL);
ptrace_attach_child(sp, cb);
}
return 0;
}
int ptrace_attach_child(struct pid *sp, void (*cb)(struct pid *sp))
{
int rc, status;
FILE *fp;
char buf[128];
rc = waitpid(sp->pid, &status, __WALL);
if (rc == sp->pid && WIFSTOPPED(status) &&
(WSTOPSIG(status) == SIGSTOP || WSTOPSIG(status) == SIGTRAP)) {
/* read PID name */
snprintf(buf, sizeof buf, "/proc/%d/cmdline", sp->pid);
fp = fopen(buf, "r");
fgets(sp->cmdline, sizeof sp->cmdline, fp);
fclose(fp);
dbg(1, "attached to PID %d (%s)\n", sp->pid, sp->cmdline);
/* set ptrace options */
run_ptrace(PTRACE_SETOPTIONS, sp, NULL,
PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK | PTRACE_O_TRACECLONE | PTRACE_O_TRACESYSGOOD);
/* callback */
if (cb)
cb(sp);
ptrace_cont_syscall(sp, 0, false);
return 0;
} else {
dbg(1, "attaching PID %d failed\n", sp->pid);
return -1;
}
}
void ptrace_traceme(void)
{
run_ptrace(PTRACE_TRACEME, NULL, NULL, NULL);
}
int ptrace_wait(struct pid *sp, int *st)
{
int rc, status;
int pid = sp ? sp->pid : -1;
rc = waitpid(pid, &status, __WALL);
if (rc == -1) {
dbg(2, "wait(%d): %s\n", pid, strerror(errno));
return rc;
}
if (pid == -1)
pid = rc;
if (WIFEXITED(status))
dbg(3, "wait(%d): process exited with status %d\n", pid, WEXITSTATUS(status));
else if (WIFSIGNALED(status))
dbg(2, "wait(%d): process terminated with signal %d\n", pid, WTERMSIG(status));
else if (WIFSTOPPED(status)) {
switch (WSTOPSIG(status)) {
case SIGSTOP:
case SIGTRAP:
case SIGTRAPS:
break;
case SIGSEGV:
dbg(1, "wait(%d): process received SIGSEGV - segmentation fault (%s)\n",
pid, sp->cmdline);
break;
default:
dbg(5, "wait(%d): process received signal %d\n", pid, WSTOPSIG(status));
}
}
if (st)
*st = status;
return rc;
}
static inline void _ptrace_cont(bool syscall, struct pid *sp, unsigned long sig, bool w8)
{
int status;
while (1) {
run_ptrace(syscall ? PTRACE_SYSCALL : PTRACE_CONT, sp, NULL, (void *) sig);
if (!w8)
break;
ptrace_wait(sp, &status);
if (!WIFSTOPPED(status))
break;
sig = WSTOPSIG(status);
if (sig == SIGTRAP || sig == SIGTRAPS)
break;
}
}
void ptrace_cont(struct pid *sp, unsigned long sig, bool w8)
{
_ptrace_cont(false, sp, sig, w8);
}
void ptrace_cont_syscall(struct pid *sp, unsigned long sig, bool w8)
{
_ptrace_cont(true, sp, sig, w8);
}
void ptrace_detach(struct pid *sp, unsigned long sig)
{
int status;
/* are we lucky? */
if (run_ptrace(PTRACE_DETACH, sp, NULL, (void *) sig) == 0)
return;
/* no, the process is not detachable - try to stop it */
syscall(SYS_tgkill, pid_tgid(sp->pid), sp->pid, SIGSTOP);
ptrace_wait(sp, &status);
if (WIFSTOPPED(status)) {
switch (WSTOPSIG(status)) {
case SIGSTOP:
case SIGTRAP:
case SIGTRAPS:
run_ptrace(PTRACE_DETACH, sp, NULL, (void *) SIGCONT);
break;
default:
run_ptrace(PTRACE_DETACH, sp, NULL, (void *) WSTOPSIG(status));
}
} else {
run_ptrace(PTRACE_DETACH, sp, NULL, (void *) 0);
}
}
void ptrace_kill(struct pid *sp)
{
kill(sp->pid, SIGKILL);
}
void ptrace_read(struct pid *sp, unsigned long addr, void *vptr, int len)
{
int i , count;
uint32_t word;
unsigned long *ptr = (unsigned long *) vptr;
count = i = 0;
while (count < len) {
word = run_ptrace(PTRACE_PEEKTEXT, sp, addr + count, NULL);
count += 4;
ptr[i++] = word;
}
}
void ptrace_write(struct pid *sp, unsigned long addr, void *vptr, int len)
{
int i, count;
uint32_t *word;
word = (uint32_t *) vptr;
i = count = 0;
while (count < len) {
run_ptrace(PTRACE_POKETEXT, sp, addr + count, *word++);
count +=4;
}
}
void ptrace_getregs(struct pid *sp, struct user_regs_struct *regs)
{
run_ptrace(PTRACE_GETREGS, sp, NULL, regs);
}
void ptrace_setregs(struct pid *sp, struct user_regs_struct *regs)
{
run_ptrace(PTRACE_SETREGS, sp, NULL, regs);
}