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 /* Print function pointer with inferior address ADDRESS onto stdio
38 print_function_pointer_address (struct gdbarch
*gdbarch
,
40 struct ui_file
*stream
,
44 = gdbarch_convert_from_func_ptr_addr (gdbarch
, address
,
47 /* If the function pointer is represented by a description, print
48 the address of the description. */
49 if (addressprint
&& func_addr
!= address
)
51 fputs_filtered ("@", stream
);
52 fputs_filtered (paddress (gdbarch
, address
), stream
);
53 fputs_filtered (": ", stream
);
55 print_address_demangle (gdbarch
, func_addr
, stream
, demangle
);
59 /* A helper for c_textual_element_type. This checks the name of the
60 typedef. This is bogus but it isn't apparent that the compiler
61 provides us the help we may need. */
64 textual_name (const char *name
)
66 return (!strcmp (name
, "wchar_t")
67 || !strcmp (name
, "char16_t")
68 || !strcmp (name
, "char32_t"));
71 /* Apply a heuristic to decide whether an array of TYPE or a pointer
72 to TYPE should be printed as a textual string. Return non-zero if
73 it should, or zero if it should be treated as an array of integers
74 or pointer to integers. FORMAT is the current format letter, or 0
77 We guess that "char" is a character. Explicitly signed and
78 unsigned character types are also characters. Integer data from
79 vector types is not. The user can override this by using the /s
83 c_textual_element_type (struct type
*type
, char format
)
85 struct type
*true_type
, *iter_type
;
87 if (format
!= 0 && format
!= 's')
90 /* We also rely on this for its side effect of setting up all the
92 true_type
= check_typedef (type
);
94 /* TYPE_CODE_CHAR is always textual. */
95 if (TYPE_CODE (true_type
) == TYPE_CODE_CHAR
)
98 /* Any other character-like types must be integral. */
99 if (TYPE_CODE (true_type
) != TYPE_CODE_INT
)
102 /* We peel typedefs one by one, looking for a match. */
106 /* Check the name of the type. */
107 if (TYPE_NAME (iter_type
) && textual_name (TYPE_NAME (iter_type
)))
110 if (TYPE_CODE (iter_type
) != TYPE_CODE_TYPEDEF
)
113 /* Peel a single typedef. If the typedef doesn't have a target
114 type, we use check_typedef and hope the result is ok -- it
115 might be for C++, where wchar_t is a built-in type. */
116 if (TYPE_TARGET_TYPE (iter_type
))
117 iter_type
= TYPE_TARGET_TYPE (iter_type
);
119 iter_type
= check_typedef (iter_type
);
124 /* Print this as a string if we can manage it. For now, no wide
125 character support. */
126 if (TYPE_CODE (true_type
) == TYPE_CODE_INT
127 && TYPE_LENGTH (true_type
) == 1)
132 /* If a one-byte TYPE_CODE_INT is missing the not-a-character
133 flag, then we treat it as text; otherwise, we assume it's
134 being used as data. */
135 if (TYPE_CODE (true_type
) == TYPE_CODE_INT
136 && TYPE_LENGTH (true_type
) == 1
137 && !TYPE_NOTTEXT (true_type
))
144 /* See val_print for a description of the various parameters of this
145 function; they are identical. The semantics of the return value is
146 also identical to val_print. */
149 c_val_print (struct type
*type
, const gdb_byte
*valaddr
,
150 int embedded_offset
, CORE_ADDR address
,
151 struct ui_file
*stream
, int recurse
,
152 const struct value
*original_value
,
153 const struct value_print_options
*options
)
155 struct gdbarch
*gdbarch
= get_type_arch (type
);
156 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
157 unsigned int i
= 0; /* Number of characters printed. */
159 struct type
*elttype
, *unresolved_elttype
;
160 struct type
*unresolved_type
= type
;
165 CHECK_TYPEDEF (type
);
166 switch (TYPE_CODE (type
))
168 case TYPE_CODE_ARRAY
:
169 unresolved_elttype
= TYPE_TARGET_TYPE (type
);
170 elttype
= check_typedef (unresolved_elttype
);
171 if (TYPE_LENGTH (type
) > 0 && TYPE_LENGTH (unresolved_elttype
) > 0)
173 LONGEST low_bound
, high_bound
;
175 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
176 error (_("Could not determine the array high bound"));
178 eltlen
= TYPE_LENGTH (elttype
);
179 len
= high_bound
- low_bound
+ 1;
180 if (options
->prettyprint_arrays
)
182 print_spaces_filtered (2 + 2 * recurse
, stream
);
185 /* Print arrays of textual chars with a string syntax, as
186 long as the entire array is valid. */
187 if (c_textual_element_type (unresolved_elttype
,
189 && value_bytes_available (original_value
, embedded_offset
,
191 && value_bits_valid (original_value
,
192 TARGET_CHAR_BIT
* embedded_offset
,
193 TARGET_CHAR_BIT
* TYPE_LENGTH (type
)))
195 /* If requested, look for the first null char and only
196 print elements up to it. */
197 if (options
->stop_print_at_null
)
199 unsigned int temp_len
;
203 && temp_len
< options
->print_max
204 && extract_unsigned_integer (valaddr
+ embedded_offset
206 eltlen
, byte_order
) != 0);
212 LA_PRINT_STRING (stream
, unresolved_elttype
,
213 valaddr
+ embedded_offset
, len
,
219 fprintf_filtered (stream
, "{");
220 /* If this is a virtual function table, print the 0th
221 entry specially, and the rest of the members
223 if (cp_is_vtbl_ptr_type (elttype
))
226 fprintf_filtered (stream
, _("%d vtable entries"),
233 val_print_array_elements (type
, valaddr
, embedded_offset
,
235 recurse
, original_value
, options
, i
);
236 fprintf_filtered (stream
, "}");
240 /* Array of unspecified length: treat like pointer to first
242 addr
= address
+ embedded_offset
;
243 goto print_unpacked_pointer
;
245 case TYPE_CODE_MEMBERPTR
:
248 val_print_scalar_formatted (type
, valaddr
, embedded_offset
,
249 original_value
, options
, 0, stream
);
252 cp_print_class_member (valaddr
+ embedded_offset
, type
, stream
, "&");
255 case TYPE_CODE_METHODPTR
:
256 cplus_print_method_ptr (valaddr
+ embedded_offset
, type
, stream
);
260 if (options
->format
&& options
->format
!= 's')
262 val_print_scalar_formatted (type
, valaddr
, embedded_offset
,
263 original_value
, options
, 0, stream
);
266 if (options
->vtblprint
&& cp_is_vtbl_ptr_type (type
))
268 /* Print the unmangled name if desired. */
269 /* Print vtable entry - we only get here if we ARE using
270 -fvtable_thunks. (Otherwise, look under
271 TYPE_CODE_STRUCT.) */
273 = extract_typed_address (valaddr
+ embedded_offset
, type
);
275 print_function_pointer_address (gdbarch
, addr
, stream
,
276 options
->addressprint
);
279 unresolved_elttype
= TYPE_TARGET_TYPE (type
);
280 elttype
= check_typedef (unresolved_elttype
);
282 addr
= unpack_pointer (type
, valaddr
+ embedded_offset
);
283 print_unpacked_pointer
:
285 if (TYPE_CODE (elttype
) == TYPE_CODE_FUNC
)
287 /* Try to print what function it points to. */
288 print_function_pointer_address (gdbarch
, addr
, stream
,
289 options
->addressprint
);
290 /* Return value is irrelevant except for string
295 if (options
->addressprint
)
296 fputs_filtered (paddress (gdbarch
, addr
), stream
);
298 /* For a pointer to a textual type, also print the string
299 pointed to, unless pointer is null. */
301 if (c_textual_element_type (unresolved_elttype
,
305 i
= val_print_string (unresolved_elttype
, NULL
,
309 else if (cp_is_vtbl_member (type
))
311 /* Print vtbl's nicely. */
312 CORE_ADDR vt_address
= unpack_pointer (type
,
316 struct minimal_symbol
*msymbol
=
317 lookup_minimal_symbol_by_pc (vt_address
);
318 if ((msymbol
!= NULL
)
319 && (vt_address
== SYMBOL_VALUE_ADDRESS (msymbol
)))
321 fputs_filtered (" <", stream
);
322 fputs_filtered (SYMBOL_PRINT_NAME (msymbol
), stream
);
323 fputs_filtered (">", stream
);
325 if (vt_address
&& options
->vtblprint
)
327 struct value
*vt_val
;
328 struct symbol
*wsym
= (struct symbol
*) NULL
;
330 struct block
*block
= (struct block
*) NULL
;
334 wsym
= lookup_symbol (SYMBOL_LINKAGE_NAME (msymbol
),
340 wtype
= SYMBOL_TYPE (wsym
);
344 wtype
= unresolved_elttype
;
346 vt_val
= value_at (wtype
, vt_address
);
347 common_val_print (vt_val
, stream
, recurse
+ 1,
348 options
, current_language
);
351 fprintf_filtered (stream
, "\n");
352 print_spaces_filtered (2 + 2 * recurse
, stream
);
357 /* Return number of characters printed, including the
358 terminating '\0' if we reached the end. val_print_string
359 takes care including the terminating '\0' if
366 elttype
= check_typedef (TYPE_TARGET_TYPE (type
));
367 if (options
->addressprint
)
370 = extract_typed_address (valaddr
+ embedded_offset
, type
);
372 fprintf_filtered (stream
, "@");
373 fputs_filtered (paddress (gdbarch
, addr
), stream
);
374 if (options
->deref_ref
)
375 fputs_filtered (": ", stream
);
377 /* De-reference the reference. */
378 if (options
->deref_ref
)
380 if (TYPE_CODE (elttype
) != TYPE_CODE_UNDEF
)
382 struct value
*deref_val
;
384 deref_val
= coerce_ref_if_computed (original_value
);
385 if (deref_val
!= NULL
)
387 /* More complicated computed references are not supported. */
388 gdb_assert (embedded_offset
== 0);
391 deref_val
= value_at (TYPE_TARGET_TYPE (type
),
392 unpack_pointer (type
,
394 + embedded_offset
)));
396 common_val_print (deref_val
, stream
, recurse
, options
,
400 fputs_filtered ("???", stream
);
404 case TYPE_CODE_UNION
:
405 if (recurse
&& !options
->unionprint
)
407 fprintf_filtered (stream
, "{...}");
411 case TYPE_CODE_STRUCT
:
412 /*FIXME: Abstract this away. */
413 if (options
->vtblprint
&& cp_is_vtbl_ptr_type (type
))
415 /* Print the unmangled name if desired. */
416 /* Print vtable entry - we only get here if NOT using
417 -fvtable_thunks. (Otherwise, look under
419 int offset
= (embedded_offset
420 + TYPE_FIELD_BITPOS (type
,
421 VTBL_FNADDR_OFFSET
) / 8);
422 struct type
*field_type
= TYPE_FIELD_TYPE (type
,
425 = extract_typed_address (valaddr
+ offset
, field_type
);
427 print_function_pointer_address (gdbarch
, addr
, stream
,
428 options
->addressprint
);
431 cp_print_value_fields_rtti (type
, valaddr
,
432 embedded_offset
, address
,
434 original_value
, options
,
441 val_print_scalar_formatted (type
, valaddr
, embedded_offset
,
442 original_value
, options
, 0, stream
);
445 len
= TYPE_NFIELDS (type
);
446 val
= unpack_long (type
, valaddr
+ embedded_offset
);
447 for (i
= 0; i
< len
; i
++)
450 if (val
== TYPE_FIELD_BITPOS (type
, i
))
457 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
461 print_longest (stream
, 'd', 0, val
);
465 case TYPE_CODE_FLAGS
:
467 val_print_scalar_formatted (type
, valaddr
, embedded_offset
,
468 original_value
, options
, 0, stream
);
470 val_print_type_code_flags (type
, valaddr
+ embedded_offset
,
475 case TYPE_CODE_METHOD
:
478 val_print_scalar_formatted (type
, valaddr
, embedded_offset
,
479 original_value
, options
, 0, stream
);
482 /* FIXME, we should consider, at least for ANSI C language,
483 eliminating the distinction made between FUNCs and POINTERs
485 fprintf_filtered (stream
, "{");
486 type_print (type
, "", stream
, -1);
487 fprintf_filtered (stream
, "} ");
488 /* Try to print what function it points to, and its address. */
489 print_address_demangle (gdbarch
, address
, stream
, demangle
);
493 if (options
->format
|| options
->output_format
)
495 struct value_print_options opts
= *options
;
496 opts
.format
= (options
->format
? options
->format
497 : options
->output_format
);
498 val_print_scalar_formatted (type
, valaddr
, embedded_offset
,
499 original_value
, &opts
, 0, stream
);
503 val
= unpack_long (type
, valaddr
+ embedded_offset
);
505 fputs_filtered ("false", stream
);
507 fputs_filtered ("true", stream
);
509 print_longest (stream
, 'd', 0, val
);
513 case TYPE_CODE_RANGE
:
514 /* FIXME: create_range_type does not set the unsigned bit in a
515 range type (I think it probably should copy it from the
516 target type), so we won't print values which are too large to
517 fit in a signed integer correctly. */
518 /* FIXME: Doesn't handle ranges of enums correctly. (Can't just
519 print with the target type, though, because the size of our
520 type and the target type might differ). */
524 if (options
->format
|| options
->output_format
)
526 struct value_print_options opts
= *options
;
528 opts
.format
= (options
->format
? options
->format
529 : options
->output_format
);
530 val_print_scalar_formatted (type
, valaddr
, embedded_offset
,
531 original_value
, &opts
, 0, stream
);
535 val_print_type_code_int (type
, valaddr
+ embedded_offset
,
537 /* C and C++ has no single byte int type, char is used
538 instead. Since we don't know whether the value is really
539 intended to be used as an integer or a character, print
540 the character equivalent as well. */
541 if (c_textual_element_type (unresolved_type
, options
->format
))
543 fputs_filtered (" ", stream
);
544 LA_PRINT_CHAR (unpack_long (type
, valaddr
+ embedded_offset
),
545 unresolved_type
, stream
);
551 if (options
->format
|| options
->output_format
)
553 struct value_print_options opts
= *options
;
554 opts
.format
= (options
->format
? options
->format
555 : options
->output_format
);
556 val_print_scalar_formatted (type
, valaddr
, embedded_offset
,
557 original_value
, &opts
, 0, stream
);
561 val
= unpack_long (type
, valaddr
+ embedded_offset
);
562 if (TYPE_UNSIGNED (type
))
563 fprintf_filtered (stream
, "%u", (unsigned int) val
);
565 fprintf_filtered (stream
, "%d", (int) val
);
566 fputs_filtered (" ", stream
);
567 LA_PRINT_CHAR (val
, unresolved_type
, stream
);
574 val_print_scalar_formatted (type
, valaddr
, embedded_offset
,
575 original_value
, options
, 0, stream
);
579 print_floating (valaddr
+ embedded_offset
, type
, stream
);
583 case TYPE_CODE_DECFLOAT
:
585 val_print_scalar_formatted (type
, valaddr
, embedded_offset
,
586 original_value
, options
, 0, stream
);
588 print_decimal_floating (valaddr
+ embedded_offset
,
593 fprintf_filtered (stream
, "void");
596 case TYPE_CODE_ERROR
:
597 fprintf_filtered (stream
, "%s", TYPE_ERROR_NAME (type
));
600 case TYPE_CODE_UNDEF
:
601 /* This happens (without TYPE_FLAG_STUB set) on systems which
602 don't use dbx xrefs (NO_DBX_XREFS in gcc) if a file has a
603 "struct foo *bar" and no complete type for struct foo in that
605 fprintf_filtered (stream
, _("<incomplete type>"));
608 case TYPE_CODE_COMPLEX
:
610 val_print_scalar_formatted (TYPE_TARGET_TYPE (type
),
611 valaddr
, embedded_offset
,
612 original_value
, options
, 0, stream
);
614 print_floating (valaddr
+ embedded_offset
,
615 TYPE_TARGET_TYPE (type
),
617 fprintf_filtered (stream
, " + ");
619 val_print_scalar_formatted (TYPE_TARGET_TYPE (type
),
622 + TYPE_LENGTH (TYPE_TARGET_TYPE (type
)),
626 print_floating (valaddr
+ embedded_offset
627 + TYPE_LENGTH (TYPE_TARGET_TYPE (type
)),
628 TYPE_TARGET_TYPE (type
),
630 fprintf_filtered (stream
, " * I");
634 error (_("Invalid C/C++ type code %d in symbol table."),
642 c_value_print (struct value
*val
, struct ui_file
*stream
,
643 const struct value_print_options
*options
)
645 struct type
*type
, *real_type
, *val_type
;
646 int full
, top
, using_enc
;
647 struct value_print_options opts
= *options
;
651 /* If it is a pointer, indicate what it points to.
653 Print type also if it is a reference.
655 C++: if it is a member pointer, we will take care
656 of that when we print it. */
658 /* Preserve the original type before stripping typedefs. We prefer
659 to pass down the original type when possible, but for local
660 checks it is better to look past the typedefs. */
661 val_type
= value_type (val
);
662 type
= check_typedef (val_type
);
664 if (TYPE_CODE (type
) == TYPE_CODE_PTR
665 || TYPE_CODE (type
) == TYPE_CODE_REF
)
667 /* Hack: remove (char *) for char strings. Their
668 type is indicated by the quoted string anyway.
669 (Don't use c_textual_element_type here; quoted strings
670 are always exactly (char *), (wchar_t *), or the like. */
671 if (TYPE_CODE (val_type
) == TYPE_CODE_PTR
672 && TYPE_NAME (val_type
) == NULL
673 && TYPE_NAME (TYPE_TARGET_TYPE (val_type
)) != NULL
674 && (strcmp (TYPE_NAME (TYPE_TARGET_TYPE (val_type
)),
676 || textual_name (TYPE_NAME (TYPE_TARGET_TYPE (val_type
)))))
680 else if (options
->objectprint
681 && (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_CLASS
))
684 if (TYPE_CODE(type
) == TYPE_CODE_REF
)
686 /* Copy value, change to pointer, so we don't get an
687 error about a non-pointer type in
688 value_rtti_target_type. */
689 struct value
*temparg
;
690 temparg
=value_copy(val
);
691 deprecated_set_value_type
692 (temparg
, lookup_pointer_type (TYPE_TARGET_TYPE (type
)));
695 /* Pointer to class, check real type of object. */
696 fprintf_filtered (stream
, "(");
698 if (value_entirely_available (val
))
700 real_type
= value_rtti_target_type (val
, &full
, &top
, &using_enc
);
703 /* RTTI entry found. */
704 if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
706 /* Create a pointer type pointing to the real
708 type
= lookup_pointer_type (real_type
);
712 /* Create a reference type referencing the real
714 type
= lookup_reference_type (real_type
);
716 /* Need to adjust pointer value. */
717 val
= value_from_pointer (type
, value_as_address (val
) - top
);
719 /* Note: When we look up RTTI entries, we don't get
720 any information on const or volatile
724 type_print (type
, "", stream
, -1);
725 fprintf_filtered (stream
, ") ");
731 fprintf_filtered (stream
, "(");
732 type_print (value_type (val
), "", stream
, -1);
733 fprintf_filtered (stream
, ") ");
737 if (!value_initialized (val
))
738 fprintf_filtered (stream
, " [uninitialized] ");
740 if (options
->objectprint
&& (TYPE_CODE (type
) == TYPE_CODE_CLASS
))
742 /* Attempt to determine real type of object. */
743 real_type
= value_rtti_type (val
, &full
, &top
, &using_enc
);
746 /* We have RTTI information, so use it. */
747 val
= value_full_object (val
, real_type
,
748 full
, top
, using_enc
);
749 fprintf_filtered (stream
, "(%s%s) ",
750 TYPE_NAME (real_type
),
751 full
? "" : _(" [incomplete object]"));
752 /* Print out object: enclosing type is same as real_type if
754 return val_print (value_enclosing_type (val
),
755 value_contents_for_printing (val
), 0,
756 value_address (val
), stream
, 0,
757 val
, &opts
, current_language
);
758 /* Note: When we look up RTTI entries, we don't get any
759 information on const or volatile attributes. */
761 else if (type
!= check_typedef (value_enclosing_type (val
)))
763 /* No RTTI information, so let's do our best. */
764 fprintf_filtered (stream
, "(%s ?) ",
765 TYPE_NAME (value_enclosing_type (val
)));
766 return val_print (value_enclosing_type (val
),
767 value_contents_for_printing (val
), 0,
768 value_address (val
), stream
, 0,
769 val
, &opts
, current_language
);
771 /* Otherwise, we end up at the return outside this "if". */
774 return val_print (val_type
, value_contents_for_printing (val
),
775 value_embedded_offset (val
),
778 val
, &opts
, current_language
);