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 #if defined(__APPLE__) 11 #include "PlatformAppleTVSimulator.h" 12 #include "PlatformAppleWatchSimulator.h" 13 #include "PlatformDarwinKernel.h" 14 #include "PlatformRemoteAppleBridge.h" 15 #include "PlatformRemoteAppleTV.h" 16 #include "PlatformRemoteAppleWatch.h" 17 #include "PlatformiOSSimulator.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 static uint32_t g_initialize_count = 0; 42 43 void PlatformMacOSX::Initialize() { 44 PlatformDarwin::Initialize(); 45 #if defined(__APPLE__) 46 PlatformiOSSimulator::Initialize(); 47 PlatformDarwinKernel::Initialize(); 48 PlatformAppleTVSimulator::Initialize(); 49 PlatformAppleWatchSimulator::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 PlatformAppleWatchSimulator::Terminate(); 79 PlatformAppleTVSimulator::Terminate(); 80 PlatformDarwinKernel::Terminate(); 81 PlatformiOSSimulator::Terminate(); 82 #endif 83 PlatformDarwin::Terminate(); 84 } 85 86 PlatformSP PlatformMacOSX::CreateInstance(bool force, const ArchSpec *arch) { 87 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM)); 88 if (log) { 89 const char *arch_name; 90 if (arch && arch->GetArchitectureName()) 91 arch_name = arch->GetArchitectureName(); 92 else 93 arch_name = "<null>"; 94 95 const char *triple_cstr = 96 arch ? arch->GetTriple().getTriple().c_str() : "<null>"; 97 98 LLDB_LOGF(log, "PlatformMacOSX::%s(force=%s, arch={%s,%s})", __FUNCTION__, 99 force ? "true" : "false", arch_name, triple_cstr); 100 } 101 102 // The only time we create an instance is when we are creating a remote 103 // macosx platform 104 const bool is_host = false; 105 106 bool create = force; 107 if (!create && arch && arch->IsValid()) { 108 const llvm::Triple &triple = arch->GetTriple(); 109 switch (triple.getVendor()) { 110 case llvm::Triple::Apple: 111 create = true; 112 break; 113 114 #if defined(__APPLE__) 115 // Only accept "unknown" for vendor if the host is Apple and it "unknown" 116 // wasn't specified (it was just returned because it was NOT specified) 117 case llvm::Triple::UnknownVendor: 118 create = !arch->TripleVendorWasSpecified(); 119 break; 120 #endif 121 default: 122 break; 123 } 124 125 if (create) { 126 switch (triple.getOS()) { 127 case llvm::Triple::Darwin: // Deprecated, but still support Darwin for 128 // historical reasons 129 case llvm::Triple::MacOSX: 130 break; 131 #if defined(__APPLE__) 132 // Only accept "vendor" for vendor if the host is Apple and it "unknown" 133 // wasn't specified (it was just returned because it was NOT specified) 134 case llvm::Triple::UnknownOS: 135 create = !arch->TripleOSWasSpecified(); 136 break; 137 #endif 138 default: 139 create = false; 140 break; 141 } 142 } 143 } 144 if (create) { 145 LLDB_LOGF(log, "PlatformMacOSX::%s() creating platform", __FUNCTION__); 146 return PlatformSP(new PlatformMacOSX(is_host)); 147 } 148 149 LLDB_LOGF(log, "PlatformMacOSX::%s() aborting creation of platform", 150 __FUNCTION__); 151 152 return PlatformSP(); 153 } 154 155 lldb_private::ConstString PlatformMacOSX::GetPluginNameStatic(bool is_host) { 156 if (is_host) { 157 static ConstString g_host_name(Platform::GetHostPlatformName()); 158 return g_host_name; 159 } else { 160 static ConstString g_remote_name("remote-macosx"); 161 return g_remote_name; 162 } 163 } 164 165 const char *PlatformMacOSX::GetDescriptionStatic(bool is_host) { 166 if (is_host) 167 return "Local Mac OS X user platform plug-in."; 168 else 169 return "Remote Mac OS X user platform plug-in."; 170 } 171 172 /// Default Constructor 173 PlatformMacOSX::PlatformMacOSX(bool is_host) : PlatformDarwin(is_host) {} 174 175 /// Destructor. 176 /// 177 /// The destructor is virtual since this class is designed to be 178 /// inherited from by the plug-in instance. 179 PlatformMacOSX::~PlatformMacOSX() {} 180 181 ConstString PlatformMacOSX::GetSDKDirectory(lldb_private::Target &target) { 182 ModuleSP exe_module_sp(target.GetExecutableModule()); 183 if (exe_module_sp) { 184 ObjectFile *objfile = exe_module_sp->GetObjectFile(); 185 if (objfile) { 186 std::string xcode_contents_path; 187 std::string default_xcode_sdk; 188 FileSpec fspec; 189 llvm::VersionTuple version = objfile->GetSDKVersion(); 190 if (!version.empty()) { 191 fspec = HostInfo::GetShlibDir(); 192 if (fspec) { 193 std::string path; 194 xcode_contents_path = fspec.GetPath(); 195 size_t pos = xcode_contents_path.find("/Xcode.app/Contents/"); 196 if (pos != std::string::npos) { 197 // LLDB.framework is inside an Xcode app bundle, we can locate the 198 // SDK from here 199 xcode_contents_path.erase(pos + strlen("/Xcode.app/Contents/")); 200 } else { 201 xcode_contents_path.clear(); 202 // Use the selected Xcode 203 int status = 0; 204 int signo = 0; 205 std::string output; 206 const char *command = "xcrun -sdk macosx --show-sdk-path"; 207 lldb_private::Status error = RunShellCommand( 208 command, // shell command to run 209 FileSpec(), // current working directory 210 &status, // Put the exit status of the process in here 211 &signo, // Put the signal that caused the process to exit in 212 // here 213 &output, // Get the output from the command and place it in this 214 // string 215 std::chrono::seconds(3)); 216 if (status == 0 && !output.empty()) { 217 size_t first_non_newline = output.find_last_not_of("\r\n"); 218 if (first_non_newline != std::string::npos) 219 output.erase(first_non_newline + 1); 220 default_xcode_sdk = output; 221 222 pos = default_xcode_sdk.find("/Xcode.app/Contents/"); 223 if (pos != std::string::npos) 224 xcode_contents_path = default_xcode_sdk.substr( 225 0, pos + strlen("/Xcode.app/Contents/")); 226 } 227 } 228 } 229 230 if (!xcode_contents_path.empty()) { 231 StreamString sdk_path; 232 sdk_path.Printf("%sDeveloper/Platforms/MacOSX.platform/Developer/" 233 "SDKs/MacOSX%u.%u.sdk", 234 xcode_contents_path.c_str(), version.getMajor(), 235 version.getMinor().getValue()); 236 fspec.SetFile(sdk_path.GetString(), FileSpec::Style::native); 237 if (FileSystem::Instance().Exists(fspec)) 238 return ConstString(sdk_path.GetString()); 239 } 240 241 if (!default_xcode_sdk.empty()) { 242 fspec.SetFile(default_xcode_sdk, FileSpec::Style::native); 243 if (FileSystem::Instance().Exists(fspec)) 244 return ConstString(default_xcode_sdk); 245 } 246 } 247 } 248 } 249 return ConstString(); 250 } 251 252 Status PlatformMacOSX::GetSymbolFile(const FileSpec &platform_file, 253 const UUID *uuid_ptr, 254 FileSpec &local_file) { 255 if (IsRemote()) { 256 if (m_remote_platform_sp) 257 return m_remote_platform_sp->GetFileWithUUID(platform_file, uuid_ptr, 258 local_file); 259 } 260 261 // Default to the local case 262 local_file = platform_file; 263 return Status(); 264 } 265 266 lldb_private::Status 267 PlatformMacOSX::GetFileWithUUID(const lldb_private::FileSpec &platform_file, 268 const lldb_private::UUID *uuid_ptr, 269 lldb_private::FileSpec &local_file) { 270 if (IsRemote() && m_remote_platform_sp) { 271 std::string local_os_build; 272 #if !defined(__linux__) 273 HostInfo::GetOSBuildString(local_os_build); 274 #endif 275 std::string remote_os_build; 276 m_remote_platform_sp->GetOSBuildString(remote_os_build); 277 if (local_os_build == remote_os_build) { 278 // same OS version: the local file is good enough 279 local_file = platform_file; 280 return Status(); 281 } else { 282 // try to find the file in the cache 283 std::string cache_path(GetLocalCacheDirectory()); 284 std::string module_path(platform_file.GetPath()); 285 cache_path.append(module_path); 286 FileSpec module_cache_spec(cache_path); 287 if (FileSystem::Instance().Exists(module_cache_spec)) { 288 local_file = module_cache_spec; 289 return Status(); 290 } 291 // bring in the remote module file 292 FileSpec module_cache_folder = 293 module_cache_spec.CopyByRemovingLastPathComponent(); 294 // try to make the local directory first 295 Status err( 296 llvm::sys::fs::create_directory(module_cache_folder.GetPath())); 297 if (err.Fail()) 298 return err; 299 err = GetFile(platform_file, module_cache_spec); 300 if (err.Fail()) 301 return err; 302 if (FileSystem::Instance().Exists(module_cache_spec)) { 303 local_file = module_cache_spec; 304 return Status(); 305 } else 306 return Status("unable to obtain valid module file"); 307 } 308 } 309 local_file = platform_file; 310 return Status(); 311 } 312 313 bool PlatformMacOSX::GetSupportedArchitectureAtIndex(uint32_t idx, 314 ArchSpec &arch) { 315 #if defined(__arm__) || defined(__arm64__) || defined(__aarch64__) 316 return ARMGetSupportedArchitectureAtIndex(idx, arch); 317 #else 318 return x86GetSupportedArchitectureAtIndex(idx, arch); 319 #endif 320 } 321 322 lldb_private::Status PlatformMacOSX::GetSharedModule( 323 const lldb_private::ModuleSpec &module_spec, Process *process, 324 lldb::ModuleSP &module_sp, 325 const lldb_private::FileSpecList *module_search_paths_ptr, 326 lldb::ModuleSP *old_module_sp_ptr, bool *did_create_ptr) { 327 Status error = GetSharedModuleWithLocalCache( 328 module_spec, module_sp, module_search_paths_ptr, old_module_sp_ptr, 329 did_create_ptr); 330 331 if (module_sp) { 332 if (module_spec.GetArchitecture().GetCore() == 333 ArchSpec::eCore_x86_64_x86_64h) { 334 ObjectFile *objfile = module_sp->GetObjectFile(); 335 if (objfile == nullptr) { 336 // We didn't find an x86_64h slice, fall back to a x86_64 slice 337 ModuleSpec module_spec_x86_64(module_spec); 338 module_spec_x86_64.GetArchitecture() = ArchSpec("x86_64-apple-macosx"); 339 lldb::ModuleSP x86_64_module_sp; 340 lldb::ModuleSP old_x86_64_module_sp; 341 bool did_create = false; 342 Status x86_64_error = GetSharedModuleWithLocalCache( 343 module_spec_x86_64, x86_64_module_sp, module_search_paths_ptr, 344 &old_x86_64_module_sp, &did_create); 345 if (x86_64_module_sp && x86_64_module_sp->GetObjectFile()) { 346 module_sp = x86_64_module_sp; 347 if (old_module_sp_ptr) 348 *old_module_sp_ptr = old_x86_64_module_sp; 349 if (did_create_ptr) 350 *did_create_ptr = did_create; 351 return x86_64_error; 352 } 353 } 354 } 355 } 356 357 if (!module_sp) { 358 error = FindBundleBinaryInExecSearchPaths (module_spec, process, module_sp, module_search_paths_ptr, old_module_sp_ptr, did_create_ptr); 359 } 360 return error; 361 } 362