1 //===-- PlatformRemoteAppleBridge.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 "PlatformRemoteAppleBridge.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/StreamString.h"
26 
27 using namespace lldb;
28 using namespace lldb_private;
29 
30 //------------------------------------------------------------------
31 /// Default Constructor
32 //------------------------------------------------------------------
33 PlatformRemoteAppleBridge::PlatformRemoteAppleBridge()
34     : PlatformRemoteDarwinDevice () {}
35 
36 //------------------------------------------------------------------
37 // Static Variables
38 //------------------------------------------------------------------
39 static uint32_t g_initialize_count = 0;
40 
41 //------------------------------------------------------------------
42 // Static Functions
43 //------------------------------------------------------------------
44 void PlatformRemoteAppleBridge::Initialize() {
45   PlatformDarwin::Initialize();
46 
47   if (g_initialize_count++ == 0) {
48     PluginManager::RegisterPlugin(PlatformRemoteAppleBridge::GetPluginNameStatic(),
49                                   PlatformRemoteAppleBridge::GetDescriptionStatic(),
50                                   PlatformRemoteAppleBridge::CreateInstance);
51   }
52 }
53 
54 void PlatformRemoteAppleBridge::Terminate() {
55   if (g_initialize_count > 0) {
56     if (--g_initialize_count == 0) {
57       PluginManager::UnregisterPlugin(PlatformRemoteAppleBridge::CreateInstance);
58     }
59   }
60 
61   PlatformDarwin::Terminate();
62 }
63 
64 PlatformSP PlatformRemoteAppleBridge::CreateInstance(bool force,
65                                                  const ArchSpec *arch) {
66   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
67   if (log) {
68     const char *arch_name;
69     if (arch && arch->GetArchitectureName())
70       arch_name = arch->GetArchitectureName();
71     else
72       arch_name = "<null>";
73 
74     const char *triple_cstr =
75         arch ? arch->GetTriple().getTriple().c_str() : "<null>";
76 
77     log->Printf("PlatformRemoteAppleBridge::%s(force=%s, arch={%s,%s})",
78                 __FUNCTION__, force ? "true" : "false", arch_name, triple_cstr);
79   }
80 
81   bool create = force;
82   if (!create && arch && arch->IsValid()) {
83     switch (arch->GetMachine()) {
84     case llvm::Triple::aarch64: {
85       const llvm::Triple &triple = arch->GetTriple();
86       llvm::Triple::VendorType vendor = triple.getVendor();
87       switch (vendor) {
88       case llvm::Triple::Apple:
89         create = true;
90         break;
91 
92 #if defined(__APPLE__)
93       // Only accept "unknown" for the vendor if the host is Apple and
94       // it "unknown" wasn't specified (it was just returned because it
95       // was NOT specified)
96       case llvm::Triple::UnknownVendor:
97         create = !arch->TripleVendorWasSpecified();
98         break;
99 
100 #endif
101       default:
102         break;
103       }
104       if (create) {
105         switch (triple.getOS()) {
106         // NEED_BRIDGEOS_TRIPLE case llvm::Triple::BridgeOS:
107           break;
108 
109         default:
110           create = false;
111           break;
112         }
113       }
114     } break;
115     default:
116       break;
117     }
118   }
119 
120   if (create) {
121     if (log)
122       log->Printf("PlatformRemoteAppleBridge::%s() creating platform",
123                   __FUNCTION__);
124 
125     return lldb::PlatformSP(new PlatformRemoteAppleBridge());
126   }
127 
128   if (log)
129     log->Printf("PlatformRemoteAppleBridge::%s() aborting creation of platform",
130                 __FUNCTION__);
131 
132   return lldb::PlatformSP();
133 }
134 
135 lldb_private::ConstString PlatformRemoteAppleBridge::GetPluginNameStatic() {
136   static ConstString g_name("remote-bridgeos");
137   return g_name;
138 }
139 
140 const char *PlatformRemoteAppleBridge::GetDescriptionStatic() {
141   return "Remote BridgeOS platform plug-in.";
142 }
143 
144 bool PlatformRemoteAppleBridge::GetSupportedArchitectureAtIndex(uint32_t idx,
145                                                             ArchSpec &arch) {
146   ArchSpec system_arch(GetSystemArchitecture());
147 
148   const ArchSpec::Core system_core = system_arch.GetCore();
149   switch (system_core) {
150   default:
151     switch (idx) {
152     case 0:
153       arch.SetTriple("arm64-apple-bridgeos");
154       return true;
155     default:
156       break;
157     }
158     break;
159 
160   case ArchSpec::eCore_arm_arm64:
161     switch (idx) {
162     case 0:
163       arch.SetTriple("arm64-apple-bridgeos");
164       return true;
165     default:
166       break;
167     }
168     break;
169   }
170   arch.Clear();
171   return false;
172 }
173 
174 
175 void PlatformRemoteAppleBridge::GetDeviceSupportDirectoryNames (std::vector<std::string> &dirnames)
176 {
177     dirnames.clear();
178     dirnames.push_back("BridgeOS DeviceSupport");
179 }
180 
181 std::string PlatformRemoteAppleBridge::GetPlatformName ()
182 {
183     return "BridgeOS.platform";
184 }
185 
186