merge from gcc
[deliverable/binutils-gdb.git] / gdb / valprint.c
CommitLineData
c906108c 1/* Print values for GDB, the GNU debugger.
5c1c87f0 2
8acc9f48 3 Copyright (C) 1986-2013 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
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
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
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.
c906108c 16
c5aa993b 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
19
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"
c906108c 28#include "language.h"
c906108c
SS
29#include "annotate.h"
30#include "valprint.h"
39424bef 31#include "floatformat.h"
d16aafd8 32#include "doublest.h"
19ca80ba 33#include "exceptions.h"
7678ef8f 34#include "dfp.h"
a6bac58e 35#include "python/python.h"
0c3acc09 36#include "ada-lang.h"
3b2b8fea
TT
37#include "gdb_obstack.h"
38#include "charset.h"
39#include <ctype.h>
c906108c
SS
40
41#include <errno.h>
42
0d63ecda
KS
43/* Maximum number of wchars returned from wchar_iterate. */
44#define MAX_WCHARS 4
45
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))
49
50/* Character buffer size saved while iterating over wchars. */
51#define WCHAR_BUFLEN_MAX WCHAR_BUFLEN (MAX_WCHARS)
52
53/* A structure to encapsulate state information from iterated
54 character conversions. */
55struct converted_character
56{
57 /* The number of characters converted. */
58 int num_chars;
59
60 /* The result of the conversion. See charset.h for more. */
61 enum wchar_iterate_result result;
62
63 /* The (saved) converted character(s). */
64 gdb_wchar_t chars[WCHAR_BUFLEN_MAX];
65
66 /* The first converted target byte. */
67 const gdb_byte *buf;
68
69 /* The number of bytes converted. */
70 size_t buflen;
71
72 /* How many times this character(s) is repeated. */
73 int repeat_count;
74};
75
76typedef struct converted_character converted_character_d;
77DEF_VEC_O (converted_character_d);
78
79
c906108c
SS
80/* Prototypes for local functions */
81
777ea8f1 82static int partial_memory_read (CORE_ADDR memaddr, gdb_byte *myaddr,
917317f4
JM
83 int len, int *errnoptr);
84
a14ed312 85static void show_print (char *, int);
c906108c 86
a14ed312 87static void set_print (char *, int);
c906108c 88
a14ed312 89static void set_radix (char *, int);
c906108c 90
a14ed312 91static void show_radix (char *, int);
c906108c 92
a14ed312 93static void set_input_radix (char *, int, struct cmd_list_element *);
c906108c 94
a14ed312 95static void set_input_radix_1 (int, unsigned);
c906108c 96
a14ed312 97static void set_output_radix (char *, int, struct cmd_list_element *);
c906108c 98
a14ed312 99static void set_output_radix_1 (int, unsigned);
c906108c 100
a14ed312 101void _initialize_valprint (void);
c906108c 102
581e13c1 103#define PRINT_MAX_DEFAULT 200 /* Start print_max off at this value. */
79a45b7d
TT
104
105struct value_print_options user_print_options =
106{
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 */
79a45b7d
TT
119 0, /* print_array_indexes */
120 0, /* deref_ref */
121 1, /* static_field_print */
a6bac58e
TT
122 1, /* pascal_static_field_print */
123 0, /* raw */
9cb709b6
TT
124 0, /* summary */
125 1 /* symbol_print */
79a45b7d
TT
126};
127
128/* Initialize *OPTS to be a copy of the user print options. */
129void
130get_user_print_options (struct value_print_options *opts)
131{
132 *opts = user_print_options;
133}
134
135/* Initialize *OPTS to be a copy of the user print options, but with
136 pretty-printing disabled. */
137void
138get_raw_print_options (struct value_print_options *opts)
139{
140 *opts = user_print_options;
141 opts->pretty = Val_no_prettyprint;
142}
143
144/* Initialize *OPTS to be a copy of the user print options, but using
145 FORMAT as the formatting option. */
146void
147get_formatted_print_options (struct value_print_options *opts,
148 char format)
149{
150 *opts = user_print_options;
151 opts->format = format;
152}
153
920d2a44
AC
154static void
155show_print_max (struct ui_file *file, int from_tty,
156 struct cmd_list_element *c, const char *value)
157{
3e43a32a
MS
158 fprintf_filtered (file,
159 _("Limit on string chars or array "
160 "elements to print is %s.\n"),
920d2a44
AC
161 value);
162}
163
c906108c
SS
164
165/* Default input and output radixes, and output format letter. */
166
167unsigned input_radix = 10;
920d2a44
AC
168static void
169show_input_radix (struct ui_file *file, int from_tty,
170 struct cmd_list_element *c, const char *value)
171{
3e43a32a
MS
172 fprintf_filtered (file,
173 _("Default input radix for entering numbers is %s.\n"),
920d2a44
AC
174 value);
175}
176
c906108c 177unsigned output_radix = 10;
920d2a44
AC
178static void
179show_output_radix (struct ui_file *file, int from_tty,
180 struct cmd_list_element *c, const char *value)
181{
3e43a32a
MS
182 fprintf_filtered (file,
183 _("Default output radix for printing of values is %s.\n"),
920d2a44
AC
184 value);
185}
c906108c 186
e79af960
JB
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. */
189
e79af960
JB
190static void
191show_print_array_indexes (struct ui_file *file, int from_tty,
192 struct cmd_list_element *c, const char *value)
193{
194 fprintf_filtered (file, _("Printing of array indexes is %s.\n"), value);
195}
196
c906108c
SS
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
581e13c1 199 print routines. */
c906108c 200
920d2a44
AC
201static void
202show_repeat_count_threshold (struct ui_file *file, int from_tty,
203 struct cmd_list_element *c, const char *value)
204{
205 fprintf_filtered (file, _("Threshold for repeated print elements is %s.\n"),
206 value);
207}
c906108c 208
581e13c1 209/* If nonzero, stops printing of char arrays at first null. */
c906108c 210
920d2a44
AC
211static void
212show_stop_print_at_null (struct ui_file *file, int from_tty,
213 struct cmd_list_element *c, const char *value)
214{
3e43a32a
MS
215 fprintf_filtered (file,
216 _("Printing of char arrays to stop "
217 "at first null char is %s.\n"),
920d2a44
AC
218 value);
219}
c906108c 220
581e13c1 221/* Controls pretty printing of structures. */
c906108c 222
920d2a44
AC
223static void
224show_prettyprint_structs (struct ui_file *file, int from_tty,
225 struct cmd_list_element *c, const char *value)
226{
227 fprintf_filtered (file, _("Prettyprinting of structures is %s.\n"), value);
228}
c906108c
SS
229
230/* Controls pretty printing of arrays. */
231
920d2a44
AC
232static void
233show_prettyprint_arrays (struct ui_file *file, int from_tty,
234 struct cmd_list_element *c, const char *value)
235{
236 fprintf_filtered (file, _("Prettyprinting of arrays is %s.\n"), value);
237}
c906108c
SS
238
239/* If nonzero, causes unions inside structures or other unions to be
581e13c1 240 printed. */
c906108c 241
920d2a44
AC
242static void
243show_unionprint (struct ui_file *file, int from_tty,
244 struct cmd_list_element *c, const char *value)
245{
3e43a32a
MS
246 fprintf_filtered (file,
247 _("Printing of unions interior to structures is %s.\n"),
920d2a44
AC
248 value);
249}
c906108c 250
581e13c1 251/* If nonzero, causes machine addresses to be printed in certain contexts. */
c906108c 252
920d2a44
AC
253static void
254show_addressprint (struct ui_file *file, int from_tty,
255 struct cmd_list_element *c, const char *value)
256{
257 fprintf_filtered (file, _("Printing of addresses is %s.\n"), value);
258}
9cb709b6
TT
259
260static void
261show_symbol_print (struct ui_file *file, int from_tty,
262 struct cmd_list_element *c, const char *value)
263{
264 fprintf_filtered (file,
265 _("Printing of symbols when printing pointers is %s.\n"),
266 value);
267}
268
c906108c 269\f
c5aa993b 270
a6bac58e
TT
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. */
274
275static int
276scalar_type_p (struct type *type)
277{
278 CHECK_TYPEDEF (type);
279 while (TYPE_CODE (type) == TYPE_CODE_REF)
280 {
281 type = TYPE_TARGET_TYPE (type);
282 CHECK_TYPEDEF (type);
283 }
284 switch (TYPE_CODE (type))
285 {
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:
a6bac58e
TT
291 return 0;
292 default:
293 return 1;
294 }
295}
296
a72c8f6a 297/* See its definition in value.h. */
0e03807e 298
a72c8f6a 299int
0e03807e
TT
300valprint_check_validity (struct ui_file *stream,
301 struct type *type,
4e07d55f 302 int embedded_offset,
0e03807e
TT
303 const struct value *val)
304{
305 CHECK_TYPEDEF (type);
306
307 if (TYPE_CODE (type) != TYPE_CODE_UNION
308 && TYPE_CODE (type) != TYPE_CODE_STRUCT
309 && TYPE_CODE (type) != TYPE_CODE_ARRAY)
310 {
4e07d55f
PA
311 if (!value_bits_valid (val, TARGET_CHAR_BIT * embedded_offset,
312 TARGET_CHAR_BIT * TYPE_LENGTH (type)))
0e03807e 313 {
585fdaa1 314 val_print_optimized_out (stream);
0e03807e
TT
315 return 0;
316 }
8cf6f0b1 317
4e07d55f 318 if (value_bits_synthetic_pointer (val, TARGET_CHAR_BIT * embedded_offset,
8cf6f0b1
TT
319 TARGET_CHAR_BIT * TYPE_LENGTH (type)))
320 {
321 fputs_filtered (_("<synthetic pointer>"), stream);
322 return 0;
323 }
4e07d55f
PA
324
325 if (!value_bytes_available (val, embedded_offset, TYPE_LENGTH (type)))
326 {
327 val_print_unavailable (stream);
328 return 0;
329 }
0e03807e
TT
330 }
331
332 return 1;
333}
334
585fdaa1
PA
335void
336val_print_optimized_out (struct ui_file *stream)
337{
338 fprintf_filtered (stream, _("<optimized out>"));
339}
340
4e07d55f
PA
341void
342val_print_unavailable (struct ui_file *stream)
343{
344 fprintf_filtered (stream, _("<unavailable>"));
345}
346
8af8e3bc
PA
347void
348val_print_invalid_address (struct ui_file *stream)
349{
350 fprintf_filtered (stream, _("<invalid address>"));
351}
352
e88acd96
TT
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.
358
359 Most arguments are as to val_print.
360
361 The additional DECORATIONS argument can be used to customize the
362 output in some small, language-specific ways. */
363
364void
365generic_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)
371{
372 struct gdbarch *gdbarch = get_type_arch (type);
e88acd96
TT
373 unsigned int i = 0; /* Number of characters printed. */
374 unsigned len;
375 struct type *elttype, *unresolved_elttype;
376 struct type *unresolved_type = type;
e88acd96
TT
377 LONGEST val;
378 CORE_ADDR addr;
379
380 CHECK_TYPEDEF (type);
381 switch (TYPE_CODE (type))
382 {
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)
387 {
388 LONGEST low_bound, high_bound;
389
390 if (!get_array_bounds (type, &low_bound, &high_bound))
391 error (_("Could not determine the array high bound"));
392
393 if (options->prettyprint_arrays)
394 {
395 print_spaces_filtered (2 + 2 * recurse, stream);
396 }
397
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;
404 }
405 /* Array of unspecified length: treat like pointer to first
406 elt. */
407 addr = address + embedded_offset;
408 goto print_unpacked_pointer;
409
410 case TYPE_CODE_MEMBERPTR:
411 val_print_scalar_formatted (type, valaddr, embedded_offset,
412 original_value, options, 0, stream);
413 break;
414
415 case TYPE_CODE_PTR:
416 if (options->format && options->format != 's')
417 {
418 val_print_scalar_formatted (type, valaddr, embedded_offset,
419 original_value, options, 0, stream);
420 break;
421 }
422 unresolved_elttype = TYPE_TARGET_TYPE (type);
423 elttype = check_typedef (unresolved_elttype);
424 {
425 addr = unpack_pointer (type, valaddr + embedded_offset);
426 print_unpacked_pointer:
427
428 if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
429 {
430 /* Try to print what function it points to. */
edf0c1b7 431 print_function_pointer_address (options, gdbarch, addr, stream);
e88acd96
TT
432 return;
433 }
434
9cb709b6
TT
435 if (options->symbol_print)
436 print_address_demangle (options, gdbarch, addr, stream, demangle);
437 else if (options->addressprint)
e88acd96
TT
438 fputs_filtered (paddress (gdbarch, addr), stream);
439 }
440 break;
441
442 case TYPE_CODE_REF:
443 elttype = check_typedef (TYPE_TARGET_TYPE (type));
444 if (options->addressprint)
445 {
446 CORE_ADDR addr
447 = extract_typed_address (valaddr + embedded_offset, type);
448
449 fprintf_filtered (stream, "@");
450 fputs_filtered (paddress (gdbarch, addr), stream);
451 if (options->deref_ref)
452 fputs_filtered (": ", stream);
453 }
454 /* De-reference the reference. */
455 if (options->deref_ref)
456 {
457 if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
458 {
459 struct value *deref_val;
460
461 deref_val = coerce_ref_if_computed (original_value);
462 if (deref_val != NULL)
463 {
464 /* More complicated computed references are not supported. */
465 gdb_assert (embedded_offset == 0);
466 }
467 else
468 deref_val = value_at (TYPE_TARGET_TYPE (type),
469 unpack_pointer (type,
470 (valaddr
471 + embedded_offset)));
472
473 common_val_print (deref_val, stream, recurse, options,
474 current_language);
475 }
476 else
477 fputs_filtered ("???", stream);
478 }
479 break;
480
481 case TYPE_CODE_ENUM:
482 if (options->format)
483 {
484 val_print_scalar_formatted (type, valaddr, embedded_offset,
485 original_value, options, 0, stream);
486 break;
487 }
488 len = TYPE_NFIELDS (type);
489 val = unpack_long (type, valaddr + embedded_offset);
490 for (i = 0; i < len; i++)
491 {
492 QUIT;
14e75d8e 493 if (val == TYPE_FIELD_ENUMVAL (type, i))
e88acd96
TT
494 {
495 break;
496 }
497 }
498 if (i < len)
499 {
500 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
501 }
502 else if (TYPE_FLAG_ENUM (type))
503 {
504 int first = 1;
505
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)
511 {
512 QUIT;
513
14e75d8e 514 if ((val & TYPE_FIELD_ENUMVAL (type, i)) != 0)
e88acd96
TT
515 {
516 if (!first)
517 fputs_filtered (" | ", stream);
518 first = 0;
519
14e75d8e 520 val &= ~TYPE_FIELD_ENUMVAL (type, i);
e88acd96
TT
521 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
522 }
523 }
524
525 if (first || val != 0)
526 {
527 if (!first)
528 fputs_filtered (" | ", stream);
529 fputs_filtered ("unknown: ", stream);
530 print_longest (stream, 'd', 0, val);
531 }
532
533 fputs_filtered (")", stream);
534 }
535 else
536 print_longest (stream, 'd', 0, val);
537 break;
538
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;
547
548 case TYPE_CODE_FUNC:
549 case TYPE_CODE_METHOD:
550 if (options->format)
551 {
552 val_print_scalar_formatted (type, valaddr, embedded_offset,
553 original_value, options, 0, stream);
554 break;
555 }
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. */
edf0c1b7 563 print_address_demangle (options, gdbarch, address, stream, demangle);
e88acd96
TT
564 break;
565
566 case TYPE_CODE_BOOL:
567 if (options->format || options->output_format)
568 {
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);
574 }
575 else
576 {
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);
584 }
585 break;
586
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). */
595
596 /* FALLTHROUGH */
597
598 case TYPE_CODE_INT:
599 if (options->format || options->output_format)
600 {
601 struct value_print_options opts = *options;
602
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);
607 }
608 else
609 val_print_type_code_int (type, valaddr + embedded_offset, stream);
610 break;
611
612 case TYPE_CODE_CHAR:
613 if (options->format || options->output_format)
614 {
615 struct value_print_options opts = *options;
616
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);
621 }
622 else
623 {
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);
631 }
632 break;
633
634 case TYPE_CODE_FLT:
635 if (options->format)
636 {
637 val_print_scalar_formatted (type, valaddr, embedded_offset,
638 original_value, options, 0, stream);
639 }
640 else
641 {
642 print_floating (valaddr + embedded_offset, type, stream);
643 }
644 break;
645
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;
654
655 case TYPE_CODE_VOID:
656 fputs_filtered (decorations->void_name, stream);
657 break;
658
659 case TYPE_CODE_ERROR:
660 fprintf_filtered (stream, "%s", TYPE_ERROR_NAME (type));
661 break;
662
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;
670
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;
696
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));
703 }
704 gdb_flush (stream);
705}
706
32b72a42
PA
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.
713
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?".
722
723 RECURSE indicates the amount of indentation to supply before
724 continuation lines; this amount is roughly twice the value of
35c0084b 725 RECURSE. */
32b72a42 726
35c0084b 727void
fc1a4b47 728val_print (struct type *type, const gdb_byte *valaddr, int embedded_offset,
79a45b7d 729 CORE_ADDR address, struct ui_file *stream, int recurse,
0e03807e 730 const struct value *val,
79a45b7d 731 const struct value_print_options *options,
d8ca156b 732 const struct language_defn *language)
c906108c 733{
19ca80ba
DJ
734 volatile struct gdb_exception except;
735 int ret = 0;
79a45b7d 736 struct value_print_options local_opts = *options;
c906108c 737 struct type *real_type = check_typedef (type);
79a45b7d
TT
738
739 if (local_opts.pretty == Val_pretty_default)
740 local_opts.pretty = (local_opts.prettyprint_structs
741 ? Val_prettyprint : Val_no_prettyprint);
c5aa993b 742
c906108c
SS
743 QUIT;
744
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. */
748
74a9bb82 749 if (TYPE_STUB (real_type))
c906108c 750 {
0e03807e 751 fprintf_filtered (stream, _("<incomplete type>"));
c906108c 752 gdb_flush (stream);
35c0084b 753 return;
c906108c 754 }
c5aa993b 755
0e03807e 756 if (!valprint_check_validity (stream, real_type, embedded_offset, val))
35c0084b 757 return;
0e03807e 758
a6bac58e
TT
759 if (!options->raw)
760 {
761 ret = apply_val_pretty_printer (type, valaddr, embedded_offset,
0e03807e
TT
762 address, stream, recurse,
763 val, options, language);
a6bac58e 764 if (ret)
35c0084b 765 return;
a6bac58e
TT
766 }
767
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))
771 {
772 fprintf_filtered (stream, "...");
35c0084b 773 return;
a6bac58e
TT
774 }
775
19ca80ba
DJ
776 TRY_CATCH (except, RETURN_MASK_ERROR)
777 {
d3eab38a
TT
778 language->la_val_print (type, valaddr, embedded_offset, address,
779 stream, recurse, val,
780 &local_opts);
19ca80ba
DJ
781 }
782 if (except.reason < 0)
783 fprintf_filtered (stream, _("<error reading variable>"));
c906108c
SS
784}
785
806048c6 786/* Check whether the value VAL is printable. Return 1 if it is;
6501578c
YQ
787 return 0 and print an appropriate error message to STREAM according to
788 OPTIONS if it is not. */
c906108c 789
806048c6 790static int
6501578c
YQ
791value_check_printable (struct value *val, struct ui_file *stream,
792 const struct value_print_options *options)
c906108c
SS
793{
794 if (val == 0)
795 {
806048c6 796 fprintf_filtered (stream, _("<address of value unknown>"));
c906108c
SS
797 return 0;
798 }
806048c6 799
0e03807e 800 if (value_entirely_optimized_out (val))
c906108c 801 {
6501578c
YQ
802 if (options->summary && !scalar_type_p (value_type (val)))
803 fprintf_filtered (stream, "...");
804 else
805 val_print_optimized_out (stream);
c906108c
SS
806 return 0;
807 }
806048c6 808
bc3b79fd
TJB
809 if (TYPE_CODE (value_type (val)) == TYPE_CODE_INTERNAL_FUNCTION)
810 {
811 fprintf_filtered (stream, _("<internal function %s>"),
812 value_internal_function_name (val));
813 return 0;
814 }
815
806048c6
DJ
816 return 1;
817}
818
d8ca156b 819/* Print using the given LANGUAGE the value VAL onto stream STREAM according
79a45b7d 820 to OPTIONS.
806048c6 821
806048c6
DJ
822 This is a preferable interface to val_print, above, because it uses
823 GDB's value mechanism. */
824
a1f5dd1b 825void
79a45b7d
TT
826common_val_print (struct value *val, struct ui_file *stream, int recurse,
827 const struct value_print_options *options,
d8ca156b 828 const struct language_defn *language)
806048c6 829{
6501578c 830 if (!value_check_printable (val, stream, options))
a1f5dd1b 831 return;
806048c6 832
0c3acc09
JB
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);
839
a1f5dd1b
TT
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);
806048c6
DJ
844}
845
7348c5e1 846/* Print on stream STREAM the value VAL according to OPTIONS. The value
8e069a98 847 is printed using the current_language syntax. */
7348c5e1 848
8e069a98 849void
79a45b7d
TT
850value_print (struct value *val, struct ui_file *stream,
851 const struct value_print_options *options)
806048c6 852{
6501578c 853 if (!value_check_printable (val, stream, options))
8e069a98 854 return;
806048c6 855
a6bac58e
TT
856 if (!options->raw)
857 {
858 int r = apply_val_pretty_printer (value_type (val),
0e03807e 859 value_contents_for_printing (val),
a6bac58e
TT
860 value_embedded_offset (val),
861 value_address (val),
0e03807e
TT
862 stream, 0,
863 val, options, current_language);
a109c7c1 864
a6bac58e 865 if (r)
8e069a98 866 return;
a6bac58e
TT
867 }
868
8e069a98 869 LA_VALUE_PRINT (val, stream, options);
c906108c
SS
870}
871
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. */
875
876void
fc1a4b47 877val_print_type_code_int (struct type *type, const gdb_byte *valaddr,
fba45db2 878 struct ui_file *stream)
c906108c 879{
50810684 880 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
d44e8473 881
c906108c
SS
882 if (TYPE_LENGTH (type) > sizeof (LONGEST))
883 {
884 LONGEST val;
885
886 if (TYPE_UNSIGNED (type)
887 && extract_long_unsigned_integer (valaddr, TYPE_LENGTH (type),
e17a4113 888 byte_order, &val))
c906108c
SS
889 {
890 print_longest (stream, 'u', 0, val);
891 }
892 else
893 {
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,
d44e8473 899 TYPE_LENGTH (type), byte_order);
c906108c
SS
900 }
901 }
902 else
903 {
c906108c
SS
904 print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0,
905 unpack_long (type, valaddr));
c906108c
SS
906 }
907}
908
4f2aea11
MK
909void
910val_print_type_code_flags (struct type *type, const gdb_byte *valaddr,
911 struct ui_file *stream)
912{
befae759 913 ULONGEST val = unpack_long (type, valaddr);
4f2aea11
MK
914 int bitpos, nfields = TYPE_NFIELDS (type);
915
916 fputs_filtered ("[ ", stream);
917 for (bitpos = 0; bitpos < nfields; bitpos++)
918 {
316703b9
MK
919 if (TYPE_FIELD_BITPOS (type, bitpos) != -1
920 && (val & ((ULONGEST)1 << bitpos)))
4f2aea11
MK
921 {
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);
926 }
927 }
928 fputs_filtered ("]", stream);
19c37f24 929}
ab2188aa
PA
930
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.
934
935 This is how the elements of an array or structure are printed
936 with a format. */
ab2188aa
PA
937
938void
939val_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)
945{
946 gdb_assert (val != NULL);
947 gdb_assert (valaddr == value_contents_for_printing_const (val));
948
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')
953 {
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;
960 }
961
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);
4e07d55f
PA
967 else if (!value_bytes_available (val, embedded_offset, TYPE_LENGTH (type)))
968 val_print_unavailable (stream);
ab2188aa
PA
969 else
970 print_scalar_formatted (valaddr + embedded_offset, type,
971 options, size, stream);
4f2aea11
MK
972}
973
c906108c
SS
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
581e13c1 976 LONG_LONG's into this one function. The format chars b,h,w,g are
bb599908 977 from print_scalar_formatted(). Numbers are printed using C
581e13c1 978 format.
bb599908
PH
979
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).
983
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
581e13c1 991 that capability. */
c906108c
SS
992
993void
bb599908 994print_longest (struct ui_file *stream, int format, int use_c_format,
fba45db2 995 LONGEST val_long)
c906108c 996{
2bfb72ee
AC
997 const char *val;
998
c906108c
SS
999 switch (format)
1000 {
1001 case 'd':
bb599908 1002 val = int_string (val_long, 10, 1, 0, 1); break;
c906108c 1003 case 'u':
bb599908 1004 val = int_string (val_long, 10, 0, 0, 1); break;
c906108c 1005 case 'x':
bb599908 1006 val = int_string (val_long, 16, 0, 0, use_c_format); break;
c906108c 1007 case 'b':
bb599908 1008 val = int_string (val_long, 16, 0, 2, 1); break;
c906108c 1009 case 'h':
bb599908 1010 val = int_string (val_long, 16, 0, 4, 1); break;
c906108c 1011 case 'w':
bb599908 1012 val = int_string (val_long, 16, 0, 8, 1); break;
c906108c 1013 case 'g':
bb599908 1014 val = int_string (val_long, 16, 0, 16, 1); break;
c906108c
SS
1015 break;
1016 case 'o':
bb599908 1017 val = int_string (val_long, 8, 0, 0, use_c_format); break;
c906108c 1018 default:
3e43a32a
MS
1019 internal_error (__FILE__, __LINE__,
1020 _("failed internal consistency check"));
bb599908 1021 }
2bfb72ee 1022 fputs_filtered (val, stream);
c906108c
SS
1023}
1024
c906108c
SS
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. */
1030
1031int
fba45db2 1032longest_to_int (LONGEST arg)
c906108c 1033{
581e13c1 1034 /* Let the compiler do the work. */
c906108c
SS
1035 int rtnval = (int) arg;
1036
581e13c1 1037 /* Check for overflows or underflows. */
c906108c
SS
1038 if (sizeof (LONGEST) > sizeof (int))
1039 {
1040 if (rtnval != arg)
1041 {
8a3fe4f8 1042 error (_("Value out of range."));
c906108c
SS
1043 }
1044 }
1045 return (rtnval);
1046}
1047
a73c86fb
AC
1048/* Print a floating point value of type TYPE (not always a
1049 TYPE_CODE_FLT), pointed to in GDB by VALADDR, on STREAM. */
c906108c
SS
1050
1051void
fc1a4b47 1052print_floating (const gdb_byte *valaddr, struct type *type,
c84141d6 1053 struct ui_file *stream)
c906108c
SS
1054{
1055 DOUBLEST doub;
1056 int inv;
a73c86fb 1057 const struct floatformat *fmt = NULL;
c906108c 1058 unsigned len = TYPE_LENGTH (type);
20389057 1059 enum float_kind kind;
c5aa993b 1060
a73c86fb
AC
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);
20389057 1064 if (fmt != NULL)
39424bef 1065 {
20389057
DJ
1066 kind = floatformat_classify (fmt, valaddr);
1067 if (kind == float_nan)
1068 {
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;
1076 }
1077 else if (kind == float_infinite)
1078 {
1079 if (floatformat_is_negative (fmt, valaddr))
1080 fputs_filtered ("-", stream);
1081 fputs_filtered ("inf", stream);
1082 return;
1083 }
7355ddba 1084 }
c906108c 1085
a73c86fb
AC
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. */
c2f05ac9 1094
c906108c
SS
1095 doub = unpack_double (type, valaddr, &inv);
1096 if (inv)
1097 {
1098 fprintf_filtered (stream, "<invalid float value>");
1099 return;
1100 }
1101
39424bef
MK
1102 /* FIXME: kettenis/2001-01-20: The following code makes too much
1103 assumptions about the host and target floating point format. */
1104
a73c86fb 1105 /* NOTE: cagney/2002-02-03: Since the TYPE of what was passed in may
c41b8590 1106 not necessarily be a TYPE_CODE_FLT, the below ignores that and
a73c86fb
AC
1107 instead uses the type's length to determine the precision of the
1108 floating-point value being printed. */
c2f05ac9 1109
c906108c 1110 if (len < sizeof (double))
c5aa993b 1111 fprintf_filtered (stream, "%.9g", (double) doub);
c906108c 1112 else if (len == sizeof (double))
c5aa993b 1113 fprintf_filtered (stream, "%.17g", (double) doub);
c906108c
SS
1114 else
1115#ifdef PRINTF_HAS_LONG_DOUBLE
1116 fprintf_filtered (stream, "%.35Lg", doub);
1117#else
39424bef
MK
1118 /* This at least wins with values that are representable as
1119 doubles. */
c906108c
SS
1120 fprintf_filtered (stream, "%.17g", (double) doub);
1121#endif
1122}
1123
7678ef8f
TJB
1124void
1125print_decimal_floating (const gdb_byte *valaddr, struct type *type,
1126 struct ui_file *stream)
1127{
e17a4113 1128 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
7678ef8f
TJB
1129 char decstr[MAX_DECIMAL_STRING];
1130 unsigned len = TYPE_LENGTH (type);
1131
e17a4113 1132 decimal_to_string (valaddr, len, byte_order, decstr);
7678ef8f
TJB
1133 fputs_filtered (decstr, stream);
1134 return;
1135}
1136
c5aa993b 1137void
fc1a4b47 1138print_binary_chars (struct ui_file *stream, const gdb_byte *valaddr,
d44e8473 1139 unsigned len, enum bfd_endian byte_order)
c906108c
SS
1140{
1141
1142#define BITS_IN_BYTES 8
1143
fc1a4b47 1144 const gdb_byte *p;
745b8ca0 1145 unsigned int i;
c5aa993b 1146 int b;
c906108c
SS
1147
1148 /* Declared "int" so it will be signed.
581e13c1
MS
1149 This ensures that right shift will shift in zeros. */
1150
c5aa993b 1151 const int mask = 0x080;
c906108c
SS
1152
1153 /* FIXME: We should be not printing leading zeroes in most cases. */
1154
d44e8473 1155 if (byte_order == BFD_ENDIAN_BIG)
c906108c
SS
1156 {
1157 for (p = valaddr;
1158 p < valaddr + len;
1159 p++)
1160 {
c5aa993b 1161 /* Every byte has 8 binary characters; peel off
581e13c1
MS
1162 and print from the MSB end. */
1163
c5aa993b
JM
1164 for (i = 0; i < (BITS_IN_BYTES * sizeof (*p)); i++)
1165 {
1166 if (*p & (mask >> i))
1167 b = 1;
1168 else
1169 b = 0;
1170
1171 fprintf_filtered (stream, "%1d", b);
1172 }
c906108c
SS
1173 }
1174 }
1175 else
1176 {
1177 for (p = valaddr + len - 1;
1178 p >= valaddr;
1179 p--)
1180 {
c5aa993b
JM
1181 for (i = 0; i < (BITS_IN_BYTES * sizeof (*p)); i++)
1182 {
1183 if (*p & (mask >> i))
1184 b = 1;
1185 else
1186 b = 0;
1187
1188 fprintf_filtered (stream, "%1d", b);
1189 }
c906108c
SS
1190 }
1191 }
c906108c
SS
1192}
1193
1194/* VALADDR points to an integer of LEN bytes.
581e13c1
MS
1195 Print it in octal on stream or format it in buf. */
1196
c906108c 1197void
fc1a4b47 1198print_octal_chars (struct ui_file *stream, const gdb_byte *valaddr,
d44e8473 1199 unsigned len, enum bfd_endian byte_order)
c906108c 1200{
fc1a4b47 1201 const gdb_byte *p;
c906108c 1202 unsigned char octa1, octa2, octa3, carry;
c5aa993b
JM
1203 int cycle;
1204
c906108c
SS
1205 /* FIXME: We should be not printing leading zeroes in most cases. */
1206
1207
1208 /* Octal is 3 bits, which doesn't fit. Yuk. So we have to track
1209 * the extra bits, which cycle every three bytes:
1210 *
1211 * Byte side: 0 1 2 3
1212 * | | | |
1213 * bit number 123 456 78 | 9 012 345 6 | 78 901 234 | 567 890 12 |
1214 *
1215 * Octal side: 0 1 carry 3 4 carry ...
1216 *
1217 * Cycle number: 0 1 2
1218 *
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.
1222 */
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
1234
1235 /* For 32 we start in cycle 2, with two bits and one bit carry;
581e13c1
MS
1236 for 64 in cycle in cycle 1, with one bit and a two bit carry. */
1237
c906108c
SS
1238 cycle = (len * BITS_IN_BYTES) % BITS_IN_OCTAL;
1239 carry = 0;
c5aa993b 1240
bb599908 1241 fputs_filtered ("0", stream);
d44e8473 1242 if (byte_order == BFD_ENDIAN_BIG)
c906108c
SS
1243 {
1244 for (p = valaddr;
1245 p < valaddr + len;
1246 p++)
1247 {
c5aa993b
JM
1248 switch (cycle)
1249 {
1250 case 0:
581e13c1
MS
1251 /* No carry in, carry out two bits. */
1252
c5aa993b
JM
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;
1259
1260 case 1:
581e13c1
MS
1261 /* Carry in two bits, carry out one bit. */
1262
c5aa993b
JM
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;
1271
1272 case 2:
581e13c1
MS
1273 /* Carry in one bit, no carry out. */
1274
c5aa993b
JM
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;
1283
1284 default:
8a3fe4f8 1285 error (_("Internal error in octal conversion;"));
c5aa993b
JM
1286 }
1287
1288 cycle++;
1289 cycle = cycle % BITS_IN_OCTAL;
c906108c
SS
1290 }
1291 }
1292 else
1293 {
1294 for (p = valaddr + len - 1;
1295 p >= valaddr;
1296 p--)
1297 {
c5aa993b
JM
1298 switch (cycle)
1299 {
1300 case 0:
1301 /* Carry out, no carry in */
581e13c1 1302
c5aa993b
JM
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;
1309
1310 case 1:
1311 /* Carry in, carry out */
581e13c1 1312
c5aa993b
JM
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;
1321
1322 case 2:
1323 /* Carry in, no carry out */
581e13c1 1324
c5aa993b
JM
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;
1333
1334 default:
8a3fe4f8 1335 error (_("Internal error in octal conversion;"));
c5aa993b
JM
1336 }
1337
1338 cycle++;
1339 cycle = cycle % BITS_IN_OCTAL;
c906108c
SS
1340 }
1341 }
1342
c906108c
SS
1343}
1344
1345/* VALADDR points to an integer of LEN bytes.
581e13c1
MS
1346 Print it in decimal on stream or format it in buf. */
1347
c906108c 1348void
fc1a4b47 1349print_decimal_chars (struct ui_file *stream, const gdb_byte *valaddr,
d44e8473 1350 unsigned len, enum bfd_endian byte_order)
c906108c
SS
1351{
1352#define TEN 10
c5aa993b 1353#define CARRY_OUT( x ) ((x) / TEN) /* extend char to int */
c906108c
SS
1354#define CARRY_LEFT( x ) ((x) % TEN)
1355#define SHIFT( x ) ((x) << 4)
c906108c
SS
1356#define LOW_NIBBLE( x ) ( (x) & 0x00F)
1357#define HIGH_NIBBLE( x ) (((x) & 0x0F0) >> 4)
1358
fc1a4b47 1359 const gdb_byte *p;
c906108c 1360 unsigned char *digits;
c5aa993b
JM
1361 int carry;
1362 int decimal_len;
1363 int i, j, decimal_digits;
1364 int dummy;
1365 int flip;
1366
c906108c 1367 /* Base-ten number is less than twice as many digits
581e13c1
MS
1368 as the base 16 number, which is 2 digits per byte. */
1369
c906108c 1370 decimal_len = len * 2 * 2;
3c37485b 1371 digits = xmalloc (decimal_len);
c906108c 1372
c5aa993b
JM
1373 for (i = 0; i < decimal_len; i++)
1374 {
c906108c 1375 digits[i] = 0;
c5aa993b 1376 }
c906108c 1377
c906108c
SS
1378 /* Ok, we have an unknown number of bytes of data to be printed in
1379 * decimal.
1380 *
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.
1384 *
1385 * The trick is that "digits" holds a base-10 number, but sometimes
581e13c1 1386 * the individual digits are > 10.
c906108c
SS
1387 *
1388 * Outer loop is per nibble (hex digit) of input, from MSD end to
1389 * LSD end.
1390 */
c5aa993b 1391 decimal_digits = 0; /* Number of decimal digits so far */
d44e8473 1392 p = (byte_order == BFD_ENDIAN_BIG) ? valaddr : valaddr + len - 1;
c906108c 1393 flip = 0;
d44e8473 1394 while ((byte_order == BFD_ENDIAN_BIG) ? (p < valaddr + len) : (p >= valaddr))
c5aa993b 1395 {
c906108c
SS
1396 /*
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.
1400 */
c5aa993b
JM
1401 for (j = 0; j < decimal_digits; j++)
1402 {
1403 digits[j] = SHIFT (digits[j]);
1404 }
1405
c906108c
SS
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.
1409 *
1410 * "flip" is used to run this loop twice for each byte.
1411 */
c5aa993b
JM
1412 if (flip == 0)
1413 {
581e13c1
MS
1414 /* Take top nibble. */
1415
c5aa993b
JM
1416 digits[0] += HIGH_NIBBLE (*p);
1417 flip = 1;
1418 }
1419 else
1420 {
581e13c1
MS
1421 /* Take low nibble and bump our pointer "p". */
1422
c5aa993b 1423 digits[0] += LOW_NIBBLE (*p);
d44e8473
MD
1424 if (byte_order == BFD_ENDIAN_BIG)
1425 p++;
1426 else
1427 p--;
c5aa993b
JM
1428 flip = 0;
1429 }
c906108c
SS
1430
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.
1438 */
1439 carry = 0;
c5aa993b
JM
1440 for (j = 0; j < decimal_len - 1; j++)
1441 {
1442 digits[j] += carry;
1443
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".
1447 */
1448 dummy = digits[j];
1449 carry = CARRY_OUT (dummy);
1450 digits[j] = CARRY_LEFT (dummy);
1451
1452 if (j >= decimal_digits && carry == 0)
1453 {
1454 /*
1455 * All higher digits are 0 and we
1456 * no longer have a carry.
1457 *
1458 * Note: "j" is 0-based, "decimal_digits" is
1459 * 1-based.
1460 */
1461 decimal_digits = j + 1;
1462 break;
1463 }
1464 }
1465 }
c906108c
SS
1466
1467 /* Ok, now "digits" is the decimal representation, with
581e13c1
MS
1468 the "decimal_digits" actual digits. Print! */
1469
c5aa993b
JM
1470 for (i = decimal_digits - 1; i >= 0; i--)
1471 {
1472 fprintf_filtered (stream, "%1d", digits[i]);
1473 }
b8c9b27d 1474 xfree (digits);
c906108c
SS
1475}
1476
1477/* VALADDR points to an integer of LEN bytes. Print it in hex on stream. */
1478
6b9acc27 1479void
fc1a4b47 1480print_hex_chars (struct ui_file *stream, const gdb_byte *valaddr,
d44e8473 1481 unsigned len, enum bfd_endian byte_order)
c906108c 1482{
fc1a4b47 1483 const gdb_byte *p;
c906108c
SS
1484
1485 /* FIXME: We should be not printing leading zeroes in most cases. */
1486
bb599908 1487 fputs_filtered ("0x", stream);
d44e8473 1488 if (byte_order == BFD_ENDIAN_BIG)
c906108c
SS
1489 {
1490 for (p = valaddr;
1491 p < valaddr + len;
1492 p++)
1493 {
1494 fprintf_filtered (stream, "%02x", *p);
1495 }
1496 }
1497 else
1498 {
1499 for (p = valaddr + len - 1;
1500 p >= valaddr;
1501 p--)
1502 {
1503 fprintf_filtered (stream, "%02x", *p);
1504 }
1505 }
c906108c
SS
1506}
1507
3e43a32a 1508/* VALADDR points to a char integer of LEN bytes.
581e13c1 1509 Print it out in appropriate language form on stream.
6b9acc27
JJ
1510 Omit any leading zero chars. */
1511
1512void
6c7a06a3
TT
1513print_char_chars (struct ui_file *stream, struct type *type,
1514 const gdb_byte *valaddr,
d44e8473 1515 unsigned len, enum bfd_endian byte_order)
6b9acc27 1516{
fc1a4b47 1517 const gdb_byte *p;
6b9acc27 1518
d44e8473 1519 if (byte_order == BFD_ENDIAN_BIG)
6b9acc27
JJ
1520 {
1521 p = valaddr;
1522 while (p < valaddr + len - 1 && *p == 0)
1523 ++p;
1524
1525 while (p < valaddr + len)
1526 {
6c7a06a3 1527 LA_EMIT_CHAR (*p, type, stream, '\'');
6b9acc27
JJ
1528 ++p;
1529 }
1530 }
1531 else
1532 {
1533 p = valaddr + len - 1;
1534 while (p > valaddr && *p == 0)
1535 --p;
1536
1537 while (p >= valaddr)
1538 {
6c7a06a3 1539 LA_EMIT_CHAR (*p, type, stream, '\'');
6b9acc27
JJ
1540 --p;
1541 }
1542 }
1543}
1544
132c57b4
TT
1545/* Print function pointer with inferior address ADDRESS onto stdio
1546 stream STREAM. */
1547
1548void
edf0c1b7
TT
1549print_function_pointer_address (const struct value_print_options *options,
1550 struct gdbarch *gdbarch,
132c57b4 1551 CORE_ADDR address,
edf0c1b7 1552 struct ui_file *stream)
132c57b4
TT
1553{
1554 CORE_ADDR func_addr
1555 = gdbarch_convert_from_func_ptr_addr (gdbarch, address,
1556 &current_target);
1557
1558 /* If the function pointer is represented by a description, print
1559 the address of the description. */
edf0c1b7 1560 if (options->addressprint && func_addr != address)
132c57b4
TT
1561 {
1562 fputs_filtered ("@", stream);
1563 fputs_filtered (paddress (gdbarch, address), stream);
1564 fputs_filtered (": ", stream);
1565 }
edf0c1b7 1566 print_address_demangle (options, gdbarch, func_addr, stream, demangle);
132c57b4
TT
1567}
1568
1569
79a45b7d 1570/* Print on STREAM using the given OPTIONS the index for the element
e79af960
JB
1571 at INDEX of an array whose index type is INDEX_TYPE. */
1572
1573void
1574maybe_print_array_index (struct type *index_type, LONGEST index,
79a45b7d
TT
1575 struct ui_file *stream,
1576 const struct value_print_options *options)
e79af960
JB
1577{
1578 struct value *index_value;
1579
79a45b7d 1580 if (!options->print_array_indexes)
e79af960
JB
1581 return;
1582
1583 index_value = value_from_longest (index_type, index);
1584
79a45b7d
TT
1585 LA_PRINT_ARRAY_INDEX (index_value, stream, options);
1586}
e79af960 1587
c906108c 1588/* Called by various <lang>_val_print routines to print elements of an
c5aa993b 1589 array in the form "<elem1>, <elem2>, <elem3>, ...".
c906108c 1590
c5aa993b
JM
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,
581e13c1 1594 perhaps we should try to use that notation when appropriate. */
c906108c
SS
1595
1596void
490f124f
PA
1597val_print_array_elements (struct type *type,
1598 const gdb_byte *valaddr, int embedded_offset,
a2bd3dcd 1599 CORE_ADDR address, struct ui_file *stream,
79a45b7d 1600 int recurse,
0e03807e 1601 const struct value *val,
79a45b7d 1602 const struct value_print_options *options,
fba45db2 1603 unsigned int i)
c906108c
SS
1604{
1605 unsigned int things_printed = 0;
1606 unsigned len;
e79af960 1607 struct type *elttype, *index_type;
c906108c
SS
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;
dbc98a8b 1614 LONGEST low_bound, high_bound;
c5aa993b 1615
c906108c
SS
1616 elttype = TYPE_TARGET_TYPE (type);
1617 eltlen = TYPE_LENGTH (check_typedef (elttype));
e79af960 1618 index_type = TYPE_INDEX_TYPE (type);
c906108c 1619
dbc98a8b 1620 if (get_array_bounds (type, &low_bound, &high_bound))
75be741b
JB
1621 {
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;
1631 }
e936309c
JB
1632 else
1633 {
dbc98a8b
KW
1634 warning (_("unable to get bounds of array, assuming null array"));
1635 low_bound = 0;
1636 len = 0;
168de233
JB
1637 }
1638
c906108c
SS
1639 annotate_array_section_begin (i, elttype);
1640
79a45b7d 1641 for (; i < len && things_printed < options->print_max; i++)
c906108c
SS
1642 {
1643 if (i != 0)
1644 {
79a45b7d 1645 if (options->prettyprint_arrays)
c906108c
SS
1646 {
1647 fprintf_filtered (stream, ",\n");
1648 print_spaces_filtered (2 + 2 * recurse, stream);
1649 }
1650 else
1651 {
1652 fprintf_filtered (stream, ", ");
1653 }
1654 }
1655 wrap_here (n_spaces (2 + 2 * recurse));
dbc98a8b 1656 maybe_print_array_index (index_type, i + low_bound,
79a45b7d 1657 stream, options);
c906108c
SS
1658
1659 rep1 = i + 1;
1660 reps = 1;
35bef4fd
TT
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)
c906108c 1664 {
35bef4fd
TT
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))
1672 {
1673 ++reps;
1674 ++rep1;
1675 }
c906108c
SS
1676 }
1677
79a45b7d 1678 if (reps > options->repeat_count_threshold)
c906108c 1679 {
490f124f
PA
1680 val_print (elttype, valaddr, embedded_offset + i * eltlen,
1681 address, stream, recurse + 1, val, options,
1682 current_language);
c906108c
SS
1683 annotate_elt_rep (reps);
1684 fprintf_filtered (stream, " <repeats %u times>", reps);
1685 annotate_elt_rep_end ();
1686
1687 i = rep1 - 1;
79a45b7d 1688 things_printed += options->repeat_count_threshold;
c906108c
SS
1689 }
1690 else
1691 {
490f124f
PA
1692 val_print (elttype, valaddr, embedded_offset + i * eltlen,
1693 address,
0e03807e 1694 stream, recurse + 1, val, options, current_language);
c906108c
SS
1695 annotate_elt ();
1696 things_printed++;
1697 }
1698 }
1699 annotate_array_section_end ();
1700 if (i < len)
1701 {
1702 fprintf_filtered (stream, "...");
1703 }
1704}
1705
917317f4
JM
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
581e13c1 1709 pointed to by ERRNOPTR if ERRNOPTR is non-null. */
917317f4
JM
1710
1711/* FIXME: cagney/1999-10-14: Only used by val_print_string. Can this
1712 function be eliminated. */
1713
1714static int
3e43a32a
MS
1715partial_memory_read (CORE_ADDR memaddr, gdb_byte *myaddr,
1716 int len, int *errnoptr)
917317f4 1717{
581e13c1
MS
1718 int nread; /* Number of bytes actually read. */
1719 int errcode; /* Error from last read. */
917317f4 1720
581e13c1 1721 /* First try a complete read. */
917317f4
JM
1722 errcode = target_read_memory (memaddr, myaddr, len);
1723 if (errcode == 0)
1724 {
581e13c1 1725 /* Got it all. */
917317f4
JM
1726 nread = len;
1727 }
1728 else
1729 {
581e13c1 1730 /* Loop, reading one byte at a time until we get as much as we can. */
917317f4
JM
1731 for (errcode = 0, nread = 0; len > 0 && errcode == 0; nread++, len--)
1732 {
1733 errcode = target_read_memory (memaddr++, myaddr++, 1);
1734 }
581e13c1 1735 /* If an error, the last read was unsuccessful, so adjust count. */
917317f4
JM
1736 if (errcode != 0)
1737 {
1738 nread--;
1739 }
1740 }
1741 if (errnoptr != NULL)
1742 {
1743 *errnoptr = errcode;
1744 }
1745 return (nread);
1746}
1747
ae6a3a4c
TJB
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.
1753
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.
1759
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.
1763
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
581e13c1 1766 given LEN). Besides, it is used much more often than target_read_string
ae6a3a4c
TJB
1767 so it is more tested. Perhaps callers of target_read_string should use
1768 this function instead? */
c906108c
SS
1769
1770int
ae6a3a4c 1771read_string (CORE_ADDR addr, int len, int width, unsigned int fetchlimit,
e17a4113 1772 enum bfd_endian byte_order, gdb_byte **buffer, int *bytes_read)
c906108c 1773{
ae6a3a4c
TJB
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. */
3e43a32a
MS
1778 gdb_byte *bufptr; /* Pointer to next available byte in
1779 buffer. */
ae6a3a4c
TJB
1780 gdb_byte *limit; /* First location past end of fetch buffer. */
1781 struct cleanup *old_chain = NULL; /* Top of the old cleanup chain. */
1782
1783 /* Decide how large of chunks to try to read in one operation. This
c906108c
SS
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
ae6a3a4c 1786 are looking for a NUL terminator to end the fetching, so we might as
c906108c
SS
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. */
1791
1792 chunksize = (len == -1 ? min (8, fetchlimit) : fetchlimit);
1793
ae6a3a4c
TJB
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. */
c906108c
SS
1796
1797 found_nul = 0;
b5096abe
PM
1798 *buffer = NULL;
1799
1800 old_chain = make_cleanup (free_current_contents, buffer);
c906108c
SS
1801
1802 if (len > 0)
1803 {
ae6a3a4c
TJB
1804 *buffer = (gdb_byte *) xmalloc (len * width);
1805 bufptr = *buffer;
c906108c 1806
917317f4 1807 nfetch = partial_memory_read (addr, bufptr, len * width, &errcode)
c906108c
SS
1808 / width;
1809 addr += nfetch * width;
1810 bufptr += nfetch * width;
1811 }
1812 else if (len == -1)
1813 {
1814 unsigned long bufsize = 0;
ae6a3a4c 1815
c906108c
SS
1816 do
1817 {
1818 QUIT;
1819 nfetch = min (chunksize, fetchlimit - bufsize);
1820
ae6a3a4c
TJB
1821 if (*buffer == NULL)
1822 *buffer = (gdb_byte *) xmalloc (nfetch * width);
c906108c 1823 else
b5096abe
PM
1824 *buffer = (gdb_byte *) xrealloc (*buffer,
1825 (nfetch + bufsize) * width);
c906108c 1826
ae6a3a4c 1827 bufptr = *buffer + bufsize * width;
c906108c
SS
1828 bufsize += nfetch;
1829
ae6a3a4c 1830 /* Read as much as we can. */
917317f4 1831 nfetch = partial_memory_read (addr, bufptr, nfetch * width, &errcode)
ae6a3a4c 1832 / width;
c906108c 1833
ae6a3a4c 1834 /* Scan this chunk for the null character that terminates the string
c906108c
SS
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
ae6a3a4c
TJB
1837 after the null character, or at the next character after the end
1838 of the buffer. */
c906108c
SS
1839
1840 limit = bufptr + nfetch * width;
1841 while (bufptr < limit)
1842 {
1843 unsigned long c;
1844
e17a4113 1845 c = extract_unsigned_integer (bufptr, width, byte_order);
c906108c
SS
1846 addr += width;
1847 bufptr += width;
1848 if (c == 0)
1849 {
1850 /* We don't care about any error which happened after
ae6a3a4c 1851 the NUL terminator. */
c906108c
SS
1852 errcode = 0;
1853 found_nul = 1;
1854 break;
1855 }
1856 }
1857 }
c5aa993b 1858 while (errcode == 0 /* no error */
ae6a3a4c
TJB
1859 && bufptr - *buffer < fetchlimit * width /* no overrun */
1860 && !found_nul); /* haven't found NUL yet */
c906108c
SS
1861 }
1862 else
ae6a3a4c
TJB
1863 { /* Length of string is really 0! */
1864 /* We always allocate *buffer. */
1865 *buffer = bufptr = xmalloc (1);
c906108c
SS
1866 errcode = 0;
1867 }
1868
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). */
ae6a3a4c
TJB
1871 *bytes_read = bufptr - *buffer;
1872
1873 QUIT;
1874
1875 discard_cleanups (old_chain);
1876
1877 return errcode;
1878}
1879
3b2b8fea
TT
1880/* Return true if print_wchar can display W without resorting to a
1881 numeric escape, false otherwise. */
1882
1883static int
1884wchar_printable (gdb_wchar_t w)
1885{
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'));
1891}
1892
1893/* A helper function that converts the contents of STRING to wide
1894 characters and then appends them to OUTPUT. */
1895
1896static void
1897append_string_as_wide (const char *string,
1898 struct obstack *output)
1899{
1900 for (; *string; ++string)
1901 {
1902 gdb_wchar_t w = gdb_btowc (*string);
1903 obstack_grow (output, &w, sizeof (gdb_wchar_t));
1904 }
1905}
1906
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. */
1915
1916static void
1917print_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)
1922{
1923 int need_escape = *need_escapep;
1924
1925 *need_escapep = 0;
1926 if (gdb_iswprint (w) && (!need_escape || (!gdb_iswdigit (w)
1927 && w != LCST ('8')
1928 && w != LCST ('9'))))
1929 {
1930 gdb_wchar_t wchar = w;
1931
1932 if (w == gdb_btowc (quoter) || w == LCST ('\\'))
1933 obstack_grow_wstr (output, LCST ("\\"));
1934 obstack_grow (output, &wchar, sizeof (gdb_wchar_t));
1935 }
1936 else
1937 {
1938 switch (w)
1939 {
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:
1962 {
1963 int i;
1964
1965 for (i = 0; i + width <= orig_len; i += width)
1966 {
1967 char octal[30];
1968 ULONGEST value;
1969
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)
08850b56
PM
1975 xsnprintf (octal, sizeof (octal), "\\%.3o",
1976 (int) (value & 0777));
3b2b8fea 1977 else
08850b56 1978 xsnprintf (octal, sizeof (octal), "\\x%lx", (long) value);
3b2b8fea
TT
1979 append_string_as_wide (octal, output);
1980 }
1981 /* If we somehow have extra bytes, print them now. */
1982 while (i < orig_len)
1983 {
1984 char octal[5];
1985
08850b56 1986 xsnprintf (octal, sizeof (octal), "\\%.3o", orig[i] & 0xff);
3b2b8fea
TT
1987 append_string_as_wide (octal, output);
1988 ++i;
1989 }
1990
1991 *need_escapep = 1;
1992 }
1993 break;
1994 }
1995 }
1996}
1997
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. */
2001
2002void
2003generic_emit_char (int c, struct type *type, struct ui_file *stream,
2004 int quoter, const char *encoding)
2005{
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;
2013
2014 buf = alloca (TYPE_LENGTH (type));
2015 pack_long (buf, type, c);
2016
2017 iter = make_wchar_iterator (buf, TYPE_LENGTH (type),
2018 encoding, TYPE_LENGTH (type));
2019 cleanups = make_cleanup_wchar_iterator (iter);
2020
2021 /* This holds the printable form of the wchar_t data. */
2022 obstack_init (&wchar_buf);
2023 make_cleanup_obstack_free (&wchar_buf);
2024
2025 while (1)
2026 {
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;
2033
2034 num_chars = wchar_iterate (iter, &result, &chars, &buf, &buflen);
2035 if (num_chars < 0)
2036 break;
2037 if (num_chars > 0)
2038 {
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;
2045
2046 print_escape = 0;
2047 for (i = 0; i < num_chars; ++i)
2048 if (!wchar_printable (chars[i]))
2049 {
2050 print_escape = 1;
2051 break;
2052 }
2053
2054 if (!print_escape)
2055 {
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);
2060 }
2061 }
2062
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);
2067 }
2068
2069 /* The output in the host encoding. */
2070 obstack_init (&output);
2071 make_cleanup_obstack_free (&output);
2072
2073 convert_between_encodings (INTERMEDIATE_ENCODING, host_charset (),
ac91cd70 2074 (gdb_byte *) obstack_base (&wchar_buf),
3b2b8fea 2075 obstack_object_size (&wchar_buf),
fff10684 2076 sizeof (gdb_wchar_t), &output, translit_char);
3b2b8fea
TT
2077 obstack_1grow (&output, '\0');
2078
2079 fputs_filtered (obstack_base (&output), stream);
2080
2081 do_cleanups (cleanups);
2082}
2083
0d63ecda
KS
2084/* Return the repeat count of the next character/byte in ITER,
2085 storing the result in VEC. */
2086
2087static int
2088count_next_character (struct wchar_iterator *iter,
2089 VEC (converted_character_d) **vec)
2090{
2091 struct converted_character *current;
2092
2093 if (VEC_empty (converted_character_d, *vec))
2094 {
2095 struct converted_character tmp;
2096 gdb_wchar_t *chars;
2097
2098 tmp.num_chars
2099 = wchar_iterate (iter, &tmp.result, &chars, &tmp.buf, &tmp.buflen);
2100 if (tmp.num_chars > 0)
2101 {
2102 gdb_assert (tmp.num_chars < MAX_WCHARS);
2103 memcpy (tmp.chars, chars, tmp.num_chars * sizeof (gdb_wchar_t));
2104 }
2105 VEC_safe_push (converted_character_d, *vec, &tmp);
2106 }
2107
2108 current = VEC_last (converted_character_d, *vec);
2109
2110 /* Count repeated characters or bytes. */
2111 current->repeat_count = 1;
2112 if (current->num_chars == -1)
2113 {
2114 /* EOF */
2115 return -1;
2116 }
2117 else
2118 {
2119 gdb_wchar_t *chars;
2120 struct converted_character d;
2121 int repeat;
2122
2123 d.repeat_count = 0;
2124
2125 while (1)
2126 {
2127 /* Get the next character. */
2128 d.num_chars
2129 = wchar_iterate (iter, &d.result, &chars, &d.buf, &d.buflen);
2130
2131 /* If a character was successfully converted, save the character
2132 into the converted character. */
2133 if (d.num_chars > 0)
2134 {
2135 gdb_assert (d.num_chars < MAX_WCHARS);
2136 memcpy (d.chars, chars, WCHAR_BUFLEN (d.num_chars));
2137 }
2138
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)
2142 {
2143 /* There are two cases to consider:
2144
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;
2156 }
2157 else
2158 break;
2159 }
2160
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;
2165 }
2166}
2167
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. */
2172
2173static void
2174print_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)
2179{
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;
2185
2186 /* Set the start state. */
2187 idx = 0;
2188 last = state = START;
2189 elem = NULL;
2190
2191 while (1)
2192 {
2193 switch (state)
2194 {
2195 case START:
2196 /* Nothing to do. */
2197 break;
2198
2199 case SINGLE:
2200 {
2201 int j;
2202
2203 /* We are outputting a single character
2204 (< options->repeat_count_threshold). */
2205
2206 if (last != SINGLE)
2207 {
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 (", "));
0d63ecda
KS
2212 obstack_grow (obstack, &wide_quote_char, sizeof (gdb_wchar_t));
2213 }
2214 /* Output the character. */
2215 for (j = 0; j < elem->repeat_count; ++j)
2216 {
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);
2223 }
2224 }
2225 break;
2226
2227 case REPEAT:
2228 {
2229 int j;
2230 char *s;
2231
2232 /* We are outputting a character with a repeat count
2233 greater than options->repeat_count_threshold. */
2234
2235 if (last == SINGLE)
2236 {
2237 /* We were outputting a single string. Terminate the
2238 string. */
0d63ecda
KS
2239 obstack_grow (obstack, &wide_quote_char, sizeof (gdb_wchar_t));
2240 }
2241 if (last != START)
2242 obstack_grow_wstr (obstack, LCST (", "));
2243
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)
2255 {
2256 gdb_wchar_t w = gdb_btowc (s[j]);
2257 obstack_grow (obstack, &w, sizeof (gdb_wchar_t));
2258 }
2259 xfree (s);
2260 }
2261 break;
2262
2263 case INCOMPLETE:
2264 /* We are outputting an incomplete sequence. */
2265 if (last == SINGLE)
2266 {
2267 /* If we were outputting a string of SINGLE characters,
2268 terminate the quote. */
0d63ecda
KS
2269 obstack_grow (obstack, &wide_quote_char, sizeof (gdb_wchar_t));
2270 }
2271 if (last != START)
2272 obstack_grow_wstr (obstack, LCST (", "));
2273
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 (">"));
2279
2280 /* We do not attempt to outupt anything after this. */
2281 state = FINISH;
2282 break;
2283
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)
e93a8774 2289 obstack_grow (obstack, &wide_quote_char, sizeof (gdb_wchar_t));
0d63ecda
KS
2290
2291 return;
2292 }
2293
2294 /* Get the next element and state. */
2295 last = state;
2296 if (state != FINISH)
2297 {
2298 elem = VEC_index (converted_character_d, chars, idx++);
2299 switch (elem->result)
2300 {
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;
2308
2309 case wchar_iterate_incomplete:
2310 state = INCOMPLETE;
2311 break;
2312
2313 case wchar_iterate_eof:
2314 state = FINISH;
2315 break;
2316 }
2317 }
2318 }
2319}
2320
3b2b8fea
TT
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. */
2330
2331void
2332generic_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)
2337{
2338 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
2339 unsigned int i;
3b2b8fea
TT
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;
0d63ecda
KS
2345 struct converted_character *last;
2346 VEC (converted_character_d) *converted_chars;
3b2b8fea
TT
2347
2348 if (length == -1)
2349 {
2350 unsigned long current_char = 1;
2351
2352 for (i = 0; current_char; ++i)
2353 {
2354 QUIT;
2355 current_char = extract_unsigned_integer (string + i * width,
2356 width, byte_order);
2357 }
2358 length = i;
2359 }
2360
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--;
2370
2371 if (length == 0)
2372 {
2373 fputs_filtered ("\"\"", stream);
2374 return;
2375 }
2376
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);
0d63ecda
KS
2380 converted_chars = NULL;
2381 make_cleanup (VEC_cleanup (converted_character_d), &converted_chars);
3b2b8fea 2382
0d63ecda
KS
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)
3b2b8fea 2387 {
0d63ecda 2388 int r;
3b2b8fea
TT
2389
2390 QUIT;
2391
0d63ecda
KS
2392 /* Grab the next character and repeat count. */
2393 r = count_next_character (iter, &converted_chars);
3b2b8fea 2394
0d63ecda
KS
2395 /* If less than zero, the end of the input string was reached. */
2396 if (r < 0)
2397 break;
3b2b8fea 2398
0d63ecda
KS
2399 /* Otherwise, add the count to the total print count and get
2400 the next character. */
2401 i += r;
2402 }
3b2b8fea 2403
0d63ecda
KS
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);
3b2b8fea 2408
0d63ecda
KS
2409 /* Ensure that CONVERTED_CHARS is terminated. */
2410 last->result = wchar_iterate_eof;
3b2b8fea 2411
0d63ecda
KS
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);
3b2b8fea 2416
0d63ecda
KS
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);
3b2b8fea
TT
2420
2421 if (force_ellipses || !finished)
2422 obstack_grow_wstr (&wchar_buf, LCST ("..."));
2423
2424 /* OUTPUT is where we collect `char's for printing. */
2425 obstack_init (&output);
2426 make_cleanup_obstack_free (&output);
2427
2428 convert_between_encodings (INTERMEDIATE_ENCODING, host_charset (),
ac91cd70 2429 (gdb_byte *) obstack_base (&wchar_buf),
3b2b8fea 2430 obstack_object_size (&wchar_buf),
fff10684 2431 sizeof (gdb_wchar_t), &output, translit_char);
3b2b8fea
TT
2432 obstack_1grow (&output, '\0');
2433
2434 fputs_filtered (obstack_base (&output), stream);
2435
2436 do_cleanups (cleanup);
2437}
2438
ae6a3a4c
TJB
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,
09ca9e2e
TT
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. */
ae6a3a4c
TJB
2446
2447int
09ca9e2e
TT
2448val_print_string (struct type *elttype, const char *encoding,
2449 CORE_ADDR addr, int len,
6c7a06a3 2450 struct ui_file *stream,
ae6a3a4c
TJB
2451 const struct value_print_options *options)
2452{
2453 int force_ellipsis = 0; /* Force ellipsis to be printed if nonzero. */
2454 int errcode; /* Errno returned from bad reads. */
581e13c1 2455 int found_nul; /* Non-zero if we found the nul char. */
ae6a3a4c
TJB
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. */
5af949e3 2460 struct gdbarch *gdbarch = get_type_arch (elttype);
e17a4113 2461 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
6c7a06a3 2462 int width = TYPE_LENGTH (elttype);
ae6a3a4c
TJB
2463
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. */
2471
3e43a32a
MS
2472 fetchlimit = (len == -1 ? options->print_max : min (len,
2473 options->print_max));
ae6a3a4c 2474
e17a4113
UW
2475 errcode = read_string (addr, len, width, fetchlimit, byte_order,
2476 &buffer, &bytes_read);
ae6a3a4c
TJB
2477 old_chain = make_cleanup (xfree, buffer);
2478
2479 addr += bytes_read;
c906108c 2480
3e43a32a
MS
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. */
ae6a3a4c
TJB
2484
2485 /* Determine found_nul by looking at the last character read. */
e17a4113
UW
2486 found_nul = extract_unsigned_integer (buffer + bytes_read - width, width,
2487 byte_order) == 0;
c906108c
SS
2488 if (len == -1 && !found_nul)
2489 {
777ea8f1 2490 gdb_byte *peekbuf;
c906108c 2491
ae6a3a4c 2492 /* We didn't find a NUL terminator we were looking for. Attempt
c5aa993b
JM
2493 to peek at the next character. If not successful, or it is not
2494 a null byte, then force ellipsis to be printed. */
c906108c 2495
777ea8f1 2496 peekbuf = (gdb_byte *) alloca (width);
c906108c
SS
2497
2498 if (target_read_memory (addr, peekbuf, width) == 0
e17a4113 2499 && extract_unsigned_integer (peekbuf, width, byte_order) != 0)
c906108c
SS
2500 force_ellipsis = 1;
2501 }
ae6a3a4c 2502 else if ((len >= 0 && errcode != 0) || (len > bytes_read / width))
c906108c
SS
2503 {
2504 /* Getting an error when we have a requested length, or fetching less
c5aa993b 2505 than the number of characters actually requested, always make us
ae6a3a4c 2506 print ellipsis. */
c906108c
SS
2507 force_ellipsis = 1;
2508 }
2509
c906108c
SS
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. */
ae6a3a4c 2513 if (errcode == 0 || bytes_read > 0)
c906108c 2514 {
be759fcf 2515 LA_PRINT_STRING (stream, elttype, buffer, bytes_read / width,
3a772aa4 2516 encoding, force_ellipsis, options);
c906108c
SS
2517 }
2518
2519 if (errcode != 0)
2520 {
2521 if (errcode == EIO)
2522 {
b012acdd 2523 fprintf_filtered (stream, "<Address ");
5af949e3 2524 fputs_filtered (paddress (gdbarch, addr), stream);
c906108c
SS
2525 fprintf_filtered (stream, " out of bounds>");
2526 }
2527 else
2528 {
b012acdd 2529 fprintf_filtered (stream, "<Error reading address ");
5af949e3 2530 fputs_filtered (paddress (gdbarch, addr), stream);
c906108c
SS
2531 fprintf_filtered (stream, ": %s>", safe_strerror (errcode));
2532 }
2533 }
ae6a3a4c 2534
c906108c
SS
2535 gdb_flush (stream);
2536 do_cleanups (old_chain);
ae6a3a4c
TJB
2537
2538 return (bytes_read / width);
c906108c 2539}
c906108c 2540\f
c5aa993b 2541
09e6485f
PA
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. */
2545
2546static unsigned input_radix_1 = 10;
2547
c906108c
SS
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! */
2551
c906108c 2552static void
fba45db2 2553set_input_radix (char *args, int from_tty, struct cmd_list_element *c)
c906108c 2554{
09e6485f 2555 set_input_radix_1 (from_tty, input_radix_1);
c906108c
SS
2556}
2557
c906108c 2558static void
fba45db2 2559set_input_radix_1 (int from_tty, unsigned radix)
c906108c
SS
2560{
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.
581e13c1 2566 (FIXME). */
c906108c
SS
2567
2568 if (radix < 2)
2569 {
09e6485f 2570 input_radix_1 = input_radix;
8a3fe4f8 2571 error (_("Nonsense input radix ``decimal %u''; input radix unchanged."),
c906108c
SS
2572 radix);
2573 }
09e6485f 2574 input_radix_1 = input_radix = radix;
c906108c
SS
2575 if (from_tty)
2576 {
3e43a32a
MS
2577 printf_filtered (_("Input radix now set to "
2578 "decimal %u, hex %x, octal %o.\n"),
c906108c
SS
2579 radix, radix, radix);
2580 }
2581}
2582
09e6485f
PA
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. */
2586
2587static unsigned output_radix_1 = 10;
2588
c906108c 2589static void
fba45db2 2590set_output_radix (char *args, int from_tty, struct cmd_list_element *c)
c906108c 2591{
09e6485f 2592 set_output_radix_1 (from_tty, output_radix_1);
c906108c
SS
2593}
2594
2595static void
fba45db2 2596set_output_radix_1 (int from_tty, unsigned radix)
c906108c
SS
2597{
2598 /* Validate the radix and disallow ones that we aren't prepared to
581e13c1 2599 handle correctly, leaving the radix unchanged. */
c906108c
SS
2600 switch (radix)
2601 {
2602 case 16:
79a45b7d 2603 user_print_options.output_format = 'x'; /* hex */
c906108c
SS
2604 break;
2605 case 10:
79a45b7d 2606 user_print_options.output_format = 0; /* decimal */
c906108c
SS
2607 break;
2608 case 8:
79a45b7d 2609 user_print_options.output_format = 'o'; /* octal */
c906108c
SS
2610 break;
2611 default:
09e6485f 2612 output_radix_1 = output_radix;
3e43a32a
MS
2613 error (_("Unsupported output radix ``decimal %u''; "
2614 "output radix unchanged."),
c906108c
SS
2615 radix);
2616 }
09e6485f 2617 output_radix_1 = output_radix = radix;
c906108c
SS
2618 if (from_tty)
2619 {
3e43a32a
MS
2620 printf_filtered (_("Output radix now set to "
2621 "decimal %u, hex %x, octal %o.\n"),
c906108c
SS
2622 radix, radix, radix);
2623 }
2624}
2625
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.
2629
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
581e13c1 2632 the 'set input-radix' command. */
c906108c
SS
2633
2634static void
fba45db2 2635set_radix (char *arg, int from_tty)
c906108c
SS
2636{
2637 unsigned radix;
2638
bb518678 2639 radix = (arg == NULL) ? 10 : parse_and_eval_long (arg);
c906108c
SS
2640 set_output_radix_1 (0, radix);
2641 set_input_radix_1 (0, radix);
2642 if (from_tty)
2643 {
3e43a32a
MS
2644 printf_filtered (_("Input and output radices now set to "
2645 "decimal %u, hex %x, octal %o.\n"),
c906108c
SS
2646 radix, radix, radix);
2647 }
2648}
2649
581e13c1 2650/* Show both the input and output radices. */
c906108c 2651
c906108c 2652static void
fba45db2 2653show_radix (char *arg, int from_tty)
c906108c
SS
2654{
2655 if (from_tty)
2656 {
2657 if (input_radix == output_radix)
2658 {
3e43a32a
MS
2659 printf_filtered (_("Input and output radices set to "
2660 "decimal %u, hex %x, octal %o.\n"),
c906108c
SS
2661 input_radix, input_radix, input_radix);
2662 }
2663 else
2664 {
3e43a32a
MS
2665 printf_filtered (_("Input radix set to decimal "
2666 "%u, hex %x, octal %o.\n"),
c906108c 2667 input_radix, input_radix, input_radix);
3e43a32a
MS
2668 printf_filtered (_("Output radix set to decimal "
2669 "%u, hex %x, octal %o.\n"),
c906108c
SS
2670 output_radix, output_radix, output_radix);
2671 }
2672 }
2673}
c906108c 2674\f
c5aa993b 2675
c906108c 2676static void
fba45db2 2677set_print (char *arg, int from_tty)
c906108c
SS
2678{
2679 printf_unfiltered (
c5aa993b 2680 "\"set print\" must be followed by the name of a print subcommand.\n");
c906108c
SS
2681 help_list (setprintlist, "set print ", -1, gdb_stdout);
2682}
2683
c906108c 2684static void
fba45db2 2685show_print (char *args, int from_tty)
c906108c
SS
2686{
2687 cmd_show_list (showprintlist, from_tty, "");
2688}
2689\f
2690void
fba45db2 2691_initialize_valprint (void)
c906108c 2692{
c906108c 2693 add_prefix_cmd ("print", no_class, set_print,
1bedd215 2694 _("Generic command for setting how things print."),
c906108c 2695 &setprintlist, "set print ", 0, &setlist);
c5aa993b 2696 add_alias_cmd ("p", "print", no_class, 1, &setlist);
581e13c1 2697 /* Prefer set print to set prompt. */
c906108c
SS
2698 add_alias_cmd ("pr", "print", no_class, 1, &setlist);
2699
2700 add_prefix_cmd ("print", no_class, show_print,
1bedd215 2701 _("Generic command for showing print settings."),
c906108c 2702 &showprintlist, "show print ", 0, &showlist);
c5aa993b
JM
2703 add_alias_cmd ("p", "print", no_class, 1, &showlist);
2704 add_alias_cmd ("pr", "print", no_class, 1, &showlist);
c906108c 2705
79a45b7d
TT
2706 add_setshow_uinteger_cmd ("elements", no_class,
2707 &user_print_options.print_max, _("\
35096d9d
AC
2708Set limit on string chars or array elements to print."), _("\
2709Show limit on string chars or array elements to print."), _("\
f81d1120 2710\"set print elements unlimited\" causes there to be no limit."),
35096d9d 2711 NULL,
920d2a44 2712 show_print_max,
35096d9d 2713 &setprintlist, &showprintlist);
c906108c 2714
79a45b7d
TT
2715 add_setshow_boolean_cmd ("null-stop", no_class,
2716 &user_print_options.stop_print_at_null, _("\
5bf193a2
AC
2717Set printing of char arrays to stop at first null char."), _("\
2718Show printing of char arrays to stop at first null char."), NULL,
2719 NULL,
920d2a44 2720 show_stop_print_at_null,
5bf193a2 2721 &setprintlist, &showprintlist);
c906108c 2722
35096d9d 2723 add_setshow_uinteger_cmd ("repeats", no_class,
79a45b7d 2724 &user_print_options.repeat_count_threshold, _("\
35096d9d
AC
2725Set threshold for repeated print elements."), _("\
2726Show threshold for repeated print elements."), _("\
f81d1120 2727\"set print repeats unlimited\" causes all elements to be individually printed."),
35096d9d 2728 NULL,
920d2a44 2729 show_repeat_count_threshold,
35096d9d 2730 &setprintlist, &showprintlist);
c906108c 2731
79a45b7d
TT
2732 add_setshow_boolean_cmd ("pretty", class_support,
2733 &user_print_options.prettyprint_structs, _("\
5bf193a2
AC
2734Set prettyprinting of structures."), _("\
2735Show prettyprinting of structures."), NULL,
2736 NULL,
920d2a44 2737 show_prettyprint_structs,
5bf193a2
AC
2738 &setprintlist, &showprintlist);
2739
79a45b7d
TT
2740 add_setshow_boolean_cmd ("union", class_support,
2741 &user_print_options.unionprint, _("\
5bf193a2
AC
2742Set printing of unions interior to structures."), _("\
2743Show printing of unions interior to structures."), NULL,
2744 NULL,
920d2a44 2745 show_unionprint,
5bf193a2
AC
2746 &setprintlist, &showprintlist);
2747
79a45b7d
TT
2748 add_setshow_boolean_cmd ("array", class_support,
2749 &user_print_options.prettyprint_arrays, _("\
5bf193a2
AC
2750Set prettyprinting of arrays."), _("\
2751Show prettyprinting of arrays."), NULL,
2752 NULL,
920d2a44 2753 show_prettyprint_arrays,
5bf193a2
AC
2754 &setprintlist, &showprintlist);
2755
79a45b7d
TT
2756 add_setshow_boolean_cmd ("address", class_support,
2757 &user_print_options.addressprint, _("\
5bf193a2
AC
2758Set printing of addresses."), _("\
2759Show printing of addresses."), NULL,
2760 NULL,
920d2a44 2761 show_addressprint,
5bf193a2 2762 &setprintlist, &showprintlist);
c906108c 2763
9cb709b6
TT
2764 add_setshow_boolean_cmd ("symbol", class_support,
2765 &user_print_options.symbol_print, _("\
2766Set printing of symbol names when printing pointers."), _("\
2767Show printing of symbol names when printing pointers."),
2768 NULL, NULL,
2769 show_symbol_print,
2770 &setprintlist, &showprintlist);
2771
1e8fb976
PA
2772 add_setshow_zuinteger_cmd ("input-radix", class_support, &input_radix_1,
2773 _("\
35096d9d
AC
2774Set default input radix for entering numbers."), _("\
2775Show default input radix for entering numbers."), NULL,
1e8fb976
PA
2776 set_input_radix,
2777 show_input_radix,
2778 &setlist, &showlist);
35096d9d 2779
1e8fb976
PA
2780 add_setshow_zuinteger_cmd ("output-radix", class_support, &output_radix_1,
2781 _("\
35096d9d
AC
2782Set default output radix for printing of values."), _("\
2783Show default output radix for printing of values."), NULL,
1e8fb976
PA
2784 set_output_radix,
2785 show_output_radix,
2786 &setlist, &showlist);
c906108c 2787
cb1a6d5f
AC
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
b66df561 2791 command. So the usual deprecated_add_set_cmd() and [deleted]
581e13c1 2792 add_show_from_set() commands aren't really appropriate. */
b66df561
AC
2793 /* FIXME: i18n: With the new add_setshow_integer command, that is no
2794 longer true - show can display anything. */
1a966eab
AC
2795 add_cmd ("radix", class_support, set_radix, _("\
2796Set default input and output number radices.\n\
c906108c 2797Use 'set input-radix' or 'set output-radix' to independently set each.\n\
1a966eab 2798Without an argument, sets both radices back to the default value of 10."),
c906108c 2799 &setlist);
1a966eab
AC
2800 add_cmd ("radix", class_support, show_radix, _("\
2801Show the default input and output number radices.\n\
2802Use 'show input-radix' or 'show output-radix' to independently show each."),
c906108c
SS
2803 &showlist);
2804
e79af960 2805 add_setshow_boolean_cmd ("array-indexes", class_support,
79a45b7d 2806 &user_print_options.print_array_indexes, _("\
e79af960
JB
2807Set printing of array indexes."), _("\
2808Show printing of array indexes"), NULL, NULL, show_print_array_indexes,
2809 &setprintlist, &showprintlist);
c906108c 2810}
This page took 1.992219 seconds and 4 git commands to generate.