1 /* Support for printing C values for GDB, the GNU debugger.
3 Copyright (C) 1986, 1988-1989, 1991-2001, 2003, 2005-2012 Free
4 Software Foundation, Inc.
6 This file is part of GDB.
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.
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.
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/>. */
22 #include "gdb_string.h"
25 #include "expression.h"
34 /* A helper for c_textual_element_type. This checks the name of the
35 typedef. This is bogus but it isn't apparent that the compiler
36 provides us the help we may need. */
39 textual_name (const char *name)
41 return (!strcmp (name, "wchar_t")
42 || !strcmp (name, "char16_t")
43 || !strcmp (name, "char32_t"));
46 /* Apply a heuristic to decide whether an array of TYPE or a pointer
47 to TYPE should be printed as a textual string. Return non-zero if
48 it should, or zero if it should be treated as an array of integers
49 or pointer to integers. FORMAT is the current format letter, or 0
52 We guess that "char" is a character. Explicitly signed and
53 unsigned character types are also characters. Integer data from
54 vector types is not. The user can override this by using the /s
58 c_textual_element_type (struct type *type, char format)
60 struct type *true_type, *iter_type;
62 if (format != 0 && format != 's')
65 /* We also rely on this for its side effect of setting up all the
67 true_type = check_typedef (type);
69 /* TYPE_CODE_CHAR is always textual. */
70 if (TYPE_CODE (true_type) == TYPE_CODE_CHAR)
73 /* Any other character-like types must be integral. */
74 if (TYPE_CODE (true_type) != TYPE_CODE_INT)
77 /* We peel typedefs one by one, looking for a match. */
81 /* Check the name of the type. */
82 if (TYPE_NAME (iter_type) && textual_name (TYPE_NAME (iter_type)))
85 if (TYPE_CODE (iter_type) != TYPE_CODE_TYPEDEF)
88 /* Peel a single typedef. If the typedef doesn't have a target
89 type, we use check_typedef and hope the result is ok -- it
90 might be for C++, where wchar_t is a built-in type. */
91 if (TYPE_TARGET_TYPE (iter_type))
92 iter_type = TYPE_TARGET_TYPE (iter_type);
94 iter_type = check_typedef (iter_type);
99 /* Print this as a string if we can manage it. For now, no wide
100 character support. */
101 if (TYPE_CODE (true_type) == TYPE_CODE_INT
102 && TYPE_LENGTH (true_type) == 1)
107 /* If a one-byte TYPE_CODE_INT is missing the not-a-character
108 flag, then we treat it as text; otherwise, we assume it's
109 being used as data. */
110 if (TYPE_CODE (true_type) == TYPE_CODE_INT
111 && TYPE_LENGTH (true_type) == 1
112 && !TYPE_NOTTEXT (true_type))
119 /* See val_print for a description of the various parameters of this
120 function; they are identical. The semantics of the return value is
121 also identical to val_print. */
124 c_val_print (struct type *type, const gdb_byte *valaddr,
125 int embedded_offset, CORE_ADDR address,
126 struct ui_file *stream, int recurse,
127 const struct value *original_value,
128 const struct value_print_options *options)
130 struct gdbarch *gdbarch = get_type_arch (type);
131 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
132 unsigned int i = 0; /* Number of characters printed. */
134 struct type *elttype, *unresolved_elttype;
135 struct type *unresolved_type = type;
140 CHECK_TYPEDEF (type);
141 switch (TYPE_CODE (type))
143 case TYPE_CODE_ARRAY:
144 unresolved_elttype = TYPE_TARGET_TYPE (type);
145 elttype = check_typedef (unresolved_elttype);
146 if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (unresolved_elttype) > 0)
148 LONGEST low_bound, high_bound;
150 if (!get_array_bounds (type, &low_bound, &high_bound))
151 error (_("Could not determine the array high bound"));
153 eltlen = TYPE_LENGTH (elttype);
154 len = high_bound - low_bound + 1;
155 if (options->prettyprint_arrays)
157 print_spaces_filtered (2 + 2 * recurse, stream);
160 /* Print arrays of textual chars with a string syntax, as
161 long as the entire array is valid. */
162 if (c_textual_element_type (unresolved_elttype,
164 && value_bytes_available (original_value, embedded_offset,
166 && value_bits_valid (original_value,
167 TARGET_CHAR_BIT * embedded_offset,
168 TARGET_CHAR_BIT * TYPE_LENGTH (type)))
170 /* If requested, look for the first null char and only
171 print elements up to it. */
172 if (options->stop_print_at_null)
174 unsigned int temp_len;
178 && temp_len < options->print_max
179 && extract_unsigned_integer (valaddr + embedded_offset
181 eltlen, byte_order) != 0);
187 LA_PRINT_STRING (stream, unresolved_elttype,
188 valaddr + embedded_offset, len,
194 fprintf_filtered (stream, "{");
195 /* If this is a virtual function table, print the 0th
196 entry specially, and the rest of the members
198 if (cp_is_vtbl_ptr_type (elttype))
201 fprintf_filtered (stream, _("%d vtable entries"),
208 val_print_array_elements (type, valaddr, embedded_offset,
210 recurse, original_value, options, i);
211 fprintf_filtered (stream, "}");
215 /* Array of unspecified length: treat like pointer to first
217 addr = address + embedded_offset;
218 goto print_unpacked_pointer;
220 case TYPE_CODE_MEMBERPTR:
223 val_print_scalar_formatted (type, valaddr, embedded_offset,
224 original_value, options, 0, stream);
227 cp_print_class_member (valaddr + embedded_offset, type, stream, "&");
230 case TYPE_CODE_METHODPTR:
231 cplus_print_method_ptr (valaddr + embedded_offset, type, stream);
235 if (options->format && options->format != 's')
237 val_print_scalar_formatted (type, valaddr, embedded_offset,
238 original_value, options, 0, stream);
241 if (options->vtblprint && cp_is_vtbl_ptr_type (type))
243 /* Print the unmangled name if desired. */
244 /* Print vtable entry - we only get here if we ARE using
245 -fvtable_thunks. (Otherwise, look under
246 TYPE_CODE_STRUCT.) */
248 = extract_typed_address (valaddr + embedded_offset, type);
250 print_function_pointer_address (gdbarch, addr, stream,
251 options->addressprint);
254 unresolved_elttype = TYPE_TARGET_TYPE (type);
255 elttype = check_typedef (unresolved_elttype);
257 addr = unpack_pointer (type, valaddr + embedded_offset);
258 print_unpacked_pointer:
260 if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
262 /* Try to print what function it points to. */
263 print_function_pointer_address (gdbarch, addr, stream,
264 options->addressprint);
265 /* Return value is irrelevant except for string
270 if (options->addressprint)
271 fputs_filtered (paddress (gdbarch, addr), stream);
273 /* For a pointer to a textual type, also print the string
274 pointed to, unless pointer is null. */
276 if (c_textual_element_type (unresolved_elttype,
280 i = val_print_string (unresolved_elttype, NULL,
284 else if (cp_is_vtbl_member (type))
286 /* Print vtbl's nicely. */
287 CORE_ADDR vt_address = unpack_pointer (type,
291 struct minimal_symbol *msymbol =
292 lookup_minimal_symbol_by_pc (vt_address);
293 if ((msymbol != NULL)
294 && (vt_address == SYMBOL_VALUE_ADDRESS (msymbol)))
296 fputs_filtered (" <", stream);
297 fputs_filtered (SYMBOL_PRINT_NAME (msymbol), stream);
298 fputs_filtered (">", stream);
300 if (vt_address && options->vtblprint)
302 struct value *vt_val;
303 struct symbol *wsym = (struct symbol *) NULL;
305 struct block *block = (struct block *) NULL;
309 wsym = lookup_symbol (SYMBOL_LINKAGE_NAME (msymbol),
315 wtype = SYMBOL_TYPE (wsym);
319 wtype = unresolved_elttype;
321 vt_val = value_at (wtype, vt_address);
322 common_val_print (vt_val, stream, recurse + 1,
323 options, current_language);
326 fprintf_filtered (stream, "\n");
327 print_spaces_filtered (2 + 2 * recurse, stream);
332 /* Return number of characters printed, including the
333 terminating '\0' if we reached the end. val_print_string
334 takes care including the terminating '\0' if
341 elttype = check_typedef (TYPE_TARGET_TYPE (type));
342 if (options->addressprint)
345 = extract_typed_address (valaddr + embedded_offset, type);
347 fprintf_filtered (stream, "@");
348 fputs_filtered (paddress (gdbarch, addr), stream);
349 if (options->deref_ref)
350 fputs_filtered (": ", stream);
352 /* De-reference the reference. */
353 if (options->deref_ref)
355 if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
357 struct value *deref_val;
359 deref_val = coerce_ref_if_computed (original_value);
360 if (deref_val != NULL)
362 /* More complicated computed references are not supported. */
363 gdb_assert (embedded_offset == 0);
366 deref_val = value_at (TYPE_TARGET_TYPE (type),
367 unpack_pointer (type,
369 + embedded_offset)));
371 common_val_print (deref_val, stream, recurse, options,
375 fputs_filtered ("???", stream);
379 case TYPE_CODE_UNION:
380 if (recurse && !options->unionprint)
382 fprintf_filtered (stream, "{...}");
386 case TYPE_CODE_STRUCT:
387 /*FIXME: Abstract this away. */
388 if (options->vtblprint && cp_is_vtbl_ptr_type (type))
390 /* Print the unmangled name if desired. */
391 /* Print vtable entry - we only get here if NOT using
392 -fvtable_thunks. (Otherwise, look under
394 int offset = (embedded_offset
395 + TYPE_FIELD_BITPOS (type,
396 VTBL_FNADDR_OFFSET) / 8);
397 struct type *field_type = TYPE_FIELD_TYPE (type,
400 = extract_typed_address (valaddr + offset, field_type);
402 print_function_pointer_address (gdbarch, addr, stream,
403 options->addressprint);
406 cp_print_value_fields_rtti (type, valaddr,
407 embedded_offset, address,
409 original_value, options,
416 val_print_scalar_formatted (type, valaddr, embedded_offset,
417 original_value, options, 0, stream);
420 len = TYPE_NFIELDS (type);
421 val = unpack_long (type, valaddr + embedded_offset);
422 for (i = 0; i < len; i++)
425 if (val == TYPE_FIELD_BITPOS (type, i))
432 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
434 else if (TYPE_FLAG_ENUM (type))
438 /* We have a "flag" enum, so we try to decompose it into
439 pieces as appropriate. A flag enum has disjoint
440 constants by definition. */
441 fputs_filtered ("(", stream);
442 for (i = 0; i < len; ++i)
446 if ((val & TYPE_FIELD_BITPOS (type, i)) != 0)
449 fputs_filtered (" | ", stream);
452 val &= ~TYPE_FIELD_BITPOS (type, i);
453 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
457 if (first || val != 0)
460 fputs_filtered (" | ", stream);
461 fputs_filtered ("unknown: ", stream);
462 print_longest (stream, 'd', 0, val);
465 fputs_filtered (")", stream);
468 print_longest (stream, 'd', 0, val);
471 case TYPE_CODE_FLAGS:
473 val_print_scalar_formatted (type, valaddr, embedded_offset,
474 original_value, options, 0, stream);
476 val_print_type_code_flags (type, valaddr + embedded_offset,
481 case TYPE_CODE_METHOD:
484 val_print_scalar_formatted (type, valaddr, embedded_offset,
485 original_value, options, 0, stream);
488 /* FIXME, we should consider, at least for ANSI C language,
489 eliminating the distinction made between FUNCs and POINTERs
491 fprintf_filtered (stream, "{");
492 type_print (type, "", stream, -1);
493 fprintf_filtered (stream, "} ");
494 /* Try to print what function it points to, and its address. */
495 print_address_demangle (gdbarch, address, stream, demangle);
499 if (options->format || options->output_format)
501 struct value_print_options opts = *options;
502 opts.format = (options->format ? options->format
503 : options->output_format);
504 val_print_scalar_formatted (type, valaddr, embedded_offset,
505 original_value, &opts, 0, stream);
509 val = unpack_long (type, valaddr + embedded_offset);
511 fputs_filtered ("false", stream);
513 fputs_filtered ("true", stream);
515 print_longest (stream, 'd', 0, val);
519 case TYPE_CODE_RANGE:
520 /* FIXME: create_range_type does not set the unsigned bit in a
521 range type (I think it probably should copy it from the
522 target type), so we won't print values which are too large to
523 fit in a signed integer correctly. */
524 /* FIXME: Doesn't handle ranges of enums correctly. (Can't just
525 print with the target type, though, because the size of our
526 type and the target type might differ). */
530 if (options->format || options->output_format)
532 struct value_print_options opts = *options;
534 opts.format = (options->format ? options->format
535 : options->output_format);
536 val_print_scalar_formatted (type, valaddr, embedded_offset,
537 original_value, &opts, 0, stream);
541 val_print_type_code_int (type, valaddr + embedded_offset,
543 /* C and C++ has no single byte int type, char is used
544 instead. Since we don't know whether the value is really
545 intended to be used as an integer or a character, print
546 the character equivalent as well. */
547 if (c_textual_element_type (unresolved_type, options->format))
549 fputs_filtered (" ", stream);
550 LA_PRINT_CHAR (unpack_long (type, valaddr + embedded_offset),
551 unresolved_type, stream);
557 if (options->format || options->output_format)
559 struct value_print_options opts = *options;
560 opts.format = (options->format ? options->format
561 : options->output_format);
562 val_print_scalar_formatted (type, valaddr, embedded_offset,
563 original_value, &opts, 0, stream);
567 val = unpack_long (type, valaddr + embedded_offset);
568 if (TYPE_UNSIGNED (type))
569 fprintf_filtered (stream, "%u", (unsigned int) val);
571 fprintf_filtered (stream, "%d", (int) val);
572 fputs_filtered (" ", stream);
573 LA_PRINT_CHAR (val, unresolved_type, stream);
580 val_print_scalar_formatted (type, valaddr, embedded_offset,
581 original_value, options, 0, stream);
585 print_floating (valaddr + embedded_offset, type, stream);
589 case TYPE_CODE_DECFLOAT:
591 val_print_scalar_formatted (type, valaddr, embedded_offset,
592 original_value, options, 0, stream);
594 print_decimal_floating (valaddr + embedded_offset,
599 fprintf_filtered (stream, "void");
602 case TYPE_CODE_ERROR:
603 fprintf_filtered (stream, "%s", TYPE_ERROR_NAME (type));
606 case TYPE_CODE_UNDEF:
607 /* This happens (without TYPE_FLAG_STUB set) on systems which
608 don't use dbx xrefs (NO_DBX_XREFS in gcc) if a file has a
609 "struct foo *bar" and no complete type for struct foo in that
611 fprintf_filtered (stream, _("<incomplete type>"));
614 case TYPE_CODE_COMPLEX:
616 val_print_scalar_formatted (TYPE_TARGET_TYPE (type),
617 valaddr, embedded_offset,
618 original_value, options, 0, stream);
620 print_floating (valaddr + embedded_offset,
621 TYPE_TARGET_TYPE (type),
623 fprintf_filtered (stream, " + ");
625 val_print_scalar_formatted (TYPE_TARGET_TYPE (type),
628 + TYPE_LENGTH (TYPE_TARGET_TYPE (type)),
632 print_floating (valaddr + embedded_offset
633 + TYPE_LENGTH (TYPE_TARGET_TYPE (type)),
634 TYPE_TARGET_TYPE (type),
636 fprintf_filtered (stream, " * I");
640 error (_("Invalid C/C++ type code %d in symbol table."),
648 c_value_print (struct value *val, struct ui_file *stream,
649 const struct value_print_options *options)
651 struct type *type, *real_type, *val_type;
652 int full, top, using_enc;
653 struct value_print_options opts = *options;
657 /* If it is a pointer, indicate what it points to.
659 Print type also if it is a reference.
661 C++: if it is a member pointer, we will take care
662 of that when we print it. */
664 /* Preserve the original type before stripping typedefs. We prefer
665 to pass down the original type when possible, but for local
666 checks it is better to look past the typedefs. */
667 val_type = value_type (val);
668 type = check_typedef (val_type);
670 if (TYPE_CODE (type) == TYPE_CODE_PTR
671 || TYPE_CODE (type) == TYPE_CODE_REF)
673 /* Hack: remove (char *) for char strings. Their
674 type is indicated by the quoted string anyway.
675 (Don't use c_textual_element_type here; quoted strings
676 are always exactly (char *), (wchar_t *), or the like. */
677 if (TYPE_CODE (val_type) == TYPE_CODE_PTR
678 && TYPE_NAME (val_type) == NULL
679 && TYPE_NAME (TYPE_TARGET_TYPE (val_type)) != NULL
680 && (strcmp (TYPE_NAME (TYPE_TARGET_TYPE (val_type)),
682 || textual_name (TYPE_NAME (TYPE_TARGET_TYPE (val_type)))))
686 else if (options->objectprint
687 && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CLASS))
690 if (TYPE_CODE(type) == TYPE_CODE_REF)
692 /* Copy value, change to pointer, so we don't get an
693 error about a non-pointer type in
694 value_rtti_target_type. */
695 struct value *temparg;
696 temparg=value_copy(val);
697 deprecated_set_value_type
698 (temparg, lookup_pointer_type (TYPE_TARGET_TYPE (type)));
701 /* Pointer to class, check real type of object. */
702 fprintf_filtered (stream, "(");
704 if (value_entirely_available (val))
706 real_type = value_rtti_indirect_type (val, &full, &top,
710 /* RTTI entry found. */
713 /* Need to adjust pointer value. */
714 val = value_from_pointer (type, value_as_address (val) - top);
716 /* Note: When we look up RTTI entries, we don't get
717 any information on const or volatile
721 type_print (type, "", stream, -1);
722 fprintf_filtered (stream, ") ");
728 fprintf_filtered (stream, "(");
729 type_print (value_type (val), "", stream, -1);
730 fprintf_filtered (stream, ") ");
734 if (!value_initialized (val))
735 fprintf_filtered (stream, " [uninitialized] ");
737 if (options->objectprint && (TYPE_CODE (type) == TYPE_CODE_CLASS))
739 /* Attempt to determine real type of object. */
740 real_type = value_rtti_type (val, &full, &top, &using_enc);
743 /* We have RTTI information, so use it. */
744 val = value_full_object (val, real_type,
745 full, top, using_enc);
746 fprintf_filtered (stream, "(%s%s) ",
747 TYPE_NAME (real_type),
748 full ? "" : _(" [incomplete object]"));
749 /* Print out object: enclosing type is same as real_type if
751 val_print (value_enclosing_type (val),
752 value_contents_for_printing (val), 0,
753 value_address (val), stream, 0,
754 val, &opts, current_language);
756 /* Note: When we look up RTTI entries, we don't get any
757 information on const or volatile attributes. */
759 else if (type != check_typedef (value_enclosing_type (val)))
761 /* No RTTI information, so let's do our best. */
762 fprintf_filtered (stream, "(%s ?) ",
763 TYPE_NAME (value_enclosing_type (val)));
764 val_print (value_enclosing_type (val),
765 value_contents_for_printing (val), 0,
766 value_address (val), stream, 0,
767 val, &opts, current_language);
770 /* Otherwise, we end up at the return outside this "if". */
773 val_print (val_type, value_contents_for_printing (val),
774 value_embedded_offset (val),
777 val, &opts, current_language);