[17/18] gdbserver: don't exit until GDB disconnects

Message ID 1444836486-25679-18-git-send-email-palves@redhat.com
State New, archived
Headers

Commit Message

Pedro Alves Oct. 14, 2015, 3:28 p.m. UTC
  When testing with "target remote" with "maint set target-non-stop on",
we regressions like this:

  Running /home/pedro/gdb/mygit/build/../src/gdb/testsuite/gdb.threads/continue-pending-after-query.exp ...
  FAIL: gdb.threads/continue-pending-after-query.exp: iter 4: continue until exit
  FAIL: gdb.threads/continue-pending-after-query.exp: iter 6: continue until exit
  FAIL: gdb.threads/continue-pending-after-query.exp: iter 10: continue until exit

		  === gdb Summary ===

  # of expected passes            28
  # of unexpected failures        3

where gdb.log shows:

  continue
  Continuing.
  Remote communication error.  Target disconnected.: Connection reset by peer.
  (gdb) FAIL: gdb.threads/continue-pending-after-query.exp: iter 4: continue until exit

Enabling gdb + gdbserver debug logs we see:

  gdbserver:  <<<< exiting linux_wait_1
  gdbserver: handling possible serial event
  gdbserver: Writing resume reply for LWP 11089.11089:0
  gdbserver: handling possible serial event
  gdbserver: GDBserver exiting

	GDB: Packet received: OK
	GDB: infrun: prepare_to_wait
	GDB: Sending packet: $vStopped#55...Packet received: W0;process:2b51
	GDB: Sending packet: $vStopped#55...Packet received: OK
	GDB: infrun: target_wait (-1.0.0, status) =
	GDB: infrun:   -1.0.0 [Thread 0],
	GDB: infrun:   status->kind = no-resumed
	GDB: Sending packet: $Hgp2b51.2b51#41...Remote connection closed
    (gdb) FAIL: gdb.threads/continue-pending-after-query.exp: iter 1: continue until exit

Notice the "Packet received: W0;process:2b51" followed by
vStopped->OK.

That means the process exit notification was successfully sent to GDB
and GDB fetched it.  That makes gdbserver exit, in
server.c:process_serial_event:

  if (!extended_protocol && have_ran && !target_running ())
    {
      /* In non-stop, defer exiting until GDB had a chance to query
	 the whole vStopped list (until it gets an OK).  */
      if (QUEUE_is_empty (notif_event_p, notif_stop.queue))
	{
	  /* Be transparent when GDB is connected through stdio -- no
	     need to spam GDB's console.  */
	  if (!remote_connection_is_stdio ())
	    fprintf (stderr, "GDBserver exiting\n");
	  remote_close ();
	  exit (0);
	}
    }

However, GDB is still busy processing an earlier "no-resumed" event,
and sends a "Hg" packet, which errors out with "Remote connection
closed".  IOW, it's not enough to wait for GDB to query the whole
vStopped list, gdbserver needs to wait until the exit event is really
processed.

The fix is to make gdbserver not disconnect until gdb does.

Tested on x86_64 Fedora, native gdbserver, remote + extended-remote +
with and without "maint set target-non-stop on".

gdb/gdbserver/ChangeLog:
2015-10-14  Pedro Alves  <palves@redhat.com>

	* remote-utils.c (readchar): Don't print "Got EOF" unless
	debugging gdbserver.
	* server.c (captured_main): Exit gdbserver if gdb disconnects when
	in "target remote" mode and there are no processes left to debug.
	(process_serial_event): Remove 'have_ran' static local and remove
	logic that exits gdbserver in "target remote" mode.
---
 gdb/gdbserver/remote-utils.c |  5 ++++-
 gdb/gdbserver/server.c       | 40 ++++++++++++++--------------------------
 2 files changed, 18 insertions(+), 27 deletions(-)
  

Patch

diff --git a/gdb/gdbserver/remote-utils.c b/gdb/gdbserver/remote-utils.c
index 77280f7..05e3d63 100644
--- a/gdb/gdbserver/remote-utils.c
+++ b/gdb/gdbserver/remote-utils.c
@@ -882,7 +882,10 @@  readchar (void)
       if (readchar_bufcnt <= 0)
 	{
 	  if (readchar_bufcnt == 0)
-	    fprintf (stderr, "readchar: Got EOF\n");
+	    {
+	      if (remote_debug)
+		fprintf (stderr, "readchar: Got EOF\n");
+	    }
 	  else
 	    perror ("readchar");
 
diff --git a/gdb/gdbserver/server.c b/gdb/gdbserver/server.c
index d83d608..5a9b471 100644
--- a/gdb/gdbserver/server.c
+++ b/gdb/gdbserver/server.c
@@ -3677,11 +3677,21 @@  captured_main (int argc, char *argv[])
 	  start_event_loop ();
 
 	  /* If an exit was requested (using the "monitor exit"
-	     command), terminate now.  The only other way to get
-	     here is for getpkt to fail; close the connection
-	     and reopen it at the top of the loop.  */
+	     command), terminate now.  */
+	  if (exit_requested)
+	    throw_quit ("Quit");
+
+	  /* The only other way to get here is for getpkt to fail:
+
+	      - If --once was specified, we're done.
 
-	  if (exit_requested || run_once)
+	      - If not in extended-remote mode, and we're no longer
+	        debugging anything, simply exit: GDB has disconnected
+	        after processing the last process exit.
+
+	      - Otherwise, close the connection and reopen it at the
+	        top of the loop.  */
+	  if (run_once || (!extended_protocol && !target_running ()))
 	    throw_quit ("Quit");
 
 	  fprintf (stderr,
@@ -3846,13 +3856,6 @@  process_serial_event (void)
   int packet_len;
   int new_packet_len = -1;
 
-  /* Used to decide when gdbserver should exit in
-     multi-mode/remote.  */
-  static int have_ran = 0;
-
-  if (!have_ran)
-    have_ran = target_running ();
-
   disable_async_io ();
 
   response_needed = 0;
@@ -4291,21 +4294,6 @@  process_serial_event (void)
 
   response_needed = 0;
 
-  if (!extended_protocol && have_ran && !target_running ())
-    {
-      /* In non-stop, defer exiting until GDB had a chance to query
-	 the whole vStopped list (until it gets an OK).  */
-      if (QUEUE_is_empty (notif_event_p, notif_stop.queue))
-	{
-	  /* Be transparent when GDB is connected through stdio -- no
-	     need to spam GDB's console.  */
-	  if (!remote_connection_is_stdio ())
-	    fprintf (stderr, "GDBserver exiting\n");
-	  remote_close ();
-	  exit (0);
-	}
-    }
-
   if (exit_requested)
     return -1;