1 //===-- PlatformRemoteAppleWatch.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 "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     log->Printf("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::thumb: {
79       const llvm::Triple &triple = arch->GetTriple();
80       llvm::Triple::VendorType vendor = triple.getVendor();
81       switch (vendor) {
82       case llvm::Triple::Apple:
83         create = true;
84         break;
85 
86 #if defined(__APPLE__)
87       // Only accept "unknown" for the vendor if the host is Apple and
88       // "unknown" wasn't specified (it was just returned because it was NOT
89       // specified)
90       case llvm::Triple::UnknownVendor:
91         create = !arch->TripleVendorWasSpecified();
92         break;
93 
94 #endif
95       default:
96         break;
97       }
98       if (create) {
99         switch (triple.getOS()) {
100         case llvm::Triple::WatchOS: // This is the right triple value for Apple
101                                     // Watch debugging
102           break;
103 
104         default:
105           create = false;
106           break;
107         }
108       }
109     } break;
110     default:
111       break;
112     }
113   }
114 
115 #if defined(__APPLE__) &&                                                      \
116     (defined(__arm__) || defined(__arm64__) || defined(__aarch64__))
117   // If lldb is running on a watch, this isn't a RemoteWatch environment; it's
118   // a local system environment.
119   if (force == false) {
120     create = false;
121   }
122 #endif
123 
124   if (create) {
125     if (log)
126       log->Printf("PlatformRemoteAppleWatch::%s() creating platform",
127                   __FUNCTION__);
128 
129     return lldb::PlatformSP(new PlatformRemoteAppleWatch());
130   }
131 
132   if (log)
133     log->Printf("PlatformRemoteAppleWatch::%s() aborting creation of platform",
134                 __FUNCTION__);
135 
136   return lldb::PlatformSP();
137 }
138 
139 lldb_private::ConstString PlatformRemoteAppleWatch::GetPluginNameStatic() {
140   static ConstString g_name("remote-watchos");
141   return g_name;
142 }
143 
144 const char *PlatformRemoteAppleWatch::GetDescriptionStatic() {
145   return "Remote Apple Watch platform plug-in.";
146 }
147 
148 /// Default Constructor
149 PlatformRemoteAppleWatch::PlatformRemoteAppleWatch()
150     : PlatformRemoteDarwinDevice() {}
151 
152 bool PlatformRemoteAppleWatch::GetSupportedArchitectureAtIndex(uint32_t idx,
153                                                                ArchSpec &arch) {
154   ArchSpec system_arch(GetSystemArchitecture());
155 
156   const ArchSpec::Core system_core = system_arch.GetCore();
157   switch (system_core) {
158   default:
159     switch (idx) {
160     case 0:
161       arch.SetTriple("arm64-apple-watchos");
162       return true;
163     case 1:
164       arch.SetTriple("armv7k-apple-watchos");
165       return true;
166     case 2:
167       arch.SetTriple("armv7s-apple-watchos");
168       return true;
169     case 3:
170       arch.SetTriple("armv7-apple-watchos");
171       return true;
172     case 4:
173       arch.SetTriple("thumbv7k-apple-watchos");
174       return true;
175     case 5:
176       arch.SetTriple("thumbv7-apple-watchos");
177       return true;
178     case 6:
179       arch.SetTriple("thumbv7s-apple-watchos");
180       return true;
181     default:
182       break;
183     }
184     break;
185 
186   case ArchSpec::eCore_arm_arm64:
187     switch (idx) {
188     case 0:
189       arch.SetTriple("arm64-apple-watchos");
190       return true;
191     case 1:
192       arch.SetTriple("armv7k-apple-watchos");
193       return true;
194     case 2:
195       arch.SetTriple("armv7s-apple-watchos");
196       return true;
197     case 3:
198       arch.SetTriple("armv7-apple-watchos");
199       return true;
200     case 4:
201       arch.SetTriple("thumbv7k-apple-watchos");
202       return true;
203     case 5:
204       arch.SetTriple("thumbv7-apple-watchos");
205       return true;
206     case 6:
207       arch.SetTriple("thumbv7s-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     default:
235       break;
236     }
237     break;
238 
239   case ArchSpec::eCore_arm_armv7s:
240     switch (idx) {
241     case 0:
242       arch.SetTriple("armv7s-apple-watchos");
243       return true;
244     case 1:
245       arch.SetTriple("armv7k-apple-watchos");
246       return true;
247     case 2:
248       arch.SetTriple("armv7-apple-watchos");
249       return true;
250     case 3:
251       arch.SetTriple("thumbv7k-apple-watchos");
252       return true;
253     case 4:
254       arch.SetTriple("thumbv7-apple-watchos");
255       return true;
256     case 5:
257       arch.SetTriple("thumbv7s-apple-watchos");
258       return true;
259     default:
260       break;
261     }
262     break;
263 
264   case ArchSpec::eCore_arm_armv7:
265     switch (idx) {
266     case 0:
267       arch.SetTriple("armv7-apple-watchos");
268       return true;
269     case 1:
270       arch.SetTriple("armv7k-apple-watchos");
271       return true;
272     case 2:
273       arch.SetTriple("thumbv7k-apple-watchos");
274       return true;
275     case 3:
276       arch.SetTriple("thumbv7-apple-watchos");
277       return true;
278     default:
279       break;
280     }
281     break;
282   }
283   arch.Clear();
284   return false;
285 }
286 
287 void PlatformRemoteAppleWatch::GetDeviceSupportDirectoryNames (std::vector<std::string> &dirnames)
288 {
289     dirnames.clear();
290     dirnames.push_back("watchOS DeviceSupport");
291 }
292 
293 std::string PlatformRemoteAppleWatch::GetPlatformName ()
294 {
295     return "WatchOS.platform";
296 }
297