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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 #include "expression.h"
31 #include "gdb_string.h"
33 int vtblprint; /* Controls printing of vtbl's */
34 int objectprint; /* Controls looking up an object's derived type
35 using what we find in its vtables. */
36 static int static_field_print; /* Controls printing of static fields. */
38 static struct obstack dont_print_vb_obstack;
39 static struct obstack dont_print_statmem_obstack;
42 cp_print_static_field PARAMS ((struct type *, value_ptr, GDB_FILE *, int, int,
43 enum val_prettyprint));
46 cplus_print_value PARAMS ((struct type *, char *, GDB_FILE *, int, int,
47 enum val_prettyprint, struct type **));
49 /* BEGIN-FIXME: Hooks into typeprint.c, find a better home for prototypes. */
52 c_type_print_base PARAMS ((struct type *, GDB_FILE *, int, int));
55 c_type_print_varspec_prefix PARAMS ((struct type *, GDB_FILE *, int, int));
58 cp_type_print_method_args PARAMS ((struct type **, char *, char *, int,
64 cp_print_class_method (valaddr, type, stream)
70 struct fn_field *f = NULL;
80 check_stub_type (TYPE_TARGET_TYPE (type));
81 domain = TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (type));
82 if (domain == (struct type *)NULL)
84 fprintf_filtered (stream, "<unknown>");
87 addr = unpack_pointer (lookup_pointer_type (builtin_type_void), valaddr);
88 if (METHOD_PTR_IS_VIRTUAL (addr))
90 offset = METHOD_PTR_TO_VOFFSET (addr);
91 len = TYPE_NFN_FIELDS (domain);
92 for (i = 0; i < len; i++)
94 f = TYPE_FN_FIELDLIST1 (domain, i);
95 len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
97 for (j = 0; j < len2; j++)
100 if (TYPE_FN_FIELD_VOFFSET (f, j) == offset)
110 sym = find_pc_function (addr);
113 error ("invalid pointer to member function");
115 len = TYPE_NFN_FIELDS (domain);
116 for (i = 0; i < len; i++)
118 f = TYPE_FN_FIELDLIST1 (domain, i);
119 len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
121 for (j = 0; j < len2; j++)
124 if (TYPE_FN_FIELD_STUB (f, j))
125 check_stub_method (domain, i, j);
126 if (STREQ (SYMBOL_NAME (sym), TYPE_FN_FIELD_PHYSNAME (f, j)))
136 fprintf_filtered (stream, "&");
137 c_type_print_varspec_prefix (TYPE_FN_FIELD_TYPE (f, j), stream, 0, 0);
138 fprintf_unfiltered (stream, kind);
139 if (TYPE_FN_FIELD_PHYSNAME (f, j)[0] == '_'
140 && TYPE_FN_FIELD_PHYSNAME (f, j)[1] == CPLUS_MARKER)
142 cp_type_print_method_args (TYPE_FN_FIELD_ARGS (f, j) + 1, "~",
143 TYPE_FN_FIELDLIST_NAME (domain, i),
148 cp_type_print_method_args (TYPE_FN_FIELD_ARGS (f, j), "",
149 TYPE_FN_FIELDLIST_NAME (domain, i),
155 fprintf_filtered (stream, "(");
156 type_print (type, "", stream, -1);
157 fprintf_filtered (stream, ") %d", (int) addr >> 3);
161 /* This was what it was for gcc 2.4.5 and earlier. */
162 static const char vtbl_ptr_name_old[] =
163 { CPLUS_MARKER,'v','t','b','l','_','p','t','r','_','t','y','p','e', 0 };
164 /* It was changed to this after 2.4.5. */
165 const char vtbl_ptr_name[] =
166 { '_','_','v','t','b','l','_','p','t','r','_','t','y','p','e', 0 };
168 /* Return truth value for assertion that TYPE is of the type
169 "pointer to virtual function". */
172 cp_is_vtbl_ptr_type(type)
175 char *typename = type_name_no_tag (type);
177 return (typename != NULL
178 && (STREQ (typename, vtbl_ptr_name)
179 || STREQ (typename, vtbl_ptr_name_old)));
182 /* Return truth value for the assertion that TYPE is of the type
183 "pointer to virtual function table". */
186 cp_is_vtbl_member(type)
189 if (TYPE_CODE (type) == TYPE_CODE_PTR)
191 type = TYPE_TARGET_TYPE (type);
192 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
194 type = TYPE_TARGET_TYPE (type);
195 if (TYPE_CODE (type) == TYPE_CODE_STRUCT /* if not using thunks */
196 || TYPE_CODE (type) == TYPE_CODE_PTR) /* if using thunks */
198 /* Virtual functions tables are full of pointers
199 to virtual functions. */
200 return cp_is_vtbl_ptr_type (type);
207 /* Mutually recursive subroutines of cplus_print_value and c_val_print to
208 print out a structure's fields: cp_print_value_fields and cplus_print_value.
210 TYPE, VALADDR, STREAM, RECURSE, and PRETTY have the
211 same meanings as in cplus_print_value and c_val_print.
213 DONT_PRINT is an array of baseclass types that we
214 should not print, or zero if called from top level. */
217 cp_print_value_fields (type, valaddr, stream, format, recurse, pretty,
218 dont_print_vb, dont_print_statmem)
224 enum val_prettyprint pretty;
225 struct type **dont_print_vb;
226 int dont_print_statmem;
228 int i, len, n_baseclasses;
229 struct obstack tmp_obstack;
230 char *last_dont_print = obstack_next_free (&dont_print_statmem_obstack);
232 check_stub_type (type);
234 fprintf_filtered (stream, "{");
235 len = TYPE_NFIELDS (type);
236 n_baseclasses = TYPE_N_BASECLASSES (type);
238 /* Print out baseclasses such that we don't print
239 duplicates of virtual baseclasses. */
240 if (n_baseclasses > 0)
241 cplus_print_value (type, valaddr, stream, format, recurse+1, pretty,
244 if (!len && n_baseclasses == 1)
245 fprintf_filtered (stream, "<No data fields>");
248 extern int inspect_it;
251 if (dont_print_statmem == 0)
253 /* If we're at top level, carve out a completely fresh
254 chunk of the obstack and use that until this particular
255 invocation returns. */
256 tmp_obstack = dont_print_statmem_obstack;
257 obstack_finish (&dont_print_statmem_obstack);
260 for (i = n_baseclasses; i < len; i++)
262 /* If requested, skip printing of static fields. */
263 if (!static_field_print && TYPE_FIELD_STATIC (type, i))
266 fprintf_filtered (stream, ", ");
267 else if (n_baseclasses > 0)
271 fprintf_filtered (stream, "\n");
272 print_spaces_filtered (2 + 2 * recurse, stream);
273 fputs_filtered ("members of ", stream);
274 fputs_filtered (type_name_no_tag (type), stream);
275 fputs_filtered (": ", stream);
282 fprintf_filtered (stream, "\n");
283 print_spaces_filtered (2 + 2 * recurse, stream);
287 wrap_here (n_spaces (2 + 2 * recurse));
291 if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
292 fputs_filtered ("\"( ptr \"", stream);
294 fputs_filtered ("\"( nodef \"", stream);
295 if (TYPE_FIELD_STATIC (type, i))
296 fputs_filtered ("static ", stream);
297 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
299 DMGL_PARAMS | DMGL_ANSI);
300 fputs_filtered ("\" \"", stream);
301 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
303 DMGL_PARAMS | DMGL_ANSI);
304 fputs_filtered ("\") \"", stream);
308 annotate_field_begin (TYPE_FIELD_TYPE (type, i));
310 if (TYPE_FIELD_STATIC (type, i))
311 fputs_filtered ("static ", stream);
312 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
314 DMGL_PARAMS | DMGL_ANSI);
315 annotate_field_name_end ();
316 fputs_filtered (" = ", stream);
317 annotate_field_value ();
320 if (!TYPE_FIELD_STATIC (type, i) && TYPE_FIELD_PACKED (type, i))
324 /* Bitfields require special handling, especially due to byte
326 if (TYPE_FIELD_IGNORE (type, i))
328 fputs_filtered ("<optimized out or zero length>", stream);
332 v = value_from_longest (TYPE_FIELD_TYPE (type, i),
333 unpack_field_as_long (type, valaddr, i));
335 val_print (TYPE_FIELD_TYPE(type, i), VALUE_CONTENTS (v), 0,
336 stream, format, 0, recurse + 1, pretty);
341 if (TYPE_FIELD_IGNORE (type, i))
343 fputs_filtered ("<optimized out or zero length>", stream);
345 else if (TYPE_FIELD_STATIC (type, i))
348 char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (type, i);
350 lookup_symbol (phys_name, 0, VAR_NAMESPACE, 0, NULL);
352 fputs_filtered ("<optimized out>", stream);
355 v = value_at (TYPE_FIELD_TYPE (type, i),
356 (CORE_ADDR)SYMBOL_BLOCK_VALUE (sym));
357 cp_print_static_field (TYPE_FIELD_TYPE (type, i), v,
358 stream, format, recurse + 1,
364 val_print (TYPE_FIELD_TYPE (type, i),
365 valaddr + TYPE_FIELD_BITPOS (type, i) / 8,
366 0, stream, format, 0, recurse + 1, pretty);
369 annotate_field_end ();
372 if (dont_print_statmem == 0)
374 /* Free the space used to deal with the printing
375 of the members from top level. */
376 obstack_free (&dont_print_statmem_obstack, last_dont_print);
377 dont_print_statmem_obstack = tmp_obstack;
382 fprintf_filtered (stream, "\n");
383 print_spaces_filtered (2 * recurse, stream);
386 fprintf_filtered (stream, "}");
389 /* Special val_print routine to avoid printing multiple copies of virtual
393 cplus_print_value (type, valaddr, stream, format, recurse, pretty,
400 enum val_prettyprint pretty;
401 struct type **dont_print_vb;
403 struct obstack tmp_obstack;
404 struct type **last_dont_print
405 = (struct type **)obstack_next_free (&dont_print_vb_obstack);
406 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
408 if (dont_print_vb == 0)
410 /* If we're at top level, carve out a completely fresh
411 chunk of the obstack and use that until this particular
412 invocation returns. */
413 tmp_obstack = dont_print_vb_obstack;
414 /* Bump up the high-water mark. Now alpha is omega. */
415 obstack_finish (&dont_print_vb_obstack);
418 for (i = 0; i < n_baseclasses; i++)
420 /* FIXME-32x64--assumes that a target pointer can fit in a char *.
421 Fix it by nuking baseclass_addr. */
426 check_stub_type (TYPE_BASECLASS (type, i));
427 basename = TYPE_NAME (TYPE_BASECLASS (type, i));
429 if (BASETYPE_VIA_VIRTUAL (type, i))
431 struct type **first_dont_print
432 = (struct type **)obstack_base (&dont_print_vb_obstack);
434 int j = (struct type **)obstack_next_free (&dont_print_vb_obstack)
438 if (TYPE_BASECLASS (type, i) == first_dont_print[j])
441 obstack_ptr_grow (&dont_print_vb_obstack, TYPE_BASECLASS (type, i));
444 /* Fix to use baseclass_offset instead. FIXME */
445 baddr = baseclass_addr (type, i, valaddr, 0, &err);
446 if (err == 0 && baddr == 0)
447 error ("could not find virtual baseclass %s\n",
448 basename ? basename : "");
452 fprintf_filtered (stream, "\n");
453 print_spaces_filtered (2 * recurse, stream);
455 fputs_filtered ("<", stream);
456 /* Not sure what the best notation is in the case where there is no
458 fputs_filtered (basename ? basename : "", stream);
459 fputs_filtered ("> = ", stream);
462 fprintf_filtered (stream, "<invalid address ");
463 print_address_numeric ((CORE_ADDR) baddr, 1, stream);
464 fprintf_filtered (stream, ">");
467 cp_print_value_fields (TYPE_BASECLASS (type, i), baddr, stream, format,
469 (struct type **) obstack_base (&dont_print_vb_obstack),
471 fputs_filtered (", ", stream);
477 if (dont_print_vb == 0)
479 /* Free the space used to deal with the printing
480 of this type from top level. */
481 obstack_free (&dont_print_vb_obstack, last_dont_print);
482 /* Reset watermark so that we can continue protecting
483 ourselves from whatever we were protecting ourselves. */
484 dont_print_vb_obstack = tmp_obstack;
488 /* Print value of a static member.
489 To avoid infinite recursion when printing a class that contains
490 a static instance of the class, we keep the addresses of all printed
491 static member classes in an obstack and refuse to print them more
494 VAL contains the value to print, TYPE, STREAM, RECURSE, and PRETTY
495 have the same meanings as in c_val_print. */
498 cp_print_static_field (type, val, stream, format, recurse, pretty)
504 enum val_prettyprint pretty;
506 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
508 CORE_ADDR *first_dont_print;
512 = (CORE_ADDR *)obstack_base (&dont_print_statmem_obstack);
513 i = (CORE_ADDR *)obstack_next_free (&dont_print_statmem_obstack)
518 if (VALUE_ADDRESS (val) == first_dont_print[i])
520 fputs_filtered ("<same as static member of an already seen type>",
526 obstack_grow (&dont_print_statmem_obstack, &VALUE_ADDRESS (val),
529 check_stub_type (type);
530 cp_print_value_fields (type, VALUE_CONTENTS (val),
531 stream, format, recurse, pretty,
535 val_print (type, VALUE_CONTENTS (val), VALUE_ADDRESS (val),
536 stream, format, 0, recurse, pretty);
540 cp_print_class_member (valaddr, domain, stream, prefix)
547 /* VAL is a byte offset into the structure type DOMAIN.
548 Find the name of the field for that offset and
552 register unsigned int i;
553 unsigned len = TYPE_NFIELDS (domain);
554 /* @@ Make VAL into bit offset */
555 LONGEST val = unpack_long (builtin_type_int, valaddr) << 3;
556 for (i = TYPE_N_BASECLASSES (domain); i < len; i++)
558 int bitpos = TYPE_FIELD_BITPOS (domain, i);
562 if (val < bitpos && i != 0)
564 /* Somehow pointing into a field. */
566 extra = (val - TYPE_FIELD_BITPOS (domain, i));
577 fprintf_filtered (stream, prefix);
578 name = type_name_no_tag (domain);
580 fputs_filtered (name, stream);
582 c_type_print_base (domain, stream, 0, 0);
583 fprintf_filtered (stream, "::");
584 fputs_filtered (TYPE_FIELD_NAME (domain, i), stream);
586 fprintf_filtered (stream, " + %d bytes", extra);
588 fprintf_filtered (stream, " (offset in bits)");
591 fprintf_filtered (stream, "%d", val >> 3);
595 _initialize_cp_valprint ()
598 (add_set_cmd ("static-members", class_support, var_boolean,
599 (char *)&static_field_print,
600 "Set printing of C++ static members.",
603 /* Turn on printing of static fields. */
604 static_field_print = 1;
607 (add_set_cmd ("vtbl", class_support, var_boolean, (char *)&vtblprint,
608 "Set printing of C++ virtual function tables.",
613 (add_set_cmd ("object", class_support, var_boolean, (char *)&objectprint,
614 "Set printing of object's derived type based on vtable info.",
618 /* Give people the defaults which they are used to. */
621 obstack_begin (&dont_print_vb_obstack, 32 * sizeof (struct type *));
622 obstack_specify_allocation (&dont_print_statmem_obstack,
623 32 * sizeof (CORE_ADDR), sizeof (CORE_ADDR),