Skip to contentSkip to conversation

gdbstub: send a terminaison packet instead of crashing gdb

PATCH Superseded KONRAD Frederic 5 participants 9 messages Mar 16, 2018 – Mar 20, 2018

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gdbstub: send a terminaison packet instead of crashing gdb

Since the commit:
commit 4486e89c219c0d1b9bd8dfa0b1dd5b0d51ff2268

Author: Stefan Hajnoczi <[email protected]>
Date:   Wed Mar 7 14:42:05 2018 +0000

vl: introduce vm_shutdown()

GDB crash when qemu exits (at least on sparc-softmmu):
Remote communication error. Target disconnected.: Connection reset by peer.
Quitting: putpkt: write failed: Broken pipe.

So send a packet to kill GDB before we exit QEMU:
[Inferior 1 (Thread 0) exited normally]

---
 gdbstub.c              | 7 +++++++
 include/exec/gdbstub.h | 2 ++
 vl.c                   | 2 ++
 3 files changed, 11 insertions(+)
gdbstub.c +7 -0
index f1d5148..a76b2fa 100644
@@ -2052,6 +2052,13 @@ int gdbserver_start(const char *device)
     return 0;
 }
 
+void gdbserver_cleanup(void)
+{
+    if (gdbserver_state) {
+        put_packet(gdbserver_state, "W00");
+    }
+}
+
 static void register_types(void)
 {
     type_register_static(&char_gdb_type_info);
include/exec/gdbstub.h +2 -0
index 9aa7756..2e8a4b8 100644
@@ -103,6 +103,8 @@ int gdbserver_start(int);
 int gdbserver_start(const char *port);
 #endif
 
+void gdbserver_cleanup(void);
+
 /**
  * gdb_has_xml:
  * This is an ugly hack to cope with both new and old gdb.
vl.c +2 -0
index 3ef04ce..0427b15 100644
@@ -4723,6 +4723,8 @@ int main(int argc, char **argv, char **envp)
 
     main_loop();
 
+    gdbserver_cleanup();
+
     /* No more vcpu or device emulation activity beyond this point */
     vm_shutdown();
 
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 14 lines · KONRAD Frederic

Since the commit:
commit 4486e89c219c0d1b9bd8dfa0b1dd5b0d51ff2268

Author: Stefan Hajnoczi <[email protected]>
Date:   Wed Mar 7 14:42:05 2018 +0000

vl: introduce vm_shutdown()

GDB crash when qemu exits (at least on sparc-softmmu):
Remote communication error. Target disconnected.: Connection reset by peer.
Quitting: putpkt: write failed: Broken pipe.

So send a packet to kill GDB before we exit QEMU:
[Inferior 1 (Thread 0) exited normally]

Quoted 6 lines
---
 gdbstub.c              | 7 +++++++
 include/exec/gdbstub.h | 2 ++
 vl.c                   | 2 ++
 3 files changed, 11 insertions(+)

We didn't send an exiting packet before commit 4486e89c219c0, so do you know why this worked before then? (Telling gdb we're exiting seems like the right thing, though.)

thanks
-- PMM

#2 lore raw
Quoted 22 lines · Peter Maydell
Quoted 14 lines · KONRAD Frederic

Since the commit:
commit 4486e89c219c0d1b9bd8dfa0b1dd5b0d51ff2268

Author: Stefan Hajnoczi <[email protected]>
Date:   Wed Mar 7 14:42:05 2018 +0000

vl: introduce vm_shutdown()

GDB crash when qemu exits (at least on sparc-softmmu):
Remote communication error. Target disconnected.: Connection reset by peer.
Quitting: putpkt: write failed: Broken pipe.

So send a packet to kill GDB before we exit QEMU:
[Inferior 1 (Thread 0) exited normally]

Quoted 6 lines
---
  gdbstub.c              | 7 +++++++
  include/exec/gdbstub.h | 2 ++
  vl.c                   | 2 ++
  3 files changed, 11 insertions(+)
Quoted 4 lines

We didn't send an exiting packet before commit 4486e89c219c0, so do you know why this worked before then? (Telling gdb we're exiting seems like the right thing, though.)

Hmmm good question, I didn't had time to investigate in detail

Before 4486e89c219c0:

(gdb) tar rem :1234
Remote debugging using :1234
0x40000000 in trap_table ()
(gdb) c
Continuing.
Remote connection closed

After 4486e89c219c0:

(gdb) tar rem :1234
Remote debugging using :1234
0x40000000 in trap_table ()
(gdb) c
Continuing.
putpkt: write failed: Connection reset by peer.

With the patch:

