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