[lldb/test] Simplify/generalize YAMLModuleTester

The class only supports a single DWARF unit (needed for my new test), and it
reimplements chunks of object and symbol file classes. We can just make it use
the real thing, save some LOC and get the full feature set.

Differential Revision: https://reviews.llvm.org/D90393
This commit is contained in:
Pavel Labath 2020-10-29 14:25:24 +01:00
parent d085697013
commit a895a446bc
5 changed files with 134 additions and 212 deletions

View File

@ -55,7 +55,7 @@ class DWARFExpressionTester : public YAMLModuleTester {
public: public:
using YAMLModuleTester::YAMLModuleTester; using YAMLModuleTester::YAMLModuleTester;
llvm::Expected<Scalar> Eval(llvm::ArrayRef<uint8_t> expr) { llvm::Expected<Scalar> Eval(llvm::ArrayRef<uint8_t> expr) {
return ::Evaluate(expr, m_module_sp, m_dwarf_unit.get()); return ::Evaluate(expr, m_module_sp, m_dwarf_unit);
} }
}; };
@ -77,68 +77,76 @@ TEST(DWARFExpression, DW_OP_pick) {
TEST(DWARFExpression, DW_OP_convert) { TEST(DWARFExpression, DW_OP_convert) {
/// Auxiliary debug info. /// Auxiliary debug info.
const char *yamldata = const char *yamldata = R"(
"debug_abbrev:\n" --- !ELF
" - Table:\n" FileHeader:
" - Code: 0x00000001\n" Class: ELFCLASS64
" Tag: DW_TAG_compile_unit\n" Data: ELFDATA2LSB
" Children: DW_CHILDREN_yes\n" Type: ET_EXEC
" Attributes:\n" Machine: EM_386
" - Attribute: DW_AT_language\n" DWARF:
" Form: DW_FORM_data2\n" debug_abbrev:
" - Code: 0x00000002\n" - Table:
" Tag: DW_TAG_base_type\n" - Code: 0x00000001
" Children: DW_CHILDREN_no\n" Tag: DW_TAG_compile_unit
" Attributes:\n" Children: DW_CHILDREN_yes
" - Attribute: DW_AT_encoding\n" Attributes:
" Form: DW_FORM_data1\n" - Attribute: DW_AT_language
" - Attribute: DW_AT_byte_size\n" Form: DW_FORM_data2
" Form: DW_FORM_data1\n" - Code: 0x00000002
"debug_info:\n" Tag: DW_TAG_base_type
" - Version: 4\n" Children: DW_CHILDREN_no
" AddrSize: 8\n" Attributes:
" Entries:\n" - Attribute: DW_AT_encoding
" - AbbrCode: 0x00000001\n" Form: DW_FORM_data1
" Values:\n" - Attribute: DW_AT_byte_size
" - Value: 0x000000000000000C\n" Form: DW_FORM_data1
// 0x0000000e: debug_info:
" - AbbrCode: 0x00000002\n" - Version: 4
" Values:\n" AddrSize: 8
" - Value: 0x0000000000000007\n" // DW_ATE_unsigned Entries:
" - Value: 0x0000000000000004\n" - AbbrCode: 0x00000001
// 0x00000011: Values:
" - AbbrCode: 0x00000002\n" - Value: 0x000000000000000C
" Values:\n" # 0x0000000e:
" - Value: 0x0000000000000007\n" // DW_ATE_unsigned - AbbrCode: 0x00000002
" - Value: 0x0000000000000008\n" Values:
// 0x00000014: - Value: 0x0000000000000007 # DW_ATE_unsigned
" - AbbrCode: 0x00000002\n" - Value: 0x0000000000000004
" Values:\n" # 0x00000011:
" - Value: 0x0000000000000005\n" // DW_ATE_signed - AbbrCode: 0x00000002
" - Value: 0x0000000000000008\n" Values:
// 0x00000017: - Value: 0x0000000000000007 # DW_ATE_unsigned
" - AbbrCode: 0x00000002\n" - Value: 0x0000000000000008
" Values:\n" # 0x00000014:
" - Value: 0x0000000000000008\n" // DW_ATE_unsigned_char - AbbrCode: 0x00000002
" - Value: 0x0000000000000001\n" Values:
// 0x0000001a: - Value: 0x0000000000000005 # DW_ATE_signed
" - AbbrCode: 0x00000002\n" - Value: 0x0000000000000008
" Values:\n" # 0x00000017:
" - Value: 0x0000000000000006\n" // DW_ATE_signed_char - AbbrCode: 0x00000002
" - Value: 0x0000000000000001\n" Values:
// 0x0000001d: - Value: 0x0000000000000008 # DW_ATE_unsigned_char
" - AbbrCode: 0x00000002\n" - Value: 0x0000000000000001
" Values:\n" # 0x0000001a:
" - Value: 0x000000000000000b\n" // DW_ATE_numeric_string - AbbrCode: 0x00000002
" - Value: 0x0000000000000001\n" Values:
" - AbbrCode: 0x00000000\n"; - Value: 0x0000000000000006 # DW_ATE_signed_char
- Value: 0x0000000000000001
# 0x0000001d:
- AbbrCode: 0x00000002
Values:
- Value: 0x000000000000000b # DW_ATE_numeric_string
- Value: 0x0000000000000001
- AbbrCode: 0x00000000
)";
uint8_t offs_uint32_t = 0x0000000e; uint8_t offs_uint32_t = 0x0000000e;
uint8_t offs_uint64_t = 0x00000011; uint8_t offs_uint64_t = 0x00000011;
uint8_t offs_sint64_t = 0x00000014; uint8_t offs_sint64_t = 0x00000014;
uint8_t offs_uchar = 0x00000017; uint8_t offs_uchar = 0x00000017;
uint8_t offs_schar = 0x0000001a; uint8_t offs_schar = 0x0000001a;
DWARFExpressionTester t(yamldata, "i386-unknown-linux"); DWARFExpressionTester t(yamldata);
ASSERT_TRUE((bool)t.GetDwarfUnit()); ASSERT_TRUE((bool)t.GetDwarfUnit());
// Constant is given as little-endian. // Constant is given as little-endian.
@ -188,7 +196,7 @@ TEST(DWARFExpression, DW_OP_convert) {
// No Module. // No Module.
EXPECT_THAT_ERROR(Evaluate({DW_OP_const1s, 'X', DW_OP_convert, 0x00}, nullptr, EXPECT_THAT_ERROR(Evaluate({DW_OP_const1s, 'X', DW_OP_convert, 0x00}, nullptr,
t.GetDwarfUnit().get()) t.GetDwarfUnit())
.takeError(), .takeError(),
llvm::Failed()); llvm::Failed());

View File

@ -39,59 +39,63 @@ TEST_F(DWARFASTParserClangTests,
EnsureAllDIEsInDeclContextHaveBeenParsedParsesOnlyMatchingEntries) { EnsureAllDIEsInDeclContextHaveBeenParsedParsesOnlyMatchingEntries) {
/// Auxiliary debug info. /// Auxiliary debug info.
const char *yamldata = const char *yamldata = R"(
"debug_abbrev:\n" --- !ELF
" - Table:\n" FileHeader:
" - Code: 0x00000001\n" Class: ELFCLASS64
" Tag: DW_TAG_compile_unit\n" Data: ELFDATA2LSB
" Children: DW_CHILDREN_yes\n" Type: ET_EXEC
" Attributes:\n" Machine: EM_386
" - Attribute: DW_AT_language\n" DWARF:
" Form: DW_FORM_data2\n" debug_abbrev:
" - Code: 0x00000002\n" - Table:
" Tag: DW_TAG_base_type\n" - Code: 0x00000001
" Children: DW_CHILDREN_no\n" Tag: DW_TAG_compile_unit
" Attributes:\n" Children: DW_CHILDREN_yes
" - Attribute: DW_AT_encoding\n" Attributes:
" Form: DW_FORM_data1\n" - Attribute: DW_AT_language
" - Attribute: DW_AT_byte_size\n" Form: DW_FORM_data2
" Form: DW_FORM_data1\n" - Code: 0x00000002
"debug_info:\n" Tag: DW_TAG_base_type
" - Version: 4\n" Children: DW_CHILDREN_no
" AddrSize: 8\n" Attributes:
" Entries:\n" - Attribute: DW_AT_encoding
" - AbbrCode: 0x00000001\n" Form: DW_FORM_data1
" Values:\n" - Attribute: DW_AT_byte_size
" - Value: 0x000000000000000C\n" Form: DW_FORM_data1
// 0x0000000e: debug_info:
" - AbbrCode: 0x00000002\n" - Version: 4
" Values:\n" AddrSize: 8
" - Value: 0x0000000000000007\n" // DW_ATE_unsigned Entries:
" - Value: 0x0000000000000004\n" - AbbrCode: 0x00000001
// 0x00000011: Values:
" - AbbrCode: 0x00000002\n" - Value: 0x000000000000000C
" Values:\n" - AbbrCode: 0x00000002
" - Value: 0x0000000000000007\n" // DW_ATE_unsigned Values:
" - Value: 0x0000000000000008\n" - Value: 0x0000000000000007 # DW_ATE_unsigned
// 0x00000014: - Value: 0x0000000000000004
" - AbbrCode: 0x00000002\n" - AbbrCode: 0x00000002
" Values:\n" Values:
" - Value: 0x0000000000000005\n" // DW_ATE_signed - Value: 0x0000000000000007 # DW_ATE_unsigned
" - Value: 0x0000000000000008\n" - Value: 0x0000000000000008
// 0x00000017: - AbbrCode: 0x00000002
" - AbbrCode: 0x00000002\n" Values:
" Values:\n" - Value: 0x0000000000000005 # DW_ATE_signed
" - Value: 0x0000000000000008\n" // DW_ATE_unsigned_char - Value: 0x0000000000000008
" - Value: 0x0000000000000001\n" - AbbrCode: 0x00000002
" - AbbrCode: 0x00000000\n"; Values:
- Value: 0x0000000000000008 # DW_ATE_unsigned_char
- Value: 0x0000000000000001
- AbbrCode: 0x00000000
)";
YAMLModuleTester t(yamldata, "i386-unknown-linux"); YAMLModuleTester t(yamldata);
ASSERT_TRUE((bool)t.GetDwarfUnit()); ASSERT_TRUE((bool)t.GetDwarfUnit());
TypeSystemClang ast_ctx("dummy ASTContext", HostInfoBase::GetTargetTriple()); TypeSystemClang ast_ctx("dummy ASTContext", HostInfoBase::GetTargetTriple());
DWARFASTParserClangStub ast_parser(ast_ctx); DWARFASTParserClangStub ast_parser(ast_ctx);
DWARFUnit *unit = t.GetDwarfUnit().get(); DWARFUnit *unit = t.GetDwarfUnit();
const DWARFDebugInfoEntry *die_first = unit->DIE().GetDIE(); const DWARFDebugInfoEntry *die_first = unit->DIE().GetDIE();
const DWARFDebugInfoEntry *die_child0 = die_first->GetFirstChild(); const DWARFDebugInfoEntry *die_child0 = die_first->GetFirstChild();
const DWARFDebugInfoEntry *die_child1 = die_child0->GetSibling(); const DWARFDebugInfoEntry *die_child1 = die_child0->GetSibling();

View File

@ -6,9 +6,11 @@ add_lldb_library(lldbSymbolHelpers
lldbCore lldbCore
lldbHost lldbHost
lldbPluginExpressionParserClang lldbPluginExpressionParserClang
lldbPluginObjectFileELF
lldbPluginSymbolFileDWARF lldbPluginSymbolFileDWARF
lldbPluginTypeSystemClang lldbPluginTypeSystemClang
lldbUtilityHelpers lldbUtilityHelpers
LLVMTestingSupport
LINK_COMPONENTS LINK_COMPONENTS
ObjectYAML ObjectYAML

View File

@ -7,112 +7,20 @@
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#include "TestingSupport/Symbol/YAMLModuleTester.h" #include "TestingSupport/Symbol/YAMLModuleTester.h"
#include "Plugins/SymbolFile/DWARF/DWARFDebugInfo.h"
#include "Plugins/TypeSystem/Clang/TypeSystemClang.h" #include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
#include "TestingSupport/TestUtilities.h"
#include "lldb/Core/Section.h" #include "lldb/Core/Section.h"
#include "llvm/ObjectYAML/DWARFEmitter.h" #include "llvm/ObjectYAML/DWARFEmitter.h"
using namespace lldb_private; using namespace lldb_private;
/// A mock module holding an object file parsed from YAML. YAMLModuleTester::YAMLModuleTester(llvm::StringRef yaml_data) {
class YAMLModule : public lldb_private::Module { llvm::Expected<TestFile> File = TestFile::fromYaml(yaml_data);
public: EXPECT_THAT_EXPECTED(File, llvm::Succeeded());
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. m_module_sp = std::make_shared<Module>(File->moduleSpec());
class YAMLObjectFile : public lldb_private::ObjectFile { auto &symfile = *llvm::cast<SymbolFileDWARF>(m_module_sp->GetSymbolFile());
const lldb::ModuleSP m_module_sp;
llvm::StringMap<std::unique_ptr<llvm::MemoryBuffer>> &m_section_map;
/// Because there is only one DataExtractor in the ObjectFile
/// interface, all sections are copied into a contiguous buffer.
std::vector<char> m_buffer;
public: m_dwarf_unit = symfile.DebugInfo().GetUnitAtIndex(0);
YAMLObjectFile(const lldb::ModuleSP &module_sp,
llvm::StringMap<std::unique_ptr<llvm::MemoryBuffer>> &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<lldb::SectionType>(name)
.Case("debug_info", lldb::eSectionTypeDWARFDebugInfo)
.Case("debug_abbrev", lldb::eSectionTypeDWARFDebugAbbrev)
.Case("debug_str", lldb::eSectionTypeDWARFDebugStr);
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<lldb_private::Section>(
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 {}; }
/// \}
};
YAMLModuleTester::YAMLModuleTester(llvm::StringRef yaml_data,
llvm::StringRef triple) {
auto sections_map = llvm::DWARFYAML::emitDebugSections(yaml_data);
if (!sections_map)
return;
m_sections_map = std::move(*sections_map);
ArchSpec arch(triple);
m_module_sp = std::make_shared<YAMLModule>(arch);
m_objfile_sp = std::make_shared<YAMLObjectFile>(m_module_sp, m_sections_map);
static_cast<YAMLModule *>(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<SymbolFileDWARF>(m_objfile_sp, nullptr);
llvm::Expected<DWARFUnitSP> dwarf_unit = DWARFUnit::extract(
*m_symfile_dwarf, uid,
*static_cast<lldb_private::DWARFDataExtractor *>(&debug_info),
DIERef::DebugInfo, &offset_ptr, nullptr);
if (dwarf_unit)
m_dwarf_unit = dwarf_unit.get();
} }

View File

@ -9,6 +9,7 @@
#ifndef LLDB_UNITTESTS_TESTINGSUPPORT_SYMBOL_YAMLMODULETESTER_H #ifndef LLDB_UNITTESTS_TESTINGSUPPORT_SYMBOL_YAMLMODULETESTER_H
#define LLDB_UNITTESTS_TESTINGSUPPORT_SYMBOL_YAMLMODULETESTER_H #define LLDB_UNITTESTS_TESTINGSUPPORT_SYMBOL_YAMLMODULETESTER_H
#include "Plugins/ObjectFile/ELF/ObjectFileELF.h"
#include "Plugins/SymbolFile/DWARF/DWARFUnit.h" #include "Plugins/SymbolFile/DWARF/DWARFUnit.h"
#include "Plugins/SymbolFile/DWARF/SymbolFileDWARF.h" #include "Plugins/SymbolFile/DWARF/SymbolFileDWARF.h"
#include "Plugins/TypeSystem/Clang/TypeSystemClang.h" #include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
@ -22,17 +23,16 @@ namespace lldb_private {
/// DWARF expressions on it. /// DWARF expressions on it.
class YAMLModuleTester { class YAMLModuleTester {
protected: protected:
SubsystemRAII<FileSystem, HostInfo, TypeSystemClang> subsystems; SubsystemRAII<FileSystem, HostInfo, TypeSystemClang, ObjectFileELF,
llvm::StringMap<std::unique_ptr<llvm::MemoryBuffer>> m_sections_map; SymbolFileDWARF>
subsystems;
lldb::ModuleSP m_module_sp; lldb::ModuleSP m_module_sp;
lldb::ObjectFileSP m_objfile_sp; DWARFUnit *m_dwarf_unit;
DWARFUnitSP m_dwarf_unit;
std::unique_ptr<SymbolFileDWARF> m_symfile_dwarf;
public: public:
/// Parse the debug info sections from the YAML description. /// Parse the debug info sections from the YAML description.
YAMLModuleTester(llvm::StringRef yaml_data, llvm::StringRef triple); YAMLModuleTester(llvm::StringRef yaml_data);
DWARFUnitSP GetDwarfUnit() const { return m_dwarf_unit; } DWARFUnit *GetDwarfUnit() const { return m_dwarf_unit; }
lldb::ModuleSP GetModule() const { return m_module_sp; } lldb::ModuleSP GetModule() const { return m_module_sp; }
}; };