From df5aa13029153bf9711e202cf4e8a6d3ff6f4d4a Mon Sep 17 00:00:00 2001 From: Geoffrey McRae Date: Tue, 28 Nov 2017 11:13:46 +1100 Subject: [PATCH] [host] Added a crash handler to write out mini dumps --- host/CrashHandler.cpp | 79 +++++++++++++++++++++++++++++++++++++++++++ host/CrashHandler.h | 29 ++++++++++++++++ host/main.cpp | 3 ++ 3 files changed, 111 insertions(+) create mode 100644 host/CrashHandler.cpp create mode 100644 host/CrashHandler.h diff --git a/host/CrashHandler.cpp b/host/CrashHandler.cpp new file mode 100644 index 00000000..4ba819c2 --- /dev/null +++ b/host/CrashHandler.cpp @@ -0,0 +1,79 @@ +/* +KVMGFX Client - A KVM Client for VGA Passthrough +Copyright (C) 2017 Geoffrey McRae + +This program is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free Software +Foundation; either version 2 of the License, or (at your option) any later +version. + +This program is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A +PARTICULAR PURPOSE. See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along with +this program; if not, write to the Free Software Foundation, Inc., 59 Temple +Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#include "CrashHandler.h" + +typedef BOOL (WINAPI * PMiniDumpWriteDump)( + _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 CrashHandler::Initialize() +{ + SetUnhandledExceptionFilter(CrashHandler::ExceptionFilter); +} + +LONG WINAPI CrashHandler::ExceptionFilter(struct _EXCEPTION_POINTERS * apExceptionInfo) +{ + HMODULE lib; + PMiniDumpWriteDump fn_MiniDumpWriteDump; + + lib = LoadLibraryA("dbghelp.dll"); + if (!lib) + return EXCEPTION_CONTINUE_SEARCH; + + fn_MiniDumpWriteDump = (PMiniDumpWriteDump)GetProcAddress(lib, "MiniDumpWriteDump"); + if (!fn_MiniDumpWriteDump) + return EXCEPTION_CONTINUE_SEARCH; + + HANDLE hFile = CreateFileA( + "kvm-ivshmem-host.dump", + GENERIC_WRITE, + FILE_SHARE_WRITE, + NULL, + CREATE_ALWAYS, + FILE_ATTRIBUTE_NORMAL, + NULL + ); + + if (hFile == INVALID_HANDLE_VALUE) + return EXCEPTION_CONTINUE_SEARCH; + + _MINIDUMP_EXCEPTION_INFORMATION info; + info.ThreadId = GetCurrentThreadId(); + info.ExceptionPointers = apExceptionInfo; + info.ClientPointers = FALSE; + + fn_MiniDumpWriteDump( + GetCurrentProcess(), + GetCurrentProcessId(), + hFile, + MiniDumpNormal, + &info, + NULL, + NULL + ); + + CloseHandle(hFile); + return EXCEPTION_CONTINUE_SEARCH; +} \ No newline at end of file diff --git a/host/CrashHandler.h b/host/CrashHandler.h new file mode 100644 index 00000000..eb984ce1 --- /dev/null +++ b/host/CrashHandler.h @@ -0,0 +1,29 @@ +/* +KVMGFX Client - A KVM Client for VGA Passthrough +Copyright (C) 2017 Geoffrey McRae + +This program is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free Software +Foundation; either version 2 of the License, or (at your option) any later +version. + +This program is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A +PARTICULAR PURPOSE. See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along with +this program; if not, write to the Free Software Foundation, Inc., 59 Temple +Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#pragma once +#include +#include + +class CrashHandler +{ +public: + static void Initialize(); +private: + static LONG WINAPI ExceptionFilter(struct _EXCEPTION_POINTERS * apExceptionInfo); +}; \ No newline at end of file diff --git a/host/main.cpp b/host/main.cpp index b5d75427..b24d47c4 100644 --- a/host/main.cpp +++ b/host/main.cpp @@ -22,6 +22,7 @@ Place, Suite 330, Boston, MA 02111-1307 USA #include "common\debug.h" #include "vendor\getopt\getopt.h" +#include "CrashHandler.h" #include "CaptureFactory.h" #include "Service.h" @@ -47,6 +48,8 @@ struct StartupArgs int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdParam, int iCmdShow) { + CrashHandler::Initialize(); + struct StartupArgs args; ZeroMemory(&args, sizeof(struct StartupArgs)); int ret = parseArgs(args);