Skip to contentSkip to conversation

gdbstub/next - bug fixes and cleanups (pre-PR)

PATCH In review Alex Bennée 5 participants 20 messages Feb 3 – Feb 4

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover gdbstub/next - bug fixes and cleanups (pre-PR)

Flushing my queue for gdbstub/next and I'll roll a PR by the end of the week to get the bug fixes in. The other re-factoring patches are towards a longer term goal of supporting more stop packet reply types but aren't critical to merge save reducing the delta. I won't include them in the PR unless reviewed:

gdbstub/user: rename and use bool for running_state include/gdbstub: fix typo
gdbstub: pass GString to gdb_build_stop_packet gdbstub/user: localise variables for reading gdb socket
gdbstub: extract stop reply message construction
gdbstub: remove the need for goto cleanup

Alex.

Alex Bennée (8):
meson-buildoptions: Remove duplicated entry of --gdb in the help text
Makefile: add python script dependency for meson-buildoptions.sh
gdbstub: remove the need for goto cleanup
gdbstub: extract stop reply message construction gdbstub/user: localise variables for reading gdb socket
gdbstub: pass GString to gdb_build_stop_packet include/gdbstub: fix typo
gdbstub/user: rename and use bool for running_state

Yodel Eldar (3):
include/gdbstub/syscalls: Add EIO and ENOSYS GDB File-I/O errno values
gdbstub: Export host_to_gdb_errno File-I/O helper function gdbstub/user-target: Convert host errno to GDB File-I/O errno

 Makefile                      |  2 +-
 gdbstub/internals.h           | 10 ++++++++
 include/gdbstub/commands.h    |  2 +-
 include/gdbstub/syscalls.h    | 11 +++++++++
 gdbstub/gdbstub.c             | 45 +++++++++++++++--------------------
 gdbstub/syscalls.c            | 36 ++++++++++++++++++++++++++++
 gdbstub/system.c              | 11 +++++++++
 gdbstub/user-target.c         | 13 ++++++----
 gdbstub/user.c                | 40 ++++++++++++++++++++-----------
 target/m68k/m68k-semi.c       | 29 ----------------------
 scripts/meson-buildoptions.py |  9 ++++++-
 scripts/meson-buildoptions.sh |  1 -
 12 files changed, 132 insertions(+), 77 deletions(-)
Signature · 1 line
2.47.3
#1 lore raw

PATCH 1/11 meson-buildoptions: Remove duplicated entry of --gdb in the help text

Since --gdb has recently been added to meson_options.txt, the help text shows up twice in the output of "./configure --help":

 $ ./configure --help | grep gdb
   --gdb=GDB-path           gdb to use for gdbstub tests [/usr/bin/gdb]
   --gdb=VALUE              Path to GDB

As the probed value is of interest we want to keep the configure text so make meson-buildoptions able to handle deferring to configure.

Fixes b46b3818cf8 ("tests/functional: Provide GDB to the functional tests")
---
 scripts/meson-buildoptions.py | 9 ++++++++-
 scripts/meson-buildoptions.sh | 1 -
 2 files changed, 8 insertions(+), 2 deletions(-)
scripts/meson-buildoptions.py +8 -1
index e636d258e8a..9e3a75c8b11 100644
@@ -53,6 +53,11 @@
     "werror",
 }
 
+# Options that configure prints help for, so we can skip
+CONFIGURE_HELP = {
+    "gdb",
+}
+
 # Builtin options that should be definable via configure.  Some of the others
 # we really do not want (e.g. c_args is defined via the native file, not
 # via -D, because it's a mix of CFLAGS and --extra-cflags); for specific
@@ -189,7 +194,9 @@ def print_help(options):
         key = cli_help_key(opt)
         # The first section includes options that have an arguments,
         # and booleans (i.e., only one of enable/disable makes sense)
-        if require_arg(opt):
+        if opt["name"] in CONFIGURE_HELP:
+            pass
+        elif require_arg(opt):
             metavar = cli_metavar(opt)
             left = f"--{key}={metavar}"
             help_line(left, opt, 27, True)
