public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Enze Li <lienze2010@hotmail.com>
To: pedro@palves.net, tom@tromey.com
Cc: gdb-patches@sourceware.org
Subject: [PATCH v8 3/3] gdb/python: Export nibbles to python layer
Date: Sat, 23 Apr 2022 17:16:55 +0800	[thread overview]
Message-ID: <MEAP282MB029360DCAA1C52684807348EDDF69@MEAP282MB0293.AUSP282.PROD.OUTLOOK.COM> (raw)
In-Reply-To: <MEAP282MB0293F859A4DA5B33CB9096A0DDF69@MEAP282MB0293.AUSP282.PROD.OUTLOOK.COM>

This patch makes it possible to allow Value.format_string() to return
nibbles output.

When we set the parameter of nibbles to True, we can achieve the
displaying binary values in groups of every four bits.

Here's an example:
  (gdb) py print (gdb.Value (1230).format_string (format='t', nibbles=True))
  0100 1100 1110
  (gdb)

Note that the parameter nibbles is only useful if format='t' is also used.

This patch also includes update to the relevant testcase and
documentation.

Tested on x86_64 openSUSE Tumbleweed(VERSION_ID="20220413").
---
 gdb/doc/python.texi                           |  5 ++
 gdb/python/py-value.c                         |  7 +-
 gdb/testsuite/gdb.python/py-format-string.exp | 73 +++++++++++++++++++
 3 files changed, 84 insertions(+), 1 deletion(-)

diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi
index cb5283e03c0..5fd757d4891 100644
--- a/gdb/doc/python.texi
+++ b/gdb/doc/python.texi
@@ -1094,6 +1094,11 @@ union} in @ref{Print Settings}).
 address, @code{False} if it shouldn't (see @code{set print address} in
 @ref{Print Settings}).
 
+@item nibbles
+@code{True} if binary values should be displayed in groups of four bits,
+known as nibbles. @code{False} if it shouldn't (see @code{set print
+nibbles} in @ref{Print Settings}).
+
 @item deref_refs
 @code{True} if C@t{++} references should be resolved to the value they
 refer to, @code{False} (the default) if they shouldn't.  Note that, unlike
diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c
index e779f491b5b..4a51ff0f94d 100644
--- a/gdb/python/py-value.c
+++ b/gdb/python/py-value.c
@@ -640,6 +640,7 @@ valpy_format_string (PyObject *self, PyObject *args, PyObject *kw)
       "unions",			/* See set print union on|off.  */
       "address",		/* See set print address on|off.  */
       "styling",		/* Should we apply styling.  */
+      "nibbles",		/* See set print nibbles on|off.  */
       /* C++ options.  */
       "deref_refs",		/* No corresponding setting.  */
       "actual_objects",		/* See set print object on|off.  */
@@ -685,13 +686,14 @@ valpy_format_string (PyObject *self, PyObject *args, PyObject *kw)
   PyObject *unions_obj = NULL;
   PyObject *address_obj = NULL;
   PyObject *styling_obj = Py_False;
