1 //===-- GDBRemote.h ----------------------------------------------*- C++-*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef LLDB_UTILITY_GDBREMOTE_H 10 #define LLDB_UTILITY_GDBREMOTE_H 11 12 #include "lldb/Utility/FileSpec.h" 13 #include "lldb/Utility/ReproducerProvider.h" 14 #include "lldb/Utility/StreamString.h" 15 #include "lldb/lldb-enumerations.h" 16 #include "lldb/lldb-public.h" 17 #include "llvm/Support/YAMLTraits.h" 18 #include "llvm/Support/raw_ostream.h" 19 20 #include <cstddef> 21 #include <cstdint> 22 #include <string> 23 #include <vector> 24 25 namespace lldb_private { 26 27 class StreamGDBRemote : public StreamString { 28 public: 29 StreamGDBRemote(); 30 31 StreamGDBRemote(uint32_t flags, uint32_t addr_size, 32 lldb::ByteOrder byte_order); 33 34 ~StreamGDBRemote() override; 35 36 /// Output a block of data to the stream performing GDB-remote escaping. 37 /// 38 /// \param[in] s 39 /// A block of data. 40 /// 41 /// \param[in] src_len 42 /// The amount of data to write. 43 /// 44 /// \return 45 /// Number of bytes written. 46 // TODO: Convert this function to take ArrayRef<uint8_t> 47 int PutEscapedBytes(const void *s, size_t src_len); 48 }; 49 50 /// GDB remote packet as used by the reproducer and the GDB remote 51 /// communication history. Packets can be serialized to file. 52 struct GDBRemotePacket { 53 54 friend llvm::yaml::MappingTraits<GDBRemotePacket>; 55 56 enum Type { ePacketTypeInvalid = 0, ePacketTypeSend, ePacketTypeRecv }; 57 58 GDBRemotePacket() = default; 59 60 void Clear() { 61 packet.data.clear(); 62 type = ePacketTypeInvalid; 63 bytes_transmitted = 0; 64 packet_idx = 0; 65 tid = LLDB_INVALID_THREAD_ID; 66 } 67 68 struct BinaryData { 69 std::string data; 70 }; 71 72 void Dump(Stream &strm) const; 73 74 BinaryData packet; 75 Type type = ePacketTypeInvalid; 76 uint32_t bytes_transmitted = 0; 77 uint32_t packet_idx = 0; 78 lldb::tid_t tid = LLDB_INVALID_THREAD_ID; 79 80 private: 81 llvm::StringRef GetTypeStr() const; 82 }; 83 84 namespace repro { 85 class PacketRecorder : public AbstractRecorder { 86 public: 87 PacketRecorder(const FileSpec &filename, std::error_code &ec) 88 : AbstractRecorder(filename, ec) {} 89 90 static llvm::Expected<std::unique_ptr<PacketRecorder>> 91 Create(const FileSpec &filename); 92 93 void Record(const GDBRemotePacket &packet); 94 }; 95 96 class GDBRemoteProvider : public repro::Provider<GDBRemoteProvider> { 97 public: 98 struct Info { 99 static const char *name; 100 static const char *file; 101 }; 102 103 GDBRemoteProvider(const FileSpec &directory) : Provider(directory) {} 104 105 llvm::raw_ostream *GetHistoryStream(); 106 PacketRecorder *GetNewPacketRecorder(); 107 108 void SetCallback(std::function<void()> callback) { 109 m_callback = std::move(callback); 110 } 111 112 void Keep() override; 113 void Discard() override; 114 115 static char ID; 116 117 private: 118 std::function<void()> m_callback; 119 std::unique_ptr<llvm::raw_fd_ostream> m_stream_up; 120 std::vector<std::unique_ptr<PacketRecorder>> m_packet_recorders; 121 }; 122 123 } // namespace repro 124 } // namespace lldb_private 125 126 LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(lldb_private::GDBRemotePacket) 127 LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(std::vector<lldb_private::GDBRemotePacket>) 128 129 namespace llvm { 130 namespace yaml { 131 132 template <> 133 struct ScalarEnumerationTraits<lldb_private::GDBRemotePacket::Type> { 134 static void enumeration(IO &io, lldb_private::GDBRemotePacket::Type &value); 135 }; 136 137 template <> struct ScalarTraits<lldb_private::GDBRemotePacket::BinaryData> { 138 static void output(const lldb_private::GDBRemotePacket::BinaryData &, void *, 139 raw_ostream &); 140 141 static StringRef input(StringRef, void *, 142 lldb_private::GDBRemotePacket::BinaryData &); 143 144 static QuotingType mustQuote(StringRef S) { return QuotingType::None; } 145 }; 146 147 template <> struct MappingTraits<lldb_private::GDBRemotePacket> { 148 static void mapping(IO &io, lldb_private::GDBRemotePacket &Packet); 149 150 static StringRef validate(IO &io, lldb_private::GDBRemotePacket &); 151 }; 152 153 } // namespace yaml 154 } // namespace llvm 155 156 #endif // LLDB_UTILITY_GDBREMOTE_H 157