gdb
[external/binutils.git] / gdb / c-valprint.c
1 /* Support for printing C values for GDB, the GNU debugger.
2
3    Copyright (C) 1986, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
4    1998, 1999, 2000, 2001, 2003, 2005, 2006, 2007, 2008, 2009
5    Free Software Foundation, Inc.
6
7    This file is part of GDB.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21
22 #include "defs.h"
23 #include "gdb_string.h"
24 #include "symtab.h"
25 #include "gdbtypes.h"
26 #include "expression.h"
27 #include "value.h"
28 #include "valprint.h"
29 #include "language.h"
30 #include "c-lang.h"
31 #include "cp-abi.h"
32 #include "target.h"
33 \f
34
35 /* Print function pointer with inferior address ADDRESS onto stdio
36    stream STREAM.  */
37
38 static void
39 print_function_pointer_address (CORE_ADDR address, struct ui_file *stream,
40                                 int addressprint)
41 {
42   CORE_ADDR func_addr = gdbarch_convert_from_func_ptr_addr (current_gdbarch,
43                                                             address,
44                                                             &current_target);
45
46   /* If the function pointer is represented by a description, print the
47      address of the description.  */
48   if (addressprint && func_addr != address)
49     {
50       fputs_filtered ("@", stream);
51       fputs_filtered (paddress (address), stream);
52       fputs_filtered (": ", stream);
53     }
54   print_address_demangle (func_addr, stream, demangle);
55 }
56
57
58 /* A helper for textual_element_type.  This checks the name of the
59    typedef.  This is bogus but it isn't apparent that the compiler
60    provides us the help we may need.  */
61
62 static int
63 textual_name (const char *name)
64 {
65   return (!strcmp (name, "wchar_t")
66           || !strcmp (name, "char16_t")
67           || !strcmp (name, "char32_t"));
68 }
69
70 /* Apply a heuristic to decide whether an array of TYPE or a pointer
71    to TYPE should be printed as a textual string.  Return non-zero if
72    it should, or zero if it should be treated as an array of integers
73    or pointer to integers.  FORMAT is the current format letter,
74    or 0 if none.
75
76    We guess that "char" is a character.  Explicitly signed and
77    unsigned character types are also characters.  Integer data from
78    vector types is not.  The user can override this by using the /s
79    format letter.  */
80
81 static int
82 textual_element_type (struct type *type, char format)
83 {
84   struct type *true_type, *iter_type;
85
86   if (format != 0 && format != 's')
87     return 0;
88
89   /* We also rely on this for its side effect of setting up all the
90      typedef pointers.  */
91   true_type = check_typedef (type);
92
93   /* TYPE_CODE_CHAR is always textual.  */
94   if (TYPE_CODE (true_type) == TYPE_CODE_CHAR)
95     return 1;
96
97   /* Any other character-like types must be integral.  */
98   if (TYPE_CODE (true_type) != TYPE_CODE_INT)
99     return 0;
100
101   /* We peel typedefs one by one, looking for a match.  */
102   iter_type = type;
103   while (iter_type)
104     {
105       /* Check the name of the type.  */
106       if (TYPE_NAME (iter_type) && textual_name (TYPE_NAME (iter_type)))
107         return 1;
108
109       if (TYPE_CODE (iter_type) != TYPE_CODE_TYPEDEF)
110         break;
111
112       /* Peel a single typedef.  If the typedef doesn't have a target
113          type, we use check_typedef and hope the result is ok -- it
114          might be for C++, where wchar_t is a built-in type.  */
115       if (TYPE_TARGET_TYPE (iter_type))
116         iter_type = TYPE_TARGET_TYPE (iter_type);
117       else
118         iter_type = check_typedef (iter_type);
119     }
120
121   if (format == 's')
122     {
123       /* Print this as a string if we can manage it.  For now, no
124          wide character support.  */
125       if (TYPE_CODE (true_type) == TYPE_CODE_INT
126           && TYPE_LENGTH (true_type) == 1)
127         return 1;
128     }
129   else
130     {
131       /* If a one-byte TYPE_CODE_INT is missing the not-a-character
132          flag, then we treat it as text; otherwise, we assume it's
133          being used as data.  */
134       if (TYPE_CODE (true_type) == TYPE_CODE_INT
135           && TYPE_LENGTH (true_type) == 1
136           && !TYPE_NOTTEXT (true_type))
137         return 1;
138     }
139
140   return 0;
141 }
142
143
144 /* Print data of type TYPE located at VALADDR (within GDB), which came from
145    the inferior at address ADDRESS, onto stdio stream STREAM according to
146    OPTIONS.  The data at VALADDR is in target byte order.
147
148    If the data are a string pointer, returns the number of string characters
149    printed.  */
150
151 int
152 c_val_print (struct type *type, const gdb_byte *valaddr, int embedded_offset,
153              CORE_ADDR address, struct ui_file *stream, int recurse,
154              const struct value_print_options *options)
155 {
156   unsigned int i = 0;   /* Number of characters printed */
157   unsigned len;
158   struct type *elttype, *unresolved_elttype;
159   struct type *unresolved_type = type;
160   unsigned eltlen;
161   LONGEST val;
162   CORE_ADDR addr;
163
164   CHECK_TYPEDEF (type);
165   switch (TYPE_CODE (type))
166     {
167     case TYPE_CODE_ARRAY:
168       unresolved_elttype = TYPE_TARGET_TYPE (type);
169       elttype = check_typedef (unresolved_elttype);
170       if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (unresolved_elttype) > 0)
171         {
172           eltlen = TYPE_LENGTH (elttype);
173           len = TYPE_LENGTH (type) / eltlen;
174           if (options->prettyprint_arrays)
175             {
176               print_spaces_filtered (2 + 2 * recurse, stream);
177             }
178
179           /* Print arrays of textual chars with a string syntax.  */
180           if (textual_element_type (unresolved_elttype, options->format))
181             {
182               /* If requested, look for the first null char and only print
183                  elements up to it.  */
184               if (options->stop_print_at_null)
185                 {
186                   unsigned int temp_len;
187
188                   for (temp_len = 0;
189                        (temp_len < len
190                         && temp_len < options->print_max
191                         && extract_unsigned_integer (valaddr + embedded_offset
192                                                      + temp_len * eltlen,
193                                                      eltlen) == 0);
194                        ++temp_len)
195                     ;
196                   len = temp_len;
197                 }
198
199               LA_PRINT_STRING (stream, unresolved_elttype,
200                                valaddr + embedded_offset, len, 0, options);
201               i = len;
202             }
203           else
204             {
205               fprintf_filtered (stream, "{");
206               /* If this is a virtual function table, print the 0th
207                  entry specially, and the rest of the members normally.  */
208               if (cp_is_vtbl_ptr_type (elttype))
209                 {
210                   i = 1;
211                   fprintf_filtered (stream, _("%d vtable entries"), len - 1);
212                 }
213               else
214                 {
215                   i = 0;
216                 }
217               val_print_array_elements (type, valaddr + embedded_offset, address, stream,
218                                         recurse, options, i);
219               fprintf_filtered (stream, "}");
220             }
221           break;
222         }
223       /* Array of unspecified length: treat like pointer to first elt.  */
224       addr = address;
225       goto print_unpacked_pointer;
226
227     case TYPE_CODE_MEMBERPTR:
228       if (options->format)
229         {
230           print_scalar_formatted (valaddr + embedded_offset, type,
231                                   options, 0, stream);
232           break;
233         }
234       cp_print_class_member (valaddr + embedded_offset, type, stream, "&");
235       break;
236
237     case TYPE_CODE_METHODPTR:
238       cplus_print_method_ptr (valaddr + embedded_offset, type, stream);
239       break;
240
241     case TYPE_CODE_PTR:
242       if (options->format && options->format != 's')
243         {
244           print_scalar_formatted (valaddr + embedded_offset, type,
245                                   options, 0, stream);
246           break;
247         }
248       if (options->vtblprint && cp_is_vtbl_ptr_type (type))
249         {
250           /* Print the unmangled name if desired.  */
251           /* Print vtable entry - we only get here if we ARE using
252              -fvtable_thunks.  (Otherwise, look under TYPE_CODE_STRUCT.) */
253           CORE_ADDR addr
254             = extract_typed_address (valaddr + embedded_offset, type);
255           print_function_pointer_address (addr, stream, options->addressprint);
256           break;
257         }
258       unresolved_elttype = TYPE_TARGET_TYPE (type);
259       elttype = check_typedef (unresolved_elttype);
260         {
261           addr = unpack_pointer (type, valaddr + embedded_offset);
262         print_unpacked_pointer:
263
264           if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
265             {
266               /* Try to print what function it points to.  */
267               print_function_pointer_address (addr, stream,
268                                               options->addressprint);
269               /* Return value is irrelevant except for string pointers.  */
270               return (0);
271             }
272
273           if (options->addressprint)
274             fputs_filtered (paddress (addr), stream);
275
276           /* For a pointer to a textual type, also print the string
277              pointed to, unless pointer is null.  */
278
279           if (textual_element_type (unresolved_elttype, options->format)
280               && addr != 0)
281             {
282               i = val_print_string (unresolved_elttype, addr, -1, stream,
283                                     options);
284             }
285           else if (cp_is_vtbl_member (type))
286             {
287               /* print vtbl's nicely */
288               CORE_ADDR vt_address = unpack_pointer (type, valaddr + embedded_offset);
289
290               struct minimal_symbol *msymbol =
291               lookup_minimal_symbol_by_pc (vt_address);
292               if ((msymbol != NULL) &&
293                   (vt_address == SYMBOL_VALUE_ADDRESS (msymbol)))
294                 {
295                   fputs_filtered (" <", stream);
296                   fputs_filtered (SYMBOL_PRINT_NAME (msymbol), stream);
297                   fputs_filtered (">", stream);
298                 }
299               if (vt_address && options->vtblprint)
300                 {
301                   struct value *vt_val;
302                   struct symbol *wsym = (struct symbol *) NULL;
303                   struct type *wtype;
304                   struct block *block = (struct block *) NULL;
305                   int is_this_fld;
306
307                   if (msymbol != NULL)
308                     wsym = lookup_symbol (SYMBOL_LINKAGE_NAME (msymbol), block,
309                                           VAR_DOMAIN, &is_this_fld);
310
311                   if (wsym)
312                     {
313                       wtype = SYMBOL_TYPE (wsym);
314                     }
315                   else
316                     {
317                       wtype = unresolved_elttype;
318                     }
319                   vt_val = value_at (wtype, vt_address);
320                   common_val_print (vt_val, stream, recurse + 1, options,
321                                     current_language);
322                   if (options->pretty)
323                     {
324                       fprintf_filtered (stream, "\n");
325                       print_spaces_filtered (2 + 2 * recurse, stream);
326                     }
327                 }
328             }
329
330           /* Return number of characters printed, including the terminating
331              '\0' if we reached the end.  val_print_string takes care including
332              the terminating '\0' if necessary.  */
333           return i;
334         }
335       break;
336
337     case TYPE_CODE_REF:
338       elttype = check_typedef (TYPE_TARGET_TYPE (type));
339       if (options->addressprint)
340         {
341           CORE_ADDR addr
342             = extract_typed_address (valaddr + embedded_offset, type);
343           fprintf_filtered (stream, "@");
344           fputs_filtered (paddress (addr), stream);
345           if (options->deref_ref)
346             fputs_filtered (": ", stream);
347         }
348       /* De-reference the reference.  */
349       if (options->deref_ref)
350         {
351           if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
352             {
353               struct value *deref_val =
354               value_at
355               (TYPE_TARGET_TYPE (type),
356                unpack_pointer (type, valaddr + embedded_offset));
357               common_val_print (deref_val, stream, recurse, options,
358                                 current_language);
359             }
360           else
361             fputs_filtered ("???", stream);
362         }
363       break;
364
365     case TYPE_CODE_UNION:
366       if (recurse && !options->unionprint)
367         {
368           fprintf_filtered (stream, "{...}");
369           break;
370         }
371       /* Fall through.  */
372     case TYPE_CODE_STRUCT:
373       /*FIXME: Abstract this away */
374       if (options->vtblprint && cp_is_vtbl_ptr_type (type))
375         {
376           /* Print the unmangled name if desired.  */
377           /* Print vtable entry - we only get here if NOT using
378              -fvtable_thunks.  (Otherwise, look under TYPE_CODE_PTR.) */
379           int offset = (embedded_offset +
380                         TYPE_FIELD_BITPOS (type, VTBL_FNADDR_OFFSET) / 8);
381           struct type *field_type = TYPE_FIELD_TYPE (type, VTBL_FNADDR_OFFSET);
382           CORE_ADDR addr
383             = extract_typed_address (valaddr + offset, field_type);
384
385           print_function_pointer_address (addr, stream, options->addressprint);
386         }
387       else
388         cp_print_value_fields (type, type, valaddr, embedded_offset, address, stream,
389                                recurse, options, NULL, 0);
390       break;
391
392     case TYPE_CODE_ENUM:
393       if (options->format)
394         {
395           print_scalar_formatted (valaddr + embedded_offset, type,
396                                   options, 0, stream);
397           break;
398         }
399       len = TYPE_NFIELDS (type);
400       val = unpack_long (type, valaddr + embedded_offset);
401       for (i = 0; i < len; i++)
402         {
403           QUIT;
404           if (val == TYPE_FIELD_BITPOS (type, i))
405             {
406               break;
407             }
408         }
409       if (i < len)
410         {
411           fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
412         }
413       else
414         {
415           print_longest (stream, 'd', 0, val);
416         }
417       break;
418
419     case TYPE_CODE_FLAGS:
420       if (options->format)
421           print_scalar_formatted (valaddr + embedded_offset, type,
422                                   options, 0, stream);
423       else
424         val_print_type_code_flags (type, valaddr + embedded_offset, stream);
425       break;
426
427     case TYPE_CODE_FUNC:
428     case TYPE_CODE_METHOD:
429       if (options->format)
430         {
431           print_scalar_formatted (valaddr + embedded_offset, type,
432                                   options, 0, stream);
433           break;
434         }
435       /* FIXME, we should consider, at least for ANSI C language, eliminating
436          the distinction made between FUNCs and POINTERs to FUNCs.  */
437       fprintf_filtered (stream, "{");
438       type_print (type, "", stream, -1);
439       fprintf_filtered (stream, "} ");
440       /* Try to print what function it points to, and its address.  */
441       print_address_demangle (address, stream, demangle);
442       break;
443
444     case TYPE_CODE_BOOL:
445       if (options->format || options->output_format)
446         {
447           struct value_print_options opts = *options;
448           opts.format = (options->format ? options->format
449                          : options->output_format);
450           print_scalar_formatted (valaddr + embedded_offset, type,
451                                   &opts, 0, stream);
452         }
453       else
454         {
455           val = unpack_long (type, valaddr + embedded_offset);
456           if (val == 0)
457             fputs_filtered ("false", stream);
458           else if (val == 1)
459             fputs_filtered ("true", stream);
460           else
461             print_longest (stream, 'd', 0, val);
462         }
463       break;
464
465     case TYPE_CODE_RANGE:
466       /* FIXME: create_range_type does not set the unsigned bit in a
467          range type (I think it probably should copy it from the target
468          type), so we won't print values which are too large to
469          fit in a signed integer correctly.  */
470       /* FIXME: Doesn't handle ranges of enums correctly.  (Can't just
471          print with the target type, though, because the size of our type
472          and the target type might differ).  */
473       /* FALLTHROUGH */
474
475     case TYPE_CODE_INT:
476       if (options->format || options->output_format)
477         {
478           struct value_print_options opts = *options;
479           opts.format = (options->format ? options->format
480                          : options->output_format);
481           print_scalar_formatted (valaddr + embedded_offset, type,
482                                   &opts, 0, stream);
483         }
484       else
485         {
486           val_print_type_code_int (type, valaddr + embedded_offset, stream);
487           /* C and C++ has no single byte int type, char is used instead.
488              Since we don't know whether the value is really intended to
489              be used as an integer or a character, print the character
490              equivalent as well.  */
491           if (textual_element_type (unresolved_type, options->format))
492             {
493               fputs_filtered (" ", stream);
494               LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr + embedded_offset),
495                              unresolved_type, stream);
496             }
497         }
498       break;
499
500     case TYPE_CODE_CHAR:
501       if (options->format || options->output_format)
502         {
503           struct value_print_options opts = *options;
504           opts.format = (options->format ? options->format
505                          : options->output_format);
506           print_scalar_formatted (valaddr + embedded_offset, type,
507                                   &opts, 0, stream);
508         }
509       else
510         {
511           val = unpack_long (type, valaddr + embedded_offset);
512           if (TYPE_UNSIGNED (type))
513             fprintf_filtered (stream, "%u", (unsigned int) val);
514           else
515             fprintf_filtered (stream, "%d", (int) val);
516           fputs_filtered (" ", stream);
517           LA_PRINT_CHAR ((unsigned char) val, unresolved_type, stream);
518         }
519       break;
520
521     case TYPE_CODE_FLT:
522       if (options->format)
523         {
524           print_scalar_formatted (valaddr + embedded_offset, type,
525                                   options, 0, stream);
526         }
527       else
528         {
529           print_floating (valaddr + embedded_offset, type, stream);
530         }
531       break;
532
533     case TYPE_CODE_DECFLOAT:
534       if (options->format)
535         print_scalar_formatted (valaddr + embedded_offset, type,
536                                 options, 0, stream);
537       else
538         print_decimal_floating (valaddr + embedded_offset, type, stream);
539       break;
540
541     case TYPE_CODE_VOID:
542       fprintf_filtered (stream, "void");
543       break;
544
545     case TYPE_CODE_ERROR:
546       fprintf_filtered (stream, _("<error type>"));
547       break;
548
549     case TYPE_CODE_UNDEF:
550       /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
551          dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
552          and no complete type for struct foo in that file.  */
553       fprintf_filtered (stream, _("<incomplete type>"));
554       break;
555
556     case TYPE_CODE_COMPLEX:
557       if (options->format)
558         print_scalar_formatted (valaddr + embedded_offset,
559                                 TYPE_TARGET_TYPE (type),
560                                 options, 0, stream);
561       else
562         print_floating (valaddr + embedded_offset, TYPE_TARGET_TYPE (type),
563                         stream);
564       fprintf_filtered (stream, " + ");
565       if (options->format)
566         print_scalar_formatted (valaddr + embedded_offset
567                                 + TYPE_LENGTH (TYPE_TARGET_TYPE (type)),
568                                 TYPE_TARGET_TYPE (type),
569                                 options, 0, stream);
570       else
571         print_floating (valaddr + embedded_offset
572                         + TYPE_LENGTH (TYPE_TARGET_TYPE (type)),
573                         TYPE_TARGET_TYPE (type),
574                         stream);
575       fprintf_filtered (stream, " * I");
576       break;
577
578     default:
579       error (_("Invalid C/C++ type code %d in symbol table."), TYPE_CODE (type));
580     }
581   gdb_flush (stream);
582   return (0);
583 }
584 \f
585 int
586 c_value_print (struct value *val, struct ui_file *stream, 
587                const struct value_print_options *options)
588 {
589   struct type *type, *real_type, *val_type;
590   int full, top, using_enc;
591   struct value_print_options opts = *options;
592
593   opts.deref_ref = 1;
594
595   /* If it is a pointer, indicate what it points to.
596
597      Print type also if it is a reference.
598
599      C++: if it is a member pointer, we will take care
600      of that when we print it.  */
601
602   /* Preserve the original type before stripping typedefs.  We prefer
603      to pass down the original type when possible, but for local
604      checks it is better to look past the typedefs.  */
605   val_type = value_type (val);
606   type = check_typedef (val_type);
607
608   if (TYPE_CODE (type) == TYPE_CODE_PTR
609       || TYPE_CODE (type) == TYPE_CODE_REF)
610     {
611       /* Hack:  remove (char *) for char strings.  Their
612          type is indicated by the quoted string anyway.
613          (Don't use textual_element_type here; quoted strings
614          are always exactly (char *), (wchar_t *), or the like.  */
615       if (TYPE_CODE (val_type) == TYPE_CODE_PTR
616           && TYPE_NAME (val_type) == NULL
617           && TYPE_NAME (TYPE_TARGET_TYPE (val_type)) != NULL
618           && (strcmp (TYPE_NAME (TYPE_TARGET_TYPE (val_type)), "char") == 0
619               || textual_name (TYPE_NAME (TYPE_TARGET_TYPE (val_type)))))
620         {
621           /* Print nothing */
622         }
623       else if (options->objectprint
624                && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CLASS))
625         {
626
627           if (TYPE_CODE(type) == TYPE_CODE_REF)
628             {
629               /* Copy value, change to pointer, so we don't get an
630                * error about a non-pointer type in value_rtti_target_type
631                */
632               struct value *temparg;
633               temparg=value_copy(val);
634               deprecated_set_value_type (temparg, lookup_pointer_type (TYPE_TARGET_TYPE(type)));
635               val=temparg;
636             }
637           /* Pointer to class, check real type of object */
638           fprintf_filtered (stream, "(");
639           real_type = value_rtti_target_type (val, &full, &top, &using_enc);
640           if (real_type)
641             {
642               /* RTTI entry found */
643               if (TYPE_CODE (type) == TYPE_CODE_PTR)
644                 {
645                   /* create a pointer type pointing to the real type */
646                   type = lookup_pointer_type (real_type);
647                 }
648               else
649                 {
650                   /* create a reference type referencing the real type */
651                   type = lookup_reference_type (real_type);
652                 }
653               /* JYG: Need to adjust pointer value. */
654               /* NOTE: cagney/2005-01-02: THIS IS BOGUS.  */
655               value_contents_writeable (val)[0] -= top;
656
657               /* Note: When we look up RTTI entries, we don't get any 
658                  information on const or volatile attributes */
659             }
660           type_print (type, "", stream, -1);
661           fprintf_filtered (stream, ") ");
662           val_type = type;
663         }
664       else
665         {
666           /* normal case */
667           fprintf_filtered (stream, "(");
668           type_print (value_type (val), "", stream, -1);
669           fprintf_filtered (stream, ") ");
670         }
671     }
672
673   if (!value_initialized (val))
674     fprintf_filtered (stream, " [uninitialized] ");
675
676   if (options->objectprint && (TYPE_CODE (type) == TYPE_CODE_CLASS))
677     {
678       /* Attempt to determine real type of object */
679       real_type = value_rtti_type (val, &full, &top, &using_enc);
680       if (real_type)
681         {
682           /* We have RTTI information, so use it */
683           val = value_full_object (val, real_type, full, top, using_enc);
684           fprintf_filtered (stream, "(%s%s) ",
685                             TYPE_NAME (real_type),
686                             full ? "" : _(" [incomplete object]"));
687           /* Print out object: enclosing type is same as real_type if full */
688           return val_print (value_enclosing_type (val),
689                             value_contents_all (val), 0,
690                             value_address (val), stream, 0,
691                             &opts, current_language);
692           /* Note: When we look up RTTI entries, we don't get any information on
693              const or volatile attributes */
694         }
695       else if (type != check_typedef (value_enclosing_type (val)))
696         {
697           /* No RTTI information, so let's do our best */
698           fprintf_filtered (stream, "(%s ?) ",
699                             TYPE_NAME (value_enclosing_type (val)));
700           return val_print (value_enclosing_type (val),
701                             value_contents_all (val), 0,
702                             value_address (val), stream, 0,
703                             &opts, current_language);
704         }
705       /* Otherwise, we end up at the return outside this "if" */
706     }
707
708   return val_print (val_type, value_contents_all (val),
709                     value_embedded_offset (val),
710                     value_address (val),
711                     stream, 0, &opts, current_language);
712 }