1 /* Support for printing C values for GDB, the GNU debugger.
3 Copyright 1986, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996,
4 1997, 1998, 1999, 2000, 2001, 2003 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
24 #include "gdb_string.h"
27 #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)
41 CORE_ADDR func_addr = CONVERT_FROM_FUNC_PTR_ADDR (address);
43 /* If the function pointer is represented by a description, print the
44 address of the description. */
45 if (addressprint && func_addr != address)
47 fputs_filtered ("@", stream);
48 print_address_numeric (address, 1, stream);
49 fputs_filtered (": ", stream);
51 print_address_demangle (func_addr, stream, demangle);
55 /* Print data of type TYPE located at VALADDR (within GDB), which came from
56 the inferior at address ADDRESS, onto stdio stream STREAM according to
57 FORMAT (a letter or 0 for natural format). The data at VALADDR is in
60 If the data are a string pointer, returns the number of string characters
63 If DEREF_REF is nonzero, then dereference references, otherwise just print
66 The PRETTY parameter controls prettyprinting. */
69 c_val_print (struct type *type, char *valaddr, int embedded_offset,
70 CORE_ADDR address, struct ui_file *stream, int format,
71 int deref_ref, int recurse, enum val_prettyprint pretty)
73 register unsigned int i = 0; /* Number of characters printed */
81 switch (TYPE_CODE (type))
84 elttype = check_typedef (TYPE_TARGET_TYPE (type));
85 if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0)
87 eltlen = TYPE_LENGTH (elttype);
88 len = TYPE_LENGTH (type) / eltlen;
89 if (prettyprint_arrays)
91 print_spaces_filtered (2 + 2 * recurse, stream);
93 /* For an array of chars, print with string syntax. */
95 ((TYPE_CODE (elttype) == TYPE_CODE_INT)
96 || ((current_language->la_language == language_m2)
97 && (TYPE_CODE (elttype) == TYPE_CODE_CHAR)))
98 && (format == 0 || format == 's'))
100 /* If requested, look for the first null char and only print
101 elements up to it. */
102 if (stop_print_at_null)
104 unsigned int temp_len;
106 /* Look for a NULL char. */
108 (valaddr + embedded_offset)[temp_len]
109 && temp_len < len && temp_len < print_max;
114 LA_PRINT_STRING (stream, valaddr + embedded_offset, len, eltlen, 0);
119 fprintf_filtered (stream, "{");
120 /* If this is a virtual function table, print the 0th
121 entry specially, and the rest of the members normally. */
122 if (cp_is_vtbl_ptr_type (elttype))
125 fprintf_filtered (stream, "%d vtable entries", len - 1);
131 val_print_array_elements (type, valaddr + embedded_offset, address, stream,
132 format, deref_ref, recurse, pretty, i);
133 fprintf_filtered (stream, "}");
137 /* Array of unspecified length: treat like pointer to first elt. */
139 goto print_unpacked_pointer;
142 if (format && format != 's')
144 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
147 if (vtblprint && cp_is_vtbl_ptr_type (type))
149 /* Print the unmangled name if desired. */
150 /* Print vtable entry - we only get here if we ARE using
151 -fvtable_thunks. (Otherwise, look under TYPE_CODE_STRUCT.) */
153 = extract_typed_address (valaddr + embedded_offset, type);
154 print_function_pointer_address (addr, stream);
157 elttype = check_typedef (TYPE_TARGET_TYPE (type));
158 if (TYPE_CODE (elttype) == TYPE_CODE_METHOD)
160 cp_print_class_method (valaddr + embedded_offset, type, stream);
162 else if (TYPE_CODE (elttype) == TYPE_CODE_MEMBER)
164 cp_print_class_member (valaddr + embedded_offset,
165 TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (type)),
170 addr = unpack_pointer (type, valaddr + embedded_offset);
171 print_unpacked_pointer:
173 if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
175 /* Try to print what function it points to. */
176 print_function_pointer_address (addr, stream);
177 /* Return value is irrelevant except for string pointers. */
181 if (addressprint && format != 's')
183 print_address_numeric (addr, 1, stream);
186 /* For a pointer to char or unsigned char, also print the string
187 pointed to, unless pointer is null. */
188 /* FIXME: need to handle wchar_t here... */
190 if (TYPE_LENGTH (elttype) == 1
191 && TYPE_CODE (elttype) == TYPE_CODE_INT
192 && (format == 0 || format == 's')
195 i = val_print_string (addr, -1, TYPE_LENGTH (elttype), stream);
197 else if (cp_is_vtbl_member (type))
199 /* print vtbl's nicely */
200 CORE_ADDR vt_address = unpack_pointer (type, valaddr + embedded_offset);
202 struct minimal_symbol *msymbol =
203 lookup_minimal_symbol_by_pc (vt_address);
204 if ((msymbol != NULL) &&
205 (vt_address == SYMBOL_VALUE_ADDRESS (msymbol)))
207 fputs_filtered (" <", stream);
208 fputs_filtered (SYMBOL_PRINT_NAME (msymbol), stream);
209 fputs_filtered (">", stream);
211 if (vt_address && vtblprint)
213 struct value *vt_val;
214 struct symbol *wsym = (struct symbol *) NULL;
216 struct block *block = (struct block *) NULL;
220 wsym = lookup_symbol (DEPRECATED_SYMBOL_NAME (msymbol), block,
221 VAR_DOMAIN, &is_this_fld, NULL);
225 wtype = SYMBOL_TYPE (wsym);
229 wtype = TYPE_TARGET_TYPE (type);
231 vt_val = value_at (wtype, vt_address, NULL);
232 val_print (VALUE_TYPE (vt_val), VALUE_CONTENTS (vt_val), 0,
233 VALUE_ADDRESS (vt_val), stream, format,
234 deref_ref, recurse + 1, pretty);
237 fprintf_filtered (stream, "\n");
238 print_spaces_filtered (2 + 2 * recurse, stream);
243 /* Return number of characters printed, including the terminating
244 '\0' if we reached the end. val_print_string takes care including
245 the terminating '\0' if necessary. */
250 case TYPE_CODE_MEMBER:
251 error ("not implemented: member type in c_val_print");
255 elttype = check_typedef (TYPE_TARGET_TYPE (type));
256 if (TYPE_CODE (elttype) == TYPE_CODE_MEMBER)
258 cp_print_class_member (valaddr + embedded_offset,
259 TYPE_DOMAIN_TYPE (elttype),
266 = extract_typed_address (valaddr + embedded_offset, type);
267 fprintf_filtered (stream, "@");
268 print_address_numeric (addr, 1, stream);
270 fputs_filtered (": ", stream);
272 /* De-reference the reference. */
275 if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
277 struct value *deref_val =
279 (TYPE_TARGET_TYPE (type),
280 unpack_pointer (lookup_pointer_type (builtin_type_void),
281 valaddr + embedded_offset),
283 val_print (VALUE_TYPE (deref_val),
284 VALUE_CONTENTS (deref_val),
286 VALUE_ADDRESS (deref_val),
294 fputs_filtered ("???", stream);
298 case TYPE_CODE_UNION:
299 if (recurse && !unionprint)
301 fprintf_filtered (stream, "{...}");
305 case TYPE_CODE_STRUCT:
306 /*FIXME: Abstract this away */
307 if (vtblprint && cp_is_vtbl_ptr_type (type))
309 /* Print the unmangled name if desired. */
310 /* Print vtable entry - we only get here if NOT using
311 -fvtable_thunks. (Otherwise, look under TYPE_CODE_PTR.) */
312 int offset = (embedded_offset +
313 TYPE_FIELD_BITPOS (type, VTBL_FNADDR_OFFSET) / 8);
314 struct type *field_type = TYPE_FIELD_TYPE (type, VTBL_FNADDR_OFFSET);
316 = extract_typed_address (valaddr + offset, field_type);
318 print_function_pointer_address (addr, stream);
321 cp_print_value_fields (type, type, valaddr, embedded_offset, address, stream, format,
322 recurse, pretty, NULL, 0);
328 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
331 len = TYPE_NFIELDS (type);
332 val = unpack_long (type, valaddr + embedded_offset);
333 for (i = 0; i < len; i++)
336 if (val == TYPE_FIELD_BITPOS (type, i))
343 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
347 print_longest (stream, 'd', 0, val);
354 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
357 /* FIXME, we should consider, at least for ANSI C language, eliminating
358 the distinction made between FUNCs and POINTERs to FUNCs. */
359 fprintf_filtered (stream, "{");
360 type_print (type, "", stream, -1);
361 fprintf_filtered (stream, "} ");
362 /* Try to print what function it points to, and its address. */
363 print_address_demangle (address, stream, demangle);
367 format = format ? format : output_format;
369 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
372 val = unpack_long (type, valaddr + embedded_offset);
374 fputs_filtered ("false", stream);
376 fputs_filtered ("true", stream);
378 print_longest (stream, 'd', 0, val);
382 case TYPE_CODE_RANGE:
383 /* FIXME: create_range_type does not set the unsigned bit in a
384 range type (I think it probably should copy it from the target
385 type), so we won't print values which are too large to
386 fit in a signed integer correctly. */
387 /* FIXME: Doesn't handle ranges of enums correctly. (Can't just
388 print with the target type, though, because the size of our type
389 and the target type might differ). */
393 format = format ? format : output_format;
396 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
400 val_print_type_code_int (type, valaddr + embedded_offset, stream);
401 /* C and C++ has no single byte int type, char is used instead.
402 Since we don't know whether the value is really intended to
403 be used as an integer or a character, print the character
404 equivalent as well. */
405 if (TYPE_LENGTH (type) == 1)
407 fputs_filtered (" ", stream);
408 LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr + embedded_offset),
415 format = format ? format : output_format;
418 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
422 val = unpack_long (type, valaddr + embedded_offset);
423 if (TYPE_UNSIGNED (type))
424 fprintf_filtered (stream, "%u", (unsigned int) val);
426 fprintf_filtered (stream, "%d", (int) val);
427 fputs_filtered (" ", stream);
428 LA_PRINT_CHAR ((unsigned char) val, stream);
435 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
439 print_floating (valaddr + embedded_offset, type, stream);
443 case TYPE_CODE_METHOD:
445 struct value *v = value_at (type, address, NULL);
446 cp_print_class_method (VALUE_CONTENTS (value_addr (v)),
447 lookup_pointer_type (type), stream);
452 fprintf_filtered (stream, "void");
455 case TYPE_CODE_ERROR:
456 fprintf_filtered (stream, "<error type>");
459 case TYPE_CODE_UNDEF:
460 /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
461 dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
462 and no complete type for struct foo in that file. */
463 fprintf_filtered (stream, "<incomplete type>");
466 case TYPE_CODE_COMPLEX:
468 print_scalar_formatted (valaddr + embedded_offset,
469 TYPE_TARGET_TYPE (type),
472 print_floating (valaddr + embedded_offset, TYPE_TARGET_TYPE (type),
474 fprintf_filtered (stream, " + ");
476 print_scalar_formatted (valaddr + embedded_offset
477 + TYPE_LENGTH (TYPE_TARGET_TYPE (type)),
478 TYPE_TARGET_TYPE (type),
481 print_floating (valaddr + embedded_offset
482 + TYPE_LENGTH (TYPE_TARGET_TYPE (type)),
483 TYPE_TARGET_TYPE (type),
485 fprintf_filtered (stream, " * I");
489 error ("Invalid C/C++ type code %d in symbol table.", TYPE_CODE (type));
496 c_value_print (struct value *val, struct ui_file *stream, int format,
497 enum val_prettyprint pretty)
499 struct type *type = VALUE_TYPE (val);
500 struct type *real_type;
501 int full, top, using_enc;
503 /* If it is a pointer, indicate what it points to.
505 Print type also if it is a reference.
507 C++: if it is a member pointer, we will take care
508 of that when we print it. */
509 if (TYPE_CODE (type) == TYPE_CODE_PTR ||
510 TYPE_CODE (type) == TYPE_CODE_REF)
512 /* Hack: remove (char *) for char strings. Their
513 type is indicated by the quoted string anyway. */
514 if (TYPE_CODE (type) == TYPE_CODE_PTR &&
515 TYPE_NAME (type) == NULL &&
516 TYPE_NAME (TYPE_TARGET_TYPE (type)) != NULL &&
517 strcmp (TYPE_NAME (TYPE_TARGET_TYPE (type)), "char") == 0)
521 else if (objectprint && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CLASS))
524 if (TYPE_CODE(type) == TYPE_CODE_REF)
526 /* Copy value, change to pointer, so we don't get an
527 * error about a non-pointer type in value_rtti_target_type
529 struct value *temparg;
530 temparg=value_copy(val);
531 VALUE_TYPE (temparg) = lookup_pointer_type(TYPE_TARGET_TYPE(type));
534 /* Pointer to class, check real type of object */
535 fprintf_filtered (stream, "(");
536 real_type = value_rtti_target_type (val, &full, &top, &using_enc);
539 /* RTTI entry found */
540 if (TYPE_CODE (type) == TYPE_CODE_PTR)
542 /* create a pointer type pointing to the real type */
543 type = lookup_pointer_type (real_type);
547 /* create a reference type referencing the real type */
548 type = lookup_reference_type (real_type);
550 /* JYG: Need to adjust pointer value. */
551 val->aligner.contents[0] -= top;
553 /* Note: When we look up RTTI entries, we don't get any
554 information on const or volatile attributes */
556 type_print (type, "", stream, -1);
557 fprintf_filtered (stream, ") ");
562 fprintf_filtered (stream, "(");
563 type_print (type, "", stream, -1);
564 fprintf_filtered (stream, ") ");
567 if (objectprint && (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_CLASS))
569 /* Attempt to determine real type of object */
570 real_type = value_rtti_type (val, &full, &top, &using_enc);
573 /* We have RTTI information, so use it */
574 val = value_full_object (val, real_type, full, top, using_enc);
575 fprintf_filtered (stream, "(%s%s) ",
576 TYPE_NAME (real_type),
577 full ? "" : " [incomplete object]");
578 /* Print out object: enclosing type is same as real_type if full */
579 return val_print (VALUE_ENCLOSING_TYPE (val), VALUE_CONTENTS_ALL (val), 0,
580 VALUE_ADDRESS (val), stream, format, 1, 0, pretty);
581 /* Note: When we look up RTTI entries, we don't get any information on
582 const or volatile attributes */
584 else if (type != VALUE_ENCLOSING_TYPE (val))
586 /* No RTTI information, so let's do our best */
587 fprintf_filtered (stream, "(%s ?) ",
588 TYPE_NAME (VALUE_ENCLOSING_TYPE (val)));
589 return val_print (VALUE_ENCLOSING_TYPE (val), VALUE_CONTENTS_ALL (val), 0,
590 VALUE_ADDRESS (val), stream, format, 1, 0, pretty);
592 /* Otherwise, we end up at the return outside this "if" */
595 return val_print (type, VALUE_CONTENTS_ALL (val), VALUE_EMBEDDED_OFFSET (val),
597 stream, format, 1, 0, pretty);