1 //===-- PlatformRemoteGDBServer.cpp -----------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/lldb-python.h"
12 #include "PlatformRemoteGDBServer.h"
15 #include <sys/sysctl.h>
18 // Other libraries and framework includes
20 #include "lldb/Breakpoint/BreakpointLocation.h"
21 #include "lldb/Core/ConnectionFileDescriptor.h"
22 #include "lldb/Core/Debugger.h"
23 #include "lldb/Core/Error.h"
24 #include "lldb/Core/Module.h"
25 #include "lldb/Core/ModuleList.h"
26 #include "lldb/Core/PluginManager.h"
27 #include "lldb/Core/StreamString.h"
28 #include "lldb/Host/FileSpec.h"
29 #include "lldb/Host/Host.h"
30 #include "lldb/Target/Process.h"
31 #include "lldb/Target/Target.h"
34 using namespace lldb_private;
36 static bool g_initialized = false;
39 PlatformRemoteGDBServer::Initialize ()
41 if (g_initialized == false)
44 PluginManager::RegisterPlugin (PlatformRemoteGDBServer::GetPluginNameStatic(),
45 PlatformRemoteGDBServer::GetDescriptionStatic(),
46 PlatformRemoteGDBServer::CreateInstance);
51 PlatformRemoteGDBServer::Terminate ()
55 g_initialized = false;
56 PluginManager::UnregisterPlugin (PlatformRemoteGDBServer::CreateInstance);
61 PlatformRemoteGDBServer::CreateInstance (bool force, const lldb_private::ArchSpec *arch)
66 create = !arch->TripleVendorWasSpecified() && !arch->TripleOSWasSpecified();
69 return new PlatformRemoteGDBServer ();
74 lldb_private::ConstString
75 PlatformRemoteGDBServer::GetPluginNameStatic()
77 static ConstString g_name("remote-gdb-server");
82 PlatformRemoteGDBServer::GetDescriptionStatic()
84 return "A platform that uses the GDB remote protocol as the communication transport.";
88 PlatformRemoteGDBServer::GetDescription ()
90 if (m_platform_description.empty())
94 // Send the get description packet
98 if (!m_platform_description.empty())
99 return m_platform_description.c_str();
100 return GetDescriptionStatic();
104 PlatformRemoteGDBServer::ResolveExecutable (const FileSpec &exe_file,
105 const ArchSpec &exe_arch,
106 lldb::ModuleSP &exe_module_sp,
107 const FileSpecList *module_search_paths_ptr)
110 error.SetErrorString ("PlatformRemoteGDBServer::ResolveExecutable() is unimplemented");
115 PlatformRemoteGDBServer::GetFile (const FileSpec &platform_file,
116 const UUID *uuid_ptr,
117 FileSpec &local_file)
119 // Default to the local case
120 local_file = platform_file;
124 //------------------------------------------------------------------
125 /// Default Constructor
126 //------------------------------------------------------------------
127 PlatformRemoteGDBServer::PlatformRemoteGDBServer () :
128 Platform(false), // This is a remote platform
133 //------------------------------------------------------------------
136 /// The destructor is virtual since this class is designed to be
137 /// inherited from by the plug-in instance.
138 //------------------------------------------------------------------
139 PlatformRemoteGDBServer::~PlatformRemoteGDBServer()
144 PlatformRemoteGDBServer::GetSupportedArchitectureAtIndex (uint32_t idx, ArchSpec &arch)
150 PlatformRemoteGDBServer::GetSoftwareBreakpointTrapOpcode (Target &target, BreakpointSite *bp_site)
152 // This isn't needed if the z/Z packets are supported in the GDB remote
153 // server. But we might need a packet to detect this.
158 PlatformRemoteGDBServer::GetRemoteOSVersion ()
160 uint32_t major, minor, update;
161 if (m_gdb_client.GetOSVersion (major, minor, update))
163 m_major_os_version = major;
164 m_minor_os_version = minor;
165 m_update_os_version = update;
172 PlatformRemoteGDBServer::GetRemoteOSBuildString (std::string &s)
174 return m_gdb_client.GetOSBuildString (s);
178 PlatformRemoteGDBServer::GetRemoteOSKernelDescription (std::string &s)
180 return m_gdb_client.GetOSKernelDescription (s);
183 // Remote Platform subclasses need to override this function
185 PlatformRemoteGDBServer::GetRemoteSystemArchitecture ()
187 return m_gdb_client.GetSystemArchitecture();
191 PlatformRemoteGDBServer::IsConnected () const
193 return m_gdb_client.IsConnected();
197 PlatformRemoteGDBServer::ConnectRemote (Args& args)
202 error.SetErrorStringWithFormat ("the platform is already connected to '%s', execute 'platform disconnect' to close the current connection",
207 if (args.GetArgumentCount() == 1)
209 const char *url = args.GetArgumentAtIndex(0);
210 m_gdb_client.SetConnection (new ConnectionFileDescriptor());
211 const ConnectionStatus status = m_gdb_client.Connect(url, &error);
212 if (status == eConnectionStatusSuccess)
214 if (m_gdb_client.HandshakeWithServer(&error))
216 m_gdb_client.QueryNoAckModeSupported();
217 m_gdb_client.GetHostInfo();
219 m_gdb_client.TestPacketSpeed(10000);
224 m_gdb_client.Disconnect();
230 error.SetErrorString ("\"platform connect\" takes a single argument: <connect-url>");
238 PlatformRemoteGDBServer::DisconnectRemote ()
241 m_gdb_client.Disconnect(&error);
246 PlatformRemoteGDBServer::GetHostname ()
248 m_gdb_client.GetHostname (m_name);
251 return m_name.c_str();
255 PlatformRemoteGDBServer::GetUserName (uint32_t uid)
257 // Try and get a cache user name first
258 const char *cached_user_name = Platform::GetUserName(uid);
259 if (cached_user_name)
260 return cached_user_name;
262 if (m_gdb_client.GetUserName(uid, name))
263 return SetCachedUserName(uid, name.c_str(), name.size());
265 SetUserNameNotFound(uid); // Negative cache so we don't keep sending packets
270 PlatformRemoteGDBServer::GetGroupName (uint32_t gid)
272 const char *cached_group_name = Platform::GetGroupName(gid);
273 if (cached_group_name)
274 return cached_group_name;
276 if (m_gdb_client.GetGroupName(gid, name))
277 return SetCachedGroupName(gid, name.c_str(), name.size());
279 SetGroupNameNotFound(gid); // Negative cache so we don't keep sending packets
284 PlatformRemoteGDBServer::FindProcesses (const ProcessInstanceInfoMatch &match_info,
285 ProcessInstanceInfoList &process_infos)
287 return m_gdb_client.FindProcesses (match_info, process_infos);
291 PlatformRemoteGDBServer::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
293 return m_gdb_client.GetProcessInfo (pid, process_info);
298 PlatformRemoteGDBServer::LaunchProcess (ProcessLaunchInfo &launch_info)
301 lldb::pid_t pid = LLDB_INVALID_PROCESS_ID;
303 m_gdb_client.SetSTDIN ("/dev/null");
304 m_gdb_client.SetSTDOUT ("/dev/null");
305 m_gdb_client.SetSTDERR ("/dev/null");
306 m_gdb_client.SetDisableASLR (launch_info.GetFlags().Test (eLaunchFlagDisableASLR));
308 const char *working_dir = launch_info.GetWorkingDirectory();
309 if (working_dir && working_dir[0])
311 m_gdb_client.SetWorkingDir (working_dir);
314 // Send the environment and the program + arguments after we connect
315 const char **argv = launch_info.GetArguments().GetConstArgumentVector();
316 const char **envp = launch_info.GetEnvironmentEntries().GetConstArgumentVector();
320 const char *env_entry;
321 for (int i=0; (env_entry = envp[i]); ++i)
323 if (m_gdb_client.SendEnvironmentPacket(env_entry) != 0)
327 const uint32_t old_packet_timeout = m_gdb_client.SetPacketTimeout (5);
328 int arg_packet_err = m_gdb_client.SendArgumentsPacket (argv);
329 m_gdb_client.SetPacketTimeout (old_packet_timeout);
330 if (arg_packet_err == 0)
332 std::string error_str;
333 if (m_gdb_client.GetLaunchSuccess (error_str))
335 pid = m_gdb_client.GetCurrentProcessID ();
336 if (pid != LLDB_INVALID_PROCESS_ID)
337 launch_info.SetProcessID (pid);
341 error.SetErrorString (error_str.c_str());
346 error.SetErrorStringWithFormat("'A' packet returned an error: %i", arg_packet_err);
352 PlatformRemoteGDBServer::Attach (lldb_private::ProcessAttachInfo &attach_info,
354 Target *target, // Can be NULL, if NULL create a new target, else use existing one
358 lldb::ProcessSP process_sp;
363 uint16_t port = m_gdb_client.LaunchGDBserverAndGetPort();
367 error.SetErrorStringWithFormat ("unable to launch a GDB server on '%s'", GetHostname ());
373 TargetSP new_target_sp;
375 error = debugger.GetTargetList().CreateTarget (debugger,
381 target = new_target_sp.get();
386 if (target && error.Success())
388 debugger.GetTargetList().SetSelectedTarget(target);
390 // The darwin always currently uses the GDB remote debugger plug-in
391 // so even when debugging locally we are debugging remotely!
392 process_sp = target->CreateProcess (listener, "gdb-remote", NULL);
396 char connect_url[256];
397 const int connect_url_len = ::snprintf (connect_url,
402 assert (connect_url_len < (int)sizeof(connect_url));
403 error = process_sp->ConnectRemote (NULL, connect_url);
405 error = process_sp->Attach(attach_info);
412 error.SetErrorString("not connected to remote gdb server");