From 44a917b3988369996de34b2e8e0228cb4a46d882 Mon Sep 17 00:00:00 2001 From: vhaudiquet Date: Sun, 8 Oct 2023 23:52:26 +0200 Subject: [PATCH] gdbstub: added REUSEADDR/PORT and socket closing --- src/gdbstub/gdbstub.c | 17 ++++++++++++++++- src/gdbstub/gdbstub.h | 1 + src/main.c | 1 + 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/gdbstub/gdbstub.c b/src/gdbstub/gdbstub.c index 337eeac..eb6aec4 100644 --- a/src/gdbstub/gdbstub.c +++ b/src/gdbstub/gdbstub.c @@ -10,6 +10,8 @@ #include /* Sockets */ +#include +#include #include #include #include @@ -31,7 +33,6 @@ void gdbstub_thread_gdb(); void gdbstub_cpu_watcher_thread(); static void gdbstub_handle_ctrlc(); - /* * Receive a packet from client gdb * Ignores the ACK on the way, and only returns the packet (not initial '$' symbol) @@ -120,6 +121,11 @@ static void gdbstub_send_unsupported() void gdbstub_start() { socket_t sock = socket(AF_INET, SOCK_STREAM, 0); + + // Set REUSEADDRESS/PORT so that we dont get "port already in use" on close + setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int)); + setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &(int){1}, sizeof(int)); + if(sock == INVALID_SOCKET) { fprintf(stderr, "Could not create gdbstub server socket: %s\n", strerror(errno)); @@ -145,6 +151,15 @@ void gdbstub_start() gdbstub_server_socket = sock; } +/* + * Gracefully close gdb socket + */ +void gdbstub_stop() +{ + close(gdb_socket); + close(gdbstub_server_socket); +} + /* * Wait for a client (gdb) to connect to this server */ diff --git a/src/gdbstub/gdbstub.h b/src/gdbstub/gdbstub.h index 5a56014..02a2565 100644 --- a/src/gdbstub/gdbstub.h +++ b/src/gdbstub/gdbstub.h @@ -2,6 +2,7 @@ #define GDBSTUB_H void gdbstub_start(); +void gdbstub_stop(); void gdbstub_wait_for_connection(); #endif diff --git a/src/main.c b/src/main.c index eabf85c..784f40b 100644 --- a/src/main.c +++ b/src/main.c @@ -36,6 +36,7 @@ int main(int argc, char** argv) if(gdbstub) { pthread_join(cpu0_thread, 0); + gdbstub_stop(); } else {