1 //===-- 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 15 #include <vector> 16 #include <set> 17 // Other libraries and framework includes 18 // Project includes 19 #include "lldb/Host/Mutex.h" 20 #include "lldb/Target/Process.h" 21 #include "GDBRemoteCommunication.h" 22 23 class ProcessGDBRemote; 24 class StringExtractorGDBRemote; 25 26 class GDBRemoteCommunicationServer : public GDBRemoteCommunication 27 { 28 public: 29 typedef std::map<uint16_t, lldb::pid_t> PortMap; 30 31 enum 32 { 33 eBroadcastBitRunPacketSent = kLoUserBroadcastBit 34 }; 35 //------------------------------------------------------------------ 36 // Constructors and Destructors 37 //------------------------------------------------------------------ 38 GDBRemoteCommunicationServer(bool is_platform); 39 40 GDBRemoteCommunicationServer(bool is_platform, 41 const lldb::PlatformSP& platform_sp); 42 43 virtual 44 ~GDBRemoteCommunicationServer(); 45 46 bool 47 GetPacketAndSendResponse (uint32_t timeout_usec, 48 lldb_private::Error &error, 49 bool &interrupt, 50 bool &quit); 51 52 virtual bool GetThreadSuffixSupported()53 GetThreadSuffixSupported () 54 { 55 return true; 56 } 57 58 // 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); 62 63 // Set both ports to zero to let the platform automatically bind to 64 // a port chosen by the OS. 65 void SetPortMap(PortMap && port_map)66 SetPortMap (PortMap &&port_map) 67 { 68 m_port_map = port_map; 69 } 70 71 //---------------------------------------------------------------------- 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 //---------------------------------------------------------------------- 80 uint16_t GetNextAvailablePort()81 GetNextAvailablePort () 82 { 83 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 AssociatePortWithProcess(uint16_t port,lldb::pid_t pid)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 FreePort(uint16_t port)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 FreePortForProcess(lldb::pid_t pid)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; 136 } 137 138 void SetPortOffset(uint16_t port_offset)139 SetPortOffset (uint16_t port_offset) 140 { 141 m_port_offset = port_offset; 142 } 143 144 //------------------------------------------------------------------ 145 /// Specify the program to launch and its arguments. 146 /// 147 /// The LaunchProcess () command can be executed to do the lauching. 148 /// 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 /// 155 /// @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 /// 167 /// @param[in] launch_flags 168 /// The launch flags to use when launching this process. 169 /// 170 /// @return 171 /// 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 185 /// An Error object indicating the success or failure of the 186 /// launch. 187 //------------------------------------------------------------------ 188 lldb_private::Error 189 LaunchProcess (); 190 191 protected: 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; 200 PortMap m_port_map; 201 uint16_t m_port_offset; 202 203 204 PacketResult 205 SendUnimplementedResponse (const char *packet); 206 207 PacketResult 208 SendErrorResponse (uint8_t error); 209 210 PacketResult 211 SendOKResponse (); 212 213 PacketResult 214 Handle_A (StringExtractorGDBRemote &packet); 215 216 PacketResult 217 Handle_qLaunchSuccess (StringExtractorGDBRemote &packet); 218 219 PacketResult 220 Handle_qHostInfo (StringExtractorGDBRemote &packet); 221 222 PacketResult 223 Handle_qLaunchGDBServer (StringExtractorGDBRemote &packet); 224 225 PacketResult 226 Handle_qKillSpawnedProcess (StringExtractorGDBRemote &packet); 227 228 PacketResult 229 Handle_k (StringExtractorGDBRemote &packet); 230 231 PacketResult 232 Handle_qPlatform_mkdir (StringExtractorGDBRemote &packet); 233 234 PacketResult 235 Handle_qPlatform_chmod (StringExtractorGDBRemote &packet); 236 237 PacketResult 238 Handle_qProcessInfoPID (StringExtractorGDBRemote &packet); 239 240 PacketResult 241 Handle_qfProcessInfo (StringExtractorGDBRemote &packet); 242 243 PacketResult 244 Handle_qsProcessInfo (StringExtractorGDBRemote &packet); 245 246 PacketResult 247 Handle_qC (StringExtractorGDBRemote &packet); 248 249 PacketResult 250 Handle_qUserName (StringExtractorGDBRemote &packet); 251 252 PacketResult 253 Handle_qGroupName (StringExtractorGDBRemote &packet); 254 255 PacketResult 256 Handle_qSpeedTest (StringExtractorGDBRemote &packet); 257 258 PacketResult 259 Handle_QEnvironment (StringExtractorGDBRemote &packet); 260 261 PacketResult 262 Handle_QLaunchArch (StringExtractorGDBRemote &packet); 263 264 PacketResult 265 Handle_QSetDisableASLR (StringExtractorGDBRemote &packet); 266 267 PacketResult 268 Handle_QSetWorkingDir (StringExtractorGDBRemote &packet); 269 270 PacketResult 271 Handle_qGetWorkingDir (StringExtractorGDBRemote &packet); 272 273 PacketResult 274 Handle_QStartNoAckMode (StringExtractorGDBRemote &packet); 275 276 PacketResult 277 Handle_QSetSTDIN (StringExtractorGDBRemote &packet); 278 279 PacketResult 280 Handle_QSetSTDOUT (StringExtractorGDBRemote &packet); 281 282 PacketResult 283 Handle_QSetSTDERR (StringExtractorGDBRemote &packet); 284 285 PacketResult 286 Handle_vFile_Open (StringExtractorGDBRemote &packet); 287 288 PacketResult 289 Handle_vFile_Close (StringExtractorGDBRemote &packet); 290 291 PacketResult 292 Handle_vFile_pRead (StringExtractorGDBRemote &packet); 293 294 PacketResult 295 Handle_vFile_pWrite (StringExtractorGDBRemote &packet); 296 297 PacketResult 298 Handle_vFile_Size (StringExtractorGDBRemote &packet); 299 300 PacketResult 301 Handle_vFile_Mode (StringExtractorGDBRemote &packet); 302 303 PacketResult 304 Handle_vFile_Exists (StringExtractorGDBRemote &packet); 305 306 PacketResult 307 Handle_vFile_symlink (StringExtractorGDBRemote &packet); 308 309 PacketResult 310 Handle_vFile_unlink (StringExtractorGDBRemote &packet); 311 312 PacketResult 313 Handle_vFile_Stat (StringExtractorGDBRemote &packet); 314 315 PacketResult 316 Handle_vFile_MD5 (StringExtractorGDBRemote &packet); 317 318 PacketResult 319 Handle_qPlatform_shell (StringExtractorGDBRemote &packet); 320 321 private: 322 bool 323 DebugserverProcessReaped (lldb::pid_t pid); 324 325 static bool 326 ReapDebugserverProcess (void *callback_baton, 327 lldb::pid_t pid, 328 bool exited, 329 int signal, 330 int status); 331 332 bool 333 DebuggedProcessReaped (lldb::pid_t pid); 334 335 static bool 336 ReapDebuggedProcess (void *callback_baton, 337 lldb::pid_t pid, 338 bool exited, 339 int signal, 340 int status); 341 342 bool 343 KillSpawnedProcess (lldb::pid_t pid); 344 345 //------------------------------------------------------------------ 346 // For GDBRemoteCommunicationServer only 347 //------------------------------------------------------------------ 348 DISALLOW_COPY_AND_ASSIGN (GDBRemoteCommunicationServer); 349 }; 350 351 #endif // liblldb_GDBRemoteCommunicationServer_h_ 352