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"
35 int vtblprint; /* Controls printing of vtbl's */
36 int objectprint; /* Controls looking up an object's derived type
37 using what we find in its vtables. */
38 static int static_field_print; /* Controls printing of static fields. */
40 static struct obstack dont_print_vb_obstack;
41 static struct obstack dont_print_statmem_obstack;
44 cp_print_static_field PARAMS ((struct type *, value_ptr, GDB_FILE *, int, int,
45 enum val_prettyprint));
48 cp_print_value PARAMS ((struct type *, char *, CORE_ADDR, GDB_FILE *,
49 int, int, enum val_prettyprint, struct type **));
52 cp_print_class_method (valaddr, type, stream)
58 struct fn_field *f = NULL;
67 struct type *target_type = check_typedef (TYPE_TARGET_TYPE (type));
69 domain = TYPE_DOMAIN_TYPE (target_type);
70 if (domain == (struct type *)NULL)
72 fprintf_filtered (stream, "<unknown>");
75 addr = unpack_pointer (lookup_pointer_type (builtin_type_void), valaddr);
76 if (METHOD_PTR_IS_VIRTUAL (addr))
78 offset = METHOD_PTR_TO_VOFFSET (addr);
79 len = TYPE_NFN_FIELDS (domain);
80 for (i = 0; i < len; i++)
82 f = TYPE_FN_FIELDLIST1 (domain, i);
83 len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
85 for (j = 0; j < len2; j++)
88 if (TYPE_FN_FIELD_VOFFSET (f, j) == offset)
90 if (TYPE_FN_FIELD_STUB (f, j))
91 check_stub_method (domain, i, j);
100 sym = find_pc_function (addr);
103 error ("invalid pointer to member function");
105 len = TYPE_NFN_FIELDS (domain);
106 for (i = 0; i < len; i++)
108 f = TYPE_FN_FIELDLIST1 (domain, i);
109 len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
111 for (j = 0; j < len2; j++)
114 if (TYPE_FN_FIELD_STUB (f, j))
115 check_stub_method (domain, i, j);
116 if (STREQ (SYMBOL_NAME (sym), TYPE_FN_FIELD_PHYSNAME (f, j)))
126 char *demangled_name;
128 fprintf_filtered (stream, "&");
129 fprintf_filtered (stream, kind);
130 demangled_name = cplus_demangle (TYPE_FN_FIELD_PHYSNAME (f, j),
131 DMGL_ANSI | DMGL_PARAMS);
132 if (demangled_name == NULL)
133 fprintf_filtered (stream, "<badly mangled name %s>",
134 TYPE_FN_FIELD_PHYSNAME (f, j));
137 fputs_filtered (demangled_name, stream);
138 free (demangled_name);
143 fprintf_filtered (stream, "(");
144 type_print (type, "", stream, -1);
145 fprintf_filtered (stream, ") %d", (int) addr >> 3);
149 /* This was what it was for gcc 2.4.5 and earlier. */
150 static const char vtbl_ptr_name_old[] =
151 { CPLUS_MARKER,'v','t','b','l','_','p','t','r','_','t','y','p','e', 0 };
152 /* It was changed to this after 2.4.5. */
153 const char vtbl_ptr_name[] =
154 { '_','_','v','t','b','l','_','p','t','r','_','t','y','p','e', 0 };
156 /* Return truth value for assertion that TYPE is of the type
157 "pointer to virtual function". */
160 cp_is_vtbl_ptr_type(type)
163 char *typename = type_name_no_tag (type);
165 return (typename != NULL
166 && (STREQ (typename, vtbl_ptr_name)
167 || STREQ (typename, vtbl_ptr_name_old)));
170 /* Return truth value for the assertion that TYPE is of the type
171 "pointer to virtual function table". */
174 cp_is_vtbl_member(type)
177 if (TYPE_CODE (type) == TYPE_CODE_PTR)
179 type = TYPE_TARGET_TYPE (type);
180 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
182 type = TYPE_TARGET_TYPE (type);
183 if (TYPE_CODE (type) == TYPE_CODE_STRUCT /* if not using thunks */
184 || TYPE_CODE (type) == TYPE_CODE_PTR) /* if using thunks */
186 /* Virtual functions tables are full of pointers
187 to virtual functions. */
188 return cp_is_vtbl_ptr_type (type);
195 /* Mutually recursive subroutines of cp_print_value and c_val_print to
196 print out a structure's fields: cp_print_value_fields and cp_print_value.
198 TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and PRETTY have the
199 same meanings as in cp_print_value and c_val_print.
201 DONT_PRINT is an array of baseclass types that we
202 should not print, or zero if called from top level. */
205 cp_print_value_fields (type, valaddr, address, stream, format, recurse, pretty,
206 dont_print_vb, dont_print_statmem)
213 enum val_prettyprint pretty;
214 struct type **dont_print_vb;
215 int dont_print_statmem;
217 int i, len, n_baseclasses;
218 struct obstack tmp_obstack;
219 char *last_dont_print = obstack_next_free (&dont_print_statmem_obstack);
221 CHECK_TYPEDEF (type);
223 fprintf_filtered (stream, "{");
224 len = TYPE_NFIELDS (type);
225 n_baseclasses = TYPE_N_BASECLASSES (type);
227 /* Print out baseclasses such that we don't print
228 duplicates of virtual baseclasses. */
229 if (n_baseclasses > 0)
230 cp_print_value (type, valaddr, address, stream,
231 format, recurse+1, pretty, dont_print_vb);
233 if (!len && n_baseclasses == 1)
234 fprintf_filtered (stream, "<No data fields>");
237 extern int inspect_it;
240 if (dont_print_statmem == 0)
242 /* If we're at top level, carve out a completely fresh
243 chunk of the obstack and use that until this particular
244 invocation returns. */
245 tmp_obstack = dont_print_statmem_obstack;
246 obstack_finish (&dont_print_statmem_obstack);
249 for (i = n_baseclasses; i < len; i++)
251 /* If requested, skip printing of static fields. */
252 if (!static_field_print && TYPE_FIELD_STATIC (type, i))
255 fprintf_filtered (stream, ", ");
256 else if (n_baseclasses > 0)
260 fprintf_filtered (stream, "\n");
261 print_spaces_filtered (2 + 2 * recurse, stream);
262 fputs_filtered ("members of ", stream);
263 fputs_filtered (type_name_no_tag (type), stream);
264 fputs_filtered (": ", stream);
271 fprintf_filtered (stream, "\n");
272 print_spaces_filtered (2 + 2 * recurse, stream);
276 wrap_here (n_spaces (2 + 2 * recurse));
280 if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
281 fputs_filtered ("\"( ptr \"", stream);
283 fputs_filtered ("\"( nodef \"", stream);
284 if (TYPE_FIELD_STATIC (type, i))
285 fputs_filtered ("static ", stream);
286 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
288 DMGL_PARAMS | DMGL_ANSI);
289 fputs_filtered ("\" \"", stream);
290 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
292 DMGL_PARAMS | DMGL_ANSI);
293 fputs_filtered ("\") \"", stream);
297 annotate_field_begin (TYPE_FIELD_TYPE (type, i));
299 if (TYPE_FIELD_STATIC (type, i))
300 fputs_filtered ("static ", stream);
301 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
303 DMGL_PARAMS | DMGL_ANSI);
304 annotate_field_name_end ();
305 fputs_filtered (" = ", stream);
306 annotate_field_value ();
309 if (!TYPE_FIELD_STATIC (type, i) && TYPE_FIELD_PACKED (type, i))
313 /* Bitfields require special handling, especially due to byte
315 if (TYPE_FIELD_IGNORE (type, i))
317 fputs_filtered ("<optimized out or zero length>", stream);
321 v = value_from_longest (TYPE_FIELD_TYPE (type, i),
322 unpack_field_as_long (type, valaddr, i));
324 val_print (TYPE_FIELD_TYPE(type, i), VALUE_CONTENTS (v), 0,
325 stream, format, 0, recurse + 1, pretty);
330 if (TYPE_FIELD_IGNORE (type, i))
332 fputs_filtered ("<optimized out or zero length>", stream);
334 else if (TYPE_FIELD_STATIC (type, i))
337 char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (type, i);
339 lookup_symbol (phys_name, 0, VAR_NAMESPACE, 0, NULL);
341 fputs_filtered ("<optimized out>", stream);
344 v = value_at (TYPE_FIELD_TYPE (type, i),
345 SYMBOL_VALUE_ADDRESS (sym),
347 cp_print_static_field (TYPE_FIELD_TYPE (type, i), v,
348 stream, format, recurse + 1,
354 val_print (TYPE_FIELD_TYPE (type, i),
355 valaddr + TYPE_FIELD_BITPOS (type, i) / 8,
356 address + TYPE_FIELD_BITPOS (type, i) / 8,
357 stream, format, 0, recurse + 1, pretty);
360 annotate_field_end ();
363 if (dont_print_statmem == 0)
365 /* Free the space used to deal with the printing
366 of the members from top level. */
367 obstack_free (&dont_print_statmem_obstack, last_dont_print);
368 dont_print_statmem_obstack = tmp_obstack;
373 fprintf_filtered (stream, "\n");
374 print_spaces_filtered (2 * recurse, stream);
377 fprintf_filtered (stream, "}");
380 /* Special val_print routine to avoid printing multiple copies of virtual
384 cp_print_value (type, valaddr, address, stream, format, recurse, pretty,
392 enum val_prettyprint pretty;
393 struct type **dont_print_vb;
395 struct obstack tmp_obstack;
396 struct type **last_dont_print
397 = (struct type **)obstack_next_free (&dont_print_vb_obstack);
398 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
400 if (dont_print_vb == 0)
402 /* If we're at top level, carve out a completely fresh
403 chunk of the obstack and use that until this particular
404 invocation returns. */
405 tmp_obstack = dont_print_vb_obstack;
406 /* Bump up the high-water mark. Now alpha is omega. */
407 obstack_finish (&dont_print_vb_obstack);
410 for (i = 0; i < n_baseclasses; i++)
413 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
414 char *basename = TYPE_NAME (baseclass);
417 if (BASETYPE_VIA_VIRTUAL (type, i))
419 struct type **first_dont_print
420 = (struct type **)obstack_base (&dont_print_vb_obstack);
422 int j = (struct type **)obstack_next_free (&dont_print_vb_obstack)
426 if (baseclass == first_dont_print[j])
429 obstack_ptr_grow (&dont_print_vb_obstack, baseclass);
432 boffset = baseclass_offset (type, i , valaddr, address);
436 fprintf_filtered (stream, "\n");
437 print_spaces_filtered (2 * recurse, stream);
439 fputs_filtered ("<", stream);
440 /* Not sure what the best notation is in the case where there is no
442 fputs_filtered (basename ? basename : "", stream);
443 fputs_filtered ("> = ", stream);
445 /* The virtual base class pointer might have been clobbered by the
446 user program. Make sure that it still points to a valid memory
449 if (boffset != -1 && (boffset < 0 || boffset >= TYPE_LENGTH (type)))
451 base_valaddr = (char *) alloca (TYPE_LENGTH (baseclass));
452 if (target_read_memory (address + boffset, base_valaddr,
453 TYPE_LENGTH (baseclass)) != 0)
457 base_valaddr = valaddr + boffset;
460 fprintf_filtered (stream, "<invalid address>");
462 cp_print_value_fields (baseclass, base_valaddr, address + boffset,
463 stream, format, recurse, pretty,
464 (struct type **) obstack_base (&dont_print_vb_obstack),
466 fputs_filtered (", ", stream);
472 if (dont_print_vb == 0)
474 /* Free the space used to deal with the printing
475 of this type from top level. */
476 obstack_free (&dont_print_vb_obstack, last_dont_print);
477 /* Reset watermark so that we can continue protecting
478 ourselves from whatever we were protecting ourselves. */
479 dont_print_vb_obstack = tmp_obstack;
483 /* Print value of a static member.
484 To avoid infinite recursion when printing a class that contains
485 a static instance of the class, we keep the addresses of all printed
486 static member classes in an obstack and refuse to print them more
489 VAL contains the value to print, TYPE, STREAM, RECURSE, and PRETTY
490 have the same meanings as in c_val_print. */
493 cp_print_static_field (type, val, stream, format, recurse, pretty)
499 enum val_prettyprint pretty;
501 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
503 CORE_ADDR *first_dont_print;
507 = (CORE_ADDR *)obstack_base (&dont_print_statmem_obstack);
508 i = (CORE_ADDR *)obstack_next_free (&dont_print_statmem_obstack)
513 if (VALUE_ADDRESS (val) == first_dont_print[i])
515 fputs_filtered ("<same as static member of an already seen type>",
521 obstack_grow (&dont_print_statmem_obstack, (char *) &VALUE_ADDRESS (val),
524 CHECK_TYPEDEF (type);
525 cp_print_value_fields (type, VALUE_CONTENTS (val), VALUE_ADDRESS (val),
526 stream, format, recurse, pretty, NULL, 1);
529 val_print (type, VALUE_CONTENTS (val), VALUE_ADDRESS (val),
530 stream, format, 0, recurse, pretty);
534 cp_print_class_member (valaddr, domain, stream, prefix)
541 /* VAL is a byte offset into the structure type DOMAIN.
542 Find the name of the field for that offset and
546 register unsigned int i;
547 unsigned len = TYPE_NFIELDS (domain);
548 /* @@ Make VAL into bit offset */
549 LONGEST val = unpack_long (builtin_type_int, valaddr) << 3;
550 for (i = TYPE_N_BASECLASSES (domain); i < len; i++)
552 int bitpos = TYPE_FIELD_BITPOS (domain, i);
556 if (val < bitpos && i != 0)
558 /* Somehow pointing into a field. */
560 extra = (val - TYPE_FIELD_BITPOS (domain, i));
571 fprintf_filtered (stream, prefix);
572 name = type_name_no_tag (domain);
574 fputs_filtered (name, stream);
576 c_type_print_base (domain, stream, 0, 0);
577 fprintf_filtered (stream, "::");
578 fputs_filtered (TYPE_FIELD_NAME (domain, i), stream);
580 fprintf_filtered (stream, " + %d bytes", extra);
582 fprintf_filtered (stream, " (offset in bits)");
585 fprintf_filtered (stream, "%d", val >> 3);
589 _initialize_cp_valprint ()
592 (add_set_cmd ("static-members", class_support, var_boolean,
593 (char *)&static_field_print,
594 "Set printing of C++ static members.",
597 /* Turn on printing of static fields. */
598 static_field_print = 1;
601 (add_set_cmd ("vtbl", class_support, var_boolean, (char *)&vtblprint,
602 "Set printing of C++ virtual function tables.",
607 (add_set_cmd ("object", class_support, var_boolean, (char *)&objectprint,
608 "Set printing of object's derived type based on vtable info.",
612 /* Give people the defaults which they are used to. */
615 obstack_begin (&dont_print_vb_obstack, 32 * sizeof (struct type *));
616 obstack_specify_allocation (&dont_print_statmem_obstack,
617 32 * sizeof (CORE_ADDR), sizeof (CORE_ADDR),