2014-11-23 19:21:17 -08:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2015-10-24 20:51:34 -05:00
|
|
|
#![cfg_attr(target_os = "nacl", allow(dead_code))]
|
|
|
|
|
2015-09-08 15:53:46 -07:00
|
|
|
use env;
|
2015-08-26 01:44:55 +01:00
|
|
|
use io::prelude::*;
|
2015-09-08 15:53:46 -07:00
|
|
|
use io;
|
2015-08-26 01:44:55 +01:00
|
|
|
use libc;
|
2015-09-08 15:53:46 -07:00
|
|
|
use str;
|
|
|
|
use sync::atomic::{self, Ordering};
|
|
|
|
|
|
|
|
pub use sys::backtrace::write;
|
2014-11-23 19:21:17 -08:00
|
|
|
|
2015-01-16 17:01:02 +02:00
|
|
|
#[cfg(target_pointer_width = "64")]
|
2015-03-25 17:06:52 -07:00
|
|
|
pub const HEX_WIDTH: usize = 18;
|
2015-01-07 17:26:55 +13:00
|
|
|
|
2015-01-16 17:01:02 +02:00
|
|
|
#[cfg(target_pointer_width = "32")]
|
2015-03-25 17:06:52 -07:00
|
|
|
pub const HEX_WIDTH: usize = 10;
|
2014-11-23 19:21:17 -08: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.
|
|
|
|
pub fn log_enabled() -> bool {
|
|
|
|
static ENABLED: atomic::AtomicIsize = atomic::AtomicIsize::new(0);
|
|
|
|
match ENABLED.load(Ordering::SeqCst) {
|
|
|
|
1 => return false,
|
|
|
|
2 => return true,
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
let val = match env::var_os("RUST_BACKTRACE") {
|
|
|
|
Some(..) => 2,
|
|
|
|
None => 1,
|
|
|
|
};
|
|
|
|
ENABLED.store(val, Ordering::SeqCst);
|
|
|
|
val == 2
|
|
|
|
}
|
2015-08-26 01:44:55 +01:00
|
|
|
|
|
|
|
// These output functions should now be used everywhere to ensure consistency.
|
|
|
|
pub fn output(w: &mut Write, idx: isize, addr: *mut libc::c_void,
|
|
|
|
s: Option<&[u8]>) -> io::Result<()> {
|
|
|
|
try!(write!(w, " {:2}: {:2$?} - ", idx, addr, HEX_WIDTH));
|
|
|
|
match s.and_then(|s| str::from_utf8(s).ok()) {
|
|
|
|
Some(string) => try!(demangle(w, string)),
|
|
|
|
None => try!(write!(w, "<unknown>")),
|
|
|
|
}
|
|
|
|
w.write_all(&['\n' as u8])
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
pub fn output_fileline(w: &mut Write, file: &[u8], line: libc::c_int,
|
|
|
|
more: bool) -> io::Result<()> {
|
|
|
|
let file = str::from_utf8(file).unwrap_or("<unknown>");
|
|
|
|
// prior line: " ##: {:2$} - func"
|
|
|
|
try!(write!(w, " {:3$}at {}:{}", "", file, line, HEX_WIDTH));
|
|
|
|
if more {
|
|
|
|
try!(write!(w, " <... and possibly more>"));
|
|
|
|
}
|
|
|
|
w.write_all(&['\n' as u8])
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-23 19:21:17 -08:00
|
|
|
// All rust symbols are in theory lists of "::"-separated identifiers. Some
|
|
|
|
// assemblers, however, can't handle these characters in symbol names. To get
|
|
|
|
// around this, we use C++-style mangling. The mangling method is:
|
|
|
|
//
|
|
|
|
// 1. Prefix the symbol with "_ZN"
|
|
|
|
// 2. For each element of the path, emit the length plus the element
|
|
|
|
// 3. End the path with "E"
|
|
|
|
//
|
2015-06-09 20:47:51 +02:00
|
|
|
// For example, "_ZN4testE" => "test" and "_ZN3foo3barE" => "foo::bar".
|
2014-11-23 19:21:17 -08:00
|
|
|
//
|
|
|
|
// We're the ones printing our backtraces, so we can't rely on anything else to
|
|
|
|
// demangle our symbols. It's *much* nicer to look at demangled symbols, so
|
|
|
|
// this function is implemented to give us nice pretty output.
|
|
|
|
//
|
|
|
|
// Note that this demangler isn't quite as fancy as it could be. We have lots
|
|
|
|
// of other information in our symbols like hashes, version, type information,
|
|
|
|
// etc. Additionally, this doesn't handle glue symbols at all.
|
2015-03-11 15:24:14 -07:00
|
|
|
pub fn demangle(writer: &mut Write, s: &str) -> io::Result<()> {
|
2014-11-23 19:21:17 -08:00
|
|
|
// First validate the symbol. If it doesn't look like anything we're
|
|
|
|
// expecting, we just print it literally. Note that we must handle non-rust
|
|
|
|
// symbols because we could have any function in the backtrace.
|
|
|
|
let mut valid = true;
|
2014-12-18 19:41:20 -08:00
|
|
|
let mut inner = s;
|
2014-11-23 19:21:17 -08:00
|
|
|
if s.len() > 4 && s.starts_with("_ZN") && s.ends_with("E") {
|
2015-01-17 16:15:52 -08:00
|
|
|
inner = &s[3 .. s.len() - 1];
|
2014-12-18 19:41:20 -08:00
|
|
|
// On Windows, dbghelp strips leading underscores, so we accept "ZN...E" form too.
|
|
|
|
} else if s.len() > 3 && s.starts_with("ZN") && s.ends_with("E") {
|
2015-01-17 16:15:52 -08:00
|
|
|
inner = &s[2 .. s.len() - 1];
|
2014-12-18 19:41:20 -08:00
|
|
|
} else {
|
|
|
|
valid = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if valid {
|
|
|
|
let mut chars = inner.chars();
|
2014-11-23 19:21:17 -08:00
|
|
|
while valid {
|
|
|
|
let mut i = 0;
|
2015-01-23 13:16:03 -05:00
|
|
|
for c in chars.by_ref() {
|
2014-11-23 19:21:17 -08:00
|
|
|
if c.is_numeric() {
|
2015-03-25 17:06:52 -07:00
|
|
|
i = i * 10 + c as usize - '0' as usize;
|
2014-11-23 19:21:17 -08:00
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if i == 0 {
|
|
|
|
valid = chars.next().is_none();
|
|
|
|
break
|
|
|
|
} else if chars.by_ref().take(i - 1).count() != i - 1 {
|
|
|
|
valid = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Alright, let's do this.
|
|
|
|
if !valid {
|
2015-03-11 15:24:14 -07:00
|
|
|
try!(writer.write_all(s.as_bytes()));
|
2014-11-23 19:21:17 -08:00
|
|
|
} else {
|
|
|
|
let mut first = true;
|
2015-03-24 16:54:09 -07:00
|
|
|
while !inner.is_empty() {
|
2014-11-23 19:21:17 -08:00
|
|
|
if !first {
|
2015-03-11 15:24:14 -07:00
|
|
|
try!(writer.write_all(b"::"));
|
2014-11-23 19:21:17 -08:00
|
|
|
} else {
|
|
|
|
first = false;
|
|
|
|
}
|
2014-12-18 19:41:20 -08:00
|
|
|
let mut rest = inner;
|
2014-11-23 19:21:17 -08:00
|
|
|
while rest.char_at(0).is_numeric() {
|
2015-01-17 16:15:52 -08:00
|
|
|
rest = &rest[1..];
|
2014-11-23 19:21:17 -08:00
|
|
|
}
|
2015-03-25 17:06:52 -07:00
|
|
|
let i: usize = inner[.. (inner.len() - rest.len())].parse().unwrap();
|
2015-01-17 16:15:52 -08:00
|
|
|
inner = &rest[i..];
|
|
|
|
rest = &rest[..i];
|
2015-03-24 16:54:09 -07:00
|
|
|
while !rest.is_empty() {
|
2014-11-23 19:21:17 -08:00
|
|
|
if rest.starts_with("$") {
|
2014-12-18 19:41:20 -08:00
|
|
|
macro_rules! demangle {
|
2015-01-05 01:51:03 -05:00
|
|
|
($($pat:expr, => $demangled:expr),*) => ({
|
2014-11-23 19:21:17 -08:00
|
|
|
$(if rest.starts_with($pat) {
|
2015-03-11 15:24:14 -07:00
|
|
|
try!(writer.write_all($demangled));
|
2015-01-26 21:21:15 -05:00
|
|
|
rest = &rest[$pat.len()..];
|
2014-11-23 19:21:17 -08:00
|
|
|
} else)*
|
|
|
|
{
|
2015-03-11 15:24:14 -07:00
|
|
|
try!(writer.write_all(rest.as_bytes()));
|
2014-11-23 19:21:17 -08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
2014-12-18 19:41:20 -08:00
|
|
|
}
|
|
|
|
|
2014-11-23 19:21:17 -08:00
|
|
|
// see src/librustc/back/link.rs for these mappings
|
|
|
|
demangle! (
|
2015-03-11 15:24:14 -07:00
|
|
|
"$SP$", => b"@",
|
|
|
|
"$BP$", => b"*",
|
|
|
|
"$RF$", => b"&",
|
|
|
|
"$LT$", => b"<",
|
|
|
|
"$GT$", => b">",
|
|
|
|
"$LP$", => b"(",
|
|
|
|
"$RP$", => b")",
|
|
|
|
"$C$", => b",",
|
2014-11-23 19:21:17 -08:00
|
|
|
|
|
|
|
// in theory we can demangle any Unicode code point, but
|
|
|
|
// for simplicity we just catch the common ones.
|
2015-03-11 15:24:14 -07:00
|
|
|
"$u7e$", => b"~",
|
|
|
|
"$u20$", => b" ",
|
|
|
|
"$u27$", => b"'",
|
|
|
|
"$u5b$", => b"[",
|
|
|
|
"$u5d$", => b"]"
|
2014-11-23 19:21:17 -08:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
let idx = match rest.find('$') {
|
|
|
|
None => rest.len(),
|
|
|
|
Some(i) => i,
|
|
|
|
};
|
2015-03-11 15:24:14 -07:00
|
|
|
try!(writer.write_all(rest[..idx].as_bytes()));
|
2015-01-17 16:15:52 -08:00
|
|
|
rest = &rest[idx..];
|
2014-11-23 19:21:17 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2015-09-08 15:53:46 -07:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use prelude::v1::*;
|
|
|
|
use sys_common;
|
|
|
|
macro_rules! t { ($a:expr, $b:expr) => ({
|
|
|
|
let mut m = Vec::new();
|
|
|
|
sys_common::backtrace::demangle(&mut m, $a).unwrap();
|
|
|
|
assert_eq!(String::from_utf8(m).unwrap(), $b);
|
|
|
|
}) }
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn demangle() {
|
|
|
|
t!("test", "test");
|
|
|
|
t!("_ZN4testE", "test");
|
|
|
|
t!("_ZN4test", "_ZN4test");
|
|
|
|
t!("_ZN4test1a2bcE", "test::a::bc");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn demangle_dollars() {
|
|
|
|
t!("_ZN4$RP$E", ")");
|
|
|
|
t!("_ZN8$RF$testE", "&test");
|
|
|
|
t!("_ZN8$BP$test4foobE", "*test::foob");
|
|
|
|
t!("_ZN9$u20$test4foobE", " test::foob");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn demangle_many_dollars() {
|
|
|
|
t!("_ZN13test$u20$test4foobE", "test test::foob");
|
|
|
|
t!("_ZN12test$BP$test4foobE", "test*test::foob");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn demangle_windows() {
|
|
|
|
t!("ZN4testE", "test");
|
|
|
|
t!("ZN13test$u20$test4foobE", "test test::foob");
|
|
|
|
t!("ZN12test$RF$test4foobE", "test&test::foob");
|
|
|
|
}
|
|
|
|
}
|