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 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"
39 /* 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
54 show_objectprint (struct ui_file
*file
, int from_tty
,
55 struct cmd_list_element
*c
,
58 fprintf_filtered (file
, _("\
59 Printing of object's derived type based on vtable info is %s.\n"),
63 int static_field_print
; /* Controls printing of static fields. */
65 show_static_field_print (struct ui_file
*file
, int from_tty
,
66 struct cmd_list_element
*c
, const char *value
)
68 fprintf_filtered (file
, _("Printing of C++ static members is %s.\n"),
73 static struct obstack dont_print_vb_obstack
;
74 static struct obstack dont_print_statmem_obstack
;
76 extern void _initialize_cp_valprint (void);
78 static void cp_print_static_field (struct type
*, struct value
*,
79 struct ui_file
*, int, int,
80 enum val_prettyprint
);
82 static void cp_print_value (struct type
*, struct type
*, const gdb_byte
*,
83 int, CORE_ADDR
, struct ui_file
*, int, int,
84 enum val_prettyprint
, struct type
**);
86 static void cp_print_hpacc_virtual_table_entries (struct type
*, int *,
88 struct ui_file
*, int,
90 enum val_prettyprint
);
93 /* GCC versions after 2.4.5 use this. */
94 const char vtbl_ptr_name
[] = "__vtbl_ptr_type";
96 /* HP aCC uses different names. */
97 const char hpacc_vtbl_ptr_name
[] = "__vfp";
98 const char hpacc_vtbl_ptr_type_name
[] = "__vftyp";
100 /* Return truth value for assertion that TYPE is of the type
101 "pointer to virtual function". */
104 cp_is_vtbl_ptr_type (struct type
*type
)
106 char *typename
= type_name_no_tag (type
);
108 return (typename
!= NULL
&& !strcmp (typename
, vtbl_ptr_name
));
111 /* Return truth value for the assertion that TYPE is of the type
112 "pointer to virtual function table". */
115 cp_is_vtbl_member (struct type
*type
)
117 /* With older versions of g++, the vtbl field pointed to an array
118 of structures. Nowadays it points directly to the structure. */
119 if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
121 type
= TYPE_TARGET_TYPE (type
);
122 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
)
124 type
= TYPE_TARGET_TYPE (type
);
125 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
/* if not using thunks */
126 || TYPE_CODE (type
) == TYPE_CODE_PTR
) /* if using thunks */
128 /* Virtual functions tables are full of pointers
129 to virtual functions. */
130 return cp_is_vtbl_ptr_type (type
);
133 else if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
) /* if not using thunks */
135 return cp_is_vtbl_ptr_type (type
);
137 else if (TYPE_CODE (type
) == TYPE_CODE_PTR
) /* if using thunks */
139 /* The type name of the thunk pointer is NULL when using dwarf2.
140 We could test for a pointer to a function, but there is
141 no type info for the virtual table either, so it wont help. */
142 return cp_is_vtbl_ptr_type (type
);
148 /* Mutually recursive subroutines of cp_print_value and c_val_print to
149 print out a structure's fields: cp_print_value_fields and cp_print_value.
151 TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and PRETTY have the
152 same meanings as in cp_print_value and c_val_print.
154 2nd argument REAL_TYPE is used to carry over the type of the derived
155 class across the recursion to base classes.
157 DONT_PRINT is an array of baseclass types that we
158 should not print, or zero if called from top level. */
161 cp_print_value_fields (struct type
*type
, struct type
*real_type
,
162 const gdb_byte
*valaddr
, int offset
, CORE_ADDR address
,
163 struct ui_file
*stream
, int format
, int recurse
,
164 enum val_prettyprint pretty
,
165 struct type
**dont_print_vb
,int dont_print_statmem
)
167 int i
, len
, n_baseclasses
;
168 char *last_dont_print
= obstack_next_free (&dont_print_statmem_obstack
);
171 CHECK_TYPEDEF (type
);
173 fprintf_filtered (stream
, "{");
174 len
= TYPE_NFIELDS (type
);
175 n_baseclasses
= TYPE_N_BASECLASSES (type
);
177 /* First, print out baseclasses such that we don't print
178 duplicates of virtual baseclasses. */
180 if (n_baseclasses
> 0)
181 cp_print_value (type
, real_type
, valaddr
, offset
, address
, stream
,
182 format
, recurse
+ 1, pretty
, dont_print_vb
);
184 /* Second, print out data fields */
186 /* If there are no data fields, or if the only field is the
187 * vtbl pointer, skip this part */
188 if ((len
== n_baseclasses
)
189 || ((len
- n_baseclasses
== 1)
190 && TYPE_HAS_VTABLE (type
)
191 && strncmp (TYPE_FIELD_NAME (type
, n_baseclasses
),
192 hpacc_vtbl_ptr_name
, 5) == 0)
194 fprintf_filtered (stream
, "<No data fields>");
197 struct obstack tmp_obstack
= dont_print_statmem_obstack
;
199 if (dont_print_statmem
== 0)
201 /* If we're at top level, carve out a completely fresh
202 chunk of the obstack and use that until this particular
203 invocation returns. */
204 obstack_finish (&dont_print_statmem_obstack
);
207 for (i
= n_baseclasses
; i
< len
; i
++)
209 /* If requested, skip printing of static fields. */
210 if (!static_field_print
&& TYPE_FIELD_STATIC (type
, i
))
213 /* If a vtable pointer appears, we'll print it out later */
214 if (TYPE_HAS_VTABLE (type
)
215 && strncmp (TYPE_FIELD_NAME (type
, i
), hpacc_vtbl_ptr_name
,
220 fprintf_filtered (stream
, ", ");
221 else if (n_baseclasses
> 0)
225 fprintf_filtered (stream
, "\n");
226 print_spaces_filtered (2 + 2 * recurse
, stream
);
227 fputs_filtered ("members of ", stream
);
228 fputs_filtered (type_name_no_tag (type
), stream
);
229 fputs_filtered (": ", stream
);
236 fprintf_filtered (stream
, "\n");
237 print_spaces_filtered (2 + 2 * recurse
, stream
);
241 wrap_here (n_spaces (2 + 2 * recurse
));
245 if (TYPE_CODE (TYPE_FIELD_TYPE (type
, i
)) == TYPE_CODE_PTR
)
246 fputs_filtered ("\"( ptr \"", stream
);
248 fputs_filtered ("\"( nodef \"", stream
);
249 if (TYPE_FIELD_STATIC (type
, i
))
250 fputs_filtered ("static ", stream
);
251 fprintf_symbol_filtered (stream
, TYPE_FIELD_NAME (type
, i
),
252 current_language
->la_language
,
253 DMGL_PARAMS
| DMGL_ANSI
);
254 fputs_filtered ("\" \"", stream
);
255 fprintf_symbol_filtered (stream
, TYPE_FIELD_NAME (type
, i
),
256 current_language
->la_language
,
257 DMGL_PARAMS
| DMGL_ANSI
);
258 fputs_filtered ("\") \"", stream
);
262 annotate_field_begin (TYPE_FIELD_TYPE (type
, i
));
264 if (TYPE_FIELD_STATIC (type
, i
))
265 fputs_filtered ("static ", stream
);
266 fprintf_symbol_filtered (stream
, TYPE_FIELD_NAME (type
, i
),
267 current_language
->la_language
,
268 DMGL_PARAMS
| DMGL_ANSI
);
269 annotate_field_name_end ();
270 /* do not print leading '=' in case of anonymous unions */
271 if (strcmp (TYPE_FIELD_NAME (type
, i
), ""))
272 fputs_filtered (" = ", stream
);
273 annotate_field_value ();
276 if (!TYPE_FIELD_STATIC (type
, i
) && TYPE_FIELD_PACKED (type
, i
))
280 /* Bitfields require special handling, especially due to byte
282 if (TYPE_FIELD_IGNORE (type
, i
))
284 fputs_filtered ("<optimized out or zero length>", stream
);
288 v
= value_from_longest
289 (TYPE_FIELD_TYPE (type
, i
),
290 unpack_field_as_long (type
, valaddr
+ offset
, i
));
292 common_val_print (v
, stream
, format
, 0, recurse
+ 1, pretty
);
297 if (TYPE_FIELD_IGNORE (type
, i
))
299 fputs_filtered ("<optimized out or zero length>", stream
);
301 else if (TYPE_FIELD_STATIC (type
, i
))
303 struct value
*v
= value_static_field (type
, i
);
305 fputs_filtered ("<optimized out>", stream
);
307 cp_print_static_field (TYPE_FIELD_TYPE (type
, i
), v
,
308 stream
, format
, recurse
+ 1,
313 val_print (TYPE_FIELD_TYPE (type
, i
),
314 valaddr
, offset
+ TYPE_FIELD_BITPOS (type
, i
) / 8,
315 address
+ TYPE_FIELD_BITPOS (type
, i
) / 8,
316 stream
, format
, 0, recurse
+ 1, pretty
);
319 annotate_field_end ();
322 if (dont_print_statmem
== 0)
324 /* Free the space used to deal with the printing
325 of the members from top level. */
326 obstack_free (&dont_print_statmem_obstack
, last_dont_print
);
327 dont_print_statmem_obstack
= tmp_obstack
;
332 fprintf_filtered (stream
, "\n");
333 print_spaces_filtered (2 * recurse
, stream
);
335 } /* if there are data fields */
336 /* Now print out the virtual table pointer if there is one */
337 if (TYPE_HAS_VTABLE (type
)
338 && strncmp (TYPE_FIELD_NAME (type
, n_baseclasses
),
339 hpacc_vtbl_ptr_name
, 5) == 0)
342 /* First get the virtual table pointer and print it out */
345 fputs_filtered ("__vfp = ", stream
);
348 fputs_filtered (", Virtual table at ", stream
);
350 /* pai: FIXME 32x64 problem? */
351 /* Not sure what the best notation is in the case where there is no
353 v
= value_from_pointer (lookup_pointer_type (builtin_type_unsigned_long
),
354 *(unsigned long *) (valaddr
+ offset
));
356 common_val_print (v
, stream
, format
, 0, recurse
+ 1, pretty
);
361 /* Print out function pointers in vtable. */
363 /* FIXME: then-clause is for non-RRBC layout of virtual
364 * table. The RRBC case in the else-clause is yet to be
365 * implemented. The if (1) below should be changed to a
366 * test for whether the executable we have was compiled
367 * with a version of HP aCC that doesn't have RRBC
372 /* no RRBC support; function pointers embedded directly
375 int vfuncs
= count_virtual_fns (real_type
);
377 fputs_filtered (" {", stream
);
379 /* FIXME : doesn't work at present */
381 fprintf_filtered (stream
, "%d entr%s: ", vfuncs
,
382 vfuncs
== 1 ? "y" : "ies");
384 fputs_filtered ("not implemented", stream
);
389 /* recursive function that prints all virtual function entries */
391 cp_print_hpacc_virtual_table_entries (real_type
, &vfuncs
, v
,
392 stream
, format
, recurse
,
395 fputs_filtered ("}", stream
);
396 } /* non-RRBC case */
399 /* FIXME -- see comments above */
400 /* RRBC support present; function pointers are found
401 * by indirection through the class segment entries. */
409 fprintf_filtered (stream
, "\n");
410 print_spaces_filtered (2 * recurse
, stream
);
413 } /* if vtable exists */
415 fprintf_filtered (stream
, "}");
418 /* Special val_print routine to avoid printing multiple copies of virtual
422 cp_print_value (struct type
*type
, struct type
*real_type
,
423 const gdb_byte
*valaddr
, int offset
, CORE_ADDR address
,
424 struct ui_file
*stream
, int format
, int recurse
,
425 enum val_prettyprint pretty
, struct type
**dont_print_vb
)
427 struct type
**last_dont_print
428 = (struct type
**) obstack_next_free (&dont_print_vb_obstack
);
429 struct obstack tmp_obstack
= dont_print_vb_obstack
;
430 int i
, n_baseclasses
= TYPE_N_BASECLASSES (type
);
432 struct type
*thistype
;
434 if (dont_print_vb
== 0)
436 /* If we're at top level, carve out a completely fresh
437 chunk of the obstack and use that until this particular
438 invocation returns. */
439 /* Bump up the high-water mark. Now alpha is omega. */
440 obstack_finish (&dont_print_vb_obstack
);
443 for (i
= 0; i
< n_baseclasses
; i
++)
447 struct type
*baseclass
= check_typedef (TYPE_BASECLASS (type
, i
));
448 char *basename
= TYPE_NAME (baseclass
);
449 const gdb_byte
*base_valaddr
;
451 if (BASETYPE_VIA_VIRTUAL (type
, i
))
453 struct type
**first_dont_print
454 = (struct type
**) obstack_base (&dont_print_vb_obstack
);
456 int j
= (struct type
**) obstack_next_free (&dont_print_vb_obstack
)
460 if (baseclass
== first_dont_print
[j
])
463 obstack_ptr_grow (&dont_print_vb_obstack
, baseclass
);
467 thistype
= real_type
;
468 if (TYPE_HAS_VTABLE (type
) && BASETYPE_VIA_VIRTUAL (type
, i
))
470 /* Assume HP/Taligent runtime convention */
471 find_rt_vbase_offset (type
, TYPE_BASECLASS (type
, i
),
472 valaddr
, offset
, &boffset
, &skip
);
474 error (_("Virtual base class offset not found from vtable while"
476 base_valaddr
= valaddr
;
480 boffset
= baseclass_offset (type
, i
,
483 skip
= ((boffset
== -1) || (boffset
+ offset
) < 0) ? 1 : -1;
485 if (BASETYPE_VIA_VIRTUAL (type
, i
))
487 /* The virtual base class pointer might have been
488 clobbered by the user program. Make sure that it
489 still points to a valid memory location. */
492 && ((boffset
+ offset
) < 0
493 || (boffset
+ offset
) >= TYPE_LENGTH (type
)))
495 /* FIXME (alloca): unsafe if baseclass is really really large. */
496 gdb_byte
*buf
= alloca (TYPE_LENGTH (baseclass
));
498 if (target_read_memory (address
+ boffset
, buf
,
499 TYPE_LENGTH (baseclass
)) != 0)
501 address
= address
+ boffset
;
504 thistype
= baseclass
;
507 base_valaddr
= valaddr
;
510 base_valaddr
= valaddr
;
513 /* now do the printing */
516 fprintf_filtered (stream
, "\n");
517 print_spaces_filtered (2 * recurse
, stream
);
519 fputs_filtered ("<", stream
);
520 /* Not sure what the best notation is in the case where there is no
522 fputs_filtered (basename
? basename
: "", stream
);
523 fputs_filtered ("> = ", stream
);
527 fprintf_filtered (stream
, "<invalid address>");
529 cp_print_value_fields (baseclass
, thistype
, base_valaddr
,
530 thisoffset
+ boffset
, address
+ boffset
,
534 obstack_base (&dont_print_vb_obstack
)),
536 fputs_filtered (", ", stream
);
542 if (dont_print_vb
== 0)
544 /* Free the space used to deal with the printing
545 of this type from top level. */
546 obstack_free (&dont_print_vb_obstack
, last_dont_print
);
547 /* Reset watermark so that we can continue protecting
548 ourselves from whatever we were protecting ourselves. */
549 dont_print_vb_obstack
= tmp_obstack
;
553 /* Print value of a static member.
554 To avoid infinite recursion when printing a class that contains
555 a static instance of the class, we keep the addresses of all printed
556 static member classes in an obstack and refuse to print them more
559 VAL contains the value to print, TYPE, STREAM, RECURSE, and PRETTY
560 have the same meanings as in c_val_print. */
563 cp_print_static_field (struct type
*type
,
565 struct ui_file
*stream
,
568 enum val_prettyprint pretty
)
570 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
572 CORE_ADDR
*first_dont_print
;
576 = (CORE_ADDR
*) obstack_base (&dont_print_statmem_obstack
);
577 i
= (CORE_ADDR
*) obstack_next_free (&dont_print_statmem_obstack
)
582 if (VALUE_ADDRESS (val
) == first_dont_print
[i
])
584 fputs_filtered ("<same as static member of an already"
591 obstack_grow (&dont_print_statmem_obstack
, (char *) &VALUE_ADDRESS (val
),
594 CHECK_TYPEDEF (type
);
595 cp_print_value_fields (type
, type
, value_contents_all (val
),
596 value_embedded_offset (val
), VALUE_ADDRESS (val
),
597 stream
, format
, recurse
, pretty
, NULL
, 1);
600 val_print (type
, value_contents_all (val
),
601 value_embedded_offset (val
), VALUE_ADDRESS (val
),
602 stream
, format
, 0, recurse
, pretty
);
606 /* Find the field in *DOMAIN, or its non-virtual base classes, with bit offset
607 OFFSET. Set *DOMAIN to the containing type and *FIELDNO to the containing
608 field number. If OFFSET is not exactly at the start of some field, set
612 cp_find_class_member (struct type
**domain_p
, int *fieldno
,
619 *domain_p
= check_typedef (*domain_p
);
621 len
= TYPE_NFIELDS (domain
);
623 for (i
= TYPE_N_BASECLASSES (domain
); i
< len
; i
++)
625 LONGEST bitpos
= TYPE_FIELD_BITPOS (domain
, i
);
628 if (offset
== bitpos
)
635 for (i
= 0; i
< TYPE_N_BASECLASSES (domain
); i
++)
637 LONGEST bitpos
= TYPE_FIELD_BITPOS (domain
, i
);
638 LONGEST bitsize
= 8 * TYPE_LENGTH (TYPE_FIELD_TYPE (domain
, i
));
640 if (offset
>= bitpos
&& offset
< bitpos
+ bitsize
)
642 *domain_p
= TYPE_FIELD_TYPE (domain
, i
);
643 cp_find_class_member (domain_p
, fieldno
, offset
- bitpos
);
652 cp_print_class_member (const gdb_byte
*valaddr
, struct type
*domain
,
653 struct ui_file
*stream
, char *prefix
)
655 /* VAL is a byte offset into the structure type DOMAIN.
656 Find the name of the field for that offset and
658 unsigned int fieldno
;
660 LONGEST val
= unpack_long (builtin_type_long
, valaddr
);
662 /* Pointers to data members are usually byte offsets into an object.
663 Because a data member can have offset zero, and a NULL pointer to
664 member must be distinct from any valid non-NULL pointer to
665 member, either the value is biased or the NULL value has a
666 special representation; both are permitted by ISO C++. HP aCC
667 used a bias of 0x20000000; HP cfront used a bias of 1; g++ 3.x
668 and other compilers which use the Itanium ABI use -1 as the NULL
669 value. GDB only supports that last form; to add support for
670 another form, make this into a cp-abi hook. */
674 fprintf_filtered (stream
, "NULL");
678 cp_find_class_member (&domain
, &fieldno
, val
<< 3);
683 fputs_filtered (prefix
, stream
);
684 name
= type_name_no_tag (domain
);
686 fputs_filtered (name
, stream
);
688 c_type_print_base (domain
, stream
, 0, 0);
689 fprintf_filtered (stream
, "::");
690 fputs_filtered (TYPE_FIELD_NAME (domain
, fieldno
), stream
);
693 fprintf_filtered (stream
, "%ld", (long) val
);
697 /* This function prints out virtual table entries for a class; it
698 * recurses on the base classes to find all virtual functions
699 * available in a class.
701 * pai/1997-05-21 Note: As the name suggests, it's currently
702 * implemented for HP aCC runtime only. g++ objects are handled
703 * differently and I have made no attempt to fold that logic in
704 * here. The runtime layout is different for the two cases. Also,
705 * this currently has only the code for non-RRBC layouts generated by
706 * the HP aCC compiler; RRBC code is stubbed out and will have to be
711 cp_print_hpacc_virtual_table_entries (struct type
*type
, int *vfuncs
,
712 struct value
*v
, struct ui_file
*stream
,
713 int format
, int recurse
,
714 enum val_prettyprint pretty
)
718 /* pai: FIXME this function doesn't work. It should handle a given
719 * virtual function only once (latest redefinition in class hierarchy)
722 /* Recursion on other classes that can share the same vtable */
723 struct type
*pbc
= primary_base_class (type
);
725 cp_print_hpacc_virtual_table_entries (pbc
, vfuncs
, v
, stream
, format
,
728 /* Now deal with vfuncs declared in this class */
729 for (fn
= 0; fn
< TYPE_NFN_FIELDS (type
); fn
++)
730 for (oi
= 0; oi
< TYPE_FN_FIELDLIST_LENGTH (type
, fn
); oi
++)
731 if (TYPE_FN_FIELD_VIRTUAL_P (TYPE_FN_FIELDLIST1 (type
, fn
), oi
))
734 const char *field_physname
;
736 /* virtual function offset */
737 int vx
= (TYPE_FN_FIELD_VOFFSET (TYPE_FN_FIELDLIST1 (type
, fn
), oi
)
740 /* Get the address of the vfunction entry */
741 struct value
*vf
= value_copy (v
);
743 (void) value_fetch_lazy (vf
);
744 /* adjust by offset */
745 /* NOTE: cagney/2005-01-02: THIS IS BOGUS. */
746 value_contents_writeable (vf
)[0] += 4 * (HP_ACC_VFUNC_START
+ vx
);
747 vf
= value_ind (vf
); /* get the entry */
748 /* make it a pointer */
749 deprecated_set_value_type (vf
, value_type (v
));
751 /* print out the entry */
752 common_val_print (vf
, stream
, format
, 0, recurse
+ 1, pretty
);
754 = TYPE_FN_FIELD_PHYSNAME (TYPE_FN_FIELDLIST1 (type
, fn
), oi
);
755 /* pai: (temp) FIXME Maybe this should be DMGL_ANSI */
756 vf_name
= cplus_demangle (field_physname
, DMGL_ARM
);
757 fprintf_filtered (stream
, " %s", vf_name
);
760 fputs_filtered (", ", stream
);
767 _initialize_cp_valprint (void)
769 add_setshow_boolean_cmd ("static-members", class_support
,
770 &static_field_print
, _("\
771 Set printing of C++ static members."), _("\
772 Show printing of C++ static members."), NULL
,
774 show_static_field_print
,
775 &setprintlist
, &showprintlist
);
776 /* Turn on printing of static fields. */
777 static_field_print
= 1;
779 add_setshow_boolean_cmd ("vtbl", class_support
, &vtblprint
, _("\
780 Set printing of C++ virtual function tables."), _("\
781 Show printing of C++ virtual function tables."), NULL
,
784 &setprintlist
, &showprintlist
);
786 add_setshow_boolean_cmd ("object", class_support
, &objectprint
, _("\
787 Set printing of object's derived type based on vtable info."), _("\
788 Show printing of object's derived type based on vtable info."), NULL
,
791 &setprintlist
, &showprintlist
);
793 /* Give people the defaults which they are used to. */
796 obstack_begin (&dont_print_vb_obstack
, 32 * sizeof (struct type
*));
797 obstack_specify_allocation (&dont_print_statmem_obstack
,
798 32 * sizeof (CORE_ADDR
), sizeof (CORE_ADDR
),