xref: /llvm-project/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.h (revision eb43e43bb5b2d19e14309e120cb8e66136ae7c82)
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 #include <unordered_set>
15 
16 #include "lldb/Core/Communication.h"
17 #include "lldb/Host/MainLoop.h"
18 #include "lldb/Host/common/NativeProcessProtocol.h"
19 #include "lldb/lldb-private-forward.h"
20 
21 #include "GDBRemoteCommunicationServerCommon.h"
22 
23 class StringExtractorGDBRemote;
24 
25 namespace lldb_private {
26 
27 namespace process_gdb_remote {
28 
29 class ProcessGDBRemote;
30 
31 class GDBRemoteCommunicationServerLLGS
32     : public GDBRemoteCommunicationServerCommon,
33       public NativeProcessProtocol::NativeDelegate {
34 public:
35   // Constructors and Destructors
36   GDBRemoteCommunicationServerLLGS(
37       MainLoop &mainloop,
38       const NativeProcessProtocol::Factory &process_factory);
39 
40   void SetLaunchInfo(const ProcessLaunchInfo &info);
41 
42   /// Launch a process with the current launch settings.
43   ///
44   /// This method supports running an lldb-gdbserver or similar
45   /// server in a situation where the startup code has been provided
46   /// with all the information for a child process to be launched.
47   ///
48   /// \return
49   ///     An Status object indicating the success or failure of the
50   ///     launch.
51   Status LaunchProcess() override;
52 
53   /// Attach to a process.
54   ///
55   /// This method supports attaching llgs to a process accessible via the
56   /// configured Platform.
57   ///
58   /// \return
59   ///     An Status object indicating the success or failure of the
60   ///     attach operation.
61   Status AttachToProcess(lldb::pid_t pid);
62 
63   /// Wait to attach to a process with a given name.
64   ///
65   /// This method supports waiting for the next instance of a process
66   /// with a given name and attaching llgs to that via the configured
67   /// Platform.
68   ///
69   /// \return
70   ///     An Status object indicating the success or failure of the
71   ///     attach operation.
72   Status AttachWaitProcess(llvm::StringRef process_name, bool include_existing);
73 
74   // NativeProcessProtocol::NativeDelegate overrides
75   void InitializeDelegate(NativeProcessProtocol *process) override;
76 
77   void ProcessStateChanged(NativeProcessProtocol *process,
78                            lldb::StateType state) override;
79 
80   void DidExec(NativeProcessProtocol *process) override;
81 
82   void
83   NewSubprocess(NativeProcessProtocol *parent_process,
84                 std::unique_ptr<NativeProcessProtocol> child_process) override;
85 
86   Status InitializeConnection(std::unique_ptr<Connection> connection);
87 
88   struct DebuggedProcess {
89     enum class Flag {
90       vkilled = (1u << 0),
91 
92       LLVM_MARK_AS_BITMASK_ENUM(vkilled)
93     };
94 
95     std::unique_ptr<NativeProcessProtocol> process_up;
96     Flag flags;
97   };
98 
99 protected:
100   MainLoop &m_mainloop;
101   MainLoop::ReadHandleUP m_network_handle_up;
102   const NativeProcessProtocol::Factory &m_process_factory;
103   lldb::tid_t m_current_tid = LLDB_INVALID_THREAD_ID;
104   lldb::tid_t m_continue_tid = LLDB_INVALID_THREAD_ID;
105   NativeProcessProtocol *m_current_process;
106   NativeProcessProtocol *m_continue_process;
107   std::recursive_mutex m_debugged_process_mutex;
108   std::unordered_map<lldb::pid_t, DebuggedProcess> m_debugged_processes;
109 
110   Communication m_stdio_communication;
111   MainLoop::ReadHandleUP m_stdio_handle_up;
112 
113   llvm::StringMap<std::unique_ptr<llvm::MemoryBuffer>> m_xfer_buffer_map;
114   std::mutex m_saved_registers_mutex;
115   std::unordered_map<uint32_t, lldb::DataBufferSP> m_saved_registers_map;
116   uint32_t m_next_saved_registers_id = 1;
117   bool m_thread_suffix_supported = false;
118   bool m_list_threads_in_stop_reply = false;
119   bool m_non_stop = false;
120   std::deque<std::string> m_stop_notification_queue;
121 
122   NativeProcessProtocol::Extension m_extensions_supported = {};
123 
124   PacketResult SendONotification(const char *buffer, uint32_t len);
125 
126   PacketResult SendWResponse(NativeProcessProtocol *process);
127 
128   StreamString PrepareStopReplyPacketForThread(NativeThreadProtocol &thread);
129 
130   PacketResult SendStopReplyPacketForThread(NativeProcessProtocol &process,
131                                             lldb::tid_t tid,
132                                             bool force_synchronous);
133 
134   PacketResult SendStopReasonForState(NativeProcessProtocol &process,
135                                       lldb::StateType process_state,
136                                       bool force_synchronous);
137 
138   void EnqueueStopReplyPackets(lldb::tid_t thread_to_skip);
139 
140   PacketResult Handle_k(StringExtractorGDBRemote &packet);
141 
142   PacketResult Handle_vKill(StringExtractorGDBRemote &packet);
143 
144   PacketResult Handle_qProcessInfo(StringExtractorGDBRemote &packet);
145 
146   PacketResult Handle_qC(StringExtractorGDBRemote &packet);
147 
148   PacketResult Handle_QSetDisableASLR(StringExtractorGDBRemote &packet);
149 
150   PacketResult Handle_QSetWorkingDir(StringExtractorGDBRemote &packet);
151 
152   PacketResult Handle_qGetWorkingDir(StringExtractorGDBRemote &packet);
153 
154   PacketResult Handle_QThreadSuffixSupported(StringExtractorGDBRemote &packet);
155 
156   PacketResult Handle_QListThreadsInStopReply(StringExtractorGDBRemote &packet);
157 
158   PacketResult ResumeProcess(NativeProcessProtocol &process,
159                              const ResumeActionList &actions);
160 
161   PacketResult Handle_C(StringExtractorGDBRemote &packet);
162 
163   PacketResult Handle_c(StringExtractorGDBRemote &packet);
164 
165   PacketResult Handle_vCont(StringExtractorGDBRemote &packet);
166 
167   PacketResult Handle_vCont_actions(StringExtractorGDBRemote &packet);
168 
169   PacketResult Handle_stop_reason(StringExtractorGDBRemote &packet);
170 
171   PacketResult Handle_qRegisterInfo(StringExtractorGDBRemote &packet);
172 
173   void AddProcessThreads(StreamGDBRemote &response,
174                          NativeProcessProtocol &process, bool &had_any);
175 
176   PacketResult Handle_qfThreadInfo(StringExtractorGDBRemote &packet);
177 
178   PacketResult Handle_qsThreadInfo(StringExtractorGDBRemote &packet);
179 
180   PacketResult Handle_p(StringExtractorGDBRemote &packet);
181 
182   PacketResult Handle_P(StringExtractorGDBRemote &packet);
183 
184   PacketResult Handle_H(StringExtractorGDBRemote &packet);
185 
186   PacketResult Handle_I(StringExtractorGDBRemote &packet);
187 
188   PacketResult Handle_interrupt(StringExtractorGDBRemote &packet);
189 
190   // Handles $m and $x packets.
191   PacketResult Handle_memory_read(StringExtractorGDBRemote &packet);
192 
193   PacketResult Handle_M(StringExtractorGDBRemote &packet);
194   PacketResult Handle__M(StringExtractorGDBRemote &packet);
195   PacketResult Handle__m(StringExtractorGDBRemote &packet);
196 
197   PacketResult
198   Handle_qMemoryRegionInfoSupported(StringExtractorGDBRemote &packet);
199 
200   PacketResult Handle_qMemoryRegionInfo(StringExtractorGDBRemote &packet);
201 
202   PacketResult Handle_Z(StringExtractorGDBRemote &packet);
203 
204   PacketResult Handle_z(StringExtractorGDBRemote &packet);
205 
206   PacketResult Handle_s(StringExtractorGDBRemote &packet);
207 
208   PacketResult Handle_qXfer(StringExtractorGDBRemote &packet);
209 
210   PacketResult Handle_QSaveRegisterState(StringExtractorGDBRemote &packet);
211 
212   PacketResult Handle_jLLDBTraceSupported(StringExtractorGDBRemote &packet);
213 
214   PacketResult Handle_jLLDBTraceStart(StringExtractorGDBRemote &packet);
215 
216   PacketResult Handle_jLLDBTraceStop(StringExtractorGDBRemote &packet);
217 
218   PacketResult Handle_jLLDBTraceGetState(StringExtractorGDBRemote &packet);
219 
220   PacketResult Handle_jLLDBTraceGetBinaryData(StringExtractorGDBRemote &packet);
221 
222   PacketResult Handle_QRestoreRegisterState(StringExtractorGDBRemote &packet);
223 
224   PacketResult Handle_vAttach(StringExtractorGDBRemote &packet);
225 
226   PacketResult Handle_vAttachWait(StringExtractorGDBRemote &packet);
227 
228   PacketResult Handle_qVAttachOrWaitSupported(StringExtractorGDBRemote &packet);
229 
230   PacketResult Handle_vAttachOrWait(StringExtractorGDBRemote &packet);
231 
232   PacketResult Handle_vRun(StringExtractorGDBRemote &packet);
233 
234   PacketResult Handle_D(StringExtractorGDBRemote &packet);
235 
236   PacketResult Handle_qThreadStopInfo(StringExtractorGDBRemote &packet);
237 
238   PacketResult Handle_jThreadsInfo(StringExtractorGDBRemote &packet);
239 
240   PacketResult Handle_qWatchpointSupportInfo(StringExtractorGDBRemote &packet);
241 
242   PacketResult Handle_qFileLoadAddress(StringExtractorGDBRemote &packet);
243 
244   PacketResult Handle_QPassSignals(StringExtractorGDBRemote &packet);
245 
246   PacketResult Handle_qSaveCore(StringExtractorGDBRemote &packet);
247 
248   PacketResult Handle_QNonStop(StringExtractorGDBRemote &packet);
249 
250   PacketResult Handle_vStopped(StringExtractorGDBRemote &packet);
251 
252   PacketResult Handle_vCtrlC(StringExtractorGDBRemote &packet);
253 
254   PacketResult Handle_g(StringExtractorGDBRemote &packet);
255 
256   PacketResult Handle_qMemTags(StringExtractorGDBRemote &packet);
257 
258   PacketResult Handle_QMemTags(StringExtractorGDBRemote &packet);
259 
260   PacketResult Handle_T(StringExtractorGDBRemote &packet);
261 
262   void SetCurrentThreadID(lldb::tid_t tid);
263 
264   lldb::tid_t GetCurrentThreadID() const;
265 
266   void SetContinueThreadID(lldb::tid_t tid);
267 
268   lldb::tid_t GetContinueThreadID() const { return m_continue_tid; }
269 
270   Status SetSTDIOFileDescriptor(int fd);
271 
272   FileSpec FindModuleFile(const std::string &module_path,
273                           const ArchSpec &arch) override;
274 
275   llvm::Expected<std::unique_ptr<llvm::MemoryBuffer>>
276   ReadXferObject(llvm::StringRef object, llvm::StringRef annex);
277 
278   static std::string XMLEncodeAttributeValue(llvm::StringRef value);
279 
280   std::vector<std::string> HandleFeatures(
281       const llvm::ArrayRef<llvm::StringRef> client_features) override;
282 
283   // Provide a response for successful continue action, i.e. send "OK"
284   // in non-stop mode, no response otherwise.
285   PacketResult SendContinueSuccessResponse();
286 
287   void AppendThreadIDToResponse(Stream &response, lldb::pid_t pid,
288                                 lldb::tid_t tid);
289 
290 private:
291   llvm::Expected<std::unique_ptr<llvm::MemoryBuffer>> BuildTargetXml();
292 
293   void HandleInferiorState_Exited(NativeProcessProtocol *process);
294 
295   void HandleInferiorState_Stopped(NativeProcessProtocol *process);
296 
297   NativeThreadProtocol *GetThreadFromSuffix(StringExtractorGDBRemote &packet);
298 
299   uint32_t GetNextSavedRegistersID();
300 
301   void MaybeCloseInferiorTerminalConnection();
302 
303   void ClearProcessSpecificData();
304 
305   void RegisterPacketHandlers();
306 
307   void DataAvailableCallback();
308 
309   void SendProcessOutput();
310 
311   void StartSTDIOForwarding();
312 
313   void StopSTDIOForwarding();
314 
315   // Call SetEnabledExtensions() with appropriate flags on the process.
316   void SetEnabledExtensions(NativeProcessProtocol &process);
317 
318   // For GDBRemoteCommunicationServerLLGS only
319   GDBRemoteCommunicationServerLLGS(const GDBRemoteCommunicationServerLLGS &) =
320       delete;
321   const GDBRemoteCommunicationServerLLGS &
322   operator=(const GDBRemoteCommunicationServerLLGS &) = delete;
323 };
324 
325 std::string LLGSArgToURL(llvm::StringRef url_arg, bool reverse_connect);
326 
327 } // namespace process_gdb_remote
328 } // namespace lldb_private
329 
330 #endif // LLDB_SOURCE_PLUGINS_PROCESS_GDB_REMOTE_GDBREMOTECOMMUNICATIONSERVERLLGS_H
331