qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Paul Noalhyt <paul@redballoonsecurity.com>
To: qemu-devel@nongnu.org
Cc: "Paul Noalhyt" <paul@redballoonsecurity.com>,
	"Alex Bennée" <alex.bennee@linaro.org>
Subject: [PATCH] gdbstub user: fix to handle the gdb disconnect command and allow reconnecting
Date: Tue, 23 May 2023 14:44:09 +0000	[thread overview]
Message-ID: <20230523144408.2278-1-paul@redballoonsecurity.com> (raw)

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



                 reply	other threads:[~2023-05-23 15:31 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230523144408.2278-1-paul@redballoonsecurity.com \
    --to=paul@redballoonsecurity.com \
    --cc=alex.bennee@linaro.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).