Skip to content

Commit 175a7fe

Browse files
committed
Reword read error messages
Try to make errors more concise and consistent, and report values were possible.
1 parent 3075aab commit 175a7fe

File tree

2 files changed

+97
-175
lines changed

2 files changed

+97
-175
lines changed

src/read/dwarf.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1725,7 +1725,7 @@ mod tests {
17251725
Err(e) => {
17261726
assert_eq!(
17271727
dwarf.format_error(e),
1728-
"Hit the end of input before it was expected at .debug_str+0x1"
1728+
"unexpected end of input at .debug_str+0x1"
17291729
);
17301730
}
17311731
}
@@ -1734,7 +1734,7 @@ mod tests {
17341734
Err(e) => {
17351735
assert_eq!(
17361736
dwarf.format_error(e),
1737-
"Hit the end of input before it was expected at .debug_str(sup)+0x1"
1737+
"unexpected end of input at .debug_str(sup)+0x1"
17381738
);
17391739
}
17401740
}

src/read/mod.rs

Lines changed: 95 additions & 173 deletions
Original file line numberDiff line numberDiff line change
@@ -414,261 +414,183 @@ pub enum Error {
414414
impl fmt::Display for Error {
415415
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> ::core::result::Result<(), fmt::Error> {
416416
match *self {
417-
Error::Io => write!(f, "An I/O error occurred while reading."),
417+
Error::Io => write!(f, "I/O error"),
418418
Error::PcRelativePointerButSectionBaseIsUndefined => {
419-
write!(
420-
f,
421-
"Found a PC relative pointer, but the section base is undefined."
422-
)
419+
write!(f, "undefined section base for PC relative pointer")
423420
}
424421
Error::TextRelativePointerButTextBaseIsUndefined => {
425-
write!(
426-
f,
427-
"Found a `.text` relative pointer, but the `.text` base is undefined."
428-
)
422+
write!(f, "undefined text base for relative pointer")
429423
}
430424
Error::DataRelativePointerButDataBaseIsUndefined => {
431-
write!(
432-
f,
433-
"Found a data relative pointer, but the data base is undefined."
434-
)
425+
write!(f, "undefined data base for relative pointer")
435426
}
436427
Error::FuncRelativePointerInBadContext => {
437-
write!(
438-
f,
439-
"Found a function relative pointer in a context that does not have a function base."
440-
)
428+
write!(f, "undefined function base for relative pointer")
441429
}
442430
Error::CannotParseOmitPointerEncoding => {
443-
write!(f, "Cannot parse a pointer with a `DW_EH_PE_omit` encoding.")
431+
write!(f, "invalid pointer encoding: DW_EH_PE_omit")
444432
}
445-
Error::BadUnsignedLeb128 => write!(f, "An error parsing an unsigned LEB128 value"),
446-
Error::BadSignedLeb128 => write!(f, "An error parsing a signed LEB128 value"),
433+
Error::BadUnsignedLeb128 => write!(f, "unsigned LEB128 overflow"),
434+
Error::BadSignedLeb128 => write!(f, "signed LEB128 overflow"),
447435
Error::AbbreviationTagZero => {
448-
write!(
449-
f,
450-
"An abbreviation declared that its tag is zero,
451-
but zero is reserved for null records"
452-
)
436+
write!(f, "invalid abbreviation tag: zero")
453437
}
454438
Error::AttributeNameZero => {
455-
write!(
456-
f,
457-
"An attribute specification declared that its name is zero,
458-
but zero is reserved for null records"
459-
)
439+
write!(f, "invalid attribute name: zero")
460440
}
461441
Error::AttributeFormZero => {
462-
write!(
463-
f,
464-
"An attribute specification declared that its form is zero,
465-
but zero is reserved for null records"
466-
)
442+
write!(f, "invalid attribute form: zero")
467443
}
468444
Error::BadHasChildren => {
469-
write!(
470-
f,
471-
"The abbreviation's has-children byte was not one of
472-
`DW_CHILDREN_{{yes,no}}`"
473-
)
445+
write!(f, "invalid abbreviation children")
474446
}
475-
Error::UnknownForm(_) => write!(f, "Found an unknown `DW_FORM_*` type"),
447+
Error::UnknownForm(val) => write!(f, "unknown attribute form: 0x{:x}", val.0),
476448
Error::DuplicateAbbreviationCode => {
477-
write!(f, "Found an abbreviation code that has already been used")
449+
write!(f, "duplicate abbreviation code")
478450
}
479-
Error::UnknownReservedLength => write!(f, "Found an unknown reserved length value"),
480-
Error::UnknownVersion(_) => write!(f, "Found an unknown DWARF version"),
481-
Error::UnknownAbbreviation(_) => {
482-
write!(f, "Found a record with an unknown abbreviation code")
451+
Error::UnknownReservedLength => write!(f, "unknown reserved initial length"),
452+
Error::UnknownVersion(version) => write!(f, "unknown DWARF version: {version}"),
453+
Error::UnknownAbbreviation(val) => {
454+
write!(f, "invalid abbreviation code: 0x{val:x}")
483455
}
484-
Error::UnexpectedEof(_) => write!(f, "Hit the end of input before it was expected"),
485-
Error::UnknownLocListsEntry(_) => write!(f, "Found an unknown location lists entry"),
486-
Error::UnknownRangeListsEntry(_) => write!(f, "Found an unknown range lists entry"),
487-
Error::UnsupportedAddressSize(_) => {
488-
write!(f, "The specified address size is not supported")
456+
Error::UnexpectedEof(_) => write!(f, "unexpected end of input"),
457+
Error::UnknownLocListsEntry(val) => {
458+
write!(f, "unknown location lists entry: 0x{:x}", val.0)
489459
}
490-
Error::UnsupportedOffsetSize(_) => {
491-
write!(f, "The specified offset size is not supported")
460+
Error::UnknownRangeListsEntry(val) => {
461+
write!(f, "unknown range lists entry: 0x{:x}", val.0)
462+
}
463+
Error::UnsupportedAddressSize(val) => {
464+
write!(f, "unsupported address size: {val}")
465+
}
466+
Error::UnsupportedOffsetSize(val) => {
467+
write!(f, "unsupported offset size: {val}")
492468
}
493469
Error::MinimumInstructionLengthZero => {
494-
write!(f, "The minimum instruction length must not be zero.")
470+
write!(f, "invalid minimum line instruction length: zero")
495471
}
496472
Error::MaximumOperationsPerInstructionZero => {
497-
write!(
498-
f,
499-
"The maximum operations per instruction must not be zero."
500-
)
501-
}
502-
Error::LineRangeZero => write!(f, "The line range must not be zero."),
503-
Error::OpcodeBaseZero => write!(f, "The opcode base must not be zero."),
504-
Error::BadUtf8 => write!(f, "Found an invalid UTF-8 string."),
505-
Error::NotCieId => write!(f, "Expected to find the CIE ID, but found something else."),
473+
write!(f, "invalid maximum operations per line instruction: zero")
474+
}
475+
Error::LineRangeZero => write!(f, "invalid line range: zero"),
476+
Error::OpcodeBaseZero => write!(f, "invalid line opcode base: zero"),
477+
Error::BadUtf8 => write!(f, "invalid UTF-8"),
478+
Error::NotCieId => write!(f, "no CIE at offset"),
506479
Error::NotFdePointer => {
507-
write!(
508-
f,
509-
"Expected to find an FDE pointer, but found a CIE pointer instead."
510-
)
480+
write!(f, "no FDE at offset")
511481
}
512-
Error::BadBranchTarget(_) => write!(f, "Invalid branch target in DWARF expression"),
482+
Error::BadBranchTarget(_) => write!(f, "invalid expression branch target"),
513483
Error::InvalidPushObjectAddress => {
514-
write!(
515-
f,
516-
"DW_OP_push_object_address used but no object address given"
517-
)
484+
write!(f, "undefined object address")
518485
}
519486
Error::NotEnoughStackItems => {
520-
write!(f, "Not enough items on stack when evaluating expression")
487+
write!(f, "expression stack underflow")
521488
}
522489
Error::TooManyIterations => {
523-
write!(f, "Too many iterations to evaluate DWARF expression")
490+
write!(f, "exceeded maximum expression iterations")
524491
}
525-
Error::InvalidExpression(_) => write!(f, "Invalid opcode in DWARF expression"),
492+
Error::InvalidExpression(val) => write!(f, "unknown expression opcode: 0x{:x}", val.0),
526493
Error::UnsupportedEvaluation => {
527-
write!(f, "Unsupported operation when evaluating expression")
494+
write!(f, "unsupported evaluation operation")
528495
}
529496
Error::InvalidPiece => {
530-
write!(
531-
f,
532-
"DWARF expression has piece followed by non-piece expression at end"
533-
)
497+
write!(f, "missing expression piece terminator")
534498
}
535499
Error::InvalidExpressionTerminator(_) => {
536-
write!(f, "Expected DW_OP_piece or DW_OP_bit_piece")
500+
write!(f, "invalid expression piece terminator")
537501
}
538502
Error::DivisionByZero => {
539-
write!(f, "Division or modulus by zero when evaluating expression")
503+
write!(f, "division by zero")
540504
}
541-
Error::TypeMismatch => write!(f, "Type mismatch when evaluating expression"),
505+
Error::TypeMismatch => write!(f, "invalid operand type: mismatch"),
542506
Error::IntegralTypeRequired => {
543-
write!(f, "Integral type expected when evaluating expression")
507+
write!(f, "invalid operand type: integral required")
544508
}
545509
Error::UnsupportedTypeOperation => {
546-
write!(
547-
f,
548-
"An expression operation used types that are not supported"
549-
)
510+
write!(f, "unsupported operand type")
550511
}
551512
Error::InvalidShiftExpression => {
552-
write!(
553-
f,
554-
"The shift value in an expression must be a non-negative integer."
555-
)
556-
}
557-
Error::InvalidDerefSize(_) => {
558-
write!(
559-
f,
560-
"The size of a deref expression must not be larger than the size of an address."
561-
)
562-
}
563-
Error::UnknownCallFrameInstruction(_) => write!(f, "An unknown DW_CFA_* instruction"),
513+
write!(f, "invalid shift length")
514+
}
515+
Error::InvalidDerefSize(val) => {
516+
write!(f, "invalid deref size: {val}")
517+
}
518+
Error::UnknownCallFrameInstruction(val) => {
519+
write!(f, "unknown call frame instruction: 0x{:x}", val.0)
520+
}
564521
Error::InvalidAddressRange => {
565-
write!(
566-
f,
567-
"The end of an address range must not be before the beginning."
568-
)
522+
write!(f, "invalid call frame instruction address")
569523
}
570-
Error::AddressOverflow => write!(f, "An address calculation overflowed."),
524+
Error::AddressOverflow => write!(f, "address overflow"),
571525
Error::CfiInstructionInInvalidContext => {
572-
write!(
573-
f,
574-
"Encountered a call frame instruction in a context in which it is not valid."
575-
)
526+
write!(f, "invalid context for call frame instruction")
576527
}
577528
Error::PopWithEmptyStack => {
578-
write!(
579-
f,
580-
"When evaluating call frame instructions, found a `DW_CFA_restore_state` stack pop \
581-
instruction, but the stack was empty, and had nothing to pop."
582-
)
529+
write!(f, "call frame stack underflow")
583530
}
584531
Error::NoUnwindInfoForAddress => {
585-
write!(f, "Do not have unwind info for the given address.")
532+
write!(f, "no unwind info")
586533
}
587534
Error::UnsupportedOffset => {
588-
write!(
589-
f,
590-
"An offset value was larger than the maximum supported value."
591-
)
592-
}
593-
Error::UnknownPointerEncoding(_) => {
594-
write!(
595-
f,
596-
"The given pointer encoding is either unknown or invalid."
597-
)
598-
}
599-
Error::NoEntryAtGivenOffset => write!(f, "Did not find an entry at the given offset."),
600-
Error::OffsetOutOfBounds => write!(f, "The given offset is out of bounds."),
601-
Error::UnknownAugmentation => write!(f, "Found an unknown CFI augmentation."),
535+
write!(f, "offset overflow")
536+
}
537+
Error::UnknownPointerEncoding(val) => {
538+
write!(f, "unknown pointer encoding: 0x{:x}", val.0)
539+
}
540+
Error::NoEntryAtGivenOffset => write!(f, "no entry at offset"),
541+
Error::OffsetOutOfBounds => write!(f, "invalid offset"),
542+
Error::UnknownAugmentation => write!(f, "unknown CFI augmentation"),
602543
Error::UnsupportedPointerEncoding => {
603-
write!(f, "We do not support the given pointer encoding yet.")
544+
write!(f, "unsupported pointer encoding")
604545
}
605-
Error::UnsupportedRegister(_) => {
606-
write!(f, "Registers larger than `u16` are not supported.")
546+
Error::UnsupportedRegister(val) => {
547+
write!(f, "unsupported register: 0x{val:x}")
607548
}
608549
Error::TooManyRegisterRules => {
609-
write!(
610-
f,
611-
"The CFI program defined more register rules than we have storage for."
612-
)
550+
write!(f, "too many CFI register rules")
613551
}
614552
Error::StackFull => {
615-
write!(
616-
f,
617-
"Attempted to push onto the CFI stack, but it was already at full capacity."
618-
)
553+
write!(f, "CFI stack overflow")
619554
}
620555
Error::VariableLengthSearchTable => {
621-
write!(
622-
f,
623-
"The `.eh_frame_hdr` binary search table claims to be variable-length encoded, \
624-
which makes binary search impossible."
625-
)
556+
write!(f, "invalid pointer encoding for search")
626557
}
627558
Error::UnsupportedUnitType => {
628-
write!(f, "The `DW_UT_*` value for this unit is not supported yet")
559+
write!(f, "unknown unit type")
629560
}
630-
Error::UnsupportedSegmentSize => write!(f, "Nonzero segment size not supported yet"),
561+
Error::UnsupportedSegmentSize => write!(f, "unsupported segment size"),
631562
Error::MissingUnitDie => {
632-
write!(
633-
f,
634-
"A compilation unit or type unit is missing its top level DIE."
635-
)
563+
write!(f, "missing root DIE")
636564
}
637565
Error::MissingSplitUnit => {
638-
write!(
639-
f,
640-
"A split DWARF section does not contain the split compilation unit."
641-
)
566+
write!(f, "missing split unit")
642567
}
643568
Error::UnsupportedAttributeForm => {
644-
write!(f, "A DIE attribute used an unsupported form.")
569+
write!(f, "unsupported attribute form")
645570
}
646571
Error::MissingFileEntryFormatPath => {
647-
write!(f, "Missing DW_LNCT_path in file entry format.")
572+
write!(f, "missing file entry format path")
648573
}
649574
Error::ExpectedStringAttributeValue => {
650-
write!(f, "Expected an attribute value to be a string form.")
575+
write!(f, "unsupported string attribute form")
651576
}
652577
Error::InvalidImplicitConst => {
653-
write!(f, "DW_FORM_implicit_const used in an invalid context.")
578+
write!(f, "invalid implicit const form")
654579
}
655-
Error::InvalidIndexSectionCount => write!(f, "Invalid section count in `.dwp` index."),
656-
Error::InvalidIndexSlotCount => write!(f, "Invalid slot count in `.dwp` index."),
657-
Error::InvalidIndexRow => write!(f, "Invalid hash row in `.dwp` index."),
658-
Error::UnknownIndexSection(_) => write!(f, "Unknown section type in `.dwp` index."),
659-
Error::UnknownIndexSectionV2(_) => {
660-
write!(f, "Unknown section type in version 2 `.dwp` index.")
580+
Error::InvalidIndexSectionCount => write!(f, "unsupported DWP section count"),
581+
Error::InvalidIndexSlotCount => write!(f, "invalid DWP slot count"),
582+
Error::InvalidIndexRow => write!(f, "invalid DWP unit index"),
583+
Error::UnknownIndexSection(val) => write!(f, "unknown DWP section type: 0x{:x}", val.0),
584+
Error::UnknownIndexSectionV2(val) => {
585+
write!(f, "unknown DWP v2 section type: 0x{:x}", val.0)
661586
}
662-
Error::InvalidMacinfoType(_) => write!(f, "Invalid macinfo type in `.debug_macinfo`."),
663-
Error::InvalidMacroType(_) => write!(f, "Invalid macro type in `.debug_macro`."),
587+
Error::InvalidMacinfoType(val) => write!(f, "unknown macinfo type: 0x{:x}", val.0),
588+
Error::InvalidMacroType(val) => write!(f, "unknown macro type: 0x{:x}", val.0),
664589
Error::UnsupportedOpcodeOperandsTable => {
665-
write!(
666-
f,
667-
"The optional `opcode_operands_table` in `.debug_macro` is currently not supported."
668-
)
590+
write!(f, "unsupported macro opcode operands table")
669591
}
670-
Error::InvalidNameAttributeIndex(_) => {
671-
write!(f, "Invalid index in a `.debug_names` attribute value.")
592+
Error::InvalidNameAttributeIndex(val) => {
593+
write!(f, "invalid index in name attribute: 0x{val:x}")
672594
}
673595
}
674596
}

0 commit comments

Comments
 (0)