1 //===-- GDBRemoteCommunicationServerLLGS.h ----------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef liblldb_GDBRemoteCommunicationServerLLGS_h_
10 #define liblldb_GDBRemoteCommunicationServerLLGS_h_
13 #include <unordered_map>
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"
20 #include "GDBRemoteCommunicationServerCommon.h"
22 class StringExtractorGDBRemote;
24 namespace lldb_private {
26 namespace process_gdb_remote {
28 class ProcessGDBRemote;
30 class GDBRemoteCommunicationServerLLGS
31 : public GDBRemoteCommunicationServerCommon,
32 public NativeProcessProtocol::NativeDelegate {
34 // Constructors and Destructors
35 GDBRemoteCommunicationServerLLGS(
37 const NativeProcessProtocol::Factory &process_factory);
39 void SetLaunchInfo(const ProcessLaunchInfo &info);
41 /// Launch a process with the current launch settings.
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.
48 /// An Status object indicating the success or failure of the
50 Status LaunchProcess() override;
52 /// Attach to a process.
54 /// This method supports attaching llgs to a process accessible via the
55 /// configured Platform.
58 /// An Status object indicating the success or failure of the
60 Status AttachToProcess(lldb::pid_t pid);
62 // NativeProcessProtocol::NativeDelegate overrides
63 void InitializeDelegate(NativeProcessProtocol *process) override;
65 void ProcessStateChanged(NativeProcessProtocol *process,
66 lldb::StateType state) override;
68 void DidExec(NativeProcessProtocol *process) override;
70 Status InitializeConnection(std::unique_ptr<Connection> &&connection);
74 MainLoop::ReadHandleUP m_network_handle_up;
75 const NativeProcessProtocol::Factory &m_process_factory;
76 lldb::tid_t m_current_tid = LLDB_INVALID_THREAD_ID;
77 lldb::tid_t m_continue_tid = LLDB_INVALID_THREAD_ID;
78 std::recursive_mutex m_debugged_process_mutex;
79 std::unique_ptr<NativeProcessProtocol> m_debugged_process_up;
81 Communication m_stdio_communication;
82 MainLoop::ReadHandleUP m_stdio_handle_up;
84 lldb::StateType m_inferior_prev_state = lldb::StateType::eStateInvalid;
85 llvm::StringMap<std::unique_ptr<llvm::MemoryBuffer>> m_xfer_buffer_map;
86 std::mutex m_saved_registers_mutex;
87 std::unordered_map<uint32_t, lldb::DataBufferSP> m_saved_registers_map;
88 uint32_t m_next_saved_registers_id = 1;
89 bool m_handshake_completed = false;
91 PacketResult SendONotification(const char *buffer, uint32_t len);
93 PacketResult SendWResponse(NativeProcessProtocol *process);
95 PacketResult SendStopReplyPacketForThread(lldb::tid_t tid);
97 PacketResult SendStopReasonForState(lldb::StateType process_state);
99 PacketResult Handle_k(StringExtractorGDBRemote &packet);
101 PacketResult Handle_qProcessInfo(StringExtractorGDBRemote &packet);
103 PacketResult Handle_qC(StringExtractorGDBRemote &packet);
105 PacketResult Handle_QSetDisableASLR(StringExtractorGDBRemote &packet);
107 PacketResult Handle_QSetWorkingDir(StringExtractorGDBRemote &packet);
109 PacketResult Handle_qGetWorkingDir(StringExtractorGDBRemote &packet);
111 PacketResult Handle_C(StringExtractorGDBRemote &packet);
113 PacketResult Handle_c(StringExtractorGDBRemote &packet);
115 PacketResult Handle_vCont(StringExtractorGDBRemote &packet);
117 PacketResult Handle_vCont_actions(StringExtractorGDBRemote &packet);
119 PacketResult Handle_stop_reason(StringExtractorGDBRemote &packet);
121 PacketResult Handle_qRegisterInfo(StringExtractorGDBRemote &packet);
123 PacketResult Handle_qfThreadInfo(StringExtractorGDBRemote &packet);
125 PacketResult Handle_qsThreadInfo(StringExtractorGDBRemote &packet);
127 PacketResult Handle_p(StringExtractorGDBRemote &packet);
129 PacketResult Handle_P(StringExtractorGDBRemote &packet);
131 PacketResult Handle_H(StringExtractorGDBRemote &packet);
133 PacketResult Handle_I(StringExtractorGDBRemote &packet);
135 PacketResult Handle_interrupt(StringExtractorGDBRemote &packet);
137 // Handles $m and $x packets.
138 PacketResult Handle_memory_read(StringExtractorGDBRemote &packet);
140 PacketResult Handle_M(StringExtractorGDBRemote &packet);
143 Handle_qMemoryRegionInfoSupported(StringExtractorGDBRemote &packet);
145 PacketResult Handle_qMemoryRegionInfo(StringExtractorGDBRemote &packet);
147 PacketResult Handle_Z(StringExtractorGDBRemote &packet);
149 PacketResult Handle_z(StringExtractorGDBRemote &packet);
151 PacketResult Handle_s(StringExtractorGDBRemote &packet);
153 PacketResult Handle_qXfer(StringExtractorGDBRemote &packet);
155 PacketResult Handle_QSaveRegisterState(StringExtractorGDBRemote &packet);
157 PacketResult Handle_jTraceStart(StringExtractorGDBRemote &packet);
159 PacketResult Handle_jTraceRead(StringExtractorGDBRemote &packet);
161 PacketResult Handle_jTraceStop(StringExtractorGDBRemote &packet);
163 PacketResult Handle_jTraceConfigRead(StringExtractorGDBRemote &packet);
165 PacketResult Handle_QRestoreRegisterState(StringExtractorGDBRemote &packet);
167 PacketResult Handle_vAttach(StringExtractorGDBRemote &packet);
169 PacketResult Handle_D(StringExtractorGDBRemote &packet);
171 PacketResult Handle_qThreadStopInfo(StringExtractorGDBRemote &packet);
173 PacketResult Handle_jThreadsInfo(StringExtractorGDBRemote &packet);
175 PacketResult Handle_qWatchpointSupportInfo(StringExtractorGDBRemote &packet);
177 PacketResult Handle_qFileLoadAddress(StringExtractorGDBRemote &packet);
179 PacketResult Handle_QPassSignals(StringExtractorGDBRemote &packet);
181 PacketResult Handle_g(StringExtractorGDBRemote &packet);
183 void SetCurrentThreadID(lldb::tid_t tid);
185 lldb::tid_t GetCurrentThreadID() const;
187 void SetContinueThreadID(lldb::tid_t tid);
189 lldb::tid_t GetContinueThreadID() const { return m_continue_tid; }
191 Status SetSTDIOFileDescriptor(int fd);
193 FileSpec FindModuleFile(const std::string &module_path,
194 const ArchSpec &arch) override;
196 llvm::Expected<std::unique_ptr<llvm::MemoryBuffer>>
197 ReadXferObject(llvm::StringRef object, llvm::StringRef annex);
199 static std::string XMLEncodeAttributeValue(llvm::StringRef value);
202 void HandleInferiorState_Exited(NativeProcessProtocol *process);
204 void HandleInferiorState_Stopped(NativeProcessProtocol *process);
206 NativeThreadProtocol *GetThreadFromSuffix(StringExtractorGDBRemote &packet);
208 uint32_t GetNextSavedRegistersID();
210 void MaybeCloseInferiorTerminalConnection();
212 void ClearProcessSpecificData();
214 void RegisterPacketHandlers();
216 void DataAvailableCallback();
218 void SendProcessOutput();
220 void StartSTDIOForwarding();
222 void StopSTDIOForwarding();
224 // For GDBRemoteCommunicationServerLLGS only
225 DISALLOW_COPY_AND_ASSIGN(GDBRemoteCommunicationServerLLGS);
228 } // namespace process_gdb_remote
229 } // namespace lldb_private
231 #endif // liblldb_GDBRemoteCommunicationServerLLGS_h_