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 ObjectFile *objfile = exe_module_sp->GetObjectFile(); 190 if (objfile) { 191 std::string xcode_contents_path; 192 std::string default_xcode_sdk; 193 FileSpec fspec; 194 llvm::VersionTuple version = objfile->GetSDKVersion(); 195 if (!version.empty()) { 196 fspec = HostInfo::GetShlibDir(); 197 if (fspec) { 198 std::string path; 199 xcode_contents_path = fspec.GetPath(); 200 size_t pos = xcode_contents_path.find("/Xcode.app/Contents/"); 201 if (pos != std::string::npos) { 202 // LLDB.framework is inside an Xcode app bundle, we can locate the 203 // SDK from here 204 xcode_contents_path.erase(pos + strlen("/Xcode.app/Contents/")); 205 } else { 206 xcode_contents_path.clear(); 207 // Use the selected Xcode 208 int status = 0; 209 int signo = 0; 210 std::string output; 211 const char *command = "xcrun -sdk macosx --show-sdk-path"; 212 lldb_private::Status error = RunShellCommand( 213 command, // shell command to run 214 FileSpec(), // current working directory 215 &status, // Put the exit status of the process in here 216 &signo, // Put the signal that caused the process to exit in 217 // here 218 &output, // Get the output from the command and place it in this 219 // string 220 std::chrono::seconds(3)); 221 if (status == 0 && !output.empty()) { 222 size_t first_non_newline = output.find_last_not_of("\r\n"); 223 if (first_non_newline != std::string::npos) 224 output.erase(first_non_newline + 1); 225 default_xcode_sdk = output; 226 227 pos = default_xcode_sdk.find("/Xcode.app/Contents/"); 228 if (pos != std::string::npos) 229 xcode_contents_path = default_xcode_sdk.substr( 230 0, pos + strlen("/Xcode.app/Contents/")); 231 } 232 } 233 } 234 235 if (!xcode_contents_path.empty()) { 236 StreamString sdk_path; 237 sdk_path.Printf("%sDeveloper/Platforms/MacOSX.platform/Developer/" 238 "SDKs/MacOSX%u.%u.sdk", 239 xcode_contents_path.c_str(), version.getMajor(), 240 version.getMinor().getValue()); 241 fspec.SetFile(sdk_path.GetString(), FileSpec::Style::native); 242 if (FileSystem::Instance().Exists(fspec)) 243 return ConstString(sdk_path.GetString()); 244 } 245 246 if (!default_xcode_sdk.empty()) { 247 fspec.SetFile(default_xcode_sdk, FileSpec::Style::native); 248 if (FileSystem::Instance().Exists(fspec)) 249 return ConstString(default_xcode_sdk); 250 } 251 } 252 } 253 } 254 return ConstString(); 255 } 256 257 Status PlatformMacOSX::GetSymbolFile(const FileSpec &platform_file, 258 const UUID *uuid_ptr, 259 FileSpec &local_file) { 260 if (IsRemote()) { 261 if (m_remote_platform_sp) 262 return m_remote_platform_sp->GetFileWithUUID(platform_file, uuid_ptr, 263 local_file); 264 } 265 266 // Default to the local case 267 local_file = platform_file; 268 return Status(); 269 } 270 271 lldb_private::Status 272 PlatformMacOSX::GetFileWithUUID(const lldb_private::FileSpec &platform_file, 273 const lldb_private::UUID *uuid_ptr, 274 lldb_private::FileSpec &local_file) { 275 if (IsRemote() && m_remote_platform_sp) { 276 std::string local_os_build; 277 #if !defined(__linux__) 278 HostInfo::GetOSBuildString(local_os_build); 279 #endif 280 std::string remote_os_build; 281 m_remote_platform_sp->GetOSBuildString(remote_os_build); 282 if (local_os_build == remote_os_build) { 283 // same OS version: the local file is good enough 284 local_file = platform_file; 285 return Status(); 286 } else { 287 // try to find the file in the cache 288 std::string cache_path(GetLocalCacheDirectory()); 289 std::string module_path(platform_file.GetPath()); 290 cache_path.append(module_path); 291 FileSpec module_cache_spec(cache_path); 292 if (FileSystem::Instance().Exists(module_cache_spec)) { 293 local_file = module_cache_spec; 294 return Status(); 295 } 296 // bring in the remote module file 297 FileSpec module_cache_folder = 298 module_cache_spec.CopyByRemovingLastPathComponent(); 299 // try to make the local directory first 300 Status err( 301 llvm::sys::fs::create_directory(module_cache_folder.GetPath())); 302 if (err.Fail()) 303 return err; 304 err = GetFile(platform_file, module_cache_spec); 305 if (err.Fail()) 306 return err; 307 if (FileSystem::Instance().Exists(module_cache_spec)) { 308 local_file = module_cache_spec; 309 return Status(); 310 } else 311 return Status("unable to obtain valid module file"); 312 } 313 } 314 local_file = platform_file; 315 return Status(); 316 } 317 318 bool PlatformMacOSX::GetSupportedArchitectureAtIndex(uint32_t idx, 319 ArchSpec &arch) { 320 #if defined(__arm__) || defined(__arm64__) || defined(__aarch64__) 321 return ARMGetSupportedArchitectureAtIndex(idx, arch); 322 #else 323 return x86GetSupportedArchitectureAtIndex(idx, arch); 324 #endif 325 } 326 327 lldb_private::Status PlatformMacOSX::GetSharedModule( 328 const lldb_private::ModuleSpec &module_spec, Process *process, 329 lldb::ModuleSP &module_sp, 330 const lldb_private::FileSpecList *module_search_paths_ptr, 331 lldb::ModuleSP *old_module_sp_ptr, bool *did_create_ptr) { 332 Status error = GetSharedModuleWithLocalCache( 333 module_spec, module_sp, module_search_paths_ptr, old_module_sp_ptr, 334 did_create_ptr); 335 336 if (module_sp) { 337 if (module_spec.GetArchitecture().GetCore() == 338 ArchSpec::eCore_x86_64_x86_64h) { 339 ObjectFile *objfile = module_sp->GetObjectFile(); 340 if (objfile == nullptr) { 341 // We didn't find an x86_64h slice, fall back to a x86_64 slice 342 ModuleSpec module_spec_x86_64(module_spec); 343 module_spec_x86_64.GetArchitecture() = ArchSpec("x86_64-apple-macosx"); 344 lldb::ModuleSP x86_64_module_sp; 345 lldb::ModuleSP old_x86_64_module_sp; 346 bool did_create = false; 347 Status x86_64_error = GetSharedModuleWithLocalCache( 348 module_spec_x86_64, x86_64_module_sp, module_search_paths_ptr, 349 &old_x86_64_module_sp, &did_create); 350 if (x86_64_module_sp && x86_64_module_sp->GetObjectFile()) { 351 module_sp = x86_64_module_sp; 352 if (old_module_sp_ptr) 353 *old_module_sp_ptr = old_x86_64_module_sp; 354 if (did_create_ptr) 355 *did_create_ptr = did_create; 356 return x86_64_error; 357 } 358 } 359 } 360 } 361 362 if (!module_sp) { 363 error = FindBundleBinaryInExecSearchPaths (module_spec, process, module_sp, module_search_paths_ptr, old_module_sp_ptr, did_create_ptr); 364 } 365 return error; 366 } 367