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