[RFA/java] Don't print overly long repeated elements in an array

Daniel Jacobowitz drow@mvista.com
Mon Apr 1 23:14:00 GMT 2002


The current Java array printer will print no more than ``set print
elements'' number of items to the screen.  This patch changes it to
print no more than that number total elements of the array, even if it
turns out to be far fewer screen items.  The difference is because Java
condenses repeated elements when printing.

The old behavior is more friendly; it lets you see that, of your ten
thousand array elements, only two are non-NULL.  However, if the
``length'' field was corrupt GDB was prone to going into an endless (or
very, very long) loop.  There was a several-MB blank area above the top
of the stack on my testcase... GDB tried to read it all.  I prefer to
be safe by default; it's easy enough to change the maximum via ``set
print elements'' if you know you're dealing with a large sparse array.

Is this OK?  Any other preference on how it should be done, Per?

-- 
Daniel Jacobowitz                           Carnegie Mellon University
MontaVista Software                         Debian GNU/Linux Developer

2002-04-02  Daniel Jacobowitz  <drow@mvista.com>

	* jv-valprint (java_value_print): Limit read array elements by
	print_max instead of printed arrray elements.

Index: jv-valprint.c
===================================================================
RCS file: /cvs/src/src/gdb/jv-valprint.c,v
retrieving revision 1.9
diff -u -p -r1.9 jv-valprint.c
--- jv-valprint.c	2001/10/21 01:57:42	1.9
+++ jv-valprint.c	2002/04/02 06:49:07
@@ -77,8 +77,7 @@ java_value_print (struct value *val, str
       && (i = strlen (name), name[i - 1] == ']'))
     {
       char buf4[4];
-      long length;
-      unsigned int things_printed = 0;
+      long length, original_length;
       int reps;
       struct type *el_type = java_primitive_type_from_name (name, i - 2);
 
@@ -88,6 +87,10 @@ java_value_print (struct value *val, str
       length = (long) extract_signed_integer (buf4, 4);
       fprintf_filtered (stream, "{length: %ld", length);
 
+      original_length = length;
+      if (length > print_max)
+	length = print_max;
+
       if (el_type == NULL)
 	{
 	  CORE_ADDR element;
@@ -95,7 +98,7 @@ java_value_print (struct value *val, str
 
 	  address += JAVA_OBJECT_SIZE + 4;	/* Skip object header and length. */
 
-	  while (i < length && things_printed < print_max)
+	  while (i < length)
 	    {
 	      char *buf;
 
@@ -131,7 +134,6 @@ java_value_print (struct value *val, str
 	      else
 		fprintf_filtered (stream, "@%s", paddr_nz (element));
 
-	      things_printed++;
 	      i += reps;
 	    }
 	}
@@ -143,7 +145,7 @@ java_value_print (struct value *val, str
 	  VALUE_ADDRESS (v) = address + JAVA_OBJECT_SIZE + 4;
 	  VALUE_ADDRESS (next_v) = VALUE_ADDRESS (v);
 
-	  while (i < length && things_printed < print_max)
+	  while (i < length)
 	    {
 	      fputs_filtered (", ", stream);
 	      wrap_here (n_spaces (2));
@@ -181,12 +183,11 @@ java_value_print (struct value *val, str
 	      val_print (VALUE_TYPE (v), VALUE_CONTENTS (v), 0, 0,
 			 stream, format, 2, 1, pretty);
 
-	      things_printed++;
 	      i += reps;
 	    }
 	}
 
-      if (i < length)
+      if (length < original_length)
 	fprintf_filtered (stream, "...");
 
       fprintf_filtered (stream, "}");



More information about the Gdb-patches mailing list