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     virtual
46     ~GDBRemoteCommunicationServerLLGS();
47 
48     //------------------------------------------------------------------
49     /// Specify the program to launch and its arguments.
50     ///
51     /// @param[in] args
52     ///     The command line to launch.
53     ///
54     /// @param[in] argc
55     ///     The number of elements in the args array of cstring pointers.
56     ///
57     /// @return
58     ///     An Error object indicating the success or failure of making
59     ///     the setting.
60     //------------------------------------------------------------------
61     Error
62     SetLaunchArguments (const char *const args[], int argc);
63 
64     //------------------------------------------------------------------
65     /// Specify the launch flags for the process.
66     ///
67     /// @param[in] launch_flags
68     ///     The launch flags to use when launching this process.
69     ///
70     /// @return
71     ///     An Error object indicating the success or failure of making
72     ///     the setting.
73     //------------------------------------------------------------------
74     Error
75     SetLaunchFlags (unsigned int launch_flags);
76 
77     //------------------------------------------------------------------
78     /// Launch a process with the current launch settings.
79     ///
80     /// This method supports running an lldb-gdbserver or similar
81     /// server in a situation where the startup code has been provided
82     /// with all the information for a child process to be launched.
83     ///
84     /// @return
85     ///     An Error object indicating the success or failure of the
86     ///     launch.
87     //------------------------------------------------------------------
88     Error
89     LaunchProcess () override;
90 
91     //------------------------------------------------------------------
92     /// Attach to a process.
93     ///
94     /// This method supports attaching llgs to a process accessible via the
95     /// configured Platform.
96     ///
97     /// @return
98     ///     An Error object indicating the success or failure of the
99     ///     attach operation.
100     //------------------------------------------------------------------
101     Error
102     AttachToProcess (lldb::pid_t pid);
103 
104     //------------------------------------------------------------------
105     // NativeProcessProtocol::NativeDelegate overrides
106     //------------------------------------------------------------------
107     void
108     InitializeDelegate (NativeProcessProtocol *process) override;
109 
110     void
111     ProcessStateChanged (NativeProcessProtocol *process, lldb::StateType state) override;
112 
113     void
114     DidExec (NativeProcessProtocol *process) override;
115 
116     Error
117     InitializeConnection (std::unique_ptr<Connection> &&connection);
118 
119 protected:
120     lldb::PlatformSP m_platform_sp;
121     MainLoop &m_mainloop;
122     MainLoop::ReadHandleUP m_read_handle_up;
123     lldb::tid_t m_current_tid;
124     lldb::tid_t m_continue_tid;
125     Mutex m_debugged_process_mutex;
126     NativeProcessProtocolSP m_debugged_process_sp;
127     Communication m_stdio_communication;
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, bool flush_on_exit);
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     PacketResult
205     Handle_m (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_qWatchpointSupportInfo (StringExtractorGDBRemote &packet);
245 
246     PacketResult
247     Handle_qFileLoadAddress (StringExtractorGDBRemote &packet);
248 
249     void
250     SetCurrentThreadID (lldb::tid_t tid);
251 
252     lldb::tid_t
253     GetCurrentThreadID () const;
254 
255     void
256     SetContinueThreadID (lldb::tid_t tid);
257 
258     lldb::tid_t
259     GetContinueThreadID () const { return m_continue_tid; }
260 
261     Error
262     SetSTDIOFileDescriptor (int fd);
263 
264     static void
265     STDIOReadThreadBytesReceived (void *baton, const void *src, size_t src_len);
266 
267     FileSpec
268     FindModuleFile (const std::string& module_path, const ArchSpec& arch) override;
269 
270 private:
271     bool
272     DebuggedProcessReaped (lldb::pid_t pid);
273 
274     static bool
275     ReapDebuggedProcess (void *callback_baton,
276                          lldb::pid_t pid,
277                          bool exited,
278                          int signal,
279                          int status);
280 
281     void
282     HandleInferiorState_Exited (NativeProcessProtocol *process);
283 
284     void
285     HandleInferiorState_Stopped (NativeProcessProtocol *process);
286 
287     void
288     FlushInferiorOutput ();
289 
290     NativeThreadProtocolSP
291     GetThreadFromSuffix (StringExtractorGDBRemote &packet);
292 
293     uint32_t
294     GetNextSavedRegistersID ();
295 
296     void
297     MaybeCloseInferiorTerminalConnection ();
298 
299     void
300     ClearProcessSpecificData ();
301 
302     void
303     RegisterPacketHandlers ();
304 
305     void
306     DataAvailableCallback ();
307 
308     //------------------------------------------------------------------
309     // For GDBRemoteCommunicationServerLLGS only
310     //------------------------------------------------------------------
311     DISALLOW_COPY_AND_ASSIGN (GDBRemoteCommunicationServerLLGS);
312 };
313 
314 } // namespace process_gdb_remote
315 } // namespace lldb_private
316 
317 #endif  // liblldb_GDBRemoteCommunicationServerLLGS_h_
318