Zero Hour rebuilt from EA's source: x64, Direct3D 11, VS2022/CMake, ~60 original bugs fixed, an AI that builds a base. Bring your own game.
C++ cmakecnccommand-and-conquercppdirectx11game-enginegame-preservationgeneralsrtssage-enginex64zero-hour
fix(gameengine): make the release crash log usable for phase 5
Olcay Seygan8f5421c70126
diff --git a/GeneralsMD/Code/CMakeLists.txt b/GeneralsMD/Code/CMakeLists.txtindex dfa707856..caa2cb22b 100644--- a/GeneralsMD/Code/CMakeLists.txt+++ b/GeneralsMD/Code/CMakeLists.txt@@ -14,6 +14,12 @@ if(MSVC)# /permissive- is off on purpose: this is 2003-era code and templates here rely on# pre-two-phase-lookup name binding (simplevec.h et al).add_compile_options(/W3 /permissive /EHsc)+ # There is no debugger on the porting machine, so ReleaseCrashInfo.txt's stack+ # dump is the only stack we get. StackDump.cpp walks the EBP chain by hand and+ # /O2 implies /Oy (no frame pointer), which leaves that dump empty; /Zi + /DEBUG+ # then give the frames names instead of bare addresses. Costs a PDB, buys Phase 5.+ add_compile_options(/Oy- /Zi)+ add_link_options(/DEBUG)endif()# ---- wwdebug --------------------------------------------------------------@@ -397,7 +403,11 @@ target_link_libraries(generals PRIVATE gameenginedevice# beside Run/'s .big files to find anything (Phase 5).add_custom_command(TARGET generals POST_BUILDCOMMAND ${CMAKE_COMMAND} -E copy_if_different- $<TARGET_FILE:generals> ${CMAKE_CURRENT_SOURCE_DIR}/../Run/)+ $<TARGET_FILE:generals> ${CMAKE_CURRENT_SOURCE_DIR}/../Run/+ # The PDB has to sit next to the exe: SymInitialize searches the exe's directory,+ # and without it the crash log's stack is addresses with no names.+ COMMAND ${CMAKE_COMMAND} -E copy_if_different+ $<TARGET_PDB_FILE:generals> ${CMAKE_CURRENT_SOURCE_DIR}/../Run/)# ===========================================================================# Tests. Run with: ctest --test-dir build -C Release --output-on-failurediff --git a/GeneralsMD/Code/GameEngine/Source/Common/INI/INI.cpp b/GeneralsMD/Code/GameEngine/Source/Common/INI/INI.cppindex 1b871d6e9..bfcba1609 100644--- a/GeneralsMD/Code/GameEngine/Source/Common/INI/INI.cpp+++ b/GeneralsMD/Code/GameEngine/Source/Common/INI/INI.cpp@@ -39,6 +39,7 @@#include "Common/FileSystem.h"#include "Common/GameAudio.h"#include "Common/Science.h"+#include "Common/StackDump.h" // g_LastErrorDump, see prepFile#include "Common/SpecialPower.h"#include "Common/ThingFactory.h"#include "Common/ThingTemplate.h"@@ -278,6 +279,12 @@ void INI::prepFile( AsciiString filename, INILoadType loadType )if( m_file == NULL ){+ // These INI_* constants live in an *unnamed* enum, so GameEngine::init's+ // catch (ErrorCode) never matches them and they all end up in its catch (...),+ // which reports "Uncaught Exception during initialization." and nothing else.+ // Leave a trace in g_LastErrorDump so ReleaseCrashInfo.txt names the file --+ // in a Release build DEBUG_CRASH above compiles to nothing.+ g_LastErrorDump.format("INI::prepFile could not open '%s'\n", filename.str());DEBUG_CRASH(( "INI::load, cannot open file '%s'\n", filename.str() ));throw INI_CANT_OPEN_FILE;diff --git a/GeneralsMD/Code/GameEngine/Source/Common/System/Debug.cpp b/GeneralsMD/Code/GameEngine/Source/Common/System/Debug.cppindex 0a90b9cef..106a60a19 100644--- a/GeneralsMD/Code/GameEngine/Source/Common/System/Debug.cpp+++ b/GeneralsMD/Code/GameEngine/Source/Common/System/Debug.cpp@@ -685,7 +685,9 @@ void ReleaseCrash(const char *reason)fprintf(theReleaseCrashLogFile, "Release Crash at %s; Reason %s\n", getCurrentTimeString(), reason);fprintf(theReleaseCrashLogFile, "\nLast error:\n%s\n\nCurrent stack:\n", g_LastErrorDump.str());const int STACKTRACE_SIZE = 12;- const int STACKTRACE_SKIP = 6;+ // 1, not 6: FillStackAddresses drops its own frame itself now, so 6 skipped+ // past everything the game did and logged two ntdll frames instead.+ const int STACKTRACE_SKIP = 1;void* stacktrace[STACKTRACE_SIZE];::FillStackAddresses(stacktrace, STACKTRACE_SIZE, STACKTRACE_SKIP);::StackDumpFromAddresses(stacktrace, STACKTRACE_SIZE, releaseCrashLogOutput);@@ -774,7 +776,9 @@ void ReleaseCrashLocalized(const AsciiString& p, const AsciiString& m)fprintf(theReleaseCrashLogFile, "Release Crash at %s; Reason %s\n", getCurrentTimeString(), mesg.str());const int STACKTRACE_SIZE = 12;- const int STACKTRACE_SKIP = 6;+ // 1, not 6: FillStackAddresses drops its own frame itself now, so 6 skipped+ // past everything the game did and logged two ntdll frames instead.+ const int STACKTRACE_SKIP = 1;void* stacktrace[STACKTRACE_SIZE];::FillStackAddresses(stacktrace, STACKTRACE_SIZE, STACKTRACE_SKIP);::StackDumpFromAddresses(stacktrace, STACKTRACE_SIZE, releaseCrashLogOutput);diff --git a/GeneralsMD/Code/GameEngine/Source/Common/System/StackDump.cpp b/GeneralsMD/Code/GameEngine/Source/Common/System/StackDump.cppindex b9e696e1b..f7cc6f74e 100644--- a/GeneralsMD/Code/GameEngine/Source/Common/System/StackDump.cpp+++ b/GeneralsMD/Code/GameEngine/Source/Common/System/StackDump.cpp@@ -127,39 +127,16 @@ BOOL InitSymbolInfo()gsSymGetLineFromAddr = (BOOL (__stdcall *)( IN HANDLE,IN DWORD,OUT PDWORD,OUT PIMAGEHLP_LINE))GetProcAddress(hInstDebugHlp , "SymGetLineFromAddr");- char pathname[_MAX_PATH+1];- char drive[10];- char directory[_MAX_PATH+1];- HANDLE process;--::SymSetOptions(SYMOPT_DEFERRED_LOADS | SYMOPT_UNDNAME | SYMOPT_LOAD_LINES | SYMOPT_OMAP_FIND_NEAREST);- process = GetCurrentProcess();-- //Get the apps name- ::GetModuleFileName(NULL, pathname, _MAX_PATH);-- // turn it into a search path- _splitpath(pathname, drive, directory, NULL, NULL);- sprintf(pathname, "%s:\\%s", drive, directory);-- // append the current directory to build a search path for SymInit- ::lstrcat(pathname, ";.;");-- if(::SymInitialize(process, pathname, FALSE))- {- // regenerate the name of the app- ::GetModuleFileName(NULL, pathname, _MAX_PATH);- if(::SymLoadModule(process, NULL, pathname, NULL, 0, 0))- {- //Load any other relevant modules (ie dlls) here- return TRUE;- }- ::SymCleanup(process);- }+ HANDLE process = GetCurrentProcess();- return(FALSE);+ // The old code built its own search path out of _splitpath, but _splitpath+ // already hands back "D:" in drive and the format string added a second colon,+ // so the path ("D::\dir\") pointed nowhere and every frame came out+ // "<Unknown>". fInvadeProcess enumerates the process' own modules instead,+ // which finds the PDB sitting next to the exe without a path at all.+ return ::SymInitialize(process, NULL, TRUE);}@@ -332,100 +309,13 @@ void FillStackAddresses(void**addresses, unsigned int count, unsigned int skip){InitSymbolInfo();- STACKFRAME stack_frame;--- HANDLE thread = GetCurrentThread();- HANDLE process = GetCurrentProcess();-- memset(&gsContext, 0, sizeof(CONTEXT));- gsContext.ContextFlags = CONTEXT_FULL;-- DWORD myeip,myesp,myebp;-_asm-{-MYEIP2:- mov eax, MYEIP2- mov dword ptr [myeip] , eax- mov eax, esp- mov dword ptr [myesp] , eax- mov eax, ebp- mov dword ptr [myebp] , eax- xor eax,eax-}-memset(&stack_frame, 0, sizeof(STACKFRAME));-stack_frame.AddrPC.Mode = AddrModeFlat;-stack_frame.AddrPC.Offset = myeip;-stack_frame.AddrStack.Mode = AddrModeFlat;-stack_frame.AddrStack.Offset = myesp;-stack_frame.AddrFrame.Mode = AddrModeFlat;-stack_frame.AddrFrame.Offset = myebp;--{-/*- if(GetThreadContext(thread, &gsContext))- {- memset(&stack_frame, 0, sizeof(STACKFRAME));- stack_frame.AddrPC.Mode = AddrModeFlat;- stack_frame.AddrPC.Offset = gsContext.Eip;- stack_frame.AddrStack.Mode = AddrModeFlat;- stack_frame.AddrStack.Offset = gsContext.Esp;- stack_frame.AddrFrame.Mode = AddrModeFlat;- stack_frame.AddrFrame.Offset = gsContext.Ebp;-*/-- Bool stillgoing = TRUE;-// unsigned int cd = count;-- // Skip some?- while (stillgoing&&skip)- {- stillgoing = StackWalk(IMAGE_FILE_MACHINE_I386,- process,- thread,- &stack_frame,- NULL, //&gsContext,- NULL,- SymFunctionTableAccess,- SymGetModuleBase,- NULL) != 0;- skip--;- }-- while(stillgoing&&count)- {- stillgoing = StackWalk(IMAGE_FILE_MACHINE_I386,- process,- thread,- &stack_frame,- NULL, //&gsContext,- NULL,- SymFunctionTableAccess,- SymGetModuleBase,- NULL) != 0;- if (stillgoing)- {- *addresses = (void*)stack_frame.AddrPC.Offset;- addresses++;- count--;- }- }-- // Fill remainder- while (count)- {- *addresses = NULL;- addresses++;- count--;- }-- }-/*- else- {- memset(addresses,NULL,count*sizeof(void*));- }-*/+ // This used to seed a STACKFRAME from inline asm and drive StackWalk() by+ // hand. On this toolchain that walk returns the seeded frame once and then+ // stops, so every crash report came back with an empty stack. CaptureStack-+ // BackTrace does the same job in one call and needs no symbols to do it; the+ // +1 drops this function's own frame, which the old code also skipped.+ memset(addresses, 0, count * sizeof(void *));+ ::CaptureStackBackTrace(skip + 1, count, addresses, NULL);}diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cppindex 93ccd23ab..4a0e24fd3 100644--- a/GeneralsMD/Code/Tests/test_gameengine.cpp+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp@@ -22,6 +22,7 @@#include "Common/INI.h"#include "Common/INIException.h"#include "Common/STLTypedefs.h"+#include "Common/StackDump.h"#include "GameClient/Water.h"#include <stdio.h>@@ -326,3 +327,28 @@ TEST(ini_unknown_block_aborts_the_file)remove( TEST_INI );}++/* ReleaseCrashInfo.txt's stack trace is the only stack this port gets - there is+ no debugger on the porting machine - and it came back empty on the first real+ crash, which is worth catching here rather than in the middle of a launch. */+static char s_stackText[ 4096 ];++static void collectStackLine( const char *line )+{+ strncat( s_stackText, line, sizeof( s_stackText ) - strlen( s_stackText ) - 1 );+}++TEST(stackdump_walks_the_callers)+{+ void *frames[ 12 ];+ memset( frames, 0, sizeof( frames ) );+ ::FillStackAddresses( frames, 12, 0 );+ CHECK( frames[ 0 ] != NULL );++ s_stackText[ 0 ] = 0;+ ::StackDumpFromAddresses( frames, 12, collectStackLine );+ /* Needs the PDB next to the exe; without symbols this is where it shows. */+ if( strstr( s_stackText, "stackdump_walks_the_callers" ) == NULL )+ printf( "%s\n", s_stackText );+ CHECK( strstr( s_stackText, "stackdump_walks_the_callers" ) != NULL );+}