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. */
27 #include "expression.h"
33 #include "gdb_string.h"
37 /* Indication of presence of HP-compiled object files */
38 extern int hp_som_som_object_present; /* defined in symtab.c */
41 int vtblprint; /* Controls printing of vtbl's */
42 int objectprint; /* Controls looking up an object's derived type
43 using what we find in its vtables. */
44 int static_field_print; /* Controls printing of static fields. */
46 static struct obstack dont_print_vb_obstack;
47 static struct obstack dont_print_statmem_obstack;
49 extern void _initialize_cp_valprint (void);
51 static void cp_print_static_field (struct type *, value_ptr,
52 struct ui_file *, int, int,
53 enum val_prettyprint);
55 static void cp_print_value (struct type *, struct type *, char *, int,
56 CORE_ADDR, struct ui_file *, int, int,
57 enum val_prettyprint, struct type **);
59 static void cp_print_hpacc_virtual_table_entries (struct type *, int *,
61 struct ui_file *, int,
63 enum val_prettyprint);
67 cp_print_class_method (char *valaddr, struct type *type, 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 (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 xfree (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 (struct type *type)
187 char *typename = type_name_no_tag (type);
189 return (typename != NULL
190 && (STREQ (typename, vtbl_ptr_name)
191 || STREQ (typename, vtbl_ptr_name_old)));
194 /* Return truth value for the assertion that TYPE is of the type
195 "pointer to virtual function table". */
198 cp_is_vtbl_member (struct type *type)
200 if (TYPE_CODE (type) == TYPE_CODE_PTR)
202 type = TYPE_TARGET_TYPE (type);
203 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
205 type = TYPE_TARGET_TYPE (type);
206 if (TYPE_CODE (type) == TYPE_CODE_STRUCT /* if not using thunks */
207 || TYPE_CODE (type) == TYPE_CODE_PTR) /* if using thunks */
209 /* Virtual functions tables are full of pointers
210 to virtual functions. */
211 return cp_is_vtbl_ptr_type (type);
218 /* Mutually recursive subroutines of cp_print_value and c_val_print to
219 print out a structure's fields: cp_print_value_fields and cp_print_value.
221 TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and PRETTY have the
222 same meanings as in cp_print_value and c_val_print.
224 2nd argument REAL_TYPE is used to carry over the type of the derived
225 class across the recursion to base classes.
227 DONT_PRINT is an array of baseclass types that we
228 should not print, or zero if called from top level. */
231 cp_print_value_fields (struct type *type, struct type *real_type, char *valaddr,
232 int offset, CORE_ADDR address, struct ui_file *stream,
233 int format, int recurse, enum val_prettyprint pretty,
234 struct type **dont_print_vb, int dont_print_statmem)
236 int i, len, n_baseclasses;
237 struct obstack tmp_obstack;
238 char *last_dont_print = obstack_next_free (&dont_print_statmem_obstack);
241 CHECK_TYPEDEF (type);
243 fprintf_filtered (stream, "{");
244 len = TYPE_NFIELDS (type);
245 n_baseclasses = TYPE_N_BASECLASSES (type);
247 /* First, print out baseclasses such that we don't print
248 duplicates of virtual baseclasses. */
250 if (n_baseclasses > 0)
251 cp_print_value (type, real_type, valaddr, offset, address, stream,
252 format, recurse + 1, pretty, dont_print_vb);
254 /* Second, print out data fields */
256 /* If there are no data fields, or if the only field is the
257 * vtbl pointer, skip this part */
258 if ((len == n_baseclasses) ||
259 ((len - n_baseclasses == 1) &&
260 TYPE_HAS_VTABLE (type) &&
261 STREQN (TYPE_FIELD_NAME (type, n_baseclasses), hpacc_vtbl_ptr_name, 5)) ||
263 fprintf_filtered (stream, "<No data fields>");
266 extern int inspect_it;
268 if (dont_print_statmem == 0)
270 /* If we're at top level, carve out a completely fresh
271 chunk of the obstack and use that until this particular
272 invocation returns. */
273 tmp_obstack = dont_print_statmem_obstack;
274 obstack_finish (&dont_print_statmem_obstack);
277 for (i = n_baseclasses; i < len; i++)
279 /* If requested, skip printing of static fields. */
280 if (!static_field_print && TYPE_FIELD_STATIC (type, i))
283 /* If a vtable pointer appears, we'll print it out later */
284 if (TYPE_HAS_VTABLE (type) && STREQN (TYPE_FIELD_NAME (type, i), hpacc_vtbl_ptr_name, 5))
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 (TYPE_FIELD_TYPE (type, i),
357 unpack_field_as_long (type, valaddr + offset, i));
359 val_print (TYPE_FIELD_TYPE (type, i), VALUE_CONTENTS (v), 0, 0,
360 stream, format, 0, recurse + 1, pretty);
365 if (TYPE_FIELD_IGNORE (type, i))
367 fputs_filtered ("<optimized out or zero length>", stream);
369 else if (TYPE_FIELD_STATIC (type, i))
371 value_ptr v = value_static_field (type, i);
373 fputs_filtered ("<optimized out>", stream);
375 cp_print_static_field (TYPE_FIELD_TYPE (type, i), v,
376 stream, format, recurse + 1,
381 val_print (TYPE_FIELD_TYPE (type, i),
382 valaddr, offset + TYPE_FIELD_BITPOS (type, i) / 8,
383 address + TYPE_FIELD_BITPOS (type, i) / 8,
384 stream, format, 0, recurse + 1, pretty);
387 annotate_field_end ();
390 if (dont_print_statmem == 0)
392 /* Free the space used to deal with the printing
393 of the members from top level. */
394 obstack_free (&dont_print_statmem_obstack, last_dont_print);
395 dont_print_statmem_obstack = tmp_obstack;
400 fprintf_filtered (stream, "\n");
401 print_spaces_filtered (2 * recurse, stream);
403 } /* if there are data fields */
404 /* Now print out the virtual table pointer if there is one */
405 if (TYPE_HAS_VTABLE (type) && STREQN (TYPE_FIELD_NAME (type, n_baseclasses), hpacc_vtbl_ptr_name, 5))
408 /* First get the virtual table pointer and print it out */
411 fputs_filtered ("__vfp = ", stream);
414 fputs_filtered (", Virtual table at ", stream);
416 /* pai: FIXME 32x64 problem? */
417 /* Not sure what the best notation is in the case where there is no
419 v = value_from_pointer (lookup_pointer_type (builtin_type_unsigned_long),
420 *(unsigned long *) (valaddr + offset));
422 val_print (VALUE_TYPE (v), VALUE_CONTENTS (v), 0, 0,
423 stream, format, 0, recurse + 1, pretty);
428 /* Print out function pointers in vtable. */
430 /* FIXME: then-clause is for non-RRBC layout of virtual
431 * table. The RRBC case in the else-clause is yet to be
432 * implemented. The if (1) below should be changed to a
433 * test for whether the executable we have was compiled
434 * with a version of HP aCC that doesn't have RRBC
439 /* no RRBC support; function pointers embedded directly in vtable */
441 int vfuncs = count_virtual_fns (real_type);
443 fputs_filtered (" {", stream);
445 /* FIXME : doesn't work at present */
447 fprintf_filtered (stream, "%d entr%s: ", vfuncs, vfuncs == 1 ? "y" : "ies");
449 fputs_filtered ("not implemented", stream);
454 /* recursive function that prints all virtual function entries */
456 cp_print_hpacc_virtual_table_entries (real_type, &vfuncs, v, stream, format, recurse, pretty);
458 fputs_filtered ("}", stream);
459 } /* non-RRBC case */
462 /* FIXME -- see comments above */
463 /* RRBC support present; function pointers are found
464 * by indirection through the class segment entries. */
472 fprintf_filtered (stream, "\n");
473 print_spaces_filtered (2 * recurse, stream);
476 } /* if vtable exists */
478 fprintf_filtered (stream, "}");
481 /* Special val_print routine to avoid printing multiple copies of virtual
485 cp_print_value (struct type *type, struct type *real_type, char *valaddr,
486 int offset, CORE_ADDR address, struct ui_file *stream,
487 int format, int recurse, enum val_prettyprint pretty,
488 struct type **dont_print_vb)
490 struct obstack tmp_obstack;
491 struct type **last_dont_print
492 = (struct type **) obstack_next_free (&dont_print_vb_obstack);
493 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
495 if (dont_print_vb == 0)
497 /* If we're at top level, carve out a completely fresh
498 chunk of the obstack and use that until this particular
499 invocation returns. */
500 tmp_obstack = dont_print_vb_obstack;
501 /* Bump up the high-water mark. Now alpha is omega. */
502 obstack_finish (&dont_print_vb_obstack);
505 for (i = 0; i < n_baseclasses; i++)
509 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
510 char *basename = TYPE_NAME (baseclass);
513 if (BASETYPE_VIA_VIRTUAL (type, i))
515 struct type **first_dont_print
516 = (struct type **) obstack_base (&dont_print_vb_obstack);
518 int j = (struct type **) obstack_next_free (&dont_print_vb_obstack)
522 if (baseclass == first_dont_print[j])
525 obstack_ptr_grow (&dont_print_vb_obstack, baseclass);
528 if (TYPE_HAS_VTABLE (type) && BASETYPE_VIA_VIRTUAL (type, i))
530 /* Assume HP/Taligent runtime convention */
531 find_rt_vbase_offset (type, TYPE_BASECLASS (type, i),
532 valaddr, offset, &boffset, &skip);
534 error ("Virtual base class offset not found from vtable while printing");
535 base_valaddr = valaddr;
539 boffset = baseclass_offset (type, i, valaddr + offset, address + offset);
540 skip = ((boffset == -1) || (boffset + offset) < 0) ? 1 : -1;
542 if (BASETYPE_VIA_VIRTUAL (type, i))
544 /* The virtual base class pointer might have been clobbered by the
545 user program. Make sure that it still points to a valid memory
548 if (boffset != -1 && ((boffset + offset) < 0 || (boffset + offset) >= TYPE_LENGTH (type)))
550 base_valaddr = (char *) alloca (TYPE_LENGTH (baseclass));
551 if (target_read_memory (address + boffset, base_valaddr,
552 TYPE_LENGTH (baseclass)) != 0)
556 base_valaddr = valaddr;
559 base_valaddr = valaddr;
562 /* now do the printing */
565 fprintf_filtered (stream, "\n");
566 print_spaces_filtered (2 * recurse, stream);
568 fputs_filtered ("<", stream);
569 /* Not sure what the best notation is in the case where there is no
571 fputs_filtered (basename ? basename : "", stream);
572 fputs_filtered ("> = ", stream);
576 fprintf_filtered (stream, "<invalid address>");
578 cp_print_value_fields (baseclass, real_type, base_valaddr, offset + boffset, address,
579 stream, format, recurse, pretty,
580 (struct type **) obstack_base (&dont_print_vb_obstack),
582 fputs_filtered (", ", stream);
588 if (dont_print_vb == 0)
590 /* Free the space used to deal with the printing
591 of this type from top level. */
592 obstack_free (&dont_print_vb_obstack, last_dont_print);
593 /* Reset watermark so that we can continue protecting
594 ourselves from whatever we were protecting ourselves. */
595 dont_print_vb_obstack = tmp_obstack;
599 /* Print value of a static member.
600 To avoid infinite recursion when printing a class that contains
601 a static instance of the class, we keep the addresses of all printed
602 static member classes in an obstack and refuse to print them more
605 VAL contains the value to print, TYPE, STREAM, RECURSE, and PRETTY
606 have the same meanings as in c_val_print. */
609 cp_print_static_field (struct type *type, value_ptr val, struct ui_file *stream,
610 int format, int recurse, enum val_prettyprint pretty)
612 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
614 CORE_ADDR *first_dont_print;
618 = (CORE_ADDR *) obstack_base (&dont_print_statmem_obstack);
619 i = (CORE_ADDR *) obstack_next_free (&dont_print_statmem_obstack)
624 if (VALUE_ADDRESS (val) == first_dont_print[i])
626 fputs_filtered ("<same as static member of an already seen type>",
632 obstack_grow (&dont_print_statmem_obstack, (char *) &VALUE_ADDRESS (val),
635 CHECK_TYPEDEF (type);
636 cp_print_value_fields (type, type, VALUE_CONTENTS_ALL (val), VALUE_EMBEDDED_OFFSET (val), VALUE_ADDRESS (val),
637 stream, format, recurse, pretty, NULL, 1);
640 val_print (type, VALUE_CONTENTS_ALL (val), VALUE_EMBEDDED_OFFSET (val), VALUE_ADDRESS (val),
641 stream, format, 0, recurse, pretty);
645 cp_print_class_member (char *valaddr, struct type *domain,
646 struct ui_file *stream, char *prefix)
649 /* VAL is a byte offset into the structure type DOMAIN.
650 Find the name of the field for that offset and
654 register unsigned int i;
655 unsigned len = TYPE_NFIELDS (domain);
657 /* @@ Make VAL into bit offset */
659 /* Note: HP aCC generates offsets that are the real byte offsets added
660 to a constant bias 0x20000000 (1 << 29). This constant bias gets
661 shifted out in the code below -- joyous happenstance! */
663 /* Note: HP cfront uses a constant bias of 1; if we support this
664 compiler ever, we will have to adjust the computation below */
666 LONGEST val = unpack_long (builtin_type_int, valaddr) << 3;
667 for (i = TYPE_N_BASECLASSES (domain); i < len; i++)
669 int bitpos = TYPE_FIELD_BITPOS (domain, i);
673 if (val < bitpos && i != 0)
675 /* Somehow pointing into a field. */
677 extra = (val - TYPE_FIELD_BITPOS (domain, i));
688 fprintf_filtered (stream, prefix);
689 name = type_name_no_tag (domain);
691 fputs_filtered (name, stream);
693 c_type_print_base (domain, stream, 0, 0);
694 fprintf_filtered (stream, "::");
695 fputs_filtered (TYPE_FIELD_NAME (domain, i), stream);
697 fprintf_filtered (stream, " + %d bytes", extra);
699 fprintf_filtered (stream, " (offset in bits)");
702 fprintf_filtered (stream, "%ld", (long) (val >> 3));
706 /* This function prints out virtual table entries for a class; it
707 * recurses on the base classes to find all virtual functions
708 * available in a class.
710 * pai/1997-05-21 Note: As the name suggests, it's currently
711 * implemented for HP aCC runtime only. g++ objects are handled
712 * differently and I have made no attempt to fold that logic in
713 * here. The runtime layout is different for the two cases. Also,
714 * this currently has only the code for non-RRBC layouts generated by
715 * the HP aCC compiler; RRBC code is stubbed out and will have to be
720 cp_print_hpacc_virtual_table_entries (struct type *type, int *vfuncs,
721 value_ptr v, struct ui_file *stream,
722 int format, int recurse,
723 enum val_prettyprint pretty)
727 /* pai: FIXME this function doesn't work. It should handle a given
728 * virtual function only once (latest redefinition in class hierarchy)
731 /* Recursion on other classes that can share the same vtable */
732 struct type *pbc = primary_base_class (type);
734 cp_print_hpacc_virtual_table_entries (pbc, vfuncs, v, stream, format, recurse, pretty);
736 /* Now deal with vfuncs declared in this class */
737 for (fn = 0; fn < TYPE_NFN_FIELDS (type); fn++)
738 for (oi = 0; oi < TYPE_FN_FIELDLIST_LENGTH (type, fn); oi++)
739 if (TYPE_FN_FIELD_VIRTUAL_P (TYPE_FN_FIELDLIST1 (type, fn), oi))
743 /* virtual function offset */
744 int vx = TYPE_FN_FIELD_VOFFSET (TYPE_FN_FIELDLIST1 (type, fn), oi) - 1;
746 /* Get the address of the vfunction entry */
747 value_ptr vf = value_copy (v);
749 (void) value_fetch_lazy (vf);
750 vf->aligner.contents[0] += 4 * (HP_ACC_VFUNC_START + vx); /* adjust by offset */
751 vf = value_ind (vf); /* get the entry */
752 VALUE_TYPE (vf) = VALUE_TYPE (v); /* make it a pointer */
754 /* print out the entry */
755 val_print (VALUE_TYPE (vf), VALUE_CONTENTS (vf), 0, 0,
756 stream, format, 0, recurse + 1, pretty);
757 vf_name = cplus_demangle (TYPE_FN_FIELD_PHYSNAME (TYPE_FN_FIELDLIST1 (type, fn), oi),
758 DMGL_ARM); /* pai: (temp) FIXME Maybe this should be DMGL_ANSI */
759 fprintf_filtered (stream, " %s", vf_name);
761 fputs_filtered (", ", stream);
768 _initialize_cp_valprint (void)
771 (add_set_cmd ("static-members", class_support, var_boolean,
772 (char *) &static_field_print,
773 "Set printing of C++ static members.",
776 /* Turn on printing of static fields. */
777 static_field_print = 1;
780 (add_set_cmd ("vtbl", class_support, var_boolean, (char *) &vtblprint,
781 "Set printing of C++ virtual function tables.",
786 (add_set_cmd ("object", class_support, var_boolean, (char *) &objectprint,
787 "Set printing of object's derived type based on vtable info.",
791 /* Give people the defaults which they are used to. */
794 obstack_begin (&dont_print_vb_obstack, 32 * sizeof (struct type *));
795 obstack_specify_allocation (&dont_print_statmem_obstack,
796 32 * sizeof (CORE_ADDR), sizeof (CORE_ADDR),