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