]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/jv-valprint.c
Copyright updates for 2007.
[thirdparty/binutils-gdb.git] / gdb / jv-valprint.c
1 /* Support for printing Java values for GDB, the GNU debugger.
2
3 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007
4 Free Software Foundation, Inc.
5
6 This file is part of GDB.
7
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
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
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.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
22
23 #include "defs.h"
24 #include "symtab.h"
25 #include "gdbtypes.h"
26 #include "gdbcore.h"
27 #include "expression.h"
28 #include "value.h"
29 #include "demangle.h"
30 #include "valprint.h"
31 #include "language.h"
32 #include "jv-lang.h"
33 #include "c-lang.h"
34 #include "annotate.h"
35 #include "gdb_string.h"
36
37 /* Local functions */
38
39 int
40 java_value_print (struct value *val, struct ui_file *stream, int format,
41 enum val_prettyprint pretty)
42 {
43 struct type *type;
44 CORE_ADDR address;
45 int i;
46 char *name;
47
48 type = value_type (val);
49 address = VALUE_ADDRESS (val) + value_offset (val);
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
57 obj_addr = unpack_pointer (type, value_contents (val));
58
59 if (obj_addr != 0)
60 {
61 type = type_from_class (java_class_from_object (val));
62 type = lookup_pointer_type (type);
63
64 val = value_at (type, address);
65 }
66 }
67
68 if (TYPE_CODE (type) == TYPE_CODE_PTR && !value_logical_not (val))
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
73 && (i = strlen (name), name[i - 1] == ']'))
74 {
75 gdb_byte buf4[4];
76 long length;
77 unsigned int things_printed = 0;
78 int reps;
79 struct type *el_type = java_primitive_type_from_name (name, i - 2);
80
81 i = 0;
82 read_memory (address + JAVA_OBJECT_SIZE, buf4, 4);
83
84 length = (long) extract_signed_integer (buf4, 4);
85 fprintf_filtered (stream, "{length: %ld", length);
86
87 if (el_type == NULL)
88 {
89 CORE_ADDR element;
90 CORE_ADDR next_element = -1; /* dummy initial value */
91
92 address += JAVA_OBJECT_SIZE + 4; /* Skip object header and length. */
93
94 while (i < length && things_printed < print_max)
95 {
96 gdb_byte *buf;
97
98 buf = alloca (TARGET_PTR_BIT / HOST_CHAR_BIT);
99 fputs_filtered (", ", stream);
100 wrap_here (n_spaces (2));
101
102 if (i > 0)
103 element = next_element;
104 else
105 {
106 read_memory (address, buf, sizeof (buf));
107 address += TARGET_PTR_BIT / HOST_CHAR_BIT;
108 /* FIXME: cagney/2003-05-24: Bogus or what. It
109 pulls a host sized pointer out of the target and
110 then extracts that as an address (while assuming
111 that the address is unsigned)! */
112 element = extract_unsigned_integer (buf, sizeof (buf));
113 }
114
115 for (reps = 1; i + reps < length; reps++)
116 {
117 read_memory (address, buf, sizeof (buf));
118 address += TARGET_PTR_BIT / HOST_CHAR_BIT;
119 /* FIXME: cagney/2003-05-24: Bogus or what. It
120 pulls a host sized pointer out of the target and
121 then extracts that as an address (while assuming
122 that the address is unsigned)! */
123 next_element = extract_unsigned_integer (buf, sizeof (buf));
124 if (next_element != element)
125 break;
126 }
127
128 if (reps == 1)
129 fprintf_filtered (stream, "%d: ", i);
130 else
131 fprintf_filtered (stream, "%d..%d: ", i, i + reps - 1);
132
133 if (element == 0)
134 fprintf_filtered (stream, "null");
135 else
136 fprintf_filtered (stream, "@%s", paddr_nz (element));
137
138 things_printed++;
139 i += reps;
140 }
141 }
142 else
143 {
144 struct value *v = allocate_value (el_type);
145 struct value *next_v = allocate_value (el_type);
146
147 VALUE_ADDRESS (v) = address + JAVA_OBJECT_SIZE + 4;
148 VALUE_ADDRESS (next_v) = VALUE_ADDRESS (v);
149
150 while (i < length && things_printed < print_max)
151 {
152 fputs_filtered (", ", stream);
153 wrap_here (n_spaces (2));
154
155 if (i > 0)
156 {
157 struct value *tmp;
158
159 tmp = next_v;
160 next_v = v;
161 v = tmp;
162 }
163 else
164 {
165 set_value_lazy (v, 1);
166 set_value_offset (v, 0);
167 }
168
169 set_value_offset (next_v, value_offset (v));
170
171 for (reps = 1; i + reps < length; reps++)
172 {
173 set_value_lazy (next_v, 1);
174 set_value_offset (next_v, value_offset (next_v) + TYPE_LENGTH (el_type));
175 if (memcmp (value_contents (v), value_contents (next_v),
176 TYPE_LENGTH (el_type)) != 0)
177 break;
178 }
179
180 if (reps == 1)
181 fprintf_filtered (stream, "%d: ", i);
182 else
183 fprintf_filtered (stream, "%d..%d: ", i, i + reps - 1);
184
185 common_val_print (v, stream, format, 2, 1, pretty);
186
187 things_printed++;
188 i += reps;
189 }
190 }
191
192 if (i < length)
193 fprintf_filtered (stream, "...");
194
195 fprintf_filtered (stream, "}");
196
197 return 0;
198 }
199
200 /* If it's type String, print it */
201
202 if (TYPE_CODE (type) == TYPE_CODE_PTR
203 && TYPE_TARGET_TYPE (type)
204 && TYPE_TAG_NAME (TYPE_TARGET_TYPE (type))
205 && strcmp (TYPE_TAG_NAME (TYPE_TARGET_TYPE (type)),
206 "java.lang.String") == 0
207 && (format == 0 || format == 's')
208 && address != 0
209 && value_as_address (val) != 0)
210 {
211 struct value *data_val;
212 CORE_ADDR data;
213 struct value *boffset_val;
214 unsigned long boffset;
215 struct value *count_val;
216 unsigned long count;
217 struct value *mark;
218
219 mark = value_mark (); /* Remember start of new values */
220
221 data_val = value_struct_elt (&val, NULL, "data", NULL, NULL);
222 data = value_as_address (data_val);
223
224 boffset_val = value_struct_elt (&val, NULL, "boffset", NULL, NULL);
225 boffset = value_as_address (boffset_val);
226
227 count_val = value_struct_elt (&val, NULL, "count", NULL, NULL);
228 count = value_as_address (count_val);
229
230 value_free_to_mark (mark); /* Release unnecessary values */
231
232 val_print_string (data + boffset, count, 2, stream);
233
234 return 0;
235 }
236
237 return common_val_print (val, stream, format, 1, 0, pretty);
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
246 static void
247 java_print_value_fields (struct type *type, const gdb_byte *valaddr,
248 CORE_ADDR address, struct ui_file *stream,
249 int format, int recurse, enum val_prettyprint pretty)
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);
268 const gdb_byte *base_valaddr;
269
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");
281 print_spaces_filtered (2 * (recurse + 1), stream);
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,
292 stream, format, recurse + 1, pretty);
293 fputs_filtered (", ", stream);
294 }
295
296 }
297
298 if (!len && n_baseclasses == 1)
299 fprintf_filtered (stream, "<No data fields>");
300 else
301 {
302 int fields_seen = 0;
303
304 for (i = n_baseclasses; i < len; i++)
305 {
306 /* If requested, skip printing of static fields. */
307 if (TYPE_FIELD_STATIC (type, i))
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 }
335 else
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);
345 if (TYPE_FIELD_STATIC (type, i))
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
360 if (TYPE_FIELD_STATIC (type, i))
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
370 if (!TYPE_FIELD_STATIC (type, i) && TYPE_FIELD_PACKED (type, i))
371 {
372 struct value *v;
373
374 /* Bitfields require special handling, especially due to byte
375 order problems. */
376 if (TYPE_FIELD_IGNORE (type, i))
377 {
378 fputs_filtered ("<optimized out or zero length>", stream);
379 }
380 else
381 {
382 v = value_from_longest (TYPE_FIELD_TYPE (type, i),
383 unpack_field_as_long (type, valaddr, i));
384
385 common_val_print (v, stream, format, 0, recurse + 1, pretty);
386 }
387 }
388 else
389 {
390 if (TYPE_FIELD_IGNORE (type, i))
391 {
392 fputs_filtered ("<optimized out or zero length>", stream);
393 }
394 else if (TYPE_FIELD_STATIC (type, i))
395 {
396 struct value *v = value_static_field (type, i);
397 if (v == NULL)
398 fputs_filtered ("<optimized out>", stream);
399 else
400 {
401 struct type *t = check_typedef (value_type (v));
402 if (TYPE_CODE (t) == TYPE_CODE_STRUCT)
403 v = value_addr (v);
404 common_val_print (v, stream, format, 0, recurse + 1,
405 pretty);
406 }
407 }
408 else if (TYPE_FIELD_TYPE (type, i) == NULL)
409 fputs_filtered ("<unknown type>", stream);
410 else
411 {
412 val_print (TYPE_FIELD_TYPE (type, i),
413 valaddr + TYPE_FIELD_BITPOS (type, i) / 8, 0,
414 address + TYPE_FIELD_BITPOS (type, i) / 8,
415 stream, format, 0, recurse + 1, pretty);
416 }
417 }
418 annotate_field_end ();
419 }
420
421 if (pretty)
422 {
423 fprintf_filtered (stream, "\n");
424 print_spaces_filtered (2 * recurse, stream);
425 }
426 }
427 fprintf_filtered (stream, "}");
428 }
429
430 /* Print data of type TYPE located at VALADDR (within GDB), which came from
431 the inferior at address ADDRESS, onto stdio stream STREAM according to
432 FORMAT (a letter or 0 for natural format). The data at VALADDR is in
433 target byte order.
434
435 If the data are a string pointer, returns the number of string characters
436 printed.
437
438 If DEREF_REF is nonzero, then dereference references, otherwise just print
439 them like pointers.
440
441 The PRETTY parameter controls prettyprinting. */
442
443 int
444 java_val_print (struct type *type, const gdb_byte *valaddr,
445 int embedded_offset, CORE_ADDR address,
446 struct ui_file *stream, int format, int deref_ref,
447 int recurse, enum val_prettyprint pretty)
448 {
449 unsigned int i = 0; /* Number of characters printed */
450 struct type *target_type;
451 CORE_ADDR addr;
452
453 CHECK_TYPEDEF (type);
454 switch (TYPE_CODE (type))
455 {
456 case TYPE_CODE_PTR:
457 if (format && format != 's')
458 {
459 print_scalar_formatted (valaddr, type, format, 0, stream);
460 break;
461 }
462 #if 0
463 if (vtblprint && cp_is_vtbl_ptr_type (type))
464 {
465 /* Print the unmangled name if desired. */
466 /* Print vtable entry - we only get here if we ARE using
467 -fvtable_thunks. (Otherwise, look under TYPE_CODE_STRUCT.) */
468 /* Extract an address, assume that it is unsigned. */
469 print_address_demangle (extract_unsigned_integer (valaddr, TYPE_LENGTH (type)),
470 stream, demangle);
471 break;
472 }
473 #endif
474 addr = unpack_pointer (type, valaddr);
475 if (addr == 0)
476 {
477 fputs_filtered ("null", stream);
478 return i;
479 }
480 target_type = check_typedef (TYPE_TARGET_TYPE (type));
481
482 if (TYPE_CODE (target_type) == TYPE_CODE_FUNC)
483 {
484 /* Try to print what function it points to. */
485 print_address_demangle (addr, stream, demangle);
486 /* Return value is irrelevant except for string pointers. */
487 return (0);
488 }
489
490 if (addressprint && format != 's')
491 {
492 fputs_filtered ("@", stream);
493 print_longest (stream, 'x', 0, (ULONGEST) addr);
494 }
495
496 return i;
497
498 case TYPE_CODE_CHAR:
499 case TYPE_CODE_INT:
500 /* Can't just call c_val_print because that prints bytes as C
501 chars. */
502 format = format ? format : output_format;
503 if (format)
504 print_scalar_formatted (valaddr, type, format, 0, stream);
505 else if (TYPE_CODE (type) == TYPE_CODE_CHAR
506 || (TYPE_CODE (type) == TYPE_CODE_INT
507 && TYPE_LENGTH (type) == 2
508 && strcmp (TYPE_NAME (type), "char") == 0))
509 LA_PRINT_CHAR ((int) unpack_long (type, valaddr), stream);
510 else
511 val_print_type_code_int (type, valaddr, stream);
512 break;
513
514 case TYPE_CODE_STRUCT:
515 java_print_value_fields (type, valaddr, address, stream, format,
516 recurse, pretty);
517 break;
518
519 default:
520 return c_val_print (type, valaddr, embedded_offset, address, stream,
521 format, deref_ref, recurse, pretty);
522 }
523
524 return 0;
525 }