-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathHardBreakPoint.h
322 lines (291 loc) · 9.07 KB
/
HardBreakPoint.h
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
318
319
320
321
322
#pragma once
#include <Windows.h>
#include <TlHelp32.h>
#include "phmap.h"
#include <vector>
class HardBreakPoint {
public:
struct BreakPoint {
int id;
void* original;
void* replacement;
};
struct DR7 {
unsigned int L0 : 1; // 局部断点0
unsigned int G0 : 1; // 全局断点0
unsigned int L1 : 1; // 局部断点1
unsigned int G1 : 1; // 全局断点1
unsigned int L2 : 1; // 局部断点2
unsigned int G2 : 1; // 全局断点2
unsigned int L3 : 1; // 局部断点3
unsigned int G3 : 1; // 全局断点3
unsigned int LE : 1; // 局部精确断点
unsigned int GE : 1; // 全局精确断点
unsigned int reserved1 : 1; // 保留位
unsigned int reserved2 : 1; // 保留位
unsigned int reserved3 : 1; // 保留位
unsigned int GD : 1;
unsigned int reserved4 : 2; // 保留位
unsigned int RW0 : 2; // 断点条件0
unsigned int LEN0 : 2; // 断点长度0
unsigned int RW1 : 2; // 断点条件1
unsigned int LEN1 : 2; // 断点长度1
unsigned int RW2 : 2; // 断点条件2
unsigned int LEN2 : 2; // 断点长度2
unsigned int RW3 : 2; // 断点条件3
unsigned int LEN3 : 2; // 断点长度3
static uint32_t DR7ToDWORD(const DR7& dr7) {
uint32_t value = 0;
value |= dr7.L0 << 0;
value |= dr7.G0 << 1;
value |= dr7.L1 << 2;
value |= dr7.G1 << 3;
value |= dr7.L2 << 4;
value |= dr7.G2 << 5;
value |= dr7.L3 << 6;
value |= dr7.G3 << 7;
value |= dr7.LE << 8;
value |= dr7.GE << 9;
value |= dr7.reserved1 << 10;
value |= dr7.reserved2 << 11;
value |= dr7.reserved3 << 12;
value |= dr7.GD << 13;
value |= dr7.reserved4 << 14;
value |= dr7.RW0 << 16;
value |= dr7.LEN0 << 18;
value |= dr7.RW1 << 20;
value |= dr7.LEN1 << 22;
value |= dr7.RW2 << 24;
value |= dr7.LEN2 << 26;
value |= dr7.RW3 << 28;
value |= dr7.LEN3 << 30;
return value;
}
};
HardBreakPoint() = delete;
static void Initialize() {
AddVectoredExceptionHandler(999999, VectoredExceptionHandler);
}
template<typename R, typename... Args>
static bool SetBreakPoint(R(*address)(Args...), R(*replacement)(Args...)) {
BreakPoint bp;
bp.original = address;
bp.replacement = replacement;
for (int i = 0; i < 4; i++) {
if (!bp_status[i]) {
bp.id = i;
breakpoints.insert({ address, bp });
bp_status[i] = true;
DWORD myId = GetCurrentThreadId();
auto threads = GetProcessThreads(GetCurrentProcessId());
for (auto thread : threads) {
if (thread != myId) {
HANDLE hThread = OpenThread(THREAD_ALL_ACCESS, FALSE, thread);
if (hThread == nullptr) {
continue;
}
SuspendThread(hThread);
if (breakpoints.size() <= 4) {
CONTEXT ctx = {};
ctx.ContextFlags = CONTEXT_ALL;
if (!GetThreadContext(hThread, &ctx)) {
ResumeThread(hThread);
CloseHandle(hThread);
continue;
}
switch (i) {
case 0:
ctx.Dr0 = reinterpret_cast<DWORD_PTR>(address);
dr7.L0 = 1;
dr7.RW0 = 0;
break;
case 1:
ctx.Dr1 = reinterpret_cast<DWORD_PTR>(address);
dr7.L1 = 1;
dr7.RW1 = 0;
break;
case 2:
ctx.Dr2 = reinterpret_cast<DWORD_PTR>(address);
dr7.L2 = 1;
dr7.RW2 = 0;
break;
case 3:
ctx.Dr3 = reinterpret_cast<DWORD_PTR>(address);
dr7.L3 = 1;
dr7.RW3 = 0;
break;
}
ctx.Dr7 = DR7::DR7ToDWORD(dr7);
ctx.ContextFlags = CONTEXT_ALL;
SetThreadContext(hThread, &ctx);
}
ResumeThread(hThread);
CloseHandle(hThread);
}
}
break;
}
}
return true;
}
template<typename R, typename... Args>
static bool RemoveBreakPoint(R(*address)(Args...)) {
BreakPoint bp = breakpoints[address];
DWORD myId = GetCurrentThreadId();
auto threads = GetProcessThreads(GetCurrentProcessId());
for (auto thread : threads) {
if (thread != myId) {
HANDLE hThread = OpenThread(THREAD_ALL_ACCESS, FALSE, thread);
if (hThread == nullptr) {
continue;
}
SuspendThread(hThread);
CONTEXT ctx = {};
ctx.ContextFlags = CONTEXT_ALL;
if (GetThreadContext(hThread, &ctx)) {
switch (bp.id) {
case 0: ctx.Dr0 = 0; break;
case 1: ctx.Dr1 = 0; break;
case 2: ctx.Dr2 = 0; break;
case 3: ctx.Dr3 = 0; break;
}
ctx.Dr7 = DR7::DR7ToDWORD(dr7);
ctx.ContextFlags = CONTEXT_ALL;
SetThreadContext(hThread, &ctx);
}
ResumeThread(hThread);
CloseHandle(hThread);
}
}
bp_status[bp.id] = false;
breakpoints.erase(address);
return true;
}
template<typename R, typename... Args>
static R CallOrigin(R(*func)(Args...), Args... args) {
for (auto [fst, snd] : breakpoints) {
if (snd.replacement == func) {
R(*originalFunc)(Args...) = reinterpret_cast<R(*)(Args...)>(snd.original);
R(*replacementFunc)(Args...) = reinterpret_cast<R(*)(Args...)>(snd.replacement);
if constexpr (std::is_void_v<R>) {
RemoveBreakPointThread(GetCurrentThread(), originalFunc);
originalFunc(args...);
SetBreakPointThread(GetCurrentThread(), originalFunc, replacementFunc);
return;
} else {
RemoveBreakPointThread(GetCurrentThread(), originalFunc);
R ret = originalFunc(args...);
SetBreakPointThread(GetCurrentThread(), originalFunc, replacementFunc);
return ret;
}
}
}
return R();
}
inline static DR7 dr7;
private:
inline static bool bp_status[] = {false, false, false, false};
inline static inline phmap::parallel_flat_hash_map<void*, BreakPoint, phmap::priv::hash_default_hash<void*>, phmap::priv::hash_default_eq<void*>, phmap::priv::Allocator<std::pair<void*, BreakPoint>>, 4, std::mutex> breakpoints;
template<typename R, typename... Args>
static bool RemoveBreakPointThread(HANDLE hThread, R(*address)(Args...)) {
BreakPoint bp = breakpoints[address];
CONTEXT ctx = {};
ctx.ContextFlags = CONTEXT_ALL;
if (GetThreadContext(hThread, &ctx)) {
switch (bp.id) {
case 0: ctx.Dr0 = 0; break;
case 1: ctx.Dr1 = 0; break;
case 2: ctx.Dr2 = 0; break;
case 3: ctx.Dr3 = 0; break;
}
ctx.Dr7 = DR7::DR7ToDWORD(dr7);
ctx.ContextFlags = CONTEXT_ALL;
SetThreadContext(hThread, &ctx);
bp_status[bp.id] = false;
breakpoints.erase(address);
return true;
}
return false;
}
template<typename R, typename... Args>
static bool SetBreakPointThread(HANDLE hThread, R(*address)(Args...), R(*replacement)(Args...)) {
if (breakpoints.size() < 4) {
BreakPoint bp;
bp.original = address;
bp.replacement = replacement;
CONTEXT ctx = {};
ctx.ContextFlags = CONTEXT_ALL;
GetThreadContext(hThread, &ctx);
for (int i = 0; i < 4; i++) {
if (!bp_status[i]) {
bp.id = i;
breakpoints.insert({ address, bp });
bp_status[i] = true;
switch (i) {
case 0:
ctx.Dr0 = reinterpret_cast<DWORD_PTR>(address);
dr7.L0 = 1;
dr7.RW0 = 0;
break;
case 1:
ctx.Dr1 = reinterpret_cast<DWORD_PTR>(address);
dr7.L1 = 1;
dr7.RW1 = 0;
break;
case 2:
ctx.Dr2 = reinterpret_cast<DWORD_PTR>(address);
dr7.L2 = 1;
dr7.RW2 = 0;
break;
case 3:
ctx.Dr3 = reinterpret_cast<DWORD_PTR>(address);
dr7.L3 = 1;
dr7.RW3 = 0;
break;
}
ctx.Dr7 = DR7::DR7ToDWORD(dr7);
ctx.ContextFlags = CONTEXT_ALL;
SetThreadContext(hThread, &ctx);
break;
}
}
}
return false;
}
static std::vector<DWORD> GetProcessThreads(DWORD processID) {
std::vector<DWORD> threadIDs;
HANDLE hThreadSnap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
if (hThreadSnap == INVALID_HANDLE_VALUE) {
return threadIDs;
}
THREADENTRY32 te32;
te32.dwSize = sizeof(THREADENTRY32);
if (Thread32First(hThreadSnap, &te32)) {
do {
if (te32.th32OwnerProcessID == processID) {
threadIDs.push_back(te32.th32ThreadID);
}
} while (Thread32Next(hThreadSnap, &te32));
}
CloseHandle(hThreadSnap);
return threadIDs;
}
static auto WINAPI VectoredExceptionHandler(PEXCEPTION_POINTERS exception) -> LONG {
if (exception->ExceptionRecord->ExceptionCode == EXCEPTION_SINGLE_STEP) {
#ifdef _WIN64
if (breakpoints.contains(exception->ExceptionRecord->ExceptionAddress)) {
exception->ContextRecord->Dr7 = DR7::DR7ToDWORD(dr7);
exception->ContextRecord->Rip = reinterpret_cast<DWORD64>(breakpoints[exception->ExceptionRecord->ExceptionAddress].replacement);
return EXCEPTION_CONTINUE_EXECUTION;
}
#else
if (breakpoints.contains(exception->ExceptionRecord->ExceptionAddress)) {
exception->ContextRecord->Dr7 = DR7::DR7ToDWORD(dr7);
exception->ContextRecord->Eip = reinterpret_cast<DWORD>(breakpoints[exception->ExceptionRecord->ExceptionAddress].replacement);
return EXCEPTION_CONTINUE_EXECUTION;
}
#endif
}
return EXCEPTION_CONTINUE_SEARCH;
}
};