Fix ada array bounds display
[deliverable/binutils-gdb.git] / gdb / jv-valprint.c
CommitLineData
c906108c 1/* Support for printing Java values for GDB, the GNU debugger.
1e4728e7 2
9b254dd1 3 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007,
7b6bb8da 4 2008, 2009, 2010, 2011 Free 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
a9762ec7 10 the Free Software Foundation; either version 3 of the License, or
c5aa993b 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 18 You should have received a copy of the GNU General Public License
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
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"
309367d4 33#include "gdb_string.h"
c906108c 34
392a587b
JM
35/* Local functions */
36
c906108c 37int
79a45b7d
TT
38java_value_print (struct value *val, struct ui_file *stream,
39 const struct value_print_options *options)
c906108c 40{
50810684 41 struct gdbarch *gdbarch = get_type_arch (value_type (val));
e17a4113 42 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
c906108c
SS
43 struct type *type;
44 CORE_ADDR address;
45 int i;
46 char *name;
79a45b7d 47 struct value_print_options opts;
c906108c 48
df407dfe 49 type = value_type (val);
42ae5230 50 address = value_address (val);
c906108c
SS
51
52 if (is_object_type (type))
53 {
54 CORE_ADDR obj_addr;
55
1777feb0 56 /* Get the run-time type, and cast the object into that. */
c906108c 57
0fd88904 58 obj_addr = unpack_pointer (type, value_contents (val));
c906108c
SS
59
60 if (obj_addr != 0)
61 {
0daa2b63 62 type = type_from_class (gdbarch, java_class_from_object (val));
c906108c
SS
63 type = lookup_pointer_type (type);
64
00a4c844 65 val = value_at (type, address);
c906108c
SS
66 }
67 }
68
c5aa993b 69 if (TYPE_CODE (type) == TYPE_CODE_PTR && !value_logical_not (val))
c906108c
SS
70 type_print (TYPE_TARGET_TYPE (type), "", stream, -1);
71
72 name = TYPE_TAG_NAME (type);
73 if (TYPE_CODE (type) == TYPE_CODE_STRUCT && name != NULL
c5aa993b 74 && (i = strlen (name), name[i - 1] == ']'))
c906108c 75 {
c68a6671 76 gdb_byte buf4[4];
c906108c
SS
77 long length;
78 unsigned int things_printed = 0;
c5aa993b 79 int reps;
0daa2b63
UW
80 struct type *el_type
81 = java_primitive_type_from_name (gdbarch, name, i - 2);
e0881a8e 82
c906108c 83 i = 0;
45d5d5ca 84 read_memory (address + get_java_object_header_size (gdbarch), buf4, 4);
c906108c 85
e17a4113 86 length = (long) extract_signed_integer (buf4, 4, byte_order);
c906108c
SS
87 fprintf_filtered (stream, "{length: %ld", length);
88
89 if (el_type == NULL)
90 {
c65ecaf3 91 CORE_ADDR element;
1777feb0 92 CORE_ADDR next_element = -1; /* Dummy initial value. */
c906108c 93
1777feb0 94 /* Skip object header and length. */
45d5d5ca 95 address += get_java_object_header_size (gdbarch) + 4;
c906108c 96
79a45b7d 97 while (i < length && things_printed < options->print_max)
c906108c 98 {
c68a6671 99 gdb_byte *buf;
c906108c 100
45d5d5ca 101 buf = alloca (gdbarch_ptr_bit (gdbarch) / HOST_CHAR_BIT);
c906108c
SS
102 fputs_filtered (", ", stream);
103 wrap_here (n_spaces (2));
104
105 if (i > 0)
106 element = next_element;
107 else
108 {
c5aa993b 109 read_memory (address, buf, sizeof (buf));
45d5d5ca 110 address += gdbarch_ptr_bit (gdbarch) / HOST_CHAR_BIT;
b276f1bb
AC
111 /* FIXME: cagney/2003-05-24: Bogus or what. It
112 pulls a host sized pointer out of the target and
113 then extracts that as an address (while assuming
114 that the address is unsigned)! */
e17a4113
UW
115 element = extract_unsigned_integer (buf, sizeof (buf),
116 byte_order);
c906108c
SS
117 }
118
c5aa993b 119 for (reps = 1; i + reps < length; reps++)
c906108c 120 {
c5aa993b 121 read_memory (address, buf, sizeof (buf));
45d5d5ca 122 address += gdbarch_ptr_bit (gdbarch) / HOST_CHAR_BIT;
b276f1bb
AC
123 /* FIXME: cagney/2003-05-24: Bogus or what. It
124 pulls a host sized pointer out of the target and
125 then extracts that as an address (while assuming
126 that the address is unsigned)! */
e17a4113
UW
127 next_element = extract_unsigned_integer (buf, sizeof (buf),
128 byte_order);
c906108c
SS
129 if (next_element != element)
130 break;
131 }
132
133 if (reps == 1)
134 fprintf_filtered (stream, "%d: ", i);
135 else
136 fprintf_filtered (stream, "%d..%d: ", i, i + reps - 1);
137
138 if (element == 0)
139 fprintf_filtered (stream, "null");
140 else
5af949e3 141 fprintf_filtered (stream, "@%s", paddress (gdbarch, element));
c906108c
SS
142
143 things_printed++;
144 i += reps;
145 }
146 }
147 else
148 {
75c9979e
AC
149 struct value *v = allocate_value (el_type);
150 struct value *next_v = allocate_value (el_type);
c906108c 151
45d5d5ca
UW
152 set_value_address (v, (address
153 + get_java_object_header_size (gdbarch) + 4));
42ae5230 154 set_value_address (next_v, value_raw_address (v));
c906108c 155
79a45b7d 156 while (i < length && things_printed < options->print_max)
c906108c
SS
157 {
158 fputs_filtered (", ", stream);
159 wrap_here (n_spaces (2));
160
161 if (i > 0)
162 {
75c9979e 163 struct value *tmp;
c906108c
SS
164
165 tmp = next_v;
166 next_v = v;
167 v = tmp;
168 }
169 else
170 {
dfa52d88 171 set_value_lazy (v, 1);
f5cf64a7 172 set_value_offset (v, 0);
c906108c
SS
173 }
174
f5cf64a7 175 set_value_offset (next_v, value_offset (v));
c906108c 176
c5aa993b 177 for (reps = 1; i + reps < length; reps++)
c906108c 178 {
dfa52d88 179 set_value_lazy (next_v, 1);
1777feb0
MS
180 set_value_offset (next_v, value_offset (next_v)
181 + TYPE_LENGTH (el_type));
c8c1c22f
PA
182 value_fetch_lazy (next_v);
183 if (!(value_available_contents_eq
184 (v, value_embedded_offset (v),
185 next_v, value_embedded_offset (next_v),
186 TYPE_LENGTH (el_type))))
c906108c
SS
187 break;
188 }
189
190 if (reps == 1)
191 fprintf_filtered (stream, "%d: ", i);
192 else
193 fprintf_filtered (stream, "%d..%d: ", i, i + reps - 1);
194
79a45b7d
TT
195 opts = *options;
196 opts.deref_ref = 1;
197 common_val_print (v, stream, 1, &opts, current_language);
c906108c
SS
198
199 things_printed++;
200 i += reps;
201 }
202 }
203
204 if (i < length)
205 fprintf_filtered (stream, "...");
206
207 fprintf_filtered (stream, "}");
208
209 return 0;
210 }
211
1777feb0 212 /* If it's type String, print it. */
c906108c
SS
213
214 if (TYPE_CODE (type) == TYPE_CODE_PTR
215 && TYPE_TARGET_TYPE (type)
d4cad8db
TT
216 && TYPE_TAG_NAME (TYPE_TARGET_TYPE (type))
217 && strcmp (TYPE_TAG_NAME (TYPE_TARGET_TYPE (type)),
218 "java.lang.String") == 0
79a45b7d 219 && (options->format == 0 || options->format == 's')
8dccf761 220 && address != 0
1aa20aa8 221 && value_as_address (val) != 0)
c906108c 222 {
0daa2b63 223 struct type *char_type;
75c9979e 224 struct value *data_val;
c906108c 225 CORE_ADDR data;
75c9979e 226 struct value *boffset_val;
c906108c 227 unsigned long boffset;
75c9979e 228 struct value *count_val;
c906108c 229 unsigned long count;
75c9979e 230 struct value *mark;
c906108c 231
1777feb0 232 mark = value_mark (); /* Remember start of new values. */
c906108c
SS
233
234 data_val = value_struct_elt (&val, NULL, "data", NULL, NULL);
1aa20aa8 235 data = value_as_address (data_val);
c906108c
SS
236
237 boffset_val = value_struct_elt (&val, NULL, "boffset", NULL, NULL);
1aa20aa8 238 boffset = value_as_address (boffset_val);
c906108c
SS
239
240 count_val = value_struct_elt (&val, NULL, "count", NULL, NULL);
1aa20aa8 241 count = value_as_address (count_val);
c906108c 242
1777feb0 243 value_free_to_mark (mark); /* Release unnecessary values. */
c906108c 244
0daa2b63 245 char_type = builtin_java_type (gdbarch)->builtin_char;
09ca9e2e
TT
246 val_print_string (char_type, NULL, data + boffset, count, stream,
247 options);
c906108c
SS
248
249 return 0;
250 }
251
79a45b7d
TT
252 opts = *options;
253 opts.deref_ref = 1;
254 return common_val_print (val, stream, 0, &opts, current_language);
c906108c
SS
255}
256
79a45b7d 257/* TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and OPTIONS have the
c906108c
SS
258 same meanings as in cp_print_value and c_val_print.
259
260 DONT_PRINT is an array of baseclass types that we
261 should not print, or zero if called from top level. */
262
392a587b 263static void
fc1a4b47 264java_print_value_fields (struct type *type, const gdb_byte *valaddr,
490f124f 265 int offset,
a2bd3dcd 266 CORE_ADDR address, struct ui_file *stream,
79a45b7d 267 int recurse,
0e03807e 268 const struct value *val,
79a45b7d 269 const struct value_print_options *options)
c906108c
SS
270{
271 int i, len, n_baseclasses;
272
273 CHECK_TYPEDEF (type);
274
275 fprintf_filtered (stream, "{");
276 len = TYPE_NFIELDS (type);
277 n_baseclasses = TYPE_N_BASECLASSES (type);
278
279 if (n_baseclasses > 0)
280 {
281 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
282
283 for (i = 0; i < n_baseclasses; i++)
284 {
285 int boffset;
286 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
287 char *basename = TYPE_NAME (baseclass);
fc1a4b47 288 const gdb_byte *base_valaddr;
c5aa993b 289
c906108c
SS
290 if (BASETYPE_VIA_VIRTUAL (type, i))
291 continue;
292
293 if (basename != NULL && strcmp (basename, "java.lang.Object") == 0)
294 continue;
295
296 boffset = 0;
297
79a45b7d 298 if (options->pretty)
c906108c
SS
299 {
300 fprintf_filtered (stream, "\n");
c5aa993b 301 print_spaces_filtered (2 * (recurse + 1), stream);
c906108c
SS
302 }
303 fputs_filtered ("<", stream);
304 /* Not sure what the best notation is in the case where there is no
305 baseclass name. */
306 fputs_filtered (basename ? basename : "", stream);
307 fputs_filtered ("> = ", stream);
308
309 base_valaddr = valaddr;
310
490f124f
PA
311 java_print_value_fields (baseclass, base_valaddr,
312 offset + boffset, address,
0e03807e 313 stream, recurse + 1, val, options);
c906108c 314 fputs_filtered (", ", stream);
c906108c 315 }
c906108c
SS
316 }
317
318 if (!len && n_baseclasses == 1)
319 fprintf_filtered (stream, "<No data fields>");
320 else
321 {
c906108c
SS
322 int fields_seen = 0;
323
324 for (i = n_baseclasses; i < len; i++)
325 {
326 /* If requested, skip printing of static fields. */
d6a843b5 327 if (field_is_static (&TYPE_FIELD (type, i)))
c906108c
SS
328 {
329 char *name = TYPE_FIELD_NAME (type, i);
e0881a8e 330
79a45b7d 331 if (!options->static_field_print)
c906108c
SS
332 continue;
333 if (name != NULL && strcmp (name, "class") == 0)
334 continue;
335 }
336 if (fields_seen)
337 fprintf_filtered (stream, ", ");
338 else if (n_baseclasses > 0)
339 {
79a45b7d 340 if (options->pretty)
c906108c
SS
341 {
342 fprintf_filtered (stream, "\n");
343 print_spaces_filtered (2 + 2 * recurse, stream);
344 fputs_filtered ("members of ", stream);
345 fputs_filtered (type_name_no_tag (type), stream);
346 fputs_filtered (": ", stream);
347 }
348 }
349 fields_seen = 1;
350
79a45b7d 351 if (options->pretty)
c906108c
SS
352 {
353 fprintf_filtered (stream, "\n");
354 print_spaces_filtered (2 + 2 * recurse, stream);
355 }
c5aa993b 356 else
c906108c
SS
357 {
358 wrap_here (n_spaces (2 + 2 * recurse));
359 }
79a45b7d 360 if (options->inspect_it)
c906108c
SS
361 {
362 if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
363 fputs_filtered ("\"( ptr \"", stream);
364 else
365 fputs_filtered ("\"( nodef \"", stream);
d6a843b5 366 if (field_is_static (&TYPE_FIELD (type, i)))
c906108c
SS
367 fputs_filtered ("static ", stream);
368 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
369 language_cplus,
370 DMGL_PARAMS | DMGL_ANSI);
371 fputs_filtered ("\" \"", stream);
372 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
373 language_cplus,
374 DMGL_PARAMS | DMGL_ANSI);
375 fputs_filtered ("\") \"", stream);
376 }
377 else
378 {
379 annotate_field_begin (TYPE_FIELD_TYPE (type, i));
380
d6a843b5 381 if (field_is_static (&TYPE_FIELD (type, i)))
c906108c
SS
382 fputs_filtered ("static ", stream);
383 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
384 language_cplus,
385 DMGL_PARAMS | DMGL_ANSI);
386 annotate_field_name_end ();
387 fputs_filtered (": ", stream);
388 annotate_field_value ();
389 }
390
d6a843b5
JK
391 if (!field_is_static (&TYPE_FIELD (type, i))
392 && TYPE_FIELD_PACKED (type, i))
c906108c 393 {
75c9979e 394 struct value *v;
c906108c
SS
395
396 /* Bitfields require special handling, especially due to byte
c5aa993b 397 order problems. */
c906108c
SS
398 if (TYPE_FIELD_IGNORE (type, i))
399 {
c5aa993b 400 fputs_filtered ("<optimized out or zero length>", stream);
c906108c 401 }
8cf6f0b1
TT
402 else if (value_bits_synthetic_pointer (val,
403 TYPE_FIELD_BITPOS (type,
404 i),
405 TYPE_FIELD_BITSIZE (type,
406 i)))
407 {
408 fputs_filtered (_("<synthetic pointer>"), stream);
409 }
0e03807e
TT
410 else if (!value_bits_valid (val, TYPE_FIELD_BITPOS (type, i),
411 TYPE_FIELD_BITSIZE (type, i)))
412 {
585fdaa1 413 val_print_optimized_out (stream);
0e03807e 414 }
c906108c
SS
415 else
416 {
79a45b7d
TT
417 struct value_print_options opts;
418
5467c6c8 419 v = value_field_bitfield (type, i, valaddr, offset, val);
c906108c 420
79a45b7d
TT
421 opts = *options;
422 opts.deref_ref = 0;
423 common_val_print (v, stream, recurse + 1,
424 &opts, current_language);
c906108c
SS
425 }
426 }
427 else
428 {
429 if (TYPE_FIELD_IGNORE (type, i))
430 {
c5aa993b 431 fputs_filtered ("<optimized out or zero length>", stream);
c906108c 432 }
d6a843b5 433 else if (field_is_static (&TYPE_FIELD (type, i)))
c906108c 434 {
75c9979e 435 struct value *v = value_static_field (type, i);
e0881a8e 436
c906108c 437 if (v == NULL)
585fdaa1 438 val_print_optimized_out (stream);
c906108c
SS
439 else
440 {
79a45b7d 441 struct value_print_options opts;
df407dfe 442 struct type *t = check_typedef (value_type (v));
e0881a8e 443
c906108c
SS
444 if (TYPE_CODE (t) == TYPE_CODE_STRUCT)
445 v = value_addr (v);
79a45b7d
TT
446 opts = *options;
447 opts.deref_ref = 0;
448 common_val_print (v, stream, recurse + 1,
449 &opts, current_language);
c906108c
SS
450 }
451 }
7a292a7a
SS
452 else if (TYPE_FIELD_TYPE (type, i) == NULL)
453 fputs_filtered ("<unknown type>", stream);
c906108c
SS
454 else
455 {
79a45b7d 456 struct value_print_options opts = *options;
e0881a8e 457
79a45b7d 458 opts.deref_ref = 0;
c5aa993b 459 val_print (TYPE_FIELD_TYPE (type, i),
490f124f
PA
460 valaddr,
461 offset + TYPE_FIELD_BITPOS (type, i) / 8,
462 address, stream, recurse + 1, val, &opts,
d8ca156b 463 current_language);
c906108c
SS
464 }
465 }
466 annotate_field_end ();
467 }
468
79a45b7d 469 if (options->pretty)
c906108c
SS
470 {
471 fprintf_filtered (stream, "\n");
472 print_spaces_filtered (2 * recurse, stream);
473 }
474 }
475 fprintf_filtered (stream, "}");
476}
477
32b72a42
PA
478/* See val_print for a description of the various parameters of this
479 function; they are identical. The semantics of the return value is
480 also identical to val_print. */
c906108c
SS
481
482int
fc1a4b47 483java_val_print (struct type *type, const gdb_byte *valaddr,
a2bd3dcd 484 int embedded_offset, CORE_ADDR address,
79a45b7d 485 struct ui_file *stream, int recurse,
0e03807e 486 const struct value *val,
79a45b7d 487 const struct value_print_options *options)
c906108c 488{
5af949e3 489 struct gdbarch *gdbarch = get_type_arch (type);
1777feb0 490 unsigned int i = 0; /* Number of characters printed. */
c906108c
SS
491 struct type *target_type;
492 CORE_ADDR addr;
493
494 CHECK_TYPEDEF (type);
495 switch (TYPE_CODE (type))
496 {
497 case TYPE_CODE_PTR:
79a45b7d 498 if (options->format && options->format != 's')
c906108c 499 {
ab2188aa
PA
500 val_print_scalar_formatted (type, valaddr, embedded_offset,
501 val, options, 0, stream);
c906108c
SS
502 break;
503 }
504#if 0
79a45b7d 505 if (options->vtblprint && cp_is_vtbl_ptr_type (type))
c906108c 506 {
c5aa993b 507 /* Print the unmangled name if desired. */
c906108c 508 /* Print vtable entry - we only get here if we ARE using
1777feb0 509 -fvtable_thunks. (Otherwise, look under TYPE_CODE_STRUCT.) */
b276f1bb 510 /* Extract an address, assume that it is unsigned. */
490f124f
PA
511 print_address_demangle
512 (gdbarch,
513 extract_unsigned_integer (valaddr + embedded_offset,
514 TYPE_LENGTH (type)),
515 stream, demangle);
c906108c
SS
516 break;
517 }
518#endif
490f124f 519 addr = unpack_pointer (type, valaddr + embedded_offset);
c906108c
SS
520 if (addr == 0)
521 {
522 fputs_filtered ("null", stream);
523 return i;
524 }
525 target_type = check_typedef (TYPE_TARGET_TYPE (type));
526
527 if (TYPE_CODE (target_type) == TYPE_CODE_FUNC)
528 {
529 /* Try to print what function it points to. */
5af949e3 530 print_address_demangle (gdbarch, addr, stream, demangle);
c906108c
SS
531 /* Return value is irrelevant except for string pointers. */
532 return (0);
533 }
534
79a45b7d 535 if (options->addressprint && options->format != 's')
c906108c
SS
536 {
537 fputs_filtered ("@", stream);
538 print_longest (stream, 'x', 0, (ULONGEST) addr);
539 }
540
541 return i;
542
543 case TYPE_CODE_CHAR:
c906108c 544 case TYPE_CODE_INT:
c55a3f73
TT
545 /* Can't just call c_val_print because that prints bytes as C
546 chars. */
79a45b7d
TT
547 if (options->format || options->output_format)
548 {
549 struct value_print_options opts = *options;
e0881a8e 550
79a45b7d
TT
551 opts.format = (options->format ? options->format
552 : options->output_format);
ab2188aa
PA
553 val_print_scalar_formatted (type, valaddr, embedded_offset,
554 val, &opts, 0, stream);
79a45b7d 555 }
c55a3f73
TT
556 else if (TYPE_CODE (type) == TYPE_CODE_CHAR
557 || (TYPE_CODE (type) == TYPE_CODE_INT
558 && TYPE_LENGTH (type) == 2
559 && strcmp (TYPE_NAME (type), "char") == 0))
490f124f
PA
560 LA_PRINT_CHAR ((int) unpack_long (type, valaddr + embedded_offset),
561 type, stream);
c906108c 562 else
490f124f 563 val_print_type_code_int (type, valaddr + embedded_offset, stream);
c906108c
SS
564 break;
565
566 case TYPE_CODE_STRUCT:
490f124f
PA
567 java_print_value_fields (type, valaddr, embedded_offset,
568 address, stream, recurse, val, options);
c906108c
SS
569 break;
570
571 default:
572 return c_val_print (type, valaddr, embedded_offset, address, stream,
0e03807e 573 recurse, val, options);
c906108c
SS
574 }
575
576 return 0;
577}
This page took 1.013957 seconds and 4 git commands to generate.