1 //===-- PlatformRemoteAppleWatch.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 "PlatformRemoteAppleWatch.h"
11 
12 // C Includes
13 // C++ Includes
14 // Other libraries and framework includes
15 // Project includes
16 #include "lldb/Breakpoint/BreakpointLocation.h"
17 #include "lldb/Core/ArchSpec.h"
18 #include "lldb/Core/Error.h"
19 #include "lldb/Core/Module.h"
20 #include "lldb/Core/ModuleList.h"
21 #include "lldb/Core/ModuleSpec.h"
22 #include "lldb/Core/PluginManager.h"
23 #include "lldb/Core/StreamString.h"
24 #include "lldb/Host/FileSpec.h"
25 #include "lldb/Host/Host.h"
26 #include "lldb/Target/Process.h"
27 #include "lldb/Target/Target.h"
28 
29 using namespace lldb;
30 using namespace lldb_private;
31 
32 PlatformRemoteAppleWatch::SDKDirectoryInfo::SDKDirectoryInfo (const lldb_private::FileSpec &sdk_dir) :
33     directory(sdk_dir),
34     build(),
35     version_major(0),
36     version_minor(0),
37     version_update(0),
38     user_cached(false)
39 {
40     const char *dirname_cstr = sdk_dir.GetFilename().GetCString();
41     const char *pos = Args::StringToVersion (dirname_cstr,
42                                              version_major,
43                                              version_minor,
44                                              version_update);
45 
46     if (pos && pos[0] == ' ' && pos[1] == '(')
47     {
48         const char *build_start = pos + 2;
49         const char *end_paren = strchr (build_start, ')');
50         if (end_paren && build_start < end_paren)
51             build.SetCStringWithLength(build_start, end_paren - build_start);
52     }
53 }
54 
55 //------------------------------------------------------------------
56 // Static Variables
57 //------------------------------------------------------------------
58 static uint32_t g_initialize_count = 0;
59 
60 //------------------------------------------------------------------
61 // Static Functions
62 //------------------------------------------------------------------
63 void
64 PlatformRemoteAppleWatch::Initialize ()
65 {
66     PlatformDarwin::Initialize ();
67 
68     if (g_initialize_count++ == 0)
69     {
70         PluginManager::RegisterPlugin (PlatformRemoteAppleWatch::GetPluginNameStatic(),
71                                        PlatformRemoteAppleWatch::GetDescriptionStatic(),
72                                        PlatformRemoteAppleWatch::CreateInstance);
73     }
74 }
75 
76 void
77 PlatformRemoteAppleWatch::Terminate ()
78 {
79     if (g_initialize_count > 0)
80     {
81         if (--g_initialize_count == 0)
82         {
83             PluginManager::UnregisterPlugin (PlatformRemoteAppleWatch::CreateInstance);
84         }
85     }
86 
87     PlatformDarwin::Terminate ();
88 }
89 
90 PlatformSP
91 PlatformRemoteAppleWatch::CreateInstance (bool force, const ArchSpec *arch)
92 {
93     bool create = force;
94     if (create == false && arch && arch->IsValid())
95     {
96         switch (arch->GetMachine())
97         {
98         case llvm::Triple::arm:
99         case llvm::Triple::aarch64:
100         case llvm::Triple::thumb:
101             {
102                 const llvm::Triple &triple = arch->GetTriple();
103                 llvm::Triple::VendorType vendor = triple.getVendor();
104                 switch (vendor)
105                 {
106                     case llvm::Triple::Apple:
107                         create = true;
108                         break;
109 
110 #if defined(__APPLE__)
111                     // Only accept "unknown" for the vendor if the host is Apple and
112                     // it "unknown" wasn't specified (it was just returned because it
113                     // was NOT specified)
114                     case llvm::Triple::UnknownArch:
115                         create = !arch->TripleVendorWasSpecified();
116                         break;
117 
118 #endif
119                     default:
120                         break;
121                 }
122                 if (create)
123                 {
124                     switch (triple.getOS())
125                     {
126                         case llvm::Triple::WatchOS:     // This is the right triple value for Apple Watch debugging
127                             break;
128 
129 #if defined(__APPLE__)
130                         // Only accept "unknown" for the OS if the host is Apple and
131                         // it "unknown" wasn't specified (it was just returned because it
132                         // was NOT specified)
133                         case llvm::Triple::UnknownOS:
134                             create = !arch->TripleOSWasSpecified();
135                             break;
136 #endif
137                         default:
138                             create = false;
139                             break;
140                     }
141                 }
142             }
143             break;
144         default:
145             break;
146         }
147     }
148 
149 #if defined(__APPLE__) && (defined(__arm__) || defined(__arm64__) || defined(__aarch64__))
150     // If lldb is running on a watch, this isn't a RemoteWatch environment; it's a local system environment.
151     if (force == false)
152     {
153         create = false;
154     }
155 #endif
156 
157 
158     if (create)
159         return lldb::PlatformSP(new PlatformRemoteAppleWatch ());
160     return lldb::PlatformSP();
161 }
162 
163 
164 lldb_private::ConstString
165 PlatformRemoteAppleWatch::GetPluginNameStatic ()
166 {
167     static ConstString g_name("remote-watchos");
168     return g_name;
169 }
170 
171 const char *
172 PlatformRemoteAppleWatch::GetDescriptionStatic()
173 {
174     return "Remote Apple Watch platform plug-in.";
175 }
176 
177 
178 //------------------------------------------------------------------
179 /// Default Constructor
180 //------------------------------------------------------------------
181 PlatformRemoteAppleWatch::PlatformRemoteAppleWatch () :
182     PlatformDarwin (false),    // This is a remote platform
183     m_sdk_directory_infos(),
184     m_device_support_directory(),
185     m_device_support_directory_for_os_version (),
186     m_build_update(),
187     m_last_module_sdk_idx (UINT32_MAX),
188     m_connected_module_sdk_idx (UINT32_MAX)
189 {
190 }
191 
192 //------------------------------------------------------------------
193 /// Destructor.
194 ///
195 /// The destructor is virtual since this class is designed to be
196 /// inherited from by the plug-in instance.
197 //------------------------------------------------------------------
198 PlatformRemoteAppleWatch::~PlatformRemoteAppleWatch()
199 {
200 }
201 
202 
203 void
204 PlatformRemoteAppleWatch::GetStatus (Stream &strm)
205 {
206     Platform::GetStatus (strm);
207     const char *sdk_directory = GetDeviceSupportDirectoryForOSVersion();
208     if (sdk_directory)
209         strm.Printf ("  SDK Path: \"%s\"\n", sdk_directory);
210     else
211         strm.PutCString ("  SDK Path: error: unable to locate SDK\n");
212 
213     const uint32_t num_sdk_infos = m_sdk_directory_infos.size();
214     for (uint32_t i=0; i<num_sdk_infos; ++i)
215     {
216         const SDKDirectoryInfo &sdk_dir_info = m_sdk_directory_infos[i];
217         strm.Printf (" SDK Roots: [%2u] \"%s\"\n",
218                      i,
219                      sdk_dir_info.directory.GetPath().c_str());
220     }
221 }
222 
223 
224 Error
225 PlatformRemoteAppleWatch::ResolveExecutable (const ModuleSpec &ms,
226                                           lldb::ModuleSP &exe_module_sp,
227                                           const FileSpecList *module_search_paths_ptr)
228 {
229     Error error;
230     // Nothing special to do here, just use the actual file and architecture
231 
232     ModuleSpec resolved_module_spec(ms);
233 
234     // Resolve any executable within a bundle on MacOSX
235     // TODO: verify that this handles shallow bundles, if not then implement one ourselves
236     Host::ResolveExecutableInBundle (resolved_module_spec.GetFileSpec());
237 
238     if (resolved_module_spec.GetFileSpec().Exists())
239     {
240         if (resolved_module_spec.GetArchitecture().IsValid() || resolved_module_spec.GetUUID().IsValid())
241         {
242             error = ModuleList::GetSharedModule (resolved_module_spec,
243                                                  exe_module_sp,
244                                                  NULL,
245                                                  NULL,
246                                                  NULL);
247 
248             if (exe_module_sp && exe_module_sp->GetObjectFile())
249                 return error;
250             exe_module_sp.reset();
251         }
252         // No valid architecture was specified or the exact ARM slice wasn't
253         // found so ask the platform for the architectures that we should be
254         // using (in the correct order) and see if we can find a match that way
255         StreamString arch_names;
256         for (uint32_t idx = 0; GetSupportedArchitectureAtIndex (idx, resolved_module_spec.GetArchitecture()); ++idx)
257         {
258             error = ModuleList::GetSharedModule (resolved_module_spec,
259                                                  exe_module_sp,
260                                                  NULL,
261                                                  NULL,
262                                                  NULL);
263             // Did we find an executable using one of the
264             if (error.Success())
265             {
266                 if (exe_module_sp && exe_module_sp->GetObjectFile())
267                     break;
268                 else
269                     error.SetErrorToGenericError();
270             }
271 
272             if (idx > 0)
273                 arch_names.PutCString (", ");
274             arch_names.PutCString (resolved_module_spec.GetArchitecture().GetArchitectureName());
275         }
276 
277         if (error.Fail() || !exe_module_sp)
278         {
279             if (resolved_module_spec.GetFileSpec().Readable())
280             {
281                 error.SetErrorStringWithFormat ("'%s' doesn't contain any '%s' platform architectures: %s",
282                                                 resolved_module_spec.GetFileSpec().GetPath().c_str(),
283                                                 GetPluginName().GetCString(),
284                                                 arch_names.GetString().c_str());
285             }
286             else
287             {
288                 error.SetErrorStringWithFormat("'%s' is not readable", resolved_module_spec.GetFileSpec().GetPath().c_str());
289             }
290         }
291     }
292     else
293     {
294         error.SetErrorStringWithFormat ("'%s' does not exist",
295                                         resolved_module_spec.GetFileSpec().GetPath().c_str());
296     }
297 
298     return error;
299 }
300 
301 FileSpec::EnumerateDirectoryResult
302 PlatformRemoteAppleWatch::GetContainedFilesIntoVectorOfStringsCallback (void *baton,
303                                                                      FileSpec::FileType file_type,
304                                                                      const FileSpec &file_spec)
305 {
306     ((PlatformRemoteAppleWatch::SDKDirectoryInfoCollection *)baton)->push_back(PlatformRemoteAppleWatch::SDKDirectoryInfo(file_spec));
307     return FileSpec::eEnumerateDirectoryResultNext;
308 }
309 
310 bool
311 PlatformRemoteAppleWatch::UpdateSDKDirectoryInfosIfNeeded()
312 {
313     if (m_sdk_directory_infos.empty())
314     {
315         const char *device_support_dir = GetDeviceSupportDirectory();
316         if (device_support_dir)
317         {
318             const bool find_directories = true;
319             const bool find_files = false;
320             const bool find_other = false;
321 
322             SDKDirectoryInfoCollection builtin_sdk_directory_infos;
323             FileSpec::EnumerateDirectory (m_device_support_directory.c_str(),
324                                           find_directories,
325                                           find_files,
326                                           find_other,
327                                           GetContainedFilesIntoVectorOfStringsCallback,
328                                           &builtin_sdk_directory_infos);
329 
330             // Only add SDK directories that have symbols in them, some SDKs only contain
331             // developer disk images and no symbols, so they aren't useful to us.
332             FileSpec sdk_symbols_symlink_fspec;
333             for (const auto &sdk_directory_info : builtin_sdk_directory_infos)
334             {
335                 sdk_symbols_symlink_fspec = sdk_directory_info.directory;
336                 sdk_symbols_symlink_fspec.AppendPathComponent("Symbols.Internal");
337                 if (sdk_symbols_symlink_fspec.Exists())
338                 {
339                     m_sdk_directory_infos.push_back(sdk_directory_info);
340                 }
341                 else
342                 {
343                     sdk_symbols_symlink_fspec.GetFilename().SetCString("Symbols");
344                     if (sdk_symbols_symlink_fspec.Exists())
345                         m_sdk_directory_infos.push_back(sdk_directory_info);
346                 }
347             }
348 
349             const uint32_t num_installed = m_sdk_directory_infos.size();
350             FileSpec local_sdk_cache("~/Library/Developer/Xcode/watchOS DeviceSupport", true);
351             if (!local_sdk_cache.Exists())
352             {
353                 local_sdk_cache = FileSpec("~/Library/Developer/Xcode/watch OS DeviceSupport", true);
354             }
355             if (!local_sdk_cache.Exists())
356             {
357                 local_sdk_cache = FileSpec("~/Library/Developer/Xcode/WatchOS DeviceSupport", true);
358             }
359             if (!local_sdk_cache.Exists())
360             {
361                 local_sdk_cache = FileSpec("~/Library/Developer/Xcode/Watch OS DeviceSupport", true);
362             }
363             if (local_sdk_cache.Exists())
364             {
365                 char path[PATH_MAX];
366                 if (local_sdk_cache.GetPath(path, sizeof(path)))
367                 {
368                     FileSpec::EnumerateDirectory (path,
369                                                   find_directories,
370                                                   find_files,
371                                                   find_other,
372                                                   GetContainedFilesIntoVectorOfStringsCallback,
373                                                   &m_sdk_directory_infos);
374                     const uint32_t num_sdk_infos = m_sdk_directory_infos.size();
375                     // First try for an exact match of major, minor and update
376                     for (uint32_t i=num_installed; i<num_sdk_infos; ++i)
377                     {
378                         m_sdk_directory_infos[i].user_cached = true;
379                     }
380                 }
381             }
382         }
383     }
384     return !m_sdk_directory_infos.empty();
385 }
386 
387 const PlatformRemoteAppleWatch::SDKDirectoryInfo *
388 PlatformRemoteAppleWatch::GetSDKDirectoryForCurrentOSVersion ()
389 {
390     uint32_t i;
391     if (UpdateSDKDirectoryInfosIfNeeded())
392     {
393         const uint32_t num_sdk_infos = m_sdk_directory_infos.size();
394 
395         // Check to see if the user specified a build string. If they did, then
396         // be sure to match it.
397         std::vector<bool> check_sdk_info(num_sdk_infos, true);
398         ConstString build(m_sdk_build);
399         if (build)
400         {
401             for (i=0; i<num_sdk_infos; ++i)
402                 check_sdk_info[i] = m_sdk_directory_infos[i].build == build;
403         }
404 
405         // If we are connected we can find the version of the OS the platform
406         // us running on and select the right SDK
407         uint32_t major, minor, update;
408         if (GetOSVersion(major, minor, update))
409         {
410             if (UpdateSDKDirectoryInfosIfNeeded())
411             {
412                 // First try for an exact match of major, minor and update
413                 for (i=0; i<num_sdk_infos; ++i)
414                 {
415                     if (check_sdk_info[i])
416                     {
417                         if (m_sdk_directory_infos[i].version_major == major &&
418                             m_sdk_directory_infos[i].version_minor == minor &&
419                             m_sdk_directory_infos[i].version_update == update)
420                         {
421                             return &m_sdk_directory_infos[i];
422                         }
423                     }
424                 }
425                 // First try for an exact match of major and minor
426                 for (i=0; i<num_sdk_infos; ++i)
427                 {
428                     if (check_sdk_info[i])
429                     {
430                         if (m_sdk_directory_infos[i].version_major == major &&
431                             m_sdk_directory_infos[i].version_minor == minor)
432                         {
433                             return &m_sdk_directory_infos[i];
434                         }
435                     }
436                 }
437                 // Lastly try to match of major version only..
438                 for (i=0; i<num_sdk_infos; ++i)
439                 {
440                     if (check_sdk_info[i])
441                     {
442                         if (m_sdk_directory_infos[i].version_major == major)
443                         {
444                             return &m_sdk_directory_infos[i];
445                         }
446                     }
447                 }
448             }
449         }
450         else if (build)
451         {
452             // No version, just a build number, search for the first one that matches
453             for (i=0; i<num_sdk_infos; ++i)
454                 if (check_sdk_info[i])
455                     return &m_sdk_directory_infos[i];
456         }
457     }
458     return NULL;
459 }
460 
461 const PlatformRemoteAppleWatch::SDKDirectoryInfo *
462 PlatformRemoteAppleWatch::GetSDKDirectoryForLatestOSVersion ()
463 {
464     const PlatformRemoteAppleWatch::SDKDirectoryInfo *result = NULL;
465     if (UpdateSDKDirectoryInfosIfNeeded())
466     {
467         const uint32_t num_sdk_infos = m_sdk_directory_infos.size();
468         // First try for an exact match of major, minor and update
469         for (uint32_t i=0; i<num_sdk_infos; ++i)
470         {
471             const SDKDirectoryInfo &sdk_dir_info = m_sdk_directory_infos[i];
472             if (sdk_dir_info.version_major != UINT32_MAX)
473             {
474                 if (result == NULL || sdk_dir_info.version_major > result->version_major)
475                 {
476                     result = &sdk_dir_info;
477                 }
478                 else if (sdk_dir_info.version_major == result->version_major)
479                 {
480                     if (sdk_dir_info.version_minor > result->version_minor)
481                     {
482                         result = &sdk_dir_info;
483                     }
484                     else if (sdk_dir_info.version_minor == result->version_minor)
485                     {
486                         if (sdk_dir_info.version_update > result->version_update)
487                         {
488                             result = &sdk_dir_info;
489                         }
490                     }
491                 }
492             }
493         }
494     }
495     return result;
496 }
497 
498 
499 
500 const char *
501 PlatformRemoteAppleWatch::GetDeviceSupportDirectory()
502 {
503     if (m_device_support_directory.empty())
504     {
505         const char *device_support_dir = GetDeveloperDirectory();
506         if (device_support_dir)
507         {
508             m_device_support_directory.assign (device_support_dir);
509             m_device_support_directory.append ("/Platforms/watchOS.platform/DeviceSupport");
510             FileSpec platform_device_support_dir (m_device_support_directory.c_str(), true);
511             if (!platform_device_support_dir.Exists())
512             {
513                 std::string alt_platform_dirname = device_support_dir;
514                 alt_platform_dirname.append ("/Platforms/WatchOS.platform/DeviceSupport");
515                 FileSpec alt_platform_device_support_dir (m_device_support_directory.c_str(), true);
516                 if (alt_platform_device_support_dir.Exists())
517                 {
518                     m_device_support_directory = alt_platform_dirname;
519                 }
520             }
521         }
522         else
523         {
524             // Assign a single NULL character so we know we tried to find the device
525             // support directory and we don't keep trying to find it over and over.
526             m_device_support_directory.assign (1, '\0');
527         }
528     }
529     // We should have put a single NULL character into m_device_support_directory
530     // or it should have a valid path if the code gets here
531     assert (m_device_support_directory.empty() == false);
532     if (m_device_support_directory[0])
533         return m_device_support_directory.c_str();
534     return NULL;
535 }
536 
537 
538 const char *
539 PlatformRemoteAppleWatch::GetDeviceSupportDirectoryForOSVersion()
540 {
541     if (m_sdk_sysroot)
542         return m_sdk_sysroot.GetCString();
543 
544     if (m_device_support_directory_for_os_version.empty())
545     {
546         const PlatformRemoteAppleWatch::SDKDirectoryInfo *sdk_dir_info = GetSDKDirectoryForCurrentOSVersion ();
547         if (sdk_dir_info == NULL)
548             sdk_dir_info = GetSDKDirectoryForLatestOSVersion ();
549         if (sdk_dir_info)
550         {
551             char path[PATH_MAX];
552             if (sdk_dir_info->directory.GetPath(path, sizeof(path)))
553             {
554                 m_device_support_directory_for_os_version = path;
555                 return m_device_support_directory_for_os_version.c_str();
556             }
557         }
558         else
559         {
560             // Assign a single NULL character so we know we tried to find the device
561             // support directory and we don't keep trying to find it over and over.
562             m_device_support_directory_for_os_version.assign (1, '\0');
563         }
564     }
565     // We should have put a single NULL character into m_device_support_directory_for_os_version
566     // or it should have a valid path if the code gets here
567     assert (m_device_support_directory_for_os_version.empty() == false);
568     if (m_device_support_directory_for_os_version[0])
569         return m_device_support_directory_for_os_version.c_str();
570     return NULL;
571 }
572 
573 uint32_t
574 PlatformRemoteAppleWatch::FindFileInAllSDKs (const char *platform_file_path,
575                                       FileSpecList &file_list)
576 {
577     if (platform_file_path && platform_file_path[0] && UpdateSDKDirectoryInfosIfNeeded())
578     {
579         const uint32_t num_sdk_infos = m_sdk_directory_infos.size();
580         lldb_private::FileSpec local_file;
581         // First try for an exact match of major, minor and update
582         for (uint32_t sdk_idx=0; sdk_idx<num_sdk_infos; ++sdk_idx)
583         {
584             if (GetFileInSDK (platform_file_path,
585                               sdk_idx,
586                               local_file))
587             {
588                 file_list.Append(local_file);
589             }
590         }
591     }
592     return file_list.GetSize();
593 }
594 
595 bool
596 PlatformRemoteAppleWatch::GetFileInSDK (const char *platform_file_path,
597                                  uint32_t sdk_idx,
598                                  lldb_private::FileSpec &local_file)
599 {
600     if (sdk_idx < m_sdk_directory_infos.size())
601     {
602         char sdkroot_path[PATH_MAX];
603         const SDKDirectoryInfo &sdk_dir_info = m_sdk_directory_infos[sdk_idx];
604         if (sdk_dir_info.directory.GetPath(sdkroot_path, sizeof(sdkroot_path)))
605         {
606             const bool symbols_dirs_only = true;
607 
608             return GetFileInSDKRoot (platform_file_path,
609                                      sdkroot_path,
610                                      symbols_dirs_only,
611                                      local_file);
612         }
613     }
614     return false;
615 }
616 
617 
618 bool
619 PlatformRemoteAppleWatch::GetFileInSDKRoot (const char *platform_file_path,
620                                      const char *sdkroot_path,
621                                      bool symbols_dirs_only,
622                                      lldb_private::FileSpec &local_file)
623 {
624     if (sdkroot_path && sdkroot_path[0] && platform_file_path && platform_file_path[0])
625     {
626         char resolved_path[PATH_MAX];
627 
628         if (!symbols_dirs_only)
629         {
630             ::snprintf (resolved_path,
631                         sizeof(resolved_path),
632                         "%s%s",
633                         sdkroot_path,
634                         platform_file_path);
635 
636             local_file.SetFile(resolved_path, true);
637             if (local_file.Exists())
638                 return true;
639         }
640 
641         ::snprintf (resolved_path,
642                     sizeof(resolved_path),
643                     "%s/Symbols.Internal%s",
644                     sdkroot_path,
645                     platform_file_path);
646 
647         local_file.SetFile(resolved_path, true);
648         if (local_file.Exists())
649             return true;
650         ::snprintf (resolved_path,
651                     sizeof(resolved_path),
652                     "%s/Symbols%s",
653                     sdkroot_path,
654                     platform_file_path);
655 
656         local_file.SetFile(resolved_path, true);
657         if (local_file.Exists())
658             return true;
659     }
660     return false;
661 }
662 
663 
664 Error
665 PlatformRemoteAppleWatch::GetSymbolFile (const FileSpec &platform_file,
666                                   const UUID *uuid_ptr,
667                                   FileSpec &local_file)
668 {
669     Error error;
670     char platform_file_path[PATH_MAX];
671     if (platform_file.GetPath(platform_file_path, sizeof(platform_file_path)))
672     {
673         char resolved_path[PATH_MAX];
674 
675         const char * os_version_dir = GetDeviceSupportDirectoryForOSVersion();
676         if (os_version_dir)
677         {
678             ::snprintf (resolved_path,
679                         sizeof(resolved_path),
680                         "%s/%s",
681                         os_version_dir,
682                         platform_file_path);
683 
684             local_file.SetFile(resolved_path, true);
685             if (local_file.Exists())
686                 return error;
687 
688             ::snprintf (resolved_path,
689                         sizeof(resolved_path),
690                         "%s/Symbols.Internal/%s",
691                         os_version_dir,
692                         platform_file_path);
693 
694             local_file.SetFile(resolved_path, true);
695             if (local_file.Exists())
696                 return error;
697             ::snprintf (resolved_path,
698                         sizeof(resolved_path),
699                         "%s/Symbols/%s",
700                         os_version_dir,
701                         platform_file_path);
702 
703             local_file.SetFile(resolved_path, true);
704             if (local_file.Exists())
705                 return error;
706 
707         }
708         local_file = platform_file;
709         if (local_file.Exists())
710             return error;
711 
712         error.SetErrorStringWithFormat ("unable to locate a platform file for '%s' in platform '%s'",
713                                         platform_file_path,
714                                         GetPluginName().GetCString());
715     }
716     else
717     {
718         error.SetErrorString ("invalid platform file argument");
719     }
720     return error;
721 }
722 
723 Error
724 PlatformRemoteAppleWatch::GetSharedModule (const ModuleSpec &module_spec,
725                                            lldb_private::Process* process,
726                                            ModuleSP &module_sp,
727                                            const FileSpecList *module_search_paths_ptr,
728                                            ModuleSP *old_module_sp_ptr,
729                                            bool *did_create_ptr)
730 {
731     // For Apple Watch, the SDK files are all cached locally on the host
732     // system. So first we ask for the file in the cached SDK,
733     // then we attempt to get a shared module for the right architecture
734     // with the right UUID.
735     const FileSpec &platform_file = module_spec.GetFileSpec();
736 
737     Error error;
738     char platform_file_path[PATH_MAX];
739 
740     if (platform_file.GetPath(platform_file_path, sizeof(platform_file_path)))
741     {
742         ModuleSpec platform_module_spec(module_spec);
743 
744         UpdateSDKDirectoryInfosIfNeeded();
745 
746         const uint32_t num_sdk_infos = m_sdk_directory_infos.size();
747 
748         // If we are connected we migth be able to correctly deduce the SDK directory
749         // using the OS build.
750         const uint32_t connected_sdk_idx = GetConnectedSDKIndex ();
751         if (connected_sdk_idx < num_sdk_infos)
752         {
753             if (GetFileInSDK (platform_file_path, connected_sdk_idx, platform_module_spec.GetFileSpec()))
754             {
755                 module_sp.reset();
756                 error = ResolveExecutable (platform_module_spec,
757                                            module_sp,
758                                            NULL);
759                 if (module_sp)
760                 {
761                     m_last_module_sdk_idx = connected_sdk_idx;
762                     error.Clear();
763                     return error;
764                 }
765             }
766         }
767 
768         // Try the last SDK index if it is set as most files from an SDK
769         // will tend to be valid in that same SDK.
770         if (m_last_module_sdk_idx < num_sdk_infos)
771         {
772             if (GetFileInSDK (platform_file_path, m_last_module_sdk_idx, platform_module_spec.GetFileSpec()))
773             {
774                 module_sp.reset();
775                 error = ResolveExecutable (platform_module_spec,
776                                            module_sp,
777                                            NULL);
778                 if (module_sp)
779                 {
780                     error.Clear();
781                     return error;
782                 }
783             }
784         }
785 
786         // First try for an exact match of major, minor and update
787         for (uint32_t sdk_idx=0; sdk_idx<num_sdk_infos; ++sdk_idx)
788         {
789             if (m_last_module_sdk_idx == sdk_idx)
790             {
791                 // Skip the last module SDK index if we already searched
792                 // it above
793                 continue;
794             }
795             if (GetFileInSDK (platform_file_path, sdk_idx, platform_module_spec.GetFileSpec()))
796             {
797                 //printf ("sdk[%u]: '%s'\n", sdk_idx, local_file.GetPath().c_str());
798 
799                 error = ResolveExecutable (platform_module_spec, module_sp, NULL);
800                 if (module_sp)
801                 {
802                     // Remember the index of the last SDK that we found a file
803                     // in in case the wrong SDK was selected.
804                     m_last_module_sdk_idx = sdk_idx;
805                     error.Clear();
806                     return error;
807                 }
808             }
809         }
810     }
811     // Not the module we are looking for... Nothing to see here...
812     module_sp.reset();
813 
814     // This may not be an SDK-related module.  Try whether we can bring in the thing to our local cache.
815     error = GetSharedModuleWithLocalCache(module_spec, module_sp, module_search_paths_ptr, old_module_sp_ptr, did_create_ptr);
816     if (error.Success())
817         return error;
818 
819     const bool always_create = false;
820     error = ModuleList::GetSharedModule (module_spec,
821                                          module_sp,
822                                          module_search_paths_ptr,
823                                          old_module_sp_ptr,
824                                          did_create_ptr,
825                                          always_create);
826 
827     if (module_sp)
828         module_sp->SetPlatformFileSpec(platform_file);
829 
830     return error;
831 }
832 
833 bool
834 PlatformRemoteAppleWatch::GetSupportedArchitectureAtIndex (uint32_t idx, ArchSpec &arch)
835 {
836     ArchSpec system_arch (GetSystemArchitecture());
837 
838     const ArchSpec::Core system_core = system_arch.GetCore();
839     switch (system_core)
840     {
841     default:
842         switch (idx)
843         {
844             case  0: arch.SetTriple ("arm64-apple-watchos");    return true;
845             case  1: arch.SetTriple ("armv7k-apple-watchos");   return true;
846             case  2: arch.SetTriple ("armv7s-apple-watchos");   return true;
847             case  3: arch.SetTriple ("armv7-apple-watchos");    return true;
848             case  4: arch.SetTriple ("thumbv7k-apple-watchos");   return true;
849             case  5: arch.SetTriple ("thumbv7-apple-watchos");    return true;
850             case  6: arch.SetTriple ("thumbv7s-apple-watchos");   return true;
851             default: break;
852         }
853         break;
854 
855     case ArchSpec::eCore_arm_arm64:
856         switch (idx)
857         {
858             case  0: arch.SetTriple ("arm64-apple-watchos");    return true;
859             case  1: arch.SetTriple ("armv7k-apple-watchos");   return true;
860             case  2: arch.SetTriple ("armv7s-apple-watchos");   return true;
861             case  3: arch.SetTriple ("armv7-apple-watchos");    return true;
862             case  4: arch.SetTriple ("thumbv7k-apple-watchos");   return true;
863             case  5: arch.SetTriple ("thumbv7-apple-watchos");    return true;
864             case  6: arch.SetTriple ("thumbv7s-apple-watchos");   return true;
865         default: break;
866         }
867         break;
868 
869     case ArchSpec::eCore_arm_armv7k:
870         switch (idx)
871         {
872             case  0: arch.SetTriple ("armv7k-apple-watchos");   return true;
873             case  1: arch.SetTriple ("armv7s-apple-watchos");   return true;
874             case  2: arch.SetTriple ("armv7-apple-watchos");    return true;
875             case  3: arch.SetTriple ("thumbv7k-apple-watchos");   return true;
876             case  4: arch.SetTriple ("thumbv7-apple-watchos");    return true;
877             case  5: arch.SetTriple ("thumbv7s-apple-watchos");   return true;
878             default: break;
879         }
880         break;
881 
882     case ArchSpec::eCore_arm_armv7s:
883         switch (idx)
884         {
885             case  0: arch.SetTriple ("armv7s-apple-watchos");   return true;
886             case  1: arch.SetTriple ("armv7k-apple-watchos");   return true;
887             case  2: arch.SetTriple ("armv7-apple-watchos");    return true;
888             case  3: arch.SetTriple ("thumbv7k-apple-watchos");   return true;
889             case  4: arch.SetTriple ("thumbv7-apple-watchos");    return true;
890             case  5: arch.SetTriple ("thumbv7s-apple-watchos");   return true;
891             default: break;
892         }
893         break;
894 
895     case ArchSpec::eCore_arm_armv7:
896         switch (idx)
897         {
898             case  0: arch.SetTriple ("armv7-apple-watchos");    return true;
899             case  1: arch.SetTriple ("armv7k-apple-watchos");   return true;
900             case  2: arch.SetTriple ("thumbv7k-apple-watchos");   return true;
901             case  3: arch.SetTriple ("thumbv7-apple-watchos");    return true;
902             default: break;
903         }
904         break;
905 
906     }
907     arch.Clear();
908     return false;
909 }
910 
911 uint32_t
912 PlatformRemoteAppleWatch::GetConnectedSDKIndex ()
913 {
914     if (IsConnected())
915     {
916         if (m_connected_module_sdk_idx == UINT32_MAX)
917         {
918             std::string build;
919             if (GetRemoteOSBuildString(build))
920             {
921                 const uint32_t num_sdk_infos = m_sdk_directory_infos.size();
922                 for (uint32_t i=0; i<num_sdk_infos; ++i)
923                 {
924                     const SDKDirectoryInfo &sdk_dir_info = m_sdk_directory_infos[i];
925                     if (strstr(sdk_dir_info.directory.GetFilename().AsCString(""), build.c_str()))
926                     {
927                         m_connected_module_sdk_idx = i;
928                     }
929                 }
930             }
931         }
932     }
933     else
934     {
935         m_connected_module_sdk_idx = UINT32_MAX;
936     }
937     return m_connected_module_sdk_idx;
938 }
939 
940