From ed3cf413dfe5f874f203f8b6a696af29cfc47dcd Mon Sep 17 00:00:00 2001 From: Oxore Date: Tue, 30 Aug 2022 16:49:38 +0300 Subject: Impl emulator stepping with GDB --- gdbremote_parser.cpp | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) (limited to 'gdbremote_parser.cpp') diff --git a/gdbremote_parser.cpp b/gdbremote_parser.cpp index d8f4f50..204690d 100644 --- a/gdbremote_parser.cpp +++ b/gdbremote_parser.cpp @@ -24,6 +24,13 @@ static inline bool isseparator(uint8_t byte) return ':' == byte || ';' == byte || ',' == byte; } +static inline bool IsTokenTypeSeparator(TokenType token_type) +{ + return token_type == TokenType::kSeparatorColon || + token_type == TokenType::kSeparatorComma || + token_type == TokenType::kSeparatorSemicolon; +} + static inline TokenType separatorTypeFromByte(uint8_t byte) { if (':' == byte) return TokenType::kSeparatorColon; @@ -143,14 +150,47 @@ struct Command { { return Packet{PacketType::kInterrupt}; } - static Packet parseContinue(const std::vector&&) + static Packet parseContinue(const std::vector&& tokens) { + const std::string first = tokens[0].data; + constexpr size_t command_len = strlen("vCont"); + // Check if it is "vCont?" command + if (first.length() > command_len && first[command_len] == '?') { + return Packet{PacketType::kContinueAskSupported}; + } + // TODO arguments return Packet{PacketType::kContinue}; } static Packet parseReadGeneralRegisters(const std::vector&&) { return Packet{PacketType::kReadGeneralRegisters}; } + static Packet parseReadMemory(const std::vector&& tokens) + { + if (tokens.size() < 3 || + tokens[0].type != TokenType::kArbitrary || + !IsTokenTypeSeparator(tokens[1].type) || + tokens[2].type != TokenType::kNumeric) + { + return Packet::None(); + } + constexpr size_t command_len = strlen("m"); + const std::string first = tokens[0].data; + const std::string second = tokens[2].data; + const uint32_t offset = strtol( + first.substr(command_len, first.length()-command_len).c_str(), + nullptr, + 16); + const uint32_t lenght = strtol(second.c_str(), nullptr, 16); + return Packet{ + PacketType::kReadMemory, + std::make_unique(offset, lenght), + }; + } + static Packet parseStep(const std::vector&&) + { + return Packet{PacketType::kStep}; + } }; static const Command commands[] = { @@ -169,7 +209,9 @@ static const Command commands[] = { { "\x03", Command::parseInterrupt }, { "vCtrlC", Command::parseInterrupt }, { "vCont", Command::parseContinue }, + { "m", Command::parseReadMemory }, { "g", Command::parseReadGeneralRegisters }, + { "s", Command::parseStep }, }; Packet Packet::Parse(std::string packet_data) -- cgit v1.2.3