1 /* Support for printing C++ values for GDB, the GNU debugger.
3 Copyright (C) 1986-2017 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 "byte-vector.h"
40 /* Controls printing of vtbl's. */
42 show_vtblprint (struct ui_file
*file
, int from_tty
,
43 struct cmd_list_element
*c
, const char *value
)
45 fprintf_filtered (file
, _("\
46 Printing of C++ virtual function tables is %s.\n"),
50 /* Controls looking up an object's derived type using what we find in
53 show_objectprint (struct ui_file
*file
, int from_tty
,
54 struct cmd_list_element
*c
,
57 fprintf_filtered (file
, _("\
58 Printing of object's derived type based on vtable info is %s.\n"),
63 show_static_field_print (struct ui_file
*file
, int from_tty
,
64 struct cmd_list_element
*c
,
67 fprintf_filtered (file
,
68 _("Printing of C++ static members is %s.\n"),
73 static struct obstack dont_print_vb_obstack
;
74 static struct obstack dont_print_statmem_obstack
;
75 static struct obstack dont_print_stat_array_obstack
;
77 extern void _initialize_cp_valprint (void);
79 static void cp_print_static_field (struct type
*, struct value
*,
80 struct ui_file
*, int,
81 const struct value_print_options
*);
83 static void cp_print_value (struct type
*, struct type
*,
85 CORE_ADDR
, struct ui_file
*,
87 const struct value_print_options
*,
91 /* GCC versions after 2.4.5 use this. */
92 extern const char vtbl_ptr_name
[] = "__vtbl_ptr_type";
94 /* Return truth value for assertion that TYPE is of the type
95 "pointer to virtual function". */
98 cp_is_vtbl_ptr_type (struct type
*type
)
100 const char *type_name
= type_name_no_tag (type
);
102 return (type_name
!= NULL
&& !strcmp (type_name
, vtbl_ptr_name
));
105 /* Return truth value for the assertion that TYPE is of the type
106 "pointer to virtual function table". */
109 cp_is_vtbl_member (struct type
*type
)
111 /* With older versions of g++, the vtbl field pointed to an array of
112 structures. Nowadays it points directly to the structure. */
113 if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
115 type
= TYPE_TARGET_TYPE (type
);
116 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
)
118 type
= TYPE_TARGET_TYPE (type
);
119 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
/* if not using thunks */
120 || TYPE_CODE (type
) == TYPE_CODE_PTR
) /* if using thunks */
122 /* Virtual functions tables are full of pointers
123 to virtual functions. */
124 return cp_is_vtbl_ptr_type (type
);
127 else if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
) /* if not using thunks */
129 return cp_is_vtbl_ptr_type (type
);
131 else if (TYPE_CODE (type
) == TYPE_CODE_PTR
) /* if using thunks */
133 /* The type name of the thunk pointer is NULL when using
134 dwarf2. We could test for a pointer to a function, but
135 there is no type info for the virtual table either, so it
137 return cp_is_vtbl_ptr_type (type
);
143 /* Mutually recursive subroutines of cp_print_value and c_val_print to
144 print out a structure's fields: cp_print_value_fields and
147 TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and OPTIONS have the same
148 meanings as in cp_print_value and c_val_print.
150 2nd argument REAL_TYPE is used to carry over the type of the
151 derived class across the recursion to base classes.
153 DONT_PRINT is an array of baseclass types that we should not print,
154 or zero if called from top level. */
157 cp_print_value_fields (struct type
*type
, struct type
*real_type
,
159 CORE_ADDR address
, struct ui_file
*stream
,
160 int recurse
, struct value
*val
,
161 const struct value_print_options
*options
,
162 struct type
**dont_print_vb
,
163 int dont_print_statmem
)
165 int i
, len
, n_baseclasses
;
167 static int last_set_recurse
= -1;
169 type
= check_typedef (type
);
173 /* Any object can be left on obstacks only during an unexpected
176 if (obstack_object_size (&dont_print_statmem_obstack
) > 0)
178 obstack_free (&dont_print_statmem_obstack
, NULL
);
179 obstack_begin (&dont_print_statmem_obstack
,
180 32 * sizeof (CORE_ADDR
));
182 if (obstack_object_size (&dont_print_stat_array_obstack
) > 0)
184 obstack_free (&dont_print_stat_array_obstack
, NULL
);
185 obstack_begin (&dont_print_stat_array_obstack
,
186 32 * sizeof (struct type
*));
190 fprintf_filtered (stream
, "{");
191 len
= TYPE_NFIELDS (type
);
192 n_baseclasses
= TYPE_N_BASECLASSES (type
);
194 /* First, print out baseclasses such that we don't print
195 duplicates of virtual baseclasses. */
197 if (n_baseclasses
> 0)
198 cp_print_value (type
, real_type
,
199 offset
, address
, stream
,
200 recurse
+ 1, val
, options
,
203 /* Second, print out data fields */
205 /* If there are no data fields, skip this part */
206 if (len
== n_baseclasses
|| !len
)
207 fprintf_filtered (stream
, "<No data fields>");
210 size_t statmem_obstack_initial_size
= 0;
211 size_t stat_array_obstack_initial_size
= 0;
212 struct type
*vptr_basetype
= NULL
;
215 if (dont_print_statmem
== 0)
217 statmem_obstack_initial_size
=
218 obstack_object_size (&dont_print_statmem_obstack
);
220 if (last_set_recurse
!= recurse
)
222 stat_array_obstack_initial_size
=
223 obstack_object_size (&dont_print_stat_array_obstack
);
225 last_set_recurse
= recurse
;
229 vptr_fieldno
= get_vptr_fieldno (type
, &vptr_basetype
);
230 for (i
= n_baseclasses
; i
< len
; i
++)
232 const gdb_byte
*valaddr
= value_contents_for_printing (val
);
234 /* If requested, skip printing of static fields. */
235 if (!options
->static_field_print
236 && field_is_static (&TYPE_FIELD (type
, i
)))
240 fprintf_filtered (stream
, ", ");
241 else if (n_baseclasses
> 0)
243 if (options
->prettyformat
)
245 fprintf_filtered (stream
, "\n");
246 print_spaces_filtered (2 + 2 * recurse
, stream
);
247 fputs_filtered ("members of ", stream
);
248 fputs_filtered (type_name_no_tag (type
), stream
);
249 fputs_filtered (": ", stream
);
254 if (options
->prettyformat
)
256 fprintf_filtered (stream
, "\n");
257 print_spaces_filtered (2 + 2 * recurse
, stream
);
261 wrap_here (n_spaces (2 + 2 * recurse
));
264 annotate_field_begin (TYPE_FIELD_TYPE (type
, i
));
266 if (field_is_static (&TYPE_FIELD (type
, i
)))
267 fputs_filtered ("static ", stream
);
268 fprintf_symbol_filtered (stream
,
269 TYPE_FIELD_NAME (type
, i
),
270 current_language
->la_language
,
271 DMGL_PARAMS
| DMGL_ANSI
);
272 annotate_field_name_end ();
273 /* Do not print leading '=' in case of anonymous
275 if (strcmp (TYPE_FIELD_NAME (type
, i
), ""))
276 fputs_filtered (" = ", stream
);
277 annotate_field_value ();
279 if (!field_is_static (&TYPE_FIELD (type
, i
))
280 && TYPE_FIELD_PACKED (type
, i
))
284 /* Bitfields require special handling, especially due to
285 byte order problems. */
286 if (TYPE_FIELD_IGNORE (type
, i
))
288 fputs_filtered ("<optimized out or zero length>", stream
);
290 else if (value_bits_synthetic_pointer (val
,
291 TYPE_FIELD_BITPOS (type
,
293 TYPE_FIELD_BITSIZE (type
,
296 fputs_filtered (_("<synthetic pointer>"), stream
);
300 struct value_print_options opts
= *options
;
304 v
= value_field_bitfield (type
, i
, valaddr
, offset
, val
);
306 common_val_print (v
, stream
, recurse
+ 1, &opts
,
312 if (TYPE_FIELD_IGNORE (type
, i
))
314 fputs_filtered ("<optimized out or zero length>",
317 else if (field_is_static (&TYPE_FIELD (type
, i
)))
319 struct value
*v
= NULL
;
323 v
= value_static_field (type
, i
);
326 CATCH (ex
, RETURN_MASK_ERROR
)
328 fprintf_filtered (stream
,
329 _("<error reading variable: %s>"),
334 cp_print_static_field (TYPE_FIELD_TYPE (type
, i
),
335 v
, stream
, recurse
+ 1,
338 else if (i
== vptr_fieldno
&& type
== vptr_basetype
)
340 int i_offset
= offset
+ TYPE_FIELD_BITPOS (type
, i
) / 8;
341 struct type
*i_type
= TYPE_FIELD_TYPE (type
, i
);
343 if (valprint_check_validity (stream
, i_type
, i_offset
, val
))
347 addr
= extract_typed_address (valaddr
+ i_offset
, i_type
);
348 print_function_pointer_address (options
,
349 get_type_arch (type
),
355 struct value_print_options opts
= *options
;
358 val_print (TYPE_FIELD_TYPE (type
, i
),
359 offset
+ TYPE_FIELD_BITPOS (type
, i
) / 8,
361 stream
, recurse
+ 1, val
, &opts
,
365 annotate_field_end ();
368 if (dont_print_statmem
== 0)
370 size_t obstack_final_size
=
371 obstack_object_size (&dont_print_statmem_obstack
);
373 if (obstack_final_size
> statmem_obstack_initial_size
)
375 /* In effect, a pop of the printed-statics stack. */
378 (char *) obstack_next_free (&dont_print_statmem_obstack
) -
379 (obstack_final_size
- statmem_obstack_initial_size
);
381 obstack_free (&dont_print_statmem_obstack
,
385 if (last_set_recurse
!= recurse
)
387 size_t obstack_final_size
=
388 obstack_object_size (&dont_print_stat_array_obstack
);
390 if (obstack_final_size
> stat_array_obstack_initial_size
)
393 (char *) obstack_next_free (&dont_print_stat_array_obstack
)
394 - (obstack_final_size
395 - stat_array_obstack_initial_size
);
397 obstack_free (&dont_print_stat_array_obstack
,
400 last_set_recurse
= -1;
404 if (options
->prettyformat
)
406 fprintf_filtered (stream
, "\n");
407 print_spaces_filtered (2 * recurse
, stream
);
409 } /* if there are data fields */
411 fprintf_filtered (stream
, "}");
414 /* Like cp_print_value_fields, but find the runtime type of the object
415 and pass it as the `real_type' argument to cp_print_value_fields.
416 This function is a hack to work around the fact that
417 common_val_print passes the embedded offset to val_print, but not
418 the enclosing type. */
421 cp_print_value_fields_rtti (struct type
*type
,
422 const gdb_byte
*valaddr
, LONGEST offset
,
424 struct ui_file
*stream
, int recurse
,
426 const struct value_print_options
*options
,
427 struct type
**dont_print_vb
,
428 int dont_print_statmem
)
430 struct type
*real_type
= NULL
;
432 /* We require all bits to be valid in order to attempt a
434 if (!value_bits_any_optimized_out (val
,
435 TARGET_CHAR_BIT
* offset
,
436 TARGET_CHAR_BIT
* TYPE_LENGTH (type
)))
442 /* Ugh, we have to convert back to a value here. */
443 value
= value_from_contents_and_address (type
, valaddr
+ offset
,
445 type
= value_type (value
);
446 /* We don't actually care about most of the result here -- just
447 the type. We already have the correct offset, due to how
448 val_print was initially called. */
449 real_type
= value_rtti_type (value
, &full
, &top
, &using_enc
);
455 cp_print_value_fields (type
, real_type
, offset
,
456 address
, stream
, recurse
, val
, options
,
457 dont_print_vb
, dont_print_statmem
);
460 /* Special val_print routine to avoid printing multiple copies of
461 virtual baseclasses. */
464 cp_print_value (struct type
*type
, struct type
*real_type
,
466 CORE_ADDR address
, struct ui_file
*stream
,
467 int recurse
, struct value
*val
,
468 const struct value_print_options
*options
,
469 struct type
**dont_print_vb
)
471 struct type
**last_dont_print
472 = (struct type
**) obstack_next_free (&dont_print_vb_obstack
);
473 struct obstack tmp_obstack
= dont_print_vb_obstack
;
474 int i
, n_baseclasses
= TYPE_N_BASECLASSES (type
);
476 struct type
*thistype
;
477 const gdb_byte
*valaddr
= value_contents_for_printing (val
);
479 if (dont_print_vb
== 0)
481 /* If we're at top level, carve out a completely fresh chunk of
482 the obstack and use that until this particular invocation
484 /* Bump up the high-water mark. Now alpha is omega. */
485 obstack_finish (&dont_print_vb_obstack
);
488 for (i
= 0; i
< n_baseclasses
; i
++)
492 struct type
*baseclass
= check_typedef (TYPE_BASECLASS (type
, i
));
493 const char *basename
= TYPE_NAME (baseclass
);
494 struct value
*base_val
= NULL
;
496 if (BASETYPE_VIA_VIRTUAL (type
, i
))
498 struct type
**first_dont_print
499 = (struct type
**) obstack_base (&dont_print_vb_obstack
);
501 int j
= (struct type
**)
502 obstack_next_free (&dont_print_vb_obstack
) - first_dont_print
;
505 if (baseclass
== first_dont_print
[j
])
508 obstack_ptr_grow (&dont_print_vb_obstack
, baseclass
);
512 thistype
= real_type
;
516 boffset
= baseclass_offset (type
, i
, valaddr
, offset
, address
, val
);
518 CATCH (ex
, RETURN_MASK_ERROR
)
520 if (ex
.error
== NOT_AVAILABLE_ERROR
)
529 if (BASETYPE_VIA_VIRTUAL (type
, i
))
531 /* The virtual base class pointer might have been
532 clobbered by the user program. Make sure that it
533 still points to a valid memory location. */
535 if ((boffset
+ offset
) < 0
536 || (boffset
+ offset
) >= TYPE_LENGTH (real_type
))
538 gdb::byte_vector
buf (TYPE_LENGTH (baseclass
));
540 if (target_read_memory (address
+ boffset
, buf
.data (),
541 TYPE_LENGTH (baseclass
)) != 0)
543 base_val
= value_from_contents_and_address (baseclass
,
546 baseclass
= value_type (base_val
);
549 thistype
= baseclass
;
562 /* Now do the printing. */
563 if (options
->prettyformat
)
565 fprintf_filtered (stream
, "\n");
566 print_spaces_filtered (2 * recurse
, stream
);
568 fputs_filtered ("<", stream
);
569 /* Not sure what the best notation is in the case where there is
570 no baseclass name. */
571 fputs_filtered (basename
? basename
: "", stream
);
572 fputs_filtered ("> = ", stream
);
575 val_print_unavailable (stream
);
577 val_print_invalid_address (stream
);
582 /* Attempt to run an extension language pretty-printer on the
583 baseclass if possible. */
586 = apply_ext_lang_val_pretty_printer (baseclass
,
587 thisoffset
+ boffset
,
588 value_address (base_val
),
594 cp_print_value_fields (baseclass
, thistype
,
595 thisoffset
+ boffset
,
596 value_address (base_val
),
597 stream
, recurse
, base_val
, options
,
599 obstack_base (&dont_print_vb_obstack
)),
602 fputs_filtered (", ", stream
);
608 if (dont_print_vb
== 0)
610 /* Free the space used to deal with the printing
611 of this type from top level. */
612 obstack_free (&dont_print_vb_obstack
, last_dont_print
);
613 /* Reset watermark so that we can continue protecting
614 ourselves from whatever we were protecting ourselves. */
615 dont_print_vb_obstack
= tmp_obstack
;
619 /* Print value of a static member. To avoid infinite recursion when
620 printing a class that contains a static instance of the class, we
621 keep the addresses of all printed static member classes in an
622 obstack and refuse to print them more than once.
624 VAL contains the value to print, TYPE, STREAM, RECURSE, and OPTIONS
625 have the same meanings as in c_val_print. */
628 cp_print_static_field (struct type
*type
,
630 struct ui_file
*stream
,
632 const struct value_print_options
*options
)
634 struct value_print_options opts
;
636 if (value_entirely_optimized_out (val
))
638 val_print_optimized_out (val
, stream
);
642 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
644 CORE_ADDR
*first_dont_print
;
649 = (CORE_ADDR
*) obstack_base (&dont_print_statmem_obstack
);
650 i
= obstack_object_size (&dont_print_statmem_obstack
)
651 / sizeof (CORE_ADDR
);
655 if (value_address (val
) == first_dont_print
[i
])
657 fputs_filtered ("<same as static member of an already"
664 addr
= value_address (val
);
665 obstack_grow (&dont_print_statmem_obstack
, (char *) &addr
,
667 type
= check_typedef (type
);
668 cp_print_value_fields (type
, value_enclosing_type (val
),
669 value_embedded_offset (val
), addr
,
670 stream
, recurse
, val
,
675 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
)
677 struct type
**first_dont_print
;
679 struct type
*target_type
= TYPE_TARGET_TYPE (type
);
682 = (struct type
**) obstack_base (&dont_print_stat_array_obstack
);
683 i
= obstack_object_size (&dont_print_stat_array_obstack
)
684 / sizeof (struct type
*);
688 if (target_type
== first_dont_print
[i
])
690 fputs_filtered ("<same as static member of an already"
697 obstack_grow (&dont_print_stat_array_obstack
,
698 (char *) &target_type
,
699 sizeof (struct type
*));
705 value_embedded_offset (val
),
707 stream
, recurse
, val
,
708 &opts
, current_language
);
711 /* Find the field in *SELF, or its non-virtual base classes, with
712 bit offset OFFSET. Set *SELF to the containing type and *FIELDNO
713 to the containing field number. If OFFSET is not exactly at the
714 start of some field, set *SELF to NULL. */
717 cp_find_class_member (struct type
**self_p
, int *fieldno
,
724 *self_p
= check_typedef (*self_p
);
726 len
= TYPE_NFIELDS (self
);
728 for (i
= TYPE_N_BASECLASSES (self
); i
< len
; i
++)
730 LONGEST bitpos
= TYPE_FIELD_BITPOS (self
, i
);
733 if (offset
== bitpos
)
740 for (i
= 0; i
< TYPE_N_BASECLASSES (self
); i
++)
742 LONGEST bitpos
= TYPE_FIELD_BITPOS (self
, i
);
743 LONGEST bitsize
= 8 * TYPE_LENGTH (TYPE_FIELD_TYPE (self
, i
));
745 if (offset
>= bitpos
&& offset
< bitpos
+ bitsize
)
747 *self_p
= TYPE_FIELD_TYPE (self
, i
);
748 cp_find_class_member (self_p
, fieldno
, offset
- bitpos
);
757 cp_print_class_member (const gdb_byte
*valaddr
, struct type
*type
,
758 struct ui_file
*stream
, const char *prefix
)
760 enum bfd_endian byte_order
= gdbarch_byte_order (get_type_arch (type
));
762 /* VAL is a byte offset into the structure type SELF_TYPE.
763 Find the name of the field for that offset and
765 struct type
*self_type
= TYPE_SELF_TYPE (type
);
769 val
= extract_signed_integer (valaddr
,
773 /* Pointers to data members are usually byte offsets into an object.
774 Because a data member can have offset zero, and a NULL pointer to
775 member must be distinct from any valid non-NULL pointer to
776 member, either the value is biased or the NULL value has a
777 special representation; both are permitted by ISO C++. HP aCC
778 used a bias of 0x20000000; HP cfront used a bias of 1; g++ 3.x
779 and other compilers which use the Itanium ABI use -1 as the NULL
780 value. GDB only supports that last form; to add support for
781 another form, make this into a cp-abi hook. */
785 fprintf_filtered (stream
, "NULL");
789 cp_find_class_member (&self_type
, &fieldno
, val
<< 3);
791 if (self_type
!= NULL
)
795 fputs_filtered (prefix
, stream
);
796 name
= type_name_no_tag (self_type
);
798 fputs_filtered (name
, stream
);
800 c_type_print_base (self_type
, stream
, 0, 0, &type_print_raw_options
);
801 fprintf_filtered (stream
, "::");
802 fputs_filtered (TYPE_FIELD_NAME (self_type
, fieldno
), stream
);
805 fprintf_filtered (stream
, "%ld", (long) val
);
810 _initialize_cp_valprint (void)
812 add_setshow_boolean_cmd ("static-members", class_support
,
813 &user_print_options
.static_field_print
, _("\
814 Set printing of C++ static members."), _("\
815 Show printing of C++ static members."), NULL
,
817 show_static_field_print
,
818 &setprintlist
, &showprintlist
);
820 add_setshow_boolean_cmd ("vtbl", class_support
,
821 &user_print_options
.vtblprint
, _("\
822 Set printing of C++ virtual function tables."), _("\
823 Show printing of C++ virtual function tables."), NULL
,
826 &setprintlist
, &showprintlist
);
828 add_setshow_boolean_cmd ("object", class_support
,
829 &user_print_options
.objectprint
, _("\
830 Set printing of object's derived type based on vtable info."), _("\
831 Show printing of object's derived type based on vtable info."), NULL
,
834 &setprintlist
, &showprintlist
);
836 obstack_begin (&dont_print_stat_array_obstack
,
837 32 * sizeof (struct type
*));
838 obstack_begin (&dont_print_statmem_obstack
,
839 32 * sizeof (CORE_ADDR
));
840 obstack_begin (&dont_print_vb_obstack
,
841 32 * sizeof (struct type
*));