This commit was generated by cvs2svn to track changes on a CVS vendor
[platform/upstream/binutils.git] / gdb / jv-valprint.c
1 /* Support for printing Java values for GDB, the GNU debugger.
2    Copyright 1997, 1998, 1999 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
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
33 int
34 java_value_print (val, stream, format, pretty)
35      value_ptr val;
36      GDB_FILE *stream;
37      int format;
38      enum val_prettyprint pretty;
39 {
40   struct type *type;
41   CORE_ADDR address;
42   int i;
43   char *name;
44
45   type = VALUE_TYPE (val);
46   address = VALUE_ADDRESS (val) + VALUE_OFFSET (val);
47
48   if (is_object_type (type))
49     {
50       CORE_ADDR obj_addr;
51
52       /* Get the run-time type, and cast the object into that */
53
54       obj_addr = unpack_pointer (type, VALUE_CONTENTS (val));
55
56       if (obj_addr != 0)
57         {
58           type = type_from_class (java_class_from_object (val));
59           type = lookup_pointer_type (type);
60
61           val = value_at (type, address, NULL);
62         }
63     }
64
65   if (TYPE_CODE (type) == TYPE_CODE_PTR && ! value_logical_not (val))
66     type_print (TYPE_TARGET_TYPE (type), "", stream, -1);
67
68   name = TYPE_TAG_NAME (type);
69   if (TYPE_CODE (type) == TYPE_CODE_STRUCT && name != NULL
70       && (i = strlen (name), name[i-1] == ']'))
71     {
72       char buf4[4];
73       long length;
74       unsigned int things_printed = 0;
75       int reps; 
76       struct type *el_type = java_primitive_type_from_name (name, i - 2);
77
78       i = 0;
79       read_memory (address + JAVA_OBJECT_SIZE, buf4, 4);
80
81       length = (long) extract_signed_integer (buf4, 4);
82       fprintf_filtered (stream, "{length: %ld", length);
83
84       if (el_type == NULL)
85         {
86           CORE_ADDR element, next_element;
87
88           address += JAVA_OBJECT_SIZE + 4; /* Skip object header and length. */
89
90           while (i < length && things_printed < print_max)
91             {
92               char buf[TARGET_PTR_BIT / HOST_CHAR_BIT];
93
94               fputs_filtered (", ", stream);
95               wrap_here (n_spaces (2));
96
97               if (i > 0)
98                 element = next_element;
99               else
100                 {
101                   read_memory (address, buf, sizeof(buf));
102                   address += TARGET_PTR_BIT / HOST_CHAR_BIT;
103                   element = extract_address (buf, sizeof(buf));
104                 }
105
106               for (reps = 1;  i + reps < length;  reps++)
107                 {
108                   read_memory (address, buf, sizeof(buf));
109                   address += TARGET_PTR_BIT / HOST_CHAR_BIT;
110                   next_element = extract_address (buf, sizeof(buf));
111                   if (next_element != element)
112                     break;
113                 }
114
115               if (reps == 1)
116                 fprintf_filtered (stream, "%d: ", i);
117               else
118                 fprintf_filtered (stream, "%d..%d: ", i, i + reps - 1);
119
120               if (element == 0)
121                 fprintf_filtered (stream, "null");
122               else
123                 fprintf_filtered (stream, "@%x", element);
124
125               things_printed++;
126               i += reps;
127             }
128         }
129       else
130         {
131           value_ptr v = allocate_value (el_type);
132           value_ptr next_v = allocate_value (el_type);
133
134           VALUE_ADDRESS (v) = address + JAVA_OBJECT_SIZE + 4;
135           VALUE_ADDRESS (next_v) = VALUE_ADDRESS (v);
136
137           while (i < length && things_printed < print_max)
138             {
139               fputs_filtered (", ", stream);
140               wrap_here (n_spaces (2));
141
142               if (i > 0)
143                 {
144                   value_ptr tmp;
145
146                   tmp = next_v;
147                   next_v = v;
148                   v = tmp;
149                 }
150               else
151                 {
152                   VALUE_LAZY (v) = 1;
153                   VALUE_OFFSET (v) = 0;
154                 }
155
156               VALUE_OFFSET (next_v) = VALUE_OFFSET (v);
157
158               for (reps = 1;  i + reps < length;  reps++)
159                 {
160                   VALUE_LAZY (next_v) = 1;
161                   VALUE_OFFSET (next_v) += TYPE_LENGTH (el_type);
162                   if (memcmp (VALUE_CONTENTS (v), VALUE_CONTENTS (next_v),
163                               TYPE_LENGTH (el_type)) != 0)
164                     break;
165                 }
166
167               if (reps == 1)
168                 fprintf_filtered (stream, "%d: ", i);
169               else
170                 fprintf_filtered (stream, "%d..%d: ", i, i + reps - 1);
171
172               val_print (VALUE_TYPE (v), VALUE_CONTENTS (v), 0, 0,
173                          stream, format, 2, 1, pretty);
174
175               things_printed++;
176               i += reps;
177             }
178         }
179
180       if (i < length)
181         fprintf_filtered (stream, "...");
182
183       fprintf_filtered (stream, "}");
184
185       return 0;
186     }
187
188   /* If it's type String, print it */
189
190   if (TYPE_CODE (type) == TYPE_CODE_PTR
191       && TYPE_TARGET_TYPE (type)
192       && TYPE_NAME (TYPE_TARGET_TYPE (type))
193       && strcmp (TYPE_NAME (TYPE_TARGET_TYPE (type)), "java.lang.String") == 0
194       && (format == 0 || format == 's')
195       && address != 0)
196     {
197       value_ptr data_val;
198       CORE_ADDR data;
199       value_ptr boffset_val;
200       unsigned long boffset;
201       value_ptr count_val;
202       unsigned long count;
203       value_ptr mark;
204
205       mark = value_mark ();     /* Remember start of new values */
206
207       data_val = value_struct_elt (&val, NULL, "data", NULL, NULL);
208       data = value_as_pointer (data_val);
209
210       boffset_val = value_struct_elt (&val, NULL, "boffset", NULL, NULL);
211       boffset = value_as_pointer (boffset_val);
212
213       count_val = value_struct_elt (&val, NULL, "count", NULL, NULL);
214       count = value_as_pointer (count_val);
215
216       value_free_to_mark (mark); /* Release unnecessary values */
217
218       val_print_string (data + boffset, count, 2, stream);
219
220       return 0;
221     }
222
223   return (val_print (type, VALUE_CONTENTS (val), 0, address,
224                      stream, format, 1, 0, pretty));
225 }
226
227 /* TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and PRETTY have the
228    same meanings as in cp_print_value and c_val_print.
229
230    DONT_PRINT is an array of baseclass types that we
231    should not print, or zero if called from top level.  */
232
233 void
234 java_print_value_fields (type, valaddr, address, stream,
235                          format, recurse, pretty)
236      struct type *type;
237      char *valaddr;
238      CORE_ADDR address;
239      GDB_FILE *stream;
240      int format;
241      int recurse;
242      enum val_prettyprint pretty;
243 {
244   int i, len, n_baseclasses;
245
246   CHECK_TYPEDEF (type);
247
248   fprintf_filtered (stream, "{");
249   len = TYPE_NFIELDS (type);
250   n_baseclasses = TYPE_N_BASECLASSES (type);
251
252   if (n_baseclasses > 0)
253     {
254       int i, n_baseclasses = TYPE_N_BASECLASSES (type);
255
256       for (i = 0; i < n_baseclasses; i++)
257         {
258           int boffset;
259           struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
260           char *basename = TYPE_NAME (baseclass);
261           char *base_valaddr;
262           
263           if (BASETYPE_VIA_VIRTUAL (type, i))
264             continue;
265
266           if (basename != NULL && strcmp (basename, "java.lang.Object") == 0)
267             continue;
268
269           boffset = 0;
270
271           if (pretty)
272             {
273               fprintf_filtered (stream, "\n");
274               print_spaces_filtered (2 * (recurse+1), stream);
275             }
276           fputs_filtered ("<", stream);
277           /* Not sure what the best notation is in the case where there is no
278              baseclass name.  */
279           fputs_filtered (basename ? basename : "", stream);
280           fputs_filtered ("> = ", stream);
281
282           base_valaddr = valaddr;
283
284           java_print_value_fields (baseclass, base_valaddr, address + boffset,
285                                    stream, format, recurse+1, pretty);
286           fputs_filtered (", ", stream);
287           
288         flush_it:
289           ;
290         }
291
292     }
293
294   if (!len && n_baseclasses == 1)
295     fprintf_filtered (stream, "<No data fields>");
296   else
297     {
298       extern int inspect_it;
299       int fields_seen = 0;
300
301       for (i = n_baseclasses; i < len; i++)
302         {
303           /* If requested, skip printing of static fields.  */
304           if (TYPE_FIELD_STATIC (type, i))
305             {
306               char *name = TYPE_FIELD_NAME (type, i);
307               if (!static_field_print)
308                 continue;
309               if (name != NULL && strcmp (name, "class") == 0)
310                 continue;
311             }
312           if (fields_seen)
313             fprintf_filtered (stream, ", ");
314           else if (n_baseclasses > 0)
315             {
316               if (pretty)
317                 {
318                   fprintf_filtered (stream, "\n");
319                   print_spaces_filtered (2 + 2 * recurse, stream);
320                   fputs_filtered ("members of ", stream);
321                   fputs_filtered (type_name_no_tag (type), stream);
322                   fputs_filtered (": ", stream);
323                 }
324             }
325           fields_seen = 1;
326
327           if (pretty)
328             {
329               fprintf_filtered (stream, "\n");
330               print_spaces_filtered (2 + 2 * recurse, stream);
331             }
332           else 
333             {
334               wrap_here (n_spaces (2 + 2 * recurse));
335             }
336           if (inspect_it)
337             {
338               if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
339                 fputs_filtered ("\"( ptr \"", stream);
340               else
341                 fputs_filtered ("\"( nodef \"", stream);
342               if (TYPE_FIELD_STATIC (type, i))
343                 fputs_filtered ("static ", stream);
344               fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
345                                        language_cplus,
346                                        DMGL_PARAMS | DMGL_ANSI);
347               fputs_filtered ("\" \"", stream);
348               fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
349                                        language_cplus,
350                                        DMGL_PARAMS | DMGL_ANSI);
351               fputs_filtered ("\") \"", stream);
352             }
353           else
354             {
355               annotate_field_begin (TYPE_FIELD_TYPE (type, i));
356
357               if (TYPE_FIELD_STATIC (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               annotate_field_name_end ();
363               fputs_filtered (": ", stream);
364               annotate_field_value ();
365             }
366
367           if (!TYPE_FIELD_STATIC (type, i) && TYPE_FIELD_PACKED (type, i))
368             {
369               value_ptr v;
370
371               /* Bitfields require special handling, especially due to byte
372                  order problems.  */
373               if (TYPE_FIELD_IGNORE (type, i))
374                 {
375                    fputs_filtered ("<optimized out or zero length>", stream);
376                 }
377               else
378                 {
379                    v = value_from_longest (TYPE_FIELD_TYPE (type, i),
380                                    unpack_field_as_long (type, valaddr, i));
381
382                    val_print (TYPE_FIELD_TYPE(type, i), VALUE_CONTENTS (v), 0,
383                               0, stream, format, 0, recurse + 1, pretty);
384                 }
385             }
386           else
387             {
388               if (TYPE_FIELD_IGNORE (type, i))
389                 {
390                    fputs_filtered ("<optimized out or zero length>", stream);
391                 }
392               else if (TYPE_FIELD_STATIC (type, i))
393                 {
394                   value_ptr v = value_static_field (type, i);
395                   if (v == NULL)
396                     fputs_filtered ("<optimized out>", stream);
397                   else
398                     {
399                       struct type *t = check_typedef (VALUE_TYPE (v));
400                       if (TYPE_CODE (t) == TYPE_CODE_STRUCT)
401                         v = value_addr (v);
402                       val_print (VALUE_TYPE (v),
403                                  VALUE_CONTENTS (v), 0, VALUE_ADDRESS (v),
404                                  stream, format, 0, recurse+1, pretty);
405                     }
406                 }
407               else if (TYPE_FIELD_TYPE (type, i) == NULL)
408                 fputs_filtered ("<unknown type>", stream);
409               else
410                 {
411                    val_print (TYPE_FIELD_TYPE (type, i), 
412                               valaddr + TYPE_FIELD_BITPOS (type, i) / 8, 0,
413                               address + TYPE_FIELD_BITPOS (type, i) / 8,
414                               stream, format, 0, recurse + 1, pretty);
415                 }
416             }
417           annotate_field_end ();
418         }
419
420       if (pretty)
421         {
422           fprintf_filtered (stream, "\n");
423           print_spaces_filtered (2 * recurse, stream);
424         }
425     }
426   fprintf_filtered (stream, "}");
427 }
428
429 /* Print data of type TYPE located at VALADDR (within GDB), which came from
430    the inferior at address ADDRESS, onto stdio stream STREAM according to
431    FORMAT (a letter or 0 for natural format).  The data at VALADDR is in
432    target byte order.
433
434    If the data are a string pointer, returns the number of string characters
435    printed.
436
437    If DEREF_REF is nonzero, then dereference references, otherwise just print
438    them like pointers.
439
440    The PRETTY parameter controls prettyprinting.  */
441
442 int
443 java_val_print (type, valaddr, embedded_offset, address, stream, format,
444                 deref_ref, recurse, pretty)
445      struct type *type;
446      char *valaddr;
447      CORE_ADDR address;
448      GDB_FILE *stream;
449      int format;
450      int deref_ref;
451      int recurse;
452      enum val_prettyprint pretty;
453 {
454   register unsigned int i = 0;          /* Number of characters printed */
455   struct type *target_type;
456   CORE_ADDR addr;
457
458   CHECK_TYPEDEF (type);
459   switch (TYPE_CODE (type))
460     {
461     case TYPE_CODE_PTR:
462       if (format && format != 's')
463         {
464           print_scalar_formatted (valaddr, type, format, 0, stream);
465           break;
466         }
467 #if 0
468       if (vtblprint && cp_is_vtbl_ptr_type(type))
469         {
470           /* Print the unmangled name if desired.  */
471           /* Print vtable entry - we only get here if we ARE using
472              -fvtable_thunks.  (Otherwise, look under TYPE_CODE_STRUCT.) */
473           print_address_demangle(extract_address (valaddr, TYPE_LENGTH (type)),
474                                  stream, demangle);
475           break;
476         }
477 #endif
478       addr = unpack_pointer (type, valaddr);
479       if (addr == 0)
480         {
481           fputs_filtered ("null", stream);
482           return i;
483         }
484       target_type = check_typedef (TYPE_TARGET_TYPE (type));
485
486       if (TYPE_CODE (target_type) == TYPE_CODE_FUNC)
487         {
488           /* Try to print what function it points to.  */
489           print_address_demangle (addr, stream, demangle);
490           /* Return value is irrelevant except for string pointers.  */
491           return (0);
492         }
493
494       if (addressprint && format != 's')
495         {
496           fputs_filtered ("@", stream);
497           print_longest (stream, 'x', 0, (ULONGEST) addr);
498         }
499
500       return i;
501
502     case TYPE_CODE_CHAR:
503       format = format ? format : output_format;
504       if (format)
505         print_scalar_formatted (valaddr, type, format, 0, stream);
506       else
507         LA_PRINT_CHAR ((int) unpack_long (type, valaddr), stream);
508       break;
509
510     case TYPE_CODE_INT:
511       /* Can't just call c_val_print because that print bytes as C chars. */
512       format = format ? format : output_format;
513       if (format)
514         print_scalar_formatted (valaddr, type, format, 0, stream);
515       else
516         val_print_type_code_int (type, valaddr, stream);
517       break;
518
519     case TYPE_CODE_STRUCT:
520       java_print_value_fields (type, valaddr, address, stream, format,
521                                recurse, pretty);
522       break;
523
524     default:
525       return c_val_print (type, valaddr, embedded_offset, address, stream,
526                           format, deref_ref, recurse, pretty);
527     }
528
529   return 0;
530 }