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