*** empty log message ***
[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 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) + TYPE_LENGTH (el_type));
181 if (memcmp (value_contents (v), value_contents (next_v),
182 TYPE_LENGTH (el_type)) != 0)
183 break;
184 }
185
186 if (reps == 1)
187 fprintf_filtered (stream, "%d: ", i);
188 else
189 fprintf_filtered (stream, "%d..%d: ", i, i + reps - 1);
190
191 opts = *options;
192 opts.deref_ref = 1;
193 common_val_print (v, stream, 1, &opts, current_language);
194
195 things_printed++;
196 i += reps;
197 }
198 }
199
200 if (i < length)
201 fprintf_filtered (stream, "...");
202
203 fprintf_filtered (stream, "}");
204
205 return 0;
206 }
207
208 /* If it's type String, print it */
209
210 if (TYPE_CODE (type) == TYPE_CODE_PTR
211 && TYPE_TARGET_TYPE (type)
212 && TYPE_TAG_NAME (TYPE_TARGET_TYPE (type))
213 && strcmp (TYPE_TAG_NAME (TYPE_TARGET_TYPE (type)),
214 "java.lang.String") == 0
215 && (options->format == 0 || options->format == 's')
216 && address != 0
217 && value_as_address (val) != 0)
218 {
219 struct type *char_type;
220 struct value *data_val;
221 CORE_ADDR data;
222 struct value *boffset_val;
223 unsigned long boffset;
224 struct value *count_val;
225 unsigned long count;
226 struct value *mark;
227
228 mark = value_mark (); /* Remember start of new values */
229
230 data_val = value_struct_elt (&val, NULL, "data", NULL, NULL);
231 data = value_as_address (data_val);
232
233 boffset_val = value_struct_elt (&val, NULL, "boffset", NULL, NULL);
234 boffset = value_as_address (boffset_val);
235
236 count_val = value_struct_elt (&val, NULL, "count", NULL, NULL);
237 count = value_as_address (count_val);
238
239 value_free_to_mark (mark); /* Release unnecessary values */
240
241 char_type = builtin_java_type (gdbarch)->builtin_char;
242 val_print_string (char_type, data + boffset, count, stream, options);
243
244 return 0;
245 }
246
247 opts = *options;
248 opts.deref_ref = 1;
249 return common_val_print (val, stream, 0, &opts, current_language);
250 }
251
252 /* TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and OPTIONS have the
253 same meanings as in cp_print_value and c_val_print.
254
255 DONT_PRINT is an array of baseclass types that we
256 should not print, or zero if called from top level. */
257
258 static void
259 java_print_value_fields (struct type *type, const gdb_byte *valaddr,
260 CORE_ADDR address, struct ui_file *stream,
261 int recurse,
262 const struct value *val,
263 const struct value_print_options *options)
264 {
265 int i, len, n_baseclasses;
266
267 CHECK_TYPEDEF (type);
268
269 fprintf_filtered (stream, "{");
270 len = TYPE_NFIELDS (type);
271 n_baseclasses = TYPE_N_BASECLASSES (type);
272
273 if (n_baseclasses > 0)
274 {
275 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
276
277 for (i = 0; i < n_baseclasses; i++)
278 {
279 int boffset;
280 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
281 char *basename = TYPE_NAME (baseclass);
282 const gdb_byte *base_valaddr;
283
284 if (BASETYPE_VIA_VIRTUAL (type, i))
285 continue;
286
287 if (basename != NULL && strcmp (basename, "java.lang.Object") == 0)
288 continue;
289
290 boffset = 0;
291
292 if (options->pretty)
293 {
294 fprintf_filtered (stream, "\n");
295 print_spaces_filtered (2 * (recurse + 1), stream);
296 }
297 fputs_filtered ("<", stream);
298 /* Not sure what the best notation is in the case where there is no
299 baseclass name. */
300 fputs_filtered (basename ? basename : "", stream);
301 fputs_filtered ("> = ", stream);
302
303 base_valaddr = valaddr;
304
305 java_print_value_fields (baseclass, base_valaddr, address + boffset,
306 stream, recurse + 1, val, options);
307 fputs_filtered (", ", stream);
308 }
309
310 }
311
312 if (!len && n_baseclasses == 1)
313 fprintf_filtered (stream, "<No data fields>");
314 else
315 {
316 int fields_seen = 0;
317
318 for (i = n_baseclasses; i < len; i++)
319 {
320 /* If requested, skip printing of static fields. */
321 if (field_is_static (&TYPE_FIELD (type, i)))
322 {
323 char *name = TYPE_FIELD_NAME (type, i);
324
325 if (!options->static_field_print)
326 continue;
327 if (name != NULL && strcmp (name, "class") == 0)
328 continue;
329 }
330 if (fields_seen)
331 fprintf_filtered (stream, ", ");
332 else if (n_baseclasses > 0)
333 {
334 if (options->pretty)
335 {
336 fprintf_filtered (stream, "\n");
337 print_spaces_filtered (2 + 2 * recurse, stream);
338 fputs_filtered ("members of ", stream);
339 fputs_filtered (type_name_no_tag (type), stream);
340 fputs_filtered (": ", stream);
341 }
342 }
343 fields_seen = 1;
344
345 if (options->pretty)
346 {
347 fprintf_filtered (stream, "\n");
348 print_spaces_filtered (2 + 2 * recurse, stream);
349 }
350 else
351 {
352 wrap_here (n_spaces (2 + 2 * recurse));
353 }
354 if (options->inspect_it)
355 {
356 if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
357 fputs_filtered ("\"( ptr \"", stream);
358 else
359 fputs_filtered ("\"( nodef \"", stream);
360 if (field_is_static (&TYPE_FIELD (type, i)))
361 fputs_filtered ("static ", stream);
362 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
363 language_cplus,
364 DMGL_PARAMS | DMGL_ANSI);
365 fputs_filtered ("\" \"", stream);
366 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
367 language_cplus,
368 DMGL_PARAMS | DMGL_ANSI);
369 fputs_filtered ("\") \"", stream);
370 }
371 else
372 {
373 annotate_field_begin (TYPE_FIELD_TYPE (type, i));
374
375 if (field_is_static (&TYPE_FIELD (type, i)))
376 fputs_filtered ("static ", stream);
377 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
378 language_cplus,
379 DMGL_PARAMS | DMGL_ANSI);
380 annotate_field_name_end ();
381 fputs_filtered (": ", stream);
382 annotate_field_value ();
383 }
384
385 if (!field_is_static (&TYPE_FIELD (type, i))
386 && TYPE_FIELD_PACKED (type, i))
387 {
388 struct value *v;
389
390 /* Bitfields require special handling, especially due to byte
391 order problems. */
392 if (TYPE_FIELD_IGNORE (type, i))
393 {
394 fputs_filtered ("<optimized out or zero length>", stream);
395 }
396 else if (!value_bits_valid (val, TYPE_FIELD_BITPOS (type, i),
397 TYPE_FIELD_BITSIZE (type, i)))
398 {
399 fputs_filtered (_("<value optimized out>"), stream);
400 }
401 else
402 {
403 struct value_print_options opts;
404
405 v = value_from_longest (TYPE_FIELD_TYPE (type, i),
406 unpack_field_as_long (type, valaddr, i));
407
408 opts = *options;
409 opts.deref_ref = 0;
410 common_val_print (v, stream, recurse + 1,
411 &opts, current_language);
412 }
413 }
414 else
415 {
416 if (TYPE_FIELD_IGNORE (type, i))
417 {
418 fputs_filtered ("<optimized out or zero length>", stream);
419 }
420 else if (field_is_static (&TYPE_FIELD (type, i)))
421 {
422 struct value *v = value_static_field (type, i);
423
424 if (v == NULL)
425 fputs_filtered ("<optimized out>", stream);
426 else
427 {
428 struct value_print_options opts;
429 struct type *t = check_typedef (value_type (v));
430
431 if (TYPE_CODE (t) == TYPE_CODE_STRUCT)
432 v = value_addr (v);
433 opts = *options;
434 opts.deref_ref = 0;
435 common_val_print (v, stream, recurse + 1,
436 &opts, current_language);
437 }
438 }
439 else if (TYPE_FIELD_TYPE (type, i) == NULL)
440 fputs_filtered ("<unknown type>", stream);
441 else
442 {
443 struct value_print_options opts = *options;
444
445 opts.deref_ref = 0;
446 val_print (TYPE_FIELD_TYPE (type, i),
447 valaddr + TYPE_FIELD_BITPOS (type, i) / 8, 0,
448 address + TYPE_FIELD_BITPOS (type, i) / 8,
449 stream, recurse + 1, val, &opts,
450 current_language);
451 }
452 }
453 annotate_field_end ();
454 }
455
456 if (options->pretty)
457 {
458 fprintf_filtered (stream, "\n");
459 print_spaces_filtered (2 * recurse, stream);
460 }
461 }
462 fprintf_filtered (stream, "}");
463 }
464
465 /* Print data of type TYPE located at VALADDR (within GDB), which came from
466 the inferior at address ADDRESS, onto stdio stream STREAM according to
467 OPTIONS. The data at VALADDR is in target byte order.
468
469 If the data are a string pointer, returns the number of string characters
470 printed. */
471
472 int
473 java_val_print (struct type *type, const gdb_byte *valaddr,
474 int embedded_offset, CORE_ADDR address,
475 struct ui_file *stream, int recurse,
476 const struct value *val,
477 const struct value_print_options *options)
478 {
479 struct gdbarch *gdbarch = get_type_arch (type);
480 unsigned int i = 0; /* Number of characters printed */
481 struct type *target_type;
482 CORE_ADDR addr;
483
484 CHECK_TYPEDEF (type);
485 switch (TYPE_CODE (type))
486 {
487 case TYPE_CODE_PTR:
488 if (options->format && options->format != 's')
489 {
490 print_scalar_formatted (valaddr, type, options, 0, stream);
491 break;
492 }
493 #if 0
494 if (options->vtblprint && cp_is_vtbl_ptr_type (type))
495 {
496 /* Print the unmangled name if desired. */
497 /* Print vtable entry - we only get here if we ARE using
498 -fvtable_thunks. (Otherwise, look under TYPE_CODE_STRUCT.) */
499 /* Extract an address, assume that it is unsigned. */
500 print_address_demangle (gdbarch,
501 extract_unsigned_integer (valaddr, TYPE_LENGTH (type)),
502 stream, demangle);
503 break;
504 }
505 #endif
506 addr = unpack_pointer (type, valaddr);
507 if (addr == 0)
508 {
509 fputs_filtered ("null", stream);
510 return i;
511 }
512 target_type = check_typedef (TYPE_TARGET_TYPE (type));
513
514 if (TYPE_CODE (target_type) == TYPE_CODE_FUNC)
515 {
516 /* Try to print what function it points to. */
517 print_address_demangle (gdbarch, addr, stream, demangle);
518 /* Return value is irrelevant except for string pointers. */
519 return (0);
520 }
521
522 if (options->addressprint && options->format != 's')
523 {
524 fputs_filtered ("@", stream);
525 print_longest (stream, 'x', 0, (ULONGEST) addr);
526 }
527
528 return i;
529
530 case TYPE_CODE_CHAR:
531 case TYPE_CODE_INT:
532 /* Can't just call c_val_print because that prints bytes as C
533 chars. */
534 if (options->format || options->output_format)
535 {
536 struct value_print_options opts = *options;
537
538 opts.format = (options->format ? options->format
539 : options->output_format);
540 print_scalar_formatted (valaddr, type, &opts, 0, stream);
541 }
542 else if (TYPE_CODE (type) == TYPE_CODE_CHAR
543 || (TYPE_CODE (type) == TYPE_CODE_INT
544 && TYPE_LENGTH (type) == 2
545 && strcmp (TYPE_NAME (type), "char") == 0))
546 LA_PRINT_CHAR ((int) unpack_long (type, valaddr), type, stream);
547 else
548 val_print_type_code_int (type, valaddr, stream);
549 break;
550
551 case TYPE_CODE_STRUCT:
552 java_print_value_fields (type, valaddr, address, stream, recurse,
553 val, options);
554 break;
555
556 default:
557 return c_val_print (type, valaddr, embedded_offset, address, stream,
558 recurse, val, options);
559 }
560
561 return 0;
562 }
This page took 0.042281 seconds and 4 git commands to generate.