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))
54 struct value *tem = val;
56 /* Get the run-time type, and cast the object into that. */
57 while (TYPE_CODE (value_type (tem)) == TYPE_CODE_PTR)
58 tem = value_ind (tem);
60 obj_addr = value_address (tem);
64 type = type_from_class (gdbarch, java_class_from_object (val));
65 type = lookup_pointer_type (type);
67 val = value_at (type, address);
71 if (TYPE_CODE (type) == TYPE_CODE_PTR && !value_logical_not (val))
72 type_print (TYPE_TARGET_TYPE (type), "", stream, -1);
74 name = TYPE_TAG_NAME (type);
75 if (TYPE_CODE (type) == TYPE_CODE_STRUCT && name != NULL
76 && (i = strlen (name), name[i - 1] == ']'))
80 unsigned int things_printed = 0;
83 = java_primitive_type_from_name (gdbarch, name, i - 2);
86 read_memory (address + get_java_object_header_size (gdbarch), buf4, 4);
88 length = (long) extract_signed_integer (buf4, 4, byte_order);
89 fprintf_filtered (stream, "{length: %ld", length);
94 CORE_ADDR next_element = -1; /* Dummy initial value. */
96 /* Skip object header and length. */
97 address += get_java_object_header_size (gdbarch) + 4;
99 while (i < length && things_printed < options->print_max)
103 buf = alloca (gdbarch_ptr_bit (gdbarch) / HOST_CHAR_BIT);
104 fputs_filtered (", ", stream);
105 wrap_here (n_spaces (2));
108 element = next_element;
111 read_memory (address, buf, sizeof (buf));
112 address += gdbarch_ptr_bit (gdbarch) / HOST_CHAR_BIT;
113 /* FIXME: cagney/2003-05-24: Bogus or what. It
114 pulls a host sized pointer out of the target and
115 then extracts that as an address (while assuming
116 that the address is unsigned)! */
117 element = extract_unsigned_integer (buf, sizeof (buf),
121 for (reps = 1; i + reps < length; reps++)
123 read_memory (address, buf, sizeof (buf));
124 address += gdbarch_ptr_bit (gdbarch) / HOST_CHAR_BIT;
125 /* FIXME: cagney/2003-05-24: Bogus or what. It
126 pulls a host sized pointer out of the target and
127 then extracts that as an address (while assuming
128 that the address is unsigned)! */
129 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", paddress (gdbarch, element));
151 struct value *v = allocate_value (el_type);
152 struct value *next_v = allocate_value (el_type);
154 set_value_address (v, (address
155 + get_java_object_header_size (gdbarch) + 4));
156 set_value_address (next_v, value_raw_address (v));
158 while (i < length && things_printed < options->print_max)
160 fputs_filtered (", ", stream);
161 wrap_here (n_spaces (2));
173 set_value_lazy (v, 1);
174 set_value_offset (v, 0);
177 set_value_offset (next_v, value_offset (v));
179 for (reps = 1; i + reps < length; reps++)
181 set_value_lazy (next_v, 1);
182 set_value_offset (next_v, value_offset (next_v)
183 + TYPE_LENGTH (el_type));
184 value_fetch_lazy (next_v);
185 if (!(value_available_contents_eq
186 (v, value_embedded_offset (v),
187 next_v, value_embedded_offset (next_v),
188 TYPE_LENGTH (el_type))))
193 fprintf_filtered (stream, "%d: ", i);
195 fprintf_filtered (stream, "%d..%d: ", i, i + reps - 1);
199 common_val_print (v, stream, 1, &opts, current_language);
207 fprintf_filtered (stream, "...");
209 fprintf_filtered (stream, "}");
214 /* If it's type String, print it. */
216 if (TYPE_CODE (type) == TYPE_CODE_PTR
217 && TYPE_TARGET_TYPE (type)
218 && TYPE_TAG_NAME (TYPE_TARGET_TYPE (type))
219 && strcmp (TYPE_TAG_NAME (TYPE_TARGET_TYPE (type)),
220 "java.lang.String") == 0
221 && (options->format == 0 || options->format == 's')
223 && value_as_address (val) != 0)
225 struct type *char_type;
226 struct value *data_val;
228 struct value *boffset_val;
229 unsigned long boffset;
230 struct value *count_val;
234 mark = value_mark (); /* Remember start of new values. */
236 data_val = value_struct_elt (&val, NULL, "data", NULL, NULL);
237 data = value_as_address (data_val);
239 boffset_val = value_struct_elt (&val, NULL, "boffset", NULL, NULL);
240 boffset = value_as_address (boffset_val);
242 count_val = value_struct_elt (&val, NULL, "count", NULL, NULL);
243 count = value_as_address (count_val);
245 value_free_to_mark (mark); /* Release unnecessary values. */
247 char_type = builtin_java_type (gdbarch)->builtin_char;
248 val_print_string (char_type, NULL, data + boffset, count, stream,
256 common_val_print (val, stream, 0, &opts, current_language);
259 /* TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and OPTIONS have the
260 same meanings as in cp_print_value and c_val_print.
262 DONT_PRINT is an array of baseclass types that we
263 should not print, or zero if called from top level. */
266 java_print_value_fields (struct type *type, const gdb_byte *valaddr,
268 CORE_ADDR address, struct ui_file *stream,
270 const struct value *val,
271 const struct value_print_options *options)
273 int i, len, n_baseclasses;
275 CHECK_TYPEDEF (type);
277 fprintf_filtered (stream, "{");
278 len = TYPE_NFIELDS (type);
279 n_baseclasses = TYPE_N_BASECLASSES (type);
281 if (n_baseclasses > 0)
283 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
285 for (i = 0; i < n_baseclasses; i++)
288 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
289 const char *basename = TYPE_NAME (baseclass);
290 const gdb_byte *base_valaddr;
292 if (BASETYPE_VIA_VIRTUAL (type, i))
295 if (basename != NULL && strcmp (basename, "java.lang.Object") == 0)
302 fprintf_filtered (stream, "\n");
303 print_spaces_filtered (2 * (recurse + 1), stream);
305 fputs_filtered ("<", stream);
306 /* Not sure what the best notation is in the case where there is no
308 fputs_filtered (basename ? basename : "", stream);
309 fputs_filtered ("> = ", stream);
311 base_valaddr = valaddr;
313 java_print_value_fields (baseclass, base_valaddr,
314 offset + boffset, address,
315 stream, recurse + 1, val, options);
316 fputs_filtered (", ", stream);
320 if (!len && n_baseclasses == 1)
321 fprintf_filtered (stream, "<No data fields>");
326 for (i = n_baseclasses; i < len; i++)
328 /* If requested, skip printing of static fields. */
329 if (field_is_static (&TYPE_FIELD (type, i)))
331 const char *name = TYPE_FIELD_NAME (type, i);
333 if (!options->static_field_print)
335 if (name != NULL && strcmp (name, "class") == 0)
339 fprintf_filtered (stream, ", ");
340 else if (n_baseclasses > 0)
344 fprintf_filtered (stream, "\n");
345 print_spaces_filtered (2 + 2 * recurse, stream);
346 fputs_filtered ("members of ", stream);
347 fputs_filtered (type_name_no_tag (type), stream);
348 fputs_filtered (": ", stream);
355 fprintf_filtered (stream, "\n");
356 print_spaces_filtered (2 + 2 * recurse, stream);
360 wrap_here (n_spaces (2 + 2 * recurse));
362 if (options->inspect_it)
364 if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
365 fputs_filtered ("\"( ptr \"", stream);
367 fputs_filtered ("\"( nodef \"", stream);
368 if (field_is_static (&TYPE_FIELD (type, i)))
369 fputs_filtered ("static ", stream);
370 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
372 DMGL_PARAMS | DMGL_ANSI);
373 fputs_filtered ("\" \"", stream);
374 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
376 DMGL_PARAMS | DMGL_ANSI);
377 fputs_filtered ("\") \"", stream);
381 annotate_field_begin (TYPE_FIELD_TYPE (type, i));
383 if (field_is_static (&TYPE_FIELD (type, i)))
384 fputs_filtered ("static ", stream);
385 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
387 DMGL_PARAMS | DMGL_ANSI);
388 annotate_field_name_end ();
389 fputs_filtered (": ", stream);
390 annotate_field_value ();
393 if (!field_is_static (&TYPE_FIELD (type, i))
394 && TYPE_FIELD_PACKED (type, i))
398 /* Bitfields require special handling, especially due to byte
400 if (TYPE_FIELD_IGNORE (type, i))
402 fputs_filtered ("<optimized out or zero length>", stream);
404 else if (value_bits_synthetic_pointer (val,
405 TYPE_FIELD_BITPOS (type,
407 TYPE_FIELD_BITSIZE (type,
410 fputs_filtered (_("<synthetic pointer>"), stream);
412 else if (!value_bits_valid (val, TYPE_FIELD_BITPOS (type, i),
413 TYPE_FIELD_BITSIZE (type, i)))
415 val_print_optimized_out (stream);
419 struct value_print_options opts;
421 v = value_field_bitfield (type, i, valaddr, offset, val);
425 common_val_print (v, stream, recurse + 1,
426 &opts, current_language);
431 if (TYPE_FIELD_IGNORE (type, i))
433 fputs_filtered ("<optimized out or zero length>", stream);
435 else if (field_is_static (&TYPE_FIELD (type, i)))
437 struct value *v = value_static_field (type, i);
440 val_print_optimized_out (stream);
443 struct value_print_options opts;
444 struct type *t = check_typedef (value_type (v));
446 if (TYPE_CODE (t) == TYPE_CODE_STRUCT)
450 common_val_print (v, stream, recurse + 1,
451 &opts, current_language);
454 else if (TYPE_FIELD_TYPE (type, i) == NULL)
455 fputs_filtered ("<unknown type>", stream);
458 struct value_print_options opts = *options;
461 val_print (TYPE_FIELD_TYPE (type, i),
463 offset + TYPE_FIELD_BITPOS (type, i) / 8,
464 address, stream, recurse + 1, val, &opts,
468 annotate_field_end ();
473 fprintf_filtered (stream, "\n");
474 print_spaces_filtered (2 * recurse, stream);
477 fprintf_filtered (stream, "}");
480 /* See val_print for a description of the various parameters of this
481 function; they are identical. */
484 java_val_print (struct type *type, const gdb_byte *valaddr,
485 int embedded_offset, CORE_ADDR address,
486 struct ui_file *stream, int recurse,
487 const struct value *val,
488 const struct value_print_options *options)
490 struct gdbarch *gdbarch = get_type_arch (type);
491 unsigned int i = 0; /* Number of characters printed. */
492 struct type *target_type;
495 CHECK_TYPEDEF (type);
496 switch (TYPE_CODE (type))
499 if (options->format && options->format != 's')
501 val_print_scalar_formatted (type, valaddr, embedded_offset,
502 val, options, 0, stream);
505 addr = unpack_pointer (type, valaddr + embedded_offset);
508 fputs_filtered ("null", stream);
511 target_type = check_typedef (TYPE_TARGET_TYPE (type));
513 if (TYPE_CODE (target_type) == TYPE_CODE_FUNC)
515 /* Try to print what function it points to. */
516 print_address_demangle (gdbarch, addr, stream, demangle);
520 if (options->addressprint && options->format != 's')
522 fputs_filtered ("@", stream);
523 print_longest (stream, 'x', 0, (ULONGEST) addr);
530 /* Can't just call c_val_print because that prints bytes as C
532 if (options->format || options->output_format)
534 struct value_print_options opts = *options;
536 opts.format = (options->format ? options->format
537 : options->output_format);
538 val_print_scalar_formatted (type, valaddr, embedded_offset,
539 val, &opts, 0, stream);
541 else if (TYPE_CODE (type) == TYPE_CODE_CHAR
542 || (TYPE_CODE (type) == TYPE_CODE_INT
543 && TYPE_LENGTH (type) == 2
544 && strcmp (TYPE_NAME (type), "char") == 0))
545 LA_PRINT_CHAR ((int) unpack_long (type, valaddr + embedded_offset),
548 val_print_type_code_int (type, valaddr + embedded_offset, stream);
551 case TYPE_CODE_STRUCT:
552 java_print_value_fields (type, valaddr, embedded_offset,
553 address, stream, recurse, val, options);
557 c_val_print (type, valaddr, embedded_offset, address, stream,
558 recurse, val, options);