]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/common/backtrace.rs
Merge pull request #20674 from jbcrail/fix-misspelled-comments
[rust.git] / src / libstd / sys / common / backtrace.rs
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.
4 //
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.
10
11 use prelude::v1::*;
12
13 use io::IoResult;
14
15 #[cfg(target_word_size = "64")] pub const HEX_WIDTH: uint = 18;
16 #[cfg(target_word_size = "32")] pub const HEX_WIDTH: uint = 10;
17
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:
21 //
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"
25 //
26 // For example, "_ZN4testE" => "test" and "_ZN3foo3bar" => "foo::bar".
27 //
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.
31 //
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.
39     let mut valid = true;
40     let mut inner = s;
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);
46     } else {
47         valid = false;
48     }
49
50     if valid {
51         let mut chars = inner.chars();
52         while valid {
53             let mut i = 0;
54             for c in chars {
55                 if c.is_numeric() {
56                     i = i * 10 + c as uint - '0' as uint;
57                 } else {
58                     break
59                 }
60             }
61             if i == 0 {
62                 valid = chars.next().is_none();
63                 break
64             } else if chars.by_ref().take(i - 1).count() != i - 1 {
65                 valid = false;
66             }
67         }
68     }
69
70     // Alright, let's do this.
71     if !valid {
72         try!(writer.write_str(s));
73     } else {
74         let mut first = true;
75         while inner.len() > 0 {
76             if !first {
77                 try!(writer.write_str("::"));
78             } else {
79                 first = false;
80             }
81             let mut rest = inner;
82             while rest.char_at(0).is_numeric() {
83                 rest = rest.slice_from(1);
84             }
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());
95                               } else)*
96                             {
97                                 try!(writer.write_str(rest));
98                                 break;
99                             }
100
101                         })
102                     }
103
104                     // see src/librustc/back/link.rs for these mappings
105                     demangle! (
106                         "$SP$", => "@",
107                         "$UP$", => "Box",
108                         "$RP$", => "*",
109                         "$BP$", => "&",
110                         "$LT$", => "<",
111                         "$GT$", => ">",
112                         "$LP$", => "(",
113                         "$RP$", => ")",
114                         "$C$", => ",",
115
116                         // in theory we can demangle any Unicode code point, but
117                         // for simplicity we just catch the common ones.
118                         "$u{20}", => " ",
119                         "$u{27}", => "'",
120                         "$u{5b}", => "[",
121                         "$u{5d}", => "]"
122                     )
123                 } else {
124                     let idx = match rest.find('$') {
125                         None => rest.len(),
126                         Some(i) => i,
127                     };
128                     try!(writer.write_str(rest.slice_to(idx)));
129                     rest = rest.slice_from(idx);
130                 }
131             }
132         }
133     }
134
135     Ok(())
136 }