From d3da3e867ef4f4f99846c5d92b824a38abccc024 Mon Sep 17 00:00:00 2001 From: "Mr. eXoDia" Date: Tue, 14 Jul 2015 02:26:43 +0200 Subject: [PATCH] DBG: fixed some more stuff (event filters are not not executed when the debugger is already closed) --- x64_dbg_dbg/_exports.cpp | 12 ++++++++++++ x64_dbg_dbg/debugger.cpp | 18 ++++++++++++++++-- x64_dbg_dbg/debugger.h | 1 + x64_dbg_dbg/stackinfo.cpp | 4 ++-- x64_dbg_dbg/x64_dbg.cpp | 9 +++++++++ x64_dbg_dbg/x64_dbg.h | 1 + x64_dbg_gui/Project/Src/main.cpp | 2 ++ 7 files changed, 43 insertions(+), 4 deletions(-) diff --git a/x64_dbg_dbg/_exports.cpp b/x64_dbg_dbg/_exports.cpp index fe670f23..8c3a21c8 100644 --- a/x64_dbg_dbg/_exports.cpp +++ b/x64_dbg_dbg/_exports.cpp @@ -26,6 +26,7 @@ #include "function.h" #include "loop.h" #include "error.h" +#include "x64_dbg.h" static bool bOnlyCipAutoComments = false; @@ -635,6 +636,17 @@ extern "C" DLL_EXPORT bool _dbg_functionoverlaps(uint start, uint end) extern "C" DLL_EXPORT uint _dbg_sendmessage(DBGMSG type, void* param1, void* param2) { + if(dbgisstopped()) + { + switch(type) //ignore win events + { + case DBG_WIN_EVENT: + case DBG_WIN_EVENT_GLOBAL: + return 0; + default: + __debugbreak(); //we cannot process messages when the debugger is stopped, this must be a bug + } + } switch(type) { case DBG_SCRIPT_LOAD: diff --git a/x64_dbg_dbg/debugger.cpp b/x64_dbg_dbg/debugger.cpp index 404cb1dd..7bc46949 100644 --- a/x64_dbg_dbg/debugger.cpp +++ b/x64_dbg_dbg/debugger.cpp @@ -36,6 +36,8 @@ static int ecount = 0; static std::vector ignoredExceptionRange; static SIZE_T cachePrivateUsage = 0; static HANDLE hEvent = 0; +static HANDLE hMemMapThread = 0; +static bool bStopMemMapThread = false; static String lastDebugText; char szFileName[MAX_PATH] = ""; char szSymbolCachePath[MAX_PATH] = ""; @@ -47,10 +49,16 @@ bool bEnableSourceDebugging = true; static DWORD WINAPI memMapThread(void* ptr) { - while(true) + while(!bStopMemMapThread) { while(!DbgIsDebugging()) + { + if(bStopMemMapThread) + break; Sleep(1); + } + if(bStopMemMapThread) + break; const SIZE_T PrivateUsage = dbggetprivateusage(fdProcessInfo->hProcess); if(cachePrivateUsage != PrivateUsage && !dbgisrunning()) //update the memory map when the memory usage changed { @@ -67,7 +75,13 @@ void dbginit() { ExceptionCodeInit(); ErrorCodeInit(); - CloseHandle(CreateThread(0, 0, memMapThread, 0, 0, 0)); + hMemMapThread = CreateThread(0, 0, memMapThread, 0, 0, 0); +} + +void dbgstop() +{ + bStopMemMapThread = true; + WaitForThreadTermination(hMemMapThread); } SIZE_T dbggetprivateusage(HANDLE hProcess, bool update) diff --git a/x64_dbg_dbg/debugger.h b/x64_dbg_dbg/debugger.h index a60c2a0c..07a293c5 100644 --- a/x64_dbg_dbg/debugger.h +++ b/x64_dbg_dbg/debugger.h @@ -75,6 +75,7 @@ typedef struct _THREADNAME_INFO //functions SIZE_T dbggetprivateusage(HANDLE hProcess, bool update = false); void dbginit(); +void dbgstop(); uint dbgdebuggedbase(); bool dbgisrunning(); bool dbgisdll(); diff --git a/x64_dbg_dbg/stackinfo.cpp b/x64_dbg_dbg/stackinfo.cpp index 98d7d26b..59bff181 100644 --- a/x64_dbg_dbg/stackinfo.cpp +++ b/x64_dbg_dbg/stackinfo.cpp @@ -117,7 +117,7 @@ BOOL CALLBACK StackReadProcessMemoryProc64(HANDLE hProcess, DWORD64 lpBaseAddres // Fix for 64-bit sizes SIZE_T bytesRead = 0; - if(MemRead(lpBaseAddress, lpBuffer, nSize, &bytesRead)) + if(MemRead((uint)lpBaseAddress, lpBuffer, nSize, &bytesRead)) { if(lpNumberOfBytesRead) *lpNumberOfBytesRead = (DWORD)bytesRead; @@ -133,7 +133,7 @@ DWORD64 CALLBACK StackGetModuleBaseProc64(HANDLE hProcess, DWORD64 Address) if(hProcess != fdProcessInfo->hProcess) __debugbreak(); - return ModBaseFromAddr(Address); + return (DWORD64)ModBaseFromAddr((uint)Address); } DWORD64 CALLBACK StackTranslateAddressProc64(HANDLE hProcess, HANDLE hThread, LPADDRESS64 lpaddr) diff --git a/x64_dbg_dbg/x64_dbg.cpp b/x64_dbg_dbg/x64_dbg.cpp index fedfed5c..5d3f6da0 100644 --- a/x64_dbg_dbg/x64_dbg.cpp +++ b/x64_dbg_dbg/x64_dbg.cpp @@ -25,6 +25,7 @@ static COMMAND* command_list = 0; static HANDLE hCommandLoopThread = 0; static bool bStopCommandLoopThread = false; static char alloctrace[MAX_PATH] = ""; +static bool bIsStopped = false; static CMDRESULT cbStrLen(int argc, char* argv[]) { @@ -366,7 +367,10 @@ extern "C" DLL_EXPORT void _dbg_dbgexitsignal() SectionLockerGlobal::Deinitialize(); dputs("Cleaning up wait objects..."); waitdeinitialize(); + dputs("Cleaning up debugger threads..."); + dbgstop(); dputs("Exit signal processed successfully!"); + bIsStopped = true; } extern "C" DLL_EXPORT bool _dbg_dbgcmddirectexec(const char* cmd) @@ -380,3 +384,8 @@ COMMAND* dbggetcommandlist() { return command_list; } + +bool dbgisstopped() +{ + return bIsStopped; +} \ No newline at end of file diff --git a/x64_dbg_dbg/x64_dbg.h b/x64_dbg_dbg/x64_dbg.h index a6478a6a..ea5ce228 100644 --- a/x64_dbg_dbg/x64_dbg.h +++ b/x64_dbg_dbg/x64_dbg.h @@ -19,5 +19,6 @@ DLL_EXPORT void _dbg_dbgexitsignal(); #endif COMMAND* dbggetcommandlist(); +bool dbgisstopped(); #endif // _X64_DBG_H diff --git a/x64_dbg_gui/Project/Src/main.cpp b/x64_dbg_gui/Project/Src/main.cpp index f4b111a6..26a5f31b 100644 --- a/x64_dbg_gui/Project/Src/main.cpp +++ b/x64_dbg_gui/Project/Src/main.cpp @@ -97,6 +97,8 @@ int main(int argc, char* argv[]) mConfiguration->save(); //save config on exit #if QT_VERSION >= QT_VERSION_CHECK(5,0,0) QAbstractEventDispatcher::instance(application.thread())->removeNativeEventFilter(filter); +#else + QAbstractEventDispatcher::instance(application.thread())->setEventFilter(nullptr); #endif return result; }