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