1 /* Support for printing C++ values for GDB, the GNU debugger.
3 Copyright (C) 1986, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
4 2000, 2001, 2002, 2003, 2005, 2006, 2007 Free 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 2 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, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
24 #include "gdb_obstack.h"
27 #include "expression.h"
33 #include "gdb_string.h"
38 #include "cp-support.h"
41 /* 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
56 show_objectprint (struct ui_file *file, int from_tty,
57 struct cmd_list_element *c,
60 fprintf_filtered (file, _("\
61 Printing of object's derived type based on vtable info is %s.\n"),
65 int static_field_print; /* Controls printing of static fields. */
67 show_static_field_print (struct ui_file *file, int from_tty,
68 struct cmd_list_element *c, const char *value)
70 fprintf_filtered (file, _("Printing of C++ static members is %s.\n"),
75 static struct obstack dont_print_vb_obstack;
76 static struct obstack dont_print_statmem_obstack;
78 extern void _initialize_cp_valprint (void);
80 static void cp_print_static_field (struct type *, struct value *,
81 struct ui_file *, int, int,
82 enum val_prettyprint);
84 static void cp_print_value (struct type *, struct type *, const gdb_byte *,
85 int, CORE_ADDR, struct ui_file *, int, int,
86 enum val_prettyprint, struct type **);
88 static void cp_print_hpacc_virtual_table_entries (struct type *, int *,
90 struct ui_file *, int,
92 enum val_prettyprint);
95 /* GCC versions after 2.4.5 use this. */
96 const char vtbl_ptr_name[] = "__vtbl_ptr_type";
98 /* HP aCC uses different names. */
99 const char hpacc_vtbl_ptr_name[] = "__vfp";
100 const char hpacc_vtbl_ptr_type_name[] = "__vftyp";
102 /* Return truth value for assertion that TYPE is of the type
103 "pointer to virtual function". */
106 cp_is_vtbl_ptr_type (struct type *type)
108 char *typename = type_name_no_tag (type);
110 return (typename != NULL && !strcmp (typename, vtbl_ptr_name));
113 /* Return truth value for the assertion that TYPE is of the type
114 "pointer to virtual function table". */
117 cp_is_vtbl_member (struct type *type)
119 /* With older versions of g++, the vtbl field pointed to an array
120 of structures. Nowadays it points directly to the structure. */
121 if (TYPE_CODE (type) == TYPE_CODE_PTR)
123 type = TYPE_TARGET_TYPE (type);
124 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
126 type = TYPE_TARGET_TYPE (type);
127 if (TYPE_CODE (type) == TYPE_CODE_STRUCT /* if not using thunks */
128 || TYPE_CODE (type) == TYPE_CODE_PTR) /* if using thunks */
130 /* Virtual functions tables are full of pointers
131 to virtual functions. */
132 return cp_is_vtbl_ptr_type (type);
135 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT) /* if not using thunks */
137 return cp_is_vtbl_ptr_type (type);
139 else if (TYPE_CODE (type) == TYPE_CODE_PTR) /* if using thunks */
141 /* The type name of the thunk pointer is NULL when using dwarf2.
142 We could test for a pointer to a function, but there is
143 no type info for the virtual table either, so it wont help. */
144 return cp_is_vtbl_ptr_type (type);
150 /* Mutually recursive subroutines of cp_print_value and c_val_print to
151 print out a structure's fields: cp_print_value_fields and cp_print_value.
153 TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and PRETTY have the
154 same meanings as in cp_print_value and c_val_print.
156 2nd argument REAL_TYPE is used to carry over the type of the derived
157 class across the recursion to base classes.
159 DONT_PRINT is an array of baseclass types that we
160 should not print, or zero if called from top level. */
163 cp_print_value_fields (struct type *type, struct type *real_type,
164 const gdb_byte *valaddr, int offset, CORE_ADDR address,
165 struct ui_file *stream, int format, int recurse,
166 enum val_prettyprint pretty,
167 struct type **dont_print_vb,int dont_print_statmem)
169 int i, len, n_baseclasses;
170 char *last_dont_print = obstack_next_free (&dont_print_statmem_obstack);
173 CHECK_TYPEDEF (type);
175 fprintf_filtered (stream, "{");
176 len = TYPE_NFIELDS (type);
177 n_baseclasses = TYPE_N_BASECLASSES (type);
179 /* First, print out baseclasses such that we don't print
180 duplicates of virtual baseclasses. */
182 if (n_baseclasses > 0)
183 cp_print_value (type, real_type, valaddr, offset, address, stream,
184 format, recurse + 1, pretty, dont_print_vb);
186 /* Second, print out data fields */
188 /* If there are no data fields, or if the only field is the
189 * vtbl pointer, skip this part */
190 if ((len == n_baseclasses)
191 || ((len - n_baseclasses == 1)
192 && TYPE_HAS_VTABLE (type)
193 && strncmp (TYPE_FIELD_NAME (type, n_baseclasses),
194 hpacc_vtbl_ptr_name, 5) == 0)
196 fprintf_filtered (stream, "<No data fields>");
199 struct obstack tmp_obstack = dont_print_statmem_obstack;
201 if (dont_print_statmem == 0)
203 /* If we're at top level, carve out a completely fresh
204 chunk of the obstack and use that until this particular
205 invocation returns. */
206 obstack_finish (&dont_print_statmem_obstack);
209 for (i = n_baseclasses; i < len; i++)
211 /* If requested, skip printing of static fields. */
212 if (!static_field_print && TYPE_FIELD_STATIC (type, i))
215 /* If a vtable pointer appears, we'll print it out later */
216 if (TYPE_HAS_VTABLE (type)
217 && strncmp (TYPE_FIELD_NAME (type, i), hpacc_vtbl_ptr_name,
222 fprintf_filtered (stream, ", ");
223 else if (n_baseclasses > 0)
227 fprintf_filtered (stream, "\n");
228 print_spaces_filtered (2 + 2 * recurse, stream);
229 fputs_filtered ("members of ", stream);
230 fputs_filtered (type_name_no_tag (type), stream);
231 fputs_filtered (": ", stream);
238 fprintf_filtered (stream, "\n");
239 print_spaces_filtered (2 + 2 * recurse, stream);
243 wrap_here (n_spaces (2 + 2 * recurse));
247 if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
248 fputs_filtered ("\"( ptr \"", stream);
250 fputs_filtered ("\"( nodef \"", stream);
251 if (TYPE_FIELD_STATIC (type, i))
252 fputs_filtered ("static ", stream);
253 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
254 current_language->la_language,
255 DMGL_PARAMS | DMGL_ANSI);
256 fputs_filtered ("\" \"", stream);
257 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
258 current_language->la_language,
259 DMGL_PARAMS | DMGL_ANSI);
260 fputs_filtered ("\") \"", stream);
264 annotate_field_begin (TYPE_FIELD_TYPE (type, i));
266 if (TYPE_FIELD_STATIC (type, i))
267 fputs_filtered ("static ", stream);
268 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
269 current_language->la_language,
270 DMGL_PARAMS | DMGL_ANSI);
271 annotate_field_name_end ();
272 /* do not print leading '=' in case of anonymous unions */
273 if (strcmp (TYPE_FIELD_NAME (type, i), ""))
274 fputs_filtered (" = ", stream);
275 annotate_field_value ();
278 if (!TYPE_FIELD_STATIC (type, i) && TYPE_FIELD_PACKED (type, i))
282 /* Bitfields require special handling, especially due to byte
284 if (TYPE_FIELD_IGNORE (type, i))
286 fputs_filtered ("<optimized out or zero length>", stream);
290 v = value_from_longest
291 (TYPE_FIELD_TYPE (type, i),
292 unpack_field_as_long (type, valaddr + offset, i));
294 common_val_print (v, stream, format, 0, recurse + 1, pretty);
299 if (TYPE_FIELD_IGNORE (type, i))
301 fputs_filtered ("<optimized out or zero length>", stream);
303 else if (TYPE_FIELD_STATIC (type, i))
305 struct value *v = value_static_field (type, i);
307 fputs_filtered ("<optimized out>", stream);
309 cp_print_static_field (TYPE_FIELD_TYPE (type, i), v,
310 stream, format, recurse + 1,
315 val_print (TYPE_FIELD_TYPE (type, i),
316 valaddr, offset + TYPE_FIELD_BITPOS (type, i) / 8,
317 address + TYPE_FIELD_BITPOS (type, i) / 8,
318 stream, format, 0, recurse + 1, pretty);
321 annotate_field_end ();
324 if (dont_print_statmem == 0)
326 /* Free the space used to deal with the printing
327 of the members from top level. */
328 obstack_free (&dont_print_statmem_obstack, last_dont_print);
329 dont_print_statmem_obstack = tmp_obstack;
334 fprintf_filtered (stream, "\n");
335 print_spaces_filtered (2 * recurse, stream);
337 } /* if there are data fields */
338 /* Now print out the virtual table pointer if there is one */
339 if (TYPE_HAS_VTABLE (type)
340 && strncmp (TYPE_FIELD_NAME (type, n_baseclasses),
341 hpacc_vtbl_ptr_name, 5) == 0)
344 /* First get the virtual table pointer and print it out */
347 fputs_filtered ("__vfp = ", stream);
350 fputs_filtered (", Virtual table at ", stream);
352 /* pai: FIXME 32x64 problem? */
353 /* Not sure what the best notation is in the case where there is no
355 v = value_from_pointer (lookup_pointer_type (builtin_type_unsigned_long),
356 *(unsigned long *) (valaddr + offset));
358 common_val_print (v, stream, format, 0, recurse + 1, pretty);
363 /* Print out function pointers in vtable. */
365 /* FIXME: then-clause is for non-RRBC layout of virtual
366 * table. The RRBC case in the else-clause is yet to be
367 * implemented. The if (1) below should be changed to a
368 * test for whether the executable we have was compiled
369 * with a version of HP aCC that doesn't have RRBC
374 /* no RRBC support; function pointers embedded directly
377 int vfuncs = count_virtual_fns (real_type);
379 fputs_filtered (" {", stream);
381 /* FIXME : doesn't work at present */
383 fprintf_filtered (stream, "%d entr%s: ", vfuncs,
384 vfuncs == 1 ? "y" : "ies");
386 fputs_filtered ("not implemented", stream);
391 /* recursive function that prints all virtual function entries */
393 cp_print_hpacc_virtual_table_entries (real_type, &vfuncs, v,
394 stream, format, recurse,
397 fputs_filtered ("}", stream);
398 } /* non-RRBC case */
401 /* FIXME -- see comments above */
402 /* RRBC support present; function pointers are found
403 * by indirection through the class segment entries. */
411 fprintf_filtered (stream, "\n");
412 print_spaces_filtered (2 * recurse, stream);
415 } /* if vtable exists */
417 fprintf_filtered (stream, "}");
420 /* Special val_print routine to avoid printing multiple copies of virtual
424 cp_print_value (struct type *type, struct type *real_type,
425 const gdb_byte *valaddr, int offset, CORE_ADDR address,
426 struct ui_file *stream, int format, int recurse,
427 enum val_prettyprint pretty, struct type **dont_print_vb)
429 struct type **last_dont_print
430 = (struct type **) obstack_next_free (&dont_print_vb_obstack);
431 struct obstack tmp_obstack = dont_print_vb_obstack;
432 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
434 struct type *thistype;
436 if (dont_print_vb == 0)
438 /* If we're at top level, carve out a completely fresh
439 chunk of the obstack and use that until this particular
440 invocation returns. */
441 /* Bump up the high-water mark. Now alpha is omega. */
442 obstack_finish (&dont_print_vb_obstack);
445 for (i = 0; i < n_baseclasses; i++)
449 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
450 char *basename = TYPE_NAME (baseclass);
451 const gdb_byte *base_valaddr;
453 if (BASETYPE_VIA_VIRTUAL (type, i))
455 struct type **first_dont_print
456 = (struct type **) obstack_base (&dont_print_vb_obstack);
458 int j = (struct type **) obstack_next_free (&dont_print_vb_obstack)
462 if (baseclass == first_dont_print[j])
465 obstack_ptr_grow (&dont_print_vb_obstack, baseclass);
469 thistype = real_type;
470 if (TYPE_HAS_VTABLE (type) && BASETYPE_VIA_VIRTUAL (type, i))
472 /* Assume HP/Taligent runtime convention */
473 find_rt_vbase_offset (type, TYPE_BASECLASS (type, i),
474 valaddr, offset, &boffset, &skip);
476 error (_("Virtual base class offset not found from vtable while"
478 base_valaddr = valaddr;
482 boffset = baseclass_offset (type, i,
485 skip = ((boffset == -1) || (boffset + offset) < 0) ? 1 : -1;
487 if (BASETYPE_VIA_VIRTUAL (type, i))
489 /* The virtual base class pointer might have been
490 clobbered by the user program. Make sure that it
491 still points to a valid memory location. */
494 && ((boffset + offset) < 0
495 || (boffset + offset) >= TYPE_LENGTH (type)))
497 /* FIXME (alloca): unsafe if baseclass is really really large. */
498 gdb_byte *buf = alloca (TYPE_LENGTH (baseclass));
500 if (target_read_memory (address + boffset, buf,
501 TYPE_LENGTH (baseclass)) != 0)
503 address = address + boffset;
506 thistype = baseclass;
509 base_valaddr = valaddr;
512 base_valaddr = valaddr;
515 /* now do the printing */
518 fprintf_filtered (stream, "\n");
519 print_spaces_filtered (2 * recurse, stream);
521 fputs_filtered ("<", stream);
522 /* Not sure what the best notation is in the case where there is no
524 fputs_filtered (basename ? basename : "", stream);
525 fputs_filtered ("> = ", stream);
529 fprintf_filtered (stream, "<invalid address>");
531 cp_print_value_fields (baseclass, thistype, base_valaddr,
532 thisoffset + boffset, address + boffset,
536 obstack_base (&dont_print_vb_obstack)),
538 fputs_filtered (", ", stream);
544 if (dont_print_vb == 0)
546 /* Free the space used to deal with the printing
547 of this type from top level. */
548 obstack_free (&dont_print_vb_obstack, last_dont_print);
549 /* Reset watermark so that we can continue protecting
550 ourselves from whatever we were protecting ourselves. */
551 dont_print_vb_obstack = tmp_obstack;
555 /* Print value of a static member.
556 To avoid infinite recursion when printing a class that contains
557 a static instance of the class, we keep the addresses of all printed
558 static member classes in an obstack and refuse to print them more
561 VAL contains the value to print, TYPE, STREAM, RECURSE, and PRETTY
562 have the same meanings as in c_val_print. */
565 cp_print_static_field (struct type *type,
567 struct ui_file *stream,
570 enum val_prettyprint pretty)
572 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
574 CORE_ADDR *first_dont_print;
578 = (CORE_ADDR *) obstack_base (&dont_print_statmem_obstack);
579 i = (CORE_ADDR *) obstack_next_free (&dont_print_statmem_obstack)
584 if (VALUE_ADDRESS (val) == first_dont_print[i])
586 fputs_filtered ("<same as static member of an already"
593 obstack_grow (&dont_print_statmem_obstack, (char *) &VALUE_ADDRESS (val),
596 CHECK_TYPEDEF (type);
597 cp_print_value_fields (type, type, value_contents_all (val),
598 value_embedded_offset (val), VALUE_ADDRESS (val),
599 stream, format, recurse, pretty, NULL, 1);
602 val_print (type, value_contents_all (val),
603 value_embedded_offset (val), VALUE_ADDRESS (val),
604 stream, format, 0, recurse, pretty);
608 /* Find the field in *DOMAIN, or its non-virtual base classes, with bit offset
609 OFFSET. Set *DOMAIN to the containing type and *FIELDNO to the containing
610 field number. If OFFSET is not exactly at the start of some field, set
614 cp_find_class_member (struct type **domain_p, int *fieldno,
621 *domain_p = check_typedef (*domain_p);
623 len = TYPE_NFIELDS (domain);
625 for (i = TYPE_N_BASECLASSES (domain); i < len; i++)
627 LONGEST bitpos = TYPE_FIELD_BITPOS (domain, i);
630 if (offset == bitpos)
637 for (i = 0; i < TYPE_N_BASECLASSES (domain); i++)
639 LONGEST bitpos = TYPE_FIELD_BITPOS (domain, i);
640 LONGEST bitsize = 8 * TYPE_LENGTH (TYPE_FIELD_TYPE (domain, i));
642 if (offset >= bitpos && offset < bitpos + bitsize)
644 *domain_p = TYPE_FIELD_TYPE (domain, i);
645 cp_find_class_member (domain_p, fieldno, offset - bitpos);
654 cp_print_class_member (const gdb_byte *valaddr, struct type *domain,
655 struct ui_file *stream, char *prefix)
657 /* VAL is a byte offset into the structure type DOMAIN.
658 Find the name of the field for that offset and
660 unsigned int fieldno;
662 LONGEST val = unpack_long (builtin_type_long, valaddr);
664 /* Pointers to data members are usually byte offsets into an object.
665 Because a data member can have offset zero, and a NULL pointer to
666 member must be distinct from any valid non-NULL pointer to
667 member, either the value is biased or the NULL value has a
668 special representation; both are permitted by ISO C++. HP aCC
669 used a bias of 0x20000000; HP cfront used a bias of 1; g++ 3.x
670 and other compilers which use the Itanium ABI use -1 as the NULL
671 value. GDB only supports that last form; to add support for
672 another form, make this into a cp-abi hook. */
676 fprintf_filtered (stream, "NULL");
680 cp_find_class_member (&domain, &fieldno, val << 3);
685 fputs_filtered (prefix, stream);
686 name = type_name_no_tag (domain);
688 fputs_filtered (name, stream);
690 c_type_print_base (domain, stream, 0, 0);
691 fprintf_filtered (stream, "::");
692 fputs_filtered (TYPE_FIELD_NAME (domain, fieldno), stream);
695 fprintf_filtered (stream, "%ld", (long) val);
699 /* This function prints out virtual table entries for a class; it
700 * recurses on the base classes to find all virtual functions
701 * available in a class.
703 * pai/1997-05-21 Note: As the name suggests, it's currently
704 * implemented for HP aCC runtime only. g++ objects are handled
705 * differently and I have made no attempt to fold that logic in
706 * here. The runtime layout is different for the two cases. Also,
707 * this currently has only the code for non-RRBC layouts generated by
708 * the HP aCC compiler; RRBC code is stubbed out and will have to be
713 cp_print_hpacc_virtual_table_entries (struct type *type, int *vfuncs,
714 struct value *v, struct ui_file *stream,
715 int format, int recurse,
716 enum val_prettyprint pretty)
720 /* pai: FIXME this function doesn't work. It should handle a given
721 * virtual function only once (latest redefinition in class hierarchy)
724 /* Recursion on other classes that can share the same vtable */
725 struct type *pbc = primary_base_class (type);
727 cp_print_hpacc_virtual_table_entries (pbc, vfuncs, v, stream, format,
730 /* Now deal with vfuncs declared in this class */
731 for (fn = 0; fn < TYPE_NFN_FIELDS (type); fn++)
732 for (oi = 0; oi < TYPE_FN_FIELDLIST_LENGTH (type, fn); oi++)
733 if (TYPE_FN_FIELD_VIRTUAL_P (TYPE_FN_FIELDLIST1 (type, fn), oi))
736 const char *field_physname;
738 /* virtual function offset */
739 int vx = (TYPE_FN_FIELD_VOFFSET (TYPE_FN_FIELDLIST1 (type, fn), oi)
742 /* Get the address of the vfunction entry */
743 struct value *vf = value_copy (v);
745 (void) value_fetch_lazy (vf);
746 /* adjust by offset */
747 /* NOTE: cagney/2005-01-02: THIS IS BOGUS. */
748 value_contents_writeable (vf)[0] += 4 * (HP_ACC_VFUNC_START + vx);
749 vf = value_ind (vf); /* get the entry */
750 /* make it a pointer */
751 deprecated_set_value_type (vf, value_type (v));
753 /* print out the entry */
754 common_val_print (vf, stream, format, 0, recurse + 1, pretty);
756 = TYPE_FN_FIELD_PHYSNAME (TYPE_FN_FIELDLIST1 (type, fn), oi);
757 /* pai: (temp) FIXME Maybe this should be DMGL_ANSI */
758 vf_name = cplus_demangle (field_physname, DMGL_ARM);
759 fprintf_filtered (stream, " %s", vf_name);
762 fputs_filtered (", ", stream);
769 _initialize_cp_valprint (void)
771 add_setshow_boolean_cmd ("static-members", class_support,
772 &static_field_print, _("\
773 Set printing of C++ static members."), _("\
774 Show printing of C++ static members."), NULL,
776 show_static_field_print,
777 &setprintlist, &showprintlist);
778 /* Turn on printing of static fields. */
779 static_field_print = 1;
781 add_setshow_boolean_cmd ("vtbl", class_support, &vtblprint, _("\
782 Set printing of C++ virtual function tables."), _("\
783 Show printing of C++ virtual function tables."), NULL,
786 &setprintlist, &showprintlist);
788 add_setshow_boolean_cmd ("object", class_support, &objectprint, _("\
789 Set printing of object's derived type based on vtable info."), _("\
790 Show printing of object's derived type based on vtable info."), NULL,
793 &setprintlist, &showprintlist);
795 /* Give people the defaults which they are used to. */
798 obstack_begin (&dont_print_vb_obstack, 32 * sizeof (struct type *));
799 obstack_specify_allocation (&dont_print_statmem_obstack,
800 32 * sizeof (CORE_ADDR), sizeof (CORE_ADDR),