1 //===-- LocateSymbolFileMacOSX.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 <dirent.h>
12 #include <dlfcn.h>
13 #include <pwd.h>
14 
15 #include <CoreFoundation/CoreFoundation.h>
16 
17 #include "Host/macosx/cfcpp/CFCBundle.h"
18 #include "Host/macosx/cfcpp/CFCData.h"
19 #include "Host/macosx/cfcpp/CFCReleaser.h"
20 #include "Host/macosx/cfcpp/CFCString.h"
21 #include "lldb/Core/ModuleList.h"
22 #include "lldb/Core/ModuleSpec.h"
23 #include "lldb/Host/Host.h"
24 #include "lldb/Symbol/ObjectFile.h"
25 #include "lldb/Utility/ArchSpec.h"
26 #include "lldb/Utility/DataBuffer.h"
27 #include "lldb/Utility/DataExtractor.h"
28 #include "lldb/Utility/Endian.h"
29 #include "lldb/Utility/LLDBLog.h"
30 #include "lldb/Utility/Log.h"
31 #include "lldb/Utility/StreamString.h"
32 #include "lldb/Utility/Timer.h"
33 #include "lldb/Utility/UUID.h"
34 #include "mach/machine.h"
35 
36 #include "llvm/ADT/ScopeExit.h"
37 #include "llvm/Support/FileSystem.h"
38 
39 using namespace lldb;
40 using namespace lldb_private;
41 
42 static CFURLRef (*g_dlsym_DBGCopyFullDSYMURLForUUID)(
43     CFUUIDRef uuid, CFURLRef exec_url) = nullptr;
44 static CFDictionaryRef (*g_dlsym_DBGCopyDSYMPropertyLists)(CFURLRef dsym_url) =
45     nullptr;
46 
47 int LocateMacOSXFilesUsingDebugSymbols(const ModuleSpec &module_spec,
48                                        ModuleSpec &return_module_spec) {
49   Log *log = GetLog(LLDBLog::Host);
50   if (!ModuleList::GetGlobalModuleListProperties().GetEnableExternalLookup()) {
51     LLDB_LOGF(log, "Spotlight lookup for .dSYM bundles is disabled.");
52     return 0;
53   }
54 
55   return_module_spec = module_spec;
56   return_module_spec.GetFileSpec().Clear();
57   return_module_spec.GetSymbolFileSpec().Clear();
58 
59   const UUID *uuid = module_spec.GetUUIDPtr();
60   const ArchSpec *arch = module_spec.GetArchitecturePtr();
61 
62   int items_found = 0;
63 
64   if (g_dlsym_DBGCopyFullDSYMURLForUUID == nullptr ||
65       g_dlsym_DBGCopyDSYMPropertyLists == nullptr) {
66     void *handle = dlopen(
67         "/System/Library/PrivateFrameworks/DebugSymbols.framework/DebugSymbols",
68         RTLD_LAZY | RTLD_LOCAL);
69     if (handle) {
70       g_dlsym_DBGCopyFullDSYMURLForUUID =
71           (CFURLRef(*)(CFUUIDRef, CFURLRef))dlsym(handle,
72                                                   "DBGCopyFullDSYMURLForUUID");
73       g_dlsym_DBGCopyDSYMPropertyLists = (CFDictionaryRef(*)(CFURLRef))dlsym(
74           handle, "DBGCopyDSYMPropertyLists");
75     }
76   }
77 
78   if (g_dlsym_DBGCopyFullDSYMURLForUUID == nullptr ||
79       g_dlsym_DBGCopyDSYMPropertyLists == nullptr) {
80     return items_found;
81   }
82 
83   if (uuid && uuid->IsValid()) {
84     // Try and locate the dSYM file using DebugSymbols first
85     llvm::ArrayRef<uint8_t> module_uuid = uuid->GetBytes();
86     if (module_uuid.size() == 16) {
87       CFCReleaser<CFUUIDRef> module_uuid_ref(::CFUUIDCreateWithBytes(
88           NULL, module_uuid[0], module_uuid[1], module_uuid[2], module_uuid[3],
89           module_uuid[4], module_uuid[5], module_uuid[6], module_uuid[7],
90           module_uuid[8], module_uuid[9], module_uuid[10], module_uuid[11],
91           module_uuid[12], module_uuid[13], module_uuid[14], module_uuid[15]));
92 
93       if (module_uuid_ref.get()) {
94         CFCReleaser<CFURLRef> exec_url;
95         const FileSpec *exec_fspec = module_spec.GetFileSpecPtr();
96         if (exec_fspec) {
97           char exec_cf_path[PATH_MAX];
98           if (exec_fspec->GetPath(exec_cf_path, sizeof(exec_cf_path)))
99             exec_url.reset(::CFURLCreateFromFileSystemRepresentation(
100                 NULL, (const UInt8 *)exec_cf_path, strlen(exec_cf_path),
101                 FALSE));
102         }
103 
104         CFCReleaser<CFURLRef> dsym_url(g_dlsym_DBGCopyFullDSYMURLForUUID(
105             module_uuid_ref.get(), exec_url.get()));
106         char path[PATH_MAX];
107 
108         if (dsym_url.get()) {
109           if (::CFURLGetFileSystemRepresentation(
110                   dsym_url.get(), true, (UInt8 *)path, sizeof(path) - 1)) {
111             if (log) {
112               LLDB_LOGF(log,
113                         "DebugSymbols framework returned dSYM path of %s for "
114                         "UUID %s -- looking for the dSYM",
115                         path, uuid->GetAsString().c_str());
116             }
117             FileSpec dsym_filespec(path);
118             if (path[0] == '~')
119               FileSystem::Instance().Resolve(dsym_filespec);
120 
121             if (FileSystem::Instance().IsDirectory(dsym_filespec)) {
122               dsym_filespec =
123                   Symbols::FindSymbolFileInBundle(dsym_filespec, uuid, arch);
124               ++items_found;
125             } else {
126               ++items_found;
127             }
128             return_module_spec.GetSymbolFileSpec() = dsym_filespec;
129           }
130 
131           bool success = false;
132           if (log) {
133             if (::CFURLGetFileSystemRepresentation(
134                     dsym_url.get(), true, (UInt8 *)path, sizeof(path) - 1)) {
135               LLDB_LOGF(log,
136                         "DebugSymbols framework returned dSYM path of %s for "
137                         "UUID %s -- looking for an exec file",
138                         path, uuid->GetAsString().c_str());
139             }
140           }
141 
142           CFCReleaser<CFDictionaryRef> dict(
143               g_dlsym_DBGCopyDSYMPropertyLists(dsym_url.get()));
144           CFDictionaryRef uuid_dict = NULL;
145           if (dict.get()) {
146             CFCString uuid_cfstr(uuid->GetAsString().c_str());
147             uuid_dict = static_cast<CFDictionaryRef>(
148                 ::CFDictionaryGetValue(dict.get(), uuid_cfstr.get()));
149           }
150           if (uuid_dict) {
151             CFStringRef exec_cf_path =
152                 static_cast<CFStringRef>(::CFDictionaryGetValue(
153                     uuid_dict, CFSTR("DBGSymbolRichExecutable")));
154             if (exec_cf_path && ::CFStringGetFileSystemRepresentation(
155                                     exec_cf_path, path, sizeof(path))) {
156               if (log) {
157                 LLDB_LOGF(log, "plist bundle has exec path of %s for UUID %s",
158                           path, uuid->GetAsString().c_str());
159               }
160               ++items_found;
161               FileSpec exec_filespec(path);
162               if (path[0] == '~')
163                 FileSystem::Instance().Resolve(exec_filespec);
164               if (FileSystem::Instance().Exists(exec_filespec)) {
165                 success = true;
166                 return_module_spec.GetFileSpec() = exec_filespec;
167               }
168             }
169           }
170 
171           if (!success) {
172             // No dictionary, check near the dSYM bundle for an executable that
173             // matches...
174             if (::CFURLGetFileSystemRepresentation(
175                     dsym_url.get(), true, (UInt8 *)path, sizeof(path) - 1)) {
176               char *dsym_extension_pos = ::strstr(path, ".dSYM");
177               if (dsym_extension_pos) {
178                 *dsym_extension_pos = '\0';
179                 if (log) {
180                   LLDB_LOGF(log,
181                             "Looking for executable binary next to dSYM "
182                             "bundle with name with name %s",
183                             path);
184                 }
185                 FileSpec file_spec(path);
186                 FileSystem::Instance().Resolve(file_spec);
187                 ModuleSpecList module_specs;
188                 ModuleSpec matched_module_spec;
189                 using namespace llvm::sys::fs;
190                 switch (get_file_type(file_spec.GetPath())) {
191 
192                 case file_type::directory_file: // Bundle directory?
193                 {
194                   CFCBundle bundle(path);
195                   CFCReleaser<CFURLRef> bundle_exe_url(
196                       bundle.CopyExecutableURL());
197                   if (bundle_exe_url.get()) {
198                     if (::CFURLGetFileSystemRepresentation(bundle_exe_url.get(),
199                                                            true, (UInt8 *)path,
200                                                            sizeof(path) - 1)) {
201                       FileSpec bundle_exe_file_spec(path);
202                       FileSystem::Instance().Resolve(bundle_exe_file_spec);
203                       if (ObjectFile::GetModuleSpecifications(
204                               bundle_exe_file_spec, 0, 0, module_specs) &&
205                           module_specs.FindMatchingModuleSpec(
206                               module_spec, matched_module_spec))
207 
208                       {
209                         ++items_found;
210                         return_module_spec.GetFileSpec() = bundle_exe_file_spec;
211                         if (log) {
212                           LLDB_LOGF(log,
213                                     "Executable binary %s next to dSYM is "
214                                     "compatible; using",
215                                     path);
216                         }
217                       }
218                     }
219                   }
220                 } break;
221 
222                 case file_type::fifo_file:      // Forget pipes
223                 case file_type::socket_file:    // We can't process socket files
224                 case file_type::file_not_found: // File doesn't exist...
225                 case file_type::status_error:
226                   break;
227 
228                 case file_type::type_unknown:
229                 case file_type::regular_file:
230                 case file_type::symlink_file:
231                 case file_type::block_file:
232                 case file_type::character_file:
233                   if (ObjectFile::GetModuleSpecifications(file_spec, 0, 0,
234                                                           module_specs) &&
235                       module_specs.FindMatchingModuleSpec(module_spec,
236                                                           matched_module_spec))
237 
238                   {
239                     ++items_found;
240                     return_module_spec.GetFileSpec() = file_spec;
241                     if (log) {
242                       LLDB_LOGF(log,
243                                 "Executable binary %s next to dSYM is "
244                                 "compatible; using",
245                                 path);
246                     }
247                   }
248                   break;
249                 }
250               }
251             }
252           }
253         }
254       }
255     }
256   }
257 
258   return items_found;
259 }
260 
261 FileSpec Symbols::FindSymbolFileInBundle(const FileSpec &dsym_bundle_fspec,
262                                          const lldb_private::UUID *uuid,
263                                          const ArchSpec *arch) {
264   std::string dsym_bundle_path = dsym_bundle_fspec.GetPath();
265   llvm::SmallString<128> buffer(dsym_bundle_path);
266   llvm::sys::path::append(buffer, "Contents", "Resources", "DWARF");
267 
268   std::error_code EC;
269   llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> vfs =
270       FileSystem::Instance().GetVirtualFileSystem();
271   llvm::vfs::recursive_directory_iterator Iter(*vfs, buffer.str(), EC);
272   llvm::vfs::recursive_directory_iterator End;
273   for (; Iter != End && !EC; Iter.increment(EC)) {
274     llvm::ErrorOr<llvm::vfs::Status> Status = vfs->status(Iter->path());
275     if (Status->isDirectory())
276       continue;
277 
278     FileSpec dsym_fspec(Iter->path());
279     ModuleSpecList module_specs;
280     if (ObjectFile::GetModuleSpecifications(dsym_fspec, 0, 0, module_specs)) {
281       ModuleSpec spec;
282       for (size_t i = 0; i < module_specs.GetSize(); ++i) {
283         bool got_spec = module_specs.GetModuleSpecAtIndex(i, spec);
284         assert(got_spec); // The call has side-effects so can't be inlined.
285         UNUSED_IF_ASSERT_DISABLED(got_spec);
286         if ((uuid == nullptr ||
287              (spec.GetUUIDPtr() && spec.GetUUID() == *uuid)) &&
288             (arch == nullptr ||
289              (spec.GetArchitecturePtr() &&
290               spec.GetArchitecture().IsCompatibleMatch(*arch)))) {
291           return dsym_fspec;
292         }
293       }
294     }
295   }
296 
297   return {};
298 }
299 
300 static bool GetModuleSpecInfoFromUUIDDictionary(CFDictionaryRef uuid_dict,
301                                                 ModuleSpec &module_spec) {
302   Log *log = GetLog(LLDBLog::Host);
303   bool success = false;
304   if (uuid_dict != NULL && CFGetTypeID(uuid_dict) == CFDictionaryGetTypeID()) {
305     std::string str;
306     CFStringRef cf_str;
307     CFDictionaryRef cf_dict;
308 
309     cf_str = (CFStringRef)CFDictionaryGetValue(
310         (CFDictionaryRef)uuid_dict, CFSTR("DBGSymbolRichExecutable"));
311     if (cf_str && CFGetTypeID(cf_str) == CFStringGetTypeID()) {
312       if (CFCString::FileSystemRepresentation(cf_str, str)) {
313         module_spec.GetFileSpec().SetFile(str.c_str(), FileSpec::Style::native);
314         FileSystem::Instance().Resolve(module_spec.GetFileSpec());
315         if (log) {
316           LLDB_LOGF(log,
317                     "From dsymForUUID plist: Symbol rich executable is at '%s'",
318                     str.c_str());
319         }
320       }
321     }
322 
323     cf_str = (CFStringRef)CFDictionaryGetValue((CFDictionaryRef)uuid_dict,
324                                                CFSTR("DBGDSYMPath"));
325     if (cf_str && CFGetTypeID(cf_str) == CFStringGetTypeID()) {
326       if (CFCString::FileSystemRepresentation(cf_str, str)) {
327         module_spec.GetSymbolFileSpec().SetFile(str.c_str(),
328                                                 FileSpec::Style::native);
329         FileSystem::Instance().Resolve(module_spec.GetFileSpec());
330         success = true;
331         if (log) {
332           LLDB_LOGF(log, "From dsymForUUID plist: dSYM is at '%s'",
333                     str.c_str());
334         }
335       }
336     }
337 
338     std::string DBGBuildSourcePath;
339     std::string DBGSourcePath;
340 
341     // If DBGVersion 1 or DBGVersion missing, ignore DBGSourcePathRemapping.
342     // If DBGVersion 2, strip last two components of path remappings from
343     //                  entries to fix an issue with a specific set of
344     //                  DBGSourcePathRemapping entries that lldb worked
345     //                  with.
346     // If DBGVersion 3, trust & use the source path remappings as-is.
347     //
348     cf_dict = (CFDictionaryRef)CFDictionaryGetValue(
349         (CFDictionaryRef)uuid_dict, CFSTR("DBGSourcePathRemapping"));
350     if (cf_dict && CFGetTypeID(cf_dict) == CFDictionaryGetTypeID()) {
351       // If we see DBGVersion with a value of 2 or higher, this is a new style
352       // DBGSourcePathRemapping dictionary
353       bool new_style_source_remapping_dictionary = false;
354       bool do_truncate_remapping_names = false;
355       std::string original_DBGSourcePath_value = DBGSourcePath;
356       cf_str = (CFStringRef)CFDictionaryGetValue((CFDictionaryRef)uuid_dict,
357                                                  CFSTR("DBGVersion"));
358       if (cf_str && CFGetTypeID(cf_str) == CFStringGetTypeID()) {
359         std::string version;
360         CFCString::FileSystemRepresentation(cf_str, version);
361         if (!version.empty() && isdigit(version[0])) {
362           int version_number = atoi(version.c_str());
363           if (version_number > 1) {
364             new_style_source_remapping_dictionary = true;
365           }
366           if (version_number == 2) {
367             do_truncate_remapping_names = true;
368           }
369         }
370       }
371 
372       CFIndex kv_pair_count = CFDictionaryGetCount((CFDictionaryRef)uuid_dict);
373       if (kv_pair_count > 0) {
374         CFStringRef *keys =
375             (CFStringRef *)malloc(kv_pair_count * sizeof(CFStringRef));
376         CFStringRef *values =
377             (CFStringRef *)malloc(kv_pair_count * sizeof(CFStringRef));
378         if (keys != nullptr && values != nullptr) {
379           CFDictionaryGetKeysAndValues((CFDictionaryRef)uuid_dict,
380                                        (const void **)keys,
381                                        (const void **)values);
382         }
383         for (CFIndex i = 0; i < kv_pair_count; i++) {
384           DBGBuildSourcePath.clear();
385           DBGSourcePath.clear();
386           if (keys[i] && CFGetTypeID(keys[i]) == CFStringGetTypeID()) {
387             CFCString::FileSystemRepresentation(keys[i], DBGBuildSourcePath);
388           }
389           if (values[i] && CFGetTypeID(values[i]) == CFStringGetTypeID()) {
390             CFCString::FileSystemRepresentation(values[i], DBGSourcePath);
391           }
392           if (!DBGBuildSourcePath.empty() && !DBGSourcePath.empty()) {
393             // In the "old style" DBGSourcePathRemapping dictionary, the
394             // DBGSourcePath values (the "values" half of key-value path pairs)
395             // were wrong.  Ignore them and use the universal DBGSourcePath
396             // string from earlier.
397             if (new_style_source_remapping_dictionary &&
398                 !original_DBGSourcePath_value.empty()) {
399               DBGSourcePath = original_DBGSourcePath_value;
400             }
401             if (DBGSourcePath[0] == '~') {
402               FileSpec resolved_source_path(DBGSourcePath.c_str());
403               FileSystem::Instance().Resolve(resolved_source_path);
404               DBGSourcePath = resolved_source_path.GetPath();
405             }
406             // With version 2 of DBGSourcePathRemapping, we can chop off the
407             // last two filename parts from the source remapping and get a more
408             // general source remapping that still works. Add this as another
409             // option in addition to the full source path remap.
410             module_spec.GetSourceMappingList().Append(DBGBuildSourcePath,
411                                                       DBGSourcePath, true);
412             if (do_truncate_remapping_names) {
413               FileSpec build_path(DBGBuildSourcePath.c_str());
414               FileSpec source_path(DBGSourcePath.c_str());
415               build_path.RemoveLastPathComponent();
416               build_path.RemoveLastPathComponent();
417               source_path.RemoveLastPathComponent();
418               source_path.RemoveLastPathComponent();
419               module_spec.GetSourceMappingList().Append(
420                   build_path.GetPath(), source_path.GetPath(), true);
421             }
422           }
423         }
424         if (keys)
425           free(keys);
426         if (values)
427           free(values);
428       }
429     }
430 
431     // If we have a DBGBuildSourcePath + DBGSourcePath pair, append them to the
432     // source remappings list.
433 
434     cf_str = (CFStringRef)CFDictionaryGetValue((CFDictionaryRef)uuid_dict,
435                                                CFSTR("DBGBuildSourcePath"));
436     if (cf_str && CFGetTypeID(cf_str) == CFStringGetTypeID()) {
437       CFCString::FileSystemRepresentation(cf_str, DBGBuildSourcePath);
438     }
439 
440     cf_str = (CFStringRef)CFDictionaryGetValue((CFDictionaryRef)uuid_dict,
441                                                CFSTR("DBGSourcePath"));
442     if (cf_str && CFGetTypeID(cf_str) == CFStringGetTypeID()) {
443       CFCString::FileSystemRepresentation(cf_str, DBGSourcePath);
444     }
445 
446     if (!DBGBuildSourcePath.empty() && !DBGSourcePath.empty()) {
447       if (DBGSourcePath[0] == '~') {
448         FileSpec resolved_source_path(DBGSourcePath.c_str());
449         FileSystem::Instance().Resolve(resolved_source_path);
450         DBGSourcePath = resolved_source_path.GetPath();
451       }
452       module_spec.GetSourceMappingList().Append(DBGBuildSourcePath,
453                                                 DBGSourcePath, true);
454     }
455   }
456   return success;
457 }
458 
459 bool Symbols::DownloadObjectAndSymbolFile(ModuleSpec &module_spec,
460                                           bool force_lookup) {
461   bool success = false;
462   const UUID *uuid_ptr = module_spec.GetUUIDPtr();
463   const FileSpec *file_spec_ptr = module_spec.GetFileSpecPtr();
464 
465   // It's expensive to check for the DBGShellCommands defaults setting, only do
466   // it once per lldb run and cache the result.
467   static bool g_have_checked_for_dbgshell_command = false;
468   static const char *g_dbgshell_command = NULL;
469   if (!g_have_checked_for_dbgshell_command) {
470     g_have_checked_for_dbgshell_command = true;
471     CFTypeRef defaults_setting = CFPreferencesCopyAppValue(
472         CFSTR("DBGShellCommands"), CFSTR("com.apple.DebugSymbols"));
473     if (defaults_setting &&
474         CFGetTypeID(defaults_setting) == CFStringGetTypeID()) {
475       char cstr_buf[PATH_MAX];
476       if (CFStringGetCString((CFStringRef)defaults_setting, cstr_buf,
477                              sizeof(cstr_buf), kCFStringEncodingUTF8)) {
478         g_dbgshell_command =
479             strdup(cstr_buf); // this malloc'ed memory will never be freed
480       }
481     }
482     if (defaults_setting) {
483       CFRelease(defaults_setting);
484     }
485   }
486 
487   // When g_dbgshell_command is NULL, the user has not enabled the use of an
488   // external program to find the symbols, don't run it for them.
489   if (!force_lookup && g_dbgshell_command == NULL) {
490     return false;
491   }
492 
493   if (uuid_ptr ||
494       (file_spec_ptr && FileSystem::Instance().Exists(*file_spec_ptr))) {
495     static bool g_located_dsym_for_uuid_exe = false;
496     static bool g_dsym_for_uuid_exe_exists = false;
497     static char g_dsym_for_uuid_exe_path[PATH_MAX];
498     if (!g_located_dsym_for_uuid_exe) {
499       g_located_dsym_for_uuid_exe = true;
500       const char *dsym_for_uuid_exe_path_cstr =
501           getenv("LLDB_APPLE_DSYMFORUUID_EXECUTABLE");
502       FileSpec dsym_for_uuid_exe_spec;
503       if (dsym_for_uuid_exe_path_cstr) {
504         dsym_for_uuid_exe_spec.SetFile(dsym_for_uuid_exe_path_cstr,
505                                        FileSpec::Style::native);
506         FileSystem::Instance().Resolve(dsym_for_uuid_exe_spec);
507         g_dsym_for_uuid_exe_exists =
508             FileSystem::Instance().Exists(dsym_for_uuid_exe_spec);
509       }
510 
511       if (!g_dsym_for_uuid_exe_exists) {
512         dsym_for_uuid_exe_spec.SetFile("/usr/local/bin/dsymForUUID",
513                                        FileSpec::Style::native);
514         g_dsym_for_uuid_exe_exists =
515             FileSystem::Instance().Exists(dsym_for_uuid_exe_spec);
516         if (!g_dsym_for_uuid_exe_exists) {
517           long bufsize;
518           if ((bufsize = sysconf(_SC_GETPW_R_SIZE_MAX)) != -1) {
519             char buffer[bufsize];
520             struct passwd pwd;
521             struct passwd *tilde_rc = NULL;
522             // we are a library so we need to use the reentrant version of
523             // getpwnam()
524             if (getpwnam_r("rc", &pwd, buffer, bufsize, &tilde_rc) == 0 &&
525                 tilde_rc && tilde_rc->pw_dir) {
526               std::string dsymforuuid_path(tilde_rc->pw_dir);
527               dsymforuuid_path += "/bin/dsymForUUID";
528               dsym_for_uuid_exe_spec.SetFile(dsymforuuid_path.c_str(),
529                                              FileSpec::Style::native);
530               g_dsym_for_uuid_exe_exists =
531                   FileSystem::Instance().Exists(dsym_for_uuid_exe_spec);
532             }
533           }
534         }
535       }
536       if (!g_dsym_for_uuid_exe_exists && g_dbgshell_command != NULL) {
537         dsym_for_uuid_exe_spec.SetFile(g_dbgshell_command,
538                                        FileSpec::Style::native);
539         FileSystem::Instance().Resolve(dsym_for_uuid_exe_spec);
540         g_dsym_for_uuid_exe_exists =
541             FileSystem::Instance().Exists(dsym_for_uuid_exe_spec);
542       }
543 
544       if (g_dsym_for_uuid_exe_exists)
545         dsym_for_uuid_exe_spec.GetPath(g_dsym_for_uuid_exe_path,
546                                        sizeof(g_dsym_for_uuid_exe_path));
547     }
548     if (g_dsym_for_uuid_exe_exists) {
549       std::string uuid_str;
550       char file_path[PATH_MAX];
551       file_path[0] = '\0';
552 
553       if (uuid_ptr)
554         uuid_str = uuid_ptr->GetAsString();
555 
556       if (file_spec_ptr)
557         file_spec_ptr->GetPath(file_path, sizeof(file_path));
558 
559       StreamString command;
560       if (!uuid_str.empty())
561         command.Printf("%s --ignoreNegativeCache --copyExecutable %s",
562                        g_dsym_for_uuid_exe_path, uuid_str.c_str());
563       else if (file_path[0] != '\0')
564         command.Printf("%s --ignoreNegativeCache --copyExecutable %s",
565                        g_dsym_for_uuid_exe_path, file_path);
566 
567       if (!command.GetString().empty()) {
568         Log *log = GetLog(LLDBLog::Host);
569         int exit_status = -1;
570         int signo = -1;
571         std::string command_output;
572         if (log) {
573           if (!uuid_str.empty())
574             LLDB_LOGF(log, "Calling %s with UUID %s to find dSYM",
575                       g_dsym_for_uuid_exe_path, uuid_str.c_str());
576           else if (file_path[0] != '\0')
577             LLDB_LOGF(log, "Calling %s with file %s to find dSYM",
578                       g_dsym_for_uuid_exe_path, file_path);
579         }
580         Status error = Host::RunShellCommand(
581             command.GetData(),
582             FileSpec(),      // current working directory
583             &exit_status,    // Exit status
584             &signo,          // Signal int *
585             &command_output, // Command output
586             std::chrono::seconds(
587                 640), // Large timeout to allow for long dsym download times
588             false);   // Don't run in a shell (we don't need shell expansion)
589         if (error.Success() && exit_status == 0 && !command_output.empty()) {
590           CFCData data(CFDataCreateWithBytesNoCopy(
591               NULL, (const UInt8 *)command_output.data(), command_output.size(),
592               kCFAllocatorNull));
593 
594           CFCReleaser<CFDictionaryRef> plist(
595               (CFDictionaryRef)::CFPropertyListCreateFromXMLData(
596                   NULL, data.get(), kCFPropertyListImmutable, NULL));
597 
598           if (plist.get() &&
599               CFGetTypeID(plist.get()) == CFDictionaryGetTypeID()) {
600             if (!uuid_str.empty()) {
601               CFCString uuid_cfstr(uuid_str.c_str());
602               CFDictionaryRef uuid_dict = (CFDictionaryRef)CFDictionaryGetValue(
603                   plist.get(), uuid_cfstr.get());
604               success =
605                   GetModuleSpecInfoFromUUIDDictionary(uuid_dict, module_spec);
606             } else {
607               const CFIndex num_values = ::CFDictionaryGetCount(plist.get());
608               if (num_values > 0) {
609                 std::vector<CFStringRef> keys(num_values, NULL);
610                 std::vector<CFDictionaryRef> values(num_values, NULL);
611                 ::CFDictionaryGetKeysAndValues(plist.get(), NULL,
612                                                (const void **)&values[0]);
613                 if (num_values == 1) {
614                   success = GetModuleSpecInfoFromUUIDDictionary(values[0],
615                                                                 module_spec);
616                   return success;
617                 } else {
618                   for (CFIndex i = 0; i < num_values; ++i) {
619                     ModuleSpec curr_module_spec;
620                     if (GetModuleSpecInfoFromUUIDDictionary(values[i],
621                                                             curr_module_spec)) {
622                       if (module_spec.GetArchitecture().IsCompatibleMatch(
623                               curr_module_spec.GetArchitecture())) {
624                         module_spec = curr_module_spec;
625                         return true;
626                       }
627                     }
628                   }
629                 }
630               }
631             }
632           }
633         } else {
634           if (log) {
635             if (!uuid_str.empty())
636               LLDB_LOGF(log, "Called %s on %s, no matches",
637                         g_dsym_for_uuid_exe_path, uuid_str.c_str());
638             else if (file_path[0] != '\0')
639               LLDB_LOGF(log, "Called %s on %s, no matches",
640                         g_dsym_for_uuid_exe_path, file_path);
641           }
642         }
643       }
644     }
645   }
646   return success;
647 }
648