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
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(-)
@@ -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)
@@ -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));
@@ -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;
@@ -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"