HydraHook
Direct3D and Core Audio hooking library for Windows
Loading...
Searching...
No Matches
CrashHandler.cpp File Reference

Crash handler implementation – SEH, minidump, CRT handlers. More...

#include <Windows.h>
#include <DbgHelp.h>
#include <eh.h>
#include <signal.h>
#include <cstdio>
#include <ctime>
#include <mutex>
#include <atomic>
#include <vector>
#include "HydraHook/Engine/HydraHookCore.h"
#include "HydraHook/Engine/HydraHookDirect3D9.h"
#include "HydraHook/Engine/HydraHookDirect3D10.h"
#include "HydraHook/Engine/HydraHookDirect3D11.h"
#include "HydraHook/Engine/HydraHookDirect3D12.h"
#include "HydraHook/Engine/HydraHookCoreAudio.h"
#include "Engine.h"
#include "CrashHandler.h"
#include "Exceptions.hpp"
#include "Utils/Global.h"
#include <spdlog/spdlog.h>

Go to the source code of this file.

Classes

struct  CrashConfigSnapshot
struct  CrashRegistration

Macros

#define WIN32_LEAN_AND_MEAN

Functions

static void ReapDeferredSnapshots ()
static void RetireSnapshot (CrashConfigSnapshot *retired)
static const char * ExceptionCodeToString (DWORD code)
static void GetModuleFromAddress (PVOID address, char *moduleName, size_t nameLen, DWORD_PTR *offset)
static MINIDUMP_TYPE GetMiniDumpTypeFlags (HYDRAHOOK_DUMP_TYPE type)
static void AppendDumpDirectory (std::vector< std::string > &dirs, std::string path)
static std::vector< std::string > DumpDirectoryCandidates (const CrashConfigSnapshot *snap)
static std::string GetProcessBaseName ()
static void WriteCrashDump (EXCEPTION_POINTERS *exInfo, const char *trigger)
static LONG HydraHookUnhandledExceptionFilter (EXCEPTION_POINTERS *exInfo)
static void HydraHookTerminateHandler ()
static void HydraHookInvalidParameterHandler (const wchar_t *expression, const wchar_t *function, const wchar_t *file, unsigned int line, uintptr_t pReserved)
static void HydraHookPurecallHandler ()
static void HydraHookSehTranslator (unsigned int code, EXCEPTION_POINTERS *info)
static CrashConfigSnapshot * MakeSnapshot (PHYDRAHOOK_ENGINE engine)
bool HydraHookCrashHandlerInstall (PHYDRAHOOK_ENGINE engine)
 Installs global crash handlers (ref-counted) for the given engine.
void HydraHookCrashHandlerUninstall (PHYDRAHOOK_ENGINE engine)
 Decrements the crash handler reference count.
void HydraHookCrashHandlerInstallThreadSEH ()
 Installs the per-thread SEH-to-C++ translator on the calling thread.
PHYDRAHOOK_ENGINE HydraHookCrashHandlerActiveOwnerForTest ()
 Test accessor. Returns the engine that owns the published crash snapshot.
bool HydraHookCrashHandlerReferencesEngine (PHYDRAHOOK_ENGINE engine)
 True when a published or deferred snapshot still names this engine. Destroy must not free the engine while this is true.

Variables

static LPTOP_LEVEL_EXCEPTION_FILTER s_prevUnhandledFilter = nullptr
static std::terminate_handler s_prevTerminateHandler = nullptr
static _invalid_parameter_handler s_prevInvalidParamHandler = nullptr
static _purecall_handler s_prevPurecallHandler = nullptr
static std::atomic< int > s_refCount { 0 }
static std::mutex s_installMutex
static std::atomic< CrashConfigSnapshot * > s_snapshot { nullptr }
static std::atomic< int > s_dumpInFlight { 0 }
static std::vector< CrashRegistration > s_registrations
static std::vector< CrashConfigSnapshot * > s_deferredSnapshots

Detailed Description

Crash handler implementation – SEH, minidump, CRT handlers.

Definition in file CrashHandler.cpp.

Macro Definition Documentation

◆ WIN32_LEAN_AND_MEAN

#define WIN32_LEAN_AND_MEAN

Definition at line 9 of file CrashHandler.cpp.

Function Documentation

◆ AppendDumpDirectory()

void AppendDumpDirectory ( std::vector< std::string > & dirs,
std::string path )
static

Definition at line 180 of file CrashHandler.cpp.

References AppendDumpDirectory().

Referenced by AppendDumpDirectory(), and DumpDirectoryCandidates().

◆ DumpDirectoryCandidates()

◆ ExceptionCodeToString()

const char * ExceptionCodeToString ( DWORD code)
static

Definition at line 96 of file CrashHandler.cpp.

References ExceptionCodeToString().

Referenced by ExceptionCodeToString(), and WriteCrashDump().

◆ GetMiniDumpTypeFlags()

MINIDUMP_TYPE GetMiniDumpTypeFlags ( HYDRAHOOK_DUMP_TYPE type)
static

◆ GetModuleFromAddress()

void GetModuleFromAddress ( PVOID address,
char * moduleName,
size_t nameLen,
DWORD_PTR * offset )
static

Definition at line 133 of file CrashHandler.cpp.

References GetModuleFromAddress().

Referenced by GetModuleFromAddress(), and WriteCrashDump().

◆ GetProcessBaseName()

std::string GetProcessBaseName ( )
static

Definition at line 217 of file CrashHandler.cpp.

References GetProcessBaseName().

Referenced by GetProcessBaseName(), and WriteCrashDump().

◆ HydraHookCrashHandlerActiveOwnerForTest()

PHYDRAHOOK_ENGINE HydraHookCrashHandlerActiveOwnerForTest ( )

