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