Skip to content

Commit

Permalink
Add option to not demangle symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisDenton committed Mar 29, 2024
1 parent adc9f5c commit 39463ae
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/symbolize/dbghelp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#![allow(bad_style)]

use super::super::{dbghelp, windows::*};
use super::{BytesOrWideString, ResolveWhat, SymbolName};
use super::{BytesOrWideString, RawSymbolName, ResolveWhat};
use core::char;
use core::ffi::c_void;
use core::marker;
Expand Down Expand Up @@ -47,8 +47,8 @@ pub struct Symbol<'a> {
}

impl Symbol<'_> {
pub fn name(&self) -> Option<SymbolName<'_>> {
Some(SymbolName::new(unsafe { &*self.name }))
pub fn name_raw(&self) -> Option<RawSymbolName<'_>> {
Some(RawSymbolName::new(unsafe { &*self.name }))
}

pub fn addr(&self) -> Option<*mut c_void> {
Expand Down
8 changes: 4 additions & 4 deletions src/symbolize/gimli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use self::gimli::NativeEndian as Endian;
use self::mmap::Mmap;
use self::stash::Stash;
use super::BytesOrWideString;
use super::RawSymbolName;
use super::ResolveWhat;
use super::SymbolName;
use addr2line::gimli;
use core::convert::TryInto;
use core::mem;
Expand Down Expand Up @@ -492,13 +492,13 @@ pub enum Symbol<'a> {
}

impl Symbol<'_> {
pub fn name(&self) -> Option<SymbolName<'_>> {
pub fn name_raw(&self) -> Option<RawSymbolName<'_>> {
match self {
Symbol::Frame { name, .. } => {
let name = name.as_ref()?;
Some(SymbolName::new(name))
Some(RawSymbolName::new(name))
}
Symbol::Symtab { name, .. } => Some(SymbolName::new(name)),
Symbol::Symtab { name, .. } => Some(RawSymbolName::new(name)),
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/symbolize/miri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use core::marker::PhantomData;

use super::super::backtrace::miri::{resolve_addr, Frame};
use super::BytesOrWideString;
use super::{ResolveWhat, SymbolName};
use super::{RawSymbolName, ResolveWhat};

pub unsafe fn resolve(what: ResolveWhat<'_>, cb: &mut dyn FnMut(&super::Symbol)) {
let sym = match what {
Expand All @@ -25,8 +25,8 @@ pub struct Symbol<'a> {
}

impl<'a> Symbol<'a> {
pub fn name(&self) -> Option<SymbolName<'_>> {
Some(SymbolName::new(&self.inner.inner.name))
pub fn name_raw(&self) -> Option<RawSymbolName<'_>> {
Some(RawSymbolName::new(&self.inner.inner.name))
}

pub fn addr(&self) -> Option<*mut c_void> {
Expand Down
40 changes: 39 additions & 1 deletion src/symbolize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,14 @@ impl Symbol {
/// utf-8).
/// * The raw bytes for the symbol name can be accessed.
pub fn name(&self) -> Option<SymbolName<'_>> {
self.inner.name()
self.name_raw().map(|s| s.demangle())
}

/// Returns the raw name of this function.
///
/// This is similar to `name` but doesn't do any demangling.
pub fn name_raw(&self) -> Option<RawSymbolName<'_>> {
self.inner.name_raw()
}

/// Returns the starting address of this function.
Expand Down Expand Up @@ -356,6 +363,37 @@ impl<'a> SymbolName<'a> {
}
}

/// A wrapper around the raw, mangled, symbol name.
pub struct RawSymbolName<'a> {
bytes: &'a [u8],
}
impl<'a> RawSymbolName<'a> {
/// Creates a new raw symbol name from the raw underlying bytes.
pub fn new(bytes: &'a [u8]) -> RawSymbolName<'a> {
RawSymbolName { bytes }
}

/// Attempt to demangle the symbol name.
pub fn demangle(self) -> SymbolName<'a> {
SymbolName::new(self.bytes)
}

/// Returns the raw (mangled) symbol name as a `str` if the symbol is valid utf-8.
pub fn as_str(&self) -> Option<&'a str> {
str::from_utf8(self.bytes).ok()
}
}
impl<'a> fmt::Display for RawSymbolName<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
format_symbol_name(fmt::Display::fmt, self.bytes, f)
}
}
impl<'a> fmt::Debug for RawSymbolName<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
format_symbol_name(fmt::Debug::fmt, self.bytes, f)
}
}

fn format_symbol_name(
fmt: fn(&str, &mut fmt::Formatter<'_>) -> fmt::Result,
mut bytes: &[u8],
Expand Down
4 changes: 2 additions & 2 deletions src/symbolize/noop.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Empty symbolication strategy used to compile for platforms that have no
//! support.

use super::{BytesOrWideString, ResolveWhat, SymbolName};
use super::{BytesOrWideString, RawSymbolName, ResolveWhat};
use core::ffi::c_void;
use core::marker;

Expand All @@ -12,7 +12,7 @@ pub struct Symbol<'a> {
}

impl Symbol<'_> {
pub fn name(&self) -> Option<SymbolName<'_>> {
pub fn name_raw(&self) -> Option<RawSymbolName<'_>> {
None
}

Expand Down

0 comments on commit 39463ae

Please sign in to comment.