forked from libbpf/blazesym
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example illustrating normalization output post-processing
Add an example illustrating how to post-process the output of address normalization to have virtual offsets instead of file offsets available. Refs: libbpf#835 Signed-off-by: Daniel Müller <deso@posteo.net>
- Loading branch information
1 parent
4d383c1
commit c1730b8
Showing
2 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
//! An example illustrating usage of the library to produce virtual offsets as | ||
//! opposed to file offsets from the normalization step by applying some | ||
//! post-processing. | ||
|
||
#![allow(clippy::fn_to_numeric_cast)] | ||
|
||
use blazesym::helper::ElfResolver; | ||
use blazesym::normalize::Normalizer; | ||
use blazesym::symbolize::Elf; | ||
use blazesym::symbolize::Input; | ||
use blazesym::symbolize::Source; | ||
use blazesym::symbolize::Symbolizer; | ||
use blazesym::symbolize::TranslateFileOffset as _; | ||
use blazesym::Addr; | ||
use blazesym::Pid; | ||
|
||
|
||
fn main() { | ||
let normalizer = Normalizer::new(); | ||
let normalized = normalizer | ||
.normalize_user_addrs(Pid::Slf, [main as Addr].as_slice()) | ||
.unwrap(); | ||
assert_eq!(normalized.outputs.len(), 1); | ||
|
||
// Normalization reports file offsets, but there are cases where virtual | ||
// addresses may be the more desirable output (e.g., when only split DWARF | ||
// information is available for later symbolization, which may not directly | ||
// be able to handle file offsets, but it does support virtual offsets). | ||
// Hence, we post-process the output to convert from one to the other. | ||
let (file_offset, meta_idx) = normalized.outputs[0]; | ||
// Find the meta data entry so that we can look at the binary to which | ||
// the file offset belongs directly. | ||
let meta = &normalized.meta[meta_idx]; | ||
let elf = meta.as_elf().unwrap(); | ||
let resolver = ElfResolver::open(&elf.path).unwrap(); | ||
// Translate the reported file offset into a virtual address. | ||
let virt_offset = resolver | ||
.file_offset_to_virt_offset(file_offset) | ||
.unwrap() | ||
.unwrap(); | ||
|
||
// Just for illustration purposes, symbolize the virtual offset now. | ||
let symbolizer = Symbolizer::new(); | ||
let src = Source::Elf(Elf::new(&elf.path)); | ||
let sym = symbolizer | ||
.symbolize_single(&src, Input::VirtOffset(virt_offset)) | ||
.unwrap() | ||
.into_sym() | ||
.unwrap(); | ||
assert_eq!(sym.name, "normalize_virt_offset::main"); | ||
} |