Skip to contentSkip to conversation

gdbstub (auxv, tests, cleanup)

PATCH Superseded Alex Bennée 3 participants 14 messages Dec 14, 2020 – Dec 16, 2020

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover gdbstub (auxv, tests, cleanup)

Hi,

I realised I had a pending gdbstub fix in my trees and then immediately ran into a bunch of minor niggles after looking at a gdb failure Peter ran into. The upshot is I've just hard gated the gdb tests to a more-recent than most distros version of gdb which hopefully involves less faff. I also did a bit of clean-up to the handling of cleaning up while I was at it.

Please review.

Alex Bennée (5):
test/guest-debug: echo QEMU command as well
configure: gate our use of GDB to 8.3.1 or above
gdbstub: drop CPUEnv from gdb_exit()
gdbstub: drop gdbserver_cleanup in favour of gdb_exit
gdbstub: ensure we clean-up when terminated

Lirong Yuan (1):
gdbstub: add support to Xfer:auxv:read: packet

 configure                                     |  7 +-
 include/exec/gdbstub.h                        | 14 +++-
 bsd-user/syscall.c                            |  6 +-
 gdbstub.c                                     | 65 ++++++++++++++++---
 linux-user/exit.c                             |  2 +-
 softmmu/vl.c                                  |  2 +-
 target/arm/arm-semi.c                         |  2 +-
 target/m68k/m68k-semi.c                       |  2 +-
 target/nios2/nios2-semi.c                     |  2 +-
 MAINTAINERS                                   |  1 +
 tests/guest-debug/run-test.py                 |  1 +
 tests/tcg/multiarch/Makefile.target           |  9 ++-
 .../multiarch/gdbstub/test-qxfer-auxv-read.py | 57 ++++++++++++++++
 13 files changed, 148 insertions(+), 22 deletions(-)
 create mode 100644 tests/tcg/multiarch/gdbstub/test-qxfer-auxv-read.py
Signature · 1 line
2.20.1
#1 lore raw

PATCH 1/6 test/guest-debug: echo QEMU command as well

This helps with debugging.

---
 tests/guest-debug/run-test.py | 1 +
 1 file changed, 1 insertion(+)
tests/guest-debug/run-test.py +1 -0
index 71c5569054..0c4f5c3808 100755
@@ -53,6 +53,7 @@ if __name__ == '__main__':
         cmd = "%s %s -g %s %s" % (args.qemu, args.qargs, socket_name,
                                   args.binary)
 
+    print("QEMU CMD: %s" % (cmd))
     inferior = subprocess.Popen(shlex.split(cmd))
 
     # Now launch gdb with our test and collect the result
Signature · 1 line
2.20.1
#2 lore raw

PATCH 2/6 configure: gate our use of GDB to 8.3.1 or above

In reply to Alex Bennée

Certain earlier versions of GDB have (possibly distro) derived issues when running against multiarch guests. Also given the problem of clashing ports it is preferable to use socket comms rather than TCP ports for testing.

---
 configure | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
configure +5 -2
index 11f5878a59..f6347382e9 100755
@@ -6721,8 +6721,11 @@ if test "$plugins" = "yes" ; then
     fi
 fi
 
