mirror of https://github.com/x64dbg/TitanEngine
Put a critical section around StepInto to make it thread-safe
This commit is contained in:
parent
1a76d61ef6
commit
fb1babcbb3
|
|
@ -40,6 +40,7 @@ LPVOID engineAttachedProcessDebugInfo = NULL;
|
||||||
wchar_t szDebuggerName[512];
|
wchar_t szDebuggerName[512];
|
||||||
bool DebugStepFinal = false;
|
bool DebugStepFinal = false;
|
||||||
LPVOID StepOutCallBack = NULL;
|
LPVOID StepOutCallBack = NULL;
|
||||||
|
CRITICAL_SECTION engineStepActiveCr;
|
||||||
|
|
||||||
// Global.Debugger.functions:
|
// Global.Debugger.functions:
|
||||||
long DebugLoopInSecondThread(LPVOID InputParameter)
|
long DebugLoopInSecondThread(LPVOID InputParameter)
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
#define _GLOBAL_DEBUGGER_H
|
#define _GLOBAL_DEBUGGER_H
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <Windows.h>
|
||||||
|
|
||||||
extern HARDWARE_DATA DebugRegister[4];
|
extern HARDWARE_DATA DebugRegister[4];
|
||||||
extern PROCESS_INFORMATION dbgProcessInformation;
|
extern PROCESS_INFORMATION dbgProcessInformation;
|
||||||
|
|
@ -39,6 +40,7 @@ extern LPVOID engineAttachedProcessDebugInfo;
|
||||||
extern wchar_t szDebuggerName[512];
|
extern wchar_t szDebuggerName[512];
|
||||||
extern bool DebugStepFinal;
|
extern bool DebugStepFinal;
|
||||||
extern LPVOID StepOutCallBack;
|
extern LPVOID StepOutCallBack;
|
||||||
|
extern CRITICAL_SECTION engineStepActiveCr;
|
||||||
|
|
||||||
long DebugLoopInSecondThread(LPVOID InputParameter);
|
long DebugLoopInSecondThread(LPVOID InputParameter);
|
||||||
void DebuggerReset();
|
void DebuggerReset();
|
||||||
|
|
|
||||||
|
|
@ -37,30 +37,35 @@ __declspec(dllexport) void TITCALL ForceClose()
|
||||||
|
|
||||||
__declspec(dllexport) void TITCALL StepInto(LPVOID StepCallBack)
|
__declspec(dllexport) void TITCALL StepInto(LPVOID StepCallBack)
|
||||||
{
|
{
|
||||||
ULONG_PTR ueCurrentPosition = GetContextData(UE_CIP);
|
EnterCriticalSection(&engineStepActiveCr);
|
||||||
unsigned char instr[16];
|
if (!engineStepActive)
|
||||||
MemoryReadSafe(dbgProcessInformation.hProcess, (void*)ueCurrentPosition, instr, sizeof(instr), 0);
|
|
||||||
char* DisassembledString = (char*)StaticDisassembleEx(ueCurrentPosition, (LPVOID)instr);
|
|
||||||
if(strstr(DisassembledString, "PUSHF"))
|
|
||||||
StepOver(StepCallBack);
|
|
||||||
else if(strstr(DisassembledString, "POP SS") || strstr(DisassembledString, "MOV SS")) //prevent the 'PUSH SS', 'POP SS' step trick
|
|
||||||
{
|
{
|
||||||
ueCurrentPosition += StaticLengthDisassemble((void*)instr);
|
ULONG_PTR ueCurrentPosition = GetContextData(UE_CIP);
|
||||||
SetBPX(ueCurrentPosition, UE_BREAKPOINT_TYPE_INT3 + UE_SINGLESHOOT, StepCallBack);
|
unsigned char instr[16];
|
||||||
}
|
MemoryReadSafe(dbgProcessInformation.hProcess, (void*)ueCurrentPosition, instr, sizeof(instr), 0);
|
||||||
else
|
char* DisassembledString = (char*)StaticDisassembleEx(ueCurrentPosition, (LPVOID)instr);
|
||||||
{
|
if (strstr(DisassembledString, "PUSHF"))
|
||||||
CONTEXT myDBGContext;
|
StepOver(StepCallBack);
|
||||||
HANDLE hActiveThread = EngineOpenThread(THREAD_GETSETSUSPEND, false, DBGEvent.dwThreadId);
|
else if (strstr(DisassembledString, "POP SS") || strstr(DisassembledString, "MOV SS")) //prevent the 'PUSH SS', 'POP SS' step trick
|
||||||
myDBGContext.ContextFlags = CONTEXT_CONTROL;
|
{
|
||||||
GetThreadContext(hActiveThread, &myDBGContext);
|
ueCurrentPosition += StaticLengthDisassemble((void*)instr);
|
||||||
myDBGContext.EFlags |= UE_TRAP_FLAG;
|
SetBPX(ueCurrentPosition, UE_BREAKPOINT_TYPE_INT3 + UE_SINGLESHOOT, StepCallBack);
|
||||||
SetThreadContext(hActiveThread, &myDBGContext);
|
}
|
||||||
EngineCloseHandle(hActiveThread);
|
else
|
||||||
engineStepActive = true;
|
{
|
||||||
engineStepCallBack = StepCallBack;
|
CONTEXT myDBGContext;
|
||||||
engineStepCount = 0;
|
HANDLE hActiveThread = EngineOpenThread(THREAD_GETSETSUSPEND, false, DBGEvent.dwThreadId);
|
||||||
|
myDBGContext.ContextFlags = CONTEXT_CONTROL;
|
||||||
|
GetThreadContext(hActiveThread, &myDBGContext);
|
||||||
|
myDBGContext.EFlags |= UE_TRAP_FLAG;
|
||||||
|
SetThreadContext(hActiveThread, &myDBGContext);
|
||||||
|
EngineCloseHandle(hActiveThread);
|
||||||
|
engineStepActive = true;
|
||||||
|
engineStepCallBack = StepCallBack;
|
||||||
|
engineStepCount = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
LeaveCriticalSection(&engineStepActiveCr);
|
||||||
}
|
}
|
||||||
|
|
||||||
__declspec(dllexport) void TITCALL StepOver(LPVOID StepCallBack)
|
__declspec(dllexport) void TITCALL StepOver(LPVOID StepCallBack)
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,33 @@
|
||||||
#define UE_MODULEx86 0x2000;
|
#define UE_MODULEx86 0x2000;
|
||||||
#define UE_MODULEx64 0x2000;
|
#define UE_MODULEx64 0x2000;
|
||||||
|
|
||||||
|
static void engineStep()
|
||||||
|
{
|
||||||
|
EnterCriticalSection(&engineStepActiveCr);
|
||||||
|
if (engineStepActive)
|
||||||
|
{
|
||||||
|
DBGCode = DBG_CONTINUE;
|
||||||
|
if (engineStepCount == 0)
|
||||||
|
{
|
||||||
|
typedef void(TITCALL* fCustomBreakPoint)(void);
|
||||||
|
auto cbStep = fCustomBreakPoint(engineStepCallBack);
|
||||||
|
engineStepActive = false;
|
||||||
|
engineStepCallBack = NULL;
|
||||||
|
LeaveCriticalSection(&engineStepActiveCr);
|
||||||
|
cbStep();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
SingleStep(engineStepCount, engineStepCallBack);
|
||||||
|
LeaveCriticalSection(&engineStepActiveCr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
LeaveCriticalSection(&engineStepActiveCr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
__declspec(dllexport) void TITCALL DebugLoop()
|
__declspec(dllexport) void TITCALL DebugLoop()
|
||||||
{
|
{
|
||||||
bool FirstBPX = true;
|
bool FirstBPX = true;
|
||||||
|
|
@ -642,20 +669,7 @@ __declspec(dllexport) void TITCALL DebugLoop()
|
||||||
EnableBPX(ResetBPXAddressTo);
|
EnableBPX(ResetBPXAddressTo);
|
||||||
ResetBPXAddressTo = NULL;
|
ResetBPXAddressTo = NULL;
|
||||||
ResetBPX = false;
|
ResetBPX = false;
|
||||||
if(engineStepActive)
|
engineStep();
|
||||||
{
|
|
||||||
if(engineStepCount == 0)
|
|
||||||
{
|
|
||||||
myCustomBreakPoint = (fCustomBreakPoint)(engineStepCallBack);
|
|
||||||
engineStepActive = false;
|
|
||||||
engineStepCallBack = NULL;
|
|
||||||
myCustomBreakPoint();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
SingleStep(engineStepCount, engineStepCallBack);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
@ -671,20 +685,7 @@ __declspec(dllexport) void TITCALL DebugLoop()
|
||||||
{
|
{
|
||||||
ResetHwBPX = false;
|
ResetHwBPX = false;
|
||||||
SetHardwareBreakPoint(DebugRegisterX.DrxBreakAddress, DebugRegisterXId, DebugRegisterX.DrxBreakPointType, DebugRegisterX.DrxBreakPointSize, (LPVOID)DebugRegisterX.DrxCallBack);
|
SetHardwareBreakPoint(DebugRegisterX.DrxBreakAddress, DebugRegisterXId, DebugRegisterX.DrxBreakPointType, DebugRegisterX.DrxBreakPointSize, (LPVOID)DebugRegisterX.DrxCallBack);
|
||||||
if(engineStepActive)
|
engineStep();
|
||||||
{
|
|
||||||
if(engineStepCount == 0)
|
|
||||||
{
|
|
||||||
myCustomBreakPoint = (fCustomBreakPoint)(engineStepCallBack);
|
|
||||||
engineStepActive = false;
|
|
||||||
engineStepCallBack = NULL;
|
|
||||||
myCustomBreakPoint();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
SingleStep(engineStepCount, engineStepCallBack);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if(ResetMemBPX) //restore memory breakpoint
|
if(ResetMemBPX) //restore memory breakpoint
|
||||||
{
|
{
|
||||||
|
|
@ -719,20 +720,7 @@ __declspec(dllexport) void TITCALL DebugLoop()
|
||||||
VirtualProtectEx(dbgProcessInformation.hProcess, (LPVOID)ResetMemBPXAddress, ResetMemBPXSize, NewProtect, &OldProtect);
|
VirtualProtectEx(dbgProcessInformation.hProcess, (LPVOID)ResetMemBPXAddress, ResetMemBPXSize, NewProtect, &OldProtect);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(engineStepActive)
|
engineStep();
|
||||||
{
|
|
||||||
if(engineStepCount == 0)
|
|
||||||
{
|
|
||||||
myCustomBreakPoint = (fCustomBreakPoint)(engineStepCallBack);
|
|
||||||
engineStepActive = false;
|
|
||||||
engineStepCallBack = NULL;
|
|
||||||
myCustomBreakPoint();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
SingleStep(engineStepCount, engineStepCallBack);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else //no resetting needed (debugger reached hardware breakpoint or the user stepped)
|
else //no resetting needed (debugger reached hardware breakpoint or the user stepped)
|
||||||
|
|
@ -867,21 +855,7 @@ __declspec(dllexport) void TITCALL DebugLoop()
|
||||||
if(strstr(DisassembledString, "PUSHF"))
|
if(strstr(DisassembledString, "PUSHF"))
|
||||||
PushfBPX = true;
|
PushfBPX = true;
|
||||||
}
|
}
|
||||||
if(engineStepActive)
|
engineStep();
|
||||||
{
|
|
||||||
DBGCode = DBG_CONTINUE;
|
|
||||||
if(engineStepCount == 0)
|
|
||||||
{
|
|
||||||
myCustomBreakPoint = (fCustomBreakPoint)(engineStepCallBack);
|
|
||||||
engineStepActive = false;
|
|
||||||
engineStepCallBack = NULL;
|
|
||||||
myCustomBreakPoint();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
SingleStep(engineStepCount, engineStepCallBack);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if(DBGCode == DBG_EXCEPTION_NOT_HANDLED) //NOTE: only call the chSingleStep callback when the debuggee generated the exception
|
if(DBGCode == DBG_EXCEPTION_NOT_HANDLED) //NOTE: only call the chSingleStep callback when the debuggee generated the exception
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@
|
||||||
#include "Global.Injector.h"
|
#include "Global.Injector.h"
|
||||||
#include "Global.Engine.Extension.h"
|
#include "Global.Engine.Extension.h"
|
||||||
#include "Global.Engine.Threading.h"
|
#include "Global.Engine.Threading.h"
|
||||||
|
#include "Global.Debugger.h"
|
||||||
|
|
||||||
// Global.Engine.Entry:
|
// Global.Engine.Entry:
|
||||||
BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
|
BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
|
||||||
|
|
@ -13,6 +14,7 @@ BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
|
||||||
{
|
{
|
||||||
case DLL_PROCESS_ATTACH:
|
case DLL_PROCESS_ATTACH:
|
||||||
engineHandle = hinstDLL;
|
engineHandle = hinstDLL;
|
||||||
|
InitializeCriticalSection(&engineStepActiveCr);
|
||||||
EngineInit();
|
EngineInit();
|
||||||
EmptyGarbage();
|
EmptyGarbage();
|
||||||
for(int i = 0; i < UE_MAX_RESERVED_MEMORY_LEFT; i++)
|
for(int i = 0; i < UE_MAX_RESERVED_MEMORY_LEFT; i++)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue