Automatic date update in version.in
[binutils-gdb.git] / gdb / cp-valprint.c
bloba803c786d2b068d4e066de930e371e17f7352af6
1 /* Support for printing C++ values for GDB, the GNU debugger.
3 Copyright (C) 1986-2023 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 3 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, see <http://www.gnu.org/licenses/>. */
20 #include "defs.h"
21 #include "gdbsupport/gdb_obstack.h"
22 #include "symtab.h"
23 #include "gdbtypes.h"
24 #include "expression.h"
25 #include "value.h"
26 #include "command.h"
27 #include "gdbcmd.h"
28 #include "demangle.h"
29 #include "annotate.h"
30 #include "c-lang.h"
31 #include "target.h"
32 #include "cp-abi.h"
33 #include "valprint.h"
34 #include "cp-support.h"
35 #include "language.h"
36 #include "extension.h"
37 #include "typeprint.h"
38 #include "gdbsupport/byte-vector.h"
39 #include "gdbarch.h"
40 #include "cli/cli-style.h"
41 #include "gdbsupport/selftest.h"
42 #include "selftest-arch.h"
44 static struct obstack dont_print_vb_obstack;
45 static struct obstack dont_print_statmem_obstack;
46 static struct obstack dont_print_stat_array_obstack;
48 static void cp_print_static_field (struct type *, struct value *,
49 struct ui_file *, int,
50 const struct value_print_options *);
52 static void cp_print_value (struct value *, struct ui_file *,
53 int, const struct value_print_options *,
54 struct type **);
57 /* GCC versions after 2.4.5 use this. */
58 const char vtbl_ptr_name[] = "__vtbl_ptr_type";
60 /* Return truth value for assertion that TYPE is of the type
61 "pointer to virtual function". */
63 int
64 cp_is_vtbl_ptr_type (struct type *type)
66 const char *type_name = type->name ();
68 return (type_name != NULL && !strcmp (type_name, vtbl_ptr_name));
71 /* Return truth value for the assertion that TYPE is of the type
72 "pointer to virtual function table". */
74 int
75 cp_is_vtbl_member (struct type *type)
77 /* With older versions of g++, the vtbl field pointed to an array of
78 structures. Nowadays it points directly to the structure. */
79 if (type->code () == TYPE_CODE_PTR)
81 type = type->target_type ();
82 if (type->code () == TYPE_CODE_ARRAY)
84 type = type->target_type ();
85 if (type->code () == TYPE_CODE_STRUCT /* if not using thunks */
86 || type->code () == TYPE_CODE_PTR) /* if using thunks */
88 /* Virtual functions tables are full of pointers
89 to virtual functions. */
90 return cp_is_vtbl_ptr_type (type);
93 else if (type->code () == TYPE_CODE_STRUCT) /* if not using thunks */
95 return cp_is_vtbl_ptr_type (type);
97 else if (type->code () == TYPE_CODE_PTR) /* if using thunks */
99 /* The type name of the thunk pointer is NULL when using
100 dwarf2. We could test for a pointer to a function, but
101 there is no type info for the virtual table either, so it
102 wont help. */
103 return cp_is_vtbl_ptr_type (type);
106 return 0;
109 /* Mutually recursive subroutines of cp_print_value and c_val_print to
110 print out a structure's fields: cp_print_value_fields and
111 cp_print_value.
113 TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and OPTIONS have the same
114 meanings as in cp_print_value and c_val_print.
116 2nd argument REAL_TYPE is used to carry over the type of the
117 derived class across the recursion to base classes.
119 DONT_PRINT is an array of baseclass types that we should not print,
120 or zero if called from top level. */
122 void
123 cp_print_value_fields (struct value *val, struct ui_file *stream,
124 int recurse, const struct value_print_options *options,
125 struct type **dont_print_vb,
126 int dont_print_statmem)
128 int i, len, n_baseclasses;
129 int fields_seen = 0;
130 static int last_set_recurse = -1;
132 struct type *type = check_typedef (val->type ());
134 if (recurse == 0)
136 /* Any object can be left on obstacks only during an unexpected
137 error. */
139 if (obstack_object_size (&dont_print_statmem_obstack) > 0)
141 obstack_free (&dont_print_statmem_obstack, NULL);
142 obstack_begin (&dont_print_statmem_obstack,
143 32 * sizeof (CORE_ADDR));
145 if (obstack_object_size (&dont_print_stat_array_obstack) > 0)
147 obstack_free (&dont_print_stat_array_obstack, NULL);
148 obstack_begin (&dont_print_stat_array_obstack,
149 32 * sizeof (struct type *));
153 gdb_printf (stream, "{");
154 len = type->num_fields ();
155 n_baseclasses = TYPE_N_BASECLASSES (type);
157 /* First, print out baseclasses such that we don't print
158 duplicates of virtual baseclasses. */
160 if (n_baseclasses > 0)
161 cp_print_value (val, stream, recurse + 1, options, dont_print_vb);
163 /* Second, print out data fields */
165 /* If there are no data fields, skip this part */
166 if (len == n_baseclasses || !len)
167 fprintf_styled (stream, metadata_style.style (), "<No data fields>");
168 else
170 size_t statmem_obstack_initial_size = 0;
171 size_t stat_array_obstack_initial_size = 0;
172 struct type *vptr_basetype = NULL;
173 int vptr_fieldno;
175 if (dont_print_statmem == 0)
177 statmem_obstack_initial_size =
178 obstack_object_size (&dont_print_statmem_obstack);
180 if (last_set_recurse != recurse)
182 stat_array_obstack_initial_size =
183 obstack_object_size (&dont_print_stat_array_obstack);
185 last_set_recurse = recurse;
189 vptr_fieldno = get_vptr_fieldno (type, &vptr_basetype);
190 for (i = n_baseclasses; i < len; i++)
192 const gdb_byte *valaddr = val->contents_for_printing ().data ();
194 /* If requested, skip printing of static fields. */
195 if (!options->static_field_print
196 && type->field (i).is_static ())
197 continue;
199 if (fields_seen)
201 gdb_puts (",", stream);
202 if (!options->prettyformat)
203 gdb_puts (" ", stream);
205 else if (n_baseclasses > 0)
207 if (options->prettyformat)
209 gdb_printf (stream, "\n");
210 print_spaces (2 + 2 * recurse, stream);
211 gdb_puts ("members of ", stream);
212 gdb_puts (type->name (), stream);
213 gdb_puts (":", stream);
216 fields_seen = 1;
218 if (options->prettyformat)
220 gdb_printf (stream, "\n");
221 print_spaces (2 + 2 * recurse, stream);
223 else
225 stream->wrap_here (2 + 2 * recurse);
228 annotate_field_begin (type->field (i).type ());
230 if (type->field (i).is_static ())
232 gdb_puts ("static ", stream);
233 fprintf_symbol (stream,
234 type->field (i).name (),
235 current_language->la_language,
236 DMGL_PARAMS | DMGL_ANSI);
238 else
239 fputs_styled (type->field (i).name (),
240 variable_name_style.style (), stream);
241 annotate_field_name_end ();
243 /* We tweak various options in a few cases below. */
244 value_print_options options_copy = *options;
245 value_print_options *opts = &options_copy;
247 /* Do not print leading '=' in case of anonymous
248 unions. */
249 if (strcmp (type->field (i).name (), ""))
250 gdb_puts (" = ", stream);
251 else
253 /* If this is an anonymous field then we want to consider it
254 as though it is at its parent's depth when it comes to the
255 max print depth. */
256 if (opts->max_depth != -1 && opts->max_depth < INT_MAX)
257 ++opts->max_depth;
259 annotate_field_value ();
261 if (!type->field (i).is_static ()
262 && type->field (i).is_packed ())
264 struct value *v;
266 /* Bitfields require special handling, especially due to
267 byte order problems. */
268 if (type->field (i).is_ignored ())
270 fputs_styled ("<optimized out or zero length>",
271 metadata_style.style (), stream);
273 else if (val->bits_synthetic_pointer
274 (type->field (i).loc_bitpos (),
275 type->field (i).bitsize ()))
277 fputs_styled (_("<synthetic pointer>"),
278 metadata_style.style (), stream);
280 else
282 opts->deref_ref = false;
284 v = value_field_bitfield (type, i, valaddr,
285 val->embedded_offset (), val);
287 common_val_print (v, stream, recurse + 1,
288 opts, current_language);
291 else
293 if (type->field (i).is_ignored ())
295 fputs_styled ("<optimized out or zero length>",
296 metadata_style.style (), stream);
298 else if (type->field (i).is_static ())
302 struct value *v = value_static_field (type, i);
304 cp_print_static_field (type->field (i).type (),
305 v, stream, recurse + 1,
306 opts);
308 catch (const gdb_exception_error &ex)
310 fprintf_styled (stream, metadata_style.style (),
311 _("<error reading variable: %s>"),
312 ex.what ());
315 else if (i == vptr_fieldno && type == vptr_basetype)
317 int i_offset = type->field (i).loc_bitpos () / 8;
318 struct type *i_type = type->field (i).type ();
320 if (valprint_check_validity (stream, i_type, i_offset, val))
322 CORE_ADDR addr;
324 i_offset += val->embedded_offset ();
325 addr = extract_typed_address (valaddr + i_offset, i_type);
326 print_function_pointer_address (opts,
327 type->arch (),
328 addr, stream);
331 else
333 struct value *v = val->primitive_field (0, i, type);
334 opts->deref_ref = false;
335 common_val_print (v, stream, recurse + 1, opts,
336 current_language);
339 annotate_field_end ();
342 if (dont_print_statmem == 0)
344 size_t obstack_final_size =
345 obstack_object_size (&dont_print_statmem_obstack);
347 if (obstack_final_size > statmem_obstack_initial_size)
349 /* In effect, a pop of the printed-statics stack. */
350 size_t shrink_bytes
351 = statmem_obstack_initial_size - obstack_final_size;
352 obstack_blank_fast (&dont_print_statmem_obstack, shrink_bytes);
355 if (last_set_recurse != recurse)
357 obstack_final_size =
358 obstack_object_size (&dont_print_stat_array_obstack);
360 if (obstack_final_size > stat_array_obstack_initial_size)
362 void *free_to_ptr =
363 (char *) obstack_next_free (&dont_print_stat_array_obstack)
364 - (obstack_final_size
365 - stat_array_obstack_initial_size);
367 obstack_free (&dont_print_stat_array_obstack,
368 free_to_ptr);
370 last_set_recurse = -1;
374 if (options->prettyformat)
376 gdb_printf (stream, "\n");
377 print_spaces (2 * recurse, stream);
379 } /* if there are data fields */
381 gdb_printf (stream, "}");
384 /* A wrapper for cp_print_value_fields that tries to apply a
385 pretty-printer first. */
387 static void
388 cp_print_value_fields_pp (struct value *val,
389 struct ui_file *stream,
390 int recurse,
391 const struct value_print_options *options,
392 struct type **dont_print_vb,
393 int dont_print_statmem)
395 int result = 0;
397 /* Attempt to run an extension language pretty-printer if
398 possible. */
399 if (!options->raw)
400 result
401 = apply_ext_lang_val_pretty_printer (val, stream,
402 recurse, options,
403 current_language);
405 if (!result)
406 cp_print_value_fields (val, stream, recurse, options, dont_print_vb,
407 dont_print_statmem);
410 /* Special val_print routine to avoid printing multiple copies of
411 virtual baseclasses. */
413 static void
414 cp_print_value (struct value *val, struct ui_file *stream,
415 int recurse, const struct value_print_options *options,
416 struct type **dont_print_vb)
418 struct type *type = check_typedef (val->type ());
419 CORE_ADDR address = val->address ();
420 struct type **last_dont_print
421 = (struct type **) obstack_next_free (&dont_print_vb_obstack);
422 struct obstack tmp_obstack = dont_print_vb_obstack;
423 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
424 const gdb_byte *valaddr = val->contents_for_printing ().data ();
426 if (dont_print_vb == 0)
428 /* If we're at top level, carve out a completely fresh chunk of
429 the obstack and use that until this particular invocation
430 returns. */
431 /* Bump up the high-water mark. Now alpha is omega. */
432 obstack_finish (&dont_print_vb_obstack);
435 for (i = 0; i < n_baseclasses; i++)
437 LONGEST boffset = 0;
438 int skip = 0;
439 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
440 const char *basename = baseclass->name ();
441 struct value *base_val = NULL;
443 if (BASETYPE_VIA_VIRTUAL (type, i))
445 struct type **first_dont_print
446 = (struct type **) obstack_base (&dont_print_vb_obstack);
448 int j = (struct type **)
449 obstack_next_free (&dont_print_vb_obstack) - first_dont_print;
451 while (--j >= 0)
452 if (baseclass == first_dont_print[j])
453 goto flush_it;
455 obstack_ptr_grow (&dont_print_vb_obstack, baseclass);
460 boffset = baseclass_offset (type, i, valaddr,
461 val->embedded_offset (),
462 address, val);
464 catch (const gdb_exception_error &ex)
466 if (ex.error == NOT_AVAILABLE_ERROR)
467 skip = -1;
468 else
469 skip = 1;
472 if (skip == 0)
474 if (BASETYPE_VIA_VIRTUAL (type, i))
476 /* The virtual base class pointer might have been
477 clobbered by the user program. Make sure that it
478 still points to a valid memory location. */
480 if (boffset < 0 || boffset >= type->length ())
482 gdb::byte_vector buf (baseclass->length ());
484 if (target_read_memory (address + boffset, buf.data (),
485 baseclass->length ()) != 0)
486 skip = 1;
487 base_val = value_from_contents_and_address (baseclass,
488 buf.data (),
489 address + boffset);
490 baseclass = base_val->type ();
491 boffset = 0;
493 else
495 base_val = val;
498 else
500 base_val = val;
504 /* Now do the printing. */
505 if (options->prettyformat)
507 gdb_printf (stream, "\n");
508 print_spaces (2 * recurse, stream);
510 gdb_puts ("<", stream);
511 /* Not sure what the best notation is in the case where there is
512 no baseclass name. */
513 gdb_puts (basename ? basename : "", stream);
514 gdb_puts ("> = ", stream);
516 if (skip < 0)
517 val_print_unavailable (stream);
518 else if (skip > 0)
519 val_print_invalid_address (stream);
520 else
522 if (!val_print_check_max_depth (stream, recurse, options,
523 current_language))
525 struct value *baseclass_val = val->primitive_field (0,
526 i, type);
528 cp_print_value_fields_pp
529 (baseclass_val, stream, recurse, options,
530 (struct type **) obstack_base (&dont_print_vb_obstack),
534 gdb_puts (", ", stream);
536 flush_it:
540 if (dont_print_vb == 0)
542 /* Free the space used to deal with the printing
543 of this type from top level. */
544 obstack_free (&dont_print_vb_obstack, last_dont_print);
545 /* Reset watermark so that we can continue protecting
546 ourselves from whatever we were protecting ourselves. */
547 dont_print_vb_obstack = tmp_obstack;
551 /* Print value of a static member. To avoid infinite recursion when
552 printing a class that contains a static instance of the class, we
553 keep the addresses of all printed static member classes in an
554 obstack and refuse to print them more than once.
556 VAL contains the value to print, TYPE, STREAM, RECURSE, and OPTIONS
557 have the same meanings as in c_val_print. */
559 static void
560 cp_print_static_field (struct type *type,
561 struct value *val,
562 struct ui_file *stream,
563 int recurse,
564 const struct value_print_options *options)
566 struct value_print_options opts;
568 if (val->entirely_optimized_out ())
570 val_print_optimized_out (val, stream);
571 return;
574 struct type *real_type = check_typedef (type);
575 if (real_type->code () == TYPE_CODE_STRUCT)
577 CORE_ADDR *first_dont_print;
578 CORE_ADDR addr = val->address ();
579 int i;
581 first_dont_print
582 = (CORE_ADDR *) obstack_base (&dont_print_statmem_obstack);
583 i = obstack_object_size (&dont_print_statmem_obstack)
584 / sizeof (CORE_ADDR);
586 while (--i >= 0)
588 if (addr == first_dont_print[i])
590 fputs_styled (_("<same as static member of an already"
591 " seen type>"),
592 metadata_style.style (), stream);
593 return;
597 obstack_grow (&dont_print_statmem_obstack, (char *) &addr,
598 sizeof (CORE_ADDR));
599 cp_print_value_fields_pp (val, stream, recurse, options, nullptr, 1);
600 return;
603 if (real_type->code () == TYPE_CODE_ARRAY)
605 struct type **first_dont_print;
606 int i;
607 struct type *target_type = type->target_type ();
609 first_dont_print
610 = (struct type **) obstack_base (&dont_print_stat_array_obstack);
611 i = obstack_object_size (&dont_print_stat_array_obstack)
612 / sizeof (struct type *);
614 while (--i >= 0)
616 if (target_type == first_dont_print[i])
618 fputs_styled (_("<same as static member of an already"
619 " seen type>"),
620 metadata_style.style (), stream);
621 return;
625 obstack_grow (&dont_print_stat_array_obstack,
626 (char *) &target_type,
627 sizeof (struct type *));
630 opts = *options;
631 opts.deref_ref = false;
632 common_val_print (val, stream, recurse, &opts, current_language);
635 /* Find the field in *SELF, or its non-virtual base classes, with
636 bit offset OFFSET. Set *SELF to the containing type and *FIELDNO
637 to the containing field number. If OFFSET is not exactly at the
638 start of some field, set *SELF to NULL. */
640 static void
641 cp_find_class_member (struct type **self_p, int *fieldno,
642 LONGEST offset)
644 struct type *self;
645 unsigned int i;
646 unsigned len;
648 *self_p = check_typedef (*self_p);
649 self = *self_p;
650 len = self->num_fields ();
652 for (i = TYPE_N_BASECLASSES (self); i < len; i++)
654 field &f = self->field (i);
655 if (f.is_static ())
656 continue;
657 LONGEST bitpos = f.loc_bitpos ();
659 QUIT;
660 if (offset == bitpos)
662 *fieldno = i;
663 return;
667 for (i = 0; i < TYPE_N_BASECLASSES (self); i++)
669 LONGEST bitpos = self->field (i).loc_bitpos ();
670 LONGEST bitsize = 8 * self->field (i).type ()->length ();
672 if (offset >= bitpos && offset < bitpos + bitsize)
674 *self_p = self->field (i).type ();
675 cp_find_class_member (self_p, fieldno, offset - bitpos);
676 return;
680 *self_p = NULL;
683 void
684 cp_print_class_member (const gdb_byte *valaddr, struct type *type,
685 struct ui_file *stream, const char *prefix)
687 enum bfd_endian byte_order = type_byte_order (type);
689 /* VAL is a byte offset into the structure type SELF_TYPE.
690 Find the name of the field for that offset and
691 print it. */
692 struct type *self_type = TYPE_SELF_TYPE (type);
693 LONGEST val;
694 int fieldno;
696 val = extract_signed_integer (valaddr,
697 type->length (),
698 byte_order);
700 /* Pointers to data members are usually byte offsets into an object.
701 Because a data member can have offset zero, and a NULL pointer to
702 member must be distinct from any valid non-NULL pointer to
703 member, either the value is biased or the NULL value has a
704 special representation; both are permitted by ISO C++. HP aCC
705 used a bias of 0x20000000; HP cfront used a bias of 1; g++ 3.x
706 and other compilers which use the Itanium ABI use -1 as the NULL
707 value. GDB only supports that last form; to add support for
708 another form, make this into a cp-abi hook. */
710 if (val == -1)
712 gdb_printf (stream, "NULL");
713 return;
716 cp_find_class_member (&self_type, &fieldno, val << 3);
718 if (self_type != NULL)
720 const char *name;
722 gdb_puts (prefix, stream);
723 name = self_type->name ();
724 if (name)
725 gdb_puts (name, stream);
726 else
727 c_type_print_base (self_type, stream, 0, 0, &type_print_raw_options);
728 gdb_printf (stream, "::");
729 fputs_styled (self_type->field (fieldno).name (),
730 variable_name_style.style (), stream);
732 else
733 gdb_printf (stream, "%ld", (long) val);
736 #if GDB_SELF_TEST
738 /* Test printing of TYPE_CODE_STRUCT values. */
740 static void
741 test_print_fields (gdbarch *arch)
743 struct field *f;
744 type *uint8_type = builtin_type (arch)->builtin_uint8;
745 type *bool_type = builtin_type (arch)->builtin_bool;
746 type *the_struct = arch_composite_type (arch, NULL, TYPE_CODE_STRUCT);
747 the_struct->set_length (4);
749 /* Value: 1110 1001
750 Fields: C-BB B-A- */
751 if (gdbarch_byte_order (arch) == BFD_ENDIAN_LITTLE)
753 f = append_composite_type_field_raw (the_struct, "A", bool_type);
754 f->set_loc_bitpos (1);
755 f->set_bitsize (1);
756 f = append_composite_type_field_raw (the_struct, "B", uint8_type);
757 f->set_loc_bitpos (3);
758 f->set_bitsize (3);
759 f = append_composite_type_field_raw (the_struct, "C", bool_type);
760 f->set_loc_bitpos (7);
761 f->set_bitsize (1);
763 /* According to the logic commented in "make_gdb_type_struct ()" of
764 * target-descriptions.c, bit positions are numbered differently for
765 * little and big endians. */
766 else
768 f = append_composite_type_field_raw (the_struct, "A", bool_type);
769 f->set_loc_bitpos (30);
770 f->set_bitsize (1);
771 f = append_composite_type_field_raw (the_struct, "B", uint8_type);
772 f->set_loc_bitpos (26);
773 f->set_bitsize (3);
774 f = append_composite_type_field_raw (the_struct, "C", bool_type);
775 f->set_loc_bitpos (24);
776 f->set_bitsize (1);
779 value *val = value::allocate (the_struct);
780 gdb_byte *contents = val->contents_writeable ().data ();
781 store_unsigned_integer (contents, val->enclosing_type ()->length (),
782 gdbarch_byte_order (arch), 0xe9);
784 string_file out;
785 struct value_print_options opts;
786 get_no_prettyformat_print_options (&opts);
787 cp_print_value_fields(val, &out, 0, &opts, NULL, 0);
788 SELF_CHECK (out.string () == "{A = false, B = 5, C = true}");
790 out.clear();
791 opts.format = 'x';
792 cp_print_value_fields(val, &out, 0, &opts, NULL, 0);
793 SELF_CHECK (out.string () == "{A = 0x0, B = 0x5, C = 0x1}");
796 #endif
799 void _initialize_cp_valprint ();
800 void
801 _initialize_cp_valprint ()
803 #if GDB_SELF_TEST
804 selftests::register_test_foreach_arch ("print-fields", test_print_fields);
805 #endif
807 obstack_begin (&dont_print_stat_array_obstack,
808 32 * sizeof (struct type *));
809 obstack_begin (&dont_print_statmem_obstack,
810 32 * sizeof (CORE_ADDR));
811 obstack_begin (&dont_print_vb_obstack,
812 32 * sizeof (struct type *));