1 /* Print values for GDB, the GNU debugger.
3 Copyright (C) 1986-2020 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"
44 /* Maximum number of wchars returned from wchar_iterate. */
47 /* A convenience macro to compute the size of a wchar_t buffer containing X
49 #define WCHAR_BUFLEN(X) ((X) * sizeof (gdb_wchar_t))
51 /* Character buffer size saved while iterating over wchars. */
52 #define WCHAR_BUFLEN_MAX WCHAR_BUFLEN (MAX_WCHARS)
54 /* A structure to encapsulate state information from iterated
55 character conversions. */
56 struct converted_character
58 /* The number of characters converted. */
61 /* The result of the conversion. See charset.h for more. */
62 enum wchar_iterate_result result
;
64 /* The (saved) converted character(s). */
65 gdb_wchar_t chars
[WCHAR_BUFLEN_MAX
];
67 /* The first converted target byte. */
70 /* The number of bytes converted. */
73 /* How many times this character(s) is repeated. */
77 /* Command lists for set/show print raw. */
78 struct cmd_list_element
*setprintrawlist
;
79 struct cmd_list_element
*showprintrawlist
;
81 /* Prototypes for local functions */
83 static int partial_memory_read (CORE_ADDR memaddr
, gdb_byte
*myaddr
,
84 int len
, int *errptr
);
86 static void set_input_radix_1 (int, unsigned);
88 static void set_output_radix_1 (int, unsigned);
90 static void val_print_type_code_flags (struct type
*type
,
91 const gdb_byte
*valaddr
,
92 struct ui_file
*stream
);
94 #define PRINT_MAX_DEFAULT 200 /* Start print_max off at this value. */
95 #define PRINT_MAX_DEPTH_DEFAULT 20 /* Start print_max_depth off at this value. */
97 struct value_print_options user_print_options
=
99 Val_prettyformat_default
, /* prettyformat */
100 0, /* prettyformat_arrays */
101 0, /* prettyformat_structs */
104 1, /* addressprint */
106 PRINT_MAX_DEFAULT
, /* print_max */
107 10, /* repeat_count_threshold */
108 0, /* output_format */
110 0, /* stop_print_at_null */
111 0, /* print_array_indexes */
113 1, /* static_field_print */
114 1, /* pascal_static_field_print */
117 1, /* symbol_print */
118 PRINT_MAX_DEPTH_DEFAULT
, /* max_depth */
122 /* Initialize *OPTS to be a copy of the user print options. */
124 get_user_print_options (struct value_print_options
*opts
)
126 *opts
= user_print_options
;
129 /* Initialize *OPTS to be a copy of the user print options, but with
130 pretty-formatting disabled. */
132 get_no_prettyformat_print_options (struct value_print_options
*opts
)
134 *opts
= user_print_options
;
135 opts
->prettyformat
= Val_no_prettyformat
;
138 /* Initialize *OPTS to be a copy of the user print options, but using
139 FORMAT as the formatting option. */
141 get_formatted_print_options (struct value_print_options
*opts
,
144 *opts
= user_print_options
;
145 opts
->format
= format
;
149 show_print_max (struct ui_file
*file
, int from_tty
,
150 struct cmd_list_element
*c
, const char *value
)
152 fprintf_filtered (file
,
153 _("Limit on string chars or array "
154 "elements to print is %s.\n"),
159 /* Default input and output radixes, and output format letter. */
161 unsigned input_radix
= 10;
163 show_input_radix (struct ui_file
*file
, int from_tty
,
164 struct cmd_list_element
*c
, const char *value
)
166 fprintf_filtered (file
,
167 _("Default input radix for entering numbers is %s.\n"),
171 unsigned output_radix
= 10;
173 show_output_radix (struct ui_file
*file
, int from_tty
,
174 struct cmd_list_element
*c
, const char *value
)
176 fprintf_filtered (file
,
177 _("Default output radix for printing of values is %s.\n"),
181 /* By default we print arrays without printing the index of each element in
182 the array. This behavior can be changed by setting PRINT_ARRAY_INDEXES. */
185 show_print_array_indexes (struct ui_file
*file
, int from_tty
,
186 struct cmd_list_element
*c
, const char *value
)
188 fprintf_filtered (file
, _("Printing of array indexes is %s.\n"), value
);
191 /* Print repeat counts if there are more than this many repetitions of an
192 element in an array. Referenced by the low level language dependent
196 show_repeat_count_threshold (struct ui_file
*file
, int from_tty
,
197 struct cmd_list_element
*c
, const char *value
)
199 fprintf_filtered (file
, _("Threshold for repeated print elements is %s.\n"),
203 /* If nonzero, stops printing of char arrays at first null. */
206 show_stop_print_at_null (struct ui_file
*file
, int from_tty
,
207 struct cmd_list_element
*c
, const char *value
)
209 fprintf_filtered (file
,
210 _("Printing of char arrays to stop "
211 "at first null char is %s.\n"),
215 /* Controls pretty printing of structures. */
218 show_prettyformat_structs (struct ui_file
*file
, int from_tty
,
219 struct cmd_list_element
*c
, const char *value
)
221 fprintf_filtered (file
, _("Pretty formatting of structures is %s.\n"), value
);
224 /* Controls pretty printing of arrays. */
227 show_prettyformat_arrays (struct ui_file
*file
, int from_tty
,
228 struct cmd_list_element
*c
, const char *value
)
230 fprintf_filtered (file
, _("Pretty formatting of arrays is %s.\n"), value
);
233 /* If nonzero, causes unions inside structures or other unions to be
237 show_unionprint (struct ui_file
*file
, int from_tty
,
238 struct cmd_list_element
*c
, const char *value
)
240 fprintf_filtered (file
,
241 _("Printing of unions interior to structures is %s.\n"),
245 /* If nonzero, causes machine addresses to be printed in certain contexts. */
248 show_addressprint (struct ui_file
*file
, int from_tty
,
249 struct cmd_list_element
*c
, const char *value
)
251 fprintf_filtered (file
, _("Printing of addresses is %s.\n"), value
);
255 show_symbol_print (struct ui_file
*file
, int from_tty
,
256 struct cmd_list_element
*c
, const char *value
)
258 fprintf_filtered (file
,
259 _("Printing of symbols when printing pointers is %s.\n"),
265 /* A helper function for val_print. When printing in "summary" mode,
266 we want to print scalar arguments, but not aggregate arguments.
267 This function distinguishes between the two. */
270 val_print_scalar_type_p (struct type
*type
)
272 type
= check_typedef (type
);
273 while (TYPE_IS_REFERENCE (type
))
275 type
= TYPE_TARGET_TYPE (type
);
276 type
= check_typedef (type
);
278 switch (TYPE_CODE (type
))
280 case TYPE_CODE_ARRAY
:
281 case TYPE_CODE_STRUCT
:
282 case TYPE_CODE_UNION
:
284 case TYPE_CODE_STRING
:
291 /* A helper function for val_print. When printing with limited depth we
292 want to print string and scalar arguments, but not aggregate arguments.
293 This function distinguishes between the two. */
296 val_print_scalar_or_string_type_p (struct type
*type
,
297 const struct language_defn
*language
)
299 return (val_print_scalar_type_p (type
)
300 || language
->la_is_string_type_p (type
));
303 /* See its definition in value.h. */
306 valprint_check_validity (struct ui_file
*stream
,
308 LONGEST embedded_offset
,
309 const struct value
*val
)
311 type
= check_typedef (type
);
313 if (type_not_associated (type
))
315 val_print_not_associated (stream
);
319 if (type_not_allocated (type
))
321 val_print_not_allocated (stream
);
325 if (TYPE_CODE (type
) != TYPE_CODE_UNION
326 && TYPE_CODE (type
) != TYPE_CODE_STRUCT
327 && TYPE_CODE (type
) != TYPE_CODE_ARRAY
)
329 if (value_bits_any_optimized_out (val
,
330 TARGET_CHAR_BIT
* embedded_offset
,
331 TARGET_CHAR_BIT
* TYPE_LENGTH (type
)))
333 val_print_optimized_out (val
, stream
);
337 if (value_bits_synthetic_pointer (val
, TARGET_CHAR_BIT
* embedded_offset
,
338 TARGET_CHAR_BIT
* TYPE_LENGTH (type
)))
340 const int is_ref
= TYPE_CODE (type
) == TYPE_CODE_REF
;
341 int ref_is_addressable
= 0;
345 const struct value
*deref_val
= coerce_ref_if_computed (val
);
347 if (deref_val
!= NULL
)
348 ref_is_addressable
= value_lval_const (deref_val
) == lval_memory
;
351 if (!is_ref
|| !ref_is_addressable
)
352 fputs_styled (_("<synthetic pointer>"), metadata_style
.style (),
355 /* C++ references should be valid even if they're synthetic. */
359 if (!value_bytes_available (val
, embedded_offset
, TYPE_LENGTH (type
)))
361 val_print_unavailable (stream
);
370 val_print_optimized_out (const struct value
*val
, struct ui_file
*stream
)
372 if (val
!= NULL
&& value_lval_const (val
) == lval_register
)
373 val_print_not_saved (stream
);
375 fprintf_styled (stream
, metadata_style
.style (), _("<optimized out>"));
379 val_print_not_saved (struct ui_file
*stream
)
381 fprintf_styled (stream
, metadata_style
.style (), _("<not saved>"));
385 val_print_unavailable (struct ui_file
*stream
)
387 fprintf_styled (stream
, metadata_style
.style (), _("<unavailable>"));
391 val_print_invalid_address (struct ui_file
*stream
)
393 fprintf_styled (stream
, metadata_style
.style (), _("<invalid address>"));
396 /* Print a pointer based on the type of its target.
398 Arguments to this functions are roughly the same as those in
399 generic_val_print. A difference is that ADDRESS is the address to print,
400 with embedded_offset already added. ELTTYPE represents
401 the pointed type after check_typedef. */
404 print_unpacked_pointer (struct type
*type
, struct type
*elttype
,
405 CORE_ADDR address
, struct ui_file
*stream
,
406 const struct value_print_options
*options
)
408 struct gdbarch
*gdbarch
= get_type_arch (type
);
410 if (TYPE_CODE (elttype
) == TYPE_CODE_FUNC
)
412 /* Try to print what function it points to. */
413 print_function_pointer_address (options
, gdbarch
, address
, stream
);
417 if (options
->symbol_print
)
418 print_address_demangle (options
, gdbarch
, address
, stream
, demangle
);
419 else if (options
->addressprint
)
420 fputs_filtered (paddress (gdbarch
, address
), stream
);
423 /* generic_val_print helper for TYPE_CODE_ARRAY. */
426 generic_val_print_array (struct type
*type
,
427 int embedded_offset
, CORE_ADDR address
,
428 struct ui_file
*stream
, int recurse
,
429 struct value
*original_value
,
430 const struct value_print_options
*options
,
432 generic_val_print_decorations
*decorations
)
434 struct type
*unresolved_elttype
= TYPE_TARGET_TYPE (type
);
435 struct type
*elttype
= check_typedef (unresolved_elttype
);
437 if (TYPE_LENGTH (type
) > 0 && TYPE_LENGTH (unresolved_elttype
) > 0)
439 LONGEST low_bound
, high_bound
;
441 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
442 error (_("Could not determine the array high bound"));
444 if (options
->prettyformat_arrays
)
446 print_spaces_filtered (2 + 2 * recurse
, stream
);
449 fputs_filtered (decorations
->array_start
, stream
);
450 val_print_array_elements (type
, embedded_offset
,
452 recurse
, original_value
, options
, 0);
453 fputs_filtered (decorations
->array_end
, stream
);
457 /* Array of unspecified length: treat like pointer to first elt. */
458 print_unpacked_pointer (type
, elttype
, address
+ embedded_offset
, stream
,
464 /* generic_val_print helper for TYPE_CODE_PTR. */
467 generic_val_print_ptr (struct type
*type
,
468 int embedded_offset
, struct ui_file
*stream
,
469 struct value
*original_value
,
470 const struct value_print_options
*options
)
472 struct gdbarch
*gdbarch
= get_type_arch (type
);
473 int unit_size
= gdbarch_addressable_memory_unit_size (gdbarch
);
475 if (options
->format
&& options
->format
!= 's')
477 val_print_scalar_formatted (type
, embedded_offset
,
478 original_value
, options
, 0, stream
);
482 struct type
*unresolved_elttype
= TYPE_TARGET_TYPE(type
);
483 struct type
*elttype
= check_typedef (unresolved_elttype
);
484 const gdb_byte
*valaddr
= value_contents_for_printing (original_value
);
485 CORE_ADDR addr
= unpack_pointer (type
,
486 valaddr
+ embedded_offset
* unit_size
);
488 print_unpacked_pointer (type
, elttype
, addr
, stream
, options
);
493 /* generic_val_print helper for TYPE_CODE_MEMBERPTR. */
496 generic_val_print_memberptr (struct type
*type
,
497 int embedded_offset
, struct ui_file
*stream
,
498 struct value
*original_value
,
499 const struct value_print_options
*options
)
501 val_print_scalar_formatted (type
, embedded_offset
,
502 original_value
, options
, 0, stream
);
505 /* Print '@' followed by the address contained in ADDRESS_BUFFER. */
508 print_ref_address (struct type
*type
, const gdb_byte
*address_buffer
,
509 int embedded_offset
, struct ui_file
*stream
)
511 struct gdbarch
*gdbarch
= get_type_arch (type
);
513 if (address_buffer
!= NULL
)
516 = extract_typed_address (address_buffer
+ embedded_offset
, type
);
518 fprintf_filtered (stream
, "@");
519 fputs_filtered (paddress (gdbarch
, address
), stream
);
521 /* Else: we have a non-addressable value, such as a DW_AT_const_value. */
524 /* If VAL is addressable, return the value contents buffer of a value that
525 represents a pointer to VAL. Otherwise return NULL. */
527 static const gdb_byte
*
528 get_value_addr_contents (struct value
*deref_val
)
530 gdb_assert (deref_val
!= NULL
);
532 if (value_lval_const (deref_val
) == lval_memory
)
533 return value_contents_for_printing_const (value_addr (deref_val
));
536 /* We have a non-addressable value, such as a DW_AT_const_value. */
541 /* generic_val_print helper for TYPE_CODE_{RVALUE_,}REF. */
544 generic_val_print_ref (struct type
*type
,
545 int embedded_offset
, struct ui_file
*stream
, int recurse
,
546 struct value
*original_value
,
547 const struct value_print_options
*options
)
549 struct type
*elttype
= check_typedef (TYPE_TARGET_TYPE (type
));
550 struct value
*deref_val
= NULL
;
551 const int value_is_synthetic
552 = value_bits_synthetic_pointer (original_value
,
553 TARGET_CHAR_BIT
* embedded_offset
,
554 TARGET_CHAR_BIT
* TYPE_LENGTH (type
));
555 const int must_coerce_ref
= ((options
->addressprint
&& value_is_synthetic
)
556 || options
->deref_ref
);
557 const int type_is_defined
= TYPE_CODE (elttype
) != TYPE_CODE_UNDEF
;
558 const gdb_byte
*valaddr
= value_contents_for_printing (original_value
);
560 if (must_coerce_ref
&& type_is_defined
)
562 deref_val
= coerce_ref_if_computed (original_value
);
564 if (deref_val
!= NULL
)
566 /* More complicated computed references are not supported. */
567 gdb_assert (embedded_offset
== 0);
570 deref_val
= value_at (TYPE_TARGET_TYPE (type
),
571 unpack_pointer (type
, valaddr
+ embedded_offset
));
573 /* Else, original_value isn't a synthetic reference or we don't have to print
574 the reference's contents.
576 Notice that for references to TYPE_CODE_STRUCT, 'set print object on' will
577 cause original_value to be a not_lval instead of an lval_computed,
578 which will make value_bits_synthetic_pointer return false.
579 This happens because if options->objectprint is true, c_value_print will
580 overwrite original_value's contents with the result of coercing
581 the reference through value_addr, and then set its type back to
582 TYPE_CODE_REF. In that case we don't have to coerce the reference again;
583 we can simply treat it as non-synthetic and move on. */
585 if (options
->addressprint
)
587 const gdb_byte
*address
= (value_is_synthetic
&& type_is_defined
588 ? get_value_addr_contents (deref_val
)
591 print_ref_address (type
, address
, embedded_offset
, stream
);
593 if (options
->deref_ref
)
594 fputs_filtered (": ", stream
);
597 if (options
->deref_ref
)
600 common_val_print (deref_val
, stream
, recurse
, options
,
603 fputs_filtered ("???", stream
);
607 /* Helper function for generic_val_print_enum.
608 This is also used to print enums in TYPE_CODE_FLAGS values. */
611 generic_val_print_enum_1 (struct type
*type
, LONGEST val
,
612 struct ui_file
*stream
)
617 len
= TYPE_NFIELDS (type
);
618 for (i
= 0; i
< len
; i
++)
621 if (val
== TYPE_FIELD_ENUMVAL (type
, i
))
628 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
630 else if (TYPE_FLAG_ENUM (type
))
634 /* We have a "flag" enum, so we try to decompose it into pieces as
635 appropriate. The enum may have multiple enumerators representing
636 the same bit, in which case we choose to only print the first one
638 for (i
= 0; i
< len
; ++i
)
642 ULONGEST enumval
= TYPE_FIELD_ENUMVAL (type
, i
);
643 int nbits
= count_one_bits_ll (enumval
);
645 gdb_assert (nbits
== 0 || nbits
== 1);
647 if ((val
& enumval
) != 0)
651 fputs_filtered ("(", stream
);
655 fputs_filtered (" | ", stream
);
657 val
&= ~TYPE_FIELD_ENUMVAL (type
, i
);
658 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
664 /* There are leftover bits, print them. */
666 fputs_filtered ("(", stream
);
668 fputs_filtered (" | ", stream
);
670 fputs_filtered ("unknown: 0x", stream
);
671 print_longest (stream
, 'x', 0, val
);
672 fputs_filtered (")", stream
);
676 /* Nothing has been printed and the value is 0, the enum value must
678 fputs_filtered ("0", stream
);
682 /* Something has been printed, close the parenthesis. */
683 fputs_filtered (")", stream
);
687 print_longest (stream
, 'd', 0, val
);
690 /* generic_val_print helper for TYPE_CODE_ENUM. */
693 generic_val_print_enum (struct type
*type
,
694 int embedded_offset
, struct ui_file
*stream
,
695 struct value
*original_value
,
696 const struct value_print_options
*options
)
699 struct gdbarch
*gdbarch
= get_type_arch (type
);
700 int unit_size
= gdbarch_addressable_memory_unit_size (gdbarch
);
704 val_print_scalar_formatted (type
, embedded_offset
,
705 original_value
, options
, 0, stream
);
709 const gdb_byte
*valaddr
= value_contents_for_printing (original_value
);
711 val
= unpack_long (type
, valaddr
+ embedded_offset
* unit_size
);
713 generic_val_print_enum_1 (type
, val
, stream
);
717 /* generic_val_print helper for TYPE_CODE_FLAGS. */
720 generic_val_print_flags (struct type
*type
,
721 int embedded_offset
, struct ui_file
*stream
,
722 struct value
*original_value
,
723 const struct value_print_options
*options
)
727 val_print_scalar_formatted (type
, embedded_offset
, original_value
,
731 const gdb_byte
*valaddr
= value_contents_for_printing (original_value
);
733 val_print_type_code_flags (type
, valaddr
+ embedded_offset
, stream
);
737 /* generic_val_print helper for TYPE_CODE_FUNC and TYPE_CODE_METHOD. */
740 generic_val_print_func (struct type
*type
,
741 int embedded_offset
, CORE_ADDR address
,
742 struct ui_file
*stream
,
743 struct value
*original_value
,
744 const struct value_print_options
*options
)
746 struct gdbarch
*gdbarch
= get_type_arch (type
);
750 val_print_scalar_formatted (type
, embedded_offset
,
751 original_value
, options
, 0, stream
);
755 /* FIXME, we should consider, at least for ANSI C language,
756 eliminating the distinction made between FUNCs and POINTERs
758 fprintf_filtered (stream
, "{");
759 type_print (type
, "", stream
, -1);
760 fprintf_filtered (stream
, "} ");
761 /* Try to print what function it points to, and its address. */
762 print_address_demangle (options
, gdbarch
, address
, stream
, demangle
);
766 /* generic_val_print helper for TYPE_CODE_BOOL. */
769 generic_val_print_bool (struct type
*type
,
770 int embedded_offset
, struct ui_file
*stream
,
771 struct value
*original_value
,
772 const struct value_print_options
*options
,
773 const struct generic_val_print_decorations
*decorations
)
776 struct gdbarch
*gdbarch
= get_type_arch (type
);
777 int unit_size
= gdbarch_addressable_memory_unit_size (gdbarch
);
779 if (options
->format
|| options
->output_format
)
781 struct value_print_options opts
= *options
;
782 opts
.format
= (options
->format
? options
->format
783 : options
->output_format
);
784 val_print_scalar_formatted (type
, embedded_offset
,
785 original_value
, &opts
, 0, stream
);
789 const gdb_byte
*valaddr
= value_contents_for_printing (original_value
);
791 val
= unpack_long (type
, valaddr
+ embedded_offset
* unit_size
);
793 fputs_filtered (decorations
->false_name
, stream
);
795 fputs_filtered (decorations
->true_name
, stream
);
797 print_longest (stream
, 'd', 0, val
);
801 /* generic_val_print helper for TYPE_CODE_INT. */
804 generic_val_print_int (struct type
*type
,
805 int embedded_offset
, struct ui_file
*stream
,
806 struct value
*original_value
,
807 const struct value_print_options
*options
)
809 struct value_print_options opts
= *options
;
811 opts
.format
= (options
->format
? options
->format
812 : options
->output_format
);
813 val_print_scalar_formatted (type
, embedded_offset
,
814 original_value
, &opts
, 0, stream
);
817 /* generic_val_print helper for TYPE_CODE_CHAR. */
820 generic_val_print_char (struct type
*type
, struct type
*unresolved_type
,
822 struct ui_file
*stream
,
823 struct value
*original_value
,
824 const struct value_print_options
*options
)
827 struct gdbarch
*gdbarch
= get_type_arch (type
);
828 int unit_size
= gdbarch_addressable_memory_unit_size (gdbarch
);
830 if (options
->format
|| options
->output_format
)
832 struct value_print_options opts
= *options
;
834 opts
.format
= (options
->format
? options
->format
835 : options
->output_format
);
836 val_print_scalar_formatted (type
, embedded_offset
,
837 original_value
, &opts
, 0, stream
);
841 const gdb_byte
*valaddr
= value_contents_for_printing (original_value
);
843 val
= unpack_long (type
, valaddr
+ embedded_offset
* unit_size
);
844 if (TYPE_UNSIGNED (type
))
845 fprintf_filtered (stream
, "%u", (unsigned int) val
);
847 fprintf_filtered (stream
, "%d", (int) val
);
848 fputs_filtered (" ", stream
);
849 LA_PRINT_CHAR (val
, unresolved_type
, stream
);
853 /* generic_val_print helper for TYPE_CODE_FLT and TYPE_CODE_DECFLOAT. */
856 generic_val_print_float (struct type
*type
,
857 int embedded_offset
, struct ui_file
*stream
,
858 struct value
*original_value
,
859 const struct value_print_options
*options
)
861 struct gdbarch
*gdbarch
= get_type_arch (type
);
862 int unit_size
= gdbarch_addressable_memory_unit_size (gdbarch
);
866 val_print_scalar_formatted (type
, embedded_offset
,
867 original_value
, options
, 0, stream
);
871 const gdb_byte
*valaddr
= value_contents_for_printing (original_value
);
873 print_floating (valaddr
+ embedded_offset
* unit_size
, type
, stream
);
877 /* generic_val_print helper for TYPE_CODE_COMPLEX. */
880 generic_val_print_complex (struct type
*type
,
881 int embedded_offset
, struct ui_file
*stream
,
882 struct value
*original_value
,
883 const struct value_print_options
*options
,
884 const struct generic_val_print_decorations
887 struct gdbarch
*gdbarch
= get_type_arch (type
);
888 int unit_size
= gdbarch_addressable_memory_unit_size (gdbarch
);
889 const gdb_byte
*valaddr
= value_contents_for_printing (original_value
);
891 fprintf_filtered (stream
, "%s", decorations
->complex_prefix
);
893 val_print_scalar_formatted (TYPE_TARGET_TYPE (type
),
894 embedded_offset
, original_value
, options
, 0,
897 print_floating (valaddr
+ embedded_offset
* unit_size
,
898 TYPE_TARGET_TYPE (type
), stream
);
899 fprintf_filtered (stream
, "%s", decorations
->complex_infix
);
901 val_print_scalar_formatted (TYPE_TARGET_TYPE (type
),
903 + type_length_units (TYPE_TARGET_TYPE (type
)),
904 original_value
, options
, 0, stream
);
906 print_floating (valaddr
+ embedded_offset
* unit_size
907 + TYPE_LENGTH (TYPE_TARGET_TYPE (type
)),
908 TYPE_TARGET_TYPE (type
), stream
);
909 fprintf_filtered (stream
, "%s", decorations
->complex_suffix
);
912 /* A generic val_print that is suitable for use by language
913 implementations of the la_val_print method. This function can
914 handle most type codes, though not all, notably exception
915 TYPE_CODE_UNION and TYPE_CODE_STRUCT, which must be implemented by
918 Most arguments are as to val_print.
920 The additional DECORATIONS argument can be used to customize the
921 output in some small, language-specific ways. */
924 generic_val_print (struct type
*type
,
925 int embedded_offset
, CORE_ADDR address
,
926 struct ui_file
*stream
, int recurse
,
927 struct value
*original_value
,
928 const struct value_print_options
*options
,
929 const struct generic_val_print_decorations
*decorations
)
931 struct type
*unresolved_type
= type
;
933 type
= check_typedef (type
);
934 switch (TYPE_CODE (type
))
936 case TYPE_CODE_ARRAY
:
937 generic_val_print_array (type
, embedded_offset
, address
, stream
,
938 recurse
, original_value
, options
, decorations
);
941 case TYPE_CODE_MEMBERPTR
:
942 generic_val_print_memberptr (type
, embedded_offset
, stream
,
943 original_value
, options
);
947 generic_val_print_ptr (type
, embedded_offset
, stream
,
948 original_value
, options
);
952 case TYPE_CODE_RVALUE_REF
:
953 generic_val_print_ref (type
, embedded_offset
, stream
, recurse
,
954 original_value
, options
);
958 generic_val_print_enum (type
, embedded_offset
, stream
,
959 original_value
, options
);
962 case TYPE_CODE_FLAGS
:
963 generic_val_print_flags (type
, embedded_offset
, stream
,
964 original_value
, options
);
968 case TYPE_CODE_METHOD
:
969 generic_val_print_func (type
, embedded_offset
, address
, stream
,
970 original_value
, options
);
974 generic_val_print_bool (type
, embedded_offset
, stream
,
975 original_value
, options
, decorations
);
978 case TYPE_CODE_RANGE
:
979 /* FIXME: create_static_range_type does not set the unsigned bit in a
980 range type (I think it probably should copy it from the
981 target type), so we won't print values which are too large to
982 fit in a signed integer correctly. */
983 /* FIXME: Doesn't handle ranges of enums correctly. (Can't just
984 print with the target type, though, because the size of our
985 type and the target type might differ). */
990 generic_val_print_int (type
, embedded_offset
, stream
,
991 original_value
, options
);
995 generic_val_print_char (type
, unresolved_type
, embedded_offset
,
996 stream
, original_value
, options
);
1000 case TYPE_CODE_DECFLOAT
:
1001 generic_val_print_float (type
, embedded_offset
, stream
,
1002 original_value
, options
);
1005 case TYPE_CODE_VOID
:
1006 fputs_filtered (decorations
->void_name
, stream
);
1009 case TYPE_CODE_ERROR
:
1010 fprintf_filtered (stream
, "%s", TYPE_ERROR_NAME (type
));
1013 case TYPE_CODE_UNDEF
:
1014 /* This happens (without TYPE_STUB set) on systems which don't use
1015 dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
1016 and no complete type for struct foo in that file. */
1017 fprintf_styled (stream
, metadata_style
.style (), _("<incomplete type>"));
1020 case TYPE_CODE_COMPLEX
:
1021 generic_val_print_complex (type
, embedded_offset
, stream
,
1022 original_value
, options
, decorations
);
1025 case TYPE_CODE_UNION
:
1026 case TYPE_CODE_STRUCT
:
1027 case TYPE_CODE_METHODPTR
:
1029 error (_("Unhandled type code %d in symbol table."),
1034 /* Print using the given LANGUAGE the data of type TYPE located at
1035 VAL's contents buffer + EMBEDDED_OFFSET (within GDB), which came
1036 from the inferior at address ADDRESS + EMBEDDED_OFFSET, onto
1037 stdio stream STREAM according to OPTIONS. VAL is the whole object
1038 that came from ADDRESS.
1040 The language printers will pass down an adjusted EMBEDDED_OFFSET to
1041 further helper subroutines as subfields of TYPE are printed. In
1042 such cases, VAL is passed down unadjusted, so
1043 that VAL can be queried for metadata about the contents data being
1044 printed, using EMBEDDED_OFFSET as an offset into VAL's contents
1045 buffer. For example: "has this field been optimized out", or "I'm
1046 printing an object while inspecting a traceframe; has this
1047 particular piece of data been collected?".
1049 RECURSE indicates the amount of indentation to supply before
1050 continuation lines; this amount is roughly twice the value of
1054 val_print (struct type
*type
, LONGEST embedded_offset
,
1055 CORE_ADDR address
, struct ui_file
*stream
, int recurse
,
1057 const struct value_print_options
*options
,
1058 const struct language_defn
*language
)
1061 struct value_print_options local_opts
= *options
;
1062 struct type
*real_type
= check_typedef (type
);
1064 if (local_opts
.prettyformat
== Val_prettyformat_default
)
1065 local_opts
.prettyformat
= (local_opts
.prettyformat_structs
1066 ? Val_prettyformat
: Val_no_prettyformat
);
1070 /* Ensure that the type is complete and not just a stub. If the type is
1071 only a stub and we can't find and substitute its complete type, then
1072 print appropriate string and return. */
1074 if (TYPE_STUB (real_type
))
1076 fprintf_styled (stream
, metadata_style
.style (), _("<incomplete type>"));
1080 if (!valprint_check_validity (stream
, real_type
, embedded_offset
, val
))
1085 ret
= apply_ext_lang_val_pretty_printer (type
, embedded_offset
,
1086 address
, stream
, recurse
,
1087 val
, options
, language
);
1092 /* Handle summary mode. If the value is a scalar, print it;
1093 otherwise, print an ellipsis. */
1094 if (options
->summary
&& !val_print_scalar_type_p (type
))
1096 fprintf_filtered (stream
, "...");
1100 /* If this value is too deep then don't print it. */
1101 if (!val_print_scalar_or_string_type_p (type
, language
)
1102 && val_print_check_max_depth (stream
, recurse
, options
, language
))
1107 language
->la_val_print (type
, embedded_offset
, address
,
1108 stream
, recurse
, val
,
1111 catch (const gdb_exception_error
&except
)
1113 fprintf_styled (stream
, metadata_style
.style (),
1114 _("<error reading variable>"));
1118 /* See valprint.h. */
1121 val_print_check_max_depth (struct ui_file
*stream
, int recurse
,
1122 const struct value_print_options
*options
,
1123 const struct language_defn
*language
)
1125 if (options
->max_depth
> -1 && recurse
>= options
->max_depth
)
1127 gdb_assert (language
->la_struct_too_deep_ellipsis
!= NULL
);
1128 fputs_filtered (language
->la_struct_too_deep_ellipsis
, stream
);
1135 /* Check whether the value VAL is printable. Return 1 if it is;
1136 return 0 and print an appropriate error message to STREAM according to
1137 OPTIONS if it is not. */
1140 value_check_printable (struct value
*val
, struct ui_file
*stream
,
1141 const struct value_print_options
*options
)
1145 fprintf_styled (stream
, metadata_style
.style (),
1146 _("<address of value unknown>"));
1150 if (value_entirely_optimized_out (val
))
1152 if (options
->summary
&& !val_print_scalar_type_p (value_type (val
)))
1153 fprintf_filtered (stream
, "...");
1155 val_print_optimized_out (val
, stream
);
1159 if (value_entirely_unavailable (val
))
1161 if (options
->summary
&& !val_print_scalar_type_p (value_type (val
)))
1162 fprintf_filtered (stream
, "...");
1164 val_print_unavailable (stream
);
1168 if (TYPE_CODE (value_type (val
)) == TYPE_CODE_INTERNAL_FUNCTION
)
1170 fprintf_styled (stream
, metadata_style
.style (),
1171 _("<internal function %s>"),
1172 value_internal_function_name (val
));
1176 if (type_not_associated (value_type (val
)))
1178 val_print_not_associated (stream
);
1182 if (type_not_allocated (value_type (val
)))
1184 val_print_not_allocated (stream
);
1191 /* Print using the given LANGUAGE the value VAL onto stream STREAM according
1194 This is a preferable interface to val_print, above, because it uses
1195 GDB's value mechanism. */
1198 common_val_print (struct value
*val
, struct ui_file
*stream
, int recurse
,
1199 const struct value_print_options
*options
,
1200 const struct language_defn
*language
)
1202 if (!value_check_printable (val
, stream
, options
))
1205 if (language
->la_language
== language_ada
)
1206 /* The value might have a dynamic type, which would cause trouble
1207 below when trying to extract the value contents (since the value
1208 size is determined from the type size which is unknown). So
1209 get a fixed representation of our value. */
1210 val
= ada_to_fixed_value (val
);
1212 if (value_lazy (val
))
1213 value_fetch_lazy (val
);
1215 val_print (value_type (val
),
1216 value_embedded_offset (val
), value_address (val
),
1218 val
, options
, language
);
1221 /* Print on stream STREAM the value VAL according to OPTIONS. The value
1222 is printed using the current_language syntax. */
1225 value_print (struct value
*val
, struct ui_file
*stream
,
1226 const struct value_print_options
*options
)
1228 if (!value_check_printable (val
, stream
, options
))
1234 = apply_ext_lang_val_pretty_printer (value_type (val
),
1235 value_embedded_offset (val
),
1236 value_address (val
),
1238 val
, options
, current_language
);
1244 LA_VALUE_PRINT (val
, stream
, options
);
1248 val_print_type_code_flags (struct type
*type
, const gdb_byte
*valaddr
,
1249 struct ui_file
*stream
)
1251 ULONGEST val
= unpack_long (type
, valaddr
);
1252 int field
, nfields
= TYPE_NFIELDS (type
);
1253 struct gdbarch
*gdbarch
= get_type_arch (type
);
1254 struct type
*bool_type
= builtin_type (gdbarch
)->builtin_bool
;
1256 fputs_filtered ("[", stream
);
1257 for (field
= 0; field
< nfields
; field
++)
1259 if (TYPE_FIELD_NAME (type
, field
)[0] != '\0')
1261 struct type
*field_type
= TYPE_FIELD_TYPE (type
, field
);
1263 if (field_type
== bool_type
1264 /* We require boolean types here to be one bit wide. This is a
1265 problematic place to notify the user of an internal error
1266 though. Instead just fall through and print the field as an
1268 && TYPE_FIELD_BITSIZE (type
, field
) == 1)
1270 if (val
& ((ULONGEST
)1 << TYPE_FIELD_BITPOS (type
, field
)))
1271 fprintf_filtered (stream
, " %s",
1272 TYPE_FIELD_NAME (type
, field
));
1276 unsigned field_len
= TYPE_FIELD_BITSIZE (type
, field
);
1278 = val
>> (TYPE_FIELD_BITPOS (type
, field
) - field_len
+ 1);
1280 if (field_len
< sizeof (ULONGEST
) * TARGET_CHAR_BIT
)
1281 field_val
&= ((ULONGEST
) 1 << field_len
) - 1;
1282 fprintf_filtered (stream
, " %s=",
1283 TYPE_FIELD_NAME (type
, field
));
1284 if (TYPE_CODE (field_type
) == TYPE_CODE_ENUM
)
1285 generic_val_print_enum_1 (field_type
, field_val
, stream
);
1287 print_longest (stream
, 'd', 0, field_val
);
1291 fputs_filtered (" ]", stream
);
1294 /* Print a scalar of data of type TYPE, pointed to in GDB by VALADDR,
1295 according to OPTIONS and SIZE on STREAM. Format i is not supported
1298 This is how the elements of an array or structure are printed
1302 val_print_scalar_formatted (struct type
*type
,
1303 LONGEST embedded_offset
,
1305 const struct value_print_options
*options
,
1307 struct ui_file
*stream
)
1309 struct gdbarch
*arch
= get_type_arch (type
);
1310 int unit_size
= gdbarch_addressable_memory_unit_size (arch
);
1312 gdb_assert (val
!= NULL
);
1314 /* If we get here with a string format, try again without it. Go
1315 all the way back to the language printers, which may call us
1317 if (options
->format
== 's')
1319 struct value_print_options opts
= *options
;
1322 val_print (type
, embedded_offset
, 0, stream
, 0, val
, &opts
,
1327 /* value_contents_for_printing fetches all VAL's contents. They are
1328 needed to check whether VAL is optimized-out or unavailable
1330 const gdb_byte
*valaddr
= value_contents_for_printing (val
);
1332 /* A scalar object that does not have all bits available can't be
1333 printed, because all bits contribute to its representation. */
1334 if (value_bits_any_optimized_out (val
,
1335 TARGET_CHAR_BIT
* embedded_offset
,
1336 TARGET_CHAR_BIT
* TYPE_LENGTH (type
)))
1337 val_print_optimized_out (val
, stream
);
1338 else if (!value_bytes_available (val
, embedded_offset
, TYPE_LENGTH (type
)))
1339 val_print_unavailable (stream
);
1341 print_scalar_formatted (valaddr
+ embedded_offset
* unit_size
, type
,
1342 options
, size
, stream
);
1345 /* Print a number according to FORMAT which is one of d,u,x,o,b,h,w,g.
1346 The raison d'etre of this function is to consolidate printing of
1347 LONG_LONG's into this one function. The format chars b,h,w,g are
1348 from print_scalar_formatted(). Numbers are printed using C
1351 USE_C_FORMAT means to use C format in all cases. Without it,
1352 'o' and 'x' format do not include the standard C radix prefix
1355 Hilfinger/2004-09-09: USE_C_FORMAT was originally called USE_LOCAL
1356 and was intended to request formatting according to the current
1357 language and would be used for most integers that GDB prints. The
1358 exceptional cases were things like protocols where the format of
1359 the integer is a protocol thing, not a user-visible thing). The
1360 parameter remains to preserve the information of what things might
1361 be printed with language-specific format, should we ever resurrect
1365 print_longest (struct ui_file
*stream
, int format
, int use_c_format
,
1373 val
= int_string (val_long
, 10, 1, 0, 1); break;
1375 val
= int_string (val_long
, 10, 0, 0, 1); break;
1377 val
= int_string (val_long
, 16, 0, 0, use_c_format
); break;
1379 val
= int_string (val_long
, 16, 0, 2, 1); break;
1381 val
= int_string (val_long
, 16, 0, 4, 1); break;
1383 val
= int_string (val_long
, 16, 0, 8, 1); break;
1385 val
= int_string (val_long
, 16, 0, 16, 1); break;
1388 val
= int_string (val_long
, 8, 0, 0, use_c_format
); break;
1390 internal_error (__FILE__
, __LINE__
,
1391 _("failed internal consistency check"));
1393 fputs_filtered (val
, stream
);
1396 /* This used to be a macro, but I don't think it is called often enough
1397 to merit such treatment. */
1398 /* Convert a LONGEST to an int. This is used in contexts (e.g. number of
1399 arguments to a function, number in a value history, register number, etc.)
1400 where the value must not be larger than can fit in an int. */
1403 longest_to_int (LONGEST arg
)
1405 /* Let the compiler do the work. */
1406 int rtnval
= (int) arg
;
1408 /* Check for overflows or underflows. */
1409 if (sizeof (LONGEST
) > sizeof (int))
1413 error (_("Value out of range."));
1419 /* Print a floating point value of floating-point type TYPE,
1420 pointed to in GDB by VALADDR, on STREAM. */
1423 print_floating (const gdb_byte
*valaddr
, struct type
*type
,
1424 struct ui_file
*stream
)
1426 std::string str
= target_float_to_string (valaddr
, type
);
1427 fputs_filtered (str
.c_str (), stream
);
1431 print_binary_chars (struct ui_file
*stream
, const gdb_byte
*valaddr
,
1432 unsigned len
, enum bfd_endian byte_order
, bool zero_pad
)
1437 bool seen_a_one
= false;
1439 /* Declared "int" so it will be signed.
1440 This ensures that right shift will shift in zeros. */
1442 const int mask
= 0x080;
1444 if (byte_order
== BFD_ENDIAN_BIG
)
1450 /* Every byte has 8 binary characters; peel off
1451 and print from the MSB end. */
1453 for (i
= 0; i
< (HOST_CHAR_BIT
* sizeof (*p
)); i
++)
1455 if (*p
& (mask
>> i
))
1460 if (zero_pad
|| seen_a_one
|| b
== '1')
1461 fputc_filtered (b
, stream
);
1469 for (p
= valaddr
+ len
- 1;
1473 for (i
= 0; i
< (HOST_CHAR_BIT
* sizeof (*p
)); i
++)
1475 if (*p
& (mask
>> i
))
1480 if (zero_pad
|| seen_a_one
|| b
== '1')
1481 fputc_filtered (b
, stream
);
1488 /* When not zero-padding, ensure that something is printed when the
1490 if (!zero_pad
&& !seen_a_one
)
1491 fputc_filtered ('0', stream
);
1494 /* A helper for print_octal_chars that emits a single octal digit,
1495 optionally suppressing it if is zero and updating SEEN_A_ONE. */
1498 emit_octal_digit (struct ui_file
*stream
, bool *seen_a_one
, int digit
)
1500 if (*seen_a_one
|| digit
!= 0)
1501 fprintf_filtered (stream
, "%o", digit
);
1506 /* VALADDR points to an integer of LEN bytes.
1507 Print it in octal on stream or format it in buf. */
1510 print_octal_chars (struct ui_file
*stream
, const gdb_byte
*valaddr
,
1511 unsigned len
, enum bfd_endian byte_order
)
1514 unsigned char octa1
, octa2
, octa3
, carry
;
1517 /* Octal is 3 bits, which doesn't fit. Yuk. So we have to track
1518 * the extra bits, which cycle every three bytes:
1520 * Byte side: 0 1 2 3
1522 * bit number 123 456 78 | 9 012 345 6 | 78 901 234 | 567 890 12 |
1524 * Octal side: 0 1 carry 3 4 carry ...
1526 * Cycle number: 0 1 2
1528 * But of course we are printing from the high side, so we have to
1529 * figure out where in the cycle we are so that we end up with no
1530 * left over bits at the end.
1532 #define BITS_IN_OCTAL 3
1533 #define HIGH_ZERO 0340
1534 #define LOW_ZERO 0034
1535 #define CARRY_ZERO 0003
1536 static_assert (HIGH_ZERO
+ LOW_ZERO
+ CARRY_ZERO
== 0xff,
1537 "cycle zero constants are wrong");
1538 #define HIGH_ONE 0200
1539 #define MID_ONE 0160
1540 #define LOW_ONE 0016
1541 #define CARRY_ONE 0001
1542 static_assert (HIGH_ONE
+ MID_ONE
+ LOW_ONE
+ CARRY_ONE
== 0xff,
1543 "cycle one constants are wrong");
1544 #define HIGH_TWO 0300
1545 #define MID_TWO 0070
1546 #define LOW_TWO 0007
1547 static_assert (HIGH_TWO
+ MID_TWO
+ LOW_TWO
== 0xff,
1548 "cycle two constants are wrong");
1550 /* For 32 we start in cycle 2, with two bits and one bit carry;
1551 for 64 in cycle in cycle 1, with one bit and a two bit carry. */
1553 cycle
= (len
* HOST_CHAR_BIT
) % BITS_IN_OCTAL
;
1556 fputs_filtered ("0", stream
);
1557 bool seen_a_one
= false;
1558 if (byte_order
== BFD_ENDIAN_BIG
)
1567 /* No carry in, carry out two bits. */
1569 octa1
= (HIGH_ZERO
& *p
) >> 5;
1570 octa2
= (LOW_ZERO
& *p
) >> 2;
1571 carry
= (CARRY_ZERO
& *p
);
1572 emit_octal_digit (stream
, &seen_a_one
, octa1
);
1573 emit_octal_digit (stream
, &seen_a_one
, octa2
);
1577 /* Carry in two bits, carry out one bit. */
1579 octa1
= (carry
<< 1) | ((HIGH_ONE
& *p
) >> 7);
1580 octa2
= (MID_ONE
& *p
) >> 4;
1581 octa3
= (LOW_ONE
& *p
) >> 1;
1582 carry
= (CARRY_ONE
& *p
);
1583 emit_octal_digit (stream
, &seen_a_one
, octa1
);
1584 emit_octal_digit (stream
, &seen_a_one
, octa2
);
1585 emit_octal_digit (stream
, &seen_a_one
, octa3
);
1589 /* Carry in one bit, no carry out. */
1591 octa1
= (carry
<< 2) | ((HIGH_TWO
& *p
) >> 6);
1592 octa2
= (MID_TWO
& *p
) >> 3;
1593 octa3
= (LOW_TWO
& *p
);
1595 emit_octal_digit (stream
, &seen_a_one
, octa1
);
1596 emit_octal_digit (stream
, &seen_a_one
, octa2
);
1597 emit_octal_digit (stream
, &seen_a_one
, octa3
);
1601 error (_("Internal error in octal conversion;"));
1605 cycle
= cycle
% BITS_IN_OCTAL
;
1610 for (p
= valaddr
+ len
- 1;
1617 /* Carry out, no carry in */
1619 octa1
= (HIGH_ZERO
& *p
) >> 5;
1620 octa2
= (LOW_ZERO
& *p
) >> 2;
1621 carry
= (CARRY_ZERO
& *p
);
1622 emit_octal_digit (stream
, &seen_a_one
, octa1
);
1623 emit_octal_digit (stream
, &seen_a_one
, octa2
);
1627 /* Carry in, carry out */
1629 octa1
= (carry
<< 1) | ((HIGH_ONE
& *p
) >> 7);
1630 octa2
= (MID_ONE
& *p
) >> 4;
1631 octa3
= (LOW_ONE
& *p
) >> 1;
1632 carry
= (CARRY_ONE
& *p
);
1633 emit_octal_digit (stream
, &seen_a_one
, octa1
);
1634 emit_octal_digit (stream
, &seen_a_one
, octa2
);
1635 emit_octal_digit (stream
, &seen_a_one
, octa3
);
1639 /* Carry in, no carry out */
1641 octa1
= (carry
<< 2) | ((HIGH_TWO
& *p
) >> 6);
1642 octa2
= (MID_TWO
& *p
) >> 3;
1643 octa3
= (LOW_TWO
& *p
);
1645 emit_octal_digit (stream
, &seen_a_one
, octa1
);
1646 emit_octal_digit (stream
, &seen_a_one
, octa2
);
1647 emit_octal_digit (stream
, &seen_a_one
, octa3
);
1651 error (_("Internal error in octal conversion;"));
1655 cycle
= cycle
% BITS_IN_OCTAL
;
1661 /* Possibly negate the integer represented by BYTES. It contains LEN
1662 bytes in the specified byte order. If the integer is negative,
1663 copy it into OUT_VEC, negate it, and return true. Otherwise, do
1664 nothing and return false. */
1667 maybe_negate_by_bytes (const gdb_byte
*bytes
, unsigned len
,
1668 enum bfd_endian byte_order
,
1669 gdb::byte_vector
*out_vec
)
1672 gdb_assert (len
> 0);
1673 if (byte_order
== BFD_ENDIAN_BIG
)
1674 sign_byte
= bytes
[0];
1676 sign_byte
= bytes
[len
- 1];
1677 if ((sign_byte
& 0x80) == 0)
1680 out_vec
->resize (len
);
1682 /* Compute -x == 1 + ~x. */
1683 if (byte_order
== BFD_ENDIAN_LITTLE
)
1686 for (unsigned i
= 0; i
< len
; ++i
)
1688 unsigned tem
= (0xff & ~bytes
[i
]) + carry
;
1689 (*out_vec
)[i
] = tem
& 0xff;
1696 for (unsigned i
= len
; i
> 0; --i
)
1698 unsigned tem
= (0xff & ~bytes
[i
- 1]) + carry
;
1699 (*out_vec
)[i
- 1] = tem
& 0xff;
1707 /* VALADDR points to an integer of LEN bytes.
1708 Print it in decimal on stream or format it in buf. */
1711 print_decimal_chars (struct ui_file
*stream
, const gdb_byte
*valaddr
,
1712 unsigned len
, bool is_signed
,
1713 enum bfd_endian byte_order
)
1716 #define CARRY_OUT( x ) ((x) / TEN) /* extend char to int */
1717 #define CARRY_LEFT( x ) ((x) % TEN)
1718 #define SHIFT( x ) ((x) << 4)
1719 #define LOW_NIBBLE( x ) ( (x) & 0x00F)
1720 #define HIGH_NIBBLE( x ) (((x) & 0x0F0) >> 4)
1725 int i
, j
, decimal_digits
;
1729 gdb::byte_vector negated_bytes
;
1731 && maybe_negate_by_bytes (valaddr
, len
, byte_order
, &negated_bytes
))
1733 fputs_filtered ("-", stream
);
1734 valaddr
= negated_bytes
.data ();
1737 /* Base-ten number is less than twice as many digits
1738 as the base 16 number, which is 2 digits per byte. */
1740 decimal_len
= len
* 2 * 2;
1741 std::vector
<unsigned char> digits (decimal_len
, 0);
1743 /* Ok, we have an unknown number of bytes of data to be printed in
1746 * Given a hex number (in nibbles) as XYZ, we start by taking X and
1747 * decimalizing it as "x1 x2" in two decimal nibbles. Then we multiply
1748 * the nibbles by 16, add Y and re-decimalize. Repeat with Z.
1750 * The trick is that "digits" holds a base-10 number, but sometimes
1751 * the individual digits are > 10.
1753 * Outer loop is per nibble (hex digit) of input, from MSD end to
1756 decimal_digits
= 0; /* Number of decimal digits so far */
1757 p
= (byte_order
== BFD_ENDIAN_BIG
) ? valaddr
: valaddr
+ len
- 1;
1759 while ((byte_order
== BFD_ENDIAN_BIG
) ? (p
< valaddr
+ len
) : (p
>= valaddr
))
1762 * Multiply current base-ten number by 16 in place.
1763 * Each digit was between 0 and 9, now is between
1766 for (j
= 0; j
< decimal_digits
; j
++)
1768 digits
[j
] = SHIFT (digits
[j
]);
1771 /* Take the next nibble off the input and add it to what
1772 * we've got in the LSB position. Bottom 'digit' is now
1773 * between 0 and 159.
1775 * "flip" is used to run this loop twice for each byte.
1779 /* Take top nibble. */
1781 digits
[0] += HIGH_NIBBLE (*p
);
1786 /* Take low nibble and bump our pointer "p". */
1788 digits
[0] += LOW_NIBBLE (*p
);
1789 if (byte_order
== BFD_ENDIAN_BIG
)
1796 /* Re-decimalize. We have to do this often enough
1797 * that we don't overflow, but once per nibble is
1798 * overkill. Easier this way, though. Note that the
1799 * carry is often larger than 10 (e.g. max initial
1800 * carry out of lowest nibble is 15, could bubble all
1801 * the way up greater than 10). So we have to do
1802 * the carrying beyond the last current digit.
1805 for (j
= 0; j
< decimal_len
- 1; j
++)
1809 /* "/" won't handle an unsigned char with
1810 * a value that if signed would be negative.
1811 * So extend to longword int via "dummy".
1814 carry
= CARRY_OUT (dummy
);
1815 digits
[j
] = CARRY_LEFT (dummy
);
1817 if (j
>= decimal_digits
&& carry
== 0)
1820 * All higher digits are 0 and we
1821 * no longer have a carry.
1823 * Note: "j" is 0-based, "decimal_digits" is
1826 decimal_digits
= j
+ 1;
1832 /* Ok, now "digits" is the decimal representation, with
1833 the "decimal_digits" actual digits. Print! */
1835 for (i
= decimal_digits
- 1; i
> 0 && digits
[i
] == 0; --i
)
1840 fprintf_filtered (stream
, "%1d", digits
[i
]);
1844 /* VALADDR points to an integer of LEN bytes. Print it in hex on stream. */
1847 print_hex_chars (struct ui_file
*stream
, const gdb_byte
*valaddr
,
1848 unsigned len
, enum bfd_endian byte_order
,
1853 fputs_filtered ("0x", stream
);
1854 if (byte_order
== BFD_ENDIAN_BIG
)
1860 /* Strip leading 0 bytes, but be sure to leave at least a
1861 single byte at the end. */
1862 for (; p
< valaddr
+ len
- 1 && !*p
; ++p
)
1866 const gdb_byte
*first
= p
;
1871 /* When not zero-padding, use a different format for the
1872 very first byte printed. */
1873 if (!zero_pad
&& p
== first
)
1874 fprintf_filtered (stream
, "%x", *p
);
1876 fprintf_filtered (stream
, "%02x", *p
);
1881 p
= valaddr
+ len
- 1;
1885 /* Strip leading 0 bytes, but be sure to leave at least a
1886 single byte at the end. */
1887 for (; p
>= valaddr
+ 1 && !*p
; --p
)
1891 const gdb_byte
*first
= p
;
1896 /* When not zero-padding, use a different format for the
1897 very first byte printed. */
1898 if (!zero_pad
&& p
== first
)
1899 fprintf_filtered (stream
, "%x", *p
);
1901 fprintf_filtered (stream
, "%02x", *p
);
1906 /* VALADDR points to a char integer of LEN bytes.
1907 Print it out in appropriate language form on stream.
1908 Omit any leading zero chars. */
1911 print_char_chars (struct ui_file
*stream
, struct type
*type
,
1912 const gdb_byte
*valaddr
,
1913 unsigned len
, enum bfd_endian byte_order
)
1917 if (byte_order
== BFD_ENDIAN_BIG
)
1920 while (p
< valaddr
+ len
- 1 && *p
== 0)
1923 while (p
< valaddr
+ len
)
1925 LA_EMIT_CHAR (*p
, type
, stream
, '\'');
1931 p
= valaddr
+ len
- 1;
1932 while (p
> valaddr
&& *p
== 0)
1935 while (p
>= valaddr
)
1937 LA_EMIT_CHAR (*p
, type
, stream
, '\'');
1943 /* Print function pointer with inferior address ADDRESS onto stdio
1947 print_function_pointer_address (const struct value_print_options
*options
,
1948 struct gdbarch
*gdbarch
,
1950 struct ui_file
*stream
)
1953 = gdbarch_convert_from_func_ptr_addr (gdbarch
, address
,
1954 current_top_target ());
1956 /* If the function pointer is represented by a description, print
1957 the address of the description. */
1958 if (options
->addressprint
&& func_addr
!= address
)
1960 fputs_filtered ("@", stream
);
1961 fputs_filtered (paddress (gdbarch
, address
), stream
);
1962 fputs_filtered (": ", stream
);
1964 print_address_demangle (options
, gdbarch
, func_addr
, stream
, demangle
);
1968 /* Print on STREAM using the given OPTIONS the index for the element
1969 at INDEX of an array whose index type is INDEX_TYPE. */
1972 maybe_print_array_index (struct type
*index_type
, LONGEST index
,
1973 struct ui_file
*stream
,
1974 const struct value_print_options
*options
)
1976 struct value
*index_value
;
1978 if (!options
->print_array_indexes
)
1981 index_value
= value_from_longest (index_type
, index
);
1983 LA_PRINT_ARRAY_INDEX (index_value
, stream
, options
);
1986 /* Called by various <lang>_val_print routines to print elements of an
1987 array in the form "<elem1>, <elem2>, <elem3>, ...".
1989 (FIXME?) Assumes array element separator is a comma, which is correct
1990 for all languages currently handled.
1991 (FIXME?) Some languages have a notation for repeated array elements,
1992 perhaps we should try to use that notation when appropriate. */
1995 val_print_array_elements (struct type
*type
,
1996 LONGEST embedded_offset
,
1997 CORE_ADDR address
, struct ui_file
*stream
,
2000 const struct value_print_options
*options
,
2003 unsigned int things_printed
= 0;
2005 struct type
*elttype
, *index_type
, *base_index_type
;
2007 /* Position of the array element we are examining to see
2008 whether it is repeated. */
2010 /* Number of repetitions we have detected so far. */
2012 LONGEST low_bound
, high_bound
;
2013 LONGEST low_pos
, high_pos
;
2015 elttype
= TYPE_TARGET_TYPE (type
);
2016 eltlen
= type_length_units (check_typedef (elttype
));
2017 index_type
= TYPE_INDEX_TYPE (type
);
2019 if (get_array_bounds (type
, &low_bound
, &high_bound
))
2021 if (TYPE_CODE (index_type
) == TYPE_CODE_RANGE
)
2022 base_index_type
= TYPE_TARGET_TYPE (index_type
);
2024 base_index_type
= index_type
;
2026 /* Non-contiguous enumerations types can by used as index types
2027 in some languages (e.g. Ada). In this case, the array length
2028 shall be computed from the positions of the first and last
2029 literal in the enumeration type, and not from the values
2030 of these literals. */
2031 if (!discrete_position (base_index_type
, low_bound
, &low_pos
)
2032 || !discrete_position (base_index_type
, high_bound
, &high_pos
))
2034 warning (_("unable to get positions in array, use bounds instead"));
2035 low_pos
= low_bound
;
2036 high_pos
= high_bound
;
2039 /* The array length should normally be HIGH_POS - LOW_POS + 1.
2040 But we have to be a little extra careful, because some languages
2041 such as Ada allow LOW_POS to be greater than HIGH_POS for
2042 empty arrays. In that situation, the array length is just zero,
2044 if (low_pos
> high_pos
)
2047 len
= high_pos
- low_pos
+ 1;
2051 warning (_("unable to get bounds of array, assuming null array"));
2056 annotate_array_section_begin (i
, elttype
);
2058 for (; i
< len
&& things_printed
< options
->print_max
; i
++)
2062 if (options
->prettyformat_arrays
)
2064 fprintf_filtered (stream
, ",\n");
2065 print_spaces_filtered (2 + 2 * recurse
, stream
);
2069 fprintf_filtered (stream
, ", ");
2072 wrap_here (n_spaces (2 + 2 * recurse
));
2073 maybe_print_array_index (index_type
, i
+ low_bound
,
2078 /* Only check for reps if repeat_count_threshold is not set to
2079 UINT_MAX (unlimited). */
2080 if (options
->repeat_count_threshold
< UINT_MAX
)
2083 && value_contents_eq (val
,
2084 embedded_offset
+ i
* eltlen
,
2095 if (reps
> options
->repeat_count_threshold
)
2097 val_print (elttype
, embedded_offset
+ i
* eltlen
,
2098 address
, stream
, recurse
+ 1, val
, options
,
2100 annotate_elt_rep (reps
);
2101 fprintf_filtered (stream
, " %p[<repeats %u times>%p]",
2102 metadata_style
.style ().ptr (), reps
, nullptr);
2103 annotate_elt_rep_end ();
2106 things_printed
+= options
->repeat_count_threshold
;
2110 val_print (elttype
, embedded_offset
+ i
* eltlen
,
2112 stream
, recurse
+ 1, val
, options
, current_language
);
2117 annotate_array_section_end ();
2120 fprintf_filtered (stream
, "...");
2124 /* Read LEN bytes of target memory at address MEMADDR, placing the
2125 results in GDB's memory at MYADDR. Returns a count of the bytes
2126 actually read, and optionally a target_xfer_status value in the
2127 location pointed to by ERRPTR if ERRPTR is non-null. */
2129 /* FIXME: cagney/1999-10-14: Only used by val_print_string. Can this
2130 function be eliminated. */
2133 partial_memory_read (CORE_ADDR memaddr
, gdb_byte
*myaddr
,
2134 int len
, int *errptr
)
2136 int nread
; /* Number of bytes actually read. */
2137 int errcode
; /* Error from last read. */
2139 /* First try a complete read. */
2140 errcode
= target_read_memory (memaddr
, myaddr
, len
);
2148 /* Loop, reading one byte at a time until we get as much as we can. */
2149 for (errcode
= 0, nread
= 0; len
> 0 && errcode
== 0; nread
++, len
--)
2151 errcode
= target_read_memory (memaddr
++, myaddr
++, 1);
2153 /* If an error, the last read was unsuccessful, so adjust count. */
2166 /* Read a string from the inferior, at ADDR, with LEN characters of
2167 WIDTH bytes each. Fetch at most FETCHLIMIT characters. BUFFER
2168 will be set to a newly allocated buffer containing the string, and
2169 BYTES_READ will be set to the number of bytes read. Returns 0 on
2170 success, or a target_xfer_status on failure.
2172 If LEN > 0, reads the lesser of LEN or FETCHLIMIT characters
2173 (including eventual NULs in the middle or end of the string).
2175 If LEN is -1, stops at the first null character (not necessarily
2176 the first null byte) up to a maximum of FETCHLIMIT characters. Set
2177 FETCHLIMIT to UINT_MAX to read as many characters as possible from
2180 Unless an exception is thrown, BUFFER will always be allocated, even on
2181 failure. In this case, some characters might have been read before the
2182 failure happened. Check BYTES_READ to recognize this situation.
2184 Note: There was a FIXME asking to make this code use target_read_string,
2185 but this function is more general (can read past null characters, up to
2186 given LEN). Besides, it is used much more often than target_read_string
2187 so it is more tested. Perhaps callers of target_read_string should use
2188 this function instead? */
2191 read_string (CORE_ADDR addr
, int len
, int width
, unsigned int fetchlimit
,
2192 enum bfd_endian byte_order
, gdb::unique_xmalloc_ptr
<gdb_byte
> *buffer
,
2195 int errcode
; /* Errno returned from bad reads. */
2196 unsigned int nfetch
; /* Chars to fetch / chars fetched. */
2197 gdb_byte
*bufptr
; /* Pointer to next available byte in
2200 /* Loop until we either have all the characters, or we encounter
2201 some error, such as bumping into the end of the address space. */
2203 buffer
->reset (nullptr);
2207 /* We want fetchlimit chars, so we might as well read them all in
2209 unsigned int fetchlen
= std::min ((unsigned) len
, fetchlimit
);
2211 buffer
->reset ((gdb_byte
*) xmalloc (fetchlen
* width
));
2212 bufptr
= buffer
->get ();
2214 nfetch
= partial_memory_read (addr
, bufptr
, fetchlen
* width
, &errcode
)
2216 addr
+= nfetch
* width
;
2217 bufptr
+= nfetch
* width
;
2221 unsigned long bufsize
= 0;
2222 unsigned int chunksize
; /* Size of each fetch, in chars. */
2223 int found_nul
; /* Non-zero if we found the nul char. */
2224 gdb_byte
*limit
; /* First location past end of fetch buffer. */
2227 /* We are looking for a NUL terminator to end the fetching, so we
2228 might as well read in blocks that are large enough to be efficient,
2229 but not so large as to be slow if fetchlimit happens to be large.
2230 So we choose the minimum of 8 and fetchlimit. We used to use 200
2231 instead of 8 but 200 is way too big for remote debugging over a
2233 chunksize
= std::min (8u, fetchlimit
);
2238 nfetch
= std::min ((unsigned long) chunksize
, fetchlimit
- bufsize
);
2240 if (*buffer
== NULL
)
2241 buffer
->reset ((gdb_byte
*) xmalloc (nfetch
* width
));
2243 buffer
->reset ((gdb_byte
*) xrealloc (buffer
->release (),
2244 (nfetch
+ bufsize
) * width
));
2246 bufptr
= buffer
->get () + bufsize
* width
;
2249 /* Read as much as we can. */
2250 nfetch
= partial_memory_read (addr
, bufptr
, nfetch
* width
, &errcode
)
2253 /* Scan this chunk for the null character that terminates the string
2254 to print. If found, we don't need to fetch any more. Note
2255 that bufptr is explicitly left pointing at the next character
2256 after the null character, or at the next character after the end
2259 limit
= bufptr
+ nfetch
* width
;
2260 while (bufptr
< limit
)
2264 c
= extract_unsigned_integer (bufptr
, width
, byte_order
);
2269 /* We don't care about any error which happened after
2270 the NUL terminator. */
2277 while (errcode
== 0 /* no error */
2278 && bufptr
- buffer
->get () < fetchlimit
* width
/* no overrun */
2279 && !found_nul
); /* haven't found NUL yet */
2282 { /* Length of string is really 0! */
2283 /* We always allocate *buffer. */
2284 buffer
->reset ((gdb_byte
*) xmalloc (1));
2285 bufptr
= buffer
->get ();
2289 /* bufptr and addr now point immediately beyond the last byte which we
2290 consider part of the string (including a '\0' which ends the string). */
2291 *bytes_read
= bufptr
- buffer
->get ();
2298 /* Return true if print_wchar can display W without resorting to a
2299 numeric escape, false otherwise. */
2302 wchar_printable (gdb_wchar_t w
)
2304 return (gdb_iswprint (w
)
2305 || w
== LCST ('\a') || w
== LCST ('\b')
2306 || w
== LCST ('\f') || w
== LCST ('\n')
2307 || w
== LCST ('\r') || w
== LCST ('\t')
2308 || w
== LCST ('\v'));
2311 /* A helper function that converts the contents of STRING to wide
2312 characters and then appends them to OUTPUT. */
2315 append_string_as_wide (const char *string
,
2316 struct obstack
*output
)
2318 for (; *string
; ++string
)
2320 gdb_wchar_t w
= gdb_btowc (*string
);
2321 obstack_grow (output
, &w
, sizeof (gdb_wchar_t
));
2325 /* Print a wide character W to OUTPUT. ORIG is a pointer to the
2326 original (target) bytes representing the character, ORIG_LEN is the
2327 number of valid bytes. WIDTH is the number of bytes in a base
2328 characters of the type. OUTPUT is an obstack to which wide
2329 characters are emitted. QUOTER is a (narrow) character indicating
2330 the style of quotes surrounding the character to be printed.
2331 NEED_ESCAPE is an in/out flag which is used to track numeric
2332 escapes across calls. */
2335 print_wchar (gdb_wint_t w
, const gdb_byte
*orig
,
2336 int orig_len
, int width
,
2337 enum bfd_endian byte_order
,
2338 struct obstack
*output
,
2339 int quoter
, int *need_escapep
)
2341 int need_escape
= *need_escapep
;
2345 /* iswprint implementation on Windows returns 1 for tab character.
2346 In order to avoid different printout on this host, we explicitly
2347 use wchar_printable function. */
2351 obstack_grow_wstr (output
, LCST ("\\a"));
2354 obstack_grow_wstr (output
, LCST ("\\b"));
2357 obstack_grow_wstr (output
, LCST ("\\f"));
2360 obstack_grow_wstr (output
, LCST ("\\n"));
2363 obstack_grow_wstr (output
, LCST ("\\r"));
2366 obstack_grow_wstr (output
, LCST ("\\t"));
2369 obstack_grow_wstr (output
, LCST ("\\v"));
2373 if (wchar_printable (w
) && (!need_escape
|| (!gdb_iswdigit (w
)
2375 && w
!= LCST ('9'))))
2377 gdb_wchar_t wchar
= w
;
2379 if (w
== gdb_btowc (quoter
) || w
== LCST ('\\'))
2380 obstack_grow_wstr (output
, LCST ("\\"));
2381 obstack_grow (output
, &wchar
, sizeof (gdb_wchar_t
));
2387 for (i
= 0; i
+ width
<= orig_len
; i
+= width
)
2392 value
= extract_unsigned_integer (&orig
[i
], width
,
2394 /* If the value fits in 3 octal digits, print it that
2395 way. Otherwise, print it as a hex escape. */
2397 xsnprintf (octal
, sizeof (octal
), "\\%.3o",
2398 (int) (value
& 0777));
2400 xsnprintf (octal
, sizeof (octal
), "\\x%lx", (long) value
);
2401 append_string_as_wide (octal
, output
);
2403 /* If we somehow have extra bytes, print them now. */
2404 while (i
< orig_len
)
2408 xsnprintf (octal
, sizeof (octal
), "\\%.3o", orig
[i
] & 0xff);
2409 append_string_as_wide (octal
, output
);
2420 /* Print the character C on STREAM as part of the contents of a
2421 literal string whose delimiter is QUOTER. ENCODING names the
2425 generic_emit_char (int c
, struct type
*type
, struct ui_file
*stream
,
2426 int quoter
, const char *encoding
)
2428 enum bfd_endian byte_order
2429 = type_byte_order (type
);
2431 int need_escape
= 0;
2433 c_buf
= (gdb_byte
*) alloca (TYPE_LENGTH (type
));
2434 pack_long (c_buf
, type
, c
);
2436 wchar_iterator
iter (c_buf
, TYPE_LENGTH (type
), encoding
, TYPE_LENGTH (type
));
2438 /* This holds the printable form of the wchar_t data. */
2439 auto_obstack wchar_buf
;
2445 const gdb_byte
*buf
;
2447 int print_escape
= 1;
2448 enum wchar_iterate_result result
;
2450 num_chars
= iter
.iterate (&result
, &chars
, &buf
, &buflen
);
2455 /* If all characters are printable, print them. Otherwise,
2456 we're going to have to print an escape sequence. We
2457 check all characters because we want to print the target
2458 bytes in the escape sequence, and we don't know character
2459 boundaries there. */
2463 for (i
= 0; i
< num_chars
; ++i
)
2464 if (!wchar_printable (chars
[i
]))
2472 for (i
= 0; i
< num_chars
; ++i
)
2473 print_wchar (chars
[i
], buf
, buflen
,
2474 TYPE_LENGTH (type
), byte_order
,
2475 &wchar_buf
, quoter
, &need_escape
);
2479 /* This handles the NUM_CHARS == 0 case as well. */
2481 print_wchar (gdb_WEOF
, buf
, buflen
, TYPE_LENGTH (type
),
2482 byte_order
, &wchar_buf
, quoter
, &need_escape
);
2485 /* The output in the host encoding. */
2486 auto_obstack output
;
2488 convert_between_encodings (INTERMEDIATE_ENCODING
, host_charset (),
2489 (gdb_byte
*) obstack_base (&wchar_buf
),
2490 obstack_object_size (&wchar_buf
),
2491 sizeof (gdb_wchar_t
), &output
, translit_char
);
2492 obstack_1grow (&output
, '\0');
2494 fputs_filtered ((const char *) obstack_base (&output
), stream
);
2497 /* Return the repeat count of the next character/byte in ITER,
2498 storing the result in VEC. */
2501 count_next_character (wchar_iterator
*iter
,
2502 std::vector
<converted_character
> *vec
)
2504 struct converted_character
*current
;
2508 struct converted_character tmp
;
2512 = iter
->iterate (&tmp
.result
, &chars
, &tmp
.buf
, &tmp
.buflen
);
2513 if (tmp
.num_chars
> 0)
2515 gdb_assert (tmp
.num_chars
< MAX_WCHARS
);
2516 memcpy (tmp
.chars
, chars
, tmp
.num_chars
* sizeof (gdb_wchar_t
));
2518 vec
->push_back (tmp
);
2521 current
= &vec
->back ();
2523 /* Count repeated characters or bytes. */
2524 current
->repeat_count
= 1;
2525 if (current
->num_chars
== -1)
2533 struct converted_character d
;
2540 /* Get the next character. */
2541 d
.num_chars
= iter
->iterate (&d
.result
, &chars
, &d
.buf
, &d
.buflen
);
2543 /* If a character was successfully converted, save the character
2544 into the converted character. */
2545 if (d
.num_chars
> 0)
2547 gdb_assert (d
.num_chars
< MAX_WCHARS
);
2548 memcpy (d
.chars
, chars
, WCHAR_BUFLEN (d
.num_chars
));
2551 /* Determine if the current character is the same as this
2553 if (d
.num_chars
== current
->num_chars
&& d
.result
== current
->result
)
2555 /* There are two cases to consider:
2557 1) Equality of converted character (num_chars > 0)
2558 2) Equality of non-converted character (num_chars == 0) */
2559 if ((current
->num_chars
> 0
2560 && memcmp (current
->chars
, d
.chars
,
2561 WCHAR_BUFLEN (current
->num_chars
)) == 0)
2562 || (current
->num_chars
== 0
2563 && current
->buflen
== d
.buflen
2564 && memcmp (current
->buf
, d
.buf
, current
->buflen
) == 0))
2565 ++current
->repeat_count
;
2573 /* Push this next converted character onto the result vector. */
2574 repeat
= current
->repeat_count
;
2580 /* Print the characters in CHARS to the OBSTACK. QUOTE_CHAR is the quote
2581 character to use with string output. WIDTH is the size of the output
2582 character type. BYTE_ORDER is the target byte order. OPTIONS
2583 is the user's print options. */
2586 print_converted_chars_to_obstack (struct obstack
*obstack
,
2587 const std::vector
<converted_character
> &chars
,
2588 int quote_char
, int width
,
2589 enum bfd_endian byte_order
,
2590 const struct value_print_options
*options
)
2593 const converted_character
*elem
;
2594 enum {START
, SINGLE
, REPEAT
, INCOMPLETE
, FINISH
} state
, last
;
2595 gdb_wchar_t wide_quote_char
= gdb_btowc (quote_char
);
2596 int need_escape
= 0;
2598 /* Set the start state. */
2600 last
= state
= START
;
2608 /* Nothing to do. */
2615 /* We are outputting a single character
2616 (< options->repeat_count_threshold). */
2620 /* We were outputting some other type of content, so we
2621 must output and a comma and a quote. */
2623 obstack_grow_wstr (obstack
, LCST (", "));
2624 obstack_grow (obstack
, &wide_quote_char
, sizeof (gdb_wchar_t
));
2626 /* Output the character. */
2627 for (j
= 0; j
< elem
->repeat_count
; ++j
)
2629 if (elem
->result
== wchar_iterate_ok
)
2630 print_wchar (elem
->chars
[0], elem
->buf
, elem
->buflen
, width
,
2631 byte_order
, obstack
, quote_char
, &need_escape
);
2633 print_wchar (gdb_WEOF
, elem
->buf
, elem
->buflen
, width
,
2634 byte_order
, obstack
, quote_char
, &need_escape
);
2643 /* We are outputting a character with a repeat count
2644 greater than options->repeat_count_threshold. */
2648 /* We were outputting a single string. Terminate the
2650 obstack_grow (obstack
, &wide_quote_char
, sizeof (gdb_wchar_t
));
2653 obstack_grow_wstr (obstack
, LCST (", "));
2655 /* Output the character and repeat string. */
2656 obstack_grow_wstr (obstack
, LCST ("'"));
2657 if (elem
->result
== wchar_iterate_ok
)
2658 print_wchar (elem
->chars
[0], elem
->buf
, elem
->buflen
, width
,
2659 byte_order
, obstack
, quote_char
, &need_escape
);
2661 print_wchar (gdb_WEOF
, elem
->buf
, elem
->buflen
, width
,
2662 byte_order
, obstack
, quote_char
, &need_escape
);
2663 obstack_grow_wstr (obstack
, LCST ("'"));
2664 std::string s
= string_printf (_(" <repeats %u times>"),
2665 elem
->repeat_count
);
2666 for (j
= 0; s
[j
]; ++j
)
2668 gdb_wchar_t w
= gdb_btowc (s
[j
]);
2669 obstack_grow (obstack
, &w
, sizeof (gdb_wchar_t
));
2675 /* We are outputting an incomplete sequence. */
2678 /* If we were outputting a string of SINGLE characters,
2679 terminate the quote. */
2680 obstack_grow (obstack
, &wide_quote_char
, sizeof (gdb_wchar_t
));
2683 obstack_grow_wstr (obstack
, LCST (", "));
2685 /* Output the incomplete sequence string. */
2686 obstack_grow_wstr (obstack
, LCST ("<incomplete sequence "));
2687 print_wchar (gdb_WEOF
, elem
->buf
, elem
->buflen
, width
, byte_order
,
2688 obstack
, 0, &need_escape
);
2689 obstack_grow_wstr (obstack
, LCST (">"));
2691 /* We do not attempt to output anything after this. */
2696 /* All done. If we were outputting a string of SINGLE
2697 characters, the string must be terminated. Otherwise,
2698 REPEAT and INCOMPLETE are always left properly terminated. */
2700 obstack_grow (obstack
, &wide_quote_char
, sizeof (gdb_wchar_t
));
2705 /* Get the next element and state. */
2707 if (state
!= FINISH
)
2709 elem
= &chars
[idx
++];
2710 switch (elem
->result
)
2712 case wchar_iterate_ok
:
2713 case wchar_iterate_invalid
:
2714 if (elem
->repeat_count
> options
->repeat_count_threshold
)
2720 case wchar_iterate_incomplete
:
2724 case wchar_iterate_eof
:
2732 /* Print the character string STRING, printing at most LENGTH
2733 characters. LENGTH is -1 if the string is nul terminated. TYPE is
2734 the type of each character. OPTIONS holds the printing options;
2735 printing stops early if the number hits print_max; repeat counts
2736 are printed as appropriate. Print ellipses at the end if we had to
2737 stop before printing LENGTH characters, or if FORCE_ELLIPSES.
2738 QUOTE_CHAR is the character to print at each end of the string. If
2739 C_STYLE_TERMINATOR is true, and the last character is 0, then it is
2743 generic_printstr (struct ui_file
*stream
, struct type
*type
,
2744 const gdb_byte
*string
, unsigned int length
,
2745 const char *encoding
, int force_ellipses
,
2746 int quote_char
, int c_style_terminator
,
2747 const struct value_print_options
*options
)
2749 enum bfd_endian byte_order
= type_byte_order (type
);
2751 int width
= TYPE_LENGTH (type
);
2753 struct converted_character
*last
;
2757 unsigned long current_char
= 1;
2759 for (i
= 0; current_char
; ++i
)
2762 current_char
= extract_unsigned_integer (string
+ i
* width
,
2768 /* If the string was not truncated due to `set print elements', and
2769 the last byte of it is a null, we don't print that, in
2770 traditional C style. */
2771 if (c_style_terminator
2774 && (extract_unsigned_integer (string
+ (length
- 1) * width
,
2775 width
, byte_order
) == 0))
2780 fputs_filtered ("\"\"", stream
);
2784 /* Arrange to iterate over the characters, in wchar_t form. */
2785 wchar_iterator
iter (string
, length
* width
, encoding
, width
);
2786 std::vector
<converted_character
> converted_chars
;
2788 /* Convert characters until the string is over or the maximum
2789 number of printed characters has been reached. */
2791 while (i
< options
->print_max
)
2797 /* Grab the next character and repeat count. */
2798 r
= count_next_character (&iter
, &converted_chars
);
2800 /* If less than zero, the end of the input string was reached. */
2804 /* Otherwise, add the count to the total print count and get
2805 the next character. */
2809 /* Get the last element and determine if the entire string was
2811 last
= &converted_chars
.back ();
2812 finished
= (last
->result
== wchar_iterate_eof
);
2814 /* Ensure that CONVERTED_CHARS is terminated. */
2815 last
->result
= wchar_iterate_eof
;
2817 /* WCHAR_BUF is the obstack we use to represent the string in
2819 auto_obstack wchar_buf
;
2821 /* Print the output string to the obstack. */
2822 print_converted_chars_to_obstack (&wchar_buf
, converted_chars
, quote_char
,
2823 width
, byte_order
, options
);
2825 if (force_ellipses
|| !finished
)
2826 obstack_grow_wstr (&wchar_buf
, LCST ("..."));
2828 /* OUTPUT is where we collect `char's for printing. */
2829 auto_obstack output
;
2831 convert_between_encodings (INTERMEDIATE_ENCODING
, host_charset (),
2832 (gdb_byte
*) obstack_base (&wchar_buf
),
2833 obstack_object_size (&wchar_buf
),
2834 sizeof (gdb_wchar_t
), &output
, translit_char
);
2835 obstack_1grow (&output
, '\0');
2837 fputs_filtered ((const char *) obstack_base (&output
), stream
);
2840 /* Print a string from the inferior, starting at ADDR and printing up to LEN
2841 characters, of WIDTH bytes a piece, to STREAM. If LEN is -1, printing
2842 stops at the first null byte, otherwise printing proceeds (including null
2843 bytes) until either print_max or LEN characters have been printed,
2844 whichever is smaller. ENCODING is the name of the string's
2845 encoding. It can be NULL, in which case the target encoding is
2849 val_print_string (struct type
*elttype
, const char *encoding
,
2850 CORE_ADDR addr
, int len
,
2851 struct ui_file
*stream
,
2852 const struct value_print_options
*options
)
2854 int force_ellipsis
= 0; /* Force ellipsis to be printed if nonzero. */
2855 int err
; /* Non-zero if we got a bad read. */
2856 int found_nul
; /* Non-zero if we found the nul char. */
2857 unsigned int fetchlimit
; /* Maximum number of chars to print. */
2859 gdb::unique_xmalloc_ptr
<gdb_byte
> buffer
; /* Dynamically growable fetch buffer. */
2860 struct gdbarch
*gdbarch
= get_type_arch (elttype
);
2861 enum bfd_endian byte_order
= type_byte_order (elttype
);
2862 int width
= TYPE_LENGTH (elttype
);
2864 /* First we need to figure out the limit on the number of characters we are
2865 going to attempt to fetch and print. This is actually pretty simple. If
2866 LEN >= zero, then the limit is the minimum of LEN and print_max. If
2867 LEN is -1, then the limit is print_max. This is true regardless of
2868 whether print_max is zero, UINT_MAX (unlimited), or something in between,
2869 because finding the null byte (or available memory) is what actually
2870 limits the fetch. */
2872 fetchlimit
= (len
== -1 ? options
->print_max
: std::min ((unsigned) len
,
2873 options
->print_max
));
2875 err
= read_string (addr
, len
, width
, fetchlimit
, byte_order
,
2876 &buffer
, &bytes_read
);
2880 /* We now have either successfully filled the buffer to fetchlimit,
2881 or terminated early due to an error or finding a null char when
2884 /* Determine found_nul by looking at the last character read. */
2886 if (bytes_read
>= width
)
2887 found_nul
= extract_unsigned_integer (buffer
.get () + bytes_read
- width
,
2888 width
, byte_order
) == 0;
2889 if (len
== -1 && !found_nul
)
2893 /* We didn't find a NUL terminator we were looking for. Attempt
2894 to peek at the next character. If not successful, or it is not
2895 a null byte, then force ellipsis to be printed. */
2897 peekbuf
= (gdb_byte
*) alloca (width
);
2899 if (target_read_memory (addr
, peekbuf
, width
) == 0
2900 && extract_unsigned_integer (peekbuf
, width
, byte_order
) != 0)
2903 else if ((len
>= 0 && err
!= 0) || (len
> bytes_read
/ width
))
2905 /* Getting an error when we have a requested length, or fetching less
2906 than the number of characters actually requested, always make us
2911 /* If we get an error before fetching anything, don't print a string.
2912 But if we fetch something and then get an error, print the string
2913 and then the error message. */
2914 if (err
== 0 || bytes_read
> 0)
2916 LA_PRINT_STRING (stream
, elttype
, buffer
.get (), bytes_read
/ width
,
2917 encoding
, force_ellipsis
, options
);
2922 std::string str
= memory_error_message (TARGET_XFER_E_IO
, gdbarch
, addr
);
2924 fprintf_filtered (stream
, _("<error: %ps>"),
2925 styled_string (metadata_style
.style (),
2929 return (bytes_read
/ width
);
2932 /* Handle 'show print max-depth'. */
2935 show_print_max_depth (struct ui_file
*file
, int from_tty
,
2936 struct cmd_list_element
*c
, const char *value
)
2938 fprintf_filtered (file
, _("Maximum print depth is %s.\n"), value
);
2942 /* The 'set input-radix' command writes to this auxiliary variable.
2943 If the requested radix is valid, INPUT_RADIX is updated; otherwise,
2944 it is left unchanged. */
2946 static unsigned input_radix_1
= 10;
2948 /* Validate an input or output radix setting, and make sure the user
2949 knows what they really did here. Radix setting is confusing, e.g.
2950 setting the input radix to "10" never changes it! */
2953 set_input_radix (const char *args
, int from_tty
, struct cmd_list_element
*c
)
2955 set_input_radix_1 (from_tty
, input_radix_1
);
2959 set_input_radix_1 (int from_tty
, unsigned radix
)
2961 /* We don't currently disallow any input radix except 0 or 1, which don't
2962 make any mathematical sense. In theory, we can deal with any input
2963 radix greater than 1, even if we don't have unique digits for every
2964 value from 0 to radix-1, but in practice we lose on large radix values.
2965 We should either fix the lossage or restrict the radix range more.
2970 input_radix_1
= input_radix
;
2971 error (_("Nonsense input radix ``decimal %u''; input radix unchanged."),
2974 input_radix_1
= input_radix
= radix
;
2977 printf_filtered (_("Input radix now set to "
2978 "decimal %u, hex %x, octal %o.\n"),
2979 radix
, radix
, radix
);
2983 /* The 'set output-radix' command writes to this auxiliary variable.
2984 If the requested radix is valid, OUTPUT_RADIX is updated,
2985 otherwise, it is left unchanged. */
2987 static unsigned output_radix_1
= 10;
2990 set_output_radix (const char *args
, int from_tty
, struct cmd_list_element
*c
)
2992 set_output_radix_1 (from_tty
, output_radix_1
);
2996 set_output_radix_1 (int from_tty
, unsigned radix
)
2998 /* Validate the radix and disallow ones that we aren't prepared to
2999 handle correctly, leaving the radix unchanged. */
3003 user_print_options
.output_format
= 'x'; /* hex */
3006 user_print_options
.output_format
= 0; /* decimal */
3009 user_print_options
.output_format
= 'o'; /* octal */
3012 output_radix_1
= output_radix
;
3013 error (_("Unsupported output radix ``decimal %u''; "
3014 "output radix unchanged."),
3017 output_radix_1
= output_radix
= radix
;
3020 printf_filtered (_("Output radix now set to "
3021 "decimal %u, hex %x, octal %o.\n"),
3022 radix
, radix
, radix
);
3026 /* Set both the input and output radix at once. Try to set the output radix
3027 first, since it has the most restrictive range. An radix that is valid as
3028 an output radix is also valid as an input radix.
3030 It may be useful to have an unusual input radix. If the user wishes to
3031 set an input radix that is not valid as an output radix, he needs to use
3032 the 'set input-radix' command. */
3035 set_radix (const char *arg
, int from_tty
)
3039 radix
= (arg
== NULL
) ? 10 : parse_and_eval_long (arg
);
3040 set_output_radix_1 (0, radix
);
3041 set_input_radix_1 (0, radix
);
3044 printf_filtered (_("Input and output radices now set to "
3045 "decimal %u, hex %x, octal %o.\n"),
3046 radix
, radix
, radix
);
3050 /* Show both the input and output radices. */
3053 show_radix (const char *arg
, int from_tty
)
3057 if (input_radix
== output_radix
)
3059 printf_filtered (_("Input and output radices set to "
3060 "decimal %u, hex %x, octal %o.\n"),
3061 input_radix
, input_radix
, input_radix
);
3065 printf_filtered (_("Input radix set to decimal "
3066 "%u, hex %x, octal %o.\n"),
3067 input_radix
, input_radix
, input_radix
);
3068 printf_filtered (_("Output radix set to decimal "
3069 "%u, hex %x, octal %o.\n"),
3070 output_radix
, output_radix
, output_radix
);
3077 set_print (const char *arg
, int from_tty
)
3080 "\"set print\" must be followed by the name of a print subcommand.\n");
3081 help_list (setprintlist
, "set print ", all_commands
, gdb_stdout
);
3085 show_print (const char *args
, int from_tty
)
3087 cmd_show_list (showprintlist
, from_tty
, "");
3091 set_print_raw (const char *arg
, int from_tty
)
3094 "\"set print raw\" must be followed by the name of a \"print raw\" subcommand.\n");
3095 help_list (setprintrawlist
, "set print raw ", all_commands
, gdb_stdout
);
3099 show_print_raw (const char *args
, int from_tty
)
3101 cmd_show_list (showprintrawlist
, from_tty
, "");
3104 /* Controls printing of vtbl's. */
3106 show_vtblprint (struct ui_file
*file
, int from_tty
,
3107 struct cmd_list_element
*c
, const char *value
)
3109 fprintf_filtered (file
, _("\
3110 Printing of C++ virtual function tables is %s.\n"),
3114 /* Controls looking up an object's derived type using what we find in
3117 show_objectprint (struct ui_file
*file
, int from_tty
,
3118 struct cmd_list_element
*c
,
3121 fprintf_filtered (file
, _("\
3122 Printing of object's derived type based on vtable info is %s.\n"),
3127 show_static_field_print (struct ui_file
*file
, int from_tty
,
3128 struct cmd_list_element
*c
,
3131 fprintf_filtered (file
,
3132 _("Printing of C++ static members is %s.\n"),
3138 /* A couple typedefs to make writing the options a bit more
3140 using boolean_option_def
3141 = gdb::option::boolean_option_def
<value_print_options
>;
3142 using uinteger_option_def
3143 = gdb::option::uinteger_option_def
<value_print_options
>;
3144 using zuinteger_unlimited_option_def
3145 = gdb::option::zuinteger_unlimited_option_def
<value_print_options
>;
3147 /* Definitions of options for the "print" and "compile print"
3149 static const gdb::option::option_def value_print_option_defs
[] = {
3151 boolean_option_def
{
3153 [] (value_print_options
*opt
) { return &opt
->addressprint
; },
3154 show_addressprint
, /* show_cmd_cb */
3155 N_("Set printing of addresses."),
3156 N_("Show printing of addresses."),
3157 NULL
, /* help_doc */
3160 boolean_option_def
{
3162 [] (value_print_options
*opt
) { return &opt
->prettyformat_arrays
; },
3163 show_prettyformat_arrays
, /* show_cmd_cb */
3164 N_("Set pretty formatting of arrays."),
3165 N_("Show pretty formatting of arrays."),
3166 NULL
, /* help_doc */
3169 boolean_option_def
{
3171 [] (value_print_options
*opt
) { return &opt
->print_array_indexes
; },
3172 show_print_array_indexes
, /* show_cmd_cb */
3173 N_("Set printing of array indexes."),
3174 N_("Show printing of array indexes."),
3175 NULL
, /* help_doc */
3178 uinteger_option_def
{
3180 [] (value_print_options
*opt
) { return &opt
->print_max
; },
3181 show_print_max
, /* show_cmd_cb */
3182 N_("Set limit on string chars or array elements to print."),
3183 N_("Show limit on string chars or array elements to print."),
3184 N_("\"unlimited\" causes there to be no limit."),
3187 zuinteger_unlimited_option_def
{
3189 [] (value_print_options
*opt
) { return &opt
->max_depth
; },
3190 show_print_max_depth
, /* show_cmd_cb */
3191 N_("Set maximum print depth for nested structures, unions and arrays."),
3192 N_("Show maximum print depth for nested structures, unions, and arrays."),
3193 N_("When structures, unions, or arrays are nested beyond this depth then they\n\
3194 will be replaced with either '{...}' or '(...)' depending on the language.\n\
3195 Use \"unlimited\" to print the complete structure.")
3198 boolean_option_def
{
3200 [] (value_print_options
*opt
) { return &opt
->stop_print_at_null
; },
3201 show_stop_print_at_null
, /* show_cmd_cb */
3202 N_("Set printing of char arrays to stop at first null char."),
3203 N_("Show printing of char arrays to stop at first null char."),
3204 NULL
, /* help_doc */
3207 boolean_option_def
{
3209 [] (value_print_options
*opt
) { return &opt
->objectprint
; },
3210 show_objectprint
, /* show_cmd_cb */
3211 _("Set printing of C++ virtual function tables."),
3212 _("Show printing of C++ virtual function tables."),
3213 NULL
, /* help_doc */
3216 boolean_option_def
{
3218 [] (value_print_options
*opt
) { return &opt
->prettyformat_structs
; },
3219 show_prettyformat_structs
, /* show_cmd_cb */
3220 N_("Set pretty formatting of structures."),
3221 N_("Show pretty formatting of structures."),
3222 NULL
, /* help_doc */
3225 boolean_option_def
{
3227 [] (value_print_options
*opt
) { return &opt
->raw
; },
3228 NULL
, /* show_cmd_cb */
3229 N_("Set whether to print values in raw form."),
3230 N_("Show whether to print values in raw form."),
3231 N_("If set, values are printed in raw form, bypassing any\n\
3232 pretty-printers for that value.")
3235 uinteger_option_def
{
3237 [] (value_print_options
*opt
) { return &opt
->repeat_count_threshold
; },
3238 show_repeat_count_threshold
, /* show_cmd_cb */
3239 N_("Set threshold for repeated print elements."),
3240 N_("Show threshold for repeated print elements."),
3241 N_("\"unlimited\" causes all elements to be individually printed."),
3244 boolean_option_def
{
3246 [] (value_print_options
*opt
) { return &opt
->static_field_print
; },
3247 show_static_field_print
, /* show_cmd_cb */
3248 N_("Set printing of C++ static members."),
3249 N_("Show printing of C++ static members."),
3250 NULL
, /* help_doc */
3253 boolean_option_def
{
3255 [] (value_print_options
*opt
) { return &opt
->symbol_print
; },
3256 show_symbol_print
, /* show_cmd_cb */
3257 N_("Set printing of symbol names when printing pointers."),
3258 N_("Show printing of symbol names when printing pointers."),
3259 NULL
, /* help_doc */
3262 boolean_option_def
{
3264 [] (value_print_options
*opt
) { return &opt
->unionprint
; },
3265 show_unionprint
, /* show_cmd_cb */
3266 N_("Set printing of unions interior to structures."),
3267 N_("Show printing of unions interior to structures."),
3268 NULL
, /* help_doc */
3271 boolean_option_def
{
3273 [] (value_print_options
*opt
) { return &opt
->vtblprint
; },
3274 show_vtblprint
, /* show_cmd_cb */
3275 N_("Set printing of C++ virtual function tables."),
3276 N_("Show printing of C++ virtual function tables."),
3277 NULL
, /* help_doc */
3281 /* See valprint.h. */
3283 gdb::option::option_def_group
3284 make_value_print_options_def_group (value_print_options
*opts
)
3286 return {{value_print_option_defs
}, opts
};
3289 void _initialize_valprint ();
3291 _initialize_valprint ()
3293 cmd_list_element
*cmd
;
3295 add_prefix_cmd ("print", no_class
, set_print
,
3296 _("Generic command for setting how things print."),
3297 &setprintlist
, "set print ", 0, &setlist
);
3298 add_alias_cmd ("p", "print", no_class
, 1, &setlist
);
3299 /* Prefer set print to set prompt. */
3300 add_alias_cmd ("pr", "print", no_class
, 1, &setlist
);
3302 add_prefix_cmd ("print", no_class
, show_print
,
3303 _("Generic command for showing print settings."),
3304 &showprintlist
, "show print ", 0, &showlist
);
3305 add_alias_cmd ("p", "print", no_class
, 1, &showlist
);
3306 add_alias_cmd ("pr", "print", no_class
, 1, &showlist
);
3308 cmd
= add_prefix_cmd ("raw", no_class
, set_print_raw
,
3310 Generic command for setting what things to print in \"raw\" mode."),
3311 &setprintrawlist
, "set print raw ", 0,
3313 deprecate_cmd (cmd
, nullptr);
3315 cmd
= add_prefix_cmd ("raw", no_class
, show_print_raw
,
3316 _("Generic command for showing \"print raw\" settings."),
3317 &showprintrawlist
, "show print raw ", 0,
3319 deprecate_cmd (cmd
, nullptr);
3321 gdb::option::add_setshow_cmds_for_options
3322 (class_support
, &user_print_options
, value_print_option_defs
,
3323 &setprintlist
, &showprintlist
);
3325 add_setshow_zuinteger_cmd ("input-radix", class_support
, &input_radix_1
,
3327 Set default input radix for entering numbers."), _("\
3328 Show default input radix for entering numbers."), NULL
,
3331 &setlist
, &showlist
);
3333 add_setshow_zuinteger_cmd ("output-radix", class_support
, &output_radix_1
,
3335 Set default output radix for printing of values."), _("\
3336 Show default output radix for printing of values."), NULL
,
3339 &setlist
, &showlist
);
3341 /* The "set radix" and "show radix" commands are special in that
3342 they are like normal set and show commands but allow two normally
3343 independent variables to be either set or shown with a single
3344 command. So the usual deprecated_add_set_cmd() and [deleted]
3345 add_show_from_set() commands aren't really appropriate. */
3346 /* FIXME: i18n: With the new add_setshow_integer command, that is no
3347 longer true - show can display anything. */
3348 add_cmd ("radix", class_support
, set_radix
, _("\
3349 Set default input and output number radices.\n\
3350 Use 'set input-radix' or 'set output-radix' to independently set each.\n\
3351 Without an argument, sets both radices back to the default value of 10."),
3353 add_cmd ("radix", class_support
, show_radix
, _("\
3354 Show the default input and output number radices.\n\
3355 Use 'show input-radix' or 'show output-radix' to independently show each."),