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