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