Simplify DAP make_source callers
[binutils-gdb.git] / gdb / cp-valprint.c
blob2634c1e33a667ef7d9ecc48bcdad5262fcd54c13
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 "event-top.h"
21 #include "extract-store-integer.h"
22 #include "gdbsupport/gdb_obstack.h"
23 #include "symtab.h"
24 #include "gdbtypes.h"
25 #include "expression.h"
26 #include "value.h"
27 #include "command.h"
28 #include "cli/cli-cmds.h"
29 #include "demangle.h"
30 #include "annotate.h"
31 #include "c-lang.h"
32 #include "target.h"
33 #include "cp-abi.h"
34 #include "valprint.h"
35 #include "cp-support.h"
36 #include "language.h"
37 #include "extension.h"
38 #include "typeprint.h"
39 #include "gdbsupport/byte-vector.h"
40 #include "gdbarch.h"
41 #include "cli/cli-style.h"
42 #include "gdbsupport/selftest.h"
43 #include "selftest-arch.h"
45 static struct obstack dont_print_vb_obstack;
46 static struct obstack dont_print_statmem_obstack;
47 static struct obstack dont_print_stat_array_obstack;
49 static void cp_print_static_field (struct type *, struct value *,
50 struct ui_file *, int,
51 const struct value_print_options *);
53 static void cp_print_value (struct value *, struct ui_file *,
54 int, const struct value_print_options *,
55 struct type **);
58 /* GCC versions after 2.4.5 use this. */
59 const char vtbl_ptr_name[] = "__vtbl_ptr_type";
61 /* Return truth value for assertion that TYPE is of the type
62 "pointer to virtual function". */
64 int
65 cp_is_vtbl_ptr_type (struct type *type)
67 const char *type_name = type->name ();
69 return (type_name != NULL && !strcmp (type_name, vtbl_ptr_name));
72 /* Return truth value for the assertion that TYPE is of the type
73 "pointer to virtual function table". */
75 int
76 cp_is_vtbl_member (struct type *type)
78 /* With older versions of g++, the vtbl field pointed to an array of
79 structures. Nowadays it points directly to the structure. */
80 if (type->code () == TYPE_CODE_PTR)
82 type = type->target_type ();
83 if (type->code () == TYPE_CODE_ARRAY)
85 type = type->target_type ();
86 if (type->code () == TYPE_CODE_STRUCT /* if not using thunks */
87 || type->code () == TYPE_CODE_PTR) /* if using thunks */
89 /* Virtual functions tables are full of pointers
90 to virtual functions. */
91 return cp_is_vtbl_ptr_type (type);
94 else if (type->code () == TYPE_CODE_STRUCT) /* if not using thunks */
96 return cp_is_vtbl_ptr_type (type);
98 else if (type->code () == TYPE_CODE_PTR) /* if using thunks */
100 /* The type name of the thunk pointer is NULL when using
101 dwarf2. We could test for a pointer to a function, but
102 there is no type info for the virtual table either, so it
103 wont help. */
104 return cp_is_vtbl_ptr_type (type);
107 return 0;
110 /* Mutually recursive subroutines of cp_print_value and c_val_print to
111 print out a structure's fields: cp_print_value_fields and
112 cp_print_value.
114 TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and OPTIONS have the same
115 meanings as in cp_print_value and c_val_print.
117 2nd argument REAL_TYPE is used to carry over the type of the
118 derived class across the recursion to base classes.
120 DONT_PRINT is an array of baseclass types that we should not print,
121 or zero if called from top level. */
123 void
124 cp_print_value_fields (struct value *val, struct ui_file *stream,
125 int recurse, const struct value_print_options *options,
126 struct type **dont_print_vb,
127 int dont_print_statmem)
129 int i, len, n_baseclasses;
130 int fields_seen = 0;
131 static int last_set_recurse = -1;
133 struct type *type = check_typedef (val->type ());
135 if (recurse == 0)
137 /* Any object can be left on obstacks only during an unexpected
138 error. */
140 if (obstack_object_size (&dont_print_statmem_obstack) > 0)
142 obstack_free (&dont_print_statmem_obstack, NULL);
143 obstack_begin (&dont_print_statmem_obstack,
144 32 * sizeof (CORE_ADDR));
146 if (obstack_object_size (&dont_print_stat_array_obstack) > 0)
148 obstack_free (&dont_print_stat_array_obstack, NULL);
149 obstack_begin (&dont_print_stat_array_obstack,
150 32 * sizeof (struct type *));
154 gdb_printf (stream, "{");
155 len = type->num_fields ();
156 n_baseclasses = TYPE_N_BASECLASSES (type);
158 /* First, print out baseclasses such that we don't print
159 duplicates of virtual baseclasses. */
161 if (n_baseclasses > 0)
162 cp_print_value (val, stream, recurse + 1, options, dont_print_vb);
164 /* Second, print out data fields */
166 /* If there are no data fields, skip this part */
167 if (len == n_baseclasses || !len)
168 fprintf_styled (stream, metadata_style.style (), "<No data fields>");
169 else
171 size_t statmem_obstack_initial_size = 0;
172 size_t stat_array_obstack_initial_size = 0;
173 struct type *vptr_basetype = NULL;
174 int vptr_fieldno;
176 if (dont_print_statmem == 0)
178 statmem_obstack_initial_size =
179 obstack_object_size (&dont_print_statmem_obstack);
181 if (last_set_recurse != recurse)
183 stat_array_obstack_initial_size =
184 obstack_object_size (&dont_print_stat_array_obstack);
186 last_set_recurse = recurse;
190 vptr_fieldno = get_vptr_fieldno (type, &vptr_basetype);
191 for (i = n_baseclasses; i < len; i++)
193 const gdb_byte *valaddr = val->contents_for_printing ().data ();
195 /* If requested, skip printing of static fields. */
196 if (!options->static_field_print
197 && type->field (i).is_static ())
198 continue;
200 if (fields_seen)
202 gdb_puts (",", stream);
203 if (!options->prettyformat)
204 gdb_puts (" ", stream);
206 else if (n_baseclasses > 0)
208 if (options->prettyformat)
210 gdb_printf (stream, "\n");
211 print_spaces (2 + 2 * recurse, stream);
212 gdb_puts ("members of ", stream);
213 gdb_puts (type->name (), stream);
214 gdb_puts (":", stream);
217 fields_seen = 1;
219 if (options->prettyformat)
221 gdb_printf (stream, "\n");
222 print_spaces (2 + 2 * recurse, stream);
224 else
226 stream->wrap_here (2 + 2 * recurse);
229 annotate_field_begin (type->field (i).type ());
231 if (type->field (i).is_static ())
233 gdb_puts ("static ", stream);
234 fprintf_symbol (stream,
235 type->field (i).name (),
236 current_language->la_language,
237 DMGL_PARAMS | DMGL_ANSI);
239 else
240 fputs_styled (type->field (i).name (),
241 variable_name_style.style (), stream);
242 annotate_field_name_end ();
244 /* We tweak various options in a few cases below. */
245 value_print_options options_copy = *options;
246 value_print_options *opts = &options_copy;
248 /* Do not print leading '=' in case of anonymous
249 unions. */
250 if (strcmp (type->field (i).name (), ""))
251 gdb_puts (" = ", stream);
252 else
254 /* If this is an anonymous field then we want to consider it
255 as though it is at its parent's depth when it comes to the
256 max print depth. */
257 if (opts->max_depth != -1 && opts->max_depth < INT_MAX)
258 ++opts->max_depth;
260 annotate_field_value ();
262 if (!type->field (i).is_static ()
263 && type->field (i).is_packed ())
265 struct value *v;
267 /* Bitfields require special handling, especially due to
268 byte order problems. */
269 if (type->field (i).is_ignored ())
271 fputs_styled ("<optimized out or zero length>",
272 metadata_style.style (), stream);
274 else if (val->bits_synthetic_pointer
275 (type->field (i).loc_bitpos (),
276 type->field (i).bitsize ()))
278 fputs_styled (_("<synthetic pointer>"),
279 metadata_style.style (), stream);
281 else
283 opts->deref_ref = false;
285 v = value_field_bitfield (type, i, valaddr,
286 val->embedded_offset (), val);
288 common_val_print (v, stream, recurse + 1,
289 opts, current_language);
292 else
294 if (type->field (i).is_ignored ())
296 fputs_styled ("<optimized out or zero length>",
297 metadata_style.style (), stream);
299 else if (type->field (i).is_static ())
303 struct value *v = value_static_field (type, i);
305 cp_print_static_field (type->field (i).type (),
306 v, stream, recurse + 1,
307 opts);
309 catch (const gdb_exception_error &ex)
311 fprintf_styled (stream, metadata_style.style (),
312 _("<error reading variable: %s>"),
313 ex.what ());
316 else if (i == vptr_fieldno && type == vptr_basetype)
318 int i_offset = type->field (i).loc_bitpos () / 8;
319 struct type *i_type = type->field (i).type ();
321 if (valprint_check_validity (stream, i_type, i_offset, val))
323 CORE_ADDR addr;
325 i_offset += val->embedded_offset ();
326 addr = extract_typed_address (valaddr + i_offset, i_type);
327 print_function_pointer_address (opts,
328 type->arch (),
329 addr, stream);
332 else
334 struct value *v = val->primitive_field (0, i, type);
335 opts->deref_ref = false;
336 common_val_print (v, stream, recurse + 1, opts,
337 current_language);
340 annotate_field_end ();
343 if (dont_print_statmem == 0)
345 size_t obstack_final_size =
346 obstack_object_size (&dont_print_statmem_obstack);
348 if (obstack_final_size > statmem_obstack_initial_size)
350 /* In effect, a pop of the printed-statics stack. */
351 size_t shrink_bytes
352 = statmem_obstack_initial_size - obstack_final_size;
353 obstack_blank_fast (&dont_print_statmem_obstack, shrink_bytes);
356 if (last_set_recurse != recurse)
358 obstack_final_size =
359 obstack_object_size (&dont_print_stat_array_obstack);
361 if (obstack_final_size > stat_array_obstack_initial_size)
363 void *free_to_ptr =
364 (char *) obstack_next_free (&dont_print_stat_array_obstack)
365 - (obstack_final_size
366 - stat_array_obstack_initial_size);
368 obstack_free (&dont_print_stat_array_obstack,
369 free_to_ptr);
371 last_set_recurse = -1;
375 if (options->prettyformat)
377 gdb_printf (stream, "\n");
378 print_spaces (2 * recurse, stream);
380 } /* if there are data fields */
382 gdb_printf (stream, "}");
385 /* A wrapper for cp_print_value_fields that tries to apply a
386 pretty-printer first. */
388 static void
389 cp_print_value_fields_pp (struct value *val,
390 struct ui_file *stream,
391 int recurse,
392 const struct value_print_options *options,
393 struct type **dont_print_vb,
394 int dont_print_statmem)
396 int result = 0;
398 /* Attempt to run an extension language pretty-printer if
399 possible. */
400 if (!options->raw)
401 result
402 = apply_ext_lang_val_pretty_printer (val, stream,
403 recurse, options,
404 current_language);
406 if (!result)
407 cp_print_value_fields (val, stream, recurse, options, dont_print_vb,
408 dont_print_statmem);
411 /* Special val_print routine to avoid printing multiple copies of
412 virtual baseclasses. */
414 static void
415 cp_print_value (struct value *val, struct ui_file *stream,
416 int recurse, const struct value_print_options *options,
417 struct type **dont_print_vb)
419 struct type *type = check_typedef (val->type ());
420 CORE_ADDR address = val->address ();
421 struct type **last_dont_print
422 = (struct type **) obstack_next_free (&dont_print_vb_obstack);
423 struct obstack tmp_obstack = dont_print_vb_obstack;
424 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
425 const gdb_byte *valaddr = val->contents_for_printing ().data ();
427 if (dont_print_vb == 0)
429 /* If we're at top level, carve out a completely fresh chunk of
430 the obstack and use that until this particular invocation
431 returns. */
432 /* Bump up the high-water mark. Now alpha is omega. */
433 obstack_finish (&dont_print_vb_obstack);
436 for (i = 0; i < n_baseclasses; i++)
438 LONGEST boffset = 0;
439 int skip = 0;
440 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
441 const char *basename = baseclass->name ();
442 struct value *base_val = NULL;
444 if (BASETYPE_VIA_VIRTUAL (type, i))
446 struct type **first_dont_print
447 = (struct type **) obstack_base (&dont_print_vb_obstack);
449 int j = (struct type **)
450 obstack_next_free (&dont_print_vb_obstack) - first_dont_print;
452 while (--j >= 0)
453 if (baseclass == first_dont_print[j])
454 goto flush_it;
456 obstack_ptr_grow (&dont_print_vb_obstack, baseclass);
461 boffset = baseclass_offset (type, i, valaddr,
462 val->embedded_offset (),
463 address, val);
465 catch (const gdb_exception_error &ex)
467 if (ex.error == NOT_AVAILABLE_ERROR)
468 skip = -1;
469 else
470 skip = 1;
473 if (skip == 0)
475 if (BASETYPE_VIA_VIRTUAL (type, i))
477 /* The virtual base class pointer might have been
478 clobbered by the user program. Make sure that it
479 still points to a valid memory location. */
481 if (boffset < 0 || boffset >= type->length ())
483 gdb::byte_vector buf (baseclass->length ());
485 if (target_read_memory (address + boffset, buf.data (),
486 baseclass->length ()) != 0)
487 skip = 1;
488 base_val = value_from_contents_and_address (baseclass,
489 buf.data (),
490 address + boffset);
491 baseclass = base_val->type ();
492 boffset = 0;
494 else
496 base_val = val;
499 else
501 base_val = val;
505 /* Now do the printing. */
506 if (options->prettyformat)
508 gdb_printf (stream, "\n");
509 print_spaces (2 * recurse, stream);
511 gdb_puts ("<", stream);
512 /* Not sure what the best notation is in the case where there is
513 no baseclass name. */
514 gdb_puts (basename ? basename : "", stream);
515 gdb_puts ("> = ", stream);
517 if (skip < 0)
518 val_print_unavailable (stream);
519 else if (skip > 0)
520 val_print_invalid_address (stream);
521 else
523 if (!val_print_check_max_depth (stream, recurse, options,
524 current_language))
526 struct value *baseclass_val = val->primitive_field (0,
527 i, type);
529 cp_print_value_fields_pp
530 (baseclass_val, stream, recurse, options,
531 (struct type **) obstack_base (&dont_print_vb_obstack),
535 gdb_puts (", ", stream);
537 flush_it:
541 if (dont_print_vb == 0)
543 /* Free the space used to deal with the printing
544 of this type from top level. */
545 obstack_free (&dont_print_vb_obstack, last_dont_print);
546 /* Reset watermark so that we can continue protecting
547 ourselves from whatever we were protecting ourselves. */
548 dont_print_vb_obstack = tmp_obstack;
552 /* Print value of a static member. To avoid infinite recursion when
553 printing a class that contains a static instance of the class, we
554 keep the addresses of all printed static member classes in an
555 obstack and refuse to print them more than once.
557 VAL contains the value to print, TYPE, STREAM, RECURSE, and OPTIONS
558 have the same meanings as in c_val_print. */
560 static void
561 cp_print_static_field (struct type *type,
562 struct value *val,
563 struct ui_file *stream,
564 int recurse,
565 const struct value_print_options *options)
567 struct value_print_options opts;
569 if (val->entirely_optimized_out ())
571 val_print_optimized_out (val, stream);
572 return;
575 struct type *real_type = check_typedef (type);
576 if (real_type->code () == TYPE_CODE_STRUCT)
578 CORE_ADDR *first_dont_print;
579 CORE_ADDR addr = val->address ();
580 int i;
582 first_dont_print
583 = (CORE_ADDR *) obstack_base (&dont_print_statmem_obstack);
584 i = obstack_object_size (&dont_print_statmem_obstack)
585 / sizeof (CORE_ADDR);
587 while (--i >= 0)
589 if (addr == first_dont_print[i])
591 fputs_styled (_("<same as static member of an already"
592 " seen type>"),
593 metadata_style.style (), stream);
594 return;
598 obstack_grow (&dont_print_statmem_obstack, (char *) &addr,
599 sizeof (CORE_ADDR));
600 cp_print_value_fields_pp (val, stream, recurse, options, nullptr, 1);
601 return;
604 if (real_type->code () == TYPE_CODE_ARRAY)
606 struct type **first_dont_print;
607 int i;
608 struct type *target_type = type->target_type ();
610 first_dont_print
611 = (struct type **) obstack_base (&dont_print_stat_array_obstack);
612 i = obstack_object_size (&dont_print_stat_array_obstack)
613 / sizeof (struct type *);
615 while (--i >= 0)
617 if (target_type == first_dont_print[i])
619 fputs_styled (_("<same as static member of an already"
620 " seen type>"),
621 metadata_style.style (), stream);
622 return;
626 obstack_grow (&dont_print_stat_array_obstack,
627 (char *) &target_type,
628 sizeof (struct type *));
631 opts = *options;
632 opts.deref_ref = false;
633 common_val_print (val, stream, recurse, &opts, current_language);
636 /* Find the field in *SELF, or its non-virtual base classes, with
637 bit offset OFFSET. Set *SELF to the containing type and *FIELDNO
638 to the containing field number. If OFFSET is not exactly at the
639 start of some field, set *SELF to NULL. */
641 static void
642 cp_find_class_member (struct type **self_p, int *fieldno,
643 LONGEST offset)
645 struct type *self;
646 unsigned int i;
647 unsigned len;
649 *self_p = check_typedef (*self_p);
650 self = *self_p;
651 len = self->num_fields ();
653 for (i = TYPE_N_BASECLASSES (self); i < len; i++)
655 field &f = self->field (i);
656 if (f.is_static ())
657 continue;
658 LONGEST bitpos = f.loc_bitpos ();
660 QUIT;
661 if (offset == bitpos)
663 *fieldno = i;
664 return;
668 for (i = 0; i < TYPE_N_BASECLASSES (self); i++)
670 LONGEST bitpos = self->field (i).loc_bitpos ();
671 LONGEST bitsize = 8 * self->field (i).type ()->length ();
673 if (offset >= bitpos && offset < bitpos + bitsize)
675 *self_p = self->field (i).type ();
676 cp_find_class_member (self_p, fieldno, offset - bitpos);
677 return;
681 *self_p = NULL;
684 void
685 cp_print_class_member (const gdb_byte *valaddr, struct type *type,
686 struct ui_file *stream, const char *prefix)
688 enum bfd_endian byte_order = type_byte_order (type);
690 /* VAL is a byte offset into the structure type SELF_TYPE.
691 Find the name of the field for that offset and
692 print it. */
693 struct type *self_type = TYPE_SELF_TYPE (type);
694 LONGEST val;
695 int fieldno;
697 val = extract_signed_integer (valaddr,
698 type->length (),
699 byte_order);
701 /* Pointers to data members are usually byte offsets into an object.
702 Because a data member can have offset zero, and a NULL pointer to
703 member must be distinct from any valid non-NULL pointer to
704 member, either the value is biased or the NULL value has a
705 special representation; both are permitted by ISO C++. HP aCC
706 used a bias of 0x20000000; HP cfront used a bias of 1; g++ 3.x
707 and other compilers which use the Itanium ABI use -1 as the NULL
708 value. GDB only supports that last form; to add support for
709 another form, make this into a cp-abi hook. */
711 if (val == -1)
713 gdb_printf (stream, "NULL");
714 return;
717 cp_find_class_member (&self_type, &fieldno, val << 3);
719 if (self_type != NULL)
721 const char *name;
723 gdb_puts (prefix, stream);
724 name = self_type->name ();
725 if (name)
726 gdb_puts (name, stream);
727 else
728 c_type_print_base (self_type, stream, 0, 0, &type_print_raw_options);
729 gdb_printf (stream, "::");
730 fputs_styled (self_type->field (fieldno).name (),
731 variable_name_style.style (), stream);
733 else
734 gdb_printf (stream, "%ld", (long) val);
737 #if GDB_SELF_TEST
739 /* Test printing of TYPE_CODE_STRUCT values. */
741 static void
742 test_print_fields (gdbarch *arch)
744 struct field *f;
745 type *uint8_type = builtin_type (arch)->builtin_uint8;
746 type *bool_type = builtin_type (arch)->builtin_bool;
747 type *the_struct = arch_composite_type (arch, NULL, TYPE_CODE_STRUCT);
748 the_struct->set_length (4);
750 /* Value: 1110 1001
751 Fields: C-BB B-A- */
752 if (gdbarch_byte_order (arch) == BFD_ENDIAN_LITTLE)
754 f = append_composite_type_field_raw (the_struct, "A", bool_type);
755 f->set_loc_bitpos (1);
756 f->set_bitsize (1);
757 f = append_composite_type_field_raw (the_struct, "B", uint8_type);
758 f->set_loc_bitpos (3);
759 f->set_bitsize (3);
760 f = append_composite_type_field_raw (the_struct, "C", bool_type);
761 f->set_loc_bitpos (7);
762 f->set_bitsize (1);
764 /* According to the logic commented in "make_gdb_type_struct ()" of
765 * target-descriptions.c, bit positions are numbered differently for
766 * little and big endians. */
767 else
769 f = append_composite_type_field_raw (the_struct, "A", bool_type);
770 f->set_loc_bitpos (30);
771 f->set_bitsize (1);
772 f = append_composite_type_field_raw (the_struct, "B", uint8_type);
773 f->set_loc_bitpos (26);
774 f->set_bitsize (3);
775 f = append_composite_type_field_raw (the_struct, "C", bool_type);
776 f->set_loc_bitpos (24);
777 f->set_bitsize (1);
780 value *val = value::allocate (the_struct);
781 gdb_byte *contents = val->contents_writeable ().data ();
782 store_unsigned_integer (contents, val->enclosing_type ()->length (),
783 gdbarch_byte_order (arch), 0xe9);
785 string_file out;
786 struct value_print_options opts;
787 get_no_prettyformat_print_options (&opts);
788 cp_print_value_fields(val, &out, 0, &opts, NULL, 0);
789 SELF_CHECK (out.string () == "{A = false, B = 5, C = true}");
791 out.clear();
792 opts.format = 'x';
793 cp_print_value_fields(val, &out, 0, &opts, NULL, 0);
794 SELF_CHECK (out.string () == "{A = 0x0, B = 0x5, C = 0x1}");
797 #endif
800 void _initialize_cp_valprint ();
801 void
802 _initialize_cp_valprint ()
804 #if GDB_SELF_TEST
805 selftests::register_test_foreach_arch ("print-fields", test_print_fields);
806 #endif
808 obstack_begin (&dont_print_stat_array_obstack,
809 32 * sizeof (struct type *));
810 obstack_begin (&dont_print_statmem_obstack,
811 32 * sizeof (CORE_ADDR));
812 obstack_begin (&dont_print_vb_obstack,
813 32 * sizeof (struct type *));