1 /* Print values for GDB, the GNU debugger.
3 Copyright (C) 1986-2023 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
30 #include "target-float.h"
31 #include "extension.h"
33 #include "gdbsupport/gdb_obstack.h"
35 #include "typeprint.h"
38 #include "gdbsupport/byte-vector.h"
39 #include "cli/cli-option.h"
41 #include "cli/cli-style.h"
42 #include "count-one-bits.h"
46 #include "gdbsupport/selftest.h"
47 #include "selftest-arch.h"
49 /* Maximum number of wchars returned from wchar_iterate. */
52 /* A convenience macro to compute the size of a wchar_t buffer containing X
54 #define WCHAR_BUFLEN(X) ((X) * sizeof (gdb_wchar_t))
56 /* Character buffer size saved while iterating over wchars. */
57 #define WCHAR_BUFLEN_MAX WCHAR_BUFLEN (MAX_WCHARS)
59 /* A structure to encapsulate state information from iterated
60 character conversions. */
61 struct converted_character
63 /* The number of characters converted. */
66 /* The result of the conversion. See charset.h for more. */
67 enum wchar_iterate_result result
;
69 /* The (saved) converted character(s). */
70 gdb_wchar_t chars
[WCHAR_BUFLEN_MAX
];
72 /* The first converted target byte. */
75 /* The number of bytes converted. */
78 /* How many times this character(s) is repeated. */
82 /* Command lists for set/show print raw. */
83 struct cmd_list_element
*setprintrawlist
;
84 struct cmd_list_element
*showprintrawlist
;
86 /* Prototypes for local functions */
88 static void set_input_radix_1 (int, unsigned);
90 static void set_output_radix_1 (int, unsigned);
92 static void val_print_type_code_flags (struct type
*type
,
93 struct value
*original_value
,
95 struct ui_file
*stream
);
97 /* Start print_max at this value. */
98 #define PRINT_MAX_DEFAULT 200
100 /* Start print_max_chars at this value (meaning follow print_max). */
101 #define PRINT_MAX_CHARS_DEFAULT PRINT_MAX_CHARS_ELEMENTS
103 /* Start print_max_depth at this value. */
104 #define PRINT_MAX_DEPTH_DEFAULT 20
106 struct value_print_options user_print_options
=
108 Val_prettyformat_default
, /* prettyformat */
109 false, /* prettyformat_arrays */
110 false, /* prettyformat_structs */
111 false, /* vtblprint */
112 true, /* unionprint */
113 true, /* addressprint */
114 false, /* nibblesprint */
115 false, /* objectprint */
116 PRINT_MAX_DEFAULT
, /* print_max */
117 PRINT_MAX_CHARS_DEFAULT
, /* print_max_chars */
118 10, /* repeat_count_threshold */
119 0, /* output_format */
121 true, /* memory_tag_violations */
122 false, /* stop_print_at_null */
123 false, /* print_array_indexes */
124 false, /* deref_ref */
125 true, /* static_field_print */
126 true, /* pascal_static_field_print */
129 true, /* symbol_print */
130 PRINT_MAX_DEPTH_DEFAULT
, /* max_depth */
133 /* Initialize *OPTS to be a copy of the user print options. */
135 get_user_print_options (struct value_print_options
*opts
)
137 *opts
= user_print_options
;
140 /* Initialize *OPTS to be a copy of the user print options, but with
141 pretty-formatting disabled. */
143 get_no_prettyformat_print_options (struct value_print_options
*opts
)
145 *opts
= user_print_options
;
146 opts
->prettyformat
= Val_no_prettyformat
;
149 /* Initialize *OPTS to be a copy of the user print options, but using
150 FORMAT as the formatting option. */
152 get_formatted_print_options (struct value_print_options
*opts
,
155 *opts
= user_print_options
;
156 opts
->format
= format
;
159 /* Implement 'show print elements'. */
162 show_print_max (struct ui_file
*file
, int from_tty
,
163 struct cmd_list_element
*c
, const char *value
)
167 (user_print_options
.print_max_chars
!= PRINT_MAX_CHARS_ELEMENTS
168 ? _("Limit on array elements to print is %s.\n")
169 : _("Limit on string chars or array elements to print is %s.\n")),
173 /* Implement 'show print characters'. */
176 show_print_max_chars (struct ui_file
*file
, int from_tty
,
177 struct cmd_list_element
*c
, const char *value
)
180 _("Limit on string characters to print is %s.\n"),
184 /* Default input and output radixes, and output format letter. */
186 unsigned input_radix
= 10;
188 show_input_radix (struct ui_file
*file
, int from_tty
,
189 struct cmd_list_element
*c
, const char *value
)
192 _("Default input radix for entering numbers is %s.\n"),
196 unsigned output_radix
= 10;
198 show_output_radix (struct ui_file
*file
, int from_tty
,
199 struct cmd_list_element
*c
, const char *value
)
202 _("Default output radix for printing of values is %s.\n"),
206 /* By default we print arrays without printing the index of each element in
207 the array. This behavior can be changed by setting PRINT_ARRAY_INDEXES. */
210 show_print_array_indexes (struct ui_file
*file
, int from_tty
,
211 struct cmd_list_element
*c
, const char *value
)
213 gdb_printf (file
, _("Printing of array indexes is %s.\n"), value
);
216 /* Print repeat counts if there are more than this many repetitions of an
217 element in an array. Referenced by the low level language dependent
221 show_repeat_count_threshold (struct ui_file
*file
, int from_tty
,
222 struct cmd_list_element
*c
, const char *value
)
224 gdb_printf (file
, _("Threshold for repeated print elements is %s.\n"),
228 /* If nonzero, prints memory tag violations for pointers. */
231 show_memory_tag_violations (struct ui_file
*file
, int from_tty
,
232 struct cmd_list_element
*c
, const char *value
)
235 _("Printing of memory tag violations is %s.\n"),
239 /* If nonzero, stops printing of char arrays at first null. */
242 show_stop_print_at_null (struct ui_file
*file
, int from_tty
,
243 struct cmd_list_element
*c
, const char *value
)
246 _("Printing of char arrays to stop "
247 "at first null char is %s.\n"),
251 /* Controls pretty printing of structures. */
254 show_prettyformat_structs (struct ui_file
*file
, int from_tty
,
255 struct cmd_list_element
*c
, const char *value
)
257 gdb_printf (file
, _("Pretty formatting of structures is %s.\n"), value
);
260 /* Controls pretty printing of arrays. */
263 show_prettyformat_arrays (struct ui_file
*file
, int from_tty
,
264 struct cmd_list_element
*c
, const char *value
)
266 gdb_printf (file
, _("Pretty formatting of arrays is %s.\n"), value
);
269 /* If nonzero, causes unions inside structures or other unions to be
273 show_unionprint (struct ui_file
*file
, int from_tty
,
274 struct cmd_list_element
*c
, const char *value
)
277 _("Printing of unions interior to structures is %s.\n"),
281 /* Controls the format of printing binary values. */
284 show_nibbles (struct ui_file
*file
, int from_tty
,
285 struct cmd_list_element
*c
, const char *value
)
288 _("Printing binary values in groups is %s.\n"),
292 /* If nonzero, causes machine addresses to be printed in certain contexts. */
295 show_addressprint (struct ui_file
*file
, int from_tty
,
296 struct cmd_list_element
*c
, const char *value
)
298 gdb_printf (file
, _("Printing of addresses is %s.\n"), value
);
302 show_symbol_print (struct ui_file
*file
, int from_tty
,
303 struct cmd_list_element
*c
, const char *value
)
306 _("Printing of symbols when printing pointers is %s.\n"),
312 /* A helper function for val_print. When printing in "summary" mode,
313 we want to print scalar arguments, but not aggregate arguments.
314 This function distinguishes between the two. */
317 val_print_scalar_type_p (struct type
*type
)
319 type
= check_typedef (type
);
320 while (TYPE_IS_REFERENCE (type
))
322 type
= type
->target_type ();
323 type
= check_typedef (type
);
325 switch (type
->code ())
327 case TYPE_CODE_ARRAY
:
328 case TYPE_CODE_STRUCT
:
329 case TYPE_CODE_UNION
:
331 case TYPE_CODE_STRING
:
338 /* A helper function for val_print. When printing with limited depth we
339 want to print string and scalar arguments, but not aggregate arguments.
340 This function distinguishes between the two. */
343 val_print_scalar_or_string_type_p (struct type
*type
,
344 const struct language_defn
*language
)
346 return (val_print_scalar_type_p (type
)
347 || language
->is_string_type_p (type
));
350 /* See valprint.h. */
353 valprint_check_validity (struct ui_file
*stream
,
355 LONGEST embedded_offset
,
356 const struct value
*val
)
358 type
= check_typedef (type
);
360 if (type_not_associated (type
))
362 val_print_not_associated (stream
);
366 if (type_not_allocated (type
))
368 val_print_not_allocated (stream
);
372 if (type
->code () != TYPE_CODE_UNION
373 && type
->code () != TYPE_CODE_STRUCT
374 && type
->code () != TYPE_CODE_ARRAY
)
376 if (value_bits_any_optimized_out (val
,
377 TARGET_CHAR_BIT
* embedded_offset
,
378 TARGET_CHAR_BIT
* type
->length ()))
380 val_print_optimized_out (val
, stream
);
384 if (value_bits_synthetic_pointer (val
, TARGET_CHAR_BIT
* embedded_offset
,
385 TARGET_CHAR_BIT
* type
->length ()))
387 const int is_ref
= type
->code () == TYPE_CODE_REF
;
388 int ref_is_addressable
= 0;
392 const struct value
*deref_val
= coerce_ref_if_computed (val
);
394 if (deref_val
!= NULL
)
395 ref_is_addressable
= value_lval_const (deref_val
) == lval_memory
;
398 if (!is_ref
|| !ref_is_addressable
)
399 fputs_styled (_("<synthetic pointer>"), metadata_style
.style (),
402 /* C++ references should be valid even if they're synthetic. */
406 if (!value_bytes_available (val
, embedded_offset
, type
->length ()))
408 val_print_unavailable (stream
);
417 val_print_optimized_out (const struct value
*val
, struct ui_file
*stream
)
419 if (val
!= NULL
&& value_lval_const (val
) == lval_register
)
420 val_print_not_saved (stream
);
422 fprintf_styled (stream
, metadata_style
.style (), _("<optimized out>"));
426 val_print_not_saved (struct ui_file
*stream
)
428 fprintf_styled (stream
, metadata_style
.style (), _("<not saved>"));
432 val_print_unavailable (struct ui_file
*stream
)
434 fprintf_styled (stream
, metadata_style
.style (), _("<unavailable>"));
438 val_print_invalid_address (struct ui_file
*stream
)
440 fprintf_styled (stream
, metadata_style
.style (), _("<invalid address>"));
443 /* Print a pointer based on the type of its target.
445 Arguments to this functions are roughly the same as those in
446 generic_val_print. A difference is that ADDRESS is the address to print,
447 with embedded_offset already added. ELTTYPE represents
448 the pointed type after check_typedef. */
451 print_unpacked_pointer (struct type
*type
, struct type
*elttype
,
452 CORE_ADDR address
, struct ui_file
*stream
,
453 const struct value_print_options
*options
)
455 struct gdbarch
*gdbarch
= type
->arch ();
457 if (elttype
->code () == TYPE_CODE_FUNC
)
459 /* Try to print what function it points to. */
460 print_function_pointer_address (options
, gdbarch
, address
, stream
);
464 if (options
->symbol_print
)
465 print_address_demangle (options
, gdbarch
, address
, stream
, demangle
);
466 else if (options
->addressprint
)
467 gdb_puts (paddress (gdbarch
, address
), stream
);
470 /* generic_val_print helper for TYPE_CODE_ARRAY. */
473 generic_val_print_array (struct value
*val
,
474 struct ui_file
*stream
, int recurse
,
475 const struct value_print_options
*options
,
477 generic_val_print_decorations
*decorations
)
479 struct type
*type
= check_typedef (value_type (val
));
480 struct type
*unresolved_elttype
= type
->target_type ();
481 struct type
*elttype
= check_typedef (unresolved_elttype
);
483 if (type
->length () > 0 && unresolved_elttype
->length () > 0)
485 LONGEST low_bound
, high_bound
;
487 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
488 error (_("Could not determine the array high bound"));
490 gdb_puts (decorations
->array_start
, stream
);
491 value_print_array_elements (val
, stream
, recurse
, options
, 0);
492 gdb_puts (decorations
->array_end
, stream
);
496 /* Array of unspecified length: treat like pointer to first elt. */
497 print_unpacked_pointer (type
, elttype
, value_address (val
),
503 /* generic_value_print helper for TYPE_CODE_PTR. */
506 generic_value_print_ptr (struct value
*val
, struct ui_file
*stream
,
507 const struct value_print_options
*options
)
510 if (options
->format
&& options
->format
!= 's')
511 value_print_scalar_formatted (val
, options
, 0, stream
);
514 struct type
*type
= check_typedef (value_type (val
));
515 struct type
*elttype
= check_typedef (type
->target_type ());
516 const gdb_byte
*valaddr
= value_contents_for_printing (val
).data ();
517 CORE_ADDR addr
= unpack_pointer (type
, valaddr
);
519 print_unpacked_pointer (type
, elttype
, addr
, stream
, options
);
524 /* Print '@' followed by the address contained in ADDRESS_BUFFER. */
527 print_ref_address (struct type
*type
, const gdb_byte
*address_buffer
,
528 int embedded_offset
, struct ui_file
*stream
)
530 struct gdbarch
*gdbarch
= type
->arch ();
532 if (address_buffer
!= NULL
)
535 = extract_typed_address (address_buffer
+ embedded_offset
, type
);
537 gdb_printf (stream
, "@");
538 gdb_puts (paddress (gdbarch
, address
), stream
);
540 /* Else: we have a non-addressable value, such as a DW_AT_const_value. */
543 /* If VAL is addressable, return the value contents buffer of a value that
544 represents a pointer to VAL. Otherwise return NULL. */
546 static const gdb_byte
*
547 get_value_addr_contents (struct value
*deref_val
)
549 gdb_assert (deref_val
!= NULL
);
551 if (value_lval_const (deref_val
) == lval_memory
)
552 return value_contents_for_printing_const (value_addr (deref_val
)).data ();
555 /* We have a non-addressable value, such as a DW_AT_const_value. */
560 /* generic_val_print helper for TYPE_CODE_{RVALUE_,}REF. */
563 generic_val_print_ref (struct type
*type
,
564 int embedded_offset
, struct ui_file
*stream
, int recurse
,
565 struct value
*original_value
,
566 const struct value_print_options
*options
)
568 struct type
*elttype
= check_typedef (type
->target_type ());
569 struct value
*deref_val
= NULL
;
570 const int value_is_synthetic
571 = value_bits_synthetic_pointer (original_value
,
572 TARGET_CHAR_BIT
* embedded_offset
,
573 TARGET_CHAR_BIT
* type
->length ());
574 const int must_coerce_ref
= ((options
->addressprint
&& value_is_synthetic
)
575 || options
->deref_ref
);
576 const int type_is_defined
= elttype
->code () != TYPE_CODE_UNDEF
;
577 const gdb_byte
*valaddr
= value_contents_for_printing (original_value
).data ();
579 if (must_coerce_ref
&& type_is_defined
)
581 deref_val
= coerce_ref_if_computed (original_value
);
583 if (deref_val
!= NULL
)
585 /* More complicated computed references are not supported. */
586 gdb_assert (embedded_offset
== 0);
589 deref_val
= value_at (type
->target_type (),
590 unpack_pointer (type
, valaddr
+ embedded_offset
));
592 /* Else, original_value isn't a synthetic reference or we don't have to print
593 the reference's contents.
595 Notice that for references to TYPE_CODE_STRUCT, 'set print object on' will
596 cause original_value to be a not_lval instead of an lval_computed,
597 which will make value_bits_synthetic_pointer return false.
598 This happens because if options->objectprint is true, c_value_print will
599 overwrite original_value's contents with the result of coercing
600 the reference through value_addr, and then set its type back to
601 TYPE_CODE_REF. In that case we don't have to coerce the reference again;
602 we can simply treat it as non-synthetic and move on. */
604 if (options
->addressprint
)
606 const gdb_byte
*address
= (value_is_synthetic
&& type_is_defined
607 ? get_value_addr_contents (deref_val
)
610 print_ref_address (type
, address
, embedded_offset
, stream
);
612 if (options
->deref_ref
)
613 gdb_puts (": ", stream
);
616 if (options
->deref_ref
)
619 common_val_print (deref_val
, stream
, recurse
, options
,
622 gdb_puts ("???", stream
);
626 /* Helper function for generic_val_print_enum.
627 This is also used to print enums in TYPE_CODE_FLAGS values. */
630 generic_val_print_enum_1 (struct type
*type
, LONGEST val
,
631 struct ui_file
*stream
)
636 len
= type
->num_fields ();
637 for (i
= 0; i
< len
; i
++)
640 if (val
== type
->field (i
).loc_enumval ())
647 fputs_styled (type
->field (i
).name (), variable_name_style
.style (),
650 else if (type
->is_flag_enum ())
654 /* We have a "flag" enum, so we try to decompose it into pieces as
655 appropriate. The enum may have multiple enumerators representing
656 the same bit, in which case we choose to only print the first one
658 for (i
= 0; i
< len
; ++i
)
662 ULONGEST enumval
= type
->field (i
).loc_enumval ();
663 int nbits
= count_one_bits_ll (enumval
);
665 gdb_assert (nbits
== 0 || nbits
== 1);
667 if ((val
& enumval
) != 0)
671 gdb_puts ("(", stream
);
675 gdb_puts (" | ", stream
);
677 val
&= ~type
->field (i
).loc_enumval ();
678 fputs_styled (type
->field (i
).name (),
679 variable_name_style
.style (), stream
);
685 /* There are leftover bits, print them. */
687 gdb_puts ("(", stream
);
689 gdb_puts (" | ", stream
);
691 gdb_puts ("unknown: 0x", stream
);
692 print_longest (stream
, 'x', 0, val
);
693 gdb_puts (")", stream
);
697 /* Nothing has been printed and the value is 0, the enum value must
699 gdb_puts ("0", stream
);
703 /* Something has been printed, close the parenthesis. */
704 gdb_puts (")", stream
);
708 print_longest (stream
, 'd', 0, val
);
711 /* generic_val_print helper for TYPE_CODE_ENUM. */
714 generic_val_print_enum (struct type
*type
,
715 int embedded_offset
, struct ui_file
*stream
,
716 struct value
*original_value
,
717 const struct value_print_options
*options
)
720 struct gdbarch
*gdbarch
= type
->arch ();
721 int unit_size
= gdbarch_addressable_memory_unit_size (gdbarch
);
723 gdb_assert (!options
->format
);
725 const gdb_byte
*valaddr
= value_contents_for_printing (original_value
).data ();
727 val
= unpack_long (type
, valaddr
+ embedded_offset
* unit_size
);
729 generic_val_print_enum_1 (type
, val
, stream
);
732 /* generic_val_print helper for TYPE_CODE_FUNC and TYPE_CODE_METHOD. */
735 generic_val_print_func (struct type
*type
,
736 int embedded_offset
, CORE_ADDR address
,
737 struct ui_file
*stream
,
738 struct value
*original_value
,
739 const struct value_print_options
*options
)
741 struct gdbarch
*gdbarch
= type
->arch ();
743 gdb_assert (!options
->format
);
745 /* FIXME, we should consider, at least for ANSI C language,
746 eliminating the distinction made between FUNCs and POINTERs to
748 gdb_printf (stream
, "{");
749 type_print (type
, "", stream
, -1);
750 gdb_printf (stream
, "} ");
751 /* Try to print what function it points to, and its address. */
752 print_address_demangle (options
, gdbarch
, address
, stream
, demangle
);
755 /* generic_value_print helper for TYPE_CODE_BOOL. */
758 generic_value_print_bool
759 (struct value
*value
, struct ui_file
*stream
,
760 const struct value_print_options
*options
,
761 const struct generic_val_print_decorations
*decorations
)
763 if (options
->format
|| options
->output_format
)
765 struct value_print_options opts
= *options
;
766 opts
.format
= (options
->format
? options
->format
767 : options
->output_format
);
768 value_print_scalar_formatted (value
, &opts
, 0, stream
);
772 const gdb_byte
*valaddr
= value_contents_for_printing (value
).data ();
773 struct type
*type
= check_typedef (value_type (value
));
774 LONGEST val
= unpack_long (type
, valaddr
);
776 gdb_puts (decorations
->false_name
, stream
);
778 gdb_puts (decorations
->true_name
, stream
);
780 print_longest (stream
, 'd', 0, val
);
784 /* generic_value_print helper for TYPE_CODE_INT. */
787 generic_value_print_int (struct value
*val
, struct ui_file
*stream
,
788 const struct value_print_options
*options
)
790 struct value_print_options opts
= *options
;
792 opts
.format
= (options
->format
? options
->format
793 : options
->output_format
);
794 value_print_scalar_formatted (val
, &opts
, 0, stream
);
797 /* generic_value_print helper for TYPE_CODE_CHAR. */
800 generic_value_print_char (struct value
*value
, struct ui_file
*stream
,
801 const struct value_print_options
*options
)
803 if (options
->format
|| options
->output_format
)
805 struct value_print_options opts
= *options
;
807 opts
.format
= (options
->format
? options
->format
808 : options
->output_format
);
809 value_print_scalar_formatted (value
, &opts
, 0, stream
);
813 struct type
*unresolved_type
= value_type (value
);
814 struct type
*type
= check_typedef (unresolved_type
);
815 const gdb_byte
*valaddr
= value_contents_for_printing (value
).data ();
817 LONGEST val
= unpack_long (type
, valaddr
);
818 if (type
->is_unsigned ())
819 gdb_printf (stream
, "%u", (unsigned int) val
);
821 gdb_printf (stream
, "%d", (int) val
);
822 gdb_puts (" ", stream
);
823 current_language
->printchar (val
, unresolved_type
, stream
);
827 /* generic_val_print helper for TYPE_CODE_FLT and TYPE_CODE_DECFLOAT. */
830 generic_val_print_float (struct type
*type
, struct ui_file
*stream
,
831 struct value
*original_value
,
832 const struct value_print_options
*options
)
834 gdb_assert (!options
->format
);
836 const gdb_byte
*valaddr
= value_contents_for_printing (original_value
).data ();
838 print_floating (valaddr
, type
, stream
);
841 /* generic_val_print helper for TYPE_CODE_FIXED_POINT. */
844 generic_val_print_fixed_point (struct value
*val
, struct ui_file
*stream
,
845 const struct value_print_options
*options
)
848 value_print_scalar_formatted (val
, options
, 0, stream
);
851 struct type
*type
= value_type (val
);
853 const gdb_byte
*valaddr
= value_contents_for_printing (val
).data ();
856 f
.read_fixed_point (gdb::make_array_view (valaddr
, type
->length ()),
857 type_byte_order (type
), type
->is_unsigned (),
858 type
->fixed_point_scaling_factor ());
860 const char *fmt
= type
->length () < 4 ? "%.11Fg" : "%.17Fg";
861 std::string str
= gmp_string_printf (fmt
, f
.val
);
862 gdb_printf (stream
, "%s", str
.c_str ());
866 /* generic_value_print helper for TYPE_CODE_COMPLEX. */
869 generic_value_print_complex (struct value
*val
, struct ui_file
*stream
,
870 const struct value_print_options
*options
,
871 const struct generic_val_print_decorations
874 gdb_printf (stream
, "%s", decorations
->complex_prefix
);
876 struct value
*real_part
= value_real_part (val
);
877 value_print_scalar_formatted (real_part
, options
, 0, stream
);
878 gdb_printf (stream
, "%s", decorations
->complex_infix
);
880 struct value
*imag_part
= value_imaginary_part (val
);
881 value_print_scalar_formatted (imag_part
, options
, 0, stream
);
882 gdb_printf (stream
, "%s", decorations
->complex_suffix
);
885 /* generic_value_print helper for TYPE_CODE_MEMBERPTR. */
888 generic_value_print_memberptr
889 (struct value
*val
, struct ui_file
*stream
,
891 const struct value_print_options
*options
,
892 const struct generic_val_print_decorations
*decorations
)
894 if (!options
->format
)
896 /* Member pointers are essentially specific to C++, and so if we
897 encounter one, we should print it according to C++ rules. */
898 struct type
*type
= check_typedef (value_type (val
));
899 const gdb_byte
*valaddr
= value_contents_for_printing (val
).data ();
900 cp_print_class_member (valaddr
, type
, stream
, "&");
903 value_print_scalar_formatted (val
, options
, 0, stream
);
906 /* See valprint.h. */
909 generic_value_print (struct value
*val
, struct ui_file
*stream
, int recurse
,
910 const struct value_print_options
*options
,
911 const struct generic_val_print_decorations
*decorations
)
913 struct type
*type
= value_type (val
);
915 type
= check_typedef (type
);
917 if (is_fixed_point_type (type
))
918 type
= type
->fixed_point_type_base_type ();
920 /* Widen a subrange to its target type, then use that type's
922 while (type
->code () == TYPE_CODE_RANGE
)
924 type
= check_typedef (type
->target_type ());
925 val
= value_cast (type
, val
);
928 switch (type
->code ())
930 case TYPE_CODE_ARRAY
:
931 generic_val_print_array (val
, stream
, recurse
, options
, decorations
);
934 case TYPE_CODE_MEMBERPTR
:
935 generic_value_print_memberptr (val
, stream
, recurse
, options
,
940 generic_value_print_ptr (val
, stream
, options
);
944 case TYPE_CODE_RVALUE_REF
:
945 generic_val_print_ref (type
, 0, stream
, recurse
,
951 value_print_scalar_formatted (val
, options
, 0, stream
);
953 generic_val_print_enum (type
, 0, stream
, val
, options
);
956 case TYPE_CODE_FLAGS
:
958 value_print_scalar_formatted (val
, options
, 0, stream
);
960 val_print_type_code_flags (type
, val
, 0, stream
);
964 case TYPE_CODE_METHOD
:
966 value_print_scalar_formatted (val
, options
, 0, stream
);
968 generic_val_print_func (type
, 0, value_address (val
), stream
,
973 generic_value_print_bool (val
, stream
, options
, decorations
);
977 generic_value_print_int (val
, stream
, options
);
981 generic_value_print_char (val
, stream
, options
);
985 case TYPE_CODE_DECFLOAT
:
987 value_print_scalar_formatted (val
, options
, 0, stream
);
989 generic_val_print_float (type
, stream
, val
, options
);
992 case TYPE_CODE_FIXED_POINT
:
993 generic_val_print_fixed_point (val
, stream
, options
);
997 gdb_puts (decorations
->void_name
, stream
);
1000 case TYPE_CODE_ERROR
:
1001 gdb_printf (stream
, "%s", TYPE_ERROR_NAME (type
));
1004 case TYPE_CODE_UNDEF
:
1005 /* This happens (without TYPE_STUB set) on systems which don't use
1006 dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
1007 and no complete type for struct foo in that file. */
1008 fprintf_styled (stream
, metadata_style
.style (), _("<incomplete type>"));
1011 case TYPE_CODE_COMPLEX
:
1012 generic_value_print_complex (val
, stream
, options
, decorations
);
1015 case TYPE_CODE_METHODPTR
:
1016 cplus_print_method_ptr (value_contents_for_printing (val
).data (), type
,
1020 case TYPE_CODE_UNION
:
1021 case TYPE_CODE_STRUCT
:
1023 error (_("Unhandled type code %d in symbol table."),
1028 /* Print using the given LANGUAGE the value VAL onto stream STREAM according
1031 This is a preferable interface to val_print, above, because it uses
1032 GDB's value mechanism. */
1035 common_val_print (struct value
*value
, struct ui_file
*stream
, int recurse
,
1036 const struct value_print_options
*options
,
1037 const struct language_defn
*language
)
1039 if (language
->la_language
== language_ada
)
1040 /* The value might have a dynamic type, which would cause trouble
1041 below when trying to extract the value contents (since the value
1042 size is determined from the type size which is unknown). So
1043 get a fixed representation of our value. */
1044 value
= ada_to_fixed_value (value
);
1046 if (value_lazy (value
))
1047 value_fetch_lazy (value
);
1049 struct value_print_options local_opts
= *options
;
1050 struct type
*type
= value_type (value
);
1051 struct type
*real_type
= check_typedef (type
);
1053 if (local_opts
.prettyformat
== Val_prettyformat_default
)
1054 local_opts
.prettyformat
= (local_opts
.prettyformat_structs
1055 ? Val_prettyformat
: Val_no_prettyformat
);
1059 /* Ensure that the type is complete and not just a stub. If the type is
1060 only a stub and we can't find and substitute its complete type, then
1061 print appropriate string and return. */
1063 if (real_type
->is_stub ())
1065 fprintf_styled (stream
, metadata_style
.style (), _("<incomplete type>"));
1069 if (!valprint_check_validity (stream
, real_type
, 0, value
))
1074 if (apply_ext_lang_val_pretty_printer (value
, stream
, recurse
, options
,
1079 /* Handle summary mode. If the value is a scalar, print it;
1080 otherwise, print an ellipsis. */
1081 if (options
->summary
&& !val_print_scalar_type_p (type
))
1083 gdb_printf (stream
, "...");
1087 /* If this value is too deep then don't print it. */
1088 if (!val_print_scalar_or_string_type_p (type
, language
)
1089 && val_print_check_max_depth (stream
, recurse
, options
, language
))
1094 language
->value_print_inner (value
, stream
, recurse
, &local_opts
);
1096 catch (const gdb_exception_error
&except
)
1098 fprintf_styled (stream
, metadata_style
.style (),
1099 _("<error reading variable: %s>"), except
.what ());
1103 /* See valprint.h. */
1106 val_print_check_max_depth (struct ui_file
*stream
, int recurse
,
1107 const struct value_print_options
*options
,
1108 const struct language_defn
*language
)
1110 if (options
->max_depth
> -1 && recurse
>= options
->max_depth
)
1112 gdb_assert (language
->struct_too_deep_ellipsis () != NULL
);
1113 gdb_puts (language
->struct_too_deep_ellipsis (), stream
);
1120 /* Check whether the value VAL is printable. Return 1 if it is;
1121 return 0 and print an appropriate error message to STREAM according to
1122 OPTIONS if it is not. */
1125 value_check_printable (struct value
*val
, struct ui_file
*stream
,
1126 const struct value_print_options
*options
)
1130 fprintf_styled (stream
, metadata_style
.style (),
1131 _("<address of value unknown>"));
1135 if (value_entirely_optimized_out (val
))
1137 if (options
->summary
&& !val_print_scalar_type_p (value_type (val
)))
1138 gdb_printf (stream
, "...");
1140 val_print_optimized_out (val
, stream
);
1144 if (value_entirely_unavailable (val
))
1146 if (options
->summary
&& !val_print_scalar_type_p (value_type (val
)))
1147 gdb_printf (stream
, "...");
1149 val_print_unavailable (stream
);
1153 if (value_type (val
)->code () == TYPE_CODE_INTERNAL_FUNCTION
)
1155 fprintf_styled (stream
, metadata_style
.style (),
1156 _("<internal function %s>"),
1157 value_internal_function_name (val
));
1161 if (type_not_associated (value_type (val
)))
1163 val_print_not_associated (stream
);
1167 if (type_not_allocated (value_type (val
)))
1169 val_print_not_allocated (stream
);
1176 /* See valprint.h. */
1179 common_val_print_checked (struct value
*val
, struct ui_file
*stream
,
1181 const struct value_print_options
*options
,
1182 const struct language_defn
*language
)
1184 if (!value_check_printable (val
, stream
, options
))
1186 common_val_print (val
, stream
, recurse
, options
, language
);
1189 /* Print on stream STREAM the value VAL according to OPTIONS. The value
1190 is printed using the current_language syntax. */
1193 value_print (struct value
*val
, struct ui_file
*stream
,
1194 const struct value_print_options
*options
)
1196 scoped_value_mark free_values
;
1198 if (!value_check_printable (val
, stream
, options
))
1204 = apply_ext_lang_val_pretty_printer (val
, stream
, 0, options
,
1211 current_language
->value_print (val
, stream
, options
);
1214 /* Meant to be used in debug sessions, so don't export it in a header file. */
1215 extern void ATTRIBUTE_UNUSED
debug_val (struct value
*val
);
1219 void ATTRIBUTE_UNUSED
1220 debug_val (struct value
*val
)
1222 value_print (val
, gdb_stdlog
, &user_print_options
);
1223 gdb_flush (gdb_stdlog
);
1227 val_print_type_code_flags (struct type
*type
, struct value
*original_value
,
1228 int embedded_offset
, struct ui_file
*stream
)
1230 const gdb_byte
*valaddr
= (value_contents_for_printing (original_value
).data ()
1232 ULONGEST val
= unpack_long (type
, valaddr
);
1233 int field
, nfields
= type
->num_fields ();
1234 struct gdbarch
*gdbarch
= type
->arch ();
1235 struct type
*bool_type
= builtin_type (gdbarch
)->builtin_bool
;
1237 gdb_puts ("[", stream
);
1238 for (field
= 0; field
< nfields
; field
++)
1240 if (type
->field (field
).name ()[0] != '\0')
1242 struct type
*field_type
= type
->field (field
).type ();
1244 if (field_type
== bool_type
1245 /* We require boolean types here to be one bit wide. This is a
1246 problematic place to notify the user of an internal error
1247 though. Instead just fall through and print the field as an
1249 && TYPE_FIELD_BITSIZE (type
, field
) == 1)
1251 if (val
& ((ULONGEST
)1 << type
->field (field
).loc_bitpos ()))
1254 styled_string (variable_name_style
.style (),
1255 type
->field (field
).name ()));
1259 unsigned field_len
= TYPE_FIELD_BITSIZE (type
, field
);
1260 ULONGEST field_val
= val
>> type
->field (field
).loc_bitpos ();
1262 if (field_len
< sizeof (ULONGEST
) * TARGET_CHAR_BIT
)
1263 field_val
&= ((ULONGEST
) 1 << field_len
) - 1;
1264 gdb_printf (stream
, " %ps=",
1265 styled_string (variable_name_style
.style (),
1266 type
->field (field
).name ()));
1267 if (field_type
->code () == TYPE_CODE_ENUM
)
1268 generic_val_print_enum_1 (field_type
, field_val
, stream
);
1270 print_longest (stream
, 'd', 0, field_val
);
1274 gdb_puts (" ]", stream
);
1277 /* See valprint.h. */
1280 value_print_scalar_formatted (struct value
*val
,
1281 const struct value_print_options
*options
,
1283 struct ui_file
*stream
)
1285 struct type
*type
= check_typedef (value_type (val
));
1287 gdb_assert (val
!= NULL
);
1289 /* If we get here with a string format, try again without it. Go
1290 all the way back to the language printers, which may call us
1292 if (options
->format
== 's')
1294 struct value_print_options opts
= *options
;
1296 opts
.deref_ref
= false;
1297 common_val_print (val
, stream
, 0, &opts
, current_language
);
1301 /* value_contents_for_printing fetches all VAL's contents. They are
1302 needed to check whether VAL is optimized-out or unavailable
1304 const gdb_byte
*valaddr
= value_contents_for_printing (val
).data ();
1306 /* A scalar object that does not have all bits available can't be
1307 printed, because all bits contribute to its representation. */
1308 if (value_bits_any_optimized_out (val
, 0,
1309 TARGET_CHAR_BIT
* type
->length ()))
1310 val_print_optimized_out (val
, stream
);
1311 else if (!value_bytes_available (val
, 0, type
->length ()))
1312 val_print_unavailable (stream
);
1314 print_scalar_formatted (valaddr
, type
, options
, size
, stream
);
1317 /* Print a number according to FORMAT which is one of d,u,x,o,b,h,w,g.
1318 The raison d'etre of this function is to consolidate printing of
1319 LONG_LONG's into this one function. The format chars b,h,w,g are
1320 from print_scalar_formatted(). Numbers are printed using C
1323 USE_C_FORMAT means to use C format in all cases. Without it,
1324 'o' and 'x' format do not include the standard C radix prefix
1327 Hilfinger/2004-09-09: USE_C_FORMAT was originally called USE_LOCAL
1328 and was intended to request formatting according to the current
1329 language and would be used for most integers that GDB prints. The
1330 exceptional cases were things like protocols where the format of
1331 the integer is a protocol thing, not a user-visible thing). The
1332 parameter remains to preserve the information of what things might
1333 be printed with language-specific format, should we ever resurrect
1337 print_longest (struct ui_file
*stream
, int format
, int use_c_format
,
1345 val
= int_string (val_long
, 10, 1, 0, 1); break;
1347 val
= int_string (val_long
, 10, 0, 0, 1); break;
1349 val
= int_string (val_long
, 16, 0, 0, use_c_format
); break;
1351 val
= int_string (val_long
, 16, 0, 2, 1); break;
1353 val
= int_string (val_long
, 16, 0, 4, 1); break;
1355 val
= int_string (val_long
, 16, 0, 8, 1); break;
1357 val
= int_string (val_long
, 16, 0, 16, 1); break;
1360 val
= int_string (val_long
, 8, 0, 0, use_c_format
); break;
1362 internal_error (_("failed internal consistency check"));
1364 gdb_puts (val
, stream
);
1367 /* This used to be a macro, but I don't think it is called often enough
1368 to merit such treatment. */
1369 /* Convert a LONGEST to an int. This is used in contexts (e.g. number of
1370 arguments to a function, number in a value history, register number, etc.)
1371 where the value must not be larger than can fit in an int. */
1374 longest_to_int (LONGEST arg
)
1376 /* Let the compiler do the work. */
1377 int rtnval
= (int) arg
;
1379 /* Check for overflows or underflows. */
1380 if (sizeof (LONGEST
) > sizeof (int))
1384 error (_("Value out of range."));
1390 /* Print a floating point value of floating-point type TYPE,
1391 pointed to in GDB by VALADDR, on STREAM. */
1394 print_floating (const gdb_byte
*valaddr
, struct type
*type
,
1395 struct ui_file
*stream
)
1397 std::string str
= target_float_to_string (valaddr
, type
);
1398 gdb_puts (str
.c_str (), stream
);
1402 print_binary_chars (struct ui_file
*stream
, const gdb_byte
*valaddr
,
1403 unsigned len
, enum bfd_endian byte_order
, bool zero_pad
,
1404 const struct value_print_options
*options
)
1409 bool seen_a_one
= false;
1410 const char *digit_separator
= nullptr;
1412 /* Declared "int" so it will be signed.
1413 This ensures that right shift will shift in zeros. */
1415 const int mask
= 0x080;
1417 if (options
->nibblesprint
)
1418 digit_separator
= current_language
->get_digit_separator();
1420 if (byte_order
== BFD_ENDIAN_BIG
)
1426 /* Every byte has 8 binary characters; peel off
1427 and print from the MSB end. */
1429 for (i
= 0; i
< (HOST_CHAR_BIT
* sizeof (*p
)); i
++)
1431 if (options
->nibblesprint
&& seen_a_one
&& i
% 4 == 0)
1432 gdb_putc (*digit_separator
, stream
);
1434 if (*p
& (mask
>> i
))
1439 if (zero_pad
|| seen_a_one
|| b
== '1')
1440 gdb_putc (b
, stream
);
1441 else if (options
->nibblesprint
)
1443 if ((0xf0 & (mask
>> i
) && (*p
& 0xf0))
1444 || (0x0f & (mask
>> i
) && (*p
& 0x0f)))
1445 gdb_putc (b
, stream
);
1455 for (p
= valaddr
+ len
- 1;
1459 for (i
= 0; i
< (HOST_CHAR_BIT
* sizeof (*p
)); i
++)
1461 if (options
->nibblesprint
&& seen_a_one
&& i
% 4 == 0)
1462 gdb_putc (*digit_separator
, stream
);
1464 if (*p
& (mask
>> i
))
1469 if (zero_pad
|| seen_a_one
|| b
== '1')
1470 gdb_putc (b
, stream
);
1471 else if (options
->nibblesprint
)
1473 if ((0xf0 & (mask
>> i
) && (*p
& 0xf0))
1474 || (0x0f & (mask
>> i
) && (*p
& 0x0f)))
1475 gdb_putc (b
, stream
);
1484 /* When not zero-padding, ensure that something is printed when the
1486 if (!zero_pad
&& !seen_a_one
)
1487 gdb_putc ('0', stream
);
1490 /* A helper for print_octal_chars that emits a single octal digit,
1491 optionally suppressing it if is zero and updating SEEN_A_ONE. */
1494 emit_octal_digit (struct ui_file
*stream
, bool *seen_a_one
, int digit
)
1496 if (*seen_a_one
|| digit
!= 0)
1497 gdb_printf (stream
, "%o", digit
);
1502 /* VALADDR points to an integer of LEN bytes.
1503 Print it in octal on stream or format it in buf. */
1506 print_octal_chars (struct ui_file
*stream
, const gdb_byte
*valaddr
,
1507 unsigned len
, enum bfd_endian byte_order
)
1510 unsigned char octa1
, octa2
, octa3
, carry
;
1513 /* Octal is 3 bits, which doesn't fit. Yuk. So we have to track
1514 * the extra bits, which cycle every three bytes:
1516 * Byte side: 0 1 2 3
1518 * bit number 123 456 78 | 9 012 345 6 | 78 901 234 | 567 890 12 |
1520 * Octal side: 0 1 carry 3 4 carry ...
1522 * Cycle number: 0 1 2
1524 * But of course we are printing from the high side, so we have to
1525 * figure out where in the cycle we are so that we end up with no
1526 * left over bits at the end.
1528 #define BITS_IN_OCTAL 3
1529 #define HIGH_ZERO 0340
1530 #define LOW_ZERO 0034
1531 #define CARRY_ZERO 0003
1532 static_assert (HIGH_ZERO
+ LOW_ZERO
+ CARRY_ZERO
== 0xff,
1533 "cycle zero constants are wrong");
1534 #define HIGH_ONE 0200
1535 #define MID_ONE 0160
1536 #define LOW_ONE 0016
1537 #define CARRY_ONE 0001
1538 static_assert (HIGH_ONE
+ MID_ONE
+ LOW_ONE
+ CARRY_ONE
== 0xff,
1539 "cycle one constants are wrong");
1540 #define HIGH_TWO 0300
1541 #define MID_TWO 0070
1542 #define LOW_TWO 0007
1543 static_assert (HIGH_TWO
+ MID_TWO
+ LOW_TWO
== 0xff,
1544 "cycle two constants are wrong");
1546 /* For 32 we start in cycle 2, with two bits and one bit carry;
1547 for 64 in cycle in cycle 1, with one bit and a two bit carry. */
1549 cycle
= (len
* HOST_CHAR_BIT
) % BITS_IN_OCTAL
;
1552 gdb_puts ("0", stream
);
1553 bool seen_a_one
= false;
1554 if (byte_order
== BFD_ENDIAN_BIG
)
1563 /* No carry in, carry out two bits. */
1565 octa1
= (HIGH_ZERO
& *p
) >> 5;
1566 octa2
= (LOW_ZERO
& *p
) >> 2;
1567 carry
= (CARRY_ZERO
& *p
);
1568 emit_octal_digit (stream
, &seen_a_one
, octa1
);
1569 emit_octal_digit (stream
, &seen_a_one
, octa2
);
1573 /* Carry in two bits, carry out one bit. */
1575 octa1
= (carry
<< 1) | ((HIGH_ONE
& *p
) >> 7);
1576 octa2
= (MID_ONE
& *p
) >> 4;
1577 octa3
= (LOW_ONE
& *p
) >> 1;
1578 carry
= (CARRY_ONE
& *p
);
1579 emit_octal_digit (stream
, &seen_a_one
, octa1
);
1580 emit_octal_digit (stream
, &seen_a_one
, octa2
);
1581 emit_octal_digit (stream
, &seen_a_one
, octa3
);
1585 /* Carry in one bit, no carry out. */
1587 octa1
= (carry
<< 2) | ((HIGH_TWO
& *p
) >> 6);
1588 octa2
= (MID_TWO
& *p
) >> 3;
1589 octa3
= (LOW_TWO
& *p
);
1591 emit_octal_digit (stream
, &seen_a_one
, octa1
);
1592 emit_octal_digit (stream
, &seen_a_one
, octa2
);
1593 emit_octal_digit (stream
, &seen_a_one
, octa3
);
1597 error (_("Internal error in octal conversion;"));
1601 cycle
= cycle
% BITS_IN_OCTAL
;
1606 for (p
= valaddr
+ len
- 1;
1613 /* Carry out, no carry in */
1615 octa1
= (HIGH_ZERO
& *p
) >> 5;
1616 octa2
= (LOW_ZERO
& *p
) >> 2;
1617 carry
= (CARRY_ZERO
& *p
);
1618 emit_octal_digit (stream
, &seen_a_one
, octa1
);
1619 emit_octal_digit (stream
, &seen_a_one
, octa2
);
1623 /* Carry in, carry out */
1625 octa1
= (carry
<< 1) | ((HIGH_ONE
& *p
) >> 7);
1626 octa2
= (MID_ONE
& *p
) >> 4;
1627 octa3
= (LOW_ONE
& *p
) >> 1;
1628 carry
= (CARRY_ONE
& *p
);
1629 emit_octal_digit (stream
, &seen_a_one
, octa1
);
1630 emit_octal_digit (stream
, &seen_a_one
, octa2
);
1631 emit_octal_digit (stream
, &seen_a_one
, octa3
);
1635 /* Carry in, no carry out */
1637 octa1
= (carry
<< 2) | ((HIGH_TWO
& *p
) >> 6);
1638 octa2
= (MID_TWO
& *p
) >> 3;
1639 octa3
= (LOW_TWO
& *p
);
1641 emit_octal_digit (stream
, &seen_a_one
, octa1
);
1642 emit_octal_digit (stream
, &seen_a_one
, octa2
);
1643 emit_octal_digit (stream
, &seen_a_one
, octa3
);
1647 error (_("Internal error in octal conversion;"));
1651 cycle
= cycle
% BITS_IN_OCTAL
;
1657 /* Possibly negate the integer represented by BYTES. It contains LEN
1658 bytes in the specified byte order. If the integer is negative,
1659 copy it into OUT_VEC, negate it, and return true. Otherwise, do
1660 nothing and return false. */
1663 maybe_negate_by_bytes (const gdb_byte
*bytes
, unsigned len
,
1664 enum bfd_endian byte_order
,
1665 gdb::byte_vector
*out_vec
)
1668 gdb_assert (len
> 0);
1669 if (byte_order
== BFD_ENDIAN_BIG
)
1670 sign_byte
= bytes
[0];
1672 sign_byte
= bytes
[len
- 1];
1673 if ((sign_byte
& 0x80) == 0)
1676 out_vec
->resize (len
);
1678 /* Compute -x == 1 + ~x. */
1679 if (byte_order
== BFD_ENDIAN_LITTLE
)
1682 for (unsigned i
= 0; i
< len
; ++i
)
1684 unsigned tem
= (0xff & ~bytes
[i
]) + carry
;
1685 (*out_vec
)[i
] = tem
& 0xff;
1692 for (unsigned i
= len
; i
> 0; --i
)
1694 unsigned tem
= (0xff & ~bytes
[i
- 1]) + carry
;
1695 (*out_vec
)[i
- 1] = tem
& 0xff;
1703 /* VALADDR points to an integer of LEN bytes.
1704 Print it in decimal on stream or format it in buf. */
1707 print_decimal_chars (struct ui_file
*stream
, const gdb_byte
*valaddr
,
1708 unsigned len
, bool is_signed
,
1709 enum bfd_endian byte_order
)
1712 #define CARRY_OUT( x ) ((x) / TEN) /* extend char to int */
1713 #define CARRY_LEFT( x ) ((x) % TEN)
1714 #define SHIFT( x ) ((x) << 4)
1715 #define LOW_NIBBLE( x ) ( (x) & 0x00F)
1716 #define HIGH_NIBBLE( x ) (((x) & 0x0F0) >> 4)
1721 int i
, j
, decimal_digits
;
1725 gdb::byte_vector negated_bytes
;
1727 && maybe_negate_by_bytes (valaddr
, len
, byte_order
, &negated_bytes
))
1729 gdb_puts ("-", stream
);
1730 valaddr
= negated_bytes
.data ();
1733 /* Base-ten number is less than twice as many digits
1734 as the base 16 number, which is 2 digits per byte. */
1736 decimal_len
= len
* 2 * 2;
1737 std::vector
<unsigned char> digits (decimal_len
, 0);
1739 /* Ok, we have an unknown number of bytes of data to be printed in
1742 * Given a hex number (in nibbles) as XYZ, we start by taking X and
1743 * decimalizing it as "x1 x2" in two decimal nibbles. Then we multiply
1744 * the nibbles by 16, add Y and re-decimalize. Repeat with Z.
1746 * The trick is that "digits" holds a base-10 number, but sometimes
1747 * the individual digits are > 10.
1749 * Outer loop is per nibble (hex digit) of input, from MSD end to
1752 decimal_digits
= 0; /* Number of decimal digits so far */
1753 p
= (byte_order
== BFD_ENDIAN_BIG
) ? valaddr
: valaddr
+ len
- 1;
1755 while ((byte_order
== BFD_ENDIAN_BIG
) ? (p
< valaddr
+ len
) : (p
>= valaddr
))
1758 * Multiply current base-ten number by 16 in place.
1759 * Each digit was between 0 and 9, now is between
1762 for (j
= 0; j
< decimal_digits
; j
++)
1764 digits
[j
] = SHIFT (digits
[j
]);
1767 /* Take the next nibble off the input and add it to what
1768 * we've got in the LSB position. Bottom 'digit' is now
1769 * between 0 and 159.
1771 * "flip" is used to run this loop twice for each byte.
1775 /* Take top nibble. */
1777 digits
[0] += HIGH_NIBBLE (*p
);
1782 /* Take low nibble and bump our pointer "p". */
1784 digits
[0] += LOW_NIBBLE (*p
);
1785 if (byte_order
== BFD_ENDIAN_BIG
)
1792 /* Re-decimalize. We have to do this often enough
1793 * that we don't overflow, but once per nibble is
1794 * overkill. Easier this way, though. Note that the
1795 * carry is often larger than 10 (e.g. max initial
1796 * carry out of lowest nibble is 15, could bubble all
1797 * the way up greater than 10). So we have to do
1798 * the carrying beyond the last current digit.
1801 for (j
= 0; j
< decimal_len
- 1; j
++)
1805 /* "/" won't handle an unsigned char with
1806 * a value that if signed would be negative.
1807 * So extend to longword int via "dummy".
1810 carry
= CARRY_OUT (dummy
);
1811 digits
[j
] = CARRY_LEFT (dummy
);
1813 if (j
>= decimal_digits
&& carry
== 0)
1816 * All higher digits are 0 and we
1817 * no longer have a carry.
1819 * Note: "j" is 0-based, "decimal_digits" is
1822 decimal_digits
= j
+ 1;
1828 /* Ok, now "digits" is the decimal representation, with
1829 the "decimal_digits" actual digits. Print! */
1831 for (i
= decimal_digits
- 1; i
> 0 && digits
[i
] == 0; --i
)
1836 gdb_printf (stream
, "%1d", digits
[i
]);
1840 /* VALADDR points to an integer of LEN bytes. Print it in hex on stream. */
1843 print_hex_chars (struct ui_file
*stream
, const gdb_byte
*valaddr
,
1844 unsigned len
, enum bfd_endian byte_order
,
1849 gdb_puts ("0x", stream
);
1850 if (byte_order
== BFD_ENDIAN_BIG
)
1856 /* Strip leading 0 bytes, but be sure to leave at least a
1857 single byte at the end. */
1858 for (; p
< valaddr
+ len
- 1 && !*p
; ++p
)
1862 const gdb_byte
*first
= p
;
1867 /* When not zero-padding, use a different format for the
1868 very first byte printed. */
1869 if (!zero_pad
&& p
== first
)
1870 gdb_printf (stream
, "%x", *p
);
1872 gdb_printf (stream
, "%02x", *p
);
1877 p
= valaddr
+ len
- 1;
1881 /* Strip leading 0 bytes, but be sure to leave at least a
1882 single byte at the end. */
1883 for (; p
>= valaddr
+ 1 && !*p
; --p
)
1887 const gdb_byte
*first
= p
;
1892 /* When not zero-padding, use a different format for the
1893 very first byte printed. */
1894 if (!zero_pad
&& p
== first
)
1895 gdb_printf (stream
, "%x", *p
);
1897 gdb_printf (stream
, "%02x", *p
);
1902 /* Print function pointer with inferior address ADDRESS onto stdio
1906 print_function_pointer_address (const struct value_print_options
*options
,
1907 struct gdbarch
*gdbarch
,
1909 struct ui_file
*stream
)
1911 CORE_ADDR func_addr
= gdbarch_convert_from_func_ptr_addr
1912 (gdbarch
, address
, current_inferior ()->top_target ());
1914 /* If the function pointer is represented by a description, print
1915 the address of the description. */
1916 if (options
->addressprint
&& func_addr
!= address
)
1918 gdb_puts ("@", stream
);
1919 gdb_puts (paddress (gdbarch
, address
), stream
);
1920 gdb_puts (": ", stream
);
1922 print_address_demangle (options
, gdbarch
, func_addr
, stream
, demangle
);
1926 /* Print on STREAM using the given OPTIONS the index for the element
1927 at INDEX of an array whose index type is INDEX_TYPE. */
1930 maybe_print_array_index (struct type
*index_type
, LONGEST index
,
1931 struct ui_file
*stream
,
1932 const struct value_print_options
*options
)
1934 if (!options
->print_array_indexes
)
1937 current_language
->print_array_index (index_type
, index
, stream
, options
);
1940 /* See valprint.h. */
1943 value_print_array_elements (struct value
*val
, struct ui_file
*stream
,
1945 const struct value_print_options
*options
,
1948 unsigned int things_printed
= 0;
1950 struct type
*elttype
, *index_type
;
1951 /* Position of the array element we are examining to see
1952 whether it is repeated. */
1954 /* Number of repetitions we have detected so far. */
1956 LONGEST low_bound
, high_bound
;
1958 struct type
*type
= check_typedef (value_type (val
));
1960 elttype
= type
->target_type ();
1961 unsigned bit_stride
= type
->bit_stride ();
1962 if (bit_stride
== 0)
1963 bit_stride
= 8 * check_typedef (elttype
)->length ();
1964 index_type
= type
->index_type ();
1965 if (index_type
->code () == TYPE_CODE_RANGE
)
1966 index_type
= index_type
->target_type ();
1968 if (get_array_bounds (type
, &low_bound
, &high_bound
))
1970 /* The array length should normally be HIGH_BOUND - LOW_BOUND +
1971 1. But we have to be a little extra careful, because some
1972 languages such as Ada allow LOW_BOUND to be greater than
1973 HIGH_BOUND for empty arrays. In that situation, the array
1974 length is just zero, not negative! */
1975 if (low_bound
> high_bound
)
1978 len
= high_bound
- low_bound
+ 1;
1982 warning (_("unable to get bounds of array, assuming null array"));
1987 annotate_array_section_begin (i
, elttype
);
1989 for (; i
< len
&& things_printed
< options
->print_max
; i
++)
1991 scoped_value_mark free_values
;
1995 if (options
->prettyformat_arrays
)
1997 gdb_printf (stream
, ",\n");
1998 print_spaces (2 + 2 * recurse
, stream
);
2001 gdb_printf (stream
, ", ");
2003 else if (options
->prettyformat_arrays
)
2005 gdb_printf (stream
, "\n");
2006 print_spaces (2 + 2 * recurse
, stream
);
2008 stream
->wrap_here (2 + 2 * recurse
);
2009 maybe_print_array_index (index_type
, i
+ low_bound
,
2012 struct value
*element
= value_from_component_bitsize (val
, elttype
,
2017 /* Only check for reps if repeat_count_threshold is not set to
2018 UINT_MAX (unlimited). */
2019 if (options
->repeat_count_threshold
< UINT_MAX
)
2023 struct value
*rep_elt
2024 = value_from_component_bitsize (val
, elttype
,
2027 if (!value_contents_eq (element
, rep_elt
))
2034 common_val_print (element
, stream
, recurse
+ 1, options
,
2037 if (reps
> options
->repeat_count_threshold
)
2039 annotate_elt_rep (reps
);
2040 gdb_printf (stream
, " %p[<repeats %u times>%p]",
2041 metadata_style
.style ().ptr (), reps
, nullptr);
2042 annotate_elt_rep_end ();
2045 things_printed
+= options
->repeat_count_threshold
;
2053 annotate_array_section_end ();
2055 gdb_printf (stream
, "...");
2056 if (options
->prettyformat_arrays
)
2058 gdb_printf (stream
, "\n");
2059 print_spaces (2 * recurse
, stream
);
2063 /* Return true if print_wchar can display W without resorting to a
2064 numeric escape, false otherwise. */
2067 wchar_printable (gdb_wchar_t w
)
2069 return (gdb_iswprint (w
)
2070 || w
== LCST ('\a') || w
== LCST ('\b')
2071 || w
== LCST ('\f') || w
== LCST ('\n')
2072 || w
== LCST ('\r') || w
== LCST ('\t')
2073 || w
== LCST ('\v'));
2076 /* A helper function that converts the contents of STRING to wide
2077 characters and then appends them to OUTPUT. */
2080 append_string_as_wide (const char *string
,
2081 struct obstack
*output
)
2083 for (; *string
; ++string
)
2085 gdb_wchar_t w
= gdb_btowc (*string
);
2086 obstack_grow (output
, &w
, sizeof (gdb_wchar_t
));
2090 /* Print a wide character W to OUTPUT. ORIG is a pointer to the
2091 original (target) bytes representing the character, ORIG_LEN is the
2092 number of valid bytes. WIDTH is the number of bytes in a base
2093 characters of the type. OUTPUT is an obstack to which wide
2094 characters are emitted. QUOTER is a (narrow) character indicating
2095 the style of quotes surrounding the character to be printed.
2096 NEED_ESCAPE is an in/out flag which is used to track numeric
2097 escapes across calls. */
2100 print_wchar (gdb_wint_t w
, const gdb_byte
*orig
,
2101 int orig_len
, int width
,
2102 enum bfd_endian byte_order
,
2103 struct obstack
*output
,
2104 int quoter
, bool *need_escapep
)
2106 bool need_escape
= *need_escapep
;
2108 *need_escapep
= false;
2110 /* If any additional cases are added to this switch block, then the
2111 function wchar_printable will likely need updating too. */
2115 obstack_grow_wstr (output
, LCST ("\\a"));
2118 obstack_grow_wstr (output
, LCST ("\\b"));
2121 obstack_grow_wstr (output
, LCST ("\\f"));
2124 obstack_grow_wstr (output
, LCST ("\\n"));
2127 obstack_grow_wstr (output
, LCST ("\\r"));
2130 obstack_grow_wstr (output
, LCST ("\\t"));
2133 obstack_grow_wstr (output
, LCST ("\\v"));
2137 if (gdb_iswprint (w
) && !(need_escape
&& gdb_iswxdigit (w
)))
2139 gdb_wchar_t wchar
= w
;
2141 if (w
== gdb_btowc (quoter
) || w
== LCST ('\\'))
2142 obstack_grow_wstr (output
, LCST ("\\"));
2143 obstack_grow (output
, &wchar
, sizeof (gdb_wchar_t
));
2149 for (i
= 0; i
+ width
<= orig_len
; i
+= width
)
2154 value
= extract_unsigned_integer (&orig
[i
], width
,
2156 /* If the value fits in 3 octal digits, print it that
2157 way. Otherwise, print it as a hex escape. */
2160 xsnprintf (octal
, sizeof (octal
), "\\%.3o",
2161 (int) (value
& 0777));
2162 *need_escapep
= false;
2166 xsnprintf (octal
, sizeof (octal
), "\\x%lx", (long) value
);
2167 /* A hex escape might require the next character
2168 to be escaped, because, unlike with octal,
2169 hex escapes have no length limit. */
2170 *need_escapep
= true;
2172 append_string_as_wide (octal
, output
);
2174 /* If we somehow have extra bytes, print them now. */
2175 while (i
< orig_len
)
2179 xsnprintf (octal
, sizeof (octal
), "\\%.3o", orig
[i
] & 0xff);
2180 *need_escapep
= false;
2181 append_string_as_wide (octal
, output
);
2190 /* Print the character C on STREAM as part of the contents of a
2191 literal string whose delimiter is QUOTER. ENCODING names the
2195 generic_emit_char (int c
, struct type
*type
, struct ui_file
*stream
,
2196 int quoter
, const char *encoding
)
2198 enum bfd_endian byte_order
2199 = type_byte_order (type
);
2201 bool need_escape
= false;
2203 c_buf
= (gdb_byte
*) alloca (type
->length ());
2204 pack_long (c_buf
, type
, c
);
2206 wchar_iterator
iter (c_buf
, type
->length (), encoding
, type
->length ());
2208 /* This holds the printable form of the wchar_t data. */
2209 auto_obstack wchar_buf
;
2215 const gdb_byte
*buf
;
2217 int print_escape
= 1;
2218 enum wchar_iterate_result result
;
2220 num_chars
= iter
.iterate (&result
, &chars
, &buf
, &buflen
);
2225 /* If all characters are printable, print them. Otherwise,
2226 we're going to have to print an escape sequence. We
2227 check all characters because we want to print the target
2228 bytes in the escape sequence, and we don't know character
2229 boundaries there. */
2233 for (i
= 0; i
< num_chars
; ++i
)
2234 if (!wchar_printable (chars
[i
]))
2242 for (i
= 0; i
< num_chars
; ++i
)
2243 print_wchar (chars
[i
], buf
, buflen
,
2244 type
->length (), byte_order
,
2245 &wchar_buf
, quoter
, &need_escape
);
2249 /* This handles the NUM_CHARS == 0 case as well. */
2251 print_wchar (gdb_WEOF
, buf
, buflen
, type
->length (),
2252 byte_order
, &wchar_buf
, quoter
, &need_escape
);
2255 /* The output in the host encoding. */
2256 auto_obstack output
;
2258 convert_between_encodings (INTERMEDIATE_ENCODING
, host_charset (),
2259 (gdb_byte
*) obstack_base (&wchar_buf
),
2260 obstack_object_size (&wchar_buf
),
2261 sizeof (gdb_wchar_t
), &output
, translit_char
);
2262 obstack_1grow (&output
, '\0');
2264 gdb_puts ((const char *) obstack_base (&output
), stream
);
2267 /* Return the repeat count of the next character/byte in ITER,
2268 storing the result in VEC. */
2271 count_next_character (wchar_iterator
*iter
,
2272 std::vector
<converted_character
> *vec
)
2274 struct converted_character
*current
;
2278 struct converted_character tmp
;
2282 = iter
->iterate (&tmp
.result
, &chars
, &tmp
.buf
, &tmp
.buflen
);
2283 if (tmp
.num_chars
> 0)
2285 gdb_assert (tmp
.num_chars
< MAX_WCHARS
);
2286 memcpy (tmp
.chars
, chars
, tmp
.num_chars
* sizeof (gdb_wchar_t
));
2288 vec
->push_back (tmp
);
2291 current
= &vec
->back ();
2293 /* Count repeated characters or bytes. */
2294 current
->repeat_count
= 1;
2295 if (current
->num_chars
== -1)
2303 struct converted_character d
;
2310 /* Get the next character. */
2311 d
.num_chars
= iter
->iterate (&d
.result
, &chars
, &d
.buf
, &d
.buflen
);
2313 /* If a character was successfully converted, save the character
2314 into the converted character. */
2315 if (d
.num_chars
> 0)
2317 gdb_assert (d
.num_chars
< MAX_WCHARS
);
2318 memcpy (d
.chars
, chars
, WCHAR_BUFLEN (d
.num_chars
));
2321 /* Determine if the current character is the same as this
2323 if (d
.num_chars
== current
->num_chars
&& d
.result
== current
->result
)
2325 /* There are two cases to consider:
2327 1) Equality of converted character (num_chars > 0)
2328 2) Equality of non-converted character (num_chars == 0) */
2329 if ((current
->num_chars
> 0
2330 && memcmp (current
->chars
, d
.chars
,
2331 WCHAR_BUFLEN (current
->num_chars
)) == 0)
2332 || (current
->num_chars
== 0
2333 && current
->buflen
== d
.buflen
2334 && memcmp (current
->buf
, d
.buf
, current
->buflen
) == 0))
2335 ++current
->repeat_count
;
2343 /* Push this next converted character onto the result vector. */
2344 repeat
= current
->repeat_count
;
2350 /* Print the characters in CHARS to the OBSTACK. QUOTE_CHAR is the quote
2351 character to use with string output. WIDTH is the size of the output
2352 character type. BYTE_ORDER is the target byte order. OPTIONS
2353 is the user's print options. */
2356 print_converted_chars_to_obstack (struct obstack
*obstack
,
2357 const std::vector
<converted_character
> &chars
,
2358 int quote_char
, int width
,
2359 enum bfd_endian byte_order
,
2360 const struct value_print_options
*options
)
2363 const converted_character
*elem
;
2364 enum {START
, SINGLE
, REPEAT
, INCOMPLETE
, FINISH
} state
, last
;
2365 gdb_wchar_t wide_quote_char
= gdb_btowc (quote_char
);
2366 bool need_escape
= false;
2368 /* Set the start state. */
2370 last
= state
= START
;
2378 /* Nothing to do. */
2385 /* We are outputting a single character
2386 (< options->repeat_count_threshold). */
2390 /* We were outputting some other type of content, so we
2391 must output and a comma and a quote. */
2393 obstack_grow_wstr (obstack
, LCST (", "));
2394 obstack_grow (obstack
, &wide_quote_char
, sizeof (gdb_wchar_t
));
2396 /* Output the character. */
2397 for (j
= 0; j
< elem
->repeat_count
; ++j
)
2399 if (elem
->result
== wchar_iterate_ok
)
2400 print_wchar (elem
->chars
[0], elem
->buf
, elem
->buflen
, width
,
2401 byte_order
, obstack
, quote_char
, &need_escape
);
2403 print_wchar (gdb_WEOF
, elem
->buf
, elem
->buflen
, width
,
2404 byte_order
, obstack
, quote_char
, &need_escape
);
2413 /* We are outputting a character with a repeat count
2414 greater than options->repeat_count_threshold. */
2418 /* We were outputting a single string. Terminate the
2420 obstack_grow (obstack
, &wide_quote_char
, sizeof (gdb_wchar_t
));
2423 obstack_grow_wstr (obstack
, LCST (", "));
2425 /* Output the character and repeat string. */
2426 obstack_grow_wstr (obstack
, LCST ("'"));
2427 if (elem
->result
== wchar_iterate_ok
)
2428 print_wchar (elem
->chars
[0], elem
->buf
, elem
->buflen
, width
,
2429 byte_order
, obstack
, quote_char
, &need_escape
);
2431 print_wchar (gdb_WEOF
, elem
->buf
, elem
->buflen
, width
,
2432 byte_order
, obstack
, quote_char
, &need_escape
);
2433 obstack_grow_wstr (obstack
, LCST ("'"));
2434 std::string s
= string_printf (_(" <repeats %u times>"),
2435 elem
->repeat_count
);
2436 for (j
= 0; s
[j
]; ++j
)
2438 gdb_wchar_t w
= gdb_btowc (s
[j
]);
2439 obstack_grow (obstack
, &w
, sizeof (gdb_wchar_t
));
2445 /* We are outputting an incomplete sequence. */
2448 /* If we were outputting a string of SINGLE characters,
2449 terminate the quote. */
2450 obstack_grow (obstack
, &wide_quote_char
, sizeof (gdb_wchar_t
));
2453 obstack_grow_wstr (obstack
, LCST (", "));
2455 /* Output the incomplete sequence string. */
2456 obstack_grow_wstr (obstack
, LCST ("<incomplete sequence "));
2457 print_wchar (gdb_WEOF
, elem
->buf
, elem
->buflen
, width
, byte_order
,
2458 obstack
, 0, &need_escape
);
2459 obstack_grow_wstr (obstack
, LCST (">"));
2461 /* We do not attempt to output anything after this. */
2466 /* All done. If we were outputting a string of SINGLE
2467 characters, the string must be terminated. Otherwise,
2468 REPEAT and INCOMPLETE are always left properly terminated. */
2470 obstack_grow (obstack
, &wide_quote_char
, sizeof (gdb_wchar_t
));
2475 /* Get the next element and state. */
2477 if (state
!= FINISH
)
2479 elem
= &chars
[idx
++];
2480 switch (elem
->result
)
2482 case wchar_iterate_ok
:
2483 case wchar_iterate_invalid
:
2484 if (elem
->repeat_count
> options
->repeat_count_threshold
)
2490 case wchar_iterate_incomplete
:
2494 case wchar_iterate_eof
:
2502 /* Print the character string STRING, printing at most LENGTH
2503 characters. LENGTH is -1 if the string is nul terminated. TYPE is
2504 the type of each character. OPTIONS holds the printing options;
2505 printing stops early if the number hits print_max_chars; repeat
2506 counts are printed as appropriate. Print ellipses at the end if we
2507 had to stop before printing LENGTH characters, or if FORCE_ELLIPSES.
2508 QUOTE_CHAR is the character to print at each end of the string. If
2509 C_STYLE_TERMINATOR is true, and the last character is 0, then it is
2513 generic_printstr (struct ui_file
*stream
, struct type
*type
,
2514 const gdb_byte
*string
, unsigned int length
,
2515 const char *encoding
, int force_ellipses
,
2516 int quote_char
, int c_style_terminator
,
2517 const struct value_print_options
*options
)
2519 enum bfd_endian byte_order
= type_byte_order (type
);
2521 int width
= type
->length ();
2523 struct converted_character
*last
;
2527 unsigned long current_char
= 1;
2529 for (i
= 0; current_char
; ++i
)
2532 current_char
= extract_unsigned_integer (string
+ i
* width
,
2538 /* If the string was not truncated due to `set print elements', and
2539 the last byte of it is a null, we don't print that, in
2540 traditional C style. */
2541 if (c_style_terminator
2544 && (extract_unsigned_integer (string
+ (length
- 1) * width
,
2545 width
, byte_order
) == 0))
2550 gdb_printf (stream
, "%c%c", quote_char
, quote_char
);
2554 /* Arrange to iterate over the characters, in wchar_t form. */
2555 wchar_iterator
iter (string
, length
* width
, encoding
, width
);
2556 std::vector
<converted_character
> converted_chars
;
2558 /* Convert characters until the string is over or the maximum
2559 number of printed characters has been reached. */
2561 unsigned int print_max_chars
= get_print_max_chars (options
);
2562 while (i
< print_max_chars
)
2568 /* Grab the next character and repeat count. */
2569 r
= count_next_character (&iter
, &converted_chars
);
2571 /* If less than zero, the end of the input string was reached. */
2575 /* Otherwise, add the count to the total print count and get
2576 the next character. */
2580 /* Get the last element and determine if the entire string was
2582 last
= &converted_chars
.back ();
2583 finished
= (last
->result
== wchar_iterate_eof
);
2585 /* Ensure that CONVERTED_CHARS is terminated. */
2586 last
->result
= wchar_iterate_eof
;
2588 /* WCHAR_BUF is the obstack we use to represent the string in
2590 auto_obstack wchar_buf
;
2592 /* Print the output string to the obstack. */
2593 print_converted_chars_to_obstack (&wchar_buf
, converted_chars
, quote_char
,
2594 width
, byte_order
, options
);
2596 if (force_ellipses
|| !finished
)
2597 obstack_grow_wstr (&wchar_buf
, LCST ("..."));
2599 /* OUTPUT is where we collect `char's for printing. */
2600 auto_obstack output
;
2602 convert_between_encodings (INTERMEDIATE_ENCODING
, host_charset (),
2603 (gdb_byte
*) obstack_base (&wchar_buf
),
2604 obstack_object_size (&wchar_buf
),
2605 sizeof (gdb_wchar_t
), &output
, translit_char
);
2606 obstack_1grow (&output
, '\0');
2608 gdb_puts ((const char *) obstack_base (&output
), stream
);
2611 /* Print a string from the inferior, starting at ADDR and printing up to LEN
2612 characters, of WIDTH bytes a piece, to STREAM. If LEN is -1, printing
2613 stops at the first null byte, otherwise printing proceeds (including null
2614 bytes) until either print_max_chars or LEN characters have been printed,
2615 whichever is smaller. ENCODING is the name of the string's
2616 encoding. It can be NULL, in which case the target encoding is
2620 val_print_string (struct type
*elttype
, const char *encoding
,
2621 CORE_ADDR addr
, int len
,
2622 struct ui_file
*stream
,
2623 const struct value_print_options
*options
)
2625 int force_ellipsis
= 0; /* Force ellipsis to be printed if nonzero. */
2626 int err
; /* Non-zero if we got a bad read. */
2627 int found_nul
; /* Non-zero if we found the nul char. */
2628 unsigned int fetchlimit
; /* Maximum number of chars to print. */
2630 gdb::unique_xmalloc_ptr
<gdb_byte
> buffer
; /* Dynamically growable fetch buffer. */
2631 struct gdbarch
*gdbarch
= elttype
->arch ();
2632 enum bfd_endian byte_order
= type_byte_order (elttype
);
2633 int width
= elttype
->length ();
2635 /* First we need to figure out the limit on the number of characters we are
2636 going to attempt to fetch and print. This is actually pretty simple.
2637 If LEN >= zero, then the limit is the minimum of LEN and print_max_chars.
2638 If LEN is -1, then the limit is print_max_chars. This is true regardless
2639 of whether print_max_chars is zero, UINT_MAX (unlimited), or something in
2640 between, because finding the null byte (or available memory) is what
2641 actually limits the fetch. */
2643 unsigned int print_max_chars
= get_print_max_chars (options
);
2644 fetchlimit
= (len
== -1
2646 : std::min ((unsigned) len
, print_max_chars
));
2648 err
= target_read_string (addr
, len
, width
, fetchlimit
,
2649 &buffer
, &bytes_read
);
2653 /* We now have either successfully filled the buffer to fetchlimit,
2654 or terminated early due to an error or finding a null char when
2657 /* Determine found_nul by looking at the last character read. */
2659 if (bytes_read
>= width
)
2660 found_nul
= extract_unsigned_integer (buffer
.get () + bytes_read
- width
,
2661 width
, byte_order
) == 0;
2662 if (len
== -1 && !found_nul
)
2666 /* We didn't find a NUL terminator we were looking for. Attempt
2667 to peek at the next character. If not successful, or it is not
2668 a null byte, then force ellipsis to be printed. */
2670 peekbuf
= (gdb_byte
*) alloca (width
);
2672 if (target_read_memory (addr
, peekbuf
, width
) == 0
2673 && extract_unsigned_integer (peekbuf
, width
, byte_order
) != 0)
2676 else if ((len
>= 0 && err
!= 0) || (len
> bytes_read
/ width
))
2678 /* Getting an error when we have a requested length, or fetching less
2679 than the number of characters actually requested, always make us
2684 /* If we get an error before fetching anything, don't print a string.
2685 But if we fetch something and then get an error, print the string
2686 and then the error message. */
2687 if (err
== 0 || bytes_read
> 0)
2688 current_language
->printstr (stream
, elttype
, buffer
.get (),
2690 encoding
, force_ellipsis
, options
);
2694 std::string str
= memory_error_message (TARGET_XFER_E_IO
, gdbarch
, addr
);
2696 gdb_printf (stream
, _("<error: %ps>"),
2697 styled_string (metadata_style
.style (),
2701 return (bytes_read
/ width
);
2704 /* Handle 'show print max-depth'. */
2707 show_print_max_depth (struct ui_file
*file
, int from_tty
,
2708 struct cmd_list_element
*c
, const char *value
)
2710 gdb_printf (file
, _("Maximum print depth is %s.\n"), value
);
2714 /* The 'set input-radix' command writes to this auxiliary variable.
2715 If the requested radix is valid, INPUT_RADIX is updated; otherwise,
2716 it is left unchanged. */
2718 static unsigned input_radix_1
= 10;
2720 /* Validate an input or output radix setting, and make sure the user
2721 knows what they really did here. Radix setting is confusing, e.g.
2722 setting the input radix to "10" never changes it! */
2725 set_input_radix (const char *args
, int from_tty
, struct cmd_list_element
*c
)
2727 set_input_radix_1 (from_tty
, input_radix_1
);
2731 set_input_radix_1 (int from_tty
, unsigned radix
)
2733 /* We don't currently disallow any input radix except 0 or 1, which don't
2734 make any mathematical sense. In theory, we can deal with any input
2735 radix greater than 1, even if we don't have unique digits for every
2736 value from 0 to radix-1, but in practice we lose on large radix values.
2737 We should either fix the lossage or restrict the radix range more.
2742 input_radix_1
= input_radix
;
2743 error (_("Nonsense input radix ``decimal %u''; input radix unchanged."),
2746 input_radix_1
= input_radix
= radix
;
2749 gdb_printf (_("Input radix now set to "
2750 "decimal %u, hex %x, octal %o.\n"),
2751 radix
, radix
, radix
);
2755 /* The 'set output-radix' command writes to this auxiliary variable.
2756 If the requested radix is valid, OUTPUT_RADIX is updated,
2757 otherwise, it is left unchanged. */
2759 static unsigned output_radix_1
= 10;
2762 set_output_radix (const char *args
, int from_tty
, struct cmd_list_element
*c
)
2764 set_output_radix_1 (from_tty
, output_radix_1
);
2768 set_output_radix_1 (int from_tty
, unsigned radix
)
2770 /* Validate the radix and disallow ones that we aren't prepared to
2771 handle correctly, leaving the radix unchanged. */
2775 user_print_options
.output_format
= 'x'; /* hex */
2778 user_print_options
.output_format
= 0; /* decimal */
2781 user_print_options
.output_format
= 'o'; /* octal */
2784 output_radix_1
= output_radix
;
2785 error (_("Unsupported output radix ``decimal %u''; "
2786 "output radix unchanged."),
2789 output_radix_1
= output_radix
= radix
;
2792 gdb_printf (_("Output radix now set to "
2793 "decimal %u, hex %x, octal %o.\n"),
2794 radix
, radix
, radix
);
2798 /* Set both the input and output radix at once. Try to set the output radix
2799 first, since it has the most restrictive range. An radix that is valid as
2800 an output radix is also valid as an input radix.
2802 It may be useful to have an unusual input radix. If the user wishes to
2803 set an input radix that is not valid as an output radix, he needs to use
2804 the 'set input-radix' command. */
2807 set_radix (const char *arg
, int from_tty
)
2811 radix
= (arg
== NULL
) ? 10 : parse_and_eval_long (arg
);
2812 set_output_radix_1 (0, radix
);
2813 set_input_radix_1 (0, radix
);
2816 gdb_printf (_("Input and output radices now set to "
2817 "decimal %u, hex %x, octal %o.\n"),
2818 radix
, radix
, radix
);
2822 /* Show both the input and output radices. */
2825 show_radix (const char *arg
, int from_tty
)
2829 if (input_radix
== output_radix
)
2831 gdb_printf (_("Input and output radices set to "
2832 "decimal %u, hex %x, octal %o.\n"),
2833 input_radix
, input_radix
, input_radix
);
2837 gdb_printf (_("Input radix set to decimal "
2838 "%u, hex %x, octal %o.\n"),
2839 input_radix
, input_radix
, input_radix
);
2840 gdb_printf (_("Output radix set to decimal "
2841 "%u, hex %x, octal %o.\n"),
2842 output_radix
, output_radix
, output_radix
);
2848 /* Controls printing of vtbl's. */
2850 show_vtblprint (struct ui_file
*file
, int from_tty
,
2851 struct cmd_list_element
*c
, const char *value
)
2853 gdb_printf (file
, _("\
2854 Printing of C++ virtual function tables is %s.\n"),
2858 /* Controls looking up an object's derived type using what we find in
2861 show_objectprint (struct ui_file
*file
, int from_tty
,
2862 struct cmd_list_element
*c
,
2865 gdb_printf (file
, _("\
2866 Printing of object's derived type based on vtable info is %s.\n"),
2871 show_static_field_print (struct ui_file
*file
, int from_tty
,
2872 struct cmd_list_element
*c
,
2876 _("Printing of C++ static members is %s.\n"),
2882 /* A couple typedefs to make writing the options a bit more
2884 using boolean_option_def
2885 = gdb::option::boolean_option_def
<value_print_options
>;
2886 using uinteger_option_def
2887 = gdb::option::uinteger_option_def
<value_print_options
>;
2888 using pinteger_option_def
2889 = gdb::option::pinteger_option_def
<value_print_options
>;
2891 /* Extra literals supported with the `set print characters' and
2892 `print -characters' commands. */
2893 static const literal_def print_characters_literals
[] =
2895 { "elements", PRINT_MAX_CHARS_ELEMENTS
},
2896 { "unlimited", PRINT_MAX_CHARS_UNLIMITED
, 0 },
2900 /* Definitions of options for the "print" and "compile print"
2902 static const gdb::option::option_def value_print_option_defs
[] = {
2904 boolean_option_def
{
2906 [] (value_print_options
*opt
) { return &opt
->addressprint
; },
2907 show_addressprint
, /* show_cmd_cb */
2908 N_("Set printing of addresses."),
2909 N_("Show printing of addresses."),
2910 NULL
, /* help_doc */
2913 boolean_option_def
{
2915 [] (value_print_options
*opt
) { return &opt
->prettyformat_arrays
; },
2916 show_prettyformat_arrays
, /* show_cmd_cb */
2917 N_("Set pretty formatting of arrays."),
2918 N_("Show pretty formatting of arrays."),
2919 NULL
, /* help_doc */
2922 boolean_option_def
{
2924 [] (value_print_options
*opt
) { return &opt
->print_array_indexes
; },
2925 show_print_array_indexes
, /* show_cmd_cb */
2926 N_("Set printing of array indexes."),
2927 N_("Show printing of array indexes."),
2928 NULL
, /* help_doc */
2931 boolean_option_def
{
2933 [] (value_print_options
*opt
) { return &opt
->nibblesprint
; },
2934 show_nibbles
, /* show_cmd_cb */
2935 N_("Set whether to print binary values in groups of four bits."),
2936 N_("Show whether to print binary values in groups of four bits."),
2937 NULL
, /* help_doc */
2940 uinteger_option_def
{
2942 [] (value_print_options
*opt
) { return &opt
->print_max_chars
; },
2943 print_characters_literals
,
2944 show_print_max_chars
, /* show_cmd_cb */
2945 N_("Set limit on string chars to print."),
2946 N_("Show limit on string chars to print."),
2947 N_("\"elements\" causes the array element limit to be used.\n"
2948 "\"unlimited\" causes there to be no limit."),
2951 uinteger_option_def
{
2953 [] (value_print_options
*opt
) { return &opt
->print_max
; },
2954 uinteger_unlimited_literals
,
2955 show_print_max
, /* show_cmd_cb */
2956 N_("Set limit on array elements to print."),
2957 N_("Show limit on array elements to print."),
2958 N_("\"unlimited\" causes there to be no limit.\n"
2959 "This setting also applies to string chars when \"print characters\"\n"
2960 "is set to \"elements\"."),
2963 pinteger_option_def
{
2965 [] (value_print_options
*opt
) { return &opt
->max_depth
; },
2966 pinteger_unlimited_literals
,
2967 show_print_max_depth
, /* show_cmd_cb */
2968 N_("Set maximum print depth for nested structures, unions and arrays."),
2969 N_("Show maximum print depth for nested structures, unions, and arrays."),
2970 N_("When structures, unions, or arrays are nested beyond this depth then they\n\
2971 will be replaced with either '{...}' or '(...)' depending on the language.\n\
2972 Use \"unlimited\" to print the complete structure.")
2975 boolean_option_def
{
2976 "memory-tag-violations",
2977 [] (value_print_options
*opt
) { return &opt
->memory_tag_violations
; },
2978 show_memory_tag_violations
, /* show_cmd_cb */
2979 N_("Set printing of memory tag violations for pointers."),
2980 N_("Show printing of memory tag violations for pointers."),
2981 N_("Issue a warning when the printed value is a pointer\n\
2982 whose logical tag doesn't match the allocation tag of the memory\n\
2983 location it points to."),
2986 boolean_option_def
{
2988 [] (value_print_options
*opt
) { return &opt
->stop_print_at_null
; },
2989 show_stop_print_at_null
, /* show_cmd_cb */
2990 N_("Set printing of char arrays to stop at first null char."),
2991 N_("Show printing of char arrays to stop at first null char."),
2992 NULL
, /* help_doc */
2995 boolean_option_def
{
2997 [] (value_print_options
*opt
) { return &opt
->objectprint
; },
2998 show_objectprint
, /* show_cmd_cb */
2999 _("Set printing of C++ virtual function tables."),
3000 _("Show printing of C++ virtual function tables."),
3001 NULL
, /* help_doc */
3004 boolean_option_def
{
3006 [] (value_print_options
*opt
) { return &opt
->prettyformat_structs
; },
3007 show_prettyformat_structs
, /* show_cmd_cb */
3008 N_("Set pretty formatting of structures."),
3009 N_("Show pretty formatting of structures."),
3010 NULL
, /* help_doc */
3013 boolean_option_def
{
3015 [] (value_print_options
*opt
) { return &opt
->raw
; },
3016 NULL
, /* show_cmd_cb */
3017 N_("Set whether to print values in raw form."),
3018 N_("Show whether to print values in raw form."),
3019 N_("If set, values are printed in raw form, bypassing any\n\
3020 pretty-printers for that value.")
3023 uinteger_option_def
{
3025 [] (value_print_options
*opt
) { return &opt
->repeat_count_threshold
; },
3026 uinteger_unlimited_literals
,
3027 show_repeat_count_threshold
, /* show_cmd_cb */
3028 N_("Set threshold for repeated print elements."),
3029 N_("Show threshold for repeated print elements."),
3030 N_("\"unlimited\" causes all elements to be individually printed."),
3033 boolean_option_def
{
3035 [] (value_print_options
*opt
) { return &opt
->static_field_print
; },
3036 show_static_field_print
, /* show_cmd_cb */
3037 N_("Set printing of C++ static members."),
3038 N_("Show printing of C++ static members."),
3039 NULL
, /* help_doc */
3042 boolean_option_def
{
3044 [] (value_print_options
*opt
) { return &opt
->symbol_print
; },
3045 show_symbol_print
, /* show_cmd_cb */
3046 N_("Set printing of symbol names when printing pointers."),
3047 N_("Show printing of symbol names when printing pointers."),
3048 NULL
, /* help_doc */
3051 boolean_option_def
{
3053 [] (value_print_options
*opt
) { return &opt
->unionprint
; },
3054 show_unionprint
, /* show_cmd_cb */
3055 N_("Set printing of unions interior to structures."),
3056 N_("Show printing of unions interior to structures."),
3057 NULL
, /* help_doc */
3060 boolean_option_def
{
3062 [] (value_print_options
*opt
) { return &opt
->vtblprint
; },
3063 show_vtblprint
, /* show_cmd_cb */
3064 N_("Set printing of C++ virtual function tables."),
3065 N_("Show printing of C++ virtual function tables."),
3066 NULL
, /* help_doc */
3070 /* See valprint.h. */
3072 gdb::option::option_def_group
3073 make_value_print_options_def_group (value_print_options
*opts
)
3075 return {{value_print_option_defs
}, opts
};
3080 /* Test printing of TYPE_CODE_FLAGS values. */
3083 test_print_flags (gdbarch
*arch
)
3085 type
*flags_type
= arch_flags_type (arch
, "test_type", 32);
3086 type
*field_type
= builtin_type (arch
)->builtin_uint32
;
3089 Fields: CCCB BAAA */
3090 append_flags_type_field (flags_type
, 0, 3, field_type
, "A");
3091 append_flags_type_field (flags_type
, 3, 2, field_type
, "B");
3092 append_flags_type_field (flags_type
, 5, 3, field_type
, "C");
3094 value
*val
= allocate_value (flags_type
);
3095 gdb_byte
*contents
= value_contents_writeable (val
).data ();
3096 store_unsigned_integer (contents
, 4, gdbarch_byte_order (arch
), 0xaa);
3099 val_print_type_code_flags (flags_type
, val
, 0, &out
);
3100 SELF_CHECK (out
.string () == "[ A=2 B=1 C=5 ]");
3105 void _initialize_valprint ();
3107 _initialize_valprint ()
3110 selftests::register_test_foreach_arch ("print-flags", test_print_flags
);
3113 set_show_commands setshow_print_cmds
3114 = add_setshow_prefix_cmd ("print", no_class
,
3115 _("Generic command for setting how things print."),
3116 _("Generic command for showing print settings."),
3117 &setprintlist
, &showprintlist
,
3118 &setlist
, &showlist
);
3119 add_alias_cmd ("p", setshow_print_cmds
.set
, no_class
, 1, &setlist
);
3120 /* Prefer set print to set prompt. */
3121 add_alias_cmd ("pr", setshow_print_cmds
.set
, no_class
, 1, &setlist
);
3122 add_alias_cmd ("p", setshow_print_cmds
.show
, no_class
, 1, &showlist
);
3123 add_alias_cmd ("pr", setshow_print_cmds
.show
, no_class
, 1, &showlist
);
3125 set_show_commands setshow_print_raw_cmds
3126 = add_setshow_prefix_cmd
3128 _("Generic command for setting what things to print in \"raw\" mode."),
3129 _("Generic command for showing \"print raw\" settings."),
3130 &setprintrawlist
, &showprintrawlist
, &setprintlist
, &showprintlist
);
3131 deprecate_cmd (setshow_print_raw_cmds
.set
, nullptr);
3132 deprecate_cmd (setshow_print_raw_cmds
.show
, nullptr);
3134 gdb::option::add_setshow_cmds_for_options
3135 (class_support
, &user_print_options
, value_print_option_defs
,
3136 &setprintlist
, &showprintlist
);
3138 add_setshow_zuinteger_cmd ("input-radix", class_support
, &input_radix_1
,
3140 Set default input radix for entering numbers."), _("\
3141 Show default input radix for entering numbers."), NULL
,
3144 &setlist
, &showlist
);
3146 add_setshow_zuinteger_cmd ("output-radix", class_support
, &output_radix_1
,
3148 Set default output radix for printing of values."), _("\
3149 Show default output radix for printing of values."), NULL
,
3152 &setlist
, &showlist
);
3154 /* The "set radix" and "show radix" commands are special in that
3155 they are like normal set and show commands but allow two normally
3156 independent variables to be either set or shown with a single
3157 command. So the usual deprecated_add_set_cmd() and [deleted]
3158 add_show_from_set() commands aren't really appropriate. */
3159 /* FIXME: i18n: With the new add_setshow_integer command, that is no
3160 longer true - show can display anything. */
3161 add_cmd ("radix", class_support
, set_radix
, _("\
3162 Set default input and output number radices.\n\
3163 Use 'set input-radix' or 'set output-radix' to independently set each.\n\
3164 Without an argument, sets both radices back to the default value of 10."),
3166 add_cmd ("radix", class_support
, show_radix
, _("\
3167 Show the default input and output number radices.\n\
3168 Use 'show input-radix' or 'show output-radix' to independently show each."),