Test accessor. Returns the engine that owns the published crash snapshot.

Definition at line 600 of file CrashHandler.cpp.

References HydraHookCrashHandlerActiveOwnerForTest(), CrashConfigSnapshot::OwnerEngine, s_installMutex, and s_snapshot.

Referenced by HydraHookCrashHandlerActiveOwnerForTest().

◆ HydraHookCrashHandlerInstall()

bool HydraHookCrashHandlerInstall ( PHYDRAHOOK_ENGINE engine)

Installs global crash handlers (ref-counted) for the given engine.

On first call: registers SetUnhandledExceptionFilter, set_terminate, _set_invalid_parameter_handler, and _set_purecall_handler. Subsequent calls increment the reference count without re-installing.

Parameters
engineEngine whose CrashHandler config controls dump output.
Returns
false when engine is null or the snapshot could not be registered.

Definition at line 490 of file CrashHandler.cpp.

References HydraHookCrashHandlerInstall(), HydraHookInvalidParameterHandler(), HydraHookPurecallHandler(), HydraHookTerminateHandler(), HydraHookUnhandledExceptionFilter(), MakeSnapshot(), ReapDeferredSnapshots(), RetireSnapshot(), s_installMutex, s_prevInvalidParamHandler, s_prevPurecallHandler, s_prevTerminateHandler, s_prevUnhandledFilter, s_refCount, s_registrations, and s_snapshot.

Referenced by HydraHookCrashHandlerInstall(), and HydraHookEngineCreate().

◆ HydraHookCrashHandlerInstallThreadSEH()

void HydraHookCrashHandlerInstallThreadSEH ( )

Installs the per-thread SEH-to-C++ translator on the calling thread.

Must be called from the engine worker thread. Bridges Win32 structured exceptions into HydraHook::Core::Exceptions::SehException so that existing try/catch blocks can catch hardware faults.

Definition at line 595 of file CrashHandler.cpp.

References HydraHookCrashHandlerInstallThreadSEH(), and HydraHookSehTranslator().

Referenced by HydraHookCrashHandlerInstallThreadSEH(), and HydraHookMainThread().

◆ HydraHookCrashHandlerReferencesEngine()

bool HydraHookCrashHandlerReferencesEngine ( PHYDRAHOOK_ENGINE engine)

True when a published or deferred snapshot still names this engine. Destroy must not free the engine while this is true.

Definition at line 607 of file CrashHandler.cpp.

References HydraHookCrashHandlerReferencesEngine(), CrashConfigSnapshot::OwnerEngine, ReapDeferredSnapshots(), s_deferredSnapshots, s_installMutex, and s_snapshot.

Referenced by DestroyEngine(), HydraHookCrashHandlerReferencesEngine(), and HydraHookEngineCreate().

◆ HydraHookCrashHandlerUninstall()

void HydraHookCrashHandlerUninstall ( PHYDRAHOOK_ENGINE engine)

Decrements the crash handler reference count.

If the uninstalling engine owns the active crash config snapshot, the next surviving registration is published. The previous snapshot is deleted only after in-flight dump writers have finished. When the last engine uninstalls, restores all previous handlers.

Parameters
engineThe engine that is being destroyed.

Definition at line 530 of file CrashHandler.cpp.

References HydraHookCrashHandlerUninstall(), ReapDeferredSnapshots(), RetireSnapshot(), s_installMutex, s_prevInvalidParamHandler, s_prevPurecallHandler, s_prevTerminateHandler, s_prevUnhandledFilter, s_refCount, s_registrations, and s_snapshot.

Referenced by DestroyEngine(), HydraHookCrashHandlerUninstall(), and HydraHookEngineCreate().

◆ HydraHookInvalidParameterHandler()

void HydraHookInvalidParameterHandler ( const wchar_t * expression,
const wchar_t * function,
const wchar_t * file,
unsigned int line,
uintptr_t pReserved )
static

◆ HydraHookPurecallHandler()

void HydraHookPurecallHandler ( )
static

◆ HydraHookSehTranslator()

void HydraHookSehTranslator ( unsigned int code,
EXCEPTION_POINTERS * info )
static

◆ HydraHookTerminateHandler()

void HydraHookTerminateHandler ( )
static

◆ HydraHookUnhandledExceptionFilter()

LONG HydraHookUnhandledExceptionFilter ( EXCEPTION_POINTERS * exInfo)
static

◆ MakeSnapshot()

CrashConfigSnapshot * MakeSnapshot ( PHYDRAHOOK_ENGINE engine)
static

◆ ReapDeferredSnapshots()

◆ RetireSnapshot()

void RetireSnapshot ( CrashConfigSnapshot * retired)
static

◆ WriteCrashDump()

Variable Documentation

◆ s_deferredSnapshots

std::vector<CrashConfigSnapshot*> s_deferredSnapshots
static

◆ s_dumpInFlight

std::atomic<int> s_dumpInFlight { 0 }
static

Definition at line 59 of file CrashHandler.cpp.

Referenced by ReapDeferredSnapshots(), RetireSnapshot(), and WriteCrashDump().

◆ s_installMutex

◆ s_prevInvalidParamHandler

_invalid_parameter_handler s_prevInvalidParamHandler = nullptr
static

◆ s_prevPurecallHandler

_purecall_handler s_prevPurecallHandler = nullptr
static

◆ s_prevTerminateHandler

std::terminate_handler s_prevTerminateHandler = nullptr
static

◆ s_prevUnhandledFilter

LPTOP_LEVEL_EXCEPTION_FILTER s_prevUnhandledFilter = nullptr
static

◆ s_refCount

std::atomic<int> s_refCount { 0 }
static

◆ s_registrations

std::vector<CrashRegistration> s_registrations
static

◆ s_snapshot