Skip to content

Commit

Permalink
Merge pull request #339 from philipc/read
Browse files Browse the repository at this point in the history
Create a read module
  • Loading branch information
philipc authored Nov 3, 2018
2 parents 4b765e4 + f498201 commit 2138f48
Show file tree
Hide file tree
Showing 26 changed files with 1,264 additions and 994 deletions.
7 changes: 3 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,11 @@ We use `rustfmt` to automatically format and style all of our code.
To install `rustfmt`:

```
$ cargo install rustfmt
$ rustup component add rustfmt-preview
```

To run `rustfmt` on `gimli`, use the `format` script in the root of the
repository:
To run `rustfmt` on `gimli`:

```
$ ./format
$ cargo fmt
```
52 changes: 34 additions & 18 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
extern crate gimli;
extern crate test;

use gimli::{AttributeValue, DebugAbbrev, DebugAranges, DebugInfo, DebugLine, DebugLineOffset,
DebugLoc, DebugLocLists, DebugPubNames, DebugPubTypes, DebugRanges, DebugRngLists,
EntriesTreeNode, Expression, Format, LittleEndian, LocationLists, Operation,
RangeLists, Reader};
use gimli::{
AttributeValue, DebugAbbrev, DebugAranges, DebugInfo, DebugLine, DebugLineOffset, DebugLoc,
DebugLocLists, DebugPubNames, DebugPubTypes, DebugRanges, DebugRngLists, EntriesTreeNode,
Expression, Format, LittleEndian, LocationLists, Operation, RangeLists, Reader,
};
use std::env;
use std::fs::File;
use std::io::Read;
Expand Down Expand Up @@ -48,10 +49,14 @@ fn bench_parsing_debug_abbrev(b: &mut test::Bencher) {
}

