forked from electronicarts/CnC_Generals_Zero_Hour
-
Notifications
You must be signed in to change notification settings - Fork 125
feat(crashdump): Add crash dump functionality for fatal errors #1594
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a241b80
feat(crashdump): Add crash dump functionality
slurmlord 5e543ba
Refactor CallbackInternal and DumpMemoryObjects
slurmlord 7399cce
Improve iteration in DumpMemoryObjects
slurmlord 7cc9b3c
Make state enums private, init all meory objs in DumpObjectsState_Begin
slurmlord bcb86fe
Include enum name in enum values, move AllocationRangeIterator implem…
slurmlord c0f1045
Simplify allocator and raw block navigation
slurmlord 810371e
Remove integration with GameMemory, offer Small and Full dump types
slurmlord 7ce2363
Already enough trailing whitespace, no need to add more
slurmlord File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -223,6 +223,9 @@ class MemoryPool; | |
| class MemoryPoolFactory; | ||
| class DynamicMemoryAllocator; | ||
| class BlockCheckpointInfo; | ||
| #ifdef RTS_ENABLE_CRASHDUMP | ||
| class AllocationRangeIterator; | ||
| #endif | ||
|
|
||
| // TYPE DEFINES /////////////////////////////////////////////////////////////// | ||
|
|
||
|
|
@@ -279,6 +282,14 @@ class Checkpointable | |
| }; | ||
| #endif | ||
|
|
||
| #ifdef RTS_ENABLE_CRASHDUMP | ||
| struct MemoryPoolAllocatedRange | ||
| { | ||
| const char* allocationAddr; | ||
| size_t allocationSize; | ||
| }; | ||
| #endif | ||
|
|
||
| // ---------------------------------------------------------------------------- | ||
| /** | ||
| A MemoryPool provides a way to efficiently allocate objects of the same (or similar) | ||
|
|
@@ -384,6 +395,9 @@ class MemoryPool | |
| /// return true iff this block was allocated by this pool. | ||
| Bool debugIsBlockInPool(void *pBlock); | ||
| #endif | ||
| #ifdef RTS_ENABLE_CRASHDUMP | ||
| friend class AllocationRangeIterator; | ||
| #endif | ||
| }; | ||
|
|
||
| // ---------------------------------------------------------------------------- | ||
|
|
@@ -474,13 +488,52 @@ class DynamicMemoryAllocator | |
| Bool debugIsPoolInDma(MemoryPool *pool); | ||
|
|
||
| #endif // MEMORYPOOL_DEBUG | ||
| #ifdef RTS_ENABLE_CRASHDUMP | ||
| MemoryPoolSingleBlock* getFirstRawBlock() const; | ||
| MemoryPoolSingleBlock* getNextRawBlock(const MemoryPoolSingleBlock* block) const; | ||
| void fillAllocationRangeForRawBlock(const MemoryPoolSingleBlock*, MemoryPoolAllocatedRange& allocationRange) const; | ||
| #endif | ||
| }; | ||
|
|
||
| // ---------------------------------------------------------------------------- | ||
| #ifdef MEMORYPOOL_DEBUG | ||
| enum { MAX_SPECIAL_USED = 256 }; | ||
| #endif | ||
|
|
||
| #ifdef RTS_ENABLE_CRASHDUMP | ||
| class AllocationRangeIterator | ||
| { | ||
| typedef const MemoryPoolAllocatedRange value_type; | ||
| typedef const MemoryPoolAllocatedRange* pointer; | ||
| typedef const MemoryPoolAllocatedRange& reference; | ||
|
|
||
| public: | ||
|
|
||
| AllocationRangeIterator(); | ||
| AllocationRangeIterator(const MemoryPoolFactory* factory); | ||
xezon marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| AllocationRangeIterator(MemoryPool& pool, MemoryPoolBlob& blob); | ||
|
||
| AllocationRangeIterator(MemoryPool* pool, MemoryPoolBlob* blob); | ||
|
|
||
| reference operator*() { return m_range; } | ||
| pointer operator->() { return &m_range; } | ||
|
|
||
| AllocationRangeIterator& operator++(); | ||
| AllocationRangeIterator operator++(int); | ||
|
|
||
| friend const bool operator== (const AllocationRangeIterator& a, const AllocationRangeIterator& b); | ||
| friend const bool operator!= (const AllocationRangeIterator& a, const AllocationRangeIterator& b); | ||
|
|
||
| private: | ||
|
|
||
| void updateRange(); | ||
| void moveToNextBlob(); | ||
| const MemoryPoolFactory* m_factory; | ||
|
||
| MemoryPool* m_currentPool; | ||
| MemoryPoolBlob* m_currentBlobInPool; | ||
| MemoryPoolAllocatedRange m_range; | ||
| }; | ||
| #endif | ||
|
|
||
| // ---------------------------------------------------------------------------- | ||
| /** | ||
| The class that manages all the MemoryPools and DynamicMemoryAllocators. | ||
|
|
@@ -573,6 +626,20 @@ class MemoryPoolFactory | |
| void debugResetCheckpoints(); | ||
|
|
||
| #endif | ||
| #ifdef RTS_ENABLE_CRASHDUMP | ||
| AllocationRangeIterator cbegin() const | ||
| { | ||
| return AllocationRangeIterator(this); | ||
| } | ||
|
|
||
| AllocationRangeIterator cend() const | ||
| { | ||
| return AllocationRangeIterator(NULL, NULL); | ||
| } | ||
|
|
||
| MemoryPool* getFirstMemoryPool() const; | ||
| friend class AllocationRangeIterator; | ||
| #endif | ||
| }; | ||
|
|
||
| // how many bytes are we allowed to 'waste' per pool allocation before the debug code starts yelling at us... | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| /* | ||
| ** Command & Conquer Generals Zero Hour(tm) | ||
| ** Copyright 2025 TheSuperHackers | ||
| ** | ||
| ** 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 3 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, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #ifdef RTS_ENABLE_CRASHDUMP | ||
| #include "DbgHelpLoader.h" | ||
|
|
||
| enum DumpType CPP_11(: Char) | ||
| { | ||
| // Smallest dump type with call stacks and some supporting variables | ||
| DumpType_Minimal = 'M', | ||
| // Large dump including all memory regions allocated by the GameMemory implementation | ||
| DumpType_Gamememory = 'X', | ||
| // Largest dump size including complete memory contents of the process | ||
| DumpType_Full = 'F', | ||
| }; | ||
|
|
||
| class MiniDumper | ||
| { | ||
| enum MiniDumperExitCode CPP_11(: Int) | ||
| { | ||
| MiniDumperExitCode_Success = 0x0, | ||
| MiniDumperExitCode_FailureWait = 0x37DA1040, | ||
| MiniDumperExitCode_FailureParam = 0x4EA527BB, | ||
| MiniDumperExitCode_ForcedTerminate = 0x158B1154, | ||
| }; | ||
|
|
||
| enum DumpObjectsState CPP_11(: Int) | ||
| { | ||
| DumpObjectsState_Begin, | ||
| DumpObjectsState_MemoryPools, | ||
| DumpObjectsState_MemoryPoolAllocations, | ||
| DumpObjectsState_DMAAllocations, | ||
| DumpObjectsState_Completed | ||
| }; | ||
|
|
||
| public: | ||
| MiniDumper(); | ||
| Bool IsInitialized() const; | ||
| void TriggerMiniDump(DumpType dumpType); | ||
| void TriggerMiniDumpForException(_EXCEPTION_POINTERS* e_info, DumpType dumpType); | ||
| static void initMiniDumper(const AsciiString& userDirPath); | ||
| static void shutdownMiniDumper(); | ||
| static LONG WINAPI DumpingExceptionFilter(_EXCEPTION_POINTERS* e_info); | ||
|
|
||
| private: | ||
slurmlord marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| void Initialize(const AsciiString& userDirPath); | ||
| void ShutDown(); | ||
| void CreateMiniDump(DumpType dumpType); | ||
| void CleanupResources(); | ||
| Bool IsDumpThreadStillRunning() const; | ||
| void ShutdownDumpThread(); | ||
| Bool ShouldWriteDataSegsForModule(const PWCHAR module) const; | ||
| #ifndef DISABLE_GAMEMEMORY | ||
| void MoveToNextAllocatorWithRawBlocks(); | ||
| void MoveToNextSingleBlock(); | ||
| void DumpMemoryObjects(ULONG64& memoryBase, ULONG& memorySize); | ||
| #endif | ||
|
|
||
| // Callbacks from dbghelp | ||
| static BOOL CALLBACK MiniDumpCallback(PVOID CallbackParam, PMINIDUMP_CALLBACK_INPUT CallbackInput, PMINIDUMP_CALLBACK_OUTPUT CallbackOutput); | ||
| BOOL CallbackInternal(const MINIDUMP_CALLBACK_INPUT& input, MINIDUMP_CALLBACK_OUTPUT& output); | ||
|
|
||
| // Thread procs | ||
| static DWORD WINAPI MiniDumpThreadProc(LPVOID lpParam); | ||
| DWORD ThreadProcInternal(); | ||
|
|
||
| // Dump file directory bookkeeping | ||
| Bool InitializeDumpDirectory(const AsciiString& userDirPath); | ||
| static void KeepNewestFiles(const std::string& directory, const DumpType dumpType, const Int keepCount); | ||
|
|
||
| // Struct to hold file information | ||
| struct FileInfo | ||
| { | ||
| std::string name; | ||
| FILETIME lastWriteTime; | ||
| }; | ||
|
|
||
| static bool CompareByLastWriteTime(const FileInfo& a, const FileInfo& b); | ||
|
|
||
| private: | ||
| Bool m_miniDumpInitialized; | ||
| Bool m_loadedDbgHelp; | ||
| DumpType m_requestedDumpType; | ||
|
|
||
| // Path buffers | ||
| Char m_dumpDir[MAX_PATH]; | ||
| Char m_dumpFile[MAX_PATH]; | ||
| WideChar m_executablePath[MAX_PATH]; | ||
|
|
||
| // Event handles | ||
| HANDLE m_dumpRequested; | ||
| HANDLE m_dumpComplete; | ||
| HANDLE m_quitting; | ||
|
|
||
| // Thread handles | ||
| HANDLE m_dumpThread; | ||
| DWORD m_dumpThreadId; | ||
|
|
||
| #ifndef DISABLE_GAMEMEMORY | ||
| // Internal memory dumping progress state | ||
| DumpObjectsState m_dumpObjectsState; | ||
| DynamicMemoryAllocator* m_currentAllocator; | ||
| MemoryPool* m_currentPool; | ||
| MemoryPoolSingleBlock* m_currentSingleBlock; | ||
|
|
||
| AllocationRangeIterator m_rangeIter; | ||
| #endif | ||
| }; | ||
|
|
||
| extern MiniDumper* TheMiniDumper; | ||
| #endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps also put "MemoryPool" into its name.
Alternatively, you could also make this class a member of "MemoryPool" and then simply name it "Iterator".
It then would become
MemoryPool::IteratorUnless there is a reason for it to be standalone or it is too much work to touch.