Debian Codegraph @

llvm/unittests/DebugInfo/LogicalView/DWARFReaderTest.cpp

raw
1 //===- llvm/unittest/DebugInfo/LogicalView/DWARFReaderTest.cpp ------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/DebugInfo/LogicalView/Core/LVCompare.h"
10 #include "llvm/DebugInfo/LogicalView/Core/LVLine.h"
11 #include "llvm/DebugInfo/LogicalView/Core/LVScope.h"
12 #include "llvm/DebugInfo/LogicalView/Core/LVSymbol.h"
13 #include "llvm/DebugInfo/LogicalView/Core/LVType.h"
14 #include "llvm/DebugInfo/LogicalView/LVReaderHandler.h"
15 #include "llvm/MC/TargetRegistry.h"
16 #include "llvm/Support/COM.h"
17 #include "llvm/Support/InitLLVM.h"
18 #include "llvm/Support/ScopedPrinter.h"
19 #include "llvm/Support/TargetSelect.h"
20 #include "llvm/Support/ToolOutputFile.h"
21 #include "llvm/Testing/Support/Error.h"
22
23 #include "gtest/gtest.h"
24
25 using namespace llvm;
26 using namespace llvm::logicalview;
27
28 extern const char *TestMainArgv0;
29
30 namespace {
31
32 const char *DwarfClang = "test-dwarf-clang.o";
33 // Two compile units: one declares `extern int foo_printf(const char *, ...);`
34 // and another one that defines the function.
35 const char *DwarfClangUnspecParams = "test-dwarf-clang-unspec-params.elf";
36 const char *DwarfClangModule = "test-dwarf-clang-module.o";
37 const char *DwarfGcc = "test-dwarf-gcc.o";
38
39 // Helper function to get the first compile unit.
40 LVScopeCompileUnit *getFirstCompileUnit(LVScopeRoot *Root) {
41 EXPECT_NE(Root, nullptr);
42 const LVScopes *CompileUnits = Root->getScopes();
43 EXPECT_NE(CompileUnits, nullptr);
44 EXPECT_GT(CompileUnits->size(), 0u);
45
46 LVScopes::const_iterator Iter = CompileUnits->begin();
47 EXPECT_NE(Iter, nullptr);
48 LVScopeCompileUnit *CompileUnit = static_cast<LVScopeCompileUnit *>(*Iter);
49 EXPECT_NE(CompileUnit, nullptr);
50 return CompileUnit;
51 }
52
53 // Helper function to create a reader.
54 std::unique_ptr<LVReader> createReader(LVReaderHandler &ReaderHandler,
55 SmallString<128> &InputsDir,
56 StringRef Filename) {
57 SmallString<128> ObjectName(InputsDir);
58 llvm::sys::path::append(ObjectName, Filename);
59
60 Expected<std::unique_ptr<LVReader>> ReaderOrErr =
61 ReaderHandler.createReader(std::string(ObjectName));
62 EXPECT_THAT_EXPECTED(ReaderOrErr, Succeeded());
63 std::unique_ptr<LVReader> Reader = std::move(*ReaderOrErr);
64 EXPECT_NE(Reader, nullptr);
65 return Reader;
66 }
67
68 // Check the logical elements basic properties.
69 void checkElementProperties(LVReader *Reader) {
70 LVScopeRoot *Root = Reader->getScopesRoot();
71 LVScopeCompileUnit *CompileUnit = getFirstCompileUnit(Root);
72
73 EXPECT_EQ(Root->getFileFormatName(), "elf64-x86-64");
74 EXPECT_EQ(Root->getName(), DwarfClang);
75
76 EXPECT_EQ(CompileUnit->getBaseAddress(), 0u);
77 EXPECT_TRUE(CompileUnit->getProducer().starts_with("clang"));
78 EXPECT_EQ(CompileUnit->getName(), "test.cpp");
79 LVSourceLanguage Language = CompileUnit->getSourceLanguage();
80 EXPECT_TRUE(Language.isValid());
81 EXPECT_EQ(Language, LVSourceLanguage::DW_LANG_C_plus_plus_14);
82 EXPECT_EQ(Language.getName(), "DW_LANG_C_plus_plus_14");
83
84 EXPECT_EQ(CompileUnit->lineCount(), 0u);
85 EXPECT_EQ(CompileUnit->scopeCount(), 1u);
86 EXPECT_EQ(CompileUnit->symbolCount(), 0u);
87 EXPECT_EQ(CompileUnit->typeCount(), 7u);
88 EXPECT_EQ(CompileUnit->rangeCount(), 1u);
89
90 const LVLocations *Ranges = CompileUnit->getRanges();
91 ASSERT_NE(Ranges, nullptr);
92 ASSERT_EQ(Ranges->size(), 1u);
93 LVLocations::const_iterator IterLocation = Ranges->begin();
94 LVLocation *Location = (*IterLocation);
95 EXPECT_STREQ(Location->getIntervalInfo().c_str(),
96 "{Range} Lines 2:9 [0x0000000000:0x000000003a]");
97
98 LVRange RangeList;
99 CompileUnit->getRanges(RangeList);
100
101 const LVRangeEntries &RangeEntries = RangeList.getEntries();
102 ASSERT_EQ(RangeEntries.size(), 2u);
103 LVRangeEntries::const_iterator IterRanges = RangeEntries.cbegin();
104 LVRangeEntry RangeEntry = *IterRanges;
105 EXPECT_EQ(RangeEntry.lower(), 0u);
106 EXPECT_EQ(RangeEntry.upper(), 0x3au);
107 EXPECT_EQ(RangeEntry.scope()->getLineNumber(), 0u);
108 EXPECT_EQ(RangeEntry.scope()->getName(), "test.cpp");
109 EXPECT_EQ(RangeEntry.scope()->getOffset(), 0x0bu);
110
111 ++IterRanges;
112 RangeEntry = *IterRanges;
113 EXPECT_EQ(RangeEntry.lower(), 0x1cu);
114 EXPECT_EQ(RangeEntry.upper(), 0x2fu);
115 EXPECT_EQ(RangeEntry.scope()->getLineNumber(), 0u);
116 EXPECT_EQ(RangeEntry.scope()->getName(), "foo::?");
117 EXPECT_EQ(RangeEntry.scope()->getOffset(), 0x71u);
118
119 const LVPublicNames &PublicNames = CompileUnit->getPublicNames();
120 ASSERT_EQ(PublicNames.size(), 1u);
121 LVPublicNames::const_iterator IterNames = PublicNames.cbegin();
122 LVScope *Function = (*IterNames).first;
123 EXPECT_EQ(Function->getName(), "foo");
124 EXPECT_EQ(Function->getLineNumber(), 2u);
125 LVNameInfo NameInfo = (*IterNames).second;
126 EXPECT_EQ(NameInfo.first, 0u);
127 EXPECT_EQ(NameInfo.second, 0x3au);
128
129 // Lines (debug and assembler) for 'foo'.
130 const LVLines *Lines = Function->getLines();
131 ASSERT_NE(Lines, nullptr);
132 ASSERT_EQ(Lines->size(), 0x12u);
133
134 // Check size of types in CompileUnit.
135 const LVTypes *Types = CompileUnit->getTypes();
136 ASSERT_NE(Types, nullptr);
137 EXPECT_EQ(Types->size(), 7u);
138
139 const auto BoolType = llvm::find_if(
140 *Types, [](const LVElement *elt) { return elt->getName() == "bool"; });
141 ASSERT_NE(BoolType, Types->end());
142 const auto IntType = llvm::find_if(
143 *Types, [](const LVElement *elt) { return elt->getName() == "int"; });
144 ASSERT_NE(IntType, Types->end());
145 EXPECT_EQ(static_cast<LVType *>(*BoolType)->getBitSize(), 8u);
146 EXPECT_EQ(static_cast<LVType *>(*BoolType)->getStorageSizeInBytes(), 1u);
147 EXPECT_EQ(static_cast<LVType *>(*IntType)->getBitSize(), 32u);
148 EXPECT_EQ(static_cast<LVType *>(*IntType)->getStorageSizeInBytes(), 4u);
149 }
150
151 // Check proper handling of DW_AT_unspecified_parameters in
152 // LVScope::addMissingElements().
153 void checkUnspecifiedParameters(LVReader *Reader) {
154 LVScopeRoot *Root = Reader->getScopesRoot();
155 LVScopeCompileUnit *CompileUnit = getFirstCompileUnit(Root);
156
157 EXPECT_EQ(Root->getFileFormatName(), "elf64-x86-64");
158 EXPECT_EQ(Root->getName(), DwarfClangUnspecParams);
159
160 const LVPublicNames &PublicNames = CompileUnit->getPublicNames();
161 ASSERT_EQ(PublicNames.size(), 1u);
162
163 LVPublicNames::const_iterator IterNames = PublicNames.cbegin();
164 LVScope *Function = (*IterNames).first;
165 EXPECT_EQ(Function->getName(), "foo_printf");
166 const LVElements *Elements = Function->getChildren();
167 ASSERT_NE(Elements, nullptr);
168 // foo_printf is a variadic function whose prototype is
169 // `int foo_printf(const char *, ...)`, where the '...' is represented by a
170 // DW_TAG_unspecified_parameters, i.e. we expect to find at least one child
171 // for which getIsUnspecified() returns true.
172 EXPECT_TRUE(llvm::any_of(*Elements, [](const LVElement *elt) {
173 return elt->getIsSymbol() &&
174 static_cast<const LVSymbol *>(elt)->getIsUnspecified();
175 }));
176 }
177
178 // Check the basic properties on parsed DW_TAG_module.
179 void checkScopeModule(LVReader *Reader) {
180 LVScopeRoot *Root = Reader->getScopesRoot();
181 LVScopeCompileUnit *CompileUnit = getFirstCompileUnit(Root);
182
183 EXPECT_EQ(Root->getFileFormatName(), "Mach-O 64-bit x86-64");
184 EXPECT_EQ(Root->getName(), DwarfClangModule);
185
186 ASSERT_NE(CompileUnit->getChildren(), nullptr);
187 LVElement *FirstChild = *(CompileUnit->getChildren()->begin());
188 EXPECT_EQ(FirstChild->getIsScope(), 1);
189 LVScopeModule *Module = static_cast<LVScopeModule *>(FirstChild);
190 EXPECT_EQ(Module->getIsModule(), 1);
191 EXPECT_EQ(Module->getName(), "DebugModule");
192 }
193
194 // Check the logical elements selection.
195 void checkElementSelection(LVReader *Reader) {
196 LVScopeRoot *Root = Reader->getScopesRoot();
197 LVScopeCompileUnit *CompileUnit = getFirstCompileUnit(Root);
198
199 // Get the matched elements.
200 LVElements MatchedElements = CompileUnit->getMatchedElements();
201 std::map<LVOffset, LVElement *> MapElements;
202 for (LVElement *Element : MatchedElements)
203 MapElements[Element->getOffset()] = Element;
204 ASSERT_EQ(MapElements.size(), 0xeu);
205
206 LVElement *Element = MapElements[0x000000004b]; // 'foo'
207 ASSERT_NE(Element, nullptr);
208 EXPECT_NE(Element->getName().find("foo"), StringRef::npos);
209 EXPECT_EQ(Element->getIsScope(), 1);
210
211 Element = MapElements[0x00000000c0]; // 'CONSTANT'
212 ASSERT_NE(Element, nullptr);
213 EXPECT_NE(Element->getName().find("CONSTANT"), StringRef::npos);
214 EXPECT_EQ(Element->getIsSymbol(), 1);
215
216 Element = MapElements[0x000000002d]; // 'INTPTR'
217 ASSERT_NE(Element, nullptr);
218 EXPECT_NE(Element->getName().find("INTPTR"), StringRef::npos);
219 EXPECT_EQ(Element->getIsType(), 1);
220
221 Element = MapElements[0x00000000af]; // 'INTEGER'
222 ASSERT_NE(Element, nullptr);
223 EXPECT_NE(Element->getName().find("INTEGER"), StringRef::npos);
224 EXPECT_EQ(Element->getIsType(), 1);
225
226 Element = MapElements[0x000000000f]; // 'movl %edx, %eax'
227 ASSERT_NE(Element, nullptr);
228 EXPECT_NE(Element->getName().find("movl"), StringRef::npos);
229 EXPECT_EQ(Element->getIsLine(), 1);
230
231 // Get the parents for the matched elements.
232 LVScopes MatchedScopes = CompileUnit->getMatchedScopes();
233 std::set<LVOffset> SetScopes;
234 for (LVScope *Scope : MatchedScopes)
235 SetScopes.insert(Scope->getOffset());
236 std::set<LVOffset>::iterator Iter;
237 ASSERT_EQ(SetScopes.size(), 3u);
238
239 Iter = SetScopes.find(0x000000000b); // CompileUnit <- 'foo'
240 EXPECT_NE(Iter, SetScopes.end());
241 Iter = SetScopes.find(0x000000009e); // Function <- 'movl %edx, %eax'
242 EXPECT_NE(Iter, SetScopes.end());
243 Iter = SetScopes.find(0x000000009e); // LexicalScope <- 'INTEGER'
244 EXPECT_NE(Iter, SetScopes.end());
245 }
246
247 // Check the logical elements comparison.
248 void checkElementComparison(LVReader *Reference, LVReader *Target) {
249 LVCompare Compare(nulls());
250 Error Err = Compare.execute(Reference, Target);
251 ASSERT_THAT_ERROR(std::move(Err), Succeeded());
252
253 // Get comparison table.
254 LVPassTable PassTable = Compare.getPassTable();
255 ASSERT_EQ(PassTable.size(), 5u);
256
257 LVReader *Reader;
258 LVElement *Element;
259 LVComparePass Pass;
260
261 // Reference: Missing Variable 'CONSTANT'
262 std::tie(Reader, Element, Pass) = PassTable[0];
263 ASSERT_NE(Reader, nullptr);
264 ASSERT_NE(Element, nullptr);
265 EXPECT_EQ(Reader, Reference);
266 EXPECT_EQ(Element->getLevel(), 4u);
267 EXPECT_EQ(Element->getLineNumber(), 5u);
268 EXPECT_EQ(Element->getName(), "CONSTANT");
269 EXPECT_EQ(Pass, LVComparePass::Missing);
270
271 // Reference: Missing TypeDefinition 'INTEGER'
272 std::tie(Reader, Element, Pass) = PassTable[1];
273 ASSERT_NE(Reader, nullptr);
274 ASSERT_NE(Element, nullptr);
275 EXPECT_EQ(Reader, Reference);
276 EXPECT_EQ(Element->getLevel(), 3u);
277 EXPECT_EQ(Element->getLineNumber(), 4u);
278 EXPECT_EQ(Element->getName(), "INTEGER");
279 EXPECT_EQ(Pass, LVComparePass::Missing);
280
281 // Reference: Missing DebugLine
282 std::tie(Reader, Element, Pass) = PassTable[2];
283 ASSERT_NE(Reader, nullptr);
284 ASSERT_NE(Element, nullptr);
285 EXPECT_EQ(Reader, Reference);
286 EXPECT_EQ(Element->getLevel(), 3u);
287 EXPECT_EQ(Element->getLineNumber(), 8u);
288 EXPECT_EQ(Element->getName(), "");
289 EXPECT_EQ(Pass, LVComparePass::Missing);
290
291 // Target: Added Variable 'CONSTANT'
292 std::tie(Reader, Element, Pass) = PassTable[3];
293 ASSERT_NE(Reader, nullptr);
294 ASSERT_NE(Element, nullptr);
295 EXPECT_EQ(Reader, Target);
296 EXPECT_EQ(Element->getLevel(), 4u);
297 EXPECT_EQ(Element->getLineNumber(), 5u);
298 EXPECT_EQ(Element->getName(), "CONSTANT");
299 EXPECT_EQ(Pass, LVComparePass::Added);
300
301 // Target: Added TypeDefinition 'INTEGER'
302 std::tie(Reader, Element, Pass) = PassTable[4];
303 ASSERT_NE(Reader, nullptr);
304 ASSERT_NE(Element, nullptr);
305 EXPECT_EQ(Reader, Target);
306 EXPECT_EQ(Element->getLevel(), 4u);
307 EXPECT_EQ(Element->getLineNumber(), 4u);
308 EXPECT_EQ(Element->getName(), "INTEGER");
309 EXPECT_EQ(Pass, LVComparePass::Added);
310 }
311
312 // Logical elements properties.
313 void elementProperties(SmallString<128> &InputsDir) {
314 // Reader options.
315 LVOptions ReaderOptions;
316 ReaderOptions.setAttributeOffset();
317 ReaderOptions.setAttributeFormat();
318 ReaderOptions.setAttributeFilename();
319 ReaderOptions.setAttributeProducer();
320 ReaderOptions.setAttributePublics();
321 ReaderOptions.setAttributeRange();
322 ReaderOptions.setAttributeLanguage();
323 ReaderOptions.setAttributeLocation();
324 ReaderOptions.setAttributeInserted();
325 ReaderOptions.setAttributeSize();
326 ReaderOptions.setPrintAll();
327 ReaderOptions.resolveDependencies();
328
329 std::vector<std::string> Objects;
330 ScopedPrinter W(outs());
331 LVReaderHandler ReaderHandler(Objects, W, ReaderOptions);
332
333 // Check logical elements properties.
334 std::unique_ptr<LVReader> Reader =
335 createReader(ReaderHandler, InputsDir, DwarfClang);
336 checkElementProperties(Reader.get());
337
338 Reader = createReader(ReaderHandler, InputsDir, DwarfClangUnspecParams);
339 checkUnspecifiedParameters(Reader.get());
340
341 Reader = createReader(ReaderHandler, InputsDir, DwarfClangModule);
342 checkScopeModule(Reader.get());
343 }
344
345 // Logical elements selection.
346 void elementSelection(SmallString<128> &InputsDir) {
347 // Reader options.
348 LVOptions ReaderOptions;
349 ReaderOptions.setAttributeOffset();
350 ReaderOptions.setPrintAll();
351
352 ReaderOptions.setSelectIgnoreCase();
353 ReaderOptions.setSelectUseRegex();
354
355 ReaderOptions.setReportList(); // Matched elements.
356 ReaderOptions.setReportView(); // Parents for matched elements.
357
358 // Add patterns.
359 ReaderOptions.Select.Generic.insert("foo");
360 ReaderOptions.Select.Generic.insert("movl[ \t]?%");
361 ReaderOptions.Select.Generic.insert("INT[a-z]*");
362 ReaderOptions.Select.Generic.insert("CONSTANT");
363
364 ReaderOptions.resolveDependencies();
365
366 std::vector<std::string> Objects;
367 ScopedPrinter W(outs());
368 LVReaderHandler ReaderHandler(Objects, W, ReaderOptions);
369
370 // Check logical elements selection.
371 std::unique_ptr<LVReader> Reader =
372 createReader(ReaderHandler, InputsDir, DwarfGcc);
373 checkElementSelection(Reader.get());
374 }
375
376 // Compare logical elements.
377 void compareElements(SmallString<128> &InputsDir) {
378 // Reader options.
379 LVOptions ReaderOptions;
380 ReaderOptions.setAttributeOffset();
381 ReaderOptions.setPrintLines();
382 ReaderOptions.setPrintSymbols();
383 ReaderOptions.setPrintTypes();
384 ReaderOptions.setCompareLines();
385 ReaderOptions.setCompareSymbols();
386 ReaderOptions.setCompareTypes();
387
388 ReaderOptions.resolveDependencies();
389
390 std::vector<std::string> Objects;
391 ScopedPrinter W(outs());
392 LVReaderHandler ReaderHandler(Objects, W, ReaderOptions);
393
394 // Check logical comparison.
395 std::unique_ptr<LVReader> Reference =
396 createReader(ReaderHandler, InputsDir, DwarfClang);
397 std::unique_ptr<LVReader> Target =
398 createReader(ReaderHandler, InputsDir, DwarfGcc);
399 checkElementComparison(Reference.get(), Target.get());
400 }
401
402 TEST(LogicalViewTest, DWARFReader) {
403 // Initialize targets and assembly printers/parsers.
404 llvm::InitializeAllTargetInfos();
405 llvm::InitializeAllTargetMCs();
406 InitializeAllDisassemblers();
407
408 llvm::sys::InitializeCOMRAII COM(llvm::sys::COMThreadingMode::MultiThreaded);
409
410 // This test requires a x86-registered-target.
411 Triple TT;
412 TT.setArch(Triple::x86_64);
413 TT.setVendor(Triple::UnknownVendor);
414 TT.setOS(Triple::UnknownOS);
415
416 std::string TargetLookupError;
417 if (!TargetRegistry::lookupTarget(TT, TargetLookupError))
418 GTEST_SKIP();
419
420 SmallString<128> InputsDir = unittest::getInputFileDirectory(TestMainArgv0);
421
422 // Logical elements general properties and selection.
423 elementProperties(InputsDir);
424 elementSelection(InputsDir);
425
426 // Compare logical elements.
427 compareElements(InputsDir);
428 }
429
430 } // namespace