1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
#include <stdio.h>
#include <windows.h>
int main()
{
DWORD dwStatus;
BOOL bFirstBreak;
CONTEXT Context = { 0 };
DEBUG_EVENT DebugEvent = { 0 };
STARTUPINFO StartupInfo = { 0 };
PROCESS_INFORMATION ProcessInformation = { 0 };
if (!CreateProcess(NULL, "c:\\a.exe", NULL, NULL, FALSE, DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS, NULL, NULL, &StartupInfo, &ProcessInformation)) {
printf("Process failed\n");
return -1;
}
printf("Process created\n");
bFirstBreak = TRUE;
while (TRUE) {
WaitForDebugEvent(&DebugEvent, INFINITE);
dwStatus = DBG_EXCEPTION_NOT_HANDLED;
if (DebugEvent.dwDebugEventCode == CREATE_PROCESS_DEBUG_EVENT) {
printf("Debug start\n");
} else if (DebugEvent.dwDebugEventCode == EXIT_PROCESS_DEBUG_EVENT) {
printf("Debug end\n");
break;
} else if (DebugEvent.dwDebugEventCode == EXCEPTION_DEBUG_EVENT) {
EXCEPTION_RECORD ExceptionRecord = DebugEvent.u.Exception.ExceptionRecord;
if (ExceptionRecord.ExceptionCode == EXCEPTION_BREAKPOINT) {
if (bFirstBreak == TRUE) {
bFirstBreak = FALSE;
Context.ContextFlags = CONTEXT_CONTROL;
GetThreadContext(ProcessInformation.hThread, &Context);
Context.EFlags = Context.EFlags | 0x100;
SetThreadContext(ProcessInformation.hThread, &Context);
dwStatus = DBG_CONTINUE;
}
} else if (ExceptionRecord.ExceptionCode == EXCEPTION_SINGLE_STEP) {
dwStatus = DBG_CONTINUE;
if (ProcessInformation.dwThreadId == DebugEvent.dwThreadId) {
Context.ContextFlags = CONTEXT_CONTROL;
GetThreadContext(ProcessInformation.hThread, &Context);
printf("%08X %08X\n", Context.Eip, ExceptionRecord.ExceptionAddress);
Context.EFlags = Context.EFlags | 0x100;
SetThreadContext(ProcessInformation.hThread, &Context);
}
}
}
ContinueDebugEvent(DebugEvent.dwProcessId, DebugEvent.dwThreadId, dwStatus);
}
return 0;
} |
Partager