33
44#include " NativeCore.hpp"
55
6+ struct DebugRegister6
7+ {
8+ union
9+ {
10+ uintptr_t Value;
11+ struct
12+ {
13+ unsigned DR0 : 1 ;
14+ unsigned DR1 : 1 ;
15+ unsigned DR2 : 1 ;
16+ unsigned DR3 : 1 ;
17+ unsigned Reserved : 9 ;
18+ unsigned BD : 1 ;
19+ unsigned BS : 1 ;
20+ unsigned BT : 1 ;
21+ };
22+ };
23+ };
24+
25+ struct DebugRegister7
26+ {
27+ union
28+ {
29+ uintptr_t Value;
30+ struct
31+ {
32+ unsigned G0 : 1 ;
33+ unsigned L0 : 1 ;
34+ unsigned G1 : 1 ;
35+ unsigned L1 : 1 ;
36+ unsigned G2 : 1 ;
37+ unsigned L2 : 1 ;
38+ unsigned G3 : 1 ;
39+ unsigned L3 : 1 ;
40+ unsigned GE : 1 ;
41+ unsigned LE : 1 ;
42+ unsigned Reserved : 6 ;
43+ unsigned RW0 : 2 ;
44+ unsigned Len0 : 2 ;
45+ unsigned RW1 : 2 ;
46+ unsigned Len1 : 2 ;
47+ unsigned RW2 : 2 ;
48+ unsigned Len2 : 2 ;
49+ unsigned RW3 : 2 ;
50+ unsigned Len3 : 2 ;
51+ };
52+ };
53+ };
54+
655bool __stdcall AttachDebuggerToProcess (RC_Pointer id)
756{
857 if (!DebugActiveProcess ((DWORD)id))
@@ -55,20 +104,23 @@ bool __stdcall AwaitDebugEvent(DebugEvent* evt, int timeoutInMilliseconds)
55104 ctx.ContextFlags = CONTEXT_CONTROL | CONTEXT_INTEGER | CONTEXT_DEBUG_REGISTERS;
56105 GetThreadContext (handle, &ctx);
57106
107+ DebugRegister6 dr6;
108+ dr6.Value = ctx.Dr6 ;
109+
58110 // Check if breakpoint was a hardware breakpoint.
59- if (ctx. Dr6 & 0b0001 )
111+ if (dr6. DR0 )
60112 {
61113 evt->ExceptionInfo .CausedBy = HardwareBreakpointRegister::Dr0;
62114 }
63- else if (ctx. Dr6 & 0b0010 )
115+ else if (dr6. DR1 )
64116 {
65117 evt->ExceptionInfo .CausedBy = HardwareBreakpointRegister::Dr1;
66118 }
67- else if (ctx. Dr6 & 0b0100 )
119+ else if (dr6. DR2 )
68120 {
69121 evt->ExceptionInfo .CausedBy = HardwareBreakpointRegister::Dr2;
70122 }
71- else if (ctx. Dr6 & 0b1000 )
123+ else if (dr6. DR3 )
72124 {
73125 evt->ExceptionInfo .CausedBy = HardwareBreakpointRegister::Dr3;
74126 }
@@ -148,35 +200,29 @@ bool __stdcall SetHardwareBreakpoint(RC_Pointer id, RC_Pointer address, Hardware
148200 }
149201
150202 decltype (CONTEXT::Dr0) addressValue = 0 ;
151- int typeValue = 0 ;
152- int sizeValue = 0 ;
203+ int accessValue = 0 ;
204+ int lengthValue = 0 ;
153205
154206 if (set)
155207 {
156208 addressValue = (decltype (CONTEXT::Dr0))address;
157209
158210 if (type == HardwareBreakpointTrigger::Execute)
159- typeValue = 0 ;
211+ accessValue = 0 ;
160212 else if (type == HardwareBreakpointTrigger::Access)
161- typeValue = 3 ;
213+ accessValue = 3 ;
162214 else if (type == HardwareBreakpointTrigger::Write)
163- typeValue = 1 ;
215+ accessValue = 1 ;
164216
165217 if (size == HardwareBreakpointSize::Size1)
166- sizeValue = 0 ;
218+ lengthValue = 0 ;
167219 else if (size == HardwareBreakpointSize::Size2)
168- sizeValue = 1 ;
220+ lengthValue = 1 ;
169221 else if (size == HardwareBreakpointSize::Size4)
170- sizeValue = 3 ;
222+ lengthValue = 3 ;
171223 else if (size == HardwareBreakpointSize::Size8)
172- sizeValue = 2 ;
224+ lengthValue = 2 ;
173225 }
174-
175- auto SetBits = [](DWORD_PTR& dw, int lowBit, int bits, int newValue)
176- {
177- DWORD_PTR mask = (1 << bits) - 1 ;
178- dw = (dw & ~(mask << lowBit)) | (newValue << lowBit);
179- };
180226
181227 auto handle = CreateToolhelp32Snapshot (TH32CS_SNAPTHREAD, 0 );
182228 if (handle != INVALID_HANDLE_VALUE)
@@ -197,31 +243,38 @@ bool __stdcall SetHardwareBreakpoint(RC_Pointer id, RC_Pointer address, Hardware
197243 ctx.ContextFlags = CONTEXT_DEBUG_REGISTERS;
198244 GetThreadContext (handle, &ctx);
199245
200- int index = 0 ;
246+ DebugRegister7 dr7;
247+ dr7.Value = ctx.Dr7 ;
201248
202249 switch (reg)
203250 {
204251 case HardwareBreakpointRegister::Dr0:
205- index = 0 ;
206252 ctx.Dr0 = addressValue;
253+ dr7.G0 = true ;
254+ dr7.RW0 = accessValue;
255+ dr7.Len0 = lengthValue;
207256 break ;
208257 case HardwareBreakpointRegister::Dr1:
209- index = 1 ;
210258 ctx.Dr1 = addressValue;
259+ dr7.G1 = true ;
260+ dr7.RW1 = accessValue;
261+ dr7.Len1 = lengthValue;
211262 break ;
212263 case HardwareBreakpointRegister::Dr2:
213- index = 2 ;
214264 ctx.Dr2 = addressValue;
265+ dr7.G2 = true ;
266+ dr7.RW2 = accessValue;
267+ dr7.Len2 = lengthValue;
215268 break ;
216269 case HardwareBreakpointRegister::Dr3:
217- index = 3 ;
218270 ctx.Dr3 = addressValue;
271+ dr7.G3 = true ;
272+ dr7.RW3 = accessValue;
273+ dr7.Len3 = lengthValue;
219274 break ;
220275 }
221276
222- SetBits (ctx.Dr7 , 16 + index * 4 , 2 , typeValue);
223- SetBits (ctx.Dr7 , 18 + index * 4 , 2 , sizeValue);
224- SetBits (ctx.Dr7 , index * 2 , 1 , set ? 1 : 0 );
277+ ctx.Dr7 = dr7.Value ;
225278
226279 SetThreadContext (handle, &ctx);
227280
0 commit comments