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 element = extract_address (buf, sizeof (buf));
116 for (reps = 1; i + reps < length; reps++)
118 read_memory (address, buf, sizeof (buf));
119 address += TARGET_PTR_BIT / HOST_CHAR_BIT;
120 next_element = extract_address (buf, sizeof (buf));
121 if (next_element != element)
126 fprintf_filtered (stream, "%d: ", i);
128 fprintf_filtered (stream, "%d..%d: ", i, i + reps - 1);
131 fprintf_filtered (stream, "null");
133 fprintf_filtered (stream, "@%s", paddr_nz (element));
141 struct value *v = allocate_value (el_type);
142 struct value *next_v = allocate_value (el_type);
144 VALUE_ADDRESS (v) = address + JAVA_OBJECT_SIZE + 4;
145 VALUE_ADDRESS (next_v) = VALUE_ADDRESS (v);
147 while (i < length && things_printed < print_max)
149 fputs_filtered (", ", stream);
150 wrap_here (n_spaces (2));
163 VALUE_OFFSET (v) = 0;
166 VALUE_OFFSET (next_v) = VALUE_OFFSET (v);
168 for (reps = 1; i + reps < length; reps++)
170 VALUE_LAZY (next_v) = 1;
171 VALUE_OFFSET (next_v) += TYPE_LENGTH (el_type);
172 if (memcmp (VALUE_CONTENTS (v), VALUE_CONTENTS (next_v),
173 TYPE_LENGTH (el_type)) != 0)
178 fprintf_filtered (stream, "%d: ", i);
180 fprintf_filtered (stream, "%d..%d: ", i, i + reps - 1);
182 val_print (VALUE_TYPE (v), VALUE_CONTENTS (v), 0, 0,
183 stream, format, 2, 1, pretty);
191 fprintf_filtered (stream, "...");
193 fprintf_filtered (stream, "}");
198 /* If it's type String, print it */
200 if (TYPE_CODE (type) == TYPE_CODE_PTR
201 && TYPE_TARGET_TYPE (type)
202 && TYPE_TAG_NAME (TYPE_TARGET_TYPE (type))
203 && strcmp (TYPE_TAG_NAME (TYPE_TARGET_TYPE (type)),
204 "java.lang.String") == 0
205 && (format == 0 || format == 's')
207 && value_as_address (val) != 0)
209 struct value *data_val;
211 struct value *boffset_val;
212 unsigned long boffset;
213 struct value *count_val;
217 mark = value_mark (); /* Remember start of new values */
219 data_val = value_struct_elt (&val, NULL, "data", NULL, NULL);
220 data = value_as_address (data_val);
222 boffset_val = value_struct_elt (&val, NULL, "boffset", NULL, NULL);
223 boffset = value_as_address (boffset_val);
225 count_val = value_struct_elt (&val, NULL, "count", NULL, NULL);
226 count = value_as_address (count_val);
228 value_free_to_mark (mark); /* Release unnecessary values */
230 val_print_string (data + boffset, count, 2, stream);
235 return (val_print (type, VALUE_CONTENTS (val), 0, address,
236 stream, format, 1, 0, pretty));
239 /* TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and PRETTY have the
240 same meanings as in cp_print_value and c_val_print.
242 DONT_PRINT is an array of baseclass types that we
243 should not print, or zero if called from top level. */
246 java_print_value_fields (struct type *type, char *valaddr, CORE_ADDR address,
247 struct ui_file *stream, int format, int recurse,
248 enum val_prettyprint pretty)
250 int i, len, n_baseclasses;
252 CHECK_TYPEDEF (type);
254 fprintf_filtered (stream, "{");
255 len = TYPE_NFIELDS (type);
256 n_baseclasses = TYPE_N_BASECLASSES (type);
258 if (n_baseclasses > 0)
260 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
262 for (i = 0; i < n_baseclasses; i++)
265 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
266 char *basename = TYPE_NAME (baseclass);
269 if (BASETYPE_VIA_VIRTUAL (type, i))
272 if (basename != NULL && strcmp (basename, "java.lang.Object") == 0)
279 fprintf_filtered (stream, "\n");
280 print_spaces_filtered (2 * (recurse + 1), stream);
282 fputs_filtered ("<", stream);
283 /* Not sure what the best notation is in the case where there is no
285 fputs_filtered (basename ? basename : "", stream);
286 fputs_filtered ("> = ", stream);
288 base_valaddr = valaddr;
290 java_print_value_fields (baseclass, base_valaddr, address + boffset,
291 stream, format, recurse + 1, pretty);
292 fputs_filtered (", ", stream);
300 if (!len && n_baseclasses == 1)
301 fprintf_filtered (stream, "<No data fields>");
304 extern int inspect_it;
307 for (i = n_baseclasses; i < len; i++)
309 /* If requested, skip printing of static fields. */
310 if (TYPE_FIELD_STATIC (type, i))
312 char *name = TYPE_FIELD_NAME (type, i);
313 if (!static_field_print)
315 if (name != NULL && strcmp (name, "class") == 0)
319 fprintf_filtered (stream, ", ");
320 else if (n_baseclasses > 0)
324 fprintf_filtered (stream, "\n");
325 print_spaces_filtered (2 + 2 * recurse, stream);
326 fputs_filtered ("members of ", stream);
327 fputs_filtered (type_name_no_tag (type), stream);
328 fputs_filtered (": ", stream);
335 fprintf_filtered (stream, "\n");
336 print_spaces_filtered (2 + 2 * recurse, stream);
340 wrap_here (n_spaces (2 + 2 * recurse));
344 if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
345 fputs_filtered ("\"( ptr \"", stream);
347 fputs_filtered ("\"( nodef \"", stream);
348 if (TYPE_FIELD_STATIC (type, i))
349 fputs_filtered ("static ", stream);
350 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
352 DMGL_PARAMS | DMGL_ANSI);
353 fputs_filtered ("\" \"", stream);
354 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
356 DMGL_PARAMS | DMGL_ANSI);
357 fputs_filtered ("\") \"", stream);
361 annotate_field_begin (TYPE_FIELD_TYPE (type, i));
363 if (TYPE_FIELD_STATIC (type, i))
364 fputs_filtered ("static ", stream);
365 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
367 DMGL_PARAMS | DMGL_ANSI);
368 annotate_field_name_end ();
369 fputs_filtered (": ", stream);
370 annotate_field_value ();
373 if (!TYPE_FIELD_STATIC (type, i) && TYPE_FIELD_PACKED (type, i))
377 /* Bitfields require special handling, especially due to byte
379 if (TYPE_FIELD_IGNORE (type, i))
381 fputs_filtered ("<optimized out or zero length>", stream);
385 v = value_from_longest (TYPE_FIELD_TYPE (type, i),
386 unpack_field_as_long (type, valaddr, i));
388 val_print (TYPE_FIELD_TYPE (type, i), VALUE_CONTENTS (v), 0,
389 0, stream, format, 0, recurse + 1, pretty);
394 if (TYPE_FIELD_IGNORE (type, i))
396 fputs_filtered ("<optimized out or zero length>", stream);
398 else if (TYPE_FIELD_STATIC (type, i))
400 struct value *v = value_static_field (type, i);
402 fputs_filtered ("<optimized out>", stream);
405 struct type *t = check_typedef (VALUE_TYPE (v));
406 if (TYPE_CODE (t) == TYPE_CODE_STRUCT)
408 val_print (VALUE_TYPE (v),
409 VALUE_CONTENTS (v), 0, VALUE_ADDRESS (v),
410 stream, format, 0, recurse + 1, pretty);
413 else if (TYPE_FIELD_TYPE (type, i) == NULL)
414 fputs_filtered ("<unknown type>", stream);
417 val_print (TYPE_FIELD_TYPE (type, i),
418 valaddr + TYPE_FIELD_BITPOS (type, i) / 8, 0,
419 address + TYPE_FIELD_BITPOS (type, i) / 8,
420 stream, format, 0, recurse + 1, pretty);
423 annotate_field_end ();
428 fprintf_filtered (stream, "\n");
429 print_spaces_filtered (2 * recurse, stream);
432 fprintf_filtered (stream, "}");
435 /* Print data of type TYPE located at VALADDR (within GDB), which came from
436 the inferior at address ADDRESS, onto stdio stream STREAM according to
437 FORMAT (a letter or 0 for natural format). The data at VALADDR is in
440 If the data are a string pointer, returns the number of string characters
443 If DEREF_REF is nonzero, then dereference references, otherwise just print
446 The PRETTY parameter controls prettyprinting. */
449 java_val_print (struct type *type, char *valaddr, int embedded_offset,
450 CORE_ADDR address, struct ui_file *stream, int format,
451 int deref_ref, int recurse, enum val_prettyprint pretty)
453 register unsigned int i = 0; /* Number of characters printed */
454 struct type *target_type;
457 CHECK_TYPEDEF (type);
458 switch (TYPE_CODE (type))
461 if (format && format != 's')
463 print_scalar_formatted (valaddr, type, format, 0, stream);
467 if (vtblprint && cp_is_vtbl_ptr_type (type))
469 /* Print the unmangled name if desired. */
470 /* Print vtable entry - we only get here if we ARE using
471 -fvtable_thunks. (Otherwise, look under TYPE_CODE_STRUCT.) */
472 print_address_demangle (extract_address (valaddr, TYPE_LENGTH (type)),
477 addr = unpack_pointer (type, valaddr);
480 fputs_filtered ("null", stream);
483 target_type = check_typedef (TYPE_TARGET_TYPE (type));
485 if (TYPE_CODE (target_type) == TYPE_CODE_FUNC)
487 /* Try to print what function it points to. */
488 print_address_demangle (addr, stream, demangle);
489 /* Return value is irrelevant except for string pointers. */
493 if (addressprint && format != 's')
495 fputs_filtered ("@", stream);
496 print_longest (stream, 'x', 0, (ULONGEST) addr);
503 /* Can't just call c_val_print because that prints bytes as C
505 format = format ? format : output_format;
507 print_scalar_formatted (valaddr, type, format, 0, stream);
508 else if (TYPE_CODE (type) == TYPE_CODE_CHAR
509 || (TYPE_CODE (type) == TYPE_CODE_INT
510 && TYPE_LENGTH (type) == 2
511 && strcmp (TYPE_NAME (type), "char") == 0))
512 LA_PRINT_CHAR ((int) unpack_long (type, valaddr), stream);
514 val_print_type_code_int (type, valaddr, stream);
517 case TYPE_CODE_STRUCT:
518 java_print_value_fields (type, valaddr, address, stream, format,
523 return c_val_print (type, valaddr, embedded_offset, address, stream,
524 format, deref_ref, recurse, pretty);