* Fixing typo that caused infinite loop upon PKE MPG.
[binutils-gdb.git] / gdb / jv-valprint.c
blobd46c4b89b161c99453ef3da8612ba3a51b35bf34
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. */
20 #include "defs.h"
21 #include "symtab.h"
22 #include "gdbtypes.h"
23 #include "expression.h"
24 #include "value.h"
25 #include "demangle.h"
26 #include "valprint.h"
27 #include "language.h"
28 #include "jv-lang.h"
29 #include "c-lang.h"
31 int
32 java_value_print (val, stream, format, pretty)
33 value_ptr val;
34 GDB_FILE *stream;
35 int format;
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));
43 if (obj_addr != 0)
45 value_ptr obj_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] == '[')
59 char buf4[4];
60 long length;
61 unsigned int things_printed = 0;
62 int i = 0;
63 int reps;
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));
77 if (i > 0)
78 element = next_element;
79 else
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)
91 break;
93 if (reps == 1)
94 fprintf_filtered (stream, "%d: ", i);
95 else
96 fprintf_filtered (stream, "%d..%d: ", i, i + reps - 1);
97 if (element == 0)
98 fprintf_filtered (stream, "null");
99 else
100 fprintf_filtered (stream, "@%x", element);
101 things_printed++;
102 i += reps;
105 else
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));
117 if (i > 0)
119 value_ptr tmp = next_v; next_v = v; v = tmp;
121 else
123 VALUE_LAZY (v) = 1;
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)
133 break;
135 if (reps == 1)
136 fprintf_filtered (stream, "%d: ", i);
137 else
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);
141 things_printed++;
142 i += reps;
146 if (i < length)
147 fprintf_filtered (stream, "...");
148 fprintf_filtered (stream, "}");
149 return 0;
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. */
162 void
163 java_print_value_fields (type, valaddr, address, stream,
164 format, recurse, pretty)
165 struct type *type;
166 char *valaddr;
167 CORE_ADDR address;
168 GDB_FILE *stream;
169 int format;
170 int recurse;
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++)
187 int boffset;
188 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
189 char *basename = TYPE_NAME (baseclass);
190 char *base_valaddr;
192 if (BASETYPE_VIA_VIRTUAL (type, i))
193 continue;
195 if (basename != NULL && strcmp (basename, "java.lang.Object") == 0)
196 continue;
198 boffset = 0;
200 if (pretty)
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
207 baseclass name. */
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);
217 flush_it:
223 if (!len && n_baseclasses == 1)
224 fprintf_filtered (stream, "<No data fields>");
225 else
227 extern int inspect_it;
228 int fields_seen = 0;
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)
237 continue;
238 if (name != NULL && strcmp (name, "class") == 0)
239 continue;
241 if (fields_seen)
242 fprintf_filtered (stream, ", ");
243 else if (n_baseclasses > 0)
245 if (pretty)
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);
254 fields_seen = 1;
256 if (pretty)
258 fprintf_filtered (stream, "\n");
259 print_spaces_filtered (2 + 2 * recurse, stream);
261 else
263 wrap_here (n_spaces (2 + 2 * recurse));
265 if (inspect_it)
267 if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
268 fputs_filtered ("\"( ptr \"", stream);
269 else
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),
274 language_cplus,
275 DMGL_PARAMS | DMGL_ANSI);
276 fputs_filtered ("\" \"", stream);
277 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
278 language_cplus,
279 DMGL_PARAMS | DMGL_ANSI);
280 fputs_filtered ("\") \"", stream);
282 else
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),
289 language_cplus,
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))
298 value_ptr v;
300 /* Bitfields require special handling, especially due to byte
301 order problems. */
302 if (TYPE_FIELD_IGNORE (type, i))
304 fputs_filtered ("<optimized out or zero length>", stream);
306 else
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);
315 else
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);
324 if (v == NULL)
325 fputs_filtered ("<optimized out>", stream);
326 else
328 struct type *t = check_typedef (VALUE_TYPE (v));
329 if (TYPE_CODE (t) == TYPE_CODE_STRUCT)
330 v = value_addr (v);
331 val_print (VALUE_TYPE (v),
332 VALUE_CONTENTS (v), VALUE_ADDRESS (v),
333 stream, format, 0, recurse+1, pretty);
336 else
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 ();
347 if (pretty)
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,
358 pretty)
359 struct type *type;
360 char *valaddr;
361 CORE_ADDR address;
362 GDB_FILE *stream;
363 int format;
364 int deref_ref;
365 int recurse;
366 enum val_prettyprint pretty;
368 register unsigned int i = 0; /* Number of characters printed */
369 struct type *elttype;
370 CORE_ADDR addr;
372 CHECK_TYPEDEF (type);
373 switch (TYPE_CODE (type))
375 case TYPE_CODE_PTR:
376 if (format && format != 's')
378 print_scalar_formatted (valaddr, type, format, 0, stream);
379 break;
381 #if 0
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)),
388 stream, demangle);
389 break;
391 #endif
392 addr = unpack_pointer (type, valaddr);
393 if (addr == 0)
395 fputs_filtered ("null", stream);
396 return i;
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. */
408 return (0);
411 if (addressprint && format != 's')
413 fputs_filtered ("@", stream);
414 print_longest (stream, 'x', 0, (ULONGEST) addr);
416 return i;
418 case TYPE_CODE_CHAR:
419 format = format ? format : output_format;
420 if (format)
422 print_scalar_formatted (valaddr, type, format, 0, stream);
424 else
426 LA_PRINT_CHAR ((int) unpack_long (type, valaddr), stream);
428 break;
429 case TYPE_CODE_STRUCT:
430 java_print_value_fields (type, valaddr, address, stream, format,
431 recurse, pretty);
432 break;
433 default:
434 return c_val_print (type, valaddr, address, stream, format,
435 deref_ref, recurse, pretty);