Skip to content

Fix symbol demangling on Windows. #19819

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 18, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions src/libstd/rt/backtrace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,18 @@ fn demangle(writer: &mut Writer, s: &str) -> IoResult<()> {
// 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;
let mut inner = s;
if s.len() > 4 && s.starts_with("_ZN") && s.ends_with("E") {
let mut chars = s.slice(3, s.len() - 1).chars();
inner = s.slice(3, s.len() - 1);
// 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") {
inner = s.slice(2, s.len() - 1);
} else {
valid = false;
}

if valid {
let mut chars = inner.chars();
while valid {
let mut i = 0;
for c in chars {
Expand All @@ -84,28 +94,25 @@ fn demangle(writer: &mut Writer, s: &str) -> IoResult<()> {
valid = false;
}
}
} else {
valid = false;
}

// Alright, let's do this.
if !valid {
try!(writer.write_str(s));
} else {
let mut s = s.slice_from(3);
let mut first = true;
while s.len() > 1 {
while inner.len() > 0 {
if !first {
try!(writer.write_str("::"));
} else {
first = false;
}
let mut rest = s;
let mut rest = inner;
while rest.char_at(0).is_numeric() {
rest = rest.slice_from(1);
}
let i: uint = from_str(s.slice_to(s.len() - rest.len())).unwrap();
s = rest.slice_from(i);
let i: uint = from_str(inner.slice_to(inner.len() - rest.len())).unwrap();
inner = rest.slice_from(i);
rest = rest.slice_to(i);
while rest.len() > 0 {
if rest.starts_with("$") {
Expand Down Expand Up @@ -999,6 +1006,9 @@ mod imp {
Some(s) => try!(super::demangle(w, s)),
None => try!(w.write(bytes[..bytes.len()-1])),
}
if displacement != 0 {
try!(write!(w, "+{:#x}", displacement));
}
}
try!(w.write(&['\n' as u8]));
}
Expand Down Expand Up @@ -1037,4 +1047,11 @@ mod test {
t!("_ZN12test$x20test4foobE", "test test::foob");
t!("_ZN12test$UP$test4foobE", "testBoxtest::foob");
}

#[test]
fn demangle_windows() {
t!("ZN4testE", "test");
t!("ZN12test$x20test4foobE", "test test::foob");
t!("ZN12test$UP$test4foobE", "testBoxtest::foob");
}
}