1 /* Support for printing Java values for GDB, the GNU debugger.
3 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007,
4 2008, 2009 Free 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 3 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, see <http://www.gnu.org/licenses/>. */
25 #include "expression.h"
33 #include "gdb_string.h"
38 java_value_print (struct value *val, struct ui_file *stream,
39 const struct value_print_options *options)
41 struct gdbarch *gdbarch = get_type_arch (value_type (val));
46 struct value_print_options opts;
48 type = value_type (val);
49 address = value_address (val);
51 if (is_object_type (type))
55 /* Get the run-time type, and cast the object into that */
57 obj_addr = unpack_pointer (type, value_contents (val));
61 type = type_from_class (gdbarch, java_class_from_object (val));
62 type = lookup_pointer_type (type);
64 val = value_at (type, address);
68 if (TYPE_CODE (type) == TYPE_CODE_PTR && !value_logical_not (val))
69 type_print (TYPE_TARGET_TYPE (type), "", stream, -1);
71 name = TYPE_TAG_NAME (type);
72 if (TYPE_CODE (type) == TYPE_CODE_STRUCT && name != NULL
73 && (i = strlen (name), name[i - 1] == ']'))
77 unsigned int things_printed = 0;
80 = java_primitive_type_from_name (gdbarch, name, i - 2);
82 read_memory (address + get_java_object_header_size (gdbarch), buf4, 4);
84 length = (long) extract_signed_integer (buf4, 4);
85 fprintf_filtered (stream, "{length: %ld", length);
90 CORE_ADDR next_element = -1; /* dummy initial value */
92 /* Skip object header and length. */
93 address += get_java_object_header_size (gdbarch) + 4;
95 while (i < length && things_printed < options->print_max)
99 buf = alloca (gdbarch_ptr_bit (gdbarch) / HOST_CHAR_BIT);
100 fputs_filtered (", ", stream);
101 wrap_here (n_spaces (2));
104 element = next_element;
107 read_memory (address, buf, sizeof (buf));
108 address += gdbarch_ptr_bit (gdbarch) / HOST_CHAR_BIT;
109 /* FIXME: cagney/2003-05-24: Bogus or what. It
110 pulls a host sized pointer out of the target and
111 then extracts that as an address (while assuming
112 that the address is unsigned)! */
113 element = extract_unsigned_integer (buf, sizeof (buf));
116 for (reps = 1; i + reps < length; reps++)
118 read_memory (address, buf, sizeof (buf));
119 address += gdbarch_ptr_bit (gdbarch) / HOST_CHAR_BIT;
120 /* FIXME: cagney/2003-05-24: Bogus or what. It
121 pulls a host sized pointer out of the target and
122 then extracts that as an address (while assuming
123 that the address is unsigned)! */
124 next_element = extract_unsigned_integer (buf, sizeof (buf));
125 if (next_element != element)
130 fprintf_filtered (stream, "%d: ", i);
132 fprintf_filtered (stream, "%d..%d: ", i, i + reps - 1);
135 fprintf_filtered (stream, "null");
137 fprintf_filtered (stream, "@%s", paddress (gdbarch, element));
145 struct value *v = allocate_value (el_type);
146 struct value *next_v = allocate_value (el_type);
148 set_value_address (v, (address
149 + get_java_object_header_size (gdbarch) + 4));
150 set_value_address (next_v, value_raw_address (v));
152 while (i < length && things_printed < options->print_max)
154 fputs_filtered (", ", stream);
155 wrap_here (n_spaces (2));
167 set_value_lazy (v, 1);
168 set_value_offset (v, 0);
171 set_value_offset (next_v, value_offset (v));
173 for (reps = 1; i + reps < length; reps++)
175 set_value_lazy (next_v, 1);
176 set_value_offset (next_v, value_offset (next_v) + TYPE_LENGTH (el_type));
177 if (memcmp (value_contents (v), value_contents (next_v),
178 TYPE_LENGTH (el_type)) != 0)
183 fprintf_filtered (stream, "%d: ", i);
185 fprintf_filtered (stream, "%d..%d: ", i, i + reps - 1);
189 common_val_print (v, stream, 1, &opts, current_language);
197 fprintf_filtered (stream, "...");
199 fprintf_filtered (stream, "}");
204 /* If it's type String, print it */
206 if (TYPE_CODE (type) == TYPE_CODE_PTR
207 && TYPE_TARGET_TYPE (type)
208 && TYPE_TAG_NAME (TYPE_TARGET_TYPE (type))
209 && strcmp (TYPE_TAG_NAME (TYPE_TARGET_TYPE (type)),
210 "java.lang.String") == 0
211 && (options->format == 0 || options->format == 's')
213 && value_as_address (val) != 0)
215 struct type *char_type;
216 struct value *data_val;
218 struct value *boffset_val;
219 unsigned long boffset;
220 struct value *count_val;
224 mark = value_mark (); /* Remember start of new values */
226 data_val = value_struct_elt (&val, NULL, "data", NULL, NULL);
227 data = value_as_address (data_val);
229 boffset_val = value_struct_elt (&val, NULL, "boffset", NULL, NULL);
230 boffset = value_as_address (boffset_val);
232 count_val = value_struct_elt (&val, NULL, "count", NULL, NULL);
233 count = value_as_address (count_val);
235 value_free_to_mark (mark); /* Release unnecessary values */
237 char_type = builtin_java_type (gdbarch)->builtin_char;
238 val_print_string (char_type, data + boffset, count, stream, options);
245 return common_val_print (val, stream, 0, &opts, current_language);
248 /* TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and OPTIONS have the
249 same meanings as in cp_print_value and c_val_print.
251 DONT_PRINT is an array of baseclass types that we
252 should not print, or zero if called from top level. */
255 java_print_value_fields (struct type *type, const gdb_byte *valaddr,
256 CORE_ADDR address, struct ui_file *stream,
258 const struct value_print_options *options)
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);
277 const gdb_byte *base_valaddr;
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, recurse + 1, options);
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 (field_is_static (&TYPE_FIELD (type, i)))
318 char *name = TYPE_FIELD_NAME (type, i);
319 if (!options->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));
348 if (options->inspect_it)
350 if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
351 fputs_filtered ("\"( ptr \"", stream);
353 fputs_filtered ("\"( nodef \"", stream);
354 if (field_is_static (&TYPE_FIELD (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 (field_is_static (&TYPE_FIELD (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 (!field_is_static (&TYPE_FIELD (type, i))
380 && 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 struct value_print_options opts;
394 v = value_from_longest (TYPE_FIELD_TYPE (type, i),
395 unpack_field_as_long (type, valaddr, i));
399 common_val_print (v, stream, recurse + 1,
400 &opts, current_language);
405 if (TYPE_FIELD_IGNORE (type, i))
407 fputs_filtered ("<optimized out or zero length>", stream);
409 else if (field_is_static (&TYPE_FIELD (type, i)))
411 struct value *v = value_static_field (type, i);
413 fputs_filtered ("<optimized out>", stream);
416 struct value_print_options opts;
417 struct type *t = check_typedef (value_type (v));
418 if (TYPE_CODE (t) == TYPE_CODE_STRUCT)
422 common_val_print (v, stream, recurse + 1,
423 &opts, current_language);
426 else if (TYPE_FIELD_TYPE (type, i) == NULL)
427 fputs_filtered ("<unknown type>", stream);
430 struct value_print_options opts = *options;
432 val_print (TYPE_FIELD_TYPE (type, i),
433 valaddr + TYPE_FIELD_BITPOS (type, i) / 8, 0,
434 address + TYPE_FIELD_BITPOS (type, i) / 8,
435 stream, recurse + 1, &opts,
439 annotate_field_end ();
444 fprintf_filtered (stream, "\n");
445 print_spaces_filtered (2 * recurse, stream);
448 fprintf_filtered (stream, "}");
451 /* Print data of type TYPE located at VALADDR (within GDB), which came from
452 the inferior at address ADDRESS, onto stdio stream STREAM according to
453 OPTIONS. The data at VALADDR is in target byte order.
455 If the data are a string pointer, returns the number of string characters
459 java_val_print (struct type *type, const gdb_byte *valaddr,
460 int embedded_offset, CORE_ADDR address,
461 struct ui_file *stream, int recurse,
462 const struct value_print_options *options)
464 struct gdbarch *gdbarch = get_type_arch (type);
465 unsigned int i = 0; /* Number of characters printed */
466 struct type *target_type;
469 CHECK_TYPEDEF (type);
470 switch (TYPE_CODE (type))
473 if (options->format && options->format != 's')
475 print_scalar_formatted (valaddr, type, options, 0, stream);
479 if (options->vtblprint && cp_is_vtbl_ptr_type (type))
481 /* Print the unmangled name if desired. */
482 /* Print vtable entry - we only get here if we ARE using
483 -fvtable_thunks. (Otherwise, look under TYPE_CODE_STRUCT.) */
484 /* Extract an address, assume that it is unsigned. */
485 print_address_demangle (gdbarch,
486 extract_unsigned_integer (valaddr, TYPE_LENGTH (type)),
491 addr = unpack_pointer (type, valaddr);
494 fputs_filtered ("null", stream);
497 target_type = check_typedef (TYPE_TARGET_TYPE (type));
499 if (TYPE_CODE (target_type) == TYPE_CODE_FUNC)
501 /* Try to print what function it points to. */
502 print_address_demangle (gdbarch, addr, stream, demangle);
503 /* Return value is irrelevant except for string pointers. */
507 if (options->addressprint && options->format != 's')
509 fputs_filtered ("@", stream);
510 print_longest (stream, 'x', 0, (ULONGEST) addr);
517 /* Can't just call c_val_print because that prints bytes as C
519 if (options->format || options->output_format)
521 struct value_print_options opts = *options;
522 opts.format = (options->format ? options->format
523 : options->output_format);
524 print_scalar_formatted (valaddr, type, &opts, 0, stream);
526 else if (TYPE_CODE (type) == TYPE_CODE_CHAR
527 || (TYPE_CODE (type) == TYPE_CODE_INT
528 && TYPE_LENGTH (type) == 2
529 && strcmp (TYPE_NAME (type), "char") == 0))
530 LA_PRINT_CHAR ((int) unpack_long (type, valaddr), type, stream);
532 val_print_type_code_int (type, valaddr, stream);
535 case TYPE_CODE_STRUCT:
536 java_print_value_fields (type, valaddr, address, stream, recurse,
541 return c_val_print (type, valaddr, embedded_offset, address, stream,