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
)
45 struct value_print_options opts
;
47 type
= value_type (val
);
48 address
= VALUE_ADDRESS (val
) + value_offset (val
);
50 if (is_object_type (type
))
54 /* Get the run-time type, and cast the object into that */
56 obj_addr
= unpack_pointer (type
, value_contents (val
));
60 type
= type_from_class (java_class_from_object (val
));
61 type
= lookup_pointer_type (type
);
63 val
= value_at (type
, address
);
67 if (TYPE_CODE (type
) == TYPE_CODE_PTR
&& !value_logical_not (val
))
68 type_print (TYPE_TARGET_TYPE (type
), "", stream
, -1);
70 name
= TYPE_TAG_NAME (type
);
71 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
&& name
!= NULL
72 && (i
= strlen (name
), name
[i
- 1] == ']'))
76 unsigned int things_printed
= 0;
78 struct type
*el_type
= java_primitive_type_from_name (name
, i
- 2);
81 read_memory (address
+ JAVA_OBJECT_SIZE
, buf4
, 4);
83 length
= (long) extract_signed_integer (buf4
, 4);
84 fprintf_filtered (stream
, "{length: %ld", length
);
89 CORE_ADDR next_element
= -1; /* dummy initial value */
91 address
+= JAVA_OBJECT_SIZE
+ 4; /* Skip object header and length. */
93 while (i
< length
&& things_printed
< options
->print_max
)
97 buf
= alloca (gdbarch_ptr_bit (current_gdbarch
) / HOST_CHAR_BIT
);
98 fputs_filtered (", ", stream
);
99 wrap_here (n_spaces (2));
102 element
= next_element
;
105 read_memory (address
, buf
, sizeof (buf
));
106 address
+= gdbarch_ptr_bit (current_gdbarch
) / HOST_CHAR_BIT
;
107 /* FIXME: cagney/2003-05-24: Bogus or what. It
108 pulls a host sized pointer out of the target and
109 then extracts that as an address (while assuming
110 that the address is unsigned)! */
111 element
= extract_unsigned_integer (buf
, sizeof (buf
));
114 for (reps
= 1; i
+ reps
< length
; reps
++)
116 read_memory (address
, buf
, sizeof (buf
));
117 address
+= gdbarch_ptr_bit (current_gdbarch
) / HOST_CHAR_BIT
;
118 /* FIXME: cagney/2003-05-24: Bogus or what. It
119 pulls a host sized pointer out of the target and
120 then extracts that as an address (while assuming
121 that the address is unsigned)! */
122 next_element
= extract_unsigned_integer (buf
, sizeof (buf
));
123 if (next_element
!= element
)
128 fprintf_filtered (stream
, "%d: ", i
);
130 fprintf_filtered (stream
, "%d..%d: ", i
, i
+ reps
- 1);
133 fprintf_filtered (stream
, "null");
135 fprintf_filtered (stream
, "@%s", paddr_nz (element
));
143 struct value
*v
= allocate_value (el_type
);
144 struct value
*next_v
= allocate_value (el_type
);
146 VALUE_ADDRESS (v
) = address
+ JAVA_OBJECT_SIZE
+ 4;
147 VALUE_ADDRESS (next_v
) = VALUE_ADDRESS (v
);
149 while (i
< length
&& things_printed
< options
->print_max
)
151 fputs_filtered (", ", stream
);
152 wrap_here (n_spaces (2));
164 set_value_lazy (v
, 1);
165 set_value_offset (v
, 0);
168 set_value_offset (next_v
, value_offset (v
));
170 for (reps
= 1; i
+ reps
< length
; reps
++)
172 set_value_lazy (next_v
, 1);
173 set_value_offset (next_v
, value_offset (next_v
) + TYPE_LENGTH (el_type
));
174 if (memcmp (value_contents (v
), value_contents (next_v
),
175 TYPE_LENGTH (el_type
)) != 0)
180 fprintf_filtered (stream
, "%d: ", i
);
182 fprintf_filtered (stream
, "%d..%d: ", i
, i
+ reps
- 1);
186 common_val_print (v
, stream
, 1, &opts
, current_language
);
194 fprintf_filtered (stream
, "...");
196 fprintf_filtered (stream
, "}");
201 /* If it's type String, print it */
203 if (TYPE_CODE (type
) == TYPE_CODE_PTR
204 && TYPE_TARGET_TYPE (type
)
205 && TYPE_TAG_NAME (TYPE_TARGET_TYPE (type
))
206 && strcmp (TYPE_TAG_NAME (TYPE_TARGET_TYPE (type
)),
207 "java.lang.String") == 0
208 && (options
->format
== 0 || options
->format
== 's')
210 && value_as_address (val
) != 0)
212 struct value
*data_val
;
214 struct value
*boffset_val
;
215 unsigned long boffset
;
216 struct value
*count_val
;
220 mark
= value_mark (); /* Remember start of new values */
222 data_val
= value_struct_elt (&val
, NULL
, "data", NULL
, NULL
);
223 data
= value_as_address (data_val
);
225 boffset_val
= value_struct_elt (&val
, NULL
, "boffset", NULL
, NULL
);
226 boffset
= value_as_address (boffset_val
);
228 count_val
= value_struct_elt (&val
, NULL
, "count", NULL
, NULL
);
229 count
= value_as_address (count_val
);
231 value_free_to_mark (mark
); /* Release unnecessary values */
233 val_print_string (java_char_type
, data
+ boffset
, count
, stream
, options
);
240 return common_val_print (val
, stream
, 0, &opts
, current_language
);
243 /* TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and OPTIONS have the
244 same meanings as in cp_print_value and c_val_print.
246 DONT_PRINT is an array of baseclass types that we
247 should not print, or zero if called from top level. */
250 java_print_value_fields (struct type
*type
, const gdb_byte
*valaddr
,
251 CORE_ADDR address
, struct ui_file
*stream
,
253 const struct value_print_options
*options
)
255 int i
, len
, n_baseclasses
;
257 CHECK_TYPEDEF (type
);
259 fprintf_filtered (stream
, "{");
260 len
= TYPE_NFIELDS (type
);
261 n_baseclasses
= TYPE_N_BASECLASSES (type
);
263 if (n_baseclasses
> 0)
265 int i
, n_baseclasses
= TYPE_N_BASECLASSES (type
);
267 for (i
= 0; i
< n_baseclasses
; i
++)
270 struct type
*baseclass
= check_typedef (TYPE_BASECLASS (type
, i
));
271 char *basename
= TYPE_NAME (baseclass
);
272 const gdb_byte
*base_valaddr
;
274 if (BASETYPE_VIA_VIRTUAL (type
, i
))
277 if (basename
!= NULL
&& strcmp (basename
, "java.lang.Object") == 0)
284 fprintf_filtered (stream
, "\n");
285 print_spaces_filtered (2 * (recurse
+ 1), stream
);
287 fputs_filtered ("<", stream
);
288 /* Not sure what the best notation is in the case where there is no
290 fputs_filtered (basename
? basename
: "", stream
);
291 fputs_filtered ("> = ", stream
);
293 base_valaddr
= valaddr
;
295 java_print_value_fields (baseclass
, base_valaddr
, address
+ boffset
,
296 stream
, recurse
+ 1, options
);
297 fputs_filtered (", ", stream
);
302 if (!len
&& n_baseclasses
== 1)
303 fprintf_filtered (stream
, "<No data fields>");
308 for (i
= n_baseclasses
; i
< len
; i
++)
310 /* If requested, skip printing of static fields. */
311 if (field_is_static (&TYPE_FIELD (type
, i
)))
313 char *name
= TYPE_FIELD_NAME (type
, i
);
314 if (!options
->static_field_print
)
316 if (name
!= NULL
&& strcmp (name
, "class") == 0)
320 fprintf_filtered (stream
, ", ");
321 else if (n_baseclasses
> 0)
325 fprintf_filtered (stream
, "\n");
326 print_spaces_filtered (2 + 2 * recurse
, stream
);
327 fputs_filtered ("members of ", stream
);
328 fputs_filtered (type_name_no_tag (type
), stream
);
329 fputs_filtered (": ", stream
);
336 fprintf_filtered (stream
, "\n");
337 print_spaces_filtered (2 + 2 * recurse
, stream
);
341 wrap_here (n_spaces (2 + 2 * recurse
));
343 if (options
->inspect_it
)
345 if (TYPE_CODE (TYPE_FIELD_TYPE (type
, i
)) == TYPE_CODE_PTR
)
346 fputs_filtered ("\"( ptr \"", stream
);
348 fputs_filtered ("\"( nodef \"", stream
);
349 if (field_is_static (&TYPE_FIELD (type
, i
)))
350 fputs_filtered ("static ", stream
);
351 fprintf_symbol_filtered (stream
, TYPE_FIELD_NAME (type
, i
),
353 DMGL_PARAMS
| DMGL_ANSI
);
354 fputs_filtered ("\" \"", stream
);
355 fprintf_symbol_filtered (stream
, TYPE_FIELD_NAME (type
, i
),
357 DMGL_PARAMS
| DMGL_ANSI
);
358 fputs_filtered ("\") \"", stream
);
362 annotate_field_begin (TYPE_FIELD_TYPE (type
, i
));
364 if (field_is_static (&TYPE_FIELD (type
, i
)))
365 fputs_filtered ("static ", stream
);
366 fprintf_symbol_filtered (stream
, TYPE_FIELD_NAME (type
, i
),
368 DMGL_PARAMS
| DMGL_ANSI
);
369 annotate_field_name_end ();
370 fputs_filtered (": ", stream
);
371 annotate_field_value ();
374 if (!field_is_static (&TYPE_FIELD (type
, i
))
375 && TYPE_FIELD_PACKED (type
, i
))
379 /* Bitfields require special handling, especially due to byte
381 if (TYPE_FIELD_IGNORE (type
, i
))
383 fputs_filtered ("<optimized out or zero length>", stream
);
387 struct value_print_options opts
;
389 v
= value_from_longest (TYPE_FIELD_TYPE (type
, i
),
390 unpack_field_as_long (type
, valaddr
, i
));
394 common_val_print (v
, stream
, recurse
+ 1,
395 &opts
, current_language
);
400 if (TYPE_FIELD_IGNORE (type
, i
))
402 fputs_filtered ("<optimized out or zero length>", stream
);
404 else if (field_is_static (&TYPE_FIELD (type
, i
)))
406 struct value
*v
= value_static_field (type
, i
);
408 fputs_filtered ("<optimized out>", stream
);
411 struct value_print_options opts
;
412 struct type
*t
= check_typedef (value_type (v
));
413 if (TYPE_CODE (t
) == TYPE_CODE_STRUCT
)
417 common_val_print (v
, stream
, recurse
+ 1,
418 &opts
, current_language
);
421 else if (TYPE_FIELD_TYPE (type
, i
) == NULL
)
422 fputs_filtered ("<unknown type>", stream
);
425 struct value_print_options opts
= *options
;
427 val_print (TYPE_FIELD_TYPE (type
, i
),
428 valaddr
+ TYPE_FIELD_BITPOS (type
, i
) / 8, 0,
429 address
+ TYPE_FIELD_BITPOS (type
, i
) / 8,
430 stream
, recurse
+ 1, &opts
,
434 annotate_field_end ();
439 fprintf_filtered (stream
, "\n");
440 print_spaces_filtered (2 * recurse
, stream
);
443 fprintf_filtered (stream
, "}");
446 /* Print data of type TYPE located at VALADDR (within GDB), which came from
447 the inferior at address ADDRESS, onto stdio stream STREAM according to
448 OPTIONS. The data at VALADDR is in target byte order.
450 If the data are a string pointer, returns the number of string characters
454 java_val_print (struct type
*type
, const gdb_byte
*valaddr
,
455 int embedded_offset
, CORE_ADDR address
,
456 struct ui_file
*stream
, int recurse
,
457 const struct value_print_options
*options
)
459 unsigned int i
= 0; /* Number of characters printed */
460 struct type
*target_type
;
463 CHECK_TYPEDEF (type
);
464 switch (TYPE_CODE (type
))
467 if (options
->format
&& options
->format
!= 's')
469 print_scalar_formatted (valaddr
, type
, options
, 0, stream
);
473 if (options
->vtblprint
&& cp_is_vtbl_ptr_type (type
))
475 /* Print the unmangled name if desired. */
476 /* Print vtable entry - we only get here if we ARE using
477 -fvtable_thunks. (Otherwise, look under TYPE_CODE_STRUCT.) */
478 /* Extract an address, assume that it is unsigned. */
479 print_address_demangle (extract_unsigned_integer (valaddr
, TYPE_LENGTH (type
)),
484 addr
= unpack_pointer (type
, valaddr
);
487 fputs_filtered ("null", stream
);
490 target_type
= check_typedef (TYPE_TARGET_TYPE (type
));
492 if (TYPE_CODE (target_type
) == TYPE_CODE_FUNC
)
494 /* Try to print what function it points to. */
495 print_address_demangle (addr
, stream
, demangle
);
496 /* Return value is irrelevant except for string pointers. */
500 if (options
->addressprint
&& options
->format
!= 's')
502 fputs_filtered ("@", stream
);
503 print_longest (stream
, 'x', 0, (ULONGEST
) addr
);
510 /* Can't just call c_val_print because that prints bytes as C
512 if (options
->format
|| options
->output_format
)
514 struct value_print_options opts
= *options
;
515 opts
.format
= (options
->format
? options
->format
516 : options
->output_format
);
517 print_scalar_formatted (valaddr
, type
, &opts
, 0, stream
);
519 else if (TYPE_CODE (type
) == TYPE_CODE_CHAR
520 || (TYPE_CODE (type
) == TYPE_CODE_INT
521 && TYPE_LENGTH (type
) == 2
522 && strcmp (TYPE_NAME (type
), "char") == 0))
523 LA_PRINT_CHAR ((int) unpack_long (type
, valaddr
), type
, stream
);
525 val_print_type_code_int (type
, valaddr
, stream
);
528 case TYPE_CODE_STRUCT
:
529 java_print_value_fields (type
, valaddr
, address
, stream
, recurse
,
534 return c_val_print (type
, valaddr
, embedded_offset
, address
, stream
,