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/lldb-private-forward.h"
20 #include "lldb/Core/Communication.h"
21 #include "lldb/Host/common/NativeProcessProtocol.h"
22 #include "lldb/Host/MainLoop.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 {
39 public:
40     //------------------------------------------------------------------
41     // Constructors and Destructors
42     //------------------------------------------------------------------
43     GDBRemoteCommunicationServerLLGS(MainLoop &mainloop);
44 
45     //------------------------------------------------------------------
46     /// Specify the program to launch and its arguments.
47     ///
48     /// @param[in] args
49     ///     The command line to launch.
50     ///
51     /// @param[in] argc
52     ///     The number of elements in the args array of cstring pointers.
53     ///
54     /// @return
55     ///     An Error object indicating the success or failure of making
56     ///     the setting.
57     //------------------------------------------------------------------
58     Error
59     SetLaunchArguments (const char *const args[], int argc);
60 
61     //------------------------------------------------------------------
62     /// Specify the launch flags for the process.
63     ///
64     /// @param[in] launch_flags
65     ///     The launch flags to use when launching this process.
66     ///
67     /// @return
68     ///     An Error object indicating the success or failure of making
69     ///     the setting.
70     //------------------------------------------------------------------
71     Error
72     SetLaunchFlags (unsigned int launch_flags);
73 
74     //------------------------------------------------------------------
75     /// Launch a process with the current launch settings.
76     ///
77     /// This method supports running an lldb-gdbserver or similar
78     /// server in a situation where the startup code has been provided
79     /// with all the information for a child process to be launched.
80     ///
81     /// @return
82     ///     An Error object indicating the success or failure of the
83     ///     launch.
84     //------------------------------------------------------------------
85     Error
86     LaunchProcess () override;
87 
88     //------------------------------------------------------------------
89     /// Attach to a process.
90     ///
91     /// This method supports attaching llgs to a process accessible via the
92     /// configured Platform.
93     ///
94     /// @return
95     ///     An Error object indicating the success or failure of the
96     ///     attach operation.
97     //------------------------------------------------------------------
98     Error
99     AttachToProcess (lldb::pid_t pid);
100 
101     //------------------------------------------------------------------
102     // NativeProcessProtocol::NativeDelegate overrides
103     //------------------------------------------------------------------
104     void
105     InitializeDelegate (NativeProcessProtocol *process) override;
106 
107     void
108     ProcessStateChanged (NativeProcessProtocol *process, lldb::StateType state) override;
109 
110     void
111     DidExec (NativeProcessProtocol *process) override;
112 
113     Error
114     InitializeConnection (std::unique_ptr<Connection> &&connection);
115 
116 protected:
117     MainLoop &m_mainloop;
118     MainLoop::ReadHandleUP m_network_handle_up;
119     lldb::tid_t m_current_tid;
120     lldb::tid_t m_continue_tid;
121     std::recursive_mutex m_debugged_process_mutex;
122     NativeProcessProtocolSP m_debugged_process_sp;
123 
124     Communication m_stdio_communication;
125     MainLoop::ReadHandleUP m_stdio_handle_up;
126 
127     lldb::StateType m_inferior_prev_state;
128     lldb::DataBufferSP m_active_auxv_buffer_sp;
129     std::mutex m_saved_registers_mutex;
130     std::unordered_map<uint32_t, lldb::DataBufferSP> m_saved_registers_map;
131     uint32_t m_next_saved_registers_id;
132     bool m_handshake_completed : 1;
133 
134     PacketResult
135     SendONotification (const char *buffer, uint32_t len);
136 
137     PacketResult
138     SendWResponse (NativeProcessProtocol *process);
139 
140     PacketResult
141     SendStopReplyPacketForThread (lldb::tid_t tid);
142 
143     PacketResult
144     SendStopReasonForState (lldb::StateType process_state);
145 
146     PacketResult
147     Handle_k (StringExtractorGDBRemote &packet);
148 
149     PacketResult
150     Handle_qProcessInfo (StringExtractorGDBRemote &packet);
151 
152     PacketResult
153     Handle_qC (StringExtractorGDBRemote &packet);
154 
155     PacketResult
156     Handle_QSetDisableASLR (StringExtractorGDBRemote &packet);
157 
158     PacketResult
159     Handle_QSetWorkingDir (StringExtractorGDBRemote &packet);
160 
161     PacketResult
162     Handle_qGetWorkingDir (StringExtractorGDBRemote &packet);
163 
164     PacketResult
165     Handle_C (StringExtractorGDBRemote &packet);
166 
167     PacketResult
168     Handle_c (StringExtractorGDBRemote &packet);
169 
170     PacketResult
171     Handle_vCont (StringExtractorGDBRemote &packet);
172 
173     PacketResult
174     Handle_vCont_actions (StringExtractorGDBRemote &packet);
175 
176     PacketResult
177     Handle_stop_reason (StringExtractorGDBRemote &packet);
178 
179     PacketResult
180     Handle_qRegisterInfo (StringExtractorGDBRemote &packet);
181 
182     PacketResult
183     Handle_qfThreadInfo (StringExtractorGDBRemote &packet);
184 
185     PacketResult
186     Handle_qsThreadInfo (StringExtractorGDBRemote &packet);
187 
188     PacketResult
189     Handle_p (StringExtractorGDBRemote &packet);
190 
191     PacketResult
192     Handle_P (StringExtractorGDBRemote &packet);
193 
194     PacketResult
195     Handle_H (StringExtractorGDBRemote &packet);
196 
197     PacketResult
198     Handle_I (StringExtractorGDBRemote &packet);
199 
200     PacketResult
201     Handle_interrupt (StringExtractorGDBRemote &packet);
202 
203     // Handles $m and $x packets.
204     PacketResult
205     Handle_memory_read (StringExtractorGDBRemote &packet);
206 
207     PacketResult
208     Handle_M (StringExtractorGDBRemote &packet);
209 
210     PacketResult
211     Handle_qMemoryRegionInfoSupported (StringExtractorGDBRemote &packet);
212 
213     PacketResult
214     Handle_qMemoryRegionInfo (StringExtractorGDBRemote &packet);
215 
216     PacketResult
217     Handle_Z (StringExtractorGDBRemote &packet);
218 
219     PacketResult
220     Handle_z (StringExtractorGDBRemote &packet);
221 
222     PacketResult
223     Handle_s (StringExtractorGDBRemote &packet);
224 
225     PacketResult
226     Handle_qXfer_auxv_read (StringExtractorGDBRemote &packet);
227 
228     PacketResult
229     Handle_QSaveRegisterState (StringExtractorGDBRemote &packet);
230 
231     PacketResult
232     Handle_QRestoreRegisterState (StringExtractorGDBRemote &packet);
233 
234     PacketResult
235     Handle_vAttach (StringExtractorGDBRemote &packet);
236 
237     PacketResult
238     Handle_D (StringExtractorGDBRemote &packet);
239 
240     PacketResult
241     Handle_qThreadStopInfo (StringExtractorGDBRemote &packet);
242 
243     PacketResult
244     Handle_jThreadsInfo (StringExtractorGDBRemote &packet);
245 
246     PacketResult
247     Handle_qWatchpointSupportInfo (StringExtractorGDBRemote &packet);
248 
249     PacketResult
250     Handle_qFileLoadAddress (StringExtractorGDBRemote &packet);
251 
252     void
253     SetCurrentThreadID (lldb::tid_t tid);
254 
255     lldb::tid_t
256     GetCurrentThreadID () const;
257 
258     void
259     SetContinueThreadID (lldb::tid_t tid);
260 
261     lldb::tid_t
262     GetContinueThreadID () const { return m_continue_tid; }
263 
264     Error
265     SetSTDIOFileDescriptor (int fd);
266 
267     FileSpec
268     FindModuleFile (const std::string& module_path, const ArchSpec& arch) override;
269 
270 private:
271     void
272     HandleInferiorState_Exited (NativeProcessProtocol *process);
273 
274     void
275     HandleInferiorState_Stopped (NativeProcessProtocol *process);
276 
277     NativeThreadProtocolSP
278     GetThreadFromSuffix (StringExtractorGDBRemote &packet);
279 
280     uint32_t
281     GetNextSavedRegistersID ();
282 
283     void
284     MaybeCloseInferiorTerminalConnection ();
285 
286     void
287     ClearProcessSpecificData ();
288 
289     void
290     RegisterPacketHandlers ();
291 
292     void
293     DataAvailableCallback ();
294 
295     void
296     SendProcessOutput ();
297 
298     void
299     StartSTDIOForwarding();
300 
301     void
302     StopSTDIOForwarding();
303 
304     //------------------------------------------------------------------
305     // For GDBRemoteCommunicationServerLLGS only
306     //------------------------------------------------------------------
307     DISALLOW_COPY_AND_ASSIGN (GDBRemoteCommunicationServerLLGS);
308 };
309 
310 } // namespace process_gdb_remote
311 } // namespace lldb_private
312 
313 #endif  // liblldb_GDBRemoteCommunicationServerLLGS_h_
314