-
Notifications
You must be signed in to change notification settings - Fork 109
/
simple_line.rs
102 lines (89 loc) · 3.93 KB
/
simple_line.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//! A simple example of parsing `.debug_line`.
use object::{Object, ObjectSection};
use std::{borrow, env, error, fs, path};
fn main() {
for path in env::args().skip(1) {
let file = fs::File::open(&path).unwrap();
let mmap = unsafe { memmap2::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<(), Box<dyn error::Error>> {
// Load a section and return as `Cow<[u8]>`.
let load_section = |id: gimli::SectionId| -> Result<borrow::Cow<[u8]>, Box<dyn error::Error>> {
Ok(match object.section_by_name(id.name()) {
Some(section) => section.uncompressed_data()?,
None => borrow::Cow::Borrowed(&[]),
})
};
// Borrow a `Cow<[u8]>` to create an `EndianSlice`.
let borrow_section = |section| gimli::EndianSlice::new(borrow::Cow::as_ref(section), endian);
// Load all of the sections.
let dwarf_sections = gimli::DwarfSections::load(&load_section)?;
// Create `EndianSlice`s for all of the sections.
let dwarf = dwarf_sections.borrow(borrow_section);
// Iterate over the compilation units.
let mut iter = dwarf.units();
while let Some(header) = iter.next()? {
println!(
"Line number info for unit at <.debug_info+0x{:x}>",
header.offset().as_debug_info_offset().unwrap().0
);
let unit = dwarf.unit(header)?;
let unit = unit.unit_ref(&dwarf);
// Get the line program for the compilation unit.
if let Some(program) = unit.line_program.clone() {
let comp_dir = if let Some(ref dir) = unit.comp_dir {
path::PathBuf::from(dir.to_string_lossy().into_owned())
} else {
path::PathBuf::new()
};
// Iterate over the line program rows.
let mut rows = program.rows();
while let Some((header, row)) = rows.next_row()? {
if row.end_sequence() {
// End of sequence indicates a possible gap in addresses.
println!("{:x} end-sequence", row.address());
} else {
// Determine the path. Real applications should cache this for performance.
let mut path = path::PathBuf::new();
if let Some(file) = row.file(header) {
path.clone_from(&comp_dir);
// The directory index 0 is defined to correspond to the compilation unit directory.
if file.directory_index() != 0 {
if let Some(dir) = file.directory(header) {
path.push(unit.attr_string(dir)?.to_string_lossy().as_ref());
}
}
path.push(
unit.attr_string(file.path_name())?
.to_string_lossy()
.as_ref(),
);
}
// Determine line/column. DWARF line/column is never 0, so we use that
// but other applications may want to display this differently.
let line = match row.line() {
Some(line) => line.get(),
None => 0,
};
let column = match row.column() {
gimli::ColumnType::LeftEdge => 0,
gimli::ColumnType::Column(column) => column.get(),
};
println!("{:x} {}:{}:{}", row.address(), path.display(), line, column);
}
}
}
}
Ok(())
}