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