public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Add gdb.current_language function
@ 2022-05-27 17:36 Tom Tromey
  2022-05-27 17:48 ` Eli Zaretskii
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Tom Tromey @ 2022-05-27 17:36 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

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".
---
 gdb/NEWS                                  |  4 ++++
 gdb/doc/python.texi                       |  6 ++++++
 gdb/python/python.c                       | 12 ++++++++++++
 gdb/testsuite/gdb.python/py-parameter.exp | 13 +++++++++++++
 4 files changed, 35 insertions(+)

diff --git a/gdb/NEWS b/gdb/NEWS
index 52ffdc4c83a..2e87f8d56f3 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -51,6 +51,10 @@ 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'.
+
 *** 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 cb5283e03c0..0767e157e4a 100644
--- a/gdb/doc/python.texi
+++ b/gdb/doc/python.texi
@@ -667,6 +667,12 @@ 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.  Note that,
+unlike @code{gdb.parameter(language')}, this function will never
+return @samp{auto}.
+@end defun
+
 @node Exception Handling
 @subsubsection Exception Handling
 @cindex python exceptions
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 ();
+}
+
 \f
 
 /* 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 <SYMBOL+OFFSET>' 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-parameter.exp b/gdb/testsuite/gdb.python/py-parameter.exp
index 199d3bc16ec..fcafa7ee717 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
-- 
2.34.1


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] Add gdb.current_language function
  2022-05-27 17:36 [PATCH] Add gdb.current_language function Tom Tromey
@ 2022-05-27 17:48 ` Eli Zaretskii
  2022-05-28 15:09 ` Enze Li
  2022-05-30  9:29 ` Andrew Burgess
  2 siblings, 0 replies; 8+ messages in thread
From: Eli Zaretskii @ 2022-05-27 17:48 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

> Date: Fri, 27 May 2022 11:36:42 -0600
> From: Tom Tromey via Gdb-patches <gdb-patches@sourceware.org>
> Cc: Tom Tromey <tromey@adacore.com>
> 
> 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".
> ---
>  gdb/NEWS                                  |  4 ++++
>  gdb/doc/python.texi                       |  6 ++++++
>  gdb/python/python.c                       | 12 ++++++++++++
>  gdb/testsuite/gdb.python/py-parameter.exp | 13 +++++++++++++
>  4 files changed, 35 insertions(+)

Thanks.

> +  ** New function gdb.current_language() that returns the name of the

Please remove the "()" part, it is not needed there, and can be
misinterpreted.

> +@defun gdb.current_language ()
> +Return the name of the current language as a string.  Note that,
> +unlike @code{gdb.parameter(language')}, this function will never
                             ^^
A quote missing there.

The documentation parts are okay with these nits fixed.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] Add gdb.current_language function
  2022-05-27 17:36 [PATCH] Add gdb.current_language function Tom Tromey
  2022-05-27 17:48 ` Eli Zaretskii
@ 2022-05-28 15:09 ` Enze Li
  2022-05-31 13:53   ` Tom Tromey
  2022-05-30  9:29 ` Andrew Burgess
  2 siblings, 1 reply; 8+ messages in thread
From: Enze Li @ 2022-05-28 15:09 UTC (permalink / raw)
  To: Tom Tromey, gdb-patches

On Fri, 2022-05-27 at 11:36 -0600, Tom Tromey via Gdb-patches wrote:
> 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".
> 
<...>
>  
> +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"
> +}
> +
> 
Hi Tom,

I see this patch and don't quite understand why a test case is defined
here but not called.  Do we need to execute this newly defined test
case?  If not, please ignore my annoying interruptions.

Thanks,
Enze


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] Add gdb.current_language function
  2022-05-27 17:36 [PATCH] Add gdb.current_language function Tom Tromey
  2022-05-27 17:48 ` Eli Zaretskii
  2022-05-28 15:09 ` Enze Li
@ 2022-05-30  9:29 ` Andrew Burgess
  2022-05-31 14:01   ` Tom Tromey
  2 siblings, 1 reply; 8+ messages in thread
From: Andrew Burgess @ 2022-05-30  9:29 UTC (permalink / raw)
  To: Tom Tromey via Gdb-patches, gdb-patches; +Cc: Tom Tromey

Tom Tromey via Gdb-patches <gdb-patches@sourceware.org> writes:

> 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".

I think you should also add Frame.current_lanaguage, and the docs for
gdb.current_lanaguage should make it clear that the Frame method should
be used in preference where a Frame is available.

I understand that there the current language does change GDB's behaviour
in places where there is no inferior and therefore no frame, so I guess
we probably do want both methods, which is a bit of a shame, but can't
be helped.

