From d77456f43e0df3673230a9510d64561997cf2e3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milo=C5=A1?= Date: Tue, 15 Oct 2024 10:56:01 +0200 Subject: [PATCH] Make sure stepping is called back on the same thread - When issuing a step command, remember its thread ID so that other threads cannot hijack the callback and trigger their breakpoints. --- TitanEngine/Global.Debugger.cpp | 2 +- TitanEngine/Global.Debugger.h | 2 +- TitanEngine/TitanEngine.Debugger.Control.cpp | 4 ++-- TitanEngine/TitanEngine.Debugger.DebugLoop.cpp | 6 +++--- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/TitanEngine/Global.Debugger.cpp b/TitanEngine/Global.Debugger.cpp index 95d09a4..e338142 100644 --- a/TitanEngine/Global.Debugger.cpp +++ b/TitanEngine/Global.Debugger.cpp @@ -31,7 +31,7 @@ std::vector tlsCallBackList; std::vector hListProcess; DWORD engineStepCount = 0; LPVOID engineStepCallBack = NULL; -bool engineStepActive = false; +DWORD engineStepTID = 0; bool engineProcessIsNowDetached = false; DWORD DBGCode = DBG_CONTINUE; bool engineFileIsBeingDebugged = false; diff --git a/TitanEngine/Global.Debugger.h b/TitanEngine/Global.Debugger.h index 1861756..ead7ca5 100644 --- a/TitanEngine/Global.Debugger.h +++ b/TitanEngine/Global.Debugger.h @@ -31,7 +31,7 @@ extern std::vector tlsCallBackList; extern std::vector hListProcess; extern DWORD engineStepCount; extern LPVOID engineStepCallBack; -extern bool engineStepActive; +extern DWORD engineStepTID; extern bool engineProcessIsNowDetached; extern DWORD DBGCode; extern bool engineFileIsBeingDebugged; diff --git a/TitanEngine/TitanEngine.Debugger.Control.cpp b/TitanEngine/TitanEngine.Debugger.Control.cpp index 3ee04fa..b3cc5d0 100644 --- a/TitanEngine/TitanEngine.Debugger.Control.cpp +++ b/TitanEngine/TitanEngine.Debugger.Control.cpp @@ -38,7 +38,7 @@ __declspec(dllexport) void TITCALL ForceClose() __declspec(dllexport) void TITCALL StepInto(LPVOID StepCallBack) { EnterCriticalSection(&engineStepActiveCr); - if (!engineStepActive) + if (engineStepTID == 0) { ULONG_PTR ueCurrentPosition = GetContextData(UE_CIP); unsigned char instr[16]; @@ -60,7 +60,7 @@ __declspec(dllexport) void TITCALL StepInto(LPVOID StepCallBack) myDBGContext.EFlags |= UE_TRAP_FLAG; SetThreadContext(hActiveThread, &myDBGContext); EngineCloseHandle(hActiveThread); - engineStepActive = true; + engineStepTID = DBGEvent.dwThreadId; engineStepCallBack = StepCallBack; engineStepCount = 0; } diff --git a/TitanEngine/TitanEngine.Debugger.DebugLoop.cpp b/TitanEngine/TitanEngine.Debugger.DebugLoop.cpp index 1c8d78c..61b1c9d 100644 --- a/TitanEngine/TitanEngine.Debugger.DebugLoop.cpp +++ b/TitanEngine/TitanEngine.Debugger.DebugLoop.cpp @@ -16,14 +16,14 @@ static void engineStep() { EnterCriticalSection(&engineStepActiveCr); - if (engineStepActive) + if (engineStepTID == DBGEvent.dwThreadId) { DBGCode = DBG_CONTINUE; if (engineStepCount == 0) { typedef void(TITCALL* fCustomBreakPoint)(void); auto cbStep = fCustomBreakPoint(engineStepCallBack); - engineStepActive = false; + engineStepTID = 0; engineStepCallBack = NULL; LeaveCriticalSection(&engineStepActiveCr); cbStep(); @@ -1237,7 +1237,7 @@ __declspec(dllexport) void TITCALL DebugLoop() //general unhandled exception callback if(DBGCode == DBG_EXCEPTION_NOT_HANDLED) { - engineStepActive = false; + engineStepTID = 0; if(DBGCustomHandler->chUnhandledException != NULL) {