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     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::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     LLDB_LOGF(log, "PlatformRemoteAppleWatch::%s() creating platform",
126               __FUNCTION__);
127 
128     return lldb::PlatformSP(new PlatformRemoteAppleWatch());
129   }
130 
131   LLDB_LOGF(log, "PlatformRemoteAppleWatch::%s() aborting creation of platform",
132             __FUNCTION__);
133 
134   return lldb::PlatformSP();
135 }
136 
137 lldb_private::ConstString PlatformRemoteAppleWatch::GetPluginNameStatic() {
138   static ConstString g_name("remote-watchos");
139   return g_name;
140 }
141 
142 const char *PlatformRemoteAppleWatch::GetDescriptionStatic() {
143   return "Remote Apple Watch platform plug-in.";
144 }
145 
146 /// Default Constructor
147 PlatformRemoteAppleWatch::PlatformRemoteAppleWatch()
148     : PlatformRemoteDarwinDevice() {}
149 
150 bool PlatformRemoteAppleWatch::GetSupportedArchitectureAtIndex(uint32_t idx,
151                                                                ArchSpec &arch) {
152   ArchSpec system_arch(GetSystemArchitecture());
153 
154   const ArchSpec::Core system_core = system_arch.GetCore();
155   switch (system_core) {
156   default:
157     switch (idx) {
158     case 0:
159       arch.SetTriple("arm64-apple-watchos");
160       return true;
161     case 1:
162       arch.SetTriple("armv7k-apple-watchos");
163       return true;
164     case 2:
165       arch.SetTriple("armv7s-apple-watchos");
166       return true;
167     case 3:
168       arch.SetTriple("armv7-apple-watchos");
169       return true;
170     case 4:
171       arch.SetTriple("thumbv7k-apple-watchos");
172       return true;
173     case 5:
174       arch.SetTriple("thumbv7-apple-watchos");
175       return true;
176     case 6:
177       arch.SetTriple("thumbv7s-apple-watchos");
178       return true;
179     default:
180       break;
181     }
182     break;
183 
184   case ArchSpec::eCore_arm_arm64:
185     switch (idx) {
186     case 0:
187       arch.SetTriple("arm64-apple-watchos");
188       return true;
189     case 1:
190       arch.SetTriple("armv7k-apple-watchos");
191       return true;
192     case 2:
193       arch.SetTriple("armv7s-apple-watchos");
194       return true;
195     case 3:
196       arch.SetTriple("armv7-apple-watchos");
197       return true;
198     case 4:
199       arch.SetTriple("thumbv7k-apple-watchos");
200       return true;
201     case 5:
202       arch.SetTriple("thumbv7-apple-watchos");
203       return true;
204     case 6:
205       arch.SetTriple("thumbv7s-apple-watchos");
206       return true;
207     default:
208       break;
209     }
210     break;
211 
212   case ArchSpec::eCore_arm_armv7k:
213     switch (idx) {
214     case 0:
215       arch.SetTriple("armv7k-apple-watchos");
216       return true;
217     case 1:
218       arch.SetTriple("armv7s-apple-watchos");
219       return true;
220     case 2:
221       arch.SetTriple("armv7-apple-watchos");
222       return true;
223     case 3:
224       arch.SetTriple("thumbv7k-apple-watchos");
225       return true;
226     case 4:
227       arch.SetTriple("thumbv7-apple-watchos");
228       return true;
229     case 5:
230       arch.SetTriple("thumbv7s-apple-watchos");
231       return true;
232     default:
233       break;
234     }
235     break;
236 
237   case ArchSpec::eCore_arm_armv7s:
238     switch (idx) {
239     case 0:
240       arch.SetTriple("armv7s-apple-watchos");
241       return true;
242     case 1:
243       arch.SetTriple("armv7k-apple-watchos");
244       return true;
245     case 2:
246       arch.SetTriple("armv7-apple-watchos");
247       return true;
248     case 3:
249       arch.SetTriple("thumbv7k-apple-watchos");
250       return true;
251     case 4:
252       arch.SetTriple("thumbv7-apple-watchos");
253       return true;
254     case 5:
255       arch.SetTriple("thumbv7s-apple-watchos");
256       return true;
257     default:
258       break;
259     }
260     break;
261 
262   case ArchSpec::eCore_arm_armv7:
263     switch (idx) {
264     case 0:
265       arch.SetTriple("armv7-apple-watchos");
266       return true;
267     case 1:
268       arch.SetTriple("armv7k-apple-watchos");
269       return true;
270     case 2:
271       arch.SetTriple("thumbv7k-apple-watchos");
272       return true;
273     case 3:
274       arch.SetTriple("thumbv7-apple-watchos");
275       return true;
276     default:
277       break;
278     }
279     break;
280   }
281   arch.Clear();
282   return false;
283 }
284 
285 void PlatformRemoteAppleWatch::GetDeviceSupportDirectoryNames (std::vector<std::string> &dirnames)
286 {
287     dirnames.clear();
288     dirnames.push_back("watchOS DeviceSupport");
289 }
290 
291 std::string PlatformRemoteAppleWatch::GetPlatformName ()
292 {
293     return "WatchOS.platform";
294 }
295