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.
This commit is contained in:
parent
6ef275e6c3
commit
1d06058a77
4 changed files with 50 additions and 50 deletions
|
@ -15,9 +15,11 @@ use crate::intrinsics;
|
||||||
use crate::mem;
|
use crate::mem;
|
||||||
use crate::ptr;
|
use crate::ptr;
|
||||||
use crate::raw;
|
use crate::raw;
|
||||||
|
use crate::sync::atomic::{AtomicBool, Ordering};
|
||||||
use crate::sys::stdio::panic_output;
|
use crate::sys::stdio::panic_output;
|
||||||
use crate::sys_common::rwlock::RWLock;
|
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;
|
use crate::thread;
|
||||||
|
|
||||||
#[cfg(not(test))]
|
#[cfg(not(test))]
|
||||||
|
@ -158,16 +160,10 @@ pub fn take_hook() -> Box<dyn Fn(&PanicInfo<'_>) + 'static + Sync + Send> {
|
||||||
fn default_hook(info: &PanicInfo<'_>) {
|
fn default_hook(info: &PanicInfo<'_>) {
|
||||||
// If this is a double panic, make sure that we print a backtrace
|
// If this is a double panic, make sure that we print a backtrace
|
||||||
// for this panic. Otherwise only print it if logging is enabled.
|
// for this panic. Otherwise only print it if logging is enabled.
|
||||||
let log_backtrace = if cfg!(feature = "backtrace") {
|
let backtrace_env = if update_panic_count(0) >= 2 {
|
||||||
let panics = update_panic_count(0);
|
RustBacktrace::Print(backtrace_rs::PrintFmt::Full)
|
||||||
|
|
||||||
if panics >= 2 {
|
|
||||||
Some(backtrace_rs::PrintFmt::Full)
|
|
||||||
} else {
|
|
||||||
backtrace::log_enabled()
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
None
|
backtrace::rust_backtrace_env()
|
||||||
};
|
};
|
||||||
|
|
||||||
// The current implementation always returns `Some`.
|
// The current implementation always returns `Some`.
|
||||||
|
@ -187,16 +183,16 @@ fn default_hook(info: &PanicInfo<'_>) {
|
||||||
let _ = writeln!(err, "thread '{}' panicked at '{}', {}",
|
let _ = writeln!(err, "thread '{}' panicked at '{}', {}",
|
||||||
name, msg, location);
|
name, msg, location);
|
||||||
|
|
||||||
if cfg!(feature = "backtrace") {
|
static FIRST_PANIC: AtomicBool = AtomicBool::new(true);
|
||||||
use crate::sync::atomic::{AtomicBool, Ordering};
|
|
||||||
|
|
||||||
static FIRST_PANIC: AtomicBool = AtomicBool::new(true);
|
match backtrace_env {
|
||||||
|
RustBacktrace::Print(format) => drop(backtrace::print(err, format)),
|
||||||
if let Some(format) = log_backtrace {
|
RustBacktrace::Disabled => {}
|
||||||
let _ = backtrace::print(err, format);
|
RustBacktrace::RuntimeDisabled => {
|
||||||
} else if FIRST_PANIC.compare_and_swap(true, false, Ordering::SeqCst) {
|
if FIRST_PANIC.swap(false, Ordering::SeqCst) {
|
||||||
let _ = writeln!(err, "note: run with `RUST_BACKTRACE=1` \
|
let _ = writeln!(err, "note: run with `RUST_BACKTRACE=1` \
|
||||||
environment variable to display a backtrace.");
|
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);
|
sys::args::init(argc, argv);
|
||||||
|
|
||||||
// Let's run some code!
|
// Let's run some code!
|
||||||
#[cfg(feature = "backtrace")]
|
|
||||||
let exit_code = panic::catch_unwind(|| {
|
let exit_code = panic::catch_unwind(|| {
|
||||||
sys_common::backtrace::__rust_begin_short_backtrace(move || main())
|
sys_common::backtrace::__rust_begin_short_backtrace(move || main())
|
||||||
});
|
});
|
||||||
#[cfg(not(feature = "backtrace"))]
|
|
||||||
let exit_code = panic::catch_unwind(move || main());
|
|
||||||
|
|
||||||
sys_common::cleanup();
|
sys_common::cleanup();
|
||||||
exit_code.unwrap_or(101) as isize
|
exit_code.unwrap_or(101) as isize
|
||||||
|
|
|
@ -7,6 +7,7 @@ use crate::io;
|
||||||
use crate::borrow::Cow;
|
use crate::borrow::Cow;
|
||||||
use crate::io::prelude::*;
|
use crate::io::prelude::*;
|
||||||
use crate::path::{self, Path, PathBuf};
|
use crate::path::{self, Path, PathBuf};
|
||||||
|
use crate::sync::atomic::{self, Ordering};
|
||||||
use crate::sys::mutex::Mutex;
|
use crate::sys::mutex::Mutex;
|
||||||
|
|
||||||
use backtrace_rs::{BacktraceFmt, BytesOrWideString, PrintFmt};
|
use backtrace_rs::{BacktraceFmt, BytesOrWideString, PrintFmt};
|
||||||
|
@ -115,8 +116,10 @@ unsafe fn _print_fmt(fmt: &mut fmt::Formatter<'_>, print_fmt: PrintFmt) -> fmt::
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Fixed frame used to clean the backtrace with `RUST_BACKTRACE=1`.
|
/// Fixed frame used to clean the backtrace with `RUST_BACKTRACE=1`. Note that
|
||||||
#[inline(never)]
|
/// 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
|
pub fn __rust_begin_short_backtrace<F, T>(f: F) -> T
|
||||||
where
|
where
|
||||||
F: FnOnce() -> T,
|
F: FnOnce() -> T,
|
||||||
|
@ -126,42 +129,49 @@ where
|
||||||
f()
|
f()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub enum RustBacktrace {
|
||||||
|
Print(PrintFmt),
|
||||||
|
Disabled,
|
||||||
|
RuntimeDisabled,
|
||||||
|
}
|
||||||
|
|
||||||
// For now logging is turned off by default, and this function checks to see
|
// 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.
|
// whether the magical environment variable is present to see if it's turned on.
|
||||||
pub fn log_enabled() -> Option<PrintFmt> {
|
pub fn rust_backtrace_env() -> RustBacktrace {
|
||||||
use crate::sync::atomic::{self, Ordering};
|
// 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
|
// Setting environment variables for Fuchsia components isn't a standard
|
||||||
// or easily supported workflow. For now, always display backtraces.
|
// or easily supported workflow. For now, always display backtraces.
|
||||||
if cfg!(target_os = "fuchsia") {
|
if cfg!(target_os = "fuchsia") {
|
||||||
return Some(PrintFmt::Full);
|
return RustBacktrace::Print(PrintFmt::Full);
|
||||||
}
|
}
|
||||||
|
|
||||||
static ENABLED: atomic::AtomicIsize = atomic::AtomicIsize::new(0);
|
static ENABLED: atomic::AtomicIsize = atomic::AtomicIsize::new(0);
|
||||||
match ENABLED.load(Ordering::SeqCst) {
|
match ENABLED.load(Ordering::SeqCst) {
|
||||||
0 => {}
|
0 => {}
|
||||||
1 => return None,
|
1 => return RustBacktrace::RuntimeDisabled,
|
||||||
2 => return Some(PrintFmt::Short),
|
2 => return RustBacktrace::Print(PrintFmt::Short),
|
||||||
_ => return Some(PrintFmt::Full),
|
_ => return RustBacktrace::Print(PrintFmt::Full),
|
||||||
}
|
}
|
||||||
|
|
||||||
let val = env::var_os("RUST_BACKTRACE").and_then(|x| {
|
let (format, cache) = env::var_os("RUST_BACKTRACE")
|
||||||
if &x == "0" {
|
.map(|x| {
|
||||||
None
|
if &x == "0" {
|
||||||
} else if &x == "full" {
|
(RustBacktrace::RuntimeDisabled, 1)
|
||||||
Some(PrintFmt::Full)
|
} else if &x == "full" {
|
||||||
} else {
|
(RustBacktrace::Print(PrintFmt::Full), 3)
|
||||||
Some(PrintFmt::Short)
|
} else {
|
||||||
}
|
(RustBacktrace::Print(PrintFmt::Short), 2)
|
||||||
});
|
}
|
||||||
ENABLED.store(
|
})
|
||||||
match val {
|
.unwrap_or((RustBacktrace::RuntimeDisabled, 1));
|
||||||
Some(v) => v as isize,
|
ENABLED.store(cache, Ordering::SeqCst);
|
||||||
None => 1,
|
format
|
||||||
},
|
|
||||||
Ordering::SeqCst,
|
|
||||||
);
|
|
||||||
val
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Prints the filename of the backtrace frame.
|
/// Prints the filename of the backtrace frame.
|
||||||
|
|
|
@ -465,12 +465,9 @@ impl Builder {
|
||||||
}
|
}
|
||||||
|
|
||||||
thread_info::set(imp::guard::current(), their_thread);
|
thread_info::set(imp::guard::current(), their_thread);
|
||||||
#[cfg(feature = "backtrace")]
|
|
||||||
let try_result = panic::catch_unwind(panic::AssertUnwindSafe(|| {
|
let try_result = panic::catch_unwind(panic::AssertUnwindSafe(|| {
|
||||||
crate::sys_common::backtrace::__rust_begin_short_backtrace(f)
|
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);
|
*their_packet.get() = Some(try_result);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue