use super::ring::{Event, MAX_FRAMES};
#[inline(never)]
pub(super) fn fill(event: &mut Event) {
event.frame_count = frames(&mut event.frames, 2) as u8;
event.thread_len = thread_name(&mut event.thread) as u8;
}
#[cfg(unix)]
mod unwind {
use std::ffi::c_void;
use std::os::raw::c_int;
#[repr(C)]
pub(super) struct Context {
_private: [u8; 0],
}
pub(super) type Trace = extern "C" fn(*mut Context, *mut c_void) -> c_int;
pub(super) const NO_REASON: c_int = 0;
pub(super) const FAILURE: c_int = 9;
extern "C" {
pub(super) fn _Unwind_Backtrace(trace: Trace, arg: *mut c_void) -> c_int;
pub(super) fn _Unwind_GetIP(context: *mut Context) -> usize;
}
}
#[cfg(unix)]
struct Collector<'a> {
frames: &'a mut [usize; MAX_FRAMES],
count: usize,
skip: usize,
}
#[cfg(unix)]
extern "C" fn collect(
context: *mut unwind::Context,
arg: *mut std::ffi::c_void,
) -> std::os::raw::c_int {
let collector = unsafe { &mut *arg.cast::<Collector<'_>>() };
let ip = unsafe { unwind::_Unwind_GetIP(context) };
if ip == 0 || collector.count >= collector.frames.len() {
return unwind::FAILURE;
}
if collector.skip > 0 {
collector.skip -= 1;
return unwind::NO_REASON;
}
collector.frames[collector.count] = ip;
collector.count += 1;
unwind::NO_REASON
}
#[cfg(unix)]
#[inline(never)]
fn frames(frames: &mut [usize; MAX_FRAMES], skip: usize) -> usize {
let mut collector = Collector {
frames,
count: 0,
skip: skip + 1,
};
unsafe {
unwind::_Unwind_Backtrace(collect, (&mut collector as *mut Collector<'_>).cast());
}
collector.count
}
#[cfg(not(unix))]
fn frames(_frames: &mut [usize; MAX_FRAMES], _skip: usize) -> usize {
0
}
#[cfg(target_os = "macos")]
fn thread_name(out: &mut [u8; 32]) -> usize {
let status = unsafe {
libc::pthread_getname_np(libc::pthread_self(), out.as_mut_ptr().cast(), out.len())
};
if status != 0 {
return 0;
}
out.iter().position(|&byte| byte == 0).unwrap_or(out.len())
}
#[cfg(target_os = "linux")]
fn thread_name(out: &mut [u8; 32]) -> usize {
let status = unsafe { libc::prctl(libc::PR_GET_NAME, out.as_mut_ptr()) };
if status != 0 {
return 0;
}
out.iter().position(|&byte| byte == 0).unwrap_or(out.len())
}
#[cfg(not(any(target_os = "macos", target_os = "linux")))]
fn thread_name(_out: &mut [u8; 32]) -> usize {
0
}