1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
/* SPDX-License-Identifier: Unlicense
*/
#pragma once
#include <cstdint>
#include <string>
#include <vector>
#include <memory>
namespace GDBRemote {
enum class FeatureSupportMark: uint8_t {
kNone = 0,
kPlus,
kMinus,
kQuestion,
};
enum class FeatureName: int {
kUnknown = 0,
kMultiprocess,
kSoftwareBreak,
kHardwareBreak,
kQRelocInsn,
kForkEvents,
kVForkEvents,
kExecEvents,
kVContSuppurted,
kQThreadEvents,
kNoResumed,
kMemoryTagging,
kPacketSize,
};
struct Feature {
const FeatureName name{};
const FeatureSupportMark mark{};
const bool has_value{};
const int value{};
const std::string raw{};
};
enum class PacketType: int {
kNone = 0,
kQuerySupported,
kQueryHaltReason,
kQueryC,
kQueryFThreadInfo,
kQuerySThreadInfo,
kQueryRTOSThreadInfo,
kQueryAttached,
kQueryTraceStatus,
kSetThreadForCont,
kSetThreadForOps,
kVMustReplyEmpty,
kEnableExtendedMode,
};
struct PacketData {
PacketData() = delete;
PacketData(const PacketData&) = delete;
PacketData(PacketData&&) = delete;
virtual ~PacketData() {}
};
struct PacketDataSupportedFeatures: public PacketData {
std::vector<Feature> features{};
virtual ~PacketDataSupportedFeatures() {}
};
struct Packet {
const PacketType type{};
const std::unique_ptr<PacketData> data{nullptr};
/** Convert raw packet data into a Packet
*
* Packet data is the data extracted by ExchangeContext::Consume function.
*/
static Packet Parse(std::string packet_data);
/** None packet creator */
static Packet None() { return Packet{}; }
/** Stringify PacketType for debugging purposes */
static const char* PacketTypeToString(PacketType type) {
switch (type) {
case PacketType::kNone:
return "None";
case PacketType::kQuerySupported:
return "qSupported";
case PacketType::kQueryHaltReason:
return "?";
case PacketType::kQueryC:
return "qC";
case PacketType::kQueryFThreadInfo:
return "qfThreadInfo";
case PacketType::kQuerySThreadInfo:
return "qsThreadInfo";
case PacketType::kQueryRTOSThreadInfo:
return "qL";
case PacketType::kQueryAttached:
return "qAttached";
case PacketType::kQueryTraceStatus:
return "qTStatus";
case PacketType::kSetThreadForCont:
return "Hc";
case PacketType::kSetThreadForOps:
return "Hg";
case PacketType::kVMustReplyEmpty:
return "vMustReplyEmpty";
case PacketType::kEnableExtendedMode:
return "!";
}
return "<unknown>";
}
};
struct ExchangeResult {
const std::string packet{};
const std::string ack{};
ExchangeResult(std::string a_packet, std::string a_response)
: packet(a_packet), ack(a_response) {}
static std::unique_ptr<ExchangeResult> Nak(std::string data=std::string{})
{
return std::make_unique<ExchangeResult>(data, std::string{"-"});
}
static std::unique_ptr<ExchangeResult> Ack(std::string data=std::string{})
{
return std::make_unique<ExchangeResult>(data, std::string{"+"});
}
};
class ExchangeContext {
public:
/** Consume next byte from input stream from GDB client
*
* Returns packet data and acknowledge response in case if a valid packet
* fully received.
*
* Returns nullptr if is in progress.
*/
std::unique_ptr<ExchangeResult> Consume(uint8_t byte);
/** Creates raw packet from packet data to be sent into socket directly
*
* It is not static nor const because after sending a packet an
* ExchangeContext must be set into awaiting acknowledge response state
* (i.e. '+' or '-'). Otherwise we will be unable to resend packet in case
* if '-' negative acknowledge is received.
*/
std::string WrapDataToSend(Packet packet=Packet{});
/** Returns last recognized packet
*
* For debugging purposes.
*/
const std::string GetLastPacket() { return _last_packet; }
private:
enum class ParsingState {
kIdle,
kPacketData,
kPacketDataBinSecond,
kChecksum1,
kChecksum2,
};
void transit(ParsingState);
static const char* parsingStateToString(ParsingState state);
ParsingState _parsing_state{ParsingState::kIdle};
std::string _packet_data{};
std::string _last_packet{};
uint8_t _checksum{};
uint8_t _given_checksum_first_byte{};
};
}
|