1 /* Support for printing C values for GDB, the GNU debugger.
2 Copyright 1986, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997
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, Boston, MA 02111-1307, USA. */
24 #include "expression.h"
32 /* Print data of type TYPE located at VALADDR (within GDB), which came from
33 the inferior at address ADDRESS, onto stdio stream STREAM according to
34 FORMAT (a letter or 0 for natural format). The data at VALADDR is in
37 If the data are a string pointer, returns the number of string characters
40 If DEREF_REF is nonzero, then dereference references, otherwise just print
43 The PRETTY parameter controls prettyprinting. */
46 c_val_print (type, valaddr, embedded_offset, address, stream, format, deref_ref, recurse,
56 enum val_prettyprint pretty;
58 register unsigned int i = 0; /* Number of characters printed */
66 switch (TYPE_CODE (type))
69 elttype = check_typedef (TYPE_TARGET_TYPE (type));
70 if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0)
72 eltlen = TYPE_LENGTH (elttype);
73 len = TYPE_LENGTH (type) / eltlen;
74 if (prettyprint_arrays)
76 print_spaces_filtered (2 + 2 * recurse, stream);
78 /* For an array of chars, print with string syntax. */
80 ((TYPE_CODE (elttype) == TYPE_CODE_INT)
81 || ((current_language->la_language == language_m2)
82 && (TYPE_CODE (elttype) == TYPE_CODE_CHAR)))
83 && (format == 0 || format == 's'))
85 /* If requested, look for the first null char and only print
87 if (stop_print_at_null)
91 /* Look for a NULL char. */
93 (valaddr + embedded_offset)[temp_len]
94 && temp_len < len && temp_len < print_max;
99 LA_PRINT_STRING (stream, valaddr + embedded_offset, len, eltlen, 0);
104 fprintf_filtered (stream, "{");
105 /* If this is a virtual function table, print the 0th
106 entry specially, and the rest of the members normally. */
107 if (cp_is_vtbl_ptr_type (elttype))
110 fprintf_filtered (stream, "%d vtable entries", len - 1);
116 val_print_array_elements (type, valaddr + embedded_offset, address, stream,
117 format, deref_ref, recurse, pretty, i);
118 fprintf_filtered (stream, "}");
122 /* Array of unspecified length: treat like pointer to first elt. */
124 goto print_unpacked_pointer;
127 if (format && format != 's')
129 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
132 if (vtblprint && cp_is_vtbl_ptr_type(type))
134 /* Print the unmangled name if desired. */
135 /* Print vtable entry - we only get here if we ARE using
136 -fvtable_thunks. (Otherwise, look under TYPE_CODE_STRUCT.) */
137 print_address_demangle(extract_address (valaddr + embedded_offset, TYPE_LENGTH (type)),
141 elttype = check_typedef (TYPE_TARGET_TYPE (type));
142 if (TYPE_CODE (elttype) == TYPE_CODE_METHOD)
144 cp_print_class_method (valaddr + embedded_offset, type, stream);
146 else if (TYPE_CODE (elttype) == TYPE_CODE_MEMBER)
148 cp_print_class_member (valaddr + embedded_offset,
149 TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (type)),
154 addr = unpack_pointer (type, valaddr + embedded_offset);
155 print_unpacked_pointer:
156 elttype = check_typedef (TYPE_TARGET_TYPE (type));
158 if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
160 /* Try to print what function it points to. */
161 print_address_demangle (addr, stream, demangle);
162 /* Return value is irrelevant except for string pointers. */
166 if (addressprint && format != 's')
168 print_address_numeric (addr, 1, stream);
171 /* For a pointer to char or unsigned char, also print the string
172 pointed to, unless pointer is null. */
173 /* FIXME: need to handle wchar_t here... */
175 if (TYPE_LENGTH (elttype) == 1
176 && TYPE_CODE (elttype) == TYPE_CODE_INT
177 && (format == 0 || format == 's')
180 i = val_print_string (addr, -1, TYPE_LENGTH (elttype), stream);
182 else if (cp_is_vtbl_member(type))
184 /* print vtbl's nicely */
185 CORE_ADDR vt_address = unpack_pointer (type, valaddr + embedded_offset);
187 struct minimal_symbol *msymbol =
188 lookup_minimal_symbol_by_pc (vt_address);
189 if ((msymbol != NULL) &&
190 (vt_address == SYMBOL_VALUE_ADDRESS (msymbol)))
192 fputs_filtered (" <", stream);
193 fputs_filtered (SYMBOL_SOURCE_NAME (msymbol), stream);
194 fputs_filtered (">", stream);
196 if (vt_address && vtblprint)
199 struct symbol *wsym = (struct symbol *)NULL;
202 struct block *block = (struct block *)NULL;
206 wsym = lookup_symbol (SYMBOL_NAME(msymbol), block,
207 VAR_NAMESPACE, &is_this_fld, &s);
211 wtype = SYMBOL_TYPE(wsym);
215 wtype = TYPE_TARGET_TYPE(type);
217 vt_val = value_at (wtype, vt_address, NULL);
218 val_print (VALUE_TYPE (vt_val), VALUE_CONTENTS (vt_val), 0,
219 VALUE_ADDRESS (vt_val), stream, format,
220 deref_ref, recurse + 1, pretty);
223 fprintf_filtered (stream, "\n");
224 print_spaces_filtered (2 + 2 * recurse, stream);
229 /* Return number of characters printed, including the terminating
230 '\0' if we reached the end. val_print_string takes care including
231 the terminating '\0' if necessary. */
236 case TYPE_CODE_MEMBER:
237 error ("not implemented: member type in c_val_print");
241 elttype = check_typedef (TYPE_TARGET_TYPE (type));
242 if (TYPE_CODE (elttype) == TYPE_CODE_MEMBER)
244 cp_print_class_member (valaddr + embedded_offset,
245 TYPE_DOMAIN_TYPE (elttype),
251 fprintf_filtered (stream, "@");
252 print_address_numeric
253 (extract_address (valaddr + embedded_offset,
254 TARGET_PTR_BIT / HOST_CHAR_BIT), 1, stream);
256 fputs_filtered (": ", stream);
258 /* De-reference the reference. */
261 if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
263 value_ptr deref_val =
265 (TYPE_TARGET_TYPE (type),
266 unpack_pointer (lookup_pointer_type (builtin_type_void),
267 valaddr + embedded_offset),
269 val_print (VALUE_TYPE (deref_val),
270 VALUE_CONTENTS (deref_val),
272 VALUE_ADDRESS (deref_val),
280 fputs_filtered ("???", stream);
284 case TYPE_CODE_UNION:
285 if (recurse && !unionprint)
287 fprintf_filtered (stream, "{...}");
291 case TYPE_CODE_STRUCT:
292 if (vtblprint && cp_is_vtbl_ptr_type(type))
294 /* Print the unmangled name if desired. */
295 /* Print vtable entry - we only get here if NOT using
296 -fvtable_thunks. (Otherwise, look under TYPE_CODE_PTR.) */
297 print_address_demangle (extract_address (
298 valaddr + embedded_offset +
299 TYPE_FIELD_BITPOS (type, VTBL_FNADDR_OFFSET) / 8,
300 TYPE_LENGTH (TYPE_FIELD_TYPE (type, VTBL_FNADDR_OFFSET))),
304 cp_print_value_fields (type, type, valaddr, embedded_offset, address, stream, format,
305 recurse, pretty, NULL, 0);
311 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
314 len = TYPE_NFIELDS (type);
315 val = unpack_long (type, valaddr + embedded_offset);
316 for (i = 0; i < len; i++)
319 if (val == TYPE_FIELD_BITPOS (type, i))
326 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
330 print_longest (stream, 'd', 0, val);
337 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
340 /* FIXME, we should consider, at least for ANSI C language, eliminating
341 the distinction made between FUNCs and POINTERs to FUNCs. */
342 fprintf_filtered (stream, "{");
343 type_print (type, "", stream, -1);
344 fprintf_filtered (stream, "} ");
345 /* Try to print what function it points to, and its address. */
346 print_address_demangle (address, stream, demangle);
350 format = format ? format : output_format;
352 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
355 val = unpack_long (type, valaddr + embedded_offset);
357 fputs_filtered ("false", stream);
359 fputs_filtered ("true", stream);
361 print_longest (stream, 'd', 0, val);
365 case TYPE_CODE_RANGE:
366 /* FIXME: create_range_type does not set the unsigned bit in a
367 range type (I think it probably should copy it from the target
368 type), so we won't print values which are too large to
369 fit in a signed integer correctly. */
370 /* FIXME: Doesn't handle ranges of enums correctly. (Can't just
371 print with the target type, though, because the size of our type
372 and the target type might differ). */
376 format = format ? format : output_format;
379 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
383 val_print_type_code_int (type, valaddr + embedded_offset, stream);
384 /* C and C++ has no single byte int type, char is used instead.
385 Since we don't know whether the value is really intended to
386 be used as an integer or a character, print the character
387 equivalent as well. */
388 if (TYPE_LENGTH (type) == 1)
390 fputs_filtered (" ", stream);
391 LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr + embedded_offset),
398 format = format ? format : output_format;
401 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
405 fprintf_filtered (stream, TYPE_UNSIGNED (type) ? "%u" : "%d",
406 unpack_long (type, valaddr + embedded_offset));
407 fputs_filtered (" ", stream);
408 LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr + embedded_offset), stream);
415 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
419 print_floating (valaddr + embedded_offset, type, stream);
423 case TYPE_CODE_METHOD:
424 cp_print_class_method (valaddr + embedded_offset, lookup_pointer_type (type), stream);
428 fprintf_filtered (stream, "void");
431 case TYPE_CODE_ERROR:
432 fprintf_filtered (stream, "<error type>");
435 case TYPE_CODE_UNDEF:
436 /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
437 dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
438 and no complete type for struct foo in that file. */
439 fprintf_filtered (stream, "<incomplete type>");
443 error ("Invalid C/C++ type code %d in symbol table.", TYPE_CODE (type));
450 c_value_print (val, stream, format, pretty)
454 enum val_prettyprint pretty;
456 struct type *type = VALUE_TYPE (val);
457 struct type * real_type;
458 int full, top, using_enc;
460 /* If it is a pointer, indicate what it points to.
462 Print type also if it is a reference.
464 C++: if it is a member pointer, we will take care
465 of that when we print it. */
466 if (TYPE_CODE (type) == TYPE_CODE_PTR ||
467 TYPE_CODE (type) == TYPE_CODE_REF)
469 /* Hack: remove (char *) for char strings. Their
470 type is indicated by the quoted string anyway. */
471 /* Hack^2: ensure that const char *, const unsigned char *
472 come out without the type but the volatile variants
473 and the signed variants don't. */
474 if (TYPE_CODE (type) == TYPE_CODE_PTR &&
475 TYPE_NAME (type) == NULL &&
476 TYPE_NAME (TYPE_TARGET_TYPE (type)) != NULL &&
477 STREQ (TYPE_NAME (TYPE_TARGET_TYPE (type)), "char"))
481 else if (objectprint && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CLASS))
483 /* Pointer to class, check real type of object */
484 fprintf_filtered (stream, "(");
485 type = value_rtti_target_type (val, &full, &top, &using_enc);
488 /* RTTI entry found */
489 type = lookup_pointer_type (type);
490 type_print (type, "", stream, -1);
494 /* No RTTI fields, do whatever we can */
495 type = VALUE_ENCLOSING_TYPE (val);
496 type_print (type, "", stream, -1);
497 fprintf_filtered (stream, " ?");
499 fprintf_filtered (stream, ") ");
504 fprintf_filtered (stream, "(");
505 type_print (type, "", stream, -1);
506 fprintf_filtered (stream, ") ");
509 if (objectprint && (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_CLASS))
511 /* Attempt to determine real type of object */
512 real_type = value_rtti_type (val, &full, &top, &using_enc);
515 /* We have RTTI information, so use it */
516 val = value_full_object (val, real_type, full, top, using_enc);
517 fprintf_filtered (stream, "(%s%s) ",
518 TYPE_NAME (real_type),
519 full ? "" : " [incomplete object]");
520 /* Print out object: enclosing type is same as real_type if full */
521 return val_print (VALUE_ENCLOSING_TYPE (val), VALUE_CONTENTS_ALL (val), 0,
522 VALUE_ADDRESS (val), stream, format, 1, 0, pretty);
524 else if (type != VALUE_ENCLOSING_TYPE (val))
526 /* No RTTI information, so let's do our best */
527 fprintf_filtered (stream, "(%s ?) ",
528 TYPE_NAME (VALUE_ENCLOSING_TYPE (val)));
529 return val_print (VALUE_ENCLOSING_TYPE (val), VALUE_CONTENTS_ALL (val), 0,
530 VALUE_ADDRESS (val), stream, format, 1, 0, pretty);
532 /* Otherwise, we end up at the return outside this "if" */
535 return val_print (type, VALUE_CONTENTS_ALL (val), VALUE_EMBEDDED_OFFSET (val),
537 stream, format, 1, 0, pretty);