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
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 (CORE_ADDR address
, struct ui_file
*stream
,
42 CORE_ADDR func_addr
= gdbarch_convert_from_func_ptr_addr (current_gdbarch
,
46 /* If the function pointer is represented by a description, print the
47 address of the description. */
48 if (addressprint
&& func_addr
!= address
)
50 fputs_filtered ("@", stream
);
51 fputs_filtered (paddress (address
), stream
);
52 fputs_filtered (": ", stream
);
54 print_address_demangle (func_addr
, stream
, demangle
);
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. */
63 textual_name (const char *name
)
65 return (!strcmp (name
, "wchar_t")
66 || !strcmp (name
, "char16_t")
67 || !strcmp (name
, "char32_t"));
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,
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
82 textual_element_type (struct type
*type
, char format
)
84 struct type
*true_type
= check_typedef (type
);
86 if (format
!= 0 && format
!= 's')
89 /* TYPE_CODE_CHAR is always textual. */
90 if (TYPE_CODE (true_type
) == TYPE_CODE_CHAR
)
92 /* Any other character-like types must be integral. */
93 if (TYPE_CODE (true_type
) != TYPE_CODE_INT
)
96 /* Check the names of the type and the typedef. */
97 if (TYPE_NAME (type
) && textual_name (TYPE_NAME (type
)))
99 if (TYPE_NAME (true_type
) && textual_name (TYPE_NAME (true_type
)))
104 /* Print this as a string if we can manage it. For now, no
105 wide character support. */
106 if (TYPE_CODE (true_type
) == TYPE_CODE_INT
107 && TYPE_LENGTH (true_type
) == 1)
112 /* If a one-byte TYPE_CODE_INT is missing the not-a-character
113 flag, then we treat it as text; otherwise, we assume it's
114 being used as data. */
115 if (TYPE_CODE (true_type
) == TYPE_CODE_INT
116 && TYPE_LENGTH (true_type
) == 1
117 && !TYPE_NOTTEXT (true_type
))
125 /* Print data of type TYPE located at VALADDR (within GDB), which came from
126 the inferior at address ADDRESS, onto stdio stream STREAM according to
127 OPTIONS. The data at VALADDR is in target byte order.
129 If the data are a string pointer, returns the number of string characters
133 c_val_print (struct type
*type
, const gdb_byte
*valaddr
, int embedded_offset
,
134 CORE_ADDR address
, struct ui_file
*stream
, int recurse
,
135 const struct value_print_options
*options
)
137 unsigned int i
= 0; /* Number of characters printed */
139 struct type
*elttype
, *unresolved_elttype
;
140 struct type
*unresolved_type
= type
;
145 CHECK_TYPEDEF (type
);
146 switch (TYPE_CODE (type
))
148 case TYPE_CODE_ARRAY
:
149 unresolved_elttype
= TYPE_TARGET_TYPE (type
);
150 elttype
= check_typedef (unresolved_elttype
);
151 if (TYPE_LENGTH (type
) > 0 && TYPE_LENGTH (unresolved_elttype
) > 0)
153 eltlen
= TYPE_LENGTH (elttype
);
154 len
= TYPE_LENGTH (type
) / eltlen
;
155 if (options
->prettyprint_arrays
)
157 print_spaces_filtered (2 + 2 * recurse
, stream
);
160 /* Print arrays of textual chars with a string syntax. */
161 if (textual_element_type (unresolved_elttype
, options
->format
))
163 /* If requested, look for the first null char and only print
164 elements up to it. */
165 if (options
->stop_print_at_null
)
167 unsigned int temp_len
;
171 && temp_len
< options
->print_max
172 && extract_unsigned_integer (valaddr
+ embedded_offset
180 LA_PRINT_STRING (stream
, unresolved_elttype
,
181 valaddr
+ embedded_offset
, len
, 0, options
);
186 fprintf_filtered (stream
, "{");
187 /* If this is a virtual function table, print the 0th
188 entry specially, and the rest of the members normally. */
189 if (cp_is_vtbl_ptr_type (elttype
))
192 fprintf_filtered (stream
, _("%d vtable entries"), len
- 1);
198 val_print_array_elements (type
, valaddr
+ embedded_offset
, address
, stream
,
199 recurse
, options
, i
);
200 fprintf_filtered (stream
, "}");
204 /* Array of unspecified length: treat like pointer to first elt. */
206 goto print_unpacked_pointer
;
208 case TYPE_CODE_MEMBERPTR
:
211 print_scalar_formatted (valaddr
+ embedded_offset
, type
,
215 cp_print_class_member (valaddr
+ embedded_offset
, type
, stream
, "&");
218 case TYPE_CODE_METHODPTR
:
219 cplus_print_method_ptr (valaddr
+ embedded_offset
, type
, stream
);
223 if (options
->format
&& options
->format
!= 's')
225 print_scalar_formatted (valaddr
+ embedded_offset
, type
,
229 if (options
->vtblprint
&& cp_is_vtbl_ptr_type (type
))
231 /* Print the unmangled name if desired. */
232 /* Print vtable entry - we only get here if we ARE using
233 -fvtable_thunks. (Otherwise, look under TYPE_CODE_STRUCT.) */
235 = extract_typed_address (valaddr
+ embedded_offset
, type
);
236 print_function_pointer_address (addr
, stream
, options
->addressprint
);
239 unresolved_elttype
= TYPE_TARGET_TYPE (type
);
240 elttype
= check_typedef (unresolved_elttype
);
242 addr
= unpack_pointer (type
, valaddr
+ embedded_offset
);
243 print_unpacked_pointer
:
245 if (TYPE_CODE (elttype
) == TYPE_CODE_FUNC
)
247 /* Try to print what function it points to. */
248 print_function_pointer_address (addr
, stream
,
249 options
->addressprint
);
250 /* Return value is irrelevant except for string pointers. */
254 if (options
->addressprint
)
255 fputs_filtered (paddress (addr
), stream
);
257 /* For a pointer to a textual type, also print the string
258 pointed to, unless pointer is null. */
260 if (textual_element_type (unresolved_elttype
, options
->format
)
263 i
= val_print_string (unresolved_elttype
, addr
, -1, stream
,
266 else if (cp_is_vtbl_member (type
))
268 /* print vtbl's nicely */
269 CORE_ADDR vt_address
= unpack_pointer (type
, valaddr
+ embedded_offset
);
271 struct minimal_symbol
*msymbol
=
272 lookup_minimal_symbol_by_pc (vt_address
);
273 if ((msymbol
!= NULL
) &&
274 (vt_address
== SYMBOL_VALUE_ADDRESS (msymbol
)))
276 fputs_filtered (" <", stream
);
277 fputs_filtered (SYMBOL_PRINT_NAME (msymbol
), stream
);
278 fputs_filtered (">", stream
);
280 if (vt_address
&& options
->vtblprint
)
282 struct value
*vt_val
;
283 struct symbol
*wsym
= (struct symbol
*) NULL
;
285 struct block
*block
= (struct block
*) NULL
;
289 wsym
= lookup_symbol (SYMBOL_LINKAGE_NAME (msymbol
), block
,
290 VAR_DOMAIN
, &is_this_fld
);
294 wtype
= SYMBOL_TYPE (wsym
);
298 wtype
= unresolved_elttype
;
300 vt_val
= value_at (wtype
, vt_address
);
301 common_val_print (vt_val
, stream
, recurse
+ 1, options
,
305 fprintf_filtered (stream
, "\n");
306 print_spaces_filtered (2 + 2 * recurse
, stream
);
311 /* Return number of characters printed, including the terminating
312 '\0' if we reached the end. val_print_string takes care including
313 the terminating '\0' if necessary. */
319 elttype
= check_typedef (TYPE_TARGET_TYPE (type
));
320 if (options
->addressprint
)
323 = extract_typed_address (valaddr
+ embedded_offset
, type
);
324 fprintf_filtered (stream
, "@");
325 fputs_filtered (paddress (addr
), stream
);
326 if (options
->deref_ref
)
327 fputs_filtered (": ", stream
);
329 /* De-reference the reference. */
330 if (options
->deref_ref
)
332 if (TYPE_CODE (elttype
) != TYPE_CODE_UNDEF
)
334 struct value
*deref_val
=
336 (TYPE_TARGET_TYPE (type
),
337 unpack_pointer (type
, valaddr
+ embedded_offset
));
338 common_val_print (deref_val
, stream
, recurse
, options
,
342 fputs_filtered ("???", stream
);
346 case TYPE_CODE_UNION
:
347 if (recurse
&& !options
->unionprint
)
349 fprintf_filtered (stream
, "{...}");
353 case TYPE_CODE_STRUCT
:
354 /*FIXME: Abstract this away */
355 if (options
->vtblprint
&& cp_is_vtbl_ptr_type (type
))
357 /* Print the unmangled name if desired. */
358 /* Print vtable entry - we only get here if NOT using
359 -fvtable_thunks. (Otherwise, look under TYPE_CODE_PTR.) */
360 int offset
= (embedded_offset
+
361 TYPE_FIELD_BITPOS (type
, VTBL_FNADDR_OFFSET
) / 8);
362 struct type
*field_type
= TYPE_FIELD_TYPE (type
, VTBL_FNADDR_OFFSET
);
364 = extract_typed_address (valaddr
+ offset
, field_type
);
366 print_function_pointer_address (addr
, stream
, options
->addressprint
);
369 cp_print_value_fields (type
, type
, valaddr
, embedded_offset
, address
, stream
,
370 recurse
, options
, NULL
, 0);
376 print_scalar_formatted (valaddr
+ embedded_offset
, type
,
380 len
= TYPE_NFIELDS (type
);
381 val
= unpack_long (type
, valaddr
+ embedded_offset
);
382 for (i
= 0; i
< len
; i
++)
385 if (val
== TYPE_FIELD_BITPOS (type
, i
))
392 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
396 print_longest (stream
, 'd', 0, val
);
400 case TYPE_CODE_FLAGS
:
402 print_scalar_formatted (valaddr
+ embedded_offset
, type
,
405 val_print_type_code_flags (type
, valaddr
+ embedded_offset
, stream
);
409 case TYPE_CODE_METHOD
:
412 print_scalar_formatted (valaddr
+ embedded_offset
, type
,
416 /* FIXME, we should consider, at least for ANSI C language, eliminating
417 the distinction made between FUNCs and POINTERs to FUNCs. */
418 fprintf_filtered (stream
, "{");
419 type_print (type
, "", stream
, -1);
420 fprintf_filtered (stream
, "} ");
421 /* Try to print what function it points to, and its address. */
422 print_address_demangle (address
, stream
, demangle
);
426 if (options
->format
|| options
->output_format
)
428 struct value_print_options opts
= *options
;
429 opts
.format
= (options
->format
? options
->format
430 : options
->output_format
);
431 print_scalar_formatted (valaddr
+ embedded_offset
, type
,
436 val
= unpack_long (type
, valaddr
+ embedded_offset
);
438 fputs_filtered ("false", stream
);
440 fputs_filtered ("true", stream
);
442 print_longest (stream
, 'd', 0, val
);
446 case TYPE_CODE_RANGE
:
447 /* FIXME: create_range_type does not set the unsigned bit in a
448 range type (I think it probably should copy it from the target
449 type), so we won't print values which are too large to
450 fit in a signed integer correctly. */
451 /* FIXME: Doesn't handle ranges of enums correctly. (Can't just
452 print with the target type, though, because the size of our type
453 and the target type might differ). */
457 if (options
->format
|| options
->output_format
)
459 struct value_print_options opts
= *options
;
460 opts
.format
= (options
->format
? options
->format
461 : options
->output_format
);
462 print_scalar_formatted (valaddr
+ embedded_offset
, type
,
467 val_print_type_code_int (type
, valaddr
+ embedded_offset
, stream
);
468 /* C and C++ has no single byte int type, char is used instead.
469 Since we don't know whether the value is really intended to
470 be used as an integer or a character, print the character
471 equivalent as well. */
472 if (textual_element_type (unresolved_type
, options
->format
))
474 fputs_filtered (" ", stream
);
475 LA_PRINT_CHAR ((unsigned char) unpack_long (type
, valaddr
+ embedded_offset
),
476 unresolved_type
, stream
);
482 if (options
->format
|| options
->output_format
)
484 struct value_print_options opts
= *options
;
485 opts
.format
= (options
->format
? options
->format
486 : options
->output_format
);
487 print_scalar_formatted (valaddr
+ embedded_offset
, type
,
492 val
= unpack_long (type
, valaddr
+ embedded_offset
);
493 if (TYPE_UNSIGNED (type
))
494 fprintf_filtered (stream
, "%u", (unsigned int) val
);
496 fprintf_filtered (stream
, "%d", (int) val
);
497 fputs_filtered (" ", stream
);
498 LA_PRINT_CHAR ((unsigned char) val
, unresolved_type
, stream
);
505 print_scalar_formatted (valaddr
+ embedded_offset
, type
,
510 print_floating (valaddr
+ embedded_offset
, type
, stream
);
514 case TYPE_CODE_DECFLOAT
:
516 print_scalar_formatted (valaddr
+ embedded_offset
, type
,
519 print_decimal_floating (valaddr
+ embedded_offset
, type
, stream
);
523 fprintf_filtered (stream
, "void");
526 case TYPE_CODE_ERROR
:
527 fprintf_filtered (stream
, _("<error type>"));
530 case TYPE_CODE_UNDEF
:
531 /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
532 dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
533 and no complete type for struct foo in that file. */
534 fprintf_filtered (stream
, _("<incomplete type>"));
537 case TYPE_CODE_COMPLEX
:
539 print_scalar_formatted (valaddr
+ embedded_offset
,
540 TYPE_TARGET_TYPE (type
),
543 print_floating (valaddr
+ embedded_offset
, TYPE_TARGET_TYPE (type
),
545 fprintf_filtered (stream
, " + ");
547 print_scalar_formatted (valaddr
+ embedded_offset
548 + TYPE_LENGTH (TYPE_TARGET_TYPE (type
)),
549 TYPE_TARGET_TYPE (type
),
552 print_floating (valaddr
+ embedded_offset
553 + TYPE_LENGTH (TYPE_TARGET_TYPE (type
)),
554 TYPE_TARGET_TYPE (type
),
556 fprintf_filtered (stream
, " * I");
560 error (_("Invalid C/C++ type code %d in symbol table."), TYPE_CODE (type
));
567 c_value_print (struct value
*val
, struct ui_file
*stream
,
568 const struct value_print_options
*options
)
570 struct type
*type
, *real_type
, *val_type
;
571 int full
, top
, using_enc
;
572 struct value_print_options opts
= *options
;
576 /* If it is a pointer, indicate what it points to.
578 Print type also if it is a reference.
580 C++: if it is a member pointer, we will take care
581 of that when we print it. */
583 /* Preserve the original type before stripping typedefs. We prefer
584 to pass down the original type when possible, but for local
585 checks it is better to look past the typedefs. */
586 val_type
= value_type (val
);
587 type
= check_typedef (val_type
);
589 if (TYPE_CODE (type
) == TYPE_CODE_PTR
590 || TYPE_CODE (type
) == TYPE_CODE_REF
)
592 /* Hack: remove (char *) for char strings. Their
593 type is indicated by the quoted string anyway.
594 (Don't use textual_element_type here; quoted strings
595 are always exactly (char *), (wchar_t *), or the like. */
596 if (TYPE_CODE (val_type
) == TYPE_CODE_PTR
597 && TYPE_NAME (val_type
) == NULL
598 && TYPE_NAME (TYPE_TARGET_TYPE (val_type
)) != NULL
599 && (strcmp (TYPE_NAME (TYPE_TARGET_TYPE (val_type
)), "char") == 0
600 || textual_name (TYPE_NAME (TYPE_TARGET_TYPE (val_type
)))))
604 else if (options
->objectprint
605 && (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_CLASS
))
608 if (TYPE_CODE(type
) == TYPE_CODE_REF
)
610 /* Copy value, change to pointer, so we don't get an
611 * error about a non-pointer type in value_rtti_target_type
613 struct value
*temparg
;
614 temparg
=value_copy(val
);
615 deprecated_set_value_type (temparg
, lookup_pointer_type (TYPE_TARGET_TYPE(type
)));
618 /* Pointer to class, check real type of object */
619 fprintf_filtered (stream
, "(");
620 real_type
= value_rtti_target_type (val
, &full
, &top
, &using_enc
);
623 /* RTTI entry found */
624 if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
626 /* create a pointer type pointing to the real type */
627 type
= lookup_pointer_type (real_type
);
631 /* create a reference type referencing the real type */
632 type
= lookup_reference_type (real_type
);
634 /* JYG: Need to adjust pointer value. */
635 /* NOTE: cagney/2005-01-02: THIS IS BOGUS. */
636 value_contents_writeable (val
)[0] -= top
;
638 /* Note: When we look up RTTI entries, we don't get any
639 information on const or volatile attributes */
641 type_print (type
, "", stream
, -1);
642 fprintf_filtered (stream
, ") ");
648 fprintf_filtered (stream
, "(");
649 type_print (value_type (val
), "", stream
, -1);
650 fprintf_filtered (stream
, ") ");
654 if (!value_initialized (val
))
655 fprintf_filtered (stream
, " [uninitialized] ");
657 if (options
->objectprint
&& (TYPE_CODE (type
) == TYPE_CODE_CLASS
))
659 /* Attempt to determine real type of object */
660 real_type
= value_rtti_type (val
, &full
, &top
, &using_enc
);
663 /* We have RTTI information, so use it */
664 val
= value_full_object (val
, real_type
, full
, top
, using_enc
);
665 fprintf_filtered (stream
, "(%s%s) ",
666 TYPE_NAME (real_type
),
667 full
? "" : _(" [incomplete object]"));
668 /* Print out object: enclosing type is same as real_type if full */
669 return val_print (value_enclosing_type (val
),
670 value_contents_all (val
), 0,
671 VALUE_ADDRESS (val
), stream
, 0,
672 &opts
, current_language
);
673 /* Note: When we look up RTTI entries, we don't get any information on
674 const or volatile attributes */
676 else if (type
!= check_typedef (value_enclosing_type (val
)))
678 /* No RTTI information, so let's do our best */
679 fprintf_filtered (stream
, "(%s ?) ",
680 TYPE_NAME (value_enclosing_type (val
)));
681 return val_print (value_enclosing_type (val
),
682 value_contents_all (val
), 0,
683 VALUE_ADDRESS (val
), stream
, 0,
684 &opts
, current_language
);
686 /* Otherwise, we end up at the return outside this "if" */
689 return val_print (val_type
, value_contents_all (val
),
690 value_embedded_offset (val
),
691 VALUE_ADDRESS (val
) + value_offset (val
),
692 stream
, 0, &opts
, current_language
);