1 /* Support for printing Java values for GDB, the GNU debugger.
2 Copyright 1997, 1998, 1999 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
24 #include "expression.h"
34 java_value_print (val, stream, format, pretty)
38 enum val_prettyprint pretty;
45 type = VALUE_TYPE (val);
46 address = VALUE_ADDRESS (val) + VALUE_OFFSET (val);
48 if (is_object_type (type))
52 /* Get the run-time type, and cast the object into that */
54 obj_addr = unpack_pointer (type, VALUE_CONTENTS (val));
58 type = type_from_class (java_class_from_object (val));
59 type = lookup_pointer_type (type);
61 val = value_at (type, address, NULL);
65 if (TYPE_CODE (type) == TYPE_CODE_PTR && ! value_logical_not (val))
66 type_print (TYPE_TARGET_TYPE (type), "", stream, -1);
68 name = TYPE_TAG_NAME (type);
69 if (TYPE_CODE (type) == TYPE_CODE_STRUCT && name != NULL
70 && (i = strlen (name), name[i-1] == ']'))
74 unsigned int things_printed = 0;
76 struct type *el_type = java_primitive_type_from_name (name, i - 2);
79 read_memory (address + JAVA_OBJECT_SIZE, buf4, 4);
81 length = (long) extract_signed_integer (buf4, 4);
82 fprintf_filtered (stream, "{length: %ld", length);
86 CORE_ADDR element, next_element;
88 address += JAVA_OBJECT_SIZE + 4; /* Skip object header and length. */
90 while (i < length && things_printed < print_max)
92 char buf[TARGET_PTR_BIT / HOST_CHAR_BIT];
94 fputs_filtered (", ", stream);
95 wrap_here (n_spaces (2));
98 element = next_element;
101 read_memory (address, buf, sizeof(buf));
102 address += TARGET_PTR_BIT / HOST_CHAR_BIT;
103 element = extract_address (buf, sizeof(buf));
106 for (reps = 1; i + reps < length; reps++)
108 read_memory (address, buf, sizeof(buf));
109 address += TARGET_PTR_BIT / HOST_CHAR_BIT;
110 next_element = extract_address (buf, sizeof(buf));
111 if (next_element != element)
116 fprintf_filtered (stream, "%d: ", i);
118 fprintf_filtered (stream, "%d..%d: ", i, i + reps - 1);
121 fprintf_filtered (stream, "null");
123 fprintf_filtered (stream, "@%x", element);
131 value_ptr v = allocate_value (el_type);
132 value_ptr next_v = allocate_value (el_type);
134 VALUE_ADDRESS (v) = address + JAVA_OBJECT_SIZE + 4;
135 VALUE_ADDRESS (next_v) = VALUE_ADDRESS (v);
137 while (i < length && things_printed < print_max)
139 fputs_filtered (", ", stream);
140 wrap_here (n_spaces (2));
153 VALUE_OFFSET (v) = 0;
156 VALUE_OFFSET (next_v) = VALUE_OFFSET (v);
158 for (reps = 1; i + reps < length; reps++)
160 VALUE_LAZY (next_v) = 1;
161 VALUE_OFFSET (next_v) += TYPE_LENGTH (el_type);
162 if (memcmp (VALUE_CONTENTS (v), VALUE_CONTENTS (next_v),
163 TYPE_LENGTH (el_type)) != 0)
168 fprintf_filtered (stream, "%d: ", i);
170 fprintf_filtered (stream, "%d..%d: ", i, i + reps - 1);
172 val_print (VALUE_TYPE (v), VALUE_CONTENTS (v), 0, 0,
173 stream, format, 2, 1, pretty);
181 fprintf_filtered (stream, "...");
183 fprintf_filtered (stream, "}");
188 /* If it's type String, print it */
190 if (TYPE_CODE (type) == TYPE_CODE_PTR
191 && TYPE_TARGET_TYPE (type)
192 && TYPE_NAME (TYPE_TARGET_TYPE (type))
193 && strcmp (TYPE_NAME (TYPE_TARGET_TYPE (type)), "java.lang.String") == 0
194 && (format == 0 || format == 's')
199 value_ptr boffset_val;
200 unsigned long boffset;
205 mark = value_mark (); /* Remember start of new values */
207 data_val = value_struct_elt (&val, NULL, "data", NULL, NULL);
208 data = value_as_pointer (data_val);
210 boffset_val = value_struct_elt (&val, NULL, "boffset", NULL, NULL);
211 boffset = value_as_pointer (boffset_val);
213 count_val = value_struct_elt (&val, NULL, "count", NULL, NULL);
214 count = value_as_pointer (count_val);
216 value_free_to_mark (mark); /* Release unnecessary values */
218 val_print_string (data + boffset, count, 2, stream);
223 return (val_print (type, VALUE_CONTENTS (val), 0, address,
224 stream, format, 1, 0, pretty));
227 /* TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and PRETTY have the
228 same meanings as in cp_print_value and c_val_print.
230 DONT_PRINT is an array of baseclass types that we
231 should not print, or zero if called from top level. */
234 java_print_value_fields (type, valaddr, address, stream,
235 format, recurse, pretty)
242 enum val_prettyprint pretty;
244 int i, len, n_baseclasses;
246 CHECK_TYPEDEF (type);
248 fprintf_filtered (stream, "{");
249 len = TYPE_NFIELDS (type);
250 n_baseclasses = TYPE_N_BASECLASSES (type);
252 if (n_baseclasses > 0)
254 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
256 for (i = 0; i < n_baseclasses; i++)
259 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
260 char *basename = TYPE_NAME (baseclass);
263 if (BASETYPE_VIA_VIRTUAL (type, i))
266 if (basename != NULL && strcmp (basename, "java.lang.Object") == 0)
273 fprintf_filtered (stream, "\n");
274 print_spaces_filtered (2 * (recurse+1), stream);
276 fputs_filtered ("<", stream);
277 /* Not sure what the best notation is in the case where there is no
279 fputs_filtered (basename ? basename : "", stream);
280 fputs_filtered ("> = ", stream);
282 base_valaddr = valaddr;
284 java_print_value_fields (baseclass, base_valaddr, address + boffset,
285 stream, format, recurse+1, pretty);
286 fputs_filtered (", ", stream);
294 if (!len && n_baseclasses == 1)
295 fprintf_filtered (stream, "<No data fields>");
298 extern int inspect_it;
301 for (i = n_baseclasses; i < len; i++)
303 /* If requested, skip printing of static fields. */
304 if (TYPE_FIELD_STATIC (type, i))
306 char *name = TYPE_FIELD_NAME (type, i);
307 if (!static_field_print)
309 if (name != NULL && strcmp (name, "class") == 0)
313 fprintf_filtered (stream, ", ");
314 else if (n_baseclasses > 0)
318 fprintf_filtered (stream, "\n");
319 print_spaces_filtered (2 + 2 * recurse, stream);
320 fputs_filtered ("members of ", stream);
321 fputs_filtered (type_name_no_tag (type), stream);
322 fputs_filtered (": ", stream);
329 fprintf_filtered (stream, "\n");
330 print_spaces_filtered (2 + 2 * recurse, stream);
334 wrap_here (n_spaces (2 + 2 * recurse));
338 if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
339 fputs_filtered ("\"( ptr \"", stream);
341 fputs_filtered ("\"( nodef \"", stream);
342 if (TYPE_FIELD_STATIC (type, i))
343 fputs_filtered ("static ", stream);
344 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
346 DMGL_PARAMS | DMGL_ANSI);
347 fputs_filtered ("\" \"", stream);
348 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
350 DMGL_PARAMS | DMGL_ANSI);
351 fputs_filtered ("\") \"", stream);
355 annotate_field_begin (TYPE_FIELD_TYPE (type, i));
357 if (TYPE_FIELD_STATIC (type, i))
358 fputs_filtered ("static ", stream);
359 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
361 DMGL_PARAMS | DMGL_ANSI);
362 annotate_field_name_end ();
363 fputs_filtered (": ", stream);
364 annotate_field_value ();
367 if (!TYPE_FIELD_STATIC (type, i) && TYPE_FIELD_PACKED (type, i))
371 /* Bitfields require special handling, especially due to byte
373 if (TYPE_FIELD_IGNORE (type, i))
375 fputs_filtered ("<optimized out or zero length>", stream);
379 v = value_from_longest (TYPE_FIELD_TYPE (type, i),
380 unpack_field_as_long (type, valaddr, i));
382 val_print (TYPE_FIELD_TYPE(type, i), VALUE_CONTENTS (v), 0,
383 0, stream, format, 0, recurse + 1, pretty);
388 if (TYPE_FIELD_IGNORE (type, i))
390 fputs_filtered ("<optimized out or zero length>", stream);
392 else if (TYPE_FIELD_STATIC (type, i))
394 value_ptr v = value_static_field (type, i);
396 fputs_filtered ("<optimized out>", stream);
399 struct type *t = check_typedef (VALUE_TYPE (v));
400 if (TYPE_CODE (t) == TYPE_CODE_STRUCT)
402 val_print (VALUE_TYPE (v),
403 VALUE_CONTENTS (v), 0, VALUE_ADDRESS (v),
404 stream, format, 0, recurse+1, pretty);
407 else if (TYPE_FIELD_TYPE (type, i) == NULL)
408 fputs_filtered ("<unknown type>", stream);
411 val_print (TYPE_FIELD_TYPE (type, i),
412 valaddr + TYPE_FIELD_BITPOS (type, i) / 8, 0,
413 address + TYPE_FIELD_BITPOS (type, i) / 8,
414 stream, format, 0, recurse + 1, pretty);
417 annotate_field_end ();
422 fprintf_filtered (stream, "\n");
423 print_spaces_filtered (2 * recurse, stream);
426 fprintf_filtered (stream, "}");
429 /* Print data of type TYPE located at VALADDR (within GDB), which came from
430 the inferior at address ADDRESS, onto stdio stream STREAM according to
431 FORMAT (a letter or 0 for natural format). The data at VALADDR is in
434 If the data are a string pointer, returns the number of string characters
437 If DEREF_REF is nonzero, then dereference references, otherwise just print
440 The PRETTY parameter controls prettyprinting. */
443 java_val_print (type, valaddr, embedded_offset, address, stream, format,
444 deref_ref, recurse, pretty)
452 enum val_prettyprint pretty;
454 register unsigned int i = 0; /* Number of characters printed */
455 struct type *target_type;
458 CHECK_TYPEDEF (type);
459 switch (TYPE_CODE (type))
462 if (format && format != 's')
464 print_scalar_formatted (valaddr, type, format, 0, stream);
468 if (vtblprint && cp_is_vtbl_ptr_type(type))
470 /* Print the unmangled name if desired. */
471 /* Print vtable entry - we only get here if we ARE using
472 -fvtable_thunks. (Otherwise, look under TYPE_CODE_STRUCT.) */
473 print_address_demangle(extract_address (valaddr, TYPE_LENGTH (type)),
478 addr = unpack_pointer (type, valaddr);
481 fputs_filtered ("null", stream);
484 target_type = check_typedef (TYPE_TARGET_TYPE (type));
486 if (TYPE_CODE (target_type) == TYPE_CODE_FUNC)
488 /* Try to print what function it points to. */
489 print_address_demangle (addr, stream, demangle);
490 /* Return value is irrelevant except for string pointers. */
494 if (addressprint && format != 's')
496 fputs_filtered ("@", stream);
497 print_longest (stream, 'x', 0, (ULONGEST) addr);
503 format = format ? format : output_format;
505 print_scalar_formatted (valaddr, type, format, 0, stream);
507 LA_PRINT_CHAR ((int) unpack_long (type, valaddr), stream);
511 /* Can't just call c_val_print because that print bytes as C chars. */
512 format = format ? format : output_format;
514 print_scalar_formatted (valaddr, type, format, 0, stream);
516 val_print_type_code_int (type, valaddr, stream);
519 case TYPE_CODE_STRUCT:
520 java_print_value_fields (type, valaddr, address, stream, format,
525 return c_val_print (type, valaddr, embedded_offset, address, stream,
526 format, deref_ref, recurse, pretty);