diff --git a/benches/bench.rs b/benches/bench.rs index 7ab8337b2..de3fd47e9 100755 --- a/benches/bench.rs +++ b/benches/bench.rs @@ -27,7 +27,8 @@ pub fn read_section(section: &str) -> Vec { fn bench_parsing_debug_abbrev(b: &mut test::Bencher) { let debug_info = read_section("debug_info"); let debug_info = DebugInfo::::new(&debug_info); - let unit = debug_info.units() + let unit = debug_info + .units() .next() .expect("Should have at least one compilation unit") .expect("And it should parse OK"); @@ -35,10 +36,10 @@ fn bench_parsing_debug_abbrev(b: &mut test::Bencher) { let debug_abbrev = read_section("debug_abbrev"); b.iter(|| { - let debug_abbrev = DebugAbbrev::::new(&debug_abbrev); - test::black_box(unit.abbreviations(debug_abbrev) - .expect("Should parse abbreviations")); - }); + let debug_abbrev = DebugAbbrev::::new(&debug_abbrev); + test::black_box(unit.abbreviations(debug_abbrev) + .expect("Should parse abbreviations")); + }); } #[bench] @@ -82,7 +83,8 @@ fn bench_parsing_debug_info_tree(b: &mut test::Bencher) { let abbrevs = unit.abbreviations(debug_abbrev) .expect("Should parse abbreviations"); - let mut tree = unit.entries_tree(&abbrevs, None).expect("Should have entries tree"); + let mut tree = unit.entries_tree(&abbrevs, None) + .expect("Should have entries tree"); parse_debug_info_tree(tree.iter()); } }); @@ -107,11 +109,11 @@ fn bench_parsing_debug_aranges(b: &mut test::Bencher) { let debug_aranges = DebugAranges::::new(&debug_aranges); b.iter(|| { - let mut aranges = debug_aranges.items(); - while let Some(arange) = aranges.next().expect("Should parse arange OK") { - test::black_box(arange); - } - }); + let mut aranges = debug_aranges.items(); + while let Some(arange) = aranges.next().expect("Should parse arange OK") { + test::black_box(arange); + } + }); } #[bench] @@ -120,11 +122,11 @@ fn bench_parsing_debug_pubnames(b: &mut test::Bencher) { let debug_pubnames = DebugPubNames::::new(&debug_pubnames); b.iter(|| { - let mut pubnames = debug_pubnames.items(); - while let Some(pubname) = pubnames.next().expect("Should parse pubname OK") { - test::black_box(pubname); - } - }); + let mut pubnames = debug_pubnames.items(); + while let Some(pubname) = pubnames.next().expect("Should parse pubname OK") { + test::black_box(pubname); + } + }); } #[bench] @@ -133,11 +135,11 @@ fn bench_parsing_debug_types(b: &mut test::Bencher) { let debug_pubtypes = DebugPubTypes::::new(&debug_pubtypes); b.iter(|| { - let mut pubtypes = debug_pubtypes.items(); - while let Some(pubtype) = pubtypes.next().expect("Should parse pubtype OK") { - test::black_box(pubtype); - } - }); + let mut pubtypes = debug_pubtypes.items(); + while let Some(pubtype) = pubtypes.next().expect("Should parse pubtype OK") { + test::black_box(pubtype); + } + }); } // We happen to know that there is a line number program and header at @@ -152,7 +154,8 @@ fn bench_parsing_line_number_program_opcodes(b: &mut test::Bencher) { let debug_line = DebugLine::::new(&debug_line); b.iter(|| { - let program = debug_line.program(OFFSET, ADDRESS_SIZE, None, None) + let program = debug_line + .program(OFFSET, ADDRESS_SIZE, None, None) .expect("Should parse line number program header"); let header = program.header(); @@ -169,19 +172,21 @@ fn bench_executing_line_number_programs(b: &mut test::Bencher) { let debug_line = DebugLine::::new(&debug_line); b.iter(|| { - let program = debug_line.program(OFFSET, ADDRESS_SIZE, None, None) + let program = debug_line + .program(OFFSET, ADDRESS_SIZE, None, None) .expect("Should parse line number program header"); let mut rows = program.rows(); - while let Some(row) = rows.next_row() - .expect("Should parse and execute all rows in the line number program") { + while let Some(row) = + rows.next_row() + .expect("Should parse and execute all rows in the line number program") { test::black_box(row); } }); } // See comment above `test_parse_self_eh_frame`. -#[cfg(target_pointer_width="64")] +#[cfg(target_pointer_width = "64")] mod cfi { extern crate fallible_iterator; extern crate gimli; @@ -198,17 +203,14 @@ mod cfi { let eh_frame = read_section("eh_frame"); let eh_frame = EhFrame::::new(&eh_frame); - let bases = BaseAddresses::default() - .set_cfi(0) - .set_data(0) - .set_text(0); + let bases = BaseAddresses::default().set_cfi(0).set_data(0).set_text(0); b.iter(|| { - let mut entries = eh_frame.entries(&bases); - while let Some(entry) = entries.next().expect("Should parse CFI entry OK") { - test::black_box(entry); - } - }); + let mut entries = eh_frame.entries(&bases); + while let Some(entry) = entries.next().expect("Should parse CFI entry OK") { + test::black_box(entry); + } + }); } #[bench] @@ -216,10 +218,7 @@ mod cfi { let eh_frame = read_section("eh_frame"); let eh_frame = EhFrame::::new(&eh_frame); - let bases = BaseAddresses::default() - .set_cfi(0) - .set_data(0) - .set_text(0); + let bases = BaseAddresses::default().set_cfi(0).set_data(0).set_text(0); b.iter(|| { let mut entries = eh_frame.entries(&bases); @@ -229,7 +228,8 @@ mod cfi { test::black_box(cie); } CieOrFde::Fde(partial) => { - let fde = partial.parse(|offset| eh_frame.cie_from_offset(&bases, offset)) + let fde = partial + .parse(|offset| eh_frame.cie_from_offset(&bases, offset)) .expect("Should be able to get CIE for FED"); test::black_box(fde); } @@ -243,10 +243,7 @@ mod cfi { let eh_frame = read_section("eh_frame"); let eh_frame = EhFrame::::new(&eh_frame); - let bases = BaseAddresses::default() - .set_cfi(0) - .set_data(0) - .set_text(0); + let bases = BaseAddresses::default().set_cfi(0).set_data(0).set_text(0); b.iter(|| { let mut entries = eh_frame.entries(&bases); @@ -254,17 +251,20 @@ mod cfi { match entry { CieOrFde::Cie(cie) => { let mut instrs = cie.instructions(); - while let Some(i) = instrs.next() - .expect("Can parse next CFI instruction OK") { + while let Some(i) = instrs + .next() + .expect("Can parse next CFI instruction OK") { test::black_box(i); } } CieOrFde::Fde(partial) => { - let fde = partial.parse(|offset| eh_frame.cie_from_offset(&bases, offset)) + let fde = partial + .parse(|offset| eh_frame.cie_from_offset(&bases, offset)) .expect("Should be able to get CIE for FED"); let mut instrs = fde.instructions(); - while let Some(i) = instrs.next() - .expect("Can parse next CFI instruction OK") { + while let Some(i) = instrs + .next() + .expect("Can parse next CFI instruction OK") { test::black_box(i); } } @@ -278,10 +278,7 @@ mod cfi { let eh_frame = read_section("eh_frame"); let eh_frame = EhFrame::::new(&eh_frame); - let bases = BaseAddresses::default() - .set_cfi(0) - .set_data(0) - .set_text(0); + let bases = BaseAddresses::default().set_cfi(0).set_data(0).set_text(0); let mut ctx = Some(UninitializedUnwindContext::new()); @@ -291,7 +288,8 @@ mod cfi { match entry { CieOrFde::Cie(_) => {} CieOrFde::Fde(partial) => { - let fde = partial.parse(|offset| eh_frame.cie_from_offset(&bases, offset)) + let fde = partial + .parse(|offset| eh_frame.cie_from_offset(&bases, offset)) .expect("Should be able to get CIE for FED"); let mut context = ctx.take() @@ -301,8 +299,9 @@ mod cfi { { let mut table = UnwindTable::new(&mut context, &fde); - while let Some(row) = table.next_row() - .expect("Should get next unwind table row") { + while let Some(row) = table + .next_row() + .expect("Should get next unwind table row") { test::black_box(row); } } @@ -318,15 +317,15 @@ mod cfi { LittleEndian, EhFrame<'input, LittleEndian>>) -> usize { - fde.instructions().fold(0, |count, _| count + 1).expect("fold over instructions OK") + fde.instructions() + .fold(0, |count, _| count + 1) + .expect("fold over instructions OK") } - fn get_fde_with_longest_cfi_instructions(eh_frame: EhFrame) + fn get_fde_with_longest_cfi_instructions + (eh_frame: EhFrame) -> FrameDescriptionEntry> { - let bases = BaseAddresses::default() - .set_cfi(0) - .set_data(0) - .set_text(0); + let bases = BaseAddresses::default().set_cfi(0).set_data(0).set_text(0); let mut longest: Option<(usize, FrameDescriptionEntry<_, _>)> = None; @@ -335,7 +334,8 @@ mod cfi { match entry { CieOrFde::Cie(_) => {} CieOrFde::Fde(partial) => { - let fde = partial.parse(|offset| eh_frame.cie_from_offset(&bases, offset)) + let fde = partial + .parse(|offset| eh_frame.cie_from_offset(&bases, offset)) .expect("Should be able to get CIE for FED"); let this_len = instrs_len(&fde); @@ -362,11 +362,11 @@ mod cfi { let fde = get_fde_with_longest_cfi_instructions(eh_frame); b.iter(|| { - let mut instrs = fde.instructions(); - while let Some(i) = instrs.next().expect("Should parse instruction OK") { - test::black_box(i); - } - }); + let mut instrs = fde.instructions(); + while let Some(i) = instrs.next().expect("Should parse instruction OK") { + test::black_box(i); + } + }); } #[bench] diff --git a/examples/dwarfdump.rs b/examples/dwarfdump.rs index b335503e0..1dea9c040 100755 --- a/examples/dwarfdump.rs +++ b/examples/dwarfdump.rs @@ -255,21 +255,26 @@ fn dump_entries(offset: usize, indent = indent); if entry.tag() == gimli::DW_TAG_compile_unit || entry.tag() == gimli::DW_TAG_type_unit { - unit.base_address = match entry.attr_value(gimli::DW_AT_low_pc) - .expect("Should parse low_pc") { + unit.base_address = match entry + .attr_value(gimli::DW_AT_low_pc) + .expect("Should parse low_pc") { Some(gimli::AttributeValue::Addr(address)) => address, _ => 0, }; - unit.comp_dir = entry.attr(gimli::DW_AT_comp_dir) + unit.comp_dir = entry + .attr(gimli::DW_AT_comp_dir) .expect("Should parse comp_dir") .and_then(|attr| attr.string_value(&debug_str)); - unit.comp_name = entry.attr(gimli::DW_AT_name) + unit.comp_name = entry + .attr(gimli::DW_AT_name) .expect("Should parse name") .and_then(|attr| attr.string_value(&debug_str)); - unit.line_program = match entry.attr_value(gimli::DW_AT_stmt_list) - .expect("Should parse stmt_list") { + unit.line_program = match entry + .attr_value(gimli::DW_AT_stmt_list) + .expect("Should parse stmt_list") { Some(gimli::AttributeValue::DebugLineRef(offset)) => { - debug_line.program(offset, unit.address_size, unit.comp_dir, unit.comp_name) + debug_line + .program(offset, unit.address_size, unit.comp_dir, unit.comp_name) .ok() } _ => None, @@ -593,10 +598,16 @@ fn dump_op(dwop: gimli::DwOp, op: gimli::Operation, newpc: &[u8] } } } - gimli::Operation::Piece { size_in_bits, bit_offset: None } => { + gimli::Operation::Piece { + size_in_bits, + bit_offset: None, + } => { print!(" {}", size_in_bits / 8); } - gimli::Operation::Piece { size_in_bits, bit_offset: Some(bit_offset) } => { + gimli::Operation::Piece { + size_in_bits, + bit_offset: Some(bit_offset), + } => { print!(" 0x{:08x} offset 0x{:08x}", size_in_bits, bit_offset); } gimli::Operation::ImplicitValue { data } => { @@ -623,7 +634,8 @@ fn dump_loc_list(debug_loc: gimli::DebugLoc, unit: &Unit) where Endian: gimli::Endianity { - let locations = debug_loc.raw_locations(offset, unit.address_size) + let locations = debug_loc + .raw_locations(offset, unit.address_size) .expect("Should have valid loc offset"); let mut locations: Vec<_> = locations.collect().expect("Should parse locations"); @@ -674,7 +686,8 @@ fn dump_range_list(debug_ranges: gimli::DebugRanges, unit: &Unit) where Endian: gimli::Endianity { - let ranges = debug_ranges.raw_ranges(offset, unit.address_size) + let ranges = debug_ranges + .raw_ranges(offset, unit.address_size) .expect("Should have valid range offset"); let ranges: Vec<_> = ranges.collect().expect("Should parse ranges"); println!("\t\tranges: {} at .debug_ranges offset {} (0x{:08x}) ({} bytes)", @@ -720,7 +733,7 @@ fn dump_line(file: &object::File, debug_abbrev: gimli::DebugAbbrev offset, _ => continue, }; @@ -781,8 +794,9 @@ fn dump_line(file: &object::File, debug_abbrev: gimli::DebugAbbrev(file: &object::File, debug_abbrev: gimli::DebugAbbrev column, @@ -860,7 +872,8 @@ fn dump_aranges(file: &object::File) while let Some(arange) = aranges.next().expect("Should parse arange OK") { let cu_offset = arange.debug_info_offset(); if Some(cu_offset) != prev_cu_offset { - let cu = debug_info.header_from_offset(cu_offset) + let cu = debug_info + .header_from_offset(cu_offset) .expect("Should parse unit header OK"); cu_die_offset = gimli::DebugInfoOffset(cu_offset.0 + cu.header_size()); prev_cu_offset = Some(cu_offset); diff --git a/rustfmt.toml b/rustfmt.toml index 675e71fb4..f10751606 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -1,2 +1,2 @@ write_mode = "Overwrite" - +use_try_shorthand = true diff --git a/src/abbrev.rs b/src/abbrev.rs index 8e713ad3a..c5b48b837 100644 --- a/src/abbrev.rs +++ b/src/abbrev.rs @@ -137,7 +137,7 @@ impl Abbreviations { let mut abbrevs = Abbreviations::empty(); loop { - let (rest, abbrev) = try!(Abbreviation::parse(input)); + let (rest, abbrev) = Abbreviation::parse(input)?; input = rest; match abbrev { @@ -210,7 +210,7 @@ impl Abbreviation { /// Parse an abbreviation's tag. fn parse_tag(input: &[u8]) -> Result<(&[u8], constants::DwTag)> { - let (rest, val) = try!(parse_unsigned_leb(input)); + let (rest, val) = parse_unsigned_leb(input)?; if val == 0 { Err(Error::AbbreviationTagZero) } else { @@ -220,7 +220,7 @@ impl Abbreviation { /// Parse an abbreviation's "does the type have children?" byte. fn parse_has_children(input: &[u8]) -> Result<(&[u8], constants::DwChildren)> { - let (rest, val) = try!(parse_u8(input)); + let (rest, val) = parse_u8(input)?; let val = constants::DwChildren(val); if val == constants::DW_CHILDREN_no || val == constants::DW_CHILDREN_yes { Ok((rest, val)) @@ -235,7 +235,7 @@ impl Abbreviation { let mut attrs = Vec::new(); loop { - let (rest, attr) = try!(AttributeSpecification::parse(input)); + let (rest, attr) = AttributeSpecification::parse(input)?; input = rest; match attr { @@ -250,14 +250,14 @@ impl Abbreviation { /// Parse an abbreviation. Return `None` for the null abbreviation, `Some` /// for an actual abbreviation. fn parse(input: &[u8]) -> Result<(&[u8], Option)> { - let (rest, code) = try!(parse_unsigned_leb(input)); + let (rest, code) = parse_unsigned_leb(input)?; if code == 0 { return Ok((rest, None)); } - let (rest, tag) = try!(Self::parse_tag(rest)); - let (rest, has_children) = try!(Self::parse_has_children(rest)); - let (rest, attributes) = try!(Self::parse_attributes(rest)); + let (rest, tag) = Self::parse_tag(rest)?; + let (rest, has_children) = Self::parse_has_children(rest)?; + let (rest, attributes) = Self::parse_attributes(rest)?; let abbrev = Abbreviation::new(code, tag, has_children, attributes); Ok((rest, Some(abbrev))) } @@ -345,7 +345,7 @@ impl AttributeSpecification { /// Parse an attribute's form. fn parse_form(input: &[u8]) -> Result<(&[u8], constants::DwForm)> { - let (rest, val) = try!(parse_unsigned_leb(input)); + let (rest, val) = parse_unsigned_leb(input)?; if val == 0 { Err(Error::AttributeFormZero) } else { @@ -356,10 +356,10 @@ impl AttributeSpecification { /// Parse an attribute specification. Returns `None` for the null attribute /// specification, `Some` for an actual attribute specification. fn parse(input: &[u8]) -> Result<(&[u8], Option)> { - let (rest, name) = try!(parse_unsigned_leb(input)); + let (rest, name) = parse_unsigned_leb(input)?; if name == 0 { // Parse the null attribute specification. - let (rest, form) = try!(parse_unsigned_leb(rest)); + let (rest, form) = parse_unsigned_leb(rest)?; return if form == 0 { Ok((rest, None)) } else { @@ -368,7 +368,7 @@ impl AttributeSpecification { } let name = constants::DwAt(name); - let (rest, form) = try!(Self::parse_form(rest)); + let (rest, form) = Self::parse_form(rest)?; let spec = AttributeSpecification::new(name, form); Ok((rest, Some(spec))) } @@ -431,24 +431,25 @@ pub mod tests { .get_contents() .unwrap(); - let abbrev1 = Abbreviation::new(1, - constants::DW_TAG_compile_unit, - constants::DW_CHILDREN_yes, - vec![ - AttributeSpecification::new(constants::DW_AT_producer, constants::DW_FORM_strp), - AttributeSpecification::new(constants::DW_AT_language, constants::DW_FORM_data2), - ]); + let abbrev1 = + Abbreviation::new(1, + constants::DW_TAG_compile_unit, + constants::DW_CHILDREN_yes, + vec![AttributeSpecification::new(constants::DW_AT_producer, + constants::DW_FORM_strp), + AttributeSpecification::new(constants::DW_AT_language, + constants::DW_FORM_data2)]); let abbrev2 = Abbreviation::new(2, constants::DW_TAG_subprogram, constants::DW_CHILDREN_no, - vec![ - AttributeSpecification::new(constants::DW_AT_name, constants::DW_FORM_string), - ]); + vec![AttributeSpecification::new(constants::DW_AT_name, + constants::DW_FORM_string)]); let debug_abbrev = DebugAbbrev::::new(&buf); let debug_abbrev_offset = DebugAbbrevOffset(extra_start.len()); - let abbrevs = debug_abbrev.abbreviations(debug_abbrev_offset) + let abbrevs = debug_abbrev + .abbreviations(debug_abbrev_offset) .expect("Should parse abbreviations"); assert_eq!(abbrevs.get(1), Some(&abbrev1)); assert_eq!(abbrevs.get(2), Some(&abbrev2)); @@ -563,20 +564,20 @@ pub mod tests { .get_contents() .unwrap(); - let abbrev1 = Abbreviation::new(1, - constants::DW_TAG_compile_unit, - constants::DW_CHILDREN_yes, - vec![ - AttributeSpecification::new(constants::DW_AT_producer, constants::DW_FORM_strp), - AttributeSpecification::new(constants::DW_AT_language, constants::DW_FORM_data2), - ]); + let abbrev1 = + Abbreviation::new(1, + constants::DW_TAG_compile_unit, + constants::DW_CHILDREN_yes, + vec![AttributeSpecification::new(constants::DW_AT_producer, + constants::DW_FORM_strp), + AttributeSpecification::new(constants::DW_AT_language, + constants::DW_FORM_data2)]); let abbrev2 = Abbreviation::new(2, constants::DW_TAG_subprogram, constants::DW_CHILDREN_no, - vec![ - AttributeSpecification::new(constants::DW_AT_name, constants::DW_FORM_string), - ]); + vec![AttributeSpecification::new(constants::DW_AT_name, + constants::DW_FORM_string)]); let (rest, abbrevs) = Abbreviations::parse(&buf).expect("Should parse abbreviations"); assert_eq!(abbrevs.get(1), Some(&abbrev1)); @@ -651,10 +652,8 @@ pub mod tests { let expect = Some(Abbreviation::new(1, constants::DW_TAG_subprogram, constants::DW_CHILDREN_no, - vec![ - AttributeSpecification::new(constants::DW_AT_name, - constants::DW_FORM_string), - ])); + vec![AttributeSpecification::new(constants::DW_AT_name, + constants::DW_FORM_string)])); let (rest, abbrev) = Abbreviation::parse(&buf).expect("Should parse abbreviation"); assert_eq!(abbrev, expect); diff --git a/src/aranges.rs b/src/aranges.rs index da2988334..e3ebf9241 100644 --- a/src/aranges.rs +++ b/src/aranges.rs @@ -110,21 +110,21 @@ impl<'input, Endian> LookupParser<'input, Endian> for ArangeParser<'input, Endia #[allow(type_complexity)] fn parse_header(input: EndianBuf) -> Result<(EndianBuf, EndianBuf, Rc)> { - let (rest, (length, format)) = try!(parse_initial_length(input)); + let (rest, (length, format)) = parse_initial_length(input)?; if length as usize > rest.len() { return Err(Error::UnexpectedEof); } let after_set = rest.range_from(length as usize..); let rest = rest.range_to(..length as usize); - let (rest, version) = try!(parse_u16(rest)); + let (rest, version) = parse_u16(rest)?; if version != 2 { return Err(Error::UnknownVersion); } - let (rest, offset) = try!(parse_debug_info_offset(rest, format)); - let (rest, address_size) = try!(parse_address_size(rest)); - let (rest, segment_size) = try!(parse_address_size(rest)); + let (rest, offset) = parse_debug_info_offset(rest, format)?; + let (rest, address_size) = parse_address_size(rest)?; + let (rest, segment_size) = parse_address_size(rest)?; // unit_length + version + offset + address_size + segment_size let header_length = match format { @@ -149,13 +149,13 @@ impl<'input, Endian> LookupParser<'input, Endian> for ArangeParser<'input, Endia Ok((after_set, rest, Rc::new(ArangeHeader { - format: format, - length: length, - version: version, - offset: offset, - address_size: address_size, - segment_size: segment_size, - }))) + format: format, + length: length, + version: version, + offset: offset, + address_size: address_size, + segment_size: segment_size, + }))) } /// Parse a single arange. Return `None` for the null arange, `Some` for an actual arange. @@ -171,12 +171,12 @@ impl<'input, Endian> LookupParser<'input, Endian> for ArangeParser<'input, Endia } let (rest, segment) = if segment_size != 0 { - try!(parse_address(input, segment_size)) + parse_address(input, segment_size)? } else { (input, 0) }; - let (rest, address) = try!(parse_address(rest, address_size)); - let (rest, length) = try!(parse_address(rest, address_size)); + let (rest, address) = parse_address(rest, address_size)?; + let (rest, length) = parse_address(rest, address_size)?; match (segment, address, length) { // There may be multiple sets of tuples, each terminated by a zero tuple. @@ -186,11 +186,11 @@ impl<'input, Endian> LookupParser<'input, Endian> for ArangeParser<'input, Endia _ => { Ok((rest, Some(ArangeEntry { - segment: segment, - address: address, - length: length, - header: header.clone(), - }))) + segment: segment, + address: address, + length: length, + header: header.clone(), + }))) } } } @@ -333,13 +333,13 @@ mod tests { #[test] fn test_parse_entry_ok() { let header = Rc::new(ArangeHeader { - format: Format::Dwarf32, - length: 0, - version: 2, - offset: DebugInfoOffset(0), - address_size: 4, - segment_size: 0, - }); + format: Format::Dwarf32, + length: 0, + version: 2, + offset: DebugInfoOffset(0), + address_size: 4, + segment_size: 0, + }); let buf = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09]; let input = EndianBuf::::new(&buf); let (rest, entry) = ArangeParser::parse_entry(input, &header) @@ -347,11 +347,11 @@ mod tests { assert_eq!(rest, EndianBuf::new(&buf[buf.len() - 1..])); assert_eq!(entry, Some(ArangeEntry { - segment: 0, - address: 0x04030201, - length: 0x08070605, - header: header.clone(), - })); + segment: 0, + address: 0x04030201, + length: 0x08070605, + header: header.clone(), + })); } #[test] diff --git a/src/cfi.rs b/src/cfi.rs index 560ff7561..3c73ef3fa 100644 --- a/src/cfi.rs +++ b/src/cfi.rs @@ -260,7 +260,7 @@ pub trait UnwindSection<'input, Endian> } let input = self.section().range_from(offset..); - if let Some((_, entry)) = try!(CommonInformationEntry::parse(bases, *self, input)) { + if let Some((_, entry)) = CommonInformationEntry::parse(bases, *self, input)? { Ok(entry) } else { Err(Error::NoEntryAtGivenOffset) @@ -312,15 +312,16 @@ pub trait UnwindSection<'input, Endian> bases: &'bases BaseAddresses, ctx: UninitializedUnwindContext<'input, Endian, Self>, address: u64) - -> Result<(UnwindTableRow<'input, Endian>, UninitializedUnwindContext<'input, Endian, Self>)> { + -> Result<(UnwindTableRow<'input, Endian>, + UninitializedUnwindContext<'input, Endian, Self>)> { let mut target_fde = None; let mut entries = self.entries(bases); - while let Some(entry) = try!(entries.next()) { + while let Some(entry) = entries.next()? { match entry { CieOrFde::Cie(_) => continue, CieOrFde::Fde(partial) => { - let fde = try!(partial.parse(|offset| self.cie_from_offset(bases, offset))); + let fde = partial.parse(|offset| self.cie_from_offset(bases, offset))?; if fde.contains(address) { target_fde = Some(fde); break; @@ -331,11 +332,11 @@ pub trait UnwindSection<'input, Endian> if let Some(fde) = target_fde { let mut result_row = None; - let mut ctx = try!(ctx.initialize(fde.cie())); + let mut ctx = ctx.initialize(fde.cie())?; { let mut table = UnwindTable::new(&mut ctx, &fde); - while let Some(row) = try!(table.next_row()) { + while let Some(row) = table.next_row()? { if row.contains(address) { result_row = Some(row.clone()); break; @@ -643,13 +644,13 @@ struct CfiEntryCommon<'input, Endian> /// end-of-entries sentinel, return `Ok(None)`. Otherwise, return /// `Ok(Some(tuple))`, where `tuple.0` is the start of the next entry and /// `tuple.1` is the parsed CFI entry data. -fn parse_cfi_entry_common<'input, Endian, Section>(input: EndianBuf<'input, Endian>) - -> Result, - CfiEntryCommon<'input, Endian>)>> +fn parse_cfi_entry_common<'input, Endian, Section> + (input: EndianBuf<'input, Endian>) + -> Result, CfiEntryCommon<'input, Endian>)>> where Endian: Endianity, Section: UnwindSection<'input, Endian> { - let (rest, (length, format)) = try!(parse_initial_length(input)); + let (rest, (length, format)) = parse_initial_length(input)?; if Section::length_value_is_end_of_entries(length) { return Ok(None); @@ -663,17 +664,18 @@ fn parse_cfi_entry_common<'input, Endian, Section>(input: EndianBuf<'input, Endi let cie_offset_input = rest.range_to(..length as usize); let (rest, cie_id_or_offset) = match Section::cie_offset_encoding(format) { - CieOffsetEncoding::U32 => try!(parse_u32_as_u64(cie_offset_input)), - CieOffsetEncoding::U64 => try!(parse_u64(cie_offset_input)), + CieOffsetEncoding::U32 => parse_u32_as_u64(cie_offset_input)?, + CieOffsetEncoding::U64 => parse_u64(cie_offset_input)?, }; - Ok(Some((rest_rest, CfiEntryCommon { - length: length, - format: format, - cie_offset_input: cie_offset_input, - cie_id_or_offset: cie_id_or_offset, - rest: rest - }))) + Ok(Some((rest_rest, + CfiEntryCommon { + length: length, + format: format, + cie_offset_input: cie_offset_input, + cie_id_or_offset: cie_id_or_offset, + rest: rest, + }))) } /// Either a `CommonInformationEntry` (CIE) or a `FrameDescriptionEntry` (FDE). @@ -699,17 +701,23 @@ fn parse_cfi_entry<'bases, 'input, Endian, Section> where Endian: Endianity, Section: UnwindSection<'input, Endian> { - let (rest_rest, CfiEntryCommon { length, format, cie_offset_input, cie_id_or_offset, rest }) = - match try!(parse_cfi_entry_common::(input)) { - None => return Ok(None), - Some(common) => common, - }; + let (rest_rest, + CfiEntryCommon { + length, + format, + cie_offset_input, + cie_id_or_offset, + rest, + }) = match parse_cfi_entry_common::(input)? { + None => return Ok(None), + Some(common) => common, + }; if Section::is_cie(format, cie_id_or_offset) { - let cie = try!(CommonInformationEntry::parse_rest(length, format, bases, section, rest)); + let cie = CommonInformationEntry::parse_rest(length, format, bases, section, rest)?; Ok(Some((rest_rest, CieOrFde::Cie(cie)))) } else { - let cie_offset = try!(u64_to_offset(cie_id_or_offset)); + let cie_offset = u64_to_offset(cie_id_or_offset)?; let cie_offset = match section.resolve_cie_offset(cie_offset_input, cie_offset) { None => return Err(Error::OffsetOutOfBounds), Some(offset) => offset, @@ -791,7 +799,8 @@ impl Augmentation { let mut chars = augmentation_str.chars(); - let first = chars.next() + let first = chars + .next() .expect("Is valid UTF-8 and length > 0, so must have at least one char."); if first != 'z' { return Err(Error::UnknownAugmentation); @@ -799,28 +808,28 @@ impl Augmentation { let mut augmentation = Augmentation::default(); - let (rest, augmentation_length) = try!(parse_unsigned_lebe(input)); - let (mut rest, rest_rest) = try!(rest.try_split_at(augmentation_length as usize)); + let (rest, augmentation_length) = parse_unsigned_lebe(input)?; + let (mut rest, rest_rest) = rest.try_split_at(augmentation_length as usize)?; for ch in chars { match ch { 'L' => { - let (rest_, encoding) = try!(parse_pointer_encoding(rest)); + let (rest_, encoding) = parse_pointer_encoding(rest)?; augmentation.lsda = Some(encoding); rest = rest_; } 'P' => { - let (rest_, encoding) = try!(parse_pointer_encoding(rest)); - let (rest_, personality) = try!(parse_encoded_pointer(encoding, - bases, - address_size, - section.section(), - rest_)); + let (rest_, encoding) = parse_pointer_encoding(rest)?; + let (rest_, personality) = parse_encoded_pointer(encoding, + bases, + address_size, + section.section(), + rest_)?; augmentation.personality = Some(personality); rest = rest_; } 'R' => { - let (rest_, encoding) = try!(parse_pointer_encoding(rest)); + let (rest_, encoding) = parse_pointer_encoding(rest)?; augmentation.fde_address_encoding = Some(encoding); rest = rest_; } @@ -855,12 +864,12 @@ impl AugmentationData { // that defines augmentation data in the FDE is the 'L' character, so we // can just check for its presence directly. - let (rest, aug_data_len) = try!(parse_unsigned_lebe(input)); - let (rest, rest_rest) = try!(rest.try_split_at(aug_data_len as usize)); + let (rest, aug_data_len) = parse_unsigned_lebe(input)?; + let (rest, rest_rest) = rest.try_split_at(aug_data_len as usize)?; let mut augmentation_data = AugmentationData::default(); if let Some(encoding) = augmentation.lsda { let (_, lsda) = - try!(parse_encoded_pointer(encoding, bases, address_size, section.section(), rest)); + parse_encoded_pointer(encoding, bases, address_size, section.section(), rest)?; augmentation_data.lsda = Some(lsda); } Ok((rest_rest, augmentation_data)) @@ -936,14 +945,16 @@ impl<'input, Endian, Section> CommonInformationEntry<'input, Endian, Section> (bases: &'bases BaseAddresses, section: Section, input: EndianBuf<'input, Endian>) - -> Result, CommonInformationEntry<'input, Endian, Section>)>> { - let (rest_rest, CfiEntryCommon { - length, - format, - cie_id_or_offset: cie_id, - rest, - .. - }) = match try!(parse_cfi_entry_common::(input)) { + -> Result, + CommonInformationEntry<'input, Endian, Section>)>> { + let (rest_rest, + CfiEntryCommon { + length, + format, + cie_id_or_offset: cie_id, + rest, + .. + }) = match parse_cfi_entry_common::(input)? { None => return Ok(None), Some(common) => common, }; @@ -952,7 +963,7 @@ impl<'input, Endian, Section> CommonInformationEntry<'input, Endian, Section> return Err(Error::NotCieId); } - let entry = try!(Self::parse_rest(length, format, bases, section, rest)); + let entry = Self::parse_rest(length, format, bases, section, rest)?; Ok(Some((rest_rest, entry))) } @@ -962,44 +973,44 @@ impl<'input, Endian, Section> CommonInformationEntry<'input, Endian, Section> section: Section, rest: EndianBuf<'input, Endian>) -> Result> { - let (rest, version) = try!(parse_u8(rest.into())); + let (rest, version) = parse_u8(rest.into())?; if !Section::compatible_version(version) { return Err(Error::UnknownVersion); } - let (rest, augmentation_string) = try!(parse_null_terminated_string(rest)); + let (rest, augmentation_string) = parse_null_terminated_string(rest)?; let rest = EndianBuf::new(rest); let aug_len = augmentation_string.to_bytes().len(); let (rest, address_size, segment_size) = if Section::has_address_and_segment_sizes(version) { - let (rest, address_size) = try!(parse_u8e(rest)); - let (rest, segment_size) = try!(parse_u8e(rest)); + let (rest, address_size) = parse_u8e(rest)?; + let (rest, segment_size) = parse_u8e(rest)?; (rest, address_size, segment_size) } else { // Assume no segments and native word size. (rest, mem::size_of::() as u8, 0) }; - let (rest, code_alignment_factor) = try!(parse_unsigned_lebe(rest)); - let (rest, data_alignment_factor) = try!(parse_signed_lebe(rest)); + let (rest, code_alignment_factor) = parse_unsigned_lebe(rest)?; + let (rest, data_alignment_factor) = parse_signed_lebe(rest)?; let (rest, return_address_register) = match Section::return_address_register_encoding(version) { ReturnAddressRegisterEncoding::U8 => { - let (rest, reg) = try!(parse_u8e(rest)); + let (rest, reg) = parse_u8e(rest)?; (rest, reg as u64) } - ReturnAddressRegisterEncoding::Uleb => try!(parse_unsigned_lebe(rest)), + ReturnAddressRegisterEncoding::Uleb => parse_unsigned_lebe(rest)?, }; let (rest, augmentation) = if aug_len == 0 { (rest, None) } else { - let augmentation_string = try!(str::from_utf8(augmentation_string.to_bytes()) - .map_err(|_| Error::BadUtf8)); + let augmentation_string = str::from_utf8(augmentation_string.to_bytes()) + .map_err(|_| Error::BadUtf8)?; let (rest, augmentation) = - try!(Augmentation::parse(augmentation_string, bases, address_size, section, rest)); + Augmentation::parse(augmentation_string, bases, address_size, section, rest)?; (rest, Some(augmentation)) }; @@ -1135,20 +1146,20 @@ impl<'input, Endian, Section> FrameDescriptionEntry<'input, Endian, Section> *func = Some(offset as u64); } - let cie = try!(get_cie(cie_pointer)); + let cie = get_cie(cie_pointer)?; let (rest, initial_segment) = if cie.segment_size > 0 { - try!(parse_address(rest, cie.segment_size)) + parse_address(rest, cie.segment_size)? } else { (rest, 0) }; let (rest, initial_address, address_range) = - try!(Self::parse_addresses(rest, &cie, bases, section)); + Self::parse_addresses(rest, &cie, bases, section)?; let (rest, aug_data) = if let Some(ref augmentation) = cie.augmentation { let (rest, aug_data) = - try!(AugmentationData::parse(augmentation, bases, cie.address_size, section, rest)); + AugmentationData::parse(augmentation, bases, cie.address_size, section, rest)?; (rest, Some(aug_data)) } else { (rest, None) @@ -1173,28 +1184,27 @@ impl<'input, Endian, Section> FrameDescriptionEntry<'input, Endian, Section> bases: &BaseAddresses, section: Section) -> Result<(EndianBuf<'input, Endian>, u64, u64)> { - let encoding = cie.augmentation.as_ref().and_then(|a| a.fde_address_encoding); + let encoding = cie.augmentation + .as_ref() + .and_then(|a| a.fde_address_encoding); if let Some(encoding) = encoding { - let (rest, initial_address) = try!(parse_encoded_pointer(encoding, - bases, - cie.address_size, - section.section(), - input)); + let (rest, initial_address) = + parse_encoded_pointer(encoding, bases, cie.address_size, section.section(), input)?; // Ignore indirection. let initial_address = initial_address.into(); // Address ranges cannot be relative to anything, so just grab the // data format bits from the encoding. - let (rest, address_range) = try!(parse_encoded_pointer(encoding.format(), - bases, - cie.address_size, - section.section(), - rest)); + let (rest, address_range) = parse_encoded_pointer(encoding.format(), + bases, + cie.address_size, + section.section(), + rest)?; Ok((rest, initial_address, address_range.into())) } else { - let (rest, initial_address) = try!(parse_address(input, cie.address_size)); - let (rest, address_range) = try!(parse_address(rest, cie.address_size)); + let (rest, initial_address) = parse_address(input, cie.address_size)?; + let (rest, address_range) = parse_address(rest, cie.address_size)?; Ok((rest, initial_address, address_range)) } } @@ -1231,7 +1241,9 @@ impl<'input, Endian, Section> FrameDescriptionEntry<'input, Endian, Section> /// Return true if this FDE's function is a trampoline for a signal handler. pub fn is_signal_trampoline(&self) -> bool { - self.cie().augmentation.map_or(false, |a| a.is_signal_trampoline) + self.cie() + .augmentation + .map_or(false, |a| a.is_signal_trampoline) } /// Return the address of the FDE's function's personality routine @@ -1318,8 +1330,8 @@ impl<'input, Endian, Section> FrameDescriptionEntry<'input, Endian, Section> /// ``` #[derive(Clone, Debug)] pub struct UninitializedUnwindContext<'input, Endian, Section>(Box>) + Endian, + Section>>) where Endian: Endianity, Section: UnwindSection<'input, Endian>; @@ -1350,7 +1362,7 @@ impl<'input, Endian, Section> UninitializedUnwindContext<'input, Endian, Section { let mut table = UnwindTable::new_internal(&mut self.0, cie, None); - while let Some(_) = try!(table.next_row()) {} + while let Some(_) = table.next_row()? {} } self.0.save_initial_rules(); @@ -1365,8 +1377,8 @@ impl<'input, Endian, Section> UninitializedUnwindContext<'input, Endian, Section /// more details. #[derive(Clone, Debug)] pub struct InitializedUnwindContext<'input, Endian, Section>(Box>) + Endian, + Section>>) where Endian: Endianity, Section: UnwindSection<'input, Endian>; @@ -1382,8 +1394,8 @@ impl<'input, Endian, Section> InitializedUnwindContext<'input, Endian, Section> } const MAX_UNWIND_STACK_DEPTH: usize = 4; -type UnwindContextStack<'input, Endian> = - ArrayVec<[UnwindTableRow<'input, Endian>; MAX_UNWIND_STACK_DEPTH]>; +type UnwindContextStack<'input, Endian> = ArrayVec<[UnwindTableRow<'input, Endian>; + MAX_UNWIND_STACK_DEPTH]>; #[derive(Clone, Debug, PartialEq, Eq)] struct UnwindContext<'input, Endian, Section> @@ -1457,7 +1469,8 @@ impl<'input, Endian, Section> UnwindContext<'input, Endian, Section> fn save_initial_rules(&mut self) { assert_eq!(self.is_initialized, false); - self.initial_rules.clone_from(&self.stack.last().unwrap().registers); + self.initial_rules + .clone_from(&self.stack.last().unwrap().registers); self.is_initialized = true; } @@ -1606,12 +1619,8 @@ impl<'input, 'fde, 'ctx, Endian, Section> UnwindTable<'input, 'fde, 'fde, 'ctx, /// /// These methods are guaranteed not to allocate, acquire locks, or perform any /// other signal-unsafe operations. -impl<'input, 'cie, 'fde, 'ctx, Endian, Section> UnwindTable<'input, - 'cie, - 'fde, - 'ctx, - Endian, - Section> +impl<'input, 'cie, 'fde, 'ctx, Endian, Section> + UnwindTable<'input, 'cie, 'fde, 'ctx, Endian, Section> where Endian: Endianity, Section: UnwindSection<'input, Endian> { @@ -1662,7 +1671,7 @@ impl<'input, 'cie, 'fde, 'ctx, Endian, Section> UnwindTable<'input, } Ok(Some(instruction)) => { - if try!(self.evaluate(instruction)) { + if self.evaluate(instruction)? { return Ok(Some(self.ctx.row())); } } @@ -1696,36 +1705,39 @@ impl<'input, 'cie, 'fde, 'ctx, Endian, Section> UnwindTable<'input, // Instructions that modify the CFA. DefCfa { register, offset } => { self.ctx.set_cfa(CfaRule::RegisterAndOffset { - register: register, - offset: offset as i64, - }); + register: register, + offset: offset as i64, + }); } - DefCfaSf { register, factored_offset } => { + DefCfaSf { + register, + factored_offset, + } => { let data_align = self.cie.data_alignment_factor as i64; self.ctx.set_cfa(CfaRule::RegisterAndOffset { - register: register, - offset: factored_offset * data_align, - }); + register: register, + offset: factored_offset * data_align, + }); } DefCfaRegister { register } => { - if let CfaRule::RegisterAndOffset { register: ref mut reg, .. } = *self.ctx - .cfa_mut() { + if let CfaRule::RegisterAndOffset { register: ref mut reg, .. } = + *self.ctx.cfa_mut() { *reg = register; } else { return Err(Error::CfiInstructionInInvalidContext); } } DefCfaOffset { offset } => { - if let CfaRule::RegisterAndOffset { offset: ref mut off, .. } = *self.ctx - .cfa_mut() { + if let CfaRule::RegisterAndOffset { offset: ref mut off, .. } = + *self.ctx.cfa_mut() { *off = offset as i64; } else { return Err(Error::CfiInstructionInInvalidContext); } } DefCfaOffsetSf { factored_offset } => { - if let CfaRule::RegisterAndOffset { offset: ref mut off, .. } = *self.ctx - .cfa_mut() { + if let CfaRule::RegisterAndOffset { offset: ref mut off, .. } = + *self.ctx.cfa_mut() { let data_align = self.cie.data_alignment_factor as i64; *off = factored_offset * data_align; } else { @@ -1738,38 +1750,65 @@ impl<'input, 'cie, 'fde, 'ctx, Endian, Section> UnwindTable<'input, // Instructions that define register rules. Undefined { register } => { - try!(self.ctx.set_register_rule(register, RegisterRule::Undefined)); + self.ctx + .set_register_rule(register, RegisterRule::Undefined)?; } SameValue { register } => { - try!(self.ctx.set_register_rule(register, RegisterRule::SameValue)); + self.ctx + .set_register_rule(register, RegisterRule::SameValue)?; } - Offset { register, factored_offset } => { + Offset { + register, + factored_offset, + } => { let offset = factored_offset as i64 * self.cie.data_alignment_factor; - try!(self.ctx.set_register_rule(register, RegisterRule::Offset(offset))); + self.ctx + .set_register_rule(register, RegisterRule::Offset(offset))?; } - OffsetExtendedSf { register, factored_offset } => { + OffsetExtendedSf { + register, + factored_offset, + } => { let offset = factored_offset * self.cie.data_alignment_factor; - try!(self.ctx.set_register_rule(register, RegisterRule::Offset(offset))); + self.ctx + .set_register_rule(register, RegisterRule::Offset(offset))?; } - ValOffset { register, factored_offset } => { + ValOffset { + register, + factored_offset, + } => { let offset = factored_offset as i64 * self.cie.data_alignment_factor; - try!(self.ctx.set_register_rule(register, RegisterRule::ValOffset(offset))); + self.ctx + .set_register_rule(register, RegisterRule::ValOffset(offset))?; } - ValOffsetSf { register, factored_offset } => { + ValOffsetSf { + register, + factored_offset, + } => { let offset = factored_offset * self.cie.data_alignment_factor; - try!(self.ctx.set_register_rule(register, RegisterRule::ValOffset(offset))); + self.ctx + .set_register_rule(register, RegisterRule::ValOffset(offset))?; } - Register { dest_register, src_register } => { - try!(self.ctx - .set_register_rule(dest_register, RegisterRule::Register(src_register))); + Register { + dest_register, + src_register, + } => { + self.ctx + .set_register_rule(dest_register, RegisterRule::Register(src_register))?; } - Expression { register, expression } => { + Expression { + register, + expression, + } => { let expression = RegisterRule::Expression(expression); - try!(self.ctx.set_register_rule(register, expression)); + self.ctx.set_register_rule(register, expression)?; } - ValExpression { register, expression } => { + ValExpression { + register, + expression, + } => { let expression = RegisterRule::ValExpression(expression); - try!(self.ctx.set_register_rule(register, expression)); + self.ctx.set_register_rule(register, expression)?; } Restore { register } => { let initial_rule = if let Some(rule) = self.ctx.get_initial_rule(register) { @@ -1780,12 +1819,12 @@ impl<'input, 'cie, 'fde, 'ctx, Endian, Section> UnwindTable<'input, return Err(Error::CfiInstructionInInvalidContext); }; - try!(self.ctx.set_register_rule(register, initial_rule)); + self.ctx.set_register_rule(register, initial_rule)?; } // Row push and pop instructions. RememberState => { - try!(self.ctx.push_row()); + self.ctx.push_row()?; } RestoreState => { assert!(self.ctx.stack.len() > 0); @@ -1833,9 +1872,9 @@ impl<'input, Endian> RegisterRuleMap<'input, Endian> .iter() .find(|rule| rule.0 == register) .map(|r| { - debug_assert_ne!(r.1, RegisterRule::Undefined); - r.1.clone() - }) + debug_assert_ne!(r.1, RegisterRule::Undefined); + r.1.clone() + }) .unwrap_or(RegisterRule::Undefined) } @@ -1876,17 +1915,19 @@ impl<'input, Endian> RegisterRuleMap<'input, Endian> } } -impl<'a, 'input, Endian> FromIterator<&'a (u8, RegisterRule<'input, Endian>)> for RegisterRuleMap<'input, Endian> +impl<'a, 'input, Endian> FromIterator<&'a (u8, RegisterRule<'input, Endian>)> + for RegisterRuleMap<'input, Endian> where Endian: 'a + Endianity, 'input: 'a { fn from_iter(iter: T) -> RegisterRuleMap<'input, Endian> - where T: IntoIterator)> + where T: IntoIterator)> { let iter = iter.into_iter(); let mut rules = RegisterRuleMap::default(); for &(reg, ref rule) in iter.filter(|r| r.1 != RegisterRule::Undefined) { - rules.set(reg, rule.clone()) + rules + .set(reg, rule.clone()) .expect("This is only used in tests, impl isn't exposed publicly. If you trip this, fix your test"); } @@ -1921,9 +1962,9 @@ impl<'input, Endian> Eq for RegisterRuleMap<'input, Endian> where Endian: Endian /// An unordered iterator for register rules. #[derive(Debug, Clone)] pub struct RegisterRuleIter<'iter, 'input, Endian>(::std::slice::Iter<'iter, - (u8, - RegisterRule<'input, - Endian>)>) + (u8, + RegisterRule<'input, + Endian>)>) where Endian: 'iter + Endianity, 'input: 'iter; @@ -2416,7 +2457,7 @@ impl<'input, Endian> CallFrameInstruction<'input, Endian> { fn parse(input: EndianBuf<'input, Endian>) -> Result<(EndianBuf<'input, Endian>, CallFrameInstruction<'input, Endian>)> { - let (rest, instruction) = try!(parse_u8e(input)); + let (rest, instruction) = parse_u8e(input)?; let high_bits = instruction & CFI_INSTRUCTION_HIGH_BITS_MASK; if high_bits == constants::DW_CFA_advance_loc.0 { @@ -2426,12 +2467,12 @@ impl<'input, Endian> CallFrameInstruction<'input, Endian> if high_bits == constants::DW_CFA_offset.0 { let register = instruction & CFI_INSTRUCTION_LOW_BITS_MASK; - let (rest, offset) = try!(parse_unsigned_lebe(rest)); + let (rest, offset) = parse_unsigned_lebe(rest)?; return Ok((rest, CallFrameInstruction::Offset { - register: register, - factored_offset: offset, - })); + register: register, + factored_offset: offset, + })); } if high_bits == constants::DW_CFA_restore.0 { @@ -2446,58 +2487,58 @@ impl<'input, Endian> CallFrameInstruction<'input, Endian> constants::DW_CFA_nop => Ok((rest, CallFrameInstruction::Nop)), constants::DW_CFA_set_loc => { - let (rest, address) = try!(parse_unsigned_lebe(rest)); + let (rest, address) = parse_unsigned_lebe(rest)?; Ok((rest, CallFrameInstruction::SetLoc { address: address })) } constants::DW_CFA_advance_loc1 => { - let (rest, delta) = try!(parse_u8e(rest)); + let (rest, delta) = parse_u8e(rest)?; Ok((rest, CallFrameInstruction::AdvanceLoc { delta: delta as u32 })) } constants::DW_CFA_advance_loc2 => { - let (rest, delta) = try!(parse_u16(rest)); + let (rest, delta) = parse_u16(rest)?; Ok((rest, CallFrameInstruction::AdvanceLoc { delta: delta as u32 })) } constants::DW_CFA_advance_loc4 => { - let (rest, delta) = try!(parse_u32(rest)); + let (rest, delta) = parse_u32(rest)?; Ok((rest, CallFrameInstruction::AdvanceLoc { delta: delta })) } constants::DW_CFA_offset_extended => { - let (rest, register) = try!(parse_unsigned_leb_as_u8e(rest)); - let (rest, offset) = try!(parse_unsigned_lebe(rest)); + let (rest, register) = parse_unsigned_leb_as_u8e(rest)?; + let (rest, offset) = parse_unsigned_lebe(rest)?; Ok((rest, CallFrameInstruction::Offset { - register: register, - factored_offset: offset, - })) + register: register, + factored_offset: offset, + })) } constants::DW_CFA_restore_extended => { - let (rest, register) = try!(parse_unsigned_leb_as_u8e(rest)); + let (rest, register) = parse_unsigned_leb_as_u8e(rest)?; Ok((rest, CallFrameInstruction::Restore { register: register })) } constants::DW_CFA_undefined => { - let (rest, register) = try!(parse_unsigned_leb_as_u8e(rest)); + let (rest, register) = parse_unsigned_leb_as_u8e(rest)?; Ok((rest, CallFrameInstruction::Undefined { register: register })) } constants::DW_CFA_same_value => { - let (rest, register) = try!(parse_unsigned_leb_as_u8e(rest)); + let (rest, register) = parse_unsigned_leb_as_u8e(rest)?; Ok((rest, CallFrameInstruction::SameValue { register: register })) } constants::DW_CFA_register => { - let (rest, dest) = try!(parse_unsigned_leb_as_u8e(rest)); - let (rest, src) = try!(parse_unsigned_leb_as_u8e(rest)); + let (rest, dest) = parse_unsigned_leb_as_u8e(rest)?; + let (rest, src) = parse_unsigned_leb_as_u8e(rest)?; Ok((rest, CallFrameInstruction::Register { - dest_register: dest, - src_register: src, - })) + dest_register: dest, + src_register: src, + })) } constants::DW_CFA_remember_state => Ok((rest, CallFrameInstruction::RememberState)), @@ -2505,93 +2546,93 @@ impl<'input, Endian> CallFrameInstruction<'input, Endian> constants::DW_CFA_restore_state => Ok((rest, CallFrameInstruction::RestoreState)), constants::DW_CFA_def_cfa => { - let (rest, register) = try!(parse_unsigned_leb_as_u8e(rest)); - let (rest, offset) = try!(parse_unsigned_lebe(rest)); + let (rest, register) = parse_unsigned_leb_as_u8e(rest)?; + let (rest, offset) = parse_unsigned_lebe(rest)?; Ok((rest, CallFrameInstruction::DefCfa { - register: register, - offset: offset, - })) + register: register, + offset: offset, + })) } constants::DW_CFA_def_cfa_register => { - let (rest, register) = try!(parse_unsigned_leb_as_u8e(rest)); + let (rest, register) = parse_unsigned_leb_as_u8e(rest)?; Ok((rest, CallFrameInstruction::DefCfaRegister { register: register })) } constants::DW_CFA_def_cfa_offset => { - let (rest, offset) = try!(parse_unsigned_lebe(rest)); + let (rest, offset) = parse_unsigned_lebe(rest)?; Ok((rest, CallFrameInstruction::DefCfaOffset { offset: offset })) } constants::DW_CFA_def_cfa_expression => { - let (rest, expression) = try!(parse_length_uleb_value(rest)); + let (rest, expression) = parse_length_uleb_value(rest)?; Ok((rest, CallFrameInstruction::DefCfaExpression { expression: expression })) } constants::DW_CFA_expression => { - let (rest, register) = try!(parse_unsigned_leb_as_u8e(rest)); - let (rest, expression) = try!(parse_length_uleb_value(rest)); + let (rest, register) = parse_unsigned_leb_as_u8e(rest)?; + let (rest, expression) = parse_length_uleb_value(rest)?; Ok((rest, CallFrameInstruction::Expression { - register: register, - expression: expression, - })) + register: register, + expression: expression, + })) } constants::DW_CFA_offset_extended_sf => { - let (rest, register) = try!(parse_unsigned_leb_as_u8e(rest)); - let (rest, offset) = try!(parse_signed_lebe(rest)); + let (rest, register) = parse_unsigned_leb_as_u8e(rest)?; + let (rest, offset) = parse_signed_lebe(rest)?; Ok((rest, CallFrameInstruction::OffsetExtendedSf { - register: register, - factored_offset: offset, - })) + register: register, + factored_offset: offset, + })) } constants::DW_CFA_def_cfa_sf => { - let (rest, register) = try!(parse_unsigned_leb_as_u8e(rest)); - let (rest, offset) = try!(parse_signed_lebe(rest)); + let (rest, register) = parse_unsigned_leb_as_u8e(rest)?; + let (rest, offset) = parse_signed_lebe(rest)?; Ok((rest, CallFrameInstruction::DefCfaSf { - register: register, - factored_offset: offset, - })) + register: register, + factored_offset: offset, + })) } constants::DW_CFA_def_cfa_offset_sf => { - let (rest, offset) = try!(parse_signed_lebe(rest)); + let (rest, offset) = parse_signed_lebe(rest)?; Ok((rest, CallFrameInstruction::DefCfaOffsetSf { factored_offset: offset })) } constants::DW_CFA_val_offset => { - let (rest, register) = try!(parse_unsigned_leb_as_u8e(rest)); - let (rest, offset) = try!(parse_unsigned_lebe(rest)); + let (rest, register) = parse_unsigned_leb_as_u8e(rest)?; + let (rest, offset) = parse_unsigned_lebe(rest)?; Ok((rest, CallFrameInstruction::ValOffset { - register: register, - factored_offset: offset, - })) + register: register, + factored_offset: offset, + })) } constants::DW_CFA_val_offset_sf => { - let (rest, register) = try!(parse_unsigned_leb_as_u8e(rest)); - let (rest, offset) = try!(parse_signed_lebe(rest)); + let (rest, register) = parse_unsigned_leb_as_u8e(rest)?; + let (rest, offset) = parse_signed_lebe(rest)?; Ok((rest, CallFrameInstruction::ValOffsetSf { - register: register, - factored_offset: offset, - })) + register: register, + factored_offset: offset, + })) } constants::DW_CFA_val_expression => { - let (rest, register) = try!(parse_unsigned_leb_as_u8e(rest)); - let (rest, expression) = try!(parse_length_uleb_value(rest)); + let (rest, register) = parse_unsigned_leb_as_u8e(rest)?; + let (rest, expression) = parse_length_uleb_value(rest)?; Ok((rest, CallFrameInstruction::ValExpression { - register: register, - expression: expression, - })) + register: register, + expression: expression, + })) } otherwise => Err(Error::UnknownCallFrameInstruction(otherwise)), @@ -2730,7 +2771,8 @@ mod tests { } Format::Dwarf64 => { let section = self.e32(endian, 0xffffffff); - section.e64(endian, &length) + section + .e64(endian, &length) .mark(&start) .e64(endian, 0xffffffffffffffff) } @@ -2751,7 +2793,8 @@ mod tests { section }; - let section = section.uleb(cie.code_alignment_factor) + let section = section + .uleb(cie.code_alignment_factor) .sleb(cie.data_alignment_factor) .uleb(cie.return_address_register) .append_bytes(cie.initial_instructions.into()) @@ -2780,8 +2823,7 @@ mod tests { let section = match T::cie_offset_encoding(fde.format) { CieOffsetEncoding::U32 => { - let section = self.e32(endian, &length) - .mark(&start); + let section = self.e32(endian, &length).mark(&start); match cie_offset.to_labelornum() { LabelOrNum::Label(ref l) => section.e32(endian, l), LabelOrNum::Num(o) => section.e32(endian, o as u32), @@ -2789,7 +2831,8 @@ mod tests { } CieOffsetEncoding::U64 => { let section = self.e32(endian, 0xffffffff); - section.e64(endian, &length) + section + .e64(endian, &length) .mark(&start) .e64(endian, cie_offset) } @@ -2804,22 +2847,29 @@ mod tests { let section = match fde.cie.address_size { 4 => { - section.e32(endian, fde.initial_address as u32) + section + .e32(endian, fde.initial_address as u32) .e32(endian, fde.address_range as u32) } 8 => { - section.e64(endian, fde.initial_address) + section + .e64(endian, fde.initial_address) .e64(endian, fde.address_range) } x => panic!("Unsupported address size: {}", x), }; let section = if let Some(ref augmentation) = fde.augmentation { - let cie_aug = fde.cie.augmentation.expect("FDE has augmentation, but CIE doesn't"); + let cie_aug = fde.cie + .augmentation + .expect("FDE has augmentation, but CIE doesn't"); if let Some(lsda) = augmentation.lsda { // We only support writing `DW_EH_PE_absptr` here. - assert_eq!(cie_aug.lsda.expect("FDE has lsda, but CIE doesn't").format(), + assert_eq!(cie_aug + .lsda + .expect("FDE has lsda, but CIE doesn't") + .format(), constants::DW_EH_PE_absptr); // Augmentation data length @@ -2961,9 +3011,7 @@ mod tests { #[test] fn test_parse_cie_32_ok() { let expected_rest = [1, 2, 3, 4, 5, 6, 7, 8, 9]; - let expected_instrs: Vec<_> = (0..4) - .map(|_| constants::DW_CFA_nop.0) - .collect(); + let expected_instrs: Vec<_> = (0..4).map(|_| constants::DW_CFA_nop.0).collect(); let mut cie: DebugFrameCie = DebugFrameCie { length: 0, @@ -3131,9 +3179,7 @@ mod tests { fn test_parse_fde_32_with_segment_ok() { let expected_rest = [1, 2, 3, 4, 5, 6, 7, 8, 9]; let cie_offset = 0xbad0bad1; - let expected_instrs: Vec<_> = (0..92) - .map(|_| constants::DW_CFA_nop.0) - .collect(); + let expected_instrs: Vec<_> = (0..92).map(|_| constants::DW_CFA_nop.0).collect(); let cie = DebugFrameCie { length: 100, @@ -3180,9 +3226,7 @@ mod tests { fn test_parse_fde_64_ok() { let expected_rest = [1, 2, 3, 4, 5, 6, 7, 8, 9]; let cie_offset = 0xbad0bad1; - let expected_instrs: Vec<_> = (0..7) - .map(|_| constants::DW_CFA_nop.0) - .collect(); + let expected_instrs: Vec<_> = (0..7).map(|_| constants::DW_CFA_nop.0).collect(); let cie = DebugFrameCie { length: 100, @@ -3228,9 +3272,7 @@ mod tests { #[test] fn test_parse_cfi_entry_on_cie_32_ok() { let expected_rest = [1, 2, 3, 4, 5, 6, 7, 8, 9]; - let expected_instrs: Vec<_> = (0..4) - .map(|_| constants::DW_CFA_nop.0) - .collect(); + let expected_instrs: Vec<_> = (0..4).map(|_| constants::DW_CFA_nop.0).collect(); let mut cie = DebugFrameCie { length: 0, @@ -3261,9 +3303,7 @@ mod tests { fn test_parse_cfi_entry_on_fde_32_ok() { let cie_offset = 0x12345678; let expected_rest = [1, 2, 3, 4, 5, 6, 7, 8, 9]; - let expected_instrs: Vec<_> = (0..4) - .map(|_| constants::DW_CFA_nop.0) - .collect(); + let expected_instrs: Vec<_> = (0..4).map(|_| constants::DW_CFA_nop.0).collect(); let cie = DebugFrameCie { length: 0, @@ -3319,21 +3359,13 @@ mod tests { #[test] fn test_cfi_entries_iter() { - let expected_instrs1: Vec<_> = (0..4) - .map(|_| constants::DW_CFA_nop.0) - .collect(); + let expected_instrs1: Vec<_> = (0..4).map(|_| constants::DW_CFA_nop.0).collect(); - let expected_instrs2: Vec<_> = (0..8) - .map(|_| constants::DW_CFA_nop.0) - .collect(); + let expected_instrs2: Vec<_> = (0..8).map(|_| constants::DW_CFA_nop.0).collect(); - let expected_instrs3: Vec<_> = (0..12) - .map(|_| constants::DW_CFA_nop.0) - .collect(); + let expected_instrs3: Vec<_> = (0..12).map(|_| constants::DW_CFA_nop.0).collect(); - let expected_instrs4: Vec<_> = (0..16) - .map(|_| constants::DW_CFA_nop.0) - .collect(); + let expected_instrs4: Vec<_> = (0..16).map(|_| constants::DW_CFA_nop.0).collect(); let mut cie1 = DebugFrameCie { length: 0, @@ -3397,7 +3429,8 @@ mod tests { instructions: EndianBuf::::new(&expected_instrs4), }; - let section = section.fde(Endian::Big, &cie1_location, &mut fde1) + let section = section + .fde(Endian::Big, &cie1_location, &mut fde1) .fde(Endian::Big, &cie2_location, &mut fde2); section.start().set_const(0); @@ -3513,9 +3546,9 @@ mod tests { assert_eq!(CallFrameInstruction::parse(input), Ok((EndianBuf::new(&expected_rest), CallFrameInstruction::Offset { - register: expected_reg as u8, - factored_offset: expected_offset, - }))); + register: expected_reg as u8, + factored_offset: expected_offset, + }))); } #[test] @@ -3619,9 +3652,9 @@ mod tests { assert_eq!(CallFrameInstruction::parse(input), Ok((EndianBuf::new(&expected_rest), CallFrameInstruction::Offset { - register: expected_reg as u8, - factored_offset: expected_offset, - }))); + register: expected_reg as u8, + factored_offset: expected_offset, + }))); } #[test] @@ -3684,9 +3717,9 @@ mod tests { assert_eq!(CallFrameInstruction::parse(input), Ok((EndianBuf::new(&expected_rest), CallFrameInstruction::Register { - dest_register: expected_dest_reg as u8, - src_register: expected_src_reg as u8, - }))); + dest_register: expected_dest_reg as u8, + src_register: expected_src_reg as u8, + }))); } #[test] @@ -3728,9 +3761,9 @@ mod tests { assert_eq!(CallFrameInstruction::parse(input), Ok((EndianBuf::new(&expected_rest), CallFrameInstruction::DefCfa { - register: expected_reg as u8, - offset: expected_offset, - }))); + register: expected_reg as u8, + offset: expected_offset, + }))); } #[test] @@ -3787,8 +3820,8 @@ mod tests { assert_eq!(CallFrameInstruction::parse(input), Ok((EndianBuf::new(&expected_rest), CallFrameInstruction::DefCfaExpression { - expression: EndianBuf::new(&expected_expr), - }))); + expression: EndianBuf::new(&expected_expr), + }))); } #[test] @@ -3817,9 +3850,9 @@ mod tests { assert_eq!(CallFrameInstruction::parse(input), Ok((EndianBuf::new(&expected_rest), CallFrameInstruction::Expression { - register: expected_reg as u8, - expression: EndianBuf::new(&expected_expr), - }))); + register: expected_reg as u8, + expression: EndianBuf::new(&expected_expr), + }))); } #[test] @@ -3837,9 +3870,9 @@ mod tests { assert_eq!(CallFrameInstruction::parse(input), Ok((EndianBuf::new(&expected_rest), CallFrameInstruction::OffsetExtendedSf { - register: expected_reg as u8, - factored_offset: expected_offset, - }))); + register: expected_reg as u8, + factored_offset: expected_offset, + }))); } #[test] @@ -3857,9 +3890,9 @@ mod tests { assert_eq!(CallFrameInstruction::parse(input), Ok((EndianBuf::new(&expected_rest), CallFrameInstruction::DefCfaSf { - register: expected_reg as u8, - factored_offset: expected_offset, - }))); + register: expected_reg as u8, + factored_offset: expected_offset, + }))); } #[test] @@ -3892,9 +3925,9 @@ mod tests { assert_eq!(CallFrameInstruction::parse(input), Ok((EndianBuf::new(&expected_rest), CallFrameInstruction::ValOffset { - register: expected_reg as u8, - factored_offset: expected_offset, - }))); + register: expected_reg as u8, + factored_offset: expected_offset, + }))); } #[test] @@ -3912,9 +3945,9 @@ mod tests { assert_eq!(CallFrameInstruction::parse(input), Ok((EndianBuf::new(&expected_rest), CallFrameInstruction::ValOffsetSf { - register: expected_reg as u8, - factored_offset: expected_offset, - }))); + register: expected_reg as u8, + factored_offset: expected_offset, + }))); } #[test] @@ -3943,9 +3976,9 @@ mod tests { assert_eq!(CallFrameInstruction::parse(input), Ok((EndianBuf::new(&expected_rest), CallFrameInstruction::ValExpression { - register: expected_reg as u8, - expression: EndianBuf::new(&expected_expr), - }))); + register: expected_reg as u8, + expression: EndianBuf::new(&expected_expr), + }))); } #[test] @@ -3988,9 +4021,9 @@ mod tests { assert_eq!(iter.next(), Ok(Some(CallFrameInstruction::ValExpression { - register: expected_reg as u8, - expression: EndianBuf::new(&expected_expr), - }))); + register: expected_reg as u8, + expression: EndianBuf::new(&expected_expr), + }))); assert_eq!(iter.next(), Ok(Some(CallFrameInstruction::AdvanceLoc { delta: expected_delta as u32 }))); @@ -4085,14 +4118,14 @@ mod tests { let ctx = UnwindContext::new(); let mut expected = ctx.clone(); expected.set_cfa(CfaRule::RegisterAndOffset { - register: 42, - offset: 36, - }); + register: 42, + offset: 36, + }); let instructions = [(Ok(false), CallFrameInstruction::DefCfa { - register: 42, - offset: 36, - })]; + register: 42, + offset: 36, + })]; assert_eval(ctx, expected, cie, None, instructions); } @@ -4102,14 +4135,14 @@ mod tests { let ctx = UnwindContext::new(); let mut expected = ctx.clone(); expected.set_cfa(CfaRule::RegisterAndOffset { - register: 42, - offset: 36 * cie.data_alignment_factor as i64, - }); + register: 42, + offset: 36 * cie.data_alignment_factor as i64, + }); let instructions = [(Ok(false), CallFrameInstruction::DefCfaSf { - register: 42, - factored_offset: 36, - })]; + register: 42, + factored_offset: 36, + })]; assert_eval(ctx, expected, cie, None, instructions); } @@ -4118,14 +4151,14 @@ mod tests { let cie: DebugFrameCie<_> = make_test_cie(); let mut ctx = UnwindContext::new(); ctx.set_cfa(CfaRule::RegisterAndOffset { - register: 3, - offset: 8, - }); + register: 3, + offset: 8, + }); let mut expected = ctx.clone(); expected.set_cfa(CfaRule::RegisterAndOffset { - register: 42, - offset: 8, - }); + register: 42, + offset: 8, + }); let instructions = [(Ok(false), CallFrameInstruction::DefCfaRegister { register: 42 })]; assert_eval(ctx, expected, cie, None, instructions); } @@ -4146,14 +4179,14 @@ mod tests { let cie: DebugFrameCie<_> = make_test_cie(); let mut ctx = UnwindContext::new(); ctx.set_cfa(CfaRule::RegisterAndOffset { - register: 3, - offset: 8, - }); + register: 3, + offset: 8, + }); let mut expected = ctx.clone(); expected.set_cfa(CfaRule::RegisterAndOffset { - register: 3, - offset: 42, - }); + register: 3, + offset: 42, + }); let instructions = [(Ok(false), CallFrameInstruction::DefCfaOffset { offset: 42 })]; assert_eval(ctx, expected, cie, None, instructions); } @@ -4187,7 +4220,9 @@ mod tests { let cie: DebugFrameCie<_> = make_test_cie(); let ctx = UnwindContext::new(); let mut expected = ctx.clone(); - expected.set_register_rule(5, RegisterRule::Undefined).unwrap(); + expected + .set_register_rule(5, RegisterRule::Undefined) + .unwrap(); let instructions = [(Ok(false), CallFrameInstruction::Undefined { register: 5 })]; assert_eval(ctx, expected, cie, None, instructions); } @@ -4197,7 +4232,9 @@ mod tests { let cie: DebugFrameCie<_> = make_test_cie(); let ctx = UnwindContext::new(); let mut expected = ctx.clone(); - expected.set_register_rule(0, RegisterRule::SameValue).unwrap(); + expected + .set_register_rule(0, RegisterRule::SameValue) + .unwrap(); let instructions = [(Ok(false), CallFrameInstruction::SameValue { register: 0 })]; assert_eval(ctx, expected, cie, None, instructions); } @@ -4207,12 +4244,14 @@ mod tests { let cie: DebugFrameCie<_> = make_test_cie(); let ctx = UnwindContext::new(); let mut expected = ctx.clone(); - expected.set_register_rule(2, RegisterRule::Offset(3 * cie.data_alignment_factor)).unwrap(); + expected + .set_register_rule(2, RegisterRule::Offset(3 * cie.data_alignment_factor)) + .unwrap(); let instructions = [(Ok(false), CallFrameInstruction::Offset { - register: 2, - factored_offset: 3, - })]; + register: 2, + factored_offset: 3, + })]; assert_eval(ctx, expected, cie, None, instructions); } @@ -4221,13 +4260,14 @@ mod tests { let cie: DebugFrameCie<_> = make_test_cie(); let ctx = UnwindContext::new(); let mut expected = ctx.clone(); - expected.set_register_rule(4, RegisterRule::Offset(-3 * cie.data_alignment_factor)) + expected + .set_register_rule(4, RegisterRule::Offset(-3 * cie.data_alignment_factor)) .unwrap(); let instructions = [(Ok(false), CallFrameInstruction::OffsetExtendedSf { - register: 4, - factored_offset: -3, - })]; + register: 4, + factored_offset: -3, + })]; assert_eval(ctx, expected, cie, None, instructions); } @@ -4236,13 +4276,14 @@ mod tests { let cie: DebugFrameCie<_> = make_test_cie(); let ctx = UnwindContext::new(); let mut expected = ctx.clone(); - expected.set_register_rule(5, RegisterRule::ValOffset(7 * cie.data_alignment_factor)) + expected + .set_register_rule(5, RegisterRule::ValOffset(7 * cie.data_alignment_factor)) .unwrap(); let instructions = [(Ok(false), CallFrameInstruction::ValOffset { - register: 5, - factored_offset: 7, - })]; + register: 5, + factored_offset: 7, + })]; assert_eval(ctx, expected, cie, None, instructions); } @@ -4251,13 +4292,14 @@ mod tests { let cie: DebugFrameCie<_> = make_test_cie(); let ctx = UnwindContext::new(); let mut expected = ctx.clone(); - expected.set_register_rule(5, RegisterRule::ValOffset(-7 * cie.data_alignment_factor)) + expected + .set_register_rule(5, RegisterRule::ValOffset(-7 * cie.data_alignment_factor)) .unwrap(); let instructions = [(Ok(false), CallFrameInstruction::ValOffsetSf { - register: 5, - factored_offset: -7, - })]; + register: 5, + factored_offset: -7, + })]; assert_eval(ctx, expected, cie, None, instructions); } @@ -4267,12 +4309,14 @@ mod tests { let cie: DebugFrameCie<_> = make_test_cie(); let ctx = UnwindContext::new(); let mut expected = ctx.clone(); - expected.set_register_rule(9, RegisterRule::Expression(EndianBuf::new(&expr))).unwrap(); + expected + .set_register_rule(9, RegisterRule::Expression(EndianBuf::new(&expr))) + .unwrap(); let instructions = [(Ok(false), CallFrameInstruction::Expression { - register: 9, - expression: EndianBuf::new(&expr), - })]; + register: 9, + expression: EndianBuf::new(&expr), + })]; assert_eval(ctx, expected, cie, None, instructions); } @@ -4282,12 +4326,14 @@ mod tests { let cie: DebugFrameCie<_> = make_test_cie(); let ctx = UnwindContext::new(); let mut expected = ctx.clone(); - expected.set_register_rule(9, RegisterRule::ValExpression(EndianBuf::new(&expr))).unwrap(); + expected + .set_register_rule(9, RegisterRule::ValExpression(EndianBuf::new(&expr))) + .unwrap(); let instructions = [(Ok(false), CallFrameInstruction::ValExpression { - register: 9, - expression: EndianBuf::new(&expr), - })]; + register: 9, + expression: EndianBuf::new(&expr), + })]; assert_eval(ctx, expected, cie, None, instructions); } @@ -4482,7 +4528,8 @@ mod tests { register: 4, offset: -12, }, - registers: [(0, RegisterRule::Offset(-16)), (3, RegisterRule::Offset(-4))] + registers: [(0, RegisterRule::Offset(-16)), + (3, RegisterRule::Offset(-4))] .into_iter() .collect(), }; @@ -4521,9 +4568,7 @@ mod tests { .sleb(-12); let instrs1 = instrs1.get_contents().unwrap(); - let instrs2: Vec<_> = (0..8) - .map(|_| constants::DW_CFA_nop.0) - .collect(); + let instrs2: Vec<_> = (0..8).map(|_| constants::DW_CFA_nop.0).collect(); let instrs3 = Section::with_endian(Endian::Big) // Initial instructions form a row, advance the address by 100. @@ -4535,9 +4580,7 @@ mod tests { .sleb(-16); let instrs3 = instrs3.get_contents().unwrap(); - let instrs4: Vec<_> = (0..16) - .map(|_| constants::DW_CFA_nop.0) - .collect(); + let instrs4: Vec<_> = (0..16).map(|_| constants::DW_CFA_nop.0).collect(); let mut cie1 = DebugFrameCie { length: 0, @@ -4601,7 +4644,8 @@ mod tests { instructions: EndianBuf::::new(&instrs4), }; - let section = section.fde(Endian::Big, &cie1_location, &mut fde1) + let section = section + .fde(Endian::Big, &cie1_location, &mut fde1) .fde(Endian::Big, &cie2_location, &mut fde2); section.start().set_const(0); diff --git a/src/lib.rs b/src/lib.rs index ba2fd51ca..c11fe79cd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -233,7 +233,7 @@ pub use unit::{AttrsIter, Attribute, AttributeValue}; /// /// let debug_info: DebugInfo = load_section(|_: &'static str| &buf); /// ``` -pub trait Section<'input> : From<&'input [u8]> { +pub trait Section<'input>: From<&'input [u8]> { /// Returns the ELF section name for this type. fn section_name() -> &'static str; } diff --git a/src/line.rs b/src/line.rs index 6dacc4bac..0ed3796f3 100644 --- a/src/line.rs +++ b/src/line.rs @@ -74,8 +74,7 @@ impl<'input, Endian> DebugLine<'input, Endian> return Err(parser::Error::UnexpectedEof); } let input = self.debug_line_section.range_from(offset.0..); - let (_, header) = - try!(LineNumberProgramHeader::parse(input, address_size, comp_dir, comp_name)); + let (_, header) = LineNumberProgramHeader::parse(input, address_size, comp_dir, comp_name)?; let program = IncompleteLineNumberProgram { header: header }; Ok(program) } @@ -109,7 +108,8 @@ pub trait LineNumberProgram<'input, Endian> fn add_file(&mut self, file: FileEntry<'input>); } -impl<'input, Endian> LineNumberProgram<'input, Endian> for IncompleteLineNumberProgram<'input, Endian> +impl<'input, Endian> LineNumberProgram<'input, Endian> + for IncompleteLineNumberProgram<'input, Endian> where Endian: Endianity { fn header<'a>(&'a self) -> &'a LineNumberProgramHeader<'input, Endian> { @@ -120,14 +120,15 @@ impl<'input, Endian> LineNumberProgram<'input, Endian> for IncompleteLineNumberP } } -impl<'program, 'input, Endian> LineNumberProgram<'input, Endian> for &'program CompleteLineNumberProgram<'input, Endian> +impl<'program, 'input, Endian> LineNumberProgram<'input, Endian> + for &'program CompleteLineNumberProgram<'input, Endian> where Endian: Endianity { fn header<'a>(&'a self) -> &'a LineNumberProgramHeader<'input, Endian> { &self.header } fn add_file(&mut self, _: FileEntry<'input>) { -// Nop. Our file table is already complete. + // Nop. Our file table is already complete. } } @@ -150,7 +151,7 @@ pub struct StateMachine<'input, Program, Endian> type OneShotStateMachine<'input, Endian> = StateMachine<'input, IncompleteLineNumberProgram<'input, - Endian>, + Endian>, Endian>; type ResumedStateMachine<'program, 'input, Endian> = @@ -206,8 +207,8 @@ impl<'input, Program, Endian> StateMachine<'input, Program, Endian> /// Step 2 of section 6.2.5.1 fn apply_operation_advance(&mut self, operation_advance: u64) { let minimum_instruction_length = self.header().minimum_instruction_length as u64; - let maximum_operations_per_instruction = - self.header().maximum_operations_per_instruction as u64; + let maximum_operations_per_instruction = self.header().maximum_operations_per_instruction as + u64; let op_index_with_advance = self.row.registers.op_index + operation_advance; @@ -363,7 +364,9 @@ impl<'input, Program, Endian> StateMachine<'input, Program, Endian> // as specified in Section 6.2.5.3. // Split the borrow here, rather than calling `self.header()`. - self.row.registers.reset(self.program.header().default_is_stmt); + self.row + .registers + .reset(self.program.header().default_is_stmt); } else { // Previous opcode was one of: // - Special - specified in Section 6.2.5.1, steps 4-7 @@ -553,9 +556,9 @@ impl<'input> Opcode<'input> { where Endian: 'header + Endianity, 'input: 'header { - let (rest, opcode) = try!(parser::parse_u8(input)); + let (rest, opcode) = parser::parse_u8(input)?; if opcode == 0 { - let (rest, length) = try!(parser::parse_unsigned_leb(rest)); + let (rest, length) = parser::parse_unsigned_leb(rest)?; let length = length as usize; if rest.len() < length { return Err(parser::Error::UnexpectedEof); @@ -563,25 +566,24 @@ impl<'input> Opcode<'input> { let instr_rest = &rest[..length]; let rest = &rest[length..]; - let (instr_rest, opcode) = try!(parser::parse_u8(instr_rest)); + let (instr_rest, opcode) = parser::parse_u8(instr_rest)?; match constants::DwLne(opcode) { constants::DW_LNE_end_sequence => Ok((rest, Opcode::EndSequence)), constants::DW_LNE_set_address => { - let (_, address) = - try!(parser::parse_address(EndianBuf::::new(instr_rest), - header.address_size)); + let (_, address) = parser::parse_address(EndianBuf::::new(instr_rest), + header.address_size)?; Ok((rest, Opcode::SetAddress(address))) } constants::DW_LNE_define_file => { - let (_, entry) = try!(FileEntry::parse(instr_rest)); + let (_, entry) = FileEntry::parse(instr_rest)?; Ok((rest, Opcode::DefineFile(entry))) } constants::DW_LNE_set_discriminator => { - let (_, discriminator) = try!(parser::parse_unsigned_leb(instr_rest)); + let (_, discriminator) = parser::parse_unsigned_leb(instr_rest)?; Ok((rest, Opcode::SetDiscriminator(discriminator))) } @@ -594,22 +596,22 @@ impl<'input> Opcode<'input> { constants::DW_LNS_copy => Ok((rest, Opcode::Copy)), constants::DW_LNS_advance_pc => { - let (rest, advance) = try!(parser::parse_unsigned_leb(rest)); + let (rest, advance) = parser::parse_unsigned_leb(rest)?; Ok((rest, Opcode::AdvancePc(advance))) } constants::DW_LNS_advance_line => { - let (rest, increment) = try!(parser::parse_signed_leb(rest)); + let (rest, increment) = parser::parse_signed_leb(rest)?; Ok((rest, Opcode::AdvanceLine(increment))) } constants::DW_LNS_set_file => { - let (rest, file) = try!(parser::parse_unsigned_leb(rest)); + let (rest, file) = parser::parse_unsigned_leb(rest)?; Ok((rest, Opcode::SetFile(file))) } constants::DW_LNS_set_column => { - let (rest, column) = try!(parser::parse_unsigned_leb(rest)); + let (rest, column) = parser::parse_unsigned_leb(rest)?; Ok((rest, Opcode::SetColumn(column))) } @@ -620,7 +622,7 @@ impl<'input> Opcode<'input> { constants::DW_LNS_const_add_pc => Ok((rest, Opcode::ConstAddPc)), constants::DW_LNS_fixed_advance_pc => { - let (rest, advance) = try!(parser::parse_u16(EndianBuf::::new(rest))); + let (rest, advance) = parser::parse_u16(EndianBuf::::new(rest))?; Ok((rest.into(), Opcode::FixedAddPc(advance))) } @@ -629,7 +631,7 @@ impl<'input> Opcode<'input> { constants::DW_LNS_set_epilogue_begin => Ok((rest, Opcode::SetEpilogueBegin)), constants::DW_LNS_set_isa => { - let (rest, isa) = try!(parser::parse_unsigned_leb(rest)); + let (rest, isa) = parser::parse_unsigned_leb(rest)?; Ok((rest, Opcode::SetIsa(isa))) } @@ -638,7 +640,7 @@ impl<'input> Opcode<'input> { } otherwise if header.standard_opcode_lengths[(opcode - 1) as usize] == 1 => { - let (rest, arg) = try!(parser::parse_unsigned_leb(rest)); + let (rest, arg) = parser::parse_unsigned_leb(rest)?; Ok((rest, Opcode::UnknownStandard1(otherwise, arg))) } @@ -647,7 +649,7 @@ impl<'input> Opcode<'input> { let mut args = Vec::with_capacity(num_args as usize); let mut rest = rest; for _ in 0..num_args { - let (rest1, arg) = try!(parser::parse_unsigned_leb(rest)); + let (rest1, arg) = parser::parse_unsigned_leb(rest)?; args.push(arg); rest = rest1; } @@ -721,9 +723,8 @@ impl<'input, Endian> OpcodesIter<'input, Endian> fn remove_trailing(&self, other: &OpcodesIter<'input, Endian>) -> OpcodesIter<'input, Endian> { debug_assert!(other.input.len() < self.input.len()); debug_assert!(other.input.as_ptr() > self.input.as_ptr()); - debug_assert!(other.input.as_ptr() <= unsafe { - self.input.as_ptr().offset(self.input.len() as isize) - }); + debug_assert!(other.input.as_ptr() <= + unsafe { self.input.as_ptr().offset(self.input.len() as isize) }); OpcodesIter { input: self.input.split_at(self.input.len() - other.input.len()).0, endian: self.endian, @@ -751,9 +752,9 @@ impl<'input, Endian> OpcodesIter<'input, Endian> } Opcode::parse(header, self.input).map(|(rest, opcode)| { - self.input = rest; - Some(opcode) - }) + self.input = rest; + Some(opcode) + }) } } @@ -1154,19 +1155,19 @@ impl<'input, Endian> LineNumberProgramHeader<'input, Endian> comp_dir: Option<&'input ffi::CStr>, comp_name: Option<&'input ffi::CStr>) -> parser::Result<(EndianBuf<'input, Endian>, LineNumberProgramHeader<'input, Endian>)> { - let (rest, (unit_length, format)) = try!(parser::parse_initial_length(input)); + let (rest, (unit_length, format)) = parser::parse_initial_length(input)?; if (rest.len() as u64) < unit_length { return Err(parser::Error::UnexpectedEof); } let next_header_input = rest.range_from(unit_length as usize..); let rest = rest.range_to(..unit_length as usize); - let (rest, version) = try!(parser::parse_u16(rest)); + let (rest, version) = parser::parse_u16(rest)?; if version < 2 || version > 4 { return Err(parser::Error::UnknownVersion); } - let (rest, header_length) = try!(parser::parse_word(rest, format)); + let (rest, header_length) = parser::parse_word(rest, format)?; if header_length as usize > rest.len() { return Err(parser::Error::UnitHeaderLengthTooShort); @@ -1174,7 +1175,7 @@ impl<'input, Endian> LineNumberProgramHeader<'input, Endian> let program_buf = rest.range_from(header_length as usize..); let rest = rest.range_to(..header_length as usize); - let (rest, minimum_instruction_length) = try!(parser::parse_u8(rest.0)); + let (rest, minimum_instruction_length) = parser::parse_u8(rest.0)?; if minimum_instruction_length == 0 { return Err(parser::Error::MinimumInstructionLengthZero); } @@ -1182,7 +1183,7 @@ impl<'input, Endian> LineNumberProgramHeader<'input, Endian> // This field did not exist before DWARF 4, but is specified to be 1 for // non-VLIW architectures, which makes it a no-op. let (rest, maximum_operations_per_instruction) = if version >= 4 { - try!(parser::parse_u8(rest)) + parser::parse_u8(rest)? } else { (rest, 1) }; @@ -1190,14 +1191,14 @@ impl<'input, Endian> LineNumberProgramHeader<'input, Endian> return Err(parser::Error::MaximumOperationsPerInstructionZero); } - let (rest, default_is_stmt) = try!(parser::parse_u8(rest)); - let (rest, line_base) = try!(parser::parse_i8(rest)); - let (rest, line_range) = try!(parser::parse_u8(rest)); + let (rest, default_is_stmt) = parser::parse_u8(rest)?; + let (rest, line_base) = parser::parse_i8(rest)?; + let (rest, line_range) = parser::parse_u8(rest)?; if line_range == 0 { return Err(parser::Error::LineRangeZero); } - let (rest, opcode_base) = try!(parser::parse_u8(rest)); + let (rest, opcode_base) = parser::parse_u8(rest)?; if opcode_base == 0 { return Err(parser::Error::OpcodeBaseZero); } @@ -1220,7 +1221,7 @@ impl<'input, Endian> LineNumberProgramHeader<'input, Endian> break; } - let (rest1, include_directory) = try!(parser::parse_null_terminated_string(rest.0)); + let (rest1, include_directory) = parser::parse_null_terminated_string(rest.0)?; rest = EndianBuf::new(rest1); include_directories.push(include_directory); } @@ -1233,13 +1234,13 @@ impl<'input, Endian> LineNumberProgramHeader<'input, Endian> if rest[0] == 0 { let comp_name = comp_name.map(|name| { - FileEntry { - path_name: name, - directory_index: 0, - last_modification: 0, - length: 0, - } - }); + FileEntry { + path_name: name, + directory_index: 0, + last_modification: 0, + length: 0, + } + }); let header = LineNumberProgramHeader { unit_length: unit_length, version: version, @@ -1262,7 +1263,7 @@ impl<'input, Endian> LineNumberProgramHeader<'input, Endian> return Ok((next_header_input, header)); } - let (rest1, file_name) = try!(FileEntry::parse(rest.0)); + let (rest1, file_name) = FileEntry::parse(rest.0)?; rest = EndianBuf::new(rest1); file_names.push(file_name); } @@ -1336,12 +1337,12 @@ impl<'input, Endian> IncompleteLineNumberProgram<'input, Endian> // We just finished a sequence. sequences.push(LineNumberSequence { - // In theory one could have multiple DW_LNE_end_sequence opcodes - // in a row. - start: sequence_start_addr.unwrap_or(0), - end: sequence_end_addr, - opcodes: opcodes.remove_trailing(&state_machine.opcodes), - }); + // In theory one could have multiple DW_LNE_end_sequence opcodes + // in a row. + start: sequence_start_addr.unwrap_or(0), + end: sequence_end_addr, + opcodes: opcodes.remove_trailing(&state_machine.opcodes), + }); sequence_start_addr = None; opcodes = state_machine.opcodes.clone(); } @@ -1406,10 +1407,10 @@ pub struct FileEntry<'input> { impl<'input> FileEntry<'input> { fn parse(input: &'input [u8]) -> parser::Result<(&'input [u8], FileEntry<'input>)> { - let (rest, path_name) = try!(parser::parse_null_terminated_string(input)); - let (rest, directory_index) = try!(parser::parse_unsigned_leb(rest)); - let (rest, last_modification) = try!(parser::parse_unsigned_leb(rest)); - let (rest, length) = try!(parser::parse_unsigned_leb(rest)); + let (rest, path_name) = parser::parse_null_terminated_string(input)?; + let (rest, directory_index) = parser::parse_unsigned_leb(rest)?; + let (rest, last_modification) = parser::parse_unsigned_leb(rest)?; + let (rest, length) = parser::parse_unsigned_leb(rest)?; let entry = FileEntry { path_name: path_name, @@ -1894,11 +1895,11 @@ mod tests { test(constants::DW_LNE_define_file, file, Opcode::DefineFile(FileEntry { - path_name: ffi::CStr::from_bytes_with_nul(&path_name).unwrap(), - directory_index: 0, - last_modification: 1, - length: 2, - })); + path_name: ffi::CStr::from_bytes_with_nul(&path_name).unwrap(), + directory_index: 0, + last_modification: 1, + length: 2, + })); // Unknown extended opcode. let operands = [1, 2, 3, 4, 5, 6]; diff --git a/src/loc.rs b/src/loc.rs index fac7cfb07..0ad232d63 100644 --- a/src/loc.rs +++ b/src/loc.rs @@ -130,7 +130,7 @@ impl<'input, Endian> RawLocationListIter<'input, Endian> return Ok(None); } - let (rest, location) = try!(LocationListEntry::parse(self.input, self.address_size)); + let (rest, location) = LocationListEntry::parse(self.input, self.address_size)?; if location.range.is_end() { self.input = EndianBuf::new(&[]); } else { @@ -182,7 +182,7 @@ impl<'input, Endian> LocationListIter<'input, Endian> /// Advance the iterator to the next location. pub fn next(&mut self) -> Result>> { loop { - let mut location = match try!(self.raw.next()) { + let mut location = match self.raw.next()? { Some(location) => location, None => return Ok(None), }; @@ -201,7 +201,9 @@ impl<'input, Endian> LocationListIter<'input, Endian> continue; } - location.range.add_base_address(self.base_address, self.raw.address_size); + location + .range + .add_base_address(self.base_address, self.raw.address_size); if location.range.begin > location.range.end { self.raw.input = EndianBuf::new(&[]); return Err(Error::InvalidLocationAddressRange); @@ -244,7 +246,7 @@ impl<'input, Endian> LocationListEntry<'input, Endian> -> Result<(EndianBuf, LocationListEntry)> where Endian: Endianity { - let (rest, range) = try!(Range::parse(input, address_size)); + let (rest, range) = Range::parse(input, address_size)?; if range.is_end() || range.is_base_address(address_size) { let location = LocationListEntry { range: range, @@ -252,8 +254,8 @@ impl<'input, Endian> LocationListEntry<'input, Endian> }; Ok((rest, location)) } else { - let (rest, len) = try!(parse_u16(rest)); - let (rest, data) = try!(take(len as usize, rest)); + let (rest, len) = parse_u16(rest)?; + let (rest, data) = take(len as usize, rest)?; let location = LocationListEntry { range: range, data: data, @@ -308,58 +310,59 @@ mod tests { // A normal location. assert_eq!(locations.next(), Ok(Some(LocationListEntry { - range: Range { - begin: 0x01010200, - end: 0x01010300, - }, - data: EndianBuf::new(&[2, 0, 0, 0]), - }))); + range: Range { + begin: 0x01010200, + end: 0x01010300, + }, + data: EndianBuf::new(&[2, 0, 0, 0]), + }))); // A base address selection followed by a normal location. assert_eq!(locations.next(), Ok(Some(LocationListEntry { - range: Range { - begin: 0x02010400, - end: 0x02010500, - }, - data: EndianBuf::new(&[3, 0, 0, 0]), - }))); + range: Range { + begin: 0x02010400, + end: 0x02010500, + }, + data: EndianBuf::new(&[3, 0, 0, 0]), + }))); // An empty location range followed by a normal location. assert_eq!(locations.next(), Ok(Some(LocationListEntry { - range: Range { - begin: 0x02010800, - end: 0x02010900, - }, - data: EndianBuf::new(&[5, 0, 0, 0]), - }))); + range: Range { + begin: 0x02010800, + end: 0x02010900, + }, + data: EndianBuf::new(&[5, 0, 0, 0]), + }))); // A location range that starts at 0. assert_eq!(locations.next(), Ok(Some(LocationListEntry { - range: Range { - begin: 0x02000000, - end: 0x02000001, - }, - data: EndianBuf::new(&[6, 0, 0, 0]), - }))); + range: Range { + begin: 0x02000000, + end: 0x02000001, + }, + data: EndianBuf::new(&[6, 0, 0, 0]), + }))); // A location range that ends at -1. assert_eq!(locations.next(), Ok(Some(LocationListEntry { - range: Range { - begin: 0x00000000, - end: 0xffffffff, - }, - data: EndianBuf::new(&[7, 0, 0, 0]), - }))); + range: Range { + begin: 0x00000000, + end: 0xffffffff, + }, + data: EndianBuf::new(&[7, 0, 0, 0]), + }))); // A location list end. assert_eq!(locations.next(), Ok(None)); // An offset at the end of buf. - let mut locations = debug_loc.locations(DebugLocOffset(buf.len()), 4, 0x01000000) + let mut locations = debug_loc + .locations(DebugLocOffset(buf.len()), 4, 0x01000000) .unwrap(); assert_eq!(locations.next(), Ok(None)); } @@ -399,58 +402,59 @@ mod tests { // A normal location. assert_eq!(locations.next(), Ok(Some(LocationListEntry { - range: Range { - begin: 0x01010200, - end: 0x01010300, - }, - data: EndianBuf::new(&[2, 0, 0, 0]), - }))); + range: Range { + begin: 0x01010200, + end: 0x01010300, + }, + data: EndianBuf::new(&[2, 0, 0, 0]), + }))); // A base address selection followed by a normal location. assert_eq!(locations.next(), Ok(Some(LocationListEntry { - range: Range { - begin: 0x02010400, - end: 0x02010500, - }, - data: EndianBuf::new(&[3, 0, 0, 0]), - }))); + range: Range { + begin: 0x02010400, + end: 0x02010500, + }, + data: EndianBuf::new(&[3, 0, 0, 0]), + }))); // An empty location range followed by a normal location. assert_eq!(locations.next(), Ok(Some(LocationListEntry { - range: Range { - begin: 0x02010800, - end: 0x02010900, - }, - data: EndianBuf::new(&[5, 0, 0, 0]), - }))); + range: Range { + begin: 0x02010800, + end: 0x02010900, + }, + data: EndianBuf::new(&[5, 0, 0, 0]), + }))); // A location range that starts at 0. assert_eq!(locations.next(), Ok(Some(LocationListEntry { - range: Range { - begin: 0x02000000, - end: 0x02000001, - }, - data: EndianBuf::new(&[6, 0, 0, 0]), - }))); + range: Range { + begin: 0x02000000, + end: 0x02000001, + }, + data: EndianBuf::new(&[6, 0, 0, 0]), + }))); // A location range that ends at -1. assert_eq!(locations.next(), Ok(Some(LocationListEntry { - range: Range { - begin: 0x0, - end: 0xffffffffffffffff, - }, - data: EndianBuf::new(&[7, 0, 0, 0]), - }))); + range: Range { + begin: 0x0, + end: 0xffffffffffffffff, + }, + data: EndianBuf::new(&[7, 0, 0, 0]), + }))); // A location list end. assert_eq!(locations.next(), Ok(None)); // An offset at the end of buf. - let mut locations = debug_loc.locations(DebugLocOffset(buf.len()), 8, 0x01000000) + let mut locations = debug_loc + .locations(DebugLocOffset(buf.len()), 8, 0x01000000) .unwrap(); assert_eq!(locations.next(), Ok(None)); } @@ -467,11 +471,15 @@ mod tests { let debug_loc = DebugLoc::::new(&buf); // An invalid location range. - let mut locations = debug_loc.locations(DebugLocOffset(0x0), 4, 0x01000000).unwrap(); + let mut locations = debug_loc + .locations(DebugLocOffset(0x0), 4, 0x01000000) + .unwrap(); assert_eq!(locations.next(), Err(Error::InvalidLocationAddressRange)); // An invalid location range after wrapping. - let mut locations = debug_loc.locations(DebugLocOffset(0x8), 4, 0x01000000).unwrap(); + let mut locations = debug_loc + .locations(DebugLocOffset(0x8), 4, 0x01000000) + .unwrap(); assert_eq!(locations.next(), Err(Error::InvalidLocationAddressRange)); // An invalid offset. diff --git a/src/lookup.rs b/src/lookup.rs index fdcb77454..f27d54b70 100755 --- a/src/lookup.rs +++ b/src/lookup.rs @@ -99,7 +99,7 @@ impl<'input, Endian, Parser> LookupEntryIter<'input, Endian, Parser> Ok(None) } else { // Parse the next header. - let (input, set, header) = try!(Parser::parse_header(self.remaining_input)); + let (input, set, header) = Parser::parse_header(self.remaining_input)?; self.remaining_input = input; self.current_set = set; self.current_header = Some(header); @@ -108,7 +108,7 @@ impl<'input, Endian, Parser> LookupEntryIter<'input, Endian, Parser> } } else { let (remaining_set, entry) = - try!(Parser::parse_entry(self.current_set, self.current_header.as_ref().unwrap())); + Parser::parse_entry(self.current_set, self.current_header.as_ref().unwrap())?; self.current_set = remaining_set; match entry { None => self.next(), @@ -175,22 +175,23 @@ impl<'input, Endian, Switch> LookupParser<'input, Endian> for PubStuffParser<'in #[allow(type_complexity)] fn parse_header(input: EndianBuf) -> Result<(EndianBuf, EndianBuf, Rc)> { - let (rest, (set_length, format)) = try!(parse_initial_length(input.into())); - let (rest, version) = try!(parse_u16(rest.into())); + let (rest, (set_length, format)) = parse_initial_length(input.into())?; + let (rest, version) = parse_u16(rest.into())?; if version != 2 { return Err(Error::UnknownVersion); } - let (rest, info_offset) = try!(Switch::parse_offset(rest.into(), format)); - let (rest, info_length) = try!(parse_word(rest.into(), format)); + let (rest, info_offset) = Switch::parse_offset(rest.into(), format)?; + let (rest, info_length) = parse_word(rest.into(), format)?; let header_length = match format { Format::Dwarf32 => 10, Format::Dwarf64 => 18, }; - let dividing_line: usize = try!(set_length.checked_sub(header_length) - .ok_or(Error::BadLength)) as usize; + let dividing_line: usize = set_length + .checked_sub(header_length) + .ok_or(Error::BadLength)? as usize; Ok((rest.range_from(dividing_line..), rest.range_to(..dividing_line), @@ -201,12 +202,12 @@ impl<'input, Endian, Switch> LookupParser<'input, Endian> for PubStuffParser<'in fn parse_entry(input: EndianBuf<'input, Endian>, header: &Rc) -> Result<(EndianBuf<'input, Endian>, Option)> { - let (rest, offset) = try!(parse_word(input.into(), Switch::format_from(header))); + let (rest, offset) = parse_word(input.into(), Switch::format_from(header))?; if offset == 0 { Ok((EndianBuf::new(&[]), None)) } else { - let (rest, name) = try!(parse_null_terminated_string(rest.into())); + let (rest, name) = parse_null_terminated_string(rest.into())?; Ok((EndianBuf::new(rest), Some(Switch::new_entry(offset, name, header)))) } diff --git a/src/op.rs b/src/op.rs index 0e2fb9838..3bfcc7f01 100644 --- a/src/op.rs +++ b/src/op.rs @@ -200,23 +200,12 @@ enum OperationEvaluationResult<'input, Endian> size: u8, space: Option, }, - AwaitingRegister { - register: u64, - offset: u64, - }, - AwaitingFrameBase { - offset: u64, - }, - AwaitingTls { - index: u64, - }, + AwaitingRegister { register: u64, offset: u64 }, + AwaitingFrameBase { offset: u64 }, + AwaitingTls { index: u64 }, AwaitingCfa, - AwaitingAtLocation { - location: DieReference, - }, - AwaitingEntryValue { - expression: EndianBuf<'input, Endian>, - }, + AwaitingAtLocation { location: DieReference }, + AwaitingEntryValue { expression: EndianBuf<'input, Endian>, }, } /// A single location of a piece of the result of a DWARF expression. @@ -303,65 +292,65 @@ impl<'input, Endian> Operation<'input, Endian> -> Result<(EndianBuf<'input, Endian>, Operation<'input, Endian>), Error> where Endian: Endianity { - let (bytes, opcode) = try!(parse_u8e(bytes)); + let (bytes, opcode) = parse_u8e(bytes)?; let name = constants::DwOp(opcode); match name { constants::DW_OP_addr => { - let (newbytes, value) = try!(parse_address(bytes, address_size)); + let (newbytes, value) = parse_address(bytes, address_size)?; Ok((newbytes, Operation::Literal { value: value })) } constants::DW_OP_deref => { Ok((bytes, Operation::Deref { - size: address_size, - space: false, - })) + size: address_size, + space: false, + })) } constants::DW_OP_const1u => { - let (newbytes, value) = try!(parse_u8e(bytes)); + let (newbytes, value) = parse_u8e(bytes)?; Ok((newbytes, Operation::Literal { value: value as u64 })) } constants::DW_OP_const1s => { - let (newbytes, value) = try!(parse_i8e(bytes)); + let (newbytes, value) = parse_i8e(bytes)?; Ok((newbytes, Operation::Literal { value: value as u64 })) } constants::DW_OP_const2u => { - let (newbytes, value) = try!(parse_u16(bytes)); + let (newbytes, value) = parse_u16(bytes)?; Ok((newbytes, Operation::Literal { value: value as u64 })) } constants::DW_OP_const2s => { - let (newbytes, value) = try!(parse_i16(bytes)); + let (newbytes, value) = parse_i16(bytes)?; Ok((newbytes, Operation::Literal { value: value as u64 })) } constants::DW_OP_const4u => { - let (newbytes, value) = try!(parse_u32(bytes)); + let (newbytes, value) = parse_u32(bytes)?; Ok((newbytes, Operation::Literal { value: value as u64 })) } constants::DW_OP_const4s => { - let (newbytes, value) = try!(parse_i32(bytes)); + let (newbytes, value) = parse_i32(bytes)?; Ok((newbytes, Operation::Literal { value: value as u64 })) } constants::DW_OP_const8u => { - let (newbytes, value) = try!(parse_u64(bytes)); + let (newbytes, value) = parse_u64(bytes)?; Ok((newbytes, Operation::Literal { value: value })) } constants::DW_OP_const8s => { - let (newbytes, value) = try!(parse_i64(bytes)); + let (newbytes, value) = parse_i64(bytes)?; Ok((newbytes, Operation::Literal { value: value as u64 })) } constants::DW_OP_constu => { - let (newbytes, value) = try!(parse_unsigned_lebe(bytes)); + let (newbytes, value) = parse_unsigned_lebe(bytes)?; Ok((newbytes, Operation::Literal { value: value })) } constants::DW_OP_consts => { - let (newbytes, value) = try!(parse_signed_lebe(bytes)); + let (newbytes, value) = parse_signed_lebe(bytes)?; Ok((newbytes, Operation::Literal { value: value as u64 })) } constants::DW_OP_dup => Ok((bytes, Operation::Pick { index: 0 })), constants::DW_OP_drop => Ok((bytes, Operation::Drop)), constants::DW_OP_over => Ok((bytes, Operation::Pick { index: 1 })), constants::DW_OP_pick => { - let (newbytes, value) = try!(parse_u8e(bytes)); + let (newbytes, value) = parse_u8e(bytes)?; Ok((newbytes, Operation::Pick { index: value })) } constants::DW_OP_swap => Ok((bytes, Operation::Swap)), @@ -369,9 +358,9 @@ impl<'input, Endian> Operation<'input, Endian> constants::DW_OP_xderef => { Ok((bytes, Operation::Deref { - size: address_size, - space: true, - })) + size: address_size, + space: true, + })) } constants::DW_OP_abs => Ok((bytes, Operation::Abs)), constants::DW_OP_and => Ok((bytes, Operation::And)), @@ -384,7 +373,7 @@ impl<'input, Endian> Operation<'input, Endian> constants::DW_OP_or => Ok((bytes, Operation::Or)), constants::DW_OP_plus => Ok((bytes, Operation::Plus)), constants::DW_OP_plus_uconst => { - let (newbytes, value) = try!(parse_unsigned_lebe(bytes)); + let (newbytes, value) = parse_unsigned_lebe(bytes)?; Ok((newbytes, Operation::PlusConstant { value: value })) } constants::DW_OP_shl => Ok((bytes, Operation::Shl)), @@ -392,9 +381,8 @@ impl<'input, Endian> Operation<'input, Endian> constants::DW_OP_shra => Ok((bytes, Operation::Shra)), constants::DW_OP_xor => Ok((bytes, Operation::Xor)), constants::DW_OP_bra => { - let (newbytes, value) = try!(parse_i16(bytes)); - Ok((newbytes, - Operation::Bra { target: try!(compute_pc(newbytes, bytecode, value)) })) + let (newbytes, value) = parse_i16(bytes)?; + Ok((newbytes, Operation::Bra { target: compute_pc(newbytes, bytecode, value)? })) } constants::DW_OP_eq => Ok((bytes, Operation::Eq)), constants::DW_OP_ge => Ok((bytes, Operation::Ge)), @@ -403,9 +391,8 @@ impl<'input, Endian> Operation<'input, Endian> constants::DW_OP_lt => Ok((bytes, Operation::Lt)), constants::DW_OP_ne => Ok((bytes, Operation::Ne)), constants::DW_OP_skip => { - let (newbytes, value) = try!(parse_i16(bytes)); - Ok((newbytes, - Operation::Skip { target: try!(compute_pc(newbytes, bytecode, value)) })) + let (newbytes, value) = parse_i16(bytes)?; + Ok((newbytes, Operation::Skip { target: compute_pc(newbytes, bytecode, value)? })) } constants::DW_OP_lit0 => Ok((bytes, Operation::Literal { value: 0 })), constants::DW_OP_lit1 => Ok((bytes, Operation::Literal { value: 1 })), @@ -472,316 +459,316 @@ impl<'input, Endian> Operation<'input, Endian> constants::DW_OP_reg30 => Ok((bytes, Operation::Register { register: 30 })), constants::DW_OP_reg31 => Ok((bytes, Operation::Register { register: 31 })), constants::DW_OP_breg0 => { - let (newbytes, value) = try!(parse_signed_lebe(bytes)); + let (newbytes, value) = parse_signed_lebe(bytes)?; Ok((newbytes, Operation::RegisterOffset { - register: 0, - offset: value, - })) + register: 0, + offset: value, + })) } constants::DW_OP_breg1 => { - let (newbytes, value) = try!(parse_signed_lebe(bytes)); + let (newbytes, value) = parse_signed_lebe(bytes)?; Ok((newbytes, Operation::RegisterOffset { - register: 1, - offset: value, - })) + register: 1, + offset: value, + })) } constants::DW_OP_breg2 => { - let (newbytes, value) = try!(parse_signed_lebe(bytes)); + let (newbytes, value) = parse_signed_lebe(bytes)?; Ok((newbytes, Operation::RegisterOffset { - register: 2, - offset: value, - })) + register: 2, + offset: value, + })) } constants::DW_OP_breg3 => { - let (newbytes, value) = try!(parse_signed_lebe(bytes)); + let (newbytes, value) = parse_signed_lebe(bytes)?; Ok((newbytes, Operation::RegisterOffset { - register: 3, - offset: value, - })) + register: 3, + offset: value, + })) } constants::DW_OP_breg4 => { - let (newbytes, value) = try!(parse_signed_lebe(bytes)); + let (newbytes, value) = parse_signed_lebe(bytes)?; Ok((newbytes, Operation::RegisterOffset { - register: 4, - offset: value, - })) + register: 4, + offset: value, + })) } constants::DW_OP_breg5 => { - let (newbytes, value) = try!(parse_signed_lebe(bytes)); + let (newbytes, value) = parse_signed_lebe(bytes)?; Ok((newbytes, Operation::RegisterOffset { - register: 5, - offset: value, - })) + register: 5, + offset: value, + })) } constants::DW_OP_breg6 => { - let (newbytes, value) = try!(parse_signed_lebe(bytes)); + let (newbytes, value) = parse_signed_lebe(bytes)?; Ok((newbytes, Operation::RegisterOffset { - register: 6, - offset: value, - })) + register: 6, + offset: value, + })) } constants::DW_OP_breg7 => { - let (newbytes, value) = try!(parse_signed_lebe(bytes)); + let (newbytes, value) = parse_signed_lebe(bytes)?; Ok((newbytes, Operation::RegisterOffset { - register: 7, - offset: value, - })) + register: 7, + offset: value, + })) } constants::DW_OP_breg8 => { - let (newbytes, value) = try!(parse_signed_lebe(bytes)); + let (newbytes, value) = parse_signed_lebe(bytes)?; Ok((newbytes, Operation::RegisterOffset { - register: 8, - offset: value, - })) + register: 8, + offset: value, + })) } constants::DW_OP_breg9 => { - let (newbytes, value) = try!(parse_signed_lebe(bytes)); + let (newbytes, value) = parse_signed_lebe(bytes)?; Ok((newbytes, Operation::RegisterOffset { - register: 9, - offset: value, - })) + register: 9, + offset: value, + })) } constants::DW_OP_breg10 => { - let (newbytes, value) = try!(parse_signed_lebe(bytes)); + let (newbytes, value) = parse_signed_lebe(bytes)?; Ok((newbytes, Operation::RegisterOffset { - register: 10, - offset: value, - })) + register: 10, + offset: value, + })) } constants::DW_OP_breg11 => { - let (newbytes, value) = try!(parse_signed_lebe(bytes)); + let (newbytes, value) = parse_signed_lebe(bytes)?; Ok((newbytes, Operation::RegisterOffset { - register: 11, - offset: value, - })) + register: 11, + offset: value, + })) } constants::DW_OP_breg12 => { - let (newbytes, value) = try!(parse_signed_lebe(bytes)); + let (newbytes, value) = parse_signed_lebe(bytes)?; Ok((newbytes, Operation::RegisterOffset { - register: 12, - offset: value, - })) + register: 12, + offset: value, + })) } constants::DW_OP_breg13 => { - let (newbytes, value) = try!(parse_signed_lebe(bytes)); + let (newbytes, value) = parse_signed_lebe(bytes)?; Ok((newbytes, Operation::RegisterOffset { - register: 13, - offset: value, - })) + register: 13, + offset: value, + })) } constants::DW_OP_breg14 => { - let (newbytes, value) = try!(parse_signed_lebe(bytes)); + let (newbytes, value) = parse_signed_lebe(bytes)?; Ok((newbytes, Operation::RegisterOffset { - register: 14, - offset: value, - })) + register: 14, + offset: value, + })) } constants::DW_OP_breg15 => { - let (newbytes, value) = try!(parse_signed_lebe(bytes)); + let (newbytes, value) = parse_signed_lebe(bytes)?; Ok((newbytes, Operation::RegisterOffset { - register: 15, - offset: value, - })) + register: 15, + offset: value, + })) } constants::DW_OP_breg16 => { - let (newbytes, value) = try!(parse_signed_lebe(bytes)); + let (newbytes, value) = parse_signed_lebe(bytes)?; Ok((newbytes, Operation::RegisterOffset { - register: 16, - offset: value, - })) + register: 16, + offset: value, + })) } constants::DW_OP_breg17 => { - let (newbytes, value) = try!(parse_signed_lebe(bytes)); + let (newbytes, value) = parse_signed_lebe(bytes)?; Ok((newbytes, Operation::RegisterOffset { - register: 17, - offset: value, - })) + register: 17, + offset: value, + })) } constants::DW_OP_breg18 => { - let (newbytes, value) = try!(parse_signed_lebe(bytes)); + let (newbytes, value) = parse_signed_lebe(bytes)?; Ok((newbytes, Operation::RegisterOffset { - register: 18, - offset: value, - })) + register: 18, + offset: value, + })) } constants::DW_OP_breg19 => { - let (newbytes, value) = try!(parse_signed_lebe(bytes)); + let (newbytes, value) = parse_signed_lebe(bytes)?; Ok((newbytes, Operation::RegisterOffset { - register: 19, - offset: value, - })) + register: 19, + offset: value, + })) } constants::DW_OP_breg20 => { - let (newbytes, value) = try!(parse_signed_lebe(bytes)); + let (newbytes, value) = parse_signed_lebe(bytes)?; Ok((newbytes, Operation::RegisterOffset { - register: 20, - offset: value, - })) + register: 20, + offset: value, + })) } constants::DW_OP_breg21 => { - let (newbytes, value) = try!(parse_signed_lebe(bytes)); + let (newbytes, value) = parse_signed_lebe(bytes)?; Ok((newbytes, Operation::RegisterOffset { - register: 21, - offset: value, - })) + register: 21, + offset: value, + })) } constants::DW_OP_breg22 => { - let (newbytes, value) = try!(parse_signed_lebe(bytes)); + let (newbytes, value) = parse_signed_lebe(bytes)?; Ok((newbytes, Operation::RegisterOffset { - register: 22, - offset: value, - })) + register: 22, + offset: value, + })) } constants::DW_OP_breg23 => { - let (newbytes, value) = try!(parse_signed_lebe(bytes)); + let (newbytes, value) = parse_signed_lebe(bytes)?; Ok((newbytes, Operation::RegisterOffset { - register: 23, - offset: value, - })) + register: 23, + offset: value, + })) } constants::DW_OP_breg24 => { - let (newbytes, value) = try!(parse_signed_lebe(bytes)); + let (newbytes, value) = parse_signed_lebe(bytes)?; Ok((newbytes, Operation::RegisterOffset { - register: 24, - offset: value, - })) + register: 24, + offset: value, + })) } constants::DW_OP_breg25 => { - let (newbytes, value) = try!(parse_signed_lebe(bytes)); + let (newbytes, value) = parse_signed_lebe(bytes)?; Ok((newbytes, Operation::RegisterOffset { - register: 25, - offset: value, - })) + register: 25, + offset: value, + })) } constants::DW_OP_breg26 => { - let (newbytes, value) = try!(parse_signed_lebe(bytes)); + let (newbytes, value) = parse_signed_lebe(bytes)?; Ok((newbytes, Operation::RegisterOffset { - register: 26, - offset: value, - })) + register: 26, + offset: value, + })) } constants::DW_OP_breg27 => { - let (newbytes, value) = try!(parse_signed_lebe(bytes)); + let (newbytes, value) = parse_signed_lebe(bytes)?; Ok((newbytes, Operation::RegisterOffset { - register: 27, - offset: value, - })) + register: 27, + offset: value, + })) } constants::DW_OP_breg28 => { - let (newbytes, value) = try!(parse_signed_lebe(bytes)); + let (newbytes, value) = parse_signed_lebe(bytes)?; Ok((newbytes, Operation::RegisterOffset { - register: 28, - offset: value, - })) + register: 28, + offset: value, + })) } constants::DW_OP_breg29 => { - let (newbytes, value) = try!(parse_signed_lebe(bytes)); + let (newbytes, value) = parse_signed_lebe(bytes)?; Ok((newbytes, Operation::RegisterOffset { - register: 29, - offset: value, - })) + register: 29, + offset: value, + })) } constants::DW_OP_breg30 => { - let (newbytes, value) = try!(parse_signed_lebe(bytes)); + let (newbytes, value) = parse_signed_lebe(bytes)?; Ok((newbytes, Operation::RegisterOffset { - register: 30, - offset: value, - })) + register: 30, + offset: value, + })) } constants::DW_OP_breg31 => { - let (newbytes, value) = try!(parse_signed_lebe(bytes)); + let (newbytes, value) = parse_signed_lebe(bytes)?; Ok((newbytes, Operation::RegisterOffset { - register: 31, - offset: value, - })) + register: 31, + offset: value, + })) } constants::DW_OP_regx => { - let (newbytes, value) = try!(parse_unsigned_lebe(bytes)); + let (newbytes, value) = parse_unsigned_lebe(bytes)?; Ok((newbytes, Operation::Register { register: value })) } constants::DW_OP_fbreg => { - let (newbytes, value) = try!(parse_signed_lebe(bytes)); + let (newbytes, value) = parse_signed_lebe(bytes)?; Ok((newbytes, Operation::FrameOffset { offset: value })) } constants::DW_OP_bregx => { - let (newbytes, regno) = try!(parse_unsigned_lebe(bytes)); - let (newbytes, offset) = try!(parse_signed_lebe(newbytes)); + let (newbytes, regno) = parse_unsigned_lebe(bytes)?; + let (newbytes, offset) = parse_signed_lebe(newbytes)?; Ok((newbytes, Operation::RegisterOffset { - register: regno, - offset: offset, - })) + register: regno, + offset: offset, + })) } constants::DW_OP_piece => { - let (newbytes, size) = try!(parse_unsigned_lebe(bytes)); + let (newbytes, size) = parse_unsigned_lebe(bytes)?; Ok((newbytes, Operation::Piece { - size_in_bits: 8 * size, - bit_offset: None, - })) + size_in_bits: 8 * size, + bit_offset: None, + })) } constants::DW_OP_deref_size => { - let (newbytes, size) = try!(parse_u8e(bytes)); + let (newbytes, size) = parse_u8e(bytes)?; Ok((newbytes, Operation::Deref { - size: size, - space: false, - })) + size: size, + space: false, + })) } constants::DW_OP_xderef_size => { - let (newbytes, size) = try!(parse_u8e(bytes)); + let (newbytes, size) = parse_u8e(bytes)?; Ok((newbytes, Operation::Deref { - size: size, - space: true, - })) + size: size, + space: true, + })) } constants::DW_OP_nop => Ok((bytes, Operation::Nop)), constants::DW_OP_push_object_address => Ok((bytes, Operation::PushObjectAddress)), constants::DW_OP_call2 => { - let (newbytes, value) = try!(parse_u16(bytes)); + let (newbytes, value) = parse_u16(bytes)?; Ok((newbytes, Operation::Call { offset: DieReference::UnitRef(UnitOffset(value as usize)) })) } constants::DW_OP_call4 => { - let (newbytes, value) = try!(parse_u32(bytes)); + let (newbytes, value) = parse_u32(bytes)?; Ok((newbytes, Operation::Call { offset: DieReference::UnitRef(UnitOffset(value as usize)) })) } constants::DW_OP_call_ref => { - let (newbytes, value) = try!(parse_offset(bytes, format)); + let (newbytes, value) = parse_offset(bytes, format)?; Ok((newbytes, Operation::Call { offset: DieReference::DebugInfoRef(DebugInfoOffset(value)) })) } @@ -789,32 +776,32 @@ impl<'input, Endian> Operation<'input, Endian> constants::DW_OP_GNU_push_tls_address => Ok((bytes, Operation::TLS)), constants::DW_OP_call_frame_cfa => Ok((bytes, Operation::CallFrameCFA)), constants::DW_OP_bit_piece => { - let (newbytes, size) = try!(parse_unsigned_lebe(bytes)); - let (newbytes, offset) = try!(parse_unsigned_lebe(newbytes)); + let (newbytes, size) = parse_unsigned_lebe(bytes)?; + let (newbytes, offset) = parse_unsigned_lebe(newbytes)?; Ok((newbytes, Operation::Piece { - size_in_bits: size, - bit_offset: Some(offset), - })) + size_in_bits: size, + bit_offset: Some(offset), + })) } constants::DW_OP_implicit_value => { - let (newbytes, data) = try!(parse_length_uleb_value(bytes)); + let (newbytes, data) = parse_length_uleb_value(bytes)?; Ok((newbytes, Operation::ImplicitValue { data: data.into() })) } constants::DW_OP_stack_value => Ok((bytes, Operation::StackValue)), constants::DW_OP_implicit_pointer | constants::DW_OP_GNU_implicit_pointer => { - let (newbytes, value) = try!(parse_offset(bytes, format)); - let (newbytes, byte_offset) = try!(parse_signed_lebe(newbytes)); + let (newbytes, value) = parse_offset(bytes, format)?; + let (newbytes, byte_offset) = parse_signed_lebe(newbytes)?; Ok((newbytes, Operation::ImplicitPointer { - value: DebugInfoOffset(value), - byte_offset: byte_offset, - })) + value: DebugInfoOffset(value), + byte_offset: byte_offset, + })) } constants::DW_OP_entry_value | constants::DW_OP_GNU_entry_value => { - let (newbytes, expression) = try!(parse_length_uleb_value(bytes)); + let (newbytes, expression) = parse_length_uleb_value(bytes)?; Ok((newbytes, Operation::EntryValue { expression: expression })) } @@ -1009,8 +996,10 @@ impl<'input, Endian> Evaluation<'input, Endian> match self.state { EvaluationState::Start(None) => { self.state = EvaluationState::Start(Some(value)); - }, - _ => panic!("`Evaluation::set_initial_value` was called twice, or after evaluation began."), + } + _ => { + panic!("`Evaluation::set_initial_value` was called twice, or after evaluation began.") + } }; } @@ -1069,17 +1058,17 @@ impl<'input, Endian> Evaluation<'input, Endian> match *operation { Operation::Deref { size, space } => { - let addr = try!(self.pop()); - let addr_space = if space { Some(try!(self.pop())) } else { None }; + let addr = self.pop()?; + let addr_space = if space { Some(self.pop()?) } else { None }; return Ok(OperationEvaluationResult::AwaitingMemory { - address: addr, - size: size, - space: addr_space, - }); + address: addr, + size: size, + space: addr_space, + }); } Operation::Drop => { - try!(self.pop()); + self.pop()?; } Operation::Pick { index } => { let len = self.stack.len(); @@ -1091,80 +1080,80 @@ impl<'input, Endian> Evaluation<'input, Endian> self.push(value); } Operation::Swap => { - let top = try!(self.pop()); - let next = try!(self.pop()); + let top = self.pop()?; + let next = self.pop()?; self.push(top); self.push(next); } Operation::Rot => { - let one = try!(self.pop()); - let two = try!(self.pop()); - let three = try!(self.pop()); + let one = self.pop()?; + let two = self.pop()?; + let three = self.pop()?; self.push(one); self.push(three); self.push(two); } Operation::Abs => { - let value = try!(self.pop_signed()); + let value = self.pop_signed()?; self.push(value.abs() as u64); } Operation::And => { - let v1 = try!(self.pop()); - let v2 = try!(self.pop()); + let v1 = self.pop()?; + let v2 = self.pop()?; self.push(v2 & v1); } Operation::Div => { - let v1 = try!(self.pop_signed()); - let v2 = try!(self.pop_signed()); + let v1 = self.pop_signed()?; + let v2 = self.pop_signed()?; if v1 == 0 { return Err(Error::DivisionByZero.into()); } self.push(v2.wrapping_div(v1) as u64); } Operation::Minus => { - let v1 = try!(self.pop()); - let v2 = try!(self.pop()); + let v1 = self.pop()?; + let v2 = self.pop()?; self.push(v2.wrapping_sub(v1)); } Operation::Mod => { - let v1 = try!(self.pop()); - let v2 = try!(self.pop()); + let v1 = self.pop()?; + let v2 = self.pop()?; if v1 == 0 { return Err(Error::DivisionByZero.into()); } self.push(v2.wrapping_rem(v1)); } Operation::Mul => { - let v1 = try!(self.pop()); - let v2 = try!(self.pop()); + let v1 = self.pop()?; + let v2 = self.pop()?; self.push(v2.wrapping_mul(v1)); } Operation::Neg => { - let v = try!(self.pop()); + let v = self.pop()?; self.push(v.wrapping_neg()); } Operation::Not => { - let value = try!(self.pop()); + let value = self.pop()?; self.push(!value); } Operation::Or => { - let v1 = try!(self.pop()); - let v2 = try!(self.pop()); + let v1 = self.pop()?; + let v2 = self.pop()?; self.push(v2 | v1); } Operation::Plus => { - let v1 = try!(self.pop()); - let v2 = try!(self.pop()); + let v1 = self.pop()?; + let v2 = self.pop()?; self.push(v2.wrapping_add(v1)); } Operation::PlusConstant { value } => { - let v = try!(self.pop()); + let v = self.pop()?; self.push(v.wrapping_add(value)); } Operation::Shl => { - let v1 = try!(self.pop()); - let v2 = try!(self.pop()); + let v1 = self.pop()?; + let v2 = self.pop()?; // Because wrapping_shl takes a u32, not a u64, we do // the check by hand. if v1 >= 64 { @@ -1174,8 +1163,8 @@ impl<'input, Endian> Evaluation<'input, Endian> } } Operation::Shr => { - let v1 = try!(self.pop()); - let v2 = try!(self.pop()); + let v1 = self.pop()?; + let v2 = self.pop()?; // Because wrapping_shr takes a u32, not a u64, we do // the check by hand. if v1 >= 64 { @@ -1185,8 +1174,8 @@ impl<'input, Endian> Evaluation<'input, Endian> } } Operation::Shra => { - let v1 = try!(self.pop()); - let v2 = try!(self.pop_signed()); + let v1 = self.pop()?; + let v2 = self.pop_signed()?; // Because wrapping_shr takes a u32, not a u64, we do // the check by hand. if v1 >= 64 { @@ -1200,46 +1189,46 @@ impl<'input, Endian> Evaluation<'input, Endian> } } Operation::Xor => { - let v1 = try!(self.pop()); - let v2 = try!(self.pop()); + let v1 = self.pop()?; + let v2 = self.pop()?; self.push(v2 ^ v1); } Operation::Bra { target } => { - let v = try!(self.pop()); + let v = self.pop()?; if v != 0 { self.pc = target; } } Operation::Eq => { - let v1 = try!(self.pop_signed()); - let v2 = try!(self.pop_signed()); + let v1 = self.pop_signed()?; + let v2 = self.pop_signed()?; self.push(if v2 == v1 { 1 } else { 0 }); } Operation::Ge => { - let v1 = try!(self.pop_signed()); - let v2 = try!(self.pop_signed()); + let v1 = self.pop_signed()?; + let v2 = self.pop_signed()?; self.push(if v2 >= v1 { 1 } else { 0 }); } Operation::Gt => { - let v1 = try!(self.pop_signed()); - let v2 = try!(self.pop_signed()); + let v1 = self.pop_signed()?; + let v2 = self.pop_signed()?; self.push(if v2 > v1 { 1 } else { 0 }); } Operation::Le => { - let v1 = try!(self.pop_signed()); - let v2 = try!(self.pop_signed()); + let v1 = self.pop_signed()?; + let v2 = self.pop_signed()?; self.push(if v2 <= v1 { 1 } else { 0 }); } Operation::Lt => { - let v1 = try!(self.pop_signed()); - let v2 = try!(self.pop_signed()); + let v1 = self.pop_signed()?; + let v2 = self.pop_signed()?; self.push(if v2 < v1 { 1 } else { 0 }); } Operation::Ne => { - let v1 = try!(self.pop_signed()); - let v2 = try!(self.pop_signed()); + let v1 = self.pop_signed()?; + let v2 = self.pop_signed()?; self.push(if v2 != v1 { 1 } else { 0 }); } @@ -1253,15 +1242,13 @@ impl<'input, Endian> Evaluation<'input, Endian> Operation::RegisterOffset { register, offset } => { return Ok(OperationEvaluationResult::AwaitingRegister { - register: register, - offset: offset as u64, - }); + register: register, + offset: offset as u64, + }); } Operation::FrameOffset { offset } => { - return Ok(OperationEvaluationResult::AwaitingFrameBase { - offset: offset as u64, - }); + return Ok(OperationEvaluationResult::AwaitingFrameBase { offset: offset as u64 }); } Operation::Nop => {} @@ -1275,16 +1262,12 @@ impl<'input, Endian> Evaluation<'input, Endian> } Operation::Call { offset } => { - return Ok(OperationEvaluationResult::AwaitingAtLocation { - location: offset, - }); + return Ok(OperationEvaluationResult::AwaitingAtLocation { location: offset }); } Operation::TLS => { - let value = try!(self.pop()); - return Ok(OperationEvaluationResult::AwaitingTls { - index: value, - }); + let value = self.pop()?; + return Ok(OperationEvaluationResult::AwaitingTls { index: value }); } Operation::CallFrameCFA => { @@ -1303,7 +1286,7 @@ impl<'input, Endian> Evaluation<'input, Endian> Operation::StackValue => { terminated = true; - current_location = Location::Scalar { value: try!(self.pop()) }; + current_location = Location::Scalar { value: self.pop()? }; } Operation::ImplicitPointer { value, byte_offset } => { @@ -1316,8 +1299,8 @@ impl<'input, Endian> Evaluation<'input, Endian> Operation::EntryValue { expression } => { return Ok(OperationEvaluationResult::AwaitingEntryValue { - expression: expression.into(), - }); + expression: expression.into(), + }); } Operation::Piece { .. } => { @@ -1326,10 +1309,10 @@ impl<'input, Endian> Evaluation<'input, Endian> } Ok(OperationEvaluationResult::Complete { - terminated: terminated, - piece_end: piece_end, - current_location: current_location, - }) + terminated: terminated, + piece_end: piece_end, + current_location: current_location, + }) } /// Get the result of this `Evaluation`. @@ -1339,7 +1322,9 @@ impl<'input, Endian> Evaluation<'input, Endian> pub fn result(self) -> Vec> { match self.state { EvaluationState::Complete => self.result, - _ => panic!("Called `Evaluation::result` on an `Evaluation` that has not been completed"), + _ => { + panic!("Called `Evaluation::result` on an `Evaluation` that has not been completed") + } } } @@ -1349,7 +1334,7 @@ impl<'input, Endian> Evaluation<'input, Endian> /// value and resume the evaluation by calling the appropriate resume_with /// method on `Evaluation`. pub fn evaluate(&mut self) -> Result, Error> - where Endian: Endianity, + where Endian: Endianity { match self.state { EvaluationState::Start(initial_value) => { @@ -1357,8 +1342,8 @@ impl<'input, Endian> Evaluation<'input, Endian> self.push(value); } self.state = EvaluationState::Ready; - }, - EvaluationState::Ready => {}, + } + EvaluationState::Ready => {} EvaluationState::Error(err) => return Err(err), EvaluationState::Complete => return Ok(EvaluationResult::Complete), EvaluationState::Waiting(_) => panic!(), @@ -1369,7 +1354,7 @@ impl<'input, Endian> Evaluation<'input, Endian> Err(e) => { self.state = EvaluationState::Error(e); Err(e) - }, + } } } @@ -1380,15 +1365,19 @@ impl<'input, Endian> Evaluation<'input, Endian> /// /// # Panics /// Panics if this `Evaluation` did not previously stop with `EvaluationResult::RequiresMemory`. - pub fn resume_with_memory(&mut self, value: u64) -> Result, Error> + pub fn resume_with_memory(&mut self, + value: u64) + -> Result, Error> where Endian: Endianity { match self.state { EvaluationState::Error(err) => return Err(err), EvaluationState::Waiting(OperationEvaluationResult::AwaitingMemory { .. }) => { self.push(value); - }, - _ => panic!("Called `Evaluation::resume_with_memory` without a preceding `EvaluationResult::RequiresMemory`"), + } + _ => { + panic!("Called `Evaluation::resume_with_memory` without a preceding `EvaluationResult::RequiresMemory`") + } }; self.evaluate_internal() @@ -1401,15 +1390,21 @@ impl<'input, Endian> Evaluation<'input, Endian> /// /// # Panics /// Panics if this `Evaluation` did not previously stop with `EvaluationResult::RequiresRegister`. - pub fn resume_with_register(&mut self, register: u64) -> Result, Error> + pub fn resume_with_register(&mut self, + register: u64) + -> Result, Error> where Endian: Endianity { match self.state { EvaluationState::Error(err) => return Err(err), - EvaluationState::Waiting(OperationEvaluationResult::AwaitingRegister { offset, .. }) => { + EvaluationState::Waiting(OperationEvaluationResult::AwaitingRegister { + offset, .. + }) => { self.push(register.wrapping_add(offset)); - }, - _ => panic!("Called `Evaluation::resume_with_register` without a preceding `EvaluationResult::RequiresRegister`"), + } + _ => { + panic!("Called `Evaluation::resume_with_register` without a preceding `EvaluationResult::RequiresRegister`") + } }; self.evaluate_internal() @@ -1422,15 +1417,19 @@ impl<'input, Endian> Evaluation<'input, Endian> /// /// # Panics /// Panics if this `Evaluation` did not previously stop with `EvaluationResult::RequiresFrameBase`. - pub fn resume_with_frame_base(&mut self, frame_base: u64) -> Result, Error> + pub fn resume_with_frame_base(&mut self, + frame_base: u64) + -> Result, Error> where Endian: Endianity { match self.state { EvaluationState::Error(err) => return Err(err), EvaluationState::Waiting(OperationEvaluationResult::AwaitingFrameBase { offset }) => { self.push(frame_base.wrapping_add(offset)); - }, - _ => panic!("Called `Evaluation::resume_with_frame_base` without a preceding `EvaluationResult::RequiresFrameBase`"), + } + _ => { + panic!("Called `Evaluation::resume_with_frame_base` without a preceding `EvaluationResult::RequiresFrameBase`") + } }; self.evaluate_internal() @@ -1450,8 +1449,10 @@ impl<'input, Endian> Evaluation<'input, Endian> EvaluationState::Error(err) => return Err(err), EvaluationState::Waiting(OperationEvaluationResult::AwaitingTls { .. }) => { self.push(value); - }, - _ => panic!("Called `Evaluation::resume_with_tls` without a preceding `EvaluationResult::RequiresTls`"), + } + _ => { + panic!("Called `Evaluation::resume_with_tls` without a preceding `EvaluationResult::RequiresTls`") + } }; self.evaluate_internal() @@ -1464,15 +1465,19 @@ impl<'input, Endian> Evaluation<'input, Endian> /// /// # Panics /// Panics if this `Evaluation` did not previously stop with `EvaluationResult::RequiresCallFrameCfa`. - pub fn resume_with_call_frame_cfa(&mut self, cfa: u64) -> Result, Error> + pub fn resume_with_call_frame_cfa(&mut self, + cfa: u64) + -> Result, Error> where Endian: Endianity { match self.state { EvaluationState::Error(err) => return Err(err), EvaluationState::Waiting(OperationEvaluationResult::AwaitingCfa) => { self.push(cfa); - }, - _ => panic!("Called `Evaluation::resume_with_call_frame_cfa` without a preceding `EvaluationResult::RequiresCallFrameCfa`"), + } + _ => { + panic!("Called `Evaluation::resume_with_call_frame_cfa` without a preceding `EvaluationResult::RequiresCallFrameCfa`") + } }; self.evaluate_internal() @@ -1485,7 +1490,9 @@ impl<'input, Endian> Evaluation<'input, Endian> /// /// # Panics /// Panics if this `Evaluation` did not previously stop with `EvaluationResult::RequiresAtLocation`. - pub fn resume_with_at_location(&mut self, bytes: EndianBuf<'input, Endian>) -> Result, Error> + pub fn resume_with_at_location(&mut self, + bytes: EndianBuf<'input, Endian>) + -> Result, Error> where Endian: Endianity { match self.state { @@ -1496,8 +1503,10 @@ impl<'input, Endian> Evaluation<'input, Endian> self.pc = bytes; self.bytecode = bytes; } - }, - _ => panic!("Called `Evaluation::resume_with_at_location` without a precedeing `EvaluationResult::RequiresAtLocation`"), + } + _ => { + panic!("Called `Evaluation::resume_with_at_location` without a precedeing `EvaluationResult::RequiresAtLocation`") + } }; self.evaluate_internal() @@ -1510,15 +1519,19 @@ impl<'input, Endian> Evaluation<'input, Endian> /// /// # Panics /// Panics if this `Evaluation` did not previously stop with `EvaluationResult::RequiresEntryValue`. - pub fn resume_with_entry_value(&mut self, entry_value: u64) -> Result, Error> + pub fn resume_with_entry_value(&mut self, + entry_value: u64) + -> Result, Error> where Endian: Endianity { match self.state { EvaluationState::Error(err) => return Err(err), EvaluationState::Waiting(OperationEvaluationResult::AwaitingEntryValue { .. }) => { self.push(entry_value); - }, - _ => panic!("Called `Evaluation::resume_with_entry_value` without a preceding `EvaluationResult::RequiresEntryValue`"), + } + _ => { + panic!("Called `Evaluation::resume_with_entry_value` without a preceding `EvaluationResult::RequiresEntryValue`") + } }; self.evaluate_internal() @@ -1546,12 +1559,16 @@ impl<'input, Endian> Evaluation<'input, Endian> } let (newpc, operation) = - try!(Operation::parse(self.pc, self.bytecode, self.address_size, self.format)); + Operation::parse(self.pc, self.bytecode, self.address_size, self.format)?; self.pc = newpc; - let op_result = try!(self.evaluate_one_operation(&operation)); + let op_result = self.evaluate_one_operation(&operation)?; match op_result { - OperationEvaluationResult::Complete { terminated, piece_end, mut current_location } => { + OperationEvaluationResult::Complete { + terminated, + piece_end, + mut current_location, + } => { if piece_end || terminated { // If we saw a piece end, like Piece, then we want to use // the operation we already decoded to see what to do. @@ -1565,13 +1582,13 @@ impl<'input, Endian> Evaluation<'input, Endian> // result is the address on the stack. assert_eq!(current_location, Location::Empty); if !self.stack.is_empty() { - current_location = Location::Address { address: try!(self.pop()) }; + current_location = Location::Address { address: self.pop()? }; } } else if !eof { - let (newpc, operation) = try!(Operation::parse(self.pc, - self.bytecode, - self.address_size, - self.format)); + let (newpc, operation) = Operation::parse(self.pc, + self.bytecode, + self.address_size, + self.format)?; self.pc = newpc; pieceop = operation; } @@ -1584,18 +1601,21 @@ impl<'input, Endian> Evaluation<'input, Endian> return Err(Error::InvalidPiece.into()); } self.result.push(Piece { - size_in_bits: None, - bit_offset: None, - location: current_location, - }); + size_in_bits: None, + bit_offset: None, + location: current_location, + }); } - Operation::Piece { size_in_bits, bit_offset } => { + Operation::Piece { + size_in_bits, + bit_offset, + } => { self.result.push(Piece { - size_in_bits: Some(size_in_bits), - bit_offset: bit_offset, - location: current_location, - }); + size_in_bits: Some(size_in_bits), + bit_offset: bit_offset, + location: current_location, + }); } _ => { @@ -1604,51 +1624,55 @@ impl<'input, Endian> Evaluation<'input, Endian> } } } - }, - OperationEvaluationResult::AwaitingMemory { address, size, space } => { + } + OperationEvaluationResult::AwaitingMemory { + address, + size, + space, + } => { self.state = EvaluationState::Waiting(op_result); return Ok(EvaluationResult::RequiresMemory { - address: address, - size: size, - space: space, - }); - }, + address: address, + size: size, + space: space, + }); + } OperationEvaluationResult::AwaitingRegister { register, .. } => { self.state = EvaluationState::Waiting(op_result); return Ok(EvaluationResult::RequiresRegister(register)); - }, + } OperationEvaluationResult::AwaitingFrameBase { .. } => { self.state = EvaluationState::Waiting(op_result); - return Ok(EvaluationResult::RequiresFrameBase); - }, + return Ok(EvaluationResult::RequiresFrameBase); + } OperationEvaluationResult::AwaitingTls { index } => { self.state = EvaluationState::Waiting(op_result); return Ok(EvaluationResult::RequiresTls(index)); - }, + } OperationEvaluationResult::AwaitingCfa => { self.state = EvaluationState::Waiting(op_result); return Ok(EvaluationResult::RequiresCallFrameCfa); - }, + } OperationEvaluationResult::AwaitingAtLocation { location } => { self.state = EvaluationState::Waiting(op_result); return Ok(EvaluationResult::RequiresAtLocation(location)); - }, + } OperationEvaluationResult::AwaitingEntryValue { expression } => { self.state = EvaluationState::Waiting(op_result); return Ok(EvaluationResult::RequiresEntryValue(expression)); - }, + } }; } // If no pieces have been seen, use the stack top as the // result. if self.result.is_empty() { - let addr = try!(self.pop()); + let addr = self.pop()?; self.result.push(Piece { - size_in_bits: None, - bit_offset: None, - location: Location::Address { address: addr }, - }); + size_in_bits: None, + bit_offset: None, + location: Location::Address { address: addr }, + }); } self.state = EvaluationState::Complete; @@ -1692,10 +1716,7 @@ mod tests { address_size: u8, format: Format) { let buf = EndianBuf::::new(input); - let value = Operation::parse(buf, - buf, - address_size, - format); + let value = Operation::parse(buf, buf, address_size, format); match value { Ok((pc, val)) => { assert_eq!(val, *expect); @@ -1707,10 +1728,7 @@ mod tests { fn check_op_parse_failure(input: &[u8], expect: Error, address_size: u8, format: Format) { let buf = EndianBuf::::new(input); - match Operation::parse(buf, - buf, - address_size, - format) { + match Operation::parse(buf, buf, address_size, format) { Err(x) => { assert_eq!(x, expect); } @@ -1725,7 +1743,9 @@ mod tests { format: Format) where F: Fn(Section) -> Section { - let input = input(Section::with_endian(Endian::Little)).get_contents().unwrap(); + let input = input(Section::with_endian(Endian::Little)) + .get_contents() + .unwrap(); for i in 1..input.len() { check_op_parse_failure(&input[..i], Error::UnexpectedEof, address_size, format); } @@ -1741,9 +1761,9 @@ mod tests { // Test all single-byte opcodes. let inputs = [(constants::DW_OP_deref, Operation::Deref { - size: address_size, - space: false, - }), + size: address_size, + space: false, + }), (constants::DW_OP_dup, Operation::Pick { index: 0 }), (constants::DW_OP_drop, Operation::Drop), (constants::DW_OP_over, Operation::Pick { index: 1 }), @@ -1751,9 +1771,9 @@ mod tests { (constants::DW_OP_rot, Operation::Rot), (constants::DW_OP_xderef, Operation::Deref { - size: address_size, - space: true, - }), + size: address_size, + space: true, + }), (constants::DW_OP_abs, Operation::Abs), (constants::DW_OP_and, Operation::And), (constants::DW_OP_div, Operation::Div), @@ -1868,15 +1888,15 @@ mod tests { (constants::DW_OP_deref_size, 19, Operation::Deref { - size: 19, - space: false, - }), + size: 19, + space: false, + }), (constants::DW_OP_xderef_size, 19, Operation::Deref { - size: 19, - space: true, - })]; + size: 19, + space: true, + })]; for item in inputs.iter() { let (opcode, arg, ref result) = *item; @@ -1998,8 +2018,8 @@ mod tests { (constants::DW_OP_call_ref, 0x1234567812345678, Operation::Call { - offset: DieReference::DebugInfoRef(DebugInfoOffset(0x1234567812345678)), - })]; + offset: DieReference::DebugInfoRef(DebugInfoOffset(0x1234567812345678)), + })]; for item in inputs.iter() { let (op, arg, ref expect) = *item; @@ -2023,17 +2043,16 @@ mod tests { -0x1eeeeeee, -0x7fffffffffffffff]; for value in values.iter() { - let mut inputs = vec!( - (constants::DW_OP_consts.0, Operation::Literal { value: *value as u64}), - (constants::DW_OP_fbreg.0, Operation::FrameOffset { offset: *value }), - ); + let mut inputs = + vec![(constants::DW_OP_consts.0, Operation::Literal { value: *value as u64 }), + (constants::DW_OP_fbreg.0, Operation::FrameOffset { offset: *value })]; for i in 0..32 { inputs.push((constants::DW_OP_breg0.0 + i, Operation::RegisterOffset { - register: i as u64, - offset: *value, - })); + register: i as u64, + offset: *value, + })); } for item in inputs.iter() { @@ -2051,19 +2070,18 @@ mod tests { let values = [0, 1, 0x100, 0x1eeeeeee, 0x7fffffffffffffff, !0u64]; for value in values.iter() { - let mut inputs = vec!( - (constants::DW_OP_constu, Operation::Literal { value: *value}), - (constants::DW_OP_plus_uconst, Operation::PlusConstant { value: *value }), - (constants::DW_OP_regx, Operation::Register { register: *value }), - ); + let mut inputs = + vec![(constants::DW_OP_constu, Operation::Literal { value: *value }), + (constants::DW_OP_plus_uconst, Operation::PlusConstant { value: *value }), + (constants::DW_OP_regx, Operation::Register { register: *value })]; // FIXME if *value < !0u64 / 8 { inputs.push((constants::DW_OP_piece, Operation::Piece { - size_in_bits: 8 * value, - bit_offset: None, - })); + size_in_bits: 8 * value, + bit_offset: None, + })); } for item in inputs.iter() { @@ -2155,7 +2173,8 @@ mod tests { #[test] fn test_op_parse_implicit_pointer() { - for op in &[constants::DW_OP_implicit_pointer, constants::DW_OP_GNU_implicit_pointer] { + for op in &[constants::DW_OP_implicit_pointer, + constants::DW_OP_GNU_implicit_pointer] { check_op_parse(|s| s.D8(op.0).D32(0x12345678).sleb(0x123), &Operation::ImplicitPointer { value: DebugInfoOffset(0x12345678), @@ -2176,7 +2195,8 @@ mod tests { #[test] fn test_op_parse_entry_value() { - for op in &[constants::DW_OP_entry_value, constants::DW_OP_GNU_entry_value] { + for op in &[constants::DW_OP_entry_value, + constants::DW_OP_GNU_entry_value] { let data = b"hello"; check_op_parse(|s| s.D8(op.0).uleb(data.len() as u64).append_bytes(&data[..]), &Operation::EntryValue { expression: EndianBuf::new(&data[..]) }, @@ -2267,16 +2287,14 @@ mod tests { initial_value: Option, max_iterations: Option, f: F) - where for <'a> F: Fn(&mut Evaluation<'a, LittleEndian>, - EvaluationResult<'a, LittleEndian>) - -> Result> + where for<'a> F: Fn(&mut Evaluation<'a, LittleEndian>, + EvaluationResult<'a, LittleEndian>) + -> Result> { let bytes = assemble(program); let bytes = EndianBuf::::new(&bytes); - let mut eval = Evaluation::::new(bytes, - address_size, - format); + let mut eval = Evaluation::::new(bytes, address_size, format); if let Some(val) = object_address { eval.set_object_address(val); @@ -2313,7 +2331,14 @@ mod tests { address_size: u8, format: Format) { - check_eval_with_args(program, expect, address_size, format, None, None, None, |_, result| Ok(result)); + check_eval_with_args(program, + expect, + address_size, + format, + None, + None, + None, + |_, result| Ok(result)); } #[test] diff --git a/src/parser.rs b/src/parser.rs index a409b6565..bfe06910e 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -361,7 +361,7 @@ pub fn parse_i64(input: EndianBuf) -> Result<(EndianBuf, pub fn parse_u8e(bytes: EndianBuf) -> Result<(EndianBuf, u8)> where Endian: Endianity { - let (bytes, value) = try!(parse_u8(bytes.into())); + let (bytes, value) = parse_u8(bytes.into())?; Ok((EndianBuf::new(bytes), value)) } @@ -371,7 +371,7 @@ pub fn parse_u8e(bytes: EndianBuf) -> Result<(EndianBuf, pub fn parse_i8e(bytes: EndianBuf) -> Result<(EndianBuf, i8)> where Endian: Endianity { - let (bytes, value) = try!(parse_i8(bytes.into())); + let (bytes, value) = parse_i8(bytes.into())?; Ok((EndianBuf::new(bytes), value)) } @@ -381,7 +381,7 @@ pub fn parse_i8e(bytes: EndianBuf) -> Result<(EndianBuf, pub fn parse_unsigned_lebe(bytes: EndianBuf) -> Result<(EndianBuf, u64)> where Endian: Endianity { - let (bytes, value) = try!(parse_unsigned_leb(bytes.into())); + let (bytes, value) = parse_unsigned_leb(bytes.into())?; Ok((EndianBuf::new(bytes), value)) } @@ -392,8 +392,8 @@ pub fn parse_unsigned_leb_as_u8e(bytes: EndianBuf) -> Result<(EndianBuf, u8)> where Endian: Endianity { - let (bytes, value) = try!(parse_unsigned_leb(bytes.into())); - let value = try!(u64_to_u8(value)); + let (bytes, value) = parse_unsigned_leb(bytes.into())?; + let value = u64_to_u8(value)?; Ok((EndianBuf::new(bytes), value)) } @@ -403,7 +403,7 @@ pub fn parse_unsigned_leb_as_u8e(bytes: EndianBuf) pub fn parse_signed_lebe(bytes: EndianBuf) -> Result<(EndianBuf, i64)> where Endian: Endianity { - let (bytes, value) = try!(parse_signed_leb(bytes.into())); + let (bytes, value) = parse_signed_leb(bytes.into())?; Ok((EndianBuf::new(bytes), value)) } @@ -450,8 +450,8 @@ pub fn u64_to_u8(x: u64) -> Result { pub fn parse_u64_as_offset(input: EndianBuf) -> Result<(EndianBuf, usize)> where Endian: Endianity { - let (rest, offset) = try!(parse_u64(input)); - let offset = try!(u64_to_offset(offset)); + let (rest, offset) = parse_u64(input)?; + let offset = u64_to_offset(offset)?; Ok((rest, offset)) } @@ -461,8 +461,8 @@ pub fn parse_u64_as_offset(input: EndianBuf) -> Result<(EndianBu pub fn parse_uleb_as_offset(input: EndianBuf) -> Result<(EndianBuf, usize)> where Endian: Endianity { - let (rest, offset) = try!(parse_unsigned_lebe(input)); - let offset = try!(u64_to_offset(offset)); + let (rest, offset) = parse_unsigned_lebe(input)?; + let offset = u64_to_offset(offset)?; Ok((rest, offset)) } @@ -488,8 +488,8 @@ pub fn parse_offset(input: EndianBuf, -> Result<(EndianBuf, usize)> where Endian: Endianity { - let (rest, offset) = try!(parse_word(input, format)); - let offset = try!(u64_to_offset(offset)); + let (rest, offset) = parse_word(input, format)?; + let offset = u64_to_offset(offset)?; Ok((rest, offset)) } @@ -523,8 +523,8 @@ pub fn parse_address_as_offset(input: EndianBuf, -> Result<(EndianBuf, usize)> where Endian: Endianity { - let (rest, offset) = try!(parse_address(input, address_size)); - let offset = try!(u64_to_offset(offset)); + let (rest, offset) = parse_address(input, address_size)?; + let offset = u64_to_offset(offset)?; Ok((rest, offset)) } @@ -554,7 +554,7 @@ pub fn parse_pointer_encoding(input: EndianBuf) -> Result<(EndianBuf, constants::DwEhPe)> where Endian: Endianity { - let (rest, eh_pe) = try!(parse_u8e(input)); + let (rest, eh_pe) = parse_u8e(input)?; let eh_pe = constants::DwEhPe(eh_pe); if eh_pe.is_valid_encoding() { @@ -588,7 +588,8 @@ impl Default for Pointer { impl Into for Pointer { fn into(self) -> u64 { match self { - Pointer::Direct(p) | Pointer::Indirect(p) => p, + Pointer::Direct(p) | + Pointer::Indirect(p) => p, } } } @@ -625,23 +626,23 @@ pub fn parse_encoded_pointer<'bases, 'input, Endian> match encoding.format() { // Unsigned variants. constants::DW_EH_PE_absptr => { - let (rest, a) = try!(parse_address(input, address_size)); + let (rest, a) = parse_address(input, address_size)?; Ok((rest, a)) } constants::DW_EH_PE_uleb128 => { - let (rest, a) = try!(parse_unsigned_lebe(input)); + let (rest, a) = parse_unsigned_lebe(input)?; Ok((rest, a)) } constants::DW_EH_PE_udata2 => { - let (rest, a) = try!(parse_u16(input)); + let (rest, a) = parse_u16(input)?; Ok((rest, a as u64)) } constants::DW_EH_PE_udata4 => { - let (rest, a) = try!(parse_u32(input)); + let (rest, a) = parse_u32(input)?; Ok((rest, a as u64)) } constants::DW_EH_PE_udata8 => { - let (rest, a) = try!(parse_u64(input)); + let (rest, a) = parse_u64(input)?; Ok((rest, a)) } @@ -650,19 +651,19 @@ pub fn parse_encoded_pointer<'bases, 'input, Endian> // in Rust), return them as u64, and rely on wrapping addition to do // the right thing when adding these offsets to their bases. constants::DW_EH_PE_sleb128 => { - let (rest, a) = try!(parse_signed_lebe(input)); + let (rest, a) = parse_signed_lebe(input)?; Ok((rest, a as u64)) } constants::DW_EH_PE_sdata2 => { - let (rest, a) = try!(parse_i16(input)); + let (rest, a) = parse_i16(input)?; Ok((rest, a as u64)) } constants::DW_EH_PE_sdata4 => { - let (rest, a) = try!(parse_i32(input)); + let (rest, a) = parse_i32(input)?; Ok((rest, a as u64)) } constants::DW_EH_PE_sdata8 => { - let (rest, a) = try!(parse_i64(input)); + let (rest, a) = parse_i64(input)?; Ok((rest, a as u64)) } @@ -681,14 +682,15 @@ pub fn parse_encoded_pointer<'bases, 'input, Endian> match encoding.application() { constants::DW_EH_PE_absptr => { - let (rest, addr) = try!(parse_data(encoding, address_size, input)); + let (rest, addr) = parse_data(encoding, address_size, input)?; Ok((rest, Pointer::new(encoding, addr.into()))) } constants::DW_EH_PE_pcrel => { if let Some(cfi) = bases.cfi { - let (rest, offset) = try!(parse_data(encoding, address_size, input)); + let (rest, offset) = parse_data(encoding, address_size, input)?; let offset_from_section = input.as_ptr() as usize - section.as_ptr() as usize; - let p = cfi.wrapping_add(offset_from_section as u64).wrapping_add(offset); + let p = cfi.wrapping_add(offset_from_section as u64) + .wrapping_add(offset); Ok((rest, Pointer::new(encoding, p))) } else { Err(Error::CfiRelativePointerButCfiBaseIsUndefined) @@ -696,7 +698,7 @@ pub fn parse_encoded_pointer<'bases, 'input, Endian> } constants::DW_EH_PE_textrel => { if let Some(text) = bases.text { - let (rest, offset) = try!(parse_data(encoding, address_size, input)); + let (rest, offset) = parse_data(encoding, address_size, input)?; Ok((rest, Pointer::new(encoding, text.wrapping_add(offset)))) } else { Err(Error::TextRelativePointerButTextBaseIsUndefined) @@ -704,7 +706,7 @@ pub fn parse_encoded_pointer<'bases, 'input, Endian> } constants::DW_EH_PE_datarel => { if let Some(data) = bases.data { - let (rest, offset) = try!(parse_data(encoding, address_size, input)); + let (rest, offset) = parse_data(encoding, address_size, input)?; Ok((rest, Pointer::new(encoding, data.wrapping_add(offset)))) } else { Err(Error::DataRelativePointerButDataBaseIsUndefined) @@ -713,7 +715,7 @@ pub fn parse_encoded_pointer<'bases, 'input, Endian> constants::DW_EH_PE_funcrel => { let func = bases.func.borrow(); if let Some(func) = *func { - let (rest, offset) = try!(parse_data(encoding, address_size, input)); + let (rest, offset) = parse_data(encoding, address_size, input)?; Ok((rest, Pointer::new(encoding, func.wrapping_add(offset)))) } else { Err(Error::FuncRelativePointerInBadContext) @@ -771,11 +773,11 @@ pub fn parse_initial_length(input: EndianBuf) -> Result<(EndianBuf, (u64, Format))> where Endian: Endianity { - let (rest, val) = try!(parse_u32_as_u64(input)); + let (rest, val) = parse_u32_as_u64(input)?; if val < MAX_DWARF_32_UNIT_LENGTH { Ok((rest, (val, Format::Dwarf32))) } else if val == DWARF_64_INITIAL_UNIT_LENGTH { - let (rest, val) = try!(parse_u64(rest)); + let (rest, val) = parse_u64(rest)?; Ok((rest, (val, Format::Dwarf64))) } else { Err(Error::UnknownReservedLength) @@ -811,7 +813,7 @@ pub fn parse_length_uleb_value(input: EndianBuf) -> Result<(EndianBuf, EndianBuf)> where Endian: Endianity { - let (rest, len) = try!(parse_unsigned_leb(input.into())); + let (rest, len) = parse_unsigned_leb(input.into())?; take(len as usize, EndianBuf::new(rest)) } diff --git a/src/pubnames.rs b/src/pubnames.rs index 8aa6365bc..d4950d59c 100755 --- a/src/pubnames.rs +++ b/src/pubnames.rs @@ -58,12 +58,12 @@ impl<'input, Endian> NamesOrTypesSwitch<'input, Endian> for NamesSwitch<'input, length: u64) -> Rc { Rc::new(PubNamesHeader { - format: format, - length: set_length, - version: version, - info_offset: offset, - info_length: length, - }) + format: format, + length: set_length, + version: version, + info_offset: offset, + info_length: length, + }) } fn new_entry(offset: u64, diff --git a/src/pubtypes.rs b/src/pubtypes.rs index 452498d7c..f81c7fa4d 100755 --- a/src/pubtypes.rs +++ b/src/pubtypes.rs @@ -57,12 +57,12 @@ impl<'input, Endian> NamesOrTypesSwitch<'input, Endian> for TypesSwitch<'input, length: u64) -> Rc { Rc::new(PubTypesHeader { - format: format, - length: set_length, - version: version, - types_offset: offset, - types_length: length, - }) + format: format, + length: set_length, + version: version, + types_offset: offset, + types_length: length, + }) } fn new_entry(offset: u64, diff --git a/src/ranges.rs b/src/ranges.rs index bd5735dcf..297e690fc 100644 --- a/src/ranges.rs +++ b/src/ranges.rs @@ -126,7 +126,7 @@ impl<'input, Endian> RawRangesIter<'input, Endian> return Ok(None); } - let (rest, range) = try!(Range::parse(self.input, self.address_size)); + let (rest, range) = Range::parse(self.input, self.address_size)?; if range.is_end() { self.input = EndianBuf::new(&[]); } else { @@ -178,7 +178,7 @@ impl<'input, Endian> RangesIter<'input, Endian> /// Advance the iterator to the next range. pub fn next(&mut self) -> Result> { loop { - let mut range = match try!(self.raw.next()) { + let mut range = match self.raw.next()? { Some(range) => range, None => return Ok(None), }; @@ -264,8 +264,8 @@ impl Range { -> Result<(EndianBuf, Range)> where Endian: Endianity { - let (rest, begin) = try!(parse_address(input, address_size)); - let (rest, end) = try!(parse_address(rest, address_size)); + let (rest, begin) = parse_address(input, address_size)?; + let (rest, end) = parse_address(rest, address_size)?; let range = Range { begin: begin, end: end, @@ -350,43 +350,44 @@ mod tests { // A normal range. assert_eq!(ranges.next(), Ok(Some(Range { - begin: 0x01010200, - end: 0x01010300, - }))); + begin: 0x01010200, + end: 0x01010300, + }))); // A base address selection followed by a normal range. assert_eq!(ranges.next(), Ok(Some(Range { - begin: 0x02010400, - end: 0x02010500, - }))); + begin: 0x02010400, + end: 0x02010500, + }))); // An empty range followed by a normal range. assert_eq!(ranges.next(), Ok(Some(Range { - begin: 0x02010800, - end: 0x02010900, - }))); + begin: 0x02010800, + end: 0x02010900, + }))); // A range that starts at 0. assert_eq!(ranges.next(), Ok(Some(Range { - begin: 0x02000000, - end: 0x02000001, - }))); + begin: 0x02000000, + end: 0x02000001, + }))); // A range that ends at -1. assert_eq!(ranges.next(), Ok(Some(Range { - begin: 0x00000000, - end: 0xffffffff, - }))); + begin: 0x00000000, + end: 0xffffffff, + }))); // A range end. assert_eq!(ranges.next(), Ok(None)); // An offset at the end of buf. - let mut ranges = debug_ranges.ranges(DebugRangesOffset(buf.len()), 4, 0x01000000) + let mut ranges = debug_ranges + .ranges(DebugRangesOffset(buf.len()), 4, 0x01000000) .unwrap(); assert_eq!(ranges.next(), Ok(None)); } @@ -426,43 +427,44 @@ mod tests { // A normal range. assert_eq!(ranges.next(), Ok(Some(Range { - begin: 0x01010200, - end: 0x01010300, - }))); + begin: 0x01010200, + end: 0x01010300, + }))); // A base address selection followed by a normal range. assert_eq!(ranges.next(), Ok(Some(Range { - begin: 0x02010400, - end: 0x02010500, - }))); + begin: 0x02010400, + end: 0x02010500, + }))); // An empty range followed by a normal range. assert_eq!(ranges.next(), Ok(Some(Range { - begin: 0x02010800, - end: 0x02010900, - }))); + begin: 0x02010800, + end: 0x02010900, + }))); // A range that starts at 0. assert_eq!(ranges.next(), Ok(Some(Range { - begin: 0x02000000, - end: 0x02000001, - }))); + begin: 0x02000000, + end: 0x02000001, + }))); // A range that ends at -1. assert_eq!(ranges.next(), Ok(Some(Range { - begin: 0x0, - end: 0xffffffffffffffff, - }))); + begin: 0x0, + end: 0xffffffffffffffff, + }))); // A range end. assert_eq!(ranges.next(), Ok(None)); // An offset at the end of buf. - let mut ranges = debug_ranges.ranges(DebugRangesOffset(buf.len()), 8, 0x01000000) + let mut ranges = debug_ranges + .ranges(DebugRangesOffset(buf.len()), 8, 0x01000000) .unwrap(); assert_eq!(ranges.next(), Ok(None)); } @@ -479,11 +481,15 @@ mod tests { let debug_ranges = DebugRanges::::new(&buf); // An invalid range. - let mut ranges = debug_ranges.ranges(DebugRangesOffset(0x0), 4, 0x01000000).unwrap(); + let mut ranges = debug_ranges + .ranges(DebugRangesOffset(0x0), 4, 0x01000000) + .unwrap(); assert_eq!(ranges.next(), Err(Error::InvalidAddressRange)); // An invalid range after wrapping. - let mut ranges = debug_ranges.ranges(DebugRangesOffset(0x8), 4, 0x01000000).unwrap(); + let mut ranges = debug_ranges + .ranges(DebugRangesOffset(0x8), 4, 0x01000000) + .unwrap(); assert_eq!(ranges.next(), Err(Error::InvalidAddressRange)); // An invalid offset. diff --git a/src/unit.rs b/src/unit.rs index 7b9e87071..903c52878 100644 --- a/src/unit.rs +++ b/src/unit.rs @@ -402,12 +402,12 @@ impl<'input, Endian> CompilationUnitHeader<'input, Endian> fn parse(input: EndianBuf, offset: DebugInfoOffset) -> Result<(EndianBuf, CompilationUnitHeader)> { - let (after_unit, header) = try!(parse_unit_header(input)); + let (after_unit, header) = parse_unit_header(input)?; Ok((after_unit, CompilationUnitHeader { - header: header, - offset: offset, - })) + header: header, + offset: offset, + })) } } @@ -415,7 +415,7 @@ impl<'input, Endian> CompilationUnitHeader<'input, Endian> fn parse_version(input: EndianBuf) -> Result<(EndianBuf, u16)> where Endian: Endianity { - let (rest, val) = try!(parse_u16(input)); + let (rest, val) = parse_u16(input)?; // DWARF 1 was very different, and is obsolete, so isn't supported by this // reader. @@ -621,12 +621,12 @@ impl<'input, Endian> UnitHeader<'input, Endian> } let input = self.range_from(offset..); Ok(EntriesCursor { - unit: self, - input: input, - abbreviations: abbreviations, - cached_current: None, - delta_depth: 0, - }) + unit: self, + input: input, + abbreviations: abbreviations, + cached_current: None, + delta_depth: 0, + }) } /// Navigate this unit's `DebuggingInformationEntry`s as a tree @@ -636,10 +636,10 @@ impl<'input, Endian> UnitHeader<'input, Endian> offset: Option) -> Result> { let mut cursor = match offset { - Some(offset) => try!(self.entries_at_offset(abbreviations, offset)), + Some(offset) => self.entries_at_offset(abbreviations, offset)?, None => self.entries(abbreviations), }; - if try!(cursor.next_entry()).is_none() { + if cursor.next_entry()?.is_none() { return Err(Error::UnexpectedEof); } if cursor.current().is_none() { @@ -659,16 +659,16 @@ fn parse_unit_header(input: EndianBuf) -> Result<(EndianBuf, UnitHeader)> where Endian: Endianity { - let (rest, (unit_length, format)) = try!(parse_initial_length(input)); + let (rest, (unit_length, format)) = parse_initial_length(input)?; if unit_length as usize > rest.len() { return Err(Error::UnexpectedEof); } let after_unit = rest.range_from(unit_length as usize..); let rest = rest.range_to(..unit_length as usize); - let (rest, version) = try!(parse_version(rest)); - let (rest, offset) = try!(parse_debug_abbrev_offset(rest, format)); - let (rest, address_size) = try!(parse_address_size(rest.into())); + let (rest, version) = parse_version(rest)?; + let (rest, offset) = parse_debug_abbrev_offset(rest, format)?; + let (rest, address_size) = parse_address_size(rest.into())?; Ok((after_unit, UnitHeader::new(unit_length, @@ -869,7 +869,7 @@ impl<'input, 'abbrev, 'unit, Endian> DebuggingInformationEntry<'input, 'abbrev, /// and return it. Returns `Ok(None)` if no attribute is found. pub fn attr(&self, name: constants::DwAt) -> Result>> { let mut attrs = self.attrs(); - while let Some(attr) = try!(attrs.next()) { + while let Some(attr) = attrs.next()? { if attr.name() == name { return Ok(Some(attr)); } @@ -882,7 +882,8 @@ impl<'input, 'abbrev, 'unit, Endian> DebuggingInformationEntry<'input, 'abbrev, pub fn attr_value_raw(&self, name: constants::DwAt) -> Result>> { - self.attr(name).map(|attr| attr.map(|attr| attr.raw_value())) + self.attr(name) + .map(|attr| attr.map(|attr| attr.raw_value())) } /// Find the first attribute in this entry which has the given name, @@ -1469,29 +1470,25 @@ impl<'input, Endian> Attribute<'input, Endian> /// Try to convert this attribute's value to an unsigned integer. pub fn udata_value(&self) -> Option { Some(match self.value { - AttributeValue::Data1(ref data) => data[0] as u64, - AttributeValue::Data2(ref data) => Endian::read_u16(data) as u64, - AttributeValue::Data4(ref data) => Endian::read_u32(data) as u64, - AttributeValue::Data8(ref data) => Endian::read_u64(data), - AttributeValue::Udata(data) => data, - _ => return None, - }) + AttributeValue::Data1(ref data) => data[0] as u64, + AttributeValue::Data2(ref data) => Endian::read_u16(data) as u64, + AttributeValue::Data4(ref data) => Endian::read_u32(data) as u64, + AttributeValue::Data8(ref data) => Endian::read_u64(data), + AttributeValue::Udata(data) => data, + _ => return None, + }) } /// Try to convert this attribute's value to a signed integer. pub fn sdata_value(&self) -> Option { Some(match self.value { - AttributeValue::Data1(ref data) => data[0] as i8 as i64, - AttributeValue::Data2(ref data) => { - Endian::read_u16(data) as i16 as i64 - } - AttributeValue::Data4(ref data) => { - Endian::read_u32(data) as i32 as i64 - } - AttributeValue::Data8(ref data) => Endian::read_u64(data) as i64, - AttributeValue::Sdata(data) => data, - _ => return None, - }) + AttributeValue::Data1(ref data) => data[0] as i8 as i64, + AttributeValue::Data2(ref data) => Endian::read_u16(data) as i16 as i64, + AttributeValue::Data4(ref data) => Endian::read_u32(data) as i32 as i64, + AttributeValue::Data8(ref data) => Endian::read_u64(data) as i64, + AttributeValue::Sdata(data) => data, + _ => return None, + }) } /// Try to convert this attribute's value to an offset. @@ -1519,10 +1516,10 @@ impl<'input, Endian> Attribute<'input, Endian> /// it is encountered in practice. fn exprloc_value(&self) -> Option> { Some(match self.value { - AttributeValue::Block(data) | - AttributeValue::Exprloc(data) => data, - _ => return None, - }) + AttributeValue::Block(data) | + AttributeValue::Exprloc(data) => data, + _ => return None, + }) } /// Try to return this attribute's value as a string reference. @@ -1544,7 +1541,7 @@ fn length_u8_value(input: EndianBuf) -> Result<(EndianBuf, EndianBuf)> where Endian: Endianity { - let (rest, len) = try!(parse_u8(input.into())); + let (rest, len) = parse_u8(input.into())?; take(len as usize, EndianBuf::new(rest)) } @@ -1552,7 +1549,7 @@ fn length_u16_value(input: EndianBuf) -> Result<(EndianBuf, EndianBuf)> where Endian: Endianity { - let (rest, len) = try!(parse_u16(input)); + let (rest, len) = parse_u16(input)?; take(len as usize, rest) } @@ -1560,7 +1557,7 @@ fn length_u32_value(input: EndianBuf) -> Result<(EndianBuf, EndianBuf)> where Endian: Endianity { - let (rest, len) = try!(parse_u32(input)); + let (rest, len) = parse_u32(input)?; take(len as usize, rest) } @@ -1779,7 +1776,7 @@ impl<'input, 'abbrev, 'entry, 'unit, Endian> AttrsIter<'input, 'abbrev, 'entry, let attr = self.attributes[0]; let rest_attr = &self.attributes[1..]; - let (rest, attr) = try!(parse_attribute(EndianBuf::new(self.input), self.entry.unit, attr)); + let (rest, attr) = parse_attribute(EndianBuf::new(self.input), self.entry.unit, attr)?; self.attributes = rest_attr; self.input = rest.into(); Ok(Some(attr)) @@ -1842,11 +1839,11 @@ impl<'input, 'abbrev, 'unit, Endian> EntriesCursor<'input, 'abbrev, 'unit, Endia Ok(after_attrs) } else { let mut attrs = current.attrs(); - while let Some(_) = try!(attrs.next()) { - } - Ok(current.after_attrs - .get() - .expect("should have after_attrs after iterating attrs")) + while let Some(_) = attrs.next()? {} + Ok(current + .after_attrs + .get() + .expect("should have after_attrs after iterating attrs")) } } else { Ok(self.input) @@ -1866,7 +1863,7 @@ impl<'input, 'abbrev, 'unit, Endian> EntriesCursor<'input, 'abbrev, 'unit, Endia /// Returns `Some` if there is a next entry, even if this entry is null. /// If there is no next entry, then `None` is returned. pub fn next_entry(&mut self) -> Result> { - let input = try!(self.after_entry()); + let input = self.after_entry()?; if input.is_empty() { self.input = input; self.cached_current = None; @@ -1875,7 +1872,7 @@ impl<'input, 'abbrev, 'unit, Endian> EntriesCursor<'input, 'abbrev, 'unit, Endia } let offset = self.get_offset(input); - match try!(parse_unsigned_leb(input)) { + match parse_unsigned_leb(input)? { (rest, 0) => { self.input = rest; self.cached_current = None; @@ -1885,12 +1882,12 @@ impl<'input, 'abbrev, 'unit, Endian> EntriesCursor<'input, 'abbrev, 'unit, Endia (rest, code) => { if let Some(abbrev) = self.abbreviations.get(code) { self.cached_current = Some(DebuggingInformationEntry { - offset: offset, - attrs_slice: rest, - after_attrs: Cell::new(None), - abbrev: abbrev, - unit: self.unit, - }); + offset: offset, + attrs_slice: rest, + after_attrs: Cell::new(None), + abbrev: abbrev, + unit: self.unit, + }); self.delta_depth = abbrev.has_children() as isize; Ok(Some(())) @@ -2027,7 +2024,7 @@ impl<'input, 'abbrev, 'unit, Endian> EntriesCursor<'input, 'abbrev, 'unit, Endia // entries, but this while loop is slightly more efficient. // Note that this doesn't handle unusual LEB128 encodings of zero // such as [0x80, 0x00]; they are still handled by next_entry(). - let mut input = try!(self.after_entry()); + let mut input = self.after_entry()?; while !input.is_empty() && input[0] == 0 { delta_depth -= 1; input = &input[1..]; @@ -2036,7 +2033,7 @@ impl<'input, 'abbrev, 'unit, Endian> EntriesCursor<'input, 'abbrev, 'unit, Endia self.cached_current = None; // The next entry should be the one we want. - if try!(self.next_entry()).is_some() { + if self.next_entry()?.is_some() { if let Some(ref entry) = self.cached_current { return Ok(Some((delta_depth, entry))); } @@ -2167,13 +2164,16 @@ impl<'input, 'abbrev, 'unit, Endian> EntriesCursor<'input, 'abbrev, 'unit, Endia // Loop until we find an entry at the current level. let mut depth = 0; loop { - if self.current().map(|entry| entry.has_children()).unwrap_or(false) { + if self.current() + .map(|entry| entry.has_children()) + .unwrap_or(false) { // This entry has children, so the next entry is // down one level. depth += 1; - let sibling_ptr = - try!(self.current().unwrap().attr_value(constants::DW_AT_sibling)); + let sibling_ptr = self.current() + .unwrap() + .attr_value(constants::DW_AT_sibling)?; if let Some(AttributeValue::UnitRef(offset)) = sibling_ptr { if self.unit.is_valid_offset(offset) { // Fast path: this entry has a DW_AT_sibling @@ -2186,7 +2186,7 @@ impl<'input, 'abbrev, 'unit, Endian> EntriesCursor<'input, 'abbrev, 'unit, Endia } } - if try!(self.next_entry()).is_none() { + if self.next_entry()?.is_none() { // End of input. return Ok(None); } @@ -2285,12 +2285,15 @@ impl<'input, 'abbrev, 'unit, Endian> EntriesTree<'input, 'abbrev, 'unit, Endian> fn next(&mut self, depth: isize) -> Result { if self.depth < depth { debug_assert_eq!(self.depth + 1, depth); - if !self.cursor.current().map(|entry| entry.has_children()).unwrap_or(false) { + if !self.cursor + .current() + .map(|entry| entry.has_children()) + .unwrap_or(false) { // Never any children. return Ok(false); } // The next entry is the child. - try!(self.cursor.next_entry()); + self.cursor.next_entry()?; if self.cursor.current().is_none() { // No children, don't adjust depth. return Ok(false); @@ -2303,9 +2306,9 @@ impl<'input, 'abbrev, 'unit, Endian> EntriesTree<'input, 'abbrev, 'unit, Endian> loop { if self.cursor.current().is_some() { - try!(self.cursor.next_sibling()); + self.cursor.next_sibling()?; } else { - try!(self.cursor.next_entry()); + self.cursor.next_entry()?; } if self.depth == depth { if self.cursor.current().is_none() { @@ -2386,7 +2389,7 @@ impl<'input, 'abbrev, 'unit, 'tree, Endian> EntriesTreeIter<'input, 'abbrev, 'un -> Result>> { if self.state == EntriesTreeIterState::None { Ok(None) - } else if try!(self.tree.next(self.depth)) { + } else if self.tree.next(self.depth)? { self.state = EntriesTreeIterState::Child; Ok(Some(EntriesTreeIter::new(self.tree, self.depth + 1))) } else { @@ -2402,7 +2405,7 @@ fn parse_type_signature(input: EndianBuf) -> Result<(EndianBuf, DebugTypeSignature)> where Endian: Endianity { - let (rest, offset) = try!(parse_u64(input)); + let (rest, offset) = parse_u64(input)?; Ok((rest, DebugTypeSignature(offset))) } @@ -2728,9 +2731,9 @@ fn parse_type_unit_header(input: EndianBuf, -> Result<(EndianBuf, TypeUnitHeader)> where Endian: Endianity { - let (after_unit, mut header) = try!(parse_unit_header(input)); - let (rest, signature) = try!(parse_type_signature(header.entries_buf)); - let (rest, type_offset) = try!(parse_type_offset(rest, header.format())); + let (after_unit, mut header) = parse_unit_header(input)?; + let (rest, signature) = parse_type_signature(header.entries_buf)?; + let (rest, type_offset) = parse_type_offset(rest, header.format())?; header.entries_buf = rest; Ok((after_unit, TypeUnitHeader::new(header, offset, signature, type_offset))) } @@ -2801,13 +2804,11 @@ mod tests { let section = match unit.format { Format::Dwarf32 => self.L32(&length), - Format::Dwarf64 => { - self.L32(0xffffffff) - .L64(&length) - } + Format::Dwarf64 => self.L32(0xffffffff).L64(&length), }; - let section = section.mark(&start) + let section = section + .mark(&start) .L16(unit.version) .offset(unit.debug_abbrev_offset.0, unit.format) .D8(unit.address_size) @@ -3166,7 +3167,9 @@ mod tests { fn section_contents(f: F) -> Vec where F: Fn(Section) -> Section { - f(Section::with_endian(Endian::Little)).get_contents().unwrap() + f(Section::with_endian(Endian::Little)) + .get_contents() + .unwrap() } #[test] @@ -3228,16 +3231,17 @@ mod tests { #[test] fn test_attribute_udata_sdata_value() { - let tests: &[(AttributeValue, _, _)] = &[(AttributeValue::Data1([1]), Some(1), Some(1)), - (AttributeValue::Data1([255]), Some(std::u8::MAX as u64), Some(-1)), - (AttributeValue::Data2([1, 0]), Some(1), Some(1)), - (AttributeValue::Data2([255; 2]), Some(std::u16::MAX as u64), Some(-1)), - (AttributeValue::Data4([1, 0, 0, 0]), Some(1), Some(1)), - (AttributeValue::Data4([255; 4]), Some(std::u32::MAX as u64), Some(-1)), - (AttributeValue::Data8([1, 0, 0, 0, 0, 0, 0, 0]), Some(1), Some(1)), - (AttributeValue::Data8([255; 8]), Some(std::u64::MAX), Some(-1)), - (AttributeValue::Sdata(1), None, Some(1)), - (AttributeValue::Udata(1), Some(1), None)]; + let tests: &[(AttributeValue, _, _)] = + &[(AttributeValue::Data1([1]), Some(1), Some(1)), + (AttributeValue::Data1([255]), Some(std::u8::MAX as u64), Some(-1)), + (AttributeValue::Data2([1, 0]), Some(1), Some(1)), + (AttributeValue::Data2([255; 2]), Some(std::u16::MAX as u64), Some(-1)), + (AttributeValue::Data4([1, 0, 0, 0]), Some(1), Some(1)), + (AttributeValue::Data4([255; 4]), Some(std::u32::MAX as u64), Some(-1)), + (AttributeValue::Data8([1, 0, 0, 0, 0, 0, 0, 0]), Some(1), Some(1)), + (AttributeValue::Data8([255; 8]), Some(std::u64::MAX), Some(-1)), + (AttributeValue::Sdata(1), None, Some(1)), + (AttributeValue::Udata(1), Some(1), None)]; for test in tests.iter() { let (value, expect_udata, expect_sdata) = *test; let attribute = Attribute { @@ -3628,14 +3632,16 @@ mod tests { Format::Dwarf32, &[]); - let abbrev = Abbreviation::new(42, - constants::DW_TAG_subprogram, - constants::DW_CHILDREN_yes, - vec![ - AttributeSpecification::new(constants::DW_AT_name, constants::DW_FORM_string), - AttributeSpecification::new(constants::DW_AT_low_pc, constants::DW_FORM_addr), - AttributeSpecification::new(constants::DW_AT_high_pc, constants::DW_FORM_addr), - ]); + let abbrev = + Abbreviation::new(42, + constants::DW_TAG_subprogram, + constants::DW_CHILDREN_yes, + vec![AttributeSpecification::new(constants::DW_AT_name, + constants::DW_FORM_string), + AttributeSpecification::new(constants::DW_AT_low_pc, + constants::DW_FORM_addr), + AttributeSpecification::new(constants::DW_AT_high_pc, + constants::DW_FORM_addr)]); // "foo", 42, 1337, 4 dangling bytes of 0xaa where children would be let buf = [0x66, 0x6f, 0x6f, 0x00, 0x2a, 0x00, 0x00, 0x00, 0x39, 0x05, 0x00, 0x00, 0xaa, @@ -3662,7 +3668,7 @@ mod tests { name: constants::DW_AT_name, value: AttributeValue::String(ffi::CStr::from_bytes_with_nul(b"foo\0") - .unwrap()), + .unwrap()), }); } otherwise => { @@ -3707,7 +3713,10 @@ mod tests { assert!(attrs.next().expect("should parse next").is_none()); assert!(entry.after_attrs.get().is_some()); - assert_eq!(entry.after_attrs.get().expect("should have entry.after_attrs"), + assert_eq!(entry + .after_attrs + .get() + .expect("should have entry.after_attrs"), &buf[buf.len() - 4..]) } @@ -3720,14 +3729,16 @@ mod tests { Format::Dwarf32, &[]); - let abbrev = Abbreviation::new(42, - constants::DW_TAG_subprogram, - constants::DW_CHILDREN_yes, - vec![ - AttributeSpecification::new(constants::DW_AT_name, constants::DW_FORM_string), - AttributeSpecification::new(constants::DW_AT_low_pc, constants::DW_FORM_addr), - AttributeSpecification::new(constants::DW_AT_high_pc, constants::DW_FORM_addr), - ]); + let abbrev = + Abbreviation::new(42, + constants::DW_TAG_subprogram, + constants::DW_CHILDREN_yes, + vec![AttributeSpecification::new(constants::DW_AT_name, + constants::DW_FORM_string), + AttributeSpecification::new(constants::DW_AT_low_pc, + constants::DW_FORM_addr), + AttributeSpecification::new(constants::DW_AT_high_pc, + constants::DW_FORM_addr)]); // "foo" let buf = [0x66, 0x6f, 0x6f, 0x00]; @@ -3753,7 +3764,7 @@ mod tests { name: constants::DW_AT_name, value: AttributeValue::String(ffi::CStr::from_bytes_with_nul(b"foo\0") - .unwrap()), + .unwrap()), }); } otherwise => { @@ -3779,7 +3790,8 @@ mod tests { fn assert_entry_name(entry: &DebuggingInformationEntry, name: &str) where Endian: Endianity { - let value = entry.attr_value(constants::DW_AT_name) + let value = entry + .attr_value(constants::DW_AT_name) .expect("Should have parsed the name attribute") .expect("Should have found the name attribute"); @@ -3800,7 +3812,8 @@ mod tests { fn assert_next_entry(cursor: &mut EntriesCursor, name: &str) where Endian: Endianity { - cursor.next_entry() + cursor + .next_entry() .expect("Should parse next entry") .expect("Should have an entry"); assert_current_name(cursor, name); @@ -3809,7 +3822,8 @@ mod tests { fn assert_next_entry_null(cursor: &mut EntriesCursor) where Endian: Endianity { - cursor.next_entry() + cursor + .next_entry() .expect("Should parse next entry") .expect("Should have an entry"); assert!(cursor.current().is_none()); @@ -3819,7 +3833,8 @@ mod tests { where Endian: Endianity { { - let (val, entry) = cursor.next_dfs() + let (val, entry) = cursor + .next_dfs() .expect("Should parse next dfs") .expect("Should not be done with traversal"); assert_eq!(val, depth); @@ -3832,7 +3847,8 @@ mod tests { where Endian: Endianity { { - let entry = cursor.next_sibling() + let entry = cursor + .next_sibling() .expect("Should parse next sibling") .expect("Should not be done with traversal"); assert_entry_name(entry, name); @@ -3843,7 +3859,8 @@ mod tests { fn assert_valid_sibling_ptr(cursor: &EntriesCursor) where Endian: Endianity { - let sibling_ptr = cursor.current() + let sibling_ptr = cursor + .current() .expect("Should have current entry") .attr_value(constants::DW_AT_sibling); match sibling_ptr { @@ -4072,7 +4089,8 @@ mod tests { let info_buf = &entries_cursor_tests_debug_info_buf(); let debug_info = DebugInfo::::new(info_buf); - let unit = debug_info.units() + let unit = debug_info + .units() .next() .expect("should have a unit result") .expect("and it should be ok"); @@ -4096,10 +4114,22 @@ mod tests { assert_next_sibling(&mut cursor, "004"); assert_next_dfs(&mut cursor, "005", 1); assert_next_sibling(&mut cursor, "006"); - assert!(cursor.next_sibling().expect("Should parse next sibling").is_none()); - assert!(cursor.next_sibling().expect("Should parse next sibling").is_none()); - assert!(cursor.next_sibling().expect("Should parse next sibling").is_none()); - assert!(cursor.next_sibling().expect("Should parse next sibling").is_none()); + assert!(cursor + .next_sibling() + .expect("Should parse next sibling") + .is_none()); + assert!(cursor + .next_sibling() + .expect("Should parse next sibling") + .is_none()); + assert!(cursor + .next_sibling() + .expect("Should parse next sibling") + .is_none()); + assert!(cursor + .next_sibling() + .expect("Should parse next sibling") + .is_none()); // And we should be able to continue with the children of the root entry. @@ -4108,7 +4138,10 @@ mod tests { // There should be no more siblings. - assert!(cursor.next_sibling().expect("Should parse next sibling").is_none()); + assert!(cursor + .next_sibling() + .expect("Should parse next sibling") + .is_none()); assert!(cursor.current().is_none()); } @@ -4191,7 +4224,10 @@ mod tests { // There should be no more siblings. - assert!(cursor.next_sibling().expect("Should parse next sibling").is_none()); + assert!(cursor + .next_sibling() + .expect("Should parse next sibling") + .is_none()); assert!(cursor.current().is_none()); } @@ -4216,7 +4252,8 @@ mod tests { let info_buf = section.get_contents().unwrap(); let debug_info = DebugInfo::::new(&info_buf); - let unit = debug_info.units() + let unit = debug_info + .units() .next() .expect("should have a unit result") .expect("and it should be ok"); @@ -4254,7 +4291,8 @@ mod tests { let info_buf = section.get_contents().unwrap(); let debug_types = DebugTypes::::new(&info_buf); - let unit = debug_types.units() + let unit = debug_types + .units() .next() .expect("should have a unit result") .expect("and it should be ok"); @@ -4274,7 +4312,8 @@ mod tests { let info_buf = &entries_cursor_tests_debug_info_buf(); let debug_info = DebugInfo::::new(info_buf); - let unit = debug_info.units() + let unit = debug_info + .units() .next() .expect("should have a unit result") .expect("and it should be ok"); @@ -4285,7 +4324,8 @@ mod tests { let abbrevs = unit.abbreviations(debug_abbrev) .expect("Should parse abbreviations"); - let mut cursor = unit.entries_at_offset(&abbrevs, UnitOffset(unit.header_size())).unwrap(); + let mut cursor = unit.entries_at_offset(&abbrevs, UnitOffset(unit.header_size())) + .unwrap(); assert_next_entry(&mut cursor, "001"); let cursor = unit.entries_at_offset(&abbrevs, UnitOffset(0)); @@ -4357,7 +4397,8 @@ mod tests { -> EntriesTreeIter<'input, 'abbrev, 'unit, 'tree, Endian> where Endian: Endianity { - let iter = iter.expect("Should parse entry").expect("Should have entry"); + let iter = iter.expect("Should parse entry") + .expect("Should have entry"); assert_entry_name(iter.entry().expect("Should have current entry"), name); iter } @@ -4389,16 +4430,21 @@ mod tests { }, offset: DebugInfoOffset(0), }; - let info_buf = - Section::with_endian(Endian::Little).comp_unit(&mut unit).get_contents().unwrap(); + let info_buf = Section::with_endian(Endian::Little) + .comp_unit(&mut unit) + .get_contents() + .unwrap(); let debug_info = DebugInfo::::new(&info_buf); - let unit = debug_info.units() + let unit = debug_info + .units() .next() .expect("Should parse unit") .expect("and it should be some"); - let abbrevs = unit.abbreviations(debug_abbrev).expect("Should parse abbreviations"); - let mut tree = unit.entries_tree(&abbrevs, None).expect("Should have entries tree"); + let abbrevs = unit.abbreviations(debug_abbrev) + .expect("Should parse abbreviations"); + let mut tree = unit.entries_tree(&abbrevs, None) + .expect("Should have entries tree"); // Test we can restart iteration of the tree. { @@ -4456,7 +4502,8 @@ mod tests { assert_null(iter.next()); // Test starting at an offset. - let mut tree = unit.entries_tree(&abbrevs, Some(entry2)).expect("Should have entries tree"); + let mut tree = unit.entries_tree(&abbrevs, Some(entry2)) + .expect("Should have entries tree"); let mut iter = tree.iter(); assert_entry_name(iter.entry().expect("Should have root entry"), "2"); assert_entry(iter.next(), "2a"); diff --git a/tests/parse_self.rs b/tests/parse_self.rs index eb11b6a12..c156ca59c 100644 --- a/tests/parse_self.rs +++ b/tests/parse_self.rs @@ -43,8 +43,7 @@ fn test_parse_self_debug_info() { let entry = cursor.current().expect("Should have a current entry"); let mut attrs = entry.attrs(); - while let Some(_) = attrs.next().expect("Should parse entry's attribute") { - } + while let Some(_) = attrs.next().expect("Should parse entry's attribute") {} } } } @@ -71,38 +70,47 @@ fn test_parse_self_debug_line() { let mut cursor = unit.entries(&abbrevs); cursor.next_dfs().expect("Should parse next dfs"); - let unit_entry = cursor.current() - .expect("Should have a root entry"); + let unit_entry = cursor.current().expect("Should have a root entry"); - let comp_dir = unit_entry.attr(gimli::DW_AT_comp_dir) + let comp_dir = unit_entry + .attr(gimli::DW_AT_comp_dir) .expect("Should parse comp_dir attribute") .and_then(|attr| attr.string_value(&debug_str)); - let comp_name = unit_entry.attr(gimli::DW_AT_name) + let comp_name = unit_entry + .attr(gimli::DW_AT_name) .expect("Should parse name attribute") .and_then(|attr| attr.string_value(&debug_str)); if let Some(AttributeValue::DebugLineRef(offset)) = - unit_entry.attr_value(gimli::DW_AT_stmt_list).expect("Should parse stmt_list") { - let program = debug_line.program(offset, unit.address_size(), comp_dir, comp_name) + unit_entry + .attr_value(gimli::DW_AT_stmt_list) + .expect("Should parse stmt_list") { + let program = debug_line + .program(offset, unit.address_size(), comp_dir, comp_name) .expect("should parse line number program header"); let mut results = Vec::new(); let mut rows = program.rows(); - while let Some((_, row)) = rows.next_row() - .expect("Should parse and execute all rows in the line number program") { + while let Some((_, row)) = + rows.next_row() + .expect("Should parse and execute all rows in the line number program") { results.push(*row); } results.reverse(); - let program = debug_line.program(offset, unit.address_size(), comp_dir, comp_name) + let program = debug_line + .program(offset, unit.address_size(), comp_dir, comp_name) .expect("should parse line number program header"); - let (program, sequences) = program.sequences() - .expect("should parse and execute the entire line number program"); + let (program, sequences) = + program + .sequences() + .expect("should parse and execute the entire line number program"); assert!(!sequences.is_empty()); // Should be at least one sequence. for sequence in sequences { let mut rows = program.resume_from(&sequence); - while let Some((_, row)) = rows.next_row() - .expect("Should parse and execute all rows after resuming") { + while let Some((_, row)) = + rows.next_row() + .expect("Should parse and execute all rows after resuming") { let other_row = results.pop().unwrap(); assert!(row.address() >= sequence.start); assert!(row.address() <= sequence.end); @@ -151,7 +159,7 @@ fn test_parse_self_debug_pubtypes() { // Because `.eh_frame` doesn't contain address sizes, we need to assume the // native word size, so this test is only valid on 64-bit machines (as the // `.eh_frame` fixture data was created on a 64-bit machine). -#[cfg(target_pointer_width="64")] +#[cfg(target_pointer_width = "64")] #[test] fn test_parse_self_eh_frame() { use gimli::{BaseAddresses, CieOrFde, EhFrame, UnwindSection}; @@ -159,10 +167,7 @@ fn test_parse_self_eh_frame() { let eh_frame = read_section("eh_frame"); let eh_frame = EhFrame::::new(&eh_frame); - let bases = BaseAddresses::default() - .set_cfi(0) - .set_data(0) - .set_text(0); + let bases = BaseAddresses::default().set_cfi(0).set_data(0).set_text(0); let mut entries = eh_frame.entries(&bases); while let Some(entry) = entries.next().expect("Should parse CFI entry OK") { match entry { @@ -173,7 +178,8 @@ fn test_parse_self_eh_frame() { } } CieOrFde::Fde(partial) => { - let fde = partial.parse(|offset| eh_frame.cie_from_offset(&bases, offset)) + let fde = partial + .parse(|offset| eh_frame.cie_from_offset(&bases, offset)) .expect("Should be able to get CIE for FDE"); let mut instrs = fde.instructions();