1 /* Support for printing C values for GDB, the GNU debugger.
3 Copyright (C) 1986-2019 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 3 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, see <http://www.gnu.org/licenses/>. */
22 /* Local non-gdb includes. */
25 #include "expression.h"
36 /* A helper for c_textual_element_type. This checks the name of the
37 typedef. This is bogus but it isn't apparent that the compiler
38 provides us the help we may need. */
41 textual_name (const char *name
)
43 return (!strcmp (name
, "wchar_t")
44 || !strcmp (name
, "char16_t")
45 || !strcmp (name
, "char32_t"));
48 /* Apply a heuristic to decide whether an array of TYPE or a pointer
49 to TYPE should be printed as a textual string. Return non-zero if
50 it should, or zero if it should be treated as an array of integers
51 or pointer to integers. FORMAT is the current format letter, or 0
54 We guess that "char" is a character. Explicitly signed and
55 unsigned character types are also characters. Integer data from
56 vector types is not. The user can override this by using the /s
60 c_textual_element_type (struct type
*type
, char format
)
62 struct type
*true_type
, *iter_type
;
64 if (format
!= 0 && format
!= 's')
67 /* We also rely on this for its side effect of setting up all the
69 true_type
= check_typedef (type
);
71 /* TYPE_CODE_CHAR is always textual. */
72 if (TYPE_CODE (true_type
) == TYPE_CODE_CHAR
)
75 /* Any other character-like types must be integral. */
76 if (TYPE_CODE (true_type
) != TYPE_CODE_INT
)
79 /* We peel typedefs one by one, looking for a match. */
83 /* Check the name of the type. */
84 if (TYPE_NAME (iter_type
) && textual_name (TYPE_NAME (iter_type
)))
87 if (TYPE_CODE (iter_type
) != TYPE_CODE_TYPEDEF
)
90 /* Peel a single typedef. If the typedef doesn't have a target
91 type, we use check_typedef and hope the result is ok -- it
92 might be for C++, where wchar_t is a built-in type. */
93 if (TYPE_TARGET_TYPE (iter_type
))
94 iter_type
= TYPE_TARGET_TYPE (iter_type
);
96 iter_type
= check_typedef (iter_type
);
101 /* Print this as a string if we can manage it. For now, no wide
102 character support. */
103 if (TYPE_CODE (true_type
) == TYPE_CODE_INT
104 && TYPE_LENGTH (true_type
) == 1)
109 /* If a one-byte TYPE_CODE_INT is missing the not-a-character
110 flag, then we treat it as text; otherwise, we assume it's
111 being used as data. */
112 if (TYPE_CODE (true_type
) == TYPE_CODE_INT
113 && TYPE_LENGTH (true_type
) == 1
114 && !TYPE_NOTTEXT (true_type
))
121 /* Decorations for C. */
123 static const struct generic_val_print_decorations c_decorations
=
135 /* Print a pointer based on the type of its target.
137 Arguments to this functions are roughly the same as those in c_val_print.
138 A difference is that ADDRESS is the address to print, with embedded_offset
139 already added. UNRESOLVED_ELTTYPE and ELTTYPE represent the pointed type,
140 respectively before and after check_typedef. */
143 print_unpacked_pointer (struct type
*type
, struct type
*elttype
,
144 struct type
*unresolved_elttype
,
145 const gdb_byte
*valaddr
, int embedded_offset
,
146 CORE_ADDR address
, struct ui_file
*stream
, int recurse
,
147 const struct value_print_options
*options
)
150 struct gdbarch
*gdbarch
= get_type_arch (type
);
152 if (TYPE_CODE (elttype
) == TYPE_CODE_FUNC
)
154 /* Try to print what function it points to. */
155 print_function_pointer_address (options
, gdbarch
, address
, stream
);
159 if (options
->symbol_print
)
160 want_space
= print_address_demangle (options
, gdbarch
, address
, stream
,
162 else if (options
->addressprint
)
164 fputs_filtered (paddress (gdbarch
, address
), stream
);
168 /* For a pointer to a textual type, also print the string
169 pointed to, unless pointer is null. */
171 if (c_textual_element_type (unresolved_elttype
, options
->format
)
175 fputs_filtered (" ", stream
);
176 val_print_string (unresolved_elttype
, NULL
, address
, -1, stream
, options
);
178 else if (cp_is_vtbl_member (type
))
180 /* Print vtbl's nicely. */
181 CORE_ADDR vt_address
= unpack_pointer (type
, valaddr
+ embedded_offset
);
182 struct bound_minimal_symbol msymbol
=
183 lookup_minimal_symbol_by_pc (vt_address
);
185 /* If 'symbol_print' is set, we did the work above. */
186 if (!options
->symbol_print
187 && (msymbol
.minsym
!= NULL
)
188 && (vt_address
== BMSYMBOL_VALUE_ADDRESS (msymbol
)))
191 fputs_filtered (" ", stream
);
192 fputs_filtered (" <", stream
);
193 fputs_filtered (MSYMBOL_PRINT_NAME (msymbol
.minsym
), stream
);
194 fputs_filtered (">", stream
);
198 if (vt_address
&& options
->vtblprint
)
200 struct value
*vt_val
;
201 struct symbol
*wsym
= NULL
;
205 fputs_filtered (" ", stream
);
207 if (msymbol
.minsym
!= NULL
)
209 const char *search_name
210 = MSYMBOL_SEARCH_NAME (msymbol
.minsym
);
211 wsym
= lookup_symbol_search_name (search_name
, NULL
,
217 wtype
= SYMBOL_TYPE (wsym
);
221 wtype
= unresolved_elttype
;
223 vt_val
= value_at (wtype
, vt_address
);
224 common_val_print (vt_val
, stream
, recurse
+ 1, options
,
226 if (options
->prettyformat
)
228 fprintf_filtered (stream
, "\n");
229 print_spaces_filtered (2 + 2 * recurse
, stream
);
235 /* c_val_print helper for TYPE_CODE_ARRAY. */
238 c_val_print_array (struct type
*type
, const gdb_byte
*valaddr
,
239 int embedded_offset
, CORE_ADDR address
,
240 struct ui_file
*stream
, int recurse
,
241 struct value
*original_value
,
242 const struct value_print_options
*options
)
244 struct type
*unresolved_elttype
= TYPE_TARGET_TYPE (type
);
245 struct type
*elttype
= check_typedef (unresolved_elttype
);
246 struct gdbarch
*arch
= get_type_arch (type
);
247 int unit_size
= gdbarch_addressable_memory_unit_size (arch
);
249 if (TYPE_LENGTH (type
) > 0 && TYPE_LENGTH (unresolved_elttype
) > 0)
251 LONGEST low_bound
, high_bound
;
253 struct gdbarch
*gdbarch
= get_type_arch (type
);
254 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
255 unsigned int i
= 0; /* Number of characters printed. */
257 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
258 error (_("Could not determine the array high bound"));
260 eltlen
= TYPE_LENGTH (elttype
);
261 len
= high_bound
- low_bound
+ 1;
262 if (options
->prettyformat_arrays
)
264 print_spaces_filtered (2 + 2 * recurse
, stream
);
267 /* Print arrays of textual chars with a string syntax, as
268 long as the entire array is valid. */
269 if (c_textual_element_type (unresolved_elttype
,
271 && value_bytes_available (original_value
, embedded_offset
,
273 && !value_bits_any_optimized_out (original_value
,
274 TARGET_CHAR_BIT
* embedded_offset
,
275 TARGET_CHAR_BIT
* TYPE_LENGTH (type
)))
277 int force_ellipses
= 0;
279 /* If requested, look for the first null char and only
280 print elements up to it. */
281 if (options
->stop_print_at_null
)
283 unsigned int temp_len
;
287 && temp_len
< options
->print_max
288 && extract_unsigned_integer (valaddr
289 + embedded_offset
* unit_size
291 eltlen
, byte_order
) != 0);
295 /* Force LA_PRINT_STRING to print ellipses if
296 we've printed the maximum characters and
297 the next character is not \000. */
298 if (temp_len
== options
->print_max
&& temp_len
< len
)
301 = extract_unsigned_integer (valaddr
302 + embedded_offset
* unit_size
312 LA_PRINT_STRING (stream
, unresolved_elttype
,
313 valaddr
+ embedded_offset
* unit_size
, len
,
314 NULL
, force_ellipses
, options
);
319 fprintf_filtered (stream
, "{");
320 /* If this is a virtual function table, print the 0th
321 entry specially, and the rest of the members
323 if (cp_is_vtbl_ptr_type (elttype
))
326 fprintf_filtered (stream
, _("%d vtable entries"),
333 val_print_array_elements (type
, embedded_offset
,
335 recurse
, original_value
, options
, i
);
336 fprintf_filtered (stream
, "}");
341 /* Array of unspecified length: treat like pointer to first elt. */
342 print_unpacked_pointer (type
, elttype
, unresolved_elttype
, valaddr
,
343 embedded_offset
, address
+ embedded_offset
,
344 stream
, recurse
, options
);
348 /* c_val_print helper for TYPE_CODE_PTR. */
351 c_val_print_ptr (struct type
*type
, const gdb_byte
*valaddr
,
352 int embedded_offset
, struct ui_file
*stream
, int recurse
,
353 struct value
*original_value
,
354 const struct value_print_options
*options
)
356 struct gdbarch
*arch
= get_type_arch (type
);
357 int unit_size
= gdbarch_addressable_memory_unit_size (arch
);
359 if (options
->format
&& options
->format
!= 's')
361 val_print_scalar_formatted (type
, embedded_offset
,
362 original_value
, options
, 0, stream
);
364 else if (options
->vtblprint
&& cp_is_vtbl_ptr_type (type
))
366 /* Print the unmangled name if desired. */
367 /* Print vtable entry - we only get here if we ARE using
368 -fvtable_thunks. (Otherwise, look under
369 TYPE_CODE_STRUCT.) */
371 = extract_typed_address (valaddr
+ embedded_offset
, type
);
372 struct gdbarch
*gdbarch
= get_type_arch (type
);
374 print_function_pointer_address (options
, gdbarch
, addr
, stream
);
378 struct type
*unresolved_elttype
= TYPE_TARGET_TYPE (type
);
379 struct type
*elttype
= check_typedef (unresolved_elttype
);
380 CORE_ADDR addr
= unpack_pointer (type
,
381 valaddr
+ embedded_offset
* unit_size
);
383 print_unpacked_pointer (type
, elttype
, unresolved_elttype
, valaddr
,
384 embedded_offset
, addr
, stream
, recurse
, options
);
388 /* c_val_print helper for TYPE_CODE_STRUCT. */
391 c_val_print_struct (struct type
*type
, const gdb_byte
*valaddr
,
392 int embedded_offset
, CORE_ADDR address
,
393 struct ui_file
*stream
, int recurse
,
394 struct value
*original_value
,
395 const struct value_print_options
*options
)
397 if (options
->vtblprint
&& cp_is_vtbl_ptr_type (type
))
399 /* Print the unmangled name if desired. */
400 /* Print vtable entry - we only get here if NOT using
401 -fvtable_thunks. (Otherwise, look under
403 struct gdbarch
*gdbarch
= get_type_arch (type
);
404 int offset
= (embedded_offset
405 + TYPE_FIELD_BITPOS (type
,
406 VTBL_FNADDR_OFFSET
) / 8);
407 struct type
*field_type
= TYPE_FIELD_TYPE (type
, VTBL_FNADDR_OFFSET
);
408 CORE_ADDR addr
= extract_typed_address (valaddr
+ offset
, field_type
);
410 print_function_pointer_address (options
, gdbarch
, addr
, stream
);
413 cp_print_value_fields_rtti (type
, valaddr
,
414 embedded_offset
, address
,
416 original_value
, options
,
420 /* c_val_print helper for TYPE_CODE_UNION. */
423 c_val_print_union (struct type
*type
, const gdb_byte
*valaddr
,
424 int embedded_offset
, CORE_ADDR address
,
425 struct ui_file
*stream
, int recurse
,
426 struct value
*original_value
,
427 const struct value_print_options
*options
)
429 if (recurse
&& !options
->unionprint
)
431 fprintf_filtered (stream
, "{...}");
435 c_val_print_struct (type
, valaddr
, embedded_offset
, address
, stream
,
436 recurse
, original_value
, options
);
440 /* c_val_print helper for TYPE_CODE_INT. */
443 c_val_print_int (struct type
*type
, struct type
*unresolved_type
,
444 const gdb_byte
*valaddr
, int embedded_offset
,
445 struct ui_file
*stream
, struct value
*original_value
,
446 const struct value_print_options
*options
)
448 struct gdbarch
*arch
= get_type_arch (type
);
449 int unit_size
= gdbarch_addressable_memory_unit_size (arch
);
451 if (options
->format
|| options
->output_format
)
453 struct value_print_options opts
= *options
;
455 opts
.format
= (options
->format
? options
->format
456 : options
->output_format
);
457 val_print_scalar_formatted (type
, embedded_offset
,
458 original_value
, &opts
, 0, stream
);
462 val_print_scalar_formatted (type
, embedded_offset
,
463 original_value
, options
, 0, stream
);
464 /* C and C++ has no single byte int type, char is used
465 instead. Since we don't know whether the value is really
466 intended to be used as an integer or a character, print
467 the character equivalent as well. */
468 if (c_textual_element_type (unresolved_type
, options
->format
))
470 fputs_filtered (" ", stream
);
471 LA_PRINT_CHAR (unpack_long (type
,
472 valaddr
+ embedded_offset
* unit_size
),
473 unresolved_type
, stream
);
478 /* c_val_print helper for TYPE_CODE_MEMBERPTR. */
481 c_val_print_memberptr (struct type
*type
, const gdb_byte
*valaddr
,
482 int embedded_offset
, CORE_ADDR address
,
483 struct ui_file
*stream
, int recurse
,
484 struct value
*original_value
,
485 const struct value_print_options
*options
)
487 if (!options
->format
)
489 cp_print_class_member (valaddr
+ embedded_offset
, type
, stream
, "&");
493 generic_val_print (type
, embedded_offset
, address
, stream
,
494 recurse
, original_value
, options
, &c_decorations
);
498 /* See val_print for a description of the various parameters of this
499 function; they are identical. */
502 c_val_print (struct type
*type
,
503 int embedded_offset
, CORE_ADDR address
,
504 struct ui_file
*stream
, int recurse
,
505 struct value
*original_value
,
506 const struct value_print_options
*options
)
508 struct type
*unresolved_type
= type
;
509 const gdb_byte
*valaddr
= value_contents_for_printing (original_value
);
511 type
= check_typedef (type
);
512 switch (TYPE_CODE (type
))
514 case TYPE_CODE_ARRAY
:
515 c_val_print_array (type
, valaddr
, embedded_offset
, address
, stream
,
516 recurse
, original_value
, options
);
519 case TYPE_CODE_METHODPTR
:
520 cplus_print_method_ptr (valaddr
+ embedded_offset
, type
, stream
);
524 c_val_print_ptr (type
, valaddr
, embedded_offset
, stream
, recurse
,
525 original_value
, options
);
528 case TYPE_CODE_UNION
:
529 c_val_print_union (type
, valaddr
, embedded_offset
, address
, stream
,
530 recurse
, original_value
, options
);
533 case TYPE_CODE_STRUCT
:
534 c_val_print_struct (type
, valaddr
, embedded_offset
, address
, stream
,
535 recurse
, original_value
, options
);
539 c_val_print_int (type
, unresolved_type
, valaddr
, embedded_offset
, stream
,
540 original_value
, options
);
543 case TYPE_CODE_MEMBERPTR
:
544 c_val_print_memberptr (type
, valaddr
, embedded_offset
, address
, stream
,
545 recurse
, original_value
, options
);
549 case TYPE_CODE_RVALUE_REF
:
551 case TYPE_CODE_FLAGS
:
553 case TYPE_CODE_METHOD
:
555 case TYPE_CODE_RANGE
:
557 case TYPE_CODE_DECFLOAT
:
559 case TYPE_CODE_ERROR
:
560 case TYPE_CODE_UNDEF
:
561 case TYPE_CODE_COMPLEX
:
564 generic_val_print (type
, embedded_offset
, address
,
565 stream
, recurse
, original_value
, options
,
572 c_value_print (struct value
*val
, struct ui_file
*stream
,
573 const struct value_print_options
*options
)
575 struct type
*type
, *real_type
, *val_type
;
578 struct value_print_options opts
= *options
;
582 /* If it is a pointer, indicate what it points to.
584 Print type also if it is a reference.
586 C++: if it is a member pointer, we will take care
587 of that when we print it. */
589 /* Preserve the original type before stripping typedefs. We prefer
590 to pass down the original type when possible, but for local
591 checks it is better to look past the typedefs. */
592 val_type
= value_type (val
);
593 type
= check_typedef (val_type
);
595 if (TYPE_CODE (type
) == TYPE_CODE_PTR
|| TYPE_IS_REFERENCE (type
))
597 /* Hack: remove (char *) for char strings. Their
598 type is indicated by the quoted string anyway.
599 (Don't use c_textual_element_type here; quoted strings
600 are always exactly (char *), (wchar_t *), or the like. */
601 if (TYPE_CODE (val_type
) == TYPE_CODE_PTR
602 && TYPE_NAME (val_type
) == NULL
603 && TYPE_NAME (TYPE_TARGET_TYPE (val_type
)) != NULL
604 && (strcmp (TYPE_NAME (TYPE_TARGET_TYPE (val_type
)),
606 || textual_name (TYPE_NAME (TYPE_TARGET_TYPE (val_type
)))))
610 else if (options
->objectprint
611 && (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_STRUCT
))
613 int is_ref
= TYPE_IS_REFERENCE (type
);
614 enum type_code refcode
= TYPE_CODE_UNDEF
;
618 val
= value_addr (val
);
619 refcode
= TYPE_CODE (type
);
622 /* Pointer to class, check real type of object. */
623 fprintf_filtered (stream
, "(");
625 if (value_entirely_available (val
))
627 real_type
= value_rtti_indirect_type (val
, &full
, &top
,
631 /* RTTI entry found. */
634 /* Need to adjust pointer value. */
635 val
= value_from_pointer (real_type
,
636 value_as_address (val
) - top
);
638 /* Note: When we look up RTTI entries, we don't get
639 any information on const or volatile
646 val
= value_ref (value_ind (val
), refcode
);
647 type
= value_type (val
);
650 type_print (type
, "", stream
, -1);
651 fprintf_filtered (stream
, ") ");
657 fprintf_filtered (stream
, "(");
658 type_print (value_type (val
), "", stream
, -1);
659 fprintf_filtered (stream
, ") ");
663 if (!value_initialized (val
))
664 fprintf_filtered (stream
, " [uninitialized] ");
666 if (options
->objectprint
&& (TYPE_CODE (type
) == TYPE_CODE_STRUCT
))
668 /* Attempt to determine real type of object. */
669 real_type
= value_rtti_type (val
, &full
, &top
, &using_enc
);
672 /* We have RTTI information, so use it. */
673 val
= value_full_object (val
, real_type
,
674 full
, top
, using_enc
);
675 fprintf_filtered (stream
, "(%s%s) ",
676 TYPE_NAME (real_type
),
677 full
? "" : _(" [incomplete object]"));
678 /* Print out object: enclosing type is same as real_type if
680 val_print (value_enclosing_type (val
),
682 value_address (val
), stream
, 0,
683 val
, &opts
, current_language
);
685 /* Note: When we look up RTTI entries, we don't get any
686 information on const or volatile attributes. */
688 else if (type
!= check_typedef (value_enclosing_type (val
)))
690 /* No RTTI information, so let's do our best. */
691 fprintf_filtered (stream
, "(%s ?) ",
692 TYPE_NAME (value_enclosing_type (val
)));
693 val_print (value_enclosing_type (val
),
695 value_address (val
), stream
, 0,
696 val
, &opts
, current_language
);
699 /* Otherwise, we end up at the return outside this "if". */
703 value_embedded_offset (val
),
706 val
, &opts
, current_language
);