1 /* Support for printing C values for GDB, the GNU debugger.
3 Copyright (C) 1986, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
4 1998, 1999, 2000, 2001, 2003, 2005, 2006, 2007, 2008, 2009, 2010, 2011
5 Free Software Foundation, Inc.
7 This file is part of GDB.
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.
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.
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/>. */
23 #include "gdb_string.h"
26 #include "expression.h"
35 /* Print function pointer with inferior address ADDRESS onto stdio
39 print_function_pointer_address (struct gdbarch *gdbarch,
41 struct ui_file *stream,
45 = gdbarch_convert_from_func_ptr_addr (gdbarch, address,
48 /* If the function pointer is represented by a description, print
49 the address of the description. */
50 if (addressprint && func_addr != address)
52 fputs_filtered ("@", stream);
53 fputs_filtered (paddress (gdbarch, address), stream);
54 fputs_filtered (": ", stream);
56 print_address_demangle (gdbarch, func_addr, stream, demangle);
60 /* A helper for c_textual_element_type. This checks the name of the
61 typedef. This is bogus but it isn't apparent that the compiler
62 provides us the help we may need. */
65 textual_name (const char *name)
67 return (!strcmp (name, "wchar_t")
68 || !strcmp (name, "char16_t")
69 || !strcmp (name, "char32_t"));
72 /* Apply a heuristic to decide whether an array of TYPE or a pointer
73 to TYPE should be printed as a textual string. Return non-zero if
74 it should, or zero if it should be treated as an array of integers
75 or pointer to integers. FORMAT is the current format letter, or 0
78 We guess that "char" is a character. Explicitly signed and
79 unsigned character types are also characters. Integer data from
80 vector types is not. The user can override this by using the /s
84 c_textual_element_type (struct type *type, char format)
86 struct type *true_type, *iter_type;
88 if (format != 0 && format != 's')
91 /* We also rely on this for its side effect of setting up all the
93 true_type = check_typedef (type);
95 /* TYPE_CODE_CHAR is always textual. */
96 if (TYPE_CODE (true_type) == TYPE_CODE_CHAR)
99 /* Any other character-like types must be integral. */
100 if (TYPE_CODE (true_type) != TYPE_CODE_INT)
103 /* We peel typedefs one by one, looking for a match. */
107 /* Check the name of the type. */
108 if (TYPE_NAME (iter_type) && textual_name (TYPE_NAME (iter_type)))
111 if (TYPE_CODE (iter_type) != TYPE_CODE_TYPEDEF)
114 /* Peel a single typedef. If the typedef doesn't have a target
115 type, we use check_typedef and hope the result is ok -- it
116 might be for C++, where wchar_t is a built-in type. */
117 if (TYPE_TARGET_TYPE (iter_type))
118 iter_type = TYPE_TARGET_TYPE (iter_type);
120 iter_type = check_typedef (iter_type);
125 /* Print this as a string if we can manage it. For now, no wide
126 character support. */
127 if (TYPE_CODE (true_type) == TYPE_CODE_INT
128 && TYPE_LENGTH (true_type) == 1)
133 /* If a one-byte TYPE_CODE_INT is missing the not-a-character
134 flag, then we treat it as text; otherwise, we assume it's
135 being used as data. */
136 if (TYPE_CODE (true_type) == TYPE_CODE_INT
137 && TYPE_LENGTH (true_type) == 1
138 && !TYPE_NOTTEXT (true_type))
146 /* Print data of type TYPE located at VALADDR (within GDB), which came
147 from the inferior at address ADDRESS, onto stdio stream STREAM
148 according to OPTIONS. The data at VALADDR is in target byte order.
150 If the data are a string pointer, returns the number of string
151 characters printed. */
154 c_val_print (struct type *type, const gdb_byte *valaddr,
155 int embedded_offset, CORE_ADDR address,
156 struct ui_file *stream, int recurse,
157 const struct value *original_value,
158 const struct value_print_options *options)
160 struct gdbarch *gdbarch = get_type_arch (type);
161 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
162 unsigned int i = 0; /* Number of characters printed. */
164 struct type *elttype, *unresolved_elttype;
165 struct type *unresolved_type = type;
170 CHECK_TYPEDEF (type);
171 switch (TYPE_CODE (type))
173 case TYPE_CODE_ARRAY:
174 unresolved_elttype = TYPE_TARGET_TYPE (type);
175 elttype = check_typedef (unresolved_elttype);
176 if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (unresolved_elttype) > 0)
178 LONGEST low_bound, high_bound;
180 if (!get_array_bounds (type, &low_bound, &high_bound))
181 error (_("Could not determine the array high bound"));
183 eltlen = TYPE_LENGTH (elttype);
184 len = high_bound - low_bound + 1;
185 if (options->prettyprint_arrays)
187 print_spaces_filtered (2 + 2 * recurse, stream);
190 /* Print arrays of textual chars with a string syntax, as
191 long as the entire array is valid. */
192 if (c_textual_element_type (unresolved_elttype,
194 && value_bits_valid (original_value,
195 TARGET_CHAR_BIT * embedded_offset,
196 TARGET_CHAR_BIT * TYPE_LENGTH (type)))
198 /* If requested, look for the first null char and only
199 print elements up to it. */
200 if (options->stop_print_at_null)
202 unsigned int temp_len;
206 && temp_len < options->print_max
207 && extract_unsigned_integer (valaddr + embedded_offset
209 eltlen, byte_order) != 0);
215 LA_PRINT_STRING (stream, unresolved_elttype,
216 valaddr + embedded_offset, len,
222 fprintf_filtered (stream, "{");
223 /* If this is a virtual function table, print the 0th
224 entry specially, and the rest of the members
226 if (cp_is_vtbl_ptr_type (elttype))
229 fprintf_filtered (stream, _("%d vtable entries"),
236 val_print_array_elements (type, valaddr, embedded_offset,
238 recurse, original_value, options, i);
239 fprintf_filtered (stream, "}");
243 /* Array of unspecified length: treat like pointer to first
246 goto print_unpacked_pointer;
248 case TYPE_CODE_MEMBERPTR:
251 print_scalar_formatted (valaddr + embedded_offset, type,
255 cp_print_class_member (valaddr + embedded_offset, type, stream, "&");
258 case TYPE_CODE_METHODPTR:
259 cplus_print_method_ptr (valaddr + embedded_offset, type, stream);
263 if (options->format && options->format != 's')
265 print_scalar_formatted (valaddr + embedded_offset, type,
269 if (options->vtblprint && cp_is_vtbl_ptr_type (type))
271 /* Print the unmangled name if desired. */
272 /* Print vtable entry - we only get here if we ARE using
273 -fvtable_thunks. (Otherwise, look under
274 TYPE_CODE_STRUCT.) */
276 = extract_typed_address (valaddr + embedded_offset, type);
278 print_function_pointer_address (gdbarch, addr, stream,
279 options->addressprint);
282 unresolved_elttype = TYPE_TARGET_TYPE (type);
283 elttype = check_typedef (unresolved_elttype);
285 addr = unpack_pointer (type, valaddr + embedded_offset);
286 print_unpacked_pointer:
288 if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
290 /* Try to print what function it points to. */
291 print_function_pointer_address (gdbarch, addr, stream,
292 options->addressprint);
293 /* Return value is irrelevant except for string
298 if (options->addressprint)
299 fputs_filtered (paddress (gdbarch, addr), stream);
301 /* For a pointer to a textual type, also print the string
302 pointed to, unless pointer is null. */
304 if (c_textual_element_type (unresolved_elttype,
308 i = val_print_string (unresolved_elttype, NULL,
312 else if (cp_is_vtbl_member (type))
314 /* Print vtbl's nicely. */
315 CORE_ADDR vt_address = unpack_pointer (type,
319 struct minimal_symbol *msymbol =
320 lookup_minimal_symbol_by_pc (vt_address);
321 if ((msymbol != NULL)
322 && (vt_address == SYMBOL_VALUE_ADDRESS (msymbol)))
324 fputs_filtered (" <", stream);
325 fputs_filtered (SYMBOL_PRINT_NAME (msymbol), stream);
326 fputs_filtered (">", stream);
328 if (vt_address && options->vtblprint)
330 struct value *vt_val;
331 struct symbol *wsym = (struct symbol *) NULL;
333 struct block *block = (struct block *) NULL;
337 wsym = lookup_symbol (SYMBOL_LINKAGE_NAME (msymbol),
343 wtype = SYMBOL_TYPE (wsym);
347 wtype = unresolved_elttype;
349 vt_val = value_at (wtype, vt_address);
350 common_val_print (vt_val, stream, recurse + 1,
351 options, current_language);
354 fprintf_filtered (stream, "\n");
355 print_spaces_filtered (2 + 2 * recurse, stream);
360 /* Return number of characters printed, including the
361 terminating '\0' if we reached the end. val_print_string
362 takes care including the terminating '\0' if
369 elttype = check_typedef (TYPE_TARGET_TYPE (type));
370 if (options->addressprint)
373 = extract_typed_address (valaddr + embedded_offset, type);
375 fprintf_filtered (stream, "@");
376 fputs_filtered (paddress (gdbarch, addr), stream);
377 if (options->deref_ref)
378 fputs_filtered (": ", stream);
380 /* De-reference the reference. */
381 if (options->deref_ref)
383 if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
385 struct value *deref_val =
387 (TYPE_TARGET_TYPE (type),
388 unpack_pointer (type, valaddr + embedded_offset));
390 common_val_print (deref_val, stream, recurse, options,
394 fputs_filtered ("???", stream);
398 case TYPE_CODE_UNION:
399 if (recurse && !options->unionprint)
401 fprintf_filtered (stream, "{...}");
405 case TYPE_CODE_STRUCT:
406 /*FIXME: Abstract this away. */
407 if (options->vtblprint && cp_is_vtbl_ptr_type (type))
409 /* Print the unmangled name if desired. */
410 /* Print vtable entry - we only get here if NOT using
411 -fvtable_thunks. (Otherwise, look under
413 int offset = (embedded_offset
414 + TYPE_FIELD_BITPOS (type,
415 VTBL_FNADDR_OFFSET) / 8);
416 struct type *field_type = TYPE_FIELD_TYPE (type,
419 = extract_typed_address (valaddr + offset, field_type);
421 print_function_pointer_address (gdbarch, addr, stream,
422 options->addressprint);
425 cp_print_value_fields_rtti (type, valaddr,
426 embedded_offset, address,
428 original_value, options,
435 print_scalar_formatted (valaddr + embedded_offset, type,
439 len = TYPE_NFIELDS (type);
440 val = unpack_long (type, valaddr + embedded_offset);
441 for (i = 0; i < len; i++)
444 if (val == TYPE_FIELD_BITPOS (type, i))
451 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
455 print_longest (stream, 'd', 0, val);
459 case TYPE_CODE_FLAGS:
461 print_scalar_formatted (valaddr + embedded_offset, type,
464 val_print_type_code_flags (type, valaddr + embedded_offset,
469 case TYPE_CODE_METHOD:
472 print_scalar_formatted (valaddr + embedded_offset, type,
476 /* FIXME, we should consider, at least for ANSI C language,
477 eliminating the distinction made between FUNCs and POINTERs
479 fprintf_filtered (stream, "{");
480 type_print (type, "", stream, -1);
481 fprintf_filtered (stream, "} ");
482 /* Try to print what function it points to, and its address. */
483 print_address_demangle (gdbarch, address, stream, demangle);
487 if (options->format || options->output_format)
489 struct value_print_options opts = *options;
490 opts.format = (options->format ? options->format
491 : options->output_format);
492 print_scalar_formatted (valaddr + embedded_offset, type,
497 val = unpack_long (type, valaddr + embedded_offset);
499 fputs_filtered ("false", stream);
501 fputs_filtered ("true", stream);
503 print_longest (stream, 'd', 0, val);
507 case TYPE_CODE_RANGE:
508 /* FIXME: create_range_type does not set the unsigned bit in a
509 range type (I think it probably should copy it from the
510 target type), so we won't print values which are too large to
511 fit in a signed integer correctly. */
512 /* FIXME: Doesn't handle ranges of enums correctly. (Can't just
513 print with the target type, though, because the size of our
514 type and the target type might differ). */
518 if (options->format || options->output_format)
520 struct value_print_options opts = *options;
522 opts.format = (options->format ? options->format
523 : options->output_format);
524 print_scalar_formatted (valaddr + embedded_offset, type,
529 val_print_type_code_int (type, valaddr + embedded_offset,
531 /* C and C++ has no single byte int type, char is used
532 instead. Since we don't know whether the value is really
533 intended to be used as an integer or a character, print
534 the character equivalent as well. */
535 if (c_textual_element_type (unresolved_type, options->format))
537 fputs_filtered (" ", stream);
538 LA_PRINT_CHAR (unpack_long (type, valaddr + embedded_offset),
539 unresolved_type, stream);
545 if (options->format || options->output_format)
547 struct value_print_options opts = *options;
548 opts.format = (options->format ? options->format
549 : options->output_format);
550 print_scalar_formatted (valaddr + embedded_offset, type,
555 val = unpack_long (type, valaddr + embedded_offset);
556 if (TYPE_UNSIGNED (type))
557 fprintf_filtered (stream, "%u", (unsigned int) val);
559 fprintf_filtered (stream, "%d", (int) val);
560 fputs_filtered (" ", stream);
561 LA_PRINT_CHAR (val, unresolved_type, stream);
568 print_scalar_formatted (valaddr + embedded_offset, type,
573 print_floating (valaddr + embedded_offset, type, stream);
577 case TYPE_CODE_DECFLOAT:
579 print_scalar_formatted (valaddr + embedded_offset, type,
582 print_decimal_floating (valaddr + embedded_offset,
587 fprintf_filtered (stream, "void");
590 case TYPE_CODE_ERROR:
591 fprintf_filtered (stream, "%s", TYPE_ERROR_NAME (type));
594 case TYPE_CODE_UNDEF:
595 /* This happens (without TYPE_FLAG_STUB set) on systems which
596 don't use dbx xrefs (NO_DBX_XREFS in gcc) if a file has a
597 "struct foo *bar" and no complete type for struct foo in that
599 fprintf_filtered (stream, _("<incomplete type>"));
602 case TYPE_CODE_COMPLEX:
604 print_scalar_formatted (valaddr + embedded_offset,
605 TYPE_TARGET_TYPE (type),
608 print_floating (valaddr + embedded_offset,
609 TYPE_TARGET_TYPE (type),
611 fprintf_filtered (stream, " + ");
613 print_scalar_formatted (valaddr + embedded_offset
614 + TYPE_LENGTH (TYPE_TARGET_TYPE (type)),
615 TYPE_TARGET_TYPE (type),
618 print_floating (valaddr + embedded_offset
619 + TYPE_LENGTH (TYPE_TARGET_TYPE (type)),
620 TYPE_TARGET_TYPE (type),
622 fprintf_filtered (stream, " * I");
626 error (_("Invalid C/C++ type code %d in symbol table."),
634 c_value_print (struct value *val, struct ui_file *stream,
635 const struct value_print_options *options)
637 struct type *type, *real_type, *val_type;
638 int full, top, using_enc;
639 struct value_print_options opts = *options;
643 /* If it is a pointer, indicate what it points to.
645 Print type also if it is a reference.
647 C++: if it is a member pointer, we will take care
648 of that when we print it. */
650 /* Preserve the original type before stripping typedefs. We prefer
651 to pass down the original type when possible, but for local
652 checks it is better to look past the typedefs. */
653 val_type = value_type (val);
654 type = check_typedef (val_type);
656 if (TYPE_CODE (type) == TYPE_CODE_PTR
657 || TYPE_CODE (type) == TYPE_CODE_REF)
659 /* Hack: remove (char *) for char strings. Their
660 type is indicated by the quoted string anyway.
661 (Don't use c_textual_element_type here; quoted strings
662 are always exactly (char *), (wchar_t *), or the like. */
663 if (TYPE_CODE (val_type) == TYPE_CODE_PTR
664 && TYPE_NAME (val_type) == NULL
665 && TYPE_NAME (TYPE_TARGET_TYPE (val_type)) != NULL
666 && (strcmp (TYPE_NAME (TYPE_TARGET_TYPE (val_type)),
668 || textual_name (TYPE_NAME (TYPE_TARGET_TYPE (val_type)))))
672 else if (options->objectprint
673 && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CLASS))
676 if (TYPE_CODE(type) == TYPE_CODE_REF)
678 /* Copy value, change to pointer, so we don't get an
679 error about a non-pointer type in
680 value_rtti_target_type. */
681 struct value *temparg;
682 temparg=value_copy(val);
683 deprecated_set_value_type
684 (temparg, lookup_pointer_type (TYPE_TARGET_TYPE (type)));
687 /* Pointer to class, check real type of object. */
688 fprintf_filtered (stream, "(");
689 real_type = value_rtti_target_type (val, &full,
693 /* RTTI entry found. */
694 if (TYPE_CODE (type) == TYPE_CODE_PTR)
696 /* Create a pointer type pointing to the real
698 type = lookup_pointer_type (real_type);
702 /* Create a reference type referencing the real
704 type = lookup_reference_type (real_type);
706 /* JYG: Need to adjust pointer value. */
707 /* NOTE: cagney/2005-01-02: THIS IS BOGUS. */
708 value_contents_writeable (val)[0] -= top;
710 /* Note: When we look up RTTI entries, we don't get any
711 information on const or volatile attributes. */
713 type_print (type, "", stream, -1);
714 fprintf_filtered (stream, ") ");
720 fprintf_filtered (stream, "(");
721 type_print (value_type (val), "", stream, -1);
722 fprintf_filtered (stream, ") ");
726 if (!value_initialized (val))
727 fprintf_filtered (stream, " [uninitialized] ");
729 if (options->objectprint && (TYPE_CODE (type) == TYPE_CODE_CLASS))
731 /* Attempt to determine real type of object. */
732 real_type = value_rtti_type (val, &full, &top, &using_enc);
735 /* We have RTTI information, so use it. */
736 val = value_full_object (val, real_type,
737 full, top, using_enc);
738 fprintf_filtered (stream, "(%s%s) ",
739 TYPE_NAME (real_type),
740 full ? "" : _(" [incomplete object]"));
741 /* Print out object: enclosing type is same as real_type if
743 return val_print (value_enclosing_type (val),
744 value_contents_for_printing (val), 0,
745 value_address (val), stream, 0,
746 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 return val_print (value_enclosing_type (val),
756 value_contents_for_printing (val), 0,
757 value_address (val), stream, 0,
758 val, &opts, current_language);
760 /* Otherwise, we end up at the return outside this "if". */
763 return val_print (val_type, value_contents_for_printing (val),
764 value_embedded_offset (val),
767 val, &opts, current_language);