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   // NativeProcessProtocol::NativeDelegate overrides
63   void InitializeDelegate(NativeProcessProtocol *process) override;
64 
65   void ProcessStateChanged(NativeProcessProtocol *process,
66                            lldb::StateType state) override;
67 
68   void DidExec(NativeProcessProtocol *process) override;
69 
70   Status InitializeConnection(std::unique_ptr<Connection> connection);
71 
72 protected:
73   MainLoop &m_mainloop;
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;
80 
81   Communication m_stdio_communication;
82   MainLoop::ReadHandleUP m_stdio_handle_up;
83 
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;
90 
91   PacketResult SendONotification(const char *buffer, uint32_t len);
92 
93   PacketResult SendWResponse(NativeProcessProtocol *process);
94 
95   PacketResult SendStopReplyPacketForThread(lldb::tid_t tid);
96 
97   PacketResult SendStopReasonForState(lldb::StateType process_state);
98 
99   PacketResult Handle_k(StringExtractorGDBRemote &packet);
100 
101   PacketResult Handle_qProcessInfo(StringExtractorGDBRemote &packet);
102 
103   PacketResult Handle_qC(StringExtractorGDBRemote &packet);
104 
105   PacketResult Handle_QSetDisableASLR(StringExtractorGDBRemote &packet);
106 
107   PacketResult Handle_QSetWorkingDir(StringExtractorGDBRemote &packet);
108 
109   PacketResult Handle_qGetWorkingDir(StringExtractorGDBRemote &packet);
110 
111   PacketResult Handle_C(StringExtractorGDBRemote &packet);
112 
113   PacketResult Handle_c(StringExtractorGDBRemote &packet);
114 
115   PacketResult Handle_vCont(StringExtractorGDBRemote &packet);
116 
117   PacketResult Handle_vCont_actions(StringExtractorGDBRemote &packet);
118 
119   PacketResult Handle_stop_reason(StringExtractorGDBRemote &packet);
120 
121   PacketResult Handle_qRegisterInfo(StringExtractorGDBRemote &packet);
122 
123   PacketResult Handle_qfThreadInfo(StringExtractorGDBRemote &packet);
124 
125   PacketResult Handle_qsThreadInfo(StringExtractorGDBRemote &packet);
126 
127   PacketResult Handle_p(StringExtractorGDBRemote &packet);
128 
129   PacketResult Handle_P(StringExtractorGDBRemote &packet);
130 
131   PacketResult Handle_H(StringExtractorGDBRemote &packet);
132 
133   PacketResult Handle_I(StringExtractorGDBRemote &packet);
134 
135   PacketResult Handle_interrupt(StringExtractorGDBRemote &packet);
136 
137   // Handles $m and $x packets.
138   PacketResult Handle_memory_read(StringExtractorGDBRemote &packet);
139 
140   PacketResult Handle_M(StringExtractorGDBRemote &packet);
141 
142   PacketResult
143   Handle_qMemoryRegionInfoSupported(StringExtractorGDBRemote &packet);
144 
145   PacketResult Handle_qMemoryRegionInfo(StringExtractorGDBRemote &packet);
146 
147   PacketResult Handle_Z(StringExtractorGDBRemote &packet);
148 
149   PacketResult Handle_z(StringExtractorGDBRemote &packet);
150 
151   PacketResult Handle_s(StringExtractorGDBRemote &packet);
152 
153   PacketResult Handle_qXfer(StringExtractorGDBRemote &packet);
154 
155   PacketResult Handle_QSaveRegisterState(StringExtractorGDBRemote &packet);
156 
157   PacketResult Handle_jTraceStart(StringExtractorGDBRemote &packet);
158 
159   PacketResult Handle_jTraceRead(StringExtractorGDBRemote &packet);
160 
161   PacketResult Handle_jTraceStop(StringExtractorGDBRemote &packet);
162 
163   PacketResult Handle_jTraceConfigRead(StringExtractorGDBRemote &packet);
164 
165   PacketResult Handle_QRestoreRegisterState(StringExtractorGDBRemote &packet);
166 
167   PacketResult Handle_vAttach(StringExtractorGDBRemote &packet);
168 
169   PacketResult Handle_D(StringExtractorGDBRemote &packet);
170 
171   PacketResult Handle_qThreadStopInfo(StringExtractorGDBRemote &packet);
172 
173   PacketResult Handle_jThreadsInfo(StringExtractorGDBRemote &packet);
174 
175   PacketResult Handle_qWatchpointSupportInfo(StringExtractorGDBRemote &packet);
176 
177   PacketResult Handle_qFileLoadAddress(StringExtractorGDBRemote &packet);
178 
179   PacketResult Handle_QPassSignals(StringExtractorGDBRemote &packet);
180 
181   PacketResult Handle_g(StringExtractorGDBRemote &packet);
182 
183   void SetCurrentThreadID(lldb::tid_t tid);
184 
185   lldb::tid_t GetCurrentThreadID() const;
186 
187   void SetContinueThreadID(lldb::tid_t tid);
188 
189   lldb::tid_t GetContinueThreadID() const { return m_continue_tid; }
190 
191   Status SetSTDIOFileDescriptor(int fd);
192 
193   FileSpec FindModuleFile(const std::string &module_path,
194                           const ArchSpec &arch) override;
195 
196   llvm::Expected<std::unique_ptr<llvm::MemoryBuffer>>
197   ReadXferObject(llvm::StringRef object, llvm::StringRef annex);
198 
199   static std::string XMLEncodeAttributeValue(llvm::StringRef value);
200 
201 private:
202   llvm::Expected<std::unique_ptr<llvm::MemoryBuffer>> BuildTargetXml();
203 
204   void HandleInferiorState_Exited(NativeProcessProtocol *process);
205 
206   void HandleInferiorState_Stopped(NativeProcessProtocol *process);
207 
208   NativeThreadProtocol *GetThreadFromSuffix(StringExtractorGDBRemote &packet);
209 
210   uint32_t GetNextSavedRegistersID();
211 
212   void MaybeCloseInferiorTerminalConnection();
213 
214   void ClearProcessSpecificData();
215 
216   void RegisterPacketHandlers();
217 
218   void DataAvailableCallback();
219 
220   void SendProcessOutput();
221 
222   void StartSTDIOForwarding();
223 
224   void StopSTDIOForwarding();
225 
226   // For GDBRemoteCommunicationServerLLGS only
227   GDBRemoteCommunicationServerLLGS(const GDBRemoteCommunicationServerLLGS &) =
228       delete;
229   const GDBRemoteCommunicationServerLLGS &
230   operator=(const GDBRemoteCommunicationServerLLGS &) = delete;
231 };
232 
233 } // namespace process_gdb_remote
234 } // namespace lldb_private
235 
236 #endif // LLDB_SOURCE_PLUGINS_PROCESS_GDB_REMOTE_GDBREMOTECOMMUNICATIONSERVERLLGS_H
237