| 1 | /* |
| 2 | * Copyright (C) 2018 HarpyWar ([email protected]) |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version 2 |
| 7 | * of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | #ifdef WIN32 |
| 19 | |
| 20 | #include <windows.h> |
| 21 | #include <Dbghelp.h> |
| 22 | |
| 23 | void make_minidump(struct _EXCEPTION_POINTERS* e) |
| 24 | { |
| 25 | auto hDbgHelp = LoadLibraryA("dbghelp"); |
| 26 | if (hDbgHelp == nullptr) |
| 27 | return; |
| 28 | auto pMiniDumpWriteDump = (decltype(&MiniDumpWriteDump))GetProcAddress(hDbgHelp, "MiniDumpWriteDump"); |
| 29 | if (pMiniDumpWriteDump == nullptr) |
| 30 | return; |
| 31 | |
| 32 | char name[MAX_PATH]; |
| 33 | { |
| 34 | auto nameEnd = name + GetModuleFileNameA(GetModuleHandleA(0), name, MAX_PATH); |
| 35 | SYSTEMTIME t; |
| 36 | GetSystemTime(&t); |
| 37 | wsprintfA(nameEnd - strlen(".exe"), |
| 38 | "_%4d%02d%02d_%02d%02d%02d.dmp", |
| 39 | t.wYear, t.wMonth, t.wDay, t.wHour, t.wMinute, t.wSecond); |
| 40 | } |
| 41 | |
| 42 | auto hFile = CreateFileA(name, GENERIC_WRITE, FILE_SHARE_READ, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); |
| 43 | if (hFile == INVALID_HANDLE_VALUE) |
| 44 | return; |
| 45 | |
| 46 | MINIDUMP_EXCEPTION_INFORMATION exceptionInfo; |
| 47 | exceptionInfo.ThreadId = GetCurrentThreadId(); |
| 48 | exceptionInfo.ExceptionPointers = e; |
| 49 | exceptionInfo.ClientPointers = FALSE; |
| 50 | |
| 51 | auto dumped = pMiniDumpWriteDump( |
| 52 | GetCurrentProcess(), |
| 53 | GetCurrentProcessId(), |
| 54 | hFile, |
| 55 | MINIDUMP_TYPE(MiniDumpWithIndirectlyReferencedMemory | MiniDumpScanMemory), |
| 56 | e ? &exceptionInfo : nullptr, |
| 57 | nullptr, |
| 58 | nullptr); |
| 59 | |
| 60 | CloseHandle(hFile); |
| 61 | |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | LONG WINAPI unhandled_handler(struct _EXCEPTION_POINTERS* e) |
| 66 | { |
| 67 | make_minidump(e); |
| 68 | return EXCEPTION_CONTINUE_SEARCH; |
| 69 | } |
| 70 | |
| 71 | #endif |