1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
15 #[cfg(target_pointer_width = "64")]
16 pub const HEX_WIDTH: uint = 18;
18 #[cfg(target_pointer_width = "32")]
19 pub const HEX_WIDTH: uint = 10;
21 // All rust symbols are in theory lists of "::"-separated identifiers. Some
22 // assemblers, however, can't handle these characters in symbol names. To get
23 // around this, we use C++-style mangling. The mangling method is:
25 // 1. Prefix the symbol with "_ZN"
26 // 2. For each element of the path, emit the length plus the element
27 // 3. End the path with "E"
29 // For example, "_ZN4testE" => "test" and "_ZN3foo3bar" => "foo::bar".
31 // We're the ones printing our backtraces, so we can't rely on anything else to
32 // demangle our symbols. It's *much* nicer to look at demangled symbols, so
33 // this function is implemented to give us nice pretty output.
35 // Note that this demangler isn't quite as fancy as it could be. We have lots
36 // of other information in our symbols like hashes, version, type information,
37 // etc. Additionally, this doesn't handle glue symbols at all.
38 pub fn demangle(writer: &mut Writer, s: &str) -> IoResult<()> {
39 // First validate the symbol. If it doesn't look like anything we're
40 // expecting, we just print it literally. Note that we must handle non-rust
41 // symbols because we could have any function in the backtrace.
44 if s.len() > 4 && s.starts_with("_ZN") && s.ends_with("E") {
45 inner = &s[3 .. s.len() - 1];
46 // On Windows, dbghelp strips leading underscores, so we accept "ZN...E" form too.
47 } else if s.len() > 3 && s.starts_with("ZN") && s.ends_with("E") {
48 inner = &s[2 .. s.len() - 1];
54 let mut chars = inner.chars();
59 i = i * 10 + c as uint - '0' as uint;
65 valid = chars.next().is_none();
67 } else if chars.by_ref().take(i - 1).count() != i - 1 {
73 // Alright, let's do this.
75 try!(writer.write_str(s));
78 while inner.len() > 0 {
80 try!(writer.write_str("::"));
85 while rest.char_at(0).is_numeric() {
88 let i: uint = inner[.. (inner.len() - rest.len())].parse().unwrap();
91 while rest.len() > 0 {
92 if rest.starts_with("$") {
93 macro_rules! demangle {
94 ($($pat:expr, => $demangled:expr),*) => ({
95 $(if rest.starts_with($pat) {
96 try!(writer.write_str($demangled));
97 rest = rest.slice_from($pat.len());
100 try!(writer.write_str(rest));
107 // see src/librustc/back/link.rs for these mappings
119 // in theory we can demangle any Unicode code point, but
120 // for simplicity we just catch the common ones.
127 let idx = match rest.find('$') {
131 try!(writer.write_str(&rest[..idx]));