Skip to content

Commit

Permalink
Sym ref
Browse files Browse the repository at this point in the history
  • Loading branch information
d-e-s-o committed Nov 2, 2023
1 parent 28cf045 commit 572af94
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 60 deletions.
16 changes: 9 additions & 7 deletions src/dwarf/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::inspect::FindAddrOpts;
use crate::inspect::SymInfo;
use crate::inspect::SymType;
use crate::symbolize::AddrCodeInfo;
use crate::symbolize::FrameCodeInfo;
use crate::symbolize::CodeInfo;
use crate::symbolize::IntSym;
use crate::symbolize::SrcLang;
use crate::Addr;
Expand Down Expand Up @@ -111,11 +111,12 @@ impl DwarfResolver {
column,
} = direct_location;

let mut direct_code_info = FrameCodeInfo {
dir,
file,
let mut direct_code_info = CodeInfo {
dir: Some(Cow::Borrowed(dir)),
file: Cow::Borrowed(file),
line,
column: column.map(|col| col.try_into().unwrap_or(u16::MAX)),
_non_exhaustive: (),
};

let inlined = if inlined_fns {
Expand All @@ -131,11 +132,12 @@ impl DwarfResolver {
column,
} = location;

FrameCodeInfo {
dir,
file,
CodeInfo {
dir: Some(Cow::Borrowed(dir)),
file: Cow::Borrowed(file),
line,
column: column.map(|col| col.try_into().unwrap_or(u16::MAX)),
_non_exhaustive: (),
}
});

Expand Down
9 changes: 5 additions & 4 deletions src/gsym/resolver.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::borrow::Cow;
use std::fmt::Debug;
use std::fmt::Formatter;
use std::fmt::Result as FmtResult;
Expand All @@ -10,7 +11,7 @@ use crate::inspect::FindAddrOpts;
use crate::inspect::SymInfo;
use crate::mmap::Mmap;
use crate::symbolize::AddrCodeInfo;
use crate::symbolize::FrameCodeInfo;
use crate::symbolize::CodeInfo;
use crate::symbolize::IntSym;
use crate::symbolize::SrcLang;
use crate::Addr;
Expand Down Expand Up @@ -75,7 +76,7 @@ impl<'dat> GsymResolver<'dat> {
Ok(slf)
}

fn query_frame_code_info(&self, file_idx: u32, line: Option<u32>) -> Result<FrameCodeInfo<'_>> {
fn query_frame_code_info(&self, file_idx: u32, line: Option<u32>) -> Result<CodeInfo<'_>> {
let finfo = self
.ctx
.file_info(file_idx as usize)
Expand All @@ -93,8 +94,8 @@ impl<'dat> GsymResolver<'dat> {
format!("failed to retrieve file name string @ {}", finfo.filename)
})?;

let info = FrameCodeInfo {
dir: Path::new(dir),
let info = CodeInfo {
dir: Cow::Borrowed(Path::new(dir)),
file,
line,
column: None,
Expand Down
2 changes: 1 addition & 1 deletion src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ where
/// `inlined_fns` is true, information about inlined calls at the very
/// address will also be looked up and reported as the optional
/// [`AddrCodeInfo::inlined`] attribute.
fn find_code_info(&self, addr: Addr, inlined_fns: bool) -> Result<Option<AddrCodeInfo>>;
fn find_code_info(&self, addr: Addr, inlined_fns: bool) -> Result<Option<AddrCodeInfo<'_>>>;
}
56 changes: 18 additions & 38 deletions src/symbolize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,26 +155,6 @@ where
}


#[derive(Debug, PartialEq)]
pub(crate) struct FrameCodeInfo<'src> {
pub dir: &'src Path,
pub file: &'src OsStr,
pub line: Option<u32>,
pub column: Option<u16>,
}

impl From<&FrameCodeInfo<'_>> for CodeInfo {
fn from(other: &FrameCodeInfo<'_>) -> Self {
Self {
dir: Some(other.dir.to_path_buf()),
file: other.file.to_os_string(),
line: other.line,
column: other.column,
_non_exhaustive: (),
}
}
}

#[derive(Debug, PartialEq)]
pub(crate) struct AddrCodeInfo<'src> {
/// Source information about the top-level frame belonging to an
Expand All @@ -183,19 +163,19 @@ pub(crate) struct AddrCodeInfo<'src> {
/// It also contains an optional name, which is necessary for
/// formats where inline information can "correct" (overwrite) the
/// name of the symbol.
pub direct: (Option<&'src str>, FrameCodeInfo<'src>),
pub direct: (Option<&'src str>, CodeInfo<'src>),
/// Source information about inlined functions, along with their names.
pub inlined: Vec<(&'src str, Option<FrameCodeInfo<'src>>)>,
pub inlined: Vec<(&'src str, Option<CodeInfo<'src>>)>,
}


