Skip to content

Instantly share code, notes, and snippets.

@egomeh
Created November 22, 2020 14:35
Show Gist options
  • Save egomeh/c632406d67e5bdf2906779d743ebbe9b to your computer and use it in GitHub Desktop.
Save egomeh/c632406d67e5bdf2906779d743ebbe9b to your computer and use it in GitHub Desktop.
#include <Windows.h>
#include <iostream>
int main()
{
STARTUPINFO startupInfo;
PROCESS_INFORMATION processInfo;
ZeroMemory(&startupInfo, sizeof(startupInfo));
startupInfo.cb = sizeof(startupInfo);
ZeroMemory(&processInfo, sizeof(processInfo));
CreateProcess(L"CrashingProgram.exe", NULL, NULL, NULL, FALSE, 0, NULL, NULL, &startupInfo, &processInfo);
DebugActiveProcess(processInfo.dwProcessId);
while (true)
{
DEBUG_EVENT debugEvent;
if (!WaitForDebugEvent(&debugEvent, 2'000))
break;
const EXCEPTION_RECORD& exceptionRecord = debugEvent.u.Exception.ExceptionRecord;
const DWORD exceptionCode = exceptionRecord.ExceptionCode;
// We only care about access violation events
if (debugEvent.dwDebugEventCode == EXCEPTION_DEBUG_EVENT && exceptionCode == EXCEPTION_ACCESS_VIOLATION)
{
DWORD64 address = debugEvent.u.Exception.ExceptionRecord.ExceptionInformation[1];
std::cout << "Access violation at address: " << std::hex << address << std::endl;
return 0;
}
ContinueDebugEvent(debugEvent.dwProcessId, debugEvent.dwThreadId, DBG_CONTINUE);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment