From 5f526bac56218def7a5e0fed47cd665645e62787 Mon Sep 17 00:00:00 2001 From: mrexodia Date: Wed, 25 Mar 2015 21:25:25 +0100 Subject: [PATCH] process & thread housekeeping functionality --- GleeBug/Debugger.Data.h | 20 ------------------ GleeBug/Debugger.Loop.cpp | 27 ++++++++++++++++++++++--- GleeBug/Debugger.Process.h | 36 +++++++++++++++++++++++++++++++++ GleeBug/Debugger.Thread.h | 30 +++++++++++++++++++++++++++ GleeBug/Debugger.cpp | 22 ++++---------------- GleeBug/Debugger.h | 13 +++--------- GleeBug/GleeBug.vcxproj | 3 ++- GleeBug/GleeBug.vcxproj.filters | 7 +++++-- GleeBug/_global.h | 3 +++ MyDebugger/main.cpp | 8 ++++++-- 10 files changed, 113 insertions(+), 56 deletions(-) delete mode 100644 GleeBug/Debugger.Data.h create mode 100644 GleeBug/Debugger.Process.h create mode 100644 GleeBug/Debugger.Thread.h diff --git a/GleeBug/Debugger.Data.h b/GleeBug/Debugger.Data.h deleted file mode 100644 index 567295d..0000000 --- a/GleeBug/Debugger.Data.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef _DEBUGGER_DATA_H -#define _DEBUGGER_DATA_H - -#include "_global.h" - -namespace GleeBug -{ - /** - \brief Process information structure. - */ - struct ProcessInfo - { - HANDLE hProcess; - HANDLE hThread; - DWORD ProcessId; - DWORD MainThreadId; - }; -} - -#endif //_DEBUGGER_DATA_H \ No newline at end of file diff --git a/GleeBug/Debugger.Loop.cpp b/GleeBug/Debugger.Loop.cpp index 781a0cd..e18f8fa 100644 --- a/GleeBug/Debugger.Loop.cpp +++ b/GleeBug/Debugger.Loop.cpp @@ -4,26 +4,47 @@ namespace GleeBug { void Debugger::createProcessEvent(CREATE_PROCESS_DEBUG_INFO* createProcess) { + //process housekeeping + ProcessInfo process(createProcess->hProcess, + createProcess->hThread, + _debugEvent.dwProcessId, + _debugEvent.dwThreadId); + _processes.insert({ process.dwProcessId, process }); + + //call the callback cbCreateProcessEvent(createProcess); } void Debugger::exitProcessEvent(EXIT_PROCESS_DEBUG_INFO* exitProcess) { - if (_debugEvent.dwProcessId == _mainProcess.ProcessId) - { + //check if the terminated process is the main debuggee + if (_debugEvent.dwProcessId == _mainProcess.dwProcessId) _breakDebugger = true; - } + + //call the callback cbExitProcessEvent(exitProcess); + + //process housekeeping + _processes.erase(_debugEvent.dwProcessId); } void Debugger::createThreadEvent(CREATE_THREAD_DEBUG_INFO* createThread) { + //thread housekeeping + ThreadInfo thread(_debugEvent.dwThreadId, createThread->hThread, createThread->lpThreadLocalBase, createThread->lpStartAddress); + _processes[_debugEvent.dwProcessId].threads.insert({ thread.dwThreadId, thread }); + + //call the callback cbCreateThreadEvent(createThread); } void Debugger::exitThreadEvent(EXIT_THREAD_DEBUG_INFO* exitThread) { + //call the callback cbExitThreadEvent(exitThread); + + //thread housekeeping + _processes[_debugEvent.dwProcessId].threads.erase(_debugEvent.dwThreadId); } void Debugger::loadDllEvent(LOAD_DLL_DEBUG_INFO* loadDll) diff --git a/GleeBug/Debugger.Process.h b/GleeBug/Debugger.Process.h new file mode 100644 index 0000000..e92b008 --- /dev/null +++ b/GleeBug/Debugger.Process.h @@ -0,0 +1,36 @@ +#ifndef _DEBUGGER_PROCESS_H +#define _DEBUGGER_PROCESS_H + +#include "_global.h" +#include "Debugger.Thread.h" + +namespace GleeBug +{ + /** + \brief Process information structure. + */ + struct ProcessInfo + { + HANDLE hProcess; + HANDLE hThread; + DWORD dwProcessId; + DWORD dwMainThreadId; + + ThreadMap threads; + + ProcessInfo() {} //fixes a 'no default constructor available' error + + ProcessInfo(HANDLE hProcess, HANDLE hThread, DWORD dwProcessId, DWORD dwMainThreadId) + { + this->hProcess = hProcess; + this->hThread = hThread; + this->dwProcessId = dwProcessId; + this->dwMainThreadId = dwMainThreadId; + this->threads.clear(); + } + }; + + typedef std::map ProcessMap; +} + +#endif //_DEBUGGER_PROCESS_H \ No newline at end of file diff --git a/GleeBug/Debugger.Thread.h b/GleeBug/Debugger.Thread.h new file mode 100644 index 0000000..4d2ee43 --- /dev/null +++ b/GleeBug/Debugger.Thread.h @@ -0,0 +1,30 @@ +#ifndef _DEBUGGER_THREADS_H +#define _DEBUGGER_THREADS_H + +#include "_global.h" + +namespace GleeBug +{ + /** + \brief Thread information structure. + */ + struct ThreadInfo + { + DWORD dwThreadId; + HANDLE hThread; + ULONG_PTR lpThreadLocalBase; + ULONG_PTR lpStartAddress; + + ThreadInfo(DWORD dwThreadId, HANDLE hThread, LPVOID lpThreadLocalBase, LPVOID lpStartAddress) + { + this->dwThreadId = dwThreadId; + this->hThread = hThread; + this->lpThreadLocalBase = (ULONG_PTR)lpThreadLocalBase; + this->lpStartAddress = (ULONG_PTR)lpStartAddress; + } + }; + + typedef std::map ThreadMap; +}; + +#endif //_DEBUGGER_THREADS_H \ No newline at end of file diff --git a/GleeBug/Debugger.cpp b/GleeBug/Debugger.cpp index 168add9..339d6c3 100644 --- a/GleeBug/Debugger.cpp +++ b/GleeBug/Debugger.cpp @@ -4,6 +4,7 @@ namespace GleeBug { Debugger::Debugger() { + _processes.clear(); } bool Debugger::Init(const wchar_t* szFilePath, @@ -12,8 +13,6 @@ namespace GleeBug { STARTUPINFOW si; memset(&si, 0, sizeof(si)); - PROCESS_INFORMATION pi; - memset(&pi, 0, sizeof(pi)); const wchar_t* szFileNameCreateProcess; wchar_t* szCommandLineCreateProcess; if (szCommandLine == NULL || !wcslen(szCommandLine)) @@ -29,7 +28,7 @@ namespace GleeBug szFileNameCreateProcess = 0; } - if (!CreateProcessW(szFileNameCreateProcess, + return !!CreateProcessW(szFileNameCreateProcess, szCommandLineCreateProcess, NULL, NULL, @@ -38,15 +37,7 @@ namespace GleeBug NULL, szCurrentDirectory, &si, - &pi)) - { - return false; - } - _mainProcess.hProcess = pi.hProcess; - _mainProcess.hThread = pi.hThread; - _mainProcess.ProcessId = pi.dwProcessId; - _mainProcess.MainThreadId = pi.dwThreadId; - return true; + &_mainProcess); } bool Debugger::Stop() @@ -56,11 +47,6 @@ namespace GleeBug bool Debugger::Detach() { - return !!DebugActiveProcessStop(_mainProcess.ProcessId); - } - - const ProcessInfo & Debugger::GetMainProcess() - { - return _mainProcess; + return !!DebugActiveProcessStop(_mainProcess.dwProcessId); } }; \ No newline at end of file diff --git a/GleeBug/Debugger.h b/GleeBug/Debugger.h index bfb3ad5..e8e146e 100644 --- a/GleeBug/Debugger.h +++ b/GleeBug/Debugger.h @@ -2,7 +2,7 @@ #define _DEBUGGER_H #include "_global.h" -#include "Debugger.Data.h" +#include "Debugger.Process.h" namespace GleeBug { @@ -45,12 +45,6 @@ namespace GleeBug */ void Start(); - /** - \brief Gets main process info. - \return The main process info. - */ - const ProcessInfo & GetMainProcess(); - protected: /** \brief Process creation debug event callback. Provide an implementation to use this callback. @@ -129,12 +123,11 @@ namespace GleeBug virtual void debugStringEvent(OUTPUT_DEBUG_STRING_INFO* debugString); virtual void ripEvent(RIP_INFO* rip); - - - ProcessInfo _mainProcess; + PROCESS_INFORMATION _mainProcess; DWORD _continueStatus; bool _breakDebugger; DEBUG_EVENT _debugEvent; + ProcessMap _processes; }; }; diff --git a/GleeBug/GleeBug.vcxproj b/GleeBug/GleeBug.vcxproj index 36958cb..467b3c2 100644 --- a/GleeBug/GleeBug.vcxproj +++ b/GleeBug/GleeBug.vcxproj @@ -139,8 +139,9 @@ - + + diff --git a/GleeBug/GleeBug.vcxproj.filters b/GleeBug/GleeBug.vcxproj.filters index 41ac337..ce2c369 100644 --- a/GleeBug/GleeBug.vcxproj.filters +++ b/GleeBug/GleeBug.vcxproj.filters @@ -29,10 +29,13 @@ Header Files - + Header Files - + + Header Files + + Header Files diff --git a/GleeBug/_global.h b/GleeBug/_global.h index ddd6f81..ef4de98 100644 --- a/GleeBug/_global.h +++ b/GleeBug/_global.h @@ -3,6 +3,9 @@ #include #include +#include +#include +#include #include #endif //_GLOBAL_H \ No newline at end of file diff --git a/MyDebugger/main.cpp b/MyDebugger/main.cpp index 05f4efb..e3398b4 100644 --- a/MyDebugger/main.cpp +++ b/MyDebugger/main.cpp @@ -3,9 +3,13 @@ int main() { - wchar_t szFilePath[256] = L"c:\\CodeBlocks\\arma_cert_bin_info\\bin\\arma_cert_bin_info.exe"; +#ifdef _WIN64 + wchar_t szFilePath[256] = L"c:\\test64.exe"; +#else //x86 + wchar_t szFilePath[256] = L"c:\\test32.exe"; +#endif //_WIN64 wchar_t szCommandLine[256] = L""; - wchar_t szCurrentDir[256] = L"c:\\CodeBlocks\\arma_cert_bin_info\\bin"; + wchar_t szCurrentDir[256] = L"c:\\"; MyDebugger dbg; if (dbg.Init(szFilePath, szCommandLine, szCurrentDir)) {