/// Source code location information for a symbol or inlined function.
#[derive(Clone, Debug, PartialEq)]
pub struct CodeInfo {
pub struct CodeInfo<'src> {
/// The directory in which the source file resides.
pub dir: Option<PathBuf>,
pub dir: Option<Cow<'src, Path>>,
/// The file that defines the symbol.
pub file: OsString,
pub file: Cow<'src, OsStr>,
/// The line number of the symbolized instruction in the source
/// code.
///
Expand All @@ -210,7 +190,7 @@ pub struct CodeInfo {
pub(crate) _non_exhaustive: (),
}

impl CodeInfo {
impl CodeInfo<'_> {
/// Helper method to retrieve the path to the represented source file,
/// on a best-effort basis. It depends on the symbolization source data
/// whether this path is absolute or relative and, if its the latter, what
Expand All @@ -228,11 +208,11 @@ impl CodeInfo {

/// A type representing an inlined function.
#[derive(Clone, Debug, PartialEq)]
pub struct InlinedFn {
pub struct InlinedFn<'src> {
/// The symbol name of the inlined function.
pub name: String,
pub name: Cow<'src, str>,
/// Source code location information for the call to the function.
pub code_info: Option<CodeInfo>,
pub code_info: Option<CodeInfo<'src>>,
/// The struct is non-exhaustive and open to extension.
pub(crate) _non_exhaustive: (),
}
Expand Down Expand Up @@ -266,9 +246,9 @@ pub(crate) struct IntSym<'src> {

/// The result of address symbolization by [`Symbolizer`].
#[derive(Clone, Debug, PartialEq)]
pub struct Sym {
pub struct Sym<'src> {
/// The symbol name that an address belongs to.
pub name: String,
pub name: Cow<'src, str>,
/// The address at which the symbol is located (i.e., its "start").
///
/// This is the "normalized" address of the symbol, as present in
Expand All @@ -288,7 +268,7 @@ pub struct Sym {
/// The symbol's size, if available.
pub size: Option<usize>,
/// Source code location information for the symbol.
pub code_info: Option<CodeInfo>,
pub code_info: Option<CodeInfo<'src>>,
/// Inlined function information, if requested and available.
///
/// Availability depends on both the underlying symbolization source (e.g.,
Expand All @@ -300,7 +280,7 @@ pub struct Sym {
/// falls into a function `f` at an inlined call to `g`, which in turn
/// contains an inlined call to `h`, the symbols will be reported in the
/// order `f`, `g`, `h`.
pub inlined: Box<[InlinedFn]>,
pub inlined: Box<[InlinedFn<'src>]>,
/// The struct is non-exhaustive and open to extension.
pub(crate) _non_exhaustive: (),
}
Expand All @@ -310,18 +290,18 @@ pub struct Sym {
// We keep this enum as exhaustive because additions to it, should they occur,
// are expected to be backwards-compatibility breaking.
#[derive(Clone, Debug, PartialEq)]
pub enum Symbolized {
pub enum Symbolized<'src> {
/// The input address was symbolized as the provided symbol.
Sym(Sym),
Sym(Sym<'src>),
/// The input address was not found and could not be symbolized.
Unknown,
}

impl Symbolized {
impl<'src> Symbolized<'src> {
/// Convert the object into a [`Sym`] reference, if the corresponding
/// variant is active.
#[inline]
pub fn as_sym(&self) -> Option<&Sym> {
pub fn as_sym(&self) -> Option<&Sym<'src>> {
match self {
Self::Sym(sym) => Some(sym),
Self::Unknown => None,
Expand All @@ -331,7 +311,7 @@ impl Symbolized {
/// Convert the object into a [`Sym`] object, if the corresponding variant
/// is active.
#[inline]
pub fn into_sym(self) -> Option<Sym> {
pub fn into_sym(self) -> Option<Sym<'src>> {
match self {
Self::Sym(sym) => Some(sym),
Self::Unknown => None,
Expand Down
17 changes: 7 additions & 10 deletions src/symbolize/symbolizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,11 @@ impl Symbolizer {

/// Symbolize an address using the provided [`SymResolver`].
#[cfg_attr(feature = "tracing", crate::log::instrument(skip_all, fields(addr = format_args!("{addr:#x}"), resolver = ?resolver)))]
fn symbolize_with_resolver(
&self,
fn symbolize_with_resolver<'slf>(
&'slf self,
addr: Addr,
resolver: &dyn SymResolver,
) -> Result<Symbolized> {
) -> Result<Symbolized<'slf>> {
let sym = if let Some(sym) = resolver.find_sym(addr)? {
sym
} else {
Expand All @@ -258,7 +258,7 @@ impl Symbolizer {
let (name, code_info) = if let Some(info) = &addr_code_info {
let name = info.direct.0;
let code_info = &info.direct.1;
(name, Some(CodeInfo::from(code_info)))
(name, Some(code_info))
} else {
(None, None)
};
Expand All @@ -276,9 +276,8 @@ impl Symbolizer {
.iter()
.map(|(name, info)| {
let name = self.maybe_demangle(name, lang);
let info = info.as_ref().map(CodeInfo::from);
InlinedFn {
name: name.to_string(),
name,
code_info: info,
_non_exhaustive: (),
}
Expand All @@ -289,9 +288,7 @@ impl Symbolizer {
};

let sym = Sym {
name: self
.maybe_demangle(name.unwrap_or(sym_name), lang)
.to_string(),
name: self.maybe_demangle(name.unwrap_or(sym_name), lang),
addr: sym_addr,
offset: (addr - sym_addr) as usize,
size: sym_size,
Expand Down Expand Up @@ -420,7 +417,7 @@ impl Symbolizer {
/// The "outer" `Symbolizer` instance.
symbolizer: &'sym Symbolizer,
/// Symbols representing the symbolized addresses.
all_symbols: Vec<Symbolized>,
all_symbols: Vec<Symbolized<'sym>>,
}

impl SymbolizeHandler<'_> {
Expand Down

0 comments on commit 572af94

Please sign in to comment.