From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id 5DnOE9s86mojWgwAWB0awg (envelope-from ) for ; Tue, 03 Nov 2026 14:58:19 -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=SramytaB; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 41122378002; Tue, 03 Nov 2026 14:58:19 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-5.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 autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [IPv6:2620:52:6:3111::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 8FA2C1E043 for ; Tue, 03 Nov 2026 14:58:18 -0500 (EST) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 93D5A4BA9015 for ; Tue, 3 Nov 2026 19:58:16 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 93D5A4BA9015 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=SramytaB Received: from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com [IPv6:2a00:1450:4864:20::42d]) by sourceware.org (Postfix) with ESMTPS id EB2684BA23EA for ; Tue, 3 Nov 2026 19:57:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EB2684BA23EA 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 EB2684BA23EA Authentication-Results: sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::42d ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1793735871; cv=none; b=Agk3weroTOB8btSwcRrsb78pS8oYCPhbuvdWTYDQ6LxsBYoIDXZyKsBV8VjQNXVcqGlUhoVkmV/ZCAMfWDCy4BiIKHwOy4ZezPaauUKea/ecUBj9K/U4VnSdzz7t//eNj5dlBxmR0I5ExLrgaBsvY6ah8i1hoEXbwQMBMTcBvRY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1793735871; c=relaxed/simple; bh=WxLgMr5c6GGt/GoeyH6wD0/f9tOlcNcWxoCw9mXI3c4=; h=DKIM-Signature:To:Date:Message-ID:MIME-Version:From:Subject; b=w8zapeUfMZAanpttXTFD385EqWKlzLGmn7h5UO8Xd0RIIZnrnQ3gqYw1ZFt6yabBkYZp5Xu+duA9UHntOK3SigUx96u1A032Mis8otOhTF/7t3lO4n7UiNrR6BOjzAP795+RVqV7vL6JdMlTZANWN29w1FQSYgHLctqyI/ghCMc= 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=SramytaB DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EB2684BA23EA Received: by mail-wr1-x42d.google.com with SMTP id ffacd0b85a97d-48df8fe8513so3971401f8f.0 for ; Tue, 03 Nov 2026 11:57:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1793735870; x=1794340670; darn=sourceware.org; h=subject:from:content-transfer-encoding:content-type:mime-version :message-id:date:to:from:to:cc:subject:date:message-id:reply-to :content-type; bh=Xfrr1vptJ5SpZF07ropX0qYpT2CF9orhYvZfDj0K060=; b=SramytaBEoEg1aigbi/S0Nz6P4/zABCPa9TEkFZk/bDZs3v0BVHrqgVzqMk5erhO1o u0dRnTldP17xPuWXvn3m4GRviGTVuswFaafOYKj1WqyKdb6tRxPuDpMwkAdI2MwYOvJU 6nayBCRw0agfPbE31UyY25kFofoxrRPtQsOVKMeJNDnxlrRGdUNVMxaAgtakNkHoU+s/ SSaBSEiIsOCXCXARtxEOsZCthMe0SP4aYLOXPf+x9EwiYxDaCHdshPjQZVrVfppkgZ6C SMqwLhFRVLEYfH/aT6e/3mmB1UgjnV4F5TBO3egdz89g8ueRI1bq7oe5oTpBRuFXTolT 6eGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793735870; x=1794340670; h=subject:from:content-transfer-encoding:content-type:mime-version :message-id:date:to:x-gm-gg:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to:content-type; bh=Xfrr1vptJ5SpZF07ropX0qYpT2CF9orhYvZfDj0K060=; b=2jqODe11IF05kKQzu4L3+oh7MaGFtfvQiv+r+D1YxorFj/JqM/T+49N+GgQZ45511b Yl5fT9A3pJ5XxXYCS/fm9tAyJtUmxJVgOtBeXObAW+eZ2ta1eBUoOB7E07avGNahEhlf s4sR9i709QXsUm/C/flTKpCyw0u2yRemsTPO4ec7JtMjrBDIF4uY07En+BkCehuY/8wm sWtTt77MP7CYe3uQG733TURTeNaJSbOhhn4dmU81mwr+cD8Tw6ni0bB6nyKxmmZQrNUx 4G/EwIuowzVPKQyKZZZdZIcn3hUApvT7CeYUloRx2mH8SjFgy7HlpZkxBPcizboAabXh wckQ== X-Gm-Message-State: APuU4Z8xSfq5o5plDN2VhV/ISAh6MbEb+meT0fZwAoIpz4JTcywFTChT bGbEow9KxLuFk10rFOFYRD1KccG7PLQ6AvxKVHA5jHBE52/q9DtdvL47h90noA== X-Gm-Gg: AYBFou2h6PAOAqoAx1zu78hqtzJfIO+XFNBi6xeT/5FpK5zlBN/nkgv3/rOzwK0zi9z 5j3uz1VpdO64omyEMsGqjOvPYShTsTV1Ii0TPNxMBHsU1counxtkI9EPJBHfwdXnIAf9hoscMni bJWoULIr7qJ4+O2w/C5HF/ib2GeBgvNsJMHd2JyGB+KwgJkOls/J+TbmaRTJuqbHOkCV9A9csMf TtisDsh8WbcvlHfKXzQSVMIs1OUACXFAA4o0eDEnL+9qhnPainMqzKpNw/qc6RYtNswnPDF51ZC Uwc9fRmX8Xj0sQuVT4nL/XAHBSGeAGBFy3WsB15+1Db5jgAWAAQCxe6dMabZBq/L84A/lJHOfT3 njI5EWcu5lJUyAKYn1E4sJLqR7A6yTWCfSLLAHmqfs0VR90RYfP4i8tbdsF2G40vYEg01c2S2Os VcnqiozFhnOvw2MhPFsV8LM0tzDApjtDTrsZcXM0xzxLZQ3lIIcE4xkrVA8QbUYIVuW0lrw3Ag0 krCNkp39GvGf1nqEu0jNWzTkkVnE6Cmy7LAo6BlDGywLuBDDJKW5rVCLIDz6YOnNiqK9eIRSJsC ZglbTxYGnJ/bVZvnlXQ1froTSZp7ELo4Mt6Ri3DQSEJKeItFXI4i X-Received: by 2002:a05:6000:2611:b0:48e:4e00:b72f with SMTP id ffacd0b85a97d-48e6e50c8ecmr30087517f8f.32.1793735869637; Tue, 03 Nov 2026 11:57:49 -0800 (PST) Received: from [127.0.1.1] ([187.40.49.159]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-48fcd03d833sm284069f8f.48.2026.11.03.11.57.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Nov 2026 11:57:49 -0800 (PST) To: gdb-patches@sourceware.org Date: Tue, 03 Nov 2026 19:57:23 +0000 Message-ID: <179373584304.213326.6133225504256345073@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From: Josip Seketa Subject: [PATCH] gdb: read a serial's error fd once per input chunk, not per character 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 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