1 //===-- PlatformRemoteAppleTV.cpp -------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include <string>
10 #include <vector>
11 
12 #include "PlatformRemoteAppleTV.h"
13 
14 #include "lldb/Breakpoint/BreakpointLocation.h"
15 #include "lldb/Core/Module.h"
16 #include "lldb/Core/ModuleList.h"
17 #include "lldb/Core/ModuleSpec.h"
18 #include "lldb/Core/PluginManager.h"
19 #include "lldb/Host/Host.h"
20 #include "lldb/Target/Process.h"
21 #include "lldb/Target/Target.h"
22 #include "lldb/Utility/ArchSpec.h"
23 #include "lldb/Utility/FileSpec.h"
24 #include "lldb/Utility/Log.h"
25 #include "lldb/Utility/Status.h"
26 #include "lldb/Utility/StreamString.h"
27 
28 using namespace lldb;
29 using namespace lldb_private;
30 
31 /// Default Constructor
32 PlatformRemoteAppleTV::PlatformRemoteAppleTV()
33     : PlatformRemoteDarwinDevice () {}
34 
35 // Static Variables
36 static uint32_t g_initialize_count = 0;
37 
38 // Static Functions
39 void PlatformRemoteAppleTV::Initialize() {
40   PlatformDarwin::Initialize();
41 
42   if (g_initialize_count++ == 0) {
43     PluginManager::RegisterPlugin(PlatformRemoteAppleTV::GetPluginNameStatic(),
44                                   PlatformRemoteAppleTV::GetDescriptionStatic(),
45                                   PlatformRemoteAppleTV::CreateInstance);
46   }
47 }
48 
49 void PlatformRemoteAppleTV::Terminate() {
50   if (g_initialize_count > 0) {
51     if (--g_initialize_count == 0) {
52       PluginManager::UnregisterPlugin(PlatformRemoteAppleTV::CreateInstance);
53     }
54   }
55 
56   PlatformDarwin::Terminate();
57 }
58 
59 PlatformSP PlatformRemoteAppleTV::CreateInstance(bool force,
60                                                  const ArchSpec *arch) {
61   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
62   if (log) {
63     const char *arch_name;
64     if (arch && arch->GetArchitectureName())
65       arch_name = arch->GetArchitectureName();
66     else
67       arch_name = "<null>";
68 
69     const char *triple_cstr =
70         arch ? arch->GetTriple().getTriple().c_str() : "<null>";
71 
72     log->Printf("PlatformRemoteAppleTV::%s(force=%s, arch={%s,%s})",
73                 __FUNCTION__, force ? "true" : "false", arch_name, triple_cstr);
74   }
75 
76   bool create = force;
77   if (!create && arch && arch->IsValid()) {
78     switch (arch->GetMachine()) {
79     case llvm::Triple::arm:
80     case llvm::Triple::aarch64:
81     case llvm::Triple::thumb: {
82       const llvm::Triple &triple = arch->GetTriple();
83       llvm::Triple::VendorType vendor = triple.getVendor();
84       switch (vendor) {
85       case llvm::Triple::Apple:
86         create = true;
87         break;
88 
89 #if defined(__APPLE__)
90       // Only accept "unknown" for the vendor if the host is Apple and
91       // "unknown" wasn't specified (it was just returned because it was NOT
92       // specified)
93       case llvm::Triple::UnknownVendor:
94         create = !arch->TripleVendorWasSpecified();
95         break;
96 
97 #endif
98       default:
99         break;
100       }
101       if (create) {
102         switch (triple.getOS()) {
103         case llvm::Triple::TvOS: // This is the right triple value for Apple TV
104                                  // debugging
105           break;
106 
107         default:
108           create = false;
109           break;
110         }
111       }
112     } break;
113     default:
114       break;
115     }
116   }
117 
118   if (create) {
119     if (log)
120       log->Printf("PlatformRemoteAppleTV::%s() creating platform",
121                   __FUNCTION__);
122 
123     return lldb::PlatformSP(new PlatformRemoteAppleTV());
124   }
125 
126   if (log)
127     log->Printf("PlatformRemoteAppleTV::%s() aborting creation of platform",
128                 __FUNCTION__);
129 
130   return lldb::PlatformSP();
131 }
132 
133 lldb_private::ConstString PlatformRemoteAppleTV::GetPluginNameStatic() {
134   static ConstString g_name("remote-tvos");
135   return g_name;
136 }
137 
138 const char *PlatformRemoteAppleTV::GetDescriptionStatic() {
139   return "Remote Apple TV platform plug-in.";
140 }
141 
142 bool PlatformRemoteAppleTV::GetSupportedArchitectureAtIndex(uint32_t idx,
143                                                             ArchSpec &arch) {
144   ArchSpec system_arch(GetSystemArchitecture());
145 
146   const ArchSpec::Core system_core = system_arch.GetCore();
147   switch (system_core) {
148   default:
149     switch (idx) {
150     case 0:
151       arch.SetTriple("arm64-apple-tvos");
152       return true;
153     case 1:
154       arch.SetTriple("armv7s-apple-tvos");
155       return true;
156     case 2:
157       arch.SetTriple("armv7-apple-tvos");
158       return true;
159     case 3:
160       arch.SetTriple("thumbv7s-apple-tvos");
161       return true;
162     case 4:
163       arch.SetTriple("thumbv7-apple-tvos");
164       return true;
165     default:
166       break;
167     }
168     break;
169 
170   case ArchSpec::eCore_arm_arm64:
171     switch (idx) {
172     case 0:
173       arch.SetTriple("arm64-apple-tvos");
174       return true;
175     case 1:
176       arch.SetTriple("armv7s-apple-tvos");
177       return true;
178     case 2:
179       arch.SetTriple("armv7-apple-tvos");
180       return true;
181     case 3:
182       arch.SetTriple("thumbv7s-apple-tvos");
183       return true;
184     case 4:
185       arch.SetTriple("thumbv7-apple-tvos");
186       return true;
187     default:
188       break;
189     }
190     break;
191 
192   case ArchSpec::eCore_arm_armv7s:
193     switch (idx) {
194     case 0:
195       arch.SetTriple("armv7s-apple-tvos");
196       return true;
197     case 1:
198       arch.SetTriple("armv7-apple-tvos");
199       return true;
200     case 2:
201       arch.SetTriple("thumbv7s-apple-tvos");
202       return true;
203     case 3:
204       arch.SetTriple("thumbv7-apple-tvos");
205       return true;
206     default:
207       break;
208     }
209     break;
210 
211   case ArchSpec::eCore_arm_armv7:
212     switch (idx) {
213     case 0:
214       arch.SetTriple("armv7-apple-tvos");
215       return true;
216     case 1:
217       arch.SetTriple("thumbv7-apple-tvos");
218       return true;
219     default:
220       break;
221     }
222     break;
223   }
224   arch.Clear();
225   return false;
226 }
227 
228 
229 void PlatformRemoteAppleTV::GetDeviceSupportDirectoryNames (std::vector<std::string> &dirnames)
230 {
231     dirnames.clear();
232     dirnames.push_back("tvOS DeviceSupport");
233 }
234 
235 std::string PlatformRemoteAppleTV::GetPlatformName ()
236 {
237     return "AppleTVOS.platform";
238 }
239 
240