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.) */
138 print_address_demangle (extract_address (valaddr + embedded_offset, TYPE_LENGTH (type)),
142 elttype = check_typedef (TYPE_TARGET_TYPE (type));
143 if (TYPE_CODE (elttype) == TYPE_CODE_METHOD)
145 cp_print_class_method (valaddr + embedded_offset, type, stream);
147 else if (TYPE_CODE (elttype) == TYPE_CODE_MEMBER)
149 cp_print_class_member (valaddr + embedded_offset,
150 TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (type)),
155 addr = unpack_pointer (type, valaddr + embedded_offset);
156 print_unpacked_pointer:
157 elttype = check_typedef (TYPE_TARGET_TYPE (type));
159 if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
161 /* Try to print what function it points to. */
162 print_address_demangle (addr, stream, demangle);
163 /* Return value is irrelevant except for string pointers. */
167 if (addressprint && format != 's')
169 print_address_numeric (addr, 1, stream);
172 /* For a pointer to char or unsigned char, also print the string
173 pointed to, unless pointer is null. */
174 /* FIXME: need to handle wchar_t here... */
176 if (TYPE_LENGTH (elttype) == 1
177 && TYPE_CODE (elttype) == TYPE_CODE_INT
178 && (format == 0 || format == 's')
181 i = val_print_string (addr, -1, TYPE_LENGTH (elttype), stream);
183 else if (cp_is_vtbl_member (type))
185 /* print vtbl's nicely */
186 CORE_ADDR vt_address = unpack_pointer (type, valaddr + embedded_offset);
188 struct minimal_symbol *msymbol =
189 lookup_minimal_symbol_by_pc (vt_address);
190 if ((msymbol != NULL) &&
191 (vt_address == SYMBOL_VALUE_ADDRESS (msymbol)))
193 fputs_filtered (" <", stream);
194 fputs_filtered (SYMBOL_SOURCE_NAME (msymbol), stream);
195 fputs_filtered (">", stream);
197 if (vt_address && vtblprint)
200 struct symbol *wsym = (struct symbol *) NULL;
203 struct block *block = (struct block *) NULL;
207 wsym = lookup_symbol (SYMBOL_NAME (msymbol), block,
208 VAR_NAMESPACE, &is_this_fld, &s);
212 wtype = SYMBOL_TYPE (wsym);
216 wtype = TYPE_TARGET_TYPE (type);
218 vt_val = value_at (wtype, vt_address, NULL);
219 val_print (VALUE_TYPE (vt_val), VALUE_CONTENTS (vt_val), 0,
220 VALUE_ADDRESS (vt_val), stream, format,
221 deref_ref, recurse + 1, pretty);
224 fprintf_filtered (stream, "\n");
225 print_spaces_filtered (2 + 2 * recurse, stream);
230 /* Return number of characters printed, including the terminating
231 '\0' if we reached the end. val_print_string takes care including
232 the terminating '\0' if necessary. */
237 case TYPE_CODE_MEMBER:
238 error ("not implemented: member type in c_val_print");
242 elttype = check_typedef (TYPE_TARGET_TYPE (type));
243 if (TYPE_CODE (elttype) == TYPE_CODE_MEMBER)
245 cp_print_class_member (valaddr + embedded_offset,
246 TYPE_DOMAIN_TYPE (elttype),
252 fprintf_filtered (stream, "@");
253 print_address_numeric
254 (extract_address (valaddr + embedded_offset,
255 TARGET_PTR_BIT / HOST_CHAR_BIT), 1, stream);
257 fputs_filtered (": ", stream);
259 /* De-reference the reference. */
262 if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
264 value_ptr deref_val =
266 (TYPE_TARGET_TYPE (type),
267 unpack_pointer (lookup_pointer_type (builtin_type_void),
268 valaddr + embedded_offset),
270 val_print (VALUE_TYPE (deref_val),
271 VALUE_CONTENTS (deref_val),
273 VALUE_ADDRESS (deref_val),
281 fputs_filtered ("???", stream);
285 case TYPE_CODE_UNION:
286 if (recurse && !unionprint)
288 fprintf_filtered (stream, "{...}");
292 case TYPE_CODE_STRUCT:
293 if (vtblprint && cp_is_vtbl_ptr_type (type))
295 /* Print the unmangled name if desired. */
296 /* Print vtable entry - we only get here if NOT using
297 -fvtable_thunks. (Otherwise, look under TYPE_CODE_PTR.) */
298 print_address_demangle (extract_address (
299 valaddr + embedded_offset +
300 TYPE_FIELD_BITPOS (type, VTBL_FNADDR_OFFSET) / 8,
301 TYPE_LENGTH (TYPE_FIELD_TYPE (type, VTBL_FNADDR_OFFSET))),
305 cp_print_value_fields (type, type, valaddr, embedded_offset, address, stream, format,
306 recurse, pretty, NULL, 0);
312 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
315 len = TYPE_NFIELDS (type);
316 val = unpack_long (type, valaddr + embedded_offset);
317 for (i = 0; i < len; i++)
320 if (val == TYPE_FIELD_BITPOS (type, i))
327 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
331 print_longest (stream, 'd', 0, val);
338 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
341 /* FIXME, we should consider, at least for ANSI C language, eliminating
342 the distinction made between FUNCs and POINTERs to FUNCs. */
343 fprintf_filtered (stream, "{");
344 type_print (type, "", stream, -1);
345 fprintf_filtered (stream, "} ");
346 /* Try to print what function it points to, and its address. */
347 print_address_demangle (address, stream, demangle);
351 format = format ? format : output_format;
353 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
356 val = unpack_long (type, valaddr + embedded_offset);
358 fputs_filtered ("false", stream);
360 fputs_filtered ("true", stream);
362 print_longest (stream, 'd', 0, val);
366 case TYPE_CODE_RANGE:
367 /* FIXME: create_range_type does not set the unsigned bit in a
368 range type (I think it probably should copy it from the target
369 type), so we won't print values which are too large to
370 fit in a signed integer correctly. */
371 /* FIXME: Doesn't handle ranges of enums correctly. (Can't just
372 print with the target type, though, because the size of our type
373 and the target type might differ). */
377 format = format ? format : output_format;
380 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
384 val_print_type_code_int (type, valaddr + embedded_offset, stream);
385 /* C and C++ has no single byte int type, char is used instead.
386 Since we don't know whether the value is really intended to
387 be used as an integer or a character, print the character
388 equivalent as well. */
389 if (TYPE_LENGTH (type) == 1)
391 fputs_filtered (" ", stream);
392 LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr + embedded_offset),
399 format = format ? format : output_format;
402 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
406 val = unpack_long (type, valaddr + embedded_offset);
407 if (TYPE_UNSIGNED (type))
408 fprintf_filtered (stream, "%u", (unsigned int) val);
410 fprintf_filtered (stream, "%d", (int) val);
411 fputs_filtered (" ", stream);
412 LA_PRINT_CHAR ((unsigned char) val, stream);
419 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
423 print_floating (valaddr + embedded_offset, type, stream);
427 case TYPE_CODE_METHOD:
428 cp_print_class_method (valaddr + embedded_offset, lookup_pointer_type (type), stream);
432 fprintf_filtered (stream, "void");
435 case TYPE_CODE_ERROR:
436 fprintf_filtered (stream, "<error type>");
439 case TYPE_CODE_UNDEF:
440 /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
441 dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
442 and no complete type for struct foo in that file. */
443 fprintf_filtered (stream, "<incomplete type>");
447 error ("Invalid C/C++ type code %d in symbol table.", TYPE_CODE (type));
454 c_value_print (val, stream, format, pretty)
456 struct ui_file *stream;
458 enum val_prettyprint pretty;
460 struct type *type = VALUE_TYPE (val);
461 struct type *real_type;
462 int full, top, using_enc;
464 /* If it is a pointer, indicate what it points to.
466 Print type also if it is a reference.
468 C++: if it is a member pointer, we will take care
469 of that when we print it. */
470 if (TYPE_CODE (type) == TYPE_CODE_PTR ||
471 TYPE_CODE (type) == TYPE_CODE_REF)
473 /* Hack: remove (char *) for char strings. Their
474 type is indicated by the quoted string anyway. */
475 if (TYPE_CODE (type) == TYPE_CODE_PTR &&
476 TYPE_NAME (type) == NULL &&
477 TYPE_NAME (TYPE_TARGET_TYPE (type)) != NULL &&
478 STREQ (TYPE_NAME (TYPE_TARGET_TYPE (type)), "char"))
482 else if (objectprint && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CLASS))
484 /* Pointer to class, check real type of object */
485 fprintf_filtered (stream, "(");
486 real_type = value_rtti_target_type (val, &full, &top, &using_enc);
489 /* RTTI entry found */
490 if (TYPE_CODE (type) == TYPE_CODE_PTR)
492 /* create a pointer type pointing to the real type */
493 type = lookup_pointer_type (real_type);
497 /* create a reference type referencing the real type */
498 type = lookup_reference_type (real_type);
500 /* Note: When we look up RTTI entries, we don't get any
501 information on const or volatile attributes */
503 type_print (type, "", stream, -1);
504 fprintf_filtered (stream, ") ");
509 fprintf_filtered (stream, "(");
510 type_print (type, "", stream, -1);
511 fprintf_filtered (stream, ") ");
514 if (objectprint && (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_CLASS))
516 /* Attempt to determine real type of object */
517 real_type = value_rtti_type (val, &full, &top, &using_enc);
520 /* We have RTTI information, so use it */
521 val = value_full_object (val, real_type, full, top, using_enc);
522 fprintf_filtered (stream, "(%s%s) ",
523 TYPE_NAME (real_type),
524 full ? "" : " [incomplete object]");
525 /* Print out object: enclosing type is same as real_type if full */
526 return val_print (VALUE_ENCLOSING_TYPE (val), VALUE_CONTENTS_ALL (val), 0,
527 VALUE_ADDRESS (val), stream, format, 1, 0, pretty);
528 /* Note: When we look up RTTI entries, we don't get any information on
529 const or volatile attributes */
531 else if (type != VALUE_ENCLOSING_TYPE (val))
533 /* No RTTI information, so let's do our best */
534 fprintf_filtered (stream, "(%s ?) ",
535 TYPE_NAME (VALUE_ENCLOSING_TYPE (val)));
536 return val_print (VALUE_ENCLOSING_TYPE (val), VALUE_CONTENTS_ALL (val), 0,
537 VALUE_ADDRESS (val), stream, format, 1, 0, pretty);
539 /* Otherwise, we end up at the return outside this "if" */
542 return val_print (type, VALUE_CONTENTS_ALL (val), VALUE_EMBEDDED_OFFSET (val),
544 stream, format, 1, 0, pretty);