1 /* Print values for GDB, the GNU debugger.
3 Copyright (C) 1986-2017 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 "floatformat.h"
33 #include "extension.h"
35 #include "gdb_obstack.h"
37 #include "typeprint.h"
40 #include "common/byte-vector.h"
42 /* Maximum number of wchars returned from wchar_iterate. */
45 /* A convenience macro to compute the size of a wchar_t buffer containing X
47 #define WCHAR_BUFLEN(X) ((X) * sizeof (gdb_wchar_t))
49 /* Character buffer size saved while iterating over wchars. */
50 #define WCHAR_BUFLEN_MAX WCHAR_BUFLEN (MAX_WCHARS)
52 /* A structure to encapsulate state information from iterated
53 character conversions. */
54 struct converted_character
56 /* The number of characters converted. */
59 /* The result of the conversion. See charset.h for more. */
60 enum wchar_iterate_result result;
62 /* The (saved) converted character(s). */
63 gdb_wchar_t chars[WCHAR_BUFLEN_MAX];
65 /* The first converted target byte. */
68 /* The number of bytes converted. */
71 /* How many times this character(s) is repeated. */
75 typedef struct converted_character converted_character_d;
76 DEF_VEC_O (converted_character_d);
78 /* Command lists for set/show print raw. */
79 struct cmd_list_element *setprintrawlist;
80 struct cmd_list_element *showprintrawlist;
82 /* Prototypes for local functions */
84 static int partial_memory_read (CORE_ADDR memaddr, gdb_byte *myaddr,
85 int len, int *errptr);
87 static void show_print (char *, int);
89 static void set_print (char *, int);
91 static void set_radix (char *, int);
93 static void show_radix (char *, int);
95 static void set_input_radix (char *, int, struct cmd_list_element *);
97 static void set_input_radix_1 (int, unsigned);
99 static void set_output_radix (char *, int, struct cmd_list_element *);
101 static void set_output_radix_1 (int, unsigned);
103 static void val_print_type_code_flags (struct type *type,
104 const gdb_byte *valaddr,
105 struct ui_file *stream);
107 #define PRINT_MAX_DEFAULT 200 /* Start print_max off at this value. */
109 struct value_print_options user_print_options =
111 Val_prettyformat_default, /* prettyformat */
112 0, /* prettyformat_arrays */
113 0, /* prettyformat_structs */
116 1, /* addressprint */
118 PRINT_MAX_DEFAULT, /* print_max */
119 10, /* repeat_count_threshold */
120 0, /* output_format */
122 0, /* stop_print_at_null */
123 0, /* print_array_indexes */
125 1, /* static_field_print */
126 1, /* pascal_static_field_print */
132 /* Initialize *OPTS to be a copy of the user print options. */
134 get_user_print_options (struct value_print_options *opts)
136 *opts = user_print_options;
139 /* Initialize *OPTS to be a copy of the user print options, but with
140 pretty-formatting disabled. */
142 get_no_prettyformat_print_options (struct value_print_options *opts)
144 *opts = user_print_options;
145 opts->prettyformat = Val_no_prettyformat;
148 /* Initialize *OPTS to be a copy of the user print options, but using
149 FORMAT as the formatting option. */
151 get_formatted_print_options (struct value_print_options *opts,
154 *opts = user_print_options;
155 opts->format = format;
159 show_print_max (struct ui_file *file, int from_tty,
160 struct cmd_list_element *c, const char *value)
162 fprintf_filtered (file,
163 _("Limit on string chars or array "
164 "elements to print is %s.\n"),
169 /* Default input and output radixes, and output format letter. */
171 unsigned input_radix = 10;
173 show_input_radix (struct ui_file *file, int from_tty,
174 struct cmd_list_element *c, const char *value)
176 fprintf_filtered (file,
177 _("Default input radix for entering numbers is %s.\n"),
181 unsigned output_radix = 10;
183 show_output_radix (struct ui_file *file, int from_tty,
184 struct cmd_list_element *c, const char *value)
186 fprintf_filtered (file,
187 _("Default output radix for printing of values is %s.\n"),
191 /* By default we print arrays without printing the index of each element in
192 the array. This behavior can be changed by setting PRINT_ARRAY_INDEXES. */
195 show_print_array_indexes (struct ui_file *file, int from_tty,
196 struct cmd_list_element *c, const char *value)
198 fprintf_filtered (file, _("Printing of array indexes is %s.\n"), value);
201 /* Print repeat counts if there are more than this many repetitions of an
202 element in an array. Referenced by the low level language dependent
206 show_repeat_count_threshold (struct ui_file *file, int from_tty,
207 struct cmd_list_element *c, const char *value)
209 fprintf_filtered (file, _("Threshold for repeated print elements is %s.\n"),
213 /* If nonzero, stops printing of char arrays at first null. */
216 show_stop_print_at_null (struct ui_file *file, int from_tty,
217 struct cmd_list_element *c, const char *value)
219 fprintf_filtered (file,
220 _("Printing of char arrays to stop "
221 "at first null char is %s.\n"),
225 /* Controls pretty printing of structures. */
228 show_prettyformat_structs (struct ui_file *file, int from_tty,
229 struct cmd_list_element *c, const char *value)
231 fprintf_filtered (file, _("Pretty formatting of structures is %s.\n"), value);
234 /* Controls pretty printing of arrays. */
237 show_prettyformat_arrays (struct ui_file *file, int from_tty,
238 struct cmd_list_element *c, const char *value)
240 fprintf_filtered (file, _("Pretty formatting of arrays is %s.\n"), value);
243 /* If nonzero, causes unions inside structures or other unions to be
247 show_unionprint (struct ui_file *file, int from_tty,
248 struct cmd_list_element *c, const char *value)
250 fprintf_filtered (file,
251 _("Printing of unions interior to structures is %s.\n"),
255 /* If nonzero, causes machine addresses to be printed in certain contexts. */
258 show_addressprint (struct ui_file *file, int from_tty,
259 struct cmd_list_element *c, const char *value)
261 fprintf_filtered (file, _("Printing of addresses is %s.\n"), value);
265 show_symbol_print (struct ui_file *file, int from_tty,
266 struct cmd_list_element *c, const char *value)
268 fprintf_filtered (file,
269 _("Printing of symbols when printing pointers is %s.\n"),
275 /* A helper function for val_print. When printing in "summary" mode,
276 we want to print scalar arguments, but not aggregate arguments.
277 This function distinguishes between the two. */
280 val_print_scalar_type_p (struct type *type)
282 type = check_typedef (type);
283 while (TYPE_IS_REFERENCE (type))
285 type = TYPE_TARGET_TYPE (type);
286 type = check_typedef (type);
288 switch (TYPE_CODE (type))
290 case TYPE_CODE_ARRAY:
291 case TYPE_CODE_STRUCT:
292 case TYPE_CODE_UNION:
294 case TYPE_CODE_STRING:
301 /* See its definition in value.h. */
304 valprint_check_validity (struct ui_file *stream,
306 LONGEST embedded_offset,
307 const struct value *val)
309 type = check_typedef (type);
311 if (type_not_associated (type))
313 val_print_not_associated (stream);
317 if (type_not_allocated (type))
319 val_print_not_allocated (stream);
323 if (TYPE_CODE (type) != TYPE_CODE_UNION
324 && TYPE_CODE (type) != TYPE_CODE_STRUCT
325 && TYPE_CODE (type) != TYPE_CODE_ARRAY)
327 if (value_bits_any_optimized_out (val,
328 TARGET_CHAR_BIT * embedded_offset,
329 TARGET_CHAR_BIT * TYPE_LENGTH (type)))
331 val_print_optimized_out (val, stream);
335 if (value_bits_synthetic_pointer (val, TARGET_CHAR_BIT * embedded_offset,
336 TARGET_CHAR_BIT * TYPE_LENGTH (type)))
338 const int is_ref = TYPE_CODE (type) == TYPE_CODE_REF;
339 int ref_is_addressable = 0;
343 const struct value *deref_val = coerce_ref_if_computed (val);
345 if (deref_val != NULL)
346 ref_is_addressable = value_lval_const (deref_val) == lval_memory;
349 if (!is_ref || !ref_is_addressable)
350 fputs_filtered (_("<synthetic pointer>"), stream);
352 /* C++ references should be valid even if they're synthetic. */
356 if (!value_bytes_available (val, embedded_offset, TYPE_LENGTH (type)))
358 val_print_unavailable (stream);
367 val_print_optimized_out (const struct value *val, struct ui_file *stream)
369 if (val != NULL && value_lval_const (val) == lval_register)
370 val_print_not_saved (stream);
372 fprintf_filtered (stream, _("<optimized out>"));
376 val_print_not_saved (struct ui_file *stream)
378 fprintf_filtered (stream, _("<not saved>"));
382 val_print_unavailable (struct ui_file *stream)
384 fprintf_filtered (stream, _("<unavailable>"));
388 val_print_invalid_address (struct ui_file *stream)
390 fprintf_filtered (stream, _("<invalid address>"));
393 /* Print a pointer based on the type of its target.
395 Arguments to this functions are roughly the same as those in
396 generic_val_print. A difference is that ADDRESS is the address to print,
397 with embedded_offset already added. ELTTYPE represents
398 the pointed type after check_typedef. */
401 print_unpacked_pointer (struct type *type, struct type *elttype,
402 CORE_ADDR address, struct ui_file *stream,
403 const struct value_print_options *options)
405 struct gdbarch *gdbarch = get_type_arch (type);
407 if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
409 /* Try to print what function it points to. */
410 print_function_pointer_address (options, gdbarch, address, stream);
414 if (options->symbol_print)
415 print_address_demangle (options, gdbarch, address, stream, demangle);
416 else if (options->addressprint)
417 fputs_filtered (paddress (gdbarch, address), stream);
420 /* generic_val_print helper for TYPE_CODE_ARRAY. */
423 generic_val_print_array (struct type *type,
424 int embedded_offset, CORE_ADDR address,
425 struct ui_file *stream, int recurse,
426 struct value *original_value,
427 const struct value_print_options *options,
429 generic_val_print_decorations *decorations)
431 struct type *unresolved_elttype = TYPE_TARGET_TYPE (type);
432 struct type *elttype = check_typedef (unresolved_elttype);
434 if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (unresolved_elttype) > 0)
436 LONGEST low_bound, high_bound;
438 if (!get_array_bounds (type, &low_bound, &high_bound))
439 error (_("Could not determine the array high bound"));
441 if (options->prettyformat_arrays)
443 print_spaces_filtered (2 + 2 * recurse, stream);
446 fputs_filtered (decorations->array_start, stream);
447 val_print_array_elements (type, embedded_offset,
449 recurse, original_value, options, 0);
450 fputs_filtered (decorations->array_end, stream);
454 /* Array of unspecified length: treat like pointer to first elt. */
455 print_unpacked_pointer (type, elttype, address + embedded_offset, stream,
461 /* generic_val_print helper for TYPE_CODE_PTR. */
464 generic_val_print_ptr (struct type *type,
465 int embedded_offset, struct ui_file *stream,
466 struct value *original_value,
467 const struct value_print_options *options)
469 struct gdbarch *gdbarch = get_type_arch (type);
470 int unit_size = gdbarch_addressable_memory_unit_size (gdbarch);
472 if (options->format && options->format != 's')
474 val_print_scalar_formatted (type, embedded_offset,
475 original_value, options, 0, stream);
479 struct type *unresolved_elttype = TYPE_TARGET_TYPE(type);
480 struct type *elttype = check_typedef (unresolved_elttype);
481 const gdb_byte *valaddr = value_contents_for_printing (original_value);
482 CORE_ADDR addr = unpack_pointer (type,
483 valaddr + embedded_offset * unit_size);
485 print_unpacked_pointer (type, elttype, addr, stream, options);
490 /* generic_val_print helper for TYPE_CODE_MEMBERPTR. */
493 generic_val_print_memberptr (struct type *type,
494 int embedded_offset, struct ui_file *stream,
495 struct value *original_value,
496 const struct value_print_options *options)
498 val_print_scalar_formatted (type, embedded_offset,
499 original_value, options, 0, stream);
502 /* Print '@' followed by the address contained in ADDRESS_BUFFER. */
505 print_ref_address (struct type *type, const gdb_byte *address_buffer,
506 int embedded_offset, struct ui_file *stream)
508 struct gdbarch *gdbarch = get_type_arch (type);
510 if (address_buffer != NULL)
513 = extract_typed_address (address_buffer + embedded_offset, type);
515 fprintf_filtered (stream, "@");
516 fputs_filtered (paddress (gdbarch, address), stream);
518 /* Else: we have a non-addressable value, such as a DW_AT_const_value. */
521 /* If VAL is addressable, return the value contents buffer of a value that
522 represents a pointer to VAL. Otherwise return NULL. */
524 static const gdb_byte *
525 get_value_addr_contents (struct value *deref_val)
527 gdb_assert (deref_val != NULL);
529 if (value_lval_const (deref_val) == lval_memory)
530 return value_contents_for_printing_const (value_addr (deref_val));
533 /* We have a non-addressable value, such as a DW_AT_const_value. */
538 /* generic_val_print helper for TYPE_CODE_{RVALUE_,}REF. */
541 generic_val_print_ref (struct type *type,
542 int embedded_offset, struct ui_file *stream, int recurse,
543 struct value *original_value,
544 const struct value_print_options *options)
546 struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type));
547 struct value *deref_val = NULL;
548 const int value_is_synthetic
549 = value_bits_synthetic_pointer (original_value,
550 TARGET_CHAR_BIT * embedded_offset,
551 TARGET_CHAR_BIT * TYPE_LENGTH (type));
552 const int must_coerce_ref = ((options->addressprint && value_is_synthetic)
553 || options->deref_ref);
554 const int type_is_defined = TYPE_CODE (elttype) != TYPE_CODE_UNDEF;
555 const gdb_byte *valaddr = value_contents_for_printing (original_value);
557 if (must_coerce_ref && type_is_defined)
559 deref_val = coerce_ref_if_computed (original_value);
561 if (deref_val != NULL)
563 /* More complicated computed references are not supported. */
564 gdb_assert (embedded_offset == 0);
567 deref_val = value_at (TYPE_TARGET_TYPE (type),
568 unpack_pointer (type, valaddr + embedded_offset));
570 /* Else, original_value isn't a synthetic reference or we don't have to print
571 the reference's contents.
573 Notice that for references to TYPE_CODE_STRUCT, 'set print object on' will
574 cause original_value to be a not_lval instead of an lval_computed,
575 which will make value_bits_synthetic_pointer return false.
576 This happens because if options->objectprint is true, c_value_print will
577 overwrite original_value's contents with the result of coercing
578 the reference through value_addr, and then set its type back to
579 TYPE_CODE_REF. In that case we don't have to coerce the reference again;
580 we can simply treat it as non-synthetic and move on. */
582 if (options->addressprint)
584 const gdb_byte *address = (value_is_synthetic && type_is_defined
585 ? get_value_addr_contents (deref_val)
588 print_ref_address (type, address, embedded_offset, stream);
590 if (options->deref_ref)
591 fputs_filtered (": ", stream);
594 if (options->deref_ref)
597 common_val_print (deref_val, stream, recurse, options,
600 fputs_filtered ("???", stream);
604 /* Helper function for generic_val_print_enum.
605 This is also used to print enums in TYPE_CODE_FLAGS values. */
608 generic_val_print_enum_1 (struct type *type, LONGEST val,
609 struct ui_file *stream)
614 len = TYPE_NFIELDS (type);
615 for (i = 0; i < len; i++)
618 if (val == TYPE_FIELD_ENUMVAL (type, i))
625 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
627 else if (TYPE_FLAG_ENUM (type))
631 /* We have a "flag" enum, so we try to decompose it into
632 pieces as appropriate. A flag enum has disjoint
633 constants by definition. */
634 fputs_filtered ("(", stream);
635 for (i = 0; i < len; ++i)
639 if ((val & TYPE_FIELD_ENUMVAL (type, i)) != 0)
642 fputs_filtered (" | ", stream);
645 val &= ~TYPE_FIELD_ENUMVAL (type, i);
646 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
650 if (first || val != 0)
653 fputs_filtered (" | ", stream);
654 fputs_filtered ("unknown: ", stream);
655 print_longest (stream, 'd', 0, val);
658 fputs_filtered (")", stream);
661 print_longest (stream, 'd', 0, val);
664 /* generic_val_print helper for TYPE_CODE_ENUM. */
667 generic_val_print_enum (struct type *type,
668 int embedded_offset, struct ui_file *stream,
669 struct value *original_value,
670 const struct value_print_options *options)
673 struct gdbarch *gdbarch = get_type_arch (type);
674 int unit_size = gdbarch_addressable_memory_unit_size (gdbarch);
678 val_print_scalar_formatted (type, embedded_offset,
679 original_value, options, 0, stream);
683 const gdb_byte *valaddr = value_contents_for_printing (original_value);
685 val = unpack_long (type, valaddr + embedded_offset * unit_size);
687 generic_val_print_enum_1 (type, val, stream);
691 /* generic_val_print helper for TYPE_CODE_FLAGS. */
694 generic_val_print_flags (struct type *type,
695 int embedded_offset, struct ui_file *stream,
696 struct value *original_value,
697 const struct value_print_options *options)
701 val_print_scalar_formatted (type, embedded_offset, original_value,
705 const gdb_byte *valaddr = value_contents_for_printing (original_value);
707 val_print_type_code_flags (type, valaddr + embedded_offset, stream);
711 /* generic_val_print helper for TYPE_CODE_FUNC and TYPE_CODE_METHOD. */
714 generic_val_print_func (struct type *type,
715 int embedded_offset, CORE_ADDR address,
716 struct ui_file *stream,
717 struct value *original_value,
718 const struct value_print_options *options)
720 struct gdbarch *gdbarch = get_type_arch (type);
724 val_print_scalar_formatted (type, embedded_offset,
725 original_value, options, 0, stream);
729 /* FIXME, we should consider, at least for ANSI C language,
730 eliminating the distinction made between FUNCs and POINTERs
732 fprintf_filtered (stream, "{");
733 type_print (type, "", stream, -1);
734 fprintf_filtered (stream, "} ");
735 /* Try to print what function it points to, and its address. */
736 print_address_demangle (options, gdbarch, address, stream, demangle);
740 /* generic_val_print helper for TYPE_CODE_BOOL. */
743 generic_val_print_bool (struct type *type,
744 int embedded_offset, struct ui_file *stream,
745 struct value *original_value,
746 const struct value_print_options *options,
747 const struct generic_val_print_decorations *decorations)
750 struct gdbarch *gdbarch = get_type_arch (type);
751 int unit_size = gdbarch_addressable_memory_unit_size (gdbarch);
753 if (options->format || options->output_format)
755 struct value_print_options opts = *options;
756 opts.format = (options->format ? options->format
757 : options->output_format);
758 val_print_scalar_formatted (type, embedded_offset,
759 original_value, &opts, 0, stream);
763 const gdb_byte *valaddr = value_contents_for_printing (original_value);
765 val = unpack_long (type, valaddr + embedded_offset * unit_size);
767 fputs_filtered (decorations->false_name, stream);
769 fputs_filtered (decorations->true_name, stream);
771 print_longest (stream, 'd', 0, val);
775 /* generic_val_print helper for TYPE_CODE_INT. */
778 generic_val_print_int (struct type *type,
779 int embedded_offset, struct ui_file *stream,
780 struct value *original_value,
781 const struct value_print_options *options)
783 struct value_print_options opts = *options;
785 opts.format = (options->format ? options->format
786 : options->output_format);
787 val_print_scalar_formatted (type, embedded_offset,
788 original_value, &opts, 0, stream);
791 /* generic_val_print helper for TYPE_CODE_CHAR. */
794 generic_val_print_char (struct type *type, struct type *unresolved_type,
796 struct ui_file *stream,
797 struct value *original_value,
798 const struct value_print_options *options)
801 struct gdbarch *gdbarch = get_type_arch (type);
802 int unit_size = gdbarch_addressable_memory_unit_size (gdbarch);
804 if (options->format || options->output_format)
806 struct value_print_options opts = *options;
808 opts.format = (options->format ? options->format
809 : options->output_format);
810 val_print_scalar_formatted (type, embedded_offset,
811 original_value, &opts, 0, stream);
815 const gdb_byte *valaddr = value_contents_for_printing (original_value);
817 val = unpack_long (type, valaddr + embedded_offset * unit_size);
818 if (TYPE_UNSIGNED (type))
819 fprintf_filtered (stream, "%u", (unsigned int) val);
821 fprintf_filtered (stream, "%d", (int) val);
822 fputs_filtered (" ", stream);
823 LA_PRINT_CHAR (val, unresolved_type, stream);
827 /* generic_val_print helper for TYPE_CODE_FLT. */
830 generic_val_print_float (struct type *type,
831 int embedded_offset, struct ui_file *stream,
832 struct value *original_value,
833 const struct value_print_options *options)
835 struct gdbarch *gdbarch = get_type_arch (type);
836 int unit_size = gdbarch_addressable_memory_unit_size (gdbarch);
840 val_print_scalar_formatted (type, embedded_offset,
841 original_value, options, 0, stream);
845 const gdb_byte *valaddr = value_contents_for_printing (original_value);
847 print_floating (valaddr + embedded_offset * unit_size, type, stream);
851 /* generic_val_print helper for TYPE_CODE_DECFLOAT. */
854 generic_val_print_decfloat (struct type *type,
855 int embedded_offset, struct ui_file *stream,
856 struct value *original_value,
857 const struct value_print_options *options)
859 struct gdbarch *gdbarch = get_type_arch (type);
860 int unit_size = gdbarch_addressable_memory_unit_size (gdbarch);
863 val_print_scalar_formatted (type, embedded_offset, original_value,
867 const gdb_byte *valaddr = value_contents_for_printing (original_value);
869 print_decimal_floating (valaddr + embedded_offset * unit_size, type,
874 /* generic_val_print helper for TYPE_CODE_COMPLEX. */
877 generic_val_print_complex (struct type *type,
878 int embedded_offset, struct ui_file *stream,
879 struct value *original_value,
880 const struct value_print_options *options,
881 const struct generic_val_print_decorations
884 struct gdbarch *gdbarch = get_type_arch (type);
885 int unit_size = gdbarch_addressable_memory_unit_size (gdbarch);
886 const gdb_byte *valaddr = value_contents_for_printing (original_value);
888 fprintf_filtered (stream, "%s", decorations->complex_prefix);
890 val_print_scalar_formatted (TYPE_TARGET_TYPE (type),
891 embedded_offset, original_value, options, 0,
894 print_floating (valaddr + embedded_offset * unit_size,
895 TYPE_TARGET_TYPE (type), stream);
896 fprintf_filtered (stream, "%s", decorations->complex_infix);
898 val_print_scalar_formatted (TYPE_TARGET_TYPE (type),
900 + type_length_units (TYPE_TARGET_TYPE (type)),
901 original_value, options, 0, stream);
903 print_floating (valaddr + embedded_offset * unit_size
904 + TYPE_LENGTH (TYPE_TARGET_TYPE (type)),
905 TYPE_TARGET_TYPE (type), stream);
906 fprintf_filtered (stream, "%s", decorations->complex_suffix);
909 /* A generic val_print that is suitable for use by language
910 implementations of the la_val_print method. This function can
911 handle most type codes, though not all, notably exception
912 TYPE_CODE_UNION and TYPE_CODE_STRUCT, which must be implemented by
915 Most arguments are as to val_print.
917 The additional DECORATIONS argument can be used to customize the
918 output in some small, language-specific ways. */
921 generic_val_print (struct type *type,
922 int embedded_offset, CORE_ADDR address,
923 struct ui_file *stream, int recurse,
924 struct value *original_value,
925 const struct value_print_options *options,
926 const struct generic_val_print_decorations *decorations)
928 struct type *unresolved_type = type;
930 type = check_typedef (type);
931 switch (TYPE_CODE (type))
933 case TYPE_CODE_ARRAY:
934 generic_val_print_array (type, embedded_offset, address, stream,
935 recurse, original_value, options, decorations);
938 case TYPE_CODE_MEMBERPTR:
939 generic_val_print_memberptr (type, embedded_offset, stream,
940 original_value, options);
944 generic_val_print_ptr (type, embedded_offset, stream,
945 original_value, options);
949 case TYPE_CODE_RVALUE_REF:
950 generic_val_print_ref (type, embedded_offset, stream, recurse,
951 original_value, options);
955 generic_val_print_enum (type, embedded_offset, stream,
956 original_value, options);
959 case TYPE_CODE_FLAGS:
960 generic_val_print_flags (type, embedded_offset, stream,
961 original_value, options);
965 case TYPE_CODE_METHOD:
966 generic_val_print_func (type, embedded_offset, address, stream,
967 original_value, options);
971 generic_val_print_bool (type, embedded_offset, stream,
972 original_value, options, decorations);
975 case TYPE_CODE_RANGE:
976 /* FIXME: create_static_range_type does not set the unsigned bit in a
977 range type (I think it probably should copy it from the
978 target type), so we won't print values which are too large to
979 fit in a signed integer correctly. */
980 /* FIXME: Doesn't handle ranges of enums correctly. (Can't just
981 print with the target type, though, because the size of our
982 type and the target type might differ). */
987 generic_val_print_int (type, embedded_offset, stream,
988 original_value, options);
992 generic_val_print_char (type, unresolved_type, embedded_offset,
993 stream, original_value, options);
997 generic_val_print_float (type, embedded_offset, stream,
998 original_value, options);
1001 case TYPE_CODE_DECFLOAT:
1002 generic_val_print_decfloat (type, embedded_offset, stream,
1003 original_value, options);
1006 case TYPE_CODE_VOID:
1007 fputs_filtered (decorations->void_name, stream);
1010 case TYPE_CODE_ERROR:
1011 fprintf_filtered (stream, "%s", TYPE_ERROR_NAME (type));
1014 case TYPE_CODE_UNDEF:
1015 /* This happens (without TYPE_STUB set) on systems which don't use
1016 dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
1017 and no complete type for struct foo in that file. */
1018 fprintf_filtered (stream, _("<incomplete type>"));
1021 case TYPE_CODE_COMPLEX:
1022 generic_val_print_complex (type, embedded_offset, stream,
1023 original_value, options, decorations);
1026 case TYPE_CODE_UNION:
1027 case TYPE_CODE_STRUCT:
1028 case TYPE_CODE_METHODPTR:
1030 error (_("Unhandled type code %d in symbol table."),
1036 /* Print using the given LANGUAGE the data of type TYPE located at
1037 VAL's contents buffer + EMBEDDED_OFFSET (within GDB), which came
1038 from the inferior at address ADDRESS + EMBEDDED_OFFSET, onto
1039 stdio stream STREAM according to OPTIONS. VAL is the whole object
1040 that came from ADDRESS.
1042 The language printers will pass down an adjusted EMBEDDED_OFFSET to
1043 further helper subroutines as subfields of TYPE are printed. In
1044 such cases, VAL is passed down unadjusted, so
1045 that VAL can be queried for metadata about the contents data being
1046 printed, using EMBEDDED_OFFSET as an offset into VAL's contents
1047 buffer. For example: "has this field been optimized out", or "I'm
1048 printing an object while inspecting a traceframe; has this
1049 particular piece of data been collected?".
1051 RECURSE indicates the amount of indentation to supply before
1052 continuation lines; this amount is roughly twice the value of
1056 val_print (struct type *type, LONGEST embedded_offset,
1057 CORE_ADDR address, struct ui_file *stream, int recurse,
1059 const struct value_print_options *options,
1060 const struct language_defn *language)
1063 struct value_print_options local_opts = *options;
1064 struct type *real_type = check_typedef (type);
1066 if (local_opts.prettyformat == Val_prettyformat_default)
1067 local_opts.prettyformat = (local_opts.prettyformat_structs
1068 ? Val_prettyformat : Val_no_prettyformat);
1072 /* Ensure that the type is complete and not just a stub. If the type is
1073 only a stub and we can't find and substitute its complete type, then
1074 print appropriate string and return. */
1076 if (TYPE_STUB (real_type))
1078 fprintf_filtered (stream, _("<incomplete type>"));
1083 if (!valprint_check_validity (stream, real_type, embedded_offset, val))
1088 ret = apply_ext_lang_val_pretty_printer (type, embedded_offset,
1089 address, stream, recurse,
1090 val, options, language);
1095 /* Handle summary mode. If the value is a scalar, print it;
1096 otherwise, print an ellipsis. */
1097 if (options->summary && !val_print_scalar_type_p (type))
1099 fprintf_filtered (stream, "...");
1105 language->la_val_print (type, embedded_offset, address,
1106 stream, recurse, val,
1109 CATCH (except, RETURN_MASK_ERROR)
1111 fprintf_filtered (stream, _("<error reading variable>"));
1116 /* Check whether the value VAL is printable. Return 1 if it is;
1117 return 0 and print an appropriate error message to STREAM according to
1118 OPTIONS if it is not. */
1121 value_check_printable (struct value *val, struct ui_file *stream,
1122 const struct value_print_options *options)
1126 fprintf_filtered (stream, _("<address of value unknown>"));
1130 if (value_entirely_optimized_out (val))
1132 if (options->summary && !val_print_scalar_type_p (value_type (val)))
1133 fprintf_filtered (stream, "...");
1135 val_print_optimized_out (val, stream);
1139 if (value_entirely_unavailable (val))
1141 if (options->summary && !val_print_scalar_type_p (value_type (val)))
1142 fprintf_filtered (stream, "...");
1144 val_print_unavailable (stream);
1148 if (TYPE_CODE (value_type (val)) == TYPE_CODE_INTERNAL_FUNCTION)
1150 fprintf_filtered (stream, _("<internal function %s>"),
1151 value_internal_function_name (val));
1155 if (type_not_associated (value_type (val)))
1157 val_print_not_associated (stream);
1161 if (type_not_allocated (value_type (val)))
1163 val_print_not_allocated (stream);
1170 /* Print using the given LANGUAGE the value VAL onto stream STREAM according
1173 This is a preferable interface to val_print, above, because it uses
1174 GDB's value mechanism. */
1177 common_val_print (struct value *val, struct ui_file *stream, int recurse,
1178 const struct value_print_options *options,
1179 const struct language_defn *language)
1181 if (!value_check_printable (val, stream, options))
1184 if (language->la_language == language_ada)
1185 /* The value might have a dynamic type, which would cause trouble
1186 below when trying to extract the value contents (since the value
1187 size is determined from the type size which is unknown). So
1188 get a fixed representation of our value. */
1189 val = ada_to_fixed_value (val);
1191 if (value_lazy (val))
1192 value_fetch_lazy (val);
1194 val_print (value_type (val),
1195 value_embedded_offset (val), value_address (val),
1197 val, options, language);
1200 /* Print on stream STREAM the value VAL according to OPTIONS. The value
1201 is printed using the current_language syntax. */
1204 value_print (struct value *val, struct ui_file *stream,
1205 const struct value_print_options *options)
1207 if (!value_check_printable (val, stream, options))
1213 = apply_ext_lang_val_pretty_printer (value_type (val),
1214 value_embedded_offset (val),
1215 value_address (val),
1217 val, options, current_language);
1223 LA_VALUE_PRINT (val, stream, options);
1227 val_print_type_code_flags (struct type *type, const gdb_byte *valaddr,
1228 struct ui_file *stream)
1230 ULONGEST val = unpack_long (type, valaddr);
1231 int field, nfields = TYPE_NFIELDS (type);
1232 struct gdbarch *gdbarch = get_type_arch (type);
1233 struct type *bool_type = builtin_type (gdbarch)->builtin_bool;
1235 fputs_filtered ("[", stream);
1236 for (field = 0; field < nfields; field++)
1238 if (TYPE_FIELD_NAME (type, field)[0] != '\0')
1240 struct type *field_type = TYPE_FIELD_TYPE (type, field);
1242 if (field_type == bool_type
1243 /* We require boolean types here to be one bit wide. This is a
1244 problematic place to notify the user of an internal error
1245 though. Instead just fall through and print the field as an
1247 && TYPE_FIELD_BITSIZE (type, field) == 1)
1249 if (val & ((ULONGEST)1 << TYPE_FIELD_BITPOS (type, field)))
1250 fprintf_filtered (stream, " %s",
1251 TYPE_FIELD_NAME (type, field));
1255 unsigned field_len = TYPE_FIELD_BITSIZE (type, field);
1257 = val >> (TYPE_FIELD_BITPOS (type, field) - field_len + 1);
1259 if (field_len < sizeof (ULONGEST) * TARGET_CHAR_BIT)
1260 field_val &= ((ULONGEST) 1 << field_len) - 1;
1261 fprintf_filtered (stream, " %s=",
1262 TYPE_FIELD_NAME (type, field));
1263 if (TYPE_CODE (field_type) == TYPE_CODE_ENUM)
1264 generic_val_print_enum_1 (field_type, field_val, stream);
1266 print_longest (stream, 'd', 0, field_val);
1270 fputs_filtered (" ]", stream);
1273 /* Print a scalar of data of type TYPE, pointed to in GDB by VALADDR,
1274 according to OPTIONS and SIZE on STREAM. Format i is not supported
1277 This is how the elements of an array or structure are printed
1281 val_print_scalar_formatted (struct type *type,
1282 LONGEST embedded_offset,
1284 const struct value_print_options *options,
1286 struct ui_file *stream)
1288 struct gdbarch *arch = get_type_arch (type);
1289 int unit_size = gdbarch_addressable_memory_unit_size (arch);
1291 gdb_assert (val != NULL);
1293 /* If we get here with a string format, try again without it. Go
1294 all the way back to the language printers, which may call us
1296 if (options->format == 's')
1298 struct value_print_options opts = *options;
1301 val_print (type, embedded_offset, 0, stream, 0, val, &opts,
1306 /* value_contents_for_printing fetches all VAL's contents. They are
1307 needed to check whether VAL is optimized-out or unavailable
1309 const gdb_byte *valaddr = value_contents_for_printing (val);
1311 /* A scalar object that does not have all bits available can't be
1312 printed, because all bits contribute to its representation. */
1313 if (value_bits_any_optimized_out (val,
1314 TARGET_CHAR_BIT * embedded_offset,
1315 TARGET_CHAR_BIT * TYPE_LENGTH (type)))
1316 val_print_optimized_out (val, stream);
1317 else if (!value_bytes_available (val, embedded_offset, TYPE_LENGTH (type)))
1318 val_print_unavailable (stream);
1320 print_scalar_formatted (valaddr + embedded_offset * unit_size, type,
1321 options, size, stream);
1324 /* Print a number according to FORMAT which is one of d,u,x,o,b,h,w,g.
1325 The raison d'etre of this function is to consolidate printing of
1326 LONG_LONG's into this one function. The format chars b,h,w,g are
1327 from print_scalar_formatted(). Numbers are printed using C
1330 USE_C_FORMAT means to use C format in all cases. Without it,
1331 'o' and 'x' format do not include the standard C radix prefix
1334 Hilfinger/2004-09-09: USE_C_FORMAT was originally called USE_LOCAL
1335 and was intended to request formating according to the current
1336 language and would be used for most integers that GDB prints. The
1337 exceptional cases were things like protocols where the format of
1338 the integer is a protocol thing, not a user-visible thing). The
1339 parameter remains to preserve the information of what things might
1340 be printed with language-specific format, should we ever resurrect
1344 print_longest (struct ui_file *stream, int format, int use_c_format,
1352 val = int_string (val_long, 10, 1, 0, 1); break;
1354 val = int_string (val_long, 10, 0, 0, 1); break;
1356 val = int_string (val_long, 16, 0, 0, use_c_format); break;
1358 val = int_string (val_long, 16, 0, 2, 1); break;
1360 val = int_string (val_long, 16, 0, 4, 1); break;
1362 val = int_string (val_long, 16, 0, 8, 1); break;
1364 val = int_string (val_long, 16, 0, 16, 1); break;
1367 val = int_string (val_long, 8, 0, 0, use_c_format); break;
1369 internal_error (__FILE__, __LINE__,
1370 _("failed internal consistency check"));
1372 fputs_filtered (val, stream);
1375 /* This used to be a macro, but I don't think it is called often enough
1376 to merit such treatment. */
1377 /* Convert a LONGEST to an int. This is used in contexts (e.g. number of
1378 arguments to a function, number in a value history, register number, etc.)
1379 where the value must not be larger than can fit in an int. */
1382 longest_to_int (LONGEST arg)
1384 /* Let the compiler do the work. */
1385 int rtnval = (int) arg;
1387 /* Check for overflows or underflows. */
1388 if (sizeof (LONGEST) > sizeof (int))
1392 error (_("Value out of range."));
1398 /* Print a floating point value of type TYPE (not always a
1399 TYPE_CODE_FLT), pointed to in GDB by VALADDR, on STREAM. */
1402 print_floating (const gdb_byte *valaddr, struct type *type,
1403 struct ui_file *stream)
1407 const struct floatformat *fmt = NULL;
1408 unsigned len = TYPE_LENGTH (type);
1409 enum float_kind kind;
1411 /* If it is a floating-point, check for obvious problems. */
1412 if (TYPE_CODE (type) == TYPE_CODE_FLT)
1413 fmt = floatformat_from_type (type);
1416 kind = floatformat_classify (fmt, valaddr);
1417 if (kind == float_nan)
1419 if (floatformat_is_negative (fmt, valaddr))
1420 fprintf_filtered (stream, "-");
1421 fprintf_filtered (stream, "nan(");
1422 fputs_filtered ("0x", stream);
1423 fputs_filtered (floatformat_mantissa (fmt, valaddr), stream);
1424 fprintf_filtered (stream, ")");
1427 else if (kind == float_infinite)
1429 if (floatformat_is_negative (fmt, valaddr))
1430 fputs_filtered ("-", stream);
1431 fputs_filtered ("inf", stream);
1436 /* NOTE: cagney/2002-01-15: The TYPE passed into print_floating()
1437 isn't necessarily a TYPE_CODE_FLT. Consequently, unpack_double
1438 needs to be used as that takes care of any necessary type
1439 conversions. Such conversions are of course direct to DOUBLEST
1440 and disregard any possible target floating point limitations.
1441 For instance, a u64 would be converted and displayed exactly on a
1442 host with 80 bit DOUBLEST but with loss of information on a host
1443 with 64 bit DOUBLEST. */
1445 doub = unpack_double (type, valaddr, &inv);
1448 fprintf_filtered (stream, "<invalid float value>");
1452 /* FIXME: kettenis/2001-01-20: The following code makes too much
1453 assumptions about the host and target floating point format. */
1455 /* NOTE: cagney/2002-02-03: Since the TYPE of what was passed in may
1456 not necessarily be a TYPE_CODE_FLT, the below ignores that and
1457 instead uses the type's length to determine the precision of the
1458 floating-point value being printed. */
1460 if (len < sizeof (double))
1461 fprintf_filtered (stream, "%.9g", (double) doub);
1462 else if (len == sizeof (double))
1463 fprintf_filtered (stream, "%.17g", (double) doub);
1465 #ifdef PRINTF_HAS_LONG_DOUBLE
1466 fprintf_filtered (stream, "%.35Lg", doub);
1468 /* This at least wins with values that are representable as
1470 fprintf_filtered (stream, "%.17g", (double) doub);
1475 print_decimal_floating (const gdb_byte *valaddr, struct type *type,
1476 struct ui_file *stream)
1478 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
1479 char decstr[MAX_DECIMAL_STRING];
1480 unsigned len = TYPE_LENGTH (type);
1482 decimal_to_string (valaddr, len, byte_order, decstr);
1483 fputs_filtered (decstr, stream);
1488 print_binary_chars (struct ui_file *stream, const gdb_byte *valaddr,
1489 unsigned len, enum bfd_endian byte_order, bool zero_pad)
1494 bool seen_a_one = false;
1496 /* Declared "int" so it will be signed.
1497 This ensures that right shift will shift in zeros. */
1499 const int mask = 0x080;
1501 if (byte_order == BFD_ENDIAN_BIG)
1507 /* Every byte has 8 binary characters; peel off
1508 and print from the MSB end. */
1510 for (i = 0; i < (HOST_CHAR_BIT * sizeof (*p)); i++)
1512 if (*p & (mask >> i))
1517 if (zero_pad || seen_a_one || b == '1')
1518 fputc_filtered (b, stream);
1526 for (p = valaddr + len - 1;
1530 for (i = 0; i < (HOST_CHAR_BIT * sizeof (*p)); i++)
1532 if (*p & (mask >> i))
1537 if (zero_pad || seen_a_one || b == '1')
1538 fputc_filtered (b, stream);
1545 /* When not zero-padding, ensure that something is printed when the
1547 if (!zero_pad && !seen_a_one)
1548 fputc_filtered ('0', stream);
1551 /* A helper for print_octal_chars that emits a single octal digit,
1552 optionally suppressing it if is zero and updating SEEN_A_ONE. */
1555 emit_octal_digit (struct ui_file *stream, bool *seen_a_one, int digit)
1557 if (*seen_a_one || digit != 0)
1558 fprintf_filtered (stream, "%o", digit);
1563 /* VALADDR points to an integer of LEN bytes.
1564 Print it in octal on stream or format it in buf. */
1567 print_octal_chars (struct ui_file *stream, const gdb_byte *valaddr,
1568 unsigned len, enum bfd_endian byte_order)
1571 unsigned char octa1, octa2, octa3, carry;
1574 /* Octal is 3 bits, which doesn't fit. Yuk. So we have to track
1575 * the extra bits, which cycle every three bytes:
1577 * Byte side: 0 1 2 3
1579 * bit number 123 456 78 | 9 012 345 6 | 78 901 234 | 567 890 12 |
1581 * Octal side: 0 1 carry 3 4 carry ...
1583 * Cycle number: 0 1 2
1585 * But of course we are printing from the high side, so we have to
1586 * figure out where in the cycle we are so that we end up with no
1587 * left over bits at the end.
1589 #define BITS_IN_OCTAL 3
1590 #define HIGH_ZERO 0340
1591 #define LOW_ZERO 0034
1592 #define CARRY_ZERO 0003
1593 static_assert (HIGH_ZERO + LOW_ZERO + CARRY_ZERO == 0xff,
1594 "cycle zero constants are wrong");
1595 #define HIGH_ONE 0200
1596 #define MID_ONE 0160
1597 #define LOW_ONE 0016
1598 #define CARRY_ONE 0001
1599 static_assert (HIGH_ONE + MID_ONE + LOW_ONE + CARRY_ONE == 0xff,
1600 "cycle one constants are wrong");
1601 #define HIGH_TWO 0300
1602 #define MID_TWO 0070
1603 #define LOW_TWO 0007
1604 static_assert (HIGH_TWO + MID_TWO + LOW_TWO == 0xff,
1605 "cycle two constants are wrong");
1607 /* For 32 we start in cycle 2, with two bits and one bit carry;
1608 for 64 in cycle in cycle 1, with one bit and a two bit carry. */
1610 cycle = (len * HOST_CHAR_BIT) % BITS_IN_OCTAL;
1613 fputs_filtered ("0", stream);
1614 bool seen_a_one = false;
1615 if (byte_order == BFD_ENDIAN_BIG)
1624 /* No carry in, carry out two bits. */
1626 octa1 = (HIGH_ZERO & *p) >> 5;
1627 octa2 = (LOW_ZERO & *p) >> 2;
1628 carry = (CARRY_ZERO & *p);
1629 emit_octal_digit (stream, &seen_a_one, octa1);
1630 emit_octal_digit (stream, &seen_a_one, octa2);
1634 /* Carry in two bits, carry out one bit. */
1636 octa1 = (carry << 1) | ((HIGH_ONE & *p) >> 7);
1637 octa2 = (MID_ONE & *p) >> 4;
1638 octa3 = (LOW_ONE & *p) >> 1;
1639 carry = (CARRY_ONE & *p);
1640 emit_octal_digit (stream, &seen_a_one, octa1);
1641 emit_octal_digit (stream, &seen_a_one, octa2);
1642 emit_octal_digit (stream, &seen_a_one, octa3);
1646 /* Carry in one bit, no carry out. */
1648 octa1 = (carry << 2) | ((HIGH_TWO & *p) >> 6);
1649 octa2 = (MID_TWO & *p) >> 3;
1650 octa3 = (LOW_TWO & *p);
1652 emit_octal_digit (stream, &seen_a_one, octa1);
1653 emit_octal_digit (stream, &seen_a_one, octa2);
1654 emit_octal_digit (stream, &seen_a_one, octa3);
1658 error (_("Internal error in octal conversion;"));
1662 cycle = cycle % BITS_IN_OCTAL;
1667 for (p = valaddr + len - 1;
1674 /* Carry out, no carry in */
1676 octa1 = (HIGH_ZERO & *p) >> 5;
1677 octa2 = (LOW_ZERO & *p) >> 2;
1678 carry = (CARRY_ZERO & *p);
1679 emit_octal_digit (stream, &seen_a_one, octa1);
1680 emit_octal_digit (stream, &seen_a_one, octa2);
1684 /* Carry in, carry out */
1686 octa1 = (carry << 1) | ((HIGH_ONE & *p) >> 7);
1687 octa2 = (MID_ONE & *p) >> 4;
1688 octa3 = (LOW_ONE & *p) >> 1;
1689 carry = (CARRY_ONE & *p);
1690 emit_octal_digit (stream, &seen_a_one, octa1);
1691 emit_octal_digit (stream, &seen_a_one, octa2);
1692 emit_octal_digit (stream, &seen_a_one, octa3);
1696 /* Carry in, no carry out */
1698 octa1 = (carry << 2) | ((HIGH_TWO & *p) >> 6);
1699 octa2 = (MID_TWO & *p) >> 3;
1700 octa3 = (LOW_TWO & *p);
1702 emit_octal_digit (stream, &seen_a_one, octa1);
1703 emit_octal_digit (stream, &seen_a_one, octa2);
1704 emit_octal_digit (stream, &seen_a_one, octa3);
1708 error (_("Internal error in octal conversion;"));
1712 cycle = cycle % BITS_IN_OCTAL;
1718 /* Possibly negate the integer represented by BYTES. It contains LEN
1719 bytes in the specified byte order. If the integer is negative,
1720 copy it into OUT_VEC, negate it, and return true. Otherwise, do
1721 nothing and return false. */
1724 maybe_negate_by_bytes (const gdb_byte *bytes, unsigned len,
1725 enum bfd_endian byte_order,
1726 gdb::byte_vector *out_vec)
1729 if (byte_order == BFD_ENDIAN_BIG)
1730 sign_byte = bytes[0];
1732 sign_byte = bytes[len - 1];
1733 if ((sign_byte & 0x80) == 0)
1736 out_vec->resize (len);
1738 /* Compute -x == 1 + ~x. */
1739 if (byte_order == BFD_ENDIAN_LITTLE)
1742 for (unsigned i = 0; i < len; ++i)
1744 unsigned tem = (0xff & ~bytes[i]) + carry;
1745 (*out_vec)[i] = tem & 0xff;
1752 for (unsigned i = len; i > 0; --i)
1754 unsigned tem = (0xff & ~bytes[i - 1]) + carry;
1755 (*out_vec)[i - 1] = tem & 0xff;
1763 /* VALADDR points to an integer of LEN bytes.
1764 Print it in decimal on stream or format it in buf. */
1767 print_decimal_chars (struct ui_file *stream, const gdb_byte *valaddr,
1768 unsigned len, bool is_signed,
1769 enum bfd_endian byte_order)
1772 #define CARRY_OUT( x ) ((x) / TEN) /* extend char to int */
1773 #define CARRY_LEFT( x ) ((x) % TEN)
1774 #define SHIFT( x ) ((x) << 4)
1775 #define LOW_NIBBLE( x ) ( (x) & 0x00F)
1776 #define HIGH_NIBBLE( x ) (((x) & 0x0F0) >> 4)
1781 int i, j, decimal_digits;
1785 gdb::byte_vector negated_bytes;
1787 && maybe_negate_by_bytes (valaddr, len, byte_order, &negated_bytes))
1789 fputs_filtered ("-", stream);
1790 valaddr = negated_bytes.data ();
1793 /* Base-ten number is less than twice as many digits
1794 as the base 16 number, which is 2 digits per byte. */
1796 decimal_len = len * 2 * 2;
1797 std::vector<unsigned char> digits (decimal_len, 0);
1799 /* Ok, we have an unknown number of bytes of data to be printed in
1802 * Given a hex number (in nibbles) as XYZ, we start by taking X and
1803 * decemalizing it as "x1 x2" in two decimal nibbles. Then we multiply
1804 * the nibbles by 16, add Y and re-decimalize. Repeat with Z.
1806 * The trick is that "digits" holds a base-10 number, but sometimes
1807 * the individual digits are > 10.
1809 * Outer loop is per nibble (hex digit) of input, from MSD end to
1812 decimal_digits = 0; /* Number of decimal digits so far */
1813 p = (byte_order == BFD_ENDIAN_BIG) ? valaddr : valaddr + len - 1;
1815 while ((byte_order == BFD_ENDIAN_BIG) ? (p < valaddr + len) : (p >= valaddr))
1818 * Multiply current base-ten number by 16 in place.
1819 * Each digit was between 0 and 9, now is between
1822 for (j = 0; j < decimal_digits; j++)
1824 digits[j] = SHIFT (digits[j]);
1827 /* Take the next nibble off the input and add it to what
1828 * we've got in the LSB position. Bottom 'digit' is now
1829 * between 0 and 159.
1831 * "flip" is used to run this loop twice for each byte.
1835 /* Take top nibble. */
1837 digits[0] += HIGH_NIBBLE (*p);
1842 /* Take low nibble and bump our pointer "p". */
1844 digits[0] += LOW_NIBBLE (*p);
1845 if (byte_order == BFD_ENDIAN_BIG)
1852 /* Re-decimalize. We have to do this often enough
1853 * that we don't overflow, but once per nibble is
1854 * overkill. Easier this way, though. Note that the
1855 * carry is often larger than 10 (e.g. max initial
1856 * carry out of lowest nibble is 15, could bubble all
1857 * the way up greater than 10). So we have to do
1858 * the carrying beyond the last current digit.
1861 for (j = 0; j < decimal_len - 1; j++)
1865 /* "/" won't handle an unsigned char with
1866 * a value that if signed would be negative.
1867 * So extend to longword int via "dummy".
1870 carry = CARRY_OUT (dummy);
1871 digits[j] = CARRY_LEFT (dummy);
1873 if (j >= decimal_digits && carry == 0)
1876 * All higher digits are 0 and we
1877 * no longer have a carry.
1879 * Note: "j" is 0-based, "decimal_digits" is
1882 decimal_digits = j + 1;
1888 /* Ok, now "digits" is the decimal representation, with
1889 the "decimal_digits" actual digits. Print! */
1891 for (i = decimal_digits - 1; i > 0 && digits[i] == 0; --i)
1896 fprintf_filtered (stream, "%1d", digits[i]);
1900 /* VALADDR points to an integer of LEN bytes. Print it in hex on stream. */
1903 print_hex_chars (struct ui_file *stream, const gdb_byte *valaddr,
1904 unsigned len, enum bfd_endian byte_order,
1909 fputs_filtered ("0x", stream);
1910 if (byte_order == BFD_ENDIAN_BIG)
1916 /* Strip leading 0 bytes, but be sure to leave at least a
1917 single byte at the end. */
1918 for (; p < valaddr + len - 1 && !*p; ++p)
1922 const gdb_byte *first = p;
1927 /* When not zero-padding, use a different format for the
1928 very first byte printed. */
1929 if (!zero_pad && p == first)
1930 fprintf_filtered (stream, "%x", *p);
1932 fprintf_filtered (stream, "%02x", *p);
1937 p = valaddr + len - 1;
1941 /* Strip leading 0 bytes, but be sure to leave at least a
1942 single byte at the end. */
1943 for (; p >= valaddr + 1 && !*p; --p)
1947 const gdb_byte *first = p;
1952 /* When not zero-padding, use a different format for the
1953 very first byte printed. */
1954 if (!zero_pad && p == first)
1955 fprintf_filtered (stream, "%x", *p);
1957 fprintf_filtered (stream, "%02x", *p);
1962 /* VALADDR points to a char integer of LEN bytes.
1963 Print it out in appropriate language form on stream.
1964 Omit any leading zero chars. */
1967 print_char_chars (struct ui_file *stream, struct type *type,
1968 const gdb_byte *valaddr,
1969 unsigned len, enum bfd_endian byte_order)
1973 if (byte_order == BFD_ENDIAN_BIG)
1976 while (p < valaddr + len - 1 && *p == 0)
1979 while (p < valaddr + len)
1981 LA_EMIT_CHAR (*p, type, stream, '\'');
1987 p = valaddr + len - 1;
1988 while (p > valaddr && *p == 0)
1991 while (p >= valaddr)
1993 LA_EMIT_CHAR (*p, type, stream, '\'');
1999 /* Print function pointer with inferior address ADDRESS onto stdio
2003 print_function_pointer_address (const struct value_print_options *options,
2004 struct gdbarch *gdbarch,
2006 struct ui_file *stream)
2009 = gdbarch_convert_from_func_ptr_addr (gdbarch, address,
2012 /* If the function pointer is represented by a description, print
2013 the address of the description. */
2014 if (options->addressprint && func_addr != address)
2016 fputs_filtered ("@", stream);
2017 fputs_filtered (paddress (gdbarch, address), stream);
2018 fputs_filtered (": ", stream);
2020 print_address_demangle (options, gdbarch, func_addr, stream, demangle);
2024 /* Print on STREAM using the given OPTIONS the index for the element
2025 at INDEX of an array whose index type is INDEX_TYPE. */
2028 maybe_print_array_index (struct type *index_type, LONGEST index,
2029 struct ui_file *stream,
2030 const struct value_print_options *options)
2032 struct value *index_value;
2034 if (!options->print_array_indexes)
2037 index_value = value_from_longest (index_type, index);
2039 LA_PRINT_ARRAY_INDEX (index_value, stream, options);
2042 /* Called by various <lang>_val_print routines to print elements of an
2043 array in the form "<elem1>, <elem2>, <elem3>, ...".
2045 (FIXME?) Assumes array element separator is a comma, which is correct
2046 for all languages currently handled.
2047 (FIXME?) Some languages have a notation for repeated array elements,
2048 perhaps we should try to use that notation when appropriate. */
2051 val_print_array_elements (struct type *type,
2052 LONGEST embedded_offset,
2053 CORE_ADDR address, struct ui_file *stream,
2056 const struct value_print_options *options,
2059 unsigned int things_printed = 0;
2061 struct type *elttype, *index_type, *base_index_type;
2063 /* Position of the array element we are examining to see
2064 whether it is repeated. */
2066 /* Number of repetitions we have detected so far. */
2068 LONGEST low_bound, high_bound;
2069 LONGEST low_pos, high_pos;
2071 elttype = TYPE_TARGET_TYPE (type);
2072 eltlen = type_length_units (check_typedef (elttype));
2073 index_type = TYPE_INDEX_TYPE (type);
2075 if (get_array_bounds (type, &low_bound, &high_bound))
2077 if (TYPE_CODE (index_type) == TYPE_CODE_RANGE)
2078 base_index_type = TYPE_TARGET_TYPE (index_type);
2080 base_index_type = index_type;
2082 /* Non-contiguous enumerations types can by used as index types
2083 in some languages (e.g. Ada). In this case, the array length
2084 shall be computed from the positions of the first and last
2085 literal in the enumeration type, and not from the values
2086 of these literals. */
2087 if (!discrete_position (base_index_type, low_bound, &low_pos)
2088 || !discrete_position (base_index_type, high_bound, &high_pos))
2090 warning (_("unable to get positions in array, use bounds instead"));
2091 low_pos = low_bound;
2092 high_pos = high_bound;
2095 /* The array length should normally be HIGH_POS - LOW_POS + 1.
2096 But we have to be a little extra careful, because some languages
2097 such as Ada allow LOW_POS to be greater than HIGH_POS for
2098 empty arrays. In that situation, the array length is just zero,
2100 if (low_pos > high_pos)
2103 len = high_pos - low_pos + 1;
2107 warning (_("unable to get bounds of array, assuming null array"));
2112 annotate_array_section_begin (i, elttype);
2114 for (; i < len && things_printed < options->print_max; i++)
2118 if (options->prettyformat_arrays)
2120 fprintf_filtered (stream, ",\n");
2121 print_spaces_filtered (2 + 2 * recurse, stream);
2125 fprintf_filtered (stream, ", ");
2128 wrap_here (n_spaces (2 + 2 * recurse));
2129 maybe_print_array_index (index_type, i + low_bound,
2134 /* Only check for reps if repeat_count_threshold is not set to
2135 UINT_MAX (unlimited). */
2136 if (options->repeat_count_threshold < UINT_MAX)
2139 && value_contents_eq (val,
2140 embedded_offset + i * eltlen,
2151 if (reps > options->repeat_count_threshold)
2153 val_print (elttype, embedded_offset + i * eltlen,
2154 address, stream, recurse + 1, val, options,
2156 annotate_elt_rep (reps);
2157 fprintf_filtered (stream, " <repeats %u times>", reps);
2158 annotate_elt_rep_end ();
2161 things_printed += options->repeat_count_threshold;
2165 val_print (elttype, embedded_offset + i * eltlen,
2167 stream, recurse + 1, val, options, current_language);
2172 annotate_array_section_end ();
2175 fprintf_filtered (stream, "...");
2179 /* Read LEN bytes of target memory at address MEMADDR, placing the
2180 results in GDB's memory at MYADDR. Returns a count of the bytes
2181 actually read, and optionally a target_xfer_status value in the
2182 location pointed to by ERRPTR if ERRPTR is non-null. */
2184 /* FIXME: cagney/1999-10-14: Only used by val_print_string. Can this
2185 function be eliminated. */
2188 partial_memory_read (CORE_ADDR memaddr, gdb_byte *myaddr,
2189 int len, int *errptr)
2191 int nread; /* Number of bytes actually read. */
2192 int errcode; /* Error from last read. */
2194 /* First try a complete read. */
2195 errcode = target_read_memory (memaddr, myaddr, len);
2203 /* Loop, reading one byte at a time until we get as much as we can. */
2204 for (errcode = 0, nread = 0; len > 0 && errcode == 0; nread++, len--)
2206 errcode = target_read_memory (memaddr++, myaddr++, 1);
2208 /* If an error, the last read was unsuccessful, so adjust count. */
2221 /* Read a string from the inferior, at ADDR, with LEN characters of WIDTH bytes
2222 each. Fetch at most FETCHLIMIT characters. BUFFER will be set to a newly
2223 allocated buffer containing the string, which the caller is responsible to
2224 free, and BYTES_READ will be set to the number of bytes read. Returns 0 on
2225 success, or a target_xfer_status on failure.
2227 If LEN > 0, reads the lesser of LEN or FETCHLIMIT characters
2228 (including eventual NULs in the middle or end of the string).
2230 If LEN is -1, stops at the first null character (not necessarily
2231 the first null byte) up to a maximum of FETCHLIMIT characters. Set
2232 FETCHLIMIT to UINT_MAX to read as many characters as possible from
2235 Unless an exception is thrown, BUFFER will always be allocated, even on
2236 failure. In this case, some characters might have been read before the
2237 failure happened. Check BYTES_READ to recognize this situation.
2239 Note: There was a FIXME asking to make this code use target_read_string,
2240 but this function is more general (can read past null characters, up to
2241 given LEN). Besides, it is used much more often than target_read_string
2242 so it is more tested. Perhaps callers of target_read_string should use
2243 this function instead? */
2246 read_string (CORE_ADDR addr, int len, int width, unsigned int fetchlimit,
2247 enum bfd_endian byte_order, gdb_byte **buffer, int *bytes_read)
2249 int errcode; /* Errno returned from bad reads. */
2250 unsigned int nfetch; /* Chars to fetch / chars fetched. */
2251 gdb_byte *bufptr; /* Pointer to next available byte in
2253 struct cleanup *old_chain = NULL; /* Top of the old cleanup chain. */
2255 /* Loop until we either have all the characters, or we encounter
2256 some error, such as bumping into the end of the address space. */
2260 old_chain = make_cleanup (free_current_contents, buffer);
2264 /* We want fetchlimit chars, so we might as well read them all in
2266 unsigned int fetchlen = std::min ((unsigned) len, fetchlimit);
2268 *buffer = (gdb_byte *) xmalloc (fetchlen * width);
2271 nfetch = partial_memory_read (addr, bufptr, fetchlen * width, &errcode)
2273 addr += nfetch * width;
2274 bufptr += nfetch * width;
2278 unsigned long bufsize = 0;
2279 unsigned int chunksize; /* Size of each fetch, in chars. */
2280 int found_nul; /* Non-zero if we found the nul char. */
2281 gdb_byte *limit; /* First location past end of fetch buffer. */
2284 /* We are looking for a NUL terminator to end the fetching, so we
2285 might as well read in blocks that are large enough to be efficient,
2286 but not so large as to be slow if fetchlimit happens to be large.
2287 So we choose the minimum of 8 and fetchlimit. We used to use 200
2288 instead of 8 but 200 is way too big for remote debugging over a
2290 chunksize = std::min (8u, fetchlimit);
2295 nfetch = std::min ((unsigned long) chunksize, fetchlimit - bufsize);
2297 if (*buffer == NULL)
2298 *buffer = (gdb_byte *) xmalloc (nfetch * width);
2300 *buffer = (gdb_byte *) xrealloc (*buffer,
2301 (nfetch + bufsize) * width);
2303 bufptr = *buffer + bufsize * width;
2306 /* Read as much as we can. */
2307 nfetch = partial_memory_read (addr, bufptr, nfetch * width, &errcode)
2310 /* Scan this chunk for the null character that terminates the string
2311 to print. If found, we don't need to fetch any more. Note
2312 that bufptr is explicitly left pointing at the next character
2313 after the null character, or at the next character after the end
2316 limit = bufptr + nfetch * width;
2317 while (bufptr < limit)
2321 c = extract_unsigned_integer (bufptr, width, byte_order);
2326 /* We don't care about any error which happened after
2327 the NUL terminator. */
2334 while (errcode == 0 /* no error */
2335 && bufptr - *buffer < fetchlimit * width /* no overrun */
2336 && !found_nul); /* haven't found NUL yet */
2339 { /* Length of string is really 0! */
2340 /* We always allocate *buffer. */
2341 *buffer = bufptr = (gdb_byte *) xmalloc (1);
2345 /* bufptr and addr now point immediately beyond the last byte which we
2346 consider part of the string (including a '\0' which ends the string). */
2347 *bytes_read = bufptr - *buffer;
2351 discard_cleanups (old_chain);
2356 /* Return true if print_wchar can display W without resorting to a
2357 numeric escape, false otherwise. */
2360 wchar_printable (gdb_wchar_t w)
2362 return (gdb_iswprint (w)
2363 || w == LCST ('\a') || w == LCST ('\b')
2364 || w == LCST ('\f') || w == LCST ('\n')
2365 || w == LCST ('\r') || w == LCST ('\t')
2366 || w == LCST ('\v'));
2369 /* A helper function that converts the contents of STRING to wide
2370 characters and then appends them to OUTPUT. */
2373 append_string_as_wide (const char *string,
2374 struct obstack *output)
2376 for (; *string; ++string)
2378 gdb_wchar_t w = gdb_btowc (*string);
2379 obstack_grow (output, &w, sizeof (gdb_wchar_t));
2383 /* Print a wide character W to OUTPUT. ORIG is a pointer to the
2384 original (target) bytes representing the character, ORIG_LEN is the
2385 number of valid bytes. WIDTH is the number of bytes in a base
2386 characters of the type. OUTPUT is an obstack to which wide
2387 characters are emitted. QUOTER is a (narrow) character indicating
2388 the style of quotes surrounding the character to be printed.
2389 NEED_ESCAPE is an in/out flag which is used to track numeric
2390 escapes across calls. */
2393 print_wchar (gdb_wint_t w, const gdb_byte *orig,
2394 int orig_len, int width,
2395 enum bfd_endian byte_order,
2396 struct obstack *output,
2397 int quoter, int *need_escapep)
2399 int need_escape = *need_escapep;
2403 /* iswprint implementation on Windows returns 1 for tab character.
2404 In order to avoid different printout on this host, we explicitly
2405 use wchar_printable function. */
2409 obstack_grow_wstr (output, LCST ("\\a"));
2412 obstack_grow_wstr (output, LCST ("\\b"));
2415 obstack_grow_wstr (output, LCST ("\\f"));
2418 obstack_grow_wstr (output, LCST ("\\n"));
2421 obstack_grow_wstr (output, LCST ("\\r"));
2424 obstack_grow_wstr (output, LCST ("\\t"));
2427 obstack_grow_wstr (output, LCST ("\\v"));
2431 if (wchar_printable (w) && (!need_escape || (!gdb_iswdigit (w)
2433 && w != LCST ('9'))))
2435 gdb_wchar_t wchar = w;
2437 if (w == gdb_btowc (quoter) || w == LCST ('\\'))
2438 obstack_grow_wstr (output, LCST ("\\"));
2439 obstack_grow (output, &wchar, sizeof (gdb_wchar_t));
2445 for (i = 0; i + width <= orig_len; i += width)
2450 value = extract_unsigned_integer (&orig[i], width,
2452 /* If the value fits in 3 octal digits, print it that
2453 way. Otherwise, print it as a hex escape. */
2455 xsnprintf (octal, sizeof (octal), "\\%.3o",
2456 (int) (value & 0777));
2458 xsnprintf (octal, sizeof (octal), "\\x%lx", (long) value);
2459 append_string_as_wide (octal, output);
2461 /* If we somehow have extra bytes, print them now. */
2462 while (i < orig_len)
2466 xsnprintf (octal, sizeof (octal), "\\%.3o", orig[i] & 0xff);
2467 append_string_as_wide (octal, output);
2478 /* Print the character C on STREAM as part of the contents of a
2479 literal string whose delimiter is QUOTER. ENCODING names the
2483 generic_emit_char (int c, struct type *type, struct ui_file *stream,
2484 int quoter, const char *encoding)
2486 enum bfd_endian byte_order
2487 = gdbarch_byte_order (get_type_arch (type));
2489 int need_escape = 0;
2491 buf = (gdb_byte *) alloca (TYPE_LENGTH (type));
2492 pack_long (buf, type, c);
2494 wchar_iterator iter (buf, TYPE_LENGTH (type), encoding, TYPE_LENGTH (type));
2496 /* This holds the printable form of the wchar_t data. */
2497 auto_obstack wchar_buf;
2503 const gdb_byte *buf;
2505 int print_escape = 1;
2506 enum wchar_iterate_result result;
2508 num_chars = iter.iterate (&result, &chars, &buf, &buflen);
2513 /* If all characters are printable, print them. Otherwise,
2514 we're going to have to print an escape sequence. We
2515 check all characters because we want to print the target
2516 bytes in the escape sequence, and we don't know character
2517 boundaries there. */
2521 for (i = 0; i < num_chars; ++i)
2522 if (!wchar_printable (chars[i]))
2530 for (i = 0; i < num_chars; ++i)
2531 print_wchar (chars[i], buf, buflen,
2532 TYPE_LENGTH (type), byte_order,
2533 &wchar_buf, quoter, &need_escape);
2537 /* This handles the NUM_CHARS == 0 case as well. */
2539 print_wchar (gdb_WEOF, buf, buflen, TYPE_LENGTH (type),
2540 byte_order, &wchar_buf, quoter, &need_escape);
2543 /* The output in the host encoding. */
2544 auto_obstack output;
2546 convert_between_encodings (INTERMEDIATE_ENCODING, host_charset (),
2547 (gdb_byte *) obstack_base (&wchar_buf),
2548 obstack_object_size (&wchar_buf),
2549 sizeof (gdb_wchar_t), &output, translit_char);
2550 obstack_1grow (&output, '\0');
2552 fputs_filtered ((const char *) obstack_base (&output), stream);
2555 /* Return the repeat count of the next character/byte in ITER,
2556 storing the result in VEC. */
2559 count_next_character (wchar_iterator *iter,
2560 VEC (converted_character_d) **vec)
2562 struct converted_character *current;
2564 if (VEC_empty (converted_character_d, *vec))
2566 struct converted_character tmp;
2570 = iter->iterate (&tmp.result, &chars, &tmp.buf, &tmp.buflen);
2571 if (tmp.num_chars > 0)
2573 gdb_assert (tmp.num_chars < MAX_WCHARS);
2574 memcpy (tmp.chars, chars, tmp.num_chars * sizeof (gdb_wchar_t));
2576 VEC_safe_push (converted_character_d, *vec, &tmp);
2579 current = VEC_last (converted_character_d, *vec);
2581 /* Count repeated characters or bytes. */
2582 current->repeat_count = 1;
2583 if (current->num_chars == -1)
2591 struct converted_character d;
2598 /* Get the next character. */
2599 d.num_chars = iter->iterate (&d.result, &chars, &d.buf, &d.buflen);
2601 /* If a character was successfully converted, save the character
2602 into the converted character. */
2603 if (d.num_chars > 0)
2605 gdb_assert (d.num_chars < MAX_WCHARS);
2606 memcpy (d.chars, chars, WCHAR_BUFLEN (d.num_chars));
2609 /* Determine if the current character is the same as this
2611 if (d.num_chars == current->num_chars && d.result == current->result)
2613 /* There are two cases to consider:
2615 1) Equality of converted character (num_chars > 0)
2616 2) Equality of non-converted character (num_chars == 0) */
2617 if ((current->num_chars > 0
2618 && memcmp (current->chars, d.chars,
2619 WCHAR_BUFLEN (current->num_chars)) == 0)
2620 || (current->num_chars == 0
2621 && current->buflen == d.buflen
2622 && memcmp (current->buf, d.buf, current->buflen) == 0))
2623 ++current->repeat_count;
2631 /* Push this next converted character onto the result vector. */
2632 repeat = current->repeat_count;
2633 VEC_safe_push (converted_character_d, *vec, &d);
2638 /* Print the characters in CHARS to the OBSTACK. QUOTE_CHAR is the quote
2639 character to use with string output. WIDTH is the size of the output
2640 character type. BYTE_ORDER is the the target byte order. OPTIONS
2641 is the user's print options. */
2644 print_converted_chars_to_obstack (struct obstack *obstack,
2645 VEC (converted_character_d) *chars,
2646 int quote_char, int width,
2647 enum bfd_endian byte_order,
2648 const struct value_print_options *options)
2651 struct converted_character *elem;
2652 enum {START, SINGLE, REPEAT, INCOMPLETE, FINISH} state, last;
2653 gdb_wchar_t wide_quote_char = gdb_btowc (quote_char);
2654 int need_escape = 0;
2656 /* Set the start state. */
2658 last = state = START;
2666 /* Nothing to do. */
2673 /* We are outputting a single character
2674 (< options->repeat_count_threshold). */
2678 /* We were outputting some other type of content, so we
2679 must output and a comma and a quote. */
2681 obstack_grow_wstr (obstack, LCST (", "));
2682 obstack_grow (obstack, &wide_quote_char, sizeof (gdb_wchar_t));
2684 /* Output the character. */
2685 for (j = 0; j < elem->repeat_count; ++j)
2687 if (elem->result == wchar_iterate_ok)
2688 print_wchar (elem->chars[0], elem->buf, elem->buflen, width,
2689 byte_order, obstack, quote_char, &need_escape);
2691 print_wchar (gdb_WEOF, elem->buf, elem->buflen, width,
2692 byte_order, obstack, quote_char, &need_escape);
2702 /* We are outputting a character with a repeat count
2703 greater than options->repeat_count_threshold. */
2707 /* We were outputting a single string. Terminate the
2709 obstack_grow (obstack, &wide_quote_char, sizeof (gdb_wchar_t));
2712 obstack_grow_wstr (obstack, LCST (", "));
2714 /* Output the character and repeat string. */
2715 obstack_grow_wstr (obstack, LCST ("'"));
2716 if (elem->result == wchar_iterate_ok)
2717 print_wchar (elem->chars[0], elem->buf, elem->buflen, width,
2718 byte_order, obstack, quote_char, &need_escape);
2720 print_wchar (gdb_WEOF, elem->buf, elem->buflen, width,
2721 byte_order, obstack, quote_char, &need_escape);
2722 obstack_grow_wstr (obstack, LCST ("'"));
2723 s = xstrprintf (_(" <repeats %u times>"), elem->repeat_count);
2724 for (j = 0; s[j]; ++j)
2726 gdb_wchar_t w = gdb_btowc (s[j]);
2727 obstack_grow (obstack, &w, sizeof (gdb_wchar_t));
2734 /* We are outputting an incomplete sequence. */
2737 /* If we were outputting a string of SINGLE characters,
2738 terminate the quote. */
2739 obstack_grow (obstack, &wide_quote_char, sizeof (gdb_wchar_t));
2742 obstack_grow_wstr (obstack, LCST (", "));
2744 /* Output the incomplete sequence string. */
2745 obstack_grow_wstr (obstack, LCST ("<incomplete sequence "));
2746 print_wchar (gdb_WEOF, elem->buf, elem->buflen, width, byte_order,
2747 obstack, 0, &need_escape);
2748 obstack_grow_wstr (obstack, LCST (">"));
2750 /* We do not attempt to outupt anything after this. */
2755 /* All done. If we were outputting a string of SINGLE
2756 characters, the string must be terminated. Otherwise,
2757 REPEAT and INCOMPLETE are always left properly terminated. */
2759 obstack_grow (obstack, &wide_quote_char, sizeof (gdb_wchar_t));
2764 /* Get the next element and state. */
2766 if (state != FINISH)
2768 elem = VEC_index (converted_character_d, chars, idx++);
2769 switch (elem->result)
2771 case wchar_iterate_ok:
2772 case wchar_iterate_invalid:
2773 if (elem->repeat_count > options->repeat_count_threshold)
2779 case wchar_iterate_incomplete:
2783 case wchar_iterate_eof:
2791 /* Print the character string STRING, printing at most LENGTH
2792 characters. LENGTH is -1 if the string is nul terminated. TYPE is
2793 the type of each character. OPTIONS holds the printing options;
2794 printing stops early if the number hits print_max; repeat counts
2795 are printed as appropriate. Print ellipses at the end if we had to
2796 stop before printing LENGTH characters, or if FORCE_ELLIPSES.
2797 QUOTE_CHAR is the character to print at each end of the string. If
2798 C_STYLE_TERMINATOR is true, and the last character is 0, then it is
2802 generic_printstr (struct ui_file *stream, struct type *type,
2803 const gdb_byte *string, unsigned int length,
2804 const char *encoding, int force_ellipses,
2805 int quote_char, int c_style_terminator,
2806 const struct value_print_options *options)
2808 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
2810 int width = TYPE_LENGTH (type);
2811 struct cleanup *cleanup;
2813 struct converted_character *last;
2814 VEC (converted_character_d) *converted_chars;
2818 unsigned long current_char = 1;
2820 for (i = 0; current_char; ++i)
2823 current_char = extract_unsigned_integer (string + i * width,
2829 /* If the string was not truncated due to `set print elements', and
2830 the last byte of it is a null, we don't print that, in
2831 traditional C style. */
2832 if (c_style_terminator
2835 && (extract_unsigned_integer (string + (length - 1) * width,
2836 width, byte_order) == 0))
2841 fputs_filtered ("\"\"", stream);
2845 /* Arrange to iterate over the characters, in wchar_t form. */
2846 wchar_iterator iter (string, length * width, encoding, width);
2847 converted_chars = NULL;
2848 cleanup = make_cleanup (VEC_cleanup (converted_character_d),
2851 /* Convert characters until the string is over or the maximum
2852 number of printed characters has been reached. */
2854 while (i < options->print_max)
2860 /* Grab the next character and repeat count. */
2861 r = count_next_character (&iter, &converted_chars);
2863 /* If less than zero, the end of the input string was reached. */
2867 /* Otherwise, add the count to the total print count and get
2868 the next character. */
2872 /* Get the last element and determine if the entire string was
2874 last = VEC_last (converted_character_d, converted_chars);
2875 finished = (last->result == wchar_iterate_eof);
2877 /* Ensure that CONVERTED_CHARS is terminated. */
2878 last->result = wchar_iterate_eof;
2880 /* WCHAR_BUF is the obstack we use to represent the string in
2882 auto_obstack wchar_buf;
2884 /* Print the output string to the obstack. */
2885 print_converted_chars_to_obstack (&wchar_buf, converted_chars, quote_char,
2886 width, byte_order, options);
2888 if (force_ellipses || !finished)
2889 obstack_grow_wstr (&wchar_buf, LCST ("..."));
2891 /* OUTPUT is where we collect `char's for printing. */
2892 auto_obstack output;
2894 convert_between_encodings (INTERMEDIATE_ENCODING, host_charset (),
2895 (gdb_byte *) obstack_base (&wchar_buf),
2896 obstack_object_size (&wchar_buf),
2897 sizeof (gdb_wchar_t), &output, translit_char);
2898 obstack_1grow (&output, '\0');
2900 fputs_filtered ((const char *) obstack_base (&output), stream);
2902 do_cleanups (cleanup);
2905 /* Print a string from the inferior, starting at ADDR and printing up to LEN
2906 characters, of WIDTH bytes a piece, to STREAM. If LEN is -1, printing
2907 stops at the first null byte, otherwise printing proceeds (including null
2908 bytes) until either print_max or LEN characters have been printed,
2909 whichever is smaller. ENCODING is the name of the string's
2910 encoding. It can be NULL, in which case the target encoding is
2914 val_print_string (struct type *elttype, const char *encoding,
2915 CORE_ADDR addr, int len,
2916 struct ui_file *stream,
2917 const struct value_print_options *options)
2919 int force_ellipsis = 0; /* Force ellipsis to be printed if nonzero. */
2920 int err; /* Non-zero if we got a bad read. */
2921 int found_nul; /* Non-zero if we found the nul char. */
2922 unsigned int fetchlimit; /* Maximum number of chars to print. */
2924 gdb_byte *buffer = NULL; /* Dynamically growable fetch buffer. */
2925 struct cleanup *old_chain = NULL; /* Top of the old cleanup chain. */
2926 struct gdbarch *gdbarch = get_type_arch (elttype);
2927 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2928 int width = TYPE_LENGTH (elttype);
2930 /* First we need to figure out the limit on the number of characters we are
2931 going to attempt to fetch and print. This is actually pretty simple. If
2932 LEN >= zero, then the limit is the minimum of LEN and print_max. If
2933 LEN is -1, then the limit is print_max. This is true regardless of
2934 whether print_max is zero, UINT_MAX (unlimited), or something in between,
2935 because finding the null byte (or available memory) is what actually
2936 limits the fetch. */
2938 fetchlimit = (len == -1 ? options->print_max : std::min ((unsigned) len,
2939 options->print_max));
2941 err = read_string (addr, len, width, fetchlimit, byte_order,
2942 &buffer, &bytes_read);
2943 old_chain = make_cleanup (xfree, buffer);
2947 /* We now have either successfully filled the buffer to fetchlimit,
2948 or terminated early due to an error or finding a null char when
2951 /* Determine found_nul by looking at the last character read. */
2953 if (bytes_read >= width)
2954 found_nul = extract_unsigned_integer (buffer + bytes_read - width, width,
2956 if (len == -1 && !found_nul)
2960 /* We didn't find a NUL terminator we were looking for. Attempt
2961 to peek at the next character. If not successful, or it is not
2962 a null byte, then force ellipsis to be printed. */
2964 peekbuf = (gdb_byte *) alloca (width);
2966 if (target_read_memory (addr, peekbuf, width) == 0
2967 && extract_unsigned_integer (peekbuf, width, byte_order) != 0)
2970 else if ((len >= 0 && err != 0) || (len > bytes_read / width))
2972 /* Getting an error when we have a requested length, or fetching less
2973 than the number of characters actually requested, always make us
2978 /* If we get an error before fetching anything, don't print a string.
2979 But if we fetch something and then get an error, print the string
2980 and then the error message. */
2981 if (err == 0 || bytes_read > 0)
2983 LA_PRINT_STRING (stream, elttype, buffer, bytes_read / width,
2984 encoding, force_ellipsis, options);
2989 std::string str = memory_error_message (TARGET_XFER_E_IO, gdbarch, addr);
2991 fprintf_filtered (stream, "<error: ");
2992 fputs_filtered (str.c_str (), stream);
2993 fprintf_filtered (stream, ">");
2997 do_cleanups (old_chain);
2999 return (bytes_read / width);
3003 /* The 'set input-radix' command writes to this auxiliary variable.
3004 If the requested radix is valid, INPUT_RADIX is updated; otherwise,
3005 it is left unchanged. */
3007 static unsigned input_radix_1 = 10;
3009 /* Validate an input or output radix setting, and make sure the user
3010 knows what they really did here. Radix setting is confusing, e.g.
3011 setting the input radix to "10" never changes it! */
3014 set_input_radix (char *args, int from_tty, struct cmd_list_element *c)
3016 set_input_radix_1 (from_tty, input_radix_1);
3020 set_input_radix_1 (int from_tty, unsigned radix)
3022 /* We don't currently disallow any input radix except 0 or 1, which don't
3023 make any mathematical sense. In theory, we can deal with any input
3024 radix greater than 1, even if we don't have unique digits for every
3025 value from 0 to radix-1, but in practice we lose on large radix values.
3026 We should either fix the lossage or restrict the radix range more.
3031 input_radix_1 = input_radix;
3032 error (_("Nonsense input radix ``decimal %u''; input radix unchanged."),
3035 input_radix_1 = input_radix = radix;
3038 printf_filtered (_("Input radix now set to "
3039 "decimal %u, hex %x, octal %o.\n"),
3040 radix, radix, radix);
3044 /* The 'set output-radix' command writes to this auxiliary variable.
3045 If the requested radix is valid, OUTPUT_RADIX is updated,
3046 otherwise, it is left unchanged. */
3048 static unsigned output_radix_1 = 10;
3051 set_output_radix (char *args, int from_tty, struct cmd_list_element *c)
3053 set_output_radix_1 (from_tty, output_radix_1);
3057 set_output_radix_1 (int from_tty, unsigned radix)
3059 /* Validate the radix and disallow ones that we aren't prepared to
3060 handle correctly, leaving the radix unchanged. */
3064 user_print_options.output_format = 'x'; /* hex */
3067 user_print_options.output_format = 0; /* decimal */
3070 user_print_options.output_format = 'o'; /* octal */
3073 output_radix_1 = output_radix;
3074 error (_("Unsupported output radix ``decimal %u''; "
3075 "output radix unchanged."),
3078 output_radix_1 = output_radix = radix;
3081 printf_filtered (_("Output radix now set to "
3082 "decimal %u, hex %x, octal %o.\n"),
3083 radix, radix, radix);
3087 /* Set both the input and output radix at once. Try to set the output radix
3088 first, since it has the most restrictive range. An radix that is valid as
3089 an output radix is also valid as an input radix.
3091 It may be useful to have an unusual input radix. If the user wishes to
3092 set an input radix that is not valid as an output radix, he needs to use
3093 the 'set input-radix' command. */
3096 set_radix (char *arg, int from_tty)
3100 radix = (arg == NULL) ? 10 : parse_and_eval_long (arg);
3101 set_output_radix_1 (0, radix);
3102 set_input_radix_1 (0, radix);
3105 printf_filtered (_("Input and output radices now set to "
3106 "decimal %u, hex %x, octal %o.\n"),
3107 radix, radix, radix);
3111 /* Show both the input and output radices. */
3114 show_radix (char *arg, int from_tty)
3118 if (input_radix == output_radix)
3120 printf_filtered (_("Input and output radices set to "
3121 "decimal %u, hex %x, octal %o.\n"),
3122 input_radix, input_radix, input_radix);
3126 printf_filtered (_("Input radix set to decimal "
3127 "%u, hex %x, octal %o.\n"),
3128 input_radix, input_radix, input_radix);
3129 printf_filtered (_("Output radix set to decimal "
3130 "%u, hex %x, octal %o.\n"),
3131 output_radix, output_radix, output_radix);
3138 set_print (char *arg, int from_tty)
3141 "\"set print\" must be followed by the name of a print subcommand.\n");
3142 help_list (setprintlist, "set print ", all_commands, gdb_stdout);
3146 show_print (char *args, int from_tty)
3148 cmd_show_list (showprintlist, from_tty, "");
3152 set_print_raw (char *arg, int from_tty)
3155 "\"set print raw\" must be followed by the name of a \"print raw\" subcommand.\n");
3156 help_list (setprintrawlist, "set print raw ", all_commands, gdb_stdout);
3160 show_print_raw (char *args, int from_tty)
3162 cmd_show_list (showprintrawlist, from_tty, "");
3167 _initialize_valprint (void)
3169 add_prefix_cmd ("print", no_class, set_print,
3170 _("Generic command for setting how things print."),
3171 &setprintlist, "set print ", 0, &setlist);
3172 add_alias_cmd ("p", "print", no_class, 1, &setlist);
3173 /* Prefer set print to set prompt. */
3174 add_alias_cmd ("pr", "print", no_class, 1, &setlist);
3176 add_prefix_cmd ("print", no_class, show_print,
3177 _("Generic command for showing print settings."),
3178 &showprintlist, "show print ", 0, &showlist);
3179 add_alias_cmd ("p", "print", no_class, 1, &showlist);
3180 add_alias_cmd ("pr", "print", no_class, 1, &showlist);
3182 add_prefix_cmd ("raw", no_class, set_print_raw,
3184 Generic command for setting what things to print in \"raw\" mode."),
3185 &setprintrawlist, "set print raw ", 0, &setprintlist);
3186 add_prefix_cmd ("raw", no_class, show_print_raw,
3187 _("Generic command for showing \"print raw\" settings."),
3188 &showprintrawlist, "show print raw ", 0, &showprintlist);
3190 add_setshow_uinteger_cmd ("elements", no_class,
3191 &user_print_options.print_max, _("\
3192 Set limit on string chars or array elements to print."), _("\
3193 Show limit on string chars or array elements to print."), _("\
3194 \"set print elements unlimited\" causes there to be no limit."),
3197 &setprintlist, &showprintlist);
3199 add_setshow_boolean_cmd ("null-stop", no_class,
3200 &user_print_options.stop_print_at_null, _("\
3201 Set printing of char arrays to stop at first null char."), _("\
3202 Show printing of char arrays to stop at first null char."), NULL,
3204 show_stop_print_at_null,
3205 &setprintlist, &showprintlist);
3207 add_setshow_uinteger_cmd ("repeats", no_class,
3208 &user_print_options.repeat_count_threshold, _("\
3209 Set threshold for repeated print elements."), _("\
3210 Show threshold for repeated print elements."), _("\
3211 \"set print repeats unlimited\" causes all elements to be individually printed."),
3213 show_repeat_count_threshold,
3214 &setprintlist, &showprintlist);
3216 add_setshow_boolean_cmd ("pretty", class_support,
3217 &user_print_options.prettyformat_structs, _("\
3218 Set pretty formatting of structures."), _("\
3219 Show pretty formatting of structures."), NULL,
3221 show_prettyformat_structs,
3222 &setprintlist, &showprintlist);
3224 add_setshow_boolean_cmd ("union", class_support,
3225 &user_print_options.unionprint, _("\
3226 Set printing of unions interior to structures."), _("\
3227 Show printing of unions interior to structures."), NULL,
3230 &setprintlist, &showprintlist);
3232 add_setshow_boolean_cmd ("array", class_support,
3233 &user_print_options.prettyformat_arrays, _("\
3234 Set pretty formatting of arrays."), _("\
3235 Show pretty formatting of arrays."), NULL,
3237 show_prettyformat_arrays,
3238 &setprintlist, &showprintlist);
3240 add_setshow_boolean_cmd ("address", class_support,
3241 &user_print_options.addressprint, _("\
3242 Set printing of addresses."), _("\
3243 Show printing of addresses."), NULL,
3246 &setprintlist, &showprintlist);
3248 add_setshow_boolean_cmd ("symbol", class_support,
3249 &user_print_options.symbol_print, _("\
3250 Set printing of symbol names when printing pointers."), _("\
3251 Show printing of symbol names when printing pointers."),
3254 &setprintlist, &showprintlist);
3256 add_setshow_zuinteger_cmd ("input-radix", class_support, &input_radix_1,
3258 Set default input radix for entering numbers."), _("\
3259 Show default input radix for entering numbers."), NULL,
3262 &setlist, &showlist);
3264 add_setshow_zuinteger_cmd ("output-radix", class_support, &output_radix_1,
3266 Set default output radix for printing of values."), _("\
3267 Show default output radix for printing of values."), NULL,
3270 &setlist, &showlist);
3272 /* The "set radix" and "show radix" commands are special in that
3273 they are like normal set and show commands but allow two normally
3274 independent variables to be either set or shown with a single
3275 command. So the usual deprecated_add_set_cmd() and [deleted]
3276 add_show_from_set() commands aren't really appropriate. */
3277 /* FIXME: i18n: With the new add_setshow_integer command, that is no
3278 longer true - show can display anything. */
3279 add_cmd ("radix", class_support, set_radix, _("\
3280 Set default input and output number radices.\n\
3281 Use 'set input-radix' or 'set output-radix' to independently set each.\n\
3282 Without an argument, sets both radices back to the default value of 10."),
3284 add_cmd ("radix", class_support, show_radix, _("\
3285 Show the default input and output number radices.\n\
3286 Use 'show input-radix' or 'show output-radix' to independently show each."),
3289 add_setshow_boolean_cmd ("array-indexes", class_support,
3290 &user_print_options.print_array_indexes, _("\
3291 Set printing of array indexes."), _("\
3292 Show printing of array indexes"), NULL, NULL, show_print_array_indexes,
3293 &setprintlist, &showprintlist);