1 /* Support for printing C++ values for GDB, the GNU debugger.
3 Copyright (C) 1986-2021 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 "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 fprintf_filtered (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
);
194 /* If requested, skip printing of static fields. */
195 if (!options
->static_field_print
196 && field_is_static (&type
->field (i
)))
201 fputs_filtered (",", stream
);
202 if (!options
->prettyformat
)
203 fputs_filtered (" ", stream
);
205 else if (n_baseclasses
> 0)
207 if (options
->prettyformat
)
209 fprintf_filtered (stream
, "\n");
210 print_spaces_filtered (2 + 2 * recurse
, stream
);
211 fputs_filtered ("members of ", stream
);
212 fputs_filtered (type
->name (), stream
);
213 fputs_filtered (":", stream
);
218 if (options
->prettyformat
)
220 fprintf_filtered (stream
, "\n");
221 print_spaces_filtered (2 + 2 * recurse
, stream
);
225 wrap_here (n_spaces (2 + 2 * recurse
));
228 annotate_field_begin (type
->field (i
).type ());
230 if (field_is_static (&type
->field (i
)))
232 fputs_filtered ("static ", stream
);
233 fprintf_symbol_filtered (stream
,
234 TYPE_FIELD_NAME (type
, i
),
235 current_language
->la_language
,
236 DMGL_PARAMS
| DMGL_ANSI
);
239 fputs_styled (TYPE_FIELD_NAME (type
, i
),
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_NAME (type
, i
), ""))
250 fputs_filtered (" = ", 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 (val
,
274 TYPE_FIELD_BITPOS (type
,
276 TYPE_FIELD_BITSIZE (type
,
279 fputs_styled (_("<synthetic pointer>"),
280 metadata_style
.style (), stream
);
286 v
= value_field_bitfield (type
, i
, valaddr
,
287 value_embedded_offset (val
), val
);
289 common_val_print (v
, stream
, recurse
+ 1,
290 opts
, current_language
);
295 if (TYPE_FIELD_IGNORE (type
, i
))
297 fputs_styled ("<optimized out or zero length>",
298 metadata_style
.style (), stream
);
300 else if (field_is_static (&type
->field (i
)))
304 struct value
*v
= value_static_field (type
, i
);
306 cp_print_static_field (type
->field (i
).type (),
307 v
, stream
, recurse
+ 1,
310 catch (const gdb_exception_error
&ex
)
312 fprintf_styled (stream
, metadata_style
.style (),
313 _("<error reading variable: %s>"),
317 else if (i
== vptr_fieldno
&& type
== vptr_basetype
)
319 int i_offset
= TYPE_FIELD_BITPOS (type
, i
) / 8;
320 struct type
*i_type
= type
->field (i
).type ();
322 if (valprint_check_validity (stream
, i_type
, i_offset
, val
))
326 i_offset
+= value_embedded_offset (val
);
327 addr
= extract_typed_address (valaddr
+ i_offset
, i_type
);
328 print_function_pointer_address (opts
,
335 struct value
*v
= value_primitive_field (val
, 0, i
, type
);
337 common_val_print (v
, stream
, recurse
+ 1, opts
,
341 annotate_field_end ();
344 if (dont_print_statmem
== 0)
346 size_t obstack_final_size
=
347 obstack_object_size (&dont_print_statmem_obstack
);
349 if (obstack_final_size
> statmem_obstack_initial_size
)
351 /* In effect, a pop of the printed-statics stack. */
353 = statmem_obstack_initial_size
- obstack_final_size
;
354 obstack_blank_fast (&dont_print_statmem_obstack
, shrink_bytes
);
357 if (last_set_recurse
!= recurse
)
360 obstack_object_size (&dont_print_stat_array_obstack
);
362 if (obstack_final_size
> stat_array_obstack_initial_size
)
365 (char *) obstack_next_free (&dont_print_stat_array_obstack
)
366 - (obstack_final_size
367 - stat_array_obstack_initial_size
);
369 obstack_free (&dont_print_stat_array_obstack
,
372 last_set_recurse
= -1;
376 if (options
->prettyformat
)
378 fprintf_filtered (stream
, "\n");
379 print_spaces_filtered (2 * recurse
, stream
);
381 } /* if there are data fields */
383 fprintf_filtered (stream
, "}");
386 /* Special val_print routine to avoid printing multiple copies of
387 virtual baseclasses. */
390 cp_print_value (struct value
*val
, struct ui_file
*stream
,
391 int recurse
, const struct value_print_options
*options
,
392 struct type
**dont_print_vb
)
394 struct type
*type
= check_typedef (value_type (val
));
395 CORE_ADDR address
= value_address (val
);
396 struct type
**last_dont_print
397 = (struct type
**) obstack_next_free (&dont_print_vb_obstack
);
398 struct obstack tmp_obstack
= dont_print_vb_obstack
;
399 int i
, n_baseclasses
= TYPE_N_BASECLASSES (type
);
400 const gdb_byte
*valaddr
= value_contents_for_printing (val
);
402 if (dont_print_vb
== 0)
404 /* If we're at top level, carve out a completely fresh chunk of
405 the obstack and use that until this particular invocation
407 /* Bump up the high-water mark. Now alpha is omega. */
408 obstack_finish (&dont_print_vb_obstack
);
411 for (i
= 0; i
< n_baseclasses
; i
++)
415 struct type
*baseclass
= check_typedef (TYPE_BASECLASS (type
, i
));
416 const char *basename
= baseclass
->name ();
417 struct value
*base_val
= NULL
;
419 if (BASETYPE_VIA_VIRTUAL (type
, i
))
421 struct type
**first_dont_print
422 = (struct type
**) obstack_base (&dont_print_vb_obstack
);
424 int j
= (struct type
**)
425 obstack_next_free (&dont_print_vb_obstack
) - first_dont_print
;
428 if (baseclass
== first_dont_print
[j
])
431 obstack_ptr_grow (&dont_print_vb_obstack
, baseclass
);
436 boffset
= baseclass_offset (type
, i
, valaddr
,
437 value_embedded_offset (val
),
440 catch (const gdb_exception_error
&ex
)
442 if (ex
.error
== NOT_AVAILABLE_ERROR
)
450 if (BASETYPE_VIA_VIRTUAL (type
, i
))
452 /* The virtual base class pointer might have been
453 clobbered by the user program. Make sure that it
454 still points to a valid memory location. */
456 if (boffset
< 0 || boffset
>= TYPE_LENGTH (type
))
458 gdb::byte_vector
buf (TYPE_LENGTH (baseclass
));
460 if (target_read_memory (address
+ boffset
, buf
.data (),
461 TYPE_LENGTH (baseclass
)) != 0)
463 base_val
= value_from_contents_and_address (baseclass
,
466 baseclass
= value_type (base_val
);
480 /* Now do the printing. */
481 if (options
->prettyformat
)
483 fprintf_filtered (stream
, "\n");
484 print_spaces_filtered (2 * recurse
, stream
);
486 fputs_filtered ("<", stream
);
487 /* Not sure what the best notation is in the case where there is
488 no baseclass name. */
489 fputs_filtered (basename
? basename
: "", stream
);
490 fputs_filtered ("> = ", stream
);
493 val_print_unavailable (stream
);
495 val_print_invalid_address (stream
);
500 if (!val_print_check_max_depth (stream
, recurse
, options
,
503 struct value
*baseclass_val
= value_primitive_field (val
, 0,
506 /* Attempt to run an extension language pretty-printer on the
507 baseclass if possible. */
510 = apply_ext_lang_val_pretty_printer (baseclass_val
, stream
,
515 cp_print_value_fields (baseclass_val
, stream
, recurse
, options
,
517 obstack_base (&dont_print_vb_obstack
)),
521 fputs_filtered (", ", stream
);
527 if (dont_print_vb
== 0)
529 /* Free the space used to deal with the printing
530 of this type from top level. */
531 obstack_free (&dont_print_vb_obstack
, last_dont_print
);
532 /* Reset watermark so that we can continue protecting
533 ourselves from whatever we were protecting ourselves. */
534 dont_print_vb_obstack
= tmp_obstack
;
538 /* Print value of a static member. To avoid infinite recursion when
539 printing a class that contains a static instance of the class, we
540 keep the addresses of all printed static member classes in an
541 obstack and refuse to print them more than once.
543 VAL contains the value to print, TYPE, STREAM, RECURSE, and OPTIONS
544 have the same meanings as in c_val_print. */
547 cp_print_static_field (struct type
*type
,
549 struct ui_file
*stream
,
551 const struct value_print_options
*options
)
553 struct value_print_options opts
;
555 if (value_entirely_optimized_out (val
))
557 val_print_optimized_out (val
, stream
);
561 struct type
*real_type
= check_typedef (type
);
562 if (real_type
->code () == TYPE_CODE_STRUCT
)
564 CORE_ADDR
*first_dont_print
;
565 CORE_ADDR addr
= value_address (val
);
569 = (CORE_ADDR
*) obstack_base (&dont_print_statmem_obstack
);
570 i
= obstack_object_size (&dont_print_statmem_obstack
)
571 / sizeof (CORE_ADDR
);
575 if (addr
== first_dont_print
[i
])
577 fputs_styled (_("<same as static member of an already"
579 metadata_style
.style (), stream
);
584 obstack_grow (&dont_print_statmem_obstack
, (char *) &addr
,
586 cp_print_value_fields (val
, stream
, recurse
, options
, NULL
, 1);
590 if (real_type
->code () == TYPE_CODE_ARRAY
)
592 struct type
**first_dont_print
;
594 struct type
*target_type
= TYPE_TARGET_TYPE (type
);
597 = (struct type
**) obstack_base (&dont_print_stat_array_obstack
);
598 i
= obstack_object_size (&dont_print_stat_array_obstack
)
599 / sizeof (struct type
*);
603 if (target_type
== first_dont_print
[i
])
605 fputs_styled (_("<same as static member of an already"
607 metadata_style
.style (), stream
);
612 obstack_grow (&dont_print_stat_array_obstack
,
613 (char *) &target_type
,
614 sizeof (struct type
*));
619 common_val_print (val
, stream
, recurse
, &opts
, current_language
);
622 /* Find the field in *SELF, or its non-virtual base classes, with
623 bit offset OFFSET. Set *SELF to the containing type and *FIELDNO
624 to the containing field number. If OFFSET is not exactly at the
625 start of some field, set *SELF to NULL. */
628 cp_find_class_member (struct type
**self_p
, int *fieldno
,
635 *self_p
= check_typedef (*self_p
);
637 len
= self
->num_fields ();
639 for (i
= TYPE_N_BASECLASSES (self
); i
< len
; i
++)
641 LONGEST bitpos
= TYPE_FIELD_BITPOS (self
, i
);
644 if (offset
== bitpos
)
651 for (i
= 0; i
< TYPE_N_BASECLASSES (self
); i
++)
653 LONGEST bitpos
= TYPE_FIELD_BITPOS (self
, i
);
654 LONGEST bitsize
= 8 * TYPE_LENGTH (self
->field (i
).type ());
656 if (offset
>= bitpos
&& offset
< bitpos
+ bitsize
)
658 *self_p
= self
->field (i
).type ();
659 cp_find_class_member (self_p
, fieldno
, offset
- bitpos
);
668 cp_print_class_member (const gdb_byte
*valaddr
, struct type
*type
,
669 struct ui_file
*stream
, const char *prefix
)
671 enum bfd_endian byte_order
= type_byte_order (type
);
673 /* VAL is a byte offset into the structure type SELF_TYPE.
674 Find the name of the field for that offset and
676 struct type
*self_type
= TYPE_SELF_TYPE (type
);
680 val
= extract_signed_integer (valaddr
,
684 /* Pointers to data members are usually byte offsets into an object.
685 Because a data member can have offset zero, and a NULL pointer to
686 member must be distinct from any valid non-NULL pointer to
687 member, either the value is biased or the NULL value has a
688 special representation; both are permitted by ISO C++. HP aCC
689 used a bias of 0x20000000; HP cfront used a bias of 1; g++ 3.x
690 and other compilers which use the Itanium ABI use -1 as the NULL
691 value. GDB only supports that last form; to add support for
692 another form, make this into a cp-abi hook. */
696 fprintf_filtered (stream
, "NULL");
700 cp_find_class_member (&self_type
, &fieldno
, val
<< 3);
702 if (self_type
!= NULL
)
706 fputs_filtered (prefix
, stream
);
707 name
= self_type
->name ();
709 fputs_filtered (name
, stream
);
711 c_type_print_base (self_type
, stream
, 0, 0, &type_print_raw_options
);
712 fprintf_filtered (stream
, "::");
713 fputs_styled (TYPE_FIELD_NAME (self_type
, fieldno
),
714 variable_name_style
.style (), stream
);
717 fprintf_filtered (stream
, "%ld", (long) val
);
722 /* Test printing of TYPE_CODE_STRUCT values. */
725 test_print_fields (gdbarch
*arch
)
728 type
*uint8_type
= builtin_type (arch
)->builtin_uint8
;
729 type
*bool_type
= builtin_type (arch
)->builtin_bool
;
730 type
*the_struct
= arch_composite_type (arch
, NULL
, TYPE_CODE_STRUCT
);
731 TYPE_LENGTH (the_struct
) = 4;
735 if (gdbarch_byte_order (arch
) == BFD_ENDIAN_LITTLE
)
737 f
= append_composite_type_field_raw (the_struct
, "A", bool_type
);
738 SET_FIELD_BITPOS (*f
, 1);
739 FIELD_BITSIZE (*f
) = 1;
740 f
= append_composite_type_field_raw (the_struct
, "B", uint8_type
);
741 SET_FIELD_BITPOS (*f
, 3);
742 FIELD_BITSIZE (*f
) = 3;
743 f
= append_composite_type_field_raw (the_struct
, "C", bool_type
);
744 SET_FIELD_BITPOS (*f
, 7);
745 FIELD_BITSIZE (*f
) = 1;
747 /* According to the logic commented in "make_gdb_type_struct ()" of
748 * target-descriptions.c, bit positions are numbered differently for
749 * little and big endians. */
752 f
= append_composite_type_field_raw (the_struct
, "A", bool_type
);
753 SET_FIELD_BITPOS (*f
, 30);
754 FIELD_BITSIZE (*f
) = 1;
755 f
= append_composite_type_field_raw (the_struct
, "B", uint8_type
);
756 SET_FIELD_BITPOS (*f
, 26);
757 FIELD_BITSIZE (*f
) = 3;
758 f
= append_composite_type_field_raw (the_struct
, "C", bool_type
);
759 SET_FIELD_BITPOS (*f
, 24);
760 FIELD_BITSIZE (*f
) = 1;
763 value
*val
= allocate_value (the_struct
);
764 gdb_byte
*contents
= value_contents_writeable (val
);
765 store_unsigned_integer (contents
, TYPE_LENGTH (value_enclosing_type (val
)),
766 gdbarch_byte_order (arch
), 0xe9);
769 struct value_print_options opts
;
770 get_no_prettyformat_print_options (&opts
);
771 cp_print_value_fields(val
, &out
, 0, &opts
, NULL
, 0);
772 SELF_CHECK (out
.string () == "{A = false, B = 5, C = true}");
776 cp_print_value_fields(val
, &out
, 0, &opts
, NULL
, 0);
777 SELF_CHECK (out
.string () == "{A = 0x0, B = 0x5, C = 0x1}");
783 void _initialize_cp_valprint ();
785 _initialize_cp_valprint ()
788 selftests::register_test_foreach_arch ("print-fields", test_print_fields
);
791 obstack_begin (&dont_print_stat_array_obstack
,
792 32 * sizeof (struct type
*));
793 obstack_begin (&dont_print_statmem_obstack
,
794 32 * sizeof (CORE_ADDR
));
795 obstack_begin (&dont_print_vb_obstack
,
796 32 * sizeof (struct type
*));