From: Josip Seketa <josip.seketa@gmail.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tromey@adacore.com>
Subject: [PATCH v2 2/2] gdb: read a serial's error fd once per input chunk, not per character
Date: Fri, 06 Nov 2026 18:44:44 +0000 [thread overview]
Message-ID: <gdb-serial-error-fd-v2.2.20261106@gmail.com> (raw)
In-Reply-To: <gdb-serial-error-fd-v2.0.20261106@gmail.com>
generic_readchar reads the error fd after every character it returns.
For a pipe target ("target remote | command"), that fd is the
command's stderr.
ser-pipe.c has no "avail" method, so each of these reads is a read(2)
on a non-blocking socket, which nearly always fails with EAGAIN.
Reading memory therefore costs one extra system call per byte.
Read the error fd only when the character is the first one of a chunk
read from the device, or is an error or end of file.
Error output written before a chunk's data is still printed before
that data is used.
Error output that arrives while GDB works through a chunk is printed
with the next chunk, or by ser_base_wait_for when GDB next waits for
input, or by handle_error_fd in the event loop.
TCP and serial connections have no error fd and are unchanged.
Also add a comment listing where the error fd is read, and why.
---
gdb/ser-base.c | 38 ++++++++++++++++++++++++++++++++++----
1 file changed, 34 insertions(+), 4 deletions(-)
diff --git a/gdb/ser-base.c b/gdb/ser-base.c
index fe84bf034..ca92d05fc 100644
--- a/gdb/ser-base.c
+++ b/gdb/ser-base.c
@@ -295,7 +295,24 @@ ser_base_wait_for (struct serial *scb, int timeout)
static constexpr size_t ERROR_FD_READ_MAX = 1024 * 1024;
-/* See ser-base.h. */
+/* See ser-base.h.
+
+ For a pipe, the error fd is the command's stderr. GDB has to keep
+ reading it while it reads the input: a command that fills it blocks,
+ and then the input GDB is waiting for does not come. It is read:
+
+ - by handle_error_fd, while the event loop runs;
+
+ - by ser_base_wait_for, while GDB is blocked waiting for input, as
+ in remote.c's readchar, when the event loop does not run;
+
+ - by do_ser_base_readchar, for serials ser_base_wait_for leaves it
+ to, every second of such a wait;
+
+ - by generic_readchar, once for each chunk of input, so that error
+ output written before the chunk is shown before it is used;
+
+ - by pipe_close, once the command has exited, for what is left. */
void
ser_base_read_error_fd (struct serial *scb, int close_fd)
@@ -420,7 +437,9 @@ do_ser_base_readchar (struct serial *scb, int timeout)
/* We also need to check and consume the stderr because it could
come before the stdout for some stubs. If we just sit and wait
- for stdout, we would hit a deadlock for that case. */
+ for stdout, we would hit a deadlock for that case. This is for
+ the serials ser_base_wait_for leaves it to; for the others, it
+ has done it already. */
ser_base_read_error_fd (scb, 0);
}
@@ -467,6 +486,11 @@ generic_readchar (struct serial *scb, int timeout,
int (do_readchar) (struct serial *scb, int timeout))
{
int ch;
+ /* Whether CH will be the first character of a chunk read from
+ SCB->fd (here or by fd_event), or an error, rather than the next
+ one of a chunk already begun. */
+ bool chunk_start = scb->bufcnt <= 0 || scb->bufp == scb->buf;
+
if (scb->bufcnt > 0)
{
ch = *scb->bufp;
@@ -497,8 +521,14 @@ generic_readchar (struct serial *scb, int timeout,
}
}
- /* Read any error output we might have. */
- ser_base_read_error_fd (scb, 1);
+ /* Read any error output we might have: once a chunk, when its
+ first character is taken, so that error output written before
+ the chunk's data is shown before any of that data is used. Not
+ for every character: for a pipe, whose ops have no avail, each
+ call is a read on SCB->error_fd that nearly always fails with
+ EAGAIN, and a large reply made one such call per byte. */
+ if (chunk_start)
+ ser_base_read_error_fd (scb, 1);
reschedule (scb);
return ch;
--
2.56.0
prev parent reply other threads:[~2026-11-06 18:45 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-03 19:57 [PATCH] " Josip Seketa
2026-11-05 20:05 ` Tom Tromey
2026-11-06 18:44 ` [PATCH v2 0/2] gdb: read a pipe command's stderr while waiting, " Josip Seketa
2026-11-06 18:44 ` [PATCH v2 1/2] gdb: watch a serial's error fd while waiting for input Josip Seketa
2026-11-06 18:44 ` Josip Seketa [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=gdb-serial-error-fd-v2.2.20261106@gmail.com \
--to=josip.seketa@gmail.com \
--cc=gdb-patches@sourceware.org \
--cc=tromey@adacore.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox