[TableGen] Allow bit fields in SearchableTables.
[llvm-project.git] / lldb / source / Symbol / LocateSymbolFile.cpp
blob66ee7589ac604994c8efe3be5eac84c3d3eeea71
1 //===-- LocateSymbolFile.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 //===----------------------------------------------------------------------===//
9 #include "lldb/Symbol/LocateSymbolFile.h"
11 #include "lldb/Core/Debugger.h"
12 #include "lldb/Core/Module.h"
13 #include "lldb/Core/ModuleList.h"
14 #include "lldb/Core/ModuleSpec.h"
15 #include "lldb/Core/Progress.h"
16 #include "lldb/Host/FileSystem.h"
17 #include "lldb/Symbol/ObjectFile.h"
18 #include "lldb/Utility/ArchSpec.h"
19 #include "lldb/Utility/DataBuffer.h"
20 #include "lldb/Utility/DataExtractor.h"
21 #include "lldb/Utility/LLDBLog.h"
22 #include "lldb/Utility/Log.h"
23 #include "lldb/Utility/StreamString.h"
24 #include "lldb/Utility/Timer.h"
25 #include "lldb/Utility/UUID.h"
27 #include "llvm/ADT/SmallSet.h"
28 #include "llvm/Support/FileSystem.h"
29 #include "llvm/Support/ThreadPool.h"
31 // From MacOSX system header "mach/machine.h"
32 typedef int cpu_type_t;
33 typedef int cpu_subtype_t;
35 using namespace lldb;
36 using namespace lldb_private;
38 #if defined(__APPLE__)
40 // Forward declaration of method defined in source/Host/macosx/Symbols.cpp
41 int LocateMacOSXFilesUsingDebugSymbols(const ModuleSpec &module_spec,
42 ModuleSpec &return_module_spec);
44 #else
46 int LocateMacOSXFilesUsingDebugSymbols(const ModuleSpec &module_spec,
47 ModuleSpec &return_module_spec) {
48 // Cannot find MacOSX files using debug symbols on non MacOSX.
49 return 0;
52 #endif
54 static bool FileAtPathContainsArchAndUUID(const FileSpec &file_fspec,
55 const ArchSpec *arch,
56 const lldb_private::UUID *uuid) {
57 ModuleSpecList module_specs;
58 if (ObjectFile::GetModuleSpecifications(file_fspec, 0, 0, module_specs)) {
59 ModuleSpec spec;
60 for (size_t i = 0; i < module_specs.GetSize(); ++i) {
61 bool got_spec = module_specs.GetModuleSpecAtIndex(i, spec);
62 UNUSED_IF_ASSERT_DISABLED(got_spec);
63 assert(got_spec);
64 if ((uuid == nullptr || (spec.GetUUIDPtr() && spec.GetUUID() == *uuid)) &&
65 (arch == nullptr ||
66 (spec.GetArchitecturePtr() &&
67 spec.GetArchitecture().IsCompatibleMatch(*arch)))) {
68 return true;
72 return false;
75 // Given a binary exec_fspec, and a ModuleSpec with an architecture/uuid,
76 // return true if there is a matching dSYM bundle next to the exec_fspec,
77 // and return that value in dsym_fspec.
78 // If there is a .dSYM.yaa compressed archive next to the exec_fspec,
79 // call through Symbols::DownloadObjectAndSymbolFile to download the
80 // expanded/uncompressed dSYM and return that filepath in dsym_fspec.
82 static bool LookForDsymNextToExecutablePath(const ModuleSpec &mod_spec,
83 const FileSpec &exec_fspec,
84 FileSpec &dsym_fspec) {
85 ConstString filename = exec_fspec.GetFilename();
86 FileSpec dsym_directory = exec_fspec;
87 dsym_directory.RemoveLastPathComponent();
89 std::string dsym_filename = filename.AsCString();
90 dsym_filename += ".dSYM";
91 dsym_directory.AppendPathComponent(dsym_filename);
92 dsym_directory.AppendPathComponent("Contents");
93 dsym_directory.AppendPathComponent("Resources");
94 dsym_directory.AppendPathComponent("DWARF");
96 if (FileSystem::Instance().Exists(dsym_directory)) {
98 // See if the binary name exists in the dSYM DWARF
99 // subdir.
100 dsym_fspec = dsym_directory;
101 dsym_fspec.AppendPathComponent(filename.AsCString());
102 if (FileSystem::Instance().Exists(dsym_fspec) &&
103 FileAtPathContainsArchAndUUID(dsym_fspec, mod_spec.GetArchitecturePtr(),
104 mod_spec.GetUUIDPtr())) {
105 return true;
108 // See if we have "../CF.framework" - so we'll look for
109 // CF.framework.dSYM/Contents/Resources/DWARF/CF
110 // We need to drop the last suffix after '.' to match
111 // 'CF' in the DWARF subdir.
112 std::string binary_name(filename.AsCString());
113 auto last_dot = binary_name.find_last_of('.');
114 if (last_dot != std::string::npos) {
115 binary_name.erase(last_dot);
116 dsym_fspec = dsym_directory;
117 dsym_fspec.AppendPathComponent(binary_name);
118 if (FileSystem::Instance().Exists(dsym_fspec) &&
119 FileAtPathContainsArchAndUUID(dsym_fspec,
120 mod_spec.GetArchitecturePtr(),
121 mod_spec.GetUUIDPtr())) {
122 return true;
127 // See if we have a .dSYM.yaa next to this executable path.
128 FileSpec dsym_yaa_fspec = exec_fspec;
129 dsym_yaa_fspec.RemoveLastPathComponent();
130 std::string dsym_yaa_filename = filename.AsCString();
131 dsym_yaa_filename += ".dSYM.yaa";
132 dsym_yaa_fspec.AppendPathComponent(dsym_yaa_filename);
134 if (FileSystem::Instance().Exists(dsym_yaa_fspec)) {
135 ModuleSpec mutable_mod_spec = mod_spec;
136 Status error;
137 if (Symbols::DownloadObjectAndSymbolFile(mutable_mod_spec, error, true) &&
138 FileSystem::Instance().Exists(mutable_mod_spec.GetSymbolFileSpec())) {
139 dsym_fspec = mutable_mod_spec.GetSymbolFileSpec();
140 return true;
144 return false;
147 // Given a ModuleSpec with a FileSpec and optionally uuid/architecture
148 // filled in, look for a .dSYM bundle next to that binary. Returns true
149 // if a .dSYM bundle is found, and that path is returned in the dsym_fspec
150 // FileSpec.
152 // This routine looks a few directory layers above the given exec_path -
153 // exec_path might be /System/Library/Frameworks/CF.framework/CF and the
154 // dSYM might be /System/Library/Frameworks/CF.framework.dSYM.
156 // If there is a .dSYM.yaa compressed archive found next to the binary,
157 // we'll call DownloadObjectAndSymbolFile to expand it into a plain .dSYM
159 static bool LocateDSYMInVincinityOfExecutable(const ModuleSpec &module_spec,
160 FileSpec &dsym_fspec) {
161 Log *log = GetLog(LLDBLog::Host);
162 const FileSpec &exec_fspec = module_spec.GetFileSpec();
163 if (exec_fspec) {
164 if (::LookForDsymNextToExecutablePath(module_spec, exec_fspec,
165 dsym_fspec)) {
166 if (log) {
167 LLDB_LOGF(log, "dSYM with matching UUID & arch found at %s",
168 dsym_fspec.GetPath().c_str());
170 return true;
171 } else {
172 FileSpec parent_dirs = exec_fspec;
174 // Remove the binary name from the FileSpec
175 parent_dirs.RemoveLastPathComponent();
177 // Add a ".dSYM" name to each directory component of the path,
178 // stripping off components. e.g. we may have a binary like
179 // /S/L/F/Foundation.framework/Versions/A/Foundation and
180 // /S/L/F/Foundation.framework.dSYM
182 // so we'll need to start with
183 // /S/L/F/Foundation.framework/Versions/A, add the .dSYM part to the
184 // "A", and if that doesn't exist, strip off the "A" and try it again
185 // with "Versions", etc., until we find a dSYM bundle or we've
186 // stripped off enough path components that there's no need to
187 // continue.
189 for (int i = 0; i < 4; i++) {
190 // Does this part of the path have a "." character - could it be a
191 // bundle's top level directory?
192 const char *fn = parent_dirs.GetFilename().AsCString();
193 if (fn == nullptr)
194 break;
195 if (::strchr(fn, '.') != nullptr) {
196 if (::LookForDsymNextToExecutablePath(module_spec, parent_dirs,
197 dsym_fspec)) {
198 if (log) {
199 LLDB_LOGF(log, "dSYM with matching UUID & arch found at %s",
200 dsym_fspec.GetPath().c_str());
202 return true;
205 parent_dirs.RemoveLastPathComponent();
209 dsym_fspec.Clear();
210 return false;
213 static FileSpec LocateExecutableSymbolFileDsym(const ModuleSpec &module_spec) {
214 const FileSpec *exec_fspec = module_spec.GetFileSpecPtr();
215 const ArchSpec *arch = module_spec.GetArchitecturePtr();
216 const UUID *uuid = module_spec.GetUUIDPtr();
218 LLDB_SCOPED_TIMERF(
219 "LocateExecutableSymbolFileDsym (file = %s, arch = %s, uuid = %p)",
220 exec_fspec ? exec_fspec->GetFilename().AsCString("<NULL>") : "<NULL>",
221 arch ? arch->GetArchitectureName() : "<NULL>", (const void *)uuid);
223 FileSpec symbol_fspec;
224 ModuleSpec dsym_module_spec;
225 // First try and find the dSYM in the same directory as the executable or in
226 // an appropriate parent directory
227 if (!LocateDSYMInVincinityOfExecutable(module_spec, symbol_fspec)) {
228 // We failed to easily find the dSYM above, so use DebugSymbols
229 LocateMacOSXFilesUsingDebugSymbols(module_spec, dsym_module_spec);
230 } else {
231 dsym_module_spec.GetSymbolFileSpec() = symbol_fspec;
234 return dsym_module_spec.GetSymbolFileSpec();
237 ModuleSpec Symbols::LocateExecutableObjectFile(const ModuleSpec &module_spec) {
238 ModuleSpec result;
239 const FileSpec &exec_fspec = module_spec.GetFileSpec();
240 const ArchSpec *arch = module_spec.GetArchitecturePtr();
241 const UUID *uuid = module_spec.GetUUIDPtr();
242 LLDB_SCOPED_TIMERF(
243 "LocateExecutableObjectFile (file = %s, arch = %s, uuid = %p)",
244 exec_fspec ? exec_fspec.GetFilename().AsCString("<NULL>") : "<NULL>",
245 arch ? arch->GetArchitectureName() : "<NULL>", (const void *)uuid);
247 ModuleSpecList module_specs;
248 ModuleSpec matched_module_spec;
249 if (exec_fspec &&
250 ObjectFile::GetModuleSpecifications(exec_fspec, 0, 0, module_specs) &&
251 module_specs.FindMatchingModuleSpec(module_spec, matched_module_spec)) {
252 result.GetFileSpec() = exec_fspec;
253 } else {
254 LocateMacOSXFilesUsingDebugSymbols(module_spec, result);
257 return result;
260 // Keep "symbols.enable-external-lookup" description in sync with this function.
262 FileSpec
263 Symbols::LocateExecutableSymbolFile(const ModuleSpec &module_spec,
264 const FileSpecList &default_search_paths) {
265 FileSpec symbol_file_spec = module_spec.GetSymbolFileSpec();
266 if (symbol_file_spec.IsAbsolute() &&
267 FileSystem::Instance().Exists(symbol_file_spec))
268 return symbol_file_spec;
270 Progress progress(llvm::formatv(
271 "Locating external symbol file for {0}",
272 module_spec.GetFileSpec().GetFilename().AsCString("<Unknown>")));
274 FileSpecList debug_file_search_paths = default_search_paths;
276 // Add module directory.
277 FileSpec module_file_spec = module_spec.GetFileSpec();
278 // We keep the unresolved pathname if it fails.
279 FileSystem::Instance().ResolveSymbolicLink(module_file_spec,
280 module_file_spec);
282 ConstString file_dir = module_file_spec.GetDirectory();
284 FileSpec file_spec(file_dir.AsCString("."));
285 FileSystem::Instance().Resolve(file_spec);
286 debug_file_search_paths.AppendIfUnique(file_spec);
289 if (ModuleList::GetGlobalModuleListProperties().GetEnableExternalLookup()) {
291 // Add current working directory.
293 FileSpec file_spec(".");
294 FileSystem::Instance().Resolve(file_spec);
295 debug_file_search_paths.AppendIfUnique(file_spec);
298 #ifndef _WIN32
299 #if defined(__NetBSD__)
300 // Add /usr/libdata/debug directory.
302 FileSpec file_spec("/usr/libdata/debug");
303 FileSystem::Instance().Resolve(file_spec);
304 debug_file_search_paths.AppendIfUnique(file_spec);
306 #else
307 // Add /usr/lib/debug directory.
309 FileSpec file_spec("/usr/lib/debug");
310 FileSystem::Instance().Resolve(file_spec);
311 debug_file_search_paths.AppendIfUnique(file_spec);
313 #endif
314 #endif // _WIN32
317 std::string uuid_str;
318 const UUID &module_uuid = module_spec.GetUUID();
319 if (module_uuid.IsValid()) {
320 // Some debug files are stored in the .build-id directory like this:
321 // /usr/lib/debug/.build-id/ff/e7fe727889ad82bb153de2ad065b2189693315.debug
322 uuid_str = module_uuid.GetAsString("");
323 std::transform(uuid_str.begin(), uuid_str.end(), uuid_str.begin(),
324 ::tolower);
325 uuid_str.insert(2, 1, '/');
326 uuid_str = uuid_str + ".debug";
329 size_t num_directories = debug_file_search_paths.GetSize();
330 for (size_t idx = 0; idx < num_directories; ++idx) {
331 FileSpec dirspec = debug_file_search_paths.GetFileSpecAtIndex(idx);
332 FileSystem::Instance().Resolve(dirspec);
333 if (!FileSystem::Instance().IsDirectory(dirspec))
334 continue;
336 std::vector<std::string> files;
337 std::string dirname = dirspec.GetPath();
339 if (!uuid_str.empty())
340 files.push_back(dirname + "/.build-id/" + uuid_str);
341 if (symbol_file_spec.GetFilename()) {
342 files.push_back(dirname + "/" +
343 symbol_file_spec.GetFilename().GetCString());
344 files.push_back(dirname + "/.debug/" +
345 symbol_file_spec.GetFilename().GetCString());
347 // Some debug files may stored in the module directory like this:
348 // /usr/lib/debug/usr/lib/library.so.debug
349 if (!file_dir.IsEmpty())
350 files.push_back(dirname + file_dir.AsCString() + "/" +
351 symbol_file_spec.GetFilename().GetCString());
354 const uint32_t num_files = files.size();
355 for (size_t idx_file = 0; idx_file < num_files; ++idx_file) {
356 const std::string &filename = files[idx_file];
357 FileSpec file_spec(filename);
358 FileSystem::Instance().Resolve(file_spec);
360 if (llvm::sys::fs::equivalent(file_spec.GetPath(),
361 module_file_spec.GetPath()))
362 continue;
364 if (FileSystem::Instance().Exists(file_spec)) {
365 lldb_private::ModuleSpecList specs;
366 const size_t num_specs =
367 ObjectFile::GetModuleSpecifications(file_spec, 0, 0, specs);
368 ModuleSpec mspec;
369 bool valid_mspec = false;
370 if (num_specs == 2) {
371 // Special case to handle both i386 and i686 from ObjectFilePECOFF
372 ModuleSpec mspec2;
373 if (specs.GetModuleSpecAtIndex(0, mspec) &&
374 specs.GetModuleSpecAtIndex(1, mspec2) &&
375 mspec.GetArchitecture().GetTriple().isCompatibleWith(
376 mspec2.GetArchitecture().GetTriple())) {
377 valid_mspec = true;
380 if (!valid_mspec) {
381 assert(num_specs <= 1 &&
382 "Symbol Vendor supports only a single architecture");
383 if (num_specs == 1) {
384 if (specs.GetModuleSpecAtIndex(0, mspec)) {
385 valid_mspec = true;
389 if (valid_mspec) {
390 // Skip the uuids check if module_uuid is invalid. For example,
391 // this happens for *.dwp files since at the moment llvm-dwp
392 // doesn't output build ids, nor does binutils dwp.
393 if (!module_uuid.IsValid() || module_uuid == mspec.GetUUID())
394 return file_spec;
400 return LocateExecutableSymbolFileDsym(module_spec);
403 void Symbols::DownloadSymbolFileAsync(const UUID &uuid) {
404 if (!ModuleList::GetGlobalModuleListProperties().GetEnableBackgroundLookup())
405 return;
407 static llvm::SmallSet<UUID, 8> g_seen_uuids;
408 static std::mutex g_mutex;
409 Debugger::GetThreadPool().async([=]() {
411 std::lock_guard<std::mutex> guard(g_mutex);
412 if (g_seen_uuids.count(uuid))
413 return;
414 g_seen_uuids.insert(uuid);
417 Status error;
418 ModuleSpec module_spec;
419 module_spec.GetUUID() = uuid;
420 if (!Symbols::DownloadObjectAndSymbolFile(module_spec, error,
421 /*force_lookup=*/true,
422 /*copy_executable=*/false))
423 return;
425 if (error.Fail())
426 return;
428 Debugger::ReportSymbolChange(module_spec);
432 #if !defined(__APPLE__)
434 FileSpec Symbols::FindSymbolFileInBundle(const FileSpec &symfile_bundle,
435 const lldb_private::UUID *uuid,
436 const ArchSpec *arch) {
437 // FIXME
438 return FileSpec();
441 bool Symbols::DownloadObjectAndSymbolFile(ModuleSpec &module_spec,
442 Status &error, bool force_lookup,
443 bool copy_executable) {
444 // Fill in the module_spec.GetFileSpec() for the object file and/or the
445 // module_spec.GetSymbolFileSpec() for the debug symbols file.
446 return false;
449 #endif