From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id fVxyNnHKaGlhogQAWB0awg (envelope-from ) for ; Thu, 15 Jan 2026 06:07:29 -0500 Authentication-Results: simark.ca; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=ESR8BwHD; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id D2AE61E070; Thu, 15 Jan 2026 06:07: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=-3.4 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,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 174051E070 for ; Thu, 15 Jan 2026 06:07:29 -0500 (EST) Received: from vm01.sourceware.org (localhost [127.0.0.1]) by sourceware.org (Postfix) with ESMTP id 712B84BA2E1C for ; Thu, 15 Jan 2026 11:07:22 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 712B84BA2E1C Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=ESR8BwHD Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id B8EF54BA2E07 for ; Thu, 15 Jan 2026 11:06:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B8EF54BA2E07 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org B8EF54BA2E07 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1768475216; cv=none; b=hUmHGkG9UZLRwIb1ZNmpi9IeCPgYjieiqSIN9jnuVf1An+QfkLUe/NWaQ9ZKhbvl9TRQZDn9fxwF+AVBtwEiK4aY7r9dwN28CjczCdajRl0gfjaRcJ/VH1Ng+9o+OpF+8AiDEy9Ls4wTN19MResqp3VuJ1h/E0m41LsLXIVetZg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1768475216; c=relaxed/simple; bh=Xd9cgF8op2Lpm/i4b+9xCXdHq0GxtL3OrJf2DeCYWyk=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=r66M4QT53c1rOrL3YhAJ2a5rUxd62HBQ+L9FReMzHakj2UcKMvPMdAI90KD7F8IFEIEbiLULPR92Iihc24nXyVEHR75gJ5+HFMWsvD5vEpV00QoRYF7sRT1cEJBrfksKq3KgkNoaFIOfk5u34TK0s5FbS0NYx7L+hTpjIWX4Q9A= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B8EF54BA2E07 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1768475216; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=mMzXPmf74+ldXMNQX2WDccNVLTsU9UkHyXLi6YMF4e0=; b=ESR8BwHDK3BwtgKbhx24A4KUtalH2QsSC4kr6NgMFAzrSjigZg4RDB+OWJCUSs3a5S/eUM 7qWEbdFbbfzgSmOMpUPPGDGaR/ReqYKIOStcrj2tb/1RWsbsErtGmNTnyZ/yhXzZduJRVA H9Xc6UHsYlgujZ5AxKHe9xAvk5PuOcI= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-319-osXfcL98Ouag9Is1UM6BNw-1; Thu, 15 Jan 2026 06:06:55 -0500 X-MC-Unique: osXfcL98Ouag9Is1UM6BNw-1 X-Mimecast-MFC-AGG-ID: osXfcL98Ouag9Is1UM6BNw_1768475214 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-4801bceb317so3141825e9.1 for ; Thu, 15 Jan 2026 03:06:55 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768475214; x=1769080014; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=mMzXPmf74+ldXMNQX2WDccNVLTsU9UkHyXLi6YMF4e0=; b=nu+PHZfoo355C0r620y225R8p9mTrdwujmu1eUbg24xijEv2G5dBoTigeEeCXjPJMl lxCJOCVVkvH/31S9FZEQGglakg4pdV0MCZpFI4oaBKZpoikSuYv7/yQtR01c4ONXRqNy gAHjUfeMJ++4DlEdS35PAF3rD0Gzt9o+5PxZC4m1K6g30ay/2QeQMGTMvdd/GjF07ZV6 XDBYRORGmmOf+s69FBZVikbKBT/Ufl8ovgcJKHY6mDozYc5DNTtOe/vWF9un2/Ymu2Uv Rl9PIzAAr20DAc14xYxnrOO5VxkXcAOwcz0VmXfRFOaVwZXkODC/0SllOzM/ppg5WxR2 5JcA== X-Gm-Message-State: AOJu0YyeEJ/ajJOVEkgQ3U8GCfH+9UMboRPGNHM8afXO1Z38XZZv0DyG kQkBBgN93vF6OEelcqK1ESfVvzFS/Z+ANLXTwkA9LTi4/eSI/SEwYtqni46Fjm7Sl0/8114TBBQ 49ou9ZJK8UCtDLH0kkRbADPHEkgsxENJHzRgxwNyCHtZkqfVXZT5jspwdfomzFb3k+oO6UFh/FM enNVqXCg++l1eVFGk40fj9SecxpFWBmxD8IVGGMJNLy3iZ/+Y= X-Gm-Gg: AY/fxX4wWTQVIWDp6V3a/DXsAF1q1WYzo8XS6iZMhsLAFICLr2oZi9usPlTazi4EFbF gBGz4MfXXnDiNcdDrqe53Zidbii0ECs4jmG6uNxS84DH8DVbNS+EGNdF6yGjNGKYbzL4OfRHYK3 DwJj1C9x/K2I5eOXxq3AV+crT+5x+QLuTZFoMahWhlFZGV9843U1F+PFg603EEaBW0bJXd6axMn gtK/4Yy+J6VtmIlbthJAE8E/+09Ki5lIh4Y2As23OaSUq1uNCx3OHFzSzFEp/2hyN2eDOwNMbGn gOl31mgVCPpMcFjron/kJpbsugPYifTSkjvftGEI06xezPmSZoP95RWS0CsbUlUomFsA4VsVGPN p6R85q3G8Z7gQWiFDWejg3kj/O+gU X-Received: by 2002:a05:600c:1c20:b0:47e:e712:aa88 with SMTP id 5b1f17b1804b1-47ee712ac96mr38999075e9.31.1768475213629; Thu, 15 Jan 2026 03:06:53 -0800 (PST) X-Received: by 2002:a05:600c:1c20:b0:47e:e712:aa88 with SMTP id 5b1f17b1804b1-47ee712ac96mr38998535e9.31.1768475213099; Thu, 15 Jan 2026 03:06:53 -0800 (PST) Received: from localhost (13.81.93.209.dyn.plus.net. [209.93.81.13]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-47f428bc400sm40714675e9.7.2026.01.15.03.06.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Jan 2026 03:06:52 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess , Patryk Sondej Subject: [PATCH] gdb: fix handling of raw ANSI escape sequences printed from Python Date: Thu, 15 Jan 2026 11:06:48 +0000 Message-ID: <2b96eaf47bcb84cf955f4e59ec34c8582b5ceb81.1768475197.git.aburgess@redhat.com> X-Mailer: git-send-email 2.47.1 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: ZrMYVJZjO-Fj_URDEAGy2Nb1egKjTGK4Vzu6_sLmYdg_1768475214 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit content-type: text/plain; charset="US-ASCII"; x-default=true 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 Bug PR gdb/33748 reports a regression with print styled output from Python when using ANSI escape sequences to apply the styling. This regression was introduced by commit: commit 3825c972a636852600b47c242826313f4b9963b8 Date: Wed Jun 18 15:02:29 2025 +0100 gdb: allow gdb.Color to work correctly with pagination Prior to this commit GDB would always forward any ANSI escape sequences directly to the output stream, but this meant that GDB didn't know which style was currently in effect. The above commit changed GDB so that we would parse the ANSI escape sequence, and then apply it to the output stream, this allowed GDB to track which style was in use, which in turn meant that GDB could correctly suspend the style and reapply it when the pager was activated. The problem though is that ANSI escape sequences can be built up in parts. For example, a user can emit the sequence to change the foreground blue, and then later emit the sequence to change the text to bold. The result is output that is both blue and bold. In the above commit, when parsing an incoming ANSI sequence, GDB was always starting from the default styling state. The result of parsing the ANSI sequence was then being applied. In our above example, this meant that when parsing the sequence for bold text we would "forget" that the current style had a blue foreground color. This can be easily fixed by starting from the current style, rather than the default style. This fix was suggested by Patryk Sondej who originally reported the bug. Co-Authored-By: Patryk Sondej Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=33748 --- .../gdb.python/py-color-via-ansi-esc.exp | 70 +++++++++++++++++++ gdb/utils.c | 5 +- 2 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 gdb/testsuite/gdb.python/py-color-via-ansi-esc.exp diff --git a/gdb/testsuite/gdb.python/py-color-via-ansi-esc.exp b/gdb/testsuite/gdb.python/py-color-via-ansi-esc.exp new file mode 100644 index 00000000000..9aac552fe1a --- /dev/null +++ b/gdb/testsuite/gdb.python/py-color-via-ansi-esc.exp @@ -0,0 +1,70 @@ +# Copyright (C) 2026 Free Software Foundation, Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# This file is part of the GDB testsuite. It tests styled output from +# Python via raw ANSI escape sequences. + +load_lib gdb-python.exp + +require allow_python_tests +require {!is_remote host} + +# Start with a fresh GDB, but enable color support. +with_ansi_styling_terminal { + clean_restart +} + +# Create two variables in the parent scope called ${NAME} and +# ${NAME}_re. The variable ${NAME} will be the string for the ANSI +# escape sequence containing VALUE, and ${NAME}_re will be the regular +# expression to match that same sequence. +proc setup_ansi { name value } { + upvar 1 $name var + upvar 1 ${name}_re var_re + + set var "\\033\[${value}m" + set var_re "\033\\\[${value}m" +} + +# Different colors. +setup_ansi red 31 +setup_ansi green 32 +setup_ansi blue 34 + +# Different intensities. +setup_ansi normal 22 +setup_ansi bold 1 +setup_ansi dim 2 + +# Italic. +setup_ansi italic 3 +setup_ansi no_italic 23 + +# Underline. +setup_ansi underline 4 +setup_ansi no_underline 24 + +# Restore all settings to default. +setup_ansi default 0 + +gdb_test_no_output "python flag=\"OUTPUT=\"" + +gdb_test "python print('%s${blue}blue ${bold}blue+bold ${green}green+bold ${normal}green ${red}red${default}' % (flag))" \ + "OUTPUT=${blue_re}blue ${bold_re}blue\\+bold ${green_re}green\\+bold ${normal_re}green ${red_re}red${default_re}" \ + "foreground colors and bold" + +gdb_test "python print('%snormal ${italic}italic ${underline}italic+underline ${no_underline}italic ${no_italic}normal' % (flag))" \ + "OUTPUT=normal ${italic_re}italic ${underline_re}italic\\+underline ${no_underline_re}italic ${no_italic_re}normal" \ + "italic and underline" diff --git a/gdb/utils.c b/gdb/utils.c index 876aad1c448..8c5f34b9687 100644 --- a/gdb/utils.c +++ b/gdb/utils.c @@ -1703,8 +1703,11 @@ pager_file::puts (const char *linebuffer) /* We don't consider escape sequences as characters, so we don't increment chars_printed here. */ + /* This style sequence might not set every style attribute, + so start with the currently applied style, and update + that. */ size_t style_len; - ui_file_style style; + ui_file_style style = m_applied_style; if (style.parse (linebuffer, &style_len) && style_len <= skip_bytes) { base-commit: 141f3b0ce1a4141ec0bbd19f1c5713999113a7de -- 2.47.1