1 /* Support for printing C++ values for GDB, the GNU debugger.
2 Copyright 1986, 1988, 1989, 1991, 1994, 1995
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., 675 Mass Ave, Cambridge, MA 02139, USA. */
25 #include "expression.h"
32 int vtblprint
; /* Controls printing of vtbl's */
33 int objectprint
; /* Controls looking up an object's derived type
34 using what we find in its vtables. */
35 static int static_field_print
; /* Controls printing of static fields. */
37 static struct obstack dont_print_vb_obstack
;
38 static struct obstack dont_print_statmem_obstack
;
41 cp_print_static_field
PARAMS ((struct type
*, value_ptr
, GDB_FILE
*, int, int,
42 enum val_prettyprint
));
45 cplus_print_value
PARAMS ((struct type
*, char *, GDB_FILE
*, int, int,
46 enum val_prettyprint
, struct type
**));
48 /* BEGIN-FIXME: Hooks into typeprint.c, find a better home for prototypes. */
51 c_type_print_base
PARAMS ((struct type
*, GDB_FILE
*, int, int));
54 c_type_print_varspec_prefix
PARAMS ((struct type
*, GDB_FILE
*, int, int));
57 cp_type_print_method_args
PARAMS ((struct type
**, char *, char *, int,
63 cp_print_class_method (valaddr
, type
, stream
)
69 struct fn_field
*f
= NULL
;
79 check_stub_type (TYPE_TARGET_TYPE (type
));
80 domain
= TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (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
)
109 sym
= find_pc_function (addr
);
112 error ("invalid pointer to member function");
114 len
= TYPE_NFN_FIELDS (domain
);
115 for (i
= 0; i
< len
; i
++)
117 f
= TYPE_FN_FIELDLIST1 (domain
, i
);
118 len2
= TYPE_FN_FIELDLIST_LENGTH (domain
, i
);
120 for (j
= 0; j
< len2
; j
++)
123 if (TYPE_FN_FIELD_STUB (f
, j
))
124 check_stub_method (domain
, i
, j
);
125 if (STREQ (SYMBOL_NAME (sym
), TYPE_FN_FIELD_PHYSNAME (f
, j
)))
135 fprintf_filtered (stream
, "&");
136 c_type_print_varspec_prefix (TYPE_FN_FIELD_TYPE (f
, j
), stream
, 0, 0);
137 fprintf_unfiltered (stream
, kind
);
138 if (TYPE_FN_FIELD_PHYSNAME (f
, j
)[0] == '_'
139 && TYPE_FN_FIELD_PHYSNAME (f
, j
)[1] == CPLUS_MARKER
)
141 cp_type_print_method_args (TYPE_FN_FIELD_ARGS (f
, j
) + 1, "~",
142 TYPE_FN_FIELDLIST_NAME (domain
, i
),
147 cp_type_print_method_args (TYPE_FN_FIELD_ARGS (f
, j
), "",
148 TYPE_FN_FIELDLIST_NAME (domain
, i
),
154 fprintf_filtered (stream
, "(");
155 type_print (type
, "", stream
, -1);
156 fprintf_filtered (stream
, ") %d", (int) addr
>> 3);
160 /* This was what it was for gcc 2.4.5 and earlier. */
161 static const char vtbl_ptr_name_old
[] =
162 { CPLUS_MARKER
,'v','t','b','l','_','p','t','r','_','t','y','p','e', 0 };
163 /* It was changed to this after 2.4.5. */
164 const char vtbl_ptr_name
[] =
165 { '_','_','v','t','b','l','_','p','t','r','_','t','y','p','e', 0 };
167 /* Return truth value for assertion that TYPE is of the type
168 "pointer to virtual function". */
171 cp_is_vtbl_ptr_type(type
)
174 char *typename
= type_name_no_tag (type
);
176 return (typename
!= NULL
177 && (STREQ (typename
, vtbl_ptr_name
)
178 || STREQ (typename
, vtbl_ptr_name_old
)));
181 /* Return truth value for the assertion that TYPE is of the type
182 "pointer to virtual function table". */
185 cp_is_vtbl_member(type
)
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
);
206 /* Mutually recursive subroutines of cplus_print_value and c_val_print to
207 print out a structure's fields: cp_print_value_fields and cplus_print_value.
209 TYPE, VALADDR, STREAM, RECURSE, and PRETTY have the
210 same meanings as in cplus_print_value and c_val_print.
212 DONT_PRINT is an array of baseclass types that we
213 should not print, or zero if called from top level. */
216 cp_print_value_fields (type
, valaddr
, stream
, format
, recurse
, pretty
,
217 dont_print_vb
, dont_print_statmem
)
223 enum val_prettyprint pretty
;
224 struct type
**dont_print_vb
;
225 int dont_print_statmem
;
227 int i
, len
, n_baseclasses
;
228 struct obstack tmp_obstack
;
229 char *last_dont_print
= obstack_next_free (&dont_print_statmem_obstack
);
231 check_stub_type (type
);
233 fprintf_filtered (stream
, "{");
234 len
= TYPE_NFIELDS (type
);
235 n_baseclasses
= TYPE_N_BASECLASSES (type
);
237 /* Print out baseclasses such that we don't print
238 duplicates of virtual baseclasses. */
239 if (n_baseclasses
> 0)
240 cplus_print_value (type
, valaddr
, stream
, format
, recurse
+1, pretty
,
243 if (!len
&& n_baseclasses
== 1)
244 fprintf_filtered (stream
, "<No data fields>");
247 extern int inspect_it
;
250 if (dont_print_statmem
== 0)
252 /* If we're at top level, carve out a completely fresh
253 chunk of the obstack and use that until this particular
254 invocation returns. */
255 tmp_obstack
= dont_print_statmem_obstack
;
256 obstack_finish (&dont_print_statmem_obstack
);
259 for (i
= n_baseclasses
; i
< len
; i
++)
261 /* If requested, skip printing of static fields. */
262 if (!static_field_print
&& TYPE_FIELD_STATIC (type
, i
))
265 fprintf_filtered (stream
, ", ");
266 else if (n_baseclasses
> 0)
270 fprintf_filtered (stream
, "\n");
271 print_spaces_filtered (2 + 2 * recurse
, stream
);
272 fputs_filtered ("members of ", stream
);
273 fputs_filtered (type_name_no_tag (type
), stream
);
274 fputs_filtered (": ", stream
);
281 fprintf_filtered (stream
, "\n");
282 print_spaces_filtered (2 + 2 * recurse
, stream
);
286 wrap_here (n_spaces (2 + 2 * recurse
));
290 if (TYPE_CODE (TYPE_FIELD_TYPE (type
, i
)) == TYPE_CODE_PTR
)
291 fputs_filtered ("\"( ptr \"", stream
);
293 fputs_filtered ("\"( nodef \"", stream
);
294 if (TYPE_FIELD_STATIC (type
, i
))
295 fputs_filtered ("static ", stream
);
296 fprintf_symbol_filtered (stream
, TYPE_FIELD_NAME (type
, i
),
298 DMGL_PARAMS
| DMGL_ANSI
);
299 fputs_filtered ("\" \"", stream
);
300 fprintf_symbol_filtered (stream
, TYPE_FIELD_NAME (type
, i
),
302 DMGL_PARAMS
| DMGL_ANSI
);
303 fputs_filtered ("\") \"", stream
);
307 annotate_field_begin (TYPE_FIELD_TYPE (type
, i
));
309 if (TYPE_FIELD_STATIC (type
, i
))
310 fputs_filtered ("static ", stream
);
311 fprintf_symbol_filtered (stream
, TYPE_FIELD_NAME (type
, i
),
313 DMGL_PARAMS
| DMGL_ANSI
);
314 annotate_field_name_end ();
315 fputs_filtered (" = ", stream
);
316 annotate_field_value ();
319 if (!TYPE_FIELD_STATIC (type
, i
) && TYPE_FIELD_PACKED (type
, i
))
323 /* Bitfields require special handling, especially due to byte
325 if (TYPE_FIELD_IGNORE (type
, i
))
327 fputs_filtered ("<optimized out or zero length>", stream
);
331 v
= value_from_longest (TYPE_FIELD_TYPE (type
, i
),
332 unpack_field_as_long (type
, valaddr
, i
));
334 val_print (TYPE_FIELD_TYPE(type
, i
), VALUE_CONTENTS (v
), 0,
335 stream
, format
, 0, recurse
+ 1, pretty
);
340 if (TYPE_FIELD_IGNORE (type
, i
))
342 fputs_filtered ("<optimized out or zero length>", stream
);
344 else if (TYPE_FIELD_STATIC (type
, i
))
347 char *phys_name
= TYPE_FIELD_STATIC_PHYSNAME (type
, i
);
349 lookup_symbol (phys_name
, 0, VAR_NAMESPACE
, 0, NULL
);
351 fputs_filtered ("<optimized out>", stream
);
354 v
= value_at (TYPE_FIELD_TYPE (type
, i
),
355 (CORE_ADDR
)SYMBOL_BLOCK_VALUE (sym
));
356 cp_print_static_field (TYPE_FIELD_TYPE (type
, i
), v
,
357 stream
, format
, recurse
+ 1,
363 val_print (TYPE_FIELD_TYPE (type
, i
),
364 valaddr
+ TYPE_FIELD_BITPOS (type
, i
) / 8,
365 0, stream
, format
, 0, recurse
+ 1, pretty
);
368 annotate_field_end ();
371 if (dont_print_statmem
== 0)
373 /* Free the space used to deal with the printing
374 of the members from top level. */
375 obstack_free (&dont_print_statmem_obstack
, last_dont_print
);
376 dont_print_statmem_obstack
= tmp_obstack
;
381 fprintf_filtered (stream
, "\n");
382 print_spaces_filtered (2 * recurse
, stream
);
385 fprintf_filtered (stream
, "}");
388 /* Special val_print routine to avoid printing multiple copies of virtual
392 cplus_print_value (type
, valaddr
, stream
, format
, recurse
, pretty
,
399 enum val_prettyprint pretty
;
400 struct type
**dont_print_vb
;
402 struct obstack tmp_obstack
;
403 struct type
**last_dont_print
404 = (struct type
**)obstack_next_free (&dont_print_vb_obstack
);
405 int i
, n_baseclasses
= TYPE_N_BASECLASSES (type
);
407 if (dont_print_vb
== 0)
409 /* If we're at top level, carve out a completely fresh
410 chunk of the obstack and use that until this particular
411 invocation returns. */
412 tmp_obstack
= dont_print_vb_obstack
;
413 /* Bump up the high-water mark. Now alpha is omega. */
414 obstack_finish (&dont_print_vb_obstack
);
417 for (i
= 0; i
< n_baseclasses
; i
++)
419 /* FIXME-32x64--assumes that a target pointer can fit in a char *.
420 Fix it by nuking baseclass_addr. */
425 check_stub_type (TYPE_BASECLASS (type
, i
));
426 basename
= TYPE_NAME (TYPE_BASECLASS (type
, i
));
428 if (BASETYPE_VIA_VIRTUAL (type
, i
))
430 struct type
**first_dont_print
431 = (struct type
**)obstack_base (&dont_print_vb_obstack
);
433 int j
= (struct type
**)obstack_next_free (&dont_print_vb_obstack
)
437 if (TYPE_BASECLASS (type
, i
) == first_dont_print
[j
])
440 obstack_ptr_grow (&dont_print_vb_obstack
, TYPE_BASECLASS (type
, i
));
443 /* Fix to use baseclass_offset instead. FIXME */
444 baddr
= baseclass_addr (type
, i
, valaddr
, 0, &err
);
445 if (err
== 0 && baddr
== 0)
446 error ("could not find virtual baseclass %s\n",
447 basename
? basename
: "");
451 fprintf_filtered (stream
, "\n");
452 print_spaces_filtered (2 * recurse
, stream
);
454 fputs_filtered ("<", stream
);
455 /* Not sure what the best notation is in the case where there is no
457 fputs_filtered (basename
? basename
: "", stream
);
458 fputs_filtered ("> = ", stream
);
461 fprintf_filtered (stream
, "<invalid address ");
462 print_address_numeric ((CORE_ADDR
) baddr
, 1, stream
);
463 fprintf_filtered (stream
, ">");
466 cp_print_value_fields (TYPE_BASECLASS (type
, i
), baddr
, stream
, format
,
468 (struct type
**) obstack_base (&dont_print_vb_obstack
),
470 fputs_filtered (", ", stream
);
476 if (dont_print_vb
== 0)
478 /* Free the space used to deal with the printing
479 of this type from top level. */
480 obstack_free (&dont_print_vb_obstack
, last_dont_print
);
481 /* Reset watermark so that we can continue protecting
482 ourselves from whatever we were protecting ourselves. */
483 dont_print_vb_obstack
= tmp_obstack
;
487 /* Print value of a static member.
488 To avoid infinite recursion when printing a class that contains
489 a static instance of the class, we keep the addresses of all printed
490 static member classes in an obstack and refuse to print them more
493 VAL contains the value to print, TYPE, STREAM, RECURSE, and PRETTY
494 have the same meanings as in c_val_print. */
497 cp_print_static_field (type
, val
, stream
, format
, recurse
, pretty
)
503 enum val_prettyprint pretty
;
505 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
507 CORE_ADDR
*first_dont_print
;
511 = (CORE_ADDR
*)obstack_base (&dont_print_statmem_obstack
);
512 i
= (CORE_ADDR
*)obstack_next_free (&dont_print_statmem_obstack
)
517 if (VALUE_ADDRESS (val
) == first_dont_print
[i
])
519 fputs_filtered ("<same as static member of an already seen type>",
525 obstack_grow (&dont_print_statmem_obstack
, &VALUE_ADDRESS (val
),
528 check_stub_type (type
);
529 cp_print_value_fields (type
, VALUE_CONTENTS (val
),
530 stream
, format
, recurse
, pretty
,
534 val_print (type
, VALUE_CONTENTS (val
), VALUE_ADDRESS (val
),
535 stream
, format
, 0, recurse
, pretty
);
539 cp_print_class_member (valaddr
, domain
, stream
, prefix
)
546 /* VAL is a byte offset into the structure type DOMAIN.
547 Find the name of the field for that offset and
551 register unsigned int i
;
552 unsigned len
= TYPE_NFIELDS (domain
);
553 /* @@ Make VAL into bit offset */
554 LONGEST val
= unpack_long (builtin_type_int
, valaddr
) << 3;
555 for (i
= TYPE_N_BASECLASSES (domain
); i
< len
; i
++)
557 int bitpos
= TYPE_FIELD_BITPOS (domain
, i
);
561 if (val
< bitpos
&& i
!= 0)
563 /* Somehow pointing into a field. */
565 extra
= (val
- TYPE_FIELD_BITPOS (domain
, i
));
576 fprintf_filtered (stream
, prefix
);
577 name
= type_name_no_tag (domain
);
579 fputs_filtered (name
, stream
);
581 c_type_print_base (domain
, stream
, 0, 0);
582 fprintf_filtered (stream
, "::");
583 fputs_filtered (TYPE_FIELD_NAME (domain
, i
), stream
);
585 fprintf_filtered (stream
, " + %d bytes", extra
);
587 fprintf_filtered (stream
, " (offset in bits)");
590 fprintf_filtered (stream
, "%d", val
>> 3);
594 _initialize_cp_valprint ()
597 (add_set_cmd ("static-members", class_support
, var_boolean
,
598 (char *)&static_field_print
,
599 "Set printing of C++ static members.",
602 /* Turn on printing of static fields. */
603 static_field_print
= 1;
606 (add_set_cmd ("vtbl", class_support
, var_boolean
, (char *)&vtblprint
,
607 "Set printing of C++ virtual function tables.",
612 (add_set_cmd ("object", class_support
, var_boolean
, (char *)&objectprint
,
613 "Set printing of object's derived type based on vtable info.",
617 /* Give people the defaults which they are used to. */
620 obstack_begin (&dont_print_vb_obstack
, 32 * sizeof (struct type
*));
621 obstack_specify_allocation (&dont_print_statmem_obstack
,
622 32 * sizeof (CORE_ADDR
), sizeof (CORE_ADDR
),