blob: 4bc0857b858f916975335ce0f52a89e0e1c8ffcd [file] [log] [blame]
Greg Clayton576d8832011-03-22 04:00:09 +00001//===-- GDBRemoteCommunicationServer.h --------------------------*- 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#ifndef liblldb_GDBRemoteCommunicationServer_h_
11#define liblldb_GDBRemoteCommunicationServer_h_
12
13// C Includes
14// C++ Includes
Daniel Maleae0f8f572013-08-26 23:57:52 +000015#include <vector>
16#include <set>
Greg Clayton576d8832011-03-22 04:00:09 +000017// Other libraries and framework includes
18// Project includes
Daniel Maleae0f8f572013-08-26 23:57:52 +000019#include "lldb/Host/Mutex.h"
Greg Clayton32e0a752011-03-30 18:16:51 +000020#include "lldb/Target/Process.h"
Greg Clayton576d8832011-03-22 04:00:09 +000021#include "GDBRemoteCommunication.h"
22
23class ProcessGDBRemote;
Greg Clayton32e0a752011-03-30 18:16:51 +000024class StringExtractorGDBRemote;
Greg Clayton576d8832011-03-22 04:00:09 +000025
26class GDBRemoteCommunicationServer : public GDBRemoteCommunication
27{
28public:
Greg Clayton29b8fc42013-11-21 01:44:58 +000029 typedef std::map<uint16_t, lldb::pid_t> PortMap;
30
Greg Clayton576d8832011-03-22 04:00:09 +000031 enum
32 {
33 eBroadcastBitRunPacketSent = kLoUserBroadcastBit
34 };
35 //------------------------------------------------------------------
36 // Constructors and Destructors
37 //------------------------------------------------------------------
Greg Clayton8b82f082011-04-12 05:54:46 +000038 GDBRemoteCommunicationServer(bool is_platform);
Greg Clayton576d8832011-03-22 04:00:09 +000039
Todd Fialab8b49ec2014-01-28 00:34:23 +000040 GDBRemoteCommunicationServer(bool is_platform,
41 const lldb::PlatformSP& platform_sp);
42
Greg Clayton576d8832011-03-22 04:00:09 +000043 virtual
44 ~GDBRemoteCommunicationServer();
45
46 bool
Greg Clayton73bf5db2011-06-17 01:22:15 +000047 GetPacketAndSendResponse (uint32_t timeout_usec,
Greg Clayton1cb64962011-03-24 04:28:38 +000048 lldb_private::Error &error,
Greg Claytond314e812011-03-23 00:09:55 +000049 bool &interrupt,
50 bool &quit);
Greg Clayton576d8832011-03-22 04:00:09 +000051
52 virtual bool
53 GetThreadSuffixSupported ()
54 {
55 return true;
56 }
57
Greg Clayton1cb64962011-03-24 04:28:38 +000058 // After connecting, do a little handshake with the client to make sure
59 // we are at least communicating
60 bool
61 HandshakeWithClient (lldb_private::Error *error_ptr);
Greg Clayton576d8832011-03-22 04:00:09 +000062
Greg Clayton8b82f082011-04-12 05:54:46 +000063 // Set both ports to zero to let the platform automatically bind to
64 // a port chosen by the OS.
65 void
Greg Clayton29b8fc42013-11-21 01:44:58 +000066 SetPortMap (PortMap &&port_map)
Greg Clayton8b82f082011-04-12 05:54:46 +000067 {
Greg Clayton29b8fc42013-11-21 01:44:58 +000068 m_port_map = port_map;
Daniel Maleae0f8f572013-08-26 23:57:52 +000069 }
70
Greg Clayton29b8fc42013-11-21 01:44:58 +000071 //----------------------------------------------------------------------
72 // If we are using a port map where we can only use certain ports,
73 // get the next available port.
74 //
75 // If we are using a port map and we are out of ports, return UINT16_MAX
76 //
77 // If we aren't using a port map, return 0 to indicate we should bind to
78 // port 0 and then figure out which port we used.
79 //----------------------------------------------------------------------
Daniel Maleae0f8f572013-08-26 23:57:52 +000080 uint16_t
Greg Clayton29b8fc42013-11-21 01:44:58 +000081 GetNextAvailablePort ()
Daniel Maleae0f8f572013-08-26 23:57:52 +000082 {
Greg Clayton29b8fc42013-11-21 01:44:58 +000083 if (m_port_map.empty())
84 return 0; // Bind to port zero and get a port, we didn't have any limitations
85
86 for (auto &pair : m_port_map)
87 {
88 if (pair.second == LLDB_INVALID_PROCESS_ID)
89 {
90 pair.second = ~(lldb::pid_t)LLDB_INVALID_PROCESS_ID;
91 return pair.first;
92 }
93 }
94 return UINT16_MAX;
95 }
96
97 bool
98 AssociatePortWithProcess (uint16_t port, lldb::pid_t pid)
99 {
100 PortMap::iterator pos = m_port_map.find(port);
101 if (pos != m_port_map.end())
102 {
103 pos->second = pid;
104 return true;
105 }
106 return false;
107 }
108
109 bool
110 FreePort (uint16_t port)
111 {
112 PortMap::iterator pos = m_port_map.find(port);
113 if (pos != m_port_map.end())
114 {
115 pos->second = LLDB_INVALID_PROCESS_ID;
116 return true;
117 }
118 return false;
119 }
120
121 bool
122 FreePortForProcess (lldb::pid_t pid)
123 {
124 if (!m_port_map.empty())
125 {
126 for (auto &pair : m_port_map)
127 {
128 if (pair.second == pid)
129 {
130 pair.second = LLDB_INVALID_PROCESS_ID;
131 return true;
132 }
133 }
134 }
135 return false;
Greg Clayton8b82f082011-04-12 05:54:46 +0000136 }
137
Greg Clayton2b98c562013-11-22 18:53:12 +0000138 void
139 SetPortOffset (uint16_t port_offset)
140 {
141 m_port_offset = port_offset;
142 }
143
Todd Fiala403edc52014-01-23 22:05:44 +0000144 //------------------------------------------------------------------
Todd Fiala3e92a2b2014-01-24 00:52:53 +0000145 /// Specify the program to launch and its arguments.
Todd Fiala403edc52014-01-23 22:05:44 +0000146 ///
Todd Fiala3e92a2b2014-01-24 00:52:53 +0000147 /// The LaunchProcess () command can be executed to do the lauching.
Todd Fiala403edc52014-01-23 22:05:44 +0000148 ///
149 /// @param[in] args
150 /// The command line to launch.
151 ///
152 /// @param[in] argc
153 /// The number of elements in the args array of cstring pointers.
154 ///
Todd Fiala3e92a2b2014-01-24 00:52:53 +0000155 /// @return
156 /// An Error object indicating the success or failure of making
157 /// the setting.
158 //------------------------------------------------------------------
159 lldb_private::Error
160 SetLaunchArguments (const char *const args[], int argc);
161
162 //------------------------------------------------------------------
163 /// Specify the launch flags for the process.
164 ///
165 /// The LaunchProcess () command can be executed to do the lauching.
166 ///
Todd Fiala403edc52014-01-23 22:05:44 +0000167 /// @param[in] launch_flags
168 /// The launch flags to use when launching this process.
169 ///
170 /// @return
Todd Fiala3e92a2b2014-01-24 00:52:53 +0000171 /// An Error object indicating the success or failure of making
172 /// the setting.
173 //------------------------------------------------------------------
174 lldb_private::Error
175 SetLaunchFlags (unsigned int launch_flags);
176
177 //------------------------------------------------------------------
178 /// Launch a process with the current launch settings.
179 ///
180 /// This method supports running an lldb-gdbserver or similar
181 /// server in a situation where the startup code has been provided
182 /// with all the information for a child process to be launched.
183 ///
184 /// @return
Todd Fiala403edc52014-01-23 22:05:44 +0000185 /// An Error object indicating the success or failure of the
186 /// launch.
187 //------------------------------------------------------------------
188 lldb_private::Error
Todd Fiala3e92a2b2014-01-24 00:52:53 +0000189 LaunchProcess ();
Todd Fiala403edc52014-01-23 22:05:44 +0000190
Greg Clayton576d8832011-03-22 04:00:09 +0000191protected:
Todd Fialab8b49ec2014-01-28 00:34:23 +0000192 lldb::PlatformSP m_platform_sp;
Greg Clayton576d8832011-03-22 04:00:09 +0000193 lldb::thread_t m_async_thread;
Greg Clayton8b82f082011-04-12 05:54:46 +0000194 lldb_private::ProcessLaunchInfo m_process_launch_info;
195 lldb_private::Error m_process_launch_error;
Daniel Maleae0f8f572013-08-26 23:57:52 +0000196 std::set<lldb::pid_t> m_spawned_pids;
197 lldb_private::Mutex m_spawned_pids_mutex;
Greg Clayton8b82f082011-04-12 05:54:46 +0000198 lldb_private::ProcessInstanceInfoList m_proc_infos;
Greg Clayton32e0a752011-03-30 18:16:51 +0000199 uint32_t m_proc_infos_index;
Greg Clayton29b8fc42013-11-21 01:44:58 +0000200 PortMap m_port_map;
Greg Clayton2b98c562013-11-22 18:53:12 +0000201 uint16_t m_port_offset;
Todd Fialab8b49ec2014-01-28 00:34:23 +0000202
Greg Clayton576d8832011-03-22 04:00:09 +0000203
Greg Clayton3dedae12013-12-06 21:45:27 +0000204 PacketResult
Greg Clayton32e0a752011-03-30 18:16:51 +0000205 SendUnimplementedResponse (const char *packet);
206
Greg Clayton3dedae12013-12-06 21:45:27 +0000207 PacketResult
Greg Clayton32e0a752011-03-30 18:16:51 +0000208 SendErrorResponse (uint8_t error);
Greg Clayton576d8832011-03-22 04:00:09 +0000209
Greg Clayton3dedae12013-12-06 21:45:27 +0000210 PacketResult
Greg Clayton1cb64962011-03-24 04:28:38 +0000211 SendOKResponse ();
Greg Clayton576d8832011-03-22 04:00:09 +0000212
Greg Clayton3dedae12013-12-06 21:45:27 +0000213 PacketResult
Greg Clayton8b82f082011-04-12 05:54:46 +0000214 Handle_A (StringExtractorGDBRemote &packet);
Daniel Maleae0f8f572013-08-26 23:57:52 +0000215
Greg Clayton3dedae12013-12-06 21:45:27 +0000216 PacketResult
Greg Clayton8b82f082011-04-12 05:54:46 +0000217 Handle_qLaunchSuccess (StringExtractorGDBRemote &packet);
218
Greg Clayton3dedae12013-12-06 21:45:27 +0000219 PacketResult
Greg Clayton32e0a752011-03-30 18:16:51 +0000220 Handle_qHostInfo (StringExtractorGDBRemote &packet);
221
Greg Clayton3dedae12013-12-06 21:45:27 +0000222 PacketResult
Greg Clayton8b82f082011-04-12 05:54:46 +0000223 Handle_qLaunchGDBServer (StringExtractorGDBRemote &packet);
Daniel Maleae0f8f572013-08-26 23:57:52 +0000224
Greg Clayton3dedae12013-12-06 21:45:27 +0000225 PacketResult
Daniel Maleae0f8f572013-08-26 23:57:52 +0000226 Handle_qKillSpawnedProcess (StringExtractorGDBRemote &packet);
Greg Clayton8b82f082011-04-12 05:54:46 +0000227
Greg Clayton3dedae12013-12-06 21:45:27 +0000228 PacketResult
Todd Fiala403edc52014-01-23 22:05:44 +0000229 Handle_k (StringExtractorGDBRemote &packet);
230
231 PacketResult
Greg Claytonfbb76342013-11-20 21:07:01 +0000232 Handle_qPlatform_mkdir (StringExtractorGDBRemote &packet);
233
Greg Clayton3dedae12013-12-06 21:45:27 +0000234 PacketResult
Greg Claytonfbb76342013-11-20 21:07:01 +0000235 Handle_qPlatform_chmod (StringExtractorGDBRemote &packet);
Daniel Maleae0f8f572013-08-26 23:57:52 +0000236
Greg Clayton3dedae12013-12-06 21:45:27 +0000237 PacketResult
Greg Clayton32e0a752011-03-30 18:16:51 +0000238 Handle_qProcessInfoPID (StringExtractorGDBRemote &packet);
239
Greg Clayton3dedae12013-12-06 21:45:27 +0000240 PacketResult
Greg Clayton32e0a752011-03-30 18:16:51 +0000241 Handle_qfProcessInfo (StringExtractorGDBRemote &packet);
242
Greg Clayton3dedae12013-12-06 21:45:27 +0000243 PacketResult
Greg Clayton32e0a752011-03-30 18:16:51 +0000244 Handle_qsProcessInfo (StringExtractorGDBRemote &packet);
245
Greg Clayton3dedae12013-12-06 21:45:27 +0000246 PacketResult
Greg Clayton8b82f082011-04-12 05:54:46 +0000247 Handle_qC (StringExtractorGDBRemote &packet);
248
Greg Clayton3dedae12013-12-06 21:45:27 +0000249 PacketResult
Greg Clayton32e0a752011-03-30 18:16:51 +0000250 Handle_qUserName (StringExtractorGDBRemote &packet);
251
Greg Clayton3dedae12013-12-06 21:45:27 +0000252 PacketResult
Greg Clayton32e0a752011-03-30 18:16:51 +0000253 Handle_qGroupName (StringExtractorGDBRemote &packet);
Greg Clayton576d8832011-03-22 04:00:09 +0000254
Greg Clayton3dedae12013-12-06 21:45:27 +0000255 PacketResult
Greg Clayton9b1e1cd2011-04-04 18:18:57 +0000256 Handle_qSpeedTest (StringExtractorGDBRemote &packet);
257
Greg Clayton3dedae12013-12-06 21:45:27 +0000258 PacketResult
Greg Clayton8b82f082011-04-12 05:54:46 +0000259 Handle_QEnvironment (StringExtractorGDBRemote &packet);
260
Greg Clayton3dedae12013-12-06 21:45:27 +0000261 PacketResult
Daniel Maleae0f8f572013-08-26 23:57:52 +0000262 Handle_QLaunchArch (StringExtractorGDBRemote &packet);
263
Greg Clayton3dedae12013-12-06 21:45:27 +0000264 PacketResult
Greg Clayton8b82f082011-04-12 05:54:46 +0000265 Handle_QSetDisableASLR (StringExtractorGDBRemote &packet);
266
Greg Clayton3dedae12013-12-06 21:45:27 +0000267 PacketResult
Jim Ingham106d0282014-06-25 02:32:56 +0000268 Handle_QSetDetachOnError (StringExtractorGDBRemote &packet);
269
270 PacketResult
Greg Clayton8b82f082011-04-12 05:54:46 +0000271 Handle_QSetWorkingDir (StringExtractorGDBRemote &packet);
Greg Claytonfbb76342013-11-20 21:07:01 +0000272
Greg Clayton3dedae12013-12-06 21:45:27 +0000273 PacketResult
Greg Claytonfbb76342013-11-20 21:07:01 +0000274 Handle_qGetWorkingDir (StringExtractorGDBRemote &packet);
Greg Clayton8b82f082011-04-12 05:54:46 +0000275
Greg Clayton3dedae12013-12-06 21:45:27 +0000276 PacketResult
Greg Clayton32e0a752011-03-30 18:16:51 +0000277 Handle_QStartNoAckMode (StringExtractorGDBRemote &packet);
Greg Clayton1cb64962011-03-24 04:28:38 +0000278
Greg Clayton3dedae12013-12-06 21:45:27 +0000279 PacketResult
Greg Clayton8b82f082011-04-12 05:54:46 +0000280 Handle_QSetSTDIN (StringExtractorGDBRemote &packet);
281
Greg Clayton3dedae12013-12-06 21:45:27 +0000282 PacketResult
Greg Clayton8b82f082011-04-12 05:54:46 +0000283 Handle_QSetSTDOUT (StringExtractorGDBRemote &packet);
284
Greg Clayton3dedae12013-12-06 21:45:27 +0000285 PacketResult
Greg Clayton8b82f082011-04-12 05:54:46 +0000286 Handle_QSetSTDERR (StringExtractorGDBRemote &packet);
287
Greg Clayton3dedae12013-12-06 21:45:27 +0000288 PacketResult
Daniel Maleae0f8f572013-08-26 23:57:52 +0000289 Handle_vFile_Open (StringExtractorGDBRemote &packet);
290
Greg Clayton3dedae12013-12-06 21:45:27 +0000291 PacketResult
Daniel Maleae0f8f572013-08-26 23:57:52 +0000292 Handle_vFile_Close (StringExtractorGDBRemote &packet);
293
Greg Clayton3dedae12013-12-06 21:45:27 +0000294 PacketResult
Daniel Maleae0f8f572013-08-26 23:57:52 +0000295 Handle_vFile_pRead (StringExtractorGDBRemote &packet);
296
Greg Clayton3dedae12013-12-06 21:45:27 +0000297 PacketResult
Daniel Maleae0f8f572013-08-26 23:57:52 +0000298 Handle_vFile_pWrite (StringExtractorGDBRemote &packet);
299
Greg Clayton3dedae12013-12-06 21:45:27 +0000300 PacketResult
Daniel Maleae0f8f572013-08-26 23:57:52 +0000301 Handle_vFile_Size (StringExtractorGDBRemote &packet);
302
Greg Clayton3dedae12013-12-06 21:45:27 +0000303 PacketResult
Daniel Maleae0f8f572013-08-26 23:57:52 +0000304 Handle_vFile_Mode (StringExtractorGDBRemote &packet);
305
Greg Clayton3dedae12013-12-06 21:45:27 +0000306 PacketResult
Daniel Maleae0f8f572013-08-26 23:57:52 +0000307 Handle_vFile_Exists (StringExtractorGDBRemote &packet);
Greg Claytonfbb76342013-11-20 21:07:01 +0000308
Greg Clayton3dedae12013-12-06 21:45:27 +0000309 PacketResult
Greg Claytonfbb76342013-11-20 21:07:01 +0000310 Handle_vFile_symlink (StringExtractorGDBRemote &packet);
311
Greg Clayton3dedae12013-12-06 21:45:27 +0000312 PacketResult
Greg Claytonfbb76342013-11-20 21:07:01 +0000313 Handle_vFile_unlink (StringExtractorGDBRemote &packet);
Daniel Maleae0f8f572013-08-26 23:57:52 +0000314
Greg Clayton3dedae12013-12-06 21:45:27 +0000315 PacketResult
Daniel Maleae0f8f572013-08-26 23:57:52 +0000316 Handle_vFile_Stat (StringExtractorGDBRemote &packet);
317
Greg Clayton3dedae12013-12-06 21:45:27 +0000318 PacketResult
Daniel Maleae0f8f572013-08-26 23:57:52 +0000319 Handle_vFile_MD5 (StringExtractorGDBRemote &packet);
320
Greg Clayton3dedae12013-12-06 21:45:27 +0000321 PacketResult
Greg Claytonfbb76342013-11-20 21:07:01 +0000322 Handle_qPlatform_shell (StringExtractorGDBRemote &packet);
Daniel Maleae0f8f572013-08-26 23:57:52 +0000323
Greg Clayton576d8832011-03-22 04:00:09 +0000324private:
Daniel Maleae0f8f572013-08-26 23:57:52 +0000325 bool
326 DebugserverProcessReaped (lldb::pid_t pid);
327
328 static bool
329 ReapDebugserverProcess (void *callback_baton,
330 lldb::pid_t pid,
331 bool exited,
332 int signal,
333 int status);
334
Todd Fiala403edc52014-01-23 22:05:44 +0000335 bool
Todd Fiala3e92a2b2014-01-24 00:52:53 +0000336 DebuggedProcessReaped (lldb::pid_t pid);
337
338 static bool
339 ReapDebuggedProcess (void *callback_baton,
340 lldb::pid_t pid,
341 bool exited,
342 int signal,
343 int status);
344
345 bool
Todd Fiala403edc52014-01-23 22:05:44 +0000346 KillSpawnedProcess (lldb::pid_t pid);
347
Greg Clayton576d8832011-03-22 04:00:09 +0000348 //------------------------------------------------------------------
349 // For GDBRemoteCommunicationServer only
350 //------------------------------------------------------------------
351 DISALLOW_COPY_AND_ASSIGN (GDBRemoteCommunicationServer);
352};
353
354#endif // liblldb_GDBRemoteCommunicationServer_h_