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