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 (val
->bits_any_optimized_out (TARGET_CHAR_BIT
* embedded_offset
,
377 TARGET_CHAR_BIT
* type
->length ()))
379 val_print_optimized_out (val
, stream
);
383 if (val
->bits_synthetic_pointer (TARGET_CHAR_BIT
* embedded_offset
,
384 TARGET_CHAR_BIT
* type
->length ()))
386 const int is_ref
= type
->code () == TYPE_CODE_REF
;
387 int ref_is_addressable
= 0;
391 const struct value
*deref_val
= coerce_ref_if_computed (val
);
393 if (deref_val
!= NULL
)
394 ref_is_addressable
= deref_val
->lval () == lval_memory
;
397 if (!is_ref
|| !ref_is_addressable
)
398 fputs_styled (_("<synthetic pointer>"), metadata_style
.style (),
401 /* C++ references should be valid even if they're synthetic. */
405 if (!val
->bytes_available (embedded_offset
, type
->length ()))
407 val_print_unavailable (stream
);
416 val_print_optimized_out (const struct value
*val
, struct ui_file
*stream
)
418 if (val
!= NULL
&& val
->lval () == lval_register
)
419 val_print_not_saved (stream
);
421 fprintf_styled (stream
, metadata_style
.style (), _("<optimized out>"));
425 val_print_not_saved (struct ui_file
*stream
)
427 fprintf_styled (stream
, metadata_style
.style (), _("<not saved>"));
431 val_print_unavailable (struct ui_file
*stream
)
433 fprintf_styled (stream
, metadata_style
.style (), _("<unavailable>"));
437 val_print_invalid_address (struct ui_file
*stream
)
439 fprintf_styled (stream
, metadata_style
.style (), _("<invalid address>"));
442 /* Print a pointer based on the type of its target.
444 Arguments to this functions are roughly the same as those in
445 generic_val_print. A difference is that ADDRESS is the address to print,
446 with embedded_offset already added. ELTTYPE represents
447 the pointed type after check_typedef. */
450 print_unpacked_pointer (struct type
*type
, struct type
*elttype
,
451 CORE_ADDR address
, struct ui_file
*stream
,
452 const struct value_print_options
*options
)
454 struct gdbarch
*gdbarch
= type
->arch ();
456 if (elttype
->code () == TYPE_CODE_FUNC
)
458 /* Try to print what function it points to. */
459 print_function_pointer_address (options
, gdbarch
, address
, stream
);
463 if (options
->symbol_print
)
464 print_address_demangle (options
, gdbarch
, address
, stream
, demangle
);
465 else if (options
->addressprint
)
466 gdb_puts (paddress (gdbarch
, address
), stream
);
469 /* generic_val_print helper for TYPE_CODE_ARRAY. */
472 generic_val_print_array (struct value
*val
,
473 struct ui_file
*stream
, int recurse
,
474 const struct value_print_options
*options
,
476 generic_val_print_decorations
*decorations
)
478 struct type
*type
= check_typedef (val
->type ());
479 struct type
*unresolved_elttype
= type
->target_type ();
480 struct type
*elttype
= check_typedef (unresolved_elttype
);
482 if (type
->length () > 0 && unresolved_elttype
->length () > 0)
484 LONGEST low_bound
, high_bound
;
486 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
487 error (_("Could not determine the array high bound"));
489 gdb_puts (decorations
->array_start
, stream
);
490 value_print_array_elements (val
, stream
, recurse
, options
, 0);
491 gdb_puts (decorations
->array_end
, stream
);
495 /* Array of unspecified length: treat like pointer to first elt. */
496 print_unpacked_pointer (type
, elttype
, val
->address (),
502 /* generic_value_print helper for TYPE_CODE_PTR. */
505 generic_value_print_ptr (struct value
*val
, struct ui_file
*stream
,
506 const struct value_print_options
*options
)
509 if (options
->format
&& options
->format
!= 's')
510 value_print_scalar_formatted (val
, options
, 0, stream
);
513 struct type
*type
= check_typedef (val
->type ());
514 struct type
*elttype
= check_typedef (type
->target_type ());
515 const gdb_byte
*valaddr
= val
->contents_for_printing ().data ();
516 CORE_ADDR addr
= unpack_pointer (type
, valaddr
);
518 print_unpacked_pointer (type
, elttype
, addr
, stream
, options
);
523 /* Print '@' followed by the address contained in ADDRESS_BUFFER. */
526 print_ref_address (struct type
*type
, const gdb_byte
*address_buffer
,
527 int embedded_offset
, struct ui_file
*stream
)
529 struct gdbarch
*gdbarch
= type
->arch ();
531 if (address_buffer
!= NULL
)
534 = extract_typed_address (address_buffer
+ embedded_offset
, type
);
536 gdb_printf (stream
, "@");
537 gdb_puts (paddress (gdbarch
, address
), stream
);
539 /* Else: we have a non-addressable value, such as a DW_AT_const_value. */
542 /* If VAL is addressable, return the value contents buffer of a value that
543 represents a pointer to VAL. Otherwise return NULL. */
545 static const gdb_byte
*
546 get_value_addr_contents (struct value
*deref_val
)
548 gdb_assert (deref_val
!= NULL
);
550 if (deref_val
->lval () == lval_memory
)
551 return value_addr (deref_val
)->contents_for_printing ().data ();
554 /* We have a non-addressable value, such as a DW_AT_const_value. */
559 /* generic_val_print helper for TYPE_CODE_{RVALUE_,}REF. */
562 generic_val_print_ref (struct type
*type
,
563 int embedded_offset
, struct ui_file
*stream
, int recurse
,
564 struct value
*original_value
,
565 const struct value_print_options
*options
)
567 struct type
*elttype
= check_typedef (type
->target_type ());
568 struct value
*deref_val
= NULL
;
569 const bool value_is_synthetic
570 = original_value
->bits_synthetic_pointer (TARGET_CHAR_BIT
* embedded_offset
,
571 TARGET_CHAR_BIT
* type
->length ());
572 const int must_coerce_ref
= ((options
->addressprint
&& value_is_synthetic
)
573 || options
->deref_ref
);
574 const int type_is_defined
= elttype
->code () != TYPE_CODE_UNDEF
;
575 const gdb_byte
*valaddr
= original_value
->contents_for_printing ().data ();
577 if (must_coerce_ref
&& type_is_defined
)
579 deref_val
= coerce_ref_if_computed (original_value
);
581 if (deref_val
!= NULL
)
583 /* More complicated computed references are not supported. */
584 gdb_assert (embedded_offset
== 0);
587 deref_val
= value_at (type
->target_type (),
588 unpack_pointer (type
, valaddr
+ embedded_offset
));
590 /* Else, original_value isn't a synthetic reference or we don't have to print
591 the reference's contents.
593 Notice that for references to TYPE_CODE_STRUCT, 'set print object on' will
594 cause original_value to be a not_lval instead of an lval_computed,
595 which will make value_bits_synthetic_pointer return false.
596 This happens because if options->objectprint is true, c_value_print will
597 overwrite original_value's contents with the result of coercing
598 the reference through value_addr, and then set its type back to
599 TYPE_CODE_REF. In that case we don't have to coerce the reference again;
600 we can simply treat it as non-synthetic and move on. */
602 if (options
->addressprint
)
604 const gdb_byte
*address
= (value_is_synthetic
&& type_is_defined
605 ? get_value_addr_contents (deref_val
)
608 print_ref_address (type
, address
, embedded_offset
, stream
);
610 if (options
->deref_ref
)
611 gdb_puts (": ", stream
);
614 if (options
->deref_ref
)
617 common_val_print (deref_val
, stream
, recurse
, options
,
620 gdb_puts ("???", stream
);
624 /* Helper function for generic_val_print_enum.
625 This is also used to print enums in TYPE_CODE_FLAGS values. */
628 generic_val_print_enum_1 (struct type
*type
, LONGEST val
,
629 struct ui_file
*stream
)
634 len
= type
->num_fields ();
635 for (i
= 0; i
< len
; i
++)
638 if (val
== type
->field (i
).loc_enumval ())
645 fputs_styled (type
->field (i
).name (), variable_name_style
.style (),
648 else if (type
->is_flag_enum ())
652 /* We have a "flag" enum, so we try to decompose it into pieces as
653 appropriate. The enum may have multiple enumerators representing
654 the same bit, in which case we choose to only print the first one
656 for (i
= 0; i
< len
; ++i
)
660 ULONGEST enumval
= type
->field (i
).loc_enumval ();
661 int nbits
= count_one_bits_ll (enumval
);
663 gdb_assert (nbits
== 0 || nbits
== 1);
665 if ((val
& enumval
) != 0)
669 gdb_puts ("(", stream
);
673 gdb_puts (" | ", stream
);
675 val
&= ~type
->field (i
).loc_enumval ();
676 fputs_styled (type
->field (i
).name (),
677 variable_name_style
.style (), stream
);
683 /* There are leftover bits, print them. */
685 gdb_puts ("(", stream
);
687 gdb_puts (" | ", stream
);
689 gdb_puts ("unknown: 0x", stream
);
690 print_longest (stream
, 'x', 0, val
);
691 gdb_puts (")", stream
);
695 /* Nothing has been printed and the value is 0, the enum value must
697 gdb_puts ("0", stream
);
701 /* Something has been printed, close the parenthesis. */
702 gdb_puts (")", stream
);
706 print_longest (stream
, 'd', 0, val
);
709 /* generic_val_print helper for TYPE_CODE_ENUM. */
712 generic_val_print_enum (struct type
*type
,
713 int embedded_offset
, struct ui_file
*stream
,
714 struct value
*original_value
,
715 const struct value_print_options
*options
)
718 struct gdbarch
*gdbarch
= type
->arch ();
719 int unit_size
= gdbarch_addressable_memory_unit_size (gdbarch
);
721 gdb_assert (!options
->format
);
723 const gdb_byte
*valaddr
= original_value
->contents_for_printing ().data ();
725 val
= unpack_long (type
, valaddr
+ embedded_offset
* unit_size
);
727 generic_val_print_enum_1 (type
, val
, stream
);
730 /* generic_val_print helper for TYPE_CODE_FUNC and TYPE_CODE_METHOD. */
733 generic_val_print_func (struct type
*type
,
734 int embedded_offset
, CORE_ADDR address
,
735 struct ui_file
*stream
,
736 struct value
*original_value
,
737 const struct value_print_options
*options
)
739 struct gdbarch
*gdbarch
= type
->arch ();
741 gdb_assert (!options
->format
);
743 /* FIXME, we should consider, at least for ANSI C language,
744 eliminating the distinction made between FUNCs and POINTERs to
746 gdb_printf (stream
, "{");
747 type_print (type
, "", stream
, -1);
748 gdb_printf (stream
, "} ");
749 /* Try to print what function it points to, and its address. */
750 print_address_demangle (options
, gdbarch
, address
, stream
, demangle
);
753 /* generic_value_print helper for TYPE_CODE_BOOL. */
756 generic_value_print_bool
757 (struct value
*value
, struct ui_file
*stream
,
758 const struct value_print_options
*options
,
759 const struct generic_val_print_decorations
*decorations
)
761 if (options
->format
|| options
->output_format
)
763 struct value_print_options opts
= *options
;
764 opts
.format
= (options
->format
? options
->format
765 : options
->output_format
);
766 value_print_scalar_formatted (value
, &opts
, 0, stream
);
770 const gdb_byte
*valaddr
= value
->contents_for_printing ().data ();
771 struct type
*type
= check_typedef (value
->type ());
772 LONGEST val
= unpack_long (type
, valaddr
);
774 gdb_puts (decorations
->false_name
, stream
);
776 gdb_puts (decorations
->true_name
, stream
);
778 print_longest (stream
, 'd', 0, val
);
782 /* generic_value_print helper for TYPE_CODE_INT. */
785 generic_value_print_int (struct value
*val
, struct ui_file
*stream
,
786 const struct value_print_options
*options
)
788 struct value_print_options opts
= *options
;
790 opts
.format
= (options
->format
? options
->format
791 : options
->output_format
);
792 value_print_scalar_formatted (val
, &opts
, 0, stream
);
795 /* generic_value_print helper for TYPE_CODE_CHAR. */
798 generic_value_print_char (struct value
*value
, struct ui_file
*stream
,
799 const struct value_print_options
*options
)
801 if (options
->format
|| options
->output_format
)
803 struct value_print_options opts
= *options
;
805 opts
.format
= (options
->format
? options
->format
806 : options
->output_format
);
807 value_print_scalar_formatted (value
, &opts
, 0, stream
);
811 struct type
*unresolved_type
= value
->type ();
812 struct type
*type
= check_typedef (unresolved_type
);
813 const gdb_byte
*valaddr
= value
->contents_for_printing ().data ();
815 LONGEST val
= unpack_long (type
, valaddr
);
816 if (type
->is_unsigned ())
817 gdb_printf (stream
, "%u", (unsigned int) val
);
819 gdb_printf (stream
, "%d", (int) val
);
820 gdb_puts (" ", stream
);
821 current_language
->printchar (val
, unresolved_type
, stream
);
825 /* generic_val_print helper for TYPE_CODE_FLT and TYPE_CODE_DECFLOAT. */
828 generic_val_print_float (struct type
*type
, struct ui_file
*stream
,
829 struct value
*original_value
,
830 const struct value_print_options
*options
)
832 gdb_assert (!options
->format
);
834 const gdb_byte
*valaddr
= original_value
->contents_for_printing ().data ();
836 print_floating (valaddr
, type
, stream
);
839 /* generic_val_print helper for TYPE_CODE_FIXED_POINT. */
842 generic_val_print_fixed_point (struct value
*val
, struct ui_file
*stream
,
843 const struct value_print_options
*options
)
846 value_print_scalar_formatted (val
, options
, 0, stream
);
849 struct type
*type
= val
->type ();
851 const gdb_byte
*valaddr
= val
->contents_for_printing ().data ();
854 f
.read_fixed_point (gdb::make_array_view (valaddr
, type
->length ()),
855 type_byte_order (type
), type
->is_unsigned (),
856 type
->fixed_point_scaling_factor ());
858 const char *fmt
= type
->length () < 4 ? "%.11Fg" : "%.17Fg";
859 std::string str
= f
.str (fmt
);
860 gdb_printf (stream
, "%s", str
.c_str ());
864 /* generic_value_print helper for TYPE_CODE_COMPLEX. */
867 generic_value_print_complex (struct value
*val
, struct ui_file
*stream
,
868 const struct value_print_options
*options
,
869 const struct generic_val_print_decorations
872 gdb_printf (stream
, "%s", decorations
->complex_prefix
);
874 struct value
*real_part
= value_real_part (val
);
875 value_print_scalar_formatted (real_part
, options
, 0, stream
);
876 gdb_printf (stream
, "%s", decorations
->complex_infix
);
878 struct value
*imag_part
= value_imaginary_part (val
);
879 value_print_scalar_formatted (imag_part
, options
, 0, stream
);
880 gdb_printf (stream
, "%s", decorations
->complex_suffix
);
883 /* generic_value_print helper for TYPE_CODE_MEMBERPTR. */
886 generic_value_print_memberptr
887 (struct value
*val
, struct ui_file
*stream
,
889 const struct value_print_options
*options
,
890 const struct generic_val_print_decorations
*decorations
)
892 if (!options
->format
)
894 /* Member pointers are essentially specific to C++, and so if we
895 encounter one, we should print it according to C++ rules. */
896 struct type
*type
= check_typedef (val
->type ());
897 const gdb_byte
*valaddr
= val
->contents_for_printing ().data ();
898 cp_print_class_member (valaddr
, type
, stream
, "&");
901 value_print_scalar_formatted (val
, options
, 0, stream
);
904 /* See valprint.h. */
907 generic_value_print (struct value
*val
, struct ui_file
*stream
, int recurse
,
908 const struct value_print_options
*options
,
909 const struct generic_val_print_decorations
*decorations
)
911 struct type
*type
= val
->type ();
913 type
= check_typedef (type
);
915 if (is_fixed_point_type (type
))
916 type
= type
->fixed_point_type_base_type ();
918 /* Widen a subrange to its target type, then use that type's
920 while (type
->code () == TYPE_CODE_RANGE
)
922 type
= check_typedef (type
->target_type ());
923 val
= value_cast (type
, val
);
926 switch (type
->code ())
928 case TYPE_CODE_ARRAY
:
929 generic_val_print_array (val
, stream
, recurse
, options
, decorations
);
932 case TYPE_CODE_MEMBERPTR
:
933 generic_value_print_memberptr (val
, stream
, recurse
, options
,
938 generic_value_print_ptr (val
, stream
, options
);
942 case TYPE_CODE_RVALUE_REF
:
943 generic_val_print_ref (type
, 0, stream
, recurse
,
949 value_print_scalar_formatted (val
, options
, 0, stream
);
951 generic_val_print_enum (type
, 0, stream
, val
, options
);
954 case TYPE_CODE_FLAGS
:
956 value_print_scalar_formatted (val
, options
, 0, stream
);
958 val_print_type_code_flags (type
, val
, 0, stream
);
962 case TYPE_CODE_METHOD
:
964 value_print_scalar_formatted (val
, options
, 0, stream
);
966 generic_val_print_func (type
, 0, val
->address (), stream
,
971 generic_value_print_bool (val
, stream
, options
, decorations
);
975 generic_value_print_int (val
, stream
, options
);
979 generic_value_print_char (val
, stream
, options
);
983 case TYPE_CODE_DECFLOAT
:
985 value_print_scalar_formatted (val
, options
, 0, stream
);
987 generic_val_print_float (type
, stream
, val
, options
);
990 case TYPE_CODE_FIXED_POINT
:
991 generic_val_print_fixed_point (val
, stream
, options
);
995 gdb_puts (decorations
->void_name
, stream
);
998 case TYPE_CODE_ERROR
:
999 gdb_printf (stream
, "%s", TYPE_ERROR_NAME (type
));
1002 case TYPE_CODE_UNDEF
:
1003 /* This happens (without TYPE_STUB set) on systems which don't use
1004 dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
1005 and no complete type for struct foo in that file. */
1006 fprintf_styled (stream
, metadata_style
.style (), _("<incomplete type>"));
1009 case TYPE_CODE_COMPLEX
:
1010 generic_value_print_complex (val
, stream
, options
, decorations
);
1013 case TYPE_CODE_METHODPTR
:
1014 cplus_print_method_ptr (val
->contents_for_printing ().data (), type
,
1018 case TYPE_CODE_UNION
:
1019 case TYPE_CODE_STRUCT
:
1021 error (_("Unhandled type code %d in symbol table."),
1026 /* Print using the given LANGUAGE the value VAL onto stream STREAM according
1029 This is a preferable interface to val_print, above, because it uses
1030 GDB's value mechanism. */
1033 common_val_print (struct value
*value
, struct ui_file
*stream
, int recurse
,
1034 const struct value_print_options
*options
,
1035 const struct language_defn
*language
)
1037 if (language
->la_language
== language_ada
)
1038 /* The value might have a dynamic type, which would cause trouble
1039 below when trying to extract the value contents (since the value
1040 size is determined from the type size which is unknown). So
1041 get a fixed representation of our value. */
1042 value
= ada_to_fixed_value (value
);
1045 value
->fetch_lazy ();
1047 struct value_print_options local_opts
= *options
;
1048 struct type
*type
= value
->type ();
1049 struct type
*real_type
= check_typedef (type
);
1051 if (local_opts
.prettyformat
== Val_prettyformat_default
)
1052 local_opts
.prettyformat
= (local_opts
.prettyformat_structs
1053 ? Val_prettyformat
: Val_no_prettyformat
);
1057 /* Ensure that the type is complete and not just a stub. If the type is
1058 only a stub and we can't find and substitute its complete type, then
1059 print appropriate string and return. */
1061 if (real_type
->is_stub ())
1063 fprintf_styled (stream
, metadata_style
.style (), _("<incomplete type>"));
1067 if (!valprint_check_validity (stream
, real_type
, 0, value
))
1072 if (apply_ext_lang_val_pretty_printer (value
, stream
, recurse
, options
,
1077 /* Handle summary mode. If the value is a scalar, print it;
1078 otherwise, print an ellipsis. */
1079 if (options
->summary
&& !val_print_scalar_type_p (type
))
1081 gdb_printf (stream
, "...");
1085 /* If this value is too deep then don't print it. */
1086 if (!val_print_scalar_or_string_type_p (type
, language
)
1087 && val_print_check_max_depth (stream
, recurse
, options
, language
))
1092 language
->value_print_inner (value
, stream
, recurse
, &local_opts
);
1094 catch (const gdb_exception_error
&except
)
1096 fprintf_styled (stream
, metadata_style
.style (),
1097 _("<error reading variable: %s>"), except
.what ());
1101 /* See valprint.h. */
1104 val_print_check_max_depth (struct ui_file
*stream
, int recurse
,
1105 const struct value_print_options
*options
,
1106 const struct language_defn
*language
)
1108 if (options
->max_depth
> -1 && recurse
>= options
->max_depth
)
1110 gdb_assert (language
->struct_too_deep_ellipsis () != NULL
);
1111 gdb_puts (language
->struct_too_deep_ellipsis (), stream
);
1118 /* Check whether the value VAL is printable. Return 1 if it is;
1119 return 0 and print an appropriate error message to STREAM according to
1120 OPTIONS if it is not. */
1123 value_check_printable (struct value
*val
, struct ui_file
*stream
,
1124 const struct value_print_options
*options
)
1128 fprintf_styled (stream
, metadata_style
.style (),
1129 _("<address of value unknown>"));
1133 if (val
->entirely_optimized_out ())
1135 if (options
->summary
&& !val_print_scalar_type_p (val
->type ()))
1136 gdb_printf (stream
, "...");
1138 val_print_optimized_out (val
, stream
);
1142 if (val
->entirely_unavailable ())
1144 if (options
->summary
&& !val_print_scalar_type_p (val
->type ()))
1145 gdb_printf (stream
, "...");
1147 val_print_unavailable (stream
);
1151 if (val
->type ()->code () == TYPE_CODE_INTERNAL_FUNCTION
)
1153 fprintf_styled (stream
, metadata_style
.style (),
1154 _("<internal function %s>"),
1155 value_internal_function_name (val
));
1159 if (type_not_associated (val
->type ()))
1161 val_print_not_associated (stream
);
1165 if (type_not_allocated (val
->type ()))
1167 val_print_not_allocated (stream
);
1174 /* See valprint.h. */
1177 common_val_print_checked (struct value
*val
, struct ui_file
*stream
,
1179 const struct value_print_options
*options
,
1180 const struct language_defn
*language
)
1182 if (!value_check_printable (val
, stream
, options
))
1184 common_val_print (val
, stream
, recurse
, options
, language
);
1187 /* Print on stream STREAM the value VAL according to OPTIONS. The value
1188 is printed using the current_language syntax. */
1191 value_print (struct value
*val
, struct ui_file
*stream
,
1192 const struct value_print_options
*options
)
1194 scoped_value_mark free_values
;
1196 if (!value_check_printable (val
, stream
, options
))
1202 = apply_ext_lang_val_pretty_printer (val
, stream
, 0, options
,
1209 current_language
->value_print (val
, stream
, options
);
1212 /* Meant to be used in debug sessions, so don't export it in a header file. */
1213 extern void ATTRIBUTE_UNUSED
debug_val (struct value
*val
);
1217 void ATTRIBUTE_UNUSED
1218 debug_val (struct value
*val
)
1220 value_print (val
, gdb_stdlog
, &user_print_options
);
1221 gdb_flush (gdb_stdlog
);
1225 val_print_type_code_flags (struct type
*type
, struct value
*original_value
,
1226 int embedded_offset
, struct ui_file
*stream
)
1228 const gdb_byte
*valaddr
= (original_value
->contents_for_printing ().data ()
1230 ULONGEST val
= unpack_long (type
, valaddr
);
1231 int field
, nfields
= type
->num_fields ();
1232 struct gdbarch
*gdbarch
= type
->arch ();
1233 struct type
*bool_type
= builtin_type (gdbarch
)->builtin_bool
;
1235 gdb_puts ("[", stream
);
1236 for (field
= 0; field
< nfields
; field
++)
1238 if (type
->field (field
).name ()[0] != '\0')
1240 struct type
*field_type
= type
->field (field
).type ();
1242 if (field_type
== bool_type
1243 /* We require boolean types here to be one bit wide. This is a
1244 problematic place to notify the user of an internal error
1245 though. Instead just fall through and print the field as an
1247 && TYPE_FIELD_BITSIZE (type
, field
) == 1)
1249 if (val
& ((ULONGEST
)1 << type
->field (field
).loc_bitpos ()))
1252 styled_string (variable_name_style
.style (),
1253 type
->field (field
).name ()));
1257 unsigned field_len
= TYPE_FIELD_BITSIZE (type
, field
);
1258 ULONGEST field_val
= val
>> type
->field (field
).loc_bitpos ();
1260 if (field_len
< sizeof (ULONGEST
) * TARGET_CHAR_BIT
)
1261 field_val
&= ((ULONGEST
) 1 << field_len
) - 1;
1262 gdb_printf (stream
, " %ps=",
1263 styled_string (variable_name_style
.style (),
1264 type
->field (field
).name ()));
1265 if (field_type
->code () == TYPE_CODE_ENUM
)
1266 generic_val_print_enum_1 (field_type
, field_val
, stream
);
1268 print_longest (stream
, 'd', 0, field_val
);
1272 gdb_puts (" ]", stream
);
1275 /* See valprint.h. */
1278 value_print_scalar_formatted (struct value
*val
,
1279 const struct value_print_options
*options
,
1281 struct ui_file
*stream
)
1283 struct type
*type
= check_typedef (val
->type ());
1285 gdb_assert (val
!= NULL
);
1287 /* If we get here with a string format, try again without it. Go
1288 all the way back to the language printers, which may call us
1290 if (options
->format
== 's')
1292 struct value_print_options opts
= *options
;
1294 opts
.deref_ref
= false;
1295 common_val_print (val
, stream
, 0, &opts
, current_language
);
1299 /* value_contents_for_printing fetches all VAL's contents. They are
1300 needed to check whether VAL is optimized-out or unavailable
1302 const gdb_byte
*valaddr
= val
->contents_for_printing ().data ();
1304 /* A scalar object that does not have all bits available can't be
1305 printed, because all bits contribute to its representation. */
1306 if (val
->bits_any_optimized_out (0,
1307 TARGET_CHAR_BIT
* type
->length ()))
1308 val_print_optimized_out (val
, stream
);
1309 else if (!val
->bytes_available (0, type
->length ()))
1310 val_print_unavailable (stream
);
1312 print_scalar_formatted (valaddr
, type
, options
, size
, stream
);
1315 /* Print a number according to FORMAT which is one of d,u,x,o,b,h,w,g.
1316 The raison d'etre of this function is to consolidate printing of
1317 LONG_LONG's into this one function. The format chars b,h,w,g are
1318 from print_scalar_formatted(). Numbers are printed using C
1321 USE_C_FORMAT means to use C format in all cases. Without it,
1322 'o' and 'x' format do not include the standard C radix prefix
1325 Hilfinger/2004-09-09: USE_C_FORMAT was originally called USE_LOCAL
1326 and was intended to request formatting according to the current
1327 language and would be used for most integers that GDB prints. The
1328 exceptional cases were things like protocols where the format of
1329 the integer is a protocol thing, not a user-visible thing). The
1330 parameter remains to preserve the information of what things might
1331 be printed with language-specific format, should we ever resurrect
1335 print_longest (struct ui_file
*stream
, int format
, int use_c_format
,
1343 val
= int_string (val_long
, 10, 1, 0, 1); break;
1345 val
= int_string (val_long
, 10, 0, 0, 1); break;
1347 val
= int_string (val_long
, 16, 0, 0, use_c_format
); break;
1349 val
= int_string (val_long
, 16, 0, 2, 1); break;
1351 val
= int_string (val_long
, 16, 0, 4, 1); break;
1353 val
= int_string (val_long
, 16, 0, 8, 1); break;
1355 val
= int_string (val_long
, 16, 0, 16, 1); break;
1358 val
= int_string (val_long
, 8, 0, 0, use_c_format
); break;
1360 internal_error (_("failed internal consistency check"));
1362 gdb_puts (val
, stream
);
1365 /* This used to be a macro, but I don't think it is called often enough
1366 to merit such treatment. */
1367 /* Convert a LONGEST to an int. This is used in contexts (e.g. number of
1368 arguments to a function, number in a value history, register number, etc.)
1369 where the value must not be larger than can fit in an int. */
1372 longest_to_int (LONGEST arg
)
1374 /* Let the compiler do the work. */
1375 int rtnval
= (int) arg
;
1377 /* Check for overflows or underflows. */
1378 if (sizeof (LONGEST
) > sizeof (int))
1382 error (_("Value out of range."));
1388 /* Print a floating point value of floating-point type TYPE,
1389 pointed to in GDB by VALADDR, on STREAM. */
1392 print_floating (const gdb_byte
*valaddr
, struct type
*type
,
1393 struct ui_file
*stream
)
1395 std::string str
= target_float_to_string (valaddr
, type
);
1396 gdb_puts (str
.c_str (), stream
);
1400 print_binary_chars (struct ui_file
*stream
, const gdb_byte
*valaddr
,
1401 unsigned len
, enum bfd_endian byte_order
, bool zero_pad
,
1402 const struct value_print_options
*options
)
1407 bool seen_a_one
= false;
1408 const char *digit_separator
= nullptr;
1410 /* Declared "int" so it will be signed.
1411 This ensures that right shift will shift in zeros. */
1413 const int mask
= 0x080;
1415 if (options
->nibblesprint
)
1416 digit_separator
= current_language
->get_digit_separator();
1418 if (byte_order
== BFD_ENDIAN_BIG
)
1424 /* Every byte has 8 binary characters; peel off
1425 and print from the MSB end. */
1427 for (i
= 0; i
< (HOST_CHAR_BIT
* sizeof (*p
)); i
++)
1429 if (options
->nibblesprint
&& seen_a_one
&& i
% 4 == 0)
1430 gdb_putc (*digit_separator
, stream
);
1432 if (*p
& (mask
>> i
))
1437 if (zero_pad
|| seen_a_one
|| b
== '1')
1438 gdb_putc (b
, stream
);
1439 else if (options
->nibblesprint
)
1441 if ((0xf0 & (mask
>> i
) && (*p
& 0xf0))
1442 || (0x0f & (mask
>> i
) && (*p
& 0x0f)))
1443 gdb_putc (b
, stream
);
1453 for (p
= valaddr
+ len
- 1;
1457 for (i
= 0; i
< (HOST_CHAR_BIT
* sizeof (*p
)); i
++)
1459 if (options
->nibblesprint
&& seen_a_one
&& i
% 4 == 0)
1460 gdb_putc (*digit_separator
, stream
);
1462 if (*p
& (mask
>> i
))
1467 if (zero_pad
|| seen_a_one
|| b
== '1')
1468 gdb_putc (b
, stream
);
1469 else if (options
->nibblesprint
)
1471 if ((0xf0 & (mask
>> i
) && (*p
& 0xf0))
1472 || (0x0f & (mask
>> i
) && (*p
& 0x0f)))
1473 gdb_putc (b
, stream
);
1482 /* When not zero-padding, ensure that something is printed when the
1484 if (!zero_pad
&& !seen_a_one
)
1485 gdb_putc ('0', stream
);
1488 /* A helper for print_octal_chars that emits a single octal digit,
1489 optionally suppressing it if is zero and updating SEEN_A_ONE. */
1492 emit_octal_digit (struct ui_file
*stream
, bool *seen_a_one
, int digit
)
1494 if (*seen_a_one
|| digit
!= 0)
1495 gdb_printf (stream
, "%o", digit
);
1500 /* VALADDR points to an integer of LEN bytes.
1501 Print it in octal on stream or format it in buf. */
1504 print_octal_chars (struct ui_file
*stream
, const gdb_byte
*valaddr
,
1505 unsigned len
, enum bfd_endian byte_order
)
1508 unsigned char octa1
, octa2
, octa3
, carry
;
1511 /* Octal is 3 bits, which doesn't fit. Yuk. So we have to track
1512 * the extra bits, which cycle every three bytes:
1514 * Byte side: 0 1 2 3
1516 * bit number 123 456 78 | 9 012 345 6 | 78 901 234 | 567 890 12 |
1518 * Octal side: 0 1 carry 3 4 carry ...
1520 * Cycle number: 0 1 2
1522 * But of course we are printing from the high side, so we have to
1523 * figure out where in the cycle we are so that we end up with no
1524 * left over bits at the end.
1526 #define BITS_IN_OCTAL 3
1527 #define HIGH_ZERO 0340
1528 #define LOW_ZERO 0034
1529 #define CARRY_ZERO 0003
1530 static_assert (HIGH_ZERO
+ LOW_ZERO
+ CARRY_ZERO
== 0xff,
1531 "cycle zero constants are wrong");
1532 #define HIGH_ONE 0200
1533 #define MID_ONE 0160
1534 #define LOW_ONE 0016
1535 #define CARRY_ONE 0001
1536 static_assert (HIGH_ONE
+ MID_ONE
+ LOW_ONE
+ CARRY_ONE
== 0xff,
1537 "cycle one constants are wrong");
1538 #define HIGH_TWO 0300
1539 #define MID_TWO 0070
1540 #define LOW_TWO 0007
1541 static_assert (HIGH_TWO
+ MID_TWO
+ LOW_TWO
== 0xff,
1542 "cycle two constants are wrong");
1544 /* For 32 we start in cycle 2, with two bits and one bit carry;
1545 for 64 in cycle in cycle 1, with one bit and a two bit carry. */
1547 cycle
= (len
* HOST_CHAR_BIT
) % BITS_IN_OCTAL
;
1550 gdb_puts ("0", stream
);
1551 bool seen_a_one
= false;
1552 if (byte_order
== BFD_ENDIAN_BIG
)
1561 /* No carry in, carry out two bits. */
1563 octa1
= (HIGH_ZERO
& *p
) >> 5;
1564 octa2
= (LOW_ZERO
& *p
) >> 2;
1565 carry
= (CARRY_ZERO
& *p
);
1566 emit_octal_digit (stream
, &seen_a_one
, octa1
);
1567 emit_octal_digit (stream
, &seen_a_one
, octa2
);
1571 /* Carry in two bits, carry out one bit. */
1573 octa1
= (carry
<< 1) | ((HIGH_ONE
& *p
) >> 7);
1574 octa2
= (MID_ONE
& *p
) >> 4;
1575 octa3
= (LOW_ONE
& *p
) >> 1;
1576 carry
= (CARRY_ONE
& *p
);
1577 emit_octal_digit (stream
, &seen_a_one
, octa1
);
1578 emit_octal_digit (stream
, &seen_a_one
, octa2
);
1579 emit_octal_digit (stream
, &seen_a_one
, octa3
);
1583 /* Carry in one bit, no carry out. */
1585 octa1
= (carry
<< 2) | ((HIGH_TWO
& *p
) >> 6);
1586 octa2
= (MID_TWO
& *p
) >> 3;
1587 octa3
= (LOW_TWO
& *p
);
1589 emit_octal_digit (stream
, &seen_a_one
, octa1
);
1590 emit_octal_digit (stream
, &seen_a_one
, octa2
);
1591 emit_octal_digit (stream
, &seen_a_one
, octa3
);
1595 error (_("Internal error in octal conversion;"));
1599 cycle
= cycle
% BITS_IN_OCTAL
;
1604 for (p
= valaddr
+ len
- 1;
1611 /* Carry out, no carry in */
1613 octa1
= (HIGH_ZERO
& *p
) >> 5;
1614 octa2
= (LOW_ZERO
& *p
) >> 2;
1615 carry
= (CARRY_ZERO
& *p
);
1616 emit_octal_digit (stream
, &seen_a_one
, octa1
);
1617 emit_octal_digit (stream
, &seen_a_one
, octa2
);
1621 /* Carry in, carry out */
1623 octa1
= (carry
<< 1) | ((HIGH_ONE
& *p
) >> 7);
1624 octa2
= (MID_ONE
& *p
) >> 4;
1625 octa3
= (LOW_ONE
& *p
) >> 1;
1626 carry
= (CARRY_ONE
& *p
);
1627 emit_octal_digit (stream
, &seen_a_one
, octa1
);
1628 emit_octal_digit (stream
, &seen_a_one
, octa2
);
1629 emit_octal_digit (stream
, &seen_a_one
, octa3
);
1633 /* Carry in, no carry out */
1635 octa1
= (carry
<< 2) | ((HIGH_TWO
& *p
) >> 6);
1636 octa2
= (MID_TWO
& *p
) >> 3;
1637 octa3
= (LOW_TWO
& *p
);
1639 emit_octal_digit (stream
, &seen_a_one
, octa1
);
1640 emit_octal_digit (stream
, &seen_a_one
, octa2
);
1641 emit_octal_digit (stream
, &seen_a_one
, octa3
);
1645 error (_("Internal error in octal conversion;"));
1649 cycle
= cycle
% BITS_IN_OCTAL
;
1655 /* Possibly negate the integer represented by BYTES. It contains LEN
1656 bytes in the specified byte order. If the integer is negative,
1657 copy it into OUT_VEC, negate it, and return true. Otherwise, do
1658 nothing and return false. */
1661 maybe_negate_by_bytes (const gdb_byte
*bytes
, unsigned len
,
1662 enum bfd_endian byte_order
,
1663 gdb::byte_vector
*out_vec
)
1666 gdb_assert (len
> 0);
1667 if (byte_order
== BFD_ENDIAN_BIG
)
1668 sign_byte
= bytes
[0];
1670 sign_byte
= bytes
[len
- 1];
1671 if ((sign_byte
& 0x80) == 0)
1674 out_vec
->resize (len
);
1676 /* Compute -x == 1 + ~x. */
1677 if (byte_order
== BFD_ENDIAN_LITTLE
)
1680 for (unsigned i
= 0; i
< len
; ++i
)
1682 unsigned tem
= (0xff & ~bytes
[i
]) + carry
;
1683 (*out_vec
)[i
] = tem
& 0xff;
1690 for (unsigned i
= len
; i
> 0; --i
)
1692 unsigned tem
= (0xff & ~bytes
[i
- 1]) + carry
;
1693 (*out_vec
)[i
- 1] = tem
& 0xff;
1701 /* VALADDR points to an integer of LEN bytes.
1702 Print it in decimal on stream or format it in buf. */
1705 print_decimal_chars (struct ui_file
*stream
, const gdb_byte
*valaddr
,
1706 unsigned len
, bool is_signed
,
1707 enum bfd_endian byte_order
)
1710 #define CARRY_OUT( x ) ((x) / TEN) /* extend char to int */
1711 #define CARRY_LEFT( x ) ((x) % TEN)
1712 #define SHIFT( x ) ((x) << 4)
1713 #define LOW_NIBBLE( x ) ( (x) & 0x00F)
1714 #define HIGH_NIBBLE( x ) (((x) & 0x0F0) >> 4)
1719 int i
, j
, decimal_digits
;
1723 gdb::byte_vector negated_bytes
;
1725 && maybe_negate_by_bytes (valaddr
, len
, byte_order
, &negated_bytes
))
1727 gdb_puts ("-", stream
);
1728 valaddr
= negated_bytes
.data ();
1731 /* Base-ten number is less than twice as many digits
1732 as the base 16 number, which is 2 digits per byte. */
1734 decimal_len
= len
* 2 * 2;
1735 std::vector
<unsigned char> digits (decimal_len
, 0);
1737 /* Ok, we have an unknown number of bytes of data to be printed in
1740 * Given a hex number (in nibbles) as XYZ, we start by taking X and
1741 * decimalizing it as "x1 x2" in two decimal nibbles. Then we multiply
1742 * the nibbles by 16, add Y and re-decimalize. Repeat with Z.
1744 * The trick is that "digits" holds a base-10 number, but sometimes
1745 * the individual digits are > 10.
1747 * Outer loop is per nibble (hex digit) of input, from MSD end to
1750 decimal_digits
= 0; /* Number of decimal digits so far */
1751 p
= (byte_order
== BFD_ENDIAN_BIG
) ? valaddr
: valaddr
+ len
- 1;
1753 while ((byte_order
== BFD_ENDIAN_BIG
) ? (p
< valaddr
+ len
) : (p
>= valaddr
))
1756 * Multiply current base-ten number by 16 in place.
1757 * Each digit was between 0 and 9, now is between
1760 for (j
= 0; j
< decimal_digits
; j
++)
1762 digits
[j
] = SHIFT (digits
[j
]);
1765 /* Take the next nibble off the input and add it to what
1766 * we've got in the LSB position. Bottom 'digit' is now
1767 * between 0 and 159.
1769 * "flip" is used to run this loop twice for each byte.
1773 /* Take top nibble. */
1775 digits
[0] += HIGH_NIBBLE (*p
);
1780 /* Take low nibble and bump our pointer "p". */
1782 digits
[0] += LOW_NIBBLE (*p
);
1783 if (byte_order
== BFD_ENDIAN_BIG
)
1790 /* Re-decimalize. We have to do this often enough
1791 * that we don't overflow, but once per nibble is
1792 * overkill. Easier this way, though. Note that the
1793 * carry is often larger than 10 (e.g. max initial
1794 * carry out of lowest nibble is 15, could bubble all
1795 * the way up greater than 10). So we have to do
1796 * the carrying beyond the last current digit.
1799 for (j
= 0; j
< decimal_len
- 1; j
++)
1803 /* "/" won't handle an unsigned char with
1804 * a value that if signed would be negative.
1805 * So extend to longword int via "dummy".
1808 carry
= CARRY_OUT (dummy
);
1809 digits
[j
] = CARRY_LEFT (dummy
);
1811 if (j
>= decimal_digits
&& carry
== 0)
1814 * All higher digits are 0 and we
1815 * no longer have a carry.
1817 * Note: "j" is 0-based, "decimal_digits" is
1820 decimal_digits
= j
+ 1;
1826 /* Ok, now "digits" is the decimal representation, with
1827 the "decimal_digits" actual digits. Print! */
1829 for (i
= decimal_digits
- 1; i
> 0 && digits
[i
] == 0; --i
)
1834 gdb_printf (stream
, "%1d", digits
[i
]);
1838 /* VALADDR points to an integer of LEN bytes. Print it in hex on stream. */
1841 print_hex_chars (struct ui_file
*stream
, const gdb_byte
*valaddr
,
1842 unsigned len
, enum bfd_endian byte_order
,
1847 gdb_puts ("0x", stream
);
1848 if (byte_order
== BFD_ENDIAN_BIG
)
1854 /* Strip leading 0 bytes, but be sure to leave at least a
1855 single byte at the end. */
1856 for (; p
< valaddr
+ len
- 1 && !*p
; ++p
)
1860 const gdb_byte
*first
= p
;
1865 /* When not zero-padding, use a different format for the
1866 very first byte printed. */
1867 if (!zero_pad
&& p
== first
)
1868 gdb_printf (stream
, "%x", *p
);
1870 gdb_printf (stream
, "%02x", *p
);
1875 p
= valaddr
+ len
- 1;
1879 /* Strip leading 0 bytes, but be sure to leave at least a
1880 single byte at the end. */
1881 for (; p
>= valaddr
+ 1 && !*p
; --p
)
1885 const gdb_byte
*first
= p
;
1890 /* When not zero-padding, use a different format for the
1891 very first byte printed. */
1892 if (!zero_pad
&& p
== first
)
1893 gdb_printf (stream
, "%x", *p
);
1895 gdb_printf (stream
, "%02x", *p
);
1900 /* Print function pointer with inferior address ADDRESS onto stdio
1904 print_function_pointer_address (const struct value_print_options
*options
,
1905 struct gdbarch
*gdbarch
,
1907 struct ui_file
*stream
)
1909 CORE_ADDR func_addr
= gdbarch_convert_from_func_ptr_addr
1910 (gdbarch
, address
, current_inferior ()->top_target ());
1912 /* If the function pointer is represented by a description, print
1913 the address of the description. */
1914 if (options
->addressprint
&& func_addr
!= address
)
1916 gdb_puts ("@", stream
);
1917 gdb_puts (paddress (gdbarch
, address
), stream
);
1918 gdb_puts (": ", stream
);
1920 print_address_demangle (options
, gdbarch
, func_addr
, stream
, demangle
);
1924 /* Print on STREAM using the given OPTIONS the index for the element
1925 at INDEX of an array whose index type is INDEX_TYPE. */
1928 maybe_print_array_index (struct type
*index_type
, LONGEST index
,
1929 struct ui_file
*stream
,
1930 const struct value_print_options
*options
)
1932 if (!options
->print_array_indexes
)
1935 current_language
->print_array_index (index_type
, index
, stream
, options
);
1938 /* See valprint.h. */
1941 value_print_array_elements (struct value
*val
, struct ui_file
*stream
,
1943 const struct value_print_options
*options
,
1946 unsigned int things_printed
= 0;
1948 struct type
*elttype
, *index_type
;
1949 /* Position of the array element we are examining to see
1950 whether it is repeated. */
1952 /* Number of repetitions we have detected so far. */
1954 LONGEST low_bound
, high_bound
;
1956 struct type
*type
= check_typedef (val
->type ());
1958 elttype
= type
->target_type ();
1959 unsigned bit_stride
= type
->bit_stride ();
1960 if (bit_stride
== 0)
1961 bit_stride
= 8 * check_typedef (elttype
)->length ();
1962 index_type
= type
->index_type ();
1963 if (index_type
->code () == TYPE_CODE_RANGE
)
1964 index_type
= index_type
->target_type ();
1966 if (get_array_bounds (type
, &low_bound
, &high_bound
))
1968 /* The array length should normally be HIGH_BOUND - LOW_BOUND +
1969 1. But we have to be a little extra careful, because some
1970 languages such as Ada allow LOW_BOUND to be greater than
1971 HIGH_BOUND for empty arrays. In that situation, the array
1972 length is just zero, not negative! */
1973 if (low_bound
> high_bound
)
1976 len
= high_bound
- low_bound
+ 1;
1980 warning (_("unable to get bounds of array, assuming null array"));
1985 annotate_array_section_begin (i
, elttype
);
1987 for (; i
< len
&& things_printed
< options
->print_max
; i
++)
1989 scoped_value_mark free_values
;
1993 if (options
->prettyformat_arrays
)
1995 gdb_printf (stream
, ",\n");
1996 print_spaces (2 + 2 * recurse
, stream
);
1999 gdb_printf (stream
, ", ");
2001 else if (options
->prettyformat_arrays
)
2003 gdb_printf (stream
, "\n");
2004 print_spaces (2 + 2 * recurse
, stream
);
2006 stream
->wrap_here (2 + 2 * recurse
);
2007 maybe_print_array_index (index_type
, i
+ low_bound
,
2010 struct value
*element
= val
->from_component_bitsize (elttype
,
2015 /* Only check for reps if repeat_count_threshold is not set to
2016 UINT_MAX (unlimited). */
2017 if (options
->repeat_count_threshold
< UINT_MAX
)
2019 bool unavailable
= element
->entirely_unavailable ();
2020 bool available
= element
->entirely_available ();
2024 struct value
*rep_elt
2025 = val
->from_component_bitsize (elttype
,
2028 bool repeated
= ((available
2029 && rep_elt
->entirely_available ()
2030 && element
->contents_eq (rep_elt
))
2032 && rep_elt
->entirely_unavailable ()));
2040 common_val_print (element
, stream
, recurse
+ 1, options
,
2043 if (reps
> options
->repeat_count_threshold
)
2045 annotate_elt_rep (reps
);
2046 gdb_printf (stream
, " %p[<repeats %u times>%p]",
2047 metadata_style
.style ().ptr (), reps
, nullptr);
2048 annotate_elt_rep_end ();
2051 things_printed
+= options
->repeat_count_threshold
;
2059 annotate_array_section_end ();
2061 gdb_printf (stream
, "...");
2062 if (options
->prettyformat_arrays
)
2064 gdb_printf (stream
, "\n");
2065 print_spaces (2 * recurse
, stream
);
2069 /* Return true if print_wchar can display W without resorting to a
2070 numeric escape, false otherwise. */
2073 wchar_printable (gdb_wchar_t w
)
2075 return (gdb_iswprint (w
)
2076 || w
== LCST ('\a') || w
== LCST ('\b')
2077 || w
== LCST ('\f') || w
== LCST ('\n')
2078 || w
== LCST ('\r') || w
== LCST ('\t')
2079 || w
== LCST ('\v'));
2082 /* A helper function that converts the contents of STRING to wide
2083 characters and then appends them to OUTPUT. */
2086 append_string_as_wide (const char *string
,
2087 struct obstack
*output
)
2089 for (; *string
; ++string
)
2091 gdb_wchar_t w
= gdb_btowc (*string
);
2092 obstack_grow (output
, &w
, sizeof (gdb_wchar_t
));
2096 /* Print a wide character W to OUTPUT. ORIG is a pointer to the
2097 original (target) bytes representing the character, ORIG_LEN is the
2098 number of valid bytes. WIDTH is the number of bytes in a base
2099 characters of the type. OUTPUT is an obstack to which wide
2100 characters are emitted. QUOTER is a (narrow) character indicating
2101 the style of quotes surrounding the character to be printed.
2102 NEED_ESCAPE is an in/out flag which is used to track numeric
2103 escapes across calls. */
2106 print_wchar (gdb_wint_t w
, const gdb_byte
*orig
,
2107 int orig_len
, int width
,
2108 enum bfd_endian byte_order
,
2109 struct obstack
*output
,
2110 int quoter
, bool *need_escapep
)
2112 bool need_escape
= *need_escapep
;
2114 *need_escapep
= false;
2116 /* If any additional cases are added to this switch block, then the
2117 function wchar_printable will likely need updating too. */
2121 obstack_grow_wstr (output
, LCST ("\\a"));
2124 obstack_grow_wstr (output
, LCST ("\\b"));
2127 obstack_grow_wstr (output
, LCST ("\\f"));
2130 obstack_grow_wstr (output
, LCST ("\\n"));
2133 obstack_grow_wstr (output
, LCST ("\\r"));
2136 obstack_grow_wstr (output
, LCST ("\\t"));
2139 obstack_grow_wstr (output
, LCST ("\\v"));
2143 if (gdb_iswprint (w
) && !(need_escape
&& gdb_iswxdigit (w
)))
2145 gdb_wchar_t wchar
= w
;
2147 if (w
== gdb_btowc (quoter
) || w
== LCST ('\\'))
2148 obstack_grow_wstr (output
, LCST ("\\"));
2149 obstack_grow (output
, &wchar
, sizeof (gdb_wchar_t
));
2155 for (i
= 0; i
+ width
<= orig_len
; i
+= width
)
2160 value
= extract_unsigned_integer (&orig
[i
], width
,
2162 /* If the value fits in 3 octal digits, print it that
2163 way. Otherwise, print it as a hex escape. */
2166 xsnprintf (octal
, sizeof (octal
), "\\%.3o",
2167 (int) (value
& 0777));
2168 *need_escapep
= false;
2172 xsnprintf (octal
, sizeof (octal
), "\\x%lx", (long) value
);
2173 /* A hex escape might require the next character
2174 to be escaped, because, unlike with octal,
2175 hex escapes have no length limit. */
2176 *need_escapep
= true;
2178 append_string_as_wide (octal
, output
);
2180 /* If we somehow have extra bytes, print them now. */
2181 while (i
< orig_len
)
2185 xsnprintf (octal
, sizeof (octal
), "\\%.3o", orig
[i
] & 0xff);
2186 *need_escapep
= false;
2187 append_string_as_wide (octal
, output
);
2196 /* Print the character C on STREAM as part of the contents of a
2197 literal string whose delimiter is QUOTER. ENCODING names the
2201 generic_emit_char (int c
, struct type
*type
, struct ui_file
*stream
,
2202 int quoter
, const char *encoding
)
2204 enum bfd_endian byte_order
2205 = type_byte_order (type
);
2207 bool need_escape
= false;
2209 c_buf
= (gdb_byte
*) alloca (type
->length ());
2210 pack_long (c_buf
, type
, c
);
2212 wchar_iterator
iter (c_buf
, type
->length (), encoding
, type
->length ());
2214 /* This holds the printable form of the wchar_t data. */
2215 auto_obstack wchar_buf
;
2221 const gdb_byte
*buf
;
2223 int print_escape
= 1;
2224 enum wchar_iterate_result result
;
2226 num_chars
= iter
.iterate (&result
, &chars
, &buf
, &buflen
);
2231 /* If all characters are printable, print them. Otherwise,
2232 we're going to have to print an escape sequence. We
2233 check all characters because we want to print the target
2234 bytes in the escape sequence, and we don't know character
2235 boundaries there. */
2239 for (i
= 0; i
< num_chars
; ++i
)
2240 if (!wchar_printable (chars
[i
]))
2248 for (i
= 0; i
< num_chars
; ++i
)
2249 print_wchar (chars
[i
], buf
, buflen
,
2250 type
->length (), byte_order
,
2251 &wchar_buf
, quoter
, &need_escape
);
2255 /* This handles the NUM_CHARS == 0 case as well. */
2257 print_wchar (gdb_WEOF
, buf
, buflen
, type
->length (),
2258 byte_order
, &wchar_buf
, quoter
, &need_escape
);
2261 /* The output in the host encoding. */
2262 auto_obstack output
;
2264 convert_between_encodings (INTERMEDIATE_ENCODING
, host_charset (),
2265 (gdb_byte
*) obstack_base (&wchar_buf
),
2266 obstack_object_size (&wchar_buf
),
2267 sizeof (gdb_wchar_t
), &output
, translit_char
);
2268 obstack_1grow (&output
, '\0');
2270 gdb_puts ((const char *) obstack_base (&output
), stream
);
2273 /* Return the repeat count of the next character/byte in ITER,
2274 storing the result in VEC. */
2277 count_next_character (wchar_iterator
*iter
,
2278 std::vector
<converted_character
> *vec
)
2280 struct converted_character
*current
;
2284 struct converted_character tmp
;
2288 = iter
->iterate (&tmp
.result
, &chars
, &tmp
.buf
, &tmp
.buflen
);
2289 if (tmp
.num_chars
> 0)
2291 gdb_assert (tmp
.num_chars
< MAX_WCHARS
);
2292 memcpy (tmp
.chars
, chars
, tmp
.num_chars
* sizeof (gdb_wchar_t
));
2294 vec
->push_back (tmp
);
2297 current
= &vec
->back ();
2299 /* Count repeated characters or bytes. */
2300 current
->repeat_count
= 1;
2301 if (current
->num_chars
== -1)
2309 struct converted_character d
;
2316 /* Get the next character. */
2317 d
.num_chars
= iter
->iterate (&d
.result
, &chars
, &d
.buf
, &d
.buflen
);
2319 /* If a character was successfully converted, save the character
2320 into the converted character. */
2321 if (d
.num_chars
> 0)
2323 gdb_assert (d
.num_chars
< MAX_WCHARS
);
2324 memcpy (d
.chars
, chars
, WCHAR_BUFLEN (d
.num_chars
));
2327 /* Determine if the current character is the same as this
2329 if (d
.num_chars
== current
->num_chars
&& d
.result
== current
->result
)
2331 /* There are two cases to consider:
2333 1) Equality of converted character (num_chars > 0)
2334 2) Equality of non-converted character (num_chars == 0) */
2335 if ((current
->num_chars
> 0
2336 && memcmp (current
->chars
, d
.chars
,
2337 WCHAR_BUFLEN (current
->num_chars
)) == 0)
2338 || (current
->num_chars
== 0
2339 && current
->buflen
== d
.buflen
2340 && memcmp (current
->buf
, d
.buf
, current
->buflen
) == 0))
2341 ++current
->repeat_count
;
2349 /* Push this next converted character onto the result vector. */
2350 repeat
= current
->repeat_count
;
2356 /* Print the characters in CHARS to the OBSTACK. QUOTE_CHAR is the quote
2357 character to use with string output. WIDTH is the size of the output
2358 character type. BYTE_ORDER is the target byte order. OPTIONS
2359 is the user's print options. *FINISHED is set to 0 if we didn't print
2360 all the elements in CHARS. */
2363 print_converted_chars_to_obstack (struct obstack
*obstack
,
2364 const std::vector
<converted_character
> &chars
,
2365 int quote_char
, int width
,
2366 enum bfd_endian byte_order
,
2367 const struct value_print_options
*options
,
2370 unsigned int idx
, num_elements
;
2371 const converted_character
*elem
;
2372 enum {START
, SINGLE
, REPEAT
, INCOMPLETE
, FINISH
} state
, last
;
2373 gdb_wchar_t wide_quote_char
= gdb_btowc (quote_char
);
2374 bool need_escape
= false;
2375 const int print_max
= options
->print_max_chars
> 0
2376 ? options
->print_max_chars
: options
->print_max
;
2378 /* Set the start state. */
2379 idx
= num_elements
= 0;
2380 last
= state
= START
;
2388 /* Nothing to do. */
2395 /* We are outputting a single character
2396 (< options->repeat_count_threshold). */
2400 /* We were outputting some other type of content, so we
2401 must output and a comma and a quote. */
2403 obstack_grow_wstr (obstack
, LCST (", "));
2404 obstack_grow (obstack
, &wide_quote_char
, sizeof (gdb_wchar_t
));
2406 /* Output the character. */
2407 int repeat_count
= elem
->repeat_count
;
2408 if (print_max
< repeat_count
+ num_elements
)
2410 repeat_count
= print_max
- num_elements
;
2413 for (j
= 0; j
< repeat_count
; ++j
)
2415 if (elem
->result
== wchar_iterate_ok
)
2416 print_wchar (elem
->chars
[0], elem
->buf
, elem
->buflen
, width
,
2417 byte_order
, obstack
, quote_char
, &need_escape
);
2419 print_wchar (gdb_WEOF
, elem
->buf
, elem
->buflen
, width
,
2420 byte_order
, obstack
, quote_char
, &need_escape
);
2430 /* We are outputting a character with a repeat count
2431 greater than options->repeat_count_threshold. */
2435 /* We were outputting a single string. Terminate the
2437 obstack_grow (obstack
, &wide_quote_char
, sizeof (gdb_wchar_t
));
2440 obstack_grow_wstr (obstack
, LCST (", "));
2442 /* Output the character and repeat string. */
2443 obstack_grow_wstr (obstack
, LCST ("'"));
2444 if (elem
->result
== wchar_iterate_ok
)
2445 print_wchar (elem
->chars
[0], elem
->buf
, elem
->buflen
, width
,
2446 byte_order
, obstack
, quote_char
, &need_escape
);
2448 print_wchar (gdb_WEOF
, elem
->buf
, elem
->buflen
, width
,
2449 byte_order
, obstack
, quote_char
, &need_escape
);
2450 obstack_grow_wstr (obstack
, LCST ("'"));
2451 std::string s
= string_printf (_(" <repeats %u times>"),
2452 elem
->repeat_count
);
2453 num_elements
+= elem
->repeat_count
;
2454 for (j
= 0; s
[j
]; ++j
)
2456 gdb_wchar_t w
= gdb_btowc (s
[j
]);
2457 obstack_grow (obstack
, &w
, sizeof (gdb_wchar_t
));
2463 /* We are outputting an incomplete sequence. */
2466 /* If we were outputting a string of SINGLE characters,
2467 terminate the quote. */
2468 obstack_grow (obstack
, &wide_quote_char
, sizeof (gdb_wchar_t
));
2471 obstack_grow_wstr (obstack
, LCST (", "));
2473 /* Output the incomplete sequence string. */
2474 obstack_grow_wstr (obstack
, LCST ("<incomplete sequence "));
2475 print_wchar (gdb_WEOF
, elem
->buf
, elem
->buflen
, width
, byte_order
,
2476 obstack
, 0, &need_escape
);
2477 obstack_grow_wstr (obstack
, LCST (">"));
2480 /* We do not attempt to output anything after this. */
2485 /* All done. If we were outputting a string of SINGLE
2486 characters, the string must be terminated. Otherwise,
2487 REPEAT and INCOMPLETE are always left properly terminated. */
2489 obstack_grow (obstack
, &wide_quote_char
, sizeof (gdb_wchar_t
));
2494 /* Get the next element and state. */
2496 if (state
!= FINISH
)
2498 elem
= &chars
[idx
++];
2499 switch (elem
->result
)
2501 case wchar_iterate_ok
:
2502 case wchar_iterate_invalid
:
2503 if (elem
->repeat_count
> options
->repeat_count_threshold
)
2509 case wchar_iterate_incomplete
:
2513 case wchar_iterate_eof
:
2521 /* Print the character string STRING, printing at most LENGTH
2522 characters. LENGTH is -1 if the string is nul terminated. TYPE is
2523 the type of each character. OPTIONS holds the printing options;
2524 printing stops early if the number hits print_max_chars; repeat
2525 counts are printed as appropriate. Print ellipses at the end if we
2526 had to stop before printing LENGTH characters, or if FORCE_ELLIPSES.
2527 QUOTE_CHAR is the character to print at each end of the string. If
2528 C_STYLE_TERMINATOR is true, and the last character is 0, then it is
2532 generic_printstr (struct ui_file
*stream
, struct type
*type
,
2533 const gdb_byte
*string
, unsigned int length
,
2534 const char *encoding
, int force_ellipses
,
2535 int quote_char
, int c_style_terminator
,
2536 const struct value_print_options
*options
)
2538 enum bfd_endian byte_order
= type_byte_order (type
);
2540 int width
= type
->length ();
2542 struct converted_character
*last
;
2546 unsigned long current_char
= 1;
2548 for (i
= 0; current_char
; ++i
)
2551 current_char
= extract_unsigned_integer (string
+ i
* width
,
2557 /* If the string was not truncated due to `set print elements', and
2558 the last byte of it is a null, we don't print that, in
2559 traditional C style. */
2560 if (c_style_terminator
2563 && (extract_unsigned_integer (string
+ (length
- 1) * width
,
2564 width
, byte_order
) == 0))
2569 gdb_printf (stream
, "%c%c", quote_char
, quote_char
);
2573 /* Arrange to iterate over the characters, in wchar_t form. */
2574 wchar_iterator
iter (string
, length
* width
, encoding
, width
);
2575 std::vector
<converted_character
> converted_chars
;
2577 /* Convert characters until the string is over or the maximum
2578 number of printed characters has been reached. */
2580 unsigned int print_max_chars
= get_print_max_chars (options
);
2581 while (i
< print_max_chars
)
2587 /* Grab the next character and repeat count. */
2588 r
= count_next_character (&iter
, &converted_chars
);
2590 /* If less than zero, the end of the input string was reached. */
2594 /* Otherwise, add the count to the total print count and get
2595 the next character. */
2599 /* Get the last element and determine if the entire string was
2601 last
= &converted_chars
.back ();
2602 finished
= (last
->result
== wchar_iterate_eof
);
2604 /* Ensure that CONVERTED_CHARS is terminated. */
2605 last
->result
= wchar_iterate_eof
;
2607 /* WCHAR_BUF is the obstack we use to represent the string in
2609 auto_obstack wchar_buf
;
2611 /* Print the output string to the obstack. */
2612 print_converted_chars_to_obstack (&wchar_buf
, converted_chars
, quote_char
,
2613 width
, byte_order
, options
, &finished
);
2615 if (force_ellipses
|| !finished
)
2616 obstack_grow_wstr (&wchar_buf
, LCST ("..."));
2618 /* OUTPUT is where we collect `char's for printing. */
2619 auto_obstack output
;
2621 convert_between_encodings (INTERMEDIATE_ENCODING
, host_charset (),
2622 (gdb_byte
*) obstack_base (&wchar_buf
),
2623 obstack_object_size (&wchar_buf
),
2624 sizeof (gdb_wchar_t
), &output
, translit_char
);
2625 obstack_1grow (&output
, '\0');
2627 gdb_puts ((const char *) obstack_base (&output
), stream
);
2630 /* Print a string from the inferior, starting at ADDR and printing up to LEN
2631 characters, of WIDTH bytes a piece, to STREAM. If LEN is -1, printing
2632 stops at the first null byte, otherwise printing proceeds (including null
2633 bytes) until either print_max_chars or LEN characters have been printed,
2634 whichever is smaller. ENCODING is the name of the string's
2635 encoding. It can be NULL, in which case the target encoding is
2639 val_print_string (struct type
*elttype
, const char *encoding
,
2640 CORE_ADDR addr
, int len
,
2641 struct ui_file
*stream
,
2642 const struct value_print_options
*options
)
2644 int force_ellipsis
= 0; /* Force ellipsis to be printed if nonzero. */
2645 int err
; /* Non-zero if we got a bad read. */
2646 int found_nul
; /* Non-zero if we found the nul char. */
2647 unsigned int fetchlimit
; /* Maximum number of chars to print. */
2649 gdb::unique_xmalloc_ptr
<gdb_byte
> buffer
; /* Dynamically growable fetch buffer. */
2650 struct gdbarch
*gdbarch
= elttype
->arch ();
2651 enum bfd_endian byte_order
= type_byte_order (elttype
);
2652 int width
= elttype
->length ();
2654 /* First we need to figure out the limit on the number of characters we are
2655 going to attempt to fetch and print. This is actually pretty simple.
2656 If LEN >= zero, then the limit is the minimum of LEN and print_max_chars.
2657 If LEN is -1, then the limit is print_max_chars. This is true regardless
2658 of whether print_max_chars is zero, UINT_MAX (unlimited), or something in
2659 between, because finding the null byte (or available memory) is what
2660 actually limits the fetch. */
2662 unsigned int print_max_chars
= get_print_max_chars (options
);
2663 fetchlimit
= (len
== -1
2665 : std::min ((unsigned) len
, print_max_chars
));
2667 err
= target_read_string (addr
, len
, width
, fetchlimit
,
2668 &buffer
, &bytes_read
);
2672 /* We now have either successfully filled the buffer to fetchlimit,
2673 or terminated early due to an error or finding a null char when
2676 /* Determine found_nul by looking at the last character read. */
2678 if (bytes_read
>= width
)
2679 found_nul
= extract_unsigned_integer (buffer
.get () + bytes_read
- width
,
2680 width
, byte_order
) == 0;
2681 if (len
== -1 && !found_nul
)
2685 /* We didn't find a NUL terminator we were looking for. Attempt
2686 to peek at the next character. If not successful, or it is not
2687 a null byte, then force ellipsis to be printed. */
2689 peekbuf
= (gdb_byte
*) alloca (width
);
2691 if (target_read_memory (addr
, peekbuf
, width
) == 0
2692 && extract_unsigned_integer (peekbuf
, width
, byte_order
) != 0)
2695 else if ((len
>= 0 && err
!= 0) || (len
> bytes_read
/ width
))
2697 /* Getting an error when we have a requested length, or fetching less
2698 than the number of characters actually requested, always make us
2703 /* If we get an error before fetching anything, don't print a string.
2704 But if we fetch something and then get an error, print the string
2705 and then the error message. */
2706 if (err
== 0 || bytes_read
> 0)
2707 current_language
->printstr (stream
, elttype
, buffer
.get (),
2709 encoding
, force_ellipsis
, options
);
2713 std::string str
= memory_error_message (TARGET_XFER_E_IO
, gdbarch
, addr
);
2715 gdb_printf (stream
, _("<error: %ps>"),
2716 styled_string (metadata_style
.style (),
2720 return (bytes_read
/ width
);
2723 /* Handle 'show print max-depth'. */
2726 show_print_max_depth (struct ui_file
*file
, int from_tty
,
2727 struct cmd_list_element
*c
, const char *value
)
2729 gdb_printf (file
, _("Maximum print depth is %s.\n"), value
);
2733 /* The 'set input-radix' command writes to this auxiliary variable.
2734 If the requested radix is valid, INPUT_RADIX is updated; otherwise,
2735 it is left unchanged. */
2737 static unsigned input_radix_1
= 10;
2739 /* Validate an input or output radix setting, and make sure the user
2740 knows what they really did here. Radix setting is confusing, e.g.
2741 setting the input radix to "10" never changes it! */
2744 set_input_radix (const char *args
, int from_tty
, struct cmd_list_element
*c
)
2746 set_input_radix_1 (from_tty
, input_radix_1
);
2750 set_input_radix_1 (int from_tty
, unsigned radix
)
2752 /* We don't currently disallow any input radix except 0 or 1, which don't
2753 make any mathematical sense. In theory, we can deal with any input
2754 radix greater than 1, even if we don't have unique digits for every
2755 value from 0 to radix-1, but in practice we lose on large radix values.
2756 We should either fix the lossage or restrict the radix range more.
2761 input_radix_1
= input_radix
;
2762 error (_("Nonsense input radix ``decimal %u''; input radix unchanged."),
2765 input_radix_1
= input_radix
= radix
;
2768 gdb_printf (_("Input radix now set to "
2769 "decimal %u, hex %x, octal %o.\n"),
2770 radix
, radix
, radix
);
2774 /* The 'set output-radix' command writes to this auxiliary variable.
2775 If the requested radix is valid, OUTPUT_RADIX is updated,
2776 otherwise, it is left unchanged. */
2778 static unsigned output_radix_1
= 10;
2781 set_output_radix (const char *args
, int from_tty
, struct cmd_list_element
*c
)
2783 set_output_radix_1 (from_tty
, output_radix_1
);
2787 set_output_radix_1 (int from_tty
, unsigned radix
)
2789 /* Validate the radix and disallow ones that we aren't prepared to
2790 handle correctly, leaving the radix unchanged. */
2794 user_print_options
.output_format
= 'x'; /* hex */
2797 user_print_options
.output_format
= 0; /* decimal */
2800 user_print_options
.output_format
= 'o'; /* octal */
2803 output_radix_1
= output_radix
;
2804 error (_("Unsupported output radix ``decimal %u''; "
2805 "output radix unchanged."),
2808 output_radix_1
= output_radix
= radix
;
2811 gdb_printf (_("Output radix now set to "
2812 "decimal %u, hex %x, octal %o.\n"),
2813 radix
, radix
, radix
);
2817 /* Set both the input and output radix at once. Try to set the output radix
2818 first, since it has the most restrictive range. An radix that is valid as
2819 an output radix is also valid as an input radix.
2821 It may be useful to have an unusual input radix. If the user wishes to
2822 set an input radix that is not valid as an output radix, he needs to use
2823 the 'set input-radix' command. */
2826 set_radix (const char *arg
, int from_tty
)
2830 radix
= (arg
== NULL
) ? 10 : parse_and_eval_long (arg
);
2831 set_output_radix_1 (0, radix
);
2832 set_input_radix_1 (0, radix
);
2835 gdb_printf (_("Input and output radices now set to "
2836 "decimal %u, hex %x, octal %o.\n"),
2837 radix
, radix
, radix
);
2841 /* Show both the input and output radices. */
2844 show_radix (const char *arg
, int from_tty
)
2848 if (input_radix
== output_radix
)
2850 gdb_printf (_("Input and output radices set to "
2851 "decimal %u, hex %x, octal %o.\n"),
2852 input_radix
, input_radix
, input_radix
);
2856 gdb_printf (_("Input radix set to decimal "
2857 "%u, hex %x, octal %o.\n"),
2858 input_radix
, input_radix
, input_radix
);
2859 gdb_printf (_("Output radix set to decimal "
2860 "%u, hex %x, octal %o.\n"),
2861 output_radix
, output_radix
, output_radix
);
2867 /* Controls printing of vtbl's. */
2869 show_vtblprint (struct ui_file
*file
, int from_tty
,
2870 struct cmd_list_element
*c
, const char *value
)
2872 gdb_printf (file
, _("\
2873 Printing of C++ virtual function tables is %s.\n"),
2877 /* Controls looking up an object's derived type using what we find in
2880 show_objectprint (struct ui_file
*file
, int from_tty
,
2881 struct cmd_list_element
*c
,
2884 gdb_printf (file
, _("\
2885 Printing of object's derived type based on vtable info is %s.\n"),
2890 show_static_field_print (struct ui_file
*file
, int from_tty
,
2891 struct cmd_list_element
*c
,
2895 _("Printing of C++ static members is %s.\n"),
2901 /* A couple typedefs to make writing the options a bit more
2903 using boolean_option_def
2904 = gdb::option::boolean_option_def
<value_print_options
>;
2905 using uinteger_option_def
2906 = gdb::option::uinteger_option_def
<value_print_options
>;
2907 using pinteger_option_def
2908 = gdb::option::pinteger_option_def
<value_print_options
>;
2910 /* Extra literals supported with the `set print characters' and
2911 `print -characters' commands. */
2912 static const literal_def print_characters_literals
[] =
2914 { "elements", PRINT_MAX_CHARS_ELEMENTS
},
2915 { "unlimited", PRINT_MAX_CHARS_UNLIMITED
, 0 },
2919 /* Definitions of options for the "print" and "compile print"
2921 static const gdb::option::option_def value_print_option_defs
[] = {
2923 boolean_option_def
{
2925 [] (value_print_options
*opt
) { return &opt
->addressprint
; },
2926 show_addressprint
, /* show_cmd_cb */
2927 N_("Set printing of addresses."),
2928 N_("Show printing of addresses."),
2929 NULL
, /* help_doc */
2932 boolean_option_def
{
2934 [] (value_print_options
*opt
) { return &opt
->prettyformat_arrays
; },
2935 show_prettyformat_arrays
, /* show_cmd_cb */
2936 N_("Set pretty formatting of arrays."),
2937 N_("Show pretty formatting of arrays."),
2938 NULL
, /* help_doc */
2941 boolean_option_def
{
2943 [] (value_print_options
*opt
) { return &opt
->print_array_indexes
; },
2944 show_print_array_indexes
, /* show_cmd_cb */
2945 N_("Set printing of array indexes."),
2946 N_("Show printing of array indexes."),
2947 NULL
, /* help_doc */
2950 boolean_option_def
{
2952 [] (value_print_options
*opt
) { return &opt
->nibblesprint
; },
2953 show_nibbles
, /* show_cmd_cb */
2954 N_("Set whether to print binary values in groups of four bits."),
2955 N_("Show whether to print binary values in groups of four bits."),
2956 NULL
, /* help_doc */
2959 uinteger_option_def
{
2961 [] (value_print_options
*opt
) { return &opt
->print_max_chars
; },
2962 print_characters_literals
,
2963 show_print_max_chars
, /* show_cmd_cb */
2964 N_("Set limit on string chars to print."),
2965 N_("Show limit on string chars to print."),
2966 N_("\"elements\" causes the array element limit to be used.\n"
2967 "\"unlimited\" causes there to be no limit."),
2970 uinteger_option_def
{
2972 [] (value_print_options
*opt
) { return &opt
->print_max
; },
2973 uinteger_unlimited_literals
,
2974 show_print_max
, /* show_cmd_cb */
2975 N_("Set limit on array elements to print."),
2976 N_("Show limit on array elements to print."),
2977 N_("\"unlimited\" causes there to be no limit.\n"
2978 "This setting also applies to string chars when \"print characters\"\n"
2979 "is set to \"elements\"."),
2982 pinteger_option_def
{
2984 [] (value_print_options
*opt
) { return &opt
->max_depth
; },
2985 pinteger_unlimited_literals
,
2986 show_print_max_depth
, /* show_cmd_cb */
2987 N_("Set maximum print depth for nested structures, unions and arrays."),
2988 N_("Show maximum print depth for nested structures, unions, and arrays."),
2989 N_("When structures, unions, or arrays are nested beyond this depth then they\n\
2990 will be replaced with either '{...}' or '(...)' depending on the language.\n\
2991 Use \"unlimited\" to print the complete structure.")
2994 boolean_option_def
{
2995 "memory-tag-violations",
2996 [] (value_print_options
*opt
) { return &opt
->memory_tag_violations
; },
2997 show_memory_tag_violations
, /* show_cmd_cb */
2998 N_("Set printing of memory tag violations for pointers."),
2999 N_("Show printing of memory tag violations for pointers."),
3000 N_("Issue a warning when the printed value is a pointer\n\
3001 whose logical tag doesn't match the allocation tag of the memory\n\
3002 location it points to."),
3005 boolean_option_def
{
3007 [] (value_print_options
*opt
) { return &opt
->stop_print_at_null
; },
3008 show_stop_print_at_null
, /* show_cmd_cb */
3009 N_("Set printing of char arrays to stop at first null char."),
3010 N_("Show printing of char arrays to stop at first null char."),
3011 NULL
, /* help_doc */
3014 boolean_option_def
{
3016 [] (value_print_options
*opt
) { return &opt
->objectprint
; },
3017 show_objectprint
, /* show_cmd_cb */
3018 _("Set printing of C++ virtual function tables."),
3019 _("Show printing of C++ virtual function tables."),
3020 NULL
, /* help_doc */
3023 boolean_option_def
{
3025 [] (value_print_options
*opt
) { return &opt
->prettyformat_structs
; },
3026 show_prettyformat_structs
, /* show_cmd_cb */
3027 N_("Set pretty formatting of structures."),
3028 N_("Show pretty formatting of structures."),
3029 NULL
, /* help_doc */
3032 boolean_option_def
{
3034 [] (value_print_options
*opt
) { return &opt
->raw
; },
3035 NULL
, /* show_cmd_cb */
3036 N_("Set whether to print values in raw form."),
3037 N_("Show whether to print values in raw form."),
3038 N_("If set, values are printed in raw form, bypassing any\n\
3039 pretty-printers for that value.")
3042 uinteger_option_def
{
3044 [] (value_print_options
*opt
) { return &opt
->repeat_count_threshold
; },
3045 uinteger_unlimited_literals
,
3046 show_repeat_count_threshold
, /* show_cmd_cb */
3047 N_("Set threshold for repeated print elements."),
3048 N_("Show threshold for repeated print elements."),
3049 N_("\"unlimited\" causes all elements to be individually printed."),
3052 boolean_option_def
{
3054 [] (value_print_options
*opt
) { return &opt
->static_field_print
; },
3055 show_static_field_print
, /* show_cmd_cb */
3056 N_("Set printing of C++ static members."),
3057 N_("Show printing of C++ static members."),
3058 NULL
, /* help_doc */
3061 boolean_option_def
{
3063 [] (value_print_options
*opt
) { return &opt
->symbol_print
; },
3064 show_symbol_print
, /* show_cmd_cb */
3065 N_("Set printing of symbol names when printing pointers."),
3066 N_("Show printing of symbol names when printing pointers."),
3067 NULL
, /* help_doc */
3070 boolean_option_def
{
3072 [] (value_print_options
*opt
) { return &opt
->unionprint
; },
3073 show_unionprint
, /* show_cmd_cb */
3074 N_("Set printing of unions interior to structures."),
3075 N_("Show printing of unions interior to structures."),
3076 NULL
, /* help_doc */
3079 boolean_option_def
{
3081 [] (value_print_options
*opt
) { return &opt
->vtblprint
; },
3082 show_vtblprint
, /* show_cmd_cb */
3083 N_("Set printing of C++ virtual function tables."),
3084 N_("Show printing of C++ virtual function tables."),
3085 NULL
, /* help_doc */
3089 /* See valprint.h. */
3091 gdb::option::option_def_group
3092 make_value_print_options_def_group (value_print_options
*opts
)
3094 return {{value_print_option_defs
}, opts
};
3099 /* Test printing of TYPE_CODE_FLAGS values. */
3102 test_print_flags (gdbarch
*arch
)
3104 type
*flags_type
= arch_flags_type (arch
, "test_type", 32);
3105 type
*field_type
= builtin_type (arch
)->builtin_uint32
;
3108 Fields: CCCB BAAA */
3109 append_flags_type_field (flags_type
, 0, 3, field_type
, "A");
3110 append_flags_type_field (flags_type
, 3, 2, field_type
, "B");
3111 append_flags_type_field (flags_type
, 5, 3, field_type
, "C");
3113 value
*val
= value::allocate (flags_type
);
3114 gdb_byte
*contents
= val
->contents_writeable ().data ();
3115 store_unsigned_integer (contents
, 4, gdbarch_byte_order (arch
), 0xaa);
3118 val_print_type_code_flags (flags_type
, val
, 0, &out
);
3119 SELF_CHECK (out
.string () == "[ A=2 B=1 C=5 ]");
3124 void _initialize_valprint ();
3126 _initialize_valprint ()
3129 selftests::register_test_foreach_arch ("print-flags", test_print_flags
);
3132 set_show_commands setshow_print_cmds
3133 = add_setshow_prefix_cmd ("print", no_class
,
3134 _("Generic command for setting how things print."),
3135 _("Generic command for showing print settings."),
3136 &setprintlist
, &showprintlist
,
3137 &setlist
, &showlist
);
3138 add_alias_cmd ("p", setshow_print_cmds
.set
, no_class
, 1, &setlist
);
3139 /* Prefer set print to set prompt. */
3140 add_alias_cmd ("pr", setshow_print_cmds
.set
, no_class
, 1, &setlist
);
3141 add_alias_cmd ("p", setshow_print_cmds
.show
, no_class
, 1, &showlist
);
3142 add_alias_cmd ("pr", setshow_print_cmds
.show
, no_class
, 1, &showlist
);
3144 set_show_commands setshow_print_raw_cmds
3145 = add_setshow_prefix_cmd
3147 _("Generic command for setting what things to print in \"raw\" mode."),
3148 _("Generic command for showing \"print raw\" settings."),
3149 &setprintrawlist
, &showprintrawlist
, &setprintlist
, &showprintlist
);
3150 deprecate_cmd (setshow_print_raw_cmds
.set
, nullptr);
3151 deprecate_cmd (setshow_print_raw_cmds
.show
, nullptr);
3153 gdb::option::add_setshow_cmds_for_options
3154 (class_support
, &user_print_options
, value_print_option_defs
,
3155 &setprintlist
, &showprintlist
);
3157 add_setshow_zuinteger_cmd ("input-radix", class_support
, &input_radix_1
,
3159 Set default input radix for entering numbers."), _("\
3160 Show default input radix for entering numbers."), NULL
,
3163 &setlist
, &showlist
);
3165 add_setshow_zuinteger_cmd ("output-radix", class_support
, &output_radix_1
,
3167 Set default output radix for printing of values."), _("\
3168 Show default output radix for printing of values."), NULL
,
3171 &setlist
, &showlist
);
3173 /* The "set radix" and "show radix" commands are special in that
3174 they are like normal set and show commands but allow two normally
3175 independent variables to be either set or shown with a single
3176 command. So the usual deprecated_add_set_cmd() and [deleted]
3177 add_show_from_set() commands aren't really appropriate. */
3178 /* FIXME: i18n: With the new add_setshow_integer command, that is no
3179 longer true - show can display anything. */
3180 add_cmd ("radix", class_support
, set_radix
, _("\
3181 Set default input and output number radices.\n\
3182 Use 'set input-radix' or 'set output-radix' to independently set each.\n\
3183 Without an argument, sets both radices back to the default value of 10."),
3185 add_cmd ("radix", class_support
, show_radix
, _("\
3186 Show the default input and output number radices.\n\
3187 Use 'show input-radix' or 'show output-radix' to independently show each."),