Skip to contentSkip to conversation

gdbstub: propagate Error to gdbserver_start (and other device setups)

RFC PATCH In review Alex Bennée 2 participants 2 messages Jan 8, 2025

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 gdbstub: propagate Error to gdbserver_start (and other device setups)

This started as a clean-up to properly pass a Error handler to the gdbserver_start so we could do the right thing for command line and HMP invocations. To that end everything that used foreach_device_config() and failed was also an immediate exit case so I mechanically tweaked all the helper functions and simplified some.

Currently an RFC, I suspect we want to rename the foreach function to something more pithy like foreach_device_config_or_exit().

Cc Ilya Leoshkevich <[email protected]>
---
 include/exec/gdbstub.h |  8 +++-
 gdbstub/system.c       | 22 +++++-----
 gdbstub/user.c         | 20 +++++----
 linux-user/main.c      |  6 +--
 monitor/hmp-cmds.c     |  2 +-
 system/vl.c            | 98 ++++++++++++++++--------------------------
 6 files changed, 70 insertions(+), 86 deletions(-)
include/exec/gdbstub.h +7 -1
index d73f424f56..0675b0b646 100644
@@ -49,12 +49,18 @@ void gdb_unregister_coprocessor_all(CPUState *cpu);
 /**
  * gdbserver_start: start the gdb server
  * @port_or_device: connection spec for gdb
+ * @errp: error handle
  *
  * For CONFIG_USER this is either a tcp port or a path to a fifo. For
  * system emulation you can use a full chardev spec for your gdbserver
  * port.
+ *
+ * The error handle should be either &error_fatal (for start-up) or
+ * &error_warn (for QMP/HMP initiated sessions).
+ *
+ * Returns true when server successfully started.
  */
-int gdbserver_start(const char *port_or_device);
+bool gdbserver_start(const char *port_or_device, Error **errp);
 
 /**
  * gdb_feature_builder_init() - Initialize GDBFeatureBuilder.
gdbstub/system.c +12 -10
index 2d9fdff2fe..8ce79fa88c 100644
@@ -330,26 +330,27 @@ static void create_processes(GDBState *s)
     gdb_create_default_process(s);
 }
 
-int gdbserver_start(const char *device)
+bool gdbserver_start(const char *device, Error **errp)
 {
     Chardev *chr = NULL;
     Chardev *mon_chr;
     g_autoptr(GString) cs = g_string_new(device);
 
     if (!first_cpu) {
-        error_report("gdbstub: meaningless to attach gdb to a "
-                     "machine without any CPU.");
-        return -1;
+        error_setg(errp, "gdbstub: meaningless to attach gdb to a "
+                   "machine without any CPU.");
+        return false;
     }
 
     if (!gdb_supports_guest_debug()) {
-        error_report("gdbstub: current accelerator doesn't "
-                     "support guest debugging");
-        return -1;
+        error_setg(errp, "gdbstub: current accelerator doesn't "
+                   "support guest debugging");
+        return false;
     }
 
     if (cs->len == 0) {
-        return -1;
+        error_setg(errp, "gdbstub: missing connection string");
+        return false;
     }
 
     trace_gdbstub_op_start(cs->str);
@@ -374,7 +375,8 @@ int gdbserver_start(const char *device)
          */
         chr = qemu_chr_new_noreplay("gdb", cs->str, true, NULL);
         if (!chr) {
-            return -1;
+            error_setg(errp, "gdbstub: couldn't create chardev");
+            return false;
         }
     }
 
@@ -406,7 +408,7 @@ int gdbserver_start(const char *device)
     gdbserver_system_state.mon_chr = mon_chr;
     gdb_syscall_reset();
 
-    return 0;
+    return true;
 }
 
 static void register_types(void)
gdbstub/user.c +11 -9
index 0b4bfa9c48..fb8f6867ea 100644
@@ -13,6 +13,7 @@
 #include "qemu/bitops.h"
 #include "qemu/cutils.h"
 #include "qemu/sockets.h"
