Update copyright year in most headers.
[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,
4c38e0a4 4 2008, 2009, 2010 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
56 /* Get the run-time type, and cast the object into that */
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);
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
AC
90 CORE_ADDR element;
91 CORE_ADDR next_element = -1; /* dummy initial value */
c906108c 92
45d5d5ca
UW
93 /* Skip object header and length. */
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);
f5cf64a7 179 set_value_offset (next_v, value_offset (next_v) + TYPE_LENGTH (el_type));
0fd88904 180 if (memcmp (value_contents (v), value_contents (next_v),
c906108c
SS
181 TYPE_LENGTH (el_type)) != 0)
182 break;
183 }
184
185 if (reps == 1)
186 fprintf_filtered (stream, "%d: ", i);
187 else
188 fprintf_filtered (stream, "%d..%d: ", i, i + reps - 1);
189
79a45b7d
TT
190 opts = *options;
191 opts.deref_ref = 1;
192 common_val_print (v, stream, 1, &opts, current_language);
c906108c
SS
193
194 things_printed++;
195 i += reps;
196 }
197 }
198
199 if (i < length)
200 fprintf_filtered (stream, "...");
201
202 fprintf_filtered (stream, "}");
203
204 return 0;
205 }
206
207 /* If it's type String, print it */
208
209 if (TYPE_CODE (type) == TYPE_CODE_PTR
210 && TYPE_TARGET_TYPE (type)
d4cad8db
TT
211 && TYPE_TAG_NAME (TYPE_TARGET_TYPE (type))
212 && strcmp (TYPE_TAG_NAME (TYPE_TARGET_TYPE (type)),
213 "java.lang.String") == 0
79a45b7d 214 && (options->format == 0 || options->format == 's')
8dccf761 215 && address != 0
1aa20aa8 216 && value_as_address (val) != 0)
c906108c 217 {
0daa2b63 218 struct type *char_type;
75c9979e 219 struct value *data_val;
c906108c 220 CORE_ADDR data;
75c9979e 221 struct value *boffset_val;
c906108c 222 unsigned long boffset;
75c9979e 223 struct value *count_val;
c906108c 224 unsigned long count;
75c9979e 225 struct value *mark;
c906108c
SS
226
227 mark = value_mark (); /* Remember start of new values */
228
229 data_val = value_struct_elt (&val, NULL, "data", NULL, NULL);
1aa20aa8 230 data = value_as_address (data_val);
c906108c
SS
231
232 boffset_val = value_struct_elt (&val, NULL, "boffset", NULL, NULL);
1aa20aa8 233 boffset = value_as_address (boffset_val);
c906108c
SS
234
235 count_val = value_struct_elt (&val, NULL, "count", NULL, NULL);
1aa20aa8 236 count = value_as_address (count_val);
c906108c 237
c5aa993b 238 value_free_to_mark (mark); /* Release unnecessary values */
c906108c 239
0daa2b63
UW
240 char_type = builtin_java_type (gdbarch)->builtin_char;
241 val_print_string (char_type, data + boffset, count, stream, options);
c906108c
SS
242
243 return 0;
244 }
245
79a45b7d
TT
246 opts = *options;
247 opts.deref_ref = 1;
248 return common_val_print (val, stream, 0, &opts, current_language);
c906108c
SS
249}
250
79a45b7d 251/* TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and OPTIONS have the
c906108c
SS
252 same meanings as in cp_print_value and c_val_print.
253
254 DONT_PRINT is an array of baseclass types that we
255 should not print, or zero if called from top level. */
256
392a587b 257static void
fc1a4b47 258java_print_value_fields (struct type *type, const gdb_byte *valaddr,
a2bd3dcd 259 CORE_ADDR address, struct ui_file *stream,
79a45b7d
TT
260 int recurse,
261 const struct value_print_options *options)
c906108c
SS
262{
263 int i, len, n_baseclasses;
264
265 CHECK_TYPEDEF (type);
266
267 fprintf_filtered (stream, "{");
268 len = TYPE_NFIELDS (type);
269 n_baseclasses = TYPE_N_BASECLASSES (type);
270
271 if (n_baseclasses > 0)
272 {
273 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
274
275 for (i = 0; i < n_baseclasses; i++)
276 {
277 int boffset;
278 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
279 char *basename = TYPE_NAME (baseclass);
fc1a4b47 280 const gdb_byte *base_valaddr;
c5aa993b 281
c906108c
SS
282 if (BASETYPE_VIA_VIRTUAL (type, i))
283 continue;
284
285 if (basename != NULL && strcmp (basename, "java.lang.Object") == 0)
286 continue;
287
288 boffset = 0;
289
79a45b7d 290 if (options->pretty)
c906108c
SS
291 {
292 fprintf_filtered (stream, "\n");
c5aa993b 293 print_spaces_filtered (2 * (recurse + 1), stream);
c906108c
SS
294 }
295 fputs_filtered ("<", stream);
296 /* Not sure what the best notation is in the case where there is no
297 baseclass name. */
298 fputs_filtered (basename ? basename : "", stream);
299 fputs_filtered ("> = ", stream);
300
301 base_valaddr = valaddr;
302
303 java_print_value_fields (baseclass, base_valaddr, address + boffset,
79a45b7d 304 stream, recurse + 1, options);
c906108c 305 fputs_filtered (", ", stream);
c906108c
SS
306 }
307
308 }
309
310 if (!len && n_baseclasses == 1)
311 fprintf_filtered (stream, "<No data fields>");
312 else
313 {
c906108c
SS
314 int fields_seen = 0;
315
316 for (i = n_baseclasses; i < len; i++)
317 {
318 /* If requested, skip printing of static fields. */
d6a843b5 319 if (field_is_static (&TYPE_FIELD (type, i)))
c906108c
SS
320 {
321 char *name = TYPE_FIELD_NAME (type, i);
79a45b7d 322 if (!options->static_field_print)
c906108c
SS
323 continue;
324 if (name != NULL && strcmp (name, "class") == 0)
325 continue;
326 }
327 if (fields_seen)
328 fprintf_filtered (stream, ", ");
329 else if (n_baseclasses > 0)
330 {
79a45b7d 331 if (options->pretty)
c906108c
SS
332 {
333 fprintf_filtered (stream, "\n");
334 print_spaces_filtered (2 + 2 * recurse, stream);
335 fputs_filtered ("members of ", stream);
336 fputs_filtered (type_name_no_tag (type), stream);
337 fputs_filtered (": ", stream);
338 }
339 }
340 fields_seen = 1;
341
79a45b7d 342 if (options->pretty)
c906108c
SS
343 {
344 fprintf_filtered (stream, "\n");
345 print_spaces_filtered (2 + 2 * recurse, stream);
346 }
c5aa993b 347 else
c906108c
SS
348 {
349 wrap_here (n_spaces (2 + 2 * recurse));
350 }
79a45b7d 351 if (options->inspect_it)
c906108c
SS
352 {
353 if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
354 fputs_filtered ("\"( ptr \"", stream);
355 else
356 fputs_filtered ("\"( nodef \"", stream);
d6a843b5 357 if (field_is_static (&TYPE_FIELD (type, i)))
c906108c
SS
358 fputs_filtered ("static ", stream);
359 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
360 language_cplus,
361 DMGL_PARAMS | DMGL_ANSI);
362 fputs_filtered ("\" \"", stream);
363 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
364 language_cplus,
365 DMGL_PARAMS | DMGL_ANSI);
366 fputs_filtered ("\") \"", stream);
367 }
368 else
369 {
370 annotate_field_begin (TYPE_FIELD_TYPE (type, i));
371
d6a843b5 372 if (field_is_static (&TYPE_FIELD (type, i)))
c906108c
SS
373 fputs_filtered ("static ", stream);
374 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
375 language_cplus,
376 DMGL_PARAMS | DMGL_ANSI);
377 annotate_field_name_end ();
378 fputs_filtered (": ", stream);
379 annotate_field_value ();
380 }
381
d6a843b5
JK
382 if (!field_is_static (&TYPE_FIELD (type, i))
383 && TYPE_FIELD_PACKED (type, i))
c906108c 384 {
75c9979e 385 struct value *v;
c906108c
SS
386
387 /* Bitfields require special handling, especially due to byte
c5aa993b 388 order problems. */
c906108c
SS
389 if (TYPE_FIELD_IGNORE (type, i))
390 {
c5aa993b 391 fputs_filtered ("<optimized out or zero length>", stream);
c906108c
SS
392 }
393 else
394 {
79a45b7d
TT
395 struct value_print_options opts;
396
c5aa993b 397 v = value_from_longest (TYPE_FIELD_TYPE (type, i),
c906108c
SS
398 unpack_field_as_long (type, valaddr, i));
399
79a45b7d
TT
400 opts = *options;
401 opts.deref_ref = 0;
402 common_val_print (v, stream, recurse + 1,
403 &opts, current_language);
c906108c
SS
404 }
405 }
406 else
407 {
408 if (TYPE_FIELD_IGNORE (type, i))
409 {
c5aa993b 410 fputs_filtered ("<optimized out or zero length>", stream);
c906108c 411 }
d6a843b5 412 else if (field_is_static (&TYPE_FIELD (type, i)))
c906108c 413 {
75c9979e 414 struct value *v = value_static_field (type, i);
c906108c
SS
415 if (v == NULL)
416 fputs_filtered ("<optimized out>", stream);
417 else
418 {
79a45b7d 419 struct value_print_options opts;
df407dfe 420 struct type *t = check_typedef (value_type (v));
c906108c
SS
421 if (TYPE_CODE (t) == TYPE_CODE_STRUCT)
422 v = value_addr (v);
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 }
7a292a7a
SS
429 else if (TYPE_FIELD_TYPE (type, i) == NULL)
430 fputs_filtered ("<unknown type>", stream);
c906108c
SS
431 else
432 {
79a45b7d
TT
433 struct value_print_options opts = *options;
434 opts.deref_ref = 0;
c5aa993b
JM
435 val_print (TYPE_FIELD_TYPE (type, i),
436 valaddr + TYPE_FIELD_BITPOS (type, i) / 8, 0,
437 address + TYPE_FIELD_BITPOS (type, i) / 8,
79a45b7d 438 stream, recurse + 1, &opts,
d8ca156b 439 current_language);
c906108c
SS
440 }
441 }
442 annotate_field_end ();
443 }
444
79a45b7d 445 if (options->pretty)
c906108c
SS
446 {
447 fprintf_filtered (stream, "\n");
448 print_spaces_filtered (2 * recurse, stream);
449 }
450 }
451 fprintf_filtered (stream, "}");
452}
453
454/* Print data of type TYPE located at VALADDR (within GDB), which came from
455 the inferior at address ADDRESS, onto stdio stream STREAM according to
79a45b7d 456 OPTIONS. The data at VALADDR is in target byte order.
c906108c
SS
457
458 If the data are a string pointer, returns the number of string characters
79a45b7d 459 printed. */
c906108c
SS
460
461int
fc1a4b47 462java_val_print (struct type *type, const gdb_byte *valaddr,
a2bd3dcd 463 int embedded_offset, CORE_ADDR address,
79a45b7d
TT
464 struct ui_file *stream, int recurse,
465 const struct value_print_options *options)
c906108c 466{
5af949e3 467 struct gdbarch *gdbarch = get_type_arch (type);
52f0bd74 468 unsigned int i = 0; /* Number of characters printed */
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 {
79a45b7d 478 print_scalar_formatted (valaddr, type, options, 0, stream);
c906108c
SS
479 break;
480 }
481#if 0
79a45b7d 482 if (options->vtblprint && cp_is_vtbl_ptr_type (type))
c906108c 483 {
c5aa993b 484 /* Print the unmangled name if desired. */
c906108c
SS
485 /* Print vtable entry - we only get here if we ARE using
486 -fvtable_thunks. (Otherwise, look under TYPE_CODE_STRUCT.) */
b276f1bb 487 /* Extract an address, assume that it is unsigned. */
5af949e3
UW
488 print_address_demangle (gdbarch,
489 extract_unsigned_integer (valaddr, TYPE_LENGTH (type)),
c5aa993b 490 stream, demangle);
c906108c
SS
491 break;
492 }
493#endif
494 addr = unpack_pointer (type, valaddr);
495 if (addr == 0)
496 {
497 fputs_filtered ("null", stream);
498 return i;
499 }
500 target_type = check_typedef (TYPE_TARGET_TYPE (type));
501
502 if (TYPE_CODE (target_type) == TYPE_CODE_FUNC)
503 {
504 /* Try to print what function it points to. */
5af949e3 505 print_address_demangle (gdbarch, addr, stream, demangle);
c906108c
SS
506 /* Return value is irrelevant except for string pointers. */
507 return (0);
508 }
509
79a45b7d 510 if (options->addressprint && options->format != 's')
c906108c
SS
511 {
512 fputs_filtered ("@", stream);
513 print_longest (stream, 'x', 0, (ULONGEST) addr);
514 }
515
516 return i;
517
518 case TYPE_CODE_CHAR:
c906108c 519 case TYPE_CODE_INT:
c55a3f73
TT
520 /* Can't just call c_val_print because that prints bytes as C
521 chars. */
79a45b7d
TT
522 if (options->format || options->output_format)
523 {
524 struct value_print_options opts = *options;
525 opts.format = (options->format ? options->format
526 : options->output_format);
527 print_scalar_formatted (valaddr, type, &opts, 0, stream);
528 }
c55a3f73
TT
529 else if (TYPE_CODE (type) == TYPE_CODE_CHAR
530 || (TYPE_CODE (type) == TYPE_CODE_INT
531 && TYPE_LENGTH (type) == 2
532 && strcmp (TYPE_NAME (type), "char") == 0))
6c7a06a3 533 LA_PRINT_CHAR ((int) unpack_long (type, valaddr), type, stream);
c906108c
SS
534 else
535 val_print_type_code_int (type, valaddr, stream);
536 break;
537
538 case TYPE_CODE_STRUCT:
79a45b7d
TT
539 java_print_value_fields (type, valaddr, address, stream, recurse,
540 options);
c906108c
SS
541 break;
542
543 default:
544 return c_val_print (type, valaddr, embedded_offset, address, stream,
79a45b7d 545 recurse, options);
c906108c
SS
546 }
547
548 return 0;
549}
This page took 0.841867 seconds and 4 git commands to generate.