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