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 #include <mutex>
14 #include <unordered_map>
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   //------------------------------------------------------------------
36   // Constructors and Destructors
37   //------------------------------------------------------------------
38   GDBRemoteCommunicationServerLLGS(
39       MainLoop &mainloop,
40       const NativeProcessProtocol::Factory &process_factory);
41 
42   void SetLaunchInfo(const ProcessLaunchInfo &info);
43 
44   //------------------------------------------------------------------
45   /// Launch a process with the current launch settings.
46   ///
47   /// This method supports running an lldb-gdbserver or similar
48   /// server in a situation where the startup code has been provided
49   /// with all the information for a child process to be launched.
50   ///
51   /// @return
52   ///     An Status object indicating the success or failure of the
53   ///     launch.
54   //------------------------------------------------------------------
55   Status LaunchProcess() override;
56 
57   //------------------------------------------------------------------
58   /// Attach to a process.
59   ///
60   /// This method supports attaching llgs to a process accessible via the
61   /// configured Platform.
62   ///
63   /// @return
64   ///     An Status object indicating the success or failure of the
65   ///     attach operation.
66   //------------------------------------------------------------------
67   Status AttachToProcess(lldb::pid_t pid);
68 
69   //------------------------------------------------------------------
70   // NativeProcessProtocol::NativeDelegate overrides
71   //------------------------------------------------------------------
72   void InitializeDelegate(NativeProcessProtocol *process) override;
73 
74   void ProcessStateChanged(NativeProcessProtocol *process,
75                            lldb::StateType state) override;
76 
77   void DidExec(NativeProcessProtocol *process) override;
78 
79   Status InitializeConnection(std::unique_ptr<Connection> &&connection);
80 
81 protected:
82   MainLoop &m_mainloop;
83   MainLoop::ReadHandleUP m_network_handle_up;
84   const NativeProcessProtocol::Factory &m_process_factory;
85   lldb::tid_t m_current_tid = LLDB_INVALID_THREAD_ID;
86   lldb::tid_t m_continue_tid = LLDB_INVALID_THREAD_ID;
87   std::recursive_mutex m_debugged_process_mutex;
88   std::unique_ptr<NativeProcessProtocol> m_debugged_process_up;
89 
90   Communication m_stdio_communication;
91   MainLoop::ReadHandleUP m_stdio_handle_up;
92 
93   lldb::StateType m_inferior_prev_state = lldb::StateType::eStateInvalid;
94   std::unique_ptr<llvm::MemoryBuffer> m_active_auxv_buffer_up;
95   std::mutex m_saved_registers_mutex;
96   std::unordered_map<uint32_t, lldb::DataBufferSP> m_saved_registers_map;
97   uint32_t m_next_saved_registers_id = 1;
98   bool m_handshake_completed = false;
99 
100   PacketResult SendONotification(const char *buffer, uint32_t len);
101 
102   PacketResult SendWResponse(NativeProcessProtocol *process);
103 
104   PacketResult SendStopReplyPacketForThread(lldb::tid_t tid);
105 
106   PacketResult SendStopReasonForState(lldb::StateType process_state);
107 
108   PacketResult Handle_k(StringExtractorGDBRemote &packet);
109 
110   PacketResult Handle_qProcessInfo(StringExtractorGDBRemote &packet);
111 
112   PacketResult Handle_qC(StringExtractorGDBRemote &packet);
113 
114   PacketResult Handle_QSetDisableASLR(StringExtractorGDBRemote &packet);
115 
116   PacketResult Handle_QSetWorkingDir(StringExtractorGDBRemote &packet);
117 
118   PacketResult Handle_qGetWorkingDir(StringExtractorGDBRemote &packet);
119 
120   PacketResult Handle_C(StringExtractorGDBRemote &packet);
121 
122   PacketResult Handle_c(StringExtractorGDBRemote &packet);
123 
124   PacketResult Handle_vCont(StringExtractorGDBRemote &packet);
125 
126   PacketResult Handle_vCont_actions(StringExtractorGDBRemote &packet);
127 
128   PacketResult Handle_stop_reason(StringExtractorGDBRemote &packet);
129 
130   PacketResult Handle_qRegisterInfo(StringExtractorGDBRemote &packet);
131 
132   PacketResult Handle_qfThreadInfo(StringExtractorGDBRemote &packet);
133 
134   PacketResult Handle_qsThreadInfo(StringExtractorGDBRemote &packet);
135 
136   PacketResult Handle_p(StringExtractorGDBRemote &packet);
137 
138   PacketResult Handle_P(StringExtractorGDBRemote &packet);
139 
140   PacketResult Handle_H(StringExtractorGDBRemote &packet);
141 
142   PacketResult Handle_I(StringExtractorGDBRemote &packet);
143 
144   PacketResult Handle_interrupt(StringExtractorGDBRemote &packet);
145 
146   // Handles $m and $x packets.
147   PacketResult Handle_memory_read(StringExtractorGDBRemote &packet);
148 
149   PacketResult Handle_M(StringExtractorGDBRemote &packet);
150 
151   PacketResult
152   Handle_qMemoryRegionInfoSupported(StringExtractorGDBRemote &packet);
153 
154   PacketResult Handle_qMemoryRegionInfo(StringExtractorGDBRemote &packet);
155 
156   PacketResult Handle_Z(StringExtractorGDBRemote &packet);
157 
158   PacketResult Handle_z(StringExtractorGDBRemote &packet);
159 
160   PacketResult Handle_s(StringExtractorGDBRemote &packet);
161 
162   PacketResult Handle_qXfer_auxv_read(StringExtractorGDBRemote &packet);
163 
164   PacketResult Handle_QSaveRegisterState(StringExtractorGDBRemote &packet);
165 
166   PacketResult Handle_jTraceStart(StringExtractorGDBRemote &packet);
167 
168   PacketResult Handle_jTraceRead(StringExtractorGDBRemote &packet);
169 
170   PacketResult Handle_jTraceStop(StringExtractorGDBRemote &packet);
171 
172   PacketResult Handle_jTraceConfigRead(StringExtractorGDBRemote &packet);
173 
174   PacketResult Handle_QRestoreRegisterState(StringExtractorGDBRemote &packet);
175 
176   PacketResult Handle_vAttach(StringExtractorGDBRemote &packet);
177 
178   PacketResult Handle_D(StringExtractorGDBRemote &packet);
179 
180   PacketResult Handle_qThreadStopInfo(StringExtractorGDBRemote &packet);
181 
182   PacketResult Handle_jThreadsInfo(StringExtractorGDBRemote &packet);
183 
184   PacketResult Handle_qWatchpointSupportInfo(StringExtractorGDBRemote &packet);
185 
186   PacketResult Handle_qFileLoadAddress(StringExtractorGDBRemote &packet);
187 
188   PacketResult Handle_QPassSignals(StringExtractorGDBRemote &packet);
189 
190   void SetCurrentThreadID(lldb::tid_t tid);
191 
192   lldb::tid_t GetCurrentThreadID() const;
193 
194   void SetContinueThreadID(lldb::tid_t tid);
195 
GetContinueThreadID()196   lldb::tid_t GetContinueThreadID() const { return m_continue_tid; }
197 
198   Status SetSTDIOFileDescriptor(int fd);
199 
200   FileSpec FindModuleFile(const std::string &module_path,
201                           const ArchSpec &arch) override;
202 
203 private:
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   //------------------------------------------------------------------
227   // For GDBRemoteCommunicationServerLLGS only
228   //------------------------------------------------------------------
229   DISALLOW_COPY_AND_ASSIGN(GDBRemoteCommunicationServerLLGS);
230 };
231 
232 } // namespace process_gdb_remote
233 } // namespace lldb_private
234 
235 #endif // liblldb_GDBRemoteCommunicationServerLLGS_h_
236