1 //! A simple example of parsing `.debug_info`.
2 
3 use object::{Object, ObjectSection};
4 use std::{borrow, env, fs};
5 
main()6 fn main() {
7     for path in env::args().skip(1) {
8         let file = fs::File::open(&path).unwrap();
9         let mmap = unsafe { memmap::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 
dump_file(object: &object::File, endian: gimli::RunTimeEndian) -> Result<(), gimli::Error>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     // Load a supplementary section. We don't have a supplementary object file,
31     // so always return an empty slice.
32     let load_section_sup = |_| Ok(borrow::Cow::Borrowed(&[][..]));
33 
34     // Load all of the sections.
35     let dwarf_cow = gimli::Dwarf::load(&load_section, &load_section_sup)?;
36 
37     // Borrow a `Cow<[u8]>` to create an `EndianSlice`.
38     let borrow_section: &dyn for<'a> Fn(
39         &'a borrow::Cow<[u8]>,
40     ) -> gimli::EndianSlice<'a, gimli::RunTimeEndian> =
41         &|section| gimli::EndianSlice::new(&*section, endian);
42 
43     // Create `EndianSlice`s for all of the sections.
44     let dwarf = dwarf_cow.borrow(&borrow_section);
45 
46     // Iterate over the compilation units.
47     let mut iter = dwarf.units();
48     while let Some(header) = iter.next()? {
49         println!(
50             "Unit at <.debug_info+0x{:x}>",
51             header.offset().as_debug_info_offset().unwrap().0
52         );
53         let unit = dwarf.unit(header)?;
54 
55         // Iterate over the Debugging Information Entries (DIEs) in the unit.
56         let mut depth = 0;
57         let mut entries = unit.entries();
58         while let Some((delta_depth, entry)) = entries.next_dfs()? {
59             depth += delta_depth;
60             println!("<{}><{:x}> {}", depth, entry.offset().0, entry.tag());
61 
62             // Iterate over the attributes in the DIE.
63             let mut attrs = entry.attrs();
64             while let Some(attr) = attrs.next()? {
65                 println!("   {}: {:?}", attr.name(), attr.value());
66             }
67         }
68     }
69     Ok(())
70 }
71