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, Boston, MA 02111-1307, USA. */
25 #include "expression.h"
31 #include "gdb_string.h"
35 /* Indication of presence of HP-compiled object files */
36 extern int hp_som_som_object_present; /* defined in symtab.c */
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;
48 cp_print_static_field PARAMS ((struct type *, value_ptr, GDB_FILE *, int, int,
49 enum val_prettyprint));
52 cp_print_value PARAMS ((struct type *, struct type *, char *, int, CORE_ADDR, GDB_FILE *,
53 int, int, enum val_prettyprint, struct type **));
56 cp_print_hpacc_virtual_table_entries PARAMS ((struct type *, int *, value_ptr, GDB_FILE *,
57 int, int, enum val_prettyprint));
61 cp_print_class_method (valaddr, type, stream)
67 struct fn_field *f = NULL;
76 struct type *target_type = check_typedef (TYPE_TARGET_TYPE (type));
78 domain = TYPE_DOMAIN_TYPE (target_type);
79 if (domain == (struct type *)NULL)
81 fprintf_filtered (stream, "<unknown>");
84 addr = unpack_pointer (lookup_pointer_type (builtin_type_void), valaddr);
85 if (METHOD_PTR_IS_VIRTUAL (addr))
87 offset = METHOD_PTR_TO_VOFFSET (addr);
88 len = TYPE_NFN_FIELDS (domain);
89 for (i = 0; i < len; i++)
91 f = TYPE_FN_FIELDLIST1 (domain, i);
92 len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
94 for (j = 0; j < len2; j++)
97 if (TYPE_FN_FIELD_VOFFSET (f, j) == offset)
99 if (TYPE_FN_FIELD_STUB (f, j))
100 check_stub_method (domain, i, j);
109 sym = find_pc_function (addr);
112 /* 1997-08-01 Currently unsupported with HP aCC */
113 if (hp_som_som_object_present)
115 fputs_filtered ("?? <not supported with HP aCC>", stream);
118 error ("invalid pointer to member function");
120 len = TYPE_NFN_FIELDS (domain);
121 for (i = 0; i < len; i++)
123 f = TYPE_FN_FIELDLIST1 (domain, i);
124 len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
126 for (j = 0; j < len2; j++)
129 if (TYPE_FN_FIELD_STUB (f, j))
130 check_stub_method (domain, i, j);
131 if (STREQ (SYMBOL_NAME (sym), TYPE_FN_FIELD_PHYSNAME (f, j)))
141 char *demangled_name;
143 fprintf_filtered (stream, "&");
144 fprintf_filtered (stream, kind);
145 demangled_name = cplus_demangle (TYPE_FN_FIELD_PHYSNAME (f, j),
146 DMGL_ANSI | DMGL_PARAMS);
147 if (demangled_name == NULL)
148 fprintf_filtered (stream, "<badly mangled name %s>",
149 TYPE_FN_FIELD_PHYSNAME (f, j));
152 fputs_filtered (demangled_name, stream);
153 free (demangled_name);
158 fprintf_filtered (stream, "(");
159 type_print (type, "", stream, -1);
160 fprintf_filtered (stream, ") %d", (int) addr >> 3);
164 /* This was what it was for gcc 2.4.5 and earlier. */
165 static const char vtbl_ptr_name_old[] =
166 { CPLUS_MARKER,'v','t','b','l','_','p','t','r','_','t','y','p','e', 0 };
167 /* It was changed to this after 2.4.5. */
168 const char vtbl_ptr_name[] =
169 { '_','_','v','t','b','l','_','p','t','r','_','t','y','p','e', 0 };
171 /* HP aCC uses different names */
172 const char hpacc_vtbl_ptr_name[] =
173 { '_', '_', 'v', 'f', 'p', 0 };
174 const char hpacc_vtbl_ptr_type_name[] =
175 { '_', '_', 'v', 'f', 't', 'y', 'p', 0 };
178 /* Return truth value for assertion that TYPE is of the type
179 "pointer to virtual function". */
182 cp_is_vtbl_ptr_type(type)
185 char *typename = type_name_no_tag (type);
187 return (typename != NULL
188 && (STREQ (typename, vtbl_ptr_name)
189 || STREQ (typename, vtbl_ptr_name_old)));
192 /* Return truth value for the assertion that TYPE is of the type
193 "pointer to virtual function table". */
196 cp_is_vtbl_member(type)
199 if (TYPE_CODE (type) == TYPE_CODE_PTR)
201 type = TYPE_TARGET_TYPE (type);
202 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
204 type = TYPE_TARGET_TYPE (type);
205 if (TYPE_CODE (type) == TYPE_CODE_STRUCT /* if not using thunks */
206 || TYPE_CODE (type) == TYPE_CODE_PTR) /* if using thunks */
208 /* Virtual functions tables are full of pointers
209 to virtual functions. */
210 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 (type, real_type, valaddr, offset, address, stream, format, recurse, pretty,
231 dont_print_vb, dont_print_statmem)
233 struct type *real_type;
240 enum val_prettyprint pretty;
241 struct type **dont_print_vb;
242 int dont_print_statmem;
244 int i, len, n_baseclasses;
245 struct obstack tmp_obstack;
246 char *last_dont_print = obstack_next_free (&dont_print_statmem_obstack);
249 CHECK_TYPEDEF (type);
251 fprintf_filtered (stream, "{");
252 len = TYPE_NFIELDS (type);
253 n_baseclasses = TYPE_N_BASECLASSES (type);
255 /* First, print out baseclasses such that we don't print
256 duplicates of virtual baseclasses. */
258 if (n_baseclasses > 0)
259 cp_print_value (type, real_type, valaddr, offset, address, stream,
260 format, recurse+1, pretty, dont_print_vb);
262 /* Second, print out data fields */
264 /* If there are no data fields, or if the only field is the
265 * vtbl pointer, skip this part */
266 if ((len == n_baseclasses) ||
267 ((len - n_baseclasses == 1) &&
268 TYPE_HAS_VTABLE(type) &&
269 STREQN(TYPE_FIELD_NAME (type, n_baseclasses), hpacc_vtbl_ptr_name, 5)) ||
271 fprintf_filtered (stream, "<No data fields>");
274 extern int inspect_it;
276 if (dont_print_statmem == 0)
278 /* If we're at top level, carve out a completely fresh
279 chunk of the obstack and use that until this particular
280 invocation returns. */
281 tmp_obstack = dont_print_statmem_obstack;
282 obstack_finish (&dont_print_statmem_obstack);
285 for (i = n_baseclasses; i < len; i++)
287 /* If requested, skip printing of static fields. */
288 if (!static_field_print && TYPE_FIELD_STATIC (type, i))
291 /* If a vtable pointer appears, we'll print it out later */
292 if (TYPE_HAS_VTABLE(type) && STREQN(TYPE_FIELD_NAME (type, i), hpacc_vtbl_ptr_name, 5))
296 fprintf_filtered (stream, ", ");
297 else if (n_baseclasses > 0)
301 fprintf_filtered (stream, "\n");
302 print_spaces_filtered (2 + 2 * recurse, stream);
303 fputs_filtered ("members of ", stream);
304 fputs_filtered (type_name_no_tag (type), stream);
305 fputs_filtered (": ", stream);
312 fprintf_filtered (stream, "\n");
313 print_spaces_filtered (2 + 2 * recurse, stream);
317 wrap_here (n_spaces (2 + 2 * recurse));
321 if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
322 fputs_filtered ("\"( ptr \"", stream);
324 fputs_filtered ("\"( nodef \"", stream);
325 if (TYPE_FIELD_STATIC (type, i))
326 fputs_filtered ("static ", stream);
327 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
329 DMGL_PARAMS | DMGL_ANSI);
330 fputs_filtered ("\" \"", stream);
331 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
333 DMGL_PARAMS | DMGL_ANSI);
334 fputs_filtered ("\") \"", stream);
338 annotate_field_begin (TYPE_FIELD_TYPE (type, i));
340 if (TYPE_FIELD_STATIC (type, i))
341 fputs_filtered ("static ", stream);
342 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
344 DMGL_PARAMS | DMGL_ANSI);
345 annotate_field_name_end ();
346 /* do not print leading '=' in case of anonymous unions */
347 if (strcmp (TYPE_FIELD_NAME (type, i), ""))
348 fputs_filtered (" = ", stream);
349 annotate_field_value ();
352 if (!TYPE_FIELD_STATIC (type, i) && TYPE_FIELD_PACKED (type, i))
356 /* Bitfields require special handling, especially due to byte
358 if (TYPE_FIELD_IGNORE (type, i))
360 fputs_filtered ("<optimized out or zero length>", stream);
364 v = value_from_longest (TYPE_FIELD_TYPE (type, i),
365 unpack_field_as_long (type, valaddr + offset, i));
367 val_print (TYPE_FIELD_TYPE(type, i), VALUE_CONTENTS (v), 0, 0,
368 stream, format, 0, recurse + 1, pretty);
373 if (TYPE_FIELD_IGNORE (type, i))
375 fputs_filtered ("<optimized out or zero length>", stream);
377 else if (TYPE_FIELD_STATIC (type, i))
379 value_ptr v = value_static_field (type, i);
381 fputs_filtered ("<optimized out>", stream);
383 cp_print_static_field (TYPE_FIELD_TYPE (type, i), v,
384 stream, format, recurse + 1,
389 val_print (TYPE_FIELD_TYPE (type, i),
390 valaddr, offset + TYPE_FIELD_BITPOS (type, i) / 8,
391 address + TYPE_FIELD_BITPOS (type, i) / 8,
392 stream, format, 0, recurse + 1, pretty);
395 annotate_field_end ();
398 if (dont_print_statmem == 0)
400 /* Free the space used to deal with the printing
401 of the members from top level. */
402 obstack_free (&dont_print_statmem_obstack, last_dont_print);
403 dont_print_statmem_obstack = tmp_obstack;
408 fprintf_filtered (stream, "\n");
409 print_spaces_filtered (2 * recurse, stream);
411 } /* if there are data fields */
412 /* Now print out the virtual table pointer if there is one */
413 if (TYPE_HAS_VTABLE(type) && STREQN(TYPE_FIELD_NAME (type, n_baseclasses), hpacc_vtbl_ptr_name, 5))
416 /* First get the virtual table pointer and print it out*/
419 fputs_filtered ("__vfp = ", stream);
422 fputs_filtered (", Virtual table at ", stream);
424 /* pai: FIXME 32x64 problem? */
425 /* Not sure what the best notation is in the case where there is no
427 v = value_from_longest (lookup_pointer_type (builtin_type_unsigned_long),
428 * (unsigned long *) (valaddr + offset));
430 val_print (VALUE_TYPE (v), VALUE_CONTENTS (v), 0, 0,
431 stream, format, 0, recurse + 1, pretty);
436 /* Print out function pointers in vtable. */
438 /* FIXME: then-clause is for non-RRBC layout of virtual
439 * table. The RRBC case in the else-clause is yet to be
440 * implemented. The if (1) below should be changed to a
441 * test for whether the executable we have was compiled
442 * with a version of HP aCC that doesn't have RRBC
447 /* no RRBC support; function pointers embedded directly in vtable */
449 int vfuncs = count_virtual_fns (real_type);
451 fputs_filtered (" {", stream);
453 /* FIXME : doesn't work at present */
455 fprintf_filtered (stream, "%d entr%s: ", vfuncs, vfuncs == 1 ? "y" : "ies");
457 fputs_filtered ("not implemented", stream);
462 /* recursive function that prints all virtual function entries */
464 cp_print_hpacc_virtual_table_entries (real_type, &vfuncs, v, stream, format, recurse, pretty);
466 fputs_filtered ("}", stream);
467 } /* non-RRBC case */
470 /* FIXME -- seem comments above */
471 /* RRBC support present; function pointers are found
472 * by indirection through the class segment entries. */
480 fprintf_filtered (stream, "\n");
481 print_spaces_filtered (2 * recurse, stream);
484 } /* if vtable exists */
486 fprintf_filtered (stream, "}");
489 /* Special val_print routine to avoid printing multiple copies of virtual
493 cp_print_value (type, real_type, valaddr, offset, address, stream, format, recurse, pretty,
496 struct type *real_type;
503 enum val_prettyprint pretty;
504 struct type **dont_print_vb;
506 struct obstack tmp_obstack;
507 struct type **last_dont_print
508 = (struct type **)obstack_next_free (&dont_print_vb_obstack);
509 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
511 if (dont_print_vb == 0)
513 /* If we're at top level, carve out a completely fresh
514 chunk of the obstack and use that until this particular
515 invocation returns. */
516 tmp_obstack = dont_print_vb_obstack;
517 /* Bump up the high-water mark. Now alpha is omega. */
518 obstack_finish (&dont_print_vb_obstack);
521 for (i = 0; i < n_baseclasses; i++)
525 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
526 char *basename = TYPE_NAME (baseclass);
529 if (BASETYPE_VIA_VIRTUAL (type, i))
531 struct type **first_dont_print
532 = (struct type **)obstack_base (&dont_print_vb_obstack);
534 int j = (struct type **)obstack_next_free (&dont_print_vb_obstack)
538 if (baseclass == first_dont_print[j])
541 obstack_ptr_grow (&dont_print_vb_obstack, baseclass);
544 if (TYPE_HAS_VTABLE (type) && BASETYPE_VIA_VIRTUAL (type, i))
546 /* Assume HP/Taligent runtime convention */
547 find_rt_vbase_offset (type, TYPE_BASECLASS (type, i),
548 valaddr, offset, &boffset, &skip);
550 error ("Virtual base class offset not found from vtable while printing");
551 base_valaddr = valaddr;
555 boffset = baseclass_offset (type, i , valaddr + offset, address + offset);
556 skip = ((boffset == -1) || (boffset+offset) < 0 ) ? 1 : -1;
558 if (BASETYPE_VIA_VIRTUAL (type, i))
560 /* The virtual base class pointer might have been clobbered by the
561 user program. Make sure that it still points to a valid memory
564 if (boffset != -1 && ((boffset+offset) < 0 || (boffset+offset) >= TYPE_LENGTH (type)))
566 base_valaddr = (char *) alloca (TYPE_LENGTH (baseclass));
567 if (target_read_memory (address + boffset, base_valaddr,
568 TYPE_LENGTH (baseclass)) != 0)
572 base_valaddr = valaddr;
575 base_valaddr = valaddr;
578 /* now do the printing */
581 fprintf_filtered (stream, "\n");
582 print_spaces_filtered (2 * recurse, stream);
584 fputs_filtered ("<", stream);
585 /* Not sure what the best notation is in the case where there is no
587 fputs_filtered (basename ? basename : "", stream);
588 fputs_filtered ("> = ", stream);
592 fprintf_filtered (stream, "<invalid address>");
594 cp_print_value_fields (baseclass, real_type, base_valaddr, offset + boffset, address,
595 stream, format, recurse, pretty,
596 (struct type **) obstack_base (&dont_print_vb_obstack),
598 fputs_filtered (", ", stream);
604 if (dont_print_vb == 0)
606 /* Free the space used to deal with the printing
607 of this type from top level. */
608 obstack_free (&dont_print_vb_obstack, last_dont_print);
609 /* Reset watermark so that we can continue protecting
610 ourselves from whatever we were protecting ourselves. */
611 dont_print_vb_obstack = tmp_obstack;
615 /* Print value of a static member.
616 To avoid infinite recursion when printing a class that contains
617 a static instance of the class, we keep the addresses of all printed
618 static member classes in an obstack and refuse to print them more
621 VAL contains the value to print, TYPE, STREAM, RECURSE, and PRETTY
622 have the same meanings as in c_val_print. */
625 cp_print_static_field (type, val, stream, format, recurse, pretty)
631 enum val_prettyprint pretty;
633 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
635 CORE_ADDR *first_dont_print;
639 = (CORE_ADDR *)obstack_base (&dont_print_statmem_obstack);
640 i = (CORE_ADDR *)obstack_next_free (&dont_print_statmem_obstack)
645 if (VALUE_ADDRESS (val) == first_dont_print[i])
647 fputs_filtered ("<same as static member of an already seen type>",
653 obstack_grow (&dont_print_statmem_obstack, (char *) &VALUE_ADDRESS (val),
656 CHECK_TYPEDEF (type);
657 cp_print_value_fields (type, type, VALUE_CONTENTS_ALL (val), VALUE_EMBEDDED_OFFSET (val), VALUE_ADDRESS (val),
658 stream, format, recurse, pretty, NULL, 1);
661 val_print (type, VALUE_CONTENTS_ALL (val), VALUE_EMBEDDED_OFFSET (val), VALUE_ADDRESS (val),
662 stream, format, 0, recurse, pretty);
666 cp_print_class_member (valaddr, domain, stream, prefix)
673 /* VAL is a byte offset into the structure type DOMAIN.
674 Find the name of the field for that offset and
678 register unsigned int i;
679 unsigned len = TYPE_NFIELDS (domain);
681 /* @@ Make VAL into bit offset */
683 /* Note: HP aCC generates offsets that are the real byte offsets added
684 to a constant bias 0x20000000 (1 << 29). This constant bias gets
685 shifted out in the code below -- joyous happenstance! */
687 /* Note: HP cfront uses a constant bias of 1; if we support this
688 compiler ever, we will have to adjust the computation below */
690 LONGEST val = unpack_long (builtin_type_int, valaddr) << 3;
691 for (i = TYPE_N_BASECLASSES (domain); i < len; i++)
693 int bitpos = TYPE_FIELD_BITPOS (domain, i);
697 if (val < bitpos && i != 0)
699 /* Somehow pointing into a field. */
701 extra = (val - TYPE_FIELD_BITPOS (domain, i));
712 fprintf_filtered (stream, prefix);
713 name = type_name_no_tag (domain);
715 fputs_filtered (name, stream);
717 c_type_print_base (domain, stream, 0, 0);
718 fprintf_filtered (stream, "::");
719 fputs_filtered (TYPE_FIELD_NAME (domain, i), stream);
721 fprintf_filtered (stream, " + %d bytes", extra);
723 fprintf_filtered (stream, " (offset in bits)");
726 fprintf_filtered (stream, "%d", val >> 3);
730 /* This function prints out virtual table entries for a class; it
731 * recurses on the base classes to find all virtual functions
732 * available in a class.
734 * pai/1997-05-21 Note: As the name suggests, it's currently
735 * implemented for HP aCC runtime only. g++ objects are handled
736 * differently and I have made no attempt to fold that logic in
737 * here. The runtime layout is different for the two cases. Also,
738 * this currently has only the code for non-RRBC layouts generated by
739 * the HP aCC compiler; RRBC code is stubbed out and will have to be
744 cp_print_hpacc_virtual_table_entries (type, vfuncs, v, stream, format, recurse, pretty)
751 enum val_prettyprint pretty;
755 /* pai: FIXME this function doesn't work. It should handle a given
756 * virtual function only once (latest redefinition in class hierarchy)
759 /* Recursion on other classes that can share the same vtable */
760 struct type * pbc = primary_base_class (type);
762 cp_print_hpacc_virtual_table_entries (pbc, vfuncs, v, stream, format, recurse, pretty);
764 /* Now deal with vfuncs declared in this class */
765 for (fn = 0; fn < TYPE_NFN_FIELDS (type); fn++)
766 for (oi = 0; oi < TYPE_FN_FIELDLIST_LENGTH (type, fn); oi++)
767 if (TYPE_FN_FIELD_VIRTUAL_P (TYPE_FN_FIELDLIST1 (type, fn), oi))
771 /* virtual function offset */
772 int vx = TYPE_FN_FIELD_VOFFSET (TYPE_FN_FIELDLIST1 (type, fn), oi) - 1;
774 /* Get the address of the vfunction entry */
775 value_ptr vf = value_copy (v);
777 (void) value_fetch_lazy (vf);
778 vf->aligner.contents[0] += 4 * (HP_ACC_VFUNC_START + vx); /* adjust by offset */
779 vf = value_ind (vf); /* get the entry */
780 VALUE_TYPE (vf) = VALUE_TYPE (v); /* make it a pointer */
782 /* print out the entry */
783 val_print (VALUE_TYPE (vf), VALUE_CONTENTS (vf), 0, 0,
784 stream, format, 0, recurse + 1, pretty);
785 vf_name = cplus_demangle (TYPE_FN_FIELD_PHYSNAME (TYPE_FN_FIELDLIST1 (type, fn), oi),
786 DMGL_ARM); /* pai: (temp) FIXME Maybe this should be DMGL_ANSI */
787 fprintf_filtered (stream, " %s", vf_name);
789 fputs_filtered (", ", stream);
796 _initialize_cp_valprint ()
799 (add_set_cmd ("static-members", class_support, var_boolean,
800 (char *)&static_field_print,
801 "Set printing of C++ static members.",
804 /* Turn on printing of static fields. */
805 static_field_print = 1;
808 (add_set_cmd ("vtbl", class_support, var_boolean, (char *)&vtblprint,
809 "Set printing of C++ virtual function tables.",
814 (add_set_cmd ("object", class_support, var_boolean, (char *)&objectprint,
815 "Set printing of object's derived type based on vtable info.",
819 /* Give people the defaults which they are used to. */
822 obstack_begin (&dont_print_vb_obstack, 32 * sizeof (struct type *));
823 obstack_specify_allocation (&dont_print_statmem_obstack,
824 32 * sizeof (CORE_ADDR), sizeof (CORE_ADDR),