* remote.c (remote_pid_to_str): If printing a process id and we
[binutils-gdb.git] / gdb / jv-valprint.c
blob08d2e6fb3d540f25c2990f5c25cf54840f1cbf67
1 /* Support for printing Java values for GDB, the GNU debugger.
3 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007,
4 2008, 2009 Free Software Foundation, Inc.
6 This file is part of GDB.
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
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
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.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "defs.h"
22 #include "symtab.h"
23 #include "gdbtypes.h"
24 #include "gdbcore.h"
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"
32 #include "annotate.h"
33 #include "gdb_string.h"
35 /* Local functions */
37 int
38 java_value_print (struct value *val, struct ui_file *stream,
39 const struct value_print_options *options)
41 struct gdbarch *gdbarch = get_type_arch (value_type (val));
42 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
43 struct type *type;
44 CORE_ADDR address;
45 int i;
46 char *name;
47 struct value_print_options opts;
49 type = value_type (val);
50 address = value_address (val);
52 if (is_object_type (type))
54 CORE_ADDR obj_addr;
56 /* Get the run-time type, and cast the object into that */
58 obj_addr = unpack_pointer (type, value_contents (val));
60 if (obj_addr != 0)
62 type = type_from_class (gdbarch, java_class_from_object (val));
63 type = lookup_pointer_type (type);
65 val = value_at (type, address);
69 if (TYPE_CODE (type) == TYPE_CODE_PTR && !value_logical_not (val))
70 type_print (TYPE_TARGET_TYPE (type), "", stream, -1);
72 name = TYPE_TAG_NAME (type);
73 if (TYPE_CODE (type) == TYPE_CODE_STRUCT && name != NULL
74 && (i = strlen (name), name[i - 1] == ']'))
76 gdb_byte buf4[4];
77 long length;
78 unsigned int things_printed = 0;
79 int reps;
80 struct type *el_type
81 = java_primitive_type_from_name (gdbarch, name, i - 2);
82 i = 0;
83 read_memory (address + get_java_object_header_size (gdbarch), buf4, 4);
85 length = (long) extract_signed_integer (buf4, 4, byte_order);
86 fprintf_filtered (stream, "{length: %ld", length);
88 if (el_type == NULL)
90 CORE_ADDR element;
91 CORE_ADDR next_element = -1; /* dummy initial value */
93 /* Skip object header and length. */
94 address += get_java_object_header_size (gdbarch) + 4;
96 while (i < length && things_printed < options->print_max)
98 gdb_byte *buf;
100 buf = alloca (gdbarch_ptr_bit (gdbarch) / HOST_CHAR_BIT);
101 fputs_filtered (", ", stream);
102 wrap_here (n_spaces (2));
104 if (i > 0)
105 element = next_element;
106 else
108 read_memory (address, buf, sizeof (buf));
109 address += gdbarch_ptr_bit (gdbarch) / HOST_CHAR_BIT;
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)! */
114 element = extract_unsigned_integer (buf, sizeof (buf),
115 byte_order);
118 for (reps = 1; i + reps < length; reps++)
120 read_memory (address, buf, sizeof (buf));
121 address += gdbarch_ptr_bit (gdbarch) / HOST_CHAR_BIT;
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)! */
126 next_element = extract_unsigned_integer (buf, sizeof (buf),
127 byte_order);
128 if (next_element != element)
129 break;
132 if (reps == 1)
133 fprintf_filtered (stream, "%d: ", i);
134 else
135 fprintf_filtered (stream, "%d..%d: ", i, i + reps - 1);
137 if (element == 0)
138 fprintf_filtered (stream, "null");
139 else
140 fprintf_filtered (stream, "@%s", paddress (gdbarch, element));
142 things_printed++;
143 i += reps;
146 else
148 struct value *v = allocate_value (el_type);
149 struct value *next_v = allocate_value (el_type);
151 set_value_address (v, (address
152 + get_java_object_header_size (gdbarch) + 4));
153 set_value_address (next_v, value_raw_address (v));
155 while (i < length && things_printed < options->print_max)
157 fputs_filtered (", ", stream);
158 wrap_here (n_spaces (2));
160 if (i > 0)
162 struct value *tmp;
164 tmp = next_v;
165 next_v = v;
166 v = tmp;
168 else
170 set_value_lazy (v, 1);
171 set_value_offset (v, 0);
174 set_value_offset (next_v, value_offset (v));
176 for (reps = 1; i + reps < length; reps++)
178 set_value_lazy (next_v, 1);
179 set_value_offset (next_v, value_offset (next_v) + TYPE_LENGTH (el_type));
180 if (memcmp (value_contents (v), value_contents (next_v),
181 TYPE_LENGTH (el_type)) != 0)
182 break;
185 if (reps == 1)
186 fprintf_filtered (stream, "%d: ", i);
187 else
188 fprintf_filtered (stream, "%d..%d: ", i, i + reps - 1);
190 opts = *options;
191 opts.deref_ref = 1;
192 common_val_print (v, stream, 1, &opts, current_language);
194 things_printed++;
195 i += reps;
199 if (i < length)
200 fprintf_filtered (stream, "...");
202 fprintf_filtered (stream, "}");
204 return 0;
207 /* If it's type String, print it */
209 if (TYPE_CODE (type) == TYPE_CODE_PTR
210 && TYPE_TARGET_TYPE (type)
211 && TYPE_TAG_NAME (TYPE_TARGET_TYPE (type))
212 && strcmp (TYPE_TAG_NAME (TYPE_TARGET_TYPE (type)),
213 "java.lang.String") == 0
214 && (options->format == 0 || options->format == 's')
215 && address != 0
216 && value_as_address (val) != 0)
218 struct type *char_type;
219 struct value *data_val;
220 CORE_ADDR data;
221 struct value *boffset_val;
222 unsigned long boffset;
223 struct value *count_val;
224 unsigned long count;
225 struct value *mark;
227 mark = value_mark (); /* Remember start of new values */
229 data_val = value_struct_elt (&val, NULL, "data", NULL, NULL);
230 data = value_as_address (data_val);
232 boffset_val = value_struct_elt (&val, NULL, "boffset", NULL, NULL);
233 boffset = value_as_address (boffset_val);
235 count_val = value_struct_elt (&val, NULL, "count", NULL, NULL);
236 count = value_as_address (count_val);
238 value_free_to_mark (mark); /* Release unnecessary values */
240 char_type = builtin_java_type (gdbarch)->builtin_char;
241 val_print_string (char_type, data + boffset, count, stream, options);
243 return 0;
246 opts = *options;
247 opts.deref_ref = 1;
248 return common_val_print (val, stream, 0, &opts, current_language);
251 /* TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and OPTIONS have the
252 same meanings as in cp_print_value and c_val_print.
254 DONT_PRINT is an array of baseclass types that we
255 should not print, or zero if called from top level. */
257 static void
258 java_print_value_fields (struct type *type, const gdb_byte *valaddr,
259 CORE_ADDR address, struct ui_file *stream,
260 int recurse,
261 const struct value_print_options *options)
263 int i, len, n_baseclasses;
265 CHECK_TYPEDEF (type);
267 fprintf_filtered (stream, "{");
268 len = TYPE_NFIELDS (type);
269 n_baseclasses = TYPE_N_BASECLASSES (type);
271 if (n_baseclasses > 0)
273 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
275 for (i = 0; i < n_baseclasses; i++)
277 int boffset;
278 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
279 char *basename = TYPE_NAME (baseclass);
280 const gdb_byte *base_valaddr;
282 if (BASETYPE_VIA_VIRTUAL (type, i))
283 continue;
285 if (basename != NULL && strcmp (basename, "java.lang.Object") == 0)
286 continue;
288 boffset = 0;
290 if (options->pretty)
292 fprintf_filtered (stream, "\n");
293 print_spaces_filtered (2 * (recurse + 1), stream);
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);
301 base_valaddr = valaddr;
303 java_print_value_fields (baseclass, base_valaddr, address + boffset,
304 stream, recurse + 1, options);
305 fputs_filtered (", ", stream);
310 if (!len && n_baseclasses == 1)
311 fprintf_filtered (stream, "<No data fields>");
312 else
314 int fields_seen = 0;
316 for (i = n_baseclasses; i < len; i++)
318 /* If requested, skip printing of static fields. */
319 if (field_is_static (&TYPE_FIELD (type, i)))
321 char *name = TYPE_FIELD_NAME (type, i);
322 if (!options->static_field_print)
323 continue;
324 if (name != NULL && strcmp (name, "class") == 0)
325 continue;
327 if (fields_seen)
328 fprintf_filtered (stream, ", ");
329 else if (n_baseclasses > 0)
331 if (options->pretty)
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);
340 fields_seen = 1;
342 if (options->pretty)
344 fprintf_filtered (stream, "\n");
345 print_spaces_filtered (2 + 2 * recurse, stream);
347 else
349 wrap_here (n_spaces (2 + 2 * recurse));
351 if (options->inspect_it)
353 if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
354 fputs_filtered ("\"( ptr \"", stream);
355 else
356 fputs_filtered ("\"( nodef \"", stream);
357 if (field_is_static (&TYPE_FIELD (type, i)))
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);
368 else
370 annotate_field_begin (TYPE_FIELD_TYPE (type, i));
372 if (field_is_static (&TYPE_FIELD (type, i)))
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 ();
382 if (!field_is_static (&TYPE_FIELD (type, i))
383 && TYPE_FIELD_PACKED (type, i))
385 struct value *v;
387 /* Bitfields require special handling, especially due to byte
388 order problems. */
389 if (TYPE_FIELD_IGNORE (type, i))
391 fputs_filtered ("<optimized out or zero length>", stream);
393 else
395 struct value_print_options opts;
397 v = value_from_longest (TYPE_FIELD_TYPE (type, i),
398 unpack_field_as_long (type, valaddr, i));
400 opts = *options;
401 opts.deref_ref = 0;
402 common_val_print (v, stream, recurse + 1,
403 &opts, current_language);
406 else
408 if (TYPE_FIELD_IGNORE (type, i))
410 fputs_filtered ("<optimized out or zero length>", stream);
412 else if (field_is_static (&TYPE_FIELD (type, i)))
414 struct value *v = value_static_field (type, i);
415 if (v == NULL)
416 fputs_filtered ("<optimized out>", stream);
417 else
419 struct value_print_options opts;
420 struct type *t = check_typedef (value_type (v));
421 if (TYPE_CODE (t) == TYPE_CODE_STRUCT)
422 v = value_addr (v);
423 opts = *options;
424 opts.deref_ref = 0;
425 common_val_print (v, stream, recurse + 1,
426 &opts, current_language);
429 else if (TYPE_FIELD_TYPE (type, i) == NULL)
430 fputs_filtered ("<unknown type>", stream);
431 else
433 struct value_print_options opts = *options;
434 opts.deref_ref = 0;
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,
438 stream, recurse + 1, &opts,
439 current_language);
442 annotate_field_end ();
445 if (options->pretty)
447 fprintf_filtered (stream, "\n");
448 print_spaces_filtered (2 * recurse, stream);
451 fprintf_filtered (stream, "}");
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
456 OPTIONS. The data at VALADDR is in target byte order.
458 If the data are a string pointer, returns the number of string characters
459 printed. */
462 java_val_print (struct type *type, const gdb_byte *valaddr,
463 int embedded_offset, CORE_ADDR address,
464 struct ui_file *stream, int recurse,
465 const struct value_print_options *options)
467 struct gdbarch *gdbarch = get_type_arch (type);
468 unsigned int i = 0; /* Number of characters printed */
469 struct type *target_type;
470 CORE_ADDR addr;
472 CHECK_TYPEDEF (type);
473 switch (TYPE_CODE (type))
475 case TYPE_CODE_PTR:
476 if (options->format && options->format != 's')
478 print_scalar_formatted (valaddr, type, options, 0, stream);
479 break;
481 #if 0
482 if (options->vtblprint && cp_is_vtbl_ptr_type (type))
484 /* Print the unmangled name if desired. */
485 /* Print vtable entry - we only get here if we ARE using
486 -fvtable_thunks. (Otherwise, look under TYPE_CODE_STRUCT.) */
487 /* Extract an address, assume that it is unsigned. */
488 print_address_demangle (gdbarch,
489 extract_unsigned_integer (valaddr, TYPE_LENGTH (type)),
490 stream, demangle);
491 break;
493 #endif
494 addr = unpack_pointer (type, valaddr);
495 if (addr == 0)
497 fputs_filtered ("null", stream);
498 return i;
500 target_type = check_typedef (TYPE_TARGET_TYPE (type));
502 if (TYPE_CODE (target_type) == TYPE_CODE_FUNC)
504 /* Try to print what function it points to. */
505 print_address_demangle (gdbarch, addr, stream, demangle);
506 /* Return value is irrelevant except for string pointers. */
507 return (0);
510 if (options->addressprint && options->format != 's')
512 fputs_filtered ("@", stream);
513 print_longest (stream, 'x', 0, (ULONGEST) addr);
516 return i;
518 case TYPE_CODE_CHAR:
519 case TYPE_CODE_INT:
520 /* Can't just call c_val_print because that prints bytes as C
521 chars. */
522 if (options->format || options->output_format)
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);
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))
533 LA_PRINT_CHAR ((int) unpack_long (type, valaddr), type, stream);
534 else
535 val_print_type_code_int (type, valaddr, stream);
536 break;
538 case TYPE_CODE_STRUCT:
539 java_print_value_fields (type, valaddr, address, stream, recurse,
540 options);
541 break;
543 default:
544 return c_val_print (type, valaddr, embedded_offset, address, stream,
545 recurse, options);
548 return 0;