xref: /openbsd-src/gnu/llvm/lldb/source/Plugins/Platform/MacOSX/PlatformMacOSX.cpp (revision 24bb5fcea3ed904bc467217bdaadb5dfc618d5bf)
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 "PlatformAppleTVSimulator.h"
13 #include "PlatformAppleWatchSimulator.h"
14 #include "PlatformDarwinKernel.h"
15 #include "PlatformRemoteAppleBridge.h"
16 #include "PlatformRemoteAppleTV.h"
17 #include "PlatformRemoteAppleWatch.h"
18 #include "PlatformiOSSimulator.h"
19 #endif
20 #include "lldb/Breakpoint/BreakpointLocation.h"
21 #include "lldb/Core/Module.h"
22 #include "lldb/Core/ModuleList.h"
23 #include "lldb/Core/ModuleSpec.h"
24 #include "lldb/Core/PluginManager.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"
31 #include "lldb/Utility/DataBufferHeap.h"
32 #include "lldb/Utility/FileSpec.h"
33 #include "lldb/Utility/Log.h"
34 #include "lldb/Utility/Status.h"
35 #include "lldb/Utility/StreamString.h"
36 
37 #include <sstream>
38 
39 using namespace lldb;
40 using namespace lldb_private;
41 
42 LLDB_PLUGIN_DEFINE(PlatformMacOSX)
43 
44 static uint32_t g_initialize_count = 0;
45 
46 void PlatformMacOSX::Initialize() {
47   PlatformDarwin::Initialize();
48   PlatformRemoteiOS::Initialize();
49 #if defined(__APPLE__)
50   PlatformiOSSimulator::Initialize();
51   PlatformDarwinKernel::Initialize();
52   PlatformAppleTVSimulator::Initialize();
53   PlatformAppleWatchSimulator::Initialize();
54   PlatformRemoteAppleTV::Initialize();
55   PlatformRemoteAppleWatch::Initialize();
56   PlatformRemoteAppleBridge::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),
66                                   PlatformMacOSX::GetDescriptionStatic(false),
67                                   PlatformMacOSX::CreateInstance);
68   }
69 }
70 
71 void PlatformMacOSX::Terminate() {
72   if (g_initialize_count > 0) {
73     if (--g_initialize_count == 0) {
74       PluginManager::UnregisterPlugin(PlatformMacOSX::CreateInstance);
75     }
76   }
77 
78 #if defined(__APPLE__)
79   PlatformRemoteAppleBridge::Terminate();
80   PlatformRemoteAppleWatch::Terminate();
81   PlatformRemoteAppleTV::Terminate();
82   PlatformAppleWatchSimulator::Terminate();
83   PlatformAppleTVSimulator::Terminate();
84   PlatformDarwinKernel::Terminate();
85   PlatformiOSSimulator::Terminate();
86 #endif
87   PlatformRemoteiOS::Terminate();
88   PlatformDarwin::Terminate();
89 }
90 
91 PlatformSP PlatformMacOSX::CreateInstance(bool force, const ArchSpec *arch) {
92   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
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 
160 lldb_private::ConstString PlatformMacOSX::GetPluginNameStatic(bool is_host) {
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
178 PlatformMacOSX::PlatformMacOSX(bool is_host) : PlatformDarwin(is_host) {}
179 
180 /// Destructor.
181 ///
182 /// The destructor is virtual since this class is designed to be
183 /// inherited from by the plug-in instance.
184 PlatformMacOSX::~PlatformMacOSX() {}
185 
186 ConstString PlatformMacOSX::GetSDKDirectory(lldb_private::Target &target) {
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 
221 Status PlatformMacOSX::GetSymbolFile(const FileSpec &platform_file,
222                                      const UUID *uuid_ptr,
223                                      FileSpec &local_file) {
224   if (IsRemote()) {
225     if (m_remote_platform_sp)
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 
235 lldb_private::Status
236 PlatformMacOSX::GetFileWithUUID(const lldb_private::FileSpec &platform_file,
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 
282 bool PlatformMacOSX::GetSupportedArchitectureAtIndex(uint32_t idx,
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 
291 lldb_private::Status PlatformMacOSX::GetSharedModule(
292     const lldb_private::ModuleSpec &module_spec, Process *process,
293     lldb::ModuleSP &module_sp,
294     const lldb_private::FileSpecList *module_search_paths_ptr,
295     llvm::SmallVectorImpl<lldb::ModuleSP> *old_modules, bool *did_create_ptr) {
296   Status error = GetSharedModuleWithLocalCache(module_spec, module_sp,
297                                                module_search_paths_ptr,
298                                                old_modules, 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         llvm::SmallVector<lldb::ModuleSP, 1> old_x86_64_modules;
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_modules, &did_create);
314         if (x86_64_module_sp && x86_64_module_sp->GetObjectFile()) {
315           module_sp = x86_64_module_sp;
316           if (old_modules)
317             old_modules->append(old_x86_64_modules.begin(),
318                                 old_x86_64_modules.end());
319           if (did_create_ptr)
320             *did_create_ptr = did_create;
321           return x86_64_error;
322         }
323       }
324     }
325   }
326 
327   if (!module_sp) {
328     error = FindBundleBinaryInExecSearchPaths(module_spec, process, module_sp,
329                                               module_search_paths_ptr,
330                                               old_modules, did_create_ptr);
331   }
332   return error;
333 }
334