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 { 601 static const uint8_t g_hex_opcode[] = { 0x00, 0x00, 0x00, 0x0d }; 602 trap_opcode = g_hex_opcode; 603 trap_opcode_size = sizeof(g_hex_opcode); 604 } 605 break; 606 case llvm::Triple::mips64el: 607 { 608 static const uint8_t g_hex_opcode[] = { 0x0d, 0x00, 0x00, 0x00 }; 609 trap_opcode = g_hex_opcode; 610 trap_opcode_size = sizeof(g_hex_opcode); 611 } 612 break; 613 } 614 615 if (bp_site->SetTrapOpcode(trap_opcode, trap_opcode_size)) 616 return trap_opcode_size; 617 return 0; 618 } 619 620 int32_t 621 PlatformLinux::GetResumeCountForLaunchInfo (ProcessLaunchInfo &launch_info) 622 { 623 int32_t resume_count = 0; 624 625 // Always resume past the initial stop when we use eLaunchFlagDebug 626 if (launch_info.GetFlags ().Test (eLaunchFlagDebug)) 627 { 628 // Resume past the stop for the final exec into the true inferior. 629 ++resume_count; 630 } 631 632 // If we're not launching a shell, we're done. 633 const FileSpec &shell = launch_info.GetShell(); 634 if (!shell) 635 return resume_count; 636 637 std::string shell_string = shell.GetPath(); 638 // We're in a shell, so for sure we have to resume past the shell exec. 639 ++resume_count; 640 641 // Figure out what shell we're planning on using. 642 const char *shell_name = strrchr (shell_string.c_str(), '/'); 643 if (shell_name == NULL) 644 shell_name = shell_string.c_str(); 645 else 646 shell_name++; 647 648 if (strcmp (shell_name, "csh") == 0 649 || strcmp (shell_name, "tcsh") == 0 650 || strcmp (shell_name, "zsh") == 0 651 || strcmp (shell_name, "sh") == 0) 652 { 653 // These shells seem to re-exec themselves. Add another resume. 654 ++resume_count; 655 } 656 657 return resume_count; 658 } 659 660 bool 661 PlatformLinux::UseLlgsForLocalDebugging () 662 { 663 PlatformLinuxPropertiesSP properties_sp = GetGlobalProperties (); 664 assert (properties_sp && "global properties shared pointer is null"); 665 return properties_sp ? properties_sp->GetUseLlgsForLocal () : false; 666 } 667 668 bool 669 PlatformLinux::CanDebugProcess () 670 { 671 if (IsHost ()) 672 { 673 // The platform only does local debugging (i.e. uses llgs) when the setting indicates we do that. 674 // Otherwise, we'll use ProcessLinux/ProcessPOSIX to handle with ProcessMonitor. 675 return UseLlgsForLocalDebugging (); 676 } 677 else 678 { 679 // If we're connected, we can debug. 680 return IsConnected (); 681 } 682 } 683 684 // For local debugging, Linux will override the debug logic to use llgs-launch rather than 685 // lldb-launch, llgs-attach. This differs from current lldb-launch, debugserver-attach 686 // approach on MacOSX. 687 lldb::ProcessSP 688 PlatformLinux::DebugProcess (ProcessLaunchInfo &launch_info, 689 Debugger &debugger, 690 Target *target, // Can be NULL, if NULL create a new target, else use existing one 691 Error &error) 692 { 693 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM)); 694 if (log) 695 log->Printf ("PlatformLinux::%s entered (target %p)", __FUNCTION__, static_cast<void*>(target)); 696 697 // If we're a remote host, use standard behavior from parent class. 698 if (!IsHost ()) 699 return PlatformPOSIX::DebugProcess (launch_info, debugger, target, error); 700 701 // 702 // For local debugging, we'll insist on having ProcessGDBRemote create the process. 703 // 704 705 ProcessSP process_sp; 706 707 // Ensure we're using llgs for local debugging. 708 if (!UseLlgsForLocalDebugging ()) 709 { 710 assert (false && "we're trying to debug a local process but platform.plugin.linux.use-llgs-for-local is false, should never get here"); 711 error.SetErrorString ("attempted to start gdb-remote-based debugging for local process but platform.plugin.linux.use-llgs-for-local is false"); 712 return process_sp; 713 } 714 715 // Make sure we stop at the entry point 716 launch_info.GetFlags ().Set (eLaunchFlagDebug); 717 718 // We always launch the process we are going to debug in a separate process 719 // group, since then we can handle ^C interrupts ourselves w/o having to worry 720 // about the target getting them as well. 721 launch_info.SetLaunchInSeparateProcessGroup(true); 722 723 // Ensure we have a target. 724 if (target == nullptr) 725 { 726 if (log) 727 log->Printf ("PlatformLinux::%s creating new target", __FUNCTION__); 728 729 TargetSP new_target_sp; 730 error = debugger.GetTargetList().CreateTarget (debugger, 731 nullptr, 732 nullptr, 733 false, 734 nullptr, 735 new_target_sp); 736 if (error.Fail ()) 737 { 738 if (log) 739 log->Printf ("PlatformLinux::%s failed to create new target: %s", __FUNCTION__, error.AsCString ()); 740 return process_sp; 741 } 742 743 target = new_target_sp.get(); 744 if (!target) 745 { 746 error.SetErrorString ("CreateTarget() returned nullptr"); 747 if (log) 748 log->Printf ("PlatformLinux::%s failed: %s", __FUNCTION__, error.AsCString ()); 749 return process_sp; 750 } 751 } 752 else 753 { 754 if (log) 755 log->Printf ("PlatformLinux::%s using provided target", __FUNCTION__); 756 } 757 758 // Mark target as currently selected target. 759 debugger.GetTargetList().SetSelectedTarget(target); 760 761 // Now create the gdb-remote process. 762 if (log) 763 log->Printf ("PlatformLinux::%s having target create process with gdb-remote plugin", __FUNCTION__); 764 process_sp = target->CreateProcess (launch_info.GetListenerForProcess(debugger), "gdb-remote", nullptr); 765 766 if (!process_sp) 767 { 768 error.SetErrorString ("CreateProcess() failed for gdb-remote process"); 769 if (log) 770 log->Printf ("PlatformLinux::%s failed: %s", __FUNCTION__, error.AsCString ()); 771 return process_sp; 772 } 773 else 774 { 775 if (log) 776 log->Printf ("PlatformLinux::%s successfully created process", __FUNCTION__); 777 } 778 779 // Set the unix signals properly. 780 process_sp->SetUnixSignals (Host::GetUnixSignals ()); 781 782 // Adjust launch for a hijacker. 783 ListenerSP listener_sp; 784 if (!launch_info.GetHijackListener ()) 785 { 786 if (log) 787 log->Printf ("PlatformLinux::%s setting up hijacker", __FUNCTION__); 788 789 listener_sp.reset (new Listener("lldb.PlatformLinux.DebugProcess.hijack")); 790 launch_info.SetHijackListener (listener_sp); 791 process_sp->HijackProcessEvents (listener_sp.get ()); 792 } 793 794 // Log file actions. 795 if (log) 796 { 797 log->Printf ("PlatformLinux::%s launching process with the following file actions:", __FUNCTION__); 798 799 StreamString stream; 800 size_t i = 0; 801 const FileAction *file_action; 802 while ((file_action = launch_info.GetFileActionAtIndex (i++)) != nullptr) 803 { 804 file_action->Dump (stream); 805 log->PutCString (stream.GetString().c_str ()); 806 stream.Clear(); 807 } 808 } 809 810 // Do the launch. 811 error = process_sp->Launch(launch_info); 812 if (error.Success ()) 813 { 814 // Handle the hijacking of process events. 815 if (listener_sp) 816 { 817 const StateType state = process_sp->WaitForProcessToStop (NULL, NULL, false, listener_sp.get()); 818 819 if (state == eStateStopped) 820 { 821 if (log) 822 log->Printf ("PlatformLinux::%s pid %" PRIu64 " state %s\n", 823 __FUNCTION__, process_sp->GetID (), StateAsCString (state)); 824 } 825 else 826 { 827 if (log) 828 log->Printf ("PlatformLinux::%s pid %" PRIu64 " state is not stopped - %s\n", 829 __FUNCTION__, process_sp->GetID (), StateAsCString (state)); 830 } 831 } 832 833 // Hook up process PTY if we have one (which we should for local debugging with llgs). 834 int pty_fd = launch_info.GetPTY().ReleaseMasterFileDescriptor(); 835 if (pty_fd != lldb_utility::PseudoTerminal::invalid_fd) 836 { 837 process_sp->SetSTDIOFileDescriptor(pty_fd); 838 if (log) 839 log->Printf ("PlatformLinux::%s pid %" PRIu64 " hooked up STDIO pty to process", __FUNCTION__, process_sp->GetID ()); 840 } 841 else 842 { 843 if (log) 844 log->Printf ("PlatformLinux::%s pid %" PRIu64 " not using process STDIO pty", __FUNCTION__, process_sp->GetID ()); 845 } 846 } 847 else 848 { 849 if (log) 850 log->Printf ("PlatformLinux::%s process launch failed: %s", __FUNCTION__, error.AsCString ()); 851 // FIXME figure out appropriate cleanup here. Do we delete the target? Do we delete the process? Does our caller do that? 852 } 853 854 return process_sp; 855 } 856 857 void 858 PlatformLinux::CalculateTrapHandlerSymbolNames () 859 { 860 m_trap_handlers.push_back (ConstString ("_sigtramp")); 861 } 862 863 Error 864 PlatformLinux::LaunchNativeProcess (ProcessLaunchInfo &launch_info, 865 NativeProcessProtocol::NativeDelegate &native_delegate, 866 NativeProcessProtocolSP &process_sp) 867 { 868 #if !defined(__linux__) 869 return Error("Only implemented on Linux hosts"); 870 #else 871 if (!IsHost ()) 872 return Error("PlatformLinux::%s (): cannot launch a debug process when not the host", __FUNCTION__); 873 874 // Retrieve the exe module. 875 lldb::ModuleSP exe_module_sp; 876 ModuleSpec exe_module_spec(launch_info.GetExecutableFile(), launch_info.GetArchitecture()); 877 878 Error error = ResolveExecutable ( 879 exe_module_spec, 880 exe_module_sp, 881 NULL); 882 883 if (!error.Success ()) 884 return error; 885 886 if (!exe_module_sp) 887 return Error("exe_module_sp could not be resolved for %s", launch_info.GetExecutableFile ().GetPath ().c_str ()); 888 889 // Launch it for debugging 890 error = process_linux::NativeProcessLinux::LaunchProcess ( 891 exe_module_sp.get (), 892 launch_info, 893 native_delegate, 894 process_sp); 895 896 return error; 897 #endif 898 } 899 900 Error 901 PlatformLinux::AttachNativeProcess (lldb::pid_t pid, 902 NativeProcessProtocol::NativeDelegate &native_delegate, 903 NativeProcessProtocolSP &process_sp) 904 { 905 #if !defined(__linux__) 906 return Error("Only implemented on Linux hosts"); 907 #else 908 if (!IsHost ()) 909 return Error("PlatformLinux::%s (): cannot attach to a debug process when not the host", __FUNCTION__); 910 911 // Launch it for debugging 912 return process_linux::NativeProcessLinux::AttachToProcess (pid, native_delegate, process_sp); 913 #endif 914 } 915