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() {
49 #if defined(__APPLE__)
51  PlatformDarwinKernel::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(),
66  }
67 }
68 
70  if (g_initialize_count > 0) {
71  if (--g_initialize_count == 0) {
72  PluginManager::UnregisterPlugin(PlatformMacOSX::CreateInstance);
73  }
74  }
75 
76 #if defined(__APPLE__)
80  PlatformDarwinKernel::Terminate();
82 #endif
86 }
87 
89  static ConstString g_host_name(Platform::GetHostPlatformName());
90  return g_host_name;
91 }
92 
94  return "Local Mac OS X user platform plug-in.";
95 }
96 
97 PlatformSP PlatformMacOSX::CreateInstance(bool force, const ArchSpec *arch) {
98  // The only time we create an instance is when we are creating a remote
99  // macosx platform which is handled by PlatformRemoteMacOSX.
100  return PlatformSP();
101 }
102 
103 /// Default Constructor
105 
107  ModuleSP exe_module_sp(target.GetExecutableModule());
108  if (!exe_module_sp)
109  return {};
110 
111  ObjectFile *objfile = exe_module_sp->GetObjectFile();
112  if (!objfile)
113  return {};
114 
115  llvm::VersionTuple version = objfile->GetSDKVersion();
116  if (version.empty())
117  return {};
118 
119  // First try to find an SDK that matches the given SDK version.
120  if (FileSpec fspec = HostInfo::GetXcodeContentsDirectory()) {
121  StreamString sdk_path;
122  sdk_path.Printf("%s/Developer/Platforms/MacOSX.platform/Developer/"
123  "SDKs/MacOSX%u.%u.sdk",
124  fspec.GetPath().c_str(), version.getMajor(),
125  version.getMinor().getValue());
126  if (FileSystem::Instance().Exists(fspec))
127  return ConstString(sdk_path.GetString());
128  }
129 
130  // Use the default SDK as a fallback.
131  FileSpec fspec(
132  HostInfo::GetXcodeSDKPath(lldb_private::XcodeSDK::GetAnyMacOS()));
133  if (fspec) {
134  if (FileSystem::Instance().Exists(fspec))
135  return ConstString(fspec.GetPath());
136  }
137 
138  return {};
139 }
140 
142  ArchSpec &arch) {
143 #if defined(__arm__) || defined(__arm64__) || defined(__aarch64__)
144  // macOS for ARM64 support both native and translated x86_64 processes
145  if (!m_num_arm_arches || idx < m_num_arm_arches) {
146  bool res = ARMGetSupportedArchitectureAtIndex(idx, arch);
147  if (res)
148  return true;
149  if (!m_num_arm_arches)
150  m_num_arm_arches = idx;
151  }
152 
153  // We can't use x86GetSupportedArchitectureAtIndex() because it uses
154  // the system architecture for some of its return values and also
155  // has a 32bits variant.
156  if (idx == m_num_arm_arches) {
157  arch.SetTriple("x86_64-apple-macosx");
158  return true;
159  } else if (idx == m_num_arm_arches + 1) {
160  arch.SetTriple("x86_64-apple-ios-macabi");
161  return true;
162  } else if (idx == m_num_arm_arches + 2) {
163  arch.SetTriple("arm64-apple-ios");
164  return true;
165  } else if (idx == m_num_arm_arches + 3) {
166  arch.SetTriple("arm64e-apple-ios");
167  return true;
168  }
169 
170  return false;
171 #else
172  return x86GetSupportedArchitectureAtIndex(idx, arch);
173 #endif
174 }
175 
177  const lldb_private::ModuleSpec &module_spec, Process *process,
178  lldb::ModuleSP &module_sp,
179  const lldb_private::FileSpecList *module_search_paths_ptr,
180  llvm::SmallVectorImpl<lldb::ModuleSP> *old_modules, bool *did_create_ptr) {
181  Status error = GetSharedModuleWithLocalCache(module_spec, module_sp,
182  module_search_paths_ptr,
183  old_modules, did_create_ptr);
184 
185  if (module_sp) {
186  if (module_spec.GetArchitecture().GetCore() ==
187  ArchSpec::eCore_x86_64_x86_64h) {
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;
196  Status x86_64_error = GetSharedModuleWithLocalCache(
197  module_spec_x86_64, x86_64_module_sp, module_search_paths_ptr,
198  &old_x86_64_modules, &did_create);
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  module_search_paths_ptr,
215  old_modules, did_create_ptr);
216  }
217  return error;
218 }
PlatformRemoteAppleTV.h
PlatformMacOSX::CreateInstance
static lldb::PlatformSP CreateInstance(bool force, const lldb_private::ArchSpec *arch)
Definition: PlatformMacOSX.cpp:97
lldb_private::ArchSpec
Definition: ArchSpec.h:33
PlatformRemoteAppleTV::Initialize
static void Initialize()
Definition: PlatformRemoteAppleTV.cpp:39
PlatformRemoteMacOSX.h
ModuleSpec.h
Host.h
PlatformDarwin::x86GetSupportedArchitectureAtIndex
bool x86GetSupportedArchitectureAtIndex(uint32_t idx, lldb_private::ArchSpec &arch)
Definition: PlatformDarwin.cpp:517
lldb_private::ArchSpec::GetCore
Core GetCore() const
Definition: ArchSpec.h:423
PlatformRemoteAppleWatch::Terminate
static void Terminate()
Definition: PlatformRemoteAppleWatch.cpp:46
lldb_private::Process
Definition: Process.h:341
lldb_private::Target::GetExecutableModule
lldb::ModuleSP GetExecutableModule()
Gets the module for the main executable.
Definition: Target.cpp:1348
Module.h
BreakpointLocation.h
PlatformAppleSimulator::Initialize
static void Initialize()
Definition: PlatformAppleSimulator.cpp:664
lldb_private::ObjectFile::GetSDKVersion
virtual llvm::VersionTuple GetSDKVersion()
Get the SDK OS version this object file was built with.
Definition: ObjectFile.h:603
PlatformDarwin::ARMGetSupportedArchitectureAtIndex
bool ARMGetSupportedArchitectureAtIndex(uint32_t idx, lldb_private::ArchSpec &arch)
The architecture selection rules for arm processors These cpu subtypes have distinct names (e....
Definition: PlatformDarwin.cpp:561
PlatformMacOSX::GetSDKDirectory
lldb_private::ConstString GetSDKDirectory(lldb_private::Target &target) override
Definition: PlatformMacOSX.cpp:106
lldb_private::Target
Definition: Target.h:445
lldb_private::StreamString::GetString
llvm::StringRef GetString() const
Definition: StreamString.cpp:51
PlatformDarwin
Definition: PlatformDarwin.h:25
Process.h
lldb_private::XcodeSDK::GetAnyMacOS
static XcodeSDK GetAnyMacOS()
Definition: XcodeSDK.h:63
PlatformMacOSX::PlatformMacOSX
PlatformMacOSX()
Default Constructor.
Definition: PlatformMacOSX.cpp:104
Target.h
PlatformMacOSX::GetSharedModule
lldb_private::Status GetSharedModule(const lldb_private::ModuleSpec &module_spec, lldb_private::Process *process, lldb::ModuleSP &module_sp, const lldb_private::FileSpecList *module_search_paths_ptr, llvm::SmallVectorImpl< lldb::ModuleSP > *old_modules, bool *did_create_ptr) override
Definition: PlatformMacOSX.cpp:176
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
PlatformRemoteMacOSX::Initialize
static void Initialize()
Definition: PlatformRemoteMacOSX.cpp:39
Log.h
lldb_private::ArchSpec::SetTriple
bool SetTriple(const llvm::Triple &triple)
Architecture triple setter.
Definition: ArchSpec.cpp:739
StreamString.h
PlatformRemoteiOS::Initialize
static void Initialize()
Definition: PlatformRemoteiOS.cpp:34
lldb_private::ConstString
Definition: ConstString.h:40
lldb_private::StreamString
Definition: StreamString.h:23
PlatformMacOSX::GetSupportedArchitectureAtIndex
bool GetSupportedArchitectureAtIndex(uint32_t idx, lldb_private::ArchSpec &arch) override
Get the platform's supported architectures in the order in which they should be searched.
Definition: PlatformMacOSX.cpp:141
PlatformRemoteAppleBridge::Terminate
static void Terminate()
Definition: PlatformRemoteAppleBridge.cpp:48
lldb_private::Platform::Terminate
static void Terminate()
Definition: Platform.cpp:149
HostInfo.h
lldb_private::Platform::Initialize
static void Initialize()
Definition: Platform.cpp:147
PlatformMacOSX::GetPluginNameStatic
static lldb_private::ConstString GetPluginNameStatic()
Definition: PlatformMacOSX.cpp:88
PlatformRemoteAppleBridge::Initialize
static void Initialize()
Definition: PlatformRemoteAppleBridge.cpp:38
ObjectFile.h
PlatformDarwin::FindBundleBinaryInExecSearchPaths
lldb_private::Status FindBundleBinaryInExecSearchPaths(const lldb_private::ModuleSpec &module_spec, lldb_private::Process *process, lldb::ModuleSP &module_sp, const lldb_private::FileSpecList *module_search_paths_ptr, llvm::SmallVectorImpl< lldb::ModuleSP > *old_modules, bool *did_create_ptr)
Definition: PlatformDarwin.cpp:1728
PlatformMacOSX::GetDescriptionStatic
static const char * GetDescriptionStatic()
Definition: PlatformMacOSX.cpp:93
PlatformMacOSX::Terminate
static void Terminate()
Definition: PlatformMacOSX.cpp:69
lldb_private::ModuleSpec
Definition: ModuleSpec.h:26
PlatformRemoteiOS.h
lldb_private::Status
Definition: Status.h:44
PlatformRemoteAppleTV::Terminate
static void Terminate()
Definition: PlatformRemoteAppleTV.cpp:49
uint32_t
PlatformAppleSimulator.h
PlatformDarwinKernel.h
PlatformDarwin::GetSharedModuleWithLocalCache
virtual lldb_private::Status GetSharedModuleWithLocalCache(const lldb_private::ModuleSpec &module_spec, lldb::ModuleSP &module_sp, const lldb_private::FileSpecList *module_search_paths_ptr, llvm::SmallVectorImpl< lldb::ModuleSP > *old_modules, bool *did_create_ptr)
Definition: PlatformDarwin.cpp:221
PlatformRemoteAppleBridge.h
PlatformAppleSimulator::Terminate
static void Terminate()
Definition: PlatformAppleSimulator.cpp:673
PluginManager.h
lldb_private::ModuleSpec::GetArchitecture
ArchSpec & GetArchitecture()
Definition: ModuleSpec.h:94
PlatformRemoteAppleWatch::Initialize
static void Initialize()
Definition: PlatformRemoteAppleWatch.cpp:35
lldb_private::Stream::Printf
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
Status.h
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
FileSpec.h
LLDB_PLUGIN_DEFINE
#define LLDB_PLUGIN_DEFINE(PluginName)
Definition: PluginManager.h:31
llvm::SmallVectorImpl
Definition: Disassembler.h:42
PlatformRemoteiOS::Terminate
static void Terminate()
Definition: PlatformRemoteiOS.cpp:44
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:348
PlatformMacOSX
Definition: PlatformMacOSX.h:14
lldb
Definition: SBAddress.h:15
PlatformRemoteAppleWatch.h
lldb_private::ObjectFile
Definition: ObjectFile.h:58
DataBufferHeap.h