1 //===-- PlatformDarwin.cpp --------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "lldb/lldb-python.h"
11 
12 #include "PlatformDarwin.h"
13 
14 // C Includes
15 // C++ Includes
16 // Other libraries and framework includes
17 // Project includes
18 #include "lldb/Breakpoint/BreakpointLocation.h"
19 #include "lldb/Core/Debugger.h"
20 #include "lldb/Core/Error.h"
21 #include "lldb/Core/Log.h"
22 #include "lldb/Core/Module.h"
23 #include "lldb/Core/ModuleSpec.h"
24 #include "lldb/Core/Timer.h"
25 #include "lldb/Host/Host.h"
26 #include "lldb/Host/Symbols.h"
27 #include "lldb/Symbol/ObjectFile.h"
28 #include "lldb/Symbol/SymbolFile.h"
29 #include "lldb/Symbol/SymbolVendor.h"
30 #include "lldb/Target/Target.h"
31 #include "llvm/ADT/STLExtras.h"
32 
33 using namespace lldb;
34 using namespace lldb_private;
35 
36 
37 //------------------------------------------------------------------
38 /// Default Constructor
39 //------------------------------------------------------------------
40 PlatformDarwin::PlatformDarwin (bool is_host) :
41     PlatformPOSIX(is_host),  // This is the local host platform
42     m_developer_directory ()
43 {
44 }
45 
46 //------------------------------------------------------------------
47 /// Destructor.
48 ///
49 /// The destructor is virtual since this class is designed to be
50 /// inherited from by the plug-in instance.
51 //------------------------------------------------------------------
52 PlatformDarwin::~PlatformDarwin()
53 {
54 }
55 
56 FileSpecList
57 PlatformDarwin::LocateExecutableScriptingResources (Target *target,
58                                                     Module &module)
59 {
60     FileSpecList file_list;
61     if (target && target->GetDebugger().GetScriptLanguage() == eScriptLanguagePython)
62     {
63         // NB some extensions might be meaningful and should not be stripped - "this.binary.file"
64         // should not lose ".file" but GetFileNameStrippingExtension() will do precisely that.
65         // Ideally, we should have a per-platform list of extensions (".exe", ".app", ".dSYM", ".framework")
66         // which should be stripped while leaving "this.binary.file" as-is.
67         FileSpec module_spec = module.GetFileSpec();
68 
69         if (module_spec)
70         {
71             SymbolVendor *symbols = module.GetSymbolVendor ();
72             if (symbols)
73             {
74                 SymbolFile *symfile = symbols->GetSymbolFile();
75                 if (symfile)
76                 {
77                     ObjectFile *objfile = symfile->GetObjectFile();
78                     if (objfile)
79                     {
80                         FileSpec symfile_spec (objfile->GetFileSpec());
81                         if (symfile_spec && symfile_spec.Exists())
82                         {
83                             while (module_spec.GetFilename())
84                             {
85                                 std::string module_basename (module_spec.GetFilename().GetCString());
86 
87                                 // FIXME: for Python, we cannot allow certain characters in module
88                                 // filenames we import. Theoretically, different scripting languages may
89                                 // have different sets of forbidden tokens in filenames, and that should
90                                 // be dealt with by each ScriptInterpreter. For now, we just replace dots
91                                 // with underscores, but if we ever support anything other than Python
92                                 // we will need to rework this
93                                 std::replace(module_basename.begin(), module_basename.end(), '.', '_');
94                                 std::replace(module_basename.begin(), module_basename.end(), ' ', '_');
95                                 std::replace(module_basename.begin(), module_basename.end(), '-', '_');
96 
97 
98                                 StreamString path_string;
99                                 // for OSX we are going to be in .dSYM/Contents/Resources/DWARF/<basename>
100                                 // let us go to .dSYM/Contents/Resources/Python/<basename>.py and see if the file exists
101                                 path_string.Printf("%s/../Python/%s.py",symfile_spec.GetDirectory().GetCString(), module_basename.c_str());
102                                 FileSpec script_fspec(path_string.GetData(), true);
103                                 if (script_fspec.Exists())
104                                 {
105                                     file_list.Append (script_fspec);
106                                     break;
107                                 }
108 
109                                 // If we didn't find the python file, then keep
110                                 // stripping the extensions and try again
111                                 ConstString filename_no_extension (module_spec.GetFileNameStrippingExtension());
112                                 if (module_spec.GetFilename() == filename_no_extension)
113                                     break;
114 
115                                 module_spec.GetFilename() = filename_no_extension;
116                             }
117                         }
118                     }
119                 }
120             }
121         }
122     }
123     return file_list;
124 }
125 
126 Error
127 PlatformDarwin::ResolveExecutable (const FileSpec &exe_file,
128                                    const ArchSpec &exe_arch,
129                                    lldb::ModuleSP &exe_module_sp,
130                                    const FileSpecList *module_search_paths_ptr)
131 {
132     Error error;
133     // Nothing special to do here, just use the actual file and architecture
134 
135     char exe_path[PATH_MAX];
136     FileSpec resolved_exe_file (exe_file);
137 
138     if (IsHost())
139     {
140         // If we have "ls" as the exe_file, resolve the executable loation based on
141         // the current path variables
142         if (!resolved_exe_file.Exists())
143         {
144             exe_file.GetPath (exe_path, sizeof(exe_path));
145             resolved_exe_file.SetFile(exe_path, true);
146         }
147 
148         if (!resolved_exe_file.Exists())
149             resolved_exe_file.ResolveExecutableLocation ();
150 
151         // Resolve any executable within a bundle on MacOSX
152         Host::ResolveExecutableInBundle (resolved_exe_file);
153 
154         if (resolved_exe_file.Exists())
155             error.Clear();
156         else
157         {
158             exe_file.GetPath (exe_path, sizeof(exe_path));
159             error.SetErrorStringWithFormat ("unable to find executable for '%s'", exe_path);
160         }
161     }
162     else
163     {
164         if (m_remote_platform_sp)
165         {
166             error = m_remote_platform_sp->ResolveExecutable (exe_file,
167                                                              exe_arch,
168                                                              exe_module_sp,
169                                                              module_search_paths_ptr);
170         }
171         else
172         {
173             // We may connect to a process and use the provided executable (Don't use local $PATH).
174 
175             // Resolve any executable within a bundle on MacOSX
176             Host::ResolveExecutableInBundle (resolved_exe_file);
177 
178             if (resolved_exe_file.Exists())
179                 error.Clear();
180             else
181                 error.SetErrorStringWithFormat("the platform is not currently connected, and '%s' doesn't exist in the system root.", resolved_exe_file.GetFilename().AsCString(""));
182         }
183     }
184 
185 
186     if (error.Success())
187     {
188         ModuleSpec module_spec (resolved_exe_file, exe_arch);
189         if (module_spec.GetArchitecture().IsValid())
190         {
191             error = ModuleList::GetSharedModule (module_spec,
192                                                  exe_module_sp,
193                                                  module_search_paths_ptr,
194                                                  NULL,
195                                                  NULL);
196 
197             if (error.Fail() || exe_module_sp.get() == NULL || exe_module_sp->GetObjectFile() == NULL)
198             {
199                 exe_module_sp.reset();
200                 error.SetErrorStringWithFormat ("'%s' doesn't contain the architecture %s",
201                                                 exe_file.GetPath().c_str(),
202                                                 exe_arch.GetArchitectureName());
203             }
204         }
205         else
206         {
207             // No valid architecture was specified, ask the platform for
208             // the architectures that we should be using (in the correct order)
209             // and see if we can find a match that way
210             StreamString arch_names;
211             for (uint32_t idx = 0; GetSupportedArchitectureAtIndex (idx, module_spec.GetArchitecture()); ++idx)
212             {
213                 error = GetSharedModule (module_spec,
214                                          exe_module_sp,
215                                          module_search_paths_ptr,
216                                          NULL,
217                                          NULL);
218                 // Did we find an executable using one of the
219                 if (error.Success())
220                 {
221                     if (exe_module_sp && exe_module_sp->GetObjectFile())
222                         break;
223                     else
224                         error.SetErrorToGenericError();
225                 }
226 
227                 if (idx > 0)
228                     arch_names.PutCString (", ");
229                 arch_names.PutCString (module_spec.GetArchitecture().GetArchitectureName());
230             }
231 
232             if (error.Fail() || !exe_module_sp)
233             {
234                 error.SetErrorStringWithFormat ("'%s' doesn't contain any '%s' platform architectures: %s",
235                                                 exe_file.GetPath().c_str(),
236                                                 GetPluginName().GetCString(),
237                                                 arch_names.GetString().c_str());
238             }
239         }
240     }
241 
242     return error;
243 }
244 
245 Error
246 PlatformDarwin::ResolveSymbolFile (Target &target,
247                                    const ModuleSpec &sym_spec,
248                                    FileSpec &sym_file)
249 {
250     Error error;
251     sym_file = sym_spec.GetSymbolFileSpec();
252     if (sym_file.Exists())
253     {
254         if (sym_file.GetFileType() == FileSpec::eFileTypeDirectory)
255         {
256             sym_file = Symbols::FindSymbolFileInBundle (sym_file,
257                                                         sym_spec.GetUUIDPtr(),
258                                                         sym_spec.GetArchitecturePtr());
259         }
260     }
261     else
262     {
263         if (sym_spec.GetUUID().IsValid())
264         {
265 
266         }
267     }
268     return error;
269 
270 }
271 
272 static lldb_private::Error
273 MakeCacheFolderForFile (const FileSpec& module_cache_spec)
274 {
275     FileSpec module_cache_folder = module_cache_spec.CopyByRemovingLastPathComponent();
276     return Host::MakeDirectory(module_cache_folder.GetPath().c_str(), eFilePermissionsDirectoryDefault);
277 }
278 
279 static lldb_private::Error
280 BringInRemoteFile (Platform* platform,
281                    const lldb_private::ModuleSpec &module_spec,
282                    const FileSpec& module_cache_spec)
283 {
284     MakeCacheFolderForFile(module_cache_spec);
285     Error err = platform->GetFile(module_spec.GetFileSpec(), module_cache_spec);
286     return err;
287 }
288 
289 lldb_private::Error
290 PlatformDarwin::GetSharedModuleWithLocalCache (const lldb_private::ModuleSpec &module_spec,
291                                                lldb::ModuleSP &module_sp,
292                                                const lldb_private::FileSpecList *module_search_paths_ptr,
293                                                lldb::ModuleSP *old_module_sp_ptr,
294                                                bool *did_create_ptr)
295 {
296 
297     Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM));
298     if (log)
299         log->Printf("[%s] Trying to find module %s/%s - platform path %s/%s symbol path %s/%s",
300                      (IsHost() ? "host" : "remote"),
301                      module_spec.GetFileSpec().GetDirectory().AsCString(),
302                      module_spec.GetFileSpec().GetFilename().AsCString(),
303                      module_spec.GetPlatformFileSpec().GetDirectory().AsCString(),
304                      module_spec.GetPlatformFileSpec().GetFilename().AsCString(),
305                      module_spec.GetSymbolFileSpec().GetDirectory().AsCString(),
306                      module_spec.GetSymbolFileSpec().GetFilename().AsCString());
307 
308     Error err;
309 
310     err = ModuleList::GetSharedModule(module_spec, module_sp, module_search_paths_ptr, old_module_sp_ptr, did_create_ptr);
311     if (module_sp)
312         return err;
313 
314     if (!IsHost())
315     {
316         std::string cache_path(GetLocalCacheDirectory());
317         // Only search for a locally cached file if we have a valid cache path
318         if (!cache_path.empty())
319         {
320             std::string module_path (module_spec.GetFileSpec().GetPath());
321             cache_path.append(module_path);
322             FileSpec module_cache_spec(cache_path.c_str(),false);
323 
324             // if rsync is supported, always bring in the file - rsync will be very efficient
325             // when files are the same on the local and remote end of the connection
326             if (this->GetSupportsRSync())
327             {
328                 err = BringInRemoteFile (this, module_spec, module_cache_spec);
329                 if (err.Fail())
330                     return err;
331                 if (module_cache_spec.Exists())
332                 {
333                     Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM));
334                     if (log)
335                         log->Printf("[%s] module %s/%s was rsynced and is now there",
336                                      (IsHost() ? "host" : "remote"),
337                                      module_spec.GetFileSpec().GetDirectory().AsCString(),
338                                      module_spec.GetFileSpec().GetFilename().AsCString());
339                     ModuleSpec local_spec(module_cache_spec, module_spec.GetArchitecture());
340                     module_sp.reset(new Module(local_spec));
341                     module_sp->SetPlatformFileSpec(module_spec.GetFileSpec());
342                     return Error();
343                 }
344             }
345 
346             // try to find the module in the cache
347             if (module_cache_spec.Exists())
348             {
349                 // get the local and remote MD5 and compare
350                 if (m_remote_platform_sp)
351                 {
352                     // when going over the *slow* GDB remote transfer mechanism we first check
353                     // the hashes of the files - and only do the actual transfer if they differ
354                     uint64_t high_local,high_remote,low_local,low_remote;
355                     Host::CalculateMD5 (module_cache_spec, low_local, high_local);
356                     m_remote_platform_sp->CalculateMD5(module_spec.GetFileSpec(), low_remote, high_remote);
357                     if (low_local != low_remote || high_local != high_remote)
358                     {
359                         // bring in the remote file
360                         Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM));
361                         if (log)
362                             log->Printf("[%s] module %s/%s needs to be replaced from remote copy",
363                                          (IsHost() ? "host" : "remote"),
364                                          module_spec.GetFileSpec().GetDirectory().AsCString(),
365                                          module_spec.GetFileSpec().GetFilename().AsCString());
366                         Error err = BringInRemoteFile (this, module_spec, module_cache_spec);
367                         if (err.Fail())
368                             return err;
369                     }
370                 }
371 
372                 ModuleSpec local_spec(module_cache_spec, module_spec.GetArchitecture());
373                 module_sp.reset(new Module(local_spec));
374                 module_sp->SetPlatformFileSpec(module_spec.GetFileSpec());
375                 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM));
376                     if (log)
377                         log->Printf("[%s] module %s/%s was found in the cache",
378                                      (IsHost() ? "host" : "remote"),
379                                      module_spec.GetFileSpec().GetDirectory().AsCString(),
380                                      module_spec.GetFileSpec().GetFilename().AsCString());
381                 return Error();
382             }
383 
384             // bring in the remote module file
385             if (log)
386                 log->Printf("[%s] module %s/%s needs to come in remotely",
387                              (IsHost() ? "host" : "remote"),
388                              module_spec.GetFileSpec().GetDirectory().AsCString(),
389                              module_spec.GetFileSpec().GetFilename().AsCString());
390             Error err = BringInRemoteFile (this, module_spec, module_cache_spec);
391             if (err.Fail())
392                 return err;
393             if (module_cache_spec.Exists())
394             {
395                 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM));
396                 if (log)
397                     log->Printf("[%s] module %s/%s is now cached and fine",
398                                  (IsHost() ? "host" : "remote"),
399                                  module_spec.GetFileSpec().GetDirectory().AsCString(),
400                                  module_spec.GetFileSpec().GetFilename().AsCString());
401                 ModuleSpec local_spec(module_cache_spec, module_spec.GetArchitecture());
402                 module_sp.reset(new Module(local_spec));
403                 module_sp->SetPlatformFileSpec(module_spec.GetFileSpec());
404                 return Error();
405             }
406             else
407                 return Error("unable to obtain valid module file");
408         }
409         else
410             return Error("no cache path");
411     }
412     else
413         return Error ("unable to resolve module");
414 }
415 
416 Error
417 PlatformDarwin::GetSharedModule (const ModuleSpec &module_spec,
418                                  ModuleSP &module_sp,
419                                  const FileSpecList *module_search_paths_ptr,
420                                  ModuleSP *old_module_sp_ptr,
421                                  bool *did_create_ptr)
422 {
423     Error error;
424     module_sp.reset();
425 
426     if (IsRemote())
427     {
428         // If we have a remote platform always, let it try and locate
429         // the shared module first.
430         if (m_remote_platform_sp)
431         {
432             error = m_remote_platform_sp->GetSharedModule (module_spec,
433                                                            module_sp,
434                                                            module_search_paths_ptr,
435                                                            old_module_sp_ptr,
436                                                            did_create_ptr);
437         }
438     }
439 
440     if (!module_sp)
441     {
442         // Fall back to the local platform and find the file locally
443         error = Platform::GetSharedModule (module_spec,
444                                            module_sp,
445                                            module_search_paths_ptr,
446                                            old_module_sp_ptr,
447                                            did_create_ptr);
448 
449         const FileSpec &platform_file = module_spec.GetFileSpec();
450         if (!module_sp && module_search_paths_ptr && platform_file)
451         {
452             // We can try to pull off part of the file path up to the bundle
453             // directory level and try any module search paths...
454             FileSpec bundle_directory;
455             if (Host::GetBundleDirectory (platform_file, bundle_directory))
456             {
457                 if (platform_file == bundle_directory)
458                 {
459                     ModuleSpec new_module_spec (module_spec);
460                     new_module_spec.GetFileSpec() = bundle_directory;
461                     if (Host::ResolveExecutableInBundle (new_module_spec.GetFileSpec()))
462                     {
463                         Error new_error (Platform::GetSharedModule (new_module_spec,
464                                                                     module_sp,
465                                                                     NULL,
466                                                                     old_module_sp_ptr,
467                                                                     did_create_ptr));
468 
469                         if (module_sp)
470                             return new_error;
471                     }
472                 }
473                 else
474                 {
475                     char platform_path[PATH_MAX];
476                     char bundle_dir[PATH_MAX];
477                     platform_file.GetPath (platform_path, sizeof(platform_path));
478                     const size_t bundle_directory_len = bundle_directory.GetPath (bundle_dir, sizeof(bundle_dir));
479                     char new_path[PATH_MAX];
480                     size_t num_module_search_paths = module_search_paths_ptr->GetSize();
481                     for (size_t i=0; i<num_module_search_paths; ++i)
482                     {
483                         const size_t search_path_len = module_search_paths_ptr->GetFileSpecAtIndex(i).GetPath(new_path, sizeof(new_path));
484                         if (search_path_len < sizeof(new_path))
485                         {
486                             snprintf (new_path + search_path_len, sizeof(new_path) - search_path_len, "/%s", platform_path + bundle_directory_len);
487                             FileSpec new_file_spec (new_path, false);
488                             if (new_file_spec.Exists())
489                             {
490                                 ModuleSpec new_module_spec (module_spec);
491                                 new_module_spec.GetFileSpec() = new_file_spec;
492                                 Error new_error (Platform::GetSharedModule (new_module_spec,
493                                                                             module_sp,
494                                                                             NULL,
495                                                                             old_module_sp_ptr,
496                                                                             did_create_ptr));
497 
498                                 if (module_sp)
499                                 {
500                                     module_sp->SetPlatformFileSpec(new_file_spec);
501                                     return new_error;
502                                 }
503                             }
504                         }
505                     }
506                 }
507             }
508         }
509     }
510     if (module_sp)
511         module_sp->SetPlatformFileSpec(module_spec.GetFileSpec());
512     return error;
513 }
514 
515 size_t
516 PlatformDarwin::GetSoftwareBreakpointTrapOpcode (Target &target, BreakpointSite *bp_site)
517 {
518     const uint8_t *trap_opcode = NULL;
519     uint32_t trap_opcode_size = 0;
520     bool bp_is_thumb = false;
521 
522     llvm::Triple::ArchType machine = target.GetArchitecture().GetMachine();
523     switch (machine)
524     {
525     case llvm::Triple::x86:
526     case llvm::Triple::x86_64:
527         {
528             static const uint8_t g_i386_breakpoint_opcode[] = { 0xCC };
529             trap_opcode = g_i386_breakpoint_opcode;
530             trap_opcode_size = sizeof(g_i386_breakpoint_opcode);
531         }
532         break;
533 
534     case llvm::Triple::arm64:
535         {
536             // TODO: fix this with actual darwin breakpoint opcode for arm64.
537             // right now debugging uses the Z packets with GDB remote so this
538             // is not needed, but the size needs to be correct...
539             static const uint8_t g_arm64_breakpoint_opcode[] = { 0xFE, 0xDE, 0xFF, 0xE7 };
540             trap_opcode = g_arm64_breakpoint_opcode;
541             trap_opcode_size = sizeof(g_arm64_breakpoint_opcode);
542         }
543         break;
544 
545     case llvm::Triple::thumb:
546         bp_is_thumb = true; // Fall through...
547     case llvm::Triple::arm:
548         {
549             static const uint8_t g_arm_breakpoint_opcode[] = { 0xFE, 0xDE, 0xFF, 0xE7 };
550             static const uint8_t g_thumb_breakpooint_opcode[] = { 0xFE, 0xDE };
551 
552             // Auto detect arm/thumb if it wasn't explicitly specified
553             if (!bp_is_thumb)
554             {
555                 lldb::BreakpointLocationSP bp_loc_sp (bp_site->GetOwnerAtIndex (0));
556                 if (bp_loc_sp)
557                     bp_is_thumb = bp_loc_sp->GetAddress().GetAddressClass () == eAddressClassCodeAlternateISA;
558             }
559             if (bp_is_thumb)
560             {
561                 trap_opcode = g_thumb_breakpooint_opcode;
562                 trap_opcode_size = sizeof(g_thumb_breakpooint_opcode);
563                 break;
564             }
565             trap_opcode = g_arm_breakpoint_opcode;
566             trap_opcode_size = sizeof(g_arm_breakpoint_opcode);
567         }
568         break;
569 
570     case llvm::Triple::ppc:
571     case llvm::Triple::ppc64:
572         {
573             static const uint8_t g_ppc_breakpoint_opcode[] = { 0x7F, 0xC0, 0x00, 0x08 };
574             trap_opcode = g_ppc_breakpoint_opcode;
575             trap_opcode_size = sizeof(g_ppc_breakpoint_opcode);
576         }
577         break;
578 
579     default:
580         assert(!"Unhandled architecture in PlatformDarwin::GetSoftwareBreakpointTrapOpcode()");
581         break;
582     }
583 
584     if (trap_opcode && trap_opcode_size)
585     {
586         if (bp_site->SetTrapOpcode(trap_opcode, trap_opcode_size))
587             return trap_opcode_size;
588     }
589     return 0;
590 
591 }
592 
593 bool
594 PlatformDarwin::GetRemoteOSVersion ()
595 {
596     if (m_remote_platform_sp)
597         return m_remote_platform_sp->GetOSVersion (m_major_os_version,
598                                                    m_minor_os_version,
599                                                    m_update_os_version);
600     return false;
601 }
602 
603 bool
604 PlatformDarwin::GetRemoteOSBuildString (std::string &s)
605 {
606     if (m_remote_platform_sp)
607         return m_remote_platform_sp->GetRemoteOSBuildString (s);
608     s.clear();
609     return false;
610 }
611 
612 bool
613 PlatformDarwin::GetRemoteOSKernelDescription (std::string &s)
614 {
615     if (m_remote_platform_sp)
616         return m_remote_platform_sp->GetRemoteOSKernelDescription (s);
617     s.clear();
618     return false;
619 }
620 
621 // Remote Platform subclasses need to override this function
622 ArchSpec
623 PlatformDarwin::GetRemoteSystemArchitecture ()
624 {
625     if (m_remote_platform_sp)
626         return m_remote_platform_sp->GetRemoteSystemArchitecture ();
627     return ArchSpec();
628 }
629 
630 
631 const char *
632 PlatformDarwin::GetHostname ()
633 {
634     if (IsHost())
635         return Platform::GetHostname();
636 
637     if (m_remote_platform_sp)
638         return m_remote_platform_sp->GetHostname ();
639     return NULL;
640 }
641 
642 bool
643 PlatformDarwin::IsConnected () const
644 {
645     if (IsHost())
646         return true;
647     else if (m_remote_platform_sp)
648         return m_remote_platform_sp->IsConnected();
649     return false;
650 }
651 
652 Error
653 PlatformDarwin::ConnectRemote (Args& args)
654 {
655     Error error;
656     if (IsHost())
657     {
658         error.SetErrorStringWithFormat ("can't connect to the host platform '%s', always connected", GetPluginName().GetCString());
659     }
660     else
661     {
662         if (!m_remote_platform_sp)
663             m_remote_platform_sp = Platform::Create ("remote-gdb-server", error);
664 
665         if (m_remote_platform_sp && error.Success())
666             error = m_remote_platform_sp->ConnectRemote (args);
667         else
668             error.SetErrorString ("failed to create a 'remote-gdb-server' platform");
669 
670         if (error.Fail())
671             m_remote_platform_sp.reset();
672     }
673 
674     if (error.Success() && m_remote_platform_sp)
675     {
676         if (m_options.get())
677         {
678             OptionGroupOptions* options = m_options.get();
679             OptionGroupPlatformRSync* m_rsync_options = (OptionGroupPlatformRSync*)options->GetGroupWithOption('r');
680             OptionGroupPlatformSSH* m_ssh_options = (OptionGroupPlatformSSH*)options->GetGroupWithOption('s');
681             OptionGroupPlatformCaching* m_cache_options = (OptionGroupPlatformCaching*)options->GetGroupWithOption('c');
682 
683             if (m_rsync_options->m_rsync)
684             {
685                 SetSupportsRSync(true);
686                 SetRSyncOpts(m_rsync_options->m_rsync_opts.c_str());
687                 SetRSyncPrefix(m_rsync_options->m_rsync_prefix.c_str());
688                 SetIgnoresRemoteHostname(m_rsync_options->m_ignores_remote_hostname);
689             }
690             if (m_ssh_options->m_ssh)
691             {
692                 SetSupportsSSH(true);
693                 SetSSHOpts(m_ssh_options->m_ssh_opts.c_str());
694             }
695             SetLocalCacheDirectory(m_cache_options->m_cache_dir.c_str());
696         }
697     }
698 
699     return error;
700 }
701 
702 Error
703 PlatformDarwin::DisconnectRemote ()
704 {
705     Error error;
706 
707     if (IsHost())
708     {
709         error.SetErrorStringWithFormat ("can't disconnect from the host platform '%s', always connected", GetPluginName().GetCString());
710     }
711     else
712     {
713         if (m_remote_platform_sp)
714             error = m_remote_platform_sp->DisconnectRemote ();
715         else
716             error.SetErrorString ("the platform is not currently connected");
717     }
718     return error;
719 }
720 
721 
722 bool
723 PlatformDarwin::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
724 {
725     bool sucess = false;
726     if (IsHost())
727     {
728         sucess = Platform::GetProcessInfo (pid, process_info);
729     }
730     else
731     {
732         if (m_remote_platform_sp)
733             sucess = m_remote_platform_sp->GetProcessInfo (pid, process_info);
734     }
735     return sucess;
736 }
737 
738 
739 
740 uint32_t
741 PlatformDarwin::FindProcesses (const ProcessInstanceInfoMatch &match_info,
742                                ProcessInstanceInfoList &process_infos)
743 {
744     uint32_t match_count = 0;
745     if (IsHost())
746     {
747         // Let the base class figure out the host details
748         match_count = Platform::FindProcesses (match_info, process_infos);
749     }
750     else
751     {
752         // If we are remote, we can only return results if we are connected
753         if (m_remote_platform_sp)
754             match_count = m_remote_platform_sp->FindProcesses (match_info, process_infos);
755     }
756     return match_count;
757 }
758 
759 Error
760 PlatformDarwin::LaunchProcess (ProcessLaunchInfo &launch_info)
761 {
762     Error error;
763 
764     if (IsHost())
765     {
766         error = Platform::LaunchProcess (launch_info);
767     }
768     else
769     {
770         if (m_remote_platform_sp)
771             error = m_remote_platform_sp->LaunchProcess (launch_info);
772         else
773             error.SetErrorString ("the platform is not currently connected");
774     }
775     return error;
776 }
777 
778 lldb::ProcessSP
779 PlatformDarwin::DebugProcess (ProcessLaunchInfo &launch_info,
780                               Debugger &debugger,
781                               Target *target,       // Can be NULL, if NULL create a new target, else use existing one
782                               Listener &listener,
783                               Error &error)
784 {
785     ProcessSP process_sp;
786 
787     if (IsHost())
788     {
789         // We are going to hand this process off to debugserver which will be in charge of setting the exit status.
790         // We still need to reap it from lldb but if we let the monitor thread also set the exit status, we set up a
791         // race between debugserver & us for who will find out about the debugged process's death.
792         launch_info.GetFlags().Set(eLaunchFlagDontSetExitStatus);
793         process_sp = Platform::DebugProcess (launch_info, debugger, target, listener, error);
794     }
795     else
796     {
797         if (m_remote_platform_sp)
798             process_sp = m_remote_platform_sp->DebugProcess (launch_info, debugger, target, listener, error);
799         else
800             error.SetErrorString ("the platform is not currently connected");
801     }
802     return process_sp;
803 
804 }
805 
806 
807 lldb::ProcessSP
808 PlatformDarwin::Attach (ProcessAttachInfo &attach_info,
809                         Debugger &debugger,
810                         Target *target,
811                         Listener &listener,
812                         Error &error)
813 {
814     lldb::ProcessSP process_sp;
815 
816     if (IsHost())
817     {
818         if (target == NULL)
819         {
820             TargetSP new_target_sp;
821 
822             error = debugger.GetTargetList().CreateTarget (debugger,
823                                                            NULL,
824                                                            NULL,
825                                                            false,
826                                                            NULL,
827                                                            new_target_sp);
828             target = new_target_sp.get();
829         }
830         else
831             error.Clear();
832 
833         if (target && error.Success())
834         {
835             debugger.GetTargetList().SetSelectedTarget(target);
836 
837             process_sp = target->CreateProcess (listener, attach_info.GetProcessPluginName(), NULL);
838 
839             if (process_sp)
840             {
841                 ListenerSP listener_sp (new Listener("lldb.PlatformDarwin.attach.hijack"));
842                 attach_info.SetHijackListener(listener_sp);
843                 process_sp->HijackProcessEvents(listener_sp.get());
844                 error = process_sp->Attach (attach_info);
845             }
846         }
847     }
848     else
849     {
850         if (m_remote_platform_sp)
851             process_sp = m_remote_platform_sp->Attach (attach_info, debugger, target, listener, error);
852         else
853             error.SetErrorString ("the platform is not currently connected");
854     }
855     return process_sp;
856 }
857 
858 const char *
859 PlatformDarwin::GetUserName (uint32_t uid)
860 {
861     // Check the cache in Platform in case we have already looked this uid up
862     const char *user_name = Platform::GetUserName(uid);
863     if (user_name)
864         return user_name;
865 
866     if (IsRemote() && m_remote_platform_sp)
867         return m_remote_platform_sp->GetUserName(uid);
868     return NULL;
869 }
870 
871 const char *
872 PlatformDarwin::GetGroupName (uint32_t gid)
873 {
874     const char *group_name = Platform::GetGroupName(gid);
875     if (group_name)
876         return group_name;
877 
878     if (IsRemote() && m_remote_platform_sp)
879         return m_remote_platform_sp->GetGroupName(gid);
880     return NULL;
881 }
882 
883 bool
884 PlatformDarwin::ModuleIsExcludedForNonModuleSpecificSearches (lldb_private::Target &target, const lldb::ModuleSP &module_sp)
885 {
886     if (!module_sp)
887         return false;
888 
889     ObjectFile *obj_file = module_sp->GetObjectFile();
890     if (!obj_file)
891         return false;
892 
893     ObjectFile::Type obj_type = obj_file->GetType();
894     if (obj_type == ObjectFile::eTypeDynamicLinker)
895         return true;
896     else
897         return false;
898 }
899 
900 bool
901 PlatformDarwin::x86GetSupportedArchitectureAtIndex (uint32_t idx, ArchSpec &arch)
902 {
903     ArchSpec host_arch = Host::GetArchitecture (Host::eSystemDefaultArchitecture);
904     if (host_arch.GetCore() == ArchSpec::eCore_x86_64_x86_64h)
905     {
906         switch (idx)
907         {
908             case 0:
909                 arch = host_arch;
910                 return true;
911 
912             case 1:
913                 arch.SetTriple("x86_64-apple-macosx");
914                 return true;
915 
916             case 2:
917                 arch = Host::GetArchitecture (Host::eSystemDefaultArchitecture32);
918                 return true;
919 
920             default: return false;
921         }
922     }
923     else
924     {
925         if (idx == 0)
926         {
927             arch = Host::GetArchitecture (Host::eSystemDefaultArchitecture);
928             return arch.IsValid();
929         }
930         else if (idx == 1)
931         {
932             ArchSpec platform_arch (Host::GetArchitecture (Host::eSystemDefaultArchitecture));
933             ArchSpec platform_arch64 (Host::GetArchitecture (Host::eSystemDefaultArchitecture64));
934             if (platform_arch.IsExactMatch(platform_arch64))
935             {
936                 // This macosx platform supports both 32 and 64 bit. Since we already
937                 // returned the 64 bit arch for idx == 0, return the 32 bit arch
938                 // for idx == 1
939                 arch = Host::GetArchitecture (Host::eSystemDefaultArchitecture32);
940                 return arch.IsValid();
941             }
942         }
943     }
944     return false;
945 }
946 
947 // The architecture selection rules for arm processors
948 // These cpu subtypes have distinct names (e.g. armv7f) but armv7 binaries run fine on an armv7f processor.
949 
950 bool
951 PlatformDarwin::ARMGetSupportedArchitectureAtIndex (uint32_t idx, ArchSpec &arch)
952 {
953     ArchSpec system_arch (GetSystemArchitecture());
954 
955     const ArchSpec::Core system_core = system_arch.GetCore();
956     switch (system_core)
957     {
958     default:
959         switch (idx)
960         {
961             case  0: arch.SetTriple ("arm64-apple-ios");    return true;
962             case  1: arch.SetTriple ("armv7-apple-ios");    return true;
963             case  2: arch.SetTriple ("armv7f-apple-ios");   return true;
964             case  3: arch.SetTriple ("armv7k-apple-ios");   return true;
965             case  4: arch.SetTriple ("armv7s-apple-ios");   return true;
966             case  5: arch.SetTriple ("armv7m-apple-ios");   return true;
967             case  6: arch.SetTriple ("armv7em-apple-ios");  return true;
968             case  7: arch.SetTriple ("armv6m-apple-ios");   return true;
969             case  8: arch.SetTriple ("armv6-apple-ios");    return true;
970             case  9: arch.SetTriple ("armv5-apple-ios");    return true;
971             case 10: arch.SetTriple ("armv4-apple-ios");    return true;
972             case 11: arch.SetTriple ("arm-apple-ios");      return true;
973             case 12: arch.SetTriple ("thumbv7-apple-ios");  return true;
974             case 13: arch.SetTriple ("thumbv7f-apple-ios"); return true;
975             case 14: arch.SetTriple ("thumbv7k-apple-ios"); return true;
976             case 15: arch.SetTriple ("thumbv7s-apple-ios"); return true;
977             case 16: arch.SetTriple ("thumbv7m-apple-ios"); return true;
978             case 17: arch.SetTriple ("thumbv7em-apple-ios"); return true;
979             case 18: arch.SetTriple ("thumbv6m-apple-ios"); return true;
980             case 19: arch.SetTriple ("thumbv6-apple-ios");  return true;
981             case 20: arch.SetTriple ("thumbv5-apple-ios");  return true;
982             case 21: arch.SetTriple ("thumbv4t-apple-ios"); return true;
983             case 22: arch.SetTriple ("thumb-apple-ios");    return true;
984             default: break;
985         }
986         break;
987 
988     case ArchSpec::eCore_arm_arm64:
989         switch (idx)
990         {
991             case  0: arch.SetTriple ("arm64-apple-ios");   return true;
992             case  1: arch.SetTriple ("armv7s-apple-ios");   return true;
993             case  2: arch.SetTriple ("armv7f-apple-ios");   return true;
994             case  3: arch.SetTriple ("armv7m-apple-ios");   return true;
995             case  4: arch.SetTriple ("armv7em-apple-ios");  return true;
996             case  5: arch.SetTriple ("armv7-apple-ios");    return true;
997             case  6: arch.SetTriple ("armv6m-apple-ios");   return true;
998             case  7: arch.SetTriple ("armv6-apple-ios");    return true;
999             case  8: arch.SetTriple ("armv5-apple-ios");    return true;
1000             case  9: arch.SetTriple ("armv4-apple-ios");    return true;
1001             case 10: arch.SetTriple ("arm-apple-ios");      return true;
1002             case 11: arch.SetTriple ("thumbv7-apple-ios");  return true;
1003             case 12: arch.SetTriple ("thumbv7f-apple-ios"); return true;
1004             case 13: arch.SetTriple ("thumbv7k-apple-ios"); return true;
1005             case 14: arch.SetTriple ("thumbv7s-apple-ios"); return true;
1006             case 15: arch.SetTriple ("thumbv7m-apple-ios"); return true;
1007             case 16: arch.SetTriple ("thumbv7em-apple-ios"); return true;
1008             case 17: arch.SetTriple ("thumbv6m-apple-ios"); return true;
1009             case 18: arch.SetTriple ("thumbv6-apple-ios");  return true;
1010             case 19: arch.SetTriple ("thumbv5-apple-ios");  return true;
1011             case 20: arch.SetTriple ("thumbv4t-apple-ios"); return true;
1012             case 21: arch.SetTriple ("thumb-apple-ios");    return true;
1013         default: break;
1014         }
1015         break;
1016 
1017     case ArchSpec::eCore_arm_armv7f:
1018         switch (idx)
1019         {
1020             case  0: arch.SetTriple ("armv7f-apple-ios");   return true;
1021             case  1: arch.SetTriple ("armv7-apple-ios");    return true;
1022             case  2: arch.SetTriple ("armv6m-apple-ios");   return true;
1023             case  3: arch.SetTriple ("armv6-apple-ios");    return true;
1024             case  4: arch.SetTriple ("armv5-apple-ios");    return true;
1025             case  5: arch.SetTriple ("armv4-apple-ios");    return true;
1026             case  6: arch.SetTriple ("arm-apple-ios");      return true;
1027             case  7: arch.SetTriple ("thumbv7f-apple-ios"); return true;
1028             case  8: arch.SetTriple ("thumbv7-apple-ios");  return true;
1029             case  9: arch.SetTriple ("thumbv6m-apple-ios"); return true;
1030             case 10: arch.SetTriple ("thumbv6-apple-ios");  return true;
1031             case 11: arch.SetTriple ("thumbv5-apple-ios");  return true;
1032             case 12: arch.SetTriple ("thumbv4t-apple-ios"); return true;
1033             case 13: arch.SetTriple ("thumb-apple-ios");    return true;
1034             default: break;
1035         }
1036         break;
1037 
1038     case ArchSpec::eCore_arm_armv7k:
1039         switch (idx)
1040         {
1041             case  0: arch.SetTriple ("armv7k-apple-ios");   return true;
1042             case  1: arch.SetTriple ("armv7-apple-ios");    return true;
1043             case  2: arch.SetTriple ("armv6m-apple-ios");   return true;
1044             case  3: arch.SetTriple ("armv6-apple-ios");    return true;
1045             case  4: arch.SetTriple ("armv5-apple-ios");    return true;
1046             case  5: arch.SetTriple ("armv4-apple-ios");    return true;
1047             case  6: arch.SetTriple ("arm-apple-ios");      return true;
1048             case  7: arch.SetTriple ("thumbv7k-apple-ios"); return true;
1049             case  8: arch.SetTriple ("thumbv7-apple-ios");  return true;
1050             case  9: arch.SetTriple ("thumbv6m-apple-ios"); return true;
1051             case 10: arch.SetTriple ("thumbv6-apple-ios");  return true;
1052             case 11: arch.SetTriple ("thumbv5-apple-ios");  return true;
1053             case 12: arch.SetTriple ("thumbv4t-apple-ios"); return true;
1054             case 13: arch.SetTriple ("thumb-apple-ios");    return true;
1055             default: break;
1056         }
1057         break;
1058 
1059     case ArchSpec::eCore_arm_armv7s:
1060         switch (idx)
1061         {
1062             case  0: arch.SetTriple ("armv7s-apple-ios");   return true;
1063             case  1: arch.SetTriple ("armv7-apple-ios");    return true;
1064             case  2: arch.SetTriple ("armv6m-apple-ios");   return true;
1065             case  3: arch.SetTriple ("armv6-apple-ios");    return true;
1066             case  4: arch.SetTriple ("armv5-apple-ios");    return true;
1067             case  5: arch.SetTriple ("armv4-apple-ios");    return true;
1068             case  6: arch.SetTriple ("arm-apple-ios");      return true;
1069             case  7: arch.SetTriple ("thumbv7s-apple-ios"); return true;
1070             case  8: arch.SetTriple ("thumbv7-apple-ios");  return true;
1071             case  9: arch.SetTriple ("thumbv6m-apple-ios"); return true;
1072             case 10: arch.SetTriple ("thumbv6-apple-ios");  return true;
1073             case 11: arch.SetTriple ("thumbv5-apple-ios");  return true;
1074             case 12: arch.SetTriple ("thumbv4t-apple-ios"); return true;
1075             case 13: arch.SetTriple ("thumb-apple-ios");    return true;
1076             default: break;
1077         }
1078         break;
1079 
1080     case ArchSpec::eCore_arm_armv7m:
1081         switch (idx)
1082         {
1083             case  0: arch.SetTriple ("armv7m-apple-ios");   return true;
1084             case  1: arch.SetTriple ("armv7-apple-ios");    return true;
1085             case  2: arch.SetTriple ("armv6m-apple-ios");   return true;
1086             case  3: arch.SetTriple ("armv6-apple-ios");    return true;
1087             case  4: arch.SetTriple ("armv5-apple-ios");    return true;
1088             case  5: arch.SetTriple ("armv4-apple-ios");    return true;
1089             case  6: arch.SetTriple ("arm-apple-ios");      return true;
1090             case  7: arch.SetTriple ("thumbv7m-apple-ios"); return true;
1091             case  8: arch.SetTriple ("thumbv7-apple-ios");  return true;
1092             case  9: arch.SetTriple ("thumbv6m-apple-ios"); return true;
1093             case 10: arch.SetTriple ("thumbv6-apple-ios");  return true;
1094             case 11: arch.SetTriple ("thumbv5-apple-ios");  return true;
1095             case 12: arch.SetTriple ("thumbv4t-apple-ios"); return true;
1096             case 13: arch.SetTriple ("thumb-apple-ios");    return true;
1097             default: break;
1098         }
1099         break;
1100 
1101     case ArchSpec::eCore_arm_armv7em:
1102         switch (idx)
1103         {
1104             case  0: arch.SetTriple ("armv7em-apple-ios");  return true;
1105             case  1: arch.SetTriple ("armv7-apple-ios");    return true;
1106             case  2: arch.SetTriple ("armv6m-apple-ios");   return true;
1107             case  3: arch.SetTriple ("armv6-apple-ios");    return true;
1108             case  4: arch.SetTriple ("armv5-apple-ios");    return true;
1109             case  5: arch.SetTriple ("armv4-apple-ios");    return true;
1110             case  6: arch.SetTriple ("arm-apple-ios");      return true;
1111             case  7: arch.SetTriple ("thumbv7em-apple-ios"); return true;
1112             case  8: arch.SetTriple ("thumbv7-apple-ios");  return true;
1113             case  9: arch.SetTriple ("thumbv6m-apple-ios"); return true;
1114             case 10: arch.SetTriple ("thumbv6-apple-ios");  return true;
1115             case 11: arch.SetTriple ("thumbv5-apple-ios");  return true;
1116             case 12: arch.SetTriple ("thumbv4t-apple-ios"); return true;
1117             case 13: arch.SetTriple ("thumb-apple-ios");    return true;
1118             default: break;
1119         }
1120         break;
1121 
1122     case ArchSpec::eCore_arm_armv7:
1123         switch (idx)
1124         {
1125             case  0: arch.SetTriple ("armv7-apple-ios");    return true;
1126             case  1: arch.SetTriple ("armv6m-apple-ios");   return true;
1127             case  2: arch.SetTriple ("armv6-apple-ios");    return true;
1128             case  3: arch.SetTriple ("armv5-apple-ios");    return true;
1129             case  4: arch.SetTriple ("armv4-apple-ios");    return true;
1130             case  5: arch.SetTriple ("arm-apple-ios");      return true;
1131             case  6: arch.SetTriple ("thumbv7-apple-ios");  return true;
1132             case  7: arch.SetTriple ("thumbv6m-apple-ios"); return true;
1133             case  8: arch.SetTriple ("thumbv6-apple-ios");  return true;
1134             case  9: arch.SetTriple ("thumbv5-apple-ios");  return true;
1135             case 10: arch.SetTriple ("thumbv4t-apple-ios"); return true;
1136             case 11: arch.SetTriple ("thumb-apple-ios");    return true;
1137             default: break;
1138         }
1139         break;
1140 
1141     case ArchSpec::eCore_arm_armv6m:
1142         switch (idx)
1143         {
1144             case 0: arch.SetTriple ("armv6m-apple-ios");   return true;
1145             case 1: arch.SetTriple ("armv6-apple-ios");    return true;
1146             case 2: arch.SetTriple ("armv5-apple-ios");    return true;
1147             case 3: arch.SetTriple ("armv4-apple-ios");    return true;
1148             case 4: arch.SetTriple ("arm-apple-ios");      return true;
1149             case 5: arch.SetTriple ("thumbv6m-apple-ios"); return true;
1150             case 6: arch.SetTriple ("thumbv6-apple-ios");  return true;
1151             case 7: arch.SetTriple ("thumbv5-apple-ios");  return true;
1152             case 8: arch.SetTriple ("thumbv4t-apple-ios"); return true;
1153             case 9: arch.SetTriple ("thumb-apple-ios");    return true;
1154             default: break;
1155         }
1156         break;
1157 
1158     case ArchSpec::eCore_arm_armv6:
1159         switch (idx)
1160         {
1161             case 0: arch.SetTriple ("armv6-apple-ios");    return true;
1162             case 1: arch.SetTriple ("armv5-apple-ios");    return true;
1163             case 2: arch.SetTriple ("armv4-apple-ios");    return true;
1164             case 3: arch.SetTriple ("arm-apple-ios");      return true;
1165             case 4: arch.SetTriple ("thumbv6-apple-ios");  return true;
1166             case 5: arch.SetTriple ("thumbv5-apple-ios");  return true;
1167             case 6: arch.SetTriple ("thumbv4t-apple-ios"); return true;
1168             case 7: arch.SetTriple ("thumb-apple-ios");    return true;
1169             default: break;
1170         }
1171         break;
1172 
1173     case ArchSpec::eCore_arm_armv5:
1174         switch (idx)
1175         {
1176             case 0: arch.SetTriple ("armv5-apple-ios");    return true;
1177             case 1: arch.SetTriple ("armv4-apple-ios");    return true;
1178             case 2: arch.SetTriple ("arm-apple-ios");      return true;
1179             case 3: arch.SetTriple ("thumbv5-apple-ios");  return true;
1180             case 4: arch.SetTriple ("thumbv4t-apple-ios"); return true;
1181             case 5: arch.SetTriple ("thumb-apple-ios");    return true;
1182             default: break;
1183         }
1184         break;
1185 
1186     case ArchSpec::eCore_arm_armv4:
1187         switch (idx)
1188         {
1189             case 0: arch.SetTriple ("armv4-apple-ios");    return true;
1190             case 1: arch.SetTriple ("arm-apple-ios");      return true;
1191             case 2: arch.SetTriple ("thumbv4t-apple-ios"); return true;
1192             case 3: arch.SetTriple ("thumb-apple-ios");    return true;
1193             default: break;
1194         }
1195         break;
1196     }
1197     arch.Clear();
1198     return false;
1199 }
1200 
1201 
1202 const char *
1203 PlatformDarwin::GetDeveloperDirectory()
1204 {
1205     if (m_developer_directory.empty())
1206     {
1207         bool developer_dir_path_valid = false;
1208         char developer_dir_path[PATH_MAX];
1209         FileSpec temp_file_spec;
1210         if (Host::GetLLDBPath (ePathTypeLLDBShlibDir, temp_file_spec))
1211         {
1212             if (temp_file_spec.GetPath (developer_dir_path, sizeof(developer_dir_path)))
1213             {
1214                 char *shared_frameworks = strstr (developer_dir_path, "/SharedFrameworks/LLDB.framework");
1215                 if (shared_frameworks)
1216                 {
1217                     ::snprintf (shared_frameworks,
1218                                 sizeof(developer_dir_path) - (shared_frameworks - developer_dir_path),
1219                                 "/Developer");
1220                     developer_dir_path_valid = true;
1221                 }
1222                 else
1223                 {
1224                     char *lib_priv_frameworks = strstr (developer_dir_path, "/Library/PrivateFrameworks/LLDB.framework");
1225                     if (lib_priv_frameworks)
1226                     {
1227                         *lib_priv_frameworks = '\0';
1228                         developer_dir_path_valid = true;
1229                     }
1230                 }
1231             }
1232         }
1233 
1234         if (!developer_dir_path_valid)
1235         {
1236             std::string xcode_dir_path;
1237             const char *xcode_select_prefix_dir = getenv ("XCODE_SELECT_PREFIX_DIR");
1238             if (xcode_select_prefix_dir)
1239                 xcode_dir_path.append (xcode_select_prefix_dir);
1240             xcode_dir_path.append ("/usr/share/xcode-select/xcode_dir_path");
1241             temp_file_spec.SetFile(xcode_dir_path.c_str(), false);
1242             size_t bytes_read = temp_file_spec.ReadFileContents(0, developer_dir_path, sizeof(developer_dir_path), NULL);
1243             if (bytes_read > 0)
1244             {
1245                 developer_dir_path[bytes_read] = '\0';
1246                 while (developer_dir_path[bytes_read-1] == '\r' ||
1247                        developer_dir_path[bytes_read-1] == '\n')
1248                     developer_dir_path[--bytes_read] = '\0';
1249                 developer_dir_path_valid = true;
1250             }
1251         }
1252 
1253         if (!developer_dir_path_valid)
1254         {
1255             FileSpec xcode_select_cmd ("/usr/bin/xcode-select", false);
1256             if (xcode_select_cmd.Exists())
1257             {
1258                 int exit_status = -1;
1259                 int signo = -1;
1260                 std::string command_output;
1261                 Error error = Host::RunShellCommand ("/usr/bin/xcode-select --print-path",
1262                                                      NULL,                                 // current working directory
1263                                                      &exit_status,
1264                                                      &signo,
1265                                                      &command_output,
1266                                                      2,                                     // short timeout
1267                                                      NULL);                                 // don't run in a shell
1268                 if (error.Success() && exit_status == 0 && !command_output.empty())
1269                 {
1270                     const char *cmd_output_ptr = command_output.c_str();
1271                     developer_dir_path[sizeof (developer_dir_path) - 1] = '\0';
1272                     size_t i;
1273                     for (i = 0; i < sizeof (developer_dir_path) - 1; i++)
1274                     {
1275                         if (cmd_output_ptr[i] == '\r' || cmd_output_ptr[i] == '\n' || cmd_output_ptr[i] == '\0')
1276                             break;
1277                         developer_dir_path[i] = cmd_output_ptr[i];
1278                     }
1279                     developer_dir_path[i] = '\0';
1280 
1281                     FileSpec devel_dir (developer_dir_path, false);
1282                     if (devel_dir.Exists() && devel_dir.IsDirectory())
1283                     {
1284                         developer_dir_path_valid = true;
1285                     }
1286                 }
1287             }
1288         }
1289 
1290         if (developer_dir_path_valid)
1291         {
1292             temp_file_spec.SetFile (developer_dir_path, false);
1293             if (temp_file_spec.Exists())
1294             {
1295                 m_developer_directory.assign (developer_dir_path);
1296                 return m_developer_directory.c_str();
1297             }
1298         }
1299         // Assign a single NULL character so we know we tried to find the device
1300         // support directory and we don't keep trying to find it over and over.
1301         m_developer_directory.assign (1, '\0');
1302     }
1303 
1304     // We should have put a single NULL character into m_developer_directory
1305     // or it should have a valid path if the code gets here
1306     assert (m_developer_directory.empty() == false);
1307     if (m_developer_directory[0])
1308         return m_developer_directory.c_str();
1309     return NULL;
1310 }
1311 
1312 
1313 BreakpointSP
1314 PlatformDarwin::SetThreadCreationBreakpoint (Target &target)
1315 {
1316     BreakpointSP bp_sp;
1317     static const char *g_bp_names[] =
1318     {
1319         "start_wqthread",
1320         "_pthread_wqthread",
1321         "_pthread_start",
1322     };
1323 
1324     static const char *g_bp_modules[] =
1325     {
1326         "libsystem_c.dylib",
1327         "libSystem.B.dylib"
1328     };
1329 
1330     FileSpecList bp_modules;
1331     for (size_t i = 0; i < llvm::array_lengthof(g_bp_modules); i++)
1332     {
1333         const char *bp_module = g_bp_modules[i];
1334         bp_modules.Append(FileSpec(bp_module, false));
1335     }
1336 
1337     bool internal = true;
1338     bool hardware = false;
1339     LazyBool skip_prologue = eLazyBoolNo;
1340     bp_sp = target.CreateBreakpoint (&bp_modules,
1341                                      NULL,
1342                                      g_bp_names,
1343                                      llvm::array_lengthof(g_bp_names),
1344                                      eFunctionNameTypeFull,
1345                                      skip_prologue,
1346                                      internal,
1347                                      hardware);
1348     bp_sp->SetBreakpointKind("thread-creation");
1349 
1350     return bp_sp;
1351 }
1352 
1353 size_t
1354 PlatformDarwin::GetEnvironment (StringList &env)
1355 {
1356     if (IsRemote())
1357     {
1358         if (m_remote_platform_sp)
1359             return m_remote_platform_sp->GetEnvironment(env);
1360         return 0;
1361     }
1362     return Host::GetEnvironment(env);
1363 }
1364 
1365 int32_t
1366 PlatformDarwin::GetResumeCountForLaunchInfo (ProcessLaunchInfo &launch_info)
1367 {
1368     const char *shell = launch_info.GetShell();
1369     if (shell == NULL)
1370         return 1;
1371 
1372     const char *shell_name = strrchr (shell, '/');
1373     if (shell_name == NULL)
1374         shell_name = shell;
1375     else
1376         shell_name++;
1377 
1378     if (strcmp (shell_name, "sh") == 0)
1379     {
1380         // /bin/sh re-exec's itself as /bin/bash requiring another resume.
1381         // But it only does this if the COMMAND_MODE environment variable
1382         // is set to "legacy".
1383         char * const *envp = (char * const*)launch_info.GetEnvironmentEntries().GetConstArgumentVector();
1384         if (envp != NULL)
1385         {
1386             for (int i = 0; envp[i] != NULL; i++)
1387             {
1388                 if (strcmp (envp[i], "COMMAND_MODE=legacy" ) == 0)
1389                     return 2;
1390             }
1391         }
1392         return 1;
1393     }
1394     else if (strcmp (shell_name, "csh") == 0
1395             || strcmp (shell_name, "tcsh") == 0
1396             || strcmp (shell_name, "zsh") == 0)
1397     {
1398         // csh and tcsh always seem to re-exec themselves.
1399         return 2;
1400     }
1401     else
1402         return 1;
1403 }
1404 
1405 void
1406 PlatformDarwin::CalculateTrapHandlerSymbolNames ()
1407 {
1408     m_trap_handlers.push_back (ConstString ("_sigtramp"));
1409 }
1410