1 /* Print 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/>. */
29 #include "target-float.h"
30 #include "extension.h"
32 #include "gdbsupport/gdb_obstack.h"
34 #include "typeprint.h"
37 #include "gdbsupport/byte-vector.h"
38 #include "cli/cli-option.h"
40 #include "cli/cli-style.h"
41 #include "count-one-bits.h"
45 #include "gdbsupport/selftest.h"
46 #include "selftest-arch.h"
48 /* Maximum number of wchars returned from wchar_iterate. */
51 /* A convenience macro to compute the size of a wchar_t buffer containing X
53 #define WCHAR_BUFLEN(X) ((X) * sizeof (gdb_wchar_t))
55 /* Character buffer size saved while iterating over wchars. */
56 #define WCHAR_BUFLEN_MAX WCHAR_BUFLEN (MAX_WCHARS)
58 /* A structure to encapsulate state information from iterated
59 character conversions. */
60 struct converted_character
62 /* The number of characters converted. */
65 /* The result of the conversion. See charset.h for more. */
66 enum wchar_iterate_result result
;
68 /* The (saved) converted character(s). */
69 gdb_wchar_t chars
[WCHAR_BUFLEN_MAX
];
71 /* The first converted target byte. */
74 /* The number of bytes converted. */
77 /* How many times this character(s) is repeated. */
81 /* Command lists for set/show print raw. */
82 struct cmd_list_element
*setprintrawlist
;
83 struct cmd_list_element
*showprintrawlist
;
85 /* Prototypes for local functions */
87 static void set_input_radix_1 (int, unsigned);
89 static void set_output_radix_1 (int, unsigned);
91 static void val_print_type_code_flags (struct type
*type
,
92 struct value
*original_value
,
94 struct ui_file
*stream
);
96 /* Start print_max at this value. */
97 #define PRINT_MAX_DEFAULT 200
99 /* Start print_max_chars at this value (meaning follow print_max). */
100 #define PRINT_MAX_CHARS_DEFAULT PRINT_MAX_CHARS_ELEMENTS
102 /* Start print_max_depth at this value. */
103 #define PRINT_MAX_DEPTH_DEFAULT 20
105 struct value_print_options user_print_options
=
107 Val_prettyformat_default
, /* prettyformat */
108 false, /* prettyformat_arrays */
109 false, /* prettyformat_structs */
110 false, /* vtblprint */
111 true, /* unionprint */
112 true, /* addressprint */
113 false, /* nibblesprint */
114 false, /* objectprint */
115 PRINT_MAX_DEFAULT
, /* print_max */
116 PRINT_MAX_CHARS_DEFAULT
, /* print_max_chars */
117 10, /* repeat_count_threshold */
118 0, /* output_format */
120 true, /* memory_tag_violations */
121 false, /* stop_print_at_null */
122 false, /* print_array_indexes */
123 false, /* deref_ref */
124 true, /* static_field_print */
125 true, /* pascal_static_field_print */
128 true, /* symbol_print */
129 PRINT_MAX_DEPTH_DEFAULT
, /* max_depth */
132 /* Initialize *OPTS to be a copy of the user print options. */
134 get_user_print_options (struct value_print_options
*opts
)
136 *opts
= user_print_options
;
139 /* Initialize *OPTS to be a copy of the user print options, but with
140 pretty-formatting disabled. */
142 get_no_prettyformat_print_options (struct value_print_options
*opts
)
144 *opts
= user_print_options
;
145 opts
->prettyformat
= Val_no_prettyformat
;
148 /* Initialize *OPTS to be a copy of the user print options, but using
149 FORMAT as the formatting option. */
151 get_formatted_print_options (struct value_print_options
*opts
,
154 *opts
= user_print_options
;
155 opts
->format
= format
;
158 /* Implement 'show print elements'. */
161 show_print_max (struct ui_file
*file
, int from_tty
,
162 struct cmd_list_element
*c
, const char *value
)
166 (user_print_options
.print_max_chars
!= PRINT_MAX_CHARS_ELEMENTS
167 ? _("Limit on array elements to print is %s.\n")
168 : _("Limit on string chars or array elements to print is %s.\n")),
172 /* Implement 'show print characters'. */
175 show_print_max_chars (struct ui_file
*file
, int from_tty
,
176 struct cmd_list_element
*c
, const char *value
)
179 _("Limit on string characters to print is %s.\n"),
183 /* Default input and output radixes, and output format letter. */
185 unsigned input_radix
= 10;
187 show_input_radix (struct ui_file
*file
, int from_tty
,
188 struct cmd_list_element
*c
, const char *value
)
191 _("Default input radix for entering numbers is %s.\n"),
195 unsigned output_radix
= 10;
197 show_output_radix (struct ui_file
*file
, int from_tty
,
198 struct cmd_list_element
*c
, const char *value
)
201 _("Default output radix for printing of values is %s.\n"),
205 /* By default we print arrays without printing the index of each element in
206 the array. This behavior can be changed by setting PRINT_ARRAY_INDEXES. */
209 show_print_array_indexes (struct ui_file
*file
, int from_tty
,
210 struct cmd_list_element
*c
, const char *value
)
212 gdb_printf (file
, _("Printing of array indexes is %s.\n"), value
);
215 /* Print repeat counts if there are more than this many repetitions of an
216 element in an array. Referenced by the low level language dependent
220 show_repeat_count_threshold (struct ui_file
*file
, int from_tty
,
221 struct cmd_list_element
*c
, const char *value
)
223 gdb_printf (file
, _("Threshold for repeated print elements is %s.\n"),
227 /* If nonzero, prints memory tag violations for pointers. */
230 show_memory_tag_violations (struct ui_file
*file
, int from_tty
,
231 struct cmd_list_element
*c
, const char *value
)
234 _("Printing of memory tag violations is %s.\n"),
238 /* If nonzero, stops printing of char arrays at first null. */
241 show_stop_print_at_null (struct ui_file
*file
, int from_tty
,
242 struct cmd_list_element
*c
, const char *value
)
245 _("Printing of char arrays to stop "
246 "at first null char is %s.\n"),
250 /* Controls pretty printing of structures. */
253 show_prettyformat_structs (struct ui_file
*file
, int from_tty
,
254 struct cmd_list_element
*c
, const char *value
)
256 gdb_printf (file
, _("Pretty formatting of structures is %s.\n"), value
);
259 /* Controls pretty printing of arrays. */
262 show_prettyformat_arrays (struct ui_file
*file
, int from_tty
,
263 struct cmd_list_element
*c
, const char *value
)
265 gdb_printf (file
, _("Pretty formatting of arrays is %s.\n"), value
);
268 /* If nonzero, causes unions inside structures or other unions to be
272 show_unionprint (struct ui_file
*file
, int from_tty
,
273 struct cmd_list_element
*c
, const char *value
)
276 _("Printing of unions interior to structures is %s.\n"),
280 /* Controls the format of printing binary values. */
283 show_nibbles (struct ui_file
*file
, int from_tty
,
284 struct cmd_list_element
*c
, const char *value
)
287 _("Printing binary values in groups is %s.\n"),
291 /* If nonzero, causes machine addresses to be printed in certain contexts. */
294 show_addressprint (struct ui_file
*file
, int from_tty
,
295 struct cmd_list_element
*c
, const char *value
)
297 gdb_printf (file
, _("Printing of addresses is %s.\n"), value
);
301 show_symbol_print (struct ui_file
*file
, int from_tty
,
302 struct cmd_list_element
*c
, const char *value
)
305 _("Printing of symbols when printing pointers is %s.\n"),
311 /* A helper function for val_print. When printing in "summary" mode,
312 we want to print scalar arguments, but not aggregate arguments.
313 This function distinguishes between the two. */
316 val_print_scalar_type_p (struct type
*type
)
318 type
= check_typedef (type
);
319 while (TYPE_IS_REFERENCE (type
))
321 type
= type
->target_type ();
322 type
= check_typedef (type
);
324 switch (type
->code ())
326 case TYPE_CODE_ARRAY
:
327 case TYPE_CODE_STRUCT
:
328 case TYPE_CODE_UNION
:
330 case TYPE_CODE_STRING
:
337 /* A helper function for val_print. When printing with limited depth we
338 want to print string and scalar arguments, but not aggregate arguments.
339 This function distinguishes between the two. */
342 val_print_scalar_or_string_type_p (struct type
*type
,
343 const struct language_defn
*language
)
345 return (val_print_scalar_type_p (type
)
346 || language
->is_string_type_p (type
));
349 /* See valprint.h. */
352 valprint_check_validity (struct ui_file
*stream
,
354 LONGEST embedded_offset
,
355 const struct value
*val
)
357 type
= check_typedef (type
);
359 if (type_not_associated (type
))
361 val_print_not_associated (stream
);
365 if (type_not_allocated (type
))
367 val_print_not_allocated (stream
);
371 if (type
->code () != TYPE_CODE_UNION
372 && type
->code () != TYPE_CODE_STRUCT
373 && type
->code () != TYPE_CODE_ARRAY
)
375 if (val
->bits_any_optimized_out (TARGET_CHAR_BIT
* embedded_offset
,
376 TARGET_CHAR_BIT
* type
->length ()))
378 val_print_optimized_out (val
, stream
);
382 if (val
->bits_synthetic_pointer (TARGET_CHAR_BIT
* embedded_offset
,
383 TARGET_CHAR_BIT
* type
->length ()))
385 const int is_ref
= type
->code () == TYPE_CODE_REF
;
386 int ref_is_addressable
= 0;
390 const struct value
*deref_val
= coerce_ref_if_computed (val
);
392 if (deref_val
!= NULL
)
393 ref_is_addressable
= deref_val
->lval () == lval_memory
;
396 if (!is_ref
|| !ref_is_addressable
)
397 fputs_styled (_("<synthetic pointer>"), metadata_style
.style (),
400 /* C++ references should be valid even if they're synthetic. */
404 if (!val
->bytes_available (embedded_offset
, type
->length ()))
406 val_print_unavailable (stream
);
415 val_print_optimized_out (const struct value
*val
, struct ui_file
*stream
)
417 if (val
!= NULL
&& val
->lval () == lval_register
)
418 val_print_not_saved (stream
);
420 fprintf_styled (stream
, metadata_style
.style (), _("<optimized out>"));
424 val_print_not_saved (struct ui_file
*stream
)
426 fprintf_styled (stream
, metadata_style
.style (), _("<not saved>"));
430 val_print_unavailable (struct ui_file
*stream
)
432 fprintf_styled (stream
, metadata_style
.style (), _("<unavailable>"));
436 val_print_invalid_address (struct ui_file
*stream
)
438 fprintf_styled (stream
, metadata_style
.style (), _("<invalid address>"));
441 /* Print a pointer based on the type of its target.
443 Arguments to this functions are roughly the same as those in
444 generic_val_print. A difference is that ADDRESS is the address to print,
445 with embedded_offset already added. ELTTYPE represents
446 the pointed type after check_typedef. */
449 print_unpacked_pointer (struct type
*type
, struct type
*elttype
,
450 CORE_ADDR address
, struct ui_file
*stream
,
451 const struct value_print_options
*options
)
453 struct gdbarch
*gdbarch
= type
->arch ();
455 if (elttype
->code () == TYPE_CODE_FUNC
)
457 /* Try to print what function it points to. */
458 print_function_pointer_address (options
, gdbarch
, address
, stream
);
462 if (options
->symbol_print
)
463 print_address_demangle (options
, gdbarch
, address
, stream
, demangle
);
464 else if (options
->addressprint
)
465 gdb_puts (paddress (gdbarch
, address
), stream
);
468 /* generic_val_print helper for TYPE_CODE_ARRAY. */
471 generic_val_print_array (struct value
*val
,
472 struct ui_file
*stream
, int recurse
,
473 const struct value_print_options
*options
,
475 generic_val_print_decorations
*decorations
)
477 struct type
*type
= check_typedef (val
->type ());
478 struct type
*unresolved_elttype
= type
->target_type ();
479 struct type
*elttype
= check_typedef (unresolved_elttype
);
481 if (type
->length () > 0 && unresolved_elttype
->length () > 0)
483 LONGEST low_bound
, high_bound
;
485 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
486 error (_("Could not determine the array high bound"));
488 gdb_puts (decorations
->array_start
, stream
);
489 value_print_array_elements (val
, stream
, recurse
, options
, 0);
490 gdb_puts (decorations
->array_end
, stream
);
494 /* Array of unspecified length: treat like pointer to first elt. */
495 print_unpacked_pointer (type
, elttype
, val
->address (),
501 /* generic_value_print helper for TYPE_CODE_PTR. */
504 generic_value_print_ptr (struct value
*val
, struct ui_file
*stream
,
505 const struct value_print_options
*options
)
508 if (options
->format
&& options
->format
!= 's')
509 value_print_scalar_formatted (val
, options
, 0, stream
);
512 struct type
*type
= check_typedef (val
->type ());
513 struct type
*elttype
= check_typedef (type
->target_type ());
514 const gdb_byte
*valaddr
= val
->contents_for_printing ().data ();
515 CORE_ADDR addr
= unpack_pointer (type
, valaddr
);
517 print_unpacked_pointer (type
, elttype
, addr
, stream
, options
);
522 /* Print '@' followed by the address contained in ADDRESS_BUFFER. */
525 print_ref_address (struct type
*type
, const gdb_byte
*address_buffer
,
526 int embedded_offset
, struct ui_file
*stream
)
528 struct gdbarch
*gdbarch
= type
->arch ();
530 if (address_buffer
!= NULL
)
533 = extract_typed_address (address_buffer
+ embedded_offset
, type
);
535 gdb_printf (stream
, "@");
536 gdb_puts (paddress (gdbarch
, address
), stream
);
538 /* Else: we have a non-addressable value, such as a DW_AT_const_value. */
541 /* If VAL is addressable, return the value contents buffer of a value that
542 represents a pointer to VAL. Otherwise return NULL. */
544 static const gdb_byte
*
545 get_value_addr_contents (struct value
*deref_val
)
547 gdb_assert (deref_val
!= NULL
);
549 if (deref_val
->lval () == lval_memory
)
550 return value_addr (deref_val
)->contents_for_printing ().data ();
553 /* We have a non-addressable value, such as a DW_AT_const_value. */
558 /* generic_val_print helper for TYPE_CODE_{RVALUE_,}REF. */
561 generic_val_print_ref (struct type
*type
,
562 int embedded_offset
, struct ui_file
*stream
, int recurse
,
563 struct value
*original_value
,
564 const struct value_print_options
*options
)
566 struct type
*elttype
= check_typedef (type
->target_type ());
567 struct value
*deref_val
= NULL
;
568 const bool value_is_synthetic
569 = original_value
->bits_synthetic_pointer (TARGET_CHAR_BIT
* embedded_offset
,
570 TARGET_CHAR_BIT
* type
->length ());
571 const int must_coerce_ref
= ((options
->addressprint
&& value_is_synthetic
)
572 || options
->deref_ref
);
573 const int type_is_defined
= elttype
->code () != TYPE_CODE_UNDEF
;
574 const gdb_byte
*valaddr
= original_value
->contents_for_printing ().data ();
576 if (must_coerce_ref
&& type_is_defined
)
578 deref_val
= coerce_ref_if_computed (original_value
);
580 if (deref_val
!= NULL
)
582 /* More complicated computed references are not supported. */
583 gdb_assert (embedded_offset
== 0);
586 deref_val
= value_at (type
->target_type (),
587 unpack_pointer (type
, valaddr
+ embedded_offset
));
589 /* Else, original_value isn't a synthetic reference or we don't have to print
590 the reference's contents.
592 Notice that for references to TYPE_CODE_STRUCT, 'set print object on' will
593 cause original_value to be a not_lval instead of an lval_computed,
594 which will make value_bits_synthetic_pointer return false.
595 This happens because if options->objectprint is true, c_value_print will
596 overwrite original_value's contents with the result of coercing
597 the reference through value_addr, and then set its type back to
598 TYPE_CODE_REF. In that case we don't have to coerce the reference again;
599 we can simply treat it as non-synthetic and move on. */
601 if (options
->addressprint
)
603 const gdb_byte
*address
= (value_is_synthetic
&& type_is_defined
604 ? get_value_addr_contents (deref_val
)
607 print_ref_address (type
, address
, embedded_offset
, stream
);
609 if (options
->deref_ref
)
610 gdb_puts (": ", stream
);
613 if (options
->deref_ref
)
616 common_val_print (deref_val
, stream
, recurse
, options
,
619 gdb_puts ("???", stream
);
623 /* Helper function for generic_val_print_enum.
624 This is also used to print enums in TYPE_CODE_FLAGS values. */
627 generic_val_print_enum_1 (struct type
*type
, LONGEST val
,
628 struct ui_file
*stream
)
633 len
= type
->num_fields ();
634 for (i
= 0; i
< len
; i
++)
637 if (val
== type
->field (i
).loc_enumval ())
644 fputs_styled (type
->field (i
).name (), variable_name_style
.style (),
647 else if (type
->is_flag_enum ())
651 /* We have a "flag" enum, so we try to decompose it into pieces as
652 appropriate. The enum may have multiple enumerators representing
653 the same bit, in which case we choose to only print the first one
655 for (i
= 0; i
< len
; ++i
)
659 ULONGEST enumval
= type
->field (i
).loc_enumval ();
660 int nbits
= count_one_bits_ll (enumval
);
662 gdb_assert (nbits
== 0 || nbits
== 1);
664 if ((val
& enumval
) != 0)
668 gdb_puts ("(", stream
);
672 gdb_puts (" | ", stream
);
674 val
&= ~type
->field (i
).loc_enumval ();
675 fputs_styled (type
->field (i
).name (),
676 variable_name_style
.style (), stream
);
682 /* There are leftover bits, print them. */
684 gdb_puts ("(", stream
);
686 gdb_puts (" | ", stream
);
688 gdb_puts ("unknown: 0x", stream
);
689 print_longest (stream
, 'x', 0, val
);
690 gdb_puts (")", stream
);
694 /* Nothing has been printed and the value is 0, the enum value must
696 gdb_puts ("0", stream
);
700 /* Something has been printed, close the parenthesis. */
701 gdb_puts (")", stream
);
705 print_longest (stream
, 'd', 0, val
);
708 /* generic_val_print helper for TYPE_CODE_ENUM. */
711 generic_val_print_enum (struct type
*type
,
712 int embedded_offset
, struct ui_file
*stream
,
713 struct value
*original_value
,
714 const struct value_print_options
*options
)
717 struct gdbarch
*gdbarch
= type
->arch ();
718 int unit_size
= gdbarch_addressable_memory_unit_size (gdbarch
);
720 gdb_assert (!options
->format
);
722 const gdb_byte
*valaddr
= original_value
->contents_for_printing ().data ();
724 val
= unpack_long (type
, valaddr
+ embedded_offset
* unit_size
);
726 generic_val_print_enum_1 (type
, val
, stream
);
729 /* generic_val_print helper for TYPE_CODE_FUNC and TYPE_CODE_METHOD. */
732 generic_val_print_func (struct type
*type
,
733 int embedded_offset
, CORE_ADDR address
,
734 struct ui_file
*stream
,
735 struct value
*original_value
,
736 const struct value_print_options
*options
)
738 struct gdbarch
*gdbarch
= type
->arch ();
740 gdb_assert (!options
->format
);
742 /* FIXME, we should consider, at least for ANSI C language,
743 eliminating the distinction made between FUNCs and POINTERs to
745 gdb_printf (stream
, "{");
746 type_print (type
, "", stream
, -1);
747 gdb_printf (stream
, "} ");
748 /* Try to print what function it points to, and its address. */
749 print_address_demangle (options
, gdbarch
, address
, stream
, demangle
);
752 /* generic_value_print helper for TYPE_CODE_BOOL. */
755 generic_value_print_bool
756 (struct value
*value
, struct ui_file
*stream
,
757 const struct value_print_options
*options
,
758 const struct generic_val_print_decorations
*decorations
)
760 if (options
->format
|| options
->output_format
)
762 struct value_print_options opts
= *options
;
763 opts
.format
= (options
->format
? options
->format
764 : options
->output_format
);
765 value_print_scalar_formatted (value
, &opts
, 0, stream
);
769 const gdb_byte
*valaddr
= value
->contents_for_printing ().data ();
770 struct type
*type
= check_typedef (value
->type ());
771 LONGEST val
= unpack_long (type
, valaddr
);
773 gdb_puts (decorations
->false_name
, stream
);
775 gdb_puts (decorations
->true_name
, stream
);
777 print_longest (stream
, 'd', 0, val
);
781 /* generic_value_print helper for TYPE_CODE_INT. */
784 generic_value_print_int (struct value
*val
, struct ui_file
*stream
,
785 const struct value_print_options
*options
)
787 struct value_print_options opts
= *options
;
789 opts
.format
= (options
->format
? options
->format
790 : options
->output_format
);
791 value_print_scalar_formatted (val
, &opts
, 0, stream
);
794 /* generic_value_print helper for TYPE_CODE_CHAR. */
797 generic_value_print_char (struct value
*value
, struct ui_file
*stream
,
798 const struct value_print_options
*options
)
800 if (options
->format
|| options
->output_format
)
802 struct value_print_options opts
= *options
;
804 opts
.format
= (options
->format
? options
->format
805 : options
->output_format
);
806 value_print_scalar_formatted (value
, &opts
, 0, stream
);
810 struct type
*unresolved_type
= value
->type ();
811 struct type
*type
= check_typedef (unresolved_type
);
812 const gdb_byte
*valaddr
= value
->contents_for_printing ().data ();
814 LONGEST val
= unpack_long (type
, valaddr
);
815 if (type
->is_unsigned ())
816 gdb_printf (stream
, "%u", (unsigned int) val
);
818 gdb_printf (stream
, "%d", (int) val
);
819 gdb_puts (" ", stream
);
820 current_language
->printchar (val
, unresolved_type
, stream
);
824 /* generic_val_print helper for TYPE_CODE_FLT and TYPE_CODE_DECFLOAT. */
827 generic_val_print_float (struct type
*type
, struct ui_file
*stream
,
828 struct value
*original_value
,
829 const struct value_print_options
*options
)
831 gdb_assert (!options
->format
);
833 const gdb_byte
*valaddr
= original_value
->contents_for_printing ().data ();
835 print_floating (valaddr
, type
, stream
);
838 /* generic_val_print helper for TYPE_CODE_FIXED_POINT. */
841 generic_val_print_fixed_point (struct value
*val
, struct ui_file
*stream
,
842 const struct value_print_options
*options
)
845 value_print_scalar_formatted (val
, options
, 0, stream
);
848 struct type
*type
= val
->type ();
850 const gdb_byte
*valaddr
= val
->contents_for_printing ().data ();
853 f
.read_fixed_point (gdb::make_array_view (valaddr
, type
->length ()),
854 type_byte_order (type
), type
->is_unsigned (),
855 type
->fixed_point_scaling_factor ());
857 const char *fmt
= type
->length () < 4 ? "%.11Fg" : "%.17Fg";
858 std::string str
= f
.str (fmt
);
859 gdb_printf (stream
, "%s", str
.c_str ());
863 /* generic_value_print helper for TYPE_CODE_COMPLEX. */
866 generic_value_print_complex (struct value
*val
, struct ui_file
*stream
,
867 const struct value_print_options
*options
,
868 const struct generic_val_print_decorations
871 gdb_printf (stream
, "%s", decorations
->complex_prefix
);
873 struct value
*real_part
= value_real_part (val
);
874 value_print_scalar_formatted (real_part
, options
, 0, stream
);
875 gdb_printf (stream
, "%s", decorations
->complex_infix
);
877 struct value
*imag_part
= value_imaginary_part (val
);
878 value_print_scalar_formatted (imag_part
, options
, 0, stream
);
879 gdb_printf (stream
, "%s", decorations
->complex_suffix
);
882 /* generic_value_print helper for TYPE_CODE_MEMBERPTR. */
885 generic_value_print_memberptr
886 (struct value
*val
, struct ui_file
*stream
,
888 const struct value_print_options
*options
,
889 const struct generic_val_print_decorations
*decorations
)
891 if (!options
->format
)
893 /* Member pointers are essentially specific to C++, and so if we
894 encounter one, we should print it according to C++ rules. */
895 struct type
*type
= check_typedef (val
->type ());
896 const gdb_byte
*valaddr
= val
->contents_for_printing ().data ();
897 cp_print_class_member (valaddr
, type
, stream
, "&");
900 value_print_scalar_formatted (val
, options
, 0, stream
);
903 /* See valprint.h. */
906 generic_value_print (struct value
*val
, struct ui_file
*stream
, int recurse
,
907 const struct value_print_options
*options
,
908 const struct generic_val_print_decorations
*decorations
)
910 struct type
*type
= val
->type ();
912 type
= check_typedef (type
);
914 if (is_fixed_point_type (type
))
915 type
= type
->fixed_point_type_base_type ();
917 /* Widen a subrange to its target type, then use that type's
919 while (type
->code () == TYPE_CODE_RANGE
)
921 type
= check_typedef (type
->target_type ());
922 val
= value_cast (type
, val
);
925 switch (type
->code ())
927 case TYPE_CODE_ARRAY
:
928 generic_val_print_array (val
, stream
, recurse
, options
, decorations
);
931 case TYPE_CODE_MEMBERPTR
:
932 generic_value_print_memberptr (val
, stream
, recurse
, options
,
937 generic_value_print_ptr (val
, stream
, options
);
941 case TYPE_CODE_RVALUE_REF
:
942 generic_val_print_ref (type
, 0, stream
, recurse
,
948 value_print_scalar_formatted (val
, options
, 0, stream
);
950 generic_val_print_enum (type
, 0, stream
, val
, options
);
953 case TYPE_CODE_FLAGS
:
955 value_print_scalar_formatted (val
, options
, 0, stream
);
957 val_print_type_code_flags (type
, val
, 0, stream
);
961 case TYPE_CODE_METHOD
:
963 value_print_scalar_formatted (val
, options
, 0, stream
);
965 generic_val_print_func (type
, 0, val
->address (), stream
,
970 generic_value_print_bool (val
, stream
, options
, decorations
);
974 generic_value_print_int (val
, stream
, options
);
978 generic_value_print_char (val
, stream
, options
);
982 case TYPE_CODE_DECFLOAT
:
984 value_print_scalar_formatted (val
, options
, 0, stream
);
986 generic_val_print_float (type
, stream
, val
, options
);
989 case TYPE_CODE_FIXED_POINT
:
990 generic_val_print_fixed_point (val
, stream
, options
);
994 gdb_puts (decorations
->void_name
, stream
);
997 case TYPE_CODE_ERROR
:
998 gdb_printf (stream
, "%s", TYPE_ERROR_NAME (type
));
1001 case TYPE_CODE_UNDEF
:
1002 /* This happens (without TYPE_STUB set) on systems which don't use
1003 dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
1004 and no complete type for struct foo in that file. */
1005 fprintf_styled (stream
, metadata_style
.style (), _("<incomplete type>"));
1008 case TYPE_CODE_COMPLEX
:
1009 generic_value_print_complex (val
, stream
, options
, decorations
);
1012 case TYPE_CODE_METHODPTR
:
1013 cplus_print_method_ptr (val
->contents_for_printing ().data (), type
,
1017 case TYPE_CODE_UNION
:
1018 case TYPE_CODE_STRUCT
:
1020 error (_("Unhandled type code %d in symbol table."),
1025 /* Print using the given LANGUAGE the value VAL onto stream STREAM according
1028 This is a preferable interface to val_print, above, because it uses
1029 GDB's value mechanism. */
1032 common_val_print (struct value
*value
, struct ui_file
*stream
, int recurse
,
1033 const struct value_print_options
*options
,
1034 const struct language_defn
*language
)
1036 if (language
->la_language
== language_ada
)
1037 /* The value might have a dynamic type, which would cause trouble
1038 below when trying to extract the value contents (since the value
1039 size is determined from the type size which is unknown). So
1040 get a fixed representation of our value. */
1041 value
= ada_to_fixed_value (value
);
1044 value
->fetch_lazy ();
1046 struct value_print_options local_opts
= *options
;
1047 struct type
*type
= value
->type ();
1048 struct type
*real_type
= check_typedef (type
);
1050 if (local_opts
.prettyformat
== Val_prettyformat_default
)
1051 local_opts
.prettyformat
= (local_opts
.prettyformat_structs
1052 ? Val_prettyformat
: Val_no_prettyformat
);
1056 if (!valprint_check_validity (stream
, real_type
, 0, value
))
1061 if (apply_ext_lang_val_pretty_printer (value
, stream
, recurse
, options
,
1066 /* Ensure that the type is complete and not just a stub. If the type is
1067 only a stub and we can't find and substitute its complete type, then
1068 print appropriate string and return. */
1070 if (real_type
->is_stub ())
1072 fprintf_styled (stream
, metadata_style
.style (), _("<incomplete type>"));
1076 /* Handle summary mode. If the value is a scalar, print it;
1077 otherwise, print an ellipsis. */
1078 if (options
->summary
&& !val_print_scalar_type_p (type
))
1080 gdb_printf (stream
, "...");
1084 /* If this value is too deep then don't print it. */
1085 if (!val_print_scalar_or_string_type_p (type
, language
)
1086 && val_print_check_max_depth (stream
, recurse
, options
, language
))
1091 language
->value_print_inner (value
, stream
, recurse
, &local_opts
);
1093 catch (const gdb_exception_error
&except
)
1095 fprintf_styled (stream
, metadata_style
.style (),
1096 _("<error reading variable: %s>"), except
.what ());
1100 /* See valprint.h. */
1103 val_print_check_max_depth (struct ui_file
*stream
, int recurse
,
1104 const struct value_print_options
*options
,
1105 const struct language_defn
*language
)
1107 if (options
->max_depth
> -1 && recurse
>= options
->max_depth
)
1109 gdb_assert (language
->struct_too_deep_ellipsis () != NULL
);
1110 gdb_puts (language
->struct_too_deep_ellipsis (), stream
);
1117 /* Check whether the value VAL is printable. Return 1 if it is;
1118 return 0 and print an appropriate error message to STREAM according to
1119 OPTIONS if it is not. */
1122 value_check_printable (struct value
*val
, struct ui_file
*stream
,
1123 const struct value_print_options
*options
)
1127 fprintf_styled (stream
, metadata_style
.style (),
1128 _("<address of value unknown>"));
1132 if (val
->entirely_optimized_out ())
1134 if (options
->summary
&& !val_print_scalar_type_p (val
->type ()))
1135 gdb_printf (stream
, "...");
1137 val_print_optimized_out (val
, stream
);
1141 if (val
->entirely_unavailable ())
1143 if (options
->summary
&& !val_print_scalar_type_p (val
->type ()))
1144 gdb_printf (stream
, "...");
1146 val_print_unavailable (stream
);
1150 if (val
->type ()->code () == TYPE_CODE_INTERNAL_FUNCTION
)
1152 fprintf_styled (stream
, metadata_style
.style (),
1153 _("<internal function %s>"),
1154 value_internal_function_name (val
));
1158 if (type_not_allocated (val
->type ()))
1160 val_print_not_allocated (stream
);
1167 /* See valprint.h. */
1170 common_val_print_checked (struct value
*val
, struct ui_file
*stream
,
1172 const struct value_print_options
*options
,
1173 const struct language_defn
*language
)
1175 if (!value_check_printable (val
, stream
, options
))
1177 common_val_print (val
, stream
, recurse
, options
, language
);
1180 /* Print on stream STREAM the value VAL according to OPTIONS. The value
1181 is printed using the current_language syntax. */
1184 value_print (struct value
*val
, struct ui_file
*stream
,
1185 const struct value_print_options
*options
)
1187 scoped_value_mark free_values
;
1189 if (!value_check_printable (val
, stream
, options
))
1195 = apply_ext_lang_val_pretty_printer (val
, stream
, 0, options
,
1202 current_language
->value_print (val
, stream
, options
);
1205 /* Meant to be used in debug sessions, so don't export it in a header file. */
1206 extern void ATTRIBUTE_UNUSED
debug_val (struct value
*val
);
1210 void ATTRIBUTE_UNUSED
1211 debug_val (struct value
*val
)
1213 value_print (val
, gdb_stdlog
, &user_print_options
);
1214 gdb_flush (gdb_stdlog
);
1218 val_print_type_code_flags (struct type
*type
, struct value
*original_value
,
1219 int embedded_offset
, struct ui_file
*stream
)
1221 const gdb_byte
*valaddr
= (original_value
->contents_for_printing ().data ()
1223 ULONGEST val
= unpack_long (type
, valaddr
);
1224 int field
, nfields
= type
->num_fields ();
1225 struct gdbarch
*gdbarch
= type
->arch ();
1226 struct type
*bool_type
= builtin_type (gdbarch
)->builtin_bool
;
1228 gdb_puts ("[", stream
);
1229 for (field
= 0; field
< nfields
; field
++)
1231 if (type
->field (field
).name ()[0] != '\0')
1233 struct type
*field_type
= type
->field (field
).type ();
1235 if (field_type
== bool_type
1236 /* We require boolean types here to be one bit wide. This is a
1237 problematic place to notify the user of an internal error
1238 though. Instead just fall through and print the field as an
1240 && type
->field (field
).bitsize () == 1)
1242 if (val
& ((ULONGEST
)1 << type
->field (field
).loc_bitpos ()))
1245 styled_string (variable_name_style
.style (),
1246 type
->field (field
).name ()));
1250 unsigned field_len
= type
->field (field
).bitsize ();
1251 ULONGEST field_val
= val
>> type
->field (field
).loc_bitpos ();
1253 if (field_len
< sizeof (ULONGEST
) * TARGET_CHAR_BIT
)
1254 field_val
&= ((ULONGEST
) 1 << field_len
) - 1;
1255 gdb_printf (stream
, " %ps=",
1256 styled_string (variable_name_style
.style (),
1257 type
->field (field
).name ()));
1258 if (field_type
->code () == TYPE_CODE_ENUM
)
1259 generic_val_print_enum_1 (field_type
, field_val
, stream
);
1261 print_longest (stream
, 'd', 0, field_val
);
1265 gdb_puts (" ]", stream
);
1268 /* See valprint.h. */
1271 value_print_scalar_formatted (struct value
*val
,
1272 const struct value_print_options
*options
,
1274 struct ui_file
*stream
)
1276 struct type
*type
= check_typedef (val
->type ());
1278 gdb_assert (val
!= NULL
);
1280 /* If we get here with a string format, try again without it. Go
1281 all the way back to the language printers, which may call us
1283 if (options
->format
== 's')
1285 struct value_print_options opts
= *options
;
1287 opts
.deref_ref
= false;
1288 common_val_print (val
, stream
, 0, &opts
, current_language
);
1292 /* value_contents_for_printing fetches all VAL's contents. They are
1293 needed to check whether VAL is optimized-out or unavailable
1295 const gdb_byte
*valaddr
= val
->contents_for_printing ().data ();
1297 /* A scalar object that does not have all bits available can't be
1298 printed, because all bits contribute to its representation. */
1299 if (val
->bits_any_optimized_out (0,
1300 TARGET_CHAR_BIT
* type
->length ()))
1301 val_print_optimized_out (val
, stream
);
1302 else if (!val
->bytes_available (0, type
->length ()))
1303 val_print_unavailable (stream
);
1305 print_scalar_formatted (valaddr
, type
, options
, size
, stream
);
1308 /* Print a number according to FORMAT which is one of d,u,x,o,b,h,w,g.
1309 The raison d'etre of this function is to consolidate printing of
1310 LONG_LONG's into this one function. The format chars b,h,w,g are
1311 from print_scalar_formatted(). Numbers are printed using C
1314 USE_C_FORMAT means to use C format in all cases. Without it,
1315 'o' and 'x' format do not include the standard C radix prefix
1318 Hilfinger/2004-09-09: USE_C_FORMAT was originally called USE_LOCAL
1319 and was intended to request formatting according to the current
1320 language and would be used for most integers that GDB prints. The
1321 exceptional cases were things like protocols where the format of
1322 the integer is a protocol thing, not a user-visible thing). The
1323 parameter remains to preserve the information of what things might
1324 be printed with language-specific format, should we ever resurrect
1328 print_longest (struct ui_file
*stream
, int format
, int use_c_format
,
1336 val
= int_string (val_long
, 10, 1, 0, 1); break;
1338 val
= int_string (val_long
, 10, 0, 0, 1); break;
1340 val
= int_string (val_long
, 16, 0, 0, use_c_format
); break;
1342 val
= int_string (val_long
, 16, 0, 2, 1); break;
1344 val
= int_string (val_long
, 16, 0, 4, 1); break;
1346 val
= int_string (val_long
, 16, 0, 8, 1); break;
1348 val
= int_string (val_long
, 16, 0, 16, 1); break;
1351 val
= int_string (val_long
, 8, 0, 0, use_c_format
); break;
1353 internal_error (_("failed internal consistency check"));
1355 gdb_puts (val
, stream
);
1358 /* This used to be a macro, but I don't think it is called often enough
1359 to merit such treatment. */
1360 /* Convert a LONGEST to an int. This is used in contexts (e.g. number of
1361 arguments to a function, number in a value history, register number, etc.)
1362 where the value must not be larger than can fit in an int. */
1365 longest_to_int (LONGEST arg
)
1367 /* Let the compiler do the work. */
1368 int rtnval
= (int) arg
;
1370 /* Check for overflows or underflows. */
1371 if (sizeof (LONGEST
) > sizeof (int))
1375 error (_("Value out of range."));
1381 /* Print a floating point value of floating-point type TYPE,
1382 pointed to in GDB by VALADDR, on STREAM. */
1385 print_floating (const gdb_byte
*valaddr
, struct type
*type
,
1386 struct ui_file
*stream
)
1388 std::string str
= target_float_to_string (valaddr
, type
);
1389 gdb_puts (str
.c_str (), stream
);
1393 print_binary_chars (struct ui_file
*stream
, const gdb_byte
*valaddr
,
1394 unsigned len
, enum bfd_endian byte_order
, bool zero_pad
,
1395 const struct value_print_options
*options
)
1400 bool seen_a_one
= false;
1401 const char *digit_separator
= nullptr;
1403 /* Declared "int" so it will be signed.
1404 This ensures that right shift will shift in zeros. */
1406 const int mask
= 0x080;
1408 if (options
->nibblesprint
)
1409 digit_separator
= current_language
->get_digit_separator();
1411 if (byte_order
== BFD_ENDIAN_BIG
)
1417 /* Every byte has 8 binary characters; peel off
1418 and print from the MSB end. */
1420 for (i
= 0; i
< (HOST_CHAR_BIT
* sizeof (*p
)); i
++)
1422 if (options
->nibblesprint
&& seen_a_one
&& i
% 4 == 0)
1423 gdb_putc (*digit_separator
, stream
);
1425 if (*p
& (mask
>> i
))
1430 if (zero_pad
|| seen_a_one
|| b
== '1')
1431 gdb_putc (b
, stream
);
1432 else if (options
->nibblesprint
)
1434 if ((0xf0 & (mask
>> i
) && (*p
& 0xf0))
1435 || (0x0f & (mask
>> i
) && (*p
& 0x0f)))
1436 gdb_putc (b
, stream
);
1446 for (p
= valaddr
+ len
- 1;
1450 for (i
= 0; i
< (HOST_CHAR_BIT
* sizeof (*p
)); i
++)
1452 if (options
->nibblesprint
&& seen_a_one
&& i
% 4 == 0)
1453 gdb_putc (*digit_separator
, stream
);
1455 if (*p
& (mask
>> i
))
1460 if (zero_pad
|| seen_a_one
|| b
== '1')
1461 gdb_putc (b
, stream
);
1462 else if (options
->nibblesprint
)
1464 if ((0xf0 & (mask
>> i
) && (*p
& 0xf0))
1465 || (0x0f & (mask
>> i
) && (*p
& 0x0f)))
1466 gdb_putc (b
, stream
);
1475 /* When not zero-padding, ensure that something is printed when the
1477 if (!zero_pad
&& !seen_a_one
)
1478 gdb_putc ('0', stream
);
1481 /* A helper for print_octal_chars that emits a single octal digit,
1482 optionally suppressing it if is zero and updating SEEN_A_ONE. */
1485 emit_octal_digit (struct ui_file
*stream
, bool *seen_a_one
, int digit
)
1487 if (*seen_a_one
|| digit
!= 0)
1488 gdb_printf (stream
, "%o", digit
);
1493 /* VALADDR points to an integer of LEN bytes.
1494 Print it in octal on stream or format it in buf. */
1497 print_octal_chars (struct ui_file
*stream
, const gdb_byte
*valaddr
,
1498 unsigned len
, enum bfd_endian byte_order
)
1501 unsigned char octa1
, octa2
, octa3
, carry
;
1504 /* Octal is 3 bits, which doesn't fit. Yuk. So we have to track
1505 * the extra bits, which cycle every three bytes:
1507 * Byte side: 0 1 2 3
1509 * bit number 123 456 78 | 9 012 345 6 | 78 901 234 | 567 890 12 |
1511 * Octal side: 0 1 carry 3 4 carry ...
1513 * Cycle number: 0 1 2
1515 * But of course we are printing from the high side, so we have to
1516 * figure out where in the cycle we are so that we end up with no
1517 * left over bits at the end.
1519 #define BITS_IN_OCTAL 3
1520 #define HIGH_ZERO 0340
1521 #define LOW_ZERO 0034
1522 #define CARRY_ZERO 0003
1523 static_assert (HIGH_ZERO
+ LOW_ZERO
+ CARRY_ZERO
== 0xff,
1524 "cycle zero constants are wrong");
1525 #define HIGH_ONE 0200
1526 #define MID_ONE 0160
1527 #define LOW_ONE 0016
1528 #define CARRY_ONE 0001
1529 static_assert (HIGH_ONE
+ MID_ONE
+ LOW_ONE
+ CARRY_ONE
== 0xff,
1530 "cycle one constants are wrong");
1531 #define HIGH_TWO 0300
1532 #define MID_TWO 0070
1533 #define LOW_TWO 0007
1534 static_assert (HIGH_TWO
+ MID_TWO
+ LOW_TWO
== 0xff,
1535 "cycle two constants are wrong");
1537 /* For 32 we start in cycle 2, with two bits and one bit carry;
1538 for 64 in cycle in cycle 1, with one bit and a two bit carry. */
1540 cycle
= (len
* HOST_CHAR_BIT
) % BITS_IN_OCTAL
;
1543 gdb_puts ("0", stream
);
1544 bool seen_a_one
= false;
1545 if (byte_order
== BFD_ENDIAN_BIG
)
1554 /* No carry in, carry out two bits. */
1556 octa1
= (HIGH_ZERO
& *p
) >> 5;
1557 octa2
= (LOW_ZERO
& *p
) >> 2;
1558 carry
= (CARRY_ZERO
& *p
);
1559 emit_octal_digit (stream
, &seen_a_one
, octa1
);
1560 emit_octal_digit (stream
, &seen_a_one
, octa2
);
1564 /* Carry in two bits, carry out one bit. */
1566 octa1
= (carry
<< 1) | ((HIGH_ONE
& *p
) >> 7);
1567 octa2
= (MID_ONE
& *p
) >> 4;
1568 octa3
= (LOW_ONE
& *p
) >> 1;
1569 carry
= (CARRY_ONE
& *p
);
1570 emit_octal_digit (stream
, &seen_a_one
, octa1
);
1571 emit_octal_digit (stream
, &seen_a_one
, octa2
);
1572 emit_octal_digit (stream
, &seen_a_one
, octa3
);
1576 /* Carry in one bit, no carry out. */
1578 octa1
= (carry
<< 2) | ((HIGH_TWO
& *p
) >> 6);
1579 octa2
= (MID_TWO
& *p
) >> 3;
1580 octa3
= (LOW_TWO
& *p
);
1582 emit_octal_digit (stream
, &seen_a_one
, octa1
);
1583 emit_octal_digit (stream
, &seen_a_one
, octa2
);
1584 emit_octal_digit (stream
, &seen_a_one
, octa3
);
1588 error (_("Internal error in octal conversion;"));
1592 cycle
= cycle
% BITS_IN_OCTAL
;
1597 for (p
= valaddr
+ len
- 1;
1604 /* Carry out, no carry in */
1606 octa1
= (HIGH_ZERO
& *p
) >> 5;
1607 octa2
= (LOW_ZERO
& *p
) >> 2;
1608 carry
= (CARRY_ZERO
& *p
);
1609 emit_octal_digit (stream
, &seen_a_one
, octa1
);
1610 emit_octal_digit (stream
, &seen_a_one
, octa2
);
1614 /* Carry in, carry out */
1616 octa1
= (carry
<< 1) | ((HIGH_ONE
& *p
) >> 7);
1617 octa2
= (MID_ONE
& *p
) >> 4;
1618 octa3
= (LOW_ONE
& *p
) >> 1;
1619 carry
= (CARRY_ONE
& *p
);
1620 emit_octal_digit (stream
, &seen_a_one
, octa1
);
1621 emit_octal_digit (stream
, &seen_a_one
, octa2
);
1622 emit_octal_digit (stream
, &seen_a_one
, octa3
);
1626 /* Carry in, no carry out */
1628 octa1
= (carry
<< 2) | ((HIGH_TWO
& *p
) >> 6);
1629 octa2
= (MID_TWO
& *p
) >> 3;
1630 octa3
= (LOW_TWO
& *p
);
1632 emit_octal_digit (stream
, &seen_a_one
, octa1
);
1633 emit_octal_digit (stream
, &seen_a_one
, octa2
);
1634 emit_octal_digit (stream
, &seen_a_one
, octa3
);
1638 error (_("Internal error in octal conversion;"));
1642 cycle
= cycle
% BITS_IN_OCTAL
;
1648 /* Possibly negate the integer represented by BYTES. It contains LEN
1649 bytes in the specified byte order. If the integer is negative,
1650 copy it into OUT_VEC, negate it, and return true. Otherwise, do
1651 nothing and return false. */
1654 maybe_negate_by_bytes (const gdb_byte
*bytes
, unsigned len
,
1655 enum bfd_endian byte_order
,
1656 gdb::byte_vector
*out_vec
)
1659 gdb_assert (len
> 0);
1660 if (byte_order
== BFD_ENDIAN_BIG
)
1661 sign_byte
= bytes
[0];
1663 sign_byte
= bytes
[len
- 1];
1664 if ((sign_byte
& 0x80) == 0)
1667 out_vec
->resize (len
);
1669 /* Compute -x == 1 + ~x. */
1670 if (byte_order
== BFD_ENDIAN_LITTLE
)
1673 for (unsigned i
= 0; i
< len
; ++i
)
1675 unsigned tem
= (0xff & ~bytes
[i
]) + carry
;
1676 (*out_vec
)[i
] = tem
& 0xff;
1683 for (unsigned i
= len
; i
> 0; --i
)
1685 unsigned tem
= (0xff & ~bytes
[i
- 1]) + carry
;
1686 (*out_vec
)[i
- 1] = tem
& 0xff;
1694 /* VALADDR points to an integer of LEN bytes.
1695 Print it in decimal on stream or format it in buf. */
1698 print_decimal_chars (struct ui_file
*stream
, const gdb_byte
*valaddr
,
1699 unsigned len
, bool is_signed
,
1700 enum bfd_endian byte_order
)
1703 #define CARRY_OUT( x ) ((x) / TEN) /* extend char to int */
1704 #define CARRY_LEFT( x ) ((x) % TEN)
1705 #define SHIFT( x ) ((x) << 4)
1706 #define LOW_NIBBLE( x ) ( (x) & 0x00F)
1707 #define HIGH_NIBBLE( x ) (((x) & 0x0F0) >> 4)
1712 int i
, j
, decimal_digits
;
1716 gdb::byte_vector negated_bytes
;
1718 && maybe_negate_by_bytes (valaddr
, len
, byte_order
, &negated_bytes
))
1720 gdb_puts ("-", stream
);
1721 valaddr
= negated_bytes
.data ();
1724 /* Base-ten number is less than twice as many digits
1725 as the base 16 number, which is 2 digits per byte. */
1727 decimal_len
= len
* 2 * 2;
1728 std::vector
<unsigned char> digits (decimal_len
, 0);
1730 /* Ok, we have an unknown number of bytes of data to be printed in
1733 * Given a hex number (in nibbles) as XYZ, we start by taking X and
1734 * decimalizing it as "x1 x2" in two decimal nibbles. Then we multiply
1735 * the nibbles by 16, add Y and re-decimalize. Repeat with Z.
1737 * The trick is that "digits" holds a base-10 number, but sometimes
1738 * the individual digits are > 10.
1740 * Outer loop is per nibble (hex digit) of input, from MSD end to
1743 decimal_digits
= 0; /* Number of decimal digits so far */
1744 p
= (byte_order
== BFD_ENDIAN_BIG
) ? valaddr
: valaddr
+ len
- 1;
1746 while ((byte_order
== BFD_ENDIAN_BIG
) ? (p
< valaddr
+ len
) : (p
>= valaddr
))
1749 * Multiply current base-ten number by 16 in place.
1750 * Each digit was between 0 and 9, now is between
1753 for (j
= 0; j
< decimal_digits
; j
++)
1755 digits
[j
] = SHIFT (digits
[j
]);
1758 /* Take the next nibble off the input and add it to what
1759 * we've got in the LSB position. Bottom 'digit' is now
1760 * between 0 and 159.
1762 * "flip" is used to run this loop twice for each byte.
1766 /* Take top nibble. */
1768 digits
[0] += HIGH_NIBBLE (*p
);
1773 /* Take low nibble and bump our pointer "p". */
1775 digits
[0] += LOW_NIBBLE (*p
);
1776 if (byte_order
== BFD_ENDIAN_BIG
)
1783 /* Re-decimalize. We have to do this often enough
1784 * that we don't overflow, but once per nibble is
1785 * overkill. Easier this way, though. Note that the
1786 * carry is often larger than 10 (e.g. max initial
1787 * carry out of lowest nibble is 15, could bubble all
1788 * the way up greater than 10). So we have to do
1789 * the carrying beyond the last current digit.
1792 for (j
= 0; j
< decimal_len
- 1; j
++)
1796 /* "/" won't handle an unsigned char with
1797 * a value that if signed would be negative.
1798 * So extend to longword int via "dummy".
1801 carry
= CARRY_OUT (dummy
);
1802 digits
[j
] = CARRY_LEFT (dummy
);
1804 if (j
>= decimal_digits
&& carry
== 0)
1807 * All higher digits are 0 and we
1808 * no longer have a carry.
1810 * Note: "j" is 0-based, "decimal_digits" is
1813 decimal_digits
= j
+ 1;
1819 /* Ok, now "digits" is the decimal representation, with
1820 the "decimal_digits" actual digits. Print! */
1822 for (i
= decimal_digits
- 1; i
> 0 && digits
[i
] == 0; --i
)
1827 gdb_printf (stream
, "%1d", digits
[i
]);
1831 /* VALADDR points to an integer of LEN bytes. Print it in hex on stream. */
1834 print_hex_chars (struct ui_file
*stream
, const gdb_byte
*valaddr
,
1835 unsigned len
, enum bfd_endian byte_order
,
1840 gdb_puts ("0x", stream
);
1841 if (byte_order
== BFD_ENDIAN_BIG
)
1847 /* Strip leading 0 bytes, but be sure to leave at least a
1848 single byte at the end. */
1849 for (; p
< valaddr
+ len
- 1 && !*p
; ++p
)
1853 const gdb_byte
*first
= p
;
1858 /* When not zero-padding, use a different format for the
1859 very first byte printed. */
1860 if (!zero_pad
&& p
== first
)
1861 gdb_printf (stream
, "%x", *p
);
1863 gdb_printf (stream
, "%02x", *p
);
1868 p
= valaddr
+ len
- 1;
1872 /* Strip leading 0 bytes, but be sure to leave at least a
1873 single byte at the end. */
1874 for (; p
>= valaddr
+ 1 && !*p
; --p
)
1878 const gdb_byte
*first
= p
;
1883 /* When not zero-padding, use a different format for the
1884 very first byte printed. */
1885 if (!zero_pad
&& p
== first
)
1886 gdb_printf (stream
, "%x", *p
);
1888 gdb_printf (stream
, "%02x", *p
);
1893 /* Print function pointer with inferior address ADDRESS onto stdio
1897 print_function_pointer_address (const struct value_print_options
*options
,
1898 struct gdbarch
*gdbarch
,
1900 struct ui_file
*stream
)
1902 CORE_ADDR func_addr
= gdbarch_convert_from_func_ptr_addr
1903 (gdbarch
, address
, current_inferior ()->top_target ());
1905 /* If the function pointer is represented by a description, print
1906 the address of the description. */
1907 if (options
->addressprint
&& func_addr
!= address
)
1909 gdb_puts ("@", stream
);
1910 gdb_puts (paddress (gdbarch
, address
), stream
);
1911 gdb_puts (": ", stream
);
1913 print_address_demangle (options
, gdbarch
, func_addr
, stream
, demangle
);
1917 /* Print on STREAM using the given OPTIONS the index for the element
1918 at INDEX of an array whose index type is INDEX_TYPE. */
1921 maybe_print_array_index (struct type
*index_type
, LONGEST index
,
1922 struct ui_file
*stream
,
1923 const struct value_print_options
*options
)
1925 if (!options
->print_array_indexes
)
1928 current_language
->print_array_index (index_type
, index
, stream
, options
);
1931 /* See valprint.h. */
1934 value_print_array_elements (struct value
*val
, struct ui_file
*stream
,
1936 const struct value_print_options
*options
,
1939 unsigned int things_printed
= 0;
1941 struct type
*elttype
, *index_type
;
1942 /* Position of the array element we are examining to see
1943 whether it is repeated. */
1945 /* Number of repetitions we have detected so far. */
1947 LONGEST low_bound
, high_bound
;
1949 struct type
*type
= check_typedef (val
->type ());
1951 elttype
= type
->target_type ();
1952 unsigned bit_stride
= type
->bit_stride ();
1953 if (bit_stride
== 0)
1954 bit_stride
= 8 * check_typedef (elttype
)->length ();
1955 index_type
= type
->index_type ();
1956 if (index_type
->code () == TYPE_CODE_RANGE
)
1957 index_type
= index_type
->target_type ();
1959 if (get_array_bounds (type
, &low_bound
, &high_bound
))
1961 /* The array length should normally be HIGH_BOUND - LOW_BOUND +
1962 1. But we have to be a little extra careful, because some
1963 languages such as Ada allow LOW_BOUND to be greater than
1964 HIGH_BOUND for empty arrays. In that situation, the array
1965 length is just zero, not negative! */
1966 if (low_bound
> high_bound
)
1969 len
= high_bound
- low_bound
+ 1;
1973 warning (_("unable to get bounds of array, assuming null array"));
1978 annotate_array_section_begin (i
, elttype
);
1980 for (; i
< len
&& things_printed
< options
->print_max
; i
++)
1982 scoped_value_mark free_values
;
1986 if (options
->prettyformat_arrays
)
1988 gdb_printf (stream
, ",\n");
1989 print_spaces (2 + 2 * recurse
, stream
);
1992 gdb_printf (stream
, ", ");
1994 else if (options
->prettyformat_arrays
)
1996 gdb_printf (stream
, "\n");
1997 print_spaces (2 + 2 * recurse
, stream
);
1999 stream
->wrap_here (2 + 2 * recurse
);
2000 maybe_print_array_index (index_type
, i
+ low_bound
,
2003 struct value
*element
= val
->from_component_bitsize (elttype
,
2008 /* Only check for reps if repeat_count_threshold is not set to
2009 UINT_MAX (unlimited). */
2010 if (options
->repeat_count_threshold
< UINT_MAX
)
2012 bool unavailable
= element
->entirely_unavailable ();
2013 bool available
= element
->entirely_available ();
2017 /* When printing large arrays this spot is called frequently, so
2018 clean up temporary values asap to prevent allocating a large
2020 scoped_value_mark free_values_inner
;
2021 struct value
*rep_elt
2022 = val
->from_component_bitsize (elttype
,
2025 bool repeated
= ((available
2026 && rep_elt
->entirely_available ()
2027 && element
->contents_eq (rep_elt
))
2029 && rep_elt
->entirely_unavailable ()));
2037 common_val_print (element
, stream
, recurse
+ 1, options
,
2040 if (reps
> options
->repeat_count_threshold
)
2042 annotate_elt_rep (reps
);
2043 gdb_printf (stream
, " %p[<repeats %u times>%p]",
2044 metadata_style
.style ().ptr (), reps
, nullptr);
2045 annotate_elt_rep_end ();
2048 things_printed
+= options
->repeat_count_threshold
;
2056 annotate_array_section_end ();
2058 gdb_printf (stream
, "...");
2059 if (options
->prettyformat_arrays
)
2061 gdb_printf (stream
, "\n");
2062 print_spaces (2 * recurse
, stream
);
2066 /* Return true if print_wchar can display W without resorting to a
2067 numeric escape, false otherwise. */
2070 wchar_printable (gdb_wchar_t w
)
2072 return (gdb_iswprint (w
)
2073 || w
== LCST ('\a') || w
== LCST ('\b')
2074 || w
== LCST ('\f') || w
== LCST ('\n')
2075 || w
== LCST ('\r') || w
== LCST ('\t')
2076 || w
== LCST ('\v'));
2079 /* A helper function that converts the contents of STRING to wide
2080 characters and then appends them to OUTPUT. */
2083 append_string_as_wide (const char *string
,
2084 struct obstack
*output
)
2086 for (; *string
; ++string
)
2088 gdb_wchar_t w
= gdb_btowc (*string
);
2089 obstack_grow (output
, &w
, sizeof (gdb_wchar_t
));
2093 /* Print a wide character W to OUTPUT. ORIG is a pointer to the
2094 original (target) bytes representing the character, ORIG_LEN is the
2095 number of valid bytes. WIDTH is the number of bytes in a base
2096 characters of the type. OUTPUT is an obstack to which wide
2097 characters are emitted. QUOTER is a (narrow) character indicating
2098 the style of quotes surrounding the character to be printed.
2099 NEED_ESCAPE is an in/out flag which is used to track numeric
2100 escapes across calls. */
2103 print_wchar (gdb_wint_t w
, const gdb_byte
*orig
,
2104 int orig_len
, int width
,
2105 enum bfd_endian byte_order
,
2106 struct obstack
*output
,
2107 int quoter
, bool *need_escapep
)
2109 bool need_escape
= *need_escapep
;
2111 *need_escapep
= false;
2113 /* If any additional cases are added to this switch block, then the
2114 function wchar_printable will likely need updating too. */
2118 obstack_grow_wstr (output
, LCST ("\\a"));
2121 obstack_grow_wstr (output
, LCST ("\\b"));
2124 obstack_grow_wstr (output
, LCST ("\\f"));
2127 obstack_grow_wstr (output
, LCST ("\\n"));
2130 obstack_grow_wstr (output
, LCST ("\\r"));
2133 obstack_grow_wstr (output
, LCST ("\\t"));
2136 obstack_grow_wstr (output
, LCST ("\\v"));
2140 if (gdb_iswprint (w
) && !(need_escape
&& gdb_iswxdigit (w
)))
2142 gdb_wchar_t wchar
= w
;
2144 if (w
== gdb_btowc (quoter
) || w
== LCST ('\\'))
2145 obstack_grow_wstr (output
, LCST ("\\"));
2146 obstack_grow (output
, &wchar
, sizeof (gdb_wchar_t
));
2152 for (i
= 0; i
+ width
<= orig_len
; i
+= width
)
2157 value
= extract_unsigned_integer (&orig
[i
], width
,
2159 /* If the value fits in 3 octal digits, print it that
2160 way. Otherwise, print it as a hex escape. */
2163 xsnprintf (octal
, sizeof (octal
), "\\%.3o",
2164 (int) (value
& 0777));
2165 *need_escapep
= false;
2169 xsnprintf (octal
, sizeof (octal
), "\\x%lx", (long) value
);
2170 /* A hex escape might require the next character
2171 to be escaped, because, unlike with octal,
2172 hex escapes have no length limit. */
2173 *need_escapep
= true;
2175 append_string_as_wide (octal
, output
);
2177 /* If we somehow have extra bytes, print them now. */
2178 while (i
< orig_len
)
2182 xsnprintf (octal
, sizeof (octal
), "\\%.3o", orig
[i
] & 0xff);
2183 *need_escapep
= false;
2184 append_string_as_wide (octal
, output
);
2193 /* Print the character C on STREAM as part of the contents of a
2194 literal string whose delimiter is QUOTER. ENCODING names the
2198 generic_emit_char (int c
, struct type
*type
, struct ui_file
*stream
,
2199 int quoter
, const char *encoding
)
2201 enum bfd_endian byte_order
2202 = type_byte_order (type
);
2204 bool need_escape
= false;
2206 c_buf
= (gdb_byte
*) alloca (type
->length ());
2207 pack_long (c_buf
, type
, c
);
2209 wchar_iterator
iter (c_buf
, type
->length (), encoding
, type
->length ());
2211 /* This holds the printable form of the wchar_t data. */
2212 auto_obstack wchar_buf
;
2218 const gdb_byte
*buf
;
2220 int print_escape
= 1;
2221 enum wchar_iterate_result result
;
2223 num_chars
= iter
.iterate (&result
, &chars
, &buf
, &buflen
);
2228 /* If all characters are printable, print them. Otherwise,
2229 we're going to have to print an escape sequence. We
2230 check all characters because we want to print the target
2231 bytes in the escape sequence, and we don't know character
2232 boundaries there. */
2236 for (i
= 0; i
< num_chars
; ++i
)
2237 if (!wchar_printable (chars
[i
]))
2245 for (i
= 0; i
< num_chars
; ++i
)
2246 print_wchar (chars
[i
], buf
, buflen
,
2247 type
->length (), byte_order
,
2248 &wchar_buf
, quoter
, &need_escape
);
2252 /* This handles the NUM_CHARS == 0 case as well. */
2254 print_wchar (gdb_WEOF
, buf
, buflen
, type
->length (),
2255 byte_order
, &wchar_buf
, quoter
, &need_escape
);
2258 /* The output in the host encoding. */
2259 auto_obstack output
;
2261 convert_between_encodings (INTERMEDIATE_ENCODING
, host_charset (),
2262 (gdb_byte
*) obstack_base (&wchar_buf
),
2263 obstack_object_size (&wchar_buf
),
2264 sizeof (gdb_wchar_t
), &output
, translit_char
);
2265 obstack_1grow (&output
, '\0');
2267 gdb_puts ((const char *) obstack_base (&output
), stream
);
2270 /* Return the repeat count of the next character/byte in ITER,
2271 storing the result in VEC. */
2274 count_next_character (wchar_iterator
*iter
,
2275 std::vector
<converted_character
> *vec
)
2277 struct converted_character
*current
;
2281 struct converted_character tmp
;
2285 = iter
->iterate (&tmp
.result
, &chars
, &tmp
.buf
, &tmp
.buflen
);
2286 if (tmp
.num_chars
> 0)
2288 gdb_assert (tmp
.num_chars
< MAX_WCHARS
);
2289 memcpy (tmp
.chars
, chars
, tmp
.num_chars
* sizeof (gdb_wchar_t
));
2291 vec
->push_back (tmp
);
2294 current
= &vec
->back ();
2296 /* Count repeated characters or bytes. */
2297 current
->repeat_count
= 1;
2298 if (current
->num_chars
== -1)
2306 struct converted_character d
;
2313 /* Get the next character. */
2314 d
.num_chars
= iter
->iterate (&d
.result
, &chars
, &d
.buf
, &d
.buflen
);
2316 /* If a character was successfully converted, save the character
2317 into the converted character. */
2318 if (d
.num_chars
> 0)
2320 gdb_assert (d
.num_chars
< MAX_WCHARS
);
2321 memcpy (d
.chars
, chars
, WCHAR_BUFLEN (d
.num_chars
));
2324 /* Determine if the current character is the same as this
2326 if (d
.num_chars
== current
->num_chars
&& d
.result
== current
->result
)
2328 /* There are two cases to consider:
2330 1) Equality of converted character (num_chars > 0)
2331 2) Equality of non-converted character (num_chars == 0) */
2332 if ((current
->num_chars
> 0
2333 && memcmp (current
->chars
, d
.chars
,
2334 WCHAR_BUFLEN (current
->num_chars
)) == 0)
2335 || (current
->num_chars
== 0
2336 && current
->buflen
== d
.buflen
2337 && memcmp (current
->buf
, d
.buf
, current
->buflen
) == 0))
2338 ++current
->repeat_count
;
2346 /* Push this next converted character onto the result vector. */
2347 repeat
= current
->repeat_count
;
2353 /* Print the characters in CHARS to the OBSTACK. QUOTE_CHAR is the quote
2354 character to use with string output. WIDTH is the size of the output
2355 character type. BYTE_ORDER is the target byte order. OPTIONS
2356 is the user's print options. *FINISHED is set to 0 if we didn't print
2357 all the elements in CHARS. */
2360 print_converted_chars_to_obstack (struct obstack
*obstack
,
2361 const std::vector
<converted_character
> &chars
,
2362 int quote_char
, int width
,
2363 enum bfd_endian byte_order
,
2364 const struct value_print_options
*options
,
2367 unsigned int idx
, num_elements
;
2368 const converted_character
*elem
;
2369 enum {START
, SINGLE
, REPEAT
, INCOMPLETE
, FINISH
} state
, last
;
2370 gdb_wchar_t wide_quote_char
= gdb_btowc (quote_char
);
2371 bool need_escape
= false;
2372 const int print_max
= options
->print_max_chars
> 0
2373 ? options
->print_max_chars
: options
->print_max
;
2375 /* Set the start state. */
2376 idx
= num_elements
= 0;
2377 last
= state
= START
;
2385 /* Nothing to do. */
2392 /* We are outputting a single character
2393 (< options->repeat_count_threshold). */
2397 /* We were outputting some other type of content, so we
2398 must output and a comma and a quote. */
2400 obstack_grow_wstr (obstack
, LCST (", "));
2401 obstack_grow (obstack
, &wide_quote_char
, sizeof (gdb_wchar_t
));
2403 /* Output the character. */
2404 int repeat_count
= elem
->repeat_count
;
2405 if (print_max
< repeat_count
+ num_elements
)
2407 repeat_count
= print_max
- num_elements
;
2410 for (j
= 0; j
< repeat_count
; ++j
)
2412 if (elem
->result
== wchar_iterate_ok
)
2413 print_wchar (elem
->chars
[0], elem
->buf
, elem
->buflen
, width
,
2414 byte_order
, obstack
, quote_char
, &need_escape
);
2416 print_wchar (gdb_WEOF
, elem
->buf
, elem
->buflen
, width
,
2417 byte_order
, obstack
, quote_char
, &need_escape
);
2427 /* We are outputting a character with a repeat count
2428 greater than options->repeat_count_threshold. */
2432 /* We were outputting a single string. Terminate the
2434 obstack_grow (obstack
, &wide_quote_char
, sizeof (gdb_wchar_t
));
2437 obstack_grow_wstr (obstack
, LCST (", "));
2439 /* Output the character and repeat string. */
2440 obstack_grow_wstr (obstack
, LCST ("'"));
2441 if (elem
->result
== wchar_iterate_ok
)
2442 print_wchar (elem
->chars
[0], elem
->buf
, elem
->buflen
, width
,
2443 byte_order
, obstack
, quote_char
, &need_escape
);
2445 print_wchar (gdb_WEOF
, elem
->buf
, elem
->buflen
, width
,
2446 byte_order
, obstack
, quote_char
, &need_escape
);
2447 obstack_grow_wstr (obstack
, LCST ("'"));
2448 std::string s
= string_printf (_(" <repeats %u times>"),
2449 elem
->repeat_count
);
2450 num_elements
+= elem
->repeat_count
;
2451 for (j
= 0; s
[j
]; ++j
)
2453 gdb_wchar_t w
= gdb_btowc (s
[j
]);
2454 obstack_grow (obstack
, &w
, sizeof (gdb_wchar_t
));
2460 /* We are outputting an incomplete sequence. */
2463 /* If we were outputting a string of SINGLE characters,
2464 terminate the quote. */
2465 obstack_grow (obstack
, &wide_quote_char
, sizeof (gdb_wchar_t
));
2468 obstack_grow_wstr (obstack
, LCST (", "));
2470 /* Output the incomplete sequence string. */
2471 obstack_grow_wstr (obstack
, LCST ("<incomplete sequence "));
2472 print_wchar (gdb_WEOF
, elem
->buf
, elem
->buflen
, width
, byte_order
,
2473 obstack
, 0, &need_escape
);
2474 obstack_grow_wstr (obstack
, LCST (">"));
2477 /* We do not attempt to output anything after this. */
2482 /* All done. If we were outputting a string of SINGLE
2483 characters, the string must be terminated. Otherwise,
2484 REPEAT and INCOMPLETE are always left properly terminated. */
2486 obstack_grow (obstack
, &wide_quote_char
, sizeof (gdb_wchar_t
));
2491 /* Get the next element and state. */
2493 if (state
!= FINISH
)
2495 elem
= &chars
[idx
++];
2496 switch (elem
->result
)
2498 case wchar_iterate_ok
:
2499 case wchar_iterate_invalid
:
2500 if (elem
->repeat_count
> options
->repeat_count_threshold
)
2506 case wchar_iterate_incomplete
:
2510 case wchar_iterate_eof
:
2518 /* Print the character string STRING, printing at most LENGTH
2519 characters. LENGTH is -1 if the string is nul terminated. TYPE is
2520 the type of each character. OPTIONS holds the printing options;
2521 printing stops early if the number hits print_max_chars; repeat
2522 counts are printed as appropriate. Print ellipses at the end if we
2523 had to stop before printing LENGTH characters, or if FORCE_ELLIPSES.
2524 QUOTE_CHAR is the character to print at each end of the string. If
2525 C_STYLE_TERMINATOR is true, and the last character is 0, then it is
2529 generic_printstr (struct ui_file
*stream
, struct type
*type
,
2530 const gdb_byte
*string
, unsigned int length
,
2531 const char *encoding
, int force_ellipses
,
2532 int quote_char
, int c_style_terminator
,
2533 const struct value_print_options
*options
)
2535 enum bfd_endian byte_order
= type_byte_order (type
);
2537 int width
= type
->length ();
2539 struct converted_character
*last
;
2543 unsigned long current_char
= 1;
2545 for (i
= 0; current_char
; ++i
)
2548 current_char
= extract_unsigned_integer (string
+ i
* width
,
2554 /* If the string was not truncated due to `set print elements', and
2555 the last byte of it is a null, we don't print that, in
2556 traditional C style. */
2557 if (c_style_terminator
2560 && (extract_unsigned_integer (string
+ (length
- 1) * width
,
2561 width
, byte_order
) == 0))
2566 gdb_printf (stream
, "%c%c", quote_char
, quote_char
);
2570 /* Arrange to iterate over the characters, in wchar_t form. */
2571 wchar_iterator
iter (string
, length
* width
, encoding
, width
);
2572 std::vector
<converted_character
> converted_chars
;
2574 /* Convert characters until the string is over or the maximum
2575 number of printed characters has been reached. */
2577 unsigned int print_max_chars
= get_print_max_chars (options
);
2578 while (i
< print_max_chars
)
2584 /* Grab the next character and repeat count. */
2585 r
= count_next_character (&iter
, &converted_chars
);
2587 /* If less than zero, the end of the input string was reached. */
2591 /* Otherwise, add the count to the total print count and get
2592 the next character. */
2596 /* Get the last element and determine if the entire string was
2598 last
= &converted_chars
.back ();
2599 finished
= (last
->result
== wchar_iterate_eof
);
2601 /* Ensure that CONVERTED_CHARS is terminated. */
2602 last
->result
= wchar_iterate_eof
;
2604 /* WCHAR_BUF is the obstack we use to represent the string in
2606 auto_obstack wchar_buf
;
2608 /* Print the output string to the obstack. */
2609 print_converted_chars_to_obstack (&wchar_buf
, converted_chars
, quote_char
,
2610 width
, byte_order
, options
, &finished
);
2612 if (force_ellipses
|| !finished
)
2613 obstack_grow_wstr (&wchar_buf
, LCST ("..."));
2615 /* OUTPUT is where we collect `char's for printing. */
2616 auto_obstack output
;
2618 convert_between_encodings (INTERMEDIATE_ENCODING
, host_charset (),
2619 (gdb_byte
*) obstack_base (&wchar_buf
),
2620 obstack_object_size (&wchar_buf
),
2621 sizeof (gdb_wchar_t
), &output
, translit_char
);
2622 obstack_1grow (&output
, '\0');
2624 gdb_puts ((const char *) obstack_base (&output
), stream
);
2627 /* Print a string from the inferior, starting at ADDR and printing up to LEN
2628 characters, of WIDTH bytes a piece, to STREAM. If LEN is -1, printing
2629 stops at the first null byte, otherwise printing proceeds (including null
2630 bytes) until either print_max_chars or LEN characters have been printed,
2631 whichever is smaller. ENCODING is the name of the string's
2632 encoding. It can be NULL, in which case the target encoding is
2636 val_print_string (struct type
*elttype
, const char *encoding
,
2637 CORE_ADDR addr
, int len
,
2638 struct ui_file
*stream
,
2639 const struct value_print_options
*options
)
2641 int force_ellipsis
= 0; /* Force ellipsis to be printed if nonzero. */
2642 int err
; /* Non-zero if we got a bad read. */
2643 int found_nul
; /* Non-zero if we found the nul char. */
2644 unsigned int fetchlimit
; /* Maximum number of chars to print. */
2646 gdb::unique_xmalloc_ptr
<gdb_byte
> buffer
; /* Dynamically growable fetch buffer. */
2647 struct gdbarch
*gdbarch
= elttype
->arch ();
2648 enum bfd_endian byte_order
= type_byte_order (elttype
);
2649 int width
= elttype
->length ();
2651 /* First we need to figure out the limit on the number of characters we are
2652 going to attempt to fetch and print. This is actually pretty simple.
2653 If LEN >= zero, then the limit is the minimum of LEN and print_max_chars.
2654 If LEN is -1, then the limit is print_max_chars. This is true regardless
2655 of whether print_max_chars is zero, UINT_MAX (unlimited), or something in
2656 between, because finding the null byte (or available memory) is what
2657 actually limits the fetch. */
2659 unsigned int print_max_chars
= get_print_max_chars (options
);
2660 fetchlimit
= (len
== -1
2662 : std::min ((unsigned) len
, print_max_chars
));
2664 err
= target_read_string (addr
, len
, width
, fetchlimit
,
2665 &buffer
, &bytes_read
);
2669 /* We now have either successfully filled the buffer to fetchlimit,
2670 or terminated early due to an error or finding a null char when
2673 /* Determine found_nul by looking at the last character read. */
2675 if (bytes_read
>= width
)
2676 found_nul
= extract_unsigned_integer (buffer
.get () + bytes_read
- width
,
2677 width
, byte_order
) == 0;
2678 if (len
== -1 && !found_nul
)
2682 /* We didn't find a NUL terminator we were looking for. Attempt
2683 to peek at the next character. If not successful, or it is not
2684 a null byte, then force ellipsis to be printed. */
2686 peekbuf
= (gdb_byte
*) alloca (width
);
2688 if (target_read_memory (addr
, peekbuf
, width
) == 0
2689 && extract_unsigned_integer (peekbuf
, width
, byte_order
) != 0)
2692 else if ((len
>= 0 && err
!= 0) || (len
> bytes_read
/ width
))
2694 /* Getting an error when we have a requested length, or fetching less
2695 than the number of characters actually requested, always make us
2700 /* If we get an error before fetching anything, don't print a string.
2701 But if we fetch something and then get an error, print the string
2702 and then the error message. */
2703 if (err
== 0 || bytes_read
> 0)
2704 current_language
->printstr (stream
, elttype
, buffer
.get (),
2706 encoding
, force_ellipsis
, options
);
2710 std::string str
= memory_error_message (TARGET_XFER_E_IO
, gdbarch
, addr
);
2712 gdb_printf (stream
, _("<error: %ps>"),
2713 styled_string (metadata_style
.style (),
2717 return (bytes_read
/ width
);
2720 /* Handle 'show print max-depth'. */
2723 show_print_max_depth (struct ui_file
*file
, int from_tty
,
2724 struct cmd_list_element
*c
, const char *value
)
2726 gdb_printf (file
, _("Maximum print depth is %s.\n"), value
);
2730 /* The 'set input-radix' command writes to this auxiliary variable.
2731 If the requested radix is valid, INPUT_RADIX is updated; otherwise,
2732 it is left unchanged. */
2734 static unsigned input_radix_1
= 10;
2736 /* Validate an input or output radix setting, and make sure the user
2737 knows what they really did here. Radix setting is confusing, e.g.
2738 setting the input radix to "10" never changes it! */
2741 set_input_radix (const char *args
, int from_tty
, struct cmd_list_element
*c
)
2743 set_input_radix_1 (from_tty
, input_radix_1
);
2747 set_input_radix_1 (int from_tty
, unsigned radix
)
2749 /* We don't currently disallow any input radix except 0 or 1, which don't
2750 make any mathematical sense. In theory, we can deal with any input
2751 radix greater than 1, even if we don't have unique digits for every
2752 value from 0 to radix-1, but in practice we lose on large radix values.
2753 We should either fix the lossage or restrict the radix range more.
2758 input_radix_1
= input_radix
;
2759 error (_("Nonsense input radix ``decimal %u''; input radix unchanged."),
2762 input_radix_1
= input_radix
= radix
;
2765 gdb_printf (_("Input radix now set to "
2766 "decimal %u, hex %x, octal %o.\n"),
2767 radix
, radix
, radix
);
2771 /* The 'set output-radix' command writes to this auxiliary variable.
2772 If the requested radix is valid, OUTPUT_RADIX is updated,
2773 otherwise, it is left unchanged. */
2775 static unsigned output_radix_1
= 10;
2778 set_output_radix (const char *args
, int from_tty
, struct cmd_list_element
*c
)
2780 set_output_radix_1 (from_tty
, output_radix_1
);
2784 set_output_radix_1 (int from_tty
, unsigned radix
)
2786 /* Validate the radix and disallow ones that we aren't prepared to
2787 handle correctly, leaving the radix unchanged. */
2791 user_print_options
.output_format
= 'x'; /* hex */
2794 user_print_options
.output_format
= 0; /* decimal */
2797 user_print_options
.output_format
= 'o'; /* octal */
2800 output_radix_1
= output_radix
;
2801 error (_("Unsupported output radix ``decimal %u''; "
2802 "output radix unchanged."),
2805 output_radix_1
= output_radix
= radix
;
2808 gdb_printf (_("Output radix now set to "
2809 "decimal %u, hex %x, octal %o.\n"),
2810 radix
, radix
, radix
);
2814 /* Set both the input and output radix at once. Try to set the output radix
2815 first, since it has the most restrictive range. An radix that is valid as
2816 an output radix is also valid as an input radix.
2818 It may be useful to have an unusual input radix. If the user wishes to
2819 set an input radix that is not valid as an output radix, he needs to use
2820 the 'set input-radix' command. */
2823 set_radix (const char *arg
, int from_tty
)
2827 radix
= (arg
== NULL
) ? 10 : parse_and_eval_long (arg
);
2828 set_output_radix_1 (0, radix
);
2829 set_input_radix_1 (0, radix
);
2832 gdb_printf (_("Input and output radices now set to "
2833 "decimal %u, hex %x, octal %o.\n"),
2834 radix
, radix
, radix
);
2838 /* Show both the input and output radices. */
2841 show_radix (const char *arg
, int from_tty
)
2845 if (input_radix
== output_radix
)
2847 gdb_printf (_("Input and output radices set to "
2848 "decimal %u, hex %x, octal %o.\n"),
2849 input_radix
, input_radix
, input_radix
);
2853 gdb_printf (_("Input radix set to decimal "
2854 "%u, hex %x, octal %o.\n"),
2855 input_radix
, input_radix
, input_radix
);
2856 gdb_printf (_("Output radix set to decimal "
2857 "%u, hex %x, octal %o.\n"),
2858 output_radix
, output_radix
, output_radix
);
2864 /* Controls printing of vtbl's. */
2866 show_vtblprint (struct ui_file
*file
, int from_tty
,
2867 struct cmd_list_element
*c
, const char *value
)
2869 gdb_printf (file
, _("\
2870 Printing of C++ virtual function tables is %s.\n"),
2874 /* Controls looking up an object's derived type using what we find in
2877 show_objectprint (struct ui_file
*file
, int from_tty
,
2878 struct cmd_list_element
*c
,
2881 gdb_printf (file
, _("\
2882 Printing of object's derived type based on vtable info is %s.\n"),
2887 show_static_field_print (struct ui_file
*file
, int from_tty
,
2888 struct cmd_list_element
*c
,
2892 _("Printing of C++ static members is %s.\n"),
2898 /* A couple typedefs to make writing the options a bit more
2900 using boolean_option_def
2901 = gdb::option::boolean_option_def
<value_print_options
>;
2902 using uinteger_option_def
2903 = gdb::option::uinteger_option_def
<value_print_options
>;
2904 using pinteger_option_def
2905 = gdb::option::pinteger_option_def
<value_print_options
>;
2907 /* Extra literals supported with the `set print characters' and
2908 `print -characters' commands. */
2909 static const literal_def print_characters_literals
[] =
2911 { "elements", PRINT_MAX_CHARS_ELEMENTS
},
2912 { "unlimited", PRINT_MAX_CHARS_UNLIMITED
, 0 },
2916 /* Definitions of options for the "print" and "compile print"
2918 static const gdb::option::option_def value_print_option_defs
[] = {
2920 boolean_option_def
{
2922 [] (value_print_options
*opt
) { return &opt
->addressprint
; },
2923 show_addressprint
, /* show_cmd_cb */
2924 N_("Set printing of addresses."),
2925 N_("Show printing of addresses."),
2926 NULL
, /* help_doc */
2929 boolean_option_def
{
2931 [] (value_print_options
*opt
) { return &opt
->prettyformat_arrays
; },
2932 show_prettyformat_arrays
, /* show_cmd_cb */
2933 N_("Set pretty formatting of arrays."),
2934 N_("Show pretty formatting of arrays."),
2935 NULL
, /* help_doc */
2938 boolean_option_def
{
2940 [] (value_print_options
*opt
) { return &opt
->print_array_indexes
; },
2941 show_print_array_indexes
, /* show_cmd_cb */
2942 N_("Set printing of array indexes."),
2943 N_("Show printing of array indexes."),
2944 NULL
, /* help_doc */
2947 boolean_option_def
{
2949 [] (value_print_options
*opt
) { return &opt
->nibblesprint
; },
2950 show_nibbles
, /* show_cmd_cb */
2951 N_("Set whether to print binary values in groups of four bits."),
2952 N_("Show whether to print binary values in groups of four bits."),
2953 NULL
, /* help_doc */
2956 uinteger_option_def
{
2958 [] (value_print_options
*opt
) { return &opt
->print_max_chars
; },
2959 print_characters_literals
,
2960 show_print_max_chars
, /* show_cmd_cb */
2961 N_("Set limit on string chars to print."),
2962 N_("Show limit on string chars to print."),
2963 N_("\"elements\" causes the array element limit to be used.\n"
2964 "\"unlimited\" causes there to be no limit."),
2967 uinteger_option_def
{
2969 [] (value_print_options
*opt
) { return &opt
->print_max
; },
2970 uinteger_unlimited_literals
,
2971 show_print_max
, /* show_cmd_cb */
2972 N_("Set limit on array elements to print."),
2973 N_("Show limit on array elements to print."),
2974 N_("\"unlimited\" causes there to be no limit.\n"
2975 "This setting also applies to string chars when \"print characters\"\n"
2976 "is set to \"elements\"."),
2979 pinteger_option_def
{
2981 [] (value_print_options
*opt
) { return &opt
->max_depth
; },
2982 pinteger_unlimited_literals
,
2983 show_print_max_depth
, /* show_cmd_cb */
2984 N_("Set maximum print depth for nested structures, unions and arrays."),
2985 N_("Show maximum print depth for nested structures, unions, and arrays."),
2986 N_("When structures, unions, or arrays are nested beyond this depth then they\n\
2987 will be replaced with either '{...}' or '(...)' depending on the language.\n\
2988 Use \"unlimited\" to print the complete structure.")
2991 boolean_option_def
{
2992 "memory-tag-violations",
2993 [] (value_print_options
*opt
) { return &opt
->memory_tag_violations
; },
2994 show_memory_tag_violations
, /* show_cmd_cb */
2995 N_("Set printing of memory tag violations for pointers."),
2996 N_("Show printing of memory tag violations for pointers."),
2997 N_("Issue a warning when the printed value is a pointer\n\
2998 whose logical tag doesn't match the allocation tag of the memory\n\
2999 location it points to."),
3002 boolean_option_def
{
3004 [] (value_print_options
*opt
) { return &opt
->stop_print_at_null
; },
3005 show_stop_print_at_null
, /* show_cmd_cb */
3006 N_("Set printing of char arrays to stop at first null char."),
3007 N_("Show printing of char arrays to stop at first null char."),
3008 NULL
, /* help_doc */
3011 boolean_option_def
{
3013 [] (value_print_options
*opt
) { return &opt
->objectprint
; },
3014 show_objectprint
, /* show_cmd_cb */
3015 _("Set printing of C++ virtual function tables."),
3016 _("Show printing of C++ virtual function tables."),
3017 NULL
, /* help_doc */
3020 boolean_option_def
{
3022 [] (value_print_options
*opt
) { return &opt
->prettyformat_structs
; },
3023 show_prettyformat_structs
, /* show_cmd_cb */
3024 N_("Set pretty formatting of structures."),
3025 N_("Show pretty formatting of structures."),
3026 NULL
, /* help_doc */
3029 boolean_option_def
{
3031 [] (value_print_options
*opt
) { return &opt
->raw
; },
3032 NULL
, /* show_cmd_cb */
3033 N_("Set whether to print values in raw form."),
3034 N_("Show whether to print values in raw form."),
3035 N_("If set, values are printed in raw form, bypassing any\n\
3036 pretty-printers for that value.")
3039 uinteger_option_def
{
3041 [] (value_print_options
*opt
) { return &opt
->repeat_count_threshold
; },
3042 uinteger_unlimited_literals
,
3043 show_repeat_count_threshold
, /* show_cmd_cb */
3044 N_("Set threshold for repeated print elements."),
3045 N_("Show threshold for repeated print elements."),
3046 N_("\"unlimited\" causes all elements to be individually printed."),
3049 boolean_option_def
{
3051 [] (value_print_options
*opt
) { return &opt
->static_field_print
; },
3052 show_static_field_print
, /* show_cmd_cb */
3053 N_("Set printing of C++ static members."),
3054 N_("Show printing of C++ static members."),
3055 NULL
, /* help_doc */
3058 boolean_option_def
{
3060 [] (value_print_options
*opt
) { return &opt
->symbol_print
; },
3061 show_symbol_print
, /* show_cmd_cb */
3062 N_("Set printing of symbol names when printing pointers."),
3063 N_("Show printing of symbol names when printing pointers."),
3064 NULL
, /* help_doc */
3067 boolean_option_def
{
3069 [] (value_print_options
*opt
) { return &opt
->unionprint
; },
3070 show_unionprint
, /* show_cmd_cb */
3071 N_("Set printing of unions interior to structures."),
3072 N_("Show printing of unions interior to structures."),
3073 NULL
, /* help_doc */
3076 boolean_option_def
{
3078 [] (value_print_options
*opt
) { return &opt
->vtblprint
; },
3079 show_vtblprint
, /* show_cmd_cb */
3080 N_("Set printing of C++ virtual function tables."),
3081 N_("Show printing of C++ virtual function tables."),
3082 NULL
, /* help_doc */
3086 /* See valprint.h. */
3088 gdb::option::option_def_group
3089 make_value_print_options_def_group (value_print_options
*opts
)
3091 return {{value_print_option_defs
}, opts
};
3096 /* Test printing of TYPE_CODE_FLAGS values. */
3099 test_print_flags (gdbarch
*arch
)
3101 type
*flags_type
= arch_flags_type (arch
, "test_type", 32);
3102 type
*field_type
= builtin_type (arch
)->builtin_uint32
;
3105 Fields: CCCB BAAA */
3106 append_flags_type_field (flags_type
, 0, 3, field_type
, "A");
3107 append_flags_type_field (flags_type
, 3, 2, field_type
, "B");
3108 append_flags_type_field (flags_type
, 5, 3, field_type
, "C");
3110 value
*val
= value::allocate (flags_type
);
3111 gdb_byte
*contents
= val
->contents_writeable ().data ();
3112 store_unsigned_integer (contents
, 4, gdbarch_byte_order (arch
), 0xaa);
3115 val_print_type_code_flags (flags_type
, val
, 0, &out
);
3116 SELF_CHECK (out
.string () == "[ A=2 B=1 C=5 ]");
3121 void _initialize_valprint ();
3123 _initialize_valprint ()
3126 selftests::register_test_foreach_arch ("print-flags", test_print_flags
);
3129 set_show_commands setshow_print_cmds
3130 = add_setshow_prefix_cmd ("print", no_class
,
3131 _("Generic command for setting how things print."),
3132 _("Generic command for showing print settings."),
3133 &setprintlist
, &showprintlist
,
3134 &setlist
, &showlist
);
3135 add_alias_cmd ("p", setshow_print_cmds
.set
, no_class
, 1, &setlist
);
3136 /* Prefer set print to set prompt. */
3137 add_alias_cmd ("pr", setshow_print_cmds
.set
, no_class
, 1, &setlist
);
3138 add_alias_cmd ("p", setshow_print_cmds
.show
, no_class
, 1, &showlist
);
3139 add_alias_cmd ("pr", setshow_print_cmds
.show
, no_class
, 1, &showlist
);
3141 set_show_commands setshow_print_raw_cmds
3142 = add_setshow_prefix_cmd
3144 _("Generic command for setting what things to print in \"raw\" mode."),
3145 _("Generic command for showing \"print raw\" settings."),
3146 &setprintrawlist
, &showprintrawlist
, &setprintlist
, &showprintlist
);
3147 deprecate_cmd (setshow_print_raw_cmds
.set
, nullptr);
3148 deprecate_cmd (setshow_print_raw_cmds
.show
, nullptr);
3150 gdb::option::add_setshow_cmds_for_options
3151 (class_support
, &user_print_options
, value_print_option_defs
,
3152 &setprintlist
, &showprintlist
);
3154 add_setshow_zuinteger_cmd ("input-radix", class_support
, &input_radix_1
,
3156 Set default input radix for entering numbers."), _("\
3157 Show default input radix for entering numbers."), NULL
,
3160 &setlist
, &showlist
);
3162 add_setshow_zuinteger_cmd ("output-radix", class_support
, &output_radix_1
,
3164 Set default output radix for printing of values."), _("\
3165 Show default output radix for printing of values."), NULL
,
3168 &setlist
, &showlist
);
3170 /* The "set radix" and "show radix" commands are special in that
3171 they are like normal set and show commands but allow two normally
3172 independent variables to be either set or shown with a single
3173 command. So the usual deprecated_add_set_cmd() and [deleted]
3174 add_show_from_set() commands aren't really appropriate. */
3175 /* FIXME: i18n: With the new add_setshow_integer command, that is no
3176 longer true - show can display anything. */
3177 add_cmd ("radix", class_support
, set_radix
, _("\
3178 Set default input and output number radices.\n\
3179 Use 'set input-radix' or 'set output-radix' to independently set each.\n\
3180 Without an argument, sets both radices back to the default value of 10."),
3182 add_cmd ("radix", class_support
, show_radix
, _("\
3183 Show the default input and output number radices.\n\
3184 Use 'show input-radix' or 'show output-radix' to independently show each."),