Skip to content
Permalink
Browse files

std: Reduce checks for `feature = "backtrace"`

This is a stylistic change to libstd to reduce the number of checks of
`feature = "backtrace"` now that we unconditionally depend on the
`backtrace` crate and rely on it having an empty implementation.
otherwise.
  • Loading branch information...
alexcrichton committed Sep 25, 2019
1 parent 6ef275e commit 1d06058a77beffb6347c77b51e12889b7bd9fc76
Showing with 50 additions and 50 deletions.
  1. +15 −19 src/libstd/panicking.rs
  2. +0 −3 src/libstd/rt.rs
  3. +35 −25 src/libstd/sys_common/backtrace.rs
  4. +0 −3 src/libstd/thread/mod.rs
@@ -15,9 +15,11 @@ use crate::intrinsics;
use crate::mem;
use crate::ptr;
use crate::raw;
use crate::sync::atomic::{AtomicBool, Ordering};
use crate::sys::stdio::panic_output;
use crate::sys_common::rwlock::RWLock;
use crate::sys_common::{thread_info, util, backtrace};
use crate::sys_common::{thread_info, util};
use crate::sys_common::backtrace::{self, RustBacktrace};
use crate::thread;

#[cfg(not(test))]
@@ -158,16 +160,10 @@ pub fn take_hook() -> Box<dyn Fn(&PanicInfo<'_>) + 'static + Sync + Send> {
fn default_hook(info: &PanicInfo<'_>) {
// If this is a double panic, make sure that we print a backtrace
// for this panic. Otherwise only print it if logging is enabled.
let log_backtrace = if cfg!(feature = "backtrace") {
let panics = update_panic_count(0);

if panics >= 2 {
Some(backtrace_rs::PrintFmt::Full)
} else {
backtrace::log_enabled()
}
let backtrace_env = if update_panic_count(0) >= 2 {
RustBacktrace::Print(backtrace_rs::PrintFmt::Full)
} else {
None
backtrace::rust_backtrace_env()
};

// The current implementation always returns `Some`.
@@ -187,16 +183,16 @@ fn default_hook(info: &PanicInfo<'_>) {
let _ = writeln!(err, "thread '{}' panicked at '{}', {}",
name, msg, location);

if cfg!(feature = "backtrace") {
use crate::sync::atomic::{AtomicBool, Ordering};

static FIRST_PANIC: AtomicBool = AtomicBool::new(true);
static FIRST_PANIC: AtomicBool = AtomicBool::new(true);

if let Some(format) = log_backtrace {
let _ = backtrace::print(err, format);
} else if FIRST_PANIC.compare_and_swap(true, false, Ordering::SeqCst) {
let _ = writeln!(err, "note: run with `RUST_BACKTRACE=1` \
environment variable to display a backtrace.");
match backtrace_env {
RustBacktrace::Print(format) => drop(backtrace::print(err, format)),
RustBacktrace::Disabled => {}
RustBacktrace::RuntimeDisabled => {
if FIRST_PANIC.swap(false, Ordering::SeqCst) {
let _ = writeln!(err, "note: run with `RUST_BACKTRACE=1` \
environment variable to display a backtrace.");
}
}
}
};
@@ -44,12 +44,9 @@ fn lang_start_internal(main: &(dyn Fn() -> i32 + Sync + crate::panic::RefUnwindS
sys::args::init(argc, argv);

// Let's run some code!
#[cfg(feature = "backtrace")]
let exit_code = panic::catch_unwind(|| {
sys_common::backtrace::__rust_begin_short_backtrace(move || main())
});
#[cfg(not(feature = "backtrace"))]
let exit_code = panic::catch_unwind(move || main());

sys_common::cleanup();
exit_code.unwrap_or(101) as isize
@@ -7,6 +7,7 @@ use crate::io;
use crate::borrow::Cow;
use crate::io::prelude::*;
use crate::path::{self, Path, PathBuf};
use crate::sync::atomic::{self, Ordering};
use crate::sys::mutex::Mutex;

use backtrace_rs::{BacktraceFmt, BytesOrWideString, PrintFmt};
@@ -115,8 +116,10 @@ unsafe fn _print_fmt(fmt: &mut fmt::Formatter<'_>, print_fmt: PrintFmt) -> fmt::
Ok(())
}

/// Fixed frame used to clean the backtrace with `RUST_BACKTRACE=1`.
#[inline(never)]
/// Fixed frame used to clean the backtrace with `RUST_BACKTRACE=1`. Note that
/// this is only inline(never) when backtraces in libstd are enabled, otherwise
/// it's fine to optimize away.
#[cfg_attr(feature = "backtrace", inline(never))]
pub fn __rust_begin_short_backtrace<F, T>(f: F) -> T
where
F: FnOnce() -> T,
@@ -126,42 +129,49 @@ where
f()
}

pub enum RustBacktrace {
Print(PrintFmt),
Disabled,
RuntimeDisabled,
}

// 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.
pub fn log_enabled() -> Option<PrintFmt> {
use crate::sync::atomic::{self, Ordering};
pub fn rust_backtrace_env() -> RustBacktrace {
// If the `backtrace` feature of this crate isn't enabled quickly return
// `None` so this can be constant propagated all over the place to turn
// optimize away callers.
if !cfg!(feature = "backtrace") {
return RustBacktrace::Disabled;
}

// Setting environment variables for Fuchsia components isn't a standard
// or easily supported workflow. For now, always display backtraces.
if cfg!(target_os = "fuchsia") {
return Some(PrintFmt::Full);
return RustBacktrace::Print(PrintFmt::Full);
}

static ENABLED: atomic::AtomicIsize = atomic::AtomicIsize::new(0);
match ENABLED.load(Ordering::SeqCst) {
0 => {}
1 => return None,
2 => return Some(PrintFmt::Short),
_ => return Some(PrintFmt::Full),
1 => return RustBacktrace::RuntimeDisabled,
2 => return RustBacktrace::Print(PrintFmt::Short),
_ => return RustBacktrace::Print(PrintFmt::Full),
}

let val = env::var_os("RUST_BACKTRACE").and_then(|x| {
if &x == "0" {
None
} else if &x == "full" {
Some(PrintFmt::Full)
} else {
Some(PrintFmt::Short)
}
});
ENABLED.store(
match val {
Some(v) => v as isize,
None => 1,
},
Ordering::SeqCst,
);
val
let (format, cache) = env::var_os("RUST_BACKTRACE")
.map(|x| {
if &x == "0" {
(RustBacktrace::RuntimeDisabled, 1)
} else if &x == "full" {
(RustBacktrace::Print(PrintFmt::Full), 3)
} else {
(RustBacktrace::Print(PrintFmt::Short), 2)
}
})
.unwrap_or((RustBacktrace::RuntimeDisabled, 1));
ENABLED.store(cache, Ordering::SeqCst);
format
}

/// Prints the filename of the backtrace frame.
@@ -465,12 +465,9 @@ impl Builder {
}

thread_info::set(imp::guard::current(), their_thread);
#[cfg(feature = "backtrace")]
let try_result = panic::catch_unwind(panic::AssertUnwindSafe(|| {
crate::sys_common::backtrace::__rust_begin_short_backtrace(f)
}));
#[cfg(not(feature = "backtrace"))]
let try_result = panic::catch_unwind(panic::AssertUnwindSafe(f));
*their_packet.get() = Some(try_result);
};

0 comments on commit 1d06058

Please sign in to comment.
You can’t perform that action at this time.