4 /// Representation of a demangled symbol name.
5 pub struct Demangle
<'a
> {
7 /// The number of ::-separated elements in the original name.
11 /// De-mangles a Rust symbol into a more readable version
13 /// All Rust symbols by default are mangled as they contain characters that
14 /// cannot be represented in all object files. The mangling mechanism is similar
15 /// to C++'s, but Rust has a few specifics to handle items like lifetimes in
18 /// This function will take a **mangled** symbol and return a value. When printed,
19 /// the de-mangled version will be written. If the symbol does not look like
20 /// a mangled symbol, the original value will be written instead.
25 /// use rustc_demangle::demangle;
27 /// assert_eq!(demangle("_ZN4testE").to_string(), "test");
28 /// assert_eq!(demangle("_ZN3foo3barE").to_string(), "foo::bar");
29 /// assert_eq!(demangle("foo").to_string(), "foo");
32 // All Rust symbols are in theory lists of "::"-separated identifiers. Some
33 // assemblers, however, can't handle these characters in symbol names. To get
34 // around this, we use C++-style mangling. The mangling method is:
36 // 1. Prefix the symbol with "_ZN"
37 // 2. For each element of the path, emit the length plus the element
38 // 3. End the path with "E"
40 // For example, "_ZN4testE" => "test" and "_ZN3foo3barE" => "foo::bar".
42 // We're the ones printing our backtraces, so we can't rely on anything else to
43 // demangle our symbols. It's *much* nicer to look at demangled symbols, so
44 // this function is implemented to give us nice pretty output.
46 // Note that this demangler isn't quite as fancy as it could be. We have lots
47 // of other information in our symbols like hashes, version, type information,
48 // etc. Additionally, this doesn't handle glue symbols at all.
49 pub fn demangle(s
: &str) -> Result
<(Demangle
, &str), ()> {
50 // First validate the symbol. If it doesn't look like anything we're
51 // expecting, we just print it literally. Note that we must handle non-Rust
52 // symbols because we could have any function in the backtrace.
53 let inner
= if s
.starts_with("_ZN") {
55 } else if s
.starts_with("ZN") {
56 // On Windows, dbghelp strips leading underscores, so we accept "ZN...E"
59 } else if s
.starts_with("__ZN") {
60 // On OSX, symbols are prefixed with an extra _
66 // only work with ascii text
67 if inner
.bytes().any(|c
| c
& 0x80 != 0) {
72 let mut chars
= inner
.chars();
73 let mut c
= chars
.next().ok_or(())?
;
75 // Decode an identifier element's length.
80 while let Some(d
) = c
.to_digit(10) {
83 .and_then(|len
| len
.checked_add(d
as usize))
85 c
= chars
.next().ok_or(())?
;
88 // `c` already contains the first character of this identifier, skip it and
89 // all the other characters of this identifier, to reach the next element.
91 c
= chars
.next().ok_or(())?
;
97 Ok((Demangle { inner, elements }
, chars
.as_str()))
100 // Rust hashes are hex digits with an `h` prepended.
101 fn is_rust_hash(s
: &str) -> bool
{
102 s
.starts_with('h'
) && s
[1..].chars().all(|c
| c
.is_digit(16))
105 impl<'a
> fmt
::Display
for Demangle
<'a
> {
106 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
107 // Alright, let's do this.
108 let mut inner
= self.inner
;
109 for element
in 0..self.elements
{
110 let mut rest
= inner
;
111 while rest
.chars().next().unwrap().is_digit(10) {
114 let i
: usize = inner
[..(inner
.len() - rest
.len())].parse().unwrap();
117 // Skip printing the hash if alternate formatting
119 if f
.alternate() && element
+ 1 == self.elements
&& is_rust_hash(&rest
) {
125 if rest
.starts_with("_$") {
129 if rest
.starts_with('
.'
) {
130 if let Some('
.'
) = rest
[1..].chars().next() {
137 } else if rest
.starts_with('$'
) {
138 let (escape
, after_escape
) = if let Some(end
) = rest
[1..].find('$'
) {
139 (&rest
[1..=end
], &rest
[end
+ 2..])
144 // see src/librustc_codegen_utils/symbol_names/legacy.rs for these mappings
145 let unescaped
= match escape
{
156 if escape
.starts_with('u'
) {
157 let digits
= &escape
[1..];
158 let all_lower_hex
= digits
.chars().all(|c
| match c
{
159 '
0'
..='
9'
| 'a'
..='f'
=> true,
162 let c
= u32::from_str_radix(digits
, 16)
164 .and_then(char::from_u32
);
165 if let (true, Some(c
)) = (all_lower_hex
, c
) {
166 // FIXME(eddyb) do we need to filter out control codepoints?
177 f
.write_str(unescaped
)?
;
179 } else if let Some(i
) = rest
.find(|c
| c
== '$'
|| c
== '
.'
) {
180 f
.write_str(&rest
[..i
])?
;
195 use std
::prelude
::v1
::*;
198 ($a
:expr
, $b
:expr
) => {
209 macro_rules
! t_nohash
{
210 ($a
:expr
, $b
:expr
) => {{
211 assert_eq
!(format
!("{:#}", ::demangle($a
)), $b
);
215 fn ok(sym
: &str, expected
: &str) -> bool
{
216 match ::try_demangle(sym
) {
218 if s
.to_string() == expected
{
221 println
!("\n{}\n!=\n{}\n", s
, expected
);
226 println
!("error demangling");
232 fn ok_err(sym
: &str) -> bool
{
233 match ::try_demangle(sym
) {
235 println
!("succeeded in demangling");
238 Err(_
) => ::demangle(sym
).to_string() == sym
,
245 t
!("_ZN4testE", "test");
247 t
!("_ZN4test1a2bcE", "test::a::bc");
251 fn demangle_dollars() {
252 t
!("_ZN4$RP$E", ")");
253 t
!("_ZN8$RF$testE", "&test");
254 t
!("_ZN8$BP$test4foobE", "*test::foob");
255 t
!("_ZN9$u20$test4foobE", " test::foob");
256 t
!("_ZN35Bar$LT$$u5b$u32$u3b$$u20$4$u5d$$GT$E", "Bar<[u32; 4]>");
260 fn demangle_many_dollars() {
261 t
!("_ZN13test$u20$test4foobE", "test test::foob");
262 t
!("_ZN12test$BP$test4foobE", "test*test::foob");
268 "__ZN5alloc9allocator6Layout9for_value17h02a996811f781011E",
269 "alloc::allocator::Layout::for_value::h02a996811f781011"
271 t
!("__ZN38_$LT$core..option..Option$LT$T$GT$$GT$6unwrap18_MSG_FILE_LINE_COL17haf7cb8d5824ee659E", "<core::option::Option<T>>::unwrap::_MSG_FILE_LINE_COL::haf7cb8d5824ee659");
272 t
!("__ZN4core5slice89_$LT$impl$u20$core..iter..traits..IntoIterator$u20$for$u20$$RF$$u27$a$u20$$u5b$T$u5d$$GT$9into_iter17h450e234d27262170E", "core::slice::<impl core::iter::traits::IntoIterator for &'a [T]>::into_iter::h450e234d27262170");
276 fn demangle_windows() {
277 t
!("ZN4testE", "test");
278 t
!("ZN13test$u20$test4foobE", "test test::foob");
279 t
!("ZN12test$RF$test4foobE", "test&test::foob");
283 fn demangle_elements_beginning_with_underscore() {
284 t
!("_ZN13_$LT$test$GT$E", "<test>");
285 t
!("_ZN28_$u7b$$u7b$closure$u7d$$u7d$E", "{{closure}}");
286 t
!("_ZN15__STATIC_FMTSTRE", "__STATIC_FMTSTR");
290 fn demangle_trait_impls() {
292 "_ZN71_$LT$Test$u20$$u2b$$u20$$u27$static$u20$as$u20$foo..Bar$LT$Test$GT$$GT$3barE",
293 "<Test + 'static as foo::Bar<Test>>::bar"
298 fn demangle_without_hash() {
299 let s
= "_ZN3foo17h05af221e174051e9E";
300 t
!(s
, "foo::h05af221e174051e9");
305 fn demangle_without_hash_edgecases() {
306 // One element, no hash.
307 t_nohash
!("_ZN3fooE", "foo");
308 // Two elements, no hash.
309 t_nohash
!("_ZN3foo3barE", "foo::bar");
310 // Longer-than-normal hash.
311 t_nohash
!("_ZN3foo20h05af221e174051e9abcE", "foo");
312 // Shorter-than-normal hash.
313 t_nohash
!("_ZN3foo5h05afE", "foo");
314 // Valid hash, but not at the end.
315 t_nohash
!("_ZN17h05af221e174051e93fooE", "h05af221e174051e9::foo");
316 // Not a valid hash, missing the 'h'.
317 t_nohash
!("_ZN3foo16ffaf221e174051e9E", "foo::ffaf221e174051e9");
318 // Not a valid hash, has a non-hex-digit.
319 t_nohash
!("_ZN3foo17hg5af221e174051e9E", "foo::hg5af221e174051e9");
323 fn demangle_thinlto() {
324 // One element, no hash.
325 t
!("_ZN3fooE.llvm.9D1C9369", "foo");
326 t
!("_ZN3fooE.llvm.9D1C9369@@16", "foo");
328 "_ZN9backtrace3foo17hbb467fcdaea5d79bE.llvm.A5310EB9",
334 fn demangle_llvm_ir_branch_labels() {
335 t
!("_ZN4core5slice77_$LT$impl$u20$core..ops..index..IndexMut$LT$I$GT$$u20$for$u20$$u5b$T$u5d$$GT$9index_mut17haf9727c2edfbc47bE.exit.i.i", "core::slice::<impl core::ops::index::IndexMut<I> for [T]>::index_mut::haf9727c2edfbc47b.exit.i.i");
336 t_nohash
!("_ZN4core5slice77_$LT$impl$u20$core..ops..index..IndexMut$LT$I$GT$$u20$for$u20$$u5b$T$u5d$$GT$9index_mut17haf9727c2edfbc47bE.exit.i.i", "core::slice::<impl core::ops::index::IndexMut<I> for [T]>::index_mut.exit.i.i");
340 fn demangle_ignores_suffix_that_doesnt_look_like_a_symbol() {
341 t_err
!("_ZN3fooE.llvm moocow");
346 ::demangle("_ZN2222222222222222222222EE").to_string();
347 ::demangle("_ZN5*70527e27.ll34csaғE").to_string();
348 ::demangle("_ZN5*70527a54.ll34_$b.1E").to_string();
353 5usage20h)3\0\0\0\0\0\0\07e2734cOsbE\
360 fn invalid_no_chop() {
365 fn handle_assoc_types() {
366 t
!("_ZN151_$LT$alloc..boxed..Box$LT$alloc..boxed..FnBox$LT$A$C$$u20$Output$u3d$R$GT$$u20$$u2b$$u20$$u27$a$GT$$u20$as$u20$core..ops..function..FnOnce$LT$A$GT$$GT$9call_once17h69e8f44b3723e1caE", "<alloc::boxed::Box<alloc::boxed::FnBox<A, Output=R> + 'a> as core::ops::function::FnOnce<A>>::call_once::h69e8f44b3723e1ca");
372 "_ZN88_$LT$core..result..Result$LT$$u21$$C$$u20$E$GT$$u20$as$u20$std..process..Termination$GT$6report17hfc41d0da4a40b3e8E",
373 "<core::result::Result<!, E> as std::process::Termination>::report::hfc41d0da4a40b3e8"
378 fn demangle_utf8_idents() {
380 "_ZN11utf8_idents157_$u10e1$$u10d0$$u10ed$$u10db$$u10d4$$u10da$$u10d0$$u10d3$_$u10d2$$u10d4$$u10db$$u10e0$$u10d8$$u10d4$$u10da$$u10d8$_$u10e1$$u10d0$$u10d3$$u10d8$$u10da$$u10d8$17h21634fd5714000aaE",
381 "utf8_idents::საჭმელად_გემრიელი_სადილი"
386 fn demangle_issue_60925() {
388 "_ZN11issue_609253foo37Foo$LT$issue_60925..llv$u6d$..Foo$GT$3foo17h059a991a004536adE",
389 "issue_60925::foo::Foo<issue_60925::llvm::Foo>::foo"