]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/jv-valprint.c
import gdb-1999-09-08 snapshot
[thirdparty/binutils-gdb.git] / gdb / jv-valprint.c
1 /* Support for printing Java values for GDB, the GNU debugger.
2 Copyright 1997, 1998, 1999 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
34 /* Local functions */
35
36 static void java_print_value_fields PARAMS ((struct type * type, char *valaddr, CORE_ADDR address, GDB_FILE * stream, int format, int recurse, enum val_prettyprint pretty));
37
38
39 int
40 java_value_print (val, stream, format, pretty)
41 value_ptr val;
42 GDB_FILE *stream;
43 int format;
44 enum val_prettyprint pretty;
45 {
46 struct type *type;
47 CORE_ADDR address;
48 int i;
49 char *name;
50
51 type = VALUE_TYPE (val);
52 address = VALUE_ADDRESS (val) + VALUE_OFFSET (val);
53
54 if (is_object_type (type))
55 {
56 CORE_ADDR obj_addr;
57
58 /* Get the run-time type, and cast the object into that */
59
60 obj_addr = unpack_pointer (type, VALUE_CONTENTS (val));
61
62 if (obj_addr != 0)
63 {
64 type = type_from_class (java_class_from_object (val));
65 type = lookup_pointer_type (type);
66
67 val = value_at (type, address, NULL);
68 }
69 }
70
71 if (TYPE_CODE (type) == TYPE_CODE_PTR && !value_logical_not (val))
72 type_print (TYPE_TARGET_TYPE (type), "", stream, -1);
73
74 name = TYPE_TAG_NAME (type);
75 if (TYPE_CODE (type) == TYPE_CODE_STRUCT && name != NULL
76 && (i = strlen (name), name[i - 1] == ']'))
77 {
78 char buf4[4];
79 long length;
80 unsigned int things_printed = 0;
81 int reps;
82 struct type *el_type = java_primitive_type_from_name (name, i - 2);
83
84 i = 0;
85 read_memory (address + JAVA_OBJECT_SIZE, buf4, 4);
86
87 length = (long) extract_signed_integer (buf4, 4);
88 fprintf_filtered (stream, "{length: %ld", length);
89
90 if (el_type == NULL)
91 {
92 CORE_ADDR element, next_element;
93
94 address += JAVA_OBJECT_SIZE + 4; /* Skip object header and length. */
95
96 while (i < length && things_printed < print_max)
97 {
98 char buf[TARGET_PTR_BIT / HOST_CHAR_BIT];
99
100 fputs_filtered (", ", stream);
101 wrap_here (n_spaces (2));
102
103 if (i > 0)
104 element = next_element;
105 else
106 {
107 read_memory (address, buf, sizeof (buf));
108 address += TARGET_PTR_BIT / HOST_CHAR_BIT;
109 element = extract_address (buf, sizeof (buf));
110 }
111
112 for (reps = 1; i + reps < length; reps++)
113 {
114 read_memory (address, buf, sizeof (buf));
115 address += TARGET_PTR_BIT / HOST_CHAR_BIT;
116 next_element = extract_address (buf, sizeof (buf));
117 if (next_element != element)
118 break;
119 }
120
121 if (reps == 1)
122 fprintf_filtered (stream, "%d: ", i);
123 else
124 fprintf_filtered (stream, "%d..%d: ", i, i + reps - 1);
125
126 if (element == 0)
127 fprintf_filtered (stream, "null");
128 else
129 fprintf_filtered (stream, "@%s", paddr_nz (element));
130
131 things_printed++;
132 i += reps;
133 }
134 }
135 else
136 {
137 value_ptr v = allocate_value (el_type);
138 value_ptr next_v = allocate_value (el_type);
139
140 VALUE_ADDRESS (v) = address + JAVA_OBJECT_SIZE + 4;
141 VALUE_ADDRESS (next_v) = VALUE_ADDRESS (v);
142
143 while (i < length && things_printed < print_max)
144 {
145 fputs_filtered (", ", stream);
146 wrap_here (n_spaces (2));
147
148 if (i > 0)
149 {
150 value_ptr tmp;
151
152 tmp = next_v;
153 next_v = v;
154 v = tmp;
155 }
156 else
157 {
158 VALUE_LAZY (v) = 1;
159 VALUE_OFFSET (v) = 0;
160 }
161
162 VALUE_OFFSET (next_v) = VALUE_OFFSET (v);
163
164 for (reps = 1; i + reps < length; reps++)
165 {
166 VALUE_LAZY (next_v) = 1;
167 VALUE_OFFSET (next_v) += TYPE_LENGTH (el_type);
168 if (memcmp (VALUE_CONTENTS (v), VALUE_CONTENTS (next_v),
169 TYPE_LENGTH (el_type)) != 0)
170 break;
171 }
172
173 if (reps == 1)
174 fprintf_filtered (stream, "%d: ", i);
175 else
176 fprintf_filtered (stream, "%d..%d: ", i, i + reps - 1);
177
178 val_print (VALUE_TYPE (v), VALUE_CONTENTS (v), 0, 0,
179 stream, format, 2, 1, pretty);
180
181 things_printed++;
182 i += reps;
183 }
184 }
185
186 if (i < length)
187 fprintf_filtered (stream, "...");
188
189 fprintf_filtered (stream, "}");
190
191 return 0;
192 }
193
194 /* If it's type String, print it */
195
196 if (TYPE_CODE (type) == TYPE_CODE_PTR
197 && TYPE_TARGET_TYPE (type)
198 && TYPE_NAME (TYPE_TARGET_TYPE (type))
199 && strcmp (TYPE_NAME (TYPE_TARGET_TYPE (type)), "java.lang.String") == 0
200 && (format == 0 || format == 's')
201 && address != 0)
202 {
203 value_ptr data_val;
204 CORE_ADDR data;
205 value_ptr boffset_val;
206 unsigned long boffset;
207 value_ptr count_val;
208 unsigned long count;
209 value_ptr mark;
210
211 mark = value_mark (); /* Remember start of new values */
212
213 data_val = value_struct_elt (&val, NULL, "data", NULL, NULL);
214 data = value_as_pointer (data_val);
215
216 boffset_val = value_struct_elt (&val, NULL, "boffset", NULL, NULL);
217 boffset = value_as_pointer (boffset_val);
218
219 count_val = value_struct_elt (&val, NULL, "count", NULL, NULL);
220 count = value_as_pointer (count_val);
221
222 value_free_to_mark (mark); /* Release unnecessary values */
223
224 val_print_string (data + boffset, count, 2, stream);
225
226 return 0;
227 }
228
229 return (val_print (type, VALUE_CONTENTS (val), 0, address,
230 stream, format, 1, 0, pretty));
231 }
232
233 /* TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and PRETTY have the
234 same meanings as in cp_print_value and c_val_print.
235
236 DONT_PRINT is an array of baseclass types that we
237 should not print, or zero if called from top level. */
238
239 static void
240 java_print_value_fields (type, valaddr, address, stream,
241 format, recurse, pretty)
242 struct type *type;
243 char *valaddr;
244 CORE_ADDR address;
245 GDB_FILE *stream;
246 int format;
247 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 extern int inspect_it;
305 int fields_seen = 0;
306
307 for (i = n_baseclasses; i < len; i++)
308 {
309 /* If requested, skip printing of static fields. */
310 if (TYPE_FIELD_STATIC (type, i))
311 {
312 char *name = TYPE_FIELD_NAME (type, i);
313 if (!static_field_print)
314 continue;
315 if (name != NULL && strcmp (name, "class") == 0)
316 continue;
317 }
318 if (fields_seen)
319 fprintf_filtered (stream, ", ");
320 else if (n_baseclasses > 0)
321 {
322 if (pretty)
323 {
324 fprintf_filtered (stream, "\n");
325 print_spaces_filtered (2 + 2 * recurse, stream);
326 fputs_filtered ("members of ", stream);
327 fputs_filtered (type_name_no_tag (type), stream);
328 fputs_filtered (": ", stream);
329 }
330 }
331 fields_seen = 1;
332
333 if (pretty)
334 {
335 fprintf_filtered (stream, "\n");
336 print_spaces_filtered (2 + 2 * recurse, stream);
337 }
338 else
339 {
340 wrap_here (n_spaces (2 + 2 * recurse));
341 }
342 if (inspect_it)
343 {
344 if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
345 fputs_filtered ("\"( ptr \"", stream);
346 else
347 fputs_filtered ("\"( nodef \"", stream);
348 if (TYPE_FIELD_STATIC (type, i))
349 fputs_filtered ("static ", stream);
350 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
351 language_cplus,
352 DMGL_PARAMS | DMGL_ANSI);
353 fputs_filtered ("\" \"", stream);
354 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
355 language_cplus,
356 DMGL_PARAMS | DMGL_ANSI);
357 fputs_filtered ("\") \"", stream);
358 }
359 else
360 {
361 annotate_field_begin (TYPE_FIELD_TYPE (type, i));
362
363 if (TYPE_FIELD_STATIC (type, i))
364 fputs_filtered ("static ", stream);
365 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
366 language_cplus,
367 DMGL_PARAMS | DMGL_ANSI);
368 annotate_field_name_end ();
369 fputs_filtered (": ", stream);
370 annotate_field_value ();
371 }
372
373 if (!TYPE_FIELD_STATIC (type, i) && TYPE_FIELD_PACKED (type, i))
374 {
375 value_ptr v;
376
377 /* Bitfields require special handling, especially due to byte
378 order problems. */
379 if (TYPE_FIELD_IGNORE (type, i))
380 {
381 fputs_filtered ("<optimized out or zero length>", stream);
382 }
383 else
384 {
385 v = value_from_longest (TYPE_FIELD_TYPE (type, i),
386 unpack_field_as_long (type, valaddr, i));
387
388 val_print (TYPE_FIELD_TYPE (type, i), VALUE_CONTENTS (v), 0,
389 0, stream, format, 0, recurse + 1, pretty);
390 }
391 }
392 else
393 {
394 if (TYPE_FIELD_IGNORE (type, i))
395 {
396 fputs_filtered ("<optimized out or zero length>", stream);
397 }
398 else if (TYPE_FIELD_STATIC (type, i))
399 {
400 value_ptr v = value_static_field (type, i);
401 if (v == NULL)
402 fputs_filtered ("<optimized out>", stream);
403 else
404 {
405 struct type *t = check_typedef (VALUE_TYPE (v));
406 if (TYPE_CODE (t) == TYPE_CODE_STRUCT)
407 v = value_addr (v);
408 val_print (VALUE_TYPE (v),
409 VALUE_CONTENTS (v), 0, VALUE_ADDRESS (v),
410 stream, format, 0, recurse + 1, pretty);
411 }
412 }
413 else if (TYPE_FIELD_TYPE (type, i) == NULL)
414 fputs_filtered ("<unknown type>", stream);
415 else
416 {
417 val_print (TYPE_FIELD_TYPE (type, i),
418 valaddr + TYPE_FIELD_BITPOS (type, i) / 8, 0,
419 address + TYPE_FIELD_BITPOS (type, i) / 8,
420 stream, format, 0, recurse + 1, pretty);
421 }
422 }
423 annotate_field_end ();
424 }
425
426 if (pretty)
427 {
428 fprintf_filtered (stream, "\n");
429 print_spaces_filtered (2 * recurse, stream);
430 }
431 }
432 fprintf_filtered (stream, "}");
433 }
434
435 /* Print data of type TYPE located at VALADDR (within GDB), which came from
436 the inferior at address ADDRESS, onto stdio stream STREAM according to
437 FORMAT (a letter or 0 for natural format). The data at VALADDR is in
438 target byte order.
439
440 If the data are a string pointer, returns the number of string characters
441 printed.
442
443 If DEREF_REF is nonzero, then dereference references, otherwise just print
444 them like pointers.
445
446 The PRETTY parameter controls prettyprinting. */
447
448 int
449 java_val_print (type, valaddr, embedded_offset, address, stream, format,
450 deref_ref, recurse, pretty)
451 struct type *type;
452 char *valaddr;
453 CORE_ADDR address;
454 GDB_FILE *stream;
455 int format;
456 int deref_ref;
457 int recurse;
458 enum val_prettyprint pretty;
459 {
460 register unsigned int i = 0; /* Number of characters printed */
461 struct type *target_type;
462 CORE_ADDR addr;
463
464 CHECK_TYPEDEF (type);
465 switch (TYPE_CODE (type))
466 {
467 case TYPE_CODE_PTR:
468 if (format && format != 's')
469 {
470 print_scalar_formatted (valaddr, type, format, 0, stream);
471 break;
472 }
473 #if 0
474 if (vtblprint && cp_is_vtbl_ptr_type (type))
475 {
476 /* Print the unmangled name if desired. */
477 /* Print vtable entry - we only get here if we ARE using
478 -fvtable_thunks. (Otherwise, look under TYPE_CODE_STRUCT.) */
479 print_address_demangle (extract_address (valaddr, TYPE_LENGTH (type)),
480 stream, demangle);
481 break;
482 }
483 #endif
484 addr = unpack_pointer (type, valaddr);
485 if (addr == 0)
486 {
487 fputs_filtered ("null", stream);
488 return i;
489 }
490 target_type = check_typedef (TYPE_TARGET_TYPE (type));
491
492 if (TYPE_CODE (target_type) == TYPE_CODE_FUNC)
493 {
494 /* Try to print what function it points to. */
495 print_address_demangle (addr, stream, demangle);
496 /* Return value is irrelevant except for string pointers. */
497 return (0);
498 }
499
500 if (addressprint && format != 's')
501 {
502 fputs_filtered ("@", stream);
503 print_longest (stream, 'x', 0, (ULONGEST) addr);
504 }
505
506 return i;
507
508 case TYPE_CODE_CHAR:
509 format = format ? format : output_format;
510 if (format)
511 print_scalar_formatted (valaddr, type, format, 0, stream);
512 else
513 LA_PRINT_CHAR ((int) unpack_long (type, valaddr), stream);
514 break;
515
516 case TYPE_CODE_INT:
517 /* Can't just call c_val_print because that print bytes as C chars. */
518 format = format ? format : output_format;
519 if (format)
520 print_scalar_formatted (valaddr, type, format, 0, stream);
521 else
522 val_print_type_code_int (type, valaddr, stream);
523 break;
524
525 case TYPE_CODE_STRUCT:
526 java_print_value_fields (type, valaddr, address, stream, format,
527 recurse, pretty);
528 break;
529
530 default:
531 return c_val_print (type, valaddr, embedded_offset, address, stream,
532 format, deref_ref, recurse, pretty);
533 }
534
535 return 0;
536 }