Skip to content

Commit fb1babc

Browse files
committed
Put a critical section around StepInto to make it thread-safe
1 parent 1a76d61 commit fb1babc

5 files changed

Lines changed: 63 additions & 79 deletions

File tree

TitanEngine/Global.Debugger.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ LPVOID engineAttachedProcessDebugInfo = NULL;
4040
wchar_t szDebuggerName[512];
4141
bool DebugStepFinal = false;
4242
LPVOID StepOutCallBack = NULL;
43+
CRITICAL_SECTION engineStepActiveCr;
4344

4445
// Global.Debugger.functions:
4546
long DebugLoopInSecondThread(LPVOID InputParameter)

TitanEngine/Global.Debugger.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define _GLOBAL_DEBUGGER_H
33

44
#include <vector>
5+
#include <Windows.h>
56

67
extern HARDWARE_DATA DebugRegister[4];
78
extern PROCESS_INFORMATION dbgProcessInformation;
@@ -39,6 +40,7 @@ extern LPVOID engineAttachedProcessDebugInfo;
3940
extern wchar_t szDebuggerName[512];
4041
extern bool DebugStepFinal;
4142
extern LPVOID StepOutCallBack;
43+
extern CRITICAL_SECTION engineStepActiveCr;
4244

4345
long DebugLoopInSecondThread(LPVOID InputParameter);
4446
void DebuggerReset();

TitanEngine/TitanEngine.Debugger.Control.cpp

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,30 +37,35 @@ __declspec(dllexport) void TITCALL ForceClose()
3737

3838
__declspec(dllexport) void TITCALL StepInto(LPVOID StepCallBack)
3939
{
40-
ULONG_PTR ueCurrentPosition = GetContextData(UE_CIP);
41-
unsigned char instr[16];
42-
MemoryReadSafe(dbgProcessInformation.hProcess, (void*)ueCurrentPosition, instr, sizeof(instr), 0);
43-
char* DisassembledString = (char*)StaticDisassembleEx(ueCurrentPosition, (LPVOID)instr);
44-
if(strstr(DisassembledString, "PUSHF"))
45-
StepOver(StepCallBack);
46-
else if(strstr(DisassembledString, "POP SS") || strstr(DisassembledString, "MOV SS")) //prevent the 'PUSH SS', 'POP SS' step trick
40+
EnterCriticalSection(&engineStepActiveCr);
41+
if (!engineStepActive)
4742
{
48-
ueCurrentPosition += StaticLengthDisassemble((void*)instr);
49-
SetBPX(ueCurrentPosition, UE_BREAKPOINT_TYPE_INT3 + UE_SINGLESHOOT, StepCallBack);
50-
}
51-
else
52-
{
53-
CONTEXT myDBGContext;
54-
HANDLE hActiveThread = EngineOpenThread(THREAD_GETSETSUSPEND, false, DBGEvent.dwThreadId);
55-
myDBGContext.ContextFlags = CONTEXT_CONTROL;
56-
GetThreadContext(hActiveThread, &myDBGContext);
57-
myDBGContext.EFlags |= UE_TRAP_FLAG;
58-
SetThreadContext(hActiveThread, &myDBGContext);
59-
EngineCloseHandle(hActiveThread);
60-
engineStepActive = true;
61-
engineStepCallBack = StepCallBack;
62-
engineStepCount = 0;
43+
ULONG_PTR ueCurrentPosition = GetContextData(UE_CIP);
44+
unsigned char instr[16];
45+
MemoryReadSafe(dbgProcessInformation.hProcess, (void*)ueCurrentPosition, instr, sizeof(instr), 0);
46+
char* DisassembledString = (char*)StaticDisassembleEx(ueCurrentPosition, (LPVOID)instr);
47+
if (strstr(DisassembledString, "PUSHF"))
48+
StepOver(StepCallBack);
49+
else if (strstr(DisassembledString, "POP SS") || strstr(DisassembledString, "MOV SS")) //prevent the 'PUSH SS', 'POP SS' step trick
50+
{
51+
ueCurrentPosition += StaticLengthDisassemble((void*)instr);
52+
SetBPX(ueCurrentPosition, UE_BREAKPOINT_TYPE_INT3 + UE_SINGLESHOOT, StepCallBack);
53+
}
54+
else
55+
{
56+
CONTEXT myDBGContext;
57+
HANDLE hActiveThread = EngineOpenThread(THREAD_GETSETSUSPEND, false, DBGEvent.dwThreadId);
58+
myDBGContext.ContextFlags = CONTEXT_CONTROL;
59+
GetThreadContext(hActiveThread, &myDBGContext);
60+
myDBGContext.EFlags |= UE_TRAP_FLAG;
61+
SetThreadContext(hActiveThread, &myDBGContext);
62+
EngineCloseHandle(hActiveThread);
63+
engineStepActive = true;
64+
engineStepCallBack = StepCallBack;
65+
engineStepCount = 0;
66+
}
6367
}
68+
LeaveCriticalSection(&engineStepActiveCr);
6469
}
6570

6671
__declspec(dllexport) void TITCALL StepOver(LPVOID StepCallBack)

TitanEngine/TitanEngine.Debugger.DebugLoop.cpp

