cd4c85a5fb3587864bea4af9c7ef498f11eadd83
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 3 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, see <http://www.gnu.org/licenses/>. */
23 #include "gdb_string.h"
26 #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
= gdbarch_convert_from_func_ptr_addr (current_gdbarch
,
45 /* If the function pointer is represented by a description, print the
46 address of the description. */
47 if (addressprint
&& func_addr
!= address
)
49 fputs_filtered ("@", stream
);
50 deprecated_print_address_numeric (address
, 1, stream
);
51 fputs_filtered (": ", stream
);
53 print_address_demangle (func_addr
, stream
, demangle
);
57 /* Apply a heuristic to decide whether an array of TYPE or a pointer
58 to TYPE should be printed as a textual string. Return non-zero if
59 it should, or zero if it should be treated as an array of integers
60 or pointer to integers. FORMAT is the current format letter,
63 We guess that "char" is a character. Explicitly signed and
64 unsigned character types are also characters. Integer data from
65 vector types is not. The user can override this by using the /s
69 textual_element_type (struct type
*type
, char format
)
71 struct type
*true_type
= check_typedef (type
);
73 if (format
!= 0 && format
!= 's')
76 /* TYPE_CODE_CHAR is always textual. */
77 if (TYPE_CODE (true_type
) == TYPE_CODE_CHAR
)
82 /* Print this as a string if we can manage it. For now, no
83 wide character support. */
84 if (TYPE_CODE (true_type
) == TYPE_CODE_INT
85 && TYPE_LENGTH (true_type
) == 1)
90 /* If a one-byte TYPE_CODE_INT is missing the not-a-character
91 flag, then we treat it as text; otherwise, we assume it's
92 being used as data. */
93 if (TYPE_CODE (true_type
) == TYPE_CODE_INT
94 && TYPE_LENGTH (true_type
) == 1
95 && !TYPE_NOTTEXT (true_type
))
103 /* Print data of type TYPE located at VALADDR (within GDB), which came from
104 the inferior at address ADDRESS, onto stdio stream STREAM according to
105 FORMAT (a letter or 0 for natural format). The data at VALADDR is in
108 If the data are a string pointer, returns the number of string characters
111 If DEREF_REF is nonzero, then dereference references, otherwise just print
114 The PRETTY parameter controls prettyprinting. */
117 c_val_print (struct type
*type
, const gdb_byte
*valaddr
, int embedded_offset
,
118 CORE_ADDR address
, struct ui_file
*stream
, int format
,
119 int deref_ref
, int recurse
, enum val_prettyprint pretty
)
121 unsigned int i
= 0; /* Number of characters printed */
123 struct type
*elttype
;
128 CHECK_TYPEDEF (type
);
129 switch (TYPE_CODE (type
))
131 case TYPE_CODE_ARRAY
:
132 elttype
= check_typedef (TYPE_TARGET_TYPE (type
));
133 if (TYPE_LENGTH (type
) > 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type
)) > 0)
135 eltlen
= TYPE_LENGTH (elttype
);
136 len
= TYPE_LENGTH (type
) / eltlen
;
137 if (prettyprint_arrays
)
139 print_spaces_filtered (2 + 2 * recurse
, stream
);
142 /* Print arrays of textual chars with a string syntax. */
143 if (textual_element_type (elttype
, format
))
145 /* If requested, look for the first null char and only print
146 elements up to it. */
147 if (stop_print_at_null
)
149 unsigned int temp_len
;
151 /* Look for a NULL char. */
153 (valaddr
+ embedded_offset
)[temp_len
]
154 && temp_len
< len
&& temp_len
< print_max
;
159 LA_PRINT_STRING (stream
, valaddr
+ embedded_offset
, len
, eltlen
, 0);
164 fprintf_filtered (stream
, "{");
165 /* If this is a virtual function table, print the 0th
166 entry specially, and the rest of the members normally. */
167 if (cp_is_vtbl_ptr_type (elttype
))
170 fprintf_filtered (stream
, _("%d vtable entries"), len
- 1);
176 val_print_array_elements (type
, valaddr
+ embedded_offset
, address
, stream
,
177 format
, deref_ref
, recurse
, pretty
, i
);
178 fprintf_filtered (stream
, "}");
182 /* Array of unspecified length: treat like pointer to first elt. */
184 goto print_unpacked_pointer
;
186 case TYPE_CODE_MEMBERPTR
:
189 print_scalar_formatted (valaddr
+ embedded_offset
, type
, format
, 0, stream
);
192 cp_print_class_member (valaddr
+ embedded_offset
,
193 TYPE_DOMAIN_TYPE (type
),
197 case TYPE_CODE_METHODPTR
:
198 cplus_print_method_ptr (valaddr
+ embedded_offset
, type
, stream
);
202 if (format
&& format
!= 's')
204 print_scalar_formatted (valaddr
+ embedded_offset
, type
, format
, 0, stream
);
207 if (vtblprint
&& cp_is_vtbl_ptr_type (type
))
209 /* Print the unmangled name if desired. */
210 /* Print vtable entry - we only get here if we ARE using
211 -fvtable_thunks. (Otherwise, look under TYPE_CODE_STRUCT.) */
213 = extract_typed_address (valaddr
+ embedded_offset
, type
);
214 print_function_pointer_address (addr
, stream
);
217 elttype
= check_typedef (TYPE_TARGET_TYPE (type
));
219 addr
= unpack_pointer (type
, valaddr
+ embedded_offset
);
220 print_unpacked_pointer
:
222 if (TYPE_CODE (elttype
) == TYPE_CODE_FUNC
)
224 /* Try to print what function it points to. */
225 print_function_pointer_address (addr
, stream
);
226 /* Return value is irrelevant except for string pointers. */
232 deprecated_print_address_numeric (addr
, 1, stream
);
235 /* For a pointer to a textual type, also print the string
236 pointed to, unless pointer is null. */
237 /* FIXME: need to handle wchar_t here... */
239 if (textual_element_type (elttype
, format
) && addr
!= 0)
241 i
= val_print_string (addr
, -1, TYPE_LENGTH (elttype
), stream
);
243 else if (cp_is_vtbl_member (type
))
245 /* print vtbl's nicely */
246 CORE_ADDR vt_address
= unpack_pointer (type
, valaddr
+ embedded_offset
);
248 struct minimal_symbol
*msymbol
=
249 lookup_minimal_symbol_by_pc (vt_address
);
250 if ((msymbol
!= NULL
) &&
251 (vt_address
== SYMBOL_VALUE_ADDRESS (msymbol
)))
253 fputs_filtered (" <", stream
);
254 fputs_filtered (SYMBOL_PRINT_NAME (msymbol
), stream
);
255 fputs_filtered (">", stream
);
257 if (vt_address
&& vtblprint
)
259 struct value
*vt_val
;
260 struct symbol
*wsym
= (struct symbol
*) NULL
;
262 struct block
*block
= (struct block
*) NULL
;
266 wsym
= lookup_symbol (DEPRECATED_SYMBOL_NAME (msymbol
), block
,
267 VAR_DOMAIN
, &is_this_fld
, NULL
);
271 wtype
= SYMBOL_TYPE (wsym
);
275 wtype
= TYPE_TARGET_TYPE (type
);
277 vt_val
= value_at (wtype
, vt_address
);
278 common_val_print (vt_val
, stream
, format
,
279 deref_ref
, recurse
+ 1, pretty
);
282 fprintf_filtered (stream
, "\n");
283 print_spaces_filtered (2 + 2 * recurse
, stream
);
288 /* Return number of characters printed, including the terminating
289 '\0' if we reached the end. val_print_string takes care including
290 the terminating '\0' if necessary. */
296 elttype
= check_typedef (TYPE_TARGET_TYPE (type
));
300 = extract_typed_address (valaddr
+ embedded_offset
, type
);
301 fprintf_filtered (stream
, "@");
302 deprecated_print_address_numeric (addr
, 1, stream
);
304 fputs_filtered (": ", stream
);
306 /* De-reference the reference. */
309 if (TYPE_CODE (elttype
) != TYPE_CODE_UNDEF
)
311 struct value
*deref_val
=
313 (TYPE_TARGET_TYPE (type
),
314 unpack_pointer (lookup_pointer_type (builtin_type_void
),
315 valaddr
+ embedded_offset
));
316 common_val_print (deref_val
, stream
, format
, deref_ref
,
320 fputs_filtered ("???", stream
);
324 case TYPE_CODE_UNION
:
325 if (recurse
&& !unionprint
)
327 fprintf_filtered (stream
, "{...}");
331 case TYPE_CODE_STRUCT
:
332 /*FIXME: Abstract this away */
333 if (vtblprint
&& cp_is_vtbl_ptr_type (type
))
335 /* Print the unmangled name if desired. */
336 /* Print vtable entry - we only get here if NOT using
337 -fvtable_thunks. (Otherwise, look under TYPE_CODE_PTR.) */
338 int offset
= (embedded_offset
+
339 TYPE_FIELD_BITPOS (type
, VTBL_FNADDR_OFFSET
) / 8);
340 struct type
*field_type
= TYPE_FIELD_TYPE (type
, VTBL_FNADDR_OFFSET
);
342 = extract_typed_address (valaddr
+ offset
, field_type
);
344 print_function_pointer_address (addr
, stream
);
347 cp_print_value_fields (type
, type
, valaddr
, embedded_offset
, address
, stream
, format
,
348 recurse
, pretty
, NULL
, 0);
354 print_scalar_formatted (valaddr
+ embedded_offset
, type
, format
, 0, stream
);
357 len
= TYPE_NFIELDS (type
);
358 val
= unpack_long (type
, valaddr
+ embedded_offset
);
359 for (i
= 0; i
< len
; i
++)
362 if (val
== TYPE_FIELD_BITPOS (type
, i
))
369 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
373 print_longest (stream
, 'd', 0, val
);
377 case TYPE_CODE_FLAGS
:
379 print_scalar_formatted (valaddr
+ embedded_offset
, type
, format
, 0, stream
);
381 val_print_type_code_flags (type
, valaddr
+ embedded_offset
, stream
);
385 case TYPE_CODE_METHOD
:
388 print_scalar_formatted (valaddr
+ embedded_offset
, type
, format
, 0, stream
);
391 /* FIXME, we should consider, at least for ANSI C language, eliminating
392 the distinction made between FUNCs and POINTERs to FUNCs. */
393 fprintf_filtered (stream
, "{");
394 type_print (type
, "", stream
, -1);
395 fprintf_filtered (stream
, "} ");
396 /* Try to print what function it points to, and its address. */
397 print_address_demangle (address
, stream
, demangle
);
401 format
= format
? format
: output_format
;
403 print_scalar_formatted (valaddr
+ embedded_offset
, type
, format
, 0, stream
);
406 val
= unpack_long (type
, valaddr
+ embedded_offset
);
408 fputs_filtered ("false", stream
);
410 fputs_filtered ("true", stream
);
412 print_longest (stream
, 'd', 0, val
);
416 case TYPE_CODE_RANGE
:
417 /* FIXME: create_range_type does not set the unsigned bit in a
418 range type (I think it probably should copy it from the target
419 type), so we won't print values which are too large to
420 fit in a signed integer correctly. */
421 /* FIXME: Doesn't handle ranges of enums correctly. (Can't just
422 print with the target type, though, because the size of our type
423 and the target type might differ). */
427 format
= format
? format
: output_format
;
430 print_scalar_formatted (valaddr
+ embedded_offset
, type
, format
, 0, stream
);
434 val_print_type_code_int (type
, valaddr
+ embedded_offset
, stream
);
435 /* C and C++ has no single byte int type, char is used instead.
436 Since we don't know whether the value is really intended to
437 be used as an integer or a character, print the character
438 equivalent as well. */
439 if (textual_element_type (type
, format
))
441 fputs_filtered (" ", stream
);
442 LA_PRINT_CHAR ((unsigned char) unpack_long (type
, valaddr
+ embedded_offset
),
449 format
= format
? format
: output_format
;
452 print_scalar_formatted (valaddr
+ embedded_offset
, type
, format
, 0, stream
);
456 val
= unpack_long (type
, valaddr
+ embedded_offset
);
457 if (TYPE_UNSIGNED (type
))
458 fprintf_filtered (stream
, "%u", (unsigned int) val
);
460 fprintf_filtered (stream
, "%d", (int) val
);
461 fputs_filtered (" ", stream
);
462 LA_PRINT_CHAR ((unsigned char) val
, stream
);
469 print_scalar_formatted (valaddr
+ embedded_offset
, type
, format
, 0, stream
);
473 print_floating (valaddr
+ embedded_offset
, type
, stream
);
478 fprintf_filtered (stream
, "void");
481 case TYPE_CODE_ERROR
:
482 fprintf_filtered (stream
, _("<error type>"));
485 case TYPE_CODE_UNDEF
:
486 /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
487 dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
488 and no complete type for struct foo in that file. */
489 fprintf_filtered (stream
, _("<incomplete type>"));
492 case TYPE_CODE_COMPLEX
:
494 print_scalar_formatted (valaddr
+ embedded_offset
,
495 TYPE_TARGET_TYPE (type
),
498 print_floating (valaddr
+ embedded_offset
, TYPE_TARGET_TYPE (type
),
500 fprintf_filtered (stream
, " + ");
502 print_scalar_formatted (valaddr
+ embedded_offset
503 + TYPE_LENGTH (TYPE_TARGET_TYPE (type
)),
504 TYPE_TARGET_TYPE (type
),
507 print_floating (valaddr
+ embedded_offset
508 + TYPE_LENGTH (TYPE_TARGET_TYPE (type
)),
509 TYPE_TARGET_TYPE (type
),
511 fprintf_filtered (stream
, " * I");
515 error (_("Invalid C/C++ type code %d in symbol table."), TYPE_CODE (type
));
522 c_value_print (struct value
*val
, struct ui_file
*stream
, int format
,
523 enum val_prettyprint pretty
)
525 struct type
*type
, *real_type
;
526 int full
, top
, using_enc
;
528 /* If it is a pointer, indicate what it points to.
530 Print type also if it is a reference.
532 C++: if it is a member pointer, we will take care
533 of that when we print it. */
535 type
= check_typedef (value_type (val
));
537 if (TYPE_CODE (type
) == TYPE_CODE_PTR
538 || TYPE_CODE (type
) == TYPE_CODE_REF
)
540 /* Hack: remove (char *) for char strings. Their
541 type is indicated by the quoted string anyway.
542 (Don't use textual_element_type here; quoted strings
543 are always exactly (char *). */
544 if (TYPE_CODE (type
) == TYPE_CODE_PTR
545 && TYPE_NAME (type
) == NULL
546 && TYPE_NAME (TYPE_TARGET_TYPE (type
)) != NULL
547 && strcmp (TYPE_NAME (TYPE_TARGET_TYPE (type
)), "char") == 0)
551 else if (objectprint
&& (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_CLASS
))
554 if (TYPE_CODE(type
) == TYPE_CODE_REF
)
556 /* Copy value, change to pointer, so we don't get an
557 * error about a non-pointer type in value_rtti_target_type
559 struct value
*temparg
;
560 temparg
=value_copy(val
);
561 deprecated_set_value_type (temparg
, lookup_pointer_type (TYPE_TARGET_TYPE(type
)));
564 /* Pointer to class, check real type of object */
565 fprintf_filtered (stream
, "(");
566 real_type
= value_rtti_target_type (val
, &full
, &top
, &using_enc
);
569 /* RTTI entry found */
570 if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
572 /* create a pointer type pointing to the real type */
573 type
= lookup_pointer_type (real_type
);
577 /* create a reference type referencing the real type */
578 type
= lookup_reference_type (real_type
);
580 /* JYG: Need to adjust pointer value. */
581 /* NOTE: cagney/2005-01-02: THIS IS BOGUS. */
582 value_contents_writeable (val
)[0] -= top
;
584 /* Note: When we look up RTTI entries, we don't get any
585 information on const or volatile attributes */
587 type_print (type
, "", stream
, -1);
588 fprintf_filtered (stream
, ") ");
593 fprintf_filtered (stream
, "(");
594 type_print (value_type (val
), "", stream
, -1);
595 fprintf_filtered (stream
, ") ");
599 if (!value_initialized (val
))
600 fprintf_filtered (stream
, " [uninitialized] ");
602 if (objectprint
&& (TYPE_CODE (type
) == TYPE_CODE_CLASS
))
604 /* Attempt to determine real type of object */
605 real_type
= value_rtti_type (val
, &full
, &top
, &using_enc
);
608 /* We have RTTI information, so use it */
609 val
= value_full_object (val
, real_type
, full
, top
, using_enc
);
610 fprintf_filtered (stream
, "(%s%s) ",
611 TYPE_NAME (real_type
),
612 full
? "" : _(" [incomplete object]"));
613 /* Print out object: enclosing type is same as real_type if full */
614 return val_print (value_enclosing_type (val
),
615 value_contents_all (val
), 0,
616 VALUE_ADDRESS (val
), stream
, format
, 1, 0, pretty
);
617 /* Note: When we look up RTTI entries, we don't get any information on
618 const or volatile attributes */
620 else if (type
!= check_typedef (value_enclosing_type (val
)))
622 /* No RTTI information, so let's do our best */
623 fprintf_filtered (stream
, "(%s ?) ",
624 TYPE_NAME (value_enclosing_type (val
)));
625 return val_print (value_enclosing_type (val
),
626 value_contents_all (val
), 0,
627 VALUE_ADDRESS (val
), stream
, format
, 1, 0, pretty
);
629 /* Otherwise, we end up at the return outside this "if" */
632 return val_print (type
, value_contents_all (val
),
633 value_embedded_offset (val
),
634 VALUE_ADDRESS (val
) + value_offset (val
),
635 stream
, format
, 1, 0, pretty
);