1 //===-- GDBRemoteCommunicationServerLLGS.h ----------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef liblldb_GDBRemoteCommunicationServerLLGS_h_
11 #define liblldb_GDBRemoteCommunicationServerLLGS_h_
12 
13 // C Includes
14 // C++ Includes
15 #include <mutex>
16 #include <unordered_map>
17 
18 // Other libraries and framework includes
19 #include "lldb/Core/Communication.h"
20 #include "lldb/Host/MainLoop.h"
21 #include "lldb/Host/common/NativeProcessProtocol.h"
22 #include "lldb/lldb-private-forward.h"
23 
24 // Project includes
25 #include "GDBRemoteCommunicationServerCommon.h"
26 
27 class StringExtractorGDBRemote;
28 
29 namespace lldb_private {
30 
31 namespace process_gdb_remote {
32 
33 class ProcessGDBRemote;
34 
35 class GDBRemoteCommunicationServerLLGS
36     : public GDBRemoteCommunicationServerCommon,
37       public NativeProcessProtocol::NativeDelegate {
38 public:
39   //------------------------------------------------------------------
40   // Constructors and Destructors
41   //------------------------------------------------------------------
42   GDBRemoteCommunicationServerLLGS(
43       MainLoop &mainloop,
44       const NativeProcessProtocol::Factory &process_factory);
45 
46   void SetLaunchInfo(const ProcessLaunchInfo &info);
47 
48   //------------------------------------------------------------------
49   /// Launch a process with the current launch settings.
50   ///
51   /// This method supports running an lldb-gdbserver or similar
52   /// server in a situation where the startup code has been provided
53   /// with all the information for a child process to be launched.
54   ///
55   /// @return
56   ///     An Status object indicating the success or failure of the
57   ///     launch.
58   //------------------------------------------------------------------
59   Status LaunchProcess() override;
60 
61   //------------------------------------------------------------------
62   /// Attach to a process.
63   ///
64   /// This method supports attaching llgs to a process accessible via the
65   /// configured Platform.
66   ///
67   /// @return
68   ///     An Status object indicating the success or failure of the
69   ///     attach operation.
70   //------------------------------------------------------------------
71   Status AttachToProcess(lldb::pid_t pid);
72 
73   //------------------------------------------------------------------
74   // NativeProcessProtocol::NativeDelegate overrides
75   //------------------------------------------------------------------
76   void InitializeDelegate(NativeProcessProtocol *process) override;
77 
78   void ProcessStateChanged(NativeProcessProtocol *process,
79                            lldb::StateType state) override;
80 
81   void DidExec(NativeProcessProtocol *process) override;
82 
83   Status InitializeConnection(std::unique_ptr<Connection> &&connection);
84 
85 protected:
86   MainLoop &m_mainloop;
87   MainLoop::ReadHandleUP m_network_handle_up;
88   const NativeProcessProtocol::Factory &m_process_factory;
89   lldb::tid_t m_current_tid = LLDB_INVALID_THREAD_ID;
90   lldb::tid_t m_continue_tid = LLDB_INVALID_THREAD_ID;
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   std::unique_ptr<llvm::MemoryBuffer> m_active_auxv_buffer_up;
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 
104   PacketResult SendONotification(const char *buffer, uint32_t len);
105 
106   PacketResult SendWResponse(NativeProcessProtocol *process);
107 
108   PacketResult SendStopReplyPacketForThread(lldb::tid_t tid);
109 
110   PacketResult SendStopReasonForState(lldb::StateType process_state);
111 
112   PacketResult Handle_k(StringExtractorGDBRemote &packet);
113 
114   PacketResult Handle_qProcessInfo(StringExtractorGDBRemote &packet);
115 
116   PacketResult Handle_qC(StringExtractorGDBRemote &packet);
117 
118   PacketResult Handle_QSetDisableASLR(StringExtractorGDBRemote &packet);
119 
120   PacketResult Handle_QSetWorkingDir(StringExtractorGDBRemote &packet);
121 
122   PacketResult Handle_qGetWorkingDir(StringExtractorGDBRemote &packet);
123 
124   PacketResult Handle_C(StringExtractorGDBRemote &packet);
125 
126   PacketResult Handle_c(StringExtractorGDBRemote &packet);
127 
128   PacketResult Handle_vCont(StringExtractorGDBRemote &packet);
129 
130   PacketResult Handle_vCont_actions(StringExtractorGDBRemote &packet);
131 
132   PacketResult Handle_stop_reason(StringExtractorGDBRemote &packet);
133 
134   PacketResult Handle_qRegisterInfo(StringExtractorGDBRemote &packet);
135 
136   PacketResult Handle_qfThreadInfo(StringExtractorGDBRemote &packet);
137 
138   PacketResult Handle_qsThreadInfo(StringExtractorGDBRemote &packet);
139 
140   PacketResult Handle_p(StringExtractorGDBRemote &packet);
141 
142   PacketResult Handle_P(StringExtractorGDBRemote &packet);
143 
144   PacketResult Handle_H(StringExtractorGDBRemote &packet);
145 
146   PacketResult Handle_I(StringExtractorGDBRemote &packet);
147 
148   PacketResult Handle_interrupt(StringExtractorGDBRemote &packet);
149 
150   // Handles $m and $x packets.
151   PacketResult Handle_memory_read(StringExtractorGDBRemote &packet);
152 
153   PacketResult Handle_M(StringExtractorGDBRemote &packet);
154 
155   PacketResult
156   Handle_qMemoryRegionInfoSupported(StringExtractorGDBRemote &packet);
157 
158   PacketResult Handle_qMemoryRegionInfo(StringExtractorGDBRemote &packet);
159 
160   PacketResult Handle_Z(StringExtractorGDBRemote &packet);
161 
162   PacketResult Handle_z(StringExtractorGDBRemote &packet);
163 
164   PacketResult Handle_s(StringExtractorGDBRemote &packet);
165 
166   PacketResult Handle_qXfer_auxv_read(StringExtractorGDBRemote &packet);
167 
168   PacketResult Handle_QSaveRegisterState(StringExtractorGDBRemote &packet);
169 
170   PacketResult Handle_jTraceStart(StringExtractorGDBRemote &packet);
171 
172   PacketResult Handle_jTraceRead(StringExtractorGDBRemote &packet);
173 
174   PacketResult Handle_jTraceStop(StringExtractorGDBRemote &packet);
175 
176   PacketResult Handle_jTraceConfigRead(StringExtractorGDBRemote &packet);
177 
178   PacketResult Handle_QRestoreRegisterState(StringExtractorGDBRemote &packet);
179 
180   PacketResult Handle_vAttach(StringExtractorGDBRemote &packet);
181 
182   PacketResult Handle_D(StringExtractorGDBRemote &packet);
183 
184   PacketResult Handle_qThreadStopInfo(StringExtractorGDBRemote &packet);
185 
186   PacketResult Handle_jThreadsInfo(StringExtractorGDBRemote &packet);
187 
188   PacketResult Handle_qWatchpointSupportInfo(StringExtractorGDBRemote &packet);
189 
190   PacketResult Handle_qFileLoadAddress(StringExtractorGDBRemote &packet);
191 
192   PacketResult Handle_QPassSignals(StringExtractorGDBRemote &packet);
193 
194   void SetCurrentThreadID(lldb::tid_t tid);
195 
196   lldb::tid_t GetCurrentThreadID() const;
197 
198   void SetContinueThreadID(lldb::tid_t tid);
199 
200   lldb::tid_t GetContinueThreadID() const { return m_continue_tid; }
201 
202   Status SetSTDIOFileDescriptor(int fd);
203 
204   FileSpec FindModuleFile(const std::string &module_path,
205                           const ArchSpec &arch) override;
206 
207 private:
208   void HandleInferiorState_Exited(NativeProcessProtocol *process);
209 
210   void HandleInferiorState_Stopped(NativeProcessProtocol *process);
211 
212   NativeThreadProtocol *GetThreadFromSuffix(StringExtractorGDBRemote &packet);
213 
214   uint32_t GetNextSavedRegistersID();
215 
216   void MaybeCloseInferiorTerminalConnection();
217 
218   void ClearProcessSpecificData();
219 
220   void RegisterPacketHandlers();
221 
222   void DataAvailableCallback();
223 
224   void SendProcessOutput();
225 
226   void StartSTDIOForwarding();
227 
228   void StopSTDIOForwarding();
229 
230   //------------------------------------------------------------------
231   // For GDBRemoteCommunicationServerLLGS only
232   //------------------------------------------------------------------
233   DISALLOW_COPY_AND_ASSIGN(GDBRemoteCommunicationServerLLGS);
234 };
235 
236 } // namespace process_gdb_remote
237 } // namespace lldb_private
238 
239 #endif // liblldb_GDBRemoteCommunicationServerLLGS_h_
240