1 /* Support for printing C++ values for GDB, the GNU debugger.
2 Copyright 1986, 1988, 1989, 1991, 1994-1996, 2000
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 (void);
50 static void cp_print_static_field (struct type *, value_ptr,
51 struct ui_file *, int, int,
52 enum val_prettyprint);
54 static void cp_print_value (struct type *, struct type *, char *, int,
55 CORE_ADDR, struct ui_file *, int, int,
56 enum val_prettyprint, struct type **);
58 static void cp_print_hpacc_virtual_table_entries (struct type *, int *,
60 struct ui_file *, int,
62 enum val_prettyprint);
66 cp_print_class_method (char *valaddr, struct type *type, struct ui_file *stream)
69 struct fn_field *f = NULL;
78 struct type *target_type = check_typedef (TYPE_TARGET_TYPE (type));
80 domain = TYPE_DOMAIN_TYPE (target_type);
81 if (domain == (struct type *) NULL)
83 fprintf_filtered (stream, "<unknown>");
86 addr = unpack_pointer (lookup_pointer_type (builtin_type_void), valaddr);
87 if (METHOD_PTR_IS_VIRTUAL (addr))
89 offset = METHOD_PTR_TO_VOFFSET (addr);
90 len = TYPE_NFN_FIELDS (domain);
91 for (i = 0; i < len; i++)
93 f = TYPE_FN_FIELDLIST1 (domain, i);
94 len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
96 for (j = 0; j < len2; j++)
99 if (TYPE_FN_FIELD_VOFFSET (f, j) == offset)
101 if (TYPE_FN_FIELD_STUB (f, j))
102 check_stub_method (domain, i, j);
111 sym = find_pc_function (addr);
114 /* 1997-08-01 Currently unsupported with HP aCC */
115 if (hp_som_som_object_present)
117 fputs_filtered ("?? <not supported with HP aCC>", stream);
120 error ("invalid pointer to member function");
122 len = TYPE_NFN_FIELDS (domain);
123 for (i = 0; i < len; i++)
125 f = TYPE_FN_FIELDLIST1 (domain, i);
126 len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
128 for (j = 0; j < len2; j++)
131 if (TYPE_FN_FIELD_STUB (f, j))
132 check_stub_method (domain, i, j);
133 if (STREQ (SYMBOL_NAME (sym), TYPE_FN_FIELD_PHYSNAME (f, j)))
143 char *demangled_name;
145 fprintf_filtered (stream, "&");
146 fprintf_filtered (stream, kind);
147 demangled_name = cplus_demangle (TYPE_FN_FIELD_PHYSNAME (f, j),
148 DMGL_ANSI | DMGL_PARAMS);
149 if (demangled_name == NULL)
150 fprintf_filtered (stream, "<badly mangled name %s>",
151 TYPE_FN_FIELD_PHYSNAME (f, j));
154 fputs_filtered (demangled_name, stream);
155 free (demangled_name);
160 fprintf_filtered (stream, "(");
161 type_print (type, "", stream, -1);
162 fprintf_filtered (stream, ") %d", (int) addr >> 3);
166 /* This was what it was for gcc 2.4.5 and earlier. */
167 static const char vtbl_ptr_name_old[] =
168 {CPLUS_MARKER, 'v', 't', 'b', 'l', '_', 'p', 't', 'r', '_', 't', 'y', 'p', 'e', 0};
169 /* It was changed to this after 2.4.5. */
170 const char vtbl_ptr_name[] =
171 {'_', '_', 'v', 't', 'b', 'l', '_', 'p', 't', 'r', '_', 't', 'y', 'p', 'e', 0};
173 /* HP aCC uses different names */
174 const char hpacc_vtbl_ptr_name[] =
175 {'_', '_', 'v', 'f', 'p', 0};
176 const char hpacc_vtbl_ptr_type_name[] =
177 {'_', '_', 'v', 'f', 't', 'y', 'p', 0};
180 /* Return truth value for assertion that TYPE is of the type
181 "pointer to virtual function". */
184 cp_is_vtbl_ptr_type (struct type *type)
186 char *typename = type_name_no_tag (type);
188 return (typename != NULL
189 && (STREQ (typename, vtbl_ptr_name)
190 || STREQ (typename, vtbl_ptr_name_old)));
193 /* Return truth value for the assertion that TYPE is of the type
194 "pointer to virtual function table". */
197 cp_is_vtbl_member (struct type *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 (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 STREQN (TYPE_FIELD_NAME (type, n_baseclasses), hpacc_vtbl_ptr_name, 5)) ||
262 fprintf_filtered (stream, "<No data fields>");
265 extern int inspect_it;
267 if (dont_print_statmem == 0)
269 /* If we're at top level, carve out a completely fresh
270 chunk of the obstack and use that until this particular
271 invocation returns. */
272 tmp_obstack = dont_print_statmem_obstack;
273 obstack_finish (&dont_print_statmem_obstack);
276 for (i = n_baseclasses; i < len; i++)
278 /* If requested, skip printing of static fields. */
279 if (!static_field_print && TYPE_FIELD_STATIC (type, i))
282 /* If a vtable pointer appears, we'll print it out later */
283 if (TYPE_HAS_VTABLE (type) && STREQN (TYPE_FIELD_NAME (type, i), hpacc_vtbl_ptr_name, 5))
287 fprintf_filtered (stream, ", ");
288 else if (n_baseclasses > 0)
292 fprintf_filtered (stream, "\n");
293 print_spaces_filtered (2 + 2 * recurse, stream);
294 fputs_filtered ("members of ", stream);
295 fputs_filtered (type_name_no_tag (type), stream);
296 fputs_filtered (": ", stream);
303 fprintf_filtered (stream, "\n");
304 print_spaces_filtered (2 + 2 * recurse, stream);
308 wrap_here (n_spaces (2 + 2 * recurse));
312 if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
313 fputs_filtered ("\"( ptr \"", stream);
315 fputs_filtered ("\"( nodef \"", stream);
316 if (TYPE_FIELD_STATIC (type, i))
317 fputs_filtered ("static ", stream);
318 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
320 DMGL_PARAMS | DMGL_ANSI);
321 fputs_filtered ("\" \"", stream);
322 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
324 DMGL_PARAMS | DMGL_ANSI);
325 fputs_filtered ("\") \"", stream);
329 annotate_field_begin (TYPE_FIELD_TYPE (type, i));
331 if (TYPE_FIELD_STATIC (type, i))
332 fputs_filtered ("static ", stream);
333 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
335 DMGL_PARAMS | DMGL_ANSI);
336 annotate_field_name_end ();
337 /* do not print leading '=' in case of anonymous unions */
338 if (strcmp (TYPE_FIELD_NAME (type, i), ""))
339 fputs_filtered (" = ", stream);
340 annotate_field_value ();
343 if (!TYPE_FIELD_STATIC (type, i) && TYPE_FIELD_PACKED (type, i))
347 /* Bitfields require special handling, especially due to byte
349 if (TYPE_FIELD_IGNORE (type, i))
351 fputs_filtered ("<optimized out or zero length>", stream);
355 v = value_from_longest (TYPE_FIELD_TYPE (type, i),
356 unpack_field_as_long (type, valaddr + offset, i));
358 val_print (TYPE_FIELD_TYPE (type, i), VALUE_CONTENTS (v), 0, 0,
359 stream, format, 0, recurse + 1, pretty);
364 if (TYPE_FIELD_IGNORE (type, i))
366 fputs_filtered ("<optimized out or zero length>", stream);
368 else if (TYPE_FIELD_STATIC (type, i))
370 value_ptr v = value_static_field (type, i);
372 fputs_filtered ("<optimized out>", stream);
374 cp_print_static_field (TYPE_FIELD_TYPE (type, i), v,
375 stream, format, recurse + 1,
380 val_print (TYPE_FIELD_TYPE (type, i),
381 valaddr, offset + TYPE_FIELD_BITPOS (type, i) / 8,
382 address + TYPE_FIELD_BITPOS (type, i) / 8,
383 stream, format, 0, recurse + 1, pretty);
386 annotate_field_end ();
389 if (dont_print_statmem == 0)
391 /* Free the space used to deal with the printing
392 of the members from top level. */
393 obstack_free (&dont_print_statmem_obstack, last_dont_print);
394 dont_print_statmem_obstack = tmp_obstack;
399 fprintf_filtered (stream, "\n");
400 print_spaces_filtered (2 * recurse, stream);
402 } /* if there are data fields */
403 /* Now print out the virtual table pointer if there is one */
404 if (TYPE_HAS_VTABLE (type) && STREQN (TYPE_FIELD_NAME (type, n_baseclasses), hpacc_vtbl_ptr_name, 5))
407 /* First get the virtual table pointer and print it out */
410 fputs_filtered ("__vfp = ", stream);
413 fputs_filtered (", Virtual table at ", stream);
415 /* pai: FIXME 32x64 problem? */
416 /* Not sure what the best notation is in the case where there is no
418 v = value_from_pointer (lookup_pointer_type (builtin_type_unsigned_long),
419 *(unsigned long *) (valaddr + offset));
421 val_print (VALUE_TYPE (v), VALUE_CONTENTS (v), 0, 0,
422 stream, format, 0, recurse + 1, pretty);
427 /* Print out function pointers in vtable. */
429 /* FIXME: then-clause is for non-RRBC layout of virtual
430 * table. The RRBC case in the else-clause is yet to be
431 * implemented. The if (1) below should be changed to a
432 * test for whether the executable we have was compiled
433 * with a version of HP aCC that doesn't have RRBC
438 /* no RRBC support; function pointers embedded directly in vtable */
440 int vfuncs = count_virtual_fns (real_type);
442 fputs_filtered (" {", stream);
444 /* FIXME : doesn't work at present */
446 fprintf_filtered (stream, "%d entr%s: ", vfuncs, vfuncs == 1 ? "y" : "ies");
448 fputs_filtered ("not implemented", stream);
453 /* recursive function that prints all virtual function entries */
455 cp_print_hpacc_virtual_table_entries (real_type, &vfuncs, v, stream, format, recurse, pretty);
457 fputs_filtered ("}", stream);
458 } /* non-RRBC case */
461 /* FIXME -- seem comments above */
462 /* RRBC support present; function pointers are found
463 * by indirection through the class segment entries. */
471 fprintf_filtered (stream, "\n");
472 print_spaces_filtered (2 * recurse, stream);
475 } /* if vtable exists */
477 fprintf_filtered (stream, "}");
480 /* Special val_print routine to avoid printing multiple copies of virtual
484 cp_print_value (struct type *type, struct type *real_type, char *valaddr,
485 int offset, CORE_ADDR address, struct ui_file *stream,
486 int format, int recurse, enum val_prettyprint pretty,
487 struct type **dont_print_vb)
489 struct obstack tmp_obstack;
490 struct type **last_dont_print
491 = (struct type **) obstack_next_free (&dont_print_vb_obstack);
492 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
494 if (dont_print_vb == 0)
496 /* If we're at top level, carve out a completely fresh
497 chunk of the obstack and use that until this particular
498 invocation returns. */
499 tmp_obstack = dont_print_vb_obstack;
500 /* Bump up the high-water mark. Now alpha is omega. */
501 obstack_finish (&dont_print_vb_obstack);
504 for (i = 0; i < n_baseclasses; i++)
508 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
509 char *basename = TYPE_NAME (baseclass);
512 if (BASETYPE_VIA_VIRTUAL (type, i))
514 struct type **first_dont_print
515 = (struct type **) obstack_base (&dont_print_vb_obstack);
517 int j = (struct type **) obstack_next_free (&dont_print_vb_obstack)
521 if (baseclass == first_dont_print[j])
524 obstack_ptr_grow (&dont_print_vb_obstack, baseclass);
527 if (TYPE_HAS_VTABLE (type) && BASETYPE_VIA_VIRTUAL (type, i))
529 /* Assume HP/Taligent runtime convention */
530 find_rt_vbase_offset (type, TYPE_BASECLASS (type, i),
531 valaddr, offset, &boffset, &skip);
533 error ("Virtual base class offset not found from vtable while printing");
534 base_valaddr = valaddr;
538 boffset = baseclass_offset (type, i, valaddr + offset, address + offset);
539 skip = ((boffset == -1) || (boffset + offset) < 0) ? 1 : -1;
541 if (BASETYPE_VIA_VIRTUAL (type, i))
543 /* The virtual base class pointer might have been clobbered by the
544 user program. Make sure that it still points to a valid memory
547 if (boffset != -1 && ((boffset + offset) < 0 || (boffset + offset) >= TYPE_LENGTH (type)))
549 base_valaddr = (char *) alloca (TYPE_LENGTH (baseclass));
550 if (target_read_memory (address + boffset, base_valaddr,
551 TYPE_LENGTH (baseclass)) != 0)
555 base_valaddr = valaddr;
558 base_valaddr = valaddr;
561 /* now do the printing */
564 fprintf_filtered (stream, "\n");
565 print_spaces_filtered (2 * recurse, stream);
567 fputs_filtered ("<", stream);
568 /* Not sure what the best notation is in the case where there is no
570 fputs_filtered (basename ? basename : "", stream);
571 fputs_filtered ("> = ", stream);
575 fprintf_filtered (stream, "<invalid address>");
577 cp_print_value_fields (baseclass, real_type, base_valaddr, offset + boffset, address,
578 stream, format, recurse, pretty,
579 (struct type **) obstack_base (&dont_print_vb_obstack),
581 fputs_filtered (", ", stream);
587 if (dont_print_vb == 0)
589 /* Free the space used to deal with the printing
590 of this type from top level. */
591 obstack_free (&dont_print_vb_obstack, last_dont_print);
592 /* Reset watermark so that we can continue protecting
593 ourselves from whatever we were protecting ourselves. */
594 dont_print_vb_obstack = tmp_obstack;
598 /* Print value of a static member.
599 To avoid infinite recursion when printing a class that contains
600 a static instance of the class, we keep the addresses of all printed
601 static member classes in an obstack and refuse to print them more
604 VAL contains the value to print, TYPE, STREAM, RECURSE, and PRETTY
605 have the same meanings as in c_val_print. */
608 cp_print_static_field (struct type *type, value_ptr val, struct ui_file *stream,
609 int format, int recurse, enum val_prettyprint pretty)
611 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
613 CORE_ADDR *first_dont_print;
617 = (CORE_ADDR *) obstack_base (&dont_print_statmem_obstack);
618 i = (CORE_ADDR *) obstack_next_free (&dont_print_statmem_obstack)
623 if (VALUE_ADDRESS (val) == first_dont_print[i])
625 fputs_filtered ("<same as static member of an already seen type>",
631 obstack_grow (&dont_print_statmem_obstack, (char *) &VALUE_ADDRESS (val),
634 CHECK_TYPEDEF (type);
635 cp_print_value_fields (type, type, VALUE_CONTENTS_ALL (val), VALUE_EMBEDDED_OFFSET (val), VALUE_ADDRESS (val),
636 stream, format, recurse, pretty, NULL, 1);
639 val_print (type, VALUE_CONTENTS_ALL (val), VALUE_EMBEDDED_OFFSET (val), VALUE_ADDRESS (val),
640 stream, format, 0, recurse, pretty);
644 cp_print_class_member (char *valaddr, struct type *domain,
645 struct ui_file *stream, char *prefix)
648 /* VAL is a byte offset into the structure type DOMAIN.
649 Find the name of the field for that offset and
653 register unsigned int i;
654 unsigned len = TYPE_NFIELDS (domain);
656 /* @@ Make VAL into bit offset */
658 /* Note: HP aCC generates offsets that are the real byte offsets added
659 to a constant bias 0x20000000 (1 << 29). This constant bias gets
660 shifted out in the code below -- joyous happenstance! */
662 /* Note: HP cfront uses a constant bias of 1; if we support this
663 compiler ever, we will have to adjust the computation below */
665 LONGEST val = unpack_long (builtin_type_int, valaddr) << 3;
666 for (i = TYPE_N_BASECLASSES (domain); i < len; i++)
668 int bitpos = TYPE_FIELD_BITPOS (domain, i);
672 if (val < bitpos && i != 0)
674 /* Somehow pointing into a field. */
676 extra = (val - TYPE_FIELD_BITPOS (domain, i));
687 fprintf_filtered (stream, prefix);
688 name = type_name_no_tag (domain);
690 fputs_filtered (name, stream);
692 c_type_print_base (domain, stream, 0, 0);
693 fprintf_filtered (stream, "::");
694 fputs_filtered (TYPE_FIELD_NAME (domain, i), stream);
696 fprintf_filtered (stream, " + %d bytes", extra);
698 fprintf_filtered (stream, " (offset in bits)");
701 fprintf_filtered (stream, "%ld", (long) (val >> 3));
705 /* This function prints out virtual table entries for a class; it
706 * recurses on the base classes to find all virtual functions
707 * available in a class.
709 * pai/1997-05-21 Note: As the name suggests, it's currently
710 * implemented for HP aCC runtime only. g++ objects are handled
711 * differently and I have made no attempt to fold that logic in
712 * here. The runtime layout is different for the two cases. Also,
713 * this currently has only the code for non-RRBC layouts generated by
714 * the HP aCC compiler; RRBC code is stubbed out and will have to be
719 cp_print_hpacc_virtual_table_entries (struct type *type, int *vfuncs,
720 value_ptr v, struct ui_file *stream,
721 int format, int recurse,
722 enum val_prettyprint pretty)
726 /* pai: FIXME this function doesn't work. It should handle a given
727 * virtual function only once (latest redefinition in class hierarchy)
730 /* Recursion on other classes that can share the same vtable */
731 struct type *pbc = primary_base_class (type);
733 cp_print_hpacc_virtual_table_entries (pbc, vfuncs, v, stream, format, recurse, pretty);
735 /* Now deal with vfuncs declared in this class */
736 for (fn = 0; fn < TYPE_NFN_FIELDS (type); fn++)
737 for (oi = 0; oi < TYPE_FN_FIELDLIST_LENGTH (type, fn); oi++)
738 if (TYPE_FN_FIELD_VIRTUAL_P (TYPE_FN_FIELDLIST1 (type, fn), oi))
742 /* virtual function offset */
743 int vx = TYPE_FN_FIELD_VOFFSET (TYPE_FN_FIELDLIST1 (type, fn), oi) - 1;
745 /* Get the address of the vfunction entry */
746 value_ptr vf = value_copy (v);
748 (void) value_fetch_lazy (vf);
749 vf->aligner.contents[0] += 4 * (HP_ACC_VFUNC_START + vx); /* adjust by offset */
750 vf = value_ind (vf); /* get the entry */
751 VALUE_TYPE (vf) = VALUE_TYPE (v); /* make it a pointer */
753 /* print out the entry */
754 val_print (VALUE_TYPE (vf), VALUE_CONTENTS (vf), 0, 0,
755 stream, format, 0, recurse + 1, pretty);
756 vf_name = cplus_demangle (TYPE_FN_FIELD_PHYSNAME (TYPE_FN_FIELDLIST1 (type, fn), oi),
757 DMGL_ARM); /* pai: (temp) FIXME Maybe this should be DMGL_ANSI */
758 fprintf_filtered (stream, " %s", vf_name);
760 fputs_filtered (", ", stream);
767 _initialize_cp_valprint (void)
770 (add_set_cmd ("static-members", class_support, var_boolean,
771 (char *) &static_field_print,
772 "Set printing of C++ static members.",
775 /* Turn on printing of static fields. */
776 static_field_print = 1;
779 (add_set_cmd ("vtbl", class_support, var_boolean, (char *) &vtblprint,
780 "Set printing of C++ virtual function tables.",
785 (add_set_cmd ("object", class_support, var_boolean, (char *) &objectprint,
786 "Set printing of object's derived type based on vtable info.",
790 /* Give people the defaults which they are used to. */
793 obstack_begin (&dont_print_vb_obstack, 32 * sizeof (struct type *));
794 obstack_specify_allocation (&dont_print_statmem_obstack,
795 32 * sizeof (CORE_ADDR), sizeof (CORE_ADDR),