]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.h
Vendor import of lldb trunk r257626:
[FreeBSD/FreeBSD.git] / source / Plugins / Process / gdb-remote / GDBRemoteCommunicationClient.h
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 <map>
16 #include <string>
17 #include <vector>
18
19 // Other libraries and framework includes
20 // Project includes
21 #include "lldb/Core/ArchSpec.h"
22 #include "lldb/Core/StructuredData.h"
23 #include "lldb/Target/Process.h"
24
25 #include "GDBRemoteCommunication.h"
26
27 namespace lldb_private {
28 namespace process_gdb_remote {
29
30 class GDBRemoteCommunicationClient : public GDBRemoteCommunication
31 {
32 public:
33     GDBRemoteCommunicationClient();
34
35     ~GDBRemoteCommunicationClient() override;
36
37     //------------------------------------------------------------------
38     // After connecting, send the handshake to the server to make sure
39     // we are communicating with it.
40     //------------------------------------------------------------------
41     bool
42     HandshakeWithServer (Error *error_ptr);
43
44     PacketResult
45     SendPacketAndWaitForResponse (const char *send_payload,
46                                   StringExtractorGDBRemote &response,
47                                   bool send_async);
48
49     PacketResult
50     SendPacketAndWaitForResponse (const char *send_payload,
51                                   size_t send_length,
52                                   StringExtractorGDBRemote &response,
53                                   bool send_async);
54
55     // For packets which specify a range of output to be returned,
56     // return all of the output via a series of request packets of the form
57     // <prefix>0,<size>
58     // <prefix><size>,<size>
59     // <prefix><size>*2,<size>
60     // <prefix><size>*3,<size>
61     // ...
62     // until a "$l..." packet is received, indicating the end.
63     // (size is in hex; this format is used by a standard gdbserver to
64     // return the given portion of the output specified by <prefix>;
65     // for example, "qXfer:libraries-svr4:read::fff,1000" means
66     // "return a chunk of the xml description file for shared
67     // library load addresses, where the chunk starts at offset 0xfff
68     // and continues for 0x1000 bytes").
69     // Concatenate the resulting server response packets together and
70     // return in response_string.  If any packet fails, the return value
71     // indicates that failure and the returned string value is undefined.
72     PacketResult
73     SendPacketsAndConcatenateResponses (const char *send_payload_prefix,
74                                         std::string &response_string);
75
76     lldb::StateType
77     SendContinuePacketAndWaitForResponse (ProcessGDBRemote *process,
78                                           const char *packet_payload,
79                                           size_t packet_length,
80                                           StringExtractorGDBRemote &response);
81
82     bool
83     SendvContPacket (ProcessGDBRemote *process,
84                      const char *payload,
85                      size_t packet_length,
86                      StringExtractorGDBRemote &response);
87
88     bool
89     GetThreadSuffixSupported () override;
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 communicate 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 (Mutex::Locker &locker, 
108                    uint32_t seconds_to_wait_for_stop, 
109                    bool &timed_out);
110
111     lldb::pid_t
112     GetCurrentProcessID (bool allow_lazy = true);
113
114     bool
115     GetLaunchSuccess (std::string &error_str);
116
117     bool
118     LaunchGDBServer (const char *remote_accept_hostname,
119                      lldb::pid_t &pid,
120                      uint16_t &port,
121                      std::string &socket_name);
122
123     size_t
124     QueryGDBServer (std::vector<std::pair<uint16_t, std::string>>& connection_urls);
125
126     bool
127     KillSpawnedProcess (lldb::pid_t pid);
128
129     //------------------------------------------------------------------
130     /// Sends a GDB remote protocol 'A' packet that delivers program
131     /// arguments to the remote server.
132     ///
133     /// @param[in] argv
134     ///     A NULL terminated array of const C strings to use as the
135     ///     arguments.
136     ///
137     /// @return
138     ///     Zero if the response was "OK", a positive value if the
139     ///     the response was "Exx" where xx are two hex digits, or
140     ///     -1 if the call is unsupported or any other unexpected
141     ///     response was received.
142     //------------------------------------------------------------------
143     int
144     SendArgumentsPacket (const ProcessLaunchInfo &launch_info);
145
146     //------------------------------------------------------------------
147     /// Sends a "QEnvironment:NAME=VALUE" packet that will build up the
148     /// environment that will get used when launching an application
149     /// in conjunction with the 'A' packet. This function can be called
150     /// multiple times in a row in order to pass on the desired
151     /// environment that the inferior should be launched with.
152     ///
153     /// @param[in] name_equal_value
154     ///     A NULL terminated C string that contains a single environment
155     ///     in the format "NAME=VALUE".
156     ///
157     /// @return
158     ///     Zero if the response was "OK", a positive value if the
159     ///     the response was "Exx" where xx are two hex digits, or
160     ///     -1 if the call is unsupported or any other unexpected
161     ///     response was received.
162     //------------------------------------------------------------------
163     int
164     SendEnvironmentPacket (char const *name_equal_value);
165
166     int
167     SendLaunchArchPacket (const char *arch);
168     
169     int
170     SendLaunchEventDataPacket(const char *data, bool *was_supported = nullptr);
171     
172     //------------------------------------------------------------------
173     /// Sends a "vAttach:PID" where PID is in hex. 
174     ///
175     /// @param[in] pid
176     ///     A process ID for the remote gdb server to attach to.
177     ///
178     /// @param[out] response
179     ///     The response received from the gdb server. If the return
180     ///     value is zero, \a response will contain a stop reply 
181     ///     packet.
182     ///
183     /// @return
184     ///     Zero if the attach was successful, or an error indicating
185     ///     an error code.
186     //------------------------------------------------------------------
187     int
188     SendAttach (lldb::pid_t pid, 
189                 StringExtractorGDBRemote& response);
190
191     //------------------------------------------------------------------
192     /// Sends a GDB remote protocol 'I' packet that delivers stdin
193     /// data to the remote process.
194     ///
195     /// @param[in] data
196     ///     A pointer to stdin data.
197     ///
198     /// @param[in] data_len
199     ///     The number of bytes available at \a data.
200     ///
201     /// @return
202     ///     Zero if the attach was successful, or an error indicating
203     ///     an error code.
204     //------------------------------------------------------------------
205     int
206     SendStdinNotification(const char* data, size_t data_len);
207
208     //------------------------------------------------------------------
209     /// Sets the path to use for stdin/out/err for a process
210     /// that will be launched with the 'A' packet.
211     ///
212     /// @param[in] path
213     ///     The path to use for stdin/out/err
214     ///
215     /// @return
216     ///     Zero if the for success, or an error code for failure.
217     //------------------------------------------------------------------
218     int
219     SetSTDIN(const FileSpec &file_spec);
220     int
221     SetSTDOUT(const FileSpec &file_spec);
222     int
223     SetSTDERR(const FileSpec &file_spec);
224
225     //------------------------------------------------------------------
226     /// Sets the disable ASLR flag to \a enable for a process that will 
227     /// be launched with the 'A' packet.
228     ///
229     /// @param[in] enable
230     ///     A boolean value indicating whether to disable ASLR or not.
231     ///
232     /// @return
233     ///     Zero if the for success, or an error code for failure.
234     //------------------------------------------------------------------
235     int
236     SetDisableASLR (bool enable);
237     
238     //------------------------------------------------------------------
239     /// Sets the DetachOnError flag to \a enable for the process controlled by the stub.
240     ///
241     /// @param[in] enable
242     ///     A boolean value indicating whether to detach on error or not.
243     ///
244     /// @return
245     ///     Zero if the for success, or an error code for failure.
246     //------------------------------------------------------------------
247     int
248     SetDetachOnError (bool enable);
249
250     //------------------------------------------------------------------
251     /// Sets the working directory to \a path for a process that will 
252     /// be launched with the 'A' packet for non platform based
253     /// connections. If this packet is sent to a GDB server that
254     /// implements the platform, it will change the current working
255     /// directory for the platform process.
256     ///
257     /// @param[in] working_dir
258     ///     The path to a directory to use when launching our process
259     ///
260     /// @return
261     ///     Zero if the for success, or an error code for failure.
262     //------------------------------------------------------------------
263     int
264     SetWorkingDir(const FileSpec &working_dir);
265
266     //------------------------------------------------------------------
267     /// Gets the current working directory of a remote platform GDB
268     /// server.
269     ///
270     /// @param[out] working_dir
271     ///     The current working directory on the remote platform.
272     ///
273     /// @return
274     ///     Boolean for success
275     //------------------------------------------------------------------
276     bool
277     GetWorkingDir(FileSpec &working_dir);
278
279     lldb::addr_t
280     AllocateMemory (size_t size, uint32_t permissions);
281
282     bool
283     DeallocateMemory (lldb::addr_t addr);
284
285     Error
286     Detach (bool keep_stopped);
287
288     Error
289     GetMemoryRegionInfo (lldb::addr_t addr, MemoryRegionInfo &range_info); 
290
291     Error
292     GetWatchpointSupportInfo (uint32_t &num); 
293
294     Error
295     GetWatchpointSupportInfo (uint32_t &num, bool& after, const ArchSpec &arch);
296     
297     Error
298     GetWatchpointsTriggerAfterInstruction (bool &after, const ArchSpec &arch);
299
300     const ArchSpec &
301     GetHostArchitecture ();
302     
303     uint32_t
304     GetHostDefaultPacketTimeout();
305
306     const ArchSpec &
307     GetProcessArchitecture ();
308
309     void
310     GetRemoteQSupported();
311
312     bool
313     GetVContSupported (char flavor);
314
315     bool
316     GetpPacketSupported (lldb::tid_t tid);
317
318     bool
319     GetxPacketSupported ();
320
321     bool
322     GetVAttachOrWaitSupported ();
323     
324     bool
325     GetSyncThreadStateSupported();
326     
327     void
328     ResetDiscoverableSettings (bool did_exec);
329
330     bool
331     GetHostInfo (bool force = false);
332
333     bool
334     GetDefaultThreadId (lldb::tid_t &tid);
335     
336     bool
337     GetOSVersion (uint32_t &major, 
338                   uint32_t &minor, 
339                   uint32_t &update);
340
341     bool
342     GetOSBuildString (std::string &s);
343     
344     bool
345     GetOSKernelDescription (std::string &s);
346
347     ArchSpec
348     GetSystemArchitecture ();
349
350     bool
351     GetHostname (std::string &s);
352
353     lldb::addr_t
354     GetShlibInfoAddr();
355
356     bool
357     GetSupportsThreadSuffix ();
358
359     bool
360     GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info);
361
362     uint32_t
363     FindProcesses (const ProcessInstanceInfoMatch &process_match_info,
364                    ProcessInstanceInfoList &process_infos);
365
366     bool
367     GetUserName (uint32_t uid, std::string &name);
368     
369     bool
370     GetGroupName (uint32_t gid, std::string &name);
371
372     bool
373     HasFullVContSupport ()
374     {
375         return GetVContSupported ('A');
376     }
377
378     bool
379     HasAnyVContSupport ()
380     {
381         return GetVContSupported ('a');
382     }
383     
384     bool
385     GetStopReply (StringExtractorGDBRemote &response);
386
387     bool
388     GetThreadStopInfo (lldb::tid_t tid, 
389                        StringExtractorGDBRemote &response);
390
391     bool
392     SupportsGDBStoppointPacket (GDBStoppointType type)
393     {
394         switch (type)
395         {
396         case eBreakpointSoftware:   return m_supports_z0;
397         case eBreakpointHardware:   return m_supports_z1;
398         case eWatchpointWrite:      return m_supports_z2;
399         case eWatchpointRead:       return m_supports_z3;
400         case eWatchpointReadWrite:  return m_supports_z4;
401         default:                    return false;
402         }
403     }
404
405     uint8_t
406     SendGDBStoppointTypePacket (GDBStoppointType type,   // Type of breakpoint or watchpoint
407                                 bool insert,              // Insert or remove?
408                                 lldb::addr_t addr,        // Address of breakpoint or watchpoint
409                                 uint32_t length);         // Byte Size of breakpoint or watchpoint
410
411     bool
412     SetNonStopMode (const bool enable);
413
414     void
415     TestPacketSpeed (const uint32_t num_packets, uint32_t max_send, uint32_t max_recv, bool json, Stream &strm);
416
417     // This packet is for testing the speed of the interface only. Both
418     // the client and server need to support it, but this allows us to
419     // measure the packet speed without any other work being done on the
420     // other end and avoids any of that work affecting the packet send
421     // and response times.
422     bool
423     SendSpeedTestPacket (uint32_t send_size, 
424                          uint32_t recv_size);
425     
426     bool
427     SetCurrentThread (uint64_t tid);
428     
429     bool
430     SetCurrentThreadForRun (uint64_t tid);
431
432     bool
433     GetQXferAuxvReadSupported ();
434
435     bool
436     GetQXferLibrariesReadSupported ();
437
438     bool
439     GetQXferLibrariesSVR4ReadSupported ();
440
441     uint64_t
442     GetRemoteMaxPacketSize();
443
444     bool
445     GetEchoSupported ();
446
447     bool
448     GetAugmentedLibrariesSVR4ReadSupported ();
449
450     bool
451     GetQXferFeaturesReadSupported ();
452
453     LazyBool
454     SupportsAllocDeallocMemory () // const
455     {
456         // Uncomment this to have lldb pretend the debug server doesn't respond to alloc/dealloc memory packets.
457         // m_supports_alloc_dealloc_memory = lldb_private::eLazyBoolNo;
458         return m_supports_alloc_dealloc_memory;
459     }
460
461     size_t
462     GetCurrentThreadIDs (std::vector<lldb::tid_t> &thread_ids,
463                          bool &sequence_mutex_unavailable);
464     
465     bool
466     GetInterruptWasSent () const
467     {
468         return m_interrupt_sent;
469     }
470     
471     lldb::user_id_t
472     OpenFile (const FileSpec& file_spec, uint32_t flags, mode_t mode, Error &error);
473     
474     bool
475     CloseFile (lldb::user_id_t fd, Error &error);
476     
477     lldb::user_id_t
478     GetFileSize (const FileSpec& file_spec);
479     
480     Error
481     GetFilePermissions(const FileSpec &file_spec, uint32_t &file_permissions);
482
483     Error
484     SetFilePermissions(const FileSpec &file_spec, uint32_t file_permissions);
485
486     uint64_t
487     ReadFile (lldb::user_id_t fd,
488               uint64_t offset,
489               void *dst,
490               uint64_t dst_len,
491               Error &error);
492     
493     uint64_t
494     WriteFile (lldb::user_id_t fd,
495                uint64_t offset,
496                const void* src,
497                uint64_t src_len,
498                Error &error);
499     
500     Error
501     CreateSymlink(const FileSpec &src,
502                   const FileSpec &dst);
503     
504     Error
505     Unlink(const FileSpec &file_spec);
506
507     Error
508     MakeDirectory(const FileSpec &file_spec, uint32_t mode);
509
510     bool
511     GetFileExists (const FileSpec& file_spec);
512     
513     Error
514     RunShellCommand(const char *command,           // Shouldn't be nullptr
515                     const FileSpec &working_dir,   // Pass empty FileSpec to use the current working directory
516                     int *status_ptr,               // Pass nullptr if you don't want the process exit status
517                     int *signo_ptr,                // Pass nullptr if you don't want the signal that caused the process to exit
518                     std::string *command_output,   // Pass nullptr if you don't want the command output
519                     uint32_t timeout_sec);         // Timeout in seconds to wait for shell program to finish
520
521     bool
522     CalculateMD5 (const FileSpec& file_spec, uint64_t &high, uint64_t &low);
523     
524     std::string
525     HarmonizeThreadIdsForProfileData (ProcessGDBRemote *process,
526                                       StringExtractorGDBRemote &inputStringExtractor);
527
528     bool
529     ReadRegister(lldb::tid_t tid,
530                  uint32_t reg_num,
531                  StringExtractorGDBRemote &response);
532
533     bool
534     ReadAllRegisters (lldb::tid_t tid,
535                       StringExtractorGDBRemote &response);
536
537     bool
538     SaveRegisterState (lldb::tid_t tid, uint32_t &save_id);
539     
540     bool
541     RestoreRegisterState (lldb::tid_t tid, uint32_t save_id);
542
543     const char *
544     GetGDBServerProgramName();
545     
546     uint32_t
547     GetGDBServerProgramVersion();
548
549     bool
550     AvoidGPackets(ProcessGDBRemote *process);
551
552     StructuredData::ObjectSP
553     GetThreadsInfo();
554
555     bool
556     GetThreadExtendedInfoSupported();
557
558     bool
559     GetLoadedDynamicLibrariesInfosSupported();
560
561     bool
562     GetModuleInfo (const FileSpec& module_file_spec,
563                    const ArchSpec& arch_spec,
564                    ModuleSpec &module_spec);
565
566     bool
567     ReadExtFeature (const lldb_private::ConstString object,
568                     const lldb_private::ConstString annex,
569                     std::string & out,
570                     lldb_private::Error & err);
571
572     void
573     ServeSymbolLookups(lldb_private::Process *process);
574
575 protected:
576     LazyBool m_supports_not_sending_acks;
577     LazyBool m_supports_thread_suffix;
578     LazyBool m_supports_threads_in_stop_reply;
579     LazyBool m_supports_vCont_all;
580     LazyBool m_supports_vCont_any;
581     LazyBool m_supports_vCont_c;
582     LazyBool m_supports_vCont_C;
583     LazyBool m_supports_vCont_s;
584     LazyBool m_supports_vCont_S;
585     LazyBool m_qHostInfo_is_valid;
586     LazyBool m_curr_pid_is_valid;
587     LazyBool m_qProcessInfo_is_valid;
588     LazyBool m_qGDBServerVersion_is_valid;
589     LazyBool m_supports_alloc_dealloc_memory;
590     LazyBool m_supports_memory_region_info;
591     LazyBool m_supports_watchpoint_support_info;
592     LazyBool m_supports_detach_stay_stopped;
593     LazyBool m_watchpoints_trigger_after_instruction;
594     LazyBool m_attach_or_wait_reply;
595     LazyBool m_prepare_for_reg_writing_reply;
596     LazyBool m_supports_p;
597     LazyBool m_supports_x;
598     LazyBool m_avoid_g_packets;
599     LazyBool m_supports_QSaveRegisterState;
600     LazyBool m_supports_qXfer_auxv_read;
601     LazyBool m_supports_qXfer_libraries_read;
602     LazyBool m_supports_qXfer_libraries_svr4_read;
603     LazyBool m_supports_qXfer_features_read;
604     LazyBool m_supports_augmented_libraries_svr4_read;
605     LazyBool m_supports_jThreadExtendedInfo;
606     LazyBool m_supports_jLoadedDynamicLibrariesInfos;
607
608     bool
609         m_supports_qProcessInfoPID:1,
610         m_supports_qfProcessInfo:1,
611         m_supports_qUserName:1,
612         m_supports_qGroupName:1,
613         m_supports_qThreadStopInfo:1,
614         m_supports_z0:1,
615         m_supports_z1:1,
616         m_supports_z2:1,
617         m_supports_z3:1,
618         m_supports_z4:1,
619         m_supports_QEnvironment:1,
620         m_supports_QEnvironmentHexEncoded:1,
621         m_supports_qSymbol:1,
622         m_qSymbol_requests_done:1,
623         m_supports_qModuleInfo:1,
624         m_supports_jThreadsInfo:1;
625     
626     lldb::pid_t m_curr_pid;
627     lldb::tid_t m_curr_tid;         // Current gdb remote protocol thread index for all other operations
628     lldb::tid_t m_curr_tid_run;     // Current gdb remote protocol thread index for continue, step, etc
629
630     uint32_t m_num_supported_hardware_watchpoints;
631
632     // If we need to send a packet while the target is running, the m_async_XXX
633     // member variables take care of making this happen.
634     Mutex m_async_mutex;
635     Predicate<bool> m_async_packet_predicate;
636     std::string m_async_packet;
637     PacketResult m_async_result;
638     StringExtractorGDBRemote m_async_response;
639     int m_async_signal; // We were asked to deliver a signal to the inferior process.
640     bool m_interrupt_sent;
641     std::string m_partial_profile_data;
642     std::map<uint64_t, uint32_t> m_thread_id_to_used_usec_map;
643     
644     ArchSpec m_host_arch;
645     ArchSpec m_process_arch;
646     uint32_t m_os_version_major;
647     uint32_t m_os_version_minor;
648     uint32_t m_os_version_update;
649     std::string m_os_build;
650     std::string m_os_kernel;
651     std::string m_hostname;
652     std::string m_gdb_server_name; // from reply to qGDBServerVersion, empty if qGDBServerVersion is not supported
653     uint32_t m_gdb_server_version; // from reply to qGDBServerVersion, zero if qGDBServerVersion is not supported
654     uint32_t m_default_packet_timeout;
655     uint64_t m_max_packet_size;  // as returned by qSupported
656
657     PacketResult
658     SendPacketAndWaitForResponseNoLock (const char *payload,
659                                         size_t payload_length,
660                                         StringExtractorGDBRemote &response);
661
662     bool
663     GetCurrentProcessInfo (bool allow_lazy_pid = true);
664
665     bool
666     GetGDBServerVersion();
667
668     // Given the list of compression types that the remote debug stub can support,
669     // possibly enable compression if we find an encoding we can handle.
670     void
671     MaybeEnableCompression (std::vector<std::string> supported_compressions);
672
673     bool
674     DecodeProcessInfoResponse (StringExtractorGDBRemote &response, 
675                                ProcessInstanceInfo &process_info);
676
677 private:
678     DISALLOW_COPY_AND_ASSIGN (GDBRemoteCommunicationClient);
679 };
680
681 } // namespace process_gdb_remote
682 } // namespace lldb_private
683
684 #endif // liblldb_GDBRemoteCommunicationClient_h_