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, 2009, 2010
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"
39 #include "python/python.h"
41 /* 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
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"),
64 show_static_field_print (struct ui_file *file, int from_tty,
65 struct cmd_list_element *c, const char *value)
67 fprintf_filtered (file, _("Printing of C++ static members is %s.\n"),
72 static struct obstack dont_print_vb_obstack;
73 static struct obstack dont_print_statmem_obstack;
74 static struct obstack dont_print_stat_array_obstack;
76 extern void _initialize_cp_valprint (void);
78 static void cp_print_static_field (struct type *, struct value *,
79 struct ui_file *, int,
80 const struct value_print_options *);
82 static void cp_print_value (struct type *, struct type *, const gdb_byte *,
83 int, CORE_ADDR, struct ui_file *, int,
84 const struct value_print_options *, 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 OPTIONS 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 recurse,
154 const struct value_print_options *options,
155 struct type **dont_print_vb, int dont_print_statmem)
157 int i, len, n_baseclasses;
159 static int last_set_recurse = -1;
161 CHECK_TYPEDEF (type);
165 if (obstack_object_size (&dont_print_statmem_obstack) > 0)
166 obstack_free (&dont_print_statmem_obstack, NULL);
167 if (obstack_object_size (&dont_print_stat_array_obstack) > 0)
168 obstack_free (&dont_print_stat_array_obstack, NULL);
171 fprintf_filtered (stream, "{");
172 len = TYPE_NFIELDS (type);
173 n_baseclasses = TYPE_N_BASECLASSES (type);
175 /* First, print out baseclasses such that we don't print
176 duplicates of virtual baseclasses. */
178 if (n_baseclasses > 0)
179 cp_print_value (type, real_type, valaddr, offset, address, stream,
180 recurse + 1, options, dont_print_vb);
182 /* Second, print out data fields */
184 /* If there are no data fields, skip this part */
185 if (len == n_baseclasses || !len)
186 fprintf_filtered (stream, "<No data fields>");
189 int statmem_obstack_initial_size = 0;
190 int stat_array_obstack_initial_size = 0;
192 if (dont_print_statmem == 0)
194 statmem_obstack_initial_size =
195 obstack_object_size (&dont_print_statmem_obstack);
197 if (last_set_recurse != recurse)
199 stat_array_obstack_initial_size =
200 obstack_object_size (&dont_print_stat_array_obstack);
201 last_set_recurse = recurse;
205 for (i = n_baseclasses; i < len; i++)
207 /* If requested, skip printing of static fields. */
208 if (!options->static_field_print
209 && field_is_static (&TYPE_FIELD (type, i)))
213 fprintf_filtered (stream, ", ");
214 else if (n_baseclasses > 0)
218 fprintf_filtered (stream, "\n");
219 print_spaces_filtered (2 + 2 * recurse, stream);
220 fputs_filtered ("members of ", stream);
221 fputs_filtered (type_name_no_tag (type), stream);
222 fputs_filtered (": ", stream);
229 fprintf_filtered (stream, "\n");
230 print_spaces_filtered (2 + 2 * recurse, stream);
234 wrap_here (n_spaces (2 + 2 * recurse));
236 if (options->inspect_it)
238 if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
239 fputs_filtered ("\"( ptr \"", stream);
241 fputs_filtered ("\"( nodef \"", stream);
242 if (field_is_static (&TYPE_FIELD (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 fputs_filtered ("\" \"", stream);
248 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
249 current_language->la_language,
250 DMGL_PARAMS | DMGL_ANSI);
251 fputs_filtered ("\") \"", stream);
255 annotate_field_begin (TYPE_FIELD_TYPE (type, i));
257 if (field_is_static (&TYPE_FIELD (type, i)))
258 fputs_filtered ("static ", stream);
259 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
260 current_language->la_language,
261 DMGL_PARAMS | DMGL_ANSI);
262 annotate_field_name_end ();
263 /* do not print leading '=' in case of anonymous unions */
264 if (strcmp (TYPE_FIELD_NAME (type, i), ""))
265 fputs_filtered (" = ", stream);
266 annotate_field_value ();
269 if (!field_is_static (&TYPE_FIELD (type, i))
270 && TYPE_FIELD_PACKED (type, i))
274 /* Bitfields require special handling, especially due to byte
276 if (TYPE_FIELD_IGNORE (type, i))
278 fputs_filtered ("<optimized out or zero length>", stream);
282 struct value_print_options opts = *options;
284 v = value_from_longest
285 (TYPE_FIELD_TYPE (type, i),
286 unpack_field_as_long (type, valaddr + offset, i));
288 common_val_print (v, stream, recurse + 1, &opts,
294 if (TYPE_FIELD_IGNORE (type, i))
296 fputs_filtered ("<optimized out or zero length>", stream);
298 else if (field_is_static (&TYPE_FIELD (type, i)))
300 struct value *v = value_static_field (type, i);
302 fputs_filtered ("<optimized out>", stream);
304 cp_print_static_field (TYPE_FIELD_TYPE (type, i), v,
305 stream, recurse + 1, options);
309 struct value_print_options opts = *options;
311 val_print (TYPE_FIELD_TYPE (type, i),
312 valaddr, offset + TYPE_FIELD_BITPOS (type, i) / 8,
314 stream, recurse + 1, &opts,
318 annotate_field_end ();
321 if (dont_print_statmem == 0)
323 int obstack_final_size =
324 obstack_object_size (&dont_print_statmem_obstack);
326 if (obstack_final_size > statmem_obstack_initial_size) {
327 /* In effect, a pop of the printed-statics stack. */
330 obstack_next_free (&dont_print_statmem_obstack) -
331 (obstack_final_size - statmem_obstack_initial_size);
333 obstack_free (&dont_print_statmem_obstack,
337 if (last_set_recurse != recurse)
339 int obstack_final_size =
340 obstack_object_size (&dont_print_stat_array_obstack);
342 if (obstack_final_size > stat_array_obstack_initial_size)
345 obstack_next_free (&dont_print_stat_array_obstack) -
346 (obstack_final_size - stat_array_obstack_initial_size);
348 obstack_free (&dont_print_stat_array_obstack,
351 last_set_recurse = -1;
357 fprintf_filtered (stream, "\n");
358 print_spaces_filtered (2 * recurse, stream);
360 } /* if there are data fields */
362 fprintf_filtered (stream, "}");
365 /* Like cp_print_value_fields, but find the runtime type of the object
366 and pass it as the `real_type' argument to cp_print_value_fields.
367 This function is a hack to work around the fact that
368 common_val_print passes the embedded offset to val_print, but not
369 the enclosing type. */
372 cp_print_value_fields_rtti (struct type *type,
373 const gdb_byte *valaddr, int offset,
375 struct ui_file *stream, int recurse,
376 const struct value_print_options *options,
377 struct type **dont_print_vb, int dont_print_statmem)
380 int full, top, using_enc;
381 struct type *real_type;
383 /* Ugh, we have to convert back to a value here. */
384 value = value_from_contents_and_address (type, valaddr + offset,
386 /* We don't actually care about most of the result here -- just the
387 type. We already have the correct offset, due to how val_print
388 was initially called. */
389 real_type = value_rtti_type (value, &full, &top, &using_enc);
393 cp_print_value_fields (type, real_type, valaddr, offset,
394 address, stream, recurse, options,
395 dont_print_vb, dont_print_statmem);
398 /* Special val_print routine to avoid printing multiple copies of virtual
402 cp_print_value (struct type *type, struct type *real_type,
403 const gdb_byte *valaddr, int offset, CORE_ADDR address,
404 struct ui_file *stream, int recurse,
405 const struct value_print_options *options,
406 struct type **dont_print_vb)
408 struct type **last_dont_print
409 = (struct type **) obstack_next_free (&dont_print_vb_obstack);
410 struct obstack tmp_obstack = dont_print_vb_obstack;
411 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
413 struct type *thistype;
415 if (dont_print_vb == 0)
417 /* If we're at top level, carve out a completely fresh
418 chunk of the obstack and use that until this particular
419 invocation returns. */
420 /* Bump up the high-water mark. Now alpha is omega. */
421 obstack_finish (&dont_print_vb_obstack);
424 for (i = 0; i < n_baseclasses; i++)
428 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
429 char *basename = TYPE_NAME (baseclass);
430 const gdb_byte *base_valaddr;
432 if (BASETYPE_VIA_VIRTUAL (type, i))
434 struct type **first_dont_print
435 = (struct type **) obstack_base (&dont_print_vb_obstack);
437 int j = (struct type **) obstack_next_free (&dont_print_vb_obstack)
441 if (baseclass == first_dont_print[j])
444 obstack_ptr_grow (&dont_print_vb_obstack, baseclass);
448 thistype = real_type;
450 boffset = baseclass_offset (type, i, valaddr + offset, address + offset);
451 skip = ((boffset == -1) || (boffset + offset) < 0) ? 1 : -1;
453 if (BASETYPE_VIA_VIRTUAL (type, i))
455 /* The virtual base class pointer might have been
456 clobbered by the user program. Make sure that it
457 still points to a valid memory location. */
460 && ((boffset + offset) < 0
461 || (boffset + offset) >= TYPE_LENGTH (real_type)))
463 /* FIXME (alloca): unsafe if baseclass is really really large. */
464 gdb_byte *buf = alloca (TYPE_LENGTH (baseclass));
466 if (target_read_memory (address + boffset, buf,
467 TYPE_LENGTH (baseclass)) != 0)
469 address = address + boffset;
472 thistype = baseclass;
475 base_valaddr = valaddr;
478 base_valaddr = valaddr;
480 /* now do the printing */
483 fprintf_filtered (stream, "\n");
484 print_spaces_filtered (2 * recurse, stream);
486 fputs_filtered ("<", stream);
487 /* Not sure what the best notation is in the case where there is no
489 fputs_filtered (basename ? basename : "", stream);
490 fputs_filtered ("> = ", stream);
494 fprintf_filtered (stream, "<invalid address>");
499 /* Attempt to run the Python pretty-printers on the
500 baseclass if possible. */
502 result = apply_val_pretty_printer (baseclass, base_valaddr,
503 thisoffset + boffset,
510 cp_print_value_fields (baseclass, thistype, base_valaddr,
511 thisoffset + boffset, address,
512 stream, recurse, options,
514 obstack_base (&dont_print_vb_obstack)),
517 fputs_filtered (", ", stream);
523 if (dont_print_vb == 0)
525 /* Free the space used to deal with the printing
526 of this type from top level. */
527 obstack_free (&dont_print_vb_obstack, last_dont_print);
528 /* Reset watermark so that we can continue protecting
529 ourselves from whatever we were protecting ourselves. */
530 dont_print_vb_obstack = tmp_obstack;
534 /* Print value of a static member.
535 To avoid infinite recursion when printing a class that contains
536 a static instance of the class, we keep the addresses of all printed
537 static member classes in an obstack and refuse to print them more
540 VAL contains the value to print, TYPE, STREAM, RECURSE, and OPTIONS
541 have the same meanings as in c_val_print. */
544 cp_print_static_field (struct type *type,
546 struct ui_file *stream,
548 const struct value_print_options *options)
550 struct value_print_options opts;
552 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
554 CORE_ADDR *first_dont_print;
559 = (CORE_ADDR *) obstack_base (&dont_print_statmem_obstack);
560 i = obstack_object_size (&dont_print_statmem_obstack)
561 / sizeof (CORE_ADDR);
565 if (value_address (val) == first_dont_print[i])
567 fputs_filtered ("<same as static member of an already"
574 addr = value_address (val);
575 obstack_grow (&dont_print_statmem_obstack, (char *) &addr,
577 CHECK_TYPEDEF (type);
578 cp_print_value_fields (type, value_enclosing_type (val),
579 value_contents_all (val),
580 value_embedded_offset (val), addr,
581 stream, recurse, options, NULL, 1);
585 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
587 struct type **first_dont_print;
589 struct type *target_type = TYPE_TARGET_TYPE (type);
592 = (struct type **) obstack_base (&dont_print_stat_array_obstack);
593 i = obstack_object_size (&dont_print_stat_array_obstack)
594 / sizeof (CORE_ADDR);
598 if (target_type == first_dont_print[i])
600 fputs_filtered ("<same as static member of an already"
607 obstack_grow (&dont_print_stat_array_obstack, (char *) &target_type,
608 sizeof (struct type *));
613 val_print (type, value_contents_all (val),
614 value_embedded_offset (val), value_address (val),
615 stream, recurse, &opts, current_language);
619 /* Find the field in *DOMAIN, or its non-virtual base classes, with bit offset
620 OFFSET. Set *DOMAIN to the containing type and *FIELDNO to the containing
621 field number. If OFFSET is not exactly at the start of some field, set
625 cp_find_class_member (struct type **domain_p, int *fieldno,
632 *domain_p = check_typedef (*domain_p);
634 len = TYPE_NFIELDS (domain);
636 for (i = TYPE_N_BASECLASSES (domain); i < len; i++)
638 LONGEST bitpos = TYPE_FIELD_BITPOS (domain, i);
641 if (offset == bitpos)
648 for (i = 0; i < TYPE_N_BASECLASSES (domain); i++)
650 LONGEST bitpos = TYPE_FIELD_BITPOS (domain, i);
651 LONGEST bitsize = 8 * TYPE_LENGTH (TYPE_FIELD_TYPE (domain, i));
653 if (offset >= bitpos && offset < bitpos + bitsize)
655 *domain_p = TYPE_FIELD_TYPE (domain, i);
656 cp_find_class_member (domain_p, fieldno, offset - bitpos);
665 cp_print_class_member (const gdb_byte *valaddr, struct type *type,
666 struct ui_file *stream, char *prefix)
668 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
670 /* VAL is a byte offset into the structure type DOMAIN.
671 Find the name of the field for that offset and
673 struct type *domain = TYPE_DOMAIN_TYPE (type);
675 unsigned int fieldno;
677 val = extract_signed_integer (valaddr, TYPE_LENGTH (type), byte_order);
679 /* Pointers to data members are usually byte offsets into an object.
680 Because a data member can have offset zero, and a NULL pointer to
681 member must be distinct from any valid non-NULL pointer to
682 member, either the value is biased or the NULL value has a
683 special representation; both are permitted by ISO C++. HP aCC
684 used a bias of 0x20000000; HP cfront used a bias of 1; g++ 3.x
685 and other compilers which use the Itanium ABI use -1 as the NULL
686 value. GDB only supports that last form; to add support for
687 another form, make this into a cp-abi hook. */
691 fprintf_filtered (stream, "NULL");
695 cp_find_class_member (&domain, &fieldno, val << 3);
700 fputs_filtered (prefix, stream);
701 name = type_name_no_tag (domain);
703 fputs_filtered (name, stream);
705 c_type_print_base (domain, stream, 0, 0);
706 fprintf_filtered (stream, "::");
707 fputs_filtered (TYPE_FIELD_NAME (domain, fieldno), stream);
710 fprintf_filtered (stream, "%ld", (long) val);
715 _initialize_cp_valprint (void)
717 add_setshow_boolean_cmd ("static-members", class_support,
718 &user_print_options.static_field_print, _("\
719 Set printing of C++ static members."), _("\
720 Show printing of C++ static members."), NULL,
722 show_static_field_print,
723 &setprintlist, &showprintlist);
725 add_setshow_boolean_cmd ("vtbl", class_support,
726 &user_print_options.vtblprint, _("\
727 Set printing of C++ virtual function tables."), _("\
728 Show printing of C++ virtual function tables."), NULL,
731 &setprintlist, &showprintlist);
733 add_setshow_boolean_cmd ("object", class_support,
734 &user_print_options.objectprint, _("\
735 Set printing of object's derived type based on vtable info."), _("\
736 Show printing of object's derived type based on vtable info."), NULL,
739 &setprintlist, &showprintlist);
741 obstack_begin (&dont_print_stat_array_obstack, 32 * sizeof (CORE_ADDR));
742 obstack_begin (&dont_print_statmem_obstack, 32 * sizeof (CORE_ADDR));
743 obstack_begin (&dont_print_vb_obstack, 32 * sizeof (struct type *));