2003-05-15 Andrew Cagney <cagney@redhat.com>
[deliverable/binutils-gdb.git] / gdb / jv-valprint.c
1 /* Support for printing Java values for GDB, the GNU debugger.
2 Copyright 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
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.
10
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.
15
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. */
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 static void java_print_value_fields (struct type * type, char *valaddr,
38 CORE_ADDR address,
39 struct ui_file *stream, int format,
40 int recurse,
41 enum val_prettyprint pretty);
42
43
44 int
45 java_value_print (struct value *val, struct ui_file *stream, int format,
46 enum val_prettyprint pretty)
47 {
48 struct type *type;
49 CORE_ADDR address;
50 int i;
51 char *name;
52
53 type = VALUE_TYPE (val);
54 address = VALUE_ADDRESS (val) + VALUE_OFFSET (val);
55
56 if (is_object_type (type))
57 {
58 CORE_ADDR obj_addr;
59
60 /* Get the run-time type, and cast the object into that */
61
62 obj_addr = unpack_pointer (type, VALUE_CONTENTS (val));
63
64 if (obj_addr != 0)
65 {
66 type = type_from_class (java_class_from_object (val));
67 type = lookup_pointer_type (type);
68
69 val = value_at (type, address, NULL);
70 }
71 }
72
73 if (TYPE_CODE (type) == TYPE_CODE_PTR && !value_logical_not (val))
74 type_print (TYPE_TARGET_TYPE (type), "", stream, -1);
75
76 name = TYPE_TAG_NAME (type);
77 if (TYPE_CODE (type) == TYPE_CODE_STRUCT && name != NULL
78 && (i = strlen (name), name[i - 1] == ']'))
79 {
80 char buf4[4];
81 long length;
82 unsigned int things_printed = 0;
83 int reps;
84 struct type *el_type = java_primitive_type_from_name (name, i - 2);
85
86 i = 0;
87 read_memory (address + JAVA_OBJECT_SIZE, buf4, 4);
88
89 length = (long) extract_signed_integer (buf4, 4);
90 fprintf_filtered (stream, "{length: %ld", length);
91
92 if (el_type == NULL)
93 {
94 CORE_ADDR element;
95 CORE_ADDR next_element = -1; /* dummy initial value */
96
97 address += JAVA_OBJECT_SIZE + 4; /* Skip object header and length. */
98
99 while (i < length && things_printed < print_max)
100 {
101 char *buf;
102
103 buf = alloca (TARGET_PTR_BIT / HOST_CHAR_BIT);
104 fputs_filtered (", ", stream);
105 wrap_here (n_spaces (2));
106
107 if (i > 0)
108 element = next_element;
109 else
110 {
111 read_memory (address, buf, sizeof (buf));
112 address += TARGET_PTR_BIT / HOST_CHAR_BIT;
113 element = extract_address (buf, sizeof (buf));
114 }
115
116 for (reps = 1; i + reps < length; reps++)
117 {
118 read_memory (address, buf, sizeof (buf));
119 address += TARGET_PTR_BIT / HOST_CHAR_BIT;
120 next_element = extract_address (buf, sizeof (buf));
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
133 fprintf_filtered (stream, "@%s", paddr_nz (element));
134
135 things_printed++;
136 i += reps;
137 }
138 }
139 else
140 {
141 struct value *v = allocate_value (el_type);
142 struct value *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 struct value *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
168 for (reps = 1; i + reps < length; reps++)
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_TAG_NAME (TYPE_TARGET_TYPE (type))
203 && strcmp (TYPE_TAG_NAME (TYPE_TARGET_TYPE (type)),
204 "java.lang.String") == 0
205 && (format == 0 || format == 's')
206 && address != 0
207 && value_as_address (val) != 0)
208 {
209 struct value *data_val;
210 CORE_ADDR data;
211 struct value *boffset_val;
212 unsigned long boffset;
213 struct value *count_val;
214 unsigned long count;
215 struct value *mark;
216
217 mark = value_mark (); /* Remember start of new values */
218
219 data_val = value_struct_elt (&val, NULL, "data", NULL, NULL);
220 data = value_as_address (data_val);
221
222 boffset_val = value_struct_elt (&val, NULL, "boffset", NULL, NULL);
223 boffset = value_as_address (boffset_val);
224
225 count_val = value_struct_elt (&val, NULL, "count", NULL, NULL);
226 count = value_as_address (count_val);
227
228 value_free_to_mark (mark); /* Release unnecessary values */
229
230 val_print_string (data + boffset, count, 2, stream);
231
232 return 0;
233 }
234
235 return (val_print (type, VALUE_CONTENTS (val), 0, address,
236 stream, format, 1, 0, pretty));
237 }
238
239 /* TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and PRETTY have the
240 same meanings as in cp_print_value and c_val_print.
241
242 DONT_PRINT is an array of baseclass types that we
243 should not print, or zero if called from top level. */
244
245 static void
246 java_print_value_fields (struct type *type, char *valaddr, CORE_ADDR address,
247 struct ui_file *stream, int format, int recurse,
248 enum val_prettyprint pretty)
249 {
250 int i, len, n_baseclasses;
251
252 CHECK_TYPEDEF (type);
253
254 fprintf_filtered (stream, "{");
255 len = TYPE_NFIELDS (type);
256 n_baseclasses = TYPE_N_BASECLASSES (type);
257
258 if (n_baseclasses > 0)
259 {
260 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
261
262 for (i = 0; i < n_baseclasses; i++)
263 {
264 int boffset;
265 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
266 char *basename = TYPE_NAME (baseclass);
267 char *base_valaddr;
268
269 if (BASETYPE_VIA_VIRTUAL (type, i))
270 continue;
271
272 if (basename != NULL && strcmp (basename, "java.lang.Object") == 0)
273 continue;
274
275 boffset = 0;
276
277 if (pretty)
278 {
279 fprintf_filtered (stream, "\n");
280 print_spaces_filtered (2 * (recurse + 1), stream);
281 }
282 fputs_filtered ("<", stream);
283 /* Not sure what the best notation is in the case where there is no
284 baseclass name. */
285 fputs_filtered (basename ? basename : "", stream);
286 fputs_filtered ("> = ", stream);
287
288 base_valaddr = valaddr;
289
290 java_print_value_fields (baseclass, base_valaddr, address + boffset,
291 stream, format, recurse + 1, pretty);
292 fputs_filtered (", ", stream);
293
294 flush_it:
295 ;
296 }
297
298 }
299
300 if (!len && n_baseclasses == 1)
301 fprintf_filtered (stream, "<No data fields>");
302 else
303 {
304 int fields_seen = 0;
305
306 for (i = n_baseclasses; i < len; i++)
307 {
308 /* If requested, skip printing of static fields. */
309 if (TYPE_FIELD_STATIC (type, i))
310 {
311 char *name = TYPE_FIELD_NAME (type, i);
312 if (!static_field_print)
313 continue;
314 if (name != NULL && strcmp (name, "class") == 0)
315 continue;
316 }
317 if (fields_seen)
318 fprintf_filtered (stream, ", ");
319 else if (n_baseclasses > 0)
320 {
321 if (pretty)
322 {
323 fprintf_filtered (stream, "\n");
324 print_spaces_filtered (2 + 2 * recurse, stream);
325 fputs_filtered ("members of ", stream);
326 fputs_filtered (type_name_no_tag (type), stream);
327 fputs_filtered (": ", stream);
328 }
329 }
330 fields_seen = 1;
331
332 if (pretty)
333 {
334 fprintf_filtered (stream, "\n");
335 print_spaces_filtered (2 + 2 * recurse, stream);
336 }
337 else
338 {
339 wrap_here (n_spaces (2 + 2 * recurse));
340 }
341 if (inspect_it)
342 {
343 if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
344 fputs_filtered ("\"( ptr \"", stream);
345 else
346 fputs_filtered ("\"( nodef \"", stream);
347 if (TYPE_FIELD_STATIC (type, i))
348 fputs_filtered ("static ", stream);
349 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
350 language_cplus,
351 DMGL_PARAMS | DMGL_ANSI);
352 fputs_filtered ("\" \"", stream);
353 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
354 language_cplus,
355 DMGL_PARAMS | DMGL_ANSI);
356 fputs_filtered ("\") \"", stream);
357 }
358 else
359 {
360 annotate_field_begin (TYPE_FIELD_TYPE (type, i));
361
362 if (TYPE_FIELD_STATIC (type, i))
363 fputs_filtered ("static ", stream);
364 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
365 language_cplus,
366 DMGL_PARAMS | DMGL_ANSI);
367 annotate_field_name_end ();
368 fputs_filtered (": ", stream);
369 annotate_field_value ();
370 }
371
372 if (!TYPE_FIELD_STATIC (type, i) && TYPE_FIELD_PACKED (type, i))
373 {
374 struct value *v;
375
376 /* Bitfields require special handling, especially due to byte
377 order problems. */
378 if (TYPE_FIELD_IGNORE (type, i))
379 {
380 fputs_filtered ("<optimized out or zero length>", stream);
381 }
382 else
383 {
384 v = value_from_longest (TYPE_FIELD_TYPE (type, i),
385 unpack_field_as_long (type, valaddr, i));
386
387 val_print (TYPE_FIELD_TYPE (type, i), VALUE_CONTENTS (v), 0,
388 0, stream, format, 0, recurse + 1, pretty);
389 }
390 }
391 else
392 {
393 if (TYPE_FIELD_IGNORE (type, i))
394 {
395 fputs_filtered ("<optimized out or zero length>", stream);
396 }
397 else if (TYPE_FIELD_STATIC (type, i))
398 {
399 struct value *v = value_static_field (type, i);
400 if (v == NULL)
401 fputs_filtered ("<optimized out>", stream);
402 else
403 {
404 struct type *t = check_typedef (VALUE_TYPE (v));
405 if (TYPE_CODE (t) == TYPE_CODE_STRUCT)
406 v = value_addr (v);
407 val_print (VALUE_TYPE (v),
408 VALUE_CONTENTS (v), 0, VALUE_ADDRESS (v),
409 stream, format, 0, recurse + 1, pretty);
410 }
411 }
412 else if (TYPE_FIELD_TYPE (type, i) == NULL)
413 fputs_filtered ("<unknown type>", stream);
414 else
415 {
416 val_print (TYPE_FIELD_TYPE (type, i),
417 valaddr + TYPE_FIELD_BITPOS (type, i) / 8, 0,
418 address + TYPE_FIELD_BITPOS (type, i) / 8,
419 stream, format, 0, recurse + 1, pretty);
420 }
421 }
422 annotate_field_end ();
423 }
424
425 if (pretty)
426 {
427 fprintf_filtered (stream, "\n");
428 print_spaces_filtered (2 * recurse, stream);
429 }
430 }
431 fprintf_filtered (stream, "}");
432 }
433
434 /* Print data of type TYPE located at VALADDR (within GDB), which came from
435 the inferior at address ADDRESS, onto stdio stream STREAM according to
436 FORMAT (a letter or 0 for natural format). The data at VALADDR is in
437 target byte order.
438
439 If the data are a string pointer, returns the number of string characters
440 printed.
441
442 If DEREF_REF is nonzero, then dereference references, otherwise just print
443 them like pointers.
444
445 The PRETTY parameter controls prettyprinting. */
446
447 int
448 java_val_print (struct type *type, char *valaddr, int embedded_offset,
449 CORE_ADDR address, struct ui_file *stream, int format,
450 int deref_ref, int recurse, enum val_prettyprint pretty)
451 {
452 register unsigned int i = 0; /* Number of characters printed */
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
466 if (vtblprint && cp_is_vtbl_ptr_type (type))
467 {
468 /* Print the unmangled name if desired. */
469 /* Print vtable entry - we only get here if we ARE using
470 -fvtable_thunks. (Otherwise, look under TYPE_CODE_STRUCT.) */
471 print_address_demangle (extract_address (valaddr, TYPE_LENGTH (type)),
472 stream, demangle);
473 break;
474 }
475 #endif
476 addr = unpack_pointer (type, valaddr);
477 if (addr == 0)
478 {
479 fputs_filtered ("null", stream);
480 return i;
481 }
482 target_type = check_typedef (TYPE_TARGET_TYPE (type));
483
484 if (TYPE_CODE (target_type) == TYPE_CODE_FUNC)
485 {
486 /* Try to print what function it points to. */
487 print_address_demangle (addr, stream, demangle);
488 /* Return value is irrelevant except for string pointers. */
489 return (0);
490 }
491
492 if (addressprint && format != 's')
493 {
494 fputs_filtered ("@", stream);
495 print_longest (stream, 'x', 0, (ULONGEST) addr);
496 }
497
498 return i;
499
500 case TYPE_CODE_CHAR:
501 case TYPE_CODE_INT:
502 /* Can't just call c_val_print because that prints bytes as C
503 chars. */
504 format = format ? format : output_format;
505 if (format)
506 print_scalar_formatted (valaddr, type, format, 0, stream);
507 else if (TYPE_CODE (type) == TYPE_CODE_CHAR
508 || (TYPE_CODE (type) == TYPE_CODE_INT
509 && TYPE_LENGTH (type) == 2
510 && strcmp (TYPE_NAME (type), "char") == 0))
511 LA_PRINT_CHAR ((int) unpack_long (type, valaddr), stream);
512 else
513 val_print_type_code_int (type, valaddr, stream);
514 break;
515
516 case TYPE_CODE_STRUCT:
517 java_print_value_fields (type, valaddr, address, stream, format,
518 recurse, pretty);
519 break;
520
521 default:
522 return c_val_print (type, valaddr, embedded_offset, address, stream,
523 format, deref_ref, recurse, pretty);
524 }
525
526 return 0;
527 }
This page took 0.041229 seconds and 5 git commands to generate.