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