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 std::vector<ArchSpec> PlatformRemoteAppleWatch::GetSupportedArchitectures() {
147   ArchSpec system_arch(GetSystemArchitecture());
148 
149   const ArchSpec::Core system_core = system_arch.GetCore();
150   switch (system_core) {
151   default:
152   case ArchSpec::eCore_arm_arm64:
153     return {
154         ArchSpec("arm64-apple-watchos"),    ArchSpec("armv7k-apple-watchos"),
155         ArchSpec("armv7s-apple-watchos"),   ArchSpec("armv7-apple-watchos"),
156         ArchSpec("thumbv7k-apple-watchos"), ArchSpec("thumbv7-apple-watchos"),
157         ArchSpec("thumbv7s-apple-watchos"), ArchSpec("arm64_32-apple-watchos")};
158 
159   case ArchSpec::eCore_arm_armv7k:
160     return {
161         ArchSpec("armv7k-apple-watchos"),  ArchSpec("armv7s-apple-watchos"),
162         ArchSpec("armv7-apple-watchos"),   ArchSpec("thumbv7k-apple-watchos"),
163         ArchSpec("thumbv7-apple-watchos"), ArchSpec("thumbv7s-apple-watchos"),
164         ArchSpec("arm64_32-apple-watchos")};
165 
166   case ArchSpec::eCore_arm_armv7s:
167     return {
168         ArchSpec("armv7s-apple-watchos"),  ArchSpec("armv7k-apple-watchos"),
169         ArchSpec("armv7-apple-watchos"),   ArchSpec("thumbv7k-apple-watchos"),
170         ArchSpec("thumbv7-apple-watchos"), ArchSpec("thumbv7s-apple-watchos"),
171         ArchSpec("arm64_32-apple-watchos")};
172 
173   case ArchSpec::eCore_arm_armv7:
174     return {ArchSpec("armv7-apple-watchos"), ArchSpec("armv7k-apple-watchos"),
175             ArchSpec("thumbv7k-apple-watchos"),
176             ArchSpec("thumbv7-apple-watchos"),
177             ArchSpec("arm64_32-apple-watchos")};
178   }
179 }
180 
181 llvm::StringRef PlatformRemoteAppleWatch::GetDeviceSupportDirectoryName() {
182   return "watchOS DeviceSupport";
183 }
184 
185 llvm::StringRef PlatformRemoteAppleWatch::GetPlatformName() {
186   return "WatchOS.platform";
187 }
188