1 //===-- PlatformRemoteAppleWatch.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 // C Includes 11 // C++ Includes 12 #include <string> 13 #include <vector> 14 15 // Other libraries and framework includes 16 // Project includes 17 #include "PlatformRemoteAppleWatch.h" 18 19 #include "lldb/Breakpoint/BreakpointLocation.h" 20 #include "lldb/Core/ArchSpec.h" 21 #include "lldb/Core/Error.h" 22 #include "lldb/Core/Log.h" 23 #include "lldb/Core/Module.h" 24 #include "lldb/Core/ModuleList.h" 25 #include "lldb/Core/ModuleSpec.h" 26 #include "lldb/Core/PluginManager.h" 27 #include "lldb/Core/StreamString.h" 28 #include "lldb/Host/FileSpec.h" 29 #include "lldb/Host/Host.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 //------------------------------------------------------------------ 37 /// Default Constructor 38 //------------------------------------------------------------------ 39 PlatformRemoteAppleWatch::PlatformRemoteAppleWatch() 40 : PlatformDarwin(false), // This is a remote platform 41 m_sdk_directory_infos(), m_device_support_directory(), 42 m_device_support_directory_for_os_version(), m_build_update(), 43 m_last_module_sdk_idx(UINT32_MAX), 44 m_connected_module_sdk_idx(UINT32_MAX) {} 45 46 PlatformRemoteAppleWatch::SDKDirectoryInfo::SDKDirectoryInfo( 47 const lldb_private::FileSpec &sdk_dir) 48 : directory(sdk_dir), build(), version_major(0), version_minor(0), 49 version_update(0), user_cached(false) { 50 llvm::StringRef dirname_str = sdk_dir.GetFilename().GetStringRef(); 51 llvm::StringRef build_str; 52 std::tie(version_major, version_minor, version_update, build_str) = 53 ParseVersionBuildDir(dirname_str); 54 build.SetString(build_str); 55 } 56 57 //------------------------------------------------------------------ 58 // Static Variables 59 //------------------------------------------------------------------ 60 static uint32_t g_initialize_count = 0; 61 62 //------------------------------------------------------------------ 63 // Static Functions 64 //------------------------------------------------------------------ 65 void PlatformRemoteAppleWatch::Initialize() { 66 PlatformDarwin::Initialize(); 67 68 if (g_initialize_count++ == 0) { 69 PluginManager::RegisterPlugin( 70 PlatformRemoteAppleWatch::GetPluginNameStatic(), 71 PlatformRemoteAppleWatch::GetDescriptionStatic(), 72 PlatformRemoteAppleWatch::CreateInstance); 73 } 74 } 75 76 void PlatformRemoteAppleWatch::Terminate() { 77 if (g_initialize_count > 0) { 78 if (--g_initialize_count == 0) { 79 PluginManager::UnregisterPlugin(PlatformRemoteAppleWatch::CreateInstance); 80 } 81 } 82 83 PlatformDarwin::Terminate(); 84 } 85 86 PlatformSP PlatformRemoteAppleWatch::CreateInstance(bool force, 87 const ArchSpec *arch) { 88 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM)); 89 if (log) { 90 const char *arch_name; 91 if (arch && arch->GetArchitectureName()) 92 arch_name = arch->GetArchitectureName(); 93 else 94 arch_name = "<null>"; 95 96 const char *triple_cstr = 97 arch ? arch->GetTriple().getTriple().c_str() : "<null>"; 98 99 log->Printf("PlatformRemoteAppleWatch::%s(force=%s, arch={%s,%s})", 100 __FUNCTION__, force ? "true" : "false", arch_name, triple_cstr); 101 } 102 103 bool create = force; 104 if (!create && arch && arch->IsValid()) { 105 switch (arch->GetMachine()) { 106 case llvm::Triple::arm: 107 case llvm::Triple::aarch64: 108 case llvm::Triple::thumb: { 109 const llvm::Triple &triple = arch->GetTriple(); 110 llvm::Triple::VendorType vendor = triple.getVendor(); 111 switch (vendor) { 112 case llvm::Triple::Apple: 113 create = true; 114 break; 115 116 #if defined(__APPLE__) 117 // Only accept "unknown" for the vendor if the host is Apple and 118 // it "unknown" wasn't specified (it was just returned because it 119 // was NOT specified) 120 case llvm::Triple::UnknownArch: 121 create = !arch->TripleVendorWasSpecified(); 122 break; 123 124 #endif 125 default: 126 break; 127 } 128 if (create) { 129 switch (triple.getOS()) { 130 case llvm::Triple::WatchOS: // This is the right triple value for Apple 131 // Watch debugging 132 break; 133 134 default: 135 create = false; 136 break; 137 } 138 } 139 } break; 140 default: 141 break; 142 } 143 } 144 145 #if defined(__APPLE__) && \ 146 (defined(__arm__) || defined(__arm64__) || defined(__aarch64__)) 147 // If lldb is running on a watch, this isn't a RemoteWatch environment; it's a 148 // local system environment. 149 if (force == false) { 150 create = false; 151 } 152 #endif 153 154 if (create) { 155 if (log) 156 log->Printf("PlatformRemoteAppleWatch::%s() creating platform", 157 __FUNCTION__); 158 159 return lldb::PlatformSP(new PlatformRemoteAppleWatch()); 160 } 161 162 if (log) 163 log->Printf("PlatformRemoteAppleWatch::%s() aborting creation of platform", 164 __FUNCTION__); 165 166 return lldb::PlatformSP(); 167 } 168 169 lldb_private::ConstString PlatformRemoteAppleWatch::GetPluginNameStatic() { 170 static ConstString g_name("remote-watchos"); 171 return g_name; 172 } 173 174 const char *PlatformRemoteAppleWatch::GetDescriptionStatic() { 175 return "Remote Apple Watch platform plug-in."; 176 } 177 178 void PlatformRemoteAppleWatch::GetStatus(Stream &strm) { 179 Platform::GetStatus(strm); 180 const char *sdk_directory = GetDeviceSupportDirectoryForOSVersion(); 181 if (sdk_directory) 182 strm.Printf(" SDK Path: \"%s\"\n", sdk_directory); 183 else 184 strm.PutCString(" SDK Path: error: unable to locate SDK\n"); 185 186 const uint32_t num_sdk_infos = m_sdk_directory_infos.size(); 187 for (uint32_t i = 0; i < num_sdk_infos; ++i) { 188 const SDKDirectoryInfo &sdk_dir_info = m_sdk_directory_infos[i]; 189 strm.Printf(" SDK Roots: [%2u] \"%s\"\n", i, 190 sdk_dir_info.directory.GetPath().c_str()); 191 } 192 } 193 194 Error PlatformRemoteAppleWatch::ResolveExecutable( 195 const ModuleSpec &ms, lldb::ModuleSP &exe_module_sp, 196 const FileSpecList *module_search_paths_ptr) { 197 Error error; 198 // Nothing special to do here, just use the actual file and architecture 199 200 ModuleSpec resolved_module_spec(ms); 201 202 // Resolve any executable within a bundle on MacOSX 203 // TODO: verify that this handles shallow bundles, if not then implement one 204 // ourselves 205 Host::ResolveExecutableInBundle(resolved_module_spec.GetFileSpec()); 206 207 if (resolved_module_spec.GetFileSpec().Exists()) { 208 if (resolved_module_spec.GetArchitecture().IsValid() || 209 resolved_module_spec.GetUUID().IsValid()) { 210 error = ModuleList::GetSharedModule(resolved_module_spec, exe_module_sp, 211 nullptr, nullptr, nullptr); 212 213 if (exe_module_sp && exe_module_sp->GetObjectFile()) 214 return error; 215 exe_module_sp.reset(); 216 } 217 // No valid architecture was specified or the exact ARM slice wasn't 218 // found so ask the platform for the architectures that we should be 219 // using (in the correct order) and see if we can find a match that way 220 StreamString arch_names; 221 for (uint32_t idx = 0; GetSupportedArchitectureAtIndex( 222 idx, resolved_module_spec.GetArchitecture()); 223 ++idx) { 224 error = ModuleList::GetSharedModule(resolved_module_spec, exe_module_sp, 225 nullptr, nullptr, nullptr); 226 // Did we find an executable using one of the 227 if (error.Success()) { 228 if (exe_module_sp && exe_module_sp->GetObjectFile()) 229 break; 230 else 231 error.SetErrorToGenericError(); 232 } 233 234 if (idx > 0) 235 arch_names.PutCString(", "); 236 arch_names.PutCString( 237 resolved_module_spec.GetArchitecture().GetArchitectureName()); 238 } 239 240 if (error.Fail() || !exe_module_sp) { 241 if (resolved_module_spec.GetFileSpec().Readable()) { 242 error.SetErrorStringWithFormat( 243 "'%s' doesn't contain any '%s' platform architectures: %s", 244 resolved_module_spec.GetFileSpec().GetPath().c_str(), 245 GetPluginName().GetCString(), arch_names.GetString().c_str()); 246 } else { 247 error.SetErrorStringWithFormat( 248 "'%s' is not readable", 249 resolved_module_spec.GetFileSpec().GetPath().c_str()); 250 } 251 } 252 } else { 253 error.SetErrorStringWithFormat( 254 "'%s' does not exist", 255 resolved_module_spec.GetFileSpec().GetPath().c_str()); 256 } 257 258 return error; 259 } 260 261 FileSpec::EnumerateDirectoryResult 262 PlatformRemoteAppleWatch::GetContainedFilesIntoVectorOfStringsCallback( 263 void *baton, FileSpec::FileType file_type, const FileSpec &file_spec) { 264 ((PlatformRemoteAppleWatch::SDKDirectoryInfoCollection *)baton) 265 ->push_back(PlatformRemoteAppleWatch::SDKDirectoryInfo(file_spec)); 266 return FileSpec::eEnumerateDirectoryResultNext; 267 } 268 269 bool PlatformRemoteAppleWatch::UpdateSDKDirectoryInfosIfNeeded() { 270 Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST); 271 if (m_sdk_directory_infos.empty()) { 272 const char *device_support_dir = GetDeviceSupportDirectory(); 273 if (log) { 274 log->Printf("PlatformRemoteAppleWatch::UpdateSDKDirectoryInfosIfNeeded " 275 "Got DeviceSupport directory %s", 276 device_support_dir); 277 } 278 if (device_support_dir) { 279 const bool find_directories = true; 280 const bool find_files = false; 281 const bool find_other = false; 282 283 SDKDirectoryInfoCollection builtin_sdk_directory_infos; 284 FileSpec::EnumerateDirectory(m_device_support_directory, find_directories, 285 find_files, find_other, 286 GetContainedFilesIntoVectorOfStringsCallback, 287 &builtin_sdk_directory_infos); 288 289 // Only add SDK directories that have symbols in them, some SDKs only 290 // contain 291 // developer disk images and no symbols, so they aren't useful to us. 292 FileSpec sdk_symbols_symlink_fspec; 293 for (const auto &sdk_directory_info : builtin_sdk_directory_infos) { 294 sdk_symbols_symlink_fspec = sdk_directory_info.directory; 295 sdk_symbols_symlink_fspec.AppendPathComponent("Symbols.Internal"); 296 if (sdk_symbols_symlink_fspec.Exists()) { 297 m_sdk_directory_infos.push_back(sdk_directory_info); 298 if (log) { 299 log->Printf("PlatformRemoteAppleWatch::" 300 "UpdateSDKDirectoryInfosIfNeeded added builtin SDK " 301 "directory %s", 302 sdk_symbols_symlink_fspec.GetPath().c_str()); 303 } 304 } else { 305 sdk_symbols_symlink_fspec.GetFilename().SetCString("Symbols"); 306 if (sdk_symbols_symlink_fspec.Exists()) 307 m_sdk_directory_infos.push_back(sdk_directory_info); 308 if (log) { 309 log->Printf("PlatformRemoteAppleWatch::" 310 "UpdateSDKDirectoryInfosIfNeeded added builtin SDK " 311 "directory %s", 312 sdk_symbols_symlink_fspec.GetPath().c_str()); 313 } 314 } 315 } 316 317 const uint32_t num_installed = m_sdk_directory_infos.size(); 318 FileSpec local_sdk_cache( 319 "~/Library/Developer/Xcode/watchOS DeviceSupport", true); 320 if (!local_sdk_cache.Exists()) { 321 local_sdk_cache = 322 FileSpec("~/Library/Developer/Xcode/watch OS DeviceSupport", true); 323 } 324 if (!local_sdk_cache.Exists()) { 325 local_sdk_cache = 326 FileSpec("~/Library/Developer/Xcode/WatchOS DeviceSupport", true); 327 } 328 if (!local_sdk_cache.Exists()) { 329 local_sdk_cache = 330 FileSpec("~/Library/Developer/Xcode/Watch OS DeviceSupport", true); 331 } 332 if (local_sdk_cache.Exists()) { 333 if (log) { 334 log->Printf("PlatformRemoteAppleWatch::" 335 "UpdateSDKDirectoryInfosIfNeeded searching %s for " 336 "additional SDKs", 337 local_sdk_cache.GetPath().c_str()); 338 } 339 char path[PATH_MAX]; 340 if (local_sdk_cache.GetPath(path, sizeof(path))) { 341 FileSpec::EnumerateDirectory( 342 path, find_directories, find_files, find_other, 343 GetContainedFilesIntoVectorOfStringsCallback, 344 &m_sdk_directory_infos); 345 const uint32_t num_sdk_infos = m_sdk_directory_infos.size(); 346 // First try for an exact match of major, minor and update 347 for (uint32_t i = num_installed; i < num_sdk_infos; ++i) { 348 m_sdk_directory_infos[i].user_cached = true; 349 if (log) { 350 log->Printf("PlatformRemoteAppleWatch::" 351 "UpdateSDKDirectoryInfosIfNeeded user SDK directory " 352 "%s", 353 m_sdk_directory_infos[i].directory.GetPath().c_str()); 354 } 355 } 356 } 357 } 358 } 359 } 360 return !m_sdk_directory_infos.empty(); 361 } 362 363 const PlatformRemoteAppleWatch::SDKDirectoryInfo * 364 PlatformRemoteAppleWatch::GetSDKDirectoryForCurrentOSVersion() { 365 uint32_t i; 366 if (UpdateSDKDirectoryInfosIfNeeded()) { 367 const uint32_t num_sdk_infos = m_sdk_directory_infos.size(); 368 369 // Check to see if the user specified a build string. If they did, then 370 // be sure to match it. 371 std::vector<bool> check_sdk_info(num_sdk_infos, true); 372 ConstString build(m_sdk_build); 373 if (build) { 374 for (i = 0; i < num_sdk_infos; ++i) 375 check_sdk_info[i] = m_sdk_directory_infos[i].build == build; 376 } 377 378 // If we are connected we can find the version of the OS the platform 379 // us running on and select the right SDK 380 uint32_t major, minor, update; 381 if (GetOSVersion(major, minor, update)) { 382 if (UpdateSDKDirectoryInfosIfNeeded()) { 383 // First try for an exact match of major, minor and update 384 for (i = 0; i < num_sdk_infos; ++i) { 385 if (check_sdk_info[i]) { 386 if (m_sdk_directory_infos[i].version_major == major && 387 m_sdk_directory_infos[i].version_minor == minor && 388 m_sdk_directory_infos[i].version_update == update) { 389 return &m_sdk_directory_infos[i]; 390 } 391 } 392 } 393 // First try for an exact match of major and minor 394 for (i = 0; i < num_sdk_infos; ++i) { 395 if (check_sdk_info[i]) { 396 if (m_sdk_directory_infos[i].version_major == major && 397 m_sdk_directory_infos[i].version_minor == minor) { 398 return &m_sdk_directory_infos[i]; 399 } 400 } 401 } 402 // Lastly try to match of major version only.. 403 for (i = 0; i < num_sdk_infos; ++i) { 404 if (check_sdk_info[i]) { 405 if (m_sdk_directory_infos[i].version_major == major) { 406 return &m_sdk_directory_infos[i]; 407 } 408 } 409 } 410 } 411 } else if (build) { 412 // No version, just a build number, search for the first one that matches 413 for (i = 0; i < num_sdk_infos; ++i) 414 if (check_sdk_info[i]) 415 return &m_sdk_directory_infos[i]; 416 } 417 } 418 return nullptr; 419 } 420 421 const PlatformRemoteAppleWatch::SDKDirectoryInfo * 422 PlatformRemoteAppleWatch::GetSDKDirectoryForLatestOSVersion() { 423 const PlatformRemoteAppleWatch::SDKDirectoryInfo *result = nullptr; 424 if (UpdateSDKDirectoryInfosIfNeeded()) { 425 const uint32_t num_sdk_infos = m_sdk_directory_infos.size(); 426 // First try for an exact match of major, minor and update 427 for (uint32_t i = 0; i < num_sdk_infos; ++i) { 428 const SDKDirectoryInfo &sdk_dir_info = m_sdk_directory_infos[i]; 429 if (sdk_dir_info.version_major != UINT32_MAX) { 430 if (result == nullptr || 431 sdk_dir_info.version_major > result->version_major) { 432 result = &sdk_dir_info; 433 } else if (sdk_dir_info.version_major == result->version_major) { 434 if (sdk_dir_info.version_minor > result->version_minor) { 435 result = &sdk_dir_info; 436 } else if (sdk_dir_info.version_minor == result->version_minor) { 437 if (sdk_dir_info.version_update > result->version_update) { 438 result = &sdk_dir_info; 439 } 440 } 441 } 442 } 443 } 444 } 445 return result; 446 } 447 448 const char *PlatformRemoteAppleWatch::GetDeviceSupportDirectory() { 449 if (m_device_support_directory.empty()) { 450 const char *device_support_dir = GetDeveloperDirectory(); 451 if (device_support_dir) { 452 m_device_support_directory.assign(device_support_dir); 453 m_device_support_directory.append( 454 "/Platforms/watchOS.platform/DeviceSupport"); 455 FileSpec platform_device_support_dir(m_device_support_directory, true); 456 if (!platform_device_support_dir.Exists()) { 457 std::string alt_platform_dirname = device_support_dir; 458 alt_platform_dirname.append( 459 "/Platforms/WatchOS.platform/DeviceSupport"); 460 FileSpec alt_platform_device_support_dir(m_device_support_directory, 461 true); 462 if (alt_platform_device_support_dir.Exists()) { 463 m_device_support_directory = alt_platform_dirname; 464 } 465 } 466 } else { 467 // Assign a single NULL character so we know we tried to find the device 468 // support directory and we don't keep trying to find it over and over. 469 m_device_support_directory.assign(1, '\0'); 470 } 471 } 472 // We should have put a single NULL character into m_device_support_directory 473 // or it should have a valid path if the code gets here 474 assert(m_device_support_directory.empty() == false); 475 if (m_device_support_directory[0]) 476 return m_device_support_directory.c_str(); 477 return nullptr; 478 } 479 480 const char *PlatformRemoteAppleWatch::GetDeviceSupportDirectoryForOSVersion() { 481 if (m_sdk_sysroot) 482 return m_sdk_sysroot.GetCString(); 483 484 if (m_device_support_directory_for_os_version.empty()) { 485 const PlatformRemoteAppleWatch::SDKDirectoryInfo *sdk_dir_info = 486 GetSDKDirectoryForCurrentOSVersion(); 487 if (sdk_dir_info == nullptr) 488 sdk_dir_info = GetSDKDirectoryForLatestOSVersion(); 489 if (sdk_dir_info) { 490 char path[PATH_MAX]; 491 if (sdk_dir_info->directory.GetPath(path, sizeof(path))) { 492 m_device_support_directory_for_os_version = path; 493 return m_device_support_directory_for_os_version.c_str(); 494 } 495 } else { 496 // Assign a single NULL character so we know we tried to find the device 497 // support directory and we don't keep trying to find it over and over. 498 m_device_support_directory_for_os_version.assign(1, '\0'); 499 } 500 } 501 // We should have put a single NULL character into 502 // m_device_support_directory_for_os_version 503 // or it should have a valid path if the code gets here 504 assert(m_device_support_directory_for_os_version.empty() == false); 505 if (m_device_support_directory_for_os_version[0]) 506 return m_device_support_directory_for_os_version.c_str(); 507 return nullptr; 508 } 509 510 uint32_t 511 PlatformRemoteAppleWatch::FindFileInAllSDKs(const char *platform_file_path, 512 FileSpecList &file_list) { 513 Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST | 514 LIBLLDB_LOG_VERBOSE); 515 if (platform_file_path && platform_file_path[0] && 516 UpdateSDKDirectoryInfosIfNeeded()) { 517 const uint32_t num_sdk_infos = m_sdk_directory_infos.size(); 518 lldb_private::FileSpec local_file; 519 // First try for an exact match of major, minor and update 520 for (uint32_t sdk_idx = 0; sdk_idx < num_sdk_infos; ++sdk_idx) { 521 if (log) { 522 log->Printf("Searching for %s in sdk path %s", platform_file_path, 523 m_sdk_directory_infos[sdk_idx].directory.GetPath().c_str()); 524 } 525 if (GetFileInSDK(platform_file_path, sdk_idx, local_file)) { 526 file_list.Append(local_file); 527 } 528 } 529 } 530 return file_list.GetSize(); 531 } 532 533 bool PlatformRemoteAppleWatch::GetFileInSDK( 534 const char *platform_file_path, uint32_t sdk_idx, 535 lldb_private::FileSpec &local_file) { 536 Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST); 537 if (sdk_idx < m_sdk_directory_infos.size()) { 538 std::string sdkroot_path = 539 m_sdk_directory_infos[sdk_idx].directory.GetPath(); 540 if (!sdkroot_path.empty() && platform_file_path && platform_file_path[0]) { 541 // We may need to interpose "/Symbols/" or "/Symbols.Internal/" between 542 // the 543 // SDK root directory and the file path. 544 545 const char *paths_to_try[] = {"Symbols", "", "Symbols.Internal", nullptr}; 546 for (size_t i = 0; paths_to_try[i] != nullptr; i++) { 547 local_file.SetFile(sdkroot_path, false); 548 if (paths_to_try[i][0] != '\0') 549 local_file.AppendPathComponent(paths_to_try[i]); 550 local_file.AppendPathComponent(platform_file_path); 551 local_file.ResolvePath(); 552 if (local_file.Exists()) { 553 if (log) 554 log->Printf("Found a copy of %s in the SDK dir %s/%s", 555 platform_file_path, sdkroot_path.c_str(), 556 paths_to_try[i]); 557 return true; 558 } 559 local_file.Clear(); 560 } 561 } 562 } 563 return false; 564 } 565 566 Error PlatformRemoteAppleWatch::GetSymbolFile(const FileSpec &platform_file, 567 const UUID *uuid_ptr, 568 FileSpec &local_file) { 569 Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST); 570 Error error; 571 char platform_file_path[PATH_MAX]; 572 if (platform_file.GetPath(platform_file_path, sizeof(platform_file_path))) { 573 char resolved_path[PATH_MAX]; 574 575 const char *os_version_dir = GetDeviceSupportDirectoryForOSVersion(); 576 if (os_version_dir) { 577 ::snprintf(resolved_path, sizeof(resolved_path), "%s/%s", os_version_dir, 578 platform_file_path); 579 580 local_file.SetFile(resolved_path, true); 581 if (local_file.Exists()) { 582 if (log) { 583 log->Printf("Found a copy of %s in the DeviceSupport dir %s", 584 platform_file_path, os_version_dir); 585 } 586 return error; 587 } 588 589 ::snprintf(resolved_path, sizeof(resolved_path), "%s/Symbols.Internal/%s", 590 os_version_dir, platform_file_path); 591 592 local_file.SetFile(resolved_path, true); 593 if (local_file.Exists()) { 594 if (log) { 595 log->Printf( 596 "Found a copy of %s in the DeviceSupport dir %s/Symbols.Internal", 597 platform_file_path, os_version_dir); 598 } 599 return error; 600 } 601 ::snprintf(resolved_path, sizeof(resolved_path), "%s/Symbols/%s", 602 os_version_dir, platform_file_path); 603 604 local_file.SetFile(resolved_path, true); 605 if (local_file.Exists()) { 606 if (log) { 607 log->Printf("Found a copy of %s in the DeviceSupport dir %s/Symbols", 608 platform_file_path, os_version_dir); 609 } 610 return error; 611 } 612 } 613 local_file = platform_file; 614 if (local_file.Exists()) 615 return error; 616 617 error.SetErrorStringWithFormat( 618 "unable to locate a platform file for '%s' in platform '%s'", 619 platform_file_path, GetPluginName().GetCString()); 620 } else { 621 error.SetErrorString("invalid platform file argument"); 622 } 623 return error; 624 } 625 626 Error PlatformRemoteAppleWatch::GetSharedModule( 627 const ModuleSpec &module_spec, lldb_private::Process *process, 628 ModuleSP &module_sp, const FileSpecList *module_search_paths_ptr, 629 ModuleSP *old_module_sp_ptr, bool *did_create_ptr) { 630 // For Apple Watch, the SDK files are all cached locally on the host 631 // system. So first we ask for the file in the cached SDK, 632 // then we attempt to get a shared module for the right architecture 633 // with the right UUID. 634 const FileSpec &platform_file = module_spec.GetFileSpec(); 635 Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST | 636 LIBLLDB_LOG_VERBOSE); 637 638 Error error; 639 char platform_file_path[PATH_MAX]; 640 641 if (platform_file.GetPath(platform_file_path, sizeof(platform_file_path))) { 642 ModuleSpec platform_module_spec(module_spec); 643 644 UpdateSDKDirectoryInfosIfNeeded(); 645 646 const uint32_t num_sdk_infos = m_sdk_directory_infos.size(); 647 648 // If we are connected we migth be able to correctly deduce the SDK 649 // directory 650 // using the OS build. 651 const uint32_t connected_sdk_idx = GetConnectedSDKIndex(); 652 if (connected_sdk_idx < num_sdk_infos) { 653 if (log) { 654 log->Printf("Searching for %s in sdk path %s", platform_file_path, 655 m_sdk_directory_infos[connected_sdk_idx] 656 .directory.GetPath() 657 .c_str()); 658 } 659 if (GetFileInSDK(platform_file_path, connected_sdk_idx, 660 platform_module_spec.GetFileSpec())) { 661 module_sp.reset(); 662 error = ResolveExecutable(platform_module_spec, module_sp, nullptr); 663 if (module_sp) { 664 m_last_module_sdk_idx = connected_sdk_idx; 665 error.Clear(); 666 return error; 667 } 668 } 669 } 670 671 // Try the last SDK index if it is set as most files from an SDK 672 // will tend to be valid in that same SDK. 673 if (m_last_module_sdk_idx < num_sdk_infos) { 674 if (log) { 675 log->Printf("Searching for %s in sdk path %s", platform_file_path, 676 m_sdk_directory_infos[m_last_module_sdk_idx] 677 .directory.GetPath() 678 .c_str()); 679 } 680 if (GetFileInSDK(platform_file_path, m_last_module_sdk_idx, 681 platform_module_spec.GetFileSpec())) { 682 module_sp.reset(); 683 error = ResolveExecutable(platform_module_spec, module_sp, nullptr); 684 if (module_sp) { 685 error.Clear(); 686 return error; 687 } 688 } 689 } 690 691 // First try for an exact match of major, minor and update 692 for (uint32_t sdk_idx = 0; sdk_idx < num_sdk_infos; ++sdk_idx) { 693 if (m_last_module_sdk_idx == sdk_idx) { 694 // Skip the last module SDK index if we already searched 695 // it above 696 continue; 697 } 698 if (log) { 699 log->Printf("Searching for %s in sdk path %s", platform_file_path, 700 m_sdk_directory_infos[sdk_idx].directory.GetPath().c_str()); 701 } 702 if (GetFileInSDK(platform_file_path, sdk_idx, 703 platform_module_spec.GetFileSpec())) { 704 // printf ("sdk[%u]: '%s'\n", sdk_idx, local_file.GetPath().c_str()); 705 706 error = ResolveExecutable(platform_module_spec, module_sp, nullptr); 707 if (module_sp) { 708 // Remember the index of the last SDK that we found a file 709 // in in case the wrong SDK was selected. 710 m_last_module_sdk_idx = sdk_idx; 711 error.Clear(); 712 return error; 713 } 714 } 715 } 716 } 717 // Not the module we are looking for... Nothing to see here... 718 module_sp.reset(); 719 720 // This may not be an SDK-related module. Try whether we can bring in the 721 // thing to our local cache. 722 error = GetSharedModuleWithLocalCache(module_spec, module_sp, 723 module_search_paths_ptr, 724 old_module_sp_ptr, did_create_ptr); 725 if (error.Success()) 726 return error; 727 728 // See if the file is present in any of the module_search_paths_ptr 729 // directories. 730 if (!module_sp && module_search_paths_ptr && platform_file) { 731 // create a vector of all the file / directory names in platform_file 732 // e.g. this might be 733 // /System/Library/PrivateFrameworks/UIFoundation.framework/UIFoundation 734 // 735 // We'll need to look in the module_search_paths_ptr directories for 736 // both "UIFoundation" and "UIFoundation.framework" -- most likely the 737 // latter will be the one we find there. 738 739 FileSpec platform_pull_apart(platform_file); 740 std::vector<std::string> path_parts; 741 ConstString unix_root_dir("/"); 742 while (true) { 743 ConstString part = platform_pull_apart.GetLastPathComponent(); 744 platform_pull_apart.RemoveLastPathComponent(); 745 if (part.IsEmpty() || part == unix_root_dir) 746 break; 747 path_parts.push_back(part.AsCString()); 748 } 749 const size_t path_parts_size = path_parts.size(); 750 751 size_t num_module_search_paths = module_search_paths_ptr->GetSize(); 752 for (size_t i = 0; i < num_module_search_paths; ++i) { 753 // Create a new FileSpec with this module_search_paths_ptr 754 // plus just the filename ("UIFoundation"), then the parent 755 // dir plus filename ("UIFoundation.framework/UIFoundation") 756 // etc - up to four names (to handle "Foo.framework/Contents/MacOS/Foo") 757 758 for (size_t j = 0; j < 4 && j < path_parts_size - 1; ++j) { 759 FileSpec path_to_try(module_search_paths_ptr->GetFileSpecAtIndex(i)); 760 761 // Add the components backwards. For 762 // .../PrivateFrameworks/UIFoundation.framework/UIFoundation 763 // path_parts is 764 // [0] UIFoundation 765 // [1] UIFoundation.framework 766 // [2] PrivateFrameworks 767 // 768 // and if 'j' is 2, we want to append path_parts[1] and then 769 // path_parts[0], aka 770 // 'UIFoundation.framework/UIFoundation', to the module_search_paths_ptr 771 // path. 772 773 for (int k = j; k >= 0; --k) { 774 path_to_try.AppendPathComponent(path_parts[k]); 775 } 776 777 if (path_to_try.Exists()) { 778 ModuleSpec new_module_spec(module_spec); 779 new_module_spec.GetFileSpec() = path_to_try; 780 Error new_error(Platform::GetSharedModule( 781 new_module_spec, process, module_sp, NULL, old_module_sp_ptr, 782 did_create_ptr)); 783 784 if (module_sp) { 785 module_sp->SetPlatformFileSpec(path_to_try); 786 return new_error; 787 } 788 } 789 } 790 } 791 } 792 793 const bool always_create = false; 794 error = ModuleList::GetSharedModule( 795 module_spec, module_sp, module_search_paths_ptr, old_module_sp_ptr, 796 did_create_ptr, always_create); 797 798 if (module_sp) 799 module_sp->SetPlatformFileSpec(platform_file); 800 801 return error; 802 } 803 804 bool PlatformRemoteAppleWatch::GetSupportedArchitectureAtIndex(uint32_t idx, 805 ArchSpec &arch) { 806 ArchSpec system_arch(GetSystemArchitecture()); 807 808 const ArchSpec::Core system_core = system_arch.GetCore(); 809 switch (system_core) { 810 default: 811 switch (idx) { 812 case 0: 813 arch.SetTriple("arm64-apple-watchos"); 814 return true; 815 case 1: 816 arch.SetTriple("armv7k-apple-watchos"); 817 return true; 818 case 2: 819 arch.SetTriple("armv7s-apple-watchos"); 820 return true; 821 case 3: 822 arch.SetTriple("armv7-apple-watchos"); 823 return true; 824 case 4: 825 arch.SetTriple("thumbv7k-apple-watchos"); 826 return true; 827 case 5: 828 arch.SetTriple("thumbv7-apple-watchos"); 829 return true; 830 case 6: 831 arch.SetTriple("thumbv7s-apple-watchos"); 832 return true; 833 default: 834 break; 835 } 836 break; 837 838 case ArchSpec::eCore_arm_arm64: 839 switch (idx) { 840 case 0: 841 arch.SetTriple("arm64-apple-watchos"); 842 return true; 843 case 1: 844 arch.SetTriple("armv7k-apple-watchos"); 845 return true; 846 case 2: 847 arch.SetTriple("armv7s-apple-watchos"); 848 return true; 849 case 3: 850 arch.SetTriple("armv7-apple-watchos"); 851 return true; 852 case 4: 853 arch.SetTriple("thumbv7k-apple-watchos"); 854 return true; 855 case 5: 856 arch.SetTriple("thumbv7-apple-watchos"); 857 return true; 858 case 6: 859 arch.SetTriple("thumbv7s-apple-watchos"); 860 return true; 861 default: 862 break; 863 } 864 break; 865 866 case ArchSpec::eCore_arm_armv7k: 867 switch (idx) { 868 case 0: 869 arch.SetTriple("armv7k-apple-watchos"); 870 return true; 871 case 1: 872 arch.SetTriple("armv7s-apple-watchos"); 873 return true; 874 case 2: 875 arch.SetTriple("armv7-apple-watchos"); 876 return true; 877 case 3: 878 arch.SetTriple("thumbv7k-apple-watchos"); 879 return true; 880 case 4: 881 arch.SetTriple("thumbv7-apple-watchos"); 882 return true; 883 case 5: 884 arch.SetTriple("thumbv7s-apple-watchos"); 885 return true; 886 default: 887 break; 888 } 889 break; 890 891 case ArchSpec::eCore_arm_armv7s: 892 switch (idx) { 893 case 0: 894 arch.SetTriple("armv7s-apple-watchos"); 895 return true; 896 case 1: 897 arch.SetTriple("armv7k-apple-watchos"); 898 return true; 899 case 2: 900 arch.SetTriple("armv7-apple-watchos"); 901 return true; 902 case 3: 903 arch.SetTriple("thumbv7k-apple-watchos"); 904 return true; 905 case 4: 906 arch.SetTriple("thumbv7-apple-watchos"); 907 return true; 908 case 5: 909 arch.SetTriple("thumbv7s-apple-watchos"); 910 return true; 911 default: 912 break; 913 } 914 break; 915 916 case ArchSpec::eCore_arm_armv7: 917 switch (idx) { 918 case 0: 919 arch.SetTriple("armv7-apple-watchos"); 920 return true; 921 case 1: 922 arch.SetTriple("armv7k-apple-watchos"); 923 return true; 924 case 2: 925 arch.SetTriple("thumbv7k-apple-watchos"); 926 return true; 927 case 3: 928 arch.SetTriple("thumbv7-apple-watchos"); 929 return true; 930 default: 931 break; 932 } 933 break; 934 } 935 arch.Clear(); 936 return false; 937 } 938 939 uint32_t PlatformRemoteAppleWatch::GetConnectedSDKIndex() { 940 if (IsConnected()) { 941 if (m_connected_module_sdk_idx == UINT32_MAX) { 942 std::string build; 943 if (GetRemoteOSBuildString(build)) { 944 const uint32_t num_sdk_infos = m_sdk_directory_infos.size(); 945 for (uint32_t i = 0; i < num_sdk_infos; ++i) { 946 const SDKDirectoryInfo &sdk_dir_info = m_sdk_directory_infos[i]; 947 if (strstr(sdk_dir_info.directory.GetFilename().AsCString(""), 948 build.c_str())) { 949 m_connected_module_sdk_idx = i; 950 } 951 } 952 } 953 } 954 } else { 955 m_connected_module_sdk_idx = UINT32_MAX; 956 } 957 return m_connected_module_sdk_idx; 958 } 959