Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
kcenon::thread::crash_handler Class Reference

Thread-safe crash handler for the entire thread system. More...

#include <crash_handler.h>

Collaboration diagram for kcenon::thread::crash_handler:
Collaboration graph

Classes

struct  callback_entry
 
struct  cleanup_entry
 
struct  crash_stats
 Get crash statistics. More...
 

Public Member Functions

void initialize (crash_safety_level level=crash_safety_level::standard, bool enable_core_dumps=false)
 Initialize crash handling with specified safety level.
 
size_t register_crash_callback (const std::string &name, crash_callback callback, int priority=100)
 Register a callback to be called during crash handling.
 
void unregister_crash_callback (size_t registration_id)
 Unregister a crash callback.
 
void register_cleanup (const std::string &name, std::function< void()> cleanup, uint32_t timeout_ms=1000)
 Register a resource cleanup function.
 
void set_crash_log_directory (const std::string &directory)
 Set custom crash log directory.
 
void set_stack_trace_enabled (bool enable)
 Enable/disable automatic stack trace generation.
 
void trigger_crash_handling (const crash_context &context)
 Manually trigger crash handling (for testing)
 
bool is_handling_crash () const
 Check if crash handler is currently active.
 
crash_stats get_stats () const
 

Static Public Member Functions

static crash_handlerinstance ()
 Get the global crash handler instance.
 

Private Member Functions

 crash_handler ()=default
 
 ~crash_handler ()
 
 crash_handler (const crash_handler &)=delete
 
crash_handleroperator= (const crash_handler &)=delete
 
void handle_crash (int signal)
 
void execute_callbacks (const crash_context &context)
 
void execute_cleanups ()
 
std::string generate_stack_trace ()
 
void write_crash_log (const crash_context &context)
 

Static Private Member Functions

static void signal_handler (int signal)
 

Private Attributes

crash_safety_level safety_level_ = crash_safety_level::standard
 
bool enable_core_dumps_ = false
 
bool stack_trace_enabled_ = true
 
std::string crash_log_directory_ = "./crash_logs"
 
std::vector< callback_entrycallbacks_
 
std::vector< cleanup_entrycleanups_
 
std::mutex callbacks_mutex_
 
std::atomic< size_t > next_callback_id_ {1}
 
std::atomic< bool > initialized_ {false}
 
std::atomic< bool > handling_crash_ {false}
 
std::atomic< size_t > total_crashes_ {0}
 
std::atomic< size_t > successful_cleanups_ {0}
 
std::atomic< size_t > failed_cleanups_ {0}
 
std::chrono::system_clock::time_point last_crash_time_
 
struct sigaction previous_handlers_ [32]
 

Detailed Description

Thread-safe crash handler for the entire thread system.

Provides comprehensive crash safety mechanisms including:

  • Signal handling (SIGSEGV, SIGABRT, SIGFPE, etc.)
  • Stack trace generation
  • Graceful shutdown coordination
  • Resource cleanup callbacks
  • Cross-platform crash reporting
Examples
crash_protection/main.cpp.

Definition at line 62 of file crash_handler.h.

Constructor & Destructor Documentation

◆ crash_handler() [1/2]

kcenon::thread::crash_handler::crash_handler ( )
privatedefault

◆ ~crash_handler()

kcenon::thread::crash_handler::~crash_handler ( )
private

◆ crash_handler() [2/2]

kcenon::thread::crash_handler::crash_handler ( const crash_handler & )
privatedelete

Member Function Documentation

◆ execute_callbacks()

void kcenon::thread::crash_handler::execute_callbacks ( const crash_context & context)
private

◆ execute_cleanups()

void kcenon::thread::crash_handler::execute_cleanups ( )
private

◆ generate_stack_trace()

std::string kcenon::thread::crash_handler::generate_stack_trace ( )
private

◆ get_stats()

crash_stats kcenon::thread::crash_handler::get_stats ( ) const
Examples
crash_protection/main.cpp.

Referenced by main().

Here is the caller graph for this function:

◆ handle_crash()

void kcenon::thread::crash_handler::handle_crash ( int signal)
private

◆ initialize()

void kcenon::thread::crash_handler::initialize ( crash_safety_level level = crash_safety_level::standard,
bool enable_core_dumps = false )

Initialize crash handling with specified safety level.

Parameters
levelSafety level to configure
enable_core_dumpsWhether to enable core dump generation
Examples
crash_protection/main.cpp.

Referenced by main().

Here is the caller graph for this function:

◆ instance()

static crash_handler & kcenon::thread::crash_handler::instance ( )
static

Get the global crash handler instance.

Returns
Reference to the singleton crash handler

Referenced by main(), and kcenon::thread::scoped_crash_callback::~scoped_crash_callback().

Here is the caller graph for this function:

◆ is_handling_crash()

bool kcenon::thread::crash_handler::is_handling_crash ( ) const

Check if crash handler is currently active.

Returns
true if currently handling a crash

◆ operator=()

