]> git.proxmox.com Git - rustc.git/blob - vendor/gimli/examples/simple.rs
New upstream version 1.71.1+dfsg1
[rustc.git] / vendor / gimli / examples / simple.rs
1 //! A simple example of parsing `.debug_info`.
2
3 use object::{Object, ObjectSection};
4 use std::{borrow, env, fs};
5
6 fn main() {
7 for path in env::args().skip(1) {
8 let file = fs::File::open(&path).unwrap();
9 let mmap = unsafe { memmap2::Mmap::map(&file).unwrap() };
10 let object = object::File::parse(&*mmap).unwrap();
11 let endian = if object.is_little_endian() {
12 gimli::RunTimeEndian::Little
13 } else {
14 gimli::RunTimeEndian::Big
15 };
16 dump_file(&object, endian).unwrap();
17 }
18 }
19
20 fn dump_file(object: &object::File, endian: gimli::RunTimeEndian) -> Result<(), gimli::Error> {
21 // Load a section and return as `Cow<[u8]>`.
22 let load_section = |id: gimli::SectionId| -> Result<borrow::Cow<[u8]>, gimli::Error> {
23 match object.section_by_name(id.name()) {
24 Some(ref section) => Ok(section
25 .uncompressed_data()
26 .unwrap_or(borrow::Cow::Borrowed(&[][..]))),
27 None => Ok(borrow::Cow::Borrowed(&[][..])),
28 }
29 };
30
31 // Load all of the sections.
32 let dwarf_cow = gimli::Dwarf::load(&load_section)?;
33
34 // Borrow a `Cow<[u8]>` to create an `EndianSlice`.
35 let borrow_section: &dyn for<'a> Fn(
36 &'a borrow::Cow<[u8]>,
37 ) -> gimli::EndianSlice<'a, gimli::RunTimeEndian> =
38 &|section| gimli::EndianSlice::new(&*section, endian);
39
40 // Create `EndianSlice`s for all of the sections.
41 let dwarf = dwarf_cow.borrow(&borrow_section);
42
43 // Iterate over the compilation units.
44 let mut iter = dwarf.units();
45 while let Some(header) = iter.next()? {
46 println!(
47 "Unit at <.debug_info+0x{:x}>",
48 header.offset().as_debug_info_offset().unwrap().0
49 );
50 let unit = dwarf.unit(header)?;
51
52 // Iterate over the Debugging Information Entries (DIEs) in the unit.
53 let mut depth = 0;
54 let mut entries = unit.entries();
55 while let Some((delta_depth, entry)) = entries.next_dfs()? {
56 depth += delta_depth;
57 println!("<{}><{:x}> {}", depth, entry.offset().0, entry.tag());
58
59 // Iterate over the attributes in the DIE.
60 let mut attrs = entry.attrs();
61 while let Some(attr) = attrs.next()? {
62 println!(" {}: {:?}", attr.name(), attr.value());
63 }
64 }
65 }
66 Ok(())
67 }