From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7872) id 91D11385700D; Sat, 18 Jun 2022 03:32:01 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 91D11385700D Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Enze Li To: gdb-cvs@sourceware.org Subject: [binutils-gdb] gdb/python: Export nibbles to python layer X-Act-Checkin: binutils-gdb X-Git-Author: Enze Li X-Git-Refname: refs/heads/master X-Git-Oldrev: 153b3c1117d02c2893c5be1b3af53ec7f161a211 X-Git-Newrev: 3f52a09075e3c62f2150375bb7fca338e7db45e7 Message-Id: <20220618033201.91D11385700D@sourceware.org> Date: Sat, 18 Jun 2022 03:32:01 +0000 (GMT) X-BeenThere: gdb-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 18 Jun 2022 03:32:01 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D3f52a09075e3= c62f2150375bb7fca338e7db45e7 commit 3f52a09075e3c62f2150375bb7fca338e7db45e7 Author: Enze Li Date: Sun Jun 12 13:25:31 2022 +0800 gdb/python: Export nibbles to python layer =20 This patch makes it possible to allow Value.format_string() to return nibbles output. =20 When we set the parameter of nibbles to True, we can achieve the displaying binary values in groups of every four bits. =20 Here's an example: (gdb) py print (gdb.Value (1230).format_string (format=3D't', nibbles= =3DTrue)) 0100 1100 1110 (gdb) =20 Note that the parameter nibbles is only useful if format=3D't' is also = used. =20 This patch also includes update to the relevant testcase and documentation. =20 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}). =20 +@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, Py= Object *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 =3D NULL; PyObject *address_obj =3D NULL; PyObject *styling_obj =3D Py_False; + PyObject *nibbles_obj =3D NULL; PyObject *deref_refs_obj =3D NULL; PyObject *actual_objects_obj =3D NULL; PyObject *static_members_obj =3D NULL; char *format =3D 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, Py= Object *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, Py= Object *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 {} { } } =20 +# Test the nibbles option for gdb.Value.format_string. +proc_with_prefix test_nibbles {} { + global current_lang + + set opts "format=3D't', nibbles=3DTrue" + with_test_prefix $opts { + if { $current_lang =3D=3D "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 =3D \\{an_int =3D 0010 1010 0010 1010 0010 1010 0010 1010= , a_char =3D 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 , 0101, 0101, 0101\\}" + check_format_string "a_symbol_pointer" $opts \ + $binary_pointer_regexp + } + if { $current_lang =3D=3D "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 =3D \\{an_int =3D 0010'1010'0010'1010'0010'1010'0010'1010= , a_char =3D 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 , 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