From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id OZqjLzYg7mrDuSkAWB0awg (envelope-from ) for ; Fri, 06 Nov 2026 13:45:10 -0500 Authentication-Results: simark.ca; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=bHj2DAPV; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id AA09C1E09A; Fri, 06 Nov 2026 13:45:10 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-2.4 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H2,RCVD_IN_VALIDITY_CERTIFIED_BLOCKED, RCVD_IN_VALIDITY_RPBL_BLOCKED,RCVD_IN_VALIDITY_SAFE_BLOCKED autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [38.145.34.32]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 3FD271E043 for ; Fri, 06 Nov 2026 13:45:09 -0500 (EST) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C5AB44BA2E0C for ; Fri, 6 Nov 2026 18:45:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C5AB44BA2E0C Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=bHj2DAPV Received: from mail-wm1-x332.google.com (mail-wm1-x332.google.com [IPv6:2a00:1450:4864:20::332]) by sourceware.org (Postfix) with ESMTPS id 1A46D4BA2E21 for ; Fri, 6 Nov 2026 18:44:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1A46D4BA2E21 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 1A46D4BA2E21 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::332 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1793990685; cv=none; b=uoN2qaLiz9YO/Iubpw21fKcKtCkS255HwfFq2W32fl0BuY+4xIHZ5XKe3RNMJpfP54cbYuKKkmAP2EfZGIKWsYFAiw1erUqIVDW5+hkVgiIsx5/ctWeD0cSyC27wArsEynhD/GRrMCyAQjQhhiBi7AIupsanDDzxArhjsou1g8Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1793990685; c=relaxed/simple; bh=TbywSSsa8YVOxjp98NAa0RygTvTk+GefL2zVs4F8U4c=; h=DKIM-Signature:To:Date:Message-ID:MIME-Version:From:Subject; b=dq+lxiN4DZWWIhN6hFLAnf3qxd06B5BciFq3yz3jAJMWn5VAyj2cRI6kwRGS6bPpj7vog1GgsKjivIzsbcFBpaXf51xe/1ZXm1eWinB+y942y/4sfTFXENGwgPwDybGbDKntAXDt3jc+8feG8oyG20cn02fdXOOb7u1OQsFroAw= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=bHj2DAPV DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1A46D4BA2E21 Received: by mail-wm1-x332.google.com with SMTP id 5b1f17b1804b1-4a210470b49so7059715e9.2 for ; Fri, 06 Nov 2026 10:44:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1793990684; x=1794595484; darn=sourceware.org; h=subject:from:content-transfer-encoding:content-type:mime-version :references:in-reply-to:message-id:date:cc:to:from:to:cc:subject :date:message-id:reply-to:content-type; bh=gjNfjsSizwa4gQFPlTmLJDdH/oRSAXvuLXcNmAdoiyU=; b=bHj2DAPV2uC7K1XxtH2HTQZmxt/yTnyhwQM8kiHur1pLZqqc6qCVvSQicswlAzWvV6 ZyKcCnSkKfWNpLRGqwM3ptoqX3UipVwRbXm/+vgJmuT7Rva8DCPMdsGcutcCFfn5BkKP 7VQl/p7fy27Yf/JIfNF0f4KfM1H/mRc6TzJfDrzNVgXgz27qei9GDn1eMzhvYd00Azrj QsTTZOxYUHBnx9XI2zBsuNvqVct7KI4Gm90hR8bZwFOKiPrkT4LlCy3T3NI3X1bhGjda daWU3IJzCEmf0pDM1OLHs2Gvgz/eT9fmySMji/9P8Gqme2DbTxOmCBCwC1wZBwVkEX6s pmbg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793990684; x=1794595484; h=subject:from:content-transfer-encoding:content-type:mime-version :references:in-reply-to:message-id:date:cc:to:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=gjNfjsSizwa4gQFPlTmLJDdH/oRSAXvuLXcNmAdoiyU=; b=wIh53LKU1GGSeK4dwmcKi9h1Z0Sscex7DoK/ZtpbnniI16/ddjg6I90wW3pUMOnbGT LR/7pHn7SBPFdOqC5o+hM0qsLmkWBDa3ylr328SxhfXX8QGCrcEoV1fOksKw6QS9dJ71 LkTg9WTDITf7VlZVgFWdUdyHfPylqvliQKQPI10qE+++eUwRB8zlSxIyobrlbnlO7dXB 8kuKOTKiQ2X+m9U8Npl7z/TbZj6f/fWHRrwPiGNBvLc+0G2ylnODjQ87SQGoOz1WuNDk II07fPlhMlVIbc9wXrTze8l2HpdtP2qPBfEIJ1dwPGKaXqlEf5r8mfHLaqlIi4KsHPw6 C5lA== X-Gm-Message-State: APuU4Z9ANSqh6mAvaNb5YW4wzlugO26LVJCQmHuGwe3I9DkSnHKhHiAu nzvMc5Yuq7lyKlszbe12EA776v4h947x4ixuAOLFPok2IxRXI3nQ1EDXBb412g== X-Gm-Gg: AYBFou04SVKFPnZyF+/KkjFUX+ooa8aoTZ6gShfEXjsYXPmfMDsvKuCpimdyBYN3OgF 74l7T8oi4z6/maWC3T93Qtjhr3nyEJOezfq1ONwU9ELGAqyHXXS473ziVr/8f6b9uuwJNzU+4qf eJhslLvu0YOosBQcWJOtZP6mCVg3co0JJIZtGSVZT4TWpwHWeV6Pb2ceT4VdAebcX16+Oc1C67K ZrCClnYTXrCceO7iAlHw3cCA0kzJTAeyXkWDwzGqxS7MKGidl8JFTct+sMqQYMpXnyvmm5U8SBq BTF+C/SxmAUCrKJjoLjfrahMntQ9PyrEIg90KQeTRNa5bW9bpjGI56VhPa1WXtLILtWmtELn7VF oOXpep5PbZlFl+bI9gftFHB4P9WtxJx/22/gadiAB8OWx+Z0HYasc0STK7uC3eA8bo0ksl4Rr3c q4dJyLL4GSj4FbxBLIeBL+pfcwbJ/SKqFV90ccg1NE/8Ql99jQeJaklgqHYbILo7glij45b8D6i RO4bXeiWhshPOR91G7PU0G+TeYRt77/zosVvrRA2t5zpVNC0UMdViYlFK+ToAPaYTRkAYXHEOvK x0tvdV/q+XRWqj1+nVxvdJ54qKrEQKmXz+BClGiYArPjNf3+8fyRos9Q4URnHcMySPb/Z6fKFA= = X-Received: by 2002:a05:600c:1d05:b0:4a1:e250:b191 with SMTP id 5b1f17b1804b1-4b6de4a6da1mr54440215e9.17.1793990683827; Fri, 06 Nov 2026 10:44:43 -0800 (PST) Received: from [127.0.1.1] ([212.23.215.93]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4b79502652csm5012725e9.4.2026.11.06.10.44.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 06 Nov 2026 10:44:43 -0800 (PST) To: gdb-patches@sourceware.org Cc: Tom Tromey Date: Fri, 06 Nov 2026 18:44:27 +0000 Message-ID: In-Reply-To: References: <179373584304.213326.6133225504256345073@gmail.com> <87pkwjxcwe.fsf@tromey.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From: Josip Seketa Subject: [PATCH v2 1/2] gdb: watch a serial's error fd while waiting for input X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org 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 #ifdef USE_WIN32API #include #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 + (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