Guest User

Untitled

a guest
May 22nd, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.64 KB | None | 0 0
  1. #include <windows.h>
  2.  
  3. DWORD OnCreateThreadDebugEvent(const LPDEBUG_EVENT);
  4. DWORD OnCreateProcessDebugEvent(const LPDEBUG_EVENT);
  5. DWORD OnExitThreadDebugEvent(const LPDEBUG_EVENT);
  6. DWORD OnExitProcessDebugEvent(const LPDEBUG_EVENT);
  7. DWORD OnLoadDllDebugEvent(const LPDEBUG_EVENT);
  8. DWORD OnUnloadDllDebugEvent(const LPDEBUG_EVENT);
  9. DWORD OnOutputDebugStringEvent(const LPDEBUG_EVENT);
  10. DWORD OnRipEvent(const LPDEBUG_EVENT);
  11.  
  12. void EnterDebugLoop(const LPDEBUG_EVENT DebugEv)
  13. {
  14. DWORD dwContinueStatus = DBG_CONTINUE; // exception continuation
  15.  
  16. for(;;)
  17. {
  18. // Wait for a debugging event to occur. The second parameter indicates
  19. // that the function does not return until a debugging event occurs.
  20.  
  21. WaitForDebugEvent(DebugEv, INFINITE);
  22.  
  23. // Process the debugging event code.
  24.  
  25. switch (DebugEv->dwDebugEventCode)
  26. {
  27. case EXCEPTION_DEBUG_EVENT:
  28. // Process the exception code. When handling
  29. // exceptions, remember to set the continuation
  30. // status parameter (dwContinueStatus). This value
  31. // is used by the ContinueDebugEvent function.
  32.  
  33. switch(DebugEv->u.Exception.ExceptionRecord.ExceptionCode)
  34. {
  35. case EXCEPTION_ACCESS_VIOLATION:
  36. // First chance: Pass this on to the system.
  37. // Last chance: Display an appropriate error.
  38. break;
  39.  
  40. case EXCEPTION_BREAKPOINT:
  41. // First chance: Display the current
  42. // instruction and register values.
  43. break;
  44.  
  45. case EXCEPTION_DATATYPE_MISALIGNMENT:
  46. // First chance: Pass this on to the system.
  47. // Last chance: Display an appropriate error.
  48. break;
  49.  
  50. case EXCEPTION_SINGLE_STEP:
  51. // First chance: Update the display of the
  52. // current instruction and register values.
  53. break;
  54.  
  55. case DBG_CONTROL_C:
  56. // First chance: Pass this on to the system.
  57. // Last chance: Display an appropriate error.
  58. break;
  59.  
  60. default:
  61. // Handle other exceptions.
  62. break;
  63. }
  64.  
  65. break;
  66.  
  67. case CREATE_THREAD_DEBUG_EVENT:
  68. // As needed, examine or change the thread's registers
  69. // with the GetThreadContext and SetThreadContext functions;
  70. // and suspend and resume thread execution with the
  71. // SuspendThread and ResumeThread functions.
  72.  
  73. dwContinueStatus = OnCreateThreadDebugEvent(DebugEv);
  74. break;
  75.  
  76. case CREATE_PROCESS_DEBUG_EVENT:
  77. // As needed, examine or change the registers of the
  78. // process's initial thread with the GetThreadContext and
  79. // SetThreadContext functions; read from and write to the
  80. // process's virtual memory with the ReadProcessMemory and
  81. // WriteProcessMemory functions; and suspend and resume
  82. // thread execution with the SuspendThread and ResumeThread
  83. // functions. Be sure to close the handle to the process image
  84. // file with CloseHandle.
  85.  
  86. dwContinueStatus = OnCreateProcessDebugEvent(DebugEv);
  87. break;
  88.  
  89. case EXIT_THREAD_DEBUG_EVENT:
  90. // Display the thread's exit code.
  91.  
  92. dwContinueStatus = OnExitThreadDebugEvent(DebugEv);
  93. break;
  94.  
  95. case EXIT_PROCESS_DEBUG_EVENT:
  96. // Display the process's exit code.
  97.  
  98. dwContinueStatus = OnExitProcessDebugEvent(DebugEv);
  99. break;
  100.  
  101. case LOAD_DLL_DEBUG_EVENT:
  102. // Read the debugging information included in the newly
  103. // loaded DLL. Be sure to close the handle to the loaded DLL
  104. // with CloseHandle.
  105.  
  106. dwContinueStatus = OnLoadDllDebugEvent(DebugEv);
  107. break;
  108.  
  109. case UNLOAD_DLL_DEBUG_EVENT:
  110. // Display a message that the DLL has been unloaded.
  111.  
  112. dwContinueStatus = OnUnloadDllDebugEvent(DebugEv);
  113. break;
  114.  
  115. case OUTPUT_DEBUG_STRING_EVENT:
  116. // Display the output debugging string.
  117.  
  118. dwContinueStatus = OnOutputDebugStringEvent(DebugEv);
  119. break;
  120.  
  121. case RIP_EVENT:
  122. dwContinueStatus = OnRipEvent(DebugEv);
  123. break;
  124. }
  125.  
  126. // Resume executing the thread that reported the debugging event.
  127.  
  128. ContinueDebugEvent(DebugEv->dwProcessId,
  129. DebugEv->dwThreadId,
  130. dwContinueStatus);
  131. }
  132. }
Add Comment
Please, Sign In to add comment