From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id qF0cM4ILXGgDnx4AWB0awg (envelope-from ) for ; Wed, 25 Jun 2025 10:45:22 -0400 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=QCXUy4ZG; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id CD3881E11E; Wed, 25 Jun 2025 10:45:22 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-10.1 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, RCVD_IN_VALIDITY_RPBL,RCVD_IN_VALIDITY_SAFE autolearn=ham autolearn_force=no version=4.0.1 Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (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 E29311E0C2 for ; Wed, 25 Jun 2025 10:45:21 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8E1693857007 for ; Wed, 25 Jun 2025 14:45:21 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8E1693857007 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=QCXUy4ZG Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id 4DB69385701B for ; Wed, 25 Jun 2025 14:43:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4DB69385701B 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 4DB69385701B Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1750862603; cv=none; b=Ow5cCagzbpbZTi9qr+G1JQZWOW/I2rUwCyw/RcyUTsQbEZ9WfXj8ixp3SeAgbq13rENgdFPTOAJgvlromP3gtfYta9LFA5rHHnIbYwh9G5UIdifp5FcAcc4Jstpv8jHX/nOCfqVRiOOmcDl6Z8f8bcBoJboSy5lTqgn/Es0/kWE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1750862603; c=relaxed/simple; bh=dP4Z/YKRJ4aUam4ZslW6CEqIP6xGyoZ7F4XNRCCXdNY=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=p9k9HG6VwgbtmCmgtycNNFzwoalR03kOoBwKIC0KXlvIyA5yFOi+ezG2ti62bbonyo0GFpg6fkMKOUSSFnM4H3IGk37ESk97ipZaQ5d/mRbIjp8UKc1U2lJmPQz7GjXsgw7wg61OKh2QbCElMb/JZpkrSgIsKNgWKPv9ounPwhs= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1750862586; 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: in-reply-to:in-reply-to:references:references; bh=XaLKJjXm0yamGzhhQJuQ3ke7kwO0qjhl3ztAI9ZB1Ww=; b=QCXUy4ZGL9xqRaS88tvnAeAKOjmvXrWoIWgisT/NJnVZfNxddLbeophwenLT6eaTDj0pwI yrd5OdWPuTWLHKVEELIv3RnJG4ZSQlucOMsRmm3nF/OrS5w4glpsCKJzbCpoMAitlKbm/I XAA10mzRROOXM2EYGXuNaNOBUgD3Slo= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-502-Q1iN1jViObaQHk-tsC4ZDQ-1; Wed, 25 Jun 2025 10:43:04 -0400 X-MC-Unique: Q1iN1jViObaQHk-tsC4ZDQ-1 X-Mimecast-MFC-AGG-ID: Q1iN1jViObaQHk-tsC4ZDQ_1750862583 Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-450d244bfabso53020715e9.0 for ; Wed, 25 Jun 2025 07:43:04 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750862583; x=1751467383; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=XaLKJjXm0yamGzhhQJuQ3ke7kwO0qjhl3ztAI9ZB1Ww=; b=YGymBYVOjNnPdHM3jgrYf+SPv9Jxi4C83GazV1NByvJEXdhJ4LHhJlLu4+JwSHq/34 fQe3gtkgRDpb1jtEK/AX1gd9JMKxEFwtQ14ii4vlThQKSxKEfpRT4aV9re/Ozewcjfks 0pn4v1J9bk7kkVZmxoe67VC+Mxm2fzr6/rcrocny8RYe+1cKERPIA4kuB+OiXHJywYBA 8KHYc9iBRCE79KV+BKYxOCqnq8P36XcNpRLevZuktJKnkWkgOi2Eg3EA+IHhwb4RfDnZ pOc7mJcZTlWosfT28cFkTGaebngWjGTLk92uAIUqVpzuT5dI9eW4DVFun/5rSycCbgg9 uCwQ== X-Gm-Message-State: AOJu0YxJVk1tuGsQJzT2LpUOKme7laiXkGZXTjxhtdeg7MV6D6ZosWcA z+1baJvV7GFylrtXnF4bDIXesCFAn7QXdvzZxKvt7RhE9U+1I45xl/FubNYYt4s2GZgw/eMcozR YwbX8PfR/WXmQsS03EmcVxR4Z+ZxzyZ2aGj0qCifn/FbpkL9MmiagD24ETa43HBmvl+AdXMQbYj 5cheQhvWWNESK3geMJMkA6yUeb8tyUcBvV+FfXC1BgrGwE4CU= X-Gm-Gg: ASbGncvQJi+ssGAqTA8rnohYmCExMAc8vQAicB6dDedHamkffcgiduD2oGGzx/F8TbP IzUYZAH0jYDgqeVfo8iZvbeZsDPnYz5/Mcii5EoWgyD9wQgmhb1qO+C2XVkOTdt1oS3Kiz5z92p 8ahCsUfjEMiEbjdwUwbTYg6Dd6O6g28I2Ccc/jdGcZtU36Trwgqj41DeC939hg5d8ZyFngHFvoE 7muFn/c1Qz0dbYa8GZ85qIGgpV/7qi+Ecn5rEoqry53eckRzVHmj13YPOCw5dPAKuKoGU9BaNOI bXDISzXC+aLNgQHGv4HY5p9yO6p5g0XBe3bnE/cv+w1t1Eo= X-Received: by 2002:a05:600c:1d92:b0:450:d104:29eb with SMTP id 5b1f17b1804b1-45381aa5656mr33560995e9.5.1750862582778; Wed, 25 Jun 2025 07:43:02 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFJfsgtOyDyWPTkwpIDE6eq302t8O2j73eUKVMWwCXnllxBa38Hf59cdqJUkjZOR0ov3hJ3ag== X-Received: by 2002:a05:600c:1d92:b0:450:d104:29eb with SMTP id 5b1f17b1804b1-45381aa5656mr33560465e9.5.1750862581938; Wed, 25 Jun 2025 07:43:01 -0700 (PDT) Received: from localhost (75.226.159.143.dyn.plus.net. [143.159.226.75]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-45382349548sm22314125e9.10.2025.06.25.07.43.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Jun 2025 07:43:01 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCHv5 1/4] gdb: allow gdb.Color to work correctly with pagination Date: Wed, 25 Jun 2025 15:42:54 +0100 Message-ID: X-Mailer: git-send-email 2.47.1 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: HuWlZmI1Onr4THlwtQj1JgnBMLFMAjsjTSMgsVHmQhQ_1750862583 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 This commit allows gdb.Color objects to be used to style output from GDB commands written in Python, and the styled output should work correctly with pagination. There are two parts to fixing this: First, GDB needs to be able to track the currently applied style within the page_file class. This means that style changes need to be achieved with calls to pager_file::emit_style_escape. Now usually, GDB does this by calling something like fprintf_styled, which takes care to apply the style for us. However, that's not really an option here as a gdb.Color isn't a full style, and as the gdb.Color object is designed to be converted directly into escape sequences that can then be printed, we really need a solution that works with this approach. However pager_file::puts already has code in place to handle escape sequences. Right now all this code does is spot the escape sequence and append it to the m_wrap_buffer. But in this commit I propose that we go one step further, parse the escape sequence back into a ui_file_style object in pager_file::puts, and then we can call pager_file::emit_style_escape. If the parsing doesn't work then we can just add the escape sequence to m_wrap_buffer as we did before. But wait, how can this work if a gdb.Color isn't a full style? Turns out that's not a problem. We only ever emit the escape sequence for those parts of a style that need changing, so a full style that sets the foreground color will emit the same escape sequence as a gdb.Color for the foreground. When we convert the escape sequence back into a ui_file_style, then we get a style with everything set to default, except the foreground color. I had hoped that this would be all that was needed. But unfortunately this doesn't work because of the second problem... ... the implementation of the Python function gdb.write() calls gdb_printf(), which calls gdb_vprintf(), which calls ui_file::vprintf, which calls ui_out::vmessage, which calls ui_out::call_do_message, and finally we reach cli_ui_out::do_message. This final do_message function does this: ui_file *stream = m_streams.back (); stream->emit_style_escape (style); stream->puts (str.c_str ()); stream->emit_style_escape (ui_file_style ()); If we imagine the case where we are emitting a style, triggered from Python like this: gdb.write(gdb.Color('red').escape_sequence(True)) the STYLE in this case will be the default ui_file_style(), and STR will hold the escape sequence we are writing. After the first change, where pager_file::puts now calls pager_file::emit_style_escape, the current style of STREAM will have been updated. But this means that the final emit_style_escape will now restore the default style. The fix for this is to avoid using the high level gdb_printf from gdb.write(), and instead use gdb_puts instead. The gdb_puts function doesn't restore the default style, which means our style modification survives. There's a new test included. This test includes what appears like a pointless extra loop (looping over a single value), but later commits in this series will add more values to this list. --- gdb/python/python.c | 18 +-- .../gdb.python/py-color-pagination.exp | 116 ++++++++++++++++++ .../gdb.python/py-color-pagination.py | 46 +++++++ gdb/utils.c | 21 +++- 4 files changed, 189 insertions(+), 12 deletions(-) create mode 100644 gdb/testsuite/gdb.python/py-color-pagination.exp create mode 100644 gdb/testsuite/gdb.python/py-color-pagination.py diff --git a/gdb/python/python.c b/gdb/python/python.c index ff50c424ad9..dddf5c39c58 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -1570,21 +1570,21 @@ gdbpy_write (PyObject *self, PyObject *args, PyObject *kw) try { + ui_file *stream; switch (stream_type) { case 1: - { - gdb_printf (gdb_stderr, "%s", arg); - break; - } + stream = gdb_stderr; + break; case 2: - { - gdb_printf (gdb_stdlog, "%s", arg); - break; - } + stream = gdb_stdlog; + break; default: - gdb_printf (gdb_stdout, "%s", arg); + stream = gdb_stdout; + break; } + + gdb_puts (arg, stream); } catch (const gdb_exception &except) { diff --git a/gdb/testsuite/gdb.python/py-color-pagination.exp b/gdb/testsuite/gdb.python/py-color-pagination.exp new file mode 100644 index 00000000000..cb410da2274 --- /dev/null +++ b/gdb/testsuite/gdb.python/py-color-pagination.exp @@ -0,0 +1,116 @@ +# Copyright (C) 2025 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 gdb.Color and how this +# interacts with GDB's pagination system. + +load_lib gdb-python.exp + +require allow_python_tests + +standard_testfile + +set pyfile [gdb_remote_download host ${srcdir}/${subdir}/${testfile}.py] + +set str "<[string repeat - 78]>" + +set black "(?:\033\\\[30;49;22;27m)" +set red "(?:\033\\\[31;49;22;27m)" +set green "(?:\033\\\[32;49;22;27m)" +set yellow "(?:\033\\\[33;49;22;27m)" +set blue "(?:\033\\\[34;49;22;27m)" +set magenta "(?:\033\\\[35;49;22;27m)" +set cyan "(?:\033\\\[36;49;22;27m)" +set white "(?:\033\\\[37;49;22;27m)" + +set any_color "(?:${black}|${red}|${green}|${yellow}|${blue}|${magenta}|${cyan}|${white})" + +# Run the command 'TYPE-fill MODE' which fills the screen with output and +# triggers the pagination prompt. Check that styling is applied correctly +# to the output. +proc test_pagination { type mode } { + + # Start with a fresh GDB, but enable color support. + with_ansi_styling_terminal { + clean_restart + } + + gdb_test_no_output "source $::pyfile" "source the script" + + gdb_test_no_output "set width 80" + gdb_test_no_output "set height 15" + + set saw_bad_color_handling false + set expected_restore_color "" + set last_color "" + gdb_test_multiple "$type-fill $mode" "" { + -re "^$type-fill $mode\r\n" { + exp_continue + } + + -re "^(${::any_color}?)(${::any_color})$::str" { + # After a continuation prompt GDB will restore the previous + # color, and then we immediately switch to a new color. + set restored_color $expect_out(1,string) + if { $restored_color ne "" + && $restored_color ne $expected_restore_color } { + set saw_bad_color_handling true + } + set last_color $expect_out(2,string) + exp_continue + } + + -re "^\033\\\[${::decimal}m$::str" { + # This catches the case where the color's escape sequence has + # not been converted back into a full style. This indicates + # something went wrong in the pager_file::puts function. + set saw_bad_color_handling true + exp_continue + } + + -re "^((?:\033\\\[m)?)$::pagination_prompt$" { + # After a pagination prompt we expect GDB to restore the last + # color. + set expected_restore_color $last_color + + # If we didn't see a color reset sequence then the pagination + # prompt will have been printed in the wrong color, this is a + # GDB bug. + set color_reset $expect_out(1,string) + if { $color_reset eq "" } { + set saw_bad_color_handling true + } + + # Send '\n' to view more output. + send_gdb "\n" + exp_continue + } + + -re "^\r\n" { + # The matches the newline sent to the continuation prompt. + exp_continue + } + + -re "^\033\\\[m\r\n$::gdb_prompt $" { + gdb_assert { !$saw_bad_color_handling } $gdb_test_name + } + } +} + +foreach_with_prefix type { color } { + foreach_with_prefix mode { write print } { + test_pagination $type $mode + } +} diff --git a/gdb/testsuite/gdb.python/py-color-pagination.py b/gdb/testsuite/gdb.python/py-color-pagination.py new file mode 100644 index 00000000000..efd501eedf5 --- /dev/null +++ b/gdb/testsuite/gdb.python/py-color-pagination.py @@ -0,0 +1,46 @@ +# Copyright (C) 2025 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 . + +import gdb + +basic_colors = ["black", "red", "green", "yellow", "blue", "magenta", "cyan", "white"] + + +def write(mode, text): + if mode == "write": + gdb.write(text) + else: + print(text, end="") + + +class ColorTester(gdb.Command): + def __init__(self): + super().__init__("color-fill", gdb.COMMAND_USER) + + def invoke(self, args, from_tty): + mode = args + str = "<" + "-" * 78 + ">" + for i in range(0, 20): + for color_name in basic_colors: + c = gdb.Color(color_name) + write(mode, c.escape_sequence(True)) + write(mode, str) + + default = gdb.Color("none") + write(mode, default.escape_sequence(True)) + write(mode, "\n") + + +ColorTester() diff --git a/gdb/utils.c b/gdb/utils.c index 6ae362c5400..d4407d43163 100644 --- a/gdb/utils.c +++ b/gdb/utils.c @@ -1702,10 +1702,25 @@ pager_file::puts (const char *linebuffer) else if (*linebuffer == '\033' && skip_ansi_escape (linebuffer, &skip_bytes)) { - m_wrap_buffer.append (linebuffer, skip_bytes); - /* Note that we don't consider this a character, so we + /* We don't consider escape sequences as characters, so we don't increment chars_printed here. */ - linebuffer += skip_bytes; + + size_t style_len; + ui_file_style style; + if (style.parse (linebuffer, &style_len) + && style_len <= skip_bytes) + { + this->emit_style_escape (style); + + linebuffer += style_len; + skip_bytes -= style_len; + } + + if (skip_bytes > 0) + { + m_wrap_buffer.append (linebuffer, skip_bytes); + linebuffer += skip_bytes; + } } else if (*linebuffer == '\r') { -- 2.47.1