(gdb) tar rem :1234
Remote debugging using :1234
0x40000000 in trap_table ()
(gdb) c
Continuing.
[Inferior 1 (Thread 0) exited normally]

We use to have this patch in our repository to avoid the remote connection closed above.

Thanks,
Fred

Quoted 3 lines

thanks
-- PMM

Reviewed-by
#3 lore raw
Quoted 57 lines · KONRAD Frederic
Quoted 22 lines · Peter Maydell
Quoted 14 lines · KONRAD Frederic

Since the commit:
commit 4486e89c219c0d1b9bd8dfa0b1dd5b0d51ff2268

Author: Stefan Hajnoczi <[email protected]>
Date:   Wed Mar 7 14:42:05 2018 +0000

vl: introduce vm_shutdown()

GDB crash when qemu exits (at least on sparc-softmmu):
Remote communication error. Target disconnected.: Connection reset by peer.
Quitting: putpkt: write failed: Broken pipe.

So send a packet to kill GDB before we exit QEMU:
[Inferior 1 (Thread 0) exited normally]

Quoted 6 lines
---
  gdbstub.c              | 7 +++++++
  include/exec/gdbstub.h | 2 ++
  vl.c                   | 2 ++
  3 files changed, 11 insertions(+)
Quoted 4 lines

We didn't send an exiting packet before commit 4486e89c219c0, so do you know why this worked before then? (Telling gdb we're exiting seems like the right thing, though.)

Hmmm good question, I didn't had time to investigate in detail

Before 4486e89c219c0:

(gdb) tar rem :1234
Remote debugging using :1234
0x40000000 in trap_table ()
(gdb) c
Continuing.
Remote connection closed

After 4486e89c219c0:

(gdb) tar rem :1234
Remote debugging using :1234
0x40000000 in trap_table ()
(gdb) c
Continuing.
putpkt: write failed: Connection reset by peer.

With the patch:

(gdb) tar rem :1234
Remote debugging using :1234
0x40000000 in trap_table ()
(gdb) c
Continuing.
[Inferior 1 (Thread 0) exited normally]

Quoted 2 lines

We use to have this patch in our repository to avoid the remote connection closed above.

Previously pause_vcpus() didn't invoke the vm change state handler. Therefore the gdbstub didn't tell GDB that the vcpu was being stopped from gdb_vm_state_change().

Now vm_shutdown() invokes vm change state handlers so the gdbstub tells GDB that the vcpu is stopping. As a result GDB sends a packet to query the state of the vcpu - but QEMU terminates and closes the connection partway through.

Here is the strace:
31365 poll([{fd=3, events=POLLIN}, {fd=7, events=POLLIN}, {fd=11, events=POLLIN}, {fd=13, events=POLLIN}], 4, -1) = 1 ([{fd=13, revents=POLLIN}]) 31365 recvfrom(13, "$T03thread:01;#05", 8192, 0, NULL, NULL) = 17
31365 poll([{fd=13, events=POLLIN}], 1, 0) = 1 ([{fd=13, revents=POLLIN}])
31365 sendto(13, "+", 1, 0, NULL, 0) = 1
31365 sendto(13, "$g#67", 5, 0, NULL, 0) = -1 EPIPE (Broken pipe)
31365 --- SIGPIPE {si_signo=SIGPIPE, si_code=SI_USER, si_pid=31365, si_uid=1000} ---

The ugly error message is because QEMU doesn't respond to GDB's "$g#67" message.

Your patch is making GDB exit gracefully.

Reviewed-by on v1 1/1
#4 lore raw
In reply to KONRAD Frederic
Quoted 7 lines · KONRAD Frederic

Since the commit:
commit 4486e89c219c0d1b9bd8dfa0b1dd5b0d51ff2268

Author: Stefan Hajnoczi <[email protected]>
Date:   Wed Mar 7 14:42:05 2018 +0000

vl: introduce vm_shutdown()

Quoted 8 lines

GDB crash when qemu exits (at least on sparc-softmmu):
Remote communication error. Target disconnected.: Connection reset by peer.
Quitting: putpkt: write failed: Broken pipe.

So send a packet to kill GDB before we exit QEMU:
[Inferior 1 (Thread 0) exited normally]

Quoted 51 lines
---
 gdbstub.c              | 7 +++++++
 include/exec/gdbstub.h | 2 ++
 vl.c                   | 2 ++
 3 files changed, 11 insertions(+)
gdbstub.c +7 -0
index f1d5148..a76b2fa 100644
@@ -2052,6 +2052,13 @@ int gdbserver_start(const char *device)
     return 0;
 }
 
