LocateSymbolFile.cpp
14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
//===-- LocateSymbolFile.cpp ------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "lldb/Symbol/LocateSymbolFile.h"
#include "lldb/Core/ModuleList.h"
#include "lldb/Core/ModuleSpec.h"
#include "lldb/Host/FileSystem.h"
#include "lldb/Symbol/ObjectFile.h"
#include "lldb/Utility/ArchSpec.h"
#include "lldb/Utility/DataBuffer.h"
#include "lldb/Utility/DataExtractor.h"
#include "lldb/Utility/Log.h"
#include "lldb/Utility/StreamString.h"
#include "lldb/Utility/Timer.h"
#include "lldb/Utility/UUID.h"
#include "llvm/Support/FileSystem.h"
// From MacOSX system header "mach/machine.h"
typedef int cpu_type_t;
typedef int cpu_subtype_t;
using namespace lldb;
using namespace lldb_private;
#if defined(__APPLE__)
// Forward declaration of method defined in source/Host/macosx/Symbols.cpp
int LocateMacOSXFilesUsingDebugSymbols(const ModuleSpec &module_spec,
ModuleSpec &return_module_spec);
#else
int LocateMacOSXFilesUsingDebugSymbols(const ModuleSpec &module_spec,
ModuleSpec &return_module_spec) {
// Cannot find MacOSX files using debug symbols on non MacOSX.
return 0;
}
#endif
static bool FileAtPathContainsArchAndUUID(const FileSpec &file_fspec,
const ArchSpec *arch,
const lldb_private::UUID *uuid) {
ModuleSpecList module_specs;
if (ObjectFile::GetModuleSpecifications(file_fspec, 0, 0, module_specs)) {
ModuleSpec spec;
for (size_t i = 0; i < module_specs.GetSize(); ++i) {
bool got_spec = module_specs.GetModuleSpecAtIndex(i, spec);
UNUSED_IF_ASSERT_DISABLED(got_spec);
assert(got_spec);
if ((uuid == nullptr || (spec.GetUUIDPtr() && spec.GetUUID() == *uuid)) &&
(arch == nullptr ||
(spec.GetArchitecturePtr() &&
spec.GetArchitecture().IsCompatibleMatch(*arch)))) {
return true;
}
}
}
return false;
}
// Given a binary exec_fspec, and a ModuleSpec with an architecture/uuid,
// return true if there is a matching dSYM bundle next to the exec_fspec,
// and return that value in dsym_fspec.
// If there is a .dSYM.yaa compressed archive next to the exec_fspec,
// call through Symbols::DownloadObjectAndSymbolFile to download the
// expanded/uncompressed dSYM and return that filepath in dsym_fspec.
static bool LookForDsymNextToExecutablePath(const ModuleSpec &mod_spec,
const FileSpec &exec_fspec,
FileSpec &dsym_fspec) {
ConstString filename = exec_fspec.GetFilename();
FileSpec dsym_directory = exec_fspec;
dsym_directory.RemoveLastPathComponent();
std::string dsym_filename = filename.AsCString();
dsym_filename += ".dSYM";
dsym_directory.AppendPathComponent(dsym_filename);
dsym_directory.AppendPathComponent("Contents");
dsym_directory.AppendPathComponent("Resources");
dsym_directory.AppendPathComponent("DWARF");
if (FileSystem::Instance().Exists(dsym_directory)) {
// See if the binary name exists in the dSYM DWARF
// subdir.
dsym_fspec = dsym_directory;
dsym_fspec.AppendPathComponent(filename.AsCString());
if (FileSystem::Instance().Exists(dsym_fspec) &&
FileAtPathContainsArchAndUUID(dsym_fspec, mod_spec.GetArchitecturePtr(),
mod_spec.GetUUIDPtr())) {
return true;
}
// See if we have "../CF.framework" - so we'll look for
// CF.framework.dSYM/Contents/Resources/DWARF/CF
// We need to drop the last suffix after '.' to match
// 'CF' in the DWARF subdir.
std::string binary_name(filename.AsCString());
auto last_dot = binary_name.find_last_of('.');
if (last_dot != std::string::npos) {
binary_name.erase(last_dot);
dsym_fspec = dsym_directory;
dsym_fspec.AppendPathComponent(binary_name);
if (FileSystem::Instance().Exists(dsym_fspec) &&
FileAtPathContainsArchAndUUID(dsym_fspec,
mod_spec.GetArchitecturePtr(),
mod_spec.GetUUIDPtr())) {
return true;
}
}
}
// See if we have a .dSYM.yaa next to this executable path.
FileSpec dsym_yaa_fspec = exec_fspec;
dsym_yaa_fspec.RemoveLastPathComponent();
std::string dsym_yaa_filename = filename.AsCString();
dsym_yaa_filename += ".dSYM.yaa";
dsym_yaa_fspec.AppendPathComponent(dsym_yaa_filename);
if (FileSystem::Instance().Exists(dsym_yaa_fspec)) {
ModuleSpec mutable_mod_spec = mod_spec;
if (Symbols::DownloadObjectAndSymbolFile(mutable_mod_spec, true) &&
FileSystem::Instance().Exists(mutable_mod_spec.GetSymbolFileSpec())) {
dsym_fspec = mutable_mod_spec.GetSymbolFileSpec();
return true;
}
}
return false;
}
// Given a ModuleSpec with a FileSpec and optionally uuid/architecture
// filled in, look for a .dSYM bundle next to that binary. Returns true
// if a .dSYM bundle is found, and that path is returned in the dsym_fspec
// FileSpec.
//
// This routine looks a few directory layers above the given exec_path -
// exec_path might be /System/Library/Frameworks/CF.framework/CF and the
// dSYM might be /System/Library/Frameworks/CF.framework.dSYM.
//
// If there is a .dSYM.yaa compressed archive found next to the binary,
// we'll call DownloadObjectAndSymbolFile to expand it into a plain .dSYM
static bool LocateDSYMInVincinityOfExecutable(const ModuleSpec &module_spec,
FileSpec &dsym_fspec) {
Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST);
const FileSpec &exec_fspec = module_spec.GetFileSpec();
if (exec_fspec) {
if (::LookForDsymNextToExecutablePath(module_spec, exec_fspec,
dsym_fspec)) {
if (log) {
LLDB_LOGF(log, "dSYM with matching UUID & arch found at %s",
dsym_fspec.GetPath().c_str());
}
return true;
} else {
FileSpec parent_dirs = exec_fspec;
// Remove the binary name from the FileSpec
parent_dirs.RemoveLastPathComponent();
// Add a ".dSYM" name to each directory component of the path,
// stripping off components. e.g. we may have a binary like
// /S/L/F/Foundation.framework/Versions/A/Foundation and
// /S/L/F/Foundation.framework.dSYM
//
// so we'll need to start with
// /S/L/F/Foundation.framework/Versions/A, add the .dSYM part to the
// "A", and if that doesn't exist, strip off the "A" and try it again
// with "Versions", etc., until we find a dSYM bundle or we've
// stripped off enough path components that there's no need to
// continue.
for (int i = 0; i < 4; i++) {
// Does this part of the path have a "." character - could it be a
// bundle's top level directory?
const char *fn = parent_dirs.GetFilename().AsCString();
if (fn == nullptr)
break;
if (::strchr(fn, '.') != nullptr) {
if (::LookForDsymNextToExecutablePath(module_spec, parent_dirs,
dsym_fspec)) {
if (log) {
LLDB_LOGF(log, "dSYM with matching UUID & arch found at %s",
dsym_fspec.GetPath().c_str());
}
return true;
}
}
parent_dirs.RemoveLastPathComponent();
}
}
}
dsym_fspec.Clear();
return false;
}
static FileSpec LocateExecutableSymbolFileDsym(const ModuleSpec &module_spec) {
const FileSpec *exec_fspec = module_spec.GetFileSpecPtr();
const ArchSpec *arch = module_spec.GetArchitecturePtr();
const UUID *uuid = module_spec.GetUUIDPtr();
static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
Timer scoped_timer(
func_cat,
"LocateExecutableSymbolFileDsym (file = %s, arch = %s, uuid = %p)",
exec_fspec ? exec_fspec->GetFilename().AsCString("<NULL>") : "<NULL>",
arch ? arch->GetArchitectureName() : "<NULL>", (const void *)uuid);
FileSpec symbol_fspec;
ModuleSpec dsym_module_spec;
// First try and find the dSYM in the same directory as the executable or in
// an appropriate parent directory
if (!LocateDSYMInVincinityOfExecutable(module_spec, symbol_fspec)) {
// We failed to easily find the dSYM above, so use DebugSymbols
LocateMacOSXFilesUsingDebugSymbols(module_spec, dsym_module_spec);
} else {
dsym_module_spec.GetSymbolFileSpec() = symbol_fspec;
}
return dsym_module_spec.GetSymbolFileSpec();
}
ModuleSpec Symbols::LocateExecutableObjectFile(const ModuleSpec &module_spec) {
ModuleSpec result;
const FileSpec &exec_fspec = module_spec.GetFileSpec();
const ArchSpec *arch = module_spec.GetArchitecturePtr();
const UUID *uuid = module_spec.GetUUIDPtr();
static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
Timer scoped_timer(
func_cat, "LocateExecutableObjectFile (file = %s, arch = %s, uuid = %p)",
exec_fspec ? exec_fspec.GetFilename().AsCString("<NULL>") : "<NULL>",
arch ? arch->GetArchitectureName() : "<NULL>", (const void *)uuid);
ModuleSpecList module_specs;
ModuleSpec matched_module_spec;
if (exec_fspec &&
ObjectFile::GetModuleSpecifications(exec_fspec, 0, 0, module_specs) &&
module_specs.FindMatchingModuleSpec(module_spec, matched_module_spec)) {
result.GetFileSpec() = exec_fspec;
} else {
LocateMacOSXFilesUsingDebugSymbols(module_spec, result);
}
return result;
}
// Keep "symbols.enable-external-lookup" description in sync with this function.
FileSpec
Symbols::LocateExecutableSymbolFile(const ModuleSpec &module_spec,
const FileSpecList &default_search_paths) {
FileSpec symbol_file_spec = module_spec.GetSymbolFileSpec();
if (symbol_file_spec.IsAbsolute() &&
FileSystem::Instance().Exists(symbol_file_spec))
return symbol_file_spec;
FileSpecList debug_file_search_paths = default_search_paths;
// Add module directory.
FileSpec module_file_spec = module_spec.GetFileSpec();
// We keep the unresolved pathname if it fails.
FileSystem::Instance().ResolveSymbolicLink(module_file_spec,
module_file_spec);
ConstString file_dir = module_file_spec.GetDirectory();
{
FileSpec file_spec(file_dir.AsCString("."));
FileSystem::Instance().Resolve(file_spec);
debug_file_search_paths.AppendIfUnique(file_spec);
}
if (ModuleList::GetGlobalModuleListProperties().GetEnableExternalLookup()) {
// Add current working directory.
{
FileSpec file_spec(".");
FileSystem::Instance().Resolve(file_spec);
debug_file_search_paths.AppendIfUnique(file_spec);
}
#ifndef _WIN32
#if defined(__NetBSD__)
// Add /usr/libdata/debug directory.
{
FileSpec file_spec("/usr/libdata/debug");
FileSystem::Instance().Resolve(file_spec);
debug_file_search_paths.AppendIfUnique(file_spec);
}
#else
// Add /usr/lib/debug directory.
{
FileSpec file_spec("/usr/lib/debug");
FileSystem::Instance().Resolve(file_spec);
debug_file_search_paths.AppendIfUnique(file_spec);
}
#endif
#endif // _WIN32
}
std::string uuid_str;
const UUID &module_uuid = module_spec.GetUUID();
if (module_uuid.IsValid()) {
// Some debug files are stored in the .build-id directory like this:
// /usr/lib/debug/.build-id/ff/e7fe727889ad82bb153de2ad065b2189693315.debug
uuid_str = module_uuid.GetAsString("");
std::transform(uuid_str.begin(), uuid_str.end(), uuid_str.begin(),
::tolower);
uuid_str.insert(2, 1, '/');
uuid_str = uuid_str + ".debug";
}
size_t num_directories = debug_file_search_paths.GetSize();
for (size_t idx = 0; idx < num_directories; ++idx) {
FileSpec dirspec = debug_file_search_paths.GetFileSpecAtIndex(idx);
FileSystem::Instance().Resolve(dirspec);
if (!FileSystem::Instance().IsDirectory(dirspec))
continue;
std::vector<std::string> files;
std::string dirname = dirspec.GetPath();
if (!uuid_str.empty())
files.push_back(dirname + "/.build-id/" + uuid_str);
if (symbol_file_spec.GetFilename()) {
files.push_back(dirname + "/" +
symbol_file_spec.GetFilename().GetCString());
files.push_back(dirname + "/.debug/" +
symbol_file_spec.GetFilename().GetCString());
// Some debug files may stored in the module directory like this:
// /usr/lib/debug/usr/lib/library.so.debug
if (!file_dir.IsEmpty())
files.push_back(dirname + file_dir.AsCString() + "/" +
symbol_file_spec.GetFilename().GetCString());
}
const uint32_t num_files = files.size();
for (size_t idx_file = 0; idx_file < num_files; ++idx_file) {
const std::string &filename = files[idx_file];
FileSpec file_spec(filename);
FileSystem::Instance().Resolve(file_spec);
if (llvm::sys::fs::equivalent(file_spec.GetPath(),
module_file_spec.GetPath()))
continue;
if (FileSystem::Instance().Exists(file_spec)) {
lldb_private::ModuleSpecList specs;
const size_t num_specs =
ObjectFile::GetModuleSpecifications(file_spec, 0, 0, specs);
assert(num_specs <= 1 &&
"Symbol Vendor supports only a single architecture");
if (num_specs == 1) {
ModuleSpec mspec;
if (specs.GetModuleSpecAtIndex(0, mspec)) {
// Skip the uuids check if module_uuid is invalid. For example,
// this happens for *.dwp files since at the moment llvm-dwp
// doesn't output build ids, nor does binutils dwp.
if (!module_uuid.IsValid() || module_uuid == mspec.GetUUID())
return file_spec;
}
}
}
}
}
return LocateExecutableSymbolFileDsym(module_spec);
}
#if !defined(__APPLE__)
FileSpec Symbols::FindSymbolFileInBundle(const FileSpec &symfile_bundle,
const lldb_private::UUID *uuid,
const ArchSpec *arch) {
// FIXME
return FileSpec();
}
bool Symbols::DownloadObjectAndSymbolFile(ModuleSpec &module_spec,
bool force_lookup) {
// Fill in the module_spec.GetFileSpec() for the object file and/or the
// module_spec.GetSymbolFileSpec() for the debug symbols file.
return false;
}
#endif