public inbox for gdb-cvs@sourceware.org
help / color / mirror / Atom feed
* [binutils-gdb] gdb/python: Export nibbles to python layer
@ 2022-06-18  3:32 Enze Li
  0 siblings, 0 replies; only message in thread
From: Enze Li @ 2022-06-18  3:32 UTC (permalink / raw)
  To: gdb-cvs

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=3f52a09075e3c62f2150375bb7fca338e7db45e7

commit 3f52a09075e3c62f2150375bb7fca338e7db45e7
Author: Enze Li <enze.li@hotmail.com>
Date:   Sun Jun 12 13:25:31 2022 +0800

    gdb/python: Export nibbles to python layer
    
    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.

Diff:
---
 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 62db5f27016..9e2e97b1e74 100644
--- a/gdb/doc/python.texi
+++ b/gdb/doc/python.texi
@@ -1105,6 +1105,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 (@pxref{Print Settings,
+set print nibbles}).
+
 @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 0f7003363cc..12e9562e64a 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


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-06-18  3:32 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-18  3:32 [binutils-gdb] gdb/python: Export nibbles to python layer Enze Li

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