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__)
12 #include "PlatformAppleSimulator.h"
13 #include "PlatformDarwinKernel.h"
14 #include "PlatformRemoteAppleBridge.h"
15 #include "PlatformRemoteAppleTV.h"
16 #include "PlatformRemoteAppleWatch.h"
17 #endif
18 #include "lldb/Breakpoint/BreakpointLocation.h"
19 #include "lldb/Core/Module.h"
20 #include "lldb/Core/ModuleList.h"
21 #include "lldb/Core/ModuleSpec.h"
22 #include "lldb/Core/PluginManager.h"
23 #include "lldb/Host/Config.h"
24 #include "lldb/Host/Host.h"
25 #include "lldb/Host/HostInfo.h"
26 #include "lldb/Symbol/ObjectFile.h"
27 #include "lldb/Target/Process.h"
28 #include "lldb/Target/Target.h"
29 #include "lldb/Utility/DataBufferHeap.h"
30 #include "lldb/Utility/FileSpec.h"
31 #include "lldb/Utility/Log.h"
32 #include "lldb/Utility/Status.h"
33 #include "lldb/Utility/StreamString.h"
34 
35 #include <sstream>
36 
37 using namespace lldb;
38 using namespace lldb_private;
39 
40 LLDB_PLUGIN_DEFINE(PlatformMacOSX)
41 
42 static uint32_t g_initialize_count = 0;
43 
44 void PlatformMacOSX::Initialize() {
45   PlatformDarwin::Initialize();
46   PlatformRemoteiOS::Initialize();
47 #if defined(__APPLE__)
48   PlatformAppleSimulator::Initialize();
49   PlatformDarwinKernel::Initialize();
50   PlatformRemoteAppleTV::Initialize();
51   PlatformRemoteAppleWatch::Initialize();
52   PlatformRemoteAppleBridge::Initialize();
53 #endif
54 
55   if (g_initialize_count++ == 0) {
56 #if defined(__APPLE__)
57     PlatformSP default_platform_sp(new PlatformMacOSX(true));
58     default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture());
59     Platform::SetHostPlatform(default_platform_sp);
60 #endif
61     PluginManager::RegisterPlugin(PlatformMacOSX::GetPluginNameStatic(false),
62                                   PlatformMacOSX::GetDescriptionStatic(false),
63                                   PlatformMacOSX::CreateInstance);
64   }
65 }
66 
67 void PlatformMacOSX::Terminate() {
68   if (g_initialize_count > 0) {
69     if (--g_initialize_count == 0) {
70       PluginManager::UnregisterPlugin(PlatformMacOSX::CreateInstance);
71     }
72   }
73 
74 #if defined(__APPLE__)
75   PlatformRemoteAppleBridge::Terminate();
76   PlatformRemoteAppleWatch::Terminate();
77   PlatformRemoteAppleTV::Terminate();
78   PlatformDarwinKernel::Terminate();
79   PlatformAppleSimulator::Terminate();
80 #endif
81   PlatformRemoteiOS::Terminate();
82   PlatformDarwin::Terminate();
83 }
84 
85 PlatformSP PlatformMacOSX::CreateInstance(bool force, const ArchSpec *arch) {
86   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
87   if (log) {
88     const char *arch_name;
89     if (arch && arch->GetArchitectureName())
90       arch_name = arch->GetArchitectureName();
91     else
92       arch_name = "<null>";
93 
94     const char *triple_cstr =
95         arch ? arch->GetTriple().getTriple().c_str() : "<null>";
96 
97     LLDB_LOGF(log, "PlatformMacOSX::%s(force=%s, arch={%s,%s})", __FUNCTION__,
98               force ? "true" : "false", arch_name, triple_cstr);
99   }
100 
101   // The only time we create an instance is when we are creating a remote
102   // macosx platform
103   const bool is_host = false;
104 
105   bool create = force;
106   if (!create && arch && arch->IsValid()) {
107     const llvm::Triple &triple = arch->GetTriple();
108     switch (triple.getVendor()) {
109     case llvm::Triple::Apple:
110       create = true;
111       break;
112 
113 #if defined(__APPLE__)
114     // Only accept "unknown" for vendor if the host is Apple and it "unknown"
115     // wasn't specified (it was just returned because it was NOT specified)
116     case llvm::Triple::UnknownVendor:
117       create = !arch->TripleVendorWasSpecified();
118       break;
119 #endif
120     default:
121       break;
122     }
123 
124     if (create) {
125       switch (triple.getOS()) {
126       case llvm::Triple::Darwin: // Deprecated, but still support Darwin for
127                                  // historical reasons
128       case llvm::Triple::MacOSX:
129         break;
130 #if defined(__APPLE__)
131       // Only accept "vendor" for vendor if the host is Apple and it "unknown"
132       // wasn't specified (it was just returned because it was NOT specified)
133       case llvm::Triple::UnknownOS:
134         create = !arch->TripleOSWasSpecified();
135         break;
136 #endif
137       default:
138         create = false;
139         break;
140       }
141     }
142   }
143   if (create) {
144     LLDB_LOGF(log, "PlatformMacOSX::%s() creating platform", __FUNCTION__);
145     return PlatformSP(new PlatformMacOSX(is_host));
146   }
147 
148   LLDB_LOGF(log, "PlatformMacOSX::%s() aborting creation of platform",
149             __FUNCTION__);
150 
151   return PlatformSP();
152 }
153 
154 lldb_private::ConstString PlatformMacOSX::GetPluginNameStatic(bool is_host) {
155   if (is_host) {
156     static ConstString g_host_name(Platform::GetHostPlatformName());
157     return g_host_name;
158   } else {
159     static ConstString g_remote_name("remote-macosx");
160     return g_remote_name;
161   }
162 }
163 
164 const char *PlatformMacOSX::GetDescriptionStatic(bool is_host) {
165   if (is_host)
166     return "Local Mac OS X user platform plug-in.";
167   else
168     return "Remote Mac OS X user platform plug-in.";
169 }
170 
171 /// Default Constructor
172 PlatformMacOSX::PlatformMacOSX(bool is_host) : PlatformDarwin(is_host) {}
173 
174 ConstString PlatformMacOSX::GetSDKDirectory(lldb_private::Target &target) {
175   ModuleSP exe_module_sp(target.GetExecutableModule());
176   if (!exe_module_sp)
177     return {};
178 
179   ObjectFile *objfile = exe_module_sp->GetObjectFile();
180   if (!objfile)
181     return {};
182 
183   llvm::VersionTuple version = objfile->GetSDKVersion();
184   if (version.empty())
185     return {};
186 
187   // First try to find an SDK that matches the given SDK version.
188   if (FileSpec fspec = HostInfo::GetXcodeContentsDirectory()) {
189     StreamString sdk_path;
190     sdk_path.Printf("%s/Developer/Platforms/MacOSX.platform/Developer/"
191                     "SDKs/MacOSX%u.%u.sdk",
192                     fspec.GetPath().c_str(), version.getMajor(),
193                     version.getMinor().getValue());
194     if (FileSystem::Instance().Exists(fspec))
195       return ConstString(sdk_path.GetString());
196   }
197 
198   // Use the default SDK as a fallback.
199   FileSpec fspec(
200       HostInfo::GetXcodeSDKPath(lldb_private::XcodeSDK::GetAnyMacOS()));
201   if (fspec) {
202     if (FileSystem::Instance().Exists(fspec))
203       return ConstString(fspec.GetPath());
204   }
205 
206   return {};
207 }
208 
209 Status PlatformMacOSX::GetSymbolFile(const FileSpec &platform_file,
210                                      const UUID *uuid_ptr,
211                                      FileSpec &local_file) {
212   if (IsRemote()) {
213     if (m_remote_platform_sp)
214       return m_remote_platform_sp->GetFileWithUUID(platform_file, uuid_ptr,
215                                                    local_file);
216   }
217 
218   // Default to the local case
219   local_file = platform_file;
220   return Status();
221 }
222 
223 lldb_private::Status
224 PlatformMacOSX::GetFileWithUUID(const lldb_private::FileSpec &platform_file,
225                                 const lldb_private::UUID *uuid_ptr,
226                                 lldb_private::FileSpec &local_file) {
227   if (IsRemote() && m_remote_platform_sp) {
228     std::string local_os_build;
229 #if !defined(__linux__)
230     HostInfo::GetOSBuildString(local_os_build);
231 #endif
232     std::string remote_os_build;
233     m_remote_platform_sp->GetOSBuildString(remote_os_build);
234     if (local_os_build == remote_os_build) {
235       // same OS version: the local file is good enough
236       local_file = platform_file;
237       return Status();
238     } else {
239       // try to find the file in the cache
240       std::string cache_path(GetLocalCacheDirectory());
241       std::string module_path(platform_file.GetPath());
242       cache_path.append(module_path);
243       FileSpec module_cache_spec(cache_path);
244       if (FileSystem::Instance().Exists(module_cache_spec)) {
245         local_file = module_cache_spec;
246         return Status();
247       }
248       // bring in the remote module file
249       FileSpec module_cache_folder =
250           module_cache_spec.CopyByRemovingLastPathComponent();
251       // try to make the local directory first
252       Status err(
253           llvm::sys::fs::create_directory(module_cache_folder.GetPath()));
254       if (err.Fail())
255         return err;
256       err = GetFile(platform_file, module_cache_spec);
257       if (err.Fail())
258         return err;
259       if (FileSystem::Instance().Exists(module_cache_spec)) {
260         local_file = module_cache_spec;
261         return Status();
262       } else
263         return Status("unable to obtain valid module file");
264     }
265   }
266   local_file = platform_file;
267   return Status();
268 }
269 
270 bool PlatformMacOSX::GetSupportedArchitectureAtIndex(uint32_t idx,
271                                                      ArchSpec &arch) {
272 #if defined(__arm__) || defined(__arm64__) || defined(__aarch64__)
273   // macOS for ARM64 support both native and translated x86_64 processes
274   if (!m_num_arm_arches || idx < m_num_arm_arches) {
275     bool res = ARMGetSupportedArchitectureAtIndex(idx, arch);
276     if (res)
277       return true;
278     if (!m_num_arm_arches)
279       m_num_arm_arches = idx;
280   }
281 
282   // We can't use x86GetSupportedArchitectureAtIndex() because it uses
283   // the system architecture for some of its return values and also
284   // has a 32bits variant.
285   if (idx == m_num_arm_arches) {
286     arch.SetTriple("x86_64-apple-macosx");
287     return true;
288   } else if (idx == m_num_arm_arches + 1) {
289     arch.SetTriple("x86_64-apple-ios-macabi");
290     return true;
291   } else if (idx == m_num_arm_arches + 2) {
292     arch.SetTriple("arm64-apple-ios");
293     return true;
294   } else if (idx == m_num_arm_arches + 3) {
295     arch.SetTriple("arm64e-apple-ios");
296     return true;
297   }
298 
299   return false;
300 #else
301   return x86GetSupportedArchitectureAtIndex(idx, arch);
302 #endif
303 }
304 
305 lldb_private::Status PlatformMacOSX::GetSharedModule(
306     const lldb_private::ModuleSpec &module_spec, Process *process,
307     lldb::ModuleSP &module_sp,
308     const lldb_private::FileSpecList *module_search_paths_ptr,
309     llvm::SmallVectorImpl<lldb::ModuleSP> *old_modules, bool *did_create_ptr) {
310   Status error = GetSharedModuleWithLocalCache(module_spec, module_sp,
311                                                module_search_paths_ptr,
312                                                old_modules, did_create_ptr);
313 
314   if (module_sp) {
315     if (module_spec.GetArchitecture().GetCore() ==
316         ArchSpec::eCore_x86_64_x86_64h) {
317       ObjectFile *objfile = module_sp->GetObjectFile();
318       if (objfile == nullptr) {
319         // We didn't find an x86_64h slice, fall back to a x86_64 slice
320         ModuleSpec module_spec_x86_64(module_spec);
321         module_spec_x86_64.GetArchitecture() = ArchSpec("x86_64-apple-macosx");
322         lldb::ModuleSP x86_64_module_sp;
323         llvm::SmallVector<lldb::ModuleSP, 1> old_x86_64_modules;
324         bool did_create = false;
325         Status x86_64_error = GetSharedModuleWithLocalCache(
326             module_spec_x86_64, x86_64_module_sp, module_search_paths_ptr,
327             &old_x86_64_modules, &did_create);
328         if (x86_64_module_sp && x86_64_module_sp->GetObjectFile()) {
329           module_sp = x86_64_module_sp;
330           if (old_modules)
331             old_modules->append(old_x86_64_modules.begin(),
332                                 old_x86_64_modules.end());
333           if (did_create_ptr)
334             *did_create_ptr = did_create;
335           return x86_64_error;
336         }
337       }
338     }
339   }
340 
341   if (!module_sp) {
342     error = FindBundleBinaryInExecSearchPaths(module_spec, process, module_sp,
343                                               module_search_paths_ptr,
344                                               old_modules, did_create_ptr);
345   }
346   return error;
347 }
348