Skip to content

Commit 3096f87

Browse files
authored
[lldb] Don't create instance of SymbolFileDWARFDebugMap for non-Mach-O files (#139170)
# Change `SymbolFileDWARFDebugMap::CreateInstance()` will return `nullptr` if the file is not a Mach-O. # Benefit This may improve **Linux** debugger launch time by skipping the creation of `SymbolFileDWARFDebugMap` during the [`SymbolFile::FindPlugin()` call](https://fburl.com/hi1w8dil), which loops through a list of `SymbolFile` plugins and tries to find the one that provides the best abilities. If the `SymbolFileDWARFDebugMap` is created during this loop, it will load the symbol table of the file in question and loop through all the compile units in the debug map (the OSO entries) to calculate the abilities. # Tests See PR.
1 parent 1bc3845 commit 3096f87

File tree

3 files changed

+159
-0
lines changed

3 files changed

+159
-0
lines changed

lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,10 @@ llvm::StringRef SymbolFileDWARFDebugMap::GetPluginDescriptionStatic() {
246246
}
247247

248248
SymbolFile *SymbolFileDWARFDebugMap::CreateInstance(ObjectFileSP objfile_sp) {
249+
assert(objfile_sp);
250+
// Don't create a debug map if the object file isn't a Mach-O.
251+
if (!objfile_sp->GetArchitecture().GetTriple().isAppleMachO())
252+
return nullptr;
249253
return new SymbolFileDWARFDebugMap(std::move(objfile_sp));
250254
}
251255

lldb/unittests/SymbolFile/DWARF/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ add_lldb_unittest(SymbolFileDWARFTests
44
DWARFDIETest.cpp
55
DWARFIndexCachingTest.cpp
66
DWARFUnitTest.cpp
7+
SymbolFileDWARFDebugMapTests.cpp
78
SymbolFileDWARFTests.cpp
89
XcodeSDKModuleTests.cpp
910

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
//===-- SymbolFileDWARFDebugMapTests.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/ObjectFile/ELF/ObjectFileELF.h"
10+
#include "Plugins/ObjectFile/Mach-O/ObjectFileMachO.h"
11+
#include "Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.h"
12+
#include "TestingSupport/SubsystemRAII.h"
13+
#include "TestingSupport/TestUtilities.h"
14+
15+
#include "lldb/Core/Module.h"
16+
#include "llvm/Testing/Support/Error.h"
17+
18+
#include "gtest/gtest.h"
19+
20+
using namespace lldb;
21+
using namespace lldb_private;
22+
using namespace lldb_private::plugin::dwarf;
23+
24+
class SymbolFileDWARFDebugMapTests : public testing::Test {
25+
SubsystemRAII<ObjectFileELF, ObjectFileMachO> subsystems;
26+
};
27+
28+
TEST_F(SymbolFileDWARFDebugMapTests, CreateInstanceReturnNonNullForMachOFile) {
29+
// A Mach-O file built for arm64 CPU type and macOS platform.
30+
const char *yamldata = R"(
31+
--- !mach-o
32+
FileHeader:
33+
magic: 0xFEEDFACF
34+
cputype: 0x0100000C
35+
cpusubtype: 0x00000000
36+
filetype: 0x00000001
37+
ncmds: 1
38+
sizeofcmds: 176
39+
flags: 0x00002000
40+
reserved: 0x00000000
41+
LoadCommands:
42+
- cmd: LC_BUILD_VERSION
43+
cmdsize: 24
44+
platform: 1
45+
minos: 658944
46+
sdk: 658944
47+
ntools: 0
48+
- cmd: LC_SEGMENT_64
49+
cmdsize: 152
50+
segname: __TEXT
51+
vmaddr: 0
52+
vmsize: 4
53+
fileoff: 208
54+
filesize: 4
55+
maxprot: 7
56+
initprot: 7
57+
nsects: 1
58+
flags: 0
59+
Sections:
60+
- sectname: __text
61+
segname: __TEXT
62+
addr: 0x0000000000000000
63+
content: 'AABBCCDD'
64+
size: 4
65+
offset: 208
66+
align: 0
67+
reloff: 0x00000000
68+
nreloc: 0
69+
flags: 0x80000400
70+
reserved1: 0x00000000
71+
reserved2: 0x00000000
72+
reserved3: 0x00000000
73+
...
74+
)";
75+
76+
// Perform setup.
77+
llvm::Expected<TestFile> file = TestFile::fromYaml(yamldata);
78+
EXPECT_THAT_EXPECTED(file, llvm::Succeeded());
79+
auto module_sp = std::make_shared<Module>(file->moduleSpec());
80+
ASSERT_NE(module_sp, nullptr);
81+
auto object_file = module_sp->GetObjectFile();
82+
ASSERT_NE(object_file, nullptr);
83+
84+
// The debug map should be non-null for Mach-O file.
85+
auto debug_map =
86+
SymbolFileDWARFDebugMap::CreateInstance(object_file->shared_from_this());
87+
ASSERT_NE(debug_map, nullptr);
88+
}
89+
90+
TEST_F(SymbolFileDWARFDebugMapTests, CreateInstanceReturnNullForNonMachOFile) {
91+
// An ELF file.
92+
const char *yamldata = R"(
93+
--- !ELF
94+
FileHeader:
95+
Class: ELFCLASS64
96+
Data: ELFDATA2LSB
97+
Type: ET_EXEC
98+
Machine: EM_386
99+
DWARF:
100+
debug_abbrev:
101+
- Table:
102+
- Code: 0x00000001
103+
Tag: DW_TAG_compile_unit
104+
Children: DW_CHILDREN_no
105+
Attributes:
106+
- Attribute: DW_AT_addr_base
107+
Form: DW_FORM_sec_offset
108+
debug_info:
109+
- Version: 5
110+
AddrSize: 4
111+
UnitType: DW_UT_compile
112+
Entries:
113+
- AbbrCode: 0x00000001
114+
Values:
115+
- Value: 0x8 # Offset of the first Address past the header
116+
- AbbrCode: 0x0
117+
debug_addr:
118+
- Version: 5
119+
AddressSize: 4
120+
Entries:
121+
- Address: 0x1234
122+
- Address: 0x5678
123+
debug_line:
124+
- Length: 42
125+
Version: 2
126+
PrologueLength: 36
127+
MinInstLength: 1
128+
DefaultIsStmt: 1
129+
LineBase: 251
130+
LineRange: 14
131+
OpcodeBase: 13
132+
StandardOpcodeLengths: [ 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1 ]
133+
IncludeDirs:
134+
- '/tmp'
135+
Files:
136+
- Name: main.cpp
137+
DirIdx: 1
138+
ModTime: 0
139+
Length: 0
140+
)";
141+
142+
// Perform setup.
143+
llvm::Expected<TestFile> file = TestFile::fromYaml(yamldata);
144+
EXPECT_THAT_EXPECTED(file, llvm::Succeeded());
145+
auto module_sp = std::make_shared<Module>(file->moduleSpec());
146+
ASSERT_NE(module_sp, nullptr);
147+
auto object_file = module_sp->GetObjectFile();
148+
ASSERT_NE(object_file, nullptr);
149+
150+
// The debug map should be null for non-Mach-O (ELF) file.
151+
auto debug_map =
152+
SymbolFileDWARFDebugMap::CreateInstance(object_file->shared_from_this());
153+
ASSERT_EQ(debug_map, nullptr);
154+
}

0 commit comments

Comments
 (0)