From 5264711b5d385fbdd6f58f19190c454bee733ece Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 4 Jun 2019 00:25:43 +0300 Subject: add couple of debug utils --- crates/ra_prof/Cargo.toml | 1 + crates/ra_prof/src/lib.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) (limited to 'crates/ra_prof') diff --git a/crates/ra_prof/Cargo.toml b/crates/ra_prof/Cargo.toml index efcce3d65..d55af18f5 100644 --- a/crates/ra_prof/Cargo.toml +++ b/crates/ra_prof/Cargo.toml @@ -8,3 +8,4 @@ publish = false [dependencies] once_cell = "0.2.0" itertools = "0.8.0" +backtrace = "0.3.28" diff --git a/crates/ra_prof/src/lib.rs b/crates/ra_prof/src/lib.rs index e681b8e0b..6ec24d86d 100644 --- a/crates/ra_prof/src/lib.rs +++ b/crates/ra_prof/src/lib.rs @@ -225,6 +225,36 @@ fn print(lvl: usize, msgs: &[Message], out: &mut impl Write, longer_than: Durati } } +/// Prints backtrace to stderr, useful for debugging. +pub fn print_backtrace() { + let bt = backtrace::Backtrace::new(); + eprintln!("{:?}", bt); +} + +thread_local!(static IN_SCOPE: RefCell = RefCell::new(false)); + +/// Allows to check if the current code is withing some dynamic scope, can be +/// useful during debugging to figure out why a function is called. +pub struct Scope { + _hidden: (), +} + +impl Scope { + pub fn enter() -> Scope { + IN_SCOPE.with(|slot| *slot.borrow_mut() = true); + Scope { _hidden: () } + } + pub fn is_active() -> bool { + IN_SCOPE.with(|slot| *slot.borrow()) + } +} + +impl Drop for Scope { + fn drop(&mut self) { + IN_SCOPE.with(|slot| *slot.borrow_mut() = false); + } +} + #[cfg(test)] mod tests { use super::*; -- cgit v1.2.3