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 <stddef.h>
21 #include <stdint.h>
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
GDBRemotePacketGDBRemotePacket58 GDBRemotePacket()
59 : packet(), type(ePacketTypeInvalid), bytes_transmitted(0), packet_idx(0),
60 tid(LLDB_INVALID_THREAD_ID) {}
61
ClearGDBRemotePacket62 void Clear() {
63 packet.data.clear();
64 type = ePacketTypeInvalid;
65 bytes_transmitted = 0;
66 packet_idx = 0;
67 tid = LLDB_INVALID_THREAD_ID;
68 }
69
70 struct BinaryData {
71 std::string data;
72 };
73
74 void Dump(Stream &strm) const;
75
76 BinaryData packet;
77 Type type;
78 uint32_t bytes_transmitted;
79 uint32_t packet_idx;
80 lldb::tid_t tid;
81
82 private:
83 llvm::StringRef GetTypeStr() const;
84 };
85
86 namespace repro {
87 class PacketRecorder : public AbstractRecorder {
88 public:
PacketRecorder(const FileSpec & filename,std::error_code & ec)89 PacketRecorder(const FileSpec &filename, std::error_code &ec)
90 : AbstractRecorder(filename, ec) {}
91
92 static llvm::Expected<std::unique_ptr<PacketRecorder>>
93 Create(const FileSpec &filename);
94
95 void Record(const GDBRemotePacket &packet);
96 };
97
98 class GDBRemoteProvider : public repro::Provider<GDBRemoteProvider> {
99 public:
100 struct Info {
101 static const char *name;
102 static const char *file;
103 };
104
GDBRemoteProvider(const FileSpec & directory)105 GDBRemoteProvider(const FileSpec &directory) : Provider(directory) {}
106
107 llvm::raw_ostream *GetHistoryStream();
108 PacketRecorder *GetNewPacketRecorder();
109
SetCallback(std::function<void ()> callback)110 void SetCallback(std::function<void()> callback) {
111 m_callback = std::move(callback);
112 }
113
114 void Keep() override;
115 void Discard() override;
116
117 static char ID;
118
119 private:
120 std::function<void()> m_callback;
121 std::unique_ptr<llvm::raw_fd_ostream> m_stream_up;
122 std::vector<std::unique_ptr<PacketRecorder>> m_packet_recorders;
123 };
124
125 } // namespace repro
126 } // namespace lldb_private
127
128 LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(lldb_private::GDBRemotePacket)
LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(std::vector<lldb_private::GDBRemotePacket>)129 LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(std::vector<lldb_private::GDBRemotePacket>)
130
131 namespace llvm {
132 namespace yaml {
133
134 template <>
135 struct ScalarEnumerationTraits<lldb_private::GDBRemotePacket::Type> {
136 static void enumeration(IO &io, lldb_private::GDBRemotePacket::Type &value);
137 };
138
139 template <> struct ScalarTraits<lldb_private::GDBRemotePacket::BinaryData> {
140 static void output(const lldb_private::GDBRemotePacket::BinaryData &, void *,
141 raw_ostream &);
142
143 static StringRef input(StringRef, void *,
144 lldb_private::GDBRemotePacket::BinaryData &);
145
146 static QuotingType mustQuote(StringRef S) { return QuotingType::None; }
147 };
148
149 template <> struct MappingTraits<lldb_private::GDBRemotePacket> {
150 static void mapping(IO &io, lldb_private::GDBRemotePacket &Packet);
151
152 static StringRef validate(IO &io, lldb_private::GDBRemotePacket &);
153 };
154
155 } // namespace yaml
156 } // namespace llvm
157
158 #endif // LLDB_UTILITY_GDBREMOTE_H
159