1 //===-llvm/unittest/DebugInfo/DWARFDieManualExtractTest.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 "DwarfGenerator.h"
10 #include "DwarfUtils.h"
11 #include "llvm/BinaryFormat/Dwarf.h"
12 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
13 #include "llvm/ObjectYAML/DWARFEmitter.h"
14 #include "llvm/Testing/Support/Error.h"
15 #include "gtest/gtest.h"
16
17 using namespace llvm;
18 using namespace llvm::dwarf;
19 using namespace utils;
20
21 namespace {
22
TEST(DWARFDie,manualExtractDump)23 TEST(DWARFDie, manualExtractDump) {
24 typedef uint32_t AddrType;
25 uint16_t Version = 4;
26 Triple Triple = getDefaultTargetTripleForAddrSize(sizeof(AddrType));
27 if (!isObjectEmissionSupported(Triple))
28 return;
29
30 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);
31 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded());
32 dwarfgen::Generator *DG = ExpectedDG.get().get();
33 dwarfgen::CompileUnit &DGCU = DG->addCompileUnit();
34 dwarfgen::DIE CUDie = DGCU.getUnitDIE();
35
36 CUDie.addAttribute(DW_AT_name, DW_FORM_strp, "/tmp/main.c");
37 CUDie.addAttribute(DW_AT_language, DW_FORM_data2, DW_LANG_C);
38
39 dwarfgen::DIE SubprogramDie = CUDie.addChild(DW_TAG_subprogram);
40 SubprogramDie.addAttribute(DW_AT_name, DW_FORM_strp, "main");
41 SubprogramDie.addAttribute(DW_AT_low_pc, DW_FORM_addr, 0x1000U);
42 SubprogramDie.addAttribute(DW_AT_high_pc, DW_FORM_addr, 0x2000U);
43
44 StringRef FileBytes = DG->generate();
45 MemoryBufferRef FileBuffer(FileBytes, "dwarf");
46 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
47 EXPECT_TRUE((bool)Obj);
48 std::unique_ptr<DWARFContext> Ctx = DWARFContext::create(**Obj);
49
50 DWARFCompileUnit *CU = Ctx->getCompileUnitForOffset(0);
51 ASSERT_NE(nullptr, CU);
52 // Manually extracting DWARF DIE.
53 uint64_t DIEOffset = CU->getOffset() + CU->getHeaderSize();
54 uint64_t NextCUOffset = CU->getNextUnitOffset();
55 DWARFDebugInfoEntry DieInfo;
56 DWARFDataExtractor DebugInfoData = CU->getDebugInfoExtractor();
57 uint32_t Depth = 0;
58 ASSERT_TRUE(
59 DieInfo.extractFast(*CU, &DIEOffset, DebugInfoData, NextCUOffset, Depth));
60 DWARFDie Die(CU, &DieInfo);
61 ASSERT_TRUE(Die.isValid());
62 ASSERT_TRUE(Die.hasChildren());
63 // Since we have extracted manually DieArray is empty.
64 // Dump function should respect the default flags and print just current DIE,
65 // and not explore children.
66 SmallString<512> Output;
67 raw_svector_ostream OS(Output);
68 Die.dump(OS);
69 constexpr size_t NumOfLines = 3;
70 SmallVector<StringRef, NumOfLines> Strings;
71 SmallVector<StringRef, NumOfLines> ValidStrings = {
72 "0x0000000b: DW_TAG_compile_unit",
73 " DW_AT_name (\"/tmp/main.c\")",
74 " DW_AT_language (DW_LANG_C)"};
75 Output.str().split(Strings, '\n', -1, false);
76 ASSERT_EQ(Strings.size(), NumOfLines);
77 for (size_t I = 0; I < NumOfLines; ++I)
78 EXPECT_EQ(ValidStrings[I], Strings[I]);
79 }
80
81 } // end anonymous namespace
82