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