From 2ec775905afd0a983d4ef0457cdc660cd22bb1be Mon Sep 17 00:00:00 2001
From: Tibor Nagy <xnagytibor@gmail.com>
Date: Thu, 14 Feb 2019 15:54:17 +0100
Subject: [PATCH] Use rustc-demangle in the stack traces

---
 Cargo.lock                         |  1 +
 Cargo.toml                         |  4 +++
 src/arch/x86_64/interrupt/trace.rs | 53 +++---------------------------
 src/lib.rs                         |  1 +
 4 files changed, 11 insertions(+), 48 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index 328f6699..96edceb3 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -179,6 +179,7 @@ dependencies = [
  "linked_list_allocator 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)",
  "raw-cpuid 4.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
  "redox_syscall 0.1.51",
+ "rustc-demangle 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)",
  "slab_allocator 0.3.1",
  "spin 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)",
  "x86 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
diff --git a/Cargo.toml b/Cargo.toml
index 8ca0a844..35cf50f4 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -22,6 +22,10 @@ version = "0.0.15"
 default-features = false
 features = ["elf32", "elf64"]
 
+[dependencies.rustc-demangle]
+version = "0.1.13"
+default-features = false
+
 [dependencies.x86]
 version = "0.9.0"
 default-features = false
diff --git a/src/arch/x86_64/interrupt/trace.rs b/src/arch/x86_64/interrupt/trace.rs
index 2bac8c71..0dec90b2 100644
--- a/src/arch/x86_64/interrupt/trace.rs
+++ b/src/arch/x86_64/interrupt/trace.rs
@@ -1,5 +1,6 @@
-use core::mem;
+use core::{mem, str};
 use goblin::elf::sym;
+use rustc_demangle::demangle;
 
 use paging::{ActivePageTable, VirtualAddress};
 
@@ -76,54 +77,10 @@ pub unsafe fn symbol_trace(addr: usize) {
                         }
 
                         if end > start {
-                            let sym_name = &elf.data[start .. end];
-
-                            print!("    ");
-
-                            if sym_name.starts_with(b"_ZN") {
-                                // Skip _ZN
-                                let mut i = 3;
-                                let mut first = true;
-                                while i < sym_name.len() {
-                                    // E is the end character
-                                    if sym_name[i] == b'E' {
-                                        break;
-                                    }
-
-                                    // Parse length string
-                                    let mut len = 0;
-                                    while i < sym_name.len() {
-                                        let b = sym_name[i];
-                                        if b >= b'0' && b <= b'9' {
-                                            i += 1;
-                                            len *= 10;
-                                            len += (b - b'0') as usize;
-                                        } else {
-                                            break;
-                                        }
-                                    }
-
-                                    // Print namespace seperator, if required
-                                    if first {
-                                        first = false;
-                                    } else {
-                                        print!("::");
-                                    }
-
-                                    // Print name string
-                                    let end = i + len;
-                                    while i < sym_name.len() && i < end {
-                                        print!("{}", sym_name[i] as char);
-                                        i += 1;
-                                    }
-                                }
-                            } else {
-                                for &b in sym_name.iter() {
-                                    print!("{}", b as char);
-                                }
+                            let sym_slice = &elf.data[start .. end - 1];
+                            if let Ok(sym_name) = str::from_utf8(sym_slice) {
+                                println!("    {:#}", demangle(sym_name));
                             }
-
-                            println!("");
                         }
                     }
                 }
diff --git a/src/lib.rs b/src/lib.rs
index af9c099a..6161003f 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -35,6 +35,7 @@ extern crate alloc;
 extern crate bitflags;
 extern crate goblin;
 extern crate linked_list_allocator;
+extern crate rustc_demangle;
 extern crate spin;
 #[cfg(feature = "slab")]
 extern crate slab_allocator;
-- 
GitLab