1 //===-- GDBRemoteCommunicationServer.h --------------------------*- 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 #ifndef liblldb_GDBRemoteCommunicationServer_h_
11 #define liblldb_GDBRemoteCommunicationServer_h_
17 // Other libraries and framework includes
19 #include "lldb/Host/Mutex.h"
20 #include "lldb/Target/Process.h"
21 #include "GDBRemoteCommunication.h"
23 class ProcessGDBRemote;
24 class StringExtractorGDBRemote;
26 class GDBRemoteCommunicationServer : public GDBRemoteCommunication
29 typedef std::map<uint16_t, lldb::pid_t> PortMap;
33 eBroadcastBitRunPacketSent = kLoUserBroadcastBit
35 //------------------------------------------------------------------
36 // Constructors and Destructors
37 //------------------------------------------------------------------
38 GDBRemoteCommunicationServer(bool is_platform);
40 GDBRemoteCommunicationServer(bool is_platform,
41 const lldb::PlatformSP& platform_sp);
44 ~GDBRemoteCommunicationServer();
47 GetPacketAndSendResponse (uint32_t timeout_usec,
48 lldb_private::Error &error,
53 GetThreadSuffixSupported ()
58 // After connecting, do a little handshake with the client to make sure
59 // we are at least communicating
61 HandshakeWithClient (lldb_private::Error *error_ptr);
63 // Set both ports to zero to let the platform automatically bind to
64 // a port chosen by the OS.
66 SetPortMap (PortMap &&port_map)
68 m_port_map = port_map;
71 //----------------------------------------------------------------------
72 // If we are using a port map where we can only use certain ports,
73 // get the next available port.
75 // If we are using a port map and we are out of ports, return UINT16_MAX
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 //----------------------------------------------------------------------
81 GetNextAvailablePort ()
83 if (m_port_map.empty())
84 return 0; // Bind to port zero and get a port, we didn't have any limitations
86 for (auto &pair : m_port_map)
88 if (pair.second == LLDB_INVALID_PROCESS_ID)
90 pair.second = ~(lldb::pid_t)LLDB_INVALID_PROCESS_ID;
98 AssociatePortWithProcess (uint16_t port, lldb::pid_t pid)
100 PortMap::iterator pos = m_port_map.find(port);
101 if (pos != m_port_map.end())
110 FreePort (uint16_t port)
112 PortMap::iterator pos = m_port_map.find(port);
113 if (pos != m_port_map.end())
115 pos->second = LLDB_INVALID_PROCESS_ID;
122 FreePortForProcess (lldb::pid_t pid)
124 if (!m_port_map.empty())
126 for (auto &pair : m_port_map)
128 if (pair.second == pid)
130 pair.second = LLDB_INVALID_PROCESS_ID;
139 SetPortOffset (uint16_t port_offset)
141 m_port_offset = port_offset;
144 //------------------------------------------------------------------
145 /// Specify the program to launch and its arguments.
147 /// The LaunchProcess () command can be executed to do the lauching.
150 /// The command line to launch.
153 /// The number of elements in the args array of cstring pointers.
156 /// An Error object indicating the success or failure of making
158 //------------------------------------------------------------------
160 SetLaunchArguments (const char *const args[], int argc);
162 //------------------------------------------------------------------
163 /// Specify the launch flags for the process.
165 /// The LaunchProcess () command can be executed to do the lauching.
167 /// @param[in] launch_flags
168 /// The launch flags to use when launching this process.
171 /// An Error object indicating the success or failure of making
173 //------------------------------------------------------------------
175 SetLaunchFlags (unsigned int launch_flags);
177 //------------------------------------------------------------------
178 /// Launch a process with the current launch settings.
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.
185 /// An Error object indicating the success or failure of the
187 //------------------------------------------------------------------
192 lldb::PlatformSP m_platform_sp;
193 lldb::thread_t m_async_thread;
194 lldb_private::ProcessLaunchInfo m_process_launch_info;
195 lldb_private::Error m_process_launch_error;
196 std::set<lldb::pid_t> m_spawned_pids;
197 lldb_private::Mutex m_spawned_pids_mutex;
198 lldb_private::ProcessInstanceInfoList m_proc_infos;
199 uint32_t m_proc_infos_index;
201 uint16_t m_port_offset;
205 SendUnimplementedResponse (const char *packet);
208 SendErrorResponse (uint8_t error);
214 Handle_A (StringExtractorGDBRemote &packet);
217 Handle_qLaunchSuccess (StringExtractorGDBRemote &packet);
220 Handle_qHostInfo (StringExtractorGDBRemote &packet);
223 Handle_qLaunchGDBServer (StringExtractorGDBRemote &packet);
226 Handle_qKillSpawnedProcess (StringExtractorGDBRemote &packet);
229 Handle_k (StringExtractorGDBRemote &packet);
232 Handle_qPlatform_mkdir (StringExtractorGDBRemote &packet);
235 Handle_qPlatform_chmod (StringExtractorGDBRemote &packet);
238 Handle_qProcessInfoPID (StringExtractorGDBRemote &packet);
241 Handle_qfProcessInfo (StringExtractorGDBRemote &packet);
244 Handle_qsProcessInfo (StringExtractorGDBRemote &packet);
247 Handle_qC (StringExtractorGDBRemote &packet);
250 Handle_qUserName (StringExtractorGDBRemote &packet);
253 Handle_qGroupName (StringExtractorGDBRemote &packet);
256 Handle_qSpeedTest (StringExtractorGDBRemote &packet);
259 Handle_QEnvironment (StringExtractorGDBRemote &packet);
262 Handle_QLaunchArch (StringExtractorGDBRemote &packet);
265 Handle_QSetDisableASLR (StringExtractorGDBRemote &packet);
268 Handle_QSetWorkingDir (StringExtractorGDBRemote &packet);
271 Handle_qGetWorkingDir (StringExtractorGDBRemote &packet);
274 Handle_QStartNoAckMode (StringExtractorGDBRemote &packet);
277 Handle_QSetSTDIN (StringExtractorGDBRemote &packet);
280 Handle_QSetSTDOUT (StringExtractorGDBRemote &packet);
283 Handle_QSetSTDERR (StringExtractorGDBRemote &packet);
286 Handle_vFile_Open (StringExtractorGDBRemote &packet);
289 Handle_vFile_Close (StringExtractorGDBRemote &packet);
292 Handle_vFile_pRead (StringExtractorGDBRemote &packet);
295 Handle_vFile_pWrite (StringExtractorGDBRemote &packet);
298 Handle_vFile_Size (StringExtractorGDBRemote &packet);
301 Handle_vFile_Mode (StringExtractorGDBRemote &packet);
304 Handle_vFile_Exists (StringExtractorGDBRemote &packet);
307 Handle_vFile_symlink (StringExtractorGDBRemote &packet);
310 Handle_vFile_unlink (StringExtractorGDBRemote &packet);
313 Handle_vFile_Stat (StringExtractorGDBRemote &packet);
316 Handle_vFile_MD5 (StringExtractorGDBRemote &packet);
319 Handle_qPlatform_shell (StringExtractorGDBRemote &packet);
323 DebugserverProcessReaped (lldb::pid_t pid);
326 ReapDebugserverProcess (void *callback_baton,
333 DebuggedProcessReaped (lldb::pid_t pid);
336 ReapDebuggedProcess (void *callback_baton,
343 KillSpawnedProcess (lldb::pid_t pid);
345 //------------------------------------------------------------------
346 // For GDBRemoteCommunicationServer only
347 //------------------------------------------------------------------
348 DISALLOW_COPY_AND_ASSIGN (GDBRemoteCommunicationServer);
351 #endif // liblldb_GDBRemoteCommunicationServer_h_