1 /* Print values for GDB, the GNU debugger.
3 Copyright (C) 1986-2021 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
30 #include "target-float.h"
31 #include "extension.h"
33 #include "gdb_obstack.h"
35 #include "typeprint.h"
38 #include "gdbsupport/byte-vector.h"
39 #include "cli/cli-option.h"
41 #include "cli/cli-style.h"
42 #include "count-one-bits.h"
46 /* Maximum number of wchars returned from wchar_iterate. */
49 /* A convenience macro to compute the size of a wchar_t buffer containing X
51 #define WCHAR_BUFLEN(X) ((X) * sizeof (gdb_wchar_t))
53 /* Character buffer size saved while iterating over wchars. */
54 #define WCHAR_BUFLEN_MAX WCHAR_BUFLEN (MAX_WCHARS)
56 /* A structure to encapsulate state information from iterated
57 character conversions. */
58 struct converted_character
60 /* The number of characters converted. */
63 /* The result of the conversion. See charset.h for more. */
64 enum wchar_iterate_result result
;
66 /* The (saved) converted character(s). */
67 gdb_wchar_t chars
[WCHAR_BUFLEN_MAX
];
69 /* The first converted target byte. */
72 /* The number of bytes converted. */
75 /* How many times this character(s) is repeated. */
79 /* Command lists for set/show print raw. */
80 struct cmd_list_element
*setprintrawlist
;
81 struct cmd_list_element
*showprintrawlist
;
83 /* Prototypes for local functions */
85 static int partial_memory_read (CORE_ADDR memaddr
, gdb_byte
*myaddr
,
86 int len
, int *errptr
);
88 static void set_input_radix_1 (int, unsigned);
90 static void set_output_radix_1 (int, unsigned);
92 static void val_print_type_code_flags (struct type
*type
,
93 struct value
*original_value
,
95 struct ui_file
*stream
);
97 #define PRINT_MAX_DEFAULT 200 /* Start print_max off at this value. */
98 #define PRINT_MAX_DEPTH_DEFAULT 20 /* Start print_max_depth off at this value. */
100 struct value_print_options user_print_options
=
102 Val_prettyformat_default
, /* prettyformat */
103 0, /* prettyformat_arrays */
104 0, /* prettyformat_structs */
107 1, /* addressprint */
109 PRINT_MAX_DEFAULT
, /* print_max */
110 10, /* repeat_count_threshold */
111 0, /* output_format */
113 1, /* memory_tag_violations */
114 0, /* stop_print_at_null */
115 0, /* print_array_indexes */
117 1, /* static_field_print */
118 1, /* pascal_static_field_print */
121 1, /* symbol_print */
122 PRINT_MAX_DEPTH_DEFAULT
, /* max_depth */
126 /* Initialize *OPTS to be a copy of the user print options. */
128 get_user_print_options (struct value_print_options
*opts
)
130 *opts
= user_print_options
;
133 /* Initialize *OPTS to be a copy of the user print options, but with
134 pretty-formatting disabled. */
136 get_no_prettyformat_print_options (struct value_print_options
*opts
)
138 *opts
= user_print_options
;
139 opts
->prettyformat
= Val_no_prettyformat
;
142 /* Initialize *OPTS to be a copy of the user print options, but using
143 FORMAT as the formatting option. */
145 get_formatted_print_options (struct value_print_options
*opts
,
148 *opts
= user_print_options
;
149 opts
->format
= format
;
153 show_print_max (struct ui_file
*file
, int from_tty
,
154 struct cmd_list_element
*c
, const char *value
)
156 fprintf_filtered (file
,
157 _("Limit on string chars or array "
158 "elements to print is %s.\n"),
163 /* Default input and output radixes, and output format letter. */
165 unsigned input_radix
= 10;
167 show_input_radix (struct ui_file
*file
, int from_tty
,
168 struct cmd_list_element
*c
, const char *value
)
170 fprintf_filtered (file
,
171 _("Default input radix for entering numbers is %s.\n"),
175 unsigned output_radix
= 10;
177 show_output_radix (struct ui_file
*file
, int from_tty
,
178 struct cmd_list_element
*c
, const char *value
)
180 fprintf_filtered (file
,
181 _("Default output radix for printing of values is %s.\n"),
185 /* By default we print arrays without printing the index of each element in
186 the array. This behavior can be changed by setting PRINT_ARRAY_INDEXES. */
189 show_print_array_indexes (struct ui_file
*file
, int from_tty
,
190 struct cmd_list_element
*c
, const char *value
)
192 fprintf_filtered (file
, _("Printing of array indexes is %s.\n"), value
);
195 /* Print repeat counts if there are more than this many repetitions of an
196 element in an array. Referenced by the low level language dependent
200 show_repeat_count_threshold (struct ui_file
*file
, int from_tty
,
201 struct cmd_list_element
*c
, const char *value
)
203 fprintf_filtered (file
, _("Threshold for repeated print elements is %s.\n"),
207 /* If nonzero, prints memory tag violations for pointers. */
210 show_memory_tag_violations (struct ui_file
*file
, int from_tty
,
211 struct cmd_list_element
*c
, const char *value
)
213 fprintf_filtered (file
,
214 _("Printing of memory tag violations is %s.\n"),
218 /* If nonzero, stops printing of char arrays at first null. */
221 show_stop_print_at_null (struct ui_file
*file
, int from_tty
,
222 struct cmd_list_element
*c
, const char *value
)
224 fprintf_filtered (file
,
225 _("Printing of char arrays to stop "
226 "at first null char is %s.\n"),
230 /* Controls pretty printing of structures. */
233 show_prettyformat_structs (struct ui_file
*file
, int from_tty
,
234 struct cmd_list_element
*c
, const char *value
)
236 fprintf_filtered (file
, _("Pretty formatting of structures is %s.\n"), value
);
239 /* Controls pretty printing of arrays. */
242 show_prettyformat_arrays (struct ui_file
*file
, int from_tty
,
243 struct cmd_list_element
*c
, const char *value
)
245 fprintf_filtered (file
, _("Pretty formatting of arrays is %s.\n"), value
);
248 /* If nonzero, causes unions inside structures or other unions to be
252 show_unionprint (struct ui_file
*file
, int from_tty
,
253 struct cmd_list_element
*c
, const char *value
)
255 fprintf_filtered (file
,
256 _("Printing of unions interior to structures is %s.\n"),
260 /* If nonzero, causes machine addresses to be printed in certain contexts. */
263 show_addressprint (struct ui_file
*file
, int from_tty
,
264 struct cmd_list_element
*c
, const char *value
)
266 fprintf_filtered (file
, _("Printing of addresses is %s.\n"), value
);
270 show_symbol_print (struct ui_file
*file
, int from_tty
,
271 struct cmd_list_element
*c
, const char *value
)
273 fprintf_filtered (file
,
274 _("Printing of symbols when printing pointers is %s.\n"),
280 /* A helper function for val_print. When printing in "summary" mode,
281 we want to print scalar arguments, but not aggregate arguments.
282 This function distinguishes between the two. */
285 val_print_scalar_type_p (struct type
*type
)
287 type
= check_typedef (type
);
288 while (TYPE_IS_REFERENCE (type
))
290 type
= TYPE_TARGET_TYPE (type
);
291 type
= check_typedef (type
);
293 switch (type
->code ())
295 case TYPE_CODE_ARRAY
:
296 case TYPE_CODE_STRUCT
:
297 case TYPE_CODE_UNION
:
299 case TYPE_CODE_STRING
:
306 /* A helper function for val_print. When printing with limited depth we
307 want to print string and scalar arguments, but not aggregate arguments.
308 This function distinguishes between the two. */
311 val_print_scalar_or_string_type_p (struct type
*type
,
312 const struct language_defn
*language
)
314 return (val_print_scalar_type_p (type
)
315 || language
->is_string_type_p (type
));
318 /* See valprint.h. */
321 valprint_check_validity (struct ui_file
*stream
,
323 LONGEST embedded_offset
,
324 const struct value
*val
)
326 type
= check_typedef (type
);
328 if (type_not_associated (type
))
330 val_print_not_associated (stream
);
334 if (type_not_allocated (type
))
336 val_print_not_allocated (stream
);
340 if (type
->code () != TYPE_CODE_UNION
341 && type
->code () != TYPE_CODE_STRUCT
342 && type
->code () != TYPE_CODE_ARRAY
)
344 if (value_bits_any_optimized_out (val
,
345 TARGET_CHAR_BIT
* embedded_offset
,
346 TARGET_CHAR_BIT
* TYPE_LENGTH (type
)))
348 val_print_optimized_out (val
, stream
);
352 if (value_bits_synthetic_pointer (val
, TARGET_CHAR_BIT
* embedded_offset
,
353 TARGET_CHAR_BIT
* TYPE_LENGTH (type
)))
355 const int is_ref
= type
->code () == TYPE_CODE_REF
;
356 int ref_is_addressable
= 0;
360 const struct value
*deref_val
= coerce_ref_if_computed (val
);
362 if (deref_val
!= NULL
)
363 ref_is_addressable
= value_lval_const (deref_val
) == lval_memory
;
366 if (!is_ref
|| !ref_is_addressable
)
367 fputs_styled (_("<synthetic pointer>"), metadata_style
.style (),
370 /* C++ references should be valid even if they're synthetic. */
374 if (!value_bytes_available (val
, embedded_offset
, TYPE_LENGTH (type
)))
376 val_print_unavailable (stream
);
385 val_print_optimized_out (const struct value
*val
, struct ui_file
*stream
)
387 if (val
!= NULL
&& value_lval_const (val
) == lval_register
)
388 val_print_not_saved (stream
);
390 fprintf_styled (stream
, metadata_style
.style (), _("<optimized out>"));
394 val_print_not_saved (struct ui_file
*stream
)
396 fprintf_styled (stream
, metadata_style
.style (), _("<not saved>"));
400 val_print_unavailable (struct ui_file
*stream
)
402 fprintf_styled (stream
, metadata_style
.style (), _("<unavailable>"));
406 val_print_invalid_address (struct ui_file
*stream
)
408 fprintf_styled (stream
, metadata_style
.style (), _("<invalid address>"));
411 /* Print a pointer based on the type of its target.
413 Arguments to this functions are roughly the same as those in
414 generic_val_print. A difference is that ADDRESS is the address to print,
415 with embedded_offset already added. ELTTYPE represents
416 the pointed type after check_typedef. */
419 print_unpacked_pointer (struct type
*type
, struct type
*elttype
,
420 CORE_ADDR address
, struct ui_file
*stream
,
421 const struct value_print_options
*options
)
423 struct gdbarch
*gdbarch
= type
->arch ();
425 if (elttype
->code () == TYPE_CODE_FUNC
)
427 /* Try to print what function it points to. */
428 print_function_pointer_address (options
, gdbarch
, address
, stream
);
432 if (options
->symbol_print
)
433 print_address_demangle (options
, gdbarch
, address
, stream
, demangle
);
434 else if (options
->addressprint
)
435 fputs_filtered (paddress (gdbarch
, address
), stream
);
438 /* generic_val_print helper for TYPE_CODE_ARRAY. */
441 generic_val_print_array (struct value
*val
,
442 struct ui_file
*stream
, int recurse
,
443 const struct value_print_options
*options
,
445 generic_val_print_decorations
*decorations
)
447 struct type
*type
= check_typedef (value_type (val
));
448 struct type
*unresolved_elttype
= TYPE_TARGET_TYPE (type
);
449 struct type
*elttype
= check_typedef (unresolved_elttype
);
451 if (TYPE_LENGTH (type
) > 0 && TYPE_LENGTH (unresolved_elttype
) > 0)
453 LONGEST low_bound
, high_bound
;
455 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
456 error (_("Could not determine the array high bound"));
458 fputs_filtered (decorations
->array_start
, stream
);
459 value_print_array_elements (val
, stream
, recurse
, options
, 0);
460 fputs_filtered (decorations
->array_end
, stream
);
464 /* Array of unspecified length: treat like pointer to first elt. */
465 print_unpacked_pointer (type
, elttype
, value_address (val
),
471 /* generic_value_print helper for TYPE_CODE_PTR. */
474 generic_value_print_ptr (struct value
*val
, struct ui_file
*stream
,
475 const struct value_print_options
*options
)
478 if (options
->format
&& options
->format
!= 's')
479 value_print_scalar_formatted (val
, options
, 0, stream
);
482 struct type
*type
= check_typedef (value_type (val
));
483 struct type
*elttype
= check_typedef (TYPE_TARGET_TYPE (type
));
484 const gdb_byte
*valaddr
= value_contents_for_printing (val
);
485 CORE_ADDR addr
= unpack_pointer (type
, valaddr
);
487 print_unpacked_pointer (type
, elttype
, addr
, stream
, options
);
492 /* Print '@' followed by the address contained in ADDRESS_BUFFER. */
495 print_ref_address (struct type
*type
, const gdb_byte
*address_buffer
,
496 int embedded_offset
, struct ui_file
*stream
)
498 struct gdbarch
*gdbarch
= type
->arch ();
500 if (address_buffer
!= NULL
)
503 = extract_typed_address (address_buffer
+ embedded_offset
, type
);
505 fprintf_filtered (stream
, "@");
506 fputs_filtered (paddress (gdbarch
, address
), stream
);
508 /* Else: we have a non-addressable value, such as a DW_AT_const_value. */
511 /* If VAL is addressable, return the value contents buffer of a value that
512 represents a pointer to VAL. Otherwise return NULL. */
514 static const gdb_byte
*
515 get_value_addr_contents (struct value
*deref_val
)
517 gdb_assert (deref_val
!= NULL
);
519 if (value_lval_const (deref_val
) == lval_memory
)
520 return value_contents_for_printing_const (value_addr (deref_val
));
523 /* We have a non-addressable value, such as a DW_AT_const_value. */
528 /* generic_val_print helper for TYPE_CODE_{RVALUE_,}REF. */
531 generic_val_print_ref (struct type
*type
,
532 int embedded_offset
, struct ui_file
*stream
, int recurse
,
533 struct value
*original_value
,
534 const struct value_print_options
*options
)
536 struct type
*elttype
= check_typedef (TYPE_TARGET_TYPE (type
));
537 struct value
*deref_val
= NULL
;
538 const int value_is_synthetic
539 = value_bits_synthetic_pointer (original_value
,
540 TARGET_CHAR_BIT
* embedded_offset
,
541 TARGET_CHAR_BIT
* TYPE_LENGTH (type
));
542 const int must_coerce_ref
= ((options
->addressprint
&& value_is_synthetic
)
543 || options
->deref_ref
);
544 const int type_is_defined
= elttype
->code () != TYPE_CODE_UNDEF
;
545 const gdb_byte
*valaddr
= value_contents_for_printing (original_value
);
547 if (must_coerce_ref
&& type_is_defined
)
549 deref_val
= coerce_ref_if_computed (original_value
);
551 if (deref_val
!= NULL
)
553 /* More complicated computed references are not supported. */
554 gdb_assert (embedded_offset
== 0);
557 deref_val
= value_at (TYPE_TARGET_TYPE (type
),
558 unpack_pointer (type
, valaddr
+ embedded_offset
));
560 /* Else, original_value isn't a synthetic reference or we don't have to print
561 the reference's contents.
563 Notice that for references to TYPE_CODE_STRUCT, 'set print object on' will
564 cause original_value to be a not_lval instead of an lval_computed,
565 which will make value_bits_synthetic_pointer return false.
566 This happens because if options->objectprint is true, c_value_print will
567 overwrite original_value's contents with the result of coercing
568 the reference through value_addr, and then set its type back to
569 TYPE_CODE_REF. In that case we don't have to coerce the reference again;
570 we can simply treat it as non-synthetic and move on. */
572 if (options
->addressprint
)
574 const gdb_byte
*address
= (value_is_synthetic
&& type_is_defined
575 ? get_value_addr_contents (deref_val
)
578 print_ref_address (type
, address
, embedded_offset
, stream
);
580 if (options
->deref_ref
)
581 fputs_filtered (": ", stream
);
584 if (options
->deref_ref
)
587 common_val_print (deref_val
, stream
, recurse
, options
,
590 fputs_filtered ("???", stream
);
594 /* Helper function for generic_val_print_enum.
595 This is also used to print enums in TYPE_CODE_FLAGS values. */
598 generic_val_print_enum_1 (struct type
*type
, LONGEST val
,
599 struct ui_file
*stream
)
604 len
= type
->num_fields ();
605 for (i
= 0; i
< len
; i
++)
608 if (val
== TYPE_FIELD_ENUMVAL (type
, i
))
615 fputs_styled (TYPE_FIELD_NAME (type
, i
), variable_name_style
.style (),
618 else if (TYPE_FLAG_ENUM (type
))
622 /* We have a "flag" enum, so we try to decompose it into pieces as
623 appropriate. The enum may have multiple enumerators representing
624 the same bit, in which case we choose to only print the first one
626 for (i
= 0; i
< len
; ++i
)
630 ULONGEST enumval
= TYPE_FIELD_ENUMVAL (type
, i
);
631 int nbits
= count_one_bits_ll (enumval
);
633 gdb_assert (nbits
== 0 || nbits
== 1);
635 if ((val
& enumval
) != 0)
639 fputs_filtered ("(", stream
);
643 fputs_filtered (" | ", stream
);
645 val
&= ~TYPE_FIELD_ENUMVAL (type
, i
);
646 fputs_styled (TYPE_FIELD_NAME (type
, i
),
647 variable_name_style
.style (), stream
);
653 /* There are leftover bits, print them. */
655 fputs_filtered ("(", stream
);
657 fputs_filtered (" | ", stream
);
659 fputs_filtered ("unknown: 0x", stream
);
660 print_longest (stream
, 'x', 0, val
);
661 fputs_filtered (")", stream
);
665 /* Nothing has been printed and the value is 0, the enum value must
667 fputs_filtered ("0", stream
);
671 /* Something has been printed, close the parenthesis. */
672 fputs_filtered (")", stream
);
676 print_longest (stream
, 'd', 0, val
);
679 /* generic_val_print helper for TYPE_CODE_ENUM. */
682 generic_val_print_enum (struct type
*type
,
683 int embedded_offset
, struct ui_file
*stream
,
684 struct value
*original_value
,
685 const struct value_print_options
*options
)
688 struct gdbarch
*gdbarch
= type
->arch ();
689 int unit_size
= gdbarch_addressable_memory_unit_size (gdbarch
);
691 gdb_assert (!options
->format
);
693 const gdb_byte
*valaddr
= value_contents_for_printing (original_value
);
695 val
= unpack_long (type
, valaddr
+ embedded_offset
* unit_size
);
697 generic_val_print_enum_1 (type
, val
, stream
);
700 /* generic_val_print helper for TYPE_CODE_FUNC and TYPE_CODE_METHOD. */
703 generic_val_print_func (struct type
*type
,
704 int embedded_offset
, CORE_ADDR address
,
705 struct ui_file
*stream
,
706 struct value
*original_value
,
707 const struct value_print_options
*options
)
709 struct gdbarch
*gdbarch
= type
->arch ();
711 gdb_assert (!options
->format
);
713 /* FIXME, we should consider, at least for ANSI C language,
714 eliminating the distinction made between FUNCs and POINTERs to
716 fprintf_filtered (stream
, "{");
717 type_print (type
, "", stream
, -1);
718 fprintf_filtered (stream
, "} ");
719 /* Try to print what function it points to, and its address. */
720 print_address_demangle (options
, gdbarch
, address
, stream
, demangle
);
723 /* generic_value_print helper for TYPE_CODE_BOOL. */
726 generic_value_print_bool
727 (struct value
*value
, struct ui_file
*stream
,
728 const struct value_print_options
*options
,
729 const struct generic_val_print_decorations
*decorations
)
731 if (options
->format
|| options
->output_format
)
733 struct value_print_options opts
= *options
;
734 opts
.format
= (options
->format
? options
->format
735 : options
->output_format
);
736 value_print_scalar_formatted (value
, &opts
, 0, stream
);
740 const gdb_byte
*valaddr
= value_contents_for_printing (value
);
741 struct type
*type
= check_typedef (value_type (value
));
742 LONGEST val
= unpack_long (type
, valaddr
);
744 fputs_filtered (decorations
->false_name
, stream
);
746 fputs_filtered (decorations
->true_name
, stream
);
748 print_longest (stream
, 'd', 0, val
);
752 /* generic_value_print helper for TYPE_CODE_INT. */
755 generic_value_print_int (struct value
*val
, struct ui_file
*stream
,
756 const struct value_print_options
*options
)
758 struct value_print_options opts
= *options
;
760 opts
.format
= (options
->format
? options
->format
761 : options
->output_format
);
762 value_print_scalar_formatted (val
, &opts
, 0, stream
);
765 /* generic_value_print helper for TYPE_CODE_CHAR. */
768 generic_value_print_char (struct value
*value
, struct ui_file
*stream
,
769 const struct value_print_options
*options
)
771 if (options
->format
|| options
->output_format
)
773 struct value_print_options opts
= *options
;
775 opts
.format
= (options
->format
? options
->format
776 : options
->output_format
);
777 value_print_scalar_formatted (value
, &opts
, 0, stream
);
781 struct type
*unresolved_type
= value_type (value
);
782 struct type
*type
= check_typedef (unresolved_type
);
783 const gdb_byte
*valaddr
= value_contents_for_printing (value
);
785 LONGEST val
= unpack_long (type
, valaddr
);
786 if (type
->is_unsigned ())
787 fprintf_filtered (stream
, "%u", (unsigned int) val
);
789 fprintf_filtered (stream
, "%d", (int) val
);
790 fputs_filtered (" ", stream
);
791 LA_PRINT_CHAR (val
, unresolved_type
, stream
);
795 /* generic_val_print helper for TYPE_CODE_FLT and TYPE_CODE_DECFLOAT. */
798 generic_val_print_float (struct type
*type
, struct ui_file
*stream
,
799 struct value
*original_value
,
800 const struct value_print_options
*options
)
802 gdb_assert (!options
->format
);
804 const gdb_byte
*valaddr
= value_contents_for_printing (original_value
);
806 print_floating (valaddr
, type
, stream
);
809 /* generic_val_print helper for TYPE_CODE_FIXED_POINT. */
812 generic_val_print_fixed_point (struct value
*val
, struct ui_file
*stream
,
813 const struct value_print_options
*options
)
816 value_print_scalar_formatted (val
, options
, 0, stream
);
819 struct type
*type
= value_type (val
);
821 const gdb_byte
*valaddr
= value_contents_for_printing (val
);
824 f
.read_fixed_point (gdb::make_array_view (valaddr
, TYPE_LENGTH (type
)),
825 type_byte_order (type
), type
->is_unsigned (),
826 type
->fixed_point_scaling_factor ());
828 const char *fmt
= TYPE_LENGTH (type
) < 4 ? "%.11Fg" : "%.17Fg";
829 std::string str
= gmp_string_printf (fmt
, f
.val
);
830 fprintf_filtered (stream
, "%s", str
.c_str ());
834 /* generic_value_print helper for TYPE_CODE_COMPLEX. */
837 generic_value_print_complex (struct value
*val
, struct ui_file
*stream
,
838 const struct value_print_options
*options
,
839 const struct generic_val_print_decorations
842 fprintf_filtered (stream
, "%s", decorations
->complex_prefix
);
844 struct value
*real_part
= value_real_part (val
);
845 value_print_scalar_formatted (real_part
, options
, 0, stream
);
846 fprintf_filtered (stream
, "%s", decorations
->complex_infix
);
848 struct value
*imag_part
= value_imaginary_part (val
);
849 value_print_scalar_formatted (imag_part
, options
, 0, stream
);
850 fprintf_filtered (stream
, "%s", decorations
->complex_suffix
);
853 /* generic_value_print helper for TYPE_CODE_MEMBERPTR. */
856 generic_value_print_memberptr
857 (struct value
*val
, struct ui_file
*stream
,
859 const struct value_print_options
*options
,
860 const struct generic_val_print_decorations
*decorations
)
862 if (!options
->format
)
864 /* Member pointers are essentially specific to C++, and so if we
865 encounter one, we should print it according to C++ rules. */
866 struct type
*type
= check_typedef (value_type (val
));
867 const gdb_byte
*valaddr
= value_contents_for_printing (val
);
868 cp_print_class_member (valaddr
, type
, stream
, "&");
871 generic_value_print (val
, stream
, recurse
, options
, decorations
);
874 /* See valprint.h. */
877 generic_value_print (struct value
*val
, struct ui_file
*stream
, int recurse
,
878 const struct value_print_options
*options
,
879 const struct generic_val_print_decorations
*decorations
)
881 struct type
*type
= value_type (val
);
883 type
= check_typedef (type
);
885 if (is_fixed_point_type (type
))
886 type
= type
->fixed_point_type_base_type ();
888 switch (type
->code ())
890 case TYPE_CODE_ARRAY
:
891 generic_val_print_array (val
, stream
, recurse
, options
, decorations
);
894 case TYPE_CODE_MEMBERPTR
:
895 generic_value_print_memberptr (val
, stream
, recurse
, options
,
900 generic_value_print_ptr (val
, stream
, options
);
904 case TYPE_CODE_RVALUE_REF
:
905 generic_val_print_ref (type
, 0, stream
, recurse
,
911 value_print_scalar_formatted (val
, options
, 0, stream
);
913 generic_val_print_enum (type
, 0, stream
, val
, options
);
916 case TYPE_CODE_FLAGS
:
918 value_print_scalar_formatted (val
, options
, 0, stream
);
920 val_print_type_code_flags (type
, val
, 0, stream
);
924 case TYPE_CODE_METHOD
:
926 value_print_scalar_formatted (val
, options
, 0, stream
);
928 generic_val_print_func (type
, 0, value_address (val
), stream
,
933 generic_value_print_bool (val
, stream
, options
, decorations
);
936 case TYPE_CODE_RANGE
:
938 generic_value_print_int (val
, stream
, options
);
942 generic_value_print_char (val
, stream
, options
);
946 case TYPE_CODE_DECFLOAT
:
948 value_print_scalar_formatted (val
, options
, 0, stream
);
950 generic_val_print_float (type
, stream
, val
, options
);
953 case TYPE_CODE_FIXED_POINT
:
954 generic_val_print_fixed_point (val
, stream
, options
);
958 fputs_filtered (decorations
->void_name
, stream
);
961 case TYPE_CODE_ERROR
:
962 fprintf_filtered (stream
, "%s", TYPE_ERROR_NAME (type
));
965 case TYPE_CODE_UNDEF
:
966 /* This happens (without TYPE_STUB set) on systems which don't use
967 dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
968 and no complete type for struct foo in that file. */
969 fprintf_styled (stream
, metadata_style
.style (), _("<incomplete type>"));
972 case TYPE_CODE_COMPLEX
:
973 generic_value_print_complex (val
, stream
, options
, decorations
);
976 case TYPE_CODE_METHODPTR
:
977 cplus_print_method_ptr (value_contents_for_printing (val
), type
,
981 case TYPE_CODE_UNION
:
982 case TYPE_CODE_STRUCT
:
984 error (_("Unhandled type code %d in symbol table."),
989 /* Helper function for val_print and common_val_print that does the
990 work. Arguments are as to val_print, but FULL_VALUE, if given, is
991 the value to be printed. */
994 do_val_print (struct value
*value
, struct ui_file
*stream
, int recurse
,
995 const struct value_print_options
*options
,
996 const struct language_defn
*language
)
999 struct value_print_options local_opts
= *options
;
1000 struct type
*type
= value_type (value
);
1001 struct type
*real_type
= check_typedef (type
);
1003 if (local_opts
.prettyformat
== Val_prettyformat_default
)
1004 local_opts
.prettyformat
= (local_opts
.prettyformat_structs
1005 ? Val_prettyformat
: Val_no_prettyformat
);
1009 /* Ensure that the type is complete and not just a stub. If the type is
1010 only a stub and we can't find and substitute its complete type, then
1011 print appropriate string and return. */
1013 if (real_type
->is_stub ())
1015 fprintf_styled (stream
, metadata_style
.style (), _("<incomplete type>"));
1019 if (!valprint_check_validity (stream
, real_type
, 0, value
))
1024 ret
= apply_ext_lang_val_pretty_printer (value
, stream
, recurse
, options
,
1030 /* Handle summary mode. If the value is a scalar, print it;
1031 otherwise, print an ellipsis. */
1032 if (options
->summary
&& !val_print_scalar_type_p (type
))
1034 fprintf_filtered (stream
, "...");
1038 /* If this value is too deep then don't print it. */
1039 if (!val_print_scalar_or_string_type_p (type
, language
)
1040 && val_print_check_max_depth (stream
, recurse
, options
, language
))
1045 language
->value_print_inner (value
, stream
, recurse
, &local_opts
);
1047 catch (const gdb_exception_error
&except
)
1049 fprintf_styled (stream
, metadata_style
.style (),
1050 _("<error reading variable>"));
1054 /* See valprint.h. */
1057 val_print_check_max_depth (struct ui_file
*stream
, int recurse
,
1058 const struct value_print_options
*options
,
1059 const struct language_defn
*language
)
1061 if (options
->max_depth
> -1 && recurse
>= options
->max_depth
)
1063 gdb_assert (language
->struct_too_deep_ellipsis () != NULL
);
1064 fputs_filtered (language
->struct_too_deep_ellipsis (), stream
);
1071 /* Check whether the value VAL is printable. Return 1 if it is;
1072 return 0 and print an appropriate error message to STREAM according to
1073 OPTIONS if it is not. */
1076 value_check_printable (struct value
*val
, struct ui_file
*stream
,
1077 const struct value_print_options
*options
)
1081 fprintf_styled (stream
, metadata_style
.style (),
1082 _("<address of value unknown>"));
1086 if (value_entirely_optimized_out (val
))
1088 if (options
->summary
&& !val_print_scalar_type_p (value_type (val
)))
1089 fprintf_filtered (stream
, "...");
1091 val_print_optimized_out (val
, stream
);
1095 if (value_entirely_unavailable (val
))
1097 if (options
->summary
&& !val_print_scalar_type_p (value_type (val
)))
1098 fprintf_filtered (stream
, "...");
1100 val_print_unavailable (stream
);
1104 if (value_type (val
)->code () == TYPE_CODE_INTERNAL_FUNCTION
)
1106 fprintf_styled (stream
, metadata_style
.style (),
1107 _("<internal function %s>"),
1108 value_internal_function_name (val
));
1112 if (type_not_associated (value_type (val
)))
1114 val_print_not_associated (stream
);
1118 if (type_not_allocated (value_type (val
)))
1120 val_print_not_allocated (stream
);
1127 /* Print using the given LANGUAGE the value VAL onto stream STREAM according
1130 This is a preferable interface to val_print, above, because it uses
1131 GDB's value mechanism. */
1134 common_val_print (struct value
*val
, struct ui_file
*stream
, int recurse
,
1135 const struct value_print_options
*options
,
1136 const struct language_defn
*language
)
1138 if (language
->la_language
== language_ada
)
1139 /* The value might have a dynamic type, which would cause trouble
1140 below when trying to extract the value contents (since the value
1141 size is determined from the type size which is unknown). So
1142 get a fixed representation of our value. */
1143 val
= ada_to_fixed_value (val
);
1145 if (value_lazy (val
))
1146 value_fetch_lazy (val
);
1148 do_val_print (val
, stream
, recurse
, options
, language
);
1151 /* See valprint.h. */
1154 common_val_print_checked (struct value
*val
, struct ui_file
*stream
,
1156 const struct value_print_options
*options
,
1157 const struct language_defn
*language
)
1159 if (!value_check_printable (val
, stream
, options
))
1161 common_val_print (val
, stream
, recurse
, options
, language
);
1164 /* Print on stream STREAM the value VAL according to OPTIONS. The value
1165 is printed using the current_language syntax. */
1168 value_print (struct value
*val
, struct ui_file
*stream
,
1169 const struct value_print_options
*options
)
1171 scoped_value_mark free_values
;
1173 if (!value_check_printable (val
, stream
, options
))
1179 = apply_ext_lang_val_pretty_printer (val
, stream
, 0, options
,
1186 current_language
->value_print (val
, stream
, options
);
1190 val_print_type_code_flags (struct type
*type
, struct value
*original_value
,
1191 int embedded_offset
, struct ui_file
*stream
)
1193 const gdb_byte
*valaddr
= (value_contents_for_printing (original_value
)
1195 ULONGEST val
= unpack_long (type
, valaddr
);
1196 int field
, nfields
= type
->num_fields ();
1197 struct gdbarch
*gdbarch
= type
->arch ();
1198 struct type
*bool_type
= builtin_type (gdbarch
)->builtin_bool
;
1200 fputs_filtered ("[", stream
);
1201 for (field
= 0; field
< nfields
; field
++)
1203 if (TYPE_FIELD_NAME (type
, field
)[0] != '\0')
1205 struct type
*field_type
= type
->field (field
).type ();
1207 if (field_type
== bool_type
1208 /* We require boolean types here to be one bit wide. This is a
1209 problematic place to notify the user of an internal error
1210 though. Instead just fall through and print the field as an
1212 && TYPE_FIELD_BITSIZE (type
, field
) == 1)
1214 if (val
& ((ULONGEST
)1 << TYPE_FIELD_BITPOS (type
, field
)))
1217 styled_string (variable_name_style
.style (),
1218 TYPE_FIELD_NAME (type
, field
)));
1222 unsigned field_len
= TYPE_FIELD_BITSIZE (type
, field
);
1224 = val
>> (TYPE_FIELD_BITPOS (type
, field
) - field_len
+ 1);
1226 if (field_len
< sizeof (ULONGEST
) * TARGET_CHAR_BIT
)
1227 field_val
&= ((ULONGEST
) 1 << field_len
) - 1;
1228 fprintf_filtered (stream
, " %ps=",
1229 styled_string (variable_name_style
.style (),
1230 TYPE_FIELD_NAME (type
, field
)));
1231 if (field_type
->code () == TYPE_CODE_ENUM
)
1232 generic_val_print_enum_1 (field_type
, field_val
, stream
);
1234 print_longest (stream
, 'd', 0, field_val
);
1238 fputs_filtered (" ]", stream
);
1241 /* See valprint.h. */
1244 value_print_scalar_formatted (struct value
*val
,
1245 const struct value_print_options
*options
,
1247 struct ui_file
*stream
)
1249 struct type
*type
= check_typedef (value_type (val
));
1251 gdb_assert (val
!= NULL
);
1253 /* If we get here with a string format, try again without it. Go
1254 all the way back to the language printers, which may call us
1256 if (options
->format
== 's')
1258 struct value_print_options opts
= *options
;
1261 common_val_print (val
, stream
, 0, &opts
, current_language
);
1265 /* value_contents_for_printing fetches all VAL's contents. They are
1266 needed to check whether VAL is optimized-out or unavailable
1268 const gdb_byte
*valaddr
= value_contents_for_printing (val
);
1270 /* A scalar object that does not have all bits available can't be
1271 printed, because all bits contribute to its representation. */
1272 if (value_bits_any_optimized_out (val
, 0,
1273 TARGET_CHAR_BIT
* TYPE_LENGTH (type
)))
1274 val_print_optimized_out (val
, stream
);
1275 else if (!value_bytes_available (val
, 0, TYPE_LENGTH (type
)))
1276 val_print_unavailable (stream
);
1278 print_scalar_formatted (valaddr
, type
, options
, size
, stream
);
1281 /* Print a number according to FORMAT which is one of d,u,x,o,b,h,w,g.
1282 The raison d'etre of this function is to consolidate printing of
1283 LONG_LONG's into this one function. The format chars b,h,w,g are
1284 from print_scalar_formatted(). Numbers are printed using C
1287 USE_C_FORMAT means to use C format in all cases. Without it,
1288 'o' and 'x' format do not include the standard C radix prefix
1291 Hilfinger/2004-09-09: USE_C_FORMAT was originally called USE_LOCAL
1292 and was intended to request formatting according to the current
1293 language and would be used for most integers that GDB prints. The
1294 exceptional cases were things like protocols where the format of
1295 the integer is a protocol thing, not a user-visible thing). The
1296 parameter remains to preserve the information of what things might
1297 be printed with language-specific format, should we ever resurrect
1301 print_longest (struct ui_file
*stream
, int format
, int use_c_format
,
1309 val
= int_string (val_long
, 10, 1, 0, 1); break;
1311 val
= int_string (val_long
, 10, 0, 0, 1); break;
1313 val
= int_string (val_long
, 16, 0, 0, use_c_format
); break;
1315 val
= int_string (val_long
, 16, 0, 2, 1); break;
1317 val
= int_string (val_long
, 16, 0, 4, 1); break;
1319 val
= int_string (val_long
, 16, 0, 8, 1); break;
1321 val
= int_string (val_long
, 16, 0, 16, 1); break;
1324 val
= int_string (val_long
, 8, 0, 0, use_c_format
); break;
1326 internal_error (__FILE__
, __LINE__
,
1327 _("failed internal consistency check"));
1329 fputs_filtered (val
, stream
);
1332 /* This used to be a macro, but I don't think it is called often enough
1333 to merit such treatment. */
1334 /* Convert a LONGEST to an int. This is used in contexts (e.g. number of
1335 arguments to a function, number in a value history, register number, etc.)
1336 where the value must not be larger than can fit in an int. */
1339 longest_to_int (LONGEST arg
)
1341 /* Let the compiler do the work. */
1342 int rtnval
= (int) arg
;
1344 /* Check for overflows or underflows. */
1345 if (sizeof (LONGEST
) > sizeof (int))
1349 error (_("Value out of range."));
1355 /* Print a floating point value of floating-point type TYPE,
1356 pointed to in GDB by VALADDR, on STREAM. */
1359 print_floating (const gdb_byte
*valaddr
, struct type
*type
,
1360 struct ui_file
*stream
)
1362 std::string str
= target_float_to_string (valaddr
, type
);
1363 fputs_filtered (str
.c_str (), stream
);
1367 print_binary_chars (struct ui_file
*stream
, const gdb_byte
*valaddr
,
1368 unsigned len
, enum bfd_endian byte_order
, bool zero_pad
)
1373 bool seen_a_one
= false;
1375 /* Declared "int" so it will be signed.
1376 This ensures that right shift will shift in zeros. */
1378 const int mask
= 0x080;
1380 if (byte_order
== BFD_ENDIAN_BIG
)
1386 /* Every byte has 8 binary characters; peel off
1387 and print from the MSB end. */
1389 for (i
= 0; i
< (HOST_CHAR_BIT
* sizeof (*p
)); i
++)
1391 if (*p
& (mask
>> i
))
1396 if (zero_pad
|| seen_a_one
|| b
== '1')
1397 fputc_filtered (b
, stream
);
1405 for (p
= valaddr
+ len
- 1;
1409 for (i
= 0; i
< (HOST_CHAR_BIT
* sizeof (*p
)); i
++)
1411 if (*p
& (mask
>> i
))
1416 if (zero_pad
|| seen_a_one
|| b
== '1')
1417 fputc_filtered (b
, stream
);
1424 /* When not zero-padding, ensure that something is printed when the
1426 if (!zero_pad
&& !seen_a_one
)
1427 fputc_filtered ('0', stream
);
1430 /* A helper for print_octal_chars that emits a single octal digit,
1431 optionally suppressing it if is zero and updating SEEN_A_ONE. */
1434 emit_octal_digit (struct ui_file
*stream
, bool *seen_a_one
, int digit
)
1436 if (*seen_a_one
|| digit
!= 0)
1437 fprintf_filtered (stream
, "%o", digit
);
1442 /* VALADDR points to an integer of LEN bytes.
1443 Print it in octal on stream or format it in buf. */
1446 print_octal_chars (struct ui_file
*stream
, const gdb_byte
*valaddr
,
1447 unsigned len
, enum bfd_endian byte_order
)
1450 unsigned char octa1
, octa2
, octa3
, carry
;
1453 /* Octal is 3 bits, which doesn't fit. Yuk. So we have to track
1454 * the extra bits, which cycle every three bytes:
1456 * Byte side: 0 1 2 3
1458 * bit number 123 456 78 | 9 012 345 6 | 78 901 234 | 567 890 12 |
1460 * Octal side: 0 1 carry 3 4 carry ...
1462 * Cycle number: 0 1 2
1464 * But of course we are printing from the high side, so we have to
1465 * figure out where in the cycle we are so that we end up with no
1466 * left over bits at the end.
1468 #define BITS_IN_OCTAL 3
1469 #define HIGH_ZERO 0340
1470 #define LOW_ZERO 0034
1471 #define CARRY_ZERO 0003
1472 static_assert (HIGH_ZERO
+ LOW_ZERO
+ CARRY_ZERO
== 0xff,
1473 "cycle zero constants are wrong");
1474 #define HIGH_ONE 0200
1475 #define MID_ONE 0160
1476 #define LOW_ONE 0016
1477 #define CARRY_ONE 0001
1478 static_assert (HIGH_ONE
+ MID_ONE
+ LOW_ONE
+ CARRY_ONE
== 0xff,
1479 "cycle one constants are wrong");
1480 #define HIGH_TWO 0300
1481 #define MID_TWO 0070
1482 #define LOW_TWO 0007
1483 static_assert (HIGH_TWO
+ MID_TWO
+ LOW_TWO
== 0xff,
1484 "cycle two constants are wrong");
1486 /* For 32 we start in cycle 2, with two bits and one bit carry;
1487 for 64 in cycle in cycle 1, with one bit and a two bit carry. */
1489 cycle
= (len
* HOST_CHAR_BIT
) % BITS_IN_OCTAL
;
1492 fputs_filtered ("0", stream
);
1493 bool seen_a_one
= false;
1494 if (byte_order
== BFD_ENDIAN_BIG
)
1503 /* No carry in, carry out two bits. */
1505 octa1
= (HIGH_ZERO
& *p
) >> 5;
1506 octa2
= (LOW_ZERO
& *p
) >> 2;
1507 carry
= (CARRY_ZERO
& *p
);
1508 emit_octal_digit (stream
, &seen_a_one
, octa1
);
1509 emit_octal_digit (stream
, &seen_a_one
, octa2
);
1513 /* Carry in two bits, carry out one bit. */
1515 octa1
= (carry
<< 1) | ((HIGH_ONE
& *p
) >> 7);
1516 octa2
= (MID_ONE
& *p
) >> 4;
1517 octa3
= (LOW_ONE
& *p
) >> 1;
1518 carry
= (CARRY_ONE
& *p
);
1519 emit_octal_digit (stream
, &seen_a_one
, octa1
);
1520 emit_octal_digit (stream
, &seen_a_one
, octa2
);
1521 emit_octal_digit (stream
, &seen_a_one
, octa3
);
1525 /* Carry in one bit, no carry out. */
1527 octa1
= (carry
<< 2) | ((HIGH_TWO
& *p
) >> 6);
1528 octa2
= (MID_TWO
& *p
) >> 3;
1529 octa3
= (LOW_TWO
& *p
);
1531 emit_octal_digit (stream
, &seen_a_one
, octa1
);
1532 emit_octal_digit (stream
, &seen_a_one
, octa2
);
1533 emit_octal_digit (stream
, &seen_a_one
, octa3
);
1537 error (_("Internal error in octal conversion;"));
1541 cycle
= cycle
% BITS_IN_OCTAL
;
1546 for (p
= valaddr
+ len
- 1;
1553 /* Carry out, no carry in */
1555 octa1
= (HIGH_ZERO
& *p
) >> 5;
1556 octa2
= (LOW_ZERO
& *p
) >> 2;
1557 carry
= (CARRY_ZERO
& *p
);
1558 emit_octal_digit (stream
, &seen_a_one
, octa1
);
1559 emit_octal_digit (stream
, &seen_a_one
, octa2
);
1563 /* Carry in, carry out */
1565 octa1
= (carry
<< 1) | ((HIGH_ONE
& *p
) >> 7);
1566 octa2
= (MID_ONE
& *p
) >> 4;
1567 octa3
= (LOW_ONE
& *p
) >> 1;
1568 carry
= (CARRY_ONE
& *p
);
1569 emit_octal_digit (stream
, &seen_a_one
, octa1
);
1570 emit_octal_digit (stream
, &seen_a_one
, octa2
);
1571 emit_octal_digit (stream
, &seen_a_one
, octa3
);
1575 /* Carry in, no carry out */
1577 octa1
= (carry
<< 2) | ((HIGH_TWO
& *p
) >> 6);
1578 octa2
= (MID_TWO
& *p
) >> 3;
1579 octa3
= (LOW_TWO
& *p
);
1581 emit_octal_digit (stream
, &seen_a_one
, octa1
);
1582 emit_octal_digit (stream
, &seen_a_one
, octa2
);
1583 emit_octal_digit (stream
, &seen_a_one
, octa3
);
1587 error (_("Internal error in octal conversion;"));
1591 cycle
= cycle
% BITS_IN_OCTAL
;
1597 /* Possibly negate the integer represented by BYTES. It contains LEN
1598 bytes in the specified byte order. If the integer is negative,
1599 copy it into OUT_VEC, negate it, and return true. Otherwise, do
1600 nothing and return false. */
1603 maybe_negate_by_bytes (const gdb_byte
*bytes
, unsigned len
,
1604 enum bfd_endian byte_order
,
1605 gdb::byte_vector
*out_vec
)
1608 gdb_assert (len
> 0);
1609 if (byte_order
== BFD_ENDIAN_BIG
)
1610 sign_byte
= bytes
[0];
1612 sign_byte
= bytes
[len
- 1];
1613 if ((sign_byte
& 0x80) == 0)
1616 out_vec
->resize (len
);
1618 /* Compute -x == 1 + ~x. */
1619 if (byte_order
== BFD_ENDIAN_LITTLE
)
1622 for (unsigned i
= 0; i
< len
; ++i
)
1624 unsigned tem
= (0xff & ~bytes
[i
]) + carry
;
1625 (*out_vec
)[i
] = tem
& 0xff;
1632 for (unsigned i
= len
; i
> 0; --i
)
1634 unsigned tem
= (0xff & ~bytes
[i
- 1]) + carry
;
1635 (*out_vec
)[i
- 1] = tem
& 0xff;
1643 /* VALADDR points to an integer of LEN bytes.
1644 Print it in decimal on stream or format it in buf. */
1647 print_decimal_chars (struct ui_file
*stream
, const gdb_byte
*valaddr
,
1648 unsigned len
, bool is_signed
,
1649 enum bfd_endian byte_order
)
1652 #define CARRY_OUT( x ) ((x) / TEN) /* extend char to int */
1653 #define CARRY_LEFT( x ) ((x) % TEN)
1654 #define SHIFT( x ) ((x) << 4)
1655 #define LOW_NIBBLE( x ) ( (x) & 0x00F)
1656 #define HIGH_NIBBLE( x ) (((x) & 0x0F0) >> 4)
1661 int i
, j
, decimal_digits
;
1665 gdb::byte_vector negated_bytes
;
1667 && maybe_negate_by_bytes (valaddr
, len
, byte_order
, &negated_bytes
))
1669 fputs_filtered ("-", stream
);
1670 valaddr
= negated_bytes
.data ();
1673 /* Base-ten number is less than twice as many digits
1674 as the base 16 number, which is 2 digits per byte. */
1676 decimal_len
= len
* 2 * 2;
1677 std::vector
<unsigned char> digits (decimal_len
, 0);
1679 /* Ok, we have an unknown number of bytes of data to be printed in
1682 * Given a hex number (in nibbles) as XYZ, we start by taking X and
1683 * decimalizing it as "x1 x2" in two decimal nibbles. Then we multiply
1684 * the nibbles by 16, add Y and re-decimalize. Repeat with Z.
1686 * The trick is that "digits" holds a base-10 number, but sometimes
1687 * the individual digits are > 10.
1689 * Outer loop is per nibble (hex digit) of input, from MSD end to
1692 decimal_digits
= 0; /* Number of decimal digits so far */
1693 p
= (byte_order
== BFD_ENDIAN_BIG
) ? valaddr
: valaddr
+ len
- 1;
1695 while ((byte_order
== BFD_ENDIAN_BIG
) ? (p
< valaddr
+ len
) : (p
>= valaddr
))
1698 * Multiply current base-ten number by 16 in place.
1699 * Each digit was between 0 and 9, now is between
1702 for (j
= 0; j
< decimal_digits
; j
++)
1704 digits
[j
] = SHIFT (digits
[j
]);
1707 /* Take the next nibble off the input and add it to what
1708 * we've got in the LSB position. Bottom 'digit' is now
1709 * between 0 and 159.
1711 * "flip" is used to run this loop twice for each byte.
1715 /* Take top nibble. */
1717 digits
[0] += HIGH_NIBBLE (*p
);
1722 /* Take low nibble and bump our pointer "p". */
1724 digits
[0] += LOW_NIBBLE (*p
);
1725 if (byte_order
== BFD_ENDIAN_BIG
)
1732 /* Re-decimalize. We have to do this often enough
1733 * that we don't overflow, but once per nibble is
1734 * overkill. Easier this way, though. Note that the
1735 * carry is often larger than 10 (e.g. max initial
1736 * carry out of lowest nibble is 15, could bubble all
1737 * the way up greater than 10). So we have to do
1738 * the carrying beyond the last current digit.
1741 for (j
= 0; j
< decimal_len
- 1; j
++)
1745 /* "/" won't handle an unsigned char with
1746 * a value that if signed would be negative.
1747 * So extend to longword int via "dummy".
1750 carry
= CARRY_OUT (dummy
);
1751 digits
[j
] = CARRY_LEFT (dummy
);
1753 if (j
>= decimal_digits
&& carry
== 0)
1756 * All higher digits are 0 and we
1757 * no longer have a carry.
1759 * Note: "j" is 0-based, "decimal_digits" is
1762 decimal_digits
= j
+ 1;
1768 /* Ok, now "digits" is the decimal representation, with
1769 the "decimal_digits" actual digits. Print! */
1771 for (i
= decimal_digits
- 1; i
> 0 && digits
[i
] == 0; --i
)
1776 fprintf_filtered (stream
, "%1d", digits
[i
]);
1780 /* VALADDR points to an integer of LEN bytes. Print it in hex on stream. */
1783 print_hex_chars (struct ui_file
*stream
, const gdb_byte
*valaddr
,
1784 unsigned len
, enum bfd_endian byte_order
,
1789 fputs_filtered ("0x", stream
);
1790 if (byte_order
== BFD_ENDIAN_BIG
)
1796 /* Strip leading 0 bytes, but be sure to leave at least a
1797 single byte at the end. */
1798 for (; p
< valaddr
+ len
- 1 && !*p
; ++p
)
1802 const gdb_byte
*first
= p
;
1807 /* When not zero-padding, use a different format for the
1808 very first byte printed. */
1809 if (!zero_pad
&& p
== first
)
1810 fprintf_filtered (stream
, "%x", *p
);
1812 fprintf_filtered (stream
, "%02x", *p
);
1817 p
= valaddr
+ len
- 1;
1821 /* Strip leading 0 bytes, but be sure to leave at least a
1822 single byte at the end. */
1823 for (; p
>= valaddr
+ 1 && !*p
; --p
)
1827 const gdb_byte
*first
= p
;
1832 /* When not zero-padding, use a different format for the
1833 very first byte printed. */
1834 if (!zero_pad
&& p
== first
)
1835 fprintf_filtered (stream
, "%x", *p
);
1837 fprintf_filtered (stream
, "%02x", *p
);
1842 /* Print function pointer with inferior address ADDRESS onto stdio
1846 print_function_pointer_address (const struct value_print_options
*options
,
1847 struct gdbarch
*gdbarch
,
1849 struct ui_file
*stream
)
1852 = gdbarch_convert_from_func_ptr_addr (gdbarch
, address
,
1853 current_top_target ());
1855 /* If the function pointer is represented by a description, print
1856 the address of the description. */
1857 if (options
->addressprint
&& func_addr
!= address
)
1859 fputs_filtered ("@", stream
);
1860 fputs_filtered (paddress (gdbarch
, address
), stream
);
1861 fputs_filtered (": ", stream
);
1863 print_address_demangle (options
, gdbarch
, func_addr
, stream
, demangle
);
1867 /* Print on STREAM using the given OPTIONS the index for the element
1868 at INDEX of an array whose index type is INDEX_TYPE. */
1871 maybe_print_array_index (struct type
*index_type
, LONGEST index
,
1872 struct ui_file
*stream
,
1873 const struct value_print_options
*options
)
1875 if (!options
->print_array_indexes
)
1878 current_language
->print_array_index (index_type
, index
, stream
, options
);
1881 /* See valprint.h. */
1884 value_print_array_elements (struct value
*val
, struct ui_file
*stream
,
1886 const struct value_print_options
*options
,
1889 unsigned int things_printed
= 0;
1891 struct type
*elttype
, *index_type
;
1893 /* Position of the array element we are examining to see
1894 whether it is repeated. */
1896 /* Number of repetitions we have detected so far. */
1898 LONGEST low_bound
, high_bound
;
1900 struct type
*type
= check_typedef (value_type (val
));
1902 elttype
= TYPE_TARGET_TYPE (type
);
1903 eltlen
= type_length_units (check_typedef (elttype
));
1904 index_type
= type
->index_type ();
1905 if (index_type
->code () == TYPE_CODE_RANGE
)
1906 index_type
= TYPE_TARGET_TYPE (index_type
);
1908 if (get_array_bounds (type
, &low_bound
, &high_bound
))
1910 /* The array length should normally be HIGH_BOUND - LOW_BOUND +
1911 1. But we have to be a little extra careful, because some
1912 languages such as Ada allow LOW_BOUND to be greater than
1913 HIGH_BOUND for empty arrays. In that situation, the array
1914 length is just zero, not negative! */
1915 if (low_bound
> high_bound
)
1918 len
= high_bound
- low_bound
+ 1;
1922 warning (_("unable to get bounds of array, assuming null array"));
1927 annotate_array_section_begin (i
, elttype
);
1929 for (; i
< len
&& things_printed
< options
->print_max
; i
++)
1931 scoped_value_mark free_values
;
1935 if (options
->prettyformat_arrays
)
1937 fprintf_filtered (stream
, ",\n");
1938 print_spaces_filtered (2 + 2 * recurse
, stream
);
1941 fprintf_filtered (stream
, ", ");
1943 else if (options
->prettyformat_arrays
)
1945 fprintf_filtered (stream
, "\n");
1946 print_spaces_filtered (2 + 2 * recurse
, stream
);
1948 wrap_here (n_spaces (2 + 2 * recurse
));
1949 maybe_print_array_index (index_type
, i
+ low_bound
,
1954 /* Only check for reps if repeat_count_threshold is not set to
1955 UINT_MAX (unlimited). */
1956 if (options
->repeat_count_threshold
< UINT_MAX
)
1959 && value_contents_eq (val
, i
* eltlen
,
1968 struct value
*element
= value_from_component (val
, elttype
, eltlen
* i
);
1969 common_val_print (element
, stream
, recurse
+ 1, options
,
1972 if (reps
> options
->repeat_count_threshold
)
1974 annotate_elt_rep (reps
);
1975 fprintf_filtered (stream
, " %p[<repeats %u times>%p]",
1976 metadata_style
.style ().ptr (), reps
, nullptr);
1977 annotate_elt_rep_end ();
1980 things_printed
+= options
->repeat_count_threshold
;
1988 annotate_array_section_end ();
1990 fprintf_filtered (stream
, "...");
1991 if (options
->prettyformat_arrays
)
1993 fprintf_filtered (stream
, "\n");
1994 print_spaces_filtered (2 * recurse
, stream
);
1998 /* Read LEN bytes of target memory at address MEMADDR, placing the
1999 results in GDB's memory at MYADDR. Returns a count of the bytes
2000 actually read, and optionally a target_xfer_status value in the
2001 location pointed to by ERRPTR if ERRPTR is non-null. */
2003 /* FIXME: cagney/1999-10-14: Only used by val_print_string. Can this
2004 function be eliminated. */
2007 partial_memory_read (CORE_ADDR memaddr
, gdb_byte
*myaddr
,
2008 int len
, int *errptr
)
2010 int nread
; /* Number of bytes actually read. */
2011 int errcode
; /* Error from last read. */
2013 /* First try a complete read. */
2014 errcode
= target_read_memory (memaddr
, myaddr
, len
);
2022 /* Loop, reading one byte at a time until we get as much as we can. */
2023 for (errcode
= 0, nread
= 0; len
> 0 && errcode
== 0; nread
++, len
--)
2025 errcode
= target_read_memory (memaddr
++, myaddr
++, 1);
2027 /* If an error, the last read was unsuccessful, so adjust count. */
2040 /* Read a string from the inferior, at ADDR, with LEN characters of
2041 WIDTH bytes each. Fetch at most FETCHLIMIT characters. BUFFER
2042 will be set to a newly allocated buffer containing the string, and
2043 BYTES_READ will be set to the number of bytes read. Returns 0 on
2044 success, or a target_xfer_status on failure.
2046 If LEN > 0, reads the lesser of LEN or FETCHLIMIT characters
2047 (including eventual NULs in the middle or end of the string).
2049 If LEN is -1, stops at the first null character (not necessarily
2050 the first null byte) up to a maximum of FETCHLIMIT characters. Set
2051 FETCHLIMIT to UINT_MAX to read as many characters as possible from
2054 Unless an exception is thrown, BUFFER will always be allocated, even on
2055 failure. In this case, some characters might have been read before the
2056 failure happened. Check BYTES_READ to recognize this situation. */
2059 read_string (CORE_ADDR addr
, int len
, int width
, unsigned int fetchlimit
,
2060 enum bfd_endian byte_order
, gdb::unique_xmalloc_ptr
<gdb_byte
> *buffer
,
2063 int errcode
; /* Errno returned from bad reads. */
2064 unsigned int nfetch
; /* Chars to fetch / chars fetched. */
2065 gdb_byte
*bufptr
; /* Pointer to next available byte in
2068 /* Loop until we either have all the characters, or we encounter
2069 some error, such as bumping into the end of the address space. */
2071 buffer
->reset (nullptr);
2075 /* We want fetchlimit chars, so we might as well read them all in
2077 unsigned int fetchlen
= std::min ((unsigned) len
, fetchlimit
);
2079 buffer
->reset ((gdb_byte
*) xmalloc (fetchlen
* width
));
2080 bufptr
= buffer
->get ();
2082 nfetch
= partial_memory_read (addr
, bufptr
, fetchlen
* width
, &errcode
)
2084 addr
+= nfetch
* width
;
2085 bufptr
+= nfetch
* width
;
2089 unsigned long bufsize
= 0;
2090 unsigned int chunksize
; /* Size of each fetch, in chars. */
2091 int found_nul
; /* Non-zero if we found the nul char. */
2092 gdb_byte
*limit
; /* First location past end of fetch buffer. */
2095 /* We are looking for a NUL terminator to end the fetching, so we
2096 might as well read in blocks that are large enough to be efficient,
2097 but not so large as to be slow if fetchlimit happens to be large.
2098 So we choose the minimum of 8 and fetchlimit. We used to use 200
2099 instead of 8 but 200 is way too big for remote debugging over a
2101 chunksize
= std::min (8u, fetchlimit
);
2106 nfetch
= std::min ((unsigned long) chunksize
, fetchlimit
- bufsize
);
2108 if (*buffer
== NULL
)
2109 buffer
->reset ((gdb_byte
*) xmalloc (nfetch
* width
));
2111 buffer
->reset ((gdb_byte
*) xrealloc (buffer
->release (),
2112 (nfetch
+ bufsize
) * width
));
2114 bufptr
= buffer
->get () + bufsize
* width
;
2117 /* Read as much as we can. */
2118 nfetch
= partial_memory_read (addr
, bufptr
, nfetch
* width
, &errcode
)
2121 /* Scan this chunk for the null character that terminates the string
2122 to print. If found, we don't need to fetch any more. Note
2123 that bufptr is explicitly left pointing at the next character
2124 after the null character, or at the next character after the end
2127 limit
= bufptr
+ nfetch
* width
;
2128 while (bufptr
< limit
)
2132 c
= extract_unsigned_integer (bufptr
, width
, byte_order
);
2137 /* We don't care about any error which happened after
2138 the NUL terminator. */
2145 while (errcode
== 0 /* no error */
2146 && bufptr
- buffer
->get () < fetchlimit
* width
/* no overrun */
2147 && !found_nul
); /* haven't found NUL yet */
2150 { /* Length of string is really 0! */
2151 /* We always allocate *buffer. */
2152 buffer
->reset ((gdb_byte
*) xmalloc (1));
2153 bufptr
= buffer
->get ();
2157 /* bufptr and addr now point immediately beyond the last byte which we
2158 consider part of the string (including a '\0' which ends the string). */
2159 *bytes_read
= bufptr
- buffer
->get ();
2166 /* Return true if print_wchar can display W without resorting to a
2167 numeric escape, false otherwise. */
2170 wchar_printable (gdb_wchar_t w
)
2172 return (gdb_iswprint (w
)
2173 || w
== LCST ('\a') || w
== LCST ('\b')
2174 || w
== LCST ('\f') || w
== LCST ('\n')
2175 || w
== LCST ('\r') || w
== LCST ('\t')
2176 || w
== LCST ('\v'));
2179 /* A helper function that converts the contents of STRING to wide
2180 characters and then appends them to OUTPUT. */
2183 append_string_as_wide (const char *string
,
2184 struct obstack
*output
)
2186 for (; *string
; ++string
)
2188 gdb_wchar_t w
= gdb_btowc (*string
);
2189 obstack_grow (output
, &w
, sizeof (gdb_wchar_t
));
2193 /* Print a wide character W to OUTPUT. ORIG is a pointer to the
2194 original (target) bytes representing the character, ORIG_LEN is the
2195 number of valid bytes. WIDTH is the number of bytes in a base
2196 characters of the type. OUTPUT is an obstack to which wide
2197 characters are emitted. QUOTER is a (narrow) character indicating
2198 the style of quotes surrounding the character to be printed.
2199 NEED_ESCAPE is an in/out flag which is used to track numeric
2200 escapes across calls. */
2203 print_wchar (gdb_wint_t w
, const gdb_byte
*orig
,
2204 int orig_len
, int width
,
2205 enum bfd_endian byte_order
,
2206 struct obstack
*output
,
2207 int quoter
, int *need_escapep
)
2209 int need_escape
= *need_escapep
;
2213 /* iswprint implementation on Windows returns 1 for tab character.
2214 In order to avoid different printout on this host, we explicitly
2215 use wchar_printable function. */
2219 obstack_grow_wstr (output
, LCST ("\\a"));
2222 obstack_grow_wstr (output
, LCST ("\\b"));
2225 obstack_grow_wstr (output
, LCST ("\\f"));
2228 obstack_grow_wstr (output
, LCST ("\\n"));
2231 obstack_grow_wstr (output
, LCST ("\\r"));
2234 obstack_grow_wstr (output
, LCST ("\\t"));
2237 obstack_grow_wstr (output
, LCST ("\\v"));
2241 if (wchar_printable (w
) && (!need_escape
|| (!gdb_iswdigit (w
)
2243 && w
!= LCST ('9'))))
2245 gdb_wchar_t wchar
= w
;
2247 if (w
== gdb_btowc (quoter
) || w
== LCST ('\\'))
2248 obstack_grow_wstr (output
, LCST ("\\"));
2249 obstack_grow (output
, &wchar
, sizeof (gdb_wchar_t
));
2255 for (i
= 0; i
+ width
<= orig_len
; i
+= width
)
2260 value
= extract_unsigned_integer (&orig
[i
], width
,
2262 /* If the value fits in 3 octal digits, print it that
2263 way. Otherwise, print it as a hex escape. */
2265 xsnprintf (octal
, sizeof (octal
), "\\%.3o",
2266 (int) (value
& 0777));
2268 xsnprintf (octal
, sizeof (octal
), "\\x%lx", (long) value
);
2269 append_string_as_wide (octal
, output
);
2271 /* If we somehow have extra bytes, print them now. */
2272 while (i
< orig_len
)
2276 xsnprintf (octal
, sizeof (octal
), "\\%.3o", orig
[i
] & 0xff);
2277 append_string_as_wide (octal
, output
);
2288 /* Print the character C on STREAM as part of the contents of a
2289 literal string whose delimiter is QUOTER. ENCODING names the
2293 generic_emit_char (int c
, struct type
*type
, struct ui_file
*stream
,
2294 int quoter
, const char *encoding
)
2296 enum bfd_endian byte_order
2297 = type_byte_order (type
);
2299 int need_escape
= 0;
2301 c_buf
= (gdb_byte
*) alloca (TYPE_LENGTH (type
));
2302 pack_long (c_buf
, type
, c
);
2304 wchar_iterator
iter (c_buf
, TYPE_LENGTH (type
), encoding
, TYPE_LENGTH (type
));
2306 /* This holds the printable form of the wchar_t data. */
2307 auto_obstack wchar_buf
;
2313 const gdb_byte
*buf
;
2315 int print_escape
= 1;
2316 enum wchar_iterate_result result
;
2318 num_chars
= iter
.iterate (&result
, &chars
, &buf
, &buflen
);
2323 /* If all characters are printable, print them. Otherwise,
2324 we're going to have to print an escape sequence. We
2325 check all characters because we want to print the target
2326 bytes in the escape sequence, and we don't know character
2327 boundaries there. */
2331 for (i
= 0; i
< num_chars
; ++i
)
2332 if (!wchar_printable (chars
[i
]))
2340 for (i
= 0; i
< num_chars
; ++i
)
2341 print_wchar (chars
[i
], buf
, buflen
,
2342 TYPE_LENGTH (type
), byte_order
,
2343 &wchar_buf
, quoter
, &need_escape
);
2347 /* This handles the NUM_CHARS == 0 case as well. */
2349 print_wchar (gdb_WEOF
, buf
, buflen
, TYPE_LENGTH (type
),
2350 byte_order
, &wchar_buf
, quoter
, &need_escape
);
2353 /* The output in the host encoding. */
2354 auto_obstack output
;
2356 convert_between_encodings (INTERMEDIATE_ENCODING
, host_charset (),
2357 (gdb_byte
*) obstack_base (&wchar_buf
),
2358 obstack_object_size (&wchar_buf
),
2359 sizeof (gdb_wchar_t
), &output
, translit_char
);
2360 obstack_1grow (&output
, '\0');
2362 fputs_filtered ((const char *) obstack_base (&output
), stream
);
2365 /* Return the repeat count of the next character/byte in ITER,
2366 storing the result in VEC. */
2369 count_next_character (wchar_iterator
*iter
,
2370 std::vector
<converted_character
> *vec
)
2372 struct converted_character
*current
;
2376 struct converted_character tmp
;
2380 = iter
->iterate (&tmp
.result
, &chars
, &tmp
.buf
, &tmp
.buflen
);
2381 if (tmp
.num_chars
> 0)
2383 gdb_assert (tmp
.num_chars
< MAX_WCHARS
);
2384 memcpy (tmp
.chars
, chars
, tmp
.num_chars
* sizeof (gdb_wchar_t
));
2386 vec
->push_back (tmp
);
2389 current
= &vec
->back ();
2391 /* Count repeated characters or bytes. */
2392 current
->repeat_count
= 1;
2393 if (current
->num_chars
== -1)
2401 struct converted_character d
;
2408 /* Get the next character. */
2409 d
.num_chars
= iter
->iterate (&d
.result
, &chars
, &d
.buf
, &d
.buflen
);
2411 /* If a character was successfully converted, save the character
2412 into the converted character. */
2413 if (d
.num_chars
> 0)
2415 gdb_assert (d
.num_chars
< MAX_WCHARS
);
2416 memcpy (d
.chars
, chars
, WCHAR_BUFLEN (d
.num_chars
));
2419 /* Determine if the current character is the same as this
2421 if (d
.num_chars
== current
->num_chars
&& d
.result
== current
->result
)
2423 /* There are two cases to consider:
2425 1) Equality of converted character (num_chars > 0)
2426 2) Equality of non-converted character (num_chars == 0) */
2427 if ((current
->num_chars
> 0
2428 && memcmp (current
->chars
, d
.chars
,
2429 WCHAR_BUFLEN (current
->num_chars
)) == 0)
2430 || (current
->num_chars
== 0
2431 && current
->buflen
== d
.buflen
2432 && memcmp (current
->buf
, d
.buf
, current
->buflen
) == 0))
2433 ++current
->repeat_count
;
2441 /* Push this next converted character onto the result vector. */
2442 repeat
= current
->repeat_count
;
2448 /* Print the characters in CHARS to the OBSTACK. QUOTE_CHAR is the quote
2449 character to use with string output. WIDTH is the size of the output
2450 character type. BYTE_ORDER is the target byte order. OPTIONS
2451 is the user's print options. */
2454 print_converted_chars_to_obstack (struct obstack
*obstack
,
2455 const std::vector
<converted_character
> &chars
,
2456 int quote_char
, int width
,
2457 enum bfd_endian byte_order
,
2458 const struct value_print_options
*options
)
2461 const converted_character
*elem
;
2462 enum {START
, SINGLE
, REPEAT
, INCOMPLETE
, FINISH
} state
, last
;
2463 gdb_wchar_t wide_quote_char
= gdb_btowc (quote_char
);
2464 int need_escape
= 0;
2466 /* Set the start state. */
2468 last
= state
= START
;
2476 /* Nothing to do. */
2483 /* We are outputting a single character
2484 (< options->repeat_count_threshold). */
2488 /* We were outputting some other type of content, so we
2489 must output and a comma and a quote. */
2491 obstack_grow_wstr (obstack
, LCST (", "));
2492 obstack_grow (obstack
, &wide_quote_char
, sizeof (gdb_wchar_t
));
2494 /* Output the character. */
2495 for (j
= 0; j
< elem
->repeat_count
; ++j
)
2497 if (elem
->result
== wchar_iterate_ok
)
2498 print_wchar (elem
->chars
[0], elem
->buf
, elem
->buflen
, width
,
2499 byte_order
, obstack
, quote_char
, &need_escape
);
2501 print_wchar (gdb_WEOF
, elem
->buf
, elem
->buflen
, width
,
2502 byte_order
, obstack
, quote_char
, &need_escape
);
2511 /* We are outputting a character with a repeat count
2512 greater than options->repeat_count_threshold. */
2516 /* We were outputting a single string. Terminate the
2518 obstack_grow (obstack
, &wide_quote_char
, sizeof (gdb_wchar_t
));
2521 obstack_grow_wstr (obstack
, LCST (", "));
2523 /* Output the character and repeat string. */
2524 obstack_grow_wstr (obstack
, LCST ("'"));
2525 if (elem
->result
== wchar_iterate_ok
)
2526 print_wchar (elem
->chars
[0], elem
->buf
, elem
->buflen
, width
,
2527 byte_order
, obstack
, quote_char
, &need_escape
);
2529 print_wchar (gdb_WEOF
, elem
->buf
, elem
->buflen
, width
,
2530 byte_order
, obstack
, quote_char
, &need_escape
);
2531 obstack_grow_wstr (obstack
, LCST ("'"));
2532 std::string s
= string_printf (_(" <repeats %u times>"),
2533 elem
->repeat_count
);
2534 for (j
= 0; s
[j
]; ++j
)
2536 gdb_wchar_t w
= gdb_btowc (s
[j
]);
2537 obstack_grow (obstack
, &w
, sizeof (gdb_wchar_t
));
2543 /* We are outputting an incomplete sequence. */
2546 /* If we were outputting a string of SINGLE characters,
2547 terminate the quote. */
2548 obstack_grow (obstack
, &wide_quote_char
, sizeof (gdb_wchar_t
));
2551 obstack_grow_wstr (obstack
, LCST (", "));
2553 /* Output the incomplete sequence string. */
2554 obstack_grow_wstr (obstack
, LCST ("<incomplete sequence "));
2555 print_wchar (gdb_WEOF
, elem
->buf
, elem
->buflen
, width
, byte_order
,
2556 obstack
, 0, &need_escape
);
2557 obstack_grow_wstr (obstack
, LCST (">"));
2559 /* We do not attempt to output anything after this. */
2564 /* All done. If we were outputting a string of SINGLE
2565 characters, the string must be terminated. Otherwise,
2566 REPEAT and INCOMPLETE are always left properly terminated. */
2568 obstack_grow (obstack
, &wide_quote_char
, sizeof (gdb_wchar_t
));
2573 /* Get the next element and state. */
2575 if (state
!= FINISH
)
2577 elem
= &chars
[idx
++];
2578 switch (elem
->result
)
2580 case wchar_iterate_ok
:
2581 case wchar_iterate_invalid
:
2582 if (elem
->repeat_count
> options
->repeat_count_threshold
)
2588 case wchar_iterate_incomplete
:
2592 case wchar_iterate_eof
:
2600 /* Print the character string STRING, printing at most LENGTH
2601 characters. LENGTH is -1 if the string is nul terminated. TYPE is
2602 the type of each character. OPTIONS holds the printing options;
2603 printing stops early if the number hits print_max; repeat counts
2604 are printed as appropriate. Print ellipses at the end if we had to
2605 stop before printing LENGTH characters, or if FORCE_ELLIPSES.
2606 QUOTE_CHAR is the character to print at each end of the string. If
2607 C_STYLE_TERMINATOR is true, and the last character is 0, then it is
2611 generic_printstr (struct ui_file
*stream
, struct type
*type
,
2612 const gdb_byte
*string
, unsigned int length
,
2613 const char *encoding
, int force_ellipses
,
2614 int quote_char
, int c_style_terminator
,
2615 const struct value_print_options
*options
)
2617 enum bfd_endian byte_order
= type_byte_order (type
);
2619 int width
= TYPE_LENGTH (type
);
2621 struct converted_character
*last
;
2625 unsigned long current_char
= 1;
2627 for (i
= 0; current_char
; ++i
)
2630 current_char
= extract_unsigned_integer (string
+ i
* width
,
2636 /* If the string was not truncated due to `set print elements', and
2637 the last byte of it is a null, we don't print that, in
2638 traditional C style. */
2639 if (c_style_terminator
2642 && (extract_unsigned_integer (string
+ (length
- 1) * width
,
2643 width
, byte_order
) == 0))
2648 fputs_filtered ("\"\"", stream
);
2652 /* Arrange to iterate over the characters, in wchar_t form. */
2653 wchar_iterator
iter (string
, length
* width
, encoding
, width
);
2654 std::vector
<converted_character
> converted_chars
;
2656 /* Convert characters until the string is over or the maximum
2657 number of printed characters has been reached. */
2659 while (i
< options
->print_max
)
2665 /* Grab the next character and repeat count. */
2666 r
= count_next_character (&iter
, &converted_chars
);
2668 /* If less than zero, the end of the input string was reached. */
2672 /* Otherwise, add the count to the total print count and get
2673 the next character. */
2677 /* Get the last element and determine if the entire string was
2679 last
= &converted_chars
.back ();
2680 finished
= (last
->result
== wchar_iterate_eof
);
2682 /* Ensure that CONVERTED_CHARS is terminated. */
2683 last
->result
= wchar_iterate_eof
;
2685 /* WCHAR_BUF is the obstack we use to represent the string in
2687 auto_obstack wchar_buf
;
2689 /* Print the output string to the obstack. */
2690 print_converted_chars_to_obstack (&wchar_buf
, converted_chars
, quote_char
,
2691 width
, byte_order
, options
);
2693 if (force_ellipses
|| !finished
)
2694 obstack_grow_wstr (&wchar_buf
, LCST ("..."));
2696 /* OUTPUT is where we collect `char's for printing. */
2697 auto_obstack output
;
2699 convert_between_encodings (INTERMEDIATE_ENCODING
, host_charset (),
2700 (gdb_byte
*) obstack_base (&wchar_buf
),
2701 obstack_object_size (&wchar_buf
),
2702 sizeof (gdb_wchar_t
), &output
, translit_char
);
2703 obstack_1grow (&output
, '\0');
2705 fputs_filtered ((const char *) obstack_base (&output
), stream
);
2708 /* Print a string from the inferior, starting at ADDR and printing up to LEN
2709 characters, of WIDTH bytes a piece, to STREAM. If LEN is -1, printing
2710 stops at the first null byte, otherwise printing proceeds (including null
2711 bytes) until either print_max or LEN characters have been printed,
2712 whichever is smaller. ENCODING is the name of the string's
2713 encoding. It can be NULL, in which case the target encoding is
2717 val_print_string (struct type
*elttype
, const char *encoding
,
2718 CORE_ADDR addr
, int len
,
2719 struct ui_file
*stream
,
2720 const struct value_print_options
*options
)
2722 int force_ellipsis
= 0; /* Force ellipsis to be printed if nonzero. */
2723 int err
; /* Non-zero if we got a bad read. */
2724 int found_nul
; /* Non-zero if we found the nul char. */
2725 unsigned int fetchlimit
; /* Maximum number of chars to print. */
2727 gdb::unique_xmalloc_ptr
<gdb_byte
> buffer
; /* Dynamically growable fetch buffer. */
2728 struct gdbarch
*gdbarch
= elttype
->arch ();
2729 enum bfd_endian byte_order
= type_byte_order (elttype
);
2730 int width
= TYPE_LENGTH (elttype
);
2732 /* First we need to figure out the limit on the number of characters we are
2733 going to attempt to fetch and print. This is actually pretty simple. If
2734 LEN >= zero, then the limit is the minimum of LEN and print_max. If
2735 LEN is -1, then the limit is print_max. This is true regardless of
2736 whether print_max is zero, UINT_MAX (unlimited), or something in between,
2737 because finding the null byte (or available memory) is what actually
2738 limits the fetch. */
2740 fetchlimit
= (len
== -1 ? options
->print_max
: std::min ((unsigned) len
,
2741 options
->print_max
));
2743 err
= read_string (addr
, len
, width
, fetchlimit
, byte_order
,
2744 &buffer
, &bytes_read
);
2748 /* We now have either successfully filled the buffer to fetchlimit,
2749 or terminated early due to an error or finding a null char when
2752 /* Determine found_nul by looking at the last character read. */
2754 if (bytes_read
>= width
)
2755 found_nul
= extract_unsigned_integer (buffer
.get () + bytes_read
- width
,
2756 width
, byte_order
) == 0;
2757 if (len
== -1 && !found_nul
)
2761 /* We didn't find a NUL terminator we were looking for. Attempt
2762 to peek at the next character. If not successful, or it is not
2763 a null byte, then force ellipsis to be printed. */
2765 peekbuf
= (gdb_byte
*) alloca (width
);
2767 if (target_read_memory (addr
, peekbuf
, width
) == 0
2768 && extract_unsigned_integer (peekbuf
, width
, byte_order
) != 0)
2771 else if ((len
>= 0 && err
!= 0) || (len
> bytes_read
/ width
))
2773 /* Getting an error when we have a requested length, or fetching less
2774 than the number of characters actually requested, always make us
2779 /* If we get an error before fetching anything, don't print a string.
2780 But if we fetch something and then get an error, print the string
2781 and then the error message. */
2782 if (err
== 0 || bytes_read
> 0)
2784 LA_PRINT_STRING (stream
, elttype
, buffer
.get (), bytes_read
/ width
,
2785 encoding
, force_ellipsis
, options
);
2790 std::string str
= memory_error_message (TARGET_XFER_E_IO
, gdbarch
, addr
);
2792 fprintf_filtered (stream
, _("<error: %ps>"),
2793 styled_string (metadata_style
.style (),
2797 return (bytes_read
/ width
);
2800 /* Handle 'show print max-depth'. */
2803 show_print_max_depth (struct ui_file
*file
, int from_tty
,
2804 struct cmd_list_element
*c
, const char *value
)
2806 fprintf_filtered (file
, _("Maximum print depth is %s.\n"), value
);
2810 /* The 'set input-radix' command writes to this auxiliary variable.
2811 If the requested radix is valid, INPUT_RADIX is updated; otherwise,
2812 it is left unchanged. */
2814 static unsigned input_radix_1
= 10;
2816 /* Validate an input or output radix setting, and make sure the user
2817 knows what they really did here. Radix setting is confusing, e.g.
2818 setting the input radix to "10" never changes it! */
2821 set_input_radix (const char *args
, int from_tty
, struct cmd_list_element
*c
)
2823 set_input_radix_1 (from_tty
, input_radix_1
);
2827 set_input_radix_1 (int from_tty
, unsigned radix
)
2829 /* We don't currently disallow any input radix except 0 or 1, which don't
2830 make any mathematical sense. In theory, we can deal with any input
2831 radix greater than 1, even if we don't have unique digits for every
2832 value from 0 to radix-1, but in practice we lose on large radix values.
2833 We should either fix the lossage or restrict the radix range more.
2838 input_radix_1
= input_radix
;
2839 error (_("Nonsense input radix ``decimal %u''; input radix unchanged."),
2842 input_radix_1
= input_radix
= radix
;
2845 printf_filtered (_("Input radix now set to "
2846 "decimal %u, hex %x, octal %o.\n"),
2847 radix
, radix
, radix
);
2851 /* The 'set output-radix' command writes to this auxiliary variable.
2852 If the requested radix is valid, OUTPUT_RADIX is updated,
2853 otherwise, it is left unchanged. */
2855 static unsigned output_radix_1
= 10;
2858 set_output_radix (const char *args
, int from_tty
, struct cmd_list_element
*c
)
2860 set_output_radix_1 (from_tty
, output_radix_1
);
2864 set_output_radix_1 (int from_tty
, unsigned radix
)
2866 /* Validate the radix and disallow ones that we aren't prepared to
2867 handle correctly, leaving the radix unchanged. */
2871 user_print_options
.output_format
= 'x'; /* hex */
2874 user_print_options
.output_format
= 0; /* decimal */
2877 user_print_options
.output_format
= 'o'; /* octal */
2880 output_radix_1
= output_radix
;
2881 error (_("Unsupported output radix ``decimal %u''; "
2882 "output radix unchanged."),
2885 output_radix_1
= output_radix
= radix
;
2888 printf_filtered (_("Output radix now set to "
2889 "decimal %u, hex %x, octal %o.\n"),
2890 radix
, radix
, radix
);
2894 /* Set both the input and output radix at once. Try to set the output radix
2895 first, since it has the most restrictive range. An radix that is valid as
2896 an output radix is also valid as an input radix.
2898 It may be useful to have an unusual input radix. If the user wishes to
2899 set an input radix that is not valid as an output radix, he needs to use
2900 the 'set input-radix' command. */
2903 set_radix (const char *arg
, int from_tty
)
2907 radix
= (arg
== NULL
) ? 10 : parse_and_eval_long (arg
);
2908 set_output_radix_1 (0, radix
);
2909 set_input_radix_1 (0, radix
);
2912 printf_filtered (_("Input and output radices now set to "
2913 "decimal %u, hex %x, octal %o.\n"),
2914 radix
, radix
, radix
);
2918 /* Show both the input and output radices. */
2921 show_radix (const char *arg
, int from_tty
)
2925 if (input_radix
== output_radix
)
2927 printf_filtered (_("Input and output radices set to "
2928 "decimal %u, hex %x, octal %o.\n"),
2929 input_radix
, input_radix
, input_radix
);
2933 printf_filtered (_("Input radix set to decimal "
2934 "%u, hex %x, octal %o.\n"),
2935 input_radix
, input_radix
, input_radix
);
2936 printf_filtered (_("Output radix set to decimal "
2937 "%u, hex %x, octal %o.\n"),
2938 output_radix
, output_radix
, output_radix
);
2944 /* Controls printing of vtbl's. */
2946 show_vtblprint (struct ui_file
*file
, int from_tty
,
2947 struct cmd_list_element
*c
, const char *value
)
2949 fprintf_filtered (file
, _("\
2950 Printing of C++ virtual function tables is %s.\n"),
2954 /* Controls looking up an object's derived type using what we find in
2957 show_objectprint (struct ui_file
*file
, int from_tty
,
2958 struct cmd_list_element
*c
,
2961 fprintf_filtered (file
, _("\
2962 Printing of object's derived type based on vtable info is %s.\n"),
2967 show_static_field_print (struct ui_file
*file
, int from_tty
,
2968 struct cmd_list_element
*c
,
2971 fprintf_filtered (file
,
2972 _("Printing of C++ static members is %s.\n"),
2978 /* A couple typedefs to make writing the options a bit more
2980 using boolean_option_def
2981 = gdb::option::boolean_option_def
<value_print_options
>;
2982 using uinteger_option_def
2983 = gdb::option::uinteger_option_def
<value_print_options
>;
2984 using zuinteger_unlimited_option_def
2985 = gdb::option::zuinteger_unlimited_option_def
<value_print_options
>;
2987 /* Definitions of options for the "print" and "compile print"
2989 static const gdb::option::option_def value_print_option_defs
[] = {
2991 boolean_option_def
{
2993 [] (value_print_options
*opt
) { return &opt
->addressprint
; },
2994 show_addressprint
, /* show_cmd_cb */
2995 N_("Set printing of addresses."),
2996 N_("Show printing of addresses."),
2997 NULL
, /* help_doc */
3000 boolean_option_def
{
3002 [] (value_print_options
*opt
) { return &opt
->prettyformat_arrays
; },
3003 show_prettyformat_arrays
, /* show_cmd_cb */
3004 N_("Set pretty formatting of arrays."),
3005 N_("Show pretty formatting of arrays."),
3006 NULL
, /* help_doc */
3009 boolean_option_def
{
3011 [] (value_print_options
*opt
) { return &opt
->print_array_indexes
; },
3012 show_print_array_indexes
, /* show_cmd_cb */
3013 N_("Set printing of array indexes."),
3014 N_("Show printing of array indexes."),
3015 NULL
, /* help_doc */
3018 uinteger_option_def
{
3020 [] (value_print_options
*opt
) { return &opt
->print_max
; },
3021 show_print_max
, /* show_cmd_cb */
3022 N_("Set limit on string chars or array elements to print."),
3023 N_("Show limit on string chars or array elements to print."),
3024 N_("\"unlimited\" causes there to be no limit."),
3027 zuinteger_unlimited_option_def
{
3029 [] (value_print_options
*opt
) { return &opt
->max_depth
; },
3030 show_print_max_depth
, /* show_cmd_cb */
3031 N_("Set maximum print depth for nested structures, unions and arrays."),
3032 N_("Show maximum print depth for nested structures, unions, and arrays."),
3033 N_("When structures, unions, or arrays are nested beyond this depth then they\n\
3034 will be replaced with either '{...}' or '(...)' depending on the language.\n\
3035 Use \"unlimited\" to print the complete structure.")
3038 boolean_option_def
{
3039 "memory-tag-violations",
3040 [] (value_print_options
*opt
) { return &opt
->memory_tag_violations
; },
3041 show_memory_tag_violations
, /* show_cmd_cb */
3042 N_("Set printing of memory tag violations for pointers."),
3043 N_("Show printing of memory tag violations for pointers."),
3044 N_("Issue a warning when the printed value is a pointer\n\
3045 whose logical tag doesn't match the allocation tag of the memory\n\
3046 location it points to."),
3049 boolean_option_def
{
3051 [] (value_print_options
*opt
) { return &opt
->stop_print_at_null
; },
3052 show_stop_print_at_null
, /* show_cmd_cb */
3053 N_("Set printing of char arrays to stop at first null char."),
3054 N_("Show printing of char arrays to stop at first null char."),
3055 NULL
, /* help_doc */
3058 boolean_option_def
{
3060 [] (value_print_options
*opt
) { return &opt
->objectprint
; },
3061 show_objectprint
, /* show_cmd_cb */
3062 _("Set printing of C++ virtual function tables."),
3063 _("Show printing of C++ virtual function tables."),
3064 NULL
, /* help_doc */
3067 boolean_option_def
{
3069 [] (value_print_options
*opt
) { return &opt
->prettyformat_structs
; },
3070 show_prettyformat_structs
, /* show_cmd_cb */
3071 N_("Set pretty formatting of structures."),
3072 N_("Show pretty formatting of structures."),
3073 NULL
, /* help_doc */
3076 boolean_option_def
{
3078 [] (value_print_options
*opt
) { return &opt
->raw
; },
3079 NULL
, /* show_cmd_cb */
3080 N_("Set whether to print values in raw form."),
3081 N_("Show whether to print values in raw form."),
3082 N_("If set, values are printed in raw form, bypassing any\n\
3083 pretty-printers for that value.")
3086 uinteger_option_def
{
3088 [] (value_print_options
*opt
) { return &opt
->repeat_count_threshold
; },
3089 show_repeat_count_threshold
, /* show_cmd_cb */
3090 N_("Set threshold for repeated print elements."),
3091 N_("Show threshold for repeated print elements."),
3092 N_("\"unlimited\" causes all elements to be individually printed."),
3095 boolean_option_def
{
3097 [] (value_print_options
*opt
) { return &opt
->static_field_print
; },
3098 show_static_field_print
, /* show_cmd_cb */
3099 N_("Set printing of C++ static members."),
3100 N_("Show printing of C++ static members."),
3101 NULL
, /* help_doc */
3104 boolean_option_def
{
3106 [] (value_print_options
*opt
) { return &opt
->symbol_print
; },
3107 show_symbol_print
, /* show_cmd_cb */
3108 N_("Set printing of symbol names when printing pointers."),
3109 N_("Show printing of symbol names when printing pointers."),
3110 NULL
, /* help_doc */
3113 boolean_option_def
{
3115 [] (value_print_options
*opt
) { return &opt
->unionprint
; },
3116 show_unionprint
, /* show_cmd_cb */
3117 N_("Set printing of unions interior to structures."),
3118 N_("Show printing of unions interior to structures."),
3119 NULL
, /* help_doc */
3122 boolean_option_def
{
3124 [] (value_print_options
*opt
) { return &opt
->vtblprint
; },
3125 show_vtblprint
, /* show_cmd_cb */
3126 N_("Set printing of C++ virtual function tables."),
3127 N_("Show printing of C++ virtual function tables."),
3128 NULL
, /* help_doc */
3132 /* See valprint.h. */
3134 gdb::option::option_def_group
3135 make_value_print_options_def_group (value_print_options
*opts
)
3137 return {{value_print_option_defs
}, opts
};
3140 void _initialize_valprint ();
3142 _initialize_valprint ()
3144 cmd_list_element
*cmd
;
3146 add_basic_prefix_cmd ("print", no_class
,
3147 _("Generic command for setting how things print."),
3148 &setprintlist
, "set print ", 0, &setlist
);
3149 add_alias_cmd ("p", "print", no_class
, 1, &setlist
);
3150 /* Prefer set print to set prompt. */
3151 add_alias_cmd ("pr", "print", no_class
, 1, &setlist
);
3153 add_show_prefix_cmd ("print", no_class
,
3154 _("Generic command for showing print settings."),
3155 &showprintlist
, "show print ", 0, &showlist
);
3156 add_alias_cmd ("p", "print", no_class
, 1, &showlist
);
3157 add_alias_cmd ("pr", "print", no_class
, 1, &showlist
);
3159 cmd
= add_basic_prefix_cmd ("raw", no_class
,
3161 Generic command for setting what things to print in \"raw\" mode."),
3162 &setprintrawlist
, "set print raw ", 0,
3164 deprecate_cmd (cmd
, nullptr);
3166 cmd
= add_show_prefix_cmd ("raw", no_class
,
3167 _("Generic command for showing \"print raw\" settings."),
3168 &showprintrawlist
, "show print raw ", 0,
3170 deprecate_cmd (cmd
, nullptr);
3172 gdb::option::add_setshow_cmds_for_options
3173 (class_support
, &user_print_options
, value_print_option_defs
,
3174 &setprintlist
, &showprintlist
);
3176 add_setshow_zuinteger_cmd ("input-radix", class_support
, &input_radix_1
,
3178 Set default input radix for entering numbers."), _("\
3179 Show default input radix for entering numbers."), NULL
,
3182 &setlist
, &showlist
);
3184 add_setshow_zuinteger_cmd ("output-radix", class_support
, &output_radix_1
,
3186 Set default output radix for printing of values."), _("\
3187 Show default output radix for printing of values."), NULL
,
3190 &setlist
, &showlist
);
3192 /* The "set radix" and "show radix" commands are special in that
3193 they are like normal set and show commands but allow two normally
3194 independent variables to be either set or shown with a single
3195 command. So the usual deprecated_add_set_cmd() and [deleted]
3196 add_show_from_set() commands aren't really appropriate. */
3197 /* FIXME: i18n: With the new add_setshow_integer command, that is no
3198 longer true - show can display anything. */
3199 add_cmd ("radix", class_support
, set_radix
, _("\
3200 Set default input and output number radices.\n\
3201 Use 'set input-radix' or 'set output-radix' to independently set each.\n\
3202 Without an argument, sets both radices back to the default value of 10."),
3204 add_cmd ("radix", class_support
, show_radix
, _("\
3205 Show the default input and output number radices.\n\
3206 Use 'show input-radix' or 'show output-radix' to independently show each."),