public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH v3] gdb: Add new 'print nibbles' feature
@ 2021-08-25 13:24 Enze Li
  2021-08-25 14:10 ` Eli Zaretskii
  2021-08-27 18:18 ` Tom Tromey
  0 siblings, 2 replies; 5+ messages in thread
From: Enze Li @ 2021-08-25 13:24 UTC (permalink / raw)
  To: gdb-patches

Make an introduction of a new print setting that can be set by 'set
print nibbles [on|off].  The default value is OFF, and it would be
changed by users manually.

And of course, 'show print nibbles' is also included in the patch.

The new feature could display binary values in groups, and each group
has four bits.

Here's a GDB session before this patch is applied.
  (gdb) print var_a
  $1 = 1230
  (gdb) print/t var_a
  $2 = 10011001110

With this patch applied, we have a new print setting to use.
  (gdb) print var_a
  $1 = 1230
  (gdb) print/t var_a
  $2 = 10011001110
  (gdb) set print nibbles on
  (gdb) print/t a
  $3 = 100 1100 1110

Tested on x86_64-linux(little-endian) and mips-linux(big-endian).

gdb/ChangeLog:
2021-08-25  Enze Li  <lienze2010@hotmail.com>

        * NEWS: Metion new command.
        * printcmd.c (print_scalar_formatted): Add new parameter and pass
        it to print_binary_chars.
        * valprint.c (struct value_print_options) <nibblesprint>:
        New member.
        (show_binary_groups): New function.
        (print_binary_chars): Add new parameter `value_print_option *`
        and use it.
        * valprint.h (struct value_print_options):
        (print_binary_chars): Add new parameter to declaration.

gdb/doc/ChangeLog:
2021-08-25  Enze Li  <lienze2010@hotmail.com>

        * gdb.texinfo (Print Settings): Document 'print nibbles'.

gdb/testsuite/ChangeLog:
2021-08-25  Enze Li  <lienze2010@hotmail.com>

        * gdb.base/printcmds.exp: Update to use new
        'print nibbles' command.
---
 gdb/NEWS                             |  5 ++++
 gdb/doc/gdb.texinfo                  | 34 ++++++++++++++++++++++++----
 gdb/printcmd.c                       |  2 +-
 gdb/testsuite/gdb.base/options.exp   |  1 +
 gdb/testsuite/gdb.base/printcmds.exp | 14 ++++++++++++
 gdb/valprint.c                       | 26 ++++++++++++++++++++-
 gdb/valprint.h                       |  6 ++++-
 7 files changed, 81 insertions(+), 7 deletions(-)

diff --git a/gdb/NEWS b/gdb/NEWS
index ec3058ea118..272b4c5be86 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -135,6 +135,11 @@ set debug event-loop
 show debug event-loop
   Control the display of debug output about GDB's event loop.
 
+set print nibbles [on|off]
+show print nibbles
+  This controls whether the 'print/t' command will display binary values
+  in groups of four bits, known as "nibbles".  The default is 'off'.
+
 set print memory-tag-violations
 show print memory-tag-violations
   Control whether to display additional information about memory tag violations
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 58479ef3ed6..8e61d5889d8 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -2141,10 +2141,10 @@ on @code{-} after the command name.  For example:
 
 @smallexample
 (@value{GDBP}) print -@key{TAB}@key{TAB}
--address         -max-depth       -raw-values      -union
--array           -null-stop       -repeats         -vtbl
--array-indexes   -object          -static-members
--elements        -pretty          -symbol
+-address         -max-depth       -pretty          -symbol
+-array           -nibbles         -raw-values      -union
+-array-indexes   -null-stop       -repeats         -vtbl
+-elements        -object          -static-members
 @end smallexample
 
 Completion will in some cases guide you with a suggestion of what kind
@@ -9977,6 +9977,10 @@ Set limit on string chars or array elements to print.  The value
 Set the threshold after which nested structures are replaced with
 ellipsis.  Related setting: @ref{set print max-depth}.
 
+@item -nibbles [@code{on}|@code{off}]
+Set whether to print binary values in groups of four bits, known
+as "nibbles". Related setting: @ref{set print nibbles}.
+
 @item -null-stop [@code{on}|@code{off}]
 Set printing of char arrays to stop at first null char.  Related
 setting: @ref{set print null-stop}.
