a15bc5768ab457f997fada1c74adc2176befd408
[binutils-gdb.git] / gdb / jv-valprint.c
1 /* Support for printing Java values for GDB, the GNU debugger.
2
3 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007,
4 2008, 2009 Free Software Foundation, Inc.
5
6 This file is part of GDB.
7
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.
12
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.
17
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/>. */
20
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"
34
35 /* Local functions */
36
37 int
38 java_value_print (struct value *val, struct ui_file *stream,
39 const struct value_print_options *options)
40 {
41 struct gdbarch *gdbarch = current_gdbarch;
42 struct type *type;
43 CORE_ADDR address;
44 int i;
45 char *name;
46 struct value_print_options opts;
47
48 type = value_type (val);
49 address = value_address (val);
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
57 obj_addr = unpack_pointer (type, value_contents (val));
58
59 if (obj_addr != 0)
60 {
61 type = type_from_class (java_class_from_object (val));
62 type = lookup_pointer_type (type);
63
64 val = value_at (type, address);
65 }
66 }
67
68 if (TYPE_CODE (type) == TYPE_CODE_PTR && !value_logical_not (val))
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
73 && (i = strlen (name), name[i - 1] == ']'))
74 {
75 gdb_byte buf4[4];
76 long length;
77 unsigned int things_printed = 0;
78 int reps;
79 struct type *el_type = java_primitive_type_from_name (name, i - 2);
80 i = 0;
81 read_memory (address + get_java_object_header_size (gdbarch), 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 {
88 CORE_ADDR element;
89 CORE_ADDR next_element = -1; /* dummy initial value */
90
91 /* Skip object header and length. */
92 address += get_java_object_header_size (gdbarch) + 4;
93
94 while (i < length && things_printed < options->print_max)
95 {
96 gdb_byte *buf;
97
98 buf = alloca (gdbarch_ptr_bit (gdbarch) / HOST_CHAR_BIT);
99 fputs_filtered (", ", stream);
100 wrap_here (n_spaces (2));
101
102 if (i > 0)
103 element = next_element;
104 else
105 {
106 read_memory (address, buf, sizeof (buf));
107 address += gdbarch_ptr_bit (gdbarch) / HOST_CHAR_BIT;
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));
113 }
114
115 for (reps = 1; i + reps < length; reps++)
116 {
117 read_memory (address, buf, sizeof (buf));
118 address += gdbarch_ptr_bit (gdbarch) / HOST_CHAR_BIT;
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));
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
136 fprintf_filtered (stream, "@%s", paddr_nz (element));
137
138 things_printed++;
139 i += reps;
140 }
141 }
142 else
143 {
144 struct value *v = allocate_value (el_type);
145 struct value *next_v = allocate_value (el_type);
146
147 set_value_address (v, (address
148 + get_java_object_header_size (gdbarch) + 4));
149 set_value_address (next_v, value_raw_address (v));
150
151 while (i < length && things_printed < options->print_max)
152 {
153 fputs_filtered (", ", stream);
154 wrap_here (n_spaces (2));
155
156 if (i > 0)
157 {
158 struct value *tmp;
159
160 tmp = next_v;
161 next_v = v;
162 v = tmp;
163 }
164 else
165 {
166 set_value_lazy (v, 1);
167 set_value_offset (v, 0);
168 }
169
170 set_value_offset (next_v, value_offset (v));
171
172 for (reps = 1; i + reps < length; reps++)
173 {
174 set_value_lazy (next_v, 1);
175 set_value_offset (next_v, value_offset (next_v) + TYPE_LENGTH (el_type));
176 if (memcmp (value_contents (v), value_contents (next_v),
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
186 opts = *options;
187 opts.deref_ref = 1;
188 common_val_print (v, stream, 1, &opts, current_language);
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)
207 && TYPE_TAG_NAME (TYPE_TARGET_TYPE (type))
208 && strcmp (TYPE_TAG_NAME (TYPE_TARGET_TYPE (type)),
209 "java.lang.String") == 0
210 && (options->format == 0 || options->format == 's')
211 && address != 0
212 && value_as_address (val) != 0)
213 {
214 struct value *data_val;
215 CORE_ADDR data;
216 struct value *boffset_val;
217 unsigned long boffset;
218 struct value *count_val;
219 unsigned long count;
220 struct value *mark;
221
222 mark = value_mark (); /* Remember start of new values */
223
224 data_val = value_struct_elt (&val, NULL, "data", NULL, NULL);
225 data = value_as_address (data_val);
226
227 boffset_val = value_struct_elt (&val, NULL, "boffset", NULL, NULL);
228 boffset = value_as_address (boffset_val);
229
230 count_val = value_struct_elt (&val, NULL, "count", NULL, NULL);
231 count = value_as_address (count_val);
232
233 value_free_to_mark (mark); /* Release unnecessary values */
234
235 val_print_string (java_char_type, data + boffset, count, stream, options);
236
237 return 0;
238 }
239
240 opts = *options;
241 opts.deref_ref = 1;
242 return common_val_print (val, stream, 0, &opts, current_language);
243 }
244
245 /* TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and OPTIONS have the
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
251 static void
252 java_print_value_fields (struct type *type, const gdb_byte *valaddr,
253 CORE_ADDR address, struct ui_file *stream,
254 int recurse,
255 const struct value_print_options *options)
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);
274 const gdb_byte *base_valaddr;
275
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
284 if (options->pretty)
285 {
286 fprintf_filtered (stream, "\n");
287 print_spaces_filtered (2 * (recurse + 1), stream);
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,
298 stream, recurse + 1, options);
299 fputs_filtered (", ", stream);
300 }
301
302 }
303
304 if (!len && n_baseclasses == 1)
305 fprintf_filtered (stream, "<No data fields>");
306 else
307 {
308 int fields_seen = 0;
309
310 for (i = n_baseclasses; i < len; i++)
311 {
312 /* If requested, skip printing of static fields. */
313 if (field_is_static (&TYPE_FIELD (type, i)))
314 {
315 char *name = TYPE_FIELD_NAME (type, i);
316 if (!options->static_field_print)
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 {
325 if (options->pretty)
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
336 if (options->pretty)
337 {
338 fprintf_filtered (stream, "\n");
339 print_spaces_filtered (2 + 2 * recurse, stream);
340 }
341 else
342 {
343 wrap_here (n_spaces (2 + 2 * recurse));
344 }
345 if (options->inspect_it)
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);
351 if (field_is_static (&TYPE_FIELD (type, i)))
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
366 if (field_is_static (&TYPE_FIELD (type, i)))
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
376 if (!field_is_static (&TYPE_FIELD (type, i))
377 && TYPE_FIELD_PACKED (type, i))
378 {
379 struct value *v;
380
381 /* Bitfields require special handling, especially due to byte
382 order problems. */
383 if (TYPE_FIELD_IGNORE (type, i))
384 {
385 fputs_filtered ("<optimized out or zero length>", stream);
386 }
387 else
388 {
389 struct value_print_options opts;
390
391 v = value_from_longest (TYPE_FIELD_TYPE (type, i),
392 unpack_field_as_long (type, valaddr, i));
393
394 opts = *options;
395 opts.deref_ref = 0;
396 common_val_print (v, stream, recurse + 1,
397 &opts, current_language);
398 }
399 }
400 else
401 {
402 if (TYPE_FIELD_IGNORE (type, i))
403 {
404 fputs_filtered ("<optimized out or zero length>", stream);
405 }
406 else if (field_is_static (&TYPE_FIELD (type, i)))
407 {
408 struct value *v = value_static_field (type, i);
409 if (v == NULL)
410 fputs_filtered ("<optimized out>", stream);
411 else
412 {
413 struct value_print_options opts;
414 struct type *t = check_typedef (value_type (v));
415 if (TYPE_CODE (t) == TYPE_CODE_STRUCT)
416 v = value_addr (v);
417 opts = *options;
418 opts.deref_ref = 0;
419 common_val_print (v, stream, recurse + 1,
420 &opts, current_language);
421 }
422 }
423 else if (TYPE_FIELD_TYPE (type, i) == NULL)
424 fputs_filtered ("<unknown type>", stream);
425 else
426 {
427 struct value_print_options opts = *options;
428 opts.deref_ref = 0;
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,
432 stream, recurse + 1, &opts,
433 current_language);
434 }
435 }
436 annotate_field_end ();
437 }
438
439 if (options->pretty)
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
450 OPTIONS. The data at VALADDR is in target byte order.
451
452 If the data are a string pointer, returns the number of string characters
453 printed. */
454
455 int
456 java_val_print (struct type *type, const gdb_byte *valaddr,
457 int embedded_offset, CORE_ADDR address,
458 struct ui_file *stream, int recurse,
459 const struct value_print_options *options)
460 {
461 unsigned int i = 0; /* Number of characters printed */
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:
469 if (options->format && options->format != 's')
470 {
471 print_scalar_formatted (valaddr, type, options, 0, stream);
472 break;
473 }
474 #if 0
475 if (options->vtblprint && cp_is_vtbl_ptr_type (type))
476 {
477 /* Print the unmangled name if desired. */
478 /* Print vtable entry - we only get here if we ARE using
479 -fvtable_thunks. (Otherwise, look under TYPE_CODE_STRUCT.) */
480 /* Extract an address, assume that it is unsigned. */
481 print_address_demangle (extract_unsigned_integer (valaddr, TYPE_LENGTH (type)),
482 stream, demangle);
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
502 if (options->addressprint && options->format != 's')
503 {
504 fputs_filtered ("@", stream);
505 print_longest (stream, 'x', 0, (ULONGEST) addr);
506 }
507
508 return i;
509
510 case TYPE_CODE_CHAR:
511 case TYPE_CODE_INT:
512 /* Can't just call c_val_print because that prints bytes as C
513 chars. */
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 }
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))
525 LA_PRINT_CHAR ((int) unpack_long (type, valaddr), type, stream);
526 else
527 val_print_type_code_int (type, valaddr, stream);
528 break;
529
530 case TYPE_CODE_STRUCT:
531 java_print_value_fields (type, valaddr, address, stream, recurse,
532 options);
533 break;
534
535 default:
536 return c_val_print (type, valaddr, embedded_offset, address, stream,
537 recurse, options);
538 }
539
540 return 0;
541 }