1 /* Print values for GDB, the GNU debugger.
3 Copyright (C) 1986-2016 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"
41 /* Maximum number of wchars returned from wchar_iterate. */
44 /* A convenience macro to compute the size of a wchar_t buffer containing X
46 #define WCHAR_BUFLEN(X) ((X) * sizeof (gdb_wchar_t))
48 /* Character buffer size saved while iterating over wchars. */
49 #define WCHAR_BUFLEN_MAX WCHAR_BUFLEN (MAX_WCHARS)
51 /* A structure to encapsulate state information from iterated
52 character conversions. */
53 struct converted_character
55 /* The number of characters converted. */
58 /* The result of the conversion. See charset.h for more. */
59 enum wchar_iterate_result result
;
61 /* The (saved) converted character(s). */
62 gdb_wchar_t chars
[WCHAR_BUFLEN_MAX
];
64 /* The first converted target byte. */
67 /* The number of bytes converted. */
70 /* How many times this character(s) is repeated. */
74 typedef struct converted_character converted_character_d
;
75 DEF_VEC_O (converted_character_d
);
77 /* Command lists for set/show print raw. */
78 struct cmd_list_element
*setprintrawlist
;
79 struct cmd_list_element
*showprintrawlist
;
81 /* Prototypes for local functions */
83 static int partial_memory_read (CORE_ADDR memaddr
, gdb_byte
*myaddr
,
84 int len
, int *errptr
);
86 static void show_print (char *, int);
88 static void set_print (char *, int);
90 static void set_radix (char *, int);
92 static void show_radix (char *, int);
94 static void set_input_radix (char *, int, struct cmd_list_element
*);
96 static void set_input_radix_1 (int, unsigned);
98 static void set_output_radix (char *, int, struct cmd_list_element
*);
100 static void set_output_radix_1 (int, unsigned);
102 static void val_print_type_code_flags (struct type
*type
,
103 const gdb_byte
*valaddr
,
104 struct ui_file
*stream
);
106 void _initialize_valprint (void);
108 #define PRINT_MAX_DEFAULT 200 /* Start print_max off at this value. */
110 struct value_print_options user_print_options
=
112 Val_prettyformat_default
, /* prettyformat */
113 0, /* prettyformat_arrays */
114 0, /* prettyformat_structs */
117 1, /* addressprint */
119 PRINT_MAX_DEFAULT
, /* print_max */
120 10, /* repeat_count_threshold */
121 0, /* output_format */
123 0, /* stop_print_at_null */
124 0, /* print_array_indexes */
126 1, /* static_field_print */
127 1, /* pascal_static_field_print */
133 /* Initialize *OPTS to be a copy of the user print options. */
135 get_user_print_options (struct value_print_options
*opts
)
137 *opts
= user_print_options
;
140 /* Initialize *OPTS to be a copy of the user print options, but with
141 pretty-formatting disabled. */
143 get_no_prettyformat_print_options (struct value_print_options
*opts
)
145 *opts
= user_print_options
;
146 opts
->prettyformat
= Val_no_prettyformat
;
149 /* Initialize *OPTS to be a copy of the user print options, but using
150 FORMAT as the formatting option. */
152 get_formatted_print_options (struct value_print_options
*opts
,
155 *opts
= user_print_options
;
156 opts
->format
= format
;
160 show_print_max (struct ui_file
*file
, int from_tty
,
161 struct cmd_list_element
*c
, const char *value
)
163 fprintf_filtered (file
,
164 _("Limit on string chars or array "
165 "elements to print is %s.\n"),
170 /* Default input and output radixes, and output format letter. */
172 unsigned input_radix
= 10;
174 show_input_radix (struct ui_file
*file
, int from_tty
,
175 struct cmd_list_element
*c
, const char *value
)
177 fprintf_filtered (file
,
178 _("Default input radix for entering numbers is %s.\n"),
182 unsigned output_radix
= 10;
184 show_output_radix (struct ui_file
*file
, int from_tty
,
185 struct cmd_list_element
*c
, const char *value
)
187 fprintf_filtered (file
,
188 _("Default output radix for printing of values is %s.\n"),
192 /* By default we print arrays without printing the index of each element in
193 the array. This behavior can be changed by setting PRINT_ARRAY_INDEXES. */
196 show_print_array_indexes (struct ui_file
*file
, int from_tty
,
197 struct cmd_list_element
*c
, const char *value
)
199 fprintf_filtered (file
, _("Printing of array indexes is %s.\n"), value
);
202 /* Print repeat counts if there are more than this many repetitions of an
203 element in an array. Referenced by the low level language dependent
207 show_repeat_count_threshold (struct ui_file
*file
, int from_tty
,
208 struct cmd_list_element
*c
, const char *value
)
210 fprintf_filtered (file
, _("Threshold for repeated print elements is %s.\n"),
214 /* If nonzero, stops printing of char arrays at first null. */
217 show_stop_print_at_null (struct ui_file
*file
, int from_tty
,
218 struct cmd_list_element
*c
, const char *value
)
220 fprintf_filtered (file
,
221 _("Printing of char arrays to stop "
222 "at first null char is %s.\n"),
226 /* Controls pretty printing of structures. */
229 show_prettyformat_structs (struct ui_file
*file
, int from_tty
,
230 struct cmd_list_element
*c
, const char *value
)
232 fprintf_filtered (file
, _("Pretty formatting of structures is %s.\n"), value
);
235 /* Controls pretty printing of arrays. */
238 show_prettyformat_arrays (struct ui_file
*file
, int from_tty
,
239 struct cmd_list_element
*c
, const char *value
)
241 fprintf_filtered (file
, _("Pretty formatting of arrays is %s.\n"), value
);
244 /* If nonzero, causes unions inside structures or other unions to be
248 show_unionprint (struct ui_file
*file
, int from_tty
,
249 struct cmd_list_element
*c
, const char *value
)
251 fprintf_filtered (file
,
252 _("Printing of unions interior to structures is %s.\n"),
256 /* If nonzero, causes machine addresses to be printed in certain contexts. */
259 show_addressprint (struct ui_file
*file
, int from_tty
,
260 struct cmd_list_element
*c
, const char *value
)
262 fprintf_filtered (file
, _("Printing of addresses is %s.\n"), value
);
266 show_symbol_print (struct ui_file
*file
, int from_tty
,
267 struct cmd_list_element
*c
, const char *value
)
269 fprintf_filtered (file
,
270 _("Printing of symbols when printing pointers is %s.\n"),
276 /* A helper function for val_print. When printing in "summary" mode,
277 we want to print scalar arguments, but not aggregate arguments.
278 This function distinguishes between the two. */
281 val_print_scalar_type_p (struct type
*type
)
283 type
= check_typedef (type
);
284 while (TYPE_CODE (type
) == TYPE_CODE_REF
)
286 type
= TYPE_TARGET_TYPE (type
);
287 type
= check_typedef (type
);
289 switch (TYPE_CODE (type
))
291 case TYPE_CODE_ARRAY
:
292 case TYPE_CODE_STRUCT
:
293 case TYPE_CODE_UNION
:
295 case TYPE_CODE_STRING
:
302 /* See its definition in value.h. */
305 valprint_check_validity (struct ui_file
*stream
,
307 LONGEST embedded_offset
,
308 const struct value
*val
)
310 type
= check_typedef (type
);
312 if (type_not_associated (type
))
314 val_print_not_associated (stream
);
318 if (type_not_allocated (type
))
320 val_print_not_allocated (stream
);
324 if (TYPE_CODE (type
) != TYPE_CODE_UNION
325 && TYPE_CODE (type
) != TYPE_CODE_STRUCT
326 && TYPE_CODE (type
) != TYPE_CODE_ARRAY
)
328 if (value_bits_any_optimized_out (val
,
329 TARGET_CHAR_BIT
* embedded_offset
,
330 TARGET_CHAR_BIT
* TYPE_LENGTH (type
)))
332 val_print_optimized_out (val
, stream
);
336 if (value_bits_synthetic_pointer (val
, TARGET_CHAR_BIT
* embedded_offset
,
337 TARGET_CHAR_BIT
* TYPE_LENGTH (type
)))
339 const int is_ref
= TYPE_CODE (type
) == TYPE_CODE_REF
;
340 int ref_is_addressable
= 0;
344 const struct value
*deref_val
= coerce_ref_if_computed (val
);
346 if (deref_val
!= NULL
)
347 ref_is_addressable
= value_lval_const (deref_val
) == lval_memory
;
350 if (!is_ref
|| !ref_is_addressable
)
351 fputs_filtered (_("<synthetic pointer>"), stream
);
353 /* C++ references should be valid even if they're synthetic. */
357 if (!value_bytes_available (val
, embedded_offset
, TYPE_LENGTH (type
)))
359 val_print_unavailable (stream
);
368 val_print_optimized_out (const struct value
*val
, struct ui_file
*stream
)
370 if (val
!= NULL
&& value_lval_const (val
) == lval_register
)
371 val_print_not_saved (stream
);
373 fprintf_filtered (stream
, _("<optimized out>"));
377 val_print_not_saved (struct ui_file
*stream
)
379 fprintf_filtered (stream
, _("<not saved>"));
383 val_print_unavailable (struct ui_file
*stream
)
385 fprintf_filtered (stream
, _("<unavailable>"));
389 val_print_invalid_address (struct ui_file
*stream
)
391 fprintf_filtered (stream
, _("<invalid address>"));
394 /* Print a pointer based on the type of its target.
396 Arguments to this functions are roughly the same as those in
397 generic_val_print. A difference is that ADDRESS is the address to print,
398 with embedded_offset already added. ELTTYPE represents
399 the pointed type after check_typedef. */
402 print_unpacked_pointer (struct type
*type
, struct type
*elttype
,
403 CORE_ADDR address
, struct ui_file
*stream
,
404 const struct value_print_options
*options
)
406 struct gdbarch
*gdbarch
= get_type_arch (type
);
408 if (TYPE_CODE (elttype
) == TYPE_CODE_FUNC
)
410 /* Try to print what function it points to. */
411 print_function_pointer_address (options
, gdbarch
, address
, stream
);
415 if (options
->symbol_print
)
416 print_address_demangle (options
, gdbarch
, address
, stream
, demangle
);
417 else if (options
->addressprint
)
418 fputs_filtered (paddress (gdbarch
, address
), stream
);
421 /* generic_val_print helper for TYPE_CODE_ARRAY. */
424 generic_val_print_array (struct type
*type
,
425 int embedded_offset
, CORE_ADDR address
,
426 struct ui_file
*stream
, int recurse
,
427 struct value
*original_value
,
428 const struct value_print_options
*options
,
430 generic_val_print_decorations
*decorations
)
432 struct type
*unresolved_elttype
= TYPE_TARGET_TYPE (type
);
433 struct type
*elttype
= check_typedef (unresolved_elttype
);
435 if (TYPE_LENGTH (type
) > 0 && TYPE_LENGTH (unresolved_elttype
) > 0)
437 LONGEST low_bound
, high_bound
;
439 if (!get_array_bounds (type
, &low_bound
, &high_bound
))
440 error (_("Could not determine the array high bound"));
442 if (options
->prettyformat_arrays
)
444 print_spaces_filtered (2 + 2 * recurse
, stream
);
447 fputs_filtered (decorations
->array_start
, stream
);
448 val_print_array_elements (type
, embedded_offset
,
450 recurse
, original_value
, options
, 0);
451 fputs_filtered (decorations
->array_end
, stream
);
455 /* Array of unspecified length: treat like pointer to first elt. */
456 print_unpacked_pointer (type
, elttype
, address
+ embedded_offset
, stream
,
462 /* generic_val_print helper for TYPE_CODE_PTR. */
465 generic_val_print_ptr (struct type
*type
,
466 int embedded_offset
, struct ui_file
*stream
,
467 struct value
*original_value
,
468 const struct value_print_options
*options
)
470 struct gdbarch
*gdbarch
= get_type_arch (type
);
471 int unit_size
= gdbarch_addressable_memory_unit_size (gdbarch
);
473 if (options
->format
&& options
->format
!= 's')
475 val_print_scalar_formatted (type
, embedded_offset
,
476 original_value
, options
, 0, stream
);
480 struct type
*unresolved_elttype
= TYPE_TARGET_TYPE(type
);
481 struct type
*elttype
= check_typedef (unresolved_elttype
);
482 const gdb_byte
*valaddr
= value_contents_for_printing (original_value
);
483 CORE_ADDR addr
= unpack_pointer (type
,
484 valaddr
+ embedded_offset
* unit_size
);
486 print_unpacked_pointer (type
, elttype
, addr
, stream
, options
);
491 /* generic_val_print helper for TYPE_CODE_MEMBERPTR. */
494 generic_val_print_memberptr (struct type
*type
,
495 int embedded_offset
, struct ui_file
*stream
,
496 struct value
*original_value
,
497 const struct value_print_options
*options
)
499 val_print_scalar_formatted (type
, embedded_offset
,
500 original_value
, options
, 0, stream
);
503 /* Print '@' followed by the address contained in ADDRESS_BUFFER. */
506 print_ref_address (struct type
*type
, const gdb_byte
*address_buffer
,
507 int embedded_offset
, struct ui_file
*stream
)
509 struct gdbarch
*gdbarch
= get_type_arch (type
);
511 if (address_buffer
!= NULL
)
514 = extract_typed_address (address_buffer
+ embedded_offset
, type
);
516 fprintf_filtered (stream
, "@");
517 fputs_filtered (paddress (gdbarch
, address
), stream
);
519 /* Else: we have a non-addressable value, such as a DW_AT_const_value. */
522 /* If VAL is addressable, return the value contents buffer of a value that
523 represents a pointer to VAL. Otherwise return NULL. */
525 static const gdb_byte
*
526 get_value_addr_contents (struct value
*deref_val
)
528 gdb_assert (deref_val
!= NULL
);
530 if (value_lval_const (deref_val
) == lval_memory
)
531 return value_contents_for_printing_const (value_addr (deref_val
));
534 /* We have a non-addressable value, such as a DW_AT_const_value. */
539 /* generic_val_print helper for TYPE_CODE_REF. */
542 generic_val_print_ref (struct type
*type
,
543 int embedded_offset
, struct ui_file
*stream
, int recurse
,
544 struct value
*original_value
,
545 const struct value_print_options
*options
)
547 struct type
*elttype
= check_typedef (TYPE_TARGET_TYPE (type
));
548 struct value
*deref_val
= NULL
;
549 const int value_is_synthetic
550 = value_bits_synthetic_pointer (original_value
,
551 TARGET_CHAR_BIT
* embedded_offset
,
552 TARGET_CHAR_BIT
* TYPE_LENGTH (type
));
553 const int must_coerce_ref
= ((options
->addressprint
&& value_is_synthetic
)
554 || options
->deref_ref
);
555 const int type_is_defined
= TYPE_CODE (elttype
) != TYPE_CODE_UNDEF
;
556 const gdb_byte
*valaddr
= value_contents_for_printing (original_value
);
558 if (must_coerce_ref
&& type_is_defined
)
560 deref_val
= coerce_ref_if_computed (original_value
);
562 if (deref_val
!= NULL
)
564 /* More complicated computed references are not supported. */
565 gdb_assert (embedded_offset
== 0);
568 deref_val
= value_at (TYPE_TARGET_TYPE (type
),
569 unpack_pointer (type
, valaddr
+ embedded_offset
));
571 /* Else, original_value isn't a synthetic reference or we don't have to print
572 the reference's contents.
574 Notice that for references to TYPE_CODE_STRUCT, 'set print object on' will
575 cause original_value to be a not_lval instead of an lval_computed,
576 which will make value_bits_synthetic_pointer return false.
577 This happens because if options->objectprint is true, c_value_print will
578 overwrite original_value's contents with the result of coercing
579 the reference through value_addr, and then set its type back to
580 TYPE_CODE_REF. In that case we don't have to coerce the reference again;
581 we can simply treat it as non-synthetic and move on. */
583 if (options
->addressprint
)
585 const gdb_byte
*address
= (value_is_synthetic
&& type_is_defined
586 ? get_value_addr_contents (deref_val
)
589 print_ref_address (type
, address
, embedded_offset
, stream
);
591 if (options
->deref_ref
)
592 fputs_filtered (": ", stream
);
595 if (options
->deref_ref
)
598 common_val_print (deref_val
, stream
, recurse
, options
,
601 fputs_filtered ("???", stream
);
605 /* Helper function for generic_val_print_enum.
606 This is also used to print enums in TYPE_CODE_FLAGS values. */
609 generic_val_print_enum_1 (struct type
*type
, LONGEST val
,
610 struct ui_file
*stream
)
615 len
= TYPE_NFIELDS (type
);
616 for (i
= 0; i
< len
; i
++)
619 if (val
== TYPE_FIELD_ENUMVAL (type
, i
))
626 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
628 else if (TYPE_FLAG_ENUM (type
))
632 /* We have a "flag" enum, so we try to decompose it into
633 pieces as appropriate. A flag enum has disjoint
634 constants by definition. */
635 fputs_filtered ("(", stream
);
636 for (i
= 0; i
< len
; ++i
)
640 if ((val
& TYPE_FIELD_ENUMVAL (type
, i
)) != 0)
643 fputs_filtered (" | ", stream
);
646 val
&= ~TYPE_FIELD_ENUMVAL (type
, i
);
647 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
651 if (first
|| val
!= 0)
654 fputs_filtered (" | ", stream
);
655 fputs_filtered ("unknown: ", stream
);
656 print_longest (stream
, 'd', 0, val
);
659 fputs_filtered (")", stream
);
662 print_longest (stream
, 'd', 0, val
);
665 /* generic_val_print helper for TYPE_CODE_ENUM. */
668 generic_val_print_enum (struct type
*type
,
669 int embedded_offset
, struct ui_file
*stream
,
670 struct value
*original_value
,
671 const struct value_print_options
*options
)
674 struct gdbarch
*gdbarch
= get_type_arch (type
);
675 int unit_size
= gdbarch_addressable_memory_unit_size (gdbarch
);
679 val_print_scalar_formatted (type
, embedded_offset
,
680 original_value
, options
, 0, stream
);
684 const gdb_byte
*valaddr
= value_contents_for_printing (original_value
);
686 val
= unpack_long (type
, valaddr
+ embedded_offset
* unit_size
);
688 generic_val_print_enum_1 (type
, val
, stream
);
692 /* generic_val_print helper for TYPE_CODE_FLAGS. */
695 generic_val_print_flags (struct type
*type
,
696 int embedded_offset
, struct ui_file
*stream
,
697 struct value
*original_value
,
698 const struct value_print_options
*options
)
702 val_print_scalar_formatted (type
, embedded_offset
, original_value
,
706 const gdb_byte
*valaddr
= value_contents_for_printing (original_value
);
708 val_print_type_code_flags (type
, valaddr
+ embedded_offset
, stream
);
712 /* generic_val_print helper for TYPE_CODE_FUNC and TYPE_CODE_METHOD. */
715 generic_val_print_func (struct type
*type
,
716 int embedded_offset
, CORE_ADDR address
,
717 struct ui_file
*stream
,
718 struct value
*original_value
,
719 const struct value_print_options
*options
)
721 struct gdbarch
*gdbarch
= get_type_arch (type
);
725 val_print_scalar_formatted (type
, embedded_offset
,
726 original_value
, options
, 0, stream
);
730 /* FIXME, we should consider, at least for ANSI C language,
731 eliminating the distinction made between FUNCs and POINTERs
733 fprintf_filtered (stream
, "{");
734 type_print (type
, "", stream
, -1);
735 fprintf_filtered (stream
, "} ");
736 /* Try to print what function it points to, and its address. */
737 print_address_demangle (options
, gdbarch
, address
, stream
, demangle
);
741 /* generic_val_print helper for TYPE_CODE_BOOL. */
744 generic_val_print_bool (struct type
*type
,
745 int embedded_offset
, struct ui_file
*stream
,
746 struct value
*original_value
,
747 const struct value_print_options
*options
,
748 const struct generic_val_print_decorations
*decorations
)
751 struct gdbarch
*gdbarch
= get_type_arch (type
);
752 int unit_size
= gdbarch_addressable_memory_unit_size (gdbarch
);
754 if (options
->format
|| options
->output_format
)
756 struct value_print_options opts
= *options
;
757 opts
.format
= (options
->format
? options
->format
758 : options
->output_format
);
759 val_print_scalar_formatted (type
, embedded_offset
,
760 original_value
, &opts
, 0, stream
);
764 const gdb_byte
*valaddr
= value_contents_for_printing (original_value
);
766 val
= unpack_long (type
, valaddr
+ embedded_offset
* unit_size
);
768 fputs_filtered (decorations
->false_name
, stream
);
770 fputs_filtered (decorations
->true_name
, stream
);
772 print_longest (stream
, 'd', 0, val
);
776 /* generic_val_print helper for TYPE_CODE_INT. */
779 generic_val_print_int (struct type
*type
,
780 int embedded_offset
, struct ui_file
*stream
,
781 struct value
*original_value
,
782 const struct value_print_options
*options
)
784 struct gdbarch
*gdbarch
= get_type_arch (type
);
785 int unit_size
= gdbarch_addressable_memory_unit_size (gdbarch
);
787 if (options
->format
|| options
->output_format
)
789 struct value_print_options opts
= *options
;
791 opts
.format
= (options
->format
? options
->format
792 : options
->output_format
);
793 val_print_scalar_formatted (type
, embedded_offset
,
794 original_value
, &opts
, 0, stream
);
798 const gdb_byte
*valaddr
= value_contents_for_printing (original_value
);
800 val_print_type_code_int (type
, valaddr
+ embedded_offset
* unit_size
,
805 /* generic_val_print helper for TYPE_CODE_CHAR. */
808 generic_val_print_char (struct type
*type
, struct type
*unresolved_type
,
810 struct ui_file
*stream
,
811 struct value
*original_value
,
812 const struct value_print_options
*options
)
815 struct gdbarch
*gdbarch
= get_type_arch (type
);
816 int unit_size
= gdbarch_addressable_memory_unit_size (gdbarch
);
818 if (options
->format
|| options
->output_format
)
820 struct value_print_options opts
= *options
;
822 opts
.format
= (options
->format
? options
->format
823 : options
->output_format
);
824 val_print_scalar_formatted (type
, embedded_offset
,
825 original_value
, &opts
, 0, stream
);
829 const gdb_byte
*valaddr
= value_contents_for_printing (original_value
);
831 val
= unpack_long (type
, valaddr
+ embedded_offset
* unit_size
);
832 if (TYPE_UNSIGNED (type
))
833 fprintf_filtered (stream
, "%u", (unsigned int) val
);
835 fprintf_filtered (stream
, "%d", (int) val
);
836 fputs_filtered (" ", stream
);
837 LA_PRINT_CHAR (val
, unresolved_type
, stream
);
841 /* generic_val_print helper for TYPE_CODE_FLT. */
844 generic_val_print_float (struct type
*type
,
845 int embedded_offset
, struct ui_file
*stream
,
846 struct value
*original_value
,
847 const struct value_print_options
*options
)
849 struct gdbarch
*gdbarch
= get_type_arch (type
);
850 int unit_size
= gdbarch_addressable_memory_unit_size (gdbarch
);
854 val_print_scalar_formatted (type
, embedded_offset
,
855 original_value
, options
, 0, stream
);
859 const gdb_byte
*valaddr
= value_contents_for_printing (original_value
);
861 print_floating (valaddr
+ embedded_offset
* unit_size
, type
, stream
);
865 /* generic_val_print helper for TYPE_CODE_DECFLOAT. */
868 generic_val_print_decfloat (struct type
*type
,
869 int embedded_offset
, struct ui_file
*stream
,
870 struct value
*original_value
,
871 const struct value_print_options
*options
)
873 struct gdbarch
*gdbarch
= get_type_arch (type
);
874 int unit_size
= gdbarch_addressable_memory_unit_size (gdbarch
);
877 val_print_scalar_formatted (type
, embedded_offset
, original_value
,
881 const gdb_byte
*valaddr
= value_contents_for_printing (original_value
);
883 print_decimal_floating (valaddr
+ embedded_offset
* unit_size
, type
,
888 /* generic_val_print helper for TYPE_CODE_COMPLEX. */
891 generic_val_print_complex (struct type
*type
,
892 int embedded_offset
, struct ui_file
*stream
,
893 struct value
*original_value
,
894 const struct value_print_options
*options
,
895 const struct generic_val_print_decorations
898 struct gdbarch
*gdbarch
= get_type_arch (type
);
899 int unit_size
= gdbarch_addressable_memory_unit_size (gdbarch
);
900 const gdb_byte
*valaddr
= value_contents_for_printing (original_value
);
902 fprintf_filtered (stream
, "%s", decorations
->complex_prefix
);
904 val_print_scalar_formatted (TYPE_TARGET_TYPE (type
),
905 embedded_offset
, original_value
, options
, 0,
908 print_floating (valaddr
+ embedded_offset
* unit_size
,
909 TYPE_TARGET_TYPE (type
), stream
);
910 fprintf_filtered (stream
, "%s", decorations
->complex_infix
);
912 val_print_scalar_formatted (TYPE_TARGET_TYPE (type
),
914 + type_length_units (TYPE_TARGET_TYPE (type
)),
915 original_value
, options
, 0, stream
);
917 print_floating (valaddr
+ embedded_offset
* unit_size
918 + TYPE_LENGTH (TYPE_TARGET_TYPE (type
)),
919 TYPE_TARGET_TYPE (type
), stream
);
920 fprintf_filtered (stream
, "%s", decorations
->complex_suffix
);
923 /* A generic val_print that is suitable for use by language
924 implementations of the la_val_print method. This function can
925 handle most type codes, though not all, notably exception
926 TYPE_CODE_UNION and TYPE_CODE_STRUCT, which must be implemented by
929 Most arguments are as to val_print.
931 The additional DECORATIONS argument can be used to customize the
932 output in some small, language-specific ways. */
935 generic_val_print (struct type
*type
,
936 int embedded_offset
, CORE_ADDR address
,
937 struct ui_file
*stream
, int recurse
,
938 struct value
*original_value
,
939 const struct value_print_options
*options
,
940 const struct generic_val_print_decorations
*decorations
)
942 struct type
*unresolved_type
= type
;
944 type
= check_typedef (type
);
945 switch (TYPE_CODE (type
))
947 case TYPE_CODE_ARRAY
:
948 generic_val_print_array (type
, embedded_offset
, address
, stream
,
949 recurse
, original_value
, options
, decorations
);
952 case TYPE_CODE_MEMBERPTR
:
953 generic_val_print_memberptr (type
, embedded_offset
, stream
,
954 original_value
, options
);
958 generic_val_print_ptr (type
, embedded_offset
, stream
,
959 original_value
, options
);
963 generic_val_print_ref (type
, embedded_offset
, stream
, recurse
,
964 original_value
, options
);
968 generic_val_print_enum (type
, embedded_offset
, stream
,
969 original_value
, options
);
972 case TYPE_CODE_FLAGS
:
973 generic_val_print_flags (type
, embedded_offset
, stream
,
974 original_value
, options
);
978 case TYPE_CODE_METHOD
:
979 generic_val_print_func (type
, embedded_offset
, address
, stream
,
980 original_value
, options
);
984 generic_val_print_bool (type
, embedded_offset
, stream
,
985 original_value
, options
, decorations
);
988 case TYPE_CODE_RANGE
:
989 /* FIXME: create_static_range_type does not set the unsigned bit in a
990 range type (I think it probably should copy it from the
991 target type), so we won't print values which are too large to
992 fit in a signed integer correctly. */
993 /* FIXME: Doesn't handle ranges of enums correctly. (Can't just
994 print with the target type, though, because the size of our
995 type and the target type might differ). */
1000 generic_val_print_int (type
, embedded_offset
, stream
,
1001 original_value
, options
);
1004 case TYPE_CODE_CHAR
:
1005 generic_val_print_char (type
, unresolved_type
, embedded_offset
,
1006 stream
, original_value
, options
);
1010 generic_val_print_float (type
, embedded_offset
, stream
,
1011 original_value
, options
);
1014 case TYPE_CODE_DECFLOAT
:
1015 generic_val_print_decfloat (type
, embedded_offset
, stream
,
1016 original_value
, options
);
1019 case TYPE_CODE_VOID
:
1020 fputs_filtered (decorations
->void_name
, stream
);
1023 case TYPE_CODE_ERROR
:
1024 fprintf_filtered (stream
, "%s", TYPE_ERROR_NAME (type
));
1027 case TYPE_CODE_UNDEF
:
1028 /* This happens (without TYPE_STUB set) on systems which don't use
1029 dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
1030 and no complete type for struct foo in that file. */
1031 fprintf_filtered (stream
, _("<incomplete type>"));
1034 case TYPE_CODE_COMPLEX
:
1035 generic_val_print_complex (type
, embedded_offset
, stream
,
1036 original_value
, options
, decorations
);
1039 case TYPE_CODE_UNION
:
1040 case TYPE_CODE_STRUCT
:
1041 case TYPE_CODE_METHODPTR
:
1043 error (_("Unhandled type code %d in symbol table."),
1049 /* Print using the given LANGUAGE the data of type TYPE located at
1050 VAL's contents buffer + EMBEDDED_OFFSET (within GDB), which came
1051 from the inferior at address ADDRESS + EMBEDDED_OFFSET, onto
1052 stdio stream STREAM according to OPTIONS. VAL is the whole object
1053 that came from ADDRESS.
1055 The language printers will pass down an adjusted EMBEDDED_OFFSET to
1056 further helper subroutines as subfields of TYPE are printed. In
1057 such cases, VAL is passed down unadjusted, so
1058 that VAL can be queried for metadata about the contents data being
1059 printed, using EMBEDDED_OFFSET as an offset into VAL's contents
1060 buffer. For example: "has this field been optimized out", or "I'm
1061 printing an object while inspecting a traceframe; has this
1062 particular piece of data been collected?".
1064 RECURSE indicates the amount of indentation to supply before
1065 continuation lines; this amount is roughly twice the value of
1069 val_print (struct type
*type
, LONGEST embedded_offset
,
1070 CORE_ADDR address
, struct ui_file
*stream
, int recurse
,
1072 const struct value_print_options
*options
,
1073 const struct language_defn
*language
)
1076 struct value_print_options local_opts
= *options
;
1077 struct type
*real_type
= check_typedef (type
);
1079 if (local_opts
.prettyformat
== Val_prettyformat_default
)
1080 local_opts
.prettyformat
= (local_opts
.prettyformat_structs
1081 ? Val_prettyformat
: Val_no_prettyformat
);
1085 /* Ensure that the type is complete and not just a stub. If the type is
1086 only a stub and we can't find and substitute its complete type, then
1087 print appropriate string and return. */
1089 if (TYPE_STUB (real_type
))
1091 fprintf_filtered (stream
, _("<incomplete type>"));
1096 if (!valprint_check_validity (stream
, real_type
, embedded_offset
, val
))
1101 const gdb_byte
*valaddr
= value_contents_for_printing (val
);
1103 ret
= apply_ext_lang_val_pretty_printer (type
, valaddr
, embedded_offset
,
1104 address
, stream
, recurse
,
1105 val
, options
, language
);
1110 /* Handle summary mode. If the value is a scalar, print it;
1111 otherwise, print an ellipsis. */
1112 if (options
->summary
&& !val_print_scalar_type_p (type
))
1114 fprintf_filtered (stream
, "...");
1120 language
->la_val_print (type
, embedded_offset
, address
,
1121 stream
, recurse
, val
,
1124 CATCH (except
, RETURN_MASK_ERROR
)
1126 fprintf_filtered (stream
, _("<error reading variable>"));
1131 /* Check whether the value VAL is printable. Return 1 if it is;
1132 return 0 and print an appropriate error message to STREAM according to
1133 OPTIONS if it is not. */
1136 value_check_printable (struct value
*val
, struct ui_file
*stream
,
1137 const struct value_print_options
*options
)
1141 fprintf_filtered (stream
, _("<address of value unknown>"));
1145 if (value_entirely_optimized_out (val
))
1147 if (options
->summary
&& !val_print_scalar_type_p (value_type (val
)))
1148 fprintf_filtered (stream
, "...");
1150 val_print_optimized_out (val
, stream
);
1154 if (value_entirely_unavailable (val
))
1156 if (options
->summary
&& !val_print_scalar_type_p (value_type (val
)))
1157 fprintf_filtered (stream
, "...");
1159 val_print_unavailable (stream
);
1163 if (TYPE_CODE (value_type (val
)) == TYPE_CODE_INTERNAL_FUNCTION
)
1165 fprintf_filtered (stream
, _("<internal function %s>"),
1166 value_internal_function_name (val
));
1170 if (type_not_associated (value_type (val
)))
1172 val_print_not_associated (stream
);
1176 if (type_not_allocated (value_type (val
)))
1178 val_print_not_allocated (stream
);
1185 /* Print using the given LANGUAGE the value VAL onto stream STREAM according
1188 This is a preferable interface to val_print, above, because it uses
1189 GDB's value mechanism. */
1192 common_val_print (struct value
*val
, struct ui_file
*stream
, int recurse
,
1193 const struct value_print_options
*options
,
1194 const struct language_defn
*language
)
1196 if (!value_check_printable (val
, stream
, options
))
1199 if (language
->la_language
== language_ada
)
1200 /* The value might have a dynamic type, which would cause trouble
1201 below when trying to extract the value contents (since the value
1202 size is determined from the type size which is unknown). So
1203 get a fixed representation of our value. */
1204 val
= ada_to_fixed_value (val
);
1206 val_print (value_type (val
),
1207 value_embedded_offset (val
), value_address (val
),
1209 val
, options
, language
);
1212 /* Print on stream STREAM the value VAL according to OPTIONS. The value
1213 is printed using the current_language syntax. */
1216 value_print (struct value
*val
, struct ui_file
*stream
,
1217 const struct value_print_options
*options
)
1219 if (!value_check_printable (val
, stream
, options
))
1225 = apply_ext_lang_val_pretty_printer (value_type (val
),
1226 value_contents_for_printing (val
),
1227 value_embedded_offset (val
),
1228 value_address (val
),
1230 val
, options
, current_language
);
1236 LA_VALUE_PRINT (val
, stream
, options
);
1239 /* Called by various <lang>_val_print routines to print
1240 TYPE_CODE_INT's. TYPE is the type. VALADDR is the address of the
1241 value. STREAM is where to print the value. */
1244 val_print_type_code_int (struct type
*type
, const gdb_byte
*valaddr
,
1245 struct ui_file
*stream
)
1247 enum bfd_endian byte_order
= gdbarch_byte_order (get_type_arch (type
));
1249 if (TYPE_LENGTH (type
) > sizeof (LONGEST
))
1253 if (TYPE_UNSIGNED (type
)
1254 && extract_long_unsigned_integer (valaddr
, TYPE_LENGTH (type
),
1257 print_longest (stream
, 'u', 0, val
);
1261 /* Signed, or we couldn't turn an unsigned value into a
1262 LONGEST. For signed values, one could assume two's
1263 complement (a reasonable assumption, I think) and do
1264 better than this. */
1265 print_hex_chars (stream
, (unsigned char *) valaddr
,
1266 TYPE_LENGTH (type
), byte_order
);
1271 print_longest (stream
, TYPE_UNSIGNED (type
) ? 'u' : 'd', 0,
1272 unpack_long (type
, valaddr
));
1277 val_print_type_code_flags (struct type
*type
, const gdb_byte
*valaddr
,
1278 struct ui_file
*stream
)
1280 ULONGEST val
= unpack_long (type
, valaddr
);
1281 int field
, nfields
= TYPE_NFIELDS (type
);
1282 struct gdbarch
*gdbarch
= get_type_arch (type
);
1283 struct type
*bool_type
= builtin_type (gdbarch
)->builtin_bool
;
1285 fputs_filtered ("[", stream
);
1286 for (field
= 0; field
< nfields
; field
++)
1288 if (TYPE_FIELD_NAME (type
, field
)[0] != '\0')
1290 struct type
*field_type
= TYPE_FIELD_TYPE (type
, field
);
1292 if (field_type
== bool_type
1293 /* We require boolean types here to be one bit wide. This is a
1294 problematic place to notify the user of an internal error
1295 though. Instead just fall through and print the field as an
1297 && TYPE_FIELD_BITSIZE (type
, field
) == 1)
1299 if (val
& ((ULONGEST
)1 << TYPE_FIELD_BITPOS (type
, field
)))
1300 fprintf_filtered (stream
, " %s",
1301 TYPE_FIELD_NAME (type
, field
));
1305 unsigned field_len
= TYPE_FIELD_BITSIZE (type
, field
);
1307 = val
>> (TYPE_FIELD_BITPOS (type
, field
) - field_len
+ 1);
1309 if (field_len
< sizeof (ULONGEST
) * TARGET_CHAR_BIT
)
1310 field_val
&= ((ULONGEST
) 1 << field_len
) - 1;
1311 fprintf_filtered (stream
, " %s=",
1312 TYPE_FIELD_NAME (type
, field
));
1313 if (TYPE_CODE (field_type
) == TYPE_CODE_ENUM
)
1314 generic_val_print_enum_1 (field_type
, field_val
, stream
);
1316 print_longest (stream
, 'd', 0, field_val
);
1320 fputs_filtered (" ]", stream
);
1323 /* Print a scalar of data of type TYPE, pointed to in GDB by VALADDR,
1324 according to OPTIONS and SIZE on STREAM. Format i is not supported
1327 This is how the elements of an array or structure are printed
1331 val_print_scalar_formatted (struct type
*type
,
1332 LONGEST embedded_offset
,
1334 const struct value_print_options
*options
,
1336 struct ui_file
*stream
)
1338 struct gdbarch
*arch
= get_type_arch (type
);
1339 int unit_size
= gdbarch_addressable_memory_unit_size (arch
);
1341 gdb_assert (val
!= NULL
);
1343 /* If we get here with a string format, try again without it. Go
1344 all the way back to the language printers, which may call us
1346 if (options
->format
== 's')
1348 struct value_print_options opts
= *options
;
1351 val_print (type
, embedded_offset
, 0, stream
, 0, val
, &opts
,
1356 /* value_contents_for_printing fetches all VAL's contents. They are
1357 needed to check whether VAL is optimized-out or unavailable
1359 const gdb_byte
*valaddr
= value_contents_for_printing (val
);
1361 /* A scalar object that does not have all bits available can't be
1362 printed, because all bits contribute to its representation. */
1363 if (value_bits_any_optimized_out (val
,
1364 TARGET_CHAR_BIT
* embedded_offset
,
1365 TARGET_CHAR_BIT
* TYPE_LENGTH (type
)))
1366 val_print_optimized_out (val
, stream
);
1367 else if (!value_bytes_available (val
, embedded_offset
, TYPE_LENGTH (type
)))
1368 val_print_unavailable (stream
);
1370 print_scalar_formatted (valaddr
+ embedded_offset
* unit_size
, type
,
1371 options
, size
, stream
);
1374 /* Print a number according to FORMAT which is one of d,u,x,o,b,h,w,g.
1375 The raison d'etre of this function is to consolidate printing of
1376 LONG_LONG's into this one function. The format chars b,h,w,g are
1377 from print_scalar_formatted(). Numbers are printed using C
1380 USE_C_FORMAT means to use C format in all cases. Without it,
1381 'o' and 'x' format do not include the standard C radix prefix
1384 Hilfinger/2004-09-09: USE_C_FORMAT was originally called USE_LOCAL
1385 and was intended to request formating according to the current
1386 language and would be used for most integers that GDB prints. The
1387 exceptional cases were things like protocols where the format of
1388 the integer is a protocol thing, not a user-visible thing). The
1389 parameter remains to preserve the information of what things might
1390 be printed with language-specific format, should we ever resurrect
1394 print_longest (struct ui_file
*stream
, int format
, int use_c_format
,
1402 val
= int_string (val_long
, 10, 1, 0, 1); break;
1404 val
= int_string (val_long
, 10, 0, 0, 1); break;
1406 val
= int_string (val_long
, 16, 0, 0, use_c_format
); break;
1408 val
= int_string (val_long
, 16, 0, 2, 1); break;
1410 val
= int_string (val_long
, 16, 0, 4, 1); break;
1412 val
= int_string (val_long
, 16, 0, 8, 1); break;
1414 val
= int_string (val_long
, 16, 0, 16, 1); break;
1417 val
= int_string (val_long
, 8, 0, 0, use_c_format
); break;
1419 internal_error (__FILE__
, __LINE__
,
1420 _("failed internal consistency check"));
1422 fputs_filtered (val
, stream
);
1425 /* This used to be a macro, but I don't think it is called often enough
1426 to merit such treatment. */
1427 /* Convert a LONGEST to an int. This is used in contexts (e.g. number of
1428 arguments to a function, number in a value history, register number, etc.)
1429 where the value must not be larger than can fit in an int. */
1432 longest_to_int (LONGEST arg
)
1434 /* Let the compiler do the work. */
1435 int rtnval
= (int) arg
;
1437 /* Check for overflows or underflows. */
1438 if (sizeof (LONGEST
) > sizeof (int))
1442 error (_("Value out of range."));
1448 /* Print a floating point value of type TYPE (not always a
1449 TYPE_CODE_FLT), pointed to in GDB by VALADDR, on STREAM. */
1452 print_floating (const gdb_byte
*valaddr
, struct type
*type
,
1453 struct ui_file
*stream
)
1457 const struct floatformat
*fmt
= NULL
;
1458 unsigned len
= TYPE_LENGTH (type
);
1459 enum float_kind kind
;
1461 /* If it is a floating-point, check for obvious problems. */
1462 if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
1463 fmt
= floatformat_from_type (type
);
1466 kind
= floatformat_classify (fmt
, valaddr
);
1467 if (kind
== float_nan
)
1469 if (floatformat_is_negative (fmt
, valaddr
))
1470 fprintf_filtered (stream
, "-");
1471 fprintf_filtered (stream
, "nan(");
1472 fputs_filtered ("0x", stream
);
1473 fputs_filtered (floatformat_mantissa (fmt
, valaddr
), stream
);
1474 fprintf_filtered (stream
, ")");
1477 else if (kind
== float_infinite
)
1479 if (floatformat_is_negative (fmt
, valaddr
))
1480 fputs_filtered ("-", stream
);
1481 fputs_filtered ("inf", stream
);
1486 /* NOTE: cagney/2002-01-15: The TYPE passed into print_floating()
1487 isn't necessarily a TYPE_CODE_FLT. Consequently, unpack_double
1488 needs to be used as that takes care of any necessary type
1489 conversions. Such conversions are of course direct to DOUBLEST
1490 and disregard any possible target floating point limitations.
1491 For instance, a u64 would be converted and displayed exactly on a
1492 host with 80 bit DOUBLEST but with loss of information on a host
1493 with 64 bit DOUBLEST. */
1495 doub
= unpack_double (type
, valaddr
, &inv
);
1498 fprintf_filtered (stream
, "<invalid float value>");
1502 /* FIXME: kettenis/2001-01-20: The following code makes too much
1503 assumptions about the host and target floating point format. */
1505 /* NOTE: cagney/2002-02-03: Since the TYPE of what was passed in may
1506 not necessarily be a TYPE_CODE_FLT, the below ignores that and
1507 instead uses the type's length to determine the precision of the
1508 floating-point value being printed. */
1510 if (len
< sizeof (double))
1511 fprintf_filtered (stream
, "%.9g", (double) doub
);
1512 else if (len
== sizeof (double))
1513 fprintf_filtered (stream
, "%.17g", (double) doub
);
1515 #ifdef PRINTF_HAS_LONG_DOUBLE
1516 fprintf_filtered (stream
, "%.35Lg", doub
);
1518 /* This at least wins with values that are representable as
1520 fprintf_filtered (stream
, "%.17g", (double) doub
);
1525 print_decimal_floating (const gdb_byte
*valaddr
, struct type
*type
,
1526 struct ui_file
*stream
)
1528 enum bfd_endian byte_order
= gdbarch_byte_order (get_type_arch (type
));
1529 char decstr
[MAX_DECIMAL_STRING
];
1530 unsigned len
= TYPE_LENGTH (type
);
1532 decimal_to_string (valaddr
, len
, byte_order
, decstr
);
1533 fputs_filtered (decstr
, stream
);
1538 print_binary_chars (struct ui_file
*stream
, const gdb_byte
*valaddr
,
1539 unsigned len
, enum bfd_endian byte_order
)
1542 #define BITS_IN_BYTES 8
1548 /* Declared "int" so it will be signed.
1549 This ensures that right shift will shift in zeros. */
1551 const int mask
= 0x080;
1553 /* FIXME: We should be not printing leading zeroes in most cases. */
1555 if (byte_order
== BFD_ENDIAN_BIG
)
1561 /* Every byte has 8 binary characters; peel off
1562 and print from the MSB end. */
1564 for (i
= 0; i
< (BITS_IN_BYTES
* sizeof (*p
)); i
++)
1566 if (*p
& (mask
>> i
))
1571 fprintf_filtered (stream
, "%1d", b
);
1577 for (p
= valaddr
+ len
- 1;
1581 for (i
= 0; i
< (BITS_IN_BYTES
* sizeof (*p
)); i
++)
1583 if (*p
& (mask
>> i
))
1588 fprintf_filtered (stream
, "%1d", b
);
1594 /* VALADDR points to an integer of LEN bytes.
1595 Print it in octal on stream or format it in buf. */
1598 print_octal_chars (struct ui_file
*stream
, const gdb_byte
*valaddr
,
1599 unsigned len
, enum bfd_endian byte_order
)
1602 unsigned char octa1
, octa2
, octa3
, carry
;
1605 /* FIXME: We should be not printing leading zeroes in most cases. */
1608 /* Octal is 3 bits, which doesn't fit. Yuk. So we have to track
1609 * the extra bits, which cycle every three bytes:
1611 * Byte side: 0 1 2 3
1613 * bit number 123 456 78 | 9 012 345 6 | 78 901 234 | 567 890 12 |
1615 * Octal side: 0 1 carry 3 4 carry ...
1617 * Cycle number: 0 1 2
1619 * But of course we are printing from the high side, so we have to
1620 * figure out where in the cycle we are so that we end up with no
1621 * left over bits at the end.
1623 #define BITS_IN_OCTAL 3
1624 #define HIGH_ZERO 0340
1625 #define LOW_ZERO 0016
1626 #define CARRY_ZERO 0003
1627 #define HIGH_ONE 0200
1628 #define MID_ONE 0160
1629 #define LOW_ONE 0016
1630 #define CARRY_ONE 0001
1631 #define HIGH_TWO 0300
1632 #define MID_TWO 0070
1633 #define LOW_TWO 0007
1635 /* For 32 we start in cycle 2, with two bits and one bit carry;
1636 for 64 in cycle in cycle 1, with one bit and a two bit carry. */
1638 cycle
= (len
* BITS_IN_BYTES
) % BITS_IN_OCTAL
;
1641 fputs_filtered ("0", stream
);
1642 if (byte_order
== BFD_ENDIAN_BIG
)
1651 /* No carry in, carry out two bits. */
1653 octa1
= (HIGH_ZERO
& *p
) >> 5;
1654 octa2
= (LOW_ZERO
& *p
) >> 2;
1655 carry
= (CARRY_ZERO
& *p
);
1656 fprintf_filtered (stream
, "%o", octa1
);
1657 fprintf_filtered (stream
, "%o", octa2
);
1661 /* Carry in two bits, carry out one bit. */
1663 octa1
= (carry
<< 1) | ((HIGH_ONE
& *p
) >> 7);
1664 octa2
= (MID_ONE
& *p
) >> 4;
1665 octa3
= (LOW_ONE
& *p
) >> 1;
1666 carry
= (CARRY_ONE
& *p
);
1667 fprintf_filtered (stream
, "%o", octa1
);
1668 fprintf_filtered (stream
, "%o", octa2
);
1669 fprintf_filtered (stream
, "%o", octa3
);
1673 /* Carry in one bit, no carry out. */
1675 octa1
= (carry
<< 2) | ((HIGH_TWO
& *p
) >> 6);
1676 octa2
= (MID_TWO
& *p
) >> 3;
1677 octa3
= (LOW_TWO
& *p
);
1679 fprintf_filtered (stream
, "%o", octa1
);
1680 fprintf_filtered (stream
, "%o", octa2
);
1681 fprintf_filtered (stream
, "%o", octa3
);
1685 error (_("Internal error in octal conversion;"));
1689 cycle
= cycle
% BITS_IN_OCTAL
;
1694 for (p
= valaddr
+ len
- 1;
1701 /* Carry out, no carry in */
1703 octa1
= (HIGH_ZERO
& *p
) >> 5;
1704 octa2
= (LOW_ZERO
& *p
) >> 2;
1705 carry
= (CARRY_ZERO
& *p
);
1706 fprintf_filtered (stream
, "%o", octa1
);
1707 fprintf_filtered (stream
, "%o", octa2
);
1711 /* Carry in, carry out */
1713 octa1
= (carry
<< 1) | ((HIGH_ONE
& *p
) >> 7);
1714 octa2
= (MID_ONE
& *p
) >> 4;
1715 octa3
= (LOW_ONE
& *p
) >> 1;
1716 carry
= (CARRY_ONE
& *p
);
1717 fprintf_filtered (stream
, "%o", octa1
);
1718 fprintf_filtered (stream
, "%o", octa2
);
1719 fprintf_filtered (stream
, "%o", octa3
);
1723 /* Carry in, no carry out */
1725 octa1
= (carry
<< 2) | ((HIGH_TWO
& *p
) >> 6);
1726 octa2
= (MID_TWO
& *p
) >> 3;
1727 octa3
= (LOW_TWO
& *p
);
1729 fprintf_filtered (stream
, "%o", octa1
);
1730 fprintf_filtered (stream
, "%o", octa2
);
1731 fprintf_filtered (stream
, "%o", octa3
);
1735 error (_("Internal error in octal conversion;"));
1739 cycle
= cycle
% BITS_IN_OCTAL
;
1745 /* VALADDR points to an integer of LEN bytes.
1746 Print it in decimal on stream or format it in buf. */
1749 print_decimal_chars (struct ui_file
*stream
, const gdb_byte
*valaddr
,
1750 unsigned len
, enum bfd_endian byte_order
)
1753 #define CARRY_OUT( x ) ((x) / TEN) /* extend char to int */
1754 #define CARRY_LEFT( x ) ((x) % TEN)
1755 #define SHIFT( x ) ((x) << 4)
1756 #define LOW_NIBBLE( x ) ( (x) & 0x00F)
1757 #define HIGH_NIBBLE( x ) (((x) & 0x0F0) >> 4)
1760 unsigned char *digits
;
1763 int i
, j
, decimal_digits
;
1767 /* Base-ten number is less than twice as many digits
1768 as the base 16 number, which is 2 digits per byte. */
1770 decimal_len
= len
* 2 * 2;
1771 digits
= (unsigned char *) xmalloc (decimal_len
);
1773 for (i
= 0; i
< decimal_len
; i
++)
1778 /* Ok, we have an unknown number of bytes of data to be printed in
1781 * Given a hex number (in nibbles) as XYZ, we start by taking X and
1782 * decemalizing it as "x1 x2" in two decimal nibbles. Then we multiply
1783 * the nibbles by 16, add Y and re-decimalize. Repeat with Z.
1785 * The trick is that "digits" holds a base-10 number, but sometimes
1786 * the individual digits are > 10.
1788 * Outer loop is per nibble (hex digit) of input, from MSD end to
1791 decimal_digits
= 0; /* Number of decimal digits so far */
1792 p
= (byte_order
== BFD_ENDIAN_BIG
) ? valaddr
: valaddr
+ len
- 1;
1794 while ((byte_order
== BFD_ENDIAN_BIG
) ? (p
< valaddr
+ len
) : (p
>= valaddr
))
1797 * Multiply current base-ten number by 16 in place.
1798 * Each digit was between 0 and 9, now is between
1801 for (j
= 0; j
< decimal_digits
; j
++)
1803 digits
[j
] = SHIFT (digits
[j
]);
1806 /* Take the next nibble off the input and add it to what
1807 * we've got in the LSB position. Bottom 'digit' is now
1808 * between 0 and 159.
1810 * "flip" is used to run this loop twice for each byte.
1814 /* Take top nibble. */
1816 digits
[0] += HIGH_NIBBLE (*p
);
1821 /* Take low nibble and bump our pointer "p". */
1823 digits
[0] += LOW_NIBBLE (*p
);
1824 if (byte_order
== BFD_ENDIAN_BIG
)
1831 /* Re-decimalize. We have to do this often enough
1832 * that we don't overflow, but once per nibble is
1833 * overkill. Easier this way, though. Note that the
1834 * carry is often larger than 10 (e.g. max initial
1835 * carry out of lowest nibble is 15, could bubble all
1836 * the way up greater than 10). So we have to do
1837 * the carrying beyond the last current digit.
1840 for (j
= 0; j
< decimal_len
- 1; j
++)
1844 /* "/" won't handle an unsigned char with
1845 * a value that if signed would be negative.
1846 * So extend to longword int via "dummy".
1849 carry
= CARRY_OUT (dummy
);
1850 digits
[j
] = CARRY_LEFT (dummy
);
1852 if (j
>= decimal_digits
&& carry
== 0)
1855 * All higher digits are 0 and we
1856 * no longer have a carry.
1858 * Note: "j" is 0-based, "decimal_digits" is
1861 decimal_digits
= j
+ 1;
1867 /* Ok, now "digits" is the decimal representation, with
1868 the "decimal_digits" actual digits. Print! */
1870 for (i
= decimal_digits
- 1; i
>= 0; i
--)
1872 fprintf_filtered (stream
, "%1d", digits
[i
]);
1877 /* VALADDR points to an integer of LEN bytes. Print it in hex on stream. */
1880 print_hex_chars (struct ui_file
*stream
, const gdb_byte
*valaddr
,
1881 unsigned len
, enum bfd_endian byte_order
)
1885 /* FIXME: We should be not printing leading zeroes in most cases. */
1887 fputs_filtered ("0x", stream
);
1888 if (byte_order
== BFD_ENDIAN_BIG
)
1894 fprintf_filtered (stream
, "%02x", *p
);
1899 for (p
= valaddr
+ len
- 1;
1903 fprintf_filtered (stream
, "%02x", *p
);
1908 /* VALADDR points to a char integer of LEN bytes.
1909 Print it out in appropriate language form on stream.
1910 Omit any leading zero chars. */
1913 print_char_chars (struct ui_file
*stream
, struct type
*type
,
1914 const gdb_byte
*valaddr
,
1915 unsigned len
, enum bfd_endian byte_order
)
1919 if (byte_order
== BFD_ENDIAN_BIG
)
1922 while (p
< valaddr
+ len
- 1 && *p
== 0)
1925 while (p
< valaddr
+ len
)
1927 LA_EMIT_CHAR (*p
, type
, stream
, '\'');
1933 p
= valaddr
+ len
- 1;
1934 while (p
> valaddr
&& *p
== 0)
1937 while (p
>= valaddr
)
1939 LA_EMIT_CHAR (*p
, type
, stream
, '\'');
1945 /* Print function pointer with inferior address ADDRESS onto stdio
1949 print_function_pointer_address (const struct value_print_options
*options
,
1950 struct gdbarch
*gdbarch
,
1952 struct ui_file
*stream
)
1955 = gdbarch_convert_from_func_ptr_addr (gdbarch
, address
,
1958 /* If the function pointer is represented by a description, print
1959 the address of the description. */
1960 if (options
->addressprint
&& func_addr
!= address
)
1962 fputs_filtered ("@", stream
);
1963 fputs_filtered (paddress (gdbarch
, address
), stream
);
1964 fputs_filtered (": ", stream
);
1966 print_address_demangle (options
, gdbarch
, func_addr
, stream
, demangle
);
1970 /* Print on STREAM using the given OPTIONS the index for the element
1971 at INDEX of an array whose index type is INDEX_TYPE. */
1974 maybe_print_array_index (struct type
*index_type
, LONGEST index
,
1975 struct ui_file
*stream
,
1976 const struct value_print_options
*options
)
1978 struct value
*index_value
;
1980 if (!options
->print_array_indexes
)
1983 index_value
= value_from_longest (index_type
, index
);
1985 LA_PRINT_ARRAY_INDEX (index_value
, stream
, options
);
1988 /* Called by various <lang>_val_print routines to print elements of an
1989 array in the form "<elem1>, <elem2>, <elem3>, ...".
1991 (FIXME?) Assumes array element separator is a comma, which is correct
1992 for all languages currently handled.
1993 (FIXME?) Some languages have a notation for repeated array elements,
1994 perhaps we should try to use that notation when appropriate. */
1997 val_print_array_elements (struct type
*type
,
1998 LONGEST embedded_offset
,
1999 CORE_ADDR address
, struct ui_file
*stream
,
2002 const struct value_print_options
*options
,
2005 unsigned int things_printed
= 0;
2007 struct type
*elttype
, *index_type
, *base_index_type
;
2009 /* Position of the array element we are examining to see
2010 whether it is repeated. */
2012 /* Number of repetitions we have detected so far. */
2014 LONGEST low_bound
, high_bound
;
2015 LONGEST low_pos
, high_pos
;
2017 elttype
= TYPE_TARGET_TYPE (type
);
2018 eltlen
= type_length_units (check_typedef (elttype
));
2019 index_type
= TYPE_INDEX_TYPE (type
);
2021 if (get_array_bounds (type
, &low_bound
, &high_bound
))
2023 if (TYPE_CODE (index_type
) == TYPE_CODE_RANGE
)
2024 base_index_type
= TYPE_TARGET_TYPE (index_type
);
2026 base_index_type
= index_type
;
2028 /* Non-contiguous enumerations types can by used as index types
2029 in some languages (e.g. Ada). In this case, the array length
2030 shall be computed from the positions of the first and last
2031 literal in the enumeration type, and not from the values
2032 of these literals. */
2033 if (!discrete_position (base_index_type
, low_bound
, &low_pos
)
2034 || !discrete_position (base_index_type
, high_bound
, &high_pos
))
2036 warning (_("unable to get positions in array, use bounds instead"));
2037 low_pos
= low_bound
;
2038 high_pos
= high_bound
;
2041 /* The array length should normally be HIGH_POS - LOW_POS + 1.
2042 But we have to be a little extra careful, because some languages
2043 such as Ada allow LOW_POS to be greater than HIGH_POS for
2044 empty arrays. In that situation, the array length is just zero,
2046 if (low_pos
> high_pos
)
2049 len
= high_pos
- low_pos
+ 1;
2053 warning (_("unable to get bounds of array, assuming null array"));
2058 annotate_array_section_begin (i
, elttype
);
2060 for (; i
< len
&& things_printed
< options
->print_max
; i
++)
2064 if (options
->prettyformat_arrays
)
2066 fprintf_filtered (stream
, ",\n");
2067 print_spaces_filtered (2 + 2 * recurse
, stream
);
2071 fprintf_filtered (stream
, ", ");
2074 wrap_here (n_spaces (2 + 2 * recurse
));
2075 maybe_print_array_index (index_type
, i
+ low_bound
,
2080 /* Only check for reps if repeat_count_threshold is not set to
2081 UINT_MAX (unlimited). */
2082 if (options
->repeat_count_threshold
< UINT_MAX
)
2085 && value_contents_eq (val
,
2086 embedded_offset
+ i
* eltlen
,
2097 if (reps
> options
->repeat_count_threshold
)
2099 val_print (elttype
, embedded_offset
+ i
* eltlen
,
2100 address
, stream
, recurse
+ 1, val
, options
,
2102 annotate_elt_rep (reps
);
2103 fprintf_filtered (stream
, " <repeats %u times>", reps
);
2104 annotate_elt_rep_end ();
2107 things_printed
+= options
->repeat_count_threshold
;
2111 val_print (elttype
, embedded_offset
+ i
* eltlen
,
2113 stream
, recurse
+ 1, val
, options
, current_language
);
2118 annotate_array_section_end ();
2121 fprintf_filtered (stream
, "...");
2125 /* Read LEN bytes of target memory at address MEMADDR, placing the
2126 results in GDB's memory at MYADDR. Returns a count of the bytes
2127 actually read, and optionally a target_xfer_status value in the
2128 location pointed to by ERRPTR if ERRPTR is non-null. */
2130 /* FIXME: cagney/1999-10-14: Only used by val_print_string. Can this
2131 function be eliminated. */
2134 partial_memory_read (CORE_ADDR memaddr
, gdb_byte
*myaddr
,
2135 int len
, int *errptr
)
2137 int nread
; /* Number of bytes actually read. */
2138 int errcode
; /* Error from last read. */
2140 /* First try a complete read. */
2141 errcode
= target_read_memory (memaddr
, myaddr
, len
);
2149 /* Loop, reading one byte at a time until we get as much as we can. */
2150 for (errcode
= 0, nread
= 0; len
> 0 && errcode
== 0; nread
++, len
--)
2152 errcode
= target_read_memory (memaddr
++, myaddr
++, 1);
2154 /* If an error, the last read was unsuccessful, so adjust count. */
2167 /* Read a string from the inferior, at ADDR, with LEN characters of WIDTH bytes
2168 each. Fetch at most FETCHLIMIT characters. BUFFER will be set to a newly
2169 allocated buffer containing the string, which the caller is responsible to
2170 free, and BYTES_READ will be set to the number of bytes read. Returns 0 on
2171 success, or a target_xfer_status on failure.
2173 If LEN > 0, reads the lesser of LEN or FETCHLIMIT characters
2174 (including eventual NULs in the middle or end of the string).
2176 If LEN is -1, stops at the first null character (not necessarily
2177 the first null byte) up to a maximum of FETCHLIMIT characters. Set
2178 FETCHLIMIT to UINT_MAX to read as many characters as possible from
2181 Unless an exception is thrown, BUFFER will always be allocated, even on
2182 failure. In this case, some characters might have been read before the
2183 failure happened. Check BYTES_READ to recognize this situation.
2185 Note: There was a FIXME asking to make this code use target_read_string,
2186 but this function is more general (can read past null characters, up to
2187 given LEN). Besides, it is used much more often than target_read_string
2188 so it is more tested. Perhaps callers of target_read_string should use
2189 this function instead? */
2192 read_string (CORE_ADDR addr
, int len
, int width
, unsigned int fetchlimit
,
2193 enum bfd_endian byte_order
, gdb_byte
**buffer
, int *bytes_read
)
2195 int errcode
; /* Errno returned from bad reads. */
2196 unsigned int nfetch
; /* Chars to fetch / chars fetched. */
2197 gdb_byte
*bufptr
; /* Pointer to next available byte in
2199 struct cleanup
*old_chain
= NULL
; /* Top of the old cleanup chain. */
2201 /* Loop until we either have all the characters, or we encounter
2202 some error, such as bumping into the end of the address space. */
2206 old_chain
= make_cleanup (free_current_contents
, buffer
);
2210 /* We want fetchlimit chars, so we might as well read them all in
2212 unsigned int fetchlen
= std::min ((unsigned) len
, fetchlimit
);
2214 *buffer
= (gdb_byte
*) xmalloc (fetchlen
* width
);
2217 nfetch
= partial_memory_read (addr
, bufptr
, fetchlen
* width
, &errcode
)
2219 addr
+= nfetch
* width
;
2220 bufptr
+= nfetch
* width
;
2224 unsigned long bufsize
= 0;
2225 unsigned int chunksize
; /* Size of each fetch, in chars. */
2226 int found_nul
; /* Non-zero if we found the nul char. */
2227 gdb_byte
*limit
; /* First location past end of fetch buffer. */
2230 /* We are looking for a NUL terminator to end the fetching, so we
2231 might as well read in blocks that are large enough to be efficient,
2232 but not so large as to be slow if fetchlimit happens to be large.
2233 So we choose the minimum of 8 and fetchlimit. We used to use 200
2234 instead of 8 but 200 is way too big for remote debugging over a
2236 chunksize
= std::min (8u, fetchlimit
);
2241 nfetch
= std::min ((unsigned long) chunksize
, fetchlimit
- bufsize
);
2243 if (*buffer
== NULL
)
2244 *buffer
= (gdb_byte
*) xmalloc (nfetch
* width
);
2246 *buffer
= (gdb_byte
*) xrealloc (*buffer
,
2247 (nfetch
+ bufsize
) * width
);
2249 bufptr
= *buffer
+ bufsize
* width
;
2252 /* Read as much as we can. */
2253 nfetch
= partial_memory_read (addr
, bufptr
, nfetch
* width
, &errcode
)
2256 /* Scan this chunk for the null character that terminates the string
2257 to print. If found, we don't need to fetch any more. Note
2258 that bufptr is explicitly left pointing at the next character
2259 after the null character, or at the next character after the end
2262 limit
= bufptr
+ nfetch
* width
;
2263 while (bufptr
< limit
)
2267 c
= extract_unsigned_integer (bufptr
, width
, byte_order
);
2272 /* We don't care about any error which happened after
2273 the NUL terminator. */
2280 while (errcode
== 0 /* no error */
2281 && bufptr
- *buffer
< fetchlimit
* width
/* no overrun */
2282 && !found_nul
); /* haven't found NUL yet */
2285 { /* Length of string is really 0! */
2286 /* We always allocate *buffer. */
2287 *buffer
= bufptr
= (gdb_byte
*) xmalloc (1);
2291 /* bufptr and addr now point immediately beyond the last byte which we
2292 consider part of the string (including a '\0' which ends the string). */
2293 *bytes_read
= bufptr
- *buffer
;
2297 discard_cleanups (old_chain
);
2302 /* Return true if print_wchar can display W without resorting to a
2303 numeric escape, false otherwise. */
2306 wchar_printable (gdb_wchar_t w
)
2308 return (gdb_iswprint (w
)
2309 || w
== LCST ('\a') || w
== LCST ('\b')
2310 || w
== LCST ('\f') || w
== LCST ('\n')
2311 || w
== LCST ('\r') || w
== LCST ('\t')
2312 || w
== LCST ('\v'));
2315 /* A helper function that converts the contents of STRING to wide
2316 characters and then appends them to OUTPUT. */
2319 append_string_as_wide (const char *string
,
2320 struct obstack
*output
)
2322 for (; *string
; ++string
)
2324 gdb_wchar_t w
= gdb_btowc (*string
);
2325 obstack_grow (output
, &w
, sizeof (gdb_wchar_t
));
2329 /* Print a wide character W to OUTPUT. ORIG is a pointer to the
2330 original (target) bytes representing the character, ORIG_LEN is the
2331 number of valid bytes. WIDTH is the number of bytes in a base
2332 characters of the type. OUTPUT is an obstack to which wide
2333 characters are emitted. QUOTER is a (narrow) character indicating
2334 the style of quotes surrounding the character to be printed.
2335 NEED_ESCAPE is an in/out flag which is used to track numeric
2336 escapes across calls. */
2339 print_wchar (gdb_wint_t w
, const gdb_byte
*orig
,
2340 int orig_len
, int width
,
2341 enum bfd_endian byte_order
,
2342 struct obstack
*output
,
2343 int quoter
, int *need_escapep
)
2345 int need_escape
= *need_escapep
;
2349 /* iswprint implementation on Windows returns 1 for tab character.
2350 In order to avoid different printout on this host, we explicitly
2351 use wchar_printable function. */
2355 obstack_grow_wstr (output
, LCST ("\\a"));
2358 obstack_grow_wstr (output
, LCST ("\\b"));
2361 obstack_grow_wstr (output
, LCST ("\\f"));
2364 obstack_grow_wstr (output
, LCST ("\\n"));
2367 obstack_grow_wstr (output
, LCST ("\\r"));
2370 obstack_grow_wstr (output
, LCST ("\\t"));
2373 obstack_grow_wstr (output
, LCST ("\\v"));
2377 if (wchar_printable (w
) && (!need_escape
|| (!gdb_iswdigit (w
)
2379 && w
!= LCST ('9'))))
2381 gdb_wchar_t wchar
= w
;
2383 if (w
== gdb_btowc (quoter
) || w
== LCST ('\\'))
2384 obstack_grow_wstr (output
, LCST ("\\"));
2385 obstack_grow (output
, &wchar
, sizeof (gdb_wchar_t
));
2391 for (i
= 0; i
+ width
<= orig_len
; i
+= width
)
2396 value
= extract_unsigned_integer (&orig
[i
], width
,
2398 /* If the value fits in 3 octal digits, print it that
2399 way. Otherwise, print it as a hex escape. */
2401 xsnprintf (octal
, sizeof (octal
), "\\%.3o",
2402 (int) (value
& 0777));
2404 xsnprintf (octal
, sizeof (octal
), "\\x%lx", (long) value
);
2405 append_string_as_wide (octal
, output
);
2407 /* If we somehow have extra bytes, print them now. */
2408 while (i
< orig_len
)
2412 xsnprintf (octal
, sizeof (octal
), "\\%.3o", orig
[i
] & 0xff);
2413 append_string_as_wide (octal
, output
);
2424 /* Print the character C on STREAM as part of the contents of a
2425 literal string whose delimiter is QUOTER. ENCODING names the
2429 generic_emit_char (int c
, struct type
*type
, struct ui_file
*stream
,
2430 int quoter
, const char *encoding
)
2432 enum bfd_endian byte_order
2433 = gdbarch_byte_order (get_type_arch (type
));
2434 struct obstack wchar_buf
, output
;
2435 struct cleanup
*cleanups
;
2437 int need_escape
= 0;
2439 buf
= (gdb_byte
*) alloca (TYPE_LENGTH (type
));
2440 pack_long (buf
, type
, c
);
2442 wchar_iterator
iter (buf
, TYPE_LENGTH (type
), encoding
, TYPE_LENGTH (type
));
2444 /* This holds the printable form of the wchar_t data. */
2445 obstack_init (&wchar_buf
);
2446 cleanups
= make_cleanup_obstack_free (&wchar_buf
);
2452 const gdb_byte
*buf
;
2454 int print_escape
= 1;
2455 enum wchar_iterate_result result
;
2457 num_chars
= iter
.iterate (&result
, &chars
, &buf
, &buflen
);
2462 /* If all characters are printable, print them. Otherwise,
2463 we're going to have to print an escape sequence. We
2464 check all characters because we want to print the target
2465 bytes in the escape sequence, and we don't know character
2466 boundaries there. */
2470 for (i
= 0; i
< num_chars
; ++i
)
2471 if (!wchar_printable (chars
[i
]))
2479 for (i
= 0; i
< num_chars
; ++i
)
2480 print_wchar (chars
[i
], buf
, buflen
,
2481 TYPE_LENGTH (type
), byte_order
,
2482 &wchar_buf
, quoter
, &need_escape
);
2486 /* This handles the NUM_CHARS == 0 case as well. */
2488 print_wchar (gdb_WEOF
, buf
, buflen
, TYPE_LENGTH (type
),
2489 byte_order
, &wchar_buf
, quoter
, &need_escape
);
2492 /* The output in the host encoding. */
2493 obstack_init (&output
);
2494 make_cleanup_obstack_free (&output
);
2496 convert_between_encodings (INTERMEDIATE_ENCODING
, host_charset (),
2497 (gdb_byte
*) obstack_base (&wchar_buf
),
2498 obstack_object_size (&wchar_buf
),
2499 sizeof (gdb_wchar_t
), &output
, translit_char
);
2500 obstack_1grow (&output
, '\0');
2502 fputs_filtered ((const char *) obstack_base (&output
), stream
);
2504 do_cleanups (cleanups
);
2507 /* Return the repeat count of the next character/byte in ITER,
2508 storing the result in VEC. */
2511 count_next_character (wchar_iterator
*iter
,
2512 VEC (converted_character_d
) **vec
)
2514 struct converted_character
*current
;
2516 if (VEC_empty (converted_character_d
, *vec
))
2518 struct converted_character tmp
;
2522 = iter
->iterate (&tmp
.result
, &chars
, &tmp
.buf
, &tmp
.buflen
);
2523 if (tmp
.num_chars
> 0)
2525 gdb_assert (tmp
.num_chars
< MAX_WCHARS
);
2526 memcpy (tmp
.chars
, chars
, tmp
.num_chars
* sizeof (gdb_wchar_t
));
2528 VEC_safe_push (converted_character_d
, *vec
, &tmp
);
2531 current
= VEC_last (converted_character_d
, *vec
);
2533 /* Count repeated characters or bytes. */
2534 current
->repeat_count
= 1;
2535 if (current
->num_chars
== -1)
2543 struct converted_character d
;
2550 /* Get the next character. */
2551 d
.num_chars
= iter
->iterate (&d
.result
, &chars
, &d
.buf
, &d
.buflen
);
2553 /* If a character was successfully converted, save the character
2554 into the converted character. */
2555 if (d
.num_chars
> 0)
2557 gdb_assert (d
.num_chars
< MAX_WCHARS
);
2558 memcpy (d
.chars
, chars
, WCHAR_BUFLEN (d
.num_chars
));
2561 /* Determine if the current character is the same as this
2563 if (d
.num_chars
== current
->num_chars
&& d
.result
== current
->result
)
2565 /* There are two cases to consider:
2567 1) Equality of converted character (num_chars > 0)
2568 2) Equality of non-converted character (num_chars == 0) */
2569 if ((current
->num_chars
> 0
2570 && memcmp (current
->chars
, d
.chars
,
2571 WCHAR_BUFLEN (current
->num_chars
)) == 0)
2572 || (current
->num_chars
== 0
2573 && current
->buflen
== d
.buflen
2574 && memcmp (current
->buf
, d
.buf
, current
->buflen
) == 0))
2575 ++current
->repeat_count
;
2583 /* Push this next converted character onto the result vector. */
2584 repeat
= current
->repeat_count
;
2585 VEC_safe_push (converted_character_d
, *vec
, &d
);
2590 /* Print the characters in CHARS to the OBSTACK. QUOTE_CHAR is the quote
2591 character to use with string output. WIDTH is the size of the output
2592 character type. BYTE_ORDER is the the target byte order. OPTIONS
2593 is the user's print options. */
2596 print_converted_chars_to_obstack (struct obstack
*obstack
,
2597 VEC (converted_character_d
) *chars
,
2598 int quote_char
, int width
,
2599 enum bfd_endian byte_order
,
2600 const struct value_print_options
*options
)
2603 struct converted_character
*elem
;
2604 enum {START
, SINGLE
, REPEAT
, INCOMPLETE
, FINISH
} state
, last
;
2605 gdb_wchar_t wide_quote_char
= gdb_btowc (quote_char
);
2606 int need_escape
= 0;
2608 /* Set the start state. */
2610 last
= state
= START
;
2618 /* Nothing to do. */
2625 /* We are outputting a single character
2626 (< options->repeat_count_threshold). */
2630 /* We were outputting some other type of content, so we
2631 must output and a comma and a quote. */
2633 obstack_grow_wstr (obstack
, LCST (", "));
2634 obstack_grow (obstack
, &wide_quote_char
, sizeof (gdb_wchar_t
));
2636 /* Output the character. */
2637 for (j
= 0; j
< elem
->repeat_count
; ++j
)
2639 if (elem
->result
== wchar_iterate_ok
)
2640 print_wchar (elem
->chars
[0], elem
->buf
, elem
->buflen
, width
,
2641 byte_order
, obstack
, quote_char
, &need_escape
);
2643 print_wchar (gdb_WEOF
, elem
->buf
, elem
->buflen
, width
,
2644 byte_order
, obstack
, quote_char
, &need_escape
);
2654 /* We are outputting a character with a repeat count
2655 greater than options->repeat_count_threshold. */
2659 /* We were outputting a single string. Terminate the
2661 obstack_grow (obstack
, &wide_quote_char
, sizeof (gdb_wchar_t
));
2664 obstack_grow_wstr (obstack
, LCST (", "));
2666 /* Output the character and repeat string. */
2667 obstack_grow_wstr (obstack
, LCST ("'"));
2668 if (elem
->result
== wchar_iterate_ok
)
2669 print_wchar (elem
->chars
[0], elem
->buf
, elem
->buflen
, width
,
2670 byte_order
, obstack
, quote_char
, &need_escape
);
2672 print_wchar (gdb_WEOF
, elem
->buf
, elem
->buflen
, width
,
2673 byte_order
, obstack
, quote_char
, &need_escape
);
2674 obstack_grow_wstr (obstack
, LCST ("'"));
2675 s
= xstrprintf (_(" <repeats %u times>"), elem
->repeat_count
);
2676 for (j
= 0; s
[j
]; ++j
)
2678 gdb_wchar_t w
= gdb_btowc (s
[j
]);
2679 obstack_grow (obstack
, &w
, sizeof (gdb_wchar_t
));
2686 /* We are outputting an incomplete sequence. */
2689 /* If we were outputting a string of SINGLE characters,
2690 terminate the quote. */
2691 obstack_grow (obstack
, &wide_quote_char
, sizeof (gdb_wchar_t
));
2694 obstack_grow_wstr (obstack
, LCST (", "));
2696 /* Output the incomplete sequence string. */
2697 obstack_grow_wstr (obstack
, LCST ("<incomplete sequence "));
2698 print_wchar (gdb_WEOF
, elem
->buf
, elem
->buflen
, width
, byte_order
,
2699 obstack
, 0, &need_escape
);
2700 obstack_grow_wstr (obstack
, LCST (">"));
2702 /* We do not attempt to outupt anything after this. */
2707 /* All done. If we were outputting a string of SINGLE
2708 characters, the string must be terminated. Otherwise,
2709 REPEAT and INCOMPLETE are always left properly terminated. */
2711 obstack_grow (obstack
, &wide_quote_char
, sizeof (gdb_wchar_t
));
2716 /* Get the next element and state. */
2718 if (state
!= FINISH
)
2720 elem
= VEC_index (converted_character_d
, chars
, idx
++);
2721 switch (elem
->result
)
2723 case wchar_iterate_ok
:
2724 case wchar_iterate_invalid
:
2725 if (elem
->repeat_count
> options
->repeat_count_threshold
)
2731 case wchar_iterate_incomplete
:
2735 case wchar_iterate_eof
:
2743 /* Print the character string STRING, printing at most LENGTH
2744 characters. LENGTH is -1 if the string is nul terminated. TYPE is
2745 the type of each character. OPTIONS holds the printing options;
2746 printing stops early if the number hits print_max; repeat counts
2747 are printed as appropriate. Print ellipses at the end if we had to
2748 stop before printing LENGTH characters, or if FORCE_ELLIPSES.
2749 QUOTE_CHAR is the character to print at each end of the string. If
2750 C_STYLE_TERMINATOR is true, and the last character is 0, then it is
2754 generic_printstr (struct ui_file
*stream
, struct type
*type
,
2755 const gdb_byte
*string
, unsigned int length
,
2756 const char *encoding
, int force_ellipses
,
2757 int quote_char
, int c_style_terminator
,
2758 const struct value_print_options
*options
)
2760 enum bfd_endian byte_order
= gdbarch_byte_order (get_type_arch (type
));
2762 int width
= TYPE_LENGTH (type
);
2763 struct obstack wchar_buf
, output
;
2764 struct cleanup
*cleanup
;
2766 struct converted_character
*last
;
2767 VEC (converted_character_d
) *converted_chars
;
2771 unsigned long current_char
= 1;
2773 for (i
= 0; current_char
; ++i
)
2776 current_char
= extract_unsigned_integer (string
+ i
* width
,
2782 /* If the string was not truncated due to `set print elements', and
2783 the last byte of it is a null, we don't print that, in
2784 traditional C style. */
2785 if (c_style_terminator
2788 && (extract_unsigned_integer (string
+ (length
- 1) * width
,
2789 width
, byte_order
) == 0))
2794 fputs_filtered ("\"\"", stream
);
2798 /* Arrange to iterate over the characters, in wchar_t form. */
2799 wchar_iterator
iter (string
, length
* width
, encoding
, width
);
2800 converted_chars
= NULL
;
2801 cleanup
= make_cleanup (VEC_cleanup (converted_character_d
),
2804 /* Convert characters until the string is over or the maximum
2805 number of printed characters has been reached. */
2807 while (i
< options
->print_max
)
2813 /* Grab the next character and repeat count. */
2814 r
= count_next_character (&iter
, &converted_chars
);
2816 /* If less than zero, the end of the input string was reached. */
2820 /* Otherwise, add the count to the total print count and get
2821 the next character. */
2825 /* Get the last element and determine if the entire string was
2827 last
= VEC_last (converted_character_d
, converted_chars
);
2828 finished
= (last
->result
== wchar_iterate_eof
);
2830 /* Ensure that CONVERTED_CHARS is terminated. */
2831 last
->result
= wchar_iterate_eof
;
2833 /* WCHAR_BUF is the obstack we use to represent the string in
2835 obstack_init (&wchar_buf
);
2836 make_cleanup_obstack_free (&wchar_buf
);
2838 /* Print the output string to the obstack. */
2839 print_converted_chars_to_obstack (&wchar_buf
, converted_chars
, quote_char
,
2840 width
, byte_order
, options
);
2842 if (force_ellipses
|| !finished
)
2843 obstack_grow_wstr (&wchar_buf
, LCST ("..."));
2845 /* OUTPUT is where we collect `char's for printing. */
2846 obstack_init (&output
);
2847 make_cleanup_obstack_free (&output
);
2849 convert_between_encodings (INTERMEDIATE_ENCODING
, host_charset (),
2850 (gdb_byte
*) obstack_base (&wchar_buf
),
2851 obstack_object_size (&wchar_buf
),
2852 sizeof (gdb_wchar_t
), &output
, translit_char
);
2853 obstack_1grow (&output
, '\0');
2855 fputs_filtered ((const char *) obstack_base (&output
), stream
);
2857 do_cleanups (cleanup
);
2860 /* Print a string from the inferior, starting at ADDR and printing up to LEN
2861 characters, of WIDTH bytes a piece, to STREAM. If LEN is -1, printing
2862 stops at the first null byte, otherwise printing proceeds (including null
2863 bytes) until either print_max or LEN characters have been printed,
2864 whichever is smaller. ENCODING is the name of the string's
2865 encoding. It can be NULL, in which case the target encoding is
2869 val_print_string (struct type
*elttype
, const char *encoding
,
2870 CORE_ADDR addr
, int len
,
2871 struct ui_file
*stream
,
2872 const struct value_print_options
*options
)
2874 int force_ellipsis
= 0; /* Force ellipsis to be printed if nonzero. */
2875 int err
; /* Non-zero if we got a bad read. */
2876 int found_nul
; /* Non-zero if we found the nul char. */
2877 unsigned int fetchlimit
; /* Maximum number of chars to print. */
2879 gdb_byte
*buffer
= NULL
; /* Dynamically growable fetch buffer. */
2880 struct cleanup
*old_chain
= NULL
; /* Top of the old cleanup chain. */
2881 struct gdbarch
*gdbarch
= get_type_arch (elttype
);
2882 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
2883 int width
= TYPE_LENGTH (elttype
);
2885 /* First we need to figure out the limit on the number of characters we are
2886 going to attempt to fetch and print. This is actually pretty simple. If
2887 LEN >= zero, then the limit is the minimum of LEN and print_max. If
2888 LEN is -1, then the limit is print_max. This is true regardless of
2889 whether print_max is zero, UINT_MAX (unlimited), or something in between,
2890 because finding the null byte (or available memory) is what actually
2891 limits the fetch. */
2893 fetchlimit
= (len
== -1 ? options
->print_max
: std::min ((unsigned) len
,
2894 options
->print_max
));
2896 err
= read_string (addr
, len
, width
, fetchlimit
, byte_order
,
2897 &buffer
, &bytes_read
);
2898 old_chain
= make_cleanup (xfree
, buffer
);
2902 /* We now have either successfully filled the buffer to fetchlimit,
2903 or terminated early due to an error or finding a null char when
2906 /* Determine found_nul by looking at the last character read. */
2908 if (bytes_read
>= width
)
2909 found_nul
= extract_unsigned_integer (buffer
+ bytes_read
- width
, width
,
2911 if (len
== -1 && !found_nul
)
2915 /* We didn't find a NUL terminator we were looking for. Attempt
2916 to peek at the next character. If not successful, or it is not
2917 a null byte, then force ellipsis to be printed. */
2919 peekbuf
= (gdb_byte
*) alloca (width
);
2921 if (target_read_memory (addr
, peekbuf
, width
) == 0
2922 && extract_unsigned_integer (peekbuf
, width
, byte_order
) != 0)
2925 else if ((len
>= 0 && err
!= 0) || (len
> bytes_read
/ width
))
2927 /* Getting an error when we have a requested length, or fetching less
2928 than the number of characters actually requested, always make us
2933 /* If we get an error before fetching anything, don't print a string.
2934 But if we fetch something and then get an error, print the string
2935 and then the error message. */
2936 if (err
== 0 || bytes_read
> 0)
2938 LA_PRINT_STRING (stream
, elttype
, buffer
, bytes_read
/ width
,
2939 encoding
, force_ellipsis
, options
);
2946 str
= memory_error_message (TARGET_XFER_E_IO
, gdbarch
, addr
);
2947 make_cleanup (xfree
, str
);
2949 fprintf_filtered (stream
, "<error: ");
2950 fputs_filtered (str
, stream
);
2951 fprintf_filtered (stream
, ">");
2955 do_cleanups (old_chain
);
2957 return (bytes_read
/ width
);
2961 /* The 'set input-radix' command writes to this auxiliary variable.
2962 If the requested radix is valid, INPUT_RADIX is updated; otherwise,
2963 it is left unchanged. */
2965 static unsigned input_radix_1
= 10;
2967 /* Validate an input or output radix setting, and make sure the user
2968 knows what they really did here. Radix setting is confusing, e.g.
2969 setting the input radix to "10" never changes it! */
2972 set_input_radix (char *args
, int from_tty
, struct cmd_list_element
*c
)
2974 set_input_radix_1 (from_tty
, input_radix_1
);
2978 set_input_radix_1 (int from_tty
, unsigned radix
)
2980 /* We don't currently disallow any input radix except 0 or 1, which don't
2981 make any mathematical sense. In theory, we can deal with any input
2982 radix greater than 1, even if we don't have unique digits for every
2983 value from 0 to radix-1, but in practice we lose on large radix values.
2984 We should either fix the lossage or restrict the radix range more.
2989 input_radix_1
= input_radix
;
2990 error (_("Nonsense input radix ``decimal %u''; input radix unchanged."),
2993 input_radix_1
= input_radix
= radix
;
2996 printf_filtered (_("Input radix now set to "
2997 "decimal %u, hex %x, octal %o.\n"),
2998 radix
, radix
, radix
);
3002 /* The 'set output-radix' command writes to this auxiliary variable.
3003 If the requested radix is valid, OUTPUT_RADIX is updated,
3004 otherwise, it is left unchanged. */
3006 static unsigned output_radix_1
= 10;
3009 set_output_radix (char *args
, int from_tty
, struct cmd_list_element
*c
)
3011 set_output_radix_1 (from_tty
, output_radix_1
);
3015 set_output_radix_1 (int from_tty
, unsigned radix
)
3017 /* Validate the radix and disallow ones that we aren't prepared to
3018 handle correctly, leaving the radix unchanged. */
3022 user_print_options
.output_format
= 'x'; /* hex */
3025 user_print_options
.output_format
= 0; /* decimal */
3028 user_print_options
.output_format
= 'o'; /* octal */
3031 output_radix_1
= output_radix
;
3032 error (_("Unsupported output radix ``decimal %u''; "
3033 "output radix unchanged."),
3036 output_radix_1
= output_radix
= radix
;
3039 printf_filtered (_("Output radix now set to "
3040 "decimal %u, hex %x, octal %o.\n"),
3041 radix
, radix
, radix
);
3045 /* Set both the input and output radix at once. Try to set the output radix
3046 first, since it has the most restrictive range. An radix that is valid as
3047 an output radix is also valid as an input radix.
3049 It may be useful to have an unusual input radix. If the user wishes to
3050 set an input radix that is not valid as an output radix, he needs to use
3051 the 'set input-radix' command. */
3054 set_radix (char *arg
, int from_tty
)
3058 radix
= (arg
== NULL
) ? 10 : parse_and_eval_long (arg
);
3059 set_output_radix_1 (0, radix
);
3060 set_input_radix_1 (0, radix
);
3063 printf_filtered (_("Input and output radices now set to "
3064 "decimal %u, hex %x, octal %o.\n"),
3065 radix
, radix
, radix
);
3069 /* Show both the input and output radices. */
3072 show_radix (char *arg
, int from_tty
)
3076 if (input_radix
== output_radix
)
3078 printf_filtered (_("Input and output radices set to "
3079 "decimal %u, hex %x, octal %o.\n"),
3080 input_radix
, input_radix
, input_radix
);
3084 printf_filtered (_("Input radix set to decimal "
3085 "%u, hex %x, octal %o.\n"),
3086 input_radix
, input_radix
, input_radix
);
3087 printf_filtered (_("Output radix set to decimal "
3088 "%u, hex %x, octal %o.\n"),
3089 output_radix
, output_radix
, output_radix
);
3096 set_print (char *arg
, int from_tty
)
3099 "\"set print\" must be followed by the name of a print subcommand.\n");
3100 help_list (setprintlist
, "set print ", all_commands
, gdb_stdout
);
3104 show_print (char *args
, int from_tty
)
3106 cmd_show_list (showprintlist
, from_tty
, "");
3110 set_print_raw (char *arg
, int from_tty
)
3113 "\"set print raw\" must be followed by the name of a \"print raw\" subcommand.\n");
3114 help_list (setprintrawlist
, "set print raw ", all_commands
, gdb_stdout
);
3118 show_print_raw (char *args
, int from_tty
)
3120 cmd_show_list (showprintrawlist
, from_tty
, "");
3125 _initialize_valprint (void)
3127 add_prefix_cmd ("print", no_class
, set_print
,
3128 _("Generic command for setting how things print."),
3129 &setprintlist
, "set print ", 0, &setlist
);
3130 add_alias_cmd ("p", "print", no_class
, 1, &setlist
);
3131 /* Prefer set print to set prompt. */
3132 add_alias_cmd ("pr", "print", no_class
, 1, &setlist
);
3134 add_prefix_cmd ("print", no_class
, show_print
,
3135 _("Generic command for showing print settings."),
3136 &showprintlist
, "show print ", 0, &showlist
);
3137 add_alias_cmd ("p", "print", no_class
, 1, &showlist
);
3138 add_alias_cmd ("pr", "print", no_class
, 1, &showlist
);
3140 add_prefix_cmd ("raw", no_class
, set_print_raw
,
3142 Generic command for setting what things to print in \"raw\" mode."),
3143 &setprintrawlist
, "set print raw ", 0, &setprintlist
);
3144 add_prefix_cmd ("raw", no_class
, show_print_raw
,
3145 _("Generic command for showing \"print raw\" settings."),
3146 &showprintrawlist
, "show print raw ", 0, &showprintlist
);
3148 add_setshow_uinteger_cmd ("elements", no_class
,
3149 &user_print_options
.print_max
, _("\
3150 Set limit on string chars or array elements to print."), _("\
3151 Show limit on string chars or array elements to print."), _("\
3152 \"set print elements unlimited\" causes there to be no limit."),
3155 &setprintlist
, &showprintlist
);
3157 add_setshow_boolean_cmd ("null-stop", no_class
,
3158 &user_print_options
.stop_print_at_null
, _("\
3159 Set printing of char arrays to stop at first null char."), _("\
3160 Show printing of char arrays to stop at first null char."), NULL
,
3162 show_stop_print_at_null
,
3163 &setprintlist
, &showprintlist
);
3165 add_setshow_uinteger_cmd ("repeats", no_class
,
3166 &user_print_options
.repeat_count_threshold
, _("\
3167 Set threshold for repeated print elements."), _("\
3168 Show threshold for repeated print elements."), _("\
3169 \"set print repeats unlimited\" causes all elements to be individually printed."),
3171 show_repeat_count_threshold
,
3172 &setprintlist
, &showprintlist
);
3174 add_setshow_boolean_cmd ("pretty", class_support
,
3175 &user_print_options
.prettyformat_structs
, _("\
3176 Set pretty formatting of structures."), _("\
3177 Show pretty formatting of structures."), NULL
,
3179 show_prettyformat_structs
,
3180 &setprintlist
, &showprintlist
);
3182 add_setshow_boolean_cmd ("union", class_support
,
3183 &user_print_options
.unionprint
, _("\
3184 Set printing of unions interior to structures."), _("\
3185 Show printing of unions interior to structures."), NULL
,
3188 &setprintlist
, &showprintlist
);
3190 add_setshow_boolean_cmd ("array", class_support
,
3191 &user_print_options
.prettyformat_arrays
, _("\
3192 Set pretty formatting of arrays."), _("\
3193 Show pretty formatting of arrays."), NULL
,
3195 show_prettyformat_arrays
,
3196 &setprintlist
, &showprintlist
);
3198 add_setshow_boolean_cmd ("address", class_support
,
3199 &user_print_options
.addressprint
, _("\
3200 Set printing of addresses."), _("\
3201 Show printing of addresses."), NULL
,
3204 &setprintlist
, &showprintlist
);
3206 add_setshow_boolean_cmd ("symbol", class_support
,
3207 &user_print_options
.symbol_print
, _("\
3208 Set printing of symbol names when printing pointers."), _("\
3209 Show printing of symbol names when printing pointers."),
3212 &setprintlist
, &showprintlist
);
3214 add_setshow_zuinteger_cmd ("input-radix", class_support
, &input_radix_1
,
3216 Set default input radix for entering numbers."), _("\
3217 Show default input radix for entering numbers."), NULL
,
3220 &setlist
, &showlist
);
3222 add_setshow_zuinteger_cmd ("output-radix", class_support
, &output_radix_1
,
3224 Set default output radix for printing of values."), _("\
3225 Show default output radix for printing of values."), NULL
,
3228 &setlist
, &showlist
);
3230 /* The "set radix" and "show radix" commands are special in that
3231 they are like normal set and show commands but allow two normally
3232 independent variables to be either set or shown with a single
3233 command. So the usual deprecated_add_set_cmd() and [deleted]
3234 add_show_from_set() commands aren't really appropriate. */
3235 /* FIXME: i18n: With the new add_setshow_integer command, that is no
3236 longer true - show can display anything. */
3237 add_cmd ("radix", class_support
, set_radix
, _("\
3238 Set default input and output number radices.\n\
3239 Use 'set input-radix' or 'set output-radix' to independently set each.\n\
3240 Without an argument, sets both radices back to the default value of 10."),
3242 add_cmd ("radix", class_support
, show_radix
, _("\
3243 Show the default input and output number radices.\n\
3244 Use 'show input-radix' or 'show output-radix' to independently show each."),
3247 add_setshow_boolean_cmd ("array-indexes", class_support
,
3248 &user_print_options
.print_array_indexes
, _("\
3249 Set printing of array indexes."), _("\
3250 Show printing of array indexes"), NULL
, NULL
, show_print_array_indexes
,
3251 &setprintlist
, &showprintlist
);