1 //===-- DWARFUnitTest.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 "Plugins/SymbolFile/DWARF/DWARFUnit.h"
10 #include "TestingSupport/Symbol/YAMLModuleTester.h"
11 #include "gmock/gmock.h"
12 #include "gtest/gtest.h"
13 
14 using namespace lldb;
15 using namespace lldb_private;
16 
TEST(DWARFUnitTest,NullUnitDie)17 TEST(DWARFUnitTest, NullUnitDie) {
18   // Make sure we don't crash parsing a null unit DIE.
19   const char *yamldata = R"(
20 --- !ELF
21 FileHeader:
22   Class:   ELFCLASS64
23   Data:    ELFDATA2LSB
24   Type:    ET_EXEC
25   Machine: EM_386
26 DWARF:
27   debug_abbrev:
28     - Table:
29         - Code:            0x00000001
30           Tag:             DW_TAG_compile_unit
31           Children:        DW_CHILDREN_yes
32           Attributes:
33             - Attribute:       DW_AT_language
34               Form:            DW_FORM_data2
35   debug_info:
36     - Version:         4
37       AddrSize:        8
38       Entries:
39         - AbbrCode:        0x00000000
40 )";
41 
42   YAMLModuleTester t(yamldata);
43   ASSERT_TRUE((bool)t.GetDwarfUnit());
44 
45   DWARFUnit *unit = t.GetDwarfUnit();
46   const DWARFDebugInfoEntry *die_first = unit->DIE().GetDIE();
47   ASSERT_NE(die_first, nullptr);
48   EXPECT_TRUE(die_first->IsNULL());
49 }
50 
TEST(DWARFUnitTest,MissingSentinel)51 TEST(DWARFUnitTest, MissingSentinel) {
52   // Make sure we don't crash if the debug info is missing a null DIE sentinel.
53   const char *yamldata = R"(
54 --- !ELF
55 FileHeader:
56   Class:   ELFCLASS64
57   Data:    ELFDATA2LSB
58   Type:    ET_EXEC
59   Machine: EM_386
60 DWARF:
61   debug_abbrev:
62     - Table:
63         - Code:            0x00000001
64           Tag:             DW_TAG_compile_unit
65           Children:        DW_CHILDREN_yes
66           Attributes:
67             - Attribute:       DW_AT_language
68               Form:            DW_FORM_data2
69   debug_info:
70     - Version:         4
71       AddrSize:        8
72       Entries:
73         - AbbrCode:        0x00000001
74           Values:
75             - Value:           0x000000000000000C
76 )";
77 
78   YAMLModuleTester t(yamldata);
79   ASSERT_TRUE((bool)t.GetDwarfUnit());
80 
81   DWARFUnit *unit = t.GetDwarfUnit();
82   const DWARFDebugInfoEntry *die_first = unit->DIE().GetDIE();
83   ASSERT_NE(die_first, nullptr);
84   EXPECT_EQ(die_first->GetFirstChild(), nullptr);
85   EXPECT_EQ(die_first->GetSibling(), nullptr);
86 }
87 
TEST(DWARFUnitTest,ClangProducer)88 TEST(DWARFUnitTest, ClangProducer) {
89   const char *yamldata = R"(
90 --- !ELF
91 FileHeader:
92   Class:   ELFCLASS64
93   Data:    ELFDATA2LSB
94   Type:    ET_EXEC
95   Machine: EM_386
96 DWARF:
97   debug_str:
98     - 'Apple clang version 13.0.0 (clang-1300.0.29.3)'
99   debug_abbrev:
100     - Table:
101         - Code:            0x00000001
102           Tag:             DW_TAG_compile_unit
103           Children:        DW_CHILDREN_yes
104           Attributes:
105             - Attribute:       DW_AT_producer
106               Form:            DW_FORM_strp
107   debug_info:
108     - Version:         4
109       AddrSize:        8
110       Entries:
111         - AbbrCode:        0x1
112           Values:
113             - Value:           0x0
114         - AbbrCode:        0x0
115 )";
116 
117   YAMLModuleTester t(yamldata);
118   DWARFUnit *unit = t.GetDwarfUnit();
119   ASSERT_TRUE((bool)unit);
120   EXPECT_EQ(unit->GetProducer(), eProducerClang);
121   EXPECT_EQ(unit->GetProducerVersion(), llvm::VersionTuple(1300, 0, 29, 3));
122 }
123 
TEST(DWARFUnitTest,LLVMGCCProducer)124 TEST(DWARFUnitTest, LLVMGCCProducer) {
125   const char *yamldata = R"(
126 --- !ELF
127 FileHeader:
128   Class:   ELFCLASS64
129   Data:    ELFDATA2LSB
130   Type:    ET_EXEC
131   Machine: EM_386
132 DWARF:
133   debug_str:
134     - 'i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)'
135   debug_abbrev:
136     - Table:
137         - Code:            0x00000001
138           Tag:             DW_TAG_compile_unit
139           Children:        DW_CHILDREN_yes
140           Attributes:
141             - Attribute:       DW_AT_producer
142               Form:            DW_FORM_strp
143   debug_info:
144     - Version:         4
145       AddrSize:        8
146       Entries:
147         - AbbrCode:        0x1
148           Values:
149             - Value:           0x0
150         - AbbrCode:        0x0
151 )";
152 
153   YAMLModuleTester t(yamldata);
154   DWARFUnit *unit = t.GetDwarfUnit();
155   ASSERT_TRUE((bool)unit);
156   EXPECT_EQ(unit->GetProducer(), eProducerLLVMGCC);
157 }
158 
TEST(DWARFUnitTest,SwiftProducer)159 TEST(DWARFUnitTest, SwiftProducer) {
160   const char *yamldata = R"(
161 --- !ELF
162 FileHeader:
163   Class:   ELFCLASS64
164   Data:    ELFDATA2LSB
165   Type:    ET_EXEC
166   Machine: EM_386
167 DWARF:
168   debug_str:
169     - 'Apple Swift version 5.5 (swiftlang-1300.0.31.1 clang-1300.0.29.1)'
170   debug_abbrev:
171     - Table:
172         - Code:            0x00000001
173           Tag:             DW_TAG_compile_unit
174           Children:        DW_CHILDREN_yes
175           Attributes:
176             - Attribute:       DW_AT_producer
177               Form:            DW_FORM_strp
178   debug_info:
179     - Version:         4
180       AddrSize:        8
181       Entries:
182         - AbbrCode:        0x1
183           Values:
184             - Value:           0x0
185         - AbbrCode:        0x0
186 )";
187 
188   YAMLModuleTester t(yamldata);
189   DWARFUnit *unit = t.GetDwarfUnit();
190   ASSERT_TRUE((bool)unit);
191   EXPECT_EQ(unit->GetProducer(), eProducerSwift);
192   EXPECT_EQ(unit->GetProducerVersion(), llvm::VersionTuple(1300, 0, 31, 1));
193 }
194