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, 2010, 2011 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);
84 read_memory (address
+ get_java_object_header_size (gdbarch
), buf4
, 4);
86 length
= (long) extract_signed_integer (buf4
, 4, byte_order
);
87 fprintf_filtered (stream
, "{length: %ld", length
);
92 CORE_ADDR next_element
= -1; /* Dummy initial value. */
94 /* Skip object header and length. */
95 address
+= get_java_object_header_size (gdbarch
) + 4;
97 while (i
< length
&& things_printed
< options
->print_max
)
101 buf
= alloca (gdbarch_ptr_bit (gdbarch
) / HOST_CHAR_BIT
);
102 fputs_filtered (", ", stream
);
103 wrap_here (n_spaces (2));
106 element
= next_element
;
109 read_memory (address
, buf
, sizeof (buf
));
110 address
+= gdbarch_ptr_bit (gdbarch
) / HOST_CHAR_BIT
;
111 /* FIXME: cagney/2003-05-24: Bogus or what. It
112 pulls a host sized pointer out of the target and
113 then extracts that as an address (while assuming
114 that the address is unsigned)! */
115 element
= extract_unsigned_integer (buf
, sizeof (buf
),
119 for (reps
= 1; i
+ reps
< length
; reps
++)
121 read_memory (address
, buf
, sizeof (buf
));
122 address
+= gdbarch_ptr_bit (gdbarch
) / HOST_CHAR_BIT
;
123 /* FIXME: cagney/2003-05-24: Bogus or what. It
124 pulls a host sized pointer out of the target and
125 then extracts that as an address (while assuming
126 that the address is unsigned)! */
127 next_element
= extract_unsigned_integer (buf
, sizeof (buf
),
129 if (next_element
!= element
)
134 fprintf_filtered (stream
, "%d: ", i
);
136 fprintf_filtered (stream
, "%d..%d: ", i
, i
+ reps
- 1);
139 fprintf_filtered (stream
, "null");
141 fprintf_filtered (stream
, "@%s", paddress (gdbarch
, element
));
149 struct value
*v
= allocate_value (el_type
);
150 struct value
*next_v
= allocate_value (el_type
);
152 set_value_address (v
, (address
153 + get_java_object_header_size (gdbarch
) + 4));
154 set_value_address (next_v
, value_raw_address (v
));
156 while (i
< length
&& things_printed
< options
->print_max
)
158 fputs_filtered (", ", stream
);
159 wrap_here (n_spaces (2));
171 set_value_lazy (v
, 1);
172 set_value_offset (v
, 0);
175 set_value_offset (next_v
, value_offset (v
));
177 for (reps
= 1; i
+ reps
< length
; reps
++)
179 set_value_lazy (next_v
, 1);
180 set_value_offset (next_v
, value_offset (next_v
)
181 + TYPE_LENGTH (el_type
));
182 if (memcmp (value_contents (v
), value_contents (next_v
),
183 TYPE_LENGTH (el_type
)) != 0)
188 fprintf_filtered (stream
, "%d: ", i
);
190 fprintf_filtered (stream
, "%d..%d: ", i
, i
+ reps
- 1);
194 common_val_print (v
, stream
, 1, &opts
, current_language
);
202 fprintf_filtered (stream
, "...");
204 fprintf_filtered (stream
, "}");
209 /* If it's type String, print it. */
211 if (TYPE_CODE (type
) == TYPE_CODE_PTR
212 && TYPE_TARGET_TYPE (type
)
213 && TYPE_TAG_NAME (TYPE_TARGET_TYPE (type
))
214 && strcmp (TYPE_TAG_NAME (TYPE_TARGET_TYPE (type
)),
215 "java.lang.String") == 0
216 && (options
->format
== 0 || options
->format
== 's')
218 && value_as_address (val
) != 0)
220 struct type
*char_type
;
221 struct value
*data_val
;
223 struct value
*boffset_val
;
224 unsigned long boffset
;
225 struct value
*count_val
;
229 mark
= value_mark (); /* Remember start of new values. */
231 data_val
= value_struct_elt (&val
, NULL
, "data", NULL
, NULL
);
232 data
= value_as_address (data_val
);
234 boffset_val
= value_struct_elt (&val
, NULL
, "boffset", NULL
, NULL
);
235 boffset
= value_as_address (boffset_val
);
237 count_val
= value_struct_elt (&val
, NULL
, "count", NULL
, NULL
);
238 count
= value_as_address (count_val
);
240 value_free_to_mark (mark
); /* Release unnecessary values. */
242 char_type
= builtin_java_type (gdbarch
)->builtin_char
;
243 val_print_string (char_type
, NULL
, data
+ boffset
, count
, stream
,
251 return common_val_print (val
, stream
, 0, &opts
, current_language
);
254 /* TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and OPTIONS have the
255 same meanings as in cp_print_value and c_val_print.
257 DONT_PRINT is an array of baseclass types that we
258 should not print, or zero if called from top level. */
261 java_print_value_fields (struct type
*type
, const gdb_byte
*valaddr
,
262 CORE_ADDR address
, struct ui_file
*stream
,
264 const struct value
*val
,
265 const struct value_print_options
*options
)
267 int i
, len
, n_baseclasses
;
269 CHECK_TYPEDEF (type
);
271 fprintf_filtered (stream
, "{");
272 len
= TYPE_NFIELDS (type
);
273 n_baseclasses
= TYPE_N_BASECLASSES (type
);
275 if (n_baseclasses
> 0)
277 int i
, n_baseclasses
= TYPE_N_BASECLASSES (type
);
279 for (i
= 0; i
< n_baseclasses
; i
++)
282 struct type
*baseclass
= check_typedef (TYPE_BASECLASS (type
, i
));
283 char *basename
= TYPE_NAME (baseclass
);
284 const gdb_byte
*base_valaddr
;
286 if (BASETYPE_VIA_VIRTUAL (type
, i
))
289 if (basename
!= NULL
&& strcmp (basename
, "java.lang.Object") == 0)
296 fprintf_filtered (stream
, "\n");
297 print_spaces_filtered (2 * (recurse
+ 1), stream
);
299 fputs_filtered ("<", stream
);
300 /* Not sure what the best notation is in the case where there is no
302 fputs_filtered (basename
? basename
: "", stream
);
303 fputs_filtered ("> = ", stream
);
305 base_valaddr
= valaddr
;
307 java_print_value_fields (baseclass
, base_valaddr
, address
+ boffset
,
308 stream
, recurse
+ 1, val
, options
);
309 fputs_filtered (", ", stream
);
314 if (!len
&& n_baseclasses
== 1)
315 fprintf_filtered (stream
, "<No data fields>");
320 for (i
= n_baseclasses
; i
< len
; i
++)
322 /* If requested, skip printing of static fields. */
323 if (field_is_static (&TYPE_FIELD (type
, i
)))
325 char *name
= TYPE_FIELD_NAME (type
, i
);
327 if (!options
->static_field_print
)
329 if (name
!= NULL
&& strcmp (name
, "class") == 0)
333 fprintf_filtered (stream
, ", ");
334 else if (n_baseclasses
> 0)
338 fprintf_filtered (stream
, "\n");
339 print_spaces_filtered (2 + 2 * recurse
, stream
);
340 fputs_filtered ("members of ", stream
);
341 fputs_filtered (type_name_no_tag (type
), stream
);
342 fputs_filtered (": ", stream
);
349 fprintf_filtered (stream
, "\n");
350 print_spaces_filtered (2 + 2 * recurse
, stream
);
354 wrap_here (n_spaces (2 + 2 * recurse
));
356 if (options
->inspect_it
)
358 if (TYPE_CODE (TYPE_FIELD_TYPE (type
, i
)) == TYPE_CODE_PTR
)
359 fputs_filtered ("\"( ptr \"", stream
);
361 fputs_filtered ("\"( nodef \"", stream
);
362 if (field_is_static (&TYPE_FIELD (type
, i
)))
363 fputs_filtered ("static ", stream
);
364 fprintf_symbol_filtered (stream
, TYPE_FIELD_NAME (type
, i
),
366 DMGL_PARAMS
| DMGL_ANSI
);
367 fputs_filtered ("\" \"", stream
);
368 fprintf_symbol_filtered (stream
, TYPE_FIELD_NAME (type
, i
),
370 DMGL_PARAMS
| DMGL_ANSI
);
371 fputs_filtered ("\") \"", stream
);
375 annotate_field_begin (TYPE_FIELD_TYPE (type
, i
));
377 if (field_is_static (&TYPE_FIELD (type
, i
)))
378 fputs_filtered ("static ", stream
);
379 fprintf_symbol_filtered (stream
, TYPE_FIELD_NAME (type
, i
),
381 DMGL_PARAMS
| DMGL_ANSI
);
382 annotate_field_name_end ();
383 fputs_filtered (": ", stream
);
384 annotate_field_value ();
387 if (!field_is_static (&TYPE_FIELD (type
, i
))
388 && TYPE_FIELD_PACKED (type
, i
))
392 /* Bitfields require special handling, especially due to byte
394 if (TYPE_FIELD_IGNORE (type
, i
))
396 fputs_filtered ("<optimized out or zero length>", stream
);
398 else if (value_bits_synthetic_pointer (val
,
399 TYPE_FIELD_BITPOS (type
,
401 TYPE_FIELD_BITSIZE (type
,
404 fputs_filtered (_("<synthetic pointer>"), stream
);
406 else if (!value_bits_valid (val
, TYPE_FIELD_BITPOS (type
, i
),
407 TYPE_FIELD_BITSIZE (type
, i
)))
409 fputs_filtered (_("<value optimized out>"), stream
);
413 struct value_print_options opts
;
415 v
= value_from_longest (TYPE_FIELD_TYPE (type
, i
),
416 unpack_field_as_long (type
, valaddr
, i
));
420 common_val_print (v
, stream
, recurse
+ 1,
421 &opts
, current_language
);
426 if (TYPE_FIELD_IGNORE (type
, i
))
428 fputs_filtered ("<optimized out or zero length>", stream
);
430 else if (field_is_static (&TYPE_FIELD (type
, i
)))
432 struct value
*v
= value_static_field (type
, i
);
435 fputs_filtered ("<optimized out>", stream
);
438 struct value_print_options opts
;
439 struct type
*t
= check_typedef (value_type (v
));
441 if (TYPE_CODE (t
) == TYPE_CODE_STRUCT
)
445 common_val_print (v
, stream
, recurse
+ 1,
446 &opts
, current_language
);
449 else if (TYPE_FIELD_TYPE (type
, i
) == NULL
)
450 fputs_filtered ("<unknown type>", stream
);
453 struct value_print_options opts
= *options
;
456 val_print (TYPE_FIELD_TYPE (type
, i
),
457 valaddr
+ TYPE_FIELD_BITPOS (type
, i
) / 8, 0,
458 address
+ TYPE_FIELD_BITPOS (type
, i
) / 8,
459 stream
, recurse
+ 1, val
, &opts
,
463 annotate_field_end ();
468 fprintf_filtered (stream
, "\n");
469 print_spaces_filtered (2 * recurse
, stream
);
472 fprintf_filtered (stream
, "}");
475 /* Print data of type TYPE located at VALADDR (within GDB), which came from
476 the inferior at address ADDRESS, onto stdio stream STREAM according to
477 OPTIONS. The data at VALADDR is in target byte order.
479 If the data are a string pointer, returns the number of string characters
483 java_val_print (struct type
*type
, const gdb_byte
*valaddr
,
484 int embedded_offset
, CORE_ADDR address
,
485 struct ui_file
*stream
, int recurse
,
486 const struct value
*val
,
487 const struct value_print_options
*options
)
489 struct gdbarch
*gdbarch
= get_type_arch (type
);
490 unsigned int i
= 0; /* Number of characters printed. */
491 struct type
*target_type
;
494 CHECK_TYPEDEF (type
);
495 switch (TYPE_CODE (type
))
498 if (options
->format
&& options
->format
!= 's')
500 print_scalar_formatted (valaddr
, type
, 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 (gdbarch
,
511 extract_unsigned_integer (valaddr
,
517 addr
= unpack_pointer (type
, valaddr
);
520 fputs_filtered ("null", stream
);
523 target_type
= check_typedef (TYPE_TARGET_TYPE (type
));
525 if (TYPE_CODE (target_type
) == TYPE_CODE_FUNC
)
527 /* Try to print what function it points to. */
528 print_address_demangle (gdbarch
, addr
, stream
, demangle
);
529 /* Return value is irrelevant except for string pointers. */
533 if (options
->addressprint
&& options
->format
!= 's')
535 fputs_filtered ("@", stream
);
536 print_longest (stream
, 'x', 0, (ULONGEST
) addr
);
543 /* Can't just call c_val_print because that prints bytes as C
545 if (options
->format
|| options
->output_format
)
547 struct value_print_options opts
= *options
;
549 opts
.format
= (options
->format
? options
->format
550 : options
->output_format
);
551 print_scalar_formatted (valaddr
, type
, &opts
, 0, stream
);
553 else if (TYPE_CODE (type
) == TYPE_CODE_CHAR
554 || (TYPE_CODE (type
) == TYPE_CODE_INT
555 && TYPE_LENGTH (type
) == 2
556 && strcmp (TYPE_NAME (type
), "char") == 0))
557 LA_PRINT_CHAR ((int) unpack_long (type
, valaddr
), type
, stream
);
559 val_print_type_code_int (type
, valaddr
, stream
);
562 case TYPE_CODE_STRUCT
:
563 java_print_value_fields (type
, valaddr
, address
, stream
, recurse
,
568 return c_val_print (type
, valaddr
, embedded_offset
, address
, stream
,
569 recurse
, val
, options
);