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, *iter_type;
86 if (format != 0 && format != 's')
89 /* We also rely on this for its side effect of setting up all the
91 true_type = check_typedef (type);
93 /* TYPE_CODE_CHAR is always textual. */
94 if (TYPE_CODE (true_type) == TYPE_CODE_CHAR)
97 /* Any other character-like types must be integral. */
98 if (TYPE_CODE (true_type) != TYPE_CODE_INT)
101 /* We peel typedefs one by one, looking for a match. */
105 /* Check the name of the type. */
106 if (TYPE_NAME (iter_type) && textual_name (TYPE_NAME (iter_type)))
109 if (TYPE_CODE (iter_type) != TYPE_CODE_TYPEDEF)
112 /* Peel a single typedef. If the typedef doesn't have a target
113 type, we use check_typedef and hope the result is ok -- it
114 might be for C++, where wchar_t is a built-in type. */
115 if (TYPE_TARGET_TYPE (iter_type))
116 iter_type = TYPE_TARGET_TYPE (iter_type);
118 iter_type = check_typedef (iter_type);
123 /* Print this as a string if we can manage it. For now, no
124 wide character support. */
125 if (TYPE_CODE (true_type) == TYPE_CODE_INT
126 && TYPE_LENGTH (true_type) == 1)
131 /* If a one-byte TYPE_CODE_INT is missing the not-a-character
132 flag, then we treat it as text; otherwise, we assume it's
133 being used as data. */
134 if (TYPE_CODE (true_type) == TYPE_CODE_INT
135 && TYPE_LENGTH (true_type) == 1
136 && !TYPE_NOTTEXT (true_type))
144 /* Print data of type TYPE located at VALADDR (within GDB), which came from
145 the inferior at address ADDRESS, onto stdio stream STREAM according to
146 OPTIONS. The data at VALADDR is in target byte order.
148 If the data are a string pointer, returns the number of string characters
152 c_val_print (struct type *type, const gdb_byte *valaddr, int embedded_offset,
153 CORE_ADDR address, struct ui_file *stream, int recurse,
154 const struct value_print_options *options)
156 unsigned int i = 0; /* Number of characters printed */
158 struct type *elttype, *unresolved_elttype;
159 struct type *unresolved_type = type;
164 CHECK_TYPEDEF (type);
165 switch (TYPE_CODE (type))
167 case TYPE_CODE_ARRAY:
168 unresolved_elttype = TYPE_TARGET_TYPE (type);
169 elttype = check_typedef (unresolved_elttype);
170 if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (unresolved_elttype) > 0)
172 eltlen = TYPE_LENGTH (elttype);
173 len = TYPE_LENGTH (type) / eltlen;
174 if (options->prettyprint_arrays)
176 print_spaces_filtered (2 + 2 * recurse, stream);
179 /* Print arrays of textual chars with a string syntax. */
180 if (textual_element_type (unresolved_elttype, options->format))
182 /* If requested, look for the first null char and only print
183 elements up to it. */
184 if (options->stop_print_at_null)
186 unsigned int temp_len;
190 && temp_len < options->print_max
191 && extract_unsigned_integer (valaddr + embedded_offset
199 LA_PRINT_STRING (stream, unresolved_elttype,
200 valaddr + embedded_offset, len, 0, options);
205 fprintf_filtered (stream, "{");
206 /* If this is a virtual function table, print the 0th
207 entry specially, and the rest of the members normally. */
208 if (cp_is_vtbl_ptr_type (elttype))
211 fprintf_filtered (stream, _("%d vtable entries"), len - 1);
217 val_print_array_elements (type, valaddr + embedded_offset, address, stream,
218 recurse, options, i);
219 fprintf_filtered (stream, "}");
223 /* Array of unspecified length: treat like pointer to first elt. */
225 goto print_unpacked_pointer;
227 case TYPE_CODE_MEMBERPTR:
230 print_scalar_formatted (valaddr + embedded_offset, type,
234 cp_print_class_member (valaddr + embedded_offset, type, stream, "&");
237 case TYPE_CODE_METHODPTR:
238 cplus_print_method_ptr (valaddr + embedded_offset, type, stream);
242 if (options->format && options->format != 's')
244 print_scalar_formatted (valaddr + embedded_offset, type,
248 if (options->vtblprint && cp_is_vtbl_ptr_type (type))
250 /* Print the unmangled name if desired. */
251 /* Print vtable entry - we only get here if we ARE using
252 -fvtable_thunks. (Otherwise, look under TYPE_CODE_STRUCT.) */
254 = extract_typed_address (valaddr + embedded_offset, type);
255 print_function_pointer_address (addr, stream, options->addressprint);
258 unresolved_elttype = TYPE_TARGET_TYPE (type);
259 elttype = check_typedef (unresolved_elttype);
261 addr = unpack_pointer (type, valaddr + embedded_offset);
262 print_unpacked_pointer:
264 if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
266 /* Try to print what function it points to. */
267 print_function_pointer_address (addr, stream,
268 options->addressprint);
269 /* Return value is irrelevant except for string pointers. */
273 if (options->addressprint)
274 fputs_filtered (paddress (addr), stream);
276 /* For a pointer to a textual type, also print the string
277 pointed to, unless pointer is null. */
279 if (textual_element_type (unresolved_elttype, options->format)
282 i = val_print_string (unresolved_elttype, addr, -1, stream,
285 else if (cp_is_vtbl_member (type))
287 /* print vtbl's nicely */
288 CORE_ADDR vt_address = unpack_pointer (type, valaddr + embedded_offset);
290 struct minimal_symbol *msymbol =
291 lookup_minimal_symbol_by_pc (vt_address);
292 if ((msymbol != NULL) &&
293 (vt_address == SYMBOL_VALUE_ADDRESS (msymbol)))
295 fputs_filtered (" <", stream);
296 fputs_filtered (SYMBOL_PRINT_NAME (msymbol), stream);
297 fputs_filtered (">", stream);
299 if (vt_address && options->vtblprint)
301 struct value *vt_val;
302 struct symbol *wsym = (struct symbol *) NULL;
304 struct block *block = (struct block *) NULL;
308 wsym = lookup_symbol (SYMBOL_LINKAGE_NAME (msymbol), block,
309 VAR_DOMAIN, &is_this_fld);
313 wtype = SYMBOL_TYPE (wsym);
317 wtype = unresolved_elttype;
319 vt_val = value_at (wtype, vt_address);
320 common_val_print (vt_val, stream, recurse + 1, options,
324 fprintf_filtered (stream, "\n");
325 print_spaces_filtered (2 + 2 * recurse, stream);
330 /* Return number of characters printed, including the terminating
331 '\0' if we reached the end. val_print_string takes care including
332 the terminating '\0' if necessary. */
338 elttype = check_typedef (TYPE_TARGET_TYPE (type));
339 if (options->addressprint)
342 = extract_typed_address (valaddr + embedded_offset, type);
343 fprintf_filtered (stream, "@");
344 fputs_filtered (paddress (addr), stream);
345 if (options->deref_ref)
346 fputs_filtered (": ", stream);
348 /* De-reference the reference. */
349 if (options->deref_ref)
351 if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
353 struct value *deref_val =
355 (TYPE_TARGET_TYPE (type),
356 unpack_pointer (type, valaddr + embedded_offset));
357 common_val_print (deref_val, stream, recurse, options,
361 fputs_filtered ("???", stream);
365 case TYPE_CODE_UNION:
366 if (recurse && !options->unionprint)
368 fprintf_filtered (stream, "{...}");
372 case TYPE_CODE_STRUCT:
373 /*FIXME: Abstract this away */
374 if (options->vtblprint && cp_is_vtbl_ptr_type (type))
376 /* Print the unmangled name if desired. */
377 /* Print vtable entry - we only get here if NOT using
378 -fvtable_thunks. (Otherwise, look under TYPE_CODE_PTR.) */
379 int offset = (embedded_offset +
380 TYPE_FIELD_BITPOS (type, VTBL_FNADDR_OFFSET) / 8);
381 struct type *field_type = TYPE_FIELD_TYPE (type, VTBL_FNADDR_OFFSET);
383 = extract_typed_address (valaddr + offset, field_type);
385 print_function_pointer_address (addr, stream, options->addressprint);
388 cp_print_value_fields (type, type, valaddr, embedded_offset, address, stream,
389 recurse, options, NULL, 0);
395 print_scalar_formatted (valaddr + embedded_offset, type,
399 len = TYPE_NFIELDS (type);
400 val = unpack_long (type, valaddr + embedded_offset);
401 for (i = 0; i < len; i++)
404 if (val == TYPE_FIELD_BITPOS (type, i))
411 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
415 print_longest (stream, 'd', 0, val);
419 case TYPE_CODE_FLAGS:
421 print_scalar_formatted (valaddr + embedded_offset, type,
424 val_print_type_code_flags (type, valaddr + embedded_offset, stream);
428 case TYPE_CODE_METHOD:
431 print_scalar_formatted (valaddr + embedded_offset, type,
435 /* FIXME, we should consider, at least for ANSI C language, eliminating
436 the distinction made between FUNCs and POINTERs to FUNCs. */
437 fprintf_filtered (stream, "{");
438 type_print (type, "", stream, -1);
439 fprintf_filtered (stream, "} ");
440 /* Try to print what function it points to, and its address. */
441 print_address_demangle (address, stream, demangle);
445 if (options->format || options->output_format)
447 struct value_print_options opts = *options;
448 opts.format = (options->format ? options->format
449 : options->output_format);
450 print_scalar_formatted (valaddr + embedded_offset, type,
455 val = unpack_long (type, valaddr + embedded_offset);
457 fputs_filtered ("false", stream);
459 fputs_filtered ("true", stream);
461 print_longest (stream, 'd', 0, val);
465 case TYPE_CODE_RANGE:
466 /* FIXME: create_range_type does not set the unsigned bit in a
467 range type (I think it probably should copy it from the target
468 type), so we won't print values which are too large to
469 fit in a signed integer correctly. */
470 /* FIXME: Doesn't handle ranges of enums correctly. (Can't just
471 print with the target type, though, because the size of our type
472 and the target type might differ). */
476 if (options->format || options->output_format)
478 struct value_print_options opts = *options;
479 opts.format = (options->format ? options->format
480 : options->output_format);
481 print_scalar_formatted (valaddr + embedded_offset, type,
486 val_print_type_code_int (type, valaddr + embedded_offset, stream);
487 /* C and C++ has no single byte int type, char is used instead.
488 Since we don't know whether the value is really intended to
489 be used as an integer or a character, print the character
490 equivalent as well. */
491 if (textual_element_type (unresolved_type, options->format))
493 fputs_filtered (" ", stream);
494 LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr + embedded_offset),
495 unresolved_type, stream);
501 if (options->format || options->output_format)
503 struct value_print_options opts = *options;
504 opts.format = (options->format ? options->format
505 : options->output_format);
506 print_scalar_formatted (valaddr + embedded_offset, type,
511 val = unpack_long (type, valaddr + embedded_offset);
512 if (TYPE_UNSIGNED (type))
513 fprintf_filtered (stream, "%u", (unsigned int) val);
515 fprintf_filtered (stream, "%d", (int) val);
516 fputs_filtered (" ", stream);
517 LA_PRINT_CHAR ((unsigned char) val, unresolved_type, stream);
524 print_scalar_formatted (valaddr + embedded_offset, type,
529 print_floating (valaddr + embedded_offset, type, stream);
533 case TYPE_CODE_DECFLOAT:
535 print_scalar_formatted (valaddr + embedded_offset, type,
538 print_decimal_floating (valaddr + embedded_offset, type, stream);
542 fprintf_filtered (stream, "void");
545 case TYPE_CODE_ERROR:
546 fprintf_filtered (stream, _("<error type>"));
549 case TYPE_CODE_UNDEF:
550 /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
551 dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
552 and no complete type for struct foo in that file. */
553 fprintf_filtered (stream, _("<incomplete type>"));
556 case TYPE_CODE_COMPLEX:
558 print_scalar_formatted (valaddr + embedded_offset,
559 TYPE_TARGET_TYPE (type),
562 print_floating (valaddr + embedded_offset, TYPE_TARGET_TYPE (type),
564 fprintf_filtered (stream, " + ");
566 print_scalar_formatted (valaddr + embedded_offset
567 + TYPE_LENGTH (TYPE_TARGET_TYPE (type)),
568 TYPE_TARGET_TYPE (type),
571 print_floating (valaddr + embedded_offset
572 + TYPE_LENGTH (TYPE_TARGET_TYPE (type)),
573 TYPE_TARGET_TYPE (type),
575 fprintf_filtered (stream, " * I");
579 error (_("Invalid C/C++ type code %d in symbol table."), TYPE_CODE (type));
586 c_value_print (struct value *val, struct ui_file *stream,
587 const struct value_print_options *options)
589 struct type *type, *real_type, *val_type;
590 int full, top, using_enc;
591 struct value_print_options opts = *options;
595 /* If it is a pointer, indicate what it points to.
597 Print type also if it is a reference.
599 C++: if it is a member pointer, we will take care
600 of that when we print it. */
602 /* Preserve the original type before stripping typedefs. We prefer
603 to pass down the original type when possible, but for local
604 checks it is better to look past the typedefs. */
605 val_type = value_type (val);
606 type = check_typedef (val_type);
608 if (TYPE_CODE (type) == TYPE_CODE_PTR
609 || TYPE_CODE (type) == TYPE_CODE_REF)
611 /* Hack: remove (char *) for char strings. Their
612 type is indicated by the quoted string anyway.
613 (Don't use textual_element_type here; quoted strings
614 are always exactly (char *), (wchar_t *), or the like. */
615 if (TYPE_CODE (val_type) == TYPE_CODE_PTR
616 && TYPE_NAME (val_type) == NULL
617 && TYPE_NAME (TYPE_TARGET_TYPE (val_type)) != NULL
618 && (strcmp (TYPE_NAME (TYPE_TARGET_TYPE (val_type)), "char") == 0
619 || textual_name (TYPE_NAME (TYPE_TARGET_TYPE (val_type)))))
623 else if (options->objectprint
624 && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CLASS))
627 if (TYPE_CODE(type) == TYPE_CODE_REF)
629 /* Copy value, change to pointer, so we don't get an
630 * error about a non-pointer type in value_rtti_target_type
632 struct value *temparg;
633 temparg=value_copy(val);
634 deprecated_set_value_type (temparg, lookup_pointer_type (TYPE_TARGET_TYPE(type)));
637 /* Pointer to class, check real type of object */
638 fprintf_filtered (stream, "(");
639 real_type = value_rtti_target_type (val, &full, &top, &using_enc);
642 /* RTTI entry found */
643 if (TYPE_CODE (type) == TYPE_CODE_PTR)
645 /* create a pointer type pointing to the real type */
646 type = lookup_pointer_type (real_type);
650 /* create a reference type referencing the real type */
651 type = lookup_reference_type (real_type);
653 /* JYG: Need to adjust pointer value. */
654 /* NOTE: cagney/2005-01-02: THIS IS BOGUS. */
655 value_contents_writeable (val)[0] -= top;
657 /* Note: When we look up RTTI entries, we don't get any
658 information on const or volatile attributes */
660 type_print (type, "", stream, -1);
661 fprintf_filtered (stream, ") ");
667 fprintf_filtered (stream, "(");
668 type_print (value_type (val), "", stream, -1);
669 fprintf_filtered (stream, ") ");
673 if (!value_initialized (val))
674 fprintf_filtered (stream, " [uninitialized] ");
676 if (options->objectprint && (TYPE_CODE (type) == TYPE_CODE_CLASS))
678 /* Attempt to determine real type of object */
679 real_type = value_rtti_type (val, &full, &top, &using_enc);
682 /* We have RTTI information, so use it */
683 val = value_full_object (val, real_type, full, top, using_enc);
684 fprintf_filtered (stream, "(%s%s) ",
685 TYPE_NAME (real_type),
686 full ? "" : _(" [incomplete object]"));
687 /* Print out object: enclosing type is same as real_type if full */
688 return val_print (value_enclosing_type (val),
689 value_contents_all (val), 0,
690 value_address (val), stream, 0,
691 &opts, current_language);
692 /* Note: When we look up RTTI entries, we don't get any information on
693 const or volatile attributes */
695 else if (type != check_typedef (value_enclosing_type (val)))
697 /* No RTTI information, so let's do our best */
698 fprintf_filtered (stream, "(%s ?) ",
699 TYPE_NAME (value_enclosing_type (val)));
700 return val_print (value_enclosing_type (val),
701 value_contents_all (val), 0,
702 value_address (val), stream, 0,
703 &opts, current_language);
705 /* Otherwise, we end up at the return outside this "if" */
708 return val_print (val_type, value_contents_all (val),
709 value_embedded_offset (val),
711 stream, 0, &opts, current_language);