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