11#include <cpptrace/cpptrace.hpp>
12#include <cpptrace/formatting.hpp>
28std::atomic<bool> g_handling{
false};
39auto demangle_exception_type(
const std::exception& e) -> std::string
41 return cpptrace::demangle(
typeid(e).
name());
44auto generate_stack_trace() -> trace_info
48 auto stack_trace = cpptrace::generate_trace(1);
49 return trace_info{stack_trace.to_string(
false)};
68 g_interrupt_handler = handler;
73 g_termination_handler = handler;
78 g_crash_handler = handler;
83 g_exception_handler = handler;
91 return "SIGSEGV (Segmentation fault)";
93 return "SIGABRT (Process abort)";
95 return "SIGILL (Illegal instruction)";
97 return "SIGFPE (Floating point exception)";
100 return "SIGBUS (Bus error)";
103 return "SIGINT (User interrupt)";
105 return "SIGTERM (Termination request)";
108 return "SIGPIPE (Broken pipe)";
112 return "SIGQUIT (User quit)";
116 return "SIGKILL (User kill process)";
120 return "SIGHUP (Terminal hangup)";
124 return "SIGBREAK (Break request)";
128 return "SIGABRT_COMPAT (Process abort compat)";
131 return "Unknown signal";
188 if(g_handling.exchange(
true, std::memory_order_acq_rel))
190 std::exit(128 + sig);
198 .signal_number = sig,
199 .signal_name = sig_name,
208 if(g_interrupt_handler)
210 try { g_interrupt_handler(sig_info); }
catch(...) {}
217 if(g_termination_handler)
219 try { g_termination_handler(sig_info); }
catch(...) {}
231 try { g_crash_handler(sig_info, trace); }
catch(...) {}
243 if(g_handling.exchange(
true, std::memory_order_acq_rel))
248 if(!g_exception_handler)
255 .exception_type =
"Unknown",
256 .exception_message =
"No active exception"
264 auto ptr = std::current_exception();
267 std::rethrow_exception(ptr);
271 exc_info.exception_message =
"Terminate called without an active exception";
274 catch(
const std::exception& e)
277 exc_info.exception_type = demangle_exception_type(e);
278 exc_info.exception_message =
"Terminate called after throwing " + exc_info.exception_type +
" : " + e.what();
281 trace = generate_stack_trace();
286 exc_info.exception_type =
"Unknown Exception Type";
287 exc_info.exception_message =
"Terminate called after throwing an unknown exception";
290 trace = generate_stack_trace();
296 g_exception_handler(exc_info, trace);
316 static std::atomic<bool> installed{
false};
317 if(installed.exchange(
true, std::memory_order_acq_rel))
365 std::signal(SIGPIPE, SIG_IGN);
void(*)(const signal_info &info) interrupt_handler_t
auto get_signal_name(int sig) noexcept -> std::string_view
auto set_interrupt_handler(interrupt_handler_t handler) -> void
void(*)(const signal_info &info) termination_handler_t
auto install_handlers(const crash_handlers &handlers) -> void
Install comprehensive crash handlers.
auto signal_handler(int sig) -> void
auto get_signal_type(int sig) noexcept -> signal_type
void(*)(const exception_info &info, const trace_info &trace) exception_handler_t
auto is_crash_signal(int sig) noexcept -> bool
auto set_termination_handler(termination_handler_t handler) -> void
void(*)(const signal_info &info, const trace_info &trace) crash_handler_t
void register_terminate_handler()
auto set_crash_handler(crash_handler_t handler) -> void
auto set_exception_handler(exception_handler_t handler) -> void
Exception information structure
Signal information structure.