-if test -n "$gdb_bin" ; then
-    echo "HAVE_GDB_BIN=$gdb_bin" >> $config_host_mak
+if test -n "$gdb_bin"; then
+    gdb_version=$($gdb_bin --version | head -n 1)
+    if version_ge ${gdb_version##* } 8.3.1; then
+        echo "HAVE_GDB_BIN=$gdb_bin" >> $config_host_mak
+    fi
 fi
 
 if test "$secret_keyring" = "yes" ; then
Signature · 1 line
2.20.1
#3 lore raw

Re: [PATCH v1 2/6] configure: gate our use of GDB to 8.3.1 or above

Quoted 5 lines · Alex Bennée

Certain earlier versions of GDB have (possibly distro) derived issues when running against multiarch guests. Also given the problem of clashing ports it is preferable to use socket comms rather than TCP ports for testing.

What's a "multiarch guest" ?

Incidentally I think the problem I have been running into with the Ubuntu gdb 8.1 is that it doesn't support registers larger than 64 bytes, and if AArch64 QEMU is emulating SVE then it tries to expose registers bigger than that to the gdbstub.

thanks
-- PMM

#4 lore raw

Re: [PATCH v1 2/6] configure: gate our use of GDB to 8.3.1 or above

Quoted 8 lines · Peter Maydell
Quoted 5 lines · Alex Bennée

Certain earlier versions of GDB have (possibly distro) derived issues when running against multiarch guests. Also given the problem of clashing ports it is preferable to use socket comms rather than TCP ports for testing.

What's a "multiarch guest" ?

I guess non-native guest would be another way of saying it. There is some hoop jumping to deal with the fact that some arches package up a fully featured multiarch aware gdb and some package up the multiarch one as a separate gdb-multiarch package.

Quoted 5 lines

Incidentally I think the problem I have been running into with the Ubuntu gdb 8.1 is that it doesn't support registers larger than 64 bytes, and if AArch64 QEMU is emulating SVE then it tries to expose registers bigger than that to the gdbstub.

So this is a missing patch from Ubuntu's gdb?

We could just peg the sha1 test to cortex-a57 - your are likely not getting the SVE tests running unless you have the compilers to build them (or using docker).

Quoted 3 lines

thanks
-- PMM

Signature · 1 line
Alex Bennée
#5 lore raw

Re: [PATCH v1 2/6] configure: gate our use of GDB to 8.3.1 or above

Quoted 18 lines · Alex Bennée
Quoted 8 lines · Peter Maydell
Quoted 5 lines · Alex Bennée

Certain earlier versions of GDB have (possibly distro) derived issues when running against multiarch guests. Also given the problem of clashing ports it is preferable to use socket comms rather than TCP ports for testing.

What's a "multiarch guest" ?

I guess non-native guest would be another way of saying it. There is some hoop jumping to deal with the fact that some arches package up a fully featured multiarch aware gdb and some package up the multiarch one as a separate gdb-multiarch package.

Quoted 7 lines
Quoted 5 lines

Incidentally I think the problem I have been running into with the Ubuntu gdb 8.1 is that it doesn't support registers larger than 64 bytes, and if AArch64 QEMU is emulating SVE then it tries to expose registers bigger than that to the gdbstub.

So this is a missing patch from Ubuntu's gdb?

No, upstream 8.1 doesn't handle this either and crashes too. I'm a bit surprised we haven't had user complaints, since gdb+QEMU would have previously worked and then started to crash gdb when we put in the SVE registers.

Quoted 3 lines

We could just peg the sha1 test to cortex-a57 - your are likely not getting the SVE tests running unless you have the compilers to build them (or using docker).

Yeah, forcing a non-SVE CPU should allow the test to work with this gdb.

thanks
-- PMM

#6 lore raw

PATCH 3/6 gdbstub: add support to Xfer:auxv:read: packet

In reply to Alex Bennée

From: Lirong Yuan <[email protected]>

This allows gdb to access the target’s auxiliary vector, which can be helpful for telling system libraries important details about the hardware, operating system, and process.

[AJB: minor tweaks to test case, update MAINTAINERS]

Message-Id: <[email protected]>

---
 gdbstub.c                                     | 54 ++++++++++++++++++
 MAINTAINERS                                   |  1 +
 tests/tcg/multiarch/Makefile.target           |  9 ++-
 .../multiarch/gdbstub/test-qxfer-auxv-read.py | 57 +++++++++++++++++++
 4 files changed, 120 insertions(+), 1 deletion(-)
 create mode 100644 tests/tcg/multiarch/gdbstub/test-qxfer-auxv-read.py
gdbstub.c +54 -0
index f19f98ab1a..ec8daa002b 100644
@@ -2172,6 +2172,12 @@ static void handle_query_supported(GdbCmdContext *gdb_ctx, void *user_ctx)
             ";ReverseStep+;ReverseContinue+");
     }
 
+#ifdef CONFIG_USER_ONLY
+    if (gdbserver_state.c_cpu->opaque) {
+        g_string_append(gdbserver_state.str_buf, ";qXfer:auxv:read+");
+    }
+#endif
+
     if (gdb_ctx->num_params &&
         strstr(gdb_ctx->params[0].data, "multiprocess+")) {
         gdbserver_state.multiprocess = true;
@@ -2233,6 +2239,46 @@ static void handle_query_xfer_features(GdbCmdContext *gdb_ctx, void *user_ctx)
                       gdbserver_state.str_buf->len, true);
 }
 
