Author: jdevlieghere Date: Thu Jul 11 13:26:53 2019 New Revision: 365819 URL: http://llvm.org/viewvc/llvm-project?rev=365819&view=rev Log: Add convenience methods to convert LLDB to LLVM data structures.
This patch adds two convenience methods named GetAsLLVM to the LLDB counterparts of the DWARF DataExtractor and the DWARF context. The DWARFContext, once created, is cached for future usage. Differential revision: https://reviews.llvm.org/D64535 Modified: lldb/trunk/include/lldb/Core/Section.h lldb/trunk/source/Plugins/SymbolFile/DWARF/DWARFContext.cpp lldb/trunk/source/Plugins/SymbolFile/DWARF/DWARFContext.h lldb/trunk/source/Plugins/SymbolFile/DWARF/DWARFDataExtractor.cpp lldb/trunk/source/Plugins/SymbolFile/DWARF/DWARFDataExtractor.h lldb/trunk/source/Plugins/SymbolFile/DWARF/DebugNamesDWARFIndex.cpp lldb/trunk/tools/lldb-test/lldb-test.cpp Modified: lldb/trunk/include/lldb/Core/Section.h URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Core/Section.h?rev=365819&r1=365818&r2=365819&view=diff ============================================================================== --- lldb/trunk/include/lldb/Core/Section.h (original) +++ lldb/trunk/include/lldb/Core/Section.h Thu Jul 11 13:26:53 2019 @@ -38,6 +38,11 @@ public: typedef collection::iterator iterator; typedef collection::const_iterator const_iterator; + const_iterator begin() const { return m_sections.begin(); } + const_iterator end() const { return m_sections.end(); } + const_iterator begin() { return m_sections.begin(); } + const_iterator end() { return m_sections.end(); } + SectionList(); ~SectionList(); Modified: lldb/trunk/source/Plugins/SymbolFile/DWARF/DWARFContext.cpp URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/SymbolFile/DWARF/DWARFContext.cpp?rev=365819&r1=365818&r2=365819&view=diff ============================================================================== --- lldb/trunk/source/Plugins/SymbolFile/DWARF/DWARFContext.cpp (original) +++ lldb/trunk/source/Plugins/SymbolFile/DWARF/DWARFContext.cpp Thu Jul 11 13:26:53 2019 @@ -100,3 +100,37 @@ const DWARFDataExtractor &DWARFContext:: return LoadOrGetSection(eSectionTypeDWARFDebugTypes, eSectionTypeDWARFDebugTypesDwo, m_data_debug_types); } + +llvm::DWARFContext &DWARFContext::GetAsLLVM() { + if (!m_llvm_context) { + llvm::StringMap<std::unique_ptr<llvm::MemoryBuffer>> section_map; + uint8_t addr_size = 0; + + auto AddSection = [&](Section §ion) { + DataExtractor section_data; + section.GetSectionData(section_data); + + // Set the address size the first time we see it. + if (addr_size == 0) + addr_size = section_data.GetByteSize(); + + llvm::StringRef data = llvm::toStringRef(section_data.GetData()); + llvm::StringRef name = section.GetName().GetStringRef(); + section_map.try_emplace( + name, llvm::MemoryBuffer::getMemBuffer(data, name, false)); + }; + + if (m_main_section_list) { + for (auto §ion : *m_main_section_list) + AddSection(*section); + } + + if (m_dwo_section_list) { + for (auto §ion : *m_dwo_section_list) + AddSection(*section); + } + + m_llvm_context = llvm::DWARFContext::create(section_map, addr_size); + } + return *m_llvm_context; +} Modified: lldb/trunk/source/Plugins/SymbolFile/DWARF/DWARFContext.h URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/SymbolFile/DWARF/DWARFContext.h?rev=365819&r1=365818&r2=365819&view=diff ============================================================================== --- lldb/trunk/source/Plugins/SymbolFile/DWARF/DWARFContext.h (original) +++ lldb/trunk/source/Plugins/SymbolFile/DWARF/DWARFContext.h Thu Jul 11 13:26:53 2019 @@ -12,6 +12,7 @@ #include "DWARFDataExtractor.h" #include "lldb/Core/Section.h" #include "llvm/ADT/Optional.h" +#include "llvm/DebugInfo/DWARF/DWARFContext.h" #include "llvm/Support/Threading.h" #include <memory> @@ -20,6 +21,7 @@ class DWARFContext { private: SectionList *m_main_section_list; SectionList *m_dwo_section_list; + mutable std::unique_ptr<llvm::DWARFContext> m_llvm_context; struct SectionData { llvm::once_flag flag; @@ -64,6 +66,8 @@ public: const DWARFDataExtractor &getOrLoadStrData(); const DWARFDataExtractor &getOrLoadStrOffsetsData(); const DWARFDataExtractor &getOrLoadDebugTypesData(); + + llvm::DWARFContext &GetAsLLVM(); }; } // namespace lldb_private Modified: lldb/trunk/source/Plugins/SymbolFile/DWARF/DWARFDataExtractor.cpp URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/SymbolFile/DWARF/DWARFDataExtractor.cpp?rev=365819&r1=365818&r2=365819&view=diff ============================================================================== --- lldb/trunk/source/Plugins/SymbolFile/DWARF/DWARFDataExtractor.cpp (original) +++ lldb/trunk/source/Plugins/SymbolFile/DWARF/DWARFDataExtractor.cpp Thu Jul 11 13:26:53 2019 @@ -7,6 +7,7 @@ //===----------------------------------------------------------------------===// #include "DWARFDataExtractor.h" +#include "llvm/ADT/StringRef.h" namespace lldb_private { @@ -19,4 +20,11 @@ dw_offset_t DWARFDataExtractor::GetDWARFOffset(lldb::offset_t *offset_ptr) const { return GetMaxU64(offset_ptr, GetDWARFSizeOfOffset()); } + +llvm::DWARFDataExtractor DWARFDataExtractor::GetAsLLVM() const { + return llvm::DWARFDataExtractor( + llvm::StringRef(reinterpret_cast<const char *>(GetDataStart()), + GetByteSize()), + GetByteOrder() == lldb::eByteOrderLittle, GetAddressByteSize()); } +} // namespace lldb_private Modified: lldb/trunk/source/Plugins/SymbolFile/DWARF/DWARFDataExtractor.h URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/SymbolFile/DWARF/DWARFDataExtractor.h?rev=365819&r1=365818&r2=365819&view=diff ============================================================================== --- lldb/trunk/source/Plugins/SymbolFile/DWARF/DWARFDataExtractor.h (original) +++ lldb/trunk/source/Plugins/SymbolFile/DWARF/DWARFDataExtractor.h Thu Jul 11 13:26:53 2019 @@ -11,6 +11,7 @@ #include "lldb/Core/dwarf.h" #include "lldb/Utility/DataExtractor.h" +#include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h" namespace lldb_private { @@ -28,6 +29,8 @@ public: size_t GetDWARFSizeofInitialLength() const { return 4; } size_t GetDWARFSizeOfOffset() const { return 4; } + + llvm::DWARFDataExtractor GetAsLLVM() const; }; } Modified: lldb/trunk/source/Plugins/SymbolFile/DWARF/DebugNamesDWARFIndex.cpp URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/SymbolFile/DWARF/DebugNamesDWARFIndex.cpp?rev=365819&r1=365818&r2=365819&view=diff ============================================================================== --- lldb/trunk/source/Plugins/SymbolFile/DWARF/DebugNamesDWARFIndex.cpp (original) +++ lldb/trunk/source/Plugins/SymbolFile/DWARF/DebugNamesDWARFIndex.cpp Thu Jul 11 13:26:53 2019 @@ -16,13 +16,6 @@ using namespace lldb_private; using namespace lldb; -static llvm::DWARFDataExtractor ToLLVM(const DWARFDataExtractor &data) { - return llvm::DWARFDataExtractor( - llvm::StringRef(reinterpret_cast<const char *>(data.GetDataStart()), - data.GetByteSize()), - data.GetByteOrder() == eByteOrderLittle, data.GetAddressByteSize()); -} - llvm::Expected<std::unique_ptr<DebugNamesDWARFIndex>> DebugNamesDWARFIndex::Create(Module &module, DWARFDataExtractor debug_names, DWARFDataExtractor debug_str, @@ -31,8 +24,8 @@ DebugNamesDWARFIndex::Create(Module &mod return llvm::make_error<llvm::StringError>("debug info null", llvm::inconvertibleErrorCode()); } - auto index_up = - llvm::make_unique<DebugNames>(ToLLVM(debug_names), ToLLVM(debug_str)); + auto index_up = llvm::make_unique<DebugNames>(debug_names.GetAsLLVM(), + debug_str.GetAsLLVM()); if (llvm::Error E = index_up->extract()) return std::move(E); Modified: lldb/trunk/tools/lldb-test/lldb-test.cpp URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/tools/lldb-test/lldb-test.cpp?rev=365819&r1=365818&r2=365819&view=diff ============================================================================== --- lldb/trunk/tools/lldb-test/lldb-test.cpp (original) +++ lldb/trunk/tools/lldb-test/lldb-test.cpp Thu Jul 11 13:26:53 2019 @@ -739,7 +739,7 @@ static void dumpSectionList(LinePrinter Printer.formatLine("File size: {0}", S->GetFileSize()); if (opts::object::SectionContents) { - DataExtractor Data; + lldb_private::DataExtractor Data; S->GetSectionData(Data); ArrayRef<uint8_t> Bytes = {Data.GetDataStart(), Data.GetDataEnd()}; Printer.formatBinary("Data: ", Bytes, 0); _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits