crash_handler/
mac.rs

1mod ffi;
2mod signal;
3mod state;
4
5/// High level exception types
6///
7/// `exception_types.h`
8#[repr(i32)]
9pub enum ExceptionType {
10    /// Could not access memory. (SIGSEGV/SIGBUS)
11    ///
12    /// Code field contains `kern_return_t` describing error.
13    /// Subcode field contains bad memory address.
14    BadAccess = 1,
15    /// Instruction failed. (SIGILL)
16    ///
17    /// Illegal or undfined instruction or operand.
18    BadInstruction = 2,
19    /// Arithmetic exception (SIGFPE)
20    ///
21    /// Exact nature of the exception is in code field.
22    Arithmetic = 3,
23    /// Emulation instruction
24    ///
25    /// Emulation support instruction encountered
26    /// Details in code and subcode fields.
27    Emulation = 4,
28    /// Software generated exception
29    ///
30    /// Exaction exception is in the code field.
31    /// Codes 0 - 0xffff reserved to hardware.
32    /// Codes 0x10000 - 0x1ffff reserved for OS emulation (Unix)
33    Software = 5,
34    /// Trace, breakpoint, etc
35    ///
36    /// Details in the code field
37    Breakpoint = 6,
38    /// System calls
39    SysCall = 7,
40    /// Mach system calls
41    MachSysCall = 8,
42    /// RPC alert
43    RpcAlert = 9,
44    /// Abnormal process exit
45    Crash = 10,
46    /// Hit resource consumption limit
47    ///
48    /// Exact resource is in the code field.
49    Resource = 11,
50    /// Violated guarded resource protections
51    Guard = 12,
52    /// Abnormal process exited to corpse state
53    CorpseNotify = 13,
54}
55
56/// A Macos exception handler
57pub struct CrashHandler;
58
59#[allow(clippy::unused_self)]
60impl CrashHandler {
61    /// Attaches the exception handler.
62    ///
63    /// The provided callback will be invoked if an exception is caught,
64    /// providing a [`crate::CrashContext`] with the details of the thread where
65    /// the exception was thrown.
66    pub fn attach(on_crash: Box<dyn crate::CrashEvent>) -> Result<Self, crate::Error> {
67        state::attach(on_crash)?;
68        Ok(Self)
69    }
70
71    /// Detaches the handler.
72    ///
73    /// This is done automatically when [`CrashHandler`] is dropped.
74    #[inline]
75    pub fn detach(self) {
76        state::detach(false);
77    }
78
79    // Raises the specified user exception
80    #[inline]
81    pub fn simulate_exception(&self, exception_info: Option<crash_context::ExceptionInfo>) -> bool {
82        state::simulate_exception(exception_info)
83    }
84}
85
86impl Drop for CrashHandler {
87    fn drop(&mut self) {
88        state::detach(false);
89    }
90}