2022-12-26 20:11:37 +01:00
|
|
|
#ifndef CRASHHANDLER_H
|
|
|
|
#define CRASHHANDLER_H
|
2024-02-04 00:55:07 +01:00
|
|
|
#include "tier1/fmtstr.h"
|
2022-12-26 20:11:37 +01:00
|
|
|
|
2023-04-08 10:50:32 +02:00
|
|
|
#define CRASHMESSAGE_MSG_EXECUTABLE "bin\\crashmsg.exe"
|
|
|
|
|
2022-12-26 20:11:37 +01:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose:
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
class CCrashHandler
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CCrashHandler();
|
|
|
|
~CCrashHandler();
|
|
|
|
|
2023-01-31 21:48:42 +01:00
|
|
|
void Init();
|
|
|
|
void Shutdown();
|
|
|
|
|
2024-02-04 00:55:07 +01:00
|
|
|
void Reset();
|
|
|
|
|
2022-12-27 02:28:43 +01:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
// Inlines:
|
|
|
|
//-------------------------------------------------------------------------
|
2022-12-28 21:38:47 +01:00
|
|
|
void Start();
|
|
|
|
void End();
|
|
|
|
|
2024-02-04 00:55:07 +01:00
|
|
|
inline void SetExit(const bool bExit) { m_bExit = bExit; };
|
|
|
|
inline bool GetExit() const { return m_bExit; };
|
2022-12-28 21:38:47 +01:00
|
|
|
|
2023-06-19 01:19:47 +02:00
|
|
|
inline bool IsValid() const { return m_hExceptionHandler != nullptr; };
|
2022-12-26 20:11:37 +01:00
|
|
|
|
2022-12-27 02:28:43 +01:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
// Formatters:
|
|
|
|
//-------------------------------------------------------------------------
|
2022-12-26 20:11:37 +01:00
|
|
|
void FormatCrash();
|
|
|
|
void FormatCallstack();
|
|
|
|
void FormatRegisters();
|
2022-12-27 14:12:46 +01:00
|
|
|
void FormatModules();
|
2022-12-26 22:00:38 +01:00
|
|
|
void FormatSystemInfo();
|
2022-12-26 23:35:41 +01:00
|
|
|
void FormatBuildInfo();
|
2022-12-26 20:11:37 +01:00
|
|
|
|
2022-12-27 02:28:43 +01:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
// Utility:
|
|
|
|
//-------------------------------------------------------------------------
|
2024-02-04 00:55:07 +01:00
|
|
|
const char* ExceptionToString() const;
|
|
|
|
const char* ExceptionToString(const DWORD nExceptionCode) const;
|
2023-05-10 00:05:38 +02:00
|
|
|
|
2024-02-04 00:55:07 +01:00
|
|
|
void SetExceptionPointers(EXCEPTION_POINTERS* const pExceptionPointers) { m_pExceptionPointers = pExceptionPointers; }
|
2023-05-10 00:05:38 +02:00
|
|
|
void SetCrashCallback(PVOID pCrashCallback) { m_pCrashCallback = pCrashCallback; }
|
2022-12-26 20:11:37 +01:00
|
|
|
|
2024-02-04 00:55:07 +01:00
|
|
|
void CaptureCallStack();
|
2022-12-27 23:08:59 +01:00
|
|
|
void WriteFile();
|
|
|
|
|
2022-12-27 02:28:43 +01:00
|
|
|
void CreateMessageProcess();
|
2023-05-10 00:05:38 +02:00
|
|
|
void CrashCallback();
|
2022-12-26 20:11:37 +01:00
|
|
|
|
2022-12-26 20:33:09 +01:00
|
|
|
private:
|
2022-12-26 20:11:37 +01:00
|
|
|
|
2022-12-27 02:28:43 +01:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
// Internals:
|
|
|
|
//-------------------------------------------------------------------------
|
2022-12-27 23:08:59 +01:00
|
|
|
void FormatExceptionAddress();
|
2024-02-04 00:55:07 +01:00
|
|
|
void FormatExceptionAddress(const LPCSTR pExceptionAddress);
|
2022-12-26 20:33:09 +01:00
|
|
|
void FormatExceptionCode();
|
2022-12-26 20:11:37 +01:00
|
|
|
|
2024-02-04 00:55:07 +01:00
|
|
|
void FormatALU(const char* const pszRegister, const DWORD64 nContent);
|
|
|
|
void FormatFPU(const char* const pszRegister, const M128A* const pxContent);
|
2022-12-26 20:11:37 +01:00
|
|
|
|
2022-12-26 20:33:09 +01:00
|
|
|
bool IsPageAccessible() const;
|
2022-12-26 20:11:37 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
enum
|
|
|
|
{
|
2024-02-04 00:55:07 +01:00
|
|
|
NUM_FRAMES_TO_CAPTURE = 128,
|
|
|
|
MAX_MODULE_HANDLES = 4096
|
2022-12-26 20:11:37 +01:00
|
|
|
};
|
|
|
|
|
2024-02-04 00:55:07 +01:00
|
|
|
// Captured callstack frames.
|
2022-12-26 20:11:37 +01:00
|
|
|
PVOID m_ppStackTrace[NUM_FRAMES_TO_CAPTURE];
|
2024-02-04 00:55:07 +01:00
|
|
|
WORD m_nCapturedFrames;
|
|
|
|
|
|
|
|
// The loaded module handles that are being tracked.
|
|
|
|
HMODULE m_ppModuleHandles[MAX_MODULE_HANDLES];
|
|
|
|
|
|
|
|
// Custom crash callback that's called after the logs have been written.
|
2023-05-10 00:05:38 +02:00
|
|
|
PVOID m_pCrashCallback;
|
2024-02-04 00:55:07 +01:00
|
|
|
|
|
|
|
// Current exception handler, only kept here for tracking as we need the
|
|
|
|
// handle if we want to remove this handler.
|
2023-05-10 00:05:38 +02:00
|
|
|
PVOID m_hExceptionHandler;
|
2024-02-04 00:55:07 +01:00
|
|
|
|
2022-12-26 20:11:37 +01:00
|
|
|
EXCEPTION_POINTERS* m_pExceptionPointers;
|
2022-12-27 02:28:43 +01:00
|
|
|
|
2024-02-04 00:55:07 +01:00
|
|
|
// 32KiB buffer containing the entire crash log, static as we shouldn't
|
|
|
|
// allocate any dynamic memory when writing the log files as that is
|
|
|
|
// unsafe during a crash.
|
|
|
|
CFmtStrQuietTruncationN<32768> m_Buffer;
|
|
|
|
|
|
|
|
// Buffer containing the module name we crashed in.
|
|
|
|
CFmtStrQuietTruncationN<256> m_CrashingModule;
|
|
|
|
|
|
|
|
// Buffer containing cmd line arguments for the external crash message
|
|
|
|
// process.
|
|
|
|
CFmtStrQuietTruncationN<256> m_MessageCmdLine;
|
2022-12-27 02:28:43 +01:00
|
|
|
uint8_t m_nCrashMsgFlags;
|
|
|
|
|
2024-02-04 00:55:07 +01:00
|
|
|
// Set when called to prevent recursive calls.
|
|
|
|
bool m_bExit;
|
|
|
|
|
|
|
|
// Set when crashmsg.exe is created to prevent recursive messages.
|
|
|
|
bool m_bMessageCreated;
|
2022-12-27 23:08:59 +01:00
|
|
|
|
2024-02-04 00:55:07 +01:00
|
|
|
mutable RTL_SRWLOCK m_Lock;
|
2022-12-26 20:11:37 +01:00
|
|
|
};
|
|
|
|
|
2024-02-04 00:55:07 +01:00
|
|
|
extern CCrashHandler g_CrashHandler;
|
2022-12-27 02:28:43 +01:00
|
|
|
|
2022-12-26 20:11:37 +01:00
|
|
|
#endif // CRASHHANDLER_H
|