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 using namespace lldb_private::plugin::dwarf; 17 18 TEST(DWARFUnitTest, NullUnitDie) { 19 // Make sure we don't crash parsing a null unit DIE. 20 const char *yamldata = R"( 21 --- !ELF 22 FileHeader: 23 Class: ELFCLASS64 24 Data: ELFDATA2LSB 25 Type: ET_EXEC 26 Machine: EM_386 27 DWARF: 28 debug_abbrev: 29 - Table: 30 - Code: 0x00000001 31 Tag: DW_TAG_compile_unit 32 Children: DW_CHILDREN_yes 33 Attributes: 34 - Attribute: DW_AT_language 35 Form: DW_FORM_data2 36 debug_info: 37 - Version: 4 38 AddrSize: 8 39 Entries: 40 - AbbrCode: 0x00000000 41 )"; 42 43 YAMLModuleTester t(yamldata); 44 ASSERT_TRUE((bool)t.GetDwarfUnit()); 45 46 DWARFUnit *unit = t.GetDwarfUnit(); 47 const DWARFDebugInfoEntry *die_first = unit->DIE().GetDIE(); 48 ASSERT_NE(die_first, nullptr); 49 EXPECT_TRUE(die_first->IsNULL()); 50 } 51 52 TEST(DWARFUnitTest, MissingSentinel) { 53 // Make sure we don't crash if the debug info is missing a null DIE sentinel. 54 const char *yamldata = R"( 55 --- !ELF 56 FileHeader: 57 Class: ELFCLASS64 58 Data: ELFDATA2LSB 59 Type: ET_EXEC 60 Machine: EM_386 61 DWARF: 62 debug_abbrev: 63 - Table: 64 - Code: 0x00000001 65 Tag: DW_TAG_compile_unit 66 Children: DW_CHILDREN_yes 67 Attributes: 68 - Attribute: DW_AT_language 69 Form: DW_FORM_data2 70 debug_info: 71 - Version: 4 72 AddrSize: 8 73 Entries: 74 - AbbrCode: 0x00000001 75 Values: 76 - Value: 0x000000000000000C 77 )"; 78 79 YAMLModuleTester t(yamldata); 80 ASSERT_TRUE((bool)t.GetDwarfUnit()); 81 82 DWARFUnit *unit = t.GetDwarfUnit(); 83 const DWARFDebugInfoEntry *die_first = unit->DIE().GetDIE(); 84 ASSERT_NE(die_first, nullptr); 85 EXPECT_EQ(die_first->GetFirstChild(), nullptr); 86 EXPECT_EQ(die_first->GetSibling(), nullptr); 87 } 88 89 TEST(DWARFUnitTest, ClangProducer) { 90 const char *yamldata = R"( 91 --- !ELF 92 FileHeader: 93 Class: ELFCLASS64 94 Data: ELFDATA2LSB 95 Type: ET_EXEC 96 Machine: EM_386 97 DWARF: 98 debug_str: 99 - 'Apple clang version 13.0.0 (clang-1300.0.29.3)' 100 debug_abbrev: 101 - Table: 102 - Code: 0x00000001 103 Tag: DW_TAG_compile_unit 104 Children: DW_CHILDREN_yes 105 Attributes: 106 - Attribute: DW_AT_producer 107 Form: DW_FORM_strp 108 debug_info: 109 - Version: 4 110 AddrSize: 8 111 Entries: 112 - AbbrCode: 0x1 113 Values: 114 - Value: 0x0 115 - AbbrCode: 0x0 116 )"; 117 118 YAMLModuleTester t(yamldata); 119 DWARFUnit *unit = t.GetDwarfUnit(); 120 ASSERT_TRUE((bool)unit); 121 EXPECT_EQ(unit->GetProducer(), eProducerClang); 122 EXPECT_EQ(unit->GetProducerVersion(), llvm::VersionTuple(1300, 0, 29, 3)); 123 } 124 125 TEST(DWARFUnitTest, SwiftProducer) { 126 const char *yamldata = R"( 127 --- !ELF 128 FileHeader: 129 Class: ELFCLASS64 130 Data: ELFDATA2LSB 131 Type: ET_EXEC 132 Machine: EM_386 133 DWARF: 134 debug_str: 135 - 'Apple Swift version 5.5 (swiftlang-1300.0.31.1 clang-1300.0.29.1)' 136 debug_abbrev: 137 - Table: 138 - Code: 0x00000001 139 Tag: DW_TAG_compile_unit 140 Children: DW_CHILDREN_yes 141 Attributes: 142 - Attribute: DW_AT_producer 143 Form: DW_FORM_strp 144 debug_info: 145 - Version: 4 146 AddrSize: 8 147 Entries: 148 - AbbrCode: 0x1 149 Values: 150 - Value: 0x0 151 - AbbrCode: 0x0 152 )"; 153 154 YAMLModuleTester t(yamldata); 155 DWARFUnit *unit = t.GetDwarfUnit(); 156 ASSERT_TRUE((bool)unit); 157 EXPECT_EQ(unit->GetProducer(), eProducerSwift); 158 EXPECT_EQ(unit->GetProducerVersion(), llvm::VersionTuple(1300, 0, 31, 1)); 159 } 160