r5sdk/r5dev/public/tier0/crashhandler.h
Kawe Mazidjatari f120354e96 Initial port to CMake
* All libraries have been isolated from each other, and build into separate artifacts.
* Project has been restructured to support isolating libraries.
* CCrashHandler now calls a callback on crash (setup from core/dllmain.cpp, this can be setup in any way for any project. This callback is getting called when the apllication crashes. Useful for flushing buffers before closing handles to logging files for example).
* Tier0 'CoreMsgV' function now calls a callback sink, which could be set by the user (currently setup to the SDK's internal logger in core/dllmain.cpp).

TODO:
* Add a batch file to autogenerate all projects.
* Add support for dedicated server.
* Add support for client dll.

Bugs:
* Game crashes on the title screen after the UI script compiler has finished (root cause unknown).
* Curl error messages are getting logged twice for the dedicated server due to the removal of all "DEDICATED" preprocessor directives to support isolating projects. This has to be fixed properly!
2023-05-10 00:05:38 +02:00

104 lines
3.1 KiB
C++

#ifndef CRASHHANDLER_H
#define CRASHHANDLER_H
#define CRASHHANDLER_MAX_MODULES 4096
#define CRASHMESSAGE_MSG_EXECUTABLE "bin\\crashmsg.exe"
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
class CCrashHandler
{
public:
CCrashHandler();
~CCrashHandler();
void Init();
void Shutdown();
//-------------------------------------------------------------------------
// Inlines:
//-------------------------------------------------------------------------
void Start();
void End();
void Lock() const { m_Mutex.lock(); };
void Unlock() const { m_Mutex.unlock(); };
void SetState(bool bState) { m_bCallState = bState; };
bool GetState() const { return m_bCallState; };
bool IsValid() const { return m_hExceptionHandler != nullptr; };
bool Handled() const { return m_bExceptionHandled; };
//-------------------------------------------------------------------------
// Formatters:
//-------------------------------------------------------------------------
void FormatCrash();
void FormatCallstack();
void FormatRegisters();
void FormatModules();
void FormatSystemInfo();
void FormatBuildInfo();
//-------------------------------------------------------------------------
// Utility:
//-------------------------------------------------------------------------
const CHAR* ExceptionToString() const;
const CHAR* ExceptionToString(DWORD nExceptionCode) const;
void SetExceptionPointers(EXCEPTION_POINTERS* pExceptionPointers) { m_pExceptionPointers = pExceptionPointers; }
void SetCrashCallback(PVOID pCrashCallback) { m_pCrashCallback = pCrashCallback; }
void AddWhitelist(void* pWhitelist);
void RemoveWhitelist(void* pWhitelist);
bool HasWhitelist();
void GetCallStack();
void WriteFile();
void CreateMessageProcess();
void CrashCallback();
private:
//-------------------------------------------------------------------------
// Internals:
//-------------------------------------------------------------------------
void FormatExceptionAddress();
void FormatExceptionAddress(LPCSTR pExceptionAddress);
void FormatExceptionCode();
void FormatALU(const CHAR* pszRegister, DWORD64 nContent);
void FormatFPU(const CHAR* pszRegister, M128A* pxContent);
bool IsPageAccessible() const;
private:
enum
{
NUM_FRAMES_TO_SEARCH = 7,
NUM_FRAMES_TO_CAPTURE = 128
};
PVOID m_ppStackTrace[NUM_FRAMES_TO_CAPTURE];
PVOID m_pCrashCallback;
PVOID m_hExceptionHandler;
EXCEPTION_POINTERS* m_pExceptionPointers;
WORD m_nCapturedFrames;
string m_svBuffer; // Buffer containing the entire crash log.
string m_svCrashMsgInfo;
uint8_t m_nCrashMsgFlags;
bool m_bCallState; // Set when called to prevent recursive calls.
bool m_bCrashMsgCreated; // Set when crashmsg.exe is created to prevent recursive messages.
bool m_bExceptionHandled; // Set on filter entry, unset within the same lock if exception was not handled, never unset if handled.
std::set<void*> m_WhiteList;
mutable std::mutex m_Mutex;
};
extern CCrashHandler* g_CrashHandler;
#endif // CRASHHANDLER_H