public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH][PR gdb/24052] Implement 'set print zero-values on|off'
       [not found] <20200425134100.3818-1-ssbssa.ref@yahoo.de>
@ 2020-04-25 13:41 ` Hannes Domani
  2020-04-29 12:14   ` Philippe Waroquiers
  0 siblings, 1 reply; 4+ messages in thread
From: Hannes Domani @ 2020-04-25 13:41 UTC (permalink / raw)
  To: gdb-patches

With this option it's possible to suppress any zero value members when printing
a structure.

Consider this example:

(gdb) p t1
$1 = {
  i1 = 0,
  i2 = 0,
  i3 = 1,
  d1 = 0,
  d2 = 2.5,
  d3 = 0,
  p1 = 0x407098 <ix>,
  p2 = 0x0,
  p3 = 0x0,
  t1 = {
    v1 = 0,
    v2 = 0
  },
  t2 = {
    v1 = 3,
    v2 = 0
  },
  t3 = {
    v1 = 4,
    v2 = 5
  }
}

With suppressed zero value members, the ouput is concise:

(gdb) set print zero-values off
(gdb) p t1
$2 = {
  i3 = 1,
  d2 = 2.5,
  p1 = 0x407098 <ix>,
  t2 = {
    v1 = 3
  },
  t3 = {
    v1 = 4,
    v2 = 5
  }
}

gdb/ChangeLog:

2020-04-25  Hannes Domani  <ssbssa@yahoo.de>

	PR gdb/24052
	* cp-valprint.c (cp_print_value_fields): Skip zero value members
	if requested.
	* valprint.c (struct value_print_options): Add zero_value_print.
	(show_zero_value_print): New function.
	* valprint.h (struct value_print_options): Add zero_value_print.

gdb/doc/ChangeLog:

2020-04-25  Hannes Domani  <ssbssa@yahoo.de>

	PR gdb/24052
	* gdb.texinfo: Document 'print zero-values'.
---
 gdb/cp-valprint.c   | 21 +++++++++++++++++++++
 gdb/doc/gdb.texinfo | 18 +++++++++++++++++-
 gdb/valprint.c      | 23 ++++++++++++++++++++++-
 gdb/valprint.h      |  3 +++
 4 files changed, 63 insertions(+), 2 deletions(-)

diff --git a/gdb/cp-valprint.c b/gdb/cp-valprint.c
index 5625a58ee7..090f2627eb 100644
--- a/gdb/cp-valprint.c
+++ b/gdb/cp-valprint.c
@@ -194,6 +194,27 @@ cp_print_value_fields (struct value *val, struct ui_file *stream,
 	      && field_is_static (&TYPE_FIELD (type, i)))
 	    continue;
 
+	  /* If requested, skip printing of zero value fields.  */
+	  if (!options->zero_value_print
+	      && !field_is_static (&TYPE_FIELD (type, i)))
+	    {
+	      if (TYPE_FIELD_IGNORE (type, i))
+		continue;
+
+	      struct value *v = value_primitive_field (val, 0, i, type);
+	      struct type *field_type = check_typedef (value_type (v));
+	      const gdb_byte *field_addr = value_contents_for_printing (v);
+	      unsigned int field_len = TYPE_LENGTH (field_type);
+	      unsigned int zeros;
+	      for (zeros = 0; zeros < field_len; zeros++)
+		{
+		  if (field_addr[zeros] != 0)
+		    break;
+		}
+	      if (zeros == field_len)
+		continue;
+	    }
+
 	  if (fields_seen)
 	    {
 	      fputs_filtered (",", stream);
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 239c078af3..dd8340fa61 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -1978,7 +1978,7 @@ on @code{-} after the command name.  For example:
 (@value{GDBP}) print -@key{TAB}@key{TAB}
 -address         -max-depth       -raw-values      -union
 -array           -null-stop       -repeats         -vtbl
--array-indexes   -object          -static-members
+-array-indexes   -object          -static-members  -zero-values
 -elements        -pretty          -symbol
 @end smallexample
 
@@ -9754,6 +9754,10 @@ Set printing of unions interior to structures.  Related setting:
 @item -vtbl [@code{on}|@code{off}]
 Set printing of C++ virtual function tables.  Related setting:
 @ref{set print vtbl}.
+
+@item -zero-values [@code{on}|@code{off}]
+Set printing of zero value members. Related setting: @ref{set print
+zero-values}.
 @end table
 
 Because the @code{print} command accepts arbitrary expressions which
@@ -11543,6 +11547,18 @@ Do not pretty print C@t{++} virtual function tables.
 
 @item show print vtbl
 Show whether C@t{++} virtual function tables are pretty printed, or not.
+
+@anchor{set print zero-values}
+@item set print zero-values
+@itemx set print zero-values on
+@cindex zero value members
+Print zero value members of structures.  The default is on.
+
+@item set print zero-values off
+Do not print zero value members of structures.
+
+@item show print zero-values
+Show whether zero value members are printed or not.
 @end table
 
 @node Pretty Printing
diff --git a/gdb/valprint.c b/gdb/valprint.c
index d10b33ab0a..411c9c1448 100644
--- a/gdb/valprint.c
+++ b/gdb/valprint.c
@@ -117,7 +117,8 @@ struct value_print_options user_print_options =
   0,				/* summary */
   1,				/* symbol_print */
   PRINT_MAX_DEPTH_DEFAULT,	/* max_depth */
-  1				/* finish_print */
+  1,				/* finish_print */
+  1,				/* zero_value_print */
 };
 
 /* Initialize *OPTS to be a copy of the user print options.  */
