1 /* Support for printing C++ values for GDB, the GNU debugger.
3 Copyright (C) 1986, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996,
4 1997, 2000, 2001, 2002, 2003, 2005, 2006
5 Free Software Foundation, Inc.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA. */
25 #include "gdb_obstack.h"
28 #include "expression.h"
34 #include "gdb_string.h"
39 #include "cp-support.h"
42 /* Controls printing of vtbl's */
45 show_vtblprint (struct ui_file *file, int from_tty,
46 struct cmd_list_element *c, const char *value)
48 fprintf_filtered (file, _("\
49 Printing of C++ virtual function tables is %s.\n"),
53 /* Controls looking up an object's derived type using what we find in
57 show_objectprint (struct ui_file *file, int from_tty,
58 struct cmd_list_element *c,
61 fprintf_filtered (file, _("\
62 Printing of object's derived type based on vtable info is %s.\n"),
66 int static_field_print; /* Controls printing of static fields. */
68 show_static_field_print (struct ui_file *file, int from_tty,
69 struct cmd_list_element *c, const char *value)
71 fprintf_filtered (file, _("Printing of C++ static members is %s.\n"),
76 static struct obstack dont_print_vb_obstack;
77 static struct obstack dont_print_statmem_obstack;
79 extern void _initialize_cp_valprint (void);
81 static void cp_print_static_field (struct type *, struct value *,
82 struct ui_file *, int, int,
83 enum val_prettyprint);
85 static void cp_print_value (struct type *, struct type *, const gdb_byte *,
86 int, CORE_ADDR, struct ui_file *, int, int,
87 enum val_prettyprint, struct type **);
89 static void cp_print_hpacc_virtual_table_entries (struct type *, int *,
91 struct ui_file *, int,
93 enum val_prettyprint);
96 /* GCC versions after 2.4.5 use this. */
97 const char vtbl_ptr_name[] = "__vtbl_ptr_type";
99 /* HP aCC uses different names. */
100 const char hpacc_vtbl_ptr_name[] = "__vfp";
101 const char hpacc_vtbl_ptr_type_name[] = "__vftyp";
103 /* Return truth value for assertion that TYPE is of the type
104 "pointer to virtual function". */
107 cp_is_vtbl_ptr_type (struct type *type)
109 char *typename = type_name_no_tag (type);
111 return (typename != NULL && !strcmp (typename, vtbl_ptr_name));
114 /* Return truth value for the assertion that TYPE is of the type
115 "pointer to virtual function table". */
118 cp_is_vtbl_member (struct type *type)
120 /* With older versions of g++, the vtbl field pointed to an array
121 of structures. Nowadays it points directly to the structure. */
122 if (TYPE_CODE (type) == TYPE_CODE_PTR)
124 type = TYPE_TARGET_TYPE (type);
125 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
127 type = TYPE_TARGET_TYPE (type);
128 if (TYPE_CODE (type) == TYPE_CODE_STRUCT /* if not using thunks */
129 || TYPE_CODE (type) == TYPE_CODE_PTR) /* if using thunks */
131 /* Virtual functions tables are full of pointers
132 to virtual functions. */
133 return cp_is_vtbl_ptr_type (type);
136 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT) /* if not using thunks */
138 return cp_is_vtbl_ptr_type (type);
140 else if (TYPE_CODE (type) == TYPE_CODE_PTR) /* if using thunks */
142 /* The type name of the thunk pointer is NULL when using dwarf2.
143 We could test for a pointer to a function, but there is
144 no type info for the virtual table either, so it wont help. */
145 return cp_is_vtbl_ptr_type (type);
151 /* Mutually recursive subroutines of cp_print_value and c_val_print to
152 print out a structure's fields: cp_print_value_fields and cp_print_value.
154 TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and PRETTY have the
155 same meanings as in cp_print_value and c_val_print.
157 2nd argument REAL_TYPE is used to carry over the type of the derived
158 class across the recursion to base classes.
160 DONT_PRINT is an array of baseclass types that we
161 should not print, or zero if called from top level. */
164 cp_print_value_fields (struct type *type, struct type *real_type,
165 const gdb_byte *valaddr, int offset, CORE_ADDR address,
166 struct ui_file *stream, int format, int recurse,
167 enum val_prettyprint pretty,
168 struct type **dont_print_vb,int dont_print_statmem)
170 int i, len, n_baseclasses;
171 struct obstack tmp_obstack;
172 char *last_dont_print = obstack_next_free (&dont_print_statmem_obstack);
175 CHECK_TYPEDEF (type);
177 fprintf_filtered (stream, "{");
178 len = TYPE_NFIELDS (type);
179 n_baseclasses = TYPE_N_BASECLASSES (type);
181 /* First, print out baseclasses such that we don't print
182 duplicates of virtual baseclasses. */
184 if (n_baseclasses > 0)
185 cp_print_value (type, real_type, valaddr, offset, address, stream,
186 format, recurse + 1, pretty, dont_print_vb);
188 /* Second, print out data fields */
190 /* If there are no data fields, or if the only field is the
191 * vtbl pointer, skip this part */
192 if ((len == n_baseclasses)
193 || ((len - n_baseclasses == 1)
194 && TYPE_HAS_VTABLE (type)
195 && strncmp (TYPE_FIELD_NAME (type, n_baseclasses),
196 hpacc_vtbl_ptr_name, 5) == 0)
198 fprintf_filtered (stream, "<No data fields>");
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 tmp_obstack = dont_print_statmem_obstack;
207 obstack_finish (&dont_print_statmem_obstack);
210 for (i = n_baseclasses; i < len; i++)
212 /* If requested, skip printing of static fields. */
213 if (!static_field_print && TYPE_FIELD_STATIC (type, i))
216 /* If a vtable pointer appears, we'll print it out later */
217 if (TYPE_HAS_VTABLE (type)
218 && strncmp (TYPE_FIELD_NAME (type, i), hpacc_vtbl_ptr_name,
223 fprintf_filtered (stream, ", ");
224 else if (n_baseclasses > 0)
228 fprintf_filtered (stream, "\n");
229 print_spaces_filtered (2 + 2 * recurse, stream);
230 fputs_filtered ("members of ", stream);
231 fputs_filtered (type_name_no_tag (type), stream);
232 fputs_filtered (": ", stream);
239 fprintf_filtered (stream, "\n");
240 print_spaces_filtered (2 + 2 * recurse, stream);
244 wrap_here (n_spaces (2 + 2 * recurse));
248 if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
249 fputs_filtered ("\"( ptr \"", stream);
251 fputs_filtered ("\"( nodef \"", stream);
252 if (TYPE_FIELD_STATIC (type, i))
253 fputs_filtered ("static ", stream);
254 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
255 current_language->la_language,
256 DMGL_PARAMS | DMGL_ANSI);
257 fputs_filtered ("\" \"", stream);
258 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
259 current_language->la_language,
260 DMGL_PARAMS | DMGL_ANSI);
261 fputs_filtered ("\") \"", stream);
265 annotate_field_begin (TYPE_FIELD_TYPE (type, i));
267 if (TYPE_FIELD_STATIC (type, i))
268 fputs_filtered ("static ", stream);
269 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
270 current_language->la_language,
271 DMGL_PARAMS | DMGL_ANSI);
272 annotate_field_name_end ();
273 /* do not print leading '=' in case of anonymous unions */
274 if (strcmp (TYPE_FIELD_NAME (type, i), ""))
275 fputs_filtered (" = ", stream);
276 annotate_field_value ();
279 if (!TYPE_FIELD_STATIC (type, i) && TYPE_FIELD_PACKED (type, i))
283 /* Bitfields require special handling, especially due to byte
285 if (TYPE_FIELD_IGNORE (type, i))
287 fputs_filtered ("<optimized out or zero length>", stream);
291 v = value_from_longest
292 (TYPE_FIELD_TYPE (type, i),
293 unpack_field_as_long (type, valaddr + offset, i));
295 common_val_print (v, stream, format, 0, recurse + 1, pretty);
300 if (TYPE_FIELD_IGNORE (type, i))
302 fputs_filtered ("<optimized out or zero length>", stream);
304 else if (TYPE_FIELD_STATIC (type, i))
306 struct value *v = value_static_field (type, i);
308 fputs_filtered ("<optimized out>", stream);
310 cp_print_static_field (TYPE_FIELD_TYPE (type, i), v,
311 stream, format, recurse + 1,
316 val_print (TYPE_FIELD_TYPE (type, i),
317 valaddr, offset + TYPE_FIELD_BITPOS (type, i) / 8,
318 address + TYPE_FIELD_BITPOS (type, i) / 8,
319 stream, format, 0, recurse + 1, pretty);
322 annotate_field_end ();
325 if (dont_print_statmem == 0)
327 /* Free the space used to deal with the printing
328 of the members from top level. */
329 obstack_free (&dont_print_statmem_obstack, last_dont_print);
330 dont_print_statmem_obstack = tmp_obstack;
335 fprintf_filtered (stream, "\n");
336 print_spaces_filtered (2 * recurse, stream);
338 } /* if there are data fields */
339 /* Now print out the virtual table pointer if there is one */
340 if (TYPE_HAS_VTABLE (type)
341 && strncmp (TYPE_FIELD_NAME (type, n_baseclasses),
342 hpacc_vtbl_ptr_name, 5) == 0)
345 /* First get the virtual table pointer and print it out */
348 fputs_filtered ("__vfp = ", stream);
351 fputs_filtered (", Virtual table at ", stream);
353 /* pai: FIXME 32x64 problem? */
354 /* Not sure what the best notation is in the case where there is no
356 v = value_from_pointer (lookup_pointer_type (builtin_type_unsigned_long),
357 *(unsigned long *) (valaddr + offset));
359 common_val_print (v, stream, format, 0, recurse + 1, pretty);
364 /* Print out function pointers in vtable. */
366 /* FIXME: then-clause is for non-RRBC layout of virtual
367 * table. The RRBC case in the else-clause is yet to be
368 * implemented. The if (1) below should be changed to a
369 * test for whether the executable we have was compiled
370 * with a version of HP aCC that doesn't have RRBC
375 /* no RRBC support; function pointers embedded directly
378 int vfuncs = count_virtual_fns (real_type);
380 fputs_filtered (" {", stream);
382 /* FIXME : doesn't work at present */
384 fprintf_filtered (stream, "%d entr%s: ", vfuncs,
385 vfuncs == 1 ? "y" : "ies");
387 fputs_filtered ("not implemented", stream);
392 /* recursive function that prints all virtual function entries */
394 cp_print_hpacc_virtual_table_entries (real_type, &vfuncs, v,
395 stream, format, recurse,
398 fputs_filtered ("}", stream);
399 } /* non-RRBC case */
402 /* FIXME -- see comments above */
403 /* RRBC support present; function pointers are found
404 * by indirection through the class segment entries. */
412 fprintf_filtered (stream, "\n");
413 print_spaces_filtered (2 * recurse, stream);
416 } /* if vtable exists */
418 fprintf_filtered (stream, "}");
421 /* Special val_print routine to avoid printing multiple copies of virtual
425 cp_print_value (struct type *type, struct type *real_type,
426 const gdb_byte *valaddr, int offset, CORE_ADDR address,
427 struct ui_file *stream, int format, int recurse,
428 enum val_prettyprint pretty, struct type **dont_print_vb)
430 struct obstack tmp_obstack;
431 struct type **last_dont_print
432 = (struct type **) obstack_next_free (&dont_print_vb_obstack);
433 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
435 struct type *thistype;
437 if (dont_print_vb == 0)
439 /* If we're at top level, carve out a completely fresh
440 chunk of the obstack and use that until this particular
441 invocation returns. */
442 tmp_obstack = dont_print_vb_obstack;
443 /* Bump up the high-water mark. Now alpha is omega. */
444 obstack_finish (&dont_print_vb_obstack);
447 for (i = 0; i < n_baseclasses; i++)
451 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
452 char *basename = TYPE_NAME (baseclass);
453 const gdb_byte *base_valaddr;
455 if (BASETYPE_VIA_VIRTUAL (type, i))
457 struct type **first_dont_print
458 = (struct type **) obstack_base (&dont_print_vb_obstack);
460 int j = (struct type **) obstack_next_free (&dont_print_vb_obstack)
464 if (baseclass == first_dont_print[j])
467 obstack_ptr_grow (&dont_print_vb_obstack, baseclass);
471 thistype = real_type;
472 if (TYPE_HAS_VTABLE (type) && BASETYPE_VIA_VIRTUAL (type, i))
474 /* Assume HP/Taligent runtime convention */
475 find_rt_vbase_offset (type, TYPE_BASECLASS (type, i),
476 valaddr, offset, &boffset, &skip);
478 error (_("Virtual base class offset not found from vtable while"
480 base_valaddr = valaddr;
484 boffset = baseclass_offset (type, i,
487 skip = ((boffset == -1) || (boffset + offset) < 0) ? 1 : -1;
489 if (BASETYPE_VIA_VIRTUAL (type, i))
491 /* The virtual base class pointer might have been
492 clobbered by the user program. Make sure that it
493 still points to a valid memory location. */
496 && ((boffset + offset) < 0
497 || (boffset + offset) >= TYPE_LENGTH (type)))
499 /* FIXME (alloca): unsafe if baseclass is really really large. */
500 gdb_byte *buf = alloca (TYPE_LENGTH (baseclass));
502 if (target_read_memory (address + boffset, buf,
503 TYPE_LENGTH (baseclass)) != 0)
505 address = address + boffset;
508 thistype = baseclass;
511 base_valaddr = valaddr;
514 base_valaddr = valaddr;
517 /* now do the printing */
520 fprintf_filtered (stream, "\n");
521 print_spaces_filtered (2 * recurse, stream);
523 fputs_filtered ("<", stream);
524 /* Not sure what the best notation is in the case where there is no
526 fputs_filtered (basename ? basename : "", stream);
527 fputs_filtered ("> = ", stream);
531 fprintf_filtered (stream, "<invalid address>");
533 cp_print_value_fields (baseclass, thistype, base_valaddr,
534 thisoffset + boffset, address + boffset,
538 obstack_base (&dont_print_vb_obstack)),
540 fputs_filtered (", ", stream);
546 if (dont_print_vb == 0)
548 /* Free the space used to deal with the printing
549 of this type from top level. */
550 obstack_free (&dont_print_vb_obstack, last_dont_print);
551 /* Reset watermark so that we can continue protecting
552 ourselves from whatever we were protecting ourselves. */
553 dont_print_vb_obstack = tmp_obstack;
557 /* Print value of a static member.
558 To avoid infinite recursion when printing a class that contains
559 a static instance of the class, we keep the addresses of all printed
560 static member classes in an obstack and refuse to print them more
563 VAL contains the value to print, TYPE, STREAM, RECURSE, and PRETTY
564 have the same meanings as in c_val_print. */
567 cp_print_static_field (struct type *type,
569 struct ui_file *stream,
572 enum val_prettyprint pretty)
574 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
576 CORE_ADDR *first_dont_print;
580 = (CORE_ADDR *) obstack_base (&dont_print_statmem_obstack);
581 i = (CORE_ADDR *) obstack_next_free (&dont_print_statmem_obstack)
586 if (VALUE_ADDRESS (val) == first_dont_print[i])
588 fputs_filtered ("<same as static member of an already"
595 obstack_grow (&dont_print_statmem_obstack, (char *) &VALUE_ADDRESS (val),
598 CHECK_TYPEDEF (type);
599 cp_print_value_fields (type, type, value_contents_all (val),
600 value_embedded_offset (val), VALUE_ADDRESS (val),
601 stream, format, recurse, pretty, NULL, 1);
604 val_print (type, value_contents_all (val),
605 value_embedded_offset (val), VALUE_ADDRESS (val),
606 stream, format, 0, recurse, pretty);
610 /* Find the field in *DOMAIN, or its non-virtual base classes, with bit offset
611 OFFSET. Set *DOMAIN to the containing type and *FIELDNO to the containing
612 field number. If OFFSET is not exactly at the start of some field, set
616 cp_find_class_member (struct type **domain_p, int *fieldno,
623 *domain_p = check_typedef (*domain_p);
625 len = TYPE_NFIELDS (domain);
627 for (i = TYPE_N_BASECLASSES (domain); i < len; i++)
629 LONGEST bitpos = TYPE_FIELD_BITPOS (domain, i);
632 if (offset == bitpos)
639 for (i = 0; i < TYPE_N_BASECLASSES (domain); i++)
641 LONGEST bitpos = TYPE_FIELD_BITPOS (domain, i);
642 LONGEST bitsize = 8 * TYPE_LENGTH (TYPE_FIELD_TYPE (domain, i));
644 if (offset >= bitpos && offset < bitpos + bitsize)
646 *domain_p = TYPE_FIELD_TYPE (domain, i);
647 cp_find_class_member (domain_p, fieldno, offset - bitpos);
656 cp_print_class_member (const gdb_byte *valaddr, struct type *domain,
657 struct ui_file *stream, char *prefix)
659 /* VAL is a byte offset into the structure type DOMAIN.
660 Find the name of the field for that offset and
662 unsigned int fieldno;
664 LONGEST val = unpack_long (builtin_type_long, valaddr);
666 /* Pointers to data members are usually byte offsets into an object.
667 Because a data member can have offset zero, and a NULL pointer to
668 member must be distinct from any valid non-NULL pointer to
669 member, either the value is biased or the NULL value has a
670 special representation; both are permitted by ISO C++. HP aCC
671 used a bias of 0x20000000; HP cfront used a bias of 1; g++ 3.x
672 and other compilers which use the Itanium ABI use -1 as the NULL
673 value. GDB only supports that last form; to add support for
674 another form, make this into a cp-abi hook. */
678 fprintf_filtered (stream, "NULL");
682 cp_find_class_member (&domain, &fieldno, val << 3);
687 fputs_filtered (prefix, stream);
688 name = type_name_no_tag (domain);
690 fputs_filtered (name, stream);
692 c_type_print_base (domain, stream, 0, 0);
693 fprintf_filtered (stream, "::");
694 fputs_filtered (TYPE_FIELD_NAME (domain, fieldno), stream);
697 fprintf_filtered (stream, "%ld", (long) val);
701 /* This function prints out virtual table entries for a class; it
702 * recurses on the base classes to find all virtual functions
703 * available in a class.
705 * pai/1997-05-21 Note: As the name suggests, it's currently
706 * implemented for HP aCC runtime only. g++ objects are handled
707 * differently and I have made no attempt to fold that logic in
708 * here. The runtime layout is different for the two cases. Also,
709 * this currently has only the code for non-RRBC layouts generated by
710 * the HP aCC compiler; RRBC code is stubbed out and will have to be
715 cp_print_hpacc_virtual_table_entries (struct type *type, int *vfuncs,
716 struct value *v, struct ui_file *stream,
717 int format, int recurse,
718 enum val_prettyprint pretty)
722 /* pai: FIXME this function doesn't work. It should handle a given
723 * virtual function only once (latest redefinition in class hierarchy)
726 /* Recursion on other classes that can share the same vtable */
727 struct type *pbc = primary_base_class (type);
729 cp_print_hpacc_virtual_table_entries (pbc, vfuncs, v, stream, format,
732 /* Now deal with vfuncs declared in this class */
733 for (fn = 0; fn < TYPE_NFN_FIELDS (type); fn++)
734 for (oi = 0; oi < TYPE_FN_FIELDLIST_LENGTH (type, fn); oi++)
735 if (TYPE_FN_FIELD_VIRTUAL_P (TYPE_FN_FIELDLIST1 (type, fn), oi))
738 const char *field_physname;
740 /* virtual function offset */
741 int vx = (TYPE_FN_FIELD_VOFFSET (TYPE_FN_FIELDLIST1 (type, fn), oi)
744 /* Get the address of the vfunction entry */
745 struct value *vf = value_copy (v);
747 (void) value_fetch_lazy (vf);
748 /* adjust by offset */
749 /* NOTE: cagney/2005-01-02: THIS IS BOGUS. */
750 value_contents_writeable (vf)[0] += 4 * (HP_ACC_VFUNC_START + vx);
751 vf = value_ind (vf); /* get the entry */
752 /* make it a pointer */
753 deprecated_set_value_type (vf, value_type (v));
755 /* print out the entry */
756 common_val_print (vf, stream, format, 0, recurse + 1, pretty);
758 = TYPE_FN_FIELD_PHYSNAME (TYPE_FN_FIELDLIST1 (type, fn), oi);
759 /* pai: (temp) FIXME Maybe this should be DMGL_ANSI */
760 vf_name = cplus_demangle (field_physname, DMGL_ARM);
761 fprintf_filtered (stream, " %s", vf_name);
763 fputs_filtered (", ", stream);
770 _initialize_cp_valprint (void)
772 add_setshow_boolean_cmd ("static-members", class_support,
773 &static_field_print, _("\
774 Set printing of C++ static members."), _("\
775 Show printing of C++ static members."), NULL,
777 show_static_field_print,
778 &setprintlist, &showprintlist);
779 /* Turn on printing of static fields. */
780 static_field_print = 1;
782 add_setshow_boolean_cmd ("vtbl", class_support, &vtblprint, _("\
783 Set printing of C++ virtual function tables."), _("\
784 Show printing of C++ virtual function tables."), NULL,
787 &setprintlist, &showprintlist);
789 add_setshow_boolean_cmd ("object", class_support, &objectprint, _("\
790 Set printing of object's derived type based on vtable info."), _("\
791 Show printing of object's derived type based on vtable info."), NULL,
794 &setprintlist, &showprintlist);
796 /* Give people the defaults which they are used to. */
799 obstack_begin (&dont_print_vb_obstack, 32 * sizeof (struct type *));
800 obstack_specify_allocation (&dont_print_statmem_obstack,
801 32 * sizeof (CORE_ADDR), sizeof (CORE_ADDR),