Compare commits

..

No commits in common. "07a0b90d369ec054b78f055cdf8e68b1463b6f82" and "ee9a6874d94765da0c58e60c9b5ac400efd5c66e" have entirely different histories.

15 changed files with 157 additions and 364 deletions

3
.gitmodules vendored
View File

@ -1,3 +0,0 @@
[submodule "pvbemu"]
path = pvbemu
url = https://git.virtual-boy.com/PVB/pvbemu.git

View File

@ -1,5 +0,0 @@
# V810 Standalone GDB Server
This is a GDB/LLDB-compatible remote serial protocol server wrapping the [shrooms-vb](https://git.virtual-boy.com/PVB/pvbemu) emulation core. You can use it with [a v810-compatible version of lldb](https://github.com/SupernaviX/v810-llvm/releases) to debug your Virtual Boy games in your favorite IDE.
This program has no input or output, so it's more of a POC than a usable tool. Future plans are to add the server to a full emulator.

View File

@ -2,7 +2,7 @@
#include <hex.h>
#include <string.h>
bool cmdMatchStr(CommandBuf *cmd, const char *str) {
bool cmd_match_str(CommandBuf *cmd, const char *str) {
size_t len = strlen(str);
if (cmd->len < len) return false;
if (!strncmp(cmd->buf, str, len)) {
@ -13,7 +13,7 @@ bool cmdMatchStr(CommandBuf *cmd, const char *str) {
return false;
}
bool cmdMatchHexNumber(CommandBuf *cmd, uint32_t *value) {
bool cmd_match_hex_number(CommandBuf *cmd, uint32_t *value) {
size_t read = 0;
size_t max_len = cmd->len;
if (max_len > 8) max_len = 8;
@ -21,7 +21,7 @@ bool cmdMatchHexNumber(CommandBuf *cmd, uint32_t *value) {
*value = 0;
for (; read < max_len; ++read) {
char digit;
if (!parseHexDigit(cmd->buf[read], &digit)) break;
if (!parse_hex_digit(cmd->buf[read], &digit)) break;
*value = (*value << 4) | digit;
}
if (!read) return false;
@ -31,13 +31,13 @@ bool cmdMatchHexNumber(CommandBuf *cmd, uint32_t *value) {
return true;
}
bool cmdMatchHexBytes(CommandBuf *cmd, const uint32_t count, uint8_t *value) {
bool cmd_match_hex_bytes(CommandBuf *cmd, const uint32_t count, uint8_t *value) {
size_t i;
if (cmd->len < (count * 2)) return false;
for (i = 0; i < count; ++i) {
char hi, lo;
if (!parseHexDigit(cmd->buf[i * 2], &hi)) return false;
if (!parseHexDigit(cmd->buf[(i * 2) + 1], &lo)) return false;
if (!parse_hex_digit(cmd->buf[i * 2], &hi)) return false;
if (!parse_hex_digit(cmd->buf[(i * 2) + 1], &lo)) return false;
value[i] = (hi << 4) | lo;
}
cmd->buf += (count * 2);

2
hex.c
View File

@ -1,6 +1,6 @@
#include <hex.h>
bool parseHexDigit(char digit, char *out) {
bool parse_hex_digit(char digit, char *out) {
if (digit >= '0' && digit <= '9') {
*out = digit - '0';
return true;

View File

@ -11,10 +11,10 @@ typedef struct CommandBuf {
} CommandBuf;
/* Try to consume a string literal. */
bool cmdMatchStr(CommandBuf *cmd, const char *str);
bool cmd_match_str(CommandBuf *cmd, const char *str);
/* Try to consume a base-16 number, and return the value. */
bool cmdMatchHexNumber(CommandBuf *cmd, uint32_t *value);
bool cmd_match_hex_number(CommandBuf *cmd, uint32_t *value);
/* Try to consume a hex-encoded list of bytes, and return the value. */
bool cmdMatchHexBytes(CommandBuf *cmd, const uint32_t count, uint8_t *value);
bool cmd_match_hex_bytes(CommandBuf *cmd, const uint32_t count, uint8_t *value);
#endif

View File

@ -3,6 +3,6 @@
#include <stdbool.h>
bool parseHexDigit(char digit, char *out);
bool parse_hex_digit(char digit, char *out);
#endif

View File

@ -35,9 +35,9 @@ typedef struct RdbRequest {
char chk;
} RdbRequest;
void rdbRequestInit(RdbRequest *req, int connfd, char *buf, size_t buflen);
void rdbRequestReset(RdbRequest *req);
void rdbRequestSetBlocking(RdbRequest *req, bool blocking);
rdb_read_result_t rdbRequestRead(RdbRequest *req, CommandBuf *cmd);
void rdb_request_init(RdbRequest *req, int connfd, char *buf, size_t buflen);
void rdb_request_reset(RdbRequest *req);
void rdb_request_set_blocking(RdbRequest *req, bool blocking);
rdb_read_result_t rdb_request_read(RdbRequest *req, CommandBuf *cmd);
#endif

View File

@ -15,12 +15,12 @@ typedef struct RdbResponse {
bool should_ack;
} RdbResponse;
void rdbResponseInit(RdbResponse *self, int connfd, char *buf, size_t buflen);
void rdbResponseBeginPacket(RdbResponse *self);
bool rdbResponseWriteStr(RdbResponse *self, const char *str);
bool rdbResponseWriteStrHex(RdbResponse *self, const char *str);
bool rdbResponseWriteI8Hex(RdbResponse *self, uint8_t value);
bool rdbResponseWriteI32Hex(RdbResponse *self, uint32_t value);
int rdbResponseSendPacket(RdbResponse *self);
void rdb_response_init(RdbResponse *self, int connfd, char *buf, size_t buflen);
void rdb_response_begin_packet(RdbResponse *self);
bool rdb_response_write_str(RdbResponse *self, const char *str);
bool rdb_response_write_str_hex(RdbResponse *self, const char *str);
bool rdb_response_write_i8_hex(RdbResponse *self, uint8_t value);
bool rdb_response_write_i32_hex(RdbResponse *self, uint32_t value);
int rdb_response_send_packet(RdbResponse *self);
#endif

View File

@ -6,37 +6,6 @@
#include <stdbool.h>
#include <vb.h>
typedef enum rdb_server_state_t {
state_stopped,
state_running,
state_stepping
} rdb_server_state_t;
typedef enum rdb_server_stop_reason_t {
/* not even stopped */
stop_reason_none,
/* we are stepping */
stop_reason_trace,
/* we hit an actual breakpoint */
stop_reason_breakpoint,
/* the user hit pause */
stop_reason_trap,
/* some opcode is not implemented */
stop_reason_not_implemented
} rdb_server_stop_reason_t;
#define RDB_SERVER_MAX_BREAKPOINTS 16
typedef struct RdbServer {
VB *sim;
uint32_t brks[RDB_SERVER_MAX_BREAKPOINTS];
uint32_t brkslen;
rdb_server_state_t state;
rdb_server_stop_reason_t stopreason;
} RdbServer;
void rdbServerInit(RdbServer *srv, VB *sim);
bool rdbServerIsRunning(RdbServer *srv);
int rdbServerHandleCommand(RdbServer *srv, CommandBuf *cmd, RdbResponse *res);
int rdbServerSendStopPacket(RdbServer *srv, RdbResponse *res);
int handle_command(RdbResponse *res, CommandBuf *cmd, VB *sim, bool *running);
#endif

52
main.c
View File

@ -1,77 +1,52 @@
#include <cmdbuf.h>
#include <errno.h>
#include <netinet/in.h>
#include <request.h>
#include <response.h>
#include <server.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <netinet/in.h>
#include <unistd.h>
#include <vb.h>
#define BUFLEN 8096
#define MAX_STEP_CYCLES 20000
int sleepNanos(long int ns) {
struct timespec time;
time.tv_sec = ns / 1000000000;
time.tv_nsec = ns % 1000000000;
return nanosleep(&time, NULL);
}
int server(int connfd, VB *sim) {
RdbRequest req;
RdbResponse res;
RdbServer srv;
char reqbuf[BUFLEN];
char resbuf[BUFLEN];
bool running;
rdb_read_result_t read_result;
int result;
rdbRequestInit(&req, connfd, reqbuf, BUFLEN);
rdbResponseInit(&res, connfd, resbuf, BUFLEN);
rdbServerInit(&srv, sim);
rdb_request_init(&req, connfd, reqbuf, BUFLEN);
rdb_response_init(&res, connfd, resbuf, BUFLEN);
running = false;
while (1) {
CommandBuf cmd;
int brk;
uint32_t cycles;
read_result = rdbRequestRead(&req, &cmd);
read_result = rdb_request_read(&req, &cmd);
if (read_result == read_result_error) {
return -1;
} else if (read_result == read_result_disconnected) {
printf("client has disconnected\n");
return 0;
} else if (read_result == read_result_pending) {
if (rdbServerIsRunning(&srv)) {
cycles = MAX_STEP_CYCLES;
brk = vbEmulate(sim, &cycles);
if (brk) {
/* We stopped for some reason */
if (brk == -1) {
/* the reason was "opcode not implemented" */
srv.state = state_stopped;
srv.stopreason = stop_reason_not_implemented;
}
result = rdbServerSendStopPacket(&srv, &res);
if (result != 0) {
return result;
}
} else {
sleepNanos((MAX_STEP_CYCLES - cycles) * 50);
}
if (running) {
printf("pretend the emulator is running now\n");
sleep(1);
}
continue;
} else {
printf("received command \"%.*s\"\n", (int) cmd.len, cmd.buf);
fflush(stdout);
result = rdbServerHandleCommand(&srv, &cmd, &res);
result = handle_command(&res, &cmd, sim, &running);
if (result != 0) {
return result;
}
rdbRequestSetBlocking(&req, !rdbServerIsRunning(&srv));
rdbRequestReset(&req);
rdb_request_set_blocking(&req, !running);
rdb_request_reset(&req);
}
}
@ -140,6 +115,7 @@ int main(int argc, char** argv) {
if (readROM(sim, argv[1])) {
return 1;
}
vbSetProgramCounter(sim, 0x07000000);
if (argc > 2) {
char *end;

View File

@ -1,9 +1,9 @@
build:
@mkdir -p build
@gcc main.c cmdbuf.c hex.c request.c response.c server.c pvbemu/core/vb.c \
-I include -I pvbemu/core \
@gcc main.c cmdbuf.c hex.c request.c response.c server.c ../vbtest/vb.c \
-I include -I ../vbtest \
-Werror -std=c90 -Wall -Wextra -Wpedantic \
-D _POSIX_C_SOURCE=199309L \
-Wno-unused-parameter -Wno-unused-function \
-o ./build/rdb
clean:
@rm -rf build

1
pvbemu

@ -1 +0,0 @@
Subproject commit 5f563e6cac80d799f6c0251f9f601321e3603ea5

View File

@ -5,26 +5,25 @@
#include <sys/socket.h>
#include <unistd.h>
void rdbRequestInit(RdbRequest *req, int connfd, char *buf, size_t buflen) {
void rdb_request_init(RdbRequest *req, int connfd, char *buf, size_t buflen) {
req->connfd = connfd;
req->outbuf = buf;
req->outbuflen = buflen;
req->blocking = true;
rdbRequestReset(req);
rdb_request_reset(req);
}
void rdbRequestReset(RdbRequest *req) {
void rdb_request_reset(RdbRequest *req) {
req->state = read_state_header;
req->inbuf.len = 0;
req->inbuf.index = 0;
req->chk = 0;
}
void rdbRequestSetBlocking(RdbRequest *req, bool blocking) {
void rdb_request_set_blocking(RdbRequest *req, bool blocking) {
req->blocking = blocking;
}
static rdb_read_result_t readChar(RdbRequest *req, char *in) {
static rdb_read_result_t read_char(RdbRequest *req, char *in) {
if (req->inbuf.index >= req->inbuf.len) {
int flags = req->blocking ? 0 : MSG_DONTWAIT;
ssize_t inlen = recv(req->connfd, req->inbuf.buf, INBUF_LEN, flags);
@ -46,7 +45,7 @@ static rdb_read_result_t readChar(RdbRequest *req, char *in) {
return read_result_success;
}
rdb_read_result_t rdbRequestRead(RdbRequest *req, CommandBuf *cmd) {
rdb_read_result_t rdb_request_read(RdbRequest *req, CommandBuf *cmd) {
rdb_read_result_t res;
char in, hi, lo;
@ -57,7 +56,7 @@ rdb_read_result_t rdbRequestRead(RdbRequest *req, CommandBuf *cmd) {
/* read any acknowledgements and continue */
do {
res = readChar(req, &in);
res = read_char(req, &in);
if (res != read_result_success) return res;
} while (in == '+');
@ -84,7 +83,7 @@ rdb_read_result_t rdbRequestRead(RdbRequest *req, CommandBuf *cmd) {
case read_state_body:
case read_state_body_escape:
while (1) {
res = readChar(req, &in);
res = read_char(req, &in);
if (res != read_result_success) return res;
if (req->state == read_state_body && in == '#') {
@ -116,11 +115,11 @@ rdb_read_result_t rdbRequestRead(RdbRequest *req, CommandBuf *cmd) {
req->state = read_state_checksum_1;
__attribute__ ((fallthrough));
case read_state_checksum_1:
res = readChar(req, &in);
res = read_char(req, &in);
if (res != read_result_success) return res;
/* check the high digit of the checksum */
if (!parseHexDigit(in, &hi)) {
if (!parse_hex_digit(in, &hi)) {
fprintf(stderr, "invalid checksum1\n");
return read_result_error;
}
@ -131,11 +130,11 @@ rdb_read_result_t rdbRequestRead(RdbRequest *req, CommandBuf *cmd) {
req->state = read_state_checksum_2;
__attribute__ ((fallthrough));
case read_state_checksum_2:
res = readChar(req, &in);
res = read_char(req, &in);
if (res != read_result_success) return res;
/* check the high digit of the checksum */
if (!parseHexDigit(in, &lo)) {
if (!parse_hex_digit(in, &lo)) {
fprintf(stderr, "invalid checksum3 %c\n", in);
return read_result_error;
}

View File

@ -4,7 +4,7 @@
#include <string.h>
#include <unistd.h>
static bool writeChar(RdbResponse *res, char out) {
static bool write_char(RdbResponse *res, char out) {
if (res->len >= res->buflen) {
return false;
}
@ -21,7 +21,7 @@ static bool writeChar(RdbResponse *res, char out) {
return true;
}
bool charToHexDigit(char in, char *out) {
bool char_to_hex_digit(char in, char *out) {
if (in & 0xf0) {
return false;
}
@ -34,12 +34,12 @@ bool charToHexDigit(char in, char *out) {
return true;
}
bool charToHexDigits(char in, char *hi, char *lo) {
return charToHexDigit((in & 0xf0) >> 4, hi)
&& charToHexDigit(in & 0x0f, lo);
bool char_to_hex_digits(char in, char *hi, char *lo) {
return char_to_hex_digit((in & 0xf0) >> 4, hi)
&& char_to_hex_digit(in & 0x0f, lo);
}
void rdbResponseInit(RdbResponse *res, int connfd, char *buf, size_t buflen) {
void rdb_response_init(RdbResponse *res, int connfd, char *buf, size_t buflen) {
res->connfd = connfd;
res->buf = buf;
res->buflen = buflen;
@ -48,7 +48,7 @@ void rdbResponseInit(RdbResponse *res, int connfd, char *buf, size_t buflen) {
res->should_ack = true;
}
void rdbResponseBeginPacket(RdbResponse *res) {
void rdb_response_begin_packet(RdbResponse *res) {
res->len = 0;
res->chk = 0;
if (res->should_ack) {
@ -57,44 +57,44 @@ void rdbResponseBeginPacket(RdbResponse *res) {
res->buf[res->len++] = '$';
}
bool rdbResponseWriteStr(RdbResponse *res, const char *str) {
bool rdb_response_write_str(RdbResponse *res, const char *str) {
size_t i, len = strlen(str);
for (i = 0; i < len; ++i) {
if (!writeChar(res, str[i])) {
if (!write_char(res, str[i])) {
return false;
}
}
return true;
}
bool rdbResponseWriteStrHex(RdbResponse *res, const char *str) {
bool rdb_response_write_str_hex(RdbResponse *res, const char *str) {
size_t i, len = strlen(str);
for (i = 0; i < len; ++i) {
char hi, lo;
if (!charToHexDigits(str[i], &hi, &lo)
|| !writeChar(res, hi)
|| !writeChar(res, lo)) {
if (!char_to_hex_digits(str[i], &hi, &lo)
|| !write_char(res, hi)
|| !write_char(res, lo)) {
return false;
}
}
return true;
}
bool rdbResponseWriteI8Hex(RdbResponse *res, uint8_t value) {
bool rdb_response_write_i8_hex(RdbResponse *res, uint8_t value) {
char hi, lo;
return charToHexDigits(value, &hi, &lo)
&& writeChar(res, hi)
&& writeChar(res, lo);
return char_to_hex_digits(value, &hi, &lo)
&& write_char(res, hi)
&& write_char(res, lo);
}
bool rdbResponseWriteI32Hex(RdbResponse *res, uint32_t value) {
return rdbResponseWriteI8Hex(res, (uint8_t) value)
&& rdbResponseWriteI8Hex(res, (uint8_t) (value >> 8))
&& rdbResponseWriteI8Hex(res, (uint8_t) (value >> 16))
&& rdbResponseWriteI8Hex(res, (uint8_t) (value >> 24));
bool rdb_response_write_i32_hex(RdbResponse *res, uint32_t value) {
return rdb_response_write_i8_hex(res, (uint8_t) value)
&& rdb_response_write_i8_hex(res, (uint8_t) (value >> 8))
&& rdb_response_write_i8_hex(res, (uint8_t) (value >> 16))
&& rdb_response_write_i8_hex(res, (uint8_t) (value >> 24));
}
int rdbResponseSendPacket(RdbResponse *res) {
int rdb_response_send_packet(RdbResponse *res) {
char hi, lo;
ssize_t rwrite;
@ -102,7 +102,7 @@ int rdbResponseSendPacket(RdbResponse *res) {
return -1;
}
res->buf[res->len++] = '#';
if (!charToHexDigits(res->chk, &hi, &lo)) {
if (!char_to_hex_digits(res->chk, &hi, &lo)) {
return -1;
}
res->buf[res->len++] = hi;

304
server.c
View File

@ -69,175 +69,103 @@ const uint32_t SYSTEM_REGISTERS[] = {
const uint32_t PC_INDEX = 32 + 13;
static int onExecute(VB *sim, uint32_t address, const uint16_t *code, int length) {
uint32_t i;
RdbServer *srv = (RdbServer *)vbGetUserData(sim);
int handle_command(RdbResponse *res, CommandBuf *cmd, VB *sim, bool *running) {
rdb_response_begin_packet(res);
(void)sim;
(void)code;
(void)length;
/* if we're stopped, just stop */
if (srv->state == state_stopped) {
return 1;
}
/* if we're stepping, we'll run this one instruction but no others */
if (srv->state == state_stepping) {
srv->state = state_stopped;
srv->stopreason = stop_reason_trace;
return 0;
}
for (i = 0; i < srv->brkslen; ++i) {
if (srv->brks[i] == address) {
srv->state = state_stopped;
srv->stopreason = stop_reason_breakpoint;
return 1;
}
}
return 0;
}
static bool addBreakpoint(RdbServer *srv, uint32_t address) {
uint32_t i;
for (i = 0; i < srv->brkslen; ++i) {
if (srv->brks[i] == address) {
/* This breakpoint is already set */
return true;
}
}
if (i == RDB_SERVER_MAX_BREAKPOINTS) {
/* We've added too many breakpoints */
return false;
}
srv->brks[i] = address;
++srv->brkslen;
return true;
}
static void removeBreakpoint(RdbServer *srv, uint32_t address) {
uint32_t i;
for (i = 0; i < srv->brkslen; ++i) {
if (srv->brks[i] == address) {
srv->brks[i] = srv->brks[srv->brkslen - 1];
--srv->brkslen;
return;
}
}
}
void rdbServerInit(RdbServer *srv, VB *sim) {
srv->sim = sim;
srv->brkslen = 0;
srv->state = state_stopped;
srv->stopreason = stop_reason_none;
vbSetUserData(sim, srv);
vbSetExecuteCallback(sim, onExecute);
}
int rdbServerHandleCommand(RdbServer *srv, CommandBuf *cmd, RdbResponse *res) {
rdbResponseBeginPacket(res);
if (cmdMatchStr(cmd, "QStartNoAckMode")) {
if (cmd_match_str(cmd, "QStartNoAckMode")) {
/* The debugger is asking us to no longer ACK messages. */
/* Note that we ack THIS response, because we already called rdbResponseBeginPacket. */
/* Note that we ack THIS response, because we already called rdb_response_begin_packet. */
res->should_ack = false;
rdbResponseWriteStr(res, "OK");
return rdbResponseSendPacket(res);
rdb_response_write_str(res, "OK");
return rdb_response_send_packet(res);
}
if (cmdMatchStr(cmd, "qSupported")) {
if (cmd_match_str(cmd, "qSupported")) {
/* The debugger is asking for a list of features we support. */
rdbResponseWriteStr(res, "no-resumed+;multiprocess;vContSupported");
return rdbResponseSendPacket(res);
rdb_response_write_str(res, "no-resumed+;multiprocess;vContSupported;QNonStop+");
return rdb_response_send_packet(res);
}
if (cmdMatchStr(cmd, "QThreadSuffixSupported")) {
if (cmd_match_str(cmd, "QThreadSuffixSupported")) {
/* The debugger is asking us to include the current thread as a suffix to some responses. */
rdbResponseWriteStr(res, "OK");
return rdbResponseSendPacket(res);
rdb_response_write_str(res, "OK");
return rdb_response_send_packet(res);
}
if (cmdMatchStr(cmd, "QListThreadsInStopReply")) {
if (cmd_match_str(cmd, "QListThreadsInStopReply")) {
/* The debugger is asking us to list all threads whenever we stop running. */
rdbResponseWriteStr(res, "OK");
return rdbResponseSendPacket(res);
rdb_response_write_str(res, "OK");
return rdb_response_send_packet(res);
}
if (cmdMatchStr(cmd, "qHostInfo")) {
if (cmd_match_str(cmd, "qHostInfo")) {
/* The debugger is asking us to describe the "host machine" getting debugged. */
rdbResponseWriteStr(res, "triple:");
rdbResponseWriteStrHex(res, "v810-unknown-vb");
rdbResponseWriteStr(res, ";endian:little;ptrsize:4;");
return rdbResponseSendPacket(res);
rdb_response_write_str(res, "triple:");
rdb_response_write_str_hex(res, "v810-unknown-vb");
rdb_response_write_str(res, ";endian:little;ptrsize:4;");
return rdb_response_send_packet(res);
}
if (cmdMatchStr(cmd, "qProcessInfo")) {
if (cmd_match_str(cmd, "qProcessInfo")) {
/* The debugger is asking us to describe the "process" getting debugged. */
/* We make up a process with id 1. */
rdbResponseWriteStr(res, "pid:1;triple:");
rdbResponseWriteStrHex(res, "v810-unknown-vb");
rdbResponseWriteStr(res, "endian:little;ptrsize:4;");
return rdbResponseSendPacket(res);
rdb_response_write_str(res, "pid:1;triple:");
rdb_response_write_str_hex(res, "v810-unknown-vb");
rdb_response_write_str(res, "endian:little;ptrsize:4;");
return rdb_response_send_packet(res);
}
if (cmdMatchStr(cmd, "qRegisterInfo")) {
if (cmd_match_str(cmd, "qRegisterInfo")) {
uint32_t reg_no;
/* The debugger is asking for information about a specific register. */
if (!cmdMatchHexNumber(cmd, &reg_no)) return 1;
if (!cmd_match_hex_number(cmd, &reg_no)) return 1;
if (reg_no <= PC_INDEX) {
rdbResponseWriteStr(res, REGISTERS[reg_no]);
rdb_response_write_str(res, REGISTERS[reg_no]);
}
return rdbResponseSendPacket(res);
return rdb_response_send_packet(res);
}
if (cmdMatchStr(cmd, "qfThreadInfo")) {
if (cmd_match_str(cmd, "qfThreadInfo")) {
/* The debugger is asking us to list all threads. Return a list with "thread 1". */
rdbResponseWriteStr(res, "mp1.t1");
return rdbResponseSendPacket(res);
rdb_response_write_str(res, "mp1.t1");
return rdb_response_send_packet(res);
}
if (cmdMatchStr(cmd, "qsThreadInfo")) {
if (cmd_match_str(cmd, "qsThreadInfo")) {
/* The debugger is asking us to list all threads. */
rdbResponseWriteStr(res, "l");
return rdbResponseSendPacket(res);
rdb_response_write_str(res, "l");
return rdb_response_send_packet(res);
}
if (cmdMatchStr(cmd, "vCont?")) {
if (cmd_match_str(cmd, "vCont?")) {
/* The debugger is asking which vCont commands we support. */
rdbResponseWriteStr(res, "c;C;s;S");
return rdbResponseSendPacket(res);
rdb_response_write_str(res, "c;C;s;S");
return rdb_response_send_packet(res);
}
if (cmdMatchStr(cmd, "vCont;s:1")) {
/* The debugger wants us to step */
srv->state = state_stepping;
return 0;
}
if (cmdMatchStr(cmd, "qC")) {
if (cmd_match_str(cmd, "qC")) {
/* The debugger is asking for the current thread id. Return "thread 1". */
rdbResponseWriteStr(res, "QCp1.t1");
return rdbResponseSendPacket(res);
rdb_response_write_str(res, "QCp1.t1");
return rdb_response_send_packet(res);
}
if (cmdMatchStr(cmd, "p")) {
if (cmd_match_str(cmd, "p")) {
uint32_t reg_no, reg_value;
/* read a register. */
if (!cmdMatchHexNumber(cmd, &reg_no)) return 1;
if (!cmd_match_hex_number(cmd, &reg_no)) return 1;
if (reg_no > PC_INDEX) {
return rdbResponseSendPacket(res);
return rdb_response_send_packet(res);
}
if (reg_no == PC_INDEX) {
reg_value = vbGetProgramCounter(srv->sim);
reg_value = vbGetProgramCounter(sim);
} else if (reg_no > 31) {
reg_value = vbGetSystemRegister(srv->sim, SYSTEM_REGISTERS[reg_no - 32]);
reg_value = vbGetSystemRegister(sim, SYSTEM_REGISTERS[reg_no - 32]);
} else {
reg_value = vbGetProgramRegister(srv->sim, reg_no);
reg_value = vbGetProgramRegister(sim, reg_no);
}
rdbResponseWriteI32Hex(res, reg_value);
return rdbResponseSendPacket(res);
rdb_response_write_i32_hex(res, reg_value);
return rdb_response_send_packet(res);
}
if (cmdMatchStr(cmd, "P")) {
if (cmd_match_str(cmd, "P")) {
uint32_t reg_no, reg_value;
uint8_t reg_bytes[4];
/* write a register. */
if (!cmdMatchHexNumber(cmd, &reg_no)) return -1;
if (!cmdMatchStr(cmd, "=")) return -1;
if (!cmdMatchHexBytes(cmd, 4, reg_bytes)) return -1;
if (!cmd_match_hex_number(cmd, &reg_no)) return -1;
if (!cmd_match_str(cmd, "=")) return -1;
if (!cmd_match_hex_bytes(cmd, 4, reg_bytes)) return -1;
reg_value = ((uint32_t) (reg_bytes[3]) << 24) |
((uint32_t) (reg_bytes[2]) << 16) |
@ -245,127 +173,57 @@ int rdbServerHandleCommand(RdbServer *srv, CommandBuf *cmd, RdbResponse *res) {
((uint32_t) reg_bytes[0]);
if (reg_no > PC_INDEX) {
return rdbResponseSendPacket(res);
return rdb_response_send_packet(res);
}
if (reg_no == PC_INDEX) {
vbSetProgramCounter(srv->sim, reg_value);
vbSetProgramCounter(sim, reg_value);
} else if (reg_no > 31) {
vbSetSystemRegister(srv->sim, SYSTEM_REGISTERS[reg_no - 32], reg_value);
vbSetSystemRegister(sim, SYSTEM_REGISTERS[reg_no - 32], reg_value);
} else {
vbSetProgramRegister(srv->sim, reg_no, reg_value);
vbSetProgramRegister(sim, reg_no, reg_value);
}
rdbResponseWriteStr(res, "OK");
return rdbResponseSendPacket(res);
rdb_response_write_str(res, "OK");
return rdb_response_send_packet(res);
}
if (cmdMatchStr(cmd, "Hc-1")) {
if (cmd_match_str(cmd, "Hc-1")) {
/* Set the "current thread" for future commands to all threads (thread -1). */
rdbResponseWriteStr(res, "OK");
return rdbResponseSendPacket(res);
rdb_response_write_str(res, "OK");
return rdb_response_send_packet(res);
}
if (cmdMatchStr(cmd, "c")) {
if (cmd_match_str(cmd, "c")) {
/* The debugger has told us to run until we are stopped. */
/* Don't send a response to this until we receive an ETX (when the debugger pauses us). */
srv->state = state_running;
srv->stopreason = stop_reason_none;
*running = true;
return 0;
}
if (cmdMatchStr(cmd, "\x03")) {
if (cmd_match_str(cmd, "\x03")) {
/* Received an ETX, indicating that the server wants to cancel the "c" command from before. */
srv->state = state_stopped;
srv->stopreason = stop_reason_trap;
*running = false;
/* Send the response to the "c" command from before. */
return rdbServerSendStopPacket(srv, res);
rdb_response_write_str(res, "T05thread:p1.t1;threads:p1.t1");
return rdb_response_send_packet(res);
}
if (cmdMatchStr(cmd, "?")) {
if (cmd_match_str(cmd, "?")) {
/* The debugger has asked us why we stopped */
return rdbServerSendStopPacket(srv, res);
rdb_response_write_str(res, "T");
rdb_response_write_str(res, *running ? "00" : "05");
rdb_response_write_str(res, "thread:p1.t1;threads:p1.t1;");
return rdb_response_send_packet(res);
}
if (cmdMatchStr(cmd, "m")) {
if (cmd_match_str(cmd, "m")) {
/* read memory */
uint32_t i, address, len;
if (!cmdMatchHexNumber(cmd, &address)) return -1;
if (!cmdMatchStr(cmd, ",")) {
/* if the server asks for too much memory, just error */
return rdbResponseSendPacket(res);
};
if (!cmdMatchHexNumber(cmd, &len)) return -1;
if (!cmd_match_hex_number(cmd, &address)) return -1;
if (!cmd_match_str(cmd, ",")) return -1;
if (!cmd_match_hex_number(cmd, &len)) return -1;
for (i = 0; i < len; ++i) {
uint8_t byte = vbRead(srv->sim, address + i, VB_U8);
rdbResponseWriteI8Hex(res, byte);
uint8_t byte = vbRead(sim, address + i, VB_U8);
rdb_response_write_i8_hex(res, byte);
}
return rdbResponseSendPacket(res);
}
if (cmdMatchStr(cmd, "M")) {
/* write memory */
uint32_t i, address, len;
if (!cmdMatchHexNumber(cmd, &address)) return -1;
if (!cmdMatchStr(cmd, ",")) return -1;
if (!cmdMatchHexNumber(cmd, &len)) return -1;
if (!cmdMatchStr(cmd, ":")) return -1;
for (i = 0; i < len; ++i) {
uint8_t byte;
if (!cmdMatchHexBytes(cmd, 1, &byte)) return -1;
vbWrite(srv->sim, address + i, VB_U8, byte);
}
rdbResponseWriteStr(res, "OK");
return rdbResponseSendPacket(res);
}
if (cmdMatchStr(cmd, "Z0,")) {
/* set a breakpoint */
uint32_t address;
if (!cmdMatchHexNumber(cmd, &address)) return -1;
if (!cmdMatchStr(cmd, ",0")) return -1;
if (!addBreakpoint(srv, address)) return -1;
rdbResponseWriteStr(res, "OK");
return rdbResponseSendPacket(res);
}
if (cmdMatchStr(cmd, "z0,")) {
/* remove a breakpoint */
uint32_t address;
if (!cmdMatchHexNumber(cmd, &address)) return -1;
if (!cmdMatchStr(cmd, ",0")) return -1;
removeBreakpoint(srv, address);
rdbResponseWriteStr(res, "OK");
return rdbResponseSendPacket(res);
return rdb_response_send_packet(res);
}
fprintf(stderr, "Unrecognized command.\n");
return rdbResponseSendPacket(res);
return rdb_response_send_packet(res);
}
bool rdbServerIsRunning(RdbServer *srv) {
/* stepping counts */
return srv->state != state_stopped;
}
int rdbServerSendStopPacket(RdbServer *srv, RdbResponse *res) {
rdbResponseBeginPacket(res);
switch (srv->stopreason) {
case stop_reason_trace:
case stop_reason_none:
rdbResponseWriteStr(res, "T00");
break;
default:
rdbResponseWriteStr(res, "T05");
}
rdbResponseWriteStr(res, "thread:p1.t1;threads:p1.t1;");
switch (srv->stopreason) {
case stop_reason_trace:
rdbResponseWriteStr(res, "reason:trace;");
break;
case stop_reason_breakpoint:
rdbResponseWriteStr(res, "reason:breakpoint;");
break;
case stop_reason_trap:
rdbResponseWriteStr(res, "reason:trap;");
break;
case stop_reason_not_implemented:
rdbResponseWriteStr(res, "reason:exception;description:opcode not implemented;");
break;
default: break;
}
return rdbResponseSendPacket(res);
}