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