diff --git a/lldb/unittests/Expression/DWARFExpressionTest.cpp b/lldb/unittests/Expression/DWARFExpressionTest.cpp --- a/lldb/unittests/Expression/DWARFExpressionTest.cpp +++ b/lldb/unittests/Expression/DWARFExpressionTest.cpp @@ -7,17 +7,12 @@ //===----------------------------------------------------------------------===// #include "lldb/Expression/DWARFExpression.h" -#include "../../source/Plugins/SymbolFile/DWARF/DWARFUnit.h" -#include "../../source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.h" -#include "TestingSupport/SubsystemRAII.h" -#include "lldb/Core/Module.h" -#include "lldb/Core/Section.h" +#include "TestingSupport/Symbol/YAMLModuleTester.h" #include "lldb/Core/Value.h" #include "lldb/Core/dwarf.h" #include "lldb/Symbol/ObjectFile.h" #include "lldb/Utility/StreamString.h" #include "llvm/ADT/StringExtras.h" -#include "llvm/ObjectYAML/DWARFEmitter.h" #include "llvm/Testing/Support/Error.h" #include "gtest/gtest.h" @@ -55,126 +50,9 @@ } } -/// A mock module holding an object file parsed from YAML. -class YAMLModule : public lldb_private::Module { -public: - YAMLModule(ArchSpec &arch) : Module(FileSpec("test"), arch) {} - void SetObjectFile(lldb::ObjectFileSP obj_file) { m_objfile_sp = obj_file; } - ObjectFile *GetObjectFile() override { return m_objfile_sp.get(); } -}; - -/// A mock object file that can be parsed from YAML. -class YAMLObjectFile : public lldb_private::ObjectFile { - const lldb::ModuleSP m_module_sp; - llvm::StringMap> &m_section_map; - /// Because there is only one DataExtractor in the ObjectFile - /// interface, all sections are copied into a contiguous buffer. - std::vector m_buffer; - -public: - YAMLObjectFile(const lldb::ModuleSP &module_sp, - llvm::StringMap> &map) - : ObjectFile(module_sp, &module_sp->GetFileSpec(), /*file_offset*/ 0, - /*length*/ 0, /*data_sp*/ nullptr, /*data_offset*/ 0), - m_module_sp(module_sp), m_section_map(map) {} - - /// Callback for initializing the module's list of sections. - void CreateSections(SectionList &unified_section_list) override { - lldb::offset_t total_bytes = 0; - for (auto &entry : m_section_map) - total_bytes += entry.getValue()->getBufferSize(); - m_buffer.reserve(total_bytes); - m_data = - DataExtractor(m_buffer.data(), total_bytes, lldb::eByteOrderLittle, 4); - - lldb::user_id_t sect_id = 1; - for (auto &entry : m_section_map) { - llvm::StringRef name = entry.getKey(); - lldb::SectionType sect_type = - llvm::StringSwitch(name) - .Case("debug_info", lldb::eSectionTypeDWARFDebugInfo) - .Case("debug_abbrev", lldb::eSectionTypeDWARFDebugAbbrev); - auto &membuf = entry.getValue(); - lldb::addr_t file_vm_addr = 0; - lldb::addr_t vm_size = 0; - lldb::offset_t file_offset = m_buffer.size(); - lldb::offset_t file_size = membuf->getBufferSize(); - m_buffer.resize(file_offset + file_size); - memcpy(m_buffer.data() + file_offset, membuf->getBufferStart(), - file_size); - uint32_t log2align = 0; - uint32_t flags = 0; - auto section_sp = std::make_shared( - m_module_sp, this, sect_id++, ConstString(name), sect_type, - file_vm_addr, vm_size, file_offset, file_size, log2align, flags); - unified_section_list.AddSection(section_sp); - } - } - - /// \{ - /// Stub methods that aren't needed here. - ConstString GetPluginName() override { return ConstString("YAMLObjectFile"); } - uint32_t GetPluginVersion() override { return 0; } - void Dump(Stream *s) override {} - uint32_t GetAddressByteSize() const override { return 8; } - uint32_t GetDependentModules(FileSpecList &file_list) override { return 0; } - bool IsExecutable() const override { return 0; } - ArchSpec GetArchitecture() override { return {}; } - Symtab *GetSymtab() override { return nullptr; } - bool IsStripped() override { return false; } - UUID GetUUID() override { return {}; } - lldb::ByteOrder GetByteOrder() const override { - return lldb::eByteOrderLittle; - } - bool ParseHeader() override { return false; } - Type CalculateType() override { return {}; } - Strata CalculateStrata() override { return {}; } - /// \} -}; - -/// Helper class that can construct a module from YAML and evaluate -/// DWARF expressions on it. -class YAMLModuleTester { - SubsystemRAII subsystems; - llvm::StringMap> m_sections_map; - lldb::ModuleSP m_module_sp; - lldb::ObjectFileSP m_objfile_sp; - DWARFUnitSP m_dwarf_unit; - std::unique_ptr m_symfile_dwarf; - -public: - /// Parse the debug info sections from the YAML description. - YAMLModuleTester(llvm::StringRef yaml_data, llvm::StringRef triple) { - auto sections_map = llvm::DWARFYAML::EmitDebugSections(yaml_data, true); - if (!sections_map) - return; - m_sections_map = std::move(*sections_map); - ArchSpec arch(triple); - m_module_sp = std::make_shared(arch); - m_objfile_sp = std::make_shared(m_module_sp, m_sections_map); - static_cast(m_module_sp.get())->SetObjectFile(m_objfile_sp); - - lldb::user_id_t uid = 0; - llvm::StringRef raw_debug_info = m_sections_map["debug_info"]->getBuffer(); - lldb_private::DataExtractor debug_info( - raw_debug_info.data(), raw_debug_info.size(), - m_objfile_sp->GetByteOrder(), m_objfile_sp->GetAddressByteSize()); - lldb::offset_t offset_ptr = 0; - m_symfile_dwarf = std::make_unique(m_objfile_sp, nullptr); - llvm::Expected dwarf_unit = DWARFUnit::extract( - *m_symfile_dwarf, uid, - *static_cast(&debug_info), - DIERef::DebugInfo, &offset_ptr); - if (dwarf_unit) - m_dwarf_unit = dwarf_unit.get(); - } - DWARFUnitSP GetDwarfUnit() { return m_dwarf_unit; } - - // Evaluate a raw DWARF expression. - llvm::Expected Eval(llvm::ArrayRef expr) { - return ::Evaluate(expr, m_module_sp, m_dwarf_unit.get()); - } -}; +llvm::Expected YAMLModuleTester::Eval(llvm::ArrayRef expr) { + return ::Evaluate(expr, m_module_sp, m_dwarf_unit.get()); +} /// Unfortunately Scalar's operator==() is really picky. static Scalar GetScalar(unsigned bits, uint64_t value, bool sign) { diff --git a/lldb/unittests/SymbolFile/DWARF/DWARFASTParserClangTests.cpp b/lldb/unittests/SymbolFile/DWARF/DWARFASTParserClangTests.cpp --- a/lldb/unittests/SymbolFile/DWARF/DWARFASTParserClangTests.cpp +++ b/lldb/unittests/SymbolFile/DWARF/DWARFASTParserClangTests.cpp @@ -8,19 +8,16 @@ #include "Plugins/SymbolFile/DWARF/DWARFASTParserClang.h" #include "Plugins/SymbolFile/DWARF/DWARFDIE.h" -#include "TestingSupport/SubsystemRAII.h" -#include "lldb/Host/HostInfo.h" +#include "TestingSupport/Symbol/YAMLModuleTester.h" #include "gmock/gmock.h" #include "gtest/gtest.h" using namespace lldb; using namespace lldb_private; -class DWARFASTParserClangTests : public testing::Test { - SubsystemRAII subsystems; -}; - namespace { +class DWARFASTParserClangTests : public testing::Test {}; + class DWARFASTParserClangStub : public DWARFASTParserClang { public: using DWARFASTParserClang::DWARFASTParserClang; @@ -39,14 +36,73 @@ // defining here, causing this test to fail, feel free to delete it. TEST_F(DWARFASTParserClangTests, EnsureAllDIEsInDeclContextHaveBeenParsedParsesOnlyMatchingEntries) { + + /// Auxiliary debug info. + const char *yamldata = + "debug_abbrev:\n" + " - Code: 0x00000001\n" + " Tag: DW_TAG_compile_unit\n" + " Children: DW_CHILDREN_yes\n" + " Attributes:\n" + " - Attribute: DW_AT_language\n" + " Form: DW_FORM_data2\n" + " - Code: 0x00000002\n" + " Tag: DW_TAG_base_type\n" + " Children: DW_CHILDREN_no\n" + " Attributes:\n" + " - Attribute: DW_AT_encoding\n" + " Form: DW_FORM_data1\n" + " - Attribute: DW_AT_byte_size\n" + " Form: DW_FORM_data1\n" + "debug_info:\n" + " - Length:\n" + " TotalLength: 0\n" + " Version: 4\n" + " AbbrOffset: 0\n" + " AddrSize: 8\n" + " Entries:\n" + " - AbbrCode: 0x00000001\n" + " Values:\n" + " - Value: 0x000000000000000C\n" + // 0x0000000e: + " - AbbrCode: 0x00000002\n" + " Values:\n" + " - Value: 0x0000000000000007\n" // DW_ATE_unsigned + " - Value: 0x0000000000000004\n" + // 0x00000011: + " - AbbrCode: 0x00000002\n" + " Values:\n" + " - Value: 0x0000000000000007\n" // DW_ATE_unsigned + " - Value: 0x0000000000000008\n" + // 0x00000014: + " - AbbrCode: 0x00000002\n" + " Values:\n" + " - Value: 0x0000000000000005\n" // DW_ATE_signed + " - Value: 0x0000000000000008\n" + // 0x00000017: + " - AbbrCode: 0x00000002\n" + " Values:\n" + " - Value: 0x0000000000000008\n" // DW_ATE_unsigned_char + " - Value: 0x0000000000000001\n" + "" + " - AbbrCode: 0x00000000\n" + " Values: []\n"; + + YAMLModuleTester t(yamldata, "i386-unknown-linux"); + ASSERT_TRUE((bool)t.GetDwarfUnit()); + TypeSystemClang ast_ctx("dummy ASTContext", HostInfoBase::GetTargetTriple()); DWARFASTParserClangStub ast_parser(ast_ctx); - DWARFUnit *unit = nullptr; - std::vector dies = {DWARFDIE(unit, (DWARFDebugInfoEntry *)1LL), - DWARFDIE(unit, (DWARFDebugInfoEntry *)2LL), - DWARFDIE(unit, (DWARFDebugInfoEntry *)3LL), - DWARFDIE(unit, (DWARFDebugInfoEntry *)4LL)}; + DWARFUnit *unit = t.GetDwarfUnit().get(); + const DWARFDebugInfoEntry *die_first = unit->DIE().GetDIE(); + const DWARFDebugInfoEntry *die_child0 = die_first->GetFirstChild(); + const DWARFDebugInfoEntry *die_child1 = die_child0->GetSibling(); + const DWARFDebugInfoEntry *die_child2 = die_child1->GetSibling(); + const DWARFDebugInfoEntry *die_child3 = die_child2->GetSibling(); + std::vector dies = { + DWARFDIE(unit, die_child0), DWARFDIE(unit, die_child1), + DWARFDIE(unit, die_child2), DWARFDIE(unit, die_child3)}; std::vector decl_ctxs = { (clang::DeclContext *)1LL, (clang::DeclContext *)2LL, (clang::DeclContext *)2LL, (clang::DeclContext *)3LL}; diff --git a/lldb/unittests/TestingSupport/CMakeLists.txt b/lldb/unittests/TestingSupport/CMakeLists.txt --- a/lldb/unittests/TestingSupport/CMakeLists.txt +++ b/lldb/unittests/TestingSupport/CMakeLists.txt @@ -1,6 +1,7 @@ set_property(DIRECTORY PROPERTY EXCLUDE_FROM_ALL ON) add_lldb_library(lldbUtilityHelpers MockTildeExpressionResolver.cpp + Symbol/YAMLModuleTester.cpp TestUtilities.cpp LINK_LIBS diff --git a/lldb/unittests/TestingSupport/Symbol/YAMLModuleTester.h b/lldb/unittests/TestingSupport/Symbol/YAMLModuleTester.h new file mode 100644 --- /dev/null +++ b/lldb/unittests/TestingSupport/Symbol/YAMLModuleTester.h @@ -0,0 +1,89 @@ +//===- YAMLModuleTester.h ---------------------------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLDB_UNITTESTS_TESTINGSUPPORT_SYMBOL_YAMLMODULETESTER_H +#define LLDB_UNITTESTS_TESTINGSUPPORT_SYMBOL_YAMLMODULETESTER_H + +#include "Plugins/SymbolFile/DWARF/DWARFUnit.h" +#include "Plugins/SymbolFile/DWARF/SymbolFileDWARF.h" +#include "TestingSupport/SubsystemRAII.h" +#include "lldb/Core/Module.h" +#include "lldb/Core/Section.h" +#include "lldb/Host/HostInfo.h" +#include "lldb/Symbol/TypeSystemClang.h" +#include "llvm/ObjectYAML/DWARFEmitter.h" + +namespace lldb_private { + +/// A mock module holding an object file parsed from YAML. +class YAMLModule : public lldb_private::Module { +public: + YAMLModule(ArchSpec &arch) : Module(FileSpec("test"), arch) {} + void SetObjectFile(lldb::ObjectFileSP obj_file) { m_objfile_sp = obj_file; } + ObjectFile *GetObjectFile() override { return m_objfile_sp.get(); } +}; + +/// A mock object file that can be parsed from YAML. +class YAMLObjectFile : public lldb_private::ObjectFile { + const lldb::ModuleSP m_module_sp; + llvm::StringMap> &m_section_map; + /// Because there is only one DataExtractor in the ObjectFile + /// interface, all sections are copied into a contiguous buffer. + std::vector m_buffer; + +public: + YAMLObjectFile(const lldb::ModuleSP &module_sp, + llvm::StringMap> &map); + + /// Callback for initializing the module's list of sections. + void CreateSections(SectionList &unified_section_list) override; + + /// \{ + /// Stub methods that aren't needed here. + ConstString GetPluginName() override { return ConstString("YAMLObjectFile"); } + uint32_t GetPluginVersion() override { return 0; } + void Dump(Stream *s) override {} + uint32_t GetAddressByteSize() const override { return 8; } + uint32_t GetDependentModules(FileSpecList &file_list) override { return 0; } + bool IsExecutable() const override { return 0; } + ArchSpec GetArchitecture() override { return {}; } + Symtab *GetSymtab() override { return nullptr; } + bool IsStripped() override { return false; } + UUID GetUUID() override { return {}; } + lldb::ByteOrder GetByteOrder() const override { + return lldb::eByteOrderLittle; + } + bool ParseHeader() override { return false; } + Type CalculateType() override { return {}; } + Strata CalculateStrata() override { return {}; } + /// \} +}; + +/// Helper class that can construct a module from YAML and evaluate +/// DWARF expressions on it. +class YAMLModuleTester { + // SubsystemRAII subsystems; + SubsystemRAII subsystems; + llvm::StringMap> m_sections_map; + lldb::ModuleSP m_module_sp; + lldb::ObjectFileSP m_objfile_sp; + DWARFUnitSP m_dwarf_unit; + std::unique_ptr m_symfile_dwarf; + +public: + /// Parse the debug info sections from the YAML description. + YAMLModuleTester(llvm::StringRef yaml_data, llvm::StringRef triple); + DWARFUnitSP GetDwarfUnit() { return m_dwarf_unit; } + + // Evaluate a raw DWARF expression. + llvm::Expected Eval(llvm::ArrayRef expr); +}; + +} // namespace lldb_private + +#endif // LLDB_UNITTESTS_TESTINGSUPPORT_SYMBOL_YAMLMODULETESTER_H diff --git a/lldb/unittests/TestingSupport/Symbol/YAMLModuleTester.cpp b/lldb/unittests/TestingSupport/Symbol/YAMLModuleTester.cpp new file mode 100644 --- /dev/null +++ b/lldb/unittests/TestingSupport/Symbol/YAMLModuleTester.cpp @@ -0,0 +1,75 @@ +//===- YAMLModuleTester.cpp -------------------------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "TestingSupport/Symbol/YAMLModuleTester.h" + +using namespace lldb_private; + +YAMLObjectFile::YAMLObjectFile(const lldb::ModuleSP &module_sp, + llvm::StringMap> &map) + : ObjectFile(module_sp, &module_sp->GetFileSpec(), /*file_offset*/ 0, + /*length*/ 0, /*data_sp*/ nullptr, /*data_offset*/ 0), + m_module_sp(module_sp), m_section_map(map) {} + +void YAMLObjectFile::CreateSections(SectionList &unified_section_list) { + lldb::offset_t total_bytes = 0; + for (auto &entry : m_section_map) + total_bytes += entry.getValue()->getBufferSize(); + m_buffer.reserve(total_bytes); + m_data = + DataExtractor(m_buffer.data(), total_bytes, lldb::eByteOrderLittle, 4); + + lldb::user_id_t sect_id = 1; + for (auto &entry : m_section_map) { + llvm::StringRef name = entry.getKey(); + lldb::SectionType sect_type = + llvm::StringSwitch(name) + .Case("debug_info", lldb::eSectionTypeDWARFDebugInfo) + .Case("debug_abbrev", lldb::eSectionTypeDWARFDebugAbbrev); + auto &membuf = entry.getValue(); + lldb::addr_t file_vm_addr = 0; + lldb::addr_t vm_size = 0; + lldb::offset_t file_offset = m_buffer.size(); + lldb::offset_t file_size = membuf->getBufferSize(); + m_buffer.resize(file_offset + file_size); + memcpy(m_buffer.data() + file_offset, membuf->getBufferStart(), + file_size); + uint32_t log2align = 0; + uint32_t flags = 0; + auto section_sp = std::make_shared( + m_module_sp, this, sect_id++, ConstString(name), sect_type, + file_vm_addr, vm_size, file_offset, file_size, log2align, flags); + unified_section_list.AddSection(section_sp); + } +} + +YAMLModuleTester::YAMLModuleTester(llvm::StringRef yaml_data, llvm::StringRef triple) { + auto sections_map = llvm::DWARFYAML::EmitDebugSections(yaml_data, true); + if (!sections_map) + return; + m_sections_map = std::move(*sections_map); + ArchSpec arch(triple); + m_module_sp = std::make_shared(arch); + m_objfile_sp = + std::make_shared(m_module_sp, m_sections_map); + static_cast(m_module_sp.get())->SetObjectFile(m_objfile_sp); + + lldb::user_id_t uid = 0; + llvm::StringRef raw_debug_info = m_sections_map["debug_info"]->getBuffer(); + lldb_private::DataExtractor debug_info( + raw_debug_info.data(), raw_debug_info.size(), + m_objfile_sp->GetByteOrder(), m_objfile_sp->GetAddressByteSize()); + lldb::offset_t offset_ptr = 0; + m_symfile_dwarf = std::make_unique(m_objfile_sp, nullptr); + llvm::Expected dwarf_unit = DWARFUnit::extract( + *m_symfile_dwarf, uid, + *static_cast(&debug_info), + DIERef::DebugInfo, &offset_ptr); + if (dwarf_unit) + m_dwarf_unit = dwarf_unit.get(); +} diff --git a/lldb/unittests/TestingSupport/module.modulemap b/lldb/unittests/TestingSupport/module.modulemap --- a/lldb/unittests/TestingSupport/module.modulemap +++ b/lldb/unittests/TestingSupport/module.modulemap @@ -13,4 +13,5 @@ module lldb_TestingSupport_Symbol { requires cplusplus module ClangTestUtils { header "Symbol/ClangTestUtils.h" export * } + module YAMLModuleTester { header "Symbol/YAMLModuleTester.h" export * } }