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