gdb: print a pipe command's stderr when the connection is reset

Message ID 179382821911.358872.8271312658591136533@gmail.com
State New
Headers
Series gdb: print a pipe command's stderr when the connection is reset |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Test passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Test passed

Commit Message

Josip Seketa Nov. 4, 2026, 9:36 p.m. UTC
  With "target remote | command", if the command writes an error to
stderr and exits without reading what GDB sent it, GDB's next read
fails with ECONNRESET: the command's end of the socketpair was closed
with unread data.
GDB then prints only:

  Remote communication error.  Target disconnected: error while
  reading: Connection reset by peer.

and the command's own error message is lost.

This is a regression from commit 0da23004a ("Change serial_readchar
to throw").
Since then ser_unix_read_prim throws on a read error, so
generic_readchar no longer reaches its ser_base_read_error_fd call.
pipe_close then closes the error fd without reading it.
Before that commit, the message was printed before the error.

Fix it in pipe_close: once the command has exited, print what is
left on its stderr before closing the error fd.
This covers every way the connection ends, and prints nothing twice,
since ser_base_read_error_fd only shows what has not been read yet.

gdb.server/server-empty-exec-name.exp was written after the
regression and expects nothing between "Remote debugging using" and
the error.
gdbserver's own message ("No program to debug", "Exiting") is now
shown there, so the test expects it.
---
Reproduce (GDB 18.1 and master 10ae2343):

  $ gdb -batch -ex "target remote | sh -c 'echo refused >&2; exit 1'"
  Remote communication error.  Target disconnected: error while reading: Connection reset by peer.

Built at 0da23004a's parent, d69939bde, the same prints "refused"
first.
At 0da23004a and on master it does not.
With this patch it does again.
If the command reads its stdin before exiting, the read gets end of
file instead, and the message was and is printed.

Tested on x86-64 GNU/Linux: make check TESTS="gdb.server/*.exp" with
--target_board=native-stdio-gdbserver and native-gdbserver, master
and patched.
No new failures; the one FAIL of both (gdb.server/non-existing-program.exp:
gdbserver exits cleanly) is unchanged.
server-empty-exec-name.exp passes 16 of 16 with the patch; with the
updated test, unpatched GDB fails its 4 "start gdbserver over stdin"
tests.
Also checked by hand: 200 lines of stderr are all printed, and a
grandchild that keeps stderr open does not delay the close (the
error fd is non-blocking).

I have no FSF copyright assignment.

 gdb/ser-base.c                                      | 4 ++--
 gdb/ser-base.h                                      | 5 +++++
 gdb/ser-pipe.c                                      | 7 +++++++
 gdb/testsuite/gdb.server/server-empty-exec-name.exp | 2 ++
 4 files changed, 16 insertions(+), 2 deletions(-)
  

Patch

diff --git a/gdb/ser-base.c b/gdb/ser-base.c
index e9c86889..9464165d 100644
--- a/gdb/ser-base.c
+++ b/gdb/ser-base.c
@@ -256,9 +256,9 @@  ser_base_wait_for (struct serial *scb, int timeout)
     }
 }
 
-/* Read any error output we might have.  */
+/* See ser-base.h.  */
 
-static void
+void
 ser_base_read_error_fd (struct serial *scb, int close_fd)
 {
   if (scb->error_fd != -1)
diff --git a/gdb/ser-base.h b/gdb/ser-base.h
index 667df4e3..87ea9f52 100644
--- a/gdb/ser-base.h
+++ b/gdb/ser-base.h
@@ -25,6 +25,11 @@ 
 struct serial;
 struct ui_file;
 
+/* Read any error output we might have on SCB's error fd, and print
+   it.  If CLOSE_FD, close the error fd at end of file.  */
+
+extern void ser_base_read_error_fd (struct serial *scb, int close_fd);
+
 extern int generic_readchar (struct serial *scb, int timeout,
 			     int (*do_readchar) (struct serial *scb,
 						 int timeout));
diff --git a/gdb/ser-pipe.c b/gdb/ser-pipe.c
index 3b755a84..ff2c2ae1 100644
--- a/gdb/ser-pipe.c
+++ b/gdb/ser-pipe.c
@@ -185,6 +185,13 @@  pipe_close (struct serial *scb)
 #endif
 	}
 
+      /* The command has exited: print what it wrote to stderr and GDB
+	 has not shown yet.  A read error on SCB->fd, e.g. ECONNRESET
+	 when the command exits without reading what GDB sent, throws
+	 before generic_readchar gets to read the error fd, so this
+	 output would otherwise be lost.  */
+      ser_base_read_error_fd (scb, 1);
+
       if (scb->error_fd != -1)
 	close (scb->error_fd);
       scb->error_fd = -1;
diff --git a/gdb/testsuite/gdb.server/server-empty-exec-name.exp b/gdb/testsuite/gdb.server/server-empty-exec-name.exp
index 85801427..f21adaa5 100644
--- a/gdb/testsuite/gdb.server/server-empty-exec-name.exp
+++ b/gdb/testsuite/gdb.server/server-empty-exec-name.exp
@@ -83,6 +83,8 @@  foreach_with_prefix proto { extended-remote remote } {
 	gdb_test "target $proto | ${::gdbserver} $server_flag --once - \"\"${args}" \
 	    [multi_line \
 		 "Remote debugging using \\| \[^\r\n\]+" \
+		 "No program to debug" \
+		 "Exiting" \
 		 "Remote communication error\\.  Target disconnected: .*"] \
 	    "start gdbserver over stdin"