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