GDBRemoteCommunicationClient.h revision 262528
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
24typedef enum
25{
26    eBreakpointSoftware = 0,
27    eBreakpointHardware,
28    eWatchpointWrite,
29    eWatchpointRead,
30    eWatchpointReadWrite
31} GDBStoppointType;
32
33class GDBRemoteCommunicationClient : public GDBRemoteCommunication
34{
35public:
36    //------------------------------------------------------------------
37    // Constructors and Destructors
38    //------------------------------------------------------------------
39    GDBRemoteCommunicationClient(bool is_platform);
40
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    PacketResult
51    SendPacketAndWaitForResponse (const char *send_payload,
52                                  StringExtractorGDBRemote &response,
53                                  bool send_async);
54
55    PacketResult
56    SendPacketAndWaitForResponse (const char *send_payload,
57                                  size_t send_length,
58                                  StringExtractorGDBRemote &response,
59                                  bool send_async);
60
61    // For packets which specify a range of output to be returned,
62    // return all of the output via a series of request packets of the form
63    // <prefix>0,<size>
64    // <prefix><size>,<size>
65    // <prefix><size>*2,<size>
66    // <prefix><size>*3,<size>
67    // ...
68    // until a "$l..." packet is received, indicating the end.
69    // (size is in hex; this format is used by a standard gdbserver to
70    // return the given portion of the output specified by <prefix>;
71    // for example, "qXfer:libraries-svr4:read::fff,1000" means
72    // "return a chunk of the xml description file for shared
73    // library load addresses, where the chunk starts at offset 0xfff
74    // and continues for 0x1000 bytes").
75    // Concatenate the resulting server response packets together and
76    // return in response_string.  If any packet fails, the return value
77    // indicates that failure and the returned string value is undefined.
78    PacketResult
79    SendPacketsAndConcatenateResponses (const char *send_payload_prefix,
80                                        std::string &response_string);
81
82    lldb::StateType
83    SendContinuePacketAndWaitForResponse (ProcessGDBRemote *process,
84                                          const char *packet_payload,
85                                          size_t packet_length,
86                                          StringExtractorGDBRemote &response);
87
88    bool
89    GetThreadSuffixSupported ();
90
91    // This packet is usually sent first and the boolean return value
92    // indicates if the packet was send and any response was received
93    // even in the response is UNIMPLEMENTED. If the packet failed to
94    // get a response, then false is returned. This quickly tells us
95    // if we were able to connect and communicte with the remote GDB
96    // server
97    bool
98    QueryNoAckModeSupported ();
99
100    void
101    GetListThreadsInStopReplySupported ();
102
103    bool
104    SendAsyncSignal (int signo);
105
106    bool
107    SendInterrupt (lldb_private::Mutex::Locker &locker,
108                   uint32_t seconds_to_wait_for_stop,
109                   bool &timed_out);
110
111    lldb::pid_t
112    GetCurrentProcessID ();
113
114    bool
115    GetLaunchSuccess (std::string &error_str);
116
117    uint16_t
118    LaunchGDBserverAndGetPort (lldb::pid_t &pid, const char *remote_accept_hostname);
119
120    bool
121    KillSpawnedProcess (lldb::pid_t pid);
122
123    //------------------------------------------------------------------
124    /// Sends a GDB remote protocol 'A' packet that delivers program
125    /// arguments to the remote server.
126    ///
127    /// @param[in] argv
128    ///     A NULL terminated array of const C strings to use as the
129    ///     arguments.
130    ///
131    /// @return
132    ///     Zero if the response was "OK", a positive value if the
133    ///     the response was "Exx" where xx are two hex digits, or
134    ///     -1 if the call is unsupported or any other unexpected
135    ///     response was received.
136    //------------------------------------------------------------------
137    int
138    SendArgumentsPacket (const lldb_private::ProcessLaunchInfo &launch_info);
139
140    //------------------------------------------------------------------
141    /// Sends a "QEnvironment:NAME=VALUE" packet that will build up the
142    /// environment that will get used when launching an application
143    /// in conjunction with the 'A' packet. This function can be called
144    /// multiple times in a row in order to pass on the desired
145    /// environment that the inferior should be launched with.
146    ///
147    /// @param[in] name_equal_value
148    ///     A NULL terminated C string that contains a single environment
149    ///     in the format "NAME=VALUE".
150    ///
151    /// @return
152    ///     Zero if the response was "OK", a positive value if the
153    ///     the response was "Exx" where xx are two hex digits, or
154    ///     -1 if the call is unsupported or any other unexpected
155    ///     response was received.
156    //------------------------------------------------------------------
157    int
158    SendEnvironmentPacket (char const *name_equal_value);
159
160    int
161    SendLaunchArchPacket (const char *arch);
162    //------------------------------------------------------------------
163    /// Sends a "vAttach:PID" where PID is in hex.
164    ///
165    /// @param[in] pid
166    ///     A process ID for the remote gdb server to attach to.
167    ///
168    /// @param[out] response
169    ///     The response received from the gdb server. If the return
170    ///     value is zero, \a response will contain a stop reply
171    ///     packet.
172    ///
173    /// @return
174    ///     Zero if the attach was successful, or an error indicating
175    ///     an error code.
176    //------------------------------------------------------------------
177    int
178    SendAttach (lldb::pid_t pid,
179                StringExtractorGDBRemote& response);
180
181
182    //------------------------------------------------------------------
183    /// Sets the path to use for stdin/out/err for a process
184    /// that will be launched with the 'A' packet.
185    ///
186    /// @param[in] path
187    ///     The path to use for stdin/out/err
188    ///
189    /// @return
190    ///     Zero if the for success, or an error code for failure.
191    //------------------------------------------------------------------
192    int
193    SetSTDIN (char const *path);
194    int
195    SetSTDOUT (char const *path);
196    int
197    SetSTDERR (char const *path);
198
199    //------------------------------------------------------------------
200    /// Sets the disable ASLR flag to \a enable for a process that will
201    /// be launched with the 'A' packet.
202    ///
203    /// @param[in] enable
204    ///     A boolean value indicating wether to disable ASLR or not.
205    ///
206    /// @return
207    ///     Zero if the for success, or an error code for failure.
208    //------------------------------------------------------------------
209    int
210    SetDisableASLR (bool enable);
211
212    //------------------------------------------------------------------
213    /// Sets the working directory to \a path for a process that will
214    /// be launched with the 'A' packet for non platform based
215    /// connections. If this packet is sent to a GDB server that
216    /// implements the platform, it will change the current working
217    /// directory for the platform process.
218    ///
219    /// @param[in] path
220    ///     The path to a directory to use when launching our processs
221    ///
222    /// @return
223    ///     Zero if the for success, or an error code for failure.
224    //------------------------------------------------------------------
225    int
226    SetWorkingDir (char const *path);
227
228    //------------------------------------------------------------------
229    /// Gets the current working directory of a remote platform GDB
230    /// server.
231    ///
232    /// @param[out] cwd
233    ///     The current working directory on the remote platform.
234    ///
235    /// @return
236    ///     Boolean for success
237    //------------------------------------------------------------------
238    bool
239    GetWorkingDir (std::string &cwd);
240
241    lldb::addr_t
242    AllocateMemory (size_t size, uint32_t permissions);
243
244    bool
245    DeallocateMemory (lldb::addr_t addr);
246
247    lldb_private::Error
248    Detach (bool keep_stopped);
249
250    lldb_private::Error
251    GetMemoryRegionInfo (lldb::addr_t addr,
252                        lldb_private::MemoryRegionInfo &range_info);
253
254    lldb_private::Error
255    GetWatchpointSupportInfo (uint32_t &num);
256
257    lldb_private::Error
258    GetWatchpointSupportInfo (uint32_t &num, bool& after);
259
260    lldb_private::Error
261    GetWatchpointsTriggerAfterInstruction (bool &after);
262
263    const lldb_private::ArchSpec &
264    GetHostArchitecture ();
265
266    uint32_t
267    GetHostDefaultPacketTimeout();
268
269    const lldb_private::ArchSpec &
270    GetProcessArchitecture ();
271
272    void
273    GetRemoteQSupported();
274
275    bool
276    GetVContSupported (char flavor);
277
278    bool
279    GetpPacketSupported (lldb::tid_t tid);
280
281    bool
282    GetVAttachOrWaitSupported ();
283
284    bool
285    GetSyncThreadStateSupported();
286
287    void
288    ResetDiscoverableSettings();
289
290    bool
291    GetHostInfo (bool force = false);
292
293    bool
294    GetOSVersion (uint32_t &major,
295                  uint32_t &minor,
296                  uint32_t &update);
297
298    bool
299    GetOSBuildString (std::string &s);
300
301    bool
302    GetOSKernelDescription (std::string &s);
303
304    lldb_private::ArchSpec
305    GetSystemArchitecture ();
306
307    bool
308    GetHostname (std::string &s);
309
310    lldb::addr_t
311    GetShlibInfoAddr();
312
313    bool
314    GetSupportsThreadSuffix ();
315
316    bool
317    GetProcessInfo (lldb::pid_t pid,
318                    lldb_private::ProcessInstanceInfo &process_info);
319
320    uint32_t
321    FindProcesses (const lldb_private::ProcessInstanceInfoMatch &process_match_info,
322                   lldb_private::ProcessInstanceInfoList &process_infos);
323
324    bool
325    GetUserName (uint32_t uid, std::string &name);
326
327    bool
328    GetGroupName (uint32_t gid, std::string &name);
329
330    bool
331    HasFullVContSupport ()
332    {
333        return GetVContSupported ('A');
334    }
335
336    bool
337    HasAnyVContSupport ()
338    {
339        return GetVContSupported ('a');
340    }
341
342    bool
343    GetStopReply (StringExtractorGDBRemote &response);
344
345    bool
346    GetThreadStopInfo (lldb::tid_t tid,
347                       StringExtractorGDBRemote &response);
348
349    bool
350    SupportsGDBStoppointPacket (GDBStoppointType type)
351    {
352        switch (type)
353        {
354        case eBreakpointSoftware:   return m_supports_z0;
355        case eBreakpointHardware:   return m_supports_z1;
356        case eWatchpointWrite:      return m_supports_z2;
357        case eWatchpointRead:       return m_supports_z3;
358        case eWatchpointReadWrite:  return m_supports_z4;
359        }
360        return false;
361    }
362    uint8_t
363    SendGDBStoppointTypePacket (GDBStoppointType type,   // Type of breakpoint or watchpoint
364                                bool insert,              // Insert or remove?
365                                lldb::addr_t addr,        // Address of breakpoint or watchpoint
366                                uint32_t length);         // Byte Size of breakpoint or watchpoint
367
368    void
369    TestPacketSpeed (const uint32_t num_packets);
370
371    // This packet is for testing the speed of the interface only. Both
372    // the client and server need to support it, but this allows us to
373    // measure the packet speed without any other work being done on the
374    // other end and avoids any of that work affecting the packet send
375    // and response times.
376    bool
377    SendSpeedTestPacket (uint32_t send_size,
378                         uint32_t recv_size);
379
380    bool
381    SetCurrentThread (uint64_t tid);
382
383    bool
384    SetCurrentThreadForRun (uint64_t tid);
385
386    bool
387    GetQXferLibrariesReadSupported ();
388
389    bool
390    GetQXferLibrariesSVR4ReadSupported ();
391
392    uint64_t
393    GetRemoteMaxPacketSize();
394
395    bool
396    GetAugmentedLibrariesSVR4ReadSupported ();
397
398    lldb_private::LazyBool
399    SupportsAllocDeallocMemory () // const
400    {
401        // Uncomment this to have lldb pretend the debug server doesn't respond to alloc/dealloc memory packets.
402        // m_supports_alloc_dealloc_memory = lldb_private::eLazyBoolNo;
403        return m_supports_alloc_dealloc_memory;
404    }
405
406    size_t
407    GetCurrentThreadIDs (std::vector<lldb::tid_t> &thread_ids,
408                         bool &sequence_mutex_unavailable);
409
410    bool
411    GetInterruptWasSent () const
412    {
413        return m_interrupt_sent;
414    }
415
416    lldb::user_id_t
417    OpenFile (const lldb_private::FileSpec& file_spec,
418              uint32_t flags,
419              mode_t mode,
420              lldb_private::Error &error);
421
422    bool
423    CloseFile (lldb::user_id_t fd,
424               lldb_private::Error &error);
425
426    lldb::user_id_t
427    GetFileSize (const lldb_private::FileSpec& file_spec);
428
429    lldb_private::Error
430    GetFilePermissions(const char *path, uint32_t &file_permissions);
431
432    lldb_private::Error
433    SetFilePermissions(const char *path, uint32_t file_permissions);
434
435    uint64_t
436    ReadFile (lldb::user_id_t fd,
437              uint64_t offset,
438              void *dst,
439              uint64_t dst_len,
440              lldb_private::Error &error);
441
442    uint64_t
443    WriteFile (lldb::user_id_t fd,
444               uint64_t offset,
445               const void* src,
446               uint64_t src_len,
447               lldb_private::Error &error);
448
449    lldb_private::Error
450    CreateSymlink (const char *src,
451                   const char *dst);
452
453    lldb_private::Error
454    Unlink (const char *path);
455
456    lldb_private::Error
457    MakeDirectory (const char *path,
458                   uint32_t mode);
459
460    bool
461    GetFileExists (const lldb_private::FileSpec& file_spec);
462
463    lldb_private::Error
464    RunShellCommand (const char *command,           // Shouldn't be NULL
465                     const char *working_dir,       // Pass NULL to use the current working directory
466                     int *status_ptr,               // Pass NULL if you don't want the process exit status
467                     int *signo_ptr,                // Pass NULL if you don't want the signal that caused the process to exit
468                     std::string *command_output,   // Pass NULL if you don't want the command output
469                     uint32_t timeout_sec);         // Timeout in seconds to wait for shell program to finish
470
471    bool
472    CalculateMD5 (const lldb_private::FileSpec& file_spec,
473                  uint64_t &high,
474                  uint64_t &low);
475
476    std::string
477    HarmonizeThreadIdsForProfileData (ProcessGDBRemote *process,
478                                      StringExtractorGDBRemote &inputStringExtractor);
479
480    bool
481    ReadRegister(lldb::tid_t tid,
482                 uint32_t reg_num,
483                 StringExtractorGDBRemote &response);
484
485    bool
486    ReadAllRegisters (lldb::tid_t tid,
487                      StringExtractorGDBRemote &response);
488
489    bool
490    SaveRegisterState (lldb::tid_t tid, uint32_t &save_id);
491
492    bool
493    RestoreRegisterState (lldb::tid_t tid, uint32_t save_id);
494
495protected:
496
497    PacketResult
498    SendPacketAndWaitForResponseNoLock (const char *payload,
499                                        size_t payload_length,
500                                        StringExtractorGDBRemote &response);
501
502    bool
503    GetCurrentProcessInfo ();
504
505    //------------------------------------------------------------------
506    // Classes that inherit from GDBRemoteCommunicationClient can see and modify these
507    //------------------------------------------------------------------
508    lldb_private::LazyBool m_supports_not_sending_acks;
509    lldb_private::LazyBool m_supports_thread_suffix;
510    lldb_private::LazyBool m_supports_threads_in_stop_reply;
511    lldb_private::LazyBool m_supports_vCont_all;
512    lldb_private::LazyBool m_supports_vCont_any;
513    lldb_private::LazyBool m_supports_vCont_c;
514    lldb_private::LazyBool m_supports_vCont_C;
515    lldb_private::LazyBool m_supports_vCont_s;
516    lldb_private::LazyBool m_supports_vCont_S;
517    lldb_private::LazyBool m_qHostInfo_is_valid;
518    lldb_private::LazyBool m_qProcessInfo_is_valid;
519    lldb_private::LazyBool m_supports_alloc_dealloc_memory;
520    lldb_private::LazyBool m_supports_memory_region_info;
521    lldb_private::LazyBool m_supports_watchpoint_support_info;
522    lldb_private::LazyBool m_supports_detach_stay_stopped;
523    lldb_private::LazyBool m_watchpoints_trigger_after_instruction;
524    lldb_private::LazyBool m_attach_or_wait_reply;
525    lldb_private::LazyBool m_prepare_for_reg_writing_reply;
526    lldb_private::LazyBool m_supports_p;
527    lldb_private::LazyBool m_supports_QSaveRegisterState;
528    lldb_private::LazyBool m_supports_qXfer_libraries_read;
529    lldb_private::LazyBool m_supports_qXfer_libraries_svr4_read;
530    lldb_private::LazyBool m_supports_augmented_libraries_svr4_read;
531
532    bool
533        m_supports_qProcessInfoPID:1,
534        m_supports_qfProcessInfo:1,
535        m_supports_qUserName:1,
536        m_supports_qGroupName:1,
537        m_supports_qThreadStopInfo:1,
538        m_supports_z0:1,
539        m_supports_z1:1,
540        m_supports_z2:1,
541        m_supports_z3:1,
542        m_supports_z4:1,
543        m_supports_QEnvironment:1,
544        m_supports_QEnvironmentHexEncoded:1;
545
546
547    lldb::tid_t m_curr_tid;         // Current gdb remote protocol thread index for all other operations
548    lldb::tid_t m_curr_tid_run;     // Current gdb remote protocol thread index for continue, step, etc
549
550
551    uint32_t m_num_supported_hardware_watchpoints;
552
553    // If we need to send a packet while the target is running, the m_async_XXX
554    // member variables take care of making this happen.
555    lldb_private::Mutex m_async_mutex;
556    lldb_private::Predicate<bool> m_async_packet_predicate;
557    std::string m_async_packet;
558    PacketResult m_async_result;
559    StringExtractorGDBRemote m_async_response;
560    int m_async_signal; // We were asked to deliver a signal to the inferior process.
561    bool m_interrupt_sent;
562    std::string m_partial_profile_data;
563    std::map<uint64_t, uint32_t> m_thread_id_to_used_usec_map;
564
565    lldb_private::ArchSpec m_host_arch;
566    lldb_private::ArchSpec m_process_arch;
567    uint32_t m_os_version_major;
568    uint32_t m_os_version_minor;
569    uint32_t m_os_version_update;
570    std::string m_os_build;
571    std::string m_os_kernel;
572    std::string m_hostname;
573    uint32_t m_default_packet_timeout;
574    uint64_t m_max_packet_size;  // as returned by qSupported
575
576    bool
577    DecodeProcessInfoResponse (StringExtractorGDBRemote &response,
578                               lldb_private::ProcessInstanceInfo &process_info);
579private:
580    //------------------------------------------------------------------
581    // For GDBRemoteCommunicationClient only
582    //------------------------------------------------------------------
583    DISALLOW_COPY_AND_ASSIGN (GDBRemoteCommunicationClient);
584};
585
586#endif  // liblldb_GDBRemoteCommunicationClient_h_
587