import std;
import vulkan_raii;
namespace crash_handler {
std::string app_name;
std::string dump_path;
bool initialized = false;
#if defined(_WIN32)
// Windows implementation using Windows Error Reporting (WER)
LONG WINAPI windows_exception_handler(EXCEPTION_POINTERS* exception_pointers) {
// Create a unique filename for the minidump
std::string filename = dump_path + "\\" + app_name + "_" +
std::to_string(std::chrono::system_clock::now().time_since_epoch().count()) + ".dmp";
// Create the minidump file
HANDLE file = CreateFileA(
filename.c_str(),
GENERIC_WRITE,
0,
nullptr,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
nullptr
);
if (file != INVALID_HANDLE_VALUE) {
// Initialize minidump info
MINIDUMP_EXCEPTION_INFORMATION exception_info;
exception_info.ThreadId = GetCurrentThreadId();
exception_info.ExceptionPointers = exception_pointers;
exception_info.ClientPointers = FALSE;
// Write the minidump
MiniDumpWriteDump(
GetCurrentProcess(),
GetCurrentProcessId(),
file,
MiniDumpWithFullMemory, // Dump type
&exception_info,
nullptr,
nullptr
);
CloseHandle(file);
std::cerr << "Minidump written to: " << filename << std::endl;
} else {
std::cerr << "Failed to create minidump file" << std::endl;
}
// Continue with normal exception handling
return EXCEPTION_CONTINUE_SEARCH;
}
void initialize(const std::string& application_name, const std::string& minidump_path) {
if (initialized) return;
app_name = application_name;
dump_path = minidump_path;
// Create the dump directory if it doesn't exist
CreateDirectoryA(dump_path.c_str(), nullptr);
// Set up the exception handler
SetUnhandledExceptionFilter(windows_exception_handler);
initialized = true;
}
#elif defined(__linux__)
// Linux implementation using Google Breakpad
// Note: This requires linking against the Google Breakpad library
#include "client/linux/handler/exception_handler.h"
// Callback for when a minidump is generated
static bool minidump_callback(const google_breakpad::MinidumpDescriptor& descriptor,
void* context, bool succeeded) {
std::cerr << "Minidump generated: " << descriptor.path() << std::endl;
return succeeded;
}
google_breakpad::ExceptionHandler* exception_handler = nullptr;
void initialize(const std::string& application_name, const std::string& minidump_path) {
if (initialized) return;
app_name = application_name;
dump_path = minidump_path;
// Create the dump directory if it doesn't exist
std::filesystem::create_directories(dump_path);
// Set up the exception handler
google_breakpad::MinidumpDescriptor descriptor(dump_path);
exception_handler = new google_breakpad::ExceptionHandler(
descriptor,
nullptr,
minidump_callback,
nullptr,
true,
-1
);
initialized = true;
}
#elif defined(__APPLE__)
// macOS implementation using Google Breakpad
// Similar to Linux implementation
#endif
}