LLDB  mainline
PlatformMacOSX.cpp
Go to the documentation of this file.
1 //===-- PlatformMacOSX.cpp --------------------------------------*- C++ -*-===//
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 "lldb/Host/Config.h"
11 
12 
13 #include <sstream>
14 
16 #include "lldb/Core/Module.h"
17 #include "lldb/Core/ModuleList.h"
18 #include "lldb/Core/ModuleSpec.h"
20 #include "lldb/Host/Host.h"
21 #include "lldb/Host/HostInfo.h"
22 #include "lldb/Symbol/ObjectFile.h"
23 #include "lldb/Target/Process.h"
24 #include "lldb/Target/Target.h"
26 #include "lldb/Utility/FileSpec.h"
27 #include "lldb/Utility/Log.h"
28 #include "lldb/Utility/Status.h"
30 
31 using namespace lldb;
32 using namespace lldb_private;
33 
35 
38 
39  if (g_initialize_count++ == 0) {
40 #if defined(__APPLE__)
41  PlatformSP default_platform_sp(new PlatformMacOSX(true));
42  default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture());
43  Platform::SetHostPlatform(default_platform_sp);
44 #endif
45  PluginManager::RegisterPlugin(PlatformMacOSX::GetPluginNameStatic(false),
48  }
49 }
50 
52  if (g_initialize_count > 0) {
53  if (--g_initialize_count == 0) {
54  PluginManager::UnregisterPlugin(PlatformMacOSX::CreateInstance);
55  }
56  }
57 
59 }
60 
61 PlatformSP PlatformMacOSX::CreateInstance(bool force, const ArchSpec *arch) {
63  if (log) {
64  const char *arch_name;
65  if (arch && arch->GetArchitectureName())
66  arch_name = arch->GetArchitectureName();
67  else
68  arch_name = "<null>";
69 
70  const char *triple_cstr =
71  arch ? arch->GetTriple().getTriple().c_str() : "<null>";
72 
73  log->Printf("PlatformMacOSX::%s(force=%s, arch={%s,%s})", __FUNCTION__,
74  force ? "true" : "false", arch_name, triple_cstr);
75  }
76 
77  // The only time we create an instance is when we are creating a remote
78  // macosx platform
79  const bool is_host = false;
80 
81  bool create = force;
82  if (!create && arch && arch->IsValid()) {
83  const llvm::Triple &triple = arch->GetTriple();
84  switch (triple.getVendor()) {
85  case llvm::Triple::Apple:
86  create = true;
87  break;
88 
89 #if defined(__APPLE__)
90  // Only accept "unknown" for vendor if the host is Apple and it "unknown"
91  // wasn't specified (it was just returned because it was NOT specified)
92  case llvm::Triple::UnknownVendor:
93  create = !arch->TripleVendorWasSpecified();
94  break;
95 #endif
96  default:
97  break;
98  }
99 
100  if (create) {
101  switch (triple.getOS()) {
102  case llvm::Triple::Darwin: // Deprecated, but still support Darwin for
103  // historical reasons
104  case llvm::Triple::MacOSX:
105  break;
106 #if defined(__APPLE__)
107  // Only accept "vendor" for vendor if the host is Apple and it "unknown"
108  // wasn't specified (it was just returned because it was NOT specified)
109  case llvm::Triple::UnknownOS:
110  create = !arch->TripleOSWasSpecified();
111  break;
112 #endif
113  default:
114  create = false;
115  break;
116  }
117  }
118  }
119  if (create) {
120  if (log)
121  log->Printf("PlatformMacOSX::%s() creating platform", __FUNCTION__);
122  return PlatformSP(new PlatformMacOSX(is_host));
123  }
124 
125  if (log)
126  log->Printf("PlatformMacOSX::%s() aborting creation of platform",
127  __FUNCTION__);
128 
129  return PlatformSP();
130 }
131 
133  if (is_host) {
134  static ConstString g_host_name(Platform::GetHostPlatformName());
135  return g_host_name;
136  } else {
137  static ConstString g_remote_name("remote-macosx");
138  return g_remote_name;
139  }
140 }
141 
142 const char *PlatformMacOSX::GetDescriptionStatic(bool is_host) {
143  if (is_host)
144  return "Local Mac OS X user platform plug-in.";
145  else
146  return "Remote Mac OS X user platform plug-in.";
147 }
148 
149 /// Default Constructor
151 
152 /// Destructor.
153 ///
154 /// The destructor is virtual since this class is designed to be
155 /// inherited from by the plug-in instance.
157 
159  ModuleSP exe_module_sp(target.GetExecutableModule());
160  if (exe_module_sp) {
161  ObjectFile *objfile = exe_module_sp->GetObjectFile();
162  if (objfile) {
163  std::string xcode_contents_path;
164  std::string default_xcode_sdk;
165  FileSpec fspec;
166  uint32_t versions[2];
167  if (objfile->GetSDKVersion(versions, 2)) {
168  fspec = HostInfo::GetShlibDir();
169  if (fspec) {
170  std::string path;
171  xcode_contents_path = fspec.GetPath();
172  size_t pos = xcode_contents_path.find("/Xcode.app/Contents/");
173  if (pos != std::string::npos) {
174  // LLDB.framework is inside an Xcode app bundle, we can locate the
175  // SDK from here
176  xcode_contents_path.erase(pos + strlen("/Xcode.app/Contents/"));
177  } else {
178  xcode_contents_path.clear();
179  // Use the selected Xcode
180  int status = 0;
181  int signo = 0;
182  std::string output;
183  const char *command = "xcrun -sdk macosx --show-sdk-path";
185  command, // shell command to run
186  NULL, // current working directory
187  &status, // Put the exit status of the process in here
188  &signo, // Put the signal that caused the process to exit in
189  // here
190  &output, // Get the output from the command and place it in this
191  // string
192  std::chrono::seconds(3));
193  if (status == 0 && !output.empty()) {
194  size_t first_non_newline = output.find_last_not_of("\r\n");
195  if (first_non_newline != std::string::npos)
196  output.erase(first_non_newline + 1);
197  default_xcode_sdk = output;
198 
199  pos = default_xcode_sdk.find("/Xcode.app/Contents/");
200  if (pos != std::string::npos)
201  xcode_contents_path = default_xcode_sdk.substr(
202  0, pos + strlen("/Xcode.app/Contents/"));
203  }
204  }
205  }
206 
207  if (!xcode_contents_path.empty()) {
208  StreamString sdk_path;
209  sdk_path.Printf("%sDeveloper/Platforms/MacOSX.platform/Developer/"
210  "SDKs/MacOSX%u.%u.sdk",
211  xcode_contents_path.c_str(), versions[0],
212  versions[1]);
213  fspec.SetFile(sdk_path.GetString(), FileSpec::Style::native);
214  if (FileSystem::Instance().Exists(fspec))
215  return ConstString(sdk_path.GetString());
216  }
217 
218  if (!default_xcode_sdk.empty()) {
219  fspec.SetFile(default_xcode_sdk, FileSpec::Style::native);
220  if (FileSystem::Instance().Exists(fspec))
221  return ConstString(default_xcode_sdk);
222  }
223  }
224  }
225  }
226  return ConstString();
227 }
228 
230  const UUID *uuid_ptr,
231  FileSpec &local_file) {
232  if (IsRemote()) {
234  return m_remote_platform_sp->GetFileWithUUID(platform_file, uuid_ptr,
235  local_file);
236  }
237 
238  // Default to the local case
239  local_file = platform_file;
240  return Status();
241 }
242 
245  const lldb_private::UUID *uuid_ptr,
246  lldb_private::FileSpec &local_file) {
247  if (IsRemote() && m_remote_platform_sp) {
248  std::string local_os_build;
249 #if !defined(__linux__)
250  HostInfo::GetOSBuildString(local_os_build);
251 #endif
252  std::string remote_os_build;
253  m_remote_platform_sp->GetOSBuildString(remote_os_build);
254  if (local_os_build == remote_os_build) {
255  // same OS version: the local file is good enough
256  local_file = platform_file;
257  return Status();
258  } else {
259  // try to find the file in the cache
260  std::string cache_path(GetLocalCacheDirectory());
261  std::string module_path(platform_file.GetPath());
262  cache_path.append(module_path);
263  FileSpec module_cache_spec(cache_path);
264  if (FileSystem::Instance().Exists(module_cache_spec)) {
265  local_file = module_cache_spec;
266  return Status();
267  }
268  // bring in the remote module file
269  FileSpec module_cache_folder =
270  module_cache_spec.CopyByRemovingLastPathComponent();
271  // try to make the local directory first
272  Status err(
273  llvm::sys::fs::create_directory(module_cache_folder.GetPath()));
274  if (err.Fail())
275  return err;
276  err = GetFile(platform_file, module_cache_spec);
277  if (err.Fail())
278  return err;
279  if (FileSystem::Instance().Exists(module_cache_spec)) {
280  local_file = module_cache_spec;
281  return Status();
282  } else
283  return Status("unable to obtain valid module file");
284  }
285  }
286  local_file = platform_file;
287  return Status();
288 }
289 
291  ArchSpec &arch) {
292 #if defined(__arm__) || defined(__arm64__) || defined(__aarch64__)
293  return ARMGetSupportedArchitectureAtIndex(idx, arch);
294 #else
295  return x86GetSupportedArchitectureAtIndex(idx, arch);
296 #endif
297 }
298 
300  const lldb_private::ModuleSpec &module_spec, Process *process,
301  lldb::ModuleSP &module_sp,
302  const lldb_private::FileSpecList *module_search_paths_ptr,
303  lldb::ModuleSP *old_module_sp_ptr, bool *did_create_ptr) {
305  module_spec, module_sp, module_search_paths_ptr, old_module_sp_ptr,
306  did_create_ptr);
307 
308  if (module_sp) {
309  if (module_spec.GetArchitecture().GetCore() ==
310  ArchSpec::eCore_x86_64_x86_64h) {
311  ObjectFile *objfile = module_sp->GetObjectFile();
312  if (objfile == NULL) {
313  // We didn't find an x86_64h slice, fall back to a x86_64 slice
314  ModuleSpec module_spec_x86_64(module_spec);
315  module_spec_x86_64.GetArchitecture() = ArchSpec("x86_64-apple-macosx");
316  lldb::ModuleSP x86_64_module_sp;
317  lldb::ModuleSP old_x86_64_module_sp;
318  bool did_create = false;
319  Status x86_64_error = GetSharedModuleWithLocalCache(
320  module_spec_x86_64, x86_64_module_sp, module_search_paths_ptr,
321  &old_x86_64_module_sp, &did_create);
322  if (x86_64_module_sp && x86_64_module_sp->GetObjectFile()) {
323  module_sp = x86_64_module_sp;
324  if (old_module_sp_ptr)
325  *old_module_sp_ptr = old_x86_64_module_sp;
326  if (did_create_ptr)
327  *did_create_ptr = did_create;
328  return x86_64_error;
329  }
330  }
331  }
332  }
333 
334  if (!module_sp) {
335  error = FindBundleBinaryInExecSearchPaths (module_spec, process, module_sp, module_search_paths_ptr, old_module_sp_ptr, did_create_ptr);
336  }
337  return error;
338 }
Core GetCore() const
Definition: ArchSpec.h:410
virtual const char * GetLocalCacheDirectory()
Definition: Platform.cpp:1361
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
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:55
An architecture specification class.
Definition: ArchSpec.h:32
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 void Terminate()
Definition: Platform.cpp:142
bool IsValid() const
Tests if this ArchSpec is valid.
Definition: ArchSpec.h:329
static uint32_t g_initialize_count
llvm::Triple & GetTriple()
Architecture triple accessor.
Definition: ArchSpec.h:431
bool TripleVendorWasSpecified() const
Definition: ArchSpec.h:334
FileSpec CopyByRemovingLastPathComponent() const
Definition: FileSpec.cpp:434
const char * GetArchitectureName() const
Returns a static string representing the current architecture.
Definition: ArchSpec.cpp:591
bool IsRemote() const
Definition: Platform.h:454
static void Initialize()
Log * GetLogIfAllCategoriesSet(uint32_t mask)
Definition: Logging.cpp:57
static void Initialize()
Definition: Platform.cpp:140
llvm::StringRef GetString() const
A plug-in interface definition class for debugging a process.
Definition: Process.h:353
lldb::ModuleSP GetExecutableModule()
Gets the module for the main executable.
Definition: Target.cpp:1380
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:106
static lldb::PlatformSP CreateInstance(bool force, const lldb_private::ArchSpec *arch)
bool x86GetSupportedArchitectureAtIndex(uint32_t idx, lldb_private::ArchSpec &arch)
lldb_private::Status GetFile(const lldb_private::FileSpec &source, const lldb_private::FileSpec &destination) override
virtual uint32_t GetSDKVersion(uint32_t *versions, uint32_t num_versions)
Get the SDK OS version this object file was built with.
Definition: ObjectFile.h:583
ArchSpec & GetArchitecture()
Definition: ModuleSpec.h:111
lldb_private::ConstString GetSDKDirectory(lldb_private::Target &target) override
PlatformMacOSX(bool is_host)
Default Constructor.
A uniqued constant string class.
Definition: ConstString.h:38
bool Fail() const
Test for error condition.
Definition: Status.cpp:181
Definition: SBAddress.h:15
bool TripleOSWasSpecified() const
Definition: ArchSpec.h:338
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 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.
void SetFile(llvm::StringRef path, Style style)
Change the file specified with a new path.
Definition: FileSpec.cpp:198
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition: FileSpec.cpp:376
void Printf(const char *format,...) __attribute__((format(printf
Definition: Log.cpp:113
Status RunShellCommand(const char *command, const FileSpec &working_dir, int *status_ptr, int *signo_ptr, std::string *command_output, const Timeout< std::micro > &timeout) override
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