Upgrade GDB from 7.3 to 7.4.1 on the vendor branch
[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-2005, 2007-2012 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #include "defs.h"
21 #include "symtab.h"
22 #include "gdbtypes.h"
23 #include "gdbcore.h"
24 #include "expression.h"
25 #include "value.h"
26 #include "demangle.h"
27 #include "valprint.h"
28 #include "language.h"
29 #include "jv-lang.h"
30 #include "c-lang.h"
31 #include "annotate.h"
32 #include "gdb_string.h"
33
34 /* Local functions */
35
36 int
37 java_value_print (struct value *val, struct ui_file *stream, 
38                   const struct value_print_options *options)
39 {
40   struct gdbarch *gdbarch = get_type_arch (value_type (val));
41   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
42   struct type *type;
43   CORE_ADDR address;
44   int i;
45   char *name;
46   struct value_print_options opts;
47
48   type = value_type (val);
49   address = value_address (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 (gdbarch, 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
80         = java_primitive_type_from_name (gdbarch, name, i - 2);
81
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)
180                                     + TYPE_LENGTH (el_type));
181                   value_fetch_lazy (next_v);
182                   if (!(value_available_contents_eq
183                         (v, value_embedded_offset (v),
184                          next_v, value_embedded_offset (next_v),
185                          TYPE_LENGTH (el_type))))
186                     break;
187                 }
188
189               if (reps == 1)
190                 fprintf_filtered (stream, "%d: ", i);
191               else
192                 fprintf_filtered (stream, "%d..%d: ", i, i + reps - 1);
193
194               opts = *options;
195               opts.deref_ref = 1;
196               common_val_print (v, stream, 1, &opts, current_language);
197
198               things_printed++;
199               i += reps;
200             }
201         }
202
203       if (i < length)
204         fprintf_filtered (stream, "...");
205
206       fprintf_filtered (stream, "}");
207
208       return 0;
209     }
210
211   /* If it's type String, print it.  */
212
213   if (TYPE_CODE (type) == TYPE_CODE_PTR
214       && TYPE_TARGET_TYPE (type)
215       && TYPE_TAG_NAME (TYPE_TARGET_TYPE (type))
216       && strcmp (TYPE_TAG_NAME (TYPE_TARGET_TYPE (type)),
217                  "java.lang.String") == 0
218       && (options->format == 0 || options->format == 's')
219       && address != 0
220       && value_as_address (val) != 0)
221     {
222       struct type *char_type;
223       struct value *data_val;
224       CORE_ADDR data;
225       struct value *boffset_val;
226       unsigned long boffset;
227       struct value *count_val;
228       unsigned long count;
229       struct value *mark;
230
231       mark = value_mark ();     /* Remember start of new values.  */
232
233       data_val = value_struct_elt (&val, NULL, "data", NULL, NULL);
234       data = value_as_address (data_val);
235
236       boffset_val = value_struct_elt (&val, NULL, "boffset", NULL, NULL);
237       boffset = value_as_address (boffset_val);
238
239       count_val = value_struct_elt (&val, NULL, "count", NULL, NULL);
240       count = value_as_address (count_val);
241
242       value_free_to_mark (mark);        /* Release unnecessary values.  */
243
244       char_type = builtin_java_type (gdbarch)->builtin_char;
245       val_print_string (char_type, NULL, data + boffset, count, stream,
246                         options);
247
248       return 0;
249     }
250
251   opts = *options;
252   opts.deref_ref = 1;
253   return common_val_print (val, stream, 0, &opts, current_language);
254 }
255
256 /* TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and OPTIONS have the
257    same meanings as in cp_print_value and c_val_print.
258
259    DONT_PRINT is an array of baseclass types that we
260    should not print, or zero if called from top level.  */
261
262 static void
263 java_print_value_fields (struct type *type, const gdb_byte *valaddr,
264                          int offset,
265                          CORE_ADDR address, struct ui_file *stream,
266                          int recurse,
267                          const struct value *val,
268                          const struct value_print_options *options)
269 {
270   int i, len, n_baseclasses;
271
272   CHECK_TYPEDEF (type);
273
274   fprintf_filtered (stream, "{");
275   len = TYPE_NFIELDS (type);
276   n_baseclasses = TYPE_N_BASECLASSES (type);
277
278   if (n_baseclasses > 0)
279     {
280       int i, n_baseclasses = TYPE_N_BASECLASSES (type);
281
282       for (i = 0; i < n_baseclasses; i++)
283         {
284           int boffset;
285           struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
286           char *basename = TYPE_NAME (baseclass);
287           const gdb_byte *base_valaddr;
288
289           if (BASETYPE_VIA_VIRTUAL (type, i))
290             continue;
291
292           if (basename != NULL && strcmp (basename, "java.lang.Object") == 0)
293             continue;
294
295           boffset = 0;
296
297           if (options->pretty)
298             {
299               fprintf_filtered (stream, "\n");
300               print_spaces_filtered (2 * (recurse + 1), stream);
301             }
302           fputs_filtered ("<", stream);
303           /* Not sure what the best notation is in the case where there is no
304              baseclass name.  */
305           fputs_filtered (basename ? basename : "", stream);
306           fputs_filtered ("> = ", stream);
307
308           base_valaddr = valaddr;
309
310           java_print_value_fields (baseclass, base_valaddr,
311                                    offset + boffset, address,
312                                    stream, recurse + 1, val, options);
313           fputs_filtered (", ", stream);
314         }
315     }
316
317   if (!len && n_baseclasses == 1)
318     fprintf_filtered (stream, "<No data fields>");
319   else
320     {
321       int fields_seen = 0;
322
323       for (i = n_baseclasses; i < len; i++)
324         {
325           /* If requested, skip printing of static fields.  */
326           if (field_is_static (&TYPE_FIELD (type, i)))
327             {
328               char *name = TYPE_FIELD_NAME (type, i);
329
330               if (!options->static_field_print)
331                 continue;
332               if (name != NULL && strcmp (name, "class") == 0)
333                 continue;
334             }
335           if (fields_seen)
336             fprintf_filtered (stream, ", ");
337           else if (n_baseclasses > 0)
338             {
339               if (options->pretty)
340                 {
341                   fprintf_filtered (stream, "\n");
342                   print_spaces_filtered (2 + 2 * recurse, stream);
343                   fputs_filtered ("members of ", stream);
344                   fputs_filtered (type_name_no_tag (type), stream);
345                   fputs_filtered (": ", stream);
346                 }
347             }
348           fields_seen = 1;
349
350           if (options->pretty)
351             {
352               fprintf_filtered (stream, "\n");
353               print_spaces_filtered (2 + 2 * recurse, stream);
354             }
355           else
356             {
357               wrap_here (n_spaces (2 + 2 * recurse));
358             }
359           if (options->inspect_it)
360             {
361               if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
362                 fputs_filtered ("\"( ptr \"", stream);
363               else
364                 fputs_filtered ("\"( nodef \"", stream);
365               if (field_is_static (&TYPE_FIELD (type, i)))
366                 fputs_filtered ("static ", stream);
367               fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
368                                        language_cplus,
369                                        DMGL_PARAMS | DMGL_ANSI);
370               fputs_filtered ("\" \"", stream);
371               fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
372                                        language_cplus,
373                                        DMGL_PARAMS | DMGL_ANSI);
374               fputs_filtered ("\") \"", stream);
375             }
376           else
377             {
378               annotate_field_begin (TYPE_FIELD_TYPE (type, i));
379
380               if (field_is_static (&TYPE_FIELD (type, i)))
381                 fputs_filtered ("static ", stream);
382               fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
383                                        language_cplus,
384                                        DMGL_PARAMS | DMGL_ANSI);
385               annotate_field_name_end ();
386               fputs_filtered (": ", stream);
387               annotate_field_value ();
388             }
389
390           if (!field_is_static (&TYPE_FIELD (type, i))
391               && TYPE_FIELD_PACKED (type, i))
392             {
393               struct value *v;
394
395               /* Bitfields require special handling, especially due to byte
396                  order problems.  */
397               if (TYPE_FIELD_IGNORE (type, i))
398                 {
399                   fputs_filtered ("<optimized out or zero length>", stream);
400                 }
401               else if (value_bits_synthetic_pointer (val,
402                                                      TYPE_FIELD_BITPOS (type,
403                                                                         i),
404                                                      TYPE_FIELD_BITSIZE (type,
405                                                                          i)))
406                 {
407                   fputs_filtered (_("<synthetic pointer>"), stream);
408                 }
409               else if (!value_bits_valid (val, TYPE_FIELD_BITPOS (type, i),
410                                           TYPE_FIELD_BITSIZE (type, i)))
411                 {
412                   val_print_optimized_out (stream);
413                 }
414               else
415                 {
416                   struct value_print_options opts;
417
418                   v = value_field_bitfield (type, i, valaddr, offset, val);
419
420                   opts = *options;
421                   opts.deref_ref = 0;
422                   common_val_print (v, stream, recurse + 1,
423                                     &opts, current_language);
424                 }
425             }
426           else
427             {
428               if (TYPE_FIELD_IGNORE (type, i))
429                 {
430                   fputs_filtered ("<optimized out or zero length>", stream);
431                 }
432               else if (field_is_static (&TYPE_FIELD (type, i)))
433                 {
434                   struct value *v = value_static_field (type, i);
435
436                   if (v == NULL)
437                     val_print_optimized_out (stream);
438                   else
439                     {
440                       struct value_print_options opts;
441                       struct type *t = check_typedef (value_type (v));
442
443                       if (TYPE_CODE (t) == TYPE_CODE_STRUCT)
444                         v = value_addr (v);
445                       opts = *options;
446                       opts.deref_ref = 0;
447                       common_val_print (v, stream, recurse + 1,
448                                         &opts, current_language);
449                     }
450                 }
451               else if (TYPE_FIELD_TYPE (type, i) == NULL)
452                 fputs_filtered ("<unknown type>", stream);
453               else
454                 {
455                   struct value_print_options opts = *options;
456
457                   opts.deref_ref = 0;
458                   val_print (TYPE_FIELD_TYPE (type, i),
459                              valaddr,
460                              offset + TYPE_FIELD_BITPOS (type, i) / 8,
461                              address, stream, recurse + 1, val, &opts,
462                              current_language);
463                 }
464             }
465           annotate_field_end ();
466         }
467
468       if (options->pretty)
469         {
470           fprintf_filtered (stream, "\n");
471           print_spaces_filtered (2 * recurse, stream);
472         }
473     }
474   fprintf_filtered (stream, "}");
475 }
476
477 /* See val_print for a description of the various parameters of this
478    function; they are identical.  The semantics of the return value is
479    also identical to val_print.  */
480
481 int
482 java_val_print (struct type *type, const gdb_byte *valaddr,
483                 int embedded_offset, CORE_ADDR address,
484                 struct ui_file *stream, int recurse,
485                 const struct value *val,
486                 const struct value_print_options *options)
487 {
488   struct gdbarch *gdbarch = get_type_arch (type);
489   unsigned int i = 0;   /* Number of characters printed.  */
490   struct type *target_type;
491   CORE_ADDR addr;
492
493   CHECK_TYPEDEF (type);
494   switch (TYPE_CODE (type))
495     {
496     case TYPE_CODE_PTR:
497       if (options->format && options->format != 's')
498         {
499           val_print_scalar_formatted (type, valaddr, embedded_offset,
500                                       val, options, 0, stream);
501           break;
502         }
503 #if 0
504       if (options->vtblprint && cp_is_vtbl_ptr_type (type))
505         {
506           /* Print the unmangled name if desired.  */
507           /* Print vtable entry - we only get here if we ARE using
508              -fvtable_thunks.  (Otherwise, look under TYPE_CODE_STRUCT.)  */
509           /* Extract an address, assume that it is unsigned.  */
510           print_address_demangle
511             (gdbarch,
512              extract_unsigned_integer (valaddr + embedded_offset,
513                                        TYPE_LENGTH (type)),
514              stream, demangle);
515           break;
516         }
517 #endif
518       addr = unpack_pointer (type, valaddr + embedded_offset);
519       if (addr == 0)
520         {
521           fputs_filtered ("null", stream);
522           return i;
523         }
524       target_type = check_typedef (TYPE_TARGET_TYPE (type));
525
526       if (TYPE_CODE (target_type) == TYPE_CODE_FUNC)
527         {
528           /* Try to print what function it points to.  */
529           print_address_demangle (gdbarch, addr, stream, demangle);
530           /* Return value is irrelevant except for string pointers.  */
531           return (0);
532         }
533
534       if (options->addressprint && options->format != 's')
535         {
536           fputs_filtered ("@", stream);
537           print_longest (stream, 'x', 0, (ULONGEST) addr);
538         }
539
540       return i;
541
542     case TYPE_CODE_CHAR:
543     case TYPE_CODE_INT:
544       /* Can't just call c_val_print because that prints bytes as C
545          chars.  */
546       if (options->format || options->output_format)
547         {
548           struct value_print_options opts = *options;
549
550           opts.format = (options->format ? options->format
551                          : options->output_format);
552           val_print_scalar_formatted (type, valaddr, embedded_offset,
553                                       val, &opts, 0, stream);
554         }
555       else if (TYPE_CODE (type) == TYPE_CODE_CHAR
556                || (TYPE_CODE (type) == TYPE_CODE_INT
557                    && TYPE_LENGTH (type) == 2
558                    && strcmp (TYPE_NAME (type), "char") == 0))
559         LA_PRINT_CHAR ((int) unpack_long (type, valaddr + embedded_offset),
560                        type, stream);
561       else
562         val_print_type_code_int (type, valaddr + embedded_offset, stream);
563       break;
564
565     case TYPE_CODE_STRUCT:
566       java_print_value_fields (type, valaddr, embedded_offset,
567                                address, stream, recurse, val, options);
568       break;
569
570     default:
571       return c_val_print (type, valaddr, embedded_offset, address, stream,
572                           recurse, val, options);
573     }
574
575   return 0;
576 }