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