Skip to main content

calltrace_cleanup

Function calltrace_cleanup 

Source
#[unsafe(no_mangle)]
pub extern "C" fn calltrace_cleanup()
Expand description

Cleanup the CallTrace library and write final output

This function performs comprehensive cleanup of the CallTrace library:

  • Flushes all pending thread-local performance counters
  • Generates and writes the final JSON trace output
  • Restores original signal handlers
  • Releases all allocated resources

§Safety

This function is automatically called when the library is unloaded via dtor attributes. It handles cleanup gracefully even during abnormal program termination.

§Thread Safety

This function is designed to be signal-safe and can be called during:

  • Normal program exit
  • Library unloading
  • Signal handler execution
  • Thread destruction

§Output Generation

If CALLTRACE_OUTPUT was specified, this function will:

  1. Collect all call trees from all threads
  2. Generate metadata (timing, counters, environment)
  3. Serialize to structured JSON format
  4. Write atomically to the specified file

§Error Handling

Cleanup continues even if individual steps fail. Errors are logged to stderr but do not prevent other cleanup operations from completing.

§Examples

Usually called automatically, but can be invoked manually:

// In C code - force immediate cleanup and output
extern void calltrace_cleanup(void);
calltrace_cleanup();

§Performance Notes

  • Thread-local counter flushing: O(number of threads)
  • JSON generation: O(total function calls)
  • File I/O: O(output file size)
  • Signal handler restoration: O(number of signals)