From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 2E116395B413 for ; Tue, 31 May 2022 16:25:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 2E116395B413 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.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-127-L0oLWQRwMXiMS1OOLKJEHg-1; Tue, 31 May 2022 12:25:54 -0400 X-MC-Unique: L0oLWQRwMXiMS1OOLKJEHg-1 Received: by mail-wm1-f72.google.com with SMTP id k7-20020a05600c1c8700b003974d762928so6254429wms.7 for ; Tue, 31 May 2022 09:25:54 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=0y5t1FB+zItfBgTAnV26FUKeUQBZmX22QT27yqxEFVQ=; b=2UyiWwQsp+yMb/ORXL0EXNQcXzc3m99JKQdMON1SYEB4IGl3vCjrdeWVsX5wFIXKDr PCIMzo7HeJFiWULqaISBdVOYsQ4jclwFD6IUKhprTH5JRLZ6bgWP0vWqNDoZZdUaJUzM M2vdfuLv6mQPH1NFfjdvgWmsyX9MqhvQs11Mry5d0rK+Bs1THUOOPb2X+RLCdQNZQAsB H/jpDbJFEBNY4Yi/vYgVdm56T0M9XPS+kXlaaU4guc7FF5ICxJ7TT3XTK9ILoIvei+XZ eFx8oEx2NRMimZrdugg/tjkBMfL1tA+tlVXsgwJ1c1emfkqvLg66yfxqJftpcglJZqVb vYAg== X-Gm-Message-State: AOAM531WMKXFdW1lzCa7UVrr8bUIiu/WkDMt4AjakLAMClB5Vc0TuXGl /CJzh1F7jb+nQ6+nTn1b7z9l/ZNnxZj/qUhrSh71a4+raNTBJMGg3+igy5S6+5LZfEpO6o/6oxZ C3wlw83HX8CkFxLkX6wiOmg== X-Received: by 2002:a05:6000:91:b0:20f:fb45:ab0a with SMTP id m17-20020a056000009100b0020ffb45ab0amr27566329wrx.34.1654014352763; Tue, 31 May 2022 09:25:52 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzfTGwTHqRep1z+6jTXbxOrE+YzUfNSxNd8z/Oxiud7UYUIyk3MlE/ltDlp6Cv1SZAQ/18/Lg== X-Received: by 2002:a05:6000:91:b0:20f:fb45:ab0a with SMTP id m17-20020a056000009100b0020ffb45ab0amr27566306wrx.34.1654014352438; Tue, 31 May 2022 09:25:52 -0700 (PDT) Received: from localhost (host109-152-215-36.range109-152.btcentralplus.com. [109.152.215.36]) by smtp.gmail.com with ESMTPSA id r16-20020adff110000000b002102f986b0esm7022754wro.98.2022.05.31.09.25.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 May 2022 09:25:51 -0700 (PDT) Date: Tue, 31 May 2022 17:25:50 +0100 From: Andrew Burgess To: Tom Tromey Cc: gdb-patches@sourceware.org Subject: Re: [PATCH] Add gdb.current_language function Message-ID: <20220531162550.GB6045@redhat.com> References: <20220527173642.1180218-1-tromey@adacore.com> <871qwbfjwu.fsf@redhat.com> <87ilpl6btv.fsf@tromey.com> MIME-Version: 1.0 In-Reply-To: <87ilpl6btv.fsf@tromey.com> X-Operating-System: Linux/5.8.18-100.fc31.x86_64 (x86_64) X-Uptime: 17:24:53 up 3 days, 7:15, X-Editor: GNU Emacs [ http://www.gnu.org/software/emacs ] X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00, DKIM_INVALID, DKIM_SIGNED, GIT_PATCH_0, KAM_DMARC_NONE, KAM_DMARC_STATUS, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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: Tue, 31 May 2022 16:25:57 -0000 * Tom Tromey via Gdb-patches [2022-05-31 08:01:00 -0600]: > Andrew> I think you should also add Frame.current_lanaguage, and the docs for > Andrew> gdb.current_lanaguage should make it clear that the Frame method should > Andrew> be used in preference where a Frame is available. > > Here's v2 with this addition. > > Tom > > commit dba44c7ff83db6ac80377b5a42ce75cf5557347f > Author: Tom Tromey > Date: Tue May 24 10:15:17 2022 -0600 > > Add gdb.current_language and gdb.Frame.language > > This adds the gdb.current_language function, which can be used to find > the current language without (1) ever having the value "auto" or (2) > having to parse the output of "show language". > > It also adds the gdb.Frame.language, which can be used to find the > language of a given frame. This is normally preferable if one has a > Frame object handy. Thanks, this LGTM. Andrew > > diff --git a/gdb/NEWS b/gdb/NEWS > index dac6dabfa42..960f90b4387 100644 > --- a/gdb/NEWS > +++ b/gdb/NEWS > @@ -58,6 +58,13 @@ maintenance info line-table > This is the same format that GDB uses when printing address, symbol, > and offset information from the disassembler. > > + ** New function gdb.current_language that returns the name of the > + current language. Unlike gdb.parameter('language'), this will > + never return 'auto'. > + > + ** New method gdb.Frame.language that returns the name of the > + frame's language. > + > *** Changes in GDB 12 > > * DBX mode is deprecated, and will be removed in GDB 13 > diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi > index f933c7d30c9..ba5a9b315e1 100644 > --- a/gdb/doc/python.texi > +++ b/gdb/doc/python.texi > @@ -668,6 +668,14 @@ Here are some examples of the possible string formats: > @end smallexample > @end defun > > +@defun gdb.current_language () > +Return the name of the current language as a string. Unlike > +@code{gdb.parameter('language')}, this function will never return > +@samp{auto}. If a @code{gdb.Frame} object is available (@pxref{Frames > +In Python}), the @code{language} method might be preferable in some > +cases, as that is not affected by the user's language setting. > +@end defun > + > @node Exception Handling > @subsubsection Exception Handling > @cindex python exceptions > @@ -5202,6 +5210,10 @@ Stack}. > Return an integer, the stack frame level for this frame. @xref{Frames, ,Stack Frames}. > @end defun > > +@defun Frame.language () > +Return a string, the source language for this frame. > +@end defun > + > @node Blocks In Python > @subsubsection Accessing blocks from Python > > diff --git a/gdb/python/py-frame.c b/gdb/python/py-frame.c > index 769e28c1a2c..9a28c36c1cc 100644 > --- a/gdb/python/py-frame.c > +++ b/gdb/python/py-frame.c > @@ -598,6 +598,29 @@ frapy_level (PyObject *self, PyObject *args) > Py_RETURN_NONE; > } > > +/* The language for this frame. */ > + > +static PyObject * > +frapy_language (PyObject *self, PyObject *args) > +{ > + try > + { > + struct frame_info *fi; > + FRAPY_REQUIRE_VALID (self, fi); > + > + enum language lang = get_frame_language (fi); > + const language_defn *lang_def = language_def (lang); > + > + return host_string_to_python_string (lang_def->name ()).release (); > + } > + catch (const gdb_exception &except) > + { > + GDB_PY_HANDLE_EXCEPTION (except); > + } > + > + Py_RETURN_NONE; > +} > + > /* Implementation of gdb.newest_frame () -> gdb.Frame. > Returns the newest frame object. */ > > @@ -771,6 +794,8 @@ Return the value of the variable in this frame." }, > "Select this frame as the user's current frame." }, > { "level", frapy_level, METH_NOARGS, > "The stack level of this frame." }, > + { "language", frapy_language, METH_NOARGS, > + "The language of this frame." }, > {NULL} /* Sentinel */ > }; > > diff --git a/gdb/python/python.c b/gdb/python/python.c > index 11aaa7ae778..9bef2252e88 100644 > --- a/gdb/python/python.c > +++ b/gdb/python/python.c > @@ -1571,6 +1571,14 @@ gdbpy_progspaces (PyObject *unused1, PyObject *unused2) > return list.release (); > } > > +/* Return the name of the current language. */ > + > +static PyObject * > +gdbpy_current_language (PyObject *unused1, PyObject *unused2) > +{ > + return host_string_to_python_string (current_language->name ()).release (); > +} > + > > > /* The "current" objfile. This is set when gdb detects that a new > @@ -2534,6 +2542,10 @@ Format ADDRESS, an address within PROG_SPACE, a gdb.Progspace, using\n\ > ARCH, a gdb.Architecture to determine the address size. The format of\n\ > the returned string is 'ADDRESS ' without the quotes." }, > > + { "current_language", gdbpy_current_language, METH_NOARGS, > + "current_language () -> string\n\ > +Return the name of the currently selected language." }, > + > {NULL, NULL, 0, NULL} > }; > > diff --git a/gdb/testsuite/gdb.python/py-frame.exp b/gdb/testsuite/gdb.python/py-frame.exp > index b91ffe62a83..4991e8a0c5d 100644 > --- a/gdb/testsuite/gdb.python/py-frame.exp > +++ b/gdb/testsuite/gdb.python/py-frame.exp > @@ -128,3 +128,9 @@ if { $pc != "" } { > " = True" \ > "test Frame.read_register($pc)" > } > + > +# Test language. > +gdb_test "python print(gdb.selected_frame().language())" "c" > +gdb_test "set language ada" > +gdb_test "python print(gdb.selected_frame().language())" "c" \ > + "frame language is not affected by global language" > diff --git a/gdb/testsuite/gdb.python/py-parameter.exp b/gdb/testsuite/gdb.python/py-parameter.exp > index 199d3bc16ec..db158ddec26 100644 > --- a/gdb/testsuite/gdb.python/py-parameter.exp > +++ b/gdb/testsuite/gdb.python/py-parameter.exp > @@ -370,6 +370,19 @@ proc_with_prefix test_throwing_parameter { } { > "gdb.GdbError does not show Python stack" > } > > +proc_with_prefix test_language {} { > + gdb_test "python print(gdb.parameter('language'))" "auto" \ > + "print language parameter" > + gdb_test "python print(gdb.current_language())" "c" \ > + "print current language" > + gdb_test_no_output "set lang rust" > + gdb_test "python print(gdb.parameter('language'))" "rust" \ > + "print language parameter for rust" > + gdb_test "python print(gdb.current_language())" "rust" \ > + "print current language for rust" > + gdb_test_no_output "set lang auto" > +} > + > test_directories > test_data_directory > test_boolean_parameter > @@ -380,6 +393,7 @@ test_really_undocumented_parameter > test_deprecated_api_parameter > test_integer_parameter > test_throwing_parameter > +test_language > > # This caused a gdb crash. > gdb_test "python print(gdb.parameter('endian'))" "auto" \ > diff --git a/gdb/testsuite/gdb.rust/pp.exp b/gdb/testsuite/gdb.rust/pp.exp > index 7c7c78b5847..e3e226c5db5 100644 > --- a/gdb/testsuite/gdb.rust/pp.exp > +++ b/gdb/testsuite/gdb.rust/pp.exp > @@ -40,3 +40,5 @@ if {![runto ${srcfile}:$line]} { > > gdb_test "print outer" " = pp::Outer \\(x\\(5\\)\\)" > gdb_test "print outer.0" " = x\\(5\\)" > + > +gdb_test "python print(gdb.selected_frame().language())" "rust"