OSDN Git Service

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