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));
42 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
47 struct value_print_options opts;
49 type = value_type (val);
50 address = value_address (val);
52 if (is_object_type (type))
56 /* Get the run-time type, and cast the object into that */
58 obj_addr = unpack_pointer (type, value_contents (val));
62 type = type_from_class (gdbarch, java_class_from_object (val));
63 type = lookup_pointer_type (type);
65 val = value_at (type, address);
69 if (TYPE_CODE (type) == TYPE_CODE_PTR && !value_logical_not (val))
70 type_print (TYPE_TARGET_TYPE (type), "", stream, -1);
72 name = TYPE_TAG_NAME (type);
73 if (TYPE_CODE (type) == TYPE_CODE_STRUCT && name != NULL
74 && (i = strlen (name), name[i - 1] == ']'))
78 unsigned int things_printed = 0;
81 = 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) + TYPE_LENGTH (el_type));
180 if (memcmp (value_contents (v), value_contents (next_v),
181 TYPE_LENGTH (el_type)) != 0)
186 fprintf_filtered (stream, "%d: ", i);
188 fprintf_filtered (stream, "%d..%d: ", i, i + reps - 1);
192 common_val_print (v, stream, 1, &opts, current_language);
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 && (options->format == 0 || options->format == 's')
216 && value_as_address (val) != 0)
218 struct type *char_type;
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 char_type = builtin_java_type (gdbarch)->builtin_char;
241 val_print_string (char_type, data + boffset, count, stream, options);
248 return common_val_print (val, stream, 0, &opts, current_language);
251 /* TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and OPTIONS have the
252 same meanings as in cp_print_value and c_val_print.
254 DONT_PRINT is an array of baseclass types that we
255 should not print, or zero if called from top level. */
258 java_print_value_fields (struct type *type, const gdb_byte *valaddr,
259 CORE_ADDR address, struct ui_file *stream,
261 const struct value_print_options *options)
263 int i, len, n_baseclasses;
265 CHECK_TYPEDEF (type);
267 fprintf_filtered (stream, "{");
268 len = TYPE_NFIELDS (type);
269 n_baseclasses = TYPE_N_BASECLASSES (type);
271 if (n_baseclasses > 0)
273 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
275 for (i = 0; i < n_baseclasses; i++)
278 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
279 char *basename = TYPE_NAME (baseclass);
280 const gdb_byte *base_valaddr;
282 if (BASETYPE_VIA_VIRTUAL (type, i))
285 if (basename != NULL && strcmp (basename, "java.lang.Object") == 0)
292 fprintf_filtered (stream, "\n");
293 print_spaces_filtered (2 * (recurse + 1), stream);
295 fputs_filtered ("<", stream);
296 /* Not sure what the best notation is in the case where there is no
298 fputs_filtered (basename ? basename : "", stream);
299 fputs_filtered ("> = ", stream);
301 base_valaddr = valaddr;
303 java_print_value_fields (baseclass, base_valaddr, address + boffset,
304 stream, recurse + 1, options);
305 fputs_filtered (", ", stream);
310 if (!len && n_baseclasses == 1)
311 fprintf_filtered (stream, "<No data fields>");
316 for (i = n_baseclasses; i < len; i++)
318 /* If requested, skip printing of static fields. */
319 if (field_is_static (&TYPE_FIELD (type, i)))
321 char *name = TYPE_FIELD_NAME (type, i);
322 if (!options->static_field_print)
324 if (name != NULL && strcmp (name, "class") == 0)
328 fprintf_filtered (stream, ", ");
329 else if (n_baseclasses > 0)
333 fprintf_filtered (stream, "\n");
334 print_spaces_filtered (2 + 2 * recurse, stream);
335 fputs_filtered ("members of ", stream);
336 fputs_filtered (type_name_no_tag (type), stream);
337 fputs_filtered (": ", stream);
344 fprintf_filtered (stream, "\n");
345 print_spaces_filtered (2 + 2 * recurse, stream);
349 wrap_here (n_spaces (2 + 2 * recurse));
351 if (options->inspect_it)
353 if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
354 fputs_filtered ("\"( ptr \"", stream);
356 fputs_filtered ("\"( nodef \"", stream);
357 if (field_is_static (&TYPE_FIELD (type, i)))
358 fputs_filtered ("static ", stream);
359 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
361 DMGL_PARAMS | DMGL_ANSI);
362 fputs_filtered ("\" \"", stream);
363 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
365 DMGL_PARAMS | DMGL_ANSI);
366 fputs_filtered ("\") \"", stream);
370 annotate_field_begin (TYPE_FIELD_TYPE (type, i));
372 if (field_is_static (&TYPE_FIELD (type, i)))
373 fputs_filtered ("static ", stream);
374 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
376 DMGL_PARAMS | DMGL_ANSI);
377 annotate_field_name_end ();
378 fputs_filtered (": ", stream);
379 annotate_field_value ();
382 if (!field_is_static (&TYPE_FIELD (type, i))
383 && TYPE_FIELD_PACKED (type, i))
387 /* Bitfields require special handling, especially due to byte
389 if (TYPE_FIELD_IGNORE (type, i))
391 fputs_filtered ("<optimized out or zero length>", stream);
395 struct value_print_options opts;
397 v = value_from_longest (TYPE_FIELD_TYPE (type, i),
398 unpack_field_as_long (type, valaddr, i));
402 common_val_print (v, stream, recurse + 1,
403 &opts, current_language);
408 if (TYPE_FIELD_IGNORE (type, i))
410 fputs_filtered ("<optimized out or zero length>", stream);
412 else if (field_is_static (&TYPE_FIELD (type, i)))
414 struct value *v = value_static_field (type, i);
416 fputs_filtered ("<optimized out>", stream);
419 struct value_print_options opts;
420 struct type *t = check_typedef (value_type (v));
421 if (TYPE_CODE (t) == TYPE_CODE_STRUCT)
425 common_val_print (v, stream, recurse + 1,
426 &opts, current_language);
429 else if (TYPE_FIELD_TYPE (type, i) == NULL)
430 fputs_filtered ("<unknown type>", stream);
433 struct value_print_options opts = *options;
435 val_print (TYPE_FIELD_TYPE (type, i),
436 valaddr + TYPE_FIELD_BITPOS (type, i) / 8, 0,
437 address + TYPE_FIELD_BITPOS (type, i) / 8,
438 stream, recurse + 1, &opts,
442 annotate_field_end ();
447 fprintf_filtered (stream, "\n");
448 print_spaces_filtered (2 * recurse, stream);
451 fprintf_filtered (stream, "}");
454 /* Print data of type TYPE located at VALADDR (within GDB), which came from
455 the inferior at address ADDRESS, onto stdio stream STREAM according to
456 OPTIONS. The data at VALADDR is in target byte order.
458 If the data are a string pointer, returns the number of string characters
462 java_val_print (struct type *type, const gdb_byte *valaddr,
463 int embedded_offset, CORE_ADDR address,
464 struct ui_file *stream, int recurse,
465 const struct value_print_options *options)
467 struct gdbarch *gdbarch = get_type_arch (type);
468 unsigned int i = 0; /* Number of characters printed */
469 struct type *target_type;
472 CHECK_TYPEDEF (type);
473 switch (TYPE_CODE (type))
476 if (options->format && options->format != 's')
478 print_scalar_formatted (valaddr, type, options, 0, stream);
482 if (options->vtblprint && cp_is_vtbl_ptr_type (type))
484 /* Print the unmangled name if desired. */
485 /* Print vtable entry - we only get here if we ARE using
486 -fvtable_thunks. (Otherwise, look under TYPE_CODE_STRUCT.) */
487 /* Extract an address, assume that it is unsigned. */
488 print_address_demangle (gdbarch,
489 extract_unsigned_integer (valaddr, TYPE_LENGTH (type)),
494 addr = unpack_pointer (type, valaddr);
497 fputs_filtered ("null", stream);
500 target_type = check_typedef (TYPE_TARGET_TYPE (type));
502 if (TYPE_CODE (target_type) == TYPE_CODE_FUNC)
504 /* Try to print what function it points to. */
505 print_address_demangle (gdbarch, addr, stream, demangle);
506 /* Return value is irrelevant except for string pointers. */
510 if (options->addressprint && options->format != 's')
512 fputs_filtered ("@", stream);
513 print_longest (stream, 'x', 0, (ULONGEST) addr);
520 /* Can't just call c_val_print because that prints bytes as C
522 if (options->format || options->output_format)
524 struct value_print_options opts = *options;
525 opts.format = (options->format ? options->format
526 : options->output_format);
527 print_scalar_formatted (valaddr, type, &opts, 0, stream);
529 else if (TYPE_CODE (type) == TYPE_CODE_CHAR
530 || (TYPE_CODE (type) == TYPE_CODE_INT
531 && TYPE_LENGTH (type) == 2
532 && strcmp (TYPE_NAME (type), "char") == 0))
533 LA_PRINT_CHAR ((int) unpack_long (type, valaddr), type, stream);
535 val_print_type_code_int (type, valaddr, stream);
538 case TYPE_CODE_STRUCT:
539 java_print_value_fields (type, valaddr, address, stream, recurse,
544 return c_val_print (type, valaddr, embedded_offset, address, stream,