This is the mail archive of the gdb-patches@sources.redhat.com mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Option to elide single-bit bitfields when printing structures


GCC has lots of single-bit bitfields in its data structures.  When you
print them out from the debugger they tend to look like this:

(gdb) p decl->common
$1 = {chain = 0x40253000, type = 0x40253138, code = FUNCTION_DECL, 
  side_effects_flag = 0, constant_flag = 0, addressable_flag = 0, 
  volatile_flag = 0, readonly_flag = 0, unsigned_flag = 0, 
  asm_written_flag = 0, used_flag = 0, nothrow_flag = 0, static_flag = 0, 
  public_flag = 1, private_flag = 0, protected_flag = 0, bounded_flag = 0, 
  lang_flag_0 = 0, lang_flag_1 = 0, lang_flag_2 = 0, lang_flag_3 = 0, 
  lang_flag_4 = 0, lang_flag_5 = 0, lang_flag_6 = 0, dummy = 0}

It's hard to see which are set and which aren't.  With this patch, you
can get it printed like this:

(gdb) set print elide-bitflags on
(gdb) p decl->common
$2 = {chain = 0x40253000, type = 0x40253138, code = FUNCTION_DECL, public_flag}

which is, IMHO, much easier to read.

Better ideas for the command name and help text would be k3wl.

zw

	* cp-valprint.c (elide_bitflag_print): New flag.
	(cp_print_value_fields): If elide_bitflag_print, skip printing
	single-bit fields whose value is zero entirely, and skip
	printing the value for single-bit fields whose value is one.
	(_initialize_cp_valprint): Add 'set print elide-bitflags'
	command, default to old behavior.

===================================================================
Index: cp-valprint.c
--- cp-valprint.c	2000/12/15 01:01:46	1.6
+++ cp-valprint.c	2001/02/22 19:50:49
@@ -41,6 +41,7 @@ int vtblprint;			/* Controls printing of
 int objectprint;		/* Controls looking up an object's derived type
 				   using what we find in its vtables.  */
 int static_field_print;		/* Controls printing of static fields. */
+int elide_bitflag_print;	/* Controls printing of single bit flags.  */
 
 static struct obstack dont_print_vb_obstack;
 static struct obstack dont_print_statmem_obstack;
@@ -273,6 +274,15 @@ cp_print_value_fields (struct type *type
 	  obstack_finish (&dont_print_statmem_obstack);
 	}
 
+      if (n_baseclasses > 0 && pretty)
+	{
+	  fprintf_filtered (stream, "\n");
+	  print_spaces_filtered (2 + 2 * recurse, stream);
+	  fputs_filtered ("members of ", stream);
+	  fputs_filtered (type_name_no_tag (type), stream);
+	  fputs_filtered (": ", stream);
+	}
+
       for (i = n_baseclasses; i < len; i++)
 	{
 	  /* If requested, skip printing of static fields.  */
@@ -280,22 +290,22 @@ cp_print_value_fields (struct type *type
 	    continue;
 
 	  /* If a vtable pointer appears, we'll print it out later */
-	  if (TYPE_HAS_VTABLE (type) && STREQN (TYPE_FIELD_NAME (type, i), hpacc_vtbl_ptr_name, 5))
+	  if (TYPE_HAS_VTABLE (type) && STREQN (TYPE_FIELD_NAME (type, i),
+						hpacc_vtbl_ptr_name, 5))
 	    continue;
 
+	  /* Elide single-bit bitfields with value zero */
+	  if (elide_bitflag_print
+	      && TYPE_FIELD_PACKED (type, i)
+	      && TYPE_FIELD_BITSIZE (type, i) == 1)
+	    {
+	      LONGEST v = unpack_field_as_long (type, valaddr + offset, i);
+	      if (v == 0)
+		continue;
+	    }
+
 	  if (fields_seen)
 	    fprintf_filtered (stream, ", ");
-	  else if (n_baseclasses > 0)
-	    {
-	      if (pretty)
-		{
-		  fprintf_filtered (stream, "\n");
-		  print_spaces_filtered (2 + 2 * recurse, stream);
-		  fputs_filtered ("members of ", stream);
-		  fputs_filtered (type_name_no_tag (type), stream);
-		  fputs_filtered (": ", stream);
-		}
-	    }
 	  fields_seen = 1;
 
 	  if (pretty)
@@ -334,8 +344,11 @@ cp_print_value_fields (struct type *type
 				       language_cplus,
 				       DMGL_PARAMS | DMGL_ANSI);
 	      annotate_field_name_end ();
-	      /* do not print leading '=' in case of anonymous unions */
-	      if (strcmp (TYPE_FIELD_NAME (type, i), ""))
+	      /* do not print leading '=' in case of anonymous unions,
+		 or for 1-bit bitfields where we just state the name */
+	      if (strcmp (TYPE_FIELD_NAME (type, i), "")
+		  && (!elide_bitflag_print || !TYPE_FIELD_PACKED (type, i)
+		      || TYPE_FIELD_BITSIZE (type, i) != 1))
 		fputs_filtered (" = ", stream);
 	      annotate_field_value ();
 	    }
@@ -350,7 +363,9 @@ cp_print_value_fields (struct type *type
 		{
 		  fputs_filtered ("<optimized out or zero length>", stream);
 		}
-	      else
+	      /* Don't print a value for 1-bit bitfields; the name suffices.  */
+	      else if (!elide_bitflag_print
+		       || TYPE_FIELD_BITSIZE (type, i) != 1)
 		{
 		  v = value_from_longest (TYPE_FIELD_TYPE (type, i),
 			  unpack_field_as_long (type, valaddr + offset, i));
@@ -787,9 +802,17 @@ _initialize_cp_valprint (void)
 		  &setprintlist),
      &showprintlist);
 
+  add_show_from_set
+    (add_set_cmd ("elide-bitflags", class_support, var_boolean,
+		  (char *) &elide_bitflag_print,
+		  "Set hiding of single-bit flags which are zero.",
+		  &setprintlist),
+     &showprintlist);
+
   /* Give people the defaults which they are used to.  */
   objectprint = 0;
   vtblprint = 0;
+  elide_bitflag_print = 0;
   obstack_begin (&dont_print_vb_obstack, 32 * sizeof (struct type *));
   obstack_specify_allocation (&dont_print_statmem_obstack,
 			      32 * sizeof (CORE_ADDR), sizeof (CORE_ADDR),


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]