]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/jv-valprint.c
2002-05-09 Michael Snyder <msnyder@redhat.com>
[thirdparty/binutils-gdb.git] / gdb / jv-valprint.c
CommitLineData
c906108c 1/* Support for printing Java values for GDB, the GNU debugger.
b6ba6518 2 Copyright 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
c906108c 3
c5aa993b 4 This file is part of GDB.
c906108c 5
c5aa993b
JM
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.
c906108c 10
c5aa993b
JM
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.
c906108c 15
c5aa993b
JM
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. */
c906108c
SS
20
21#include "defs.h"
22#include "symtab.h"
23#include "gdbtypes.h"
7a292a7a 24#include "gdbcore.h"
c906108c
SS
25#include "expression.h"
26#include "value.h"
27#include "demangle.h"
28#include "valprint.h"
29#include "language.h"
30#include "jv-lang.h"
31#include "c-lang.h"
7a292a7a 32#include "annotate.h"
c906108c 33
392a587b
JM
34/* Local functions */
35
d9fcf2fb
JM
36static void java_print_value_fields (struct type * type, char *valaddr,
37 CORE_ADDR address,
38 struct ui_file *stream, int format,
39 int recurse,
40 enum val_prettyprint pretty);
392a587b
JM
41
42
c906108c 43int
75c9979e 44java_value_print (struct value *val, struct ui_file *stream, int format,
fba45db2 45 enum val_prettyprint pretty)
c906108c
SS
46{
47 struct type *type;
48 CORE_ADDR address;
49 int i;
50 char *name;
51
52 type = VALUE_TYPE (val);
53 address = VALUE_ADDRESS (val) + VALUE_OFFSET (val);
54
55 if (is_object_type (type))
56 {
57 CORE_ADDR obj_addr;
58
59 /* Get the run-time type, and cast the object into that */
60
61 obj_addr = unpack_pointer (type, VALUE_CONTENTS (val));
62
63 if (obj_addr != 0)
64 {
65 type = type_from_class (java_class_from_object (val));
66 type = lookup_pointer_type (type);
67
68 val = value_at (type, address, NULL);
69 }
70 }
71
c5aa993b 72 if (TYPE_CODE (type) == TYPE_CODE_PTR && !value_logical_not (val))
c906108c
SS
73 type_print (TYPE_TARGET_TYPE (type), "", stream, -1);
74
75 name = TYPE_TAG_NAME (type);
76 if (TYPE_CODE (type) == TYPE_CODE_STRUCT && name != NULL
c5aa993b 77 && (i = strlen (name), name[i - 1] == ']'))
c906108c
SS
78 {
79 char buf4[4];
80 long length;
81 unsigned int things_printed = 0;
c5aa993b 82 int reps;
c906108c
SS
83 struct type *el_type = java_primitive_type_from_name (name, i - 2);
84
85 i = 0;
86 read_memory (address + JAVA_OBJECT_SIZE, buf4, 4);
87
88 length = (long) extract_signed_integer (buf4, 4);
89 fprintf_filtered (stream, "{length: %ld", length);
90
91 if (el_type == NULL)
92 {
c65ecaf3
AC
93 CORE_ADDR element;
94 CORE_ADDR next_element = -1; /* dummy initial value */
c906108c 95
c5aa993b 96 address += JAVA_OBJECT_SIZE + 4; /* Skip object header and length. */
c906108c
SS
97
98 while (i < length && things_printed < print_max)
99 {
35fc8285 100 char *buf;
c906108c 101
35fc8285 102 buf = alloca (TARGET_PTR_BIT / HOST_CHAR_BIT);
c906108c
SS
103 fputs_filtered (", ", stream);
104 wrap_here (n_spaces (2));
105
106 if (i > 0)
107 element = next_element;
108 else
109 {
c5aa993b 110 read_memory (address, buf, sizeof (buf));
c906108c 111 address += TARGET_PTR_BIT / HOST_CHAR_BIT;
c5aa993b 112 element = extract_address (buf, sizeof (buf));
c906108c
SS
113 }
114
c5aa993b 115 for (reps = 1; i + reps < length; reps++)
c906108c 116 {
c5aa993b 117 read_memory (address, buf, sizeof (buf));
c906108c 118 address += TARGET_PTR_BIT / HOST_CHAR_BIT;
c5aa993b 119 next_element = extract_address (buf, sizeof (buf));
c906108c
SS
120 if (next_element != element)
121 break;
122 }
123
124 if (reps == 1)
125 fprintf_filtered (stream, "%d: ", i);
126 else
127 fprintf_filtered (stream, "%d..%d: ", i, i + reps - 1);
128
129 if (element == 0)
130 fprintf_filtered (stream, "null");
131 else
d4f3574e 132 fprintf_filtered (stream, "@%s", paddr_nz (element));
c906108c
SS
133
134 things_printed++;
135 i += reps;
136 }
137 }
138 else
139 {
75c9979e
AC
140 struct value *v = allocate_value (el_type);
141 struct value *next_v = allocate_value (el_type);
c906108c
SS
142
143 VALUE_ADDRESS (v) = address + JAVA_OBJECT_SIZE + 4;
144 VALUE_ADDRESS (next_v) = VALUE_ADDRESS (v);
145
146 while (i < length && things_printed < print_max)
147 {
148 fputs_filtered (", ", stream);
149 wrap_here (n_spaces (2));
150
151 if (i > 0)
152 {
75c9979e 153 struct value *tmp;
c906108c
SS
154
155 tmp = next_v;
156 next_v = v;
157 v = tmp;
158 }
159 else
160 {
161 VALUE_LAZY (v) = 1;
162 VALUE_OFFSET (v) = 0;
163 }
164
165 VALUE_OFFSET (next_v) = VALUE_OFFSET (v);
166
c5aa993b 167 for (reps = 1; i + reps < length; reps++)
c906108c
SS
168 {
169 VALUE_LAZY (next_v) = 1;
170 VALUE_OFFSET (next_v) += TYPE_LENGTH (el_type);
171 if (memcmp (VALUE_CONTENTS (v), VALUE_CONTENTS (next_v),
172 TYPE_LENGTH (el_type)) != 0)
173 break;
174 }
175
176 if (reps == 1)
177 fprintf_filtered (stream, "%d: ", i);
178 else
179 fprintf_filtered (stream, "%d..%d: ", i, i + reps - 1);
180
181 val_print (VALUE_TYPE (v), VALUE_CONTENTS (v), 0, 0,
182 stream, format, 2, 1, pretty);
183
184 things_printed++;
185 i += reps;
186 }
187 }
188
189 if (i < length)
190 fprintf_filtered (stream, "...");
191
192 fprintf_filtered (stream, "}");
193
194 return 0;
195 }
196
197 /* If it's type String, print it */
198
199 if (TYPE_CODE (type) == TYPE_CODE_PTR
200 && TYPE_TARGET_TYPE (type)
201 && TYPE_NAME (TYPE_TARGET_TYPE (type))
8dccf761 202 && strcmp (TYPE_NAME (TYPE_TARGET_TYPE (type)), "java.lang.String") == 0
c906108c 203 && (format == 0 || format == 's')
8dccf761 204 && address != 0
1aa20aa8 205 && value_as_address (val) != 0)
c906108c 206 {
75c9979e 207 struct value *data_val;
c906108c 208 CORE_ADDR data;
75c9979e 209 struct value *boffset_val;
c906108c 210 unsigned long boffset;
75c9979e 211 struct value *count_val;
c906108c 212 unsigned long count;
75c9979e 213 struct value *mark;
c906108c
SS
214
215 mark = value_mark (); /* Remember start of new values */
216
217 data_val = value_struct_elt (&val, NULL, "data", NULL, NULL);
1aa20aa8 218 data = value_as_address (data_val);
c906108c
SS
219
220 boffset_val = value_struct_elt (&val, NULL, "boffset", NULL, NULL);
1aa20aa8 221 boffset = value_as_address (boffset_val);
c906108c
SS
222
223 count_val = value_struct_elt (&val, NULL, "count", NULL, NULL);
1aa20aa8 224 count = value_as_address (count_val);
c906108c 225
c5aa993b 226 value_free_to_mark (mark); /* Release unnecessary values */
c906108c
SS
227
228 val_print_string (data + boffset, count, 2, stream);
229
230 return 0;
231 }
232
233 return (val_print (type, VALUE_CONTENTS (val), 0, address,
234 stream, format, 1, 0, pretty));
235}
236
237/* TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and PRETTY have the
238 same meanings as in cp_print_value and c_val_print.
239
240 DONT_PRINT is an array of baseclass types that we
241 should not print, or zero if called from top level. */
242
392a587b 243static void
fba45db2
KB
244java_print_value_fields (struct type *type, char *valaddr, CORE_ADDR address,
245 struct ui_file *stream, int format, int recurse,
246 enum val_prettyprint pretty)
c906108c
SS
247{
248 int i, len, n_baseclasses;
249
250 CHECK_TYPEDEF (type);
251
252 fprintf_filtered (stream, "{");
253 len = TYPE_NFIELDS (type);
254 n_baseclasses = TYPE_N_BASECLASSES (type);
255
256 if (n_baseclasses > 0)
257 {
258 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
259
260 for (i = 0; i < n_baseclasses; i++)
261 {
262 int boffset;
263 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
264 char *basename = TYPE_NAME (baseclass);
265 char *base_valaddr;
c5aa993b 266
c906108c
SS
267 if (BASETYPE_VIA_VIRTUAL (type, i))
268 continue;
269
270 if (basename != NULL && strcmp (basename, "java.lang.Object") == 0)
271 continue;
272
273 boffset = 0;
274
275 if (pretty)
276 {
277 fprintf_filtered (stream, "\n");
c5aa993b 278 print_spaces_filtered (2 * (recurse + 1), stream);
c906108c
SS
279 }
280 fputs_filtered ("<", stream);
281 /* Not sure what the best notation is in the case where there is no
282 baseclass name. */
283 fputs_filtered (basename ? basename : "", stream);
284 fputs_filtered ("> = ", stream);
285
286 base_valaddr = valaddr;
287
288 java_print_value_fields (baseclass, base_valaddr, address + boffset,
c5aa993b 289 stream, format, recurse + 1, pretty);
c906108c 290 fputs_filtered (", ", stream);
c5aa993b 291
c906108c
SS
292 flush_it:
293 ;
294 }
295
296 }
297
298 if (!len && n_baseclasses == 1)
299 fprintf_filtered (stream, "<No data fields>");
300 else
301 {
302 extern int inspect_it;
303 int fields_seen = 0;
304
305 for (i = n_baseclasses; i < len; i++)
306 {
307 /* If requested, skip printing of static fields. */
308 if (TYPE_FIELD_STATIC (type, i))
309 {
310 char *name = TYPE_FIELD_NAME (type, i);
311 if (!static_field_print)
312 continue;
313 if (name != NULL && strcmp (name, "class") == 0)
314 continue;
315 }
316 if (fields_seen)
317 fprintf_filtered (stream, ", ");
318 else if (n_baseclasses > 0)
319 {
320 if (pretty)
321 {
322 fprintf_filtered (stream, "\n");
323 print_spaces_filtered (2 + 2 * recurse, stream);
324 fputs_filtered ("members of ", stream);
325 fputs_filtered (type_name_no_tag (type), stream);
326 fputs_filtered (": ", stream);
327 }
328 }
329 fields_seen = 1;
330
331 if (pretty)
332 {
333 fprintf_filtered (stream, "\n");
334 print_spaces_filtered (2 + 2 * recurse, stream);
335 }
c5aa993b 336 else
c906108c
SS
337 {
338 wrap_here (n_spaces (2 + 2 * recurse));
339 }
340 if (inspect_it)
341 {
342 if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
343 fputs_filtered ("\"( ptr \"", stream);
344 else
345 fputs_filtered ("\"( nodef \"", stream);
346 if (TYPE_FIELD_STATIC (type, i))
347 fputs_filtered ("static ", stream);
348 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
349 language_cplus,
350 DMGL_PARAMS | DMGL_ANSI);
351 fputs_filtered ("\" \"", stream);
352 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
353 language_cplus,
354 DMGL_PARAMS | DMGL_ANSI);
355 fputs_filtered ("\") \"", stream);
356 }
357 else
358 {
359 annotate_field_begin (TYPE_FIELD_TYPE (type, i));
360
361 if (TYPE_FIELD_STATIC (type, i))
362 fputs_filtered ("static ", stream);
363 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
364 language_cplus,
365 DMGL_PARAMS | DMGL_ANSI);
366 annotate_field_name_end ();
367 fputs_filtered (": ", stream);
368 annotate_field_value ();
369 }
370
371 if (!TYPE_FIELD_STATIC (type, i) && TYPE_FIELD_PACKED (type, i))
372 {
75c9979e 373 struct value *v;
c906108c
SS
374
375 /* Bitfields require special handling, especially due to byte
c5aa993b 376 order problems. */
c906108c
SS
377 if (TYPE_FIELD_IGNORE (type, i))
378 {
c5aa993b 379 fputs_filtered ("<optimized out or zero length>", stream);
c906108c
SS
380 }
381 else
382 {
c5aa993b 383 v = value_from_longest (TYPE_FIELD_TYPE (type, i),
c906108c
SS
384 unpack_field_as_long (type, valaddr, i));
385
c5aa993b
JM
386 val_print (TYPE_FIELD_TYPE (type, i), VALUE_CONTENTS (v), 0,
387 0, stream, format, 0, recurse + 1, pretty);
c906108c
SS
388 }
389 }
390 else
391 {
392 if (TYPE_FIELD_IGNORE (type, i))
393 {
c5aa993b 394 fputs_filtered ("<optimized out or zero length>", stream);
c906108c
SS
395 }
396 else if (TYPE_FIELD_STATIC (type, i))
397 {
75c9979e 398 struct value *v = value_static_field (type, i);
c906108c
SS
399 if (v == NULL)
400 fputs_filtered ("<optimized out>", stream);
401 else
402 {
403 struct type *t = check_typedef (VALUE_TYPE (v));
404 if (TYPE_CODE (t) == TYPE_CODE_STRUCT)
405 v = value_addr (v);
406 val_print (VALUE_TYPE (v),
407 VALUE_CONTENTS (v), 0, VALUE_ADDRESS (v),
c5aa993b 408 stream, format, 0, recurse + 1, pretty);
c906108c
SS
409 }
410 }
7a292a7a
SS
411 else if (TYPE_FIELD_TYPE (type, i) == NULL)
412 fputs_filtered ("<unknown type>", stream);
c906108c
SS
413 else
414 {
c5aa993b
JM
415 val_print (TYPE_FIELD_TYPE (type, i),
416 valaddr + TYPE_FIELD_BITPOS (type, i) / 8, 0,
417 address + TYPE_FIELD_BITPOS (type, i) / 8,
418 stream, format, 0, recurse + 1, pretty);
c906108c
SS
419 }
420 }
421 annotate_field_end ();
422 }
423
424 if (pretty)
425 {
426 fprintf_filtered (stream, "\n");
427 print_spaces_filtered (2 * recurse, stream);
428 }
429 }
430 fprintf_filtered (stream, "}");
431}
432
433/* Print data of type TYPE located at VALADDR (within GDB), which came from
434 the inferior at address ADDRESS, onto stdio stream STREAM according to
435 FORMAT (a letter or 0 for natural format). The data at VALADDR is in
436 target byte order.
437
438 If the data are a string pointer, returns the number of string characters
439 printed.
440
441 If DEREF_REF is nonzero, then dereference references, otherwise just print
442 them like pointers.
443
444 The PRETTY parameter controls prettyprinting. */
445
446int
fba45db2
KB
447java_val_print (struct type *type, char *valaddr, int embedded_offset,
448 CORE_ADDR address, struct ui_file *stream, int format,
449 int deref_ref, int recurse, enum val_prettyprint pretty)
c906108c 450{
c5aa993b 451 register unsigned int i = 0; /* Number of characters printed */
c906108c
SS
452 struct type *target_type;
453 CORE_ADDR addr;
454
455 CHECK_TYPEDEF (type);
456 switch (TYPE_CODE (type))
457 {
458 case TYPE_CODE_PTR:
459 if (format && format != 's')
460 {
461 print_scalar_formatted (valaddr, type, format, 0, stream);
462 break;
463 }
464#if 0
c5aa993b 465 if (vtblprint && cp_is_vtbl_ptr_type (type))
c906108c 466 {
c5aa993b 467 /* Print the unmangled name if desired. */
c906108c
SS
468 /* Print vtable entry - we only get here if we ARE using
469 -fvtable_thunks. (Otherwise, look under TYPE_CODE_STRUCT.) */
c5aa993b
JM
470 print_address_demangle (extract_address (valaddr, TYPE_LENGTH (type)),
471 stream, demangle);
c906108c
SS
472 break;
473 }
474#endif
475 addr = unpack_pointer (type, valaddr);
476 if (addr == 0)
477 {
478 fputs_filtered ("null", stream);
479 return i;
480 }
481 target_type = check_typedef (TYPE_TARGET_TYPE (type));
482
483 if (TYPE_CODE (target_type) == TYPE_CODE_FUNC)
484 {
485 /* Try to print what function it points to. */
486 print_address_demangle (addr, stream, demangle);
487 /* Return value is irrelevant except for string pointers. */
488 return (0);
489 }
490
491 if (addressprint && format != 's')
492 {
493 fputs_filtered ("@", stream);
494 print_longest (stream, 'x', 0, (ULONGEST) addr);
495 }
496
497 return i;
498
499 case TYPE_CODE_CHAR:
500 format = format ? format : output_format;
501 if (format)
502 print_scalar_formatted (valaddr, type, format, 0, stream);
503 else
504 LA_PRINT_CHAR ((int) unpack_long (type, valaddr), stream);
505 break;
506
507 case TYPE_CODE_INT:
508 /* Can't just call c_val_print because that print bytes as C chars. */
509 format = format ? format : output_format;
510 if (format)
511 print_scalar_formatted (valaddr, type, format, 0, stream);
512 else
513 val_print_type_code_int (type, valaddr, stream);
514 break;
515
516 case TYPE_CODE_STRUCT:
517 java_print_value_fields (type, valaddr, address, stream, format,
518 recurse, pretty);
519 break;
520
521 default:
522 return c_val_print (type, valaddr, embedded_offset, address, stream,
523 format, deref_ref, recurse, pretty);
524 }
525
526 return 0;
527}