scripts/meson-buildoptions.sh +0 -1
index 3d0d1323446..e8edc5252a3 100644
@@ -58,7 +58,6 @@ meson_options_help() {
   printf "%s\n" '  --enable-ubsan           enable undefined behaviour sanitizer'
   printf "%s\n" '  --firmwarepath=VALUES    search PATH for firmware files [share/qemu-'
   printf "%s\n" '                           firmware]'
-  printf "%s\n" '  --gdb=VALUE              Path to GDB'
   printf "%s\n" '  --iasl=VALUE             Path to ACPI disassembler'
   printf "%s\n" '  --includedir=VALUE       Header file directory [include]'
   printf "%s\n" '  --interp-prefix=VALUE    where to find shared libraries etc., use %M for'
Signature · 1 line
2.47.3
#2 lore raw

PATCH 2/11 Makefile: add python script dependency for meson-buildoptions.sh

In reply to Alex Bennée

If we update the script we should also make sure we regenerate the help text.

---
 Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Makefile +1 -1
index 9fb55dcf330..54547a37b1a 100644
@@ -130,7 +130,7 @@ Makefile.mtest: build.ninja scripts/mtest2make.py
 
 .PHONY: update-buildoptions
 all update-buildoptions: $(SRC_PATH)/scripts/meson-buildoptions.sh
-$(SRC_PATH)/scripts/meson-buildoptions.sh: $(SRC_PATH)/meson_options.txt
+$(SRC_PATH)/scripts/meson-buildoptions.sh: $(SRC_PATH)/meson_options.txt $(SRC_PATH)/scripts/meson-buildoptions.py
 	$(MESON) introspect --buildoptions $(SRC_PATH)/meson.build | $(PYTHON) \
 	  scripts/meson-buildoptions.py > [email protected] && mv [email protected] $@
 endif
Signature · 1 line
2.47.3
#3 lore raw

PATCH 3/11 gdbstub: remove the need for goto cleanup

In reply to Alex Bennée

We already set a default error reply which we can only overwrite if we successfully follow the chain of checks. Initialise the variables as NULL and use that to gate the construction of the filled out stop/reply packet.

---
 gdbstub/gdbstub.c | 30 +++++++++++++-----------------
 1 file changed, 13 insertions(+), 17 deletions(-)
gdbstub/gdbstub.c +13 -17
index 1f8cd118924..d4db7ba30cc 100644
@@ -1413,36 +1413,32 @@ static void handle_v_cont(GArray *params, void *user_ctx)
 
 static void handle_v_attach(GArray *params, void *user_ctx)
 {
-    GDBProcess *process;
-    CPUState *cpu;
+    GDBProcess *process = NULL;
+    CPUState *cpu = NULL;
 
+    /* Default error reply */
     g_string_assign(gdbserver_state.str_buf, "E22");
-    if (!params->len) {
-        goto cleanup;
-    }
-
-    process = gdb_get_process(gdb_get_cmd_param(params, 0)->val_ul);
-    if (!process) {
-        goto cleanup;
+    if (params->len) {
+        process = gdb_get_process(gdb_get_cmd_param(params, 0)->val_ul);
     }
 
-    cpu = gdb_get_first_cpu_in_process(process);
-    if (!cpu) {
-        goto cleanup;
+    if (process) {
+        cpu = gdb_get_first_cpu_in_process(process);
     }
 
-    process->attached = true;
-    gdbserver_state.g_cpu = cpu;
-    gdbserver_state.c_cpu = cpu;
+    if (cpu) {
+        process->attached = true;
+        gdbserver_state.g_cpu = cpu;
+        gdbserver_state.c_cpu = cpu;
 
     if (gdbserver_state.allow_stop_reply) {
         g_string_printf(gdbserver_state.str_buf, "T%02xthread:", GDB_SIGNAL_TRAP);
         gdb_append_thread_id(cpu, gdbserver_state.str_buf);
         g_string_append_c(gdbserver_state.str_buf, ';');
         gdbserver_state.allow_stop_reply = false;
-cleanup:
-        gdb_put_strbuf();
     }
+
+    gdb_put_strbuf();
 }
 
 static void handle_v_kill(GArray *params, void *user_ctx)
Signature · 1 line
2.47.3
#4 lore raw

Re: [PATCH 03/11] gdbstub: remove the need for goto cleanup

Hi, Alex

Quoted 7 lines · Alex Bennée

We already set a default error reply which we can only overwrite if we successfully follow the chain of checks. Initialise the variables as NULL and use that to gate the construction of the filled out stop/reply packet.

Thanks for the patch: glad to see a goto gone.

Quoted 47 lines
---
 gdbstub/gdbstub.c | 30 +++++++++++++-----------------
 1 file changed, 13 insertions(+), 17 deletions(-)
gdbstub/gdbstub.c +11 -15
index 1f8cd118924..d4db7ba30cc 100644
@@ -1413,36 +1413,32 @@ static void handle_v_cont(GArray *params, void *user_ctx)
 
 static void handle_v_attach(GArray *params, void *user_ctx)
 {
-    GDBProcess *process;
-    CPUState *cpu;
+    GDBProcess *process = NULL;
+    CPUState *cpu = NULL;
 
+    /* Default error reply */
     g_string_assign(gdbserver_state.str_buf, "E22");
-    if (!params->len) {
-        goto cleanup;
-    }
-
-    process = gdb_get_process(gdb_get_cmd_param(params, 0)->val_ul);
-    if (!process) {
-        goto cleanup;
+    if (params->len) {
+        process = gdb_get_process(gdb_get_cmd_param(params, 0)->val_ul);
     }
 
-    cpu = gdb_get_first_cpu_in_process(process);
-    if (!cpu) {
-        goto cleanup;
+    if (process) {
+        cpu = gdb_get_first_cpu_in_process(process);
     }
 
-    process->attached = true;
-    gdbserver_state.g_cpu = cpu;
-    gdbserver_state.c_cpu = cpu;
+    if (cpu) {
+        process->attached = true;
+        gdbserver_state.g_cpu = cpu;
+        gdbserver_state.c_cpu = cpu;
 
     if (gdbserver_state.allow_stop_reply) {
         g_string_printf(gdbserver_state.str_buf, "T%02xthread:", GDB_SIGNAL_TRAP);

gdbstub/gdbstub.c static void handle_v_cont(GArray *params, void *user_ctx) · line 1443

Quoted 10 lines
gdbstub/gdbstub.c +2 -2
         gdb_append_thread_id(cpu, gdbserver_state.str_buf);
         g_string_append_c(gdbserver_state.str_buf, ';');
         gdbserver_state.allow_stop_reply = false;
-cleanup:
-        gdb_put_strbuf();
     }
+
+    gdb_put_strbuf();
 }
 

The cpu gated block doesn't have a closing brace here thereby breaking build, but it does in the next patch. Maybe a git add -p gone awry?

Also, would it make sense to move the cpu and the to-be nested block below it into the process gated block to avoid the cpu check whenever process is NULL?

Yodel

Quoted 5 lines
gdbstub/gdbstub.c +0 -1
 static void handle_v_kill(GArray *params, void *user_ctx)
--

2.47.3

#5 lore raw

Re: [PATCH 03/11] gdbstub: remove the need for goto cleanup

Quoted 4 lines · Yodel Eldar

Hi, Alex

Quoted 1 line · Alex Bennée
Quoted 8 lines
Quoted 6 lines

We already set a default error reply which we can only overwrite if we successfully follow the chain of checks. Initialise the variables as NULL and use that to gate the construction of the filled out stop/reply packet.

Thanks for the patch: glad to see a goto gone.

It is not yet merged, but should be within the next 2 weeks now.

Thanks for your patience with this workflow!

Quoted 3 lines
Quoted 3 lines
---
  gdbstub/gdbstub.c | 30 +++++++++++++-----------------
  1 file changed, 13 insertions(+), 17 deletions(-)
#6 lore raw

PATCH 4/11 gdbstub: extract stop reply message construction

In reply to Alex Bennée

The stop reply message we send can include a lot of extra information and a bunch is mode dependant. Extract the construction into a helper and add specialised versions for system and user mode.

The correct response for system mode should be of the form:

T05core:N;

Where N is the core ID.

---
 gdbstub/internals.h |  7 +++++++
 gdbstub/gdbstub.c   | 15 ++++++---------
 gdbstub/system.c    | 11 +++++++++++
 gdbstub/user.c      | 12 ++++++++++++
 4 files changed, 36 insertions(+), 9 deletions(-)
gdbstub/internals.h +7 -0
index 92466b28c18..3134a6e8eb2 100644
@@ -237,4 +237,11 @@ void gdb_breakpoint_remove_all(CPUState *cs);
 int gdb_target_memory_rw_debug(CPUState *cs, hwaddr addr,
                                uint8_t *buf, int len, bool is_write);
 
+/**
+ * gdb_build_stop_packet() - craft the stop packet
+ * @cs: CPUState
+ */
+
+void gdb_build_stop_packet(CPUState *cs);
+
 #endif /* GDBSTUB_INTERNALS_H */
gdbstub/gdbstub.c +6 -9
index d4db7ba30cc..c82fb5ad324 100644
@@ -1431,11 +1431,10 @@ static void handle_v_attach(GArray *params, void *user_ctx)
         gdbserver_state.g_cpu = cpu;
         gdbserver_state.c_cpu = cpu;
 
-    if (gdbserver_state.allow_stop_reply) {
-        g_string_printf(gdbserver_state.str_buf, "T%02xthread:", GDB_SIGNAL_TRAP);
-        gdb_append_thread_id(cpu, gdbserver_state.str_buf);
-        g_string_append_c(gdbserver_state.str_buf, ';');
-        gdbserver_state.allow_stop_reply = false;
+        if (gdbserver_state.allow_stop_reply) {
+            gdb_build_stop_packet(cpu);
+            gdbserver_state.allow_stop_reply = false;
+        }
     }
 
     gdb_put_strbuf();
@@ -2037,11 +2036,9 @@ static void handle_gen_set(GArray *params, void *user_ctx)
 static void handle_target_halt(GArray *params, void *user_ctx)
 {
     if (gdbserver_state.allow_stop_reply) {
-        g_string_printf(gdbserver_state.str_buf, "T%02xthread:", GDB_SIGNAL_TRAP);
-        gdb_append_thread_id(gdbserver_state.c_cpu, gdbserver_state.str_buf);
-        g_string_append_c(gdbserver_state.str_buf, ';');
-        gdb_put_strbuf();
+        gdb_build_stop_packet(gdbserver_state.c_cpu);
         gdbserver_state.allow_stop_reply = false;
+        gdb_put_strbuf();
     }
     /*
      * Remove all the breakpoints when this query is issued,
gdbstub/system.c +11 -0
index 79f80256e3a..6963c930b01 100644
@@ -662,3 +662,14 @@ void gdb_breakpoint_remove_all(CPUState *cs)
         ops->remove_all_breakpoints(cs);
     }
 }
+
+/*
+ * The minimal system-mode stop reply packet is:
+ *   T05core:{id};
+ */
+
+void gdb_build_stop_packet(CPUState *cs)
+{
+    g_string_printf(gdbserver_state.str_buf,
+                    "T%02xcore:%02x;", GDB_SIGNAL_TRAP, gdb_get_cpu_index(cs));
+}
gdbstub/user.c +12 -0
index a2327c61352..cc71cda3e33 100644
@@ -974,3 +974,15 @@ void gdb_handle_query_xfer_siginfo(GArray *params, void *user_ctx)
     gdb_put_packet_binary(gdbserver_state.str_buf->str,
                           gdbserver_state.str_buf->len, true);
 }
+
+/*
+ * The minimal user-mode stop reply packet is:
+ *   T05thread:{id};
+ */
+
+void gdb_build_stop_packet(CPUState *cs)
+{
+    g_string_printf(gdbserver_state.str_buf, "T%02xthread:", GDB_SIGNAL_TRAP);
+    gdb_append_thread_id(cs, gdbserver_state.str_buf);
+    g_string_append_c(gdbserver_state.str_buf, ';');
+}
Signature · 1 line
2.47.3
Reviewed-by on v1 4/11
#7 lore raw

Re: [PATCH 04/11] gdbstub: extract stop reply message construction

Quoted 10 lines · Alex Bennée

The stop reply message we send can include a lot of extra information and a bunch is mode dependant. Extract the construction into a helper and add specialised versions for system and user mode.

The correct response for system mode should be of the form:

   T05core:N;

Where N is the core ID.

Quoted 7 lines
---
  gdbstub/internals.h |  7 +++++++
  gdbstub/gdbstub.c   | 15 ++++++---------
  gdbstub/system.c    | 11 +++++++++++
  gdbstub/user.c      | 12 ++++++++++++
  4 files changed, 36 insertions(+), 9 deletions(-)

r~

#8 lore raw

PATCH 5/11 gdbstub/user: localise variables for reading gdb socket

In reply to Alex Bennée

Keep things tidy by moving the declarations to the block where they are used.

---
 gdbstub/user.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
gdbstub/user.c +3 -7
index cc71cda3e33..c7a3ef947ed 100644
@@ -198,9 +198,6 @@ void gdb_qemu_exit(int code)
 int gdb_handlesig(CPUState *cpu, int sig, const char *reason, void *siginfo,
                   int siginfo_len)
 {
-    char buf[256];
-    int n;
-
     if (!gdbserver_state.init || gdbserver_user_state.fd < 0) {
         return sig;
     }
@@ -246,11 +243,10 @@ int gdb_handlesig(CPUState *cpu, int sig, const char *reason, void *siginfo,
     gdbserver_state.state = RS_IDLE;
     gdbserver_user_state.running_state = 0;
     while (gdbserver_user_state.running_state == 0) {
-        n = read(gdbserver_user_state.fd, buf, 256);
+        char buf[256];
+        int n = read(gdbserver_user_state.fd, buf, 256);
         if (n > 0) {
-            int i;
-
-            for (i = 0; i < n; i++) {
+            for (int i = 0; i < n; i++) {
                 gdb_read_byte(buf[i]);
             }
         } else {
Signature · 1 line
2.47.3
Reviewed-by on v1 5/11
#9 lore raw

Re: [PATCH 05/11] gdbstub/user: localise variables for reading gdb socket

Quoted 7 lines · Alex Bennée

Keep things tidy by moving the declarations to the block where they are used.

---
  gdbstub/user.c | 10 +++-------
  1 file changed, 3 insertions(+), 7 deletions(-)

r~

Quoted 30 lines
gdbstub/user.c +3 -7
index cc71cda3e33..c7a3ef947ed 100644
@@ -198,9 +198,6 @@ void gdb_qemu_exit(int code)
  int gdb_handlesig(CPUState *cpu, int sig, const char *reason, void *siginfo,
                    int siginfo_len)
  {
-    char buf[256];
-    int n;
-
      if (!gdbserver_state.init || gdbserver_user_state.fd < 0) {
          return sig;
      }
@@ -246,11 +243,10 @@ int gdb_handlesig(CPUState *cpu, int sig, const char *reason, void *siginfo,
      gdbserver_state.state = RS_IDLE;
      gdbserver_user_state.running_state = 0;
      while (gdbserver_user_state.running_state == 0) {
-        n = read(gdbserver_user_state.fd, buf, 256);
+        char buf[256];
+        int n = read(gdbserver_user_state.fd, buf, 256);
          if (n > 0) {
-            int i;
-
-            for (i = 0; i < n; i++) {
+            for (int i = 0; i < n; i++) {
                  gdb_read_byte(buf[i]);
              }
          } else {
#10 lore raw

PATCH 6/11 gdbstub: pass GString to gdb_build_stop_packet

In reply to Alex Bennée

The other functions we are going to clean-up work variously with there own dynamically allocated GStrings or with the common shared buffer.

---
 gdbstub/internals.h | 5 ++++-
 gdbstub/gdbstub.c   | 4 ++--
 gdbstub/system.c    | 4 ++--
 gdbstub/user.c      | 8 ++++----
 4 files changed, 12 insertions(+), 9 deletions(-)
gdbstub/internals.h +4 -1
index 3134a6e8eb2..9b25bf58b8e 100644
@@ -239,9 +239,12 @@ int gdb_target_memory_rw_debug(CPUState *cs, hwaddr addr,
 
 /**
  * gdb_build_stop_packet() - craft the stop packet
+ * @buf: GString buffer for building the packet
  * @cs: CPUState
+ *
+ * Craft the Stop/Reply packet when we halt.
  */
 
-void gdb_build_stop_packet(CPUState *cs);
+void gdb_build_stop_packet(GString *buf, CPUState *cs);
 
 #endif /* GDBSTUB_INTERNALS_H */
gdbstub/gdbstub.c +2 -2
index c82fb5ad324..b45eb7c7b2b 100644
@@ -1432,7 +1432,7 @@ static void handle_v_attach(GArray *params, void *user_ctx)
         gdbserver_state.c_cpu = cpu;
 
         if (gdbserver_state.allow_stop_reply) {
-            gdb_build_stop_packet(cpu);
+            gdb_build_stop_packet(gdbserver_state.str_buf, cpu);
             gdbserver_state.allow_stop_reply = false;
         }
     }
@@ -2036,7 +2036,7 @@ static void handle_gen_set(GArray *params, void *user_ctx)
 static void handle_target_halt(GArray *params, void *user_ctx)
 {
     if (gdbserver_state.allow_stop_reply) {
-        gdb_build_stop_packet(gdbserver_state.c_cpu);
+        gdb_build_stop_packet(gdbserver_state.str_buf, gdbserver_state.c_cpu);
         gdbserver_state.allow_stop_reply = false;
         gdb_put_strbuf();
     }
gdbstub/system.c +2 -2
index 6963c930b01..8ec8b7ea336 100644
@@ -668,8 +668,8 @@ void gdb_breakpoint_remove_all(CPUState *cs)
  *   T05core:{id};
  */
 
-void gdb_build_stop_packet(CPUState *cs)
+void gdb_build_stop_packet(GString *buf, CPUState *cs)
 {
-    g_string_printf(gdbserver_state.str_buf,
+    g_string_printf(buf,
                     "T%02xcore:%02x;", GDB_SIGNAL_TRAP, gdb_get_cpu_index(cs));
 }
gdbstub/user.c +4 -4
index c7a3ef947ed..a16f37616b1 100644
@@ -976,9 +976,9 @@ void gdb_handle_query_xfer_siginfo(GArray *params, void *user_ctx)
  *   T05thread:{id};
  */
 
-void gdb_build_stop_packet(CPUState *cs)
+void gdb_build_stop_packet(GString *buf, CPUState *cs)
 {
-    g_string_printf(gdbserver_state.str_buf, "T%02xthread:", GDB_SIGNAL_TRAP);
-    gdb_append_thread_id(cs, gdbserver_state.str_buf);
-    g_string_append_c(gdbserver_state.str_buf, ';');
+    g_string_printf(buf, "T%02xthread:", GDB_SIGNAL_TRAP);
+    gdb_append_thread_id(cs, buf);
+    g_string_append_c(buf, ';');
 }
Signature · 1 line
2.47.3
#11 lore raw

Re: [PATCH 06/11] gdbstub: pass GString to gdb_build_stop_packet

Quoted 1 line · Alex Bennée

The other functions we are going to clean-up work variously with there

their...

Quoted 83 lines

own dynamically allocated GStrings or with the common shared buffer.

---
 gdbstub/internals.h | 5 ++++-
 gdbstub/gdbstub.c   | 4 ++--
 gdbstub/system.c    | 4 ++--
 gdbstub/user.c      | 8 ++++----
 4 files changed, 12 insertions(+), 9 deletions(-)
gdbstub/internals.h +4 -1
index 3134a6e8eb2..9b25bf58b8e 100644
@@ -239,9 +239,12 @@ int gdb_target_memory_rw_debug(CPUState *cs, hwaddr addr,
 
 /**
  * gdb_build_stop_packet() - craft the stop packet
+ * @buf: GString buffer for building the packet
  * @cs: CPUState
+ *
+ * Craft the Stop/Reply packet when we halt.
  */
 
-void gdb_build_stop_packet(CPUState *cs);
+void gdb_build_stop_packet(GString *buf, CPUState *cs);
 
 #endif /* GDBSTUB_INTERNALS_H */
gdbstub/gdbstub.c +2 -2
index c82fb5ad324..b45eb7c7b2b 100644
@@ -1432,7 +1432,7 @@ static void handle_v_attach(GArray *params, void *user_ctx)
         gdbserver_state.c_cpu = cpu;
 
         if (gdbserver_state.allow_stop_reply) {
-            gdb_build_stop_packet(cpu);
+            gdb_build_stop_packet(gdbserver_state.str_buf, cpu);
             gdbserver_state.allow_stop_reply = false;
         }
     }
@@ -2036,7 +2036,7 @@ static void handle_gen_set(GArray *params, void *user_ctx)
 static void handle_target_halt(GArray *params, void *user_ctx)
 {
     if (gdbserver_state.allow_stop_reply) {
-        gdb_build_stop_packet(gdbserver_state.c_cpu);
+        gdb_build_stop_packet(gdbserver_state.str_buf, gdbserver_state.c_cpu);
         gdbserver_state.allow_stop_reply = false;
         gdb_put_strbuf();
     }
gdbstub/system.c +2 -2
index 6963c930b01..8ec8b7ea336 100644
@@ -668,8 +668,8 @@ void gdb_breakpoint_remove_all(CPUState *cs)
  *   T05core:{id};
  */
 
-void gdb_build_stop_packet(CPUState *cs)
+void gdb_build_stop_packet(GString *buf, CPUState *cs)
 {
-    g_string_printf(gdbserver_state.str_buf,
+    g_string_printf(buf,
                     "T%02xcore:%02x;", GDB_SIGNAL_TRAP, gdb_get_cpu_index(cs));
 }
gdbstub/user.c +4 -4
index c7a3ef947ed..a16f37616b1 100644
@@ -976,9 +976,9 @@ void gdb_handle_query_xfer_siginfo(GArray *params, void *user_ctx)
  *   T05thread:{id};
  */
 
-void gdb_build_stop_packet(CPUState *cs)
+void gdb_build_stop_packet(GString *buf, CPUState *cs)
 {
-    g_string_printf(gdbserver_state.str_buf, "T%02xthread:", GDB_SIGNAL_TRAP);
-    gdb_append_thread_id(cs, gdbserver_state.str_buf);
-    g_string_append_c(gdbserver_state.str_buf, ';');
+    g_string_printf(buf, "T%02xthread:", GDB_SIGNAL_TRAP);
+    gdb_append_thread_id(cs, buf);
+    g_string_append_c(buf, ';');
 }
Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
Reviewed-by on v1 6/11
#12 lore raw

Re: [PATCH 06/11] gdbstub: pass GString to gdb_build_stop_packet

In reply to Alex Bennée
Quoted 10 lines · Alex Bennée

The other functions we are going to clean-up work variously with there own dynamically allocated GStrings or with the common shared buffer.

---
  gdbstub/internals.h | 5 ++++-
  gdbstub/gdbstub.c   | 4 ++--
  gdbstub/system.c    | 4 ++--
  gdbstub/user.c      | 8 ++++----
  4 files changed, 12 insertions(+), 9 deletions(-)

This function is brand new in patch 4.
Why not just merge these two patches?

Either way,

r~

Quoted 74 lines
gdbstub/internals.h +4 -1
index 3134a6e8eb2..9b25bf58b8e 100644
@@ -239,9 +239,12 @@ int gdb_target_memory_rw_debug(CPUState *cs, hwaddr addr,
 
  /**
   * gdb_build_stop_packet() - craft the stop packet
+ * @buf: GString buffer for building the packet
   * @cs: CPUState
+ *
+ * Craft the Stop/Reply packet when we halt.
   */
 
-void gdb_build_stop_packet(CPUState *cs);
+void gdb_build_stop_packet(GString *buf, CPUState *cs);
 
  #endif /* GDBSTUB_INTERNALS_H */
gdbstub/gdbstub.c +2 -2
index c82fb5ad324..b45eb7c7b2b 100644
@@ -1432,7 +1432,7 @@ static void handle_v_attach(GArray *params, void *user_ctx)
          gdbserver_state.c_cpu = cpu;
 
          if (gdbserver_state.allow_stop_reply) {
-            gdb_build_stop_packet(cpu);
+            gdb_build_stop_packet(gdbserver_state.str_buf, cpu);
              gdbserver_state.allow_stop_reply = false;
          }
      }
@@ -2036,7 +2036,7 @@ static void handle_gen_set(GArray *params, void *user_ctx)
  static void handle_target_halt(GArray *params, void *user_ctx)
  {
      if (gdbserver_state.allow_stop_reply) {
-        gdb_build_stop_packet(gdbserver_state.c_cpu);
+        gdb_build_stop_packet(gdbserver_state.str_buf, gdbserver_state.c_cpu);
          gdbserver_state.allow_stop_reply = false;
          gdb_put_strbuf();
      }
gdbstub/system.c +2 -2
index 6963c930b01..8ec8b7ea336 100644
@@ -668,8 +668,8 @@ void gdb_breakpoint_remove_all(CPUState *cs)
   *   T05core:{id};
   */
 
-void gdb_build_stop_packet(CPUState *cs)
+void gdb_build_stop_packet(GString *buf, CPUState *cs)
  {
-    g_string_printf(gdbserver_state.str_buf,
+    g_string_printf(buf,
                      "T%02xcore:%02x;", GDB_SIGNAL_TRAP, gdb_get_cpu_index(cs));
  }
gdbstub/user.c +4 -4
index c7a3ef947ed..a16f37616b1 100644
@@ -976,9 +976,9 @@ void gdb_handle_query_xfer_siginfo(GArray *params, void *user_ctx)
   *   T05thread:{id};
   */
 
-void gdb_build_stop_packet(CPUState *cs)
+void gdb_build_stop_packet(GString *buf, CPUState *cs)
  {
-    g_string_printf(gdbserver_state.str_buf, "T%02xthread:", GDB_SIGNAL_TRAP);
-    gdb_append_thread_id(cs, gdbserver_state.str_buf);
-    g_string_append_c(gdbserver_state.str_buf, ';');
+    g_string_printf(buf, "T%02xthread:", GDB_SIGNAL_TRAP);
+    gdb_append_thread_id(cs, buf);
+    g_string_append_c(buf, ';');
  }
#13 lore raw

PATCH 7/11 include/gdbstub/syscalls: Add EIO and ENOSYS GDB File-I/O errno values

In reply to Alex Bennée

From: Yodel Eldar <[email protected]>

This patch adds the EIO and ENOSYS errno values that were missing from the GDB Manual [1] when the other errno values were defined.

[1] https://sourceware.org/gdb/current/onlinedocs/gdb.html/Errno-Values.html

Message-ID: <[email protected]>

---
 include/gdbstub/syscalls.h | 2 ++
 1 file changed, 2 insertions(+)
include/gdbstub/syscalls.h +2 -0
index d63228e96b4..6200416f77a 100644
@@ -22,6 +22,7 @@
 #define GDB_EPERM           1
 #define GDB_ENOENT          2
 #define GDB_EINTR           4
+#define GDB_EIO             5
 #define GDB_EBADF           9
 #define GDB_EACCES         13
 #define GDB_EFAULT         14
@@ -37,6 +38,7 @@
 #define GDB_ENOSPC         28
 #define GDB_ESPIPE         29
 #define GDB_EROFS          30
+#define GDB_ENOSYS         88
 #define GDB_ENAMETOOLONG   91
 #define GDB_EUNKNOWN       9999
 
Signature · 1 line
2.47.3
#14 lore raw

PATCH 8/11 gdbstub: Export host_to_gdb_errno File-I/O helper function

In reply to Alex Bennée

From: Yodel Eldar <[email protected]>

Move host_to_gdb_errno from target/m68k/m68k-semi.c to gdbstub/syscalls.c. Declare it in include/gdbstub/syscalls.h.

Add both newly added GDB File-I/O supported errno values, EIO and ENOSYS, to the mapping.

Message-ID: <[email protected]>

---
 include/gdbstub/syscalls.h |  9 +++++++++
 gdbstub/syscalls.c         | 36 ++++++++++++++++++++++++++++++++++++
 target/m68k/m68k-semi.c    | 29 -----------------------------
 3 files changed, 45 insertions(+), 29 deletions(-)
include/gdbstub/syscalls.h +9 -0
index 6200416f77a..a09559128b1 100644
@@ -102,6 +102,15 @@ void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...);
  */
 int use_gdb_syscalls(void);
 
+/**
+ * host_to_gdb_errno: convert host errno to GDB errno value
+ * @err: errno from host
+ *
+ * Given an error number from the host, this helper function returns
+ * its GDB File-I/O specified representation.
+ */
+int host_to_gdb_errno(int err);
+
 /**
  * gdb_exit: exit gdb session, reporting inferior status
  * @code: exit code reported
gdbstub/syscalls.c +36 -0
index d8bb90cc1c7..15050984a04 100644
@@ -145,6 +145,42 @@ void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
     gdb_syscall_handling(gdbserver_syscall_state.syscall_buf);
 }
 
+/*
+ * Map host error numbers to their GDB protocol counterparts.
+ * For the list of GDB File-I/O supported error numbers, please consult:
+ * https://sourceware.org/gdb/current/onlinedocs/gdb.html/Errno-Values.html
+ */
+int host_to_gdb_errno(int err)
+{
+#define E(X)  case E##X: return GDB_E##X
+    switch (err) {
+    E(PERM);
+    E(NOENT);
+    E(INTR);
+    E(IO);
+    E(BADF);
+    E(ACCES);
+    E(FAULT);
+    E(BUSY);
+    E(EXIST);
+    E(NODEV);
+    E(NOTDIR);
+    E(ISDIR);
+    E(INVAL);
+    E(NFILE);
+    E(MFILE);
+    E(FBIG);
+    E(NOSPC);
+    E(SPIPE);
+    E(ROFS);
+    E(NOSYS);
+    E(NAMETOOLONG);
+    default:
+        return GDB_EUNKNOWN;
+    }
+#undef E
+}
+
 /*
  * GDB Command Handlers
  */
target/m68k/m68k-semi.c +0 -29
index 578a08dfee8..bdc798a28cb 100644
@@ -46,35 +46,6 @@
 #define HOSTED_ISATTY 12
 #define HOSTED_SYSTEM 13
 
-static int host_to_gdb_errno(int err)
-{
-#define E(X)  case E##X: return GDB_E##X
-    switch (err) {
-    E(PERM);
-    E(NOENT);
-    E(INTR);
-    E(BADF);
-    E(ACCES);
-    E(FAULT);
-    E(BUSY);
-    E(EXIST);
-    E(NODEV);
-    E(NOTDIR);
-    E(ISDIR);
-    E(INVAL);
-    E(NFILE);
-    E(MFILE);
-    E(FBIG);
-    E(NOSPC);
-    E(SPIPE);
-    E(ROFS);
-    E(NAMETOOLONG);
-    default:
-        return GDB_EUNKNOWN;
-    }
-#undef E
-}
-
 static void m68k_semi_u32_cb(CPUState *cs, uint64_t ret, int err)
 {
     CPUM68KState *env = cpu_env(cs);
Signature · 1 line
2.47.3
#15 lore raw

PATCH 9/11 gdbstub/user-target: Convert host errno to GDB File-I/O errno

In reply to Alex Bennée

From: Yodel Eldar <[email protected]>

Use host_to_gdb_errno to convert host-supplied errnos to their GDB File-I/O remote protocol values, and use them in F reply packets.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2751

Message-ID: <[email protected]>

---
 gdbstub/user-target.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
gdbstub/user-target.c +9 -4
index 43231e695e8..7ef0282f70e 100644
@@ -10,6 +10,7 @@
 #include "qemu/osdep.h"
 #include "exec/gdbstub.h"
 #include "gdbstub/commands.h"
+#include "gdbstub/syscalls.h"
 #include "qemu.h"
 #include "internals.h"
 #ifdef CONFIG_LINUX
@@ -315,7 +316,8 @@ void gdb_handle_v_file_open(GArray *params, void *user_ctx)
     int fd = open(filename, flags, mode);
 #endif
     if (fd < 0) {
-        g_string_printf(gdbserver_state.str_buf, "F-1,%x", errno);
+        int gdb_errno = host_to_gdb_errno(errno);
+        g_string_printf(gdbserver_state.str_buf, "F-1,%x", gdb_errno);
     } else {
         g_string_printf(gdbserver_state.str_buf, "F%x", fd);
     }
@@ -327,7 +329,8 @@ void gdb_handle_v_file_close(GArray *params, void *user_ctx)
     int fd = gdb_get_cmd_param(params, 0)->val_ul;
 
     if (close(fd) == -1) {
-        g_string_printf(gdbserver_state.str_buf, "F-1,%x", errno);
+        int gdb_errno = host_to_gdb_errno(errno);
+        g_string_printf(gdbserver_state.str_buf, "F-1,%x", gdb_errno);
         gdb_put_strbuf();
         return;
     }
@@ -350,7 +353,8 @@ void gdb_handle_v_file_pread(GArray *params, void *user_ctx)
 
     ssize_t n = pread(fd, buf, bufsiz, offset);
     if (n < 0) {
-        g_string_printf(gdbserver_state.str_buf, "F-1,%x", errno);
+        int gdb_errno = host_to_gdb_errno(errno);
+        g_string_printf(gdbserver_state.str_buf, "F-1,%x", gdb_errno);
         gdb_put_strbuf();
         return;
     }
@@ -373,7 +377,8 @@ void gdb_handle_v_file_readlink(GArray *params, void *user_ctx)
     ssize_t n = readlink(filename, buf, BUFSIZ);
 #endif
     if (n < 0) {
-        g_string_printf(gdbserver_state.str_buf, "F-1,%x", errno);
+        int gdb_errno = host_to_gdb_errno(errno);
+        g_string_printf(gdbserver_state.str_buf, "F-1,%x", gdb_errno);
         gdb_put_strbuf();
         return;
     }
Signature · 1 line
2.47.3
#16 lore raw

PATCH 10/11 include/gdbstub: fix typo

In reply to Alex Bennée
---
 include/gdbstub/commands.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/gdbstub/commands.h +1 -1
index bff3674872e..989a95c33f9 100644
@@ -50,7 +50,7 @@ typedef union GdbCmdVariant {
  * '.' -> Skip 1 char unless reached "\0"
  * Any other value is treated as the delimiter value itself
  *
- * @allow_stop_reply: True iff the gdbstub can respond to this command with a
+ * @allow_stop_reply: True if the gdbstub can respond to this command with a
  * "stop reply" packet. The list of commands that accept such response is
  * defined at the GDB Remote Serial Protocol documentation. See:
  * https://sourceware.org/gdb/onlinedocs/gdb/Stop-Reply-Packets.html#Stop-Reply-Packets.
Signature · 1 line
2.47.3
#17 lore raw

Re: [PATCH 10/11] include/gdbstub: fix typo

Quoted 11 lines · Alex Bennée
---
 include/gdbstub/commands.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/gdbstub/commands.h +0 -0
index bff3674872e..989a95c33f9 100644

include/gdbstub/commands.h typedef union GdbCmdVariant { · line 56

Quoted 8 lines
include/gdbstub/commands.h +1 -1
@@ -50,7 +50,7 @@ typedef union GdbCmdVariant {
  * '.' -> Skip 1 char unless reached "\0"
  * Any other value is treated as the delimiter value itself
  *
- * @allow_stop_reply: True iff the gdbstub can respond to this command with a
+ * @allow_stop_reply: True if the gdbstub can respond to this command with a
  * "stop reply" packet. The list of commands that accept such response is
  * defined at the GDB Remote Serial Protocol documentation. See:
  * https://sourceware.org/gdb/onlinedocs/gdb/Stop-Reply-Packets.html#Stop-Reply-Packets.

"iff" is not generally a typo: it is an abbreviation for "if and only if". We tend to prefer not to use that abbreviation, though, because unless you happen to have a background in mathematics you probably won't recognise it.

In this case I don't think spelling out "if and only if" brings us any more clarity than using a plain "if", so I'm OK with the change itself and we can just tweak the commit message.

thanks
-- PMM

#18 lore raw

PATCH 11/11 gdbstub/user: rename and use bool for running_state

In reply to Alex Bennée

This is just a clean-up to document what we using the running variable for in user-mode.

---
 gdbstub/user.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)
gdbstub/user.c +11 -7
index a16f37616b1..97eb13e796a 100644
@@ -87,7 +87,11 @@ enum GDBForkMessage {
 typedef struct {
     int fd;
     char *socket_path;
-    int running_state;
+    /*
+     * running state of the guest, when we process a packet that restarts
+     * the guest we set this to true.
+     */
+    bool running;
     /*
      * Store syscalls mask without memory allocation in order to avoid
      * implementing synchronization.
@@ -241,8 +245,8 @@ int gdb_handlesig(CPUState *cpu, int sig, const char *reason, void *siginfo,
 
     sig = 0;
     gdbserver_state.state = RS_IDLE;
-    gdbserver_user_state.running_state = 0;
-    while (gdbserver_user_state.running_state == 0) {
+    gdbserver_user_state.running = false;
+    while (!gdbserver_user_state.running) {
         char buf[256];
         int n = read(gdbserver_user_state.fd, buf, 256);
         if (n > 0) {
@@ -611,11 +615,11 @@ void gdbserver_fork_end(CPUState *cpu, pid_t pid)
 
     gdbserver_state.state = RS_IDLE;
     gdbserver_state.allow_stop_reply = false;
-    gdbserver_user_state.running_state = 0;
+    gdbserver_user_state.running = false;
     for (;;) {
         switch (gdbserver_user_state.fork_state) {
         case GDB_FORK_ENABLED:
-            if (gdbserver_user_state.running_state) {
+            if (gdbserver_user_state.running) {
                 close(fd);
                 return;
             }
@@ -728,7 +732,7 @@ void gdb_handle_query_attached(GArray *params, void *user_ctx)
 
 void gdb_continue(void)
 {
-    gdbserver_user_state.running_state = 1;
+    gdbserver_user_state.running = true;
     trace_gdbstub_op_continue();
 }
 
@@ -750,7 +754,7 @@ int gdb_continue_partial(char *newstates)
             cpu_single_step(cpu, gdbserver_state.sstep_flags);
         }
     }
-    gdbserver_user_state.running_state = 1;
+    gdbserver_user_state.running = true;
     return res;
 }
 
Signature · 1 line
2.47.3
Reviewed-by on v1 11/11
#19 lore raw

Re: [PATCH 11/11] gdbstub/user: rename and use bool for running_state

Quoted 7 lines · Alex Bennée

This is just a clean-up to document what we using the running variable for in user-mode.

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

r~

Quoted 61 lines
gdbstub/user.c +11 -7
index a16f37616b1..97eb13e796a 100644
@@ -87,7 +87,11 @@ enum GDBForkMessage {
  typedef struct {
      int fd;
      char *socket_path;
-    int running_state;
+    /*
+     * running state of the guest, when we process a packet that restarts
+     * the guest we set this to true.
+     */
+    bool running;
      /*
       * Store syscalls mask without memory allocation in order to avoid
       * implementing synchronization.
@@ -241,8 +245,8 @@ int gdb_handlesig(CPUState *cpu, int sig, const char *reason, void *siginfo,
 
      sig = 0;
      gdbserver_state.state = RS_IDLE;
-    gdbserver_user_state.running_state = 0;
-    while (gdbserver_user_state.running_state == 0) {
+    gdbserver_user_state.running = false;
+    while (!gdbserver_user_state.running) {
          char buf[256];
          int n = read(gdbserver_user_state.fd, buf, 256);
          if (n > 0) {
@@ -611,11 +615,11 @@ void gdbserver_fork_end(CPUState *cpu, pid_t pid)
 
      gdbserver_state.state = RS_IDLE;
      gdbserver_state.allow_stop_reply = false;
-    gdbserver_user_state.running_state = 0;
+    gdbserver_user_state.running = false;
      for (;;) {
          switch (gdbserver_user_state.fork_state) {
          case GDB_FORK_ENABLED:
-            if (gdbserver_user_state.running_state) {
+            if (gdbserver_user_state.running) {
                  close(fd);
                  return;
              }
@@ -728,7 +732,7 @@ void gdb_handle_query_attached(GArray *params, void *user_ctx)
 
  void gdb_continue(void)
  {
-    gdbserver_user_state.running_state = 1;
+    gdbserver_user_state.running = true;
      trace_gdbstub_op_continue();
  }
 
@@ -750,7 +754,7 @@ int gdb_continue_partial(char *newstates)
              cpu_single_step(cpu, gdbserver_state.sstep_flags);
          }
      }
-    gdbserver_user_state.running_state = 1;
+    gdbserver_user_state.running = true;
      return res;
  }