diff --git a/include/llvm/DebugInfo/DWARF/DWARFDebugFrame.h b/include/llvm/DebugInfo/DWARF/DWARFDebugFrame.h index f61b4e41229..a711fb29544 100644 --- a/include/llvm/DebugInfo/DWARF/DWARFDebugFrame.h +++ b/include/llvm/DebugInfo/DWARF/DWARFDebugFrame.h @@ -29,8 +29,8 @@ public: DWARFDebugFrame(bool IsEH); ~DWARFDebugFrame(); - /// \brief Dump the section data into the given stream. - void dump(raw_ostream &OS) const; + /// Dump the section data into the given stream. + void dump(raw_ostream &OS, Optional Offset) const; /// \brief Parse the section from raw data. /// data is assumed to be pointing to the beginning of the section. @@ -39,6 +39,9 @@ public: /// Return whether the section has any entries. bool empty() const { return Entries.empty(); } + /// Return the entry at the given offset or nullptr. + FrameEntry *getEntryAtOffset(uint64_t Offset) const; + private: std::vector> Entries; }; diff --git a/lib/DebugInfo/DWARF/DWARFContext.cpp b/lib/DebugInfo/DWARF/DWARFContext.cpp index f20a7ca024b..69a4fe64b75 100644 --- a/lib/DebugInfo/DWARF/DWARFContext.cpp +++ b/lib/DebugInfo/DWARF/DWARFContext.cpp @@ -299,14 +299,12 @@ void DWARFContext::dump( } if (shouldDump(Explicit, ".debug_frame", DIDT_ID_DebugFrame, - DObj->getDebugFrameSection())) { - getDebugFrame()->dump(OS); - } + DObj->getDebugFrameSection())) + getDebugFrame()->dump(OS, DumpOffset); if (shouldDump(Explicit, ".eh_frame", DIDT_ID_DebugFrame, - DObj->getEHFrameSection())) { - getEHFrame()->dump(OS); - } + DObj->getEHFrameSection())) + getEHFrame()->dump(OS, DumpOffset); if (DumpType & DIDT_DebugMacro) { if (Explicit || !getDebugMacro()->empty()) { diff --git a/lib/DebugInfo/DWARF/DWARFDebugFrame.cpp b/lib/DebugInfo/DWARF/DWARFDebugFrame.cpp index 6e8b5f4f471..bceb0162b35 100644 --- a/lib/DebugInfo/DWARF/DWARFDebugFrame.cpp +++ b/lib/DebugInfo/DWARF/DWARFDebugFrame.cpp @@ -11,7 +11,6 @@ #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/Optional.h" -#include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SmallString.h" #include "llvm/ADT/StringExtras.h" #include "llvm/ADT/StringRef.h" @@ -46,19 +45,26 @@ public: FrameKind getKind() const { return Kind; } virtual uint64_t getOffset() const { return Offset; } - /// \brief Parse and store a sequence of CFI instructions from Data, + /// Parse and store a sequence of CFI instructions from Data, /// 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. virtual void parseInstructions(DataExtractor Data, uint32_t *Offset, uint32_t EndOffset); - /// \brief Dump the entry header to the given output stream. + /// 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. + /// Dump the entry's instructions to the given output stream. virtual void dumpInstructions(raw_ostream &OS) const; + /// Dump the entire entry to the given output stream. + void dump(raw_ostream &OS) const { + dumpHeader(OS); + dumpInstructions(OS); + OS << "\n"; + } + protected: const FrameKind Kind; @@ -692,11 +698,24 @@ void DWARFDebugFrame::parse(DataExtractor Data) { } } -void DWARFDebugFrame::dump(raw_ostream &OS) const { - OS << "\n"; - for (const auto &Entry : Entries) { - Entry->dumpHeader(OS); - Entry->dumpInstructions(OS); - OS << "\n"; - } +FrameEntry *DWARFDebugFrame::getEntryAtOffset(uint64_t Offset) const { + auto It = + std::lower_bound(Entries.begin(), Entries.end(), Offset, + [](const std::unique_ptr &E, + uint64_t Offset) { return E->getOffset() < Offset; }); + if (It != Entries.end() && (*It)->getOffset() == Offset) + return It->get(); + return nullptr; +} + +void DWARFDebugFrame::dump(raw_ostream &OS, Optional Offset) const { + if (Offset) { + if (auto *Entry = getEntryAtOffset(*Offset)) + Entry->dump(OS); + return; + } + + OS << "\n"; + for (const auto &Entry : Entries) + Entry->dump(OS); } diff --git a/test/tools/llvm-dwarfdump/X86/debug_frame_offset.test b/test/tools/llvm-dwarfdump/X86/debug_frame_offset.test new file mode 100644 index 00000000000..a441364e8fc --- /dev/null +++ b/test/tools/llvm-dwarfdump/X86/debug_frame_offset.test @@ -0,0 +1,14 @@ +RUN: llc -filetype=obj %p/../../dsymutil/Inputs/frame-dw2.ll -o - \ +RUN: | llvm-dwarfdump -debug-frame=0x00000014 - | FileCheck %s +CHECK: .debug_frame contents: +CHECK-NEXT: 00000014 00000014 00000000 FDE cie=00000000 pc=00000000...0000001d +CHECK-NEXT: DW_CFA_advance_loc: 1 +CHECK-NOT: pc + +RUN: llvm-dwarfdump %p/../../dsymutil/Inputs/basic1.macho.x86_64.o \ +RUN: -eh-frame=0x00000018 | FileCheck %s --check-prefix=EH +EH: .eh_frame contents: +EH-NEXT: 00000018 00000024 0000001c FDE cie=0000001c pc=fffffd00...fffffd24 +EH-NEXT: DW_CFA_advance_loc: 1 +EH-NOT: pc +EH-NOT: CIE