1 //===-- GDBRemoteCommunicationClient.h --------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_GDBRemoteCommunicationClient_h_
11 #define liblldb_GDBRemoteCommunicationClient_h_
13 #include "GDBRemoteClientBase.h"
23 // Other libraries and framework includes
25 #include "lldb/Core/ArchSpec.h"
26 #include "lldb/Core/StructuredData.h"
27 #include "lldb/Target/Process.h"
29 #include "llvm/ADT/Optional.h"
31 namespace lldb_private {
32 namespace process_gdb_remote {
34 class GDBRemoteCommunicationClient : public GDBRemoteClientBase {
36 GDBRemoteCommunicationClient();
38 ~GDBRemoteCommunicationClient() override;
40 //------------------------------------------------------------------
41 // After connecting, send the handshake to the server to make sure
42 // we are communicating with it.
43 //------------------------------------------------------------------
44 bool HandshakeWithServer(Error *error_ptr);
46 // For packets which specify a range of output to be returned,
47 // return all of the output via a series of request packets of the form
49 // <prefix><size>,<size>
50 // <prefix><size>*2,<size>
51 // <prefix><size>*3,<size>
53 // until a "$l..." packet is received, indicating the end.
54 // (size is in hex; this format is used by a standard gdbserver to
55 // return the given portion of the output specified by <prefix>;
56 // for example, "qXfer:libraries-svr4:read::fff,1000" means
57 // "return a chunk of the xml description file for shared
58 // library load addresses, where the chunk starts at offset 0xfff
59 // and continues for 0x1000 bytes").
60 // Concatenate the resulting server response packets together and
61 // return in response_string. If any packet fails, the return value
62 // indicates that failure and the returned string value is undefined.
64 SendPacketsAndConcatenateResponses(const char *send_payload_prefix,
65 std::string &response_string);
67 bool GetThreadSuffixSupported();
69 // This packet is usually sent first and the boolean return value
70 // indicates if the packet was send and any response was received
71 // even in the response is UNIMPLEMENTED. If the packet failed to
72 // get a response, then false is returned. This quickly tells us
73 // if we were able to connect and communicate with the remote GDB
75 bool QueryNoAckModeSupported();
77 void GetListThreadsInStopReplySupported();
79 lldb::pid_t GetCurrentProcessID(bool allow_lazy = true);
81 bool GetLaunchSuccess(std::string &error_str);
83 bool LaunchGDBServer(const char *remote_accept_hostname, lldb::pid_t &pid,
84 uint16_t &port, std::string &socket_name);
86 size_t QueryGDBServer(
87 std::vector<std::pair<uint16_t, std::string>> &connection_urls);
89 bool KillSpawnedProcess(lldb::pid_t pid);
91 //------------------------------------------------------------------
92 /// Sends a GDB remote protocol 'A' packet that delivers program
93 /// arguments to the remote server.
96 /// A NULL terminated array of const C strings to use as the
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 SendArgumentsPacket(const ProcessLaunchInfo &launch_info);
107 //------------------------------------------------------------------
108 /// Sends a "QEnvironment:NAME=VALUE" packet that will build up the
109 /// environment that will get used when launching an application
110 /// in conjunction with the 'A' packet. This function can be called
111 /// multiple times in a row in order to pass on the desired
112 /// environment that the inferior should be launched with.
114 /// @param[in] name_equal_value
115 /// A NULL terminated C string that contains a single environment
116 /// in the format "NAME=VALUE".
119 /// Zero if the response was "OK", a positive value if the
120 /// the response was "Exx" where xx are two hex digits, or
121 /// -1 if the call is unsupported or any other unexpected
122 /// response was received.
123 //------------------------------------------------------------------
124 int SendEnvironmentPacket(char const *name_equal_value);
126 int SendLaunchArchPacket(const char *arch);
128 int SendLaunchEventDataPacket(const char *data,
129 bool *was_supported = nullptr);
131 //------------------------------------------------------------------
132 /// Sends a "vAttach:PID" where PID is in hex.
135 /// A process ID for the remote gdb server to attach to.
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
143 /// Zero if the attach was successful, or an error indicating
145 //------------------------------------------------------------------
146 int SendAttach(lldb::pid_t pid, StringExtractorGDBRemote &response);
148 //------------------------------------------------------------------
149 /// Sends a GDB remote protocol 'I' packet that delivers stdin
150 /// data to the remote process.
153 /// A pointer to stdin data.
155 /// @param[in] data_len
156 /// The number of bytes available at \a data.
159 /// Zero if the attach was successful, or an error indicating
161 //------------------------------------------------------------------
162 int SendStdinNotification(const char *data, size_t data_len);
164 //------------------------------------------------------------------
165 /// Sets the path to use for stdin/out/err for a process
166 /// that will be launched with the 'A' packet.
169 /// The path to use for stdin/out/err
172 /// Zero if the for success, or an error code for failure.
173 //------------------------------------------------------------------
174 int SetSTDIN(const FileSpec &file_spec);
175 int SetSTDOUT(const FileSpec &file_spec);
176 int SetSTDERR(const FileSpec &file_spec);
178 //------------------------------------------------------------------
179 /// Sets the disable ASLR flag to \a enable for a process that will
180 /// be launched with the 'A' packet.
182 /// @param[in] enable
183 /// A boolean value indicating whether to disable ASLR or not.
186 /// Zero if the for success, or an error code for failure.
187 //------------------------------------------------------------------
188 int SetDisableASLR(bool enable);
190 //------------------------------------------------------------------
191 /// Sets the DetachOnError flag to \a enable for the process controlled by the
194 /// @param[in] enable
195 /// A boolean value indicating whether to detach on error or not.
198 /// Zero if the for success, or an error code for failure.
199 //------------------------------------------------------------------
200 int SetDetachOnError(bool enable);
202 //------------------------------------------------------------------
203 /// Sets the working directory to \a path for a process that will
204 /// be launched with the 'A' packet for non platform based
205 /// connections. If this packet is sent to a GDB server that
206 /// implements the platform, it will change the current working
207 /// directory for the platform process.
209 /// @param[in] working_dir
210 /// The path to a directory to use when launching our process
213 /// Zero if the for success, or an error code for failure.
214 //------------------------------------------------------------------
215 int SetWorkingDir(const FileSpec &working_dir);
217 //------------------------------------------------------------------
218 /// Gets the current working directory of a remote platform GDB
221 /// @param[out] working_dir
222 /// The current working directory on the remote platform.
225 /// Boolean for success
226 //------------------------------------------------------------------
227 bool GetWorkingDir(FileSpec &working_dir);
229 lldb::addr_t AllocateMemory(size_t size, uint32_t permissions);
231 bool DeallocateMemory(lldb::addr_t addr);
233 Error Detach(bool keep_stopped);
235 Error GetMemoryRegionInfo(lldb::addr_t addr, MemoryRegionInfo &range_info);
237 Error GetWatchpointSupportInfo(uint32_t &num);
239 Error GetWatchpointSupportInfo(uint32_t &num, bool &after,
240 const ArchSpec &arch);
242 Error GetWatchpointsTriggerAfterInstruction(bool &after,
243 const ArchSpec &arch);
245 const ArchSpec &GetHostArchitecture();
247 std::chrono::seconds GetHostDefaultPacketTimeout();
249 const ArchSpec &GetProcessArchitecture();
251 void GetRemoteQSupported();
253 bool GetVContSupported(char flavor);
255 bool GetpPacketSupported(lldb::tid_t tid);
257 bool GetxPacketSupported();
259 bool GetVAttachOrWaitSupported();
261 bool GetSyncThreadStateSupported();
263 void ResetDiscoverableSettings(bool did_exec);
265 bool GetHostInfo(bool force = false);
267 bool GetDefaultThreadId(lldb::tid_t &tid);
269 bool GetOSVersion(uint32_t &major, uint32_t &minor, uint32_t &update);
271 bool GetOSBuildString(std::string &s);
273 bool GetOSKernelDescription(std::string &s);
275 ArchSpec GetSystemArchitecture();
277 bool GetHostname(std::string &s);
279 lldb::addr_t GetShlibInfoAddr();
281 bool GetSupportsThreadSuffix();
283 bool GetProcessInfo(lldb::pid_t pid, ProcessInstanceInfo &process_info);
285 uint32_t FindProcesses(const ProcessInstanceInfoMatch &process_match_info,
286 ProcessInstanceInfoList &process_infos);
288 bool GetUserName(uint32_t uid, std::string &name);
290 bool GetGroupName(uint32_t gid, std::string &name);
292 bool HasFullVContSupport() { return GetVContSupported('A'); }
294 bool HasAnyVContSupport() { return GetVContSupported('a'); }
296 bool GetStopReply(StringExtractorGDBRemote &response);
298 bool GetThreadStopInfo(lldb::tid_t tid, StringExtractorGDBRemote &response);
300 bool SupportsGDBStoppointPacket(GDBStoppointType type) {
302 case eBreakpointSoftware:
303 return m_supports_z0;
304 case eBreakpointHardware:
305 return m_supports_z1;
306 case eWatchpointWrite:
307 return m_supports_z2;
308 case eWatchpointRead:
309 return m_supports_z3;
310 case eWatchpointReadWrite:
311 return m_supports_z4;
317 uint8_t SendGDBStoppointTypePacket(
318 GDBStoppointType type, // Type of breakpoint or watchpoint
319 bool insert, // Insert or remove?
320 lldb::addr_t addr, // Address of breakpoint or watchpoint
321 uint32_t length); // Byte Size of breakpoint or watchpoint
323 bool SetNonStopMode(const bool enable);
325 void TestPacketSpeed(const uint32_t num_packets, uint32_t max_send,
326 uint32_t max_recv, uint64_t recv_amount, bool json,
329 // This packet is for testing the speed of the interface only. Both
330 // the client and server need to support it, but this allows us to
331 // measure the packet speed without any other work being done on the
332 // other end and avoids any of that work affecting the packet send
333 // and response times.
334 bool SendSpeedTestPacket(uint32_t send_size, uint32_t recv_size);
336 bool SetCurrentThread(uint64_t tid);
338 bool SetCurrentThreadForRun(uint64_t tid);
340 bool GetQXferAuxvReadSupported();
342 bool GetQXferLibrariesReadSupported();
344 bool GetQXferLibrariesSVR4ReadSupported();
346 uint64_t GetRemoteMaxPacketSize();
348 bool GetEchoSupported();
350 bool GetAugmentedLibrariesSVR4ReadSupported();
352 bool GetQXferFeaturesReadSupported();
354 LazyBool SupportsAllocDeallocMemory() // const
356 // Uncomment this to have lldb pretend the debug server doesn't respond to
357 // alloc/dealloc memory packets.
358 // m_supports_alloc_dealloc_memory = lldb_private::eLazyBoolNo;
359 return m_supports_alloc_dealloc_memory;
362 size_t GetCurrentThreadIDs(std::vector<lldb::tid_t> &thread_ids,
363 bool &sequence_mutex_unavailable);
365 lldb::user_id_t OpenFile(const FileSpec &file_spec, uint32_t flags,
366 mode_t mode, Error &error);
368 bool CloseFile(lldb::user_id_t fd, Error &error);
370 lldb::user_id_t GetFileSize(const FileSpec &file_spec);
372 Error GetFilePermissions(const FileSpec &file_spec,
373 uint32_t &file_permissions);
375 Error SetFilePermissions(const FileSpec &file_spec,
376 uint32_t file_permissions);
378 uint64_t ReadFile(lldb::user_id_t fd, uint64_t offset, void *dst,
379 uint64_t dst_len, Error &error);
381 uint64_t WriteFile(lldb::user_id_t fd, uint64_t offset, const void *src,
382 uint64_t src_len, Error &error);
384 Error CreateSymlink(const FileSpec &src, const FileSpec &dst);
386 Error Unlink(const FileSpec &file_spec);
388 Error MakeDirectory(const FileSpec &file_spec, uint32_t mode);
390 bool GetFileExists(const FileSpec &file_spec);
392 Error RunShellCommand(
393 const char *command, // Shouldn't be nullptr
394 const FileSpec &working_dir, // Pass empty FileSpec to use the current
396 int *status_ptr, // Pass nullptr if you don't want the process exit status
397 int *signo_ptr, // Pass nullptr if you don't want the signal that caused
398 // the process to exit
400 *command_output, // Pass nullptr if you don't want the command output
401 uint32_t timeout_sec); // Timeout in seconds to wait for shell program to
404 bool CalculateMD5(const FileSpec &file_spec, uint64_t &high, uint64_t &low);
406 lldb::DataBufferSP ReadRegister(
409 reg_num); // Must be the eRegisterKindProcessPlugin register number
411 lldb::DataBufferSP ReadAllRegisters(lldb::tid_t tid);
414 WriteRegister(lldb::tid_t tid,
415 uint32_t reg_num, // eRegisterKindProcessPlugin register number
416 llvm::ArrayRef<uint8_t> data);
418 bool WriteAllRegisters(lldb::tid_t tid, llvm::ArrayRef<uint8_t> data);
420 bool SaveRegisterState(lldb::tid_t tid, uint32_t &save_id);
422 bool RestoreRegisterState(lldb::tid_t tid, uint32_t save_id);
424 bool SyncThreadState(lldb::tid_t tid);
426 const char *GetGDBServerProgramName();
428 uint32_t GetGDBServerProgramVersion();
430 bool AvoidGPackets(ProcessGDBRemote *process);
432 StructuredData::ObjectSP GetThreadsInfo();
434 bool GetThreadExtendedInfoSupported();
436 bool GetLoadedDynamicLibrariesInfosSupported();
438 bool GetSharedCacheInfoSupported();
440 bool GetModuleInfo(const FileSpec &module_file_spec,
441 const ArchSpec &arch_spec, ModuleSpec &module_spec);
443 llvm::Optional<std::vector<ModuleSpec>>
444 GetModulesInfo(llvm::ArrayRef<FileSpec> module_file_specs,
445 const llvm::Triple &triple);
447 bool ReadExtFeature(const lldb_private::ConstString object,
448 const lldb_private::ConstString annex, std::string &out,
449 lldb_private::Error &err);
451 void ServeSymbolLookups(lldb_private::Process *process);
453 //------------------------------------------------------------------
454 /// Return the feature set supported by the gdb-remote server.
456 /// This method returns the remote side's response to the qSupported
457 /// packet. The response is the complete string payload returned
461 /// The string returned by the server to the qSupported query.
462 //------------------------------------------------------------------
463 const std::string &GetServerSupportedFeatures() const {
464 return m_qSupported_response;
467 //------------------------------------------------------------------
468 /// Return the array of async JSON packet types supported by the remote.
470 /// This method returns the remote side's array of supported JSON
471 /// packet types as a list of type names. Each of the results are
472 /// expected to have an Enable{type_name} command to enable and configure
473 /// the related feature. Each type_name for an enabled feature will
474 /// possibly send async-style packets that contain a payload of a
475 /// binhex-encoded JSON dictionary. The dictionary will have a
476 /// string field named 'type', that contains the type_name of the
477 /// supported packet type.
479 /// There is a Plugin category called structured-data plugins.
480 /// A plugin indicates whether it knows how to handle a type_name.
481 /// If so, it can be used to process the async JSON packet.
484 /// The string returned by the server to the qSupported query.
485 //------------------------------------------------------------------
486 lldb_private::StructuredData::Array *GetSupportedStructuredDataPlugins();
488 //------------------------------------------------------------------
489 /// Configure a StructuredData feature on the remote end.
491 /// @see \b Process::ConfigureStructuredData(...) for details.
492 //------------------------------------------------------------------
494 ConfigureRemoteStructuredData(const ConstString &type_name,
495 const StructuredData::ObjectSP &config_sp);
498 LazyBool m_supports_not_sending_acks;
499 LazyBool m_supports_thread_suffix;
500 LazyBool m_supports_threads_in_stop_reply;
501 LazyBool m_supports_vCont_all;
502 LazyBool m_supports_vCont_any;
503 LazyBool m_supports_vCont_c;
504 LazyBool m_supports_vCont_C;
505 LazyBool m_supports_vCont_s;
506 LazyBool m_supports_vCont_S;
507 LazyBool m_qHostInfo_is_valid;
508 LazyBool m_curr_pid_is_valid;
509 LazyBool m_qProcessInfo_is_valid;
510 LazyBool m_qGDBServerVersion_is_valid;
511 LazyBool m_supports_alloc_dealloc_memory;
512 LazyBool m_supports_memory_region_info;
513 LazyBool m_supports_watchpoint_support_info;
514 LazyBool m_supports_detach_stay_stopped;
515 LazyBool m_watchpoints_trigger_after_instruction;
516 LazyBool m_attach_or_wait_reply;
517 LazyBool m_prepare_for_reg_writing_reply;
518 LazyBool m_supports_p;
519 LazyBool m_supports_x;
520 LazyBool m_avoid_g_packets;
521 LazyBool m_supports_QSaveRegisterState;
522 LazyBool m_supports_qXfer_auxv_read;
523 LazyBool m_supports_qXfer_libraries_read;
524 LazyBool m_supports_qXfer_libraries_svr4_read;
525 LazyBool m_supports_qXfer_features_read;
526 LazyBool m_supports_augmented_libraries_svr4_read;
527 LazyBool m_supports_jThreadExtendedInfo;
528 LazyBool m_supports_jLoadedDynamicLibrariesInfos;
529 LazyBool m_supports_jGetSharedCacheInfo;
531 bool m_supports_qProcessInfoPID : 1, m_supports_qfProcessInfo : 1,
532 m_supports_qUserName : 1, m_supports_qGroupName : 1,
533 m_supports_qThreadStopInfo : 1, m_supports_z0 : 1, m_supports_z1 : 1,
534 m_supports_z2 : 1, m_supports_z3 : 1, m_supports_z4 : 1,
535 m_supports_QEnvironment : 1, m_supports_QEnvironmentHexEncoded : 1,
536 m_supports_qSymbol : 1, m_qSymbol_requests_done : 1,
537 m_supports_qModuleInfo : 1, m_supports_jThreadsInfo : 1,
538 m_supports_jModulesInfo : 1;
540 lldb::pid_t m_curr_pid;
541 lldb::tid_t m_curr_tid; // Current gdb remote protocol thread index for all
543 lldb::tid_t m_curr_tid_run; // Current gdb remote protocol thread index for
544 // continue, step, etc
546 uint32_t m_num_supported_hardware_watchpoints;
548 ArchSpec m_host_arch;
549 ArchSpec m_process_arch;
550 uint32_t m_os_version_major;
551 uint32_t m_os_version_minor;
552 uint32_t m_os_version_update;
553 std::string m_os_build;
554 std::string m_os_kernel;
555 std::string m_hostname;
556 std::string m_gdb_server_name; // from reply to qGDBServerVersion, empty if
557 // qGDBServerVersion is not supported
558 uint32_t m_gdb_server_version; // from reply to qGDBServerVersion, zero if
559 // qGDBServerVersion is not supported
560 std::chrono::seconds m_default_packet_timeout;
561 uint64_t m_max_packet_size; // as returned by qSupported
562 std::string m_qSupported_response; // the complete response to qSupported
564 bool m_supported_async_json_packets_is_valid;
565 lldb_private::StructuredData::ObjectSP m_supported_async_json_packets_sp;
567 bool GetCurrentProcessInfo(bool allow_lazy_pid = true);
569 bool GetGDBServerVersion();
571 // Given the list of compression types that the remote debug stub can support,
572 // possibly enable compression if we find an encoding we can handle.
573 void MaybeEnableCompression(std::vector<std::string> supported_compressions);
575 bool DecodeProcessInfoResponse(StringExtractorGDBRemote &response,
576 ProcessInstanceInfo &process_info);
578 void OnRunPacketSent(bool first) override;
580 PacketResult SendThreadSpecificPacketAndWaitForResponse(
581 lldb::tid_t tid, StreamString &&payload,
582 StringExtractorGDBRemote &response, bool send_async);
585 DISALLOW_COPY_AND_ASSIGN(GDBRemoteCommunicationClient);
588 } // namespace process_gdb_remote
589 } // namespace lldb_private
591 #endif // liblldb_GDBRemoteCommunicationClient_h_