|
| 1 | +/* |
| 2 | +** Command & Conquer Generals Zero Hour(tm) |
| 3 | +** Copyright 2025 TheSuperHackers |
| 4 | +** |
| 5 | +** This program is free software: you can redistribute it and/or modify |
| 6 | +** it under the terms of the GNU General Public License as published by |
| 7 | +** the Free Software Foundation, either version 3 of the License, or |
| 8 | +** (at your option) any later version. |
| 9 | +** |
| 10 | +** This program is distributed in the hope that it will be useful, |
| 11 | +** but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +** GNU General Public License for more details. |
| 14 | +** |
| 15 | +** You should have received a copy of the GNU General Public License |
| 16 | +** along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | +*/ |
| 18 | + |
| 19 | +#pragma once |
| 20 | + |
| 21 | +#ifdef RTS_ENABLE_CRASHDUMP |
| 22 | +#include "DbgHelpLoader.h" |
| 23 | + |
| 24 | +enum DumpType CPP_11(: Char) |
| 25 | +{ |
| 26 | + // Smallest dump type with call stacks and some supporting variables |
| 27 | + DumpType_Minimal = 'M', |
| 28 | + // Largest dump size including complete memory contents of the process |
| 29 | + DumpType_Full = 'F', |
| 30 | +}; |
| 31 | + |
| 32 | +class MiniDumper |
| 33 | +{ |
| 34 | + enum MiniDumperExitCode CPP_11(: Int) |
| 35 | + { |
| 36 | + MiniDumperExitCode_Success = 0x0, |
| 37 | + MiniDumperExitCode_FailureWait = 0x37DA1040, |
| 38 | + MiniDumperExitCode_FailureParam = 0x4EA527BB, |
| 39 | + MiniDumperExitCode_ForcedTerminate = 0x158B1154, |
| 40 | + }; |
| 41 | + |
| 42 | +public: |
| 43 | + MiniDumper(); |
| 44 | + Bool IsInitialized() const; |
| 45 | + void TriggerMiniDump(DumpType dumpType); |
| 46 | + void TriggerMiniDumpForException(_EXCEPTION_POINTERS* e_info, DumpType dumpType); |
| 47 | + static void initMiniDumper(const AsciiString& userDirPath); |
| 48 | + static void shutdownMiniDumper(); |
| 49 | + static LONG WINAPI DumpingExceptionFilter(_EXCEPTION_POINTERS* e_info); |
| 50 | + |
| 51 | +private: |
| 52 | + void Initialize(const AsciiString& userDirPath); |
| 53 | + void ShutDown(); |
| 54 | + void CreateMiniDump(DumpType dumpType); |
| 55 | + void CleanupResources(); |
| 56 | + Bool IsDumpThreadStillRunning() const; |
| 57 | + void ShutdownDumpThread(); |
| 58 | + |
| 59 | + // Thread procs |
| 60 | + static DWORD WINAPI MiniDumpThreadProc(LPVOID lpParam); |
| 61 | + DWORD ThreadProcInternal(); |
| 62 | + |
| 63 | + // Dump file directory bookkeeping |
| 64 | + Bool InitializeDumpDirectory(const AsciiString& userDirPath); |
| 65 | + static void KeepNewestFiles(const std::string& directory, const DumpType dumpType, const Int keepCount); |
| 66 | + |
| 67 | + // Struct to hold file information |
| 68 | + struct FileInfo |
| 69 | + { |
| 70 | + std::string name; |
| 71 | + FILETIME lastWriteTime; |
| 72 | + }; |
| 73 | + |
| 74 | + static bool CompareByLastWriteTime(const FileInfo& a, const FileInfo& b); |
| 75 | + |
| 76 | +private: |
| 77 | + Bool m_miniDumpInitialized; |
| 78 | + Bool m_loadedDbgHelp; |
| 79 | + DumpType m_requestedDumpType; |
| 80 | + |
| 81 | + // Path buffers |
| 82 | + Char m_dumpDir[MAX_PATH]; |
| 83 | + Char m_dumpFile[MAX_PATH]; |
| 84 | + WideChar m_executablePath[MAX_PATH]; |
| 85 | + |
| 86 | + // Event handles |
| 87 | + HANDLE m_dumpRequested; |
| 88 | + HANDLE m_dumpComplete; |
| 89 | + HANDLE m_quitting; |
| 90 | + |
| 91 | + // Thread handles |
| 92 | + HANDLE m_dumpThread; |
| 93 | + DWORD m_dumpThreadId; |
| 94 | +}; |
| 95 | + |
| 96 | +extern MiniDumper* TheMiniDumper; |
| 97 | +#endif |
0 commit comments