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