Lines changed: 31 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,33 @@
1212
#define UE_MODULEx86 0x2000;
1313
#define UE_MODULEx64 0x2000;
1414

15+
static void engineStep()
16+
{
17+
EnterCriticalSection(&engineStepActiveCr);
18+
if (engineStepActive)
19+
{
20+
DBGCode = DBG_CONTINUE;
21+
if (engineStepCount == 0)
22+
{
23+
typedef void(TITCALL* fCustomBreakPoint)(void);
24+
auto cbStep = fCustomBreakPoint(engineStepCallBack);
25+
engineStepActive = false;
26+
engineStepCallBack = NULL;
27+
LeaveCriticalSection(&engineStepActiveCr);
28+
cbStep();
29+
}
30+
else
31+
{
32+
SingleStep(engineStepCount, engineStepCallBack);
33+
LeaveCriticalSection(&engineStepActiveCr);
34+
}
35+
}
36+
else
37+
{
38+
LeaveCriticalSection(&engineStepActiveCr);
39+
}
40+
}
41+
1542
__declspec(dllexport) void TITCALL DebugLoop()
1643
{
1744
bool FirstBPX = true;
@@ -642,20 +669,7 @@ __declspec(dllexport) void TITCALL DebugLoop()
642669
EnableBPX(ResetBPXAddressTo);
643670
ResetBPXAddressTo = NULL;
644671
ResetBPX = false;
645-
if(engineStepActive)
646-
{
647-
if(engineStepCount == 0)
648-
{
649-
myCustomBreakPoint = (fCustomBreakPoint)(engineStepCallBack);
650-
engineStepActive = false;
651-
engineStepCallBack = NULL;
652-
myCustomBreakPoint();
653-
}
654-
else
655-
{
656-
SingleStep(engineStepCount, engineStepCallBack);
657-
}
658-
}
672+
engineStep();
659673
}
660674
else
661675
{
@@ -671,20 +685,7 @@ __declspec(dllexport) void TITCALL DebugLoop()
671685
{
672686
ResetHwBPX = false;
673687
SetHardwareBreakPoint(DebugRegisterX.DrxBreakAddress, DebugRegisterXId, DebugRegisterX.DrxBreakPointType, DebugRegisterX.DrxBreakPointSize, (LPVOID)DebugRegisterX.DrxCallBack);
674-
if(engineStepActive)
675-
{
676-
if(engineStepCount == 0)
677-
{
678-
myCustomBreakPoint = (fCustomBreakPoint)(engineStepCallBack);
679-
engineStepActive = false;
680-
engineStepCallBack = NULL;
681-
myCustomBreakPoint();
682-
}
683-
else
684-
{
685-
SingleStep(engineStepCount, engineStepCallBack);
686-
}
687-
}
688+
engineStep();
688689
}
689690
if(ResetMemBPX) //restore memory breakpoint
690691
{
@@ -719,20 +720,7 @@ __declspec(dllexport) void TITCALL DebugLoop()
719720
VirtualProtectEx(dbgProcessInformation.hProcess, (LPVOID)ResetMemBPXAddress, ResetMemBPXSize, NewProtect, &OldProtect);
720721
}
721722

722-
if(engineStepActive)
723-
{
724-
if(engineStepCount == 0)
725-
{
726-
myCustomBreakPoint = (fCustomBreakPoint)(engineStepCallBack);
727-
engineStepActive = false;
728-
engineStepCallBack = NULL;
729-
myCustomBreakPoint();
730-
}
731-
else
732-
{
733-
SingleStep(engineStepCount, engineStepCallBack);
734-
}
735-
}
723+
engineStep();
736724
}
737725
}
738726
else //no resetting needed (debugger reached hardware breakpoint or the user stepped)
@@ -867,21 +855,7 @@ __declspec(dllexport) void TITCALL DebugLoop()
867855
if(strstr(DisassembledString, "PUSHF"))
868856
PushfBPX = true;
869857
}
870-
if(engineStepActive)
871-
{
872-
DBGCode = DBG_CONTINUE;
873-
if(engineStepCount == 0)
874-
{
875-
myCustomBreakPoint = (fCustomBreakPoint)(engineStepCallBack);
876-
engineStepActive = false;
877-
engineStepCallBack = NULL;
878-
myCustomBreakPoint();
879-
}
880-
else
881-
{
882-
SingleStep(engineStepCount, engineStepCallBack);
883-
}
884-
}
858+
engineStep();
885859
}
886860
if(DBGCode == DBG_EXCEPTION_NOT_HANDLED) //NOTE: only call the chSingleStep callback when the debuggee generated the exception
887861
{

TitanEngine/TitanEngine.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "Global.Injector.h"
66
#include "Global.Engine.Extension.h"
77
#include "Global.Engine.Threading.h"
8+
#include "Global.Debugger.h"
89

910
// Global.Engine.Entry:
1011
BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
@@ -13,6 +14,7 @@ BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
1314
{
1415
case DLL_PROCESS_ATTACH:
1516
engineHandle = hinstDLL;
17+
InitializeCriticalSection(&engineStepActiveCr);
1618
EngineInit();
1719
EmptyGarbage();
1820
for(int i = 0; i < UE_MAX_RESERVED_MEMORY_LEFT; i++)

0 commit comments

Comments
 (0)