Skip to content

Commit

Permalink
Introduce ForEachFn typedef
Browse files Browse the repository at this point in the history
Introduce the ForEachFn typedef to the inspect functionality. Having
this typedef in place will make it easier to adjust the signature of the
user provided callback/iteration function.

Signed-off-by: Daniel Müller <deso@posteo.net>
  • Loading branch information
d-e-s-o authored and danielocfb committed Oct 3, 2024
1 parent a3e2eea commit 7c7be13
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 18 deletions.
3 changes: 2 additions & 1 deletion src/breakpad/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::path::Path;
use std::path::PathBuf;

use crate::inspect::FindAddrOpts;
use crate::inspect::ForEachFn;
use crate::inspect::Inspect;
use crate::inspect::SymInfo;
use crate::mmap::Mmap;
Expand Down Expand Up @@ -221,7 +222,7 @@ impl Inspect for BreakpadResolver {
}

/// Perform an operation on each symbol.
fn for_each(&self, opts: &FindAddrOpts, f: &mut dyn FnMut(&SymInfo<'_>)) -> Result<()> {
fn for_each(&self, opts: &FindAddrOpts, f: &mut ForEachFn<'_>) -> Result<()> {
if let SymType::Variable = opts.sym_type {
return Err(Error::with_unsupported(
"breakpad logic does not currently support variable iteration",
Expand Down
3 changes: 2 additions & 1 deletion src/dwarf/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use crate::elf::ElfParser;
use crate::elf::DEFAULT_DEBUG_DIRS;
use crate::error::IntoCowStr;
use crate::inspect::FindAddrOpts;
use crate::inspect::ForEachFn;
use crate::inspect::Inspect;
use crate::inspect::SymInfo;
use crate::log::debug;
Expand Down Expand Up @@ -328,7 +329,7 @@ impl Inspect for DwarfResolver {
}
}

fn for_each(&self, _opts: &FindAddrOpts, _f: &mut dyn FnMut(&SymInfo<'_>)) -> Result<()> {
fn for_each(&self, _opts: &FindAddrOpts, _f: &mut ForEachFn<'_>) -> Result<()> {
// TODO: Implement this functionality.
Err(Error::with_unsupported(
"DWARF logic does not currently support symbol iteration",
Expand Down
13 changes: 5 additions & 8 deletions src/elf/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::path::PathBuf;

use crate::insert_map::InsertMap;
use crate::inspect::FindAddrOpts;
use crate::inspect::ForEachFn;
use crate::inspect::SymInfo;
use crate::mmap::Mmap;
use crate::once::OnceCell;
Expand Down Expand Up @@ -790,7 +791,7 @@ impl ElfParser {
opts: &FindAddrOpts,
syms: &[&Elf64_Sym],
str2sym: &[(&str, usize)],
f: &mut dyn FnMut(&SymInfo<'_>),
f: &mut ForEachFn<'_>,
) -> Result<()> {
let shdrs = self.cache.ensure_shdrs()?;

Expand Down Expand Up @@ -823,18 +824,14 @@ impl ElfParser {

/// Perform an operation on each symbol.
#[allow(clippy::needless_borrows_for_generic_args)]
pub(crate) fn for_each(
&self,
opts: &FindAddrOpts,
mut f: &mut dyn FnMut(&SymInfo<'_>),
) -> Result<()> {
pub(crate) fn for_each(&self, opts: &FindAddrOpts, f: &mut ForEachFn) -> Result<()> {
let symtab = self.cache.ensure_symtab()?;
let str2symtab = self.cache.ensure_str2symtab()?;
let () = self.for_each_sym_impl(opts, symtab, str2symtab, &mut f)?;
let () = self.for_each_sym_impl(opts, symtab, str2symtab, f)?;

let dynsym = self.cache.ensure_dynsym()?;
let str2dynsym = self.cache.ensure_str2dynsym()?;
let () = self.for_each_sym_impl(opts, dynsym, str2dynsym, &mut f)?;
let () = self.for_each_sym_impl(opts, dynsym, str2dynsym, f)?;

Ok(())
}
Expand Down
3 changes: 2 additions & 1 deletion src/elf/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::dwarf::DwarfResolver;
use crate::elf::DEFAULT_DEBUG_DIRS;
use crate::file_cache::FileCache;
use crate::inspect::FindAddrOpts;
use crate::inspect::ForEachFn;
use crate::inspect::Inspect;
use crate::inspect::SymInfo;
use crate::once::OnceCell;
Expand Down Expand Up @@ -201,7 +202,7 @@ impl Inspect for ElfResolver {
}
}

fn for_each(&self, opts: &FindAddrOpts, f: &mut dyn FnMut(&SymInfo<'_>)) -> Result<()> {
fn for_each(&self, opts: &FindAddrOpts, f: &mut ForEachFn<'_>) -> Result<()> {
let parser = self.parser();
parser.deref().for_each(opts, f)
}
Expand Down
7 changes: 2 additions & 5 deletions src/inspect/inspector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::breakpad::BreakpadResolver;
use crate::elf::ElfResolverData;
use crate::elf::DEFAULT_DEBUG_DIRS;
use crate::file_cache::FileCache;
use crate::inspect::ForEachFn;
use crate::Result;

#[cfg(feature = "breakpad")]
Expand Down Expand Up @@ -164,11 +165,7 @@ impl Inspector {
where
F: FnMut(&SymInfo<'_>),
{
fn for_each_impl(
slf: &Inspector,
src: &Source,
f: &mut dyn FnMut(&SymInfo<'_>),
) -> Result<()> {
fn for_each_impl(slf: &Inspector, src: &Source, f: &mut ForEachFn<'_>) -> Result<()> {
let (resolver, opts) = match src {
#[cfg(feature = "breakpad")]
Source::Breakpad(Breakpad {
Expand Down
6 changes: 5 additions & 1 deletion src/inspect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ pub(crate) struct FindAddrOpts {
}


/// The signature of a function for iterating over symbols.
pub(crate) type ForEachFn<'f> = dyn FnMut(&SymInfo<'_>) + 'f;


/// The trait providing inspection functionality.
pub(crate) trait Inspect
where
Expand All @@ -95,5 +99,5 @@ where
fn find_addr(&self, name: &str, opts: &FindAddrOpts) -> Result<Vec<SymInfo<'_>>>;

/// Perform an operation on each symbol.
fn for_each(&self, opts: &FindAddrOpts, f: &mut dyn FnMut(&SymInfo<'_>)) -> Result<()>;
fn for_each(&self, opts: &FindAddrOpts, f: &mut ForEachFn<'_>) -> Result<()>;
}
3 changes: 2 additions & 1 deletion src/ksym.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::path::Path;
use std::path::PathBuf;

use crate::inspect::FindAddrOpts;
use crate::inspect::ForEachFn;
use crate::inspect::Inspect;
use crate::inspect::SymInfo;
use crate::once::OnceCell;
Expand Down Expand Up @@ -180,7 +181,7 @@ impl Inspect for KSymResolver {
Ok(syms)
}

fn for_each(&self, opts: &FindAddrOpts, f: &mut dyn FnMut(&SymInfo<'_>)) -> Result<()> {
fn for_each(&self, opts: &FindAddrOpts, f: &mut ForEachFn<'_>) -> Result<()> {
if let SymType::Variable = opts.sym_type {
return Ok(())
}
Expand Down

0 comments on commit 7c7be13

Please sign in to comment.