From 39fe35a09e991a9ebc77b5467f1f9794f78cf422 Mon Sep 17 00:00:00 2001 From: the_janitor Date: Sun, 19 Sep 2021 03:05:46 +0200 Subject: [PATCH] Fixed a bug in which thread termination froze --- .../TitanEngine.Debugger.DebugLoop.cpp | 34 +++++++++++++++---- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/TitanEngine/TitanEngine.Debugger.DebugLoop.cpp b/TitanEngine/TitanEngine.Debugger.DebugLoop.cpp index 499104f..a021ca6 100644 --- a/TitanEngine/TitanEngine.Debugger.DebugLoop.cpp +++ b/TitanEngine/TitanEngine.Debugger.DebugLoop.cpp @@ -109,15 +109,30 @@ __declspec(dllexport) void TITCALL DebugLoop() } } - if (IsDbgReplyLaterSupported && DBGEvent.dwDebugEventCode == EXCEPTION_DEBUG_EVENT) + if (IsDbgReplyLaterSupported) { - // Check if there is a thread processing a single step - if (ThreadBeingProcessed != 0 && DBGEvent.dwThreadId != ThreadBeingProcessed) + if (DBGEvent.dwDebugEventCode == EXCEPTION_DEBUG_EVENT) { - // Reply to the dbg event later - DBGCode = DBG_REPLY_LATER; + // Check if there is a thread processing a single step + if (ThreadBeingProcessed != 0 && DBGEvent.dwThreadId != ThreadBeingProcessed) + { + // Reply to the dbg event later + DBGCode = DBG_REPLY_LATER; - goto continue_dbg_event; + goto continue_dbg_event; + } + } + else if (DBGEvent.dwDebugEventCode == EXIT_THREAD_DEBUG_EVENT) + { + if (ThreadBeingProcessed != 0 && DBGEvent.dwThreadId == ThreadBeingProcessed) + { + // Resume the other threads since the thread being processed is exiting + for (auto& Thread : SuspendedThreads) + ResumeThread(Thread.hThread); + + SuspendedThreads.clear(); + ThreadBeingProcessed = 0; + } } } @@ -1394,7 +1409,7 @@ __declspec(dllexport) void TITCALL DebugLoop() break; } - if (IsDbgReplyLaterSupported) + if (IsDbgReplyLaterSupported && DBGEvent.dwDebugEventCode != EXIT_THREAD_DEBUG_EVENT) { CONTEXT DbgCtx; @@ -1414,6 +1429,11 @@ __declspec(dllexport) void TITCALL DebugLoop() if (ThreadBeingProcessed == Thread.dwThreadId) continue; + // Check if the thread is already suspended + for (auto& SuspendedThread : SuspendedThreads) + if (SuspendedThread.dwThreadId == Thread.dwThreadId) + continue; + if (SuspendThread(Thread.hThread) != -1) SuspendedThreads.push_back(Thread); }