1 /* Support for printing C++ values for GDB, the GNU debugger.
3 Copyright (C) 1986-2022 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/>. */
21 #include "gdbsupport/gdb_obstack.h"
24 #include "expression.h"
34 #include "cp-support.h"
36 #include "extension.h"
37 #include "typeprint.h"
38 #include "gdbsupport/byte-vector.h"
40 #include "cli/cli-style.h"
41 #include "gdbsupport/selftest.h"
42 #include "selftest-arch.h"
44 static struct obstack dont_print_vb_obstack
;
45 static struct obstack dont_print_statmem_obstack
;
46 static struct obstack dont_print_stat_array_obstack
;
48 static void cp_print_static_field (struct type
*, struct value
*,
49 struct ui_file
*, int,
50 const struct value_print_options
*);
52 static void cp_print_value (struct value
*, struct ui_file
*,
53 int, const struct value_print_options
*,
57 /* GCC versions after 2.4.5 use this. */
58 const char vtbl_ptr_name
[] = "__vtbl_ptr_type";
60 /* Return truth value for assertion that TYPE is of the type
61 "pointer to virtual function". */
64 cp_is_vtbl_ptr_type (struct type
*type
)
66 const char *type_name
= type
->name ();
68 return (type_name
!= NULL
&& !strcmp (type_name
, vtbl_ptr_name
));
71 /* Return truth value for the assertion that TYPE is of the type
72 "pointer to virtual function table". */
75 cp_is_vtbl_member (struct type
*type
)
77 /* With older versions of g++, the vtbl field pointed to an array of
78 structures. Nowadays it points directly to the structure. */
79 if (type
->code () == TYPE_CODE_PTR
)
81 type
= TYPE_TARGET_TYPE (type
);
82 if (type
->code () == TYPE_CODE_ARRAY
)
84 type
= TYPE_TARGET_TYPE (type
);
85 if (type
->code () == TYPE_CODE_STRUCT
/* if not using thunks */
86 || type
->code () == TYPE_CODE_PTR
) /* if using thunks */
88 /* Virtual functions tables are full of pointers
89 to virtual functions. */
90 return cp_is_vtbl_ptr_type (type
);
93 else if (type
->code () == TYPE_CODE_STRUCT
) /* if not using thunks */
95 return cp_is_vtbl_ptr_type (type
);
97 else if (type
->code () == TYPE_CODE_PTR
) /* if using thunks */
99 /* The type name of the thunk pointer is NULL when using
100 dwarf2. We could test for a pointer to a function, but
101 there is no type info for the virtual table either, so it
103 return cp_is_vtbl_ptr_type (type
);
109 /* Mutually recursive subroutines of cp_print_value and c_val_print to
110 print out a structure's fields: cp_print_value_fields and
113 TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and OPTIONS have the same
114 meanings as in cp_print_value and c_val_print.
116 2nd argument REAL_TYPE is used to carry over the type of the
117 derived class across the recursion to base classes.
119 DONT_PRINT is an array of baseclass types that we should not print,
120 or zero if called from top level. */
123 cp_print_value_fields (struct value
*val
, struct ui_file
*stream
,
124 int recurse
, const struct value_print_options
*options
,
125 struct type
**dont_print_vb
,
126 int dont_print_statmem
)
128 int i
, len
, n_baseclasses
;
130 static int last_set_recurse
= -1;
132 struct type
*type
= check_typedef (value_type (val
));
136 /* Any object can be left on obstacks only during an unexpected
139 if (obstack_object_size (&dont_print_statmem_obstack
) > 0)
141 obstack_free (&dont_print_statmem_obstack
, NULL
);
142 obstack_begin (&dont_print_statmem_obstack
,
143 32 * sizeof (CORE_ADDR
));
145 if (obstack_object_size (&dont_print_stat_array_obstack
) > 0)
147 obstack_free (&dont_print_stat_array_obstack
, NULL
);
148 obstack_begin (&dont_print_stat_array_obstack
,
149 32 * sizeof (struct type
*));
153 gdb_printf (stream
, "{");
154 len
= type
->num_fields ();
155 n_baseclasses
= TYPE_N_BASECLASSES (type
);
157 /* First, print out baseclasses such that we don't print
158 duplicates of virtual baseclasses. */
160 if (n_baseclasses
> 0)
161 cp_print_value (val
, stream
, recurse
+ 1, options
, dont_print_vb
);
163 /* Second, print out data fields */
165 /* If there are no data fields, skip this part */
166 if (len
== n_baseclasses
|| !len
)
167 fprintf_styled (stream
, metadata_style
.style (), "<No data fields>");
170 size_t statmem_obstack_initial_size
= 0;
171 size_t stat_array_obstack_initial_size
= 0;
172 struct type
*vptr_basetype
= NULL
;
175 if (dont_print_statmem
== 0)
177 statmem_obstack_initial_size
=
178 obstack_object_size (&dont_print_statmem_obstack
);
180 if (last_set_recurse
!= recurse
)
182 stat_array_obstack_initial_size
=
183 obstack_object_size (&dont_print_stat_array_obstack
);
185 last_set_recurse
= recurse
;
189 vptr_fieldno
= get_vptr_fieldno (type
, &vptr_basetype
);
190 for (i
= n_baseclasses
; i
< len
; i
++)
192 const gdb_byte
*valaddr
= value_contents_for_printing (val
).data ();
194 /* If requested, skip printing of static fields. */
195 if (!options
->static_field_print
196 && field_is_static (&type
->field (i
)))
201 gdb_puts (",", stream
);
202 if (!options
->prettyformat
)
203 gdb_puts (" ", stream
);
205 else if (n_baseclasses
> 0)
207 if (options
->prettyformat
)
209 gdb_printf (stream
, "\n");
210 print_spaces (2 + 2 * recurse
, stream
);
211 gdb_puts ("members of ", stream
);
212 gdb_puts (type
->name (), stream
);
213 gdb_puts (":", stream
);
218 if (options
->prettyformat
)
220 gdb_printf (stream
, "\n");
221 print_spaces (2 + 2 * recurse
, stream
);
225 stream
->wrap_here (2 + 2 * recurse
);
228 annotate_field_begin (type
->field (i
).type ());
230 if (field_is_static (&type
->field (i
)))
232 gdb_puts ("static ", stream
);
233 fprintf_symbol (stream
,
234 type
->field (i
).name (),
235 current_language
->la_language
,
236 DMGL_PARAMS
| DMGL_ANSI
);
239 fputs_styled (type
->field (i
).name (),
240 variable_name_style
.style (), stream
);
241 annotate_field_name_end ();
243 /* We tweak various options in a few cases below. */
244 value_print_options options_copy
= *options
;
245 value_print_options
*opts
= &options_copy
;
247 /* Do not print leading '=' in case of anonymous
249 if (strcmp (type
->field (i
).name (), ""))
250 gdb_puts (" = ", stream
);
253 /* If this is an anonymous field then we want to consider it
254 as though it is at its parent's depth when it comes to the
256 if (opts
->max_depth
!= -1 && opts
->max_depth
< INT_MAX
)
259 annotate_field_value ();
261 if (!field_is_static (&type
->field (i
))
262 && TYPE_FIELD_PACKED (type
, i
))
266 /* Bitfields require special handling, especially due to
267 byte order problems. */
268 if (TYPE_FIELD_IGNORE (type
, i
))
270 fputs_styled ("<optimized out or zero length>",
271 metadata_style
.style (), stream
);
273 else if (value_bits_synthetic_pointer
274 (val
, type
->field (i
).loc_bitpos (),
275 TYPE_FIELD_BITSIZE (type
, i
)))
277 fputs_styled (_("<synthetic pointer>"),
278 metadata_style
.style (), stream
);
284 v
= value_field_bitfield (type
, i
, valaddr
,
285 value_embedded_offset (val
), val
);
287 common_val_print (v
, stream
, recurse
+ 1,
288 opts
, current_language
);
293 if (TYPE_FIELD_IGNORE (type
, i
))
295 fputs_styled ("<optimized out or zero length>",
296 metadata_style
.style (), stream
);
298 else if (field_is_static (&type
->field (i
)))
302 struct value
*v
= value_static_field (type
, i
);
304 cp_print_static_field (type
->field (i
).type (),
305 v
, stream
, recurse
+ 1,
308 catch (const gdb_exception_error
&ex
)
310 fprintf_styled (stream
, metadata_style
.style (),
311 _("<error reading variable: %s>"),
315 else if (i
== vptr_fieldno
&& type
== vptr_basetype
)
317 int i_offset
= type
->field (i
).loc_bitpos () / 8;
318 struct type
*i_type
= type
->field (i
).type ();
320 if (valprint_check_validity (stream
, i_type
, i_offset
, val
))
324 i_offset
+= value_embedded_offset (val
);
325 addr
= extract_typed_address (valaddr
+ i_offset
, i_type
);
326 print_function_pointer_address (opts
,
333 struct value
*v
= value_primitive_field (val
, 0, i
, type
);
335 common_val_print (v
, stream
, recurse
+ 1, opts
,
339 annotate_field_end ();
342 if (dont_print_statmem
== 0)
344 size_t obstack_final_size
=
345 obstack_object_size (&dont_print_statmem_obstack
);
347 if (obstack_final_size
> statmem_obstack_initial_size
)
349 /* In effect, a pop of the printed-statics stack. */
351 = statmem_obstack_initial_size
- obstack_final_size
;
352 obstack_blank_fast (&dont_print_statmem_obstack
, shrink_bytes
);
355 if (last_set_recurse
!= recurse
)
358 obstack_object_size (&dont_print_stat_array_obstack
);
360 if (obstack_final_size
> stat_array_obstack_initial_size
)
363 (char *) obstack_next_free (&dont_print_stat_array_obstack
)
364 - (obstack_final_size
365 - stat_array_obstack_initial_size
);
367 obstack_free (&dont_print_stat_array_obstack
,
370 last_set_recurse
= -1;
374 if (options
->prettyformat
)
376 gdb_printf (stream
, "\n");
377 print_spaces (2 * recurse
, stream
);
379 } /* if there are data fields */
381 gdb_printf (stream
, "}");
384 /* Special val_print routine to avoid printing multiple copies of
385 virtual baseclasses. */
388 cp_print_value (struct value
*val
, struct ui_file
*stream
,
389 int recurse
, const struct value_print_options
*options
,
390 struct type
**dont_print_vb
)
392 struct type
*type
= check_typedef (value_type (val
));
393 CORE_ADDR address
= value_address (val
);
394 struct type
**last_dont_print
395 = (struct type
**) obstack_next_free (&dont_print_vb_obstack
);
396 struct obstack tmp_obstack
= dont_print_vb_obstack
;
397 int i
, n_baseclasses
= TYPE_N_BASECLASSES (type
);
398 const gdb_byte
*valaddr
= value_contents_for_printing (val
).data ();
400 if (dont_print_vb
== 0)
402 /* If we're at top level, carve out a completely fresh chunk of
403 the obstack and use that until this particular invocation
405 /* Bump up the high-water mark. Now alpha is omega. */
406 obstack_finish (&dont_print_vb_obstack
);
409 for (i
= 0; i
< n_baseclasses
; i
++)
413 struct type
*baseclass
= check_typedef (TYPE_BASECLASS (type
, i
));
414 const char *basename
= baseclass
->name ();
415 struct value
*base_val
= NULL
;
417 if (BASETYPE_VIA_VIRTUAL (type
, i
))
419 struct type
**first_dont_print
420 = (struct type
**) obstack_base (&dont_print_vb_obstack
);
422 int j
= (struct type
**)
423 obstack_next_free (&dont_print_vb_obstack
) - first_dont_print
;
426 if (baseclass
== first_dont_print
[j
])
429 obstack_ptr_grow (&dont_print_vb_obstack
, baseclass
);
434 boffset
= baseclass_offset (type
, i
, valaddr
,
435 value_embedded_offset (val
),
438 catch (const gdb_exception_error
&ex
)
440 if (ex
.error
== NOT_AVAILABLE_ERROR
)
448 if (BASETYPE_VIA_VIRTUAL (type
, i
))
450 /* The virtual base class pointer might have been
451 clobbered by the user program. Make sure that it
452 still points to a valid memory location. */
454 if (boffset
< 0 || boffset
>= TYPE_LENGTH (type
))
456 gdb::byte_vector
buf (TYPE_LENGTH (baseclass
));
458 if (target_read_memory (address
+ boffset
, buf
.data (),
459 TYPE_LENGTH (baseclass
)) != 0)
461 base_val
= value_from_contents_and_address (baseclass
,
464 baseclass
= value_type (base_val
);
478 /* Now do the printing. */
479 if (options
->prettyformat
)
481 gdb_printf (stream
, "\n");
482 print_spaces (2 * recurse
, stream
);
484 gdb_puts ("<", stream
);
485 /* Not sure what the best notation is in the case where there is
486 no baseclass name. */
487 gdb_puts (basename
? basename
: "", stream
);
488 gdb_puts ("> = ", stream
);
491 val_print_unavailable (stream
);
493 val_print_invalid_address (stream
);
498 if (!val_print_check_max_depth (stream
, recurse
, options
,
501 struct value
*baseclass_val
= value_primitive_field (val
, 0,
504 /* Attempt to run an extension language pretty-printer on the
505 baseclass if possible. */
508 = apply_ext_lang_val_pretty_printer (baseclass_val
, stream
,
513 cp_print_value_fields (baseclass_val
, stream
, recurse
, options
,
515 obstack_base (&dont_print_vb_obstack
)),
519 gdb_puts (", ", stream
);
525 if (dont_print_vb
== 0)
527 /* Free the space used to deal with the printing
528 of this type from top level. */
529 obstack_free (&dont_print_vb_obstack
, last_dont_print
);
530 /* Reset watermark so that we can continue protecting
531 ourselves from whatever we were protecting ourselves. */
532 dont_print_vb_obstack
= tmp_obstack
;
536 /* Print value of a static member. To avoid infinite recursion when
537 printing a class that contains a static instance of the class, we
538 keep the addresses of all printed static member classes in an
539 obstack and refuse to print them more than once.
541 VAL contains the value to print, TYPE, STREAM, RECURSE, and OPTIONS
542 have the same meanings as in c_val_print. */
545 cp_print_static_field (struct type
*type
,
547 struct ui_file
*stream
,
549 const struct value_print_options
*options
)
551 struct value_print_options opts
;
553 if (value_entirely_optimized_out (val
))
555 val_print_optimized_out (val
, stream
);
559 struct type
*real_type
= check_typedef (type
);
560 if (real_type
->code () == TYPE_CODE_STRUCT
)
562 CORE_ADDR
*first_dont_print
;
563 CORE_ADDR addr
= value_address (val
);
567 = (CORE_ADDR
*) obstack_base (&dont_print_statmem_obstack
);
568 i
= obstack_object_size (&dont_print_statmem_obstack
)
569 / sizeof (CORE_ADDR
);
573 if (addr
== first_dont_print
[i
])
575 fputs_styled (_("<same as static member of an already"
577 metadata_style
.style (), stream
);
582 obstack_grow (&dont_print_statmem_obstack
, (char *) &addr
,
584 cp_print_value_fields (val
, stream
, recurse
, options
, NULL
, 1);
588 if (real_type
->code () == TYPE_CODE_ARRAY
)
590 struct type
**first_dont_print
;
592 struct type
*target_type
= TYPE_TARGET_TYPE (type
);
595 = (struct type
**) obstack_base (&dont_print_stat_array_obstack
);
596 i
= obstack_object_size (&dont_print_stat_array_obstack
)
597 / sizeof (struct type
*);
601 if (target_type
== first_dont_print
[i
])
603 fputs_styled (_("<same as static member of an already"
605 metadata_style
.style (), stream
);
610 obstack_grow (&dont_print_stat_array_obstack
,
611 (char *) &target_type
,
612 sizeof (struct type
*));
617 common_val_print (val
, stream
, recurse
, &opts
, current_language
);
620 /* Find the field in *SELF, or its non-virtual base classes, with
621 bit offset OFFSET. Set *SELF to the containing type and *FIELDNO
622 to the containing field number. If OFFSET is not exactly at the
623 start of some field, set *SELF to NULL. */
626 cp_find_class_member (struct type
**self_p
, int *fieldno
,
633 *self_p
= check_typedef (*self_p
);
635 len
= self
->num_fields ();
637 for (i
= TYPE_N_BASECLASSES (self
); i
< len
; i
++)
639 LONGEST bitpos
= self
->field (i
).loc_bitpos ();
642 if (offset
== bitpos
)
649 for (i
= 0; i
< TYPE_N_BASECLASSES (self
); i
++)
651 LONGEST bitpos
= self
->field (i
).loc_bitpos ();
652 LONGEST bitsize
= 8 * TYPE_LENGTH (self
->field (i
).type ());
654 if (offset
>= bitpos
&& offset
< bitpos
+ bitsize
)
656 *self_p
= self
->field (i
).type ();
657 cp_find_class_member (self_p
, fieldno
, offset
- bitpos
);
666 cp_print_class_member (const gdb_byte
*valaddr
, struct type
*type
,
667 struct ui_file
*stream
, const char *prefix
)
669 enum bfd_endian byte_order
= type_byte_order (type
);
671 /* VAL is a byte offset into the structure type SELF_TYPE.
672 Find the name of the field for that offset and
674 struct type
*self_type
= TYPE_SELF_TYPE (type
);
678 val
= extract_signed_integer (valaddr
,
682 /* Pointers to data members are usually byte offsets into an object.
683 Because a data member can have offset zero, and a NULL pointer to
684 member must be distinct from any valid non-NULL pointer to
685 member, either the value is biased or the NULL value has a
686 special representation; both are permitted by ISO C++. HP aCC
687 used a bias of 0x20000000; HP cfront used a bias of 1; g++ 3.x
688 and other compilers which use the Itanium ABI use -1 as the NULL
689 value. GDB only supports that last form; to add support for
690 another form, make this into a cp-abi hook. */
694 gdb_printf (stream
, "NULL");
698 cp_find_class_member (&self_type
, &fieldno
, val
<< 3);
700 if (self_type
!= NULL
)
704 gdb_puts (prefix
, stream
);
705 name
= self_type
->name ();
707 gdb_puts (name
, stream
);
709 c_type_print_base (self_type
, stream
, 0, 0, &type_print_raw_options
);
710 gdb_printf (stream
, "::");
711 fputs_styled (self_type
->field (fieldno
).name (),
712 variable_name_style
.style (), stream
);
715 gdb_printf (stream
, "%ld", (long) val
);
720 /* Test printing of TYPE_CODE_STRUCT values. */
723 test_print_fields (gdbarch
*arch
)
726 type
*uint8_type
= builtin_type (arch
)->builtin_uint8
;
727 type
*bool_type
= builtin_type (arch
)->builtin_bool
;
728 type
*the_struct
= arch_composite_type (arch
, NULL
, TYPE_CODE_STRUCT
);
729 TYPE_LENGTH (the_struct
) = 4;
733 if (gdbarch_byte_order (arch
) == BFD_ENDIAN_LITTLE
)
735 f
= append_composite_type_field_raw (the_struct
, "A", bool_type
);
736 f
->set_loc_bitpos (1);
737 FIELD_BITSIZE (*f
) = 1;
738 f
= append_composite_type_field_raw (the_struct
, "B", uint8_type
);
739 f
->set_loc_bitpos (3);
740 FIELD_BITSIZE (*f
) = 3;
741 f
= append_composite_type_field_raw (the_struct
, "C", bool_type
);
742 f
->set_loc_bitpos (7);
743 FIELD_BITSIZE (*f
) = 1;
745 /* According to the logic commented in "make_gdb_type_struct ()" of
746 * target-descriptions.c, bit positions are numbered differently for
747 * little and big endians. */
750 f
= append_composite_type_field_raw (the_struct
, "A", bool_type
);
751 f
->set_loc_bitpos (30);
752 FIELD_BITSIZE (*f
) = 1;
753 f
= append_composite_type_field_raw (the_struct
, "B", uint8_type
);
754 f
->set_loc_bitpos (26);
755 FIELD_BITSIZE (*f
) = 3;
756 f
= append_composite_type_field_raw (the_struct
, "C", bool_type
);
757 f
->set_loc_bitpos (24);
758 FIELD_BITSIZE (*f
) = 1;
761 value
*val
= allocate_value (the_struct
);
762 gdb_byte
*contents
= value_contents_writeable (val
).data ();
763 store_unsigned_integer (contents
, TYPE_LENGTH (value_enclosing_type (val
)),
764 gdbarch_byte_order (arch
), 0xe9);
767 struct value_print_options opts
;
768 get_no_prettyformat_print_options (&opts
);
769 cp_print_value_fields(val
, &out
, 0, &opts
, NULL
, 0);
770 SELF_CHECK (out
.string () == "{A = false, B = 5, C = true}");
774 cp_print_value_fields(val
, &out
, 0, &opts
, NULL
, 0);
775 SELF_CHECK (out
.string () == "{A = 0x0, B = 0x5, C = 0x1}");
781 void _initialize_cp_valprint ();
783 _initialize_cp_valprint ()
786 selftests::register_test_foreach_arch ("print-fields", test_print_fields
);
789 obstack_begin (&dont_print_stat_array_obstack
,
790 32 * sizeof (struct type
*));
791 obstack_begin (&dont_print_statmem_obstack
,
792 32 * sizeof (CORE_ADDR
));
793 obstack_begin (&dont_print_vb_obstack
,
794 32 * sizeof (struct type
*));