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