gdb-server/main.c

187 lines
4.6 KiB
C

#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 <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];
rdb_read_result_t read_result;
int result;
rdbRequestInit(&req, connfd, reqbuf, BUFLEN);
rdbResponseInit(&res, connfd, resbuf, BUFLEN);
rdbServerInit(&srv, sim);
while (1) {
CommandBuf cmd;
int brk;
uint32_t cycles;
read_result = rdbRequestRead(&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);
}
}
continue;
} else {
printf("received command \"%.*s\"\n", (int) cmd.len, cmd.buf);
fflush(stdout);
result = rdbServerHandleCommand(&srv, &cmd, &res);
if (result != 0) {
return result;
}
rdbRequestSetBlocking(&req, !rdbServerIsRunning(&srv));
rdbRequestReset(&req);
}
}
}
int readROM(VB *sim, char *filename) {
FILE *file = fopen(filename, "rb");
uint8_t *rom;
long size;
if (!file) {
perror("could not open file");
return 1;
}
if (fseek(file, 0, SEEK_END)) {
perror("could not seek file end");
return 1;
}
size = ftell(file);
if (size == -1) {
perror("could not read file size");
return 1;
}
if (fseek(file, 0, SEEK_SET)) {
perror("could not seek file start");
return 1;
}
rom = malloc(size);
if (!rom) {
perror("could not allocate ROM");
return 1;
}
fread(rom, 1, size, file);
if (ferror(file)) {
perror("could not read file");
return 1;
}
if (fclose(file)) {
perror("could not close file");
return 1;
}
vbSetCartROM(sim, rom, size);
return 0;
}
int main(int argc, char** argv) {
VB *sim;
short port;
int fd, connfd;
struct sockaddr_in addr, cliaddr;
socklen_t cliaddrlen;
int response;
if (argc < 2) {
fprintf(stderr, "Please pass a ROM file\n");
return 1;
}
sim = malloc(vbSizeOf());
if (!sim) {
return 1;
}
vbInit(sim);
if (readROM(sim, argv[1])) {
return 1;
}
if (argc > 2) {
char *end;
port = (short) strtol(argv[2], &end, 10);
if (argv[2] == end) {
perror("could not parse port");
return 1;
}
} else {
port = 8080;
}
fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd == -1) {
perror("could not open socket");
return 1;
}
memset(&addr, 0, sizeof(addr));
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
perror("could not bind socket");
return 1;
}
if (listen(fd, 1) == -1) {
perror("could not listen on socket");
return 1;
}
printf("connecting\n");
cliaddrlen = sizeof(cliaddr);
connfd = accept(fd, (struct sockaddr *) &cliaddr, &cliaddrlen);
if (connfd == -1) {
perror("could not accept connection");
return 1;
}
printf("connected\n");
response = server(connfd, sim);
return close(connfd)
|| close(fd)
|| response;
}