1 /* Support for printing C++ values for GDB, the GNU debugger.
3 Copyright (C) 1986, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
4 2000, 2001, 2002, 2003, 2005, 2006, 2007 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 3 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, see <http://www.gnu.org/licenses/>. */
22 #include "gdb_obstack.h"
25 #include "expression.h"
31 #include "gdb_string.h"
36 #include "cp-support.h"
39 /* Controls printing of vtbl's */
42 show_vtblprint (struct ui_file
*file
, int from_tty
,
43 struct cmd_list_element
*c
, const char *value
)
45 fprintf_filtered (file
, _("\
46 Printing of C++ virtual function tables is %s.\n"),
50 /* Controls looking up an object's derived type using what we find in
54 show_objectprint (struct ui_file
*file
, int from_tty
,
55 struct cmd_list_element
*c
,
58 fprintf_filtered (file
, _("\
59 Printing of object's derived type based on vtable info is %s.\n"),
63 int static_field_print
; /* Controls printing of static fields. */
65 show_static_field_print (struct ui_file
*file
, int from_tty
,
66 struct cmd_list_element
*c
, const char *value
)
68 fprintf_filtered (file
, _("Printing of C++ static members is %s.\n"),
73 static struct obstack dont_print_vb_obstack
;
74 static struct obstack dont_print_statmem_obstack
;
76 extern void _initialize_cp_valprint (void);
78 static void cp_print_static_field (struct type
*, struct value
*,
79 struct ui_file
*, int, int,
80 enum val_prettyprint
);
82 static void cp_print_value (struct type
*, struct type
*, const gdb_byte
*,
83 int, CORE_ADDR
, struct ui_file
*, int, int,
84 enum val_prettyprint
, struct type
**);
87 /* GCC versions after 2.4.5 use this. */
88 const char vtbl_ptr_name
[] = "__vtbl_ptr_type";
90 /* Return truth value for assertion that TYPE is of the type
91 "pointer to virtual function". */
94 cp_is_vtbl_ptr_type (struct type
*type
)
96 char *typename
= type_name_no_tag (type
);
98 return (typename
!= NULL
&& !strcmp (typename
, vtbl_ptr_name
));
101 /* Return truth value for the assertion that TYPE is of the type
102 "pointer to virtual function table". */
105 cp_is_vtbl_member (struct type
*type
)
107 /* With older versions of g++, the vtbl field pointed to an array
108 of structures. Nowadays it points directly to the structure. */
109 if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
111 type
= TYPE_TARGET_TYPE (type
);
112 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
)
114 type
= TYPE_TARGET_TYPE (type
);
115 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
/* if not using thunks */
116 || TYPE_CODE (type
) == TYPE_CODE_PTR
) /* if using thunks */
118 /* Virtual functions tables are full of pointers
119 to virtual functions. */
120 return cp_is_vtbl_ptr_type (type
);
123 else if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
) /* if not using thunks */
125 return cp_is_vtbl_ptr_type (type
);
127 else if (TYPE_CODE (type
) == TYPE_CODE_PTR
) /* if using thunks */
129 /* The type name of the thunk pointer is NULL when using dwarf2.
130 We could test for a pointer to a function, but there is
131 no type info for the virtual table either, so it wont help. */
132 return cp_is_vtbl_ptr_type (type
);
138 /* Mutually recursive subroutines of cp_print_value and c_val_print to
139 print out a structure's fields: cp_print_value_fields and cp_print_value.
141 TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and PRETTY have the
142 same meanings as in cp_print_value and c_val_print.
144 2nd argument REAL_TYPE is used to carry over the type of the derived
145 class across the recursion to base classes.
147 DONT_PRINT is an array of baseclass types that we
148 should not print, or zero if called from top level. */
151 cp_print_value_fields (struct type
*type
, struct type
*real_type
,
152 const gdb_byte
*valaddr
, int offset
, CORE_ADDR address
,
153 struct ui_file
*stream
, int format
, int recurse
,
154 enum val_prettyprint pretty
,
155 struct type
**dont_print_vb
,int dont_print_statmem
)
157 int i
, len
, n_baseclasses
;
158 char *last_dont_print
= obstack_next_free (&dont_print_statmem_obstack
);
161 CHECK_TYPEDEF (type
);
163 fprintf_filtered (stream
, "{");
164 len
= TYPE_NFIELDS (type
);
165 n_baseclasses
= TYPE_N_BASECLASSES (type
);
167 /* First, print out baseclasses such that we don't print
168 duplicates of virtual baseclasses. */
170 if (n_baseclasses
> 0)
171 cp_print_value (type
, real_type
, valaddr
, offset
, address
, stream
,
172 format
, recurse
+ 1, pretty
, dont_print_vb
);
174 /* Second, print out data fields */
176 /* If there are no data fields, skip this part */
177 if (len
== n_baseclasses
|| !len
)
178 fprintf_filtered (stream
, "<No data fields>");
181 struct obstack tmp_obstack
= dont_print_statmem_obstack
;
183 if (dont_print_statmem
== 0)
185 /* If we're at top level, carve out a completely fresh
186 chunk of the obstack and use that until this particular
187 invocation returns. */
188 obstack_finish (&dont_print_statmem_obstack
);
191 for (i
= n_baseclasses
; i
< len
; i
++)
193 /* If requested, skip printing of static fields. */
194 if (!static_field_print
&& TYPE_FIELD_STATIC (type
, i
))
198 fprintf_filtered (stream
, ", ");
199 else if (n_baseclasses
> 0)
203 fprintf_filtered (stream
, "\n");
204 print_spaces_filtered (2 + 2 * recurse
, stream
);
205 fputs_filtered ("members of ", stream
);
206 fputs_filtered (type_name_no_tag (type
), stream
);
207 fputs_filtered (": ", stream
);
214 fprintf_filtered (stream
, "\n");
215 print_spaces_filtered (2 + 2 * recurse
, stream
);
219 wrap_here (n_spaces (2 + 2 * recurse
));
223 if (TYPE_CODE (TYPE_FIELD_TYPE (type
, i
)) == TYPE_CODE_PTR
)
224 fputs_filtered ("\"( ptr \"", stream
);
226 fputs_filtered ("\"( nodef \"", stream
);
227 if (TYPE_FIELD_STATIC (type
, i
))
228 fputs_filtered ("static ", stream
);
229 fprintf_symbol_filtered (stream
, TYPE_FIELD_NAME (type
, i
),
230 current_language
->la_language
,
231 DMGL_PARAMS
| DMGL_ANSI
);
232 fputs_filtered ("\" \"", stream
);
233 fprintf_symbol_filtered (stream
, TYPE_FIELD_NAME (type
, i
),
234 current_language
->la_language
,
235 DMGL_PARAMS
| DMGL_ANSI
);
236 fputs_filtered ("\") \"", stream
);
240 annotate_field_begin (TYPE_FIELD_TYPE (type
, i
));
242 if (TYPE_FIELD_STATIC (type
, i
))
243 fputs_filtered ("static ", stream
);
244 fprintf_symbol_filtered (stream
, TYPE_FIELD_NAME (type
, i
),
245 current_language
->la_language
,
246 DMGL_PARAMS
| DMGL_ANSI
);
247 annotate_field_name_end ();
248 /* do not print leading '=' in case of anonymous unions */
249 if (strcmp (TYPE_FIELD_NAME (type
, i
), ""))
250 fputs_filtered (" = ", stream
);
251 annotate_field_value ();
254 if (!TYPE_FIELD_STATIC (type
, i
) && TYPE_FIELD_PACKED (type
, i
))
258 /* Bitfields require special handling, especially due to byte
260 if (TYPE_FIELD_IGNORE (type
, i
))
262 fputs_filtered ("<optimized out or zero length>", stream
);
266 v
= value_from_longest
267 (TYPE_FIELD_TYPE (type
, i
),
268 unpack_field_as_long (type
, valaddr
+ offset
, i
));
270 common_val_print (v
, stream
, format
, 0, recurse
+ 1, pretty
);
275 if (TYPE_FIELD_IGNORE (type
, i
))
277 fputs_filtered ("<optimized out or zero length>", stream
);
279 else if (TYPE_FIELD_STATIC (type
, i
))
281 struct value
*v
= value_static_field (type
, i
);
283 fputs_filtered ("<optimized out>", stream
);
285 cp_print_static_field (TYPE_FIELD_TYPE (type
, i
), v
,
286 stream
, format
, recurse
+ 1,
291 val_print (TYPE_FIELD_TYPE (type
, i
),
292 valaddr
, offset
+ TYPE_FIELD_BITPOS (type
, i
) / 8,
293 address
+ TYPE_FIELD_BITPOS (type
, i
) / 8,
294 stream
, format
, 0, recurse
+ 1, pretty
);
297 annotate_field_end ();
300 if (dont_print_statmem
== 0)
302 /* Free the space used to deal with the printing
303 of the members from top level. */
304 obstack_free (&dont_print_statmem_obstack
, last_dont_print
);
305 dont_print_statmem_obstack
= tmp_obstack
;
310 fprintf_filtered (stream
, "\n");
311 print_spaces_filtered (2 * recurse
, stream
);
313 } /* if there are data fields */
315 fprintf_filtered (stream
, "}");
318 /* Special val_print routine to avoid printing multiple copies of virtual
322 cp_print_value (struct type
*type
, struct type
*real_type
,
323 const gdb_byte
*valaddr
, int offset
, CORE_ADDR address
,
324 struct ui_file
*stream
, int format
, int recurse
,
325 enum val_prettyprint pretty
, struct type
**dont_print_vb
)
327 struct type
**last_dont_print
328 = (struct type
**) obstack_next_free (&dont_print_vb_obstack
);
329 struct obstack tmp_obstack
= dont_print_vb_obstack
;
330 int i
, n_baseclasses
= TYPE_N_BASECLASSES (type
);
332 struct type
*thistype
;
334 if (dont_print_vb
== 0)
336 /* If we're at top level, carve out a completely fresh
337 chunk of the obstack and use that until this particular
338 invocation returns. */
339 /* Bump up the high-water mark. Now alpha is omega. */
340 obstack_finish (&dont_print_vb_obstack
);
343 for (i
= 0; i
< n_baseclasses
; i
++)
347 struct type
*baseclass
= check_typedef (TYPE_BASECLASS (type
, i
));
348 char *basename
= TYPE_NAME (baseclass
);
349 const gdb_byte
*base_valaddr
;
351 if (BASETYPE_VIA_VIRTUAL (type
, i
))
353 struct type
**first_dont_print
354 = (struct type
**) obstack_base (&dont_print_vb_obstack
);
356 int j
= (struct type
**) obstack_next_free (&dont_print_vb_obstack
)
360 if (baseclass
== first_dont_print
[j
])
363 obstack_ptr_grow (&dont_print_vb_obstack
, baseclass
);
367 thistype
= real_type
;
369 boffset
= baseclass_offset (type
, i
, valaddr
+ offset
, address
);
370 skip
= ((boffset
== -1) || (boffset
+ offset
) < 0) ? 1 : -1;
372 if (BASETYPE_VIA_VIRTUAL (type
, i
))
374 /* The virtual base class pointer might have been
375 clobbered by the user program. Make sure that it
376 still points to a valid memory location. */
379 && ((boffset
+ offset
) < 0
380 || (boffset
+ offset
) >= TYPE_LENGTH (type
)))
382 /* FIXME (alloca): unsafe if baseclass is really really large. */
383 gdb_byte
*buf
= alloca (TYPE_LENGTH (baseclass
));
385 if (target_read_memory (address
+ boffset
, buf
,
386 TYPE_LENGTH (baseclass
)) != 0)
388 address
= address
+ boffset
;
391 thistype
= baseclass
;
394 base_valaddr
= valaddr
;
397 base_valaddr
= valaddr
;
399 /* now do the printing */
402 fprintf_filtered (stream
, "\n");
403 print_spaces_filtered (2 * recurse
, stream
);
405 fputs_filtered ("<", stream
);
406 /* Not sure what the best notation is in the case where there is no
408 fputs_filtered (basename
? basename
: "", stream
);
409 fputs_filtered ("> = ", stream
);
413 fprintf_filtered (stream
, "<invalid address>");
415 cp_print_value_fields (baseclass
, thistype
, base_valaddr
,
416 thisoffset
+ boffset
, address
+ boffset
,
420 obstack_base (&dont_print_vb_obstack
)),
422 fputs_filtered (", ", stream
);
428 if (dont_print_vb
== 0)
430 /* Free the space used to deal with the printing
431 of this type from top level. */
432 obstack_free (&dont_print_vb_obstack
, last_dont_print
);
433 /* Reset watermark so that we can continue protecting
434 ourselves from whatever we were protecting ourselves. */
435 dont_print_vb_obstack
= tmp_obstack
;
439 /* Print value of a static member.
440 To avoid infinite recursion when printing a class that contains
441 a static instance of the class, we keep the addresses of all printed
442 static member classes in an obstack and refuse to print them more
445 VAL contains the value to print, TYPE, STREAM, RECURSE, and PRETTY
446 have the same meanings as in c_val_print. */
449 cp_print_static_field (struct type
*type
,
451 struct ui_file
*stream
,
454 enum val_prettyprint pretty
)
456 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
458 CORE_ADDR
*first_dont_print
;
462 = (CORE_ADDR
*) obstack_base (&dont_print_statmem_obstack
);
463 i
= (CORE_ADDR
*) obstack_next_free (&dont_print_statmem_obstack
)
468 if (VALUE_ADDRESS (val
) == first_dont_print
[i
])
470 fputs_filtered ("<same as static member of an already"
477 obstack_grow (&dont_print_statmem_obstack
, (char *) &VALUE_ADDRESS (val
),
480 CHECK_TYPEDEF (type
);
481 cp_print_value_fields (type
, type
, value_contents_all (val
),
482 value_embedded_offset (val
), VALUE_ADDRESS (val
),
483 stream
, format
, recurse
, pretty
, NULL
, 1);
486 val_print (type
, value_contents_all (val
),
487 value_embedded_offset (val
), VALUE_ADDRESS (val
),
488 stream
, format
, 0, recurse
, pretty
);
492 /* Find the field in *DOMAIN, or its non-virtual base classes, with bit offset
493 OFFSET. Set *DOMAIN to the containing type and *FIELDNO to the containing
494 field number. If OFFSET is not exactly at the start of some field, set
498 cp_find_class_member (struct type
**domain_p
, int *fieldno
,
505 *domain_p
= check_typedef (*domain_p
);
507 len
= TYPE_NFIELDS (domain
);
509 for (i
= TYPE_N_BASECLASSES (domain
); i
< len
; i
++)
511 LONGEST bitpos
= TYPE_FIELD_BITPOS (domain
, i
);
514 if (offset
== bitpos
)
521 for (i
= 0; i
< TYPE_N_BASECLASSES (domain
); i
++)
523 LONGEST bitpos
= TYPE_FIELD_BITPOS (domain
, i
);
524 LONGEST bitsize
= 8 * TYPE_LENGTH (TYPE_FIELD_TYPE (domain
, i
));
526 if (offset
>= bitpos
&& offset
< bitpos
+ bitsize
)
528 *domain_p
= TYPE_FIELD_TYPE (domain
, i
);
529 cp_find_class_member (domain_p
, fieldno
, offset
- bitpos
);
538 cp_print_class_member (const gdb_byte
*valaddr
, struct type
*domain
,
539 struct ui_file
*stream
, char *prefix
)
541 /* VAL is a byte offset into the structure type DOMAIN.
542 Find the name of the field for that offset and
544 unsigned int fieldno
;
546 LONGEST val
= unpack_long (builtin_type_long
, valaddr
);
548 /* Pointers to data members are usually byte offsets into an object.
549 Because a data member can have offset zero, and a NULL pointer to
550 member must be distinct from any valid non-NULL pointer to
551 member, either the value is biased or the NULL value has a
552 special representation; both are permitted by ISO C++. HP aCC
553 used a bias of 0x20000000; HP cfront used a bias of 1; g++ 3.x
554 and other compilers which use the Itanium ABI use -1 as the NULL
555 value. GDB only supports that last form; to add support for
556 another form, make this into a cp-abi hook. */
560 fprintf_filtered (stream
, "NULL");
564 cp_find_class_member (&domain
, &fieldno
, val
<< 3);
569 fputs_filtered (prefix
, stream
);
570 name
= type_name_no_tag (domain
);
572 fputs_filtered (name
, stream
);
574 c_type_print_base (domain
, stream
, 0, 0);
575 fprintf_filtered (stream
, "::");
576 fputs_filtered (TYPE_FIELD_NAME (domain
, fieldno
), stream
);
579 fprintf_filtered (stream
, "%ld", (long) val
);
584 _initialize_cp_valprint (void)
586 add_setshow_boolean_cmd ("static-members", class_support
,
587 &static_field_print
, _("\
588 Set printing of C++ static members."), _("\
589 Show printing of C++ static members."), NULL
,
591 show_static_field_print
,
592 &setprintlist
, &showprintlist
);
593 /* Turn on printing of static fields. */
594 static_field_print
= 1;
596 add_setshow_boolean_cmd ("vtbl", class_support
, &vtblprint
, _("\
597 Set printing of C++ virtual function tables."), _("\
598 Show printing of C++ virtual function tables."), NULL
,
601 &setprintlist
, &showprintlist
);
603 add_setshow_boolean_cmd ("object", class_support
, &objectprint
, _("\
604 Set printing of object's derived type based on vtable info."), _("\
605 Show printing of object's derived type based on vtable info."), NULL
,
608 &setprintlist
, &showprintlist
);
610 /* Give people the defaults which they are used to. */
613 obstack_begin (&dont_print_vb_obstack
, 32 * sizeof (struct type
*));
614 obstack_specify_allocation (&dont_print_statmem_obstack
,
615 32 * sizeof (CORE_ADDR
), sizeof (CORE_ADDR
),