@@ -2984,6 +2985,17 @@ show_static_field_print (struct ui_file *file, int from_tty,
 		    value);
 }
 
+/* Controls printing of zero value members.  */
+static void
+show_zero_value_print (struct ui_file *file, int from_tty,
+		       struct cmd_list_element *c,
+		       const char *value)
+{
+  fprintf_filtered (file,
+		    _("Printing of zero value members is %s.\n"),
+		    value);
+}
+
 \f
 
 /* A couple typedefs to make writing the options a bit more
@@ -3127,6 +3139,15 @@ pretty-printers for that value.")
     N_("Show printing of C++ virtual function tables."),
     NULL, /* help_doc */
   },
+
+  boolean_option_def {
+    "zero-values",
+    [] (value_print_options *opt) { return &opt->zero_value_print; },
+    show_zero_value_print, /* show_cmd_cb */
+    N_("Set printing of zero value members."),
+    N_("Show printing of zero value members."),
+    NULL, /* help_doc */
+  },
 };
 
 /* See valprint.h.  */
diff --git a/gdb/valprint.h b/gdb/valprint.h
index 57bc0339fc..915d8f3f7e 100644
--- a/gdb/valprint.h
+++ b/gdb/valprint.h
@@ -100,6 +100,9 @@ struct value_print_options
 
   /* Whether "finish" should print the value.  */
   bool finish_print;
