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