1 /* Support for printing Java values for GDB, the GNU debugger.
3 Copyright (C) 1997-2014 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"
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
);
68 type
= value_type (val
);
72 if (TYPE_CODE (type
) == TYPE_CODE_PTR
&& !value_logical_not (val
))
73 type_print (TYPE_TARGET_TYPE (type
), "", stream
, -1);
75 name
= TYPE_TAG_NAME (type
);
76 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
&& name
!= NULL
77 && (i
= strlen (name
), name
[i
- 1] == ']'))
81 unsigned int things_printed
= 0;
84 = java_primitive_type_from_name (gdbarch
, name
, i
- 2);
87 read_memory (address
+ get_java_object_header_size (gdbarch
), buf4
, 4);
89 length
= (long) extract_signed_integer (buf4
, 4, byte_order
);
90 fprintf_filtered (stream
, "{length: %ld", length
);
95 CORE_ADDR next_element
= -1; /* Dummy initial value. */
97 /* Skip object header and length. */
98 address
+= get_java_object_header_size (gdbarch
) + 4;
100 while (i
< length
&& things_printed
< options
->print_max
)
104 buf
= alloca (gdbarch_ptr_bit (gdbarch
) / HOST_CHAR_BIT
);
105 fputs_filtered (", ", stream
);
106 wrap_here (n_spaces (2));
109 element
= next_element
;
112 read_memory (address
, buf
, sizeof (buf
));
113 address
+= gdbarch_ptr_bit (gdbarch
) / HOST_CHAR_BIT
;
114 /* FIXME: cagney/2003-05-24: Bogus or what. It
115 pulls a host sized pointer out of the target and
116 then extracts that as an address (while assuming
117 that the address is unsigned)! */
118 element
= extract_unsigned_integer (buf
, sizeof (buf
),
122 for (reps
= 1; i
+ reps
< length
; reps
++)
124 read_memory (address
, buf
, sizeof (buf
));
125 address
+= gdbarch_ptr_bit (gdbarch
) / 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
),
132 if (next_element
!= element
)
137 fprintf_filtered (stream
, "%d: ", i
);
139 fprintf_filtered (stream
, "%d..%d: ", i
, i
+ reps
- 1);
142 fprintf_filtered (stream
, "null");
144 fprintf_filtered (stream
, "@%s", paddress (gdbarch
, element
));
152 struct value
*v
= allocate_value (el_type
);
153 struct value
*next_v
= allocate_value (el_type
);
155 set_value_address (v
, (address
156 + get_java_object_header_size (gdbarch
) + 4));
157 set_value_address (next_v
, value_raw_address (v
));
159 while (i
< length
&& things_printed
< options
->print_max
)
161 fputs_filtered (", ", stream
);
162 wrap_here (n_spaces (2));
174 set_value_lazy (v
, 1);
175 set_value_offset (v
, 0);
178 set_value_offset (next_v
, value_offset (v
));
180 for (reps
= 1; i
+ reps
< length
; reps
++)
182 set_value_lazy (next_v
, 1);
183 set_value_offset (next_v
, value_offset (next_v
)
184 + TYPE_LENGTH (el_type
));
185 value_fetch_lazy (next_v
);
186 if (!(value_available_contents_eq
187 (v
, value_embedded_offset (v
),
188 next_v
, value_embedded_offset (next_v
),
189 TYPE_LENGTH (el_type
))))
194 fprintf_filtered (stream
, "%d: ", i
);
196 fprintf_filtered (stream
, "%d..%d: ", i
, i
+ reps
- 1);
200 common_val_print (v
, stream
, 1, &opts
, current_language
);
208 fprintf_filtered (stream
, "...");
210 fprintf_filtered (stream
, "}");
215 /* If it's type String, print it. */
217 if (TYPE_CODE (type
) == TYPE_CODE_PTR
218 && TYPE_TARGET_TYPE (type
)
219 && TYPE_TAG_NAME (TYPE_TARGET_TYPE (type
))
220 && strcmp (TYPE_TAG_NAME (TYPE_TARGET_TYPE (type
)),
221 "java.lang.String") == 0
222 && (options
->format
== 0 || options
->format
== 's')
224 && value_as_address (val
) != 0)
226 struct type
*char_type
;
227 struct value
*data_val
;
229 struct value
*boffset_val
;
230 unsigned long boffset
;
231 struct value
*count_val
;
235 fputs_filtered (" ", stream
);
237 mark
= value_mark (); /* Remember start of new values. */
239 data_val
= value_struct_elt (&val
, NULL
, "data", NULL
, NULL
);
240 data
= value_as_address (data_val
);
242 boffset_val
= value_struct_elt (&val
, NULL
, "boffset", NULL
, NULL
);
243 boffset
= value_as_address (boffset_val
);
245 count_val
= value_struct_elt (&val
, NULL
, "count", NULL
, NULL
);
246 count
= value_as_address (count_val
);
248 value_free_to_mark (mark
); /* Release unnecessary values. */
250 char_type
= builtin_java_type (gdbarch
)->builtin_char
;
251 val_print_string (char_type
, NULL
, data
+ boffset
, count
, stream
,
259 common_val_print (val
, stream
, 0, &opts
, current_language
);
262 /* TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and OPTIONS have the
263 same meanings as in cp_print_value and c_val_print.
265 DONT_PRINT is an array of baseclass types that we
266 should not print, or zero if called from top level. */
269 java_print_value_fields (struct type
*type
, const gdb_byte
*valaddr
,
271 CORE_ADDR address
, struct ui_file
*stream
,
273 const struct value
*val
,
274 const struct value_print_options
*options
)
276 int i
, len
, n_baseclasses
;
278 CHECK_TYPEDEF (type
);
280 fprintf_filtered (stream
, "{");
281 len
= TYPE_NFIELDS (type
);
282 n_baseclasses
= TYPE_N_BASECLASSES (type
);
284 if (n_baseclasses
> 0)
286 int i
, n_baseclasses
= TYPE_N_BASECLASSES (type
);
288 for (i
= 0; i
< n_baseclasses
; i
++)
291 struct type
*baseclass
= check_typedef (TYPE_BASECLASS (type
, i
));
292 const char *basename
= TYPE_NAME (baseclass
);
293 const gdb_byte
*base_valaddr
;
295 if (BASETYPE_VIA_VIRTUAL (type
, i
))
298 if (basename
!= NULL
&& strcmp (basename
, "java.lang.Object") == 0)
303 if (options
->prettyformat
)
305 fprintf_filtered (stream
, "\n");
306 print_spaces_filtered (2 * (recurse
+ 1), stream
);
308 fputs_filtered ("<", stream
);
309 /* Not sure what the best notation is in the case where there is no
311 fputs_filtered (basename
? basename
: "", stream
);
312 fputs_filtered ("> = ", stream
);
314 base_valaddr
= valaddr
;
316 java_print_value_fields (baseclass
, base_valaddr
,
317 offset
+ boffset
, address
,
318 stream
, recurse
+ 1, val
, options
);
319 fputs_filtered (", ", stream
);
323 if (!len
&& n_baseclasses
== 1)
324 fprintf_filtered (stream
, "<No data fields>");
329 for (i
= n_baseclasses
; i
< len
; i
++)
331 /* If requested, skip printing of static fields. */
332 if (field_is_static (&TYPE_FIELD (type
, i
)))
334 const char *name
= TYPE_FIELD_NAME (type
, i
);
336 if (!options
->static_field_print
)
338 if (name
!= NULL
&& strcmp (name
, "class") == 0)
342 fprintf_filtered (stream
, ", ");
343 else if (n_baseclasses
> 0)
345 if (options
->prettyformat
)
347 fprintf_filtered (stream
, "\n");
348 print_spaces_filtered (2 + 2 * recurse
, stream
);
349 fputs_filtered ("members of ", stream
);
350 fputs_filtered (type_name_no_tag (type
), stream
);
351 fputs_filtered (": ", stream
);
356 if (options
->prettyformat
)
358 fprintf_filtered (stream
, "\n");
359 print_spaces_filtered (2 + 2 * recurse
, stream
);
363 wrap_here (n_spaces (2 + 2 * recurse
));
366 annotate_field_begin (TYPE_FIELD_TYPE (type
, i
));
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 annotate_field_name_end ();
374 fputs_filtered (": ", stream
);
375 annotate_field_value ();
377 if (!field_is_static (&TYPE_FIELD (type
, i
))
378 && TYPE_FIELD_PACKED (type
, i
))
382 /* Bitfields require special handling, especially due to byte
384 if (TYPE_FIELD_IGNORE (type
, i
))
386 fputs_filtered ("<optimized out or zero length>", stream
);
388 else if (value_bits_synthetic_pointer (val
,
389 TYPE_FIELD_BITPOS (type
,
391 TYPE_FIELD_BITSIZE (type
,
394 fputs_filtered (_("<synthetic pointer>"), stream
);
396 else if (!value_bits_valid (val
, TYPE_FIELD_BITPOS (type
, i
),
397 TYPE_FIELD_BITSIZE (type
, i
)))
399 val_print_optimized_out (val
, stream
);
403 struct value_print_options opts
;
405 v
= value_field_bitfield (type
, i
, valaddr
, offset
, val
);
409 common_val_print (v
, stream
, recurse
+ 1,
410 &opts
, current_language
);
415 if (TYPE_FIELD_IGNORE (type
, i
))
417 fputs_filtered ("<optimized out or zero length>", stream
);
419 else if (field_is_static (&TYPE_FIELD (type
, i
)))
421 struct value_print_options opts
;
422 struct value
*v
= value_static_field (type
, i
);
423 struct type
*t
= check_typedef (value_type (v
));
425 if (TYPE_CODE (t
) == TYPE_CODE_STRUCT
)
429 common_val_print (v
, stream
, recurse
+ 1,
430 &opts
, current_language
);
432 else if (TYPE_FIELD_TYPE (type
, i
) == NULL
)
433 fputs_filtered ("<unknown type>", stream
);
436 struct value_print_options opts
= *options
;
439 val_print (TYPE_FIELD_TYPE (type
, i
),
441 offset
+ TYPE_FIELD_BITPOS (type
, i
) / 8,
442 address
, stream
, recurse
+ 1, val
, &opts
,
446 annotate_field_end ();
449 if (options
->prettyformat
)
451 fprintf_filtered (stream
, "\n");
452 print_spaces_filtered (2 * recurse
, stream
);
455 fprintf_filtered (stream
, "}");
458 /* See val_print for a description of the various parameters of this
459 function; they are identical. */
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
*val
,
466 const struct value_print_options
*options
)
468 struct gdbarch
*gdbarch
= get_type_arch (type
);
469 struct type
*target_type
;
472 CHECK_TYPEDEF (type
);
473 switch (TYPE_CODE (type
))
476 if (options
->format
&& options
->format
!= 's')
478 val_print_scalar_formatted (type
, valaddr
, embedded_offset
,
479 val
, options
, 0, stream
);
482 addr
= unpack_pointer (type
, valaddr
+ embedded_offset
);
485 fputs_filtered ("null", stream
);
488 target_type
= check_typedef (TYPE_TARGET_TYPE (type
));
490 if (TYPE_CODE (target_type
) == TYPE_CODE_FUNC
)
492 /* Try to print what function it points to. */
493 print_address_demangle (options
, gdbarch
, addr
, stream
, demangle
);
497 if (options
->addressprint
&& options
->format
!= 's')
499 fputs_filtered ("@", stream
);
500 print_longest (stream
, 'x', 0, (ULONGEST
) addr
);
507 /* Can't just call c_val_print because that prints bytes as C
509 if (options
->format
|| options
->output_format
)
511 struct value_print_options opts
= *options
;
513 opts
.format
= (options
->format
? options
->format
514 : options
->output_format
);
515 val_print_scalar_formatted (type
, valaddr
, embedded_offset
,
516 val
, &opts
, 0, stream
);
518 else if (TYPE_CODE (type
) == TYPE_CODE_CHAR
519 || (TYPE_CODE (type
) == TYPE_CODE_INT
520 && TYPE_LENGTH (type
) == 2
521 && strcmp (TYPE_NAME (type
), "char") == 0))
522 LA_PRINT_CHAR ((int) unpack_long (type
, valaddr
+ embedded_offset
),
525 val_print_type_code_int (type
, valaddr
+ embedded_offset
, stream
);
528 case TYPE_CODE_STRUCT
:
529 java_print_value_fields (type
, valaddr
, embedded_offset
,
530 address
, stream
, recurse
, val
, options
);
534 c_val_print (type
, valaddr
, embedded_offset
, address
, stream
,
535 recurse
, val
, options
);