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 type
*, struct type
*,
52 CORE_ADDR
, struct ui_file
*,
54 const struct value_print_options
*,
57 static void cp_print_value (struct value
*, struct ui_file
*,
58 int, const struct value_print_options
*,
62 /* GCC versions after 2.4.5 use this. */
63 const char vtbl_ptr_name
[] = "__vtbl_ptr_type";
65 /* Return truth value for assertion that TYPE is of the type
66 "pointer to virtual function". */
69 cp_is_vtbl_ptr_type (struct type
*type
)
71 const char *type_name
= TYPE_NAME (type
);
73 return (type_name
!= NULL
&& !strcmp (type_name
, vtbl_ptr_name
));
76 /* Return truth value for the assertion that TYPE is of the type
77 "pointer to virtual function table". */
80 cp_is_vtbl_member (struct type
*type
)
82 /* With older versions of g++, the vtbl field pointed to an array of
83 structures. Nowadays it points directly to the structure. */
84 if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
86 type
= TYPE_TARGET_TYPE (type
);
87 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
)
89 type
= TYPE_TARGET_TYPE (type
);
90 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
/* if not using thunks */
91 || TYPE_CODE (type
) == TYPE_CODE_PTR
) /* if using thunks */
93 /* Virtual functions tables are full of pointers
94 to virtual functions. */
95 return cp_is_vtbl_ptr_type (type
);
98 else if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
) /* if not using thunks */
100 return cp_is_vtbl_ptr_type (type
);
102 else if (TYPE_CODE (type
) == TYPE_CODE_PTR
) /* if using thunks */
104 /* The type name of the thunk pointer is NULL when using
105 dwarf2. We could test for a pointer to a function, but
106 there is no type info for the virtual table either, so it
108 return cp_is_vtbl_ptr_type (type
);
114 /* Mutually recursive subroutines of cp_print_value and c_val_print to
115 print out a structure's fields: cp_print_value_fields and
118 TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and OPTIONS have the same
119 meanings as in cp_print_value and c_val_print.
121 2nd argument REAL_TYPE is used to carry over the type of the
122 derived class across the recursion to base classes.
124 DONT_PRINT is an array of baseclass types that we should not print,
125 or zero if called from top level. */
128 cp_print_value_fields (struct type
*type
, struct type
*real_type
,
130 CORE_ADDR address
, struct ui_file
*stream
,
131 int recurse
, struct value
*val
,
132 const struct value_print_options
*options
,
133 struct type
**dont_print_vb
,
134 int dont_print_statmem
)
136 int i
, len
, n_baseclasses
;
138 static int last_set_recurse
= -1;
140 type
= check_typedef (type
);
144 /* Any object can be left on obstacks only during an unexpected
147 if (obstack_object_size (&dont_print_statmem_obstack
) > 0)
149 obstack_free (&dont_print_statmem_obstack
, NULL
);
150 obstack_begin (&dont_print_statmem_obstack
,
151 32 * sizeof (CORE_ADDR
));
153 if (obstack_object_size (&dont_print_stat_array_obstack
) > 0)
155 obstack_free (&dont_print_stat_array_obstack
, NULL
);
156 obstack_begin (&dont_print_stat_array_obstack
,
157 32 * sizeof (struct type
*));
161 fprintf_filtered (stream
, "{");
162 len
= TYPE_NFIELDS (type
);
163 n_baseclasses
= TYPE_N_BASECLASSES (type
);
165 /* First, print out baseclasses such that we don't print
166 duplicates of virtual baseclasses. */
168 if (n_baseclasses
> 0)
169 cp_print_value (type
, real_type
,
170 offset
, address
, stream
,
171 recurse
+ 1, val
, options
,
174 /* Second, print out data fields */
176 /* If there are no data fields, skip this part */
177 if (len
== n_baseclasses
|| !len
)
178 fprintf_styled (stream
, metadata_style
.style (), "<No data fields>");
181 size_t statmem_obstack_initial_size
= 0;
182 size_t stat_array_obstack_initial_size
= 0;
183 struct type
*vptr_basetype
= NULL
;
186 if (dont_print_statmem
== 0)
188 statmem_obstack_initial_size
=
189 obstack_object_size (&dont_print_statmem_obstack
);
191 if (last_set_recurse
!= recurse
)
193 stat_array_obstack_initial_size
=
194 obstack_object_size (&dont_print_stat_array_obstack
);
196 last_set_recurse
= recurse
;
200 vptr_fieldno
= get_vptr_fieldno (type
, &vptr_basetype
);
201 for (i
= n_baseclasses
; i
< len
; i
++)
203 const gdb_byte
*valaddr
= value_contents_for_printing (val
);
205 /* If requested, skip printing of static fields. */
206 if (!options
->static_field_print
207 && field_is_static (&TYPE_FIELD (type
, i
)))
212 fputs_filtered (",", stream
);
213 if (!options
->prettyformat
)
214 fputs_filtered (" ", stream
);
216 else if (n_baseclasses
> 0)
218 if (options
->prettyformat
)
220 fprintf_filtered (stream
, "\n");
221 print_spaces_filtered (2 + 2 * recurse
, stream
);
222 fputs_filtered ("members of ", stream
);
223 fputs_filtered (TYPE_NAME (type
), stream
);
224 fputs_filtered (":", stream
);
229 if (options
->prettyformat
)
231 fprintf_filtered (stream
, "\n");
232 print_spaces_filtered (2 + 2 * recurse
, stream
);
236 wrap_here (n_spaces (2 + 2 * recurse
));
239 annotate_field_begin (TYPE_FIELD_TYPE (type
, i
));
241 if (field_is_static (&TYPE_FIELD (type
, i
)))
243 fputs_filtered ("static ", stream
);
244 fprintf_symbol_filtered (stream
,
245 TYPE_FIELD_NAME (type
, i
),
246 current_language
->la_language
,
247 DMGL_PARAMS
| DMGL_ANSI
);
250 fputs_styled (TYPE_FIELD_NAME (type
, i
),
251 variable_name_style
.style (), stream
);
252 annotate_field_name_end ();
254 /* We tweak various options in a few cases below. */
255 value_print_options options_copy
= *options
;
256 value_print_options
*opts
= &options_copy
;
258 /* Do not print leading '=' in case of anonymous
260 if (strcmp (TYPE_FIELD_NAME (type
, i
), ""))
261 fputs_filtered (" = ", stream
);
264 /* If this is an anonymous field then we want to consider it
265 as though it is at its parent's depth when it comes to the
267 if (opts
->max_depth
!= -1 && opts
->max_depth
< INT_MAX
)
270 annotate_field_value ();
272 if (!field_is_static (&TYPE_FIELD (type
, i
))
273 && TYPE_FIELD_PACKED (type
, i
))
277 /* Bitfields require special handling, especially due to
278 byte order problems. */
279 if (TYPE_FIELD_IGNORE (type
, i
))
281 fputs_styled ("<optimized out or zero length>",
282 metadata_style
.style (), stream
);
284 else if (value_bits_synthetic_pointer (val
,
285 TYPE_FIELD_BITPOS (type
,
287 TYPE_FIELD_BITSIZE (type
,
290 fputs_styled (_("<synthetic pointer>"),
291 metadata_style
.style (), stream
);
297 v
= value_field_bitfield (type
, i
, valaddr
, offset
, val
);
299 common_val_print (v
, stream
, recurse
+ 1,
300 opts
, current_language
);
305 if (TYPE_FIELD_IGNORE (type
, i
))
307 fputs_styled ("<optimized out or zero length>",
308 metadata_style
.style (), stream
);
310 else if (field_is_static (&TYPE_FIELD (type
, i
)))
314 struct value
*v
= value_static_field (type
, i
);
316 cp_print_static_field (TYPE_FIELD_TYPE (type
, i
),
317 v
, stream
, recurse
+ 1,
320 catch (const gdb_exception_error
&ex
)
322 fprintf_styled (stream
, metadata_style
.style (),
323 _("<error reading variable: %s>"),
327 else if (i
== vptr_fieldno
&& type
== vptr_basetype
)
329 int i_offset
= offset
+ TYPE_FIELD_BITPOS (type
, i
) / 8;
330 struct type
*i_type
= TYPE_FIELD_TYPE (type
, i
);
332 if (valprint_check_validity (stream
, i_type
, i_offset
, val
))
336 i_offset
+= value_embedded_offset (val
);
337 addr
= extract_typed_address (valaddr
+ i_offset
, i_type
);
338 print_function_pointer_address (opts
,
339 get_type_arch (type
),
346 val_print (TYPE_FIELD_TYPE (type
, i
),
347 offset
+ TYPE_FIELD_BITPOS (type
, i
) / 8,
349 stream
, recurse
+ 1, val
, opts
,
353 annotate_field_end ();
356 if (dont_print_statmem
== 0)
358 size_t obstack_final_size
=
359 obstack_object_size (&dont_print_statmem_obstack
);
361 if (obstack_final_size
> statmem_obstack_initial_size
)
363 /* In effect, a pop of the printed-statics stack. */
365 = statmem_obstack_initial_size
- obstack_final_size
;
366 obstack_blank_fast (&dont_print_statmem_obstack
, shrink_bytes
);
369 if (last_set_recurse
!= recurse
)
372 obstack_object_size (&dont_print_stat_array_obstack
);
374 if (obstack_final_size
> stat_array_obstack_initial_size
)
377 (char *) obstack_next_free (&dont_print_stat_array_obstack
)
378 - (obstack_final_size
379 - stat_array_obstack_initial_size
);
381 obstack_free (&dont_print_stat_array_obstack
,
384 last_set_recurse
= -1;
388 if (options
->prettyformat
)
390 fprintf_filtered (stream
, "\n");
391 print_spaces_filtered (2 * recurse
, stream
);
393 } /* if there are data fields */
395 fprintf_filtered (stream
, "}");
398 /* Mutually recursive subroutines of cp_print_value and c_value_print
399 to print out a structure's fields: cp_print_value_fields and
402 VAL, ADDRESS, STREAM, RECURSE, and OPTIONS have the same meanings
403 as in cp_print_value and c_value_print.
405 DONT_PRINT is an array of baseclass types that we should not print,
406 or zero if called from top level. */
409 cp_print_value_fields (struct value
*val
, struct ui_file
*stream
,
410 int recurse
, const struct value_print_options
*options
,
411 struct type
**dont_print_vb
,
412 int dont_print_statmem
)
414 int i
, len
, n_baseclasses
;
416 static int last_set_recurse
= -1;
418 struct type
*type
= check_typedef (value_type (val
));
422 /* Any object can be left on obstacks only during an unexpected
425 if (obstack_object_size (&dont_print_statmem_obstack
) > 0)
427 obstack_free (&dont_print_statmem_obstack
, NULL
);
428 obstack_begin (&dont_print_statmem_obstack
,
429 32 * sizeof (CORE_ADDR
));
431 if (obstack_object_size (&dont_print_stat_array_obstack
) > 0)
433 obstack_free (&dont_print_stat_array_obstack
, NULL
);
434 obstack_begin (&dont_print_stat_array_obstack
,
435 32 * sizeof (struct type
*));
439 fprintf_filtered (stream
, "{");
440 len
= TYPE_NFIELDS (type
);
441 n_baseclasses
= TYPE_N_BASECLASSES (type
);
443 /* First, print out baseclasses such that we don't print
444 duplicates of virtual baseclasses. */
446 if (n_baseclasses
> 0)
447 cp_print_value (val
, stream
, recurse
+ 1, options
, dont_print_vb
);
449 /* Second, print out data fields */
451 /* If there are no data fields, skip this part */
452 if (len
== n_baseclasses
|| !len
)
453 fprintf_styled (stream
, metadata_style
.style (), "<No data fields>");
456 size_t statmem_obstack_initial_size
= 0;
457 size_t stat_array_obstack_initial_size
= 0;
458 struct type
*vptr_basetype
= NULL
;
461 if (dont_print_statmem
== 0)
463 statmem_obstack_initial_size
=
464 obstack_object_size (&dont_print_statmem_obstack
);
466 if (last_set_recurse
!= recurse
)
468 stat_array_obstack_initial_size
=
469 obstack_object_size (&dont_print_stat_array_obstack
);
471 last_set_recurse
= recurse
;
475 vptr_fieldno
= get_vptr_fieldno (type
, &vptr_basetype
);
476 for (i
= n_baseclasses
; i
< len
; i
++)
478 const gdb_byte
*valaddr
= value_contents_for_printing (val
);
480 /* If requested, skip printing of static fields. */
481 if (!options
->static_field_print
482 && field_is_static (&TYPE_FIELD (type
, i
)))
487 fputs_filtered (",", stream
);
488 if (!options
->prettyformat
)
489 fputs_filtered (" ", stream
);
491 else if (n_baseclasses
> 0)
493 if (options
->prettyformat
)
495 fprintf_filtered (stream
, "\n");
496 print_spaces_filtered (2 + 2 * recurse
, stream
);
497 fputs_filtered ("members of ", stream
);
498 fputs_filtered (TYPE_NAME (type
), stream
);
499 fputs_filtered (":", stream
);
504 if (options
->prettyformat
)
506 fprintf_filtered (stream
, "\n");
507 print_spaces_filtered (2 + 2 * recurse
, stream
);
511 wrap_here (n_spaces (2 + 2 * recurse
));
514 annotate_field_begin (TYPE_FIELD_TYPE (type
, i
));
516 if (field_is_static (&TYPE_FIELD (type
, i
)))
518 fputs_filtered ("static ", stream
);
519 fprintf_symbol_filtered (stream
,
520 TYPE_FIELD_NAME (type
, i
),
521 current_language
->la_language
,
522 DMGL_PARAMS
| DMGL_ANSI
);
525 fputs_styled (TYPE_FIELD_NAME (type
, i
),
526 variable_name_style
.style (), stream
);
527 annotate_field_name_end ();
529 /* We tweak various options in a few cases below. */
530 value_print_options options_copy
= *options
;
531 value_print_options
*opts
= &options_copy
;
533 /* Do not print leading '=' in case of anonymous
535 if (strcmp (TYPE_FIELD_NAME (type
, i
), ""))
536 fputs_filtered (" = ", stream
);
539 /* If this is an anonymous field then we want to consider it
540 as though it is at its parent's depth when it comes to the
542 if (opts
->max_depth
!= -1 && opts
->max_depth
< INT_MAX
)
545 annotate_field_value ();
547 if (!field_is_static (&TYPE_FIELD (type
, i
))
548 && TYPE_FIELD_PACKED (type
, i
))
552 /* Bitfields require special handling, especially due to
553 byte order problems. */
554 if (TYPE_FIELD_IGNORE (type
, i
))
556 fputs_styled ("<optimized out or zero length>",
557 metadata_style
.style (), stream
);
559 else if (value_bits_synthetic_pointer (val
,
560 TYPE_FIELD_BITPOS (type
,
562 TYPE_FIELD_BITSIZE (type
,
565 fputs_styled (_("<synthetic pointer>"),
566 metadata_style
.style (), stream
);
572 v
= value_field_bitfield (type
, i
, valaddr
,
573 value_embedded_offset (val
), val
);
575 common_val_print (v
, stream
, recurse
+ 1,
576 opts
, current_language
);
581 if (TYPE_FIELD_IGNORE (type
, i
))
583 fputs_styled ("<optimized out or zero length>",
584 metadata_style
.style (), stream
);
586 else if (field_is_static (&TYPE_FIELD (type
, i
)))
590 struct value
*v
= value_static_field (type
, i
);
592 cp_print_static_field (TYPE_FIELD_TYPE (type
, i
),
593 v
, stream
, recurse
+ 1,
596 catch (const gdb_exception_error
&ex
)
598 fprintf_styled (stream
, metadata_style
.style (),
599 _("<error reading variable: %s>"),
603 else if (i
== vptr_fieldno
&& type
== vptr_basetype
)
605 int i_offset
= TYPE_FIELD_BITPOS (type
, i
) / 8;
606 struct type
*i_type
= TYPE_FIELD_TYPE (type
, i
);
608 if (valprint_check_validity (stream
, i_type
, i_offset
, val
))
612 i_offset
+= value_embedded_offset (val
);
613 addr
= extract_typed_address (valaddr
+ i_offset
, i_type
);
614 print_function_pointer_address (opts
,
615 get_type_arch (type
),
621 struct value
*v
= value_primitive_field (val
, 0, i
, type
);
623 common_val_print (v
, stream
, recurse
+ 1, opts
,
627 annotate_field_end ();
630 if (dont_print_statmem
== 0)
632 size_t obstack_final_size
=
633 obstack_object_size (&dont_print_statmem_obstack
);
635 if (obstack_final_size
> statmem_obstack_initial_size
)
637 /* In effect, a pop of the printed-statics stack. */
639 = statmem_obstack_initial_size
- obstack_final_size
;
640 obstack_blank_fast (&dont_print_statmem_obstack
, shrink_bytes
);
643 if (last_set_recurse
!= recurse
)
646 obstack_object_size (&dont_print_stat_array_obstack
);
648 if (obstack_final_size
> stat_array_obstack_initial_size
)
651 (char *) obstack_next_free (&dont_print_stat_array_obstack
)
652 - (obstack_final_size
653 - stat_array_obstack_initial_size
);
655 obstack_free (&dont_print_stat_array_obstack
,
658 last_set_recurse
= -1;
662 if (options
->prettyformat
)
664 fprintf_filtered (stream
, "\n");
665 print_spaces_filtered (2 * recurse
, stream
);
667 } /* if there are data fields */
669 fprintf_filtered (stream
, "}");
672 /* Like cp_print_value_fields, but find the runtime type of the object
673 and pass it as the `real_type' argument to cp_print_value_fields.
674 This function is a hack to work around the fact that
675 common_val_print passes the embedded offset to val_print, but not
676 the enclosing type. */
679 cp_print_value_fields_rtti (struct type
*type
,
680 const gdb_byte
*valaddr
, LONGEST offset
,
682 struct ui_file
*stream
, int recurse
,
684 const struct value_print_options
*options
,
685 struct type
**dont_print_vb
,
686 int dont_print_statmem
)
688 struct type
*real_type
= NULL
;
690 /* We require all bits to be valid in order to attempt a
692 if (!value_bits_any_optimized_out (val
,
693 TARGET_CHAR_BIT
* offset
,
694 TARGET_CHAR_BIT
* TYPE_LENGTH (type
)))
700 /* Ugh, we have to convert back to a value here. */
701 value
= value_from_contents_and_address (type
, valaddr
+ offset
,
703 type
= value_type (value
);
704 /* We don't actually care about most of the result here -- just
705 the type. We already have the correct offset, due to how
706 val_print was initially called. */
707 real_type
= value_rtti_type (value
, &full
, &top
, &using_enc
);
713 cp_print_value_fields (type
, real_type
, offset
,
714 address
, stream
, recurse
, val
, options
,
715 dont_print_vb
, dont_print_statmem
);
718 /* Special value_print routine to avoid printing multiple copies of
719 virtual baseclasses. */
722 cp_print_value (struct type
*type
, struct type
*real_type
,
724 CORE_ADDR address
, struct ui_file
*stream
,
725 int recurse
, struct value
*val
,
726 const struct value_print_options
*options
,
727 struct type
**dont_print_vb
)
729 struct type
**last_dont_print
730 = (struct type
**) obstack_next_free (&dont_print_vb_obstack
);
731 struct obstack tmp_obstack
= dont_print_vb_obstack
;
732 int i
, n_baseclasses
= TYPE_N_BASECLASSES (type
);
734 struct type
*thistype
;
735 const gdb_byte
*valaddr
= value_contents_for_printing (val
);
737 if (dont_print_vb
== 0)
739 /* If we're at top level, carve out a completely fresh chunk of
740 the obstack and use that until this particular invocation
742 /* Bump up the high-water mark. Now alpha is omega. */
743 obstack_finish (&dont_print_vb_obstack
);
746 for (i
= 0; i
< n_baseclasses
; i
++)
750 struct type
*baseclass
= check_typedef (TYPE_BASECLASS (type
, i
));
751 const char *basename
= TYPE_NAME (baseclass
);
752 struct value
*base_val
= NULL
;
754 if (BASETYPE_VIA_VIRTUAL (type
, i
))
756 struct type
**first_dont_print
757 = (struct type
**) obstack_base (&dont_print_vb_obstack
);
759 int j
= (struct type
**)
760 obstack_next_free (&dont_print_vb_obstack
) - first_dont_print
;
763 if (baseclass
== first_dont_print
[j
])
766 obstack_ptr_grow (&dont_print_vb_obstack
, baseclass
);
770 thistype
= real_type
;
774 boffset
= baseclass_offset (type
, i
, valaddr
, offset
, address
, val
);
776 catch (const gdb_exception_error
&ex
)
778 if (ex
.error
== NOT_AVAILABLE_ERROR
)
786 if (BASETYPE_VIA_VIRTUAL (type
, i
))
788 /* The virtual base class pointer might have been
789 clobbered by the user program. Make sure that it
790 still points to a valid memory location. */
792 if ((boffset
+ offset
) < 0
793 || (boffset
+ offset
) >= TYPE_LENGTH (real_type
))
795 gdb::byte_vector
buf (TYPE_LENGTH (baseclass
));
797 if (target_read_memory (address
+ boffset
, buf
.data (),
798 TYPE_LENGTH (baseclass
)) != 0)
800 base_val
= value_from_contents_and_address (baseclass
,
803 baseclass
= value_type (base_val
);
806 thistype
= baseclass
;
819 /* Now do the printing. */
820 if (options
->prettyformat
)
822 fprintf_filtered (stream
, "\n");
823 print_spaces_filtered (2 * recurse
, stream
);
825 fputs_filtered ("<", stream
);
826 /* Not sure what the best notation is in the case where there is
827 no baseclass name. */
828 fputs_filtered (basename
? basename
: "", stream
);
829 fputs_filtered ("> = ", stream
);
832 val_print_unavailable (stream
);
834 val_print_invalid_address (stream
);
839 if (options
->max_depth
> -1
840 && recurse
>= options
->max_depth
)
842 const struct language_defn
*language
= current_language
;
843 gdb_assert (language
->la_struct_too_deep_ellipsis
!= NULL
);
844 fputs_filtered (language
->la_struct_too_deep_ellipsis
, stream
);
848 /* Attempt to run an extension language pretty-printer on the
849 baseclass if possible. */
853 = value_from_component (base_val
, baseclass
,
854 thisoffset
+ boffset
);
856 = apply_ext_lang_val_pretty_printer (v
, stream
, recurse
,
862 cp_print_value_fields (baseclass
, thistype
,
863 thisoffset
+ boffset
,
864 value_address (base_val
),
865 stream
, recurse
, base_val
, options
,
867 obstack_base (&dont_print_vb_obstack
)),
871 fputs_filtered (", ", stream
);
877 if (dont_print_vb
== 0)
879 /* Free the space used to deal with the printing
880 of this type from top level. */
881 obstack_free (&dont_print_vb_obstack
, last_dont_print
);
882 /* Reset watermark so that we can continue protecting
883 ourselves from whatever we were protecting ourselves. */
884 dont_print_vb_obstack
= tmp_obstack
;
888 /* Special val_print routine to avoid printing multiple copies of
889 virtual baseclasses. */
892 cp_print_value (struct value
*val
, struct ui_file
*stream
,
893 int recurse
, const struct value_print_options
*options
,
894 struct type
**dont_print_vb
)
896 struct type
*type
= check_typedef (value_type (val
));
897 CORE_ADDR address
= value_address (val
);
898 struct type
**last_dont_print
899 = (struct type
**) obstack_next_free (&dont_print_vb_obstack
);
900 struct obstack tmp_obstack
= dont_print_vb_obstack
;
901 int i
, n_baseclasses
= TYPE_N_BASECLASSES (type
);
902 const gdb_byte
*valaddr
= value_contents_for_printing (val
);
904 if (dont_print_vb
== 0)
906 /* If we're at top level, carve out a completely fresh chunk of
907 the obstack and use that until this particular invocation
909 /* Bump up the high-water mark. Now alpha is omega. */
910 obstack_finish (&dont_print_vb_obstack
);
913 for (i
= 0; i
< n_baseclasses
; i
++)
917 struct type
*baseclass
= check_typedef (TYPE_BASECLASS (type
, i
));
918 const char *basename
= TYPE_NAME (baseclass
);
919 struct value
*base_val
= NULL
;
921 if (BASETYPE_VIA_VIRTUAL (type
, i
))
923 struct type
**first_dont_print
924 = (struct type
**) obstack_base (&dont_print_vb_obstack
);
926 int j
= (struct type
**)
927 obstack_next_free (&dont_print_vb_obstack
) - first_dont_print
;
930 if (baseclass
== first_dont_print
[j
])
933 obstack_ptr_grow (&dont_print_vb_obstack
, baseclass
);
938 boffset
= baseclass_offset (type
, i
, valaddr
,
939 value_embedded_offset (val
),
942 catch (const gdb_exception_error
&ex
)
944 if (ex
.error
== NOT_AVAILABLE_ERROR
)
952 if (BASETYPE_VIA_VIRTUAL (type
, i
))
954 /* The virtual base class pointer might have been
955 clobbered by the user program. Make sure that it
956 still points to a valid memory location. */
958 if (boffset
< 0 || boffset
>= TYPE_LENGTH (type
))
960 gdb::byte_vector
buf (TYPE_LENGTH (baseclass
));
962 if (target_read_memory (address
+ boffset
, buf
.data (),
963 TYPE_LENGTH (baseclass
)) != 0)
965 base_val
= value_from_contents_and_address (baseclass
,
968 baseclass
= value_type (base_val
);
982 /* Now do the printing. */
983 if (options
->prettyformat
)
985 fprintf_filtered (stream
, "\n");
986 print_spaces_filtered (2 * recurse
, stream
);
988 fputs_filtered ("<", stream
);
989 /* Not sure what the best notation is in the case where there is
990 no baseclass name. */
991 fputs_filtered (basename
? basename
: "", stream
);
992 fputs_filtered ("> = ", stream
);
995 val_print_unavailable (stream
);
997 val_print_invalid_address (stream
);
1002 if (options
->max_depth
> -1
1003 && recurse
>= options
->max_depth
)
1005 const struct language_defn
*language
= current_language
;
1006 gdb_assert (language
->la_struct_too_deep_ellipsis
!= NULL
);
1007 fputs_filtered (language
->la_struct_too_deep_ellipsis
, stream
);
1011 struct value
*baseclass_val
= value_primitive_field (val
, 0,
1014 /* Attempt to run an extension language pretty-printer on the
1015 baseclass if possible. */
1018 = apply_ext_lang_val_pretty_printer (baseclass_val
, stream
,
1023 cp_print_value_fields (baseclass_val
, stream
, recurse
, options
,
1025 obstack_base (&dont_print_vb_obstack
)),
1029 fputs_filtered (", ", stream
);
1035 if (dont_print_vb
== 0)
1037 /* Free the space used to deal with the printing
1038 of this type from top level. */
1039 obstack_free (&dont_print_vb_obstack
, last_dont_print
);
1040 /* Reset watermark so that we can continue protecting
1041 ourselves from whatever we were protecting ourselves. */
1042 dont_print_vb_obstack
= tmp_obstack
;
1046 /* Print value of a static member. To avoid infinite recursion when
1047 printing a class that contains a static instance of the class, we
1048 keep the addresses of all printed static member classes in an
1049 obstack and refuse to print them more than once.
1051 VAL contains the value to print, TYPE, STREAM, RECURSE, and OPTIONS
1052 have the same meanings as in c_val_print. */
1055 cp_print_static_field (struct type
*type
,
1057 struct ui_file
*stream
,
1059 const struct value_print_options
*options
)
1061 struct value_print_options opts
;
1063 if (value_entirely_optimized_out (val
))
1065 val_print_optimized_out (val
, stream
);
1069 struct type
*real_type
= check_typedef (type
);
1070 if (TYPE_CODE (real_type
) == TYPE_CODE_STRUCT
)
1072 CORE_ADDR
*first_dont_print
;
1077 = (CORE_ADDR
*) obstack_base (&dont_print_statmem_obstack
);
1078 i
= obstack_object_size (&dont_print_statmem_obstack
)
1079 / sizeof (CORE_ADDR
);
1083 if (value_address (val
) == first_dont_print
[i
])
1085 fputs_styled (_("<same as static member of an already"
1087 metadata_style
.style (), stream
);
1092 addr
= value_address (val
);
1093 obstack_grow (&dont_print_statmem_obstack
, (char *) &addr
,
1094 sizeof (CORE_ADDR
));
1095 cp_print_value_fields (type
, value_enclosing_type (val
),
1096 value_embedded_offset (val
), addr
,
1097 stream
, recurse
, val
,
1102 if (TYPE_CODE (real_type
) == TYPE_CODE_ARRAY
)
1104 struct type
**first_dont_print
;
1106 struct type
*target_type
= TYPE_TARGET_TYPE (type
);
1109 = (struct type
**) obstack_base (&dont_print_stat_array_obstack
);
1110 i
= obstack_object_size (&dont_print_stat_array_obstack
)
1111 / sizeof (struct type
*);
1115 if (target_type
== first_dont_print
[i
])
1117 fputs_styled (_("<same as static member of an already"
1119 metadata_style
.style (), stream
);
1124 obstack_grow (&dont_print_stat_array_obstack
,
1125 (char *) &target_type
,
1126 sizeof (struct type
*));
1131 common_val_print (val
, stream
, recurse
, &opts
, current_language
);
1134 /* Find the field in *SELF, or its non-virtual base classes, with
1135 bit offset OFFSET. Set *SELF to the containing type and *FIELDNO
1136 to the containing field number. If OFFSET is not exactly at the
1137 start of some field, set *SELF to NULL. */
1140 cp_find_class_member (struct type
**self_p
, int *fieldno
,
1147 *self_p
= check_typedef (*self_p
);
1149 len
= TYPE_NFIELDS (self
);
1151 for (i
= TYPE_N_BASECLASSES (self
); i
< len
; i
++)
1153 LONGEST bitpos
= TYPE_FIELD_BITPOS (self
, i
);
1156 if (offset
== bitpos
)
1163 for (i
= 0; i
< TYPE_N_BASECLASSES (self
); i
++)
1165 LONGEST bitpos
= TYPE_FIELD_BITPOS (self
, i
);
1166 LONGEST bitsize
= 8 * TYPE_LENGTH (TYPE_FIELD_TYPE (self
, i
));
1168 if (offset
>= bitpos
&& offset
< bitpos
+ bitsize
)
1170 *self_p
= TYPE_FIELD_TYPE (self
, i
);
1171 cp_find_class_member (self_p
, fieldno
, offset
- bitpos
);
1180 cp_print_class_member (const gdb_byte
*valaddr
, struct type
*type
,
1181 struct ui_file
*stream
, const char *prefix
)
1183 enum bfd_endian byte_order
= type_byte_order (type
);
1185 /* VAL is a byte offset into the structure type SELF_TYPE.
1186 Find the name of the field for that offset and
1188 struct type
*self_type
= TYPE_SELF_TYPE (type
);
1192 val
= extract_signed_integer (valaddr
,
1196 /* Pointers to data members are usually byte offsets into an object.
1197 Because a data member can have offset zero, and a NULL pointer to
1198 member must be distinct from any valid non-NULL pointer to
1199 member, either the value is biased or the NULL value has a
1200 special representation; both are permitted by ISO C++. HP aCC
1201 used a bias of 0x20000000; HP cfront used a bias of 1; g++ 3.x
1202 and other compilers which use the Itanium ABI use -1 as the NULL
1203 value. GDB only supports that last form; to add support for
1204 another form, make this into a cp-abi hook. */
1208 fprintf_filtered (stream
, "NULL");
1212 cp_find_class_member (&self_type
, &fieldno
, val
<< 3);
1214 if (self_type
!= NULL
)
1218 fputs_filtered (prefix
, stream
);
1219 name
= TYPE_NAME (self_type
);
1221 fputs_filtered (name
, stream
);
1223 c_type_print_base (self_type
, stream
, 0, 0, &type_print_raw_options
);
1224 fprintf_filtered (stream
, "::");
1225 fputs_styled (TYPE_FIELD_NAME (self_type
, fieldno
),
1226 variable_name_style
.style (), stream
);
1229 fprintf_filtered (stream
, "%ld", (long) val
);
1233 void _initialize_cp_valprint ();
1235 _initialize_cp_valprint ()
1237 obstack_begin (&dont_print_stat_array_obstack
,
1238 32 * sizeof (struct type
*));
1239 obstack_begin (&dont_print_statmem_obstack
,
1240 32 * sizeof (CORE_ADDR
));
1241 obstack_begin (&dont_print_vb_obstack
,
1242 32 * sizeof (struct type
*));