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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Segment out SymbolResolver to a trait #715

Merged
merged 9 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions aya/src/maps/stack_trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,21 @@ impl<'a, T: Borrow<MapData>> IntoIterator for &'a StackTraceMap<T> {
}
}

/// A resolver for symbols based on an address obtained from a stacktrace.
pub trait SymbolResolver {
/// Resolve a symbol for a given address, if possible.
fn resolve_sym(&self, addr: u64) -> Option<String>;
}

/// The simplest resolver: a direct map from addresses to strings.
pub type SimpleSymbolResolver = BTreeMap<u64, String>;

impl SymbolResolver for SimpleSymbolResolver {
fn resolve_sym(&self, addr: u64) -> Option<String> {
self.range(..=addr).next_back().map(|(_, s)| s.clone())
}
}

/// A kernel or user space stack trace.
///
/// See the [`StackTraceMap`] documentation for examples.
Expand All @@ -174,12 +189,9 @@ impl StackTrace {
/// You can use [`util::kernel_symbols()`](crate::util::kernel_symbols) to load kernel symbols. For
/// user-space traces you need to provide the symbols, for example loading
/// them from debug info.
pub fn resolve(&mut self, symbols: &BTreeMap<u64, String>) -> &StackTrace {
pub fn resolve<R: SymbolResolver>(&mut self, symbols: &R) -> &StackTrace {
for frame in self.frames.iter_mut() {
frame.symbol_name = symbols
.range(..=frame.ip)
.next_back()
.map(|(_, s)| s.clone())
frame.symbol_name = symbols.resolve_sym(frame.ip)
}

self
Expand Down
3 changes: 2 additions & 1 deletion aya/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::{
Pod,
};

use crate::maps::stack_trace::SimpleSymbolResolver;
use libc::{if_nametoindex, sysconf, uname, utsname, _SC_PAGESIZE};

/// Represents a kernel version, in major.minor.release version.
Expand Down Expand Up @@ -204,7 +205,7 @@ fn parse_cpu_ranges(data: &str) -> Result<Vec<u32>, ()> {
/// Loads kernel symbols from `/proc/kallsyms`.
///
/// The symbols can be passed to [`StackTrace::resolve`](crate::maps::stack_trace::StackTrace::resolve).
pub fn kernel_symbols() -> Result<BTreeMap<u64, String>, io::Error> {
pub fn kernel_symbols() -> Result<SimpleSymbolResolver, io::Error> {
let mut reader = BufReader::new(File::open("/proc/kallsyms")?);
parse_kernel_symbols(&mut reader)
}
Expand Down