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