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