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