1 /* Support for printing C values for GDB, the GNU debugger.
2 Copyright 1986, 1988, 1989, 1991-1997, 2000
3 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
25 #include "expression.h"
33 /* Print data of type TYPE located at VALADDR (within GDB), which came from
34 the inferior at address ADDRESS, onto stdio stream STREAM according to
35 FORMAT (a letter or 0 for natural format). The data at VALADDR is in
38 If the data are a string pointer, returns the number of string characters
41 If DEREF_REF is nonzero, then dereference references, otherwise just print
44 The PRETTY parameter controls prettyprinting. */
47 c_val_print (type, valaddr, embedded_offset, address, stream, format, deref_ref, recurse,
53 struct ui_file *stream;
57 enum val_prettyprint pretty;
59 register unsigned int i = 0; /* Number of characters printed */
67 switch (TYPE_CODE (type))
70 elttype = check_typedef (TYPE_TARGET_TYPE (type));
71 if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0)
73 eltlen = TYPE_LENGTH (elttype);
74 len = TYPE_LENGTH (type) / eltlen;
75 if (prettyprint_arrays)
77 print_spaces_filtered (2 + 2 * recurse, stream);
79 /* For an array of chars, print with string syntax. */
81 ((TYPE_CODE (elttype) == TYPE_CODE_INT)
82 || ((current_language->la_language == language_m2)
83 && (TYPE_CODE (elttype) == TYPE_CODE_CHAR)))
84 && (format == 0 || format == 's'))
86 /* If requested, look for the first null char and only print
88 if (stop_print_at_null)
90 unsigned int temp_len;
92 /* Look for a NULL char. */
94 (valaddr + embedded_offset)[temp_len]
95 && temp_len < len && temp_len < print_max;
100 LA_PRINT_STRING (stream, valaddr + embedded_offset, len, eltlen, 0);
105 fprintf_filtered (stream, "{");
106 /* If this is a virtual function table, print the 0th
107 entry specially, and the rest of the members normally. */
108 if (cp_is_vtbl_ptr_type (elttype))
111 fprintf_filtered (stream, "%d vtable entries", len - 1);
117 val_print_array_elements (type, valaddr + embedded_offset, address, stream,
118 format, deref_ref, recurse, pretty, i);
119 fprintf_filtered (stream, "}");
123 /* Array of unspecified length: treat like pointer to first elt. */
125 goto print_unpacked_pointer;
128 if (format && format != 's')
130 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
133 if (vtblprint && cp_is_vtbl_ptr_type (type))
135 /* Print the unmangled name if desired. */
136 /* Print vtable entry - we only get here if we ARE using
137 -fvtable_thunks. (Otherwise, look under TYPE_CODE_STRUCT.) */
139 = extract_typed_address (valaddr + embedded_offset, type);
140 print_address_demangle (addr, stream, demangle);
143 elttype = check_typedef (TYPE_TARGET_TYPE (type));
144 if (TYPE_CODE (elttype) == TYPE_CODE_METHOD)
146 cp_print_class_method (valaddr + embedded_offset, type, stream);
148 else if (TYPE_CODE (elttype) == TYPE_CODE_MEMBER)
150 cp_print_class_member (valaddr + embedded_offset,
151 TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (type)),
156 addr = unpack_pointer (type, valaddr + embedded_offset);
157 print_unpacked_pointer:
158 elttype = check_typedef (TYPE_TARGET_TYPE (type));
160 if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
162 /* Try to print what function it points to. */
163 print_address_demangle (addr, stream, demangle);
164 /* Return value is irrelevant except for string pointers. */
168 if (addressprint && format != 's')
170 print_address_numeric (addr, 1, stream);
173 /* For a pointer to char or unsigned char, also print the string
174 pointed to, unless pointer is null. */
175 /* FIXME: need to handle wchar_t here... */
177 if (TYPE_LENGTH (elttype) == 1
178 && TYPE_CODE (elttype) == TYPE_CODE_INT
179 && (format == 0 || format == 's')
182 i = val_print_string (addr, -1, TYPE_LENGTH (elttype), stream);
184 else if (cp_is_vtbl_member (type))
186 /* print vtbl's nicely */
187 CORE_ADDR vt_address = unpack_pointer (type, valaddr + embedded_offset);
189 struct minimal_symbol *msymbol =
190 lookup_minimal_symbol_by_pc (vt_address);
191 if ((msymbol != NULL) &&
192 (vt_address == SYMBOL_VALUE_ADDRESS (msymbol)))
194 fputs_filtered (" <", stream);
195 fputs_filtered (SYMBOL_SOURCE_NAME (msymbol), stream);
196 fputs_filtered (">", stream);
198 if (vt_address && vtblprint)
201 struct symbol *wsym = (struct symbol *) NULL;
204 struct block *block = (struct block *) NULL;
208 wsym = lookup_symbol (SYMBOL_NAME (msymbol), block,
209 VAR_NAMESPACE, &is_this_fld, &s);
213 wtype = SYMBOL_TYPE (wsym);
217 wtype = TYPE_TARGET_TYPE (type);
219 vt_val = value_at (wtype, vt_address, NULL);
220 val_print (VALUE_TYPE (vt_val), VALUE_CONTENTS (vt_val), 0,
221 VALUE_ADDRESS (vt_val), stream, format,
222 deref_ref, recurse + 1, pretty);
225 fprintf_filtered (stream, "\n");
226 print_spaces_filtered (2 + 2 * recurse, stream);
231 /* Return number of characters printed, including the terminating
232 '\0' if we reached the end. val_print_string takes care including
233 the terminating '\0' if necessary. */
238 case TYPE_CODE_MEMBER:
239 error ("not implemented: member type in c_val_print");
243 elttype = check_typedef (TYPE_TARGET_TYPE (type));
244 if (TYPE_CODE (elttype) == TYPE_CODE_MEMBER)
246 cp_print_class_member (valaddr + embedded_offset,
247 TYPE_DOMAIN_TYPE (elttype),
254 = extract_typed_address (valaddr + embedded_offset, type);
255 fprintf_filtered (stream, "@");
256 print_address_numeric (addr, 1, stream);
258 fputs_filtered (": ", stream);
260 /* De-reference the reference. */
263 if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
265 value_ptr deref_val =
267 (TYPE_TARGET_TYPE (type),
268 unpack_pointer (lookup_pointer_type (builtin_type_void),
269 valaddr + embedded_offset),
271 val_print (VALUE_TYPE (deref_val),
272 VALUE_CONTENTS (deref_val),
274 VALUE_ADDRESS (deref_val),
282 fputs_filtered ("???", stream);
286 case TYPE_CODE_UNION:
287 if (recurse && !unionprint)
289 fprintf_filtered (stream, "{...}");
293 case TYPE_CODE_STRUCT:
294 if (vtblprint && cp_is_vtbl_ptr_type (type))
296 /* Print the unmangled name if desired. */
297 /* Print vtable entry - we only get here if NOT using
298 -fvtable_thunks. (Otherwise, look under TYPE_CODE_PTR.) */
299 int offset = (embedded_offset +
300 TYPE_FIELD_BITPOS (type, VTBL_FNADDR_OFFSET) / 8);
301 struct type *field_type = TYPE_FIELD_TYPE (type, VTBL_FNADDR_OFFSET);
303 = extract_typed_address (valaddr + offset, field_type);
305 print_address_demangle (addr, stream, demangle);
308 cp_print_value_fields (type, type, valaddr, embedded_offset, address, stream, format,
309 recurse, pretty, NULL, 0);
315 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
318 len = TYPE_NFIELDS (type);
319 val = unpack_long (type, valaddr + embedded_offset);
320 for (i = 0; i < len; i++)
323 if (val == TYPE_FIELD_BITPOS (type, i))
330 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
334 print_longest (stream, 'd', 0, val);
341 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
344 /* FIXME, we should consider, at least for ANSI C language, eliminating
345 the distinction made between FUNCs and POINTERs to FUNCs. */
346 fprintf_filtered (stream, "{");
347 type_print (type, "", stream, -1);
348 fprintf_filtered (stream, "} ");
349 /* Try to print what function it points to, and its address. */
350 print_address_demangle (address, stream, demangle);
354 format = format ? format : output_format;
356 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
359 val = unpack_long (type, valaddr + embedded_offset);
361 fputs_filtered ("false", stream);
363 fputs_filtered ("true", stream);
365 print_longest (stream, 'd', 0, val);
369 case TYPE_CODE_RANGE:
370 /* FIXME: create_range_type does not set the unsigned bit in a
371 range type (I think it probably should copy it from the target
372 type), so we won't print values which are too large to
373 fit in a signed integer correctly. */
374 /* FIXME: Doesn't handle ranges of enums correctly. (Can't just
375 print with the target type, though, because the size of our type
376 and the target type might differ). */
380 format = format ? format : output_format;
383 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
387 val_print_type_code_int (type, valaddr + embedded_offset, stream);
388 /* C and C++ has no single byte int type, char is used instead.
389 Since we don't know whether the value is really intended to
390 be used as an integer or a character, print the character
391 equivalent as well. */
392 if (TYPE_LENGTH (type) == 1)
394 fputs_filtered (" ", stream);
395 LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr + embedded_offset),
402 format = format ? format : output_format;
405 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
409 val = unpack_long (type, valaddr + embedded_offset);
410 if (TYPE_UNSIGNED (type))
411 fprintf_filtered (stream, "%u", (unsigned int) val);
413 fprintf_filtered (stream, "%d", (int) val);
414 fputs_filtered (" ", stream);
415 LA_PRINT_CHAR ((unsigned char) val, stream);
422 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
426 print_floating (valaddr + embedded_offset, type, stream);
430 case TYPE_CODE_METHOD:
431 cp_print_class_method (valaddr + embedded_offset, lookup_pointer_type (type), stream);
435 fprintf_filtered (stream, "void");
438 case TYPE_CODE_ERROR:
439 fprintf_filtered (stream, "<error type>");
442 case TYPE_CODE_UNDEF:
443 /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
444 dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
445 and no complete type for struct foo in that file. */
446 fprintf_filtered (stream, "<incomplete type>");
450 error ("Invalid C/C++ type code %d in symbol table.", TYPE_CODE (type));
457 c_value_print (val, stream, format, pretty)
459 struct ui_file *stream;
461 enum val_prettyprint pretty;
463 struct type *type = VALUE_TYPE (val);
464 struct type *real_type;
465 int full, top, using_enc;
467 /* If it is a pointer, indicate what it points to.
469 Print type also if it is a reference.
471 C++: if it is a member pointer, we will take care
472 of that when we print it. */
473 if (TYPE_CODE (type) == TYPE_CODE_PTR ||
474 TYPE_CODE (type) == TYPE_CODE_REF)
476 /* Hack: remove (char *) for char strings. Their
477 type is indicated by the quoted string anyway. */
478 if (TYPE_CODE (type) == TYPE_CODE_PTR &&
479 TYPE_NAME (type) == NULL &&
480 TYPE_NAME (TYPE_TARGET_TYPE (type)) != NULL &&
481 STREQ (TYPE_NAME (TYPE_TARGET_TYPE (type)), "char"))
485 else if (objectprint && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CLASS))
488 if (TYPE_CODE(type) == TYPE_CODE_REF)
490 /* Copy value, change to pointer, so we don't get an
491 * error about a non-pointer type in value_rtti_target_type
494 temparg=value_copy(val);
495 VALUE_TYPE (temparg) = lookup_pointer_type(TYPE_TARGET_TYPE(type));
498 /* Pointer to class, check real type of object */
499 fprintf_filtered (stream, "(");
500 real_type = value_rtti_target_type (val, &full, &top, &using_enc);
503 /* RTTI entry found */
504 if (TYPE_CODE (type) == TYPE_CODE_PTR)
506 /* create a pointer type pointing to the real type */
507 type = lookup_pointer_type (real_type);
511 /* create a reference type referencing the real type */
512 type = lookup_reference_type (real_type);
514 /* JYG: Need to adjust pointer value. */
515 val->aligner.contents[0] -= top;
517 /* Note: When we look up RTTI entries, we don't get any
518 information on const or volatile attributes */
520 type_print (type, "", stream, -1);
521 fprintf_filtered (stream, ") ");
526 fprintf_filtered (stream, "(");
527 type_print (type, "", stream, -1);
528 fprintf_filtered (stream, ") ");
531 if (objectprint && (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_CLASS))
533 /* Attempt to determine real type of object */
534 real_type = value_rtti_type (val, &full, &top, &using_enc);
537 /* We have RTTI information, so use it */
538 val = value_full_object (val, real_type, full, top, using_enc);
539 fprintf_filtered (stream, "(%s%s) ",
540 TYPE_NAME (real_type),
541 full ? "" : " [incomplete object]");
542 /* Print out object: enclosing type is same as real_type if full */
543 return val_print (VALUE_ENCLOSING_TYPE (val), VALUE_CONTENTS_ALL (val), 0,
544 VALUE_ADDRESS (val), stream, format, 1, 0, pretty);
545 /* Note: When we look up RTTI entries, we don't get any information on
546 const or volatile attributes */
548 else if (type != VALUE_ENCLOSING_TYPE (val))
550 /* No RTTI information, so let's do our best */
551 fprintf_filtered (stream, "(%s ?) ",
552 TYPE_NAME (VALUE_ENCLOSING_TYPE (val)));
553 return val_print (VALUE_ENCLOSING_TYPE (val), VALUE_CONTENTS_ALL (val), 0,
554 VALUE_ADDRESS (val), stream, format, 1, 0, pretty);
556 /* Otherwise, we end up at the return outside this "if" */
559 return val_print (type, VALUE_CONTENTS_ALL (val), VALUE_EMBEDDED_OFFSET (val),
561 stream, format, 1, 0, pretty);