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"
33 #include "gdb_string.h"
37 static void java_print_value_fields (struct type * type, char *valaddr,
39 struct ui_file *stream, int format,
41 enum val_prettyprint pretty);
45 java_value_print (struct value *val, struct ui_file *stream, int format,
46 enum val_prettyprint pretty)
53 type = VALUE_TYPE (val);
54 address = VALUE_ADDRESS (val) + VALUE_OFFSET (val);
56 if (is_object_type (type))
60 /* Get the run-time type, and cast the object into that */
62 obj_addr = unpack_pointer (type, VALUE_CONTENTS (val));
66 type = type_from_class (java_class_from_object (val));
67 type = lookup_pointer_type (type);
69 val = value_at (type, address, NULL);
73 if (TYPE_CODE (type) == TYPE_CODE_PTR && !value_logical_not (val))
74 type_print (TYPE_TARGET_TYPE (type), "", stream, -1);
76 name = TYPE_TAG_NAME (type);
77 if (TYPE_CODE (type) == TYPE_CODE_STRUCT && name != NULL
78 && (i = strlen (name), name[i - 1] == ']'))
82 unsigned int things_printed = 0;
84 struct type *el_type = java_primitive_type_from_name (name, i - 2);
87 read_memory (address + JAVA_OBJECT_SIZE, buf4, 4);
89 length = (long) extract_signed_integer (buf4, 4);
90 fprintf_filtered (stream, "{length: %ld", length);
95 CORE_ADDR next_element = -1; /* dummy initial value */
97 address += JAVA_OBJECT_SIZE + 4; /* Skip object header and length. */
99 while (i < length && things_printed < print_max)
103 buf = alloca (TARGET_PTR_BIT / HOST_CHAR_BIT);
104 fputs_filtered (", ", stream);
105 wrap_here (n_spaces (2));
108 element = next_element;
111 read_memory (address, buf, sizeof (buf));
112 address += TARGET_PTR_BIT / HOST_CHAR_BIT;
113 /* FIXME: cagney/2003-05-24: Bogus or what. It
114 pulls a host sized pointer out of the target and
115 then extracts that as an address (while assuming
116 that the address is unsigned)! */
117 element = extract_unsigned_integer (buf, sizeof (buf));
120 for (reps = 1; i + reps < length; reps++)
122 read_memory (address, buf, sizeof (buf));
123 address += TARGET_PTR_BIT / HOST_CHAR_BIT;
124 /* FIXME: cagney/2003-05-24: Bogus or what. It
125 pulls a host sized pointer out of the target and
126 then extracts that as an address (while assuming
127 that the address is unsigned)! */
128 next_element = extract_unsigned_integer (buf, sizeof (buf));
129 if (next_element != element)
134 fprintf_filtered (stream, "%d: ", i);
136 fprintf_filtered (stream, "%d..%d: ", i, i + reps - 1);
139 fprintf_filtered (stream, "null");
141 fprintf_filtered (stream, "@%s", paddr_nz (element));
149 struct value *v = allocate_value (el_type);
150 struct value *next_v = allocate_value (el_type);
152 VALUE_ADDRESS (v) = address + JAVA_OBJECT_SIZE + 4;
153 VALUE_ADDRESS (next_v) = VALUE_ADDRESS (v);
155 while (i < length && things_printed < print_max)
157 fputs_filtered (", ", stream);
158 wrap_here (n_spaces (2));
171 VALUE_OFFSET (v) = 0;
174 VALUE_OFFSET (next_v) = VALUE_OFFSET (v);
176 for (reps = 1; i + reps < length; reps++)
178 VALUE_LAZY (next_v) = 1;
179 VALUE_OFFSET (next_v) += TYPE_LENGTH (el_type);
180 if (memcmp (VALUE_CONTENTS (v), VALUE_CONTENTS (next_v),
181 TYPE_LENGTH (el_type)) != 0)
186 fprintf_filtered (stream, "%d: ", i);
188 fprintf_filtered (stream, "%d..%d: ", i, i + reps - 1);
190 val_print (VALUE_TYPE (v), VALUE_CONTENTS (v), 0, 0,
191 stream, format, 2, 1, pretty);
199 fprintf_filtered (stream, "...");
201 fprintf_filtered (stream, "}");
206 /* If it's type String, print it */
208 if (TYPE_CODE (type) == TYPE_CODE_PTR
209 && TYPE_TARGET_TYPE (type)
210 && TYPE_TAG_NAME (TYPE_TARGET_TYPE (type))
211 && strcmp (TYPE_TAG_NAME (TYPE_TARGET_TYPE (type)),
212 "java.lang.String") == 0
213 && (format == 0 || format == 's')
215 && value_as_address (val) != 0)
217 struct value *data_val;
219 struct value *boffset_val;
220 unsigned long boffset;
221 struct value *count_val;
225 mark = value_mark (); /* Remember start of new values */
227 data_val = value_struct_elt (&val, NULL, "data", NULL, NULL);
228 data = value_as_address (data_val);
230 boffset_val = value_struct_elt (&val, NULL, "boffset", NULL, NULL);
231 boffset = value_as_address (boffset_val);
233 count_val = value_struct_elt (&val, NULL, "count", NULL, NULL);
234 count = value_as_address (count_val);
236 value_free_to_mark (mark); /* Release unnecessary values */
238 val_print_string (data + boffset, count, 2, stream);
243 return (val_print (type, VALUE_CONTENTS (val), 0, address,
244 stream, format, 1, 0, pretty));
247 /* TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and PRETTY have the
248 same meanings as in cp_print_value and c_val_print.
250 DONT_PRINT is an array of baseclass types that we
251 should not print, or zero if called from top level. */
254 java_print_value_fields (struct type *type, char *valaddr, CORE_ADDR address,
255 struct ui_file *stream, int format, int recurse,
256 enum val_prettyprint pretty)
258 int i, len, n_baseclasses;
260 CHECK_TYPEDEF (type);
262 fprintf_filtered (stream, "{");
263 len = TYPE_NFIELDS (type);
264 n_baseclasses = TYPE_N_BASECLASSES (type);
266 if (n_baseclasses > 0)
268 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
270 for (i = 0; i < n_baseclasses; i++)
273 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
274 char *basename = TYPE_NAME (baseclass);
277 if (BASETYPE_VIA_VIRTUAL (type, i))
280 if (basename != NULL && strcmp (basename, "java.lang.Object") == 0)
287 fprintf_filtered (stream, "\n");
288 print_spaces_filtered (2 * (recurse + 1), stream);
290 fputs_filtered ("<", stream);
291 /* Not sure what the best notation is in the case where there is no
293 fputs_filtered (basename ? basename : "", stream);
294 fputs_filtered ("> = ", stream);
296 base_valaddr = valaddr;
298 java_print_value_fields (baseclass, base_valaddr, address + boffset,
299 stream, format, recurse + 1, pretty);
300 fputs_filtered (", ", stream);
308 if (!len && n_baseclasses == 1)
309 fprintf_filtered (stream, "<No data fields>");
314 for (i = n_baseclasses; i < len; i++)
316 /* If requested, skip printing of static fields. */
317 if (TYPE_FIELD_STATIC (type, i))
319 char *name = TYPE_FIELD_NAME (type, i);
320 if (!static_field_print)
322 if (name != NULL && strcmp (name, "class") == 0)
326 fprintf_filtered (stream, ", ");
327 else if (n_baseclasses > 0)
331 fprintf_filtered (stream, "\n");
332 print_spaces_filtered (2 + 2 * recurse, stream);
333 fputs_filtered ("members of ", stream);
334 fputs_filtered (type_name_no_tag (type), stream);
335 fputs_filtered (": ", stream);
342 fprintf_filtered (stream, "\n");
343 print_spaces_filtered (2 + 2 * recurse, stream);
347 wrap_here (n_spaces (2 + 2 * recurse));
351 if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
352 fputs_filtered ("\"( ptr \"", stream);
354 fputs_filtered ("\"( nodef \"", stream);
355 if (TYPE_FIELD_STATIC (type, i))
356 fputs_filtered ("static ", stream);
357 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
359 DMGL_PARAMS | DMGL_ANSI);
360 fputs_filtered ("\" \"", stream);
361 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
363 DMGL_PARAMS | DMGL_ANSI);
364 fputs_filtered ("\") \"", stream);
368 annotate_field_begin (TYPE_FIELD_TYPE (type, i));
370 if (TYPE_FIELD_STATIC (type, i))
371 fputs_filtered ("static ", stream);
372 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
374 DMGL_PARAMS | DMGL_ANSI);
375 annotate_field_name_end ();
376 fputs_filtered (": ", stream);
377 annotate_field_value ();
380 if (!TYPE_FIELD_STATIC (type, i) && TYPE_FIELD_PACKED (type, i))
384 /* Bitfields require special handling, especially due to byte
386 if (TYPE_FIELD_IGNORE (type, i))
388 fputs_filtered ("<optimized out or zero length>", stream);
392 v = value_from_longest (TYPE_FIELD_TYPE (type, i),
393 unpack_field_as_long (type, valaddr, i));
395 val_print (TYPE_FIELD_TYPE (type, i), VALUE_CONTENTS (v), 0,
396 0, stream, format, 0, recurse + 1, pretty);
401 if (TYPE_FIELD_IGNORE (type, i))
403 fputs_filtered ("<optimized out or zero length>", stream);
405 else if (TYPE_FIELD_STATIC (type, i))
407 struct value *v = value_static_field (type, i);
409 fputs_filtered ("<optimized out>", stream);
412 struct type *t = check_typedef (VALUE_TYPE (v));
413 if (TYPE_CODE (t) == TYPE_CODE_STRUCT)
415 val_print (VALUE_TYPE (v),
416 VALUE_CONTENTS (v), 0, VALUE_ADDRESS (v),
417 stream, format, 0, recurse + 1, pretty);
420 else if (TYPE_FIELD_TYPE (type, i) == NULL)
421 fputs_filtered ("<unknown type>", stream);
424 val_print (TYPE_FIELD_TYPE (type, i),
425 valaddr + TYPE_FIELD_BITPOS (type, i) / 8, 0,
426 address + TYPE_FIELD_BITPOS (type, i) / 8,
427 stream, format, 0, recurse + 1, pretty);
430 annotate_field_end ();
435 fprintf_filtered (stream, "\n");
436 print_spaces_filtered (2 * recurse, stream);
439 fprintf_filtered (stream, "}");
442 /* Print data of type TYPE located at VALADDR (within GDB), which came from
443 the inferior at address ADDRESS, onto stdio stream STREAM according to
444 FORMAT (a letter or 0 for natural format). The data at VALADDR is in
447 If the data are a string pointer, returns the number of string characters
450 If DEREF_REF is nonzero, then dereference references, otherwise just print
453 The PRETTY parameter controls prettyprinting. */
456 java_val_print (struct type *type, char *valaddr, int embedded_offset,
457 CORE_ADDR address, struct ui_file *stream, int format,
458 int deref_ref, int recurse, enum val_prettyprint pretty)
460 unsigned int i = 0; /* Number of characters printed */
461 struct type *target_type;
464 CHECK_TYPEDEF (type);
465 switch (TYPE_CODE (type))
468 if (format && format != 's')
470 print_scalar_formatted (valaddr, type, format, 0, stream);
474 if (vtblprint && cp_is_vtbl_ptr_type (type))
476 /* Print the unmangled name if desired. */
477 /* Print vtable entry - we only get here if we ARE using
478 -fvtable_thunks. (Otherwise, look under TYPE_CODE_STRUCT.) */
479 /* Extract an address, assume that it is unsigned. */
480 print_address_demangle (extract_unsigned_integer (valaddr, TYPE_LENGTH (type)),
485 addr = unpack_pointer (type, valaddr);
488 fputs_filtered ("null", stream);
491 target_type = check_typedef (TYPE_TARGET_TYPE (type));
493 if (TYPE_CODE (target_type) == TYPE_CODE_FUNC)
495 /* Try to print what function it points to. */
496 print_address_demangle (addr, stream, demangle);
497 /* Return value is irrelevant except for string pointers. */
501 if (addressprint && format != 's')
503 fputs_filtered ("@", stream);
504 print_longest (stream, 'x', 0, (ULONGEST) addr);
511 /* Can't just call c_val_print because that prints bytes as C
513 format = format ? format : output_format;
515 print_scalar_formatted (valaddr, type, format, 0, stream);
516 else if (TYPE_CODE (type) == TYPE_CODE_CHAR
517 || (TYPE_CODE (type) == TYPE_CODE_INT
518 && TYPE_LENGTH (type) == 2
519 && strcmp (TYPE_NAME (type), "char") == 0))
520 LA_PRINT_CHAR ((int) unpack_long (type, valaddr), stream);
522 val_print_type_code_int (type, valaddr, stream);
525 case TYPE_CODE_STRUCT:
526 java_print_value_fields (type, valaddr, address, stream, format,
531 return c_val_print (type, valaddr, embedded_offset, address, stream,
532 format, deref_ref, recurse, pretty);