gdbstub: added REUSEADDR/PORT and socket closing

master
vhaudiquet 1 year ago
parent c3fa088a38
commit 44a917b398
  1. 17
      src/gdbstub/gdbstub.c
  2. 1
      src/gdbstub/gdbstub.h
  3. 1
      src/main.c

@ -10,6 +10,8 @@
#include <string.h> #include <string.h>
/* Sockets */ /* Sockets */
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h> #include <sys/types.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <arpa/inet.h> #include <arpa/inet.h>
@ -31,7 +33,6 @@ void gdbstub_thread_gdb();
void gdbstub_cpu_watcher_thread(); void gdbstub_cpu_watcher_thread();
static void gdbstub_handle_ctrlc(); static void gdbstub_handle_ctrlc();
/* /*
* Receive a packet from client gdb * Receive a packet from client gdb
* Ignores the ACK on the way, and only returns the packet (not initial '$' symbol) * 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() void gdbstub_start()
{ {
socket_t sock = socket(AF_INET, SOCK_STREAM, 0); 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) if(sock == INVALID_SOCKET)
{ {
fprintf(stderr, "Could not create gdbstub server socket: %s\n", strerror(errno)); fprintf(stderr, "Could not create gdbstub server socket: %s\n", strerror(errno));
@ -145,6 +151,15 @@ void gdbstub_start()
gdbstub_server_socket = sock; 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 * Wait for a client (gdb) to connect to this server
*/ */

@ -2,6 +2,7 @@
#define GDBSTUB_H #define GDBSTUB_H
void gdbstub_start(); void gdbstub_start();
void gdbstub_stop();
void gdbstub_wait_for_connection(); void gdbstub_wait_for_connection();
#endif #endif

@ -36,6 +36,7 @@ int main(int argc, char** argv)
if(gdbstub) if(gdbstub)
{ {
pthread_join(cpu0_thread, 0); pthread_join(cpu0_thread, 0);
gdbstub_stop();
} }
else else
{ {

Loading…
Cancel
Save