2016-12-04 16:38:27 -05:00
|
|
|
/// Common code for printing the backtrace in the same way across the different
|
|
|
|
/// supported platforms.
|
|
|
|
|
2019-02-11 04:23:21 +09:00
|
|
|
use crate::env;
|
2019-09-04 12:14:34 -07:00
|
|
|
use crate::fmt;
|
2019-02-11 04:23:21 +09:00
|
|
|
use crate::io;
|
2019-05-15 07:30:15 -07:00
|
|
|
use crate::io::prelude::*;
|
2019-02-11 04:23:21 +09:00
|
|
|
use crate::path::{self, Path};
|
|
|
|
use crate::sync::atomic::{self, Ordering};
|
|
|
|
use crate::sys::mutex::Mutex;
|
|
|
|
|
2019-09-04 12:14:34 -07:00
|
|
|
use backtrace::{BacktraceFmt, BytesOrWideString, PrintFmt};
|
2016-12-04 16:38:27 -05:00
|
|
|
|
|
|
|
/// Max number of frames to print.
|
|
|
|
const MAX_NB_FRAMES: usize = 100;
|
|
|
|
|
|
|
|
/// Prints the current backtrace.
|
2019-09-04 12:14:34 -07:00
|
|
|
pub fn print(w: &mut dyn Write, format: PrintFmt) -> io::Result<()> {
|
2016-12-04 16:38:27 -05:00
|
|
|
static LOCK: Mutex = Mutex::new();
|
|
|
|
|
2018-12-14 16:47:18 -08:00
|
|
|
// There are issues currently linking libbacktrace into tests, and in
|
|
|
|
// general during libstd's own unit tests we're not testing this path. In
|
|
|
|
// test mode immediately return here to optimize away any references to the
|
|
|
|
// libbacktrace symbols
|
|
|
|
if cfg!(test) {
|
2019-05-15 07:30:15 -07:00
|
|
|
return Ok(());
|
2018-12-14 16:47:18 -08:00
|
|
|
}
|
|
|
|
|
2016-12-04 16:38:27 -05:00
|
|
|
// Use a lock to prevent mixed output in multithreading context.
|
|
|
|
// Some platforms also requires it, like `SymFromAddr` on Windows.
|
|
|
|
unsafe {
|
|
|
|
LOCK.lock();
|
|
|
|
let res = _print(w, format);
|
|
|
|
LOCK.unlock();
|
|
|
|
res
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-04 12:14:34 -07:00
|
|
|
fn _print(w: &mut dyn Write, format: PrintFmt) -> io::Result<()> {
|
|
|
|
struct DisplayBacktrace {
|
|
|
|
format: PrintFmt,
|
|
|
|
}
|
|
|
|
impl fmt::Display for DisplayBacktrace {
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
_print_fmt(fmt, self.format)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
write!(w, "{}", DisplayBacktrace { format })
|
|
|
|
}
|
2016-12-04 16:38:27 -05:00
|
|
|
|
2019-09-04 12:14:34 -07:00
|
|
|
fn _print_fmt(fmt: &mut fmt::Formatter<'_>, print_fmt: PrintFmt) -> fmt::Result {
|
|
|
|
let mut print_path = move |fmt: &mut fmt::Formatter<'_>, bows: BytesOrWideString<'_>| {
|
|
|
|
output_filename(fmt, bows, print_fmt)
|
|
|
|
};
|
|
|
|
let mut bt_fmt = BacktraceFmt::new(fmt, print_fmt, &mut print_path);
|
|
|
|
bt_fmt.add_context()?;
|
|
|
|
let mut skipped = false;
|
2019-05-15 07:30:15 -07:00
|
|
|
unsafe {
|
2019-09-04 12:14:34 -07:00
|
|
|
let mut idx = 0;
|
|
|
|
let mut res = Ok(());
|
2019-05-15 07:30:15 -07:00
|
|
|
backtrace::trace_unsynchronized(|frame| {
|
2019-09-04 12:14:34 -07:00
|
|
|
if print_fmt == PrintFmt::Short && idx > MAX_NB_FRAMES {
|
|
|
|
skipped = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-15 07:30:15 -07:00
|
|
|
let mut hit = false;
|
2019-09-04 12:14:34 -07:00
|
|
|
let mut stop = false;
|
2019-05-15 07:30:15 -07:00
|
|
|
backtrace::resolve_frame_unsynchronized(frame, |symbol| {
|
|
|
|
hit = true;
|
2019-09-04 12:14:34 -07:00
|
|
|
if print_fmt == PrintFmt::Short {
|
|
|
|
if let Some(sym) = symbol.name().and_then(|s| s.as_str()) {
|
|
|
|
if sym.contains("__rust_begin_short_backtrace") {
|
|
|
|
skipped = true;
|
|
|
|
stop = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res = bt_fmt.frame().symbol(frame, symbol);
|
2019-05-15 07:30:15 -07:00
|
|
|
});
|
2019-09-04 12:14:34 -07:00
|
|
|
if stop {
|
|
|
|
return false;
|
|
|
|
}
|
2019-05-15 07:30:15 -07:00
|
|
|
if !hit {
|
2019-09-04 12:14:34 -07:00
|
|
|
res = bt_fmt.frame().print_raw(frame.ip(), None, None, None);
|
2017-03-04 10:27:52 -05:00
|
|
|
}
|
2019-09-04 12:14:34 -07:00
|
|
|
|
|
|
|
idx += 1;
|
|
|
|
res.is_ok()
|
2019-05-15 07:30:15 -07:00
|
|
|
});
|
2019-09-04 12:14:34 -07:00
|
|
|
res?;
|
2017-03-04 10:27:52 -05:00
|
|
|
}
|
2019-09-04 12:14:34 -07:00
|
|
|
bt_fmt.finish()?;
|
|
|
|
if skipped {
|
2019-05-15 07:30:15 -07:00
|
|
|
writeln!(
|
2019-09-04 12:14:34 -07:00
|
|
|
fmt,
|
2019-05-15 07:30:15 -07:00
|
|
|
"note: Some details are omitted, \
|
|
|
|
run with `RUST_BACKTRACE=full` for a verbose backtrace."
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
Ok(())
|
2017-03-04 10:27:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Fixed frame used to clean the backtrace with `RUST_BACKTRACE=1`.
|
|
|
|
#[inline(never)]
|
|
|
|
pub fn __rust_begin_short_backtrace<F, T>(f: F) -> T
|
2019-05-15 07:30:15 -07:00
|
|
|
where
|
|
|
|
F: FnOnce() -> T,
|
|
|
|
F: Send,
|
|
|
|
T: Send,
|
2017-03-04 10:27:52 -05:00
|
|
|
{
|
|
|
|
f()
|
2016-12-04 16:38:27 -05:00
|
|
|
}
|
|
|
|
|
2015-09-08 15:53:46 -07:00
|
|
|
// 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.
|
2019-09-04 12:14:34 -07:00
|
|
|
pub fn log_enabled() -> Option<PrintFmt> {
|
2015-09-08 15:53:46 -07:00
|
|
|
static ENABLED: atomic::AtomicIsize = atomic::AtomicIsize::new(0);
|
|
|
|
match ENABLED.load(Ordering::SeqCst) {
|
2018-03-29 14:59:13 -07:00
|
|
|
0 => {}
|
2016-12-04 16:38:27 -05:00
|
|
|
1 => return None,
|
2019-09-04 12:14:34 -07:00
|
|
|
2 => return Some(PrintFmt::Short),
|
|
|
|
_ => return Some(PrintFmt::Full),
|
2015-09-08 15:53:46 -07:00
|
|
|
}
|
|
|
|
|
2019-05-15 07:30:15 -07:00
|
|
|
let val = env::var_os("RUST_BACKTRACE").and_then(|x| {
|
2018-07-23 22:00:51 -07:00
|
|
|
if &x == "0" {
|
2016-12-04 16:38:27 -05:00
|
|
|
None
|
|
|
|
} else if &x == "full" {
|
2019-09-04 12:14:34 -07:00
|
|
|
Some(PrintFmt::Full)
|
2016-12-04 16:38:27 -05:00
|
|
|
} else {
|
2019-09-04 12:14:34 -07:00
|
|
|
Some(PrintFmt::Short)
|
2018-07-23 22:00:51 -07:00
|
|
|
}
|
2019-05-15 07:30:15 -07:00
|
|
|
});
|
|
|
|
ENABLED.store(
|
|
|
|
match val {
|
|
|
|
Some(v) => v as isize,
|
|
|
|
None => 1,
|
|
|
|
},
|
|
|
|
Ordering::SeqCst,
|
2018-07-23 22:00:51 -07:00
|
|
|
);
|
2016-12-04 16:38:27 -05:00
|
|
|
val
|
2015-09-08 15:53:46 -07:00
|
|
|
}
|
2015-08-26 01:44:55 +01:00
|
|
|
|
2019-09-04 12:14:34 -07:00
|
|
|
/// Prints the filename of the backtrace frame.
|
|
|
|
///
|
|
|
|
/// See also `output`.
|
|
|
|
fn output_filename(
|
|
|
|
fmt: &mut fmt::Formatter<'_>,
|
|
|
|
bows: BytesOrWideString<'_>,
|
|
|
|
print_fmt: PrintFmt,
|
|
|
|
) -> fmt::Result {
|
|
|
|
#[cfg(windows)]
|
|
|
|
let path_buf;
|
|
|
|
let file = match bows {
|
|
|
|
#[cfg(unix)]
|
|
|
|
BytesOrWideString::Bytes(bytes) => {
|
|
|
|
use crate::os::unix::prelude::*;
|
|
|
|
Path::new(crate::ffi::OsStr::from_bytes(bytes))
|
2016-12-04 16:38:27 -05:00
|
|
|
}
|
2019-09-04 12:14:34 -07:00
|
|
|
#[cfg(not(unix))]
|
|
|
|
BytesOrWideString::Bytes(bytes) => {
|
|
|
|
Path::new(crate::str::from_utf8(bytes).unwrap_or("<unknown>"))
|
2019-05-15 07:30:15 -07:00
|
|
|
}
|
|
|
|
#[cfg(windows)]
|
2019-09-04 12:14:34 -07:00
|
|
|
BytesOrWideString::Wide(wide) => {
|
|
|
|
use crate::os::windows::prelude::*;
|
|
|
|
path_buf = crate::ffi::OsString::from_wide(wide);
|
|
|
|
Path::new(&path_buf)
|
2019-05-15 07:30:15 -07:00
|
|
|
}
|
2019-09-04 12:14:34 -07:00
|
|
|
#[cfg(not(windows))]
|
|
|
|
BytesOrWideString::Wide(_wide) => {
|
|
|
|
Path::new("<unknown>")
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if print_fmt == PrintFmt::Short && file.is_absolute() {
|
|
|
|
if let Ok(cwd) = env::current_dir() {
|
|
|
|
if let Ok(stripped) = file.strip_prefix(&cwd) {
|
|
|
|
if let Some(s) = stripped.to_str() {
|
|
|
|
return write!(fmt, ".{}{}", path::MAIN_SEPARATOR, s);
|
2019-05-15 07:30:15 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-04 12:14:34 -07:00
|
|
|
fmt::Display::fmt(&file.display(), fmt)
|
2015-08-26 01:44:55 +01:00
|
|
|
}
|