1 //===-- PlatformRemoteiOS.cpp ---------------------------------------------===//
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 "PlatformRemoteiOS.h"
10 
11 #include "lldb/Breakpoint/BreakpointLocation.h"
12 #include "lldb/Core/Module.h"
13 #include "lldb/Core/ModuleList.h"
14 #include "lldb/Core/ModuleSpec.h"
15 #include "lldb/Core/PluginManager.h"
16 #include "lldb/Host/Host.h"
17 #include "lldb/Target/Process.h"
18 #include "lldb/Target/Target.h"
19 #include "lldb/Utility/ArchSpec.h"
20 #include "lldb/Utility/FileSpec.h"
21 #include "lldb/Utility/Log.h"
22 #include "lldb/Utility/Status.h"
23 #include "lldb/Utility/StreamString.h"
24 
25 using namespace lldb;
26 using namespace lldb_private;
27 
28 LLDB_PLUGIN_DEFINE(PlatformRemoteiOS)
29 
30 // Static Variables
31 static uint32_t g_initialize_count = 0;
32 
33 // Static Functions
Initialize()34 void PlatformRemoteiOS::Initialize() {
35   PlatformDarwin::Initialize();
36 
37   if (g_initialize_count++ == 0) {
38     PluginManager::RegisterPlugin(PlatformRemoteiOS::GetPluginNameStatic(),
39                                   PlatformRemoteiOS::GetDescriptionStatic(),
40                                   PlatformRemoteiOS::CreateInstance);
41   }
42 }
43 
Terminate()44 void PlatformRemoteiOS::Terminate() {
45   if (g_initialize_count > 0) {
46     if (--g_initialize_count == 0) {
47       PluginManager::UnregisterPlugin(PlatformRemoteiOS::CreateInstance);
48     }
49   }
50 
51   PlatformDarwin::Terminate();
52 }
53 
CreateInstance(bool force,const ArchSpec * arch)54 PlatformSP PlatformRemoteiOS::CreateInstance(bool force, const ArchSpec *arch) {
55   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
56   if (log) {
57     const char *arch_name;
58     if (arch && arch->GetArchitectureName())
59       arch_name = arch->GetArchitectureName();
60     else
61       arch_name = "<null>";
62 
63     const char *triple_cstr =
64         arch ? arch->GetTriple().getTriple().c_str() : "<null>";
65 
66     LLDB_LOGF(log, "PlatformRemoteiOS::%s(force=%s, arch={%s,%s})",
67               __FUNCTION__, force ? "true" : "false", arch_name, triple_cstr);
68   }
69 
70   bool create = force;
71   if (!create && arch && arch->IsValid()) {
72     switch (arch->GetMachine()) {
73     case llvm::Triple::arm:
74     case llvm::Triple::aarch64:
75     case llvm::Triple::thumb: {
76       const llvm::Triple &triple = arch->GetTriple();
77       llvm::Triple::VendorType vendor = triple.getVendor();
78       switch (vendor) {
79       case llvm::Triple::Apple:
80         create = true;
81         break;
82 
83 #if defined(__APPLE__)
84       // Only accept "unknown" for the vendor if the host is Apple and
85       // "unknown" wasn't specified (it was just returned because it was NOT
86       // specified)
87       case llvm::Triple::UnknownVendor:
88         create = !arch->TripleVendorWasSpecified();
89         break;
90 
91 #endif
92       default:
93         break;
94       }
95       if (create) {
96         switch (triple.getOS()) {
97         case llvm::Triple::Darwin: // Deprecated, but still support Darwin for
98                                    // historical reasons
99         case llvm::Triple::IOS:    // This is the right triple value for iOS
100                                    // debugging
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       LLDB_LOGF(log, "PlatformRemoteiOS::%s() creating platform", __FUNCTION__);
117 
118     return lldb::PlatformSP(new PlatformRemoteiOS());
119   }
120 
121   if (log)
122     LLDB_LOGF(log, "PlatformRemoteiOS::%s() aborting creation of platform",
123               __FUNCTION__);
124 
125   return lldb::PlatformSP();
126 }
127 
GetPluginNameStatic()128 lldb_private::ConstString PlatformRemoteiOS::GetPluginNameStatic() {
129   static ConstString g_name("remote-ios");
130   return g_name;
131 }
132 
GetDescriptionStatic()133 const char *PlatformRemoteiOS::GetDescriptionStatic() {
134   return "Remote iOS platform plug-in.";
135 }
136 
137 /// Default Constructor
PlatformRemoteiOS()138 PlatformRemoteiOS::PlatformRemoteiOS()
139     : PlatformRemoteDarwinDevice() {}
140 
GetSupportedArchitectureAtIndex(uint32_t idx,ArchSpec & arch)141 bool PlatformRemoteiOS::GetSupportedArchitectureAtIndex(uint32_t idx,
142                                                         ArchSpec &arch) {
143   return ARMGetSupportedArchitectureAtIndex(idx, arch);
144 }
145 
GetDeviceSupportDirectoryName()146 llvm::StringRef PlatformRemoteiOS::GetDeviceSupportDirectoryName() {
147   return "iOS DeviceSupport";
148 }
149 
GetPlatformName()150 llvm::StringRef PlatformRemoteiOS::GetPlatformName() {
151   return "iPhoneOS.platform";
152 }
153