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 "target-float.h"
31 #include "extension.h"
33 #include "gdb_obstack.h"
35 #include "typeprint.h"
38 #include "common/byte-vector.h"
40 /* Maximum number of wchars returned from wchar_iterate. */
43 /* A convenience macro to compute the size of a wchar_t buffer containing X
45 #define WCHAR_BUFLEN(X) ((X) * sizeof (gdb_wchar_t))
47 /* Character buffer size saved while iterating over wchars. */
48 #define WCHAR_BUFLEN_MAX WCHAR_BUFLEN (MAX_WCHARS)
50 /* A structure to encapsulate state information from iterated
51 character conversions. */
52 struct converted_character
54 /* The number of characters converted. */
57 /* The result of the conversion. See charset.h for more. */
58 enum wchar_iterate_result result
;
60 /* The (saved) converted character(s). */
61 gdb_wchar_t chars
[WCHAR_BUFLEN_MAX
];
63 /* The first converted target byte. */
66 /* The number of bytes converted. */
69 /* How many times this character(s) is repeated. */
73 typedef struct converted_character converted_character_d
;
74 DEF_VEC_O (converted_character_d
);
76 /* Command lists for set/show print raw. */
77 struct cmd_list_element
*setprintrawlist
;
78 struct cmd_list_element
*showprintrawlist
;
80 /* Prototypes for local functions */
82 static int partial_memory_read (CORE_ADDR memaddr
, gdb_byte
*myaddr
,
83 int len
, int *errptr
);
85 static void set_input_radix (char *, int, struct cmd_list_element
*);
87 static void set_input_radix_1 (int, unsigned);
89 static void set_output_radix (char *, int, struct cmd_list_element
*);
91 static void set_output_radix_1 (int, unsigned);
93 static void val_print_type_code_flags (struct type
*type
,
94 const gdb_byte
*valaddr
,
95 struct ui_file
*stream
);
97 #define PRINT_MAX_DEFAULT 200 /* Start print_max off at this value. */
99 struct value_print_options user_print_options
=
101 Val_prettyformat_default
, /* prettyformat */
102 0, /* prettyformat_arrays */
103 0, /* prettyformat_structs */
106 1, /* addressprint */
108 PRINT_MAX_DEFAULT
, /* print_max */
109 10, /* repeat_count_threshold */
110 0, /* output_format */
112 0, /* stop_print_at_null */
113 0, /* print_array_indexes */
115 1, /* static_field_print */
116 1, /* pascal_static_field_print */
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 /* See its definition in value.h. */
294 valprint_check_validity (struct ui_file
*stream
,
296 LONGEST embedded_offset
,
297 const struct value
*val
)
299 type
= check_typedef (type
);
301 if (type_not_associated (type
))
303 val_print_not_associated (stream
);
307 if (type_not_allocated (type
))
309 val_print_not_allocated (stream
);
313 if (TYPE_CODE (type
) != TYPE_CODE_UNION
314 && TYPE_CODE (type
) != TYPE_CODE_STRUCT
315 && TYPE_CODE (type
) != TYPE_CODE_ARRAY
)
317 if (value_bits_any_optimized_out (val
,
318 TARGET_CHAR_BIT
* embedded_offset
,
319 TARGET_CHAR_BIT
* TYPE_LENGTH (type
)))
321 val_print_optimized_out (val
, stream
);
325 if (value_bits_synthetic_pointer (val
, TARGET_CHAR_BIT
* embedded_offset
,
326 TARGET_CHAR_BIT
* TYPE_LENGTH (type
)))
328 const int is_ref
= TYPE_CODE (type
) == TYPE_CODE_REF
;
329 int ref_is_addressable
= 0;
333 const struct value
*deref_val
= coerce_ref_if_computed (val
);
335 if (deref_val
!= NULL
)
336 ref_is_addressable
= value_lval_const (deref_val
) == lval_memory
;
339 if (!is_ref
|| !ref_is_addressable
)
340 fputs_filtered (_("<synthetic pointer>"), stream
);
342 /* C++ references should be valid even if they're synthetic. */
346 if (!value_bytes_available (val
, embedded_offset
, TYPE_LENGTH (type
)))
348 val_print_unavailable (stream
);
357 val_print_optimized_out (const struct value
*val
, struct ui_file
*stream
)
359 if (val
!= NULL
&& value_lval_const (val
) == lval_register
)
360 val_print_not_saved (stream
);
362 fprintf_filtered (stream
, _("<optimized out>"));
366 val_print_not_saved (struct ui_file
*stream
)
368 fprintf_filtered (stream
, _("<not saved>"));
372 val_print_unavailable (struct ui_file
*stream
)
374 fprintf_filtered (stream
, _("<unavailable>"));
378 val_print_invalid_address (struct ui_file
*stream
)
380 fprintf_filtered (stream
, _("<invalid address>"));
383 /* Print a pointer based on the type of its target.
385 Arguments to this functions are roughly the same as those in
386 generic_val_print. A difference is that ADDRESS is the address to print,
387 with embedded_offset already added. ELTTYPE represents
388 the pointed type after check_typedef. */
391 print_unpacked_pointer (struct type
*type
, struct type
*elttype
,
392 CORE_ADDR address
, struct ui_file
*stream
,
393 const struct value_print_options
*options
)
395 struct gdbarch
*gdbarch
= get_type_arch (type
);
397 if (TYPE_CODE (elttype
) == TYPE_CODE_FUNC
)
399 /* Try to print what function it points to. */
400 print_function_pointer_address (options
, gdbarch
, address
, stream
);
404 if (options
->symbol_print
)
405 print_address_demangle (options
, gdbarch
, address
, stream
, demangle
);
406 else if (options
->addressprint
)
407 fputs_filtered (paddress (gdbarch
, address
), stream
);
410 /* generic_val_print helper for TYPE_CODE_ARRAY. */
413 generic_val_print_array (struct type
*type
,
414 int embedded_offset
, CORE_ADDR address
,
415 struct ui_file
*stream
, int recurse
,
416 struct value
*original_value
,
417 const struct value_print_options
*options
,
419 generic_val_print_decorations
*decorations
)
421 struct type
*unresolved_elttype
= TYPE_TARGET_TYPE (type
);
422 struct type
*elttype
= check_typedef (unresolved_elttype
);
424 if (TYPE_LENGTH (type
) > 0 && TYPE_LENGTH (unresolved_elttype
) > 0)
426 LONGEST low_bound
, high_bound
;
428 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
429 error (_("Could not determine the array high bound"));
431 if (options
->prettyformat_arrays
)
433 print_spaces_filtered (2 + 2 * recurse
, stream
);
436 fputs_filtered (decorations
->array_start
, stream
);
437 val_print_array_elements (type
, embedded_offset
,
439 recurse
, original_value
, options
, 0);
440 fputs_filtered (decorations
->array_end
, stream
);
444 /* Array of unspecified length: treat like pointer to first elt. */
445 print_unpacked_pointer (type
, elttype
, address
+ embedded_offset
, stream
,
451 /* generic_val_print helper for TYPE_CODE_PTR. */
454 generic_val_print_ptr (struct type
*type
,
455 int embedded_offset
, struct ui_file
*stream
,
456 struct value
*original_value
,
457 const struct value_print_options
*options
)
459 struct gdbarch
*gdbarch
= get_type_arch (type
);
460 int unit_size
= gdbarch_addressable_memory_unit_size (gdbarch
);
462 if (options
->format
&& options
->format
!= 's')
464 val_print_scalar_formatted (type
, embedded_offset
,
465 original_value
, options
, 0, stream
);
469 struct type
*unresolved_elttype
= TYPE_TARGET_TYPE(type
);
470 struct type
*elttype
= check_typedef (unresolved_elttype
);
471 const gdb_byte
*valaddr
= value_contents_for_printing (original_value
);
472 CORE_ADDR addr
= unpack_pointer (type
,
473 valaddr
+ embedded_offset
* unit_size
);
475 print_unpacked_pointer (type
, elttype
, addr
, stream
, options
);
480 /* generic_val_print helper for TYPE_CODE_MEMBERPTR. */
483 generic_val_print_memberptr (struct type
*type
,
484 int embedded_offset
, struct ui_file
*stream
,
485 struct value
*original_value
,
486 const struct value_print_options
*options
)
488 val_print_scalar_formatted (type
, embedded_offset
,
489 original_value
, options
, 0, stream
);
492 /* Print '@' followed by the address contained in ADDRESS_BUFFER. */
495 print_ref_address (struct type
*type
, const gdb_byte
*address_buffer
,
496 int embedded_offset
, struct ui_file
*stream
)
498 struct gdbarch
*gdbarch
= get_type_arch (type
);
500 if (address_buffer
!= NULL
)
503 = extract_typed_address (address_buffer
+ embedded_offset
, type
);
505 fprintf_filtered (stream
, "@");
506 fputs_filtered (paddress (gdbarch
, address
), stream
);
508 /* Else: we have a non-addressable value, such as a DW_AT_const_value. */
511 /* If VAL is addressable, return the value contents buffer of a value that
512 represents a pointer to VAL. Otherwise return NULL. */
514 static const gdb_byte
*
515 get_value_addr_contents (struct value
*deref_val
)
517 gdb_assert (deref_val
!= NULL
);
519 if (value_lval_const (deref_val
) == lval_memory
)
520 return value_contents_for_printing_const (value_addr (deref_val
));
523 /* We have a non-addressable value, such as a DW_AT_const_value. */
528 /* generic_val_print helper for TYPE_CODE_{RVALUE_,}REF. */
531 generic_val_print_ref (struct type
*type
,
532 int embedded_offset
, struct ui_file
*stream
, int recurse
,
533 struct value
*original_value
,
534 const struct value_print_options
*options
)
536 struct type
*elttype
= check_typedef (TYPE_TARGET_TYPE (type
));
537 struct value
*deref_val
= NULL
;
538 const int value_is_synthetic
539 = value_bits_synthetic_pointer (original_value
,
540 TARGET_CHAR_BIT
* embedded_offset
,
541 TARGET_CHAR_BIT
* TYPE_LENGTH (type
));
542 const int must_coerce_ref
= ((options
->addressprint
&& value_is_synthetic
)
543 || options
->deref_ref
);
544 const int type_is_defined
= TYPE_CODE (elttype
) != TYPE_CODE_UNDEF
;
545 const gdb_byte
*valaddr
= value_contents_for_printing (original_value
);
547 if (must_coerce_ref
&& type_is_defined
)
549 deref_val
= coerce_ref_if_computed (original_value
);
551 if (deref_val
!= NULL
)
553 /* More complicated computed references are not supported. */
554 gdb_assert (embedded_offset
== 0);
557 deref_val
= value_at (TYPE_TARGET_TYPE (type
),
558 unpack_pointer (type
, valaddr
+ embedded_offset
));
560 /* Else, original_value isn't a synthetic reference or we don't have to print
561 the reference's contents.
563 Notice that for references to TYPE_CODE_STRUCT, 'set print object on' will
564 cause original_value to be a not_lval instead of an lval_computed,
565 which will make value_bits_synthetic_pointer return false.
566 This happens because if options->objectprint is true, c_value_print will
567 overwrite original_value's contents with the result of coercing
568 the reference through value_addr, and then set its type back to
569 TYPE_CODE_REF. In that case we don't have to coerce the reference again;
570 we can simply treat it as non-synthetic and move on. */
572 if (options
->addressprint
)
574 const gdb_byte
*address
= (value_is_synthetic
&& type_is_defined
575 ? get_value_addr_contents (deref_val
)
578 print_ref_address (type
, address
, embedded_offset
, stream
);
580 if (options
->deref_ref
)
581 fputs_filtered (": ", stream
);
584 if (options
->deref_ref
)
587 common_val_print (deref_val
, stream
, recurse
, options
,
590 fputs_filtered ("???", stream
);
594 /* Helper function for generic_val_print_enum.
595 This is also used to print enums in TYPE_CODE_FLAGS values. */
598 generic_val_print_enum_1 (struct type
*type
, LONGEST val
,
599 struct ui_file
*stream
)
604 len
= TYPE_NFIELDS (type
);
605 for (i
= 0; i
< len
; i
++)
608 if (val
== TYPE_FIELD_ENUMVAL (type
, i
))
615 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
617 else if (TYPE_FLAG_ENUM (type
))
621 /* We have a "flag" enum, so we try to decompose it into
622 pieces as appropriate. A flag enum has disjoint
623 constants by definition. */
624 fputs_filtered ("(", stream
);
625 for (i
= 0; i
< len
; ++i
)
629 if ((val
& TYPE_FIELD_ENUMVAL (type
, i
)) != 0)
632 fputs_filtered (" | ", stream
);
635 val
&= ~TYPE_FIELD_ENUMVAL (type
, i
);
636 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
640 if (first
|| val
!= 0)
643 fputs_filtered (" | ", stream
);
644 fputs_filtered ("unknown: ", stream
);
645 print_longest (stream
, 'd', 0, val
);
648 fputs_filtered (")", stream
);
651 print_longest (stream
, 'd', 0, val
);
654 /* generic_val_print helper for TYPE_CODE_ENUM. */
657 generic_val_print_enum (struct type
*type
,
658 int embedded_offset
, struct ui_file
*stream
,
659 struct value
*original_value
,
660 const struct value_print_options
*options
)
663 struct gdbarch
*gdbarch
= get_type_arch (type
);
664 int unit_size
= gdbarch_addressable_memory_unit_size (gdbarch
);
668 val_print_scalar_formatted (type
, embedded_offset
,
669 original_value
, options
, 0, stream
);
673 const gdb_byte
*valaddr
= value_contents_for_printing (original_value
);
675 val
= unpack_long (type
, valaddr
+ embedded_offset
* unit_size
);
677 generic_val_print_enum_1 (type
, val
, stream
);
681 /* generic_val_print helper for TYPE_CODE_FLAGS. */
684 generic_val_print_flags (struct type
*type
,
685 int embedded_offset
, struct ui_file
*stream
,
686 struct value
*original_value
,
687 const struct value_print_options
*options
)
691 val_print_scalar_formatted (type
, embedded_offset
, original_value
,
695 const gdb_byte
*valaddr
= value_contents_for_printing (original_value
);
697 val_print_type_code_flags (type
, valaddr
+ embedded_offset
, stream
);
701 /* generic_val_print helper for TYPE_CODE_FUNC and TYPE_CODE_METHOD. */
704 generic_val_print_func (struct type
*type
,
705 int embedded_offset
, CORE_ADDR address
,
706 struct ui_file
*stream
,
707 struct value
*original_value
,
708 const struct value_print_options
*options
)
710 struct gdbarch
*gdbarch
= get_type_arch (type
);
714 val_print_scalar_formatted (type
, embedded_offset
,
715 original_value
, options
, 0, stream
);
719 /* FIXME, we should consider, at least for ANSI C language,
720 eliminating the distinction made between FUNCs and POINTERs
722 fprintf_filtered (stream
, "{");
723 type_print (type
, "", stream
, -1);
724 fprintf_filtered (stream
, "} ");
725 /* Try to print what function it points to, and its address. */
726 print_address_demangle (options
, gdbarch
, address
, stream
, demangle
);
730 /* generic_val_print helper for TYPE_CODE_BOOL. */
733 generic_val_print_bool (struct type
*type
,
734 int embedded_offset
, struct ui_file
*stream
,
735 struct value
*original_value
,
736 const struct value_print_options
*options
,
737 const struct generic_val_print_decorations
*decorations
)
740 struct gdbarch
*gdbarch
= get_type_arch (type
);
741 int unit_size
= gdbarch_addressable_memory_unit_size (gdbarch
);
743 if (options
->format
|| options
->output_format
)
745 struct value_print_options opts
= *options
;
746 opts
.format
= (options
->format
? options
->format
747 : options
->output_format
);
748 val_print_scalar_formatted (type
, embedded_offset
,
749 original_value
, &opts
, 0, stream
);
753 const gdb_byte
*valaddr
= value_contents_for_printing (original_value
);
755 val
= unpack_long (type
, valaddr
+ embedded_offset
* unit_size
);
757 fputs_filtered (decorations
->false_name
, stream
);
759 fputs_filtered (decorations
->true_name
, stream
);
761 print_longest (stream
, 'd', 0, val
);
765 /* generic_val_print helper for TYPE_CODE_INT. */
768 generic_val_print_int (struct type
*type
,
769 int embedded_offset
, struct ui_file
*stream
,
770 struct value
*original_value
,
771 const struct value_print_options
*options
)
773 struct value_print_options opts
= *options
;
775 opts
.format
= (options
->format
? options
->format
776 : options
->output_format
);
777 val_print_scalar_formatted (type
, embedded_offset
,
778 original_value
, &opts
, 0, stream
);
781 /* generic_val_print helper for TYPE_CODE_CHAR. */
784 generic_val_print_char (struct type
*type
, struct type
*unresolved_type
,
786 struct ui_file
*stream
,
787 struct value
*original_value
,
788 const struct value_print_options
*options
)
791 struct gdbarch
*gdbarch
= get_type_arch (type
);
792 int unit_size
= gdbarch_addressable_memory_unit_size (gdbarch
);
794 if (options
->format
|| options
->output_format
)
796 struct value_print_options opts
= *options
;
798 opts
.format
= (options
->format
? options
->format
799 : options
->output_format
);
800 val_print_scalar_formatted (type
, embedded_offset
,
801 original_value
, &opts
, 0, stream
);
805 const gdb_byte
*valaddr
= value_contents_for_printing (original_value
);
807 val
= unpack_long (type
, valaddr
+ embedded_offset
* unit_size
);
808 if (TYPE_UNSIGNED (type
))
809 fprintf_filtered (stream
, "%u", (unsigned int) val
);
811 fprintf_filtered (stream
, "%d", (int) val
);
812 fputs_filtered (" ", stream
);
813 LA_PRINT_CHAR (val
, unresolved_type
, stream
);
817 /* generic_val_print helper for TYPE_CODE_FLT and TYPE_CODE_DECFLOAT. */
820 generic_val_print_float (struct type
*type
,
821 int embedded_offset
, struct ui_file
*stream
,
822 struct value
*original_value
,
823 const struct value_print_options
*options
)
825 struct gdbarch
*gdbarch
= get_type_arch (type
);
826 int unit_size
= gdbarch_addressable_memory_unit_size (gdbarch
);
830 val_print_scalar_formatted (type
, embedded_offset
,
831 original_value
, options
, 0, stream
);
835 const gdb_byte
*valaddr
= value_contents_for_printing (original_value
);
837 print_floating (valaddr
+ embedded_offset
* unit_size
, type
, stream
);
841 /* generic_val_print helper for TYPE_CODE_COMPLEX. */
844 generic_val_print_complex (struct type
*type
,
845 int embedded_offset
, struct ui_file
*stream
,
846 struct value
*original_value
,
847 const struct value_print_options
*options
,
848 const struct generic_val_print_decorations
851 struct gdbarch
*gdbarch
= get_type_arch (type
);
852 int unit_size
= gdbarch_addressable_memory_unit_size (gdbarch
);
853 const gdb_byte
*valaddr
= value_contents_for_printing (original_value
);
855 fprintf_filtered (stream
, "%s", decorations
->complex_prefix
);
857 val_print_scalar_formatted (TYPE_TARGET_TYPE (type
),
858 embedded_offset
, original_value
, options
, 0,
861 print_floating (valaddr
+ embedded_offset
* unit_size
,
862 TYPE_TARGET_TYPE (type
), stream
);
863 fprintf_filtered (stream
, "%s", decorations
->complex_infix
);
865 val_print_scalar_formatted (TYPE_TARGET_TYPE (type
),
867 + type_length_units (TYPE_TARGET_TYPE (type
)),
868 original_value
, options
, 0, stream
);
870 print_floating (valaddr
+ embedded_offset
* unit_size
871 + TYPE_LENGTH (TYPE_TARGET_TYPE (type
)),
872 TYPE_TARGET_TYPE (type
), stream
);
873 fprintf_filtered (stream
, "%s", decorations
->complex_suffix
);
876 /* A generic val_print that is suitable for use by language
877 implementations of the la_val_print method. This function can
878 handle most type codes, though not all, notably exception
879 TYPE_CODE_UNION and TYPE_CODE_STRUCT, which must be implemented by
882 Most arguments are as to val_print.
884 The additional DECORATIONS argument can be used to customize the
885 output in some small, language-specific ways. */
888 generic_val_print (struct type
*type
,
889 int embedded_offset
, CORE_ADDR address
,
890 struct ui_file
*stream
, int recurse
,
891 struct value
*original_value
,
892 const struct value_print_options
*options
,
893 const struct generic_val_print_decorations
*decorations
)
895 struct type
*unresolved_type
= type
;
897 type
= check_typedef (type
);
898 switch (TYPE_CODE (type
))
900 case TYPE_CODE_ARRAY
:
901 generic_val_print_array (type
, embedded_offset
, address
, stream
,
902 recurse
, original_value
, options
, decorations
);
905 case TYPE_CODE_MEMBERPTR
:
906 generic_val_print_memberptr (type
, embedded_offset
, stream
,
907 original_value
, options
);
911 generic_val_print_ptr (type
, embedded_offset
, stream
,
912 original_value
, options
);
916 case TYPE_CODE_RVALUE_REF
:
917 generic_val_print_ref (type
, embedded_offset
, stream
, recurse
,
918 original_value
, options
);
922 generic_val_print_enum (type
, embedded_offset
, stream
,
923 original_value
, options
);
926 case TYPE_CODE_FLAGS
:
927 generic_val_print_flags (type
, embedded_offset
, stream
,
928 original_value
, options
);
932 case TYPE_CODE_METHOD
:
933 generic_val_print_func (type
, embedded_offset
, address
, stream
,
934 original_value
, options
);
938 generic_val_print_bool (type
, embedded_offset
, stream
,
939 original_value
, options
, decorations
);
942 case TYPE_CODE_RANGE
:
943 /* FIXME: create_static_range_type does not set the unsigned bit in a
944 range type (I think it probably should copy it from the
945 target type), so we won't print values which are too large to
946 fit in a signed integer correctly. */
947 /* FIXME: Doesn't handle ranges of enums correctly. (Can't just
948 print with the target type, though, because the size of our
949 type and the target type might differ). */
954 generic_val_print_int (type
, embedded_offset
, stream
,
955 original_value
, options
);
959 generic_val_print_char (type
, unresolved_type
, embedded_offset
,
960 stream
, original_value
, options
);
964 case TYPE_CODE_DECFLOAT
:
965 generic_val_print_float (type
, embedded_offset
, stream
,
966 original_value
, options
);
970 fputs_filtered (decorations
->void_name
, stream
);
973 case TYPE_CODE_ERROR
:
974 fprintf_filtered (stream
, "%s", TYPE_ERROR_NAME (type
));
977 case TYPE_CODE_UNDEF
:
978 /* This happens (without TYPE_STUB set) on systems which don't use
979 dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
980 and no complete type for struct foo in that file. */
981 fprintf_filtered (stream
, _("<incomplete type>"));
984 case TYPE_CODE_COMPLEX
:
985 generic_val_print_complex (type
, embedded_offset
, stream
,
986 original_value
, options
, decorations
);
989 case TYPE_CODE_UNION
:
990 case TYPE_CODE_STRUCT
:
991 case TYPE_CODE_METHODPTR
:
993 error (_("Unhandled type code %d in symbol table."),
999 /* Print using the given LANGUAGE the data of type TYPE located at
1000 VAL's contents buffer + EMBEDDED_OFFSET (within GDB), which came
1001 from the inferior at address ADDRESS + EMBEDDED_OFFSET, onto
1002 stdio stream STREAM according to OPTIONS. VAL is the whole object
1003 that came from ADDRESS.
1005 The language printers will pass down an adjusted EMBEDDED_OFFSET to
1006 further helper subroutines as subfields of TYPE are printed. In
1007 such cases, VAL is passed down unadjusted, so
1008 that VAL can be queried for metadata about the contents data being
1009 printed, using EMBEDDED_OFFSET as an offset into VAL's contents
1010 buffer. For example: "has this field been optimized out", or "I'm
1011 printing an object while inspecting a traceframe; has this
1012 particular piece of data been collected?".
1014 RECURSE indicates the amount of indentation to supply before
1015 continuation lines; this amount is roughly twice the value of
1019 val_print (struct type
*type
, LONGEST embedded_offset
,
1020 CORE_ADDR address
, struct ui_file
*stream
, int recurse
,
1022 const struct value_print_options
*options
,
1023 const struct language_defn
*language
)
1026 struct value_print_options local_opts
= *options
;
1027 struct type
*real_type
= check_typedef (type
);
1029 if (local_opts
.prettyformat
== Val_prettyformat_default
)
1030 local_opts
.prettyformat
= (local_opts
.prettyformat_structs
1031 ? Val_prettyformat
: Val_no_prettyformat
);
1035 /* Ensure that the type is complete and not just a stub. If the type is
1036 only a stub and we can't find and substitute its complete type, then
1037 print appropriate string and return. */
1039 if (TYPE_STUB (real_type
))
1041 fprintf_filtered (stream
, _("<incomplete type>"));
1046 if (!valprint_check_validity (stream
, real_type
, embedded_offset
, val
))
1051 ret
= apply_ext_lang_val_pretty_printer (type
, embedded_offset
,
1052 address
, stream
, recurse
,
1053 val
, options
, language
);
1058 /* Handle summary mode. If the value is a scalar, print it;
1059 otherwise, print an ellipsis. */
1060 if (options
->summary
&& !val_print_scalar_type_p (type
))
1062 fprintf_filtered (stream
, "...");
1068 language
->la_val_print (type
, embedded_offset
, address
,
1069 stream
, recurse
, val
,
1072 CATCH (except
, RETURN_MASK_ERROR
)
1074 fprintf_filtered (stream
, _("<error reading variable>"));
1079 /* Check whether the value VAL is printable. Return 1 if it is;
1080 return 0 and print an appropriate error message to STREAM according to
1081 OPTIONS if it is not. */
1084 value_check_printable (struct value
*val
, struct ui_file
*stream
,
1085 const struct value_print_options
*options
)
1089 fprintf_filtered (stream
, _("<address of value unknown>"));
1093 if (value_entirely_optimized_out (val
))
1095 if (options
->summary
&& !val_print_scalar_type_p (value_type (val
)))
1096 fprintf_filtered (stream
, "...");
1098 val_print_optimized_out (val
, stream
);
1102 if (value_entirely_unavailable (val
))
1104 if (options
->summary
&& !val_print_scalar_type_p (value_type (val
)))
1105 fprintf_filtered (stream
, "...");
1107 val_print_unavailable (stream
);
1111 if (TYPE_CODE (value_type (val
)) == TYPE_CODE_INTERNAL_FUNCTION
)
1113 fprintf_filtered (stream
, _("<internal function %s>"),
1114 value_internal_function_name (val
));
1118 if (type_not_associated (value_type (val
)))
1120 val_print_not_associated (stream
);
1124 if (type_not_allocated (value_type (val
)))
1126 val_print_not_allocated (stream
);
1133 /* Print using the given LANGUAGE the value VAL onto stream STREAM according
1136 This is a preferable interface to val_print, above, because it uses
1137 GDB's value mechanism. */
1140 common_val_print (struct value
*val
, struct ui_file
*stream
, int recurse
,
1141 const struct value_print_options
*options
,
1142 const struct language_defn
*language
)
1144 if (!value_check_printable (val
, stream
, options
))
1147 if (language
->la_language
== language_ada
)
1148 /* The value might have a dynamic type, which would cause trouble
1149 below when trying to extract the value contents (since the value
1150 size is determined from the type size which is unknown). So
1151 get a fixed representation of our value. */
1152 val
= ada_to_fixed_value (val
);
1154 if (value_lazy (val
))
1155 value_fetch_lazy (val
);
1157 val_print (value_type (val
),
1158 value_embedded_offset (val
), value_address (val
),
1160 val
, options
, language
);
1163 /* Print on stream STREAM the value VAL according to OPTIONS. The value
1164 is printed using the current_language syntax. */
1167 value_print (struct value
*val
, struct ui_file
*stream
,
1168 const struct value_print_options
*options
)
1170 if (!value_check_printable (val
, stream
, options
))
1176 = apply_ext_lang_val_pretty_printer (value_type (val
),
1177 value_embedded_offset (val
),
1178 value_address (val
),
1180 val
, options
, current_language
);
1186 LA_VALUE_PRINT (val
, stream
, options
);
1190 val_print_type_code_flags (struct type
*type
, const gdb_byte
*valaddr
,
1191 struct ui_file
*stream
)
1193 ULONGEST val
= unpack_long (type
, valaddr
);
1194 int field
, nfields
= TYPE_NFIELDS (type
);
1195 struct gdbarch
*gdbarch
= get_type_arch (type
);
1196 struct type
*bool_type
= builtin_type (gdbarch
)->builtin_bool
;
1198 fputs_filtered ("[", stream
);
1199 for (field
= 0; field
< nfields
; field
++)
1201 if (TYPE_FIELD_NAME (type
, field
)[0] != '\0')
1203 struct type
*field_type
= TYPE_FIELD_TYPE (type
, field
);
1205 if (field_type
== bool_type
1206 /* We require boolean types here to be one bit wide. This is a
1207 problematic place to notify the user of an internal error
1208 though. Instead just fall through and print the field as an
1210 && TYPE_FIELD_BITSIZE (type
, field
) == 1)
1212 if (val
& ((ULONGEST
)1 << TYPE_FIELD_BITPOS (type
, field
)))
1213 fprintf_filtered (stream
, " %s",
1214 TYPE_FIELD_NAME (type
, field
));
1218 unsigned field_len
= TYPE_FIELD_BITSIZE (type
, field
);
1220 = val
>> (TYPE_FIELD_BITPOS (type
, field
) - field_len
+ 1);
1222 if (field_len
< sizeof (ULONGEST
) * TARGET_CHAR_BIT
)
1223 field_val
&= ((ULONGEST
) 1 << field_len
) - 1;
1224 fprintf_filtered (stream
, " %s=",
1225 TYPE_FIELD_NAME (type
, field
));
1226 if (TYPE_CODE (field_type
) == TYPE_CODE_ENUM
)
1227 generic_val_print_enum_1 (field_type
, field_val
, stream
);
1229 print_longest (stream
, 'd', 0, field_val
);
1233 fputs_filtered (" ]", stream
);
1236 /* Print a scalar of data of type TYPE, pointed to in GDB by VALADDR,
1237 according to OPTIONS and SIZE on STREAM. Format i is not supported
1240 This is how the elements of an array or structure are printed
1244 val_print_scalar_formatted (struct type
*type
,
1245 LONGEST embedded_offset
,
1247 const struct value_print_options
*options
,
1249 struct ui_file
*stream
)
1251 struct gdbarch
*arch
= get_type_arch (type
);
1252 int unit_size
= gdbarch_addressable_memory_unit_size (arch
);
1254 gdb_assert (val
!= NULL
);
1256 /* If we get here with a string format, try again without it. Go
1257 all the way back to the language printers, which may call us
1259 if (options
->format
== 's')
1261 struct value_print_options opts
= *options
;
1264 val_print (type
, embedded_offset
, 0, stream
, 0, val
, &opts
,
1269 /* value_contents_for_printing fetches all VAL's contents. They are
1270 needed to check whether VAL is optimized-out or unavailable
1272 const gdb_byte
*valaddr
= value_contents_for_printing (val
);
1274 /* A scalar object that does not have all bits available can't be
1275 printed, because all bits contribute to its representation. */
1276 if (value_bits_any_optimized_out (val
,
1277 TARGET_CHAR_BIT
* embedded_offset
,
1278 TARGET_CHAR_BIT
* TYPE_LENGTH (type
)))
1279 val_print_optimized_out (val
, stream
);
1280 else if (!value_bytes_available (val
, embedded_offset
, TYPE_LENGTH (type
)))
1281 val_print_unavailable (stream
);
1283 print_scalar_formatted (valaddr
+ embedded_offset
* unit_size
, type
,
1284 options
, size
, stream
);
1287 /* Print a number according to FORMAT which is one of d,u,x,o,b,h,w,g.
1288 The raison d'etre of this function is to consolidate printing of
1289 LONG_LONG's into this one function. The format chars b,h,w,g are
1290 from print_scalar_formatted(). Numbers are printed using C
1293 USE_C_FORMAT means to use C format in all cases. Without it,
1294 'o' and 'x' format do not include the standard C radix prefix
1297 Hilfinger/2004-09-09: USE_C_FORMAT was originally called USE_LOCAL
1298 and was intended to request formating according to the current
1299 language and would be used for most integers that GDB prints. The
1300 exceptional cases were things like protocols where the format of
1301 the integer is a protocol thing, not a user-visible thing). The
1302 parameter remains to preserve the information of what things might
1303 be printed with language-specific format, should we ever resurrect
1307 print_longest (struct ui_file
*stream
, int format
, int use_c_format
,
1315 val
= int_string (val_long
, 10, 1, 0, 1); break;
1317 val
= int_string (val_long
, 10, 0, 0, 1); break;
1319 val
= int_string (val_long
, 16, 0, 0, use_c_format
); break;
1321 val
= int_string (val_long
, 16, 0, 2, 1); break;
1323 val
= int_string (val_long
, 16, 0, 4, 1); break;
1325 val
= int_string (val_long
, 16, 0, 8, 1); break;
1327 val
= int_string (val_long
, 16, 0, 16, 1); break;
1330 val
= int_string (val_long
, 8, 0, 0, use_c_format
); break;
1332 internal_error (__FILE__
, __LINE__
,
1333 _("failed internal consistency check"));
1335 fputs_filtered (val
, stream
);
1338 /* This used to be a macro, but I don't think it is called often enough
1339 to merit such treatment. */
1340 /* Convert a LONGEST to an int. This is used in contexts (e.g. number of
1341 arguments to a function, number in a value history, register number, etc.)
1342 where the value must not be larger than can fit in an int. */
1345 longest_to_int (LONGEST arg
)
1347 /* Let the compiler do the work. */
1348 int rtnval
= (int) arg
;
1350 /* Check for overflows or underflows. */
1351 if (sizeof (LONGEST
) > sizeof (int))
1355 error (_("Value out of range."));
1361 /* Print a floating point value of floating-point type TYPE,
1362 pointed to in GDB by VALADDR, on STREAM. */
1365 print_floating (const gdb_byte
*valaddr
, struct type
*type
,
1366 struct ui_file
*stream
)
1368 std::string str
= target_float_to_string (valaddr
, type
);
1369 fputs_filtered (str
.c_str (), stream
);
1373 print_binary_chars (struct ui_file
*stream
, const gdb_byte
*valaddr
,
1374 unsigned len
, enum bfd_endian byte_order
, bool zero_pad
)
1379 bool seen_a_one
= false;
1381 /* Declared "int" so it will be signed.
1382 This ensures that right shift will shift in zeros. */
1384 const int mask
= 0x080;
1386 if (byte_order
== BFD_ENDIAN_BIG
)
1392 /* Every byte has 8 binary characters; peel off
1393 and print from the MSB end. */
1395 for (i
= 0; i
< (HOST_CHAR_BIT
* sizeof (*p
)); i
++)
1397 if (*p
& (mask
>> i
))
1402 if (zero_pad
|| seen_a_one
|| b
== '1')
1403 fputc_filtered (b
, stream
);
1411 for (p
= valaddr
+ len
- 1;
1415 for (i
= 0; i
< (HOST_CHAR_BIT
* sizeof (*p
)); i
++)
1417 if (*p
& (mask
>> i
))
1422 if (zero_pad
|| seen_a_one
|| b
== '1')
1423 fputc_filtered (b
, stream
);
1430 /* When not zero-padding, ensure that something is printed when the
1432 if (!zero_pad
&& !seen_a_one
)
1433 fputc_filtered ('0', stream
);
1436 /* A helper for print_octal_chars that emits a single octal digit,
1437 optionally suppressing it if is zero and updating SEEN_A_ONE. */
1440 emit_octal_digit (struct ui_file
*stream
, bool *seen_a_one
, int digit
)
1442 if (*seen_a_one
|| digit
!= 0)
1443 fprintf_filtered (stream
, "%o", digit
);
1448 /* VALADDR points to an integer of LEN bytes.
1449 Print it in octal on stream or format it in buf. */
1452 print_octal_chars (struct ui_file
*stream
, const gdb_byte
*valaddr
,
1453 unsigned len
, enum bfd_endian byte_order
)
1456 unsigned char octa1
, octa2
, octa3
, carry
;
1459 /* Octal is 3 bits, which doesn't fit. Yuk. So we have to track
1460 * the extra bits, which cycle every three bytes:
1462 * Byte side: 0 1 2 3
1464 * bit number 123 456 78 | 9 012 345 6 | 78 901 234 | 567 890 12 |
1466 * Octal side: 0 1 carry 3 4 carry ...
1468 * Cycle number: 0 1 2
1470 * But of course we are printing from the high side, so we have to
1471 * figure out where in the cycle we are so that we end up with no
1472 * left over bits at the end.
1474 #define BITS_IN_OCTAL 3
1475 #define HIGH_ZERO 0340
1476 #define LOW_ZERO 0034
1477 #define CARRY_ZERO 0003
1478 static_assert (HIGH_ZERO
+ LOW_ZERO
+ CARRY_ZERO
== 0xff,
1479 "cycle zero constants are wrong");
1480 #define HIGH_ONE 0200
1481 #define MID_ONE 0160
1482 #define LOW_ONE 0016
1483 #define CARRY_ONE 0001
1484 static_assert (HIGH_ONE
+ MID_ONE
+ LOW_ONE
+ CARRY_ONE
== 0xff,
1485 "cycle one constants are wrong");
1486 #define HIGH_TWO 0300
1487 #define MID_TWO 0070
1488 #define LOW_TWO 0007
1489 static_assert (HIGH_TWO
+ MID_TWO
+ LOW_TWO
== 0xff,
1490 "cycle two constants are wrong");
1492 /* For 32 we start in cycle 2, with two bits and one bit carry;
1493 for 64 in cycle in cycle 1, with one bit and a two bit carry. */
1495 cycle
= (len
* HOST_CHAR_BIT
) % BITS_IN_OCTAL
;
1498 fputs_filtered ("0", stream
);
1499 bool seen_a_one
= false;
1500 if (byte_order
== BFD_ENDIAN_BIG
)
1509 /* No carry in, carry out two bits. */
1511 octa1
= (HIGH_ZERO
& *p
) >> 5;
1512 octa2
= (LOW_ZERO
& *p
) >> 2;
1513 carry
= (CARRY_ZERO
& *p
);
1514 emit_octal_digit (stream
, &seen_a_one
, octa1
);
1515 emit_octal_digit (stream
, &seen_a_one
, octa2
);
1519 /* Carry in two bits, carry out one bit. */
1521 octa1
= (carry
<< 1) | ((HIGH_ONE
& *p
) >> 7);
1522 octa2
= (MID_ONE
& *p
) >> 4;
1523 octa3
= (LOW_ONE
& *p
) >> 1;
1524 carry
= (CARRY_ONE
& *p
);
1525 emit_octal_digit (stream
, &seen_a_one
, octa1
);
1526 emit_octal_digit (stream
, &seen_a_one
, octa2
);
1527 emit_octal_digit (stream
, &seen_a_one
, octa3
);
1531 /* Carry in one bit, no carry out. */
1533 octa1
= (carry
<< 2) | ((HIGH_TWO
& *p
) >> 6);
1534 octa2
= (MID_TWO
& *p
) >> 3;
1535 octa3
= (LOW_TWO
& *p
);
1537 emit_octal_digit (stream
, &seen_a_one
, octa1
);
1538 emit_octal_digit (stream
, &seen_a_one
, octa2
);
1539 emit_octal_digit (stream
, &seen_a_one
, octa3
);
1543 error (_("Internal error in octal conversion;"));
1547 cycle
= cycle
% BITS_IN_OCTAL
;
1552 for (p
= valaddr
+ len
- 1;
1559 /* Carry out, no carry in */
1561 octa1
= (HIGH_ZERO
& *p
) >> 5;
1562 octa2
= (LOW_ZERO
& *p
) >> 2;
1563 carry
= (CARRY_ZERO
& *p
);
1564 emit_octal_digit (stream
, &seen_a_one
, octa1
);
1565 emit_octal_digit (stream
, &seen_a_one
, octa2
);
1569 /* Carry in, carry out */
1571 octa1
= (carry
<< 1) | ((HIGH_ONE
& *p
) >> 7);
1572 octa2
= (MID_ONE
& *p
) >> 4;
1573 octa3
= (LOW_ONE
& *p
) >> 1;
1574 carry
= (CARRY_ONE
& *p
);
1575 emit_octal_digit (stream
, &seen_a_one
, octa1
);
1576 emit_octal_digit (stream
, &seen_a_one
, octa2
);
1577 emit_octal_digit (stream
, &seen_a_one
, octa3
);
1581 /* Carry in, no carry out */
1583 octa1
= (carry
<< 2) | ((HIGH_TWO
& *p
) >> 6);
1584 octa2
= (MID_TWO
& *p
) >> 3;
1585 octa3
= (LOW_TWO
& *p
);
1587 emit_octal_digit (stream
, &seen_a_one
, octa1
);
1588 emit_octal_digit (stream
, &seen_a_one
, octa2
);
1589 emit_octal_digit (stream
, &seen_a_one
, octa3
);
1593 error (_("Internal error in octal conversion;"));
1597 cycle
= cycle
% BITS_IN_OCTAL
;
1603 /* Possibly negate the integer represented by BYTES. It contains LEN
1604 bytes in the specified byte order. If the integer is negative,
1605 copy it into OUT_VEC, negate it, and return true. Otherwise, do
1606 nothing and return false. */
1609 maybe_negate_by_bytes (const gdb_byte
*bytes
, unsigned len
,
1610 enum bfd_endian byte_order
,
1611 gdb::byte_vector
*out_vec
)
1614 if (byte_order
== BFD_ENDIAN_BIG
)
1615 sign_byte
= bytes
[0];
1617 sign_byte
= bytes
[len
- 1];
1618 if ((sign_byte
& 0x80) == 0)
1621 out_vec
->resize (len
);
1623 /* Compute -x == 1 + ~x. */
1624 if (byte_order
== BFD_ENDIAN_LITTLE
)
1627 for (unsigned i
= 0; i
< len
; ++i
)
1629 unsigned tem
= (0xff & ~bytes
[i
]) + carry
;
1630 (*out_vec
)[i
] = tem
& 0xff;
1637 for (unsigned i
= len
; i
> 0; --i
)
1639 unsigned tem
= (0xff & ~bytes
[i
- 1]) + carry
;
1640 (*out_vec
)[i
- 1] = tem
& 0xff;
1648 /* VALADDR points to an integer of LEN bytes.
1649 Print it in decimal on stream or format it in buf. */
1652 print_decimal_chars (struct ui_file
*stream
, const gdb_byte
*valaddr
,
1653 unsigned len
, bool is_signed
,
1654 enum bfd_endian byte_order
)
1657 #define CARRY_OUT( x ) ((x) / TEN) /* extend char to int */
1658 #define CARRY_LEFT( x ) ((x) % TEN)
1659 #define SHIFT( x ) ((x) << 4)
1660 #define LOW_NIBBLE( x ) ( (x) & 0x00F)
1661 #define HIGH_NIBBLE( x ) (((x) & 0x0F0) >> 4)
1666 int i
, j
, decimal_digits
;
1670 gdb::byte_vector negated_bytes
;
1672 && maybe_negate_by_bytes (valaddr
, len
, byte_order
, &negated_bytes
))
1674 fputs_filtered ("-", stream
);
1675 valaddr
= negated_bytes
.data ();
1678 /* Base-ten number is less than twice as many digits
1679 as the base 16 number, which is 2 digits per byte. */
1681 decimal_len
= len
* 2 * 2;
1682 std::vector
<unsigned char> digits (decimal_len
, 0);
1684 /* Ok, we have an unknown number of bytes of data to be printed in
1687 * Given a hex number (in nibbles) as XYZ, we start by taking X and
1688 * decemalizing it as "x1 x2" in two decimal nibbles. Then we multiply
1689 * the nibbles by 16, add Y and re-decimalize. Repeat with Z.
1691 * The trick is that "digits" holds a base-10 number, but sometimes
1692 * the individual digits are > 10.
1694 * Outer loop is per nibble (hex digit) of input, from MSD end to
1697 decimal_digits
= 0; /* Number of decimal digits so far */
1698 p
= (byte_order
== BFD_ENDIAN_BIG
) ? valaddr
: valaddr
+ len
- 1;
1700 while ((byte_order
== BFD_ENDIAN_BIG
) ? (p
< valaddr
+ len
) : (p
>= valaddr
))
1703 * Multiply current base-ten number by 16 in place.
1704 * Each digit was between 0 and 9, now is between
1707 for (j
= 0; j
< decimal_digits
; j
++)
1709 digits
[j
] = SHIFT (digits
[j
]);
1712 /* Take the next nibble off the input and add it to what
1713 * we've got in the LSB position. Bottom 'digit' is now
1714 * between 0 and 159.
1716 * "flip" is used to run this loop twice for each byte.
1720 /* Take top nibble. */
1722 digits
[0] += HIGH_NIBBLE (*p
);
1727 /* Take low nibble and bump our pointer "p". */
1729 digits
[0] += LOW_NIBBLE (*p
);
1730 if (byte_order
== BFD_ENDIAN_BIG
)
1737 /* Re-decimalize. We have to do this often enough
1738 * that we don't overflow, but once per nibble is
1739 * overkill. Easier this way, though. Note that the
1740 * carry is often larger than 10 (e.g. max initial
1741 * carry out of lowest nibble is 15, could bubble all
1742 * the way up greater than 10). So we have to do
1743 * the carrying beyond the last current digit.
1746 for (j
= 0; j
< decimal_len
- 1; j
++)
1750 /* "/" won't handle an unsigned char with
1751 * a value that if signed would be negative.
1752 * So extend to longword int via "dummy".
1755 carry
= CARRY_OUT (dummy
);
1756 digits
[j
] = CARRY_LEFT (dummy
);
1758 if (j
>= decimal_digits
&& carry
== 0)
1761 * All higher digits are 0 and we
1762 * no longer have a carry.
1764 * Note: "j" is 0-based, "decimal_digits" is
1767 decimal_digits
= j
+ 1;
1773 /* Ok, now "digits" is the decimal representation, with
1774 the "decimal_digits" actual digits. Print! */
1776 for (i
= decimal_digits
- 1; i
> 0 && digits
[i
] == 0; --i
)
1781 fprintf_filtered (stream
, "%1d", digits
[i
]);
1785 /* VALADDR points to an integer of LEN bytes. Print it in hex on stream. */
1788 print_hex_chars (struct ui_file
*stream
, const gdb_byte
*valaddr
,
1789 unsigned len
, enum bfd_endian byte_order
,
1794 fputs_filtered ("0x", stream
);
1795 if (byte_order
== BFD_ENDIAN_BIG
)
1801 /* Strip leading 0 bytes, but be sure to leave at least a
1802 single byte at the end. */
1803 for (; p
< valaddr
+ len
- 1 && !*p
; ++p
)
1807 const gdb_byte
*first
= p
;
1812 /* When not zero-padding, use a different format for the
1813 very first byte printed. */
1814 if (!zero_pad
&& p
== first
)
1815 fprintf_filtered (stream
, "%x", *p
);
1817 fprintf_filtered (stream
, "%02x", *p
);
1822 p
= valaddr
+ len
- 1;
1826 /* Strip leading 0 bytes, but be sure to leave at least a
1827 single byte at the end. */
1828 for (; p
>= valaddr
+ 1 && !*p
; --p
)
1832 const gdb_byte
*first
= p
;
1837 /* When not zero-padding, use a different format for the
1838 very first byte printed. */
1839 if (!zero_pad
&& p
== first
)
1840 fprintf_filtered (stream
, "%x", *p
);
1842 fprintf_filtered (stream
, "%02x", *p
);
1847 /* VALADDR points to a char integer of LEN bytes.
1848 Print it out in appropriate language form on stream.
1849 Omit any leading zero chars. */
1852 print_char_chars (struct ui_file
*stream
, struct type
*type
,
1853 const gdb_byte
*valaddr
,
1854 unsigned len
, enum bfd_endian byte_order
)
1858 if (byte_order
== BFD_ENDIAN_BIG
)
1861 while (p
< valaddr
+ len
- 1 && *p
== 0)
1864 while (p
< valaddr
+ len
)
1866 LA_EMIT_CHAR (*p
, type
, stream
, '\'');
1872 p
= valaddr
+ len
- 1;
1873 while (p
> valaddr
&& *p
== 0)
1876 while (p
>= valaddr
)
1878 LA_EMIT_CHAR (*p
, type
, stream
, '\'');
1884 /* Print function pointer with inferior address ADDRESS onto stdio
1888 print_function_pointer_address (const struct value_print_options
*options
,
1889 struct gdbarch
*gdbarch
,
1891 struct ui_file
*stream
)
1894 = gdbarch_convert_from_func_ptr_addr (gdbarch
, address
,
1897 /* If the function pointer is represented by a description, print
1898 the address of the description. */
1899 if (options
->addressprint
&& func_addr
!= address
)
1901 fputs_filtered ("@", stream
);
1902 fputs_filtered (paddress (gdbarch
, address
), stream
);
1903 fputs_filtered (": ", stream
);
1905 print_address_demangle (options
, gdbarch
, func_addr
, stream
, demangle
);
1909 /* Print on STREAM using the given OPTIONS the index for the element
1910 at INDEX of an array whose index type is INDEX_TYPE. */
1913 maybe_print_array_index (struct type
*index_type
, LONGEST index
,
1914 struct ui_file
*stream
,
1915 const struct value_print_options
*options
)
1917 struct value
*index_value
;
1919 if (!options
->print_array_indexes
)
1922 index_value
= value_from_longest (index_type
, index
);
1924 LA_PRINT_ARRAY_INDEX (index_value
, stream
, options
);
1927 /* Called by various <lang>_val_print routines to print elements of an
1928 array in the form "<elem1>, <elem2>, <elem3>, ...".
1930 (FIXME?) Assumes array element separator is a comma, which is correct
1931 for all languages currently handled.
1932 (FIXME?) Some languages have a notation for repeated array elements,
1933 perhaps we should try to use that notation when appropriate. */
1936 val_print_array_elements (struct type
*type
,
1937 LONGEST embedded_offset
,
1938 CORE_ADDR address
, struct ui_file
*stream
,
1941 const struct value_print_options
*options
,
1944 unsigned int things_printed
= 0;
1946 struct type
*elttype
, *index_type
, *base_index_type
;
1948 /* Position of the array element we are examining to see
1949 whether it is repeated. */
1951 /* Number of repetitions we have detected so far. */
1953 LONGEST low_bound
, high_bound
;
1954 LONGEST low_pos
, high_pos
;
1956 elttype
= TYPE_TARGET_TYPE (type
);
1957 eltlen
= type_length_units (check_typedef (elttype
));
1958 index_type
= TYPE_INDEX_TYPE (type
);
1960 if (get_array_bounds (type
, &low_bound
, &high_bound
))
1962 if (TYPE_CODE (index_type
) == TYPE_CODE_RANGE
)
1963 base_index_type
= TYPE_TARGET_TYPE (index_type
);
1965 base_index_type
= index_type
;
1967 /* Non-contiguous enumerations types can by used as index types
1968 in some languages (e.g. Ada). In this case, the array length
1969 shall be computed from the positions of the first and last
1970 literal in the enumeration type, and not from the values
1971 of these literals. */
1972 if (!discrete_position (base_index_type
, low_bound
, &low_pos
)
1973 || !discrete_position (base_index_type
, high_bound
, &high_pos
))
1975 warning (_("unable to get positions in array, use bounds instead"));
1976 low_pos
= low_bound
;
1977 high_pos
= high_bound
;
1980 /* The array length should normally be HIGH_POS - LOW_POS + 1.
1981 But we have to be a little extra careful, because some languages
1982 such as Ada allow LOW_POS to be greater than HIGH_POS for
1983 empty arrays. In that situation, the array length is just zero,
1985 if (low_pos
> high_pos
)
1988 len
= high_pos
- low_pos
+ 1;
1992 warning (_("unable to get bounds of array, assuming null array"));
1997 annotate_array_section_begin (i
, elttype
);
1999 for (; i
< len
&& things_printed
< options
->print_max
; i
++)
2003 if (options
->prettyformat_arrays
)
2005 fprintf_filtered (stream
, ",\n");
2006 print_spaces_filtered (2 + 2 * recurse
, stream
);
2010 fprintf_filtered (stream
, ", ");
2013 wrap_here (n_spaces (2 + 2 * recurse
));
2014 maybe_print_array_index (index_type
, i
+ low_bound
,
2019 /* Only check for reps if repeat_count_threshold is not set to
2020 UINT_MAX (unlimited). */
2021 if (options
->repeat_count_threshold
< UINT_MAX
)
2024 && value_contents_eq (val
,
2025 embedded_offset
+ i
* eltlen
,
2036 if (reps
> options
->repeat_count_threshold
)
2038 val_print (elttype
, embedded_offset
+ i
* eltlen
,
2039 address
, stream
, recurse
+ 1, val
, options
,
2041 annotate_elt_rep (reps
);
2042 fprintf_filtered (stream
, " <repeats %u times>", reps
);
2043 annotate_elt_rep_end ();
2046 things_printed
+= options
->repeat_count_threshold
;
2050 val_print (elttype
, embedded_offset
+ i
* eltlen
,
2052 stream
, recurse
+ 1, val
, options
, current_language
);
2057 annotate_array_section_end ();
2060 fprintf_filtered (stream
, "...");
2064 /* Read LEN bytes of target memory at address MEMADDR, placing the
2065 results in GDB's memory at MYADDR. Returns a count of the bytes
2066 actually read, and optionally a target_xfer_status value in the
2067 location pointed to by ERRPTR if ERRPTR is non-null. */
2069 /* FIXME: cagney/1999-10-14: Only used by val_print_string. Can this
2070 function be eliminated. */
2073 partial_memory_read (CORE_ADDR memaddr
, gdb_byte
*myaddr
,
2074 int len
, int *errptr
)
2076 int nread
; /* Number of bytes actually read. */
2077 int errcode
; /* Error from last read. */
2079 /* First try a complete read. */
2080 errcode
= target_read_memory (memaddr
, myaddr
, len
);
2088 /* Loop, reading one byte at a time until we get as much as we can. */
2089 for (errcode
= 0, nread
= 0; len
> 0 && errcode
== 0; nread
++, len
--)
2091 errcode
= target_read_memory (memaddr
++, myaddr
++, 1);
2093 /* If an error, the last read was unsuccessful, so adjust count. */
2106 /* Read a string from the inferior, at ADDR, with LEN characters of WIDTH bytes
2107 each. Fetch at most FETCHLIMIT characters. BUFFER will be set to a newly
2108 allocated buffer containing the string, which the caller is responsible to
2109 free, and BYTES_READ will be set to the number of bytes read. Returns 0 on
2110 success, or a target_xfer_status on failure.
2112 If LEN > 0, reads the lesser of LEN or FETCHLIMIT characters
2113 (including eventual NULs in the middle or end of the string).
2115 If LEN is -1, stops at the first null character (not necessarily
2116 the first null byte) up to a maximum of FETCHLIMIT characters. Set
2117 FETCHLIMIT to UINT_MAX to read as many characters as possible from
2120 Unless an exception is thrown, BUFFER will always be allocated, even on
2121 failure. In this case, some characters might have been read before the
2122 failure happened. Check BYTES_READ to recognize this situation.
2124 Note: There was a FIXME asking to make this code use target_read_string,
2125 but this function is more general (can read past null characters, up to
2126 given LEN). Besides, it is used much more often than target_read_string
2127 so it is more tested. Perhaps callers of target_read_string should use
2128 this function instead? */
2131 read_string (CORE_ADDR addr
, int len
, int width
, unsigned int fetchlimit
,
2132 enum bfd_endian byte_order
, gdb_byte
**buffer
, int *bytes_read
)
2134 int errcode
; /* Errno returned from bad reads. */
2135 unsigned int nfetch
; /* Chars to fetch / chars fetched. */
2136 gdb_byte
*bufptr
; /* Pointer to next available byte in
2138 struct cleanup
*old_chain
= NULL
; /* Top of the old cleanup chain. */
2140 /* Loop until we either have all the characters, or we encounter
2141 some error, such as bumping into the end of the address space. */
2145 old_chain
= make_cleanup (free_current_contents
, buffer
);
2149 /* We want fetchlimit chars, so we might as well read them all in
2151 unsigned int fetchlen
= std::min ((unsigned) len
, fetchlimit
);
2153 *buffer
= (gdb_byte
*) xmalloc (fetchlen
* width
);
2156 nfetch
= partial_memory_read (addr
, bufptr
, fetchlen
* width
, &errcode
)
2158 addr
+= nfetch
* width
;
2159 bufptr
+= nfetch
* width
;
2163 unsigned long bufsize
= 0;
2164 unsigned int chunksize
; /* Size of each fetch, in chars. */
2165 int found_nul
; /* Non-zero if we found the nul char. */
2166 gdb_byte
*limit
; /* First location past end of fetch buffer. */
2169 /* We are looking for a NUL terminator to end the fetching, so we
2170 might as well read in blocks that are large enough to be efficient,
2171 but not so large as to be slow if fetchlimit happens to be large.
2172 So we choose the minimum of 8 and fetchlimit. We used to use 200
2173 instead of 8 but 200 is way too big for remote debugging over a
2175 chunksize
= std::min (8u, fetchlimit
);
2180 nfetch
= std::min ((unsigned long) chunksize
, fetchlimit
- bufsize
);
2182 if (*buffer
== NULL
)
2183 *buffer
= (gdb_byte
*) xmalloc (nfetch
* width
);
2185 *buffer
= (gdb_byte
*) xrealloc (*buffer
,
2186 (nfetch
+ bufsize
) * width
);
2188 bufptr
= *buffer
+ bufsize
* width
;
2191 /* Read as much as we can. */
2192 nfetch
= partial_memory_read (addr
, bufptr
, nfetch
* width
, &errcode
)
2195 /* Scan this chunk for the null character that terminates the string
2196 to print. If found, we don't need to fetch any more. Note
2197 that bufptr is explicitly left pointing at the next character
2198 after the null character, or at the next character after the end
2201 limit
= bufptr
+ nfetch
* width
;
2202 while (bufptr
< limit
)
2206 c
= extract_unsigned_integer (bufptr
, width
, byte_order
);
2211 /* We don't care about any error which happened after
2212 the NUL terminator. */
2219 while (errcode
== 0 /* no error */
2220 && bufptr
- *buffer
< fetchlimit
* width
/* no overrun */
2221 && !found_nul
); /* haven't found NUL yet */
2224 { /* Length of string is really 0! */
2225 /* We always allocate *buffer. */
2226 *buffer
= bufptr
= (gdb_byte
*) xmalloc (1);
2230 /* bufptr and addr now point immediately beyond the last byte which we
2231 consider part of the string (including a '\0' which ends the string). */
2232 *bytes_read
= bufptr
- *buffer
;
2236 discard_cleanups (old_chain
);
2241 /* Return true if print_wchar can display W without resorting to a
2242 numeric escape, false otherwise. */
2245 wchar_printable (gdb_wchar_t w
)
2247 return (gdb_iswprint (w
)
2248 || w
== LCST ('\a') || w
== LCST ('\b')
2249 || w
== LCST ('\f') || w
== LCST ('\n')
2250 || w
== LCST ('\r') || w
== LCST ('\t')
2251 || w
== LCST ('\v'));
2254 /* A helper function that converts the contents of STRING to wide
2255 characters and then appends them to OUTPUT. */
2258 append_string_as_wide (const char *string
,
2259 struct obstack
*output
)
2261 for (; *string
; ++string
)
2263 gdb_wchar_t w
= gdb_btowc (*string
);
2264 obstack_grow (output
, &w
, sizeof (gdb_wchar_t
));
2268 /* Print a wide character W to OUTPUT. ORIG is a pointer to the
2269 original (target) bytes representing the character, ORIG_LEN is the
2270 number of valid bytes. WIDTH is the number of bytes in a base
2271 characters of the type. OUTPUT is an obstack to which wide
2272 characters are emitted. QUOTER is a (narrow) character indicating
2273 the style of quotes surrounding the character to be printed.
2274 NEED_ESCAPE is an in/out flag which is used to track numeric
2275 escapes across calls. */
2278 print_wchar (gdb_wint_t w
, const gdb_byte
*orig
,
2279 int orig_len
, int width
,
2280 enum bfd_endian byte_order
,
2281 struct obstack
*output
,
2282 int quoter
, int *need_escapep
)
2284 int need_escape
= *need_escapep
;
2288 /* iswprint implementation on Windows returns 1 for tab character.
2289 In order to avoid different printout on this host, we explicitly
2290 use wchar_printable function. */
2294 obstack_grow_wstr (output
, LCST ("\\a"));
2297 obstack_grow_wstr (output
, LCST ("\\b"));
2300 obstack_grow_wstr (output
, LCST ("\\f"));
2303 obstack_grow_wstr (output
, LCST ("\\n"));
2306 obstack_grow_wstr (output
, LCST ("\\r"));
2309 obstack_grow_wstr (output
, LCST ("\\t"));
2312 obstack_grow_wstr (output
, LCST ("\\v"));
2316 if (wchar_printable (w
) && (!need_escape
|| (!gdb_iswdigit (w
)
2318 && w
!= LCST ('9'))))
2320 gdb_wchar_t wchar
= w
;
2322 if (w
== gdb_btowc (quoter
) || w
== LCST ('\\'))
2323 obstack_grow_wstr (output
, LCST ("\\"));
2324 obstack_grow (output
, &wchar
, sizeof (gdb_wchar_t
));
2330 for (i
= 0; i
+ width
<= orig_len
; i
+= width
)
2335 value
= extract_unsigned_integer (&orig
[i
], width
,
2337 /* If the value fits in 3 octal digits, print it that
2338 way. Otherwise, print it as a hex escape. */
2340 xsnprintf (octal
, sizeof (octal
), "\\%.3o",
2341 (int) (value
& 0777));
2343 xsnprintf (octal
, sizeof (octal
), "\\x%lx", (long) value
);
2344 append_string_as_wide (octal
, output
);
2346 /* If we somehow have extra bytes, print them now. */
2347 while (i
< orig_len
)
2351 xsnprintf (octal
, sizeof (octal
), "\\%.3o", orig
[i
] & 0xff);
2352 append_string_as_wide (octal
, output
);
2363 /* Print the character C on STREAM as part of the contents of a
2364 literal string whose delimiter is QUOTER. ENCODING names the
2368 generic_emit_char (int c
, struct type
*type
, struct ui_file
*stream
,
2369 int quoter
, const char *encoding
)
2371 enum bfd_endian byte_order
2372 = gdbarch_byte_order (get_type_arch (type
));
2374 int need_escape
= 0;
2376 buf
= (gdb_byte
*) alloca (TYPE_LENGTH (type
));
2377 pack_long (buf
, type
, c
);
2379 wchar_iterator
iter (buf
, TYPE_LENGTH (type
), encoding
, TYPE_LENGTH (type
));
2381 /* This holds the printable form of the wchar_t data. */
2382 auto_obstack wchar_buf
;
2388 const gdb_byte
*buf
;
2390 int print_escape
= 1;
2391 enum wchar_iterate_result result
;
2393 num_chars
= iter
.iterate (&result
, &chars
, &buf
, &buflen
);
2398 /* If all characters are printable, print them. Otherwise,
2399 we're going to have to print an escape sequence. We
2400 check all characters because we want to print the target
2401 bytes in the escape sequence, and we don't know character
2402 boundaries there. */
2406 for (i
= 0; i
< num_chars
; ++i
)
2407 if (!wchar_printable (chars
[i
]))
2415 for (i
= 0; i
< num_chars
; ++i
)
2416 print_wchar (chars
[i
], buf
, buflen
,
2417 TYPE_LENGTH (type
), byte_order
,
2418 &wchar_buf
, quoter
, &need_escape
);
2422 /* This handles the NUM_CHARS == 0 case as well. */
2424 print_wchar (gdb_WEOF
, buf
, buflen
, TYPE_LENGTH (type
),
2425 byte_order
, &wchar_buf
, quoter
, &need_escape
);
2428 /* The output in the host encoding. */
2429 auto_obstack output
;
2431 convert_between_encodings (INTERMEDIATE_ENCODING
, host_charset (),
2432 (gdb_byte
*) obstack_base (&wchar_buf
),
2433 obstack_object_size (&wchar_buf
),
2434 sizeof (gdb_wchar_t
), &output
, translit_char
);
2435 obstack_1grow (&output
, '\0');
2437 fputs_filtered ((const char *) obstack_base (&output
), stream
);
2440 /* Return the repeat count of the next character/byte in ITER,
2441 storing the result in VEC. */
2444 count_next_character (wchar_iterator
*iter
,
2445 VEC (converted_character_d
) **vec
)
2447 struct converted_character
*current
;
2449 if (VEC_empty (converted_character_d
, *vec
))
2451 struct converted_character tmp
;
2455 = iter
->iterate (&tmp
.result
, &chars
, &tmp
.buf
, &tmp
.buflen
);
2456 if (tmp
.num_chars
> 0)
2458 gdb_assert (tmp
.num_chars
< MAX_WCHARS
);
2459 memcpy (tmp
.chars
, chars
, tmp
.num_chars
* sizeof (gdb_wchar_t
));
2461 VEC_safe_push (converted_character_d
, *vec
, &tmp
);
2464 current
= VEC_last (converted_character_d
, *vec
);
2466 /* Count repeated characters or bytes. */
2467 current
->repeat_count
= 1;
2468 if (current
->num_chars
== -1)
2476 struct converted_character d
;
2483 /* Get the next character. */
2484 d
.num_chars
= iter
->iterate (&d
.result
, &chars
, &d
.buf
, &d
.buflen
);
2486 /* If a character was successfully converted, save the character
2487 into the converted character. */
2488 if (d
.num_chars
> 0)
2490 gdb_assert (d
.num_chars
< MAX_WCHARS
);
2491 memcpy (d
.chars
, chars
, WCHAR_BUFLEN (d
.num_chars
));
2494 /* Determine if the current character is the same as this
2496 if (d
.num_chars
== current
->num_chars
&& d
.result
== current
->result
)
2498 /* There are two cases to consider:
2500 1) Equality of converted character (num_chars > 0)
2501 2) Equality of non-converted character (num_chars == 0) */
2502 if ((current
->num_chars
> 0
2503 && memcmp (current
->chars
, d
.chars
,
2504 WCHAR_BUFLEN (current
->num_chars
)) == 0)
2505 || (current
->num_chars
== 0
2506 && current
->buflen
== d
.buflen
2507 && memcmp (current
->buf
, d
.buf
, current
->buflen
) == 0))
2508 ++current
->repeat_count
;
2516 /* Push this next converted character onto the result vector. */
2517 repeat
= current
->repeat_count
;
2518 VEC_safe_push (converted_character_d
, *vec
, &d
);
2523 /* Print the characters in CHARS to the OBSTACK. QUOTE_CHAR is the quote
2524 character to use with string output. WIDTH is the size of the output
2525 character type. BYTE_ORDER is the the target byte order. OPTIONS
2526 is the user's print options. */
2529 print_converted_chars_to_obstack (struct obstack
*obstack
,
2530 VEC (converted_character_d
) *chars
,
2531 int quote_char
, int width
,
2532 enum bfd_endian byte_order
,
2533 const struct value_print_options
*options
)
2536 struct converted_character
*elem
;
2537 enum {START
, SINGLE
, REPEAT
, INCOMPLETE
, FINISH
} state
, last
;
2538 gdb_wchar_t wide_quote_char
= gdb_btowc (quote_char
);
2539 int need_escape
= 0;
2541 /* Set the start state. */
2543 last
= state
= START
;
2551 /* Nothing to do. */
2558 /* We are outputting a single character
2559 (< options->repeat_count_threshold). */
2563 /* We were outputting some other type of content, so we
2564 must output and a comma and a quote. */
2566 obstack_grow_wstr (obstack
, LCST (", "));
2567 obstack_grow (obstack
, &wide_quote_char
, sizeof (gdb_wchar_t
));
2569 /* Output the character. */
2570 for (j
= 0; j
< elem
->repeat_count
; ++j
)
2572 if (elem
->result
== wchar_iterate_ok
)
2573 print_wchar (elem
->chars
[0], elem
->buf
, elem
->buflen
, width
,
2574 byte_order
, obstack
, quote_char
, &need_escape
);
2576 print_wchar (gdb_WEOF
, elem
->buf
, elem
->buflen
, width
,
2577 byte_order
, obstack
, quote_char
, &need_escape
);
2587 /* We are outputting a character with a repeat count
2588 greater than options->repeat_count_threshold. */
2592 /* We were outputting a single string. Terminate the
2594 obstack_grow (obstack
, &wide_quote_char
, sizeof (gdb_wchar_t
));
2597 obstack_grow_wstr (obstack
, LCST (", "));
2599 /* Output the character and repeat string. */
2600 obstack_grow_wstr (obstack
, LCST ("'"));
2601 if (elem
->result
== wchar_iterate_ok
)
2602 print_wchar (elem
->chars
[0], elem
->buf
, elem
->buflen
, width
,
2603 byte_order
, obstack
, quote_char
, &need_escape
);
2605 print_wchar (gdb_WEOF
, elem
->buf
, elem
->buflen
, width
,
2606 byte_order
, obstack
, quote_char
, &need_escape
);
2607 obstack_grow_wstr (obstack
, LCST ("'"));
2608 s
= xstrprintf (_(" <repeats %u times>"), elem
->repeat_count
);
2609 for (j
= 0; s
[j
]; ++j
)
2611 gdb_wchar_t w
= gdb_btowc (s
[j
]);
2612 obstack_grow (obstack
, &w
, sizeof (gdb_wchar_t
));
2619 /* We are outputting an incomplete sequence. */
2622 /* If we were outputting a string of SINGLE characters,
2623 terminate the quote. */
2624 obstack_grow (obstack
, &wide_quote_char
, sizeof (gdb_wchar_t
));
2627 obstack_grow_wstr (obstack
, LCST (", "));
2629 /* Output the incomplete sequence string. */
2630 obstack_grow_wstr (obstack
, LCST ("<incomplete sequence "));
2631 print_wchar (gdb_WEOF
, elem
->buf
, elem
->buflen
, width
, byte_order
,
2632 obstack
, 0, &need_escape
);
2633 obstack_grow_wstr (obstack
, LCST (">"));
2635 /* We do not attempt to outupt anything after this. */
2640 /* All done. If we were outputting a string of SINGLE
2641 characters, the string must be terminated. Otherwise,
2642 REPEAT and INCOMPLETE are always left properly terminated. */
2644 obstack_grow (obstack
, &wide_quote_char
, sizeof (gdb_wchar_t
));
2649 /* Get the next element and state. */
2651 if (state
!= FINISH
)
2653 elem
= VEC_index (converted_character_d
, chars
, idx
++);
2654 switch (elem
->result
)
2656 case wchar_iterate_ok
:
2657 case wchar_iterate_invalid
:
2658 if (elem
->repeat_count
> options
->repeat_count_threshold
)
2664 case wchar_iterate_incomplete
:
2668 case wchar_iterate_eof
:
2676 /* Print the character string STRING, printing at most LENGTH
2677 characters. LENGTH is -1 if the string is nul terminated. TYPE is
2678 the type of each character. OPTIONS holds the printing options;
2679 printing stops early if the number hits print_max; repeat counts
2680 are printed as appropriate. Print ellipses at the end if we had to
2681 stop before printing LENGTH characters, or if FORCE_ELLIPSES.
2682 QUOTE_CHAR is the character to print at each end of the string. If
2683 C_STYLE_TERMINATOR is true, and the last character is 0, then it is
2687 generic_printstr (struct ui_file
*stream
, struct type
*type
,
2688 const gdb_byte
*string
, unsigned int length
,
2689 const char *encoding
, int force_ellipses
,
2690 int quote_char
, int c_style_terminator
,
2691 const struct value_print_options
*options
)
2693 enum bfd_endian byte_order
= gdbarch_byte_order (get_type_arch (type
));
2695 int width
= TYPE_LENGTH (type
);
2696 struct cleanup
*cleanup
;
2698 struct converted_character
*last
;
2699 VEC (converted_character_d
) *converted_chars
;
2703 unsigned long current_char
= 1;
2705 for (i
= 0; current_char
; ++i
)
2708 current_char
= extract_unsigned_integer (string
+ i
* width
,
2714 /* If the string was not truncated due to `set print elements', and
2715 the last byte of it is a null, we don't print that, in
2716 traditional C style. */
2717 if (c_style_terminator
2720 && (extract_unsigned_integer (string
+ (length
- 1) * width
,
2721 width
, byte_order
) == 0))
2726 fputs_filtered ("\"\"", stream
);
2730 /* Arrange to iterate over the characters, in wchar_t form. */
2731 wchar_iterator
iter (string
, length
* width
, encoding
, width
);
2732 converted_chars
= NULL
;
2733 cleanup
= make_cleanup (VEC_cleanup (converted_character_d
),
2736 /* Convert characters until the string is over or the maximum
2737 number of printed characters has been reached. */
2739 while (i
< options
->print_max
)
2745 /* Grab the next character and repeat count. */
2746 r
= count_next_character (&iter
, &converted_chars
);
2748 /* If less than zero, the end of the input string was reached. */
2752 /* Otherwise, add the count to the total print count and get
2753 the next character. */
2757 /* Get the last element and determine if the entire string was
2759 last
= VEC_last (converted_character_d
, converted_chars
);
2760 finished
= (last
->result
== wchar_iterate_eof
);
2762 /* Ensure that CONVERTED_CHARS is terminated. */
2763 last
->result
= wchar_iterate_eof
;
2765 /* WCHAR_BUF is the obstack we use to represent the string in
2767 auto_obstack wchar_buf
;
2769 /* Print the output string to the obstack. */
2770 print_converted_chars_to_obstack (&wchar_buf
, converted_chars
, quote_char
,
2771 width
, byte_order
, options
);
2773 if (force_ellipses
|| !finished
)
2774 obstack_grow_wstr (&wchar_buf
, LCST ("..."));
2776 /* OUTPUT is where we collect `char's for printing. */
2777 auto_obstack output
;
2779 convert_between_encodings (INTERMEDIATE_ENCODING
, host_charset (),
2780 (gdb_byte
*) obstack_base (&wchar_buf
),
2781 obstack_object_size (&wchar_buf
),
2782 sizeof (gdb_wchar_t
), &output
, translit_char
);
2783 obstack_1grow (&output
, '\0');
2785 fputs_filtered ((const char *) obstack_base (&output
), stream
);
2787 do_cleanups (cleanup
);
2790 /* Print a string from the inferior, starting at ADDR and printing up to LEN
2791 characters, of WIDTH bytes a piece, to STREAM. If LEN is -1, printing
2792 stops at the first null byte, otherwise printing proceeds (including null
2793 bytes) until either print_max or LEN characters have been printed,
2794 whichever is smaller. ENCODING is the name of the string's
2795 encoding. It can be NULL, in which case the target encoding is
2799 val_print_string (struct type
*elttype
, const char *encoding
,
2800 CORE_ADDR addr
, int len
,
2801 struct ui_file
*stream
,
2802 const struct value_print_options
*options
)
2804 int force_ellipsis
= 0; /* Force ellipsis to be printed if nonzero. */
2805 int err
; /* Non-zero if we got a bad read. */
2806 int found_nul
; /* Non-zero if we found the nul char. */
2807 unsigned int fetchlimit
; /* Maximum number of chars to print. */
2809 gdb_byte
*buffer
= NULL
; /* Dynamically growable fetch buffer. */
2810 struct cleanup
*old_chain
= NULL
; /* Top of the old cleanup chain. */
2811 struct gdbarch
*gdbarch
= get_type_arch (elttype
);
2812 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
2813 int width
= TYPE_LENGTH (elttype
);
2815 /* First we need to figure out the limit on the number of characters we are
2816 going to attempt to fetch and print. This is actually pretty simple. If
2817 LEN >= zero, then the limit is the minimum of LEN and print_max. If
2818 LEN is -1, then the limit is print_max. This is true regardless of
2819 whether print_max is zero, UINT_MAX (unlimited), or something in between,
2820 because finding the null byte (or available memory) is what actually
2821 limits the fetch. */
2823 fetchlimit
= (len
== -1 ? options
->print_max
: std::min ((unsigned) len
,
2824 options
->print_max
));
2826 err
= read_string (addr
, len
, width
, fetchlimit
, byte_order
,
2827 &buffer
, &bytes_read
);
2828 old_chain
= make_cleanup (xfree
, buffer
);
2832 /* We now have either successfully filled the buffer to fetchlimit,
2833 or terminated early due to an error or finding a null char when
2836 /* Determine found_nul by looking at the last character read. */
2838 if (bytes_read
>= width
)
2839 found_nul
= extract_unsigned_integer (buffer
+ bytes_read
- width
, width
,
2841 if (len
== -1 && !found_nul
)
2845 /* We didn't find a NUL terminator we were looking for. Attempt
2846 to peek at the next character. If not successful, or it is not
2847 a null byte, then force ellipsis to be printed. */
2849 peekbuf
= (gdb_byte
*) alloca (width
);
2851 if (target_read_memory (addr
, peekbuf
, width
) == 0
2852 && extract_unsigned_integer (peekbuf
, width
, byte_order
) != 0)
2855 else if ((len
>= 0 && err
!= 0) || (len
> bytes_read
/ width
))
2857 /* Getting an error when we have a requested length, or fetching less
2858 than the number of characters actually requested, always make us
2863 /* If we get an error before fetching anything, don't print a string.
2864 But if we fetch something and then get an error, print the string
2865 and then the error message. */
2866 if (err
== 0 || bytes_read
> 0)
2868 LA_PRINT_STRING (stream
, elttype
, buffer
, bytes_read
/ width
,
2869 encoding
, force_ellipsis
, options
);
2874 std::string str
= memory_error_message (TARGET_XFER_E_IO
, gdbarch
, addr
);
2876 fprintf_filtered (stream
, "<error: ");
2877 fputs_filtered (str
.c_str (), stream
);
2878 fprintf_filtered (stream
, ">");
2882 do_cleanups (old_chain
);
2884 return (bytes_read
/ width
);
2888 /* The 'set input-radix' command writes to this auxiliary variable.
2889 If the requested radix is valid, INPUT_RADIX is updated; otherwise,
2890 it is left unchanged. */
2892 static unsigned input_radix_1
= 10;
2894 /* Validate an input or output radix setting, and make sure the user
2895 knows what they really did here. Radix setting is confusing, e.g.
2896 setting the input radix to "10" never changes it! */
2899 set_input_radix (char *args
, int from_tty
, struct cmd_list_element
*c
)
2901 set_input_radix_1 (from_tty
, input_radix_1
);
2905 set_input_radix_1 (int from_tty
, unsigned radix
)
2907 /* We don't currently disallow any input radix except 0 or 1, which don't
2908 make any mathematical sense. In theory, we can deal with any input
2909 radix greater than 1, even if we don't have unique digits for every
2910 value from 0 to radix-1, but in practice we lose on large radix values.
2911 We should either fix the lossage or restrict the radix range more.
2916 input_radix_1
= input_radix
;
2917 error (_("Nonsense input radix ``decimal %u''; input radix unchanged."),
2920 input_radix_1
= input_radix
= radix
;
2923 printf_filtered (_("Input radix now set to "
2924 "decimal %u, hex %x, octal %o.\n"),
2925 radix
, radix
, radix
);
2929 /* The 'set output-radix' command writes to this auxiliary variable.
2930 If the requested radix is valid, OUTPUT_RADIX is updated,
2931 otherwise, it is left unchanged. */
2933 static unsigned output_radix_1
= 10;
2936 set_output_radix (char *args
, int from_tty
, struct cmd_list_element
*c
)
2938 set_output_radix_1 (from_tty
, output_radix_1
);
2942 set_output_radix_1 (int from_tty
, unsigned radix
)
2944 /* Validate the radix and disallow ones that we aren't prepared to
2945 handle correctly, leaving the radix unchanged. */
2949 user_print_options
.output_format
= 'x'; /* hex */
2952 user_print_options
.output_format
= 0; /* decimal */
2955 user_print_options
.output_format
= 'o'; /* octal */
2958 output_radix_1
= output_radix
;
2959 error (_("Unsupported output radix ``decimal %u''; "
2960 "output radix unchanged."),
2963 output_radix_1
= output_radix
= radix
;
2966 printf_filtered (_("Output radix now set to "
2967 "decimal %u, hex %x, octal %o.\n"),
2968 radix
, radix
, radix
);
2972 /* Set both the input and output radix at once. Try to set the output radix
2973 first, since it has the most restrictive range. An radix that is valid as
2974 an output radix is also valid as an input radix.
2976 It may be useful to have an unusual input radix. If the user wishes to
2977 set an input radix that is not valid as an output radix, he needs to use
2978 the 'set input-radix' command. */
2981 set_radix (const char *arg
, int from_tty
)
2985 radix
= (arg
== NULL
) ? 10 : parse_and_eval_long (arg
);
2986 set_output_radix_1 (0, radix
);
2987 set_input_radix_1 (0, radix
);
2990 printf_filtered (_("Input and output radices now set to "
2991 "decimal %u, hex %x, octal %o.\n"),
2992 radix
, radix
, radix
);
2996 /* Show both the input and output radices. */
2999 show_radix (const char *arg
, int from_tty
)
3003 if (input_radix
== output_radix
)
3005 printf_filtered (_("Input and output radices set to "
3006 "decimal %u, hex %x, octal %o.\n"),
3007 input_radix
, input_radix
, input_radix
);
3011 printf_filtered (_("Input radix set to decimal "
3012 "%u, hex %x, octal %o.\n"),
3013 input_radix
, input_radix
, input_radix
);
3014 printf_filtered (_("Output radix set to decimal "
3015 "%u, hex %x, octal %o.\n"),
3016 output_radix
, output_radix
, output_radix
);
3023 set_print (const char *arg
, int from_tty
)
3026 "\"set print\" must be followed by the name of a print subcommand.\n");
3027 help_list (setprintlist
, "set print ", all_commands
, gdb_stdout
);
3031 show_print (const char *args
, int from_tty
)
3033 cmd_show_list (showprintlist
, from_tty
, "");
3037 set_print_raw (const char *arg
, int from_tty
)
3040 "\"set print raw\" must be followed by the name of a \"print raw\" subcommand.\n");
3041 help_list (setprintrawlist
, "set print raw ", all_commands
, gdb_stdout
);
3045 show_print_raw (const char *args
, int from_tty
)
3047 cmd_show_list (showprintrawlist
, from_tty
, "");
3052 _initialize_valprint (void)
3054 add_prefix_cmd ("print", no_class
, set_print
,
3055 _("Generic command for setting how things print."),
3056 &setprintlist
, "set print ", 0, &setlist
);
3057 add_alias_cmd ("p", "print", no_class
, 1, &setlist
);
3058 /* Prefer set print to set prompt. */
3059 add_alias_cmd ("pr", "print", no_class
, 1, &setlist
);
3061 add_prefix_cmd ("print", no_class
, show_print
,
3062 _("Generic command for showing print settings."),
3063 &showprintlist
, "show print ", 0, &showlist
);
3064 add_alias_cmd ("p", "print", no_class
, 1, &showlist
);
3065 add_alias_cmd ("pr", "print", no_class
, 1, &showlist
);
3067 add_prefix_cmd ("raw", no_class
, set_print_raw
,
3069 Generic command for setting what things to print in \"raw\" mode."),
3070 &setprintrawlist
, "set print raw ", 0, &setprintlist
);
3071 add_prefix_cmd ("raw", no_class
, show_print_raw
,
3072 _("Generic command for showing \"print raw\" settings."),
3073 &showprintrawlist
, "show print raw ", 0, &showprintlist
);
3075 add_setshow_uinteger_cmd ("elements", no_class
,
3076 &user_print_options
.print_max
, _("\
3077 Set limit on string chars or array elements to print."), _("\
3078 Show limit on string chars or array elements to print."), _("\
3079 \"set print elements unlimited\" causes there to be no limit."),
3082 &setprintlist
, &showprintlist
);
3084 add_setshow_boolean_cmd ("null-stop", no_class
,
3085 &user_print_options
.stop_print_at_null
, _("\
3086 Set printing of char arrays to stop at first null char."), _("\
3087 Show printing of char arrays to stop at first null char."), NULL
,
3089 show_stop_print_at_null
,
3090 &setprintlist
, &showprintlist
);
3092 add_setshow_uinteger_cmd ("repeats", no_class
,
3093 &user_print_options
.repeat_count_threshold
, _("\
3094 Set threshold for repeated print elements."), _("\
3095 Show threshold for repeated print elements."), _("\
3096 \"set print repeats unlimited\" causes all elements to be individually printed."),
3098 show_repeat_count_threshold
,
3099 &setprintlist
, &showprintlist
);
3101 add_setshow_boolean_cmd ("pretty", class_support
,
3102 &user_print_options
.prettyformat_structs
, _("\
3103 Set pretty formatting of structures."), _("\
3104 Show pretty formatting of structures."), NULL
,
3106 show_prettyformat_structs
,
3107 &setprintlist
, &showprintlist
);
3109 add_setshow_boolean_cmd ("union", class_support
,
3110 &user_print_options
.unionprint
, _("\
3111 Set printing of unions interior to structures."), _("\
3112 Show printing of unions interior to structures."), NULL
,
3115 &setprintlist
, &showprintlist
);
3117 add_setshow_boolean_cmd ("array", class_support
,
3118 &user_print_options
.prettyformat_arrays
, _("\
3119 Set pretty formatting of arrays."), _("\
3120 Show pretty formatting of arrays."), NULL
,
3122 show_prettyformat_arrays
,
3123 &setprintlist
, &showprintlist
);
3125 add_setshow_boolean_cmd ("address", class_support
,
3126 &user_print_options
.addressprint
, _("\
3127 Set printing of addresses."), _("\
3128 Show printing of addresses."), NULL
,
3131 &setprintlist
, &showprintlist
);
3133 add_setshow_boolean_cmd ("symbol", class_support
,
3134 &user_print_options
.symbol_print
, _("\
3135 Set printing of symbol names when printing pointers."), _("\
3136 Show printing of symbol names when printing pointers."),
3139 &setprintlist
, &showprintlist
);
3141 add_setshow_zuinteger_cmd ("input-radix", class_support
, &input_radix_1
,
3143 Set default input radix for entering numbers."), _("\
3144 Show default input radix for entering numbers."), NULL
,
3147 &setlist
, &showlist
);
3149 add_setshow_zuinteger_cmd ("output-radix", class_support
, &output_radix_1
,
3151 Set default output radix for printing of values."), _("\
3152 Show default output radix for printing of values."), NULL
,
3155 &setlist
, &showlist
);
3157 /* The "set radix" and "show radix" commands are special in that
3158 they are like normal set and show commands but allow two normally
3159 independent variables to be either set or shown with a single
3160 command. So the usual deprecated_add_set_cmd() and [deleted]
3161 add_show_from_set() commands aren't really appropriate. */
3162 /* FIXME: i18n: With the new add_setshow_integer command, that is no
3163 longer true - show can display anything. */
3164 add_cmd ("radix", class_support
, set_radix
, _("\
3165 Set default input and output number radices.\n\
3166 Use 'set input-radix' or 'set output-radix' to independently set each.\n\
3167 Without an argument, sets both radices back to the default value of 10."),
3169 add_cmd ("radix", class_support
, show_radix
, _("\
3170 Show the default input and output number radices.\n\
3171 Use 'show input-radix' or 'show output-radix' to independently show each."),
3174 add_setshow_boolean_cmd ("array-indexes", class_support
,
3175 &user_print_options
.print_array_indexes
, _("\
3176 Set printing of array indexes."), _("\
3177 Show printing of array indexes"), NULL
, NULL
, show_print_array_indexes
,
3178 &setprintlist
, &showprintlist
);