[PATCH][PR gdb/24052] Implement 'set print zero-values on|off'

Hannes Domani ssbssa@yahoo.de
Sat Apr 25 13:41:00 GMT 2020


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);
+}
+
 
 
 /* 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



More information about the Gdb-patches mailing list