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/ModuleSpec.h"
12 #include "lldb/Host/FileSystem.h"
13 #include "lldb/Symbol/ObjectFile.h"
14 #include "lldb/Target/Target.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 Symbols::LocateExecutableSymbolFile(const ModuleSpec &module_spec) {
256   FileSpec symbol_file_spec = module_spec.GetSymbolFileSpec();
257   if (symbol_file_spec.IsAbsolute() &&
258       FileSystem::Instance().Exists(symbol_file_spec))
259     return symbol_file_spec;
260 
261   const char *symbol_filename = symbol_file_spec.GetFilename().AsCString();
262   if (symbol_filename && symbol_filename[0]) {
263     FileSpecList debug_file_search_paths(
264         Target::GetDefaultDebugFileSearchPaths());
265 
266     // Add module directory.
267     FileSpec module_file_spec = module_spec.GetFileSpec();
268     // We keep the unresolved pathname if it fails.
269     FileSystem::Instance().ResolveSymbolicLink(module_file_spec,
270                                                module_file_spec);
271 
272     const ConstString &file_dir = module_file_spec.GetDirectory();
273     {
274       FileSpec file_spec(file_dir.AsCString("."));
275       FileSystem::Instance().Resolve(file_spec);
276       debug_file_search_paths.AppendIfUnique(file_spec);
277     }
278 
279     if (ModuleList::GetGlobalModuleListProperties().GetEnableExternalLookup()) {
280 
281       // Add current working directory.
282       {
283         FileSpec file_spec(".");
284         FileSystem::Instance().Resolve(file_spec);
285         debug_file_search_paths.AppendIfUnique(file_spec);
286       }
287 
288 #ifndef _WIN32
289 #if defined(__NetBSD__)
290       // Add /usr/libdata/debug directory.
291       {
292         FileSpec file_spec("/usr/libdata/debug");
293         FileSystem::Instance().Resolve(file_spec);
294         debug_file_search_paths.AppendIfUnique(file_spec);
295       }
296 #else
297       // Add /usr/lib/debug directory.
298       {
299         FileSpec file_spec("/usr/lib/debug");
300         FileSystem::Instance().Resolve(file_spec);
301         debug_file_search_paths.AppendIfUnique(file_spec);
302       }
303 #endif
304 #endif // _WIN32
305     }
306 
307     std::string uuid_str;
308     const UUID &module_uuid = module_spec.GetUUID();
309     if (module_uuid.IsValid()) {
310       // Some debug files are stored in the .build-id directory like this:
311       //   /usr/lib/debug/.build-id/ff/e7fe727889ad82bb153de2ad065b2189693315.debug
312       uuid_str = module_uuid.GetAsString("");
313       std::transform(uuid_str.begin(), uuid_str.end(), uuid_str.begin(),
314                      ::tolower);
315       uuid_str.insert(2, 1, '/');
316       uuid_str = uuid_str + ".debug";
317     }
318 
319     size_t num_directories = debug_file_search_paths.GetSize();
320     for (size_t idx = 0; idx < num_directories; ++idx) {
321       FileSpec dirspec = debug_file_search_paths.GetFileSpecAtIndex(idx);
322       FileSystem::Instance().Resolve(dirspec);
323       if (!FileSystem::Instance().IsDirectory(dirspec))
324         continue;
325 
326       std::vector<std::string> files;
327       std::string dirname = dirspec.GetPath();
328 
329       files.push_back(dirname + "/" + symbol_filename);
330       files.push_back(dirname + "/.debug/" + symbol_filename);
331       files.push_back(dirname + "/.build-id/" + uuid_str);
332 
333       // Some debug files may stored in the module directory like this:
334       //   /usr/lib/debug/usr/lib/library.so.debug
335       if (!file_dir.IsEmpty())
336         files.push_back(dirname + file_dir.AsCString() + "/" + symbol_filename);
337 
338       const uint32_t num_files = files.size();
339       for (size_t idx_file = 0; idx_file < num_files; ++idx_file) {
340         const std::string &filename = files[idx_file];
341         FileSpec file_spec(filename);
342         FileSystem::Instance().Resolve(file_spec);
343 
344         if (llvm::sys::fs::equivalent(file_spec.GetPath(),
345                                       module_file_spec.GetPath()))
346           continue;
347 
348         if (FileSystem::Instance().Exists(file_spec)) {
349           lldb_private::ModuleSpecList specs;
350           const size_t num_specs =
351               ObjectFile::GetModuleSpecifications(file_spec, 0, 0, specs);
352           assert(num_specs <= 1 &&
353                  "Symbol Vendor supports only a single architecture");
354           if (num_specs == 1) {
355             ModuleSpec mspec;
356             if (specs.GetModuleSpecAtIndex(0, mspec)) {
357               // Skip the uuids check if module_uuid is invalid. For example,
358               // this happens for *.dwp files since at the moment llvm-dwp
359               // doesn't output build ids, nor does binutils dwp.
360               if (!module_uuid.IsValid() || module_uuid == mspec.GetUUID())
361                 return file_spec;
362             }
363           }
364         }
365       }
366     }
367   }
368 
369   return LocateExecutableSymbolFileDsym(module_spec);
370 }
371 
372 #if !defined(__APPLE__)
373 
374 FileSpec Symbols::FindSymbolFileInBundle(const FileSpec &symfile_bundle,
375                                          const lldb_private::UUID *uuid,
376                                          const ArchSpec *arch) {
377   // FIXME
378   return FileSpec();
379 }
380 
381 bool Symbols::DownloadObjectAndSymbolFile(ModuleSpec &module_spec,
382                                           bool force_lookup) {
383   // Fill in the module_spec.GetFileSpec() for the object file and/or the
384   // module_spec.GetSymbolFileSpec() for the debug symbols file.
385   return false;
386 }
387 
388 #endif
389