-
Notifications
You must be signed in to change notification settings - Fork 380
/
Copy pathDebugger.cpp
317 lines (274 loc) · 7.95 KB
/
Debugger.cpp
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/user.h>
#ifdef __linux__
#include <sys/ptrace.h>
#include <experimental/filesystem>
#endif
#include "NativeCore.hpp"
#ifdef __linux__
namespace fs = std::experimental::filesystem;
#endif
int ualarm(unsigned int milliseconds)
{
struct itimerval nval = { 0 };
nval.it_value.tv_sec = milliseconds / 1000;
nval.it_value.tv_usec = static_cast<long int>(milliseconds % 1000) * 1000;
struct itimerval oval;
if (setitimer(ITIMER_REAL, &nval, &oval) < 0)
return 0;
else
return oval.it_value.tv_sec;
}
pid_t waitpid_timeout(pid_t pid, int* status, int options, int timeoutInMilliseconds, bool& timedOut)
{
#ifdef __linux__
struct sigaction sig = {};
sig.sa_flags = 0;
sig.sa_handler = [](int) {};
sigfillset(&sig.sa_mask);
sigaction(SIGALRM, &sig, nullptr);
ualarm(timeoutInMilliseconds);
auto res = waitpid(pid, status, options);
if (res == -1 && errno == EINTR)
{
timedOut = true;
}
else
{
ualarm(0); // Cancel the alarm.
timedOut = false;
}
return res;
#else
return 0;
#endif
}
pid_t waitpid_timeout(int* status, int timeoutInMilliseconds, bool& timedOut)
{
return waitpid_timeout(-1, status, 0, timeoutInMilliseconds, timedOut);
}
extern "C" bool RC_CallConv AttachDebuggerToProcess(RC_Pointer id)
{
//TODO: Attach to all threads.
#ifdef __linux__
ptrace(PTRACE_ATTACH, static_cast<pid_t>(reinterpret_cast<intptr_t>(id)), nullptr, nullptr);
waitpid(-1, nullptr, 0);
ptrace(PTRACE_CONT, static_cast<pid_t>(reinterpret_cast<intptr_t>(id)), nullptr, nullptr);
#endif
return false;
}
extern "C" void RC_CallConv DetachDebuggerFromProcess(RC_Pointer id)
{
//TODO: Detach to all threads.
#ifdef __linux__
ptrace(PTRACE_DETACH, static_cast<pid_t>(reinterpret_cast<intptr_t>(id)), nullptr, nullptr);
#endif
}
extern "C" bool RC_CallConv AwaitDebugEvent(DebugEvent* evt, int timeoutInMilliseconds)
{
#ifdef __linux__
int status;
bool timedOut;
auto tid = waitpid_timeout(&status, timeoutInMilliseconds, timedOut);
if (timedOut)
{
return false;
}
auto result = false;
if (tid > 0)
{
evt->ThreadId = reinterpret_cast<RC_Pointer>(static_cast<intptr_t>(tid));
siginfo_t si;
if (ptrace(PTRACE_GETSIGINFO, tid, nullptr, &si) == 0)
{
if (si.si_signo == SIGTRAP)
{
struct user_regs_struct regs;
if (ptrace(PTRACE_GETREGS, tid, nullptr, ®s) == 0)
{
DebugRegister6 dr6;
dr6.Value = ptrace(PTRACE_PEEKUSER, tid, offsetof(struct user, u_debugreg[6]), nullptr);
// Check if breakpoint was a hardware breakpoint.
if (dr6.DR0)
{
evt->ExceptionInfo.CausedBy = HardwareBreakpointRegister::Dr0;
}
else if (dr6.DR1)
{
evt->ExceptionInfo.CausedBy = HardwareBreakpointRegister::Dr1;
}
else if (dr6.DR2)
{
evt->ExceptionInfo.CausedBy = HardwareBreakpointRegister::Dr2;
}
else if (dr6.DR3)
{
evt->ExceptionInfo.CausedBy = HardwareBreakpointRegister::Dr3;
}
else
{
evt->ExceptionInfo.CausedBy = HardwareBreakpointRegister::InvalidRegister;
}
// Copy registers.
auto& reg = evt->ExceptionInfo.Registers;
#ifdef RECLASSNET64
reg.Rax = reinterpret_cast<RC_Pointer>(regs.rax);
reg.Rbx = reinterpret_cast<RC_Pointer>(regs.rbx);
reg.Rcx = reinterpret_cast<RC_Pointer>(regs.rcx);
reg.Rdx = reinterpret_cast<RC_Pointer>(regs.rdx);
reg.Rdi = reinterpret_cast<RC_Pointer>(regs.rdi);
reg.Rsi = reinterpret_cast<RC_Pointer>(regs.rsi);
reg.Rsp = reinterpret_cast<RC_Pointer>(regs.rsp);
reg.Rbp = reinterpret_cast<RC_Pointer>(regs.rbp);
reg.Rip = reinterpret_cast<RC_Pointer>(regs.rip);
reg.R8 = reinterpret_cast<RC_Pointer>(regs.r8);
reg.R9 = reinterpret_cast<RC_Pointer>(regs.r9);
reg.R10 = reinterpret_cast<RC_Pointer>(regs.r10);
reg.R11 = reinterpret_cast<RC_Pointer>(regs.r11);
reg.R12 = reinterpret_cast<RC_Pointer>(regs.r12);
reg.R13 = reinterpret_cast<RC_Pointer>(regs.r13);
reg.R14 = reinterpret_cast<RC_Pointer>(regs.r14);
reg.R15 = reinterpret_cast<RC_Pointer>(regs.r15);
#else
reg.Eax = reinterpret_cast<RC_Pointer>(regs.eax);
reg.Ebx = reinterpret_cast<RC_Pointer>(regs.ebx);
reg.Ecx = reinterpret_cast<RC_Pointer>(regs.ecx);
reg.Edx = reinterpret_cast<RC_Pointer>(regs.edx);
reg.Edi = reinterpret_cast<RC_Pointer>(regs.edi);
reg.Esi = reinterpret_cast<RC_Pointer>(regs.esi);
reg.Esp = reinterpret_cast<RC_Pointer>(regs.esp);
reg.Ebp = reinterpret_cast<RC_Pointer>(regs.ebp);
reg.Eip = reinterpret_cast<RC_Pointer>(regs.eip);
#endif
result = true;
}
}
if (result == false)
{
ptrace(PTRACE_CONT, tid, nullptr, si.si_signo);
}
}
}
return result;
#else
return false;
#endif
}
extern "C" void RC_CallConv HandleDebugEvent(DebugEvent* evt)
{
#ifdef __linux__
auto tid = static_cast<pid_t>(reinterpret_cast<intptr_t>(evt->ThreadId));
siginfo_t si;
if (ptrace(PTRACE_GETSIGINFO, tid, 0, &si) == 0)
{
auto signal = 0;
switch (evt->ContinueStatus)
{
case DebugContinueStatus::Handled:
signal = 0;
break;
case DebugContinueStatus::NotHandled:
signal = si.si_signo;
break;
}
if (signal == SIGSTOP)
{
signal = 0;
}
ptrace(PTRACE_CONT, tid, nullptr, signal);
}
#endif
}
extern "C" bool RC_CallConv SetHardwareBreakpoint(RC_Pointer id, RC_Pointer address, HardwareBreakpointRegister reg, HardwareBreakpointTrigger type, HardwareBreakpointSize size, bool set)
{
#ifdef __linux__
if (reg == HardwareBreakpointRegister::InvalidRegister)
{
return false;
}
intptr_t addressValue = 0;
auto accessValue = 0;
auto lengthValue = 0;
if (set)
{
addressValue = reinterpret_cast<intptr_t>(address);
if (type == HardwareBreakpointTrigger::Execute)
accessValue = 0;
else if (type == HardwareBreakpointTrigger::Access)
accessValue = 3;
else if (type == HardwareBreakpointTrigger::Write)
accessValue = 1;
if (size == HardwareBreakpointSize::Size1)
lengthValue = 0;
else if (size == HardwareBreakpointSize::Size2)
lengthValue = 1;
else if (size == HardwareBreakpointSize::Size4)
lengthValue = 3;
else if (size == HardwareBreakpointSize::Size8)
lengthValue = 2;
}
auto tasksPath = fs::path("/proc") / std::to_string(reinterpret_cast<intptr_t>(id)) / "task";
if (fs::is_directory(tasksPath))
{
for (auto& d : fs::directory_iterator(tasksPath))
{
if (fs::is_directory(d))
{
auto taskPath = d.path();
auto name = taskPath.filename().string();
if (is_number(name))
{
auto tid = parse_type<size_t>(name);
// Stop the thread. TODO: Check if the thread was already paused.
for (int i = 0; i < 10; ++i)
{
kill(tid, SIGSTOP);
bool timedOut;
waitpid_timeout(tid, nullptr, 0, 100, timedOut);
if (!timedOut)
{
break;
}
}
DebugRegister7 dr7;
dr7.Value = ptrace(PTRACE_PEEKUSER, tid, offsetof(struct user, u_debugreg[7]), nullptr);
intptr_t registerAddress;
switch (reg)
{
case HardwareBreakpointRegister::Dr0:
registerAddress = offsetof(struct user, u_debugreg[0]);
dr7.G0 = true;
dr7.RW0 = accessValue;
dr7.Len0 = lengthValue;
break;
case HardwareBreakpointRegister::Dr1:
registerAddress = offsetof(struct user, u_debugreg[1]);
dr7.G1 = true;
dr7.RW1 = accessValue;
dr7.Len1 = lengthValue;
break;
case HardwareBreakpointRegister::Dr2:
registerAddress = offsetof(struct user, u_debugreg[2]);
dr7.G2 = true;
dr7.RW2 = accessValue;
dr7.Len2 = lengthValue;
break;
case HardwareBreakpointRegister::Dr3:
registerAddress = offsetof(struct user, u_debugreg[3]);
dr7.G3 = true;
dr7.RW3 = accessValue;
dr7.Len3 = lengthValue;
break;
}
ptrace(PTRACE_POKEUSER, tid, registerAddress, addressValue);
ptrace(PTRACE_POKEUSER, tid, offsetof(struct user, u_debugreg[7]), dr7.Value);
ptrace(PTRACE_CONT, tid, nullptr, nullptr);
}
}
}
}
#endif
return true;
}