Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Josip Seketa <josip.seketa@gmail.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tromey@adacore.com>
Subject: [PATCH v2 0/2] gdb: read a pipe command's stderr while waiting, not per character
Date: Fri, 06 Nov 2026 18:44:11 +0000	[thread overview]
Message-ID: <gdb-serial-error-fd-v2.0.20261106@gmail.com> (raw)
In-Reply-To: <87pkwjxcwe.fsf@tromey.com>

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


  reply	other threads:[~2026-11-06 18:44 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-03 19:57 [PATCH] gdb: read a serial's error fd once per input chunk, " Josip Seketa
2026-11-05 20:05 ` Tom Tromey
2026-11-06 18:44   ` Josip Seketa [this message]
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

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.0.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