feat(crash): add crash reporting
This commit is contained in:
parent
842c8fb19d
commit
0fa35696bb
8
src/FatalHandler.h
Normal file
8
src/FatalHandler.h
Normal file
@ -0,0 +1,8 @@
|
||||
//
|
||||
// Created by illyum on 9/13/2024.
|
||||
//
|
||||
|
||||
#ifndef FATALHANDLER_H
|
||||
#define FATALHANDLER_H
|
||||
|
||||
#endif //FATALHANDLER_H
|
29
src/platform/posix/FatalHandlerPosix.cpp
Normal file
29
src/platform/posix/FatalHandlerPosix.cpp
Normal file
@ -0,0 +1,29 @@
|
||||
//
|
||||
// Created by illyum on 9/13/2024.
|
||||
//
|
||||
|
||||
#include "FatalHandler.h"
|
||||
#include <execinfo.h>
|
||||
#include <cstdlib>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
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();
|
||||
}
|
56
src/platform/windows/FatalHandlerWindows.cpp
Normal file
56
src/platform/windows/FatalHandlerWindows.cpp
Normal file
@ -0,0 +1,56 @@
|
||||
//
|
||||
// Created by illyum on 9/13/2024.
|
||||
//
|
||||
|
||||
#include "FatalHandler.h"
|
||||
#include <windows.h>
|
||||
#include <dbghelp.h>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
|
||||
#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();
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user