* linux-low.c (regsets_fetch_inferior_registers): Fix memory leak.
[gdb/SamB.git] / gdb / jv-valprint.c
bloba1b8a715174844622b04a51f75e33aae03ac6f90
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 type *type;
42 CORE_ADDR address;
43 int i;
44 char *name;
45 struct value_print_options opts;
47 type = value_type (val);
48 address = VALUE_ADDRESS (val) + value_offset (val);
50 if (is_object_type (type))
52 CORE_ADDR obj_addr;
54 /* Get the run-time type, and cast the object into that */
56 obj_addr = unpack_pointer (type, value_contents (val));
58 if (obj_addr != 0)
60 type = type_from_class (java_class_from_object (val));
61 type = lookup_pointer_type (type);
63 val = value_at (type, address);
67 if (TYPE_CODE (type) == TYPE_CODE_PTR && !value_logical_not (val))
68 type_print (TYPE_TARGET_TYPE (type), "", stream, -1);
70 name = TYPE_TAG_NAME (type);
71 if (TYPE_CODE (type) == TYPE_CODE_STRUCT && name != NULL
72 && (i = strlen (name), name[i - 1] == ']'))
74 gdb_byte buf4[4];
75 long length;
76 unsigned int things_printed = 0;
77 int reps;
78 struct type *el_type = java_primitive_type_from_name (name, i - 2);
80 i = 0;
81 read_memory (address + JAVA_OBJECT_SIZE, buf4, 4);
83 length = (long) extract_signed_integer (buf4, 4);
84 fprintf_filtered (stream, "{length: %ld", length);
86 if (el_type == NULL)
88 CORE_ADDR element;
89 CORE_ADDR next_element = -1; /* dummy initial value */
91 address += JAVA_OBJECT_SIZE + 4; /* Skip object header and length. */
93 while (i < length && things_printed < options->print_max)
95 gdb_byte *buf;
97 buf = alloca (gdbarch_ptr_bit (current_gdbarch) / HOST_CHAR_BIT);
98 fputs_filtered (", ", stream);
99 wrap_here (n_spaces (2));
101 if (i > 0)
102 element = next_element;
103 else
105 read_memory (address, buf, sizeof (buf));
106 address += gdbarch_ptr_bit (current_gdbarch) / HOST_CHAR_BIT;
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));
114 for (reps = 1; i + reps < length; reps++)
116 read_memory (address, buf, sizeof (buf));
117 address += gdbarch_ptr_bit (current_gdbarch) / HOST_CHAR_BIT;
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));
123 if (next_element != element)
124 break;
127 if (reps == 1)
128 fprintf_filtered (stream, "%d: ", i);
129 else
130 fprintf_filtered (stream, "%d..%d: ", i, i + reps - 1);
132 if (element == 0)
133 fprintf_filtered (stream, "null");
134 else
135 fprintf_filtered (stream, "@%s", paddr_nz (element));
137 things_printed++;
138 i += reps;
141 else
143 struct value *v = allocate_value (el_type);
144 struct value *next_v = allocate_value (el_type);
146 VALUE_ADDRESS (v) = address + JAVA_OBJECT_SIZE + 4;
147 VALUE_ADDRESS (next_v) = VALUE_ADDRESS (v);
149 while (i < length && things_printed < options->print_max)
151 fputs_filtered (", ", stream);
152 wrap_here (n_spaces (2));
154 if (i > 0)
156 struct value *tmp;
158 tmp = next_v;
159 next_v = v;
160 v = tmp;
162 else
164 set_value_lazy (v, 1);
165 set_value_offset (v, 0);
168 set_value_offset (next_v, value_offset (v));
170 for (reps = 1; i + reps < length; reps++)
172 set_value_lazy (next_v, 1);
173 set_value_offset (next_v, value_offset (next_v) + TYPE_LENGTH (el_type));
174 if (memcmp (value_contents (v), value_contents (next_v),
175 TYPE_LENGTH (el_type)) != 0)
176 break;
179 if (reps == 1)
180 fprintf_filtered (stream, "%d: ", i);
181 else
182 fprintf_filtered (stream, "%d..%d: ", i, i + reps - 1);
184 opts = *options;
185 opts.deref_ref = 1;
186 common_val_print (v, stream, 1, &opts, current_language);
188 things_printed++;
189 i += reps;
193 if (i < length)
194 fprintf_filtered (stream, "...");
196 fprintf_filtered (stream, "}");
198 return 0;
201 /* If it's type String, print it */
203 if (TYPE_CODE (type) == TYPE_CODE_PTR
204 && TYPE_TARGET_TYPE (type)
205 && TYPE_TAG_NAME (TYPE_TARGET_TYPE (type))
206 && strcmp (TYPE_TAG_NAME (TYPE_TARGET_TYPE (type)),
207 "java.lang.String") == 0
208 && (options->format == 0 || options->format == 's')
209 && address != 0
210 && value_as_address (val) != 0)
212 struct value *data_val;
213 CORE_ADDR data;
214 struct value *boffset_val;
215 unsigned long boffset;
216 struct value *count_val;
217 unsigned long count;
218 struct value *mark;
220 mark = value_mark (); /* Remember start of new values */
222 data_val = value_struct_elt (&val, NULL, "data", NULL, NULL);
223 data = value_as_address (data_val);
225 boffset_val = value_struct_elt (&val, NULL, "boffset", NULL, NULL);
226 boffset = value_as_address (boffset_val);
228 count_val = value_struct_elt (&val, NULL, "count", NULL, NULL);
229 count = value_as_address (count_val);
231 value_free_to_mark (mark); /* Release unnecessary values */
233 val_print_string (java_char_type, data + boffset, count, stream, options);
235 return 0;
238 opts = *options;
239 opts.deref_ref = 1;
240 return common_val_print (val, stream, 0, &opts, current_language);
243 /* TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and OPTIONS have the
244 same meanings as in cp_print_value and c_val_print.
246 DONT_PRINT is an array of baseclass types that we
247 should not print, or zero if called from top level. */
249 static void
250 java_print_value_fields (struct type *type, const gdb_byte *valaddr,
251 CORE_ADDR address, struct ui_file *stream,
252 int recurse,
253 const struct value_print_options *options)
255 int i, len, n_baseclasses;
257 CHECK_TYPEDEF (type);
259 fprintf_filtered (stream, "{");
260 len = TYPE_NFIELDS (type);
261 n_baseclasses = TYPE_N_BASECLASSES (type);
263 if (n_baseclasses > 0)
265 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
267 for (i = 0; i < n_baseclasses; i++)
269 int boffset;
270 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
271 char *basename = TYPE_NAME (baseclass);
272 const gdb_byte *base_valaddr;
274 if (BASETYPE_VIA_VIRTUAL (type, i))
275 continue;
277 if (basename != NULL && strcmp (basename, "java.lang.Object") == 0)
278 continue;
280 boffset = 0;
282 if (options->pretty)
284 fprintf_filtered (stream, "\n");
285 print_spaces_filtered (2 * (recurse + 1), stream);
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);
293 base_valaddr = valaddr;
295 java_print_value_fields (baseclass, base_valaddr, address + boffset,
296 stream, recurse + 1, options);
297 fputs_filtered (", ", stream);
302 if (!len && n_baseclasses == 1)
303 fprintf_filtered (stream, "<No data fields>");
304 else
306 int fields_seen = 0;
308 for (i = n_baseclasses; i < len; i++)
310 /* If requested, skip printing of static fields. */
311 if (field_is_static (&TYPE_FIELD (type, i)))
313 char *name = TYPE_FIELD_NAME (type, i);
314 if (!options->static_field_print)
315 continue;
316 if (name != NULL && strcmp (name, "class") == 0)
317 continue;
319 if (fields_seen)
320 fprintf_filtered (stream, ", ");
321 else if (n_baseclasses > 0)
323 if (options->pretty)
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);
332 fields_seen = 1;
334 if (options->pretty)
336 fprintf_filtered (stream, "\n");
337 print_spaces_filtered (2 + 2 * recurse, stream);
339 else
341 wrap_here (n_spaces (2 + 2 * recurse));
343 if (options->inspect_it)
345 if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
346 fputs_filtered ("\"( ptr \"", stream);
347 else
348 fputs_filtered ("\"( nodef \"", stream);
349 if (field_is_static (&TYPE_FIELD (type, i)))
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);
360 else
362 annotate_field_begin (TYPE_FIELD_TYPE (type, i));
364 if (field_is_static (&TYPE_FIELD (type, i)))
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 ();
374 if (!field_is_static (&TYPE_FIELD (type, i))
375 && TYPE_FIELD_PACKED (type, i))
377 struct value *v;
379 /* Bitfields require special handling, especially due to byte
380 order problems. */
381 if (TYPE_FIELD_IGNORE (type, i))
383 fputs_filtered ("<optimized out or zero length>", stream);
385 else
387 struct value_print_options opts;
389 v = value_from_longest (TYPE_FIELD_TYPE (type, i),
390 unpack_field_as_long (type, valaddr, i));
392 opts = *options;
393 opts.deref_ref = 0;
394 common_val_print (v, stream, recurse + 1,
395 &opts, current_language);
398 else
400 if (TYPE_FIELD_IGNORE (type, i))
402 fputs_filtered ("<optimized out or zero length>", stream);
404 else if (field_is_static (&TYPE_FIELD (type, i)))
406 struct value *v = value_static_field (type, i);
407 if (v == NULL)
408 fputs_filtered ("<optimized out>", stream);
409 else
411 struct value_print_options opts;
412 struct type *t = check_typedef (value_type (v));
413 if (TYPE_CODE (t) == TYPE_CODE_STRUCT)
414 v = value_addr (v);
415 opts = *options;
416 opts.deref_ref = 0;
417 common_val_print (v, stream, recurse + 1,
418 &opts, current_language);
421 else if (TYPE_FIELD_TYPE (type, i) == NULL)
422 fputs_filtered ("<unknown type>", stream);
423 else
425 struct value_print_options opts = *options;
426 opts.deref_ref = 0;
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,
430 stream, recurse + 1, &opts,
431 current_language);
434 annotate_field_end ();
437 if (options->pretty)
439 fprintf_filtered (stream, "\n");
440 print_spaces_filtered (2 * recurse, stream);
443 fprintf_filtered (stream, "}");
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
448 OPTIONS. The data at VALADDR is in target byte order.
450 If the data are a string pointer, returns the number of string characters
451 printed. */
454 java_val_print (struct type *type, const gdb_byte *valaddr,
455 int embedded_offset, CORE_ADDR address,
456 struct ui_file *stream, int recurse,
457 const struct value_print_options *options)
459 unsigned int i = 0; /* Number of characters printed */
460 struct type *target_type;
461 CORE_ADDR addr;
463 CHECK_TYPEDEF (type);
464 switch (TYPE_CODE (type))
466 case TYPE_CODE_PTR:
467 if (options->format && options->format != 's')
469 print_scalar_formatted (valaddr, type, options, 0, stream);
470 break;
472 #if 0
473 if (options->vtblprint && cp_is_vtbl_ptr_type (type))
475 /* Print the unmangled name if desired. */
476 /* Print vtable entry - we only get here if we ARE using
477 -fvtable_thunks. (Otherwise, look under TYPE_CODE_STRUCT.) */
478 /* Extract an address, assume that it is unsigned. */
479 print_address_demangle (extract_unsigned_integer (valaddr, TYPE_LENGTH (type)),
480 stream, demangle);
481 break;
483 #endif
484 addr = unpack_pointer (type, valaddr);
485 if (addr == 0)
487 fputs_filtered ("null", stream);
488 return i;
490 target_type = check_typedef (TYPE_TARGET_TYPE (type));
492 if (TYPE_CODE (target_type) == TYPE_CODE_FUNC)
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);
500 if (options->addressprint && options->format != 's')
502 fputs_filtered ("@", stream);
503 print_longest (stream, 'x', 0, (ULONGEST) addr);
506 return i;
508 case TYPE_CODE_CHAR:
509 case TYPE_CODE_INT:
510 /* Can't just call c_val_print because that prints bytes as C
511 chars. */
512 if (options->format || options->output_format)
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);
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))
523 LA_PRINT_CHAR ((int) unpack_long (type, valaddr), type, stream);
524 else
525 val_print_type_code_int (type, valaddr, stream);
526 break;
528 case TYPE_CODE_STRUCT:
529 java_print_value_fields (type, valaddr, address, stream, recurse,
530 options);
531 break;
533 default:
534 return c_val_print (type, valaddr, embedded_offset, address, stream,
535 recurse, options);
538 return 0;