wg.conf.5: Fix a typo (in-inline comments are *not* allowed)
[dragonfly.git] / contrib / gdb-7 / gdb / valprint.c
blob18cff49f3b50a11dbfb5a710de692e584623be69
1 /* Print values for GDB, the GNU debugger.
3 Copyright (C) 1986-2013 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/>. */
20 #include "defs.h"
21 #include "gdb_string.h"
22 #include "symtab.h"
23 #include "gdbtypes.h"
24 #include "value.h"
25 #include "gdbcore.h"
26 #include "gdbcmd.h"
27 #include "target.h"
28 #include "language.h"
29 #include "annotate.h"
30 #include "valprint.h"
31 #include "floatformat.h"
32 #include "doublest.h"
33 #include "exceptions.h"
34 #include "dfp.h"
35 #include "python/python.h"
36 #include "ada-lang.h"
37 #include "gdb_obstack.h"
38 #include "charset.h"
39 #include <ctype.h>
41 #include <errno.h>
43 /* Maximum number of wchars returned from wchar_iterate. */
44 #define MAX_WCHARS 4
46 /* A convenience macro to compute the size of a wchar_t buffer containing X
47 characters. */
48 #define WCHAR_BUFLEN(X) ((X) * sizeof (gdb_wchar_t))
50 /* Character buffer size saved while iterating over wchars. */
51 #define WCHAR_BUFLEN_MAX WCHAR_BUFLEN (MAX_WCHARS)
53 /* A structure to encapsulate state information from iterated
54 character conversions. */
55 struct converted_character
57 /* The number of characters converted. */
58 int num_chars;
60 /* The result of the conversion. See charset.h for more. */
61 enum wchar_iterate_result result;
63 /* The (saved) converted character(s). */
64 gdb_wchar_t chars[WCHAR_BUFLEN_MAX];
66 /* The first converted target byte. */
67 const gdb_byte *buf;
69 /* The number of bytes converted. */
70 size_t buflen;
72 /* How many times this character(s) is repeated. */
73 int repeat_count;
76 typedef struct converted_character converted_character_d;
77 DEF_VEC_O (converted_character_d);
80 /* Prototypes for local functions */
82 static int partial_memory_read (CORE_ADDR memaddr, gdb_byte *myaddr,
83 int len, int *errnoptr);
85 static void show_print (char *, int);
87 static void set_print (char *, int);
89 static void set_radix (char *, int);
91 static void show_radix (char *, int);
93 static void set_input_radix (char *, int, struct cmd_list_element *);
95 static void set_input_radix_1 (int, unsigned);
97 static void set_output_radix (char *, int, struct cmd_list_element *);
99 static void set_output_radix_1 (int, unsigned);
101 void _initialize_valprint (void);
103 #define PRINT_MAX_DEFAULT 200 /* Start print_max off at this value. */
105 struct value_print_options user_print_options =
107 Val_pretty_default, /* pretty */
108 0, /* prettyprint_arrays */
109 0, /* prettyprint_structs */
110 0, /* vtblprint */
111 1, /* unionprint */
112 1, /* addressprint */
113 0, /* objectprint */
114 PRINT_MAX_DEFAULT, /* print_max */
115 10, /* repeat_count_threshold */
116 0, /* output_format */
117 0, /* format */
118 0, /* stop_print_at_null */
119 0, /* print_array_indexes */
120 0, /* deref_ref */
121 1, /* static_field_print */
122 1, /* pascal_static_field_print */
123 0, /* raw */
124 0, /* summary */
125 1 /* symbol_print */
128 /* Initialize *OPTS to be a copy of the user print options. */
129 void
130 get_user_print_options (struct value_print_options *opts)
132 *opts = user_print_options;
135 /* Initialize *OPTS to be a copy of the user print options, but with
136 pretty-printing disabled. */
137 void
138 get_raw_print_options (struct value_print_options *opts)
140 *opts = user_print_options;
141 opts->pretty = Val_no_prettyprint;
144 /* Initialize *OPTS to be a copy of the user print options, but using
145 FORMAT as the formatting option. */
146 void
147 get_formatted_print_options (struct value_print_options *opts,
148 char format)
150 *opts = user_print_options;
151 opts->format = format;
154 static void
155 show_print_max (struct ui_file *file, int from_tty,
156 struct cmd_list_element *c, const char *value)
158 fprintf_filtered (file,
159 _("Limit on string chars or array "
160 "elements to print is %s.\n"),
161 value);
165 /* Default input and output radixes, and output format letter. */
167 unsigned input_radix = 10;
168 static void
169 show_input_radix (struct ui_file *file, int from_tty,
170 struct cmd_list_element *c, const char *value)
172 fprintf_filtered (file,
173 _("Default input radix for entering numbers is %s.\n"),
174 value);
177 unsigned output_radix = 10;
178 static void
179 show_output_radix (struct ui_file *file, int from_tty,
180 struct cmd_list_element *c, const char *value)
182 fprintf_filtered (file,
183 _("Default output radix for printing of values is %s.\n"),
184 value);
187 /* By default we print arrays without printing the index of each element in
188 the array. This behavior can be changed by setting PRINT_ARRAY_INDEXES. */
190 static void
191 show_print_array_indexes (struct ui_file *file, int from_tty,
192 struct cmd_list_element *c, const char *value)
194 fprintf_filtered (file, _("Printing of array indexes is %s.\n"), value);
197 /* Print repeat counts if there are more than this many repetitions of an
198 element in an array. Referenced by the low level language dependent
199 print routines. */
201 static void
202 show_repeat_count_threshold (struct ui_file *file, int from_tty,
203 struct cmd_list_element *c, const char *value)
205 fprintf_filtered (file, _("Threshold for repeated print elements is %s.\n"),
206 value);
209 /* If nonzero, stops printing of char arrays at first null. */
211 static void
212 show_stop_print_at_null (struct ui_file *file, int from_tty,
213 struct cmd_list_element *c, const char *value)
215 fprintf_filtered (file,
216 _("Printing of char arrays to stop "
217 "at first null char is %s.\n"),
218 value);
221 /* Controls pretty printing of structures. */
223 static void
224 show_prettyprint_structs (struct ui_file *file, int from_tty,
225 struct cmd_list_element *c, const char *value)
227 fprintf_filtered (file, _("Prettyprinting of structures is %s.\n"), value);
230 /* Controls pretty printing of arrays. */
232 static void
233 show_prettyprint_arrays (struct ui_file *file, int from_tty,
234 struct cmd_list_element *c, const char *value)
236 fprintf_filtered (file, _("Prettyprinting of arrays is %s.\n"), value);
239 /* If nonzero, causes unions inside structures or other unions to be
240 printed. */
242 static void
243 show_unionprint (struct ui_file *file, int from_tty,
244 struct cmd_list_element *c, const char *value)
246 fprintf_filtered (file,
247 _("Printing of unions interior to structures is %s.\n"),
248 value);
251 /* If nonzero, causes machine addresses to be printed in certain contexts. */
253 static void
254 show_addressprint (struct ui_file *file, int from_tty,
255 struct cmd_list_element *c, const char *value)
257 fprintf_filtered (file, _("Printing of addresses is %s.\n"), value);
260 static void
261 show_symbol_print (struct ui_file *file, int from_tty,
262 struct cmd_list_element *c, const char *value)
264 fprintf_filtered (file,
265 _("Printing of symbols when printing pointers is %s.\n"),
266 value);
271 /* A helper function for val_print. When printing in "summary" mode,
272 we want to print scalar arguments, but not aggregate arguments.
273 This function distinguishes between the two. */
275 static int
276 scalar_type_p (struct type *type)
278 CHECK_TYPEDEF (type);
279 while (TYPE_CODE (type) == TYPE_CODE_REF)
281 type = TYPE_TARGET_TYPE (type);
282 CHECK_TYPEDEF (type);
284 switch (TYPE_CODE (type))
286 case TYPE_CODE_ARRAY:
287 case TYPE_CODE_STRUCT:
288 case TYPE_CODE_UNION:
289 case TYPE_CODE_SET:
290 case TYPE_CODE_STRING:
291 return 0;
292 default:
293 return 1;
297 /* See its definition in value.h. */
300 valprint_check_validity (struct ui_file *stream,
301 struct type *type,
302 int embedded_offset,
303 const struct value *val)
305 CHECK_TYPEDEF (type);
307 if (TYPE_CODE (type) != TYPE_CODE_UNION
308 && TYPE_CODE (type) != TYPE_CODE_STRUCT
309 && TYPE_CODE (type) != TYPE_CODE_ARRAY)
311 if (!value_bits_valid (val, TARGET_CHAR_BIT * embedded_offset,
312 TARGET_CHAR_BIT * TYPE_LENGTH (type)))
314 val_print_optimized_out (stream);
315 return 0;
318 if (value_bits_synthetic_pointer (val, TARGET_CHAR_BIT * embedded_offset,
319 TARGET_CHAR_BIT * TYPE_LENGTH (type)))
321 fputs_filtered (_("<synthetic pointer>"), stream);
322 return 0;
325 if (!value_bytes_available (val, embedded_offset, TYPE_LENGTH (type)))
327 val_print_unavailable (stream);
328 return 0;
332 return 1;
335 void
336 val_print_optimized_out (struct ui_file *stream)
338 fprintf_filtered (stream, _("<optimized out>"));
341 void
342 val_print_unavailable (struct ui_file *stream)
344 fprintf_filtered (stream, _("<unavailable>"));
347 void
348 val_print_invalid_address (struct ui_file *stream)
350 fprintf_filtered (stream, _("<invalid address>"));
353 /* A generic val_print that is suitable for use by language
354 implementations of the la_val_print method. This function can
355 handle most type codes, though not all, notably exception
356 TYPE_CODE_UNION and TYPE_CODE_STRUCT, which must be implemented by
357 the caller.
359 Most arguments are as to val_print.
361 The additional DECORATIONS argument can be used to customize the
362 output in some small, language-specific ways. */
364 void
365 generic_val_print (struct type *type, const gdb_byte *valaddr,
366 int embedded_offset, CORE_ADDR address,
367 struct ui_file *stream, int recurse,
368 const struct value *original_value,
369 const struct value_print_options *options,
370 const struct generic_val_print_decorations *decorations)
372 struct gdbarch *gdbarch = get_type_arch (type);
373 unsigned int i = 0; /* Number of characters printed. */
374 unsigned len;
375 struct type *elttype, *unresolved_elttype;
376 struct type *unresolved_type = type;
377 LONGEST val;
378 CORE_ADDR addr;
380 CHECK_TYPEDEF (type);
381 switch (TYPE_CODE (type))
383 case TYPE_CODE_ARRAY:
384 unresolved_elttype = TYPE_TARGET_TYPE (type);
385 elttype = check_typedef (unresolved_elttype);
386 if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (unresolved_elttype) > 0)
388 LONGEST low_bound, high_bound;
390 if (!get_array_bounds (type, &low_bound, &high_bound))
391 error (_("Could not determine the array high bound"));
393 if (options->prettyprint_arrays)
395 print_spaces_filtered (2 + 2 * recurse, stream);
398 fprintf_filtered (stream, "{");
399 val_print_array_elements (type, valaddr, embedded_offset,
400 address, stream,
401 recurse, original_value, options, 0);
402 fprintf_filtered (stream, "}");
403 break;
405 /* Array of unspecified length: treat like pointer to first
406 elt. */
407 addr = address + embedded_offset;
408 goto print_unpacked_pointer;
410 case TYPE_CODE_MEMBERPTR:
411 val_print_scalar_formatted (type, valaddr, embedded_offset,
412 original_value, options, 0, stream);
413 break;
415 case TYPE_CODE_PTR:
416 if (options->format && options->format != 's')
418 val_print_scalar_formatted (type, valaddr, embedded_offset,
419 original_value, options, 0, stream);
420 break;
422 unresolved_elttype = TYPE_TARGET_TYPE (type);
423 elttype = check_typedef (unresolved_elttype);
425 addr = unpack_pointer (type, valaddr + embedded_offset);
426 print_unpacked_pointer:
428 if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
430 /* Try to print what function it points to. */
431 print_function_pointer_address (options, gdbarch, addr, stream);
432 return;
435 if (options->symbol_print)
436 print_address_demangle (options, gdbarch, addr, stream, demangle);
437 else if (options->addressprint)
438 fputs_filtered (paddress (gdbarch, addr), stream);
440 break;
442 case TYPE_CODE_REF:
443 elttype = check_typedef (TYPE_TARGET_TYPE (type));
444 if (options->addressprint)
446 CORE_ADDR addr
447 = extract_typed_address (valaddr + embedded_offset, type);
449 fprintf_filtered (stream, "@");
450 fputs_filtered (paddress (gdbarch, addr), stream);
451 if (options->deref_ref)
452 fputs_filtered (": ", stream);
454 /* De-reference the reference. */
455 if (options->deref_ref)
457 if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
459 struct value *deref_val;
461 deref_val = coerce_ref_if_computed (original_value);
462 if (deref_val != NULL)
464 /* More complicated computed references are not supported. */
465 gdb_assert (embedded_offset == 0);
467 else
468 deref_val = value_at (TYPE_TARGET_TYPE (type),
469 unpack_pointer (type,
470 (valaddr
471 + embedded_offset)));
473 common_val_print (deref_val, stream, recurse, options,
474 current_language);
476 else
477 fputs_filtered ("???", stream);
479 break;
481 case TYPE_CODE_ENUM:
482 if (options->format)
484 val_print_scalar_formatted (type, valaddr, embedded_offset,
485 original_value, options, 0, stream);
486 break;
488 len = TYPE_NFIELDS (type);
489 val = unpack_long (type, valaddr + embedded_offset);
490 for (i = 0; i < len; i++)
492 QUIT;
493 if (val == TYPE_FIELD_ENUMVAL (type, i))
495 break;
498 if (i < len)
500 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
502 else if (TYPE_FLAG_ENUM (type))
504 int first = 1;
506 /* We have a "flag" enum, so we try to decompose it into
507 pieces as appropriate. A flag enum has disjoint
508 constants by definition. */
509 fputs_filtered ("(", stream);
510 for (i = 0; i < len; ++i)
512 QUIT;
514 if ((val & TYPE_FIELD_ENUMVAL (type, i)) != 0)
516 if (!first)
517 fputs_filtered (" | ", stream);
518 first = 0;
520 val &= ~TYPE_FIELD_ENUMVAL (type, i);
521 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
525 if (first || val != 0)
527 if (!first)
528 fputs_filtered (" | ", stream);
529 fputs_filtered ("unknown: ", stream);
530 print_longest (stream, 'd', 0, val);
533 fputs_filtered (")", stream);
535 else
536 print_longest (stream, 'd', 0, val);
537 break;
539 case TYPE_CODE_FLAGS:
540 if (options->format)
541 val_print_scalar_formatted (type, valaddr, embedded_offset,
542 original_value, options, 0, stream);
543 else
544 val_print_type_code_flags (type, valaddr + embedded_offset,
545 stream);
546 break;
548 case TYPE_CODE_FUNC:
549 case TYPE_CODE_METHOD:
550 if (options->format)
552 val_print_scalar_formatted (type, valaddr, embedded_offset,
553 original_value, options, 0, stream);
554 break;
556 /* FIXME, we should consider, at least for ANSI C language,
557 eliminating the distinction made between FUNCs and POINTERs
558 to FUNCs. */
559 fprintf_filtered (stream, "{");
560 type_print (type, "", stream, -1);
561 fprintf_filtered (stream, "} ");
562 /* Try to print what function it points to, and its address. */
563 print_address_demangle (options, gdbarch, address, stream, demangle);
564 break;
566 case TYPE_CODE_BOOL:
567 if (options->format || options->output_format)
569 struct value_print_options opts = *options;
570 opts.format = (options->format ? options->format
571 : options->output_format);
572 val_print_scalar_formatted (type, valaddr, embedded_offset,
573 original_value, &opts, 0, stream);
575 else
577 val = unpack_long (type, valaddr + embedded_offset);
578 if (val == 0)
579 fputs_filtered (decorations->false_name, stream);
580 else if (val == 1)
581 fputs_filtered (decorations->true_name, stream);
582 else
583 print_longest (stream, 'd', 0, val);
585 break;
587 case TYPE_CODE_RANGE:
588 /* FIXME: create_range_type does not set the unsigned bit in a
589 range type (I think it probably should copy it from the
590 target type), so we won't print values which are too large to
591 fit in a signed integer correctly. */
592 /* FIXME: Doesn't handle ranges of enums correctly. (Can't just
593 print with the target type, though, because the size of our
594 type and the target type might differ). */
596 /* FALLTHROUGH */
598 case TYPE_CODE_INT:
599 if (options->format || options->output_format)
601 struct value_print_options opts = *options;
603 opts.format = (options->format ? options->format
604 : options->output_format);
605 val_print_scalar_formatted (type, valaddr, embedded_offset,
606 original_value, &opts, 0, stream);
608 else
609 val_print_type_code_int (type, valaddr + embedded_offset, stream);
610 break;
612 case TYPE_CODE_CHAR:
613 if (options->format || options->output_format)
615 struct value_print_options opts = *options;
617 opts.format = (options->format ? options->format
618 : options->output_format);
619 val_print_scalar_formatted (type, valaddr, embedded_offset,
620 original_value, &opts, 0, stream);
622 else
624 val = unpack_long (type, valaddr + embedded_offset);
625 if (TYPE_UNSIGNED (type))
626 fprintf_filtered (stream, "%u", (unsigned int) val);
627 else
628 fprintf_filtered (stream, "%d", (int) val);
629 fputs_filtered (" ", stream);
630 LA_PRINT_CHAR (val, unresolved_type, stream);
632 break;
634 case TYPE_CODE_FLT:
635 if (options->format)
637 val_print_scalar_formatted (type, valaddr, embedded_offset,
638 original_value, options, 0, stream);
640 else
642 print_floating (valaddr + embedded_offset, type, stream);
644 break;
646 case TYPE_CODE_DECFLOAT:
647 if (options->format)
648 val_print_scalar_formatted (type, valaddr, embedded_offset,
649 original_value, options, 0, stream);
650 else
651 print_decimal_floating (valaddr + embedded_offset,
652 type, stream);
653 break;
655 case TYPE_CODE_VOID:
656 fputs_filtered (decorations->void_name, stream);
657 break;
659 case TYPE_CODE_ERROR:
660 fprintf_filtered (stream, "%s", TYPE_ERROR_NAME (type));
661 break;
663 case TYPE_CODE_UNDEF:
664 /* This happens (without TYPE_FLAG_STUB set) on systems which
665 don't use dbx xrefs (NO_DBX_XREFS in gcc) if a file has a
666 "struct foo *bar" and no complete type for struct foo in that
667 file. */
668 fprintf_filtered (stream, _("<incomplete type>"));
669 break;
671 case TYPE_CODE_COMPLEX:
672 fprintf_filtered (stream, "%s", decorations->complex_prefix);
673 if (options->format)
674 val_print_scalar_formatted (TYPE_TARGET_TYPE (type),
675 valaddr, embedded_offset,
676 original_value, options, 0, stream);
677 else
678 print_floating (valaddr + embedded_offset,
679 TYPE_TARGET_TYPE (type),
680 stream);
681 fprintf_filtered (stream, "%s", decorations->complex_infix);
682 if (options->format)
683 val_print_scalar_formatted (TYPE_TARGET_TYPE (type),
684 valaddr,
685 embedded_offset
686 + TYPE_LENGTH (TYPE_TARGET_TYPE (type)),
687 original_value,
688 options, 0, stream);
689 else
690 print_floating (valaddr + embedded_offset
691 + TYPE_LENGTH (TYPE_TARGET_TYPE (type)),
692 TYPE_TARGET_TYPE (type),
693 stream);
694 fprintf_filtered (stream, "%s", decorations->complex_suffix);
695 break;
697 case TYPE_CODE_UNION:
698 case TYPE_CODE_STRUCT:
699 case TYPE_CODE_METHODPTR:
700 default:
701 error (_("Unhandled type code %d in symbol table."),
702 TYPE_CODE (type));
704 gdb_flush (stream);
707 /* Print using the given LANGUAGE the data of type TYPE located at
708 VALADDR + EMBEDDED_OFFSET (within GDB), which came from the
709 inferior at address ADDRESS + EMBEDDED_OFFSET, onto stdio stream
710 STREAM according to OPTIONS. VAL is the whole object that came
711 from ADDRESS. VALADDR must point to the head of VAL's contents
712 buffer.
714 The language printers will pass down an adjusted EMBEDDED_OFFSET to
715 further helper subroutines as subfields of TYPE are printed. In
716 such cases, VALADDR is passed down unadjusted, as well as VAL, so
717 that VAL can be queried for metadata about the contents data being
718 printed, using EMBEDDED_OFFSET as an offset into VAL's contents
719 buffer. For example: "has this field been optimized out", or "I'm
720 printing an object while inspecting a traceframe; has this
721 particular piece of data been collected?".
723 RECURSE indicates the amount of indentation to supply before
724 continuation lines; this amount is roughly twice the value of
725 RECURSE. */
727 void
728 val_print (struct type *type, const gdb_byte *valaddr, int embedded_offset,
729 CORE_ADDR address, struct ui_file *stream, int recurse,
730 const struct value *val,
731 const struct value_print_options *options,
732 const struct language_defn *language)
734 volatile struct gdb_exception except;
735 int ret = 0;
736 struct value_print_options local_opts = *options;
737 struct type *real_type = check_typedef (type);
739 if (local_opts.pretty == Val_pretty_default)
740 local_opts.pretty = (local_opts.prettyprint_structs
741 ? Val_prettyprint : Val_no_prettyprint);
743 QUIT;
745 /* Ensure that the type is complete and not just a stub. If the type is
746 only a stub and we can't find and substitute its complete type, then
747 print appropriate string and return. */
749 if (TYPE_STUB (real_type))
751 fprintf_filtered (stream, _("<incomplete type>"));
752 gdb_flush (stream);
753 return;
756 if (!valprint_check_validity (stream, real_type, embedded_offset, val))
757 return;
759 if (!options->raw)
761 ret = apply_val_pretty_printer (type, valaddr, embedded_offset,
762 address, stream, recurse,
763 val, options, language);
764 if (ret)
765 return;
768 /* Handle summary mode. If the value is a scalar, print it;
769 otherwise, print an ellipsis. */
770 if (options->summary && !scalar_type_p (type))
772 fprintf_filtered (stream, "...");
773 return;
776 TRY_CATCH (except, RETURN_MASK_ERROR)
778 language->la_val_print (type, valaddr, embedded_offset, address,
779 stream, recurse, val,
780 &local_opts);
782 if (except.reason < 0)
783 fprintf_filtered (stream, _("<error reading variable>"));
786 /* Check whether the value VAL is printable. Return 1 if it is;
787 return 0 and print an appropriate error message to STREAM according to
788 OPTIONS if it is not. */
790 static int
791 value_check_printable (struct value *val, struct ui_file *stream,
792 const struct value_print_options *options)
794 if (val == 0)
796 fprintf_filtered (stream, _("<address of value unknown>"));
797 return 0;
800 if (value_entirely_optimized_out (val))
802 if (options->summary && !scalar_type_p (value_type (val)))
803 fprintf_filtered (stream, "...");
804 else
805 val_print_optimized_out (stream);
806 return 0;
809 if (TYPE_CODE (value_type (val)) == TYPE_CODE_INTERNAL_FUNCTION)
811 fprintf_filtered (stream, _("<internal function %s>"),
812 value_internal_function_name (val));
813 return 0;
816 return 1;
819 /* Print using the given LANGUAGE the value VAL onto stream STREAM according
820 to OPTIONS.
822 This is a preferable interface to val_print, above, because it uses
823 GDB's value mechanism. */
825 void
826 common_val_print (struct value *val, struct ui_file *stream, int recurse,
827 const struct value_print_options *options,
828 const struct language_defn *language)
830 if (!value_check_printable (val, stream, options))
831 return;
833 if (language->la_language == language_ada)
834 /* The value might have a dynamic type, which would cause trouble
835 below when trying to extract the value contents (since the value
836 size is determined from the type size which is unknown). So
837 get a fixed representation of our value. */
838 val = ada_to_fixed_value (val);
840 val_print (value_type (val), value_contents_for_printing (val),
841 value_embedded_offset (val), value_address (val),
842 stream, recurse,
843 val, options, language);
846 /* Print on stream STREAM the value VAL according to OPTIONS. The value
847 is printed using the current_language syntax. */
849 void
850 value_print (struct value *val, struct ui_file *stream,
851 const struct value_print_options *options)
853 if (!value_check_printable (val, stream, options))
854 return;
856 if (!options->raw)
858 int r = apply_val_pretty_printer (value_type (val),
859 value_contents_for_printing (val),
860 value_embedded_offset (val),
861 value_address (val),
862 stream, 0,
863 val, options, current_language);
865 if (r)
866 return;
869 LA_VALUE_PRINT (val, stream, options);
872 /* Called by various <lang>_val_print routines to print
873 TYPE_CODE_INT's. TYPE is the type. VALADDR is the address of the
874 value. STREAM is where to print the value. */
876 void
877 val_print_type_code_int (struct type *type, const gdb_byte *valaddr,
878 struct ui_file *stream)
880 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
882 if (TYPE_LENGTH (type) > sizeof (LONGEST))
884 LONGEST val;
886 if (TYPE_UNSIGNED (type)
887 && extract_long_unsigned_integer (valaddr, TYPE_LENGTH (type),
888 byte_order, &val))
890 print_longest (stream, 'u', 0, val);
892 else
894 /* Signed, or we couldn't turn an unsigned value into a
895 LONGEST. For signed values, one could assume two's
896 complement (a reasonable assumption, I think) and do
897 better than this. */
898 print_hex_chars (stream, (unsigned char *) valaddr,
899 TYPE_LENGTH (type), byte_order);
902 else
904 print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0,
905 unpack_long (type, valaddr));
909 void
910 val_print_type_code_flags (struct type *type, const gdb_byte *valaddr,
911 struct ui_file *stream)
913 ULONGEST val = unpack_long (type, valaddr);
914 int bitpos, nfields = TYPE_NFIELDS (type);
916 fputs_filtered ("[ ", stream);
917 for (bitpos = 0; bitpos < nfields; bitpos++)
919 if (TYPE_FIELD_BITPOS (type, bitpos) != -1
920 && (val & ((ULONGEST)1 << bitpos)))
922 if (TYPE_FIELD_NAME (type, bitpos))
923 fprintf_filtered (stream, "%s ", TYPE_FIELD_NAME (type, bitpos));
924 else
925 fprintf_filtered (stream, "#%d ", bitpos);
928 fputs_filtered ("]", stream);
931 /* Print a scalar of data of type TYPE, pointed to in GDB by VALADDR,
932 according to OPTIONS and SIZE on STREAM. Format i is not supported
933 at this level.
935 This is how the elements of an array or structure are printed
936 with a format. */
938 void
939 val_print_scalar_formatted (struct type *type,
940 const gdb_byte *valaddr, int embedded_offset,
941 const struct value *val,
942 const struct value_print_options *options,
943 int size,
944 struct ui_file *stream)
946 gdb_assert (val != NULL);
947 gdb_assert (valaddr == value_contents_for_printing_const (val));
949 /* If we get here with a string format, try again without it. Go
950 all the way back to the language printers, which may call us
951 again. */
952 if (options->format == 's')
954 struct value_print_options opts = *options;
955 opts.format = 0;
956 opts.deref_ref = 0;
957 val_print (type, valaddr, embedded_offset, 0, stream, 0, val, &opts,
958 current_language);
959 return;
962 /* A scalar object that does not have all bits available can't be
963 printed, because all bits contribute to its representation. */
964 if (!value_bits_valid (val, TARGET_CHAR_BIT * embedded_offset,
965 TARGET_CHAR_BIT * TYPE_LENGTH (type)))
966 val_print_optimized_out (stream);
967 else if (!value_bytes_available (val, embedded_offset, TYPE_LENGTH (type)))
968 val_print_unavailable (stream);
969 else
970 print_scalar_formatted (valaddr + embedded_offset, type,
971 options, size, stream);
974 /* Print a number according to FORMAT which is one of d,u,x,o,b,h,w,g.
975 The raison d'etre of this function is to consolidate printing of
976 LONG_LONG's into this one function. The format chars b,h,w,g are
977 from print_scalar_formatted(). Numbers are printed using C
978 format.
980 USE_C_FORMAT means to use C format in all cases. Without it,
981 'o' and 'x' format do not include the standard C radix prefix
982 (leading 0 or 0x).
984 Hilfinger/2004-09-09: USE_C_FORMAT was originally called USE_LOCAL
985 and was intended to request formating according to the current
986 language and would be used for most integers that GDB prints. The
987 exceptional cases were things like protocols where the format of
988 the integer is a protocol thing, not a user-visible thing). The
989 parameter remains to preserve the information of what things might
990 be printed with language-specific format, should we ever resurrect
991 that capability. */
993 void
994 print_longest (struct ui_file *stream, int format, int use_c_format,
995 LONGEST val_long)
997 const char *val;
999 switch (format)
1001 case 'd':
1002 val = int_string (val_long, 10, 1, 0, 1); break;
1003 case 'u':
1004 val = int_string (val_long, 10, 0, 0, 1); break;
1005 case 'x':
1006 val = int_string (val_long, 16, 0, 0, use_c_format); break;
1007 case 'b':
1008 val = int_string (val_long, 16, 0, 2, 1); break;
1009 case 'h':
1010 val = int_string (val_long, 16, 0, 4, 1); break;
1011 case 'w':
1012 val = int_string (val_long, 16, 0, 8, 1); break;
1013 case 'g':
1014 val = int_string (val_long, 16, 0, 16, 1); break;
1015 break;
1016 case 'o':
1017 val = int_string (val_long, 8, 0, 0, use_c_format); break;
1018 default:
1019 internal_error (__FILE__, __LINE__,
1020 _("failed internal consistency check"));
1022 fputs_filtered (val, stream);
1025 /* This used to be a macro, but I don't think it is called often enough
1026 to merit such treatment. */
1027 /* Convert a LONGEST to an int. This is used in contexts (e.g. number of
1028 arguments to a function, number in a value history, register number, etc.)
1029 where the value must not be larger than can fit in an int. */
1032 longest_to_int (LONGEST arg)
1034 /* Let the compiler do the work. */
1035 int rtnval = (int) arg;
1037 /* Check for overflows or underflows. */
1038 if (sizeof (LONGEST) > sizeof (int))
1040 if (rtnval != arg)
1042 error (_("Value out of range."));
1045 return (rtnval);
1048 /* Print a floating point value of type TYPE (not always a
1049 TYPE_CODE_FLT), pointed to in GDB by VALADDR, on STREAM. */
1051 void
1052 print_floating (const gdb_byte *valaddr, struct type *type,
1053 struct ui_file *stream)
1055 DOUBLEST doub;
1056 int inv;
1057 const struct floatformat *fmt = NULL;
1058 unsigned len = TYPE_LENGTH (type);
1059 enum float_kind kind;
1061 /* If it is a floating-point, check for obvious problems. */
1062 if (TYPE_CODE (type) == TYPE_CODE_FLT)
1063 fmt = floatformat_from_type (type);
1064 if (fmt != NULL)
1066 kind = floatformat_classify (fmt, valaddr);
1067 if (kind == float_nan)
1069 if (floatformat_is_negative (fmt, valaddr))
1070 fprintf_filtered (stream, "-");
1071 fprintf_filtered (stream, "nan(");
1072 fputs_filtered ("0x", stream);
1073 fputs_filtered (floatformat_mantissa (fmt, valaddr), stream);
1074 fprintf_filtered (stream, ")");
1075 return;
1077 else if (kind == float_infinite)
1079 if (floatformat_is_negative (fmt, valaddr))
1080 fputs_filtered ("-", stream);
1081 fputs_filtered ("inf", stream);
1082 return;
1086 /* NOTE: cagney/2002-01-15: The TYPE passed into print_floating()
1087 isn't necessarily a TYPE_CODE_FLT. Consequently, unpack_double
1088 needs to be used as that takes care of any necessary type
1089 conversions. Such conversions are of course direct to DOUBLEST
1090 and disregard any possible target floating point limitations.
1091 For instance, a u64 would be converted and displayed exactly on a
1092 host with 80 bit DOUBLEST but with loss of information on a host
1093 with 64 bit DOUBLEST. */
1095 doub = unpack_double (type, valaddr, &inv);
1096 if (inv)
1098 fprintf_filtered (stream, "<invalid float value>");
1099 return;
1102 /* FIXME: kettenis/2001-01-20: The following code makes too much
1103 assumptions about the host and target floating point format. */
1105 /* NOTE: cagney/2002-02-03: Since the TYPE of what was passed in may
1106 not necessarily be a TYPE_CODE_FLT, the below ignores that and
1107 instead uses the type's length to determine the precision of the
1108 floating-point value being printed. */
1110 if (len < sizeof (double))
1111 fprintf_filtered (stream, "%.9g", (double) doub);
1112 else if (len == sizeof (double))
1113 fprintf_filtered (stream, "%.17g", (double) doub);
1114 else
1115 #ifdef PRINTF_HAS_LONG_DOUBLE
1116 fprintf_filtered (stream, "%.35Lg", doub);
1117 #else
1118 /* This at least wins with values that are representable as
1119 doubles. */
1120 fprintf_filtered (stream, "%.17g", (double) doub);
1121 #endif
1124 void
1125 print_decimal_floating (const gdb_byte *valaddr, struct type *type,
1126 struct ui_file *stream)
1128 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
1129 char decstr[MAX_DECIMAL_STRING];
1130 unsigned len = TYPE_LENGTH (type);
1132 decimal_to_string (valaddr, len, byte_order, decstr);
1133 fputs_filtered (decstr, stream);
1134 return;
1137 void
1138 print_binary_chars (struct ui_file *stream, const gdb_byte *valaddr,
1139 unsigned len, enum bfd_endian byte_order)
1142 #define BITS_IN_BYTES 8
1144 const gdb_byte *p;
1145 unsigned int i;
1146 int b;
1148 /* Declared "int" so it will be signed.
1149 This ensures that right shift will shift in zeros. */
1151 const int mask = 0x080;
1153 /* FIXME: We should be not printing leading zeroes in most cases. */
1155 if (byte_order == BFD_ENDIAN_BIG)
1157 for (p = valaddr;
1158 p < valaddr + len;
1159 p++)
1161 /* Every byte has 8 binary characters; peel off
1162 and print from the MSB end. */
1164 for (i = 0; i < (BITS_IN_BYTES * sizeof (*p)); i++)
1166 if (*p & (mask >> i))
1167 b = 1;
1168 else
1169 b = 0;
1171 fprintf_filtered (stream, "%1d", b);
1175 else
1177 for (p = valaddr + len - 1;
1178 p >= valaddr;
1179 p--)
1181 for (i = 0; i < (BITS_IN_BYTES * sizeof (*p)); i++)
1183 if (*p & (mask >> i))
1184 b = 1;
1185 else
1186 b = 0;
1188 fprintf_filtered (stream, "%1d", b);
1194 /* VALADDR points to an integer of LEN bytes.
1195 Print it in octal on stream or format it in buf. */
1197 void
1198 print_octal_chars (struct ui_file *stream, const gdb_byte *valaddr,
1199 unsigned len, enum bfd_endian byte_order)
1201 const gdb_byte *p;
1202 unsigned char octa1, octa2, octa3, carry;
1203 int cycle;
1205 /* FIXME: We should be not printing leading zeroes in most cases. */
1208 /* Octal is 3 bits, which doesn't fit. Yuk. So we have to track
1209 * the extra bits, which cycle every three bytes:
1211 * Byte side: 0 1 2 3
1212 * | | | |
1213 * bit number 123 456 78 | 9 012 345 6 | 78 901 234 | 567 890 12 |
1215 * Octal side: 0 1 carry 3 4 carry ...
1217 * Cycle number: 0 1 2
1219 * But of course we are printing from the high side, so we have to
1220 * figure out where in the cycle we are so that we end up with no
1221 * left over bits at the end.
1223 #define BITS_IN_OCTAL 3
1224 #define HIGH_ZERO 0340
1225 #define LOW_ZERO 0016
1226 #define CARRY_ZERO 0003
1227 #define HIGH_ONE 0200
1228 #define MID_ONE 0160
1229 #define LOW_ONE 0016
1230 #define CARRY_ONE 0001
1231 #define HIGH_TWO 0300
1232 #define MID_TWO 0070
1233 #define LOW_TWO 0007
1235 /* For 32 we start in cycle 2, with two bits and one bit carry;
1236 for 64 in cycle in cycle 1, with one bit and a two bit carry. */
1238 cycle = (len * BITS_IN_BYTES) % BITS_IN_OCTAL;
1239 carry = 0;
1241 fputs_filtered ("0", stream);
1242 if (byte_order == BFD_ENDIAN_BIG)
1244 for (p = valaddr;
1245 p < valaddr + len;
1246 p++)
1248 switch (cycle)
1250 case 0:
1251 /* No carry in, carry out two bits. */
1253 octa1 = (HIGH_ZERO & *p) >> 5;
1254 octa2 = (LOW_ZERO & *p) >> 2;
1255 carry = (CARRY_ZERO & *p);
1256 fprintf_filtered (stream, "%o", octa1);
1257 fprintf_filtered (stream, "%o", octa2);
1258 break;
1260 case 1:
1261 /* Carry in two bits, carry out one bit. */
1263 octa1 = (carry << 1) | ((HIGH_ONE & *p) >> 7);
1264 octa2 = (MID_ONE & *p) >> 4;
1265 octa3 = (LOW_ONE & *p) >> 1;
1266 carry = (CARRY_ONE & *p);
1267 fprintf_filtered (stream, "%o", octa1);
1268 fprintf_filtered (stream, "%o", octa2);
1269 fprintf_filtered (stream, "%o", octa3);
1270 break;
1272 case 2:
1273 /* Carry in one bit, no carry out. */
1275 octa1 = (carry << 2) | ((HIGH_TWO & *p) >> 6);
1276 octa2 = (MID_TWO & *p) >> 3;
1277 octa3 = (LOW_TWO & *p);
1278 carry = 0;
1279 fprintf_filtered (stream, "%o", octa1);
1280 fprintf_filtered (stream, "%o", octa2);
1281 fprintf_filtered (stream, "%o", octa3);
1282 break;
1284 default:
1285 error (_("Internal error in octal conversion;"));
1288 cycle++;
1289 cycle = cycle % BITS_IN_OCTAL;
1292 else
1294 for (p = valaddr + len - 1;
1295 p >= valaddr;
1296 p--)
1298 switch (cycle)
1300 case 0:
1301 /* Carry out, no carry in */
1303 octa1 = (HIGH_ZERO & *p) >> 5;
1304 octa2 = (LOW_ZERO & *p) >> 2;
1305 carry = (CARRY_ZERO & *p);
1306 fprintf_filtered (stream, "%o", octa1);
1307 fprintf_filtered (stream, "%o", octa2);
1308 break;
1310 case 1:
1311 /* Carry in, carry out */
1313 octa1 = (carry << 1) | ((HIGH_ONE & *p) >> 7);
1314 octa2 = (MID_ONE & *p) >> 4;
1315 octa3 = (LOW_ONE & *p) >> 1;
1316 carry = (CARRY_ONE & *p);
1317 fprintf_filtered (stream, "%o", octa1);
1318 fprintf_filtered (stream, "%o", octa2);
1319 fprintf_filtered (stream, "%o", octa3);
1320 break;
1322 case 2:
1323 /* Carry in, no carry out */
1325 octa1 = (carry << 2) | ((HIGH_TWO & *p) >> 6);
1326 octa2 = (MID_TWO & *p) >> 3;
1327 octa3 = (LOW_TWO & *p);
1328 carry = 0;
1329 fprintf_filtered (stream, "%o", octa1);
1330 fprintf_filtered (stream, "%o", octa2);
1331 fprintf_filtered (stream, "%o", octa3);
1332 break;
1334 default:
1335 error (_("Internal error in octal conversion;"));
1338 cycle++;
1339 cycle = cycle % BITS_IN_OCTAL;
1345 /* VALADDR points to an integer of LEN bytes.
1346 Print it in decimal on stream or format it in buf. */
1348 void
1349 print_decimal_chars (struct ui_file *stream, const gdb_byte *valaddr,
1350 unsigned len, enum bfd_endian byte_order)
1352 #define TEN 10
1353 #define CARRY_OUT( x ) ((x) / TEN) /* extend char to int */
1354 #define CARRY_LEFT( x ) ((x) % TEN)
1355 #define SHIFT( x ) ((x) << 4)
1356 #define LOW_NIBBLE( x ) ( (x) & 0x00F)
1357 #define HIGH_NIBBLE( x ) (((x) & 0x0F0) >> 4)
1359 const gdb_byte *p;
1360 unsigned char *digits;
1361 int carry;
1362 int decimal_len;
1363 int i, j, decimal_digits;
1364 int dummy;
1365 int flip;
1367 /* Base-ten number is less than twice as many digits
1368 as the base 16 number, which is 2 digits per byte. */
1370 decimal_len = len * 2 * 2;
1371 digits = xmalloc (decimal_len);
1373 for (i = 0; i < decimal_len; i++)
1375 digits[i] = 0;
1378 /* Ok, we have an unknown number of bytes of data to be printed in
1379 * decimal.
1381 * Given a hex number (in nibbles) as XYZ, we start by taking X and
1382 * decemalizing it as "x1 x2" in two decimal nibbles. Then we multiply
1383 * the nibbles by 16, add Y and re-decimalize. Repeat with Z.
1385 * The trick is that "digits" holds a base-10 number, but sometimes
1386 * the individual digits are > 10.
1388 * Outer loop is per nibble (hex digit) of input, from MSD end to
1389 * LSD end.
1391 decimal_digits = 0; /* Number of decimal digits so far */
1392 p = (byte_order == BFD_ENDIAN_BIG) ? valaddr : valaddr + len - 1;
1393 flip = 0;
1394 while ((byte_order == BFD_ENDIAN_BIG) ? (p < valaddr + len) : (p >= valaddr))
1397 * Multiply current base-ten number by 16 in place.
1398 * Each digit was between 0 and 9, now is between
1399 * 0 and 144.
1401 for (j = 0; j < decimal_digits; j++)
1403 digits[j] = SHIFT (digits[j]);
1406 /* Take the next nibble off the input and add it to what
1407 * we've got in the LSB position. Bottom 'digit' is now
1408 * between 0 and 159.
1410 * "flip" is used to run this loop twice for each byte.
1412 if (flip == 0)
1414 /* Take top nibble. */
1416 digits[0] += HIGH_NIBBLE (*p);
1417 flip = 1;
1419 else
1421 /* Take low nibble and bump our pointer "p". */
1423 digits[0] += LOW_NIBBLE (*p);
1424 if (byte_order == BFD_ENDIAN_BIG)
1425 p++;
1426 else
1427 p--;
1428 flip = 0;
1431 /* Re-decimalize. We have to do this often enough
1432 * that we don't overflow, but once per nibble is
1433 * overkill. Easier this way, though. Note that the
1434 * carry is often larger than 10 (e.g. max initial
1435 * carry out of lowest nibble is 15, could bubble all
1436 * the way up greater than 10). So we have to do
1437 * the carrying beyond the last current digit.
1439 carry = 0;
1440 for (j = 0; j < decimal_len - 1; j++)
1442 digits[j] += carry;
1444 /* "/" won't handle an unsigned char with
1445 * a value that if signed would be negative.
1446 * So extend to longword int via "dummy".
1448 dummy = digits[j];
1449 carry = CARRY_OUT (dummy);
1450 digits[j] = CARRY_LEFT (dummy);
1452 if (j >= decimal_digits && carry == 0)
1455 * All higher digits are 0 and we
1456 * no longer have a carry.
1458 * Note: "j" is 0-based, "decimal_digits" is
1459 * 1-based.
1461 decimal_digits = j + 1;
1462 break;
1467 /* Ok, now "digits" is the decimal representation, with
1468 the "decimal_digits" actual digits. Print! */
1470 for (i = decimal_digits - 1; i >= 0; i--)
1472 fprintf_filtered (stream, "%1d", digits[i]);
1474 xfree (digits);
1477 /* VALADDR points to an integer of LEN bytes. Print it in hex on stream. */
1479 void
1480 print_hex_chars (struct ui_file *stream, const gdb_byte *valaddr,
1481 unsigned len, enum bfd_endian byte_order)
1483 const gdb_byte *p;
1485 /* FIXME: We should be not printing leading zeroes in most cases. */
1487 fputs_filtered ("0x", stream);
1488 if (byte_order == BFD_ENDIAN_BIG)
1490 for (p = valaddr;
1491 p < valaddr + len;
1492 p++)
1494 fprintf_filtered (stream, "%02x", *p);
1497 else
1499 for (p = valaddr + len - 1;
1500 p >= valaddr;
1501 p--)
1503 fprintf_filtered (stream, "%02x", *p);
1508 /* VALADDR points to a char integer of LEN bytes.
1509 Print it out in appropriate language form on stream.
1510 Omit any leading zero chars. */
1512 void
1513 print_char_chars (struct ui_file *stream, struct type *type,
1514 const gdb_byte *valaddr,
1515 unsigned len, enum bfd_endian byte_order)
1517 const gdb_byte *p;
1519 if (byte_order == BFD_ENDIAN_BIG)
1521 p = valaddr;
1522 while (p < valaddr + len - 1 && *p == 0)
1523 ++p;
1525 while (p < valaddr + len)
1527 LA_EMIT_CHAR (*p, type, stream, '\'');
1528 ++p;
1531 else
1533 p = valaddr + len - 1;
1534 while (p > valaddr && *p == 0)
1535 --p;
1537 while (p >= valaddr)
1539 LA_EMIT_CHAR (*p, type, stream, '\'');
1540 --p;
1545 /* Print function pointer with inferior address ADDRESS onto stdio
1546 stream STREAM. */
1548 void
1549 print_function_pointer_address (const struct value_print_options *options,
1550 struct gdbarch *gdbarch,
1551 CORE_ADDR address,
1552 struct ui_file *stream)
1554 CORE_ADDR func_addr
1555 = gdbarch_convert_from_func_ptr_addr (gdbarch, address,
1556 &current_target);
1558 /* If the function pointer is represented by a description, print
1559 the address of the description. */
1560 if (options->addressprint && func_addr != address)
1562 fputs_filtered ("@", stream);
1563 fputs_filtered (paddress (gdbarch, address), stream);
1564 fputs_filtered (": ", stream);
1566 print_address_demangle (options, gdbarch, func_addr, stream, demangle);
1570 /* Print on STREAM using the given OPTIONS the index for the element
1571 at INDEX of an array whose index type is INDEX_TYPE. */
1573 void
1574 maybe_print_array_index (struct type *index_type, LONGEST index,
1575 struct ui_file *stream,
1576 const struct value_print_options *options)
1578 struct value *index_value;
1580 if (!options->print_array_indexes)
1581 return;
1583 index_value = value_from_longest (index_type, index);
1585 LA_PRINT_ARRAY_INDEX (index_value, stream, options);
1588 /* Called by various <lang>_val_print routines to print elements of an
1589 array in the form "<elem1>, <elem2>, <elem3>, ...".
1591 (FIXME?) Assumes array element separator is a comma, which is correct
1592 for all languages currently handled.
1593 (FIXME?) Some languages have a notation for repeated array elements,
1594 perhaps we should try to use that notation when appropriate. */
1596 void
1597 val_print_array_elements (struct type *type,
1598 const gdb_byte *valaddr, int embedded_offset,
1599 CORE_ADDR address, struct ui_file *stream,
1600 int recurse,
1601 const struct value *val,
1602 const struct value_print_options *options,
1603 unsigned int i)
1605 unsigned int things_printed = 0;
1606 unsigned len;
1607 struct type *elttype, *index_type;
1608 unsigned eltlen;
1609 /* Position of the array element we are examining to see
1610 whether it is repeated. */
1611 unsigned int rep1;
1612 /* Number of repetitions we have detected so far. */
1613 unsigned int reps;
1614 LONGEST low_bound, high_bound;
1616 elttype = TYPE_TARGET_TYPE (type);
1617 eltlen = TYPE_LENGTH (check_typedef (elttype));
1618 index_type = TYPE_INDEX_TYPE (type);
1620 if (get_array_bounds (type, &low_bound, &high_bound))
1622 /* The array length should normally be HIGH_BOUND - LOW_BOUND + 1.
1623 But we have to be a little extra careful, because some languages
1624 such as Ada allow LOW_BOUND to be greater than HIGH_BOUND for
1625 empty arrays. In that situation, the array length is just zero,
1626 not negative! */
1627 if (low_bound > high_bound)
1628 len = 0;
1629 else
1630 len = high_bound - low_bound + 1;
1632 else
1634 warning (_("unable to get bounds of array, assuming null array"));
1635 low_bound = 0;
1636 len = 0;
1639 annotate_array_section_begin (i, elttype);
1641 for (; i < len && things_printed < options->print_max; i++)
1643 if (i != 0)
1645 if (options->prettyprint_arrays)
1647 fprintf_filtered (stream, ",\n");
1648 print_spaces_filtered (2 + 2 * recurse, stream);
1650 else
1652 fprintf_filtered (stream, ", ");
1655 wrap_here (n_spaces (2 + 2 * recurse));
1656 maybe_print_array_index (index_type, i + low_bound,
1657 stream, options);
1659 rep1 = i + 1;
1660 reps = 1;
1661 /* Only check for reps if repeat_count_threshold is not set to
1662 UINT_MAX (unlimited). */
1663 if (options->repeat_count_threshold < UINT_MAX)
1665 while (rep1 < len
1666 && value_available_contents_eq (val,
1667 embedded_offset + i * eltlen,
1668 val,
1669 (embedded_offset
1670 + rep1 * eltlen),
1671 eltlen))
1673 ++reps;
1674 ++rep1;
1678 if (reps > options->repeat_count_threshold)
1680 val_print (elttype, valaddr, embedded_offset + i * eltlen,
1681 address, stream, recurse + 1, val, options,
1682 current_language);
1683 annotate_elt_rep (reps);
1684 fprintf_filtered (stream, " <repeats %u times>", reps);
1685 annotate_elt_rep_end ();
1687 i = rep1 - 1;
1688 things_printed += options->repeat_count_threshold;
1690 else
1692 val_print (elttype, valaddr, embedded_offset + i * eltlen,
1693 address,
1694 stream, recurse + 1, val, options, current_language);
1695 annotate_elt ();
1696 things_printed++;
1699 annotate_array_section_end ();
1700 if (i < len)
1702 fprintf_filtered (stream, "...");
1706 /* Read LEN bytes of target memory at address MEMADDR, placing the
1707 results in GDB's memory at MYADDR. Returns a count of the bytes
1708 actually read, and optionally an errno value in the location
1709 pointed to by ERRNOPTR if ERRNOPTR is non-null. */
1711 /* FIXME: cagney/1999-10-14: Only used by val_print_string. Can this
1712 function be eliminated. */
1714 static int
1715 partial_memory_read (CORE_ADDR memaddr, gdb_byte *myaddr,
1716 int len, int *errnoptr)
1718 int nread; /* Number of bytes actually read. */
1719 int errcode; /* Error from last read. */
1721 /* First try a complete read. */
1722 errcode = target_read_memory (memaddr, myaddr, len);
1723 if (errcode == 0)
1725 /* Got it all. */
1726 nread = len;
1728 else
1730 /* Loop, reading one byte at a time until we get as much as we can. */
1731 for (errcode = 0, nread = 0; len > 0 && errcode == 0; nread++, len--)
1733 errcode = target_read_memory (memaddr++, myaddr++, 1);
1735 /* If an error, the last read was unsuccessful, so adjust count. */
1736 if (errcode != 0)
1738 nread--;
1741 if (errnoptr != NULL)
1743 *errnoptr = errcode;
1745 return (nread);
1748 /* Read a string from the inferior, at ADDR, with LEN characters of WIDTH bytes
1749 each. Fetch at most FETCHLIMIT characters. BUFFER will be set to a newly
1750 allocated buffer containing the string, which the caller is responsible to
1751 free, and BYTES_READ will be set to the number of bytes read. Returns 0 on
1752 success, or errno on failure.
1754 If LEN > 0, reads exactly LEN characters (including eventual NULs in
1755 the middle or end of the string). If LEN is -1, stops at the first
1756 null character (not necessarily the first null byte) up to a maximum
1757 of FETCHLIMIT characters. Set FETCHLIMIT to UINT_MAX to read as many
1758 characters as possible from the string.
1760 Unless an exception is thrown, BUFFER will always be allocated, even on
1761 failure. In this case, some characters might have been read before the
1762 failure happened. Check BYTES_READ to recognize this situation.
1764 Note: There was a FIXME asking to make this code use target_read_string,
1765 but this function is more general (can read past null characters, up to
1766 given LEN). Besides, it is used much more often than target_read_string
1767 so it is more tested. Perhaps callers of target_read_string should use
1768 this function instead? */
1771 read_string (CORE_ADDR addr, int len, int width, unsigned int fetchlimit,
1772 enum bfd_endian byte_order, gdb_byte **buffer, int *bytes_read)
1774 int found_nul; /* Non-zero if we found the nul char. */
1775 int errcode; /* Errno returned from bad reads. */
1776 unsigned int nfetch; /* Chars to fetch / chars fetched. */
1777 unsigned int chunksize; /* Size of each fetch, in chars. */
1778 gdb_byte *bufptr; /* Pointer to next available byte in
1779 buffer. */
1780 gdb_byte *limit; /* First location past end of fetch buffer. */
1781 struct cleanup *old_chain = NULL; /* Top of the old cleanup chain. */
1783 /* Decide how large of chunks to try to read in one operation. This
1784 is also pretty simple. If LEN >= zero, then we want fetchlimit chars,
1785 so we might as well read them all in one operation. If LEN is -1, we
1786 are looking for a NUL terminator to end the fetching, so we might as
1787 well read in blocks that are large enough to be efficient, but not so
1788 large as to be slow if fetchlimit happens to be large. So we choose the
1789 minimum of 8 and fetchlimit. We used to use 200 instead of 8 but
1790 200 is way too big for remote debugging over a serial line. */
1792 chunksize = (len == -1 ? min (8, fetchlimit) : fetchlimit);
1794 /* Loop until we either have all the characters, or we encounter
1795 some error, such as bumping into the end of the address space. */
1797 found_nul = 0;
1798 *buffer = NULL;
1800 old_chain = make_cleanup (free_current_contents, buffer);
1802 if (len > 0)
1804 *buffer = (gdb_byte *) xmalloc (len * width);
1805 bufptr = *buffer;
1807 nfetch = partial_memory_read (addr, bufptr, len * width, &errcode)
1808 / width;
1809 addr += nfetch * width;
1810 bufptr += nfetch * width;
1812 else if (len == -1)
1814 unsigned long bufsize = 0;
1818 QUIT;
1819 nfetch = min (chunksize, fetchlimit - bufsize);
1821 if (*buffer == NULL)
1822 *buffer = (gdb_byte *) xmalloc (nfetch * width);
1823 else
1824 *buffer = (gdb_byte *) xrealloc (*buffer,
1825 (nfetch + bufsize) * width);
1827 bufptr = *buffer + bufsize * width;
1828 bufsize += nfetch;
1830 /* Read as much as we can. */
1831 nfetch = partial_memory_read (addr, bufptr, nfetch * width, &errcode)
1832 / width;
1834 /* Scan this chunk for the null character that terminates the string
1835 to print. If found, we don't need to fetch any more. Note
1836 that bufptr is explicitly left pointing at the next character
1837 after the null character, or at the next character after the end
1838 of the buffer. */
1840 limit = bufptr + nfetch * width;
1841 while (bufptr < limit)
1843 unsigned long c;
1845 c = extract_unsigned_integer (bufptr, width, byte_order);
1846 addr += width;
1847 bufptr += width;
1848 if (c == 0)
1850 /* We don't care about any error which happened after
1851 the NUL terminator. */
1852 errcode = 0;
1853 found_nul = 1;
1854 break;
1858 while (errcode == 0 /* no error */
1859 && bufptr - *buffer < fetchlimit * width /* no overrun */
1860 && !found_nul); /* haven't found NUL yet */
1862 else
1863 { /* Length of string is really 0! */
1864 /* We always allocate *buffer. */
1865 *buffer = bufptr = xmalloc (1);
1866 errcode = 0;
1869 /* bufptr and addr now point immediately beyond the last byte which we
1870 consider part of the string (including a '\0' which ends the string). */
1871 *bytes_read = bufptr - *buffer;
1873 QUIT;
1875 discard_cleanups (old_chain);
1877 return errcode;
1880 /* Return true if print_wchar can display W without resorting to a
1881 numeric escape, false otherwise. */
1883 static int
1884 wchar_printable (gdb_wchar_t w)
1886 return (gdb_iswprint (w)
1887 || w == LCST ('\a') || w == LCST ('\b')
1888 || w == LCST ('\f') || w == LCST ('\n')
1889 || w == LCST ('\r') || w == LCST ('\t')
1890 || w == LCST ('\v'));
1893 /* A helper function that converts the contents of STRING to wide
1894 characters and then appends them to OUTPUT. */
1896 static void
1897 append_string_as_wide (const char *string,
1898 struct obstack *output)
1900 for (; *string; ++string)
1902 gdb_wchar_t w = gdb_btowc (*string);
1903 obstack_grow (output, &w, sizeof (gdb_wchar_t));
1907 /* Print a wide character W to OUTPUT. ORIG is a pointer to the
1908 original (target) bytes representing the character, ORIG_LEN is the
1909 number of valid bytes. WIDTH is the number of bytes in a base
1910 characters of the type. OUTPUT is an obstack to which wide
1911 characters are emitted. QUOTER is a (narrow) character indicating
1912 the style of quotes surrounding the character to be printed.
1913 NEED_ESCAPE is an in/out flag which is used to track numeric
1914 escapes across calls. */
1916 static void
1917 print_wchar (gdb_wint_t w, const gdb_byte *orig,
1918 int orig_len, int width,
1919 enum bfd_endian byte_order,
1920 struct obstack *output,
1921 int quoter, int *need_escapep)
1923 int need_escape = *need_escapep;
1925 *need_escapep = 0;
1926 if (gdb_iswprint (w) && (!need_escape || (!gdb_iswdigit (w)
1927 && w != LCST ('8')
1928 && w != LCST ('9'))))
1930 gdb_wchar_t wchar = w;
1932 if (w == gdb_btowc (quoter) || w == LCST ('\\'))
1933 obstack_grow_wstr (output, LCST ("\\"));
1934 obstack_grow (output, &wchar, sizeof (gdb_wchar_t));
1936 else
1938 switch (w)
1940 case LCST ('\a'):
1941 obstack_grow_wstr (output, LCST ("\\a"));
1942 break;
1943 case LCST ('\b'):
1944 obstack_grow_wstr (output, LCST ("\\b"));
1945 break;
1946 case LCST ('\f'):
1947 obstack_grow_wstr (output, LCST ("\\f"));
1948 break;
1949 case LCST ('\n'):
1950 obstack_grow_wstr (output, LCST ("\\n"));
1951 break;
1952 case LCST ('\r'):
1953 obstack_grow_wstr (output, LCST ("\\r"));
1954 break;
1955 case LCST ('\t'):
1956 obstack_grow_wstr (output, LCST ("\\t"));
1957 break;
1958 case LCST ('\v'):
1959 obstack_grow_wstr (output, LCST ("\\v"));
1960 break;
1961 default:
1963 int i;
1965 for (i = 0; i + width <= orig_len; i += width)
1967 char octal[30];
1968 ULONGEST value;
1970 value = extract_unsigned_integer (&orig[i], width,
1971 byte_order);
1972 /* If the value fits in 3 octal digits, print it that
1973 way. Otherwise, print it as a hex escape. */
1974 if (value <= 0777)
1975 xsnprintf (octal, sizeof (octal), "\\%.3o",
1976 (int) (value & 0777));
1977 else
1978 xsnprintf (octal, sizeof (octal), "\\x%lx", (long) value);
1979 append_string_as_wide (octal, output);
1981 /* If we somehow have extra bytes, print them now. */
1982 while (i < orig_len)
1984 char octal[5];
1986 xsnprintf (octal, sizeof (octal), "\\%.3o", orig[i] & 0xff);
1987 append_string_as_wide (octal, output);
1988 ++i;
1991 *need_escapep = 1;
1993 break;
1998 /* Print the character C on STREAM as part of the contents of a
1999 literal string whose delimiter is QUOTER. ENCODING names the
2000 encoding of C. */
2002 void
2003 generic_emit_char (int c, struct type *type, struct ui_file *stream,
2004 int quoter, const char *encoding)
2006 enum bfd_endian byte_order
2007 = gdbarch_byte_order (get_type_arch (type));
2008 struct obstack wchar_buf, output;
2009 struct cleanup *cleanups;
2010 gdb_byte *buf;
2011 struct wchar_iterator *iter;
2012 int need_escape = 0;
2014 buf = alloca (TYPE_LENGTH (type));
2015 pack_long (buf, type, c);
2017 iter = make_wchar_iterator (buf, TYPE_LENGTH (type),
2018 encoding, TYPE_LENGTH (type));
2019 cleanups = make_cleanup_wchar_iterator (iter);
2021 /* This holds the printable form of the wchar_t data. */
2022 obstack_init (&wchar_buf);
2023 make_cleanup_obstack_free (&wchar_buf);
2025 while (1)
2027 int num_chars;
2028 gdb_wchar_t *chars;
2029 const gdb_byte *buf;
2030 size_t buflen;
2031 int print_escape = 1;
2032 enum wchar_iterate_result result;
2034 num_chars = wchar_iterate (iter, &result, &chars, &buf, &buflen);
2035 if (num_chars < 0)
2036 break;
2037 if (num_chars > 0)
2039 /* If all characters are printable, print them. Otherwise,
2040 we're going to have to print an escape sequence. We
2041 check all characters because we want to print the target
2042 bytes in the escape sequence, and we don't know character
2043 boundaries there. */
2044 int i;
2046 print_escape = 0;
2047 for (i = 0; i < num_chars; ++i)
2048 if (!wchar_printable (chars[i]))
2050 print_escape = 1;
2051 break;
2054 if (!print_escape)
2056 for (i = 0; i < num_chars; ++i)
2057 print_wchar (chars[i], buf, buflen,
2058 TYPE_LENGTH (type), byte_order,
2059 &wchar_buf, quoter, &need_escape);
2063 /* This handles the NUM_CHARS == 0 case as well. */
2064 if (print_escape)
2065 print_wchar (gdb_WEOF, buf, buflen, TYPE_LENGTH (type),
2066 byte_order, &wchar_buf, quoter, &need_escape);
2069 /* The output in the host encoding. */
2070 obstack_init (&output);
2071 make_cleanup_obstack_free (&output);
2073 convert_between_encodings (INTERMEDIATE_ENCODING, host_charset (),
2074 (gdb_byte *) obstack_base (&wchar_buf),
2075 obstack_object_size (&wchar_buf),
2076 sizeof (gdb_wchar_t), &output, translit_char);
2077 obstack_1grow (&output, '\0');
2079 fputs_filtered (obstack_base (&output), stream);
2081 do_cleanups (cleanups);
2084 /* Return the repeat count of the next character/byte in ITER,
2085 storing the result in VEC. */
2087 static int
2088 count_next_character (struct wchar_iterator *iter,
2089 VEC (converted_character_d) **vec)
2091 struct converted_character *current;
2093 if (VEC_empty (converted_character_d, *vec))
2095 struct converted_character tmp;
2096 gdb_wchar_t *chars;
2098 tmp.num_chars
2099 = wchar_iterate (iter, &tmp.result, &chars, &tmp.buf, &tmp.buflen);
2100 if (tmp.num_chars > 0)
2102 gdb_assert (tmp.num_chars < MAX_WCHARS);
2103 memcpy (tmp.chars, chars, tmp.num_chars * sizeof (gdb_wchar_t));
2105 VEC_safe_push (converted_character_d, *vec, &tmp);
2108 current = VEC_last (converted_character_d, *vec);
2110 /* Count repeated characters or bytes. */
2111 current->repeat_count = 1;
2112 if (current->num_chars == -1)
2114 /* EOF */
2115 return -1;
2117 else
2119 gdb_wchar_t *chars;
2120 struct converted_character d;
2121 int repeat;
2123 d.repeat_count = 0;
2125 while (1)
2127 /* Get the next character. */
2128 d.num_chars
2129 = wchar_iterate (iter, &d.result, &chars, &d.buf, &d.buflen);
2131 /* If a character was successfully converted, save the character
2132 into the converted character. */
2133 if (d.num_chars > 0)
2135 gdb_assert (d.num_chars < MAX_WCHARS);
2136 memcpy (d.chars, chars, WCHAR_BUFLEN (d.num_chars));
2139 /* Determine if the current character is the same as this
2140 new character. */
2141 if (d.num_chars == current->num_chars && d.result == current->result)
2143 /* There are two cases to consider:
2145 1) Equality of converted character (num_chars > 0)
2146 2) Equality of non-converted character (num_chars == 0) */
2147 if ((current->num_chars > 0
2148 && memcmp (current->chars, d.chars,
2149 WCHAR_BUFLEN (current->num_chars)) == 0)
2150 || (current->num_chars == 0
2151 && current->buflen == d.buflen
2152 && memcmp (current->buf, d.buf, current->buflen) == 0))
2153 ++current->repeat_count;
2154 else
2155 break;
2157 else
2158 break;
2161 /* Push this next converted character onto the result vector. */
2162 repeat = current->repeat_count;
2163 VEC_safe_push (converted_character_d, *vec, &d);
2164 return repeat;
2168 /* Print the characters in CHARS to the OBSTACK. QUOTE_CHAR is the quote
2169 character to use with string output. WIDTH is the size of the output
2170 character type. BYTE_ORDER is the the target byte order. OPTIONS
2171 is the user's print options. */
2173 static void
2174 print_converted_chars_to_obstack (struct obstack *obstack,
2175 VEC (converted_character_d) *chars,
2176 int quote_char, int width,
2177 enum bfd_endian byte_order,
2178 const struct value_print_options *options)
2180 unsigned int idx;
2181 struct converted_character *elem;
2182 enum {START, SINGLE, REPEAT, INCOMPLETE, FINISH} state, last;
2183 gdb_wchar_t wide_quote_char = gdb_btowc (quote_char);
2184 int need_escape = 0;
2186 /* Set the start state. */
2187 idx = 0;
2188 last = state = START;
2189 elem = NULL;
2191 while (1)
2193 switch (state)
2195 case START:
2196 /* Nothing to do. */
2197 break;
2199 case SINGLE:
2201 int j;
2203 /* We are outputting a single character
2204 (< options->repeat_count_threshold). */
2206 if (last != SINGLE)
2208 /* We were outputting some other type of content, so we
2209 must output and a comma and a quote. */
2210 if (last != START)
2211 obstack_grow_wstr (obstack, LCST (", "));
2212 obstack_grow (obstack, &wide_quote_char, sizeof (gdb_wchar_t));
2214 /* Output the character. */
2215 for (j = 0; j < elem->repeat_count; ++j)
2217 if (elem->result == wchar_iterate_ok)
2218 print_wchar (elem->chars[0], elem->buf, elem->buflen, width,
2219 byte_order, obstack, quote_char, &need_escape);
2220 else
2221 print_wchar (gdb_WEOF, elem->buf, elem->buflen, width,
2222 byte_order, obstack, quote_char, &need_escape);
2225 break;
2227 case REPEAT:
2229 int j;
2230 char *s;
2232 /* We are outputting a character with a repeat count
2233 greater than options->repeat_count_threshold. */
2235 if (last == SINGLE)
2237 /* We were outputting a single string. Terminate the
2238 string. */
2239 obstack_grow (obstack, &wide_quote_char, sizeof (gdb_wchar_t));
2241 if (last != START)
2242 obstack_grow_wstr (obstack, LCST (", "));
2244 /* Output the character and repeat string. */
2245 obstack_grow_wstr (obstack, LCST ("'"));
2246 if (elem->result == wchar_iterate_ok)
2247 print_wchar (elem->chars[0], elem->buf, elem->buflen, width,
2248 byte_order, obstack, quote_char, &need_escape);
2249 else
2250 print_wchar (gdb_WEOF, elem->buf, elem->buflen, width,
2251 byte_order, obstack, quote_char, &need_escape);
2252 obstack_grow_wstr (obstack, LCST ("'"));
2253 s = xstrprintf (_(" <repeats %u times>"), elem->repeat_count);
2254 for (j = 0; s[j]; ++j)
2256 gdb_wchar_t w = gdb_btowc (s[j]);
2257 obstack_grow (obstack, &w, sizeof (gdb_wchar_t));
2259 xfree (s);
2261 break;
2263 case INCOMPLETE:
2264 /* We are outputting an incomplete sequence. */
2265 if (last == SINGLE)
2267 /* If we were outputting a string of SINGLE characters,
2268 terminate the quote. */
2269 obstack_grow (obstack, &wide_quote_char, sizeof (gdb_wchar_t));
2271 if (last != START)
2272 obstack_grow_wstr (obstack, LCST (", "));
2274 /* Output the incomplete sequence string. */
2275 obstack_grow_wstr (obstack, LCST ("<incomplete sequence "));
2276 print_wchar (gdb_WEOF, elem->buf, elem->buflen, width, byte_order,
2277 obstack, 0, &need_escape);
2278 obstack_grow_wstr (obstack, LCST (">"));
2280 /* We do not attempt to outupt anything after this. */
2281 state = FINISH;
2282 break;
2284 case FINISH:
2285 /* All done. If we were outputting a string of SINGLE
2286 characters, the string must be terminated. Otherwise,
2287 REPEAT and INCOMPLETE are always left properly terminated. */
2288 if (last == SINGLE)
2289 obstack_grow (obstack, &wide_quote_char, sizeof (gdb_wchar_t));
2291 return;
2294 /* Get the next element and state. */
2295 last = state;
2296 if (state != FINISH)
2298 elem = VEC_index (converted_character_d, chars, idx++);
2299 switch (elem->result)
2301 case wchar_iterate_ok:
2302 case wchar_iterate_invalid:
2303 if (elem->repeat_count > options->repeat_count_threshold)
2304 state = REPEAT;
2305 else
2306 state = SINGLE;
2307 break;
2309 case wchar_iterate_incomplete:
2310 state = INCOMPLETE;
2311 break;
2313 case wchar_iterate_eof:
2314 state = FINISH;
2315 break;
2321 /* Print the character string STRING, printing at most LENGTH
2322 characters. LENGTH is -1 if the string is nul terminated. TYPE is
2323 the type of each character. OPTIONS holds the printing options;
2324 printing stops early if the number hits print_max; repeat counts
2325 are printed as appropriate. Print ellipses at the end if we had to
2326 stop before printing LENGTH characters, or if FORCE_ELLIPSES.
2327 QUOTE_CHAR is the character to print at each end of the string. If
2328 C_STYLE_TERMINATOR is true, and the last character is 0, then it is
2329 omitted. */
2331 void
2332 generic_printstr (struct ui_file *stream, struct type *type,
2333 const gdb_byte *string, unsigned int length,
2334 const char *encoding, int force_ellipses,
2335 int quote_char, int c_style_terminator,
2336 const struct value_print_options *options)
2338 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
2339 unsigned int i;
2340 int width = TYPE_LENGTH (type);
2341 struct obstack wchar_buf, output;
2342 struct cleanup *cleanup;
2343 struct wchar_iterator *iter;
2344 int finished = 0;
2345 struct converted_character *last;
2346 VEC (converted_character_d) *converted_chars;
2348 if (length == -1)
2350 unsigned long current_char = 1;
2352 for (i = 0; current_char; ++i)
2354 QUIT;
2355 current_char = extract_unsigned_integer (string + i * width,
2356 width, byte_order);
2358 length = i;
2361 /* If the string was not truncated due to `set print elements', and
2362 the last byte of it is a null, we don't print that, in
2363 traditional C style. */
2364 if (c_style_terminator
2365 && !force_ellipses
2366 && length > 0
2367 && (extract_unsigned_integer (string + (length - 1) * width,
2368 width, byte_order) == 0))
2369 length--;
2371 if (length == 0)
2373 fputs_filtered ("\"\"", stream);
2374 return;
2377 /* Arrange to iterate over the characters, in wchar_t form. */
2378 iter = make_wchar_iterator (string, length * width, encoding, width);
2379 cleanup = make_cleanup_wchar_iterator (iter);
2380 converted_chars = NULL;
2381 make_cleanup (VEC_cleanup (converted_character_d), &converted_chars);
2383 /* Convert characters until the string is over or the maximum
2384 number of printed characters has been reached. */
2385 i = 0;
2386 while (i < options->print_max)
2388 int r;
2390 QUIT;
2392 /* Grab the next character and repeat count. */
2393 r = count_next_character (iter, &converted_chars);
2395 /* If less than zero, the end of the input string was reached. */
2396 if (r < 0)
2397 break;
2399 /* Otherwise, add the count to the total print count and get
2400 the next character. */
2401 i += r;
2404 /* Get the last element and determine if the entire string was
2405 processed. */
2406 last = VEC_last (converted_character_d, converted_chars);
2407 finished = (last->result == wchar_iterate_eof);
2409 /* Ensure that CONVERTED_CHARS is terminated. */
2410 last->result = wchar_iterate_eof;
2412 /* WCHAR_BUF is the obstack we use to represent the string in
2413 wchar_t form. */
2414 obstack_init (&wchar_buf);
2415 make_cleanup_obstack_free (&wchar_buf);
2417 /* Print the output string to the obstack. */
2418 print_converted_chars_to_obstack (&wchar_buf, converted_chars, quote_char,
2419 width, byte_order, options);
2421 if (force_ellipses || !finished)
2422 obstack_grow_wstr (&wchar_buf, LCST ("..."));
2424 /* OUTPUT is where we collect `char's for printing. */
2425 obstack_init (&output);
2426 make_cleanup_obstack_free (&output);
2428 convert_between_encodings (INTERMEDIATE_ENCODING, host_charset (),
2429 (gdb_byte *) obstack_base (&wchar_buf),
2430 obstack_object_size (&wchar_buf),
2431 sizeof (gdb_wchar_t), &output, translit_char);
2432 obstack_1grow (&output, '\0');
2434 fputs_filtered (obstack_base (&output), stream);
2436 do_cleanups (cleanup);
2439 /* Print a string from the inferior, starting at ADDR and printing up to LEN
2440 characters, of WIDTH bytes a piece, to STREAM. If LEN is -1, printing
2441 stops at the first null byte, otherwise printing proceeds (including null
2442 bytes) until either print_max or LEN characters have been printed,
2443 whichever is smaller. ENCODING is the name of the string's
2444 encoding. It can be NULL, in which case the target encoding is
2445 assumed. */
2448 val_print_string (struct type *elttype, const char *encoding,
2449 CORE_ADDR addr, int len,
2450 struct ui_file *stream,
2451 const struct value_print_options *options)
2453 int force_ellipsis = 0; /* Force ellipsis to be printed if nonzero. */
2454 int errcode; /* Errno returned from bad reads. */
2455 int found_nul; /* Non-zero if we found the nul char. */
2456 unsigned int fetchlimit; /* Maximum number of chars to print. */
2457 int bytes_read;
2458 gdb_byte *buffer = NULL; /* Dynamically growable fetch buffer. */
2459 struct cleanup *old_chain = NULL; /* Top of the old cleanup chain. */
2460 struct gdbarch *gdbarch = get_type_arch (elttype);
2461 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2462 int width = TYPE_LENGTH (elttype);
2464 /* First we need to figure out the limit on the number of characters we are
2465 going to attempt to fetch and print. This is actually pretty simple. If
2466 LEN >= zero, then the limit is the minimum of LEN and print_max. If
2467 LEN is -1, then the limit is print_max. This is true regardless of
2468 whether print_max is zero, UINT_MAX (unlimited), or something in between,
2469 because finding the null byte (or available memory) is what actually
2470 limits the fetch. */
2472 fetchlimit = (len == -1 ? options->print_max : min (len,
2473 options->print_max));
2475 errcode = read_string (addr, len, width, fetchlimit, byte_order,
2476 &buffer, &bytes_read);
2477 old_chain = make_cleanup (xfree, buffer);
2479 addr += bytes_read;
2481 /* We now have either successfully filled the buffer to fetchlimit,
2482 or terminated early due to an error or finding a null char when
2483 LEN is -1. */
2485 /* Determine found_nul by looking at the last character read. */
2486 found_nul = extract_unsigned_integer (buffer + bytes_read - width, width,
2487 byte_order) == 0;
2488 if (len == -1 && !found_nul)
2490 gdb_byte *peekbuf;
2492 /* We didn't find a NUL terminator we were looking for. Attempt
2493 to peek at the next character. If not successful, or it is not
2494 a null byte, then force ellipsis to be printed. */
2496 peekbuf = (gdb_byte *) alloca (width);
2498 if (target_read_memory (addr, peekbuf, width) == 0
2499 && extract_unsigned_integer (peekbuf, width, byte_order) != 0)
2500 force_ellipsis = 1;
2502 else if ((len >= 0 && errcode != 0) || (len > bytes_read / width))
2504 /* Getting an error when we have a requested length, or fetching less
2505 than the number of characters actually requested, always make us
2506 print ellipsis. */
2507 force_ellipsis = 1;
2510 /* If we get an error before fetching anything, don't print a string.
2511 But if we fetch something and then get an error, print the string
2512 and then the error message. */
2513 if (errcode == 0 || bytes_read > 0)
2515 LA_PRINT_STRING (stream, elttype, buffer, bytes_read / width,
2516 encoding, force_ellipsis, options);
2519 if (errcode != 0)
2521 if (errcode == EIO)
2523 fprintf_filtered (stream, "<Address ");
2524 fputs_filtered (paddress (gdbarch, addr), stream);
2525 fprintf_filtered (stream, " out of bounds>");
2527 else
2529 fprintf_filtered (stream, "<Error reading address ");
2530 fputs_filtered (paddress (gdbarch, addr), stream);
2531 fprintf_filtered (stream, ": %s>", safe_strerror (errcode));
2535 gdb_flush (stream);
2536 do_cleanups (old_chain);
2538 return (bytes_read / width);
2542 /* The 'set input-radix' command writes to this auxiliary variable.
2543 If the requested radix is valid, INPUT_RADIX is updated; otherwise,
2544 it is left unchanged. */
2546 static unsigned input_radix_1 = 10;
2548 /* Validate an input or output radix setting, and make sure the user
2549 knows what they really did here. Radix setting is confusing, e.g.
2550 setting the input radix to "10" never changes it! */
2552 static void
2553 set_input_radix (char *args, int from_tty, struct cmd_list_element *c)
2555 set_input_radix_1 (from_tty, input_radix_1);
2558 static void
2559 set_input_radix_1 (int from_tty, unsigned radix)
2561 /* We don't currently disallow any input radix except 0 or 1, which don't
2562 make any mathematical sense. In theory, we can deal with any input
2563 radix greater than 1, even if we don't have unique digits for every
2564 value from 0 to radix-1, but in practice we lose on large radix values.
2565 We should either fix the lossage or restrict the radix range more.
2566 (FIXME). */
2568 if (radix < 2)
2570 input_radix_1 = input_radix;
2571 error (_("Nonsense input radix ``decimal %u''; input radix unchanged."),
2572 radix);
2574 input_radix_1 = input_radix = radix;
2575 if (from_tty)
2577 printf_filtered (_("Input radix now set to "
2578 "decimal %u, hex %x, octal %o.\n"),
2579 radix, radix, radix);
2583 /* The 'set output-radix' command writes to this auxiliary variable.
2584 If the requested radix is valid, OUTPUT_RADIX is updated,
2585 otherwise, it is left unchanged. */
2587 static unsigned output_radix_1 = 10;
2589 static void
2590 set_output_radix (char *args, int from_tty, struct cmd_list_element *c)
2592 set_output_radix_1 (from_tty, output_radix_1);
2595 static void
2596 set_output_radix_1 (int from_tty, unsigned radix)
2598 /* Validate the radix and disallow ones that we aren't prepared to
2599 handle correctly, leaving the radix unchanged. */
2600 switch (radix)
2602 case 16:
2603 user_print_options.output_format = 'x'; /* hex */
2604 break;
2605 case 10:
2606 user_print_options.output_format = 0; /* decimal */
2607 break;
2608 case 8:
2609 user_print_options.output_format = 'o'; /* octal */
2610 break;
2611 default:
2612 output_radix_1 = output_radix;
2613 error (_("Unsupported output radix ``decimal %u''; "
2614 "output radix unchanged."),
2615 radix);
2617 output_radix_1 = output_radix = radix;
2618 if (from_tty)
2620 printf_filtered (_("Output radix now set to "
2621 "decimal %u, hex %x, octal %o.\n"),
2622 radix, radix, radix);
2626 /* Set both the input and output radix at once. Try to set the output radix
2627 first, since it has the most restrictive range. An radix that is valid as
2628 an output radix is also valid as an input radix.
2630 It may be useful to have an unusual input radix. If the user wishes to
2631 set an input radix that is not valid as an output radix, he needs to use
2632 the 'set input-radix' command. */
2634 static void
2635 set_radix (char *arg, int from_tty)
2637 unsigned radix;
2639 radix = (arg == NULL) ? 10 : parse_and_eval_long (arg);
2640 set_output_radix_1 (0, radix);
2641 set_input_radix_1 (0, radix);
2642 if (from_tty)
2644 printf_filtered (_("Input and output radices now set to "
2645 "decimal %u, hex %x, octal %o.\n"),
2646 radix, radix, radix);
2650 /* Show both the input and output radices. */
2652 static void
2653 show_radix (char *arg, int from_tty)
2655 if (from_tty)
2657 if (input_radix == output_radix)
2659 printf_filtered (_("Input and output radices set to "
2660 "decimal %u, hex %x, octal %o.\n"),
2661 input_radix, input_radix, input_radix);
2663 else
2665 printf_filtered (_("Input radix set to decimal "
2666 "%u, hex %x, octal %o.\n"),
2667 input_radix, input_radix, input_radix);
2668 printf_filtered (_("Output radix set to decimal "
2669 "%u, hex %x, octal %o.\n"),
2670 output_radix, output_radix, output_radix);
2676 static void
2677 set_print (char *arg, int from_tty)
2679 printf_unfiltered (
2680 "\"set print\" must be followed by the name of a print subcommand.\n");
2681 help_list (setprintlist, "set print ", -1, gdb_stdout);
2684 static void
2685 show_print (char *args, int from_tty)
2687 cmd_show_list (showprintlist, from_tty, "");
2690 void
2691 _initialize_valprint (void)
2693 add_prefix_cmd ("print", no_class, set_print,
2694 _("Generic command for setting how things print."),
2695 &setprintlist, "set print ", 0, &setlist);
2696 add_alias_cmd ("p", "print", no_class, 1, &setlist);
2697 /* Prefer set print to set prompt. */
2698 add_alias_cmd ("pr", "print", no_class, 1, &setlist);
2700 add_prefix_cmd ("print", no_class, show_print,
2701 _("Generic command for showing print settings."),
2702 &showprintlist, "show print ", 0, &showlist);
2703 add_alias_cmd ("p", "print", no_class, 1, &showlist);
2704 add_alias_cmd ("pr", "print", no_class, 1, &showlist);
2706 add_setshow_uinteger_cmd ("elements", no_class,
2707 &user_print_options.print_max, _("\
2708 Set limit on string chars or array elements to print."), _("\
2709 Show limit on string chars or array elements to print."), _("\
2710 \"set print elements 0\" causes there to be no limit."),
2711 NULL,
2712 show_print_max,
2713 &setprintlist, &showprintlist);
2715 add_setshow_boolean_cmd ("null-stop", no_class,
2716 &user_print_options.stop_print_at_null, _("\
2717 Set printing of char arrays to stop at first null char."), _("\
2718 Show printing of char arrays to stop at first null char."), NULL,
2719 NULL,
2720 show_stop_print_at_null,
2721 &setprintlist, &showprintlist);
2723 add_setshow_uinteger_cmd ("repeats", no_class,
2724 &user_print_options.repeat_count_threshold, _("\
2725 Set threshold for repeated print elements."), _("\
2726 Show threshold for repeated print elements."), _("\
2727 \"set print repeats 0\" causes all elements to be individually printed."),
2728 NULL,
2729 show_repeat_count_threshold,
2730 &setprintlist, &showprintlist);
2732 add_setshow_boolean_cmd ("pretty", class_support,
2733 &user_print_options.prettyprint_structs, _("\
2734 Set prettyprinting of structures."), _("\
2735 Show prettyprinting of structures."), NULL,
2736 NULL,
2737 show_prettyprint_structs,
2738 &setprintlist, &showprintlist);
2740 add_setshow_boolean_cmd ("union", class_support,
2741 &user_print_options.unionprint, _("\
2742 Set printing of unions interior to structures."), _("\
2743 Show printing of unions interior to structures."), NULL,
2744 NULL,
2745 show_unionprint,
2746 &setprintlist, &showprintlist);
2748 add_setshow_boolean_cmd ("array", class_support,
2749 &user_print_options.prettyprint_arrays, _("\
2750 Set prettyprinting of arrays."), _("\
2751 Show prettyprinting of arrays."), NULL,
2752 NULL,
2753 show_prettyprint_arrays,
2754 &setprintlist, &showprintlist);
2756 add_setshow_boolean_cmd ("address", class_support,
2757 &user_print_options.addressprint, _("\
2758 Set printing of addresses."), _("\
2759 Show printing of addresses."), NULL,
2760 NULL,
2761 show_addressprint,
2762 &setprintlist, &showprintlist);
2764 add_setshow_boolean_cmd ("symbol", class_support,
2765 &user_print_options.symbol_print, _("\
2766 Set printing of symbol names when printing pointers."), _("\
2767 Show printing of symbol names when printing pointers."),
2768 NULL, NULL,
2769 show_symbol_print,
2770 &setprintlist, &showprintlist);
2772 add_setshow_zuinteger_cmd ("input-radix", class_support, &input_radix_1,
2773 _("\
2774 Set default input radix for entering numbers."), _("\
2775 Show default input radix for entering numbers."), NULL,
2776 set_input_radix,
2777 show_input_radix,
2778 &setlist, &showlist);
2780 add_setshow_zuinteger_cmd ("output-radix", class_support, &output_radix_1,
2781 _("\
2782 Set default output radix for printing of values."), _("\
2783 Show default output radix for printing of values."), NULL,
2784 set_output_radix,
2785 show_output_radix,
2786 &setlist, &showlist);
2788 /* The "set radix" and "show radix" commands are special in that
2789 they are like normal set and show commands but allow two normally
2790 independent variables to be either set or shown with a single
2791 command. So the usual deprecated_add_set_cmd() and [deleted]
2792 add_show_from_set() commands aren't really appropriate. */
2793 /* FIXME: i18n: With the new add_setshow_integer command, that is no
2794 longer true - show can display anything. */
2795 add_cmd ("radix", class_support, set_radix, _("\
2796 Set default input and output number radices.\n\
2797 Use 'set input-radix' or 'set output-radix' to independently set each.\n\
2798 Without an argument, sets both radices back to the default value of 10."),
2799 &setlist);
2800 add_cmd ("radix", class_support, show_radix, _("\
2801 Show the default input and output number radices.\n\
2802 Use 'show input-radix' or 'show output-radix' to independently show each."),
2803 &showlist);
2805 add_setshow_boolean_cmd ("array-indexes", class_support,
2806 &user_print_options.print_array_indexes, _("\
2807 Set printing of array indexes."), _("\
2808 Show printing of array indexes"), NULL, NULL, show_print_array_indexes,
2809 &setprintlist, &showprintlist);