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 #include <unordered_map> 18 // Other libraries and framework includes 19 // Project includes 20 #include "lldb/lldb-private-forward.h" 21 #include "lldb/Core/Communication.h" 22 #include "lldb/Host/Mutex.h" 23 #include "lldb/Target/Process.h" 24 #include "GDBRemoteCommunication.h" 25 26 #include "../../../Host/common/NativeProcessProtocol.h" 27 28 class ProcessGDBRemote; 29 class StringExtractorGDBRemote; 30 31 class GDBRemoteCommunicationServer : 32 public GDBRemoteCommunication, 33 public lldb_private::NativeProcessProtocol::NativeDelegate 34 { 35 public: 36 typedef std::map<uint16_t, lldb::pid_t> PortMap; 37 38 enum 39 { 40 eBroadcastBitRunPacketSent = kLoUserBroadcastBit 41 }; 42 //------------------------------------------------------------------ 43 // Constructors and Destructors 44 //------------------------------------------------------------------ 45 GDBRemoteCommunicationServer(bool is_platform); 46 47 GDBRemoteCommunicationServer(bool is_platform, 48 const lldb::PlatformSP& platform_sp, 49 lldb::DebuggerSP& debugger_sp); 50 51 virtual 52 ~GDBRemoteCommunicationServer(); 53 54 PacketResult 55 GetPacketAndSendResponse (uint32_t timeout_usec, 56 lldb_private::Error &error, 57 bool &interrupt, 58 bool &quit); 59 60 virtual bool 61 GetThreadSuffixSupported () 62 { 63 return true; 64 } 65 66 // After connecting, do a little handshake with the client to make sure 67 // we are at least communicating 68 bool 69 HandshakeWithClient (lldb_private::Error *error_ptr); 70 71 // Set both ports to zero to let the platform automatically bind to 72 // a port chosen by the OS. 73 void 74 SetPortMap (PortMap &&port_map) 75 { 76 m_port_map = port_map; 77 } 78 79 //---------------------------------------------------------------------- 80 // If we are using a port map where we can only use certain ports, 81 // get the next available port. 82 // 83 // If we are using a port map and we are out of ports, return UINT16_MAX 84 // 85 // If we aren't using a port map, return 0 to indicate we should bind to 86 // port 0 and then figure out which port we used. 87 //---------------------------------------------------------------------- 88 uint16_t 89 GetNextAvailablePort () 90 { 91 if (m_port_map.empty()) 92 return 0; // Bind to port zero and get a port, we didn't have any limitations 93 94 for (auto &pair : m_port_map) 95 { 96 if (pair.second == LLDB_INVALID_PROCESS_ID) 97 { 98 pair.second = ~(lldb::pid_t)LLDB_INVALID_PROCESS_ID; 99 return pair.first; 100 } 101 } 102 return UINT16_MAX; 103 } 104 105 bool 106 AssociatePortWithProcess (uint16_t port, lldb::pid_t pid) 107 { 108 PortMap::iterator pos = m_port_map.find(port); 109 if (pos != m_port_map.end()) 110 { 111 pos->second = pid; 112 return true; 113 } 114 return false; 115 } 116 117 bool 118 FreePort (uint16_t port) 119 { 120 PortMap::iterator pos = m_port_map.find(port); 121 if (pos != m_port_map.end()) 122 { 123 pos->second = LLDB_INVALID_PROCESS_ID; 124 return true; 125 } 126 return false; 127 } 128 129 bool 130 FreePortForProcess (lldb::pid_t pid) 131 { 132 if (!m_port_map.empty()) 133 { 134 for (auto &pair : m_port_map) 135 { 136 if (pair.second == pid) 137 { 138 pair.second = LLDB_INVALID_PROCESS_ID; 139 return true; 140 } 141 } 142 } 143 return false; 144 } 145 146 void 147 SetPortOffset (uint16_t port_offset) 148 { 149 m_port_offset = port_offset; 150 } 151 152 //------------------------------------------------------------------ 153 /// Specify the program to launch and its arguments. 154 /// 155 /// The LaunchProcess () command can be executed to do the lauching. 156 /// 157 /// @param[in] args 158 /// The command line to launch. 159 /// 160 /// @param[in] argc 161 /// The number of elements in the args array of cstring pointers. 162 /// 163 /// @return 164 /// An Error object indicating the success or failure of making 165 /// the setting. 166 //------------------------------------------------------------------ 167 lldb_private::Error 168 SetLaunchArguments (const char *const args[], int argc); 169 170 //------------------------------------------------------------------ 171 /// Specify the launch flags for the process. 172 /// 173 /// The LaunchProcess () command can be executed to do the lauching. 174 /// 175 /// @param[in] launch_flags 176 /// The launch flags to use when launching this process. 177 /// 178 /// @return 179 /// An Error object indicating the success or failure of making 180 /// the setting. 181 //------------------------------------------------------------------ 182 lldb_private::Error 183 SetLaunchFlags (unsigned int launch_flags); 184 185 //------------------------------------------------------------------ 186 /// Launch a process with the current launch settings. 187 /// 188 /// This method supports running an lldb-gdbserver or similar 189 /// server in a situation where the startup code has been provided 190 /// with all the information for a child process to be launched. 191 /// 192 /// @return 193 /// An Error object indicating the success or failure of the 194 /// launch. 195 //------------------------------------------------------------------ 196 lldb_private::Error 197 LaunchProcess (); 198 199 //------------------------------------------------------------------ 200 /// Attach to a process. 201 /// 202 /// This method supports attaching llgs to a process accessible via the 203 /// configured Platform. 204 /// 205 /// @return 206 /// An Error object indicating the success or failure of the 207 /// attach operation. 208 //------------------------------------------------------------------ 209 lldb_private::Error 210 AttachToProcess (lldb::pid_t pid); 211 212 //------------------------------------------------------------------ 213 // NativeProcessProtocol::NativeDelegate overrides 214 //------------------------------------------------------------------ 215 void 216 InitializeDelegate (lldb_private::NativeProcessProtocol *process) override; 217 218 void 219 ProcessStateChanged (lldb_private::NativeProcessProtocol *process, lldb::StateType state) override; 220 221 protected: 222 lldb::PlatformSP m_platform_sp; 223 lldb::thread_t m_async_thread; 224 lldb_private::ProcessLaunchInfo m_process_launch_info; 225 lldb_private::Error m_process_launch_error; 226 std::set<lldb::pid_t> m_spawned_pids; 227 lldb_private::Mutex m_spawned_pids_mutex; 228 lldb_private::ProcessInstanceInfoList m_proc_infos; 229 uint32_t m_proc_infos_index; 230 PortMap m_port_map; 231 uint16_t m_port_offset; 232 lldb::tid_t m_current_tid; 233 lldb::tid_t m_continue_tid; 234 lldb_private::Mutex m_debugged_process_mutex; 235 lldb_private::NativeProcessProtocolSP m_debugged_process_sp; 236 lldb::DebuggerSP m_debugger_sp; 237 Communication m_stdio_communication; 238 bool m_exit_now; // use in asynchronous handling to indicate process should exit. 239 lldb::StateType m_inferior_prev_state; 240 bool m_thread_suffix_supported; 241 bool m_list_threads_in_stop_reply; 242 lldb::DataBufferSP m_active_auxv_buffer_sp; 243 lldb_private::Mutex m_saved_registers_mutex; 244 std::unordered_map<uint32_t, lldb::DataBufferSP> m_saved_registers_map; 245 uint32_t m_next_saved_registers_id; 246 247 PacketResult 248 SendUnimplementedResponse (const char *packet); 249 250 PacketResult 251 SendErrorResponse (uint8_t error); 252 253 PacketResult 254 SendIllFormedResponse (const StringExtractorGDBRemote &packet, const char *error_message); 255 256 PacketResult 257 SendOKResponse (); 258 259 PacketResult 260 SendONotification (const char *buffer, uint32_t len); 261 262 PacketResult 263 SendWResponse (lldb_private::NativeProcessProtocol *process); 264 265 PacketResult 266 SendStopReplyPacketForThread (lldb::tid_t tid); 267 268 PacketResult 269 SendStopReasonForState (lldb::StateType process_state, bool flush_on_exit); 270 271 PacketResult 272 Handle_A (StringExtractorGDBRemote &packet); 273 274 PacketResult 275 Handle_qLaunchSuccess (StringExtractorGDBRemote &packet); 276 277 PacketResult 278 Handle_qHostInfo (StringExtractorGDBRemote &packet); 279 280 PacketResult 281 Handle_qLaunchGDBServer (StringExtractorGDBRemote &packet); 282 283 PacketResult 284 Handle_qKillSpawnedProcess (StringExtractorGDBRemote &packet); 285 286 PacketResult 287 Handle_k (StringExtractorGDBRemote &packet); 288 289 PacketResult 290 Handle_qPlatform_mkdir (StringExtractorGDBRemote &packet); 291 292 PacketResult 293 Handle_qPlatform_chmod (StringExtractorGDBRemote &packet); 294 295 PacketResult 296 Handle_qProcessInfo (StringExtractorGDBRemote &packet); 297 298 PacketResult 299 Handle_qProcessInfoPID (StringExtractorGDBRemote &packet); 300 301 PacketResult 302 Handle_qfProcessInfo (StringExtractorGDBRemote &packet); 303 304 PacketResult 305 Handle_qsProcessInfo (StringExtractorGDBRemote &packet); 306 307 PacketResult 308 Handle_qC (StringExtractorGDBRemote &packet); 309 310 PacketResult 311 Handle_qUserName (StringExtractorGDBRemote &packet); 312 313 PacketResult 314 Handle_qGroupName (StringExtractorGDBRemote &packet); 315 316 PacketResult 317 Handle_qSpeedTest (StringExtractorGDBRemote &packet); 318 319 PacketResult 320 Handle_QEnvironment (StringExtractorGDBRemote &packet); 321 322 PacketResult 323 Handle_QLaunchArch (StringExtractorGDBRemote &packet); 324 325 PacketResult 326 Handle_QSetDisableASLR (StringExtractorGDBRemote &packet); 327 328 PacketResult 329 Handle_QSetDetachOnError (StringExtractorGDBRemote &packet); 330 331 PacketResult 332 Handle_QSetWorkingDir (StringExtractorGDBRemote &packet); 333 334 PacketResult 335 Handle_qGetWorkingDir (StringExtractorGDBRemote &packet); 336 337 PacketResult 338 Handle_QStartNoAckMode (StringExtractorGDBRemote &packet); 339 340 PacketResult 341 Handle_QSetSTDIN (StringExtractorGDBRemote &packet); 342 343 PacketResult 344 Handle_QSetSTDOUT (StringExtractorGDBRemote &packet); 345 346 PacketResult 347 Handle_QSetSTDERR (StringExtractorGDBRemote &packet); 348 349 PacketResult 350 Handle_C (StringExtractorGDBRemote &packet); 351 352 PacketResult 353 Handle_c (StringExtractorGDBRemote &packet, bool skip_file_pos_adjustment = false); 354 355 PacketResult 356 Handle_vCont (StringExtractorGDBRemote &packet); 357 358 PacketResult 359 Handle_vCont_actions (StringExtractorGDBRemote &packet); 360 361 PacketResult 362 Handle_stop_reason (StringExtractorGDBRemote &packet); 363 364 PacketResult 365 Handle_vFile_Open (StringExtractorGDBRemote &packet); 366 367 PacketResult 368 Handle_vFile_Close (StringExtractorGDBRemote &packet); 369 370 PacketResult 371 Handle_vFile_pRead (StringExtractorGDBRemote &packet); 372 373 PacketResult 374 Handle_vFile_pWrite (StringExtractorGDBRemote &packet); 375 376 PacketResult 377 Handle_vFile_Size (StringExtractorGDBRemote &packet); 378 379 PacketResult 380 Handle_vFile_Mode (StringExtractorGDBRemote &packet); 381 382 PacketResult 383 Handle_vFile_Exists (StringExtractorGDBRemote &packet); 384 385 PacketResult 386 Handle_vFile_symlink (StringExtractorGDBRemote &packet); 387 388 PacketResult 389 Handle_vFile_unlink (StringExtractorGDBRemote &packet); 390 391 PacketResult 392 Handle_vFile_Stat (StringExtractorGDBRemote &packet); 393 394 PacketResult 395 Handle_vFile_MD5 (StringExtractorGDBRemote &packet); 396 397 PacketResult 398 Handle_qPlatform_shell (StringExtractorGDBRemote &packet); 399 400 PacketResult 401 Handle_qRegisterInfo (StringExtractorGDBRemote &packet); 402 403 PacketResult 404 Handle_qfThreadInfo (StringExtractorGDBRemote &packet); 405 406 PacketResult 407 Handle_qsThreadInfo (StringExtractorGDBRemote &packet); 408 409 PacketResult 410 Handle_p (StringExtractorGDBRemote &packet); 411 412 PacketResult 413 Handle_P (StringExtractorGDBRemote &packet); 414 415 PacketResult 416 Handle_H (StringExtractorGDBRemote &packet); 417 418 PacketResult 419 Handle_interrupt (StringExtractorGDBRemote &packet); 420 421 PacketResult 422 Handle_m (StringExtractorGDBRemote &packet); 423 424 PacketResult 425 Handle_M (StringExtractorGDBRemote &packet); 426 427 PacketResult 428 Handle_qMemoryRegionInfoSupported (StringExtractorGDBRemote &packet); 429 430 PacketResult 431 Handle_qMemoryRegionInfo (StringExtractorGDBRemote &packet); 432 433 PacketResult 434 Handle_Z (StringExtractorGDBRemote &packet); 435 436 PacketResult 437 Handle_z (StringExtractorGDBRemote &packet); 438 439 PacketResult 440 Handle_s (StringExtractorGDBRemote &packet); 441 442 PacketResult 443 Handle_qSupported (StringExtractorGDBRemote &packet); 444 445 PacketResult 446 Handle_QThreadSuffixSupported (StringExtractorGDBRemote &packet); 447 448 PacketResult 449 Handle_QListThreadsInStopReply (StringExtractorGDBRemote &packet); 450 451 PacketResult 452 Handle_qXfer_auxv_read (StringExtractorGDBRemote &packet); 453 454 PacketResult 455 Handle_QSaveRegisterState (StringExtractorGDBRemote &packet); 456 457 PacketResult 458 Handle_QRestoreRegisterState (StringExtractorGDBRemote &packet); 459 460 PacketResult 461 Handle_vAttach (StringExtractorGDBRemote &packet); 462 463 void 464 SetCurrentThreadID (lldb::tid_t tid); 465 466 lldb::tid_t 467 GetCurrentThreadID () const; 468 469 void 470 SetContinueThreadID (lldb::tid_t tid); 471 472 lldb::tid_t 473 GetContinueThreadID () const { return m_continue_tid; } 474 475 lldb_private::Error 476 SetSTDIOFileDescriptor (int fd); 477 478 static void 479 STDIOReadThreadBytesReceived (void *baton, const void *src, size_t src_len); 480 481 private: 482 bool 483 DebugserverProcessReaped (lldb::pid_t pid); 484 485 static bool 486 ReapDebugserverProcess (void *callback_baton, 487 lldb::pid_t pid, 488 bool exited, 489 int signal, 490 int status); 491 492 bool 493 DebuggedProcessReaped (lldb::pid_t pid); 494 495 static bool 496 ReapDebuggedProcess (void *callback_baton, 497 lldb::pid_t pid, 498 bool exited, 499 int signal, 500 int status); 501 502 bool 503 KillSpawnedProcess (lldb::pid_t pid); 504 505 bool 506 IsGdbServer () 507 { 508 return !m_is_platform; 509 } 510 511 /// Launch a process from lldb-gdbserver 512 lldb_private::Error 513 LaunchDebugServerProcess (); 514 515 /// Launch a process from lldb-platform 516 lldb_private::Error 517 LaunchPlatformProcess (); 518 519 void 520 HandleInferiorState_Exited (lldb_private::NativeProcessProtocol *process); 521 522 void 523 HandleInferiorState_Stopped (lldb_private::NativeProcessProtocol *process); 524 525 void 526 FlushInferiorOutput (); 527 528 lldb_private::NativeThreadProtocolSP 529 GetThreadFromSuffix (StringExtractorGDBRemote &packet); 530 531 uint32_t 532 GetNextSavedRegistersID (); 533 534 void 535 MaybeCloseInferiorTerminalConnection (); 536 537 //------------------------------------------------------------------ 538 // For GDBRemoteCommunicationServer only 539 //------------------------------------------------------------------ 540 DISALLOW_COPY_AND_ASSIGN (GDBRemoteCommunicationServer); 541 }; 542 543 #endif // liblldb_GDBRemoteCommunicationServer_h_ 544