Automatic date update in version.in
[binutils-gdb.git] / gdb / cp-valprint.c
blobf2a2ca5288dac6b7064ba42cdbd186d6031c6423
1 /* Support for printing C++ values for GDB, the GNU debugger.
3 Copyright (C) 1986-2024 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 "gdbsupport/gdb_obstack.h"
21 #include "symtab.h"
22 #include "gdbtypes.h"
23 #include "expression.h"
24 #include "value.h"
25 #include "command.h"
26 #include "gdbcmd.h"
27 #include "demangle.h"
28 #include "annotate.h"
29 #include "c-lang.h"
30 #include "target.h"
31 #include "cp-abi.h"
32 #include "valprint.h"
33 #include "cp-support.h"
34 #include "language.h"
35 #include "extension.h"
36 #include "typeprint.h"
37 #include "gdbsupport/byte-vector.h"
38 #include "gdbarch.h"
39 #include "cli/cli-style.h"
40 #include "gdbsupport/selftest.h"
41 #include "selftest-arch.h"
43 static struct obstack dont_print_vb_obstack;
44 static struct obstack dont_print_statmem_obstack;
45 static struct obstack dont_print_stat_array_obstack;
47 static void cp_print_static_field (struct type *, struct value *,
48 struct ui_file *, int,
49 const struct value_print_options *);
51 static void cp_print_value (struct value *, struct ui_file *,
52 int, const struct value_print_options *,
53 struct type **);
56 /* GCC versions after 2.4.5 use this. */
57 const char vtbl_ptr_name[] = "__vtbl_ptr_type";
59 /* Return truth value for assertion that TYPE is of the type
60 "pointer to virtual function". */
62 int
63 cp_is_vtbl_ptr_type (struct type *type)
65 const char *type_name = type->name ();
67 return (type_name != NULL && !strcmp (type_name, vtbl_ptr_name));
70 /* Return truth value for the assertion that TYPE is of the type
71 "pointer to virtual function table". */
73 int
74 cp_is_vtbl_member (struct type *type)
76 /* With older versions of g++, the vtbl field pointed to an array of
77 structures. Nowadays it points directly to the structure. */
78 if (type->code () == TYPE_CODE_PTR)
80 type = type->target_type ();
81 if (type->code () == TYPE_CODE_ARRAY)
83 type = type->target_type ();
84 if (type->code () == TYPE_CODE_STRUCT /* if not using thunks */
85 || type->code () == TYPE_CODE_PTR) /* if using thunks */
87 /* Virtual functions tables are full of pointers
88 to virtual functions. */
89 return cp_is_vtbl_ptr_type (type);
92 else if (type->code () == TYPE_CODE_STRUCT) /* if not using thunks */
94 return cp_is_vtbl_ptr_type (type);
96 else if (type->code () == TYPE_CODE_PTR) /* if using thunks */
98 /* The type name of the thunk pointer is NULL when using
99 dwarf2. We could test for a pointer to a function, but
100 there is no type info for the virtual table either, so it
101 wont help. */
102 return cp_is_vtbl_ptr_type (type);
105 return 0;
108 /* Mutually recursive subroutines of cp_print_value and c_val_print to
109 print out a structure's fields: cp_print_value_fields and
110 cp_print_value.
112 TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and OPTIONS have the same
113 meanings as in cp_print_value and c_val_print.
115 2nd argument REAL_TYPE is used to carry over the type of the
116 derived class across the recursion to base classes.
118 DONT_PRINT is an array of baseclass types that we should not print,
119 or zero if called from top level. */
121 void
122 cp_print_value_fields (struct value *val, struct ui_file *stream,
123 int recurse, const struct value_print_options *options,
124 struct type **dont_print_vb,
125 int dont_print_statmem)
127 int i, len, n_baseclasses;
128 int fields_seen = 0;
129 static int last_set_recurse = -1;
131 struct type *type = check_typedef (val->type ());
133 if (recurse == 0)
135 /* Any object can be left on obstacks only during an unexpected
136 error. */
138 if (obstack_object_size (&dont_print_statmem_obstack) > 0)
140 obstack_free (&dont_print_statmem_obstack, NULL);
141 obstack_begin (&dont_print_statmem_obstack,
142 32 * sizeof (CORE_ADDR));
144 if (obstack_object_size (&dont_print_stat_array_obstack) > 0)
146 obstack_free (&dont_print_stat_array_obstack, NULL);
147 obstack_begin (&dont_print_stat_array_obstack,
148 32 * sizeof (struct type *));
152 gdb_printf (stream, "{");
153 len = type->num_fields ();
154 n_baseclasses = TYPE_N_BASECLASSES (type);
156 /* First, print out baseclasses such that we don't print
157 duplicates of virtual baseclasses. */
159 if (n_baseclasses > 0)
160 cp_print_value (val, stream, recurse + 1, options, dont_print_vb);
162 /* Second, print out data fields */
164 /* If there are no data fields, skip this part */
165 if (len == n_baseclasses || !len)
166 fprintf_styled (stream, metadata_style.style (), "<No data fields>");
167 else
169 size_t statmem_obstack_initial_size = 0;
170 size_t stat_array_obstack_initial_size = 0;
171 struct type *vptr_basetype = NULL;
172 int vptr_fieldno;
174 if (dont_print_statmem == 0)
176 statmem_obstack_initial_size =
177 obstack_object_size (&dont_print_statmem_obstack);
179 if (last_set_recurse != recurse)
181 stat_array_obstack_initial_size =
182 obstack_object_size (&dont_print_stat_array_obstack);
184 last_set_recurse = recurse;
188 vptr_fieldno = get_vptr_fieldno (type, &vptr_basetype);
189 for (i = n_baseclasses; i < len; i++)
191 const gdb_byte *valaddr = val->contents_for_printing ().data ();
193 /* If requested, skip printing of static fields. */
194 if (!options->static_field_print
195 && type->field (i).is_static ())
196 continue;
198 if (fields_seen)
200 gdb_puts (",", stream);
201 if (!options->prettyformat)
202 gdb_puts (" ", stream);
204 else if (n_baseclasses > 0)
206 if (options->prettyformat)
208 gdb_printf (stream, "\n");
209 print_spaces (2 + 2 * recurse, stream);
210 gdb_puts ("members of ", stream);
211 gdb_puts (type->name (), stream);
212 gdb_puts (":", stream);
215 fields_seen = 1;
217 if (options->prettyformat)
219 gdb_printf (stream, "\n");
220 print_spaces (2 + 2 * recurse, stream);
222 else
224 stream->wrap_here (2 + 2 * recurse);
227 annotate_field_begin (type->field (i).type ());
229 if (type->field (i).is_static ())
231 gdb_puts ("static ", stream);
232 fprintf_symbol (stream,
233 type->field (i).name (),
234 current_language->la_language,
235 DMGL_PARAMS | DMGL_ANSI);
237 else
238 fputs_styled (type->field (i).name (),
239 variable_name_style.style (), stream);
240 annotate_field_name_end ();
242 /* We tweak various options in a few cases below. */
243 value_print_options options_copy = *options;
244 value_print_options *opts = &options_copy;
246 /* Do not print leading '=' in case of anonymous
247 unions. */
248 if (strcmp (type->field (i).name (), ""))
249 gdb_puts (" = ", stream);
250 else
252 /* If this is an anonymous field then we want to consider it
253 as though it is at its parent's depth when it comes to the
254 max print depth. */
255 if (opts->max_depth != -1 && opts->max_depth < INT_MAX)
256 ++opts->max_depth;
258 annotate_field_value ();
260 if (!type->field (i).is_static ()
261 && type->field (i).is_packed ())
263 struct value *v;
265 /* Bitfields require special handling, especially due to
266 byte order problems. */
267 if (type->field (i).is_ignored ())
269 fputs_styled ("<optimized out or zero length>",
270 metadata_style.style (), stream);
272 else if (val->bits_synthetic_pointer
273 (type->field (i).loc_bitpos (),
274 type->field (i).bitsize ()))
276 fputs_styled (_("<synthetic pointer>"),
277 metadata_style.style (), stream);
279 else
281 opts->deref_ref = false;
283 v = value_field_bitfield (type, i, valaddr,
284 val->embedded_offset (), val);
286 common_val_print (v, stream, recurse + 1,
287 opts, current_language);
290 else
292 if (type->field (i).is_ignored ())
294 fputs_styled ("<optimized out or zero length>",
295 metadata_style.style (), stream);
297 else if (type->field (i).is_static ())
301 struct value *v = value_static_field (type, i);
303 cp_print_static_field (type->field (i).type (),
304 v, stream, recurse + 1,
305 opts);
307 catch (const gdb_exception_error &ex)
309 fprintf_styled (stream, metadata_style.style (),
310 _("<error reading variable: %s>"),
311 ex.what ());
314 else if (i == vptr_fieldno && type == vptr_basetype)
316 int i_offset = type->field (i).loc_bitpos () / 8;
317 struct type *i_type = type->field (i).type ();
319 if (valprint_check_validity (stream, i_type, i_offset, val))
321 CORE_ADDR addr;
323 i_offset += val->embedded_offset ();
324 addr = extract_typed_address (valaddr + i_offset, i_type);
325 print_function_pointer_address (opts,
326 type->arch (),
327 addr, stream);
330 else
332 struct value *v = val->primitive_field (0, i, type);
333 opts->deref_ref = false;
334 common_val_print (v, stream, recurse + 1, opts,
335 current_language);
338 annotate_field_end ();
341 if (dont_print_statmem == 0)
343 size_t obstack_final_size =
344 obstack_object_size (&dont_print_statmem_obstack);
346 if (obstack_final_size > statmem_obstack_initial_size)
348 /* In effect, a pop of the printed-statics stack. */
349 size_t shrink_bytes
350 = statmem_obstack_initial_size - obstack_final_size;
351 obstack_blank_fast (&dont_print_statmem_obstack, shrink_bytes);
354 if (last_set_recurse != recurse)
356 obstack_final_size =
357 obstack_object_size (&dont_print_stat_array_obstack);
359 if (obstack_final_size > stat_array_obstack_initial_size)
361 void *free_to_ptr =
362 (char *) obstack_next_free (&dont_print_stat_array_obstack)
363 - (obstack_final_size
364 - stat_array_obstack_initial_size);
366 obstack_free (&dont_print_stat_array_obstack,
367 free_to_ptr);
369 last_set_recurse = -1;
373 if (options->prettyformat)
375 gdb_printf (stream, "\n");
376 print_spaces (2 * recurse, stream);
378 } /* if there are data fields */
380 gdb_printf (stream, "}");
383 /* A wrapper for cp_print_value_fields that tries to apply a
384 pretty-printer first. */
386 static void
387 cp_print_value_fields_pp (struct value *val,
388 struct ui_file *stream,
389 int recurse,
390 const struct value_print_options *options,
391 struct type **dont_print_vb,
392 int dont_print_statmem)
394 int result = 0;
396 /* Attempt to run an extension language pretty-printer if
397 possible. */
398 if (!options->raw)
399 result
400 = apply_ext_lang_val_pretty_printer (val, stream,
401 recurse, options,
402 current_language);
404 if (!result)
405 cp_print_value_fields (val, stream, recurse, options, dont_print_vb,
406 dont_print_statmem);
409 /* Special val_print routine to avoid printing multiple copies of
410 virtual baseclasses. */
412 static void
413 cp_print_value (struct value *val, struct ui_file *stream,
414 int recurse, const struct value_print_options *options,
415 struct type **dont_print_vb)
417 struct type *type = check_typedef (val->type ());
418 CORE_ADDR address = val->address ();
419 struct type **last_dont_print
420 = (struct type **) obstack_next_free (&dont_print_vb_obstack);
421 struct obstack tmp_obstack = dont_print_vb_obstack;
422 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
423 const gdb_byte *valaddr = val->contents_for_printing ().data ();
425 if (dont_print_vb == 0)
427 /* If we're at top level, carve out a completely fresh chunk of
428 the obstack and use that until this particular invocation
429 returns. */
430 /* Bump up the high-water mark. Now alpha is omega. */
431 obstack_finish (&dont_print_vb_obstack);
434 for (i = 0; i < n_baseclasses; i++)
436 LONGEST boffset = 0;
437 int skip = 0;
438 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
439 const char *basename = baseclass->name ();
440 struct value *base_val = NULL;
442 if (BASETYPE_VIA_VIRTUAL (type, i))
444 struct type **first_dont_print
445 = (struct type **) obstack_base (&dont_print_vb_obstack);
447 int j = (struct type **)
448 obstack_next_free (&dont_print_vb_obstack) - first_dont_print;
450 while (--j >= 0)
451 if (baseclass == first_dont_print[j])
452 goto flush_it;
454 obstack_ptr_grow (&dont_print_vb_obstack, baseclass);
459 boffset = baseclass_offset (type, i, valaddr,
460 val->embedded_offset (),
461 address, val);
463 catch (const gdb_exception_error &ex)
465 if (ex.error == NOT_AVAILABLE_ERROR)
466 skip = -1;
467 else
468 skip = 1;
471 if (skip == 0)
473 if (BASETYPE_VIA_VIRTUAL (type, i))
475 /* The virtual base class pointer might have been
476 clobbered by the user program. Make sure that it
477 still points to a valid memory location. */
479 if (boffset < 0 || boffset >= type->length ())
481 gdb::byte_vector buf (baseclass->length ());
483 if (target_read_memory (address + boffset, buf.data (),
484 baseclass->length ()) != 0)
485 skip = 1;
486 base_val = value_from_contents_and_address (baseclass,
487 buf.data (),
488 address + boffset);
489 baseclass = base_val->type ();
490 boffset = 0;
492 else
494 base_val = val;
497 else
499 base_val = val;
503 /* Now do the printing. */
504 if (options->prettyformat)
506 gdb_printf (stream, "\n");
507 print_spaces (2 * recurse, stream);
509 gdb_puts ("<", stream);
510 /* Not sure what the best notation is in the case where there is
511 no baseclass name. */
512 gdb_puts (basename ? basename : "", stream);
513 gdb_puts ("> = ", stream);
515 if (skip < 0)
516 val_print_unavailable (stream);
517 else if (skip > 0)
518 val_print_invalid_address (stream);
519 else
521 if (!val_print_check_max_depth (stream, recurse, options,
522 current_language))
524 struct value *baseclass_val = val->primitive_field (0,
525 i, type);
527 cp_print_value_fields_pp
528 (baseclass_val, stream, recurse, options,
529 (struct type **) obstack_base (&dont_print_vb_obstack),
533 gdb_puts (", ", stream);
535 flush_it:
539 if (dont_print_vb == 0)
541 /* Free the space used to deal with the printing
542 of this type from top level. */
543 obstack_free (&dont_print_vb_obstack, last_dont_print);
544 /* Reset watermark so that we can continue protecting
545 ourselves from whatever we were protecting ourselves. */
546 dont_print_vb_obstack = tmp_obstack;
550 /* Print value of a static member. To avoid infinite recursion when
551 printing a class that contains a static instance of the class, we
552 keep the addresses of all printed static member classes in an
553 obstack and refuse to print them more than once.
555 VAL contains the value to print, TYPE, STREAM, RECURSE, and OPTIONS
556 have the same meanings as in c_val_print. */
558 static void
559 cp_print_static_field (struct type *type,
560 struct value *val,
561 struct ui_file *stream,
562 int recurse,
563 const struct value_print_options *options)
565 struct value_print_options opts;
567 if (val->entirely_optimized_out ())
569 val_print_optimized_out (val, stream);
570 return;
573 struct type *real_type = check_typedef (type);
574 if (real_type->code () == TYPE_CODE_STRUCT)
576 CORE_ADDR *first_dont_print;
577 CORE_ADDR addr = val->address ();
578 int i;
580 first_dont_print
581 = (CORE_ADDR *) obstack_base (&dont_print_statmem_obstack);
582 i = obstack_object_size (&dont_print_statmem_obstack)
583 / sizeof (CORE_ADDR);
585 while (--i >= 0)
587 if (addr == first_dont_print[i])
589 fputs_styled (_("<same as static member of an already"
590 " seen type>"),
591 metadata_style.style (), stream);
592 return;
596 obstack_grow (&dont_print_statmem_obstack, (char *) &addr,
597 sizeof (CORE_ADDR));
598 cp_print_value_fields_pp (val, stream, recurse, options, nullptr, 1);
599 return;
602 if (real_type->code () == TYPE_CODE_ARRAY)
604 struct type **first_dont_print;
605 int i;
606 struct type *target_type = type->target_type ();
608 first_dont_print
609 = (struct type **) obstack_base (&dont_print_stat_array_obstack);
610 i = obstack_object_size (&dont_print_stat_array_obstack)
611 / sizeof (struct type *);
613 while (--i >= 0)
615 if (target_type == first_dont_print[i])
617 fputs_styled (_("<same as static member of an already"
618 " seen type>"),
619 metadata_style.style (), stream);
620 return;
624 obstack_grow (&dont_print_stat_array_obstack,
625 (char *) &target_type,
626 sizeof (struct type *));
629 opts = *options;
630 opts.deref_ref = false;
631 common_val_print (val, stream, recurse, &opts, current_language);
634 /* Find the field in *SELF, or its non-virtual base classes, with
635 bit offset OFFSET. Set *SELF to the containing type and *FIELDNO
636 to the containing field number. If OFFSET is not exactly at the
637 start of some field, set *SELF to NULL. */
639 static void
640 cp_find_class_member (struct type **self_p, int *fieldno,
641 LONGEST offset)
643 struct type *self;
644 unsigned int i;
645 unsigned len;
647 *self_p = check_typedef (*self_p);
648 self = *self_p;
649 len = self->num_fields ();
651 for (i = TYPE_N_BASECLASSES (self); i < len; i++)
653 field &f = self->field (i);
654 if (f.is_static ())
655 continue;
656 LONGEST bitpos = f.loc_bitpos ();
658 QUIT;
659 if (offset == bitpos)
661 *fieldno = i;
662 return;
666 for (i = 0; i < TYPE_N_BASECLASSES (self); i++)
668 LONGEST bitpos = self->field (i).loc_bitpos ();
669 LONGEST bitsize = 8 * self->field (i).type ()->length ();
671 if (offset >= bitpos && offset < bitpos + bitsize)
673 *self_p = self->field (i).type ();
674 cp_find_class_member (self_p, fieldno, offset - bitpos);
675 return;
679 *self_p = NULL;
682 void
683 cp_print_class_member (const gdb_byte *valaddr, struct type *type,
684 struct ui_file *stream, const char *prefix)
686 enum bfd_endian byte_order = type_byte_order (type);
688 /* VAL is a byte offset into the structure type SELF_TYPE.
689 Find the name of the field for that offset and
690 print it. */
691 struct type *self_type = TYPE_SELF_TYPE (type);
692 LONGEST val;
693 int fieldno;
695 val = extract_signed_integer (valaddr,
696 type->length (),
697 byte_order);
699 /* Pointers to data members are usually byte offsets into an object.
700 Because a data member can have offset zero, and a NULL pointer to
701 member must be distinct from any valid non-NULL pointer to
702 member, either the value is biased or the NULL value has a
703 special representation; both are permitted by ISO C++. HP aCC
704 used a bias of 0x20000000; HP cfront used a bias of 1; g++ 3.x
705 and other compilers which use the Itanium ABI use -1 as the NULL
706 value. GDB only supports that last form; to add support for
707 another form, make this into a cp-abi hook. */
709 if (val == -1)
711 gdb_printf (stream, "NULL");
712 return;
715 cp_find_class_member (&self_type, &fieldno, val << 3);
717 if (self_type != NULL)
719 const char *name;
721 gdb_puts (prefix, stream);
722 name = self_type->name ();
723 if (name)
724 gdb_puts (name, stream);
725 else
726 c_type_print_base (self_type, stream, 0, 0, &type_print_raw_options);
727 gdb_printf (stream, "::");
728 fputs_styled (self_type->field (fieldno).name (),
729 variable_name_style.style (), stream);
731 else
732 gdb_printf (stream, "%ld", (long) val);
735 #if GDB_SELF_TEST
737 /* Test printing of TYPE_CODE_STRUCT values. */
739 static void
740 test_print_fields (gdbarch *arch)
742 struct field *f;
743 type *uint8_type = builtin_type (arch)->builtin_uint8;
744 type *bool_type = builtin_type (arch)->builtin_bool;
745 type *the_struct = arch_composite_type (arch, NULL, TYPE_CODE_STRUCT);
746 the_struct->set_length (4);
748 /* Value: 1110 1001
749 Fields: C-BB B-A- */
750 if (gdbarch_byte_order (arch) == BFD_ENDIAN_LITTLE)
752 f = append_composite_type_field_raw (the_struct, "A", bool_type);
753 f->set_loc_bitpos (1);
754 f->set_bitsize (1);
755 f = append_composite_type_field_raw (the_struct, "B", uint8_type);
756 f->set_loc_bitpos (3);
757 f->set_bitsize (3);
758 f = append_composite_type_field_raw (the_struct, "C", bool_type);
759 f->set_loc_bitpos (7);
760 f->set_bitsize (1);
762 /* According to the logic commented in "make_gdb_type_struct ()" of
763 * target-descriptions.c, bit positions are numbered differently for
764 * little and big endians. */
765 else
767 f = append_composite_type_field_raw (the_struct, "A", bool_type);
768 f->set_loc_bitpos (30);
769 f->set_bitsize (1);
770 f = append_composite_type_field_raw (the_struct, "B", uint8_type);
771 f->set_loc_bitpos (26);
772 f->set_bitsize (3);
773 f = append_composite_type_field_raw (the_struct, "C", bool_type);
774 f->set_loc_bitpos (24);
775 f->set_bitsize (1);
778 value *val = value::allocate (the_struct);
779 gdb_byte *contents = val->contents_writeable ().data ();
780 store_unsigned_integer (contents, val->enclosing_type ()->length (),
781 gdbarch_byte_order (arch), 0xe9);
783 string_file out;
784 struct value_print_options opts;
785 get_no_prettyformat_print_options (&opts);
786 cp_print_value_fields(val, &out, 0, &opts, NULL, 0);
787 SELF_CHECK (out.string () == "{A = false, B = 5, C = true}");
789 out.clear();
790 opts.format = 'x';
791 cp_print_value_fields(val, &out, 0, &opts, NULL, 0);
792 SELF_CHECK (out.string () == "{A = 0x0, B = 0x5, C = 0x1}");
795 #endif
798 void _initialize_cp_valprint ();
799 void
800 _initialize_cp_valprint ()
802 #if GDB_SELF_TEST
803 selftests::register_test_foreach_arch ("print-fields", test_print_fields);
804 #endif
806 obstack_begin (&dont_print_stat_array_obstack,
807 32 * sizeof (struct type *));
808 obstack_begin (&dont_print_statmem_obstack,
809 32 * sizeof (CORE_ADDR));
810 obstack_begin (&dont_print_vb_obstack,
811 32 * sizeof (struct type *));