1 /* Support for printing C++ values for GDB, the GNU debugger.
3 Copyright (C) 1986, 1988-1989, 1991-1997, 2000-2003, 2005-2012 Free
4 Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "gdb_obstack.h"
25 #include "expression.h"
31 #include "gdb_string.h"
36 #include "cp-support.h"
38 #include "python/python.h"
39 #include "exceptions.h"
40 #include "typeprint.h"
42 /* Controls printing of vtbl's. */
44 show_vtblprint (struct ui_file *file, int from_tty,
45 struct cmd_list_element *c, const char *value)
47 fprintf_filtered (file, _("\
48 Printing of C++ virtual function tables is %s.\n"),
52 /* Controls looking up an object's derived type using what we find in
55 show_objectprint (struct ui_file *file, int from_tty,
56 struct cmd_list_element *c,
59 fprintf_filtered (file, _("\
60 Printing of object's derived type based on vtable info is %s.\n"),
65 show_static_field_print (struct ui_file *file, int from_tty,
66 struct cmd_list_element *c,
69 fprintf_filtered (file,
70 _("Printing of C++ static members is %s.\n"),
75 static struct obstack dont_print_vb_obstack;
76 static struct obstack dont_print_statmem_obstack;
77 static struct obstack dont_print_stat_array_obstack;
79 extern void _initialize_cp_valprint (void);
81 static void cp_print_static_field (struct type *, struct value *,
82 struct ui_file *, int,
83 const struct value_print_options *);
85 static void cp_print_value (struct type *, struct type *,
86 const gdb_byte *, int,
87 CORE_ADDR, struct ui_file *,
88 int, const struct value *,
89 const struct value_print_options *,
93 /* GCC versions after 2.4.5 use this. */
94 const char vtbl_ptr_name[] = "__vtbl_ptr_type";
96 /* Return truth value for assertion that TYPE is of the type
97 "pointer to virtual function". */
100 cp_is_vtbl_ptr_type (struct type *type)
102 const char *typename = type_name_no_tag (type);
104 return (typename != NULL && !strcmp (typename, vtbl_ptr_name));
107 /* Return truth value for the assertion that TYPE is of the type
108 "pointer to virtual function table". */
111 cp_is_vtbl_member (struct type *type)
113 /* With older versions of g++, the vtbl field pointed to an array of
114 structures. Nowadays it points directly to the structure. */
115 if (TYPE_CODE (type) == TYPE_CODE_PTR)
117 type = TYPE_TARGET_TYPE (type);
118 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
120 type = TYPE_TARGET_TYPE (type);
121 if (TYPE_CODE (type) == TYPE_CODE_STRUCT /* if not using thunks */
122 || TYPE_CODE (type) == TYPE_CODE_PTR) /* if using thunks */
124 /* Virtual functions tables are full of pointers
125 to virtual functions. */
126 return cp_is_vtbl_ptr_type (type);
129 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT) /* if not using thunks */
131 return cp_is_vtbl_ptr_type (type);
133 else if (TYPE_CODE (type) == TYPE_CODE_PTR) /* if using thunks */
135 /* The type name of the thunk pointer is NULL when using
136 dwarf2. We could test for a pointer to a function, but
137 there is no type info for the virtual table either, so it
139 return cp_is_vtbl_ptr_type (type);
145 /* Mutually recursive subroutines of cp_print_value and c_val_print to
146 print out a structure's fields: cp_print_value_fields and
149 TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and OPTIONS have the same
150 meanings as in cp_print_value and c_val_print.
152 2nd argument REAL_TYPE is used to carry over the type of the
153 derived class across the recursion to base classes.
155 DONT_PRINT is an array of baseclass types that we should not print,
156 or zero if called from top level. */
159 cp_print_value_fields (struct type *type, struct type *real_type,
160 const gdb_byte *valaddr, int offset,
161 CORE_ADDR address, struct ui_file *stream,
162 int recurse, const struct value *val,
163 const struct value_print_options *options,
164 struct type **dont_print_vb,
165 int dont_print_statmem)
167 int i, len, n_baseclasses;
169 static int last_set_recurse = -1;
171 CHECK_TYPEDEF (type);
175 /* Any object can be left on obstacks only during an unexpected
178 if (obstack_object_size (&dont_print_statmem_obstack) > 0)
180 obstack_free (&dont_print_statmem_obstack, NULL);
181 obstack_begin (&dont_print_statmem_obstack,
182 32 * sizeof (CORE_ADDR));
184 if (obstack_object_size (&dont_print_stat_array_obstack) > 0)
186 obstack_free (&dont_print_stat_array_obstack, NULL);
187 obstack_begin (&dont_print_stat_array_obstack,
188 32 * sizeof (struct type *));
192 fprintf_filtered (stream, "{");
193 len = TYPE_NFIELDS (type);
194 n_baseclasses = TYPE_N_BASECLASSES (type);
196 /* First, print out baseclasses such that we don't print
197 duplicates of virtual baseclasses. */
199 if (n_baseclasses > 0)
200 cp_print_value (type, real_type, valaddr,
201 offset, address, stream,
202 recurse + 1, val, options,
205 /* Second, print out data fields */
207 /* If there are no data fields, skip this part */
208 if (len == n_baseclasses || !len)
209 fprintf_filtered (stream, "<No data fields>");
212 int statmem_obstack_initial_size = 0;
213 int stat_array_obstack_initial_size = 0;
214 struct type *vptr_basetype = NULL;
217 if (dont_print_statmem == 0)
219 statmem_obstack_initial_size =
220 obstack_object_size (&dont_print_statmem_obstack);
222 if (last_set_recurse != recurse)
224 stat_array_obstack_initial_size =
225 obstack_object_size (&dont_print_stat_array_obstack);
227 last_set_recurse = recurse;
231 vptr_fieldno = get_vptr_fieldno (type, &vptr_basetype);
232 for (i = n_baseclasses; i < len; i++)
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)
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);
256 fprintf_filtered (stream, "\n");
257 print_spaces_filtered (2 + 2 * recurse, stream);
261 wrap_here (n_spaces (2 + 2 * recurse));
263 if (options->inspect_it)
265 if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
266 fputs_filtered ("\"( ptr \"", stream);
268 fputs_filtered ("\"( nodef \"", stream);
269 if (field_is_static (&TYPE_FIELD (type, i)))
270 fputs_filtered ("static ", stream);
271 fprintf_symbol_filtered (stream,
272 TYPE_FIELD_NAME (type, i),
273 current_language->la_language,
274 DMGL_PARAMS | DMGL_ANSI);
275 fputs_filtered ("\" \"", stream);
276 fprintf_symbol_filtered (stream,
277 TYPE_FIELD_NAME (type, i),
278 current_language->la_language,
279 DMGL_PARAMS | DMGL_ANSI);
280 fputs_filtered ("\") \"", stream);
284 annotate_field_begin (TYPE_FIELD_TYPE (type, i));
286 if (field_is_static (&TYPE_FIELD (type, i)))
287 fputs_filtered ("static ", stream);
288 fprintf_symbol_filtered (stream,
289 TYPE_FIELD_NAME (type, i),
290 current_language->la_language,
291 DMGL_PARAMS | DMGL_ANSI);
292 annotate_field_name_end ();
293 /* Do not print leading '=' in case of anonymous
295 if (strcmp (TYPE_FIELD_NAME (type, i), ""))
296 fputs_filtered (" = ", stream);
297 annotate_field_value ();
300 if (!field_is_static (&TYPE_FIELD (type, i))
301 && TYPE_FIELD_PACKED (type, i))
305 /* Bitfields require special handling, especially due to
306 byte order problems. */
307 if (TYPE_FIELD_IGNORE (type, i))
309 fputs_filtered ("<optimized out or zero length>", stream);
311 else if (value_bits_synthetic_pointer (val,
312 TYPE_FIELD_BITPOS (type,
314 TYPE_FIELD_BITSIZE (type,
317 fputs_filtered (_("<synthetic pointer>"), stream);
319 else if (!value_bits_valid (val,
320 TYPE_FIELD_BITPOS (type, i),
321 TYPE_FIELD_BITSIZE (type, i)))
323 val_print_optimized_out (stream);
327 struct value_print_options opts = *options;
331 v = value_field_bitfield (type, i, valaddr, offset, val);
333 common_val_print (v, stream, recurse + 1, &opts,
339 if (TYPE_FIELD_IGNORE (type, i))
341 fputs_filtered ("<optimized out or zero length>",
344 else if (field_is_static (&TYPE_FIELD (type, i)))
346 volatile struct gdb_exception ex;
347 struct value *v = NULL;
349 TRY_CATCH (ex, RETURN_MASK_ERROR)
351 v = value_static_field (type, i);
355 fprintf_filtered (stream,
356 _("<error reading variable: %s>"),
359 val_print_optimized_out (stream);
361 cp_print_static_field (TYPE_FIELD_TYPE (type, i),
362 v, stream, recurse + 1,
365 else if (i == vptr_fieldno && type == vptr_basetype)
367 int i_offset = offset + TYPE_FIELD_BITPOS (type, i) / 8;
368 struct type *i_type = TYPE_FIELD_TYPE (type, i);
370 if (valprint_check_validity (stream, i_type, i_offset, val))
374 addr = extract_typed_address (valaddr + i_offset, i_type);
375 print_function_pointer_address (options,
376 get_type_arch (type),
382 struct value_print_options opts = *options;
385 val_print (TYPE_FIELD_TYPE (type, i),
387 offset + TYPE_FIELD_BITPOS (type, i) / 8,
389 stream, recurse + 1, val, &opts,
393 annotate_field_end ();
396 if (dont_print_statmem == 0)
398 int obstack_final_size =
399 obstack_object_size (&dont_print_statmem_obstack);
401 if (obstack_final_size > statmem_obstack_initial_size)
403 /* In effect, a pop of the printed-statics stack. */
406 obstack_next_free (&dont_print_statmem_obstack) -
407 (obstack_final_size - statmem_obstack_initial_size);
409 obstack_free (&dont_print_statmem_obstack,
413 if (last_set_recurse != recurse)
415 int obstack_final_size =
416 obstack_object_size (&dont_print_stat_array_obstack);
418 if (obstack_final_size > stat_array_obstack_initial_size)
421 obstack_next_free (&dont_print_stat_array_obstack)
422 - (obstack_final_size
423 - stat_array_obstack_initial_size);
425 obstack_free (&dont_print_stat_array_obstack,
428 last_set_recurse = -1;
434 fprintf_filtered (stream, "\n");
435 print_spaces_filtered (2 * recurse, stream);
437 } /* if there are data fields */
439 fprintf_filtered (stream, "}");
442 /* Like cp_print_value_fields, but find the runtime type of the object
443 and pass it as the `real_type' argument to cp_print_value_fields.
444 This function is a hack to work around the fact that
445 common_val_print passes the embedded offset to val_print, but not
446 the enclosing type. */
449 cp_print_value_fields_rtti (struct type *type,
450 const gdb_byte *valaddr, int offset,
452 struct ui_file *stream, int recurse,
453 const struct value *val,
454 const struct value_print_options *options,
455 struct type **dont_print_vb,
456 int dont_print_statmem)
458 struct type *real_type = NULL;
460 /* We require all bits to be valid in order to attempt a
462 if (value_bits_valid (val, TARGET_CHAR_BIT * offset,
463 TARGET_CHAR_BIT * TYPE_LENGTH (type)))
466 int full, top, using_enc;
468 /* Ugh, we have to convert back to a value here. */
469 value = value_from_contents_and_address (type, valaddr + offset,
471 /* We don't actually care about most of the result here -- just
472 the type. We already have the correct offset, due to how
473 val_print was initially called. */
474 real_type = value_rtti_type (value, &full, &top, &using_enc);
480 cp_print_value_fields (type, real_type, valaddr, offset,
481 address, stream, recurse, val, options,
482 dont_print_vb, dont_print_statmem);
485 /* Special val_print routine to avoid printing multiple copies of
486 virtual baseclasses. */
489 cp_print_value (struct type *type, struct type *real_type,
490 const gdb_byte *valaddr, int offset,
491 CORE_ADDR address, struct ui_file *stream,
492 int recurse, const struct value *val,
493 const struct value_print_options *options,
494 struct type **dont_print_vb)
496 struct type **last_dont_print
497 = (struct type **) obstack_next_free (&dont_print_vb_obstack);
498 struct obstack tmp_obstack = dont_print_vb_obstack;
499 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
501 struct type *thistype;
503 if (dont_print_vb == 0)
505 /* If we're at top level, carve out a completely fresh chunk of
506 the obstack and use that until this particular invocation
508 /* Bump up the high-water mark. Now alpha is omega. */
509 obstack_finish (&dont_print_vb_obstack);
512 for (i = 0; i < n_baseclasses; i++)
516 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
517 const char *basename = TYPE_NAME (baseclass);
518 const gdb_byte *base_valaddr = NULL;
519 const struct value *base_val = NULL;
520 volatile struct gdb_exception ex;
522 if (BASETYPE_VIA_VIRTUAL (type, i))
524 struct type **first_dont_print
525 = (struct type **) obstack_base (&dont_print_vb_obstack);
527 int j = (struct type **)
528 obstack_next_free (&dont_print_vb_obstack) - first_dont_print;
531 if (baseclass == first_dont_print[j])
534 obstack_ptr_grow (&dont_print_vb_obstack, baseclass);
538 thistype = real_type;
540 TRY_CATCH (ex, RETURN_MASK_ERROR)
542 boffset = baseclass_offset (type, i, valaddr, offset, address, val);
544 if (ex.reason < 0 && ex.error == NOT_AVAILABLE_ERROR)
546 else if (ex.reason < 0)
552 if (BASETYPE_VIA_VIRTUAL (type, i))
554 /* The virtual base class pointer might have been
555 clobbered by the user program. Make sure that it
556 still points to a valid memory location. */
558 if ((boffset + offset) < 0
559 || (boffset + offset) >= TYPE_LENGTH (real_type))
562 struct cleanup *back_to;
564 buf = xmalloc (TYPE_LENGTH (baseclass));
565 back_to = make_cleanup (xfree, buf);
567 if (target_read_memory (address + boffset, buf,
568 TYPE_LENGTH (baseclass)) != 0)
570 base_val = value_from_contents_and_address (baseclass,
575 thistype = baseclass;
576 base_valaddr = value_contents_for_printing_const (base_val);
577 do_cleanups (back_to);
581 base_valaddr = valaddr;
587 base_valaddr = valaddr;
592 /* Now do the printing. */
595 fprintf_filtered (stream, "\n");
596 print_spaces_filtered (2 * recurse, stream);
598 fputs_filtered ("<", stream);
599 /* Not sure what the best notation is in the case where there is
600 no baseclass name. */
601 fputs_filtered (basename ? basename : "", stream);
602 fputs_filtered ("> = ", stream);
605 val_print_unavailable (stream);
607 val_print_invalid_address (stream);
612 /* Attempt to run the Python pretty-printers on the
613 baseclass if possible. */
615 result = apply_val_pretty_printer (baseclass, base_valaddr,
616 thisoffset + boffset,
617 value_address (base_val),
618 stream, recurse, base_val,
619 options, current_language);
624 cp_print_value_fields (baseclass, thistype, base_valaddr,
625 thisoffset + boffset,
626 value_address (base_val),
627 stream, recurse, base_val, options,
629 obstack_base (&dont_print_vb_obstack)),
632 fputs_filtered (", ", stream);
638 if (dont_print_vb == 0)
640 /* Free the space used to deal with the printing
641 of this type from top level. */
642 obstack_free (&dont_print_vb_obstack, last_dont_print);
643 /* Reset watermark so that we can continue protecting
644 ourselves from whatever we were protecting ourselves. */
645 dont_print_vb_obstack = tmp_obstack;
649 /* Print value of a static member. To avoid infinite recursion when
650 printing a class that contains a static instance of the class, we
651 keep the addresses of all printed static member classes in an
652 obstack and refuse to print them more than once.
654 VAL contains the value to print, TYPE, STREAM, RECURSE, and OPTIONS
655 have the same meanings as in c_val_print. */
658 cp_print_static_field (struct type *type,
660 struct ui_file *stream,
662 const struct value_print_options *options)
664 struct value_print_options opts;
666 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
668 CORE_ADDR *first_dont_print;
673 = (CORE_ADDR *) obstack_base (&dont_print_statmem_obstack);
674 i = obstack_object_size (&dont_print_statmem_obstack)
675 / sizeof (CORE_ADDR);
679 if (value_address (val) == first_dont_print[i])
681 fputs_filtered ("<same as static member of an already"
688 addr = value_address (val);
689 obstack_grow (&dont_print_statmem_obstack, (char *) &addr,
691 CHECK_TYPEDEF (type);
692 cp_print_value_fields (type, value_enclosing_type (val),
693 value_contents_for_printing (val),
694 value_embedded_offset (val), addr,
695 stream, recurse, val,
700 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
702 struct type **first_dont_print;
704 struct type *target_type = TYPE_TARGET_TYPE (type);
707 = (struct type **) obstack_base (&dont_print_stat_array_obstack);
708 i = obstack_object_size (&dont_print_stat_array_obstack)
709 / sizeof (struct type *);
713 if (target_type == first_dont_print[i])
715 fputs_filtered ("<same as static member of an already"
722 obstack_grow (&dont_print_stat_array_obstack,
723 (char *) &target_type,
724 sizeof (struct type *));
729 val_print (type, value_contents_for_printing (val),
730 value_embedded_offset (val),
732 stream, recurse, val,
733 &opts, current_language);
737 /* Find the field in *DOMAIN, or its non-virtual base classes, with
738 bit offset OFFSET. Set *DOMAIN to the containing type and *FIELDNO
739 to the containing field number. If OFFSET is not exactly at the
740 start of some field, set *DOMAIN to NULL. */
743 cp_find_class_member (struct type **domain_p, int *fieldno,
750 *domain_p = check_typedef (*domain_p);
752 len = TYPE_NFIELDS (domain);
754 for (i = TYPE_N_BASECLASSES (domain); i < len; i++)
756 LONGEST bitpos = TYPE_FIELD_BITPOS (domain, i);
759 if (offset == bitpos)
766 for (i = 0; i < TYPE_N_BASECLASSES (domain); i++)
768 LONGEST bitpos = TYPE_FIELD_BITPOS (domain, i);
769 LONGEST bitsize = 8 * TYPE_LENGTH (TYPE_FIELD_TYPE (domain, i));
771 if (offset >= bitpos && offset < bitpos + bitsize)
773 *domain_p = TYPE_FIELD_TYPE (domain, i);
774 cp_find_class_member (domain_p, fieldno, offset - bitpos);
783 cp_print_class_member (const gdb_byte *valaddr, struct type *type,
784 struct ui_file *stream, char *prefix)
786 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
788 /* VAL is a byte offset into the structure type DOMAIN.
789 Find the name of the field for that offset and
791 struct type *domain = TYPE_DOMAIN_TYPE (type);
793 unsigned int fieldno;
795 val = extract_signed_integer (valaddr,
799 /* Pointers to data members are usually byte offsets into an object.
800 Because a data member can have offset zero, and a NULL pointer to
801 member must be distinct from any valid non-NULL pointer to
802 member, either the value is biased or the NULL value has a
803 special representation; both are permitted by ISO C++. HP aCC
804 used a bias of 0x20000000; HP cfront used a bias of 1; g++ 3.x
805 and other compilers which use the Itanium ABI use -1 as the NULL
806 value. GDB only supports that last form; to add support for
807 another form, make this into a cp-abi hook. */
811 fprintf_filtered (stream, "NULL");
815 cp_find_class_member (&domain, &fieldno, val << 3);
821 fputs_filtered (prefix, stream);
822 name = type_name_no_tag (domain);
824 fputs_filtered (name, stream);
826 c_type_print_base (domain, stream, 0, 0, &type_print_raw_options);
827 fprintf_filtered (stream, "::");
828 fputs_filtered (TYPE_FIELD_NAME (domain, fieldno), stream);
831 fprintf_filtered (stream, "%ld", (long) val);
836 _initialize_cp_valprint (void)
838 add_setshow_boolean_cmd ("static-members", class_support,
839 &user_print_options.static_field_print, _("\
840 Set printing of C++ static members."), _("\
841 Show printing of C++ static members."), NULL,
843 show_static_field_print,
844 &setprintlist, &showprintlist);
846 add_setshow_boolean_cmd ("vtbl", class_support,
847 &user_print_options.vtblprint, _("\
848 Set printing of C++ virtual function tables."), _("\
849 Show printing of C++ virtual function tables."), NULL,
852 &setprintlist, &showprintlist);
854 add_setshow_boolean_cmd ("object", class_support,
855 &user_print_options.objectprint, _("\
856 Set printing of object's derived type based on vtable info."), _("\
857 Show printing of object's derived type based on vtable info."), NULL,
860 &setprintlist, &showprintlist);
862 obstack_begin (&dont_print_stat_array_obstack,
863 32 * sizeof (struct type *));
864 obstack_begin (&dont_print_statmem_obstack,
865 32 * sizeof (CORE_ADDR));
866 obstack_begin (&dont_print_vb_obstack,
867 32 * sizeof (struct type *));