Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom backtrace output for Fuchsia #64133

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
42 changes: 41 additions & 1 deletion src/libstd/sys_common/backtrace.rs
Original file line number Diff line number Diff line change
@@ -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.

Expand All @@ -12,9 +13,11 @@ use crate::sys::mutex::Mutex;

use backtrace::{BytesOrWideString, Frame, Symbol};

pub const HEX_WIDTH: usize = 2 + 2 * mem::size_of::<usize>();
#[cfg(not(target_os = "fuchsia"))]
const HEX_WIDTH: usize = 2 + 2 * mem::size_of::<usize>();

/// Max number of frames to print.
#[cfg(not(target_os = "fuchsia"))]
const MAX_NB_FRAMES: usize = 100;

/// Prints the current backtrace.
Expand All @@ -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:")?;

Expand Down Expand Up @@ -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, T>(f: F) -> T
Expand All @@ -81,13 +109,15 @@ 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,
}

// 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<PrintFormat> {
static ENABLED: atomic::AtomicIsize = atomic::AtomicIsize::new(0);
match ENABLED.load(Ordering::SeqCst) {
Expand Down Expand Up @@ -116,6 +146,15 @@ pub fn log_enabled() -> Option<PrintFormat> {
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<PrintFormat> {
Some(PrintFormat::Full)
}

#[cfg(not(target_os = "fuchsia"))]
struct Printer<'a, 'b> {
format: PrintFormat,
done: bool,
Expand All @@ -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 }
Expand Down