1 /* Support for printing Java values for GDB, the GNU debugger.
2 Copyright 1997-2000 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
25 #include "expression.h"
36 static void java_print_value_fields (struct type
* type
, char *valaddr
,
38 struct ui_file
*stream
, int format
,
40 enum val_prettyprint pretty
);
44 java_value_print (val
, stream
, format
, pretty
)
46 struct ui_file
*stream
;
48 enum val_prettyprint pretty
;
55 type
= VALUE_TYPE (val
);
56 address
= VALUE_ADDRESS (val
) + VALUE_OFFSET (val
);
58 if (is_object_type (type
))
62 /* Get the run-time type, and cast the object into that */
64 obj_addr
= unpack_pointer (type
, VALUE_CONTENTS (val
));
68 type
= type_from_class (java_class_from_object (val
));
69 type
= lookup_pointer_type (type
);
71 val
= value_at (type
, address
, NULL
);
75 if (TYPE_CODE (type
) == TYPE_CODE_PTR
&& !value_logical_not (val
))
76 type_print (TYPE_TARGET_TYPE (type
), "", stream
, -1);
78 name
= TYPE_TAG_NAME (type
);
79 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
&& name
!= NULL
80 && (i
= strlen (name
), name
[i
- 1] == ']'))
84 unsigned int things_printed
= 0;
86 struct type
*el_type
= java_primitive_type_from_name (name
, i
- 2);
89 read_memory (address
+ JAVA_OBJECT_SIZE
, buf4
, 4);
91 length
= (long) extract_signed_integer (buf4
, 4);
92 fprintf_filtered (stream
, "{length: %ld", length
);
96 CORE_ADDR element
, next_element
;
98 address
+= JAVA_OBJECT_SIZE
+ 4; /* Skip object header and length. */
100 while (i
< length
&& things_printed
< print_max
)
102 char buf
[TARGET_PTR_BIT
/ HOST_CHAR_BIT
];
104 fputs_filtered (", ", stream
);
105 wrap_here (n_spaces (2));
108 element
= next_element
;
111 read_memory (address
, buf
, sizeof (buf
));
112 address
+= TARGET_PTR_BIT
/ HOST_CHAR_BIT
;
113 element
= extract_address (buf
, sizeof (buf
));
116 for (reps
= 1; i
+ reps
< length
; reps
++)
118 read_memory (address
, buf
, sizeof (buf
));
119 address
+= TARGET_PTR_BIT
/ HOST_CHAR_BIT
;
120 next_element
= extract_address (buf
, sizeof (buf
));
121 if (next_element
!= element
)
126 fprintf_filtered (stream
, "%d: ", i
);
128 fprintf_filtered (stream
, "%d..%d: ", i
, i
+ reps
- 1);
131 fprintf_filtered (stream
, "null");
133 fprintf_filtered (stream
, "@%s", paddr_nz (element
));
141 value_ptr v
= allocate_value (el_type
);
142 value_ptr next_v
= allocate_value (el_type
);
144 VALUE_ADDRESS (v
) = address
+ JAVA_OBJECT_SIZE
+ 4;
145 VALUE_ADDRESS (next_v
) = VALUE_ADDRESS (v
);
147 while (i
< length
&& things_printed
< print_max
)
149 fputs_filtered (", ", stream
);
150 wrap_here (n_spaces (2));
163 VALUE_OFFSET (v
) = 0;
166 VALUE_OFFSET (next_v
) = VALUE_OFFSET (v
);
168 for (reps
= 1; i
+ reps
< length
; reps
++)
170 VALUE_LAZY (next_v
) = 1;
171 VALUE_OFFSET (next_v
) += TYPE_LENGTH (el_type
);
172 if (memcmp (VALUE_CONTENTS (v
), VALUE_CONTENTS (next_v
),
173 TYPE_LENGTH (el_type
)) != 0)
178 fprintf_filtered (stream
, "%d: ", i
);
180 fprintf_filtered (stream
, "%d..%d: ", i
, i
+ reps
- 1);
182 val_print (VALUE_TYPE (v
), VALUE_CONTENTS (v
), 0, 0,
183 stream
, format
, 2, 1, pretty
);
191 fprintf_filtered (stream
, "...");
193 fprintf_filtered (stream
, "}");
198 /* If it's type String, print it */
200 if (TYPE_CODE (type
) == TYPE_CODE_PTR
201 && TYPE_TARGET_TYPE (type
)
202 && TYPE_NAME (TYPE_TARGET_TYPE (type
))
203 && strcmp (TYPE_NAME (TYPE_TARGET_TYPE (type
)), "java.lang.String") == 0
204 && (format
== 0 || format
== 's')
209 value_ptr boffset_val
;
210 unsigned long boffset
;
215 mark
= value_mark (); /* Remember start of new values */
217 data_val
= value_struct_elt (&val
, NULL
, "data", NULL
, NULL
);
218 data
= value_as_pointer (data_val
);
220 boffset_val
= value_struct_elt (&val
, NULL
, "boffset", NULL
, NULL
);
221 boffset
= value_as_pointer (boffset_val
);
223 count_val
= value_struct_elt (&val
, NULL
, "count", NULL
, NULL
);
224 count
= value_as_pointer (count_val
);
226 value_free_to_mark (mark
); /* Release unnecessary values */
228 val_print_string (data
+ boffset
, count
, 2, stream
);
233 return (val_print (type
, VALUE_CONTENTS (val
), 0, address
,
234 stream
, format
, 1, 0, pretty
));
237 /* TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and PRETTY have the
238 same meanings as in cp_print_value and c_val_print.
240 DONT_PRINT is an array of baseclass types that we
241 should not print, or zero if called from top level. */
244 java_print_value_fields (type
, valaddr
, address
, stream
,
245 format
, recurse
, pretty
)
249 struct ui_file
*stream
;
252 enum val_prettyprint pretty
;
254 int i
, len
, n_baseclasses
;
256 CHECK_TYPEDEF (type
);
258 fprintf_filtered (stream
, "{");
259 len
= TYPE_NFIELDS (type
);
260 n_baseclasses
= TYPE_N_BASECLASSES (type
);
262 if (n_baseclasses
> 0)
264 int i
, n_baseclasses
= TYPE_N_BASECLASSES (type
);
266 for (i
= 0; i
< n_baseclasses
; i
++)
269 struct type
*baseclass
= check_typedef (TYPE_BASECLASS (type
, i
));
270 char *basename
= TYPE_NAME (baseclass
);
273 if (BASETYPE_VIA_VIRTUAL (type
, i
))
276 if (basename
!= NULL
&& strcmp (basename
, "java.lang.Object") == 0)
283 fprintf_filtered (stream
, "\n");
284 print_spaces_filtered (2 * (recurse
+ 1), stream
);
286 fputs_filtered ("<", stream
);
287 /* Not sure what the best notation is in the case where there is no
289 fputs_filtered (basename
? basename
: "", stream
);
290 fputs_filtered ("> = ", stream
);
292 base_valaddr
= valaddr
;
294 java_print_value_fields (baseclass
, base_valaddr
, address
+ boffset
,
295 stream
, format
, recurse
+ 1, pretty
);
296 fputs_filtered (", ", stream
);
304 if (!len
&& n_baseclasses
== 1)
305 fprintf_filtered (stream
, "<No data fields>");
308 extern int inspect_it
;
311 for (i
= n_baseclasses
; i
< len
; i
++)
313 /* If requested, skip printing of static fields. */
314 if (TYPE_FIELD_STATIC (type
, i
))
316 char *name
= TYPE_FIELD_NAME (type
, i
);
317 if (!static_field_print
)
319 if (name
!= NULL
&& strcmp (name
, "class") == 0)
323 fprintf_filtered (stream
, ", ");
324 else if (n_baseclasses
> 0)
328 fprintf_filtered (stream
, "\n");
329 print_spaces_filtered (2 + 2 * recurse
, stream
);
330 fputs_filtered ("members of ", stream
);
331 fputs_filtered (type_name_no_tag (type
), stream
);
332 fputs_filtered (": ", stream
);
339 fprintf_filtered (stream
, "\n");
340 print_spaces_filtered (2 + 2 * recurse
, stream
);
344 wrap_here (n_spaces (2 + 2 * recurse
));
348 if (TYPE_CODE (TYPE_FIELD_TYPE (type
, i
)) == TYPE_CODE_PTR
)
349 fputs_filtered ("\"( ptr \"", stream
);
351 fputs_filtered ("\"( nodef \"", stream
);
352 if (TYPE_FIELD_STATIC (type
, i
))
353 fputs_filtered ("static ", stream
);
354 fprintf_symbol_filtered (stream
, TYPE_FIELD_NAME (type
, i
),
356 DMGL_PARAMS
| DMGL_ANSI
);
357 fputs_filtered ("\" \"", stream
);
358 fprintf_symbol_filtered (stream
, TYPE_FIELD_NAME (type
, i
),
360 DMGL_PARAMS
| DMGL_ANSI
);
361 fputs_filtered ("\") \"", stream
);
365 annotate_field_begin (TYPE_FIELD_TYPE (type
, i
));
367 if (TYPE_FIELD_STATIC (type
, i
))
368 fputs_filtered ("static ", stream
);
369 fprintf_symbol_filtered (stream
, TYPE_FIELD_NAME (type
, i
),
371 DMGL_PARAMS
| DMGL_ANSI
);
372 annotate_field_name_end ();
373 fputs_filtered (": ", stream
);
374 annotate_field_value ();
377 if (!TYPE_FIELD_STATIC (type
, i
) && 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 v
= value_from_longest (TYPE_FIELD_TYPE (type
, i
),
390 unpack_field_as_long (type
, valaddr
, i
));
392 val_print (TYPE_FIELD_TYPE (type
, i
), VALUE_CONTENTS (v
), 0,
393 0, stream
, format
, 0, recurse
+ 1, pretty
);
398 if (TYPE_FIELD_IGNORE (type
, i
))
400 fputs_filtered ("<optimized out or zero length>", stream
);
402 else if (TYPE_FIELD_STATIC (type
, i
))
404 value_ptr v
= value_static_field (type
, i
);
406 fputs_filtered ("<optimized out>", stream
);
409 struct type
*t
= check_typedef (VALUE_TYPE (v
));
410 if (TYPE_CODE (t
) == TYPE_CODE_STRUCT
)
412 val_print (VALUE_TYPE (v
),
413 VALUE_CONTENTS (v
), 0, VALUE_ADDRESS (v
),
414 stream
, format
, 0, recurse
+ 1, pretty
);
417 else if (TYPE_FIELD_TYPE (type
, i
) == NULL
)
418 fputs_filtered ("<unknown type>", stream
);
421 val_print (TYPE_FIELD_TYPE (type
, i
),
422 valaddr
+ TYPE_FIELD_BITPOS (type
, i
) / 8, 0,
423 address
+ TYPE_FIELD_BITPOS (type
, i
) / 8,
424 stream
, format
, 0, recurse
+ 1, pretty
);
427 annotate_field_end ();
432 fprintf_filtered (stream
, "\n");
433 print_spaces_filtered (2 * recurse
, stream
);
436 fprintf_filtered (stream
, "}");
439 /* Print data of type TYPE located at VALADDR (within GDB), which came from
440 the inferior at address ADDRESS, onto stdio stream STREAM according to
441 FORMAT (a letter or 0 for natural format). The data at VALADDR is in
444 If the data are a string pointer, returns the number of string characters
447 If DEREF_REF is nonzero, then dereference references, otherwise just print
450 The PRETTY parameter controls prettyprinting. */
453 java_val_print (type
, valaddr
, embedded_offset
, address
, stream
, format
,
454 deref_ref
, recurse
, pretty
)
459 struct ui_file
*stream
;
463 enum val_prettyprint pretty
;
465 register unsigned int i
= 0; /* Number of characters printed */
466 struct type
*target_type
;
469 CHECK_TYPEDEF (type
);
470 switch (TYPE_CODE (type
))
473 if (format
&& format
!= 's')
475 print_scalar_formatted (valaddr
, type
, format
, 0, stream
);
479 if (vtblprint
&& cp_is_vtbl_ptr_type (type
))
481 /* Print the unmangled name if desired. */
482 /* Print vtable entry - we only get here if we ARE using
483 -fvtable_thunks. (Otherwise, look under TYPE_CODE_STRUCT.) */
484 print_address_demangle (extract_address (valaddr
, TYPE_LENGTH (type
)),
489 addr
= unpack_pointer (type
, valaddr
);
492 fputs_filtered ("null", stream
);
495 target_type
= check_typedef (TYPE_TARGET_TYPE (type
));
497 if (TYPE_CODE (target_type
) == TYPE_CODE_FUNC
)
499 /* Try to print what function it points to. */
500 print_address_demangle (addr
, stream
, demangle
);
501 /* Return value is irrelevant except for string pointers. */
505 if (addressprint
&& format
!= 's')
507 fputs_filtered ("@", stream
);
508 print_longest (stream
, 'x', 0, (ULONGEST
) addr
);
514 format
= format
? format
: output_format
;
516 print_scalar_formatted (valaddr
, type
, format
, 0, stream
);
518 LA_PRINT_CHAR ((int) unpack_long (type
, valaddr
), stream
);
522 /* Can't just call c_val_print because that print bytes as C chars. */
523 format
= format
? format
: output_format
;
525 print_scalar_formatted (valaddr
, type
, format
, 0, stream
);
527 val_print_type_code_int (type
, valaddr
, stream
);
530 case TYPE_CODE_STRUCT
:
531 java_print_value_fields (type
, valaddr
, address
, stream
, format
,
536 return c_val_print (type
, valaddr
, embedded_offset
, address
, stream
,
537 format
, deref_ref
, recurse
, pretty
);