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 val_print (VALUE_TYPE (v), VALUE_CONTENTS (v), 0, 0,
193 stream, format, 2, 1, pretty);
201 fprintf_filtered (stream, "...");
203 fprintf_filtered (stream, "}");
208 /* If it's type String, print it */
210 if (TYPE_CODE (type) == TYPE_CODE_PTR
211 && TYPE_TARGET_TYPE (type)
212 && TYPE_TAG_NAME (TYPE_TARGET_TYPE (type))
213 && strcmp (TYPE_TAG_NAME (TYPE_TARGET_TYPE (type)),
214 "java.lang.String") == 0
215 && (format == 0 || format == 's')
217 && value_as_address (val) != 0)
219 struct value *data_val;
221 struct value *boffset_val;
222 unsigned long boffset;
223 struct value *count_val;
227 mark = value_mark (); /* Remember start of new values */
229 data_val = value_struct_elt (&val, NULL, "data", NULL, NULL);
230 data = value_as_address (data_val);
232 boffset_val = value_struct_elt (&val, NULL, "boffset", NULL, NULL);
233 boffset = value_as_address (boffset_val);
235 count_val = value_struct_elt (&val, NULL, "count", NULL, NULL);
236 count = value_as_address (count_val);
238 value_free_to_mark (mark); /* Release unnecessary values */
240 val_print_string (data + boffset, count, 2, stream);
245 return (val_print (type, VALUE_CONTENTS (val), 0, address,
246 stream, format, 1, 0, pretty));
249 /* TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and PRETTY have the
250 same meanings as in cp_print_value and c_val_print.
252 DONT_PRINT is an array of baseclass types that we
253 should not print, or zero if called from top level. */
256 java_print_value_fields (struct type *type, char *valaddr, CORE_ADDR address,
257 struct ui_file *stream, int format, int recurse,
258 enum val_prettyprint pretty)
260 int i, len, n_baseclasses;
262 CHECK_TYPEDEF (type);
264 fprintf_filtered (stream, "{");
265 len = TYPE_NFIELDS (type);
266 n_baseclasses = TYPE_N_BASECLASSES (type);
268 if (n_baseclasses > 0)
270 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
272 for (i = 0; i < n_baseclasses; i++)
275 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
276 char *basename = TYPE_NAME (baseclass);
279 if (BASETYPE_VIA_VIRTUAL (type, i))
282 if (basename != NULL && strcmp (basename, "java.lang.Object") == 0)
289 fprintf_filtered (stream, "\n");
290 print_spaces_filtered (2 * (recurse + 1), stream);
292 fputs_filtered ("<", stream);
293 /* Not sure what the best notation is in the case where there is no
295 fputs_filtered (basename ? basename : "", stream);
296 fputs_filtered ("> = ", stream);
298 base_valaddr = valaddr;
300 java_print_value_fields (baseclass, base_valaddr, address + boffset,
301 stream, format, recurse + 1, pretty);
302 fputs_filtered (", ", stream);
307 if (!len && n_baseclasses == 1)
308 fprintf_filtered (stream, "<No data fields>");
313 for (i = n_baseclasses; i < len; i++)
315 /* If requested, skip printing of static fields. */
316 if (TYPE_FIELD_STATIC (type, i))
318 char *name = TYPE_FIELD_NAME (type, i);
319 if (!static_field_print)
321 if (name != NULL && strcmp (name, "class") == 0)
325 fprintf_filtered (stream, ", ");
326 else if (n_baseclasses > 0)
330 fprintf_filtered (stream, "\n");
331 print_spaces_filtered (2 + 2 * recurse, stream);
332 fputs_filtered ("members of ", stream);
333 fputs_filtered (type_name_no_tag (type), stream);
334 fputs_filtered (": ", stream);
341 fprintf_filtered (stream, "\n");
342 print_spaces_filtered (2 + 2 * recurse, stream);
346 wrap_here (n_spaces (2 + 2 * recurse));
350 if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
351 fputs_filtered ("\"( ptr \"", stream);
353 fputs_filtered ("\"( nodef \"", stream);
354 if (TYPE_FIELD_STATIC (type, i))
355 fputs_filtered ("static ", stream);
356 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
358 DMGL_PARAMS | DMGL_ANSI);
359 fputs_filtered ("\" \"", stream);
360 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
362 DMGL_PARAMS | DMGL_ANSI);
363 fputs_filtered ("\") \"", stream);
367 annotate_field_begin (TYPE_FIELD_TYPE (type, i));
369 if (TYPE_FIELD_STATIC (type, i))
370 fputs_filtered ("static ", stream);
371 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
373 DMGL_PARAMS | DMGL_ANSI);
374 annotate_field_name_end ();
375 fputs_filtered (": ", stream);
376 annotate_field_value ();
379 if (!TYPE_FIELD_STATIC (type, i) && TYPE_FIELD_PACKED (type, i))
383 /* Bitfields require special handling, especially due to byte
385 if (TYPE_FIELD_IGNORE (type, i))
387 fputs_filtered ("<optimized out or zero length>", stream);
391 v = value_from_longest (TYPE_FIELD_TYPE (type, i),
392 unpack_field_as_long (type, valaddr, i));
394 val_print (TYPE_FIELD_TYPE (type, i), VALUE_CONTENTS (v), 0,
395 0, stream, format, 0, recurse + 1, pretty);
400 if (TYPE_FIELD_IGNORE (type, i))
402 fputs_filtered ("<optimized out or zero length>", stream);
404 else if (TYPE_FIELD_STATIC (type, i))
406 struct value *v = value_static_field (type, i);
408 fputs_filtered ("<optimized out>", stream);
411 struct type *t = check_typedef (VALUE_TYPE (v));
412 if (TYPE_CODE (t) == TYPE_CODE_STRUCT)
414 val_print (VALUE_TYPE (v),
415 VALUE_CONTENTS (v), 0, VALUE_ADDRESS (v),
416 stream, format, 0, recurse + 1, pretty);
419 else if (TYPE_FIELD_TYPE (type, i) == NULL)
420 fputs_filtered ("<unknown type>", stream);
423 val_print (TYPE_FIELD_TYPE (type, i),
424 valaddr + TYPE_FIELD_BITPOS (type, i) / 8, 0,
425 address + TYPE_FIELD_BITPOS (type, i) / 8,
426 stream, format, 0, recurse + 1, pretty);
429 annotate_field_end ();
434 fprintf_filtered (stream, "\n");
435 print_spaces_filtered (2 * recurse, stream);
438 fprintf_filtered (stream, "}");
441 /* Print data of type TYPE located at VALADDR (within GDB), which came from
442 the inferior at address ADDRESS, onto stdio stream STREAM according to
443 FORMAT (a letter or 0 for natural format). The data at VALADDR is in
446 If the data are a string pointer, returns the number of string characters
449 If DEREF_REF is nonzero, then dereference references, otherwise just print
452 The PRETTY parameter controls prettyprinting. */
455 java_val_print (struct type *type, char *valaddr, int embedded_offset,
456 CORE_ADDR address, struct ui_file *stream, int format,
457 int deref_ref, int recurse, enum val_prettyprint pretty)
459 unsigned int i = 0; /* Number of characters printed */
460 struct type *target_type;
463 CHECK_TYPEDEF (type);
464 switch (TYPE_CODE (type))
467 if (format && format != 's')
469 print_scalar_formatted (valaddr, type, format, 0, stream);
473 if (vtblprint && cp_is_vtbl_ptr_type (type))
475 /* Print the unmangled name if desired. */
476 /* Print vtable entry - we only get here if we ARE using
477 -fvtable_thunks. (Otherwise, look under TYPE_CODE_STRUCT.) */
478 /* Extract an address, assume that it is unsigned. */
479 print_address_demangle (extract_unsigned_integer (valaddr, TYPE_LENGTH (type)),
484 addr = unpack_pointer (type, valaddr);
487 fputs_filtered ("null", stream);
490 target_type = check_typedef (TYPE_TARGET_TYPE (type));
492 if (TYPE_CODE (target_type) == TYPE_CODE_FUNC)
494 /* Try to print what function it points to. */
495 print_address_demangle (addr, stream, demangle);
496 /* Return value is irrelevant except for string pointers. */
500 if (addressprint && format != 's')
502 fputs_filtered ("@", stream);
503 print_longest (stream, 'x', 0, (ULONGEST) addr);
510 /* Can't just call c_val_print because that prints bytes as C
512 format = format ? format : output_format;
514 print_scalar_formatted (valaddr, type, format, 0, stream);
515 else if (TYPE_CODE (type) == TYPE_CODE_CHAR
516 || (TYPE_CODE (type) == TYPE_CODE_INT
517 && TYPE_LENGTH (type) == 2
518 && strcmp (TYPE_NAME (type), "char") == 0))
519 LA_PRINT_CHAR ((int) unpack_long (type, valaddr), stream);
521 val_print_type_code_int (type, valaddr, stream);
524 case TYPE_CODE_STRUCT:
525 java_print_value_fields (type, valaddr, address, stream, format,
530 return c_val_print (type, valaddr, embedded_offset, address, stream,
531 format, deref_ref, recurse, pretty);