/* DbugLoop.cpp : Demonstrating the Debug Loop Author : Mithun Shanbhag, mithunshanbhag@yahoo.com Resources : MSDN Online Documentation. OS : Platform : */ /* ================== Module Description ================== */ /* */ /* includes */ #include #include #include #include using namespace std ; //----< outputs string in Heading format >---- void Title ( const std::string & str, char ch ) { for ( int i = 0 ; i < ( int ) str.length() ; i ++ ) std::cout << ch ; std::cout << "\n" << str << "\n" ; for ( int i = 0 ; i < ( int ) str.length() ; i ++ ) std::cout << ch ; std::cout << "\n\n" ; } //----< Display Information on a CREATE_PROCESS_DEBUG_EVENT event >---- void CreateProcessDebugEventInfo ( CREATE_PROCESS_DEBUG_INFO & cpdi ) { Title ( "Debug Event : Create Process", '-' ) ; cout << setw(22) << right << "hFile" << " : " << hex << "0x" << cpdi.hFile << "\n" << setw(22) << right << "hProcess" << " : " << hex << "0x" << cpdi.hProcess << "\n" << setw(22) << right << "hThread" << " : " << hex << "0x" << cpdi.hThread << "\n" << setw(22) << right << "lpBaseOfImage" << " : " << hex << "0x" << cpdi.lpBaseOfImage << "\n" << setw(22) << right << "dwDebugInfoFileOffset" << " : " << hex << "0x" << cpdi.dwDebugInfoFileOffset << "\n" << setw(22) << right << "nDebugInfoSize" << " : " << hex << "0x" << cpdi.nDebugInfoSize << "\n" << setw(22) << right << "lpThreadLocalBase" << " : " << hex << "0x" << cpdi.lpThreadLocalBase << "\n" << setw(22) << right << "lpStartAddress" << " : " << hex << "0x" << cpdi.lpStartAddress << "\n" << setw(22) << right << "lpImageName" << " : " << hex << "0x" << cpdi.lpImageName << "\n" << setw(22) << right << "fUnicode" << " : " << hex << "0x" << cpdi.fUnicode << "\n\n" ; } //----< Display Information on a CREATE_THREAD_DEBUG_EVENT event >---- void CreateThreadDebugEventInfo ( CREATE_THREAD_DEBUG_INFO & ctdi ) { Title ( "Debug Event : Create Thread", '-' ) ; cout << setw(22) << right << "hThread" << " : " << hex << "0x" << ctdi.hThread << "\n" << setw(22) << right << "lpStartAddress" << " : " << hex << "0x" << ctdi.lpStartAddress << "\n" << setw(22) << right << "lpThreadLocalBase" << " : " << hex << "0x" << ctdi.lpThreadLocalBase << "\n\n" ; } //----< Display Information on an EXIT_PROCESS_DEBUG_EVENT event >---- void ExitProcessDebugEventInfo ( EXIT_PROCESS_DEBUG_INFO & epdi ) { Title ( "Debug Event : Exit Process", '-' ) ; cout << setw(22) << right << "dwExitCode" << " : " << hex << "0x" << epdi.dwExitCode << "\n\n" ; } //----< Display Information on an EXIT_THREAD_DEBUG_EVENT event >---- void ExitThreadDebugEventInfo ( EXIT_THREAD_DEBUG_INFO & etdi ) { Title ( "Debug Event : Exit Thread", '-' ) ; cout << setw(22) << right << "dwExitCode" << " : " << hex << "0x" << etdi.dwExitCode << "\n\n" ; } //----< Display Information on a LOAD_DLL_DEBUG_EVENT event >---- void LoadDllDebugEventInfo ( LOAD_DLL_DEBUG_INFO & lddi ) { Title ( "Debug Event : Load Dll", '-' ) ; cout << setw(22) << right << "dwDebugInfoFileOffset" << " : " << hex << "0x" << lddi.dwDebugInfoFileOffset << "\n" << setw(22) << right << "fUnicode" << " : " << hex << "0x" << lddi.fUnicode << "\n" << setw(22) << right << "hFile" << " : " << hex << "0x" << lddi.hFile << "\n" << setw(22) << right << "lpBaseOfDll" << " : " << hex << "0x" << lddi.lpBaseOfDll << "\n" << setw(22) << right << "lpImageName" << " : " << hex << "0x" << lddi.lpImageName << "\n" << setw(22) << right << "nDebugInfoSize" << " : " << hex << "0x" << lddi.nDebugInfoSize << "\n\n" ; } //----< Display Information on an UNLOAD_DLL_DEBUG_EVENT event >---- void UnloadDllDebugEventInfo ( UNLOAD_DLL_DEBUG_INFO & uddi ) { Title ( "Debug Event : Unload Dll", '-' ) ; cout << setw(22) << right << "lpBaseOfDll" << " : " << hex << "0x" << uddi.lpBaseOfDll << "\n\n" ; } //----< Display Information on an OUTPUT_DEBUG_STRING_EVENT event >---- void OutputDebugStringDebugEventInfo ( OUTPUT_DEBUG_STRING_INFO & odsi ) { Title ( "Debug Event : Output Debug String", '-' ) ; cout << setw(22) << right << "fUnicode" << " : " << hex << "0x" << odsi.fUnicode << "\n" << setw(22) << right << "nDebugStringLength" << " : " << hex << "0x" << odsi.nDebugStringLength << "\n" // Actual Usage -> use ReadProcessMemory to read the string output to the debugger << setw(22) << right << "lpDebugStringData" << " : " << hex << "0x" << (LPVOID) odsi.lpDebugStringData << "\n\n" ; } //----< Display Information on an EXCEPTION_DEBUG_EVENT event >---- void ExceptionDebugEventInfo ( EXCEPTION_DEBUG_INFO & edi ) { Title ( "Debug Event : Exception", '-' ) ; cout << setw(22) << right << "dwFirstChance" << " : " << hex << "0x" << edi.dwFirstChance << "\n" << setw(22) << right << "ExceptionCode" << " : " << hex << "0x" << edi.ExceptionRecord.ExceptionCode << "\n" << setw(22) << right << "ExceptionFlags" << " : " << hex << "0x" << edi.ExceptionRecord.ExceptionFlags << "\n" << setw(22) << right << "ExceptionRecord" << " : " << hex << "0x" << edi.ExceptionRecord.ExceptionRecord << "\n" << setw(22) << right << "ExceptionAddress" << " : " << hex << "0x" << edi.ExceptionRecord.ExceptionAddress << "\n" << setw(22) << right << "NumberParameters" << " : " << hex << "0x" << edi.ExceptionRecord.NumberParameters << "\n\n" ; } //----< Main Entry Routine >---- void main () { Title ( "DEMONSTRATING THE DEBUG LOOP", '*' ) ; DEBUG_EVENT DbEv ; bool bContinue ; DWORD dwContinueStatus ; STARTUPINFO si ; PROCESS_INFORMATION pi ; GetStartupInfo ( & si ) ; // FIX THIS -> take app name from command line CreateProcess ( "../exe/Debug/exe.exe", /* application name */ NULL, /* command line */ NULL, /* process attributes */ NULL, /* thread attributes */ FALSE, /* inherit handles */ CREATE_NEW_CONSOLE | DEBUG_ONLY_THIS_PROCESS, /* creation flags */ NULL, /* environment */ NULL, /* current directory */ & si, /* startup info */ & pi /* process information */ ) ; CloseHandle ( pi.hProcess ) ; CloseHandle ( pi.hThread ) ; bContinue = true ; dwContinueStatus = DBG_CONTINUE ; while ( bContinue == true ) { WaitForDebugEvent ( & DbEv, INFINITE ) ; switch ( DbEv.dwDebugEventCode ) { /* Generated whenever a new process is created in a process being debugged or whenever the debugger begins debugging an already active process. The system generates this debugging event before the process begins to execute in user mode and before the system generates any other debugging events for the new process. */ case CREATE_PROCESS_DEBUG_EVENT : CreateProcessDebugEventInfo ( DbEv.u.CreateProcessInfo ) ; dwContinueStatus = DBG_CONTINUE ; break ; /* Generated whenever a new thread is created in a process being debugged or whenever the debugger begins debugging an already active process. This debugging event is generated before the new thread begins to execute in user mode. */ case CREATE_THREAD_DEBUG_EVENT : CreateThreadDebugEventInfo ( DbEv.u.CreateThread ) ; dwContinueStatus = DBG_CONTINUE ; break ; /* Generated whenever the last thread in a process being debugged exits. This debugging event occurs immediately after the system unloads the process's DLLs and updates the process's exit code. NOTE - The debugger deallocates any internal structures associated with the process on receipt of this debugging event. The "system" closes the debugger's handle to the exiting process and all of the process's threads. The "debugger" should not close these handles. NOTE - The kernel-mode portion of process shutdown cannot be completed until the debugger that receives this event calls ContinueDebugEvent(). Until then, the process handles are open and the virtual address space is not released, so the debugger can examine the child process. To receive notification when the kernel-mode portion of process shutdown is complete, duplicate the handle returned with CREATE_PROCESS_DEBUG_EVENT, call ContinueDebugEvent(), and then wait for the duplicated process handle to be signaled. */ case EXIT_PROCESS_DEBUG_EVENT : ExitProcessDebugEventInfo ( DbEv.u.ExitProcess ) ; bContinue = false ; dwContinueStatus = DBG_CONTINUE ; break ; /* Generated whenever a thread that is part of a process being debugged exits. The system generates this debugging event immediately after it updates the thread's exit code. NOTE - This debugging event does not occur if the exiting thread is the last thread of a process. In this case, the EXIT_PROCESS_DEBUG_EVENT debugging event occurs instead. NOTE - The debugger deallocates any internal structures associated with the thread on receipt of this debugging event. The "system" closes the debugger's handle to the exiting thread. The "debugger" should not close this handle. */ case EXIT_THREAD_DEBUG_EVENT : ExitThreadDebugEventInfo ( DbEv.u.ExitThread ) ; dwContinueStatus = DBG_CONTINUE ; break ; /* Generated whenever a process being debugged loads a DLL. This debugging event occurs when the system loader resolves links to a DLL or when the debugged process uses the LoadLibrary() function. This debugging event is called each time the dll loads into the address space. If the DLL's reference count falls to 0, the DLL is unloaded. The next time the DLL is loaded, this event will be generated again. NOTE - Typically, a debugger loads a symbol table associated with the DLL on receipt of this debugging event. */ case LOAD_DLL_DEBUG_EVENT : LoadDllDebugEventInfo ( DbEv.u.LoadDll ) ; dwContinueStatus = DBG_CONTINUE ; break ; /* Generated whenever a process being debugged unloads a DLL by using the FreeLibrary() function. This debugging event only occurs the last time a DLL is unloaded from a process's address space (that is, when the DLL's usage count is zero). NOTE - Typically, a debugger unloads a symbol table associated with the DLL upon receiving this debugging event. NOTE - When a process exits, the system automatically unloads the process's DLLs, but does not generate an UNLOAD_DLL_DEBUG_EVENT debugging event. */ case UNLOAD_DLL_DEBUG_EVENT : UnloadDllDebugEventInfo ( DbEv.u.UnloadDll ) ; dwContinueStatus = DBG_CONTINUE ; break ; /* Generated when a process being debugged uses the OutputDebugString function. */ case OUTPUT_DEBUG_STRING_EVENT : OutputDebugStringDebugEventInfo ( DbEv.u.DebugString ) ; dwContinueStatus = DBG_CONTINUE ; break ; case RIP_EVENT : break ; /* Generated whenever an exception occurs in the process being debugged. Possible exceptions include attempting to access inaccessible memory, executing breakpoint instructions, attempting to divide by zero, or any other exception noted in Structured Exception Handling. */ case EXCEPTION_DEBUG_EVENT : ExceptionDebugEventInfo ( DbEv.u.Exception ) ; switch ( DbEv.u.Exception.ExceptionRecord.ExceptionCode ) { case EXCEPTION_ACCESS_VIOLATION : case EXCEPTION_ARRAY_BOUNDS_EXCEEDED : case EXCEPTION_BREAKPOINT : case EXCEPTION_DATATYPE_MISALIGNMENT : case EXCEPTION_FLT_DENORMAL_OPERAND : case EXCEPTION_FLT_DIVIDE_BY_ZERO : case EXCEPTION_FLT_INEXACT_RESULT : case EXCEPTION_FLT_INVALID_OPERATION : case EXCEPTION_FLT_OVERFLOW : case EXCEPTION_FLT_STACK_CHECK : case EXCEPTION_FLT_UNDERFLOW : case EXCEPTION_ILLEGAL_INSTRUCTION : case EXCEPTION_IN_PAGE_ERROR : case EXCEPTION_INT_DIVIDE_BY_ZERO : case EXCEPTION_INT_OVERFLOW : case EXCEPTION_INVALID_DISPOSITION : case EXCEPTION_NONCONTINUABLE_EXCEPTION : case EXCEPTION_PRIV_INSTRUCTION : case EXCEPTION_SINGLE_STEP : case EXCEPTION_STACK_OVERFLOW : case DBG_CONTROL_C : default : break ; } break ; default : break ; } ContinueDebugEvent ( DbEv.dwProcessId, DbEv.dwThreadId, dwContinueStatus ) ; } Title ( "END OF DEMONSTRATION", '*' ) ; }