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__)
18#endif
20#include "lldb/Core/Debugger.h"
21#include "lldb/Core/Module.h"
25#include "lldb/Host/Config.h"
26#include "lldb/Host/Host.h"
27#include "lldb/Host/HostInfo.h"
29#include "lldb/Target/Process.h"
30#include "lldb/Target/Target.h"
33#include "lldb/Utility/Log.h"
34#include "lldb/Utility/Status.h"
36
37#include <sstream>
38
39using namespace lldb;
40using namespace lldb_private;
41
43
45
46void PlatformMacOSX::Initialize() {
50#if defined(__APPLE__)
52 PlatformDarwinKernel::Initialize();
56#endif
57
58 if (g_initialize_count++ == 0) {
59#if defined(__APPLE__)
60 PlatformSP default_platform_sp(new PlatformMacOSX());
61 default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture());
62 Platform::SetHostPlatform(default_platform_sp);
63#endif
67 }
68}
69
71 if (g_initialize_count > 0) {
72 if (--g_initialize_count == 0) {
74 }
75 }
76
77#if defined(__APPLE__)
81 PlatformDarwinKernel::Terminate();
83#endif
87}
88
90 return "Local Mac OS X user platform plug-in.";
91}
92
93PlatformSP PlatformMacOSX::CreateInstance(bool force, const ArchSpec *arch) {
94 // The only time we create an instance is when we are creating a remote
95 // macosx platform which is handled by PlatformRemoteMacOSX.
96 return PlatformSP();
97}
98
99/// Default Constructor
101
103 ModuleSP exe_module_sp(target.GetExecutableModule());
104 if (!exe_module_sp)
105 return {};
106
107 ObjectFile *objfile = exe_module_sp->GetObjectFile();
108 if (!objfile)
109 return {};
110
111 llvm::VersionTuple version = objfile->GetSDKVersion();
112 if (version.empty())
113 return {};
114
115 // First try to find an SDK that matches the given SDK version.
116 if (FileSpec fspec = HostInfo::GetXcodeContentsDirectory()) {
117 StreamString sdk_path;
118 sdk_path.Printf("%s/Developer/Platforms/MacOSX.platform/Developer/"
119 "SDKs/MacOSX%u.%u.sdk",
120 fspec.GetPath().c_str(), version.getMajor(),
121 *version.getMinor());
122 if (FileSystem::Instance().Exists(fspec))
123 return ConstString(sdk_path.GetString());
124 }
125
126 // Use the default SDK as a fallback.
127 auto sdk_path_or_err =
128 HostInfo::GetSDKRoot(HostInfo::SDKOptions{XcodeSDK::GetAnyMacOS()});
129 if (!sdk_path_or_err) {
130 Debugger::ReportError("Error while searching for Xcode SDK: " +
131 toString(sdk_path_or_err.takeError()));
132 return {};
133 }
134
135 FileSpec fspec(*sdk_path_or_err);
136 if (fspec) {
137 if (FileSystem::Instance().Exists(fspec))
138 return ConstString(fspec.GetPath());
139 }
140
141 return {};
142}
143
144std::vector<ArchSpec>
146 std::vector<ArchSpec> result;
147#if defined(__arm__) || defined(__arm64__) || defined(__aarch64__)
148 // When cmdline lldb is run on iOS, watchOS, etc, it is still
149 // using "PlatformMacOSX".
150 llvm::Triple::OSType host_os = GetHostOSType();
151 ARMGetSupportedArchitectures(result, host_os);
152
153 if (host_os == llvm::Triple::MacOSX) {
154 // We can't use x86GetSupportedArchitectures() because it uses
155 // the system architecture for some of its return values and also
156 // has a 32bits variant.
157 result.push_back(ArchSpec("x86_64-apple-macosx"));
158 result.push_back(ArchSpec("x86_64-apple-ios-macabi"));
159 result.push_back(ArchSpec("arm64-apple-ios-macabi"));
160 result.push_back(ArchSpec("arm64e-apple-ios-macabi"));
161
162 // On Apple Silicon, the host platform is compatible with iOS triples to
163 // support unmodified "iPhone and iPad Apps on Apple Silicon Macs". Because
164 // the binaries are identical, we must rely on the host architecture to
165 // tell them apart and mark the host platform as compatible or not.
166 if (!process_host_arch ||
167 process_host_arch.GetTriple().getOS() == llvm::Triple::MacOSX) {
168 result.push_back(ArchSpec("arm64-apple-ios"));
169 result.push_back(ArchSpec("arm64e-apple-ios"));
170 }
171 }
172#else
174 result.push_back(ArchSpec("x86_64-apple-ios-macabi"));
175#endif
176 return result;
177}
178
180 const lldb_private::ModuleSpec &module_spec, Process *process,
181 lldb::ModuleSP &module_sp,
182 const lldb_private::FileSpecList *module_search_paths_ptr,
183 llvm::SmallVectorImpl<lldb::ModuleSP> *old_modules, bool *did_create_ptr) {
184 Status error = GetSharedModuleWithLocalCache(module_spec, module_sp,
185 module_search_paths_ptr,
186 old_modules, did_create_ptr);
187
188 if (module_sp) {
189 if (module_spec.GetArchitecture().GetCore() ==
191 ObjectFile *objfile = module_sp->GetObjectFile();
192 if (objfile == nullptr) {
193 // We didn't find an x86_64h slice, fall back to a x86_64 slice
194 ModuleSpec module_spec_x86_64(module_spec);
195 module_spec_x86_64.GetArchitecture() = ArchSpec("x86_64-apple-macosx");
196 lldb::ModuleSP x86_64_module_sp;
197 llvm::SmallVector<lldb::ModuleSP, 1> old_x86_64_modules;
198 bool did_create = false;
200 module_spec_x86_64, x86_64_module_sp, module_search_paths_ptr,
201 &old_x86_64_modules, &did_create);
202 if (x86_64_module_sp && x86_64_module_sp->GetObjectFile()) {
203 module_sp = x86_64_module_sp;
204 if (old_modules)
205 old_modules->append(old_x86_64_modules.begin(),
206 old_x86_64_modules.end());
207 if (did_create_ptr)
208 *did_create_ptr = did_create;
209 return x86_64_error;
210 }
211 }
212 }
213 }
214
215 if (!module_sp) {
216 error = FindBundleBinaryInExecSearchPaths(module_spec, process, module_sp,
217 module_search_paths_ptr,
218 old_modules, did_create_ptr);
219 }
220 return error;
221}
222
224 return "macOS DeviceSupport";
225}
226
227llvm::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:31
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:39
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:1488
A file collection class.
Definition: FileSpecList.h:24
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:366
static FileSystem & Instance()
ArchSpec & GetArchitecture()
Definition: ModuleSpec.h:89
A plug-in interface definition class for object file parsers.
Definition: ObjectFile.h:43
virtual llvm::VersionTuple GetSDKVersion()
Get the SDK OS version this object file was built with.
Definition: ObjectFile.h:606
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:146
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:335
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:107
lldb::ModuleSP GetExecutableModule()
Gets the module for the main executable.
Definition: Target.cpp:1373
static XcodeSDK GetAnyMacOS()
Definition: XcodeSDK.h:63
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
const char * toString(AppleArm64ExceptionClass EC)
Definition: SBAddress.h:15