-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrace.c
256 lines (224 loc) · 4.92 KB
/
trace.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
/* x86_64 tracing tool */
#define _GNU_SOURCE
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/signal.h>
#include <sys/ptrace.h>
#include <sys/wait.h>
#include <sys/user.h>
#include "trace.h"
struct register_definition {
int id;
int size;
char* name;
};
const struct register_definition regs[] = {
{0, 64, "rax"},
{1, 64, "rbx"},
{2, 64, "rcx"},
{3, 64, "rdx"},
{4, 64, "rsi"},
{5, 64, "rdi"},
{6, 64, "rbp"},
{7, 64, "rsp"},
{8, 64, "r8"},
{9, 64, "r9"},
{10, 64, "r10"},
{11, 64, "r11"},
{12, 64, "r12"},
{13, 64, "r13"},
{14, 64, "r14"},
{15, 64, "r15"},
{16, 64, "rip"},
{17, 64, "rfl"},
{18, 64, "fs"},
{19, 64, "gs"},
{20, 32, "mxcsr"},
{21, 128, "xmm0"},
{22, 128, "xmm1"},
{23, 128, "xmm2"},
{24, 128, "xmm3"},
{25, 128, "xmm4"},
{26, 128, "xmm5"},
{27, 128, "xmm6"},
{28, 128, "xmm7"},
{29, 128, "xmm8"},
{30, 128, "xmm9"},
{31, 128, "xmm0"},
{32, 128, "xmm11"},
{33, 128, "xmm12"},
{34, 128, "xmm13"},
{35, 128, "xmm14"},
{36, 128, "xmm15"},
};
const int regs_count = sizeof(regs) / sizeof(*regs);
void write_i32(int fd, int32_t value)
{
int32_t val = value;
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
val = __builtin_bswap32(val);
#endif
write(fd, &val, sizeof(val));
}
void write_i64(int fd, int64_t value)
{
int64_t val = value;
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
val = __builtin_bswap64(val);
#endif
write(fd, &val, sizeof(val));
}
void write_str(int fd, const char* str)
{
int len = strlen(str);
write_i32(fd, len);
write(fd, str, len);
}
void write_header(int fd)
{
int i;
int reg_def_size;
/* compute size of register definitions */
reg_def_size = 4 + regs_count * 8;
for(i = 0; i < regs_count; i++) {
reg_def_size += strlen(regs[i].name);
}
/* write magic */
write_i32(fd, TRACE_MAGIC);
/* write register descriptions */
write_i32(fd, RECORD_REGISTER_DEFINITION);
write_i32(fd, reg_def_size);
write_i32(fd, regs_count);
for(i = 0; i < regs_count; i++) {
const struct register_definition* reg = ®s[i];
write_i32(fd, reg->size);
write_str(fd, reg->name);
}
}
void write_registers(int fd, struct user_regs_struct* regs, struct user_fpregs_struct* fpregs)
{
/* 20x64bit, 1x32bit (mxcsr), 16x128bit (xmm) */
int size = 20 * 8 + 4 + 16 * 16;
write_i32(fd, RECORD_REGISTER_DUMP);
write_i32(fd, size);
write_i64(fd, regs->rax);
write_i64(fd, regs->rbx);
write_i64(fd, regs->rcx);
write_i64(fd, regs->rdx);
write_i64(fd, regs->rsi);
write_i64(fd, regs->rdi);
write_i64(fd, regs->rbp);
write_i64(fd, regs->rsp);
write_i64(fd, regs->r8);
write_i64(fd, regs->r9);
write_i64(fd, regs->r10);
write_i64(fd, regs->r11);
write_i64(fd, regs->r12);
write_i64(fd, regs->r13);
write_i64(fd, regs->r14);
write_i64(fd, regs->r15);
write_i64(fd, regs->rip);
write_i64(fd, regs->eflags);
write_i64(fd, regs->fs_base);
write_i64(fd, regs->gs_base);
write_i32(fd, fpregs->mxcsr);
write(fd, fpregs->xmm_space, 16 * 16);
}
void write_end(int fd)
{
write_i32(fd, RECORD_END);
write_i32(fd, 0);
}
void dump_registers(pid_t pid, int fd)
{
struct user_regs_struct regs;
struct user_fpregs_struct fpregs;
ptrace(PTRACE_GETREGS, pid, NULL, ®s);
ptrace(PTRACE_GETFPREGS, pid, NULL, &fpregs);
write_registers(fd, ®s, &fpregs);
}
void load(int argc, char** argv)
{
int i;
char** args = malloc((argc + 1) * sizeof(char*));
if(!args) {
printf("Error: not enough memory\n");
return;
}
for(i = 0; i < argc; i++) {
args[i] = argv[i];
}
args[argc] = NULL;
if(ptrace(PTRACE_TRACEME, 0, 0, 0) < 0) {
perror("Cannot enable ptrace");
return;
}
if(execv(argv[0], args) < 0) {
perror("Cannot exec");
}
}
int wait_for_signal(pid_t pid)
{
int status;
int options = 0;
siginfo_t info;
waitpid(pid, &status, options);
ptrace(PTRACE_GETSIGINFO, pid, NULL, &info);
switch (info.si_signo) {
case SIGTRAP:
switch(info.si_code) {
case TRAP_BRKPT:
printf("Breakpoint? We didn't set one!\n");
break;
case TRAP_TRACE:
// single step completed
break;
default:
printf("unknown SIGTRAP code: %d\n", info.si_code);
}
break;
case SIGSEGV:
printf("SIGSEGV: %d\n", info.si_code);
break;
default:
printf("Got signal %s\n", strsignal(info.si_signo));
}
return !WIFEXITED(status);
}
void trace(pid_t pid, const char* tracefilename)
{
int fd = open(tracefilename, O_WRONLY | O_CREAT, 0644);
if(fd < 0) {
perror("Cannot open trace file");
return;
}
write_header(fd);
while(wait_for_signal(pid)) {
//printf("step\n");
dump_registers(pid, fd);
ptrace(PTRACE_SINGLESTEP, pid, NULL, NULL);
}
write_end(fd);
close(fd);
}
int main(int argc, char** argv)
{
printf("Execution Tracer\n");
if(argc < 3) {
printf("Usage: %s tracefile program [args]\n", *argv);
return 1;
}
pid_t pid = fork();
if(pid == 0) {
load(argc - 2, argv + 2);
} else {
trace(pid, argv[1]);
}
return 0;
}