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
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, CORE_ADDR address,
40 struct ui_file *stream, int addressprint)
42 CORE_ADDR func_addr = gdbarch_convert_from_func_ptr_addr (gdbarch, address,
45 /* If the function pointer is represented by a description, print the
46 address of the description. */
47 if (addressprint && func_addr != address)
49 fputs_filtered ("@", stream);
50 fputs_filtered (paddress (gdbarch, address), stream);
51 fputs_filtered (": ", stream);
53 print_address_demangle (gdbarch, func_addr, stream, demangle);
57 /* A helper for c_textual_element_type. This checks the name of the
58 typedef. This is bogus but it isn't apparent that the compiler
59 provides us the help we may need. */
62 textual_name (const char *name)
64 return (!strcmp (name, "wchar_t")
65 || !strcmp (name, "char16_t")
66 || !strcmp (name, "char32_t"));
69 /* Apply a heuristic to decide whether an array of TYPE or a pointer
70 to TYPE should be printed as a textual string. Return non-zero if
71 it should, or zero if it should be treated as an array of integers
72 or pointer to integers. FORMAT is the current format letter,
75 We guess that "char" is a character. Explicitly signed and
76 unsigned character types are also characters. Integer data from
77 vector types is not. The user can override this by using the /s
81 c_textual_element_type (struct type *type, char format)
83 struct type *true_type, *iter_type;
85 if (format != 0 && format != 's')
88 /* We also rely on this for its side effect of setting up all the
90 true_type = check_typedef (type);
92 /* TYPE_CODE_CHAR is always textual. */
93 if (TYPE_CODE (true_type) == TYPE_CODE_CHAR)
96 /* Any other character-like types must be integral. */
97 if (TYPE_CODE (true_type) != TYPE_CODE_INT)
100 /* We peel typedefs one by one, looking for a match. */
104 /* Check the name of the type. */
105 if (TYPE_NAME (iter_type) && textual_name (TYPE_NAME (iter_type)))
108 if (TYPE_CODE (iter_type) != TYPE_CODE_TYPEDEF)
111 /* Peel a single typedef. If the typedef doesn't have a target
112 type, we use check_typedef and hope the result is ok -- it
113 might be for C++, where wchar_t is a built-in type. */
114 if (TYPE_TARGET_TYPE (iter_type))
115 iter_type = TYPE_TARGET_TYPE (iter_type);
117 iter_type = check_typedef (iter_type);
122 /* Print this as a string if we can manage it. For now, no
123 wide character support. */
124 if (TYPE_CODE (true_type) == TYPE_CODE_INT
125 && TYPE_LENGTH (true_type) == 1)
130 /* If a one-byte TYPE_CODE_INT is missing the not-a-character
131 flag, then we treat it as text; otherwise, we assume it's
132 being used as data. */
133 if (TYPE_CODE (true_type) == TYPE_CODE_INT
134 && TYPE_LENGTH (true_type) == 1
135 && !TYPE_NOTTEXT (true_type))
143 /* Print data of type TYPE located at VALADDR (within GDB), which came from
144 the inferior at address ADDRESS, onto stdio stream STREAM according to
145 OPTIONS. The data at VALADDR is in target byte order.
147 If the data are a string pointer, returns the number of string characters
151 c_val_print (struct type *type, const gdb_byte *valaddr, int embedded_offset,
152 CORE_ADDR address, struct ui_file *stream, int recurse,
153 const struct value *original_value,
154 const struct value_print_options *options)
156 struct gdbarch *gdbarch = get_type_arch (type);
157 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
158 unsigned int i = 0; /* Number of characters printed */
160 struct type *elttype, *unresolved_elttype;
161 struct type *unresolved_type = type;
166 CHECK_TYPEDEF (type);
167 switch (TYPE_CODE (type))
169 case TYPE_CODE_ARRAY:
170 unresolved_elttype = TYPE_TARGET_TYPE (type);
171 elttype = check_typedef (unresolved_elttype);
172 if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (unresolved_elttype) > 0)
174 eltlen = TYPE_LENGTH (elttype);
175 len = TYPE_LENGTH (type) / eltlen;
176 if (options->prettyprint_arrays)
178 print_spaces_filtered (2 + 2 * recurse, stream);
181 /* Print arrays of textual chars with a string syntax, as
182 long as the entire array is valid. */
183 if (c_textual_element_type (unresolved_elttype, options->format)
184 && value_bits_valid (original_value,
185 TARGET_CHAR_BIT * embedded_offset,
186 TARGET_CHAR_BIT * TYPE_LENGTH (type)))
188 /* If requested, look for the first null char and only print
189 elements up to it. */
190 if (options->stop_print_at_null)
192 unsigned int temp_len;
196 && temp_len < options->print_max
197 && extract_unsigned_integer (valaddr + embedded_offset
199 eltlen, byte_order) != 0);
205 LA_PRINT_STRING (stream, unresolved_elttype,
206 valaddr + embedded_offset, len,
212 fprintf_filtered (stream, "{");
213 /* If this is a virtual function table, print the 0th
214 entry specially, and the rest of the members normally. */
215 if (cp_is_vtbl_ptr_type (elttype))
218 fprintf_filtered (stream, _("%d vtable entries"), len - 1);
224 val_print_array_elements (type, valaddr + embedded_offset,
225 address + embedded_offset, stream,
226 recurse, original_value, options, i);
227 fprintf_filtered (stream, "}");
231 /* Array of unspecified length: treat like pointer to first elt. */
233 goto print_unpacked_pointer;
235 case TYPE_CODE_MEMBERPTR:
238 print_scalar_formatted (valaddr + embedded_offset, type,
242 cp_print_class_member (valaddr + embedded_offset, type, stream, "&");
245 case TYPE_CODE_METHODPTR:
246 cplus_print_method_ptr (valaddr + embedded_offset, type, stream);
250 if (options->format && options->format != 's')
252 print_scalar_formatted (valaddr + embedded_offset, type,
256 if (options->vtblprint && cp_is_vtbl_ptr_type (type))
258 /* Print the unmangled name if desired. */
259 /* Print vtable entry - we only get here if we ARE using
260 -fvtable_thunks. (Otherwise, look under TYPE_CODE_STRUCT.) */
262 = extract_typed_address (valaddr + embedded_offset, type);
264 print_function_pointer_address (gdbarch, addr, stream,
265 options->addressprint);
268 unresolved_elttype = TYPE_TARGET_TYPE (type);
269 elttype = check_typedef (unresolved_elttype);
271 addr = unpack_pointer (type, valaddr + embedded_offset);
272 print_unpacked_pointer:
274 if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
276 /* Try to print what function it points to. */
277 print_function_pointer_address (gdbarch, addr, stream,
278 options->addressprint);
279 /* Return value is irrelevant except for string pointers. */
283 if (options->addressprint)
284 fputs_filtered (paddress (gdbarch, addr), stream);
286 /* For a pointer to a textual type, also print the string
287 pointed to, unless pointer is null. */
289 if (c_textual_element_type (unresolved_elttype, options->format)
292 i = val_print_string (unresolved_elttype, NULL, addr, -1, stream,
295 else if (cp_is_vtbl_member (type))
297 /* print vtbl's nicely */
298 CORE_ADDR vt_address = unpack_pointer (type, valaddr + embedded_offset);
300 struct minimal_symbol *msymbol =
301 lookup_minimal_symbol_by_pc (vt_address);
302 if ((msymbol != NULL)
303 && (vt_address == SYMBOL_VALUE_ADDRESS (msymbol)))
305 fputs_filtered (" <", stream);
306 fputs_filtered (SYMBOL_PRINT_NAME (msymbol), stream);
307 fputs_filtered (">", stream);
309 if (vt_address && options->vtblprint)
311 struct value *vt_val;
312 struct symbol *wsym = (struct symbol *) NULL;
314 struct block *block = (struct block *) NULL;
318 wsym = lookup_symbol (SYMBOL_LINKAGE_NAME (msymbol), block,
319 VAR_DOMAIN, &is_this_fld);
323 wtype = SYMBOL_TYPE (wsym);
327 wtype = unresolved_elttype;
329 vt_val = value_at (wtype, vt_address);
330 common_val_print (vt_val, stream, recurse + 1, options,
334 fprintf_filtered (stream, "\n");
335 print_spaces_filtered (2 + 2 * recurse, stream);
340 /* Return number of characters printed, including the terminating
341 '\0' if we reached the end. val_print_string takes care including
342 the terminating '\0' if necessary. */
348 elttype = check_typedef (TYPE_TARGET_TYPE (type));
349 if (options->addressprint)
352 = extract_typed_address (valaddr + embedded_offset, type);
354 fprintf_filtered (stream, "@");
355 fputs_filtered (paddress (gdbarch, addr), stream);
356 if (options->deref_ref)
357 fputs_filtered (": ", stream);
359 /* De-reference the reference. */
360 if (options->deref_ref)
362 if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
364 struct value *deref_val =
366 (TYPE_TARGET_TYPE (type),
367 unpack_pointer (type, valaddr + embedded_offset));
369 common_val_print (deref_val, stream, recurse, options,
373 fputs_filtered ("???", stream);
377 case TYPE_CODE_UNION:
378 if (recurse && !options->unionprint)
380 fprintf_filtered (stream, "{...}");
384 case TYPE_CODE_STRUCT:
385 /*FIXME: Abstract this away */
386 if (options->vtblprint && cp_is_vtbl_ptr_type (type))
388 /* Print the unmangled name if desired. */
389 /* Print vtable entry - we only get here if NOT using
390 -fvtable_thunks. (Otherwise, look under TYPE_CODE_PTR.) */
391 int offset = (embedded_offset +
392 TYPE_FIELD_BITPOS (type, VTBL_FNADDR_OFFSET) / 8);
393 struct type *field_type = TYPE_FIELD_TYPE (type, VTBL_FNADDR_OFFSET);
395 = extract_typed_address (valaddr + offset, field_type);
397 print_function_pointer_address (gdbarch, addr, stream,
398 options->addressprint);
401 cp_print_value_fields_rtti (type, valaddr,
402 embedded_offset, address, stream,
403 recurse, original_value, options, NULL, 0);
409 print_scalar_formatted (valaddr + embedded_offset, type,
413 len = TYPE_NFIELDS (type);
414 val = unpack_long (type, valaddr + embedded_offset);
415 for (i = 0; i < len; i++)
418 if (val == TYPE_FIELD_BITPOS (type, i))
425 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
429 print_longest (stream, 'd', 0, val);
433 case TYPE_CODE_FLAGS:
435 print_scalar_formatted (valaddr + embedded_offset, type,
438 val_print_type_code_flags (type, valaddr + embedded_offset, stream);
442 case TYPE_CODE_METHOD:
445 print_scalar_formatted (valaddr + embedded_offset, type,
449 /* FIXME, we should consider, at least for ANSI C language, eliminating
450 the distinction made between FUNCs and POINTERs to FUNCs. */
451 fprintf_filtered (stream, "{");
452 type_print (type, "", stream, -1);
453 fprintf_filtered (stream, "} ");
454 /* Try to print what function it points to, and its address. */
455 print_address_demangle (gdbarch, address, stream, demangle);
459 if (options->format || options->output_format)
461 struct value_print_options opts = *options;
462 opts.format = (options->format ? options->format
463 : options->output_format);
464 print_scalar_formatted (valaddr + embedded_offset, type,
469 val = unpack_long (type, valaddr + embedded_offset);
471 fputs_filtered ("false", stream);
473 fputs_filtered ("true", stream);
475 print_longest (stream, 'd', 0, val);
479 case TYPE_CODE_RANGE:
480 /* FIXME: create_range_type does not set the unsigned bit in a
481 range type (I think it probably should copy it from the target
482 type), so we won't print values which are too large to
483 fit in a signed integer correctly. */
484 /* FIXME: Doesn't handle ranges of enums correctly. (Can't just
485 print with the target type, though, because the size of our type
486 and the target type might differ). */
490 if (options->format || options->output_format)
492 struct value_print_options opts = *options;
494 opts.format = (options->format ? options->format
495 : options->output_format);
496 print_scalar_formatted (valaddr + embedded_offset, type,
501 val_print_type_code_int (type, valaddr + embedded_offset, stream);
502 /* C and C++ has no single byte int type, char is used instead.
503 Since we don't know whether the value is really intended to
504 be used as an integer or a character, print the character
505 equivalent as well. */
506 if (c_textual_element_type (unresolved_type, options->format))
508 fputs_filtered (" ", stream);
509 LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr + embedded_offset),
510 unresolved_type, stream);
516 if (options->format || options->output_format)
518 struct value_print_options opts = *options;
519 opts.format = (options->format ? options->format
520 : options->output_format);
521 print_scalar_formatted (valaddr + embedded_offset, type,
526 val = unpack_long (type, valaddr + embedded_offset);
527 if (TYPE_UNSIGNED (type))
528 fprintf_filtered (stream, "%u", (unsigned int) val);
530 fprintf_filtered (stream, "%d", (int) val);
531 fputs_filtered (" ", stream);
532 LA_PRINT_CHAR ((unsigned char) val, unresolved_type, stream);
539 print_scalar_formatted (valaddr + embedded_offset, type,
544 print_floating (valaddr + embedded_offset, type, stream);
548 case TYPE_CODE_DECFLOAT:
550 print_scalar_formatted (valaddr + embedded_offset, type,
553 print_decimal_floating (valaddr + embedded_offset, type, stream);
557 fprintf_filtered (stream, "void");
560 case TYPE_CODE_ERROR:
561 fprintf_filtered (stream, "%s", TYPE_ERROR_NAME (type));
564 case TYPE_CODE_UNDEF:
565 /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
566 dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
567 and no complete type for struct foo in that file. */
568 fprintf_filtered (stream, _("<incomplete type>"));
571 case TYPE_CODE_COMPLEX:
573 print_scalar_formatted (valaddr + embedded_offset,
574 TYPE_TARGET_TYPE (type),
577 print_floating (valaddr + embedded_offset, TYPE_TARGET_TYPE (type),
579 fprintf_filtered (stream, " + ");
581 print_scalar_formatted (valaddr + embedded_offset
582 + TYPE_LENGTH (TYPE_TARGET_TYPE (type)),
583 TYPE_TARGET_TYPE (type),
586 print_floating (valaddr + embedded_offset
587 + TYPE_LENGTH (TYPE_TARGET_TYPE (type)),
588 TYPE_TARGET_TYPE (type),
590 fprintf_filtered (stream, " * I");
594 error (_("Invalid C/C++ type code %d in symbol table."), TYPE_CODE (type));
601 c_value_print (struct value *val, struct ui_file *stream,
602 const struct value_print_options *options)
604 struct type *type, *real_type, *val_type;
605 int full, top, using_enc;
606 struct value_print_options opts = *options;
610 /* If it is a pointer, indicate what it points to.
612 Print type also if it is a reference.
614 C++: if it is a member pointer, we will take care
615 of that when we print it. */
617 /* Preserve the original type before stripping typedefs. We prefer
618 to pass down the original type when possible, but for local
619 checks it is better to look past the typedefs. */
620 val_type = value_type (val);
621 type = check_typedef (val_type);
623 if (TYPE_CODE (type) == TYPE_CODE_PTR
624 || TYPE_CODE (type) == TYPE_CODE_REF)
626 /* Hack: remove (char *) for char strings. Their
627 type is indicated by the quoted string anyway.
628 (Don't use c_textual_element_type here; quoted strings
629 are always exactly (char *), (wchar_t *), or the like. */
630 if (TYPE_CODE (val_type) == TYPE_CODE_PTR
631 && TYPE_NAME (val_type) == NULL
632 && TYPE_NAME (TYPE_TARGET_TYPE (val_type)) != NULL
633 && (strcmp (TYPE_NAME (TYPE_TARGET_TYPE (val_type)), "char") == 0
634 || textual_name (TYPE_NAME (TYPE_TARGET_TYPE (val_type)))))
638 else if (options->objectprint
639 && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CLASS))
642 if (TYPE_CODE(type) == TYPE_CODE_REF)
644 /* Copy value, change to pointer, so we don't get an
645 * error about a non-pointer type in value_rtti_target_type
647 struct value *temparg;
648 temparg=value_copy(val);
649 deprecated_set_value_type (temparg, lookup_pointer_type (TYPE_TARGET_TYPE(type)));
652 /* Pointer to class, check real type of object */
653 fprintf_filtered (stream, "(");
654 real_type = value_rtti_target_type (val, &full, &top, &using_enc);
657 /* RTTI entry found */
658 if (TYPE_CODE (type) == TYPE_CODE_PTR)
660 /* create a pointer type pointing to the real type */
661 type = lookup_pointer_type (real_type);
665 /* create a reference type referencing the real type */
666 type = lookup_reference_type (real_type);
668 /* JYG: Need to adjust pointer value. */
669 /* NOTE: cagney/2005-01-02: THIS IS BOGUS. */
670 value_contents_writeable (val)[0] -= top;
672 /* Note: When we look up RTTI entries, we don't get any
673 information on const or volatile attributes */
675 type_print (type, "", stream, -1);
676 fprintf_filtered (stream, ") ");
682 fprintf_filtered (stream, "(");
683 type_print (value_type (val), "", stream, -1);
684 fprintf_filtered (stream, ") ");
688 if (!value_initialized (val))
689 fprintf_filtered (stream, " [uninitialized] ");
691 if (options->objectprint && (TYPE_CODE (type) == TYPE_CODE_CLASS))
693 /* Attempt to determine real type of object */
694 real_type = value_rtti_type (val, &full, &top, &using_enc);
697 /* We have RTTI information, so use it */
698 val = value_full_object (val, real_type, full, top, using_enc);
699 fprintf_filtered (stream, "(%s%s) ",
700 TYPE_NAME (real_type),
701 full ? "" : _(" [incomplete object]"));
702 /* Print out object: enclosing type is same as real_type if full */
703 return val_print (value_enclosing_type (val),
704 value_contents_for_printing (val), 0,
705 value_address (val), stream, 0,
706 val, &opts, current_language);
707 /* Note: When we look up RTTI entries, we don't get any information on
708 const or volatile attributes */
710 else if (type != check_typedef (value_enclosing_type (val)))
712 /* No RTTI information, so let's do our best */
713 fprintf_filtered (stream, "(%s ?) ",
714 TYPE_NAME (value_enclosing_type (val)));
715 return val_print (value_enclosing_type (val),
716 value_contents_for_printing (val), 0,
717 value_address (val), stream, 0,
718 val, &opts, current_language);
720 /* Otherwise, we end up at the return outside this "if" */
723 return val_print (val_type, value_contents_for_printing (val),
724 value_embedded_offset (val),
727 val, &opts, current_language);