180814287SRaphael Isemann //===-- PlatformRemoteAppleWatch.cpp --------------------------------------===//
25e88be9fSJason Molenda //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65e88be9fSJason Molenda //
75e88be9fSJason Molenda //===----------------------------------------------------------------------===//
85e88be9fSJason Molenda 
926f34fb6SEugene Zelenko #include <string>
1026f34fb6SEugene Zelenko #include <vector>
1126f34fb6SEugene Zelenko 
1226f34fb6SEugene Zelenko #include "PlatformRemoteAppleWatch.h"
1326f34fb6SEugene Zelenko 
145e88be9fSJason Molenda #include "lldb/Breakpoint/BreakpointLocation.h"
155e88be9fSJason Molenda #include "lldb/Core/Module.h"
165e88be9fSJason Molenda #include "lldb/Core/ModuleList.h"
175e88be9fSJason Molenda #include "lldb/Core/ModuleSpec.h"
185e88be9fSJason Molenda #include "lldb/Core/PluginManager.h"
195e88be9fSJason Molenda #include "lldb/Host/Host.h"
205e88be9fSJason Molenda #include "lldb/Target/Process.h"
215e88be9fSJason Molenda #include "lldb/Target/Target.h"
225f19b907SPavel Labath #include "lldb/Utility/ArchSpec.h"
235713a05bSZachary Turner #include "lldb/Utility/FileSpec.h"
24c34698a8SPavel Labath #include "lldb/Utility/LLDBLog.h"
256f9e6901SZachary Turner #include "lldb/Utility/Log.h"
2697206d57SZachary Turner #include "lldb/Utility/Status.h"
27bf9a7730SZachary Turner #include "lldb/Utility/StreamString.h"
285e88be9fSJason Molenda 
295e88be9fSJason Molenda using namespace lldb;
305e88be9fSJason Molenda using namespace lldb_private;
315e88be9fSJason Molenda 
325e88be9fSJason Molenda // Static Variables
335e88be9fSJason Molenda static uint32_t g_initialize_count = 0;
345e88be9fSJason Molenda 
355e88be9fSJason Molenda // Static Functions
Initialize()36b9c1b51eSKate Stone void PlatformRemoteAppleWatch::Initialize() {
375e88be9fSJason Molenda   PlatformDarwin::Initialize();
385e88be9fSJason Molenda 
39b9c1b51eSKate Stone   if (g_initialize_count++ == 0) {
40b9c1b51eSKate Stone     PluginManager::RegisterPlugin(
41b9c1b51eSKate Stone         PlatformRemoteAppleWatch::GetPluginNameStatic(),
425e88be9fSJason Molenda         PlatformRemoteAppleWatch::GetDescriptionStatic(),
435e88be9fSJason Molenda         PlatformRemoteAppleWatch::CreateInstance);
445e88be9fSJason Molenda   }
455e88be9fSJason Molenda }
465e88be9fSJason Molenda 
Terminate()47b9c1b51eSKate Stone void PlatformRemoteAppleWatch::Terminate() {
48b9c1b51eSKate Stone   if (g_initialize_count > 0) {
49b9c1b51eSKate Stone     if (--g_initialize_count == 0) {
505e88be9fSJason Molenda       PluginManager::UnregisterPlugin(PlatformRemoteAppleWatch::CreateInstance);
515e88be9fSJason Molenda     }
525e88be9fSJason Molenda   }
535e88be9fSJason Molenda 
545e88be9fSJason Molenda   PlatformDarwin::Terminate();
555e88be9fSJason Molenda }
565e88be9fSJason Molenda 
CreateInstance(bool force,const ArchSpec * arch)57b9c1b51eSKate Stone PlatformSP PlatformRemoteAppleWatch::CreateInstance(bool force,
58b9c1b51eSKate Stone                                                     const ArchSpec *arch) {
59a007a6d8SPavel Labath   Log *log = GetLog(LLDBLog::Platform);
60b9c1b51eSKate Stone   if (log) {
61f638ffddSJason Molenda     const char *arch_name;
62f638ffddSJason Molenda     if (arch && arch->GetArchitectureName())
63f638ffddSJason Molenda       arch_name = arch->GetArchitectureName();
64f638ffddSJason Molenda     else
65f638ffddSJason Molenda       arch_name = "<null>";
66f638ffddSJason Molenda 
67b9c1b51eSKate Stone     const char *triple_cstr =
68b9c1b51eSKate Stone         arch ? arch->GetTriple().getTriple().c_str() : "<null>";
69f638ffddSJason Molenda 
7063e5fb76SJonas Devlieghere     LLDB_LOGF(log, "PlatformRemoteAppleWatch::%s(force=%s, arch={%s,%s})",
71b9c1b51eSKate Stone               __FUNCTION__, force ? "true" : "false", arch_name, triple_cstr);
72f638ffddSJason Molenda   }
73f638ffddSJason Molenda 
745e88be9fSJason Molenda   bool create = force;
75b9c1b51eSKate Stone   if (!create && arch && arch->IsValid()) {
76b9c1b51eSKate Stone     switch (arch->GetMachine()) {
775e88be9fSJason Molenda     case llvm::Triple::arm:
785e88be9fSJason Molenda     case llvm::Triple::aarch64:
797dd7a360SJason Molenda     case llvm::Triple::aarch64_32:
80b9c1b51eSKate Stone     case llvm::Triple::thumb: {
815e88be9fSJason Molenda       const llvm::Triple &triple = arch->GetTriple();
825e88be9fSJason Molenda       llvm::Triple::VendorType vendor = triple.getVendor();
83b9c1b51eSKate Stone       switch (vendor) {
845e88be9fSJason Molenda       case llvm::Triple::Apple:
855e88be9fSJason Molenda         create = true;
865e88be9fSJason Molenda         break;
875e88be9fSJason Molenda 
885e88be9fSJason Molenda #if defined(__APPLE__)
89c791417aSBob Wilson       // Only accept "unknown" for the vendor if the host is Apple and
9005097246SAdrian Prantl       // "unknown" wasn't specified (it was just returned because it was NOT
9105097246SAdrian Prantl       // specified)
92d8024371SAdrian Prantl       case llvm::Triple::UnknownVendor:
935e88be9fSJason Molenda         create = !arch->TripleVendorWasSpecified();
945e88be9fSJason Molenda         break;
955e88be9fSJason Molenda 
965e88be9fSJason Molenda #endif
975e88be9fSJason Molenda       default:
985e88be9fSJason Molenda         break;
995e88be9fSJason Molenda       }
100b9c1b51eSKate Stone       if (create) {
101b9c1b51eSKate Stone         switch (triple.getOS()) {
102b9c1b51eSKate Stone         case llvm::Triple::WatchOS: // This is the right triple value for Apple
103b9c1b51eSKate Stone                                     // Watch debugging
1045e88be9fSJason Molenda           break;
1055e88be9fSJason Molenda 
1065e88be9fSJason Molenda         default:
1075e88be9fSJason Molenda           create = false;
1085e88be9fSJason Molenda           break;
1095e88be9fSJason Molenda         }
1105e88be9fSJason Molenda       }
111b9c1b51eSKate Stone     } break;
1125e88be9fSJason Molenda     default:
1135e88be9fSJason Molenda       break;
1145e88be9fSJason Molenda     }
1155e88be9fSJason Molenda   }
1165e88be9fSJason Molenda 
117b9c1b51eSKate Stone #if defined(__APPLE__) &&                                                      \
118b9c1b51eSKate Stone     (defined(__arm__) || defined(__arm64__) || defined(__aarch64__))
11905097246SAdrian Prantl   // If lldb is running on a watch, this isn't a RemoteWatch environment; it's
12005097246SAdrian Prantl   // a local system environment.
121b9c1b51eSKate Stone   if (force == false) {
1225e88be9fSJason Molenda     create = false;
1235e88be9fSJason Molenda   }
1245e88be9fSJason Molenda #endif
1255e88be9fSJason Molenda 
126b9c1b51eSKate Stone   if (create) {
12763e5fb76SJonas Devlieghere     LLDB_LOGF(log, "PlatformRemoteAppleWatch::%s() creating platform",
128b9c1b51eSKate Stone               __FUNCTION__);
129f638ffddSJason Molenda 
1305e88be9fSJason Molenda     return lldb::PlatformSP(new PlatformRemoteAppleWatch());
131f638ffddSJason Molenda   }
132f638ffddSJason Molenda 
13363e5fb76SJonas Devlieghere   LLDB_LOGF(log, "PlatformRemoteAppleWatch::%s() aborting creation of platform",
134b9c1b51eSKate Stone             __FUNCTION__);
135f638ffddSJason Molenda 
1365e88be9fSJason Molenda   return lldb::PlatformSP();
1375e88be9fSJason Molenda }
1385e88be9fSJason Molenda 
GetDescriptionStatic()139a458ef4fSPavel Labath llvm::StringRef PlatformRemoteAppleWatch::GetDescriptionStatic() {
1405e88be9fSJason Molenda   return "Remote Apple Watch platform plug-in.";
1415e88be9fSJason Molenda }
1425e88be9fSJason Molenda 
1431781d6f7SJason Molenda /// Default Constructor
PlatformRemoteAppleWatch()1441781d6f7SJason Molenda PlatformRemoteAppleWatch::PlatformRemoteAppleWatch()
1451781d6f7SJason Molenda     : PlatformRemoteDarwinDevice() {}
1465e88be9fSJason Molenda 
147*dde487e5SJonas Devlieghere std::vector<ArchSpec>
GetSupportedArchitectures(const ArchSpec & host_info)148*dde487e5SJonas Devlieghere PlatformRemoteAppleWatch::GetSupportedArchitectures(const ArchSpec &host_info) {
1495e88be9fSJason Molenda   ArchSpec system_arch(GetSystemArchitecture());
1505e88be9fSJason Molenda 
1515e88be9fSJason Molenda   const ArchSpec::Core system_core = system_arch.GetCore();
152b9c1b51eSKate Stone   switch (system_core) {
1535e88be9fSJason Molenda   default:
1545e88be9fSJason Molenda   case ArchSpec::eCore_arm_arm64:
155e7c48f3cSPavel Labath     return {
156e7c48f3cSPavel Labath         ArchSpec("arm64-apple-watchos"),    ArchSpec("armv7k-apple-watchos"),
157e7c48f3cSPavel Labath         ArchSpec("armv7s-apple-watchos"),   ArchSpec("armv7-apple-watchos"),
158e7c48f3cSPavel Labath         ArchSpec("thumbv7k-apple-watchos"), ArchSpec("thumbv7-apple-watchos"),
159e7c48f3cSPavel Labath         ArchSpec("thumbv7s-apple-watchos"), ArchSpec("arm64_32-apple-watchos")};
1605e88be9fSJason Molenda 
1615e88be9fSJason Molenda   case ArchSpec::eCore_arm_armv7k:
162e7c48f3cSPavel Labath     return {
163e7c48f3cSPavel Labath         ArchSpec("armv7k-apple-watchos"),  ArchSpec("armv7s-apple-watchos"),
164e7c48f3cSPavel Labath         ArchSpec("armv7-apple-watchos"),   ArchSpec("thumbv7k-apple-watchos"),
165e7c48f3cSPavel Labath         ArchSpec("thumbv7-apple-watchos"), ArchSpec("thumbv7s-apple-watchos"),
166e7c48f3cSPavel Labath         ArchSpec("arm64_32-apple-watchos")};
1675e88be9fSJason Molenda 
1685e88be9fSJason Molenda   case ArchSpec::eCore_arm_armv7s:
169e7c48f3cSPavel Labath     return {
170e7c48f3cSPavel Labath         ArchSpec("armv7s-apple-watchos"),  ArchSpec("armv7k-apple-watchos"),
171e7c48f3cSPavel Labath         ArchSpec("armv7-apple-watchos"),   ArchSpec("thumbv7k-apple-watchos"),
172e7c48f3cSPavel Labath         ArchSpec("thumbv7-apple-watchos"), ArchSpec("thumbv7s-apple-watchos"),
173e7c48f3cSPavel Labath         ArchSpec("arm64_32-apple-watchos")};
1745e88be9fSJason Molenda 
1755e88be9fSJason Molenda   case ArchSpec::eCore_arm_armv7:
176e7c48f3cSPavel Labath     return {ArchSpec("armv7-apple-watchos"), ArchSpec("armv7k-apple-watchos"),
177e7c48f3cSPavel Labath             ArchSpec("thumbv7k-apple-watchos"),
178e7c48f3cSPavel Labath             ArchSpec("thumbv7-apple-watchos"),
179e7c48f3cSPavel Labath             ArchSpec("arm64_32-apple-watchos")};
1805e88be9fSJason Molenda   }
1815e88be9fSJason Molenda }
1825e88be9fSJason Molenda 
GetDeviceSupportDirectoryName()183173bb3c2SJonas Devlieghere llvm::StringRef PlatformRemoteAppleWatch::GetDeviceSupportDirectoryName() {
184173bb3c2SJonas Devlieghere   return "watchOS DeviceSupport";
1855e88be9fSJason Molenda }
1861781d6f7SJason Molenda 
GetPlatformName()187173bb3c2SJonas Devlieghere llvm::StringRef PlatformRemoteAppleWatch::GetPlatformName() {
1881781d6f7SJason Molenda   return "WatchOS.platform";
1895e88be9fSJason Molenda }
190