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