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 1/2] gdb: watch a serial's error fd while waiting for input
Date: Fri, 06 Nov 2026 18:44:27 +0000	[thread overview]
Message-ID: <gdb-serial-error-fd-v2.1.20261106@gmail.com> (raw)
In-Reply-To: <gdb-serial-error-fd-v2.0.20261106@gmail.com>

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


  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] 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     ` Josip Seketa [this message]
2026-11-06 18:44     ` [PATCH v2 2/2] gdb: read a serial's error fd once per input chunk, " 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.1.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