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 <unordered_map>
16 
17 // Other libraries and framework includes
18 #include "lldb/lldb-private-forward.h"
19 #include "lldb/Core/Communication.h"
20 #include "lldb/Host/Mutex.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(const lldb::PlatformSP& platform_sp, 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     lldb::PlatformSP m_platform_sp;
118     MainLoop &m_mainloop;
119     MainLoop::ReadHandleUP m_network_handle_up;
120     lldb::tid_t m_current_tid;
121     lldb::tid_t m_continue_tid;
122     Mutex m_debugged_process_mutex;
123     NativeProcessProtocolSP m_debugged_process_sp;
124 
125     Communication m_stdio_communication;
126     MainLoop::ReadHandleUP m_stdio_handle_up;
127 
128     lldb::StateType m_inferior_prev_state;
129     lldb::DataBufferSP m_active_auxv_buffer_sp;
130     Mutex m_saved_registers_mutex;
131     std::unordered_map<uint32_t, lldb::DataBufferSP> m_saved_registers_map;
132     uint32_t m_next_saved_registers_id;
133     bool m_handshake_completed : 1;
134 
135     PacketResult
136     SendONotification (const char *buffer, uint32_t len);
137 
138     PacketResult
139     SendWResponse (NativeProcessProtocol *process);
140 
141     PacketResult
142     SendStopReplyPacketForThread (lldb::tid_t tid);
143 
144     PacketResult
145     SendStopReasonForState (lldb::StateType process_state);
146 
147     PacketResult
148     Handle_k (StringExtractorGDBRemote &packet);
149 
150     PacketResult
151     Handle_qProcessInfo (StringExtractorGDBRemote &packet);
152 
153     PacketResult
154     Handle_qC (StringExtractorGDBRemote &packet);
155 
156     PacketResult
157     Handle_QSetDisableASLR (StringExtractorGDBRemote &packet);
158 
159     PacketResult
160     Handle_QSetWorkingDir (StringExtractorGDBRemote &packet);
161 
162     PacketResult
163     Handle_qGetWorkingDir (StringExtractorGDBRemote &packet);
164 
165     PacketResult
166     Handle_C (StringExtractorGDBRemote &packet);
167 
168     PacketResult
169     Handle_c (StringExtractorGDBRemote &packet);
170 
171     PacketResult
172     Handle_vCont (StringExtractorGDBRemote &packet);
173 
174     PacketResult
175     Handle_vCont_actions (StringExtractorGDBRemote &packet);
176 
177     PacketResult
178     Handle_stop_reason (StringExtractorGDBRemote &packet);
179 
180     PacketResult
181     Handle_qRegisterInfo (StringExtractorGDBRemote &packet);
182 
183     PacketResult
184     Handle_qfThreadInfo (StringExtractorGDBRemote &packet);
185 
186     PacketResult
187     Handle_qsThreadInfo (StringExtractorGDBRemote &packet);
188 
189     PacketResult
190     Handle_p (StringExtractorGDBRemote &packet);
191 
192     PacketResult
193     Handle_P (StringExtractorGDBRemote &packet);
194 
195     PacketResult
196     Handle_H (StringExtractorGDBRemote &packet);
197 
198     PacketResult
199     Handle_I (StringExtractorGDBRemote &packet);
200 
201     PacketResult
202     Handle_interrupt (StringExtractorGDBRemote &packet);
203 
204     // Handles $m and $x packets.
205     PacketResult
206     Handle_memory_read (StringExtractorGDBRemote &packet);
207 
208     PacketResult
209     Handle_M (StringExtractorGDBRemote &packet);
210 
211     PacketResult
212     Handle_qMemoryRegionInfoSupported (StringExtractorGDBRemote &packet);
213 
214     PacketResult
215     Handle_qMemoryRegionInfo (StringExtractorGDBRemote &packet);
216 
217     PacketResult
218     Handle_Z (StringExtractorGDBRemote &packet);
219 
220     PacketResult
221     Handle_z (StringExtractorGDBRemote &packet);
222 
223     PacketResult
224     Handle_s (StringExtractorGDBRemote &packet);
225 
226     PacketResult
227     Handle_qXfer_auxv_read (StringExtractorGDBRemote &packet);
228 
229     PacketResult
230     Handle_QSaveRegisterState (StringExtractorGDBRemote &packet);
231 
232     PacketResult
233     Handle_QRestoreRegisterState (StringExtractorGDBRemote &packet);
234 
235     PacketResult
236     Handle_vAttach (StringExtractorGDBRemote &packet);
237 
238     PacketResult
239     Handle_D (StringExtractorGDBRemote &packet);
240 
241     PacketResult
242     Handle_qThreadStopInfo (StringExtractorGDBRemote &packet);
243 
244     PacketResult
245     Handle_jThreadsInfo (StringExtractorGDBRemote &packet);
246 
247     PacketResult
248     Handle_qWatchpointSupportInfo (StringExtractorGDBRemote &packet);
249 
250     PacketResult
251     Handle_qFileLoadAddress (StringExtractorGDBRemote &packet);
252 
253     void
254     SetCurrentThreadID (lldb::tid_t tid);
255 
256     lldb::tid_t
257     GetCurrentThreadID () const;
258 
259     void
260     SetContinueThreadID (lldb::tid_t tid);
261 
262     lldb::tid_t
263     GetContinueThreadID () const { return m_continue_tid; }
264 
265     Error
266     SetSTDIOFileDescriptor (int fd);
267 
268     FileSpec
269     FindModuleFile (const std::string& module_path, const ArchSpec& arch) override;
270 
271 private:
272     void
273     HandleInferiorState_Exited (NativeProcessProtocol *process);
274 
275     void
276     HandleInferiorState_Stopped (NativeProcessProtocol *process);
277 
278     NativeThreadProtocolSP
279     GetThreadFromSuffix (StringExtractorGDBRemote &packet);
280 
281     uint32_t
282     GetNextSavedRegistersID ();
283 
284     void
285     MaybeCloseInferiorTerminalConnection ();
286 
287     void
288     ClearProcessSpecificData ();
289 
290     void
291     RegisterPacketHandlers ();
292 
293     void
294     DataAvailableCallback ();
295 
296     void
297     SendProcessOutput ();
298 
299     void
300     StartSTDIOForwarding();
301 
302     void
303     StopSTDIOForwarding();
304 
305     //------------------------------------------------------------------
306     // For GDBRemoteCommunicationServerLLGS only
307     //------------------------------------------------------------------
308     DISALLOW_COPY_AND_ASSIGN (GDBRemoteCommunicationServerLLGS);
309 };
310 
311 } // namespace process_gdb_remote
312 } // namespace lldb_private
313 
314 #endif  // liblldb_GDBRemoteCommunicationServerLLGS_h_
315