1 /* Support for printing C++ values for GDB, the GNU debugger.
2 Copyright 1986, 1988, 1989, 1991, 1994, 1995, 1996
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"
34 int vtblprint; /* Controls printing of vtbl's */
35 int objectprint; /* Controls looking up an object's derived type
36 using what we find in its vtables. */
37 static int static_field_print; /* Controls printing of static fields. */
39 static struct obstack dont_print_vb_obstack;
40 static struct obstack dont_print_statmem_obstack;
43 cp_print_static_field PARAMS ((struct type *, value_ptr, GDB_FILE *, int, int,
44 enum val_prettyprint));
47 cp_print_value PARAMS ((struct type *, char *, CORE_ADDR, GDB_FILE *,
48 int, int, enum val_prettyprint, struct type **));
51 cp_print_class_method (valaddr, type, stream)
57 struct fn_field *f = NULL;
66 struct type *target_type = check_typedef (TYPE_TARGET_TYPE (type));
68 domain = TYPE_DOMAIN_TYPE (target_type);
69 if (domain == (struct type *)NULL)
71 fprintf_filtered (stream, "<unknown>");
74 addr = unpack_pointer (lookup_pointer_type (builtin_type_void), valaddr);
75 if (METHOD_PTR_IS_VIRTUAL (addr))
77 offset = METHOD_PTR_TO_VOFFSET (addr);
78 len = TYPE_NFN_FIELDS (domain);
79 for (i = 0; i < len; i++)
81 f = TYPE_FN_FIELDLIST1 (domain, i);
82 len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
84 for (j = 0; j < len2; j++)
87 if (TYPE_FN_FIELD_VOFFSET (f, j) == offset)
97 sym = find_pc_function (addr);
100 error ("invalid pointer to member function");
102 len = TYPE_NFN_FIELDS (domain);
103 for (i = 0; i < len; i++)
105 f = TYPE_FN_FIELDLIST1 (domain, i);
106 len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
108 for (j = 0; j < len2; j++)
111 if (TYPE_FN_FIELD_STUB (f, j))
112 check_stub_method (domain, i, j);
113 if (STREQ (SYMBOL_NAME (sym), TYPE_FN_FIELD_PHYSNAME (f, j)))
123 fprintf_filtered (stream, "&");
124 c_type_print_varspec_prefix (TYPE_FN_FIELD_TYPE (f, j), stream, 0, 0);
125 fprintf_unfiltered (stream, kind);
126 if (TYPE_FN_FIELD_PHYSNAME (f, j)[0] == '_'
127 && is_cplus_marker (TYPE_FN_FIELD_PHYSNAME (f, j)[1]))
129 cp_type_print_method_args (TYPE_FN_FIELD_ARGS (f, j) + 1, "~",
130 TYPE_FN_FIELDLIST_NAME (domain, i),
135 cp_type_print_method_args (TYPE_FN_FIELD_ARGS (f, j), "",
136 TYPE_FN_FIELDLIST_NAME (domain, i),
142 fprintf_filtered (stream, "(");
143 type_print (type, "", stream, -1);
144 fprintf_filtered (stream, ") %d", (int) addr >> 3);
148 /* This was what it was for gcc 2.4.5 and earlier. */
149 static const char vtbl_ptr_name_old[] =
150 { CPLUS_MARKER,'v','t','b','l','_','p','t','r','_','t','y','p','e', 0 };
151 /* It was changed to this after 2.4.5. */
152 const char vtbl_ptr_name[] =
153 { '_','_','v','t','b','l','_','p','t','r','_','t','y','p','e', 0 };
155 /* Return truth value for assertion that TYPE is of the type
156 "pointer to virtual function". */
159 cp_is_vtbl_ptr_type(type)
162 char *typename = type_name_no_tag (type);
164 return (typename != NULL
165 && (STREQ (typename, vtbl_ptr_name)
166 || STREQ (typename, vtbl_ptr_name_old)));
169 /* Return truth value for the assertion that TYPE is of the type
170 "pointer to virtual function table". */
173 cp_is_vtbl_member(type)
176 if (TYPE_CODE (type) == TYPE_CODE_PTR)
178 type = TYPE_TARGET_TYPE (type);
179 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
181 type = TYPE_TARGET_TYPE (type);
182 if (TYPE_CODE (type) == TYPE_CODE_STRUCT /* if not using thunks */
183 || TYPE_CODE (type) == TYPE_CODE_PTR) /* if using thunks */
185 /* Virtual functions tables are full of pointers
186 to virtual functions. */
187 return cp_is_vtbl_ptr_type (type);
194 /* Mutually recursive subroutines of cp_print_value and c_val_print to
195 print out a structure's fields: cp_print_value_fields and cp_print_value.
197 TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and PRETTY have the
198 same meanings as in cp_print_value and c_val_print.
200 DONT_PRINT is an array of baseclass types that we
201 should not print, or zero if called from top level. */
204 cp_print_value_fields (type, valaddr, address, stream, format, recurse, pretty,
205 dont_print_vb, dont_print_statmem)
212 enum val_prettyprint pretty;
213 struct type **dont_print_vb;
214 int dont_print_statmem;
216 int i, len, n_baseclasses;
217 struct obstack tmp_obstack;
218 char *last_dont_print = obstack_next_free (&dont_print_statmem_obstack);
220 CHECK_TYPEDEF (type);
222 fprintf_filtered (stream, "{");
223 len = TYPE_NFIELDS (type);
224 n_baseclasses = TYPE_N_BASECLASSES (type);
226 /* Print out baseclasses such that we don't print
227 duplicates of virtual baseclasses. */
228 if (n_baseclasses > 0)
229 cp_print_value (type, valaddr, address, stream,
230 format, recurse+1, pretty, dont_print_vb);
232 if (!len && n_baseclasses == 1)
233 fprintf_filtered (stream, "<No data fields>");
236 extern int inspect_it;
239 if (dont_print_statmem == 0)
241 /* If we're at top level, carve out a completely fresh
242 chunk of the obstack and use that until this particular
243 invocation returns. */
244 tmp_obstack = dont_print_statmem_obstack;
245 obstack_finish (&dont_print_statmem_obstack);
248 for (i = n_baseclasses; i < len; i++)
250 /* If requested, skip printing of static fields. */
251 if (!static_field_print && TYPE_FIELD_STATIC (type, i))
254 fprintf_filtered (stream, ", ");
255 else if (n_baseclasses > 0)
259 fprintf_filtered (stream, "\n");
260 print_spaces_filtered (2 + 2 * recurse, stream);
261 fputs_filtered ("members of ", stream);
262 fputs_filtered (type_name_no_tag (type), stream);
263 fputs_filtered (": ", stream);
270 fprintf_filtered (stream, "\n");
271 print_spaces_filtered (2 + 2 * recurse, stream);
275 wrap_here (n_spaces (2 + 2 * recurse));
279 if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
280 fputs_filtered ("\"( ptr \"", stream);
282 fputs_filtered ("\"( nodef \"", stream);
283 if (TYPE_FIELD_STATIC (type, i))
284 fputs_filtered ("static ", stream);
285 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
287 DMGL_PARAMS | DMGL_ANSI);
288 fputs_filtered ("\" \"", stream);
289 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
291 DMGL_PARAMS | DMGL_ANSI);
292 fputs_filtered ("\") \"", stream);
296 annotate_field_begin (TYPE_FIELD_TYPE (type, i));
298 if (TYPE_FIELD_STATIC (type, i))
299 fputs_filtered ("static ", stream);
300 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
302 DMGL_PARAMS | DMGL_ANSI);
303 annotate_field_name_end ();
304 fputs_filtered (" = ", stream);
305 annotate_field_value ();
308 if (!TYPE_FIELD_STATIC (type, i) && TYPE_FIELD_PACKED (type, i))
312 /* Bitfields require special handling, especially due to byte
314 if (TYPE_FIELD_IGNORE (type, i))
316 fputs_filtered ("<optimized out or zero length>", stream);
320 v = value_from_longest (TYPE_FIELD_TYPE (type, i),
321 unpack_field_as_long (type, valaddr, i));
323 val_print (TYPE_FIELD_TYPE(type, i), VALUE_CONTENTS (v), 0,
324 stream, format, 0, recurse + 1, pretty);
329 if (TYPE_FIELD_IGNORE (type, i))
331 fputs_filtered ("<optimized out or zero length>", stream);
333 else if (TYPE_FIELD_STATIC (type, i))
336 char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (type, i);
338 lookup_symbol (phys_name, 0, VAR_NAMESPACE, 0, NULL);
340 fputs_filtered ("<optimized out>", stream);
343 v = value_at (TYPE_FIELD_TYPE (type, i),
344 (CORE_ADDR)SYMBOL_BLOCK_VALUE (sym));
345 cp_print_static_field (TYPE_FIELD_TYPE (type, i), v,
346 stream, format, recurse + 1,
352 val_print (TYPE_FIELD_TYPE (type, i),
353 valaddr + TYPE_FIELD_BITPOS (type, i) / 8,
354 0, stream, format, 0, recurse + 1, pretty);
357 annotate_field_end ();
360 if (dont_print_statmem == 0)
362 /* Free the space used to deal with the printing
363 of the members from top level. */
364 obstack_free (&dont_print_statmem_obstack, last_dont_print);
365 dont_print_statmem_obstack = tmp_obstack;
370 fprintf_filtered (stream, "\n");
371 print_spaces_filtered (2 * recurse, stream);
374 fprintf_filtered (stream, "}");
377 /* Special val_print routine to avoid printing multiple copies of virtual
381 cp_print_value (type, valaddr, address, stream, format, recurse, pretty,
389 enum val_prettyprint pretty;
390 struct type **dont_print_vb;
392 struct obstack tmp_obstack;
393 struct type **last_dont_print
394 = (struct type **)obstack_next_free (&dont_print_vb_obstack);
395 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
397 if (dont_print_vb == 0)
399 /* If we're at top level, carve out a completely fresh
400 chunk of the obstack and use that until this particular
401 invocation returns. */
402 tmp_obstack = dont_print_vb_obstack;
403 /* Bump up the high-water mark. Now alpha is omega. */
404 obstack_finish (&dont_print_vb_obstack);
407 for (i = 0; i < n_baseclasses; i++)
410 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
411 char *basename = TYPE_NAME (baseclass);
413 if (BASETYPE_VIA_VIRTUAL (type, i))
415 struct type **first_dont_print
416 = (struct type **)obstack_base (&dont_print_vb_obstack);
418 int j = (struct type **)obstack_next_free (&dont_print_vb_obstack)
422 if (baseclass == first_dont_print[j])
425 obstack_ptr_grow (&dont_print_vb_obstack, baseclass);
428 boffset = baseclass_offset (type, i , valaddr, address);
432 fprintf_filtered (stream, "\n");
433 print_spaces_filtered (2 * recurse, stream);
435 fputs_filtered ("<", stream);
436 /* Not sure what the best notation is in the case where there is no
438 fputs_filtered (basename ? basename : "", stream);
439 fputs_filtered ("> = ", stream);
441 fprintf_filtered (stream, "<invalid address>");
443 cp_print_value_fields (baseclass, valaddr + boffset, address + boffset,
444 stream, format, recurse, pretty,
445 (struct type **) obstack_base (&dont_print_vb_obstack),
447 fputs_filtered (", ", stream);
453 if (dont_print_vb == 0)
455 /* Free the space used to deal with the printing
456 of this type from top level. */
457 obstack_free (&dont_print_vb_obstack, last_dont_print);
458 /* Reset watermark so that we can continue protecting
459 ourselves from whatever we were protecting ourselves. */
460 dont_print_vb_obstack = tmp_obstack;
464 /* Print value of a static member.
465 To avoid infinite recursion when printing a class that contains
466 a static instance of the class, we keep the addresses of all printed
467 static member classes in an obstack and refuse to print them more
470 VAL contains the value to print, TYPE, STREAM, RECURSE, and PRETTY
471 have the same meanings as in c_val_print. */
474 cp_print_static_field (type, val, stream, format, recurse, pretty)
480 enum val_prettyprint pretty;
482 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
484 CORE_ADDR *first_dont_print;
488 = (CORE_ADDR *)obstack_base (&dont_print_statmem_obstack);
489 i = (CORE_ADDR *)obstack_next_free (&dont_print_statmem_obstack)
494 if (VALUE_ADDRESS (val) == first_dont_print[i])
496 fputs_filtered ("<same as static member of an already seen type>",
502 obstack_grow (&dont_print_statmem_obstack, (char *) &VALUE_ADDRESS (val),
505 CHECK_TYPEDEF (type);
506 cp_print_value_fields (type, VALUE_CONTENTS (val), VALUE_ADDRESS (val),
507 stream, format, recurse, pretty, NULL, 1);
510 val_print (type, VALUE_CONTENTS (val), VALUE_ADDRESS (val),
511 stream, format, 0, recurse, pretty);
515 cp_print_class_member (valaddr, domain, stream, prefix)
522 /* VAL is a byte offset into the structure type DOMAIN.
523 Find the name of the field for that offset and
527 register unsigned int i;
528 unsigned len = TYPE_NFIELDS (domain);
529 /* @@ Make VAL into bit offset */
530 LONGEST val = unpack_long (builtin_type_int, valaddr) << 3;
531 for (i = TYPE_N_BASECLASSES (domain); i < len; i++)
533 int bitpos = TYPE_FIELD_BITPOS (domain, i);
537 if (val < bitpos && i != 0)
539 /* Somehow pointing into a field. */
541 extra = (val - TYPE_FIELD_BITPOS (domain, i));
552 fprintf_filtered (stream, prefix);
553 name = type_name_no_tag (domain);
555 fputs_filtered (name, stream);
557 c_type_print_base (domain, stream, 0, 0);
558 fprintf_filtered (stream, "::");
559 fputs_filtered (TYPE_FIELD_NAME (domain, i), stream);
561 fprintf_filtered (stream, " + %d bytes", extra);
563 fprintf_filtered (stream, " (offset in bits)");
566 fprintf_filtered (stream, "%d", val >> 3);
570 _initialize_cp_valprint ()
573 (add_set_cmd ("static-members", class_support, var_boolean,
574 (char *)&static_field_print,
575 "Set printing of C++ static members.",
578 /* Turn on printing of static fields. */
579 static_field_print = 1;
582 (add_set_cmd ("vtbl", class_support, var_boolean, (char *)&vtblprint,
583 "Set printing of C++ virtual function tables.",
588 (add_set_cmd ("object", class_support, var_boolean, (char *)&objectprint,
589 "Set printing of object's derived type based on vtable info.",
593 /* Give people the defaults which they are used to. */
596 obstack_begin (&dont_print_vb_obstack, 32 * sizeof (struct type *));
597 obstack_specify_allocation (&dont_print_statmem_obstack,
598 32 * sizeof (CORE_ADDR), sizeof (CORE_ADDR),