From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-io1-xd2a.google.com (mail-io1-xd2a.google.com [IPv6:2607:f8b0:4864:20::d2a]) by sourceware.org (Postfix) with ESMTPS id ABB65389682B for ; Mon, 10 Jan 2022 21:51:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org ABB65389682B Received: by mail-io1-xd2a.google.com with SMTP id 19so19603473ioz.4 for ; Mon, 10 Jan 2022 13:51:49 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=rsqg5I/AON2yf2JU9vzTAyQYD2EB+Jm7ROgplZkmCII=; b=1WxcyeIW2lEXohMBuPpLPLG/l/1xwxdeiT0YTTboGfq776ymNg0k//FYvJPcu6OVn2 3oJlt5AZLhAmvqWkPwkHAvVoi18ceeMzoUXXuEbzqZhRN3RZNHlRFJMIgU4e9N2zcG9T JILdZedEat+/lyzLRjn3SeHtXzI5jB/Md2ZjYpLPQoZyVPnw2gNVKPcg1EjlQnjWvmd0 wJNaX8ABOGxOfdXisKm7peeSnRtFS6vvi9zMY3L6LrYPZ/L072FyS3W98u5FASzXoGmM 9vp2O6MoJcHgshyLXUUJCViPFwqrofpDQ83KgZjIyZzCElXEg/9D0PeL7cJ7ewSbUE1G 8BSQ== X-Gm-Message-State: AOAM530MASTxXFub50AEte9WWQGzbPrptAfmmSri9dYwqf0AkZ6vOR55 4sjZpzYxYsfuseBu+1/iPPBt6nSZOaGUpw== X-Google-Smtp-Source: ABdhPJzm8pCGO6Qup+qQUYEEGcNIHmIGBXuUAVykcqUu0T3FwnW0YDx48SeiQvhAKWSAJeo80S+lMQ== X-Received: by 2002:a05:6638:3ef:: with SMTP id s15mr923563jaq.33.1641851509038; Mon, 10 Jan 2022 13:51:49 -0800 (PST) Received: from murgatroyd.Home (75-166-134-30.hlrn.qwest.net. [75.166.134.30]) by smtp.gmail.com with ESMTPSA id a1sm4820642iol.55.2022.01.10.13.51.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Jan 2022 13:51:48 -0800 (PST) From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH v2 3/3] Add a way to temporarily set a gdb parameter from Python Date: Mon, 10 Jan 2022 14:51:44 -0700 Message-Id: <20220110215144.1714909-4-tromey@adacore.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220110215144.1714909-1-tromey@adacore.com> References: <20220110215144.1714909-1-tromey@adacore.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-11.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 10 Jan 2022 21:51:51 -0000 It's sometimes useful to temporarily set some gdb parameter from Python. Now that the 'endian' crash is fixed, and now that the current language is no longer captured by the Python layer, it seems reasonable to add a helper function for this situation. This adds a new gdb.with_parameter function. This creates a context manager which temporarily sets some parameter to a specified value. The old value is restored when the context is exited. This is most useful with the Python "with" statement: with gdb.with_parameter('language', 'ada'): ... do Ada stuff This also adds a simple function to set a parameter, gdb.set_parameter, as suggested by Andrew. This is PR python/10790. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=10790 --- gdb/NEWS | 7 ++++++ gdb/doc/python.texi | 26 +++++++++++++++++++++ gdb/python/lib/gdb/__init__.py | 17 ++++++++++++++ gdb/testsuite/gdb.python/py-lookup-type.exp | 18 ++++++++++++++ 4 files changed, 68 insertions(+) diff --git a/gdb/NEWS b/gdb/NEWS index c26e15b530a..c3c73b4e004 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -120,6 +120,13 @@ clone-inferior is equivalent to the existing 'maint packet' CLI command; it allows a user specified packet to be sent to the remote target. + ** New gdb.set_parameter(NAME, VALUE). This sets the gdb parameter + NAME to VALUE. + + ** New gdb.with_parameter(NAME, VALUE). This returns a context + manager that temporarily sets the gdb parameter NAME to VALUE, + then resets it when the context is exited. + * New features in the GDB remote stub, GDBserver ** GDBserver is now supported on OpenRISC GNU/Linux. diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi index 6bd5f6b90ac..2f309d7d91b 100644 --- a/gdb/doc/python.texi +++ b/gdb/doc/python.texi @@ -332,6 +332,32 @@ parameter's value is converted to a Python value of the appropriate type, and returned. @end defun +@findex gdb.set_parameter +@defun gdb.set_parameter (name, value) +Sets the gdb parameter @var{name} to @var{value}. As with +@code{gdb.parameter}, the parameter name string may contain spaces if +the parameter has a multi-part name. +@end defun + +@findex gdb.with_parameter +@defun gdb.with_parameter (name, value) +Create a Python context manager (for use with the Python +@command{with} statement) that temporarily sets the gdb parameter +@var{name} to @var{value}. On exit from the context, the previous +value will be restored. + +This uses @code{gdb.parameter} in its implementation, so it can throw +the same exceptions as that function. + +For example, it's sometimes useful to evaluate some Python code with a +particular gdb language: + +@smallexample +with gdb.with_parameter('language', 'pascal'): + ... language-specific operations +@end smallexample +@end defun + @findex gdb.history @defun gdb.history (number) Return a value from @value{GDBN}'s value history (@pxref{Value diff --git a/gdb/python/lib/gdb/__init__.py b/gdb/python/lib/gdb/__init__.py index 11a1b444bfd..b32c66f6cb4 100644 --- a/gdb/python/lib/gdb/__init__.py +++ b/gdb/python/lib/gdb/__init__.py @@ -17,6 +17,7 @@ import traceback import os import sys import _gdb +from contextlib import contextmanager # Python 3 moved "reload" if sys.version_info >= (3, 4): @@ -231,6 +232,22 @@ def find_pc_line(pc): return current_progspace().find_pc_line(pc) +def set_parameter(name, value): + """Set the GDB parameter NAME to VALUE.""" + execute('set ' + name + ' ' + str(value), to_string=True) + + +@contextmanager +def with_parameter(name, value): + """Temporarily set the GDB parameter NAME to VALUE. + Note that this is a context manager.""" + old_value = parameter(name) + set_parameter(name, value) + # Nothing that useful to return. + yield None + set_parameter(name, old_value) + + try: from pygments import formatters, lexers, highlight diff --git a/gdb/testsuite/gdb.python/py-lookup-type.exp b/gdb/testsuite/gdb.python/py-lookup-type.exp index 534a5fd418e..596ab9a9af8 100644 --- a/gdb/testsuite/gdb.python/py-lookup-type.exp +++ b/gdb/testsuite/gdb.python/py-lookup-type.exp @@ -55,6 +55,19 @@ test_lookup_type "objective-c" "char" test_lookup_type "pascal" "char" +gdb_test "show language" \ + "The current source language is .pascal.." \ + "show language before 'with'" +gdb_test_multiline "look up type using set_parameter" \ + "python" "" \ + "with gdb.with_parameter('language', 'ada'):" "" \ + " print(gdb.lookup_type('character'))" "" \ + "end" "character" +gdb_test "show language" \ + "The current source language is .pascal.." \ + "show language after 'with'" + + # Ensure that the language can be changed from within Python and still # affect the results. gdb_test_multiline "look up ada type from another language" \ @@ -62,3 +75,8 @@ gdb_test_multiline "look up ada type from another language" \ "gdb.execute('set language ada')" "" \ "print(gdb.lookup_type('character'))" "" \ "end" "character" + +gdb_test_no_output "python gdb.set_parameter('language', 'rust')" +gdb_test "show language" \ + "The current source language is .rust.." \ + "show language after 'set_parameter'" -- 2.31.1