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 "PlatformRemoteMacOSX.h" 11 #include "PlatformRemoteiOS.h" 12 #if defined(__APPLE__) 13 #include "PlatformAppleSimulator.h" 14 #include "PlatformDarwinKernel.h" 15 #include "PlatformRemoteAppleBridge.h" 16 #include "PlatformRemoteAppleTV.h" 17 #include "PlatformRemoteAppleWatch.h" 18 #endif 19 #include "lldb/Breakpoint/BreakpointLocation.h" 20 #include "lldb/Core/Module.h" 21 #include "lldb/Core/ModuleList.h" 22 #include "lldb/Core/ModuleSpec.h" 23 #include "lldb/Core/PluginManager.h" 24 #include "lldb/Host/Config.h" 25 #include "lldb/Host/Host.h" 26 #include "lldb/Host/HostInfo.h" 27 #include "lldb/Symbol/ObjectFile.h" 28 #include "lldb/Target/Process.h" 29 #include "lldb/Target/Target.h" 30 #include "lldb/Utility/DataBufferHeap.h" 31 #include "lldb/Utility/FileSpec.h" 32 #include "lldb/Utility/Log.h" 33 #include "lldb/Utility/Status.h" 34 #include "lldb/Utility/StreamString.h" 35 36 #include <sstream> 37 38 using namespace lldb; 39 using namespace lldb_private; 40 41 LLDB_PLUGIN_DEFINE(PlatformMacOSX) 42 43 static uint32_t g_initialize_count = 0; 44 45 void PlatformMacOSX::Initialize() { 46 PlatformDarwin::Initialize(); 47 PlatformRemoteiOS::Initialize(); 48 PlatformRemoteMacOSX::Initialize(); 49 #if defined(__APPLE__) 50 PlatformAppleSimulator::Initialize(); 51 PlatformDarwinKernel::Initialize(); 52 PlatformRemoteAppleTV::Initialize(); 53 PlatformRemoteAppleWatch::Initialize(); 54 PlatformRemoteAppleBridge::Initialize(); 55 #endif 56 57 if (g_initialize_count++ == 0) { 58 #if defined(__APPLE__) 59 PlatformSP default_platform_sp(new PlatformMacOSX()); 60 default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture()); 61 Platform::SetHostPlatform(default_platform_sp); 62 #endif 63 PluginManager::RegisterPlugin(PlatformMacOSX::GetPluginNameStatic(), 64 PlatformMacOSX::GetDescriptionStatic(), 65 PlatformMacOSX::CreateInstance); 66 } 67 } 68 69 void PlatformMacOSX::Terminate() { 70 if (g_initialize_count > 0) { 71 if (--g_initialize_count == 0) { 72 PluginManager::UnregisterPlugin(PlatformMacOSX::CreateInstance); 73 } 74 } 75 76 #if defined(__APPLE__) 77 PlatformRemoteAppleBridge::Terminate(); 78 PlatformRemoteAppleWatch::Terminate(); 79 PlatformRemoteAppleTV::Terminate(); 80 PlatformDarwinKernel::Terminate(); 81 PlatformAppleSimulator::Terminate(); 82 #endif 83 PlatformRemoteMacOSX::Initialize(); 84 PlatformRemoteiOS::Terminate(); 85 PlatformDarwin::Terminate(); 86 } 87 88 llvm::StringRef PlatformMacOSX::GetDescriptionStatic() { 89 return "Local Mac OS X user platform plug-in."; 90 } 91 92 PlatformSP PlatformMacOSX::CreateInstance(bool force, const ArchSpec *arch) { 93 // The only time we create an instance is when we are creating a remote 94 // macosx platform which is handled by PlatformRemoteMacOSX. 95 return PlatformSP(); 96 } 97 98 /// Default Constructor 99 PlatformMacOSX::PlatformMacOSX() : PlatformDarwinDevice(true) {} 100 101 ConstString PlatformMacOSX::GetSDKDirectory(lldb_private::Target &target) { 102 ModuleSP exe_module_sp(target.GetExecutableModule()); 103 if (!exe_module_sp) 104 return {}; 105 106 ObjectFile *objfile = exe_module_sp->GetObjectFile(); 107 if (!objfile) 108 return {}; 109 110 llvm::VersionTuple version = objfile->GetSDKVersion(); 111 if (version.empty()) 112 return {}; 113 114 // First try to find an SDK that matches the given SDK version. 115 if (FileSpec fspec = HostInfo::GetXcodeContentsDirectory()) { 116 StreamString sdk_path; 117 sdk_path.Printf("%s/Developer/Platforms/MacOSX.platform/Developer/" 118 "SDKs/MacOSX%u.%u.sdk", 119 fspec.GetPath().c_str(), version.getMajor(), 120 version.getMinor().getValue()); 121 if (FileSystem::Instance().Exists(fspec)) 122 return ConstString(sdk_path.GetString()); 123 } 124 125 // Use the default SDK as a fallback. 126 FileSpec fspec( 127 HostInfo::GetXcodeSDKPath(lldb_private::XcodeSDK::GetAnyMacOS())); 128 if (fspec) { 129 if (FileSystem::Instance().Exists(fspec)) 130 return ConstString(fspec.GetPath()); 131 } 132 133 return {}; 134 } 135 136 std::vector<ArchSpec> 137 PlatformMacOSX::GetSupportedArchitectures(const ArchSpec &process_host_arch) { 138 std::vector<ArchSpec> result; 139 #if defined(__arm__) || defined(__arm64__) || defined(__aarch64__) 140 // When cmdline lldb is run on iOS, watchOS, etc, it is still 141 // using "PlatformMacOSX". 142 llvm::Triple::OSType host_os = GetHostOSType(); 143 ARMGetSupportedArchitectures(result, host_os); 144 145 if (host_os == llvm::Triple::MacOSX) { 146 // We can't use x86GetSupportedArchitectures() because it uses 147 // the system architecture for some of its return values and also 148 // has a 32bits variant. 149 result.push_back(ArchSpec("x86_64-apple-macosx")); 150 result.push_back(ArchSpec("x86_64-apple-ios-macabi")); 151 result.push_back(ArchSpec("arm64-apple-ios-macabi")); 152 result.push_back(ArchSpec("arm64e-apple-ios-macabi")); 153 154 // On Apple Silicon, the host platform is compatible with iOS triples to 155 // support unmodified "iPhone and iPad Apps on Apple Silicon Macs". Because 156 // the binaries are identical, we must rely on the host architecture to 157 // tell them apart and mark the host platform as compatible or not. 158 if (!process_host_arch || 159 process_host_arch.GetTriple().getOS() == llvm::Triple::MacOSX) { 160 result.push_back(ArchSpec("arm64-apple-ios")); 161 result.push_back(ArchSpec("arm64e-apple-ios")); 162 } 163 } 164 #else 165 x86GetSupportedArchitectures(result); 166 result.push_back(ArchSpec("x86_64-apple-ios-macabi")); 167 #endif 168 return result; 169 } 170 171 lldb_private::Status PlatformMacOSX::GetSharedModule( 172 const lldb_private::ModuleSpec &module_spec, Process *process, 173 lldb::ModuleSP &module_sp, 174 const lldb_private::FileSpecList *module_search_paths_ptr, 175 llvm::SmallVectorImpl<lldb::ModuleSP> *old_modules, bool *did_create_ptr) { 176 Status error = GetSharedModuleWithLocalCache(module_spec, module_sp, 177 module_search_paths_ptr, 178 old_modules, did_create_ptr); 179 180 if (module_sp) { 181 if (module_spec.GetArchitecture().GetCore() == 182 ArchSpec::eCore_x86_64_x86_64h) { 183 ObjectFile *objfile = module_sp->GetObjectFile(); 184 if (objfile == nullptr) { 185 // We didn't find an x86_64h slice, fall back to a x86_64 slice 186 ModuleSpec module_spec_x86_64(module_spec); 187 module_spec_x86_64.GetArchitecture() = ArchSpec("x86_64-apple-macosx"); 188 lldb::ModuleSP x86_64_module_sp; 189 llvm::SmallVector<lldb::ModuleSP, 1> old_x86_64_modules; 190 bool did_create = false; 191 Status x86_64_error = GetSharedModuleWithLocalCache( 192 module_spec_x86_64, x86_64_module_sp, module_search_paths_ptr, 193 &old_x86_64_modules, &did_create); 194 if (x86_64_module_sp && x86_64_module_sp->GetObjectFile()) { 195 module_sp = x86_64_module_sp; 196 if (old_modules) 197 old_modules->append(old_x86_64_modules.begin(), 198 old_x86_64_modules.end()); 199 if (did_create_ptr) 200 *did_create_ptr = did_create; 201 return x86_64_error; 202 } 203 } 204 } 205 } 206 207 if (!module_sp) { 208 error = FindBundleBinaryInExecSearchPaths(module_spec, process, module_sp, 209 module_search_paths_ptr, 210 old_modules, did_create_ptr); 211 } 212 return error; 213 } 214 215 llvm::StringRef PlatformMacOSX::GetDeviceSupportDirectoryName() { 216 return "macOS DeviceSupport"; 217 } 218 219 llvm::StringRef PlatformMacOSX::GetPlatformName() { return "MacOSX.platform"; } 220