1 //===-- PlatformDarwinDevice.cpp ------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "PlatformDarwinDevice.h" 10 #include "lldb/Core/Module.h" 11 #include "lldb/Core/ModuleList.h" 12 #include "lldb/Core/ModuleSpec.h" 13 #include "lldb/Host/HostInfo.h" 14 #include "lldb/Utility/FileSpec.h" 15 #include "lldb/Utility/LLDBLog.h" 16 #include "lldb/Utility/Log.h" 17 #include <optional> 18 19 using namespace lldb; 20 using namespace lldb_private; 21 22 PlatformDarwinDevice::~PlatformDarwinDevice() = default; 23 24 FileSystem::EnumerateDirectoryResult 25 PlatformDarwinDevice::GetContainedFilesIntoVectorOfStringsCallback( 26 void *baton, llvm::sys::fs::file_type ft, llvm::StringRef path) { 27 ((PlatformDarwinDevice::SDKDirectoryInfoCollection *)baton) 28 ->push_back(PlatformDarwinDevice::SDKDirectoryInfo(FileSpec(path))); 29 return FileSystem::eEnumerateDirectoryResultNext; 30 } 31 32 bool PlatformDarwinDevice::UpdateSDKDirectoryInfosIfNeeded() { 33 Log *log = GetLog(LLDBLog::Host); 34 std::lock_guard<std::mutex> guard(m_sdk_dir_mutex); 35 if (m_sdk_directory_infos.empty()) { 36 // A --sysroot option was supplied - add it to our list of SDKs to check 37 if (!m_sdk_sysroot.empty()) { 38 FileSpec sdk_sysroot_fspec(m_sdk_sysroot.c_str()); 39 FileSystem::Instance().Resolve(sdk_sysroot_fspec); 40 const SDKDirectoryInfo sdk_sysroot_directory_info(sdk_sysroot_fspec); 41 m_sdk_directory_infos.push_back(sdk_sysroot_directory_info); 42 if (log) { 43 LLDB_LOGF(log, 44 "PlatformDarwinDevice::UpdateSDKDirectoryInfosIfNeeded added " 45 "--sysroot SDK directory %s", 46 m_sdk_sysroot.c_str()); 47 } 48 return true; 49 } 50 const char *device_support_dir = GetDeviceSupportDirectory(); 51 if (log) { 52 LLDB_LOGF(log, 53 "PlatformDarwinDevice::UpdateSDKDirectoryInfosIfNeeded Got " 54 "DeviceSupport directory %s", 55 device_support_dir); 56 } 57 if (device_support_dir) { 58 const bool find_directories = true; 59 const bool find_files = false; 60 const bool find_other = false; 61 62 SDKDirectoryInfoCollection builtin_sdk_directory_infos; 63 FileSystem::Instance().EnumerateDirectory( 64 m_device_support_directory, find_directories, find_files, find_other, 65 GetContainedFilesIntoVectorOfStringsCallback, 66 &builtin_sdk_directory_infos); 67 68 // Only add SDK directories that have symbols in them, some SDKs only 69 // contain developer disk images and no symbols, so they aren't useful to 70 // us. 71 FileSpec sdk_symbols_symlink_fspec; 72 for (const auto &sdk_directory_info : builtin_sdk_directory_infos) { 73 sdk_symbols_symlink_fspec = sdk_directory_info.directory; 74 sdk_symbols_symlink_fspec.AppendPathComponent("Symbols"); 75 if (FileSystem::Instance().Exists(sdk_symbols_symlink_fspec)) { 76 m_sdk_directory_infos.push_back(sdk_directory_info); 77 if (log) { 78 LLDB_LOGF(log, 79 "PlatformDarwinDevice::UpdateSDKDirectoryInfosIfNeeded " 80 "added builtin SDK directory %s", 81 sdk_symbols_symlink_fspec.GetPath().c_str()); 82 } 83 } 84 } 85 86 const uint32_t num_installed = m_sdk_directory_infos.size(); 87 llvm::StringRef dirname = GetDeviceSupportDirectoryName(); 88 std::string local_sdk_cache_str = "~/Library/Developer/Xcode/"; 89 local_sdk_cache_str += std::string(dirname); 90 FileSpec local_sdk_cache(local_sdk_cache_str.c_str()); 91 FileSystem::Instance().Resolve(local_sdk_cache); 92 if (FileSystem::Instance().Exists(local_sdk_cache)) { 93 if (log) { 94 LLDB_LOGF(log, 95 "PlatformDarwinDevice::UpdateSDKDirectoryInfosIfNeeded " 96 "searching %s for additional SDKs", 97 local_sdk_cache.GetPath().c_str()); 98 } 99 char path[PATH_MAX]; 100 if (local_sdk_cache.GetPath(path, sizeof(path))) { 101 FileSystem::Instance().EnumerateDirectory( 102 path, find_directories, find_files, find_other, 103 GetContainedFilesIntoVectorOfStringsCallback, 104 &m_sdk_directory_infos); 105 const uint32_t num_sdk_infos = m_sdk_directory_infos.size(); 106 // First try for an exact match of major, minor and update 107 for (uint32_t i = num_installed; i < num_sdk_infos; ++i) { 108 m_sdk_directory_infos[i].user_cached = true; 109 if (log) { 110 LLDB_LOGF(log, 111 "PlatformDarwinDevice::" 112 "UpdateSDKDirectoryInfosIfNeeded " 113 "user SDK directory %s", 114 m_sdk_directory_infos[i].directory.GetPath().c_str()); 115 } 116 } 117 } 118 } 119 120 const char *addtional_platform_dirs = getenv("PLATFORM_SDK_DIRECTORY"); 121 if (addtional_platform_dirs) { 122 SDKDirectoryInfoCollection env_var_sdk_directory_infos; 123 FileSystem::Instance().EnumerateDirectory( 124 addtional_platform_dirs, find_directories, find_files, find_other, 125 GetContainedFilesIntoVectorOfStringsCallback, 126 &env_var_sdk_directory_infos); 127 FileSpec sdk_symbols_symlink_fspec; 128 for (const auto &sdk_directory_info : env_var_sdk_directory_infos) { 129 sdk_symbols_symlink_fspec = sdk_directory_info.directory; 130 sdk_symbols_symlink_fspec.AppendPathComponent("Symbols"); 131 if (FileSystem::Instance().Exists(sdk_symbols_symlink_fspec)) { 132 m_sdk_directory_infos.push_back(sdk_directory_info); 133 if (log) { 134 LLDB_LOGF(log, 135 "PlatformDarwinDevice::UpdateSDKDirectoryInfosIfNeeded " 136 "added env var SDK directory %s", 137 sdk_symbols_symlink_fspec.GetPath().c_str()); 138 } 139 } 140 } 141 } 142 } 143 } 144 return !m_sdk_directory_infos.empty(); 145 } 146 147 const PlatformDarwinDevice::SDKDirectoryInfo * 148 PlatformDarwinDevice::GetSDKDirectoryForCurrentOSVersion() { 149 uint32_t i; 150 if (UpdateSDKDirectoryInfosIfNeeded()) { 151 const uint32_t num_sdk_infos = m_sdk_directory_infos.size(); 152 std::vector<bool> check_sdk_info(num_sdk_infos, true); 153 154 // Prefer the user SDK build string. 155 std::string build = GetSDKBuild(); 156 157 // Fall back to the platform's build string. 158 if (build.empty()) { 159 if (std::optional<std::string> os_build_str = GetOSBuildString()) 160 build.assign(*os_build_str); 161 } 162 163 // If we have a build string, only check platforms for which the build 164 // string matches. 165 if (!build.empty()) { 166 for (i = 0; i < num_sdk_infos; ++i) 167 check_sdk_info[i] = m_sdk_directory_infos[i].build.GetStringRef() == 168 llvm::StringRef(build); 169 } 170 171 // If we are connected we can find the version of the OS the platform us 172 // running on and select the right SDK 173 llvm::VersionTuple version = GetOSVersion(); 174 if (!version.empty()) { 175 if (UpdateSDKDirectoryInfosIfNeeded()) { 176 // First try for an exact match of major, minor and update. 177 for (i = 0; i < num_sdk_infos; ++i) { 178 if (check_sdk_info[i]) { 179 if (m_sdk_directory_infos[i].version == version) 180 return &m_sdk_directory_infos[i]; 181 } 182 } 183 // Try for an exact match of major and minor. 184 for (i = 0; i < num_sdk_infos; ++i) { 185 if (check_sdk_info[i]) { 186 if (m_sdk_directory_infos[i].version.getMajor() == 187 version.getMajor() && 188 m_sdk_directory_infos[i].version.getMinor() == 189 version.getMinor()) { 190 return &m_sdk_directory_infos[i]; 191 } 192 } 193 } 194 // Lastly try to match of major version only. 195 for (i = 0; i < num_sdk_infos; ++i) { 196 if (check_sdk_info[i]) { 197 if (m_sdk_directory_infos[i].version.getMajor() == 198 version.getMajor()) { 199 return &m_sdk_directory_infos[i]; 200 } 201 } 202 } 203 } 204 } else if (!build.empty()) { 205 // No version, just a build number, return the first one that matches. 206 for (i = 0; i < num_sdk_infos; ++i) 207 if (check_sdk_info[i]) 208 return &m_sdk_directory_infos[i]; 209 } 210 } 211 return nullptr; 212 } 213 214 const PlatformDarwinDevice::SDKDirectoryInfo * 215 PlatformDarwinDevice::GetSDKDirectoryForLatestOSVersion() { 216 const PlatformDarwinDevice::SDKDirectoryInfo *result = nullptr; 217 if (UpdateSDKDirectoryInfosIfNeeded()) { 218 auto max = std::max_element( 219 m_sdk_directory_infos.begin(), m_sdk_directory_infos.end(), 220 [](const SDKDirectoryInfo &a, const SDKDirectoryInfo &b) { 221 return a.version < b.version; 222 }); 223 if (max != m_sdk_directory_infos.end()) 224 result = &*max; 225 } 226 return result; 227 } 228 229 const char *PlatformDarwinDevice::GetDeviceSupportDirectory() { 230 std::string platform_dir = 231 ("/Platforms/" + GetPlatformName() + "/DeviceSupport").str(); 232 if (m_device_support_directory.empty()) { 233 if (FileSpec fspec = HostInfo::GetXcodeDeveloperDirectory()) { 234 m_device_support_directory = fspec.GetPath(); 235 m_device_support_directory.append(platform_dir.c_str()); 236 } else { 237 // Assign a single NULL character so we know we tried to find the device 238 // support directory and we don't keep trying to find it over and over. 239 m_device_support_directory.assign(1, '\0'); 240 } 241 } 242 // We should have put a single NULL character into m_device_support_directory 243 // or it should have a valid path if the code gets here 244 assert(m_device_support_directory.empty() == false); 245 if (m_device_support_directory[0]) 246 return m_device_support_directory.c_str(); 247 return nullptr; 248 } 249 250 const char *PlatformDarwinDevice::GetDeviceSupportDirectoryForOSVersion() { 251 if (!m_sdk_sysroot.empty()) 252 return m_sdk_sysroot.c_str(); 253 254 if (m_device_support_directory_for_os_version.empty()) { 255 const PlatformDarwinDevice::SDKDirectoryInfo *sdk_dir_info = 256 GetSDKDirectoryForCurrentOSVersion(); 257 if (sdk_dir_info == nullptr) 258 sdk_dir_info = GetSDKDirectoryForLatestOSVersion(); 259 if (sdk_dir_info) { 260 char path[PATH_MAX]; 261 if (sdk_dir_info->directory.GetPath(path, sizeof(path))) { 262 m_device_support_directory_for_os_version = path; 263 return m_device_support_directory_for_os_version.c_str(); 264 } 265 } else { 266 // Assign a single NULL character so we know we tried to find the device 267 // support directory and we don't keep trying to find it over and over. 268 m_device_support_directory_for_os_version.assign(1, '\0'); 269 } 270 } 271 // We should have put a single NULL character into 272 // m_device_support_directory_for_os_version or it should have a valid path 273 // if the code gets here 274 assert(m_device_support_directory_for_os_version.empty() == false); 275 if (m_device_support_directory_for_os_version[0]) 276 return m_device_support_directory_for_os_version.c_str(); 277 return nullptr; 278 } 279 280 static lldb_private::Status 281 MakeCacheFolderForFile(const FileSpec &module_cache_spec) { 282 FileSpec module_cache_folder = 283 module_cache_spec.CopyByRemovingLastPathComponent(); 284 return llvm::sys::fs::create_directory(module_cache_folder.GetPath()); 285 } 286 287 static lldb_private::Status 288 BringInRemoteFile(Platform *platform, 289 const lldb_private::ModuleSpec &module_spec, 290 const FileSpec &module_cache_spec) { 291 MakeCacheFolderForFile(module_cache_spec); 292 Status err = platform->GetFile(module_spec.GetFileSpec(), module_cache_spec); 293 return err; 294 } 295 296 lldb_private::Status PlatformDarwinDevice::GetSharedModuleWithLocalCache( 297 const lldb_private::ModuleSpec &module_spec, lldb::ModuleSP &module_sp, 298 const lldb_private::FileSpecList *module_search_paths_ptr, 299 llvm::SmallVectorImpl<lldb::ModuleSP> *old_modules, bool *did_create_ptr) { 300 301 Log *log = GetLog(LLDBLog::Platform); 302 LLDB_LOGF(log, 303 "[%s] Trying to find module %s/%s - platform path %s/%s symbol " 304 "path %s/%s", 305 (IsHost() ? "host" : "remote"), 306 module_spec.GetFileSpec().GetDirectory().AsCString(), 307 module_spec.GetFileSpec().GetFilename().AsCString(), 308 module_spec.GetPlatformFileSpec().GetDirectory().AsCString(), 309 module_spec.GetPlatformFileSpec().GetFilename().AsCString(), 310 module_spec.GetSymbolFileSpec().GetDirectory().AsCString(), 311 module_spec.GetSymbolFileSpec().GetFilename().AsCString()); 312 313 Status err; 314 315 if (CheckLocalSharedCache()) { 316 // When debugging on the host, we are most likely using the same shared 317 // cache as our inferior. The dylibs from the shared cache might not 318 // exist on the filesystem, so let's use the images in our own memory 319 // to create the modules. 320 321 // Check if the requested image is in our shared cache. 322 SharedCacheImageInfo image_info = 323 HostInfo::GetSharedCacheImageInfo(module_spec.GetFileSpec().GetPath()); 324 325 // If we found it and it has the correct UUID, let's proceed with 326 // creating a module from the memory contents. 327 if (image_info.uuid && 328 (!module_spec.GetUUID() || module_spec.GetUUID() == image_info.uuid)) { 329 ModuleSpec shared_cache_spec(module_spec.GetFileSpec(), image_info.uuid, 330 image_info.data_sp); 331 err = ModuleList::GetSharedModule(shared_cache_spec, module_sp, 332 module_search_paths_ptr, old_modules, 333 did_create_ptr); 334 if (module_sp) { 335 LLDB_LOGF(log, "[%s] module %s was found in the in-memory shared cache", 336 (IsHost() ? "host" : "remote"), 337 module_spec.GetFileSpec().GetPath().c_str()); 338 return err; 339 } 340 } 341 342 // We failed to find the module in our shared cache. Let's see if we have a 343 // copy in our device support directory. 344 FileSpec device_support_spec(GetDeviceSupportDirectoryForOSVersion()); 345 device_support_spec.AppendPathComponent("Symbols"); 346 device_support_spec.AppendPathComponent( 347 module_spec.GetFileSpec().GetPath()); 348 FileSystem::Instance().Resolve(device_support_spec); 349 if (FileSystem::Instance().Exists(device_support_spec)) { 350 ModuleSpec local_spec(device_support_spec, module_spec.GetUUID()); 351 err = ModuleList::GetSharedModule(local_spec, module_sp, 352 module_search_paths_ptr, old_modules, 353 did_create_ptr); 354 if (module_sp) { 355 LLDB_LOGF(log, 356 "[%s] module %s was found in Device Support " 357 "directory: %s", 358 (IsHost() ? "host" : "remote"), 359 module_spec.GetFileSpec().GetPath().c_str(), 360 local_spec.GetFileSpec().GetPath().c_str()); 361 return err; 362 } 363 } 364 } 365 366 err = ModuleList::GetSharedModule(module_spec, module_sp, 367 module_search_paths_ptr, old_modules, 368 did_create_ptr); 369 if (module_sp) 370 return err; 371 372 if (!IsHost()) { 373 std::string cache_path(GetLocalCacheDirectory()); 374 // Only search for a locally cached file if we have a valid cache path 375 if (!cache_path.empty()) { 376 std::string module_path(module_spec.GetFileSpec().GetPath()); 377 cache_path.append(module_path); 378 FileSpec module_cache_spec(cache_path); 379 380 // if rsync is supported, always bring in the file - rsync will be very 381 // efficient when files are the same on the local and remote end of the 382 // connection 383 if (this->GetSupportsRSync()) { 384 err = BringInRemoteFile(this, module_spec, module_cache_spec); 385 if (err.Fail()) 386 return err; 387 if (FileSystem::Instance().Exists(module_cache_spec)) { 388 Log *log = GetLog(LLDBLog::Platform); 389 LLDB_LOGF(log, "[%s] module %s/%s was rsynced and is now there", 390 (IsHost() ? "host" : "remote"), 391 module_spec.GetFileSpec().GetDirectory().AsCString(), 392 module_spec.GetFileSpec().GetFilename().AsCString()); 393 ModuleSpec local_spec(module_cache_spec, 394 module_spec.GetArchitecture()); 395 module_sp = std::make_shared<Module>(local_spec); 396 module_sp->SetPlatformFileSpec(module_spec.GetFileSpec()); 397 return Status(); 398 } 399 } 400 401 // try to find the module in the cache 402 if (FileSystem::Instance().Exists(module_cache_spec)) { 403 // get the local and remote MD5 and compare 404 if (m_remote_platform_sp) { 405 // when going over the *slow* GDB remote transfer mechanism we first 406 // check the hashes of the files - and only do the actual transfer if 407 // they differ 408 auto MD5 = llvm::sys::fs::md5_contents(module_cache_spec.GetPath()); 409 if (!MD5) 410 return Status(MD5.getError()); 411 412 Log *log = GetLog(LLDBLog::Platform); 413 bool requires_transfer = true; 414 llvm::ErrorOr<llvm::MD5::MD5Result> remote_md5 = 415 m_remote_platform_sp->CalculateMD5(module_spec.GetFileSpec()); 416 if (std::error_code ec = remote_md5.getError()) 417 LLDB_LOG(log, "couldn't get md5 sum from remote: {0}", 418 ec.message()); 419 else 420 requires_transfer = *MD5 != *remote_md5; 421 if (requires_transfer) { 422 // bring in the remote file 423 LLDB_LOGF(log, 424 "[%s] module %s/%s needs to be replaced from remote copy", 425 (IsHost() ? "host" : "remote"), 426 module_spec.GetFileSpec().GetDirectory().AsCString(), 427 module_spec.GetFileSpec().GetFilename().AsCString()); 428 Status err = 429 BringInRemoteFile(this, module_spec, module_cache_spec); 430 if (err.Fail()) 431 return err; 432 } 433 } 434 435 ModuleSpec local_spec(module_cache_spec, module_spec.GetArchitecture()); 436 module_sp = std::make_shared<Module>(local_spec); 437 module_sp->SetPlatformFileSpec(module_spec.GetFileSpec()); 438 Log *log = GetLog(LLDBLog::Platform); 439 LLDB_LOGF(log, "[%s] module %s/%s was found in the cache", 440 (IsHost() ? "host" : "remote"), 441 module_spec.GetFileSpec().GetDirectory().AsCString(), 442 module_spec.GetFileSpec().GetFilename().AsCString()); 443 return Status(); 444 } 445 446 // bring in the remote module file 447 LLDB_LOGF(log, "[%s] module %s/%s needs to come in remotely", 448 (IsHost() ? "host" : "remote"), 449 module_spec.GetFileSpec().GetDirectory().AsCString(), 450 module_spec.GetFileSpec().GetFilename().AsCString()); 451 Status err = BringInRemoteFile(this, module_spec, module_cache_spec); 452 if (err.Fail()) 453 return err; 454 if (FileSystem::Instance().Exists(module_cache_spec)) { 455 Log *log = GetLog(LLDBLog::Platform); 456 LLDB_LOGF(log, "[%s] module %s/%s is now cached and fine", 457 (IsHost() ? "host" : "remote"), 458 module_spec.GetFileSpec().GetDirectory().AsCString(), 459 module_spec.GetFileSpec().GetFilename().AsCString()); 460 ModuleSpec local_spec(module_cache_spec, module_spec.GetArchitecture()); 461 module_sp = std::make_shared<Module>(local_spec); 462 module_sp->SetPlatformFileSpec(module_spec.GetFileSpec()); 463 return Status(); 464 } else 465 return Status::FromErrorString("unable to obtain valid module file"); 466 } else 467 return Status::FromErrorString("no cache path"); 468 } else 469 return Status::FromErrorString("unable to resolve module"); 470 } 471