diff --git a/Cargo.lock b/Cargo.lock index e0d30647809a9..8c12f8b769ba5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -110,8 +110,6 @@ dependencies = [ [[package]] name = "backtrace" version = "0.3.35" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1371048253fa3bac6704bfd6bbfc922ee9bdcee8881330d40f308b81cc5adc55" dependencies = [ "backtrace-sys", "cfg-if", diff --git a/Cargo.toml b/Cargo.toml index a242f090fbc07..d933b8307fa52 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -69,6 +69,8 @@ rustc-workspace-hack = { path = 'src/tools/rustc-workspace-hack' } rustc-std-workspace-core = { path = 'src/tools/rustc-std-workspace-core' } rustc-std-workspace-alloc = { path = 'src/tools/rustc-std-workspace-alloc' } rustc-std-workspace-std = { path = 'src/tools/rustc-std-workspace-std' } +backtrace = { path = "../backtrace-rs" } +# backtrace-sys = { path = "../backtrace-rs/crates/backtrace-sys" } [patch."https://github.com/rust-lang/rust-clippy"] clippy_lints = { path = "src/tools/clippy/clippy_lints" } diff --git a/src/libstd/sys_common/backtrace.rs b/src/libstd/sys_common/backtrace.rs index bf37ff7ddbd3a..045b04072723b 100644 --- a/src/libstd/sys_common/backtrace.rs +++ b/src/libstd/sys_common/backtrace.rs @@ -1,3 +1,4 @@ +#![cfg_attr(target_os = "fuchsia", allow(unused_imports))] /// Common code for printing the backtrace in the same way across the different /// supported platforms. @@ -12,9 +13,11 @@ use crate::sys::mutex::Mutex; use backtrace::{BytesOrWideString, Frame, Symbol}; -pub const HEX_WIDTH: usize = 2 + 2 * mem::size_of::(); +#[cfg(not(target_os = "fuchsia"))] +const HEX_WIDTH: usize = 2 + 2 * mem::size_of::(); /// Max number of frames to print. +#[cfg(not(target_os = "fuchsia"))] const MAX_NB_FRAMES: usize = 100; /// Prints the current backtrace. @@ -39,6 +42,7 @@ pub fn print(w: &mut dyn Write, format: PrintFormat) -> io::Result<()> { } } +#[cfg(not(target_os = "fuchsia"))] fn _print(w: &mut dyn Write, format: PrintFormat) -> io::Result<()> { writeln!(w, "stack backtrace:")?; @@ -66,6 +70,30 @@ fn _print(w: &mut dyn Write, format: PrintFormat) -> io::Result<()> { Ok(()) } +#[cfg(target_os = "fuchsia")] +fn _print(w: &mut dyn Write, format: PrintFormat) -> io::Result<()> { + let _ = format; + + struct DisplayBacktrace; + impl crate::fmt::Display for DisplayBacktrace { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + let mut fb = backtrace::FuchsiaBacktraceFmt::new(f); + fb.add_context()?; + let mut res = Ok(()); + unsafe { + backtrace::trace_unsynchronized(|frame| { + res = fb.add_frame(frame.ip()); + res.is_ok() + }); + } + res?; + fb.finish() + } + } + + write!(w, "stack backtrace:\n{}", DisplayBacktrace) +} + /// Fixed frame used to clean the backtrace with `RUST_BACKTRACE=1`. #[inline(never)] pub fn __rust_begin_short_backtrace(f: F) -> T @@ -81,6 +109,7 @@ where #[derive(Debug, Copy, Clone, Eq, PartialEq)] pub enum PrintFormat { /// Show only relevant data from the backtrace. + #[cfg_attr(target_os = "fuchsia", allow(unused))] Short = 2, /// Show all the frames with absolute path for files. Full = 3, @@ -88,6 +117,7 @@ pub enum PrintFormat { // For now logging is turned off by default, and this function checks to see // whether the magical environment variable is present to see if it's turned on. +#[cfg(not(target_os = "fuchsia"))] pub fn log_enabled() -> Option { static ENABLED: atomic::AtomicIsize = atomic::AtomicIsize::new(0); match ENABLED.load(Ordering::SeqCst) { @@ -116,6 +146,15 @@ pub fn log_enabled() -> Option { val } +// Configuring logging via environment variables doesn't make sense for +// Fuchsia components, which don't inherit environment variables. +// For now, leave this always enabled. +#[cfg(target_os = "fuchsia")] +pub fn log_enabled() -> Option { + Some(PrintFormat::Full) +} + +#[cfg(not(target_os = "fuchsia"))] struct Printer<'a, 'b> { format: PrintFormat, done: bool, @@ -124,6 +163,7 @@ struct Printer<'a, 'b> { out: &'a mut (dyn Write + 'b), } +#[cfg(not(target_os = "fuchsia"))] impl<'a, 'b> Printer<'a, 'b> { fn new(format: PrintFormat, out: &'a mut (dyn Write + 'b)) -> Printer<'a, 'b> { Printer { format, done: false, skipped: false, idx: 0, out }