-
Notifications
You must be signed in to change notification settings - Fork 37
/
ministrace.c
236 lines (206 loc) · 5.69 KB
/
ministrace.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
#include <sys/ptrace.h>
#include <bits/types.h>
#include <sys/user.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <assert.h>
#include <string.h>
#include "syscalls.h"
#include "syscallents.h"
/* cheap trick for reading syscall number / return value. */
#ifdef __amd64__
#define eax rax
#define orig_eax orig_rax
#else
#endif
#define offsetof(a, b) __builtin_offsetof(a,b)
#define get_reg(child, name) __get_reg(child, offsetof(struct user, regs.name))
long __get_reg(pid_t child, int off) {
long val = ptrace(PTRACE_PEEKUSER, child, off);
assert(errno == 0);
return val;
}
int wait_for_syscall(pid_t child) {
int status;
while (1) {
ptrace(PTRACE_SYSCALL, child, 0, 0);
waitpid(child, &status, 0);
if (WIFSTOPPED(status) && WSTOPSIG(status) & 0x80)
return 0;
if (WIFEXITED(status))
return 1;
fprintf(stderr, "[stopped %d (%x)]\n", status, WSTOPSIG(status));
}
}
const char *syscall_name(int scn) {
struct syscall_entry *ent;
static char buf[128];
if (scn <= MAX_SYSCALL_NUM) {
ent = &syscalls[scn];
if (ent->name)
return ent->name;
}
snprintf(buf, sizeof buf, "sys_%d", scn);
return buf;
}
long get_syscall_arg(pid_t child, int which) {
switch (which) {
#ifdef __amd64__
case 0: return get_reg(child, rdi);
case 1: return get_reg(child, rsi);
case 2: return get_reg(child, rdx);
case 3: return get_reg(child, r10);
case 4: return get_reg(child, r8);
case 5: return get_reg(child, r9);
#else
case 0: return get_reg(child, ebx);
case 1: return get_reg(child, ecx);
case 2: return get_reg(child, edx);
case 3: return get_reg(child, esi);
case 4: return get_reg(child, edi);
case 5: return get_reg(child, ebp);
#endif
default: return -1L;
}
}
char *read_string(pid_t child, unsigned long addr) {
char *val = malloc(4096);
int allocated = 4096;
int read = 0;
unsigned long tmp;
while (1) {
if (read + sizeof tmp > allocated) {
allocated *= 2;
val = realloc(val, allocated);
}
tmp = ptrace(PTRACE_PEEKDATA, child, addr + read);
if(errno != 0) {
val[read] = 0;
break;
}
memcpy(val + read, &tmp, sizeof tmp);
if (memchr(&tmp, 0, sizeof tmp) != NULL)
break;
read += sizeof tmp;
}
return val;
}
void print_syscall_args(pid_t child, int num) {
struct syscall_entry *ent = NULL;
int nargs = SYSCALL_MAXARGS;
int i;
char *strval;
if (num <= MAX_SYSCALL_NUM && syscalls[num].name) {
ent = &syscalls[num];
nargs = ent->nargs;
}
for (i = 0; i < nargs; i++) {
long arg = get_syscall_arg(child, i);
int type = ent ? ent->args[i] : ARG_PTR;
switch (type) {
case ARG_INT:
fprintf(stderr, "%ld", arg);
break;
case ARG_STR:
strval = read_string(child, arg);
fprintf(stderr, "\"%s\"", strval);
free(strval);
break;
default:
fprintf(stderr, "0x%lx", arg);
break;
}
if (i != nargs - 1)
fprintf(stderr, ", ");
}
}
void print_syscall(pid_t child, int syscall_req) {
int num;
num = get_reg(child, orig_eax);
assert(errno == 0);
fprintf(stderr, "%s(", syscall_name(num));
print_syscall_args(child, num);
fprintf(stderr, ") = ");
if( syscall_req <= MAX_SYSCALL_NUM) {
pause_on( num, syscall_req);
}
}
int pause_on(int syscall_req, int syscall) {
if(syscall == syscall_req)
do {
char buf[2];
fgets(buf, sizeof(buf), stdin); // waits until enter to continue
} while(0);
}
int do_trace(pid_t child, int syscall_req) {
int status;
int retval;
waitpid(child, &status, 0);
assert(WIFSTOPPED(status));
ptrace(PTRACE_SETOPTIONS, child, 0, PTRACE_O_TRACESYSGOOD);
while(1) {
if (wait_for_syscall(child) != 0)
break;
print_syscall(child, syscall_req);
if (wait_for_syscall(child) != 0)
break;
retval = get_reg(child, eax);
assert(errno == 0);
fprintf(stderr, "%d\n", retval);
}
return 0;
}
int do_child(int argc, char **argv) {
char *args [argc+1];
int i;
for (i=0;i<argc;i++)
args[i] = argv[i];
args[argc] = NULL;
ptrace(PTRACE_TRACEME);
kill(getpid(), SIGSTOP);
return execvp(args[0], args);
}
int main(int argc, char **argv) {
pid_t child;
int push = 1;
int syscall = -1;
if (argc < 2) {
fprintf(stderr, "Usage: %s [-s <syscall int>|-n <syscall name>] <program> <args>\n", argv[0]);
exit(1);
}
if(strcmp(argv[1], "-s") == 0) {
syscall = atoi(argv[2]);
if (syscall > MAX_SYSCALL_NUM || syscall < 0) {
fprintf(stderr, "Error: %s is an invalid syscall\n", argv[2]);
exit(1);
}
push = 3;
}
if(strcmp(argv[1], "-n") == 0) {
char *syscallname = argv[2];
struct syscall_entry *ent;
int i;
for(i = 0; i < sizeof(syscalls)/sizeof(*syscalls); i++) {
ent = &syscalls[i];
if(strcmp(syscallname, ent->name) == 0) {
syscall = i;
break;
}
}
if(syscall == -1) {
fprintf(stderr, "Error: %s is an invalid syscall\n", argv[2]);
exit(1);
}
push = 3;
}
child = fork();
if (child == 0) {
return do_child(argc-push, argv+push);
} else {
return do_trace(child, syscall);
}
}