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
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 2 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, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA. */
25 #include "gdb_string.h"
28 #include "expression.h"
37 /* Print function pointer with inferior address ADDRESS onto stdio
41 print_function_pointer_address (CORE_ADDR address, struct ui_file *stream)
43 CORE_ADDR func_addr = gdbarch_convert_from_func_ptr_addr (current_gdbarch,
47 /* If the function pointer is represented by a description, print the
48 address of the description. */
49 if (addressprint && func_addr != address)
51 fputs_filtered ("@", stream);
52 deprecated_print_address_numeric (address, 1, stream);
53 fputs_filtered (": ", stream);
55 print_address_demangle (func_addr, stream, demangle);
59 /* Print data of type TYPE located at VALADDR (within GDB), which came from
60 the inferior at address ADDRESS, onto stdio stream STREAM according to
61 FORMAT (a letter or 0 for natural format). The data at VALADDR is in
64 If the data are a string pointer, returns the number of string characters
67 If DEREF_REF is nonzero, then dereference references, otherwise just print
70 The PRETTY parameter controls prettyprinting. */
73 c_val_print (struct type *type, const gdb_byte *valaddr, int embedded_offset,
74 CORE_ADDR address, struct ui_file *stream, int format,
75 int deref_ref, int recurse, enum val_prettyprint pretty)
77 unsigned int i = 0; /* Number of characters printed */
85 switch (TYPE_CODE (type))
88 elttype = check_typedef (TYPE_TARGET_TYPE (type));
89 if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0)
91 eltlen = TYPE_LENGTH (elttype);
92 len = TYPE_LENGTH (type) / eltlen;
93 if (prettyprint_arrays)
95 print_spaces_filtered (2 + 2 * recurse, stream);
97 /* For an array of chars, print with string syntax. */
99 ((TYPE_CODE (elttype) == TYPE_CODE_INT && TYPE_NOSIGN (elttype))
100 || ((current_language->la_language == language_m2)
101 && (TYPE_CODE (elttype) == TYPE_CODE_CHAR)))
102 && (format == 0 || format == 's'))
104 /* If requested, look for the first null char and only print
105 elements up to it. */
106 if (stop_print_at_null)
108 unsigned int temp_len;
110 /* Look for a NULL char. */
112 (valaddr + embedded_offset)[temp_len]
113 && temp_len < len && temp_len < print_max;
118 LA_PRINT_STRING (stream, valaddr + embedded_offset, len, eltlen, 0);
123 fprintf_filtered (stream, "{");
124 /* If this is a virtual function table, print the 0th
125 entry specially, and the rest of the members normally. */
126 if (cp_is_vtbl_ptr_type (elttype))
129 fprintf_filtered (stream, _("%d vtable entries"), len - 1);
135 val_print_array_elements (type, valaddr + embedded_offset, address, stream,
136 format, deref_ref, recurse, pretty, i);
137 fprintf_filtered (stream, "}");
141 /* Array of unspecified length: treat like pointer to first elt. */
143 goto print_unpacked_pointer;
145 case TYPE_CODE_MEMBERPTR:
148 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
151 cp_print_class_member (valaddr + embedded_offset,
152 TYPE_DOMAIN_TYPE (type),
156 case TYPE_CODE_METHODPTR:
157 cplus_print_method_ptr (valaddr + embedded_offset, type, stream);
161 if (format && format != 's')
163 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
166 if (vtblprint && cp_is_vtbl_ptr_type (type))
168 /* Print the unmangled name if desired. */
169 /* Print vtable entry - we only get here if we ARE using
170 -fvtable_thunks. (Otherwise, look under TYPE_CODE_STRUCT.) */
172 = extract_typed_address (valaddr + embedded_offset, type);
173 print_function_pointer_address (addr, stream);
176 elttype = check_typedef (TYPE_TARGET_TYPE (type));
178 addr = unpack_pointer (type, valaddr + embedded_offset);
179 print_unpacked_pointer:
181 if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
183 /* Try to print what function it points to. */
184 print_function_pointer_address (addr, stream);
185 /* Return value is irrelevant except for string pointers. */
189 if (addressprint && format != 's')
191 deprecated_print_address_numeric (addr, 1, stream);
194 /* For a pointer to char or unsigned char, also print the string
195 pointed to, unless pointer is null. */
196 /* FIXME: need to handle wchar_t here... */
198 if (TYPE_LENGTH (elttype) == 1
199 && TYPE_CODE (elttype) == TYPE_CODE_INT
200 && (format == 0 || format == 's')
203 i = val_print_string (addr, -1, TYPE_LENGTH (elttype), stream);
205 else if (cp_is_vtbl_member (type))
207 /* print vtbl's nicely */
208 CORE_ADDR vt_address = unpack_pointer (type, valaddr + embedded_offset);
210 struct minimal_symbol *msymbol =
211 lookup_minimal_symbol_by_pc (vt_address);
212 if ((msymbol != NULL) &&
213 (vt_address == SYMBOL_VALUE_ADDRESS (msymbol)))
215 fputs_filtered (" <", stream);
216 fputs_filtered (SYMBOL_PRINT_NAME (msymbol), stream);
217 fputs_filtered (">", stream);
219 if (vt_address && vtblprint)
221 struct value *vt_val;
222 struct symbol *wsym = (struct symbol *) NULL;
224 struct block *block = (struct block *) NULL;
228 wsym = lookup_symbol (DEPRECATED_SYMBOL_NAME (msymbol), block,
229 VAR_DOMAIN, &is_this_fld, NULL);
233 wtype = SYMBOL_TYPE (wsym);
237 wtype = TYPE_TARGET_TYPE (type);
239 vt_val = value_at (wtype, vt_address);
240 common_val_print (vt_val, stream, format,
241 deref_ref, recurse + 1, pretty);
244 fprintf_filtered (stream, "\n");
245 print_spaces_filtered (2 + 2 * recurse, stream);
250 /* Return number of characters printed, including the terminating
251 '\0' if we reached the end. val_print_string takes care including
252 the terminating '\0' if necessary. */
258 elttype = check_typedef (TYPE_TARGET_TYPE (type));
262 = extract_typed_address (valaddr + embedded_offset, type);
263 fprintf_filtered (stream, "@");
264 deprecated_print_address_numeric (addr, 1, stream);
266 fputs_filtered (": ", stream);
268 /* De-reference the reference. */
271 if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
273 struct value *deref_val =
275 (TYPE_TARGET_TYPE (type),
276 unpack_pointer (lookup_pointer_type (builtin_type_void),
277 valaddr + embedded_offset));
278 common_val_print (deref_val, stream, format, deref_ref,
282 fputs_filtered ("???", stream);
286 case TYPE_CODE_UNION:
287 if (recurse && !unionprint)
289 fprintf_filtered (stream, "{...}");
293 case TYPE_CODE_STRUCT:
294 /*FIXME: Abstract this away */
295 if (vtblprint && cp_is_vtbl_ptr_type (type))
297 /* Print the unmangled name if desired. */
298 /* Print vtable entry - we only get here if NOT using
299 -fvtable_thunks. (Otherwise, look under TYPE_CODE_PTR.) */
300 int offset = (embedded_offset +
301 TYPE_FIELD_BITPOS (type, VTBL_FNADDR_OFFSET) / 8);
302 struct type *field_type = TYPE_FIELD_TYPE (type, VTBL_FNADDR_OFFSET);
304 = extract_typed_address (valaddr + offset, field_type);
306 print_function_pointer_address (addr, stream);
309 cp_print_value_fields (type, type, valaddr, embedded_offset, address, stream, format,
310 recurse, pretty, NULL, 0);
316 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
319 len = TYPE_NFIELDS (type);
320 val = unpack_long (type, valaddr + embedded_offset);
321 for (i = 0; i < len; i++)
324 if (val == TYPE_FIELD_BITPOS (type, i))
331 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
335 print_longest (stream, 'd', 0, val);
339 case TYPE_CODE_FLAGS:
341 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
343 val_print_type_code_flags (type, valaddr + embedded_offset, stream);
347 case TYPE_CODE_METHOD:
350 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
353 /* FIXME, we should consider, at least for ANSI C language, eliminating
354 the distinction made between FUNCs and POINTERs to FUNCs. */
355 fprintf_filtered (stream, "{");
356 type_print (type, "", stream, -1);
357 fprintf_filtered (stream, "} ");
358 /* Try to print what function it points to, and its address. */
359 print_address_demangle (address, stream, demangle);
363 format = format ? format : output_format;
365 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
368 val = unpack_long (type, valaddr + embedded_offset);
370 fputs_filtered ("false", stream);
372 fputs_filtered ("true", stream);
374 print_longest (stream, 'd', 0, val);
378 case TYPE_CODE_RANGE:
379 /* FIXME: create_range_type does not set the unsigned bit in a
380 range type (I think it probably should copy it from the target
381 type), so we won't print values which are too large to
382 fit in a signed integer correctly. */
383 /* FIXME: Doesn't handle ranges of enums correctly. (Can't just
384 print with the target type, though, because the size of our type
385 and the target type might differ). */
389 format = format ? format : output_format;
392 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
396 val_print_type_code_int (type, valaddr + embedded_offset, stream);
397 /* C and C++ has no single byte int type, char is used instead.
398 Since we don't know whether the value is really intended to
399 be used as an integer or a character, print the character
400 equivalent as well. */
401 if (TYPE_LENGTH (type) == 1)
403 fputs_filtered (" ", stream);
404 LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr + embedded_offset),
411 format = format ? format : output_format;
414 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
418 val = unpack_long (type, valaddr + embedded_offset);
419 if (TYPE_UNSIGNED (type))
420 fprintf_filtered (stream, "%u", (unsigned int) val);
422 fprintf_filtered (stream, "%d", (int) val);
423 fputs_filtered (" ", stream);
424 LA_PRINT_CHAR ((unsigned char) val, stream);
431 print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream);
435 print_floating (valaddr + embedded_offset, type, stream);
440 fprintf_filtered (stream, "void");
443 case TYPE_CODE_ERROR:
444 fprintf_filtered (stream, _("<error type>"));
447 case TYPE_CODE_UNDEF:
448 /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
449 dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
450 and no complete type for struct foo in that file. */
451 fprintf_filtered (stream, _("<incomplete type>"));
454 case TYPE_CODE_COMPLEX:
456 print_scalar_formatted (valaddr + embedded_offset,
457 TYPE_TARGET_TYPE (type),
460 print_floating (valaddr + embedded_offset, TYPE_TARGET_TYPE (type),
462 fprintf_filtered (stream, " + ");
464 print_scalar_formatted (valaddr + embedded_offset
465 + TYPE_LENGTH (TYPE_TARGET_TYPE (type)),
466 TYPE_TARGET_TYPE (type),
469 print_floating (valaddr + embedded_offset
470 + TYPE_LENGTH (TYPE_TARGET_TYPE (type)),
471 TYPE_TARGET_TYPE (type),
473 fprintf_filtered (stream, " * I");
477 error (_("Invalid C/C++ type code %d in symbol table."), TYPE_CODE (type));
484 c_value_print (struct value *val, struct ui_file *stream, int format,
485 enum val_prettyprint pretty)
487 struct type *type, *real_type;
488 int full, top, using_enc;
490 /* If it is a pointer, indicate what it points to.
492 Print type also if it is a reference.
494 C++: if it is a member pointer, we will take care
495 of that when we print it. */
497 type = check_typedef (value_type (val));
499 if (TYPE_CODE (type) == TYPE_CODE_PTR
500 || TYPE_CODE (type) == TYPE_CODE_REF)
502 /* Hack: remove (char *) for char strings. Their
503 type is indicated by the quoted string anyway. */
504 if (TYPE_CODE (type) == TYPE_CODE_PTR
505 && TYPE_NAME (type) == NULL
506 && TYPE_NAME (TYPE_TARGET_TYPE (type)) != NULL
507 && strcmp (TYPE_NAME (TYPE_TARGET_TYPE (type)), "char") == 0)
511 else if (objectprint && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CLASS))
514 if (TYPE_CODE(type) == TYPE_CODE_REF)
516 /* Copy value, change to pointer, so we don't get an
517 * error about a non-pointer type in value_rtti_target_type
519 struct value *temparg;
520 temparg=value_copy(val);
521 deprecated_set_value_type (temparg, lookup_pointer_type (TYPE_TARGET_TYPE(type)));
524 /* Pointer to class, check real type of object */
525 fprintf_filtered (stream, "(");
526 real_type = value_rtti_target_type (val, &full, &top, &using_enc);
529 /* RTTI entry found */
530 if (TYPE_CODE (type) == TYPE_CODE_PTR)
532 /* create a pointer type pointing to the real type */
533 type = lookup_pointer_type (real_type);
537 /* create a reference type referencing the real type */
538 type = lookup_reference_type (real_type);
540 /* JYG: Need to adjust pointer value. */
541 /* NOTE: cagney/2005-01-02: THIS IS BOGUS. */
542 value_contents_writeable (val)[0] -= top;
544 /* Note: When we look up RTTI entries, we don't get any
545 information on const or volatile attributes */
547 type_print (type, "", stream, -1);
548 fprintf_filtered (stream, ") ");
553 fprintf_filtered (stream, "(");
554 type_print (value_type (val), "", stream, -1);
555 fprintf_filtered (stream, ") ");
559 if (objectprint && (TYPE_CODE (type) == TYPE_CODE_CLASS))
561 /* Attempt to determine real type of object */
562 real_type = value_rtti_type (val, &full, &top, &using_enc);
565 /* We have RTTI information, so use it */
566 val = value_full_object (val, real_type, full, top, using_enc);
567 fprintf_filtered (stream, "(%s%s) ",
568 TYPE_NAME (real_type),
569 full ? "" : _(" [incomplete object]"));
570 /* Print out object: enclosing type is same as real_type if full */
571 return val_print (value_enclosing_type (val),
572 value_contents_all (val), 0,
573 VALUE_ADDRESS (val), stream, format, 1, 0, pretty);
574 /* Note: When we look up RTTI entries, we don't get any information on
575 const or volatile attributes */
577 else if (type != check_typedef (value_enclosing_type (val)))
579 /* No RTTI information, so let's do our best */
580 fprintf_filtered (stream, "(%s ?) ",
581 TYPE_NAME (value_enclosing_type (val)));
582 return val_print (value_enclosing_type (val),
583 value_contents_all (val), 0,
584 VALUE_ADDRESS (val), stream, format, 1, 0, pretty);
586 /* Otherwise, we end up at the return outside this "if" */
589 return val_print (type, value_contents_all (val),
590 value_embedded_offset (val),
591 VALUE_ADDRESS (val) + value_offset (val),
592 stream, format, 1, 0, pretty);