Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to not demangle symbols #607

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
45 changes: 44 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,42 @@ 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()
}

/// Returns the raw symbol name as a list of bytes
pub fn as_bytes(&self) -> &'a [u8] {
self.bytes
}
}
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