[PATCH] gdbstub user: fix to handle the gdb disconnect command and allow reconnecting

Paul Noalhyt posted 1 patch 11 months, 2 weeks ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20230523144408.2278-1-paul@redballoonsecurity.com
Maintainers: "Alex Bennée" <alex.bennee@linaro.org>, "Philippe Mathieu-Daudé" <philmd@linaro.org>
gdbstub/user.c | 50 +++++++++++++++++++++++++++++++++++++++-----------
1 file changed, 39 insertions(+), 11 deletions(-)
[PATCH] gdbstub user: fix to handle the gdb disconnect command and allow reconnecting
Posted by Paul Noalhyt 11 months, 2 weeks ago
Hi,

I'm sending this to patch an issue I noticed in qemu-arm (user mode).
Noticed that when disconnecting gdb (gdb `disconnect` command), the
program would resume execution, instead of staying halted on the current
instruction. I modified the code so that the socket stays open waiting for
future connections.

This is my first contribution to the QEMU codebase.

Paul.

---
 gdbstub/user.c | 50 +++++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 39 insertions(+), 11 deletions(-)

diff --git a/gdbstub/user.c b/gdbstub/user.c
index 5b375be1d9..e583d472ae 100644
--- a/gdbstub/user.c
+++ b/gdbstub/user.c
@@ -24,6 +24,7 @@
 /* User-mode specific state */
 typedef struct {
     int fd;
+    int tcp_fd;
     char *socket_path;
     int running_state;
 } GDBUserState;
@@ -115,6 +116,10 @@ void gdb_exit(int code)
     }
 }
 
+static bool gdb_accept_tcp(int gdb_fd, bool reopen);
+static bool gdb_accept_socket(int gdb_fd, bool reopen);
+static int gdbserver_open_socket(const char *path);
+
 int gdb_handlesig(CPUState *cpu, int sig)
 {
     char buf[256];
@@ -160,14 +165,29 @@ int gdb_handlesig(CPUState *cpu, int sig)
             }
         } else {
             /*
-             * XXX: Connection closed.  Should probably wait for another
-             * connection before continuing.
+             * Connection closed.  Wait for another connection
+             * before continuing.
              */
             if (n == 0) {
                 close(gdbserver_user_state.fd);
             }
-            gdbserver_user_state.fd = -1;
-            return sig;
+            bool reopen_error = false;
+            if (gdbserver_user_state.socket_path) {
+                unlink(gdbserver_user_state.socket_path);
+                int gdb_fd;
+                gdb_fd =
+                    gdbserver_open_socket(gdbserver_user_state.socket_path);
+                if (gdb_fd < 0 || !gdb_accept_socket(gdb_fd, true)) {
+                    reopen_error = true;
+                }
+            } else if (!gdb_accept_tcp(gdbserver_user_state.tcp_fd, true)) {
+                reopen_error = true;
+            }
+            if (reopen_error) {
+                close(gdbserver_user_state.fd);
+                gdbserver_user_state.fd = -1;
+                return sig;
+            }
         }
     }
     sig = gdbserver_state.signal;
@@ -201,7 +221,7 @@ static void gdb_accept_init(int fd)
     gdb_has_xml = false;
 }
 
-static bool gdb_accept_socket(int gdb_fd)
+static bool gdb_accept_socket(int gdb_fd, bool reopen)
 {
     int fd;
 
@@ -215,8 +235,11 @@ static bool gdb_accept_socket(int gdb_fd)
             break;
         }
     }
-
-    gdb_accept_init(fd);
+    if (reopen) {
+        gdbserver_user_state.fd = fd;
+    } else {
+        gdb_accept_init(fd);
+    }
     return true;
 }
 
@@ -249,7 +272,7 @@ static int gdbserver_open_socket(const char *path)
     return fd;
 }
 
-static bool gdb_accept_tcp(int gdb_fd)
+static bool gdb_accept_tcp(int gdb_fd, bool reopen)
 {
     struct sockaddr_in sockaddr = {};
     socklen_t len;
@@ -274,7 +297,11 @@ static bool gdb_accept_tcp(int gdb_fd)
         return false;
     }
 
-    gdb_accept_init(fd);
+    if (reopen) {
+        gdbserver_user_state.fd = fd;
+    } else {
+        gdb_accept_init(fd);
+    }
     return true;
 }
 
@@ -326,9 +353,10 @@ int gdbserver_start(const char *port_or_path)
         return -1;
     }
 
-    if (port > 0 && gdb_accept_tcp(gdb_fd)) {
+    if (port > 0 && gdb_accept_tcp(gdb_fd, false)) {
+        gdbserver_user_state.tcp_fd = gdb_fd;
         return 0;
-    } else if (gdb_accept_socket(gdb_fd)) {
+    } else if (gdb_accept_socket(gdb_fd, false)) {
         gdbserver_user_state.socket_path = g_strdup(port_or_path);
         return 0;
     }
-- 
2.30.2