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