1 //===-- PlatformRemoteGDBServer.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 "PlatformRemoteGDBServer.h" 13 #include "lldb/Host/Config.h" 14 15 // C Includes 16 #ifndef LLDB_DISABLE_POSIX 17 #include <sys/sysctl.h> 18 #endif 19 20 // C++ Includes 21 // Other libraries and framework includes 22 // Project includes 23 #include "lldb/Breakpoint/BreakpointLocation.h" 24 #include "lldb/Core/ConnectionFileDescriptor.h" 25 #include "lldb/Core/Debugger.h" 26 #include "lldb/Core/Error.h" 27 #include "lldb/Core/Module.h" 28 #include "lldb/Core/ModuleList.h" 29 #include "lldb/Core/PluginManager.h" 30 #include "lldb/Core/StreamString.h" 31 #include "lldb/Host/FileSpec.h" 32 #include "lldb/Host/Host.h" 33 #include "lldb/Target/Process.h" 34 #include "lldb/Target/Target.h" 35 36 using namespace lldb; 37 using namespace lldb_private; 38 39 static bool g_initialized = false; 40 41 void 42 PlatformRemoteGDBServer::Initialize () 43 { 44 if (g_initialized == false) 45 { 46 g_initialized = true; 47 PluginManager::RegisterPlugin (PlatformRemoteGDBServer::GetPluginNameStatic(), 48 PlatformRemoteGDBServer::GetDescriptionStatic(), 49 PlatformRemoteGDBServer::CreateInstance); 50 } 51 } 52 53 void 54 PlatformRemoteGDBServer::Terminate () 55 { 56 if (g_initialized) 57 { 58 g_initialized = false; 59 PluginManager::UnregisterPlugin (PlatformRemoteGDBServer::CreateInstance); 60 } 61 } 62 63 Platform* 64 PlatformRemoteGDBServer::CreateInstance (bool force, const lldb_private::ArchSpec *arch) 65 { 66 bool create = force; 67 if (!create) 68 { 69 create = !arch->TripleVendorWasSpecified() && !arch->TripleOSWasSpecified(); 70 } 71 if (create) 72 return new PlatformRemoteGDBServer (); 73 return NULL; 74 } 75 76 77 lldb_private::ConstString 78 PlatformRemoteGDBServer::GetPluginNameStatic() 79 { 80 static ConstString g_name("remote-gdb-server"); 81 return g_name; 82 } 83 84 const char * 85 PlatformRemoteGDBServer::GetDescriptionStatic() 86 { 87 return "A platform that uses the GDB remote protocol as the communication transport."; 88 } 89 90 const char * 91 PlatformRemoteGDBServer::GetDescription () 92 { 93 if (m_platform_description.empty()) 94 { 95 if (IsConnected()) 96 { 97 // Send the get description packet 98 } 99 } 100 101 if (!m_platform_description.empty()) 102 return m_platform_description.c_str(); 103 return GetDescriptionStatic(); 104 } 105 106 Error 107 PlatformRemoteGDBServer::ResolveExecutable (const FileSpec &exe_file, 108 const ArchSpec &exe_arch, 109 lldb::ModuleSP &exe_module_sp, 110 const FileSpecList *module_search_paths_ptr) 111 { 112 Error error; 113 //error.SetErrorString ("PlatformRemoteGDBServer::ResolveExecutable() is unimplemented"); 114 if (m_gdb_client.GetFileExists(exe_file)) 115 return error; 116 // TODO: get the remote end to somehow resolve this file 117 error.SetErrorString("file not found on remote end"); 118 return error; 119 } 120 121 Error 122 PlatformRemoteGDBServer::GetFile (const FileSpec &platform_file, 123 const UUID *uuid_ptr, 124 FileSpec &local_file) 125 { 126 // Default to the local case 127 local_file = platform_file; 128 return Error(); 129 } 130 131 //------------------------------------------------------------------ 132 /// Default Constructor 133 //------------------------------------------------------------------ 134 PlatformRemoteGDBServer::PlatformRemoteGDBServer () : 135 Platform(false), // This is a remote platform 136 m_gdb_client(true) 137 { 138 } 139 140 //------------------------------------------------------------------ 141 /// Destructor. 142 /// 143 /// The destructor is virtual since this class is designed to be 144 /// inherited from by the plug-in instance. 145 //------------------------------------------------------------------ 146 PlatformRemoteGDBServer::~PlatformRemoteGDBServer() 147 { 148 } 149 150 bool 151 PlatformRemoteGDBServer::GetSupportedArchitectureAtIndex (uint32_t idx, ArchSpec &arch) 152 { 153 return false; 154 } 155 156 size_t 157 PlatformRemoteGDBServer::GetSoftwareBreakpointTrapOpcode (Target &target, BreakpointSite *bp_site) 158 { 159 // This isn't needed if the z/Z packets are supported in the GDB remote 160 // server. But we might need a packet to detect this. 161 return 0; 162 } 163 164 bool 165 PlatformRemoteGDBServer::GetRemoteOSVersion () 166 { 167 uint32_t major, minor, update; 168 if (m_gdb_client.GetOSVersion (major, minor, update)) 169 { 170 m_major_os_version = major; 171 m_minor_os_version = minor; 172 m_update_os_version = update; 173 return true; 174 } 175 return false; 176 } 177 178 bool 179 PlatformRemoteGDBServer::GetRemoteOSBuildString (std::string &s) 180 { 181 return m_gdb_client.GetOSBuildString (s); 182 } 183 184 bool 185 PlatformRemoteGDBServer::GetRemoteOSKernelDescription (std::string &s) 186 { 187 return m_gdb_client.GetOSKernelDescription (s); 188 } 189 190 // Remote Platform subclasses need to override this function 191 ArchSpec 192 PlatformRemoteGDBServer::GetRemoteSystemArchitecture () 193 { 194 return m_gdb_client.GetSystemArchitecture(); 195 } 196 197 lldb_private::ConstString 198 PlatformRemoteGDBServer::GetRemoteWorkingDirectory() 199 { 200 if (IsConnected()) 201 { 202 if (!m_working_dir) 203 { 204 std::string cwd; 205 if (m_gdb_client.GetWorkingDir(cwd)) 206 m_working_dir = ConstString(cwd.c_str()); 207 } 208 return m_working_dir; 209 } 210 else 211 { 212 return Platform::GetRemoteWorkingDirectory(); 213 } 214 } 215 216 bool 217 PlatformRemoteGDBServer::SetRemoteWorkingDirectory(const lldb_private::ConstString &path) 218 { 219 if (IsConnected()) 220 { 221 // Clear the working directory it case it doesn't get set correctly. This will 222 // for use to re-read it 223 m_working_dir.Clear(); 224 return m_gdb_client.SetWorkingDir(path.GetCString()) == 0; 225 } 226 else 227 return Platform::SetRemoteWorkingDirectory(path); 228 } 229 230 bool 231 PlatformRemoteGDBServer::IsConnected () const 232 { 233 return m_gdb_client.IsConnected(); 234 } 235 236 Error 237 PlatformRemoteGDBServer::ConnectRemote (Args& args) 238 { 239 Error error; 240 if (IsConnected()) 241 { 242 error.SetErrorStringWithFormat ("the platform is already connected to '%s', execute 'platform disconnect' to close the current connection", 243 GetHostname()); 244 } 245 else 246 { 247 if (args.GetArgumentCount() == 1) 248 { 249 const char *url = args.GetArgumentAtIndex(0); 250 m_gdb_client.SetConnection (new ConnectionFileDescriptor()); 251 const ConnectionStatus status = m_gdb_client.Connect(url, &error); 252 if (status == eConnectionStatusSuccess) 253 { 254 if (m_gdb_client.HandshakeWithServer(&error)) 255 { 256 m_gdb_client.QueryNoAckModeSupported(); 257 m_gdb_client.GetHostInfo(); 258 // If a working directory was set prior to connecting, send it down now 259 if (m_working_dir) 260 m_gdb_client.SetWorkingDir(m_working_dir.GetCString()); 261 #if 0 262 m_gdb_client.TestPacketSpeed(10000); 263 #endif 264 } 265 else 266 { 267 m_gdb_client.Disconnect(); 268 } 269 } 270 } 271 else 272 { 273 error.SetErrorString ("\"platform connect\" takes a single argument: <connect-url>"); 274 } 275 } 276 277 if (error.Success()) 278 { 279 280 } 281 282 return error; 283 } 284 285 Error 286 PlatformRemoteGDBServer::DisconnectRemote () 287 { 288 Error error; 289 m_gdb_client.Disconnect(&error); 290 return error; 291 } 292 293 const char * 294 PlatformRemoteGDBServer::GetHostname () 295 { 296 m_gdb_client.GetHostname (m_name); 297 if (m_name.empty()) 298 return NULL; 299 return m_name.c_str(); 300 } 301 302 const char * 303 PlatformRemoteGDBServer::GetUserName (uint32_t uid) 304 { 305 // Try and get a cache user name first 306 const char *cached_user_name = Platform::GetUserName(uid); 307 if (cached_user_name) 308 return cached_user_name; 309 std::string name; 310 if (m_gdb_client.GetUserName(uid, name)) 311 return SetCachedUserName(uid, name.c_str(), name.size()); 312 313 SetUserNameNotFound(uid); // Negative cache so we don't keep sending packets 314 return NULL; 315 } 316 317 const char * 318 PlatformRemoteGDBServer::GetGroupName (uint32_t gid) 319 { 320 const char *cached_group_name = Platform::GetGroupName(gid); 321 if (cached_group_name) 322 return cached_group_name; 323 std::string name; 324 if (m_gdb_client.GetGroupName(gid, name)) 325 return SetCachedGroupName(gid, name.c_str(), name.size()); 326 327 SetGroupNameNotFound(gid); // Negative cache so we don't keep sending packets 328 return NULL; 329 } 330 331 uint32_t 332 PlatformRemoteGDBServer::FindProcesses (const ProcessInstanceInfoMatch &match_info, 333 ProcessInstanceInfoList &process_infos) 334 { 335 return m_gdb_client.FindProcesses (match_info, process_infos); 336 } 337 338 bool 339 PlatformRemoteGDBServer::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info) 340 { 341 return m_gdb_client.GetProcessInfo (pid, process_info); 342 } 343 344 345 Error 346 PlatformRemoteGDBServer::LaunchProcess (ProcessLaunchInfo &launch_info) 347 { 348 Error error; 349 lldb::pid_t pid = LLDB_INVALID_PROCESS_ID; 350 351 m_gdb_client.SetSTDIN ("/dev/null"); 352 m_gdb_client.SetSTDOUT ("/dev/null"); 353 m_gdb_client.SetSTDERR ("/dev/null"); 354 m_gdb_client.SetDisableASLR (launch_info.GetFlags().Test (eLaunchFlagDisableASLR)); 355 356 const char *working_dir = launch_info.GetWorkingDirectory(); 357 if (working_dir && working_dir[0]) 358 { 359 m_gdb_client.SetWorkingDir (working_dir); 360 } 361 362 // Send the environment and the program + arguments after we connect 363 const char **envp = launch_info.GetEnvironmentEntries().GetConstArgumentVector(); 364 365 if (envp) 366 { 367 const char *env_entry; 368 for (int i=0; (env_entry = envp[i]); ++i) 369 { 370 if (m_gdb_client.SendEnvironmentPacket(env_entry) != 0) 371 break; 372 } 373 } 374 375 ArchSpec arch_spec = launch_info.GetArchitecture(); 376 const char *arch_triple = arch_spec.GetTriple().str().c_str(); 377 378 m_gdb_client.SendLaunchArchPacket(arch_triple); 379 380 const uint32_t old_packet_timeout = m_gdb_client.SetPacketTimeout (5); 381 int arg_packet_err = m_gdb_client.SendArgumentsPacket (launch_info); 382 m_gdb_client.SetPacketTimeout (old_packet_timeout); 383 if (arg_packet_err == 0) 384 { 385 std::string error_str; 386 if (m_gdb_client.GetLaunchSuccess (error_str)) 387 { 388 pid = m_gdb_client.GetCurrentProcessID (); 389 if (pid != LLDB_INVALID_PROCESS_ID) 390 launch_info.SetProcessID (pid); 391 } 392 else 393 { 394 error.SetErrorString (error_str.c_str()); 395 } 396 } 397 else 398 { 399 error.SetErrorStringWithFormat("'A' packet returned an error: %i", arg_packet_err); 400 } 401 return error; 402 } 403 404 lldb::ProcessSP 405 PlatformRemoteGDBServer::DebugProcess (lldb_private::ProcessLaunchInfo &launch_info, 406 lldb_private::Debugger &debugger, 407 lldb_private::Target *target, // Can be NULL, if NULL create a new target, else use existing one 408 lldb_private::Listener &listener, 409 lldb_private::Error &error) 410 { 411 lldb::ProcessSP process_sp; 412 if (IsRemote()) 413 { 414 if (IsConnected()) 415 { 416 lldb::pid_t debugserver_pid = LLDB_INVALID_PROCESS_ID; 417 uint16_t port = m_gdb_client.LaunchGDBserverAndGetPort(debugserver_pid); 418 419 if (port == 0) 420 { 421 error.SetErrorStringWithFormat ("unable to launch a GDB server on '%s'", GetHostname ()); 422 } 423 else 424 { 425 if (target == NULL) 426 { 427 TargetSP new_target_sp; 428 429 error = debugger.GetTargetList().CreateTarget (debugger, 430 NULL, 431 NULL, 432 false, 433 NULL, 434 new_target_sp); 435 target = new_target_sp.get(); 436 } 437 else 438 error.Clear(); 439 440 if (target && error.Success()) 441 { 442 debugger.GetTargetList().SetSelectedTarget(target); 443 444 // The darwin always currently uses the GDB remote debugger plug-in 445 // so even when debugging locally we are debugging remotely! 446 process_sp = target->CreateProcess (listener, "gdb-remote", NULL); 447 448 if (process_sp) 449 { 450 char connect_url[256]; 451 const char *override_hostname = getenv("LLDB_PLATFORM_REMOTE_GDB_SERVER_HOSTNAME"); 452 const char *port_offset_c_str = getenv("LLDB_PLATFORM_REMOTE_GDB_SERVER_PORT_OFFSET"); 453 int port_offset = port_offset_c_str ? ::atoi(port_offset_c_str) : 0; 454 const int connect_url_len = ::snprintf (connect_url, 455 sizeof(connect_url), 456 "connect://%s:%u", 457 override_hostname ? override_hostname : GetHostname (), 458 port + port_offset); 459 assert (connect_url_len < (int)sizeof(connect_url)); 460 error = process_sp->ConnectRemote (NULL, connect_url); 461 if (error.Success()) 462 error = process_sp->Launch(launch_info); 463 else if (debugserver_pid != LLDB_INVALID_PROCESS_ID) 464 m_gdb_client.KillSpawnedProcess(debugserver_pid); 465 } 466 } 467 } 468 } 469 else 470 { 471 error.SetErrorString("not connected to remote gdb server"); 472 } 473 } 474 return process_sp; 475 476 } 477 478 lldb::ProcessSP 479 PlatformRemoteGDBServer::Attach (lldb_private::ProcessAttachInfo &attach_info, 480 Debugger &debugger, 481 Target *target, // Can be NULL, if NULL create a new target, else use existing one 482 Listener &listener, 483 Error &error) 484 { 485 lldb::ProcessSP process_sp; 486 if (IsRemote()) 487 { 488 if (IsConnected()) 489 { 490 lldb::pid_t debugserver_pid = LLDB_INVALID_PROCESS_ID; 491 uint16_t port = m_gdb_client.LaunchGDBserverAndGetPort(debugserver_pid); 492 493 if (port == 0) 494 { 495 error.SetErrorStringWithFormat ("unable to launch a GDB server on '%s'", GetHostname ()); 496 } 497 else 498 { 499 if (target == NULL) 500 { 501 TargetSP new_target_sp; 502 503 error = debugger.GetTargetList().CreateTarget (debugger, 504 NULL, 505 NULL, 506 false, 507 NULL, 508 new_target_sp); 509 target = new_target_sp.get(); 510 } 511 else 512 error.Clear(); 513 514 if (target && error.Success()) 515 { 516 debugger.GetTargetList().SetSelectedTarget(target); 517 518 // The darwin always currently uses the GDB remote debugger plug-in 519 // so even when debugging locally we are debugging remotely! 520 process_sp = target->CreateProcess (listener, "gdb-remote", NULL); 521 522 if (process_sp) 523 { 524 char connect_url[256]; 525 const char *override_hostname = getenv("LLDB_PLATFORM_REMOTE_GDB_SERVER_HOSTNAME"); 526 const char *port_offset_c_str = getenv("LLDB_PLATFORM_REMOTE_GDB_SERVER_PORT_OFFSET"); 527 int port_offset = port_offset_c_str ? ::atoi(port_offset_c_str) : 0; 528 const int connect_url_len = ::snprintf (connect_url, 529 sizeof(connect_url), 530 "connect://%s:%u", 531 override_hostname ? override_hostname : GetHostname (), 532 port + port_offset); 533 assert (connect_url_len < (int)sizeof(connect_url)); 534 error = process_sp->ConnectRemote (NULL, connect_url); 535 if (error.Success()) 536 error = process_sp->Attach(attach_info); 537 else if (debugserver_pid != LLDB_INVALID_PROCESS_ID) 538 { 539 m_gdb_client.KillSpawnedProcess(debugserver_pid); 540 } 541 } 542 } 543 } 544 } 545 else 546 { 547 error.SetErrorString("not connected to remote gdb server"); 548 } 549 } 550 return process_sp; 551 } 552 553 Error 554 PlatformRemoteGDBServer::MakeDirectory (const char *path, uint32_t mode) 555 { 556 Error error = m_gdb_client.MakeDirectory(path,mode); 557 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM); 558 if (log) 559 log->Printf ("PlatformRemoteGDBServer::MakeDirectory(path='%s', mode=%o) error = %u (%s)", path, mode, error.GetError(), error.AsCString()); 560 return error; 561 } 562 563 564 Error 565 PlatformRemoteGDBServer::GetFilePermissions (const char *path, uint32_t &file_permissions) 566 { 567 Error error = m_gdb_client.GetFilePermissions(path, file_permissions); 568 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM); 569 if (log) 570 log->Printf ("PlatformRemoteGDBServer::GetFilePermissions(path='%s', file_permissions=%o) error = %u (%s)", path, file_permissions, error.GetError(), error.AsCString()); 571 return error; 572 } 573 574 Error 575 PlatformRemoteGDBServer::SetFilePermissions (const char *path, uint32_t file_permissions) 576 { 577 Error error = m_gdb_client.SetFilePermissions(path, file_permissions); 578 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM); 579 if (log) 580 log->Printf ("PlatformRemoteGDBServer::SetFilePermissions(path='%s', file_permissions=%o) error = %u (%s)", path, file_permissions, error.GetError(), error.AsCString()); 581 return error; 582 } 583 584 585 lldb::user_id_t 586 PlatformRemoteGDBServer::OpenFile (const lldb_private::FileSpec& file_spec, 587 uint32_t flags, 588 uint32_t mode, 589 Error &error) 590 { 591 return m_gdb_client.OpenFile (file_spec, flags, mode, error); 592 } 593 594 bool 595 PlatformRemoteGDBServer::CloseFile (lldb::user_id_t fd, Error &error) 596 { 597 return m_gdb_client.CloseFile (fd, error); 598 } 599 600 lldb::user_id_t 601 PlatformRemoteGDBServer::GetFileSize (const lldb_private::FileSpec& file_spec) 602 { 603 return m_gdb_client.GetFileSize(file_spec); 604 } 605 606 uint64_t 607 PlatformRemoteGDBServer::ReadFile (lldb::user_id_t fd, 608 uint64_t offset, 609 void *dst, 610 uint64_t dst_len, 611 Error &error) 612 { 613 return m_gdb_client.ReadFile (fd, offset, dst, dst_len, error); 614 } 615 616 uint64_t 617 PlatformRemoteGDBServer::WriteFile (lldb::user_id_t fd, 618 uint64_t offset, 619 const void* src, 620 uint64_t src_len, 621 Error &error) 622 { 623 return m_gdb_client.WriteFile (fd, offset, src, src_len, error); 624 } 625 626 lldb_private::Error 627 PlatformRemoteGDBServer::PutFile (const lldb_private::FileSpec& source, 628 const lldb_private::FileSpec& destination, 629 uint32_t uid, 630 uint32_t gid) 631 { 632 return Platform::PutFile(source,destination,uid,gid); 633 } 634 635 Error 636 PlatformRemoteGDBServer::CreateSymlink (const char *src, // The name of the link is in src 637 const char *dst) // The symlink points to dst 638 { 639 Error error = m_gdb_client.CreateSymlink (src, dst); 640 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM); 641 if (log) 642 log->Printf ("PlatformRemoteGDBServer::CreateSymlink(src='%s', dst='%s') error = %u (%s)", src, dst, error.GetError(), error.AsCString()); 643 return error; 644 } 645 646 Error 647 PlatformRemoteGDBServer::Unlink (const char *path) 648 { 649 Error error = m_gdb_client.Unlink (path); 650 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM); 651 if (log) 652 log->Printf ("PlatformRemoteGDBServer::Unlink(path='%s') error = %u (%s)", path, error.GetError(), error.AsCString()); 653 return error; 654 } 655 656 bool 657 PlatformRemoteGDBServer::GetFileExists (const lldb_private::FileSpec& file_spec) 658 { 659 return m_gdb_client.GetFileExists (file_spec); 660 } 661 662 lldb_private::Error 663 PlatformRemoteGDBServer::RunShellCommand (const char *command, // Shouldn't be NULL 664 const char *working_dir, // Pass NULL to use the current working directory 665 int *status_ptr, // Pass NULL if you don't want the process exit status 666 int *signo_ptr, // Pass NULL if you don't want the signal that caused the process to exit 667 std::string *command_output, // Pass NULL if you don't want the command output 668 uint32_t timeout_sec) // Timeout in seconds to wait for shell program to finish 669 { 670 return m_gdb_client.RunShellCommand (command, working_dir, status_ptr, signo_ptr, command_output, timeout_sec); 671 } 672