+void gdbserver_cleanup(void)
+{
+    if (gdbserver_state) {
+        put_packet(gdbserver_state, "W00");
+    }
+}
+
 static void register_types(void)
 {
     type_register_static(&char_gdb_type_info);
include/exec/gdbstub.h +2 -0
index 9aa7756..2e8a4b8 100644
@@ -103,6 +103,8 @@ int gdbserver_start(int);
 int gdbserver_start(const char *port);
 #endif
 
+void gdbserver_cleanup(void);
+
 /**
  * gdb_has_xml:
  * This is an ugly hack to cope with both new and old gdb.
vl.c +2 -0
index 3ef04ce..0427b15 100644
@@ -4723,6 +4723,8 @@ int main(int argc, char **argv, char **envp)
 
     main_loop();
 
+    gdbserver_cleanup();
+
     /* No more vcpu or device emulation activity beyond this point */
     vm_shutdown();
 
#5 lore raw

Hi Philippe,

Thanks for the review!

BTW I forgot the for 2.12 tag can this be included in 2.12 or is it too late?

Thanks,
Fred

Quoted 70 lines · Philippe Mathieu-Daudé
Quoted 7 lines · KONRAD Frederic

Since the commit:
commit 4486e89c219c0d1b9bd8dfa0b1dd5b0d51ff2268

Author: Stefan Hajnoczi <[email protected]>
Date:   Wed Mar 7 14:42:05 2018 +0000

vl: introduce vm_shutdown()

Quoted 8 lines

GDB crash when qemu exits (at least on sparc-softmmu):
Remote communication error. Target disconnected.: Connection reset by peer.
Quitting: putpkt: write failed: Broken pipe.

So send a packet to kill GDB before we exit QEMU:
[Inferior 1 (Thread 0) exited normally]

Quoted 51 lines
---
  gdbstub.c              | 7 +++++++
  include/exec/gdbstub.h | 2 ++
  vl.c                   | 2 ++
  3 files changed, 11 insertions(+)
gdbstub.c +7 -0
index f1d5148..a76b2fa 100644
@@ -2052,6 +2052,13 @@ int gdbserver_start(const char *device)
      return 0;
  }
 
+void gdbserver_cleanup(void)
+{
+    if (gdbserver_state) {
+        put_packet(gdbserver_state, "W00");
+    }
+}
+
  static void register_types(void)
  {
      type_register_static(&char_gdb_type_info);
include/exec/gdbstub.h +2 -0
index 9aa7756..2e8a4b8 100644
@@ -103,6 +103,8 @@ int gdbserver_start(int);
  int gdbserver_start(const char *port);
  #endif
 
+void gdbserver_cleanup(void);
+
  /**
   * gdb_has_xml:
   * This is an ugly hack to cope with both new and old gdb.
vl.c +2 -0
index 3ef04ce..0427b15 100644
@@ -4723,6 +4723,8 @@ int main(int argc, char **argv, char **envp)
 
      main_loop();
 
+    gdbserver_cleanup();
+
      /* No more vcpu or device emulation activity beyond this point */
      vm_shutdown();
 
#6 lore raw
Quoted 6 lines · KONRAD Frederic

Hi Philippe,

Thanks for the review!

BTW I forgot the for 2.12 tag can this be included in 2.12 or is it too late?

It's a bug-fix, so it can be included during softfreeze. I've amended the subject line if it helps the right maintainer notice; or you can resend a v2 with the typos fixed, R-b comments added, and the 2.12 tag in your resend.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#7 lore raw
In reply to KONRAD Frederic

On 03/16/2018 11:23 AM, KONRAD Frederic wrote:

In the subject: s/terminaison/termination/

Quoted 8 lines

Since the commit:
commit 4486e89c219c0d1b9bd8dfa0b1dd5b0d51ff2268

Author: Stefan Hajnoczi <[email protected]>
Date:   Wed Mar 7 14:42:05 2018 +0000

vl: introduce vm_shutdown()

GDB crash when qemu exits (at least on sparc-softmmu):

s/crash/crashes/

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#8 lore raw

oops sorry for that, I'll resend.

Thanks,
Fred

Quoted 15 lines · Eric Blake

On 03/16/2018 11:23 AM, KONRAD Frederic wrote:

In the subject: s/terminaison/termination/

Quoted 8 lines

Since the commit:
commit 4486e89c219c0d1b9bd8dfa0b1dd5b0d51ff2268
Author: Stefan Hajnoczi <[email protected]>
Date:   Wed Mar 7 14:42:05 2018 +0000

     vl: introduce vm_shutdown()

GDB crash when qemu exits (at least on sparc-softmmu):

s/crash/crashes/