Skip to content

Commit

Permalink
Introduce KsymResolver::load_from_reader() constructor
Browse files Browse the repository at this point in the history
Introduce the KsymResolver::load_from_reader() constructor, which:
1) allows us to better test the resolver, because we can provide an
   in-memory buffer
2) addresses a deficiency where, in the Symbolizer, we were effectively
   circumventing our FileCache construct and re-opening the kallsyms
   file by path

Signed-off-by: Daniel Müller <deso@posteo.net>
  • Loading branch information
d-e-s-o authored and danielocfb committed Oct 8, 2024
1 parent 9216114 commit 7475bee
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
24 changes: 17 additions & 7 deletions src/ksym.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ use std::borrow::Cow;
use std::fmt::Debug;
use std::fmt::Formatter;
use std::fmt::Result as FmtResult;
#[cfg(test)]
use std::fs::File;
use std::io::BufRead;
use std::io::BufRead as _;
use std::io::BufReader;
use std::io::Read;
use std::ops::ControlFlow;
use std::path::Path;
use std::path::PathBuf;
Expand Down Expand Up @@ -70,17 +72,25 @@ impl<'ksym> From<&'ksym Ksym> for SymInfo<'ksym> {
///
/// The users should provide the path of kallsyms, so you can provide
/// a copy from other devices.
pub struct KSymResolver {
pub(crate) struct KSymResolver {
/// An index over `syms` that is sorted by name.
by_name_idx: OnceCell<Box<[usize]>>,
syms: Vec<Ksym>,
file_name: PathBuf,
}

impl KSymResolver {
pub(crate) fn load_file_name(filename: PathBuf) -> Result<Self> {
let f = File::open(&filename)?;
let mut reader = BufReader::new(f);
#[cfg(test)]
fn load_file_name(path: &Path) -> Result<Self> {
let f = File::open(path)?;
Self::load_from_reader(f, path)
}

pub fn load_from_reader<R>(reader: R, path: &Path) -> Result<Self>
where
R: Read,
{
let mut reader = BufReader::new(reader);
let mut line = String::new();
let mut syms = Vec::with_capacity(DFL_KSYM_CAP);

Expand Down Expand Up @@ -111,7 +121,7 @@ impl KSymResolver {
let slf = Self {
syms,
by_name_idx: OnceCell::new(),
file_name: filename,
file_name: path.to_path_buf(),
};
Ok(slf)
}
Expand Down Expand Up @@ -235,7 +245,7 @@ mod tests {
/// Check that we can use a `KSymResolver` to find symbols.
#[test]
fn ksym_resolver_load_find() {
let result = KSymResolver::load_file_name(PathBuf::from(KALLSYMS));
let result = KSymResolver::load_file_name(Path::new(KALLSYMS));
let resolver = match result {
Ok(resolver) => resolver,
Err(err) if err.kind() == ErrorKind::NotFound => return,
Expand Down
5 changes: 2 additions & 3 deletions src/symbolize/symbolizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -879,9 +879,8 @@ impl Symbolizer {
Ok(handler.all_symbols)
}

fn create_ksym_resolver(&self, path: &Path, _file: &File) -> Result<Rc<KSymResolver>> {
// TODO: Should really use `file` and not `path` for the instantiation.
let resolver = KSymResolver::load_file_name(path.to_path_buf())?;
fn create_ksym_resolver(&self, path: &Path, file: &File) -> Result<Rc<KSymResolver>> {
let resolver = KSymResolver::load_from_reader(file, path)?;
let resolver = Rc::new(resolver);
Ok(resolver)
}
Expand Down

0 comments on commit 7475bee

Please sign in to comment.