1 //===-- PlatformMacOSX.cpp ------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "PlatformMacOSX.h"
10 #include "PlatformRemoteMacOSX.h"
11 #include "PlatformRemoteiOS.h"
12 #if defined(__APPLE__)
13 #include "PlatformAppleSimulator.h"
14 #include "PlatformDarwinKernel.h"
15 #include "PlatformRemoteAppleBridge.h"
16 #include "PlatformRemoteAppleTV.h"
17 #include "PlatformRemoteAppleWatch.h"
18 #include "PlatformRemoteAppleXR.h"
20 #include "lldb/Breakpoint/BreakpointLocation.h"
21 #include "lldb/Core/Debugger.h"
22 #include "lldb/Core/Module.h"
23 #include "lldb/Core/ModuleList.h"
24 #include "lldb/Core/ModuleSpec.h"
25 #include "lldb/Core/PluginManager.h"
26 #include "lldb/Host/Config.h"
27 #include "lldb/Host/Host.h"
28 #include "lldb/Host/HostInfo.h"
29 #include "lldb/Symbol/ObjectFile.h"
30 #include "lldb/Target/Process.h"
31 #include "lldb/Target/Target.h"
32 #include "lldb/Utility/DataBufferHeap.h"
33 #include "lldb/Utility/FileSpec.h"
34 #include "lldb/Utility/Log.h"
35 #include "lldb/Utility/Status.h"
36 #include "lldb/Utility/StreamString.h"
41 using namespace lldb_private
;
43 LLDB_PLUGIN_DEFINE(PlatformMacOSX
)
45 static uint32_t g_initialize_count
= 0;
47 void PlatformMacOSX::Initialize() {
48 PlatformDarwin::Initialize();
49 PlatformRemoteiOS::Initialize();
50 PlatformRemoteMacOSX::Initialize();
51 #if defined(__APPLE__)
52 PlatformAppleSimulator::Initialize();
53 PlatformDarwinKernel::Initialize();
54 PlatformRemoteAppleTV::Initialize();
55 PlatformRemoteAppleWatch::Initialize();
56 PlatformRemoteAppleBridge::Initialize();
57 PlatformRemoteAppleXR::Initialize();
60 if (g_initialize_count
++ == 0) {
61 #if defined(__APPLE__)
62 PlatformSP
default_platform_sp(new PlatformMacOSX());
63 default_platform_sp
->SetSystemArchitecture(HostInfo::GetArchitecture());
64 Platform::SetHostPlatform(default_platform_sp
);
66 PluginManager::RegisterPlugin(PlatformMacOSX::GetPluginNameStatic(),
67 PlatformMacOSX::GetDescriptionStatic(),
68 PlatformMacOSX::CreateInstance
);
72 void PlatformMacOSX::Terminate() {
73 if (g_initialize_count
> 0) {
74 if (--g_initialize_count
== 0) {
75 PluginManager::UnregisterPlugin(PlatformMacOSX::CreateInstance
);
79 #if defined(__APPLE__)
80 PlatformRemoteAppleXR::Terminate();
81 PlatformRemoteAppleBridge::Terminate();
82 PlatformRemoteAppleWatch::Terminate();
83 PlatformRemoteAppleTV::Terminate();
84 PlatformDarwinKernel::Terminate();
85 PlatformAppleSimulator::Terminate();
87 PlatformRemoteMacOSX::Initialize();
88 PlatformRemoteiOS::Terminate();
89 PlatformDarwin::Terminate();
92 llvm::StringRef
PlatformMacOSX::GetDescriptionStatic() {
93 return "Local Mac OS X user platform plug-in.";
96 PlatformSP
PlatformMacOSX::CreateInstance(bool force
, const ArchSpec
*arch
) {
97 // The only time we create an instance is when we are creating a remote
98 // macosx platform which is handled by PlatformRemoteMacOSX.
102 /// Default Constructor
103 PlatformMacOSX::PlatformMacOSX() : PlatformDarwinDevice(true) {}
105 ConstString
PlatformMacOSX::GetSDKDirectory(lldb_private::Target
&target
) {
106 ModuleSP
exe_module_sp(target
.GetExecutableModule());
110 ObjectFile
*objfile
= exe_module_sp
->GetObjectFile();
114 llvm::VersionTuple version
= objfile
->GetSDKVersion();
118 // First try to find an SDK that matches the given SDK version.
119 if (FileSpec fspec
= HostInfo::GetXcodeContentsDirectory()) {
120 StreamString sdk_path
;
121 sdk_path
.Printf("%s/Developer/Platforms/MacOSX.platform/Developer/"
122 "SDKs/MacOSX%u.%u.sdk",
123 fspec
.GetPath().c_str(), version
.getMajor(),
124 *version
.getMinor());
125 if (FileSystem::Instance().Exists(fspec
))
126 return ConstString(sdk_path
.GetString());
129 // Use the default SDK as a fallback.
130 auto sdk_path_or_err
=
131 HostInfo::GetSDKRoot(HostInfo::SDKOptions
{XcodeSDK::GetAnyMacOS()});
132 if (!sdk_path_or_err
) {
133 Debugger::ReportError("Error while searching for Xcode SDK: " +
134 toString(sdk_path_or_err
.takeError()));
138 FileSpec
fspec(*sdk_path_or_err
);
140 if (FileSystem::Instance().Exists(fspec
))
141 return ConstString(fspec
.GetPath());
147 std::vector
<ArchSpec
>
148 PlatformMacOSX::GetSupportedArchitectures(const ArchSpec
&process_host_arch
) {
149 std::vector
<ArchSpec
> result
;
150 #if defined(__arm__) || defined(__arm64__) || defined(__aarch64__)
151 // When cmdline lldb is run on iOS, watchOS, etc, it is still
152 // using "PlatformMacOSX".
153 llvm::Triple::OSType host_os
= GetHostOSType();
154 ARMGetSupportedArchitectures(result
, host_os
);
156 if (host_os
== llvm::Triple::MacOSX
) {
157 // We can't use x86GetSupportedArchitectures() because it uses
158 // the system architecture for some of its return values and also
159 // has a 32bits variant.
160 result
.push_back(ArchSpec("x86_64-apple-macosx"));
161 result
.push_back(ArchSpec("x86_64-apple-ios-macabi"));
162 result
.push_back(ArchSpec("arm64-apple-ios-macabi"));
163 result
.push_back(ArchSpec("arm64e-apple-ios-macabi"));
165 // On Apple Silicon, the host platform is compatible with iOS triples to
166 // support unmodified "iPhone and iPad Apps on Apple Silicon Macs". Because
167 // the binaries are identical, we must rely on the host architecture to
168 // tell them apart and mark the host platform as compatible or not.
169 if (!process_host_arch
||
170 process_host_arch
.GetTriple().getOS() == llvm::Triple::MacOSX
) {
171 result
.push_back(ArchSpec("arm64-apple-ios"));
172 result
.push_back(ArchSpec("arm64e-apple-ios"));
176 x86GetSupportedArchitectures(result
);
177 result
.push_back(ArchSpec("x86_64-apple-ios-macabi"));
182 lldb_private::Status
PlatformMacOSX::GetSharedModule(
183 const lldb_private::ModuleSpec
&module_spec
, Process
*process
,
184 lldb::ModuleSP
&module_sp
,
185 const lldb_private::FileSpecList
*module_search_paths_ptr
,
186 llvm::SmallVectorImpl
<lldb::ModuleSP
> *old_modules
, bool *did_create_ptr
) {
187 Status error
= GetSharedModuleWithLocalCache(module_spec
, module_sp
,
188 module_search_paths_ptr
,
189 old_modules
, did_create_ptr
);
192 if (module_spec
.GetArchitecture().GetCore() ==
193 ArchSpec::eCore_x86_64_x86_64h
) {
194 ObjectFile
*objfile
= module_sp
->GetObjectFile();
195 if (objfile
== nullptr) {
196 // We didn't find an x86_64h slice, fall back to a x86_64 slice
197 ModuleSpec
module_spec_x86_64(module_spec
);
198 module_spec_x86_64
.GetArchitecture() = ArchSpec("x86_64-apple-macosx");
199 lldb::ModuleSP x86_64_module_sp
;
200 llvm::SmallVector
<lldb::ModuleSP
, 1> old_x86_64_modules
;
201 bool did_create
= false;
202 Status x86_64_error
= GetSharedModuleWithLocalCache(
203 module_spec_x86_64
, x86_64_module_sp
, module_search_paths_ptr
,
204 &old_x86_64_modules
, &did_create
);
205 if (x86_64_module_sp
&& x86_64_module_sp
->GetObjectFile()) {
206 module_sp
= x86_64_module_sp
;
208 old_modules
->append(old_x86_64_modules
.begin(),
209 old_x86_64_modules
.end());
211 *did_create_ptr
= did_create
;
219 error
= FindBundleBinaryInExecSearchPaths(module_spec
, process
, module_sp
,
220 module_search_paths_ptr
,
221 old_modules
, did_create_ptr
);
226 llvm::StringRef
PlatformMacOSX::GetDeviceSupportDirectoryName() {
227 return "macOS DeviceSupport";
230 llvm::StringRef
PlatformMacOSX::GetPlatformName() { return "MacOSX.platform"; }