summaryrefslogtreecommitdiffstats
path: root/vendor/gimli/examples/simple.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /vendor/gimli/examples/simple.rs
parentInitial commit. (diff)
downloadrustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz
rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/gimli/examples/simple.rs')
-rw-r--r--vendor/gimli/examples/simple.rs67
1 files changed, 67 insertions, 0 deletions
diff --git a/vendor/gimli/examples/simple.rs b/vendor/gimli/examples/simple.rs
new file mode 100644
index 000000000..b73ab7552
--- /dev/null
+++ b/vendor/gimli/examples/simple.rs
@@ -0,0 +1,67 @@
+//! A simple example of parsing `.debug_info`.
+
+use object::{Object, ObjectSection};
+use std::{borrow, env, fs};
+
+fn main() {
+ for path in env::args().skip(1) {
+ let file = fs::File::open(&path).unwrap();
+ let mmap = unsafe { memmap::Mmap::map(&file).unwrap() };
+ let object = object::File::parse(&*mmap).unwrap();
+ let endian = if object.is_little_endian() {
+ gimli::RunTimeEndian::Little
+ } else {
+ gimli::RunTimeEndian::Big
+ };
+ dump_file(&object, endian).unwrap();
+ }
+}
+
+fn dump_file(object: &object::File, endian: gimli::RunTimeEndian) -> Result<(), gimli::Error> {
+ // Load a section and return as `Cow<[u8]>`.
+ let load_section = |id: gimli::SectionId| -> Result<borrow::Cow<[u8]>, gimli::Error> {
+ match object.section_by_name(id.name()) {
+ Some(ref section) => Ok(section
+ .uncompressed_data()
+ .unwrap_or(borrow::Cow::Borrowed(&[][..]))),
+ None => Ok(borrow::Cow::Borrowed(&[][..])),
+ }
+ };
+
+ // Load all of the sections.
+ let dwarf_cow = gimli::Dwarf::load(&load_section)?;
+
+ // Borrow a `Cow<[u8]>` to create an `EndianSlice`.
+ let borrow_section: &dyn for<'a> Fn(
+ &'a borrow::Cow<[u8]>,
+ ) -> gimli::EndianSlice<'a, gimli::RunTimeEndian> =
+ &|section| gimli::EndianSlice::new(&*section, endian);
+
+ // Create `EndianSlice`s for all of the sections.
+ let dwarf = dwarf_cow.borrow(&borrow_section);
+
+ // Iterate over the compilation units.
+ let mut iter = dwarf.units();
+ while let Some(header) = iter.next()? {
+ println!(
+ "Unit at <.debug_info+0x{:x}>",
+ header.offset().as_debug_info_offset().unwrap().0
+ );
+ let unit = dwarf.unit(header)?;
+
+ // Iterate over the Debugging Information Entries (DIEs) in the unit.
+ let mut depth = 0;
+ let mut entries = unit.entries();
+ while let Some((delta_depth, entry)) = entries.next_dfs()? {
+ depth += delta_depth;
+ println!("<{}><{:x}> {}", depth, entry.offset().0, entry.tag());
+
+ // Iterate over the attributes in the DIE.
+ let mut attrs = entry.attrs();
+ while let Some(attr) = attrs.next()? {
+ println!(" {}: {:?}", attr.name(), attr.value());
+ }
+ }
+ }
+ Ok(())
+}