From 2a1cf8931ea7642e2ba566e0e80dd0b2dd958b72 Mon Sep 17 00:00:00 2001 From: Anton Sviridov Date: Thu, 7 Mar 2024 09:57:28 +0000 Subject: [PATCH] Add scala native demangling and tests --- Cargo.lock | 7 +++++ samply-symbols/Cargo.toml | 1 + samply-symbols/src/demangle.rs | 54 ++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index e95df83b..d72982c6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1774,6 +1774,7 @@ dependencies = [ "pdb-addr2line", "rangemap", "rustc-demangle", + "scala-native-demangle", "srcsrv", "thiserror", "uuid", @@ -1783,6 +1784,12 @@ dependencies = [ "zerocopy-derive", ] +[[package]] +name = "scala-native-demangle" +version = "0.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0a4416eddc0eaf31e04aa4039bd3db4288ea1ba613955d86cf9c310049c5d1e2" + [[package]] name = "scopeguard" version = "1.2.0" diff --git a/samply-symbols/Cargo.toml b/samply-symbols/Cargo.toml index 25698e1b..5cdfa36e 100644 --- a/samply-symbols/Cargo.toml +++ b/samply-symbols/Cargo.toml @@ -38,6 +38,7 @@ thiserror = "1.0.57" cpp_demangle = "0.4.0" msvc-demangler = "0.10.0" rustc-demangle = "0.1.21" +scala-native-demangle = "0.0.6" bitflags = "2" bytesize = { version = "1.0.1", optional = true } bitvec = { version = "1.0.0", optional = true } diff --git a/samply-symbols/src/demangle.rs b/samply-symbols/src/demangle.rs index 5d35d9b6..975892fa 100644 --- a/samply-symbols/src/demangle.rs +++ b/samply-symbols/src/demangle.rs @@ -14,6 +14,12 @@ pub fn demangle_any(name: &str) -> String { return msvc_demangler::demangle(name, flags).unwrap_or_else(|_| name.to_string()); } + if name.starts_with("__S") { + if let Ok(symbol) = scala_native_demangle::demangle_with_defaults(&name[1..name.len()]) { + return symbol; + } + } + if let Ok(demangled_symbol) = rustc_demangle::try_demangle(name) { return format!("{demangled_symbol:#}"); } @@ -37,3 +43,51 @@ pub fn demangle_any(name: &str) -> String { name.to_owned() } + +#[cfg(test)] +mod tests { + use crate::demangle::demangle_any; + #[test] + fn cpp_demangling() { + assert_eq!( + demangle_any("_ZNK8KxVectorI16KxfArcFileRecordjEixEj"), + "KxVector::operator[](unsigned int) const" + ) + } + + #[test] + fn mscvc_demangling() { + assert_eq!( + demangle_any("??_R3?$KxSet@V?$KxSpe@DI@@I@@8"), + "KxSet, unsigned int>::`RTTI Class Hierarchy Descriptor'" + ) + } + + #[test] + fn rust_demangling() { + assert_eq!( + demangle_any( + "_RNvMsr_NtCs3ssYzQotkvD_3std4pathNtB5_7PathBuf3newCs15kBYyAo9fc_7mycrate" + ), + "::new" + ) + } + + #[test] + fn ocaml_demangling() { + assert_eq!(demangle_any("camlA__b__c_1002"), "A.b.c_1002") + } + + #[test] + fn scala_native_demangling() { + assert_eq!( + demangle_any("__SM17java.lang.IntegerD7compareiiiEo"), + "java.lang.Integer.compare(Int,Int): Int" + ) + } + + #[test] + fn no_demangling() { + assert_eq!(demangle_any("_!!!!!!!bla"), "!!!!!!!bla") + } +}