1 /* Support for printing C++ values for GDB, the GNU debugger.
2 Copyright 1986, 1988, 1989, 1991, 1994, 1995, 1996
3 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
26 #include "expression.h"
32 #include "gdb_string.h"
36 /* Indication of presence of HP-compiled object files */
37 extern int hp_som_som_object_present; /* defined in symtab.c */
40 int vtblprint; /* Controls printing of vtbl's */
41 int objectprint; /* Controls looking up an object's derived type
42 using what we find in its vtables. */
43 int static_field_print; /* Controls printing of static fields. */
45 static struct obstack dont_print_vb_obstack;
46 static struct obstack dont_print_statmem_obstack;
48 extern void _initialize_cp_valprint PARAMS ((void));
51 cp_print_static_field PARAMS ((struct type *, value_ptr, GDB_FILE *, int, int,
52 enum val_prettyprint));
55 cp_print_value PARAMS ((struct type *, struct type *, char *, int, CORE_ADDR, GDB_FILE *,
56 int, int, enum val_prettyprint, struct type **));
59 cp_print_hpacc_virtual_table_entries PARAMS ((struct type *, int *, value_ptr, GDB_FILE *,
60 int, int, enum val_prettyprint));
64 cp_print_class_method (valaddr, type, 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 (lookup_pointer_type (builtin_type_void), 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 for (j = 0; j < len2; j++)
100 if (TYPE_FN_FIELD_VOFFSET (f, j) == offset)
102 if (TYPE_FN_FIELD_STUB (f, j))
103 check_stub_method (domain, i, j);
112 sym = find_pc_function (addr);
115 /* 1997-08-01 Currently unsupported with HP aCC */
116 if (hp_som_som_object_present)
118 fputs_filtered ("?? <not supported with HP aCC>", stream);
121 error ("invalid pointer to member function");
123 len = TYPE_NFN_FIELDS (domain);
124 for (i = 0; i < len; i++)
126 f = TYPE_FN_FIELDLIST1 (domain, i);
127 len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
129 for (j = 0; j < len2; j++)
132 if (TYPE_FN_FIELD_STUB (f, j))
133 check_stub_method (domain, i, j);
134 if (STREQ (SYMBOL_NAME (sym), TYPE_FN_FIELD_PHYSNAME (f, j)))
144 char *demangled_name;
146 fprintf_filtered (stream, "&");
147 fprintf_filtered (stream, kind);
148 demangled_name = cplus_demangle (TYPE_FN_FIELD_PHYSNAME (f, j),
149 DMGL_ANSI | DMGL_PARAMS);
150 if (demangled_name == NULL)
151 fprintf_filtered (stream, "<badly mangled name %s>",
152 TYPE_FN_FIELD_PHYSNAME (f, j));
155 fputs_filtered (demangled_name, stream);
156 free (demangled_name);
161 fprintf_filtered (stream, "(");
162 type_print (type, "", stream, -1);
163 fprintf_filtered (stream, ") %d", (int) addr >> 3);
167 /* This was what it was for gcc 2.4.5 and earlier. */
168 static const char vtbl_ptr_name_old[] =
169 {CPLUS_MARKER, 'v', 't', 'b', 'l', '_', 'p', 't', 'r', '_', 't', 'y', 'p', 'e', 0};
170 /* It was changed to this after 2.4.5. */
171 const char vtbl_ptr_name[] =
172 {'_', '_', 'v', 't', 'b', 'l', '_', 'p', 't', 'r', '_', 't', 'y', 'p', 'e', 0};
174 /* HP aCC uses different names */
175 const char hpacc_vtbl_ptr_name[] =
176 {'_', '_', 'v', 'f', 'p', 0};
177 const char hpacc_vtbl_ptr_type_name[] =
178 {'_', '_', 'v', 'f', 't', 'y', 'p', 0};
181 /* Return truth value for assertion that TYPE is of the type
182 "pointer to virtual function". */
185 cp_is_vtbl_ptr_type (type)
188 char *typename = type_name_no_tag (type);
190 return (typename != NULL
191 && (STREQ (typename, vtbl_ptr_name)
192 || STREQ (typename, vtbl_ptr_name_old)));
195 /* Return truth value for the assertion that TYPE is of the type
196 "pointer to virtual function table". */
199 cp_is_vtbl_member (type)
202 if (TYPE_CODE (type) == TYPE_CODE_PTR)
204 type = TYPE_TARGET_TYPE (type);
205 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
207 type = TYPE_TARGET_TYPE (type);
208 if (TYPE_CODE (type) == TYPE_CODE_STRUCT /* if not using thunks */
209 || TYPE_CODE (type) == TYPE_CODE_PTR) /* if using thunks */
211 /* Virtual functions tables are full of pointers
212 to virtual functions. */
213 return cp_is_vtbl_ptr_type (type);
220 /* Mutually recursive subroutines of cp_print_value and c_val_print to
221 print out a structure's fields: cp_print_value_fields and cp_print_value.
223 TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and PRETTY have the
224 same meanings as in cp_print_value and c_val_print.
226 2nd argument REAL_TYPE is used to carry over the type of the derived
227 class across the recursion to base classes.
229 DONT_PRINT is an array of baseclass types that we
230 should not print, or zero if called from top level. */
233 cp_print_value_fields (type, real_type, valaddr, offset, address, stream, format, recurse, pretty,
234 dont_print_vb, dont_print_statmem)
236 struct type *real_type;
243 enum val_prettyprint pretty;
244 struct type **dont_print_vb;
245 int dont_print_statmem;
247 int i, len, n_baseclasses;
248 struct obstack tmp_obstack;
249 char *last_dont_print = obstack_next_free (&dont_print_statmem_obstack);
252 CHECK_TYPEDEF (type);
254 fprintf_filtered (stream, "{");
255 len = TYPE_NFIELDS (type);
256 n_baseclasses = TYPE_N_BASECLASSES (type);
258 /* First, print out baseclasses such that we don't print
259 duplicates of virtual baseclasses. */
261 if (n_baseclasses > 0)
262 cp_print_value (type, real_type, valaddr, offset, address, stream,
263 format, recurse + 1, pretty, dont_print_vb);
265 /* Second, print out data fields */
267 /* If there are no data fields, or if the only field is the
268 * vtbl pointer, skip this part */
269 if ((len == n_baseclasses) ||
270 ((len - n_baseclasses == 1) &&
271 TYPE_HAS_VTABLE (type) &&
272 STREQN (TYPE_FIELD_NAME (type, n_baseclasses), hpacc_vtbl_ptr_name, 5)) ||
274 fprintf_filtered (stream, "<No data fields>");
277 extern int inspect_it;
279 if (dont_print_statmem == 0)
281 /* If we're at top level, carve out a completely fresh
282 chunk of the obstack and use that until this particular
283 invocation returns. */
284 tmp_obstack = dont_print_statmem_obstack;
285 obstack_finish (&dont_print_statmem_obstack);
288 for (i = n_baseclasses; i < len; i++)
290 /* If requested, skip printing of static fields. */
291 if (!static_field_print && TYPE_FIELD_STATIC (type, i))
294 /* If a vtable pointer appears, we'll print it out later */
295 if (TYPE_HAS_VTABLE (type) && STREQN (TYPE_FIELD_NAME (type, i), hpacc_vtbl_ptr_name, 5))
299 fprintf_filtered (stream, ", ");
300 else if (n_baseclasses > 0)
304 fprintf_filtered (stream, "\n");
305 print_spaces_filtered (2 + 2 * recurse, stream);
306 fputs_filtered ("members of ", stream);
307 fputs_filtered (type_name_no_tag (type), stream);
308 fputs_filtered (": ", stream);
315 fprintf_filtered (stream, "\n");
316 print_spaces_filtered (2 + 2 * recurse, stream);
320 wrap_here (n_spaces (2 + 2 * recurse));
324 if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
325 fputs_filtered ("\"( ptr \"", stream);
327 fputs_filtered ("\"( nodef \"", stream);
328 if (TYPE_FIELD_STATIC (type, i))
329 fputs_filtered ("static ", stream);
330 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
332 DMGL_PARAMS | DMGL_ANSI);
333 fputs_filtered ("\" \"", stream);
334 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
336 DMGL_PARAMS | DMGL_ANSI);
337 fputs_filtered ("\") \"", stream);
341 annotate_field_begin (TYPE_FIELD_TYPE (type, i));
343 if (TYPE_FIELD_STATIC (type, i))
344 fputs_filtered ("static ", stream);
345 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
347 DMGL_PARAMS | DMGL_ANSI);
348 annotate_field_name_end ();
349 /* do not print leading '=' in case of anonymous unions */
350 if (strcmp (TYPE_FIELD_NAME (type, i), ""))
351 fputs_filtered (" = ", stream);
352 annotate_field_value ();
355 if (!TYPE_FIELD_STATIC (type, i) && TYPE_FIELD_PACKED (type, i))
359 /* Bitfields require special handling, especially due to byte
361 if (TYPE_FIELD_IGNORE (type, i))
363 fputs_filtered ("<optimized out or zero length>", stream);
367 v = value_from_longest (TYPE_FIELD_TYPE (type, i),
368 unpack_field_as_long (type, valaddr + offset, i));
370 val_print (TYPE_FIELD_TYPE (type, i), VALUE_CONTENTS (v), 0, 0,
371 stream, format, 0, recurse + 1, pretty);
376 if (TYPE_FIELD_IGNORE (type, i))
378 fputs_filtered ("<optimized out or zero length>", stream);
380 else if (TYPE_FIELD_STATIC (type, i))
382 value_ptr v = value_static_field (type, i);
384 fputs_filtered ("<optimized out>", stream);
386 cp_print_static_field (TYPE_FIELD_TYPE (type, i), v,
387 stream, format, recurse + 1,
392 val_print (TYPE_FIELD_TYPE (type, i),
393 valaddr, offset + TYPE_FIELD_BITPOS (type, i) / 8,
394 address + TYPE_FIELD_BITPOS (type, i) / 8,
395 stream, format, 0, recurse + 1, pretty);
398 annotate_field_end ();
401 if (dont_print_statmem == 0)
403 /* Free the space used to deal with the printing
404 of the members from top level. */
405 obstack_free (&dont_print_statmem_obstack, last_dont_print);
406 dont_print_statmem_obstack = tmp_obstack;
411 fprintf_filtered (stream, "\n");
412 print_spaces_filtered (2 * recurse, stream);
414 } /* if there are data fields */
415 /* Now print out the virtual table pointer if there is one */
416 if (TYPE_HAS_VTABLE (type) && STREQN (TYPE_FIELD_NAME (type, n_baseclasses), hpacc_vtbl_ptr_name, 5))
419 /* First get the virtual table pointer and print it out */
422 fputs_filtered ("__vfp = ", stream);
425 fputs_filtered (", Virtual table at ", stream);
427 /* pai: FIXME 32x64 problem? */
428 /* Not sure what the best notation is in the case where there is no
430 v = value_from_longest (lookup_pointer_type (builtin_type_unsigned_long),
431 *(unsigned long *) (valaddr + offset));
433 val_print (VALUE_TYPE (v), VALUE_CONTENTS (v), 0, 0,
434 stream, format, 0, recurse + 1, pretty);
439 /* Print out function pointers in vtable. */
441 /* FIXME: then-clause is for non-RRBC layout of virtual
442 * table. The RRBC case in the else-clause is yet to be
443 * implemented. The if (1) below should be changed to a
444 * test for whether the executable we have was compiled
445 * with a version of HP aCC that doesn't have RRBC
450 /* no RRBC support; function pointers embedded directly in vtable */
452 int vfuncs = count_virtual_fns (real_type);
454 fputs_filtered (" {", stream);
456 /* FIXME : doesn't work at present */
458 fprintf_filtered (stream, "%d entr%s: ", vfuncs, vfuncs == 1 ? "y" : "ies");
460 fputs_filtered ("not implemented", stream);
465 /* recursive function that prints all virtual function entries */
467 cp_print_hpacc_virtual_table_entries (real_type, &vfuncs, v, stream, format, recurse, pretty);
469 fputs_filtered ("}", stream);
470 } /* non-RRBC case */
473 /* FIXME -- seem comments above */
474 /* RRBC support present; function pointers are found
475 * by indirection through the class segment entries. */
483 fprintf_filtered (stream, "\n");
484 print_spaces_filtered (2 * recurse, stream);
487 } /* if vtable exists */
489 fprintf_filtered (stream, "}");
492 /* Special val_print routine to avoid printing multiple copies of virtual
496 cp_print_value (type, real_type, valaddr, offset, address, stream, format, recurse, pretty,
499 struct type *real_type;
506 enum val_prettyprint pretty;
507 struct type **dont_print_vb;
509 struct obstack tmp_obstack;
510 struct type **last_dont_print
511 = (struct type **) obstack_next_free (&dont_print_vb_obstack);
512 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
514 if (dont_print_vb == 0)
516 /* If we're at top level, carve out a completely fresh
517 chunk of the obstack and use that until this particular
518 invocation returns. */
519 tmp_obstack = dont_print_vb_obstack;
520 /* Bump up the high-water mark. Now alpha is omega. */
521 obstack_finish (&dont_print_vb_obstack);
524 for (i = 0; i < n_baseclasses; i++)
528 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
529 char *basename = TYPE_NAME (baseclass);
532 if (BASETYPE_VIA_VIRTUAL (type, i))
534 struct type **first_dont_print
535 = (struct type **) obstack_base (&dont_print_vb_obstack);
537 int j = (struct type **) obstack_next_free (&dont_print_vb_obstack)
541 if (baseclass == first_dont_print[j])
544 obstack_ptr_grow (&dont_print_vb_obstack, baseclass);
547 if (TYPE_HAS_VTABLE (type) && BASETYPE_VIA_VIRTUAL (type, i))
549 /* Assume HP/Taligent runtime convention */
550 find_rt_vbase_offset (type, TYPE_BASECLASS (type, i),
551 valaddr, offset, &boffset, &skip);
553 error ("Virtual base class offset not found from vtable while printing");
554 base_valaddr = valaddr;
558 boffset = baseclass_offset (type, i, valaddr + offset, address + offset);
559 skip = ((boffset == -1) || (boffset + offset) < 0) ? 1 : -1;
561 if (BASETYPE_VIA_VIRTUAL (type, i))
563 /* The virtual base class pointer might have been clobbered by the
564 user program. Make sure that it still points to a valid memory
567 if (boffset != -1 && ((boffset + offset) < 0 || (boffset + offset) >= TYPE_LENGTH (type)))
569 base_valaddr = (char *) alloca (TYPE_LENGTH (baseclass));
570 if (target_read_memory (address + boffset, base_valaddr,
571 TYPE_LENGTH (baseclass)) != 0)
575 base_valaddr = valaddr;
578 base_valaddr = valaddr;
581 /* now do the printing */
584 fprintf_filtered (stream, "\n");
585 print_spaces_filtered (2 * recurse, stream);
587 fputs_filtered ("<", stream);
588 /* Not sure what the best notation is in the case where there is no
590 fputs_filtered (basename ? basename : "", stream);
591 fputs_filtered ("> = ", stream);
595 fprintf_filtered (stream, "<invalid address>");
597 cp_print_value_fields (baseclass, real_type, base_valaddr, offset + boffset, address,
598 stream, format, recurse, pretty,
599 (struct type **) obstack_base (&dont_print_vb_obstack),
601 fputs_filtered (", ", stream);
607 if (dont_print_vb == 0)
609 /* Free the space used to deal with the printing
610 of this type from top level. */
611 obstack_free (&dont_print_vb_obstack, last_dont_print);
612 /* Reset watermark so that we can continue protecting
613 ourselves from whatever we were protecting ourselves. */
614 dont_print_vb_obstack = tmp_obstack;
618 /* Print value of a static member.
619 To avoid infinite recursion when printing a class that contains
620 a static instance of the class, we keep the addresses of all printed
621 static member classes in an obstack and refuse to print them more
624 VAL contains the value to print, TYPE, STREAM, RECURSE, and PRETTY
625 have the same meanings as in c_val_print. */
628 cp_print_static_field (type, val, stream, format, recurse, pretty)
634 enum val_prettyprint pretty;
636 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
638 CORE_ADDR *first_dont_print;
642 = (CORE_ADDR *) obstack_base (&dont_print_statmem_obstack);
643 i = (CORE_ADDR *) obstack_next_free (&dont_print_statmem_obstack)
648 if (VALUE_ADDRESS (val) == first_dont_print[i])
650 fputs_filtered ("<same as static member of an already seen type>",
656 obstack_grow (&dont_print_statmem_obstack, (char *) &VALUE_ADDRESS (val),
659 CHECK_TYPEDEF (type);
660 cp_print_value_fields (type, type, VALUE_CONTENTS_ALL (val), VALUE_EMBEDDED_OFFSET (val), VALUE_ADDRESS (val),
661 stream, format, recurse, pretty, NULL, 1);
664 val_print (type, VALUE_CONTENTS_ALL (val), VALUE_EMBEDDED_OFFSET (val), VALUE_ADDRESS (val),
665 stream, format, 0, recurse, pretty);
669 cp_print_class_member (valaddr, domain, stream, prefix)
676 /* VAL is a byte offset into the structure type DOMAIN.
677 Find the name of the field for that offset and
681 register unsigned int i;
682 unsigned len = TYPE_NFIELDS (domain);
684 /* @@ Make VAL into bit offset */
686 /* Note: HP aCC generates offsets that are the real byte offsets added
687 to a constant bias 0x20000000 (1 << 29). This constant bias gets
688 shifted out in the code below -- joyous happenstance! */
690 /* Note: HP cfront uses a constant bias of 1; if we support this
691 compiler ever, we will have to adjust the computation below */
693 LONGEST val = unpack_long (builtin_type_int, valaddr) << 3;
694 for (i = TYPE_N_BASECLASSES (domain); i < len; i++)
696 int bitpos = TYPE_FIELD_BITPOS (domain, i);
700 if (val < bitpos && i != 0)
702 /* Somehow pointing into a field. */
704 extra = (val - TYPE_FIELD_BITPOS (domain, i));
715 fprintf_filtered (stream, prefix);
716 name = type_name_no_tag (domain);
718 fputs_filtered (name, stream);
720 c_type_print_base (domain, stream, 0, 0);
721 fprintf_filtered (stream, "::");
722 fputs_filtered (TYPE_FIELD_NAME (domain, i), stream);
724 fprintf_filtered (stream, " + %d bytes", extra);
726 fprintf_filtered (stream, " (offset in bits)");
729 fprintf_filtered (stream, "%d", val >> 3);
733 /* This function prints out virtual table entries for a class; it
734 * recurses on the base classes to find all virtual functions
735 * available in a class.
737 * pai/1997-05-21 Note: As the name suggests, it's currently
738 * implemented for HP aCC runtime only. g++ objects are handled
739 * differently and I have made no attempt to fold that logic in
740 * here. The runtime layout is different for the two cases. Also,
741 * this currently has only the code for non-RRBC layouts generated by
742 * the HP aCC compiler; RRBC code is stubbed out and will have to be
747 cp_print_hpacc_virtual_table_entries (type, vfuncs, v, stream, format, recurse, pretty)
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, recurse, pretty);
767 /* Now deal with vfuncs declared in this class */
768 for (fn = 0; fn < TYPE_NFN_FIELDS (type); fn++)
769 for (oi = 0; oi < TYPE_FN_FIELDLIST_LENGTH (type, fn); oi++)
770 if (TYPE_FN_FIELD_VIRTUAL_P (TYPE_FN_FIELDLIST1 (type, fn), oi))
774 /* virtual function offset */
775 int vx = TYPE_FN_FIELD_VOFFSET (TYPE_FN_FIELDLIST1 (type, fn), oi) - 1;
777 /* Get the address of the vfunction entry */
778 value_ptr vf = value_copy (v);
780 (void) value_fetch_lazy (vf);
781 vf->aligner.contents[0] += 4 * (HP_ACC_VFUNC_START + vx); /* adjust by offset */
782 vf = value_ind (vf); /* get the entry */
783 VALUE_TYPE (vf) = VALUE_TYPE (v); /* make it a pointer */
785 /* print out the entry */
786 val_print (VALUE_TYPE (vf), VALUE_CONTENTS (vf), 0, 0,
787 stream, format, 0, recurse + 1, pretty);
788 vf_name = cplus_demangle (TYPE_FN_FIELD_PHYSNAME (TYPE_FN_FIELDLIST1 (type, fn), oi),
789 DMGL_ARM); /* pai: (temp) FIXME Maybe this should be DMGL_ANSI */
790 fprintf_filtered (stream, " %s", vf_name);
792 fputs_filtered (", ", stream);
799 _initialize_cp_valprint ()
802 (add_set_cmd ("static-members", class_support, var_boolean,
803 (char *) &static_field_print,
804 "Set printing of C++ static members.",
807 /* Turn on printing of static fields. */
808 static_field_print = 1;
811 (add_set_cmd ("vtbl", class_support, var_boolean, (char *) &vtblprint,
812 "Set printing of C++ virtual function tables.",
817 (add_set_cmd ("object", class_support, var_boolean, (char *) &objectprint,
818 "Set printing of object's derived type based on vtable info.",
822 /* Give people the defaults which they are used to. */
825 obstack_begin (&dont_print_vb_obstack, 32 * sizeof (struct type *));
826 obstack_specify_allocation (&dont_print_statmem_obstack,
827 32 * sizeof (CORE_ADDR), sizeof (CORE_ADDR),