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/Core/Error.h" 25 #include "lldb/Core/Debugger.h" 26 #include "lldb/Core/Log.h" 27 #include "lldb/Core/Module.h" 28 #include "lldb/Core/ModuleList.h" 29 #include "lldb/Core/ModuleSpec.h" 30 #include "lldb/Core/PluginManager.h" 31 #include "lldb/Core/StreamString.h" 32 #include "lldb/Host/FileSpec.h" 33 #include "lldb/Host/HostInfo.h" 34 #include "lldb/Target/Target.h" 35 #include "lldb/Target/Process.h" 36 37 #if defined(__linux__) 38 #include "../../Process/Linux/NativeProcessLinux.h" 39 #endif 40 41 using namespace lldb; 42 using namespace lldb_private; 43 44 static uint32_t g_initialize_count = 0; 45 46 //------------------------------------------------------------------ 47 /// Code to handle the PlatformLinux settings 48 //------------------------------------------------------------------ 49 50 static PropertyDefinition 51 g_properties[] = 52 { 53 { "use-llgs-for-local" , OptionValue::eTypeBoolean, true, false, NULL, NULL, "Control whether the platform uses llgs for local debug sessions." }, 54 { NULL , OptionValue::eTypeInvalid, false, 0 , NULL, NULL, NULL } 55 }; 56 57 enum { 58 ePropertyUseLlgsForLocal = 0, 59 }; 60 61 62 63 class PlatformLinuxProperties : public Properties 64 { 65 public: 66 67 static ConstString & 68 GetSettingName () 69 { 70 static ConstString g_setting_name("linux"); 71 return g_setting_name; 72 } 73 74 PlatformLinuxProperties() : 75 Properties () 76 { 77 m_collection_sp.reset (new OptionValueProperties(GetSettingName())); 78 m_collection_sp->Initialize(g_properties); 79 } 80 81 virtual 82 ~PlatformLinuxProperties() 83 { 84 } 85 86 bool 87 GetUseLlgsForLocal() const 88 { 89 const uint32_t idx = ePropertyUseLlgsForLocal; 90 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0); 91 } 92 }; 93 94 typedef std::shared_ptr<PlatformLinuxProperties> PlatformLinuxPropertiesSP; 95 96 static const PlatformLinuxPropertiesSP & 97 GetGlobalProperties() 98 { 99 static PlatformLinuxPropertiesSP g_settings_sp; 100 if (!g_settings_sp) 101 g_settings_sp.reset (new PlatformLinuxProperties ()); 102 return g_settings_sp; 103 } 104 105 void 106 PlatformLinux::DebuggerInitialize (lldb_private::Debugger &debugger) 107 { 108 if (!PluginManager::GetSettingForPlatformPlugin (debugger, PlatformLinuxProperties::GetSettingName())) 109 { 110 const bool is_global_setting = true; 111 PluginManager::CreateSettingForPlatformPlugin (debugger, 112 GetGlobalProperties()->GetValueProperties(), 113 ConstString ("Properties for the PlatformLinux plug-in."), 114 is_global_setting); 115 } 116 } 117 118 119 //------------------------------------------------------------------ 120 121 Platform * 122 PlatformLinux::CreateInstance (bool force, const ArchSpec *arch) 123 { 124 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM)); 125 if (log) 126 { 127 const char *arch_name; 128 if (arch && arch->GetArchitectureName ()) 129 arch_name = arch->GetArchitectureName (); 130 else 131 arch_name = "<null>"; 132 133 const char *triple_cstr = arch ? arch->GetTriple ().getTriple ().c_str() : "<null>"; 134 135 log->Printf ("PlatformLinux::%s(force=%s, arch={%s,%s})", __FUNCTION__, force ? "true" : "false", arch_name, triple_cstr); 136 } 137 138 bool create = force; 139 if (create == false && arch && arch->IsValid()) 140 { 141 const llvm::Triple &triple = arch->GetTriple(); 142 switch (triple.getVendor()) 143 { 144 case llvm::Triple::PC: 145 create = true; 146 break; 147 148 #if defined(__linux__) 149 // Only accept "unknown" for the vendor if the host is linux and 150 // it "unknown" wasn't specified (it was just returned because it 151 // was NOT specified_ 152 case llvm::Triple::VendorType::UnknownVendor: 153 create = !arch->TripleVendorWasSpecified(); 154 break; 155 #endif 156 default: 157 break; 158 } 159 160 if (create) 161 { 162 switch (triple.getOS()) 163 { 164 case llvm::Triple::Linux: 165 break; 166 167 #if defined(__linux__) 168 // Only accept "unknown" for the OS if the host is linux and 169 // it "unknown" wasn't specified (it was just returned because it 170 // was NOT specified) 171 case llvm::Triple::OSType::UnknownOS: 172 create = !arch->TripleOSWasSpecified(); 173 break; 174 #endif 175 default: 176 create = false; 177 break; 178 } 179 } 180 } 181 182 if (create) 183 { 184 if (log) 185 log->Printf ("PlatformLinux::%s() creating remote-linux platform", __FUNCTION__); 186 return new PlatformLinux(false); 187 } 188 189 if (log) 190 log->Printf ("PlatformLinux::%s() aborting creation of remote-linux platform", __FUNCTION__); 191 192 return NULL; 193 } 194 195 196 lldb_private::ConstString 197 PlatformLinux::GetPluginNameStatic (bool is_host) 198 { 199 if (is_host) 200 { 201 static ConstString g_host_name(Platform::GetHostPlatformName ()); 202 return g_host_name; 203 } 204 else 205 { 206 static ConstString g_remote_name("remote-linux"); 207 return g_remote_name; 208 } 209 } 210 211 const char * 212 PlatformLinux::GetPluginDescriptionStatic (bool is_host) 213 { 214 if (is_host) 215 return "Local Linux user platform plug-in."; 216 else 217 return "Remote Linux user platform plug-in."; 218 } 219 220 lldb_private::ConstString 221 PlatformLinux::GetPluginName() 222 { 223 return GetPluginNameStatic(IsHost()); 224 } 225 226 void 227 PlatformLinux::Initialize () 228 { 229 if (g_initialize_count++ == 0) 230 { 231 #if defined(__linux__) 232 PlatformSP default_platform_sp (new PlatformLinux(true)); 233 default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture()); 234 Platform::SetDefaultPlatform (default_platform_sp); 235 #endif 236 PluginManager::RegisterPlugin(PlatformLinux::GetPluginNameStatic(false), 237 PlatformLinux::GetPluginDescriptionStatic(false), 238 PlatformLinux::CreateInstance, 239 PlatformLinux::DebuggerInitialize); 240 } 241 } 242 243 void 244 PlatformLinux::Terminate () 245 { 246 if (g_initialize_count > 0) 247 { 248 if (--g_initialize_count == 0) 249 { 250 PluginManager::UnregisterPlugin (PlatformLinux::CreateInstance); 251 } 252 } 253 } 254 255 Error 256 PlatformLinux::ResolveExecutable (const FileSpec &exe_file, 257 const ArchSpec &exe_arch, 258 lldb::ModuleSP &exe_module_sp, 259 const FileSpecList *module_search_paths_ptr) 260 { 261 Error error; 262 // Nothing special to do here, just use the actual file and architecture 263 264 char exe_path[PATH_MAX]; 265 FileSpec resolved_exe_file (exe_file); 266 267 if (IsHost()) 268 { 269 // If we have "ls" as the exe_file, resolve the executable location based on 270 // the current path variables 271 if (!resolved_exe_file.Exists()) 272 { 273 exe_file.GetPath(exe_path, sizeof(exe_path)); 274 resolved_exe_file.SetFile(exe_path, true); 275 } 276 277 if (!resolved_exe_file.Exists()) 278 resolved_exe_file.ResolveExecutableLocation (); 279 280 if (resolved_exe_file.Exists()) 281 error.Clear(); 282 else 283 { 284 exe_file.GetPath(exe_path, sizeof(exe_path)); 285 error.SetErrorStringWithFormat("unable to find executable for '%s'", exe_path); 286 } 287 } 288 else 289 { 290 if (m_remote_platform_sp) 291 { 292 error = m_remote_platform_sp->ResolveExecutable (exe_file, 293 exe_arch, 294 exe_module_sp, 295 NULL); 296 } 297 else 298 { 299 // We may connect to a process and use the provided executable (Don't use local $PATH). 300 301 if (resolved_exe_file.Exists()) 302 error.Clear(); 303 else 304 error.SetErrorStringWithFormat("the platform is not currently connected, and '%s' doesn't exist in the system root.", exe_path); 305 } 306 } 307 308 if (error.Success()) 309 { 310 ModuleSpec module_spec (resolved_exe_file, exe_arch); 311 if (exe_arch.IsValid()) 312 { 313 error = ModuleList::GetSharedModule (module_spec, 314 exe_module_sp, 315 NULL, 316 NULL, 317 NULL); 318 if (error.Fail()) 319 { 320 // If we failed, it may be because the vendor and os aren't known. If that is the 321 // case, try setting them to the host architecture and give it another try. 322 llvm::Triple &module_triple = module_spec.GetArchitecture().GetTriple(); 323 bool is_vendor_specified = (module_triple.getVendor() != llvm::Triple::UnknownVendor); 324 bool is_os_specified = (module_triple.getOS() != llvm::Triple::UnknownOS); 325 if (!is_vendor_specified || !is_os_specified) 326 { 327 const llvm::Triple &host_triple = HostInfo::GetArchitecture(HostInfo::eArchKindDefault).GetTriple(); 328 329 if (!is_vendor_specified) 330 module_triple.setVendorName (host_triple.getVendorName()); 331 if (!is_os_specified) 332 module_triple.setOSName (host_triple.getOSName()); 333 334 error = ModuleList::GetSharedModule (module_spec, 335 exe_module_sp, 336 NULL, 337 NULL, 338 NULL); 339 } 340 } 341 342 // TODO find out why exe_module_sp might be NULL 343 if (!exe_module_sp || exe_module_sp->GetObjectFile() == NULL) 344 { 345 exe_module_sp.reset(); 346 error.SetErrorStringWithFormat ("'%s' doesn't contain the architecture %s", 347 exe_file.GetPath().c_str(), 348 exe_arch.GetArchitectureName()); 349 } 350 } 351 else 352 { 353 // No valid architecture was specified, ask the platform for 354 // the architectures that we should be using (in the correct order) 355 // and see if we can find a match that way 356 StreamString arch_names; 357 for (uint32_t idx = 0; GetSupportedArchitectureAtIndex (idx, module_spec.GetArchitecture()); ++idx) 358 { 359 error = ModuleList::GetSharedModule (module_spec, 360 exe_module_sp, 361 NULL, 362 NULL, 363 NULL); 364 // Did we find an executable using one of the 365 if (error.Success()) 366 { 367 if (exe_module_sp && exe_module_sp->GetObjectFile()) 368 break; 369 else 370 error.SetErrorToGenericError(); 371 } 372 373 if (idx > 0) 374 arch_names.PutCString (", "); 375 arch_names.PutCString (module_spec.GetArchitecture().GetArchitectureName()); 376 } 377 378 if (error.Fail() || !exe_module_sp) 379 { 380 if (exe_file.Readable()) 381 { 382 error.SetErrorStringWithFormat ("'%s' doesn't contain any '%s' platform architectures: %s", 383 exe_file.GetPath().c_str(), 384 GetPluginName().GetCString(), 385 arch_names.GetString().c_str()); 386 } 387 else 388 { 389 error.SetErrorStringWithFormat("'%s' is not readable", exe_file.GetPath().c_str()); 390 } 391 } 392 } 393 } 394 395 return error; 396 } 397 398 Error 399 PlatformLinux::GetFileWithUUID (const FileSpec &platform_file, 400 const UUID *uuid_ptr, FileSpec &local_file) 401 { 402 if (IsRemote()) 403 { 404 if (m_remote_platform_sp) 405 return m_remote_platform_sp->GetFileWithUUID (platform_file, uuid_ptr, local_file); 406 } 407 408 // Default to the local case 409 local_file = platform_file; 410 return Error(); 411 } 412 413 414 //------------------------------------------------------------------ 415 /// Default Constructor 416 //------------------------------------------------------------------ 417 PlatformLinux::PlatformLinux (bool is_host) : 418 PlatformPOSIX(is_host) // This is the local host platform 419 { 420 } 421 422 //------------------------------------------------------------------ 423 /// Destructor. 424 /// 425 /// The destructor is virtual since this class is designed to be 426 /// inherited from by the plug-in instance. 427 //------------------------------------------------------------------ 428 PlatformLinux::~PlatformLinux() 429 { 430 } 431 432 bool 433 PlatformLinux::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info) 434 { 435 bool success = false; 436 if (IsHost()) 437 { 438 success = Platform::GetProcessInfo (pid, process_info); 439 } 440 else 441 { 442 if (m_remote_platform_sp) 443 success = m_remote_platform_sp->GetProcessInfo (pid, process_info); 444 } 445 return success; 446 } 447 448 bool 449 PlatformLinux::GetSupportedArchitectureAtIndex (uint32_t idx, ArchSpec &arch) 450 { 451 if (idx == 0) 452 { 453 arch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault); 454 return arch.IsValid(); 455 } 456 else if (idx == 1) 457 { 458 // If the default host architecture is 64-bit, look for a 32-bit variant 459 ArchSpec hostArch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault); 460 if (hostArch.IsValid() && hostArch.GetTriple().isArch64Bit()) 461 { 462 arch = HostInfo::GetArchitecture(HostInfo::eArchKind32); 463 return arch.IsValid(); 464 } 465 } 466 return false; 467 } 468 469 void 470 PlatformLinux::GetStatus (Stream &strm) 471 { 472 Platform::GetStatus(strm); 473 474 #ifndef LLDB_DISABLE_POSIX 475 struct utsname un; 476 477 if (uname(&un)) 478 return; 479 480 strm.Printf (" Kernel: %s\n", un.sysname); 481 strm.Printf (" Release: %s\n", un.release); 482 strm.Printf (" Version: %s\n", un.version); 483 #endif 484 } 485 486 size_t 487 PlatformLinux::GetSoftwareBreakpointTrapOpcode (Target &target, 488 BreakpointSite *bp_site) 489 { 490 ArchSpec arch = target.GetArchitecture(); 491 const uint8_t *trap_opcode = NULL; 492 size_t trap_opcode_size = 0; 493 494 switch (arch.GetMachine()) 495 { 496 default: 497 assert(false && "CPU type not supported!"); 498 break; 499 500 case llvm::Triple::aarch64: 501 { 502 static const uint8_t g_aarch64_opcode[] = { 0x00, 0x00, 0x20, 0xd4 }; 503 trap_opcode = g_aarch64_opcode; 504 trap_opcode_size = sizeof(g_aarch64_opcode); 505 } 506 break; 507 case llvm::Triple::x86: 508 case llvm::Triple::x86_64: 509 { 510 static const uint8_t g_i386_breakpoint_opcode[] = { 0xCC }; 511 trap_opcode = g_i386_breakpoint_opcode; 512 trap_opcode_size = sizeof(g_i386_breakpoint_opcode); 513 } 514 break; 515 case llvm::Triple::hexagon: 516 { 517 static const uint8_t g_hex_opcode[] = { 0x0c, 0xdb, 0x00, 0x54 }; 518 trap_opcode = g_hex_opcode; 519 trap_opcode_size = sizeof(g_hex_opcode); 520 } 521 break; 522 } 523 524 if (bp_site->SetTrapOpcode(trap_opcode, trap_opcode_size)) 525 return trap_opcode_size; 526 return 0; 527 } 528 529 Error 530 PlatformLinux::LaunchProcess (ProcessLaunchInfo &launch_info) 531 { 532 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM)); 533 Error error; 534 535 if (IsHost()) 536 { 537 if (log) 538 log->Printf ("PlatformLinux::%s() launching process as host", __FUNCTION__); 539 540 if (launch_info.GetFlags().Test (eLaunchFlagLaunchInShell)) 541 { 542 const bool is_localhost = true; 543 const bool will_debug = launch_info.GetFlags().Test(eLaunchFlagDebug); 544 const bool first_arg_is_full_shell_command = false; 545 uint32_t num_resumes = GetResumeCountForLaunchInfo (launch_info); 546 if (!launch_info.ConvertArgumentsForLaunchingInShell (error, 547 is_localhost, 548 will_debug, 549 first_arg_is_full_shell_command, 550 num_resumes)) 551 return error; 552 } 553 error = Platform::LaunchProcess (launch_info); 554 } 555 else 556 { 557 if (m_remote_platform_sp) 558 { 559 if (log) 560 log->Printf ("PlatformLinux::%s() attempting to launch remote process", __FUNCTION__); 561 error = m_remote_platform_sp->LaunchProcess (launch_info); 562 } 563 else 564 { 565 if (log) 566 log->Printf ("PlatformLinux::%s() attempted to launch process but is not the host and no remote platform set", __FUNCTION__); 567 error.SetErrorString ("the platform is not currently connected"); 568 } 569 } 570 return error; 571 } 572 573 // Linux processes can not be launched by spawning and attaching. 574 bool 575 PlatformLinux::CanDebugProcess () 576 { 577 // If we're the host, launch via normal host setup. 578 if (IsHost ()) 579 return false; 580 581 // If we're connected, we can debug. 582 return IsConnected (); 583 } 584 585 lldb::ProcessSP 586 PlatformLinux::Attach(ProcessAttachInfo &attach_info, 587 Debugger &debugger, 588 Target *target, 589 Listener &listener, 590 Error &error) 591 { 592 lldb::ProcessSP process_sp; 593 if (IsHost()) 594 { 595 if (target == NULL) 596 { 597 TargetSP new_target_sp; 598 ArchSpec emptyArchSpec; 599 600 error = debugger.GetTargetList().CreateTarget (debugger, 601 NULL, 602 emptyArchSpec, 603 false, 604 m_remote_platform_sp, 605 new_target_sp); 606 target = new_target_sp.get(); 607 } 608 else 609 error.Clear(); 610 611 if (target && error.Success()) 612 { 613 debugger.GetTargetList().SetSelectedTarget(target); 614 615 process_sp = target->CreateProcess (listener, 616 attach_info.GetProcessPluginName(), 617 NULL); 618 619 if (process_sp) 620 error = process_sp->Attach (attach_info); 621 } 622 } 623 else 624 { 625 if (m_remote_platform_sp) 626 process_sp = m_remote_platform_sp->Attach (attach_info, debugger, target, listener, error); 627 else 628 error.SetErrorString ("the platform is not currently connected"); 629 } 630 return process_sp; 631 } 632 633 void 634 PlatformLinux::CalculateTrapHandlerSymbolNames () 635 { 636 m_trap_handlers.push_back (ConstString ("_sigtramp")); 637 } 638 639 Error 640 PlatformLinux::LaunchNativeProcess ( 641 ProcessLaunchInfo &launch_info, 642 lldb_private::NativeProcessProtocol::NativeDelegate &native_delegate, 643 NativeProcessProtocolSP &process_sp) 644 { 645 #if !defined(__linux__) 646 return Error("only implemented on Linux hosts"); 647 #else 648 if (!IsHost ()) 649 return Error("PlatformLinux::%s (): cannot launch a debug process when not the host", __FUNCTION__); 650 651 // Retrieve the exe module. 652 lldb::ModuleSP exe_module_sp; 653 654 Error error = ResolveExecutable ( 655 launch_info.GetExecutableFile (), 656 launch_info.GetArchitecture (), 657 exe_module_sp, 658 NULL); 659 660 if (!error.Success ()) 661 return error; 662 663 if (!exe_module_sp) 664 return Error("exe_module_sp could not be resolved for %s", launch_info.GetExecutableFile ().GetPath ().c_str ()); 665 666 // Launch it for debugging 667 error = NativeProcessLinux::LaunchProcess ( 668 exe_module_sp.get (), 669 launch_info, 670 native_delegate, 671 process_sp); 672 673 return error; 674 #endif 675 } 676 677 Error 678 PlatformLinux::AttachNativeProcess (lldb::pid_t pid, 679 lldb_private::NativeProcessProtocol::NativeDelegate &native_delegate, 680 NativeProcessProtocolSP &process_sp) 681 { 682 #if !defined(__linux__) 683 return Error("only implemented on Linux hosts"); 684 #else 685 if (!IsHost ()) 686 return Error("PlatformLinux::%s (): cannot attach to a debug process when not the host", __FUNCTION__); 687 688 // Launch it for debugging 689 return NativeProcessLinux::AttachToProcess (pid, native_delegate, process_sp); 690 #endif 691 } 692