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() : PlatformDarwin(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 bool PlatformMacOSX::GetSupportedArchitectureAtIndex(uint32_t idx, 137 ArchSpec &arch) { 138 #if defined(__arm__) || defined(__arm64__) || defined(__aarch64__) 139 // macOS for ARM64 support both native and translated x86_64 processes 140 if (!m_num_arm_arches || idx < m_num_arm_arches) { 141 bool res = ARMGetSupportedArchitectureAtIndex(idx, arch); 142 if (res) 143 return true; 144 if (!m_num_arm_arches) 145 m_num_arm_arches = idx; 146 } 147 148 // We can't use x86GetSupportedArchitectureAtIndex() because it uses 149 // the system architecture for some of its return values and also 150 // has a 32bits variant. 151 if (idx == m_num_arm_arches) { 152 arch.SetTriple("x86_64-apple-macosx"); 153 return true; 154 } else if (idx == m_num_arm_arches + 1) { 155 arch.SetTriple("x86_64-apple-ios-macabi"); 156 return true; 157 } else if (idx == m_num_arm_arches + 2) { 158 arch.SetTriple("arm64-apple-ios"); 159 return true; 160 } else if (idx == m_num_arm_arches + 3) { 161 arch.SetTriple("arm64e-apple-ios"); 162 return true; 163 } 164 165 return false; 166 #else 167 return x86GetSupportedArchitectureAtIndex(idx, arch); 168 #endif 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