+  PyObject *nibbles_obj = NULL;
   PyObject *deref_refs_obj = NULL;
   PyObject *actual_objects_obj = NULL;
   PyObject *static_members_obj = NULL;
   char *format = NULL;
   if (!gdb_PyArg_ParseTupleAndKeywords (args,
 					kw,
-					"|O!O!O!O!O!O!O!O!O!O!O!IIIs",
+					"|O!O!O!O!O!O!O!O!O!O!O!O!IIIs",
 					keywords,
 					&PyBool_Type, &raw_obj,
 					&PyBool_Type, &pretty_arrays_obj,
@@ -701,6 +703,7 @@ valpy_format_string (PyObject *self, PyObject *args, PyObject *kw)
 					&PyBool_Type, &unions_obj,
 					&PyBool_Type, &address_obj,
 					&PyBool_Type, &styling_obj,
+					&PyBool_Type, &nibbles_obj,
 					&PyBool_Type, &deref_refs_obj,
 					&PyBool_Type, &actual_objects_obj,
 					&PyBool_Type, &static_members_obj,
@@ -725,6 +728,8 @@ valpy_format_string (PyObject *self, PyObject *args, PyObject *kw)
     return NULL;
   if (!copy_py_bool_obj (&opts.addressprint, address_obj))
     return NULL;
+  if (!copy_py_bool_obj (&opts.nibblesprint, nibbles_obj))
+    return NULL;
   if (!copy_py_bool_obj (&opts.deref_ref, deref_refs_obj))
     return NULL;
   if (!copy_py_bool_obj (&opts.objectprint, actual_objects_obj))
diff --git a/gdb/testsuite/gdb.python/py-format-string.exp b/gdb/testsuite/gdb.python/py-format-string.exp
index 63b87e73476..7705a245ee2 100644
--- a/gdb/testsuite/gdb.python/py-format-string.exp
+++ b/gdb/testsuite/gdb.python/py-format-string.exp
@@ -536,6 +536,78 @@ proc_with_prefix test_address {} {
   }
 }
 
+# Test the nibbles option for gdb.Value.format_string.
+proc_with_prefix test_nibbles {} {
+  global current_lang
+
+  set opts "format='t', nibbles=True"
+  with_test_prefix $opts {
+    if { $current_lang == "c" } {
+	set binary_pointer_regexp "\[ 0-1\]+"
+	gdb_test "python print (gdb.Value (42).format_string (${opts}))" \
+	  "0010 1010" \
+	  "42 with option ${opts}"
+
+	check_format_string "a_point_t" $opts
+	check_format_string "a_point_t_pointer" $opts \
+	  $binary_pointer_regexp
+	check_format_string "another_point" $opts
+
+	check_format_string "a_struct_with_union" $opts \
+	  "\\{the_union = \\{an_int = 0010 1010 0010 1010 0010 1010 0010 1010, a_char = 0010 1010\\}\\}"
+	check_format_string "an_enum" $opts \
+	  "0001"
+	check_format_string "a_string" $opts \
+	  $binary_pointer_regexp
+	check_format_string "a_binary_string" $opts \
+	  $binary_pointer_regexp
+	check_format_string "a_binary_string_array" $opts \
+	  "\\{0110 1000, 0110 0101, 0110 1100, 0110 1100, 0110 1111, 0, 0111 0111, 0110 1111, 0111 0010, 0110 1100, 0110 0100, 0\\}"
+	check_format_string "a_big_string" $opts \
+	  "\\{0100 0001, 0100 0010, 0100 0011, 0100 0100, 0100 0101, \[, 0-1\]+\.\.\.\\}"
+	check_format_string "an_array" $opts \
+	  "\\{0010, 0011, 0101\\}"
+	check_format_string "an_array_with_repetition" $opts \
+	  "\\{0001, 0011 <repeats 12 times>, 0101, 0101, 0101\\}"
+	check_format_string "a_symbol_pointer" $opts \
+	  $binary_pointer_regexp
+    }
+    if { $current_lang == "c++" } {
+	set binary_pointer_regexp "\['0-1\]+"
+	gdb_test "python print (gdb.Value (42).format_string (${opts}))" \
+	  "0010'1010" \
+	  "42 with option ${opts}"
+
+	check_format_string "a_point_t" $opts
+	check_format_string "a_point_t_pointer" $opts \
+	  $binary_pointer_regexp
+	check_format_string "another_point" $opts
+
+	check_format_string "a_struct_with_union" $opts \
+	  "\\{the_union = \\{an_int = 0010'1010'0010'1010'0010'1010'0010'1010, a_char = 0010'1010\\}\\}"
+	check_format_string "an_enum" $opts \
+	  "0001"
+	check_format_string "a_string" $opts \
+	  $binary_pointer_regexp
+	check_format_string "a_binary_string" $opts \
+	  $binary_pointer_regexp
+	check_format_string "a_binary_string_array" $opts \
+	  "\\{0110'1000, 0110'0101, 0110'1100, 0110'1100, 0110'1111, 0, 0111'0111, 0110'1111, 0111'0010, 0110'1100, 0110'0100, 0\\}"
+	check_format_string "a_big_string" $opts \
+	  "\\{0100'0001, 0100'0010, 0100'0011, 0100'0100, 0100'0101, \[, '0-1\]+\.\.\.\\}"
+	check_format_string "an_array" $opts \
+	  "\\{0010, 0011, 0101\\}"
+	check_format_string "an_array_with_repetition" $opts \
+	  "\\{0001, 0011 <repeats 12 times>, 0101, 0101, 0101\\}"
+	check_format_string "a_symbol_pointer" $opts \
+	  $binary_pointer_regexp
+
+	check_format_string "a_point_t_ref" $opts
+	check_format_string "a_base_ref" $opts
+    }
+  }
+}
+
 # Test the deref_refs option for gdb.Value.format_string.
 proc_with_prefix test_deref_refs {} {
   global current_lang
@@ -1047,6 +1119,7 @@ proc_with_prefix test_all_common {} {
   test_symbols
   test_unions
   test_address
+  test_nibbles
   test_deref_refs
   test_actual_objects
   test_static_members
-- 
2.35.3


  parent reply	other threads:[~2022-04-23  9:17 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-23  9:12 [PING][PATCH v8 0/3] gdb: Add new 'print nibbles' feature Enze Li
2022-04-23  9:14 ` [PATCH v8 1/3] " Enze Li
2022-04-23  9:15 ` [PATCH v8 2/3] gdb/doc: Documentation for the new print command Enze Li
2022-04-23  9:20   ` Eli Zaretskii
2022-04-23  9:16 ` Enze Li [this message]
2022-04-23  9:22   ` [PATCH v8 3/3] gdb/python: Export nibbles to python layer Eli Zaretskii
2022-06-07 13:44     ` Enze Li
2022-06-07 13:44     ` Enze Li
2022-06-06 13:41 ` [PING][PATCH v8 0/3] gdb: Add new 'print nibbles' feature Tom Tromey
2022-06-07 13:29   ` Enze Li
2022-06-18  3:29 ` Enze Li
     [not found] <20220416175963.3211-1-lienze2010@hotmail.com>
     [not found] ` <20220416181725.7176-1-lienze2010@hotmail.com>
2022-04-16 18:17   ` [PATCH v8 3/3] gdb/python: Export nibbles to python layer Enze Li

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=MEAP282MB029360DCAA1C52684807348EDDF69@MEAP282MB0293.AUSP282.PROD.OUTLOOK.COM \
    --to=lienze2010@hotmail.com \
    --cc=gdb-patches@sourceware.org \
    --cc=pedro@palves.net \
    --cc=tom@tromey.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).