1 //===-- PlatformRemoteAppleWatch.cpp --------------------------------------===//
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 "PlatformRemoteAppleWatch.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 // Static Variables
32 static uint32_t g_initialize_count = 0;
33 
34 // Static Functions
35 void PlatformRemoteAppleWatch::Initialize() {
36   PlatformDarwin::Initialize();
37 
38   if (g_initialize_count++ == 0) {
39     PluginManager::RegisterPlugin(
40         PlatformRemoteAppleWatch::GetPluginNameStatic(),
41         PlatformRemoteAppleWatch::GetDescriptionStatic(),
42         PlatformRemoteAppleWatch::CreateInstance);
43   }
44 }
45 
46 void PlatformRemoteAppleWatch::Terminate() {
47   if (g_initialize_count > 0) {
48     if (--g_initialize_count == 0) {
49       PluginManager::UnregisterPlugin(PlatformRemoteAppleWatch::CreateInstance);
50     }
51   }
52 
53   PlatformDarwin::Terminate();
54 }
55 
56 PlatformSP PlatformRemoteAppleWatch::CreateInstance(bool force,
57                                                     const ArchSpec *arch) {
58   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
59   if (log) {
60     const char *arch_name;
61     if (arch && arch->GetArchitectureName())
62       arch_name = arch->GetArchitectureName();
63     else
64       arch_name = "<null>";
65 
66     const char *triple_cstr =
67         arch ? arch->GetTriple().getTriple().c_str() : "<null>";
68 
69     LLDB_LOGF(log, "PlatformRemoteAppleWatch::%s(force=%s, arch={%s,%s})",
70               __FUNCTION__, force ? "true" : "false", arch_name, triple_cstr);
71   }
72 
73   bool create = force;
74   if (!create && arch && arch->IsValid()) {
75     switch (arch->GetMachine()) {
76     case llvm::Triple::arm:
77     case llvm::Triple::aarch64:
78     case llvm::Triple::aarch64_32:
79     case llvm::Triple::thumb: {
80       const llvm::Triple &triple = arch->GetTriple();
81       llvm::Triple::VendorType vendor = triple.getVendor();
82       switch (vendor) {
83       case llvm::Triple::Apple:
84         create = true;
85         break;
86 
87 #if defined(__APPLE__)
88       // Only accept "unknown" for the vendor if the host is Apple and
89       // "unknown" wasn't specified (it was just returned because it was NOT
90       // specified)
91       case llvm::Triple::UnknownVendor:
92         create = !arch->TripleVendorWasSpecified();
93         break;
94 
95 #endif
96       default:
97         break;
98       }
99       if (create) {
100         switch (triple.getOS()) {
101         case llvm::Triple::WatchOS: // This is the right triple value for Apple
102                                     // Watch debugging
103           break;
104 
105         default:
106           create = false;
107           break;
108         }
109       }
110     } break;
111     default:
112       break;
113     }
114   }
115 
116 #if defined(__APPLE__) &&                                                      \
117     (defined(__arm__) || defined(__arm64__) || defined(__aarch64__))
118   // If lldb is running on a watch, this isn't a RemoteWatch environment; it's
119   // a local system environment.
120   if (force == false) {
121     create = false;
122   }
123 #endif
124 
125   if (create) {
126     LLDB_LOGF(log, "PlatformRemoteAppleWatch::%s() creating platform",
127               __FUNCTION__);
128 
129     return lldb::PlatformSP(new PlatformRemoteAppleWatch());
130   }
131 
132   LLDB_LOGF(log, "PlatformRemoteAppleWatch::%s() aborting creation of platform",
133             __FUNCTION__);
134 
135   return lldb::PlatformSP();
136 }
137 
138 llvm::StringRef PlatformRemoteAppleWatch::GetDescriptionStatic() {
139   return "Remote Apple Watch platform plug-in.";
140 }
141 
142 /// Default Constructor
143 PlatformRemoteAppleWatch::PlatformRemoteAppleWatch()
144     : PlatformRemoteDarwinDevice() {}
145 
146 bool PlatformRemoteAppleWatch::GetSupportedArchitectureAtIndex(uint32_t idx,
147                                                                ArchSpec &arch) {
148   ArchSpec system_arch(GetSystemArchitecture());
149 
150   const ArchSpec::Core system_core = system_arch.GetCore();
151   switch (system_core) {
152   default:
153     switch (idx) {
154     case 0:
155       arch.SetTriple("arm64-apple-watchos");
156       return true;
157     case 1:
158       arch.SetTriple("armv7k-apple-watchos");
159       return true;
160     case 2:
161       arch.SetTriple("armv7s-apple-watchos");
162       return true;
163     case 3:
164       arch.SetTriple("armv7-apple-watchos");
165       return true;
166     case 4:
167       arch.SetTriple("thumbv7k-apple-watchos");
168       return true;
169     case 5:
170       arch.SetTriple("thumbv7-apple-watchos");
171       return true;
172     case 6:
173       arch.SetTriple("thumbv7s-apple-watchos");
174       return true;
175     case 7:
176       arch.SetTriple("arm64_32-apple-watchos");
177       return true;
178     default:
179       break;
180     }
181     break;
182 
183   case ArchSpec::eCore_arm_arm64:
184     switch (idx) {
185     case 0:
186       arch.SetTriple("arm64-apple-watchos");
187       return true;
188     case 1:
189       arch.SetTriple("armv7k-apple-watchos");
190       return true;
191     case 2:
192       arch.SetTriple("armv7s-apple-watchos");
193       return true;
194     case 3:
195       arch.SetTriple("armv7-apple-watchos");
196       return true;
197     case 4:
198       arch.SetTriple("thumbv7k-apple-watchos");
199       return true;
200     case 5:
201       arch.SetTriple("thumbv7-apple-watchos");
202       return true;
203     case 6:
204       arch.SetTriple("thumbv7s-apple-watchos");
205       return true;
206     case 7:
207       arch.SetTriple("arm64_32-apple-watchos");
208       return true;
209     default:
210       break;
211     }
212     break;
213 
214   case ArchSpec::eCore_arm_armv7k:
215     switch (idx) {
216     case 0:
217       arch.SetTriple("armv7k-apple-watchos");
218       return true;
219     case 1:
220       arch.SetTriple("armv7s-apple-watchos");
221       return true;
222     case 2:
223       arch.SetTriple("armv7-apple-watchos");
224       return true;
225     case 3:
226       arch.SetTriple("thumbv7k-apple-watchos");
227       return true;
228     case 4:
229       arch.SetTriple("thumbv7-apple-watchos");
230       return true;
231     case 5:
232       arch.SetTriple("thumbv7s-apple-watchos");
233       return true;
234     case 6:
235       arch.SetTriple("arm64_32-apple-watchos");
236       return true;
237     default:
238       break;
239     }
240     break;
241 
242   case ArchSpec::eCore_arm_armv7s:
243     switch (idx) {
244     case 0:
245       arch.SetTriple("armv7s-apple-watchos");
246       return true;
247     case 1:
248       arch.SetTriple("armv7k-apple-watchos");
249       return true;
250     case 2:
251       arch.SetTriple("armv7-apple-watchos");
252       return true;
253     case 3:
254       arch.SetTriple("thumbv7k-apple-watchos");
255       return true;
256     case 4:
257       arch.SetTriple("thumbv7-apple-watchos");
258       return true;
259     case 5:
260       arch.SetTriple("thumbv7s-apple-watchos");
261       return true;
262     case 6:
263       arch.SetTriple("arm64_32-apple-watchos");
264       return true;
265     default:
266       break;
267     }
268     break;
269 
270   case ArchSpec::eCore_arm_armv7:
271     switch (idx) {
272     case 0:
273       arch.SetTriple("armv7-apple-watchos");
274       return true;
275     case 1:
276       arch.SetTriple("armv7k-apple-watchos");
277       return true;
278     case 2:
279       arch.SetTriple("thumbv7k-apple-watchos");
280       return true;
281     case 3:
282       arch.SetTriple("thumbv7-apple-watchos");
283       return true;
284     case 4:
285       arch.SetTriple("arm64_32-apple-watchos");
286       return true;
287     default:
288       break;
289     }
290     break;
291   }
292   arch.Clear();
293   return false;
294 }
295 
296 llvm::StringRef PlatformRemoteAppleWatch::GetDeviceSupportDirectoryName() {
297   return "watchOS DeviceSupport";
298 }
299 
300 llvm::StringRef PlatformRemoteAppleWatch::GetPlatformName() {
301   return "WatchOS.platform";
302 }
303