From 946abf3aed154d046e619bd28b27caf19b7c8bb6 Mon Sep 17 00:00:00 2001 From: "Mr. eXoDia" Date: Sun, 22 Mar 2015 12:50:55 +0100 Subject: [PATCH] debug loop --- JoyBug/Debugger.Core.cpp | 19 ++++--- JoyBug/Debugger.Core.h | 1 + JoyBug/Debugger.Loop.cpp | 99 +++++++++++++++++++++++++++++++++++ JoyBug/Debugger.Loop.h | 11 ++++ JoyBug/Debugger.State.h | 24 +++++---- JoyBug/JoyBug.vcxproj | 2 + JoyBug/JoyBug.vcxproj.filters | 6 +++ JoyBug/main.cpp | 6 +-- 8 files changed, 149 insertions(+), 19 deletions(-) create mode 100644 JoyBug/Debugger.Loop.cpp create mode 100644 JoyBug/Debugger.Loop.h diff --git a/JoyBug/Debugger.Core.cpp b/JoyBug/Debugger.Core.cpp index cd1ded8..3f00155 100644 --- a/JoyBug/Debugger.Core.cpp +++ b/JoyBug/Debugger.Core.cpp @@ -42,22 +42,27 @@ namespace Debugger { return false; } - state.process.hProcess = pi.hProcess; - state.process.hThread = pi.hThread; - state.process.ProcessId = pi.dwProcessId; - state.process.MainThreadId = pi.dwThreadId; + 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; + *process = state.Process; return true; } bool Stop() { - return !!TerminateProcess(state.process.hProcess, 0); + return !!TerminateProcess(state.Process.hProcess, 0); } bool Detach() { - return !!DebugActiveProcessStop(state.process.ProcessId); + return !!DebugActiveProcessStop(state.Process.ProcessId); + } + + DebugState* State() + { + return &state; } }; \ No newline at end of file diff --git a/JoyBug/Debugger.Core.h b/JoyBug/Debugger.Core.h index e6ce903..3f101fa 100644 --- a/JoyBug/Debugger.Core.h +++ b/JoyBug/Debugger.Core.h @@ -12,6 +12,7 @@ namespace Debugger ProcessInfo* process); bool Stop(); bool Detach(); + DebugState* State(); }; #endif //_DEBUGGER_CORE_H \ No newline at end of file diff --git a/JoyBug/Debugger.Loop.cpp b/JoyBug/Debugger.Loop.cpp new file mode 100644 index 0000000..c48701b --- /dev/null +++ b/JoyBug/Debugger.Loop.cpp @@ -0,0 +1,99 @@ +#include "Debugger.Loop.h" +#include "Debugger.Core.h" + +namespace Debugger +{ + static void CreateProcessEvent(CREATE_PROCESS_DEBUG_INFO* CreateProcess, DebugState* state) + { + puts("> CreateProcessEvent"); + } + + static void ExitProcessEvent(EXIT_PROCESS_DEBUG_INFO* ExitProcess, DebugState* state) + { + puts("> ExitProcessEvent"); + if (state->DebugEvent.dwProcessId == state->Process.ProcessId) + { + state->BreakDebugger = true; + } + } + + static void CreateThreadEvent(CREATE_THREAD_DEBUG_INFO* CreateThread, DebugState* state) + { + puts("> CreateThreadEvent"); + } + + static void ExitThreadEvent(EXIT_THREAD_DEBUG_INFO* ExitThread, DebugState* state) + { + puts("> ExitThreadEvent"); + } + + static void LoadDllEvent(LOAD_DLL_DEBUG_INFO* LoadDll, DebugState* state) + { + puts("> LoadDllEvent"); + } + + static void UnloadDllEvent(UNLOAD_DLL_DEBUG_INFO* UnloadDll, DebugState* state) + { + puts("> UnloadDllEvent"); + } + + static void ExceptionEvent(EXCEPTION_DEBUG_INFO* Exception, DebugState* state) + { + puts("> ExceptionEvent"); + } + + static void DebugStringEvent(OUTPUT_DEBUG_STRING_INFO* DebugString, DebugState* state) + { + puts("> DebugStringEvent"); + } + + static void RipEvent(RIP_INFO* Rip, DebugState* state) + { + puts("> RipEvent"); + } + + void Loop() + { + DebugState* state = State(); + state->ContinueStatus = DBG_EXCEPTION_NOT_HANDLED; + while (!state->BreakDebugger) + { + if (!WaitForDebugEvent(&state->DebugEvent, INFINITE)) + break; + + switch (state->DebugEvent.dwDebugEventCode) + { + case CREATE_PROCESS_DEBUG_EVENT: + CreateProcessEvent(&state->DebugEvent.u.CreateProcessInfo, state); + break; + case EXIT_PROCESS_DEBUG_EVENT: + ExitProcessEvent(&state->DebugEvent.u.ExitProcess, state); + break; + case CREATE_THREAD_DEBUG_EVENT: + CreateThreadEvent(&state->DebugEvent.u.CreateThread, state); + break; + case EXIT_THREAD_DEBUG_EVENT: + ExitThreadEvent(&state->DebugEvent.u.ExitThread, state); + break; + case LOAD_DLL_DEBUG_EVENT: + LoadDllEvent(&state->DebugEvent.u.LoadDll, state); + break; + case UNLOAD_DLL_DEBUG_EVENT: + UnloadDllEvent(&state->DebugEvent.u.UnloadDll, state); + break; + case EXCEPTION_DEBUG_EVENT: + ExceptionEvent(&state->DebugEvent.u.Exception, state); + break; + case OUTPUT_DEBUG_STRING_EVENT: + DebugStringEvent(&state->DebugEvent.u.DebugString, state); + break; + case RIP_EVENT: + RipEvent(&state->DebugEvent.u.RipInfo, state); + break; + } + + if (!ContinueDebugEvent(state->DebugEvent.dwProcessId, state->DebugEvent.dwThreadId, state->ContinueStatus)) + break; + } + } +} \ No newline at end of file diff --git a/JoyBug/Debugger.Loop.h b/JoyBug/Debugger.Loop.h new file mode 100644 index 0000000..7a0516b --- /dev/null +++ b/JoyBug/Debugger.Loop.h @@ -0,0 +1,11 @@ +#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/JoyBug/Debugger.State.h b/JoyBug/Debugger.State.h index abc991b..99d0aa1 100644 --- a/JoyBug/Debugger.State.h +++ b/JoyBug/Debugger.State.h @@ -3,17 +3,23 @@ #include "_global.h" -struct ProcessInfo +namespace Debugger { - HANDLE hProcess; - HANDLE hThread; - DWORD ProcessId; - DWORD MainThreadId; -}; + struct ProcessInfo + { + HANDLE hProcess; + HANDLE hThread; + DWORD ProcessId; + DWORD MainThreadId; + }; -struct DebugState -{ - ProcessInfo process; + struct DebugState + { + ProcessInfo Process; + DEBUG_EVENT DebugEvent; + DWORD ContinueStatus; + bool BreakDebugger; + }; }; #endif //_DEBUG_STATE_H \ No newline at end of file diff --git a/JoyBug/JoyBug.vcxproj b/JoyBug/JoyBug.vcxproj index 55bc210..7023a71 100644 --- a/JoyBug/JoyBug.vcxproj +++ b/JoyBug/JoyBug.vcxproj @@ -65,11 +65,13 @@ + + diff --git a/JoyBug/JoyBug.vcxproj.filters b/JoyBug/JoyBug.vcxproj.filters index 729d120..f86752c 100644 --- a/JoyBug/JoyBug.vcxproj.filters +++ b/JoyBug/JoyBug.vcxproj.filters @@ -24,6 +24,9 @@ Source Files + + Source Files + @@ -35,5 +38,8 @@ Header Files + + Header Files + \ No newline at end of file diff --git a/JoyBug/main.cpp b/JoyBug/main.cpp index 1911c48..b6bc444 100644 --- a/JoyBug/main.cpp +++ b/JoyBug/main.cpp @@ -1,17 +1,17 @@ #include #include "Debugger.Core.h" +#include "Debugger.Loop.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"; - ProcessInfo process; + Debugger::ProcessInfo process; if (Debugger::Init(szFilePath, NULL, szCurrentDir, &process)) { printf("Debugger::Init success! PID: %X\n", process.ProcessId); - bool bDetached = Debugger::Detach(); - printf("Debugger::Detach returned %s\n", bDetached ? "true" : "false"); + Debugger::Loop(); } else {