From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id aYplAqHtrmigag0AWB0awg (envelope-from ) for ; Wed, 27 Aug 2025 07:36:01 -0400 Authentication-Results: simark.ca; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=bMjM5C68; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 05BCC1E048; Wed, 27 Aug 2025 07:36:01 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-0.5 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIM_INVALID,DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_DNSWL_LOW, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED,RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED autolearn=no 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 08E881E043 for ; Wed, 27 Aug 2025 07:36:00 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 946F13843B4D for ; Wed, 27 Aug 2025 11:35:59 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 946F13843B4D Authentication-Results: sourceware.org; dkim=fail reason="signature verification failed" (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=bMjM5C68 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 6579E385275F for ; Wed, 27 Aug 2025 11:34:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6579E385275F 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 6579E385275F 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=1756294464; cv=none; b=xWqMluY9KrlJPZKFGaleNA+L9nH3CdYdq6e7oZOHXCyfHvkfEpYT7F4/c05Db/dA5v5tYUtvAiWK+9RpaNWPjEvKCzalG+ZpCZOpD2CY+aYFB5WB0jD2Z5IJfIPOHULErHVE+j0ya9K1Lrt9ELe+aBnhrMgIYks/ETzgi4MN1Ho= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1756294464; c=relaxed/simple; bh=bBnVKCxj8815PGrQRZH5bVbOX5m1iFnv4TqNhyukx7s=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=uUgVyx1g80OM1PFYJJYy4CipVnHZZKWDPcC49umWUjTOjxnRtclCHu46WA6r6WZvElPKx+dGAV8pX/cEondLMOPwTFVRLWWCxUaEyvAg5npEwSZ2wcR/6VbPh3aFoy2UybYZNn0DhQK8cV/kP7bciFDYKyIOYPMl0ef0iSb0t7c= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6579E385275F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1756294464; 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=6oLyMqggeUMKjhuxEfJGVG++3dNXPV59a2XOjyhl/f8=; b=bMjM5C68OQ1wgMl6A8v/xadkQni5rugXm1Te4iGIq4akwjfGx4Em2Y60DqCe85nO/z2FZn ZgsmemDPJR+a9iVdWFs13xiqIifYCW9ntrSiWWVZescZAJBdOhKYk1DpziExgOeh01HPc+ DZA8sOSUJzJzU/t0riE2Y/KYtSTIxSE= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-323-faY8vOKfMx-gZbwyyrm6_A-1; Wed, 27 Aug 2025 07:34:23 -0400 X-MC-Unique: faY8vOKfMx-gZbwyyrm6_A-1 X-Mimecast-MFC-AGG-ID: faY8vOKfMx-gZbwyyrm6_A_1756294462 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-45b71fe31ffso3660775e9.2 for ; Wed, 27 Aug 2025 04:34:22 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756294461; x=1756899261; 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=oXdTrWHS+uoyfi7KJiywBqRm8Bfvh1tUEXGsNGxXfcg=; b=oZnuZ+VAuUx8QyHtYpOlFnNj6VKZbxcGgC2a3lEwTDbRC93exYSKN5fp3jv5aYKYEI WkHzK3nRLAQMlF32WLIMSZaFmjEfLads24Rcyi7yQW22TZDHUVXDO4CfIl7w2IELlJng dVSjMggTJoZnjrKa1t6R88ddHx6G49bef1VGUNVfDb1DSqHrS/fXjHd4IrvZZFsMtRWf S/d0uo9Bc2+6T+Nm7m596FWkO4AaHHYTNNb6COfSyLp4twzqjcMgB8+bAq0v+0hjtBTV RAq75JNVcHvuUWJ4/riJtKz+ynPJeO8P8D2amErp15D8sXQoig40dsRs6OwK4vnZONiv gO/g== X-Gm-Message-State: AOJu0YyVNqnWnB0IGZ1G6DiWcoAo5rKRL18WGbrRZ9nh5z75715XlUj+ JohdpO6mTzM2qL6oIJho659BY2INb50Dl1/XvOhCTxoiQsfq4rp560xumQHfo11xvLpNP1hHrR0 r1/2FQMpiQ3XC3OCARxBnncmiBu72FrW1FQBQxzaOK5TbJfs5lYciwCDU3/MAu6REEewOhj9GH9 YaFZiGVXrTHeEZOZHYUmm4mq31L1rOgOZ6jGApyKV8ABUeNIE= X-Gm-Gg: ASbGnct5dvvPyin+dOuS++UXJ0V64VHdU2pYipS/d2/8/3l6nV4QQhiZfZuml7PDmIL Ly03LD7PgT5QB99O3pCpzn+ng9hl1j9grwm/tssEMZVRD1AkxyCmSQcLa9MA0Z1byf8ED/43A0L Cut0/pPN3IABiEafZjcZRqgksO7ZHY4ATkyghMwwGsQHx6gvsL8PHMtfD4MME4f54Ph8AOSTOP+ b8DXV4l4RQckeEjb40rXpjwbhjFAh59p/51ueOVVsqyk10al8kWMN+yalQKGQO73Ck0fUwP6xy6 3N51EjK9UFrNNXMw+3q28n1Z2wg1pbBOkw4= X-Received: by 2002:a05:600c:4fcd:b0:459:db71:74d7 with SMTP id 5b1f17b1804b1-45b517d40damr150181135e9.27.1756294460853; Wed, 27 Aug 2025 04:34:20 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEpyNGZfZ4DkTiT6pygCt4cbNkkvQuoMPunLXscsfOVAxPZcwcHqOYhhzbXIePAiF2MVejsqg== X-Received: by 2002:a05:600c:4fcd:b0:459:db71:74d7 with SMTP id 5b1f17b1804b1-45b517d40damr150180785e9.27.1756294460296; Wed, 27 Aug 2025 04:34:20 -0700 (PDT) Received: from localhost ([31.111.84.207]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-45b6f0c6b99sm28114385e9.4.2025.08.27.04.34.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Aug 2025 04:34:19 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess , Eli Zaretskii Subject: [PATCHv8 3/3] gdb/python: extend gdb.write to support styled output Date: Wed, 27 Aug 2025 12:34:12 +0100 Message-ID: <31de42bf45326efa2c5a58bdb96c831923971ab1.1756294307.git.aburgess@redhat.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 1 X-Mimecast-MFC-PROC-ID: gpzCBu-4edPO6L6OwEAVF4ScrHBxylI6M5D79JhUh9c_1756294462 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 It is already possible to produce styled output from Python by converting the gdb.Style to its escape code sequence, and writing that to the output stream. But this commit adds an alternative option to the mix by extending the existing gdb.write() function to accept a 'style' argument. The value of this argument can be 'None' to indicate no style change should be performed, this is the default, and matches the existing behaviour. Or the new 'style' argument can be a gdb.Style object, in which case the specified style is applied only for the string passed to gdb.write, after which the default style is re-applied. Using gdb.write with a style object more closely matches how GDB handles styling internally, and has the benefit that the user doesn't need to remember to restore the default style when they are done. Reviewed-By: Eli Zaretskii --- gdb/NEWS | 3 + gdb/doc/python.texi | 9 ++- gdb/python/py-style.c | 27 +++++++++ gdb/python/python-internal.h | 15 +++++ gdb/python/python.c | 30 ++++++++-- .../gdb.python/py-color-pagination.exp | 56 +++++++++++++++++++ .../gdb.python/py-color-pagination.py | 16 ++++++ gdb/testsuite/gdb.python/py-style.exp | 29 ++++++++++ 8 files changed, 179 insertions(+), 6 deletions(-) diff --git a/gdb/NEWS b/gdb/NEWS index 6539c917652..14b825226d0 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -148,6 +148,9 @@ info threads [-gid] [-stopped] [-running] [ID]... Use gdb.StyleParameterSet(NAME) to create 'set style NAME ...' and 'show style NAME ...' parameters. + ** The gdb.write() function now takes an additional, optional, + 'style' argument, which can be used to style the output. + ** The memory_source argument (the second argument) has been removed from gdb.disassembler.builtin_disassemble. This argument was never used by GDB, and was added by mistake. The unused argument diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi index cc033d1bccd..9bd090971a6 100644 --- a/gdb/doc/python.texi +++ b/gdb/doc/python.texi @@ -452,7 +452,7 @@ Basic Python historical compatibility. @end defun -@defun gdb.write (string @r{[}, stream@r{]}) +@defun gdb.write (string @r{[}, stream@r{]} @r{[}, style@r{]}) Print a string to @value{GDBN}'s paginated output stream. The optional @var{stream} determines the stream to print to. The default stream is @value{GDBN}'s standard output stream. Possible stream @@ -475,6 +475,13 @@ Basic Python @value{GDBN}'s log stream (@pxref{Logging Output}). @end table +The @var{style} should be a @code{gdb.Style} object (@pxref{Styles In +Python}), or @code{None} (the default). If @var{style} is @code{None} +then the current style for @var{stream} will be applied to @var{text}. +If @var{style} is a @code{gdb.Style} object, then this style is +applied to @var{text}, after which the default output style is +restored. + Writing to @code{sys.stdout} or @code{sys.stderr} will automatically call this function and will automatically direct the output to the relevant stream. diff --git a/gdb/python/py-style.c b/gdb/python/py-style.c index b3983af3a85..0e9af1fc4c4 100644 --- a/gdb/python/py-style.c +++ b/gdb/python/py-style.c @@ -353,6 +353,15 @@ stylepy_init (PyObject *self, PyObject *args, PyObject *kwargs) +/* See python-internal.h. */ + +bool +gdbpy_is_style (PyObject *obj) +{ + gdb_assert (obj != nullptr); + return PyObject_TypeCheck (obj, &style_object_type); +} + /* Return the ui_file_style for STYLEPY. If the style cannot be found, then return an empty optional, and set a Python error. */ @@ -369,6 +378,24 @@ stylepy_to_style (style_object *stylepy) return style; } +/* See python-internal.h. */ + +std::optional +gdbpy_style_object_to_ui_file_style (PyObject *obj) +{ + gdb_assert (obj != nullptr); + if (!gdbpy_is_style (obj)) + { + PyErr_Format + (PyExc_TypeError, _("argument must be a gdb.Style object, not %s."), + Py_TYPE (obj)->tp_name); + return {}; + } + + style_object *style_obj = (style_object *) obj; + return stylepy_to_style (style_obj); +} + /* Implementation of gdb.Style.escape_sequence(). Return the escape sequence to apply Style. If styling is turned off, then this returns the empty string. Can raise an exception if a named style can no longer diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h index f61a1753ac4..e11b9f92b52 100644 --- a/gdb/python/python-internal.h +++ b/gdb/python/python-internal.h @@ -570,6 +570,21 @@ struct symtab_and_line *sal_object_to_symtab_and_line (PyObject *obj); frame_info_ptr frame_object_to_frame_info (PyObject *frame_obj); struct gdbarch *arch_object_to_gdbarch (PyObject *obj); +/* Return true if OBJ is a gdb.Style object. OBJ must not be NULL. */ + +extern bool gdbpy_is_style (PyObject *obj); + +/* Return the ui_file_style from OBJ, a gdb.Style object. OBJ must not be + NULL, but can be a non gdb.Style object, in which case a Python error is + set and an empty optional is returned. + + It is also possible that OBJ is a gdb.Style object, but the underlying + style cannot be fetched for some reason. If this happens then a Python + error is set and an empty optional is returned. */ + +extern std::optional + gdbpy_style_object_to_ui_file_style (PyObject *obj); + extern PyObject *gdbpy_execute_mi_command (PyObject *self, PyObject *args, PyObject *kw); diff --git a/gdb/python/python.c b/gdb/python/python.c index 1af7896eb08..77d96da61c6 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -1561,12 +1561,22 @@ static PyObject * gdbpy_write (PyObject *self, PyObject *args, PyObject *kw) { const char *arg; - static const char *keywords[] = { "text", "stream", NULL }; + static const char *keywords[] = { "text", "stream", "style", nullptr }; int stream_type = 0; + PyObject *style_obj = Py_None; - if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|i", keywords, &arg, - &stream_type)) - return NULL; + if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|iO", keywords, &arg, + &stream_type, &style_obj)) + return nullptr; + + if (style_obj != Py_None && !gdbpy_is_style (style_obj)) + { + PyErr_Format + (PyExc_TypeError, + _("'style' argument must be gdb.Style or None, not %s."), + Py_TYPE (style_obj)->tp_name); + return nullptr; + } try { @@ -1584,7 +1594,17 @@ gdbpy_write (PyObject *self, PyObject *args, PyObject *kw) break; } - gdb_puts (arg, stream); + if (style_obj == Py_None) + gdb_puts (arg, stream); + else + { + std::optional style + = gdbpy_style_object_to_ui_file_style (style_obj); + if (!style.has_value ()) + return nullptr; + + fputs_styled (arg, style.value (), 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 index 1974dbb4186..6b10f9ca793 100644 --- a/gdb/testsuite/gdb.python/py-color-pagination.exp +++ b/gdb/testsuite/gdb.python/py-color-pagination.exp @@ -131,8 +131,64 @@ proc test_pagination { type mode } { } } +# Run the command 'style-fill-v2' which fills the screen with output and +# triggers the pagination prompt. Check that styling is applied correctly +# to the output. This v2 command is exercising passing a style to +# gdb.write() rather than passing the escape sequence for the style. +proc test_pagination_v2 { } { + set saw_bad_color_handling false + set expected_restore_color "" + set last_color "" + gdb_test_multiple "style-fill-v2" "" { + -re "^style-fill-v2\r\n" { + exp_continue + } + + -re "^(${::any_color}\033\\\[m)(${::any_color})$::str\033\\\[m" { + # 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 $expected_restore_color } { + set saw_bad_color_handling true + } + set last_color $expect_out(2,string) + exp_continue + } + + -re "^(${::any_color})$::str\033\\\[m" { + # This pattern matches printing STR in all cases that are not + # immediately after a pagination prompt. In this case there is + # a single escape sequence to set the color. + set last_color $expect_out(1,string) + exp_continue + } + + -re "^$::pagination_prompt$" { + # After a pagination prompt we expect GDB to restore the last + # color, but this will then be disabled due to a styled + # gdb.write emitting a return to default style escape sequence. + set expected_restore_color "$last_color\033\[m" + + # 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 "^$::gdb_prompt $" { + gdb_assert { !$saw_bad_color_handling } $gdb_test_name + } + } +} + foreach_with_prefix type { color style } { foreach_with_prefix mode { write print } { test_pagination $type $mode } } + +test_pagination_v2 diff --git a/gdb/testsuite/gdb.python/py-color-pagination.py b/gdb/testsuite/gdb.python/py-color-pagination.py index f0252e5bf86..9cdc76cb0ce 100644 --- a/gdb/testsuite/gdb.python/py-color-pagination.py +++ b/gdb/testsuite/gdb.python/py-color-pagination.py @@ -62,5 +62,21 @@ class StyleTester(gdb.Command): write(mode, "\n") +class StyleTester2(gdb.Command): + def __init__(self): + super().__init__("style-fill-v2", gdb.COMMAND_USER) + + def invoke(self, args, from_tty): + str = "<" + "-" * 78 + ">" + for i in range(0, 20): + for color_name in basic_colors: + c = gdb.Color(color_name) + s = gdb.Style(foreground=c) + gdb.write(str, style=s) + + gdb.write("\n") + + ColorTester() StyleTester() +StyleTester2() diff --git a/gdb/testsuite/gdb.python/py-style.exp b/gdb/testsuite/gdb.python/py-style.exp index b2efe9a97e3..491e189774e 100644 --- a/gdb/testsuite/gdb.python/py-style.exp +++ b/gdb/testsuite/gdb.python/py-style.exp @@ -340,3 +340,32 @@ gdb_test_no_output "python input_text = \"a totally different string that is als gdb_test "python print(output_text)" \ "^this is a unique string that is unlikely to appear elsewhere 12345" \ "check the output_text is still valid" + +# Test gdb.write passing in a style. Define a helper function to +# ensure all output is flushed before we return to the prompt. +gdb_test_multiline "create function to call gdb.write then flush" \ + "python" "" \ + "def write_and_flush(*args, **kwargs):" "" \ + " gdb.write(*args, **kwargs)" "" \ + " gdb.write(\"\\n\")" "" \ + " gdb.flush(gdb.STDOUT)" "" \ + "end" "" + +gdb_test "python write_and_flush(\"some text\")" \ + "^some text" "unstyled text, no style passed" + +gdb_test "python write_and_flush(\"some text\", style=None)" \ + "^some text" "unstyled text, pass style as None" + +gdb_test "python write_and_flush(\"some text\", style=filename_style)" \ + "^\033\\\[34;41;2;23;24;27msome text\033\\\[m" \ + "styled output, pass style by keyword" + +gdb_test "python write_and_flush(\"some text\", gdb.STDOUT, filename_style)" \ + "^\033\\\[34;41;2;23;24;27msome text\033\\\[m" \ + "styled output, pass style by position" + +gdb_test "python write_and_flush(\"some text\", style='filename')" \ + [multi_line \ + "Python Exception : 'style' argument must be gdb\\.Style or None, not str\\." \ + "Error occurred in Python: 'style' argument must be gdb\\.Style or None, not str\\."] -- 2.47.1