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 /* 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 static void cp_print_static_field (struct type *, struct value *,
78 struct ui_file *, int,
79 const struct value_print_options *);
81 static void cp_print_value (struct type *, struct type *,
83 CORE_ADDR, struct ui_file *,
85 const struct value_print_options *,
89 /* GCC versions after 2.4.5 use this. */
90 extern const char vtbl_ptr_name[] = "__vtbl_ptr_type";
92 /* Return truth value for assertion that TYPE is of the type
93 "pointer to virtual function". */
96 cp_is_vtbl_ptr_type (struct type *type)
98 const char *type_name = TYPE_NAME (type);
100 return (type_name != NULL && !strcmp (type_name, vtbl_ptr_name));
103 /* Return truth value for the assertion that TYPE is of the type
104 "pointer to virtual function table". */
107 cp_is_vtbl_member (struct type *type)
109 /* With older versions of g++, the vtbl field pointed to an array of
110 structures. Nowadays it points directly to the structure. */
111 if (TYPE_CODE (type) == TYPE_CODE_PTR)
113 type = TYPE_TARGET_TYPE (type);
114 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
116 type = TYPE_TARGET_TYPE (type);
117 if (TYPE_CODE (type) == TYPE_CODE_STRUCT /* if not using thunks */
118 || TYPE_CODE (type) == TYPE_CODE_PTR) /* if using thunks */
120 /* Virtual functions tables are full of pointers
121 to virtual functions. */
122 return cp_is_vtbl_ptr_type (type);
125 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT) /* if not using thunks */
127 return cp_is_vtbl_ptr_type (type);
129 else if (TYPE_CODE (type) == TYPE_CODE_PTR) /* if using thunks */
131 /* The type name of the thunk pointer is NULL when using
132 dwarf2. We could test for a pointer to a function, but
133 there is no type info for the virtual table either, so it
135 return cp_is_vtbl_ptr_type (type);
141 /* Mutually recursive subroutines of cp_print_value and c_val_print to
142 print out a structure's fields: cp_print_value_fields and
145 TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and OPTIONS have the same
146 meanings as in cp_print_value and c_val_print.
148 2nd argument REAL_TYPE is used to carry over the type of the
149 derived class across the recursion to base classes.
151 DONT_PRINT is an array of baseclass types that we should not print,
152 or zero if called from top level. */
155 cp_print_value_fields (struct type *type, struct type *real_type,
157 CORE_ADDR address, struct ui_file *stream,
158 int recurse, struct value *val,
159 const struct value_print_options *options,
160 struct type **dont_print_vb,
161 int dont_print_statmem)
163 int i, len, n_baseclasses;
165 static int last_set_recurse = -1;
167 type = check_typedef (type);
171 /* Any object can be left on obstacks only during an unexpected
174 if (obstack_object_size (&dont_print_statmem_obstack) > 0)
176 obstack_free (&dont_print_statmem_obstack, NULL);
177 obstack_begin (&dont_print_statmem_obstack,
178 32 * sizeof (CORE_ADDR));
180 if (obstack_object_size (&dont_print_stat_array_obstack) > 0)
182 obstack_free (&dont_print_stat_array_obstack, NULL);
183 obstack_begin (&dont_print_stat_array_obstack,
184 32 * sizeof (struct type *));
188 fprintf_filtered (stream, "{");
189 len = TYPE_NFIELDS (type);
190 n_baseclasses = TYPE_N_BASECLASSES (type);
192 /* First, print out baseclasses such that we don't print
193 duplicates of virtual baseclasses. */
195 if (n_baseclasses > 0)
196 cp_print_value (type, real_type,
197 offset, address, stream,
198 recurse + 1, val, options,
201 /* Second, print out data fields */
203 /* If there are no data fields, skip this part */
204 if (len == n_baseclasses || !len)
205 fprintf_filtered (stream, "<No data fields>");
208 size_t statmem_obstack_initial_size = 0;
209 size_t stat_array_obstack_initial_size = 0;
210 struct type *vptr_basetype = NULL;
213 if (dont_print_statmem == 0)
215 statmem_obstack_initial_size =
216 obstack_object_size (&dont_print_statmem_obstack);
218 if (last_set_recurse != recurse)
220 stat_array_obstack_initial_size =
221 obstack_object_size (&dont_print_stat_array_obstack);
223 last_set_recurse = recurse;
227 vptr_fieldno = get_vptr_fieldno (type, &vptr_basetype);
228 for (i = n_baseclasses; i < len; i++)
230 const gdb_byte *valaddr = value_contents_for_printing (val);
232 /* If requested, skip printing of static fields. */
233 if (!options->static_field_print
234 && field_is_static (&TYPE_FIELD (type, i)))
239 fputs_filtered (",", stream);
240 if (!options->prettyformat)
241 fputs_filtered (" ", stream);
243 else if (n_baseclasses > 0)
245 if (options->prettyformat)
247 fprintf_filtered (stream, "\n");
248 print_spaces_filtered (2 + 2 * recurse, stream);
249 fputs_filtered ("members of ", stream);
250 fputs_filtered (TYPE_NAME (type), stream);
251 fputs_filtered (":", stream);
256 if (options->prettyformat)
258 fprintf_filtered (stream, "\n");
259 print_spaces_filtered (2 + 2 * recurse, stream);
263 wrap_here (n_spaces (2 + 2 * recurse));
266 annotate_field_begin (TYPE_FIELD_TYPE (type, i));
268 if (field_is_static (&TYPE_FIELD (type, i)))
269 fputs_filtered ("static ", stream);
270 fprintf_symbol_filtered (stream,
271 TYPE_FIELD_NAME (type, i),
272 current_language->la_language,
273 DMGL_PARAMS | DMGL_ANSI);
274 annotate_field_name_end ();
276 /* We tweak various options in a few cases below. */
277 value_print_options options_copy = *options;
278 value_print_options *opts = &options_copy;
280 /* Do not print leading '=' in case of anonymous
282 if (strcmp (TYPE_FIELD_NAME (type, i), ""))
283 fputs_filtered (" = ", stream);
286 /* If this is an anonymous field then we want to consider it
287 as though it is at its parent's depth when it comes to the
289 if (opts->max_depth != -1 && opts->max_depth < INT_MAX)
292 annotate_field_value ();
294 if (!field_is_static (&TYPE_FIELD (type, i))
295 && TYPE_FIELD_PACKED (type, i))
299 /* Bitfields require special handling, especially due to
300 byte order problems. */
301 if (TYPE_FIELD_IGNORE (type, i))
303 fputs_filtered ("<optimized out or zero length>", stream);
305 else if (value_bits_synthetic_pointer (val,
306 TYPE_FIELD_BITPOS (type,
308 TYPE_FIELD_BITSIZE (type,
311 fputs_filtered (_("<synthetic pointer>"), stream);
317 v = value_field_bitfield (type, i, valaddr, offset, val);
319 common_val_print (v, stream, recurse + 1,
320 opts, current_language);
325 if (TYPE_FIELD_IGNORE (type, i))
327 fputs_filtered ("<optimized out or zero length>",
330 else if (field_is_static (&TYPE_FIELD (type, i)))
334 struct value *v = value_static_field (type, i);
336 cp_print_static_field (TYPE_FIELD_TYPE (type, i),
337 v, stream, recurse + 1,
340 catch (const gdb_exception_error &ex)
342 fprintf_filtered (stream,
343 _("<error reading variable: %s>"),
347 else if (i == vptr_fieldno && type == vptr_basetype)
349 int i_offset = offset + TYPE_FIELD_BITPOS (type, i) / 8;
350 struct type *i_type = TYPE_FIELD_TYPE (type, i);
352 if (valprint_check_validity (stream, i_type, i_offset, val))
356 addr = extract_typed_address (valaddr + i_offset, i_type);
357 print_function_pointer_address (opts,
358 get_type_arch (type),
365 val_print (TYPE_FIELD_TYPE (type, i),
366 offset + TYPE_FIELD_BITPOS (type, i) / 8,
368 stream, recurse + 1, val, opts,
372 annotate_field_end ();
375 if (dont_print_statmem == 0)
377 size_t obstack_final_size =
378 obstack_object_size (&dont_print_statmem_obstack);
380 if (obstack_final_size > statmem_obstack_initial_size)
382 /* In effect, a pop of the printed-statics stack. */
384 = statmem_obstack_initial_size - obstack_final_size;
385 obstack_blank_fast (&dont_print_statmem_obstack, shrink_bytes);
388 if (last_set_recurse != recurse)
391 obstack_object_size (&dont_print_stat_array_obstack);
393 if (obstack_final_size > stat_array_obstack_initial_size)
396 (char *) obstack_next_free (&dont_print_stat_array_obstack)
397 - (obstack_final_size
398 - stat_array_obstack_initial_size);
400 obstack_free (&dont_print_stat_array_obstack,
403 last_set_recurse = -1;
407 if (options->prettyformat)
409 fprintf_filtered (stream, "\n");
410 print_spaces_filtered (2 * recurse, stream);
412 } /* if there are data fields */
414 fprintf_filtered (stream, "}");
417 /* Like cp_print_value_fields, but find the runtime type of the object
418 and pass it as the `real_type' argument to cp_print_value_fields.
419 This function is a hack to work around the fact that
420 common_val_print passes the embedded offset to val_print, but not
421 the enclosing type. */
424 cp_print_value_fields_rtti (struct type *type,
425 const gdb_byte *valaddr, LONGEST offset,
427 struct ui_file *stream, int recurse,
429 const struct value_print_options *options,
430 struct type **dont_print_vb,
431 int dont_print_statmem)
433 struct type *real_type = NULL;
435 /* We require all bits to be valid in order to attempt a
437 if (!value_bits_any_optimized_out (val,
438 TARGET_CHAR_BIT * offset,
439 TARGET_CHAR_BIT * TYPE_LENGTH (type)))
445 /* Ugh, we have to convert back to a value here. */
446 value = value_from_contents_and_address (type, valaddr + offset,
448 type = value_type (value);
449 /* We don't actually care about most of the result here -- just
450 the type. We already have the correct offset, due to how
451 val_print was initially called. */
452 real_type = value_rtti_type (value, &full, &top, &using_enc);
458 cp_print_value_fields (type, real_type, offset,
459 address, stream, recurse, val, options,
460 dont_print_vb, dont_print_statmem);
463 /* Special val_print routine to avoid printing multiple copies of
464 virtual baseclasses. */
467 cp_print_value (struct type *type, struct type *real_type,
469 CORE_ADDR address, struct ui_file *stream,
470 int recurse, struct value *val,
471 const struct value_print_options *options,
472 struct type **dont_print_vb)
474 struct type **last_dont_print
475 = (struct type **) obstack_next_free (&dont_print_vb_obstack);
476 struct obstack tmp_obstack = dont_print_vb_obstack;
477 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
479 struct type *thistype;
480 const gdb_byte *valaddr = value_contents_for_printing (val);
482 if (dont_print_vb == 0)
484 /* If we're at top level, carve out a completely fresh chunk of
485 the obstack and use that until this particular invocation
487 /* Bump up the high-water mark. Now alpha is omega. */
488 obstack_finish (&dont_print_vb_obstack);
491 for (i = 0; i < n_baseclasses; i++)
495 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
496 const char *basename = TYPE_NAME (baseclass);
497 struct value *base_val = NULL;
499 if (BASETYPE_VIA_VIRTUAL (type, i))
501 struct type **first_dont_print
502 = (struct type **) obstack_base (&dont_print_vb_obstack);
504 int j = (struct type **)
505 obstack_next_free (&dont_print_vb_obstack) - first_dont_print;
508 if (baseclass == first_dont_print[j])
511 obstack_ptr_grow (&dont_print_vb_obstack, baseclass);
515 thistype = real_type;
519 boffset = baseclass_offset (type, i, valaddr, offset, address, val);
521 catch (const gdb_exception_error &ex)
523 if (ex.error == NOT_AVAILABLE_ERROR)
531 if (BASETYPE_VIA_VIRTUAL (type, i))
533 /* The virtual base class pointer might have been
534 clobbered by the user program. Make sure that it
535 still points to a valid memory location. */
537 if ((boffset + offset) < 0
538 || (boffset + offset) >= TYPE_LENGTH (real_type))
540 gdb::byte_vector buf (TYPE_LENGTH (baseclass));
542 if (target_read_memory (address + boffset, buf.data (),
543 TYPE_LENGTH (baseclass)) != 0)
545 base_val = value_from_contents_and_address (baseclass,
548 baseclass = value_type (base_val);
551 thistype = baseclass;
564 /* Now do the printing. */
565 if (options->prettyformat)
567 fprintf_filtered (stream, "\n");
568 print_spaces_filtered (2 * recurse, stream);
570 fputs_filtered ("<", stream);
571 /* Not sure what the best notation is in the case where there is
572 no baseclass name. */
573 fputs_filtered (basename ? basename : "", stream);
574 fputs_filtered ("> = ", stream);
577 val_print_unavailable (stream);
579 val_print_invalid_address (stream);
584 if (options->max_depth > -1
585 && recurse >= options->max_depth)
587 const struct language_defn *language = current_language;
588 gdb_assert (language->la_struct_too_deep_ellipsis != NULL);
589 fputs_filtered (language->la_struct_too_deep_ellipsis, stream);
593 /* Attempt to run an extension language pretty-printer on the
594 baseclass if possible. */
597 = apply_ext_lang_val_pretty_printer (baseclass,
598 thisoffset + boffset,
599 value_address (base_val),
605 cp_print_value_fields (baseclass, thistype,
606 thisoffset + boffset,
607 value_address (base_val),
608 stream, recurse, base_val, options,
610 obstack_base (&dont_print_vb_obstack)),
614 fputs_filtered (", ", stream);
620 if (dont_print_vb == 0)
622 /* Free the space used to deal with the printing
623 of this type from top level. */
624 obstack_free (&dont_print_vb_obstack, last_dont_print);
625 /* Reset watermark so that we can continue protecting
626 ourselves from whatever we were protecting ourselves. */
627 dont_print_vb_obstack = tmp_obstack;
631 /* Print value of a static member. To avoid infinite recursion when
632 printing a class that contains a static instance of the class, we
633 keep the addresses of all printed static member classes in an
634 obstack and refuse to print them more than once.
636 VAL contains the value to print, TYPE, STREAM, RECURSE, and OPTIONS
637 have the same meanings as in c_val_print. */
640 cp_print_static_field (struct type *type,
642 struct ui_file *stream,
644 const struct value_print_options *options)
646 struct value_print_options opts;
648 if (value_entirely_optimized_out (val))
650 val_print_optimized_out (val, stream);
654 struct type *real_type = check_typedef (type);
655 if (TYPE_CODE (real_type) == TYPE_CODE_STRUCT)
657 CORE_ADDR *first_dont_print;
662 = (CORE_ADDR *) obstack_base (&dont_print_statmem_obstack);
663 i = obstack_object_size (&dont_print_statmem_obstack)
664 / sizeof (CORE_ADDR);
668 if (value_address (val) == first_dont_print[i])
670 fputs_filtered ("<same as static member of an already"
677 addr = value_address (val);
678 obstack_grow (&dont_print_statmem_obstack, (char *) &addr,
680 cp_print_value_fields (type, value_enclosing_type (val),
681 value_embedded_offset (val), addr,
682 stream, recurse, val,
687 if (TYPE_CODE (real_type) == TYPE_CODE_ARRAY)
689 struct type **first_dont_print;
691 struct type *target_type = TYPE_TARGET_TYPE (type);
694 = (struct type **) obstack_base (&dont_print_stat_array_obstack);
695 i = obstack_object_size (&dont_print_stat_array_obstack)
696 / sizeof (struct type *);
700 if (target_type == first_dont_print[i])
702 fputs_filtered ("<same as static member of an already"
709 obstack_grow (&dont_print_stat_array_obstack,
710 (char *) &target_type,
711 sizeof (struct type *));
717 value_embedded_offset (val),
719 stream, recurse, val,
720 &opts, current_language);
723 /* Find the field in *SELF, or its non-virtual base classes, with
724 bit offset OFFSET. Set *SELF to the containing type and *FIELDNO
725 to the containing field number. If OFFSET is not exactly at the
726 start of some field, set *SELF to NULL. */
729 cp_find_class_member (struct type **self_p, int *fieldno,
736 *self_p = check_typedef (*self_p);
738 len = TYPE_NFIELDS (self);
740 for (i = TYPE_N_BASECLASSES (self); i < len; i++)
742 LONGEST bitpos = TYPE_FIELD_BITPOS (self, i);
745 if (offset == bitpos)
752 for (i = 0; i < TYPE_N_BASECLASSES (self); i++)
754 LONGEST bitpos = TYPE_FIELD_BITPOS (self, i);
755 LONGEST bitsize = 8 * TYPE_LENGTH (TYPE_FIELD_TYPE (self, i));
757 if (offset >= bitpos && offset < bitpos + bitsize)
759 *self_p = TYPE_FIELD_TYPE (self, i);
760 cp_find_class_member (self_p, fieldno, offset - bitpos);
769 cp_print_class_member (const gdb_byte *valaddr, struct type *type,
770 struct ui_file *stream, const char *prefix)
772 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
774 /* VAL is a byte offset into the structure type SELF_TYPE.
775 Find the name of the field for that offset and
777 struct type *self_type = TYPE_SELF_TYPE (type);
781 val = extract_signed_integer (valaddr,
785 /* Pointers to data members are usually byte offsets into an object.
786 Because a data member can have offset zero, and a NULL pointer to
787 member must be distinct from any valid non-NULL pointer to
788 member, either the value is biased or the NULL value has a
789 special representation; both are permitted by ISO C++. HP aCC
790 used a bias of 0x20000000; HP cfront used a bias of 1; g++ 3.x
791 and other compilers which use the Itanium ABI use -1 as the NULL
792 value. GDB only supports that last form; to add support for
793 another form, make this into a cp-abi hook. */
797 fprintf_filtered (stream, "NULL");
801 cp_find_class_member (&self_type, &fieldno, val << 3);
803 if (self_type != NULL)
807 fputs_filtered (prefix, stream);
808 name = TYPE_NAME (self_type);
810 fputs_filtered (name, stream);
812 c_type_print_base (self_type, stream, 0, 0, &type_print_raw_options);
813 fprintf_filtered (stream, "::");
814 fputs_filtered (TYPE_FIELD_NAME (self_type, fieldno), stream);
817 fprintf_filtered (stream, "%ld", (long) val);
822 _initialize_cp_valprint (void)
824 add_setshow_boolean_cmd ("static-members", class_support,
825 &user_print_options.static_field_print, _("\
826 Set printing of C++ static members."), _("\
827 Show printing of C++ static members."), NULL,
829 show_static_field_print,
830 &setprintlist, &showprintlist);
832 add_setshow_boolean_cmd ("vtbl", class_support,
833 &user_print_options.vtblprint, _("\
834 Set printing of C++ virtual function tables."), _("\
835 Show printing of C++ virtual function tables."), NULL,
838 &setprintlist, &showprintlist);
840 add_setshow_boolean_cmd ("object", class_support,
841 &user_print_options.objectprint, _("\
842 Set printing of object's derived type based on vtable info."), _("\
843 Show printing of object's derived type based on vtable info."), NULL,
846 &setprintlist, &showprintlist);
848 obstack_begin (&dont_print_stat_array_obstack,
849 32 * sizeof (struct type *));
850 obstack_begin (&dont_print_statmem_obstack,
851 32 * sizeof (CORE_ADDR));
852 obstack_begin (&dont_print_vb_obstack,
853 32 * sizeof (struct type *));