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     log->Printf("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     if (log)
116       log->Printf("PlatformRemoteAppleBridge::%s() creating platform",
117                   __FUNCTION__);
118 
119     return lldb::PlatformSP(new PlatformRemoteAppleBridge());
120   }
121 
122   if (log)
123     log->Printf("PlatformRemoteAppleBridge::%s() aborting creation of platform",
124                 __FUNCTION__);
125 
126   return lldb::PlatformSP();
127 }
128 
129 lldb_private::ConstString PlatformRemoteAppleBridge::GetPluginNameStatic() {
130   static ConstString g_name("remote-bridgeos");
131   return g_name;
132 }
133 
134 const char *PlatformRemoteAppleBridge::GetDescriptionStatic() {
135   return "Remote BridgeOS platform plug-in.";
136 }
137 
138 bool PlatformRemoteAppleBridge::GetSupportedArchitectureAtIndex(uint32_t idx,
139                                                             ArchSpec &arch) {
140   ArchSpec system_arch(GetSystemArchitecture());
141 
142   const ArchSpec::Core system_core = system_arch.GetCore();
143   switch (system_core) {
144   default:
145     switch (idx) {
146     case 0:
147       arch.SetTriple("arm64-apple-bridgeos");
148       return true;
149     default:
150       break;
151     }
152     break;
153 
154   case ArchSpec::eCore_arm_arm64:
155     switch (idx) {
156     case 0:
157       arch.SetTriple("arm64-apple-bridgeos");
158       return true;
159     default:
160       break;
161     }
162     break;
163   }
164   arch.Clear();
165   return false;
166 }
167 
168 
169 void PlatformRemoteAppleBridge::GetDeviceSupportDirectoryNames (std::vector<std::string> &dirnames)
170 {
171     dirnames.clear();
172     dirnames.push_back("BridgeOS DeviceSupport");
173 }
174 
175 std::string PlatformRemoteAppleBridge::GetPlatformName ()
176 {
177     return "BridgeOS.platform";
178 }
179 
180