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 /// Destructor. 175 /// 176 /// The destructor is virtual since this class is designed to be 177 /// inherited from by the plug-in instance. 178 PlatformMacOSX::~PlatformMacOSX() {} 179 180 ConstString PlatformMacOSX::GetSDKDirectory(lldb_private::Target &target) { 181 ModuleSP exe_module_sp(target.GetExecutableModule()); 182 if (!exe_module_sp) 183 return {}; 184 185 ObjectFile *objfile = exe_module_sp->GetObjectFile(); 186 if (!objfile) 187 return {}; 188 189 llvm::VersionTuple version = objfile->GetSDKVersion(); 190 if (version.empty()) 191 return {}; 192 193 // First try to find an SDK that matches the given SDK version. 194 if (FileSpec fspec = HostInfo::GetXcodeContentsDirectory()) { 195 StreamString sdk_path; 196 sdk_path.Printf("%s/Developer/Platforms/MacOSX.platform/Developer/" 197 "SDKs/MacOSX%u.%u.sdk", 198 fspec.GetPath().c_str(), version.getMajor(), 199 version.getMinor().getValue()); 200 if (FileSystem::Instance().Exists(fspec)) 201 return ConstString(sdk_path.GetString()); 202 } 203 204 // Use the default SDK as a fallback. 205 FileSpec fspec( 206 HostInfo::GetXcodeSDKPath(lldb_private::XcodeSDK::GetAnyMacOS())); 207 if (fspec) { 208 if (FileSystem::Instance().Exists(fspec)) 209 return ConstString(fspec.GetPath()); 210 } 211 212 return {}; 213 } 214 215 Status PlatformMacOSX::GetSymbolFile(const FileSpec &platform_file, 216 const UUID *uuid_ptr, 217 FileSpec &local_file) { 218 if (IsRemote()) { 219 if (m_remote_platform_sp) 220 return m_remote_platform_sp->GetFileWithUUID(platform_file, uuid_ptr, 221 local_file); 222 } 223 224 // Default to the local case 225 local_file = platform_file; 226 return Status(); 227 } 228 229 lldb_private::Status 230 PlatformMacOSX::GetFileWithUUID(const lldb_private::FileSpec &platform_file, 231 const lldb_private::UUID *uuid_ptr, 232 lldb_private::FileSpec &local_file) { 233 if (IsRemote() && m_remote_platform_sp) { 234 std::string local_os_build; 235 #if !defined(__linux__) 236 HostInfo::GetOSBuildString(local_os_build); 237 #endif 238 std::string remote_os_build; 239 m_remote_platform_sp->GetOSBuildString(remote_os_build); 240 if (local_os_build == remote_os_build) { 241 // same OS version: the local file is good enough 242 local_file = platform_file; 243 return Status(); 244 } else { 245 // try to find the file in the cache 246 std::string cache_path(GetLocalCacheDirectory()); 247 std::string module_path(platform_file.GetPath()); 248 cache_path.append(module_path); 249 FileSpec module_cache_spec(cache_path); 250 if (FileSystem::Instance().Exists(module_cache_spec)) { 251 local_file = module_cache_spec; 252 return Status(); 253 } 254 // bring in the remote module file 255 FileSpec module_cache_folder = 256 module_cache_spec.CopyByRemovingLastPathComponent(); 257 // try to make the local directory first 258 Status err( 259 llvm::sys::fs::create_directory(module_cache_folder.GetPath())); 260 if (err.Fail()) 261 return err; 262 err = GetFile(platform_file, module_cache_spec); 263 if (err.Fail()) 264 return err; 265 if (FileSystem::Instance().Exists(module_cache_spec)) { 266 local_file = module_cache_spec; 267 return Status(); 268 } else 269 return Status("unable to obtain valid module file"); 270 } 271 } 272 local_file = platform_file; 273 return Status(); 274 } 275 276 bool PlatformMacOSX::GetSupportedArchitectureAtIndex(uint32_t idx, 277 ArchSpec &arch) { 278 #if defined(__arm__) || defined(__arm64__) || defined(__aarch64__) 279 // macOS for ARM64 support both native and translated x86_64 processes 280 if (!m_num_arm_arches || idx < m_num_arm_arches) { 281 bool res = ARMGetSupportedArchitectureAtIndex(idx, arch); 282 if (res) 283 return true; 284 if (!m_num_arm_arches) 285 m_num_arm_arches = idx; 286 } 287 288 // We can't use x86GetSupportedArchitectureAtIndex() because it uses 289 // the system architecture for some of its return values and also 290 // has a 32bits variant. 291 if (idx == m_num_arm_arches) { 292 arch.SetTriple("x86_64-apple-macosx"); 293 return true; 294 } else if (idx == m_num_arm_arches + 1) { 295 arch.SetTriple("x86_64-apple-ios-macabi"); 296 return true; 297 } else if (idx == m_num_arm_arches + 2) { 298 arch.SetTriple("arm64-apple-ios"); 299 return true; 300 } else if (idx == m_num_arm_arches + 3) { 301 arch.SetTriple("arm64e-apple-ios"); 302 return true; 303 } 304 305 return false; 306 #else 307 return x86GetSupportedArchitectureAtIndex(idx, arch); 308 #endif 309 } 310 311 lldb_private::Status PlatformMacOSX::GetSharedModule( 312 const lldb_private::ModuleSpec &module_spec, Process *process, 313 lldb::ModuleSP &module_sp, 314 const lldb_private::FileSpecList *module_search_paths_ptr, 315 lldb::ModuleSP *old_module_sp_ptr, bool *did_create_ptr) { 316 Status error = GetSharedModuleWithLocalCache( 317 module_spec, module_sp, module_search_paths_ptr, old_module_sp_ptr, 318 did_create_ptr); 319 320 if (module_sp) { 321 if (module_spec.GetArchitecture().GetCore() == 322 ArchSpec::eCore_x86_64_x86_64h) { 323 ObjectFile *objfile = module_sp->GetObjectFile(); 324 if (objfile == nullptr) { 325 // We didn't find an x86_64h slice, fall back to a x86_64 slice 326 ModuleSpec module_spec_x86_64(module_spec); 327 module_spec_x86_64.GetArchitecture() = ArchSpec("x86_64-apple-macosx"); 328 lldb::ModuleSP x86_64_module_sp; 329 lldb::ModuleSP old_x86_64_module_sp; 330 bool did_create = false; 331 Status x86_64_error = GetSharedModuleWithLocalCache( 332 module_spec_x86_64, x86_64_module_sp, module_search_paths_ptr, 333 &old_x86_64_module_sp, &did_create); 334 if (x86_64_module_sp && x86_64_module_sp->GetObjectFile()) { 335 module_sp = x86_64_module_sp; 336 if (old_module_sp_ptr) 337 *old_module_sp_ptr = old_x86_64_module_sp; 338 if (did_create_ptr) 339 *did_create_ptr = did_create; 340 return x86_64_error; 341 } 342 } 343 } 344 } 345 346 if (!module_sp) { 347 error = FindBundleBinaryInExecSearchPaths (module_spec, process, module_sp, module_search_paths_ptr, old_module_sp_ptr, did_create_ptr); 348 } 349 return error; 350 } 351