1 //===-- GDBRemoteCommunicationServerLLGS.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_GDBRemoteCommunicationServerLLGS_h_ 11 #define liblldb_GDBRemoteCommunicationServerLLGS_h_ 12 13 // C Includes 14 // C++ Includes 15 #include <mutex> 16 #include <unordered_map> 17 18 // Other libraries and framework includes 19 #include "lldb/Core/Communication.h" 20 #include "lldb/Host/MainLoop.h" 21 #include "lldb/Host/common/NativeProcessProtocol.h" 22 #include "lldb/lldb-private-forward.h" 23 24 // Project includes 25 #include "GDBRemoteCommunicationServerCommon.h" 26 27 class StringExtractorGDBRemote; 28 29 namespace lldb_private { 30 31 namespace process_gdb_remote { 32 33 class ProcessGDBRemote; 34 35 class GDBRemoteCommunicationServerLLGS 36 : public GDBRemoteCommunicationServerCommon, 37 public NativeProcessProtocol::NativeDelegate { 38 public: 39 //------------------------------------------------------------------ 40 // Constructors and Destructors 41 //------------------------------------------------------------------ 42 GDBRemoteCommunicationServerLLGS( 43 MainLoop &mainloop, 44 const NativeProcessProtocol::Factory &process_factory); 45 46 //------------------------------------------------------------------ 47 /// Specify the program to launch and its arguments. 48 /// 49 /// @param[in] args 50 /// The command line to launch. 51 /// 52 /// @param[in] argc 53 /// The number of elements in the args array of cstring pointers. 54 /// 55 /// @return 56 /// An Status object indicating the success or failure of making 57 /// the setting. 58 //------------------------------------------------------------------ 59 Status SetLaunchArguments(const char *const args[], int argc); 60 61 //------------------------------------------------------------------ 62 /// Specify the launch flags for the process. 63 /// 64 /// @param[in] launch_flags 65 /// The launch flags to use when launching this process. 66 /// 67 /// @return 68 /// An Status object indicating the success or failure of making 69 /// the setting. 70 //------------------------------------------------------------------ 71 Status SetLaunchFlags(unsigned int launch_flags); 72 73 //------------------------------------------------------------------ 74 /// Launch a process with the current launch settings. 75 /// 76 /// This method supports running an lldb-gdbserver or similar 77 /// server in a situation where the startup code has been provided 78 /// with all the information for a child process to be launched. 79 /// 80 /// @return 81 /// An Status object indicating the success or failure of the 82 /// launch. 83 //------------------------------------------------------------------ 84 Status LaunchProcess() override; 85 86 //------------------------------------------------------------------ 87 /// Attach to a process. 88 /// 89 /// This method supports attaching llgs to a process accessible via the 90 /// configured Platform. 91 /// 92 /// @return 93 /// An Status object indicating the success or failure of the 94 /// attach operation. 95 //------------------------------------------------------------------ 96 Status AttachToProcess(lldb::pid_t pid); 97 98 //------------------------------------------------------------------ 99 // NativeProcessProtocol::NativeDelegate overrides 100 //------------------------------------------------------------------ 101 void InitializeDelegate(NativeProcessProtocol *process) override; 102 103 void ProcessStateChanged(NativeProcessProtocol *process, 104 lldb::StateType state) override; 105 106 void DidExec(NativeProcessProtocol *process) override; 107 108 Status InitializeConnection(std::unique_ptr<Connection> &&connection); 109 110 protected: 111 MainLoop &m_mainloop; 112 MainLoop::ReadHandleUP m_network_handle_up; 113 const NativeProcessProtocol::Factory &m_process_factory; 114 lldb::tid_t m_current_tid = LLDB_INVALID_THREAD_ID; 115 lldb::tid_t m_continue_tid = LLDB_INVALID_THREAD_ID; 116 std::recursive_mutex m_debugged_process_mutex; 117 std::unique_ptr<NativeProcessProtocol> m_debugged_process_up; 118 119 Communication m_stdio_communication; 120 MainLoop::ReadHandleUP m_stdio_handle_up; 121 122 lldb::StateType m_inferior_prev_state = lldb::StateType::eStateInvalid; 123 std::unique_ptr<llvm::MemoryBuffer> m_active_auxv_buffer_up; 124 std::mutex m_saved_registers_mutex; 125 std::unordered_map<uint32_t, lldb::DataBufferSP> m_saved_registers_map; 126 uint32_t m_next_saved_registers_id = 1; 127 bool m_handshake_completed = false; 128 129 PacketResult SendONotification(const char *buffer, uint32_t len); 130 131 PacketResult SendWResponse(NativeProcessProtocol *process); 132 133 PacketResult SendStopReplyPacketForThread(lldb::tid_t tid); 134 135 PacketResult SendStopReasonForState(lldb::StateType process_state); 136 137 PacketResult Handle_k(StringExtractorGDBRemote &packet); 138 139 PacketResult Handle_qProcessInfo(StringExtractorGDBRemote &packet); 140 141 PacketResult Handle_qC(StringExtractorGDBRemote &packet); 142 143 PacketResult Handle_QSetDisableASLR(StringExtractorGDBRemote &packet); 144 145 PacketResult Handle_QSetWorkingDir(StringExtractorGDBRemote &packet); 146 147 PacketResult Handle_qGetWorkingDir(StringExtractorGDBRemote &packet); 148 149 PacketResult Handle_C(StringExtractorGDBRemote &packet); 150 151 PacketResult Handle_c(StringExtractorGDBRemote &packet); 152 153 PacketResult Handle_vCont(StringExtractorGDBRemote &packet); 154 155 PacketResult Handle_vCont_actions(StringExtractorGDBRemote &packet); 156 157 PacketResult Handle_stop_reason(StringExtractorGDBRemote &packet); 158 159 PacketResult Handle_qRegisterInfo(StringExtractorGDBRemote &packet); 160 161 PacketResult Handle_qfThreadInfo(StringExtractorGDBRemote &packet); 162 163 PacketResult Handle_qsThreadInfo(StringExtractorGDBRemote &packet); 164 165 PacketResult Handle_p(StringExtractorGDBRemote &packet); 166 167 PacketResult Handle_P(StringExtractorGDBRemote &packet); 168 169 PacketResult Handle_H(StringExtractorGDBRemote &packet); 170 171 PacketResult Handle_I(StringExtractorGDBRemote &packet); 172 173 PacketResult Handle_interrupt(StringExtractorGDBRemote &packet); 174 175 // Handles $m and $x packets. 176 PacketResult Handle_memory_read(StringExtractorGDBRemote &packet); 177 178 PacketResult Handle_M(StringExtractorGDBRemote &packet); 179 180 PacketResult 181 Handle_qMemoryRegionInfoSupported(StringExtractorGDBRemote &packet); 182 183 PacketResult Handle_qMemoryRegionInfo(StringExtractorGDBRemote &packet); 184 185 PacketResult Handle_Z(StringExtractorGDBRemote &packet); 186 187 PacketResult Handle_z(StringExtractorGDBRemote &packet); 188 189 PacketResult Handle_s(StringExtractorGDBRemote &packet); 190 191 PacketResult Handle_qXfer_auxv_read(StringExtractorGDBRemote &packet); 192 193 PacketResult Handle_QSaveRegisterState(StringExtractorGDBRemote &packet); 194 195 PacketResult Handle_jTraceStart(StringExtractorGDBRemote &packet); 196 197 PacketResult Handle_jTraceRead(StringExtractorGDBRemote &packet); 198 199 PacketResult Handle_jTraceStop(StringExtractorGDBRemote &packet); 200 201 PacketResult Handle_jTraceConfigRead(StringExtractorGDBRemote &packet); 202 203 PacketResult Handle_QRestoreRegisterState(StringExtractorGDBRemote &packet); 204 205 PacketResult Handle_vAttach(StringExtractorGDBRemote &packet); 206 207 PacketResult Handle_D(StringExtractorGDBRemote &packet); 208 209 PacketResult Handle_qThreadStopInfo(StringExtractorGDBRemote &packet); 210 211 PacketResult Handle_jThreadsInfo(StringExtractorGDBRemote &packet); 212 213 PacketResult Handle_qWatchpointSupportInfo(StringExtractorGDBRemote &packet); 214 215 PacketResult Handle_qFileLoadAddress(StringExtractorGDBRemote &packet); 216 217 PacketResult Handle_QPassSignals(StringExtractorGDBRemote &packet); 218 219 void SetCurrentThreadID(lldb::tid_t tid); 220 221 lldb::tid_t GetCurrentThreadID() const; 222 223 void SetContinueThreadID(lldb::tid_t tid); 224 225 lldb::tid_t GetContinueThreadID() const { return m_continue_tid; } 226 227 Status SetSTDIOFileDescriptor(int fd); 228 229 FileSpec FindModuleFile(const std::string &module_path, 230 const ArchSpec &arch) override; 231 232 private: 233 void HandleInferiorState_Exited(NativeProcessProtocol *process); 234 235 void HandleInferiorState_Stopped(NativeProcessProtocol *process); 236 237 NativeThreadProtocol *GetThreadFromSuffix(StringExtractorGDBRemote &packet); 238 239 uint32_t GetNextSavedRegistersID(); 240 241 void MaybeCloseInferiorTerminalConnection(); 242 243 void ClearProcessSpecificData(); 244 245 void RegisterPacketHandlers(); 246 247 void DataAvailableCallback(); 248 249 void SendProcessOutput(); 250 251 void StartSTDIOForwarding(); 252 253 void StopSTDIOForwarding(); 254 255 //------------------------------------------------------------------ 256 // For GDBRemoteCommunicationServerLLGS only 257 //------------------------------------------------------------------ 258 DISALLOW_COPY_AND_ASSIGN(GDBRemoteCommunicationServerLLGS); 259 }; 260 261 } // namespace process_gdb_remote 262 } // namespace lldb_private 263 264 #endif // liblldb_GDBRemoteCommunicationServerLLGS_h_ 265