Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [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

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