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