From 9e4728e82abfd11af0ae9ead9ab9ea6842947936 Mon Sep 17 00:00:00 2001 From: Peter Edwards Date: Wed, 1 Jan 2025 03:13:21 +0000 Subject: [PATCH] throw, don't abort --- dwarf_die.cc | 23 +++++++++-------------- dwarf_frame.cc | 10 ++++------ dwarf_lines.cc | 6 ++---- dwarf_reader.cc | 11 ++++++----- dwarf_unit.cc | 2 +- dwarfproc.cc | 15 +++++---------- process.cc | 6 ++++-- 7 files changed, 31 insertions(+), 42 deletions(-) diff --git a/dwarf_die.cc b/dwarf_die.cc index 007c3f1..323214a 100644 --- a/dwarf_die.cc +++ b/dwarf_die.cc @@ -68,8 +68,7 @@ DIE::containsAddress(Elf::Addr addr) const start = uintmax_t(low); break; default: - abort(); - break; + throw (Exception() << "unhandled form " << low.form() << " for DW_AT_low_pc"); } switch (high.form()) { @@ -89,7 +88,7 @@ DIE::containsAddress(Elf::Addr addr) const end = start + uintmax_t(high); break; default: - abort(); + throw (Exception() << "unhandled form " << high.form() << " for DW_AT_high_pc"); } return start <= addr && end > addr ? ContainsAddr::YES : ContainsAddr::NO; } @@ -318,9 +317,7 @@ DIE::Attribute::Value::Value(DWARFReader &r, const FormEntry &forment, Unit *uni break; default: - addr = 0; - abort(); - break; + throw (Exception() << "unhandled form " << forment.form << " creating value for DIE"); } } @@ -429,7 +426,7 @@ DIE::Attribute::operator intmax_t() const case DW_FORM_sec_offset: return value().addr; default: - abort(); + throw (Exception() << "no conversion from form " << formp->form << " to intmax_t"); } } @@ -459,7 +456,7 @@ DIE::Attribute::operator uintmax_t() const case DW_FORM_rnglistx: return die.unit->rnglistx(value().udata); default: - abort(); + throw (Exception() << "no conversion from form " << formp->form << " to uintmax_t"); } } @@ -504,8 +501,7 @@ Ranges::Ranges(const DIE &die, uintmax_t base) { case DW_RLE_startx_endx: { /* auto startx = */ r.getuleb128(); /* auto endx = */ r.getuleb128(); - abort(); - break; + throw (Exception() << "DW_RLE_startx_endx is not handled"); } case DW_RLE_startx_length: { @@ -539,7 +535,7 @@ Ranges::Ranges(const DIE &die, uintmax_t base) { break; } default: - abort(); + throw (Exception() << "unhandled DW_RLE_ entry " << entryType); } } } @@ -579,7 +575,7 @@ DIE::Attribute::operator std::string() const return die.unit->strx(value().addr); default: - abort(); + throw (Exception() << "no conversion from form " << formp->form << " to string"); } } @@ -609,8 +605,7 @@ DIE::Attribute::operator DIE() const break; } default: - abort(); - break; + throw (Exception() << "no conversion from form " << formp->form << " to DIE"); } // Try this unit first (if we're dealing with the same Info) diff --git a/dwarf_frame.cc b/dwarf_frame.cc index 12f7733..72ca35a 100644 --- a/dwarf_frame.cc +++ b/dwarf_frame.cc @@ -38,7 +38,7 @@ CFI::decodeAddress(DWARFReader &f, uint8_t encoding, uintptr_t sectionVa) const base = f.getint(sizeof (Elf::Word)); break; default: - __builtin_unreachable(); + throw (Exception() << "unhandled encoding " << (encoding & 0xfU) << " while decoding CFI address"); } switch (encoding & 0xf0U & ~unsigned(DW_EH_PE_indirect)) { @@ -60,8 +60,7 @@ CFI::decodeAddress(DWARFReader &f, uint8_t encoding, uintptr_t sectionVa) const } default: - abort(); - break; + throw (Exception() << "unhandled base encoding inforation " << (encoding & 0xf0U) << " while decoding CFI address"); } return { base, (encoding & DW_EH_PE_indirect ) != 0 }; } @@ -448,13 +447,12 @@ CIE::execInsns(DWARFReader &r, uintmax_t addr, uintmax_t wantAddr) const case DW_CFA_GNU_window_save: case DW_CFA_GNU_negative_offset_extended: default: - abort(); + throw (Exception() << "unhandled secondary CFA instruction " << op); } break; default: - abort(); - break; + throw (Exception() << "unhandled CFA instruction " << op); } } return frame; diff --git a/dwarf_lines.cc b/dwarf_lines.cc index 593d357..eb64c78 100644 --- a/dwarf_lines.cc +++ b/dwarf_lines.cc @@ -180,8 +180,7 @@ LineInfo::build(DWARFReader &r, Unit &unit) break; default: r.skip(len - 1); - abort(); - break; + throw (Exception() << "unhandled DW_LNE_ instruction in line data"); } } else { /* Standard opcode. */ @@ -225,11 +224,10 @@ LineInfo::build(DWARFReader &r, Unit &unit) state.isa = r.getuleb128(); break; default: { - abort(); int argCount = opcode_lengths[opcode - 1]; for (int i = 0; i < argCount; i++) r.getuleb128(); - break; + throw (Exception() << "unhandled DW_LNS_ instruction in line data"); } case DW_LNS_none: break; diff --git a/dwarf_reader.cc b/dwarf_reader.cc index 861775b..c087837 100644 --- a/dwarf_reader.cc +++ b/dwarf_reader.cc @@ -17,7 +17,7 @@ DWARFReader::readForm(const Info &info, Unit &unit, Form form) return; default: - abort(); + throw (Exception() << "unhandled form when reading form " << form); } } @@ -27,8 +27,6 @@ DWARFReader::readFormString(const Info &dwarf, Unit &unit, Form form) switch (form) { case DW_FORM_string: return getstring(); - default: - abort(); case DW_FORM_line_strp: { auto off = getuint(unit.dwarfLen); return dwarf.debugLineStrings.io()->readString(off); @@ -41,6 +39,9 @@ DWARFReader::readFormString(const Info &dwarf, Unit &unit, Form form) size_t off = getuleb128(); return unit.strx(off); } + default: { + throw (Exception() << "unhandled form " << form << " when reading string"); + } } } @@ -57,7 +58,7 @@ DWARFReader::readFormUnsigned(Form form) case DW_FORM_data4: return getu32(); default: - abort(); + throw (Exception() << "unhandled form " << form << " when reading unsigned"); } } @@ -67,7 +68,7 @@ DWARFReader::readFormSigned(Form form) (void)this; // avoid warnings about making this static. switch (form) { default: - abort(); + throw (Exception() << "unhandled form " << form << " when reading signed"); } } diff --git a/dwarf_unit.cc b/dwarf_unit.cc index 17c7716..a1cbe8d 100644 --- a/dwarf_unit.cc +++ b/dwarf_unit.cc @@ -91,7 +91,7 @@ Unit::Unit(const Info *di, DWARFReader &r) r.getBytes(sizeof id, &id[0]); break; default: - abort(); + throw (Exception() << "unhandled DW_UT_ unit type value " << unitType); } } else { abbrevOffset = r.getuint(version <= 2 ? 4 : dwarfLen); diff --git a/dwarfproc.cc b/dwarfproc.cc index 558dd45..845c768 100644 --- a/dwarfproc.cc +++ b/dwarfproc.cc @@ -163,9 +163,8 @@ ExpressionStack::eval(Process &proc, const Dwarf::DIE::Attribute &attr, r.skip(len); break; } - default: - abort(); // can implement it when we see it. + throw (Exception() << "unhandled DW_LLE_" << lle); } } } else { @@ -187,7 +186,7 @@ ExpressionStack::eval(Process &proc, const Dwarf::DIE::Attribute &attr, r.skip(len); } } - abort(); + throw (Exception() << ".debug_loc search failed"); case Dwarf::DW_FORM_block1: case Dwarf::DW_FORM_block: @@ -197,7 +196,7 @@ ExpressionStack::eval(Process &proc, const Dwarf::DIE::Attribute &attr, return eval(proc, r, frame, reloc); } default: - abort(); + throw (Exception() << "unhandled attribute form " << attr.form() << " evaluating expression"); } } @@ -425,10 +424,7 @@ ExpressionStack::eval(Process &proc, Dwarf::DWARFReader &r, const StackFrame *fr // FALLTHROUGH default: - abort(); - if (proc.context.debug) - *proc.context.debug << "error evaluating DWARF OP " << op << " (" << int(op) << ")\n"; - return -1; + throw (Exception() << "error evaluating DWARF OP " << op << " (" << int(op) << ")"); } } return poptop(); @@ -481,8 +477,7 @@ std::optional StackFrame::unwind(Process &p) { case VAL_OFFSET: case VAL_EXPRESSION: case REG: - abort(); - break; + throw (Exception() << "unhandled CFA value type " << dcf.cfaValue.type); case OFFSET: cfa = Elf::getReg(regs, dcf.cfaReg) + dcf.cfaValue.u.offset; diff --git a/process.cc b/process.cc index fc152e6..fe3ef13 100644 --- a/process.cc +++ b/process.cc @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -1038,9 +1039,10 @@ ThreadStack::unwind(Process &p, Elf::CoreRegisters ®s) std::shared_ptr Process::load(Context &context, Elf::Object::sptr exec, std::string id) { pid_t pid; - std::istringstream(id) >> pid; + auto [ ptr, ec ] = std::from_chars(id.data(), id.data() + id.size(), pid); + std::shared_ptr proc; - if (pid != 0) { + if (ec == std::errc() && ptr == id.data() + id.size()) { if (kill(pid, 0) != 0) throw Exception() << "process " << pid << ": " << strerror(errno); proc = std::make_shared(context, exec, pid);