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 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
, int format
,
39 enum val_prettyprint pretty
)
46 type
= value_type (val
);
47 address
= VALUE_ADDRESS (val
) + value_offset (val
);
49 if (is_object_type (type
))
53 /* Get the run-time type, and cast the object into that */
55 obj_addr
= unpack_pointer (type
, value_contents (val
));
59 type
= type_from_class (java_class_from_object (val
));
60 type
= lookup_pointer_type (type
);
62 val
= value_at (type
, address
);
66 if (TYPE_CODE (type
) == TYPE_CODE_PTR
&& !value_logical_not (val
))
67 type_print (TYPE_TARGET_TYPE (type
), "", stream
, -1);
69 name
= TYPE_TAG_NAME (type
);
70 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
&& name
!= NULL
71 && (i
= strlen (name
), name
[i
- 1] == ']'))
75 unsigned int things_printed
= 0;
77 struct type
*el_type
= java_primitive_type_from_name (name
, i
- 2);
80 read_memory (address
+ JAVA_OBJECT_SIZE
, buf4
, 4);
82 length
= (long) extract_signed_integer (buf4
, 4);
83 fprintf_filtered (stream
, "{length: %ld", length
);
88 CORE_ADDR next_element
= -1; /* dummy initial value */
90 address
+= JAVA_OBJECT_SIZE
+ 4; /* Skip object header and length. */
92 while (i
< length
&& things_printed
< print_max
)
96 buf
= alloca (gdbarch_ptr_bit (current_gdbarch
) / HOST_CHAR_BIT
);
97 fputs_filtered (", ", stream
);
98 wrap_here (n_spaces (2));
101 element
= next_element
;
104 read_memory (address
, buf
, sizeof (buf
));
105 address
+= gdbarch_ptr_bit (current_gdbarch
) / HOST_CHAR_BIT
;
106 /* FIXME: cagney/2003-05-24: Bogus or what. It
107 pulls a host sized pointer out of the target and
108 then extracts that as an address (while assuming
109 that the address is unsigned)! */
110 element
= extract_unsigned_integer (buf
, sizeof (buf
));
113 for (reps
= 1; i
+ reps
< length
; reps
++)
115 read_memory (address
, buf
, sizeof (buf
));
116 address
+= gdbarch_ptr_bit (current_gdbarch
) / HOST_CHAR_BIT
;
117 /* FIXME: cagney/2003-05-24: Bogus or what. It
118 pulls a host sized pointer out of the target and
119 then extracts that as an address (while assuming
120 that the address is unsigned)! */
121 next_element
= extract_unsigned_integer (buf
, sizeof (buf
));
122 if (next_element
!= element
)
127 fprintf_filtered (stream
, "%d: ", i
);
129 fprintf_filtered (stream
, "%d..%d: ", i
, i
+ reps
- 1);
132 fprintf_filtered (stream
, "null");
134 fprintf_filtered (stream
, "@%s", paddr_nz (element
));
142 struct value
*v
= allocate_value (el_type
);
143 struct value
*next_v
= allocate_value (el_type
);
145 VALUE_ADDRESS (v
) = address
+ JAVA_OBJECT_SIZE
+ 4;
146 VALUE_ADDRESS (next_v
) = VALUE_ADDRESS (v
);
148 while (i
< length
&& things_printed
< print_max
)
150 fputs_filtered (", ", stream
);
151 wrap_here (n_spaces (2));
163 set_value_lazy (v
, 1);
164 set_value_offset (v
, 0);
167 set_value_offset (next_v
, value_offset (v
));
169 for (reps
= 1; i
+ reps
< length
; reps
++)
171 set_value_lazy (next_v
, 1);
172 set_value_offset (next_v
, value_offset (next_v
) + TYPE_LENGTH (el_type
));
173 if (memcmp (value_contents (v
), value_contents (next_v
),
174 TYPE_LENGTH (el_type
)) != 0)
179 fprintf_filtered (stream
, "%d: ", i
);
181 fprintf_filtered (stream
, "%d..%d: ", i
, i
+ reps
- 1);
183 common_val_print (v
, stream
, format
, 2, 1, pretty
,
192 fprintf_filtered (stream
, "...");
194 fprintf_filtered (stream
, "}");
199 /* If it's type String, print it */
201 if (TYPE_CODE (type
) == TYPE_CODE_PTR
202 && TYPE_TARGET_TYPE (type
)
203 && TYPE_TAG_NAME (TYPE_TARGET_TYPE (type
))
204 && strcmp (TYPE_TAG_NAME (TYPE_TARGET_TYPE (type
)),
205 "java.lang.String") == 0
206 && (format
== 0 || format
== 's')
208 && value_as_address (val
) != 0)
210 struct value
*data_val
;
212 struct value
*boffset_val
;
213 unsigned long boffset
;
214 struct value
*count_val
;
218 mark
= value_mark (); /* Remember start of new values */
220 data_val
= value_struct_elt (&val
, NULL
, "data", NULL
, NULL
);
221 data
= value_as_address (data_val
);
223 boffset_val
= value_struct_elt (&val
, NULL
, "boffset", NULL
, NULL
);
224 boffset
= value_as_address (boffset_val
);
226 count_val
= value_struct_elt (&val
, NULL
, "count", NULL
, NULL
);
227 count
= value_as_address (count_val
);
229 value_free_to_mark (mark
); /* Release unnecessary values */
231 val_print_string (data
+ boffset
, count
, 2, stream
);
236 return common_val_print (val
, stream
, format
, 1, 0, pretty
,
240 /* TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and PRETTY have the
241 same meanings as in cp_print_value and c_val_print.
243 DONT_PRINT is an array of baseclass types that we
244 should not print, or zero if called from top level. */
247 java_print_value_fields (struct type
*type
, const gdb_byte
*valaddr
,
248 CORE_ADDR address
, struct ui_file
*stream
,
249 int format
, int recurse
, enum val_prettyprint pretty
)
251 int i
, len
, n_baseclasses
;
253 CHECK_TYPEDEF (type
);
255 fprintf_filtered (stream
, "{");
256 len
= TYPE_NFIELDS (type
);
257 n_baseclasses
= TYPE_N_BASECLASSES (type
);
259 if (n_baseclasses
> 0)
261 int i
, n_baseclasses
= TYPE_N_BASECLASSES (type
);
263 for (i
= 0; i
< n_baseclasses
; i
++)
266 struct type
*baseclass
= check_typedef (TYPE_BASECLASS (type
, i
));
267 char *basename
= TYPE_NAME (baseclass
);
268 const gdb_byte
*base_valaddr
;
270 if (BASETYPE_VIA_VIRTUAL (type
, i
))
273 if (basename
!= NULL
&& strcmp (basename
, "java.lang.Object") == 0)
280 fprintf_filtered (stream
, "\n");
281 print_spaces_filtered (2 * (recurse
+ 1), stream
);
283 fputs_filtered ("<", stream
);
284 /* Not sure what the best notation is in the case where there is no
286 fputs_filtered (basename
? basename
: "", stream
);
287 fputs_filtered ("> = ", stream
);
289 base_valaddr
= valaddr
;
291 java_print_value_fields (baseclass
, base_valaddr
, address
+ boffset
,
292 stream
, format
, recurse
+ 1, pretty
);
293 fputs_filtered (", ", stream
);
298 if (!len
&& n_baseclasses
== 1)
299 fprintf_filtered (stream
, "<No data fields>");
304 for (i
= n_baseclasses
; i
< len
; i
++)
306 /* If requested, skip printing of static fields. */
307 if (TYPE_FIELD_STATIC (type
, i
))
309 char *name
= TYPE_FIELD_NAME (type
, i
);
310 if (!static_field_print
)
312 if (name
!= NULL
&& strcmp (name
, "class") == 0)
316 fprintf_filtered (stream
, ", ");
317 else if (n_baseclasses
> 0)
321 fprintf_filtered (stream
, "\n");
322 print_spaces_filtered (2 + 2 * recurse
, stream
);
323 fputs_filtered ("members of ", stream
);
324 fputs_filtered (type_name_no_tag (type
), stream
);
325 fputs_filtered (": ", stream
);
332 fprintf_filtered (stream
, "\n");
333 print_spaces_filtered (2 + 2 * recurse
, stream
);
337 wrap_here (n_spaces (2 + 2 * recurse
));
341 if (TYPE_CODE (TYPE_FIELD_TYPE (type
, i
)) == TYPE_CODE_PTR
)
342 fputs_filtered ("\"( ptr \"", stream
);
344 fputs_filtered ("\"( nodef \"", stream
);
345 if (TYPE_FIELD_STATIC (type
, i
))
346 fputs_filtered ("static ", stream
);
347 fprintf_symbol_filtered (stream
, TYPE_FIELD_NAME (type
, i
),
349 DMGL_PARAMS
| DMGL_ANSI
);
350 fputs_filtered ("\" \"", stream
);
351 fprintf_symbol_filtered (stream
, TYPE_FIELD_NAME (type
, i
),
353 DMGL_PARAMS
| DMGL_ANSI
);
354 fputs_filtered ("\") \"", stream
);
358 annotate_field_begin (TYPE_FIELD_TYPE (type
, i
));
360 if (TYPE_FIELD_STATIC (type
, i
))
361 fputs_filtered ("static ", stream
);
362 fprintf_symbol_filtered (stream
, TYPE_FIELD_NAME (type
, i
),
364 DMGL_PARAMS
| DMGL_ANSI
);
365 annotate_field_name_end ();
366 fputs_filtered (": ", stream
);
367 annotate_field_value ();
370 if (!TYPE_FIELD_STATIC (type
, i
) && TYPE_FIELD_PACKED (type
, i
))
374 /* Bitfields require special handling, especially due to byte
376 if (TYPE_FIELD_IGNORE (type
, i
))
378 fputs_filtered ("<optimized out or zero length>", stream
);
382 v
= value_from_longest (TYPE_FIELD_TYPE (type
, i
),
383 unpack_field_as_long (type
, valaddr
, i
));
385 common_val_print (v
, stream
, format
, 0, recurse
+ 1,
386 pretty
, current_language
);
391 if (TYPE_FIELD_IGNORE (type
, i
))
393 fputs_filtered ("<optimized out or zero length>", stream
);
395 else if (TYPE_FIELD_STATIC (type
, i
))
397 struct value
*v
= value_static_field (type
, i
);
399 fputs_filtered ("<optimized out>", stream
);
402 struct type
*t
= check_typedef (value_type (v
));
403 if (TYPE_CODE (t
) == TYPE_CODE_STRUCT
)
405 common_val_print (v
, stream
, format
, 0, recurse
+ 1,
406 pretty
, current_language
);
409 else if (TYPE_FIELD_TYPE (type
, i
) == NULL
)
410 fputs_filtered ("<unknown type>", stream
);
413 val_print (TYPE_FIELD_TYPE (type
, i
),
414 valaddr
+ TYPE_FIELD_BITPOS (type
, i
) / 8, 0,
415 address
+ TYPE_FIELD_BITPOS (type
, i
) / 8,
416 stream
, format
, 0, recurse
+ 1, pretty
,
420 annotate_field_end ();
425 fprintf_filtered (stream
, "\n");
426 print_spaces_filtered (2 * recurse
, stream
);
429 fprintf_filtered (stream
, "}");
432 /* Print data of type TYPE located at VALADDR (within GDB), which came from
433 the inferior at address ADDRESS, onto stdio stream STREAM according to
434 FORMAT (a letter or 0 for natural format). The data at VALADDR is in
437 If the data are a string pointer, returns the number of string characters
440 If DEREF_REF is nonzero, then dereference references, otherwise just print
443 The PRETTY parameter controls prettyprinting. */
446 java_val_print (struct type
*type
, const gdb_byte
*valaddr
,
447 int embedded_offset
, CORE_ADDR address
,
448 struct ui_file
*stream
, int format
, int deref_ref
,
449 int recurse
, enum val_prettyprint pretty
)
451 unsigned int i
= 0; /* Number of characters printed */
452 struct type
*target_type
;
455 CHECK_TYPEDEF (type
);
456 switch (TYPE_CODE (type
))
459 if (format
&& format
!= 's')
461 print_scalar_formatted (valaddr
, type
, format
, 0, stream
);
465 if (vtblprint
&& cp_is_vtbl_ptr_type (type
))
467 /* Print the unmangled name if desired. */
468 /* Print vtable entry - we only get here if we ARE using
469 -fvtable_thunks. (Otherwise, look under TYPE_CODE_STRUCT.) */
470 /* Extract an address, assume that it is unsigned. */
471 print_address_demangle (extract_unsigned_integer (valaddr
, TYPE_LENGTH (type
)),
476 addr
= unpack_pointer (type
, valaddr
);
479 fputs_filtered ("null", stream
);
482 target_type
= check_typedef (TYPE_TARGET_TYPE (type
));
484 if (TYPE_CODE (target_type
) == TYPE_CODE_FUNC
)
486 /* Try to print what function it points to. */
487 print_address_demangle (addr
, stream
, demangle
);
488 /* Return value is irrelevant except for string pointers. */
492 if (addressprint
&& format
!= 's')
494 fputs_filtered ("@", stream
);
495 print_longest (stream
, 'x', 0, (ULONGEST
) addr
);
502 /* Can't just call c_val_print because that prints bytes as C
504 format
= format
? format
: output_format
;
506 print_scalar_formatted (valaddr
, type
, format
, 0, stream
);
507 else if (TYPE_CODE (type
) == TYPE_CODE_CHAR
508 || (TYPE_CODE (type
) == TYPE_CODE_INT
509 && TYPE_LENGTH (type
) == 2
510 && strcmp (TYPE_NAME (type
), "char") == 0))
511 LA_PRINT_CHAR ((int) unpack_long (type
, valaddr
), stream
);
513 val_print_type_code_int (type
, valaddr
, stream
);
516 case TYPE_CODE_STRUCT
:
517 java_print_value_fields (type
, valaddr
, address
, stream
, format
,
522 return c_val_print (type
, valaddr
, embedded_offset
, address
, stream
,
523 format
, deref_ref
, recurse
, pretty
);
This page took 0.041216 seconds and 4 git commands to generate.