1 /* Support for printing Java values for GDB, the GNU debugger.
2 Copyright 1997 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, Boston, MA 02111-1307, USA. */
23 #include "expression.h"
32 java_value_print (val
, stream
, format
, pretty
)
36 enum val_prettyprint pretty
;
38 struct type
*type
= VALUE_TYPE (val
);
39 CORE_ADDR address
= VALUE_ADDRESS (val
) + VALUE_OFFSET (val
);
40 if (is_object_type (type
))
42 CORE_ADDR obj_addr
= unpack_pointer (type
, VALUE_CONTENTS (val
));
46 = value_at (TYPE_TARGET_TYPE (type
), obj_addr
, NULL
);
47 type
= type_from_class (java_class_from_object (obj_val
));
48 type
= lookup_pointer_type (type
);
51 if (TYPE_CODE (type
) == TYPE_CODE_PTR
&& ! value_logical_not (val
))
53 type_print (TYPE_TARGET_TYPE (type
), "", stream
, -1);
56 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
&& TYPE_TAG_NAME (type
) != NULL
57 && TYPE_TAG_NAME (type
)[0] == '[')
61 unsigned int things_printed
= 0;
64 read_memory (address
+ JAVA_OBJECT_SIZE
, buf4
, 4);
65 length
= (long) extract_signed_integer (buf4
, 4);
66 fprintf_filtered (stream
, "{length: %ld", length
);
67 if (TYPE_TAG_NAME (type
)[1] == 'L'
68 || TYPE_TAG_NAME (type
)[1] == '[')
70 CORE_ADDR element
, next_element
;
71 address
+= JAVA_OBJECT_SIZE
+ 4; /* Skip object header and length. */
72 while (i
< length
&& things_printed
< print_max
)
74 char buf
[TARGET_PTR_BIT
/ HOST_CHAR_BIT
];
75 fputs_filtered (", ", stream
);
76 wrap_here (n_spaces (2));
78 element
= next_element
;
81 read_memory (address
, buf
, sizeof(buf
));
82 address
+= TARGET_PTR_BIT
/ HOST_CHAR_BIT
;
83 element
= extract_address (buf
, sizeof(buf
));
85 for (reps
= 1; i
+ reps
< length
; reps
++)
87 read_memory (address
, buf
, sizeof(buf
));
88 address
+= TARGET_PTR_BIT
/ HOST_CHAR_BIT
;
89 next_element
= extract_address (buf
, sizeof(buf
));
90 if (next_element
!= element
)
94 fprintf_filtered (stream
, "%d: ", i
);
96 fprintf_filtered (stream
, "%d..%d: ", i
, i
+ reps
- 1);
98 fprintf_filtered (stream
, "null");
100 fprintf_filtered (stream
, "@%x", element
);
107 struct type
*el_type
= java_primitive_type (TYPE_TAG_NAME (type
)[1]);
108 value_ptr v
= allocate_value (el_type
);
109 value_ptr next_v
= allocate_value (el_type
);
110 VALUE_ADDRESS (v
) = address
+ JAVA_OBJECT_SIZE
+ 4;
111 VALUE_ADDRESS (next_v
) = VALUE_ADDRESS (v
);
113 while (i
< length
&& things_printed
< print_max
)
115 fputs_filtered (", ", stream
);
116 wrap_here (n_spaces (2));
119 value_ptr tmp
= next_v
; next_v
= v
; v
= tmp
;
124 VALUE_OFFSET (v
) = 0;
126 VALUE_OFFSET (next_v
) = VALUE_OFFSET (v
);
127 for (reps
= 1; i
+ reps
< length
; reps
++)
129 VALUE_LAZY (next_v
) = 1;
130 VALUE_OFFSET (next_v
) += TYPE_LENGTH (el_type
);
131 if (memcmp (VALUE_CONTENTS (v
), VALUE_CONTENTS (next_v
),
132 TYPE_LENGTH (el_type
)) != 0)
136 fprintf_filtered (stream
, "%d: ", i
);
138 fprintf_filtered (stream
, "%d..%d: ", i
, i
+ reps
- 1);
139 val_print (VALUE_TYPE (v
), VALUE_CONTENTS (v
), 0,
140 stream
, format
, 2, 1, pretty
);
147 fprintf_filtered (stream
, "...");
148 fprintf_filtered (stream
, "}");
152 return (val_print (type
, VALUE_CONTENTS (val
), address
,
153 stream
, format
, 1, 0, pretty
));
156 /* TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and PRETTY have the
157 same meanings as in cp_print_value and c_val_print.
159 DONT_PRINT is an array of baseclass types that we
160 should not print, or zero if called from top level. */
163 java_print_value_fields (type
, valaddr
, address
, stream
,
164 format
, recurse
, pretty
)
171 enum val_prettyprint pretty
;
173 int i
, len
, n_baseclasses
;
175 CHECK_TYPEDEF (type
);
177 fprintf_filtered (stream
, "{");
178 len
= TYPE_NFIELDS (type
);
179 n_baseclasses
= TYPE_N_BASECLASSES (type
);
181 if (n_baseclasses
> 0)
183 int i
, n_baseclasses
= TYPE_N_BASECLASSES (type
);
185 for (i
= 0; i
< n_baseclasses
; i
++)
188 struct type
*baseclass
= check_typedef (TYPE_BASECLASS (type
, i
));
189 char *basename
= TYPE_NAME (baseclass
);
192 if (BASETYPE_VIA_VIRTUAL (type
, i
))
195 if (basename
!= NULL
&& strcmp (basename
, "java.lang.Object") == 0)
202 fprintf_filtered (stream
, "\n");
203 print_spaces_filtered (2 * (recurse
+1), stream
);
205 fputs_filtered ("<", stream
);
206 /* Not sure what the best notation is in the case where there is no
208 fputs_filtered (basename
? basename
: "", stream
);
209 fputs_filtered ("> = ", stream
);
211 base_valaddr
= valaddr
;
213 java_print_value_fields (baseclass
, base_valaddr
, address
+ boffset
,
214 stream
, format
, recurse
+1, pretty
);
215 fputs_filtered (", ", stream
);
223 if (!len
&& n_baseclasses
== 1)
224 fprintf_filtered (stream
, "<No data fields>");
227 extern int inspect_it
;
230 for (i
= n_baseclasses
; i
< len
; i
++)
232 /* If requested, skip printing of static fields. */
233 if (TYPE_FIELD_STATIC (type
, i
))
235 char *name
= TYPE_FIELD_NAME (type
, i
);
236 if (!static_field_print
)
238 if (name
!= NULL
&& strcmp (name
, "class") == 0)
242 fprintf_filtered (stream
, ", ");
243 else if (n_baseclasses
> 0)
247 fprintf_filtered (stream
, "\n");
248 print_spaces_filtered (2 + 2 * recurse
, stream
);
249 fputs_filtered ("members of ", stream
);
250 fputs_filtered (type_name_no_tag (type
), stream
);
251 fputs_filtered (": ", stream
);
258 fprintf_filtered (stream
, "\n");
259 print_spaces_filtered (2 + 2 * recurse
, stream
);
263 wrap_here (n_spaces (2 + 2 * recurse
));
267 if (TYPE_CODE (TYPE_FIELD_TYPE (type
, i
)) == TYPE_CODE_PTR
)
268 fputs_filtered ("\"( ptr \"", stream
);
270 fputs_filtered ("\"( nodef \"", stream
);
271 if (TYPE_FIELD_STATIC (type
, i
))
272 fputs_filtered ("static ", stream
);
273 fprintf_symbol_filtered (stream
, TYPE_FIELD_NAME (type
, i
),
275 DMGL_PARAMS
| DMGL_ANSI
);
276 fputs_filtered ("\" \"", stream
);
277 fprintf_symbol_filtered (stream
, TYPE_FIELD_NAME (type
, i
),
279 DMGL_PARAMS
| DMGL_ANSI
);
280 fputs_filtered ("\") \"", stream
);
284 annotate_field_begin (TYPE_FIELD_TYPE (type
, i
));
286 if (TYPE_FIELD_STATIC (type
, i
))
287 fputs_filtered ("static ", stream
);
288 fprintf_symbol_filtered (stream
, TYPE_FIELD_NAME (type
, i
),
290 DMGL_PARAMS
| DMGL_ANSI
);
291 annotate_field_name_end ();
292 fputs_filtered (": ", stream
);
293 annotate_field_value ();
296 if (!TYPE_FIELD_STATIC (type
, i
) && TYPE_FIELD_PACKED (type
, i
))
300 /* Bitfields require special handling, especially due to byte
302 if (TYPE_FIELD_IGNORE (type
, i
))
304 fputs_filtered ("<optimized out or zero length>", stream
);
308 v
= value_from_longest (TYPE_FIELD_TYPE (type
, i
),
309 unpack_field_as_long (type
, valaddr
, i
));
311 val_print (TYPE_FIELD_TYPE(type
, i
), VALUE_CONTENTS (v
), 0,
312 stream
, format
, 0, recurse
+ 1, pretty
);
317 if (TYPE_FIELD_IGNORE (type
, i
))
319 fputs_filtered ("<optimized out or zero length>", stream
);
321 else if (TYPE_FIELD_STATIC (type
, i
))
323 value_ptr v
= value_static_field (type
, i
);
325 fputs_filtered ("<optimized out>", stream
);
328 struct type
*t
= check_typedef (VALUE_TYPE (v
));
329 if (TYPE_CODE (t
) == TYPE_CODE_STRUCT
)
331 val_print (VALUE_TYPE (v
),
332 VALUE_CONTENTS (v
), VALUE_ADDRESS (v
),
333 stream
, format
, 0, recurse
+1, pretty
);
338 val_print (TYPE_FIELD_TYPE (type
, i
),
339 valaddr
+ TYPE_FIELD_BITPOS (type
, i
) / 8,
340 address
+ TYPE_FIELD_BITPOS (type
, i
) / 8,
341 stream
, format
, 0, recurse
+ 1, pretty
);
344 annotate_field_end ();
349 fprintf_filtered (stream
, "\n");
350 print_spaces_filtered (2 * recurse
, stream
);
353 fprintf_filtered (stream
, "}");
357 java_val_print (type
, valaddr
, address
, stream
, format
, deref_ref
, recurse
,
366 enum val_prettyprint pretty
;
368 register unsigned int i
= 0; /* Number of characters printed */
369 struct type
*elttype
;
372 CHECK_TYPEDEF (type
);
373 switch (TYPE_CODE (type
))
376 if (format
&& format
!= 's')
378 print_scalar_formatted (valaddr
, type
, format
, 0, stream
);
382 if (vtblprint
&& cp_is_vtbl_ptr_type(type
))
384 /* Print the unmangled name if desired. */
385 /* Print vtable entry - we only get here if we ARE using
386 -fvtable_thunks. (Otherwise, look under TYPE_CODE_STRUCT.) */
387 print_address_demangle(extract_address (valaddr
, TYPE_LENGTH (type
)),
392 addr
= unpack_pointer (type
, valaddr
);
395 fputs_filtered ("null", stream
);
398 elttype
= check_typedef (TYPE_TARGET_TYPE (type
));
400 print_unpacked_pointer
:
401 elttype
= check_typedef (TYPE_TARGET_TYPE (type
));
403 if (TYPE_CODE (elttype
) == TYPE_CODE_FUNC
)
405 /* Try to print what function it points to. */
406 print_address_demangle (addr
, stream
, demangle
);
407 /* Return value is irrelevant except for string pointers. */
411 if (addressprint
&& format
!= 's')
413 fputs_filtered ("@", stream
);
414 print_longest (stream
, 'x', 0, (ULONGEST
) addr
);
419 format
= format
? format
: output_format
;
422 print_scalar_formatted (valaddr
, type
, format
, 0, stream
);
426 LA_PRINT_CHAR ((int) unpack_long (type
, valaddr
), stream
);
429 case TYPE_CODE_STRUCT
:
430 java_print_value_fields (type
, valaddr
, address
, stream
, format
,
434 return c_val_print (type
, valaddr
, address
, stream
, format
,
435 deref_ref
, recurse
, pretty
);