Skip to content

Commit a895a44

Browse files
committed
[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
1 parent d085697 commit a895a44

File tree

5 files changed

+134
-212
lines changed

5 files changed

+134
-212
lines changed

lldb/unittests/Expression/DWARFExpressionTest.cpp

Lines changed: 66 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class DWARFExpressionTester : public YAMLModuleTester {
5555
public:
5656
using YAMLModuleTester::YAMLModuleTester;
5757
llvm::Expected<Scalar> Eval(llvm::ArrayRef<uint8_t> expr) {
58-
return ::Evaluate(expr, m_module_sp, m_dwarf_unit.get());
58+
return ::Evaluate(expr, m_module_sp, m_dwarf_unit);
5959
}
6060
};
6161

@@ -77,68 +77,76 @@ TEST(DWARFExpression, DW_OP_pick) {
7777

7878
TEST(DWARFExpression, DW_OP_convert) {
7979
/// Auxiliary debug info.
80-
const char *yamldata =
81-
"debug_abbrev:\n"
82-
" - Table:\n"
83-
" - Code: 0x00000001\n"
84-
" Tag: DW_TAG_compile_unit\n"
85-
" Children: DW_CHILDREN_yes\n"
86-
" Attributes:\n"
87-
" - Attribute: DW_AT_language\n"
88-
" Form: DW_FORM_data2\n"
89-
" - Code: 0x00000002\n"
90-
" Tag: DW_TAG_base_type\n"
91-
" Children: DW_CHILDREN_no\n"
92-
" Attributes:\n"
93-
" - Attribute: DW_AT_encoding\n"
94-
" Form: DW_FORM_data1\n"
95-
" - Attribute: DW_AT_byte_size\n"
96-
" Form: DW_FORM_data1\n"
97-
"debug_info:\n"
98-
" - Version: 4\n"
99-
" AddrSize: 8\n"
100-
" Entries:\n"
101-
" - AbbrCode: 0x00000001\n"
102-
" Values:\n"
103-
" - Value: 0x000000000000000C\n"
104-
// 0x0000000e:
105-
" - AbbrCode: 0x00000002\n"
106-
" Values:\n"
107-
" - Value: 0x0000000000000007\n" // DW_ATE_unsigned
108-
" - Value: 0x0000000000000004\n"
109-
// 0x00000011:
110-
" - AbbrCode: 0x00000002\n"
111-
" Values:\n"
112-
" - Value: 0x0000000000000007\n" // DW_ATE_unsigned
113-
" - Value: 0x0000000000000008\n"
114-
// 0x00000014:
115-
" - AbbrCode: 0x00000002\n"
116-
" Values:\n"
117-
" - Value: 0x0000000000000005\n" // DW_ATE_signed
118-
" - Value: 0x0000000000000008\n"
119-
// 0x00000017:
120-
" - AbbrCode: 0x00000002\n"
121-
" Values:\n"
122-
" - Value: 0x0000000000000008\n" // DW_ATE_unsigned_char
123-
" - Value: 0x0000000000000001\n"
124-
// 0x0000001a:
125-
" - AbbrCode: 0x00000002\n"
126-
" Values:\n"
127-
" - Value: 0x0000000000000006\n" // DW_ATE_signed_char
128-
" - Value: 0x0000000000000001\n"
129-
// 0x0000001d:
130-
" - AbbrCode: 0x00000002\n"
131-
" Values:\n"
132-
" - Value: 0x000000000000000b\n" // DW_ATE_numeric_string
133-
" - Value: 0x0000000000000001\n"
134-
" - AbbrCode: 0x00000000\n";
80+
const char *yamldata = R"(
81+
--- !ELF
82+
FileHeader:
83+
Class: ELFCLASS64
84+
Data: ELFDATA2LSB
85+
Type: ET_EXEC
86+
Machine: EM_386
87+
DWARF:
88+
debug_abbrev:
89+
- Table:
90+
- Code: 0x00000001
91+
Tag: DW_TAG_compile_unit
92+
Children: DW_CHILDREN_yes
93+
Attributes:
94+
- Attribute: DW_AT_language
95+
Form: DW_FORM_data2
96+
- Code: 0x00000002
97+
Tag: DW_TAG_base_type
98+
Children: DW_CHILDREN_no
99+
Attributes:
100+
- Attribute: DW_AT_encoding
101+
Form: DW_FORM_data1
102+
- Attribute: DW_AT_byte_size
103+
Form: DW_FORM_data1
104+
debug_info:
105+
- Version: 4
106+
AddrSize: 8
107+
Entries:
108+
- AbbrCode: 0x00000001
109+
Values:
110+
- Value: 0x000000000000000C
111+
# 0x0000000e:
112+
- AbbrCode: 0x00000002
113+
Values:
114+
- Value: 0x0000000000000007 # DW_ATE_unsigned
115+
- Value: 0x0000000000000004
116+
# 0x00000011:
117+
- AbbrCode: 0x00000002
118+
Values:
119+
- Value: 0x0000000000000007 # DW_ATE_unsigned
120+
- Value: 0x0000000000000008
121+
# 0x00000014:
122+
- AbbrCode: 0x00000002
123+
Values:
124+
- Value: 0x0000000000000005 # DW_ATE_signed
125+
- Value: 0x0000000000000008
126+
# 0x00000017:
127+
- AbbrCode: 0x00000002
128+
Values:
129+
- Value: 0x0000000000000008 # DW_ATE_unsigned_char
130+
- Value: 0x0000000000000001
131+
# 0x0000001a:
132+
- AbbrCode: 0x00000002
133+
Values:
134+
- Value: 0x0000000000000006 # DW_ATE_signed_char
135+
- Value: 0x0000000000000001
136+
# 0x0000001d:
137+
- AbbrCode: 0x00000002
138+
Values:
139+
- Value: 0x000000000000000b # DW_ATE_numeric_string
140+
- Value: 0x0000000000000001
141+
- AbbrCode: 0x00000000
142+
)";
135143
uint8_t offs_uint32_t = 0x0000000e;
136144
uint8_t offs_uint64_t = 0x00000011;
137145
uint8_t offs_sint64_t = 0x00000014;
138146
uint8_t offs_uchar = 0x00000017;
139147
uint8_t offs_schar = 0x0000001a;
140148

141-
DWARFExpressionTester t(yamldata, "i386-unknown-linux");
149+
DWARFExpressionTester t(yamldata);
142150
ASSERT_TRUE((bool)t.GetDwarfUnit());
143151

144152
// Constant is given as little-endian.
@@ -188,7 +196,7 @@ TEST(DWARFExpression, DW_OP_convert) {
188196

189197
// No Module.
190198
EXPECT_THAT_ERROR(Evaluate({DW_OP_const1s, 'X', DW_OP_convert, 0x00}, nullptr,
191-
t.GetDwarfUnit().get())
199+
t.GetDwarfUnit())
192200
.takeError(),
193201
llvm::Failed());
194202

lldb/unittests/SymbolFile/DWARF/DWARFASTParserClangTests.cpp

Lines changed: 51 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -39,59 +39,63 @@ TEST_F(DWARFASTParserClangTests,
3939
EnsureAllDIEsInDeclContextHaveBeenParsedParsesOnlyMatchingEntries) {
4040

4141
/// Auxiliary debug info.
42-
const char *yamldata =
43-
"debug_abbrev:\n"
44-
" - Table:\n"
45-
" - Code: 0x00000001\n"
46-
" Tag: DW_TAG_compile_unit\n"
47-
" Children: DW_CHILDREN_yes\n"
48-
" Attributes:\n"
49-
" - Attribute: DW_AT_language\n"
50-
" Form: DW_FORM_data2\n"
51-
" - Code: 0x00000002\n"
52-
" Tag: DW_TAG_base_type\n"
53-
" Children: DW_CHILDREN_no\n"
54-
" Attributes:\n"
55-
" - Attribute: DW_AT_encoding\n"
56-
" Form: DW_FORM_data1\n"
57-
" - Attribute: DW_AT_byte_size\n"
58-
" Form: DW_FORM_data1\n"
59-
"debug_info:\n"
60-
" - Version: 4\n"
61-
" AddrSize: 8\n"
62-
" Entries:\n"
63-
" - AbbrCode: 0x00000001\n"
64-
" Values:\n"
65-
" - Value: 0x000000000000000C\n"
66-
// 0x0000000e:
67-
" - AbbrCode: 0x00000002\n"
68-
" Values:\n"
69-
" - Value: 0x0000000000000007\n" // DW_ATE_unsigned
70-
" - Value: 0x0000000000000004\n"
71-
// 0x00000011:
72-
" - AbbrCode: 0x00000002\n"
73-
" Values:\n"
74-
" - Value: 0x0000000000000007\n" // DW_ATE_unsigned
75-
" - Value: 0x0000000000000008\n"
76-
// 0x00000014:
77-
" - AbbrCode: 0x00000002\n"
78-
" Values:\n"
79-
" - Value: 0x0000000000000005\n" // DW_ATE_signed
80-
" - Value: 0x0000000000000008\n"
81-
// 0x00000017:
82-
" - AbbrCode: 0x00000002\n"
83-
" Values:\n"
84-
" - Value: 0x0000000000000008\n" // DW_ATE_unsigned_char
85-
" - Value: 0x0000000000000001\n"
86-
" - AbbrCode: 0x00000000\n";
42+
const char *yamldata = R"(
43+
--- !ELF
44+
FileHeader:
45+
Class: ELFCLASS64
46+
Data: ELFDATA2LSB
47+
Type: ET_EXEC
48+
Machine: EM_386
49+
DWARF:
50+
debug_abbrev:
51+
- Table:
52+
- Code: 0x00000001
53+
Tag: DW_TAG_compile_unit
54+
Children: DW_CHILDREN_yes
55+
Attributes:
56+
- Attribute: DW_AT_language
57+
Form: DW_FORM_data2
58+
- Code: 0x00000002
59+
Tag: DW_TAG_base_type
60+
Children: DW_CHILDREN_no
61+
Attributes:
62+
- Attribute: DW_AT_encoding
63+
Form: DW_FORM_data1
64+
- Attribute: DW_AT_byte_size
65+
Form: DW_FORM_data1
66+
debug_info:
67+
- Version: 4
68+
AddrSize: 8
69+
Entries:
70+
- AbbrCode: 0x00000001
71+
Values:
72+
- Value: 0x000000000000000C
73+
- AbbrCode: 0x00000002
74+
Values:
75+
- Value: 0x0000000000000007 # DW_ATE_unsigned
76+
- Value: 0x0000000000000004
77+
- AbbrCode: 0x00000002
78+
Values:
79+
- Value: 0x0000000000000007 # DW_ATE_unsigned
80+
- Value: 0x0000000000000008
81+
- AbbrCode: 0x00000002
82+
Values:
83+
- Value: 0x0000000000000005 # DW_ATE_signed
84+
- Value: 0x0000000000000008
85+
- AbbrCode: 0x00000002
86+
Values:
87+
- Value: 0x0000000000000008 # DW_ATE_unsigned_char
88+
- Value: 0x0000000000000001
89+
- AbbrCode: 0x00000000
90+
)";
8791

88-
YAMLModuleTester t(yamldata, "i386-unknown-linux");
92+
YAMLModuleTester t(yamldata);
8993
ASSERT_TRUE((bool)t.GetDwarfUnit());
9094

9195
TypeSystemClang ast_ctx("dummy ASTContext", HostInfoBase::GetTargetTriple());
9296
DWARFASTParserClangStub ast_parser(ast_ctx);
9397

94-
DWARFUnit *unit = t.GetDwarfUnit().get();
98+
DWARFUnit *unit = t.GetDwarfUnit();
9599
const DWARFDebugInfoEntry *die_first = unit->DIE().GetDIE();
96100
const DWARFDebugInfoEntry *die_child0 = die_first->GetFirstChild();
97101
const DWARFDebugInfoEntry *die_child1 = die_child0->GetSibling();

lldb/unittests/TestingSupport/Symbol/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ add_lldb_library(lldbSymbolHelpers
66
lldbCore
77
lldbHost
88
lldbPluginExpressionParserClang
9+
lldbPluginObjectFileELF
910
lldbPluginSymbolFileDWARF
1011
lldbPluginTypeSystemClang
1112
lldbUtilityHelpers
13+
LLVMTestingSupport
1214

1315
LINK_COMPONENTS
1416
ObjectYAML

lldb/unittests/TestingSupport/Symbol/YAMLModuleTester.cpp

Lines changed: 8 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -7,112 +7,20 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "TestingSupport/Symbol/YAMLModuleTester.h"
10+
#include "Plugins/SymbolFile/DWARF/DWARFDebugInfo.h"
1011
#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
12+
#include "TestingSupport/TestUtilities.h"
1113
#include "lldb/Core/Section.h"
1214
#include "llvm/ObjectYAML/DWARFEmitter.h"
1315

1416
using namespace lldb_private;
1517

16-
/// A mock module holding an object file parsed from YAML.
17-
class YAMLModule : public lldb_private::Module {
18-
public:
19-
YAMLModule(ArchSpec &arch) : Module(FileSpec("test"), arch) {}
20-
void SetObjectFile(lldb::ObjectFileSP obj_file) { m_objfile_sp = obj_file; }
21-
ObjectFile *GetObjectFile() override { return m_objfile_sp.get(); }
22-
};
18+
YAMLModuleTester::YAMLModuleTester(llvm::StringRef yaml_data) {
19+
llvm::Expected<TestFile> File = TestFile::fromYaml(yaml_data);
20+
EXPECT_THAT_EXPECTED(File, llvm::Succeeded());
2321

24-
/// A mock object file that can be parsed from YAML.
25-
class YAMLObjectFile : public lldb_private::ObjectFile {
26-
const lldb::ModuleSP m_module_sp;
27-
llvm::StringMap<std::unique_ptr<llvm::MemoryBuffer>> &m_section_map;
28-
/// Because there is only one DataExtractor in the ObjectFile
29-
/// interface, all sections are copied into a contiguous buffer.
30-
std::vector<char> m_buffer;
22+
m_module_sp = std::make_shared<Module>(File->moduleSpec());
23+
auto &symfile = *llvm::cast<SymbolFileDWARF>(m_module_sp->GetSymbolFile());
3124

32-
public:
33-
YAMLObjectFile(const lldb::ModuleSP &module_sp,
34-
llvm::StringMap<std::unique_ptr<llvm::MemoryBuffer>> &map)
35-
: ObjectFile(module_sp, &module_sp->GetFileSpec(), /*file_offset*/ 0,
36-
/*length*/ 0, /*data_sp*/ nullptr, /*data_offset*/ 0),
37-
m_module_sp(module_sp), m_section_map(map) {}
38-
39-
/// Callback for initializing the module's list of sections.
40-
void CreateSections(SectionList &unified_section_list) override {
41-
lldb::offset_t total_bytes = 0;
42-
for (auto &entry : m_section_map)
43-
total_bytes += entry.getValue()->getBufferSize();
44-
m_buffer.reserve(total_bytes);
45-
m_data =
46-
DataExtractor(m_buffer.data(), total_bytes, lldb::eByteOrderLittle, 4);
47-
48-
lldb::user_id_t sect_id = 1;
49-
for (auto &entry : m_section_map) {
50-
llvm::StringRef name = entry.getKey();
51-
lldb::SectionType sect_type =
52-
llvm::StringSwitch<lldb::SectionType>(name)
53-
.Case("debug_info", lldb::eSectionTypeDWARFDebugInfo)
54-
.Case("debug_abbrev", lldb::eSectionTypeDWARFDebugAbbrev)
55-
.Case("debug_str", lldb::eSectionTypeDWARFDebugStr);
56-
auto &membuf = entry.getValue();
57-
lldb::addr_t file_vm_addr = 0;
58-
lldb::addr_t vm_size = 0;
59-
lldb::offset_t file_offset = m_buffer.size();
60-
lldb::offset_t file_size = membuf->getBufferSize();
61-
m_buffer.resize(file_offset + file_size);
62-
memcpy(m_buffer.data() + file_offset, membuf->getBufferStart(),
63-
file_size);
64-
uint32_t log2align = 0;
65-
uint32_t flags = 0;
66-
auto section_sp = std::make_shared<lldb_private::Section>(
67-
m_module_sp, this, sect_id++, ConstString(name), sect_type,
68-
file_vm_addr, vm_size, file_offset, file_size, log2align, flags);
69-
unified_section_list.AddSection(section_sp);
70-
}
71-
}
72-
73-
/// \{
74-
/// Stub methods that aren't needed here.
75-
ConstString GetPluginName() override { return ConstString("YAMLObjectFile"); }
76-
uint32_t GetPluginVersion() override { return 0; }
77-
void Dump(Stream *s) override {}
78-
uint32_t GetAddressByteSize() const override { return 8; }
79-
uint32_t GetDependentModules(FileSpecList &file_list) override { return 0; }
80-
bool IsExecutable() const override { return 0; }
81-
ArchSpec GetArchitecture() override { return {}; }
82-
Symtab *GetSymtab() override { return nullptr; }
83-
bool IsStripped() override { return false; }
84-
UUID GetUUID() override { return {}; }
85-
lldb::ByteOrder GetByteOrder() const override {
86-
return lldb::eByteOrderLittle;
87-
}
88-
bool ParseHeader() override { return false; }
89-
Type CalculateType() override { return {}; }
90-
Strata CalculateStrata() override { return {}; }
91-
/// \}
92-
};
93-
94-
YAMLModuleTester::YAMLModuleTester(llvm::StringRef yaml_data,
95-
llvm::StringRef triple) {
96-
auto sections_map = llvm::DWARFYAML::emitDebugSections(yaml_data);
97-
if (!sections_map)
98-
return;
99-
m_sections_map = std::move(*sections_map);
100-
ArchSpec arch(triple);
101-
m_module_sp = std::make_shared<YAMLModule>(arch);
102-
m_objfile_sp = std::make_shared<YAMLObjectFile>(m_module_sp, m_sections_map);
103-
static_cast<YAMLModule *>(m_module_sp.get())->SetObjectFile(m_objfile_sp);
104-
105-
lldb::user_id_t uid = 0;
106-
llvm::StringRef raw_debug_info = m_sections_map["debug_info"]->getBuffer();
107-
lldb_private::DataExtractor debug_info(
108-
raw_debug_info.data(), raw_debug_info.size(),
109-
m_objfile_sp->GetByteOrder(), m_objfile_sp->GetAddressByteSize());
110-
lldb::offset_t offset_ptr = 0;
111-
m_symfile_dwarf = std::make_unique<SymbolFileDWARF>(m_objfile_sp, nullptr);
112-
llvm::Expected<DWARFUnitSP> dwarf_unit = DWARFUnit::extract(
113-
*m_symfile_dwarf, uid,
114-
*static_cast<lldb_private::DWARFDataExtractor *>(&debug_info),
115-
DIERef::DebugInfo, &offset_ptr, nullptr);
116-
if (dwarf_unit)
117-
m_dwarf_unit = dwarf_unit.get();
25+
m_dwarf_unit = symfile.DebugInfo().GetUnitAtIndex(0);
11826
}

0 commit comments

Comments
 (0)