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
, const gdb_byte
*valaddr
,
425 int embedded_offset
, CORE_ADDR address
,
426 struct ui_file
*stream
, int recurse
,
427 const 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
, valaddr
, 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
, const gdb_byte
*valaddr
,
466 int embedded_offset
, struct ui_file
*stream
,
467 const 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
, valaddr
, 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 CORE_ADDR addr
= unpack_pointer (type
,
483 valaddr
+ embedded_offset
* unit_size
);
485 print_unpacked_pointer (type
, elttype
, addr
, stream
, options
);
490 /* generic_val_print helper for TYPE_CODE_MEMBERPTR. */
493 generic_val_print_memberptr (struct type
*type
, const gdb_byte
*valaddr
,
494 int embedded_offset
, struct ui_file
*stream
,
495 const struct value
*original_value
,
496 const struct value_print_options
*options
)
498 val_print_scalar_formatted (type
, valaddr
, embedded_offset
,
499 original_value
, options
, 0, stream
);
502 /* Print '@' followed by the address contained in ADDRESS_BUFFER. */
505 print_ref_address (struct type
*type
, const gdb_byte
*address_buffer
,
506 int embedded_offset
, struct ui_file
*stream
)
508 struct gdbarch
*gdbarch
= get_type_arch (type
);
510 if (address_buffer
!= NULL
)
513 = extract_typed_address (address_buffer
+ embedded_offset
, type
);
515 fprintf_filtered (stream
, "@");
516 fputs_filtered (paddress (gdbarch
, address
), stream
);
518 /* Else: we have a non-addressable value, such as a DW_AT_const_value. */
521 /* If VAL is addressable, return the value contents buffer of a value that
522 represents a pointer to VAL. Otherwise return NULL. */
524 static const gdb_byte
*
525 get_value_addr_contents (struct value
*deref_val
)
527 gdb_assert (deref_val
!= NULL
);
529 if (value_lval_const (deref_val
) == lval_memory
)
530 return value_contents_for_printing_const (value_addr (deref_val
));
533 /* We have a non-addressable value, such as a DW_AT_const_value. */
538 /* generic_val_print helper for TYPE_CODE_REF. */
541 generic_val_print_ref (struct type
*type
, const gdb_byte
*valaddr
,
542 int embedded_offset
, struct ui_file
*stream
, int recurse
,
543 const struct value
*original_value
,
544 const struct value_print_options
*options
)
546 struct type
*elttype
= check_typedef (TYPE_TARGET_TYPE (type
));
547 struct value
*deref_val
= NULL
;
548 const int value_is_synthetic
549 = value_bits_synthetic_pointer (original_value
,
550 TARGET_CHAR_BIT
* embedded_offset
,
551 TARGET_CHAR_BIT
* TYPE_LENGTH (type
));
552 const int must_coerce_ref
= ((options
->addressprint
&& value_is_synthetic
)
553 || options
->deref_ref
);
554 const int type_is_defined
= TYPE_CODE (elttype
) != TYPE_CODE_UNDEF
;
556 if (must_coerce_ref
&& type_is_defined
)
558 deref_val
= coerce_ref_if_computed (original_value
);
560 if (deref_val
!= NULL
)
562 /* More complicated computed references are not supported. */
563 gdb_assert (embedded_offset
== 0);
566 deref_val
= value_at (TYPE_TARGET_TYPE (type
),
567 unpack_pointer (type
, valaddr
+ embedded_offset
));
569 /* Else, original_value isn't a synthetic reference or we don't have to print
570 the reference's contents.
572 Notice that for references to TYPE_CODE_STRUCT, 'set print object on' will
573 cause original_value to be a not_lval instead of an lval_computed,
574 which will make value_bits_synthetic_pointer return false.
575 This happens because if options->objectprint is true, c_value_print will
576 overwrite original_value's contents with the result of coercing
577 the reference through value_addr, and then set its type back to
578 TYPE_CODE_REF. In that case we don't have to coerce the reference again;
579 we can simply treat it as non-synthetic and move on. */
581 if (options
->addressprint
)
583 const gdb_byte
*address
= (value_is_synthetic
&& type_is_defined
584 ? get_value_addr_contents (deref_val
)
587 print_ref_address (type
, address
, embedded_offset
, stream
);
589 if (options
->deref_ref
)
590 fputs_filtered (": ", stream
);
593 if (options
->deref_ref
)
596 common_val_print (deref_val
, stream
, recurse
, options
,
599 fputs_filtered ("???", stream
);
603 /* Helper function for generic_val_print_enum.
604 This is also used to print enums in TYPE_CODE_FLAGS values. */
607 generic_val_print_enum_1 (struct type
*type
, LONGEST val
,
608 struct ui_file
*stream
)
613 len
= TYPE_NFIELDS (type
);
614 for (i
= 0; i
< len
; i
++)
617 if (val
== TYPE_FIELD_ENUMVAL (type
, i
))
624 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
626 else if (TYPE_FLAG_ENUM (type
))
630 /* We have a "flag" enum, so we try to decompose it into
631 pieces as appropriate. A flag enum has disjoint
632 constants by definition. */
633 fputs_filtered ("(", stream
);
634 for (i
= 0; i
< len
; ++i
)
638 if ((val
& TYPE_FIELD_ENUMVAL (type
, i
)) != 0)
641 fputs_filtered (" | ", stream
);
644 val
&= ~TYPE_FIELD_ENUMVAL (type
, i
);
645 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
649 if (first
|| val
!= 0)
652 fputs_filtered (" | ", stream
);
653 fputs_filtered ("unknown: ", stream
);
654 print_longest (stream
, 'd', 0, val
);
657 fputs_filtered (")", stream
);
660 print_longest (stream
, 'd', 0, val
);
663 /* generic_val_print helper for TYPE_CODE_ENUM. */
666 generic_val_print_enum (struct type
*type
, const gdb_byte
*valaddr
,
667 int embedded_offset
, struct ui_file
*stream
,
668 const struct value
*original_value
,
669 const struct value_print_options
*options
)
672 struct gdbarch
*gdbarch
= get_type_arch (type
);
673 int unit_size
= gdbarch_addressable_memory_unit_size (gdbarch
);
677 val_print_scalar_formatted (type
, valaddr
, embedded_offset
,
678 original_value
, options
, 0, stream
);
681 val
= unpack_long (type
, valaddr
+ embedded_offset
* unit_size
);
683 generic_val_print_enum_1 (type
, val
, stream
);
686 /* generic_val_print helper for TYPE_CODE_FLAGS. */
689 generic_val_print_flags (struct type
*type
, const gdb_byte
*valaddr
,
690 int embedded_offset
, struct ui_file
*stream
,
691 const struct value
*original_value
,
692 const struct value_print_options
*options
)
696 val_print_scalar_formatted (type
, valaddr
, embedded_offset
, original_value
,
699 val_print_type_code_flags (type
, valaddr
+ embedded_offset
, stream
);
702 /* generic_val_print helper for TYPE_CODE_FUNC and TYPE_CODE_METHOD. */
705 generic_val_print_func (struct type
*type
, const gdb_byte
*valaddr
,
706 int embedded_offset
, CORE_ADDR address
,
707 struct ui_file
*stream
,
708 const struct value
*original_value
,
709 const struct value_print_options
*options
)
711 struct gdbarch
*gdbarch
= get_type_arch (type
);
715 val_print_scalar_formatted (type
, valaddr
, embedded_offset
,
716 original_value
, options
, 0, stream
);
720 /* FIXME, we should consider, at least for ANSI C language,
721 eliminating the distinction made between FUNCs and POINTERs
723 fprintf_filtered (stream
, "{");
724 type_print (type
, "", stream
, -1);
725 fprintf_filtered (stream
, "} ");
726 /* Try to print what function it points to, and its address. */
727 print_address_demangle (options
, gdbarch
, address
, stream
, demangle
);
731 /* generic_val_print helper for TYPE_CODE_BOOL. */
734 generic_val_print_bool (struct type
*type
, const gdb_byte
*valaddr
,
735 int embedded_offset
, struct ui_file
*stream
,
736 const struct value
*original_value
,
737 const struct value_print_options
*options
,
738 const struct generic_val_print_decorations
*decorations
)
741 struct gdbarch
*gdbarch
= get_type_arch (type
);
742 int unit_size
= gdbarch_addressable_memory_unit_size (gdbarch
);
744 if (options
->format
|| options
->output_format
)
746 struct value_print_options opts
= *options
;
747 opts
.format
= (options
->format
? options
->format
748 : options
->output_format
);
749 val_print_scalar_formatted (type
, valaddr
, embedded_offset
,
750 original_value
, &opts
, 0, stream
);
754 val
= unpack_long (type
, valaddr
+ embedded_offset
* unit_size
);
756 fputs_filtered (decorations
->false_name
, stream
);
758 fputs_filtered (decorations
->true_name
, stream
);
760 print_longest (stream
, 'd', 0, val
);
764 /* generic_val_print helper for TYPE_CODE_INT. */
767 generic_val_print_int (struct type
*type
, const gdb_byte
*valaddr
,
768 int embedded_offset
, struct ui_file
*stream
,
769 const struct value
*original_value
,
770 const struct value_print_options
*options
)
772 struct gdbarch
*gdbarch
= get_type_arch (type
);
773 int unit_size
= gdbarch_addressable_memory_unit_size (gdbarch
);
775 if (options
->format
|| options
->output_format
)
777 struct value_print_options opts
= *options
;
779 opts
.format
= (options
->format
? options
->format
780 : options
->output_format
);
781 val_print_scalar_formatted (type
, valaddr
, embedded_offset
,
782 original_value
, &opts
, 0, stream
);
785 val_print_type_code_int (type
, valaddr
+ embedded_offset
* unit_size
,
789 /* generic_val_print helper for TYPE_CODE_CHAR. */
792 generic_val_print_char (struct type
*type
, struct type
*unresolved_type
,
793 const gdb_byte
*valaddr
, int embedded_offset
,
794 struct ui_file
*stream
,
795 const struct value
*original_value
,
796 const struct value_print_options
*options
)
799 struct gdbarch
*gdbarch
= get_type_arch (type
);
800 int unit_size
= gdbarch_addressable_memory_unit_size (gdbarch
);
802 if (options
->format
|| options
->output_format
)
804 struct value_print_options opts
= *options
;
806 opts
.format
= (options
->format
? options
->format
807 : options
->output_format
);
808 val_print_scalar_formatted (type
, valaddr
, embedded_offset
,
809 original_value
, &opts
, 0, stream
);
813 val
= unpack_long (type
, valaddr
+ embedded_offset
* unit_size
);
814 if (TYPE_UNSIGNED (type
))
815 fprintf_filtered (stream
, "%u", (unsigned int) val
);
817 fprintf_filtered (stream
, "%d", (int) val
);
818 fputs_filtered (" ", stream
);
819 LA_PRINT_CHAR (val
, unresolved_type
, stream
);
823 /* generic_val_print helper for TYPE_CODE_FLT. */
826 generic_val_print_float (struct type
*type
, const gdb_byte
*valaddr
,
827 int embedded_offset
, struct ui_file
*stream
,
828 const struct value
*original_value
,
829 const struct value_print_options
*options
)
831 struct gdbarch
*gdbarch
= get_type_arch (type
);
832 int unit_size
= gdbarch_addressable_memory_unit_size (gdbarch
);
836 val_print_scalar_formatted (type
, valaddr
, embedded_offset
,
837 original_value
, options
, 0, stream
);
841 print_floating (valaddr
+ embedded_offset
* unit_size
, type
, stream
);
845 /* generic_val_print helper for TYPE_CODE_DECFLOAT. */
848 generic_val_print_decfloat (struct type
*type
, const gdb_byte
*valaddr
,
849 int embedded_offset
, struct ui_file
*stream
,
850 const struct value
*original_value
,
851 const struct value_print_options
*options
)
853 struct gdbarch
*gdbarch
= get_type_arch (type
);
854 int unit_size
= gdbarch_addressable_memory_unit_size (gdbarch
);
857 val_print_scalar_formatted (type
, valaddr
, embedded_offset
, original_value
,
860 print_decimal_floating (valaddr
+ embedded_offset
* unit_size
, type
,
864 /* generic_val_print helper for TYPE_CODE_COMPLEX. */
867 generic_val_print_complex (struct type
*type
, const gdb_byte
*valaddr
,
868 int embedded_offset
, struct ui_file
*stream
,
869 const struct value
*original_value
,
870 const struct value_print_options
*options
,
871 const struct generic_val_print_decorations
874 struct gdbarch
*gdbarch
= get_type_arch (type
);
875 int unit_size
= gdbarch_addressable_memory_unit_size (gdbarch
);
877 fprintf_filtered (stream
, "%s", decorations
->complex_prefix
);
879 val_print_scalar_formatted (TYPE_TARGET_TYPE (type
), valaddr
,
880 embedded_offset
, original_value
, options
, 0,
883 print_floating (valaddr
+ embedded_offset
* unit_size
,
884 TYPE_TARGET_TYPE (type
), stream
);
885 fprintf_filtered (stream
, "%s", decorations
->complex_infix
);
887 val_print_scalar_formatted (TYPE_TARGET_TYPE (type
), valaddr
,
889 + type_length_units (TYPE_TARGET_TYPE (type
)),
890 original_value
, options
, 0, stream
);
892 print_floating (valaddr
+ embedded_offset
* unit_size
893 + TYPE_LENGTH (TYPE_TARGET_TYPE (type
)),
894 TYPE_TARGET_TYPE (type
), stream
);
895 fprintf_filtered (stream
, "%s", decorations
->complex_suffix
);
898 /* A generic val_print that is suitable for use by language
899 implementations of the la_val_print method. This function can
900 handle most type codes, though not all, notably exception
901 TYPE_CODE_UNION and TYPE_CODE_STRUCT, which must be implemented by
904 Most arguments are as to val_print.
906 The additional DECORATIONS argument can be used to customize the
907 output in some small, language-specific ways. */
910 generic_val_print (struct type
*type
, const gdb_byte
*valaddr
,
911 int embedded_offset
, CORE_ADDR address
,
912 struct ui_file
*stream
, int recurse
,
913 const struct value
*original_value
,
914 const struct value_print_options
*options
,
915 const struct generic_val_print_decorations
*decorations
)
917 struct type
*unresolved_type
= type
;
919 type
= check_typedef (type
);
920 switch (TYPE_CODE (type
))
922 case TYPE_CODE_ARRAY
:
923 generic_val_print_array (type
, valaddr
, embedded_offset
, address
, stream
,
924 recurse
, original_value
, options
, decorations
);
927 case TYPE_CODE_MEMBERPTR
:
928 generic_val_print_memberptr (type
, valaddr
, embedded_offset
, stream
,
929 original_value
, options
);
933 generic_val_print_ptr (type
, valaddr
, embedded_offset
, stream
,
934 original_value
, options
);
938 generic_val_print_ref (type
, valaddr
, embedded_offset
, stream
, recurse
,
939 original_value
, options
);
943 generic_val_print_enum (type
, valaddr
, embedded_offset
, stream
,
944 original_value
, options
);
947 case TYPE_CODE_FLAGS
:
948 generic_val_print_flags (type
, valaddr
, embedded_offset
, stream
,
949 original_value
, options
);
953 case TYPE_CODE_METHOD
:
954 generic_val_print_func (type
, valaddr
, embedded_offset
, address
, stream
,
955 original_value
, options
);
959 generic_val_print_bool (type
, valaddr
, embedded_offset
, stream
,
960 original_value
, options
, decorations
);
963 case TYPE_CODE_RANGE
:
964 /* FIXME: create_static_range_type does not set the unsigned bit in a
965 range type (I think it probably should copy it from the
966 target type), so we won't print values which are too large to
967 fit in a signed integer correctly. */
968 /* FIXME: Doesn't handle ranges of enums correctly. (Can't just
969 print with the target type, though, because the size of our
970 type and the target type might differ). */
975 generic_val_print_int (type
, valaddr
, embedded_offset
, stream
,
976 original_value
, options
);
980 generic_val_print_char (type
, unresolved_type
, valaddr
, embedded_offset
,
981 stream
, original_value
, options
);
985 generic_val_print_float (type
, valaddr
, embedded_offset
, stream
,
986 original_value
, options
);
989 case TYPE_CODE_DECFLOAT
:
990 generic_val_print_decfloat (type
, valaddr
, embedded_offset
, stream
,
991 original_value
, options
);
995 fputs_filtered (decorations
->void_name
, stream
);
998 case TYPE_CODE_ERROR
:
999 fprintf_filtered (stream
, "%s", TYPE_ERROR_NAME (type
));
1002 case TYPE_CODE_UNDEF
:
1003 /* This happens (without TYPE_STUB set) on systems which don't use
1004 dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
1005 and no complete type for struct foo in that file. */
1006 fprintf_filtered (stream
, _("<incomplete type>"));
1009 case TYPE_CODE_COMPLEX
:
1010 generic_val_print_complex (type
, valaddr
, embedded_offset
, stream
,
1011 original_value
, options
, decorations
);
1014 case TYPE_CODE_UNION
:
1015 case TYPE_CODE_STRUCT
:
1016 case TYPE_CODE_METHODPTR
:
1018 error (_("Unhandled type code %d in symbol table."),
1024 /* Print using the given LANGUAGE the data of type TYPE located at
1025 VALADDR + EMBEDDED_OFFSET (within GDB), which came from the
1026 inferior at address ADDRESS + EMBEDDED_OFFSET, onto stdio stream
1027 STREAM according to OPTIONS. VAL is the whole object that came
1028 from ADDRESS. VALADDR must point to the head of VAL's contents
1031 The language printers will pass down an adjusted EMBEDDED_OFFSET to
1032 further helper subroutines as subfields of TYPE are printed. In
1033 such cases, VALADDR is passed down unadjusted, as well as VAL, so
1034 that VAL can be queried for metadata about the contents data being
1035 printed, using EMBEDDED_OFFSET as an offset into VAL's contents
1036 buffer. For example: "has this field been optimized out", or "I'm
1037 printing an object while inspecting a traceframe; has this
1038 particular piece of data been collected?".
1040 RECURSE indicates the amount of indentation to supply before
1041 continuation lines; this amount is roughly twice the value of
1045 val_print (struct type
*type
, const gdb_byte
*valaddr
, LONGEST embedded_offset
,
1046 CORE_ADDR address
, struct ui_file
*stream
, int recurse
,
1047 const struct value
*val
,
1048 const struct value_print_options
*options
,
1049 const struct language_defn
*language
)
1052 struct value_print_options local_opts
= *options
;
1053 struct type
*real_type
= check_typedef (type
);
1055 if (local_opts
.prettyformat
== Val_prettyformat_default
)
1056 local_opts
.prettyformat
= (local_opts
.prettyformat_structs
1057 ? Val_prettyformat
: Val_no_prettyformat
);
1061 /* Ensure that the type is complete and not just a stub. If the type is
1062 only a stub and we can't find and substitute its complete type, then
1063 print appropriate string and return. */
1065 if (TYPE_STUB (real_type
))
1067 fprintf_filtered (stream
, _("<incomplete type>"));
1072 if (!valprint_check_validity (stream
, real_type
, embedded_offset
, val
))
1077 ret
= apply_ext_lang_val_pretty_printer (type
, valaddr
, embedded_offset
,
1078 address
, stream
, recurse
,
1079 val
, options
, language
);
1084 /* Handle summary mode. If the value is a scalar, print it;
1085 otherwise, print an ellipsis. */
1086 if (options
->summary
&& !val_print_scalar_type_p (type
))
1088 fprintf_filtered (stream
, "...");
1094 language
->la_val_print (type
, valaddr
, embedded_offset
, address
,
1095 stream
, recurse
, val
,
1098 CATCH (except
, RETURN_MASK_ERROR
)
1100 fprintf_filtered (stream
, _("<error reading variable>"));
1105 /* Check whether the value VAL is printable. Return 1 if it is;
1106 return 0 and print an appropriate error message to STREAM according to
1107 OPTIONS if it is not. */
1110 value_check_printable (struct value
*val
, struct ui_file
*stream
,
1111 const struct value_print_options
*options
)
1115 fprintf_filtered (stream
, _("<address of value unknown>"));
1119 if (value_entirely_optimized_out (val
))
1121 if (options
->summary
&& !val_print_scalar_type_p (value_type (val
)))
1122 fprintf_filtered (stream
, "...");
1124 val_print_optimized_out (val
, stream
);
1128 if (value_entirely_unavailable (val
))
1130 if (options
->summary
&& !val_print_scalar_type_p (value_type (val
)))
1131 fprintf_filtered (stream
, "...");
1133 val_print_unavailable (stream
);
1137 if (TYPE_CODE (value_type (val
)) == TYPE_CODE_INTERNAL_FUNCTION
)
1139 fprintf_filtered (stream
, _("<internal function %s>"),
1140 value_internal_function_name (val
));
1144 if (type_not_associated (value_type (val
)))
1146 val_print_not_associated (stream
);
1150 if (type_not_allocated (value_type (val
)))
1152 val_print_not_allocated (stream
);
1159 /* Print using the given LANGUAGE the value VAL onto stream STREAM according
1162 This is a preferable interface to val_print, above, because it uses
1163 GDB's value mechanism. */
1166 common_val_print (struct value
*val
, struct ui_file
*stream
, int recurse
,
1167 const struct value_print_options
*options
,
1168 const struct language_defn
*language
)
1170 if (!value_check_printable (val
, stream
, options
))
1173 if (language
->la_language
== language_ada
)
1174 /* The value might have a dynamic type, which would cause trouble
1175 below when trying to extract the value contents (since the value
1176 size is determined from the type size which is unknown). So
1177 get a fixed representation of our value. */
1178 val
= ada_to_fixed_value (val
);
1180 val_print (value_type (val
), value_contents_for_printing (val
),
1181 value_embedded_offset (val
), value_address (val
),
1183 val
, options
, language
);
1186 /* Print on stream STREAM the value VAL according to OPTIONS. The value
1187 is printed using the current_language syntax. */
1190 value_print (struct value
*val
, struct ui_file
*stream
,
1191 const struct value_print_options
*options
)
1193 if (!value_check_printable (val
, stream
, options
))
1199 = apply_ext_lang_val_pretty_printer (value_type (val
),
1200 value_contents_for_printing (val
),
1201 value_embedded_offset (val
),
1202 value_address (val
),
1204 val
, options
, current_language
);
1210 LA_VALUE_PRINT (val
, stream
, options
);
1213 /* Called by various <lang>_val_print routines to print
1214 TYPE_CODE_INT's. TYPE is the type. VALADDR is the address of the
1215 value. STREAM is where to print the value. */
1218 val_print_type_code_int (struct type
*type
, const gdb_byte
*valaddr
,
1219 struct ui_file
*stream
)
1221 enum bfd_endian byte_order
= gdbarch_byte_order (get_type_arch (type
));
1223 if (TYPE_LENGTH (type
) > sizeof (LONGEST
))
1227 if (TYPE_UNSIGNED (type
)
1228 && extract_long_unsigned_integer (valaddr
, TYPE_LENGTH (type
),
1231 print_longest (stream
, 'u', 0, val
);
1235 /* Signed, or we couldn't turn an unsigned value into a
1236 LONGEST. For signed values, one could assume two's
1237 complement (a reasonable assumption, I think) and do
1238 better than this. */
1239 print_hex_chars (stream
, (unsigned char *) valaddr
,
1240 TYPE_LENGTH (type
), byte_order
);
1245 print_longest (stream
, TYPE_UNSIGNED (type
) ? 'u' : 'd', 0,
1246 unpack_long (type
, valaddr
));
1251 val_print_type_code_flags (struct type
*type
, const gdb_byte
*valaddr
,
1252 struct ui_file
*stream
)
1254 ULONGEST val
= unpack_long (type
, valaddr
);
1255 int field
, nfields
= TYPE_NFIELDS (type
);
1256 struct gdbarch
*gdbarch
= get_type_arch (type
);
1257 struct type
*bool_type
= builtin_type (gdbarch
)->builtin_bool
;
1259 fputs_filtered ("[", stream
);
1260 for (field
= 0; field
< nfields
; field
++)
1262 if (TYPE_FIELD_NAME (type
, field
)[0] != '\0')
1264 struct type
*field_type
= TYPE_FIELD_TYPE (type
, field
);
1266 if (field_type
== bool_type
1267 /* We require boolean types here to be one bit wide. This is a
1268 problematic place to notify the user of an internal error
1269 though. Instead just fall through and print the field as an
1271 && TYPE_FIELD_BITSIZE (type
, field
) == 1)
1273 if (val
& ((ULONGEST
)1 << TYPE_FIELD_BITPOS (type
, field
)))
1274 fprintf_filtered (stream
, " %s",
1275 TYPE_FIELD_NAME (type
, field
));
1279 unsigned field_len
= TYPE_FIELD_BITSIZE (type
, field
);
1281 = val
>> (TYPE_FIELD_BITPOS (type
, field
) - field_len
+ 1);
1283 if (field_len
< sizeof (ULONGEST
) * TARGET_CHAR_BIT
)
1284 field_val
&= ((ULONGEST
) 1 << field_len
) - 1;
1285 fprintf_filtered (stream
, " %s=",
1286 TYPE_FIELD_NAME (type
, field
));
1287 if (TYPE_CODE (field_type
) == TYPE_CODE_ENUM
)
1288 generic_val_print_enum_1 (field_type
, field_val
, stream
);
1290 print_longest (stream
, 'd', 0, field_val
);
1294 fputs_filtered (" ]", stream
);
1297 /* Print a scalar of data of type TYPE, pointed to in GDB by VALADDR,
1298 according to OPTIONS and SIZE on STREAM. Format i is not supported
1301 This is how the elements of an array or structure are printed
1305 val_print_scalar_formatted (struct type
*type
,
1306 const gdb_byte
*valaddr
, LONGEST embedded_offset
,
1307 const struct value
*val
,
1308 const struct value_print_options
*options
,
1310 struct ui_file
*stream
)
1312 struct gdbarch
*arch
= get_type_arch (type
);
1313 int unit_size
= gdbarch_addressable_memory_unit_size (arch
);
1315 gdb_assert (val
!= NULL
);
1316 gdb_assert (valaddr
== value_contents_for_printing_const (val
));
1318 /* If we get here with a string format, try again without it. Go
1319 all the way back to the language printers, which may call us
1321 if (options
->format
== 's')
1323 struct value_print_options opts
= *options
;
1326 val_print (type
, valaddr
, embedded_offset
, 0, stream
, 0, val
, &opts
,
1331 /* A scalar object that does not have all bits available can't be
1332 printed, because all bits contribute to its representation. */
1333 if (value_bits_any_optimized_out (val
,
1334 TARGET_CHAR_BIT
* embedded_offset
,
1335 TARGET_CHAR_BIT
* TYPE_LENGTH (type
)))
1336 val_print_optimized_out (val
, stream
);
1337 else if (!value_bytes_available (val
, embedded_offset
, TYPE_LENGTH (type
)))
1338 val_print_unavailable (stream
);
1340 print_scalar_formatted (valaddr
+ embedded_offset
* unit_size
, type
,
1341 options
, size
, stream
);
1344 /* Print a number according to FORMAT which is one of d,u,x,o,b,h,w,g.
1345 The raison d'etre of this function is to consolidate printing of
1346 LONG_LONG's into this one function. The format chars b,h,w,g are
1347 from print_scalar_formatted(). Numbers are printed using C
1350 USE_C_FORMAT means to use C format in all cases. Without it,
1351 'o' and 'x' format do not include the standard C radix prefix
1354 Hilfinger/2004-09-09: USE_C_FORMAT was originally called USE_LOCAL
1355 and was intended to request formating according to the current
1356 language and would be used for most integers that GDB prints. The
1357 exceptional cases were things like protocols where the format of
1358 the integer is a protocol thing, not a user-visible thing). The
1359 parameter remains to preserve the information of what things might
1360 be printed with language-specific format, should we ever resurrect
1364 print_longest (struct ui_file
*stream
, int format
, int use_c_format
,
1372 val
= int_string (val_long
, 10, 1, 0, 1); break;
1374 val
= int_string (val_long
, 10, 0, 0, 1); break;
1376 val
= int_string (val_long
, 16, 0, 0, use_c_format
); break;
1378 val
= int_string (val_long
, 16, 0, 2, 1); break;
1380 val
= int_string (val_long
, 16, 0, 4, 1); break;
1382 val
= int_string (val_long
, 16, 0, 8, 1); break;
1384 val
= int_string (val_long
, 16, 0, 16, 1); break;
1387 val
= int_string (val_long
, 8, 0, 0, use_c_format
); break;
1389 internal_error (__FILE__
, __LINE__
,
1390 _("failed internal consistency check"));
1392 fputs_filtered (val
, stream
);
1395 /* This used to be a macro, but I don't think it is called often enough
1396 to merit such treatment. */
1397 /* Convert a LONGEST to an int. This is used in contexts (e.g. number of
1398 arguments to a function, number in a value history, register number, etc.)
1399 where the value must not be larger than can fit in an int. */
1402 longest_to_int (LONGEST arg
)
1404 /* Let the compiler do the work. */
1405 int rtnval
= (int) arg
;
1407 /* Check for overflows or underflows. */
1408 if (sizeof (LONGEST
) > sizeof (int))
1412 error (_("Value out of range."));
1418 /* Print a floating point value of type TYPE (not always a
1419 TYPE_CODE_FLT), pointed to in GDB by VALADDR, on STREAM. */
1422 print_floating (const gdb_byte
*valaddr
, struct type
*type
,
1423 struct ui_file
*stream
)
1427 const struct floatformat
*fmt
= NULL
;
1428 unsigned len
= TYPE_LENGTH (type
);
1429 enum float_kind kind
;
1431 /* If it is a floating-point, check for obvious problems. */
1432 if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
1433 fmt
= floatformat_from_type (type
);
1436 kind
= floatformat_classify (fmt
, valaddr
);
1437 if (kind
== float_nan
)
1439 if (floatformat_is_negative (fmt
, valaddr
))
1440 fprintf_filtered (stream
, "-");
1441 fprintf_filtered (stream
, "nan(");
1442 fputs_filtered ("0x", stream
);
1443 fputs_filtered (floatformat_mantissa (fmt
, valaddr
), stream
);
1444 fprintf_filtered (stream
, ")");
1447 else if (kind
== float_infinite
)
1449 if (floatformat_is_negative (fmt
, valaddr
))
1450 fputs_filtered ("-", stream
);
1451 fputs_filtered ("inf", stream
);
1456 /* NOTE: cagney/2002-01-15: The TYPE passed into print_floating()
1457 isn't necessarily a TYPE_CODE_FLT. Consequently, unpack_double
1458 needs to be used as that takes care of any necessary type
1459 conversions. Such conversions are of course direct to DOUBLEST
1460 and disregard any possible target floating point limitations.
1461 For instance, a u64 would be converted and displayed exactly on a
1462 host with 80 bit DOUBLEST but with loss of information on a host
1463 with 64 bit DOUBLEST. */
1465 doub
= unpack_double (type
, valaddr
, &inv
);
1468 fprintf_filtered (stream
, "<invalid float value>");
1472 /* FIXME: kettenis/2001-01-20: The following code makes too much
1473 assumptions about the host and target floating point format. */
1475 /* NOTE: cagney/2002-02-03: Since the TYPE of what was passed in may
1476 not necessarily be a TYPE_CODE_FLT, the below ignores that and
1477 instead uses the type's length to determine the precision of the
1478 floating-point value being printed. */
1480 if (len
< sizeof (double))
1481 fprintf_filtered (stream
, "%.9g", (double) doub
);
1482 else if (len
== sizeof (double))
1483 fprintf_filtered (stream
, "%.17g", (double) doub
);
1485 #ifdef PRINTF_HAS_LONG_DOUBLE
1486 fprintf_filtered (stream
, "%.35Lg", doub
);
1488 /* This at least wins with values that are representable as
1490 fprintf_filtered (stream
, "%.17g", (double) doub
);
1495 print_decimal_floating (const gdb_byte
*valaddr
, struct type
*type
,
1496 struct ui_file
*stream
)
1498 enum bfd_endian byte_order
= gdbarch_byte_order (get_type_arch (type
));
1499 char decstr
[MAX_DECIMAL_STRING
];
1500 unsigned len
= TYPE_LENGTH (type
);
1502 decimal_to_string (valaddr
, len
, byte_order
, decstr
);
1503 fputs_filtered (decstr
, stream
);
1508 print_binary_chars (struct ui_file
*stream
, const gdb_byte
*valaddr
,
1509 unsigned len
, enum bfd_endian byte_order
)
1512 #define BITS_IN_BYTES 8
1518 /* Declared "int" so it will be signed.
1519 This ensures that right shift will shift in zeros. */
1521 const int mask
= 0x080;
1523 /* FIXME: We should be not printing leading zeroes in most cases. */
1525 if (byte_order
== BFD_ENDIAN_BIG
)
1531 /* Every byte has 8 binary characters; peel off
1532 and print from the MSB end. */
1534 for (i
= 0; i
< (BITS_IN_BYTES
* sizeof (*p
)); i
++)
1536 if (*p
& (mask
>> i
))
1541 fprintf_filtered (stream
, "%1d", b
);
1547 for (p
= valaddr
+ len
- 1;
1551 for (i
= 0; i
< (BITS_IN_BYTES
* sizeof (*p
)); i
++)
1553 if (*p
& (mask
>> i
))
1558 fprintf_filtered (stream
, "%1d", b
);
1564 /* VALADDR points to an integer of LEN bytes.
1565 Print it in octal on stream or format it in buf. */
1568 print_octal_chars (struct ui_file
*stream
, const gdb_byte
*valaddr
,
1569 unsigned len
, enum bfd_endian byte_order
)
1572 unsigned char octa1
, octa2
, octa3
, carry
;
1575 /* FIXME: We should be not printing leading zeroes in most cases. */
1578 /* Octal is 3 bits, which doesn't fit. Yuk. So we have to track
1579 * the extra bits, which cycle every three bytes:
1581 * Byte side: 0 1 2 3
1583 * bit number 123 456 78 | 9 012 345 6 | 78 901 234 | 567 890 12 |
1585 * Octal side: 0 1 carry 3 4 carry ...
1587 * Cycle number: 0 1 2
1589 * But of course we are printing from the high side, so we have to
1590 * figure out where in the cycle we are so that we end up with no
1591 * left over bits at the end.
1593 #define BITS_IN_OCTAL 3
1594 #define HIGH_ZERO 0340
1595 #define LOW_ZERO 0016
1596 #define CARRY_ZERO 0003
1597 #define HIGH_ONE 0200
1598 #define MID_ONE 0160
1599 #define LOW_ONE 0016
1600 #define CARRY_ONE 0001
1601 #define HIGH_TWO 0300
1602 #define MID_TWO 0070
1603 #define LOW_TWO 0007
1605 /* For 32 we start in cycle 2, with two bits and one bit carry;
1606 for 64 in cycle in cycle 1, with one bit and a two bit carry. */
1608 cycle
= (len
* BITS_IN_BYTES
) % BITS_IN_OCTAL
;
1611 fputs_filtered ("0", stream
);
1612 if (byte_order
== BFD_ENDIAN_BIG
)
1621 /* No carry in, carry out two bits. */
1623 octa1
= (HIGH_ZERO
& *p
) >> 5;
1624 octa2
= (LOW_ZERO
& *p
) >> 2;
1625 carry
= (CARRY_ZERO
& *p
);
1626 fprintf_filtered (stream
, "%o", octa1
);
1627 fprintf_filtered (stream
, "%o", octa2
);
1631 /* Carry in two bits, carry out one bit. */
1633 octa1
= (carry
<< 1) | ((HIGH_ONE
& *p
) >> 7);
1634 octa2
= (MID_ONE
& *p
) >> 4;
1635 octa3
= (LOW_ONE
& *p
) >> 1;
1636 carry
= (CARRY_ONE
& *p
);
1637 fprintf_filtered (stream
, "%o", octa1
);
1638 fprintf_filtered (stream
, "%o", octa2
);
1639 fprintf_filtered (stream
, "%o", octa3
);
1643 /* Carry in one bit, no carry out. */
1645 octa1
= (carry
<< 2) | ((HIGH_TWO
& *p
) >> 6);
1646 octa2
= (MID_TWO
& *p
) >> 3;
1647 octa3
= (LOW_TWO
& *p
);
1649 fprintf_filtered (stream
, "%o", octa1
);
1650 fprintf_filtered (stream
, "%o", octa2
);
1651 fprintf_filtered (stream
, "%o", octa3
);
1655 error (_("Internal error in octal conversion;"));
1659 cycle
= cycle
% BITS_IN_OCTAL
;
1664 for (p
= valaddr
+ len
- 1;
1671 /* Carry out, no carry in */
1673 octa1
= (HIGH_ZERO
& *p
) >> 5;
1674 octa2
= (LOW_ZERO
& *p
) >> 2;
1675 carry
= (CARRY_ZERO
& *p
);
1676 fprintf_filtered (stream
, "%o", octa1
);
1677 fprintf_filtered (stream
, "%o", octa2
);
1681 /* Carry in, carry out */
1683 octa1
= (carry
<< 1) | ((HIGH_ONE
& *p
) >> 7);
1684 octa2
= (MID_ONE
& *p
) >> 4;
1685 octa3
= (LOW_ONE
& *p
) >> 1;
1686 carry
= (CARRY_ONE
& *p
);
1687 fprintf_filtered (stream
, "%o", octa1
);
1688 fprintf_filtered (stream
, "%o", octa2
);
1689 fprintf_filtered (stream
, "%o", octa3
);
1693 /* Carry in, no carry out */
1695 octa1
= (carry
<< 2) | ((HIGH_TWO
& *p
) >> 6);
1696 octa2
= (MID_TWO
& *p
) >> 3;
1697 octa3
= (LOW_TWO
& *p
);
1699 fprintf_filtered (stream
, "%o", octa1
);
1700 fprintf_filtered (stream
, "%o", octa2
);
1701 fprintf_filtered (stream
, "%o", octa3
);
1705 error (_("Internal error in octal conversion;"));
1709 cycle
= cycle
% BITS_IN_OCTAL
;
1715 /* VALADDR points to an integer of LEN bytes.
1716 Print it in decimal on stream or format it in buf. */
1719 print_decimal_chars (struct ui_file
*stream
, const gdb_byte
*valaddr
,
1720 unsigned len
, enum bfd_endian byte_order
)
1723 #define CARRY_OUT( x ) ((x) / TEN) /* extend char to int */
1724 #define CARRY_LEFT( x ) ((x) % TEN)
1725 #define SHIFT( x ) ((x) << 4)
1726 #define LOW_NIBBLE( x ) ( (x) & 0x00F)
1727 #define HIGH_NIBBLE( x ) (((x) & 0x0F0) >> 4)
1730 unsigned char *digits
;
1733 int i
, j
, decimal_digits
;
1737 /* Base-ten number is less than twice as many digits
1738 as the base 16 number, which is 2 digits per byte. */
1740 decimal_len
= len
* 2 * 2;
1741 digits
= (unsigned char *) xmalloc (decimal_len
);
1743 for (i
= 0; i
< decimal_len
; i
++)
1748 /* Ok, we have an unknown number of bytes of data to be printed in
1751 * Given a hex number (in nibbles) as XYZ, we start by taking X and
1752 * decemalizing it as "x1 x2" in two decimal nibbles. Then we multiply
1753 * the nibbles by 16, add Y and re-decimalize. Repeat with Z.
1755 * The trick is that "digits" holds a base-10 number, but sometimes
1756 * the individual digits are > 10.
1758 * Outer loop is per nibble (hex digit) of input, from MSD end to
1761 decimal_digits
= 0; /* Number of decimal digits so far */
1762 p
= (byte_order
== BFD_ENDIAN_BIG
) ? valaddr
: valaddr
+ len
- 1;
1764 while ((byte_order
== BFD_ENDIAN_BIG
) ? (p
< valaddr
+ len
) : (p
>= valaddr
))
1767 * Multiply current base-ten number by 16 in place.
1768 * Each digit was between 0 and 9, now is between
1771 for (j
= 0; j
< decimal_digits
; j
++)
1773 digits
[j
] = SHIFT (digits
[j
]);
1776 /* Take the next nibble off the input and add it to what
1777 * we've got in the LSB position. Bottom 'digit' is now
1778 * between 0 and 159.
1780 * "flip" is used to run this loop twice for each byte.
1784 /* Take top nibble. */
1786 digits
[0] += HIGH_NIBBLE (*p
);
1791 /* Take low nibble and bump our pointer "p". */
1793 digits
[0] += LOW_NIBBLE (*p
);
1794 if (byte_order
== BFD_ENDIAN_BIG
)
1801 /* Re-decimalize. We have to do this often enough
1802 * that we don't overflow, but once per nibble is
1803 * overkill. Easier this way, though. Note that the
1804 * carry is often larger than 10 (e.g. max initial
1805 * carry out of lowest nibble is 15, could bubble all
1806 * the way up greater than 10). So we have to do
1807 * the carrying beyond the last current digit.
1810 for (j
= 0; j
< decimal_len
- 1; j
++)
1814 /* "/" won't handle an unsigned char with
1815 * a value that if signed would be negative.
1816 * So extend to longword int via "dummy".
1819 carry
= CARRY_OUT (dummy
);
1820 digits
[j
] = CARRY_LEFT (dummy
);
1822 if (j
>= decimal_digits
&& carry
== 0)
1825 * All higher digits are 0 and we
1826 * no longer have a carry.
1828 * Note: "j" is 0-based, "decimal_digits" is
1831 decimal_digits
= j
+ 1;
1837 /* Ok, now "digits" is the decimal representation, with
1838 the "decimal_digits" actual digits. Print! */
1840 for (i
= decimal_digits
- 1; i
>= 0; i
--)
1842 fprintf_filtered (stream
, "%1d", digits
[i
]);
1847 /* VALADDR points to an integer of LEN bytes. Print it in hex on stream. */
1850 print_hex_chars (struct ui_file
*stream
, const gdb_byte
*valaddr
,
1851 unsigned len
, enum bfd_endian byte_order
)
1855 /* FIXME: We should be not printing leading zeroes in most cases. */
1857 fputs_filtered ("0x", stream
);
1858 if (byte_order
== BFD_ENDIAN_BIG
)
1864 fprintf_filtered (stream
, "%02x", *p
);
1869 for (p
= valaddr
+ len
- 1;
1873 fprintf_filtered (stream
, "%02x", *p
);
1878 /* VALADDR points to a char integer of LEN bytes.
1879 Print it out in appropriate language form on stream.
1880 Omit any leading zero chars. */
1883 print_char_chars (struct ui_file
*stream
, struct type
*type
,
1884 const gdb_byte
*valaddr
,
1885 unsigned len
, enum bfd_endian byte_order
)
1889 if (byte_order
== BFD_ENDIAN_BIG
)
1892 while (p
< valaddr
+ len
- 1 && *p
== 0)
1895 while (p
< valaddr
+ len
)
1897 LA_EMIT_CHAR (*p
, type
, stream
, '\'');
1903 p
= valaddr
+ len
- 1;
1904 while (p
> valaddr
&& *p
== 0)
1907 while (p
>= valaddr
)
1909 LA_EMIT_CHAR (*p
, type
, stream
, '\'');
1915 /* Print function pointer with inferior address ADDRESS onto stdio
1919 print_function_pointer_address (const struct value_print_options
*options
,
1920 struct gdbarch
*gdbarch
,
1922 struct ui_file
*stream
)
1925 = gdbarch_convert_from_func_ptr_addr (gdbarch
, address
,
1928 /* If the function pointer is represented by a description, print
1929 the address of the description. */
1930 if (options
->addressprint
&& func_addr
!= address
)
1932 fputs_filtered ("@", stream
);
1933 fputs_filtered (paddress (gdbarch
, address
), stream
);
1934 fputs_filtered (": ", stream
);
1936 print_address_demangle (options
, gdbarch
, func_addr
, stream
, demangle
);
1940 /* Print on STREAM using the given OPTIONS the index for the element
1941 at INDEX of an array whose index type is INDEX_TYPE. */
1944 maybe_print_array_index (struct type
*index_type
, LONGEST index
,
1945 struct ui_file
*stream
,
1946 const struct value_print_options
*options
)
1948 struct value
*index_value
;
1950 if (!options
->print_array_indexes
)
1953 index_value
= value_from_longest (index_type
, index
);
1955 LA_PRINT_ARRAY_INDEX (index_value
, stream
, options
);
1958 /* Called by various <lang>_val_print routines to print elements of an
1959 array in the form "<elem1>, <elem2>, <elem3>, ...".
1961 (FIXME?) Assumes array element separator is a comma, which is correct
1962 for all languages currently handled.
1963 (FIXME?) Some languages have a notation for repeated array elements,
1964 perhaps we should try to use that notation when appropriate. */
1967 val_print_array_elements (struct type
*type
,
1968 const gdb_byte
*valaddr
, LONGEST embedded_offset
,
1969 CORE_ADDR address
, struct ui_file
*stream
,
1971 const struct value
*val
,
1972 const struct value_print_options
*options
,
1975 unsigned int things_printed
= 0;
1977 struct type
*elttype
, *index_type
, *base_index_type
;
1979 /* Position of the array element we are examining to see
1980 whether it is repeated. */
1982 /* Number of repetitions we have detected so far. */
1984 LONGEST low_bound
, high_bound
;
1985 LONGEST low_pos
, high_pos
;
1987 elttype
= TYPE_TARGET_TYPE (type
);
1988 eltlen
= type_length_units (check_typedef (elttype
));
1989 index_type
= TYPE_INDEX_TYPE (type
);
1991 if (get_array_bounds (type
, &low_bound
, &high_bound
))
1993 if (TYPE_CODE (index_type
) == TYPE_CODE_RANGE
)
1994 base_index_type
= TYPE_TARGET_TYPE (index_type
);
1996 base_index_type
= index_type
;
1998 /* Non-contiguous enumerations types can by used as index types
1999 in some languages (e.g. Ada). In this case, the array length
2000 shall be computed from the positions of the first and last
2001 literal in the enumeration type, and not from the values
2002 of these literals. */
2003 if (!discrete_position (base_index_type
, low_bound
, &low_pos
)
2004 || !discrete_position (base_index_type
, high_bound
, &high_pos
))
2006 warning (_("unable to get positions in array, use bounds instead"));
2007 low_pos
= low_bound
;
2008 high_pos
= high_bound
;
2011 /* The array length should normally be HIGH_POS - LOW_POS + 1.
2012 But we have to be a little extra careful, because some languages
2013 such as Ada allow LOW_POS to be greater than HIGH_POS for
2014 empty arrays. In that situation, the array length is just zero,
2016 if (low_pos
> high_pos
)
2019 len
= high_pos
- low_pos
+ 1;
2023 warning (_("unable to get bounds of array, assuming null array"));
2028 annotate_array_section_begin (i
, elttype
);
2030 for (; i
< len
&& things_printed
< options
->print_max
; i
++)
2034 if (options
->prettyformat_arrays
)
2036 fprintf_filtered (stream
, ",\n");
2037 print_spaces_filtered (2 + 2 * recurse
, stream
);
2041 fprintf_filtered (stream
, ", ");
2044 wrap_here (n_spaces (2 + 2 * recurse
));
2045 maybe_print_array_index (index_type
, i
+ low_bound
,
2050 /* Only check for reps if repeat_count_threshold is not set to
2051 UINT_MAX (unlimited). */
2052 if (options
->repeat_count_threshold
< UINT_MAX
)
2055 && value_contents_eq (val
,
2056 embedded_offset
+ i
* eltlen
,
2067 if (reps
> options
->repeat_count_threshold
)
2069 val_print (elttype
, valaddr
, embedded_offset
+ i
* eltlen
,
2070 address
, stream
, recurse
+ 1, val
, options
,
2072 annotate_elt_rep (reps
);
2073 fprintf_filtered (stream
, " <repeats %u times>", reps
);
2074 annotate_elt_rep_end ();
2077 things_printed
+= options
->repeat_count_threshold
;
2081 val_print (elttype
, valaddr
, embedded_offset
+ i
* eltlen
,
2083 stream
, recurse
+ 1, val
, options
, current_language
);
2088 annotate_array_section_end ();
2091 fprintf_filtered (stream
, "...");
2095 /* Read LEN bytes of target memory at address MEMADDR, placing the
2096 results in GDB's memory at MYADDR. Returns a count of the bytes
2097 actually read, and optionally a target_xfer_status value in the
2098 location pointed to by ERRPTR if ERRPTR is non-null. */
2100 /* FIXME: cagney/1999-10-14: Only used by val_print_string. Can this
2101 function be eliminated. */
2104 partial_memory_read (CORE_ADDR memaddr
, gdb_byte
*myaddr
,
2105 int len
, int *errptr
)
2107 int nread
; /* Number of bytes actually read. */
2108 int errcode
; /* Error from last read. */
2110 /* First try a complete read. */
2111 errcode
= target_read_memory (memaddr
, myaddr
, len
);
2119 /* Loop, reading one byte at a time until we get as much as we can. */
2120 for (errcode
= 0, nread
= 0; len
> 0 && errcode
== 0; nread
++, len
--)
2122 errcode
= target_read_memory (memaddr
++, myaddr
++, 1);
2124 /* If an error, the last read was unsuccessful, so adjust count. */
2137 /* Read a string from the inferior, at ADDR, with LEN characters of WIDTH bytes
2138 each. Fetch at most FETCHLIMIT characters. BUFFER will be set to a newly
2139 allocated buffer containing the string, which the caller is responsible to
2140 free, and BYTES_READ will be set to the number of bytes read. Returns 0 on
2141 success, or a target_xfer_status on failure.
2143 If LEN > 0, reads the lesser of LEN or FETCHLIMIT characters
2144 (including eventual NULs in the middle or end of the string).
2146 If LEN is -1, stops at the first null character (not necessarily
2147 the first null byte) up to a maximum of FETCHLIMIT characters. Set
2148 FETCHLIMIT to UINT_MAX to read as many characters as possible from
2151 Unless an exception is thrown, BUFFER will always be allocated, even on
2152 failure. In this case, some characters might have been read before the
2153 failure happened. Check BYTES_READ to recognize this situation.
2155 Note: There was a FIXME asking to make this code use target_read_string,
2156 but this function is more general (can read past null characters, up to
2157 given LEN). Besides, it is used much more often than target_read_string
2158 so it is more tested. Perhaps callers of target_read_string should use
2159 this function instead? */
2162 read_string (CORE_ADDR addr
, int len
, int width
, unsigned int fetchlimit
,
2163 enum bfd_endian byte_order
, gdb_byte
**buffer
, int *bytes_read
)
2165 int errcode
; /* Errno returned from bad reads. */
2166 unsigned int nfetch
; /* Chars to fetch / chars fetched. */
2167 gdb_byte
*bufptr
; /* Pointer to next available byte in
2169 struct cleanup
*old_chain
= NULL
; /* Top of the old cleanup chain. */
2171 /* Loop until we either have all the characters, or we encounter
2172 some error, such as bumping into the end of the address space. */
2176 old_chain
= make_cleanup (free_current_contents
, buffer
);
2180 /* We want fetchlimit chars, so we might as well read them all in
2182 unsigned int fetchlen
= std::min ((unsigned) len
, fetchlimit
);
2184 *buffer
= (gdb_byte
*) xmalloc (fetchlen
* width
);
2187 nfetch
= partial_memory_read (addr
, bufptr
, fetchlen
* width
, &errcode
)
2189 addr
+= nfetch
* width
;
2190 bufptr
+= nfetch
* width
;
2194 unsigned long bufsize
= 0;
2195 unsigned int chunksize
; /* Size of each fetch, in chars. */
2196 int found_nul
; /* Non-zero if we found the nul char. */
2197 gdb_byte
*limit
; /* First location past end of fetch buffer. */
2200 /* We are looking for a NUL terminator to end the fetching, so we
2201 might as well read in blocks that are large enough to be efficient,
2202 but not so large as to be slow if fetchlimit happens to be large.
2203 So we choose the minimum of 8 and fetchlimit. We used to use 200
2204 instead of 8 but 200 is way too big for remote debugging over a
2206 chunksize
= std::min (8u, fetchlimit
);
2211 nfetch
= std::min ((unsigned long) chunksize
, fetchlimit
- bufsize
);
2213 if (*buffer
== NULL
)
2214 *buffer
= (gdb_byte
*) xmalloc (nfetch
* width
);
2216 *buffer
= (gdb_byte
*) xrealloc (*buffer
,
2217 (nfetch
+ bufsize
) * width
);
2219 bufptr
= *buffer
+ bufsize
* width
;
2222 /* Read as much as we can. */
2223 nfetch
= partial_memory_read (addr
, bufptr
, nfetch
* width
, &errcode
)
2226 /* Scan this chunk for the null character that terminates the string
2227 to print. If found, we don't need to fetch any more. Note
2228 that bufptr is explicitly left pointing at the next character
2229 after the null character, or at the next character after the end
2232 limit
= bufptr
+ nfetch
* width
;
2233 while (bufptr
< limit
)
2237 c
= extract_unsigned_integer (bufptr
, width
, byte_order
);
2242 /* We don't care about any error which happened after
2243 the NUL terminator. */
2250 while (errcode
== 0 /* no error */
2251 && bufptr
- *buffer
< fetchlimit
* width
/* no overrun */
2252 && !found_nul
); /* haven't found NUL yet */
2255 { /* Length of string is really 0! */
2256 /* We always allocate *buffer. */
2257 *buffer
= bufptr
= (gdb_byte
*) xmalloc (1);
2261 /* bufptr and addr now point immediately beyond the last byte which we
2262 consider part of the string (including a '\0' which ends the string). */
2263 *bytes_read
= bufptr
- *buffer
;
2267 discard_cleanups (old_chain
);
2272 /* Return true if print_wchar can display W without resorting to a
2273 numeric escape, false otherwise. */
2276 wchar_printable (gdb_wchar_t w
)
2278 return (gdb_iswprint (w
)
2279 || w
== LCST ('\a') || w
== LCST ('\b')
2280 || w
== LCST ('\f') || w
== LCST ('\n')
2281 || w
== LCST ('\r') || w
== LCST ('\t')
2282 || w
== LCST ('\v'));
2285 /* A helper function that converts the contents of STRING to wide
2286 characters and then appends them to OUTPUT. */
2289 append_string_as_wide (const char *string
,
2290 struct obstack
*output
)
2292 for (; *string
; ++string
)
2294 gdb_wchar_t w
= gdb_btowc (*string
);
2295 obstack_grow (output
, &w
, sizeof (gdb_wchar_t
));
2299 /* Print a wide character W to OUTPUT. ORIG is a pointer to the
2300 original (target) bytes representing the character, ORIG_LEN is the
2301 number of valid bytes. WIDTH is the number of bytes in a base
2302 characters of the type. OUTPUT is an obstack to which wide
2303 characters are emitted. QUOTER is a (narrow) character indicating
2304 the style of quotes surrounding the character to be printed.
2305 NEED_ESCAPE is an in/out flag which is used to track numeric
2306 escapes across calls. */
2309 print_wchar (gdb_wint_t w
, const gdb_byte
*orig
,
2310 int orig_len
, int width
,
2311 enum bfd_endian byte_order
,
2312 struct obstack
*output
,
2313 int quoter
, int *need_escapep
)
2315 int need_escape
= *need_escapep
;
2319 /* iswprint implementation on Windows returns 1 for tab character.
2320 In order to avoid different printout on this host, we explicitly
2321 use wchar_printable function. */
2325 obstack_grow_wstr (output
, LCST ("\\a"));
2328 obstack_grow_wstr (output
, LCST ("\\b"));
2331 obstack_grow_wstr (output
, LCST ("\\f"));
2334 obstack_grow_wstr (output
, LCST ("\\n"));
2337 obstack_grow_wstr (output
, LCST ("\\r"));
2340 obstack_grow_wstr (output
, LCST ("\\t"));
2343 obstack_grow_wstr (output
, LCST ("\\v"));
2347 if (wchar_printable (w
) && (!need_escape
|| (!gdb_iswdigit (w
)
2349 && w
!= LCST ('9'))))
2351 gdb_wchar_t wchar
= w
;
2353 if (w
== gdb_btowc (quoter
) || w
== LCST ('\\'))
2354 obstack_grow_wstr (output
, LCST ("\\"));
2355 obstack_grow (output
, &wchar
, sizeof (gdb_wchar_t
));
2361 for (i
= 0; i
+ width
<= orig_len
; i
+= width
)
2366 value
= extract_unsigned_integer (&orig
[i
], width
,
2368 /* If the value fits in 3 octal digits, print it that
2369 way. Otherwise, print it as a hex escape. */
2371 xsnprintf (octal
, sizeof (octal
), "\\%.3o",
2372 (int) (value
& 0777));
2374 xsnprintf (octal
, sizeof (octal
), "\\x%lx", (long) value
);
2375 append_string_as_wide (octal
, output
);
2377 /* If we somehow have extra bytes, print them now. */
2378 while (i
< orig_len
)
2382 xsnprintf (octal
, sizeof (octal
), "\\%.3o", orig
[i
] & 0xff);
2383 append_string_as_wide (octal
, output
);
2394 /* Print the character C on STREAM as part of the contents of a
2395 literal string whose delimiter is QUOTER. ENCODING names the
2399 generic_emit_char (int c
, struct type
*type
, struct ui_file
*stream
,
2400 int quoter
, const char *encoding
)
2402 enum bfd_endian byte_order
2403 = gdbarch_byte_order (get_type_arch (type
));
2404 struct obstack wchar_buf
, output
;
2405 struct cleanup
*cleanups
;
2407 int need_escape
= 0;
2409 buf
= (gdb_byte
*) alloca (TYPE_LENGTH (type
));
2410 pack_long (buf
, type
, c
);
2412 wchar_iterator
iter (buf
, TYPE_LENGTH (type
), encoding
, TYPE_LENGTH (type
));
2414 /* This holds the printable form of the wchar_t data. */
2415 obstack_init (&wchar_buf
);
2416 cleanups
= make_cleanup_obstack_free (&wchar_buf
);
2422 const gdb_byte
*buf
;
2424 int print_escape
= 1;
2425 enum wchar_iterate_result result
;
2427 num_chars
= iter
.iterate (&result
, &chars
, &buf
, &buflen
);
2432 /* If all characters are printable, print them. Otherwise,
2433 we're going to have to print an escape sequence. We
2434 check all characters because we want to print the target
2435 bytes in the escape sequence, and we don't know character
2436 boundaries there. */
2440 for (i
= 0; i
< num_chars
; ++i
)
2441 if (!wchar_printable (chars
[i
]))
2449 for (i
= 0; i
< num_chars
; ++i
)
2450 print_wchar (chars
[i
], buf
, buflen
,
2451 TYPE_LENGTH (type
), byte_order
,
2452 &wchar_buf
, quoter
, &need_escape
);
2456 /* This handles the NUM_CHARS == 0 case as well. */
2458 print_wchar (gdb_WEOF
, buf
, buflen
, TYPE_LENGTH (type
),
2459 byte_order
, &wchar_buf
, quoter
, &need_escape
);
2462 /* The output in the host encoding. */
2463 obstack_init (&output
);
2464 make_cleanup_obstack_free (&output
);
2466 convert_between_encodings (INTERMEDIATE_ENCODING
, host_charset (),
2467 (gdb_byte
*) obstack_base (&wchar_buf
),
2468 obstack_object_size (&wchar_buf
),
2469 sizeof (gdb_wchar_t
), &output
, translit_char
);
2470 obstack_1grow (&output
, '\0');
2472 fputs_filtered ((const char *) obstack_base (&output
), stream
);
2474 do_cleanups (cleanups
);
2477 /* Return the repeat count of the next character/byte in ITER,
2478 storing the result in VEC. */
2481 count_next_character (wchar_iterator
*iter
,
2482 VEC (converted_character_d
) **vec
)
2484 struct converted_character
*current
;
2486 if (VEC_empty (converted_character_d
, *vec
))
2488 struct converted_character tmp
;
2492 = iter
->iterate (&tmp
.result
, &chars
, &tmp
.buf
, &tmp
.buflen
);
2493 if (tmp
.num_chars
> 0)
2495 gdb_assert (tmp
.num_chars
< MAX_WCHARS
);
2496 memcpy (tmp
.chars
, chars
, tmp
.num_chars
* sizeof (gdb_wchar_t
));
2498 VEC_safe_push (converted_character_d
, *vec
, &tmp
);
2501 current
= VEC_last (converted_character_d
, *vec
);
2503 /* Count repeated characters or bytes. */
2504 current
->repeat_count
= 1;
2505 if (current
->num_chars
== -1)
2513 struct converted_character d
;
2520 /* Get the next character. */
2521 d
.num_chars
= iter
->iterate (&d
.result
, &chars
, &d
.buf
, &d
.buflen
);
2523 /* If a character was successfully converted, save the character
2524 into the converted character. */
2525 if (d
.num_chars
> 0)
2527 gdb_assert (d
.num_chars
< MAX_WCHARS
);
2528 memcpy (d
.chars
, chars
, WCHAR_BUFLEN (d
.num_chars
));
2531 /* Determine if the current character is the same as this
2533 if (d
.num_chars
== current
->num_chars
&& d
.result
== current
->result
)
2535 /* There are two cases to consider:
2537 1) Equality of converted character (num_chars > 0)
2538 2) Equality of non-converted character (num_chars == 0) */
2539 if ((current
->num_chars
> 0
2540 && memcmp (current
->chars
, d
.chars
,
2541 WCHAR_BUFLEN (current
->num_chars
)) == 0)
2542 || (current
->num_chars
== 0
2543 && current
->buflen
== d
.buflen
2544 && memcmp (current
->buf
, d
.buf
, current
->buflen
) == 0))
2545 ++current
->repeat_count
;
2553 /* Push this next converted character onto the result vector. */
2554 repeat
= current
->repeat_count
;
2555 VEC_safe_push (converted_character_d
, *vec
, &d
);
2560 /* Print the characters in CHARS to the OBSTACK. QUOTE_CHAR is the quote
2561 character to use with string output. WIDTH is the size of the output
2562 character type. BYTE_ORDER is the the target byte order. OPTIONS
2563 is the user's print options. */
2566 print_converted_chars_to_obstack (struct obstack
*obstack
,
2567 VEC (converted_character_d
) *chars
,
2568 int quote_char
, int width
,
2569 enum bfd_endian byte_order
,
2570 const struct value_print_options
*options
)
2573 struct converted_character
*elem
;
2574 enum {START
, SINGLE
, REPEAT
, INCOMPLETE
, FINISH
} state
, last
;
2575 gdb_wchar_t wide_quote_char
= gdb_btowc (quote_char
);
2576 int need_escape
= 0;
2578 /* Set the start state. */
2580 last
= state
= START
;
2588 /* Nothing to do. */
2595 /* We are outputting a single character
2596 (< options->repeat_count_threshold). */
2600 /* We were outputting some other type of content, so we
2601 must output and a comma and a quote. */
2603 obstack_grow_wstr (obstack
, LCST (", "));
2604 obstack_grow (obstack
, &wide_quote_char
, sizeof (gdb_wchar_t
));
2606 /* Output the character. */
2607 for (j
= 0; j
< elem
->repeat_count
; ++j
)
2609 if (elem
->result
== wchar_iterate_ok
)
2610 print_wchar (elem
->chars
[0], elem
->buf
, elem
->buflen
, width
,
2611 byte_order
, obstack
, quote_char
, &need_escape
);
2613 print_wchar (gdb_WEOF
, elem
->buf
, elem
->buflen
, width
,
2614 byte_order
, obstack
, quote_char
, &need_escape
);
2624 /* We are outputting a character with a repeat count
2625 greater than options->repeat_count_threshold. */
2629 /* We were outputting a single string. Terminate the
2631 obstack_grow (obstack
, &wide_quote_char
, sizeof (gdb_wchar_t
));
2634 obstack_grow_wstr (obstack
, LCST (", "));
2636 /* Output the character and repeat string. */
2637 obstack_grow_wstr (obstack
, LCST ("'"));
2638 if (elem
->result
== wchar_iterate_ok
)
2639 print_wchar (elem
->chars
[0], elem
->buf
, elem
->buflen
, width
,
2640 byte_order
, obstack
, quote_char
, &need_escape
);
2642 print_wchar (gdb_WEOF
, elem
->buf
, elem
->buflen
, width
,
2643 byte_order
, obstack
, quote_char
, &need_escape
);
2644 obstack_grow_wstr (obstack
, LCST ("'"));
2645 s
= xstrprintf (_(" <repeats %u times>"), elem
->repeat_count
);
2646 for (j
= 0; s
[j
]; ++j
)
2648 gdb_wchar_t w
= gdb_btowc (s
[j
]);
2649 obstack_grow (obstack
, &w
, sizeof (gdb_wchar_t
));
2656 /* We are outputting an incomplete sequence. */
2659 /* If we were outputting a string of SINGLE characters,
2660 terminate the quote. */
2661 obstack_grow (obstack
, &wide_quote_char
, sizeof (gdb_wchar_t
));
2664 obstack_grow_wstr (obstack
, LCST (", "));
2666 /* Output the incomplete sequence string. */
2667 obstack_grow_wstr (obstack
, LCST ("<incomplete sequence "));
2668 print_wchar (gdb_WEOF
, elem
->buf
, elem
->buflen
, width
, byte_order
,
2669 obstack
, 0, &need_escape
);
2670 obstack_grow_wstr (obstack
, LCST (">"));
2672 /* We do not attempt to outupt anything after this. */
2677 /* All done. If we were outputting a string of SINGLE
2678 characters, the string must be terminated. Otherwise,
2679 REPEAT and INCOMPLETE are always left properly terminated. */
2681 obstack_grow (obstack
, &wide_quote_char
, sizeof (gdb_wchar_t
));
2686 /* Get the next element and state. */
2688 if (state
!= FINISH
)
2690 elem
= VEC_index (converted_character_d
, chars
, idx
++);
2691 switch (elem
->result
)
2693 case wchar_iterate_ok
:
2694 case wchar_iterate_invalid
:
2695 if (elem
->repeat_count
> options
->repeat_count_threshold
)
2701 case wchar_iterate_incomplete
:
2705 case wchar_iterate_eof
:
2713 /* Print the character string STRING, printing at most LENGTH
2714 characters. LENGTH is -1 if the string is nul terminated. TYPE is
2715 the type of each character. OPTIONS holds the printing options;
2716 printing stops early if the number hits print_max; repeat counts
2717 are printed as appropriate. Print ellipses at the end if we had to
2718 stop before printing LENGTH characters, or if FORCE_ELLIPSES.
2719 QUOTE_CHAR is the character to print at each end of the string. If
2720 C_STYLE_TERMINATOR is true, and the last character is 0, then it is
2724 generic_printstr (struct ui_file
*stream
, struct type
*type
,
2725 const gdb_byte
*string
, unsigned int length
,
2726 const char *encoding
, int force_ellipses
,
2727 int quote_char
, int c_style_terminator
,
2728 const struct value_print_options
*options
)
2730 enum bfd_endian byte_order
= gdbarch_byte_order (get_type_arch (type
));
2732 int width
= TYPE_LENGTH (type
);
2733 struct obstack wchar_buf
, output
;
2734 struct cleanup
*cleanup
;
2736 struct converted_character
*last
;
2737 VEC (converted_character_d
) *converted_chars
;
2741 unsigned long current_char
= 1;
2743 for (i
= 0; current_char
; ++i
)
2746 current_char
= extract_unsigned_integer (string
+ i
* width
,
2752 /* If the string was not truncated due to `set print elements', and
2753 the last byte of it is a null, we don't print that, in
2754 traditional C style. */
2755 if (c_style_terminator
2758 && (extract_unsigned_integer (string
+ (length
- 1) * width
,
2759 width
, byte_order
) == 0))
2764 fputs_filtered ("\"\"", stream
);
2768 /* Arrange to iterate over the characters, in wchar_t form. */
2769 wchar_iterator
iter (string
, length
* width
, encoding
, width
);
2770 converted_chars
= NULL
;
2771 cleanup
= make_cleanup (VEC_cleanup (converted_character_d
),
2774 /* Convert characters until the string is over or the maximum
2775 number of printed characters has been reached. */
2777 while (i
< options
->print_max
)
2783 /* Grab the next character and repeat count. */
2784 r
= count_next_character (&iter
, &converted_chars
);
2786 /* If less than zero, the end of the input string was reached. */
2790 /* Otherwise, add the count to the total print count and get
2791 the next character. */
2795 /* Get the last element and determine if the entire string was
2797 last
= VEC_last (converted_character_d
, converted_chars
);
2798 finished
= (last
->result
== wchar_iterate_eof
);
2800 /* Ensure that CONVERTED_CHARS is terminated. */
2801 last
->result
= wchar_iterate_eof
;
2803 /* WCHAR_BUF is the obstack we use to represent the string in
2805 obstack_init (&wchar_buf
);
2806 make_cleanup_obstack_free (&wchar_buf
);
2808 /* Print the output string to the obstack. */
2809 print_converted_chars_to_obstack (&wchar_buf
, converted_chars
, quote_char
,
2810 width
, byte_order
, options
);
2812 if (force_ellipses
|| !finished
)
2813 obstack_grow_wstr (&wchar_buf
, LCST ("..."));
2815 /* OUTPUT is where we collect `char's for printing. */
2816 obstack_init (&output
);
2817 make_cleanup_obstack_free (&output
);
2819 convert_between_encodings (INTERMEDIATE_ENCODING
, host_charset (),
2820 (gdb_byte
*) obstack_base (&wchar_buf
),
2821 obstack_object_size (&wchar_buf
),
2822 sizeof (gdb_wchar_t
), &output
, translit_char
);
2823 obstack_1grow (&output
, '\0');
2825 fputs_filtered ((const char *) obstack_base (&output
), stream
);
2827 do_cleanups (cleanup
);
2830 /* Print a string from the inferior, starting at ADDR and printing up to LEN
2831 characters, of WIDTH bytes a piece, to STREAM. If LEN is -1, printing
2832 stops at the first null byte, otherwise printing proceeds (including null
2833 bytes) until either print_max or LEN characters have been printed,
2834 whichever is smaller. ENCODING is the name of the string's
2835 encoding. It can be NULL, in which case the target encoding is
2839 val_print_string (struct type
*elttype
, const char *encoding
,
2840 CORE_ADDR addr
, int len
,
2841 struct ui_file
*stream
,
2842 const struct value_print_options
*options
)
2844 int force_ellipsis
= 0; /* Force ellipsis to be printed if nonzero. */
2845 int err
; /* Non-zero if we got a bad read. */
2846 int found_nul
; /* Non-zero if we found the nul char. */
2847 unsigned int fetchlimit
; /* Maximum number of chars to print. */
2849 gdb_byte
*buffer
= NULL
; /* Dynamically growable fetch buffer. */
2850 struct cleanup
*old_chain
= NULL
; /* Top of the old cleanup chain. */
2851 struct gdbarch
*gdbarch
= get_type_arch (elttype
);
2852 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
2853 int width
= TYPE_LENGTH (elttype
);
2855 /* First we need to figure out the limit on the number of characters we are
2856 going to attempt to fetch and print. This is actually pretty simple. If
2857 LEN >= zero, then the limit is the minimum of LEN and print_max. If
2858 LEN is -1, then the limit is print_max. This is true regardless of
2859 whether print_max is zero, UINT_MAX (unlimited), or something in between,
2860 because finding the null byte (or available memory) is what actually
2861 limits the fetch. */
2863 fetchlimit
= (len
== -1 ? options
->print_max
: std::min ((unsigned) len
,
2864 options
->print_max
));
2866 err
= read_string (addr
, len
, width
, fetchlimit
, byte_order
,
2867 &buffer
, &bytes_read
);
2868 old_chain
= make_cleanup (xfree
, buffer
);
2872 /* We now have either successfully filled the buffer to fetchlimit,
2873 or terminated early due to an error or finding a null char when
2876 /* Determine found_nul by looking at the last character read. */
2878 if (bytes_read
>= width
)
2879 found_nul
= extract_unsigned_integer (buffer
+ bytes_read
- width
, width
,
2881 if (len
== -1 && !found_nul
)
2885 /* We didn't find a NUL terminator we were looking for. Attempt
2886 to peek at the next character. If not successful, or it is not
2887 a null byte, then force ellipsis to be printed. */
2889 peekbuf
= (gdb_byte
*) alloca (width
);
2891 if (target_read_memory (addr
, peekbuf
, width
) == 0
2892 && extract_unsigned_integer (peekbuf
, width
, byte_order
) != 0)
2895 else if ((len
>= 0 && err
!= 0) || (len
> bytes_read
/ width
))
2897 /* Getting an error when we have a requested length, or fetching less
2898 than the number of characters actually requested, always make us
2903 /* If we get an error before fetching anything, don't print a string.
2904 But if we fetch something and then get an error, print the string
2905 and then the error message. */
2906 if (err
== 0 || bytes_read
> 0)
2908 LA_PRINT_STRING (stream
, elttype
, buffer
, bytes_read
/ width
,
2909 encoding
, force_ellipsis
, options
);
2916 str
= memory_error_message (TARGET_XFER_E_IO
, gdbarch
, addr
);
2917 make_cleanup (xfree
, str
);
2919 fprintf_filtered (stream
, "<error: ");
2920 fputs_filtered (str
, stream
);
2921 fprintf_filtered (stream
, ">");
2925 do_cleanups (old_chain
);
2927 return (bytes_read
/ width
);
2931 /* The 'set input-radix' command writes to this auxiliary variable.
2932 If the requested radix is valid, INPUT_RADIX is updated; otherwise,
2933 it is left unchanged. */
2935 static unsigned input_radix_1
= 10;
2937 /* Validate an input or output radix setting, and make sure the user
2938 knows what they really did here. Radix setting is confusing, e.g.
2939 setting the input radix to "10" never changes it! */
2942 set_input_radix (char *args
, int from_tty
, struct cmd_list_element
*c
)
2944 set_input_radix_1 (from_tty
, input_radix_1
);
2948 set_input_radix_1 (int from_tty
, unsigned radix
)
2950 /* We don't currently disallow any input radix except 0 or 1, which don't
2951 make any mathematical sense. In theory, we can deal with any input
2952 radix greater than 1, even if we don't have unique digits for every
2953 value from 0 to radix-1, but in practice we lose on large radix values.
2954 We should either fix the lossage or restrict the radix range more.
2959 input_radix_1
= input_radix
;
2960 error (_("Nonsense input radix ``decimal %u''; input radix unchanged."),
2963 input_radix_1
= input_radix
= radix
;
2966 printf_filtered (_("Input radix now set to "
2967 "decimal %u, hex %x, octal %o.\n"),
2968 radix
, radix
, radix
);
2972 /* The 'set output-radix' command writes to this auxiliary variable.
2973 If the requested radix is valid, OUTPUT_RADIX is updated,
2974 otherwise, it is left unchanged. */
2976 static unsigned output_radix_1
= 10;
2979 set_output_radix (char *args
, int from_tty
, struct cmd_list_element
*c
)
2981 set_output_radix_1 (from_tty
, output_radix_1
);
2985 set_output_radix_1 (int from_tty
, unsigned radix
)
2987 /* Validate the radix and disallow ones that we aren't prepared to
2988 handle correctly, leaving the radix unchanged. */
2992 user_print_options
.output_format
= 'x'; /* hex */
2995 user_print_options
.output_format
= 0; /* decimal */
2998 user_print_options
.output_format
= 'o'; /* octal */
3001 output_radix_1
= output_radix
;
3002 error (_("Unsupported output radix ``decimal %u''; "
3003 "output radix unchanged."),
3006 output_radix_1
= output_radix
= radix
;
3009 printf_filtered (_("Output radix now set to "
3010 "decimal %u, hex %x, octal %o.\n"),
3011 radix
, radix
, radix
);
3015 /* Set both the input and output radix at once. Try to set the output radix
3016 first, since it has the most restrictive range. An radix that is valid as
3017 an output radix is also valid as an input radix.
3019 It may be useful to have an unusual input radix. If the user wishes to
3020 set an input radix that is not valid as an output radix, he needs to use
3021 the 'set input-radix' command. */
3024 set_radix (char *arg
, int from_tty
)
3028 radix
= (arg
== NULL
) ? 10 : parse_and_eval_long (arg
);
3029 set_output_radix_1 (0, radix
);
3030 set_input_radix_1 (0, radix
);
3033 printf_filtered (_("Input and output radices now set to "
3034 "decimal %u, hex %x, octal %o.\n"),
3035 radix
, radix
, radix
);
3039 /* Show both the input and output radices. */
3042 show_radix (char *arg
, int from_tty
)
3046 if (input_radix
== output_radix
)
3048 printf_filtered (_("Input and output radices set to "
3049 "decimal %u, hex %x, octal %o.\n"),
3050 input_radix
, input_radix
, input_radix
);
3054 printf_filtered (_("Input radix set to decimal "
3055 "%u, hex %x, octal %o.\n"),
3056 input_radix
, input_radix
, input_radix
);
3057 printf_filtered (_("Output radix set to decimal "
3058 "%u, hex %x, octal %o.\n"),
3059 output_radix
, output_radix
, output_radix
);
3066 set_print (char *arg
, int from_tty
)
3069 "\"set print\" must be followed by the name of a print subcommand.\n");
3070 help_list (setprintlist
, "set print ", all_commands
, gdb_stdout
);
3074 show_print (char *args
, int from_tty
)
3076 cmd_show_list (showprintlist
, from_tty
, "");
3080 set_print_raw (char *arg
, int from_tty
)
3083 "\"set print raw\" must be followed by the name of a \"print raw\" subcommand.\n");
3084 help_list (setprintrawlist
, "set print raw ", all_commands
, gdb_stdout
);
3088 show_print_raw (char *args
, int from_tty
)
3090 cmd_show_list (showprintrawlist
, from_tty
, "");
3095 _initialize_valprint (void)
3097 add_prefix_cmd ("print", no_class
, set_print
,
3098 _("Generic command for setting how things print."),
3099 &setprintlist
, "set print ", 0, &setlist
);
3100 add_alias_cmd ("p", "print", no_class
, 1, &setlist
);
3101 /* Prefer set print to set prompt. */
3102 add_alias_cmd ("pr", "print", no_class
, 1, &setlist
);
3104 add_prefix_cmd ("print", no_class
, show_print
,
3105 _("Generic command for showing print settings."),
3106 &showprintlist
, "show print ", 0, &showlist
);
3107 add_alias_cmd ("p", "print", no_class
, 1, &showlist
);
3108 add_alias_cmd ("pr", "print", no_class
, 1, &showlist
);
3110 add_prefix_cmd ("raw", no_class
, set_print_raw
,
3112 Generic command for setting what things to print in \"raw\" mode."),
3113 &setprintrawlist
, "set print raw ", 0, &setprintlist
);
3114 add_prefix_cmd ("raw", no_class
, show_print_raw
,
3115 _("Generic command for showing \"print raw\" settings."),
3116 &showprintrawlist
, "show print raw ", 0, &showprintlist
);
3118 add_setshow_uinteger_cmd ("elements", no_class
,
3119 &user_print_options
.print_max
, _("\
3120 Set limit on string chars or array elements to print."), _("\
3121 Show limit on string chars or array elements to print."), _("\
3122 \"set print elements unlimited\" causes there to be no limit."),
3125 &setprintlist
, &showprintlist
);
3127 add_setshow_boolean_cmd ("null-stop", no_class
,
3128 &user_print_options
.stop_print_at_null
, _("\
3129 Set printing of char arrays to stop at first null char."), _("\
3130 Show printing of char arrays to stop at first null char."), NULL
,
3132 show_stop_print_at_null
,
3133 &setprintlist
, &showprintlist
);
3135 add_setshow_uinteger_cmd ("repeats", no_class
,
3136 &user_print_options
.repeat_count_threshold
, _("\
3137 Set threshold for repeated print elements."), _("\
3138 Show threshold for repeated print elements."), _("\
3139 \"set print repeats unlimited\" causes all elements to be individually printed."),
3141 show_repeat_count_threshold
,
3142 &setprintlist
, &showprintlist
);
3144 add_setshow_boolean_cmd ("pretty", class_support
,
3145 &user_print_options
.prettyformat_structs
, _("\
3146 Set pretty formatting of structures."), _("\
3147 Show pretty formatting of structures."), NULL
,
3149 show_prettyformat_structs
,
3150 &setprintlist
, &showprintlist
);
3152 add_setshow_boolean_cmd ("union", class_support
,
3153 &user_print_options
.unionprint
, _("\
3154 Set printing of unions interior to structures."), _("\
3155 Show printing of unions interior to structures."), NULL
,
3158 &setprintlist
, &showprintlist
);
3160 add_setshow_boolean_cmd ("array", class_support
,
3161 &user_print_options
.prettyformat_arrays
, _("\
3162 Set pretty formatting of arrays."), _("\
3163 Show pretty formatting of arrays."), NULL
,
3165 show_prettyformat_arrays
,
3166 &setprintlist
, &showprintlist
);
3168 add_setshow_boolean_cmd ("address", class_support
,
3169 &user_print_options
.addressprint
, _("\
3170 Set printing of addresses."), _("\
3171 Show printing of addresses."), NULL
,
3174 &setprintlist
, &showprintlist
);
3176 add_setshow_boolean_cmd ("symbol", class_support
,
3177 &user_print_options
.symbol_print
, _("\
3178 Set printing of symbol names when printing pointers."), _("\
3179 Show printing of symbol names when printing pointers."),
3182 &setprintlist
, &showprintlist
);
3184 add_setshow_zuinteger_cmd ("input-radix", class_support
, &input_radix_1
,
3186 Set default input radix for entering numbers."), _("\
3187 Show default input radix for entering numbers."), NULL
,
3190 &setlist
, &showlist
);
3192 add_setshow_zuinteger_cmd ("output-radix", class_support
, &output_radix_1
,
3194 Set default output radix for printing of values."), _("\
3195 Show default output radix for printing of values."), NULL
,
3198 &setlist
, &showlist
);
3200 /* The "set radix" and "show radix" commands are special in that
3201 they are like normal set and show commands but allow two normally
3202 independent variables to be either set or shown with a single
3203 command. So the usual deprecated_add_set_cmd() and [deleted]
3204 add_show_from_set() commands aren't really appropriate. */
3205 /* FIXME: i18n: With the new add_setshow_integer command, that is no
3206 longer true - show can display anything. */
3207 add_cmd ("radix", class_support
, set_radix
, _("\
3208 Set default input and output number radices.\n\
3209 Use 'set input-radix' or 'set output-radix' to independently set each.\n\
3210 Without an argument, sets both radices back to the default value of 10."),
3212 add_cmd ("radix", class_support
, show_radix
, _("\
3213 Show the default input and output number radices.\n\
3214 Use 'show input-radix' or 'show output-radix' to independently show each."),
3217 add_setshow_boolean_cmd ("array-indexes", class_support
,
3218 &user_print_options
.print_array_indexes
, _("\
3219 Set printing of array indexes."), _("\
3220 Show printing of array indexes"), NULL
, NULL
, show_print_array_indexes
,
3221 &setprintlist
, &showprintlist
);