Having a Frame method means users don't need to switch frames just to
figure out the lanague of a specific frame.  Adding the method now means
users wont be tempted to write code that relies on the global method,
which could lead to the wrong result in some cases.

Thanks,
Andrew


> ---
>  gdb/NEWS                                  |  4 ++++
>  gdb/doc/python.texi                       |  6 ++++++
>  gdb/python/python.c                       | 12 ++++++++++++
>  gdb/testsuite/gdb.python/py-parameter.exp | 13 +++++++++++++
>  4 files changed, 35 insertions(+)
>
> diff --git a/gdb/NEWS b/gdb/NEWS
> index 52ffdc4c83a..2e87f8d56f3 100644
> --- a/gdb/NEWS
> +++ b/gdb/NEWS
> @@ -51,6 +51,10 @@ 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'.
> +
>  *** 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 cb5283e03c0..0767e157e4a 100644
> --- a/gdb/doc/python.texi
> +++ b/gdb/doc/python.texi
> @@ -667,6 +667,12 @@ 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.  Note that,
> +unlike @code{gdb.parameter(language')}, this function will never
> +return @samp{auto}.
> +@end defun
> +
>  @node Exception Handling
>  @subsubsection Exception Handling
>  @cindex python exceptions
> 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 ();
> +}
> +
>  \f
>  
>  /* 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 <SYMBOL+OFFSET>' 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-parameter.exp b/gdb/testsuite/gdb.python/py-parameter.exp
> index 199d3bc16ec..fcafa7ee717 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
> -- 
> 2.34.1


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] Add gdb.current_language function
  2022-05-28 15:09 ` Enze Li
@ 2022-05-31 13:53   ` Tom Tromey
  0 siblings, 0 replies; 8+ messages in thread
From: Tom Tromey @ 2022-05-31 13:53 UTC (permalink / raw)
  To: Enze Li; +Cc: Tom Tromey, gdb-patches

Enze> I see this patch and don't quite understand why a test case is defined
Enze> here but not called.  Do we need to execute this newly defined test
Enze> case?  If not, please ignore my annoying interruptions.

Yeah, I just forgot to do that.  I added the line now.

Tom

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] Add gdb.current_language function
  2022-05-30  9:29 ` Andrew Burgess
@ 2022-05-31 14:01   ` Tom Tromey
  2022-05-31 15:50     ` Eli Zaretskii
  2022-05-31 16:25     ` Andrew Burgess
  0 siblings, 2 replies; 8+ messages in thread
From: Tom Tromey @ 2022-05-31 14:01 UTC (permalink / raw)
  To: Andrew Burgess; +Cc: Tom Tromey via Gdb-patches, Tom Tromey

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 <tromey@adacore.com>
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.

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 ();
+}
+
 \f
 
 /* 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 <SYMBOL+OFFSET>' 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"

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] Add gdb.current_language function
  2022-05-31 14:01   ` Tom Tromey
@ 2022-05-31 15:50     ` Eli Zaretskii
  2022-05-31 16:25     ` Andrew Burgess
  1 sibling, 0 replies; 8+ messages in thread
From: Eli Zaretskii @ 2022-05-31 15:50 UTC (permalink / raw)
  To: Tom Tromey; +Cc: aburgess, gdb-patches

> Date: Tue, 31 May 2022 08:01:00 -0600
> From: Tom Tromey via Gdb-patches <gdb-patches@sourceware.org>
> Cc: Tom Tromey <tromey@adacore.com>,
>  Tom Tromey via Gdb-patches <gdb-patches@sourceware.org>
> 
> 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.

Thanks, the documentation parts are OK.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] Add gdb.current_language function
  2022-05-31 14:01   ` Tom Tromey
  2022-05-31 15:50     ` Eli Zaretskii
@ 2022-05-31 16:25     ` Andrew Burgess
  1 sibling, 0 replies; 8+ messages in thread
From: Andrew Burgess @ 2022-05-31 16:25 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

* Tom Tromey via Gdb-patches <gdb-patches@sourceware.org> [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 <tromey@adacore.com>
> 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 ();
> +}
> +
>  \f
>  
>  /* 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 <SYMBOL+OFFSET>' 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"


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2022-05-31 16:25 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-27 17:36 [PATCH] Add gdb.current_language function Tom Tromey
2022-05-27 17:48 ` Eli Zaretskii
2022-05-28 15:09 ` Enze Li
2022-05-31 13:53   ` Tom Tromey
2022-05-30  9:29 ` Andrew Burgess
2022-05-31 14:01   ` Tom Tromey
2022-05-31 15:50     ` Eli Zaretskii
2022-05-31 16:25     ` Andrew Burgess

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).