1 /* Support for printing C++ values for GDB, the GNU debugger.
3 Copyright (C) 1986-2024 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/>. */
20 #include "event-top.h"
21 #include "extract-store-integer.h"
22 #include "gdbsupport/gdb_obstack.h"
25 #include "expression.h"
28 #include "cli/cli-cmds.h"
35 #include "cp-support.h"
37 #include "extension.h"
38 #include "typeprint.h"
39 #include "gdbsupport/byte-vector.h"
41 #include "cli/cli-style.h"
42 #include "gdbsupport/selftest.h"
43 #include "selftest-arch.h"
45 static struct obstack dont_print_vb_obstack
;
46 static struct obstack dont_print_statmem_obstack
;
47 static struct obstack dont_print_stat_array_obstack
;
49 static void cp_print_static_field (struct type
*, struct value
*,
50 struct ui_file
*, int,
51 const struct value_print_options
*);
53 static void cp_print_value (struct value
*, struct ui_file
*,
54 int, const struct value_print_options
*,
58 /* GCC versions after 2.4.5 use this. */
59 const char vtbl_ptr_name
[] = "__vtbl_ptr_type";
61 /* Return truth value for assertion that TYPE is of the type
62 "pointer to virtual function". */
65 cp_is_vtbl_ptr_type (struct type
*type
)
67 const char *type_name
= type
->name ();
69 return (type_name
!= NULL
&& !strcmp (type_name
, vtbl_ptr_name
));
72 /* Return truth value for the assertion that TYPE is of the type
73 "pointer to virtual function table". */
76 cp_is_vtbl_member (struct type
*type
)
78 /* With older versions of g++, the vtbl field pointed to an array of
79 structures. Nowadays it points directly to the structure. */
80 if (type
->code () == TYPE_CODE_PTR
)
82 type
= type
->target_type ();
83 if (type
->code () == TYPE_CODE_ARRAY
)
85 type
= type
->target_type ();
86 if (type
->code () == TYPE_CODE_STRUCT
/* if not using thunks */
87 || type
->code () == TYPE_CODE_PTR
) /* if using thunks */
89 /* Virtual functions tables are full of pointers
90 to virtual functions. */
91 return cp_is_vtbl_ptr_type (type
);
94 else if (type
->code () == TYPE_CODE_STRUCT
) /* if not using thunks */
96 return cp_is_vtbl_ptr_type (type
);
98 else if (type
->code () == TYPE_CODE_PTR
) /* if using thunks */
100 /* The type name of the thunk pointer is NULL when using
101 dwarf2. We could test for a pointer to a function, but
102 there is no type info for the virtual table either, so it
104 return cp_is_vtbl_ptr_type (type
);
110 /* Mutually recursive subroutines of cp_print_value and c_val_print to
111 print out a structure's fields: cp_print_value_fields and
114 TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and OPTIONS have the same
115 meanings as in cp_print_value and c_val_print.
117 2nd argument REAL_TYPE is used to carry over the type of the
118 derived class across the recursion to base classes.
120 DONT_PRINT is an array of baseclass types that we should not print,
121 or zero if called from top level. */
124 cp_print_value_fields (struct value
*val
, struct ui_file
*stream
,
125 int recurse
, const struct value_print_options
*options
,
126 struct type
**dont_print_vb
,
127 int dont_print_statmem
)
129 int i
, len
, n_baseclasses
;
131 static int last_set_recurse
= -1;
133 struct type
*type
= check_typedef (val
->type ());
137 /* Any object can be left on obstacks only during an unexpected
140 if (obstack_object_size (&dont_print_statmem_obstack
) > 0)
142 obstack_free (&dont_print_statmem_obstack
, NULL
);
143 obstack_begin (&dont_print_statmem_obstack
,
144 32 * sizeof (CORE_ADDR
));
146 if (obstack_object_size (&dont_print_stat_array_obstack
) > 0)
148 obstack_free (&dont_print_stat_array_obstack
, NULL
);
149 obstack_begin (&dont_print_stat_array_obstack
,
150 32 * sizeof (struct type
*));
154 gdb_printf (stream
, "{");
155 len
= type
->num_fields ();
156 n_baseclasses
= TYPE_N_BASECLASSES (type
);
158 /* First, print out baseclasses such that we don't print
159 duplicates of virtual baseclasses. */
161 if (n_baseclasses
> 0)
162 cp_print_value (val
, stream
, recurse
+ 1, options
, dont_print_vb
);
164 /* Second, print out data fields */
166 /* If there are no data fields, skip this part */
167 if (len
== n_baseclasses
|| !len
)
168 fprintf_styled (stream
, metadata_style
.style (), "<No data fields>");
171 size_t statmem_obstack_initial_size
= 0;
172 size_t stat_array_obstack_initial_size
= 0;
173 struct type
*vptr_basetype
= NULL
;
176 if (dont_print_statmem
== 0)
178 statmem_obstack_initial_size
=
179 obstack_object_size (&dont_print_statmem_obstack
);
181 if (last_set_recurse
!= recurse
)
183 stat_array_obstack_initial_size
=
184 obstack_object_size (&dont_print_stat_array_obstack
);
186 last_set_recurse
= recurse
;
190 vptr_fieldno
= get_vptr_fieldno (type
, &vptr_basetype
);
191 for (i
= n_baseclasses
; i
< len
; i
++)
193 const gdb_byte
*valaddr
= val
->contents_for_printing ().data ();
195 /* If requested, skip printing of static fields. */
196 if (!options
->static_field_print
197 && type
->field (i
).is_static ())
202 gdb_puts (",", stream
);
203 if (!options
->prettyformat
)
204 gdb_puts (" ", stream
);
206 else if (n_baseclasses
> 0)
208 if (options
->prettyformat
)
210 gdb_printf (stream
, "\n");
211 print_spaces (2 + 2 * recurse
, stream
);
212 gdb_puts ("members of ", stream
);
213 gdb_puts (type
->name (), stream
);
214 gdb_puts (":", stream
);
219 if (options
->prettyformat
)
221 gdb_printf (stream
, "\n");
222 print_spaces (2 + 2 * recurse
, stream
);
226 stream
->wrap_here (2 + 2 * recurse
);
229 annotate_field_begin (type
->field (i
).type ());
231 if (type
->field (i
).is_static ())
233 gdb_puts ("static ", stream
);
234 fprintf_symbol (stream
,
235 type
->field (i
).name (),
236 current_language
->la_language
,
237 DMGL_PARAMS
| DMGL_ANSI
);
240 fputs_styled (type
->field (i
).name (),
241 variable_name_style
.style (), stream
);
242 annotate_field_name_end ();
244 /* We tweak various options in a few cases below. */
245 value_print_options options_copy
= *options
;
246 value_print_options
*opts
= &options_copy
;
248 /* Do not print leading '=' in case of anonymous
250 if (strcmp (type
->field (i
).name (), ""))
251 gdb_puts (" = ", stream
);
254 /* If this is an anonymous field then we want to consider it
255 as though it is at its parent's depth when it comes to the
257 if (opts
->max_depth
!= -1 && opts
->max_depth
< INT_MAX
)
260 annotate_field_value ();
262 if (!type
->field (i
).is_static ()
263 && type
->field (i
).is_packed ())
267 /* Bitfields require special handling, especially due to
268 byte order problems. */
269 if (type
->field (i
).is_ignored ())
271 fputs_styled ("<optimized out or zero length>",
272 metadata_style
.style (), stream
);
274 else if (val
->bits_synthetic_pointer
275 (type
->field (i
).loc_bitpos (),
276 type
->field (i
).bitsize ()))
278 fputs_styled (_("<synthetic pointer>"),
279 metadata_style
.style (), stream
);
283 opts
->deref_ref
= false;
285 v
= value_field_bitfield (type
, i
, valaddr
,
286 val
->embedded_offset (), val
);
288 common_val_print (v
, stream
, recurse
+ 1,
289 opts
, current_language
);
294 if (type
->field (i
).is_ignored ())
296 fputs_styled ("<optimized out or zero length>",
297 metadata_style
.style (), stream
);
299 else if (type
->field (i
).is_static ())
303 struct value
*v
= value_static_field (type
, i
);
305 cp_print_static_field (type
->field (i
).type (),
306 v
, stream
, recurse
+ 1,
309 catch (const gdb_exception_error
&ex
)
311 fprintf_styled (stream
, metadata_style
.style (),
312 _("<error reading variable: %s>"),
316 else if (i
== vptr_fieldno
&& type
== vptr_basetype
)
318 int i_offset
= type
->field (i
).loc_bitpos () / 8;
319 struct type
*i_type
= type
->field (i
).type ();
321 if (valprint_check_validity (stream
, i_type
, i_offset
, val
))
325 i_offset
+= val
->embedded_offset ();
326 addr
= extract_typed_address (valaddr
+ i_offset
, i_type
);
327 print_function_pointer_address (opts
,
334 struct value
*v
= val
->primitive_field (0, i
, type
);
335 opts
->deref_ref
= false;
336 common_val_print (v
, stream
, recurse
+ 1, opts
,
340 annotate_field_end ();
343 if (dont_print_statmem
== 0)
345 size_t obstack_final_size
=
346 obstack_object_size (&dont_print_statmem_obstack
);
348 if (obstack_final_size
> statmem_obstack_initial_size
)
350 /* In effect, a pop of the printed-statics stack. */
352 = statmem_obstack_initial_size
- obstack_final_size
;
353 obstack_blank_fast (&dont_print_statmem_obstack
, shrink_bytes
);
356 if (last_set_recurse
!= recurse
)
359 obstack_object_size (&dont_print_stat_array_obstack
);
361 if (obstack_final_size
> stat_array_obstack_initial_size
)
364 (char *) obstack_next_free (&dont_print_stat_array_obstack
)
365 - (obstack_final_size
366 - stat_array_obstack_initial_size
);
368 obstack_free (&dont_print_stat_array_obstack
,
371 last_set_recurse
= -1;
375 if (options
->prettyformat
)
377 gdb_printf (stream
, "\n");
378 print_spaces (2 * recurse
, stream
);
380 } /* if there are data fields */
382 gdb_printf (stream
, "}");
385 /* A wrapper for cp_print_value_fields that tries to apply a
386 pretty-printer first. */
389 cp_print_value_fields_pp (struct value
*val
,
390 struct ui_file
*stream
,
392 const struct value_print_options
*options
,
393 struct type
**dont_print_vb
,
394 int dont_print_statmem
)
398 /* Attempt to run an extension language pretty-printer if
402 = apply_ext_lang_val_pretty_printer (val
, stream
,
407 cp_print_value_fields (val
, stream
, recurse
, options
, dont_print_vb
,
411 /* Special val_print routine to avoid printing multiple copies of
412 virtual baseclasses. */
415 cp_print_value (struct value
*val
, struct ui_file
*stream
,
416 int recurse
, const struct value_print_options
*options
,
417 struct type
**dont_print_vb
)
419 struct type
*type
= check_typedef (val
->type ());
420 CORE_ADDR address
= val
->address ();
421 struct type
**last_dont_print
422 = (struct type
**) obstack_next_free (&dont_print_vb_obstack
);
423 struct obstack tmp_obstack
= dont_print_vb_obstack
;
424 int i
, n_baseclasses
= TYPE_N_BASECLASSES (type
);
425 const gdb_byte
*valaddr
= val
->contents_for_printing ().data ();
427 if (dont_print_vb
== 0)
429 /* If we're at top level, carve out a completely fresh chunk of
430 the obstack and use that until this particular invocation
432 /* Bump up the high-water mark. Now alpha is omega. */
433 obstack_finish (&dont_print_vb_obstack
);
436 for (i
= 0; i
< n_baseclasses
; i
++)
440 struct type
*baseclass
= check_typedef (TYPE_BASECLASS (type
, i
));
441 const char *basename
= baseclass
->name ();
442 struct value
*base_val
= NULL
;
444 if (BASETYPE_VIA_VIRTUAL (type
, i
))
446 struct type
**first_dont_print
447 = (struct type
**) obstack_base (&dont_print_vb_obstack
);
449 int j
= (struct type
**)
450 obstack_next_free (&dont_print_vb_obstack
) - first_dont_print
;
453 if (baseclass
== first_dont_print
[j
])
456 obstack_ptr_grow (&dont_print_vb_obstack
, baseclass
);
461 boffset
= baseclass_offset (type
, i
, valaddr
,
462 val
->embedded_offset (),
465 catch (const gdb_exception_error
&ex
)
467 if (ex
.error
== NOT_AVAILABLE_ERROR
)
475 if (BASETYPE_VIA_VIRTUAL (type
, i
))
477 /* The virtual base class pointer might have been
478 clobbered by the user program. Make sure that it
479 still points to a valid memory location. */
481 if (boffset
< 0 || boffset
>= type
->length ())
483 gdb::byte_vector
buf (baseclass
->length ());
485 if (target_read_memory (address
+ boffset
, buf
.data (),
486 baseclass
->length ()) != 0)
488 base_val
= value_from_contents_and_address (baseclass
,
491 baseclass
= base_val
->type ();
505 /* Now do the printing. */
506 if (options
->prettyformat
)
508 gdb_printf (stream
, "\n");
509 print_spaces (2 * recurse
, stream
);
511 gdb_puts ("<", stream
);
512 /* Not sure what the best notation is in the case where there is
513 no baseclass name. */
514 gdb_puts (basename
? basename
: "", stream
);
515 gdb_puts ("> = ", stream
);
518 val_print_unavailable (stream
);
520 val_print_invalid_address (stream
);
523 if (!val_print_check_max_depth (stream
, recurse
, options
,
526 struct value
*baseclass_val
= val
->primitive_field (0,
529 cp_print_value_fields_pp
530 (baseclass_val
, stream
, recurse
, options
,
531 (struct type
**) obstack_base (&dont_print_vb_obstack
),
535 gdb_puts (", ", stream
);
541 if (dont_print_vb
== 0)
543 /* Free the space used to deal with the printing
544 of this type from top level. */
545 obstack_free (&dont_print_vb_obstack
, last_dont_print
);
546 /* Reset watermark so that we can continue protecting
547 ourselves from whatever we were protecting ourselves. */
548 dont_print_vb_obstack
= tmp_obstack
;
552 /* Print value of a static member. To avoid infinite recursion when
553 printing a class that contains a static instance of the class, we
554 keep the addresses of all printed static member classes in an
555 obstack and refuse to print them more than once.
557 VAL contains the value to print, TYPE, STREAM, RECURSE, and OPTIONS
558 have the same meanings as in c_val_print. */
561 cp_print_static_field (struct type
*type
,
563 struct ui_file
*stream
,
565 const struct value_print_options
*options
)
567 struct value_print_options opts
;
569 if (val
->entirely_optimized_out ())
571 val_print_optimized_out (val
, stream
);
575 struct type
*real_type
= check_typedef (type
);
576 if (real_type
->code () == TYPE_CODE_STRUCT
)
578 CORE_ADDR
*first_dont_print
;
579 CORE_ADDR addr
= val
->address ();
583 = (CORE_ADDR
*) obstack_base (&dont_print_statmem_obstack
);
584 i
= obstack_object_size (&dont_print_statmem_obstack
)
585 / sizeof (CORE_ADDR
);
589 if (addr
== first_dont_print
[i
])
591 fputs_styled (_("<same as static member of an already"
593 metadata_style
.style (), stream
);
598 obstack_grow (&dont_print_statmem_obstack
, (char *) &addr
,
600 cp_print_value_fields_pp (val
, stream
, recurse
, options
, nullptr, 1);
604 if (real_type
->code () == TYPE_CODE_ARRAY
)
606 struct type
**first_dont_print
;
608 struct type
*target_type
= type
->target_type ();
611 = (struct type
**) obstack_base (&dont_print_stat_array_obstack
);
612 i
= obstack_object_size (&dont_print_stat_array_obstack
)
613 / sizeof (struct type
*);
617 if (target_type
== first_dont_print
[i
])
619 fputs_styled (_("<same as static member of an already"
621 metadata_style
.style (), stream
);
626 obstack_grow (&dont_print_stat_array_obstack
,
627 (char *) &target_type
,
628 sizeof (struct type
*));
632 opts
.deref_ref
= false;
633 common_val_print (val
, stream
, recurse
, &opts
, current_language
);
636 /* Find the field in *SELF, or its non-virtual base classes, with
637 bit offset OFFSET. Set *SELF to the containing type and *FIELDNO
638 to the containing field number. If OFFSET is not exactly at the
639 start of some field, set *SELF to NULL. */
642 cp_find_class_member (struct type
**self_p
, int *fieldno
,
649 *self_p
= check_typedef (*self_p
);
651 len
= self
->num_fields ();
653 for (i
= TYPE_N_BASECLASSES (self
); i
< len
; i
++)
655 field
&f
= self
->field (i
);
658 LONGEST bitpos
= f
.loc_bitpos ();
661 if (offset
== bitpos
)
668 for (i
= 0; i
< TYPE_N_BASECLASSES (self
); i
++)
670 LONGEST bitpos
= self
->field (i
).loc_bitpos ();
671 LONGEST bitsize
= 8 * self
->field (i
).type ()->length ();
673 if (offset
>= bitpos
&& offset
< bitpos
+ bitsize
)
675 *self_p
= self
->field (i
).type ();
676 cp_find_class_member (self_p
, fieldno
, offset
- bitpos
);
685 cp_print_class_member (const gdb_byte
*valaddr
, struct type
*type
,
686 struct ui_file
*stream
, const char *prefix
)
688 enum bfd_endian byte_order
= type_byte_order (type
);
690 /* VAL is a byte offset into the structure type SELF_TYPE.
691 Find the name of the field for that offset and
693 struct type
*self_type
= TYPE_SELF_TYPE (type
);
697 val
= extract_signed_integer (valaddr
,
701 /* Pointers to data members are usually byte offsets into an object.
702 Because a data member can have offset zero, and a NULL pointer to
703 member must be distinct from any valid non-NULL pointer to
704 member, either the value is biased or the NULL value has a
705 special representation; both are permitted by ISO C++. HP aCC
706 used a bias of 0x20000000; HP cfront used a bias of 1; g++ 3.x
707 and other compilers which use the Itanium ABI use -1 as the NULL
708 value. GDB only supports that last form; to add support for
709 another form, make this into a cp-abi hook. */
713 gdb_printf (stream
, "NULL");
717 cp_find_class_member (&self_type
, &fieldno
, val
<< 3);
719 if (self_type
!= NULL
)
723 gdb_puts (prefix
, stream
);
724 name
= self_type
->name ();
726 gdb_puts (name
, stream
);
728 c_type_print_base (self_type
, stream
, 0, 0, &type_print_raw_options
);
729 gdb_printf (stream
, "::");
730 fputs_styled (self_type
->field (fieldno
).name (),
731 variable_name_style
.style (), stream
);
734 gdb_printf (stream
, "%ld", (long) val
);
739 /* Test printing of TYPE_CODE_STRUCT values. */
742 test_print_fields (gdbarch
*arch
)
745 type
*uint8_type
= builtin_type (arch
)->builtin_uint8
;
746 type
*bool_type
= builtin_type (arch
)->builtin_bool
;
747 type
*the_struct
= arch_composite_type (arch
, NULL
, TYPE_CODE_STRUCT
);
748 the_struct
->set_length (4);
752 if (gdbarch_byte_order (arch
) == BFD_ENDIAN_LITTLE
)
754 f
= append_composite_type_field_raw (the_struct
, "A", bool_type
);
755 f
->set_loc_bitpos (1);
757 f
= append_composite_type_field_raw (the_struct
, "B", uint8_type
);
758 f
->set_loc_bitpos (3);
760 f
= append_composite_type_field_raw (the_struct
, "C", bool_type
);
761 f
->set_loc_bitpos (7);
764 /* According to the logic commented in "make_gdb_type_struct ()" of
765 * target-descriptions.c, bit positions are numbered differently for
766 * little and big endians. */
769 f
= append_composite_type_field_raw (the_struct
, "A", bool_type
);
770 f
->set_loc_bitpos (30);
772 f
= append_composite_type_field_raw (the_struct
, "B", uint8_type
);
773 f
->set_loc_bitpos (26);
775 f
= append_composite_type_field_raw (the_struct
, "C", bool_type
);
776 f
->set_loc_bitpos (24);
780 value
*val
= value::allocate (the_struct
);
781 gdb_byte
*contents
= val
->contents_writeable ().data ();
782 store_unsigned_integer (contents
, val
->enclosing_type ()->length (),
783 gdbarch_byte_order (arch
), 0xe9);
786 struct value_print_options opts
;
787 get_no_prettyformat_print_options (&opts
);
788 cp_print_value_fields(val
, &out
, 0, &opts
, NULL
, 0);
789 SELF_CHECK (out
.string () == "{A = false, B = 5, C = true}");
793 cp_print_value_fields(val
, &out
, 0, &opts
, NULL
, 0);
794 SELF_CHECK (out
.string () == "{A = 0x0, B = 0x5, C = 0x1}");
800 void _initialize_cp_valprint ();
802 _initialize_cp_valprint ()
805 selftests::register_test_foreach_arch ("print-fields", test_print_fields
);
808 obstack_begin (&dont_print_stat_array_obstack
,
809 32 * sizeof (struct type
*));
810 obstack_begin (&dont_print_statmem_obstack
,
811 32 * sizeof (CORE_ADDR
));
812 obstack_begin (&dont_print_vb_obstack
,
813 32 * sizeof (struct type
*));