1 /* Support for printing Java values for GDB, the GNU debugger.
2 Copyright 1997, 1998, 1999, 2000 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,
19 Boston, MA 02111-1307, USA. */
25 #include "expression.h"
36 static void java_print_value_fields (struct type * type, char *valaddr,
38 struct ui_file *stream, int format,
40 enum val_prettyprint pretty);
44 java_value_print (struct value *val, struct ui_file *stream, int format,
45 enum val_prettyprint pretty)
52 type = VALUE_TYPE (val);
53 address = VALUE_ADDRESS (val) + VALUE_OFFSET (val);
55 if (is_object_type (type))
59 /* Get the run-time type, and cast the object into that */
61 obj_addr = unpack_pointer (type, VALUE_CONTENTS (val));
65 type = type_from_class (java_class_from_object (val));
66 type = lookup_pointer_type (type);
68 val = value_at (type, address, NULL);
72 if (TYPE_CODE (type) == TYPE_CODE_PTR && !value_logical_not (val))
73 type_print (TYPE_TARGET_TYPE (type), "", stream, -1);
75 name = TYPE_TAG_NAME (type);
76 if (TYPE_CODE (type) == TYPE_CODE_STRUCT && name != NULL
77 && (i = strlen (name), name[i - 1] == ']'))
81 unsigned int things_printed = 0;
83 struct type *el_type = java_primitive_type_from_name (name, i - 2);
86 read_memory (address + JAVA_OBJECT_SIZE, buf4, 4);
88 length = (long) extract_signed_integer (buf4, 4);
89 fprintf_filtered (stream, "{length: %ld", length);
94 CORE_ADDR next_element = -1; /* dummy initial value */
96 address += JAVA_OBJECT_SIZE + 4; /* Skip object header and length. */
98 while (i < length && things_printed < print_max)
102 buf = alloca (TARGET_PTR_BIT / HOST_CHAR_BIT);
103 fputs_filtered (", ", stream);
104 wrap_here (n_spaces (2));
107 element = next_element;
110 read_memory (address, buf, sizeof (buf));
111 address += TARGET_PTR_BIT / HOST_CHAR_BIT;
112 element = extract_address (buf, sizeof (buf));
115 for (reps = 1; i + reps < length; reps++)
117 read_memory (address, buf, sizeof (buf));
118 address += TARGET_PTR_BIT / HOST_CHAR_BIT;
119 next_element = extract_address (buf, sizeof (buf));
120 if (next_element != element)
125 fprintf_filtered (stream, "%d: ", i);
127 fprintf_filtered (stream, "%d..%d: ", i, i + reps - 1);
130 fprintf_filtered (stream, "null");
132 fprintf_filtered (stream, "@%s", paddr_nz (element));
140 struct value *v = allocate_value (el_type);
141 struct value *next_v = allocate_value (el_type);
143 VALUE_ADDRESS (v) = address + JAVA_OBJECT_SIZE + 4;
144 VALUE_ADDRESS (next_v) = VALUE_ADDRESS (v);
146 while (i < length && things_printed < print_max)
148 fputs_filtered (", ", stream);
149 wrap_here (n_spaces (2));
162 VALUE_OFFSET (v) = 0;
165 VALUE_OFFSET (next_v) = VALUE_OFFSET (v);
167 for (reps = 1; i + reps < length; reps++)
169 VALUE_LAZY (next_v) = 1;
170 VALUE_OFFSET (next_v) += TYPE_LENGTH (el_type);
171 if (memcmp (VALUE_CONTENTS (v), VALUE_CONTENTS (next_v),
172 TYPE_LENGTH (el_type)) != 0)
177 fprintf_filtered (stream, "%d: ", i);
179 fprintf_filtered (stream, "%d..%d: ", i, i + reps - 1);
181 val_print (VALUE_TYPE (v), VALUE_CONTENTS (v), 0, 0,
182 stream, format, 2, 1, pretty);
190 fprintf_filtered (stream, "...");
192 fprintf_filtered (stream, "}");
197 /* If it's type String, print it */
199 if (TYPE_CODE (type) == TYPE_CODE_PTR
200 && TYPE_TARGET_TYPE (type)
201 && TYPE_NAME (TYPE_TARGET_TYPE (type))
202 && strcmp (TYPE_NAME (TYPE_TARGET_TYPE (type)), "java.lang.String") == 0
203 && (format == 0 || format == 's')
205 && value_as_address (val) != 0)
207 struct value *data_val;
209 struct value *boffset_val;
210 unsigned long boffset;
211 struct value *count_val;
215 mark = value_mark (); /* Remember start of new values */
217 data_val = value_struct_elt (&val, NULL, "data", NULL, NULL);
218 data = value_as_address (data_val);
220 boffset_val = value_struct_elt (&val, NULL, "boffset", NULL, NULL);
221 boffset = value_as_address (boffset_val);
223 count_val = value_struct_elt (&val, NULL, "count", NULL, NULL);
224 count = value_as_address (count_val);
226 value_free_to_mark (mark); /* Release unnecessary values */
228 val_print_string (data + boffset, count, 2, stream);
233 return (val_print (type, VALUE_CONTENTS (val), 0, address,
234 stream, format, 1, 0, pretty));
237 /* TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and PRETTY have the
238 same meanings as in cp_print_value and c_val_print.
240 DONT_PRINT is an array of baseclass types that we
241 should not print, or zero if called from top level. */
244 java_print_value_fields (struct type *type, char *valaddr, CORE_ADDR address,
245 struct ui_file *stream, int format, int recurse,
246 enum val_prettyprint pretty)
248 int i, len, n_baseclasses;
250 CHECK_TYPEDEF (type);
252 fprintf_filtered (stream, "{");
253 len = TYPE_NFIELDS (type);
254 n_baseclasses = TYPE_N_BASECLASSES (type);
256 if (n_baseclasses > 0)
258 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
260 for (i = 0; i < n_baseclasses; i++)
263 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
264 char *basename = TYPE_NAME (baseclass);
267 if (BASETYPE_VIA_VIRTUAL (type, i))
270 if (basename != NULL && strcmp (basename, "java.lang.Object") == 0)
277 fprintf_filtered (stream, "\n");
278 print_spaces_filtered (2 * (recurse + 1), stream);
280 fputs_filtered ("<", stream);
281 /* Not sure what the best notation is in the case where there is no
283 fputs_filtered (basename ? basename : "", stream);
284 fputs_filtered ("> = ", stream);
286 base_valaddr = valaddr;
288 java_print_value_fields (baseclass, base_valaddr, address + boffset,
289 stream, format, recurse + 1, pretty);
290 fputs_filtered (", ", stream);
298 if (!len && n_baseclasses == 1)
299 fprintf_filtered (stream, "<No data fields>");
302 extern int inspect_it;
305 for (i = n_baseclasses; i < len; i++)
307 /* If requested, skip printing of static fields. */
308 if (TYPE_FIELD_STATIC (type, i))
310 char *name = TYPE_FIELD_NAME (type, i);
311 if (!static_field_print)
313 if (name != NULL && strcmp (name, "class") == 0)
317 fprintf_filtered (stream, ", ");
318 else if (n_baseclasses > 0)
322 fprintf_filtered (stream, "\n");
323 print_spaces_filtered (2 + 2 * recurse, stream);
324 fputs_filtered ("members of ", stream);
325 fputs_filtered (type_name_no_tag (type), stream);
326 fputs_filtered (": ", stream);
333 fprintf_filtered (stream, "\n");
334 print_spaces_filtered (2 + 2 * recurse, stream);
338 wrap_here (n_spaces (2 + 2 * recurse));
342 if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
343 fputs_filtered ("\"( ptr \"", stream);
345 fputs_filtered ("\"( nodef \"", stream);
346 if (TYPE_FIELD_STATIC (type, i))
347 fputs_filtered ("static ", stream);
348 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
350 DMGL_PARAMS | DMGL_ANSI);
351 fputs_filtered ("\" \"", stream);
352 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
354 DMGL_PARAMS | DMGL_ANSI);
355 fputs_filtered ("\") \"", stream);
359 annotate_field_begin (TYPE_FIELD_TYPE (type, i));
361 if (TYPE_FIELD_STATIC (type, i))
362 fputs_filtered ("static ", stream);
363 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
365 DMGL_PARAMS | DMGL_ANSI);
366 annotate_field_name_end ();
367 fputs_filtered (": ", stream);
368 annotate_field_value ();
371 if (!TYPE_FIELD_STATIC (type, i) && TYPE_FIELD_PACKED (type, i))
375 /* Bitfields require special handling, especially due to byte
377 if (TYPE_FIELD_IGNORE (type, i))
379 fputs_filtered ("<optimized out or zero length>", stream);
383 v = value_from_longest (TYPE_FIELD_TYPE (type, i),
384 unpack_field_as_long (type, valaddr, i));
386 val_print (TYPE_FIELD_TYPE (type, i), VALUE_CONTENTS (v), 0,
387 0, stream, format, 0, recurse + 1, pretty);
392 if (TYPE_FIELD_IGNORE (type, i))
394 fputs_filtered ("<optimized out or zero length>", stream);
396 else if (TYPE_FIELD_STATIC (type, i))
398 struct value *v = value_static_field (type, i);
400 fputs_filtered ("<optimized out>", stream);
403 struct type *t = check_typedef (VALUE_TYPE (v));
404 if (TYPE_CODE (t) == TYPE_CODE_STRUCT)
406 val_print (VALUE_TYPE (v),
407 VALUE_CONTENTS (v), 0, VALUE_ADDRESS (v),
408 stream, format, 0, recurse + 1, pretty);
411 else if (TYPE_FIELD_TYPE (type, i) == NULL)
412 fputs_filtered ("<unknown type>", stream);
415 val_print (TYPE_FIELD_TYPE (type, i),
416 valaddr + TYPE_FIELD_BITPOS (type, i) / 8, 0,
417 address + TYPE_FIELD_BITPOS (type, i) / 8,
418 stream, format, 0, recurse + 1, pretty);
421 annotate_field_end ();
426 fprintf_filtered (stream, "\n");
427 print_spaces_filtered (2 * recurse, stream);
430 fprintf_filtered (stream, "}");
433 /* Print data of type TYPE located at VALADDR (within GDB), which came from
434 the inferior at address ADDRESS, onto stdio stream STREAM according to
435 FORMAT (a letter or 0 for natural format). The data at VALADDR is in
438 If the data are a string pointer, returns the number of string characters
441 If DEREF_REF is nonzero, then dereference references, otherwise just print
444 The PRETTY parameter controls prettyprinting. */
447 java_val_print (struct type *type, char *valaddr, int embedded_offset,
448 CORE_ADDR address, struct ui_file *stream, int format,
449 int deref_ref, int recurse, enum val_prettyprint pretty)
451 register unsigned int i = 0; /* Number of characters printed */
452 struct type *target_type;
455 CHECK_TYPEDEF (type);
456 switch (TYPE_CODE (type))
459 if (format && format != 's')
461 print_scalar_formatted (valaddr, type, format, 0, stream);
465 if (vtblprint && cp_is_vtbl_ptr_type (type))
467 /* Print the unmangled name if desired. */
468 /* Print vtable entry - we only get here if we ARE using
469 -fvtable_thunks. (Otherwise, look under TYPE_CODE_STRUCT.) */
470 print_address_demangle (extract_address (valaddr, TYPE_LENGTH (type)),
475 addr = unpack_pointer (type, valaddr);
478 fputs_filtered ("null", stream);
481 target_type = check_typedef (TYPE_TARGET_TYPE (type));
483 if (TYPE_CODE (target_type) == TYPE_CODE_FUNC)
485 /* Try to print what function it points to. */
486 print_address_demangle (addr, stream, demangle);
487 /* Return value is irrelevant except for string pointers. */
491 if (addressprint && format != 's')
493 fputs_filtered ("@", stream);
494 print_longest (stream, 'x', 0, (ULONGEST) addr);
501 /* Can't just call c_val_print because that prints bytes as C
503 format = format ? format : output_format;
505 print_scalar_formatted (valaddr, type, format, 0, stream);
506 else if (TYPE_CODE (type) == TYPE_CODE_CHAR
507 || (TYPE_CODE (type) == TYPE_CODE_INT
508 && TYPE_LENGTH (type) == 2
509 && strcmp (TYPE_NAME (type), "char") == 0))
510 LA_PRINT_CHAR ((int) unpack_long (type, valaddr), stream);
512 val_print_type_code_int (type, valaddr, stream);
515 case TYPE_CODE_STRUCT:
516 java_print_value_fields (type, valaddr, address, stream, format,
521 return c_val_print (type, valaddr, embedded_offset, address, stream,
522 format, deref_ref, recurse, pretty);