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 "PlatformRemoteiOS.h"
11 #if defined(__APPLE__)
14 #include "PlatformDarwinKernel.h"
16 #include "PlatformRemoteAppleTV.h"
18 #include "PlatformiOSSimulator.h"
19 #endif
21 #include "lldb/Core/Module.h"
22 #include "lldb/Core/ModuleList.h"
23 #include "lldb/Core/ModuleSpec.h"
25 #include "lldb/Host/Config.h"
26 #include "lldb/Host/Host.h"
27 #include "lldb/Host/HostInfo.h"
28 #include "lldb/Symbol/ObjectFile.h"
29 #include "lldb/Target/Process.h"
30 #include "lldb/Target/Target.h"
32 #include "lldb/Utility/FileSpec.h"
33 #include "lldb/Utility/Log.h"
34 #include "lldb/Utility/Status.h"
36 
37 #include <sstream>
38 
39 using namespace lldb;
40 using namespace lldb_private;
41 
43 
45 
46 void PlatformMacOSX::Initialize() {
49 #if defined(__APPLE__)
51  PlatformDarwinKernel::Initialize();
57 #endif
58 
59  if (g_initialize_count++ == 0) {
60 #if defined(__APPLE__)
61  PlatformSP default_platform_sp(new PlatformMacOSX(true));
62  default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture());
63  Platform::SetHostPlatform(default_platform_sp);
64 #endif
65  PluginManager::RegisterPlugin(PlatformMacOSX::GetPluginNameStatic(false),
68  }
69 }
70 
72  if (g_initialize_count > 0) {
73  if (--g_initialize_count == 0) {
74  PluginManager::UnregisterPlugin(PlatformMacOSX::CreateInstance);
75  }
76  }
77 
78 #if defined(__APPLE__)
84  PlatformDarwinKernel::Terminate();
86 #endif
89 }
90 
91 PlatformSP PlatformMacOSX::CreateInstance(bool force, const ArchSpec *arch) {
93  if (log) {
94  const char *arch_name;
95  if (arch && arch->GetArchitectureName())
96  arch_name = arch->GetArchitectureName();
97  else
98  arch_name = "<null>";
99 
100  const char *triple_cstr =
101  arch ? arch->GetTriple().getTriple().c_str() : "<null>";
102 
103  LLDB_LOGF(log, "PlatformMacOSX::%s(force=%s, arch={%s,%s})", __FUNCTION__,
104  force ? "true" : "false", arch_name, triple_cstr);
105  }
106 
107  // The only time we create an instance is when we are creating a remote
108  // macosx platform
109  const bool is_host = false;
110 
111  bool create = force;
112  if (!create && arch && arch->IsValid()) {
113  const llvm::Triple &triple = arch->GetTriple();
114  switch (triple.getVendor()) {
115  case llvm::Triple::Apple:
116  create = true;
117  break;
118 
119 #if defined(__APPLE__)
120  // Only accept "unknown" for vendor if the host is Apple and it "unknown"
121  // wasn't specified (it was just returned because it was NOT specified)
122  case llvm::Triple::UnknownVendor:
123  create = !arch->TripleVendorWasSpecified();
124  break;
125 #endif
126  default:
127  break;
128  }
129 
130  if (create) {
131  switch (triple.getOS()) {
132  case llvm::Triple::Darwin: // Deprecated, but still support Darwin for
133  // historical reasons
134  case llvm::Triple::MacOSX:
135  break;
136 #if defined(__APPLE__)
137  // Only accept "vendor" for vendor if the host is Apple and it "unknown"
138  // wasn't specified (it was just returned because it was NOT specified)
139  case llvm::Triple::UnknownOS:
140  create = !arch->TripleOSWasSpecified();
141  break;
142 #endif
143  default:
144  create = false;
145  break;
146  }
147  }
148  }
149  if (create) {
150  LLDB_LOGF(log, "PlatformMacOSX::%s() creating platform", __FUNCTION__);
151  return PlatformSP(new PlatformMacOSX(is_host));
152  }
153 
154  LLDB_LOGF(log, "PlatformMacOSX::%s() aborting creation of platform",
155  __FUNCTION__);
156 
157  return PlatformSP();
158 }
159 
161  if (is_host) {
162  static ConstString g_host_name(Platform::GetHostPlatformName());
163  return g_host_name;
164  } else {
165  static ConstString g_remote_name("remote-macosx");
166  return g_remote_name;
167  }
168 }
169 
170 const char *PlatformMacOSX::GetDescriptionStatic(bool is_host) {
171  if (is_host)
172  return "Local Mac OS X user platform plug-in.";
173  else
174  return "Remote Mac OS X user platform plug-in.";
175 }
176 
177 /// Default Constructor
179 
180 /// Destructor.
181 ///
182 /// The destructor is virtual since this class is designed to be
183 /// inherited from by the plug-in instance.
185 
187  ModuleSP exe_module_sp(target.GetExecutableModule());
188  if (!exe_module_sp)
189  return {};
190 
191  ObjectFile *objfile = exe_module_sp->GetObjectFile();
192  if (!objfile)
193  return {};
194 
195  llvm::VersionTuple version = objfile->GetSDKVersion();
196  if (version.empty())
197  return {};
198 
199  // First try to find an SDK that matches the given SDK version.
200  if (FileSpec fspec = HostInfo::GetXcodeContentsDirectory()) {
201  StreamString sdk_path;
202  sdk_path.Printf("%s/Developer/Platforms/MacOSX.platform/Developer/"
203  "SDKs/MacOSX%u.%u.sdk",
204  fspec.GetPath().c_str(), version.getMajor(),
205  version.getMinor().getValue());
206  if (FileSystem::Instance().Exists(fspec))
207  return ConstString(sdk_path.GetString());
208  }
209 
210  // Use the default SDK as a fallback.
211  FileSpec fspec(
212  HostInfo::GetXcodeSDKPath(lldb_private::XcodeSDK::GetAnyMacOS()));
213  if (fspec) {
214  if (FileSystem::Instance().Exists(fspec))
215  return ConstString(fspec.GetPath());
216  }
217 
218  return {};
219 }
220 
222  const UUID *uuid_ptr,
223  FileSpec &local_file) {
224  if (IsRemote()) {
226  return m_remote_platform_sp->GetFileWithUUID(platform_file, uuid_ptr,
227  local_file);
228  }
229 
230  // Default to the local case
231  local_file = platform_file;
232  return Status();
233 }
234 
237  const lldb_private::UUID *uuid_ptr,
238  lldb_private::FileSpec &local_file) {
239  if (IsRemote() && m_remote_platform_sp) {
240  std::string local_os_build;
241 #if !defined(__linux__)
242  HostInfo::GetOSBuildString(local_os_build);
243 #endif
244  std::string remote_os_build;
245  m_remote_platform_sp->GetOSBuildString(remote_os_build);
246  if (local_os_build == remote_os_build) {
247  // same OS version: the local file is good enough
248  local_file = platform_file;
249  return Status();
250  } else {
251  // try to find the file in the cache
252  std::string cache_path(GetLocalCacheDirectory());
253  std::string module_path(platform_file.GetPath());
254  cache_path.append(module_path);
255  FileSpec module_cache_spec(cache_path);
256  if (FileSystem::Instance().Exists(module_cache_spec)) {
257  local_file = module_cache_spec;
258  return Status();
259  }
260  // bring in the remote module file
261  FileSpec module_cache_folder =
262  module_cache_spec.CopyByRemovingLastPathComponent();
263  // try to make the local directory first
264  Status err(
265  llvm::sys::fs::create_directory(module_cache_folder.GetPath()));
266  if (err.Fail())
267  return err;
268  err = GetFile(platform_file, module_cache_spec);
269  if (err.Fail())
270  return err;
271  if (FileSystem::Instance().Exists(module_cache_spec)) {
272  local_file = module_cache_spec;
273  return Status();
274  } else
275  return Status("unable to obtain valid module file");
276  }
277  }
278  local_file = platform_file;
279  return Status();
280 }
281 
283  ArchSpec &arch) {
284 #if defined(__arm__) || defined(__arm64__) || defined(__aarch64__)
285  return ARMGetSupportedArchitectureAtIndex(idx, arch);
286 #else
287  return x86GetSupportedArchitectureAtIndex(idx, arch);
288 #endif
289 }
290 
292  const lldb_private::ModuleSpec &module_spec, Process *process,
293  lldb::ModuleSP &module_sp,
294  const lldb_private::FileSpecList *module_search_paths_ptr,
295  lldb::ModuleSP *old_module_sp_ptr, bool *did_create_ptr) {
297  module_spec, module_sp, module_search_paths_ptr, old_module_sp_ptr,
298  did_create_ptr);
299 
300  if (module_sp) {
301  if (module_spec.GetArchitecture().GetCore() ==
302  ArchSpec::eCore_x86_64_x86_64h) {
303  ObjectFile *objfile = module_sp->GetObjectFile();
304  if (objfile == nullptr) {
305  // We didn't find an x86_64h slice, fall back to a x86_64 slice
306  ModuleSpec module_spec_x86_64(module_spec);
307  module_spec_x86_64.GetArchitecture() = ArchSpec("x86_64-apple-macosx");
308  lldb::ModuleSP x86_64_module_sp;
309  lldb::ModuleSP old_x86_64_module_sp;
310  bool did_create = false;
311  Status x86_64_error = GetSharedModuleWithLocalCache(
312  module_spec_x86_64, x86_64_module_sp, module_search_paths_ptr,
313  &old_x86_64_module_sp, &did_create);
314  if (x86_64_module_sp && x86_64_module_sp->GetObjectFile()) {
315  module_sp = x86_64_module_sp;
316  if (old_module_sp_ptr)
317  *old_module_sp_ptr = old_x86_64_module_sp;
318  if (did_create_ptr)
319  *did_create_ptr = did_create;
320  return x86_64_error;
321  }
322  }
323  }
324  }
325 
326  if (!module_sp) {
327  error = FindBundleBinaryInExecSearchPaths (module_spec, process, module_sp, module_search_paths_ptr, old_module_sp_ptr, did_create_ptr);
328  }
329  return error;
330 }
Core GetCore() const
Definition: ArchSpec.h:413
virtual const char * GetLocalCacheDirectory()
Definition: Platform.cpp:1356
A class that represents a running process on the host machine.
static const char * GetDescriptionStatic(bool is_host)
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, lldb::ModuleSP *old_module_sp_ptr, bool *did_create_ptr)
bool ARMGetSupportedArchitectureAtIndex(uint32_t idx, lldb_private::ArchSpec &arch)
A file utility class.
Definition: FileSpec.h:56
An architecture specification class.
Definition: ArchSpec.h:33
static lldb_private::ConstString GetPluginNameStatic(bool is_host)
A plug-in interface definition class for object file parsers.
Definition: ObjectFile.h:58
#define LIBLLDB_LOG_PLATFORM
Definition: Logging.h:39
static void Terminate()
lldb_private::Status GetSymbolFile(const lldb_private::FileSpec &platform_file, const lldb_private::UUID *uuid_ptr, lldb_private::FileSpec &local_file)
static XcodeSDK GetAnyMacOS()
Definition: XcodeSDK.h:63
static void Terminate()
Definition: Platform.cpp:152
bool IsValid() const
Tests if this ArchSpec is valid.
Definition: ArchSpec.h:332
static uint32_t g_initialize_count
llvm::Triple & GetTriple()
Architecture triple accessor.
Definition: ArchSpec.h:434
bool TripleVendorWasSpecified() const
Definition: ArchSpec.h:337
FileSpec CopyByRemovingLastPathComponent() const
Definition: FileSpec.cpp:406
const char * GetArchitectureName() const
Returns a static string representing the current architecture.
Definition: ArchSpec.cpp:585
#define LLDB_PLUGIN_DEFINE(PluginName)
Definition: PluginManager.h:31
bool IsRemote() const
Definition: Platform.h:443
static llvm::raw_ostream & error(Stream &strm)
Log * GetLogIfAllCategoriesSet(uint32_t mask)
Definition: Logging.cpp:58
static void Initialize()
Definition: Platform.cpp:150
llvm::StringRef GetString() const
A plug-in interface definition class for debugging a process.
Definition: Process.h:362
lldb::ModuleSP GetExecutableModule()
Gets the module for the main executable.
Definition: Target.cpp:1342
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
static lldb::PlatformSP CreateInstance(bool force, const lldb_private::ArchSpec *arch)
static void Initialize()
bool x86GetSupportedArchitectureAtIndex(uint32_t idx, lldb_private::ArchSpec &arch)
lldb_private::Status GetFile(const lldb_private::FileSpec &source, const lldb_private::FileSpec &destination) override
ArchSpec & GetArchitecture()
Definition: ModuleSpec.h:111
#define LLDB_LOGF(log,...)
Definition: Log.h:249
lldb_private::ConstString GetSDKDirectory(lldb_private::Target &target) override
PlatformMacOSX(bool is_host)
Default Constructor.
A uniqued constant string class.
Definition: ConstString.h:40
bool Fail() const
Test for error condition.
Definition: Status.cpp:182
Definition: SBAddress.h:15
bool TripleOSWasSpecified() const
Definition: ArchSpec.h:341
lldb_private::Status GetFileWithUUID(const lldb_private::FileSpec &platform_file, const lldb_private::UUID *uuid_ptr, lldb_private::FileSpec &local_file) override
Locate a file for a platform.
virtual llvm::VersionTuple GetSDKVersion()
Get the SDK OS version this object file was built with.
Definition: ObjectFile.h:577
virtual lldb_private::Status GetSharedModuleWithLocalCache(const lldb_private::ModuleSpec &module_spec, lldb::ModuleSP &module_sp, const lldb_private::FileSpecList *module_search_paths_ptr, lldb::ModuleSP *old_module_sp_ptr, bool *did_create_ptr)
~PlatformMacOSX() override
Destructor.
static void Terminate()
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
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, lldb::ModuleSP *old_module_sp_ptr, bool *did_create_ptr) override
bool GetSupportedArchitectureAtIndex(uint32_t idx, lldb_private::ArchSpec &arch) override
Get the platform&#39;s supported architectures in the order in which they should be searched.
An error handling class.
Definition: Status.h:44