* [PATCH] gdb: read a serial's error fd once per input chunk, not per character
@ 2026-11-03 19:57 Josip Seketa
2026-11-05 20:05 ` Tom Tromey
0 siblings, 1 reply; 5+ messages in thread
From: Josip Seketa @ 2026-11-03 19:57 UTC (permalink / raw)
To: gdb-patches
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, at the next one-second wait in
do_ser_base_readchar, or by handle_error_fd in the event loop, as when
GDB is idle.
TCP and serial connections have no error fd and are unchanged.
---
This is my first GDB patch, so I'm happy to learn and to rework it as
needed. I came across it by accident while doing embedded work: I was
speeding up a GDB connection to a board over "| ssh" layer by layer
(the stub, the transport, GDB itself), and GDB's side turned out to
make one system call per byte.
I don't have write access; please push it if approved.
Thank you for taking a look.
Measured on x86-64 GNU/Linux, GDB master 10ae2343:
1. A program holding 2 MiB of data, run by gdbserver on a pipe:
target remote | exec gdbserver - prog
2. "dump binary memory" of the 2 MiB.
3. Before and after, alternated, 3 runs each.
Before: 1.86 to 1.89 s, 2,689,827 read calls.
After: 0.034 to 0.035 s, 1,793 read calls.
The dumped bytes are identical.
Over "| ssh host stub" to a board (STM32H753, 1.875 MiB of flash,
GDB 18.1): 5.2 to 5.4 s before, 4.0 to 4.4 s after.
Tested: make check TESTS="gdb.server/*.exp" with
--target_board=native-stdio-gdbserver and with native-gdbserver,
before and after the patch.
The results are the same: 1027 and 1124 expected passes, and the
same one FAIL in both (gdb.server/non-existing-program.exp:
gdbserver exits cleanly).
I have no FSF copyright assignment.
This is a tiny change: 13 lines, most of them a comment.
gdb/ser-base.c | 15 +++++++++++++--
1 file changed, 13 insertions(+), 2 deletions(-)
diff --git a/gdb/ser-base.c b/gdb/ser-base.c
index e9c86889..a6d39018 100644
--- a/gdb/ser-base.c
+++ b/gdb/ser-base.c
@@ -428,6 +428,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;
@@ -458,8 +463,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
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] gdb: read a serial's error fd once per input chunk, not per character
2026-11-03 19:57 [PATCH] gdb: read a serial's error fd once per input chunk, not per character 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
0 siblings, 1 reply; 5+ messages in thread
From: Tom Tromey @ 2026-11-05 20:05 UTC (permalink / raw)
To: Josip Seketa; +Cc: gdb-patches
>>>>> Josip Seketa <josip.seketa@gmail.com> writes:
> 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.
Thank you for the patch.
I hadn't previously taken any notice of this code. Ugly stuff :-(
> 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.
I find it a bit hard to untangle all this but I think there are two
cases here:
1. In async mode, the error fd is handled via the main select/poll loop.
2. In sync mode, the error fd is handled when reading from the main
serial fd as well.
And (again IIUC) your patch addresses an issue with scenario 2.
And I guess this makes sense. We can't always handle the error fd via
the poll loop, because in sync mode gdb will be blocked in some read()
in remote.c.
Ok so far. But then, consider this comment from ser-base.c:
/* 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. */
From this I think the idea is to consider a remote that's writing to
both its stdout and stderr. And, if the stderr pipe fills up, then a
read attempt by gdb on the remote's stdout will never finish -- because
the remote will be blocked while writing.
So, gdb has to be sure to drain the error output while reading.
And so my question is whether your patch preserves this. I somewhat
suspect it does not.
Also I was wondering why you changed the call in generic_readchar (which
passes "1" as the close_fd parameter) and not the call in
do_ser_base_readchar.
I didn't dig much deeper than this. The code here is a bit confusing I
think.
Anyway, I do think some kind of change here would be good. I'm just not
sure exactly what.
Maybe one idea would be if the read from the remote's stdout fails (like
if we're doing a non-blocking read, which I didn't look into), then that
might be a good time to read from the error fd?
I also have a crazier idea involving threads but TBH I'm not sure that
is sensible at all.
Finally I wanted to say:
> Before: 1.86 to 1.89 s, 2,689,827 read calls.
> After: 0.034 to 0.035 s, 1,793 read calls.
This is a nice find, thank you.
Here at AdaCore, we've noticed that remote testing over ssh is
noticeably slower than remote testing over a socket. Investigating this
has never gotten to the top of my to-do list -- but I wonder if you've
found the problem.
Tom
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v2 0/2] gdb: read a pipe command's stderr while waiting, not per character
2026-11-05 20:05 ` Tom Tromey
@ 2026-11-06 18:44 ` 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 ` [PATCH v2 2/2] gdb: read a serial's error fd once per input chunk, not per character Josip Seketa
0 siblings, 2 replies; 5+ messages in thread
From: Josip Seketa @ 2026-11-06 18:44 UTC (permalink / raw)
To: gdb-patches; +Cc: Tom Tromey
Hi Tom,
Thank you for the review. It was very welcome, and it sent me down the
rabbit hole, well past the surface.
First a correction: the numbers in my v1 were wrong, and you quoted
them, so I would like to fix that before anything else.
"1.86 s against 0.034 s" was timed with GDB running under strace,
which makes every read far slower, so it inflated the very cost I was
measuring.
The test program was also mostly one repeated byte, which the protocol
compresses, so little of it crossed the pipe.
And the read calls were counted for the whole session, not the dump.
Without strace, that same test takes 0.04-0.07 s on master and
0.02-0.03 s with the patch: about 2x, not 55x.
The real gain is in the tables below.
You were right to ask about draining.
v1 never deadlocked, because the wait in do_ser_base_readchar drains
the error fd every second, and v1 did not touch that.
But the read per character also drained it while GDB worked through a
chunk, so v1 could be slower when a stub blocks on stderr in the
middle of a reply.
To measure it, a fake stub wrote 300 KiB to stderr, more than the
socket holds, with each of 13 memory replies (3 rounds, alternated;
all output arrived every time):
master v1 v2
before each reply 10.4-13.5 s 8.4-13.3 s 0.20-0.21 s
in the middle of it 1.3-2.3 s 1.2-4.2 s 0.19-0.20 s
The first row is master's one-second wait, once per reply.
So patch 1 goes one step further than your idea of reading the error
fd when the read fails: GDB waits for the input and the error fd
together, and prints error output as it arrives.
Why generic_readchar and not do_ser_base_readchar: only the former
runs for every character. The other one runs once per second of
waiting.
On your two cases, I think the split is whether the event loop runs,
not the mode. While GDB is blocked in remote.c's readchar the serial
may be async, but handle_error_fd cannot fire, so the wait itself has
to watch the error fd. The reads in ser_base_wait_for,
do_ser_base_readchar and generic_readchar are now commented where they
are used, and ser_base_read_error_fd lists every place that reads the
error fd and why.
The per-byte cost is still gone. Without strace, with memory that
does not compress, over "target remote | exec gdbserver - prog", the
bytes identical, 6 rounds, alternated:
master v2
dump 2 MiB 0.75-0.77 s 0.05-0.09 s
read calls 2,130,240 576
connect, run, kill 0.94-0.97 s 0.15-0.19 s
read calls 2,373,809 1,607
The second row needs no dump: GDB reads the shared libraries through
the same pipe when it connects.
All times here are from one laptop with CPU boost on. With boost off
they are 1.9x longer, and the read calls do not change. So the ratio
is what to compare, about 10x for the dump and 5x for connecting.
The program, if you want to repeat it:
static unsigned char big[2 << 20];
void stop (void) {}
int main (void)
{
unsigned x = 2463534242u;
for (unsigned i = 0; i < sizeof big; i++)
{
x ^= x << 13; x ^= x >> 17; x ^= x << 5;
big[i] = x;
}
stop ();
return big[0];
}
About ssh: it depends on how you connect. The same 2 MiB, gdbserver
on the same machine, 5 rounds each, alternated:
master v2
"| ssh localhost" 0.769-0.797 s 0.055-0.118 s
"| gdbserver" 0.744-0.762 s 0.051-0.081 s
TCP port 0.051-0.065 s 0.060-0.078 s
So with "target remote | ssh" this could be what you noticed: v2 is
about as fast as a socket, and master is not. Localhost has no
network delay, so a real link will look different. If ssh only starts
gdbserver and GDB connects over TCP, this will not help. How do you
connect?
This applies on top of my other patch, which also updates a test that
this series needs:
https://inbox.sourceware.org/gdb-patches/179382821911.358872.8271312658591136533@gmail.com/
Tested on x86-64 GNU/Linux, on top of that patch and the two fixes I
am sending separately: gdb.base, gdb.server,
gdb.threads, gdb.mi, gdb.multi and gdb.python, on native-stdio-gdbserver,
native-gdbserver and native-extended-gdbserver, compared with master
test by test. No new failures, apart from the finish_step_over
assertion (bug 26284), which shows up under CPU load on master as
well, at the same rate.
A stub with random stderr bursts, split replies and pauses, 1500 runs
on pipes and 300 on TCP, Unix sockets and a pty, plus 330 under ASan
and UBSan: memory identical, all stderr lines there, one run with one
line out of order that did not come back (GDB's own output can fall
inside a partly read line, on master too). Also checked by hand:
stderr closed mid-session, Ctrl-C, MI, a stub writing stderr without
pause (GDB still retries every 2 seconds), and an error fd above
FD_SETSIZE.
Not tested on Windows. Serials with "avail" keep the old wait there,
but patch 2 and the limit in patch 1 do apply.
This series is larger than v1, so it no longer counts as a tiny change,
and I have no FSF copyright assignment. This is my own work, not my
employer's. What do you need from me?
Josip Seketa (2):
gdb: watch a serial's error fd while waiting for input
gdb: read a serial's error fd once per input chunk, not per character
gdb/ser-base.c | 87 ++++++++++++++++++++++++++++++++++++++++++++------
1 file changed, 78 insertions(+), 9 deletions(-)
--
2.56.0
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v2 1/2] gdb: watch a serial's error fd while waiting for input
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 ` Josip Seketa
2026-11-06 18:44 ` [PATCH v2 2/2] gdb: read a serial's error fd once per input chunk, not per character Josip Seketa
1 sibling, 0 replies; 5+ messages in thread
From: Josip Seketa @ 2026-11-06 18:44 UTC (permalink / raw)
To: gdb-patches; +Cc: Tom Tromey
ser_base_wait_for waits only for the serial's input.
A pipe command that fills its stderr socket blocks, and the input GDB
waits for does not come until the error fd is drained, which
do_ser_base_readchar does each time its one-second wait expires.
So there is no deadlock, but every full socket can cost a second.
Wait for the error fd as well, and print what arrives at once.
At end of file it is closed, because it would stay readable.
A deadline keeps error output from extending the timeout.
Serials with an "avail" method (ser-mingw.c) keep the old behaviour,
since they may not be able to select on the error fd, and so does an
error fd too large for an fd_set.
ser_base_read_error_fd reads until a read would block, so a command
that writes as fast as GDB reads could keep it there past the
timeout.
Limit one call to 1 MiB, well above what a socket holds, so that
normal output is still read in one go.
---
gdb/ser-base.c | 49 ++++++++++++++++++++++++++++++++++++++++++++-----
1 file changed, 44 insertions(+), 5 deletions(-)
diff --git a/gdb/ser-base.c b/gdb/ser-base.c
index 9464165d3..fe84bf034 100644
--- a/gdb/ser-base.c
+++ b/gdb/ser-base.c
@@ -24,6 +24,7 @@
#include "gdbsupport/gdb_select.h"
#include "gdbsupport/gdb_sys_time.h"
+#include <chrono>
#ifdef USE_WIN32API
#include <winsock2.h>
#endif
@@ -204,7 +205,9 @@ push_event (void *context)
}
/* Wait for input on scb, with timeout seconds. Returns 0 on success,
- otherwise SERIAL_TIMEOUT or SERIAL_ERROR. */
+ otherwise SERIAL_TIMEOUT or SERIAL_ERROR. Error output that arrives
+ meanwhile is printed at once: the remote may be blocked writing it,
+ and then the input would never come. */
/* NOTE: Some of the code below is dead. The only possible values of
the TIMEOUT parameter are ONE and ZERO. OTOH, we should probably
@@ -214,28 +217,43 @@ push_event (void *context)
static int
ser_base_wait_for (struct serial *scb, int timeout)
{
+ /* Error output must not extend the timeout. */
+ const auto deadline = (std::chrono::steady_clock::now ()
+ + std::chrono::seconds (timeout));
+
while (1)
{
int numfds;
struct timeval tv;
fd_set readfds, exceptfds;
int nfds;
+ /* Serials with an "avail" method may not be able to select on the
+ error fd, and it may be too large for an fd_set. The wait in
+ do_ser_base_readchar drains it then. */
+ int error_fd = (scb->ops->avail == nullptr && scb->error_fd < FD_SETSIZE
+ ? scb->error_fd : -1);
/* NOTE: Some OS's can scramble the READFDS when the select()
call fails (ex the kernel with Red Hat 5.2). Initialize all
arguments before each call. */
- tv.tv_sec = timeout;
- tv.tv_usec = 0;
+ auto left = std::chrono::duration_cast<std::chrono::microseconds>
+ (deadline - std::chrono::steady_clock::now ()).count ();
+ if (left < 0)
+ left = 0;
+ tv.tv_sec = left / 1000000;
+ tv.tv_usec = left % 1000000;
FD_ZERO (&readfds);
FD_ZERO (&exceptfds);
FD_SET (scb->fd, &readfds);
FD_SET (scb->fd, &exceptfds);
+ if (error_fd != -1)
+ FD_SET (error_fd, &readfds);
QUIT;
- nfds = scb->fd + 1;
+ nfds = std::max (scb->fd, error_fd) + 1;
if (timeout >= 0)
numfds = interruptible_select (nfds, &readfds, 0, &exceptfds, &tv);
else
@@ -252,10 +270,31 @@ ser_base_wait_for (struct serial *scb, int timeout)
poll. */
}
+ if (error_fd != -1 && FD_ISSET (error_fd, &readfds))
+ {
+ /* Closes the error fd at end of file, which stays readable. */
+ ser_base_read_error_fd (scb, 1);
+ if (!FD_ISSET (scb->fd, &readfds) && !FD_ISSET (scb->fd, &exceptfds))
+ {
+ /* The error fd can stay readable, so SELECT's own timeout
+ may never come. */
+ if (left == 0)
+ return SERIAL_TIMEOUT;
+ continue;
+ }
+ }
+
return 0;
}
}
+/* The most one call of ser_base_read_error_fd reads. This is well
+ above what a socket holds, so that normal output is read in one go,
+ but a command that writes as fast as GDB reads cannot keep GDB here
+ past its timeout. */
+
+static constexpr size_t ERROR_FD_READ_MAX = 1024 * 1024;
+
/* See ser-base.h. */
void
@@ -266,7 +305,7 @@ ser_base_read_error_fd (struct serial *scb, int close_fd)
ssize_t s;
char buf[GDB_MI_MSG_WIDTH + 1];
- for (;;)
+ for (size_t total = 0; total < ERROR_FD_READ_MAX; total += s)
{
char *current;
char *newline;
--
2.56.0
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v2 2/2] gdb: read a serial's error fd once per input chunk, not per character
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
1 sibling, 0 replies; 5+ messages in thread
From: Josip Seketa @ 2026-11-06 18:44 UTC (permalink / raw)
To: gdb-patches; +Cc: Tom Tromey
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
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-11-06 18:45 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-03 19:57 [PATCH] gdb: read a serial's error fd once per input chunk, not per character 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 ` [PATCH v2 2/2] gdb: read a serial's error fd once per input chunk, not per character Josip Seketa
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox