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