summaryrefslogtreecommitdiff
path: root/gdbremote_parser.hpp
blob: 856206afbf8de73a16144f3f76f3764d3a0f2133 (plain)
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/* 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,
    kQueryRcmd,
    kQStartNoAckMode,
    kSetThreadForCont,
    kSetThreadForOps,
    kVMustReplyEmpty,
    kEnableExtendedMode,
    kInterrupt,
    kContinue,
    kContinueAskSupported,
    kReadGeneralRegisters,
    kWriteGeneralRegisters,
    kReadRegister,
    kWriteRegister,
    kReadMemory,
    kWriteMemory,
    kStep,
    kSetBreakpoint,
    kDeleteBreakpoint,
};

struct PacketData {
    virtual ~PacketData() {};
protected:
    PacketData(const PacketData&) = delete;
    PacketData(PacketData&&) = delete;
    PacketData() = default;
};

struct PacketDataSupportedFeatures: public PacketData {
    std::vector<Feature> features{};
    virtual ~PacketDataSupportedFeatures() {}
};

struct PacketDataReadMemory: public PacketData {
    PacketDataReadMemory(uint32_t a_offset, uint32_t a_length)
        : offset(a_offset), length(a_length) {}
    uint32_t offset{}, length{};
    virtual ~PacketDataReadMemory() {}
};

struct PacketDataWriteMemory: public PacketData {
    PacketDataWriteMemory(uint32_t a_offset, uint32_t a_length, std::vector<uint8_t>&& a_data)
        : offset(a_offset), length(a_length), data(std::move(a_data)) {}
    uint32_t offset{}, length{};
    std::vector<uint8_t> data{};
    virtual ~PacketDataWriteMemory() {}
};

struct PacketDataRcmd: public PacketData {
    PacketDataRcmd(std::string&& a_data): data(std::move(a_data)) {}
    std::string data{};
    virtual ~PacketDataRcmd() {}
};

enum class BreakpointType: uint32_t {
    kMin = 0,
    kSoftwareBreakpoint = 0,
    kHardwareBreakpoint = 1,
    kWriteWatchpoint = 2,
    kReadWatchpoint = 3,
    kAccessWatchpoint = 4,
    kUnsupported // Keep it last and unassigned
};

struct PacketDataBreakpoint: public PacketData {
    PacketDataBreakpoint(BreakpointType a_type, uint32_t a_offset, uint32_t a_length)
        : type(a_type), offset(a_offset), length(a_length) {}
    BreakpointType type{};
    uint32_t offset{}, length{};
    virtual ~PacketDataBreakpoint() {}
};

struct PacketDataWriteGeneralRegisters: public PacketData {
    PacketDataWriteGeneralRegisters(std::vector<uint32_t>&& regs): registers(std::move(regs)) {}
    std::vector<uint32_t> registers{};
    virtual ~PacketDataWriteGeneralRegisters() {}
};

struct PacketDataReadRegister: public PacketData {
    PacketDataReadRegister(uint32_t a_reg_id): reg_id(a_reg_id) {}
    uint32_t reg_id{};
    virtual ~PacketDataReadRegister() {}
};

struct PacketDataWriteRegister: public PacketData {
    PacketDataWriteRegister(uint32_t a_reg_id, uint32_t a_value)
        : reg_id(a_reg_id), value(a_value) {}
    uint32_t reg_id{}, value{};
    virtual ~PacketDataWriteRegister() {}
};

struct Packet {
    const PacketType type{};
    const std::unique_ptr<const 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::kQueryRcmd:
            return "qRcmd";
        case PacketType::kQStartNoAckMode:
            return "QStartNoAckMode";
        case PacketType::kSetThreadForCont:
            return "Hc";
        case PacketType::kSetThreadForOps:
            return "Hg";
        case PacketType::kVMustReplyEmpty:
            return "vMustReplyEmpty";
        case PacketType::kEnableExtendedMode:
            return "!";
        case PacketType::kInterrupt:
            return "vCtrlC";
        case PacketType::kContinue:
            return "c";
        case PacketType::kContinueAskSupported:
            return "vCont?";
        case PacketType::kReadGeneralRegisters:
            return "g";
        case PacketType::kWriteGeneralRegisters:
            return "G";
        case PacketType::kReadRegister:
            return "p";
        case PacketType::kWriteRegister:
            return "P";
        case PacketType::kReadMemory:
            return "m";
        case PacketType::kWriteMemory:
            return "M";
        case PacketType::kStep:
            return "s";
        case PacketType::kSetBreakpoint:
            return "Z";
        case PacketType::kDeleteBreakpoint:
            return "z";
        }
        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(std::string packet=std::string{});

    /** 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{};
};

}