Skip to content

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.

fix(debug): walk the stack with the 64-bit dbghelp entry points

Olcay Seygan46800f580d43
diff --git a/GeneralsMD/Code/Libraries/Source/debug/debug_stack.cpp b/GeneralsMD/Code/Libraries/Source/debug/debug_stack.cpp
index eaa36a4e8..9390883e2 100644
--- a/GeneralsMD/Code/Libraries/Source/debug/debug_stack.cpp
+++ b/GeneralsMD/Code/Libraries/Source/debug/debug_stack.cpp
@@ -104,8 +104,15 @@ static void InitDbghelp(void)
// Set options
gDbg._SymSetOptions(gDbg._SymGetOptions()|SYMOPT_DEFERRED_LOADS|SYMOPT_LOAD_LINES);
- // Init module
- gDbg._SymInitialize((HANDLE)GetCurrentProcessId(),NULL,TRUE);
+ // Init module. This used to pass (HANDLE)GetCurrentProcessId() here and in
+ // every Sym* call below, while StackWalk got GetCurrentProcess() -- dbghelp
+ // keys its per-process state by whatever it is handed, so the walk ran with
+ // callbacks that had no symbol context, returned the frame it was seeded with
+ // and stopped: every crash report in this port read "1 addresses:". The real
+ // handle is what dbghelp documents and what GameEngine's StackDump.cpp already
+ // uses, so the two agree now. Re-initializing an already-initialized handle
+ // just fails harmlessly.
+ gDbg._SymInitialize(GetCurrentProcess(),NULL,TRUE);
// Check: are we using a newer version of dbghelp.dll?
// (older versions have some serious issues.. err... bugs)
@@ -149,7 +156,7 @@ void DebugStackwalk::Signature::GetSymbol(unsigned addr, char *buf, unsigned buf
buf+=wsprintf(buf,"%08x",addr);
// determine module
- unsigned modBase=gDbg._SymGetModuleBase((HANDLE)GetCurrentProcessId(),addr);
+ unsigned modBase=gDbg._SymGetModuleBase(GetCurrentProcess(),addr);
if (!modBase)
{
strcpy(buf," (unknown module)");
@@ -181,7 +188,7 @@ void DebugStackwalk::Signature::GetSymbol(unsigned addr, char *buf, unsigned buf
symPtr->SizeOfStruct=sizeof(IMAGEHLP_SYMBOL);
symPtr->MaxNameLength=sizeof(symbolBuffer)-sizeof(IMAGEHLP_SYMBOL);
DWORD displacement;
- if (!gDbg._SymGetSymFromAddr((HANDLE)GetCurrentProcessId(),addr,&displacement,symPtr))
+ if (!gDbg._SymGetSymFromAddr(GetCurrentProcess(),addr,&displacement,symPtr))
return;
if ((unsigned int)(bufEnd-buf)<strlen(symPtr->Name)+16)
return;
@@ -191,7 +198,7 @@ void DebugStackwalk::Signature::GetSymbol(unsigned addr, char *buf, unsigned buf
IMAGEHLP_LINE line;
memset(&line,0,sizeof(line));
line.SizeOfStruct=sizeof(line);
- if (!gDbg._SymGetLineFromAddr((HANDLE)GetCurrentProcessId(),addr,&displacement,&line))
+ if (!gDbg._SymGetLineFromAddr(GetCurrentProcess(),addr,&displacement,&line))
return;
p=strrchr(line.FileName,'\\'); // use filename only, strip off path
@@ -223,7 +230,7 @@ void DebugStackwalk::Signature::GetSymbol(unsigned addr,
DFAIL_IF(bufFile&&sizeFile<16) return;
// determine module
- unsigned modBase=gDbg._SymGetModuleBase((HANDLE)GetCurrentProcessId(),addr);
+ unsigned modBase=gDbg._SymGetModuleBase(GetCurrentProcess(),addr);
if (!modBase)
{
if (bufMod)
@@ -264,7 +271,7 @@ void DebugStackwalk::Signature::GetSymbol(unsigned addr,
symPtr->SizeOfStruct=sizeof(IMAGEHLP_SYMBOL);
symPtr->MaxNameLength=sizeof(symbolBuffer)-sizeof(IMAGEHLP_SYMBOL);
DWORD displacement;
- if (gDbg._SymGetSymFromAddr((HANDLE)GetCurrentProcessId(),addr,&displacement,symPtr))
+ if (gDbg._SymGetSymFromAddr(GetCurrentProcess(),addr,&displacement,symPtr))
{
strncpy(bufSym,symPtr->Name,sizeSym);
bufSym[sizeSym-1]=0;
@@ -282,7 +289,7 @@ void DebugStackwalk::Signature::GetSymbol(unsigned addr,
memset(&line,0,sizeof(line));
line.SizeOfStruct=sizeof(line);
DWORD displacement;
- if (!gDbg._SymGetLineFromAddr((HANDLE)GetCurrentProcessId(),addr,&displacement,&line))
+ if (!gDbg._SymGetLineFromAddr(GetCurrentProcess(),addr,&displacement,&line))
strcpy(bufFile,"(unknown)");
else
{
@@ -341,11 +348,14 @@ int DebugStackwalk::StackWalk(Signature &sig, struct _CONTEXT *ctx)
sig.m_numAddr=0;
// bail out if no stack walk available
- if (!gDbg._StackWalk)
+ if (!gDbg._StackWalk64)
return 0;
// Set up the stack frame structure for the start point of the stack walk (i.e. here).
- STACKFRAME stackFrame;
+ // STACKFRAME/StackWalk (the 32-bit-only originals) are what this used to use, but
+ // on a current dbghelp.dll that call fails on its first step with
+ // ERROR_PARTIAL_COPY and the walk produces nothing at all.
+ STACKFRAME64 stackFrame;
memset(&stackFrame,0,sizeof(stackFrame));
stackFrame.AddrPC.Mode = AddrModeFlat;
@@ -379,13 +389,13 @@ int DebugStackwalk::StackWalk(Signature &sig, struct _CONTEXT *ctx)
// Walk the stack by the requested number of return address iterations.
bool skipFirst=!ctx;
while (sig.m_numAddr<Signature::MAX_ADDR&&
- gDbg._StackWalk(IMAGE_FILE_MACHINE_I386,GetCurrentProcess(),GetCurrentThread(),
- &stackFrame,NULL,NULL,gDbg._SymFunctionTableAccess,gDbg._SymGetModuleBase,NULL))
+ gDbg._StackWalk64(IMAGE_FILE_MACHINE_I386,GetCurrentProcess(),GetCurrentThread(),
+ &stackFrame,NULL,NULL,gDbg._SymFunctionTableAccess64,gDbg._SymGetModuleBase64,NULL))
{
if (skipFirst)
skipFirst=false;
else
- sig.m_addr[sig.m_numAddr++]=stackFrame.AddrPC.Offset;
+ sig.m_addr[sig.m_numAddr++]=(unsigned)stackFrame.AddrPC.Offset;
}
return sig.m_numAddr;
diff --git a/GeneralsMD/Code/Libraries/Source/debug/debug_stack.inl b/GeneralsMD/Code/Libraries/Source/debug/debug_stack.inl
index df4121024..4fef371db 100644
--- a/GeneralsMD/Code/Libraries/Source/debug/debug_stack.inl
+++ b/GeneralsMD/Code/Libraries/Source/debug/debug_stack.inl
@@ -57,6 +57,26 @@ DBGHELP(SymGetLineFromAddr,
(HANDLE hProcess, DWORD dwAddr, PDWORD pdwDisplacement,
PIMAGEHLP_LINE Line))
+// StackWalk/SymFunctionTableAccess/SymGetModuleBase are the 32-bit-only originals.
+// On a current dbghelp.dll the legacy StackWalk fails outright (ERROR_PARTIAL_COPY
+// on its very first step), so the walker uses the 64-bit trio instead; the Sym*
+// lookups above still work, they only take an address.
+DBGHELP(StackWalk64,
+ BOOL,
+ (DWORD MachineType, HANDLE hProcess, HANDLE hThread, LPSTACKFRAME64 StackFrame,
+ PVOID ContextRecord, PREAD_PROCESS_MEMORY_ROUTINE64 ReadMemoryRoutine,
+ PFUNCTION_TABLE_ACCESS_ROUTINE64 FunctionTableAccessRoutine,
+ PGET_MODULE_BASE_ROUTINE64 GetModuleBaseRoutine,
+ PTRANSLATE_ADDRESS_ROUTINE64 TranslateAddress))
+
+DBGHELP(SymFunctionTableAccess64,
+ PVOID,
+ (HANDLE hProcess, DWORD64 AddrBase))
+
+DBGHELP(SymGetModuleBase64,
+ DWORD64,
+ (HANDLE hProcess, DWORD64 dwAddr))
+
// keep this always as last entry
DBGHELP(SymCleanup,
BOOL,
diff --git a/GeneralsMD/Code/Tests/test_debug.cpp b/GeneralsMD/Code/Tests/test_debug.cpp
index 5bb599cd3..f10f18072 100644
--- a/GeneralsMD/Code/Tests/test_debug.cpp
+++ b/GeneralsMD/Code/Tests/test_debug.cpp
@@ -18,8 +18,9 @@
* the literal "!debug.io flat add" and has no logic.
*
* Deliberately NOT covered: the con/net/ods/flat I/O classes (window, named
- * pipe, OutputDebugString, log files), DebugStackwalk (needs dbghelp and real
- * frames), the exception filter, and debug.exit (it calls exit(1)).
+ * pipe, OutputDebugString, log files), the exception filter, and debug.exit
+ * (it calls exit(1)). DebugStackwalk is covered, but only just: it needs
+ * dbghelp and a PDB next to the test exe.
*/
#include "test_harness.h"
@@ -846,3 +847,31 @@ TEST(debug_memory_realloc_truth_table)
CHECK_EQ(DebugReAllocMemory(p, 0), (void *)0);
}
+
+
+/* The crash reports out of generals.exe all said "1 addresses:": on a current
+ dbghelp.dll the 32-bit-only StackWalk this used fails on its very first step,
+ and the one address in the report was the frame the walk had been seeded with.
+ Walking out of a helper and finding main() proves the walk moves and that the
+ symbols resolve - the latter needs the PDB next to the test exe. */
+static int walkFromHere(DebugStackwalk::Signature &sig)
+{
+ return DebugStackwalk::StackWalk(sig);
+}
+
+TEST(stackwalk_walks_past_the_frame_it_started_in)
+{
+ DebugStackwalk::Signature sig;
+ int count = walkFromHere(sig);
+ CHECK(count > 1);
+
+ bool sawMain = false;
+ for (unsigned i = 0; i < sig.Size(); ++i)
+ {
+ char buf[512];
+ sig.GetSymbol(sig.GetAddress(i), buf, sizeof(buf));
+ if (strstr(buf, "main+") != 0)
+ sawMain = true;
+ }
+ CHECK(sawMain);
+}