+
+  /* If true, print fields with a zero value.  */
+  bool zero_value_print;
 };
 
 /* Create an option_def_group for the value_print options, with OPTS
-- 
2.26.2


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

* Re: [PATCH][PR gdb/24052] Implement 'set print zero-values on|off'
  2020-04-25 13:41 ` [PATCH][PR gdb/24052] Implement 'set print zero-values on|off' Hannes Domani
@ 2020-04-29 12:14   ` Philippe Waroquiers
  2020-04-29 12:43     ` Hannes Domani
  0 siblings, 1 reply; 4+ messages in thread
From: Philippe Waroquiers @ 2020-04-29 12:14 UTC (permalink / raw)
  To: Hannes Domani, gdb-patches

Nice idea.
Could that be applied on array also ?
(as for arrays containing zeroes here and there, -repeats 2 or 3
does not bring much in space gained).

Thanks
Philippe


On Sat, 2020-04-25 at 15:41 +0200, Hannes Domani via Gdb-patches wrote:
> With this option it's possible to suppress any zero value members when printing
> a structure.
> 
> Consider this example:
> 
> (gdb) p t1
> $1 = {
>   i1 = 0,
>   i2 = 0,
>   i3 = 1,
>   d1 = 0,
>   d2 = 2.5,
>   d3 = 0,
>   p1 = 0x407098 <ix>,
>   p2 = 0x0,
>   p3 = 0x0,
>   t1 = {
>     v1 = 0,
>     v2 = 0
>   },
>   t2 = {
>     v1 = 3,
>     v2 = 0
>   },
>   t3 = {
>     v1 = 4,
>     v2 = 5
>   }
> }
> 
> With suppressed zero value members, the ouput is concise:
> 
> (gdb) set print zero-values off
> (gdb) p t1
> $2 = {
>   i3 = 1,
>   d2 = 2.5,
>   p1 = 0x407098 <ix>,
>   t2 = {
>     v1 = 3
>   },
>   t3 = {
>     v1 = 4,
>     v2 = 5
>   }
> }
> 
> gdb/ChangeLog:
> 
> 2020-04-25  Hannes Domani  <ssbssa@yahoo.de>
> 
> 	PR gdb/24052
> 	* cp-valprint.c (cp_print_value_fields): Skip zero value members
> 	if requested.
> 	* valprint.c (struct value_print_options): Add zero_value_print.
> 	(show_zero_value_print): New function.
> 	* valprint.h (struct value_print_options): Add zero_value_print.
> 
> gdb/doc/ChangeLog:
> 
> 2020-04-25  Hannes Domani  <ssbssa@yahoo.de>
> 
> 	PR gdb/24052
> 	* gdb.texinfo: Document 'print zero-values'.
> ---
>  gdb/cp-valprint.c   | 21 +++++++++++++++++++++
>  gdb/doc/gdb.texinfo | 18 +++++++++++++++++-
>  gdb/valprint.c      | 23 ++++++++++++++++++++++-
>  gdb/valprint.h      |  3 +++
>  4 files changed, 63 insertions(+), 2 deletions(-)
> 
> diff --git a/gdb/cp-valprint.c b/gdb/cp-valprint.c
> index 5625a58ee7..090f2627eb 100644
> --- a/gdb/cp-valprint.c
> +++ b/gdb/cp-valprint.c
> @@ -194,6 +194,27 @@ cp_print_value_fields (struct value *val, struct ui_file *stream,
>  	      && field_is_static (&TYPE_FIELD (type, i)))
>  	    continue;
>  
> +	  /* If requested, skip printing of zero value fields.  */
> +	  if (!options->zero_value_print
> +	      && !field_is_static (&TYPE_FIELD (type, i)))
> +	    {
> +	      if (TYPE_FIELD_IGNORE (type, i))
> +		continue;
> +
> +	      struct value *v = value_primitive_field (val, 0, i, type);
> +	      struct type *field_type = check_typedef (value_type (v));
> +	      const gdb_byte *field_addr = value_contents_for_printing (v);
> +	      unsigned int field_len = TYPE_LENGTH (field_type);
> +	      unsigned int zeros;
> +	      for (zeros = 0; zeros < field_len; zeros++)
> +		{
> +		  if (field_addr[zeros] != 0)
> +		    break;
> +		}
> +	      if (zeros == field_len)
> +		continue;
> +	    }
> +
>  	  if (fields_seen)
>  	    {
>  	      fputs_filtered (",", stream);
> diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
> index 239c078af3..dd8340fa61 100644
> --- a/gdb/doc/gdb.texinfo
> +++ b/gdb/doc/gdb.texinfo
> @@ -1978,7 +1978,7 @@ on @code{-} after the command name.  For example:
>  (@value{GDBP}) print -@key{TAB}@key{TAB}
>  -address         -max-depth       -raw-values      -union
>  -array           -null-stop       -repeats         -vtbl
> --array-indexes   -object          -static-members
> +-array-indexes   -object          -static-members  -zero-values
>  -elements        -pretty          -symbol
>  @end smallexample
>  
> @@ -9754,6 +9754,10 @@ Set printing of unions interior to structures.  Related setting:
>  @item -vtbl [@code{on}|@code{off}]
>  Set printing of C++ virtual function tables.  Related setting:
>  @ref{set print vtbl}.
> +
> +@item -zero-values [@code{on}|@code{off}]
> +Set printing of zero value members. Related setting: @ref{set print
> +zero-values}.
>  @end table
>  
>  Because the @code{print} command accepts arbitrary expressions which
> @@ -11543,6 +11547,18 @@ Do not pretty print C@t{++} virtual function tables.
>  
>  @item show print vtbl
>  Show whether C@t{++} virtual function tables are pretty printed, or not.
> +
> +@anchor{set print zero-values}
> +@item set print zero-values
> +@itemx set print zero-values on
> +@cindex zero value members
> +Print zero value members of structures.  The default is on.
> +
> +@item set print zero-values off
> +Do not print zero value members of structures.
> +
> +@item show print zero-values
> +Show whether zero value members are printed or not.
>  @end table
>  
>  @node Pretty Printing
> diff --git a/gdb/valprint.c b/gdb/valprint.c
> index d10b33ab0a..411c9c1448 100644
> --- a/gdb/valprint.c
> +++ b/gdb/valprint.c
> @@ -117,7 +117,8 @@ struct value_print_options user_print_options =
>    0,				/* summary */
>    1,				/* symbol_print */
>    PRINT_MAX_DEPTH_DEFAULT,	/* max_depth */
> -  1				/* finish_print */
> +  1,				/* finish_print */
> +  1,				/* zero_value_print */
>  };
>  
>  /* Initialize *OPTS to be a copy of the user print options.  */
> @@ -2984,6 +2985,17 @@ show_static_field_print (struct ui_file *file, int from_tty,
>  		    value);
>  }
>  
> +/* Controls printing of zero value members.  */
> +static void
> +show_zero_value_print (struct ui_file *file, int from_tty,
> +		       struct cmd_list_element *c,
> +		       const char *value)
> +{
> +  fprintf_filtered (file,
> +		    _("Printing of zero value members is %s.\n"),
> +		    value);
> +}
> +
>  \f
>  
>  /* A couple typedefs to make writing the options a bit more
> @@ -3127,6 +3139,15 @@ pretty-printers for that value.")
>      N_("Show printing of C++ virtual function tables."),
>      NULL, /* help_doc */
>    },
> +
> +  boolean_option_def {
> +    "zero-values",
> +    [] (value_print_options *opt) { return &opt->zero_value_print; },
> +    show_zero_value_print, /* show_cmd_cb */
> +    N_("Set printing of zero value members."),
> +    N_("Show printing of zero value members."),
> +    NULL, /* help_doc */
> +  },
>  };
>  
>  /* See valprint.h.  */
> diff --git a/gdb/valprint.h b/gdb/valprint.h
> index 57bc0339fc..915d8f3f7e 100644
> --- a/gdb/valprint.h
> +++ b/gdb/valprint.h
> @@ -100,6 +100,9 @@ struct value_print_options
>  
>    /* Whether "finish" should print the value.  */
>    bool finish_print;
> +
> +  /* If true, print fields with a zero value.  */
> +  bool zero_value_print;
>  };
>  
>  /* Create an option_def_group for the value_print options, with OPTS


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

* Re: [PATCH][PR gdb/24052] Implement 'set print zero-values on|off'
  2020-04-29 12:14   ` Philippe Waroquiers
@ 2020-04-29 12:43     ` Hannes Domani
  2020-04-29 16:41       ` Philippe Waroquiers
  0 siblings, 1 reply; 4+ messages in thread
From: Hannes Domani @ 2020-04-29 12:43 UTC (permalink / raw)
  To: Gdb-patches

 Am Mittwoch, 29. April 2020, 14:14:33 MESZ hat Philippe Waroquiers <philippe.waroquiers@skynet.be> Folgendes geschrieben:

> Nice idea.

I also thought this was a good idea when I saw the PR.


> Could that be applied on array also ?
> (as for arrays containing zeroes here and there, -repeats 2 or 3
> does not bring much in space gained).

I guess it would be possible, if you don't care that the array indexes don't
match what you see once there are some missing entries.

You don't have that problem with structs, because each entry has a name.

Or maybe we show the array-index after each missing entry?


Hannes

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

* Re: [PATCH][PR gdb/24052] Implement 'set print zero-values on|off'
  2020-04-29 12:43     ` Hannes Domani
@ 2020-04-29 16:41       ` Philippe Waroquiers
  0 siblings, 0 replies; 4+ messages in thread
From: Philippe Waroquiers @ 2020-04-29 16:41 UTC (permalink / raw)
  To: Hannes Domani, Gdb-patches

On Wed, 2020-04-29 at 12:43 +0000, Hannes Domani via Gdb-patches wrote:
>  Am Mittwoch, 29. April 2020, 14:14:33 MESZ hat Philippe Waroquiers <philippe.waroquiers@skynet.be> Folgendes geschrieben:
> 
> > Nice idea.
> 
> I also thought this was a good idea when I saw the PR.
> 
> 
> > Could that be applied on array also ?
> > (as for arrays containing zeroes here and there, -repeats 2 or 3
> > does not bring much in space gained).
> 
> I guess it would be possible, if you don't care that the array indexes don't
> match what you see once there are some missing entries.
> 
> You don't have that problem with structs, because each entry has a name.
> 
> Or maybe we show the array-index after each missing entry?
Maybe, or user responsibility/choice to use -array-indexes or not.

Philippe



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

end of thread, other threads:[~2020-04-29 16:41 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <20200425134100.3818-1-ssbssa.ref@yahoo.de>
2020-04-25 13:41 ` [PATCH][PR gdb/24052] Implement 'set print zero-values on|off' Hannes Domani
2020-04-29 12:14   ` Philippe Waroquiers
2020-04-29 12:43     ` Hannes Domani
2020-04-29 16:41       ` Philippe Waroquiers

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