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.GetHostInfo(); 257 // If a working directory was set prior to connecting, send it down now 258 if (m_working_dir) 259 m_gdb_client.SetWorkingDir(m_working_dir.GetCString()); 260 #if 0 261 m_gdb_client.TestPacketSpeed(10000); 262 #endif 263 } 264 else 265 { 266 m_gdb_client.Disconnect(); 267 if (error.Success()) 268 error.SetErrorString("handshake failed"); 269 } 270 } 271 } 272 else 273 { 274 error.SetErrorString ("\"platform connect\" takes a single argument: <connect-url>"); 275 } 276 } 277 278 return error; 279 } 280 281 Error 282 PlatformRemoteGDBServer::DisconnectRemote () 283 { 284 Error error; 285 m_gdb_client.Disconnect(&error); 286 return error; 287 } 288 289 const char * 290 PlatformRemoteGDBServer::GetHostname () 291 { 292 m_gdb_client.GetHostname (m_name); 293 if (m_name.empty()) 294 return NULL; 295 return m_name.c_str(); 296 } 297 298 const char * 299 PlatformRemoteGDBServer::GetUserName (uint32_t uid) 300 { 301 // Try and get a cache user name first 302 const char *cached_user_name = Platform::GetUserName(uid); 303 if (cached_user_name) 304 return cached_user_name; 305 std::string name; 306 if (m_gdb_client.GetUserName(uid, name)) 307 return SetCachedUserName(uid, name.c_str(), name.size()); 308 309 SetUserNameNotFound(uid); // Negative cache so we don't keep sending packets 310 return NULL; 311 } 312 313 const char * 314 PlatformRemoteGDBServer::GetGroupName (uint32_t gid) 315 { 316 const char *cached_group_name = Platform::GetGroupName(gid); 317 if (cached_group_name) 318 return cached_group_name; 319 std::string name; 320 if (m_gdb_client.GetGroupName(gid, name)) 321 return SetCachedGroupName(gid, name.c_str(), name.size()); 322 323 SetGroupNameNotFound(gid); // Negative cache so we don't keep sending packets 324 return NULL; 325 } 326 327 uint32_t 328 PlatformRemoteGDBServer::FindProcesses (const ProcessInstanceInfoMatch &match_info, 329 ProcessInstanceInfoList &process_infos) 330 { 331 return m_gdb_client.FindProcesses (match_info, process_infos); 332 } 333 334 bool 335 PlatformRemoteGDBServer::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info) 336 { 337 return m_gdb_client.GetProcessInfo (pid, process_info); 338 } 339 340 341 Error 342 PlatformRemoteGDBServer::LaunchProcess (ProcessLaunchInfo &launch_info) 343 { 344 Error error; 345 lldb::pid_t pid = LLDB_INVALID_PROCESS_ID; 346 347 m_gdb_client.SetSTDIN ("/dev/null"); 348 m_gdb_client.SetSTDOUT ("/dev/null"); 349 m_gdb_client.SetSTDERR ("/dev/null"); 350 m_gdb_client.SetDisableASLR (launch_info.GetFlags().Test (eLaunchFlagDisableASLR)); 351 352 const char *working_dir = launch_info.GetWorkingDirectory(); 353 if (working_dir && working_dir[0]) 354 { 355 m_gdb_client.SetWorkingDir (working_dir); 356 } 357 358 // Send the environment and the program + arguments after we connect 359 const char **envp = launch_info.GetEnvironmentEntries().GetConstArgumentVector(); 360 361 if (envp) 362 { 363 const char *env_entry; 364 for (int i=0; (env_entry = envp[i]); ++i) 365 { 366 if (m_gdb_client.SendEnvironmentPacket(env_entry) != 0) 367 break; 368 } 369 } 370 371 ArchSpec arch_spec = launch_info.GetArchitecture(); 372 const char *arch_triple = arch_spec.GetTriple().str().c_str(); 373 374 m_gdb_client.SendLaunchArchPacket(arch_triple); 375 376 const uint32_t old_packet_timeout = m_gdb_client.SetPacketTimeout (5); 377 int arg_packet_err = m_gdb_client.SendArgumentsPacket (launch_info); 378 m_gdb_client.SetPacketTimeout (old_packet_timeout); 379 if (arg_packet_err == 0) 380 { 381 std::string error_str; 382 if (m_gdb_client.GetLaunchSuccess (error_str)) 383 { 384 pid = m_gdb_client.GetCurrentProcessID (); 385 if (pid != LLDB_INVALID_PROCESS_ID) 386 launch_info.SetProcessID (pid); 387 } 388 else 389 { 390 error.SetErrorString (error_str.c_str()); 391 } 392 } 393 else 394 { 395 error.SetErrorStringWithFormat("'A' packet returned an error: %i", arg_packet_err); 396 } 397 return error; 398 } 399 400 lldb::ProcessSP 401 PlatformRemoteGDBServer::DebugProcess (lldb_private::ProcessLaunchInfo &launch_info, 402 lldb_private::Debugger &debugger, 403 lldb_private::Target *target, // Can be NULL, if NULL create a new target, else use existing one 404 lldb_private::Listener &listener, 405 lldb_private::Error &error) 406 { 407 lldb::ProcessSP process_sp; 408 if (IsRemote()) 409 { 410 if (IsConnected()) 411 { 412 lldb::pid_t debugserver_pid = LLDB_INVALID_PROCESS_ID; 413 uint16_t port = m_gdb_client.LaunchGDBserverAndGetPort(debugserver_pid); 414 415 if (port == 0) 416 { 417 error.SetErrorStringWithFormat ("unable to launch a GDB server on '%s'", GetHostname ()); 418 } 419 else 420 { 421 if (target == NULL) 422 { 423 TargetSP new_target_sp; 424 425 error = debugger.GetTargetList().CreateTarget (debugger, 426 NULL, 427 NULL, 428 false, 429 NULL, 430 new_target_sp); 431 target = new_target_sp.get(); 432 } 433 else 434 error.Clear(); 435 436 if (target && error.Success()) 437 { 438 debugger.GetTargetList().SetSelectedTarget(target); 439 440 // The darwin always currently uses the GDB remote debugger plug-in 441 // so even when debugging locally we are debugging remotely! 442 process_sp = target->CreateProcess (listener, "gdb-remote", NULL); 443 444 if (process_sp) 445 { 446 char connect_url[256]; 447 const char *override_hostname = getenv("LLDB_PLATFORM_REMOTE_GDB_SERVER_HOSTNAME"); 448 const char *port_offset_c_str = getenv("LLDB_PLATFORM_REMOTE_GDB_SERVER_PORT_OFFSET"); 449 int port_offset = port_offset_c_str ? ::atoi(port_offset_c_str) : 0; 450 const int connect_url_len = ::snprintf (connect_url, 451 sizeof(connect_url), 452 "connect://%s:%u", 453 override_hostname ? override_hostname : GetHostname (), 454 port + port_offset); 455 assert (connect_url_len < (int)sizeof(connect_url)); 456 error = process_sp->ConnectRemote (NULL, connect_url); 457 if (error.Success()) 458 error = process_sp->Launch(launch_info); 459 else if (debugserver_pid != LLDB_INVALID_PROCESS_ID) 460 m_gdb_client.KillSpawnedProcess(debugserver_pid); 461 } 462 } 463 } 464 } 465 else 466 { 467 error.SetErrorString("not connected to remote gdb server"); 468 } 469 } 470 return process_sp; 471 472 } 473 474 lldb::ProcessSP 475 PlatformRemoteGDBServer::Attach (lldb_private::ProcessAttachInfo &attach_info, 476 Debugger &debugger, 477 Target *target, // Can be NULL, if NULL create a new target, else use existing one 478 Listener &listener, 479 Error &error) 480 { 481 lldb::ProcessSP process_sp; 482 if (IsRemote()) 483 { 484 if (IsConnected()) 485 { 486 lldb::pid_t debugserver_pid = LLDB_INVALID_PROCESS_ID; 487 uint16_t port = m_gdb_client.LaunchGDBserverAndGetPort(debugserver_pid); 488 489 if (port == 0) 490 { 491 error.SetErrorStringWithFormat ("unable to launch a GDB server on '%s'", GetHostname ()); 492 } 493 else 494 { 495 if (target == NULL) 496 { 497 TargetSP new_target_sp; 498 499 error = debugger.GetTargetList().CreateTarget (debugger, 500 NULL, 501 NULL, 502 false, 503 NULL, 504 new_target_sp); 505 target = new_target_sp.get(); 506 } 507 else 508 error.Clear(); 509 510 if (target && error.Success()) 511 { 512 debugger.GetTargetList().SetSelectedTarget(target); 513 514 // The darwin always currently uses the GDB remote debugger plug-in 515 // so even when debugging locally we are debugging remotely! 516 process_sp = target->CreateProcess (listener, "gdb-remote", NULL); 517 518 if (process_sp) 519 { 520 char connect_url[256]; 521 const char *override_hostname = getenv("LLDB_PLATFORM_REMOTE_GDB_SERVER_HOSTNAME"); 522 const char *port_offset_c_str = getenv("LLDB_PLATFORM_REMOTE_GDB_SERVER_PORT_OFFSET"); 523 int port_offset = port_offset_c_str ? ::atoi(port_offset_c_str) : 0; 524 const int connect_url_len = ::snprintf (connect_url, 525 sizeof(connect_url), 526 "connect://%s:%u", 527 override_hostname ? override_hostname : GetHostname (), 528 port + port_offset); 529 assert (connect_url_len < (int)sizeof(connect_url)); 530 error = process_sp->ConnectRemote (NULL, connect_url); 531 if (error.Success()) 532 error = process_sp->Attach(attach_info); 533 else if (debugserver_pid != LLDB_INVALID_PROCESS_ID) 534 { 535 m_gdb_client.KillSpawnedProcess(debugserver_pid); 536 } 537 } 538 } 539 } 540 } 541 else 542 { 543 error.SetErrorString("not connected to remote gdb server"); 544 } 545 } 546 return process_sp; 547 } 548 549 Error 550 PlatformRemoteGDBServer::MakeDirectory (const char *path, uint32_t mode) 551 { 552 Error error = m_gdb_client.MakeDirectory(path,mode); 553 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM); 554 if (log) 555 log->Printf ("PlatformRemoteGDBServer::MakeDirectory(path='%s', mode=%o) error = %u (%s)", path, mode, error.GetError(), error.AsCString()); 556 return error; 557 } 558 559 560 Error 561 PlatformRemoteGDBServer::GetFilePermissions (const char *path, uint32_t &file_permissions) 562 { 563 Error error = m_gdb_client.GetFilePermissions(path, file_permissions); 564 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM); 565 if (log) 566 log->Printf ("PlatformRemoteGDBServer::GetFilePermissions(path='%s', file_permissions=%o) error = %u (%s)", path, file_permissions, error.GetError(), error.AsCString()); 567 return error; 568 } 569 570 Error 571 PlatformRemoteGDBServer::SetFilePermissions (const char *path, uint32_t file_permissions) 572 { 573 Error error = m_gdb_client.SetFilePermissions(path, file_permissions); 574 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM); 575 if (log) 576 log->Printf ("PlatformRemoteGDBServer::SetFilePermissions(path='%s', file_permissions=%o) error = %u (%s)", path, file_permissions, error.GetError(), error.AsCString()); 577 return error; 578 } 579 580 581 lldb::user_id_t 582 PlatformRemoteGDBServer::OpenFile (const lldb_private::FileSpec& file_spec, 583 uint32_t flags, 584 uint32_t mode, 585 Error &error) 586 { 587 return m_gdb_client.OpenFile (file_spec, flags, mode, error); 588 } 589 590 bool 591 PlatformRemoteGDBServer::CloseFile (lldb::user_id_t fd, Error &error) 592 { 593 return m_gdb_client.CloseFile (fd, error); 594 } 595 596 lldb::user_id_t 597 PlatformRemoteGDBServer::GetFileSize (const lldb_private::FileSpec& file_spec) 598 { 599 return m_gdb_client.GetFileSize(file_spec); 600 } 601 602 uint64_t 603 PlatformRemoteGDBServer::ReadFile (lldb::user_id_t fd, 604 uint64_t offset, 605 void *dst, 606 uint64_t dst_len, 607 Error &error) 608 { 609 return m_gdb_client.ReadFile (fd, offset, dst, dst_len, error); 610 } 611 612 uint64_t 613 PlatformRemoteGDBServer::WriteFile (lldb::user_id_t fd, 614 uint64_t offset, 615 const void* src, 616 uint64_t src_len, 617 Error &error) 618 { 619 return m_gdb_client.WriteFile (fd, offset, src, src_len, error); 620 } 621 622 lldb_private::Error 623 PlatformRemoteGDBServer::PutFile (const lldb_private::FileSpec& source, 624 const lldb_private::FileSpec& destination, 625 uint32_t uid, 626 uint32_t gid) 627 { 628 return Platform::PutFile(source,destination,uid,gid); 629 } 630 631 Error 632 PlatformRemoteGDBServer::CreateSymlink (const char *src, // The name of the link is in src 633 const char *dst) // The symlink points to dst 634 { 635 Error error = m_gdb_client.CreateSymlink (src, dst); 636 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM); 637 if (log) 638 log->Printf ("PlatformRemoteGDBServer::CreateSymlink(src='%s', dst='%s') error = %u (%s)", src, dst, error.GetError(), error.AsCString()); 639 return error; 640 } 641 642 Error 643 PlatformRemoteGDBServer::Unlink (const char *path) 644 { 645 Error error = m_gdb_client.Unlink (path); 646 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM); 647 if (log) 648 log->Printf ("PlatformRemoteGDBServer::Unlink(path='%s') error = %u (%s)", path, error.GetError(), error.AsCString()); 649 return error; 650 } 651 652 bool 653 PlatformRemoteGDBServer::GetFileExists (const lldb_private::FileSpec& file_spec) 654 { 655 return m_gdb_client.GetFileExists (file_spec); 656 } 657 658 lldb_private::Error 659 PlatformRemoteGDBServer::RunShellCommand (const char *command, // Shouldn't be NULL 660 const char *working_dir, // Pass NULL to use the current working directory 661 int *status_ptr, // Pass NULL if you don't want the process exit status 662 int *signo_ptr, // Pass NULL if you don't want the signal that caused the process to exit 663 std::string *command_output, // Pass NULL if you don't want the command output 664 uint32_t timeout_sec) // Timeout in seconds to wait for shell program to finish 665 { 666 return m_gdb_client.RunShellCommand (command, working_dir, status_ptr, signo_ptr, command_output, timeout_sec); 667 } 668