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 struct utsname un; 507 508 if (uname(&un)) 509 return; 510 511 strm.Printf (" Kernel: %s\n", un.sysname); 512 strm.Printf (" Release: %s\n", un.release); 513 strm.Printf (" Version: %s\n", un.version); 514 #endif 515 } 516 517 size_t 518 PlatformLinux::GetSoftwareBreakpointTrapOpcode (Target &target, 519 BreakpointSite *bp_site) 520 { 521 ArchSpec arch = target.GetArchitecture(); 522 const uint8_t *trap_opcode = NULL; 523 size_t trap_opcode_size = 0; 524 525 switch (arch.GetMachine()) 526 { 527 default: 528 assert(false && "CPU type not supported!"); 529 break; 530 531 case llvm::Triple::aarch64: 532 { 533 static const uint8_t g_aarch64_opcode[] = { 0x00, 0x00, 0x20, 0xd4 }; 534 trap_opcode = g_aarch64_opcode; 535 trap_opcode_size = sizeof(g_aarch64_opcode); 536 } 537 break; 538 case llvm::Triple::x86: 539 case llvm::Triple::x86_64: 540 { 541 static const uint8_t g_i386_breakpoint_opcode[] = { 0xCC }; 542 trap_opcode = g_i386_breakpoint_opcode; 543 trap_opcode_size = sizeof(g_i386_breakpoint_opcode); 544 } 545 break; 546 case llvm::Triple::hexagon: 547 { 548 static const uint8_t g_hex_opcode[] = { 0x0c, 0xdb, 0x00, 0x54 }; 549 trap_opcode = g_hex_opcode; 550 trap_opcode_size = sizeof(g_hex_opcode); 551 } 552 break; 553 case llvm::Triple::arm: 554 { 555 // The ARM reference recommends the use of 0xe7fddefe and 0xdefe 556 // but the linux kernel does otherwise. 557 static const uint8_t g_arm_breakpoint_opcode[] = { 0xf0, 0x01, 0xf0, 0xe7 }; 558 static const uint8_t g_thumb_breakpoint_opcode[] = { 0x01, 0xde }; 559 560 lldb::BreakpointLocationSP bp_loc_sp (bp_site->GetOwnerAtIndex (0)); 561 AddressClass addr_class = eAddressClassUnknown; 562 563 if (bp_loc_sp) 564 addr_class = bp_loc_sp->GetAddress ().GetAddressClass (); 565 566 if (addr_class == eAddressClassCodeAlternateISA 567 || (addr_class == eAddressClassUnknown 568 && bp_loc_sp->GetAddress().GetOffset() & 1)) 569 { 570 trap_opcode = g_thumb_breakpoint_opcode; 571 trap_opcode_size = sizeof(g_thumb_breakpoint_opcode); 572 } 573 else 574 { 575 trap_opcode = g_arm_breakpoint_opcode; 576 trap_opcode_size = sizeof(g_arm_breakpoint_opcode); 577 } 578 } 579 break; 580 } 581 582 if (bp_site->SetTrapOpcode(trap_opcode, trap_opcode_size)) 583 return trap_opcode_size; 584 return 0; 585 } 586 587 int32_t 588 PlatformLinux::GetResumeCountForLaunchInfo (ProcessLaunchInfo &launch_info) 589 { 590 int32_t resume_count = 0; 591 592 // Always resume past the initial stop when we use eLaunchFlagDebug 593 if (launch_info.GetFlags ().Test (eLaunchFlagDebug)) 594 { 595 // Resume past the stop for the final exec into the true inferior. 596 ++resume_count; 597 } 598 599 // If we're not launching a shell, we're done. 600 const FileSpec &shell = launch_info.GetShell(); 601 if (!shell) 602 return resume_count; 603 604 std::string shell_string = shell.GetPath(); 605 // We're in a shell, so for sure we have to resume past the shell exec. 606 ++resume_count; 607 608 // Figure out what shell we're planning on using. 609 const char *shell_name = strrchr (shell_string.c_str(), '/'); 610 if (shell_name == NULL) 611 shell_name = shell_string.c_str(); 612 else 613 shell_name++; 614 615 if (strcmp (shell_name, "csh") == 0 616 || strcmp (shell_name, "tcsh") == 0 617 || strcmp (shell_name, "zsh") == 0 618 || strcmp (shell_name, "sh") == 0) 619 { 620 // These shells seem to re-exec themselves. Add another resume. 621 ++resume_count; 622 } 623 624 return resume_count; 625 } 626 627 bool 628 PlatformLinux::UseLlgsForLocalDebugging () 629 { 630 PlatformLinuxPropertiesSP properties_sp = GetGlobalProperties (); 631 assert (properties_sp && "global properties shared pointer is null"); 632 return properties_sp ? properties_sp->GetUseLlgsForLocal () : false; 633 } 634 635 bool 636 PlatformLinux::CanDebugProcess () 637 { 638 if (IsHost ()) 639 { 640 // The platform only does local debugging (i.e. uses llgs) when the setting indicates we do that. 641 // Otherwise, we'll use ProcessLinux/ProcessPOSIX to handle with ProcessMonitor. 642 return UseLlgsForLocalDebugging (); 643 } 644 else 645 { 646 // If we're connected, we can debug. 647 return IsConnected (); 648 } 649 } 650 651 // For local debugging, Linux will override the debug logic to use llgs-launch rather than 652 // lldb-launch, llgs-attach. This differs from current lldb-launch, debugserver-attach 653 // approach on MacOSX. 654 lldb::ProcessSP 655 PlatformLinux::DebugProcess (ProcessLaunchInfo &launch_info, 656 Debugger &debugger, 657 Target *target, // Can be NULL, if NULL create a new target, else use existing one 658 Error &error) 659 { 660 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM)); 661 if (log) 662 log->Printf ("PlatformLinux::%s entered (target %p)", __FUNCTION__, static_cast<void*>(target)); 663 664 // If we're a remote host, use standard behavior from parent class. 665 if (!IsHost ()) 666 return PlatformPOSIX::DebugProcess (launch_info, debugger, target, error); 667 668 // 669 // For local debugging, we'll insist on having ProcessGDBRemote create the process. 670 // 671 672 ProcessSP process_sp; 673 674 // Ensure we're using llgs for local debugging. 675 if (!UseLlgsForLocalDebugging ()) 676 { 677 assert (false && "we're trying to debug a local process but platform.plugin.linux.use-llgs-for-local is false, should never get here"); 678 error.SetErrorString ("attempted to start gdb-remote-based debugging for local process but platform.plugin.linux.use-llgs-for-local is false"); 679 return process_sp; 680 } 681 682 // Make sure we stop at the entry point 683 launch_info.GetFlags ().Set (eLaunchFlagDebug); 684 685 // We always launch the process we are going to debug in a separate process 686 // group, since then we can handle ^C interrupts ourselves w/o having to worry 687 // about the target getting them as well. 688 launch_info.SetLaunchInSeparateProcessGroup(true); 689 690 // Ensure we have a target. 691 if (target == nullptr) 692 { 693 if (log) 694 log->Printf ("PlatformLinux::%s creating new target", __FUNCTION__); 695 696 TargetSP new_target_sp; 697 error = debugger.GetTargetList().CreateTarget (debugger, 698 nullptr, 699 nullptr, 700 false, 701 nullptr, 702 new_target_sp); 703 if (error.Fail ()) 704 { 705 if (log) 706 log->Printf ("PlatformLinux::%s failed to create new target: %s", __FUNCTION__, error.AsCString ()); 707 return process_sp; 708 } 709 710 target = new_target_sp.get(); 711 if (!target) 712 { 713 error.SetErrorString ("CreateTarget() returned nullptr"); 714 if (log) 715 log->Printf ("PlatformLinux::%s failed: %s", __FUNCTION__, error.AsCString ()); 716 return process_sp; 717 } 718 } 719 else 720 { 721 if (log) 722 log->Printf ("PlatformLinux::%s using provided target", __FUNCTION__); 723 } 724 725 // Mark target as currently selected target. 726 debugger.GetTargetList().SetSelectedTarget(target); 727 728 // Now create the gdb-remote process. 729 if (log) 730 log->Printf ("PlatformLinux::%s having target create process with gdb-remote plugin", __FUNCTION__); 731 process_sp = target->CreateProcess (launch_info.GetListenerForProcess(debugger), "gdb-remote", nullptr); 732 733 if (!process_sp) 734 { 735 error.SetErrorString ("CreateProcess() failed for gdb-remote process"); 736 if (log) 737 log->Printf ("PlatformLinux::%s failed: %s", __FUNCTION__, error.AsCString ()); 738 return process_sp; 739 } 740 else 741 { 742 if (log) 743 log->Printf ("PlatformLinux::%s successfully created process", __FUNCTION__); 744 } 745 746 // Set the unix signals properly. 747 process_sp->SetUnixSignals (Host::GetUnixSignals ()); 748 749 // Adjust launch for a hijacker. 750 ListenerSP listener_sp; 751 if (!launch_info.GetHijackListener ()) 752 { 753 if (log) 754 log->Printf ("PlatformLinux::%s setting up hijacker", __FUNCTION__); 755 756 listener_sp.reset (new Listener("lldb.PlatformLinux.DebugProcess.hijack")); 757 launch_info.SetHijackListener (listener_sp); 758 process_sp->HijackProcessEvents (listener_sp.get ()); 759 } 760 761 // Log file actions. 762 if (log) 763 { 764 log->Printf ("PlatformLinux::%s launching process with the following file actions:", __FUNCTION__); 765 766 StreamString stream; 767 size_t i = 0; 768 const FileAction *file_action; 769 while ((file_action = launch_info.GetFileActionAtIndex (i++)) != nullptr) 770 { 771 file_action->Dump (stream); 772 log->PutCString (stream.GetString().c_str ()); 773 stream.Clear(); 774 } 775 } 776 777 // Do the launch. 778 error = process_sp->Launch(launch_info); 779 if (error.Success ()) 780 { 781 // Handle the hijacking of process events. 782 if (listener_sp) 783 { 784 const StateType state = process_sp->WaitForProcessToStop (NULL, NULL, false, listener_sp.get()); 785 process_sp->RestoreProcessEvents(); 786 787 if (state == eStateStopped) 788 { 789 if (log) 790 log->Printf ("PlatformLinux::%s pid %" PRIu64 " state %s\n", 791 __FUNCTION__, process_sp->GetID (), StateAsCString (state)); 792 } 793 else 794 { 795 if (log) 796 log->Printf ("PlatformLinux::%s pid %" PRIu64 " state is not stopped - %s\n", 797 __FUNCTION__, process_sp->GetID (), StateAsCString (state)); 798 } 799 } 800 801 // Hook up process PTY if we have one (which we should for local debugging with llgs). 802 int pty_fd = launch_info.GetPTY().ReleaseMasterFileDescriptor(); 803 if (pty_fd != lldb_utility::PseudoTerminal::invalid_fd) 804 { 805 process_sp->SetSTDIOFileDescriptor(pty_fd); 806 if (log) 807 log->Printf ("PlatformLinux::%s pid %" PRIu64 " hooked up STDIO pty to process", __FUNCTION__, process_sp->GetID ()); 808 } 809 else 810 { 811 if (log) 812 log->Printf ("PlatformLinux::%s pid %" PRIu64 " not using process STDIO pty", __FUNCTION__, process_sp->GetID ()); 813 } 814 } 815 else 816 { 817 if (log) 818 log->Printf ("PlatformLinux::%s process launch failed: %s", __FUNCTION__, error.AsCString ()); 819 // FIXME figure out appropriate cleanup here. Do we delete the target? Do we delete the process? Does our caller do that? 820 } 821 822 return process_sp; 823 } 824 825 void 826 PlatformLinux::CalculateTrapHandlerSymbolNames () 827 { 828 m_trap_handlers.push_back (ConstString ("_sigtramp")); 829 } 830 831 Error 832 PlatformLinux::LaunchNativeProcess ( 833 ProcessLaunchInfo &launch_info, 834 lldb_private::NativeProcessProtocol::NativeDelegate &native_delegate, 835 NativeProcessProtocolSP &process_sp) 836 { 837 #if !defined(__linux__) || defined(__ANDROID_NDK__) 838 return Error("only implemented on Linux hosts"); 839 #else 840 if (!IsHost ()) 841 return Error("PlatformLinux::%s (): cannot launch a debug process when not the host", __FUNCTION__); 842 843 // Retrieve the exe module. 844 lldb::ModuleSP exe_module_sp; 845 ModuleSpec exe_module_spec(launch_info.GetExecutableFile(), launch_info.GetArchitecture()); 846 847 Error error = ResolveExecutable ( 848 exe_module_spec, 849 exe_module_sp, 850 NULL); 851 852 if (!error.Success ()) 853 return error; 854 855 if (!exe_module_sp) 856 return Error("exe_module_sp could not be resolved for %s", launch_info.GetExecutableFile ().GetPath ().c_str ()); 857 858 // Launch it for debugging 859 error = NativeProcessLinux::LaunchProcess ( 860 exe_module_sp.get (), 861 launch_info, 862 native_delegate, 863 process_sp); 864 865 return error; 866 #endif 867 } 868 869 Error 870 PlatformLinux::AttachNativeProcess (lldb::pid_t pid, 871 lldb_private::NativeProcessProtocol::NativeDelegate &native_delegate, 872 NativeProcessProtocolSP &process_sp) 873 { 874 #if !defined(__linux__) || defined(__ANDROID_NDK__) 875 return Error("only implemented on Linux hosts"); 876 #else 877 if (!IsHost ()) 878 return Error("PlatformLinux::%s (): cannot attach to a debug process when not the host", __FUNCTION__); 879 880 // Launch it for debugging 881 return NativeProcessLinux::AttachToProcess (pid, native_delegate, process_sp); 882 #endif 883 } 884