1 /* Support for printing Java values for GDB, the GNU debugger.
3 Copyright (C) 1997-2005, 2007-2012 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "expression.h"
32 #include "gdb_string.h"
37 java_value_print (struct value *val, struct ui_file *stream,
38 const struct value_print_options *options)
40 struct gdbarch *gdbarch = get_type_arch (value_type (val));
41 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
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);
83 read_memory (address + get_java_object_header_size (gdbarch), buf4, 4);
85 length = (long) extract_signed_integer (buf4, 4, byte_order);
86 fprintf_filtered (stream, "{length: %ld", length);
91 CORE_ADDR next_element = -1; /* Dummy initial value. */
93 /* Skip object header and length. */
94 address += get_java_object_header_size (gdbarch) + 4;
96 while (i < length && things_printed < options->print_max)
100 buf = alloca (gdbarch_ptr_bit (gdbarch) / HOST_CHAR_BIT);
101 fputs_filtered (", ", stream);
102 wrap_here (n_spaces (2));
105 element = next_element;
108 read_memory (address, buf, sizeof (buf));
109 address += gdbarch_ptr_bit (gdbarch) / HOST_CHAR_BIT;
110 /* FIXME: cagney/2003-05-24: Bogus or what. It
111 pulls a host sized pointer out of the target and
112 then extracts that as an address (while assuming
113 that the address is unsigned)! */
114 element = extract_unsigned_integer (buf, sizeof (buf),
118 for (reps = 1; i + reps < length; reps++)
120 read_memory (address, buf, sizeof (buf));
121 address += gdbarch_ptr_bit (gdbarch) / HOST_CHAR_BIT;
122 /* FIXME: cagney/2003-05-24: Bogus or what. It
123 pulls a host sized pointer out of the target and
124 then extracts that as an address (while assuming
125 that the address is unsigned)! */
126 next_element = extract_unsigned_integer (buf, sizeof (buf),
128 if (next_element != element)
133 fprintf_filtered (stream, "%d: ", i);
135 fprintf_filtered (stream, "%d..%d: ", i, i + reps - 1);
138 fprintf_filtered (stream, "null");
140 fprintf_filtered (stream, "@%s", paddress (gdbarch, element));
148 struct value *v = allocate_value (el_type);
149 struct value *next_v = allocate_value (el_type);
151 set_value_address (v, (address
152 + get_java_object_header_size (gdbarch) + 4));
153 set_value_address (next_v, value_raw_address (v));
155 while (i < length && things_printed < options->print_max)
157 fputs_filtered (", ", stream);
158 wrap_here (n_spaces (2));
170 set_value_lazy (v, 1);
171 set_value_offset (v, 0);
174 set_value_offset (next_v, value_offset (v));
176 for (reps = 1; i + reps < length; reps++)
178 set_value_lazy (next_v, 1);
179 set_value_offset (next_v, value_offset (next_v)
180 + TYPE_LENGTH (el_type));
181 value_fetch_lazy (next_v);
182 if (!(value_available_contents_eq
183 (v, value_embedded_offset (v),
184 next_v, value_embedded_offset (next_v),
185 TYPE_LENGTH (el_type))))
190 fprintf_filtered (stream, "%d: ", i);
192 fprintf_filtered (stream, "%d..%d: ", i, i + reps - 1);
196 common_val_print (v, stream, 1, &opts, current_language);
204 fprintf_filtered (stream, "...");
206 fprintf_filtered (stream, "}");
211 /* If it's type String, print it. */
213 if (TYPE_CODE (type) == TYPE_CODE_PTR
214 && TYPE_TARGET_TYPE (type)
215 && TYPE_TAG_NAME (TYPE_TARGET_TYPE (type))
216 && strcmp (TYPE_TAG_NAME (TYPE_TARGET_TYPE (type)),
217 "java.lang.String") == 0
218 && (options->format == 0 || options->format == 's')
220 && value_as_address (val) != 0)
222 struct type *char_type;
223 struct value *data_val;
225 struct value *boffset_val;
226 unsigned long boffset;
227 struct value *count_val;
231 mark = value_mark (); /* Remember start of new values. */
233 data_val = value_struct_elt (&val, NULL, "data", NULL, NULL);
234 data = value_as_address (data_val);
236 boffset_val = value_struct_elt (&val, NULL, "boffset", NULL, NULL);
237 boffset = value_as_address (boffset_val);
239 count_val = value_struct_elt (&val, NULL, "count", NULL, NULL);
240 count = value_as_address (count_val);
242 value_free_to_mark (mark); /* Release unnecessary values. */
244 char_type = builtin_java_type (gdbarch)->builtin_char;
245 val_print_string (char_type, NULL, data + boffset, count, stream,
253 return common_val_print (val, stream, 0, &opts, current_language);
256 /* TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and OPTIONS have the
257 same meanings as in cp_print_value and c_val_print.
259 DONT_PRINT is an array of baseclass types that we
260 should not print, or zero if called from top level. */
263 java_print_value_fields (struct type *type, const gdb_byte *valaddr,
265 CORE_ADDR address, struct ui_file *stream,
267 const struct value *val,
268 const struct value_print_options *options)
270 int i, len, n_baseclasses;
272 CHECK_TYPEDEF (type);
274 fprintf_filtered (stream, "{");
275 len = TYPE_NFIELDS (type);
276 n_baseclasses = TYPE_N_BASECLASSES (type);
278 if (n_baseclasses > 0)
280 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
282 for (i = 0; i < n_baseclasses; i++)
285 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
286 const char *basename = TYPE_NAME (baseclass);
287 const gdb_byte *base_valaddr;
289 if (BASETYPE_VIA_VIRTUAL (type, i))
292 if (basename != NULL && strcmp (basename, "java.lang.Object") == 0)
299 fprintf_filtered (stream, "\n");
300 print_spaces_filtered (2 * (recurse + 1), stream);
302 fputs_filtered ("<", stream);
303 /* Not sure what the best notation is in the case where there is no
305 fputs_filtered (basename ? basename : "", stream);
306 fputs_filtered ("> = ", stream);
308 base_valaddr = valaddr;
310 java_print_value_fields (baseclass, base_valaddr,
311 offset + boffset, address,
312 stream, recurse + 1, val, options);
313 fputs_filtered (", ", stream);
317 if (!len && n_baseclasses == 1)
318 fprintf_filtered (stream, "<No data fields>");
323 for (i = n_baseclasses; i < len; i++)
325 /* If requested, skip printing of static fields. */
326 if (field_is_static (&TYPE_FIELD (type, i)))
328 const char *name = TYPE_FIELD_NAME (type, i);
330 if (!options->static_field_print)
332 if (name != NULL && strcmp (name, "class") == 0)
336 fprintf_filtered (stream, ", ");
337 else if (n_baseclasses > 0)
341 fprintf_filtered (stream, "\n");
342 print_spaces_filtered (2 + 2 * recurse, stream);
343 fputs_filtered ("members of ", stream);
344 fputs_filtered (type_name_no_tag (type), stream);
345 fputs_filtered (": ", stream);
352 fprintf_filtered (stream, "\n");
353 print_spaces_filtered (2 + 2 * recurse, stream);
357 wrap_here (n_spaces (2 + 2 * recurse));
359 if (options->inspect_it)
361 if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
362 fputs_filtered ("\"( ptr \"", stream);
364 fputs_filtered ("\"( nodef \"", stream);
365 if (field_is_static (&TYPE_FIELD (type, i)))
366 fputs_filtered ("static ", stream);
367 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
369 DMGL_PARAMS | DMGL_ANSI);
370 fputs_filtered ("\" \"", stream);
371 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
373 DMGL_PARAMS | DMGL_ANSI);
374 fputs_filtered ("\") \"", stream);
378 annotate_field_begin (TYPE_FIELD_TYPE (type, i));
380 if (field_is_static (&TYPE_FIELD (type, i)))
381 fputs_filtered ("static ", stream);
382 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
384 DMGL_PARAMS | DMGL_ANSI);
385 annotate_field_name_end ();
386 fputs_filtered (": ", stream);
387 annotate_field_value ();
390 if (!field_is_static (&TYPE_FIELD (type, i))
391 && TYPE_FIELD_PACKED (type, i))
395 /* Bitfields require special handling, especially due to byte
397 if (TYPE_FIELD_IGNORE (type, i))
399 fputs_filtered ("<optimized out or zero length>", stream);
401 else if (value_bits_synthetic_pointer (val,
402 TYPE_FIELD_BITPOS (type,
404 TYPE_FIELD_BITSIZE (type,
407 fputs_filtered (_("<synthetic pointer>"), stream);
409 else if (!value_bits_valid (val, TYPE_FIELD_BITPOS (type, i),
410 TYPE_FIELD_BITSIZE (type, i)))
412 val_print_optimized_out (stream);
416 struct value_print_options opts;
418 v = value_field_bitfield (type, i, valaddr, offset, val);
422 common_val_print (v, stream, recurse + 1,
423 &opts, current_language);
428 if (TYPE_FIELD_IGNORE (type, i))
430 fputs_filtered ("<optimized out or zero length>", stream);
432 else if (field_is_static (&TYPE_FIELD (type, i)))
434 struct value *v = value_static_field (type, i);
437 val_print_optimized_out (stream);
440 struct value_print_options opts;
441 struct type *t = check_typedef (value_type (v));
443 if (TYPE_CODE (t) == TYPE_CODE_STRUCT)
447 common_val_print (v, stream, recurse + 1,
448 &opts, current_language);
451 else if (TYPE_FIELD_TYPE (type, i) == NULL)
452 fputs_filtered ("<unknown type>", stream);
455 struct value_print_options opts = *options;
458 val_print (TYPE_FIELD_TYPE (type, i),
460 offset + TYPE_FIELD_BITPOS (type, i) / 8,
461 address, stream, recurse + 1, val, &opts,
465 annotate_field_end ();
470 fprintf_filtered (stream, "\n");
471 print_spaces_filtered (2 * recurse, stream);
474 fprintf_filtered (stream, "}");
477 /* See val_print for a description of the various parameters of this
478 function; they are identical. The semantics of the return value is
479 also identical to val_print. */
482 java_val_print (struct type *type, const gdb_byte *valaddr,
483 int embedded_offset, CORE_ADDR address,
484 struct ui_file *stream, int recurse,
485 const struct value *val,
486 const struct value_print_options *options)
488 struct gdbarch *gdbarch = get_type_arch (type);
489 unsigned int i = 0; /* Number of characters printed. */
490 struct type *target_type;
493 CHECK_TYPEDEF (type);
494 switch (TYPE_CODE (type))
497 if (options->format && options->format != 's')
499 val_print_scalar_formatted (type, valaddr, embedded_offset,
500 val, options, 0, stream);
504 if (options->vtblprint && cp_is_vtbl_ptr_type (type))
506 /* Print the unmangled name if desired. */
507 /* Print vtable entry - we only get here if we ARE using
508 -fvtable_thunks. (Otherwise, look under TYPE_CODE_STRUCT.) */
509 /* Extract an address, assume that it is unsigned. */
510 print_address_demangle
512 extract_unsigned_integer (valaddr + embedded_offset,
518 addr = unpack_pointer (type, valaddr + embedded_offset);
521 fputs_filtered ("null", stream);
524 target_type = check_typedef (TYPE_TARGET_TYPE (type));
526 if (TYPE_CODE (target_type) == TYPE_CODE_FUNC)
528 /* Try to print what function it points to. */
529 print_address_demangle (gdbarch, addr, stream, demangle);
530 /* Return value is irrelevant except for string pointers. */
534 if (options->addressprint && options->format != 's')
536 fputs_filtered ("@", stream);
537 print_longest (stream, 'x', 0, (ULONGEST) addr);
544 /* Can't just call c_val_print because that prints bytes as C
546 if (options->format || options->output_format)
548 struct value_print_options opts = *options;
550 opts.format = (options->format ? options->format
551 : options->output_format);
552 val_print_scalar_formatted (type, valaddr, embedded_offset,
553 val, &opts, 0, stream);
555 else if (TYPE_CODE (type) == TYPE_CODE_CHAR
556 || (TYPE_CODE (type) == TYPE_CODE_INT
557 && TYPE_LENGTH (type) == 2
558 && strcmp (TYPE_NAME (type), "char") == 0))
559 LA_PRINT_CHAR ((int) unpack_long (type, valaddr + embedded_offset),
562 val_print_type_code_int (type, valaddr + embedded_offset, stream);
565 case TYPE_CODE_STRUCT:
566 java_print_value_fields (type, valaddr, embedded_offset,
567 address, stream, recurse, val, options);
571 return c_val_print (type, valaddr, embedded_offset, address, stream,
572 recurse, val, options);