diff --git a/src/FatalHandler.h b/src/FatalHandler.h new file mode 100644 index 0000000..163049b --- /dev/null +++ b/src/FatalHandler.h @@ -0,0 +1,8 @@ +// +// Created by illyum on 9/13/2024. +// + +#ifndef FATALHANDLER_H +#define FATALHANDLER_H + +#endif //FATALHANDLER_H diff --git a/src/platform/posix/FatalHandlerPosix.cpp b/src/platform/posix/FatalHandlerPosix.cpp new file mode 100644 index 0000000..ade1e26 --- /dev/null +++ b/src/platform/posix/FatalHandlerPosix.cpp @@ -0,0 +1,29 @@ +// +// Created by illyum on 9/13/2024. +// + +#include "FatalHandler.h" +#include +#include +#include +#include +#include + +void FatalHandler(const std::string& message) { + std::ofstream crashFile("crash.log", std::ios::app); + crashFile << "FATAL ERROR: " << message << std::endl; + + void* callstack[128]; + int frames = backtrace(callstack, 128); + char** symbols = backtrace_symbols(callstack, frames); + + crashFile << "Stack trace:\n"; + for (int i = 0; i < frames; ++i) { + crashFile << symbols[i] << std::endl; + } + + free(symbols); + crashFile.close(); + + std::abort(); +} \ No newline at end of file diff --git a/src/platform/windows/FatalHandlerWindows.cpp b/src/platform/windows/FatalHandlerWindows.cpp new file mode 100644 index 0000000..e75f4dc --- /dev/null +++ b/src/platform/windows/FatalHandlerWindows.cpp @@ -0,0 +1,56 @@ +// +// Created by illyum on 9/13/2024. +// + +#include "FatalHandler.h" +#include +#include +#include +#include + +#pragma comment(lib, "dbghelp.lib") + +void FatalHandler(const std::string& message) { + std::ofstream crashFile("crash.log", std::ios::app); + crashFile << "FATAL ERROR: " << message << std::endl; + + HANDLE process = GetCurrentProcess(); + SymInitialize(process, NULL, TRUE); + + CONTEXT context; + RtlCaptureContext(&context); + + STACKFRAME64 stackFrame = {}; + stackFrame.AddrPC.Offset = context.Rip; + stackFrame.AddrPC.Mode = AddrModeFlat; + stackFrame.AddrFrame.Offset = context.Rbp; + stackFrame.AddrFrame.Mode = AddrModeFlat; + stackFrame.AddrStack.Offset = context.Rsp; + stackFrame.AddrStack.Mode = AddrModeFlat; + + crashFile << "Stack trace:\n"; + + while (StackWalk64(IMAGE_FILE_MACHINE_AMD64, process, GetCurrentThread(), &stackFrame, &context, NULL, SymFunctionTableAccess64, SymGetModuleBase64, NULL)) { + DWORD64 address = stackFrame.AddrPC.Offset; + if (address == 0) { + break; + } + + char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(TCHAR)]; + PSYMBOL_INFO symbol = (PSYMBOL_INFO)buffer; + symbol->SizeOfStruct = sizeof(SYMBOL_INFO); + symbol->MaxNameLen = MAX_SYM_NAME; + + DWORD64 displacement = 0; + if (SymFromAddr(process, address, &displacement, symbol)) { + crashFile << symbol->Name << " - 0x" << std::hex << symbol->Address << std::dec << "\n"; + } else { + crashFile << "Unresolved symbol at address: 0x" << std::hex << address << std::dec << "\n"; + } + } + + SymCleanup(process); + crashFile.close(); + + std::abort(); +} \ No newline at end of file