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