From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7872) id 6FBD73857BB7; Sat, 18 Jun 2022 03:31:51 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6FBD73857BB7 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: Add new 'print nibbles' feature X-Act-Checkin: binutils-gdb X-Git-Author: Enze Li X-Git-Refname: refs/heads/master X-Git-Oldrev: 40d4cb8bccb0f46f34826564ea3502717ca8f0ce X-Git-Newrev: 21a527dfc85d62c9e90c65ac4076f517a6b76a48 Message-Id: <20220618033151.6FBD73857BB7@sourceware.org> Date: Sat, 18 Jun 2022 03:31:51 +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:31:51 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D21a527dfc85d= 62c9e90c65ac4076f517a6b76a48 commit 21a527dfc85d62c9e90c65ac4076f517a6b76a48 Author: Enze Li Date: Sun Jun 12 13:22:25 2022 +0800 gdb: Add new 'print nibbles' feature =20 Make an introduction of a new print setting that can be set by 'set print nibbles [on|off]'. The default value if OFF, which can be changed by user manually. Of course, 'show print nibbles' is also included in the patch. =20 The new feature displays binary values by group, with four bits per group. The motivation for this work is to enhance the readability of binary values. =20 Here's a GDB session before this patch is applied. (gdb) print var_a $1 =3D 1230 (gdb) print/t var_a $2 =3D 10011001110 =20 With this patch applied, we can use the new print setting to display the new form of the binary values. (gdb) print var_a $1 =3D 1230 (gdb) print/t var_a $2 =3D 10011001110 (gdb) set print nibbles on (gdb) print/t var_a $3 =3D 0100 1100 1110 =20 Tested on x86_64 openSUSE Tumbleweed. Diff: --- gdb/c-lang.c | 4 +++ gdb/language.h | 7 ++++++ gdb/printcmd.c | 2 +- gdb/rust-lang.h | 5 ++++ gdb/testsuite/gdb.base/options.exp | 1 + gdb/testsuite/gdb.base/printcmds.exp | 43 ++++++++++++++++++++++++++++++++ gdb/valprint.c | 48 ++++++++++++++++++++++++++++++++= +++- gdb/valprint.h | 6 ++++- 8 files changed, 113 insertions(+), 3 deletions(-) diff --git a/gdb/c-lang.c b/gdb/c-lang.c index 76896352954..87373085f24 100644 --- a/gdb/c-lang.c +++ b/gdb/c-lang.c @@ -857,6 +857,10 @@ public: const char *natural_name () const override { return "C++"; } =20 + /* See language.h */ + const char *get_digit_separator () const override + { return "\'"; } + /* See language.h. */ =20 const std::vector &filename_extensions () const override diff --git a/gdb/language.h b/gdb/language.h index f2885000259..c6812c52634 100644 --- a/gdb/language.h +++ b/gdb/language.h @@ -282,6 +282,13 @@ struct language_defn =20 virtual const char *natural_name () const =3D 0; =20 + /* Digit separator of the language. */ + + virtual const char *get_digit_separator () const + { + return " "; + } + /* Return a vector of file extensions for this language. The extension must include the ".", like ".c". If this language doesn't need to provide any filename extensions, this may be an empty vector (which is diff --git a/gdb/printcmd.c b/gdb/printcmd.c index 806c5d1b004..ce3dd3882d9 100644 --- a/gdb/printcmd.c +++ b/gdb/printcmd.c @@ -485,7 +485,7 @@ print_scalar_formatted (const gdb_byte *valaddr, struct= type *type, break; =20 case 't': - print_binary_chars (stream, valaddr, len, byte_order, size > 0); + print_binary_chars (stream, valaddr, len, byte_order, size > 0, opti= ons); break; case 'x': print_hex_chars (stream, valaddr, len, byte_order, size > 0); diff --git a/gdb/rust-lang.h b/gdb/rust-lang.h index 514494a2c82..6da57f8633f 100644 --- a/gdb/rust-lang.h +++ b/gdb/rust-lang.h @@ -71,6 +71,11 @@ public: =20 /* See language.h. */ =20 + const char *get_digit_separator () const override + { return "_"; } + + /* See language.h. */ + const std::vector &filename_extensions () const override { static const std::vector extensions =3D { ".rs" }; diff --git a/gdb/testsuite/gdb.base/options.exp b/gdb/testsuite/gdb.base/op= tions.exp index 41466614282..d10e7f8d66f 100644 --- a/gdb/testsuite/gdb.base/options.exp +++ b/gdb/testsuite/gdb.base/options.exp @@ -168,6 +168,7 @@ proc_with_prefix test-print {{prefix ""}} { "-elements" "-max-depth" "-memory-tag-violations" + "-nibbles" "-null-stop" "-object" "-pretty" diff --git a/gdb/testsuite/gdb.base/printcmds.exp b/gdb/testsuite/gdb.base/= printcmds.exp index 3260c8a3c09..04d390d3658 100644 --- a/gdb/testsuite/gdb.base/printcmds.exp +++ b/gdb/testsuite/gdb.base/printcmds.exp @@ -709,6 +709,48 @@ proc test_print_char_arrays {} { gdb_test_no_output "set print address off" "address off char arrays" } =20 +proc test_print_nibbles {} { + gdb_test_no_output "set print nibbles on" + foreach lang_line { + {"ada" "0" "0" "0011 0000" "1111 1111" "1111" "0001" "1111 0000 1111"} + {"asm" "0" "0" "0011 0000" "1111 1111" "1111" "0001" "1111 0000 1111"} + {"c" "0" "0" "0011 0000" "1111 1111" "1111" "0001" "1111 0000 1111"} + {"c++" "0" "0" "0011'0000" "1111'1111" "1111" "0001" "1111'0000'1111"} + {"d" "0" "0" "0011 0000" "1111 1111" "1111" "0001" "1111 0000 1111"} + {"fortran" "0" "0" "0011 0000" "1111 1111" "1111" "0001" "1111 0000 1111"} + {"go" "0" "0" "0011 0000" "1111 1111" "1111" "0001" "1111 0000 1111"} + {"minimal" "0" "0" "0011 0000" "1111 1111" "1111" "0001" "1111 0000 1111"} + {"objective-c" "0" "0" "0011 0000" "1111 1111" "1111" "0001" "1111 0000 1= 111"} + {"opencl" "0" "0" "0011 0000" "1111 1111" "1111" "0001" "1111 0000 1111"} + {"pascal" "0" "0" "0011 0000" "1111 1111" "1111" "0001" "1111 0000 1111"} + {"rust" "0" "0" "0011_0000" "1111_1111" "1111" "0001" "1111_0000_1111"} + } { + set lang [lindex $lang_line 0] + set val1 [lindex $lang_line 1] + set val2 [lindex $lang_line 2] + set val3 [lindex $lang_line 3] + set val4 [lindex $lang_line 4] + set val5 [lindex $lang_line 5] + set val6 [lindex $lang_line 6] + set val7 [lindex $lang_line 7] + set val8 [lindex $lang_line 8] + + with_test_prefix "$lang" { + gdb_test_no_output "set language $lang" + gdb_test "p/t 0" $val1 + gdb_test "p/t 0x0" $val2 + gdb_test "p/t 0x30" $val3 + gdb_test "p/t 0xff" $val4 + gdb_test "p/t 0x0f" $val5 + gdb_test "p/t 0x01" $val6 + gdb_test "p/t 0xf0f" $val7 + gdb_test "p/t 0x70f" $val8 + gdb_test_no_output "set language auto" + } + } + gdb_test_no_output "set print nibbles off" +} + proc test_print_string_constants {} { global gdb_prompt =20 @@ -1086,6 +1128,7 @@ test_print_int_arrays test_print_typedef_arrays test_artificial_arrays test_print_char_arrays +test_print_nibbles # We used to do the runto main here. test_print_string_constants test_print_array_constants diff --git a/gdb/valprint.c b/gdb/valprint.c index 47114676934..e63ec30f4c7 100644 --- a/gdb/valprint.c +++ b/gdb/valprint.c @@ -105,6 +105,7 @@ struct value_print_options user_print_options =3D 0, /* vtblprint */ 1, /* unionprint */ 1, /* addressprint */ + false, /* nibblesprint */ 0, /* objectprint */ PRINT_MAX_DEFAULT, /* print_max */ 10, /* repeat_count_threshold */ @@ -257,6 +258,17 @@ show_unionprint (struct ui_file *file, int from_tty, value); } =20 +/* Controls the format of printing binary values. */ + +static void +show_nibbles (struct ui_file *file, int from_tty, + struct cmd_list_element *c, const char *value) +{ + gdb_printf (file, + _("Printing binary values in groups is %s.\n"), + value); +} + /* If nonzero, causes machine addresses to be printed in certain contexts.= */ =20 static void @@ -1357,18 +1369,23 @@ print_floating (const gdb_byte *valaddr, struct typ= e *type, =20 void print_binary_chars (struct ui_file *stream, const gdb_byte *valaddr, - unsigned len, enum bfd_endian byte_order, bool zero_pad) + unsigned len, enum bfd_endian byte_order, bool zero_pad, + const struct value_print_options *options) { const gdb_byte *p; unsigned int i; int b; bool seen_a_one =3D false; + const char *digit_separator =3D nullptr; =20 /* Declared "int" so it will be signed. This ensures that right shift will shift in zeros. */ =20 const int mask =3D 0x080; =20 + if (options->nibblesprint) + digit_separator =3D current_language->get_digit_separator(); + if (byte_order =3D=3D BFD_ENDIAN_BIG) { for (p =3D valaddr; @@ -1380,6 +1397,9 @@ print_binary_chars (struct ui_file *stream, const gdb= _byte *valaddr, =20 for (i =3D 0; i < (HOST_CHAR_BIT * sizeof (*p)); i++) { + if (options->nibblesprint && seen_a_one && i % 4 =3D=3D 0) + gdb_putc (*digit_separator, stream); + if (*p & (mask >> i)) b =3D '1'; else @@ -1387,6 +1407,13 @@ print_binary_chars (struct ui_file *stream, const gd= b_byte *valaddr, =20 if (zero_pad || seen_a_one || b =3D=3D '1') gdb_putc (b, stream); + else if (options->nibblesprint) + { + if ((0xf0 & (mask >> i) && (*p & 0xf0)) + || (0x0f & (mask >> i) && (*p & 0x0f))) + gdb_putc (b, stream); + } + if (b =3D=3D '1') seen_a_one =3D true; } @@ -1400,6 +1427,9 @@ print_binary_chars (struct ui_file *stream, const gdb= _byte *valaddr, { for (i =3D 0; i < (HOST_CHAR_BIT * sizeof (*p)); i++) { + if (options->nibblesprint && seen_a_one && i % 4 =3D=3D 0) + gdb_putc (*digit_separator, stream); + if (*p & (mask >> i)) b =3D '1'; else @@ -1407,6 +1437,13 @@ print_binary_chars (struct ui_file *stream, const gd= b_byte *valaddr, =20 if (zero_pad || seen_a_one || b =3D=3D '1') gdb_putc (b, stream); + else if (options->nibblesprint) + { + if ((0xf0 & (mask >> i) && (*p & 0xf0)) + || (0x0f & (mask >> i) && (*p & 0x0f))) + gdb_putc (b, stream); + } + if (b =3D=3D '1') seen_a_one =3D true; } @@ -2837,6 +2874,15 @@ static const gdb::option::option_def value_print_opt= ion_defs[] =3D { NULL, /* help_doc */ }, =20 + boolean_option_def { + "nibbles", + [] (value_print_options *opt) { return &opt->nibblesprint; }, + show_nibbles, /* show_cmd_cb */ + N_("Set whether to print binary values in groups of four bits."), + N_("Show whether to print binary values in groups of four bits."), + NULL, /* help_doc */ + }, + uinteger_option_def { "elements", [] (value_print_options *opt) { return &opt->print_max; }, diff --git a/gdb/valprint.h b/gdb/valprint.h index ff536fbe4f0..d6ad45d7580 100644 --- a/gdb/valprint.h +++ b/gdb/valprint.h @@ -44,6 +44,9 @@ struct value_print_options /* Controls printing of addresses. */ bool addressprint; =20 + /* Controls printing of nibbles. */ + bool nibblesprint; + /* Controls looking up an object's derived type using what we find in its vtables. */ bool objectprint; @@ -149,7 +152,8 @@ extern void value_print_scalar_formatted int size, struct ui_file *stream); =20 extern void print_binary_chars (struct ui_file *, const gdb_byte *, - unsigned int, enum bfd_endian, bool); + unsigned int, enum bfd_endian, bool, + const struct value_print_options *options); =20 extern void print_octal_chars (struct ui_file *, const gdb_byte *, unsigned int, enum bfd_endian);