crash_handler & kcenon::thread::crash_handler::operator= ( const crash_handler & )
privatedelete

◆ register_cleanup()

void kcenon::thread::crash_handler::register_cleanup ( const std::string & name,
std::function< void()> cleanup,
uint32_t timeout_ms = 1000 )

Register a resource cleanup function.

Parameters
nameUnique name for cleanup
cleanupFunction to call for cleanup
timeout_msMaximum time to wait for cleanup
Examples
crash_protection/main.cpp.

Referenced by main().

Here is the caller graph for this function:

◆ register_crash_callback()

size_t kcenon::thread::crash_handler::register_crash_callback ( const std::string & name,
crash_callback callback,
int priority = 100 )

Register a callback to be called during crash handling.

Parameters
nameUnique name for the callback
callbackFunction to call during crash
priorityPriority order (lower numbers called first)
Returns
Registration ID for later removal
Examples
crash_protection/main.cpp.

Referenced by main().

Here is the caller graph for this function:

◆ set_crash_log_directory()

void kcenon::thread::crash_handler::set_crash_log_directory ( const std::string & directory)

Set custom crash log directory.

Parameters
directoryPath to directory for crash logs
Examples
crash_protection/main.cpp.

Referenced by main().

Here is the caller graph for this function:

◆ set_stack_trace_enabled()

void kcenon::thread::crash_handler::set_stack_trace_enabled ( bool enable)

Enable/disable automatic stack trace generation.

Parameters
enableWhether to generate stack traces

◆ signal_handler()

static void kcenon::thread::crash_handler::signal_handler ( int signal)
staticprivate

◆ trigger_crash_handling()

void kcenon::thread::crash_handler::trigger_crash_handling ( const crash_context & context)

Manually trigger crash handling (for testing)

Parameters
contextCustom crash context
Examples
crash_protection/main.cpp.

Referenced by main().

Here is the caller graph for this function:

◆ unregister_crash_callback()

void kcenon::thread::crash_handler::unregister_crash_callback ( size_t registration_id)

Unregister a crash callback.

Parameters
registration_idID returned from register_crash_callback
Examples
crash_protection/main.cpp.

Referenced by main(), and kcenon::thread::scoped_crash_callback::~scoped_crash_callback().

Here is the caller graph for this function:

◆ write_crash_log()

void kcenon::thread::crash_handler::write_crash_log ( const crash_context & context)
private

Member Data Documentation

◆ callbacks_

std::vector<callback_entry> kcenon::thread::crash_handler::callbacks_
private

Definition at line 176 of file crash_handler.h.

◆ callbacks_mutex_

std::mutex kcenon::thread::crash_handler::callbacks_mutex_
mutableprivate

Definition at line 178 of file crash_handler.h.

◆ cleanups_

std::vector<cleanup_entry> kcenon::thread::crash_handler::cleanups_
private

Definition at line 177 of file crash_handler.h.

◆ crash_log_directory_

std::string kcenon::thread::crash_handler::crash_log_directory_ = "./crash_logs"
private

Definition at line 173 of file crash_handler.h.

◆ enable_core_dumps_

bool kcenon::thread::crash_handler::enable_core_dumps_ = false
private

Definition at line 171 of file crash_handler.h.

◆ failed_cleanups_

std::atomic<size_t> kcenon::thread::crash_handler::failed_cleanups_ {0}
private

Definition at line 186 of file crash_handler.h.

186{0};

◆ handling_crash_

std::atomic<bool> kcenon::thread::crash_handler::handling_crash_ {false}
private

Definition at line 183 of file crash_handler.h.

183{false};

◆ initialized_

std::atomic<bool> kcenon::thread::crash_handler::initialized_ {false}
private

Definition at line 182 of file crash_handler.h.

182{false};

◆ last_crash_time_

std::chrono::system_clock::time_point kcenon::thread::crash_handler::last_crash_time_
private

Definition at line 187 of file crash_handler.h.

◆ next_callback_id_

std::atomic<size_t> kcenon::thread::crash_handler::next_callback_id_ {1}
private

Definition at line 179 of file crash_handler.h.

179{1};

◆ previous_handlers_

struct sigaction kcenon::thread::crash_handler::previous_handlers_[32]
private

Definition at line 193 of file crash_handler.h.

◆ safety_level_

crash_safety_level kcenon::thread::crash_handler::safety_level_ = crash_safety_level::standard
private

Definition at line 170 of file crash_handler.h.

◆ stack_trace_enabled_

bool kcenon::thread::crash_handler::stack_trace_enabled_ = true
private

Definition at line 172 of file crash_handler.h.

◆ successful_cleanups_

std::atomic<size_t> kcenon::thread::crash_handler::successful_cleanups_ {0}
private

Definition at line 185 of file crash_handler.h.

185{0};

◆ total_crashes_

std::atomic<size_t> kcenon::thread::crash_handler::total_crashes_ {0}
private

Definition at line 184 of file crash_handler.h.

184{0};

The documentation for this class was generated from the following file: