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
47void PlatformMacOSX::Initialize() {
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
73 if (g_initialize_count > 0) {
74 if (--g_initialize_count == 0) {
76 }
77 }
78
79#if defined(__APPLE__)
84 PlatformDarwinKernel::Terminate();
86#endif
90}
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 =
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()));
135 return {};
136 }
137
138 FileSpec fspec(*sdk_path_or_err);
139 if (fspec) {
140 if (FileSystem::Instance().Exists(fspec))
141 return ConstString(fspec.GetPath());
142 }
143
144 return {};
145}
146
147std::vector<ArchSpec>
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);
155
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"));
164
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"));
173 }
174 }
175#else
177 result.push_back(ArchSpec("x86_64-apple-ios-macabi"));
178#endif
179 return result;
180}
181
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);
190
191 if (module_sp) {
192 if (module_spec.GetArchitecture().GetCore() ==
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;
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;
207 if (old_modules)
208 old_modules->append(old_x86_64_modules.begin(),
209 old_x86_64_modules.end());
210 if (did_create_ptr)
211 *did_create_ptr = did_create;
212 return x86_64_error;
213 }
214 }
215 }
216 }
217
218 if (!module_sp) {
219 error = FindBundleBinaryInExecSearchPaths(module_spec, process, module_sp,
220 module_search_paths_ptr,
221 old_modules, did_create_ptr);
222 }
223 return error;
224}
225
227 return "macOS DeviceSupport";
228}
229
230llvm::StringRef PlatformMacOSX::GetPlatformName() { return "MacOSX.platform"; }
static llvm::raw_ostream & error(Stream &strm)
static uint32_t g_initialize_count
static uint32_t g_initialize_count
#define LLDB_PLUGIN_DEFINE(PluginName)
Definition: PluginManager.h:32
An architecture specification class.
Definition: ArchSpec.h:31
llvm::Triple & GetTriple()
Architecture triple accessor.
Definition: ArchSpec.h:450
Core GetCore() const
Definition: ArchSpec.h:429
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.
Definition: Debugger.cpp:1593
A file collection class.
Definition: FileSpecList.h:91
A file utility class.
Definition: FileSpec.h:56
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition: FileSpec.cpp:367
static FileSystem & Instance()
ArchSpec & GetArchitecture()
Definition: ModuleSpec.h:89
A plug-in interface definition class for object file parsers.
Definition: ObjectFile.h:44
virtual llvm::VersionTuple GetSDKVersion()
Get the SDK OS version this object file was built with.
Definition: ObjectFile.h:632
Abstract Darwin platform with a potential device support directory.
virtual Status GetSharedModuleWithLocalCache(const ModuleSpec &module_spec, lldb::ModuleSP &module_sp, const FileSpecList *module_search_paths_ptr, llvm::SmallVectorImpl< lldb::ModuleSP > *old_modules, bool *did_create_ptr)
static llvm::Triple::OSType GetHostOSType()
Status FindBundleBinaryInExecSearchPaths(const ModuleSpec &module_spec, Process *process, lldb::ModuleSP &module_sp, const FileSpecList *module_search_paths_ptr, llvm::SmallVectorImpl< lldb::ModuleSP > *old_modules, bool *did_create_ptr)
void x86GetSupportedArchitectures(std::vector< ArchSpec > &archs)
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()
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.
Status GetSharedModule(const ModuleSpec &module_spec, Process *process, lldb::ModuleSP &module_sp, const FileSpecList *module_search_paths_ptr, llvm::SmallVectorImpl< lldb::ModuleSP > *old_modules, bool *did_create_ptr) override
PlatformMacOSX()
Default Constructor.
static void SetHostPlatform(const lldb::PlatformSP &platform_sp)
Definition: Platform.cpp:145
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:341
An error handling class.
Definition: Status.h:44
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:1423
static XcodeSDK GetAnyMacOS()
Definition: XcodeSDK.h:65
A class that represents a running process on the host machine.
const char * toString(AppleArm64ExceptionClass EC)
Definition: SBAddress.h:15
std::shared_ptr< lldb_private::Platform > PlatformSP
Definition: lldb-forward.h:386
std::shared_ptr< lldb_private::Module > ModuleSP
Definition: lldb-forward.h:371