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. */
123 c_val_print (struct type *type, const gdb_byte *valaddr,
124 int embedded_offset, CORE_ADDR address,
125 struct ui_file *stream, int recurse,
126 const struct value *original_value,
127 const struct value_print_options *options)
129 struct gdbarch *gdbarch = get_type_arch (type);
130 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
131 unsigned int i = 0; /* Number of characters printed. */
133 struct type *elttype, *unresolved_elttype;
134 struct type *unresolved_type = type;
139 CHECK_TYPEDEF (type);
140 switch (TYPE_CODE (type))
142 case TYPE_CODE_ARRAY:
143 unresolved_elttype = TYPE_TARGET_TYPE (type);
144 elttype = check_typedef (unresolved_elttype);
145 if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (unresolved_elttype) > 0)
147 LONGEST low_bound, high_bound;
149 if (!get_array_bounds (type, &low_bound, &high_bound))
150 error (_("Could not determine the array high bound"));
152 eltlen = TYPE_LENGTH (elttype);
153 len = high_bound - low_bound + 1;
154 if (options->prettyprint_arrays)
156 print_spaces_filtered (2 + 2 * recurse, stream);
159 /* Print arrays of textual chars with a string syntax, as
160 long as the entire array is valid. */
161 if (c_textual_element_type (unresolved_elttype,
163 && value_bytes_available (original_value, embedded_offset,
165 && value_bits_valid (original_value,
166 TARGET_CHAR_BIT * embedded_offset,
167 TARGET_CHAR_BIT * TYPE_LENGTH (type)))
169 /* If requested, look for the first null char and only
170 print elements up to it. */
171 if (options->stop_print_at_null)
173 unsigned int temp_len;
177 && temp_len < options->print_max
178 && extract_unsigned_integer (valaddr + embedded_offset
180 eltlen, byte_order) != 0);
186 LA_PRINT_STRING (stream, unresolved_elttype,
187 valaddr + embedded_offset, len,
193 fprintf_filtered (stream, "{");
194 /* If this is a virtual function table, print the 0th
195 entry specially, and the rest of the members
197 if (cp_is_vtbl_ptr_type (elttype))
200 fprintf_filtered (stream, _("%d vtable entries"),
207 val_print_array_elements (type, valaddr, embedded_offset,
209 recurse, original_value, options, i);
210 fprintf_filtered (stream, "}");
214 /* Array of unspecified length: treat like pointer to first
216 addr = address + embedded_offset;
217 goto print_unpacked_pointer;
219 case TYPE_CODE_MEMBERPTR:
222 val_print_scalar_formatted (type, valaddr, embedded_offset,
223 original_value, options, 0, stream);
226 cp_print_class_member (valaddr + embedded_offset, type, stream, "&");
229 case TYPE_CODE_METHODPTR:
230 cplus_print_method_ptr (valaddr + embedded_offset, type, stream);
234 if (options->format && options->format != 's')
236 val_print_scalar_formatted (type, valaddr, embedded_offset,
237 original_value, options, 0, stream);
240 if (options->vtblprint && cp_is_vtbl_ptr_type (type))
242 /* Print the unmangled name if desired. */
243 /* Print vtable entry - we only get here if we ARE using
244 -fvtable_thunks. (Otherwise, look under
245 TYPE_CODE_STRUCT.) */
247 = extract_typed_address (valaddr + embedded_offset, type);
249 print_function_pointer_address (gdbarch, addr, stream,
250 options->addressprint);
253 unresolved_elttype = TYPE_TARGET_TYPE (type);
254 elttype = check_typedef (unresolved_elttype);
256 addr = unpack_pointer (type, valaddr + embedded_offset);
257 print_unpacked_pointer:
259 if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
261 /* Try to print what function it points to. */
262 print_function_pointer_address (gdbarch, addr, stream,
263 options->addressprint);
267 if (options->addressprint)
268 fputs_filtered (paddress (gdbarch, addr), stream);
270 /* For a pointer to a textual type, also print the string
271 pointed to, unless pointer is null. */
273 if (c_textual_element_type (unresolved_elttype,
277 i = val_print_string (unresolved_elttype, NULL,
281 else if (cp_is_vtbl_member (type))
283 /* Print vtbl's nicely. */
284 CORE_ADDR vt_address = unpack_pointer (type,
288 struct minimal_symbol *msymbol =
289 lookup_minimal_symbol_by_pc (vt_address);
290 if ((msymbol != NULL)
291 && (vt_address == SYMBOL_VALUE_ADDRESS (msymbol)))
293 fputs_filtered (" <", stream);
294 fputs_filtered (SYMBOL_PRINT_NAME (msymbol), stream);
295 fputs_filtered (">", stream);
297 if (vt_address && options->vtblprint)
299 struct value *vt_val;
300 struct symbol *wsym = (struct symbol *) NULL;
302 struct block *block = (struct block *) NULL;
306 wsym = lookup_symbol (SYMBOL_LINKAGE_NAME (msymbol),
312 wtype = SYMBOL_TYPE (wsym);
316 wtype = unresolved_elttype;
318 vt_val = value_at (wtype, vt_address);
319 common_val_print (vt_val, stream, recurse + 1,
320 options, current_language);
323 fprintf_filtered (stream, "\n");
324 print_spaces_filtered (2 + 2 * recurse, stream);
333 elttype = check_typedef (TYPE_TARGET_TYPE (type));
334 if (options->addressprint)
337 = extract_typed_address (valaddr + embedded_offset, type);
339 fprintf_filtered (stream, "@");
340 fputs_filtered (paddress (gdbarch, addr), stream);
341 if (options->deref_ref)
342 fputs_filtered (": ", stream);
344 /* De-reference the reference. */
345 if (options->deref_ref)
347 if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
349 struct value *deref_val;
351 deref_val = coerce_ref_if_computed (original_value);
352 if (deref_val != NULL)
354 /* More complicated computed references are not supported. */
355 gdb_assert (embedded_offset == 0);
358 deref_val = value_at (TYPE_TARGET_TYPE (type),
359 unpack_pointer (type,
361 + embedded_offset)));
363 common_val_print (deref_val, stream, recurse, options,
367 fputs_filtered ("???", stream);
371 case TYPE_CODE_UNION:
372 if (recurse && !options->unionprint)
374 fprintf_filtered (stream, "{...}");
378 case TYPE_CODE_STRUCT:
379 /*FIXME: Abstract this away. */
380 if (options->vtblprint && cp_is_vtbl_ptr_type (type))
382 /* Print the unmangled name if desired. */
383 /* Print vtable entry - we only get here if NOT using
384 -fvtable_thunks. (Otherwise, look under
386 int offset = (embedded_offset
387 + TYPE_FIELD_BITPOS (type,
388 VTBL_FNADDR_OFFSET) / 8);
389 struct type *field_type = TYPE_FIELD_TYPE (type,
392 = extract_typed_address (valaddr + offset, field_type);
394 print_function_pointer_address (gdbarch, addr, stream,
395 options->addressprint);
398 cp_print_value_fields_rtti (type, valaddr,
399 embedded_offset, address,
401 original_value, options,
408 val_print_scalar_formatted (type, valaddr, embedded_offset,
409 original_value, options, 0, stream);
412 len = TYPE_NFIELDS (type);
413 val = unpack_long (type, valaddr + embedded_offset);
414 for (i = 0; i < len; i++)
417 if (val == TYPE_FIELD_BITPOS (type, i))
424 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
426 else if (TYPE_FLAG_ENUM (type))
430 /* We have a "flag" enum, so we try to decompose it into
431 pieces as appropriate. A flag enum has disjoint
432 constants by definition. */
433 fputs_filtered ("(", stream);
434 for (i = 0; i < len; ++i)
438 if ((val & TYPE_FIELD_BITPOS (type, i)) != 0)
441 fputs_filtered (" | ", stream);
444 val &= ~TYPE_FIELD_BITPOS (type, i);
445 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
449 if (first || val != 0)
452 fputs_filtered (" | ", stream);
453 fputs_filtered ("unknown: ", stream);
454 print_longest (stream, 'd', 0, val);
457 fputs_filtered (")", stream);
460 print_longest (stream, 'd', 0, val);
463 case TYPE_CODE_FLAGS:
465 val_print_scalar_formatted (type, valaddr, embedded_offset,
466 original_value, options, 0, stream);
468 val_print_type_code_flags (type, valaddr + embedded_offset,
473 case TYPE_CODE_METHOD:
476 val_print_scalar_formatted (type, valaddr, embedded_offset,
477 original_value, options, 0, stream);
480 /* FIXME, we should consider, at least for ANSI C language,
481 eliminating the distinction made between FUNCs and POINTERs
483 fprintf_filtered (stream, "{");
484 type_print (type, "", stream, -1);
485 fprintf_filtered (stream, "} ");
486 /* Try to print what function it points to, and its address. */
487 print_address_demangle (gdbarch, address, stream, demangle);
491 if (options->format || options->output_format)
493 struct value_print_options opts = *options;
494 opts.format = (options->format ? options->format
495 : options->output_format);
496 val_print_scalar_formatted (type, valaddr, embedded_offset,
497 original_value, &opts, 0, stream);
501 val = unpack_long (type, valaddr + embedded_offset);
503 fputs_filtered ("false", stream);
505 fputs_filtered ("true", stream);
507 print_longest (stream, 'd', 0, val);
511 case TYPE_CODE_RANGE:
512 /* FIXME: create_range_type does not set the unsigned bit in a
513 range type (I think it probably should copy it from the
514 target type), so we won't print values which are too large to
515 fit in a signed integer correctly. */
516 /* FIXME: Doesn't handle ranges of enums correctly. (Can't just
517 print with the target type, though, because the size of our
518 type and the target type might differ). */
522 if (options->format || options->output_format)
524 struct value_print_options opts = *options;
526 opts.format = (options->format ? options->format
527 : options->output_format);
528 val_print_scalar_formatted (type, valaddr, embedded_offset,
529 original_value, &opts, 0, stream);
533 val_print_type_code_int (type, valaddr + embedded_offset,
535 /* C and C++ has no single byte int type, char is used
536 instead. Since we don't know whether the value is really
537 intended to be used as an integer or a character, print
538 the character equivalent as well. */
539 if (c_textual_element_type (unresolved_type, options->format))
541 fputs_filtered (" ", stream);
542 LA_PRINT_CHAR (unpack_long (type, valaddr + embedded_offset),
543 unresolved_type, stream);
549 if (options->format || options->output_format)
551 struct value_print_options opts = *options;
552 opts.format = (options->format ? options->format
553 : options->output_format);
554 val_print_scalar_formatted (type, valaddr, embedded_offset,
555 original_value, &opts, 0, stream);
559 val = unpack_long (type, valaddr + embedded_offset);
560 if (TYPE_UNSIGNED (type))
561 fprintf_filtered (stream, "%u", (unsigned int) val);
563 fprintf_filtered (stream, "%d", (int) val);
564 fputs_filtered (" ", stream);
565 LA_PRINT_CHAR (val, unresolved_type, stream);
572 val_print_scalar_formatted (type, valaddr, embedded_offset,
573 original_value, options, 0, stream);
577 print_floating (valaddr + embedded_offset, type, stream);
581 case TYPE_CODE_DECFLOAT:
583 val_print_scalar_formatted (type, valaddr, embedded_offset,
584 original_value, options, 0, stream);
586 print_decimal_floating (valaddr + embedded_offset,
591 fprintf_filtered (stream, "void");
594 case TYPE_CODE_ERROR:
595 fprintf_filtered (stream, "%s", TYPE_ERROR_NAME (type));
598 case TYPE_CODE_UNDEF:
599 /* This happens (without TYPE_FLAG_STUB set) on systems which
600 don't use dbx xrefs (NO_DBX_XREFS in gcc) if a file has a
601 "struct foo *bar" and no complete type for struct foo in that
603 fprintf_filtered (stream, _("<incomplete type>"));
606 case TYPE_CODE_COMPLEX:
608 val_print_scalar_formatted (TYPE_TARGET_TYPE (type),
609 valaddr, embedded_offset,
610 original_value, options, 0, stream);
612 print_floating (valaddr + embedded_offset,
613 TYPE_TARGET_TYPE (type),
615 fprintf_filtered (stream, " + ");
617 val_print_scalar_formatted (TYPE_TARGET_TYPE (type),
620 + TYPE_LENGTH (TYPE_TARGET_TYPE (type)),
624 print_floating (valaddr + embedded_offset
625 + TYPE_LENGTH (TYPE_TARGET_TYPE (type)),
626 TYPE_TARGET_TYPE (type),
628 fprintf_filtered (stream, " * I");
632 error (_("Invalid C/C++ type code %d in symbol table."),
639 c_value_print (struct value *val, struct ui_file *stream,
640 const struct value_print_options *options)
642 struct type *type, *real_type, *val_type;
643 int full, top, using_enc;
644 struct value_print_options opts = *options;
648 /* If it is a pointer, indicate what it points to.
650 Print type also if it is a reference.
652 C++: if it is a member pointer, we will take care
653 of that when we print it. */
655 /* Preserve the original type before stripping typedefs. We prefer
656 to pass down the original type when possible, but for local
657 checks it is better to look past the typedefs. */
658 val_type = value_type (val);
659 type = check_typedef (val_type);
661 if (TYPE_CODE (type) == TYPE_CODE_PTR
662 || TYPE_CODE (type) == TYPE_CODE_REF)
664 /* Hack: remove (char *) for char strings. Their
665 type is indicated by the quoted string anyway.
666 (Don't use c_textual_element_type here; quoted strings
667 are always exactly (char *), (wchar_t *), or the like. */
668 if (TYPE_CODE (val_type) == TYPE_CODE_PTR
669 && TYPE_NAME (val_type) == NULL
670 && TYPE_NAME (TYPE_TARGET_TYPE (val_type)) != NULL
671 && (strcmp (TYPE_NAME (TYPE_TARGET_TYPE (val_type)),
673 || textual_name (TYPE_NAME (TYPE_TARGET_TYPE (val_type)))))
677 else if (options->objectprint
678 && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CLASS))
681 if (TYPE_CODE(type) == TYPE_CODE_REF)
683 /* Copy value, change to pointer, so we don't get an
684 error about a non-pointer type in
685 value_rtti_target_type. */
686 struct value *temparg;
687 temparg=value_copy(val);
688 deprecated_set_value_type
689 (temparg, lookup_pointer_type (TYPE_TARGET_TYPE (type)));
692 /* Pointer to class, check real type of object. */
693 fprintf_filtered (stream, "(");
695 if (value_entirely_available (val))
697 real_type = value_rtti_indirect_type (val, &full, &top,
701 /* RTTI entry found. */
704 /* Need to adjust pointer value. */
705 val = value_from_pointer (type, value_as_address (val) - top);
707 /* Note: When we look up RTTI entries, we don't get
708 any information on const or volatile
712 type_print (type, "", stream, -1);
713 fprintf_filtered (stream, ") ");
719 fprintf_filtered (stream, "(");
720 type_print (value_type (val), "", stream, -1);
721 fprintf_filtered (stream, ") ");
725 if (!value_initialized (val))
726 fprintf_filtered (stream, " [uninitialized] ");
728 if (options->objectprint && (TYPE_CODE (type) == TYPE_CODE_CLASS))
730 /* Attempt to determine real type of object. */
731 real_type = value_rtti_type (val, &full, &top, &using_enc);
734 /* We have RTTI information, so use it. */
735 val = value_full_object (val, real_type,
736 full, top, using_enc);
737 fprintf_filtered (stream, "(%s%s) ",
738 TYPE_NAME (real_type),
739 full ? "" : _(" [incomplete object]"));
740 /* Print out object: enclosing type is same as real_type if
742 val_print (value_enclosing_type (val),
743 value_contents_for_printing (val), 0,
744 value_address (val), stream, 0,
745 val, &opts, current_language);
747 /* Note: When we look up RTTI entries, we don't get any
748 information on const or volatile attributes. */
750 else if (type != check_typedef (value_enclosing_type (val)))
752 /* No RTTI information, so let's do our best. */
753 fprintf_filtered (stream, "(%s ?) ",
754 TYPE_NAME (value_enclosing_type (val)));
755 val_print (value_enclosing_type (val),
756 value_contents_for_printing (val), 0,
757 value_address (val), stream, 0,
758 val, &opts, current_language);
761 /* Otherwise, we end up at the return outside this "if". */
764 val_print (val_type, value_contents_for_printing (val),
765 value_embedded_offset (val),
768 val, &opts, current_language);