-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathdebugger.cc
More file actions
169 lines (130 loc) · 4.18 KB
/
debugger.cc
File metadata and controls
169 lines (130 loc) · 4.18 KB
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
/**
* Hardware debugger for memory.js
* A lot of the hardware debugging code is based on ReClass.NET
* https://github.com/ReClassNET/ReClass.NET
*/
#include <node.h>
#include <windows.h>
#include <TlHelp32.h>
#include <vector>
#include "debugger.h"
#include "module.h"
bool debugger::attach(DWORD processId, bool killOnDetatch) {
if (DebugActiveProcess(processId) == 0) {
return false;
}
DebugSetProcessKillOnExit(killOnDetatch);
return true;
}
bool debugger::detatch(DWORD processId) {
return DebugActiveProcessStop(processId) != 0;
}
bool debugger::setHardwareBreakpoint(DWORD processId, DWORD64 address, Register reg, int trigger, int size) {
const char* errorMessage = "";
std::vector<THREADENTRY32> threads = module::getThreads(0, &errorMessage);
if (strcmp(errorMessage, "")) {
return false;
}
for (std::vector<THREADENTRY32>::size_type i = 0; i != threads.size(); i++) {
if (threads[i].th32OwnerProcessID != processId) {
continue;
}
HANDLE threadHandle = OpenThread(THREAD_SUSPEND_RESUME | THREAD_GET_CONTEXT | THREAD_SET_CONTEXT, false, threads[i].th32ThreadID);
if (threadHandle == 0) {
continue;
}
SuspendThread(threadHandle);
CONTEXT context = { 0 };
context.ContextFlags = CONTEXT_DEBUG_REGISTERS;
GetThreadContext(threadHandle, &context);
DebugRegister7 dr7;
dr7.Value = context.Dr7;
if (reg == Register::DR0) {
context.Dr0 = address;
dr7.G0 = true;
dr7.RW0 = trigger;
dr7.Len0 = size;
}
if (reg == Register::DR1) {
context.Dr1 = address;
dr7.G1 = true;
dr7.RW1 = trigger;
dr7.Len1 = size;
}
if (reg == Register::DR2) {
context.Dr2 = address;
dr7.G2 = true;
dr7.RW2 = trigger;
dr7.Len2 = size;
}
if (reg == Register::DR3) {
context.Dr3 = address;
dr7.G3 = true;
dr7.RW3 = trigger;
dr7.Len3 = size;
}
context.Dr7 = dr7.Value;
SetThreadContext(threadHandle, &context);
ResumeThread(threadHandle);
CloseHandle(threadHandle);
return true;
}
return false;
}
bool debugger::awaitDebugEvent(DWORD millisTimeout, DebugEvent *info) {
DEBUG_EVENT debugEvent = {};
if (WaitForDebugEvent(&debugEvent, millisTimeout) == 0) {
return false;
}
if (debugEvent.dwDebugEventCode == CREATE_PROCESS_DEBUG_EVENT) {
CloseHandle(debugEvent.u.CreateProcessInfo.hFile);
}
if (debugEvent.dwDebugEventCode == LOAD_DLL_DEBUG_EVENT) {
CloseHandle(debugEvent.u.LoadDll.hFile);
}
if (debugEvent.dwDebugEventCode == EXCEPTION_DEBUG_EVENT) {
EXCEPTION_DEBUG_INFO exception = debugEvent.u.Exception;
info->processId = debugEvent.dwProcessId;
info->threadId = debugEvent.dwThreadId;
info->exceptionAddress = exception.ExceptionRecord.ExceptionAddress;
info->exceptionCode = exception.ExceptionRecord.ExceptionCode;
info->exceptionFlags = exception.ExceptionRecord.ExceptionFlags;
HANDLE handle = OpenThread(THREAD_GET_CONTEXT, false, debugEvent.dwThreadId);
if (handle == 0) {
return false;
}
CONTEXT context = {};
context.ContextFlags = CONTEXT_CONTROL | CONTEXT_INTEGER | CONTEXT_DEBUG_REGISTERS;
GetThreadContext(handle, &context);
DebugRegister6 dr6;
dr6.Value = context.Dr6;
if (dr6.DR0) {
info->hardwareRegister = Register::DR0;
}
if (dr6.DR1) {
info->hardwareRegister = Register::DR1;
}
if (dr6.DR2) {
info->hardwareRegister = Register::DR2;
}
if (dr6.DR3) {
info->hardwareRegister = Register::DR3;
}
if (!dr6.DR0 && !dr6.DR1 && !dr6.DR2 && !dr6.DR3) {
info->hardwareRegister = Register::Invalid;
}
CloseHandle(handle);
} else {
ContinueDebugEvent(debugEvent.dwProcessId, debugEvent.dwThreadId, DBG_CONTINUE);
}
return true;
}
bool debugger::handleDebugEvent(DWORD processId, DWORD threadId) {
return ContinueDebugEvent(processId, threadId, DBG_CONTINUE);
// if (status == DebugContinueStatus::Handled) {
// return ContinueDebugEvent(processId, threadId, DBG_CONTINUE) != 0;
// }
// if (status == DebugContinueStatus::NotHandled) {
// return ContinueDebugEvent(processId, threadId, DBG_EXCEPTION_NOT_HANDLED) != 0;
// }
}