Path: blob/main/contrib/llvm-project/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.h
39644 views
//===-- GDBRemoteCommunication.h --------------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef LLDB_SOURCE_PLUGINS_PROCESS_GDB_REMOTE_GDBREMOTECOMMUNICATION_H9#define LLDB_SOURCE_PLUGINS_PROCESS_GDB_REMOTE_GDBREMOTECOMMUNICATION_H1011#include "GDBRemoteCommunicationHistory.h"1213#include <condition_variable>14#include <future>15#include <mutex>16#include <queue>17#include <string>18#include <vector>1920#include "lldb/Core/Communication.h"21#include "lldb/Host/Config.h"22#include "lldb/Host/HostThread.h"23#include "lldb/Utility/Args.h"24#include "lldb/Utility/Listener.h"25#include "lldb/Utility/Predicate.h"26#include "lldb/Utility/StringExtractorGDBRemote.h"27#include "lldb/lldb-public.h"2829namespace lldb_private {30namespace repro {31class PacketRecorder;32}33namespace process_gdb_remote {3435enum GDBStoppointType {36eStoppointInvalid = -1,37eBreakpointSoftware = 0,38eBreakpointHardware,39eWatchpointWrite,40eWatchpointRead,41eWatchpointReadWrite42};4344enum class CompressionType {45None = 0, // no compression46ZlibDeflate, // zlib's deflate compression scheme, requires zlib or Apple's47// libcompression48LZFSE, // an Apple compression scheme, requires Apple's libcompression49LZ4, // lz compression - called "lz4 raw" in libcompression terms, compat with50// https://code.google.com/p/lz4/51LZMA, // Lempel–Ziv–Markov chain algorithm52};5354// Data included in the vFile:fstat packet.55// https://sourceware.org/gdb/onlinedocs/gdb/struct-stat.html#struct-stat56struct GDBRemoteFStatData {57llvm::support::ubig32_t gdb_st_dev;58llvm::support::ubig32_t gdb_st_ino;59llvm::support::ubig32_t gdb_st_mode;60llvm::support::ubig32_t gdb_st_nlink;61llvm::support::ubig32_t gdb_st_uid;62llvm::support::ubig32_t gdb_st_gid;63llvm::support::ubig32_t gdb_st_rdev;64llvm::support::ubig64_t gdb_st_size;65llvm::support::ubig64_t gdb_st_blksize;66llvm::support::ubig64_t gdb_st_blocks;67llvm::support::ubig32_t gdb_st_atime;68llvm::support::ubig32_t gdb_st_mtime;69llvm::support::ubig32_t gdb_st_ctime;70};71static_assert(sizeof(GDBRemoteFStatData) == 64,72"size of GDBRemoteFStatData is not 64");7374enum GDBErrno {75#define HANDLE_ERRNO(name, value) GDB_##name = value,76#include "Plugins/Process/gdb-remote/GDBRemoteErrno.def"77GDB_EUNKNOWN = 999978};7980class ProcessGDBRemote;8182class GDBRemoteCommunication : public Communication {83public:84enum class PacketType { Invalid = 0, Standard, Notify };8586enum class PacketResult {87Success = 0, // Success88ErrorSendFailed, // Status sending the packet89ErrorSendAck, // Didn't get an ack back after sending a packet90ErrorReplyFailed, // Status getting the reply91ErrorReplyTimeout, // Timed out waiting for reply92ErrorReplyInvalid, // Got a reply but it wasn't valid for the packet that93// was sent94ErrorReplyAck, // Sending reply ack failed95ErrorDisconnected, // We were disconnected96ErrorNoSequenceLock // We couldn't get the sequence lock for a multi-packet97// request98};99100// Class to change the timeout for a given scope and restore it to the101// original value when the102// created ScopedTimeout object got out of scope103class ScopedTimeout {104public:105ScopedTimeout(GDBRemoteCommunication &gdb_comm,106std::chrono::seconds timeout);107~ScopedTimeout();108109private:110GDBRemoteCommunication &m_gdb_comm;111std::chrono::seconds m_saved_timeout;112// Don't ever reduce the timeout for a packet, only increase it. If the113// requested timeout if less than the current timeout, we don't set it114// and won't need to restore it.115bool m_timeout_modified;116};117118GDBRemoteCommunication();119120~GDBRemoteCommunication() override;121122PacketResult GetAck();123124size_t SendAck();125126size_t SendNack();127128char CalculcateChecksum(llvm::StringRef payload);129130PacketType CheckForPacket(const uint8_t *src, size_t src_len,131StringExtractorGDBRemote &packet);132133bool GetSendAcks() { return m_send_acks; }134135// Set the global packet timeout.136//137// For clients, this is the timeout that gets used when sending138// packets and waiting for responses. For servers, this is used when waiting139// for ACKs.140std::chrono::seconds SetPacketTimeout(std::chrono::seconds packet_timeout) {141const auto old_packet_timeout = m_packet_timeout;142m_packet_timeout = packet_timeout;143return old_packet_timeout;144}145146std::chrono::seconds GetPacketTimeout() const { return m_packet_timeout; }147148// Start a debugserver instance on the current host using the149// supplied connection URL.150Status StartDebugserverProcess(151const char *url,152Platform *platform, // If non nullptr, then check with the platform for153// the GDB server binary if it can't be located154ProcessLaunchInfo &launch_info, uint16_t *port, const Args *inferior_args,155int pass_comm_fd); // Communication file descriptor to pass during156// fork/exec to avoid having to connect/accept157158void DumpHistory(Stream &strm);159160void SetPacketRecorder(repro::PacketRecorder *recorder);161162static llvm::Error ConnectLocally(GDBRemoteCommunication &client,163GDBRemoteCommunication &server);164165/// Expand GDB run-length encoding.166static std::string ExpandRLE(std::string);167168protected:169std::chrono::seconds m_packet_timeout;170uint32_t m_echo_number;171LazyBool m_supports_qEcho;172GDBRemoteCommunicationHistory m_history;173bool m_send_acks;174bool m_is_platform; // Set to true if this class represents a platform,175// false if this class represents a debug session for176// a single process177178std::string m_bytes;179std::recursive_mutex m_bytes_mutex;180CompressionType m_compression_type;181182PacketResult SendPacketNoLock(llvm::StringRef payload);183PacketResult SendNotificationPacketNoLock(llvm::StringRef notify_type,184std::deque<std::string>& queue,185llvm::StringRef payload);186PacketResult SendRawPacketNoLock(llvm::StringRef payload,187bool skip_ack = false);188189PacketResult ReadPacket(StringExtractorGDBRemote &response,190Timeout<std::micro> timeout, bool sync_on_timeout);191192PacketResult WaitForPacketNoLock(StringExtractorGDBRemote &response,193Timeout<std::micro> timeout,194bool sync_on_timeout);195196bool CompressionIsEnabled() {197return m_compression_type != CompressionType::None;198}199200// If compression is enabled, decompress the packet in m_bytes and update201// m_bytes with the uncompressed version.202// Returns 'true' packet was decompressed and m_bytes is the now-decompressed203// text.204// Returns 'false' if unable to decompress or if the checksum was invalid.205//206// NB: Once the packet has been decompressed, checksum cannot be computed207// based208// on m_bytes. The checksum was for the compressed packet.209bool DecompressPacket();210211Status StartListenThread(const char *hostname = "127.0.0.1",212uint16_t port = 0);213214bool JoinListenThread();215216lldb::thread_result_t ListenThread();217218private:219// Promise used to grab the port number from listening thread220std::promise<uint16_t> m_port_promise;221222HostThread m_listen_thread;223std::string m_listen_url;224225#if defined(HAVE_LIBCOMPRESSION)226CompressionType m_decompression_scratch_type = CompressionType::None;227void *m_decompression_scratch = nullptr;228#endif229230GDBRemoteCommunication(const GDBRemoteCommunication &) = delete;231const GDBRemoteCommunication &232operator=(const GDBRemoteCommunication &) = delete;233};234235} // namespace process_gdb_remote236} // namespace lldb_private237238namespace llvm {239template <>240struct format_provider<241lldb_private::process_gdb_remote::GDBRemoteCommunication::PacketResult> {242static void format(const lldb_private::process_gdb_remote::243GDBRemoteCommunication::PacketResult &state,244raw_ostream &Stream, StringRef Style);245};246} // namespace llvm247248#endif // LLDB_SOURCE_PLUGINS_PROCESS_GDB_REMOTE_GDBREMOTECOMMUNICATION_H249250251