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