#[inline]
fn impl_bench_parsing_debug_info<R: Reader>(debug_info: DebugInfo<R>, debug_abbrev: DebugAbbrev<R>) {
fn impl_bench_parsing_debug_info<R: Reader>(
debug_info: DebugInfo<R>,
debug_abbrev: DebugAbbrev<R>,
) {
let mut iter = debug_info.units();
while let Some(unit) = iter.next().expect("Should parse compilation unit") {
let abbrevs = unit.abbreviations(&debug_abbrev)
let abbrevs = unit
.abbreviations(&debug_abbrev)
.expect("Should parse abbreviations");

let mut cursor = unit.entries(&abbrevs);
Expand Down Expand Up @@ -102,10 +107,12 @@ fn bench_parsing_debug_info_tree(b: &mut test::Bencher) {

let mut iter = debug_info.units();
while let Some(unit) = iter.next().expect("Should parse compilation unit") {
let abbrevs = unit.abbreviations(&debug_abbrev)
let abbrevs = unit
.abbreviations(&debug_abbrev)
.expect("Should parse abbreviations");

let mut tree = unit.entries_tree(&abbrevs, None)
let mut tree = unit
.entries_tree(&abbrevs, None)
.expect("Should have entries tree");
let root = tree.root().expect("Should parse root entry");
parse_debug_info_tree(root);
Expand Down Expand Up @@ -200,7 +207,8 @@ fn bench_executing_line_number_programs(b: &mut test::Bencher) {
.expect("Should parse line number program header");

let mut rows = program.rows();
while let Some(row) = rows.next_row()
while let Some(row) = rows
.next_row()
.expect("Should parse and execute all rows in the line number program")
{
test::black_box(row);
Expand All @@ -225,7 +233,8 @@ fn bench_parsing_debug_loc(b: &mut test::Bencher) {

let mut iter = debug_info.units();
while let Some(unit) = iter.next().expect("Should parse compilation unit") {
let abbrevs = unit.abbreviations(&debug_abbrev)
let abbrevs = unit
.abbreviations(&debug_abbrev)
.expect("Should parse abbreviations");

let mut cursor = unit.entries(&abbrevs);
Expand Down Expand Up @@ -283,7 +292,8 @@ fn bench_parsing_debug_ranges(b: &mut test::Bencher) {

let mut iter = debug_info.units();
while let Some(unit) = iter.next().expect("Should parse compilation unit") {
let abbrevs = unit.abbreviations(&debug_abbrev)
let abbrevs = unit
.abbreviations(&debug_abbrev)
.expect("Should parse abbreviations");

let mut cursor = unit.entries(&abbrevs);
Expand Down Expand Up @@ -332,7 +342,8 @@ fn debug_info_expressions<R: Reader>(

let mut iter = debug_info.units();
while let Some(unit) = iter.next().expect("Should parse compilation unit") {
let abbrevs = unit.abbreviations(debug_abbrev)
let abbrevs = unit
.abbreviations(debug_abbrev)
.expect("Should parse abbreviations");

let mut cursor = unit.entries(&abbrevs);
Expand Down Expand Up @@ -399,7 +410,8 @@ fn debug_loc_expressions<R: Reader>(

let mut iter = debug_info.units();
while let Some(unit) = iter.next().expect("Should parse compilation unit") {
let abbrevs = unit.abbreviations(debug_abbrev)
let abbrevs = unit
.abbreviations(debug_abbrev)
.expect("Should parse abbreviations");

let mut cursor = unit.entries(&abbrevs);
Expand Down Expand Up @@ -494,11 +506,13 @@ mod cfi {
extern crate gimli;
extern crate test;

use super::*;
use self::fallible_iterator::FallibleIterator;
use super::*;

use gimli::{BaseAddresses, CieOrFde, EhFrame, FrameDescriptionEntry, LittleEndian,
UninitializedUnwindContext, UnwindSection, UnwindTable};
use gimli::{
BaseAddresses, CieOrFde, EhFrame, FrameDescriptionEntry, LittleEndian,
UninitializedUnwindContext, UnwindSection, UnwindTable,
};

#[bench]
fn iterate_entries_and_do_not_parse_any_fde(b: &mut test::Bencher) {
Expand Down Expand Up @@ -594,7 +608,8 @@ mod cfi {
.parse(|offset| eh_frame.cie_from_offset(&bases, offset))
.expect("Should be able to get CIE for FED");

let mut context = ctx.take()
let mut context = ctx
.take()
.unwrap()
.initialize(fde.cie())
.expect("Should be able to initialize ctx");
Expand Down Expand Up @@ -695,7 +710,8 @@ mod cfi {
let mut ctx = Some(UninitializedUnwindContext::new());

b.iter(|| {
let mut context = ctx.take()
let mut context = ctx
.take()
.unwrap()
.initialize(fde.cie())
.expect("Should be able to initialize ctx");
Expand Down
10 changes: 6 additions & 4 deletions examples/dwarf-validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ use object::Object;
use rayon::prelude::*;
use std::borrow::{Borrow, Cow};
use std::env;
use std::io::{self, BufWriter, Write};
use std::error;
use std::fs;
use std::io::{self, BufWriter, Write};
use std::iter::Iterator;
use std::path::{Path, PathBuf};
use std::process;
use std::error;
use std::sync::Mutex;
use typed_arena::Arena;

Expand Down Expand Up @@ -115,9 +115,11 @@ where
S: gimli::Section<gimli::EndianSlice<'a, Endian>>,
Endian: gimli::Endianity + Send + Sync,
'file: 'input,
'a: 'file
'a: 'file,
{
let data = file.section_data_by_name(S::section_name()).unwrap_or(Cow::Borrowed(&[]));
let data = file
.section_data_by_name(S::section_name())
.unwrap_or(Cow::Borrowed(&[]));
let data_ref = (*arena.alloc(data)).borrow();
S::from(gimli::EndianSlice::new(data_ref, endian))
}
Expand Down
76 changes: 51 additions & 25 deletions examples/dwarfdump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ use std::borrow::{Borrow, Cow};
use std::cmp::min;
use std::collections::HashMap;
use std::env;
use std::error;
use std::fmt::{self, Debug};
use std::fs;
use std::io;
use std::io::{BufWriter, Write};
use std::fs;
use std::iter::Iterator;
use std::process;
use std::error;
use std::mem;
use std::process;
use std::result;
use std::fmt::{self, Debug};
use std::sync::{Condvar, Mutex};
use typed_arena::Arena;

Expand Down Expand Up @@ -564,11 +564,9 @@ where
object::Machine::X86_64 => gimli::X86_64::register_name,
_ => register_name_none,
};
let register_name = |register| {
match arch_register_name(register) {
Some(name) => Cow::Borrowed(name),
None => Cow::Owned(format!("{}", register.0)),
}
let register_name = |register| match arch_register_name(register) {
Some(name) => Cow::Borrowed(name),
None => Cow::Owned(format!("{}", register.0)),
};

let mut eh_frame: gimli::EhFrame<_> = load_section(&arena, file, endian);
Expand Down Expand Up @@ -725,7 +723,8 @@ fn dump_cfi_instructions<R: Reader, W: Write>(
writeln!(
w,
" DW_CFA_def_cfa ({}, {})",
register_name(register), offset
register_name(register),
offset
)?;
}
DefCfaSf {
Expand All @@ -735,11 +734,16 @@ fn dump_cfi_instructions<R: Reader, W: Write>(
writeln!(
w,
" DW_CFA_def_cfa_sf ({}, {})",
register_name(register), factored_offset
register_name(register),
factored_offset
)?;
}
DefCfaRegister { register } => {
writeln!(w, " DW_CFA_def_cfa_register ({})", register_name(register))?;
writeln!(
w,
" DW_CFA_def_cfa_register ({})",
register_name(register)
)?;
}
DefCfaOffset { offset } => {
writeln!(w, " DW_CFA_def_cfa_offset ({})", offset)?;
Expand All @@ -755,10 +759,18 @@ fn dump_cfi_instructions<R: Reader, W: Write>(
writeln!(w, " DW_CFA_def_cfa_expression (...)")?;
}
Undefined { register } => {
writeln!(w, " DW_CFA_undefined ({})", register_name(register))?;
writeln!(
w,
" DW_CFA_undefined ({})",
register_name(register)
)?;
}
SameValue { register } => {
writeln!(w, " DW_CFA_same_value ({})", register_name(register))?;
writeln!(
w,
" DW_CFA_same_value ({})",
register_name(register)
)?;
}
Offset {
register,
Expand All @@ -767,7 +779,8 @@ fn dump_cfi_instructions<R: Reader, W: Write>(
writeln!(
w,
" DW_CFA_offset ({}, {})",
register_name(register), factored_offset
register_name(register),
factored_offset
)?;
}
OffsetExtendedSf {
Expand All @@ -777,7 +790,8 @@ fn dump_cfi_instructions<R: Reader, W: Write>(
writeln!(
w,
" DW_CFA_offset_extended_sf ({}, {})",
register_name(register), factored_offset
register_name(register),
factored_offset
)?;
}
ValOffset {
Expand All @@ -787,7 +801,8 @@ fn dump_cfi_instructions<R: Reader, W: Write>(
writeln!(
w,
" DW_CFA_val_offset ({}, {})",
register_name(register), factored_offset
register_name(register),
factored_offset
)?;
}
ValOffsetSf {
Expand All @@ -797,7 +812,8 @@ fn dump_cfi_instructions<R: Reader, W: Write>(
writeln!(
w,
" DW_CFA_val_offset_sf ({}, {})",
register_name(register), factored_offset
register_name(register),
factored_offset
)?;
}
Register {
Expand All @@ -807,14 +823,19 @@ fn dump_cfi_instructions<R: Reader, W: Write>(
writeln!(
w,
" DW_CFA_register ({}, {})",
register_name(dest_register), register_name(src_register)
register_name(dest_register),
register_name(src_register)
)?;
}
Expression {
register,
expression: _,
} => {
writeln!(w, " DW_CFA_expression ({}, ...)", register_name(register))?;
writeln!(
w,
" DW_CFA_expression ({}, ...)",
register_name(register)
)?;
}
ValExpression {
register,
Expand All @@ -827,7 +848,11 @@ fn dump_cfi_instructions<R: Reader, W: Write>(
)?;
}
Restore { register } => {
writeln!(w, " DW_CFA_restore ({})", register_name(register))?;
writeln!(
w,
" DW_CFA_restore ({})",
register_name(register)
)?;
}
RememberState => {
writeln!(w, " DW_CFA_remember_state")?;
Expand Down Expand Up @@ -1067,8 +1092,7 @@ fn dump_entries<R: Reader, W: Write>(
unit.address_size,
unit.comp_dir.clone(),
unit.comp_name.clone(),
)
.ok(),
).ok(),
_ => None,
}
}
Expand Down Expand Up @@ -1713,9 +1737,11 @@ fn dump_line_program<R: Reader, W: Write>(
Some(gimli::AttributeValue::DebugLineRef(offset)) => offset,
_ => return Ok(()),
};
let comp_dir = root.attr(gimli::DW_AT_comp_dir)?
let comp_dir = root
.attr(gimli::DW_AT_comp_dir)?
.and_then(|attr| attr.string_value(debug_str));
let comp_name = root.attr(gimli::DW_AT_name)?
let comp_name = root
.attr(gimli::DW_AT_name)?
.and_then(|attr| attr.string_value(debug_str));

let program = debug_line.program(offset, unit.address_size(), comp_dir, comp_name);
Expand Down
3 changes: 2 additions & 1 deletion src/arch.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use parser::Register;
// TODO: don't depend on read
use read::Register;

macro_rules! registers {
($struct_name:ident, { $($name:ident = ($val:expr, $disp:expr)),+ }) => {
Expand Down
8 changes: 3 additions & 5 deletions src/leb128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ fn low_bits_of_u64(val: u64) -> u8 {
/// encoded.
pub mod read {
use super::{low_bits_of_byte, CONTINUATION_BIT, SIGN_BIT};
use parser::{Error, Result};
use reader::Reader;
use read::{Error, Reader, Result};

/// Read an unsigned LEB128 number from the given `Reader` and
/// return it or an error if reading failed.
Expand Down Expand Up @@ -186,10 +185,9 @@ pub mod write {

#[cfg(test)]
mod tests {
use super::{low_bits_of_byte, read, write, low_bits_of_u64, CONTINUATION_BIT};
use super::{low_bits_of_byte, low_bits_of_u64, read, write, CONTINUATION_BIT};
use endianity::NativeEndian;
use endian_slice::EndianSlice;
use parser::Error;
use read::{EndianSlice, Error};
use std;
use std::io;

Expand Down
Loading

0 comments on commit 2138f48

Please sign in to comment.