diff --git a/x64_dbg_exe/crashdump.cpp b/x64_dbg_exe/crashdump.cpp new file mode 100644 index 00000000..ada194d5 --- /dev/null +++ b/x64_dbg_exe/crashdump.cpp @@ -0,0 +1,87 @@ +#include +#include +#include +#include "crashdump.h" + +BOOL +(WINAPI* + MiniDumpWriteDumpPtr)( + _In_ HANDLE hProcess, + _In_ DWORD ProcessId, + _In_ HANDLE hFile, + _In_ MINIDUMP_TYPE DumpType, + _In_opt_ PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam, + _In_opt_ PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam, + _In_opt_ PMINIDUMP_CALLBACK_INFORMATION CallbackParam + ); + +void CrashDumpInitialize() +{ + // Find the DbgHelp module first + HMODULE module = LoadLibrary("dbghelp.dll"); + + if(module) + *(FARPROC*)&MiniDumpWriteDumpPtr = GetProcAddress(module, "MiniDumpWriteDump"); + + if(MiniDumpWriteDumpPtr) + AddVectoredExceptionHandler(0, CrashDumpVectoredHandler); +} + +void CrashDumpFatal(const char* Format, ...) +{ + char buffer[1024]; + va_list va; + + va_start(va, Format); + vsnprintf_s(buffer, _TRUNCATE, Format, va); + va_end(va); + + MessageBox(nullptr, buffer, "Error", MB_ICONERROR); +} + +void CrashDumpCreate(EXCEPTION_POINTERS* ExceptionPointers) +{ + // Generate a crash dump file in the root directory + wchar_t dumpDir[MAX_PATH]; + wchar_t currentDir[MAX_PATH]; + + if(!GetCurrentDirectoryW(ARRAYSIZE(currentDir), currentDir)) + { + CrashDumpFatal("Unable to obtain current directory during crash dump\n"); + return; + } + + // Append the name + swprintf_s(dumpDir, L"%ws\\minidump-%p.dmp", currentDir, ExceptionPointers->ContextRecord->Rip); + + // Open the file + HANDLE fileHandle = CreateFileW(dumpDir, GENERIC_READ | GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); + + if(fileHandle == INVALID_HANDLE_VALUE) + { + CrashDumpFatal("Failed to open file path '%ws' while generating crash dump\n", dumpDir); + return; + } + + // Create the mini dump with DbgHelp + MINIDUMP_EXCEPTION_INFORMATION info; + memset(&info, 0, sizeof(MINIDUMP_EXCEPTION_INFORMATION)); + + info.ThreadId = GetCurrentThreadId(); + info.ExceptionPointers = ExceptionPointers; + info.ClientPointers = TRUE; + + if(!MiniDumpWriteDumpPtr(GetCurrentProcess(), GetCurrentProcessId(), fileHandle, MiniDumpNormal, &info, nullptr, nullptr)) + CrashDumpFatal("MiniDumpWriteDump failed. Error: %u\n", GetLastError()); + + // Close the file & done + CloseHandle(fileHandle); +} + +LONG CALLBACK CrashDumpVectoredHandler(EXCEPTION_POINTERS* ExceptionInfo) +{ + if(ExceptionInfo) + CrashDumpCreate(ExceptionInfo); + + return EXCEPTION_CONTINUE_SEARCH; +} \ No newline at end of file diff --git a/x64_dbg_exe/crashdump.h b/x64_dbg_exe/crashdump.h new file mode 100644 index 00000000..c28741c9 --- /dev/null +++ b/x64_dbg_exe/crashdump.h @@ -0,0 +1,4 @@ +#pragma once + +void CrashDumpInitialize(); +LONG CALLBACK CrashDumpVectoredHandler(EXCEPTION_POINTERS* ExceptionInfo); \ No newline at end of file diff --git a/x64_dbg_exe/x64_dbg_exe.cpp b/x64_dbg_exe/x64_dbg_exe.cpp index 63221261..6046f1b9 100644 --- a/x64_dbg_exe/x64_dbg_exe.cpp +++ b/x64_dbg_exe/x64_dbg_exe.cpp @@ -6,6 +6,7 @@ #include #include +#include "crashdump.h" #include "..\x64_dbg_bridge\bridgemain.h" /** @@ -23,6 +24,8 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { + CrashDumpInitialize(); + const char* errormsg = BridgeInit(); if(errormsg) { diff --git a/x64_dbg_exe/x64_dbg_exe.vcxproj b/x64_dbg_exe/x64_dbg_exe.vcxproj index a4f49b51..6599b178 100644 --- a/x64_dbg_exe/x64_dbg_exe.vcxproj +++ b/x64_dbg_exe/x64_dbg_exe.vcxproj @@ -19,9 +19,11 @@ + + diff --git a/x64_dbg_exe/x64_dbg_exe.vcxproj.filters b/x64_dbg_exe/x64_dbg_exe.vcxproj.filters index 26d9b663..5163459d 100644 --- a/x64_dbg_exe/x64_dbg_exe.vcxproj.filters +++ b/x64_dbg_exe/x64_dbg_exe.vcxproj.filters @@ -18,11 +18,17 @@ Source Files + + Source Files + Header Files + + Header Files +