use crate::arch;
use crate::{
traphandlers::{tls, CallThreadState},
VMRuntimeLimits,
};
use std::ops::ControlFlow;
#[derive(Debug)]
pub struct Backtrace(Vec<Frame>);
#[derive(Debug)]
pub struct Frame {
pc: usize,
fp: usize,
}
impl Frame {
pub fn pc(&self) -> usize {
self.pc
}
pub fn fp(&self) -> usize {
self.fp
}
}
impl Backtrace {
pub fn empty() -> Backtrace {
Backtrace(Vec::new())
}
pub fn new(limits: *const VMRuntimeLimits) -> Backtrace {
tls::with(|state| match state {
Some(state) => unsafe { Self::new_with_trap_state(limits, state, None) },
None => Backtrace(vec![]),
})
}
pub(crate) unsafe fn new_with_trap_state(
limits: *const VMRuntimeLimits,
state: &CallThreadState,
trap_pc_and_fp: Option<(usize, usize)>,
) -> Backtrace {
let mut frames = vec![];
Self::trace_with_trap_state(limits, state, trap_pc_and_fp, |frame| {
frames.push(frame);
ControlFlow::Continue(())
});
Backtrace(frames)
}
pub fn trace(limits: *const VMRuntimeLimits, f: impl FnMut(Frame) -> ControlFlow<()>) {
tls::with(|state| match state {
Some(state) => unsafe { Self::trace_with_trap_state(limits, state, None, f) },
None => {}
});
}
pub(crate) unsafe fn trace_with_trap_state(
limits: *const VMRuntimeLimits,
state: &CallThreadState,
trap_pc_and_fp: Option<(usize, usize)>,
mut f: impl FnMut(Frame) -> ControlFlow<()>,
) {
log::trace!("====== Capturing Backtrace ======");
let (last_wasm_exit_pc, last_wasm_exit_fp) = match trap_pc_and_fp {
Some((pc, fp)) => {
assert!(std::ptr::eq(limits, state.limits));
(pc, fp)
}
None => {
let pc = *(*limits).last_wasm_exit_pc.get();
let fp = *(*limits).last_wasm_exit_fp.get();
(pc, fp)
}
};
let activations = std::iter::once((
last_wasm_exit_pc,
last_wasm_exit_fp,
*(*limits).last_wasm_entry_sp.get(),
))
.chain(
state
.iter()
.filter(|state| std::ptr::eq(limits, state.limits))
.map(|state| {
(
state.old_last_wasm_exit_pc(),
state.old_last_wasm_exit_fp(),
state.old_last_wasm_entry_sp(),
)
}),
)
.take_while(|&(pc, fp, sp)| {
if pc == 0 {
debug_assert_eq!(fp, 0);
debug_assert_eq!(sp, 0);
}
pc != 0
});
for (pc, fp, sp) in activations {
if let ControlFlow::Break(()) = Self::trace_through_wasm(pc, fp, sp, &mut f) {
log::trace!("====== Done Capturing Backtrace (closure break) ======");
return;
}
}
log::trace!("====== Done Capturing Backtrace (reached end of activations) ======");
}
unsafe fn trace_through_wasm(
mut pc: usize,
mut fp: usize,
trampoline_sp: usize,
mut f: impl FnMut(Frame) -> ControlFlow<()>,
) -> ControlFlow<()> {
log::trace!("=== Tracing through contiguous sequence of Wasm frames ===");
log::trace!("trampoline_sp = 0x{:016x}", trampoline_sp);
log::trace!(" initial pc = 0x{:016x}", pc);
log::trace!(" initial fp = 0x{:016x}", fp);
assert_ne!(pc, 0);
assert_ne!(fp, 0);
assert_ne!(trampoline_sp, 0);
arch::assert_entry_sp_is_aligned(trampoline_sp);
loop {
assert!(trampoline_sp >= fp, "{trampoline_sp:#x} >= {fp:#x}");
arch::assert_fp_is_aligned(fp);
log::trace!("--- Tracing through one Wasm frame ---");
log::trace!("pc = {:p}", pc as *const ());
log::trace!("fp = {:p}", fp as *const ());
f(Frame { pc, fp })?;
pc = arch::get_next_older_pc_from_fp(fp);
assert_eq!(arch::NEXT_OLDER_FP_FROM_FP_OFFSET, 0);
let next_older_fp = *(fp as *mut usize).add(arch::NEXT_OLDER_FP_FROM_FP_OFFSET);
if arch::reached_entry_sp(next_older_fp, trampoline_sp) {
log::trace!("=== Done tracing contiguous sequence of Wasm frames ===");
return ControlFlow::Continue(());
}
assert!(next_older_fp > fp, "{next_older_fp:#x} > {fp:#x}");
fp = next_older_fp;
}
}
pub fn frames<'a>(
&'a self,
) -> impl ExactSizeIterator<Item = &'a Frame> + DoubleEndedIterator + 'a {
self.0.iter()
}
}