*** empty log message ***
[deliverable/binutils-gdb.git] / gdb / jv-valprint.c
CommitLineData
c906108c 1/* Support for printing Java values for GDB, the GNU debugger.
1e4728e7 2
9b254dd1
DJ
3 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007,
4 2008 Free Software Foundation, Inc.
c906108c 5
c5aa993b 6 This file is part of GDB.
c906108c 7
c5aa993b
JM
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
a9762ec7 10 the Free Software Foundation; either version 3 of the License, or
c5aa993b 11 (at your option) any later version.
c906108c 12
c5aa993b
JM
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.
c906108c 17
c5aa993b 18 You should have received a copy of the GNU General Public License
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
20
21#include "defs.h"
22#include "symtab.h"
23#include "gdbtypes.h"
7a292a7a 24#include "gdbcore.h"
c906108c
SS
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"
7a292a7a 32#include "annotate.h"
309367d4 33#include "gdb_string.h"
c906108c 34
392a587b
JM
35/* Local functions */
36
c906108c 37int
75c9979e 38java_value_print (struct value *val, struct ui_file *stream, int format,
fba45db2 39 enum val_prettyprint pretty)
c906108c
SS
40{
41 struct type *type;
42 CORE_ADDR address;
43 int i;
44 char *name;
45
df407dfe
AC
46 type = value_type (val);
47 address = VALUE_ADDRESS (val) + value_offset (val);
c906108c
SS
48
49 if (is_object_type (type))
50 {
51 CORE_ADDR obj_addr;
52
53 /* Get the run-time type, and cast the object into that */
54
0fd88904 55 obj_addr = unpack_pointer (type, value_contents (val));
c906108c
SS
56
57 if (obj_addr != 0)
58 {
59 type = type_from_class (java_class_from_object (val));
60 type = lookup_pointer_type (type);
61
00a4c844 62 val = value_at (type, address);
c906108c
SS
63 }
64 }
65
c5aa993b 66 if (TYPE_CODE (type) == TYPE_CODE_PTR && !value_logical_not (val))
c906108c
SS
67 type_print (TYPE_TARGET_TYPE (type), "", stream, -1);
68
69 name = TYPE_TAG_NAME (type);
70 if (TYPE_CODE (type) == TYPE_CODE_STRUCT && name != NULL
c5aa993b 71 && (i = strlen (name), name[i - 1] == ']'))
c906108c 72 {
c68a6671 73 gdb_byte buf4[4];
c906108c
SS
74 long length;
75 unsigned int things_printed = 0;
c5aa993b 76 int reps;
c906108c
SS
77 struct type *el_type = java_primitive_type_from_name (name, i - 2);
78
79 i = 0;
80 read_memory (address + JAVA_OBJECT_SIZE, buf4, 4);
81
82 length = (long) extract_signed_integer (buf4, 4);
83 fprintf_filtered (stream, "{length: %ld", length);
84
85 if (el_type == NULL)
86 {
c65ecaf3
AC
87 CORE_ADDR element;
88 CORE_ADDR next_element = -1; /* dummy initial value */
c906108c 89
c5aa993b 90 address += JAVA_OBJECT_SIZE + 4; /* Skip object header and length. */
c906108c
SS
91
92 while (i < length && things_printed < print_max)
93 {
c68a6671 94 gdb_byte *buf;
c906108c 95
819844ad 96 buf = alloca (gdbarch_ptr_bit (current_gdbarch) / HOST_CHAR_BIT);
c906108c
SS
97 fputs_filtered (", ", stream);
98 wrap_here (n_spaces (2));
99
100 if (i > 0)
101 element = next_element;
102 else
103 {
c5aa993b 104 read_memory (address, buf, sizeof (buf));
819844ad 105 address += gdbarch_ptr_bit (current_gdbarch) / HOST_CHAR_BIT;
b276f1bb
AC
106 /* FIXME: cagney/2003-05-24: Bogus or what. It
107 pulls a host sized pointer out of the target and
108 then extracts that as an address (while assuming
109 that the address is unsigned)! */
110 element = extract_unsigned_integer (buf, sizeof (buf));
c906108c
SS
111 }
112
c5aa993b 113 for (reps = 1; i + reps < length; reps++)
c906108c 114 {
c5aa993b 115 read_memory (address, buf, sizeof (buf));
819844ad 116 address += gdbarch_ptr_bit (current_gdbarch) / HOST_CHAR_BIT;
b276f1bb
AC
117 /* FIXME: cagney/2003-05-24: Bogus or what. It
118 pulls a host sized pointer out of the target and
119 then extracts that as an address (while assuming
120 that the address is unsigned)! */
121 next_element = extract_unsigned_integer (buf, sizeof (buf));
c906108c
SS
122 if (next_element != element)
123 break;
124 }
125
126 if (reps == 1)
127 fprintf_filtered (stream, "%d: ", i);
128 else
129 fprintf_filtered (stream, "%d..%d: ", i, i + reps - 1);
130
131 if (element == 0)
132 fprintf_filtered (stream, "null");
133 else
d4f3574e 134 fprintf_filtered (stream, "@%s", paddr_nz (element));
c906108c
SS
135
136 things_printed++;
137 i += reps;
138 }
139 }
140 else
141 {
75c9979e
AC
142 struct value *v = allocate_value (el_type);
143 struct value *next_v = allocate_value (el_type);
c906108c
SS
144
145 VALUE_ADDRESS (v) = address + JAVA_OBJECT_SIZE + 4;
146 VALUE_ADDRESS (next_v) = VALUE_ADDRESS (v);
147
148 while (i < length && things_printed < print_max)
149 {
150 fputs_filtered (", ", stream);
151 wrap_here (n_spaces (2));
152
153 if (i > 0)
154 {
75c9979e 155 struct value *tmp;
c906108c
SS
156
157 tmp = next_v;
158 next_v = v;
159 v = tmp;
160 }
161 else
162 {
dfa52d88 163 set_value_lazy (v, 1);
f5cf64a7 164 set_value_offset (v, 0);
c906108c
SS
165 }
166
f5cf64a7 167 set_value_offset (next_v, value_offset (v));
c906108c 168
c5aa993b 169 for (reps = 1; i + reps < length; reps++)
c906108c 170 {
dfa52d88 171 set_value_lazy (next_v, 1);
f5cf64a7 172 set_value_offset (next_v, value_offset (next_v) + TYPE_LENGTH (el_type));
0fd88904 173 if (memcmp (value_contents (v), value_contents (next_v),
c906108c
SS
174 TYPE_LENGTH (el_type)) != 0)
175 break;
176 }
177
178 if (reps == 1)
179 fprintf_filtered (stream, "%d: ", i);
180 else
181 fprintf_filtered (stream, "%d..%d: ", i, i + reps - 1);
182
d8ca156b
JB
183 common_val_print (v, stream, format, 2, 1, pretty,
184 current_language);
c906108c
SS
185
186 things_printed++;
187 i += reps;
188 }
189 }
190
191 if (i < length)
192 fprintf_filtered (stream, "...");
193
194 fprintf_filtered (stream, "}");
195
196 return 0;
197 }
198
199 /* If it's type String, print it */
200
201 if (TYPE_CODE (type) == TYPE_CODE_PTR
202 && TYPE_TARGET_TYPE (type)
d4cad8db
TT
203 && TYPE_TAG_NAME (TYPE_TARGET_TYPE (type))
204 && strcmp (TYPE_TAG_NAME (TYPE_TARGET_TYPE (type)),
205 "java.lang.String") == 0
c906108c 206 && (format == 0 || format == 's')
8dccf761 207 && address != 0
1aa20aa8 208 && value_as_address (val) != 0)
c906108c 209 {
75c9979e 210 struct value *data_val;
c906108c 211 CORE_ADDR data;
75c9979e 212 struct value *boffset_val;
c906108c 213 unsigned long boffset;
75c9979e 214 struct value *count_val;
c906108c 215 unsigned long count;
75c9979e 216 struct value *mark;
c906108c
SS
217
218 mark = value_mark (); /* Remember start of new values */
219
220 data_val = value_struct_elt (&val, NULL, "data", NULL, NULL);
1aa20aa8 221 data = value_as_address (data_val);
c906108c
SS
222
223 boffset_val = value_struct_elt (&val, NULL, "boffset", NULL, NULL);
1aa20aa8 224 boffset = value_as_address (boffset_val);
c906108c
SS
225
226 count_val = value_struct_elt (&val, NULL, "count", NULL, NULL);
1aa20aa8 227 count = value_as_address (count_val);
c906108c 228
c5aa993b 229 value_free_to_mark (mark); /* Release unnecessary values */
c906108c
SS
230
231 val_print_string (data + boffset, count, 2, stream);
232
233 return 0;
234 }
235
d8ca156b
JB
236 return common_val_print (val, stream, format, 1, 0, pretty,
237 current_language);
c906108c
SS
238}
239
240/* TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and PRETTY have the
241 same meanings as in cp_print_value and c_val_print.
242
243 DONT_PRINT is an array of baseclass types that we
244 should not print, or zero if called from top level. */
245
392a587b 246static void
fc1a4b47 247java_print_value_fields (struct type *type, const gdb_byte *valaddr,
a2bd3dcd
AC
248 CORE_ADDR address, struct ui_file *stream,
249 int format, int recurse, enum val_prettyprint pretty)
c906108c
SS
250{
251 int i, len, n_baseclasses;
252
253 CHECK_TYPEDEF (type);
254
255 fprintf_filtered (stream, "{");
256 len = TYPE_NFIELDS (type);
257 n_baseclasses = TYPE_N_BASECLASSES (type);
258
259 if (n_baseclasses > 0)
260 {
261 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
262
263 for (i = 0; i < n_baseclasses; i++)
264 {
265 int boffset;
266 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
267 char *basename = TYPE_NAME (baseclass);
fc1a4b47 268 const gdb_byte *base_valaddr;
c5aa993b 269
c906108c
SS
270 if (BASETYPE_VIA_VIRTUAL (type, i))
271 continue;
272
273 if (basename != NULL && strcmp (basename, "java.lang.Object") == 0)
274 continue;
275
276 boffset = 0;
277
278 if (pretty)
279 {
280 fprintf_filtered (stream, "\n");
c5aa993b 281 print_spaces_filtered (2 * (recurse + 1), stream);
c906108c
SS
282 }
283 fputs_filtered ("<", stream);
284 /* Not sure what the best notation is in the case where there is no
285 baseclass name. */
286 fputs_filtered (basename ? basename : "", stream);
287 fputs_filtered ("> = ", stream);
288
289 base_valaddr = valaddr;
290
291 java_print_value_fields (baseclass, base_valaddr, address + boffset,
c5aa993b 292 stream, format, recurse + 1, pretty);
c906108c 293 fputs_filtered (", ", stream);
c906108c
SS
294 }
295
296 }
297
298 if (!len && n_baseclasses == 1)
299 fprintf_filtered (stream, "<No data fields>");
300 else
301 {
c906108c
SS
302 int fields_seen = 0;
303
304 for (i = n_baseclasses; i < len; i++)
305 {
306 /* If requested, skip printing of static fields. */
d6a843b5 307 if (field_is_static (&TYPE_FIELD (type, i)))
c906108c
SS
308 {
309 char *name = TYPE_FIELD_NAME (type, i);
310 if (!static_field_print)
311 continue;
312 if (name != NULL && strcmp (name, "class") == 0)
313 continue;
314 }
315 if (fields_seen)
316 fprintf_filtered (stream, ", ");
317 else if (n_baseclasses > 0)
318 {
319 if (pretty)
320 {
321 fprintf_filtered (stream, "\n");
322 print_spaces_filtered (2 + 2 * recurse, stream);
323 fputs_filtered ("members of ", stream);
324 fputs_filtered (type_name_no_tag (type), stream);
325 fputs_filtered (": ", stream);
326 }
327 }
328 fields_seen = 1;
329
330 if (pretty)
331 {
332 fprintf_filtered (stream, "\n");
333 print_spaces_filtered (2 + 2 * recurse, stream);
334 }
c5aa993b 335 else
c906108c
SS
336 {
337 wrap_here (n_spaces (2 + 2 * recurse));
338 }
339 if (inspect_it)
340 {
341 if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
342 fputs_filtered ("\"( ptr \"", stream);
343 else
344 fputs_filtered ("\"( nodef \"", stream);
d6a843b5 345 if (field_is_static (&TYPE_FIELD (type, i)))
c906108c
SS
346 fputs_filtered ("static ", stream);
347 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
348 language_cplus,
349 DMGL_PARAMS | DMGL_ANSI);
350 fputs_filtered ("\" \"", stream);
351 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
352 language_cplus,
353 DMGL_PARAMS | DMGL_ANSI);
354 fputs_filtered ("\") \"", stream);
355 }
356 else
357 {
358 annotate_field_begin (TYPE_FIELD_TYPE (type, i));
359
d6a843b5 360 if (field_is_static (&TYPE_FIELD (type, i)))
c906108c
SS
361 fputs_filtered ("static ", stream);
362 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
363 language_cplus,
364 DMGL_PARAMS | DMGL_ANSI);
365 annotate_field_name_end ();
366 fputs_filtered (": ", stream);
367 annotate_field_value ();
368 }
369
d6a843b5
JK
370 if (!field_is_static (&TYPE_FIELD (type, i))
371 && TYPE_FIELD_PACKED (type, i))
c906108c 372 {
75c9979e 373 struct value *v;
c906108c
SS
374
375 /* Bitfields require special handling, especially due to byte
c5aa993b 376 order problems. */
c906108c
SS
377 if (TYPE_FIELD_IGNORE (type, i))
378 {
c5aa993b 379 fputs_filtered ("<optimized out or zero length>", stream);
c906108c
SS
380 }
381 else
382 {
c5aa993b 383 v = value_from_longest (TYPE_FIELD_TYPE (type, i),
c906108c
SS
384 unpack_field_as_long (type, valaddr, i));
385
d8ca156b
JB
386 common_val_print (v, stream, format, 0, recurse + 1,
387 pretty, current_language);
c906108c
SS
388 }
389 }
390 else
391 {
392 if (TYPE_FIELD_IGNORE (type, i))
393 {
c5aa993b 394 fputs_filtered ("<optimized out or zero length>", stream);
c906108c 395 }
d6a843b5 396 else if (field_is_static (&TYPE_FIELD (type, i)))
c906108c 397 {
75c9979e 398 struct value *v = value_static_field (type, i);
c906108c
SS
399 if (v == NULL)
400 fputs_filtered ("<optimized out>", stream);
401 else
402 {
df407dfe 403 struct type *t = check_typedef (value_type (v));
c906108c
SS
404 if (TYPE_CODE (t) == TYPE_CODE_STRUCT)
405 v = value_addr (v);
806048c6 406 common_val_print (v, stream, format, 0, recurse + 1,
d8ca156b 407 pretty, current_language);
c906108c
SS
408 }
409 }
7a292a7a
SS
410 else if (TYPE_FIELD_TYPE (type, i) == NULL)
411 fputs_filtered ("<unknown type>", stream);
c906108c
SS
412 else
413 {
c5aa993b
JM
414 val_print (TYPE_FIELD_TYPE (type, i),
415 valaddr + TYPE_FIELD_BITPOS (type, i) / 8, 0,
416 address + TYPE_FIELD_BITPOS (type, i) / 8,
d8ca156b
JB
417 stream, format, 0, recurse + 1, pretty,
418 current_language);
c906108c
SS
419 }
420 }
421 annotate_field_end ();
422 }
423
424 if (pretty)
425 {
426 fprintf_filtered (stream, "\n");
427 print_spaces_filtered (2 * recurse, stream);
428 }
429 }
430 fprintf_filtered (stream, "}");
431}
432
433/* Print data of type TYPE located at VALADDR (within GDB), which came from
434 the inferior at address ADDRESS, onto stdio stream STREAM according to
435 FORMAT (a letter or 0 for natural format). The data at VALADDR is in
436 target byte order.
437
438 If the data are a string pointer, returns the number of string characters
439 printed.
440
441 If DEREF_REF is nonzero, then dereference references, otherwise just print
442 them like pointers.
443
444 The PRETTY parameter controls prettyprinting. */
445
446int
fc1a4b47 447java_val_print (struct type *type, const gdb_byte *valaddr,
a2bd3dcd
AC
448 int embedded_offset, CORE_ADDR address,
449 struct ui_file *stream, int format, int deref_ref,
450 int recurse, enum val_prettyprint pretty)
c906108c 451{
52f0bd74 452 unsigned int i = 0; /* Number of characters printed */
c906108c
SS
453 struct type *target_type;
454 CORE_ADDR addr;
455
456 CHECK_TYPEDEF (type);
457 switch (TYPE_CODE (type))
458 {
459 case TYPE_CODE_PTR:
460 if (format && format != 's')
461 {
462 print_scalar_formatted (valaddr, type, format, 0, stream);
463 break;
464 }
465#if 0
c5aa993b 466 if (vtblprint && cp_is_vtbl_ptr_type (type))
c906108c 467 {
c5aa993b 468 /* Print the unmangled name if desired. */
c906108c
SS
469 /* Print vtable entry - we only get here if we ARE using
470 -fvtable_thunks. (Otherwise, look under TYPE_CODE_STRUCT.) */
b276f1bb
AC
471 /* Extract an address, assume that it is unsigned. */
472 print_address_demangle (extract_unsigned_integer (valaddr, TYPE_LENGTH (type)),
c5aa993b 473 stream, demangle);
c906108c
SS
474 break;
475 }
476#endif
477 addr = unpack_pointer (type, valaddr);
478 if (addr == 0)
479 {
480 fputs_filtered ("null", stream);
481 return i;
482 }
483 target_type = check_typedef (TYPE_TARGET_TYPE (type));
484
485 if (TYPE_CODE (target_type) == TYPE_CODE_FUNC)
486 {
487 /* Try to print what function it points to. */
488 print_address_demangle (addr, stream, demangle);
489 /* Return value is irrelevant except for string pointers. */
490 return (0);
491 }
492
493 if (addressprint && format != 's')
494 {
495 fputs_filtered ("@", stream);
496 print_longest (stream, 'x', 0, (ULONGEST) addr);
497 }
498
499 return i;
500
501 case TYPE_CODE_CHAR:
c906108c 502 case TYPE_CODE_INT:
c55a3f73
TT
503 /* Can't just call c_val_print because that prints bytes as C
504 chars. */
c906108c
SS
505 format = format ? format : output_format;
506 if (format)
507 print_scalar_formatted (valaddr, type, format, 0, stream);
c55a3f73
TT
508 else if (TYPE_CODE (type) == TYPE_CODE_CHAR
509 || (TYPE_CODE (type) == TYPE_CODE_INT
510 && TYPE_LENGTH (type) == 2
511 && strcmp (TYPE_NAME (type), "char") == 0))
512 LA_PRINT_CHAR ((int) unpack_long (type, valaddr), stream);
c906108c
SS
513 else
514 val_print_type_code_int (type, valaddr, stream);
515 break;
516
517 case TYPE_CODE_STRUCT:
518 java_print_value_fields (type, valaddr, address, stream, format,
519 recurse, pretty);
520 break;
521
522 default:
523 return c_val_print (type, valaddr, embedded_offset, address, stream,
524 format, deref_ref, recurse, pretty);
525 }
526
527 return 0;
528}
This page took 0.673349 seconds and 4 git commands to generate.