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, 2008
5 Free Software Foundation, Inc.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "gdb_obstack.h"
26 #include "expression.h"
32 #include "gdb_string.h"
37 #include "cp-support.h"
40 /* Controls printing of vtbl's */
43 show_vtblprint (struct ui_file
*file
, int from_tty
,
44 struct cmd_list_element
*c
, const char *value
)
46 fprintf_filtered (file
, _("\
47 Printing of C++ virtual function tables is %s.\n"),
51 /* Controls looking up an object's derived type using what we find in
55 show_objectprint (struct ui_file
*file
, int from_tty
,
56 struct cmd_list_element
*c
,
59 fprintf_filtered (file
, _("\
60 Printing of object's derived type based on vtable info is %s.\n"),
64 int static_field_print
; /* Controls printing of static fields. */
66 show_static_field_print (struct ui_file
*file
, int from_tty
,
67 struct cmd_list_element
*c
, const char *value
)
69 fprintf_filtered (file
, _("Printing of C++ static members is %s.\n"),
74 static struct obstack dont_print_vb_obstack
;
75 static struct obstack dont_print_statmem_obstack
;
77 extern void _initialize_cp_valprint (void);
79 static void cp_print_static_field (struct type
*, struct value
*,
80 struct ui_file
*, int, int,
81 enum val_prettyprint
);
83 static void cp_print_value (struct type
*, struct type
*, const gdb_byte
*,
84 int, CORE_ADDR
, struct ui_file
*, int, int,
85 enum val_prettyprint
, struct type
**);
88 /* GCC versions after 2.4.5 use this. */
89 const char vtbl_ptr_name
[] = "__vtbl_ptr_type";
91 /* Return truth value for assertion that TYPE is of the type
92 "pointer to virtual function". */
95 cp_is_vtbl_ptr_type (struct type
*type
)
97 char *typename
= type_name_no_tag (type
);
99 return (typename
!= NULL
&& !strcmp (typename
, vtbl_ptr_name
));
102 /* Return truth value for the assertion that TYPE is of the type
103 "pointer to virtual function table". */
106 cp_is_vtbl_member (struct type
*type
)
108 /* With older versions of g++, the vtbl field pointed to an array
109 of structures. Nowadays it points directly to the structure. */
110 if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
112 type
= TYPE_TARGET_TYPE (type
);
113 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
)
115 type
= TYPE_TARGET_TYPE (type
);
116 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
/* if not using thunks */
117 || TYPE_CODE (type
) == TYPE_CODE_PTR
) /* if using thunks */
119 /* Virtual functions tables are full of pointers
120 to virtual functions. */
121 return cp_is_vtbl_ptr_type (type
);
124 else if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
) /* if not using thunks */
126 return cp_is_vtbl_ptr_type (type
);
128 else if (TYPE_CODE (type
) == TYPE_CODE_PTR
) /* if using thunks */
130 /* The type name of the thunk pointer is NULL when using dwarf2.
131 We could test for a pointer to a function, but there is
132 no type info for the virtual table either, so it wont help. */
133 return cp_is_vtbl_ptr_type (type
);
139 /* Mutually recursive subroutines of cp_print_value and c_val_print to
140 print out a structure's fields: cp_print_value_fields and cp_print_value.
142 TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and PRETTY have the
143 same meanings as in cp_print_value and c_val_print.
145 2nd argument REAL_TYPE is used to carry over the type of the derived
146 class across the recursion to base classes.
148 DONT_PRINT is an array of baseclass types that we
149 should not print, or zero if called from top level. */
152 cp_print_value_fields (struct type
*type
, struct type
*real_type
,
153 const gdb_byte
*valaddr
, int offset
, CORE_ADDR address
,
154 struct ui_file
*stream
, int format
, int recurse
,
155 enum val_prettyprint pretty
,
156 struct type
**dont_print_vb
,int dont_print_statmem
)
158 int i
, len
, n_baseclasses
;
159 char *last_dont_print
= obstack_next_free (&dont_print_statmem_obstack
);
162 CHECK_TYPEDEF (type
);
164 fprintf_filtered (stream
, "{");
165 len
= TYPE_NFIELDS (type
);
166 n_baseclasses
= TYPE_N_BASECLASSES (type
);
168 /* First, print out baseclasses such that we don't print
169 duplicates of virtual baseclasses. */
171 if (n_baseclasses
> 0)
172 cp_print_value (type
, real_type
, valaddr
, offset
, address
, stream
,
173 format
, recurse
+ 1, pretty
, dont_print_vb
);
175 /* Second, print out data fields */
177 /* If there are no data fields, skip this part */
178 if (len
== n_baseclasses
|| !len
)
179 fprintf_filtered (stream
, "<No data fields>");
182 struct obstack tmp_obstack
= dont_print_statmem_obstack
;
184 if (dont_print_statmem
== 0)
186 /* If we're at top level, carve out a completely fresh
187 chunk of the obstack and use that until this particular
188 invocation returns. */
189 obstack_finish (&dont_print_statmem_obstack
);
192 for (i
= n_baseclasses
; i
< len
; i
++)
194 /* If requested, skip printing of static fields. */
195 if (!static_field_print
196 && field_is_static (&TYPE_FIELD (type
, i
)))
200 fprintf_filtered (stream
, ", ");
201 else if (n_baseclasses
> 0)
205 fprintf_filtered (stream
, "\n");
206 print_spaces_filtered (2 + 2 * recurse
, stream
);
207 fputs_filtered ("members of ", stream
);
208 fputs_filtered (type_name_no_tag (type
), stream
);
209 fputs_filtered (": ", stream
);
216 fprintf_filtered (stream
, "\n");
217 print_spaces_filtered (2 + 2 * recurse
, stream
);
221 wrap_here (n_spaces (2 + 2 * recurse
));
225 if (TYPE_CODE (TYPE_FIELD_TYPE (type
, i
)) == TYPE_CODE_PTR
)
226 fputs_filtered ("\"( ptr \"", stream
);
228 fputs_filtered ("\"( nodef \"", stream
);
229 if (field_is_static (&TYPE_FIELD (type
, i
)))
230 fputs_filtered ("static ", stream
);
231 fprintf_symbol_filtered (stream
, TYPE_FIELD_NAME (type
, i
),
232 current_language
->la_language
,
233 DMGL_PARAMS
| DMGL_ANSI
);
234 fputs_filtered ("\" \"", stream
);
235 fprintf_symbol_filtered (stream
, TYPE_FIELD_NAME (type
, i
),
236 current_language
->la_language
,
237 DMGL_PARAMS
| DMGL_ANSI
);
238 fputs_filtered ("\") \"", stream
);
242 annotate_field_begin (TYPE_FIELD_TYPE (type
, i
));
244 if (field_is_static (&TYPE_FIELD (type
, i
)))
245 fputs_filtered ("static ", stream
);
246 fprintf_symbol_filtered (stream
, TYPE_FIELD_NAME (type
, i
),
247 current_language
->la_language
,
248 DMGL_PARAMS
| DMGL_ANSI
);
249 annotate_field_name_end ();
250 /* do not print leading '=' in case of anonymous unions */
251 if (strcmp (TYPE_FIELD_NAME (type
, i
), ""))
252 fputs_filtered (" = ", stream
);
253 annotate_field_value ();
256 if (!field_is_static (&TYPE_FIELD (type
, i
))
257 && TYPE_FIELD_PACKED (type
, i
))
261 /* Bitfields require special handling, especially due to byte
263 if (TYPE_FIELD_IGNORE (type
, i
))
265 fputs_filtered ("<optimized out or zero length>", stream
);
269 v
= value_from_longest
270 (TYPE_FIELD_TYPE (type
, i
),
271 unpack_field_as_long (type
, valaddr
+ offset
, i
));
273 common_val_print (v
, stream
, format
, 0, recurse
+ 1, pretty
,
279 if (TYPE_FIELD_IGNORE (type
, i
))
281 fputs_filtered ("<optimized out or zero length>", stream
);
283 else if (field_is_static (&TYPE_FIELD (type
, i
)))
285 struct value
*v
= value_static_field (type
, i
);
287 fputs_filtered ("<optimized out>", stream
);
289 cp_print_static_field (TYPE_FIELD_TYPE (type
, i
), v
,
290 stream
, format
, recurse
+ 1,
295 val_print (TYPE_FIELD_TYPE (type
, i
),
296 valaddr
, offset
+ TYPE_FIELD_BITPOS (type
, i
) / 8,
297 address
+ TYPE_FIELD_BITPOS (type
, i
) / 8,
298 stream
, format
, 0, recurse
+ 1, pretty
,
302 annotate_field_end ();
305 if (dont_print_statmem
== 0)
307 /* Free the space used to deal with the printing
308 of the members from top level. */
309 obstack_free (&dont_print_statmem_obstack
, last_dont_print
);
310 dont_print_statmem_obstack
= tmp_obstack
;
315 fprintf_filtered (stream
, "\n");
316 print_spaces_filtered (2 * recurse
, stream
);
318 } /* if there are data fields */
320 fprintf_filtered (stream
, "}");
323 /* Special val_print routine to avoid printing multiple copies of virtual
327 cp_print_value (struct type
*type
, struct type
*real_type
,
328 const gdb_byte
*valaddr
, int offset
, CORE_ADDR address
,
329 struct ui_file
*stream
, int format
, int recurse
,
330 enum val_prettyprint pretty
, struct type
**dont_print_vb
)
332 struct type
**last_dont_print
333 = (struct type
**) obstack_next_free (&dont_print_vb_obstack
);
334 struct obstack tmp_obstack
= dont_print_vb_obstack
;
335 int i
, n_baseclasses
= TYPE_N_BASECLASSES (type
);
337 struct type
*thistype
;
339 if (dont_print_vb
== 0)
341 /* If we're at top level, carve out a completely fresh
342 chunk of the obstack and use that until this particular
343 invocation returns. */
344 /* Bump up the high-water mark. Now alpha is omega. */
345 obstack_finish (&dont_print_vb_obstack
);
348 for (i
= 0; i
< n_baseclasses
; i
++)
352 struct type
*baseclass
= check_typedef (TYPE_BASECLASS (type
, i
));
353 char *basename
= TYPE_NAME (baseclass
);
354 const gdb_byte
*base_valaddr
;
356 if (BASETYPE_VIA_VIRTUAL (type
, i
))
358 struct type
**first_dont_print
359 = (struct type
**) obstack_base (&dont_print_vb_obstack
);
361 int j
= (struct type
**) obstack_next_free (&dont_print_vb_obstack
)
365 if (baseclass
== first_dont_print
[j
])
368 obstack_ptr_grow (&dont_print_vb_obstack
, baseclass
);
372 thistype
= real_type
;
374 boffset
= baseclass_offset (type
, i
, valaddr
+ offset
, address
);
375 skip
= ((boffset
== -1) || (boffset
+ offset
) < 0) ? 1 : -1;
377 if (BASETYPE_VIA_VIRTUAL (type
, i
))
379 /* The virtual base class pointer might have been
380 clobbered by the user program. Make sure that it
381 still points to a valid memory location. */
384 && ((boffset
+ offset
) < 0
385 || (boffset
+ offset
) >= TYPE_LENGTH (type
)))
387 /* FIXME (alloca): unsafe if baseclass is really really large. */
388 gdb_byte
*buf
= alloca (TYPE_LENGTH (baseclass
));
390 if (target_read_memory (address
+ boffset
, buf
,
391 TYPE_LENGTH (baseclass
)) != 0)
393 address
= address
+ boffset
;
396 thistype
= baseclass
;
399 base_valaddr
= valaddr
;
402 base_valaddr
= valaddr
;
404 /* now do the printing */
407 fprintf_filtered (stream
, "\n");
408 print_spaces_filtered (2 * recurse
, stream
);
410 fputs_filtered ("<", stream
);
411 /* Not sure what the best notation is in the case where there is no
413 fputs_filtered (basename
? basename
: "", stream
);
414 fputs_filtered ("> = ", stream
);
418 fprintf_filtered (stream
, "<invalid address>");
420 cp_print_value_fields (baseclass
, thistype
, base_valaddr
,
421 thisoffset
+ boffset
, address
+ boffset
,
425 obstack_base (&dont_print_vb_obstack
)),
427 fputs_filtered (", ", stream
);
433 if (dont_print_vb
== 0)
435 /* Free the space used to deal with the printing
436 of this type from top level. */
437 obstack_free (&dont_print_vb_obstack
, last_dont_print
);
438 /* Reset watermark so that we can continue protecting
439 ourselves from whatever we were protecting ourselves. */
440 dont_print_vb_obstack
= tmp_obstack
;
444 /* Print value of a static member.
445 To avoid infinite recursion when printing a class that contains
446 a static instance of the class, we keep the addresses of all printed
447 static member classes in an obstack and refuse to print them more
450 VAL contains the value to print, TYPE, STREAM, RECURSE, and PRETTY
451 have the same meanings as in c_val_print. */
454 cp_print_static_field (struct type
*type
,
456 struct ui_file
*stream
,
459 enum val_prettyprint pretty
)
461 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
463 CORE_ADDR
*first_dont_print
;
467 = (CORE_ADDR
*) obstack_base (&dont_print_statmem_obstack
);
468 i
= (CORE_ADDR
*) obstack_next_free (&dont_print_statmem_obstack
)
473 if (VALUE_ADDRESS (val
) == first_dont_print
[i
])
475 fputs_filtered ("<same as static member of an already"
482 obstack_grow (&dont_print_statmem_obstack
, (char *) &VALUE_ADDRESS (val
),
485 CHECK_TYPEDEF (type
);
486 cp_print_value_fields (type
, type
, value_contents_all (val
),
487 value_embedded_offset (val
), VALUE_ADDRESS (val
),
488 stream
, format
, recurse
, pretty
, NULL
, 1);
491 val_print (type
, value_contents_all (val
),
492 value_embedded_offset (val
), VALUE_ADDRESS (val
),
493 stream
, format
, 0, recurse
, pretty
, current_language
);
497 /* Find the field in *DOMAIN, or its non-virtual base classes, with bit offset
498 OFFSET. Set *DOMAIN to the containing type and *FIELDNO to the containing
499 field number. If OFFSET is not exactly at the start of some field, set
503 cp_find_class_member (struct type
**domain_p
, int *fieldno
,
510 *domain_p
= check_typedef (*domain_p
);
512 len
= TYPE_NFIELDS (domain
);
514 for (i
= TYPE_N_BASECLASSES (domain
); i
< len
; i
++)
516 LONGEST bitpos
= TYPE_FIELD_BITPOS (domain
, i
);
519 if (offset
== bitpos
)
526 for (i
= 0; i
< TYPE_N_BASECLASSES (domain
); i
++)
528 LONGEST bitpos
= TYPE_FIELD_BITPOS (domain
, i
);
529 LONGEST bitsize
= 8 * TYPE_LENGTH (TYPE_FIELD_TYPE (domain
, i
));
531 if (offset
>= bitpos
&& offset
< bitpos
+ bitsize
)
533 *domain_p
= TYPE_FIELD_TYPE (domain
, i
);
534 cp_find_class_member (domain_p
, fieldno
, offset
- bitpos
);
543 cp_print_class_member (const gdb_byte
*valaddr
, struct type
*type
,
544 struct ui_file
*stream
, char *prefix
)
546 /* VAL is a byte offset into the structure type DOMAIN.
547 Find the name of the field for that offset and
549 struct type
*domain
= TYPE_DOMAIN_TYPE (type
);
550 LONGEST val
= extract_signed_integer (valaddr
, TYPE_LENGTH (type
));
551 unsigned int fieldno
;
553 /* Pointers to data members are usually byte offsets into an object.
554 Because a data member can have offset zero, and a NULL pointer to
555 member must be distinct from any valid non-NULL pointer to
556 member, either the value is biased or the NULL value has a
557 special representation; both are permitted by ISO C++. HP aCC
558 used a bias of 0x20000000; HP cfront used a bias of 1; g++ 3.x
559 and other compilers which use the Itanium ABI use -1 as the NULL
560 value. GDB only supports that last form; to add support for
561 another form, make this into a cp-abi hook. */
565 fprintf_filtered (stream
, "NULL");
569 cp_find_class_member (&domain
, &fieldno
, val
<< 3);
574 fputs_filtered (prefix
, stream
);
575 name
= type_name_no_tag (domain
);
577 fputs_filtered (name
, stream
);
579 c_type_print_base (domain
, stream
, 0, 0);
580 fprintf_filtered (stream
, "::");
581 fputs_filtered (TYPE_FIELD_NAME (domain
, fieldno
), stream
);
584 fprintf_filtered (stream
, "%ld", (long) val
);
589 _initialize_cp_valprint (void)
591 add_setshow_boolean_cmd ("static-members", class_support
,
592 &static_field_print
, _("\
593 Set printing of C++ static members."), _("\
594 Show printing of C++ static members."), NULL
,
596 show_static_field_print
,
597 &setprintlist
, &showprintlist
);
598 /* Turn on printing of static fields. */
599 static_field_print
= 1;
601 add_setshow_boolean_cmd ("vtbl", class_support
, &vtblprint
, _("\
602 Set printing of C++ virtual function tables."), _("\
603 Show printing of C++ virtual function tables."), NULL
,
606 &setprintlist
, &showprintlist
);
608 add_setshow_boolean_cmd ("object", class_support
, &objectprint
, _("\
609 Set printing of object's derived type based on vtable info."), _("\
610 Show printing of object's derived type based on vtable info."), NULL
,
613 &setprintlist
, &showprintlist
);
615 /* Give people the defaults which they are used to. */
618 obstack_begin (&dont_print_vb_obstack
, 32 * sizeof (struct type
*));
619 obstack_specify_allocation (&dont_print_statmem_obstack
,
620 32 * sizeof (CORE_ADDR
), sizeof (CORE_ADDR
),