diff options
author | Daniel Wilhelm <daniel@wili.li> | 2014-04-18 17:30:42 +0200 |
---|---|---|
committer | Daniel Wilhelm <daniel@wili.li> | 2014-04-18 17:30:42 +0200 |
commit | 767bb3951c65e38627cb0bbad9a3756e1cda2520 (patch) | |
tree | 460b18606d2c3472d5aa08444db4db62c6410248 /zen/debug_minidump.cpp | |
parent | 6.0 (diff) | |
download | FreeFileSync-767bb3951c65e38627cb0bbad9a3756e1cda2520.tar.gz FreeFileSync-767bb3951c65e38627cb0bbad9a3756e1cda2520.tar.bz2 FreeFileSync-767bb3951c65e38627cb0bbad9a3756e1cda2520.zip |
6.1
Diffstat (limited to 'zen/debug_minidump.cpp')
-rw-r--r-- | zen/debug_minidump.cpp | 55 |
1 files changed, 40 insertions, 15 deletions
diff --git a/zen/debug_minidump.cpp b/zen/debug_minidump.cpp index 1b625015..29500ae4 100644 --- a/zen/debug_minidump.cpp +++ b/zen/debug_minidump.cpp @@ -9,18 +9,33 @@ #include <sstream> #include <cassert> #include <cstdlib> //malloc(), free() +#include <zen/file_error.h> +#include <zen/scope_guard.h> +#include <zen/time.h> #include "win.h" //includes "windows.h" #include "DbgHelp.h" //available for MSC only #pragma comment(lib, "Dbghelp.lib") +using namespace zen; + namespace { -LONG WINAPI writeDumpOnException(EXCEPTION_POINTERS* pExceptionInfo) +LONG WINAPI writeDumpOnException(EXCEPTION_POINTERS* pExceptionInfo) //blocks showing message boxes on success and error! { - HANDLE hFile = ::CreateFile(L"exception.dmp", GENERIC_READ | GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); - if (hFile != INVALID_HANDLE_VALUE) + assert(false); + + const Zstring filename = L"CrashDump " + formatTime<Zstring>(L"%Y-%m-%d %H%M%S") + L".dmp"; + { + HANDLE hFile = ::CreateFile(filename.c_str(), GENERIC_READ | GENERIC_WRITE, 0, nullptr, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, nullptr); + if (hFile == INVALID_HANDLE_VALUE) + { + ::MessageBox(nullptr, (replaceCpy<std::wstring>(L"Cannot write file %x.", L"%x", fmtFileName(filename)) + L"\n\n" + formatSystemError(L"CreateFile", ::GetLastError())).c_str(), L"Application Crash", MB_SERVICE_NOTIFICATION | MB_ICONERROR); + std::terminate(); + } + ZEN_ON_SCOPE_EXIT(::CloseHandle(hFile)); + MINIDUMP_EXCEPTION_INFORMATION exInfo = {}; exInfo.ThreadId = ::GetCurrentThreadId(); exInfo.ExceptionPointers = pExceptionInfo; @@ -28,23 +43,33 @@ LONG WINAPI writeDumpOnException(EXCEPTION_POINTERS* pExceptionInfo) MINIDUMP_EXCEPTION_INFORMATION* exceptParam = pExceptionInfo ? &exInfo : nullptr; - /*bool rv = */ - ::MiniDumpWriteDump(::GetCurrentProcess (), //__in HANDLE hProcess, - ::GetCurrentProcessId(), //__in DWORD ProcessId, - hFile, //__in HANDLE hFile, - MiniDumpWithDataSegs, //__in MINIDUMP_TYPE DumpType, ->Standard: MiniDumpNormal, Medium: MiniDumpWithDataSegs, Full: MiniDumpWithFullMemory - exceptParam, //__in PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam, - nullptr, //__in PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam, - nullptr); //__in PMINIDUMP_CALLBACK_INFORMATION CallbackParam + if (!::MiniDumpWriteDump(::GetCurrentProcess (), //__in HANDLE hProcess, + ::GetCurrentProcessId(), //__in DWORD ProcessId, + hFile, //__in HANDLE hFile, + MiniDumpWithDataSegs, //__in MINIDUMP_TYPE DumpType, ->Standard: MiniDumpNormal, Medium: MiniDumpWithDataSegs, Full: MiniDumpWithFullMemory + exceptParam, //__in PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam, + nullptr, //__in PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam, + nullptr)) //__in PMINIDUMP_CALLBACK_INFORMATION CallbackParam + { + ::MessageBox(nullptr, (replaceCpy<std::wstring>(L"Cannot write file %x.", L"%x", fmtFileName(filename)) + L"\n\n" + formatSystemError(L"MiniDumpWriteDump", ::GetLastError())).c_str(), L"Application Crash", MB_SERVICE_NOTIFICATION | MB_ICONERROR); + std::terminate(); + } + } //close file before showing success message + + //attention: the app has not yet officially crashed! => use MB_SERVICE_NOTIFICATION to avoid Win32 GUI callbacks while message box is shown! + ::MessageBox(nullptr, replaceCpy<std::wstring>(L"Crash dump file %x written!", L"%x", fmtFileName(filename)).c_str(), L"Application Crash", MB_SERVICE_NOTIFICATION | MB_ICONERROR); + std::terminate(); - ::CloseHandle(hFile); - } - assert(false); return EXCEPTION_EXECUTE_HANDLER; } //ensure that a dump-file is written for uncaught exceptions -struct OnStartup { OnStartup() { ::SetUnhandledExceptionFilter(writeDumpOnException); }} dummy; +struct OnStartup { + OnStartup() + { + /*LPTOP_LEVEL_EXCEPTION_FILTER oldFilter = */ ::SetUnhandledExceptionFilter(writeDumpOnException); + //oldFilter == &__CxxUnhandledExceptionFilter() by default! + }} dummy; } |