1 /* Print values for GDB, the GNU debugger.
3 Copyright (C) 1986-2025 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "event-top.h"
21 #include "extract-store-integer.h"
26 #include "cli/cli-cmds.h"
31 #include "target-float.h"
32 #include "extension.h"
34 #include "gdbsupport/gdb_obstack.h"
36 #include "typeprint.h"
39 #include "gdbsupport/byte-vector.h"
40 #include "cli/cli-option.h"
42 #include "cli/cli-style.h"
43 #include "count-one-bits.h"
47 #include "gdbsupport/selftest.h"
48 #include "selftest-arch.h"
50 /* Maximum number of wchars returned from wchar_iterate. */
53 /* A convenience macro to compute the size of a wchar_t buffer containing X
55 #define WCHAR_BUFLEN(X) ((X) * sizeof (gdb_wchar_t))
57 /* Character buffer size saved while iterating over wchars. */
58 #define WCHAR_BUFLEN_MAX WCHAR_BUFLEN (MAX_WCHARS)
60 /* A structure to encapsulate state information from iterated
61 character conversions. */
62 struct converted_character
64 /* The number of characters converted. */
67 /* The result of the conversion. See charset.h for more. */
68 enum wchar_iterate_result result
;
70 /* The (saved) converted character(s). */
71 gdb_wchar_t chars
[WCHAR_BUFLEN_MAX
];
73 /* The first converted target byte. */
76 /* The number of bytes converted. */
79 /* How many times this character(s) is repeated. */
83 /* Command lists for set/show print raw. */
84 struct cmd_list_element
*setprintrawlist
;
85 struct cmd_list_element
*showprintrawlist
;
87 /* Prototypes for local functions */
89 static void set_input_radix_1 (int, unsigned);
91 static void set_output_radix_1 (int, unsigned);
93 static void val_print_type_code_flags (struct type
*type
,
94 struct value
*original_value
,
96 struct ui_file
*stream
);
98 /* Start print_max at this value. */
99 #define PRINT_MAX_DEFAULT 200
101 /* Start print_max_chars at this value (meaning follow print_max). */
102 #define PRINT_MAX_CHARS_DEFAULT PRINT_MAX_CHARS_ELEMENTS
104 /* Start print_max_depth at this value. */
105 #define PRINT_MAX_DEPTH_DEFAULT 20
107 struct value_print_options user_print_options
=
109 Val_prettyformat_default
, /* prettyformat */
110 false, /* prettyformat_arrays */
111 false, /* prettyformat_structs */
112 false, /* vtblprint */
113 true, /* unionprint */
114 true, /* addressprint */
115 false, /* nibblesprint */
116 false, /* objectprint */
117 PRINT_MAX_DEFAULT
, /* print_max */
118 PRINT_MAX_CHARS_DEFAULT
, /* print_max_chars */
119 10, /* repeat_count_threshold */
120 0, /* output_format */
122 true, /* memory_tag_violations */
123 false, /* stop_print_at_null */
124 false, /* print_array_indexes */
125 false, /* deref_ref */
126 true, /* static_field_print */
127 true, /* pascal_static_field_print */
130 true, /* symbol_print */
131 PRINT_MAX_DEPTH_DEFAULT
, /* max_depth */
134 /* Initialize *OPTS to be a copy of the user print options. */
136 get_user_print_options (struct value_print_options
*opts
)
138 *opts
= user_print_options
;
141 /* Initialize *OPTS to be a copy of the user print options, but with
142 pretty-formatting disabled. */
144 get_no_prettyformat_print_options (struct value_print_options
*opts
)
146 *opts
= user_print_options
;
147 opts
->prettyformat
= Val_no_prettyformat
;
150 /* Initialize *OPTS to be a copy of the user print options, but using
151 FORMAT as the formatting option. */
153 get_formatted_print_options (struct value_print_options
*opts
,
156 *opts
= user_print_options
;
157 opts
->format
= format
;
160 /* Implement 'show print elements'. */
163 show_print_max (struct ui_file
*file
, int from_tty
,
164 struct cmd_list_element
*c
, const char *value
)
168 (user_print_options
.print_max_chars
!= PRINT_MAX_CHARS_ELEMENTS
169 ? _("Limit on array elements to print is %s.\n")
170 : _("Limit on string chars or array elements to print is %s.\n")),
174 /* Implement 'show print characters'. */
177 show_print_max_chars (struct ui_file
*file
, int from_tty
,
178 struct cmd_list_element
*c
, const char *value
)
181 _("Limit on string characters to print is %s.\n"),
185 /* Default input and output radixes, and output format letter. */
187 unsigned input_radix
= 10;
189 show_input_radix (struct ui_file
*file
, int from_tty
,
190 struct cmd_list_element
*c
, const char *value
)
193 _("Default input radix for entering numbers is %s.\n"),
197 unsigned output_radix
= 10;
199 show_output_radix (struct ui_file
*file
, int from_tty
,
200 struct cmd_list_element
*c
, const char *value
)
203 _("Default output radix for printing of values is %s.\n"),
207 /* By default we print arrays without printing the index of each element in
208 the array. This behavior can be changed by setting PRINT_ARRAY_INDEXES. */
211 show_print_array_indexes (struct ui_file
*file
, int from_tty
,
212 struct cmd_list_element
*c
, const char *value
)
214 gdb_printf (file
, _("Printing of array indexes is %s.\n"), value
);
217 /* Print repeat counts if there are more than this many repetitions of an
218 element in an array. Referenced by the low level language dependent
222 show_repeat_count_threshold (struct ui_file
*file
, int from_tty
,
223 struct cmd_list_element
*c
, const char *value
)
225 gdb_printf (file
, _("Threshold for repeated print elements is %s.\n"),
229 /* If nonzero, prints memory tag violations for pointers. */
232 show_memory_tag_violations (struct ui_file
*file
, int from_tty
,
233 struct cmd_list_element
*c
, const char *value
)
236 _("Printing of memory tag violations is %s.\n"),
240 /* If nonzero, stops printing of char arrays at first null. */
243 show_stop_print_at_null (struct ui_file
*file
, int from_tty
,
244 struct cmd_list_element
*c
, const char *value
)
247 _("Printing of char arrays to stop "
248 "at first null char is %s.\n"),
252 /* Controls pretty printing of structures. */
255 show_prettyformat_structs (struct ui_file
*file
, int from_tty
,
256 struct cmd_list_element
*c
, const char *value
)
258 gdb_printf (file
, _("Pretty formatting of structures is %s.\n"), value
);
261 /* Controls pretty printing of arrays. */
264 show_prettyformat_arrays (struct ui_file
*file
, int from_tty
,
265 struct cmd_list_element
*c
, const char *value
)
267 gdb_printf (file
, _("Pretty formatting of arrays is %s.\n"), value
);
270 /* If nonzero, causes unions inside structures or other unions to be
274 show_unionprint (struct ui_file
*file
, int from_tty
,
275 struct cmd_list_element
*c
, const char *value
)
278 _("Printing of unions interior to structures is %s.\n"),
282 /* Controls the format of printing binary values. */
285 show_nibbles (struct ui_file
*file
, int from_tty
,
286 struct cmd_list_element
*c
, const char *value
)
289 _("Printing binary values in groups is %s.\n"),
293 /* If nonzero, causes machine addresses to be printed in certain contexts. */
296 show_addressprint (struct ui_file
*file
, int from_tty
,
297 struct cmd_list_element
*c
, const char *value
)
299 gdb_printf (file
, _("Printing of addresses is %s.\n"), value
);
303 show_symbol_print (struct ui_file
*file
, int from_tty
,
304 struct cmd_list_element
*c
, const char *value
)
307 _("Printing of symbols when printing pointers is %s.\n"),
313 /* A helper function for val_print. When printing in "summary" mode,
314 we want to print scalar arguments, but not aggregate arguments.
315 This function distinguishes between the two. */
318 val_print_scalar_type_p (struct type
*type
)
320 type
= check_typedef (type
);
321 while (TYPE_IS_REFERENCE (type
))
323 type
= type
->target_type ();
324 type
= check_typedef (type
);
326 switch (type
->code ())
328 case TYPE_CODE_ARRAY
:
329 case TYPE_CODE_STRUCT
:
330 case TYPE_CODE_UNION
:
332 case TYPE_CODE_STRING
:
339 /* A helper function for val_print. When printing with limited depth we
340 want to print string and scalar arguments, but not aggregate arguments.
341 This function distinguishes between the two. */
344 val_print_scalar_or_string_type_p (struct type
*type
,
345 const struct language_defn
*language
)
347 return (val_print_scalar_type_p (type
)
348 || language
->is_string_type_p (type
));
351 /* See valprint.h. */
354 valprint_check_validity (struct ui_file
*stream
,
356 LONGEST embedded_offset
,
357 const struct value
*val
)
359 type
= check_typedef (type
);
361 if (type_not_associated (type
))
363 val_print_not_associated (stream
);
367 if (type_not_allocated (type
))
369 val_print_not_allocated (stream
);
373 if (type
->code () != TYPE_CODE_UNION
374 && type
->code () != TYPE_CODE_STRUCT
375 && type
->code () != TYPE_CODE_ARRAY
)
377 if (val
->bits_any_optimized_out (TARGET_CHAR_BIT
* embedded_offset
,
378 TARGET_CHAR_BIT
* type
->length ()))
380 val_print_optimized_out (val
, stream
);
384 if (val
->bits_synthetic_pointer (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
= deref_val
->lval () == 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 (!val
->bytes_available (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
&& val
->lval () == 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 (val
->type ());
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
, val
->address (),
503 /* generic_val_print helper for TYPE_CODE_STRING. */
506 generic_val_print_string (struct value
*val
,
507 struct ui_file
*stream
, int recurse
,
508 const struct value_print_options
*options
,
509 const struct generic_val_print_decorations
512 struct type
*type
= check_typedef (val
->type ());
513 struct type
*unresolved_elttype
= type
->target_type ();
514 struct type
*elttype
= check_typedef (unresolved_elttype
);
516 if (type
->length () > 0 && unresolved_elttype
->length () > 0)
518 LONGEST low_bound
, high_bound
;
520 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
521 error (_("Could not determine the array high bound"));
523 const gdb_byte
*valaddr
= val
->contents_for_printing ().data ();
524 int force_ellipses
= 0;
525 enum bfd_endian byte_order
= type_byte_order (type
);
528 eltlen
= elttype
->length ();
529 len
= high_bound
- low_bound
+ 1;
531 /* If requested, look for the first null char and only
532 print elements up to it. */
533 if (options
->stop_print_at_null
)
535 unsigned int print_max_chars
= get_print_max_chars (options
);
536 unsigned int temp_len
;
540 && temp_len
< print_max_chars
541 && extract_unsigned_integer (valaddr
+ temp_len
* eltlen
,
542 eltlen
, byte_order
) != 0);
546 /* Force printstr to print ellipses if
547 we've printed the maximum characters and
548 the next character is not \000. */
549 if (temp_len
== print_max_chars
&& temp_len
< len
)
552 = extract_unsigned_integer (valaddr
+ temp_len
* eltlen
,
561 current_language
->printstr (stream
, unresolved_elttype
, valaddr
, len
,
562 nullptr, force_ellipses
, options
);
566 /* Array of unspecified length: treat like pointer to first elt. */
567 print_unpacked_pointer (type
, elttype
, val
->address (),
573 /* generic_value_print helper for TYPE_CODE_PTR. */
576 generic_value_print_ptr (struct value
*val
, struct ui_file
*stream
,
577 const struct value_print_options
*options
)
580 if (options
->format
&& options
->format
!= 's')
581 value_print_scalar_formatted (val
, options
, 0, stream
);
584 struct type
*type
= check_typedef (val
->type ());
585 struct type
*elttype
= check_typedef (type
->target_type ());
586 const gdb_byte
*valaddr
= val
->contents_for_printing ().data ();
587 CORE_ADDR addr
= unpack_pointer (type
, valaddr
);
589 print_unpacked_pointer (type
, elttype
, addr
, stream
, options
);
594 /* Print '@' followed by the address contained in ADDRESS_BUFFER. */
597 print_ref_address (struct type
*type
, const gdb_byte
*address_buffer
,
598 int embedded_offset
, struct ui_file
*stream
)
600 struct gdbarch
*gdbarch
= type
->arch ();
602 if (address_buffer
!= NULL
)
605 = extract_typed_address (address_buffer
+ embedded_offset
, type
);
607 gdb_printf (stream
, "@");
608 gdb_puts (paddress (gdbarch
, address
), stream
);
610 /* Else: we have a non-addressable value, such as a DW_AT_const_value. */
613 /* If VAL is addressable, return the value contents buffer of a value that
614 represents a pointer to VAL. Otherwise return NULL. */
616 static const gdb_byte
*
617 get_value_addr_contents (struct value
*deref_val
)
619 gdb_assert (deref_val
!= NULL
);
621 if (deref_val
->lval () == lval_memory
)
622 return value_addr (deref_val
)->contents_for_printing ().data ();
625 /* We have a non-addressable value, such as a DW_AT_const_value. */
630 /* generic_val_print helper for TYPE_CODE_{RVALUE_,}REF. */
633 generic_val_print_ref (struct type
*type
,
634 int embedded_offset
, struct ui_file
*stream
, int recurse
,
635 struct value
*original_value
,
636 const struct value_print_options
*options
)
638 struct type
*elttype
= check_typedef (type
->target_type ());
639 struct value
*deref_val
= NULL
;
640 const bool value_is_synthetic
641 = original_value
->bits_synthetic_pointer (TARGET_CHAR_BIT
* embedded_offset
,
642 TARGET_CHAR_BIT
* type
->length ());
643 const int must_coerce_ref
= ((options
->addressprint
&& value_is_synthetic
)
644 || options
->deref_ref
);
645 const int type_is_defined
= elttype
->code () != TYPE_CODE_UNDEF
;
646 const gdb_byte
*valaddr
= original_value
->contents_for_printing ().data ();
648 if (must_coerce_ref
&& type_is_defined
)
650 deref_val
= coerce_ref_if_computed (original_value
);
652 if (deref_val
!= NULL
)
654 /* More complicated computed references are not supported. */
655 gdb_assert (embedded_offset
== 0);
658 deref_val
= value_at (type
->target_type (),
659 unpack_pointer (type
, valaddr
+ embedded_offset
));
661 /* Else, original_value isn't a synthetic reference or we don't have to print
662 the reference's contents.
664 Notice that for references to TYPE_CODE_STRUCT, 'set print object on' will
665 cause original_value to be a not_lval instead of an lval_computed,
666 which will make value_bits_synthetic_pointer return false.
667 This happens because if options->objectprint is true, c_value_print will
668 overwrite original_value's contents with the result of coercing
669 the reference through value_addr, and then set its type back to
670 TYPE_CODE_REF. In that case we don't have to coerce the reference again;
671 we can simply treat it as non-synthetic and move on. */
673 if (options
->addressprint
)
675 const gdb_byte
*address
= (value_is_synthetic
&& type_is_defined
676 ? get_value_addr_contents (deref_val
)
679 print_ref_address (type
, address
, embedded_offset
, stream
);
681 if (options
->deref_ref
)
682 gdb_puts (": ", stream
);
685 if (options
->deref_ref
)
688 common_val_print (deref_val
, stream
, recurse
, options
,
691 gdb_puts ("???", stream
);
695 /* Helper function for generic_val_print_enum.
696 This is also used to print enums in TYPE_CODE_FLAGS values. */
699 generic_val_print_enum_1 (struct type
*type
, LONGEST val
,
700 struct ui_file
*stream
)
705 len
= type
->num_fields ();
706 for (i
= 0; i
< len
; i
++)
709 if (val
== type
->field (i
).loc_enumval ())
716 fputs_styled (type
->field (i
).name (), variable_name_style
.style (),
719 else if (type
->is_flag_enum ())
723 /* We have a "flag" enum, so we try to decompose it into pieces as
724 appropriate. The enum may have multiple enumerators representing
725 the same bit, in which case we choose to only print the first one
727 for (i
= 0; i
< len
; ++i
)
731 ULONGEST enumval
= type
->field (i
).loc_enumval ();
732 int nbits
= count_one_bits_ll (enumval
);
734 gdb_assert (nbits
== 0 || nbits
== 1);
736 if ((val
& enumval
) != 0)
740 gdb_puts ("(", stream
);
744 gdb_puts (" | ", stream
);
746 val
&= ~type
->field (i
).loc_enumval ();
747 fputs_styled (type
->field (i
).name (),
748 variable_name_style
.style (), stream
);
754 /* There are leftover bits, print them. */
756 gdb_puts ("(", stream
);
758 gdb_puts (" | ", stream
);
760 gdb_puts ("unknown: 0x", stream
);
761 print_longest (stream
, 'x', 0, val
);
762 gdb_puts (")", stream
);
766 /* Nothing has been printed and the value is 0, the enum value must
768 gdb_puts ("0", stream
);
772 /* Something has been printed, close the parenthesis. */
773 gdb_puts (")", stream
);
777 print_longest (stream
, 'd', 0, val
);
780 /* generic_val_print helper for TYPE_CODE_ENUM. */
783 generic_val_print_enum (struct type
*type
,
784 int embedded_offset
, struct ui_file
*stream
,
785 struct value
*original_value
,
786 const struct value_print_options
*options
)
789 struct gdbarch
*gdbarch
= type
->arch ();
790 int unit_size
= gdbarch_addressable_memory_unit_size (gdbarch
);
792 gdb_assert (!options
->format
);
794 const gdb_byte
*valaddr
= original_value
->contents_for_printing ().data ();
796 val
= unpack_long (type
, valaddr
+ embedded_offset
* unit_size
);
798 generic_val_print_enum_1 (type
, val
, stream
);
801 /* generic_val_print helper for TYPE_CODE_FUNC and TYPE_CODE_METHOD. */
804 generic_val_print_func (struct type
*type
,
805 int embedded_offset
, CORE_ADDR address
,
806 struct ui_file
*stream
,
807 struct value
*original_value
,
808 const struct value_print_options
*options
)
810 struct gdbarch
*gdbarch
= type
->arch ();
812 gdb_assert (!options
->format
);
814 /* FIXME, we should consider, at least for ANSI C language,
815 eliminating the distinction made between FUNCs and POINTERs to
817 gdb_printf (stream
, "{");
818 type_print (type
, "", stream
, -1);
819 gdb_printf (stream
, "} ");
820 /* Try to print what function it points to, and its address. */
821 print_address_demangle (options
, gdbarch
, address
, stream
, demangle
);
824 /* generic_value_print helper for TYPE_CODE_BOOL. */
827 generic_value_print_bool
828 (struct value
*value
, struct ui_file
*stream
,
829 const struct value_print_options
*options
,
830 const struct generic_val_print_decorations
*decorations
)
832 if (options
->format
|| options
->output_format
)
834 struct value_print_options opts
= *options
;
835 opts
.format
= (options
->format
? options
->format
836 : options
->output_format
);
837 value_print_scalar_formatted (value
, &opts
, 0, stream
);
841 const gdb_byte
*valaddr
= value
->contents_for_printing ().data ();
842 struct type
*type
= check_typedef (value
->type ());
843 LONGEST val
= unpack_long (type
, valaddr
);
845 gdb_puts (decorations
->false_name
, stream
);
847 gdb_puts (decorations
->true_name
, stream
);
849 print_longest (stream
, 'd', 0, val
);
853 /* generic_value_print helper for TYPE_CODE_INT. */
856 generic_value_print_int (struct value
*val
, struct ui_file
*stream
,
857 const struct value_print_options
*options
)
859 struct value_print_options opts
= *options
;
861 opts
.format
= (options
->format
? options
->format
862 : options
->output_format
);
863 value_print_scalar_formatted (val
, &opts
, 0, stream
);
866 /* generic_value_print helper for TYPE_CODE_CHAR. */
869 generic_value_print_char (struct value
*value
, struct ui_file
*stream
,
870 const struct value_print_options
*options
)
872 if (options
->format
|| options
->output_format
)
874 struct value_print_options opts
= *options
;
876 opts
.format
= (options
->format
? options
->format
877 : options
->output_format
);
878 value_print_scalar_formatted (value
, &opts
, 0, stream
);
882 struct type
*unresolved_type
= value
->type ();
883 struct type
*type
= check_typedef (unresolved_type
);
884 const gdb_byte
*valaddr
= value
->contents_for_printing ().data ();
886 LONGEST val
= unpack_long (type
, valaddr
);
887 if (type
->is_unsigned ())
888 gdb_printf (stream
, "%u", (unsigned int) val
);
890 gdb_printf (stream
, "%d", (int) val
);
891 gdb_puts (" ", stream
);
892 current_language
->printchar (val
, unresolved_type
, stream
);
896 /* generic_val_print helper for TYPE_CODE_FLT and TYPE_CODE_DECFLOAT. */
899 generic_val_print_float (struct type
*type
, struct ui_file
*stream
,
900 struct value
*original_value
,
901 const struct value_print_options
*options
)
903 gdb_assert (!options
->format
);
905 const gdb_byte
*valaddr
= original_value
->contents_for_printing ().data ();
907 print_floating (valaddr
, type
, stream
);
910 /* generic_val_print helper for TYPE_CODE_FIXED_POINT. */
913 generic_val_print_fixed_point (struct value
*val
, struct ui_file
*stream
,
914 const struct value_print_options
*options
)
917 value_print_scalar_formatted (val
, options
, 0, stream
);
920 struct type
*type
= val
->type ();
922 const gdb_byte
*valaddr
= val
->contents_for_printing ().data ();
925 f
.read_fixed_point (gdb::make_array_view (valaddr
, type
->length ()),
926 type_byte_order (type
), type
->is_unsigned (),
927 type
->fixed_point_scaling_factor ());
929 const char *fmt
= type
->length () < 4 ? "%.11Fg" : "%.17Fg";
930 std::string str
= f
.str (fmt
);
931 gdb_printf (stream
, "%s", str
.c_str ());
935 /* generic_value_print helper for TYPE_CODE_COMPLEX. */
938 generic_value_print_complex (struct value
*val
, struct ui_file
*stream
,
939 const struct value_print_options
*options
,
940 const struct generic_val_print_decorations
943 gdb_printf (stream
, "%s", decorations
->complex_prefix
);
945 struct value
*real_part
= value_real_part (val
);
946 value_print_scalar_formatted (real_part
, options
, 0, stream
);
947 gdb_printf (stream
, "%s", decorations
->complex_infix
);
949 struct value
*imag_part
= value_imaginary_part (val
);
950 value_print_scalar_formatted (imag_part
, options
, 0, stream
);
951 gdb_printf (stream
, "%s", decorations
->complex_suffix
);
954 /* generic_value_print helper for TYPE_CODE_MEMBERPTR. */
957 generic_value_print_memberptr
958 (struct value
*val
, struct ui_file
*stream
,
960 const struct value_print_options
*options
,
961 const struct generic_val_print_decorations
*decorations
)
963 if (!options
->format
)
965 /* Member pointers are essentially specific to C++, and so if we
966 encounter one, we should print it according to C++ rules. */
967 struct type
*type
= check_typedef (val
->type ());
968 const gdb_byte
*valaddr
= val
->contents_for_printing ().data ();
969 cp_print_class_member (valaddr
, type
, stream
, "&");
972 value_print_scalar_formatted (val
, options
, 0, stream
);
975 /* See valprint.h. */
978 generic_value_print (struct value
*val
, struct ui_file
*stream
, int recurse
,
979 const struct value_print_options
*options
,
980 const struct generic_val_print_decorations
*decorations
)
982 struct type
*type
= val
->type ();
984 type
= check_typedef (type
);
986 if (is_fixed_point_type (type
))
987 type
= type
->fixed_point_type_base_type ();
989 /* Widen a subrange to its target type, then use that type's
991 while (type
->code () == TYPE_CODE_RANGE
)
993 type
= check_typedef (type
->target_type ());
994 val
= value_cast (type
, val
);
997 switch (type
->code ())
999 case TYPE_CODE_ARRAY
:
1000 generic_val_print_array (val
, stream
, recurse
, options
, decorations
);
1003 case TYPE_CODE_STRING
:
1004 generic_val_print_string (val
, stream
, recurse
, options
, decorations
);
1007 case TYPE_CODE_MEMBERPTR
:
1008 generic_value_print_memberptr (val
, stream
, recurse
, options
,
1013 generic_value_print_ptr (val
, stream
, options
);
1017 case TYPE_CODE_RVALUE_REF
:
1018 generic_val_print_ref (type
, 0, stream
, recurse
,
1022 case TYPE_CODE_ENUM
:
1023 if (options
->format
)
1024 value_print_scalar_formatted (val
, options
, 0, stream
);
1026 generic_val_print_enum (type
, 0, stream
, val
, options
);
1029 case TYPE_CODE_FLAGS
:
1030 if (options
->format
)
1031 value_print_scalar_formatted (val
, options
, 0, stream
);
1033 val_print_type_code_flags (type
, val
, 0, stream
);
1036 case TYPE_CODE_FUNC
:
1037 case TYPE_CODE_METHOD
:
1038 if (options
->format
)
1039 value_print_scalar_formatted (val
, options
, 0, stream
);
1041 generic_val_print_func (type
, 0, val
->address (), stream
,
1045 case TYPE_CODE_BOOL
:
1046 generic_value_print_bool (val
, stream
, options
, decorations
);
1050 generic_value_print_int (val
, stream
, options
);
1053 case TYPE_CODE_CHAR
:
1054 generic_value_print_char (val
, stream
, options
);
1058 case TYPE_CODE_DECFLOAT
:
1059 if (options
->format
)
1060 value_print_scalar_formatted (val
, options
, 0, stream
);
1062 generic_val_print_float (type
, stream
, val
, options
);
1065 case TYPE_CODE_FIXED_POINT
:
1066 generic_val_print_fixed_point (val
, stream
, options
);
1069 case TYPE_CODE_VOID
:
1070 gdb_puts (decorations
->void_name
, stream
);
1073 case TYPE_CODE_ERROR
:
1074 gdb_printf (stream
, "%s", TYPE_ERROR_NAME (type
));
1077 case TYPE_CODE_UNDEF
:
1078 /* This happens (without TYPE_STUB set) on systems which don't use
1079 dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
1080 and no complete type for struct foo in that file. */
1081 fprintf_styled (stream
, metadata_style
.style (), _("<incomplete type>"));
1084 case TYPE_CODE_COMPLEX
:
1085 generic_value_print_complex (val
, stream
, options
, decorations
);
1088 case TYPE_CODE_METHODPTR
:
1089 cplus_print_method_ptr (val
->contents_for_printing ().data (), type
,
1093 case TYPE_CODE_UNION
:
1094 case TYPE_CODE_STRUCT
:
1096 error (_("Unhandled type code %d in symbol table."),
1101 /* Print using the given LANGUAGE the value VAL onto stream STREAM according
1104 This is a preferable interface to val_print, above, because it uses
1105 GDB's value mechanism. */
1108 common_val_print (struct value
*value
, struct ui_file
*stream
, int recurse
,
1109 const struct value_print_options
*options
,
1110 const struct language_defn
*language
)
1112 if (language
->la_language
== language_ada
)
1113 /* The value might have a dynamic type, which would cause trouble
1114 below when trying to extract the value contents (since the value
1115 size is determined from the type size which is unknown). So
1116 get a fixed representation of our value. */
1117 value
= ada_to_fixed_value (value
);
1120 value
->fetch_lazy ();
1122 struct value_print_options local_opts
= *options
;
1123 struct type
*type
= value
->type ();
1124 struct type
*real_type
= check_typedef (type
);
1126 if (local_opts
.prettyformat
== Val_prettyformat_default
)
1127 local_opts
.prettyformat
= (local_opts
.prettyformat_structs
1128 ? Val_prettyformat
: Val_no_prettyformat
);
1132 if (!valprint_check_validity (stream
, real_type
, 0, value
))
1137 if (apply_ext_lang_val_pretty_printer (value
, stream
, recurse
, options
,
1142 /* Ensure that the type is complete and not just a stub. If the type is
1143 only a stub and we can't find and substitute its complete type, then
1144 print appropriate string and return. */
1146 if (real_type
->is_stub ())
1148 fprintf_styled (stream
, metadata_style
.style (), _("<incomplete type>"));
1152 /* Handle summary mode. If the value is a scalar, print it;
1153 otherwise, print an ellipsis. */
1154 if (options
->summary
&& !val_print_scalar_type_p (type
))
1156 gdb_printf (stream
, "...");
1160 /* If this value is too deep then don't print it. */
1161 if (!val_print_scalar_or_string_type_p (type
, language
)
1162 && val_print_check_max_depth (stream
, recurse
, options
, language
))
1167 language
->value_print_inner (value
, stream
, recurse
, &local_opts
);
1169 catch (const gdb_exception_error
&except
)
1171 fprintf_styled (stream
, metadata_style
.style (),
1172 _("<error reading variable: %s>"), except
.what ());
1176 /* See valprint.h. */
1179 val_print_check_max_depth (struct ui_file
*stream
, int recurse
,
1180 const struct value_print_options
*options
,
1181 const struct language_defn
*language
)
1183 if (options
->max_depth
> -1 && recurse
>= options
->max_depth
)
1185 gdb_assert (language
->struct_too_deep_ellipsis () != NULL
);
1186 gdb_puts (language
->struct_too_deep_ellipsis (), stream
);
1193 /* Check whether the value VAL is printable. Return 1 if it is;
1194 return 0 and print an appropriate error message to STREAM according to
1195 OPTIONS if it is not. */
1198 value_check_printable (struct value
*val
, struct ui_file
*stream
,
1199 const struct value_print_options
*options
)
1203 fprintf_styled (stream
, metadata_style
.style (),
1204 _("<address of value unknown>"));
1208 if (val
->entirely_optimized_out ())
1210 if (options
->summary
&& !val_print_scalar_type_p (val
->type ()))
1211 gdb_printf (stream
, "...");
1213 val_print_optimized_out (val
, stream
);
1217 if (val
->entirely_unavailable ())
1219 if (options
->summary
&& !val_print_scalar_type_p (val
->type ()))
1220 gdb_printf (stream
, "...");
1222 val_print_unavailable (stream
);
1226 if (val
->type ()->code () == TYPE_CODE_INTERNAL_FUNCTION
)
1228 fprintf_styled (stream
, metadata_style
.style (),
1229 _("<internal function %s>"),
1230 value_internal_function_name (val
));
1234 if (type_not_allocated (val
->type ()))
1236 val_print_not_allocated (stream
);
1243 /* See valprint.h. */
1246 common_val_print_checked (struct value
*val
, struct ui_file
*stream
,
1248 const struct value_print_options
*options
,
1249 const struct language_defn
*language
)
1251 if (!value_check_printable (val
, stream
, options
))
1253 common_val_print (val
, stream
, recurse
, options
, language
);
1256 /* Print on stream STREAM the value VAL according to OPTIONS. The value
1257 is printed using the current_language syntax. */
1260 value_print (struct value
*val
, struct ui_file
*stream
,
1261 const struct value_print_options
*options
)
1263 scoped_value_mark free_values
;
1265 if (!value_check_printable (val
, stream
, options
))
1271 = apply_ext_lang_val_pretty_printer (val
, stream
, 0, options
,
1278 current_language
->value_print (val
, stream
, options
);
1281 /* Meant to be used in debug sessions, so don't export it in a header file. */
1282 extern void ATTRIBUTE_UNUSED
debug_val (struct value
*val
);
1286 void ATTRIBUTE_UNUSED
1287 debug_val (struct value
*val
)
1289 value_print (val
, gdb_stdlog
, &user_print_options
);
1290 gdb_flush (gdb_stdlog
);
1294 val_print_type_code_flags (struct type
*type
, struct value
*original_value
,
1295 int embedded_offset
, struct ui_file
*stream
)
1297 const gdb_byte
*valaddr
= (original_value
->contents_for_printing ().data ()
1299 ULONGEST val
= unpack_long (type
, valaddr
);
1300 int field
, nfields
= type
->num_fields ();
1301 struct gdbarch
*gdbarch
= type
->arch ();
1302 struct type
*bool_type
= builtin_type (gdbarch
)->builtin_bool
;
1304 gdb_puts ("[", stream
);
1305 for (field
= 0; field
< nfields
; field
++)
1307 if (type
->field (field
).name ()[0] != '\0')
1309 struct type
*field_type
= type
->field (field
).type ();
1311 if (field_type
== bool_type
1312 /* We require boolean types here to be one bit wide. This is a
1313 problematic place to notify the user of an internal error
1314 though. Instead just fall through and print the field as an
1316 && type
->field (field
).bitsize () == 1)
1318 if (val
& ((ULONGEST
)1 << type
->field (field
).loc_bitpos ()))
1321 styled_string (variable_name_style
.style (),
1322 type
->field (field
).name ()));
1326 unsigned field_len
= type
->field (field
).bitsize ();
1327 ULONGEST field_val
= val
>> type
->field (field
).loc_bitpos ();
1329 if (field_len
< sizeof (ULONGEST
) * TARGET_CHAR_BIT
)
1330 field_val
&= ((ULONGEST
) 1 << field_len
) - 1;
1331 gdb_printf (stream
, " %ps=",
1332 styled_string (variable_name_style
.style (),
1333 type
->field (field
).name ()));
1334 if (field_type
->code () == TYPE_CODE_ENUM
)
1335 generic_val_print_enum_1 (field_type
, field_val
, stream
);
1337 print_longest (stream
, 'd', 0, field_val
);
1341 gdb_puts (" ]", stream
);
1344 /* See valprint.h. */
1347 value_print_scalar_formatted (struct value
*val
,
1348 const struct value_print_options
*options
,
1350 struct ui_file
*stream
)
1352 struct type
*type
= check_typedef (val
->type ());
1354 gdb_assert (val
!= NULL
);
1356 /* If we get here with a string format, try again without it. Go
1357 all the way back to the language printers, which may call us
1359 if (options
->format
== 's')
1361 struct value_print_options opts
= *options
;
1363 opts
.deref_ref
= false;
1364 common_val_print (val
, stream
, 0, &opts
, current_language
);
1368 /* value_contents_for_printing fetches all VAL's contents. They are
1369 needed to check whether VAL is optimized-out or unavailable
1371 const gdb_byte
*valaddr
= val
->contents_for_printing ().data ();
1373 /* A scalar object that does not have all bits available can't be
1374 printed, because all bits contribute to its representation. */
1375 if (val
->bits_any_optimized_out (0,
1376 TARGET_CHAR_BIT
* type
->length ()))
1377 val_print_optimized_out (val
, stream
);
1378 else if (!val
->bytes_available (0, type
->length ()))
1379 val_print_unavailable (stream
);
1381 print_scalar_formatted (valaddr
, type
, options
, size
, stream
);
1384 /* Print a number according to FORMAT which is one of d,u,x,o,b,h,w,g.
1385 The raison d'etre of this function is to consolidate printing of
1386 LONG_LONG's into this one function. The format chars b,h,w,g are
1387 from print_scalar_formatted(). Numbers are printed using C
1390 USE_C_FORMAT means to use C format in all cases. Without it,
1391 'o' and 'x' format do not include the standard C radix prefix
1394 Hilfinger/2004-09-09: USE_C_FORMAT was originally called USE_LOCAL
1395 and was intended to request formatting according to the current
1396 language and would be used for most integers that GDB prints. The
1397 exceptional cases were things like protocols where the format of
1398 the integer is a protocol thing, not a user-visible thing). The
1399 parameter remains to preserve the information of what things might
1400 be printed with language-specific format, should we ever resurrect
1404 print_longest (struct ui_file
*stream
, int format
, int use_c_format
,
1412 val
= int_string (val_long
, 10, 1, 0, 1); break;
1414 val
= int_string (val_long
, 10, 0, 0, 1); break;
1416 val
= int_string (val_long
, 16, 0, 0, use_c_format
); break;
1418 val
= int_string (val_long
, 16, 0, 2, 1); break;
1420 val
= int_string (val_long
, 16, 0, 4, 1); break;
1422 val
= int_string (val_long
, 16, 0, 8, 1); break;
1424 val
= int_string (val_long
, 16, 0, 16, 1); break;
1427 val
= int_string (val_long
, 8, 0, 0, use_c_format
); break;
1429 internal_error (_("failed internal consistency check"));
1431 gdb_puts (val
, stream
);
1434 /* This used to be a macro, but I don't think it is called often enough
1435 to merit such treatment. */
1436 /* Convert a LONGEST to an int. This is used in contexts (e.g. number of
1437 arguments to a function, number in a value history, register number, etc.)
1438 where the value must not be larger than can fit in an int. */
1441 longest_to_int (LONGEST arg
)
1443 /* Let the compiler do the work. */
1444 int rtnval
= (int) arg
;
1446 /* Check for overflows or underflows. */
1447 if (sizeof (LONGEST
) > sizeof (int))
1451 error (_("Value out of range."));
1457 /* Print a floating point value of floating-point type TYPE,
1458 pointed to in GDB by VALADDR, on STREAM. */
1461 print_floating (const gdb_byte
*valaddr
, struct type
*type
,
1462 struct ui_file
*stream
)
1464 std::string str
= target_float_to_string (valaddr
, type
);
1465 gdb_puts (str
.c_str (), stream
);
1469 print_binary_chars (struct ui_file
*stream
, const gdb_byte
*valaddr
,
1470 unsigned len
, enum bfd_endian byte_order
, bool zero_pad
,
1471 const struct value_print_options
*options
)
1476 bool seen_a_one
= false;
1477 const char *digit_separator
= nullptr;
1479 /* Declared "int" so it will be signed.
1480 This ensures that right shift will shift in zeros. */
1482 const int mask
= 0x080;
1484 if (options
->nibblesprint
)
1485 digit_separator
= current_language
->get_digit_separator();
1487 if (byte_order
== BFD_ENDIAN_BIG
)
1493 /* Every byte has 8 binary characters; peel off
1494 and print from the MSB end. */
1496 for (i
= 0; i
< (HOST_CHAR_BIT
* sizeof (*p
)); i
++)
1498 if (options
->nibblesprint
&& seen_a_one
&& i
% 4 == 0)
1499 gdb_putc (*digit_separator
, stream
);
1501 if (*p
& (mask
>> i
))
1506 if (zero_pad
|| seen_a_one
|| b
== '1')
1507 gdb_putc (b
, stream
);
1508 else if (options
->nibblesprint
)
1510 if ((0xf0 & (mask
>> i
) && (*p
& 0xf0))
1511 || (0x0f & (mask
>> i
) && (*p
& 0x0f)))
1512 gdb_putc (b
, stream
);
1522 for (p
= valaddr
+ len
- 1;
1526 for (i
= 0; i
< (HOST_CHAR_BIT
* sizeof (*p
)); i
++)
1528 if (options
->nibblesprint
&& seen_a_one
&& i
% 4 == 0)
1529 gdb_putc (*digit_separator
, stream
);
1531 if (*p
& (mask
>> i
))
1536 if (zero_pad
|| seen_a_one
|| b
== '1')
1537 gdb_putc (b
, stream
);
1538 else if (options
->nibblesprint
)
1540 if ((0xf0 & (mask
>> i
) && (*p
& 0xf0))
1541 || (0x0f & (mask
>> i
) && (*p
& 0x0f)))
1542 gdb_putc (b
, stream
);
1551 /* When not zero-padding, ensure that something is printed when the
1553 if (!zero_pad
&& !seen_a_one
)
1554 gdb_putc ('0', stream
);
1557 /* A helper for print_octal_chars that emits a single octal digit,
1558 optionally suppressing it if is zero and updating SEEN_A_ONE. */
1561 emit_octal_digit (struct ui_file
*stream
, bool *seen_a_one
, int digit
)
1563 if (*seen_a_one
|| digit
!= 0)
1564 gdb_printf (stream
, "%o", digit
);
1569 /* VALADDR points to an integer of LEN bytes.
1570 Print it in octal on stream or format it in buf. */
1573 print_octal_chars (struct ui_file
*stream
, const gdb_byte
*valaddr
,
1574 unsigned len
, enum bfd_endian byte_order
)
1577 unsigned char octa1
, octa2
, octa3
, carry
;
1580 /* Octal is 3 bits, which doesn't fit. Yuk. So we have to track
1581 * the extra bits, which cycle every three bytes:
1583 * Byte side: 0 1 2 3
1585 * bit number 123 456 78 | 9 012 345 6 | 78 901 234 | 567 890 12 |
1587 * Octal side: 0 1 carry 3 4 carry ...
1589 * Cycle number: 0 1 2
1591 * But of course we are printing from the high side, so we have to
1592 * figure out where in the cycle we are so that we end up with no
1593 * left over bits at the end.
1595 #define BITS_IN_OCTAL 3
1596 #define HIGH_ZERO 0340
1597 #define LOW_ZERO 0034
1598 #define CARRY_ZERO 0003
1599 static_assert (HIGH_ZERO
+ LOW_ZERO
+ CARRY_ZERO
== 0xff,
1600 "cycle zero constants are wrong");
1601 #define HIGH_ONE 0200
1602 #define MID_ONE 0160
1603 #define LOW_ONE 0016
1604 #define CARRY_ONE 0001
1605 static_assert (HIGH_ONE
+ MID_ONE
+ LOW_ONE
+ CARRY_ONE
== 0xff,
1606 "cycle one constants are wrong");
1607 #define HIGH_TWO 0300
1608 #define MID_TWO 0070
1609 #define LOW_TWO 0007
1610 static_assert (HIGH_TWO
+ MID_TWO
+ LOW_TWO
== 0xff,
1611 "cycle two constants are wrong");
1613 /* For 32 we start in cycle 2, with two bits and one bit carry;
1614 for 64 in cycle in cycle 1, with one bit and a two bit carry. */
1616 cycle
= (len
* HOST_CHAR_BIT
) % BITS_IN_OCTAL
;
1619 gdb_puts ("0", stream
);
1620 bool seen_a_one
= false;
1621 if (byte_order
== BFD_ENDIAN_BIG
)
1630 /* No carry in, carry out two bits. */
1632 octa1
= (HIGH_ZERO
& *p
) >> 5;
1633 octa2
= (LOW_ZERO
& *p
) >> 2;
1634 carry
= (CARRY_ZERO
& *p
);
1635 emit_octal_digit (stream
, &seen_a_one
, octa1
);
1636 emit_octal_digit (stream
, &seen_a_one
, octa2
);
1640 /* Carry in two bits, carry out one bit. */
1642 octa1
= (carry
<< 1) | ((HIGH_ONE
& *p
) >> 7);
1643 octa2
= (MID_ONE
& *p
) >> 4;
1644 octa3
= (LOW_ONE
& *p
) >> 1;
1645 carry
= (CARRY_ONE
& *p
);
1646 emit_octal_digit (stream
, &seen_a_one
, octa1
);
1647 emit_octal_digit (stream
, &seen_a_one
, octa2
);
1648 emit_octal_digit (stream
, &seen_a_one
, octa3
);
1652 /* Carry in one bit, no carry out. */
1654 octa1
= (carry
<< 2) | ((HIGH_TWO
& *p
) >> 6);
1655 octa2
= (MID_TWO
& *p
) >> 3;
1656 octa3
= (LOW_TWO
& *p
);
1658 emit_octal_digit (stream
, &seen_a_one
, octa1
);
1659 emit_octal_digit (stream
, &seen_a_one
, octa2
);
1660 emit_octal_digit (stream
, &seen_a_one
, octa3
);
1664 error (_("Internal error in octal conversion;"));
1668 cycle
= cycle
% BITS_IN_OCTAL
;
1673 for (p
= valaddr
+ len
- 1;
1680 /* Carry out, no carry in */
1682 octa1
= (HIGH_ZERO
& *p
) >> 5;
1683 octa2
= (LOW_ZERO
& *p
) >> 2;
1684 carry
= (CARRY_ZERO
& *p
);
1685 emit_octal_digit (stream
, &seen_a_one
, octa1
);
1686 emit_octal_digit (stream
, &seen_a_one
, octa2
);
1690 /* Carry in, carry out */
1692 octa1
= (carry
<< 1) | ((HIGH_ONE
& *p
) >> 7);
1693 octa2
= (MID_ONE
& *p
) >> 4;
1694 octa3
= (LOW_ONE
& *p
) >> 1;
1695 carry
= (CARRY_ONE
& *p
);
1696 emit_octal_digit (stream
, &seen_a_one
, octa1
);
1697 emit_octal_digit (stream
, &seen_a_one
, octa2
);
1698 emit_octal_digit (stream
, &seen_a_one
, octa3
);
1702 /* Carry in, no carry out */
1704 octa1
= (carry
<< 2) | ((HIGH_TWO
& *p
) >> 6);
1705 octa2
= (MID_TWO
& *p
) >> 3;
1706 octa3
= (LOW_TWO
& *p
);
1708 emit_octal_digit (stream
, &seen_a_one
, octa1
);
1709 emit_octal_digit (stream
, &seen_a_one
, octa2
);
1710 emit_octal_digit (stream
, &seen_a_one
, octa3
);
1714 error (_("Internal error in octal conversion;"));
1718 cycle
= cycle
% BITS_IN_OCTAL
;
1724 /* Possibly negate the integer represented by BYTES. It contains LEN
1725 bytes in the specified byte order. If the integer is negative,
1726 copy it into OUT_VEC, negate it, and return true. Otherwise, do
1727 nothing and return false. */
1730 maybe_negate_by_bytes (const gdb_byte
*bytes
, unsigned len
,
1731 enum bfd_endian byte_order
,
1732 gdb::byte_vector
*out_vec
)
1735 gdb_assert (len
> 0);
1736 if (byte_order
== BFD_ENDIAN_BIG
)
1737 sign_byte
= bytes
[0];
1739 sign_byte
= bytes
[len
- 1];
1740 if ((sign_byte
& 0x80) == 0)
1743 out_vec
->resize (len
);
1745 /* Compute -x == 1 + ~x. */
1746 if (byte_order
== BFD_ENDIAN_LITTLE
)
1749 for (unsigned i
= 0; i
< len
; ++i
)
1751 unsigned tem
= (0xff & ~bytes
[i
]) + carry
;
1752 (*out_vec
)[i
] = tem
& 0xff;
1759 for (unsigned i
= len
; i
> 0; --i
)
1761 unsigned tem
= (0xff & ~bytes
[i
- 1]) + carry
;
1762 (*out_vec
)[i
- 1] = tem
& 0xff;
1770 /* VALADDR points to an integer of LEN bytes.
1771 Print it in decimal on stream or format it in buf. */
1774 print_decimal_chars (struct ui_file
*stream
, const gdb_byte
*valaddr
,
1775 unsigned len
, bool is_signed
,
1776 enum bfd_endian byte_order
)
1779 #define CARRY_OUT( x ) ((x) / TEN) /* extend char to int */
1780 #define CARRY_LEFT( x ) ((x) % TEN)
1781 #define SHIFT( x ) ((x) << 4)
1782 #define LOW_NIBBLE( x ) ( (x) & 0x00F)
1783 #define HIGH_NIBBLE( x ) (((x) & 0x0F0) >> 4)
1788 int i
, j
, decimal_digits
;
1792 gdb::byte_vector negated_bytes
;
1794 && maybe_negate_by_bytes (valaddr
, len
, byte_order
, &negated_bytes
))
1796 gdb_puts ("-", stream
);
1797 valaddr
= negated_bytes
.data ();
1800 /* Base-ten number is less than twice as many digits
1801 as the base 16 number, which is 2 digits per byte. */
1803 decimal_len
= len
* 2 * 2;
1804 std::vector
<unsigned char> digits (decimal_len
, 0);
1806 /* Ok, we have an unknown number of bytes of data to be printed in
1809 * Given a hex number (in nibbles) as XYZ, we start by taking X and
1810 * decimalizing it as "x1 x2" in two decimal nibbles. Then we multiply
1811 * the nibbles by 16, add Y and re-decimalize. Repeat with Z.
1813 * The trick is that "digits" holds a base-10 number, but sometimes
1814 * the individual digits are > 10.
1816 * Outer loop is per nibble (hex digit) of input, from MSD end to
1819 decimal_digits
= 0; /* Number of decimal digits so far */
1820 p
= (byte_order
== BFD_ENDIAN_BIG
) ? valaddr
: valaddr
+ len
- 1;
1822 while ((byte_order
== BFD_ENDIAN_BIG
) ? (p
< valaddr
+ len
) : (p
>= valaddr
))
1825 * Multiply current base-ten number by 16 in place.
1826 * Each digit was between 0 and 9, now is between
1829 for (j
= 0; j
< decimal_digits
; j
++)
1831 digits
[j
] = SHIFT (digits
[j
]);
1834 /* Take the next nibble off the input and add it to what
1835 * we've got in the LSB position. Bottom 'digit' is now
1836 * between 0 and 159.
1838 * "flip" is used to run this loop twice for each byte.
1842 /* Take top nibble. */
1844 digits
[0] += HIGH_NIBBLE (*p
);
1849 /* Take low nibble and bump our pointer "p". */
1851 digits
[0] += LOW_NIBBLE (*p
);
1852 if (byte_order
== BFD_ENDIAN_BIG
)
1859 /* Re-decimalize. We have to do this often enough
1860 * that we don't overflow, but once per nibble is
1861 * overkill. Easier this way, though. Note that the
1862 * carry is often larger than 10 (e.g. max initial
1863 * carry out of lowest nibble is 15, could bubble all
1864 * the way up greater than 10). So we have to do
1865 * the carrying beyond the last current digit.
1868 for (j
= 0; j
< decimal_len
- 1; j
++)
1872 /* "/" won't handle an unsigned char with
1873 * a value that if signed would be negative.
1874 * So extend to longword int via "dummy".
1877 carry
= CARRY_OUT (dummy
);
1878 digits
[j
] = CARRY_LEFT (dummy
);
1880 if (j
>= decimal_digits
&& carry
== 0)
1883 * All higher digits are 0 and we
1884 * no longer have a carry.
1886 * Note: "j" is 0-based, "decimal_digits" is
1889 decimal_digits
= j
+ 1;
1895 /* Ok, now "digits" is the decimal representation, with
1896 the "decimal_digits" actual digits. Print! */
1898 for (i
= decimal_digits
- 1; i
> 0 && digits
[i
] == 0; --i
)
1903 gdb_printf (stream
, "%1d", digits
[i
]);
1907 /* VALADDR points to an integer of LEN bytes. Print it in hex on stream. */
1910 print_hex_chars (struct ui_file
*stream
, const gdb_byte
*valaddr
,
1911 unsigned len
, enum bfd_endian byte_order
,
1916 gdb_puts ("0x", stream
);
1917 if (byte_order
== BFD_ENDIAN_BIG
)
1923 /* Strip leading 0 bytes, but be sure to leave at least a
1924 single byte at the end. */
1925 for (; p
< valaddr
+ len
- 1 && !*p
; ++p
)
1929 const gdb_byte
*first
= p
;
1934 /* When not zero-padding, use a different format for the
1935 very first byte printed. */
1936 if (!zero_pad
&& p
== first
)
1937 gdb_printf (stream
, "%x", *p
);
1939 gdb_printf (stream
, "%02x", *p
);
1944 p
= valaddr
+ len
- 1;
1948 /* Strip leading 0 bytes, but be sure to leave at least a
1949 single byte at the end. */
1950 for (; p
>= valaddr
+ 1 && !*p
; --p
)
1954 const gdb_byte
*first
= p
;
1959 /* When not zero-padding, use a different format for the
1960 very first byte printed. */
1961 if (!zero_pad
&& p
== first
)
1962 gdb_printf (stream
, "%x", *p
);
1964 gdb_printf (stream
, "%02x", *p
);
1969 /* Print function pointer with inferior address ADDRESS onto stdio
1973 print_function_pointer_address (const struct value_print_options
*options
,
1974 struct gdbarch
*gdbarch
,
1976 struct ui_file
*stream
)
1978 CORE_ADDR func_addr
= gdbarch_convert_from_func_ptr_addr
1979 (gdbarch
, address
, current_inferior ()->top_target ());
1981 /* If the function pointer is represented by a description, print
1982 the address of the description. */
1983 if (options
->addressprint
&& func_addr
!= address
)
1985 gdb_puts ("@", stream
);
1986 gdb_puts (paddress (gdbarch
, address
), stream
);
1987 gdb_puts (": ", stream
);
1989 print_address_demangle (options
, gdbarch
, func_addr
, stream
, demangle
);
1993 /* Print on STREAM using the given OPTIONS the index for the element
1994 at INDEX of an array whose index type is INDEX_TYPE. */
1997 maybe_print_array_index (struct type
*index_type
, LONGEST index
,
1998 struct ui_file
*stream
,
1999 const struct value_print_options
*options
)
2001 if (!options
->print_array_indexes
)
2004 current_language
->print_array_index (index_type
, index
, stream
, options
);
2007 /* See valprint.h. */
2010 value_print_array_elements (struct value
*val
, struct ui_file
*stream
,
2012 const struct value_print_options
*options
,
2015 unsigned int things_printed
= 0;
2017 struct type
*elttype
, *index_type
;
2018 /* Position of the array element we are examining to see
2019 whether it is repeated. */
2021 /* Number of repetitions we have detected so far. */
2023 LONGEST low_bound
, high_bound
;
2025 struct type
*type
= check_typedef (val
->type ());
2027 elttype
= type
->target_type ();
2028 unsigned bit_stride
= type
->bit_stride ();
2029 if (bit_stride
== 0)
2030 bit_stride
= 8 * check_typedef (elttype
)->length ();
2031 index_type
= type
->index_type ();
2032 if (index_type
->code () == TYPE_CODE_RANGE
)
2033 index_type
= index_type
->target_type ();
2035 if (get_array_bounds (type
, &low_bound
, &high_bound
))
2037 /* The array length should normally be HIGH_BOUND - LOW_BOUND +
2038 1. But we have to be a little extra careful, because some
2039 languages such as Ada allow LOW_BOUND to be greater than
2040 HIGH_BOUND for empty arrays. In that situation, the array
2041 length is just zero, not negative! */
2042 if (low_bound
> high_bound
)
2045 len
= high_bound
- low_bound
+ 1;
2049 warning (_("unable to get bounds of array, assuming null array"));
2054 annotate_array_section_begin (i
, elttype
);
2056 for (; i
< len
&& things_printed
< options
->print_max
; i
++)
2058 scoped_value_mark free_values
;
2062 if (options
->prettyformat_arrays
)
2064 gdb_printf (stream
, ",\n");
2065 print_spaces (2 + 2 * recurse
, stream
);
2068 gdb_printf (stream
, ", ");
2070 else if (options
->prettyformat_arrays
)
2072 gdb_printf (stream
, "\n");
2073 print_spaces (2 + 2 * recurse
, stream
);
2075 stream
->wrap_here (2 + 2 * recurse
);
2076 maybe_print_array_index (index_type
, i
+ low_bound
,
2079 struct value
*element
= val
->from_component_bitsize (elttype
,
2084 /* Only check for reps if repeat_count_threshold is not set to
2085 UINT_MAX (unlimited). */
2086 if (options
->repeat_count_threshold
< UINT_MAX
)
2088 bool unavailable
= element
->entirely_unavailable ();
2089 bool available
= element
->entirely_available ();
2093 /* When printing large arrays this spot is called frequently, so
2094 clean up temporary values asap to prevent allocating a large
2096 scoped_value_mark free_values_inner
;
2097 struct value
*rep_elt
2098 = val
->from_component_bitsize (elttype
,
2101 bool repeated
= ((available
2102 && rep_elt
->entirely_available ()
2103 && element
->contents_eq (rep_elt
))
2105 && rep_elt
->entirely_unavailable ()));
2113 common_val_print (element
, stream
, recurse
+ 1, options
,
2116 if (reps
> options
->repeat_count_threshold
)
2118 annotate_elt_rep (reps
);
2119 gdb_printf (stream
, " %p[<repeats %u times>%p]",
2120 metadata_style
.style ().ptr (), reps
, nullptr);
2121 annotate_elt_rep_end ();
2124 things_printed
+= options
->repeat_count_threshold
;
2132 annotate_array_section_end ();
2134 gdb_printf (stream
, "...");
2135 if (options
->prettyformat_arrays
)
2137 gdb_printf (stream
, "\n");
2138 print_spaces (2 * recurse
, stream
);
2142 /* Return true if print_wchar can display W without resorting to a
2143 numeric escape, false otherwise. */
2146 wchar_printable (gdb_wchar_t w
)
2148 return (gdb_iswprint (w
)
2149 || w
== LCST ('\a') || w
== LCST ('\b')
2150 || w
== LCST ('\f') || w
== LCST ('\n')
2151 || w
== LCST ('\r') || w
== LCST ('\t')
2152 || w
== LCST ('\v'));
2155 /* A helper function that converts the contents of STRING to wide
2156 characters and then appends them to OUTPUT. */
2159 append_string_as_wide (const char *string
,
2160 struct obstack
*output
)
2162 for (; *string
; ++string
)
2164 gdb_wchar_t w
= gdb_btowc (*string
);
2165 obstack_grow (output
, &w
, sizeof (gdb_wchar_t
));
2169 /* Print a wide character W to OUTPUT. ORIG is a pointer to the
2170 original (target) bytes representing the character, ORIG_LEN is the
2171 number of valid bytes. WIDTH is the number of bytes in a base
2172 characters of the type. OUTPUT is an obstack to which wide
2173 characters are emitted. QUOTER is a (narrow) character indicating
2174 the style of quotes surrounding the character to be printed.
2175 NEED_ESCAPE is an in/out flag which is used to track numeric
2176 escapes across calls. */
2179 print_wchar (gdb_wint_t w
, const gdb_byte
*orig
,
2180 int orig_len
, int width
,
2181 enum bfd_endian byte_order
,
2182 struct obstack
*output
,
2183 int quoter
, bool *need_escapep
)
2185 bool need_escape
= *need_escapep
;
2187 *need_escapep
= false;
2189 /* If any additional cases are added to this switch block, then the
2190 function wchar_printable will likely need updating too. */
2194 obstack_grow_wstr (output
, LCST ("\\a"));
2197 obstack_grow_wstr (output
, LCST ("\\b"));
2200 obstack_grow_wstr (output
, LCST ("\\f"));
2203 obstack_grow_wstr (output
, LCST ("\\n"));
2206 obstack_grow_wstr (output
, LCST ("\\r"));
2209 obstack_grow_wstr (output
, LCST ("\\t"));
2212 obstack_grow_wstr (output
, LCST ("\\v"));
2216 if (gdb_iswprint (w
) && !(need_escape
&& gdb_iswxdigit (w
)))
2218 gdb_wchar_t wchar
= w
;
2220 if (w
== gdb_btowc (quoter
) || w
== LCST ('\\'))
2221 obstack_grow_wstr (output
, LCST ("\\"));
2222 obstack_grow (output
, &wchar
, sizeof (gdb_wchar_t
));
2228 for (i
= 0; i
+ width
<= orig_len
; i
+= width
)
2233 value
= extract_unsigned_integer (&orig
[i
], width
,
2235 /* If the value fits in 3 octal digits, print it that
2236 way. Otherwise, print it as a hex escape. */
2239 xsnprintf (octal
, sizeof (octal
), "\\%.3o",
2240 (int) (value
& 0777));
2241 *need_escapep
= false;
2245 xsnprintf (octal
, sizeof (octal
), "\\x%lx", (long) value
);
2246 /* A hex escape might require the next character
2247 to be escaped, because, unlike with octal,
2248 hex escapes have no length limit. */
2249 *need_escapep
= true;
2251 append_string_as_wide (octal
, output
);
2253 /* If we somehow have extra bytes, print them now. */
2254 while (i
< orig_len
)
2258 xsnprintf (octal
, sizeof (octal
), "\\%.3o", orig
[i
] & 0xff);
2259 *need_escapep
= false;
2260 append_string_as_wide (octal
, output
);
2269 /* Print the character C on STREAM as part of the contents of a
2270 literal string whose delimiter is QUOTER. ENCODING names the
2274 generic_emit_char (int c
, struct type
*type
, struct ui_file
*stream
,
2275 int quoter
, const char *encoding
)
2277 enum bfd_endian byte_order
2278 = type_byte_order (type
);
2280 bool need_escape
= false;
2282 c_buf
= (gdb_byte
*) alloca (type
->length ());
2283 pack_long (c_buf
, type
, c
);
2285 wchar_iterator
iter (c_buf
, type
->length (), encoding
, type
->length ());
2287 /* This holds the printable form of the wchar_t data. */
2288 auto_obstack wchar_buf
;
2294 const gdb_byte
*buf
;
2296 int print_escape
= 1;
2297 enum wchar_iterate_result result
;
2299 num_chars
= iter
.iterate (&result
, &chars
, &buf
, &buflen
);
2304 /* If all characters are printable, print them. Otherwise,
2305 we're going to have to print an escape sequence. We
2306 check all characters because we want to print the target
2307 bytes in the escape sequence, and we don't know character
2308 boundaries there. */
2312 for (i
= 0; i
< num_chars
; ++i
)
2313 if (!wchar_printable (chars
[i
]))
2321 for (i
= 0; i
< num_chars
; ++i
)
2322 print_wchar (chars
[i
], buf
, buflen
,
2323 type
->length (), byte_order
,
2324 &wchar_buf
, quoter
, &need_escape
);
2328 /* This handles the NUM_CHARS == 0 case as well. */
2330 print_wchar (gdb_WEOF
, buf
, buflen
, type
->length (),
2331 byte_order
, &wchar_buf
, quoter
, &need_escape
);
2334 /* The output in the host encoding. */
2335 auto_obstack output
;
2337 convert_between_encodings (INTERMEDIATE_ENCODING
, host_charset (),
2338 (gdb_byte
*) obstack_base (&wchar_buf
),
2339 obstack_object_size (&wchar_buf
),
2340 sizeof (gdb_wchar_t
), &output
, translit_char
);
2341 obstack_1grow (&output
, '\0');
2343 gdb_puts ((const char *) obstack_base (&output
), stream
);
2346 /* Return the repeat count of the next character/byte in ITER,
2347 storing the result in VEC. */
2350 count_next_character (wchar_iterator
*iter
,
2351 std::vector
<converted_character
> *vec
)
2353 struct converted_character
*current
;
2357 struct converted_character tmp
;
2361 = iter
->iterate (&tmp
.result
, &chars
, &tmp
.buf
, &tmp
.buflen
);
2362 if (tmp
.num_chars
> 0)
2364 gdb_assert (tmp
.num_chars
< MAX_WCHARS
);
2365 memcpy (tmp
.chars
, chars
, tmp
.num_chars
* sizeof (gdb_wchar_t
));
2367 vec
->push_back (tmp
);
2370 current
= &vec
->back ();
2372 /* Count repeated characters or bytes. */
2373 current
->repeat_count
= 1;
2374 if (current
->num_chars
== -1)
2382 struct converted_character d
;
2389 /* Get the next character. */
2390 d
.num_chars
= iter
->iterate (&d
.result
, &chars
, &d
.buf
, &d
.buflen
);
2392 /* If a character was successfully converted, save the character
2393 into the converted character. */
2394 if (d
.num_chars
> 0)
2396 gdb_assert (d
.num_chars
< MAX_WCHARS
);
2397 memcpy (d
.chars
, chars
, WCHAR_BUFLEN (d
.num_chars
));
2400 /* Determine if the current character is the same as this
2402 if (d
.num_chars
== current
->num_chars
&& d
.result
== current
->result
)
2404 /* There are two cases to consider:
2406 1) Equality of converted character (num_chars > 0)
2407 2) Equality of non-converted character (num_chars == 0) */
2408 if ((current
->num_chars
> 0
2409 && memcmp (current
->chars
, d
.chars
,
2410 WCHAR_BUFLEN (current
->num_chars
)) == 0)
2411 || (current
->num_chars
== 0
2412 && current
->buflen
== d
.buflen
2413 && memcmp (current
->buf
, d
.buf
, current
->buflen
) == 0))
2414 ++current
->repeat_count
;
2422 /* Push this next converted character onto the result vector. */
2423 repeat
= current
->repeat_count
;
2429 /* Print the characters in CHARS to the OBSTACK. QUOTE_CHAR is the quote
2430 character to use with string output. WIDTH is the size of the output
2431 character type. BYTE_ORDER is the target byte order. OPTIONS
2432 is the user's print options. *FINISHED is set to 0 if we didn't print
2433 all the elements in CHARS. */
2436 print_converted_chars_to_obstack (struct obstack
*obstack
,
2437 const std::vector
<converted_character
> &chars
,
2438 int quote_char
, int width
,
2439 enum bfd_endian byte_order
,
2440 const struct value_print_options
*options
,
2443 unsigned int idx
, num_elements
;
2444 const converted_character
*elem
;
2445 enum {START
, SINGLE
, REPEAT
, INCOMPLETE
, FINISH
} state
, last
;
2446 gdb_wchar_t wide_quote_char
= gdb_btowc (quote_char
);
2447 bool need_escape
= false;
2448 const int print_max
= options
->print_max_chars
> 0
2449 ? options
->print_max_chars
: options
->print_max
;
2451 /* Set the start state. */
2452 idx
= num_elements
= 0;
2453 last
= state
= START
;
2461 /* Nothing to do. */
2468 /* We are outputting a single character
2469 (< options->repeat_count_threshold). */
2473 /* We were outputting some other type of content, so we
2474 must output and a comma and a quote. */
2476 obstack_grow_wstr (obstack
, LCST (", "));
2477 obstack_grow (obstack
, &wide_quote_char
, sizeof (gdb_wchar_t
));
2479 /* Output the character. */
2480 int repeat_count
= elem
->repeat_count
;
2481 if (print_max
< repeat_count
+ num_elements
)
2483 repeat_count
= print_max
- num_elements
;
2486 for (j
= 0; j
< repeat_count
; ++j
)
2488 if (elem
->result
== wchar_iterate_ok
)
2489 print_wchar (elem
->chars
[0], elem
->buf
, elem
->buflen
, width
,
2490 byte_order
, obstack
, quote_char
, &need_escape
);
2492 print_wchar (gdb_WEOF
, elem
->buf
, elem
->buflen
, width
,
2493 byte_order
, obstack
, quote_char
, &need_escape
);
2503 /* We are outputting a character with a repeat count
2504 greater than options->repeat_count_threshold. */
2508 /* We were outputting a single string. Terminate the
2510 obstack_grow (obstack
, &wide_quote_char
, sizeof (gdb_wchar_t
));
2513 obstack_grow_wstr (obstack
, LCST (", "));
2515 /* Output the character and repeat string. */
2516 obstack_grow_wstr (obstack
, LCST ("'"));
2517 if (elem
->result
== wchar_iterate_ok
)
2518 print_wchar (elem
->chars
[0], elem
->buf
, elem
->buflen
, width
,
2519 byte_order
, obstack
, quote_char
, &need_escape
);
2521 print_wchar (gdb_WEOF
, elem
->buf
, elem
->buflen
, width
,
2522 byte_order
, obstack
, quote_char
, &need_escape
);
2523 obstack_grow_wstr (obstack
, LCST ("'"));
2524 std::string s
= string_printf (_(" <repeats %u times>"),
2525 elem
->repeat_count
);
2526 num_elements
+= elem
->repeat_count
;
2527 for (j
= 0; s
[j
]; ++j
)
2529 gdb_wchar_t w
= gdb_btowc (s
[j
]);
2530 obstack_grow (obstack
, &w
, sizeof (gdb_wchar_t
));
2536 /* We are outputting an incomplete sequence. */
2539 /* If we were outputting a string of SINGLE characters,
2540 terminate the quote. */
2541 obstack_grow (obstack
, &wide_quote_char
, sizeof (gdb_wchar_t
));
2544 obstack_grow_wstr (obstack
, LCST (", "));
2546 /* Output the incomplete sequence string. */
2547 obstack_grow_wstr (obstack
, LCST ("<incomplete sequence "));
2548 print_wchar (gdb_WEOF
, elem
->buf
, elem
->buflen
, width
, byte_order
,
2549 obstack
, 0, &need_escape
);
2550 obstack_grow_wstr (obstack
, LCST (">"));
2553 /* We do not attempt to output anything after this. */
2558 /* All done. If we were outputting a string of SINGLE
2559 characters, the string must be terminated. Otherwise,
2560 REPEAT and INCOMPLETE are always left properly terminated. */
2562 obstack_grow (obstack
, &wide_quote_char
, sizeof (gdb_wchar_t
));
2567 /* Get the next element and state. */
2569 if (state
!= FINISH
)
2571 elem
= &chars
[idx
++];
2572 switch (elem
->result
)
2574 case wchar_iterate_ok
:
2575 case wchar_iterate_invalid
:
2576 if (elem
->repeat_count
> options
->repeat_count_threshold
)
2582 case wchar_iterate_incomplete
:
2586 case wchar_iterate_eof
:
2594 /* Print the character string STRING, printing at most LENGTH
2595 characters. LENGTH is -1 if the string is nul terminated. TYPE is
2596 the type of each character. OPTIONS holds the printing options;
2597 printing stops early if the number hits print_max_chars; repeat
2598 counts are printed as appropriate. Print ellipses at the end if we
2599 had to stop before printing LENGTH characters, or if FORCE_ELLIPSES.
2600 QUOTE_CHAR is the character to print at each end of the string. If
2601 C_STYLE_TERMINATOR is true, and the last character is 0, then it is
2605 generic_printstr (struct ui_file
*stream
, struct type
*type
,
2606 const gdb_byte
*string
, unsigned int length
,
2607 const char *encoding
, int force_ellipses
,
2608 int quote_char
, int c_style_terminator
,
2609 const struct value_print_options
*options
)
2611 enum bfd_endian byte_order
= type_byte_order (type
);
2613 int width
= type
->length ();
2615 struct converted_character
*last
;
2619 unsigned long current_char
= 1;
2621 for (i
= 0; current_char
; ++i
)
2624 current_char
= extract_unsigned_integer (string
+ i
* width
,
2630 /* If the string was not truncated due to `set print elements', and
2631 the last byte of it is a null, we don't print that, in
2632 traditional C style. */
2633 if (c_style_terminator
2636 && (extract_unsigned_integer (string
+ (length
- 1) * width
,
2637 width
, byte_order
) == 0))
2642 gdb_printf (stream
, "%c%c", quote_char
, quote_char
);
2646 /* Arrange to iterate over the characters, in wchar_t form. */
2647 wchar_iterator
iter (string
, length
* width
, encoding
, width
);
2648 std::vector
<converted_character
> converted_chars
;
2650 /* Convert characters until the string is over or the maximum
2651 number of printed characters has been reached. */
2653 unsigned int print_max_chars
= get_print_max_chars (options
);
2654 while (i
< print_max_chars
)
2660 /* Grab the next character and repeat count. */
2661 r
= count_next_character (&iter
, &converted_chars
);
2663 /* If less than zero, the end of the input string was reached. */
2667 /* Otherwise, add the count to the total print count and get
2668 the next character. */
2672 /* Get the last element and determine if the entire string was
2674 last
= &converted_chars
.back ();
2675 finished
= (last
->result
== wchar_iterate_eof
);
2677 /* Ensure that CONVERTED_CHARS is terminated. */
2678 last
->result
= wchar_iterate_eof
;
2680 /* WCHAR_BUF is the obstack we use to represent the string in
2682 auto_obstack wchar_buf
;
2684 /* Print the output string to the obstack. */
2685 print_converted_chars_to_obstack (&wchar_buf
, converted_chars
, quote_char
,
2686 width
, byte_order
, options
, &finished
);
2688 if (force_ellipses
|| !finished
)
2689 obstack_grow_wstr (&wchar_buf
, LCST ("..."));
2691 /* OUTPUT is where we collect `char's for printing. */
2692 auto_obstack output
;
2694 convert_between_encodings (INTERMEDIATE_ENCODING
, host_charset (),
2695 (gdb_byte
*) obstack_base (&wchar_buf
),
2696 obstack_object_size (&wchar_buf
),
2697 sizeof (gdb_wchar_t
), &output
, translit_char
);
2698 obstack_1grow (&output
, '\0');
2700 gdb_puts ((const char *) obstack_base (&output
), stream
);
2703 /* Print a string from the inferior, starting at ADDR and printing up to LEN
2704 characters, of WIDTH bytes a piece, to STREAM. If LEN is -1, printing
2705 stops at the first null byte, otherwise printing proceeds (including null
2706 bytes) until either print_max_chars or LEN characters have been printed,
2707 whichever is smaller. ENCODING is the name of the string's
2708 encoding. It can be NULL, in which case the target encoding is
2712 val_print_string (struct type
*elttype
, const char *encoding
,
2713 CORE_ADDR addr
, int len
,
2714 struct ui_file
*stream
,
2715 const struct value_print_options
*options
)
2717 int force_ellipsis
= 0; /* Force ellipsis to be printed if nonzero. */
2718 int err
; /* Non-zero if we got a bad read. */
2719 int found_nul
; /* Non-zero if we found the nul char. */
2720 unsigned int fetchlimit
; /* Maximum number of chars to print. */
2722 gdb::unique_xmalloc_ptr
<gdb_byte
> buffer
; /* Dynamically growable fetch buffer. */
2723 struct gdbarch
*gdbarch
= elttype
->arch ();
2724 enum bfd_endian byte_order
= type_byte_order (elttype
);
2725 int width
= elttype
->length ();
2727 /* First we need to figure out the limit on the number of characters we are
2728 going to attempt to fetch and print. This is actually pretty simple.
2729 If LEN >= zero, then the limit is the minimum of LEN and print_max_chars.
2730 If LEN is -1, then the limit is print_max_chars. This is true regardless
2731 of whether print_max_chars is zero, UINT_MAX (unlimited), or something in
2732 between, because finding the null byte (or available memory) is what
2733 actually limits the fetch. */
2735 unsigned int print_max_chars
= get_print_max_chars (options
);
2736 fetchlimit
= (len
== -1
2738 : std::min ((unsigned) len
, print_max_chars
));
2740 err
= target_read_string (addr
, len
, width
, fetchlimit
,
2741 &buffer
, &bytes_read
);
2745 /* We now have either successfully filled the buffer to fetchlimit,
2746 or terminated early due to an error or finding a null char when
2749 /* Determine found_nul by looking at the last character read. */
2751 if (bytes_read
>= width
)
2752 found_nul
= extract_unsigned_integer (buffer
.get () + bytes_read
- width
,
2753 width
, byte_order
) == 0;
2754 if (len
== -1 && !found_nul
)
2758 /* We didn't find a NUL terminator we were looking for. Attempt
2759 to peek at the next character. If not successful, or it is not
2760 a null byte, then force ellipsis to be printed. */
2762 peekbuf
= (gdb_byte
*) alloca (width
);
2764 if (target_read_memory (addr
, peekbuf
, width
) == 0
2765 && extract_unsigned_integer (peekbuf
, width
, byte_order
) != 0)
2768 else if ((len
>= 0 && err
!= 0) || (len
> bytes_read
/ width
))
2770 /* Getting an error when we have a requested length, or fetching less
2771 than the number of characters actually requested, always make us
2776 /* If we get an error before fetching anything, don't print a string.
2777 But if we fetch something and then get an error, print the string
2778 and then the error message. */
2779 if (err
== 0 || bytes_read
> 0)
2780 current_language
->printstr (stream
, elttype
, buffer
.get (),
2782 encoding
, force_ellipsis
, options
);
2786 std::string str
= memory_error_message (TARGET_XFER_E_IO
, gdbarch
, addr
);
2788 gdb_printf (stream
, _("<error: %ps>"),
2789 styled_string (metadata_style
.style (),
2793 return (bytes_read
/ width
);
2796 /* Handle 'show print max-depth'. */
2799 show_print_max_depth (struct ui_file
*file
, int from_tty
,
2800 struct cmd_list_element
*c
, const char *value
)
2802 gdb_printf (file
, _("Maximum print depth is %s.\n"), value
);
2806 /* The 'set input-radix' command writes to this auxiliary variable.
2807 If the requested radix is valid, INPUT_RADIX is updated; otherwise,
2808 it is left unchanged. */
2810 static unsigned input_radix_1
= 10;
2812 /* Validate an input or output radix setting, and make sure the user
2813 knows what they really did here. Radix setting is confusing, e.g.
2814 setting the input radix to "10" never changes it! */
2817 set_input_radix (const char *args
, int from_tty
, struct cmd_list_element
*c
)
2819 set_input_radix_1 (from_tty
, input_radix_1
);
2823 set_input_radix_1 (int from_tty
, unsigned radix
)
2825 /* We don't currently disallow any input radix except 0 or 1, which don't
2826 make any mathematical sense. In theory, we can deal with any input
2827 radix greater than 1, even if we don't have unique digits for every
2828 value from 0 to radix-1, but in practice we lose on large radix values.
2829 We should either fix the lossage or restrict the radix range more.
2834 input_radix_1
= input_radix
;
2835 error (_("Nonsense input radix ``decimal %u''; input radix unchanged."),
2838 input_radix_1
= input_radix
= radix
;
2841 gdb_printf (_("Input radix now set to "
2842 "decimal %u, hex %x, octal %o.\n"),
2843 radix
, radix
, radix
);
2847 /* The 'set output-radix' command writes to this auxiliary variable.
2848 If the requested radix is valid, OUTPUT_RADIX is updated,
2849 otherwise, it is left unchanged. */
2851 static unsigned output_radix_1
= 10;
2854 set_output_radix (const char *args
, int from_tty
, struct cmd_list_element
*c
)
2856 set_output_radix_1 (from_tty
, output_radix_1
);
2860 set_output_radix_1 (int from_tty
, unsigned radix
)
2862 /* Validate the radix and disallow ones that we aren't prepared to
2863 handle correctly, leaving the radix unchanged. */
2867 user_print_options
.output_format
= 'x'; /* hex */
2870 user_print_options
.output_format
= 0; /* decimal */
2873 user_print_options
.output_format
= 'o'; /* octal */
2876 output_radix_1
= output_radix
;
2877 error (_("Unsupported output radix ``decimal %u''; "
2878 "output radix unchanged."),
2881 output_radix_1
= output_radix
= radix
;
2884 gdb_printf (_("Output radix now set to "
2885 "decimal %u, hex %x, octal %o.\n"),
2886 radix
, radix
, radix
);
2890 /* Set both the input and output radix at once. Try to set the output radix
2891 first, since it has the most restrictive range. An radix that is valid as
2892 an output radix is also valid as an input radix.
2894 It may be useful to have an unusual input radix. If the user wishes to
2895 set an input radix that is not valid as an output radix, he needs to use
2896 the 'set input-radix' command. */
2899 set_radix (const char *arg
, int from_tty
)
2903 radix
= (arg
== NULL
) ? 10 : parse_and_eval_long (arg
);
2904 set_output_radix_1 (0, radix
);
2905 set_input_radix_1 (0, radix
);
2908 gdb_printf (_("Input and output radices now set to "
2909 "decimal %u, hex %x, octal %o.\n"),
2910 radix
, radix
, radix
);
2914 /* Show both the input and output radices. */
2917 show_radix (const char *arg
, int from_tty
)
2921 if (input_radix
== output_radix
)
2923 gdb_printf (_("Input and output radices set to "
2924 "decimal %u, hex %x, octal %o.\n"),
2925 input_radix
, input_radix
, input_radix
);
2929 gdb_printf (_("Input radix set to decimal "
2930 "%u, hex %x, octal %o.\n"),
2931 input_radix
, input_radix
, input_radix
);
2932 gdb_printf (_("Output radix set to decimal "
2933 "%u, hex %x, octal %o.\n"),
2934 output_radix
, output_radix
, output_radix
);
2940 /* Controls printing of vtbl's. */
2942 show_vtblprint (struct ui_file
*file
, int from_tty
,
2943 struct cmd_list_element
*c
, const char *value
)
2945 gdb_printf (file
, _("\
2946 Printing of C++ virtual function tables is %s.\n"),
2950 /* Controls looking up an object's derived type using what we find in
2953 show_objectprint (struct ui_file
*file
, int from_tty
,
2954 struct cmd_list_element
*c
,
2957 gdb_printf (file
, _("\
2958 Printing of object's derived type based on vtable info is %s.\n"),
2963 show_static_field_print (struct ui_file
*file
, int from_tty
,
2964 struct cmd_list_element
*c
,
2968 _("Printing of C++ static members is %s.\n"),
2974 /* A couple typedefs to make writing the options a bit more
2976 using boolean_option_def
2977 = gdb::option::boolean_option_def
<value_print_options
>;
2978 using uinteger_option_def
2979 = gdb::option::uinteger_option_def
<value_print_options
>;
2980 using pinteger_option_def
2981 = gdb::option::pinteger_option_def
<value_print_options
>;
2983 /* Extra literals supported with the `set print characters' and
2984 `print -characters' commands. */
2985 static const literal_def print_characters_literals
[] =
2987 { "elements", PRINT_MAX_CHARS_ELEMENTS
},
2988 { "unlimited", PRINT_MAX_CHARS_UNLIMITED
, 0 },
2992 /* Definitions of options for the "print" and "compile print"
2994 static const gdb::option::option_def value_print_option_defs
[] = {
2996 boolean_option_def
{
2998 [] (value_print_options
*opt
) { return &opt
->addressprint
; },
2999 show_addressprint
, /* show_cmd_cb */
3000 N_("Set printing of addresses."),
3001 N_("Show printing of addresses."),
3002 NULL
, /* help_doc */
3005 boolean_option_def
{
3007 [] (value_print_options
*opt
) { return &opt
->prettyformat_arrays
; },
3008 show_prettyformat_arrays
, /* show_cmd_cb */
3009 N_("Set pretty formatting of arrays."),
3010 N_("Show pretty formatting of arrays."),
3011 NULL
, /* help_doc */
3014 boolean_option_def
{
3016 [] (value_print_options
*opt
) { return &opt
->print_array_indexes
; },
3017 show_print_array_indexes
, /* show_cmd_cb */
3018 N_("Set printing of array indexes."),
3019 N_("Show printing of array indexes."),
3020 NULL
, /* help_doc */
3023 boolean_option_def
{
3025 [] (value_print_options
*opt
) { return &opt
->nibblesprint
; },
3026 show_nibbles
, /* show_cmd_cb */
3027 N_("Set whether to print binary values in groups of four bits."),
3028 N_("Show whether to print binary values in groups of four bits."),
3029 NULL
, /* help_doc */
3032 uinteger_option_def
{
3034 [] (value_print_options
*opt
) { return &opt
->print_max_chars
; },
3035 print_characters_literals
,
3036 show_print_max_chars
, /* show_cmd_cb */
3037 N_("Set limit on string chars to print."),
3038 N_("Show limit on string chars to print."),
3039 N_("\"elements\" causes the array element limit to be used.\n"
3040 "\"unlimited\" causes there to be no limit."),
3043 uinteger_option_def
{
3045 [] (value_print_options
*opt
) { return &opt
->print_max
; },
3046 uinteger_unlimited_literals
,
3047 show_print_max
, /* show_cmd_cb */
3048 N_("Set limit on array elements to print."),
3049 N_("Show limit on array elements to print."),
3050 N_("\"unlimited\" causes there to be no limit.\n"
3051 "This setting also applies to string chars when \"print characters\"\n"
3052 "is set to \"elements\"."),
3055 pinteger_option_def
{
3057 [] (value_print_options
*opt
) { return &opt
->max_depth
; },
3058 pinteger_unlimited_literals
,
3059 show_print_max_depth
, /* show_cmd_cb */
3060 N_("Set maximum print depth for nested structures, unions and arrays."),
3061 N_("Show maximum print depth for nested structures, unions, and arrays."),
3062 N_("When structures, unions, or arrays are nested beyond this depth then they\n\
3063 will be replaced with either '{...}' or '(...)' depending on the language.\n\
3064 Use \"unlimited\" to print the complete structure.")
3067 boolean_option_def
{
3068 "memory-tag-violations",
3069 [] (value_print_options
*opt
) { return &opt
->memory_tag_violations
; },
3070 show_memory_tag_violations
, /* show_cmd_cb */
3071 N_("Set printing of memory tag violations for pointers."),
3072 N_("Show printing of memory tag violations for pointers."),
3073 N_("Issue a warning when the printed value is a pointer\n\
3074 whose logical tag doesn't match the allocation tag of the memory\n\
3075 location it points to."),
3078 boolean_option_def
{
3080 [] (value_print_options
*opt
) { return &opt
->stop_print_at_null
; },
3081 show_stop_print_at_null
, /* show_cmd_cb */
3082 N_("Set printing of char arrays to stop at first null char."),
3083 N_("Show printing of char arrays to stop at first null char."),
3084 NULL
, /* help_doc */
3087 boolean_option_def
{
3089 [] (value_print_options
*opt
) { return &opt
->objectprint
; },
3090 show_objectprint
, /* show_cmd_cb */
3091 _("Set printing of C++ virtual function tables."),
3092 _("Show printing of C++ virtual function tables."),
3093 NULL
, /* help_doc */
3096 boolean_option_def
{
3098 [] (value_print_options
*opt
) { return &opt
->prettyformat_structs
; },
3099 show_prettyformat_structs
, /* show_cmd_cb */
3100 N_("Set pretty formatting of structures."),
3101 N_("Show pretty formatting of structures."),
3102 NULL
, /* help_doc */
3105 boolean_option_def
{
3107 [] (value_print_options
*opt
) { return &opt
->raw
; },
3108 NULL
, /* show_cmd_cb */
3109 N_("Set whether to print values in raw form."),
3110 N_("Show whether to print values in raw form."),
3111 N_("If set, values are printed in raw form, bypassing any\n\
3112 pretty-printers for that value.")
3115 uinteger_option_def
{
3117 [] (value_print_options
*opt
) { return &opt
->repeat_count_threshold
; },
3118 uinteger_unlimited_literals
,
3119 show_repeat_count_threshold
, /* show_cmd_cb */
3120 N_("Set threshold for repeated print elements."),
3121 N_("Show threshold for repeated print elements."),
3122 N_("\"unlimited\" causes all elements to be individually printed."),
3125 boolean_option_def
{
3127 [] (value_print_options
*opt
) { return &opt
->static_field_print
; },
3128 show_static_field_print
, /* show_cmd_cb */
3129 N_("Set printing of C++ static members."),
3130 N_("Show printing of C++ static members."),
3131 NULL
, /* help_doc */
3134 boolean_option_def
{
3136 [] (value_print_options
*opt
) { return &opt
->symbol_print
; },
3137 show_symbol_print
, /* show_cmd_cb */
3138 N_("Set printing of symbol names when printing pointers."),
3139 N_("Show printing of symbol names when printing pointers."),
3140 NULL
, /* help_doc */
3143 boolean_option_def
{
3145 [] (value_print_options
*opt
) { return &opt
->unionprint
; },
3146 show_unionprint
, /* show_cmd_cb */
3147 N_("Set printing of unions interior to structures."),
3148 N_("Show printing of unions interior to structures."),
3149 NULL
, /* help_doc */
3152 boolean_option_def
{
3154 [] (value_print_options
*opt
) { return &opt
->vtblprint
; },
3155 show_vtblprint
, /* show_cmd_cb */
3156 N_("Set printing of C++ virtual function tables."),
3157 N_("Show printing of C++ virtual function tables."),
3158 NULL
, /* help_doc */
3162 /* See valprint.h. */
3164 gdb::option::option_def_group
3165 make_value_print_options_def_group (value_print_options
*opts
)
3167 return {{value_print_option_defs
}, opts
};
3172 /* Test printing of TYPE_CODE_FLAGS values. */
3175 test_print_flags (gdbarch
*arch
)
3177 type
*flags_type
= arch_flags_type (arch
, "test_type", 32);
3178 type
*field_type
= builtin_type (arch
)->builtin_uint32
;
3181 Fields: CCCB BAAA */
3182 append_flags_type_field (flags_type
, 0, 3, field_type
, "A");
3183 append_flags_type_field (flags_type
, 3, 2, field_type
, "B");
3184 append_flags_type_field (flags_type
, 5, 3, field_type
, "C");
3186 value
*val
= value::allocate (flags_type
);
3187 gdb_byte
*contents
= val
->contents_writeable ().data ();
3188 store_unsigned_integer (contents
, 4, gdbarch_byte_order (arch
), 0xaa);
3191 val_print_type_code_flags (flags_type
, val
, 0, &out
);
3192 SELF_CHECK (out
.string () == "[ A=2 B=1 C=5 ]");
3197 INIT_GDB_FILE (valprint
)
3200 selftests::register_test_foreach_arch ("print-flags", test_print_flags
);
3203 set_show_commands setshow_print_cmds
3204 = add_setshow_prefix_cmd ("print", no_class
,
3205 _("Generic command for setting how things print."),
3206 _("Generic command for showing print settings."),
3207 &setprintlist
, &showprintlist
,
3208 &setlist
, &showlist
);
3209 add_alias_cmd ("p", setshow_print_cmds
.set
, no_class
, 1, &setlist
);
3210 /* Prefer set print to set prompt. */
3211 add_alias_cmd ("pr", setshow_print_cmds
.set
, no_class
, 1, &setlist
);
3212 add_alias_cmd ("p", setshow_print_cmds
.show
, no_class
, 1, &showlist
);
3213 add_alias_cmd ("pr", setshow_print_cmds
.show
, no_class
, 1, &showlist
);
3215 set_show_commands setshow_print_raw_cmds
3216 = add_setshow_prefix_cmd
3218 _("Generic command for setting what things to print in \"raw\" mode."),
3219 _("Generic command for showing \"print raw\" settings."),
3220 &setprintrawlist
, &showprintrawlist
, &setprintlist
, &showprintlist
);
3221 deprecate_cmd (setshow_print_raw_cmds
.set
, nullptr);
3222 deprecate_cmd (setshow_print_raw_cmds
.show
, nullptr);
3224 gdb::option::add_setshow_cmds_for_options
3225 (class_support
, &user_print_options
, value_print_option_defs
,
3226 &setprintlist
, &showprintlist
);
3228 add_setshow_zuinteger_cmd ("input-radix", class_support
, &input_radix_1
,
3230 Set default input radix for entering numbers."), _("\
3231 Show default input radix for entering numbers."), NULL
,
3234 &setlist
, &showlist
);
3236 add_setshow_zuinteger_cmd ("output-radix", class_support
, &output_radix_1
,
3238 Set default output radix for printing of values."), _("\
3239 Show default output radix for printing of values."), NULL
,
3242 &setlist
, &showlist
);
3244 /* The "set radix" and "show radix" commands are special in that
3245 they are like normal set and show commands but allow two normally
3246 independent variables to be either set or shown with a single
3247 command. So the usual deprecated_add_set_cmd() and [deleted]
3248 add_show_from_set() commands aren't really appropriate. */
3249 /* FIXME: i18n: With the new add_setshow_integer command, that is no
3250 longer true - show can display anything. */
3251 add_cmd ("radix", class_support
, set_radix
, _("\
3252 Set default input and output number radices.\n\
3253 Use 'set input-radix' or 'set output-radix' to independently set each.\n\
3254 Without an argument, sets both radices back to the default value of 10."),
3256 add_cmd ("radix", class_support
, show_radix
, _("\
3257 Show the default input and output number radices.\n\
3258 Use 'show input-radix' or 'show output-radix' to independently show each."),