1 /* Support for printing C++ values for GDB, the GNU debugger.
3 Copyright (C) 1986-2020 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"
42 static struct obstack dont_print_vb_obstack
;
43 static struct obstack dont_print_statmem_obstack
;
44 static struct obstack dont_print_stat_array_obstack
;
46 static void cp_print_static_field (struct type
*, struct value
*,
47 struct ui_file
*, int,
48 const struct value_print_options
*);
50 static void cp_print_value (struct value
*, struct ui_file
*,
51 int, const struct value_print_options
*,
55 /* GCC versions after 2.4.5 use this. */
56 const char vtbl_ptr_name
[] = "__vtbl_ptr_type";
58 /* Return truth value for assertion that TYPE is of the type
59 "pointer to virtual function". */
62 cp_is_vtbl_ptr_type (struct type
*type
)
64 const char *type_name
= TYPE_NAME (type
);
66 return (type_name
!= NULL
&& !strcmp (type_name
, vtbl_ptr_name
));
69 /* Return truth value for the assertion that TYPE is of the type
70 "pointer to virtual function table". */
73 cp_is_vtbl_member (struct type
*type
)
75 /* With older versions of g++, the vtbl field pointed to an array of
76 structures. Nowadays it points directly to the structure. */
77 if (type
->code () == TYPE_CODE_PTR
)
79 type
= TYPE_TARGET_TYPE (type
);
80 if (type
->code () == TYPE_CODE_ARRAY
)
82 type
= TYPE_TARGET_TYPE (type
);
83 if (type
->code () == TYPE_CODE_STRUCT
/* if not using thunks */
84 || type
->code () == TYPE_CODE_PTR
) /* if using thunks */
86 /* Virtual functions tables are full of pointers
87 to virtual functions. */
88 return cp_is_vtbl_ptr_type (type
);
91 else if (type
->code () == TYPE_CODE_STRUCT
) /* if not using thunks */
93 return cp_is_vtbl_ptr_type (type
);
95 else if (type
->code () == TYPE_CODE_PTR
) /* if using thunks */
97 /* The type name of the thunk pointer is NULL when using
98 dwarf2. We could test for a pointer to a function, but
99 there is no type info for the virtual table either, so it
101 return cp_is_vtbl_ptr_type (type
);
107 /* Mutually recursive subroutines of cp_print_value and c_val_print to
108 print out a structure's fields: cp_print_value_fields and
111 TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and OPTIONS have the same
112 meanings as in cp_print_value and c_val_print.
114 2nd argument REAL_TYPE is used to carry over the type of the
115 derived class across the recursion to base classes.
117 DONT_PRINT is an array of baseclass types that we should not print,
118 or zero if called from top level. */
121 cp_print_value_fields (struct value
*val
, struct ui_file
*stream
,
122 int recurse
, const struct value_print_options
*options
,
123 struct type
**dont_print_vb
,
124 int dont_print_statmem
)
126 int i
, len
, n_baseclasses
;
128 static int last_set_recurse
= -1;
130 struct type
*type
= check_typedef (value_type (val
));
134 /* Any object can be left on obstacks only during an unexpected
137 if (obstack_object_size (&dont_print_statmem_obstack
) > 0)
139 obstack_free (&dont_print_statmem_obstack
, NULL
);
140 obstack_begin (&dont_print_statmem_obstack
,
141 32 * sizeof (CORE_ADDR
));
143 if (obstack_object_size (&dont_print_stat_array_obstack
) > 0)
145 obstack_free (&dont_print_stat_array_obstack
, NULL
);
146 obstack_begin (&dont_print_stat_array_obstack
,
147 32 * sizeof (struct type
*));
151 fprintf_filtered (stream
, "{");
152 len
= TYPE_NFIELDS (type
);
153 n_baseclasses
= TYPE_N_BASECLASSES (type
);
155 /* First, print out baseclasses such that we don't print
156 duplicates of virtual baseclasses. */
158 if (n_baseclasses
> 0)
159 cp_print_value (val
, stream
, recurse
+ 1, options
, dont_print_vb
);
161 /* Second, print out data fields */
163 /* If there are no data fields, skip this part */
164 if (len
== n_baseclasses
|| !len
)
165 fprintf_styled (stream
, metadata_style
.style (), "<No data fields>");
168 size_t statmem_obstack_initial_size
= 0;
169 size_t stat_array_obstack_initial_size
= 0;
170 struct type
*vptr_basetype
= NULL
;
173 if (dont_print_statmem
== 0)
175 statmem_obstack_initial_size
=
176 obstack_object_size (&dont_print_statmem_obstack
);
178 if (last_set_recurse
!= recurse
)
180 stat_array_obstack_initial_size
=
181 obstack_object_size (&dont_print_stat_array_obstack
);
183 last_set_recurse
= recurse
;
187 vptr_fieldno
= get_vptr_fieldno (type
, &vptr_basetype
);
188 for (i
= n_baseclasses
; i
< len
; i
++)
190 const gdb_byte
*valaddr
= value_contents_for_printing (val
);
192 /* If requested, skip printing of static fields. */
193 if (!options
->static_field_print
194 && field_is_static (&TYPE_FIELD (type
, i
)))
199 fputs_filtered (",", stream
);
200 if (!options
->prettyformat
)
201 fputs_filtered (" ", stream
);
203 else if (n_baseclasses
> 0)
205 if (options
->prettyformat
)
207 fprintf_filtered (stream
, "\n");
208 print_spaces_filtered (2 + 2 * recurse
, stream
);
209 fputs_filtered ("members of ", stream
);
210 fputs_filtered (TYPE_NAME (type
), stream
);
211 fputs_filtered (":", stream
);
216 if (options
->prettyformat
)
218 fprintf_filtered (stream
, "\n");
219 print_spaces_filtered (2 + 2 * recurse
, stream
);
223 wrap_here (n_spaces (2 + 2 * recurse
));
226 annotate_field_begin (TYPE_FIELD_TYPE (type
, i
));
228 if (field_is_static (&TYPE_FIELD (type
, i
)))
230 fputs_filtered ("static ", stream
);
231 fprintf_symbol_filtered (stream
,
232 TYPE_FIELD_NAME (type
, i
),
233 current_language
->la_language
,
234 DMGL_PARAMS
| DMGL_ANSI
);
237 fputs_styled (TYPE_FIELD_NAME (type
, i
),
238 variable_name_style
.style (), stream
);
239 annotate_field_name_end ();
241 /* We tweak various options in a few cases below. */
242 value_print_options options_copy
= *options
;
243 value_print_options
*opts
= &options_copy
;
245 /* Do not print leading '=' in case of anonymous
247 if (strcmp (TYPE_FIELD_NAME (type
, i
), ""))
248 fputs_filtered (" = ", stream
);
251 /* If this is an anonymous field then we want to consider it
252 as though it is at its parent's depth when it comes to the
254 if (opts
->max_depth
!= -1 && opts
->max_depth
< INT_MAX
)
257 annotate_field_value ();
259 if (!field_is_static (&TYPE_FIELD (type
, i
))
260 && TYPE_FIELD_PACKED (type
, i
))
264 /* Bitfields require special handling, especially due to
265 byte order problems. */
266 if (TYPE_FIELD_IGNORE (type
, i
))
268 fputs_styled ("<optimized out or zero length>",
269 metadata_style
.style (), stream
);
271 else if (value_bits_synthetic_pointer (val
,
272 TYPE_FIELD_BITPOS (type
,
274 TYPE_FIELD_BITSIZE (type
,
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 (type
, i
)))
302 struct value
*v
= value_static_field (type
, i
);
304 cp_print_static_field (TYPE_FIELD_TYPE (type
, i
),
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_BITPOS (type
, i
) / 8;
318 struct type
*i_type
= TYPE_FIELD_TYPE (type
, i
);
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
,
327 get_type_arch (type
),
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 fprintf_filtered (stream
, "\n");
377 print_spaces_filtered (2 * recurse
, stream
);
379 } /* if there are data fields */
381 fprintf_filtered (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
);
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
= TYPE_NAME (baseclass
);
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 fprintf_filtered (stream
, "\n");
482 print_spaces_filtered (2 * recurse
, stream
);
484 fputs_filtered ("<", stream
);
485 /* Not sure what the best notation is in the case where there is
486 no baseclass name. */
487 fputs_filtered (basename
? basename
: "", stream
);
488 fputs_filtered ("> = ", stream
);
491 val_print_unavailable (stream
);
493 val_print_invalid_address (stream
);
498 if (options
->max_depth
> -1
499 && recurse
>= options
->max_depth
)
501 const struct language_defn
*language
= current_language
;
502 gdb_assert (language
->la_struct_too_deep_ellipsis
!= NULL
);
503 fputs_filtered (language
->la_struct_too_deep_ellipsis
, stream
);
507 struct value
*baseclass_val
= value_primitive_field (val
, 0,
510 /* Attempt to run an extension language pretty-printer on the
511 baseclass if possible. */
514 = apply_ext_lang_val_pretty_printer (baseclass_val
, stream
,
519 cp_print_value_fields (baseclass_val
, stream
, recurse
, options
,
521 obstack_base (&dont_print_vb_obstack
)),
525 fputs_filtered (", ", stream
);
531 if (dont_print_vb
== 0)
533 /* Free the space used to deal with the printing
534 of this type from top level. */
535 obstack_free (&dont_print_vb_obstack
, last_dont_print
);
536 /* Reset watermark so that we can continue protecting
537 ourselves from whatever we were protecting ourselves. */
538 dont_print_vb_obstack
= tmp_obstack
;
542 /* Print value of a static member. To avoid infinite recursion when
543 printing a class that contains a static instance of the class, we
544 keep the addresses of all printed static member classes in an
545 obstack and refuse to print them more than once.
547 VAL contains the value to print, TYPE, STREAM, RECURSE, and OPTIONS
548 have the same meanings as in c_val_print. */
551 cp_print_static_field (struct type
*type
,
553 struct ui_file
*stream
,
555 const struct value_print_options
*options
)
557 struct value_print_options opts
;
559 if (value_entirely_optimized_out (val
))
561 val_print_optimized_out (val
, stream
);
565 struct type
*real_type
= check_typedef (type
);
566 if (real_type
->code () == TYPE_CODE_STRUCT
)
568 CORE_ADDR
*first_dont_print
;
569 CORE_ADDR addr
= value_address (val
);
573 = (CORE_ADDR
*) obstack_base (&dont_print_statmem_obstack
);
574 i
= obstack_object_size (&dont_print_statmem_obstack
)
575 / sizeof (CORE_ADDR
);
579 if (addr
== first_dont_print
[i
])
581 fputs_styled (_("<same as static member of an already"
583 metadata_style
.style (), stream
);
588 obstack_grow (&dont_print_statmem_obstack
, (char *) &addr
,
590 cp_print_value_fields (val
, stream
, recurse
, options
, NULL
, 1);
594 if (real_type
->code () == TYPE_CODE_ARRAY
)
596 struct type
**first_dont_print
;
598 struct type
*target_type
= TYPE_TARGET_TYPE (type
);
601 = (struct type
**) obstack_base (&dont_print_stat_array_obstack
);
602 i
= obstack_object_size (&dont_print_stat_array_obstack
)
603 / sizeof (struct type
*);
607 if (target_type
== first_dont_print
[i
])
609 fputs_styled (_("<same as static member of an already"
611 metadata_style
.style (), stream
);
616 obstack_grow (&dont_print_stat_array_obstack
,
617 (char *) &target_type
,
618 sizeof (struct type
*));
623 common_val_print (val
, stream
, recurse
, &opts
, current_language
);
626 /* Find the field in *SELF, or its non-virtual base classes, with
627 bit offset OFFSET. Set *SELF to the containing type and *FIELDNO
628 to the containing field number. If OFFSET is not exactly at the
629 start of some field, set *SELF to NULL. */
632 cp_find_class_member (struct type
**self_p
, int *fieldno
,
639 *self_p
= check_typedef (*self_p
);
641 len
= TYPE_NFIELDS (self
);
643 for (i
= TYPE_N_BASECLASSES (self
); i
< len
; i
++)
645 LONGEST bitpos
= TYPE_FIELD_BITPOS (self
, i
);
648 if (offset
== bitpos
)
655 for (i
= 0; i
< TYPE_N_BASECLASSES (self
); i
++)
657 LONGEST bitpos
= TYPE_FIELD_BITPOS (self
, i
);
658 LONGEST bitsize
= 8 * TYPE_LENGTH (TYPE_FIELD_TYPE (self
, i
));
660 if (offset
>= bitpos
&& offset
< bitpos
+ bitsize
)
662 *self_p
= TYPE_FIELD_TYPE (self
, i
);
663 cp_find_class_member (self_p
, fieldno
, offset
- bitpos
);
672 cp_print_class_member (const gdb_byte
*valaddr
, struct type
*type
,
673 struct ui_file
*stream
, const char *prefix
)
675 enum bfd_endian byte_order
= type_byte_order (type
);
677 /* VAL is a byte offset into the structure type SELF_TYPE.
678 Find the name of the field for that offset and
680 struct type
*self_type
= TYPE_SELF_TYPE (type
);
684 val
= extract_signed_integer (valaddr
,
688 /* Pointers to data members are usually byte offsets into an object.
689 Because a data member can have offset zero, and a NULL pointer to
690 member must be distinct from any valid non-NULL pointer to
691 member, either the value is biased or the NULL value has a
692 special representation; both are permitted by ISO C++. HP aCC
693 used a bias of 0x20000000; HP cfront used a bias of 1; g++ 3.x
694 and other compilers which use the Itanium ABI use -1 as the NULL
695 value. GDB only supports that last form; to add support for
696 another form, make this into a cp-abi hook. */
700 fprintf_filtered (stream
, "NULL");
704 cp_find_class_member (&self_type
, &fieldno
, val
<< 3);
706 if (self_type
!= NULL
)
710 fputs_filtered (prefix
, stream
);
711 name
= TYPE_NAME (self_type
);
713 fputs_filtered (name
, stream
);
715 c_type_print_base (self_type
, stream
, 0, 0, &type_print_raw_options
);
716 fprintf_filtered (stream
, "::");
717 fputs_styled (TYPE_FIELD_NAME (self_type
, fieldno
),
718 variable_name_style
.style (), stream
);
721 fprintf_filtered (stream
, "%ld", (long) val
);
725 void _initialize_cp_valprint ();
727 _initialize_cp_valprint ()
729 obstack_begin (&dont_print_stat_array_obstack
,
730 32 * sizeof (struct type
*));
731 obstack_begin (&dont_print_statmem_obstack
,
732 32 * sizeof (CORE_ADDR
));
733 obstack_begin (&dont_print_vb_obstack
,
734 32 * sizeof (struct type
*));