1 /* Support for printing C++ values for GDB, the GNU debugger.
3 Copyright (C) 1986-2019 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
*,
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 (type
);
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
) == TYPE_CODE_PTR
)
82 type
= TYPE_TARGET_TYPE (type
);
83 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
)
85 type
= TYPE_TARGET_TYPE (type
);
86 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
/* if not using thunks */
87 || TYPE_CODE (type
) == 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
) == TYPE_CODE_STRUCT
) /* if not using thunks */
96 return cp_is_vtbl_ptr_type (type
);
98 else if (TYPE_CODE (type
) == 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 type
*type
, struct type
*real_type
,
126 CORE_ADDR address
, struct ui_file
*stream
,
127 int recurse
, struct value
*val
,
128 const struct value_print_options
*options
,
129 struct type
**dont_print_vb
,
130 int dont_print_statmem
)
132 int i
, len
, n_baseclasses
;
134 static int last_set_recurse
= -1;
136 type
= check_typedef (type
);
140 /* Any object can be left on obstacks only during an unexpected
143 if (obstack_object_size (&dont_print_statmem_obstack
) > 0)
145 obstack_free (&dont_print_statmem_obstack
, NULL
);
146 obstack_begin (&dont_print_statmem_obstack
,
147 32 * sizeof (CORE_ADDR
));
149 if (obstack_object_size (&dont_print_stat_array_obstack
) > 0)
151 obstack_free (&dont_print_stat_array_obstack
, NULL
);
152 obstack_begin (&dont_print_stat_array_obstack
,
153 32 * sizeof (struct type
*));
157 fprintf_filtered (stream
, "{");
158 len
= TYPE_NFIELDS (type
);
159 n_baseclasses
= TYPE_N_BASECLASSES (type
);
161 /* First, print out baseclasses such that we don't print
162 duplicates of virtual baseclasses. */
164 if (n_baseclasses
> 0)
165 cp_print_value (type
, real_type
,
166 offset
, address
, stream
,
167 recurse
+ 1, val
, options
,
170 /* Second, print out data fields */
172 /* If there are no data fields, skip this part */
173 if (len
== n_baseclasses
|| !len
)
174 fprintf_styled (stream
, metadata_style
.style (), "<No data fields>");
177 size_t statmem_obstack_initial_size
= 0;
178 size_t stat_array_obstack_initial_size
= 0;
179 struct type
*vptr_basetype
= NULL
;
182 if (dont_print_statmem
== 0)
184 statmem_obstack_initial_size
=
185 obstack_object_size (&dont_print_statmem_obstack
);
187 if (last_set_recurse
!= recurse
)
189 stat_array_obstack_initial_size
=
190 obstack_object_size (&dont_print_stat_array_obstack
);
192 last_set_recurse
= recurse
;
196 vptr_fieldno
= get_vptr_fieldno (type
, &vptr_basetype
);
197 for (i
= n_baseclasses
; i
< len
; i
++)
199 const gdb_byte
*valaddr
= value_contents_for_printing (val
);
201 /* If requested, skip printing of static fields. */
202 if (!options
->static_field_print
203 && field_is_static (&TYPE_FIELD (type
, i
)))
208 fputs_filtered (",", stream
);
209 if (!options
->prettyformat
)
210 fputs_filtered (" ", stream
);
212 else if (n_baseclasses
> 0)
214 if (options
->prettyformat
)
216 fprintf_filtered (stream
, "\n");
217 print_spaces_filtered (2 + 2 * recurse
, stream
);
218 fputs_filtered ("members of ", stream
);
219 fputs_filtered (TYPE_NAME (type
), stream
);
220 fputs_filtered (":", stream
);
225 if (options
->prettyformat
)
227 fprintf_filtered (stream
, "\n");
228 print_spaces_filtered (2 + 2 * recurse
, stream
);
232 wrap_here (n_spaces (2 + 2 * recurse
));
235 annotate_field_begin (TYPE_FIELD_TYPE (type
, i
));
237 if (field_is_static (&TYPE_FIELD (type
, i
)))
238 fputs_filtered ("static ", stream
);
239 fprintf_symbol_filtered (stream
,
240 TYPE_FIELD_NAME (type
, i
),
241 current_language
->la_language
,
242 DMGL_PARAMS
| DMGL_ANSI
);
243 annotate_field_name_end ();
245 /* We tweak various options in a few cases below. */
246 value_print_options options_copy
= *options
;
247 value_print_options
*opts
= &options_copy
;
249 /* Do not print leading '=' in case of anonymous
251 if (strcmp (TYPE_FIELD_NAME (type
, i
), ""))
252 fputs_filtered (" = ", stream
);
255 /* If this is an anonymous field then we want to consider it
256 as though it is at its parent's depth when it comes to the
258 if (opts
->max_depth
!= -1 && opts
->max_depth
< INT_MAX
)
261 annotate_field_value ();
263 if (!field_is_static (&TYPE_FIELD (type
, i
))
264 && TYPE_FIELD_PACKED (type
, i
))
268 /* Bitfields require special handling, especially due to
269 byte order problems. */
270 if (TYPE_FIELD_IGNORE (type
, i
))
272 fputs_styled ("<optimized out or zero length>",
273 metadata_style
.style (), stream
);
275 else if (value_bits_synthetic_pointer (val
,
276 TYPE_FIELD_BITPOS (type
,
278 TYPE_FIELD_BITSIZE (type
,
281 fputs_styled (_("<synthetic pointer>"),
282 metadata_style
.style (), stream
);
288 v
= value_field_bitfield (type
, i
, valaddr
, offset
, val
);
290 common_val_print (v
, stream
, recurse
+ 1,
291 opts
, current_language
);
296 if (TYPE_FIELD_IGNORE (type
, i
))
298 fputs_styled ("<optimized out or zero length>",
299 metadata_style
.style (), stream
);
301 else if (field_is_static (&TYPE_FIELD (type
, i
)))
305 struct value
*v
= value_static_field (type
, i
);
307 cp_print_static_field (TYPE_FIELD_TYPE (type
, i
),
308 v
, stream
, recurse
+ 1,
311 catch (const gdb_exception_error
&ex
)
313 fprintf_styled (stream
, metadata_style
.style (),
314 _("<error reading variable: %s>"),
318 else if (i
== vptr_fieldno
&& type
== vptr_basetype
)
320 int i_offset
= offset
+ TYPE_FIELD_BITPOS (type
, i
) / 8;
321 struct type
*i_type
= TYPE_FIELD_TYPE (type
, i
);
323 if (valprint_check_validity (stream
, i_type
, i_offset
, val
))
327 addr
= extract_typed_address (valaddr
+ i_offset
, i_type
);
328 print_function_pointer_address (opts
,
329 get_type_arch (type
),
336 val_print (TYPE_FIELD_TYPE (type
, i
),
337 offset
+ TYPE_FIELD_BITPOS (type
, i
) / 8,
339 stream
, recurse
+ 1, val
, opts
,
343 annotate_field_end ();
346 if (dont_print_statmem
== 0)
348 size_t obstack_final_size
=
349 obstack_object_size (&dont_print_statmem_obstack
);
351 if (obstack_final_size
> statmem_obstack_initial_size
)
353 /* In effect, a pop of the printed-statics stack. */
355 = statmem_obstack_initial_size
- obstack_final_size
;
356 obstack_blank_fast (&dont_print_statmem_obstack
, shrink_bytes
);
359 if (last_set_recurse
!= recurse
)
362 obstack_object_size (&dont_print_stat_array_obstack
);
364 if (obstack_final_size
> stat_array_obstack_initial_size
)
367 (char *) obstack_next_free (&dont_print_stat_array_obstack
)
368 - (obstack_final_size
369 - stat_array_obstack_initial_size
);
371 obstack_free (&dont_print_stat_array_obstack
,
374 last_set_recurse
= -1;
378 if (options
->prettyformat
)
380 fprintf_filtered (stream
, "\n");
381 print_spaces_filtered (2 * recurse
, stream
);
383 } /* if there are data fields */
385 fprintf_filtered (stream
, "}");
388 /* Like cp_print_value_fields, but find the runtime type of the object
389 and pass it as the `real_type' argument to cp_print_value_fields.
390 This function is a hack to work around the fact that
391 common_val_print passes the embedded offset to val_print, but not
392 the enclosing type. */
395 cp_print_value_fields_rtti (struct type
*type
,
396 const gdb_byte
*valaddr
, LONGEST offset
,
398 struct ui_file
*stream
, int recurse
,
400 const struct value_print_options
*options
,
401 struct type
**dont_print_vb
,
402 int dont_print_statmem
)
404 struct type
*real_type
= NULL
;
406 /* We require all bits to be valid in order to attempt a
408 if (!value_bits_any_optimized_out (val
,
409 TARGET_CHAR_BIT
* offset
,
410 TARGET_CHAR_BIT
* TYPE_LENGTH (type
)))
416 /* Ugh, we have to convert back to a value here. */
417 value
= value_from_contents_and_address (type
, valaddr
+ offset
,
419 type
= value_type (value
);
420 /* We don't actually care about most of the result here -- just
421 the type. We already have the correct offset, due to how
422 val_print was initially called. */
423 real_type
= value_rtti_type (value
, &full
, &top
, &using_enc
);
429 cp_print_value_fields (type
, real_type
, offset
,
430 address
, stream
, recurse
, val
, options
,
431 dont_print_vb
, dont_print_statmem
);
434 /* Special val_print routine to avoid printing multiple copies of
435 virtual baseclasses. */
438 cp_print_value (struct type
*type
, struct type
*real_type
,
440 CORE_ADDR address
, struct ui_file
*stream
,
441 int recurse
, struct value
*val
,
442 const struct value_print_options
*options
,
443 struct type
**dont_print_vb
)
445 struct type
**last_dont_print
446 = (struct type
**) obstack_next_free (&dont_print_vb_obstack
);
447 struct obstack tmp_obstack
= dont_print_vb_obstack
;
448 int i
, n_baseclasses
= TYPE_N_BASECLASSES (type
);
450 struct type
*thistype
;
451 const gdb_byte
*valaddr
= value_contents_for_printing (val
);
453 if (dont_print_vb
== 0)
455 /* If we're at top level, carve out a completely fresh chunk of
456 the obstack and use that until this particular invocation
458 /* Bump up the high-water mark. Now alpha is omega. */
459 obstack_finish (&dont_print_vb_obstack
);
462 for (i
= 0; i
< n_baseclasses
; i
++)
466 struct type
*baseclass
= check_typedef (TYPE_BASECLASS (type
, i
));
467 const char *basename
= TYPE_NAME (baseclass
);
468 struct value
*base_val
= NULL
;
470 if (BASETYPE_VIA_VIRTUAL (type
, i
))
472 struct type
**first_dont_print
473 = (struct type
**) obstack_base (&dont_print_vb_obstack
);
475 int j
= (struct type
**)
476 obstack_next_free (&dont_print_vb_obstack
) - first_dont_print
;
479 if (baseclass
== first_dont_print
[j
])
482 obstack_ptr_grow (&dont_print_vb_obstack
, baseclass
);
486 thistype
= real_type
;
490 boffset
= baseclass_offset (type
, i
, valaddr
, offset
, address
, val
);
492 catch (const gdb_exception_error
&ex
)
494 if (ex
.error
== NOT_AVAILABLE_ERROR
)
502 if (BASETYPE_VIA_VIRTUAL (type
, i
))
504 /* The virtual base class pointer might have been
505 clobbered by the user program. Make sure that it
506 still points to a valid memory location. */
508 if ((boffset
+ offset
) < 0
509 || (boffset
+ offset
) >= TYPE_LENGTH (real_type
))
511 gdb::byte_vector
buf (TYPE_LENGTH (baseclass
));
513 if (target_read_memory (address
+ boffset
, buf
.data (),
514 TYPE_LENGTH (baseclass
)) != 0)
516 base_val
= value_from_contents_and_address (baseclass
,
519 baseclass
= value_type (base_val
);
522 thistype
= baseclass
;
535 /* Now do the printing. */
536 if (options
->prettyformat
)
538 fprintf_filtered (stream
, "\n");
539 print_spaces_filtered (2 * recurse
, stream
);
541 fputs_filtered ("<", stream
);
542 /* Not sure what the best notation is in the case where there is
543 no baseclass name. */
544 fputs_filtered (basename
? basename
: "", stream
);
545 fputs_filtered ("> = ", stream
);
548 val_print_unavailable (stream
);
550 val_print_invalid_address (stream
);
555 if (options
->max_depth
> -1
556 && recurse
>= options
->max_depth
)
558 const struct language_defn
*language
= current_language
;
559 gdb_assert (language
->la_struct_too_deep_ellipsis
!= NULL
);
560 fputs_filtered (language
->la_struct_too_deep_ellipsis
, stream
);
564 /* Attempt to run an extension language pretty-printer on the
565 baseclass if possible. */
568 = apply_ext_lang_val_pretty_printer (baseclass
,
569 thisoffset
+ boffset
,
570 value_address (base_val
),
576 cp_print_value_fields (baseclass
, thistype
,
577 thisoffset
+ boffset
,
578 value_address (base_val
),
579 stream
, recurse
, base_val
, options
,
581 obstack_base (&dont_print_vb_obstack
)),
585 fputs_filtered (", ", stream
);
591 if (dont_print_vb
== 0)
593 /* Free the space used to deal with the printing
594 of this type from top level. */
595 obstack_free (&dont_print_vb_obstack
, last_dont_print
);
596 /* Reset watermark so that we can continue protecting
597 ourselves from whatever we were protecting ourselves. */
598 dont_print_vb_obstack
= tmp_obstack
;
602 /* Print value of a static member. To avoid infinite recursion when
603 printing a class that contains a static instance of the class, we
604 keep the addresses of all printed static member classes in an
605 obstack and refuse to print them more than once.
607 VAL contains the value to print, TYPE, STREAM, RECURSE, and OPTIONS
608 have the same meanings as in c_val_print. */
611 cp_print_static_field (struct type
*type
,
613 struct ui_file
*stream
,
615 const struct value_print_options
*options
)
617 struct value_print_options opts
;
619 if (value_entirely_optimized_out (val
))
621 val_print_optimized_out (val
, stream
);
625 struct type
*real_type
= check_typedef (type
);
626 if (TYPE_CODE (real_type
) == TYPE_CODE_STRUCT
)
628 CORE_ADDR
*first_dont_print
;
633 = (CORE_ADDR
*) obstack_base (&dont_print_statmem_obstack
);
634 i
= obstack_object_size (&dont_print_statmem_obstack
)
635 / sizeof (CORE_ADDR
);
639 if (value_address (val
) == first_dont_print
[i
])
641 fputs_styled (_("<same as static member of an already"
643 metadata_style
.style (), stream
);
648 addr
= value_address (val
);
649 obstack_grow (&dont_print_statmem_obstack
, (char *) &addr
,
651 cp_print_value_fields (type
, value_enclosing_type (val
),
652 value_embedded_offset (val
), addr
,
653 stream
, recurse
, val
,
658 if (TYPE_CODE (real_type
) == TYPE_CODE_ARRAY
)
660 struct type
**first_dont_print
;
662 struct type
*target_type
= TYPE_TARGET_TYPE (type
);
665 = (struct type
**) obstack_base (&dont_print_stat_array_obstack
);
666 i
= obstack_object_size (&dont_print_stat_array_obstack
)
667 / sizeof (struct type
*);
671 if (target_type
== first_dont_print
[i
])
673 fputs_styled (_("<same as static member of an already"
675 metadata_style
.style (), stream
);
680 obstack_grow (&dont_print_stat_array_obstack
,
681 (char *) &target_type
,
682 sizeof (struct type
*));
688 value_embedded_offset (val
),
690 stream
, recurse
, val
,
691 &opts
, current_language
);
694 /* Find the field in *SELF, or its non-virtual base classes, with
695 bit offset OFFSET. Set *SELF to the containing type and *FIELDNO
696 to the containing field number. If OFFSET is not exactly at the
697 start of some field, set *SELF to NULL. */
700 cp_find_class_member (struct type
**self_p
, int *fieldno
,
707 *self_p
= check_typedef (*self_p
);
709 len
= TYPE_NFIELDS (self
);
711 for (i
= TYPE_N_BASECLASSES (self
); i
< len
; i
++)
713 LONGEST bitpos
= TYPE_FIELD_BITPOS (self
, i
);
716 if (offset
== bitpos
)
723 for (i
= 0; i
< TYPE_N_BASECLASSES (self
); i
++)
725 LONGEST bitpos
= TYPE_FIELD_BITPOS (self
, i
);
726 LONGEST bitsize
= 8 * TYPE_LENGTH (TYPE_FIELD_TYPE (self
, i
));
728 if (offset
>= bitpos
&& offset
< bitpos
+ bitsize
)
730 *self_p
= TYPE_FIELD_TYPE (self
, i
);
731 cp_find_class_member (self_p
, fieldno
, offset
- bitpos
);
740 cp_print_class_member (const gdb_byte
*valaddr
, struct type
*type
,
741 struct ui_file
*stream
, const char *prefix
)
743 enum bfd_endian byte_order
= type_byte_order (type
);
745 /* VAL is a byte offset into the structure type SELF_TYPE.
746 Find the name of the field for that offset and
748 struct type
*self_type
= TYPE_SELF_TYPE (type
);
752 val
= extract_signed_integer (valaddr
,
756 /* Pointers to data members are usually byte offsets into an object.
757 Because a data member can have offset zero, and a NULL pointer to
758 member must be distinct from any valid non-NULL pointer to
759 member, either the value is biased or the NULL value has a
760 special representation; both are permitted by ISO C++. HP aCC
761 used a bias of 0x20000000; HP cfront used a bias of 1; g++ 3.x
762 and other compilers which use the Itanium ABI use -1 as the NULL
763 value. GDB only supports that last form; to add support for
764 another form, make this into a cp-abi hook. */
768 fprintf_filtered (stream
, "NULL");
772 cp_find_class_member (&self_type
, &fieldno
, val
<< 3);
774 if (self_type
!= NULL
)
778 fputs_filtered (prefix
, stream
);
779 name
= TYPE_NAME (self_type
);
781 fputs_filtered (name
, stream
);
783 c_type_print_base (self_type
, stream
, 0, 0, &type_print_raw_options
);
784 fprintf_filtered (stream
, "::");
785 fputs_filtered (TYPE_FIELD_NAME (self_type
, fieldno
), stream
);
788 fprintf_filtered (stream
, "%ld", (long) val
);
793 _initialize_cp_valprint (void)
795 obstack_begin (&dont_print_stat_array_obstack
,
796 32 * sizeof (struct type
*));
797 obstack_begin (&dont_print_statmem_obstack
,
798 32 * sizeof (CORE_ADDR
));
799 obstack_begin (&dont_print_vb_obstack
,
800 32 * sizeof (struct type
*));