+#ifdef CONFIG_USER_ONLY
+static void handle_query_xfer_auxv(GdbCmdContext *gdb_ctx, void *user_ctx)
+{
+    TaskState *ts;
+    unsigned long offset, len, saved_auxv, auxv_len;
+    const char *mem;
+
+    if (gdb_ctx->num_params < 2) {
+        put_packet("E22");
+        return;
+    }
+
+    offset = gdb_ctx->params[0].val_ul;
+    len = gdb_ctx->params[1].val_ul;
+    ts = gdbserver_state.c_cpu->opaque;
+    saved_auxv = ts->info->saved_auxv;
+    auxv_len = ts->info->auxv_len;
+    mem = (const char *)(saved_auxv + offset);
+    if (offset > auxv_len) {
+        put_packet("E00");
+        return;
+    }
+
+    if (len > (MAX_PACKET_LENGTH - 5) / 2) {
+        len = (MAX_PACKET_LENGTH - 5) / 2;
+    }
+
+    if (len < auxv_len - offset) {
+        g_string_assign(gdbserver_state.str_buf, "m");
+        memtox(gdbserver_state.str_buf, mem, len);
+    } else {
+        g_string_assign(gdbserver_state.str_buf, "l");
+        memtox(gdbserver_state.str_buf, mem, auxv_len - offset);
+    }
+
+    put_packet_binary(gdbserver_state.str_buf->str,
+                      gdbserver_state.str_buf->len, true);
+}
+#endif
+
 static void handle_query_attached(GdbCmdContext *gdb_ctx, void *user_ctx)
 {
     put_packet(GDB_ATTACHED);
@@ -2338,6 +2384,14 @@ static GdbCmdParseEntry gdb_gen_query_table[] = {
         .cmd_startswith = 1,
         .schema = "s:l,l0"
     },
+#ifdef CONFIG_USER_ONLY
+    {
+        .handler = handle_query_xfer_auxv,
+        .cmd = "Xfer:auxv:read::",
+        .cmd_startswith = 1,
+        .schema = "l,l0"
+    },
+#endif
     {
         .handler = handle_query_attached,
         .cmd = "Attached:",
MAINTAINERS +1 -0
index a83416d54c..9dedc37be4 100644
@@ -2295,6 +2295,7 @@ R: Philippe Mathieu-Daudé <[email protected]>
 S: Maintained
 F: gdbstub*
 F: gdb-xml/
+F: tests/tcg/multiarch/gdbstub/
 
 Memory API
 M: Paolo Bonzini <[email protected]>
tests/tcg/multiarch/Makefile.target +8 -1
index cb49cc9ccb..c8cdb1e04d 100644
@@ -54,7 +54,14 @@ run-gdbstub-sha1: sha1
 		--bin $< --test $(MULTIARCH_SRC)/gdbstub/sha1.py, \
 	"basic gdbstub support")
 
-EXTRA_RUNS += run-gdbstub-sha1
+run-gdbstub-qxfer-auxv-read: sha1
+	$(call run-test, $@, $(GDB_SCRIPT) \
+		--gdb $(HAVE_GDB_BIN) \
+		--qemu $(QEMU) --qargs "$(QEMU_OPTS)" \
+		--bin $< --test $(MULTIARCH_SRC)/gdbstub/test-qxfer-auxv-read.py, \
+	"basic gdbstub qXfer:auxv:read support")
+
+EXTRA_RUNS += run-gdbstub-sha1 run-gdbstub-qxfer-auxv-read
 endif
 
 
tests/tcg/multiarch/gdbstub/test-qxfer-auxv-read.py new file +57 -0

57 lines not on this page · raw message

Signature · 1 line
2.20.1
Reviewed-by on v1 3/6
#7 lore raw

Re: [PATCH v1 3/6] gdbstub: add support to Xfer:auxv:read: packet

Quoted 9 lines · Alex Bennée

From: Lirong Yuan <[email protected]>

This allows gdb to access the target’s auxiliary vector, which can be helpful for telling system libraries important details about the hardware, operating system, and process.

[AJB: minor tweaks to test case, update MAINTAINERS]
Quoted 8 lines

Message-Id: <[email protected]>

---
 gdbstub.c                                     | 54 ++++++++++++++++++
 MAINTAINERS                                   |  1 +
 tests/tcg/multiarch/Makefile.target           |  9 ++-
 .../multiarch/gdbstub/test-qxfer-auxv-read.py | 57 +++++++++++++++++++
 4 files changed, 120 insertions(+), 1 deletion(-)
 create mode 100644 tests/tcg/multiarch/gdbstub/test-qxfer-auxv-read.py

r~

#8 lore raw

PATCH 4/6 gdbstub: drop CPUEnv from gdb_exit()

In reply to Alex Bennée

gdb_exit() has never needed anything from env and I doubt we are going to start now.

---
 include/exec/gdbstub.h    | 2 +-
 bsd-user/syscall.c        | 6 +++---
 gdbstub.c                 | 2 +-
 linux-user/exit.c         | 2 +-
 target/arm/arm-semi.c     | 2 +-
 target/m68k/m68k-semi.c   | 2 +-
 target/nios2/nios2-semi.c | 2 +-
 7 files changed, 9 insertions(+), 9 deletions(-)
include/exec/gdbstub.h +1 -1
index 94d8f83e92..492db0f512 100644
@@ -46,7 +46,7 @@ void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...);
 void gdb_do_syscallv(gdb_syscall_complete_cb cb, const char *fmt, va_list va);
 int use_gdb_syscalls(void);
 void gdb_set_stop_cpu(CPUState *cpu);
