From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2126) id 5BDC13858D3C; Wed, 1 Jun 2022 12:28:09 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5BDC13858D3C Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Tom Tromey To: gdb-cvs@sourceware.org Subject: [binutils-gdb] Add gdb.current_language and gdb.Frame.language X-Act-Checkin: binutils-gdb X-Git-Author: Tom Tromey X-Git-Refname: refs/heads/master X-Git-Oldrev: 46c7fd95fc42466a5a8c3b3d70925f1a8af68de3 X-Git-Newrev: 80fa4b2a606763e71c4b599fa88288f554a0ea5b Message-Id: <20220601122809.5BDC13858D3C@sourceware.org> Date: Wed, 1 Jun 2022 12:28:09 +0000 (GMT) X-BeenThere: gdb-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 01 Jun 2022 12:28:09 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D80fa4b2a6067= 63e71c4b599fa88288f554a0ea5b commit 80fa4b2a606763e71c4b599fa88288f554a0ea5b Author: Tom Tromey Date: Tue May 24 10:15:17 2022 -0600 Add gdb.current_language and gdb.Frame.language =20 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". =20 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. Diff: --- gdb/NEWS | 7 +++++++ gdb/doc/python.texi | 12 ++++++++++++ gdb/python/py-frame.c | 25 +++++++++++++++++++++++++ gdb/python/python.c | 12 ++++++++++++ gdb/testsuite/gdb.python/py-frame.exp | 6 ++++++ gdb/testsuite/gdb.python/py-parameter.exp | 14 ++++++++++++++ gdb/testsuite/gdb.rust/pp.exp | 2 ++ 7 files changed, 78 insertions(+) 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. =20 + ** 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 =20 * 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 =20 +@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, ,S= tack Frames}. @end defun =20 +@defun Frame.language () +Return a string, the source language for this frame. +@end defun + @node Blocks In Python @subsubsection Accessing blocks from Python =20 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; } =20 +/* 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 =3D get_frame_language (fi); + const language_defn *lang_def =3D 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. */ =20 @@ -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 */ }; =20 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 *unuse= d2) return list.release (); } =20 +/* 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 = (); +} + =0C =20 /* 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." }, =20 + { "current_language", gdbpy_current_language, METH_NOARGS, + "current_language () -> string\n\ +Return the name of the currently selected language." }, + {NULL, NULL, 0, NULL} }; =20 diff --git a/gdb/testsuite/gdb.python/py-frame.exp b/gdb/testsuite/gdb.pyth= on/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 !=3D "" } { " =3D 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" } =20 +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 =20 # 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]} { =20 gdb_test "print outer" " =3D pp::Outer \\(x\\(5\\)\\)" gdb_test "print outer.0" " =3D x\\(5\\)" + +gdb_test "python print(gdb.selected_frame().language())" "rust"