1 /* Print values for GDB, the GNU debugger.
3 Copyright (C) 1986-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
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_value_print helper for TYPE_CODE_PTR. */
506 generic_value_print_ptr (struct value
*val
, struct ui_file
*stream
,
507 const struct value_print_options
*options
)
510 if (options
->format
&& options
->format
!= 's')
511 value_print_scalar_formatted (val
, options
, 0, stream
);
514 struct type
*type
= check_typedef (val
->type ());
515 struct type
*elttype
= check_typedef (type
->target_type ());
516 const gdb_byte
*valaddr
= val
->contents_for_printing ().data ();
517 CORE_ADDR addr
= unpack_pointer (type
, valaddr
);
519 print_unpacked_pointer (type
, elttype
, addr
, stream
, options
);
524 /* Print '@' followed by the address contained in ADDRESS_BUFFER. */
527 print_ref_address (struct type
*type
, const gdb_byte
*address_buffer
,
528 int embedded_offset
, struct ui_file
*stream
)
530 struct gdbarch
*gdbarch
= type
->arch ();
532 if (address_buffer
!= NULL
)
535 = extract_typed_address (address_buffer
+ embedded_offset
, type
);
537 gdb_printf (stream
, "@");
538 gdb_puts (paddress (gdbarch
, address
), stream
);
540 /* Else: we have a non-addressable value, such as a DW_AT_const_value. */
543 /* If VAL is addressable, return the value contents buffer of a value that
544 represents a pointer to VAL. Otherwise return NULL. */
546 static const gdb_byte
*
547 get_value_addr_contents (struct value
*deref_val
)
549 gdb_assert (deref_val
!= NULL
);
551 if (deref_val
->lval () == lval_memory
)
552 return value_addr (deref_val
)->contents_for_printing ().data ();
555 /* We have a non-addressable value, such as a DW_AT_const_value. */
560 /* generic_val_print helper for TYPE_CODE_{RVALUE_,}REF. */
563 generic_val_print_ref (struct type
*type
,
564 int embedded_offset
, struct ui_file
*stream
, int recurse
,
565 struct value
*original_value
,
566 const struct value_print_options
*options
)
568 struct type
*elttype
= check_typedef (type
->target_type ());
569 struct value
*deref_val
= NULL
;
570 const bool value_is_synthetic
571 = original_value
->bits_synthetic_pointer (TARGET_CHAR_BIT
* embedded_offset
,
572 TARGET_CHAR_BIT
* type
->length ());
573 const int must_coerce_ref
= ((options
->addressprint
&& value_is_synthetic
)
574 || options
->deref_ref
);
575 const int type_is_defined
= elttype
->code () != TYPE_CODE_UNDEF
;
576 const gdb_byte
*valaddr
= original_value
->contents_for_printing ().data ();
578 if (must_coerce_ref
&& type_is_defined
)
580 deref_val
= coerce_ref_if_computed (original_value
);
582 if (deref_val
!= NULL
)
584 /* More complicated computed references are not supported. */
585 gdb_assert (embedded_offset
== 0);
588 deref_val
= value_at (type
->target_type (),
589 unpack_pointer (type
, valaddr
+ embedded_offset
));
591 /* Else, original_value isn't a synthetic reference or we don't have to print
592 the reference's contents.
594 Notice that for references to TYPE_CODE_STRUCT, 'set print object on' will
595 cause original_value to be a not_lval instead of an lval_computed,
596 which will make value_bits_synthetic_pointer return false.
597 This happens because if options->objectprint is true, c_value_print will
598 overwrite original_value's contents with the result of coercing
599 the reference through value_addr, and then set its type back to
600 TYPE_CODE_REF. In that case we don't have to coerce the reference again;
601 we can simply treat it as non-synthetic and move on. */
603 if (options
->addressprint
)
605 const gdb_byte
*address
= (value_is_synthetic
&& type_is_defined
606 ? get_value_addr_contents (deref_val
)
609 print_ref_address (type
, address
, embedded_offset
, stream
);
611 if (options
->deref_ref
)
612 gdb_puts (": ", stream
);
615 if (options
->deref_ref
)
618 common_val_print (deref_val
, stream
, recurse
, options
,
621 gdb_puts ("???", stream
);
625 /* Helper function for generic_val_print_enum.
626 This is also used to print enums in TYPE_CODE_FLAGS values. */
629 generic_val_print_enum_1 (struct type
*type
, LONGEST val
,
630 struct ui_file
*stream
)
635 len
= type
->num_fields ();
636 for (i
= 0; i
< len
; i
++)
639 if (val
== type
->field (i
).loc_enumval ())
646 fputs_styled (type
->field (i
).name (), variable_name_style
.style (),
649 else if (type
->is_flag_enum ())
653 /* We have a "flag" enum, so we try to decompose it into pieces as
654 appropriate. The enum may have multiple enumerators representing
655 the same bit, in which case we choose to only print the first one
657 for (i
= 0; i
< len
; ++i
)
661 ULONGEST enumval
= type
->field (i
).loc_enumval ();
662 int nbits
= count_one_bits_ll (enumval
);
664 gdb_assert (nbits
== 0 || nbits
== 1);
666 if ((val
& enumval
) != 0)
670 gdb_puts ("(", stream
);
674 gdb_puts (" | ", stream
);
676 val
&= ~type
->field (i
).loc_enumval ();
677 fputs_styled (type
->field (i
).name (),
678 variable_name_style
.style (), stream
);
684 /* There are leftover bits, print them. */
686 gdb_puts ("(", stream
);
688 gdb_puts (" | ", stream
);
690 gdb_puts ("unknown: 0x", stream
);
691 print_longest (stream
, 'x', 0, val
);
692 gdb_puts (")", stream
);
696 /* Nothing has been printed and the value is 0, the enum value must
698 gdb_puts ("0", stream
);
702 /* Something has been printed, close the parenthesis. */
703 gdb_puts (")", stream
);
707 print_longest (stream
, 'd', 0, val
);
710 /* generic_val_print helper for TYPE_CODE_ENUM. */
713 generic_val_print_enum (struct type
*type
,
714 int embedded_offset
, struct ui_file
*stream
,
715 struct value
*original_value
,
716 const struct value_print_options
*options
)
719 struct gdbarch
*gdbarch
= type
->arch ();
720 int unit_size
= gdbarch_addressable_memory_unit_size (gdbarch
);
722 gdb_assert (!options
->format
);
724 const gdb_byte
*valaddr
= original_value
->contents_for_printing ().data ();
726 val
= unpack_long (type
, valaddr
+ embedded_offset
* unit_size
);
728 generic_val_print_enum_1 (type
, val
, stream
);
731 /* generic_val_print helper for TYPE_CODE_FUNC and TYPE_CODE_METHOD. */
734 generic_val_print_func (struct type
*type
,
735 int embedded_offset
, CORE_ADDR address
,
736 struct ui_file
*stream
,
737 struct value
*original_value
,
738 const struct value_print_options
*options
)
740 struct gdbarch
*gdbarch
= type
->arch ();
742 gdb_assert (!options
->format
);
744 /* FIXME, we should consider, at least for ANSI C language,
745 eliminating the distinction made between FUNCs and POINTERs to
747 gdb_printf (stream
, "{");
748 type_print (type
, "", stream
, -1);
749 gdb_printf (stream
, "} ");
750 /* Try to print what function it points to, and its address. */
751 print_address_demangle (options
, gdbarch
, address
, stream
, demangle
);
754 /* generic_value_print helper for TYPE_CODE_BOOL. */
757 generic_value_print_bool
758 (struct value
*value
, struct ui_file
*stream
,
759 const struct value_print_options
*options
,
760 const struct generic_val_print_decorations
*decorations
)
762 if (options
->format
|| options
->output_format
)
764 struct value_print_options opts
= *options
;
765 opts
.format
= (options
->format
? options
->format
766 : options
->output_format
);
767 value_print_scalar_formatted (value
, &opts
, 0, stream
);
771 const gdb_byte
*valaddr
= value
->contents_for_printing ().data ();
772 struct type
*type
= check_typedef (value
->type ());
773 LONGEST val
= unpack_long (type
, valaddr
);
775 gdb_puts (decorations
->false_name
, stream
);
777 gdb_puts (decorations
->true_name
, stream
);
779 print_longest (stream
, 'd', 0, val
);
783 /* generic_value_print helper for TYPE_CODE_INT. */
786 generic_value_print_int (struct value
*val
, struct ui_file
*stream
,
787 const struct value_print_options
*options
)
789 struct value_print_options opts
= *options
;
791 opts
.format
= (options
->format
? options
->format
792 : options
->output_format
);
793 value_print_scalar_formatted (val
, &opts
, 0, stream
);
796 /* generic_value_print helper for TYPE_CODE_CHAR. */
799 generic_value_print_char (struct value
*value
, struct ui_file
*stream
,
800 const struct value_print_options
*options
)
802 if (options
->format
|| options
->output_format
)
804 struct value_print_options opts
= *options
;
806 opts
.format
= (options
->format
? options
->format
807 : options
->output_format
);
808 value_print_scalar_formatted (value
, &opts
, 0, stream
);
812 struct type
*unresolved_type
= value
->type ();
813 struct type
*type
= check_typedef (unresolved_type
);
814 const gdb_byte
*valaddr
= value
->contents_for_printing ().data ();
816 LONGEST val
= unpack_long (type
, valaddr
);
817 if (type
->is_unsigned ())
818 gdb_printf (stream
, "%u", (unsigned int) val
);
820 gdb_printf (stream
, "%d", (int) val
);
821 gdb_puts (" ", stream
);
822 current_language
->printchar (val
, unresolved_type
, stream
);
826 /* generic_val_print helper for TYPE_CODE_FLT and TYPE_CODE_DECFLOAT. */
829 generic_val_print_float (struct type
*type
, struct ui_file
*stream
,
830 struct value
*original_value
,
831 const struct value_print_options
*options
)
833 gdb_assert (!options
->format
);
835 const gdb_byte
*valaddr
= original_value
->contents_for_printing ().data ();
837 print_floating (valaddr
, type
, stream
);
840 /* generic_val_print helper for TYPE_CODE_FIXED_POINT. */
843 generic_val_print_fixed_point (struct value
*val
, struct ui_file
*stream
,
844 const struct value_print_options
*options
)
847 value_print_scalar_formatted (val
, options
, 0, stream
);
850 struct type
*type
= val
->type ();
852 const gdb_byte
*valaddr
= val
->contents_for_printing ().data ();
855 f
.read_fixed_point (gdb::make_array_view (valaddr
, type
->length ()),
856 type_byte_order (type
), type
->is_unsigned (),
857 type
->fixed_point_scaling_factor ());
859 const char *fmt
= type
->length () < 4 ? "%.11Fg" : "%.17Fg";
860 std::string str
= f
.str (fmt
);
861 gdb_printf (stream
, "%s", str
.c_str ());
865 /* generic_value_print helper for TYPE_CODE_COMPLEX. */
868 generic_value_print_complex (struct value
*val
, struct ui_file
*stream
,
869 const struct value_print_options
*options
,
870 const struct generic_val_print_decorations
873 gdb_printf (stream
, "%s", decorations
->complex_prefix
);
875 struct value
*real_part
= value_real_part (val
);
876 value_print_scalar_formatted (real_part
, options
, 0, stream
);
877 gdb_printf (stream
, "%s", decorations
->complex_infix
);
879 struct value
*imag_part
= value_imaginary_part (val
);
880 value_print_scalar_formatted (imag_part
, options
, 0, stream
);
881 gdb_printf (stream
, "%s", decorations
->complex_suffix
);
884 /* generic_value_print helper for TYPE_CODE_MEMBERPTR. */
887 generic_value_print_memberptr
888 (struct value
*val
, struct ui_file
*stream
,
890 const struct value_print_options
*options
,
891 const struct generic_val_print_decorations
*decorations
)
893 if (!options
->format
)
895 /* Member pointers are essentially specific to C++, and so if we
896 encounter one, we should print it according to C++ rules. */
897 struct type
*type
= check_typedef (val
->type ());
898 const gdb_byte
*valaddr
= val
->contents_for_printing ().data ();
899 cp_print_class_member (valaddr
, type
, stream
, "&");
902 value_print_scalar_formatted (val
, options
, 0, stream
);
905 /* See valprint.h. */
908 generic_value_print (struct value
*val
, struct ui_file
*stream
, int recurse
,
909 const struct value_print_options
*options
,
910 const struct generic_val_print_decorations
*decorations
)
912 struct type
*type
= val
->type ();
914 type
= check_typedef (type
);
916 if (is_fixed_point_type (type
))
917 type
= type
->fixed_point_type_base_type ();
919 /* Widen a subrange to its target type, then use that type's
921 while (type
->code () == TYPE_CODE_RANGE
)
923 type
= check_typedef (type
->target_type ());
924 val
= value_cast (type
, val
);
927 switch (type
->code ())
929 case TYPE_CODE_ARRAY
:
930 generic_val_print_array (val
, stream
, recurse
, options
, decorations
);
933 case TYPE_CODE_MEMBERPTR
:
934 generic_value_print_memberptr (val
, stream
, recurse
, options
,
939 generic_value_print_ptr (val
, stream
, options
);
943 case TYPE_CODE_RVALUE_REF
:
944 generic_val_print_ref (type
, 0, stream
, recurse
,
950 value_print_scalar_formatted (val
, options
, 0, stream
);
952 generic_val_print_enum (type
, 0, stream
, val
, options
);
955 case TYPE_CODE_FLAGS
:
957 value_print_scalar_formatted (val
, options
, 0, stream
);
959 val_print_type_code_flags (type
, val
, 0, stream
);
963 case TYPE_CODE_METHOD
:
965 value_print_scalar_formatted (val
, options
, 0, stream
);
967 generic_val_print_func (type
, 0, val
->address (), stream
,
972 generic_value_print_bool (val
, stream
, options
, decorations
);
976 generic_value_print_int (val
, stream
, options
);
980 generic_value_print_char (val
, stream
, options
);
984 case TYPE_CODE_DECFLOAT
:
986 value_print_scalar_formatted (val
, options
, 0, stream
);
988 generic_val_print_float (type
, stream
, val
, options
);
991 case TYPE_CODE_FIXED_POINT
:
992 generic_val_print_fixed_point (val
, stream
, options
);
996 gdb_puts (decorations
->void_name
, stream
);
999 case TYPE_CODE_ERROR
:
1000 gdb_printf (stream
, "%s", TYPE_ERROR_NAME (type
));
1003 case TYPE_CODE_UNDEF
:
1004 /* This happens (without TYPE_STUB set) on systems which don't use
1005 dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
1006 and no complete type for struct foo in that file. */
1007 fprintf_styled (stream
, metadata_style
.style (), _("<incomplete type>"));
1010 case TYPE_CODE_COMPLEX
:
1011 generic_value_print_complex (val
, stream
, options
, decorations
);
1014 case TYPE_CODE_METHODPTR
:
1015 cplus_print_method_ptr (val
->contents_for_printing ().data (), type
,
1019 case TYPE_CODE_UNION
:
1020 case TYPE_CODE_STRUCT
:
1022 error (_("Unhandled type code %d in symbol table."),
1027 /* Print using the given LANGUAGE the value VAL onto stream STREAM according
1030 This is a preferable interface to val_print, above, because it uses
1031 GDB's value mechanism. */
1034 common_val_print (struct value
*value
, struct ui_file
*stream
, int recurse
,
1035 const struct value_print_options
*options
,
1036 const struct language_defn
*language
)
1038 if (language
->la_language
== language_ada
)
1039 /* The value might have a dynamic type, which would cause trouble
1040 below when trying to extract the value contents (since the value
1041 size is determined from the type size which is unknown). So
1042 get a fixed representation of our value. */
1043 value
= ada_to_fixed_value (value
);
1046 value
->fetch_lazy ();
1048 struct value_print_options local_opts
= *options
;
1049 struct type
*type
= value
->type ();
1050 struct type
*real_type
= check_typedef (type
);
1052 if (local_opts
.prettyformat
== Val_prettyformat_default
)
1053 local_opts
.prettyformat
= (local_opts
.prettyformat_structs
1054 ? Val_prettyformat
: Val_no_prettyformat
);
1058 if (!valprint_check_validity (stream
, real_type
, 0, value
))
1063 if (apply_ext_lang_val_pretty_printer (value
, stream
, recurse
, options
,
1068 /* Ensure that the type is complete and not just a stub. If the type is
1069 only a stub and we can't find and substitute its complete type, then
1070 print appropriate string and return. */
1072 if (real_type
->is_stub ())
1074 fprintf_styled (stream
, metadata_style
.style (), _("<incomplete type>"));
1078 /* Handle summary mode. If the value is a scalar, print it;
1079 otherwise, print an ellipsis. */
1080 if (options
->summary
&& !val_print_scalar_type_p (type
))
1082 gdb_printf (stream
, "...");
1086 /* If this value is too deep then don't print it. */
1087 if (!val_print_scalar_or_string_type_p (type
, language
)
1088 && val_print_check_max_depth (stream
, recurse
, options
, language
))
1093 language
->value_print_inner (value
, stream
, recurse
, &local_opts
);
1095 catch (const gdb_exception_error
&except
)
1097 fprintf_styled (stream
, metadata_style
.style (),
1098 _("<error reading variable: %s>"), except
.what ());
1102 /* See valprint.h. */
1105 val_print_check_max_depth (struct ui_file
*stream
, int recurse
,
1106 const struct value_print_options
*options
,
1107 const struct language_defn
*language
)
1109 if (options
->max_depth
> -1 && recurse
>= options
->max_depth
)
1111 gdb_assert (language
->struct_too_deep_ellipsis () != NULL
);
1112 gdb_puts (language
->struct_too_deep_ellipsis (), stream
);
1119 /* Check whether the value VAL is printable. Return 1 if it is;
1120 return 0 and print an appropriate error message to STREAM according to
1121 OPTIONS if it is not. */
1124 value_check_printable (struct value
*val
, struct ui_file
*stream
,
1125 const struct value_print_options
*options
)
1129 fprintf_styled (stream
, metadata_style
.style (),
1130 _("<address of value unknown>"));
1134 if (val
->entirely_optimized_out ())
1136 if (options
->summary
&& !val_print_scalar_type_p (val
->type ()))
1137 gdb_printf (stream
, "...");
1139 val_print_optimized_out (val
, stream
);
1143 if (val
->entirely_unavailable ())
1145 if (options
->summary
&& !val_print_scalar_type_p (val
->type ()))
1146 gdb_printf (stream
, "...");
1148 val_print_unavailable (stream
);
1152 if (val
->type ()->code () == TYPE_CODE_INTERNAL_FUNCTION
)
1154 fprintf_styled (stream
, metadata_style
.style (),
1155 _("<internal function %s>"),
1156 value_internal_function_name (val
));
1160 if (type_not_allocated (val
->type ()))
1162 val_print_not_allocated (stream
);
1169 /* See valprint.h. */
1172 common_val_print_checked (struct value
*val
, struct ui_file
*stream
,
1174 const struct value_print_options
*options
,
1175 const struct language_defn
*language
)
1177 if (!value_check_printable (val
, stream
, options
))
1179 common_val_print (val
, stream
, recurse
, options
, language
);
1182 /* Print on stream STREAM the value VAL according to OPTIONS. The value
1183 is printed using the current_language syntax. */
1186 value_print (struct value
*val
, struct ui_file
*stream
,
1187 const struct value_print_options
*options
)
1189 scoped_value_mark free_values
;
1191 if (!value_check_printable (val
, stream
, options
))
1197 = apply_ext_lang_val_pretty_printer (val
, stream
, 0, options
,
1204 current_language
->value_print (val
, stream
, options
);
1207 /* Meant to be used in debug sessions, so don't export it in a header file. */
1208 extern void ATTRIBUTE_UNUSED
debug_val (struct value
*val
);
1212 void ATTRIBUTE_UNUSED
1213 debug_val (struct value
*val
)
1215 value_print (val
, gdb_stdlog
, &user_print_options
);
1216 gdb_flush (gdb_stdlog
);
1220 val_print_type_code_flags (struct type
*type
, struct value
*original_value
,
1221 int embedded_offset
, struct ui_file
*stream
)
1223 const gdb_byte
*valaddr
= (original_value
->contents_for_printing ().data ()
1225 ULONGEST val
= unpack_long (type
, valaddr
);
1226 int field
, nfields
= type
->num_fields ();
1227 struct gdbarch
*gdbarch
= type
->arch ();
1228 struct type
*bool_type
= builtin_type (gdbarch
)->builtin_bool
;
1230 gdb_puts ("[", stream
);
1231 for (field
= 0; field
< nfields
; field
++)
1233 if (type
->field (field
).name ()[0] != '\0')
1235 struct type
*field_type
= type
->field (field
).type ();
1237 if (field_type
== bool_type
1238 /* We require boolean types here to be one bit wide. This is a
1239 problematic place to notify the user of an internal error
1240 though. Instead just fall through and print the field as an
1242 && type
->field (field
).bitsize () == 1)
1244 if (val
& ((ULONGEST
)1 << type
->field (field
).loc_bitpos ()))
1247 styled_string (variable_name_style
.style (),
1248 type
->field (field
).name ()));
1252 unsigned field_len
= type
->field (field
).bitsize ();
1253 ULONGEST field_val
= val
>> type
->field (field
).loc_bitpos ();
1255 if (field_len
< sizeof (ULONGEST
) * TARGET_CHAR_BIT
)
1256 field_val
&= ((ULONGEST
) 1 << field_len
) - 1;
1257 gdb_printf (stream
, " %ps=",
1258 styled_string (variable_name_style
.style (),
1259 type
->field (field
).name ()));
1260 if (field_type
->code () == TYPE_CODE_ENUM
)
1261 generic_val_print_enum_1 (field_type
, field_val
, stream
);
1263 print_longest (stream
, 'd', 0, field_val
);
1267 gdb_puts (" ]", stream
);
1270 /* See valprint.h. */
1273 value_print_scalar_formatted (struct value
*val
,
1274 const struct value_print_options
*options
,
1276 struct ui_file
*stream
)
1278 struct type
*type
= check_typedef (val
->type ());
1280 gdb_assert (val
!= NULL
);
1282 /* If we get here with a string format, try again without it. Go
1283 all the way back to the language printers, which may call us
1285 if (options
->format
== 's')
1287 struct value_print_options opts
= *options
;
1289 opts
.deref_ref
= false;
1290 common_val_print (val
, stream
, 0, &opts
, current_language
);
1294 /* value_contents_for_printing fetches all VAL's contents. They are
1295 needed to check whether VAL is optimized-out or unavailable
1297 const gdb_byte
*valaddr
= val
->contents_for_printing ().data ();
1299 /* A scalar object that does not have all bits available can't be
1300 printed, because all bits contribute to its representation. */
1301 if (val
->bits_any_optimized_out (0,
1302 TARGET_CHAR_BIT
* type
->length ()))
1303 val_print_optimized_out (val
, stream
);
1304 else if (!val
->bytes_available (0, type
->length ()))
1305 val_print_unavailable (stream
);
1307 print_scalar_formatted (valaddr
, type
, options
, size
, stream
);
1310 /* Print a number according to FORMAT which is one of d,u,x,o,b,h,w,g.
1311 The raison d'etre of this function is to consolidate printing of
1312 LONG_LONG's into this one function. The format chars b,h,w,g are
1313 from print_scalar_formatted(). Numbers are printed using C
1316 USE_C_FORMAT means to use C format in all cases. Without it,
1317 'o' and 'x' format do not include the standard C radix prefix
1320 Hilfinger/2004-09-09: USE_C_FORMAT was originally called USE_LOCAL
1321 and was intended to request formatting according to the current
1322 language and would be used for most integers that GDB prints. The
1323 exceptional cases were things like protocols where the format of
1324 the integer is a protocol thing, not a user-visible thing). The
1325 parameter remains to preserve the information of what things might
1326 be printed with language-specific format, should we ever resurrect
1330 print_longest (struct ui_file
*stream
, int format
, int use_c_format
,
1338 val
= int_string (val_long
, 10, 1, 0, 1); break;
1340 val
= int_string (val_long
, 10, 0, 0, 1); break;
1342 val
= int_string (val_long
, 16, 0, 0, use_c_format
); break;
1344 val
= int_string (val_long
, 16, 0, 2, 1); break;
1346 val
= int_string (val_long
, 16, 0, 4, 1); break;
1348 val
= int_string (val_long
, 16, 0, 8, 1); break;
1350 val
= int_string (val_long
, 16, 0, 16, 1); break;
1353 val
= int_string (val_long
, 8, 0, 0, use_c_format
); break;
1355 internal_error (_("failed internal consistency check"));
1357 gdb_puts (val
, stream
);
1360 /* This used to be a macro, but I don't think it is called often enough
1361 to merit such treatment. */
1362 /* Convert a LONGEST to an int. This is used in contexts (e.g. number of
1363 arguments to a function, number in a value history, register number, etc.)
1364 where the value must not be larger than can fit in an int. */
1367 longest_to_int (LONGEST arg
)
1369 /* Let the compiler do the work. */
1370 int rtnval
= (int) arg
;
1372 /* Check for overflows or underflows. */
1373 if (sizeof (LONGEST
) > sizeof (int))
1377 error (_("Value out of range."));
1383 /* Print a floating point value of floating-point type TYPE,
1384 pointed to in GDB by VALADDR, on STREAM. */
1387 print_floating (const gdb_byte
*valaddr
, struct type
*type
,
1388 struct ui_file
*stream
)
1390 std::string str
= target_float_to_string (valaddr
, type
);
1391 gdb_puts (str
.c_str (), stream
);
1395 print_binary_chars (struct ui_file
*stream
, const gdb_byte
*valaddr
,
1396 unsigned len
, enum bfd_endian byte_order
, bool zero_pad
,
1397 const struct value_print_options
*options
)
1402 bool seen_a_one
= false;
1403 const char *digit_separator
= nullptr;
1405 /* Declared "int" so it will be signed.
1406 This ensures that right shift will shift in zeros. */
1408 const int mask
= 0x080;
1410 if (options
->nibblesprint
)
1411 digit_separator
= current_language
->get_digit_separator();
1413 if (byte_order
== BFD_ENDIAN_BIG
)
1419 /* Every byte has 8 binary characters; peel off
1420 and print from the MSB end. */
1422 for (i
= 0; i
< (HOST_CHAR_BIT
* sizeof (*p
)); i
++)
1424 if (options
->nibblesprint
&& seen_a_one
&& i
% 4 == 0)
1425 gdb_putc (*digit_separator
, stream
);
1427 if (*p
& (mask
>> i
))
1432 if (zero_pad
|| seen_a_one
|| b
== '1')
1433 gdb_putc (b
, stream
);
1434 else if (options
->nibblesprint
)
1436 if ((0xf0 & (mask
>> i
) && (*p
& 0xf0))
1437 || (0x0f & (mask
>> i
) && (*p
& 0x0f)))
1438 gdb_putc (b
, stream
);
1448 for (p
= valaddr
+ len
- 1;
1452 for (i
= 0; i
< (HOST_CHAR_BIT
* sizeof (*p
)); i
++)
1454 if (options
->nibblesprint
&& seen_a_one
&& i
% 4 == 0)
1455 gdb_putc (*digit_separator
, stream
);
1457 if (*p
& (mask
>> i
))
1462 if (zero_pad
|| seen_a_one
|| b
== '1')
1463 gdb_putc (b
, stream
);
1464 else if (options
->nibblesprint
)
1466 if ((0xf0 & (mask
>> i
) && (*p
& 0xf0))
1467 || (0x0f & (mask
>> i
) && (*p
& 0x0f)))
1468 gdb_putc (b
, stream
);
1477 /* When not zero-padding, ensure that something is printed when the
1479 if (!zero_pad
&& !seen_a_one
)
1480 gdb_putc ('0', stream
);
1483 /* A helper for print_octal_chars that emits a single octal digit,
1484 optionally suppressing it if is zero and updating SEEN_A_ONE. */
1487 emit_octal_digit (struct ui_file
*stream
, bool *seen_a_one
, int digit
)
1489 if (*seen_a_one
|| digit
!= 0)
1490 gdb_printf (stream
, "%o", digit
);
1495 /* VALADDR points to an integer of LEN bytes.
1496 Print it in octal on stream or format it in buf. */
1499 print_octal_chars (struct ui_file
*stream
, const gdb_byte
*valaddr
,
1500 unsigned len
, enum bfd_endian byte_order
)
1503 unsigned char octa1
, octa2
, octa3
, carry
;
1506 /* Octal is 3 bits, which doesn't fit. Yuk. So we have to track
1507 * the extra bits, which cycle every three bytes:
1509 * Byte side: 0 1 2 3
1511 * bit number 123 456 78 | 9 012 345 6 | 78 901 234 | 567 890 12 |
1513 * Octal side: 0 1 carry 3 4 carry ...
1515 * Cycle number: 0 1 2
1517 * But of course we are printing from the high side, so we have to
1518 * figure out where in the cycle we are so that we end up with no
1519 * left over bits at the end.
1521 #define BITS_IN_OCTAL 3
1522 #define HIGH_ZERO 0340
1523 #define LOW_ZERO 0034
1524 #define CARRY_ZERO 0003
1525 static_assert (HIGH_ZERO
+ LOW_ZERO
+ CARRY_ZERO
== 0xff,
1526 "cycle zero constants are wrong");
1527 #define HIGH_ONE 0200
1528 #define MID_ONE 0160
1529 #define LOW_ONE 0016
1530 #define CARRY_ONE 0001
1531 static_assert (HIGH_ONE
+ MID_ONE
+ LOW_ONE
+ CARRY_ONE
== 0xff,
1532 "cycle one constants are wrong");
1533 #define HIGH_TWO 0300
1534 #define MID_TWO 0070
1535 #define LOW_TWO 0007
1536 static_assert (HIGH_TWO
+ MID_TWO
+ LOW_TWO
== 0xff,
1537 "cycle two constants are wrong");
1539 /* For 32 we start in cycle 2, with two bits and one bit carry;
1540 for 64 in cycle in cycle 1, with one bit and a two bit carry. */
1542 cycle
= (len
* HOST_CHAR_BIT
) % BITS_IN_OCTAL
;
1545 gdb_puts ("0", stream
);
1546 bool seen_a_one
= false;
1547 if (byte_order
== BFD_ENDIAN_BIG
)
1556 /* No carry in, carry out two bits. */
1558 octa1
= (HIGH_ZERO
& *p
) >> 5;
1559 octa2
= (LOW_ZERO
& *p
) >> 2;
1560 carry
= (CARRY_ZERO
& *p
);
1561 emit_octal_digit (stream
, &seen_a_one
, octa1
);
1562 emit_octal_digit (stream
, &seen_a_one
, octa2
);
1566 /* Carry in two bits, carry out one bit. */
1568 octa1
= (carry
<< 1) | ((HIGH_ONE
& *p
) >> 7);
1569 octa2
= (MID_ONE
& *p
) >> 4;
1570 octa3
= (LOW_ONE
& *p
) >> 1;
1571 carry
= (CARRY_ONE
& *p
);
1572 emit_octal_digit (stream
, &seen_a_one
, octa1
);
1573 emit_octal_digit (stream
, &seen_a_one
, octa2
);
1574 emit_octal_digit (stream
, &seen_a_one
, octa3
);
1578 /* Carry in one bit, no carry out. */
1580 octa1
= (carry
<< 2) | ((HIGH_TWO
& *p
) >> 6);
1581 octa2
= (MID_TWO
& *p
) >> 3;
1582 octa3
= (LOW_TWO
& *p
);
1584 emit_octal_digit (stream
, &seen_a_one
, octa1
);
1585 emit_octal_digit (stream
, &seen_a_one
, octa2
);
1586 emit_octal_digit (stream
, &seen_a_one
, octa3
);
1590 error (_("Internal error in octal conversion;"));
1594 cycle
= cycle
% BITS_IN_OCTAL
;
1599 for (p
= valaddr
+ len
- 1;
1606 /* Carry out, no carry in */
1608 octa1
= (HIGH_ZERO
& *p
) >> 5;
1609 octa2
= (LOW_ZERO
& *p
) >> 2;
1610 carry
= (CARRY_ZERO
& *p
);
1611 emit_octal_digit (stream
, &seen_a_one
, octa1
);
1612 emit_octal_digit (stream
, &seen_a_one
, octa2
);
1616 /* Carry in, carry out */
1618 octa1
= (carry
<< 1) | ((HIGH_ONE
& *p
) >> 7);
1619 octa2
= (MID_ONE
& *p
) >> 4;
1620 octa3
= (LOW_ONE
& *p
) >> 1;
1621 carry
= (CARRY_ONE
& *p
);
1622 emit_octal_digit (stream
, &seen_a_one
, octa1
);
1623 emit_octal_digit (stream
, &seen_a_one
, octa2
);
1624 emit_octal_digit (stream
, &seen_a_one
, octa3
);
1628 /* Carry in, no carry out */
1630 octa1
= (carry
<< 2) | ((HIGH_TWO
& *p
) >> 6);
1631 octa2
= (MID_TWO
& *p
) >> 3;
1632 octa3
= (LOW_TWO
& *p
);
1634 emit_octal_digit (stream
, &seen_a_one
, octa1
);
1635 emit_octal_digit (stream
, &seen_a_one
, octa2
);
1636 emit_octal_digit (stream
, &seen_a_one
, octa3
);
1640 error (_("Internal error in octal conversion;"));
1644 cycle
= cycle
% BITS_IN_OCTAL
;
1650 /* Possibly negate the integer represented by BYTES. It contains LEN
1651 bytes in the specified byte order. If the integer is negative,
1652 copy it into OUT_VEC, negate it, and return true. Otherwise, do
1653 nothing and return false. */
1656 maybe_negate_by_bytes (const gdb_byte
*bytes
, unsigned len
,
1657 enum bfd_endian byte_order
,
1658 gdb::byte_vector
*out_vec
)
1661 gdb_assert (len
> 0);
1662 if (byte_order
== BFD_ENDIAN_BIG
)
1663 sign_byte
= bytes
[0];
1665 sign_byte
= bytes
[len
- 1];
1666 if ((sign_byte
& 0x80) == 0)
1669 out_vec
->resize (len
);
1671 /* Compute -x == 1 + ~x. */
1672 if (byte_order
== BFD_ENDIAN_LITTLE
)
1675 for (unsigned i
= 0; i
< len
; ++i
)
1677 unsigned tem
= (0xff & ~bytes
[i
]) + carry
;
1678 (*out_vec
)[i
] = tem
& 0xff;
1685 for (unsigned i
= len
; i
> 0; --i
)
1687 unsigned tem
= (0xff & ~bytes
[i
- 1]) + carry
;
1688 (*out_vec
)[i
- 1] = tem
& 0xff;
1696 /* VALADDR points to an integer of LEN bytes.
1697 Print it in decimal on stream or format it in buf. */
1700 print_decimal_chars (struct ui_file
*stream
, const gdb_byte
*valaddr
,
1701 unsigned len
, bool is_signed
,
1702 enum bfd_endian byte_order
)
1705 #define CARRY_OUT( x ) ((x) / TEN) /* extend char to int */
1706 #define CARRY_LEFT( x ) ((x) % TEN)
1707 #define SHIFT( x ) ((x) << 4)
1708 #define LOW_NIBBLE( x ) ( (x) & 0x00F)
1709 #define HIGH_NIBBLE( x ) (((x) & 0x0F0) >> 4)
1714 int i
, j
, decimal_digits
;
1718 gdb::byte_vector negated_bytes
;
1720 && maybe_negate_by_bytes (valaddr
, len
, byte_order
, &negated_bytes
))
1722 gdb_puts ("-", stream
);
1723 valaddr
= negated_bytes
.data ();
1726 /* Base-ten number is less than twice as many digits
1727 as the base 16 number, which is 2 digits per byte. */
1729 decimal_len
= len
* 2 * 2;
1730 std::vector
<unsigned char> digits (decimal_len
, 0);
1732 /* Ok, we have an unknown number of bytes of data to be printed in
1735 * Given a hex number (in nibbles) as XYZ, we start by taking X and
1736 * decimalizing it as "x1 x2" in two decimal nibbles. Then we multiply
1737 * the nibbles by 16, add Y and re-decimalize. Repeat with Z.
1739 * The trick is that "digits" holds a base-10 number, but sometimes
1740 * the individual digits are > 10.
1742 * Outer loop is per nibble (hex digit) of input, from MSD end to
1745 decimal_digits
= 0; /* Number of decimal digits so far */
1746 p
= (byte_order
== BFD_ENDIAN_BIG
) ? valaddr
: valaddr
+ len
- 1;
1748 while ((byte_order
== BFD_ENDIAN_BIG
) ? (p
< valaddr
+ len
) : (p
>= valaddr
))
1751 * Multiply current base-ten number by 16 in place.
1752 * Each digit was between 0 and 9, now is between
1755 for (j
= 0; j
< decimal_digits
; j
++)
1757 digits
[j
] = SHIFT (digits
[j
]);
1760 /* Take the next nibble off the input and add it to what
1761 * we've got in the LSB position. Bottom 'digit' is now
1762 * between 0 and 159.
1764 * "flip" is used to run this loop twice for each byte.
1768 /* Take top nibble. */
1770 digits
[0] += HIGH_NIBBLE (*p
);
1775 /* Take low nibble and bump our pointer "p". */
1777 digits
[0] += LOW_NIBBLE (*p
);
1778 if (byte_order
== BFD_ENDIAN_BIG
)
1785 /* Re-decimalize. We have to do this often enough
1786 * that we don't overflow, but once per nibble is
1787 * overkill. Easier this way, though. Note that the
1788 * carry is often larger than 10 (e.g. max initial
1789 * carry out of lowest nibble is 15, could bubble all
1790 * the way up greater than 10). So we have to do
1791 * the carrying beyond the last current digit.
1794 for (j
= 0; j
< decimal_len
- 1; j
++)
1798 /* "/" won't handle an unsigned char with
1799 * a value that if signed would be negative.
1800 * So extend to longword int via "dummy".
1803 carry
= CARRY_OUT (dummy
);
1804 digits
[j
] = CARRY_LEFT (dummy
);
1806 if (j
>= decimal_digits
&& carry
== 0)
1809 * All higher digits are 0 and we
1810 * no longer have a carry.
1812 * Note: "j" is 0-based, "decimal_digits" is
1815 decimal_digits
= j
+ 1;
1821 /* Ok, now "digits" is the decimal representation, with
1822 the "decimal_digits" actual digits. Print! */
1824 for (i
= decimal_digits
- 1; i
> 0 && digits
[i
] == 0; --i
)
1829 gdb_printf (stream
, "%1d", digits
[i
]);
1833 /* VALADDR points to an integer of LEN bytes. Print it in hex on stream. */
1836 print_hex_chars (struct ui_file
*stream
, const gdb_byte
*valaddr
,
1837 unsigned len
, enum bfd_endian byte_order
,
1842 gdb_puts ("0x", stream
);
1843 if (byte_order
== BFD_ENDIAN_BIG
)
1849 /* Strip leading 0 bytes, but be sure to leave at least a
1850 single byte at the end. */
1851 for (; p
< valaddr
+ len
- 1 && !*p
; ++p
)
1855 const gdb_byte
*first
= p
;
1860 /* When not zero-padding, use a different format for the
1861 very first byte printed. */
1862 if (!zero_pad
&& p
== first
)
1863 gdb_printf (stream
, "%x", *p
);
1865 gdb_printf (stream
, "%02x", *p
);
1870 p
= valaddr
+ len
- 1;
1874 /* Strip leading 0 bytes, but be sure to leave at least a
1875 single byte at the end. */
1876 for (; p
>= valaddr
+ 1 && !*p
; --p
)
1880 const gdb_byte
*first
= p
;
1885 /* When not zero-padding, use a different format for the
1886 very first byte printed. */
1887 if (!zero_pad
&& p
== first
)
1888 gdb_printf (stream
, "%x", *p
);
1890 gdb_printf (stream
, "%02x", *p
);
1895 /* Print function pointer with inferior address ADDRESS onto stdio
1899 print_function_pointer_address (const struct value_print_options
*options
,
1900 struct gdbarch
*gdbarch
,
1902 struct ui_file
*stream
)
1904 CORE_ADDR func_addr
= gdbarch_convert_from_func_ptr_addr
1905 (gdbarch
, address
, current_inferior ()->top_target ());
1907 /* If the function pointer is represented by a description, print
1908 the address of the description. */
1909 if (options
->addressprint
&& func_addr
!= address
)
1911 gdb_puts ("@", stream
);
1912 gdb_puts (paddress (gdbarch
, address
), stream
);
1913 gdb_puts (": ", stream
);
1915 print_address_demangle (options
, gdbarch
, func_addr
, stream
, demangle
);
1919 /* Print on STREAM using the given OPTIONS the index for the element
1920 at INDEX of an array whose index type is INDEX_TYPE. */
1923 maybe_print_array_index (struct type
*index_type
, LONGEST index
,
1924 struct ui_file
*stream
,
1925 const struct value_print_options
*options
)
1927 if (!options
->print_array_indexes
)
1930 current_language
->print_array_index (index_type
, index
, stream
, options
);
1933 /* See valprint.h. */
1936 value_print_array_elements (struct value
*val
, struct ui_file
*stream
,
1938 const struct value_print_options
*options
,
1941 unsigned int things_printed
= 0;
1943 struct type
*elttype
, *index_type
;
1944 /* Position of the array element we are examining to see
1945 whether it is repeated. */
1947 /* Number of repetitions we have detected so far. */
1949 LONGEST low_bound
, high_bound
;
1951 struct type
*type
= check_typedef (val
->type ());
1953 elttype
= type
->target_type ();
1954 unsigned bit_stride
= type
->bit_stride ();
1955 if (bit_stride
== 0)
1956 bit_stride
= 8 * check_typedef (elttype
)->length ();
1957 index_type
= type
->index_type ();
1958 if (index_type
->code () == TYPE_CODE_RANGE
)
1959 index_type
= index_type
->target_type ();
1961 if (get_array_bounds (type
, &low_bound
, &high_bound
))
1963 /* The array length should normally be HIGH_BOUND - LOW_BOUND +
1964 1. But we have to be a little extra careful, because some
1965 languages such as Ada allow LOW_BOUND to be greater than
1966 HIGH_BOUND for empty arrays. In that situation, the array
1967 length is just zero, not negative! */
1968 if (low_bound
> high_bound
)
1971 len
= high_bound
- low_bound
+ 1;
1975 warning (_("unable to get bounds of array, assuming null array"));
1980 annotate_array_section_begin (i
, elttype
);
1982 for (; i
< len
&& things_printed
< options
->print_max
; i
++)
1984 scoped_value_mark free_values
;
1988 if (options
->prettyformat_arrays
)
1990 gdb_printf (stream
, ",\n");
1991 print_spaces (2 + 2 * recurse
, stream
);
1994 gdb_printf (stream
, ", ");
1996 else if (options
->prettyformat_arrays
)
1998 gdb_printf (stream
, "\n");
1999 print_spaces (2 + 2 * recurse
, stream
);
2001 stream
->wrap_here (2 + 2 * recurse
);
2002 maybe_print_array_index (index_type
, i
+ low_bound
,
2005 struct value
*element
= val
->from_component_bitsize (elttype
,
2010 /* Only check for reps if repeat_count_threshold is not set to
2011 UINT_MAX (unlimited). */
2012 if (options
->repeat_count_threshold
< UINT_MAX
)
2014 bool unavailable
= element
->entirely_unavailable ();
2015 bool available
= element
->entirely_available ();
2019 /* When printing large arrays this spot is called frequently, so
2020 clean up temporary values asap to prevent allocating a large
2022 scoped_value_mark free_values_inner
;
2023 struct value
*rep_elt
2024 = val
->from_component_bitsize (elttype
,
2027 bool repeated
= ((available
2028 && rep_elt
->entirely_available ()
2029 && element
->contents_eq (rep_elt
))
2031 && rep_elt
->entirely_unavailable ()));
2039 common_val_print (element
, stream
, recurse
+ 1, options
,
2042 if (reps
> options
->repeat_count_threshold
)
2044 annotate_elt_rep (reps
);
2045 gdb_printf (stream
, " %p[<repeats %u times>%p]",
2046 metadata_style
.style ().ptr (), reps
, nullptr);
2047 annotate_elt_rep_end ();
2050 things_printed
+= options
->repeat_count_threshold
;
2058 annotate_array_section_end ();
2060 gdb_printf (stream
, "...");
2061 if (options
->prettyformat_arrays
)
2063 gdb_printf (stream
, "\n");
2064 print_spaces (2 * recurse
, stream
);
2068 /* Return true if print_wchar can display W without resorting to a
2069 numeric escape, false otherwise. */
2072 wchar_printable (gdb_wchar_t w
)
2074 return (gdb_iswprint (w
)
2075 || w
== LCST ('\a') || w
== LCST ('\b')
2076 || w
== LCST ('\f') || w
== LCST ('\n')
2077 || w
== LCST ('\r') || w
== LCST ('\t')
2078 || w
== LCST ('\v'));
2081 /* A helper function that converts the contents of STRING to wide
2082 characters and then appends them to OUTPUT. */
2085 append_string_as_wide (const char *string
,
2086 struct obstack
*output
)
2088 for (; *string
; ++string
)
2090 gdb_wchar_t w
= gdb_btowc (*string
);
2091 obstack_grow (output
, &w
, sizeof (gdb_wchar_t
));
2095 /* Print a wide character W to OUTPUT. ORIG is a pointer to the
2096 original (target) bytes representing the character, ORIG_LEN is the
2097 number of valid bytes. WIDTH is the number of bytes in a base
2098 characters of the type. OUTPUT is an obstack to which wide
2099 characters are emitted. QUOTER is a (narrow) character indicating
2100 the style of quotes surrounding the character to be printed.
2101 NEED_ESCAPE is an in/out flag which is used to track numeric
2102 escapes across calls. */
2105 print_wchar (gdb_wint_t w
, const gdb_byte
*orig
,
2106 int orig_len
, int width
,
2107 enum bfd_endian byte_order
,
2108 struct obstack
*output
,
2109 int quoter
, bool *need_escapep
)
2111 bool need_escape
= *need_escapep
;
2113 *need_escapep
= false;
2115 /* If any additional cases are added to this switch block, then the
2116 function wchar_printable will likely need updating too. */
2120 obstack_grow_wstr (output
, LCST ("\\a"));
2123 obstack_grow_wstr (output
, LCST ("\\b"));
2126 obstack_grow_wstr (output
, LCST ("\\f"));
2129 obstack_grow_wstr (output
, LCST ("\\n"));
2132 obstack_grow_wstr (output
, LCST ("\\r"));
2135 obstack_grow_wstr (output
, LCST ("\\t"));
2138 obstack_grow_wstr (output
, LCST ("\\v"));
2142 if (gdb_iswprint (w
) && !(need_escape
&& gdb_iswxdigit (w
)))
2144 gdb_wchar_t wchar
= w
;
2146 if (w
== gdb_btowc (quoter
) || w
== LCST ('\\'))
2147 obstack_grow_wstr (output
, LCST ("\\"));
2148 obstack_grow (output
, &wchar
, sizeof (gdb_wchar_t
));
2154 for (i
= 0; i
+ width
<= orig_len
; i
+= width
)
2159 value
= extract_unsigned_integer (&orig
[i
], width
,
2161 /* If the value fits in 3 octal digits, print it that
2162 way. Otherwise, print it as a hex escape. */
2165 xsnprintf (octal
, sizeof (octal
), "\\%.3o",
2166 (int) (value
& 0777));
2167 *need_escapep
= false;
2171 xsnprintf (octal
, sizeof (octal
), "\\x%lx", (long) value
);
2172 /* A hex escape might require the next character
2173 to be escaped, because, unlike with octal,
2174 hex escapes have no length limit. */
2175 *need_escapep
= true;
2177 append_string_as_wide (octal
, output
);
2179 /* If we somehow have extra bytes, print them now. */
2180 while (i
< orig_len
)
2184 xsnprintf (octal
, sizeof (octal
), "\\%.3o", orig
[i
] & 0xff);
2185 *need_escapep
= false;
2186 append_string_as_wide (octal
, output
);
2195 /* Print the character C on STREAM as part of the contents of a
2196 literal string whose delimiter is QUOTER. ENCODING names the
2200 generic_emit_char (int c
, struct type
*type
, struct ui_file
*stream
,
2201 int quoter
, const char *encoding
)
2203 enum bfd_endian byte_order
2204 = type_byte_order (type
);
2206 bool need_escape
= false;
2208 c_buf
= (gdb_byte
*) alloca (type
->length ());
2209 pack_long (c_buf
, type
, c
);
2211 wchar_iterator
iter (c_buf
, type
->length (), encoding
, type
->length ());
2213 /* This holds the printable form of the wchar_t data. */
2214 auto_obstack wchar_buf
;
2220 const gdb_byte
*buf
;
2222 int print_escape
= 1;
2223 enum wchar_iterate_result result
;
2225 num_chars
= iter
.iterate (&result
, &chars
, &buf
, &buflen
);
2230 /* If all characters are printable, print them. Otherwise,
2231 we're going to have to print an escape sequence. We
2232 check all characters because we want to print the target
2233 bytes in the escape sequence, and we don't know character
2234 boundaries there. */
2238 for (i
= 0; i
< num_chars
; ++i
)
2239 if (!wchar_printable (chars
[i
]))
2247 for (i
= 0; i
< num_chars
; ++i
)
2248 print_wchar (chars
[i
], buf
, buflen
,
2249 type
->length (), byte_order
,
2250 &wchar_buf
, quoter
, &need_escape
);
2254 /* This handles the NUM_CHARS == 0 case as well. */
2256 print_wchar (gdb_WEOF
, buf
, buflen
, type
->length (),
2257 byte_order
, &wchar_buf
, quoter
, &need_escape
);
2260 /* The output in the host encoding. */
2261 auto_obstack output
;
2263 convert_between_encodings (INTERMEDIATE_ENCODING
, host_charset (),
2264 (gdb_byte
*) obstack_base (&wchar_buf
),
2265 obstack_object_size (&wchar_buf
),
2266 sizeof (gdb_wchar_t
), &output
, translit_char
);
2267 obstack_1grow (&output
, '\0');
2269 gdb_puts ((const char *) obstack_base (&output
), stream
);
2272 /* Return the repeat count of the next character/byte in ITER,
2273 storing the result in VEC. */
2276 count_next_character (wchar_iterator
*iter
,
2277 std::vector
<converted_character
> *vec
)
2279 struct converted_character
*current
;
2283 struct converted_character tmp
;
2287 = iter
->iterate (&tmp
.result
, &chars
, &tmp
.buf
, &tmp
.buflen
);
2288 if (tmp
.num_chars
> 0)
2290 gdb_assert (tmp
.num_chars
< MAX_WCHARS
);
2291 memcpy (tmp
.chars
, chars
, tmp
.num_chars
* sizeof (gdb_wchar_t
));
2293 vec
->push_back (tmp
);
2296 current
= &vec
->back ();
2298 /* Count repeated characters or bytes. */
2299 current
->repeat_count
= 1;
2300 if (current
->num_chars
== -1)
2308 struct converted_character d
;
2315 /* Get the next character. */
2316 d
.num_chars
= iter
->iterate (&d
.result
, &chars
, &d
.buf
, &d
.buflen
);
2318 /* If a character was successfully converted, save the character
2319 into the converted character. */
2320 if (d
.num_chars
> 0)
2322 gdb_assert (d
.num_chars
< MAX_WCHARS
);
2323 memcpy (d
.chars
, chars
, WCHAR_BUFLEN (d
.num_chars
));
2326 /* Determine if the current character is the same as this
2328 if (d
.num_chars
== current
->num_chars
&& d
.result
== current
->result
)
2330 /* There are two cases to consider:
2332 1) Equality of converted character (num_chars > 0)
2333 2) Equality of non-converted character (num_chars == 0) */
2334 if ((current
->num_chars
> 0
2335 && memcmp (current
->chars
, d
.chars
,
2336 WCHAR_BUFLEN (current
->num_chars
)) == 0)
2337 || (current
->num_chars
== 0
2338 && current
->buflen
== d
.buflen
2339 && memcmp (current
->buf
, d
.buf
, current
->buflen
) == 0))
2340 ++current
->repeat_count
;
2348 /* Push this next converted character onto the result vector. */
2349 repeat
= current
->repeat_count
;
2355 /* Print the characters in CHARS to the OBSTACK. QUOTE_CHAR is the quote
2356 character to use with string output. WIDTH is the size of the output
2357 character type. BYTE_ORDER is the target byte order. OPTIONS
2358 is the user's print options. *FINISHED is set to 0 if we didn't print
2359 all the elements in CHARS. */
2362 print_converted_chars_to_obstack (struct obstack
*obstack
,
2363 const std::vector
<converted_character
> &chars
,
2364 int quote_char
, int width
,
2365 enum bfd_endian byte_order
,
2366 const struct value_print_options
*options
,
2369 unsigned int idx
, num_elements
;
2370 const converted_character
*elem
;
2371 enum {START
, SINGLE
, REPEAT
, INCOMPLETE
, FINISH
} state
, last
;
2372 gdb_wchar_t wide_quote_char
= gdb_btowc (quote_char
);
2373 bool need_escape
= false;
2374 const int print_max
= options
->print_max_chars
> 0
2375 ? options
->print_max_chars
: options
->print_max
;
2377 /* Set the start state. */
2378 idx
= num_elements
= 0;
2379 last
= state
= START
;
2387 /* Nothing to do. */
2394 /* We are outputting a single character
2395 (< options->repeat_count_threshold). */
2399 /* We were outputting some other type of content, so we
2400 must output and a comma and a quote. */
2402 obstack_grow_wstr (obstack
, LCST (", "));
2403 obstack_grow (obstack
, &wide_quote_char
, sizeof (gdb_wchar_t
));
2405 /* Output the character. */
2406 int repeat_count
= elem
->repeat_count
;
2407 if (print_max
< repeat_count
+ num_elements
)
2409 repeat_count
= print_max
- num_elements
;
2412 for (j
= 0; j
< repeat_count
; ++j
)
2414 if (elem
->result
== wchar_iterate_ok
)
2415 print_wchar (elem
->chars
[0], elem
->buf
, elem
->buflen
, width
,
2416 byte_order
, obstack
, quote_char
, &need_escape
);
2418 print_wchar (gdb_WEOF
, elem
->buf
, elem
->buflen
, width
,
2419 byte_order
, obstack
, quote_char
, &need_escape
);
2429 /* We are outputting a character with a repeat count
2430 greater than options->repeat_count_threshold. */
2434 /* We were outputting a single string. Terminate the
2436 obstack_grow (obstack
, &wide_quote_char
, sizeof (gdb_wchar_t
));
2439 obstack_grow_wstr (obstack
, LCST (", "));
2441 /* Output the character and repeat string. */
2442 obstack_grow_wstr (obstack
, LCST ("'"));
2443 if (elem
->result
== wchar_iterate_ok
)
2444 print_wchar (elem
->chars
[0], elem
->buf
, elem
->buflen
, width
,
2445 byte_order
, obstack
, quote_char
, &need_escape
);
2447 print_wchar (gdb_WEOF
, elem
->buf
, elem
->buflen
, width
,
2448 byte_order
, obstack
, quote_char
, &need_escape
);
2449 obstack_grow_wstr (obstack
, LCST ("'"));
2450 std::string s
= string_printf (_(" <repeats %u times>"),
2451 elem
->repeat_count
);
2452 num_elements
+= elem
->repeat_count
;
2453 for (j
= 0; s
[j
]; ++j
)
2455 gdb_wchar_t w
= gdb_btowc (s
[j
]);
2456 obstack_grow (obstack
, &w
, sizeof (gdb_wchar_t
));
2462 /* We are outputting an incomplete sequence. */
2465 /* If we were outputting a string of SINGLE characters,
2466 terminate the quote. */
2467 obstack_grow (obstack
, &wide_quote_char
, sizeof (gdb_wchar_t
));
2470 obstack_grow_wstr (obstack
, LCST (", "));
2472 /* Output the incomplete sequence string. */
2473 obstack_grow_wstr (obstack
, LCST ("<incomplete sequence "));
2474 print_wchar (gdb_WEOF
, elem
->buf
, elem
->buflen
, width
, byte_order
,
2475 obstack
, 0, &need_escape
);
2476 obstack_grow_wstr (obstack
, LCST (">"));
2479 /* We do not attempt to output anything after this. */
2484 /* All done. If we were outputting a string of SINGLE
2485 characters, the string must be terminated. Otherwise,
2486 REPEAT and INCOMPLETE are always left properly terminated. */
2488 obstack_grow (obstack
, &wide_quote_char
, sizeof (gdb_wchar_t
));
2493 /* Get the next element and state. */
2495 if (state
!= FINISH
)
2497 elem
= &chars
[idx
++];
2498 switch (elem
->result
)
2500 case wchar_iterate_ok
:
2501 case wchar_iterate_invalid
:
2502 if (elem
->repeat_count
> options
->repeat_count_threshold
)
2508 case wchar_iterate_incomplete
:
2512 case wchar_iterate_eof
:
2520 /* Print the character string STRING, printing at most LENGTH
2521 characters. LENGTH is -1 if the string is nul terminated. TYPE is
2522 the type of each character. OPTIONS holds the printing options;
2523 printing stops early if the number hits print_max_chars; repeat
2524 counts are printed as appropriate. Print ellipses at the end if we
2525 had to stop before printing LENGTH characters, or if FORCE_ELLIPSES.
2526 QUOTE_CHAR is the character to print at each end of the string. If
2527 C_STYLE_TERMINATOR is true, and the last character is 0, then it is
2531 generic_printstr (struct ui_file
*stream
, struct type
*type
,
2532 const gdb_byte
*string
, unsigned int length
,
2533 const char *encoding
, int force_ellipses
,
2534 int quote_char
, int c_style_terminator
,
2535 const struct value_print_options
*options
)
2537 enum bfd_endian byte_order
= type_byte_order (type
);
2539 int width
= type
->length ();
2541 struct converted_character
*last
;
2545 unsigned long current_char
= 1;
2547 for (i
= 0; current_char
; ++i
)
2550 current_char
= extract_unsigned_integer (string
+ i
* width
,
2556 /* If the string was not truncated due to `set print elements', and
2557 the last byte of it is a null, we don't print that, in
2558 traditional C style. */
2559 if (c_style_terminator
2562 && (extract_unsigned_integer (string
+ (length
- 1) * width
,
2563 width
, byte_order
) == 0))
2568 gdb_printf (stream
, "%c%c", quote_char
, quote_char
);
2572 /* Arrange to iterate over the characters, in wchar_t form. */
2573 wchar_iterator
iter (string
, length
* width
, encoding
, width
);
2574 std::vector
<converted_character
> converted_chars
;
2576 /* Convert characters until the string is over or the maximum
2577 number of printed characters has been reached. */
2579 unsigned int print_max_chars
= get_print_max_chars (options
);
2580 while (i
< print_max_chars
)
2586 /* Grab the next character and repeat count. */
2587 r
= count_next_character (&iter
, &converted_chars
);
2589 /* If less than zero, the end of the input string was reached. */
2593 /* Otherwise, add the count to the total print count and get
2594 the next character. */
2598 /* Get the last element and determine if the entire string was
2600 last
= &converted_chars
.back ();
2601 finished
= (last
->result
== wchar_iterate_eof
);
2603 /* Ensure that CONVERTED_CHARS is terminated. */
2604 last
->result
= wchar_iterate_eof
;
2606 /* WCHAR_BUF is the obstack we use to represent the string in
2608 auto_obstack wchar_buf
;
2610 /* Print the output string to the obstack. */
2611 print_converted_chars_to_obstack (&wchar_buf
, converted_chars
, quote_char
,
2612 width
, byte_order
, options
, &finished
);
2614 if (force_ellipses
|| !finished
)
2615 obstack_grow_wstr (&wchar_buf
, LCST ("..."));
2617 /* OUTPUT is where we collect `char's for printing. */
2618 auto_obstack output
;
2620 convert_between_encodings (INTERMEDIATE_ENCODING
, host_charset (),
2621 (gdb_byte
*) obstack_base (&wchar_buf
),
2622 obstack_object_size (&wchar_buf
),
2623 sizeof (gdb_wchar_t
), &output
, translit_char
);
2624 obstack_1grow (&output
, '\0');
2626 gdb_puts ((const char *) obstack_base (&output
), stream
);
2629 /* Print a string from the inferior, starting at ADDR and printing up to LEN
2630 characters, of WIDTH bytes a piece, to STREAM. If LEN is -1, printing
2631 stops at the first null byte, otherwise printing proceeds (including null
2632 bytes) until either print_max_chars or LEN characters have been printed,
2633 whichever is smaller. ENCODING is the name of the string's
2634 encoding. It can be NULL, in which case the target encoding is
2638 val_print_string (struct type
*elttype
, const char *encoding
,
2639 CORE_ADDR addr
, int len
,
2640 struct ui_file
*stream
,
2641 const struct value_print_options
*options
)
2643 int force_ellipsis
= 0; /* Force ellipsis to be printed if nonzero. */
2644 int err
; /* Non-zero if we got a bad read. */
2645 int found_nul
; /* Non-zero if we found the nul char. */
2646 unsigned int fetchlimit
; /* Maximum number of chars to print. */
2648 gdb::unique_xmalloc_ptr
<gdb_byte
> buffer
; /* Dynamically growable fetch buffer. */
2649 struct gdbarch
*gdbarch
= elttype
->arch ();
2650 enum bfd_endian byte_order
= type_byte_order (elttype
);
2651 int width
= elttype
->length ();
2653 /* First we need to figure out the limit on the number of characters we are
2654 going to attempt to fetch and print. This is actually pretty simple.
2655 If LEN >= zero, then the limit is the minimum of LEN and print_max_chars.
2656 If LEN is -1, then the limit is print_max_chars. This is true regardless
2657 of whether print_max_chars is zero, UINT_MAX (unlimited), or something in
2658 between, because finding the null byte (or available memory) is what
2659 actually limits the fetch. */
2661 unsigned int print_max_chars
= get_print_max_chars (options
);
2662 fetchlimit
= (len
== -1
2664 : std::min ((unsigned) len
, print_max_chars
));
2666 err
= target_read_string (addr
, len
, width
, fetchlimit
,
2667 &buffer
, &bytes_read
);
2671 /* We now have either successfully filled the buffer to fetchlimit,
2672 or terminated early due to an error or finding a null char when
2675 /* Determine found_nul by looking at the last character read. */
2677 if (bytes_read
>= width
)
2678 found_nul
= extract_unsigned_integer (buffer
.get () + bytes_read
- width
,
2679 width
, byte_order
) == 0;
2680 if (len
== -1 && !found_nul
)
2684 /* We didn't find a NUL terminator we were looking for. Attempt
2685 to peek at the next character. If not successful, or it is not
2686 a null byte, then force ellipsis to be printed. */
2688 peekbuf
= (gdb_byte
*) alloca (width
);
2690 if (target_read_memory (addr
, peekbuf
, width
) == 0
2691 && extract_unsigned_integer (peekbuf
, width
, byte_order
) != 0)
2694 else if ((len
>= 0 && err
!= 0) || (len
> bytes_read
/ width
))
2696 /* Getting an error when we have a requested length, or fetching less
2697 than the number of characters actually requested, always make us
2702 /* If we get an error before fetching anything, don't print a string.
2703 But if we fetch something and then get an error, print the string
2704 and then the error message. */
2705 if (err
== 0 || bytes_read
> 0)
2706 current_language
->printstr (stream
, elttype
, buffer
.get (),
2708 encoding
, force_ellipsis
, options
);
2712 std::string str
= memory_error_message (TARGET_XFER_E_IO
, gdbarch
, addr
);
2714 gdb_printf (stream
, _("<error: %ps>"),
2715 styled_string (metadata_style
.style (),
2719 return (bytes_read
/ width
);
2722 /* Handle 'show print max-depth'. */
2725 show_print_max_depth (struct ui_file
*file
, int from_tty
,
2726 struct cmd_list_element
*c
, const char *value
)
2728 gdb_printf (file
, _("Maximum print depth is %s.\n"), value
);
2732 /* The 'set input-radix' command writes to this auxiliary variable.
2733 If the requested radix is valid, INPUT_RADIX is updated; otherwise,
2734 it is left unchanged. */
2736 static unsigned input_radix_1
= 10;
2738 /* Validate an input or output radix setting, and make sure the user
2739 knows what they really did here. Radix setting is confusing, e.g.
2740 setting the input radix to "10" never changes it! */
2743 set_input_radix (const char *args
, int from_tty
, struct cmd_list_element
*c
)
2745 set_input_radix_1 (from_tty
, input_radix_1
);
2749 set_input_radix_1 (int from_tty
, unsigned radix
)
2751 /* We don't currently disallow any input radix except 0 or 1, which don't
2752 make any mathematical sense. In theory, we can deal with any input
2753 radix greater than 1, even if we don't have unique digits for every
2754 value from 0 to radix-1, but in practice we lose on large radix values.
2755 We should either fix the lossage or restrict the radix range more.
2760 input_radix_1
= input_radix
;
2761 error (_("Nonsense input radix ``decimal %u''; input radix unchanged."),
2764 input_radix_1
= input_radix
= radix
;
2767 gdb_printf (_("Input radix now set to "
2768 "decimal %u, hex %x, octal %o.\n"),
2769 radix
, radix
, radix
);
2773 /* The 'set output-radix' command writes to this auxiliary variable.
2774 If the requested radix is valid, OUTPUT_RADIX is updated,
2775 otherwise, it is left unchanged. */
2777 static unsigned output_radix_1
= 10;
2780 set_output_radix (const char *args
, int from_tty
, struct cmd_list_element
*c
)
2782 set_output_radix_1 (from_tty
, output_radix_1
);
2786 set_output_radix_1 (int from_tty
, unsigned radix
)
2788 /* Validate the radix and disallow ones that we aren't prepared to
2789 handle correctly, leaving the radix unchanged. */
2793 user_print_options
.output_format
= 'x'; /* hex */
2796 user_print_options
.output_format
= 0; /* decimal */
2799 user_print_options
.output_format
= 'o'; /* octal */
2802 output_radix_1
= output_radix
;
2803 error (_("Unsupported output radix ``decimal %u''; "
2804 "output radix unchanged."),
2807 output_radix_1
= output_radix
= radix
;
2810 gdb_printf (_("Output radix now set to "
2811 "decimal %u, hex %x, octal %o.\n"),
2812 radix
, radix
, radix
);
2816 /* Set both the input and output radix at once. Try to set the output radix
2817 first, since it has the most restrictive range. An radix that is valid as
2818 an output radix is also valid as an input radix.
2820 It may be useful to have an unusual input radix. If the user wishes to
2821 set an input radix that is not valid as an output radix, he needs to use
2822 the 'set input-radix' command. */
2825 set_radix (const char *arg
, int from_tty
)
2829 radix
= (arg
== NULL
) ? 10 : parse_and_eval_long (arg
);
2830 set_output_radix_1 (0, radix
);
2831 set_input_radix_1 (0, radix
);
2834 gdb_printf (_("Input and output radices now set to "
2835 "decimal %u, hex %x, octal %o.\n"),
2836 radix
, radix
, radix
);
2840 /* Show both the input and output radices. */
2843 show_radix (const char *arg
, int from_tty
)
2847 if (input_radix
== output_radix
)
2849 gdb_printf (_("Input and output radices set to "
2850 "decimal %u, hex %x, octal %o.\n"),
2851 input_radix
, input_radix
, input_radix
);
2855 gdb_printf (_("Input radix set to decimal "
2856 "%u, hex %x, octal %o.\n"),
2857 input_radix
, input_radix
, input_radix
);
2858 gdb_printf (_("Output radix set to decimal "
2859 "%u, hex %x, octal %o.\n"),
2860 output_radix
, output_radix
, output_radix
);
2866 /* Controls printing of vtbl's. */
2868 show_vtblprint (struct ui_file
*file
, int from_tty
,
2869 struct cmd_list_element
*c
, const char *value
)
2871 gdb_printf (file
, _("\
2872 Printing of C++ virtual function tables is %s.\n"),
2876 /* Controls looking up an object's derived type using what we find in
2879 show_objectprint (struct ui_file
*file
, int from_tty
,
2880 struct cmd_list_element
*c
,
2883 gdb_printf (file
, _("\
2884 Printing of object's derived type based on vtable info is %s.\n"),
2889 show_static_field_print (struct ui_file
*file
, int from_tty
,
2890 struct cmd_list_element
*c
,
2894 _("Printing of C++ static members is %s.\n"),
2900 /* A couple typedefs to make writing the options a bit more
2902 using boolean_option_def
2903 = gdb::option::boolean_option_def
<value_print_options
>;
2904 using uinteger_option_def
2905 = gdb::option::uinteger_option_def
<value_print_options
>;
2906 using pinteger_option_def
2907 = gdb::option::pinteger_option_def
<value_print_options
>;
2909 /* Extra literals supported with the `set print characters' and
2910 `print -characters' commands. */
2911 static const literal_def print_characters_literals
[] =
2913 { "elements", PRINT_MAX_CHARS_ELEMENTS
},
2914 { "unlimited", PRINT_MAX_CHARS_UNLIMITED
, 0 },
2918 /* Definitions of options for the "print" and "compile print"
2920 static const gdb::option::option_def value_print_option_defs
[] = {
2922 boolean_option_def
{
2924 [] (value_print_options
*opt
) { return &opt
->addressprint
; },
2925 show_addressprint
, /* show_cmd_cb */
2926 N_("Set printing of addresses."),
2927 N_("Show printing of addresses."),
2928 NULL
, /* help_doc */
2931 boolean_option_def
{
2933 [] (value_print_options
*opt
) { return &opt
->prettyformat_arrays
; },
2934 show_prettyformat_arrays
, /* show_cmd_cb */
2935 N_("Set pretty formatting of arrays."),
2936 N_("Show pretty formatting of arrays."),
2937 NULL
, /* help_doc */
2940 boolean_option_def
{
2942 [] (value_print_options
*opt
) { return &opt
->print_array_indexes
; },
2943 show_print_array_indexes
, /* show_cmd_cb */
2944 N_("Set printing of array indexes."),
2945 N_("Show printing of array indexes."),
2946 NULL
, /* help_doc */
2949 boolean_option_def
{
2951 [] (value_print_options
*opt
) { return &opt
->nibblesprint
; },
2952 show_nibbles
, /* show_cmd_cb */
2953 N_("Set whether to print binary values in groups of four bits."),
2954 N_("Show whether to print binary values in groups of four bits."),
2955 NULL
, /* help_doc */
2958 uinteger_option_def
{
2960 [] (value_print_options
*opt
) { return &opt
->print_max_chars
; },
2961 print_characters_literals
,
2962 show_print_max_chars
, /* show_cmd_cb */
2963 N_("Set limit on string chars to print."),
2964 N_("Show limit on string chars to print."),
2965 N_("\"elements\" causes the array element limit to be used.\n"
2966 "\"unlimited\" causes there to be no limit."),
2969 uinteger_option_def
{
2971 [] (value_print_options
*opt
) { return &opt
->print_max
; },
2972 uinteger_unlimited_literals
,
2973 show_print_max
, /* show_cmd_cb */
2974 N_("Set limit on array elements to print."),
2975 N_("Show limit on array elements to print."),
2976 N_("\"unlimited\" causes there to be no limit.\n"
2977 "This setting also applies to string chars when \"print characters\"\n"
2978 "is set to \"elements\"."),
2981 pinteger_option_def
{
2983 [] (value_print_options
*opt
) { return &opt
->max_depth
; },
2984 pinteger_unlimited_literals
,
2985 show_print_max_depth
, /* show_cmd_cb */
2986 N_("Set maximum print depth for nested structures, unions and arrays."),
2987 N_("Show maximum print depth for nested structures, unions, and arrays."),
2988 N_("When structures, unions, or arrays are nested beyond this depth then they\n\
2989 will be replaced with either '{...}' or '(...)' depending on the language.\n\
2990 Use \"unlimited\" to print the complete structure.")
2993 boolean_option_def
{
2994 "memory-tag-violations",
2995 [] (value_print_options
*opt
) { return &opt
->memory_tag_violations
; },
2996 show_memory_tag_violations
, /* show_cmd_cb */
2997 N_("Set printing of memory tag violations for pointers."),
2998 N_("Show printing of memory tag violations for pointers."),
2999 N_("Issue a warning when the printed value is a pointer\n\
3000 whose logical tag doesn't match the allocation tag of the memory\n\
3001 location it points to."),
3004 boolean_option_def
{
3006 [] (value_print_options
*opt
) { return &opt
->stop_print_at_null
; },
3007 show_stop_print_at_null
, /* show_cmd_cb */
3008 N_("Set printing of char arrays to stop at first null char."),
3009 N_("Show printing of char arrays to stop at first null char."),
3010 NULL
, /* help_doc */
3013 boolean_option_def
{
3015 [] (value_print_options
*opt
) { return &opt
->objectprint
; },
3016 show_objectprint
, /* show_cmd_cb */
3017 _("Set printing of C++ virtual function tables."),
3018 _("Show printing of C++ virtual function tables."),
3019 NULL
, /* help_doc */
3022 boolean_option_def
{
3024 [] (value_print_options
*opt
) { return &opt
->prettyformat_structs
; },
3025 show_prettyformat_structs
, /* show_cmd_cb */
3026 N_("Set pretty formatting of structures."),
3027 N_("Show pretty formatting of structures."),
3028 NULL
, /* help_doc */
3031 boolean_option_def
{
3033 [] (value_print_options
*opt
) { return &opt
->raw
; },
3034 NULL
, /* show_cmd_cb */
3035 N_("Set whether to print values in raw form."),
3036 N_("Show whether to print values in raw form."),
3037 N_("If set, values are printed in raw form, bypassing any\n\
3038 pretty-printers for that value.")
3041 uinteger_option_def
{
3043 [] (value_print_options
*opt
) { return &opt
->repeat_count_threshold
; },
3044 uinteger_unlimited_literals
,
3045 show_repeat_count_threshold
, /* show_cmd_cb */
3046 N_("Set threshold for repeated print elements."),
3047 N_("Show threshold for repeated print elements."),
3048 N_("\"unlimited\" causes all elements to be individually printed."),
3051 boolean_option_def
{
3053 [] (value_print_options
*opt
) { return &opt
->static_field_print
; },
3054 show_static_field_print
, /* show_cmd_cb */
3055 N_("Set printing of C++ static members."),
3056 N_("Show printing of C++ static members."),
3057 NULL
, /* help_doc */
3060 boolean_option_def
{
3062 [] (value_print_options
*opt
) { return &opt
->symbol_print
; },
3063 show_symbol_print
, /* show_cmd_cb */
3064 N_("Set printing of symbol names when printing pointers."),
3065 N_("Show printing of symbol names when printing pointers."),
3066 NULL
, /* help_doc */
3069 boolean_option_def
{
3071 [] (value_print_options
*opt
) { return &opt
->unionprint
; },
3072 show_unionprint
, /* show_cmd_cb */
3073 N_("Set printing of unions interior to structures."),
3074 N_("Show printing of unions interior to structures."),
3075 NULL
, /* help_doc */
3078 boolean_option_def
{
3080 [] (value_print_options
*opt
) { return &opt
->vtblprint
; },
3081 show_vtblprint
, /* show_cmd_cb */
3082 N_("Set printing of C++ virtual function tables."),
3083 N_("Show printing of C++ virtual function tables."),
3084 NULL
, /* help_doc */
3088 /* See valprint.h. */
3090 gdb::option::option_def_group
3091 make_value_print_options_def_group (value_print_options
*opts
)
3093 return {{value_print_option_defs
}, opts
};
3098 /* Test printing of TYPE_CODE_FLAGS values. */
3101 test_print_flags (gdbarch
*arch
)
3103 type
*flags_type
= arch_flags_type (arch
, "test_type", 32);
3104 type
*field_type
= builtin_type (arch
)->builtin_uint32
;
3107 Fields: CCCB BAAA */
3108 append_flags_type_field (flags_type
, 0, 3, field_type
, "A");
3109 append_flags_type_field (flags_type
, 3, 2, field_type
, "B");
3110 append_flags_type_field (flags_type
, 5, 3, field_type
, "C");
3112 value
*val
= value::allocate (flags_type
);
3113 gdb_byte
*contents
= val
->contents_writeable ().data ();
3114 store_unsigned_integer (contents
, 4, gdbarch_byte_order (arch
), 0xaa);
3117 val_print_type_code_flags (flags_type
, val
, 0, &out
);
3118 SELF_CHECK (out
.string () == "[ A=2 B=1 C=5 ]");
3123 void _initialize_valprint ();
3125 _initialize_valprint ()
3128 selftests::register_test_foreach_arch ("print-flags", test_print_flags
);
3131 set_show_commands setshow_print_cmds
3132 = add_setshow_prefix_cmd ("print", no_class
,
3133 _("Generic command for setting how things print."),
3134 _("Generic command for showing print settings."),
3135 &setprintlist
, &showprintlist
,
3136 &setlist
, &showlist
);
3137 add_alias_cmd ("p", setshow_print_cmds
.set
, no_class
, 1, &setlist
);
3138 /* Prefer set print to set prompt. */
3139 add_alias_cmd ("pr", setshow_print_cmds
.set
, no_class
, 1, &setlist
);
3140 add_alias_cmd ("p", setshow_print_cmds
.show
, no_class
, 1, &showlist
);
3141 add_alias_cmd ("pr", setshow_print_cmds
.show
, no_class
, 1, &showlist
);
3143 set_show_commands setshow_print_raw_cmds
3144 = add_setshow_prefix_cmd
3146 _("Generic command for setting what things to print in \"raw\" mode."),
3147 _("Generic command for showing \"print raw\" settings."),
3148 &setprintrawlist
, &showprintrawlist
, &setprintlist
, &showprintlist
);
3149 deprecate_cmd (setshow_print_raw_cmds
.set
, nullptr);
3150 deprecate_cmd (setshow_print_raw_cmds
.show
, nullptr);
3152 gdb::option::add_setshow_cmds_for_options
3153 (class_support
, &user_print_options
, value_print_option_defs
,
3154 &setprintlist
, &showprintlist
);
3156 add_setshow_zuinteger_cmd ("input-radix", class_support
, &input_radix_1
,
3158 Set default input radix for entering numbers."), _("\
3159 Show default input radix for entering numbers."), NULL
,
3162 &setlist
, &showlist
);
3164 add_setshow_zuinteger_cmd ("output-radix", class_support
, &output_radix_1
,
3166 Set default output radix for printing of values."), _("\
3167 Show default output radix for printing of values."), NULL
,
3170 &setlist
, &showlist
);
3172 /* The "set radix" and "show radix" commands are special in that
3173 they are like normal set and show commands but allow two normally
3174 independent variables to be either set or shown with a single
3175 command. So the usual deprecated_add_set_cmd() and [deleted]
3176 add_show_from_set() commands aren't really appropriate. */
3177 /* FIXME: i18n: With the new add_setshow_integer command, that is no
3178 longer true - show can display anything. */
3179 add_cmd ("radix", class_support
, set_radix
, _("\
3180 Set default input and output number radices.\n\
3181 Use 'set input-radix' or 'set output-radix' to independently set each.\n\
3182 Without an argument, sets both radices back to the default value of 10."),
3184 add_cmd ("radix", class_support
, show_radix
, _("\
3185 Show the default input and output number radices.\n\
3186 Use 'show input-radix' or 'show output-radix' to independently show each."),