1 /* Support for printing Java values for GDB, the GNU debugger.
3 Copyright 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free
4 Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
27 #include "expression.h"
35 #include "gdb_string.h"
39 static void java_print_value_fields (struct type * type, char *valaddr,
41 struct ui_file *stream, int format,
43 enum val_prettyprint pretty);
47 java_value_print (struct value *val, struct ui_file *stream, int format,
48 enum val_prettyprint pretty)
55 type = VALUE_TYPE (val);
56 address = VALUE_ADDRESS (val) + VALUE_OFFSET (val);
58 if (is_object_type (type))
62 /* Get the run-time type, and cast the object into that */
64 obj_addr = unpack_pointer (type, VALUE_CONTENTS (val));
68 type = type_from_class (java_class_from_object (val));
69 type = lookup_pointer_type (type);
71 val = value_at (type, address, NULL);
75 if (TYPE_CODE (type) == TYPE_CODE_PTR && !value_logical_not (val))
76 type_print (TYPE_TARGET_TYPE (type), "", stream, -1);
78 name = TYPE_TAG_NAME (type);
79 if (TYPE_CODE (type) == TYPE_CODE_STRUCT && name != NULL
80 && (i = strlen (name), name[i - 1] == ']'))
84 unsigned int things_printed = 0;
86 struct type *el_type = java_primitive_type_from_name (name, i - 2);
89 read_memory (address + JAVA_OBJECT_SIZE, buf4, 4);
91 length = (long) extract_signed_integer (buf4, 4);
92 fprintf_filtered (stream, "{length: %ld", length);
97 CORE_ADDR next_element = -1; /* dummy initial value */
99 address += JAVA_OBJECT_SIZE + 4; /* Skip object header and length. */
101 while (i < length && things_printed < print_max)
105 buf = alloca (TARGET_PTR_BIT / HOST_CHAR_BIT);
106 fputs_filtered (", ", stream);
107 wrap_here (n_spaces (2));
110 element = next_element;
113 read_memory (address, buf, sizeof (buf));
114 address += TARGET_PTR_BIT / HOST_CHAR_BIT;
115 /* FIXME: cagney/2003-05-24: Bogus or what. It
116 pulls a host sized pointer out of the target and
117 then extracts that as an address (while assuming
118 that the address is unsigned)! */
119 element = extract_unsigned_integer (buf, sizeof (buf));
122 for (reps = 1; i + reps < length; reps++)
124 read_memory (address, buf, sizeof (buf));
125 address += TARGET_PTR_BIT / HOST_CHAR_BIT;
126 /* FIXME: cagney/2003-05-24: Bogus or what. It
127 pulls a host sized pointer out of the target and
128 then extracts that as an address (while assuming
129 that the address is unsigned)! */
130 next_element = extract_unsigned_integer (buf, sizeof (buf));
131 if (next_element != element)
136 fprintf_filtered (stream, "%d: ", i);
138 fprintf_filtered (stream, "%d..%d: ", i, i + reps - 1);
141 fprintf_filtered (stream, "null");
143 fprintf_filtered (stream, "@%s", paddr_nz (element));
151 struct value *v = allocate_value (el_type);
152 struct value *next_v = allocate_value (el_type);
154 VALUE_ADDRESS (v) = address + JAVA_OBJECT_SIZE + 4;
155 VALUE_ADDRESS (next_v) = VALUE_ADDRESS (v);
157 while (i < length && things_printed < print_max)
159 fputs_filtered (", ", stream);
160 wrap_here (n_spaces (2));
173 VALUE_OFFSET (v) = 0;
176 VALUE_OFFSET (next_v) = VALUE_OFFSET (v);
178 for (reps = 1; i + reps < length; reps++)
180 VALUE_LAZY (next_v) = 1;
181 VALUE_OFFSET (next_v) += TYPE_LENGTH (el_type);
182 if (memcmp (VALUE_CONTENTS (v), VALUE_CONTENTS (next_v),
183 TYPE_LENGTH (el_type)) != 0)
188 fprintf_filtered (stream, "%d: ", i);
190 fprintf_filtered (stream, "%d..%d: ", i, i + reps - 1);
192 common_val_print (v, stream, format, 2, 1, pretty);
200 fprintf_filtered (stream, "...");
202 fprintf_filtered (stream, "}");
207 /* If it's type String, print it */
209 if (TYPE_CODE (type) == TYPE_CODE_PTR
210 && TYPE_TARGET_TYPE (type)
211 && TYPE_TAG_NAME (TYPE_TARGET_TYPE (type))
212 && strcmp (TYPE_TAG_NAME (TYPE_TARGET_TYPE (type)),
213 "java.lang.String") == 0
214 && (format == 0 || format == 's')
216 && value_as_address (val) != 0)
218 struct value *data_val;
220 struct value *boffset_val;
221 unsigned long boffset;
222 struct value *count_val;
226 mark = value_mark (); /* Remember start of new values */
228 data_val = value_struct_elt (&val, NULL, "data", NULL, NULL);
229 data = value_as_address (data_val);
231 boffset_val = value_struct_elt (&val, NULL, "boffset", NULL, NULL);
232 boffset = value_as_address (boffset_val);
234 count_val = value_struct_elt (&val, NULL, "count", NULL, NULL);
235 count = value_as_address (count_val);
237 value_free_to_mark (mark); /* Release unnecessary values */
239 val_print_string (data + boffset, count, 2, stream);
244 return common_val_print (val, 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);
305 if (!len && n_baseclasses == 1)
306 fprintf_filtered (stream, "<No data fields>");
311 for (i = n_baseclasses; i < len; i++)
313 /* If requested, skip printing of static fields. */
314 if (TYPE_FIELD_STATIC (type, i))
316 char *name = TYPE_FIELD_NAME (type, i);
317 if (!static_field_print)
319 if (name != NULL && strcmp (name, "class") == 0)
323 fprintf_filtered (stream, ", ");
324 else if (n_baseclasses > 0)
328 fprintf_filtered (stream, "\n");
329 print_spaces_filtered (2 + 2 * recurse, stream);
330 fputs_filtered ("members of ", stream);
331 fputs_filtered (type_name_no_tag (type), stream);
332 fputs_filtered (": ", stream);
339 fprintf_filtered (stream, "\n");
340 print_spaces_filtered (2 + 2 * recurse, stream);
344 wrap_here (n_spaces (2 + 2 * recurse));
348 if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
349 fputs_filtered ("\"( ptr \"", stream);
351 fputs_filtered ("\"( nodef \"", stream);
352 if (TYPE_FIELD_STATIC (type, i))
353 fputs_filtered ("static ", stream);
354 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
356 DMGL_PARAMS | DMGL_ANSI);
357 fputs_filtered ("\" \"", stream);
358 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
360 DMGL_PARAMS | DMGL_ANSI);
361 fputs_filtered ("\") \"", stream);
365 annotate_field_begin (TYPE_FIELD_TYPE (type, i));
367 if (TYPE_FIELD_STATIC (type, i))
368 fputs_filtered ("static ", stream);
369 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
371 DMGL_PARAMS | DMGL_ANSI);
372 annotate_field_name_end ();
373 fputs_filtered (": ", stream);
374 annotate_field_value ();
377 if (!TYPE_FIELD_STATIC (type, i) && TYPE_FIELD_PACKED (type, i))
381 /* Bitfields require special handling, especially due to byte
383 if (TYPE_FIELD_IGNORE (type, i))
385 fputs_filtered ("<optimized out or zero length>", stream);
389 v = value_from_longest (TYPE_FIELD_TYPE (type, i),
390 unpack_field_as_long (type, valaddr, i));
392 common_val_print (v, stream, format, 0, recurse + 1, pretty);
397 if (TYPE_FIELD_IGNORE (type, i))
399 fputs_filtered ("<optimized out or zero length>", stream);
401 else if (TYPE_FIELD_STATIC (type, i))
403 struct value *v = value_static_field (type, i);
405 fputs_filtered ("<optimized out>", stream);
408 struct type *t = check_typedef (VALUE_TYPE (v));
409 if (TYPE_CODE (t) == TYPE_CODE_STRUCT)
411 common_val_print (v, stream, format, 0, recurse + 1,
415 else if (TYPE_FIELD_TYPE (type, i) == NULL)
416 fputs_filtered ("<unknown type>", stream);
419 val_print (TYPE_FIELD_TYPE (type, i),
420 valaddr + TYPE_FIELD_BITPOS (type, i) / 8, 0,
421 address + TYPE_FIELD_BITPOS (type, i) / 8,
422 stream, format, 0, recurse + 1, pretty);
425 annotate_field_end ();
430 fprintf_filtered (stream, "\n");
431 print_spaces_filtered (2 * recurse, stream);
434 fprintf_filtered (stream, "}");
437 /* Print data of type TYPE located at VALADDR (within GDB), which came from
438 the inferior at address ADDRESS, onto stdio stream STREAM according to
439 FORMAT (a letter or 0 for natural format). The data at VALADDR is in
442 If the data are a string pointer, returns the number of string characters
445 If DEREF_REF is nonzero, then dereference references, otherwise just print
448 The PRETTY parameter controls prettyprinting. */
451 java_val_print (struct type *type, char *valaddr, int embedded_offset,
452 CORE_ADDR address, struct ui_file *stream, int format,
453 int deref_ref, int recurse, enum val_prettyprint pretty)
455 unsigned int i = 0; /* Number of characters printed */
456 struct type *target_type;
459 CHECK_TYPEDEF (type);
460 switch (TYPE_CODE (type))
463 if (format && format != 's')
465 print_scalar_formatted (valaddr, type, format, 0, stream);
469 if (vtblprint && cp_is_vtbl_ptr_type (type))
471 /* Print the unmangled name if desired. */
472 /* Print vtable entry - we only get here if we ARE using
473 -fvtable_thunks. (Otherwise, look under TYPE_CODE_STRUCT.) */
474 /* Extract an address, assume that it is unsigned. */
475 print_address_demangle (extract_unsigned_integer (valaddr, TYPE_LENGTH (type)),
480 addr = unpack_pointer (type, valaddr);
483 fputs_filtered ("null", stream);
486 target_type = check_typedef (TYPE_TARGET_TYPE (type));
488 if (TYPE_CODE (target_type) == TYPE_CODE_FUNC)
490 /* Try to print what function it points to. */
491 print_address_demangle (addr, stream, demangle);
492 /* Return value is irrelevant except for string pointers. */
496 if (addressprint && format != 's')
498 fputs_filtered ("@", stream);
499 print_longest (stream, 'x', 0, (ULONGEST) addr);
506 /* Can't just call c_val_print because that prints bytes as C
508 format = format ? format : output_format;
510 print_scalar_formatted (valaddr, type, format, 0, stream);
511 else if (TYPE_CODE (type) == TYPE_CODE_CHAR
512 || (TYPE_CODE (type) == TYPE_CODE_INT
513 && TYPE_LENGTH (type) == 2
514 && strcmp (TYPE_NAME (type), "char") == 0))
515 LA_PRINT_CHAR ((int) unpack_long (type, valaddr), stream);
517 val_print_type_code_int (type, valaddr, stream);
520 case TYPE_CODE_STRUCT:
521 java_print_value_fields (type, valaddr, address, stream, format,
526 return c_val_print (type, valaddr, embedded_offset, address, stream,
527 format, deref_ref, recurse, pretty);