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 14 // C++ Includes 15 #include <algorithm> 16 #include <fstream> 17 #include <vector> 18 19 // Other libraries and framework includes 20 // Project includes 21 #include "lldb/Breakpoint/BreakpointIDList.h" 22 #include "lldb/Core/DataBufferHeap.h" 23 #include "lldb/Core/Debugger.h" 24 #include "lldb/Core/Error.h" 25 #include "lldb/Core/Log.h" 26 #include "lldb/Core/Module.h" 27 #include "lldb/Core/ModuleSpec.h" 28 #include "lldb/Core/PluginManager.h" 29 #include "lldb/Core/StructuredData.h" 30 #include "lldb/Host/FileSpec.h" 31 #include "lldb/Host/FileSystem.h" 32 #include "lldb/Host/Host.h" 33 #include "lldb/Host/HostInfo.h" 34 #include "lldb/Interpreter/OptionValueProperties.h" 35 #include "lldb/Interpreter/Property.h" 36 #include "lldb/Symbol/ObjectFile.h" 37 #include "lldb/Target/Process.h" 38 #include "lldb/Target/Target.h" 39 #include "lldb/Target/UnixSignals.h" 40 #include "lldb/Utility/Utils.h" 41 #include "llvm/Support/FileSystem.h" 42 43 #include "Utility/ModuleCache.h" 44 45 // Define these constants from POSIX mman.h rather than include the file 46 // so that they will be correct even when compiled on Linux. 47 #define MAP_PRIVATE 2 48 #define MAP_ANON 0x1000 49 50 using namespace lldb; 51 using namespace lldb_private; 52 53 static uint32_t g_initialize_count = 0; 54 55 // Use a singleton function for g_local_platform_sp to avoid init 56 // constructors since LLDB is often part of a shared library 57 static PlatformSP& 58 GetHostPlatformSP () 59 { 60 static PlatformSP g_platform_sp; 61 return g_platform_sp; 62 } 63 64 const char * 65 Platform::GetHostPlatformName () 66 { 67 return "host"; 68 } 69 70 namespace { 71 72 PropertyDefinition 73 g_properties[] = 74 { 75 { "use-module-cache" , OptionValue::eTypeBoolean , true, true, nullptr, nullptr, "Use module cache." }, 76 { "module-cache-directory", OptionValue::eTypeFileSpec, true, 0 , nullptr, nullptr, "Root directory for cached modules." }, 77 { nullptr , OptionValue::eTypeInvalid , false, 0, nullptr, nullptr, nullptr } 78 }; 79 80 enum 81 { 82 ePropertyUseModuleCache, 83 ePropertyModuleCacheDirectory 84 }; 85 86 } // namespace 87 88 89 ConstString 90 PlatformProperties::GetSettingName () 91 { 92 static ConstString g_setting_name("platform"); 93 return g_setting_name; 94 } 95 96 PlatformProperties::PlatformProperties () 97 { 98 m_collection_sp.reset (new OptionValueProperties (GetSettingName ())); 99 m_collection_sp->Initialize (g_properties); 100 101 auto module_cache_dir = GetModuleCacheDirectory (); 102 if (!module_cache_dir) 103 { 104 if (!HostInfo::GetLLDBPath (ePathTypeGlobalLLDBTempSystemDir, module_cache_dir)) 105 module_cache_dir = FileSpec ("/tmp/lldb", false); 106 module_cache_dir.AppendPathComponent ("module_cache"); 107 SetModuleCacheDirectory (module_cache_dir); 108 } 109 } 110 111 bool 112 PlatformProperties::GetUseModuleCache () const 113 { 114 const auto idx = ePropertyUseModuleCache; 115 return m_collection_sp->GetPropertyAtIndexAsBoolean ( 116 nullptr, idx, g_properties[idx].default_uint_value != 0); 117 } 118 119 bool 120 PlatformProperties::SetUseModuleCache (bool use_module_cache) 121 { 122 return m_collection_sp->SetPropertyAtIndexAsBoolean (nullptr, ePropertyUseModuleCache, use_module_cache); 123 } 124 125 FileSpec 126 PlatformProperties::GetModuleCacheDirectory () const 127 { 128 return m_collection_sp->GetPropertyAtIndexAsFileSpec (nullptr, ePropertyModuleCacheDirectory); 129 } 130 131 bool 132 PlatformProperties::SetModuleCacheDirectory (const FileSpec& dir_spec) 133 { 134 return m_collection_sp->SetPropertyAtIndexAsFileSpec (nullptr, ePropertyModuleCacheDirectory, dir_spec); 135 } 136 137 //------------------------------------------------------------------ 138 /// Get the native host platform plug-in. 139 /// 140 /// There should only be one of these for each host that LLDB runs 141 /// upon that should be statically compiled in and registered using 142 /// preprocessor macros or other similar build mechanisms. 143 /// 144 /// This platform will be used as the default platform when launching 145 /// or attaching to processes unless another platform is specified. 146 //------------------------------------------------------------------ 147 PlatformSP 148 Platform::GetHostPlatform () 149 { 150 return GetHostPlatformSP (); 151 } 152 153 static std::vector<PlatformSP> & 154 GetPlatformList() 155 { 156 static std::vector<PlatformSP> g_platform_list; 157 return g_platform_list; 158 } 159 160 static Mutex & 161 GetPlatformListMutex () 162 { 163 static Mutex g_mutex(Mutex::eMutexTypeRecursive); 164 return g_mutex; 165 } 166 167 void 168 Platform::Initialize () 169 { 170 g_initialize_count++; 171 } 172 173 void 174 Platform::Terminate () 175 { 176 if (g_initialize_count > 0) 177 { 178 if (--g_initialize_count == 0) 179 { 180 Mutex::Locker locker(GetPlatformListMutex ()); 181 GetPlatformList().clear(); 182 } 183 } 184 } 185 186 const PlatformPropertiesSP & 187 Platform::GetGlobalPlatformProperties () 188 { 189 static const auto g_settings_sp (std::make_shared<PlatformProperties> ()); 190 return g_settings_sp; 191 } 192 193 void 194 Platform::SetHostPlatform (const lldb::PlatformSP &platform_sp) 195 { 196 // The native platform should use its static void Platform::Initialize() 197 // function to register itself as the native platform. 198 GetHostPlatformSP () = platform_sp; 199 200 if (platform_sp) 201 { 202 Mutex::Locker locker(GetPlatformListMutex ()); 203 GetPlatformList().push_back(platform_sp); 204 } 205 } 206 207 Error 208 Platform::GetFileWithUUID (const FileSpec &platform_file, 209 const UUID *uuid_ptr, 210 FileSpec &local_file) 211 { 212 // Default to the local case 213 local_file = platform_file; 214 return Error(); 215 } 216 217 FileSpecList 218 Platform::LocateExecutableScriptingResources (Target *target, Module &module, Stream* feedback_stream) 219 { 220 return FileSpecList(); 221 } 222 223 //PlatformSP 224 //Platform::FindPlugin (Process *process, const ConstString &plugin_name) 225 //{ 226 // PlatformCreateInstance create_callback = NULL; 227 // if (plugin_name) 228 // { 229 // create_callback = PluginManager::GetPlatformCreateCallbackForPluginName (plugin_name); 230 // if (create_callback) 231 // { 232 // ArchSpec arch; 233 // if (process) 234 // { 235 // arch = process->GetTarget().GetArchitecture(); 236 // } 237 // PlatformSP platform_sp(create_callback(process, &arch)); 238 // if (platform_sp) 239 // return platform_sp; 240 // } 241 // } 242 // else 243 // { 244 // for (uint32_t idx = 0; (create_callback = PluginManager::GetPlatformCreateCallbackAtIndex(idx)) != NULL; ++idx) 245 // { 246 // PlatformSP platform_sp(create_callback(process, nullptr)); 247 // if (platform_sp) 248 // return platform_sp; 249 // } 250 // } 251 // return PlatformSP(); 252 //} 253 254 Error 255 Platform::GetSharedModule (const ModuleSpec &module_spec, 256 Process* process, 257 ModuleSP &module_sp, 258 const FileSpecList *module_search_paths_ptr, 259 ModuleSP *old_module_sp_ptr, 260 bool *did_create_ptr) 261 { 262 if (IsHost ()) 263 return ModuleList::GetSharedModule (module_spec, 264 module_sp, 265 module_search_paths_ptr, 266 old_module_sp_ptr, 267 did_create_ptr, 268 false); 269 270 return GetRemoteSharedModule (module_spec, 271 process, 272 module_sp, 273 [&](const ModuleSpec &spec) 274 { 275 return ModuleList::GetSharedModule ( 276 spec, module_sp, module_search_paths_ptr, old_module_sp_ptr, did_create_ptr, false); 277 }, 278 did_create_ptr); 279 } 280 281 bool 282 Platform::GetModuleSpec (const FileSpec& module_file_spec, 283 const ArchSpec& arch, 284 ModuleSpec &module_spec) 285 { 286 ModuleSpecList module_specs; 287 if (ObjectFile::GetModuleSpecifications (module_file_spec, 0, 0, module_specs) == 0) 288 return false; 289 290 ModuleSpec matched_module_spec; 291 return module_specs.FindMatchingModuleSpec (ModuleSpec (module_file_spec, arch), 292 module_spec); 293 } 294 295 PlatformSP 296 Platform::Find (const ConstString &name) 297 { 298 if (name) 299 { 300 static ConstString g_host_platform_name ("host"); 301 if (name == g_host_platform_name) 302 return GetHostPlatform(); 303 304 Mutex::Locker locker(GetPlatformListMutex ()); 305 for (const auto &platform_sp : GetPlatformList()) 306 { 307 if (platform_sp->GetName() == name) 308 return platform_sp; 309 } 310 } 311 return PlatformSP(); 312 } 313 314 PlatformSP 315 Platform::Create (const ConstString &name, Error &error) 316 { 317 PlatformCreateInstance create_callback = NULL; 318 lldb::PlatformSP platform_sp; 319 if (name) 320 { 321 static ConstString g_host_platform_name ("host"); 322 if (name == g_host_platform_name) 323 return GetHostPlatform(); 324 325 create_callback = PluginManager::GetPlatformCreateCallbackForPluginName (name); 326 if (create_callback) 327 platform_sp = create_callback(true, NULL); 328 else 329 error.SetErrorStringWithFormat ("unable to find a plug-in for the platform named \"%s\"", name.GetCString()); 330 } 331 else 332 error.SetErrorString ("invalid platform name"); 333 334 if (platform_sp) 335 { 336 Mutex::Locker locker(GetPlatformListMutex ()); 337 GetPlatformList().push_back(platform_sp); 338 } 339 340 return platform_sp; 341 } 342 343 344 PlatformSP 345 Platform::Create (const ArchSpec &arch, ArchSpec *platform_arch_ptr, Error &error) 346 { 347 lldb::PlatformSP platform_sp; 348 if (arch.IsValid()) 349 { 350 // Scope for locker 351 { 352 // First try exact arch matches across all platforms already created 353 Mutex::Locker locker(GetPlatformListMutex ()); 354 for (const auto &platform_sp : GetPlatformList()) 355 { 356 if (platform_sp->IsCompatibleArchitecture(arch, true, platform_arch_ptr)) 357 return platform_sp; 358 } 359 360 // Next try compatible arch matches across all platforms already created 361 for (const auto &platform_sp : GetPlatformList()) 362 { 363 if (platform_sp->IsCompatibleArchitecture(arch, false, platform_arch_ptr)) 364 return platform_sp; 365 } 366 } 367 368 PlatformCreateInstance create_callback; 369 // First try exact arch matches across all platform plug-ins 370 uint32_t idx; 371 for (idx = 0; (create_callback = PluginManager::GetPlatformCreateCallbackAtIndex (idx)); ++idx) 372 { 373 if (create_callback) 374 { 375 platform_sp = create_callback(false, &arch); 376 if (platform_sp && platform_sp->IsCompatibleArchitecture(arch, true, platform_arch_ptr)) 377 { 378 Mutex::Locker locker(GetPlatformListMutex ()); 379 GetPlatformList().push_back(platform_sp); 380 return platform_sp; 381 } 382 } 383 } 384 // Next try compatible arch matches across all platform plug-ins 385 for (idx = 0; (create_callback = PluginManager::GetPlatformCreateCallbackAtIndex (idx)); ++idx) 386 { 387 if (create_callback) 388 { 389 platform_sp = create_callback(false, &arch); 390 if (platform_sp && platform_sp->IsCompatibleArchitecture(arch, false, platform_arch_ptr)) 391 { 392 Mutex::Locker locker(GetPlatformListMutex ()); 393 GetPlatformList().push_back(platform_sp); 394 return platform_sp; 395 } 396 } 397 } 398 } 399 else 400 error.SetErrorString ("invalid platform name"); 401 if (platform_arch_ptr) 402 platform_arch_ptr->Clear(); 403 platform_sp.reset(); 404 return platform_sp; 405 } 406 407 //------------------------------------------------------------------ 408 /// Default Constructor 409 //------------------------------------------------------------------ 410 Platform::Platform (bool is_host) : 411 m_is_host (is_host), 412 m_os_version_set_while_connected (false), 413 m_system_arch_set_while_connected (false), 414 m_sdk_sysroot (), 415 m_sdk_build (), 416 m_working_dir (), 417 m_remote_url (), 418 m_name (), 419 m_major_os_version (UINT32_MAX), 420 m_minor_os_version (UINT32_MAX), 421 m_update_os_version (UINT32_MAX), 422 m_system_arch(), 423 m_mutex (Mutex::eMutexTypeRecursive), 424 m_uid_map(), 425 m_gid_map(), 426 m_max_uid_name_len (0), 427 m_max_gid_name_len (0), 428 m_supports_rsync (false), 429 m_rsync_opts (), 430 m_rsync_prefix (), 431 m_supports_ssh (false), 432 m_ssh_opts (), 433 m_ignores_remote_hostname (false), 434 m_trap_handlers(), 435 m_calculated_trap_handlers (false), 436 m_module_cache (llvm::make_unique<ModuleCache> ()) 437 { 438 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); 439 if (log) 440 log->Printf ("%p Platform::Platform()", static_cast<void*>(this)); 441 } 442 443 //------------------------------------------------------------------ 444 /// Destructor. 445 /// 446 /// The destructor is virtual since this class is designed to be 447 /// inherited from by the plug-in instance. 448 //------------------------------------------------------------------ 449 Platform::~Platform() 450 { 451 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); 452 if (log) 453 log->Printf ("%p Platform::~Platform()", static_cast<void*>(this)); 454 } 455 456 void 457 Platform::GetStatus (Stream &strm) 458 { 459 uint32_t major = UINT32_MAX; 460 uint32_t minor = UINT32_MAX; 461 uint32_t update = UINT32_MAX; 462 std::string s; 463 strm.Printf (" Platform: %s\n", GetPluginName().GetCString()); 464 465 ArchSpec arch (GetSystemArchitecture()); 466 if (arch.IsValid()) 467 { 468 if (!arch.GetTriple().str().empty()) 469 strm.Printf(" Triple: %s\n", arch.GetTriple().str().c_str()); 470 } 471 472 if (GetOSVersion(major, minor, update)) 473 { 474 strm.Printf("OS Version: %u", major); 475 if (minor != UINT32_MAX) 476 strm.Printf(".%u", minor); 477 if (update != UINT32_MAX) 478 strm.Printf(".%u", update); 479 480 if (GetOSBuildString (s)) 481 strm.Printf(" (%s)", s.c_str()); 482 483 strm.EOL(); 484 } 485 486 if (GetOSKernelDescription (s)) 487 strm.Printf(" Kernel: %s\n", s.c_str()); 488 489 if (IsHost()) 490 { 491 strm.Printf(" Hostname: %s\n", GetHostname()); 492 } 493 else 494 { 495 const bool is_connected = IsConnected(); 496 if (is_connected) 497 strm.Printf(" Hostname: %s\n", GetHostname()); 498 strm.Printf(" Connected: %s\n", is_connected ? "yes" : "no"); 499 } 500 501 if (GetWorkingDirectory()) 502 { 503 strm.Printf("WorkingDir: %s\n", GetWorkingDirectory().GetCString()); 504 } 505 if (!IsConnected()) 506 return; 507 508 std::string specific_info(GetPlatformSpecificConnectionInformation()); 509 510 if (specific_info.empty() == false) 511 strm.Printf("Platform-specific connection: %s\n", specific_info.c_str()); 512 } 513 514 515 bool 516 Platform::GetOSVersion (uint32_t &major, 517 uint32_t &minor, 518 uint32_t &update) 519 { 520 Mutex::Locker locker (m_mutex); 521 522 bool success = m_major_os_version != UINT32_MAX; 523 if (IsHost()) 524 { 525 if (!success) 526 { 527 // We have a local host platform 528 success = HostInfo::GetOSVersion(m_major_os_version, m_minor_os_version, m_update_os_version); 529 m_os_version_set_while_connected = success; 530 } 531 } 532 else 533 { 534 // We have a remote platform. We can only fetch the remote 535 // OS version if we are connected, and we don't want to do it 536 // more than once. 537 538 const bool is_connected = IsConnected(); 539 540 bool fetch = false; 541 if (success) 542 { 543 // We have valid OS version info, check to make sure it wasn't 544 // manually set prior to connecting. If it was manually set prior 545 // to connecting, then lets fetch the actual OS version info 546 // if we are now connected. 547 if (is_connected && !m_os_version_set_while_connected) 548 fetch = true; 549 } 550 else 551 { 552 // We don't have valid OS version info, fetch it if we are connected 553 fetch = is_connected; 554 } 555 556 if (fetch) 557 { 558 success = GetRemoteOSVersion (); 559 m_os_version_set_while_connected = success; 560 } 561 } 562 563 if (success) 564 { 565 major = m_major_os_version; 566 minor = m_minor_os_version; 567 update = m_update_os_version; 568 } 569 return success; 570 } 571 572 bool 573 Platform::GetOSBuildString (std::string &s) 574 { 575 s.clear(); 576 577 if (IsHost()) 578 #if !defined(__linux__) 579 return HostInfo::GetOSBuildString(s); 580 #else 581 return false; 582 #endif 583 else 584 return GetRemoteOSBuildString (s); 585 } 586 587 bool 588 Platform::GetOSKernelDescription (std::string &s) 589 { 590 if (IsHost()) 591 #if !defined(__linux__) 592 return HostInfo::GetOSKernelDescription(s); 593 #else 594 return false; 595 #endif 596 else 597 return GetRemoteOSKernelDescription (s); 598 } 599 600 void 601 Platform::AddClangModuleCompilationOptions (Target *target, std::vector<std::string> &options) 602 { 603 std::vector<std::string> default_compilation_options = 604 { 605 "-x", "c++", "-Xclang", "-nostdsysteminc", "-Xclang", "-nostdsysteminc" 606 }; 607 608 options.insert(options.end(), 609 default_compilation_options.begin(), 610 default_compilation_options.end()); 611 } 612 613 614 FileSpec 615 Platform::GetWorkingDirectory () 616 { 617 if (IsHost()) 618 { 619 char cwd[PATH_MAX]; 620 if (getcwd(cwd, sizeof(cwd))) 621 return FileSpec{cwd, true}; 622 else 623 return FileSpec{}; 624 } 625 else 626 { 627 if (!m_working_dir) 628 m_working_dir = GetRemoteWorkingDirectory(); 629 return m_working_dir; 630 } 631 } 632 633 634 struct RecurseCopyBaton 635 { 636 const FileSpec& dst; 637 Platform *platform_ptr; 638 Error error; 639 }; 640 641 642 static FileSpec::EnumerateDirectoryResult 643 RecurseCopy_Callback (void *baton, 644 FileSpec::FileType file_type, 645 const FileSpec &src) 646 { 647 RecurseCopyBaton* rc_baton = (RecurseCopyBaton*)baton; 648 switch (file_type) 649 { 650 case FileSpec::eFileTypePipe: 651 case FileSpec::eFileTypeSocket: 652 // we have no way to copy pipes and sockets - ignore them and continue 653 return FileSpec::eEnumerateDirectoryResultNext; 654 break; 655 656 case FileSpec::eFileTypeDirectory: 657 { 658 // make the new directory and get in there 659 FileSpec dst_dir = rc_baton->dst; 660 if (!dst_dir.GetFilename()) 661 dst_dir.GetFilename() = src.GetLastPathComponent(); 662 Error error = rc_baton->platform_ptr->MakeDirectory(dst_dir, lldb::eFilePermissionsDirectoryDefault); 663 if (error.Fail()) 664 { 665 rc_baton->error.SetErrorStringWithFormat("unable to setup directory %s on remote end", 666 dst_dir.GetCString()); 667 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out 668 } 669 670 // now recurse 671 std::string src_dir_path (src.GetPath()); 672 673 // Make a filespec that only fills in the directory of a FileSpec so 674 // when we enumerate we can quickly fill in the filename for dst copies 675 FileSpec recurse_dst; 676 recurse_dst.GetDirectory().SetCString(dst_dir.GetPath().c_str()); 677 RecurseCopyBaton rc_baton2 = { recurse_dst, rc_baton->platform_ptr, Error() }; 678 FileSpec::EnumerateDirectory(src_dir_path.c_str(), true, true, true, RecurseCopy_Callback, &rc_baton2); 679 if (rc_baton2.error.Fail()) 680 { 681 rc_baton->error.SetErrorString(rc_baton2.error.AsCString()); 682 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out 683 } 684 return FileSpec::eEnumerateDirectoryResultNext; 685 } 686 break; 687 688 case FileSpec::eFileTypeSymbolicLink: 689 { 690 // copy the file and keep going 691 FileSpec dst_file = rc_baton->dst; 692 if (!dst_file.GetFilename()) 693 dst_file.GetFilename() = src.GetFilename(); 694 695 FileSpec src_resolved; 696 697 rc_baton->error = FileSystem::Readlink(src, src_resolved); 698 699 if (rc_baton->error.Fail()) 700 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out 701 702 rc_baton->error = rc_baton->platform_ptr->CreateSymlink(dst_file, src_resolved); 703 704 if (rc_baton->error.Fail()) 705 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out 706 707 return FileSpec::eEnumerateDirectoryResultNext; 708 } 709 break; 710 case FileSpec::eFileTypeRegular: 711 { 712 // copy the file and keep going 713 FileSpec dst_file = rc_baton->dst; 714 if (!dst_file.GetFilename()) 715 dst_file.GetFilename() = src.GetFilename(); 716 Error err = rc_baton->platform_ptr->PutFile(src, dst_file); 717 if (err.Fail()) 718 { 719 rc_baton->error.SetErrorString(err.AsCString()); 720 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out 721 } 722 return FileSpec::eEnumerateDirectoryResultNext; 723 } 724 break; 725 726 case FileSpec::eFileTypeInvalid: 727 case FileSpec::eFileTypeOther: 728 case FileSpec::eFileTypeUnknown: 729 rc_baton->error.SetErrorStringWithFormat("invalid file detected during copy: %s", src.GetPath().c_str()); 730 return FileSpec::eEnumerateDirectoryResultQuit; // got an error, bail out 731 break; 732 } 733 llvm_unreachable("Unhandled FileSpec::FileType!"); 734 } 735 736 Error 737 Platform::Install (const FileSpec& src, const FileSpec& dst) 738 { 739 Error error; 740 741 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM); 742 if (log) 743 log->Printf ("Platform::Install (src='%s', dst='%s')", src.GetPath().c_str(), dst.GetPath().c_str()); 744 FileSpec fixed_dst(dst); 745 746 if (!fixed_dst.GetFilename()) 747 fixed_dst.GetFilename() = src.GetFilename(); 748 749 FileSpec working_dir = GetWorkingDirectory(); 750 751 if (dst) 752 { 753 if (dst.GetDirectory()) 754 { 755 const char first_dst_dir_char = dst.GetDirectory().GetCString()[0]; 756 if (first_dst_dir_char == '/' || first_dst_dir_char == '\\') 757 { 758 fixed_dst.GetDirectory() = dst.GetDirectory(); 759 } 760 // If the fixed destination file doesn't have a directory yet, 761 // then we must have a relative path. We will resolve this relative 762 // path against the platform's working directory 763 if (!fixed_dst.GetDirectory()) 764 { 765 FileSpec relative_spec; 766 std::string path; 767 if (working_dir) 768 { 769 relative_spec = working_dir; 770 relative_spec.AppendPathComponent(dst.GetPath()); 771 fixed_dst.GetDirectory() = relative_spec.GetDirectory(); 772 } 773 else 774 { 775 error.SetErrorStringWithFormat("platform working directory must be valid for relative path '%s'", dst.GetPath().c_str()); 776 return error; 777 } 778 } 779 } 780 else 781 { 782 if (working_dir) 783 { 784 fixed_dst.GetDirectory().SetCString(working_dir.GetCString()); 785 } 786 else 787 { 788 error.SetErrorStringWithFormat("platform working directory must be valid for relative path '%s'", dst.GetPath().c_str()); 789 return error; 790 } 791 } 792 } 793 else 794 { 795 if (working_dir) 796 { 797 fixed_dst.GetDirectory().SetCString(working_dir.GetCString()); 798 } 799 else 800 { 801 error.SetErrorStringWithFormat("platform working directory must be valid when destination directory is empty"); 802 return error; 803 } 804 } 805 806 if (log) 807 log->Printf ("Platform::Install (src='%s', dst='%s') fixed_dst='%s'", src.GetPath().c_str(), dst.GetPath().c_str(), fixed_dst.GetPath().c_str()); 808 809 if (GetSupportsRSync()) 810 { 811 error = PutFile(src, dst); 812 } 813 else 814 { 815 switch (src.GetFileType()) 816 { 817 case FileSpec::eFileTypeDirectory: 818 { 819 if (GetFileExists (fixed_dst)) 820 Unlink(fixed_dst); 821 uint32_t permissions = src.GetPermissions(); 822 if (permissions == 0) 823 permissions = eFilePermissionsDirectoryDefault; 824 error = MakeDirectory(fixed_dst, permissions); 825 if (error.Success()) 826 { 827 // Make a filespec that only fills in the directory of a FileSpec so 828 // when we enumerate we can quickly fill in the filename for dst copies 829 FileSpec recurse_dst; 830 recurse_dst.GetDirectory().SetCString(fixed_dst.GetCString()); 831 std::string src_dir_path (src.GetPath()); 832 RecurseCopyBaton baton = { recurse_dst, this, Error() }; 833 FileSpec::EnumerateDirectory(src_dir_path.c_str(), true, true, true, RecurseCopy_Callback, &baton); 834 return baton.error; 835 } 836 } 837 break; 838 839 case FileSpec::eFileTypeRegular: 840 if (GetFileExists (fixed_dst)) 841 Unlink(fixed_dst); 842 error = PutFile(src, fixed_dst); 843 break; 844 845 case FileSpec::eFileTypeSymbolicLink: 846 { 847 if (GetFileExists (fixed_dst)) 848 Unlink(fixed_dst); 849 FileSpec src_resolved; 850 error = FileSystem::Readlink(src, src_resolved); 851 if (error.Success()) 852 error = CreateSymlink(dst, src_resolved); 853 } 854 break; 855 case FileSpec::eFileTypePipe: 856 error.SetErrorString("platform install doesn't handle pipes"); 857 break; 858 case FileSpec::eFileTypeSocket: 859 error.SetErrorString("platform install doesn't handle sockets"); 860 break; 861 case FileSpec::eFileTypeInvalid: 862 case FileSpec::eFileTypeUnknown: 863 case FileSpec::eFileTypeOther: 864 error.SetErrorString("platform install doesn't handle non file or directory items"); 865 break; 866 } 867 } 868 return error; 869 } 870 871 bool 872 Platform::SetWorkingDirectory(const FileSpec &file_spec) 873 { 874 if (IsHost()) 875 { 876 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM); 877 if (log) 878 log->Printf("Platform::SetWorkingDirectory('%s')", 879 file_spec.GetCString()); 880 if (file_spec) 881 { 882 if (::chdir(file_spec.GetCString()) == 0) 883 return true; 884 } 885 return false; 886 } 887 else 888 { 889 m_working_dir.Clear(); 890 return SetRemoteWorkingDirectory(file_spec); 891 } 892 } 893 894 Error 895 Platform::MakeDirectory(const FileSpec &file_spec, uint32_t permissions) 896 { 897 if (IsHost()) 898 return FileSystem::MakeDirectory(file_spec, permissions); 899 else 900 { 901 Error error; 902 error.SetErrorStringWithFormat("remote platform %s doesn't support %s", GetPluginName().GetCString(), __PRETTY_FUNCTION__); 903 return error; 904 } 905 } 906 907 Error 908 Platform::GetFilePermissions(const FileSpec &file_spec, uint32_t &file_permissions) 909 { 910 if (IsHost()) 911 return FileSystem::GetFilePermissions(file_spec, file_permissions); 912 else 913 { 914 Error error; 915 error.SetErrorStringWithFormat("remote platform %s doesn't support %s", GetPluginName().GetCString(), __PRETTY_FUNCTION__); 916 return error; 917 } 918 } 919 920 Error 921 Platform::SetFilePermissions(const FileSpec &file_spec, uint32_t file_permissions) 922 { 923 if (IsHost()) 924 return FileSystem::SetFilePermissions(file_spec, file_permissions); 925 else 926 { 927 Error error; 928 error.SetErrorStringWithFormat("remote platform %s doesn't support %s", GetPluginName().GetCString(), __PRETTY_FUNCTION__); 929 return error; 930 } 931 } 932 933 ConstString 934 Platform::GetName () 935 { 936 return GetPluginName(); 937 } 938 939 const char * 940 Platform::GetHostname () 941 { 942 if (IsHost()) 943 return "127.0.0.1"; 944 945 if (m_name.empty()) 946 return NULL; 947 return m_name.c_str(); 948 } 949 950 bool 951 Platform::SetRemoteWorkingDirectory(const FileSpec &working_dir) 952 { 953 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM); 954 if (log) 955 log->Printf("Platform::SetRemoteWorkingDirectory('%s')", 956 working_dir.GetCString()); 957 m_working_dir = working_dir; 958 return true; 959 } 960 961 const char * 962 Platform::GetUserName (uint32_t uid) 963 { 964 #if !defined(LLDB_DISABLE_POSIX) 965 const char *user_name = GetCachedUserName(uid); 966 if (user_name) 967 return user_name; 968 if (IsHost()) 969 { 970 std::string name; 971 if (HostInfo::LookupUserName(uid, name)) 972 return SetCachedUserName (uid, name.c_str(), name.size()); 973 } 974 #endif 975 return NULL; 976 } 977 978 const char * 979 Platform::GetGroupName (uint32_t gid) 980 { 981 #if !defined(LLDB_DISABLE_POSIX) 982 const char *group_name = GetCachedGroupName(gid); 983 if (group_name) 984 return group_name; 985 if (IsHost()) 986 { 987 std::string name; 988 if (HostInfo::LookupGroupName(gid, name)) 989 return SetCachedGroupName (gid, name.c_str(), name.size()); 990 } 991 #endif 992 return NULL; 993 } 994 995 bool 996 Platform::SetOSVersion (uint32_t major, 997 uint32_t minor, 998 uint32_t update) 999 { 1000 if (IsHost()) 1001 { 1002 // We don't need anyone setting the OS version for the host platform, 1003 // we should be able to figure it out by calling HostInfo::GetOSVersion(...). 1004 return false; 1005 } 1006 else 1007 { 1008 // We have a remote platform, allow setting the target OS version if 1009 // we aren't connected, since if we are connected, we should be able to 1010 // request the remote OS version from the connected platform. 1011 if (IsConnected()) 1012 return false; 1013 else 1014 { 1015 // We aren't connected and we might want to set the OS version 1016 // ahead of time before we connect so we can peruse files and 1017 // use a local SDK or PDK cache of support files to disassemble 1018 // or do other things. 1019 m_major_os_version = major; 1020 m_minor_os_version = minor; 1021 m_update_os_version = update; 1022 return true; 1023 } 1024 } 1025 return false; 1026 } 1027 1028 1029 Error 1030 Platform::ResolveExecutable (const ModuleSpec &module_spec, 1031 lldb::ModuleSP &exe_module_sp, 1032 const FileSpecList *module_search_paths_ptr) 1033 { 1034 Error error; 1035 if (module_spec.GetFileSpec().Exists()) 1036 { 1037 if (module_spec.GetArchitecture().IsValid()) 1038 { 1039 error = ModuleList::GetSharedModule (module_spec, 1040 exe_module_sp, 1041 module_search_paths_ptr, 1042 NULL, 1043 NULL); 1044 } 1045 else 1046 { 1047 // No valid architecture was specified, ask the platform for 1048 // the architectures that we should be using (in the correct order) 1049 // and see if we can find a match that way 1050 ModuleSpec arch_module_spec(module_spec); 1051 for (uint32_t idx = 0; GetSupportedArchitectureAtIndex (idx, arch_module_spec.GetArchitecture()); ++idx) 1052 { 1053 error = ModuleList::GetSharedModule (arch_module_spec, 1054 exe_module_sp, 1055 module_search_paths_ptr, 1056 NULL, 1057 NULL); 1058 // Did we find an executable using one of the 1059 if (error.Success() && exe_module_sp) 1060 break; 1061 } 1062 } 1063 } 1064 else 1065 { 1066 error.SetErrorStringWithFormat ("'%s' does not exist", 1067 module_spec.GetFileSpec().GetPath().c_str()); 1068 } 1069 return error; 1070 } 1071 1072 Error 1073 Platform::ResolveSymbolFile (Target &target, 1074 const ModuleSpec &sym_spec, 1075 FileSpec &sym_file) 1076 { 1077 Error error; 1078 if (sym_spec.GetSymbolFileSpec().Exists()) 1079 sym_file = sym_spec.GetSymbolFileSpec(); 1080 else 1081 error.SetErrorString("unable to resolve symbol file"); 1082 return error; 1083 1084 } 1085 1086 1087 1088 bool 1089 Platform::ResolveRemotePath (const FileSpec &platform_path, 1090 FileSpec &resolved_platform_path) 1091 { 1092 resolved_platform_path = platform_path; 1093 return resolved_platform_path.ResolvePath(); 1094 } 1095 1096 1097 const ArchSpec & 1098 Platform::GetSystemArchitecture() 1099 { 1100 if (IsHost()) 1101 { 1102 if (!m_system_arch.IsValid()) 1103 { 1104 // We have a local host platform 1105 m_system_arch = HostInfo::GetArchitecture(); 1106 m_system_arch_set_while_connected = m_system_arch.IsValid(); 1107 } 1108 } 1109 else 1110 { 1111 // We have a remote platform. We can only fetch the remote 1112 // system architecture if we are connected, and we don't want to do it 1113 // more than once. 1114 1115 const bool is_connected = IsConnected(); 1116 1117 bool fetch = false; 1118 if (m_system_arch.IsValid()) 1119 { 1120 // We have valid OS version info, check to make sure it wasn't 1121 // manually set prior to connecting. If it was manually set prior 1122 // to connecting, then lets fetch the actual OS version info 1123 // if we are now connected. 1124 if (is_connected && !m_system_arch_set_while_connected) 1125 fetch = true; 1126 } 1127 else 1128 { 1129 // We don't have valid OS version info, fetch it if we are connected 1130 fetch = is_connected; 1131 } 1132 1133 if (fetch) 1134 { 1135 m_system_arch = GetRemoteSystemArchitecture (); 1136 m_system_arch_set_while_connected = m_system_arch.IsValid(); 1137 } 1138 } 1139 return m_system_arch; 1140 } 1141 1142 1143 Error 1144 Platform::ConnectRemote (Args& args) 1145 { 1146 Error error; 1147 if (IsHost()) 1148 error.SetErrorStringWithFormat ("The currently selected platform (%s) is the host platform and is always connected.", GetPluginName().GetCString()); 1149 else 1150 error.SetErrorStringWithFormat ("Platform::ConnectRemote() is not supported by %s", GetPluginName().GetCString()); 1151 return error; 1152 } 1153 1154 Error 1155 Platform::DisconnectRemote () 1156 { 1157 Error error; 1158 if (IsHost()) 1159 error.SetErrorStringWithFormat ("The currently selected platform (%s) is the host platform and is always connected.", GetPluginName().GetCString()); 1160 else 1161 error.SetErrorStringWithFormat ("Platform::DisconnectRemote() is not supported by %s", GetPluginName().GetCString()); 1162 return error; 1163 } 1164 1165 bool 1166 Platform::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info) 1167 { 1168 // Take care of the host case so that each subclass can just 1169 // call this function to get the host functionality. 1170 if (IsHost()) 1171 return Host::GetProcessInfo (pid, process_info); 1172 return false; 1173 } 1174 1175 uint32_t 1176 Platform::FindProcesses (const ProcessInstanceInfoMatch &match_info, 1177 ProcessInstanceInfoList &process_infos) 1178 { 1179 // Take care of the host case so that each subclass can just 1180 // call this function to get the host functionality. 1181 uint32_t match_count = 0; 1182 if (IsHost()) 1183 match_count = Host::FindProcesses (match_info, process_infos); 1184 return match_count; 1185 } 1186 1187 1188 Error 1189 Platform::LaunchProcess (ProcessLaunchInfo &launch_info) 1190 { 1191 Error error; 1192 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM)); 1193 if (log) 1194 log->Printf ("Platform::%s()", __FUNCTION__); 1195 1196 // Take care of the host case so that each subclass can just 1197 // call this function to get the host functionality. 1198 if (IsHost()) 1199 { 1200 if (::getenv ("LLDB_LAUNCH_FLAG_LAUNCH_IN_TTY")) 1201 launch_info.GetFlags().Set (eLaunchFlagLaunchInTTY); 1202 1203 if (launch_info.GetFlags().Test (eLaunchFlagLaunchInShell)) 1204 { 1205 const bool is_localhost = true; 1206 const bool will_debug = launch_info.GetFlags().Test(eLaunchFlagDebug); 1207 const bool first_arg_is_full_shell_command = false; 1208 uint32_t num_resumes = GetResumeCountForLaunchInfo (launch_info); 1209 if (log) 1210 { 1211 const FileSpec &shell = launch_info.GetShell(); 1212 const char *shell_str = (shell) ? shell.GetPath().c_str() : "<null>"; 1213 log->Printf ("Platform::%s GetResumeCountForLaunchInfo() returned %" PRIu32 ", shell is '%s'", 1214 __FUNCTION__, 1215 num_resumes, 1216 shell_str); 1217 } 1218 1219 if (!launch_info.ConvertArgumentsForLaunchingInShell (error, 1220 is_localhost, 1221 will_debug, 1222 first_arg_is_full_shell_command, 1223 num_resumes)) 1224 return error; 1225 } 1226 else if (launch_info.GetFlags().Test(eLaunchFlagShellExpandArguments)) 1227 { 1228 error = ShellExpandArguments(launch_info); 1229 if (error.Fail()) 1230 return error; 1231 } 1232 1233 if (log) 1234 log->Printf ("Platform::%s final launch_info resume count: %" PRIu32, __FUNCTION__, launch_info.GetResumeCount ()); 1235 1236 error = Host::LaunchProcess (launch_info); 1237 } 1238 else 1239 error.SetErrorString ("base lldb_private::Platform class can't launch remote processes"); 1240 return error; 1241 } 1242 1243 Error 1244 Platform::ShellExpandArguments (ProcessLaunchInfo &launch_info) 1245 { 1246 if (IsHost()) 1247 return Host::ShellExpandArguments(launch_info); 1248 return Error("base lldb_private::Platform class can't expand arguments"); 1249 } 1250 1251 Error 1252 Platform::KillProcess (const lldb::pid_t pid) 1253 { 1254 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM)); 1255 if (log) 1256 log->Printf ("Platform::%s, pid %" PRIu64, __FUNCTION__, pid); 1257 1258 // Try to find a process plugin to handle this Kill request. If we can't, fall back to 1259 // the default OS implementation. 1260 size_t num_debuggers = Debugger::GetNumDebuggers(); 1261 for (size_t didx = 0; didx < num_debuggers; ++didx) 1262 { 1263 DebuggerSP debugger = Debugger::GetDebuggerAtIndex(didx); 1264 lldb_private::TargetList &targets = debugger->GetTargetList(); 1265 for (int tidx = 0; tidx < targets.GetNumTargets(); ++tidx) 1266 { 1267 ProcessSP process = targets.GetTargetAtIndex(tidx)->GetProcessSP(); 1268 if (process->GetID() == pid) 1269 return process->Destroy(true); 1270 } 1271 } 1272 1273 if (!IsHost()) 1274 { 1275 return Error("base lldb_private::Platform class can't kill remote processes unless " 1276 "they are controlled by a process plugin"); 1277 } 1278 Host::Kill(pid, SIGTERM); 1279 return Error(); 1280 } 1281 1282 lldb::ProcessSP 1283 Platform::DebugProcess (ProcessLaunchInfo &launch_info, 1284 Debugger &debugger, 1285 Target *target, // Can be NULL, if NULL create a new target, else use existing one 1286 Error &error) 1287 { 1288 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM)); 1289 if (log) 1290 log->Printf ("Platform::%s entered (target %p)", __FUNCTION__, static_cast<void*>(target)); 1291 1292 ProcessSP process_sp; 1293 // Make sure we stop at the entry point 1294 launch_info.GetFlags ().Set (eLaunchFlagDebug); 1295 // We always launch the process we are going to debug in a separate process 1296 // group, since then we can handle ^C interrupts ourselves w/o having to worry 1297 // about the target getting them as well. 1298 launch_info.SetLaunchInSeparateProcessGroup(true); 1299 1300 error = LaunchProcess (launch_info); 1301 if (error.Success()) 1302 { 1303 if (log) 1304 log->Printf ("Platform::%s LaunchProcess() call succeeded (pid=%" PRIu64 ")", __FUNCTION__, launch_info.GetProcessID ()); 1305 if (launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID) 1306 { 1307 ProcessAttachInfo attach_info (launch_info); 1308 process_sp = Attach (attach_info, debugger, target, error); 1309 if (process_sp) 1310 { 1311 if (log) 1312 log->Printf ("Platform::%s Attach() succeeded, Process plugin: %s", __FUNCTION__, process_sp->GetPluginName ().AsCString ()); 1313 launch_info.SetHijackListener(attach_info.GetHijackListener()); 1314 1315 // Since we attached to the process, it will think it needs to detach 1316 // if the process object just goes away without an explicit call to 1317 // Process::Kill() or Process::Detach(), so let it know to kill the 1318 // process if this happens. 1319 process_sp->SetShouldDetach (false); 1320 1321 // If we didn't have any file actions, the pseudo terminal might 1322 // have been used where the slave side was given as the file to 1323 // open for stdin/out/err after we have already opened the master 1324 // so we can read/write stdin/out/err. 1325 int pty_fd = launch_info.GetPTY().ReleaseMasterFileDescriptor(); 1326 if (pty_fd != lldb_utility::PseudoTerminal::invalid_fd) 1327 { 1328 process_sp->SetSTDIOFileDescriptor(pty_fd); 1329 } 1330 } 1331 else 1332 { 1333 if (log) 1334 log->Printf ("Platform::%s Attach() failed: %s", __FUNCTION__, error.AsCString ()); 1335 } 1336 } 1337 else 1338 { 1339 if (log) 1340 log->Printf ("Platform::%s LaunchProcess() returned launch_info with invalid process id", __FUNCTION__); 1341 } 1342 } 1343 else 1344 { 1345 if (log) 1346 log->Printf ("Platform::%s LaunchProcess() failed: %s", __FUNCTION__, error.AsCString ()); 1347 } 1348 1349 return process_sp; 1350 } 1351 1352 1353 lldb::PlatformSP 1354 Platform::GetPlatformForArchitecture (const ArchSpec &arch, ArchSpec *platform_arch_ptr) 1355 { 1356 lldb::PlatformSP platform_sp; 1357 Error error; 1358 if (arch.IsValid()) 1359 platform_sp = Platform::Create (arch, platform_arch_ptr, error); 1360 return platform_sp; 1361 } 1362 1363 1364 //------------------------------------------------------------------ 1365 /// Lets a platform answer if it is compatible with a given 1366 /// architecture and the target triple contained within. 1367 //------------------------------------------------------------------ 1368 bool 1369 Platform::IsCompatibleArchitecture (const ArchSpec &arch, bool exact_arch_match, ArchSpec *compatible_arch_ptr) 1370 { 1371 // If the architecture is invalid, we must answer true... 1372 if (arch.IsValid()) 1373 { 1374 ArchSpec platform_arch; 1375 // Try for an exact architecture match first. 1376 if (exact_arch_match) 1377 { 1378 for (uint32_t arch_idx=0; GetSupportedArchitectureAtIndex (arch_idx, platform_arch); ++arch_idx) 1379 { 1380 if (arch.IsExactMatch(platform_arch)) 1381 { 1382 if (compatible_arch_ptr) 1383 *compatible_arch_ptr = platform_arch; 1384 return true; 1385 } 1386 } 1387 } 1388 else 1389 { 1390 for (uint32_t arch_idx=0; GetSupportedArchitectureAtIndex (arch_idx, platform_arch); ++arch_idx) 1391 { 1392 if (arch.IsCompatibleMatch(platform_arch)) 1393 { 1394 if (compatible_arch_ptr) 1395 *compatible_arch_ptr = platform_arch; 1396 return true; 1397 } 1398 } 1399 } 1400 } 1401 if (compatible_arch_ptr) 1402 compatible_arch_ptr->Clear(); 1403 return false; 1404 } 1405 1406 Error 1407 Platform::PutFile (const FileSpec& source, 1408 const FileSpec& destination, 1409 uint32_t uid, 1410 uint32_t gid) 1411 { 1412 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM)); 1413 if (log) 1414 log->Printf("[PutFile] Using block by block transfer....\n"); 1415 1416 uint32_t source_open_options = File::eOpenOptionRead | File::eOpenOptionCloseOnExec; 1417 if (source.GetFileType() == FileSpec::eFileTypeSymbolicLink) 1418 source_open_options |= File::eOpenoptionDontFollowSymlinks; 1419 1420 File source_file(source, source_open_options, lldb::eFilePermissionsUserRW); 1421 Error error; 1422 uint32_t permissions = source_file.GetPermissions(error); 1423 if (permissions == 0) 1424 permissions = lldb::eFilePermissionsFileDefault; 1425 1426 if (!source_file.IsValid()) 1427 return Error("PutFile: unable to open source file"); 1428 lldb::user_id_t dest_file = OpenFile (destination, 1429 File::eOpenOptionCanCreate | 1430 File::eOpenOptionWrite | 1431 File::eOpenOptionTruncate | 1432 File::eOpenOptionCloseOnExec, 1433 permissions, 1434 error); 1435 if (log) 1436 log->Printf ("dest_file = %" PRIu64 "\n", dest_file); 1437 1438 if (error.Fail()) 1439 return error; 1440 if (dest_file == UINT64_MAX) 1441 return Error("unable to open target file"); 1442 lldb::DataBufferSP buffer_sp(new DataBufferHeap(1024, 0)); 1443 uint64_t offset = 0; 1444 for (;;) 1445 { 1446 size_t bytes_read = buffer_sp->GetByteSize(); 1447 error = source_file.Read(buffer_sp->GetBytes(), bytes_read); 1448 if (error.Fail() || bytes_read == 0) 1449 break; 1450 1451 const uint64_t bytes_written = WriteFile(dest_file, offset, 1452 buffer_sp->GetBytes(), bytes_read, error); 1453 if (error.Fail()) 1454 break; 1455 1456 offset += bytes_written; 1457 if (bytes_written != bytes_read) 1458 { 1459 // We didn't write the correct number of bytes, so adjust 1460 // the file position in the source file we are reading from... 1461 source_file.SeekFromStart(offset); 1462 } 1463 } 1464 CloseFile(dest_file, error); 1465 1466 if (uid == UINT32_MAX && gid == UINT32_MAX) 1467 return error; 1468 1469 // TODO: ChownFile? 1470 1471 return error; 1472 } 1473 1474 Error 1475 Platform::GetFile(const FileSpec &source, 1476 const FileSpec &destination) 1477 { 1478 Error error("unimplemented"); 1479 return error; 1480 } 1481 1482 Error 1483 Platform::CreateSymlink(const FileSpec &src, // The name of the link is in src 1484 const FileSpec &dst) // The symlink points to dst 1485 { 1486 Error error("unimplemented"); 1487 return error; 1488 } 1489 1490 bool 1491 Platform::GetFileExists(const lldb_private::FileSpec &file_spec) 1492 { 1493 return false; 1494 } 1495 1496 Error 1497 Platform::Unlink(const FileSpec &path) 1498 { 1499 Error error("unimplemented"); 1500 return error; 1501 } 1502 1503 uint64_t 1504 Platform::ConvertMmapFlagsToPlatform(const ArchSpec &arch, unsigned flags) 1505 { 1506 uint64_t flags_platform = 0; 1507 if (flags & eMmapFlagsPrivate) 1508 flags_platform |= MAP_PRIVATE; 1509 if (flags & eMmapFlagsAnon) 1510 flags_platform |= MAP_ANON; 1511 return flags_platform; 1512 } 1513 1514 lldb_private::Error 1515 Platform::RunShellCommand(const char *command, // Shouldn't be NULL 1516 const FileSpec &working_dir, // Pass empty FileSpec to use the current working directory 1517 int *status_ptr, // Pass NULL if you don't want the process exit status 1518 int *signo_ptr, // Pass NULL if you don't want the signal that caused the process to exit 1519 std::string *command_output, // Pass NULL if you don't want the command output 1520 uint32_t timeout_sec) // Timeout in seconds to wait for shell program to finish 1521 { 1522 if (IsHost()) 1523 return Host::RunShellCommand (command, working_dir, status_ptr, signo_ptr, command_output, timeout_sec); 1524 else 1525 return Error("unimplemented"); 1526 } 1527 1528 1529 bool 1530 Platform::CalculateMD5 (const FileSpec& file_spec, 1531 uint64_t &low, 1532 uint64_t &high) 1533 { 1534 if (IsHost()) 1535 return FileSystem::CalculateMD5(file_spec, low, high); 1536 else 1537 return false; 1538 } 1539 1540 void 1541 Platform::SetLocalCacheDirectory (const char* local) 1542 { 1543 m_local_cache_directory.assign(local); 1544 } 1545 1546 const char* 1547 Platform::GetLocalCacheDirectory () 1548 { 1549 return m_local_cache_directory.c_str(); 1550 } 1551 1552 static OptionDefinition 1553 g_rsync_option_table[] = 1554 { 1555 { LLDB_OPT_SET_ALL, false, "rsync" , 'r', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone , "Enable rsync." }, 1556 { LLDB_OPT_SET_ALL, false, "rsync-opts" , 'R', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeCommandName , "Platform-specific options required for rsync to work." }, 1557 { LLDB_OPT_SET_ALL, false, "rsync-prefix" , 'P', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeCommandName , "Platform-specific rsync prefix put before the remote path." }, 1558 { 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." }, 1559 }; 1560 1561 static OptionDefinition 1562 g_ssh_option_table[] = 1563 { 1564 { LLDB_OPT_SET_ALL, false, "ssh" , 's', OptionParser::eNoArgument, NULL, NULL, 0, eArgTypeNone , "Enable SSH." }, 1565 { LLDB_OPT_SET_ALL, false, "ssh-opts" , 'S', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeCommandName , "Platform-specific options required for SSH to work." }, 1566 }; 1567 1568 static OptionDefinition 1569 g_caching_option_table[] = 1570 { 1571 { LLDB_OPT_SET_ALL, false, "local-cache-dir" , 'c', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypePath , "Path in which to store local copies of files." }, 1572 }; 1573 1574 OptionGroupPlatformRSync::OptionGroupPlatformRSync () 1575 { 1576 } 1577 1578 OptionGroupPlatformRSync::~OptionGroupPlatformRSync () 1579 { 1580 } 1581 1582 const lldb_private::OptionDefinition* 1583 OptionGroupPlatformRSync::GetDefinitions () 1584 { 1585 return g_rsync_option_table; 1586 } 1587 1588 void 1589 OptionGroupPlatformRSync::OptionParsingStarting (CommandInterpreter &interpreter) 1590 { 1591 m_rsync = false; 1592 m_rsync_opts.clear(); 1593 m_rsync_prefix.clear(); 1594 m_ignores_remote_hostname = false; 1595 } 1596 1597 lldb_private::Error 1598 OptionGroupPlatformRSync::SetOptionValue (CommandInterpreter &interpreter, 1599 uint32_t option_idx, 1600 const char *option_arg) 1601 { 1602 Error error; 1603 char short_option = (char) GetDefinitions()[option_idx].short_option; 1604 switch (short_option) 1605 { 1606 case 'r': 1607 m_rsync = true; 1608 break; 1609 1610 case 'R': 1611 m_rsync_opts.assign(option_arg); 1612 break; 1613 1614 case 'P': 1615 m_rsync_prefix.assign(option_arg); 1616 break; 1617 1618 case 'i': 1619 m_ignores_remote_hostname = true; 1620 break; 1621 1622 default: 1623 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option); 1624 break; 1625 } 1626 1627 return error; 1628 } 1629 1630 uint32_t 1631 OptionGroupPlatformRSync::GetNumDefinitions () 1632 { 1633 return llvm::array_lengthof(g_rsync_option_table); 1634 } 1635 1636 lldb::BreakpointSP 1637 Platform::SetThreadCreationBreakpoint (lldb_private::Target &target) 1638 { 1639 return lldb::BreakpointSP(); 1640 } 1641 1642 OptionGroupPlatformSSH::OptionGroupPlatformSSH () 1643 { 1644 } 1645 1646 OptionGroupPlatformSSH::~OptionGroupPlatformSSH () 1647 { 1648 } 1649 1650 const lldb_private::OptionDefinition* 1651 OptionGroupPlatformSSH::GetDefinitions () 1652 { 1653 return g_ssh_option_table; 1654 } 1655 1656 void 1657 OptionGroupPlatformSSH::OptionParsingStarting (CommandInterpreter &interpreter) 1658 { 1659 m_ssh = false; 1660 m_ssh_opts.clear(); 1661 } 1662 1663 lldb_private::Error 1664 OptionGroupPlatformSSH::SetOptionValue (CommandInterpreter &interpreter, 1665 uint32_t option_idx, 1666 const char *option_arg) 1667 { 1668 Error error; 1669 char short_option = (char) GetDefinitions()[option_idx].short_option; 1670 switch (short_option) 1671 { 1672 case 's': 1673 m_ssh = true; 1674 break; 1675 1676 case 'S': 1677 m_ssh_opts.assign(option_arg); 1678 break; 1679 1680 default: 1681 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option); 1682 break; 1683 } 1684 1685 return error; 1686 } 1687 1688 uint32_t 1689 OptionGroupPlatformSSH::GetNumDefinitions () 1690 { 1691 return llvm::array_lengthof(g_ssh_option_table); 1692 } 1693 1694 OptionGroupPlatformCaching::OptionGroupPlatformCaching () 1695 { 1696 } 1697 1698 OptionGroupPlatformCaching::~OptionGroupPlatformCaching () 1699 { 1700 } 1701 1702 const lldb_private::OptionDefinition* 1703 OptionGroupPlatformCaching::GetDefinitions () 1704 { 1705 return g_caching_option_table; 1706 } 1707 1708 void 1709 OptionGroupPlatformCaching::OptionParsingStarting (CommandInterpreter &interpreter) 1710 { 1711 m_cache_dir.clear(); 1712 } 1713 1714 lldb_private::Error 1715 OptionGroupPlatformCaching::SetOptionValue (CommandInterpreter &interpreter, 1716 uint32_t option_idx, 1717 const char *option_arg) 1718 { 1719 Error error; 1720 char short_option = (char) GetDefinitions()[option_idx].short_option; 1721 switch (short_option) 1722 { 1723 case 'c': 1724 m_cache_dir.assign(option_arg); 1725 break; 1726 1727 default: 1728 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option); 1729 break; 1730 } 1731 1732 return error; 1733 } 1734 1735 uint32_t 1736 OptionGroupPlatformCaching::GetNumDefinitions () 1737 { 1738 return llvm::array_lengthof(g_caching_option_table); 1739 } 1740 1741 size_t 1742 Platform::GetEnvironment (StringList &environment) 1743 { 1744 environment.Clear(); 1745 return false; 1746 } 1747 1748 const std::vector<ConstString> & 1749 Platform::GetTrapHandlerSymbolNames () 1750 { 1751 if (!m_calculated_trap_handlers) 1752 { 1753 Mutex::Locker locker (m_mutex); 1754 if (!m_calculated_trap_handlers) 1755 { 1756 CalculateTrapHandlerSymbolNames(); 1757 m_calculated_trap_handlers = true; 1758 } 1759 } 1760 return m_trap_handlers; 1761 } 1762 1763 Error 1764 Platform::GetCachedExecutable (ModuleSpec &module_spec, 1765 lldb::ModuleSP &module_sp, 1766 const FileSpecList *module_search_paths_ptr, 1767 Platform &remote_platform) 1768 { 1769 const auto platform_spec = module_spec.GetFileSpec (); 1770 const auto error = LoadCachedExecutable (module_spec, 1771 module_sp, 1772 module_search_paths_ptr, 1773 remote_platform); 1774 if (error.Success ()) 1775 { 1776 module_spec.GetFileSpec () = module_sp->GetFileSpec (); 1777 module_spec.GetPlatformFileSpec () = platform_spec; 1778 } 1779 1780 return error; 1781 } 1782 1783 Error 1784 Platform::LoadCachedExecutable (const ModuleSpec &module_spec, 1785 lldb::ModuleSP &module_sp, 1786 const FileSpecList *module_search_paths_ptr, 1787 Platform &remote_platform) 1788 { 1789 return GetRemoteSharedModule (module_spec, 1790 nullptr, 1791 module_sp, 1792 [&](const ModuleSpec &spec) 1793 { 1794 return remote_platform.ResolveExecutable ( 1795 spec, module_sp, module_search_paths_ptr); 1796 }, 1797 nullptr); 1798 } 1799 1800 Error 1801 Platform::GetRemoteSharedModule (const ModuleSpec &module_spec, 1802 Process* process, 1803 lldb::ModuleSP &module_sp, 1804 const ModuleResolver &module_resolver, 1805 bool *did_create_ptr) 1806 { 1807 // Get module information from a target. 1808 ModuleSpec resolved_module_spec; 1809 bool got_module_spec = false; 1810 if (process) 1811 { 1812 // Try to get module information from the process 1813 if (process->GetModuleSpec (module_spec.GetFileSpec (), module_spec.GetArchitecture (), resolved_module_spec)) 1814 got_module_spec = true; 1815 } 1816 1817 if (!got_module_spec) 1818 { 1819 // Get module information from a target. 1820 if (!GetModuleSpec (module_spec.GetFileSpec (), module_spec.GetArchitecture (), resolved_module_spec)) 1821 return module_resolver (module_spec); 1822 } 1823 1824 // Trying to find a module by UUID on local file system. 1825 const auto error = module_resolver (resolved_module_spec); 1826 if (error.Fail ()) 1827 { 1828 if (GetCachedSharedModule (resolved_module_spec, module_sp, did_create_ptr)) 1829 return Error (); 1830 } 1831 1832 return error; 1833 } 1834 1835 bool 1836 Platform::GetCachedSharedModule (const ModuleSpec &module_spec, 1837 lldb::ModuleSP &module_sp, 1838 bool *did_create_ptr) 1839 { 1840 if (IsHost() || 1841 !GetGlobalPlatformProperties ()->GetUseModuleCache ()) 1842 return false; 1843 1844 Log *log = GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PLATFORM); 1845 1846 // Check local cache for a module. 1847 auto error = m_module_cache->GetAndPut ( 1848 GetModuleCacheRoot (), 1849 GetCacheHostname (), 1850 module_spec, 1851 [=](const ModuleSpec &module_spec, const FileSpec &tmp_download_file_spec) 1852 { 1853 return DownloadModuleSlice (module_spec.GetFileSpec (), 1854 module_spec.GetObjectOffset (), 1855 module_spec.GetObjectSize (), 1856 tmp_download_file_spec); 1857 1858 }, 1859 module_sp, 1860 did_create_ptr); 1861 if (error.Success ()) 1862 return true; 1863 1864 if (log) 1865 log->Printf("Platform::%s - module %s not found in local cache: %s", 1866 __FUNCTION__, module_spec.GetUUID ().GetAsString ().c_str (), error.AsCString ()); 1867 return false; 1868 } 1869 1870 Error 1871 Platform::DownloadModuleSlice (const FileSpec& src_file_spec, 1872 const uint64_t src_offset, 1873 const uint64_t src_size, 1874 const FileSpec& dst_file_spec) 1875 { 1876 Error error; 1877 1878 std::ofstream dst (dst_file_spec.GetPath(), std::ios::out | std::ios::binary); 1879 if (!dst.is_open()) 1880 { 1881 error.SetErrorStringWithFormat ("unable to open destination file: %s", dst_file_spec.GetPath ().c_str ()); 1882 return error; 1883 } 1884 1885 auto src_fd = OpenFile (src_file_spec, 1886 File::eOpenOptionRead, 1887 lldb::eFilePermissionsFileDefault, 1888 error); 1889 1890 if (error.Fail ()) 1891 { 1892 error.SetErrorStringWithFormat ("unable to open source file: %s", error.AsCString ()); 1893 return error; 1894 } 1895 1896 std::vector<char> buffer (1024); 1897 auto offset = src_offset; 1898 uint64_t total_bytes_read = 0; 1899 while (total_bytes_read < src_size) 1900 { 1901 const auto to_read = std::min (static_cast<uint64_t>(buffer.size ()), src_size - total_bytes_read); 1902 const uint64_t n_read = ReadFile (src_fd, offset, &buffer[0], to_read, error); 1903 if (error.Fail ()) 1904 break; 1905 if (n_read == 0) 1906 { 1907 error.SetErrorString ("read 0 bytes"); 1908 break; 1909 } 1910 offset += n_read; 1911 total_bytes_read += n_read; 1912 dst.write (&buffer[0], n_read); 1913 } 1914 1915 Error close_error; 1916 CloseFile (src_fd, close_error); // Ignoring close error. 1917 1918 return error; 1919 } 1920 1921 FileSpec 1922 Platform::GetModuleCacheRoot () 1923 { 1924 auto dir_spec = GetGlobalPlatformProperties ()->GetModuleCacheDirectory (); 1925 dir_spec.AppendPathComponent (GetName ().AsCString ()); 1926 return dir_spec; 1927 } 1928 1929 const char * 1930 Platform::GetCacheHostname () 1931 { 1932 return GetHostname (); 1933 } 1934 1935 const UnixSignalsSP & 1936 Platform::GetRemoteUnixSignals() 1937 { 1938 static const auto s_default_unix_signals_sp = std::make_shared<UnixSignals>(); 1939 return s_default_unix_signals_sp; 1940 } 1941 1942 const UnixSignalsSP & 1943 Platform::GetUnixSignals() 1944 { 1945 if (IsHost()) 1946 return Host::GetUnixSignals(); 1947 return GetRemoteUnixSignals(); 1948 } 1949