logicaffeine_forge/
segv_trace.rs1#[cfg(all(target_os = "linux", target_arch = "x86_64"))]
11pub fn install() {
12 use std::sync::Once;
13 static ONCE: Once = Once::new();
14 if std::env::var_os("LOGOS_SEGV_TRAP").is_none() {
15 return;
16 }
17 ONCE.call_once(|| unsafe {
18 let mut sa: libc::sigaction = std::mem::zeroed();
19 sa.sa_sigaction = handler as usize;
20 sa.sa_flags = libc::SA_SIGINFO;
21 libc::sigemptyset(&mut sa.sa_mask);
22 libc::sigaction(libc::SIGSEGV, &sa, std::ptr::null_mut());
23 libc::sigaction(libc::SIGBUS, &sa, std::ptr::null_mut());
24 });
25}
26
27#[cfg(not(all(target_os = "linux", target_arch = "x86_64")))]
28pub fn install() {}
29
30#[cfg(all(target_os = "linux", target_arch = "x86_64"))]
31extern "C" fn handler(sig: libc::c_int, info: *mut libc::siginfo_t, ucontext: *mut libc::c_void) {
32 unsafe {
33 let addr_ptr = (info as *const u8).add(16) as *const usize;
36 let fault_addr = addr_ptr.read_unaligned();
37 let uc = ucontext as *const u8;
41 let greg = |i: usize| (uc.add(40 + i * 8) as *const i64).read_unaligned();
42 let (rdi, rsi, rdx, rax, rcx, rip) =
43 (greg(8), greg(9), greg(12), greg(13), greg(14), greg(16));
44 let r8 = greg(0);
45 let r9 = greg(1);
46 let rbx = greg(11);
47 let mut code = [0u8; 16];
49 for (k, b) in code.iter_mut().enumerate() {
50 *b = (rip as *const u8).add(k).read();
51 }
52 let msg = format!(
53 "\n*** LOGOS_SEGV_TRAP: sig {sig} fault=0x{fault_addr:016x} rip=0x{rip:016x}\n rdi(base)=0x{rdi:016x} rsi(sp)=0x{rsi:016x} rax=0x{rax:016x} rdx=0x{rdx:016x} rcx=0x{rcx:016x}\n r8=0x{r8:016x} r9=0x{r9:016x} rbx=0x{rbx:016x}\n code@rip={code:02x?} ***\n"
54 );
55 libc::write(2, msg.as_ptr() as *const libc::c_void, msg.len());
56 let mut sa: libc::sigaction = std::mem::zeroed();
58 sa.sa_sigaction = libc::SIG_DFL;
59 libc::sigemptyset(&mut sa.sa_mask);
60 libc::sigaction(sig, &sa, std::ptr::null_mut());
61 libc::raise(sig);
62 }
63}