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