-void gdb_exit(CPUArchState *, int);
+void gdb_exit(int);
 #ifdef CONFIG_USER_ONLY
 /**
  * gdb_handlesig: yield control to gdb
bsd-user/syscall.c +3 -3
index d38ec7a162..adc3d21b54 100644
@@ -333,7 +333,7 @@ abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1,
 #ifdef CONFIG_GPROF
         _mcleanup();
 #endif
-        gdb_exit(cpu_env, arg1);
+        gdb_exit(arg1);
         qemu_plugin_atexit_cb();
         /* XXX: should free thread stack and CPU env */
         _exit(arg1);
@@ -435,7 +435,7 @@ abi_long do_netbsd_syscall(void *cpu_env, int num, abi_long arg1,
 #ifdef CONFIG_GPROF
         _mcleanup();
 #endif
-        gdb_exit(cpu_env, arg1);
+        gdb_exit(arg1);
         qemu_plugin_atexit_cb();
         /* XXX: should free thread stack and CPU env */
         _exit(arg1);
@@ -514,7 +514,7 @@ abi_long do_openbsd_syscall(void *cpu_env, int num, abi_long arg1,
 #ifdef CONFIG_GPROF
         _mcleanup();
 #endif
-        gdb_exit(cpu_env, arg1);
+        gdb_exit(arg1);
         qemu_plugin_atexit_cb();
         /* XXX: should free thread stack and CPU env */
         _exit(arg1);
gdbstub.c +1 -1
index ec8daa002b..f6566c7b82 100644
@@ -3068,7 +3068,7 @@ static void gdb_read_byte(uint8_t ch)
 }
 
 /* Tell the remote gdb that the process has exited.  */
-void gdb_exit(CPUArchState *env, int code)
+void gdb_exit(int code)
 {
   char buf[4];
 
linux-user/exit.c +1 -1
index 1594015444..70b344048c 100644
@@ -34,6 +34,6 @@ void preexit_cleanup(CPUArchState *env, int code)
 #ifdef CONFIG_GCOV
         __gcov_dump();
 #endif
-        gdb_exit(env, code);
+        gdb_exit(code);
         qemu_plugin_atexit_cb();
 }
target/arm/arm-semi.c +1 -1
index c892e0e674..5d5654bec0 100644
@@ -1100,7 +1100,7 @@ target_ulong do_arm_semihosting(CPUARMState *env)
              */
             ret = (args == ADP_Stopped_ApplicationExit) ? 0 : 1;
         }
