a15bc5768ab457f997fada1c74adc2176befd408
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
= current_gdbarch
;
46 struct value_print_options opts
;
48 type
= value_type (val
);
49 address
= value_address (val
);
51 if (is_object_type (type
))
55 /* Get the run-time type, and cast the object into that */
57 obj_addr
= unpack_pointer (type
, value_contents (val
));
61 type
= type_from_class (java_class_from_object (val
));
62 type
= lookup_pointer_type (type
);
64 val
= value_at (type
, address
);
68 if (TYPE_CODE (type
) == TYPE_CODE_PTR
&& !value_logical_not (val
))
69 type_print (TYPE_TARGET_TYPE (type
), "", stream
, -1);
71 name
= TYPE_TAG_NAME (type
);
72 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
&& name
!= NULL
73 && (i
= strlen (name
), name
[i
- 1] == ']'))
77 unsigned int things_printed
= 0;
79 struct type
*el_type
= java_primitive_type_from_name (name
, i
- 2);
81 read_memory (address
+ get_java_object_header_size (gdbarch
), 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 /* Skip object header and length. */
92 address
+= get_java_object_header_size (gdbarch
) + 4;
94 while (i
< length
&& things_printed
< options
->print_max
)
98 buf
= alloca (gdbarch_ptr_bit (gdbarch
) / HOST_CHAR_BIT
);
99 fputs_filtered (", ", stream
);
100 wrap_here (n_spaces (2));
103 element
= next_element
;
106 read_memory (address
, buf
, sizeof (buf
));
107 address
+= gdbarch_ptr_bit (gdbarch
) / HOST_CHAR_BIT
;
108 /* FIXME: cagney/2003-05-24: Bogus or what. It
109 pulls a host sized pointer out of the target and
110 then extracts that as an address (while assuming
111 that the address is unsigned)! */
112 element
= extract_unsigned_integer (buf
, sizeof (buf
));
115 for (reps
= 1; i
+ reps
< length
; reps
++)
117 read_memory (address
, buf
, sizeof (buf
));
118 address
+= gdbarch_ptr_bit (gdbarch
) / HOST_CHAR_BIT
;
119 /* FIXME: cagney/2003-05-24: Bogus or what. It
120 pulls a host sized pointer out of the target and
121 then extracts that as an address (while assuming
122 that the address is unsigned)! */
123 next_element
= extract_unsigned_integer (buf
, sizeof (buf
));
124 if (next_element
!= element
)
129 fprintf_filtered (stream
, "%d: ", i
);
131 fprintf_filtered (stream
, "%d..%d: ", i
, i
+ reps
- 1);
134 fprintf_filtered (stream
, "null");
136 fprintf_filtered (stream
, "@%s", paddr_nz (element
));
144 struct value
*v
= allocate_value (el_type
);
145 struct value
*next_v
= allocate_value (el_type
);
147 set_value_address (v
, (address
148 + get_java_object_header_size (gdbarch
) + 4));
149 set_value_address (next_v
, value_raw_address (v
));
151 while (i
< length
&& things_printed
< options
->print_max
)
153 fputs_filtered (", ", stream
);
154 wrap_here (n_spaces (2));
166 set_value_lazy (v
, 1);
167 set_value_offset (v
, 0);
170 set_value_offset (next_v
, value_offset (v
));
172 for (reps
= 1; i
+ reps
< length
; reps
++)
174 set_value_lazy (next_v
, 1);
175 set_value_offset (next_v
, value_offset (next_v
) + TYPE_LENGTH (el_type
));
176 if (memcmp (value_contents (v
), value_contents (next_v
),
177 TYPE_LENGTH (el_type
)) != 0)
182 fprintf_filtered (stream
, "%d: ", i
);
184 fprintf_filtered (stream
, "%d..%d: ", i
, i
+ reps
- 1);
188 common_val_print (v
, stream
, 1, &opts
, current_language
);
196 fprintf_filtered (stream
, "...");
198 fprintf_filtered (stream
, "}");
203 /* If it's type String, print it */
205 if (TYPE_CODE (type
) == TYPE_CODE_PTR
206 && TYPE_TARGET_TYPE (type
)
207 && TYPE_TAG_NAME (TYPE_TARGET_TYPE (type
))
208 && strcmp (TYPE_TAG_NAME (TYPE_TARGET_TYPE (type
)),
209 "java.lang.String") == 0
210 && (options
->format
== 0 || options
->format
== 's')
212 && value_as_address (val
) != 0)
214 struct value
*data_val
;
216 struct value
*boffset_val
;
217 unsigned long boffset
;
218 struct value
*count_val
;
222 mark
= value_mark (); /* Remember start of new values */
224 data_val
= value_struct_elt (&val
, NULL
, "data", NULL
, NULL
);
225 data
= value_as_address (data_val
);
227 boffset_val
= value_struct_elt (&val
, NULL
, "boffset", NULL
, NULL
);
228 boffset
= value_as_address (boffset_val
);
230 count_val
= value_struct_elt (&val
, NULL
, "count", NULL
, NULL
);
231 count
= value_as_address (count_val
);
233 value_free_to_mark (mark
); /* Release unnecessary values */
235 val_print_string (java_char_type
, data
+ boffset
, count
, stream
, options
);
242 return common_val_print (val
, stream
, 0, &opts
, current_language
);
245 /* TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and OPTIONS have the
246 same meanings as in cp_print_value and c_val_print.
248 DONT_PRINT is an array of baseclass types that we
249 should not print, or zero if called from top level. */
252 java_print_value_fields (struct type
*type
, const gdb_byte
*valaddr
,
253 CORE_ADDR address
, struct ui_file
*stream
,
255 const struct value_print_options
*options
)
257 int i
, len
, n_baseclasses
;
259 CHECK_TYPEDEF (type
);
261 fprintf_filtered (stream
, "{");
262 len
= TYPE_NFIELDS (type
);
263 n_baseclasses
= TYPE_N_BASECLASSES (type
);
265 if (n_baseclasses
> 0)
267 int i
, n_baseclasses
= TYPE_N_BASECLASSES (type
);
269 for (i
= 0; i
< n_baseclasses
; i
++)
272 struct type
*baseclass
= check_typedef (TYPE_BASECLASS (type
, i
));
273 char *basename
= TYPE_NAME (baseclass
);
274 const gdb_byte
*base_valaddr
;
276 if (BASETYPE_VIA_VIRTUAL (type
, i
))
279 if (basename
!= NULL
&& strcmp (basename
, "java.lang.Object") == 0)
286 fprintf_filtered (stream
, "\n");
287 print_spaces_filtered (2 * (recurse
+ 1), stream
);
289 fputs_filtered ("<", stream
);
290 /* Not sure what the best notation is in the case where there is no
292 fputs_filtered (basename
? basename
: "", stream
);
293 fputs_filtered ("> = ", stream
);
295 base_valaddr
= valaddr
;
297 java_print_value_fields (baseclass
, base_valaddr
, address
+ boffset
,
298 stream
, recurse
+ 1, options
);
299 fputs_filtered (", ", stream
);
304 if (!len
&& n_baseclasses
== 1)
305 fprintf_filtered (stream
, "<No data fields>");
310 for (i
= n_baseclasses
; i
< len
; i
++)
312 /* If requested, skip printing of static fields. */
313 if (field_is_static (&TYPE_FIELD (type
, i
)))
315 char *name
= TYPE_FIELD_NAME (type
, i
);
316 if (!options
->static_field_print
)
318 if (name
!= NULL
&& strcmp (name
, "class") == 0)
322 fprintf_filtered (stream
, ", ");
323 else if (n_baseclasses
> 0)
327 fprintf_filtered (stream
, "\n");
328 print_spaces_filtered (2 + 2 * recurse
, stream
);
329 fputs_filtered ("members of ", stream
);
330 fputs_filtered (type_name_no_tag (type
), stream
);
331 fputs_filtered (": ", stream
);
338 fprintf_filtered (stream
, "\n");
339 print_spaces_filtered (2 + 2 * recurse
, stream
);
343 wrap_here (n_spaces (2 + 2 * recurse
));
345 if (options
->inspect_it
)
347 if (TYPE_CODE (TYPE_FIELD_TYPE (type
, i
)) == TYPE_CODE_PTR
)
348 fputs_filtered ("\"( ptr \"", stream
);
350 fputs_filtered ("\"( nodef \"", stream
);
351 if (field_is_static (&TYPE_FIELD (type
, i
)))
352 fputs_filtered ("static ", stream
);
353 fprintf_symbol_filtered (stream
, TYPE_FIELD_NAME (type
, i
),
355 DMGL_PARAMS
| DMGL_ANSI
);
356 fputs_filtered ("\" \"", stream
);
357 fprintf_symbol_filtered (stream
, TYPE_FIELD_NAME (type
, i
),
359 DMGL_PARAMS
| DMGL_ANSI
);
360 fputs_filtered ("\") \"", stream
);
364 annotate_field_begin (TYPE_FIELD_TYPE (type
, i
));
366 if (field_is_static (&TYPE_FIELD (type
, i
)))
367 fputs_filtered ("static ", stream
);
368 fprintf_symbol_filtered (stream
, TYPE_FIELD_NAME (type
, i
),
370 DMGL_PARAMS
| DMGL_ANSI
);
371 annotate_field_name_end ();
372 fputs_filtered (": ", stream
);
373 annotate_field_value ();
376 if (!field_is_static (&TYPE_FIELD (type
, i
))
377 && TYPE_FIELD_PACKED (type
, i
))
381 /* Bitfields require special handling, especially due to byte
383 if (TYPE_FIELD_IGNORE (type
, i
))
385 fputs_filtered ("<optimized out or zero length>", stream
);
389 struct value_print_options opts
;
391 v
= value_from_longest (TYPE_FIELD_TYPE (type
, i
),
392 unpack_field_as_long (type
, valaddr
, i
));
396 common_val_print (v
, stream
, recurse
+ 1,
397 &opts
, current_language
);
402 if (TYPE_FIELD_IGNORE (type
, i
))
404 fputs_filtered ("<optimized out or zero length>", stream
);
406 else if (field_is_static (&TYPE_FIELD (type
, i
)))
408 struct value
*v
= value_static_field (type
, i
);
410 fputs_filtered ("<optimized out>", stream
);
413 struct value_print_options opts
;
414 struct type
*t
= check_typedef (value_type (v
));
415 if (TYPE_CODE (t
) == TYPE_CODE_STRUCT
)
419 common_val_print (v
, stream
, recurse
+ 1,
420 &opts
, current_language
);
423 else if (TYPE_FIELD_TYPE (type
, i
) == NULL
)
424 fputs_filtered ("<unknown type>", stream
);
427 struct value_print_options opts
= *options
;
429 val_print (TYPE_FIELD_TYPE (type
, i
),
430 valaddr
+ TYPE_FIELD_BITPOS (type
, i
) / 8, 0,
431 address
+ TYPE_FIELD_BITPOS (type
, i
) / 8,
432 stream
, recurse
+ 1, &opts
,
436 annotate_field_end ();
441 fprintf_filtered (stream
, "\n");
442 print_spaces_filtered (2 * recurse
, stream
);
445 fprintf_filtered (stream
, "}");
448 /* Print data of type TYPE located at VALADDR (within GDB), which came from
449 the inferior at address ADDRESS, onto stdio stream STREAM according to
450 OPTIONS. The data at VALADDR is in target byte order.
452 If the data are a string pointer, returns the number of string characters
456 java_val_print (struct type
*type
, const gdb_byte
*valaddr
,
457 int embedded_offset
, CORE_ADDR address
,
458 struct ui_file
*stream
, int recurse
,
459 const struct value_print_options
*options
)
461 unsigned int i
= 0; /* Number of characters printed */
462 struct type
*target_type
;
465 CHECK_TYPEDEF (type
);
466 switch (TYPE_CODE (type
))
469 if (options
->format
&& options
->format
!= 's')
471 print_scalar_formatted (valaddr
, type
, options
, 0, stream
);
475 if (options
->vtblprint
&& cp_is_vtbl_ptr_type (type
))
477 /* Print the unmangled name if desired. */
478 /* Print vtable entry - we only get here if we ARE using
479 -fvtable_thunks. (Otherwise, look under TYPE_CODE_STRUCT.) */
480 /* Extract an address, assume that it is unsigned. */
481 print_address_demangle (extract_unsigned_integer (valaddr
, TYPE_LENGTH (type
)),
486 addr
= unpack_pointer (type
, valaddr
);
489 fputs_filtered ("null", stream
);
492 target_type
= check_typedef (TYPE_TARGET_TYPE (type
));
494 if (TYPE_CODE (target_type
) == TYPE_CODE_FUNC
)
496 /* Try to print what function it points to. */
497 print_address_demangle (addr
, stream
, demangle
);
498 /* Return value is irrelevant except for string pointers. */
502 if (options
->addressprint
&& options
->format
!= 's')
504 fputs_filtered ("@", stream
);
505 print_longest (stream
, 'x', 0, (ULONGEST
) addr
);
512 /* Can't just call c_val_print because that prints bytes as C
514 if (options
->format
|| options
->output_format
)
516 struct value_print_options opts
= *options
;
517 opts
.format
= (options
->format
? options
->format
518 : options
->output_format
);
519 print_scalar_formatted (valaddr
, type
, &opts
, 0, stream
);
521 else if (TYPE_CODE (type
) == TYPE_CODE_CHAR
522 || (TYPE_CODE (type
) == TYPE_CODE_INT
523 && TYPE_LENGTH (type
) == 2
524 && strcmp (TYPE_NAME (type
), "char") == 0))
525 LA_PRINT_CHAR ((int) unpack_long (type
, valaddr
), type
, stream
);
527 val_print_type_code_int (type
, valaddr
, stream
);
530 case TYPE_CODE_STRUCT
:
531 java_print_value_fields (type
, valaddr
, address
, stream
, recurse
,
536 return c_val_print (type
, valaddr
, embedded_offset
, address
, stream
,