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   void
82   NewSubprocess(NativeProcessProtocol *parent_process,
83                 std::unique_ptr<NativeProcessProtocol> child_process) override;
84 
85   Status InitializeConnection(std::unique_ptr<Connection> connection);
86 
87 protected:
88   MainLoop &m_mainloop;
89   MainLoop::ReadHandleUP m_network_handle_up;
90   const NativeProcessProtocol::Factory &m_process_factory;
91   lldb::tid_t m_current_tid = LLDB_INVALID_THREAD_ID;
92   lldb::tid_t m_continue_tid = LLDB_INVALID_THREAD_ID;
93   NativeProcessProtocol *m_current_process;
94   NativeProcessProtocol *m_continue_process;
95   std::recursive_mutex m_debugged_process_mutex;
96   std::unordered_map<lldb::pid_t, std::unique_ptr<NativeProcessProtocol>>
97       m_debugged_processes;
98 
99   Communication m_stdio_communication;
100   MainLoop::ReadHandleUP m_stdio_handle_up;
101 
102   lldb::StateType m_inferior_prev_state = lldb::StateType::eStateInvalid;
103   llvm::StringMap<std::unique_ptr<llvm::MemoryBuffer>> m_xfer_buffer_map;
104   std::mutex m_saved_registers_mutex;
105   std::unordered_map<uint32_t, lldb::DataBufferSP> m_saved_registers_map;
106   uint32_t m_next_saved_registers_id = 1;
107   bool m_handshake_completed = false;
108   bool m_thread_suffix_supported = false;
109   bool m_list_threads_in_stop_reply = false;
110 
111   NativeProcessProtocol::Extension m_extensions_supported = {};
112 
113   PacketResult SendONotification(const char *buffer, uint32_t len);
114 
115   PacketResult SendWResponse(NativeProcessProtocol *process);
116 
117   PacketResult SendStopReplyPacketForThread(lldb::tid_t tid);
118 
119   PacketResult SendStopReasonForState(lldb::StateType process_state);
120 
121   PacketResult Handle_k(StringExtractorGDBRemote &packet);
122 
123   PacketResult Handle_qProcessInfo(StringExtractorGDBRemote &packet);
124 
125   PacketResult Handle_qC(StringExtractorGDBRemote &packet);
126 
127   PacketResult Handle_QSetDisableASLR(StringExtractorGDBRemote &packet);
128 
129   PacketResult Handle_QSetWorkingDir(StringExtractorGDBRemote &packet);
130 
131   PacketResult Handle_qGetWorkingDir(StringExtractorGDBRemote &packet);
132 
133   PacketResult Handle_QThreadSuffixSupported(StringExtractorGDBRemote &packet);
134 
135   PacketResult Handle_QListThreadsInStopReply(StringExtractorGDBRemote &packet);
136 
137   PacketResult Handle_C(StringExtractorGDBRemote &packet);
138 
139   PacketResult Handle_c(StringExtractorGDBRemote &packet);
140 
141   PacketResult Handle_vCont(StringExtractorGDBRemote &packet);
142 
143   PacketResult Handle_vCont_actions(StringExtractorGDBRemote &packet);
144 
145   PacketResult Handle_stop_reason(StringExtractorGDBRemote &packet);
146 
147   PacketResult Handle_qRegisterInfo(StringExtractorGDBRemote &packet);
148 
149   PacketResult Handle_qfThreadInfo(StringExtractorGDBRemote &packet);
150 
151   PacketResult Handle_qsThreadInfo(StringExtractorGDBRemote &packet);
152 
153   PacketResult Handle_p(StringExtractorGDBRemote &packet);
154 
155   PacketResult Handle_P(StringExtractorGDBRemote &packet);
156 
157   PacketResult Handle_H(StringExtractorGDBRemote &packet);
158 
159   PacketResult Handle_I(StringExtractorGDBRemote &packet);
160 
161   PacketResult Handle_interrupt(StringExtractorGDBRemote &packet);
162 
163   // Handles $m and $x packets.
164   PacketResult Handle_memory_read(StringExtractorGDBRemote &packet);
165 
166   PacketResult Handle_M(StringExtractorGDBRemote &packet);
167   PacketResult Handle__M(StringExtractorGDBRemote &packet);
168   PacketResult Handle__m(StringExtractorGDBRemote &packet);
169 
170   PacketResult
171   Handle_qMemoryRegionInfoSupported(StringExtractorGDBRemote &packet);
172 
173   PacketResult Handle_qMemoryRegionInfo(StringExtractorGDBRemote &packet);
174 
175   PacketResult Handle_Z(StringExtractorGDBRemote &packet);
176 
177   PacketResult Handle_z(StringExtractorGDBRemote &packet);
178 
179   PacketResult Handle_s(StringExtractorGDBRemote &packet);
180 
181   PacketResult Handle_qXfer(StringExtractorGDBRemote &packet);
182 
183   PacketResult Handle_QSaveRegisterState(StringExtractorGDBRemote &packet);
184 
185   PacketResult Handle_jLLDBTraceSupported(StringExtractorGDBRemote &packet);
186 
187   PacketResult Handle_jLLDBTraceStart(StringExtractorGDBRemote &packet);
188 
189   PacketResult Handle_jLLDBTraceStop(StringExtractorGDBRemote &packet);
190 
191   PacketResult Handle_jLLDBTraceGetState(StringExtractorGDBRemote &packet);
192 
193   PacketResult Handle_jLLDBTraceGetBinaryData(StringExtractorGDBRemote &packet);
194 
195   PacketResult Handle_QRestoreRegisterState(StringExtractorGDBRemote &packet);
196 
197   PacketResult Handle_vAttach(StringExtractorGDBRemote &packet);
198 
199   PacketResult Handle_vAttachWait(StringExtractorGDBRemote &packet);
200 
201   PacketResult Handle_qVAttachOrWaitSupported(StringExtractorGDBRemote &packet);
202 
203   PacketResult Handle_vAttachOrWait(StringExtractorGDBRemote &packet);
204 
205   PacketResult Handle_D(StringExtractorGDBRemote &packet);
206 
207   PacketResult Handle_qThreadStopInfo(StringExtractorGDBRemote &packet);
208 
209   PacketResult Handle_jThreadsInfo(StringExtractorGDBRemote &packet);
210 
211   PacketResult Handle_qWatchpointSupportInfo(StringExtractorGDBRemote &packet);
212 
213   PacketResult Handle_qFileLoadAddress(StringExtractorGDBRemote &packet);
214 
215   PacketResult Handle_QPassSignals(StringExtractorGDBRemote &packet);
216 
217   PacketResult Handle_g(StringExtractorGDBRemote &packet);
218 
219   PacketResult Handle_qMemTags(StringExtractorGDBRemote &packet);
220 
221   PacketResult Handle_QMemTags(StringExtractorGDBRemote &packet);
222 
223   void SetCurrentThreadID(lldb::tid_t tid);
224 
225   lldb::tid_t GetCurrentThreadID() const;
226 
227   void SetContinueThreadID(lldb::tid_t tid);
228 
229   lldb::tid_t GetContinueThreadID() const { return m_continue_tid; }
230 
231   Status SetSTDIOFileDescriptor(int fd);
232 
233   FileSpec FindModuleFile(const std::string &module_path,
234                           const ArchSpec &arch) override;
235 
236   llvm::Expected<std::unique_ptr<llvm::MemoryBuffer>>
237   ReadXferObject(llvm::StringRef object, llvm::StringRef annex);
238 
239   static std::string XMLEncodeAttributeValue(llvm::StringRef value);
240 
241   virtual std::vector<std::string> HandleFeatures(
242       const llvm::ArrayRef<llvm::StringRef> client_features) override;
243 
244 private:
245   llvm::Expected<std::unique_ptr<llvm::MemoryBuffer>> BuildTargetXml();
246 
247   void HandleInferiorState_Exited(NativeProcessProtocol *process);
248 
249   void HandleInferiorState_Stopped(NativeProcessProtocol *process);
250 
251   NativeThreadProtocol *GetThreadFromSuffix(StringExtractorGDBRemote &packet);
252 
253   uint32_t GetNextSavedRegistersID();
254 
255   void MaybeCloseInferiorTerminalConnection();
256 
257   void ClearProcessSpecificData();
258 
259   void RegisterPacketHandlers();
260 
261   void DataAvailableCallback();
262 
263   void SendProcessOutput();
264 
265   void StartSTDIOForwarding();
266 
267   void StopSTDIOForwarding();
268 
269   // Read thread-id from packet.  If the thread-id is correct, returns it.
270   // Otherwise, returns the error.
271   //
272   // If allow_all is true, then the pid/tid value of -1 ('all') will be allowed.
273   // In any case, the function assumes that exactly one inferior is being
274   // debugged and rejects pid values that do no match that inferior.
275   llvm::Expected<lldb::tid_t> ReadTid(StringExtractorGDBRemote &packet,
276                                       bool allow_all, lldb::pid_t default_pid);
277 
278   // Call SetEnabledExtensions() with appropriate flags on the process.
279   void SetEnabledExtensions(NativeProcessProtocol &process);
280 
281   // For GDBRemoteCommunicationServerLLGS only
282   GDBRemoteCommunicationServerLLGS(const GDBRemoteCommunicationServerLLGS &) =
283       delete;
284   const GDBRemoteCommunicationServerLLGS &
285   operator=(const GDBRemoteCommunicationServerLLGS &) = delete;
286 };
287 
288 } // namespace process_gdb_remote
289 } // namespace lldb_private
290 
291 #endif // LLDB_SOURCE_PLUGINS_PROCESS_GDB_REMOTE_GDBREMOTECOMMUNICATIONSERVERLLGS_H
292