210 lines · cpp
1//===-- DWARFDIETest.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-exception6//7//===----------------------------------------------------------------------===//8 9#include "Plugins/SymbolFile/DWARF/DWARFDIE.h"10#include "Plugins/SymbolFile/DWARF/DWARFDebugInfo.h"11#include "Plugins/SymbolFile/DWARF/DWARFDeclContext.h"12#include "Plugins/SymbolFile/DWARF/DebugNamesDWARFIndex.h"13#include "TestingSupport/Symbol/YAMLModuleTester.h"14#include "lldb/lldb-private-enumerations.h"15#include "llvm/ADT/STLExtras.h"16#include "gmock/gmock.h"17#include "gtest/gtest.h"18 19using namespace lldb;20using namespace lldb_private;21using namespace lldb_private::plugin::dwarf;22using StringRef = llvm::StringRef;23 24static void25check_num_matches(DebugNamesDWARFIndex &index, int expected_num_matches,26 llvm::ArrayRef<DWARFDeclContext::Entry> ctx_entries) {27 DWARFDeclContext ctx(ctx_entries);28 int num_matches = 0;29 30 index.GetFullyQualifiedType(ctx, [&](DWARFDIE die) {31 num_matches++;32 return IterationAction::Continue;33 });34 ASSERT_EQ(num_matches, expected_num_matches);35}36 37static DWARFDeclContext::Entry make_entry(const char *c) {38 return DWARFDeclContext::Entry(llvm::dwarf::DW_TAG_class_type, c);39}40 41TEST(DWARFDebugNamesIndexTest, FullyQualifiedQueryWithIDXParent) {42 const char *yamldata = R"(43--- !ELF44FileHeader:45 Class: ELFCLASS6446 Data: ELFDATA2LSB47 Type: ET_EXEC48 Machine: EM_38649DWARF:50 debug_str:51 - '1'52 - '2'53 - '3'54 debug_abbrev:55 - Table:56 # We intentionally don't nest types in debug_info: if the nesting is not57 # inferred from debug_names, we want the test to fail.58 - Code: 0x159 Tag: DW_TAG_compile_unit60 Children: DW_CHILDREN_yes61 - Code: 0x262 Tag: DW_TAG_class_type63 Children: DW_CHILDREN_no64 Attributes:65 - Attribute: DW_AT_name66 Form: DW_FORM_strp67 debug_info:68 - Version: 469 AddrSize: 870 Entries:71 - AbbrCode: 0x172 - AbbrCode: 0x273 Values:74 - Value: 0x0 # Name "1"75 - AbbrCode: 0x276 Values:77 - Value: 0x2 # Name "2"78 - AbbrCode: 0x279 Values:80 - Value: 0x4 # Name "3"81 - AbbrCode: 0x082 debug_names:83 Abbreviations:84 - Code: 0x1185 Tag: DW_TAG_class_type86 Indices:87 - Idx: DW_IDX_parent88 Form: DW_FORM_flag_present89 - Idx: DW_IDX_die_offset90 Form: DW_FORM_ref491 - Code: 0x2292 Tag: DW_TAG_class_type93 Indices:94 - Idx: DW_IDX_parent95 Form: DW_FORM_ref496 - Idx: DW_IDX_die_offset97 Form: DW_FORM_ref498 Entries:99 - Name: 0x0 # strp to Name1100 Code: 0x11101 Values:102 - 0xc # Die offset to entry named "1"103 - Name: 0x2 # strp to Name2104 Code: 0x22105 Values:106 - 0x0 # Parent = First entry ("1")107 - 0x11 # Die offset to entry named "1:2"108 - Name: 0x4 # strp to Name3109 Code: 0x22110 Values:111 - 0x6 # Parent = Second entry ("1::2")112 - 0x16 # Die offset to entry named "1::2::3"113 - Name: 0x4 # strp to Name3114 Code: 0x11115 Values:116 - 0x16 # Die offset to entry named "3"117)";118 119 YAMLModuleTester t(yamldata);120 auto *symbol_file =121 llvm::cast<SymbolFileDWARF>(t.GetModule()->GetSymbolFile());122 auto *index = static_cast<DebugNamesDWARFIndex *>(symbol_file->getIndex());123 ASSERT_NE(index, nullptr);124 125 check_num_matches(*index, 1, {make_entry("1")});126 check_num_matches(*index, 1, {make_entry("2"), make_entry("1")});127 check_num_matches(*index, 1,128 {make_entry("3"), make_entry("2"), make_entry("1")});129 check_num_matches(*index, 0, {make_entry("2")});130 check_num_matches(*index, 1, {make_entry("3")});131}132 133TEST(DWARFDebugNamesIndexTest, FullyQualifiedQueryWithoutIDXParent) {134 const char *yamldata = R"(135--- !ELF136FileHeader:137 Class: ELFCLASS64138 Data: ELFDATA2LSB139 Type: ET_EXEC140 Machine: EM_386141DWARF:142 debug_str:143 - '1'144 - '2'145 debug_abbrev:146 - Table:147 - Code: 0x1148 Tag: DW_TAG_compile_unit149 Children: DW_CHILDREN_yes150 - Code: 0x2151 Tag: DW_TAG_class_type152 Children: DW_CHILDREN_yes153 Attributes:154 - Attribute: DW_AT_name155 Form: DW_FORM_strp156 - Code: 0x3157 Tag: DW_TAG_class_type158 Children: DW_CHILDREN_no159 Attributes:160 - Attribute: DW_AT_name161 Form: DW_FORM_strp162 debug_info:163 - Version: 4164 AddrSize: 8165 Entries:166 - AbbrCode: 0x1167 - AbbrCode: 0x2168 Values:169 - Value: 0x0 # Name "1"170 - AbbrCode: 0x3171 Values:172 - Value: 0x2 # Name "2"173 - AbbrCode: 0x0174 - AbbrCode: 0x3175 Values:176 - Value: 0x2 # Name "2"177 - AbbrCode: 0x0178 debug_names:179 Abbreviations:180 - Code: 0x1181 Tag: DW_TAG_class_type182 Indices:183 - Idx: DW_IDX_die_offset184 Form: DW_FORM_ref4185 Entries:186 - Name: 0x0 # strp to Name1187 Code: 0x1188 Values:189 - 0xc # Die offset to entry named "1"190 - Name: 0x2 # strp to Name2191 Code: 0x1192 Values:193 - 0x11 # Die offset to entry named "1::2"194 - Name: 0x2 # strp to Name2195 Code: 0x1196 Values:197 - 0x17 # Die offset to entry named "2"198)";199 200 YAMLModuleTester t(yamldata);201 auto *symbol_file =202 llvm::cast<SymbolFileDWARF>(t.GetModule()->GetSymbolFile());203 auto *index = static_cast<DebugNamesDWARFIndex *>(symbol_file->getIndex());204 ASSERT_NE(index, nullptr);205 206 check_num_matches(*index, 1, {make_entry("1")});207 check_num_matches(*index, 1, {make_entry("2"), make_entry("1")});208 check_num_matches(*index, 1, {make_entry("2")});209}210