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