-        gdb_exit(env, ret);
+        gdb_exit(ret);
         exit(ret);
     case TARGET_SYS_SYNCCACHE:
         /*
target/m68k/m68k-semi.c +1 -1
index 8e5fbfc8fa..6230a789b6 100644
@@ -194,7 +194,7 @@ void do_m68k_semihosting(CPUM68KState *env, int nr)
     args = env->dregs[1];
     switch (nr) {
     case HOSTED_EXIT:
-        gdb_exit(env, env->dregs[0]);
+        gdb_exit(env->dregs[0]);
         exit(env->dregs[0]);
     case HOSTED_OPEN:
         GET_ARG(0);
target/nios2/nios2-semi.c +1 -1
index d7a80dd303..e508b2fafc 100644
@@ -215,7 +215,7 @@ void do_nios2_semihosting(CPUNios2State *env)
     args = env->regs[R_ARG1];
     switch (nr) {
     case HOSTED_EXIT:
-        gdb_exit(env, env->regs[R_ARG0]);
+        gdb_exit(env->regs[R_ARG0]);
         exit(env->regs[R_ARG0]);
     case HOSTED_OPEN:
         GET_ARG(0);
Signature · 1 line
2.20.1
Reviewed-by on v1 4/6
#9 lore raw

Re: [PATCH v1 4/6] gdbstub: drop CPUEnv from gdb_exit()

Quoted 4 lines · Alex Bennée

gdb_exit() has never needed anything from env and I doubt we are going to start now.

Quoted 9 lines
---
 include/exec/gdbstub.h    | 2 +-
 bsd-user/syscall.c        | 6 +++---
 gdbstub.c                 | 2 +-
 linux-user/exit.c         | 2 +-
 target/arm/arm-semi.c     | 2 +-
 target/m68k/m68k-semi.c   | 2 +-
 target/nios2/nios2-semi.c | 2 +-
 7 files changed, 9 insertions(+), 9 deletions(-)

r~

#10 lore raw

PATCH 5/6 gdbstub: drop gdbserver_cleanup in favour of gdb_exit

In reply to Alex Bennée

Despite it's name it didn't actually clean-up so let us document gdb_exit() better and use that.

---
 include/exec/gdbstub.h | 14 +++++++++++---
 gdbstub.c              |  7 -------
 softmmu/vl.c           |  2 +-
 3 files changed, 12 insertions(+), 11 deletions(-)
include/exec/gdbstub.h +11 -3
index 492db0f512..ff0b7bc45e 100644
@@ -46,7 +46,17 @@ void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...);
 void gdb_do_syscallv(gdb_syscall_complete_cb cb, const char *fmt, va_list va);
 int use_gdb_syscalls(void);
 void gdb_set_stop_cpu(CPUState *cpu);
-void gdb_exit(int);
+
+/**
+ * gdb_exit: exit gdb session, reporting inferior status
+ * @code: exit code reported
+ *
+ * This closes the session and sends a final packet to GDB reporting
+ * the exit status of the program. It also cleans up any connections
+ * detritus before returning.
+ */
+void gdb_exit(int code);
+
 #ifdef CONFIG_USER_ONLY
 /**
  * gdb_handlesig: yield control to gdb
@@ -187,8 +197,6 @@ static inline uint8_t * gdb_get_reg_ptr(GByteArray *buf, int len)
  */
 int gdbserver_start(const char *port_or_device);
 
-void gdbserver_cleanup(void);
-
 /**
  * gdb_has_xml:
  * This is an ugly hack to cope with both new and old gdb.
gdbstub.c +0 -7
index f6566c7b82..7172f063a1 100644
@@ -3547,13 +3547,6 @@ int gdbserver_start(const char *device)
     return 0;
 }
 
-void gdbserver_cleanup(void)
-{
-    if (gdbserver_state.init) {
-        put_packet("W00");
-    }
-}
-
 static void register_types(void)
 {
     type_register_static(&char_gdb_type_info);
softmmu/vl.c +1 -1
index e6e0ad5a92..0694a1d602 100644
@@ -4509,7 +4509,7 @@ void qemu_init(int argc, char **argv, char **envp)
 
 void qemu_cleanup(void)
 {
-    gdbserver_cleanup();
+    gdb_exit(0);
 
     /*
      * cleaning up the migration object cancels any existing migration
Signature · 1 line
2.20.1
Reviewed-by on v1 5/6
#11 lore raw

Re: [PATCH v1 5/6] gdbstub: drop gdbserver_cleanup in favour of gdb_exit

Quoted 9 lines · Alex Bennée

Despite it's name it didn't actually clean-up so let us document gdb_exit() better and use that.

---
 include/exec/gdbstub.h | 14 +++++++++++---
 gdbstub.c              |  7 -------
 softmmu/vl.c           |  2 +-
 3 files changed, 12 insertions(+), 11 deletions(-)

r~

#12 lore raw

PATCH 6/6 gdbstub: ensure we clean-up when terminated

In reply to Alex Bennée

If you kill the inferior from GDB we end up leaving our socket lying around. Fix this by calling gdb_exit() first.

---
 gdbstub.c | 2 ++
 1 file changed, 2 insertions(+)
gdbstub.c +2 -0
index 7172f063a1..61fe2360e3 100644
@@ -1978,6 +1978,7 @@ static void handle_v_kill(GdbCmdContext *gdb_ctx, void *user_ctx)
     /* Kill the target */
     put_packet("OK");
     error_report("QEMU: Terminated via GDBstub");
+    gdb_exit(0);
     exit(0);
 }
 
@@ -2539,6 +2540,7 @@ static int gdb_handle_packet(const char *line_buf)
     case 'k':
         /* Kill the target */
         error_report("QEMU: Terminated via GDBstub");
+        gdb_exit(0);
         exit(0);
     case 'D':
         {
Signature · 1 line
2.20.1