Skip to content

Commit

Permalink
Introduce Sym::to_path() helper method
Browse files Browse the repository at this point in the history
This change introduces a helper method for retrieving the source file
path to the symbol to the Sym type. We adjust the relevant code snippets
to make use of this method as well.

Signed-off-by: Daniel Müller <[email protected]>
  • Loading branch information
d-e-s-o authored and danielocfb committed Sep 18, 2023
1 parent 8754cb5 commit 45ac4c4
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 41 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Unreleased
----------
- Added `size` member to `symbolize::Sym` type
- Added `size` member and `to_path` helper to `symbolize::Sym` type


0.2.0-alpha.6
Expand Down
11 changes: 2 additions & 9 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

mod args;

use std::path::PathBuf;

use anyhow::Context;
use anyhow::Result;

Expand Down Expand Up @@ -109,14 +107,9 @@ fn symbolize(symbolize: args::Symbolize) -> Result<()> {

let symbolize::Sym {
name, addr, offset, ..
} = sym;

let path = match (sym.dir, sym.file) {
(Some(dir), Some(file)) => Some(dir.join(file)),
(dir, file) => dir.or_else(|| file.map(PathBuf::from)),
};
} = &sym;

let src_loc = if let (Some(path), Some(line)) = (path, sym.line) {
let src_loc = if let (Some(path), Some(line)) = (sym.to_path(), sym.line) {
if let Some(col) = sym.column {
format!(" {}:{line}:{col}", path.display())
} else {
Expand Down
10 changes: 2 additions & 8 deletions examples/addr2ln.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::env;
use std::path::PathBuf;

use anyhow::bail;
use anyhow::Context as _;
Expand Down Expand Up @@ -45,14 +44,9 @@ fn main() -> Result<()> {

let Sym {
name, addr, offset, ..
} = sym;
} = &sym;

let path = match (sym.dir, sym.file) {
(Some(dir), Some(file)) => Some(dir.join(file)),
(dir, file) => dir.or_else(|| file.map(PathBuf::from)),
};

let src_loc = if let (Some(path), Some(line)) = (path, sym.line) {
let src_loc = if let (Some(path), Some(line)) = (sym.to_path(), sym.line) {
if let Some(col) = sym.column {
format!(" {}:{line}:{col}", path.display())
} else {
Expand Down
10 changes: 2 additions & 8 deletions examples/addr2ln_pid.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::env;
use std::path::PathBuf;

use anyhow::bail;
use anyhow::Context as _;
Expand Down Expand Up @@ -48,14 +47,9 @@ print its symbol, the file name of the source, and the line number.",

let Sym {
name, addr, offset, ..
} = sym;
} = &sym;

let path = match (sym.dir, sym.file) {
(Some(dir), Some(file)) => Some(dir.join(file)),
(dir, file) => dir.or_else(|| file.map(PathBuf::from)),
};

let src_loc = if let (Some(path), Some(line)) = (path, sym.line) {
let src_loc = if let (Some(path), Some(line)) = (sym.to_path(), sym.line) {
if let Some(col) = sym.column {
format!(" {}:{line}:{col}", path.display())
} else {
Expand Down
10 changes: 2 additions & 8 deletions examples/backtrace.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::cmp::min;
use std::mem::size_of;
use std::mem::transmute;
use std::path::PathBuf;
use std::ptr;

use blazesym::symbolize::Process;
Expand Down Expand Up @@ -40,14 +39,9 @@ fn symbolize_current_bt() {

let Sym {
name, addr, offset, ..
} = sym;
} = &sym;

let path = match (sym.dir, sym.file) {
(Some(dir), Some(file)) => Some(dir.join(file)),
(dir, file) => dir.or_else(|| file.map(PathBuf::from)),
};

let src_loc = if let (Some(path), Some(line)) = (path, sym.line) {
let src_loc = if let (Some(path), Some(line)) = (sym.to_path(), sym.line) {
if let Some(col) = sym.column {
format!(" {}:{line}:{col}", path.display())
} else {
Expand Down
9 changes: 2 additions & 7 deletions src/symbolize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,9 @@
//!
//! let Sym {
//! name, addr, offset, ..
//! } = sym;
//! } = &sym;
//!
//! let path = match (sym.dir, sym.file) {
//! (Some(dir), Some(file)) => Some(dir.join(file)),
//! (dir, file) => dir.or_else(|| file.map(PathBuf::from)),
//! };
//!
//! let src_loc = if let (Some(path), Some(line)) = (path, sym.line) {
//! let src_loc = if let (Some(path), Some(line)) = (sym.to_path(), sym.line) {
//! if let Some(col) = sym.column {
//! format!(" {}:{line}:{col}", path.display())
//! } else {
Expand Down
49 changes: 49 additions & 0 deletions src/symbolize/symbolizer.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::borrow::Cow;
use std::ffi::OsStr;
use std::ffi::OsString;
use std::fmt::Debug;
Expand Down Expand Up @@ -110,6 +111,25 @@ pub struct Sym {
pub(crate) _non_exhaustive: (),
}

impl Sym {
/// 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
/// directory it is relative to. In general this path is mostly intended for
/// displaying purposes.
///
/// # Notes
/// For the unlikely case that only a source code directory is present, no
/// file path will be reported.
#[inline]
pub fn to_path(&self) -> Option<Cow<'_, Path>> {
self.file.as_ref().map(|f| match &self.dir {
Some(dir) => Cow::Owned(dir.join(f)),
None => Cow::Borrowed(Path::new(f)),
})
}
}


/// A builder for configurable construction of [`Symbolizer`] objects.
///
Expand Down Expand Up @@ -501,6 +521,35 @@ mod tests {
use test_log::test;


/// Check that we can correctly construct the source code path to a symbol.
#[test]
fn symbol_source_code_path() {
let mut sym = Sym {
name: "symbol".to_string(),
addr: 0x1337,
offset: 42,
size: Some(43),
dir: None,
file: None,
line: Some(1),
column: Some(2),
_non_exhaustive: (),
};
assert_eq!(sym.to_path(), None);

sym.dir = Some(PathBuf::from("/foobar"));
assert_eq!(sym.to_path(), None);

sym.file = Some(OsString::from("source.c"));
assert_eq!(
sym.to_path().as_deref(),
Some(Path::new("/foobar/source.c"))
);

sym.dir = None;
assert_eq!(sym.to_path().as_deref(), Some(Path::new("source.c")));
}

/// Check that we can symbolize an address residing in a zip archive.
#[test]
fn symbolize_zip() {
Expand Down

0 comments on commit 45ac4c4

Please sign in to comment.