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