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, 2008, 2009
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
,
42 CORE_ADDR func_addr
= gdbarch_convert_from_func_ptr_addr (current_gdbarch
,
46 /* If the function pointer is represented by a description, print the
47 address of the description. */
48 if (addressprint
&& func_addr
!= address
)
50 fputs_filtered ("@", stream
);
51 fputs_filtered (paddress (address
), stream
);
52 fputs_filtered (": ", stream
);
54 print_address_demangle (func_addr
, stream
, demangle
);
58 /* Apply a heuristic to decide whether an array of TYPE or a pointer
59 to TYPE should be printed as a textual string. Return non-zero if
60 it should, or zero if it should be treated as an array of integers
61 or pointer to integers. FORMAT is the current format letter,
64 We guess that "char" is a character. Explicitly signed and
65 unsigned character types are also characters. Integer data from
66 vector types is not. The user can override this by using the /s
70 textual_element_type (struct type
*type
, char format
)
72 struct type
*true_type
= check_typedef (type
);
74 if (format
!= 0 && format
!= 's')
77 /* TYPE_CODE_CHAR is always textual. */
78 if (TYPE_CODE (true_type
) == TYPE_CODE_CHAR
)
83 /* Print this as a string if we can manage it. For now, no
84 wide character support. */
85 if (TYPE_CODE (true_type
) == TYPE_CODE_INT
86 && TYPE_LENGTH (true_type
) == 1)
91 /* If a one-byte TYPE_CODE_INT is missing the not-a-character
92 flag, then we treat it as text; otherwise, we assume it's
93 being used as data. */
94 if (TYPE_CODE (true_type
) == TYPE_CODE_INT
95 && TYPE_LENGTH (true_type
) == 1
96 && !TYPE_NOTTEXT (true_type
))
104 /* Print data of type TYPE located at VALADDR (within GDB), which came from
105 the inferior at address ADDRESS, onto stdio stream STREAM according to
106 OPTIONS. The data at VALADDR is in target byte order.
108 If the data are a string pointer, returns the number of string characters
112 c_val_print (struct type
*type
, const gdb_byte
*valaddr
, int embedded_offset
,
113 CORE_ADDR address
, struct ui_file
*stream
, int recurse
,
114 const struct value_print_options
*options
)
116 unsigned int i
= 0; /* Number of characters printed */
118 struct type
*elttype
;
123 CHECK_TYPEDEF (type
);
124 switch (TYPE_CODE (type
))
126 case TYPE_CODE_ARRAY
:
127 elttype
= check_typedef (TYPE_TARGET_TYPE (type
));
128 if (TYPE_LENGTH (type
) > 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type
)) > 0)
130 eltlen
= TYPE_LENGTH (elttype
);
131 len
= TYPE_LENGTH (type
) / eltlen
;
132 if (options
->prettyprint_arrays
)
134 print_spaces_filtered (2 + 2 * recurse
, stream
);
137 /* Print arrays of textual chars with a string syntax. */
138 if (textual_element_type (elttype
, options
->format
))
140 /* If requested, look for the first null char and only print
141 elements up to it. */
142 if (options
->stop_print_at_null
)
144 unsigned int temp_len
;
146 /* Look for a NULL char. */
148 (valaddr
+ embedded_offset
)[temp_len
]
149 && temp_len
< len
&& temp_len
< options
->print_max
;
154 LA_PRINT_STRING (stream
, valaddr
+ embedded_offset
, len
, eltlen
, 0, options
);
159 fprintf_filtered (stream
, "{");
160 /* If this is a virtual function table, print the 0th
161 entry specially, and the rest of the members normally. */
162 if (cp_is_vtbl_ptr_type (elttype
))
165 fprintf_filtered (stream
, _("%d vtable entries"), len
- 1);
171 val_print_array_elements (type
, valaddr
+ embedded_offset
, address
, stream
,
172 recurse
, options
, i
);
173 fprintf_filtered (stream
, "}");
177 /* Array of unspecified length: treat like pointer to first elt. */
179 goto print_unpacked_pointer
;
181 case TYPE_CODE_MEMBERPTR
:
184 print_scalar_formatted (valaddr
+ embedded_offset
, type
,
188 cp_print_class_member (valaddr
+ embedded_offset
, type
, stream
, "&");
191 case TYPE_CODE_METHODPTR
:
192 cplus_print_method_ptr (valaddr
+ embedded_offset
, type
, stream
);
196 if (options
->format
&& options
->format
!= 's')
198 print_scalar_formatted (valaddr
+ embedded_offset
, type
,
202 if (options
->vtblprint
&& cp_is_vtbl_ptr_type (type
))
204 /* Print the unmangled name if desired. */
205 /* Print vtable entry - we only get here if we ARE using
206 -fvtable_thunks. (Otherwise, look under TYPE_CODE_STRUCT.) */
208 = extract_typed_address (valaddr
+ embedded_offset
, type
);
209 print_function_pointer_address (addr
, stream
, options
->addressprint
);
212 elttype
= check_typedef (TYPE_TARGET_TYPE (type
));
214 addr
= unpack_pointer (type
, valaddr
+ embedded_offset
);
215 print_unpacked_pointer
:
217 if (TYPE_CODE (elttype
) == TYPE_CODE_FUNC
)
219 /* Try to print what function it points to. */
220 print_function_pointer_address (addr
, stream
,
221 options
->addressprint
);
222 /* Return value is irrelevant except for string pointers. */
226 if (options
->addressprint
)
227 fputs_filtered (paddress (addr
), stream
);
229 /* For a pointer to a textual type, also print the string
230 pointed to, unless pointer is null. */
231 /* FIXME: need to handle wchar_t here... */
233 if (textual_element_type (elttype
, options
->format
)
236 i
= val_print_string (addr
, -1, TYPE_LENGTH (elttype
), stream
,
239 else if (cp_is_vtbl_member (type
))
241 /* print vtbl's nicely */
242 CORE_ADDR vt_address
= unpack_pointer (type
, valaddr
+ embedded_offset
);
244 struct minimal_symbol
*msymbol
=
245 lookup_minimal_symbol_by_pc (vt_address
);
246 if ((msymbol
!= NULL
) &&
247 (vt_address
== SYMBOL_VALUE_ADDRESS (msymbol
)))
249 fputs_filtered (" <", stream
);
250 fputs_filtered (SYMBOL_PRINT_NAME (msymbol
), stream
);
251 fputs_filtered (">", stream
);
253 if (vt_address
&& options
->vtblprint
)
255 struct value
*vt_val
;
256 struct symbol
*wsym
= (struct symbol
*) NULL
;
258 struct block
*block
= (struct block
*) NULL
;
262 wsym
= lookup_symbol (SYMBOL_LINKAGE_NAME (msymbol
), block
,
263 VAR_DOMAIN
, &is_this_fld
);
267 wtype
= SYMBOL_TYPE (wsym
);
271 wtype
= TYPE_TARGET_TYPE (type
);
273 vt_val
= value_at (wtype
, vt_address
);
274 common_val_print (vt_val
, stream
, recurse
+ 1, options
,
278 fprintf_filtered (stream
, "\n");
279 print_spaces_filtered (2 + 2 * recurse
, stream
);
284 /* Return number of characters printed, including the terminating
285 '\0' if we reached the end. val_print_string takes care including
286 the terminating '\0' if necessary. */
292 elttype
= check_typedef (TYPE_TARGET_TYPE (type
));
293 if (options
->addressprint
)
296 = extract_typed_address (valaddr
+ embedded_offset
, type
);
297 fprintf_filtered (stream
, "@");
298 fputs_filtered (paddress (addr
), stream
);
299 if (options
->deref_ref
)
300 fputs_filtered (": ", stream
);
302 /* De-reference the reference. */
303 if (options
->deref_ref
)
305 if (TYPE_CODE (elttype
) != TYPE_CODE_UNDEF
)
307 struct value
*deref_val
=
309 (TYPE_TARGET_TYPE (type
),
310 unpack_pointer (type
, valaddr
+ embedded_offset
));
311 common_val_print (deref_val
, stream
, recurse
, options
,
315 fputs_filtered ("???", stream
);
319 case TYPE_CODE_UNION
:
320 if (recurse
&& !options
->unionprint
)
322 fprintf_filtered (stream
, "{...}");
326 case TYPE_CODE_STRUCT
:
327 /*FIXME: Abstract this away */
328 if (options
->vtblprint
&& cp_is_vtbl_ptr_type (type
))
330 /* Print the unmangled name if desired. */
331 /* Print vtable entry - we only get here if NOT using
332 -fvtable_thunks. (Otherwise, look under TYPE_CODE_PTR.) */
333 int offset
= (embedded_offset
+
334 TYPE_FIELD_BITPOS (type
, VTBL_FNADDR_OFFSET
) / 8);
335 struct type
*field_type
= TYPE_FIELD_TYPE (type
, VTBL_FNADDR_OFFSET
);
337 = extract_typed_address (valaddr
+ offset
, field_type
);
339 print_function_pointer_address (addr
, stream
, options
->addressprint
);
342 cp_print_value_fields (type
, type
, valaddr
, embedded_offset
, address
, stream
,
343 recurse
, options
, NULL
, 0);
349 print_scalar_formatted (valaddr
+ embedded_offset
, type
,
353 len
= TYPE_NFIELDS (type
);
354 val
= unpack_long (type
, valaddr
+ embedded_offset
);
355 for (i
= 0; i
< len
; i
++)
358 if (val
== TYPE_FIELD_BITPOS (type
, i
))
365 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
369 print_longest (stream
, 'd', 0, val
);
373 case TYPE_CODE_FLAGS
:
375 print_scalar_formatted (valaddr
+ embedded_offset
, type
,
378 val_print_type_code_flags (type
, valaddr
+ embedded_offset
, stream
);
382 case TYPE_CODE_METHOD
:
385 print_scalar_formatted (valaddr
+ embedded_offset
, type
,
389 /* FIXME, we should consider, at least for ANSI C language, eliminating
390 the distinction made between FUNCs and POINTERs to FUNCs. */
391 fprintf_filtered (stream
, "{");
392 type_print (type
, "", stream
, -1);
393 fprintf_filtered (stream
, "} ");
394 /* Try to print what function it points to, and its address. */
395 print_address_demangle (address
, stream
, demangle
);
399 if (options
->format
|| options
->output_format
)
401 struct value_print_options opts
= *options
;
402 opts
.format
= (options
->format
? options
->format
403 : options
->output_format
);
404 print_scalar_formatted (valaddr
+ embedded_offset
, type
,
409 val
= unpack_long (type
, valaddr
+ embedded_offset
);
411 fputs_filtered ("false", stream
);
413 fputs_filtered ("true", stream
);
415 print_longest (stream
, 'd', 0, val
);
419 case TYPE_CODE_RANGE
:
420 /* FIXME: create_range_type does not set the unsigned bit in a
421 range type (I think it probably should copy it from the target
422 type), so we won't print values which are too large to
423 fit in a signed integer correctly. */
424 /* FIXME: Doesn't handle ranges of enums correctly. (Can't just
425 print with the target type, though, because the size of our type
426 and the target type might differ). */
430 if (options
->format
|| options
->output_format
)
432 struct value_print_options opts
= *options
;
433 opts
.format
= (options
->format
? options
->format
434 : options
->output_format
);
435 print_scalar_formatted (valaddr
+ embedded_offset
, type
,
440 val_print_type_code_int (type
, valaddr
+ embedded_offset
, stream
);
441 /* C and C++ has no single byte int type, char is used instead.
442 Since we don't know whether the value is really intended to
443 be used as an integer or a character, print the character
444 equivalent as well. */
445 if (textual_element_type (type
, options
->format
))
447 fputs_filtered (" ", stream
);
448 LA_PRINT_CHAR ((unsigned char) unpack_long (type
, valaddr
+ embedded_offset
),
455 if (options
->format
|| options
->output_format
)
457 struct value_print_options opts
= *options
;
458 opts
.format
= (options
->format
? options
->format
459 : options
->output_format
);
460 print_scalar_formatted (valaddr
+ embedded_offset
, type
,
465 val
= unpack_long (type
, valaddr
+ embedded_offset
);
466 if (TYPE_UNSIGNED (type
))
467 fprintf_filtered (stream
, "%u", (unsigned int) val
);
469 fprintf_filtered (stream
, "%d", (int) val
);
470 fputs_filtered (" ", stream
);
471 LA_PRINT_CHAR ((unsigned char) val
, stream
);
478 print_scalar_formatted (valaddr
+ embedded_offset
, type
,
483 print_floating (valaddr
+ embedded_offset
, type
, stream
);
487 case TYPE_CODE_DECFLOAT
:
489 print_scalar_formatted (valaddr
+ embedded_offset
, type
,
492 print_decimal_floating (valaddr
+ embedded_offset
, type
, stream
);
496 fprintf_filtered (stream
, "void");
499 case TYPE_CODE_ERROR
:
500 fprintf_filtered (stream
, _("<error type>"));
503 case TYPE_CODE_UNDEF
:
504 /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
505 dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
506 and no complete type for struct foo in that file. */
507 fprintf_filtered (stream
, _("<incomplete type>"));
510 case TYPE_CODE_COMPLEX
:
512 print_scalar_formatted (valaddr
+ embedded_offset
,
513 TYPE_TARGET_TYPE (type
),
516 print_floating (valaddr
+ embedded_offset
, TYPE_TARGET_TYPE (type
),
518 fprintf_filtered (stream
, " + ");
520 print_scalar_formatted (valaddr
+ embedded_offset
521 + TYPE_LENGTH (TYPE_TARGET_TYPE (type
)),
522 TYPE_TARGET_TYPE (type
),
525 print_floating (valaddr
+ embedded_offset
526 + TYPE_LENGTH (TYPE_TARGET_TYPE (type
)),
527 TYPE_TARGET_TYPE (type
),
529 fprintf_filtered (stream
, " * I");
533 error (_("Invalid C/C++ type code %d in symbol table."), TYPE_CODE (type
));
540 c_value_print (struct value
*val
, struct ui_file
*stream
,
541 const struct value_print_options
*options
)
543 struct type
*type
, *real_type
;
544 int full
, top
, using_enc
;
545 struct value_print_options opts
= *options
;
549 /* If it is a pointer, indicate what it points to.
551 Print type also if it is a reference.
553 C++: if it is a member pointer, we will take care
554 of that when we print it. */
556 type
= check_typedef (value_type (val
));
558 if (TYPE_CODE (type
) == TYPE_CODE_PTR
559 || TYPE_CODE (type
) == TYPE_CODE_REF
)
561 /* Hack: remove (char *) for char strings. Their
562 type is indicated by the quoted string anyway.
563 (Don't use textual_element_type here; quoted strings
564 are always exactly (char *). */
565 if (TYPE_CODE (type
) == TYPE_CODE_PTR
566 && TYPE_NAME (type
) == NULL
567 && TYPE_NAME (TYPE_TARGET_TYPE (type
)) != NULL
568 && strcmp (TYPE_NAME (TYPE_TARGET_TYPE (type
)), "char") == 0)
572 else if (options
->objectprint
573 && (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_CLASS
))
576 if (TYPE_CODE(type
) == TYPE_CODE_REF
)
578 /* Copy value, change to pointer, so we don't get an
579 * error about a non-pointer type in value_rtti_target_type
581 struct value
*temparg
;
582 temparg
=value_copy(val
);
583 deprecated_set_value_type (temparg
, lookup_pointer_type (TYPE_TARGET_TYPE(type
)));
586 /* Pointer to class, check real type of object */
587 fprintf_filtered (stream
, "(");
588 real_type
= value_rtti_target_type (val
, &full
, &top
, &using_enc
);
591 /* RTTI entry found */
592 if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
594 /* create a pointer type pointing to the real type */
595 type
= lookup_pointer_type (real_type
);
599 /* create a reference type referencing the real type */
600 type
= lookup_reference_type (real_type
);
602 /* JYG: Need to adjust pointer value. */
603 /* NOTE: cagney/2005-01-02: THIS IS BOGUS. */
604 value_contents_writeable (val
)[0] -= top
;
606 /* Note: When we look up RTTI entries, we don't get any
607 information on const or volatile attributes */
609 type_print (type
, "", stream
, -1);
610 fprintf_filtered (stream
, ") ");
615 fprintf_filtered (stream
, "(");
616 type_print (value_type (val
), "", stream
, -1);
617 fprintf_filtered (stream
, ") ");
621 if (!value_initialized (val
))
622 fprintf_filtered (stream
, " [uninitialized] ");
624 if (options
->objectprint
&& (TYPE_CODE (type
) == TYPE_CODE_CLASS
))
626 /* Attempt to determine real type of object */
627 real_type
= value_rtti_type (val
, &full
, &top
, &using_enc
);
630 /* We have RTTI information, so use it */
631 val
= value_full_object (val
, real_type
, full
, top
, using_enc
);
632 fprintf_filtered (stream
, "(%s%s) ",
633 TYPE_NAME (real_type
),
634 full
? "" : _(" [incomplete object]"));
635 /* Print out object: enclosing type is same as real_type if full */
636 return val_print (value_enclosing_type (val
),
637 value_contents_all (val
), 0,
638 VALUE_ADDRESS (val
), stream
, 0,
639 &opts
, current_language
);
640 /* Note: When we look up RTTI entries, we don't get any information on
641 const or volatile attributes */
643 else if (type
!= check_typedef (value_enclosing_type (val
)))
645 /* No RTTI information, so let's do our best */
646 fprintf_filtered (stream
, "(%s ?) ",
647 TYPE_NAME (value_enclosing_type (val
)));
648 return val_print (value_enclosing_type (val
),
649 value_contents_all (val
), 0,
650 VALUE_ADDRESS (val
), stream
, 0,
651 &opts
, current_language
);
653 /* Otherwise, we end up at the return outside this "if" */
656 return val_print (type
, value_contents_all (val
),
657 value_embedded_offset (val
),
658 VALUE_ADDRESS (val
) + value_offset (val
),
659 stream
, 0, &opts
, current_language
);
This page took 0.042814 seconds and 4 git commands to generate.