LLDB mainline
PlatformMacOSX.cpp
Go to the documentation of this file.
1//===-- PlatformMacOSX.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 "PlatformMacOSX.h"
11#include "PlatformRemoteiOS.h"
12#if defined(__APPLE__)
19#endif
21#include "lldb/Core/Debugger.h"
22#include "lldb/Core/Module.h"
26#include "lldb/Host/Config.h"
27#include "lldb/Host/Host.h"
28#include "lldb/Host/HostInfo.h"
30#include "lldb/Target/Process.h"
31#include "lldb/Target/Target.h"
34#include "lldb/Utility/Log.h"
35#include "lldb/Utility/Status.h"
37
38#include <sstream>
39
40using namespace lldb;
41using namespace lldb_private;
42
44
45static uint32_t g_initialize_count = 0;
46
51#if defined(__APPLE__)
53 PlatformDarwinKernel::Initialize();
58#endif
59
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);
65#endif
69 }
70}
71
91
93 return "Local Mac OS X user platform plug-in.";
94}
95
97 // The only time we create an instance is when we are creating a remote
98 // macosx platform which is handled by PlatformRemoteMacOSX.
99 return PlatformSP();
100}
101
102/// Default Constructor
104
106 ModuleSP exe_module_sp(target.GetExecutableModule());
107 if (!exe_module_sp)
108 return {};
109
110 ObjectFile *objfile = exe_module_sp->GetObjectFile();
111 if (!objfile)
112 return {};
113
114 llvm::VersionTuple version = objfile->GetSDKVersion();
115 if (version.empty())
116 return {};
117
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());
127 }
128
129 // Use the default SDK as a fallback.
130 auto sdk_path_or_err =
132 if (!sdk_path_or_err) {
133 Debugger::ReportError(toString(sdk_path_or_err.takeError()));
134 return {};
135 }
136
137 if (FileSystem::Instance().Exists(*sdk_path_or_err))
138 return ConstString(sdk_path_or_err->GetPath());
139
140 return {};
141}
142
143std::vector<ArchSpec>
145 std::vector<ArchSpec> result;
146#if defined(__arm__) || defined(__arm64__) || defined(__aarch64__)
147 // When cmdline lldb is run on iOS, watchOS, etc, it is still
148 // using "PlatformMacOSX".
149 llvm::Triple::OSType host_os = GetHostOSType();
150 ARMGetSupportedArchitectures(result, host_os);
151
152 if (host_os == llvm::Triple::MacOSX) {
153 // We can't use x86GetSupportedArchitectures() because it uses
154 // the system architecture for some of its return values and also
155 // has a 32bits variant.
156 result.push_back(ArchSpec("x86_64-apple-macosx"));
157 result.push_back(ArchSpec("x86_64-apple-ios-macabi"));
158 result.push_back(ArchSpec("arm64-apple-ios-macabi"));
159 result.push_back(ArchSpec("arm64e-apple-ios-macabi"));
160
161 // On Apple Silicon, the host platform is compatible with iOS triples to
162 // support unmodified "iPhone and iPad Apps on Apple Silicon Macs". Because
163 // the binaries are identical, we must rely on the host architecture to
164 // tell them apart and mark the host platform as compatible or not.
165 if (!process_host_arch ||
166 process_host_arch.GetTriple().getOS() == llvm::Triple::MacOSX) {
167 result.push_back(ArchSpec("arm64-apple-ios"));
168 result.push_back(ArchSpec("arm64e-apple-ios"));
169 }
170 }
171#else
173 result.push_back(ArchSpec("x86_64-apple-ios-macabi"));
174#endif
175 return result;
176}
177
179 const lldb_private::ModuleSpec &module_spec, Process *process,
180 lldb::ModuleSP &module_sp,
181 llvm::SmallVectorImpl<lldb::ModuleSP> *old_modules, bool *did_create_ptr) {
183 module_spec, module_sp, old_modules, did_create_ptr, process);
184
185 if (module_sp) {
186 if (module_spec.GetArchitecture().GetCore() ==
188 ObjectFile *objfile = module_sp->GetObjectFile();
189 if (objfile == nullptr) {
190 // We didn't find an x86_64h slice, fall back to a x86_64 slice
191 ModuleSpec module_spec_x86_64(module_spec);
192 module_spec_x86_64.GetArchitecture() = ArchSpec("x86_64-apple-macosx");
193 lldb::ModuleSP x86_64_module_sp;
194 llvm::SmallVector<lldb::ModuleSP, 1> old_x86_64_modules;
195 bool did_create = false;
197 module_spec_x86_64, x86_64_module_sp, &old_x86_64_modules,
198 &did_create, process);
199 if (x86_64_module_sp && x86_64_module_sp->GetObjectFile()) {
200 module_sp = x86_64_module_sp;
201 if (old_modules)
202 old_modules->append(old_x86_64_modules.begin(),
203 old_x86_64_modules.end());
204 if (did_create_ptr)
205 *did_create_ptr = did_create;
206 return x86_64_error;
207 }
208 }
209 }
210 }
211
212 if (!module_sp) {
213 error = FindBundleBinaryInExecSearchPaths(module_spec, process, module_sp,
214 old_modules, did_create_ptr);
215 }
216 return error;
217}
218
220 return "macOS DeviceSupport";
221}
222
223llvm::StringRef PlatformMacOSX::GetPlatformName() { return "MacOSX.platform"; }
static llvm::raw_ostream & error(Stream &strm)
static uint32_t g_initialize_count
#define LLDB_PLUGIN_DEFINE(PluginName)
An architecture specification class.
Definition ArchSpec.h:32
llvm::Triple & GetTriple()
Architecture triple accessor.
Definition ArchSpec.h:460
Core GetCore() const
Definition ArchSpec.h:451
A uniqued constant string class.
Definition ConstString.h:40
static void ReportError(std::string message, std::optional< lldb::user_id_t > debugger_id=std::nullopt, std::once_flag *once=nullptr)
Report error events.
A file utility class.
Definition FileSpec.h:57
static FileSystem & Instance()
ArchSpec & GetArchitecture()
Definition ModuleSpec.h:93
A plug-in interface definition class for object file parsers.
Definition ObjectFile.h:46
virtual llvm::VersionTuple GetSDKVersion()
Get the SDK OS version this object file was built with.
Definition ObjectFile.h:626
Abstract Darwin platform with a potential device support directory.
virtual Status GetSharedModuleWithLocalCache(const ModuleSpec &module_spec, lldb::ModuleSP &module_sp, llvm::SmallVectorImpl< lldb::ModuleSP > *old_modules, bool *did_create_ptr, lldb_private::Process *process)
static llvm::Triple::OSType GetHostOSType()
Status FindBundleBinaryInExecSearchPaths(const ModuleSpec &module_spec, Process *process, lldb::ModuleSP &module_sp, llvm::SmallVectorImpl< lldb::ModuleSP > *old_modules, bool *did_create_ptr)
void x86GetSupportedArchitectures(std::vector< ArchSpec > &archs)
static llvm::Expected< FileSpec > ResolveXcodeSDK(XcodeSDK sdk)
Resolve an XcodeSDK to an on-disk path under a Progress event.
void ARMGetSupportedArchitectures(std::vector< ArchSpec > &archs, std::optional< llvm::Triple::OSType > os={})
The architecture selection rules for arm processors These cpu subtypes have distinct names (e....
ConstString GetSDKDirectory(Target &target) override
static llvm::StringRef GetDescriptionStatic()
llvm::StringRef GetDeviceSupportDirectoryName() override
llvm::StringRef GetPlatformName() override
static llvm::StringRef GetPluginNameStatic()
Status GetSharedModule(const ModuleSpec &module_spec, Process *process, lldb::ModuleSP &module_sp, llvm::SmallVectorImpl< lldb::ModuleSP > *old_modules, bool *did_create_ptr) override
static lldb::PlatformSP CreateInstance(bool force, const ArchSpec *arch)
std::vector< ArchSpec > GetSupportedArchitectures(const ArchSpec &process_host_arch) override
Get the platform's supported architectures in the order in which they should be searched.
PlatformMacOSX()
Default Constructor.
static void SetHostPlatform(const lldb::PlatformSP &platform_sp)
Definition Platform.cpp:150
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
static bool UnregisterPlugin(ABICreateInstance create_callback)
A plug-in interface definition class for debugging a process.
Definition Process.h:357
An error handling class.
Definition Status.h:118
llvm::StringRef GetString() const
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition Stream.cpp:134
lldb::ModuleSP GetExecutableModule()
Gets the module for the main executable.
Definition Target.cpp:1593
static XcodeSDK GetAnyMacOS()
Definition XcodeSDK.h:71
A class that represents a running process on the host machine.
std::string toString(FormatterBytecode::OpCodes op)
std::shared_ptr< lldb_private::Platform > PlatformSP
std::shared_ptr< lldb_private::Module > ModuleSP