Skip to content
Closed
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
48 changes: 34 additions & 14 deletions src/libstd/sys_common/backtrace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use io::prelude::*;
use io;
use str;
use sync::atomic::{self, Ordering};
use path::{self, Path};
use sys::mutex::Mutex;
use ptr;

Expand Down Expand Up @@ -196,6 +195,39 @@ fn output(w: &mut Write, idx: usize, frame: Frame,
w.write_all(b"\n")
}

#[cfg(not(target_os = "cloudabi"))]
fn output_fileline_relative(w: &mut Write,
file: &str,
line: u32,
format: PrintFormat) -> io::Result<bool> {
use path::{self, Path};

let file_path = Path::new(file);
let mut already_printed = false;
if format == PrintFormat::Short && file_path.is_absolute() {
if let Ok(cwd) = env::current_dir() {
if let Ok(stripped) = file_path.strip_prefix(&cwd) {
if let Some(s) = stripped.to_str() {
write!(w, " at .{}{}:{}", path::MAIN_SEPARATOR, s, line)?;
already_printed = true;
}
}
}
}
Ok(already_printed)
}

#[cfg(target_os = "cloudabi")]
fn output_fileline_relative(_: &mut Write,
_: &str,
_: u32,
_: PrintFormat) -> io::Result<bool> {
// CloudABI does not provide a global file system namespace, nor a
// process working directory. Don't attempt to make pathnames
// relative to the working directory.
Ok(false)
}

/// Print the filename and line number of the backtrace frame.
///
/// See also `output`.
Expand All @@ -215,19 +247,7 @@ fn output_fileline(w: &mut Write,
}

let file = str::from_utf8(file).unwrap_or("<unknown>");
let file_path = Path::new(file);
let mut already_printed = false;
if format == PrintFormat::Short && file_path.is_absolute() {
if let Ok(cwd) = env::current_dir() {
if let Ok(stripped) = file_path.strip_prefix(&cwd) {
if let Some(s) = stripped.to_str() {
write!(w, " at .{}{}:{}", path::MAIN_SEPARATOR, s, line)?;
already_printed = true;
}
}
}
}
if !already_printed {
if !output_fileline_relative(w, file, line, format)? {
write!(w, " at {}:{}", file, line)?;
}

Expand Down