@@ -11335,6 +11339,28 @@ Stop printing element indexes when displaying arrays.
 Show whether the index of each element is printed when displaying
 arrays.
 
+@anchor{set print nibbles}
+@item set print nibbles
+@itemx set print nibbles on
+Print binary values in groups of four bits, known as "nibbles",  when
+using the print command of @value{GDBN} with the option @samp{/t}.
+For example, this is what it looks like with @code{set print nibbles on}:
+
+@smallexample
+@group
+(@value{GDBP}) print val_flags
+$1 = 1230
+(@value{GDBP}) print/t val_flags
+$2 = 100 1100 1110
+@end group
+@end smallexample
+
+@item set print nibbles off
+Stop printing binary values in groups.  This is the default.
+
+@item show print nibbles
+Show whether to print binary values in groups of four bits.
+
 @anchor{set print elements}
 @item set print elements @var{number-of-elements}
 @itemx set print elements unlimited
diff --git a/gdb/printcmd.c b/gdb/printcmd.c
index 2fe3f4b0cc5..62713f1a91f 100644
--- a/gdb/printcmd.c
+++ b/gdb/printcmd.c
@@ -490,7 +490,7 @@ print_scalar_formatted (const gdb_byte *valaddr, struct type *type,
       break;
 
     case 't':
-      print_binary_chars (stream, valaddr, len, byte_order, size > 0);
+      print_binary_chars (stream, valaddr, len, byte_order, size > 0, options);
       break;
     case 'x':
       print_hex_chars (stream, valaddr, len, byte_order, size > 0);
diff --git a/gdb/testsuite/gdb.base/options.exp b/gdb/testsuite/gdb.base/options.exp
index 6b48a0f4a4b..1f331b270eb 100644
--- a/gdb/testsuite/gdb.base/options.exp
+++ b/gdb/testsuite/gdb.base/options.exp
@@ -167,6 +167,7 @@ proc_with_prefix test-print {{prefix ""}} {
 	"-array-indexes"
 	"-elements"
 	"-max-depth"
+	"-nibbles"
 	"-memory-tag-violations"
 	"-null-stop"
 	"-object"
diff --git a/gdb/testsuite/gdb.base/printcmds.exp b/gdb/testsuite/gdb.base/printcmds.exp
index 8967de3e948..0dbcf92815c 100644
--- a/gdb/testsuite/gdb.base/printcmds.exp
+++ b/gdb/testsuite/gdb.base/printcmds.exp
@@ -690,6 +690,19 @@ proc test_print_char_arrays {} {
     gdb_test_no_output "set print address off" "address off char arrays"
 }
 
+proc test_print_nibbles {} {
+    gdb_test_no_output "set print nibbles on"
+    gdb_test "p/t 0"		" = 0"
+    gdb_test "p/t 0x0"		" = 0"
+    gdb_test "p/t 0x30"		" = 11 0000"
+    gdb_test "p/t 0xff"		" = 1111 1111"
+    gdb_test "p/t 0x0f"		" = 1111"
+    gdb_test "p/t 0x01"		" = 1"
+    gdb_test "p/t 0xf0f"	" = 1111 0000 1111"
+    gdb_test "p/t 0x70f"	" = 111 0000 1111"
+    gdb_test_no_output "set print nibbles off"
+}
+
 proc test_print_string_constants {} {
     global gdb_prompt
 
@@ -1068,6 +1081,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 324055da93f..9f945f81a59 100644
--- a/gdb/valprint.c
+++ b/gdb/valprint.c
@@ -108,6 +108,7 @@ struct value_print_options user_print_options =
   0,				/* vtblprint */
   1,				/* unionprint */
   1,				/* addressprint */
+  0,				/* nibblesprint */
   0,				/* objectprint */
   PRINT_MAX_DEFAULT,		/* print_max */
   10,				/* repeat_count_threshold */
@@ -260,6 +261,15 @@ show_unionprint (struct ui_file *file, int from_tty,
 		    value);
 }
 
+static void
+show_nibbles (struct ui_file *file, int from_tty,
+		       struct cmd_list_element *c, const char *value)
+{
+  fprintf_filtered (file,
+		    _("Printing binary values in groups is %s.\n"),
+		    value);
+}
+
 /* If nonzero, causes machine addresses to be printed in certain contexts.  */
 
 static void
@@ -1367,7 +1377,8 @@ print_floating (const gdb_byte *valaddr, struct type *type,
 
 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;
@@ -1390,6 +1401,8 @@ print_binary_chars (struct ui_file *stream, const gdb_byte *valaddr,
 
 	  for (i = 0; i < (HOST_CHAR_BIT * sizeof (*p)); i++)
 	    {
+	      if (options->nibblesprint && seen_a_one && i % 4 == 0)
+		fputc_filtered (' ', stream);
 	      if (*p & (mask >> i))
 		b = '1';
 	      else
@@ -1410,6 +1423,8 @@ print_binary_chars (struct ui_file *stream, const gdb_byte *valaddr,
 	{
 	  for (i = 0; i < (HOST_CHAR_BIT * sizeof (*p)); i++)
 	    {
+	      if (options->nibblesprint && seen_a_one && i % 4 == 0)
+		fputc_filtered (' ', stream);
 	      if (*p & (mask >> i))
 		b = '1';
 	      else
@@ -3016,6 +3031,15 @@ static const gdb::option::option_def value_print_option_defs[] = {
     NULL, /* help_doc */
   },
 
+  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 e1dae2cc8fc..bb19c41ab68 100644
--- a/gdb/valprint.h
+++ b/gdb/valprint.h
@@ -44,6 +44,9 @@ struct value_print_options
   /* Controls printing of addresses.  */
   bool addressprint;
 
+  /* 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);
 
 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);
 
 extern void print_octal_chars (struct ui_file *, const gdb_byte *,
 			       unsigned int, enum bfd_endian);
-- 
2.30.2


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v3] gdb: Add new 'print nibbles' feature
  2021-08-25 13:24 [PATCH v3] gdb: Add new 'print nibbles' feature Enze Li
@ 2021-08-25 14:10 ` Eli Zaretskii
  2021-08-29  4:13   ` Enze Li
  2021-08-27 18:18 ` Tom Tromey
  1 sibling, 1 reply; 5+ messages in thread
From: Eli Zaretskii @ 2021-08-25 14:10 UTC (permalink / raw)
  To: Enze Li; +Cc: gdb-patches

> Date: Wed, 25 Aug 2021 21:24:16 +0800
> From: Enze Li via Gdb-patches <gdb-patches@sourceware.org>
> 
> Make an introduction of a new print setting that can be set by 'set
> print nibbles [on|off].  The default value is OFF, and it would be
> changed by users manually.
> 
> And of course, 'show print nibbles' is also included in the patch.
> 
> The new feature could display binary values in groups, and each group
> has four bits.

Thanks.

>   (gdb) print/t a
>   $3 = 100 1100 1110

Wouldn't it be better to show

    $3 = 0100 1100 1110

instead?

> --- a/gdb/NEWS
> +++ b/gdb/NEWS
> @@ -135,6 +135,11 @@ set debug event-loop
>  show debug event-loop
>    Control the display of debug output about GDB's event loop.
>  
> +set print nibbles [on|off]
> +show print nibbles
> +  This controls whether the 'print/t' command will display binary values
> +  in groups of four bits, known as "nibbles".  The default is 'off'.
> +

This part is OK.

> +@item -nibbles [@code{on}|@code{off}]
> +Set whether to print binary values in groups of four bits, known
> +as "nibbles". 
      ^^^^^^^^^
``nibbles'', please, to produce nicer quotes.

>                Related setting: @ref{set print nibbles}.

A lone "@ref" is seldom a good idea, except in the HTML output.
Suggest to say just @xref{set print nibbles}, without the "Related
setting" part.

> +@anchor{set print nibbles}
> +@item set print nibbles
> +@itemx set print nibbles on
> +Print binary values in groups of four bits, known as "nibbles",  when
                                                        ^^^^^^^^^
Here, I'd use @dfn{nibbles} instead of quotes, and add a @cindex for
"nibbles".

> +@item set print nibbles off
> +Stop printing binary values in groups.  This is the default.

I'd say "Don't" instead of "Stop".

Thanks.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v3] gdb: Add new 'print nibbles' feature
  2021-08-25 13:24 [PATCH v3] gdb: Add new 'print nibbles' feature Enze Li
  2021-08-25 14:10 ` Eli Zaretskii
@ 2021-08-27 18:18 ` Tom Tromey
  2021-08-29  4:46   ` Enze Li
  1 sibling, 1 reply; 5+ messages in thread
From: Tom Tromey @ 2021-08-27 18:18 UTC (permalink / raw)
  To: Enze Li via Gdb-patches

>>>>> "Enze" == Enze Li via Gdb-patches <gdb-patches@sourceware.org> writes:

Enze> Make an introduction of a new print setting that can be set by 'set
Enze> print nibbles [on|off].  The default value is OFF, and it would be
Enze> changed by users manually.

Enze> And of course, 'show print nibbles' is also included in the patch.

Thanks for the patch.

Enze>   (gdb) set print nibbles on
Enze>   (gdb) print/t a
Enze>   $3 = 100 1100 1110

I don't recall v1 or v2 of the patch, but I wonder why a space here.
C++14 uses a single quote, which IMO is ugly, but on the other hand
using this for C++ at least would mean that's GDB's output is C++-like
... conforming to the current language is normally how GDB works.  (And
round-tripping is nice, though in this case GDB doesn't accept that
syntax for input.)  For Rust, underscore would be a better choice.

I dunno, maybe that's overkill, or maybe it's comfortably added later
on.

Enze> +  /* Controls printing of nibbles.  */
Enze> +  bool nibblesprint;

The initializer for this uses "0" but should use "false":

Enze> +  0,				/* nibblesprint */


Other than the language thing, which can be discussed, I think the patch
looks fine.  Thanks.

Tom

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v3] gdb: Add new 'print nibbles' feature
  2021-08-25 14:10 ` Eli Zaretskii
@ 2021-08-29  4:13   ` Enze Li
  0 siblings, 0 replies; 5+ messages in thread
From: Enze Li @ 2021-08-29  4:13 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: gdb-patches

Hi Eli,

Thanks for the review.

On 8/25/21 10:10 PM, Eli Zaretskii wrote:
>>   (gdb) print/t a
>>   $3 = 100 1100 1110
> Wouldn't it be better to show
>
>     $3 = 0100 1100 1110
>
> instead?

That would be better. I've thought about it this way, too. However, this patch has been testing for a long time, and could we put this modification into the next commit?

Kind Regards

Enze




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v3] gdb: Add new 'print nibbles' feature
  2021-08-27 18:18 ` Tom Tromey
@ 2021-08-29  4:46   ` Enze Li
  0 siblings, 0 replies; 5+ messages in thread
From: Enze Li @ 2021-08-29  4:46 UTC (permalink / raw)
  To: Tom Tromey, Enze Li via Gdb-patches

Hi Tom,

Thanks for the review.

Oops, sorry about the delay! It's been a long time since the last submission.

Here's the address of the relevant content.

[PATCH v2] https://sourceware.org/pipermail/gdb-patches/2020-December/173974.html

[PATCH] https://sourceware.org/pipermail/gdb-patches/2020-December/173731.html

On 8/28/21 2:18 AM, Tom Tromey wrote:
> Enze>   (gdb) set print nibbles on
> Enze>   (gdb) print/t a
> Enze>   $3 = 100 1100 1110
>
> I don't recall v1 or v2 of the patch, but I wonder why a space here.
When I use GDB to debug a program, it is difficult to observe the value of bit bits. If the binary value is display in groups, it can increase binary readability.
> C++14 uses a single quote, which IMO is ugly, but on the other hand
> using this for C++ at least would mean that's GDB's output is C++-like
> ... conforming to the current language is normally how GDB works.  (And
> round-tripping is nice, though in this case GDB doesn't accept that
> syntax for input.)  For Rust, underscore would be a better choice.
>
> I dunno, maybe that's overkill, or maybe it's comfortably added later
> on.
>
Since this patch has been testing for a long time, could we put this modification into the commit next time?


Thanks & Kind Regards

Enze




^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-08-29  4:46 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-25 13:24 [PATCH v3] gdb: Add new 'print nibbles' feature Enze Li
2021-08-25 14:10 ` Eli Zaretskii
2021-08-29  4:13   ` Enze Li
2021-08-27 18:18 ` Tom Tromey
2021-08-29  4:46   ` 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).