1 /* Support for printing C++ values for GDB, the GNU debugger.
2 Copyright 1986, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
4 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., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
24 #include "gdb_obstack.h"
27 #include "expression.h"
33 #include "gdb_string.h"
39 int vtblprint; /* Controls printing of vtbl's */
40 int objectprint; /* Controls looking up an object's derived type
41 using what we find in its vtables. */
42 int static_field_print; /* Controls printing of static fields. */
44 static struct obstack dont_print_vb_obstack;
45 static struct obstack dont_print_statmem_obstack;
47 extern void _initialize_cp_valprint (void);
49 static void cp_print_static_field (struct type *, struct value *,
50 struct ui_file *, int, int,
51 enum val_prettyprint);
53 static void cp_print_value (struct type *, struct type *, char *, int,
54 CORE_ADDR, struct ui_file *, int, int,
55 enum val_prettyprint, struct type **);
57 static void cp_print_hpacc_virtual_table_entries (struct type *, int *,
59 struct ui_file *, int,
61 enum val_prettyprint);
65 cp_print_class_method (char *valaddr,
67 struct ui_file *stream)
70 struct fn_field *f = NULL;
79 struct type *target_type = check_typedef (TYPE_TARGET_TYPE (type));
81 domain = TYPE_DOMAIN_TYPE (target_type);
82 if (domain == (struct type *) NULL)
84 fprintf_filtered (stream, "<unknown>");
87 addr = unpack_pointer (type, valaddr);
88 if (METHOD_PTR_IS_VIRTUAL (addr))
90 offset = METHOD_PTR_TO_VOFFSET (addr);
91 len = TYPE_NFN_FIELDS (domain);
92 for (i = 0; i < len; i++)
94 f = TYPE_FN_FIELDLIST1 (domain, i);
95 len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
97 check_stub_method_group (domain, i);
98 for (j = 0; j < len2; j++)
100 if (TYPE_FN_FIELD_VOFFSET (f, j) == offset)
110 sym = find_pc_function (addr);
113 /* 1997-08-01 Currently unsupported with HP aCC */
114 if (deprecated_hp_som_som_object_present)
116 fputs_filtered ("?? <not supported with HP aCC>", stream);
119 error ("invalid pointer to member function");
121 len = TYPE_NFN_FIELDS (domain);
122 for (i = 0; i < len; i++)
124 f = TYPE_FN_FIELDLIST1 (domain, i);
125 len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
127 check_stub_method_group (domain, i);
128 for (j = 0; j < len2; j++)
130 if (strcmp (DEPRECATED_SYMBOL_NAME (sym), TYPE_FN_FIELD_PHYSNAME (f, j))
139 char *demangled_name;
141 fprintf_filtered (stream, "&");
142 fputs_filtered (kind, stream);
143 demangled_name = cplus_demangle (TYPE_FN_FIELD_PHYSNAME (f, j),
144 DMGL_ANSI | DMGL_PARAMS);
145 if (demangled_name == NULL)
146 fprintf_filtered (stream, "<badly mangled name %s>",
147 TYPE_FN_FIELD_PHYSNAME (f, j));
150 fputs_filtered (demangled_name, stream);
151 xfree (demangled_name);
156 fprintf_filtered (stream, "(");
157 type_print (type, "", stream, -1);
158 fprintf_filtered (stream, ") %d", (int) addr >> 3);
162 /* GCC versions after 2.4.5 use this. */
163 const char vtbl_ptr_name[] = "__vtbl_ptr_type";
165 /* HP aCC uses different names. */
166 const char hpacc_vtbl_ptr_name[] = "__vfp";
167 const char hpacc_vtbl_ptr_type_name[] = "__vftyp";
169 /* Return truth value for assertion that TYPE is of the type
170 "pointer to virtual function". */
173 cp_is_vtbl_ptr_type (struct type *type)
175 char *typename = type_name_no_tag (type);
177 return (typename != NULL && !strcmp (typename, vtbl_ptr_name));
180 /* Return truth value for the assertion that TYPE is of the type
181 "pointer to virtual function table". */
184 cp_is_vtbl_member (struct type *type)
186 /* With older versions of g++, the vtbl field pointed to an array
187 of structures. Nowadays it points directly to the structure. */
188 if (TYPE_CODE (type) == TYPE_CODE_PTR)
190 type = TYPE_TARGET_TYPE (type);
191 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
193 type = TYPE_TARGET_TYPE (type);
194 if (TYPE_CODE (type) == TYPE_CODE_STRUCT /* if not using thunks */
195 || TYPE_CODE (type) == TYPE_CODE_PTR) /* if using thunks */
197 /* Virtual functions tables are full of pointers
198 to virtual functions. */
199 return cp_is_vtbl_ptr_type (type);
202 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT) /* if not using thunks */
204 return cp_is_vtbl_ptr_type (type);
206 else if (TYPE_CODE (type) == TYPE_CODE_PTR) /* if using thunks */
208 /* The type name of the thunk pointer is NULL when using dwarf2.
209 We could test for a pointer to a function, but there is
210 no type info for the virtual table either, so it wont help. */
211 return cp_is_vtbl_ptr_type (type);
217 /* Mutually recursive subroutines of cp_print_value and c_val_print to
218 print out a structure's fields: cp_print_value_fields and cp_print_value.
220 TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and PRETTY have the
221 same meanings as in cp_print_value and c_val_print.
223 2nd argument REAL_TYPE is used to carry over the type of the derived
224 class across the recursion to base classes.
226 DONT_PRINT is an array of baseclass types that we
227 should not print, or zero if called from top level. */
230 cp_print_value_fields (struct type *type, struct type *real_type, char *valaddr,
231 int offset, CORE_ADDR address, struct ui_file *stream,
232 int format, int recurse, enum val_prettyprint pretty,
233 struct type **dont_print_vb, int dont_print_statmem)
235 int i, len, n_baseclasses;
236 struct obstack tmp_obstack;
237 char *last_dont_print = obstack_next_free (&dont_print_statmem_obstack);
240 CHECK_TYPEDEF (type);
242 fprintf_filtered (stream, "{");
243 len = TYPE_NFIELDS (type);
244 n_baseclasses = TYPE_N_BASECLASSES (type);
246 /* First, print out baseclasses such that we don't print
247 duplicates of virtual baseclasses. */
249 if (n_baseclasses > 0)
250 cp_print_value (type, real_type, valaddr, offset, address, stream,
251 format, recurse + 1, pretty, dont_print_vb);
253 /* Second, print out data fields */
255 /* If there are no data fields, or if the only field is the
256 * vtbl pointer, skip this part */
257 if ((len == n_baseclasses)
258 || ((len - n_baseclasses == 1)
259 && TYPE_HAS_VTABLE (type)
260 && strncmp (TYPE_FIELD_NAME (type, n_baseclasses),
261 hpacc_vtbl_ptr_name, 5) == 0)
263 fprintf_filtered (stream, "<No data fields>");
266 if (dont_print_statmem == 0)
268 /* If we're at top level, carve out a completely fresh
269 chunk of the obstack and use that until this particular
270 invocation returns. */
271 tmp_obstack = dont_print_statmem_obstack;
272 obstack_finish (&dont_print_statmem_obstack);
275 for (i = n_baseclasses; i < len; i++)
277 /* If requested, skip printing of static fields. */
278 if (!static_field_print && TYPE_FIELD_STATIC (type, i))
281 /* If a vtable pointer appears, we'll print it out later */
282 if (TYPE_HAS_VTABLE (type)
283 && strncmp (TYPE_FIELD_NAME (type, i), hpacc_vtbl_ptr_name,
288 fprintf_filtered (stream, ", ");
289 else if (n_baseclasses > 0)
293 fprintf_filtered (stream, "\n");
294 print_spaces_filtered (2 + 2 * recurse, stream);
295 fputs_filtered ("members of ", stream);
296 fputs_filtered (type_name_no_tag (type), stream);
297 fputs_filtered (": ", stream);
304 fprintf_filtered (stream, "\n");
305 print_spaces_filtered (2 + 2 * recurse, stream);
309 wrap_here (n_spaces (2 + 2 * recurse));
313 if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
314 fputs_filtered ("\"( ptr \"", stream);
316 fputs_filtered ("\"( nodef \"", stream);
317 if (TYPE_FIELD_STATIC (type, i))
318 fputs_filtered ("static ", stream);
319 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
321 DMGL_PARAMS | DMGL_ANSI);
322 fputs_filtered ("\" \"", stream);
323 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
325 DMGL_PARAMS | DMGL_ANSI);
326 fputs_filtered ("\") \"", stream);
330 annotate_field_begin (TYPE_FIELD_TYPE (type, i));
332 if (TYPE_FIELD_STATIC (type, i))
333 fputs_filtered ("static ", stream);
334 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
336 DMGL_PARAMS | DMGL_ANSI);
337 annotate_field_name_end ();
338 /* do not print leading '=' in case of anonymous unions */
339 if (strcmp (TYPE_FIELD_NAME (type, i), ""))
340 fputs_filtered (" = ", stream);
341 annotate_field_value ();
344 if (!TYPE_FIELD_STATIC (type, i) && TYPE_FIELD_PACKED (type, i))
348 /* Bitfields require special handling, especially due to byte
350 if (TYPE_FIELD_IGNORE (type, i))
352 fputs_filtered ("<optimized out or zero length>", stream);
356 v = value_from_longest
357 (TYPE_FIELD_TYPE (type, i),
358 unpack_field_as_long (type, valaddr + offset, i));
360 val_print (TYPE_FIELD_TYPE (type, i), VALUE_CONTENTS (v),
361 0, 0, stream, format, 0, recurse + 1, pretty);
366 if (TYPE_FIELD_IGNORE (type, i))
368 fputs_filtered ("<optimized out or zero length>", stream);
370 else if (TYPE_FIELD_STATIC (type, i))
372 struct value *v = value_static_field (type, i);
374 fputs_filtered ("<optimized out>", stream);
376 cp_print_static_field (TYPE_FIELD_TYPE (type, i), v,
377 stream, format, recurse + 1,
382 val_print (TYPE_FIELD_TYPE (type, i),
383 valaddr, offset + TYPE_FIELD_BITPOS (type, i) / 8,
384 address + TYPE_FIELD_BITPOS (type, i) / 8,
385 stream, format, 0, recurse + 1, pretty);
388 annotate_field_end ();
391 if (dont_print_statmem == 0)
393 /* Free the space used to deal with the printing
394 of the members from top level. */
395 obstack_free (&dont_print_statmem_obstack, last_dont_print);
396 dont_print_statmem_obstack = tmp_obstack;
401 fprintf_filtered (stream, "\n");
402 print_spaces_filtered (2 * recurse, stream);
404 } /* if there are data fields */
405 /* Now print out the virtual table pointer if there is one */
406 if (TYPE_HAS_VTABLE (type)
407 && strncmp (TYPE_FIELD_NAME (type, n_baseclasses),
408 hpacc_vtbl_ptr_name, 5) == 0)
411 /* First get the virtual table pointer and print it out */
414 fputs_filtered ("__vfp = ", stream);
417 fputs_filtered (", Virtual table at ", stream);
419 /* pai: FIXME 32x64 problem? */
420 /* Not sure what the best notation is in the case where there is no
422 v = value_from_pointer (lookup_pointer_type (builtin_type_unsigned_long),
423 *(unsigned long *) (valaddr + offset));
425 val_print (value_type (v), VALUE_CONTENTS (v), 0, 0,
426 stream, format, 0, recurse + 1, pretty);
431 /* Print out function pointers in vtable. */
433 /* FIXME: then-clause is for non-RRBC layout of virtual
434 * table. The RRBC case in the else-clause is yet to be
435 * implemented. The if (1) below should be changed to a
436 * test for whether the executable we have was compiled
437 * with a version of HP aCC that doesn't have RRBC
442 /* no RRBC support; function pointers embedded directly
445 int vfuncs = count_virtual_fns (real_type);
447 fputs_filtered (" {", stream);
449 /* FIXME : doesn't work at present */
451 fprintf_filtered (stream, "%d entr%s: ", vfuncs,
452 vfuncs == 1 ? "y" : "ies");
454 fputs_filtered ("not implemented", stream);
459 /* recursive function that prints all virtual function entries */
461 cp_print_hpacc_virtual_table_entries (real_type, &vfuncs, v,
462 stream, format, recurse,
465 fputs_filtered ("}", stream);
466 } /* non-RRBC case */
469 /* FIXME -- see comments above */
470 /* RRBC support present; function pointers are found
471 * by indirection through the class segment entries. */
479 fprintf_filtered (stream, "\n");
480 print_spaces_filtered (2 * recurse, stream);
483 } /* if vtable exists */
485 fprintf_filtered (stream, "}");
488 /* Special val_print routine to avoid printing multiple copies of virtual
492 cp_print_value (struct type *type, struct type *real_type, char *valaddr,
493 int offset, CORE_ADDR address, struct ui_file *stream,
494 int format, int recurse, enum val_prettyprint pretty,
495 struct type **dont_print_vb)
497 struct obstack tmp_obstack;
498 struct type **last_dont_print
499 = (struct type **) obstack_next_free (&dont_print_vb_obstack);
500 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
502 struct type *thistype;
504 if (dont_print_vb == 0)
506 /* If we're at top level, carve out a completely fresh
507 chunk of the obstack and use that until this particular
508 invocation returns. */
509 tmp_obstack = dont_print_vb_obstack;
510 /* Bump up the high-water mark. Now alpha is omega. */
511 obstack_finish (&dont_print_vb_obstack);
514 for (i = 0; i < n_baseclasses; i++)
518 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
519 char *basename = TYPE_NAME (baseclass);
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 **) obstack_next_free (&dont_print_vb_obstack)
531 if (baseclass == first_dont_print[j])
534 obstack_ptr_grow (&dont_print_vb_obstack, baseclass);
538 thistype = real_type;
539 if (TYPE_HAS_VTABLE (type) && BASETYPE_VIA_VIRTUAL (type, i))
541 /* Assume HP/Taligent runtime convention */
542 find_rt_vbase_offset (type, TYPE_BASECLASS (type, i),
543 valaddr, offset, &boffset, &skip);
545 error ("Virtual base class offset not found from vtable while"
547 base_valaddr = valaddr;
551 boffset = baseclass_offset (type, i,
554 skip = ((boffset == -1) || (boffset + offset) < 0) ? 1 : -1;
556 if (BASETYPE_VIA_VIRTUAL (type, i))
558 /* The virtual base class pointer might have been
559 clobbered by the user program. Make sure that it
560 still points to a valid memory location. */
563 && ((boffset + offset) < 0
564 || (boffset + offset) >= TYPE_LENGTH (type)))
566 /* FIXME (alloca): unsafe if baseclass is really really large. */
567 base_valaddr = (char *) alloca (TYPE_LENGTH (baseclass));
568 if (target_read_memory (address + boffset, base_valaddr,
569 TYPE_LENGTH (baseclass)) != 0)
571 address = address + boffset;
574 thistype = baseclass;
577 base_valaddr = valaddr;
580 base_valaddr = valaddr;
583 /* now do the printing */
586 fprintf_filtered (stream, "\n");
587 print_spaces_filtered (2 * recurse, stream);
589 fputs_filtered ("<", stream);
590 /* Not sure what the best notation is in the case where there is no
592 fputs_filtered (basename ? basename : "", stream);
593 fputs_filtered ("> = ", stream);
597 fprintf_filtered (stream, "<invalid address>");
599 cp_print_value_fields (baseclass, thistype, base_valaddr,
600 thisoffset + boffset, address + boffset,
604 obstack_base (&dont_print_vb_obstack)),
606 fputs_filtered (", ", stream);
612 if (dont_print_vb == 0)
614 /* Free the space used to deal with the printing
615 of this type from top level. */
616 obstack_free (&dont_print_vb_obstack, last_dont_print);
617 /* Reset watermark so that we can continue protecting
618 ourselves from whatever we were protecting ourselves. */
619 dont_print_vb_obstack = tmp_obstack;
623 /* Print value of a static member.
624 To avoid infinite recursion when printing a class that contains
625 a static instance of the class, we keep the addresses of all printed
626 static member classes in an obstack and refuse to print them more
629 VAL contains the value to print, TYPE, STREAM, RECURSE, and PRETTY
630 have the same meanings as in c_val_print. */
633 cp_print_static_field (struct type *type,
635 struct ui_file *stream,
638 enum val_prettyprint pretty)
640 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
642 CORE_ADDR *first_dont_print;
646 = (CORE_ADDR *) obstack_base (&dont_print_statmem_obstack);
647 i = (CORE_ADDR *) obstack_next_free (&dont_print_statmem_obstack)
652 if (VALUE_ADDRESS (val) == first_dont_print[i])
654 fputs_filtered ("<same as static member of an already"
661 obstack_grow (&dont_print_statmem_obstack, (char *) &VALUE_ADDRESS (val),
664 CHECK_TYPEDEF (type);
665 cp_print_value_fields (type, type, VALUE_CONTENTS_ALL (val),
666 VALUE_EMBEDDED_OFFSET (val), VALUE_ADDRESS (val),
667 stream, format, recurse, pretty, NULL, 1);
670 val_print (type, VALUE_CONTENTS_ALL (val),
671 VALUE_EMBEDDED_OFFSET (val), VALUE_ADDRESS (val),
672 stream, format, 0, recurse, pretty);
676 cp_print_class_member (char *valaddr, struct type *domain,
677 struct ui_file *stream, char *prefix)
680 /* VAL is a byte offset into the structure type DOMAIN.
681 Find the name of the field for that offset and
686 unsigned len = TYPE_NFIELDS (domain);
688 /* @@ Make VAL into bit offset */
690 /* Note: HP aCC generates offsets that are the real byte offsets added
691 to a constant bias 0x20000000 (1 << 29). This constant bias gets
692 shifted out in the code below -- joyous happenstance! */
694 /* Note: HP cfront uses a constant bias of 1; if we support this
695 compiler ever, we will have to adjust the computation below */
697 LONGEST val = unpack_long (builtin_type_int, valaddr) << 3;
698 for (i = TYPE_N_BASECLASSES (domain); i < len; i++)
700 int bitpos = TYPE_FIELD_BITPOS (domain, i);
704 if (val < bitpos && i != 0)
706 /* Somehow pointing into a field. */
708 extra = (val - TYPE_FIELD_BITPOS (domain, i));
719 fputs_filtered (prefix, stream);
720 name = type_name_no_tag (domain);
722 fputs_filtered (name, stream);
724 c_type_print_base (domain, stream, 0, 0);
725 fprintf_filtered (stream, "::");
726 fputs_filtered (TYPE_FIELD_NAME (domain, i), stream);
728 fprintf_filtered (stream, " + %d bytes", extra);
730 fprintf_filtered (stream, " (offset in bits)");
733 fprintf_filtered (stream, "%ld", (long) (val >> 3));
737 /* This function prints out virtual table entries for a class; it
738 * recurses on the base classes to find all virtual functions
739 * available in a class.
741 * pai/1997-05-21 Note: As the name suggests, it's currently
742 * implemented for HP aCC runtime only. g++ objects are handled
743 * differently and I have made no attempt to fold that logic in
744 * here. The runtime layout is different for the two cases. Also,
745 * this currently has only the code for non-RRBC layouts generated by
746 * the HP aCC compiler; RRBC code is stubbed out and will have to be
751 cp_print_hpacc_virtual_table_entries (struct type *type, int *vfuncs,
752 struct value *v, struct ui_file *stream,
753 int format, int recurse,
754 enum val_prettyprint pretty)
758 /* pai: FIXME this function doesn't work. It should handle a given
759 * virtual function only once (latest redefinition in class hierarchy)
762 /* Recursion on other classes that can share the same vtable */
763 struct type *pbc = primary_base_class (type);
765 cp_print_hpacc_virtual_table_entries (pbc, vfuncs, v, stream, format,
768 /* Now deal with vfuncs declared in this class */
769 for (fn = 0; fn < TYPE_NFN_FIELDS (type); fn++)
770 for (oi = 0; oi < TYPE_FN_FIELDLIST_LENGTH (type, fn); oi++)
771 if (TYPE_FN_FIELD_VIRTUAL_P (TYPE_FN_FIELDLIST1 (type, fn), oi))
774 const char *field_physname;
776 /* virtual function offset */
777 int vx = (TYPE_FN_FIELD_VOFFSET (TYPE_FN_FIELDLIST1 (type, fn), oi)
780 /* Get the address of the vfunction entry */
781 struct value *vf = value_copy (v);
783 (void) value_fetch_lazy (vf);
784 /* adjust by offset */
785 vf->aligner.contents[0] += 4 * (HP_ACC_VFUNC_START + vx);
786 vf = value_ind (vf); /* get the entry */
787 vf->type = value_type (v); /* make it a pointer */
789 /* print out the entry */
790 val_print (value_type (vf), VALUE_CONTENTS (vf), 0, 0,
791 stream, format, 0, recurse + 1, pretty);
793 = TYPE_FN_FIELD_PHYSNAME (TYPE_FN_FIELDLIST1 (type, fn), oi);
794 /* pai: (temp) FIXME Maybe this should be DMGL_ANSI */
795 vf_name = cplus_demangle (field_physname, DMGL_ARM);
796 fprintf_filtered (stream, " %s", vf_name);
798 fputs_filtered (", ", stream);
805 _initialize_cp_valprint (void)
807 deprecated_add_show_from_set
808 (add_set_cmd ("static-members", class_support, var_boolean,
809 (char *) &static_field_print,
810 "Set printing of C++ static members.",
813 /* Turn on printing of static fields. */
814 static_field_print = 1;
816 deprecated_add_show_from_set
817 (add_set_cmd ("vtbl", class_support, var_boolean, (char *) &vtblprint,
818 "Set printing of C++ virtual function tables.",
822 deprecated_add_show_from_set
823 (add_set_cmd ("object", class_support, var_boolean, (char *) &objectprint,
824 "Set printing of object's derived type based on vtable info.",
828 /* Give people the defaults which they are used to. */
831 obstack_begin (&dont_print_vb_obstack, 32 * sizeof (struct type *));
832 obstack_specify_allocation (&dont_print_statmem_obstack,
833 32 * sizeof (CORE_ADDR), sizeof (CORE_ADDR),