1 //===-- GDBRemoteCommunicationServerLLGS.h ----------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef LLDB_SOURCE_PLUGINS_PROCESS_GDB_REMOTE_GDBREMOTECOMMUNICATIONSERVERLLGS_H 10 #define LLDB_SOURCE_PLUGINS_PROCESS_GDB_REMOTE_GDBREMOTECOMMUNICATIONSERVERLLGS_H 11 12 #include <mutex> 13 #include <unordered_map> 14 15 #include "lldb/Core/Communication.h" 16 #include "lldb/Host/MainLoop.h" 17 #include "lldb/Host/common/NativeProcessProtocol.h" 18 #include "lldb/lldb-private-forward.h" 19 20 #include "GDBRemoteCommunicationServerCommon.h" 21 22 class StringExtractorGDBRemote; 23 24 namespace lldb_private { 25 26 namespace process_gdb_remote { 27 28 class ProcessGDBRemote; 29 30 class GDBRemoteCommunicationServerLLGS 31 : public GDBRemoteCommunicationServerCommon, 32 public NativeProcessProtocol::NativeDelegate { 33 public: 34 // Constructors and Destructors 35 GDBRemoteCommunicationServerLLGS( 36 MainLoop &mainloop, 37 const NativeProcessProtocol::Factory &process_factory); 38 39 void SetLaunchInfo(const ProcessLaunchInfo &info); 40 41 /// Launch a process with the current launch settings. 42 /// 43 /// This method supports running an lldb-gdbserver or similar 44 /// server in a situation where the startup code has been provided 45 /// with all the information for a child process to be launched. 46 /// 47 /// \return 48 /// An Status object indicating the success or failure of the 49 /// launch. 50 Status LaunchProcess() override; 51 52 /// Attach to a process. 53 /// 54 /// This method supports attaching llgs to a process accessible via the 55 /// configured Platform. 56 /// 57 /// \return 58 /// An Status object indicating the success or failure of the 59 /// attach operation. 60 Status AttachToProcess(lldb::pid_t pid); 61 62 /// Wait to attach to a process with a given name. 63 /// 64 /// This method supports waiting for the next instance of a process 65 /// with a given name and attaching llgs to that via the configured 66 /// Platform. 67 /// 68 /// \return 69 /// An Status object indicating the success or failure of the 70 /// attach operation. 71 Status AttachWaitProcess(llvm::StringRef process_name, bool include_existing); 72 73 // NativeProcessProtocol::NativeDelegate overrides 74 void InitializeDelegate(NativeProcessProtocol *process) override; 75 76 void ProcessStateChanged(NativeProcessProtocol *process, 77 lldb::StateType state) override; 78 79 void DidExec(NativeProcessProtocol *process) override; 80 81 Status InitializeConnection(std::unique_ptr<Connection> connection); 82 83 protected: 84 MainLoop &m_mainloop; 85 MainLoop::ReadHandleUP m_network_handle_up; 86 const NativeProcessProtocol::Factory &m_process_factory; 87 lldb::tid_t m_current_tid = LLDB_INVALID_THREAD_ID; 88 lldb::tid_t m_continue_tid = LLDB_INVALID_THREAD_ID; 89 NativeProcessProtocol *m_current_process; 90 NativeProcessProtocol *m_continue_process; 91 std::recursive_mutex m_debugged_process_mutex; 92 std::unique_ptr<NativeProcessProtocol> m_debugged_process_up; 93 94 Communication m_stdio_communication; 95 MainLoop::ReadHandleUP m_stdio_handle_up; 96 97 lldb::StateType m_inferior_prev_state = lldb::StateType::eStateInvalid; 98 llvm::StringMap<std::unique_ptr<llvm::MemoryBuffer>> m_xfer_buffer_map; 99 std::mutex m_saved_registers_mutex; 100 std::unordered_map<uint32_t, lldb::DataBufferSP> m_saved_registers_map; 101 uint32_t m_next_saved_registers_id = 1; 102 bool m_handshake_completed = false; 103 bool m_thread_suffix_supported = false; 104 bool m_list_threads_in_stop_reply = false; 105 106 PacketResult SendONotification(const char *buffer, uint32_t len); 107 108 PacketResult SendWResponse(NativeProcessProtocol *process); 109 110 PacketResult SendStopReplyPacketForThread(lldb::tid_t tid); 111 112 PacketResult SendStopReasonForState(lldb::StateType process_state); 113 114 PacketResult Handle_k(StringExtractorGDBRemote &packet); 115 116 PacketResult Handle_qProcessInfo(StringExtractorGDBRemote &packet); 117 118 PacketResult Handle_qC(StringExtractorGDBRemote &packet); 119 120 PacketResult Handle_QSetDisableASLR(StringExtractorGDBRemote &packet); 121 122 PacketResult Handle_QSetWorkingDir(StringExtractorGDBRemote &packet); 123 124 PacketResult Handle_qGetWorkingDir(StringExtractorGDBRemote &packet); 125 126 PacketResult Handle_QThreadSuffixSupported(StringExtractorGDBRemote &packet); 127 128 PacketResult Handle_QListThreadsInStopReply(StringExtractorGDBRemote &packet); 129 130 PacketResult Handle_C(StringExtractorGDBRemote &packet); 131 132 PacketResult Handle_c(StringExtractorGDBRemote &packet); 133 134 PacketResult Handle_vCont(StringExtractorGDBRemote &packet); 135 136 PacketResult Handle_vCont_actions(StringExtractorGDBRemote &packet); 137 138 PacketResult Handle_stop_reason(StringExtractorGDBRemote &packet); 139 140 PacketResult Handle_qRegisterInfo(StringExtractorGDBRemote &packet); 141 142 PacketResult Handle_qfThreadInfo(StringExtractorGDBRemote &packet); 143 144 PacketResult Handle_qsThreadInfo(StringExtractorGDBRemote &packet); 145 146 PacketResult Handle_p(StringExtractorGDBRemote &packet); 147 148 PacketResult Handle_P(StringExtractorGDBRemote &packet); 149 150 PacketResult Handle_H(StringExtractorGDBRemote &packet); 151 152 PacketResult Handle_I(StringExtractorGDBRemote &packet); 153 154 PacketResult Handle_interrupt(StringExtractorGDBRemote &packet); 155 156 // Handles $m and $x packets. 157 PacketResult Handle_memory_read(StringExtractorGDBRemote &packet); 158 159 PacketResult Handle_M(StringExtractorGDBRemote &packet); 160 PacketResult Handle__M(StringExtractorGDBRemote &packet); 161 PacketResult Handle__m(StringExtractorGDBRemote &packet); 162 163 PacketResult 164 Handle_qMemoryRegionInfoSupported(StringExtractorGDBRemote &packet); 165 166 PacketResult Handle_qMemoryRegionInfo(StringExtractorGDBRemote &packet); 167 168 PacketResult Handle_Z(StringExtractorGDBRemote &packet); 169 170 PacketResult Handle_z(StringExtractorGDBRemote &packet); 171 172 PacketResult Handle_s(StringExtractorGDBRemote &packet); 173 174 PacketResult Handle_qXfer(StringExtractorGDBRemote &packet); 175 176 PacketResult Handle_QSaveRegisterState(StringExtractorGDBRemote &packet); 177 178 PacketResult Handle_jLLDBTraceSupported(StringExtractorGDBRemote &packet); 179 180 PacketResult Handle_jLLDBTraceStart(StringExtractorGDBRemote &packet); 181 182 PacketResult Handle_jLLDBTraceStop(StringExtractorGDBRemote &packet); 183 184 PacketResult Handle_jLLDBTraceGetState(StringExtractorGDBRemote &packet); 185 186 PacketResult Handle_jLLDBTraceGetBinaryData(StringExtractorGDBRemote &packet); 187 188 PacketResult Handle_QRestoreRegisterState(StringExtractorGDBRemote &packet); 189 190 PacketResult Handle_vAttach(StringExtractorGDBRemote &packet); 191 192 PacketResult Handle_vAttachWait(StringExtractorGDBRemote &packet); 193 194 PacketResult Handle_qVAttachOrWaitSupported(StringExtractorGDBRemote &packet); 195 196 PacketResult Handle_vAttachOrWait(StringExtractorGDBRemote &packet); 197 198 PacketResult Handle_D(StringExtractorGDBRemote &packet); 199 200 PacketResult Handle_qThreadStopInfo(StringExtractorGDBRemote &packet); 201 202 PacketResult Handle_jThreadsInfo(StringExtractorGDBRemote &packet); 203 204 PacketResult Handle_qWatchpointSupportInfo(StringExtractorGDBRemote &packet); 205 206 PacketResult Handle_qFileLoadAddress(StringExtractorGDBRemote &packet); 207 208 PacketResult Handle_QPassSignals(StringExtractorGDBRemote &packet); 209 210 PacketResult Handle_g(StringExtractorGDBRemote &packet); 211 212 void SetCurrentThreadID(lldb::tid_t tid); 213 214 lldb::tid_t GetCurrentThreadID() const; 215 216 void SetContinueThreadID(lldb::tid_t tid); 217 218 lldb::tid_t GetContinueThreadID() const { return m_continue_tid; } 219 220 Status SetSTDIOFileDescriptor(int fd); 221 222 FileSpec FindModuleFile(const std::string &module_path, 223 const ArchSpec &arch) override; 224 225 llvm::Expected<std::unique_ptr<llvm::MemoryBuffer>> 226 ReadXferObject(llvm::StringRef object, llvm::StringRef annex); 227 228 static std::string XMLEncodeAttributeValue(llvm::StringRef value); 229 230 virtual std::vector<std::string> HandleFeatures( 231 const llvm::ArrayRef<llvm::StringRef> client_features) override; 232 233 private: 234 llvm::Expected<std::unique_ptr<llvm::MemoryBuffer>> BuildTargetXml(); 235 236 void HandleInferiorState_Exited(NativeProcessProtocol *process); 237 238 void HandleInferiorState_Stopped(NativeProcessProtocol *process); 239 240 NativeThreadProtocol *GetThreadFromSuffix(StringExtractorGDBRemote &packet); 241 242 uint32_t GetNextSavedRegistersID(); 243 244 void MaybeCloseInferiorTerminalConnection(); 245 246 void ClearProcessSpecificData(); 247 248 void RegisterPacketHandlers(); 249 250 void DataAvailableCallback(); 251 252 void SendProcessOutput(); 253 254 void StartSTDIOForwarding(); 255 256 void StopSTDIOForwarding(); 257 258 // Read thread-id from packet. If the thread-id is correct, returns it. 259 // Otherwise, returns the error. 260 // 261 // If allow_all is true, then the pid/tid value of -1 ('all') will be allowed. 262 // In any case, the function assumes that exactly one inferior is being 263 // debugged and rejects pid values that do no match that inferior. 264 llvm::Expected<lldb::tid_t> ReadTid(StringExtractorGDBRemote &packet, 265 bool allow_all, lldb::pid_t default_pid); 266 267 // For GDBRemoteCommunicationServerLLGS only 268 GDBRemoteCommunicationServerLLGS(const GDBRemoteCommunicationServerLLGS &) = 269 delete; 270 const GDBRemoteCommunicationServerLLGS & 271 operator=(const GDBRemoteCommunicationServerLLGS &) = delete; 272 }; 273 274 } // namespace process_gdb_remote 275 } // namespace lldb_private 276 277 #endif // LLDB_SOURCE_PLUGINS_PROCESS_GDB_REMOTE_GDBREMOTECOMMUNICATIONSERVERLLGS_H 278