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