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"
10 #include "PlatformRemoteMacOSX.h"
11 #include "PlatformRemoteiOS.h"
12 #if defined(__APPLE__)
13 #include "PlatformAppleSimulator.h"
14 #include "PlatformDarwinKernel.h"
16 #include "PlatformRemoteAppleTV.h"
18 #endif
20 #include "lldb/Core/Module.h"
21 #include "lldb/Core/ModuleList.h"
22 #include "lldb/Core/ModuleSpec.h"
24 #include "lldb/Host/Config.h"
25 #include "lldb/Host/Host.h"
26 #include "lldb/Host/HostInfo.h"
27 #include "lldb/Symbol/ObjectFile.h"
28 #include "lldb/Target/Process.h"
29 #include "lldb/Target/Target.h"
31 #include "lldb/Utility/FileSpec.h"
32 #include "lldb/Utility/Log.h"
33 #include "lldb/Utility/Status.h"
35 
36 #include <sstream>
37 
38 using namespace lldb;
39 using namespace lldb_private;
40 
42 
44 
45 void PlatformMacOSX::Initialize() {
46  PlatformDarwin::Initialize();
47  PlatformRemoteiOS::Initialize();
48  PlatformRemoteMacOSX::Initialize();
49 #if defined(__APPLE__)
50  PlatformAppleSimulator::Initialize();
51  PlatformDarwinKernel::Initialize();
52  PlatformRemoteAppleTV::Initialize();
53  PlatformRemoteAppleWatch::Initialize();
54  PlatformRemoteAppleBridge::Initialize();
55 #endif
56 
57  if (g_initialize_count++ == 0) {
58 #if defined(__APPLE__)
59  PlatformSP default_platform_sp(new PlatformMacOSX());
60  default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture());
61  Platform::SetHostPlatform(default_platform_sp);
62 #endif
63  PluginManager::RegisterPlugin(PlatformMacOSX::GetPluginNameStatic(),
64  PlatformMacOSX::GetDescriptionStatic(),
65  PlatformMacOSX::CreateInstance);
66  }
67 }
68 
69 void PlatformMacOSX::Terminate() {
70  if (g_initialize_count > 0) {
71  if (--g_initialize_count == 0) {
72  PluginManager::UnregisterPlugin(PlatformMacOSX::CreateInstance);
73  }
74  }
75 
76 #if defined(__APPLE__)
77  PlatformRemoteAppleBridge::Terminate();
78  PlatformRemoteAppleWatch::Terminate();
79  PlatformRemoteAppleTV::Terminate();
80  PlatformDarwinKernel::Terminate();
81  PlatformAppleSimulator::Terminate();
82 #endif
83  PlatformRemoteMacOSX::Initialize();
84  PlatformRemoteiOS::Terminate();
85  PlatformDarwin::Terminate();
86 }
87 
88 llvm::StringRef PlatformMacOSX::GetDescriptionStatic() {
89  return "Local Mac OS X user platform plug-in.";
90 }
91 
92 PlatformSP PlatformMacOSX::CreateInstance(bool force, const ArchSpec *arch) {
93  // The only time we create an instance is when we are creating a remote
94  // macosx platform which is handled by PlatformRemoteMacOSX.
95  return PlatformSP();
96 }
97 
98 /// Default Constructor
99 PlatformMacOSX::PlatformMacOSX() : PlatformDarwinDevice(true) {}
100 
102  ModuleSP exe_module_sp(target.GetExecutableModule());
103  if (!exe_module_sp)
104  return {};
105 
106  ObjectFile *objfile = exe_module_sp->GetObjectFile();
107  if (!objfile)
108  return {};
109 
110  llvm::VersionTuple version = objfile->GetSDKVersion();
111  if (version.empty())
112  return {};
113 
114  // First try to find an SDK that matches the given SDK version.
115  if (FileSpec fspec = HostInfo::GetXcodeContentsDirectory()) {
116  StreamString sdk_path;
117  sdk_path.Printf("%s/Developer/Platforms/MacOSX.platform/Developer/"
118  "SDKs/MacOSX%u.%u.sdk",
119  fspec.GetPath().c_str(), version.getMajor(),
120  version.getMinor().getValue());
121  if (FileSystem::Instance().Exists(fspec))
122  return ConstString(sdk_path.GetString());
123  }
124 
125  // Use the default SDK as a fallback.
126  FileSpec fspec(
127  HostInfo::GetXcodeSDKPath(lldb_private::XcodeSDK::GetAnyMacOS()));
128  if (fspec) {
129  if (FileSystem::Instance().Exists(fspec))
130  return ConstString(fspec.GetPath());
131  }
132 
133  return {};
134 }
135 
136 std::vector<ArchSpec>
138  std::vector<ArchSpec> result;
139 #if defined(__arm__) || defined(__arm64__) || defined(__aarch64__)
140  // When cmdline lldb is run on iOS, watchOS, etc, it is still
141  // using "PlatformMacOSX".
142  llvm::Triple::OSType host_os = GetHostOSType();
143  ARMGetSupportedArchitectures(result, host_os);
144 
145  if (host_os == llvm::Triple::MacOSX) {
146  // We can't use x86GetSupportedArchitectures() because it uses
147  // the system architecture for some of its return values and also
148  // has a 32bits variant.
149  result.push_back(ArchSpec("x86_64-apple-macosx"));
150  result.push_back(ArchSpec("x86_64-apple-ios-macabi"));
151  result.push_back(ArchSpec("arm64-apple-ios-macabi"));
152  result.push_back(ArchSpec("arm64e-apple-ios-macabi"));
153 
154  // On Apple Silicon, the host platform is compatible with iOS triples to
155  // support unmodified "iPhone and iPad Apps on Apple Silicon Macs". Because
156  // the binaries are identical, we must rely on the host architecture to
157  // tell them apart and mark the host platform as compatible or not.
158  if (!process_host_arch ||
159  process_host_arch.GetTriple().getOS() == llvm::Triple::MacOSX) {
160  result.push_back(ArchSpec("arm64-apple-ios"));
161  result.push_back(ArchSpec("arm64e-apple-ios"));
162  }
163  }
164 #else
166  result.push_back(ArchSpec("x86_64-apple-ios-macabi"));
167 #endif
168  return result;
169 }
170 
172  const lldb_private::ModuleSpec &module_spec, Process *process,
173  lldb::ModuleSP &module_sp,
174  const lldb_private::FileSpecList *module_search_paths_ptr,
175  llvm::SmallVectorImpl<lldb::ModuleSP> *old_modules, bool *did_create_ptr) {
176  Status error = GetSharedModuleWithLocalCache(module_spec, module_sp,
177  module_search_paths_ptr,
178  old_modules, did_create_ptr);
179 
180  if (module_sp) {
181  if (module_spec.GetArchitecture().GetCore() ==
183  ObjectFile *objfile = module_sp->GetObjectFile();
184  if (objfile == nullptr) {
185  // We didn't find an x86_64h slice, fall back to a x86_64 slice
186  ModuleSpec module_spec_x86_64(module_spec);
187  module_spec_x86_64.GetArchitecture() = ArchSpec("x86_64-apple-macosx");
188  lldb::ModuleSP x86_64_module_sp;
189  llvm::SmallVector<lldb::ModuleSP, 1> old_x86_64_modules;
190  bool did_create = false;
191  Status x86_64_error = GetSharedModuleWithLocalCache(
192  module_spec_x86_64, x86_64_module_sp, module_search_paths_ptr,
193  &old_x86_64_modules, &did_create);
194  if (x86_64_module_sp && x86_64_module_sp->GetObjectFile()) {
195  module_sp = x86_64_module_sp;
196  if (old_modules)
197  old_modules->append(old_x86_64_modules.begin(),
198  old_x86_64_modules.end());
199  if (did_create_ptr)
200  *did_create_ptr = did_create;
201  return x86_64_error;
202  }
203  }
204  }
205  }
206 
207  if (!module_sp) {
208  error = FindBundleBinaryInExecSearchPaths(module_spec, process, module_sp,
209  module_search_paths_ptr,
210  old_modules, did_create_ptr);
211  }
212  return error;
213 }
214 
216  return "macOS DeviceSupport";
217 }
218 
219 llvm::StringRef PlatformMacOSX::GetPlatformName() { return "MacOSX.platform"; }
PlatformRemoteAppleTV.h
lldb_private::ArchSpec
Definition: ArchSpec.h:33
lldb_private::PlatformDarwin::x86GetSupportedArchitectures
void x86GetSupportedArchitectures(std::vector< ArchSpec > &archs)
Definition: PlatformDarwin.cpp:354
PlatformRemoteMacOSX.h
ModuleSpec.h
Host.h
lldb_private::ArchSpec::GetCore
Core GetCore() const
Definition: ArchSpec.h:434
lldb_private::Process
Definition: Process.h:338
lldb_private::Target::GetExecutableModule
lldb::ModuleSP GetExecutableModule()
Gets the module for the main executable.
Definition: Target.cpp:1357
lldb_private::PlatformDarwin::ARMGetSupportedArchitectures
void ARMGetSupportedArchitectures(std::vector< ArchSpec > &archs, llvm::Optional< llvm::Triple::OSType > os={})
The architecture selection rules for arm processors These cpu subtypes have distinct names (e....
Definition: PlatformDarwin.cpp:474
Module.h
BreakpointLocation.h
lldb_private::ObjectFile::GetSDKVersion
virtual llvm::VersionTuple GetSDKVersion()
Get the SDK OS version this object file was built with.
Definition: ObjectFile.h:625
lldb_private::ArchSpec::GetTriple
llvm::Triple & GetTriple()
Architecture triple accessor.
Definition: ArchSpec.h:455
lldb_private::Target
Definition: Target.h:464
lldb_private::StreamString::GetString
llvm::StringRef GetString() const
Definition: StreamString.cpp:51
Process.h
lldb_private::XcodeSDK::GetAnyMacOS
static XcodeSDK GetAnyMacOS()
Definition: XcodeSDK.h:63
Target.h
g_initialize_count
static uint32_t g_initialize_count
Definition: PlatformMacOSX.cpp:43
PlatformMacOSX.h
lldb_private::FileSpec
Definition: FileSpec.h:56
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
ModuleList.h
lldb_private::PlatformMacOSX::GetSharedModule
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
Definition: PlatformMacOSX.cpp:171
Log.h
lldb_private::ArchSpec::eCore_x86_64_x86_64h
@ eCore_x86_64_x86_64h
Definition: ArchSpec.h:200
lldb_private::PlatformMacOSX::GetSDKDirectory
ConstString GetSDKDirectory(Target &target) override
Definition: PlatformMacOSX.cpp:101
StreamString.h
lldb_private::ConstString
Definition: ConstString.h:40
lldb_private::StreamString
Definition: StreamString.h:23
HostInfo.h
ObjectFile.h
lldb_private::PlatformMacOSX::GetDeviceSupportDirectoryName
llvm::StringRef GetDeviceSupportDirectoryName() override
Definition: PlatformMacOSX.cpp:215
lldb_private::ModuleSpec
Definition: ModuleSpec.h:27
PlatformRemoteiOS.h
lldb_private::Status
Definition: Status.h:44
uint32_t
lldb_private::PlatformMacOSX::GetPlatformName
llvm::StringRef GetPlatformName() override
Definition: PlatformMacOSX.cpp:219
lldb_private::PlatformDarwinDevice
Abstract Darwin platform with a potential device support directory.
Definition: PlatformDarwinDevice.h:21
PlatformAppleSimulator.h
PlatformDarwinKernel.h
lldb_private::PlatformDarwinDevice::GetSharedModuleWithLocalCache
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)
Definition: PlatformDarwinDevice.cpp:286
PlatformRemoteAppleBridge.h
lldb_private::PlatformDarwin::FindBundleBinaryInExecSearchPaths
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)
Definition: PlatformDarwin.cpp:1066
lldb_private::PlatformDarwin::GetHostOSType
static llvm::Triple::OSType GetHostOSType()
Definition: PlatformDarwin.cpp:1168
PluginManager.h
lldb_private::ModuleSpec::GetArchitecture
ArchSpec & GetArchitecture()
Definition: ModuleSpec.h:89
lldb_private::Stream::Printf
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
lldb_private::FileSystem::Instance
static FileSystem & Instance()
Definition: common/FileSystem.cpp:46
Status.h
lldb_private::PlatformMacOSX::GetSupportedArchitectures
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.
Definition: PlatformMacOSX.cpp:137
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::PlatformMacOSX
Definition: PlatformMacOSX.h:31
FileSpec.h
LLDB_PLUGIN_DEFINE
#define LLDB_PLUGIN_DEFINE(PluginName)
Definition: PluginManager.h:31
llvm::SmallVectorImpl
Definition: Disassembler.h:42
lldb_private::FileSpec::GetPath
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition: FileSpec.cpp:347
lldb
Definition: SBAddress.h:15
PlatformRemoteAppleWatch.h
lldb_private::ObjectFile
Definition: ObjectFile.h:60
DataBufferHeap.h