1 //===-- Platform.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 "lldb/Target/Platform.h" 11 12 // C Includes 13 // C++ Includes 14 // Other libraries and framework includes 15 // Project includes 16 #include "lldb/Breakpoint/BreakpointIDList.h" 17 #include "lldb/Core/Error.h" 18 #include "lldb/Core/Log.h" 19 #include "lldb/Core/ModuleSpec.h" 20 #include "lldb/Core/PluginManager.h" 21 #include "lldb/Host/FileSpec.h" 22 #include "lldb/Host/Host.h" 23 #include "lldb/Target/Process.h" 24 #include "lldb/Target/Target.h" 25 #include "lldb/Utility/Utils.h" 26 27 using namespace lldb; 28 using namespace lldb_private; 29 30 // Use a singleton function for g_local_platform_sp to avoid init 31 // constructors since LLDB is often part of a shared library 32 static PlatformSP& 33 GetDefaultPlatformSP () 34 { 35 static PlatformSP g_default_platform_sp; 36 return g_default_platform_sp; 37 } 38 39 static Mutex & 40 GetConnectedPlatformListMutex () 41 { 42 static Mutex g_remote_connected_platforms_mutex (Mutex::eMutexTypeRecursive); 43 return g_remote_connected_platforms_mutex; 44 } 45 static std::vector<PlatformSP> & 46 GetConnectedPlatformList () 47 { 48 static std::vector<PlatformSP> g_remote_connected_platforms; 49 return g_remote_connected_platforms; 50 } 51 52 53 const char * 54 Platform::GetHostPlatformName () 55 { 56 return "host"; 57 } 58 59 //------------------------------------------------------------------ 60 /// Get the native host platform plug-in. 61 /// 62 /// There should only be one of these for each host that LLDB runs 63 /// upon that should be statically compiled in and registered using 64 /// preprocessor macros or other similar build mechanisms. 65 /// 66 /// This platform will be used as the default platform when launching 67 /// or attaching to processes unless another platform is specified. 68 //------------------------------------------------------------------ 69 PlatformSP 70 Platform::GetDefaultPlatform () 71 { 72 return GetDefaultPlatformSP (); 73 } 74 75 void 76 Platform::SetDefaultPlatform (const lldb::PlatformSP &platform_sp) 77 { 78 // The native platform should use its static void Platform::Initialize() 79 // function to register itself as the native platform. 80 GetDefaultPlatformSP () = platform_sp; 81 } 82 83 Error 84 Platform::GetFileWithUUID (const FileSpec &platform_file, 85 const UUID *uuid_ptr, 86 FileSpec &local_file) 87 { 88 // Default to the local case 89 local_file = platform_file; 90 return Error(); 91 } 92 93 FileSpecList 94 Platform::LocateExecutableScriptingResources (Target *target, Module &module) 95 { 96 return FileSpecList(); 97 } 98 99 Platform* 100 Platform::FindPlugin (Process *process, const ConstString &plugin_name) 101 { 102 PlatformCreateInstance create_callback = NULL; 103 if (plugin_name) 104 { 105 create_callback = PluginManager::GetPlatformCreateCallbackForPluginName (plugin_name); 106 if (create_callback) 107 { 108 ArchSpec arch; 109 if (process) 110 { 111 arch = process->GetTarget().GetArchitecture(); 112 } 113 std::unique_ptr<Platform> instance_ap(create_callback(process, &arch)); 114 if (instance_ap.get()) 115 return instance_ap.release(); 116 } 117 } 118 else 119 { 120 for (uint32_t idx = 0; (create_callback = PluginManager::GetPlatformCreateCallbackAtIndex(idx)) != NULL; ++idx) 121 { 122 std::unique_ptr<Platform> instance_ap(create_callback(process, nullptr)); 123 if (instance_ap.get()) 124 return instance_ap.release(); 125 } 126 } 127 return NULL; 128 } 129 130 Error 131 Platform::GetSharedModule (const ModuleSpec &module_spec, 132 ModuleSP &module_sp, 133 const FileSpecList *module_search_paths_ptr, 134 ModuleSP *old_module_sp_ptr, 135 bool *did_create_ptr) 136 { 137 // Don't do any path remapping for the default implementation 138 // of the platform GetSharedModule function, just call through 139 // to our static ModuleList function. Platform subclasses that 140 // implement remote debugging, might have a developer kits 141 // installed that have cached versions of the files for the 142 // remote target, or might implement a download and cache 143 // locally implementation. 144 const bool always_create = false; 145 return ModuleList::GetSharedModule (module_spec, 146 module_sp, 147 module_search_paths_ptr, 148 old_module_sp_ptr, 149 did_create_ptr, 150 always_create); 151 } 152 153 PlatformSP 154 Platform::Create (const char *platform_name, Error &error) 155 { 156 PlatformCreateInstance create_callback = NULL; 157 lldb::PlatformSP platform_sp; 158 if (platform_name && platform_name[0]) 159 { 160 ConstString const_platform_name (platform_name); 161 create_callback = PluginManager::GetPlatformCreateCallbackForPluginName (const_platform_name); 162 if (create_callback) 163 platform_sp.reset(create_callback(true, NULL)); 164 else 165 error.SetErrorStringWithFormat ("unable to find a plug-in for the platform named \"%s\"", platform_name); 166 } 167 else 168 error.SetErrorString ("invalid platform name"); 169 return platform_sp; 170 } 171 172 173 PlatformSP 174 Platform::Create (const ArchSpec &arch, ArchSpec *platform_arch_ptr, Error &error) 175 { 176 lldb::PlatformSP platform_sp; 177 if (arch.IsValid()) 178 { 179 uint32_t idx; 180 PlatformCreateInstance create_callback; 181 // First try exact arch matches across all platform plug-ins 182 bool exact = true; 183 for (idx = 0; (create_callback = PluginManager::GetPlatformCreateCallbackAtIndex (idx)); ++idx) 184 { 185 if (create_callback) 186 { 187 platform_sp.reset(create_callback(false, &arch)); 188 if (platform_sp && platform_sp->IsCompatibleArchitecture(arch, exact, platform_arch_ptr)) 189 return platform_sp; 190 } 191 } 192 // Next try compatible arch matches across all platform plug-ins 193 exact = false; 194 for (idx = 0; (create_callback = PluginManager::GetPlatformCreateCallbackAtIndex (idx)); ++idx) 195 { 196 if (create_callback) 197 { 198 platform_sp.reset(create_callback(false, &arch)); 199 if (platform_sp && platform_sp->IsCompatibleArchitecture(arch, exact, platform_arch_ptr)) 200 return platform_sp; 201 } 202 } 203 } 204 else 205 error.SetErrorString ("invalid platform name"); 206 if (platform_arch_ptr) 207 platform_arch_ptr->Clear(); 208 platform_sp.reset(); 209 return platform_sp; 210 } 211 212 uint32_t 213 Platform::GetNumConnectedRemotePlatforms () 214 { 215 Mutex::Locker locker (GetConnectedPlatformListMutex ()); 216 return GetConnectedPlatformList().size(); 217 } 218 219 PlatformSP 220 Platform::GetConnectedRemotePlatformAtIndex (uint32_t idx) 221 { 222 PlatformSP platform_sp; 223 { 224 Mutex::Locker locker (GetConnectedPlatformListMutex ()); 225 if (idx < GetConnectedPlatformList().size()) 226 platform_sp = GetConnectedPlatformList ()[idx]; 227 } 228 return platform_sp; 229 } 230 231 //------------------------------------------------------------------ 232 /// Default Constructor 233 //------------------------------------------------------------------ 234 Platform::Platform (bool is_host) : 235 m_is_host (is_host), 236 m_os_version_set_while_connected (false), 237 m_system_arch_set_while_connected (false), 238 m_sdk_sysroot (), 239 m_sdk_build (), 240 m_working_dir (), 241 m_remote_url (), 242 m_name (), 243 m_major_os_version (UINT32_MAX), 244 m_minor_os_version (UINT32_MAX), 245 m_update_os_version (UINT32_MAX), 246 m_system_arch(), 247 m_uid_map_mutex (Mutex::eMutexTypeNormal), 248 m_gid_map_mutex (Mutex::eMutexTypeNormal), 249 m_uid_map(), 250 m_gid_map(), 251 m_max_uid_name_len (0), 252 m_max_gid_name_len (0), 253 m_supports_rsync (false), 254 m_rsync_opts (), 255 m_rsync_prefix (), 256 m_supports_ssh (false), 257 m_ssh_opts (), 258 m_ignores_remote_hostname (false), 259 m_trap_handlers(), 260 m_calculated_trap_handlers (false), 261 m_trap_handler_mutex() 262 { 263 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); 264 if (log) 265 log->Printf ("%p Platform::Platform()", static_cast<void*>(this)); 266 } 267 268 //------------------------------------------------------------------ 269 /// Destructor. 270 /// 271 /// The destructor is virtual since this class is designed to be 272 /// inherited from by the plug-in instance. 273 //------------------------------------------------------------------ 274 Platform::~Platform() 275 { 276 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); 277 if (log) 278 log->Printf ("%p Platform::~Platform()", static_cast<void*>(this)); 279 } 280 281 void 282 Platform::GetStatus (Stream &strm) 283 { 284 uint32_t major = UINT32_MAX; 285 uint32_t minor = UINT32_MAX; 286 uint32_t update = UINT32_MAX; 287 std::string s; 288 strm.Printf (" Platform: %s\n", GetPluginName().GetCString()); 289 290 ArchSpec arch (GetSystemArchitecture()); 291 if (arch.IsValid()) 292 { 293 if (!arch.GetTriple().str().empty()) 294 strm.Printf(" Triple: %s\n", arch.GetTriple().str().c_str()); 295 } 296 297 if (GetOSVersion(major, minor, update)) 298 { 299 strm.Printf("OS Version: %u", major); 300 if (minor != UINT32_MAX) 301 strm.Printf(".%u", minor); 302 if (update != UINT32_MAX) 303 strm.Printf(".%u", update); 304 305 if (GetOSBuildString (s)) 306 strm.Printf(" (%s)", s.c_str()); 307 308 strm.EOL(); 309 } 310 311 if (GetOSKernelDescription (s)) 312 strm.Printf(" Kernel: %s\n", s.c_str()); 313 314 if (IsHost()) 315 { 316 strm.Printf(" Hostname: %s\n", GetHostname()); 317 } 318 else 319 { 320 const bool is_connected = IsConnected(); 321 if (is_connected) 322 strm.Printf(" Hostname: %s\n", GetHostname()); 323 strm.Printf(" Connected: %s\n", is_connected ? "yes" : "no"); 324 } 325 326 if (GetWorkingDirectory()) 327 { 328 strm.Printf("WorkingDir: %s\n", GetWorkingDirectory().GetCString()); 329 } 330 if (!IsConnected()) 331 return; 332 333 std::string specific_info(GetPlatformSpecificConnectionInformation()); 334 335 if (specific_info.empty() == false) 336 strm.Printf("Platform-specific connection: %s\n", specific_info.c_str()); 337 } 338 339 340 bool 341 Platform::GetOSVersion (uint32_t &major, 342 uint32_t &minor, 343 uint32_t &update) 344 { 345 bool success = m_major_os_version != UINT32_MAX; 346 if (IsHost()) 347 { 348 if (!success) 349 { 350 // We have a local host platform 351 success = Host::GetOSVersion (m_major_os_version, 352 m_minor_os_version, 353 m_update_os_version); 354 m_os_version_set_while_connected = success; 355 } 356 } 357 else 358 { 359 // We have a remote platform. We can only fetch the remote 360 // OS version if we are connected, and we don't want to do it 361 // more than once. 362 363 const bool is_connected = IsConnected(); 364 365 bool fetch = false; 366 if (success) 367 { 368 // We have valid OS version info, check to make sure it wasn't 369 // manually set prior to connecting. If it was manually set prior 370 // to connecting, then lets fetch the actual OS version info 371 // if we are now connected. 372 if (is_connected && !m_os_version_set_while_connected) 373 fetch = true; 374 } 375 else 376 { 377 // We don't have valid OS version info, fetch it if we are connected 378 fetch = is_connected; 379 } 380 381 if (fetch) 382 { 383 success = GetRemoteOSVersion (); 384 m_os_version_set_while_connected = success; 385 } 386 } 387 388 if (success) 389 { 390 major = m_major_os_version; 391 minor = m_minor_os_version; 392 update = m_update_os_version; 393 } 394 return success; 395 } 396 397 bool 398 Platform::GetOSBuildString (std::string &s) 399 { 400 if (IsHost()) 401 return Host::GetOSBuildString (s); 402 else 403 return GetRemoteOSBuildString (s); 404 } 405 406 bool 407 Platform::GetOSKernelDescription (std::string &s) 408 { 409 if (IsHost()) 410 return Host::GetOSKernelDescription (s); 411 else 412 return GetRemoteOSKernelDescription (s); 413 } 414 415 ConstString 416 Platform::GetWorkingDirectory () 417 { 418 if (IsHost()) 419 { 420 char cwd[PATH_MAX]; 421 if (getcwd(cwd, sizeof(cwd))) 422 return ConstString(cwd); 423 else 424 return ConstString(); 425 } 426 else 427 { 428 if (!m_working_dir) 429 m_working_dir = GetRemoteWorkingDirectory(); 430 return m_working_dir; 431 } 432 } 433 434 435 struct RecurseCopyBaton 436 { 437 const FileSpec& dst; 438 Platform *platform_ptr; 439 Error error; 440 }; 441 442 443 static FileSpec::EnumerateDirectoryResult 444 RecurseCopy_Callback (void *baton, 445 FileSpec::FileType file_type, 446 const FileSpec &src) 447 { 448 RecurseCopyBaton* rc_baton = (RecurseCopyBaton*)baton; 449 switch (file_type) 450 { 451 case FileSpec::eFileTypePipe: 452 case FileSpec::eFileTypeSocket: 453 // we have no way to copy pipes and sockets - ignore them and continue 454 return FileSpec::eEnumerateDirectoryResultNext; 455 break; 456 457 case FileSpec::eFileTypeDirectory: 458 { 459 // make the new directory and get in there 460 FileSpec dst_dir = rc_baton->dst; 461 if (!dst_dir.GetFilename()) 462 dst_dir.GetFilename() = src.GetLastPathComponent(); 463 std::string dst_dir_path (dst_dir.GetPath()); 464 Error error = rc_baton->platform_ptr->MakeDirectory(dst_dir_path.c_str(), lldb::eFilePermissionsDirectoryDefault); 465 if (error.Fail()) 466 { 467 rc_baton->error.SetErrorStringWithFormat("unable to setup directory %s on remote end", dst_dir_path.c_str()); 468 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out 469 } 470 471 // now recurse 472 std::string src_dir_path (src.GetPath()); 473 474 // Make a filespec that only fills in the directory of a FileSpec so 475 // when we enumerate we can quickly fill in the filename for dst copies 476 FileSpec recurse_dst; 477 recurse_dst.GetDirectory().SetCString(dst_dir.GetPath().c_str()); 478 RecurseCopyBaton rc_baton2 = { recurse_dst, rc_baton->platform_ptr, Error() }; 479 FileSpec::EnumerateDirectory(src_dir_path.c_str(), true, true, true, RecurseCopy_Callback, &rc_baton2); 480 if (rc_baton2.error.Fail()) 481 { 482 rc_baton->error.SetErrorString(rc_baton2.error.AsCString()); 483 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out 484 } 485 return FileSpec::eEnumerateDirectoryResultNext; 486 } 487 break; 488 489 case FileSpec::eFileTypeSymbolicLink: 490 { 491 // copy the file and keep going 492 FileSpec dst_file = rc_baton->dst; 493 if (!dst_file.GetFilename()) 494 dst_file.GetFilename() = src.GetFilename(); 495 496 char buf[PATH_MAX]; 497 498 rc_baton->error = Host::Readlink (src.GetPath().c_str(), buf, sizeof(buf)); 499 500 if (rc_baton->error.Fail()) 501 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out 502 503 rc_baton->error = rc_baton->platform_ptr->CreateSymlink(dst_file.GetPath().c_str(), buf); 504 505 if (rc_baton->error.Fail()) 506 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out 507 508 return FileSpec::eEnumerateDirectoryResultNext; 509 } 510 break; 511 case FileSpec::eFileTypeRegular: 512 { 513 // copy the file and keep going 514 FileSpec dst_file = rc_baton->dst; 515 if (!dst_file.GetFilename()) 516 dst_file.GetFilename() = src.GetFilename(); 517 Error err = rc_baton->platform_ptr->PutFile(src, dst_file); 518 if (err.Fail()) 519 { 520 rc_baton->error.SetErrorString(err.AsCString()); 521 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out 522 } 523 return FileSpec::eEnumerateDirectoryResultNext; 524 } 525 break; 526 527 case FileSpec::eFileTypeInvalid: 528 case FileSpec::eFileTypeOther: 529 case FileSpec::eFileTypeUnknown: 530 default: 531 rc_baton->error.SetErrorStringWithFormat("invalid file detected during copy: %s", src.GetPath().c_str()); 532 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out 533 break; 534 } 535 } 536 537 Error 538 Platform::Install (const FileSpec& src, const FileSpec& dst) 539 { 540 Error error; 541 542 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM); 543 if (log) 544 log->Printf ("Platform::Install (src='%s', dst='%s')", src.GetPath().c_str(), dst.GetPath().c_str()); 545 FileSpec fixed_dst(dst); 546 547 if (!fixed_dst.GetFilename()) 548 fixed_dst.GetFilename() = src.GetFilename(); 549 550 ConstString working_dir = GetWorkingDirectory(); 551 552 if (dst) 553 { 554 if (dst.GetDirectory()) 555 { 556 const char first_dst_dir_char = dst.GetDirectory().GetCString()[0]; 557 if (first_dst_dir_char == '/' || first_dst_dir_char == '\\') 558 { 559 fixed_dst.GetDirectory() = dst.GetDirectory(); 560 } 561 // If the fixed destination file doesn't have a directory yet, 562 // then we must have a relative path. We will resolve this relative 563 // path against the platform's working directory 564 if (!fixed_dst.GetDirectory()) 565 { 566 FileSpec relative_spec; 567 std::string path; 568 if (working_dir) 569 { 570 relative_spec.SetFile(working_dir.GetCString(), false); 571 relative_spec.AppendPathComponent(dst.GetPath().c_str()); 572 fixed_dst.GetDirectory() = relative_spec.GetDirectory(); 573 } 574 else 575 { 576 error.SetErrorStringWithFormat("platform working directory must be valid for relative path '%s'", dst.GetPath().c_str()); 577 return error; 578 } 579 } 580 } 581 else 582 { 583 if (working_dir) 584 { 585 fixed_dst.GetDirectory() = working_dir; 586 } 587 else 588 { 589 error.SetErrorStringWithFormat("platform working directory must be valid for relative path '%s'", dst.GetPath().c_str()); 590 return error; 591 } 592 } 593 } 594 else 595 { 596 if (working_dir) 597 { 598 fixed_dst.GetDirectory() = working_dir; 599 } 600 else 601 { 602 error.SetErrorStringWithFormat("platform working directory must be valid when destination directory is empty"); 603 return error; 604 } 605 } 606 607 if (log) 608 log->Printf ("Platform::Install (src='%s', dst='%s') fixed_dst='%s'", src.GetPath().c_str(), dst.GetPath().c_str(), fixed_dst.GetPath().c_str()); 609 610 if (GetSupportsRSync()) 611 { 612 error = PutFile(src, dst); 613 } 614 else 615 { 616 switch (src.GetFileType()) 617 { 618 case FileSpec::eFileTypeDirectory: 619 { 620 if (GetFileExists (fixed_dst)) 621 Unlink (fixed_dst.GetPath().c_str()); 622 uint32_t permissions = src.GetPermissions(); 623 if (permissions == 0) 624 permissions = eFilePermissionsDirectoryDefault; 625 std::string dst_dir_path(fixed_dst.GetPath()); 626 error = MakeDirectory(dst_dir_path.c_str(), permissions); 627 if (error.Success()) 628 { 629 // Make a filespec that only fills in the directory of a FileSpec so 630 // when we enumerate we can quickly fill in the filename for dst copies 631 FileSpec recurse_dst; 632 recurse_dst.GetDirectory().SetCString(dst_dir_path.c_str()); 633 std::string src_dir_path (src.GetPath()); 634 RecurseCopyBaton baton = { recurse_dst, this, Error() }; 635 FileSpec::EnumerateDirectory(src_dir_path.c_str(), true, true, true, RecurseCopy_Callback, &baton); 636 return baton.error; 637 } 638 } 639 break; 640 641 case FileSpec::eFileTypeRegular: 642 if (GetFileExists (fixed_dst)) 643 Unlink (fixed_dst.GetPath().c_str()); 644 error = PutFile(src, fixed_dst); 645 break; 646 647 case FileSpec::eFileTypeSymbolicLink: 648 { 649 if (GetFileExists (fixed_dst)) 650 Unlink (fixed_dst.GetPath().c_str()); 651 char buf[PATH_MAX]; 652 error = Host::Readlink(src.GetPath().c_str(), buf, sizeof(buf)); 653 if (error.Success()) 654 error = CreateSymlink(dst.GetPath().c_str(), buf); 655 } 656 break; 657 case FileSpec::eFileTypePipe: 658 error.SetErrorString("platform install doesn't handle pipes"); 659 break; 660 case FileSpec::eFileTypeSocket: 661 error.SetErrorString("platform install doesn't handle sockets"); 662 break; 663 case FileSpec::eFileTypeInvalid: 664 case FileSpec::eFileTypeUnknown: 665 case FileSpec::eFileTypeOther: 666 error.SetErrorString("platform install doesn't handle non file or directory items"); 667 break; 668 } 669 } 670 return error; 671 } 672 673 bool 674 Platform::SetWorkingDirectory (const ConstString &path) 675 { 676 if (IsHost()) 677 { 678 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM); 679 if (log) 680 log->Printf("Platform::SetWorkingDirectory('%s')", path.GetCString()); 681 #ifdef _WIN32 682 // Not implemented on Windows 683 return false; 684 #else 685 if (path) 686 { 687 if (chdir(path.GetCString()) == 0) 688 return true; 689 } 690 return false; 691 #endif 692 } 693 else 694 { 695 m_working_dir.Clear(); 696 return SetRemoteWorkingDirectory(path); 697 } 698 } 699 700 Error 701 Platform::MakeDirectory (const char *path, uint32_t permissions) 702 { 703 if (IsHost()) 704 return Host::MakeDirectory (path, permissions); 705 else 706 { 707 Error error; 708 error.SetErrorStringWithFormat("remote platform %s doesn't support %s", GetPluginName().GetCString(), __PRETTY_FUNCTION__); 709 return error; 710 } 711 } 712 713 Error 714 Platform::GetFilePermissions (const char *path, uint32_t &file_permissions) 715 { 716 if (IsHost()) 717 return Host::GetFilePermissions(path, file_permissions); 718 else 719 { 720 Error error; 721 error.SetErrorStringWithFormat("remote platform %s doesn't support %s", GetPluginName().GetCString(), __PRETTY_FUNCTION__); 722 return error; 723 } 724 } 725 726 Error 727 Platform::SetFilePermissions (const char *path, uint32_t file_permissions) 728 { 729 if (IsHost()) 730 return Host::SetFilePermissions(path, file_permissions); 731 else 732 { 733 Error error; 734 error.SetErrorStringWithFormat("remote platform %s doesn't support %s", GetPluginName().GetCString(), __PRETTY_FUNCTION__); 735 return error; 736 } 737 } 738 739 ConstString 740 Platform::GetName () 741 { 742 return GetPluginName(); 743 } 744 745 const char * 746 Platform::GetHostname () 747 { 748 if (IsHost()) 749 return "127.0.0.1"; 750 751 if (m_name.empty()) 752 return NULL; 753 return m_name.c_str(); 754 } 755 756 bool 757 Platform::SetRemoteWorkingDirectory(const ConstString &path) 758 { 759 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM); 760 if (log) 761 log->Printf("Platform::SetRemoteWorkingDirectory('%s')", path.GetCString()); 762 m_working_dir = path; 763 return true; 764 } 765 766 const char * 767 Platform::GetUserName (uint32_t uid) 768 { 769 const char *user_name = GetCachedUserName(uid); 770 if (user_name) 771 return user_name; 772 if (IsHost()) 773 { 774 std::string name; 775 if (Host::GetUserName(uid, name)) 776 return SetCachedUserName (uid, name.c_str(), name.size()); 777 } 778 return NULL; 779 } 780 781 const char * 782 Platform::GetGroupName (uint32_t gid) 783 { 784 const char *group_name = GetCachedGroupName(gid); 785 if (group_name) 786 return group_name; 787 if (IsHost()) 788 { 789 std::string name; 790 if (Host::GetGroupName(gid, name)) 791 return SetCachedGroupName (gid, name.c_str(), name.size()); 792 } 793 return NULL; 794 } 795 796 bool 797 Platform::SetOSVersion (uint32_t major, 798 uint32_t minor, 799 uint32_t update) 800 { 801 if (IsHost()) 802 { 803 // We don't need anyone setting the OS version for the host platform, 804 // we should be able to figure it out by calling Host::GetOSVersion(...). 805 return false; 806 } 807 else 808 { 809 // We have a remote platform, allow setting the target OS version if 810 // we aren't connected, since if we are connected, we should be able to 811 // request the remote OS version from the connected platform. 812 if (IsConnected()) 813 return false; 814 else 815 { 816 // We aren't connected and we might want to set the OS version 817 // ahead of time before we connect so we can peruse files and 818 // use a local SDK or PDK cache of support files to disassemble 819 // or do other things. 820 m_major_os_version = major; 821 m_minor_os_version = minor; 822 m_update_os_version = update; 823 return true; 824 } 825 } 826 return false; 827 } 828 829 830 Error 831 Platform::ResolveExecutable (const FileSpec &exe_file, 832 const ArchSpec &exe_arch, 833 lldb::ModuleSP &exe_module_sp, 834 const FileSpecList *module_search_paths_ptr) 835 { 836 Error error; 837 if (exe_file.Exists()) 838 { 839 ModuleSpec module_spec (exe_file, exe_arch); 840 if (module_spec.GetArchitecture().IsValid()) 841 { 842 error = ModuleList::GetSharedModule (module_spec, 843 exe_module_sp, 844 module_search_paths_ptr, 845 NULL, 846 NULL); 847 } 848 else 849 { 850 // No valid architecture was specified, ask the platform for 851 // the architectures that we should be using (in the correct order) 852 // and see if we can find a match that way 853 for (uint32_t idx = 0; GetSupportedArchitectureAtIndex (idx, module_spec.GetArchitecture()); ++idx) 854 { 855 error = ModuleList::GetSharedModule (module_spec, 856 exe_module_sp, 857 module_search_paths_ptr, 858 NULL, 859 NULL); 860 // Did we find an executable using one of the 861 if (error.Success() && exe_module_sp) 862 break; 863 } 864 } 865 } 866 else 867 { 868 error.SetErrorStringWithFormat ("'%s' does not exist", 869 exe_file.GetPath().c_str()); 870 } 871 return error; 872 } 873 874 Error 875 Platform::ResolveSymbolFile (Target &target, 876 const ModuleSpec &sym_spec, 877 FileSpec &sym_file) 878 { 879 Error error; 880 if (sym_spec.GetSymbolFileSpec().Exists()) 881 sym_file = sym_spec.GetSymbolFileSpec(); 882 else 883 error.SetErrorString("unable to resolve symbol file"); 884 return error; 885 886 } 887 888 889 890 bool 891 Platform::ResolveRemotePath (const FileSpec &platform_path, 892 FileSpec &resolved_platform_path) 893 { 894 resolved_platform_path = platform_path; 895 return resolved_platform_path.ResolvePath(); 896 } 897 898 899 const ArchSpec & 900 Platform::GetSystemArchitecture() 901 { 902 if (IsHost()) 903 { 904 if (!m_system_arch.IsValid()) 905 { 906 // We have a local host platform 907 m_system_arch = Host::GetArchitecture(); 908 m_system_arch_set_while_connected = m_system_arch.IsValid(); 909 } 910 } 911 else 912 { 913 // We have a remote platform. We can only fetch the remote 914 // system architecture if we are connected, and we don't want to do it 915 // more than once. 916 917 const bool is_connected = IsConnected(); 918 919 bool fetch = false; 920 if (m_system_arch.IsValid()) 921 { 922 // We have valid OS version info, check to make sure it wasn't 923 // manually set prior to connecting. If it was manually set prior 924 // to connecting, then lets fetch the actual OS version info 925 // if we are now connected. 926 if (is_connected && !m_system_arch_set_while_connected) 927 fetch = true; 928 } 929 else 930 { 931 // We don't have valid OS version info, fetch it if we are connected 932 fetch = is_connected; 933 } 934 935 if (fetch) 936 { 937 m_system_arch = GetRemoteSystemArchitecture (); 938 m_system_arch_set_while_connected = m_system_arch.IsValid(); 939 } 940 } 941 return m_system_arch; 942 } 943 944 945 Error 946 Platform::ConnectRemote (Args& args) 947 { 948 Error error; 949 if (IsHost()) 950 error.SetErrorStringWithFormat ("The currently selected platform (%s) is the host platform and is always connected.", GetPluginName().GetCString()); 951 else 952 error.SetErrorStringWithFormat ("Platform::ConnectRemote() is not supported by %s", GetPluginName().GetCString()); 953 return error; 954 } 955 956 Error 957 Platform::DisconnectRemote () 958 { 959 Error error; 960 if (IsHost()) 961 error.SetErrorStringWithFormat ("The currently selected platform (%s) is the host platform and is always connected.", GetPluginName().GetCString()); 962 else 963 error.SetErrorStringWithFormat ("Platform::DisconnectRemote() is not supported by %s", GetPluginName().GetCString()); 964 return error; 965 } 966 967 bool 968 Platform::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info) 969 { 970 // Take care of the host case so that each subclass can just 971 // call this function to get the host functionality. 972 if (IsHost()) 973 return Host::GetProcessInfo (pid, process_info); 974 return false; 975 } 976 977 uint32_t 978 Platform::FindProcesses (const ProcessInstanceInfoMatch &match_info, 979 ProcessInstanceInfoList &process_infos) 980 { 981 // Take care of the host case so that each subclass can just 982 // call this function to get the host functionality. 983 uint32_t match_count = 0; 984 if (IsHost()) 985 match_count = Host::FindProcesses (match_info, process_infos); 986 return match_count; 987 } 988 989 990 Error 991 Platform::LaunchProcess (ProcessLaunchInfo &launch_info) 992 { 993 Error error; 994 // Take care of the host case so that each subclass can just 995 // call this function to get the host functionality. 996 if (IsHost()) 997 { 998 if (::getenv ("LLDB_LAUNCH_FLAG_LAUNCH_IN_TTY")) 999 launch_info.GetFlags().Set (eLaunchFlagLaunchInTTY); 1000 1001 if (launch_info.GetFlags().Test (eLaunchFlagLaunchInShell)) 1002 { 1003 const bool is_localhost = true; 1004 const bool will_debug = launch_info.GetFlags().Test(eLaunchFlagDebug); 1005 const bool first_arg_is_full_shell_command = false; 1006 uint32_t num_resumes = GetResumeCountForLaunchInfo (launch_info); 1007 if (!launch_info.ConvertArgumentsForLaunchingInShell (error, 1008 is_localhost, 1009 will_debug, 1010 first_arg_is_full_shell_command, 1011 num_resumes)) 1012 return error; 1013 } 1014 1015 error = Host::LaunchProcess (launch_info); 1016 } 1017 else 1018 error.SetErrorString ("base lldb_private::Platform class can't launch remote processes"); 1019 return error; 1020 } 1021 1022 lldb::ProcessSP 1023 Platform::DebugProcess (ProcessLaunchInfo &launch_info, 1024 Debugger &debugger, 1025 Target *target, // Can be NULL, if NULL create a new target, else use existing one 1026 Listener &listener, 1027 Error &error) 1028 { 1029 ProcessSP process_sp; 1030 // Make sure we stop at the entry point 1031 launch_info.GetFlags ().Set (eLaunchFlagDebug); 1032 // We always launch the process we are going to debug in a separate process 1033 // group, since then we can handle ^C interrupts ourselves w/o having to worry 1034 // about the target getting them as well. 1035 launch_info.SetLaunchInSeparateProcessGroup(true); 1036 1037 error = LaunchProcess (launch_info); 1038 if (error.Success()) 1039 { 1040 if (launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID) 1041 { 1042 ProcessAttachInfo attach_info (launch_info); 1043 process_sp = Attach (attach_info, debugger, target, listener, error); 1044 if (process_sp) 1045 { 1046 launch_info.SetHijackListener(attach_info.GetHijackListener()); 1047 1048 // Since we attached to the process, it will think it needs to detach 1049 // if the process object just goes away without an explicit call to 1050 // Process::Kill() or Process::Detach(), so let it know to kill the 1051 // process if this happens. 1052 process_sp->SetShouldDetach (false); 1053 1054 // If we didn't have any file actions, the pseudo terminal might 1055 // have been used where the slave side was given as the file to 1056 // open for stdin/out/err after we have already opened the master 1057 // so we can read/write stdin/out/err. 1058 int pty_fd = launch_info.GetPTY().ReleaseMasterFileDescriptor(); 1059 if (pty_fd != lldb_utility::PseudoTerminal::invalid_fd) 1060 { 1061 process_sp->SetSTDIOFileDescriptor(pty_fd); 1062 } 1063 } 1064 } 1065 } 1066 return process_sp; 1067 } 1068 1069 1070 lldb::PlatformSP 1071 Platform::GetPlatformForArchitecture (const ArchSpec &arch, ArchSpec *platform_arch_ptr) 1072 { 1073 lldb::PlatformSP platform_sp; 1074 Error error; 1075 if (arch.IsValid()) 1076 platform_sp = Platform::Create (arch, platform_arch_ptr, error); 1077 return platform_sp; 1078 } 1079 1080 1081 //------------------------------------------------------------------ 1082 /// Lets a platform answer if it is compatible with a given 1083 /// architecture and the target triple contained within. 1084 //------------------------------------------------------------------ 1085 bool 1086 Platform::IsCompatibleArchitecture (const ArchSpec &arch, bool exact_arch_match, ArchSpec *compatible_arch_ptr) 1087 { 1088 // If the architecture is invalid, we must answer true... 1089 if (arch.IsValid()) 1090 { 1091 ArchSpec platform_arch; 1092 // Try for an exact architecture match first. 1093 if (exact_arch_match) 1094 { 1095 for (uint32_t arch_idx=0; GetSupportedArchitectureAtIndex (arch_idx, platform_arch); ++arch_idx) 1096 { 1097 if (arch.IsExactMatch(platform_arch)) 1098 { 1099 if (compatible_arch_ptr) 1100 *compatible_arch_ptr = platform_arch; 1101 return true; 1102 } 1103 } 1104 } 1105 else 1106 { 1107 for (uint32_t arch_idx=0; GetSupportedArchitectureAtIndex (arch_idx, platform_arch); ++arch_idx) 1108 { 1109 if (arch.IsCompatibleMatch(platform_arch)) 1110 { 1111 if (compatible_arch_ptr) 1112 *compatible_arch_ptr = platform_arch; 1113 return true; 1114 } 1115 } 1116 } 1117 } 1118 if (compatible_arch_ptr) 1119 compatible_arch_ptr->Clear(); 1120 return false; 1121 } 1122 1123 Error 1124 Platform::PutFile (const FileSpec& source, 1125 const FileSpec& destination, 1126 uint32_t uid, 1127 uint32_t gid) 1128 { 1129 Error error("unimplemented"); 1130 return error; 1131 } 1132 1133 Error 1134 Platform::GetFile (const FileSpec& source, 1135 const FileSpec& destination) 1136 { 1137 Error error("unimplemented"); 1138 return error; 1139 } 1140 1141 Error 1142 Platform::CreateSymlink (const char *src, // The name of the link is in src 1143 const char *dst)// The symlink points to dst 1144 { 1145 Error error("unimplemented"); 1146 return error; 1147 } 1148 1149 bool 1150 Platform::GetFileExists (const lldb_private::FileSpec& file_spec) 1151 { 1152 return false; 1153 } 1154 1155 Error 1156 Platform::Unlink (const char *path) 1157 { 1158 Error error("unimplemented"); 1159 return error; 1160 } 1161 1162 1163 1164 lldb_private::Error 1165 Platform::RunShellCommand (const char *command, // Shouldn't be NULL 1166 const char *working_dir, // Pass NULL to use the current working directory 1167 int *status_ptr, // Pass NULL if you don't want the process exit status 1168 int *signo_ptr, // Pass NULL if you don't want the signal that caused the process to exit 1169 std::string *command_output, // Pass NULL if you don't want the command output 1170 uint32_t timeout_sec) // Timeout in seconds to wait for shell program to finish 1171 { 1172 if (IsHost()) 1173 return Host::RunShellCommand (command, working_dir, status_ptr, signo_ptr, command_output, timeout_sec); 1174 else 1175 return Error("unimplemented"); 1176 } 1177 1178 1179 bool 1180 Platform::CalculateMD5 (const FileSpec& file_spec, 1181 uint64_t &low, 1182 uint64_t &high) 1183 { 1184 if (IsHost()) 1185 return Host::CalculateMD5(file_spec, low, high); 1186 else 1187 return false; 1188 } 1189 1190 Error 1191 Platform::LaunchNativeProcess ( 1192 ProcessLaunchInfo &launch_info, 1193 lldb_private::NativeProcessProtocol::NativeDelegate &native_delegate, 1194 NativeProcessProtocolSP &process_sp) 1195 { 1196 // Platforms should override this implementation if they want to 1197 // support lldb-gdbserver. 1198 return Error("unimplemented"); 1199 } 1200 1201 Error 1202 Platform::AttachNativeProcess (lldb::pid_t pid, 1203 lldb_private::NativeProcessProtocol::NativeDelegate &native_delegate, 1204 NativeProcessProtocolSP &process_sp) 1205 { 1206 // Platforms should override this implementation if they want to 1207 // support lldb-gdbserver. 1208 return Error("unimplemented"); 1209 } 1210 1211 void 1212 Platform::SetLocalCacheDirectory (const char* local) 1213 { 1214 m_local_cache_directory.assign(local); 1215 } 1216 1217 const char* 1218 Platform::GetLocalCacheDirectory () 1219 { 1220 return m_local_cache_directory.c_str(); 1221 } 1222 1223 static OptionDefinition 1224 g_rsync_option_table[] = 1225 { 1226 { LLDB_OPT_SET_ALL, false, "rsync" , 'r', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone , "Enable rsync." }, 1227 { LLDB_OPT_SET_ALL, false, "rsync-opts" , 'R', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeCommandName , "Platform-specific options required for rsync to work." }, 1228 { LLDB_OPT_SET_ALL, false, "rsync-prefix" , 'P', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeCommandName , "Platform-specific rsync prefix put before the remote path." }, 1229 { LLDB_OPT_SET_ALL, false, "ignore-remote-hostname" , 'i', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone , "Do not automatically fill in the remote hostname when composing the rsync command." }, 1230 }; 1231 1232 static OptionDefinition 1233 g_ssh_option_table[] = 1234 { 1235 { LLDB_OPT_SET_ALL, false, "ssh" , 's', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone , "Enable SSH." }, 1236 { LLDB_OPT_SET_ALL, false, "ssh-opts" , 'S', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeCommandName , "Platform-specific options required for SSH to work." }, 1237 }; 1238 1239 static OptionDefinition 1240 g_caching_option_table[] = 1241 { 1242 { LLDB_OPT_SET_ALL, false, "local-cache-dir" , 'c', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypePath , "Path in which to store local copies of files." }, 1243 }; 1244 1245 OptionGroupPlatformRSync::OptionGroupPlatformRSync () 1246 { 1247 } 1248 1249 OptionGroupPlatformRSync::~OptionGroupPlatformRSync () 1250 { 1251 } 1252 1253 const lldb_private::OptionDefinition* 1254 OptionGroupPlatformRSync::GetDefinitions () 1255 { 1256 return g_rsync_option_table; 1257 } 1258 1259 void 1260 OptionGroupPlatformRSync::OptionParsingStarting (CommandInterpreter &interpreter) 1261 { 1262 m_rsync = false; 1263 m_rsync_opts.clear(); 1264 m_rsync_prefix.clear(); 1265 m_ignores_remote_hostname = false; 1266 } 1267 1268 lldb_private::Error 1269 OptionGroupPlatformRSync::SetOptionValue (CommandInterpreter &interpreter, 1270 uint32_t option_idx, 1271 const char *option_arg) 1272 { 1273 Error error; 1274 char short_option = (char) GetDefinitions()[option_idx].short_option; 1275 switch (short_option) 1276 { 1277 case 'r': 1278 m_rsync = true; 1279 break; 1280 1281 case 'R': 1282 m_rsync_opts.assign(option_arg); 1283 break; 1284 1285 case 'P': 1286 m_rsync_prefix.assign(option_arg); 1287 break; 1288 1289 case 'i': 1290 m_ignores_remote_hostname = true; 1291 break; 1292 1293 default: 1294 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option); 1295 break; 1296 } 1297 1298 return error; 1299 } 1300 1301 uint32_t 1302 OptionGroupPlatformRSync::GetNumDefinitions () 1303 { 1304 return llvm::array_lengthof(g_rsync_option_table); 1305 } 1306 1307 lldb::BreakpointSP 1308 Platform::SetThreadCreationBreakpoint (lldb_private::Target &target) 1309 { 1310 return lldb::BreakpointSP(); 1311 } 1312 1313 OptionGroupPlatformSSH::OptionGroupPlatformSSH () 1314 { 1315 } 1316 1317 OptionGroupPlatformSSH::~OptionGroupPlatformSSH () 1318 { 1319 } 1320 1321 const lldb_private::OptionDefinition* 1322 OptionGroupPlatformSSH::GetDefinitions () 1323 { 1324 return g_ssh_option_table; 1325 } 1326 1327 void 1328 OptionGroupPlatformSSH::OptionParsingStarting (CommandInterpreter &interpreter) 1329 { 1330 m_ssh = false; 1331 m_ssh_opts.clear(); 1332 } 1333 1334 lldb_private::Error 1335 OptionGroupPlatformSSH::SetOptionValue (CommandInterpreter &interpreter, 1336 uint32_t option_idx, 1337 const char *option_arg) 1338 { 1339 Error error; 1340 char short_option = (char) GetDefinitions()[option_idx].short_option; 1341 switch (short_option) 1342 { 1343 case 's': 1344 m_ssh = true; 1345 break; 1346 1347 case 'S': 1348 m_ssh_opts.assign(option_arg); 1349 break; 1350 1351 default: 1352 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option); 1353 break; 1354 } 1355 1356 return error; 1357 } 1358 1359 uint32_t 1360 OptionGroupPlatformSSH::GetNumDefinitions () 1361 { 1362 return llvm::array_lengthof(g_ssh_option_table); 1363 } 1364 1365 OptionGroupPlatformCaching::OptionGroupPlatformCaching () 1366 { 1367 } 1368 1369 OptionGroupPlatformCaching::~OptionGroupPlatformCaching () 1370 { 1371 } 1372 1373 const lldb_private::OptionDefinition* 1374 OptionGroupPlatformCaching::GetDefinitions () 1375 { 1376 return g_caching_option_table; 1377 } 1378 1379 void 1380 OptionGroupPlatformCaching::OptionParsingStarting (CommandInterpreter &interpreter) 1381 { 1382 m_cache_dir.clear(); 1383 } 1384 1385 lldb_private::Error 1386 OptionGroupPlatformCaching::SetOptionValue (CommandInterpreter &interpreter, 1387 uint32_t option_idx, 1388 const char *option_arg) 1389 { 1390 Error error; 1391 char short_option = (char) GetDefinitions()[option_idx].short_option; 1392 switch (short_option) 1393 { 1394 case 'c': 1395 m_cache_dir.assign(option_arg); 1396 break; 1397 1398 default: 1399 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option); 1400 break; 1401 } 1402 1403 return error; 1404 } 1405 1406 uint32_t 1407 OptionGroupPlatformCaching::GetNumDefinitions () 1408 { 1409 return llvm::array_lengthof(g_caching_option_table); 1410 } 1411 1412 size_t 1413 Platform::GetEnvironment (StringList &environment) 1414 { 1415 environment.Clear(); 1416 return false; 1417 } 1418 1419 const std::vector<ConstString> & 1420 Platform::GetTrapHandlerSymbolNames () 1421 { 1422 if (!m_calculated_trap_handlers) 1423 { 1424 Mutex::Locker locker (m_trap_handler_mutex); 1425 if (!m_calculated_trap_handlers) 1426 { 1427 CalculateTrapHandlerSymbolNames(); 1428 m_calculated_trap_handlers = true; 1429 } 1430 } 1431 return m_trap_handlers; 1432 } 1433 1434