From: Andrew Burgess <aburgess@redhat.com>
To: gdb-patches@sourceware.org
Cc: Andrew Burgess <aburgess@redhat.com>
Subject: [PATCHv2 1/2] gdb/python: add gdb.host_charset function
Date: Wed, 12 Jan 2022 14:30:27 +0000 [thread overview]
Message-ID: <47e7f45420ce152a65cc02f57a640c52c6724c12.1641997490.git.aburgess@redhat.com> (raw)
In-Reply-To: <cover.1641997490.git.aburgess@redhat.com>
We already have gdb.target_charset and gdb.target_wide_charset. This
commit adds gdb.host_charset along the same lines.
---
gdb/NEWS | 3 ++
gdb/doc/python.texi | 8 ++++
gdb/python/python.c | 13 +++++++
gdb/testsuite/gdb.python/py-charset.exp | 50 +++++++++++++++++++++++++
4 files changed, 74 insertions(+)
create mode 100644 gdb/testsuite/gdb.python/py-charset.exp
diff --git a/gdb/NEWS b/gdb/NEWS
index c1f30563a93..8c13cefb22f 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -143,6 +143,9 @@ show debug lin-lwp
is equivalent to the existing 'maint packet' CLI command; it
allows a user specified packet to be sent to the remote target.
+ ** New function gdb.host_charset(), returns a string, which is the
+ name of the current host charset.
+
* 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..38fce5b38e3 100644
--- a/gdb/doc/python.texi
+++ b/gdb/doc/python.texi
@@ -518,6 +518,14 @@
never returned.
@end defun
+@findex gdb.host_charset
+@defun gdb.host_charset ()
+Return a string, the name of the current host character set
+(@pxref{Character Sets}). This differs from
+@code{gdb.parameter('host-charset')} in that @samp{auto} is never
+returned.
+@end defun
+
@findex gdb.solib_name
@defun gdb.solib_name (address)
Return the name of the shared library holding the given @var{address}
diff --git a/gdb/python/python.c b/gdb/python/python.c
index e05b99c0bec..4dcda53d9ab 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -571,6 +571,16 @@ gdbpy_target_wide_charset (PyObject *self, PyObject *args)
return PyUnicode_Decode (cset, strlen (cset), host_charset (), NULL);
}
+/* Implement gdb.host_charset(). */
+
+static PyObject *
+gdbpy_host_charset (PyObject *self, PyObject *args)
+{
+ const char *cset = host_charset ();
+
+ return PyUnicode_Decode (cset, strlen (cset), host_charset (), NULL);
+}
+
/* A Python function which evaluates a string using the gdb CLI. */
static PyObject *
@@ -2281,6 +2291,9 @@ Return the name of the current target charset." },
{ "target_wide_charset", gdbpy_target_wide_charset, METH_NOARGS,
"target_wide_charset () -> string.\n\
Return the name of the current target wide charset." },
+ { "host_charset", gdbpy_host_charset, METH_NOARGS,
+ "host_charset () -> string.\n\
+Return the name of the current host charset." },
{ "rbreak", (PyCFunction) gdbpy_rbreak, METH_VARARGS | METH_KEYWORDS,
"rbreak (Regex) -> List.\n\
Return a Tuple containing gdb.Breakpoint objects that match the given Regex." },
diff --git a/gdb/testsuite/gdb.python/py-charset.exp b/gdb/testsuite/gdb.python/py-charset.exp
new file mode 100644
index 00000000000..e4af0e5b56f
--- /dev/null
+++ b/gdb/testsuite/gdb.python/py-charset.exp
@@ -0,0 +1,50 @@
+# Copyright 2022 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+load_lib gdb-python.exp
+
+gdb_exit
+gdb_start
+
+# Skip all tests if Python scripting is not enabled.
+if { [skip_python_tests] } { continue }
+
+# Each test data has 4 parts:
+# 1. The string used in 'show XXX-charset' command,
+# 2. The string expected in the output of the command used in #1,
+# 3. The string used is gdb.XXXX_charset() python function call,
+# 4. A string that is a regexp appended to the result of #1, used to
+# match the output of #3
+foreach test_data { {host host host ""} \
+ {target target target ""} \
+ {target-wide "target wide" \
+ "target_wide" "(LE|BE)?"} } {
+ with_test_prefix "charset=[lindex $test_data 0]" {
+ set charset "unknown"
+ gdb_test_multiple "show [lindex $test_data 0]-charset" "" {
+ -re "The [lindex $test_data 1] character set is \"auto; currently (\[^\"\]*)\".*$gdb_prompt $" {
+ set charset $expect_out(1,string)
+ pass $gdb_test_name
+ }
+ -re "The [lindex $test_data 1] character set is \"(\[^\"\]*)\".*$gdb_prompt $" {
+ set charset $expect_out(1,string)
+ pass $gdb_test_name
+ }
+ }
+ set charset "${charset}[lindex $test_data 3]"
+ gdb_test "python print(gdb.[lindex $test_data 2]_charset())" \
+ "${charset}"
+ }
+}
--
2.25.4
next prev parent reply other threads:[~2022-01-12 14:30 UTC|newest]
Thread overview: 36+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-01-07 14:23 [PATCH 0/4] Source highlight non utf-8 characters using Python Andrew Burgess
2022-01-07 14:23 ` [PATCH 1/4] gdb: new 'maint flush source-cache' command Andrew Burgess
2022-01-07 14:49 ` Eli Zaretskii
2022-01-11 12:13 ` Andrew Burgess
2022-01-11 13:31 ` Eli Zaretskii
2022-01-12 11:38 ` Andrew Burgess
2022-01-10 15:18 ` Tom Tromey
2022-01-07 14:23 ` [PATCH 2/4] gdb: erase items from the source_cache::m_offset_cache Andrew Burgess
2022-01-10 15:24 ` Tom Tromey
2022-01-11 12:17 ` Andrew Burgess
2022-01-11 14:54 ` Tom Tromey
2022-01-12 11:38 ` Andrew Burgess
2022-01-07 14:23 ` [PATCH 3/4] gdb: add 'maint set/show gnu-source-highlight enabled' command Andrew Burgess
2022-01-07 14:53 ` Eli Zaretskii
2022-01-11 13:07 ` Andrew Burgess
2022-01-11 13:34 ` Eli Zaretskii
2022-01-12 11:37 ` Andrew Burgess
2022-01-10 15:58 ` Tom Tromey
2022-01-07 14:23 ` [PATCH 4/4] gdb/python: handle non utf-8 characters when source highlighting Andrew Burgess
2022-01-10 3:27 ` Simon Marchi
2022-01-10 10:41 ` Andrew Burgess
2022-01-10 15:32 ` Simon Marchi
2022-01-11 13:10 ` Andrew Burgess
2022-01-11 19:24 ` Tom Tromey
2022-01-11 19:42 ` Patrick Monnerat
2022-01-12 14:30 ` [PATCHv2 0/2] Source highlight non utf-8 characters using Python Andrew Burgess
2022-01-12 14:30 ` Andrew Burgess [this message]
2022-01-12 15:02 ` [PATCHv2 1/2] gdb/python: add gdb.host_charset function Eli Zaretskii
2022-01-12 15:23 ` Tom Tromey
2022-01-12 16:05 ` Andrew Burgess
2022-01-12 14:30 ` [PATCHv2 2/2] gdb/python: handle non utf-8 characters when source highlighting Andrew Burgess
2022-01-12 15:32 ` Tom Tromey
2022-01-12 15:59 ` Andrew Burgess
2022-01-19 17:44 ` Andrew Burgess
2022-01-21 16:59 ` Simon Marchi
2022-01-26 23:13 ` 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=47e7f45420ce152a65cc02f57a640c52c6724c12.1641997490.git.aburgess@redhat.com \
--to=aburgess@redhat.com \
--cc=gdb-patches@sourceware.org \
/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).