From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id YZMtL0kg7mrDuSkAWB0awg (envelope-from ) for ; Fri, 06 Nov 2026 13:45:29 -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=p7AiX0zS; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id B5DB61E09A; Fri, 06 Nov 2026 13:45:29 -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 D64751E043 for ; Fri, 06 Nov 2026 13:45:27 -0500 (EST) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6DC334BA2E09 for ; Fri, 6 Nov 2026 18:45:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6DC334BA2E09 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=p7AiX0zS Received: from mail-wm1-x32c.google.com (mail-wm1-x32c.google.com [IPv6:2a00:1450:4864:20::32c]) by sourceware.org (Postfix) with ESMTPS id E85C04BA2E27 for ; Fri, 6 Nov 2026 18:45:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E85C04BA2E27 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 E85C04BA2E27 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::32c ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1793990703; cv=none; b=kgyd9TL524OHLTuqL2eV4dDLfP0RDNc5R1kijHTvsSHcU4dPJ0BDnav/YqmALdT78UhEh8f6FKtk4yb8OAjaWb+whzOw8OBZhsjBOgexvdBrwOXu58Z7UsHsHylD2eCp1BpjhXlHDK0i2KQTdzlHHzer2G53N8/uXD5pXLl/9/E= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1793990703; c=relaxed/simple; bh=Kt7eZQI8SvmwUxm9CLRJmgp0PdREsEuVhlx5FN9P+lY=; h=DKIM-Signature:To:Date:Message-ID:MIME-Version:From:Subject; b=xjuO7LmKBACQZTPQp1kDpjkkquEl4Pm1tefEjOnUDOeaNfdKaOUZ2LbcWsBFUzbvMMrrPw2KdMgqN0gOt1PpUDsnszBJXwEqRgkazBsFNOG5/HJGlrnbbDtdu6dSfu/3Ts/t2gL3BObcftvRazUSqjV13TUXmeu4wqyaI6Y4pdg= 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=p7AiX0zS DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E85C04BA2E27 Received: by mail-wm1-x32c.google.com with SMTP id 5b1f17b1804b1-4a20bdaf39cso10779265e9.1 for ; Fri, 06 Nov 2026 10:45:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1793990702; x=1794595502; 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=elnh+yjcIzhuKHcD1B6khwYtcoR+h29Vhbkse2ngcTU=; b=p7AiX0zSAUcG0ny+QI7uA6cF8DUL1AXB3Xlkg/q/w3HJTsuU7wEDiYH2mTz9tIzS+G RS/6YQwsKKe1l3+NL7rWMO+Hppa10PG7lWX3WptP4TP2UdxUUSHTuUw9mAFtMjbogeG0 8jEGN2ys1DBP2N3bTakqShfkdhno/tPV5zgR5Y7MuotujkPtPa0OhyfFFnzyJp/aY++J 2BTzKP0sZv129USsdbQL4wWfCFXPKawhxZZLRRpv3lIrrW9RioclIPLW3BsL5lpcT3mx m4Z1GDiI6NUqlC/55vaCtkLItK6GRneW0jGmSHPEaPpk5evUAwa6soIJzXHqP5m2OGa7 WdQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793990702; x=1794595502; 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=elnh+yjcIzhuKHcD1B6khwYtcoR+h29Vhbkse2ngcTU=; b=h0YxQGnZHGm1OGHfn/3/bSh8IrCPmJvSmyVReEeoj+AAVv8E/HkaZJy0Z/Aaefqtm2 JnPODu+QVPJ/BZBpTh2Bh1WWz56JxofilvK+pFCd9DqpUJ/ZNJYUN1hM+eBe7yEM4GXq 8KBxycmn7FXmG0+49SUtUZqoX6qDZCt67TdncLnvhl+vYGS3NdwUaWABMTk2FPme+8B0 O+07g/eQkMJmABoTWiKL2K0vCYfc2sn6mDug54z8qAgOgtKZbHBrAGiAoA1HA/lLqTlQ bGLQuuXDKqG4sqeFM8Bg/0mbxlG6ocNtAZizl/p5nb4yRT8x3i5AV+9gGdJYC15fmBXk /jmw== X-Gm-Message-State: APuU4Z/tvrFBxqbX0HWnaqX9/wDglC4yXYuyqhmN26+5XfXRXKapnmuG CVHJzWMA2hMQ5BHge7VY6KPzCyFYmeZHbkmsF95MJEVRmL1One/MKJ2SbQJhyQ== X-Gm-Gg: AYBFou2dAGxghiEsKfKYhuedlX0hJjw0E9BAqI4BwLy59tiCh9q5r6SPbSU52h9qzN9 zsmjpITOFTJktM2WcdBQUWFCTwUZ14aDFy8dfZY7lYptuwihayAA0aumrbBCUSQteOfGB9c182/ 2L4bxi1UPOsNE/USRXqEc1l0ql26NvAf1oMdkc7IfAhHMK6UMNQtk+bSkNQnPpQ7GdfD/rLpD3m lTNxSD9WZ34dzGzOUv2qyZ57JgM1CCHj6k4OUIfyugxmfc2fI0wsqnt4gYyRTKfYni57A/6XES0 FYbajEozj7ICULi+spixnH6IN+XmxD2PLq+N6mvUseYVj7ELMouOp72sH/L8bN2Ff7D15UsxkC+ YYEbusK6Bv2oSCT5PKZkn5ObfUV3B4EnnaVS7nXeFkg2Rn/jCF8JDlKjK91fpevev2TSjbx5xZH P0gzk0ICOq03TgPZk26tfxwuBSfOfHBDwRkFEoWN6XnlqW0RYLtvNsIYWau6UJD1UfbNRqKPanI 5nywipeYlbsy834NZHq7DeQQYb8VsDt5rFTXc0sK9tF6slGgpMhjpCEmJnbvGNsONXWKl1pGxHn cJE23LerE1OeqJqor45Czgl8AXvxgpnzIZfSTmdpHeUxBO6ny1Kq X-Received: by 2002:a05:600c:8718:b0:4a1:ddaa:571e with SMTP id 5b1f17b1804b1-4b6ddcf599fmr62067965e9.15.1793990701680; Fri, 06 Nov 2026 10:45:01 -0800 (PST) Received: from [127.0.1.1] ([212.23.215.93]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4b57203a98fsm137567925e9.4.2026.11.06.10.45.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 06 Nov 2026 10:45:01 -0800 (PST) To: gdb-patches@sourceware.org Cc: Tom Tromey Date: Fri, 06 Nov 2026 18:44:44 +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 2/2] 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, or by ser_base_wait_for when GDB next waits for input, or by handle_error_fd in the event loop. TCP and serial connections have no error fd and are unchanged. Also add a comment listing where the error fd is read, and why. --- gdb/ser-base.c | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/gdb/ser-base.c b/gdb/ser-base.c index fe84bf034..ca92d05fc 100644 --- a/gdb/ser-base.c +++ b/gdb/ser-base.c @@ -295,7 +295,24 @@ ser_base_wait_for (struct serial *scb, int timeout) static constexpr size_t ERROR_FD_READ_MAX = 1024 * 1024; -/* See ser-base.h. */ +/* See ser-base.h. + + For a pipe, the error fd is the command's stderr. GDB has to keep + reading it while it reads the input: a command that fills it blocks, + and then the input GDB is waiting for does not come. It is read: + + - by handle_error_fd, while the event loop runs; + + - by ser_base_wait_for, while GDB is blocked waiting for input, as + in remote.c's readchar, when the event loop does not run; + + - by do_ser_base_readchar, for serials ser_base_wait_for leaves it + to, every second of such a wait; + + - by generic_readchar, once for each chunk of input, so that error + output written before the chunk is shown before it is used; + + - by pipe_close, once the command has exited, for what is left. */ void ser_base_read_error_fd (struct serial *scb, int close_fd) @@ -420,7 +437,9 @@ do_ser_base_readchar (struct serial *scb, int timeout) /* We also need to check and consume the stderr because it could come before the stdout for some stubs. If we just sit and wait - for stdout, we would hit a deadlock for that case. */ + for stdout, we would hit a deadlock for that case. This is for + the serials ser_base_wait_for leaves it to; for the others, it + has done it already. */ ser_base_read_error_fd (scb, 0); } @@ -467,6 +486,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; @@ -497,8 +521,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