Skip to content

Commit accf030

Browse files
committed
Added DebugRegister helper classes.
1 parent bf57aea commit accf030

File tree

2 files changed

+84
-31
lines changed

2 files changed

+84
-31
lines changed

NativeCore/Windows/Debugger.cpp

Lines changed: 80 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,55 @@
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+
655
bool __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

NativeCore/Windows/NativeCore.vcxproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@
9999
</PrecompiledHeader>
100100
<WarningLevel>Level3</WarningLevel>
101101
<Optimization>Disabled</Optimization>
102-
<PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;_USRDLL;NATIVECORE_EXPORTS;BEA_ENGINE_STATIC;_CRT_SECURE_NO_WARNINGS;NOMINMAX;%(PreprocessorDefinitions)</PreprocessorDefinitions>
102+
<PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;_USRDLL;NATIVECORE_EXPORTS;BEA_ENGINE_STATIC;_CRT_SECURE_NO_WARNINGS;NOMINMAX;RECLASSNET32;%(PreprocessorDefinitions)</PreprocessorDefinitions>
103103
</ClCompile>
104104
<Link>
105105
<SubSystem>Windows</SubSystem>
@@ -113,7 +113,7 @@
113113
</PrecompiledHeader>
114114
<WarningLevel>Level3</WarningLevel>
115115
<Optimization>Disabled</Optimization>
116-
<PreprocessorDefinitions>_DEBUG;_WINDOWS;_USRDLL;NATIVECORE_EXPORTS;BEA_ENGINE_STATIC;_CRT_SECURE_NO_WARNINGS;NOMINMAX;%(PreprocessorDefinitions)</PreprocessorDefinitions>
116+
<PreprocessorDefinitions>_DEBUG;_WINDOWS;_USRDLL;NATIVECORE_EXPORTS;BEA_ENGINE_STATIC;_CRT_SECURE_NO_WARNINGS;NOMINMAX;RECLASSNET64;%(PreprocessorDefinitions)</PreprocessorDefinitions>
117117
</ClCompile>
118118
<Link>
119119
<SubSystem>Windows</SubSystem>
@@ -129,7 +129,7 @@
129129
<Optimization>MaxSpeed</Optimization>
130130
<FunctionLevelLinking>true</FunctionLevelLinking>
131131
<IntrinsicFunctions>true</IntrinsicFunctions>
132-
<PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;_USRDLL;NATIVECORE_EXPORTS;BEA_ENGINE_STATIC;_CRT_SECURE_NO_WARNINGS;NOMINMAX;%(PreprocessorDefinitions)</PreprocessorDefinitions>
132+
<PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;_USRDLL;NATIVECORE_EXPORTS;BEA_ENGINE_STATIC;_CRT_SECURE_NO_WARNINGS;NOMINMAX;RECLASSNET32;%(PreprocessorDefinitions)</PreprocessorDefinitions>
133133
</ClCompile>
134134
<Link>
135135
<SubSystem>Windows</SubSystem>
@@ -147,7 +147,7 @@
147147
<Optimization>MaxSpeed</Optimization>
148148
<FunctionLevelLinking>true</FunctionLevelLinking>
149149
<IntrinsicFunctions>true</IntrinsicFunctions>
150-
<PreprocessorDefinitions>NDEBUG;_WINDOWS;_USRDLL;NATIVECORE_EXPORTS;BEA_ENGINE_STATIC;_CRT_SECURE_NO_WARNINGS;NOMINMAX;%(PreprocessorDefinitions)</PreprocessorDefinitions>
150+
<PreprocessorDefinitions>NDEBUG;_WINDOWS;_USRDLL;NATIVECORE_EXPORTS;BEA_ENGINE_STATIC;_CRT_SECURE_NO_WARNINGS;NOMINMAX;RECLASSNET64;%(PreprocessorDefinitions)</PreprocessorDefinitions>
151151
</ClCompile>
152152
<Link>
153153
<SubSystem>Windows</SubSystem>

0 commit comments

Comments
 (0)