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_word_size = "64")] pub const HEX_WIDTH: uint = 18;
16 #[cfg(target_word_size = "32")] pub const HEX_WIDTH: uint = 10;
18 // All rust symbols are in theory lists of "::"-separated identifiers. Some
19 // assemblers, however, can't handle these characters in symbol names. To get
20 // around this, we use C++-style mangling. The mangling method is:
22 // 1. Prefix the symbol with "_ZN"
23 // 2. For each element of the path, emit the length plus the element
24 // 3. End the path with "E"
26 // For example, "_ZN4testE" => "test" and "_ZN3foo3bar" => "foo::bar".
28 // We're the ones printing our backtraces, so we can't rely on anything else to
29 // demangle our symbols. It's *much* nicer to look at demangled symbols, so
30 // this function is implemented to give us nice pretty output.
32 // Note that this demangler isn't quite as fancy as it could be. We have lots
33 // of other information in our symbols like hashes, version, type information,
34 // etc. Additionally, this doesn't handle glue symbols at all.
35 pub fn demangle(writer: &mut Writer, s: &str) -> IoResult<()> {
36 // First validate the symbol. If it doesn't look like anything we're
37 // expecting, we just print it literally. Note that we must handle non-rust
38 // symbols because we could have any function in the backtrace.
41 if s.len() > 4 && s.starts_with("_ZN") && s.ends_with("E") {
42 inner = s.slice(3, s.len() - 1);
43 // On Windows, dbghelp strips leading underscores, so we accept "ZN...E" form too.
44 } else if s.len() > 3 && s.starts_with("ZN") && s.ends_with("E") {
45 inner = s.slice(2, s.len() - 1);
51 let mut chars = inner.chars();
56 i = i * 10 + c as uint - '0' as uint;
62 valid = chars.next().is_none();
64 } else if chars.by_ref().take(i - 1).count() != i - 1 {
70 // Alright, let's do this.
72 try!(writer.write_str(s));
75 while inner.len() > 0 {
77 try!(writer.write_str("::"));
82 while rest.char_at(0).is_numeric() {
83 rest = rest.slice_from(1);
85 let i: uint = inner.slice_to(inner.len() - rest.len()).parse().unwrap();
86 inner = rest.slice_from(i);
87 rest = rest.slice_to(i);
88 while rest.len() > 0 {
89 if rest.starts_with("$") {
90 macro_rules! demangle {
91 ($($pat:expr, => $demangled:expr),*) => ({
92 $(if rest.starts_with($pat) {
93 try!(writer.write_str($demangled));
94 rest = rest.slice_from($pat.len());
97 try!(writer.write_str(rest));
104 // see src/librustc/back/link.rs for these mappings
116 // in theory we can demangle any Unicode code point, but
117 // for simplicity we just catch the common ones.
124 let idx = match rest.find('$') {
128 try!(writer.write_str(rest.slice_to(idx)));
129 rest = rest.slice_from(idx);