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