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