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