From 187bdeff1dad6b44be9faab29e2fc2326da77e54 Mon Sep 17 00:00:00 2001 From: "Mr. eXoDia" Date: Wed, 25 Mar 2015 02:33:32 +0100 Subject: [PATCH] c++ stuff --- GleeBug.sln | 6 +++ GleeBug/Debugger.Core.cpp | 68 ---------------------------- GleeBug/Debugger.Core.h | 18 -------- GleeBug/Debugger.Data.h | 17 +++++++ GleeBug/Debugger.Loop.cpp | 79 ++++++++++++++++----------------- GleeBug/Debugger.Loop.h | 11 ----- GleeBug/Debugger.Misc.cpp | 9 ++++ GleeBug/Debugger.State.h | 25 ----------- GleeBug/Debugger.cpp | 60 +++++++++++++++++++++++++ GleeBug/Debugger.h | 24 +++++++++- GleeBug/GleeBug.vcxproj | 70 ++++++++++++++++++++++++++--- GleeBug/GleeBug.vcxproj.filters | 24 ++++------ GleeBug/_global.h | 1 + GleeBug/main.cpp | 12 ++--- 14 files changed, 235 insertions(+), 189 deletions(-) delete mode 100644 GleeBug/Debugger.Core.cpp delete mode 100644 GleeBug/Debugger.Core.h create mode 100644 GleeBug/Debugger.Data.h delete mode 100644 GleeBug/Debugger.Loop.h create mode 100644 GleeBug/Debugger.Misc.cpp delete mode 100644 GleeBug/Debugger.State.h diff --git a/GleeBug.sln b/GleeBug.sln index 16e139c..4c90686 100644 --- a/GleeBug.sln +++ b/GleeBug.sln @@ -8,13 +8,19 @@ EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Win32 = Debug|Win32 + Debug|x64 = Debug|x64 Release|Win32 = Release|Win32 + Release|x64 = Release|x64 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {B65A3680-9B6B-44E6-A046-649F94DF9F56}.Debug|Win32.ActiveCfg = Debug|Win32 {B65A3680-9B6B-44E6-A046-649F94DF9F56}.Debug|Win32.Build.0 = Debug|Win32 + {B65A3680-9B6B-44E6-A046-649F94DF9F56}.Debug|x64.ActiveCfg = Debug|x64 + {B65A3680-9B6B-44E6-A046-649F94DF9F56}.Debug|x64.Build.0 = Debug|x64 {B65A3680-9B6B-44E6-A046-649F94DF9F56}.Release|Win32.ActiveCfg = Release|Win32 {B65A3680-9B6B-44E6-A046-649F94DF9F56}.Release|Win32.Build.0 = Release|Win32 + {B65A3680-9B6B-44E6-A046-649F94DF9F56}.Release|x64.ActiveCfg = Release|x64 + {B65A3680-9B6B-44E6-A046-649F94DF9F56}.Release|x64.Build.0 = Release|x64 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/GleeBug/Debugger.Core.cpp b/GleeBug/Debugger.Core.cpp deleted file mode 100644 index 3f00155..0000000 --- a/GleeBug/Debugger.Core.cpp +++ /dev/null @@ -1,68 +0,0 @@ -#include "Debugger.Core.h" -#include - -namespace Debugger -{ - static DebugState state; - - bool Init(const wchar_t* szFilePath, - const wchar_t* szCommandLine, - const wchar_t* szCurrentDirectory, - ProcessInfo* process) - { - 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)) - { - szCommandLineCreateProcess = 0; - szFileNameCreateProcess = szFilePath; - } - else - { - wchar_t szCreateWithCmdLine[1024]; - swprintf_s(szCreateWithCmdLine, L"\"%s\" %s", szFilePath, szCommandLine); - szCommandLineCreateProcess = szCreateWithCmdLine; - szFileNameCreateProcess = 0; - } - - if (!CreateProcessW(szFileNameCreateProcess, - szCommandLineCreateProcess, - NULL, - NULL, - FALSE, - DEBUG_PROCESS | CREATE_NEW_CONSOLE, - NULL, - szCurrentDirectory, - &si, - &pi)) - { - return false; - } - state.Process.hProcess = pi.hProcess; - state.Process.hThread = pi.hThread; - state.Process.ProcessId = pi.dwProcessId; - state.Process.MainThreadId = pi.dwThreadId; - if (process) - *process = state.Process; - return true; - } - - bool Stop() - { - return !!TerminateProcess(state.Process.hProcess, 0); - } - - bool Detach() - { - return !!DebugActiveProcessStop(state.Process.ProcessId); - } - - DebugState* State() - { - return &state; - } -}; \ No newline at end of file diff --git a/GleeBug/Debugger.Core.h b/GleeBug/Debugger.Core.h deleted file mode 100644 index 3f101fa..0000000 --- a/GleeBug/Debugger.Core.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef _DEBUGGER_CORE_H -#define _DEBUGGER_CORE_H - -#include "_global.h" -#include "Debugger.State.h" - -namespace Debugger -{ - bool Init(const wchar_t* szFilePath, - const wchar_t* szCommandLine, - const wchar_t* szCurrentDirectory, - ProcessInfo* process); - bool Stop(); - bool Detach(); - DebugState* State(); -}; - -#endif //_DEBUGGER_CORE_H \ No newline at end of file diff --git a/GleeBug/Debugger.Data.h b/GleeBug/Debugger.Data.h new file mode 100644 index 0000000..fcf6141 --- /dev/null +++ b/GleeBug/Debugger.Data.h @@ -0,0 +1,17 @@ +#ifndef _DEBUGGER_DATA_H +#define _DEBUGGER_DATA_H + +#include "_global.h" + +namespace GleeBug +{ + 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 c48701b..3a72e94 100644 --- a/GleeBug/Debugger.Loop.cpp +++ b/GleeBug/Debugger.Loop.cpp @@ -1,99 +1,98 @@ -#include "Debugger.Loop.h" -#include "Debugger.Core.h" +#include "Debugger.h" -namespace Debugger +namespace GleeBug { - static void CreateProcessEvent(CREATE_PROCESS_DEBUG_INFO* CreateProcess, DebugState* state) + void Debugger::createProcessEvent(CREATE_PROCESS_DEBUG_INFO* CreateProcess) { - puts("> CreateProcessEvent"); + log("Debugger::createProcessEvent"); } - static void ExitProcessEvent(EXIT_PROCESS_DEBUG_INFO* ExitProcess, DebugState* state) + void Debugger::exitProcessEvent(EXIT_PROCESS_DEBUG_INFO* ExitProcess) { - puts("> ExitProcessEvent"); - if (state->DebugEvent.dwProcessId == state->Process.ProcessId) + log("Debugger::exitProcessEvent"); + if (_debugEvent.dwProcessId == _mainProcess.ProcessId) { - state->BreakDebugger = true; + _breakDebugger = true; } } - static void CreateThreadEvent(CREATE_THREAD_DEBUG_INFO* CreateThread, DebugState* state) + void Debugger::createThreadEvent(CREATE_THREAD_DEBUG_INFO* CreateThread) { - puts("> CreateThreadEvent"); + log("Debugger::createThreadEvent"); } - static void ExitThreadEvent(EXIT_THREAD_DEBUG_INFO* ExitThread, DebugState* state) + void Debugger::exitThreadEvent(EXIT_THREAD_DEBUG_INFO* ExitThread) { - puts("> ExitThreadEvent"); + log("Debugger::exitThreadEvent"); } - static void LoadDllEvent(LOAD_DLL_DEBUG_INFO* LoadDll, DebugState* state) + void Debugger::loadDllEvent(LOAD_DLL_DEBUG_INFO* LoadDll) { - puts("> LoadDllEvent"); + log("Debugger::loadDllEvent"); } - static void UnloadDllEvent(UNLOAD_DLL_DEBUG_INFO* UnloadDll, DebugState* state) + void Debugger::unloadDllEvent(UNLOAD_DLL_DEBUG_INFO* UnloadDll) { - puts("> UnloadDllEvent"); + log("Debugger::unloadDllEvent"); } - static void ExceptionEvent(EXCEPTION_DEBUG_INFO* Exception, DebugState* state) + void Debugger::exceptionEvent(EXCEPTION_DEBUG_INFO* Exception) { - puts("> ExceptionEvent"); + log("Debugger::exceptionEvent"); } - static void DebugStringEvent(OUTPUT_DEBUG_STRING_INFO* DebugString, DebugState* state) + void Debugger::debugStringEvent(OUTPUT_DEBUG_STRING_INFO* DebugString) { - puts("> DebugStringEvent"); + log("Debugger::debugStringEvent"); } - static void RipEvent(RIP_INFO* Rip, DebugState* state) + void Debugger::ripEvent(RIP_INFO* Rip) { - puts("> RipEvent"); + log("Debugger::ripEvent"); } - void Loop() + void Debugger::Start() { - DebugState* state = State(); - state->ContinueStatus = DBG_EXCEPTION_NOT_HANDLED; - while (!state->BreakDebugger) + _continueStatus = DBG_EXCEPTION_NOT_HANDLED; + _breakDebugger = false; + while (!_breakDebugger) { - if (!WaitForDebugEvent(&state->DebugEvent, INFINITE)) + if (!WaitForDebugEvent(&_debugEvent, INFINITE)) break; - switch (state->DebugEvent.dwDebugEventCode) + switch (_debugEvent.dwDebugEventCode) { case CREATE_PROCESS_DEBUG_EVENT: - CreateProcessEvent(&state->DebugEvent.u.CreateProcessInfo, state); + createProcessEvent(&_debugEvent.u.CreateProcessInfo); break; case EXIT_PROCESS_DEBUG_EVENT: - ExitProcessEvent(&state->DebugEvent.u.ExitProcess, state); + exitProcessEvent(&_debugEvent.u.ExitProcess); break; case CREATE_THREAD_DEBUG_EVENT: - CreateThreadEvent(&state->DebugEvent.u.CreateThread, state); + createThreadEvent(&_debugEvent.u.CreateThread); break; case EXIT_THREAD_DEBUG_EVENT: - ExitThreadEvent(&state->DebugEvent.u.ExitThread, state); + exitThreadEvent(&_debugEvent.u.ExitThread); break; case LOAD_DLL_DEBUG_EVENT: - LoadDllEvent(&state->DebugEvent.u.LoadDll, state); + loadDllEvent(&_debugEvent.u.LoadDll); break; case UNLOAD_DLL_DEBUG_EVENT: - UnloadDllEvent(&state->DebugEvent.u.UnloadDll, state); + unloadDllEvent(&_debugEvent.u.UnloadDll); break; case EXCEPTION_DEBUG_EVENT: - ExceptionEvent(&state->DebugEvent.u.Exception, state); + exceptionEvent(&_debugEvent.u.Exception); break; case OUTPUT_DEBUG_STRING_EVENT: - DebugStringEvent(&state->DebugEvent.u.DebugString, state); + debugStringEvent(&_debugEvent.u.DebugString); break; case RIP_EVENT: - RipEvent(&state->DebugEvent.u.RipInfo, state); + ripEvent(&_debugEvent.u.RipInfo); break; } - if (!ContinueDebugEvent(state->DebugEvent.dwProcessId, state->DebugEvent.dwThreadId, state->ContinueStatus)) + if (!ContinueDebugEvent(_debugEvent.dwProcessId, _debugEvent.dwThreadId, _continueStatus)) break; } } -} \ No newline at end of file +}; \ No newline at end of file diff --git a/GleeBug/Debugger.Loop.h b/GleeBug/Debugger.Loop.h deleted file mode 100644 index 7a0516b..0000000 --- a/GleeBug/Debugger.Loop.h +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef _DEBUGGER_LOOP_H -#define _DEBUGGER_LOOP_H - -#include "_global.h" - -namespace Debugger -{ - void Loop(); -}; - -#endif //_DEBUGGER_LOOP_H \ No newline at end of file diff --git a/GleeBug/Debugger.Misc.cpp b/GleeBug/Debugger.Misc.cpp new file mode 100644 index 0000000..0bea2a2 --- /dev/null +++ b/GleeBug/Debugger.Misc.cpp @@ -0,0 +1,9 @@ +#include "Debugger.h" + +namespace GleeBug +{ + void Debugger::log(std::string msg) + { + puts(msg.c_str()); + } +}; \ No newline at end of file diff --git a/GleeBug/Debugger.State.h b/GleeBug/Debugger.State.h deleted file mode 100644 index 99d0aa1..0000000 --- a/GleeBug/Debugger.State.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef _DEBUG_STATE_H -#define _DEBUG_STATE_H - -#include "_global.h" - -namespace Debugger -{ - struct ProcessInfo - { - HANDLE hProcess; - HANDLE hThread; - DWORD ProcessId; - DWORD MainThreadId; - }; - - struct DebugState - { - ProcessInfo Process; - DEBUG_EVENT DebugEvent; - DWORD ContinueStatus; - bool BreakDebugger; - }; -}; - -#endif //_DEBUG_STATE_H \ No newline at end of file diff --git a/GleeBug/Debugger.cpp b/GleeBug/Debugger.cpp index c4e575d..168add9 100644 --- a/GleeBug/Debugger.cpp +++ b/GleeBug/Debugger.cpp @@ -2,5 +2,65 @@ namespace GleeBug { + Debugger::Debugger() + { + } + bool Debugger::Init(const wchar_t* szFilePath, + const wchar_t* szCommandLine, + const wchar_t* szCurrentDirectory) + { + 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)) + { + szCommandLineCreateProcess = 0; + szFileNameCreateProcess = szFilePath; + } + else + { + wchar_t szCreateWithCmdLine[1024]; + swprintf_s(szCreateWithCmdLine, L"\"%s\" %s", szFilePath, szCommandLine); + szCommandLineCreateProcess = szCreateWithCmdLine; + szFileNameCreateProcess = 0; + } + + if (!CreateProcessW(szFileNameCreateProcess, + szCommandLineCreateProcess, + NULL, + NULL, + FALSE, + DEBUG_PROCESS | CREATE_NEW_CONSOLE, + NULL, + szCurrentDirectory, + &si, + &pi)) + { + return false; + } + _mainProcess.hProcess = pi.hProcess; + _mainProcess.hThread = pi.hThread; + _mainProcess.ProcessId = pi.dwProcessId; + _mainProcess.MainThreadId = pi.dwThreadId; + return true; + } + + bool Debugger::Stop() + { + return !!TerminateProcess(_mainProcess.hProcess, 0); + } + + bool Debugger::Detach() + { + return !!DebugActiveProcessStop(_mainProcess.ProcessId); + } + + const ProcessInfo & Debugger::GetMainProcess() + { + return _mainProcess; + } }; \ No newline at end of file diff --git a/GleeBug/Debugger.h b/GleeBug/Debugger.h index 12f539b..340360a 100644 --- a/GleeBug/Debugger.h +++ b/GleeBug/Debugger.h @@ -2,6 +2,7 @@ #define _DEBUGGER_H #include "_global.h" +#include "Debugger.Data.h" namespace GleeBug { @@ -44,8 +45,29 @@ namespace GleeBug */ void Start(); + /** + \brief Gets main process info. + \return The main process info. + */ + const ProcessInfo & GetMainProcess(); + + protected: + void createProcessEvent(CREATE_PROCESS_DEBUG_INFO* CreateProcess); + void exitProcessEvent(EXIT_PROCESS_DEBUG_INFO* ExitProcess); + void createThreadEvent(CREATE_THREAD_DEBUG_INFO* CreateThread); + void exitThreadEvent(EXIT_THREAD_DEBUG_INFO* ExitThread); + void loadDllEvent(LOAD_DLL_DEBUG_INFO* LoadDll); + void unloadDllEvent(UNLOAD_DLL_DEBUG_INFO* UnloadDll); + void exceptionEvent(EXCEPTION_DEBUG_INFO* Exception); + void debugStringEvent(OUTPUT_DEBUG_STRING_INFO* DebugString); + void ripEvent(RIP_INFO* Rip); + void log(std::string msg); + private: - //state variables + ProcessInfo _mainProcess; + DWORD _continueStatus; + bool _breakDebugger; + DEBUG_EVENT _debugEvent; }; }; diff --git a/GleeBug/GleeBug.vcxproj b/GleeBug/GleeBug.vcxproj index f2201fd..6273092 100644 --- a/GleeBug/GleeBug.vcxproj +++ b/GleeBug/GleeBug.vcxproj @@ -5,10 +5,18 @@ Debug Win32 + + Debug + x64 + Release Win32 + + Release + x64 + {B65A3680-9B6B-44E6-A046-649F94DF9F56} @@ -21,6 +29,12 @@ v120 MultiByte + + Application + true + v120 + MultiByte + Application false @@ -28,17 +42,41 @@ true MultiByte + + Application + false + v120 + true + MultiByte + + + + + + + - + + $(SolutionDir)bin\x32\$(Configuration)\ + + + $(SolutionDir)bin\x64\$(Configuration)\ + + + $(SolutionDir)bin\x32\$(Configuration)\ + + + $(SolutionDir)bin\x64\$(Configuration)\ + Level3 @@ -49,6 +87,16 @@ true + + + Level3 + Disabled + true + + + true + + Level3 @@ -63,18 +111,30 @@ true + + + Level3 + MaxSpeed + true + true + true + + + true + true + true + + - + - + - - diff --git a/GleeBug/GleeBug.vcxproj.filters b/GleeBug/GleeBug.vcxproj.filters index 44fcc73..58fab4c 100644 --- a/GleeBug/GleeBug.vcxproj.filters +++ b/GleeBug/GleeBug.vcxproj.filters @@ -18,34 +18,28 @@ Source Files - - Source Files - Source Files - - Source Files - Source Files + + Source Files + + + Source Files + - - Header Files - Header Files - - Header Files - - - Header Files - Header Files + + Header Files + \ No newline at end of file diff --git a/GleeBug/_global.h b/GleeBug/_global.h index 2aa6ad9..ddd6f81 100644 --- a/GleeBug/_global.h +++ b/GleeBug/_global.h @@ -2,6 +2,7 @@ #define _GLOBAL_H #include +#include #include #endif //_GLOBAL_H \ No newline at end of file diff --git a/GleeBug/main.cpp b/GleeBug/main.cpp index b6bc444..c175a3d 100644 --- a/GleeBug/main.cpp +++ b/GleeBug/main.cpp @@ -1,17 +1,17 @@ #include -#include "Debugger.Core.h" -#include "Debugger.Loop.h" +#include "Debugger.h" int main() { wchar_t szFilePath[256] = L"c:\\CodeBlocks\\arma_cert_bin_info\\bin\\arma_cert_bin_info.exe"; wchar_t szCommandLine[256] = L""; wchar_t szCurrentDir[256] = L"c:\\CodeBlocks\\arma_cert_bin_info\\bin"; - Debugger::ProcessInfo process; - if (Debugger::Init(szFilePath, NULL, szCurrentDir, &process)) + GleeBug::Debugger dbg; + if (dbg.Init(szFilePath, szCommandLine, szCurrentDir)) { - printf("Debugger::Init success! PID: %X\n", process.ProcessId); - Debugger::Loop(); + printf("Debugger::Init success! PID: %X\n", dbg.GetMainProcess().ProcessId); + dbg.Start(); + printf("Debugger::Start finished!"); } else {