From: Eli Bendersky Date: Thu, 21 Feb 2013 22:53:19 +0000 (+0000) Subject: Previously, parsing capability of the .debug_frame section was added X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=705085da37eed9557202ff1e86f9462842ef7af9;p=platform%2Fupstream%2Fllvm.git Previously, parsing capability of the .debug_frame section was added to lib/DebugInfo, with dumping in llvm-dwarfdump. This patch adds initial ability to parse and dump CFA instructions contained in entries. To keep it manageable, the patch omits some more advanced capabilities (accounted in TODOs): * Parsing of instructions with BLOCK arguments (expression lists) * Dumping of actual instruction arguments (currently only names are dumped). This is quite tricky since the dumper has to effectively "interpret" the instructions. llvm-svn: 175820 --- diff --git a/llvm/lib/DebugInfo/DWARFDebugFrame.cpp b/llvm/lib/DebugInfo/DWARFDebugFrame.cpp index 69e3a3a..951c7a0 100644 --- a/llvm/lib/DebugInfo/DWARFDebugFrame.cpp +++ b/llvm/lib/DebugInfo/DWARFDebugFrame.cpp @@ -10,8 +10,12 @@ #include "DWARFDebugFrame.h" #include "llvm/ADT/SmallString.h" #include "llvm/Support/DataTypes.h" +#include "llvm/Support/ErrorHandling.h" #include "llvm/Support/Dwarf.h" #include "llvm/Support/Format.h" +#include "llvm/Support/raw_ostream.h" +#include +#include using namespace llvm; using namespace dwarf; @@ -29,9 +33,21 @@ public: } FrameKind getKind() const { return Kind; } + virtual uint64_t getOffset() const { return Offset; } + /// \brief Parse and store a sequence of CFI instructions from our data + /// stream, starting at Offset and ending at EndOffset. If everything + /// goes well, Offset should be equal to EndOffset when this method + /// returns. Otherwise, an error occurred. + /// TODO: Improve error reporting... + virtual void parseInstructions(uint32_t &Offset, uint32_t EndOffset); + + /// \brief Dump the entry header to the given output stream. virtual void dumpHeader(raw_ostream &OS) const = 0; + /// \brief Dump the entry's instructions to the given output stream. + virtual void dumpInstructions(raw_ostream &OS) const; + protected: const FrameKind Kind; @@ -44,8 +60,143 @@ protected: /// \brief Entry length as specified in DWARF. uint64_t Length; + + /// An entry may contain CFI instructions. An instruction consists of an + /// opcode and an optional sequence of operands. + typedef std::vector Operands; + struct Instruction { + Instruction(uint8_t Opcode) + : Opcode(Opcode) + {} + + uint8_t Opcode; + Operands Ops; + }; + + std::vector Instructions; + + /// Convenience methods to add a new instruction with the given opcode and + /// operands to the Instructions vector. + void addInstruction(uint8_t Opcode) { + Instructions.push_back(Instruction(Opcode)); + } + + void addInstruction(uint8_t Opcode, uint64_t Operand1) { + Instructions.push_back(Instruction(Opcode)); + Instructions.back().Ops.push_back(Operand1); + } + + void addInstruction(uint8_t Opcode, uint64_t Operand1, uint64_t Operand2) { + Instructions.push_back(Instruction(Opcode)); + Instructions.back().Ops.push_back(Operand1); + Instructions.back().Ops.push_back(Operand2); + } }; + +// See DWARF standard v3, section 7.23 +const uint8_t DWARF_CFI_PRIMARY_OPCODE_MASK = 0xc0; +const uint8_t DWARF_CFI_PRIMARY_OPERAND_MASK = 0x3f; + + +void FrameEntry::parseInstructions(uint32_t &Offset, uint32_t EndOffset) { + while (Offset < EndOffset) { + uint8_t Opcode = Data.getU8(&Offset); + // Some instructions have a primary opcode encoded in the top bits. + uint8_t Primary = Opcode & DWARF_CFI_PRIMARY_OPCODE_MASK; + + if (Primary) { + // If it's a primary opcode, the first operand is encoded in the bottom + // bits of the opcode itself. + uint64_t Op1 = Opcode & DWARF_CFI_PRIMARY_OPERAND_MASK; + switch (Primary) { + default: llvm_unreachable("Impossible primary CFI opcode"); + case DW_CFA_advance_loc: + case DW_CFA_restore: + addInstruction(Primary, Op1); + break; + case DW_CFA_offset: + addInstruction(Primary, Op1, Data.getULEB128(&Offset)); + break; + } + } else { + // Extended opcode - its value is Opcode itself. + switch (Opcode) { + default: llvm_unreachable("Invalid extended CFI opcode"); + case DW_CFA_nop: + case DW_CFA_remember_state: + case DW_CFA_restore_state: + // No operands + addInstruction(Opcode); + break; + case DW_CFA_set_loc: + // Operands: Address + addInstruction(Opcode, Data.getAddress(&Offset)); + break; + case DW_CFA_advance_loc1: + // Operands: 1-byte delta + addInstruction(Opcode, Data.getU8(&Offset)); + break; + case DW_CFA_advance_loc2: + // Operands: 2-byte delta + addInstruction(Opcode, Data.getU16(&Offset)); + break; + case DW_CFA_advance_loc4: + // Operands: 4-byte delta + addInstruction(Opcode, Data.getU32(&Offset)); + break; + case DW_CFA_restore_extended: + case DW_CFA_undefined: + case DW_CFA_same_value: + case DW_CFA_def_cfa_register: + case DW_CFA_def_cfa_offset: + // Operands: ULEB128 + addInstruction(Opcode, Data.getULEB128(&Offset)); + break; + case DW_CFA_def_cfa_offset_sf: + // Operands: SLEB128 + addInstruction(Opcode, Data.getSLEB128(&Offset)); + break; + case DW_CFA_offset_extended: + case DW_CFA_register: + case DW_CFA_def_cfa: + case DW_CFA_val_offset: + // Operands: ULEB128, ULEB128 + addInstruction(Opcode, Data.getULEB128(&Offset), + Data.getULEB128(&Offset)); + break; + case DW_CFA_offset_extended_sf: + case DW_CFA_def_cfa_sf: + case DW_CFA_val_offset_sf: + // Operands: ULEB128, SLEB128 + addInstruction(Opcode, Data.getULEB128(&Offset), + Data.getSLEB128(&Offset)); + break; + case DW_CFA_def_cfa_expression: + case DW_CFA_expression: + case DW_CFA_val_expression: + // TODO: implement this + report_fatal_error("Values with expressions not implemented yet!"); + } + } + } +} + + +void FrameEntry::dumpInstructions(raw_ostream &OS) const { + // TODO: at the moment only instruction names are dumped. Expand this to + // dump operands as well. + for (std::vector::const_iterator I = Instructions.begin(), + E = Instructions.end(); + I != E; ++I) { + uint8_t Opcode = I->Opcode; + if (Opcode & DWARF_CFI_PRIMARY_OPCODE_MASK) + Opcode &= DWARF_CFI_PRIMARY_OPCODE_MASK; + OS << " " << CallFrameString(Opcode) << ":\n"; + } +} + + namespace { /// \brief DWARF Common Information Entry (CIE) class CIE : public FrameEntry { @@ -69,9 +220,12 @@ public: << "\n"; OS << format(" Version: %d\n", Version); OS << " Augmentation: \"" << Augmentation << "\"\n"; - OS << format(" Code alignment factor: %u\n", (uint32_t)CodeAlignmentFactor); - OS << format(" Data alignment factor: %d\n", (int32_t)DataAlignmentFactor); - OS << format(" Return address column: %d\n", (int32_t)ReturnAddressRegister); + OS << format(" Code alignment factor: %u\n", + (uint32_t)CodeAlignmentFactor); + OS << format(" Data alignment factor: %d\n", + (int32_t)DataAlignmentFactor); + OS << format(" Return address column: %d\n", + (int32_t)ReturnAddressRegister); OS << "\n"; } @@ -111,7 +265,6 @@ public: (int32_t)LinkedCIEOffset, (uint32_t)InitialLocation, (uint32_t)InitialLocation + (uint32_t)AddressRange); - OS << "\n"; if (LinkedCIE) { OS << format("%p\n", LinkedCIE); } @@ -208,7 +361,15 @@ void DWARFDebugFrame::parse(DataExtractor Data) { Entries.push_back(NewFDE); } - Offset = EndStructureOffset; + Entries.back()->parseInstructions(Offset, EndStructureOffset); + + if (Offset != EndStructureOffset) { + std::string Str; + raw_string_ostream OS(Str); + OS << format("Parsing entry instructions at %lx failed", + Entries.back()->getOffset()); + report_fatal_error(Str); + } } } @@ -217,7 +378,10 @@ void DWARFDebugFrame::dump(raw_ostream &OS) const { OS << "\n"; for (EntryVector::const_iterator I = Entries.begin(), E = Entries.end(); I != E; ++I) { - (*I)->dumpHeader(OS); + FrameEntry *Entry = *I; + Entry->dumpHeader(OS); + Entry->dumpInstructions(OS); + OS << "\n"; } } diff --git a/llvm/lib/Support/Dwarf.cpp b/llvm/lib/Support/Dwarf.cpp index 615efb8b..0f91c11 100644 --- a/llvm/lib/Support/Dwarf.cpp +++ b/llvm/lib/Support/Dwarf.cpp @@ -688,6 +688,7 @@ const char *llvm::dwarf::MacinfoString(unsigned Encoding) { /// encodings. const char *llvm::dwarf::CallFrameString(unsigned Encoding) { switch (Encoding) { + case DW_CFA_nop: return "DW_CFA_nop"; case DW_CFA_advance_loc: return "DW_CFA_advance_loc"; case DW_CFA_offset: return "DW_CFA_offset"; case DW_CFA_restore: return "DW_CFA_restore"; diff --git a/llvm/test/DebugInfo/dwarfdump-debug-frame-simple.test b/llvm/test/DebugInfo/dwarfdump-debug-frame-simple.test index 595dd37..c2427d8 100644 --- a/llvm/test/DebugInfo/dwarfdump-debug-frame-simple.test +++ b/llvm/test/DebugInfo/dwarfdump-debug-frame-simple.test @@ -6,9 +6,22 @@ ; FRAMES: 00000000 00000010 ffffffff CIE ; FRAMES: Version: 1 +; FRAMES: DW_CFA_def_cfa +; FRAMES-NEXT: DW_CFA_offset +; FRAMES-NEXT: DW_CFA_nop +; FRAMES-NEXT: DW_CFA_nop ; FRAMES: 00000014 00000010 00000000 FDE cie=00000000 pc=00000000...00000022 +; FRAMES: DW_CFA_advance_loc +; FRAMES-NEXT: DW_CFA_def_cfa_offset +; FRAMES-NEXT: DW_CFA_nop + ; FRAMES: 00000028 00000014 00000000 FDE cie=00000000 pc=00000030...00000080 +; FRAMES: DW_CFA_advance_loc +; FRAMES-NEXT: DW_CFA_def_cfa_offset +; FRAMES-NEXT: DW_CFA_offset +; FRAMES-NEXT: DW_CFA_advance_loc +; FRAMES-NEXT: DW_CFA_def_cfa_register ; FRAMES-NOT: CIE ; FRAMES-NOT: FDE