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                         default:
162                             create = false;
163                             break;
164                     }
165                 }
166             }
167             break;
168         default:
169             break;
170         }
171     }
172 
173     if (create)
174     {
175         if (log)
176             log->Printf ("PlatformRemoteAppleTV::%s() creating platform", __FUNCTION__);
177 
178         return lldb::PlatformSP(new PlatformRemoteAppleTV ());
179     }
180 
181     if (log)
182         log->Printf ("PlatformRemoteAppleTV::%s() aborting creation of platform", __FUNCTION__);
183 
184     return lldb::PlatformSP();
185 }
186 
187 lldb_private::ConstString
188 PlatformRemoteAppleTV::GetPluginNameStatic ()
189 {
190     static ConstString g_name("remote-tvos");
191     return g_name;
192 }
193 
194 const char *
195 PlatformRemoteAppleTV::GetDescriptionStatic()
196 {
197     return "Remote Apple TV platform plug-in.";
198 }
199 
200 void
201 PlatformRemoteAppleTV::GetStatus (Stream &strm)
202 {
203     Platform::GetStatus (strm);
204     const char *sdk_directory = GetDeviceSupportDirectoryForOSVersion();
205     if (sdk_directory)
206         strm.Printf ("  SDK Path: \"%s\"\n", sdk_directory);
207     else
208         strm.PutCString ("  SDK Path: error: unable to locate SDK\n");
209 
210     const uint32_t num_sdk_infos = m_sdk_directory_infos.size();
211     for (uint32_t i=0; i<num_sdk_infos; ++i)
212     {
213         const SDKDirectoryInfo &sdk_dir_info = m_sdk_directory_infos[i];
214         strm.Printf (" SDK Roots: [%2u] \"%s\"\n",
215                      i,
216                      sdk_dir_info.directory.GetPath().c_str());
217     }
218 }
219 
220 Error
221 PlatformRemoteAppleTV::ResolveExecutable (const ModuleSpec &ms,
222                                           lldb::ModuleSP &exe_module_sp,
223                                           const FileSpecList *module_search_paths_ptr)
224 {
225     Error error;
226     // Nothing special to do here, just use the actual file and architecture
227 
228     ModuleSpec resolved_module_spec(ms);
229 
230     // Resolve any executable within a bundle on MacOSX
231     // TODO: verify that this handles shallow bundles, if not then implement one ourselves
232     Host::ResolveExecutableInBundle (resolved_module_spec.GetFileSpec());
233 
234     if (resolved_module_spec.GetFileSpec().Exists())
235     {
236         if (resolved_module_spec.GetArchitecture().IsValid() || resolved_module_spec.GetUUID().IsValid())
237         {
238             error = ModuleList::GetSharedModule(resolved_module_spec,
239                                                 exe_module_sp,
240                                                 nullptr,
241                                                 nullptr,
242                                                 nullptr);
243 
244             if (exe_module_sp && exe_module_sp->GetObjectFile())
245                 return error;
246             exe_module_sp.reset();
247         }
248         // No valid architecture was specified or the exact ARM slice wasn't
249         // found so ask the platform for the architectures that we should be
250         // using (in the correct order) and see if we can find a match that way
251         StreamString arch_names;
252         for (uint32_t idx = 0; GetSupportedArchitectureAtIndex (idx, resolved_module_spec.GetArchitecture()); ++idx)
253         {
254             error = ModuleList::GetSharedModule(resolved_module_spec,
255                                                 exe_module_sp,
256                                                 nullptr,
257                                                 nullptr,
258                                                 nullptr);
259             // Did we find an executable using one of the
260             if (error.Success())
261             {
262                 if (exe_module_sp && exe_module_sp->GetObjectFile())
263                     break;
264                 else
265                     error.SetErrorToGenericError();
266             }
267 
268             if (idx > 0)
269                 arch_names.PutCString (", ");
270             arch_names.PutCString (resolved_module_spec.GetArchitecture().GetArchitectureName());
271         }
272 
273         if (error.Fail() || !exe_module_sp)
274         {
275             if (resolved_module_spec.GetFileSpec().Readable())
276             {
277                 error.SetErrorStringWithFormat ("'%s' doesn't contain any '%s' platform architectures: %s",
278                                                 resolved_module_spec.GetFileSpec().GetPath().c_str(),
279                                                 GetPluginName().GetCString(),
280                                                 arch_names.GetString().c_str());
281             }
282             else
283             {
284                 error.SetErrorStringWithFormat("'%s' is not readable", resolved_module_spec.GetFileSpec().GetPath().c_str());
285             }
286         }
287     }
288     else
289     {
290         error.SetErrorStringWithFormat ("'%s' does not exist",
291                                         resolved_module_spec.GetFileSpec().GetPath().c_str());
292     }
293 
294     return error;
295 }
296 
297 FileSpec::EnumerateDirectoryResult
298 PlatformRemoteAppleTV::GetContainedFilesIntoVectorOfStringsCallback (void *baton,
299                                                                      FileSpec::FileType file_type,
300                                                                      const FileSpec &file_spec)
301 {
302     ((PlatformRemoteAppleTV::SDKDirectoryInfoCollection *)baton)->push_back(PlatformRemoteAppleTV::SDKDirectoryInfo(file_spec));
303     return FileSpec::eEnumerateDirectoryResultNext;
304 }
305 
306 bool
307 PlatformRemoteAppleTV::UpdateSDKDirectoryInfosIfNeeded()
308 {
309     Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST);
310     if (m_sdk_directory_infos.empty())
311     {
312         const char *device_support_dir = GetDeviceSupportDirectory();
313         if (log)
314         {
315             log->Printf ("PlatformRemoteAppleTV::UpdateSDKDirectoryInfosIfNeeded Got DeviceSupport directory %s", device_support_dir);
316         }
317         if (device_support_dir)
318         {
319             const bool find_directories = true;
320             const bool find_files = false;
321             const bool find_other = false;
322 
323             SDKDirectoryInfoCollection builtin_sdk_directory_infos;
324             FileSpec::EnumerateDirectory (m_device_support_directory.c_str(),
325                                           find_directories,
326                                           find_files,
327                                           find_other,
328                                           GetContainedFilesIntoVectorOfStringsCallback,
329                                           &builtin_sdk_directory_infos);
330 
331             // Only add SDK directories that have symbols in them, some SDKs only contain
332             // developer disk images and no symbols, so they aren't useful to us.
333             FileSpec sdk_symbols_symlink_fspec;
334             for (const auto &sdk_directory_info : builtin_sdk_directory_infos)
335             {
336                 sdk_symbols_symlink_fspec = sdk_directory_info.directory;
337                 sdk_symbols_symlink_fspec.AppendPathComponent("Symbols.Internal");
338                 if (sdk_symbols_symlink_fspec.Exists())
339                 {
340                     m_sdk_directory_infos.push_back(sdk_directory_info);
341                     if (log)
342                     {
343                         log->Printf ("PlatformRemoteAppleTV::UpdateSDKDirectoryInfosIfNeeded added builtin SDK directory %s", sdk_symbols_symlink_fspec.GetPath().c_str());
344                     }
345                 }
346                 else
347                 {
348                     sdk_symbols_symlink_fspec.GetFilename().SetCString("Symbols");
349                     if (sdk_symbols_symlink_fspec.Exists())
350                         m_sdk_directory_infos.push_back(sdk_directory_info);
351                     if (log)
352                     {
353                         log->Printf ("PlatformRemoteAppleTV::UpdateSDKDirectoryInfosIfNeeded added builtin SDK directory %s", sdk_symbols_symlink_fspec.GetPath().c_str());
354                     }
355                 }
356             }
357 
358             const uint32_t num_installed = m_sdk_directory_infos.size();
359             FileSpec local_sdk_cache("~/Library/Developer/Xcode/tvOS DeviceSupport", true);
360             if (!local_sdk_cache.Exists())
361             {
362                 // Try looking for another possible name
363                 local_sdk_cache = FileSpec("~/Library/Developer/Xcode/Apple TVOS 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/AppleTVOS 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/AppleTV OS DeviceSupport", true);
374             }
375             if (!local_sdk_cache.Exists())
376             {
377                 // Try looking for another possible name
378                 local_sdk_cache = FileSpec("~/Library/Developer/Xcode/Apple TV OS DeviceSupport", true);
379             }
380             if (local_sdk_cache.Exists())
381             {
382                 if (log)
383                 {
384                     log->Printf ("PlatformRemoteAppleTV::UpdateSDKDirectoryInfosIfNeeded searching %s for additional SDKs", local_sdk_cache.GetPath().c_str());
385                 }
386                 char path[PATH_MAX];
387                 if (local_sdk_cache.GetPath(path, sizeof(path)))
388                 {
389                     FileSpec::EnumerateDirectory (path,
390                                                   find_directories,
391                                                   find_files,
392                                                   find_other,
393                                                   GetContainedFilesIntoVectorOfStringsCallback,
394                                                   &m_sdk_directory_infos);
395                     const uint32_t num_sdk_infos = m_sdk_directory_infos.size();
396                     // First try for an exact match of major, minor and update
397                     for (uint32_t i=num_installed; i<num_sdk_infos; ++i)
398                     {
399                         m_sdk_directory_infos[i].user_cached = true;
400                         if (log)
401                         {
402                             log->Printf ("PlatformRemoteAppleTV::UpdateSDKDirectoryInfosIfNeeded user SDK directory %s", m_sdk_directory_infos[i].directory.GetPath().c_str());
403                         }
404                     }
405                 }
406             }
407         }
408     }
409     return !m_sdk_directory_infos.empty();
410 }
411 
412 const PlatformRemoteAppleTV::SDKDirectoryInfo *
413 PlatformRemoteAppleTV::GetSDKDirectoryForCurrentOSVersion ()
414 {
415     uint32_t i;
416     if (UpdateSDKDirectoryInfosIfNeeded())
417     {
418         const uint32_t num_sdk_infos = m_sdk_directory_infos.size();
419 
420         // Check to see if the user specified a build string. If they did, then
421         // be sure to match it.
422         std::vector<bool> check_sdk_info(num_sdk_infos, true);
423         ConstString build(m_sdk_build);
424         if (build)
425         {
426             for (i=0; i<num_sdk_infos; ++i)
427                 check_sdk_info[i] = m_sdk_directory_infos[i].build == build;
428         }
429 
430         // If we are connected we can find the version of the OS the platform
431         // us running on and select the right SDK
432         uint32_t major, minor, update;
433         if (GetOSVersion(major, minor, update))
434         {
435             if (UpdateSDKDirectoryInfosIfNeeded())
436             {
437                 // First try for an exact match of major, minor and update
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                             m_sdk_directory_infos[i].version_update == update)
445                         {
446                             return &m_sdk_directory_infos[i];
447                         }
448                     }
449                 }
450                 // First try for an exact match of major and minor
451                 for (i=0; i<num_sdk_infos; ++i)
452                 {
453                     if (check_sdk_info[i])
454                     {
455                         if (m_sdk_directory_infos[i].version_major == major &&
456                             m_sdk_directory_infos[i].version_minor == minor)
457                         {
458                             return &m_sdk_directory_infos[i];
459                         }
460                     }
461                 }
462                 // Lastly try to match of major version only..
463                 for (i=0; i<num_sdk_infos; ++i)
464                 {
465                     if (check_sdk_info[i])
466                     {
467                         if (m_sdk_directory_infos[i].version_major == major)
468                         {
469                             return &m_sdk_directory_infos[i];
470                         }
471                     }
472                 }
473             }
474         }
475         else if (build)
476         {
477             // No version, just a build number, search for the first one that matches
478             for (i=0; i<num_sdk_infos; ++i)
479                 if (check_sdk_info[i])
480                     return &m_sdk_directory_infos[i];
481         }
482     }
483     return nullptr;
484 }
485 
486 const PlatformRemoteAppleTV::SDKDirectoryInfo *
487 PlatformRemoteAppleTV::GetSDKDirectoryForLatestOSVersion ()
488 {
489     const PlatformRemoteAppleTV::SDKDirectoryInfo *result = nullptr;
490     if (UpdateSDKDirectoryInfosIfNeeded())
491     {
492         const uint32_t num_sdk_infos = m_sdk_directory_infos.size();
493         // First try for an exact match of major, minor and update
494         for (uint32_t i=0; i<num_sdk_infos; ++i)
495         {
496             const SDKDirectoryInfo &sdk_dir_info = m_sdk_directory_infos[i];
497             if (sdk_dir_info.version_major != UINT32_MAX)
498             {
499                 if (result == nullptr || sdk_dir_info.version_major > result->version_major)
500                 {
501                     result = &sdk_dir_info;
502                 }
503                 else if (sdk_dir_info.version_major == result->version_major)
504                 {
505                     if (sdk_dir_info.version_minor > result->version_minor)
506                     {
507                         result = &sdk_dir_info;
508                     }
509                     else if (sdk_dir_info.version_minor == result->version_minor)
510                     {
511                         if (sdk_dir_info.version_update > result->version_update)
512                         {
513                             result = &sdk_dir_info;
514                         }
515                     }
516                 }
517             }
518         }
519     }
520     return result;
521 }
522 
523 const char *
524 PlatformRemoteAppleTV::GetDeviceSupportDirectory()
525 {
526     if (m_device_support_directory.empty())
527     {
528         const char *device_support_dir = GetDeveloperDirectory();
529         if (device_support_dir)
530         {
531             m_device_support_directory.assign (device_support_dir);
532             m_device_support_directory.append ("/Platforms/AppleTVOS.platform/DeviceSupport");
533         }
534         else
535         {
536             // Assign a single NULL character so we know we tried to find the device
537             // support directory and we don't keep trying to find it over and over.
538             m_device_support_directory.assign (1, '\0');
539         }
540     }
541     // We should have put a single NULL character into m_device_support_directory
542     // or it should have a valid path if the code gets here
543     assert (m_device_support_directory.empty() == false);
544     if (m_device_support_directory[0])
545         return m_device_support_directory.c_str();
546     return nullptr;
547 }
548 
549 const char *
550 PlatformRemoteAppleTV::GetDeviceSupportDirectoryForOSVersion()
551 {
552     if (m_sdk_sysroot)
553         return m_sdk_sysroot.GetCString();
554 
555     if (m_device_support_directory_for_os_version.empty())
556     {
557         const PlatformRemoteAppleTV::SDKDirectoryInfo *sdk_dir_info = GetSDKDirectoryForCurrentOSVersion ();
558         if (sdk_dir_info == nullptr)
559             sdk_dir_info = GetSDKDirectoryForLatestOSVersion ();
560         if (sdk_dir_info)
561         {
562             char path[PATH_MAX];
563             if (sdk_dir_info->directory.GetPath(path, sizeof(path)))
564             {
565                 m_device_support_directory_for_os_version = path;
566                 return m_device_support_directory_for_os_version.c_str();
567             }
568         }
569         else
570         {
571             // Assign a single NULL character so we know we tried to find the device
572             // support directory and we don't keep trying to find it over and over.
573             m_device_support_directory_for_os_version.assign (1, '\0');
574         }
575     }
576     // We should have put a single NULL character into m_device_support_directory_for_os_version
577     // or it should have a valid path if the code gets here
578     assert (m_device_support_directory_for_os_version.empty() == false);
579     if (m_device_support_directory_for_os_version[0])
580         return m_device_support_directory_for_os_version.c_str();
581     return nullptr;
582 }
583 
584 uint32_t
585 PlatformRemoteAppleTV::FindFileInAllSDKs (const char *platform_file_path,
586                                       FileSpecList &file_list)
587 {
588     Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST | LIBLLDB_LOG_VERBOSE);
589     if (platform_file_path && platform_file_path[0] && UpdateSDKDirectoryInfosIfNeeded())
590     {
591         const uint32_t num_sdk_infos = m_sdk_directory_infos.size();
592         lldb_private::FileSpec local_file;
593         // First try for an exact match of major, minor and update
594         for (uint32_t sdk_idx=0; sdk_idx<num_sdk_infos; ++sdk_idx)
595         {
596             if (log)
597             {
598                 log->Printf ("Searching for %s in sdk path %s", platform_file_path, m_sdk_directory_infos[sdk_idx].directory.GetPath().c_str());
599             }
600             if (GetFileInSDK (platform_file_path,
601                               sdk_idx,
602                               local_file))
603             {
604                 file_list.Append(local_file);
605             }
606         }
607     }
608     return file_list.GetSize();
609 }
610 
611 bool
612 PlatformRemoteAppleTV::GetFileInSDK (const char *platform_file_path,
613                                  uint32_t sdk_idx,
614                                  lldb_private::FileSpec &local_file)
615 {
616     if (sdk_idx < m_sdk_directory_infos.size())
617     {
618         char sdkroot_path[PATH_MAX];
619         const SDKDirectoryInfo &sdk_dir_info = m_sdk_directory_infos[sdk_idx];
620         if (sdk_dir_info.directory.GetPath(sdkroot_path, sizeof(sdkroot_path)))
621         {
622             const bool symbols_dirs_only = true;
623 
624             return GetFileInSDKRoot (platform_file_path,
625                                      sdkroot_path,
626                                      symbols_dirs_only,
627                                      local_file);
628         }
629     }
630     return false;
631 }
632 
633 bool
634 PlatformRemoteAppleTV::GetFileInSDKRoot (const char *platform_file_path,
635                                      const char *sdkroot_path,
636                                      bool symbols_dirs_only,
637                                      lldb_private::FileSpec &local_file)
638 {
639     Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST);
640     if (sdkroot_path && sdkroot_path[0] && platform_file_path && platform_file_path[0])
641     {
642         char resolved_path[PATH_MAX];
643 
644         if (!symbols_dirs_only)
645         {
646             ::snprintf (resolved_path,
647                         sizeof(resolved_path),
648                         "%s%s",
649                         sdkroot_path,
650                         platform_file_path);
651 
652             local_file.SetFile(resolved_path, true);
653             if (local_file.Exists())
654             {
655                 if (log)
656                 {
657                     log->Printf ("Found a copy of %s in the SDK dir %s", platform_file_path, sdkroot_path);
658                 }
659                 return true;
660             }
661         }
662 
663         ::snprintf (resolved_path,
664                     sizeof(resolved_path),
665                     "%s/Symbols.Internal%s",
666                     sdkroot_path,
667                     platform_file_path);
668 
669         local_file.SetFile(resolved_path, true);
670         if (local_file.Exists())
671         {
672             if (log)
673             {
674                 log->Printf ("Found a copy of %s in the SDK dir %s/Symbols.Internal", platform_file_path, sdkroot_path);
675             }
676             return true;
677         }
678         ::snprintf (resolved_path,
679                     sizeof(resolved_path),
680                     "%s/Symbols%s",
681                     sdkroot_path,
682                     platform_file_path);
683 
684         local_file.SetFile(resolved_path, true);
685         if (local_file.Exists())
686         {
687             if (log)
688             {
689                 log->Printf ("Found a copy of %s in the SDK dir %s/Symbols", platform_file_path, sdkroot_path);
690             }
691             return true;
692         }
693     }
694     return false;
695 }
696 
697 Error
698 PlatformRemoteAppleTV::GetSymbolFile (const FileSpec &platform_file,
699                                   const UUID *uuid_ptr,
700                                   FileSpec &local_file)
701 {
702     Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST);
703     Error error;
704     char platform_file_path[PATH_MAX];
705     if (platform_file.GetPath(platform_file_path, sizeof(platform_file_path)))
706     {
707         char resolved_path[PATH_MAX];
708 
709         const char * os_version_dir = GetDeviceSupportDirectoryForOSVersion();
710         if (os_version_dir)
711         {
712             ::snprintf (resolved_path,
713                         sizeof(resolved_path),
714                         "%s/%s",
715                         os_version_dir,
716                         platform_file_path);
717 
718             local_file.SetFile(resolved_path, true);
719             if (local_file.Exists())
720             {
721                 if (log)
722                 {
723                     log->Printf ("Found a copy of %s in the DeviceSupport dir %s", platform_file_path, os_version_dir);
724                 }
725                 return error;
726             }
727 
728             ::snprintf (resolved_path,
729                         sizeof(resolved_path),
730                         "%s/Symbols.Internal/%s",
731                         os_version_dir,
732                         platform_file_path);
733 
734             local_file.SetFile(resolved_path, true);
735             if (local_file.Exists())
736             {
737                 if (log)
738                 {
739                     log->Printf ("Found a copy of %s in the DeviceSupport dir %s/Symbols.Internal", platform_file_path, os_version_dir);
740                 }
741                 return error;
742             }
743             ::snprintf (resolved_path,
744                         sizeof(resolved_path),
745                         "%s/Symbols/%s",
746                         os_version_dir,
747                         platform_file_path);
748 
749             local_file.SetFile(resolved_path, true);
750             if (local_file.Exists())
751             {
752                 if (log)
753                 {
754                     log->Printf ("Found a copy of %s in the DeviceSupport dir %s/Symbols", platform_file_path, os_version_dir);
755                 }
756                 return error;
757             }
758         }
759         local_file = platform_file;
760         if (local_file.Exists())
761             return error;
762 
763         error.SetErrorStringWithFormat ("unable to locate a platform file for '%s' in platform '%s'",
764                                         platform_file_path,
765                                         GetPluginName().GetCString());
766     }
767     else
768     {
769         error.SetErrorString ("invalid platform file argument");
770     }
771     return error;
772 }
773 
774 Error
775 PlatformRemoteAppleTV::GetSharedModule (const ModuleSpec &module_spec,
776                                         lldb_private::Process* process,
777                                         ModuleSP &module_sp,
778                                         const FileSpecList *module_search_paths_ptr,
779                                         ModuleSP *old_module_sp_ptr,
780                                         bool *did_create_ptr)
781 {
782     // For Apple TV, the SDK files are all cached locally on the host
783     // system. So first we ask for the file in the cached SDK,
784     // then we attempt to get a shared module for the right architecture
785     // with the right UUID.
786     const FileSpec &platform_file = module_spec.GetFileSpec();
787     Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST | LIBLLDB_LOG_VERBOSE);
788 
789     Error error;
790     char platform_file_path[PATH_MAX];
791 
792     if (platform_file.GetPath(platform_file_path, sizeof(platform_file_path)))
793     {
794         ModuleSpec platform_module_spec(module_spec);
795 
796         UpdateSDKDirectoryInfosIfNeeded();
797 
798         const uint32_t num_sdk_infos = m_sdk_directory_infos.size();
799 
800         // If we are connected we migth be able to correctly deduce the SDK directory
801         // using the OS build.
802         const uint32_t connected_sdk_idx = GetConnectedSDKIndex ();
803         if (connected_sdk_idx < num_sdk_infos)
804         {
805             if (log)
806             {
807                 log->Printf ("Searching for %s in sdk path %s", platform_file_path, m_sdk_directory_infos[connected_sdk_idx].directory.GetPath().c_str());
808             }
809             if (GetFileInSDK (platform_file_path, connected_sdk_idx, platform_module_spec.GetFileSpec()))
810             {
811                 module_sp.reset();
812                 error = ResolveExecutable(platform_module_spec,
813                                           module_sp,
814                                           nullptr);
815                 if (module_sp)
816                 {
817                     m_last_module_sdk_idx = connected_sdk_idx;
818                     error.Clear();
819                     return error;
820                 }
821             }
822         }
823 
824         // Try the last SDK index if it is set as most files from an SDK
825         // will tend to be valid in that same SDK.
826         if (m_last_module_sdk_idx < num_sdk_infos)
827         {
828             if (log)
829             {
830                 log->Printf ("Searching for %s in sdk path %s", platform_file_path, m_sdk_directory_infos[m_last_module_sdk_idx].directory.GetPath().c_str());
831             }
832             if (GetFileInSDK (platform_file_path, m_last_module_sdk_idx, platform_module_spec.GetFileSpec()))
833             {
834                 module_sp.reset();
835                 error = ResolveExecutable(platform_module_spec,
836                                           module_sp,
837                                           nullptr);
838                 if (module_sp)
839                 {
840                     error.Clear();
841                     return error;
842                 }
843             }
844         }
845 
846         // First try for an exact match of major, minor and update
847         for (uint32_t sdk_idx=0; sdk_idx<num_sdk_infos; ++sdk_idx)
848         {
849             if (m_last_module_sdk_idx == sdk_idx)
850             {
851                 // Skip the last module SDK index if we already searched
852                 // it above
853                 continue;
854             }
855             if (log)
856             {
857                 log->Printf ("Searching for %s in sdk path %s", platform_file_path, m_sdk_directory_infos[sdk_idx].directory.GetPath().c_str());
858             }
859             if (GetFileInSDK (platform_file_path, sdk_idx, platform_module_spec.GetFileSpec()))
860             {
861                 //printf ("sdk[%u]: '%s'\n", sdk_idx, local_file.GetPath().c_str());
862 
863                 error = ResolveExecutable(platform_module_spec, module_sp, nullptr);
864                 if (module_sp)
865                 {
866                     // Remember the index of the last SDK that we found a file
867                     // in in case the wrong SDK was selected.
868                     m_last_module_sdk_idx = sdk_idx;
869                     error.Clear();
870                     return error;
871                 }
872             }
873         }
874     }
875     // Not the module we are looking for... Nothing to see here...
876     module_sp.reset();
877 
878     // This may not be an SDK-related module.  Try whether we can bring in the thing to our local cache.
879     error = GetSharedModuleWithLocalCache(module_spec, module_sp, module_search_paths_ptr, old_module_sp_ptr, did_create_ptr);
880     if (error.Success())
881         return error;
882 
883     const bool always_create = false;
884     error = ModuleList::GetSharedModule (module_spec,
885                                          module_sp,
886                                          module_search_paths_ptr,
887                                          old_module_sp_ptr,
888                                          did_create_ptr,
889                                          always_create);
890 
891     if (module_sp)
892         module_sp->SetPlatformFileSpec(platform_file);
893 
894     return error;
895 }
896 
897 bool
898 PlatformRemoteAppleTV::GetSupportedArchitectureAtIndex (uint32_t idx, ArchSpec &arch)
899 {
900     ArchSpec system_arch (GetSystemArchitecture());
901 
902     const ArchSpec::Core system_core = system_arch.GetCore();
903     switch (system_core)
904     {
905     default:
906         switch (idx)
907         {
908             case  0: arch.SetTriple ("arm64-apple-tvos");    return true;
909             case  1: arch.SetTriple ("armv7s-apple-tvos");   return true;
910             case  2: arch.SetTriple ("armv7-apple-tvos");    return true;
911             case  3: arch.SetTriple ("thumbv7s-apple-tvos");   return true;
912             case  4: arch.SetTriple ("thumbv7-apple-tvos");    return true;
913             default: break;
914         }
915         break;
916 
917     case ArchSpec::eCore_arm_arm64:
918         switch (idx)
919         {
920             case  0: arch.SetTriple ("arm64-apple-tvos");    return true;
921             case  1: arch.SetTriple ("armv7s-apple-tvos");   return true;
922             case  2: arch.SetTriple ("armv7-apple-tvos");    return true;
923             case  3: arch.SetTriple ("thumbv7s-apple-tvos");   return true;
924             case  4: arch.SetTriple ("thumbv7-apple-tvos");    return true;
925         default: break;
926         }
927         break;
928 
929     case ArchSpec::eCore_arm_armv7s:
930         switch (idx)
931         {
932             case  0: arch.SetTriple ("armv7s-apple-tvos");   return true;
933             case  1: arch.SetTriple ("armv7-apple-tvos");    return true;
934             case  2: arch.SetTriple ("thumbv7s-apple-tvos");   return true;
935             case  3: arch.SetTriple ("thumbv7-apple-tvos");    return true;
936             default: break;
937         }
938         break;
939 
940     case ArchSpec::eCore_arm_armv7:
941         switch (idx)
942         {
943             case  0: arch.SetTriple ("armv7-apple-tvos");    return true;
944             case  1: arch.SetTriple ("thumbv7-apple-tvos");    return true;
945             default: break;
946         }
947         break;
948     }
949     arch.Clear();
950     return false;
951 }
952 
953 uint32_t
954 PlatformRemoteAppleTV::GetConnectedSDKIndex ()
955 {
956     if (IsConnected())
957     {
958         if (m_connected_module_sdk_idx == UINT32_MAX)
959         {
960             std::string build;
961             if (GetRemoteOSBuildString(build))
962             {
963                 const uint32_t num_sdk_infos = m_sdk_directory_infos.size();
964                 for (uint32_t i=0; i<num_sdk_infos; ++i)
965                 {
966                     const SDKDirectoryInfo &sdk_dir_info = m_sdk_directory_infos[i];
967                     if (strstr(sdk_dir_info.directory.GetFilename().AsCString(""), build.c_str()))
968                     {
969                         m_connected_module_sdk_idx = i;
970                     }
971                 }
972             }
973         }
974     }
975     else
976     {
977         m_connected_module_sdk_idx = UINT32_MAX;
978     }
979     return m_connected_module_sdk_idx;
980 }
981