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 "common/byte-vector.h"
40 static struct obstack dont_print_vb_obstack;
41 static struct obstack dont_print_statmem_obstack;
42 static struct obstack dont_print_stat_array_obstack;
44 static void cp_print_static_field (struct type *, struct value *,
45 struct ui_file *, int,
46 const struct value_print_options *);
48 static void cp_print_value (struct type *, struct type *,
50 CORE_ADDR, struct ui_file *,
52 const struct value_print_options *,
56 /* GCC versions after 2.4.5 use this. */
57 extern const char vtbl_ptr_name[] = "__vtbl_ptr_type";
59 /* Return truth value for assertion that TYPE is of the type
60 "pointer to virtual function". */
63 cp_is_vtbl_ptr_type (struct type *type)
65 const char *type_name = TYPE_NAME (type);
67 return (type_name != NULL && !strcmp (type_name, vtbl_ptr_name));
70 /* Return truth value for the assertion that TYPE is of the type
71 "pointer to virtual function table". */
74 cp_is_vtbl_member (struct type *type)
76 /* With older versions of g++, the vtbl field pointed to an array of
77 structures. Nowadays it points directly to the structure. */
78 if (TYPE_CODE (type) == TYPE_CODE_PTR)
80 type = TYPE_TARGET_TYPE (type);
81 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
83 type = TYPE_TARGET_TYPE (type);
84 if (TYPE_CODE (type) == TYPE_CODE_STRUCT /* if not using thunks */
85 || TYPE_CODE (type) == TYPE_CODE_PTR) /* if using thunks */
87 /* Virtual functions tables are full of pointers
88 to virtual functions. */
89 return cp_is_vtbl_ptr_type (type);
92 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT) /* if not using thunks */
94 return cp_is_vtbl_ptr_type (type);
96 else if (TYPE_CODE (type) == TYPE_CODE_PTR) /* if using thunks */
98 /* The type name of the thunk pointer is NULL when using
99 dwarf2. We could test for a pointer to a function, but
100 there is no type info for the virtual table either, so it
102 return cp_is_vtbl_ptr_type (type);
108 /* Mutually recursive subroutines of cp_print_value and c_val_print to
109 print out a structure's fields: cp_print_value_fields and
112 TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and OPTIONS have the same
113 meanings as in cp_print_value and c_val_print.
115 2nd argument REAL_TYPE is used to carry over the type of the
116 derived class across the recursion to base classes.
118 DONT_PRINT is an array of baseclass types that we should not print,
119 or zero if called from top level. */
122 cp_print_value_fields (struct type *type, struct type *real_type,
124 CORE_ADDR address, struct ui_file *stream,
125 int recurse, struct value *val,
126 const struct value_print_options *options,
127 struct type **dont_print_vb,
128 int dont_print_statmem)
130 int i, len, n_baseclasses;
132 static int last_set_recurse = -1;
134 type = check_typedef (type);
138 /* Any object can be left on obstacks only during an unexpected
141 if (obstack_object_size (&dont_print_statmem_obstack) > 0)
143 obstack_free (&dont_print_statmem_obstack, NULL);
144 obstack_begin (&dont_print_statmem_obstack,
145 32 * sizeof (CORE_ADDR));
147 if (obstack_object_size (&dont_print_stat_array_obstack) > 0)
149 obstack_free (&dont_print_stat_array_obstack, NULL);
150 obstack_begin (&dont_print_stat_array_obstack,
151 32 * sizeof (struct type *));
155 fprintf_filtered (stream, "{");
156 len = TYPE_NFIELDS (type);
157 n_baseclasses = TYPE_N_BASECLASSES (type);
159 /* First, print out baseclasses such that we don't print
160 duplicates of virtual baseclasses. */
162 if (n_baseclasses > 0)
163 cp_print_value (type, real_type,
164 offset, address, stream,
165 recurse + 1, val, options,
168 /* Second, print out data fields */
170 /* If there are no data fields, skip this part */
171 if (len == n_baseclasses || !len)
172 fprintf_filtered (stream, "<No data fields>");
175 size_t statmem_obstack_initial_size = 0;
176 size_t stat_array_obstack_initial_size = 0;
177 struct type *vptr_basetype = NULL;
180 if (dont_print_statmem == 0)
182 statmem_obstack_initial_size =
183 obstack_object_size (&dont_print_statmem_obstack);
185 if (last_set_recurse != recurse)
187 stat_array_obstack_initial_size =
188 obstack_object_size (&dont_print_stat_array_obstack);
190 last_set_recurse = recurse;
194 vptr_fieldno = get_vptr_fieldno (type, &vptr_basetype);
195 for (i = n_baseclasses; i < len; i++)
197 const gdb_byte *valaddr = value_contents_for_printing (val);
199 /* If requested, skip printing of static fields. */
200 if (!options->static_field_print
201 && field_is_static (&TYPE_FIELD (type, i)))
206 fputs_filtered (",", stream);
207 if (!options->prettyformat)
208 fputs_filtered (" ", stream);
210 else if (n_baseclasses > 0)
212 if (options->prettyformat)
214 fprintf_filtered (stream, "\n");
215 print_spaces_filtered (2 + 2 * recurse, stream);
216 fputs_filtered ("members of ", stream);
217 fputs_filtered (TYPE_NAME (type), stream);
218 fputs_filtered (":", stream);
223 if (options->prettyformat)
225 fprintf_filtered (stream, "\n");
226 print_spaces_filtered (2 + 2 * recurse, stream);
230 wrap_here (n_spaces (2 + 2 * recurse));
233 annotate_field_begin (TYPE_FIELD_TYPE (type, i));
235 if (field_is_static (&TYPE_FIELD (type, i)))
236 fputs_filtered ("static ", stream);
237 fprintf_symbol_filtered (stream,
238 TYPE_FIELD_NAME (type, i),
239 current_language->la_language,
240 DMGL_PARAMS | DMGL_ANSI);
241 annotate_field_name_end ();
243 /* We tweak various options in a few cases below. */
244 value_print_options options_copy = *options;
245 value_print_options *opts = &options_copy;
247 /* Do not print leading '=' in case of anonymous
249 if (strcmp (TYPE_FIELD_NAME (type, i), ""))
250 fputs_filtered (" = ", stream);
253 /* If this is an anonymous field then we want to consider it
254 as though it is at its parent's depth when it comes to the
256 if (opts->max_depth != -1 && opts->max_depth < INT_MAX)
259 annotate_field_value ();
261 if (!field_is_static (&TYPE_FIELD (type, i))
262 && TYPE_FIELD_PACKED (type, i))
266 /* Bitfields require special handling, especially due to
267 byte order problems. */
268 if (TYPE_FIELD_IGNORE (type, i))
270 fputs_filtered ("<optimized out or zero length>", stream);
272 else if (value_bits_synthetic_pointer (val,
273 TYPE_FIELD_BITPOS (type,
275 TYPE_FIELD_BITSIZE (type,
278 fputs_filtered (_("<synthetic pointer>"), stream);
284 v = value_field_bitfield (type, i, valaddr, offset, val);
286 common_val_print (v, stream, recurse + 1,
287 opts, current_language);
292 if (TYPE_FIELD_IGNORE (type, i))
294 fputs_filtered ("<optimized out or zero length>",
297 else if (field_is_static (&TYPE_FIELD (type, i)))
301 struct value *v = value_static_field (type, i);
303 cp_print_static_field (TYPE_FIELD_TYPE (type, i),
304 v, stream, recurse + 1,
307 catch (const gdb_exception_error &ex)
309 fprintf_filtered (stream,
310 _("<error reading variable: %s>"),
314 else if (i == vptr_fieldno && type == vptr_basetype)
316 int i_offset = offset + TYPE_FIELD_BITPOS (type, i) / 8;
317 struct type *i_type = TYPE_FIELD_TYPE (type, i);
319 if (valprint_check_validity (stream, i_type, i_offset, val))
323 addr = extract_typed_address (valaddr + i_offset, i_type);
324 print_function_pointer_address (opts,
325 get_type_arch (type),
332 val_print (TYPE_FIELD_TYPE (type, i),
333 offset + TYPE_FIELD_BITPOS (type, i) / 8,
335 stream, recurse + 1, val, 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 /* Like cp_print_value_fields, but find the runtime type of the object
385 and pass it as the `real_type' argument to cp_print_value_fields.
386 This function is a hack to work around the fact that
387 common_val_print passes the embedded offset to val_print, but not
388 the enclosing type. */
391 cp_print_value_fields_rtti (struct type *type,
392 const gdb_byte *valaddr, LONGEST offset,
394 struct ui_file *stream, int recurse,
396 const struct value_print_options *options,
397 struct type **dont_print_vb,
398 int dont_print_statmem)
400 struct type *real_type = NULL;
402 /* We require all bits to be valid in order to attempt a
404 if (!value_bits_any_optimized_out (val,
405 TARGET_CHAR_BIT * offset,
406 TARGET_CHAR_BIT * TYPE_LENGTH (type)))
412 /* Ugh, we have to convert back to a value here. */
413 value = value_from_contents_and_address (type, valaddr + offset,
415 type = value_type (value);
416 /* We don't actually care about most of the result here -- just
417 the type. We already have the correct offset, due to how
418 val_print was initially called. */
419 real_type = value_rtti_type (value, &full, &top, &using_enc);
425 cp_print_value_fields (type, real_type, offset,
426 address, stream, recurse, val, options,
427 dont_print_vb, dont_print_statmem);
430 /* Special val_print routine to avoid printing multiple copies of
431 virtual baseclasses. */
434 cp_print_value (struct type *type, struct type *real_type,
436 CORE_ADDR address, struct ui_file *stream,
437 int recurse, struct value *val,
438 const struct value_print_options *options,
439 struct type **dont_print_vb)
441 struct type **last_dont_print
442 = (struct type **) obstack_next_free (&dont_print_vb_obstack);
443 struct obstack tmp_obstack = dont_print_vb_obstack;
444 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
446 struct type *thistype;
447 const gdb_byte *valaddr = value_contents_for_printing (val);
449 if (dont_print_vb == 0)
451 /* If we're at top level, carve out a completely fresh chunk of
452 the obstack and use that until this particular invocation
454 /* Bump up the high-water mark. Now alpha is omega. */
455 obstack_finish (&dont_print_vb_obstack);
458 for (i = 0; i < n_baseclasses; i++)
462 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
463 const char *basename = TYPE_NAME (baseclass);
464 struct value *base_val = NULL;
466 if (BASETYPE_VIA_VIRTUAL (type, i))
468 struct type **first_dont_print
469 = (struct type **) obstack_base (&dont_print_vb_obstack);
471 int j = (struct type **)
472 obstack_next_free (&dont_print_vb_obstack) - first_dont_print;
475 if (baseclass == first_dont_print[j])
478 obstack_ptr_grow (&dont_print_vb_obstack, baseclass);
482 thistype = real_type;
486 boffset = baseclass_offset (type, i, valaddr, offset, address, val);
488 catch (const gdb_exception_error &ex)
490 if (ex.error == NOT_AVAILABLE_ERROR)
498 if (BASETYPE_VIA_VIRTUAL (type, i))
500 /* The virtual base class pointer might have been
501 clobbered by the user program. Make sure that it
502 still points to a valid memory location. */
504 if ((boffset + offset) < 0
505 || (boffset + offset) >= TYPE_LENGTH (real_type))
507 gdb::byte_vector buf (TYPE_LENGTH (baseclass));
509 if (target_read_memory (address + boffset, buf.data (),
510 TYPE_LENGTH (baseclass)) != 0)
512 base_val = value_from_contents_and_address (baseclass,
515 baseclass = value_type (base_val);
518 thistype = baseclass;
531 /* Now do the printing. */
532 if (options->prettyformat)
534 fprintf_filtered (stream, "\n");
535 print_spaces_filtered (2 * recurse, stream);
537 fputs_filtered ("<", stream);
538 /* Not sure what the best notation is in the case where there is
539 no baseclass name. */
540 fputs_filtered (basename ? basename : "", stream);
541 fputs_filtered ("> = ", stream);
544 val_print_unavailable (stream);
546 val_print_invalid_address (stream);
551 if (options->max_depth > -1
552 && recurse >= options->max_depth)
554 const struct language_defn *language = current_language;
555 gdb_assert (language->la_struct_too_deep_ellipsis != NULL);
556 fputs_filtered (language->la_struct_too_deep_ellipsis, stream);
560 /* Attempt to run an extension language pretty-printer on the
561 baseclass if possible. */
564 = apply_ext_lang_val_pretty_printer (baseclass,
565 thisoffset + boffset,
566 value_address (base_val),
572 cp_print_value_fields (baseclass, thistype,
573 thisoffset + boffset,
574 value_address (base_val),
575 stream, recurse, base_val, options,
577 obstack_base (&dont_print_vb_obstack)),
581 fputs_filtered (", ", stream);
587 if (dont_print_vb == 0)
589 /* Free the space used to deal with the printing
590 of this type from top level. */
591 obstack_free (&dont_print_vb_obstack, last_dont_print);
592 /* Reset watermark so that we can continue protecting
593 ourselves from whatever we were protecting ourselves. */
594 dont_print_vb_obstack = tmp_obstack;
598 /* Print value of a static member. To avoid infinite recursion when
599 printing a class that contains a static instance of the class, we
600 keep the addresses of all printed static member classes in an
601 obstack and refuse to print them more than once.
603 VAL contains the value to print, TYPE, STREAM, RECURSE, and OPTIONS
604 have the same meanings as in c_val_print. */
607 cp_print_static_field (struct type *type,
609 struct ui_file *stream,
611 const struct value_print_options *options)
613 struct value_print_options opts;
615 if (value_entirely_optimized_out (val))
617 val_print_optimized_out (val, stream);
621 struct type *real_type = check_typedef (type);
622 if (TYPE_CODE (real_type) == TYPE_CODE_STRUCT)
624 CORE_ADDR *first_dont_print;
629 = (CORE_ADDR *) obstack_base (&dont_print_statmem_obstack);
630 i = obstack_object_size (&dont_print_statmem_obstack)
631 / sizeof (CORE_ADDR);
635 if (value_address (val) == first_dont_print[i])
637 fputs_filtered ("<same as static member of an already"
644 addr = value_address (val);
645 obstack_grow (&dont_print_statmem_obstack, (char *) &addr,
647 cp_print_value_fields (type, value_enclosing_type (val),
648 value_embedded_offset (val), addr,
649 stream, recurse, val,
654 if (TYPE_CODE (real_type) == TYPE_CODE_ARRAY)
656 struct type **first_dont_print;
658 struct type *target_type = TYPE_TARGET_TYPE (type);
661 = (struct type **) obstack_base (&dont_print_stat_array_obstack);
662 i = obstack_object_size (&dont_print_stat_array_obstack)
663 / sizeof (struct type *);
667 if (target_type == first_dont_print[i])
669 fputs_filtered ("<same as static member of an already"
676 obstack_grow (&dont_print_stat_array_obstack,
677 (char *) &target_type,
678 sizeof (struct type *));
684 value_embedded_offset (val),
686 stream, recurse, val,
687 &opts, current_language);
690 /* Find the field in *SELF, or its non-virtual base classes, with
691 bit offset OFFSET. Set *SELF to the containing type and *FIELDNO
692 to the containing field number. If OFFSET is not exactly at the
693 start of some field, set *SELF to NULL. */
696 cp_find_class_member (struct type **self_p, int *fieldno,
703 *self_p = check_typedef (*self_p);
705 len = TYPE_NFIELDS (self);
707 for (i = TYPE_N_BASECLASSES (self); i < len; i++)
709 LONGEST bitpos = TYPE_FIELD_BITPOS (self, i);
712 if (offset == bitpos)
719 for (i = 0; i < TYPE_N_BASECLASSES (self); i++)
721 LONGEST bitpos = TYPE_FIELD_BITPOS (self, i);
722 LONGEST bitsize = 8 * TYPE_LENGTH (TYPE_FIELD_TYPE (self, i));
724 if (offset >= bitpos && offset < bitpos + bitsize)
726 *self_p = TYPE_FIELD_TYPE (self, i);
727 cp_find_class_member (self_p, fieldno, offset - bitpos);
736 cp_print_class_member (const gdb_byte *valaddr, struct type *type,
737 struct ui_file *stream, const char *prefix)
739 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
741 /* VAL is a byte offset into the structure type SELF_TYPE.
742 Find the name of the field for that offset and
744 struct type *self_type = TYPE_SELF_TYPE (type);
748 val = extract_signed_integer (valaddr,
752 /* Pointers to data members are usually byte offsets into an object.
753 Because a data member can have offset zero, and a NULL pointer to
754 member must be distinct from any valid non-NULL pointer to
755 member, either the value is biased or the NULL value has a
756 special representation; both are permitted by ISO C++. HP aCC
757 used a bias of 0x20000000; HP cfront used a bias of 1; g++ 3.x
758 and other compilers which use the Itanium ABI use -1 as the NULL
759 value. GDB only supports that last form; to add support for
760 another form, make this into a cp-abi hook. */
764 fprintf_filtered (stream, "NULL");
768 cp_find_class_member (&self_type, &fieldno, val << 3);
770 if (self_type != NULL)
774 fputs_filtered (prefix, stream);
775 name = TYPE_NAME (self_type);
777 fputs_filtered (name, stream);
779 c_type_print_base (self_type, stream, 0, 0, &type_print_raw_options);
780 fprintf_filtered (stream, "::");
781 fputs_filtered (TYPE_FIELD_NAME (self_type, fieldno), stream);
784 fprintf_filtered (stream, "%ld", (long) val);
789 _initialize_cp_valprint (void)
791 obstack_begin (&dont_print_stat_array_obstack,
792 32 * sizeof (struct type *));
793 obstack_begin (&dont_print_statmem_obstack,
794 32 * sizeof (CORE_ADDR));
795 obstack_begin (&dont_print_vb_obstack,
796 32 * sizeof (struct type *));