1 //===-- PlatformLinux.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/lldb-python.h" 11 12 #include "PlatformLinux.h" 13 #include "lldb/Host/Config.h" 14 15 // C Includes 16 #include <stdio.h> 17 #ifndef LLDB_DISABLE_POSIX 18 #include <sys/utsname.h> 19 #endif 20 21 // C++ Includes 22 // Other libraries and framework includes 23 // Project includes 24 #include "lldb/Breakpoint/BreakpointLocation.h" 25 #include "lldb/Core/Debugger.h" 26 #include "lldb/Core/Error.h" 27 #include "lldb/Core/Log.h" 28 #include "lldb/Core/Module.h" 29 #include "lldb/Core/ModuleList.h" 30 #include "lldb/Core/ModuleSpec.h" 31 #include "lldb/Core/PluginManager.h" 32 #include "lldb/Core/State.h" 33 #include "lldb/Core/StreamString.h" 34 #include "lldb/Host/FileSpec.h" 35 #include "lldb/Host/HostInfo.h" 36 #include "lldb/Interpreter/OptionValueProperties.h" 37 #include "lldb/Interpreter/Property.h" 38 #include "lldb/Target/Target.h" 39 #include "lldb/Target/Process.h" 40 41 #if defined(__linux__) 42 #include "../../Process/Linux/NativeProcessLinux.h" 43 #endif 44 45 using namespace lldb; 46 using namespace lldb_private; 47 using namespace lldb_private::platform_linux; 48 49 static uint32_t g_initialize_count = 0; 50 51 //------------------------------------------------------------------ 52 /// Code to handle the PlatformLinux settings 53 //------------------------------------------------------------------ 54 55 namespace 56 { 57 enum 58 { 59 ePropertyUseLlgsForLocal = 0, 60 }; 61 62 class PlatformLinuxProperties : public Properties 63 { 64 public: 65 static ConstString& 66 GetSettingName (); 67 68 PlatformLinuxProperties(); 69 70 virtual 71 ~PlatformLinuxProperties() = default; 72 73 bool 74 GetUseLlgsForLocal() const; 75 76 private: 77 static const PropertyDefinition* 78 GetStaticPropertyDefinitions(); 79 }; 80 81 typedef std::shared_ptr<PlatformLinuxProperties> PlatformLinuxPropertiesSP; 82 83 } // anonymous namespace 84 85 PlatformLinuxProperties::PlatformLinuxProperties() : 86 Properties () 87 { 88 m_collection_sp.reset (new OptionValueProperties(GetSettingName ())); 89 m_collection_sp->Initialize (GetStaticPropertyDefinitions ()); 90 } 91 92 ConstString& 93 PlatformLinuxProperties::GetSettingName () 94 { 95 static ConstString g_setting_name("linux"); 96 return g_setting_name; 97 } 98 99 bool 100 PlatformLinuxProperties::GetUseLlgsForLocal() const 101 { 102 const uint32_t idx = ePropertyUseLlgsForLocal; 103 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, GetStaticPropertyDefinitions()[idx].default_uint_value != 0); 104 } 105 106 const PropertyDefinition* 107 PlatformLinuxProperties::GetStaticPropertyDefinitions() 108 { 109 static PropertyDefinition 110 g_properties[] = 111 { 112 { "use-llgs-for-local" , OptionValue::eTypeBoolean, true, true, NULL, NULL, "Control whether the platform uses llgs for local debug sessions." }, 113 { NULL , OptionValue::eTypeInvalid, false, 0 , NULL, NULL, NULL } 114 }; 115 116 // Allow environment variable to disable llgs-local. 117 if (getenv("PLATFORM_LINUX_DISABLE_LLGS_LOCAL")) 118 g_properties[ePropertyUseLlgsForLocal].default_uint_value = false; 119 120 return g_properties; 121 } 122 123 static const PlatformLinuxPropertiesSP & 124 GetGlobalProperties() 125 { 126 static PlatformLinuxPropertiesSP g_settings_sp; 127 if (!g_settings_sp) 128 g_settings_sp.reset (new PlatformLinuxProperties ()); 129 return g_settings_sp; 130 } 131 132 void 133 PlatformLinux::DebuggerInitialize (Debugger &debugger) 134 { 135 if (!PluginManager::GetSettingForPlatformPlugin (debugger, PlatformLinuxProperties::GetSettingName())) 136 { 137 const bool is_global_setting = true; 138 PluginManager::CreateSettingForPlatformPlugin (debugger, 139 GetGlobalProperties()->GetValueProperties(), 140 ConstString ("Properties for the PlatformLinux plug-in."), 141 is_global_setting); 142 } 143 } 144 145 146 //------------------------------------------------------------------ 147 148 PlatformSP 149 PlatformLinux::CreateInstance (bool force, const ArchSpec *arch) 150 { 151 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM)); 152 if (log) 153 { 154 const char *arch_name; 155 if (arch && arch->GetArchitectureName ()) 156 arch_name = arch->GetArchitectureName (); 157 else 158 arch_name = "<null>"; 159 160 const char *triple_cstr = arch ? arch->GetTriple ().getTriple ().c_str() : "<null>"; 161 162 log->Printf ("PlatformLinux::%s(force=%s, arch={%s,%s})", __FUNCTION__, force ? "true" : "false", arch_name, triple_cstr); 163 } 164 165 bool create = force; 166 if (create == false && arch && arch->IsValid()) 167 { 168 const llvm::Triple &triple = arch->GetTriple(); 169 switch (triple.getVendor()) 170 { 171 case llvm::Triple::PC: 172 create = true; 173 break; 174 175 #if defined(__linux__) 176 // Only accept "unknown" for the vendor if the host is linux and 177 // it "unknown" wasn't specified (it was just returned because it 178 // was NOT specified_ 179 case llvm::Triple::VendorType::UnknownVendor: 180 create = !arch->TripleVendorWasSpecified(); 181 break; 182 #endif 183 default: 184 break; 185 } 186 187 if (create) 188 { 189 switch (triple.getOS()) 190 { 191 case llvm::Triple::Linux: 192 break; 193 194 #if defined(__linux__) 195 // Only accept "unknown" for the OS if the host is linux and 196 // it "unknown" wasn't specified (it was just returned because it 197 // was NOT specified) 198 case llvm::Triple::OSType::UnknownOS: 199 create = !arch->TripleOSWasSpecified(); 200 break; 201 #endif 202 default: 203 create = false; 204 break; 205 } 206 } 207 } 208 209 if (create) 210 { 211 if (log) 212 log->Printf ("PlatformLinux::%s() creating remote-linux platform", __FUNCTION__); 213 return PlatformSP(new PlatformLinux(false)); 214 } 215 216 if (log) 217 log->Printf ("PlatformLinux::%s() aborting creation of remote-linux platform", __FUNCTION__); 218 219 return PlatformSP(); 220 } 221 222 223 ConstString 224 PlatformLinux::GetPluginNameStatic (bool is_host) 225 { 226 if (is_host) 227 { 228 static ConstString g_host_name(Platform::GetHostPlatformName ()); 229 return g_host_name; 230 } 231 else 232 { 233 static ConstString g_remote_name("remote-linux"); 234 return g_remote_name; 235 } 236 } 237 238 const char * 239 PlatformLinux::GetPluginDescriptionStatic (bool is_host) 240 { 241 if (is_host) 242 return "Local Linux user platform plug-in."; 243 else 244 return "Remote Linux user platform plug-in."; 245 } 246 247 ConstString 248 PlatformLinux::GetPluginName() 249 { 250 return GetPluginNameStatic(IsHost()); 251 } 252 253 void 254 PlatformLinux::Initialize () 255 { 256 PlatformPOSIX::Initialize (); 257 258 if (g_initialize_count++ == 0) 259 { 260 #if defined(__linux__) && !defined(__ANDROID__) 261 PlatformSP default_platform_sp (new PlatformLinux(true)); 262 default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture()); 263 Platform::SetHostPlatform (default_platform_sp); 264 #endif 265 PluginManager::RegisterPlugin(PlatformLinux::GetPluginNameStatic(false), 266 PlatformLinux::GetPluginDescriptionStatic(false), 267 PlatformLinux::CreateInstance, 268 PlatformLinux::DebuggerInitialize); 269 } 270 } 271 272 void 273 PlatformLinux::Terminate () 274 { 275 if (g_initialize_count > 0) 276 { 277 if (--g_initialize_count == 0) 278 { 279 PluginManager::UnregisterPlugin (PlatformLinux::CreateInstance); 280 } 281 } 282 283 PlatformPOSIX::Terminate (); 284 } 285 286 Error 287 PlatformLinux::ResolveExecutable (const ModuleSpec &ms, 288 lldb::ModuleSP &exe_module_sp, 289 const FileSpecList *module_search_paths_ptr) 290 { 291 Error error; 292 // Nothing special to do here, just use the actual file and architecture 293 294 char exe_path[PATH_MAX]; 295 ModuleSpec resolved_module_spec (ms); 296 297 if (IsHost()) 298 { 299 // If we have "ls" as the exe_file, resolve the executable location based on 300 // the current path variables 301 if (!resolved_module_spec.GetFileSpec().Exists()) 302 { 303 resolved_module_spec.GetFileSpec().GetPath(exe_path, sizeof(exe_path)); 304 resolved_module_spec.GetFileSpec().SetFile(exe_path, true); 305 } 306 307 if (!resolved_module_spec.GetFileSpec().Exists()) 308 resolved_module_spec.GetFileSpec().ResolveExecutableLocation (); 309 310 if (resolved_module_spec.GetFileSpec().Exists()) 311 error.Clear(); 312 else 313 { 314 error.SetErrorStringWithFormat("unable to find executable for '%s'", resolved_module_spec.GetFileSpec().GetPath().c_str()); 315 } 316 } 317 else 318 { 319 if (m_remote_platform_sp) 320 { 321 error = GetCachedExecutable (resolved_module_spec, exe_module_sp, nullptr, *m_remote_platform_sp); 322 } 323 else 324 { 325 // We may connect to a process and use the provided executable (Don't use local $PATH). 326 327 if (resolved_module_spec.GetFileSpec().Exists()) 328 error.Clear(); 329 else 330 error.SetErrorStringWithFormat("the platform is not currently connected, and '%s' doesn't exist in the system root.", exe_path); 331 } 332 } 333 334 if (error.Success()) 335 { 336 if (resolved_module_spec.GetArchitecture().IsValid()) 337 { 338 error = ModuleList::GetSharedModule (resolved_module_spec, 339 exe_module_sp, 340 NULL, 341 NULL, 342 NULL); 343 if (error.Fail()) 344 { 345 // If we failed, it may be because the vendor and os aren't known. If that is the 346 // case, try setting them to the host architecture and give it another try. 347 llvm::Triple &module_triple = resolved_module_spec.GetArchitecture().GetTriple(); 348 bool is_vendor_specified = (module_triple.getVendor() != llvm::Triple::UnknownVendor); 349 bool is_os_specified = (module_triple.getOS() != llvm::Triple::UnknownOS); 350 if (!is_vendor_specified || !is_os_specified) 351 { 352 const llvm::Triple &host_triple = HostInfo::GetArchitecture(HostInfo::eArchKindDefault).GetTriple(); 353 354 if (!is_vendor_specified) 355 module_triple.setVendorName (host_triple.getVendorName()); 356 if (!is_os_specified) 357 module_triple.setOSName (host_triple.getOSName()); 358 359 error = ModuleList::GetSharedModule (resolved_module_spec, 360 exe_module_sp, 361 NULL, 362 NULL, 363 NULL); 364 } 365 } 366 367 // TODO find out why exe_module_sp might be NULL 368 if (!exe_module_sp || exe_module_sp->GetObjectFile() == NULL) 369 { 370 exe_module_sp.reset(); 371 error.SetErrorStringWithFormat ("'%s' doesn't contain the architecture %s", 372 resolved_module_spec.GetFileSpec().GetPath().c_str(), 373 resolved_module_spec.GetArchitecture().GetArchitectureName()); 374 } 375 } 376 else 377 { 378 // No valid architecture was specified, ask the platform for 379 // the architectures that we should be using (in the correct order) 380 // and see if we can find a match that way 381 StreamString arch_names; 382 for (uint32_t idx = 0; GetSupportedArchitectureAtIndex (idx, resolved_module_spec.GetArchitecture()); ++idx) 383 { 384 error = ModuleList::GetSharedModule (resolved_module_spec, 385 exe_module_sp, 386 NULL, 387 NULL, 388 NULL); 389 // Did we find an executable using one of the 390 if (error.Success()) 391 { 392 if (exe_module_sp && exe_module_sp->GetObjectFile()) 393 break; 394 else 395 error.SetErrorToGenericError(); 396 } 397 398 if (idx > 0) 399 arch_names.PutCString (", "); 400 arch_names.PutCString (resolved_module_spec.GetArchitecture().GetArchitectureName()); 401 } 402 403 if (error.Fail() || !exe_module_sp) 404 { 405 if (resolved_module_spec.GetFileSpec().Readable()) 406 { 407 error.SetErrorStringWithFormat ("'%s' doesn't contain any '%s' platform architectures: %s", 408 resolved_module_spec.GetFileSpec().GetPath().c_str(), 409 GetPluginName().GetCString(), 410 arch_names.GetString().c_str()); 411 } 412 else 413 { 414 error.SetErrorStringWithFormat("'%s' is not readable", resolved_module_spec.GetFileSpec().GetPath().c_str()); 415 } 416 } 417 } 418 } 419 420 return error; 421 } 422 423 Error 424 PlatformLinux::GetFileWithUUID (const FileSpec &platform_file, 425 const UUID *uuid_ptr, FileSpec &local_file) 426 { 427 if (IsRemote()) 428 { 429 if (m_remote_platform_sp) 430 return m_remote_platform_sp->GetFileWithUUID (platform_file, uuid_ptr, local_file); 431 } 432 433 // Default to the local case 434 local_file = platform_file; 435 return Error(); 436 } 437 438 439 //------------------------------------------------------------------ 440 /// Default Constructor 441 //------------------------------------------------------------------ 442 PlatformLinux::PlatformLinux (bool is_host) : 443 PlatformPOSIX(is_host) // This is the local host platform 444 { 445 } 446 447 //------------------------------------------------------------------ 448 /// Destructor. 449 /// 450 /// The destructor is virtual since this class is designed to be 451 /// inherited from by the plug-in instance. 452 //------------------------------------------------------------------ 453 PlatformLinux::~PlatformLinux() 454 { 455 } 456 457 bool 458 PlatformLinux::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info) 459 { 460 bool success = false; 461 if (IsHost()) 462 { 463 success = Platform::GetProcessInfo (pid, process_info); 464 } 465 else 466 { 467 if (m_remote_platform_sp) 468 success = m_remote_platform_sp->GetProcessInfo (pid, process_info); 469 } 470 return success; 471 } 472 473 uint32_t 474 PlatformLinux::FindProcesses (const ProcessInstanceInfoMatch &match_info, 475 ProcessInstanceInfoList &process_infos) 476 { 477 uint32_t match_count = 0; 478 if (IsHost()) 479 { 480 // Let the base class figure out the host details 481 match_count = Platform::FindProcesses (match_info, process_infos); 482 } 483 else 484 { 485 // If we are remote, we can only return results if we are connected 486 if (m_remote_platform_sp) 487 match_count = m_remote_platform_sp->FindProcesses (match_info, process_infos); 488 } 489 return match_count; 490 } 491 492 bool 493 PlatformLinux::GetSupportedArchitectureAtIndex (uint32_t idx, ArchSpec &arch) 494 { 495 if (idx == 0) 496 { 497 arch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault); 498 return arch.IsValid(); 499 } 500 else if (idx == 1) 501 { 502 // If the default host architecture is 64-bit, look for a 32-bit variant 503 ArchSpec hostArch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault); 504 if (hostArch.IsValid() && hostArch.GetTriple().isArch64Bit()) 505 { 506 arch = HostInfo::GetArchitecture(HostInfo::eArchKind32); 507 return arch.IsValid(); 508 } 509 } 510 return false; 511 } 512 513 void 514 PlatformLinux::GetStatus (Stream &strm) 515 { 516 Platform::GetStatus(strm); 517 518 #ifndef LLDB_DISABLE_POSIX 519 // Display local kernel information only when we are running in host mode. 520 // Otherwise, we would end up printing non-Linux information (when running 521 // on Mac OS for example). 522 if (IsHost()) 523 { 524 struct utsname un; 525 526 if (uname(&un)) 527 return; 528 529 strm.Printf (" Kernel: %s\n", un.sysname); 530 strm.Printf (" Release: %s\n", un.release); 531 strm.Printf (" Version: %s\n", un.version); 532 } 533 #endif 534 } 535 536 size_t 537 PlatformLinux::GetSoftwareBreakpointTrapOpcode (Target &target, 538 BreakpointSite *bp_site) 539 { 540 ArchSpec arch = target.GetArchitecture(); 541 const uint8_t *trap_opcode = NULL; 542 size_t trap_opcode_size = 0; 543 544 switch (arch.GetMachine()) 545 { 546 default: 547 assert(false && "CPU type not supported!"); 548 break; 549 550 case llvm::Triple::aarch64: 551 { 552 static const uint8_t g_aarch64_opcode[] = { 0x00, 0x00, 0x20, 0xd4 }; 553 trap_opcode = g_aarch64_opcode; 554 trap_opcode_size = sizeof(g_aarch64_opcode); 555 } 556 break; 557 case llvm::Triple::x86: 558 case llvm::Triple::x86_64: 559 { 560 static const uint8_t g_i386_breakpoint_opcode[] = { 0xCC }; 561 trap_opcode = g_i386_breakpoint_opcode; 562 trap_opcode_size = sizeof(g_i386_breakpoint_opcode); 563 } 564 break; 565 case llvm::Triple::hexagon: 566 { 567 static const uint8_t g_hex_opcode[] = { 0x0c, 0xdb, 0x00, 0x54 }; 568 trap_opcode = g_hex_opcode; 569 trap_opcode_size = sizeof(g_hex_opcode); 570 } 571 break; 572 case llvm::Triple::arm: 573 { 574 // The ARM reference recommends the use of 0xe7fddefe and 0xdefe 575 // but the linux kernel does otherwise. 576 static const uint8_t g_arm_breakpoint_opcode[] = { 0xf0, 0x01, 0xf0, 0xe7 }; 577 static const uint8_t g_thumb_breakpoint_opcode[] = { 0x01, 0xde }; 578 579 lldb::BreakpointLocationSP bp_loc_sp (bp_site->GetOwnerAtIndex (0)); 580 AddressClass addr_class = eAddressClassUnknown; 581 582 if (bp_loc_sp) 583 addr_class = bp_loc_sp->GetAddress ().GetAddressClass (); 584 585 if (addr_class == eAddressClassCodeAlternateISA 586 || (addr_class == eAddressClassUnknown 587 && bp_loc_sp->GetAddress().GetOffset() & 1)) 588 { 589 trap_opcode = g_thumb_breakpoint_opcode; 590 trap_opcode_size = sizeof(g_thumb_breakpoint_opcode); 591 } 592 else 593 { 594 trap_opcode = g_arm_breakpoint_opcode; 595 trap_opcode_size = sizeof(g_arm_breakpoint_opcode); 596 } 597 } 598 break; 599 case llvm::Triple::mips64: 600 case llvm::Triple::mips64el: 601 { 602 static const uint8_t g_hex_opcode[] = { 0x00, 0x00, 0x00, 0x0d }; 603 trap_opcode = g_hex_opcode; 604 trap_opcode_size = sizeof(g_hex_opcode); 605 } 606 break; 607 } 608 609 if (bp_site->SetTrapOpcode(trap_opcode, trap_opcode_size)) 610 return trap_opcode_size; 611 return 0; 612 } 613 614 int32_t 615 PlatformLinux::GetResumeCountForLaunchInfo (ProcessLaunchInfo &launch_info) 616 { 617 int32_t resume_count = 0; 618 619 // Always resume past the initial stop when we use eLaunchFlagDebug 620 if (launch_info.GetFlags ().Test (eLaunchFlagDebug)) 621 { 622 // Resume past the stop for the final exec into the true inferior. 623 ++resume_count; 624 } 625 626 // If we're not launching a shell, we're done. 627 const FileSpec &shell = launch_info.GetShell(); 628 if (!shell) 629 return resume_count; 630 631 std::string shell_string = shell.GetPath(); 632 // We're in a shell, so for sure we have to resume past the shell exec. 633 ++resume_count; 634 635 // Figure out what shell we're planning on using. 636 const char *shell_name = strrchr (shell_string.c_str(), '/'); 637 if (shell_name == NULL) 638 shell_name = shell_string.c_str(); 639 else 640 shell_name++; 641 642 if (strcmp (shell_name, "csh") == 0 643 || strcmp (shell_name, "tcsh") == 0 644 || strcmp (shell_name, "zsh") == 0 645 || strcmp (shell_name, "sh") == 0) 646 { 647 // These shells seem to re-exec themselves. Add another resume. 648 ++resume_count; 649 } 650 651 return resume_count; 652 } 653 654 bool 655 PlatformLinux::UseLlgsForLocalDebugging () 656 { 657 PlatformLinuxPropertiesSP properties_sp = GetGlobalProperties (); 658 assert (properties_sp && "global properties shared pointer is null"); 659 return properties_sp ? properties_sp->GetUseLlgsForLocal () : false; 660 } 661 662 bool 663 PlatformLinux::CanDebugProcess () 664 { 665 if (IsHost ()) 666 { 667 // The platform only does local debugging (i.e. uses llgs) when the setting indicates we do that. 668 // Otherwise, we'll use ProcessLinux/ProcessPOSIX to handle with ProcessMonitor. 669 return UseLlgsForLocalDebugging (); 670 } 671 else 672 { 673 // If we're connected, we can debug. 674 return IsConnected (); 675 } 676 } 677 678 // For local debugging, Linux will override the debug logic to use llgs-launch rather than 679 // lldb-launch, llgs-attach. This differs from current lldb-launch, debugserver-attach 680 // approach on MacOSX. 681 lldb::ProcessSP 682 PlatformLinux::DebugProcess (ProcessLaunchInfo &launch_info, 683 Debugger &debugger, 684 Target *target, // Can be NULL, if NULL create a new target, else use existing one 685 Error &error) 686 { 687 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM)); 688 if (log) 689 log->Printf ("PlatformLinux::%s entered (target %p)", __FUNCTION__, static_cast<void*>(target)); 690 691 // If we're a remote host, use standard behavior from parent class. 692 if (!IsHost ()) 693 return PlatformPOSIX::DebugProcess (launch_info, debugger, target, error); 694 695 // 696 // For local debugging, we'll insist on having ProcessGDBRemote create the process. 697 // 698 699 ProcessSP process_sp; 700 701 // Ensure we're using llgs for local debugging. 702 if (!UseLlgsForLocalDebugging ()) 703 { 704 assert (false && "we're trying to debug a local process but platform.plugin.linux.use-llgs-for-local is false, should never get here"); 705 error.SetErrorString ("attempted to start gdb-remote-based debugging for local process but platform.plugin.linux.use-llgs-for-local is false"); 706 return process_sp; 707 } 708 709 // Make sure we stop at the entry point 710 launch_info.GetFlags ().Set (eLaunchFlagDebug); 711 712 // We always launch the process we are going to debug in a separate process 713 // group, since then we can handle ^C interrupts ourselves w/o having to worry 714 // about the target getting them as well. 715 launch_info.SetLaunchInSeparateProcessGroup(true); 716 717 // Ensure we have a target. 718 if (target == nullptr) 719 { 720 if (log) 721 log->Printf ("PlatformLinux::%s creating new target", __FUNCTION__); 722 723 TargetSP new_target_sp; 724 error = debugger.GetTargetList().CreateTarget (debugger, 725 nullptr, 726 nullptr, 727 false, 728 nullptr, 729 new_target_sp); 730 if (error.Fail ()) 731 { 732 if (log) 733 log->Printf ("PlatformLinux::%s failed to create new target: %s", __FUNCTION__, error.AsCString ()); 734 return process_sp; 735 } 736 737 target = new_target_sp.get(); 738 if (!target) 739 { 740 error.SetErrorString ("CreateTarget() returned nullptr"); 741 if (log) 742 log->Printf ("PlatformLinux::%s failed: %s", __FUNCTION__, error.AsCString ()); 743 return process_sp; 744 } 745 } 746 else 747 { 748 if (log) 749 log->Printf ("PlatformLinux::%s using provided target", __FUNCTION__); 750 } 751 752 // Mark target as currently selected target. 753 debugger.GetTargetList().SetSelectedTarget(target); 754 755 // Now create the gdb-remote process. 756 if (log) 757 log->Printf ("PlatformLinux::%s having target create process with gdb-remote plugin", __FUNCTION__); 758 process_sp = target->CreateProcess (launch_info.GetListenerForProcess(debugger), "gdb-remote", nullptr); 759 760 if (!process_sp) 761 { 762 error.SetErrorString ("CreateProcess() failed for gdb-remote process"); 763 if (log) 764 log->Printf ("PlatformLinux::%s failed: %s", __FUNCTION__, error.AsCString ()); 765 return process_sp; 766 } 767 else 768 { 769 if (log) 770 log->Printf ("PlatformLinux::%s successfully created process", __FUNCTION__); 771 } 772 773 // Set the unix signals properly. 774 process_sp->SetUnixSignals (Host::GetUnixSignals ()); 775 776 // Adjust launch for a hijacker. 777 ListenerSP listener_sp; 778 if (!launch_info.GetHijackListener ()) 779 { 780 if (log) 781 log->Printf ("PlatformLinux::%s setting up hijacker", __FUNCTION__); 782 783 listener_sp.reset (new Listener("lldb.PlatformLinux.DebugProcess.hijack")); 784 launch_info.SetHijackListener (listener_sp); 785 process_sp->HijackProcessEvents (listener_sp.get ()); 786 } 787 788 // Log file actions. 789 if (log) 790 { 791 log->Printf ("PlatformLinux::%s launching process with the following file actions:", __FUNCTION__); 792 793 StreamString stream; 794 size_t i = 0; 795 const FileAction *file_action; 796 while ((file_action = launch_info.GetFileActionAtIndex (i++)) != nullptr) 797 { 798 file_action->Dump (stream); 799 log->PutCString (stream.GetString().c_str ()); 800 stream.Clear(); 801 } 802 } 803 804 // Do the launch. 805 error = process_sp->Launch(launch_info); 806 if (error.Success ()) 807 { 808 // Handle the hijacking of process events. 809 if (listener_sp) 810 { 811 const StateType state = process_sp->WaitForProcessToStop (NULL, NULL, false, listener_sp.get()); 812 813 if (state == eStateStopped) 814 { 815 if (log) 816 log->Printf ("PlatformLinux::%s pid %" PRIu64 " state %s\n", 817 __FUNCTION__, process_sp->GetID (), StateAsCString (state)); 818 } 819 else 820 { 821 if (log) 822 log->Printf ("PlatformLinux::%s pid %" PRIu64 " state is not stopped - %s\n", 823 __FUNCTION__, process_sp->GetID (), StateAsCString (state)); 824 } 825 } 826 827 // Hook up process PTY if we have one (which we should for local debugging with llgs). 828 int pty_fd = launch_info.GetPTY().ReleaseMasterFileDescriptor(); 829 if (pty_fd != lldb_utility::PseudoTerminal::invalid_fd) 830 { 831 process_sp->SetSTDIOFileDescriptor(pty_fd); 832 if (log) 833 log->Printf ("PlatformLinux::%s pid %" PRIu64 " hooked up STDIO pty to process", __FUNCTION__, process_sp->GetID ()); 834 } 835 else 836 { 837 if (log) 838 log->Printf ("PlatformLinux::%s pid %" PRIu64 " not using process STDIO pty", __FUNCTION__, process_sp->GetID ()); 839 } 840 } 841 else 842 { 843 if (log) 844 log->Printf ("PlatformLinux::%s process launch failed: %s", __FUNCTION__, error.AsCString ()); 845 // FIXME figure out appropriate cleanup here. Do we delete the target? Do we delete the process? Does our caller do that? 846 } 847 848 return process_sp; 849 } 850 851 void 852 PlatformLinux::CalculateTrapHandlerSymbolNames () 853 { 854 m_trap_handlers.push_back (ConstString ("_sigtramp")); 855 } 856 857 Error 858 PlatformLinux::LaunchNativeProcess (ProcessLaunchInfo &launch_info, 859 NativeProcessProtocol::NativeDelegate &native_delegate, 860 NativeProcessProtocolSP &process_sp) 861 { 862 #if !defined(__linux__) 863 return Error("Only implemented on Linux hosts"); 864 #else 865 if (!IsHost ()) 866 return Error("PlatformLinux::%s (): cannot launch a debug process when not the host", __FUNCTION__); 867 868 // Retrieve the exe module. 869 lldb::ModuleSP exe_module_sp; 870 ModuleSpec exe_module_spec(launch_info.GetExecutableFile(), launch_info.GetArchitecture()); 871 872 Error error = ResolveExecutable ( 873 exe_module_spec, 874 exe_module_sp, 875 NULL); 876 877 if (!error.Success ()) 878 return error; 879 880 if (!exe_module_sp) 881 return Error("exe_module_sp could not be resolved for %s", launch_info.GetExecutableFile ().GetPath ().c_str ()); 882 883 // Launch it for debugging 884 error = process_linux::NativeProcessLinux::LaunchProcess ( 885 exe_module_sp.get (), 886 launch_info, 887 native_delegate, 888 process_sp); 889 890 return error; 891 #endif 892 } 893 894 Error 895 PlatformLinux::AttachNativeProcess (lldb::pid_t pid, 896 NativeProcessProtocol::NativeDelegate &native_delegate, 897 NativeProcessProtocolSP &process_sp) 898 { 899 #if !defined(__linux__) 900 return Error("Only implemented on Linux hosts"); 901 #else 902 if (!IsHost ()) 903 return Error("PlatformLinux::%s (): cannot attach to a debug process when not the host", __FUNCTION__); 904 905 // Launch it for debugging 906 return process_linux::NativeProcessLinux::AttachToProcess (pid, native_delegate, process_sp); 907 #endif 908 } 909