+#include "qapi/error.h"
 #include "exec/hwaddr.h"
 #include "exec/tb-flush.h"
 #include "exec/gdbstub.h"
@@ -372,15 +373,15 @@ static bool gdb_accept_tcp(int gdb_fd)
     return true;
 }
 
-static int gdbserver_open_port(int port)
+static int gdbserver_open_port(int port, Error **errp)
 {
     struct sockaddr_in sockaddr;
     int fd, ret;
 
     fd = socket(PF_INET, SOCK_STREAM, 0);
     if (fd < 0) {
-        perror("socket");
-        return -1;
+        error_setg(errp, "Failed to bind socket: %s", strerror(errno));
+        return false;
     }
     qemu_set_cloexec(fd);
 
@@ -405,31 +406,32 @@ static int gdbserver_open_port(int port)
     return fd;
 }
 
-int gdbserver_start(const char *port_or_path)
+bool gdbserver_start(const char *port_or_path, Error **errp)
 {
     int port = g_ascii_strtoull(port_or_path, NULL, 10);
     int gdb_fd;
 
     if (port > 0) {
-        gdb_fd = gdbserver_open_port(port);
+        gdb_fd = gdbserver_open_port(port, errp);
     } else {
         gdb_fd = gdbserver_open_socket(port_or_path);
     }
 
     if (gdb_fd < 0) {
-        return -1;
+        return false;
     }
 
     if (port > 0 && gdb_accept_tcp(gdb_fd)) {
-        return 0;
+        return true;
     } else if (gdb_accept_socket(gdb_fd)) {
         gdbserver_user_state.socket_path = g_strdup(port_or_path);
-        return 0;
+        return true;
     }
 
     /* gone wrong */
     close(gdb_fd);
-    return -1;
+    error_setg(errp, "gdbstub: failed to accept connection");
+    return false;
 }
 
 void gdbserver_fork_start(void)
linux-user/main.c +1 -5
index b97634a32d..7198fa0986 100644
@@ -1023,11 +1023,7 @@ int main(int argc, char **argv, char **envp)
     target_cpu_copy_regs(env, regs);
 
     if (gdbstub) {
-        if (gdbserver_start(gdbstub) < 0) {
-            fprintf(stderr, "qemu: could not open gdbserver on %s\n",
-                    gdbstub);
-            exit(EXIT_FAILURE);
-        }
+        gdbserver_start(gdbstub, &error_fatal);
         gdb_handlesig(cpu, 0, NULL, NULL, 0);
     }
 
monitor/hmp-cmds.c +1 -1
index 80b2e5ff9f..0aa22e1ae2 100644
@@ -285,7 +285,7 @@ void hmp_gdbserver(Monitor *mon, const QDict *qdict)
         device = "tcp::" DEFAULT_GDBSTUB_PORT;
     }
 
-    if (gdbserver_start(device) < 0) {
+    if (!gdbserver_start(device, &error_warn)) {
         monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
                        device);
     } else if (strcmp(device, "none") == 0) {
system/vl.c +38 -60

185 lines not on this page · raw message

Signature · 1 line
2.39.5
Acked-by on v1 1/1
#1 lore raw
Quoted 8 lines · Alex Bennée

This started as a clean-up to properly pass a Error handler to the gdbserver_start so we could do the right thing for command line and HMP invocations. To that end everything that used foreach_device_config() and failed was also an immediate exit case so I mechanically tweaked all the helper functions and simplified some.

Currently an RFC, I suspect we want to rename the foreach function to something more pithy like foreach_device_config_or_exit().

I agree with such a rename, adding comments like /* failure will exit via error_fatal */ everywhere is not optimal.

Quoted 11 lines
Cc Ilya Leoshkevich <[email protected]>

---
 include/exec/gdbstub.h |  8 +++-
 gdbstub/system.c       | 22 +++++-----
 gdbstub/user.c         | 20 +++++----
 linux-user/main.c      |  6 +--
 monitor/hmp-cmds.c     |  2 +-
 system/vl.c            | 98 ++++++++++++++++------------------------ --

 6 files changed, 70 insertions(+), 86 deletions(-)