public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Andrew Burgess <aburgess@redhat.com>
To: Tom Tromey via Gdb-patches <gdb-patches@sourceware.org>,
	gdb-patches@sourceware.org
Cc: Tom Tromey <tromey@adacore.com>
Subject: Re: [PATCH] Add gdb.current_language function
Date: Mon, 30 May 2022 10:29:21 +0100	[thread overview]
Message-ID: <871qwbfjwu.fsf@redhat.com> (raw)
In-Reply-To: <20220527173642.1180218-1-tromey@adacore.com>

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


  parent reply	other threads:[~2022-05-30  9:29 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-27 17:36 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 [this message]
2022-05-31 14:01   ` Tom Tromey
2022-05-31 15:50     ` Eli Zaretskii
2022-05-31 16:25     ` Andrew Burgess

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=871qwbfjwu.fsf@redhat.com \
    --to=aburgess@redhat.com \
    --cc=gdb-patches@sourceware.org \
    --cc=tromey@adacore.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).