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