mirror of
https://github.com/1dot13/source.git
synced 2026-08-12 14:10:23 +02:00
Capture crashes with a first-chance vectored handler and dump them heap-free
Report crashes for every player, not only those running under Wine's WINEDEBUG.
A last-chance UnhandledExceptionFilter is no good: faults on the message-pump /
WindowProcedure path have no game __except on their stack, and under Wine the
WndProc dispatch swallows them before they ever reach "unhandled". Install a
vectored handler instead -- it runs first-chance, ahead of every frame handler,
records the fault and returns EXCEPTION_CONTINUE_SEARCH, so normal SEH is
unaffected.
The dump is deliberately heap-free. The crash most worth reporting is often heap
corruption, so anything that allocates (the game Logger, std::vector, DbgHelp's
Sym* family) would fault again and take the report down with it. Using only stack
buffers and raw Win32, writeExceptionBacktrace writes the registers, the faulting
address, a UTC timestamp, the build id, the loaded-module table and a
bounds-checked manual EBP walk into a fresh numbered crash_report_NNN.txt.
The module table is what makes the addresses mean anything. Our executables are
linked /DYNAMICBASE and keep their .reloc section, so the loader is free to move
the image: Wine leaves it at the preferred base, Windows ASLR does not. A report
listing only runtime addresses is symbolizable by luck, and silently wrong once
the luck runs out. Recording where each image actually landed turns the offset
into arithmetic:
llvm-symbolizer --obj=JA2.exe --adjust-vma=$((<JA2.exe base> - 0x400000))
It is walked off the PEB loader list because both alternatives -- EnumProcessModules
and DbgHelp's module APIs -- allocate, and this path must not. Having the table
also frees the backtrace from restricting itself to our own module's return
addresses: a fault inside ddraw/fmod/bink is exactly the case worth seeing, and
an address belonging to no module is recognizable as the frame-pointer debris it
is.
Two things go into the report beside the machine state. czVersionString stamps
the build, so a report matches the exact PDB it has to be symbolized against, and
the optional HANDLE from Ja2 Settings names the player, so a report can be tied
to whoever raises it with us. The handle is sanitized where it is set rather than
where it is used: it is player input that lands in a line-oriented text report,
so anything that could forge a line (CR/LF, control and non-ASCII characters) is
dropped and the length is capped.
Finally the player is told. The message is composed in the handler, while the
fault details and the report's filename are still in hand, but shown from
SGPExit: first-chance means the exception may yet be handled downstream, and a
message box pumps messages, which on the heap that just faulted is a second
crash. It replaces the generic "Unhandled exception. Unable to recover." box
rather than adding to it -- it says more, and it names the file we need attached
to the bug report.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
committed by
majcosta
co-authored by
Claude Opus 5
parent
1e2ce70315
commit
71cc553a07
+40
-1
@@ -630,6 +630,7 @@ static vfs::String getGameID()
|
||||
}
|
||||
|
||||
#include "debug_util.h"
|
||||
#include "GameVersion.h" // czVersionString, stamped into crash reports
|
||||
#include <vfs/Aspects/vfs_logging.h>
|
||||
|
||||
class VfsLogAdapter : public vfs::Aspects::ILogger
|
||||
@@ -681,8 +682,35 @@ private:
|
||||
// }
|
||||
//};
|
||||
|
||||
// Catch crashes on any thread/path. A last-chance UnhandledExceptionFilter is no
|
||||
// good: faults on the message-pump/WindowProcedure path have no game __except on
|
||||
// their stack, and under Wine the WndProc dispatch swallows them before they ever
|
||||
// reach "unhandled". A vectored handler runs first-chance, ahead of every frame
|
||||
// handler; it only records the fault and continues the search.
|
||||
static LONG CALLBACK VectoredCrashHandler(EXCEPTION_POINTERS* pExceptInfo)
|
||||
{
|
||||
switch (pExceptInfo->ExceptionRecord->ExceptionCode)
|
||||
{
|
||||
case EXCEPTION_ACCESS_VIOLATION:
|
||||
case EXCEPTION_ILLEGAL_INSTRUCTION:
|
||||
case EXCEPTION_IN_PAGE_ERROR:
|
||||
case EXCEPTION_STACK_OVERFLOW:
|
||||
case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
|
||||
case EXCEPTION_INT_DIVIDE_BY_ZERO:
|
||||
case EXCEPTION_PRIV_INSTRUCTION:
|
||||
sgp::writeExceptionBacktrace(pExceptInfo);
|
||||
break;
|
||||
default:
|
||||
break; // C++ EH (0xE06D7363) and other first-chance noise: ignore
|
||||
}
|
||||
return EXCEPTION_CONTINUE_SEARCH; // never handle; let normal SEH run
|
||||
}
|
||||
|
||||
int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCommandLine, int sCommandShow)
|
||||
{
|
||||
AddVectoredExceptionHandler(1, VectoredCrashHandler); // 1 = call first
|
||||
sgp::setCrashBuildId(czVersionString); // packaged: the commit SHA whose PDB we kept
|
||||
|
||||
#ifdef _DEBUG
|
||||
// Use this one ONLY if you're having memory corruption issues that can be repeated in a short time
|
||||
// Otherwise it will just run out of memory.
|
||||
@@ -895,7 +923,13 @@ void SGPExit(void)
|
||||
|
||||
ShutdownStandardGamingPlatform();
|
||||
// ShowCursor(TRUE);
|
||||
if(strlen(gzErrorMsg))
|
||||
// A recorded crash says more than "unable to recover", and names the report the
|
||||
// player has to send us, so it replaces the generic box rather than adding to it.
|
||||
if (const wchar_t* crashMsg = sgp::crashReportMessage())
|
||||
{
|
||||
MessageBoxW(NULL, crashMsg, L"Jagged Alliance 2 1.13 - Crash", MB_OK | MB_ICONERROR);
|
||||
}
|
||||
else if(strlen(gzErrorMsg))
|
||||
{
|
||||
MessageBox(NULL, gzErrorMsg, "Error", MB_OK | MB_ICONERROR );
|
||||
}
|
||||
@@ -913,6 +947,11 @@ void GetRuntimeSettings( )
|
||||
vfs::PropertyContainer oProps;
|
||||
oProps.initFromIniFile(GAME_INI_FILE);
|
||||
PopulateSectionFromCommandLine(oProps, "Ja2 Settings");
|
||||
|
||||
// Optional player handle stamped into crash reports written from here on, so a
|
||||
// report can be tied to whoever raises it with us. Unset = field omitted.
|
||||
sgp::setCrashUserHandle(
|
||||
oProps.getStringProperty("Ja2 Settings", L"HANDLE").c_str());
|
||||
|
||||
vfs::String loc = oProps.getStringProperty("Ja2 Settings", L"LOCALE");
|
||||
if(!loc.empty())
|
||||
|
||||
Reference in New Issue
Block a user