Skip to content

Commit

Permalink
only collect backtrace when RUST_BACKTRACE is set; resolve symbols la…
Browse files Browse the repository at this point in the history
…zily when printing
  • Loading branch information
RalfJung committed Aug 4, 2017
1 parent 726b027 commit ac49e7c
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 48 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions miri/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ pub fn eval_main<'a, 'tcx: 'a>(
tcx.sess.err("the evaluated program leaked memory");
}
}
Err(e) => {
ecx.report(&e);
Err(mut e) => {
ecx.report(&mut e);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ log = "0.3.6"
log_settings = "0.1.1"
lazy_static = "0.2.8"
regex = "0.2.2"
backtrace = "0.3"
backtrace = { version = "0.3", git = "https://github.com/alexcrichton/backtrace-rs" }
10 changes: 7 additions & 3 deletions src/librustc_mir/interpret/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::error::Error;
use std::fmt;
use std::{fmt, env};

use rustc::mir;
use rustc::ty::{FnSig, Ty, layout};
Expand All @@ -15,14 +15,18 @@ use backtrace::Backtrace;
#[derive(Debug)]
pub struct EvalError<'tcx> {
pub kind: EvalErrorKind<'tcx>,
pub backtrace: Backtrace,
pub backtrace: Option<Backtrace>,
}

impl<'tcx> From<EvalErrorKind<'tcx>> for EvalError<'tcx> {
fn from(kind: EvalErrorKind<'tcx>) -> Self {
let backtrace = match env::var("RUST_BACKTRACE") {
Ok(ref val) if !val.is_empty() => Some(Backtrace::new_unresolved()),
_ => None
};
EvalError {
kind,
backtrace: Backtrace::new(),
backtrace,
}
}
}
Expand Down
76 changes: 40 additions & 36 deletions src/librustc_mir/interpret/eval_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1716,49 +1716,53 @@ impl<'a, 'tcx, M: Machine<'tcx>> EvalContext<'a, 'tcx, M> {
Ok(())
}

pub fn report(&self, e: &EvalError) {
let mut trace_text = "\n################################\nerror occurred in miri at\n".to_string();
let mut skip_init = true;
'frames: for (i, frame) in e.backtrace.frames().iter().enumerate() {
for symbol in frame.symbols() {
if let Some(name) = symbol.name() {
// unmangle the symbol via `to_string`
let name = name.to_string();
if name.starts_with("miri::after_analysis") {
// don't report initialization gibberish
break 'frames;
} else if name.starts_with("backtrace::capture::Backtrace::new")
pub fn report(&self, e: &mut EvalError) {
if let Some(ref mut backtrace) = e.backtrace {
let mut trace_text = "\n\nAn error occurred in miri:\n".to_string();
let mut skip_init = true;
backtrace.resolve();
'frames: for (i, frame) in backtrace.frames().iter().enumerate() {
for symbol in frame.symbols() {
if let Some(name) = symbol.name() {
// unmangle the symbol via `to_string`
let name = name.to_string();
if name.starts_with("miri::after_analysis") {
// don't report initialization gibberish
break 'frames;
} else if name.starts_with("backtrace::capture::Backtrace::new")
// debug mode produces funky symbol names
|| name.starts_with("backtrace::capture::{{impl}}::new") {
// don't report backtrace internals
skip_init = false;
continue 'frames;
|| name.starts_with("backtrace::capture::{{impl}}::new") {
// don't report backtrace internals
skip_init = false;
continue 'frames;
}
}
}
}
if skip_init {
continue;
}
write!(trace_text, "{}\n", i).unwrap();
for symbol in frame.symbols() {
if let Some(name) = symbol.name() {
write!(trace_text, "# {}\n", name).unwrap();
} else {
write!(trace_text, "# <unknown>\n").unwrap();
}
if let Some(file_path) = symbol.filename() {
write!(trace_text, "{}", file_path.display()).unwrap();
} else {
write!(trace_text, "<unknown_file>").unwrap();
if skip_init {
continue;
}
if let Some(line) = symbol.lineno() {
write!(trace_text, ":{}\n", line).unwrap();
} else {
write!(trace_text, "\n").unwrap();
for symbol in frame.symbols() {
write!(trace_text, "{}: " , i).unwrap();
if let Some(name) = symbol.name() {
write!(trace_text, "{}\n", name).unwrap();
} else {
write!(trace_text, "<unknown>\n").unwrap();
}
write!(trace_text, "\tat ").unwrap();
if let Some(file_path) = symbol.filename() {
write!(trace_text, "{}", file_path.display()).unwrap();
} else {
write!(trace_text, "<unknown_file>").unwrap();
}
if let Some(line) = symbol.lineno() {
write!(trace_text, ":{}\n", line).unwrap();
} else {
write!(trace_text, "\n").unwrap();
}
}
}
error!("{}", trace_text);
}
trace!("{}", trace_text);
if let Some(frame) = self.stack().last() {
let block = &frame.mir.basic_blocks()[frame.block];
let span = if frame.stmt < block.statements.len() {
Expand Down

0 comments on commit ac49e7c

Please sign in to comment.