1 //===-- GDBRemoteCommunicationClient.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_GDBRemoteCommunicationClient_h_
11 #define liblldb_GDBRemoteCommunicationClient_h_
12 
13 // C Includes
14 // C++ Includes
15 #include <vector>
16 
17 // Other libraries and framework includes
18 // Project includes
19 #include "lldb/Core/ArchSpec.h"
20 
21 #include "GDBRemoteCommunication.h"
22 
23 typedef enum
24 {
25     eBreakpointSoftware = 0,
26     eBreakpointHardware,
27     eWatchpointWrite,
28     eWatchpointRead,
29     eWatchpointReadWrite
30 } GDBStoppointType;
31 
32 class GDBRemoteCommunicationClient : public GDBRemoteCommunication
33 {
34 public:
35     //------------------------------------------------------------------
36     // Constructors and Destructors
37     //------------------------------------------------------------------
38     GDBRemoteCommunicationClient(bool is_platform);
39 
40     virtual
41     ~GDBRemoteCommunicationClient();
42 
43     //------------------------------------------------------------------
44     // After connecting, send the handshake to the server to make sure
45     // we are communicating with it.
46     //------------------------------------------------------------------
47     bool
48     HandshakeWithServer (lldb_private::Error *error_ptr);
49 
50     size_t
51     SendPacketAndWaitForResponse (const char *send_payload,
52                                   StringExtractorGDBRemote &response,
53                                   bool send_async);
54 
55     size_t
56     SendPacketAndWaitForResponse (const char *send_payload,
57                                   size_t send_length,
58                                   StringExtractorGDBRemote &response,
59                                   bool send_async);
60 
61     lldb::StateType
62     SendContinuePacketAndWaitForResponse (ProcessGDBRemote *process,
63                                           const char *packet_payload,
64                                           size_t packet_length,
65                                           StringExtractorGDBRemote &response);
66 
67     virtual bool
68     GetThreadSuffixSupported ();
69 
70     void
71     QueryNoAckModeSupported ();
72 
73     bool
74     SendAsyncSignal (int signo);
75 
76     bool
77     SendInterrupt (lldb_private::Mutex::Locker &locker,
78                    uint32_t seconds_to_wait_for_stop,
79                    bool &sent_interrupt,
80                    bool &timed_out);
81 
82     lldb::pid_t
83     GetCurrentProcessID ();
84 
85     bool
86     GetLaunchSuccess (std::string &error_str);
87 
88     uint16_t
89     LaunchGDBserverAndGetPort ();
90 
91     //------------------------------------------------------------------
92     /// Sends a GDB remote protocol 'A' packet that delivers program
93     /// arguments to the remote server.
94     ///
95     /// @param[in] argv
96     ///     A NULL terminated array of const C strings to use as the
97     ///     arguments.
98     ///
99     /// @return
100     ///     Zero if the response was "OK", a positive value if the
101     ///     the response was "Exx" where xx are two hex digits, or
102     ///     -1 if the call is unsupported or any other unexpected
103     ///     response was received.
104     //------------------------------------------------------------------
105     int
106     SendArgumentsPacket (char const *argv[]);
107 
108     //------------------------------------------------------------------
109     /// Sends a "QEnvironment:NAME=VALUE" packet that will build up the
110     /// environment that will get used when launching an application
111     /// in conjunction with the 'A' packet. This function can be called
112     /// multiple times in a row in order to pass on the desired
113     /// environment that the inferior should be launched with.
114     ///
115     /// @param[in] name_equal_value
116     ///     A NULL terminated C string that contains a single environment
117     ///     in the format "NAME=VALUE".
118     ///
119     /// @return
120     ///     Zero if the response was "OK", a positive value if the
121     ///     the response was "Exx" where xx are two hex digits, or
122     ///     -1 if the call is unsupported or any other unexpected
123     ///     response was received.
124     //------------------------------------------------------------------
125     int
126     SendEnvironmentPacket (char const *name_equal_value);
127 
128     int
129     SendLaunchArchPacket (const char *arch);
130     //------------------------------------------------------------------
131     /// Sends a "vAttach:PID" where PID is in hex.
132     ///
133     /// @param[in] pid
134     ///     A process ID for the remote gdb server to attach to.
135     ///
136     /// @param[out] response
137     ///     The response received from the gdb server. If the return
138     ///     value is zero, \a response will contain a stop reply
139     ///     packet.
140     ///
141     /// @return
142     ///     Zero if the attach was successful, or an error indicating
143     ///     an error code.
144     //------------------------------------------------------------------
145     int
146     SendAttach (lldb::pid_t pid,
147                 StringExtractorGDBRemote& response);
148 
149 
150     //------------------------------------------------------------------
151     /// Sets the path to use for stdin/out/err for a process
152     /// that will be launched with the 'A' packet.
153     ///
154     /// @param[in] path
155     ///     The path to use for stdin/out/err
156     ///
157     /// @return
158     ///     Zero if the for success, or an error code for failure.
159     //------------------------------------------------------------------
160     int
161     SetSTDIN (char const *path);
162     int
163     SetSTDOUT (char const *path);
164     int
165     SetSTDERR (char const *path);
166 
167     //------------------------------------------------------------------
168     /// Sets the disable ASLR flag to \a enable for a process that will
169     /// be launched with the 'A' packet.
170     ///
171     /// @param[in] enable
172     ///     A boolean value indicating wether to disable ASLR or not.
173     ///
174     /// @return
175     ///     Zero if the for success, or an error code for failure.
176     //------------------------------------------------------------------
177     int
178     SetDisableASLR (bool enable);
179 
180     //------------------------------------------------------------------
181     /// Sets the working directory to \a path for a process that will
182     /// be launched with the 'A' packet.
183     ///
184     /// @param[in] path
185     ///     The path to a directory to use when launching our processs
186     ///
187     /// @return
188     ///     Zero if the for success, or an error code for failure.
189     //------------------------------------------------------------------
190     int
191     SetWorkingDir (char const *path);
192 
193     lldb::addr_t
194     AllocateMemory (size_t size, uint32_t permissions);
195 
196     bool
197     DeallocateMemory (lldb::addr_t addr);
198 
199     const lldb_private::ArchSpec &
200     GetHostArchitecture ();
201 
202     bool
203     GetVContSupported (char flavor);
204 
205     void
206     ResetDiscoverableSettings();
207 
208     bool
209     GetHostInfo (bool force = false);
210 
211     bool
212     GetOSVersion (uint32_t &major,
213                   uint32_t &minor,
214                   uint32_t &update);
215 
216     bool
217     GetOSBuildString (std::string &s);
218 
219     bool
220     GetOSKernelDescription (std::string &s);
221 
222     lldb_private::ArchSpec
223     GetSystemArchitecture ();
224 
225     bool
226     GetHostname (std::string &s);
227 
228     bool
229     GetSupportsThreadSuffix ();
230 
231     bool
232     GetProcessInfo (lldb::pid_t pid,
233                     lldb_private::ProcessInstanceInfo &process_info);
234 
235     uint32_t
236     FindProcesses (const lldb_private::ProcessInstanceInfoMatch &process_match_info,
237                    lldb_private::ProcessInstanceInfoList &process_infos);
238 
239     bool
240     GetUserName (uint32_t uid, std::string &name);
241 
242     bool
243     GetGroupName (uint32_t gid, std::string &name);
244 
245     bool
246     HasFullVContSupport ()
247     {
248         return GetVContSupported ('A');
249     }
250 
251     bool
252     HasAnyVContSupport ()
253     {
254         return GetVContSupported ('a');
255     }
256 
257     uint32_t
258     SetPacketTimeout (uint32_t packet_timeout)
259     {
260         const uint32_t old_packet_timeout = m_packet_timeout;
261         m_packet_timeout = packet_timeout;
262         return old_packet_timeout;
263     }
264 
265     bool
266     GetStopReply (StringExtractorGDBRemote &response);
267 
268     bool
269     GetThreadStopInfo (uint32_t tid,
270                        StringExtractorGDBRemote &response);
271 
272     bool
273     SupportsGDBStoppointPacket (GDBStoppointType type)
274     {
275         switch (type)
276         {
277         case eBreakpointSoftware:   return m_supports_z0;
278         case eBreakpointHardware:   return m_supports_z1;
279         case eWatchpointWrite:      return m_supports_z2;
280         case eWatchpointRead:       return m_supports_z3;
281         case eWatchpointReadWrite:  return m_supports_z4;
282         default:                    break;
283         }
284         return false;
285     }
286     uint8_t
287     SendGDBStoppointTypePacket (GDBStoppointType type,   // Type of breakpoint or watchpoint
288                                 bool insert,              // Insert or remove?
289                                 lldb::addr_t addr,        // Address of breakpoint or watchpoint
290                                 uint32_t length);         // Byte Size of breakpoint or watchpoint
291 
292     void
293     TestPacketSpeed (const uint32_t num_packets);
294 
295     // This packet is for testing the speed of the interface only. Both
296     // the client and server need to support it, but this allows us to
297     // measure the packet speed without any other work being done on the
298     // other end and avoids any of that work affecting the packet send
299     // and response times.
300     bool
301     SendSpeedTestPacket (uint32_t send_size,
302                          uint32_t recv_size);
303 
304     bool
305     SetCurrentThread (int tid);
306 
307     bool
308     SetCurrentThreadForRun (int tid);
309 
310     lldb_private::LazyBool
311     SupportsAllocDeallocMemory () const
312     {
313         return m_supports_alloc_dealloc_memory;
314     }
315 
316     size_t
317     GetCurrentThreadIDs (std::vector<lldb::tid_t> &thread_ids,
318                          bool &sequence_mutex_unavailable);
319 
320 protected:
321 
322     //------------------------------------------------------------------
323     // Classes that inherit from GDBRemoteCommunicationClient can see and modify these
324     //------------------------------------------------------------------
325     lldb_private::LazyBool m_supports_not_sending_acks;
326     lldb_private::LazyBool m_supports_thread_suffix;
327     lldb_private::LazyBool m_supports_vCont_all;
328     lldb_private::LazyBool m_supports_vCont_any;
329     lldb_private::LazyBool m_supports_vCont_c;
330     lldb_private::LazyBool m_supports_vCont_C;
331     lldb_private::LazyBool m_supports_vCont_s;
332     lldb_private::LazyBool m_supports_vCont_S;
333     lldb_private::LazyBool m_qHostInfo_is_valid;
334     lldb_private::LazyBool m_supports_alloc_dealloc_memory;
335 
336     bool
337         m_supports_qProcessInfoPID:1,
338         m_supports_qfProcessInfo:1,
339         m_supports_qUserName:1,
340         m_supports_qGroupName:1,
341         m_supports_qThreadStopInfo:1,
342         m_supports_z0:1,
343         m_supports_z1:1,
344         m_supports_z2:1,
345         m_supports_z3:1,
346         m_supports_z4:1;
347 
348 
349     lldb::tid_t m_curr_tid;         // Current gdb remote protocol thread index for all other operations
350     lldb::tid_t m_curr_tid_run;     // Current gdb remote protocol thread index for continue, step, etc
351 
352 
353     // If we need to send a packet while the target is running, the m_async_XXX
354     // member variables take care of making this happen.
355     lldb_private::Mutex m_async_mutex;
356     lldb_private::Predicate<bool> m_async_packet_predicate;
357     std::string m_async_packet;
358     StringExtractorGDBRemote m_async_response;
359     int m_async_signal; // We were asked to deliver a signal to the inferior process.
360 
361     lldb_private::ArchSpec m_host_arch;
362     uint32_t m_os_version_major;
363     uint32_t m_os_version_minor;
364     uint32_t m_os_version_update;
365     std::string m_os_build;
366     std::string m_os_kernel;
367     std::string m_hostname;
368 
369     bool
370     DecodeProcessInfoResponse (StringExtractorGDBRemote &response,
371                                lldb_private::ProcessInstanceInfo &process_info);
372 private:
373     //------------------------------------------------------------------
374     // For GDBRemoteCommunicationClient only
375     //------------------------------------------------------------------
376     DISALLOW_COPY_AND_ASSIGN (GDBRemoteCommunicationClient);
377 };
378 
379 #endif  // liblldb_GDBRemoteCommunicationClient_h_
380