]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/valprint.c
import gdb-1999-09-08 snapshot
[thirdparty/binutils-gdb.git] / gdb / valprint.c
CommitLineData
c906108c
SS
1/* Print values for GDB, the GNU debugger.
2 Copyright 1986, 1988, 1989, 1991, 1992, 1993, 1994, 1998
c5aa993b 3 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
9 the Free Software Foundation; either version 2 of the License, or
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
JM
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
c906108c
SS
21
22#include "defs.h"
23#include "gdb_string.h"
24#include "symtab.h"
25#include "gdbtypes.h"
26#include "value.h"
27#include "gdbcore.h"
28#include "gdbcmd.h"
29#include "target.h"
30#include "obstack.h"
31#include "language.h"
32#include "demangle.h"
33#include "annotate.h"
34#include "valprint.h"
35
36#include <errno.h>
37
38/* Prototypes for local functions */
39
c5aa993b
JM
40static void print_hex_chars PARAMS ((GDB_FILE *, unsigned char *,
41 unsigned int));
c906108c
SS
42
43static void show_print PARAMS ((char *, int));
44
45static void set_print PARAMS ((char *, int));
46
47static void set_radix PARAMS ((char *, int));
48
49static void show_radix PARAMS ((char *, int));
50
51static void set_input_radix PARAMS ((char *, int, struct cmd_list_element *));
52
53static void set_input_radix_1 PARAMS ((int, unsigned));
54
55static void set_output_radix PARAMS ((char *, int, struct cmd_list_element *));
56
57static void set_output_radix_1 PARAMS ((int, unsigned));
58
59void _initialize_valprint PARAMS ((void));
60
61/* Maximum number of chars to print for a string pointer value or vector
62 contents, or UINT_MAX for no limit. Note that "set print elements 0"
63 stores UINT_MAX in print_max, which displays in a show command as
64 "unlimited". */
65
66unsigned int print_max;
67#define PRINT_MAX_DEFAULT 200 /* Start print_max off at this value. */
68
69/* Default input and output radixes, and output format letter. */
70
71unsigned input_radix = 10;
72unsigned output_radix = 10;
73int output_format = 0;
74
75/* Print repeat counts if there are more than this many repetitions of an
76 element in an array. Referenced by the low level language dependent
77 print routines. */
78
79unsigned int repeat_count_threshold = 10;
80
81/* If nonzero, stops printing of char arrays at first null. */
82
83int stop_print_at_null;
84
85/* Controls pretty printing of structures. */
86
87int prettyprint_structs;
88
89/* Controls pretty printing of arrays. */
90
91int prettyprint_arrays;
92
93/* If nonzero, causes unions inside structures or other unions to be
94 printed. */
95
96int unionprint; /* Controls printing of nested unions. */
97
98/* If nonzero, causes machine addresses to be printed in certain contexts. */
99
100int addressprint; /* Controls printing of machine addresses */
c906108c 101\f
c5aa993b 102
c906108c
SS
103/* Print data of type TYPE located at VALADDR (within GDB), which came from
104 the inferior at address ADDRESS, onto stdio stream STREAM according to
105 FORMAT (a letter, or 0 for natural format using TYPE).
106
107 If DEREF_REF is nonzero, then dereference references, otherwise just print
108 them like pointers.
109
110 The PRETTY parameter controls prettyprinting.
111
112 If the data are a string pointer, returns the number of string characters
113 printed.
114
115 FIXME: The data at VALADDR is in target byte order. If gdb is ever
116 enhanced to be able to debug more than the single target it was compiled
117 for (specific CPU type and thus specific target byte ordering), then
118 either the print routines are going to have to take this into account,
119 or the data is going to have to be passed into here already converted
120 to the host byte ordering, whichever is more convenient. */
121
122
123int
124val_print (type, valaddr, embedded_offset, address,
c5aa993b 125 stream, format, deref_ref, recurse, pretty)
c906108c
SS
126 struct type *type;
127 char *valaddr;
128 int embedded_offset;
129 CORE_ADDR address;
130 GDB_FILE *stream;
131 int format;
132 int deref_ref;
133 int recurse;
134 enum val_prettyprint pretty;
135{
136 struct type *real_type = check_typedef (type);
137 if (pretty == Val_pretty_default)
138 {
139 pretty = prettyprint_structs ? Val_prettyprint : Val_no_prettyprint;
140 }
c5aa993b 141
c906108c
SS
142 QUIT;
143
144 /* Ensure that the type is complete and not just a stub. If the type is
145 only a stub and we can't find and substitute its complete type, then
146 print appropriate string and return. */
147
148 if (TYPE_FLAGS (real_type) & TYPE_FLAG_STUB)
149 {
150 fprintf_filtered (stream, "<incomplete type>");
151 gdb_flush (stream);
152 return (0);
153 }
c5aa993b 154
c906108c 155 return (LA_VAL_PRINT (type, valaddr, embedded_offset, address,
c5aa993b 156 stream, format, deref_ref, recurse, pretty));
c906108c
SS
157}
158
159/* Print the value VAL in C-ish syntax on stream STREAM.
160 FORMAT is a format-letter, or 0 for print in natural format of data type.
161 If the object printed is a string pointer, returns
162 the number of string bytes printed. */
163
164int
165value_print (val, stream, format, pretty)
166 value_ptr val;
167 GDB_FILE *stream;
168 int format;
169 enum val_prettyprint pretty;
170{
171 if (val == 0)
172 {
173 printf_filtered ("<address of value unknown>");
174 return 0;
175 }
176 if (VALUE_OPTIMIZED_OUT (val))
177 {
178 printf_filtered ("<value optimized out>");
179 return 0;
180 }
181 return LA_VALUE_PRINT (val, stream, format, pretty);
182}
183
184/* Called by various <lang>_val_print routines to print
185 TYPE_CODE_INT's. TYPE is the type. VALADDR is the address of the
186 value. STREAM is where to print the value. */
187
188void
189val_print_type_code_int (type, valaddr, stream)
190 struct type *type;
191 char *valaddr;
192 GDB_FILE *stream;
193{
194 if (TYPE_LENGTH (type) > sizeof (LONGEST))
195 {
196 LONGEST val;
197
198 if (TYPE_UNSIGNED (type)
199 && extract_long_unsigned_integer (valaddr, TYPE_LENGTH (type),
200 &val))
201 {
202 print_longest (stream, 'u', 0, val);
203 }
204 else
205 {
206 /* Signed, or we couldn't turn an unsigned value into a
207 LONGEST. For signed values, one could assume two's
208 complement (a reasonable assumption, I think) and do
209 better than this. */
210 print_hex_chars (stream, (unsigned char *) valaddr,
211 TYPE_LENGTH (type));
212 }
213 }
214 else
215 {
216#ifdef PRINT_TYPELESS_INTEGER
217 PRINT_TYPELESS_INTEGER (stream, type, unpack_long (type, valaddr));
218#else
219 print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0,
220 unpack_long (type, valaddr));
221#endif
222 }
223}
224
225/* Print a number according to FORMAT which is one of d,u,x,o,b,h,w,g.
226 The raison d'etre of this function is to consolidate printing of
227 LONG_LONG's into this one function. Some platforms have long longs but
228 don't have a printf() that supports "ll" in the format string. We handle
229 these by seeing if the number is representable as either a signed or
230 unsigned long, depending upon what format is desired, and if not we just
231 bail out and print the number in hex.
232
233 The format chars b,h,w,g are from print_scalar_formatted(). If USE_LOCAL,
234 format it according to the current language (this should be used for most
235 integers which GDB prints, the exception is things like protocols where
236 the format of the integer is a protocol thing, not a user-visible thing).
c5aa993b 237 */
c906108c
SS
238
239#if defined (CC_HAS_LONG_LONG) && !defined (PRINTF_HAS_LONG_LONG)
c5aa993b 240static void print_decimal PARAMS ((GDB_FILE * stream, char *sign, int use_local, ULONGEST val_ulong));
c906108c
SS
241static void
242print_decimal (stream, sign, use_local, val_ulong)
243 GDB_FILE *stream;
244 char *sign;
245 int use_local;
246 ULONGEST val_ulong;
247{
248 unsigned long temp[3];
249 int i = 0;
250 do
251 {
252 temp[i] = val_ulong % (1000 * 1000 * 1000);
253 val_ulong /= (1000 * 1000 * 1000);
254 i++;
255 }
256 while (val_ulong != 0 && i < (sizeof (temp) / sizeof (temp[0])));
257 switch (i)
258 {
259 case 1:
260 fprintf_filtered (stream, "%s%lu",
261 sign, temp[0]);
262 break;
263 case 2:
264 fprintf_filtered (stream, "%s%lu%09lu",
265 sign, temp[1], temp[0]);
266 break;
267 case 3:
268 fprintf_filtered (stream, "%s%lu%09lu%09lu",
269 sign, temp[2], temp[1], temp[0]);
270 break;
271 default:
272 abort ();
273 }
274 return;
275}
276#endif
277
278void
279print_longest (stream, format, use_local, val_long)
280 GDB_FILE *stream;
281 int format;
282 int use_local;
283 LONGEST val_long;
284{
285#if defined (CC_HAS_LONG_LONG) && !defined (PRINTF_HAS_LONG_LONG)
286 if (sizeof (long) < sizeof (LONGEST))
287 {
288 switch (format)
289 {
290 case 'd':
291 {
292 /* Print a signed value, that doesn't fit in a long */
293 if ((long) val_long != val_long)
294 {
295 if (val_long < 0)
296 print_decimal (stream, "-", use_local, -val_long);
297 else
298 print_decimal (stream, "", use_local, val_long);
299 return;
300 }
301 break;
302 }
303 case 'u':
304 {
305 /* Print an unsigned value, that doesn't fit in a long */
306 if ((unsigned long) val_long != (ULONGEST) val_long)
307 {
308 print_decimal (stream, "", use_local, val_long);
309 return;
310 }
311 break;
312 }
313 case 'x':
314 case 'o':
315 case 'b':
316 case 'h':
317 case 'w':
318 case 'g':
319 /* Print as unsigned value, must fit completely in unsigned long */
320 {
321 unsigned long temp = val_long;
322 if (temp != val_long)
323 {
324 /* Urk, can't represent value in long so print in hex.
325 Do shift in two operations so that if sizeof (long)
326 == sizeof (LONGEST) we can avoid warnings from
327 picky compilers about shifts >= the size of the
328 shiftee in bits */
329 unsigned long vbot = (unsigned long) val_long;
330 LONGEST temp = (val_long >> (sizeof (long) * HOST_CHAR_BIT - 1));
331 unsigned long vtop = temp >> 1;
332 fprintf_filtered (stream, "0x%lx%08lx", vtop, vbot);
333 return;
334 }
335 break;
336 }
337 }
338 }
339#endif
340
341#if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
342 switch (format)
343 {
344 case 'd':
345 fprintf_filtered (stream,
346 use_local ? local_decimal_format_custom ("ll")
c5aa993b 347 : "%lld",
c906108c
SS
348 val_long);
349 break;
350 case 'u':
351 fprintf_filtered (stream, "%llu", val_long);
352 break;
353 case 'x':
354 fprintf_filtered (stream,
355 use_local ? local_hex_format_custom ("ll")
c5aa993b 356 : "%llx",
c906108c
SS
357 val_long);
358 break;
359 case 'o':
360 fprintf_filtered (stream,
361 use_local ? local_octal_format_custom ("ll")
c5aa993b 362 : "%llo",
c906108c
SS
363 val_long);
364 break;
365 case 'b':
366 fprintf_filtered (stream, local_hex_format_custom ("02ll"), val_long);
367 break;
368 case 'h':
369 fprintf_filtered (stream, local_hex_format_custom ("04ll"), val_long);
370 break;
371 case 'w':
372 fprintf_filtered (stream, local_hex_format_custom ("08ll"), val_long);
373 break;
374 case 'g':
375 fprintf_filtered (stream, local_hex_format_custom ("016ll"), val_long);
376 break;
377 default:
378 abort ();
379 }
c5aa993b 380#else /* !CC_HAS_LONG_LONG || !PRINTF_HAS_LONG_LONG */
c906108c
SS
381 /* In the following it is important to coerce (val_long) to a long. It does
382 nothing if !LONG_LONG, but it will chop off the top half (which we know
383 we can ignore) if the host supports long longs. */
384
385 switch (format)
386 {
387 case 'd':
388 fprintf_filtered (stream,
389 use_local ? local_decimal_format_custom ("l")
c5aa993b 390 : "%ld",
c906108c
SS
391 (long) val_long);
392 break;
393 case 'u':
394 fprintf_filtered (stream, "%lu", (unsigned long) val_long);
395 break;
396 case 'x':
397 fprintf_filtered (stream,
398 use_local ? local_hex_format_custom ("l")
c5aa993b 399 : "%lx",
c906108c
SS
400 (unsigned long) val_long);
401 break;
402 case 'o':
403 fprintf_filtered (stream,
404 use_local ? local_octal_format_custom ("l")
c5aa993b 405 : "%lo",
c906108c
SS
406 (unsigned long) val_long);
407 break;
408 case 'b':
409 fprintf_filtered (stream, local_hex_format_custom ("02l"),
410 (unsigned long) val_long);
411 break;
412 case 'h':
413 fprintf_filtered (stream, local_hex_format_custom ("04l"),
414 (unsigned long) val_long);
415 break;
416 case 'w':
417 fprintf_filtered (stream, local_hex_format_custom ("08l"),
418 (unsigned long) val_long);
419 break;
420 case 'g':
421 fprintf_filtered (stream, local_hex_format_custom ("016l"),
422 (unsigned long) val_long);
423 break;
424 default:
425 abort ();
426 }
427#endif /* CC_HAS_LONG_LONG || PRINTF_HAS_LONG_LONG */
428}
429
7a292a7a 430#if 0
c906108c
SS
431void
432strcat_longest (format, use_local, val_long, buf, buflen)
433 int format;
434 int use_local;
435 LONGEST val_long;
436 char *buf;
437 int buflen; /* ignored, for now */
438{
439#if defined (CC_HAS_LONG_LONG) && !defined (PRINTF_HAS_LONG_LONG)
440 long vtop, vbot;
441
442 vtop = val_long >> (sizeof (long) * HOST_CHAR_BIT);
443 vbot = (long) val_long;
444
445 if ((format == 'd' && (val_long < INT_MIN || val_long > INT_MAX))
c5aa993b 446 || ((format == 'u' || format == 'x') && (unsigned long long) val_long > UINT_MAX))
c906108c
SS
447 {
448 sprintf (buf, "0x%lx%08lx", vtop, vbot);
449 return;
450 }
451#endif
452
453#ifdef PRINTF_HAS_LONG_LONG
454 switch (format)
455 {
456 case 'd':
457 sprintf (buf,
c5aa993b 458 (use_local ? local_decimal_format_custom ("ll") : "%lld"),
c906108c
SS
459 val_long);
460 break;
461 case 'u':
c5aa993b 462 sprintf (buf, "%llu", val_long);
c906108c
SS
463 break;
464 case 'x':
465 sprintf (buf,
c5aa993b
JM
466 (use_local ? local_hex_format_custom ("ll") : "%llx"),
467
c906108c
SS
468 val_long);
469 break;
470 case 'o':
471 sprintf (buf,
c5aa993b 472 (use_local ? local_octal_format_custom ("ll") : "%llo"),
c906108c
SS
473 val_long);
474 break;
475 case 'b':
c5aa993b 476 sprintf (buf, local_hex_format_custom ("02ll"), val_long);
c906108c
SS
477 break;
478 case 'h':
c5aa993b 479 sprintf (buf, local_hex_format_custom ("04ll"), val_long);
c906108c
SS
480 break;
481 case 'w':
c5aa993b 482 sprintf (buf, local_hex_format_custom ("08ll"), val_long);
c906108c
SS
483 break;
484 case 'g':
c5aa993b 485 sprintf (buf, local_hex_format_custom ("016ll"), val_long);
c906108c
SS
486 break;
487 default:
488 abort ();
489 }
490#else /* !PRINTF_HAS_LONG_LONG */
491 /* In the following it is important to coerce (val_long) to a long. It does
492 nothing if !LONG_LONG, but it will chop off the top half (which we know
493 we can ignore) if the host supports long longs. */
494
495 switch (format)
496 {
497 case 'd':
c5aa993b
JM
498 sprintf (buf, (use_local ? local_decimal_format_custom ("l") : "%ld"),
499 ((long) val_long));
c906108c
SS
500 break;
501 case 'u':
c5aa993b 502 sprintf (buf, "%lu", ((unsigned long) val_long));
c906108c
SS
503 break;
504 case 'x':
c5aa993b 505 sprintf (buf, (use_local ? local_hex_format_custom ("l") : "%lx"),
c906108c
SS
506 ((long) val_long));
507 break;
508 case 'o':
c5aa993b 509 sprintf (buf, (use_local ? local_octal_format_custom ("l") : "%lo"),
c906108c
SS
510 ((long) val_long));
511 break;
512 case 'b':
c5aa993b 513 sprintf (buf, local_hex_format_custom ("02l"),
c906108c
SS
514 ((long) val_long));
515 break;
516 case 'h':
c5aa993b 517 sprintf (buf, local_hex_format_custom ("04l"),
c906108c
SS
518 ((long) val_long));
519 break;
520 case 'w':
c5aa993b 521 sprintf (buf, local_hex_format_custom ("08l"),
c906108c
SS
522 ((long) val_long));
523 break;
524 case 'g':
525 sprintf (buf, local_hex_format_custom ("016l"),
526 ((long) val_long));
527 break;
528 default:
529 abort ();
530 }
c5aa993b 531
c906108c
SS
532#endif /* !PRINTF_HAS_LONG_LONG */
533}
7a292a7a 534#endif
c906108c
SS
535
536/* This used to be a macro, but I don't think it is called often enough
537 to merit such treatment. */
538/* Convert a LONGEST to an int. This is used in contexts (e.g. number of
539 arguments to a function, number in a value history, register number, etc.)
540 where the value must not be larger than can fit in an int. */
541
542int
543longest_to_int (arg)
544 LONGEST arg;
545{
546 /* Let the compiler do the work */
547 int rtnval = (int) arg;
548
549 /* Check for overflows or underflows */
550 if (sizeof (LONGEST) > sizeof (int))
551 {
552 if (rtnval != arg)
553 {
554 error ("Value out of range.");
555 }
556 }
557 return (rtnval);
558}
559
560/* Print a floating point value of type TYPE, pointed to in GDB by VALADDR,
561 on STREAM. */
562
563void
564print_floating (valaddr, type, stream)
565 char *valaddr;
566 struct type *type;
567 GDB_FILE *stream;
568{
569 DOUBLEST doub;
570 int inv;
571 unsigned len = TYPE_LENGTH (type);
c5aa993b 572
c906108c
SS
573#if defined (IEEE_FLOAT)
574
575 /* Check for NaN's. Note that this code does not depend on us being
576 on an IEEE conforming system. It only depends on the target
577 machine using IEEE representation. This means (a)
578 cross-debugging works right, and (2) IEEE_FLOAT can (and should)
579 be defined for systems like the 68881, which uses IEEE
580 representation, but is not IEEE conforming. */
581
582 {
583 unsigned long low, high;
584 /* Is the sign bit 0? */
585 int nonnegative;
586 /* Is it is a NaN (i.e. the exponent is all ones and
587 the fraction is nonzero)? */
588 int is_nan;
589
590 /* For lint, initialize these two variables to suppress warning: */
591 low = high = nonnegative = 0;
592 if (len == 4)
593 {
594 /* It's single precision. */
595 /* Assume that floating point byte order is the same as
596 integer byte order. */
597 low = extract_unsigned_integer (valaddr, 4);
598 nonnegative = ((low & 0x80000000) == 0);
c5aa993b 599 is_nan = ((((low >> 23) & 0xFF) == 0xFF)
c906108c
SS
600 && 0 != (low & 0x7FFFFF));
601 low &= 0x7fffff;
602 high = 0;
603 }
604 else if (len == 8)
605 {
606 /* It's double precision. Get the high and low words. */
607
608 /* Assume that floating point byte order is the same as
609 integer byte order. */
610 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
611 {
612 low = extract_unsigned_integer (valaddr + 4, 4);
613 high = extract_unsigned_integer (valaddr, 4);
614 }
615 else
616 {
617 low = extract_unsigned_integer (valaddr, 4);
618 high = extract_unsigned_integer (valaddr + 4, 4);
619 }
620 nonnegative = ((high & 0x80000000) == 0);
621 is_nan = (((high >> 20) & 0x7ff) == 0x7ff
c5aa993b 622 && !((((high & 0xfffff) == 0)) && (low == 0)));
c906108c
SS
623 high &= 0xfffff;
624 }
625 else
d4f3574e
SS
626 {
627#ifdef TARGET_ANALYZE_FLOATING
628 TARGET_ANALYZE_FLOATING;
629#else
630 /* Extended. We can't detect extended NaNs for this target.
631 Also note that currently extendeds get nuked to double in
632 REGISTER_CONVERTIBLE. */
633 is_nan = 0;
634#endif
635 }
c906108c
SS
636
637 if (is_nan)
638 {
639 /* The meaning of the sign and fraction is not defined by IEEE.
640 But the user might know what they mean. For example, they
641 (in an implementation-defined manner) distinguish between
642 signaling and quiet NaN's. */
643 if (high)
d4f3574e 644 fprintf_filtered (stream, "-NaN(0x%lx%.8lx)" + !!nonnegative,
c906108c
SS
645 high, low);
646 else
647 fprintf_filtered (stream, "-NaN(0x%lx)" + nonnegative, low);
648 return;
649 }
650 }
651#endif /* IEEE_FLOAT. */
652
653 doub = unpack_double (type, valaddr, &inv);
654 if (inv)
655 {
656 fprintf_filtered (stream, "<invalid float value>");
657 return;
658 }
659
660 if (len < sizeof (double))
c5aa993b 661 fprintf_filtered (stream, "%.9g", (double) doub);
c906108c 662 else if (len == sizeof (double))
c5aa993b 663 fprintf_filtered (stream, "%.17g", (double) doub);
c906108c
SS
664 else
665#ifdef PRINTF_HAS_LONG_DOUBLE
666 fprintf_filtered (stream, "%.35Lg", doub);
667#else
668 /* This at least wins with values that are representable as doubles */
669 fprintf_filtered (stream, "%.17g", (double) doub);
670#endif
671}
672
c5aa993b 673void
c906108c
SS
674print_binary_chars (stream, valaddr, len)
675 GDB_FILE *stream;
676 unsigned char *valaddr;
677 unsigned len;
678{
679
680#define BITS_IN_BYTES 8
681
682 unsigned char *p;
c5aa993b
JM
683 int i;
684 int b;
c906108c
SS
685
686 /* Declared "int" so it will be signed.
687 * This ensures that right shift will shift in zeros.
688 */
c5aa993b 689 const int mask = 0x080;
c906108c
SS
690
691 /* FIXME: We should be not printing leading zeroes in most cases. */
692
693 fprintf_filtered (stream, local_binary_format_prefix ());
694 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
695 {
696 for (p = valaddr;
697 p < valaddr + len;
698 p++)
699 {
c5aa993b
JM
700 /* Every byte has 8 binary characters; peel off
701 * and print from the MSB end.
702 */
703 for (i = 0; i < (BITS_IN_BYTES * sizeof (*p)); i++)
704 {
705 if (*p & (mask >> i))
706 b = 1;
707 else
708 b = 0;
709
710 fprintf_filtered (stream, "%1d", b);
711 }
c906108c
SS
712 }
713 }
714 else
715 {
716 for (p = valaddr + len - 1;
717 p >= valaddr;
718 p--)
719 {
c5aa993b
JM
720 for (i = 0; i < (BITS_IN_BYTES * sizeof (*p)); i++)
721 {
722 if (*p & (mask >> i))
723 b = 1;
724 else
725 b = 0;
726
727 fprintf_filtered (stream, "%1d", b);
728 }
c906108c
SS
729 }
730 }
731 fprintf_filtered (stream, local_binary_format_suffix ());
732}
733
734/* VALADDR points to an integer of LEN bytes.
735 * Print it in octal on stream or format it in buf.
736 */
737void
738print_octal_chars (stream, valaddr, len)
739 GDB_FILE *stream;
740 unsigned char *valaddr;
741 unsigned len;
742{
743 unsigned char *p;
744 unsigned char octa1, octa2, octa3, carry;
c5aa993b
JM
745 int cycle;
746
c906108c
SS
747 /* FIXME: We should be not printing leading zeroes in most cases. */
748
749
750 /* Octal is 3 bits, which doesn't fit. Yuk. So we have to track
751 * the extra bits, which cycle every three bytes:
752 *
753 * Byte side: 0 1 2 3
754 * | | | |
755 * bit number 123 456 78 | 9 012 345 6 | 78 901 234 | 567 890 12 |
756 *
757 * Octal side: 0 1 carry 3 4 carry ...
758 *
759 * Cycle number: 0 1 2
760 *
761 * But of course we are printing from the high side, so we have to
762 * figure out where in the cycle we are so that we end up with no
763 * left over bits at the end.
764 */
765#define BITS_IN_OCTAL 3
766#define HIGH_ZERO 0340
767#define LOW_ZERO 0016
768#define CARRY_ZERO 0003
769#define HIGH_ONE 0200
770#define MID_ONE 0160
771#define LOW_ONE 0016
772#define CARRY_ONE 0001
773#define HIGH_TWO 0300
774#define MID_TWO 0070
775#define LOW_TWO 0007
776
777 /* For 32 we start in cycle 2, with two bits and one bit carry;
778 * for 64 in cycle in cycle 1, with one bit and a two bit carry.
779 */
780 cycle = (len * BITS_IN_BYTES) % BITS_IN_OCTAL;
781 carry = 0;
c5aa993b 782
c906108c
SS
783 fprintf_filtered (stream, local_octal_format_prefix ());
784 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
785 {
786 for (p = valaddr;
787 p < valaddr + len;
788 p++)
789 {
c5aa993b
JM
790 switch (cycle)
791 {
792 case 0:
793 /* No carry in, carry out two bits.
794 */
795 octa1 = (HIGH_ZERO & *p) >> 5;
796 octa2 = (LOW_ZERO & *p) >> 2;
797 carry = (CARRY_ZERO & *p);
798 fprintf_filtered (stream, "%o", octa1);
799 fprintf_filtered (stream, "%o", octa2);
800 break;
801
802 case 1:
803 /* Carry in two bits, carry out one bit.
804 */
805 octa1 = (carry << 1) | ((HIGH_ONE & *p) >> 7);
806 octa2 = (MID_ONE & *p) >> 4;
807 octa3 = (LOW_ONE & *p) >> 1;
808 carry = (CARRY_ONE & *p);
809 fprintf_filtered (stream, "%o", octa1);
810 fprintf_filtered (stream, "%o", octa2);
811 fprintf_filtered (stream, "%o", octa3);
812 break;
813
814 case 2:
815 /* Carry in one bit, no carry out.
816 */
817 octa1 = (carry << 2) | ((HIGH_TWO & *p) >> 6);
818 octa2 = (MID_TWO & *p) >> 3;
819 octa3 = (LOW_TWO & *p);
820 carry = 0;
821 fprintf_filtered (stream, "%o", octa1);
822 fprintf_filtered (stream, "%o", octa2);
823 fprintf_filtered (stream, "%o", octa3);
824 break;
825
826 default:
827 error ("Internal error in octal conversion;");
828 }
829
830 cycle++;
831 cycle = cycle % BITS_IN_OCTAL;
c906108c
SS
832 }
833 }
834 else
835 {
836 for (p = valaddr + len - 1;
837 p >= valaddr;
838 p--)
839 {
c5aa993b
JM
840 switch (cycle)
841 {
842 case 0:
843 /* Carry out, no carry in */
844 octa1 = (HIGH_ZERO & *p) >> 5;
845 octa2 = (LOW_ZERO & *p) >> 2;
846 carry = (CARRY_ZERO & *p);
847 fprintf_filtered (stream, "%o", octa1);
848 fprintf_filtered (stream, "%o", octa2);
849 break;
850
851 case 1:
852 /* Carry in, carry out */
853 octa1 = (carry << 1) | ((HIGH_ONE & *p) >> 7);
854 octa2 = (MID_ONE & *p) >> 4;
855 octa3 = (LOW_ONE & *p) >> 1;
856 carry = (CARRY_ONE & *p);
857 fprintf_filtered (stream, "%o", octa1);
858 fprintf_filtered (stream, "%o", octa2);
859 fprintf_filtered (stream, "%o", octa3);
860 break;
861
862 case 2:
863 /* Carry in, no carry out */
864 octa1 = (carry << 2) | ((HIGH_TWO & *p) >> 6);
865 octa2 = (MID_TWO & *p) >> 3;
866 octa3 = (LOW_TWO & *p);
867 carry = 0;
868 fprintf_filtered (stream, "%o", octa1);
869 fprintf_filtered (stream, "%o", octa2);
870 fprintf_filtered (stream, "%o", octa3);
871 break;
872
873 default:
874 error ("Internal error in octal conversion;");
875 }
876
877 cycle++;
878 cycle = cycle % BITS_IN_OCTAL;
c906108c
SS
879 }
880 }
881
882 fprintf_filtered (stream, local_octal_format_suffix ());
883}
884
885/* VALADDR points to an integer of LEN bytes.
886 * Print it in decimal on stream or format it in buf.
887 */
888void
889print_decimal_chars (stream, valaddr, len)
890 GDB_FILE *stream;
891 unsigned char *valaddr;
892 unsigned len;
893{
894#define TEN 10
895#define TWO_TO_FOURTH 16
c5aa993b 896#define CARRY_OUT( x ) ((x) / TEN) /* extend char to int */
c906108c
SS
897#define CARRY_LEFT( x ) ((x) % TEN)
898#define SHIFT( x ) ((x) << 4)
899#define START_P \
900 ((TARGET_BYTE_ORDER == BIG_ENDIAN) ? valaddr : valaddr + len - 1)
901#define NOT_END_P \
902 ((TARGET_BYTE_ORDER == BIG_ENDIAN) ? (p < valaddr + len) : (p >= valaddr))
903#define NEXT_P \
904 ((TARGET_BYTE_ORDER == BIG_ENDIAN) ? p++ : p-- )
905#define LOW_NIBBLE( x ) ( (x) & 0x00F)
906#define HIGH_NIBBLE( x ) (((x) & 0x0F0) >> 4)
907
908 unsigned char *p;
909 unsigned char *digits;
c5aa993b
JM
910 int carry;
911 int decimal_len;
912 int i, j, decimal_digits;
913 int dummy;
914 int flip;
915
c906108c
SS
916 /* Base-ten number is less than twice as many digits
917 * as the base 16 number, which is 2 digits per byte.
918 */
919 decimal_len = len * 2 * 2;
c5aa993b
JM
920 digits = (unsigned char *) malloc (decimal_len);
921 if (digits == NULL)
922 error ("Can't allocate memory for conversion to decimal.");
c906108c 923
c5aa993b
JM
924 for (i = 0; i < decimal_len; i++)
925 {
c906108c 926 digits[i] = 0;
c5aa993b 927 }
c906108c
SS
928
929 fprintf_filtered (stream, local_decimal_format_prefix ());
930
931 /* Ok, we have an unknown number of bytes of data to be printed in
932 * decimal.
933 *
934 * Given a hex number (in nibbles) as XYZ, we start by taking X and
935 * decemalizing it as "x1 x2" in two decimal nibbles. Then we multiply
936 * the nibbles by 16, add Y and re-decimalize. Repeat with Z.
937 *
938 * The trick is that "digits" holds a base-10 number, but sometimes
939 * the individual digits are > 10.
940 *
941 * Outer loop is per nibble (hex digit) of input, from MSD end to
942 * LSD end.
943 */
c5aa993b 944 decimal_digits = 0; /* Number of decimal digits so far */
c906108c
SS
945 p = START_P;
946 flip = 0;
c5aa993b
JM
947 while (NOT_END_P)
948 {
c906108c
SS
949 /*
950 * Multiply current base-ten number by 16 in place.
951 * Each digit was between 0 and 9, now is between
952 * 0 and 144.
953 */
c5aa993b
JM
954 for (j = 0; j < decimal_digits; j++)
955 {
956 digits[j] = SHIFT (digits[j]);
957 }
958
c906108c
SS
959 /* Take the next nibble off the input and add it to what
960 * we've got in the LSB position. Bottom 'digit' is now
961 * between 0 and 159.
962 *
963 * "flip" is used to run this loop twice for each byte.
964 */
c5aa993b
JM
965 if (flip == 0)
966 {
967 /* Take top nibble.
968 */
969 digits[0] += HIGH_NIBBLE (*p);
970 flip = 1;
971 }
972 else
973 {
974 /* Take low nibble and bump our pointer "p".
975 */
976 digits[0] += LOW_NIBBLE (*p);
977 NEXT_P;
978 flip = 0;
979 }
c906108c
SS
980
981 /* Re-decimalize. We have to do this often enough
982 * that we don't overflow, but once per nibble is
983 * overkill. Easier this way, though. Note that the
984 * carry is often larger than 10 (e.g. max initial
985 * carry out of lowest nibble is 15, could bubble all
986 * the way up greater than 10). So we have to do
987 * the carrying beyond the last current digit.
988 */
989 carry = 0;
c5aa993b
JM
990 for (j = 0; j < decimal_len - 1; j++)
991 {
992 digits[j] += carry;
993
994 /* "/" won't handle an unsigned char with
995 * a value that if signed would be negative.
996 * So extend to longword int via "dummy".
997 */
998 dummy = digits[j];
999 carry = CARRY_OUT (dummy);
1000 digits[j] = CARRY_LEFT (dummy);
1001
1002 if (j >= decimal_digits && carry == 0)
1003 {
1004 /*
1005 * All higher digits are 0 and we
1006 * no longer have a carry.
1007 *
1008 * Note: "j" is 0-based, "decimal_digits" is
1009 * 1-based.
1010 */
1011 decimal_digits = j + 1;
1012 break;
1013 }
1014 }
1015 }
c906108c
SS
1016
1017 /* Ok, now "digits" is the decimal representation, with
1018 * the "decimal_digits" actual digits. Print!
1019 */
c5aa993b
JM
1020 for (i = decimal_digits - 1; i >= 0; i--)
1021 {
1022 fprintf_filtered (stream, "%1d", digits[i]);
1023 }
1024 free (digits);
1025
c906108c
SS
1026 fprintf_filtered (stream, local_decimal_format_suffix ());
1027}
1028
1029/* VALADDR points to an integer of LEN bytes. Print it in hex on stream. */
1030
1031static void
1032print_hex_chars (stream, valaddr, len)
1033 GDB_FILE *stream;
1034 unsigned char *valaddr;
1035 unsigned len;
1036{
1037 unsigned char *p;
1038
1039 /* FIXME: We should be not printing leading zeroes in most cases. */
1040
1041 fprintf_filtered (stream, local_hex_format_prefix ());
1042 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
1043 {
1044 for (p = valaddr;
1045 p < valaddr + len;
1046 p++)
1047 {
1048 fprintf_filtered (stream, "%02x", *p);
1049 }
1050 }
1051 else
1052 {
1053 for (p = valaddr + len - 1;
1054 p >= valaddr;
1055 p--)
1056 {
1057 fprintf_filtered (stream, "%02x", *p);
1058 }
1059 }
1060 fprintf_filtered (stream, local_hex_format_suffix ());
1061}
1062
1063/* Called by various <lang>_val_print routines to print elements of an
c5aa993b 1064 array in the form "<elem1>, <elem2>, <elem3>, ...".
c906108c 1065
c5aa993b
JM
1066 (FIXME?) Assumes array element separator is a comma, which is correct
1067 for all languages currently handled.
1068 (FIXME?) Some languages have a notation for repeated array elements,
1069 perhaps we should try to use that notation when appropriate.
1070 */
c906108c
SS
1071
1072void
1073val_print_array_elements (type, valaddr, address, stream, format, deref_ref,
1074 recurse, pretty, i)
1075 struct type *type;
1076 char *valaddr;
1077 CORE_ADDR address;
1078 GDB_FILE *stream;
1079 int format;
1080 int deref_ref;
1081 int recurse;
1082 enum val_prettyprint pretty;
1083 unsigned int i;
1084{
1085 unsigned int things_printed = 0;
1086 unsigned len;
1087 struct type *elttype;
1088 unsigned eltlen;
1089 /* Position of the array element we are examining to see
1090 whether it is repeated. */
1091 unsigned int rep1;
1092 /* Number of repetitions we have detected so far. */
1093 unsigned int reps;
c5aa993b 1094
c906108c
SS
1095 elttype = TYPE_TARGET_TYPE (type);
1096 eltlen = TYPE_LENGTH (check_typedef (elttype));
1097 len = TYPE_LENGTH (type) / eltlen;
1098
1099 annotate_array_section_begin (i, elttype);
1100
1101 for (; i < len && things_printed < print_max; i++)
1102 {
1103 if (i != 0)
1104 {
1105 if (prettyprint_arrays)
1106 {
1107 fprintf_filtered (stream, ",\n");
1108 print_spaces_filtered (2 + 2 * recurse, stream);
1109 }
1110 else
1111 {
1112 fprintf_filtered (stream, ", ");
1113 }
1114 }
1115 wrap_here (n_spaces (2 + 2 * recurse));
1116
1117 rep1 = i + 1;
1118 reps = 1;
c5aa993b 1119 while ((rep1 < len) &&
c906108c
SS
1120 !memcmp (valaddr + i * eltlen, valaddr + rep1 * eltlen, eltlen))
1121 {
1122 ++reps;
1123 ++rep1;
1124 }
1125
1126 if (reps > repeat_count_threshold)
1127 {
1128 val_print (elttype, valaddr + i * eltlen, 0, 0, stream, format,
1129 deref_ref, recurse + 1, pretty);
1130 annotate_elt_rep (reps);
1131 fprintf_filtered (stream, " <repeats %u times>", reps);
1132 annotate_elt_rep_end ();
1133
1134 i = rep1 - 1;
1135 things_printed += repeat_count_threshold;
1136 }
1137 else
1138 {
1139 val_print (elttype, valaddr + i * eltlen, 0, 0, stream, format,
1140 deref_ref, recurse + 1, pretty);
1141 annotate_elt ();
1142 things_printed++;
1143 }
1144 }
1145 annotate_array_section_end ();
1146 if (i < len)
1147 {
1148 fprintf_filtered (stream, "...");
1149 }
1150}
1151
1152/* Print a string from the inferior, starting at ADDR and printing up to LEN
c5aa993b
JM
1153 characters, of WIDTH bytes a piece, to STREAM. If LEN is -1, printing
1154 stops at the first null byte, otherwise printing proceeds (including null
1155 bytes) until either print_max or LEN characters have been printed,
1156 whichever is smaller. */
c906108c
SS
1157
1158/* FIXME: Use target_read_string. */
1159
1160int
1161val_print_string (addr, len, width, stream)
c5aa993b
JM
1162 CORE_ADDR addr;
1163 int len;
1164 int width;
1165 GDB_FILE *stream;
c906108c
SS
1166{
1167 int force_ellipsis = 0; /* Force ellipsis to be printed if nonzero. */
1168 int errcode; /* Errno returned from bad reads. */
1169 unsigned int fetchlimit; /* Maximum number of chars to print. */
1170 unsigned int nfetch; /* Chars to fetch / chars fetched. */
1171 unsigned int chunksize; /* Size of each fetch, in chars. */
1172 char *buffer = NULL; /* Dynamically growable fetch buffer. */
1173 char *bufptr; /* Pointer to next available byte in buffer. */
1174 char *limit; /* First location past end of fetch buffer. */
c5aa993b 1175 struct cleanup *old_chain = NULL; /* Top of the old cleanup chain. */
c906108c
SS
1176 int found_nul; /* Non-zero if we found the nul char */
1177
1178 /* First we need to figure out the limit on the number of characters we are
1179 going to attempt to fetch and print. This is actually pretty simple. If
1180 LEN >= zero, then the limit is the minimum of LEN and print_max. If
1181 LEN is -1, then the limit is print_max. This is true regardless of
1182 whether print_max is zero, UINT_MAX (unlimited), or something in between,
1183 because finding the null byte (or available memory) is what actually
1184 limits the fetch. */
1185
1186 fetchlimit = (len == -1 ? print_max : min (len, print_max));
1187
1188 /* Now decide how large of chunks to try to read in one operation. This
1189 is also pretty simple. If LEN >= zero, then we want fetchlimit chars,
1190 so we might as well read them all in one operation. If LEN is -1, we
1191 are looking for a null terminator to end the fetching, so we might as
1192 well read in blocks that are large enough to be efficient, but not so
1193 large as to be slow if fetchlimit happens to be large. So we choose the
1194 minimum of 8 and fetchlimit. We used to use 200 instead of 8 but
1195 200 is way too big for remote debugging over a serial line. */
1196
1197 chunksize = (len == -1 ? min (8, fetchlimit) : fetchlimit);
1198
1199 /* Loop until we either have all the characters to print, or we encounter
1200 some error, such as bumping into the end of the address space. */
1201
1202 found_nul = 0;
1203 old_chain = make_cleanup (null_cleanup, 0);
1204
1205 if (len > 0)
1206 {
1207 buffer = (char *) xmalloc (len * width);
1208 bufptr = buffer;
1209 old_chain = make_cleanup (free, buffer);
1210
1211 nfetch = target_read_memory_partial (addr, bufptr, len * width, &errcode)
1212 / width;
1213 addr += nfetch * width;
1214 bufptr += nfetch * width;
1215 }
1216 else if (len == -1)
1217 {
1218 unsigned long bufsize = 0;
1219 do
1220 {
1221 QUIT;
1222 nfetch = min (chunksize, fetchlimit - bufsize);
1223
1224 if (buffer == NULL)
1225 buffer = (char *) xmalloc (nfetch * width);
1226 else
1227 {
1228 discard_cleanups (old_chain);
1229 buffer = (char *) xrealloc (buffer, (nfetch + bufsize) * width);
1230 }
1231
1232 old_chain = make_cleanup (free, buffer);
1233 bufptr = buffer + bufsize * width;
1234 bufsize += nfetch;
1235
1236 /* Read as much as we can. */
1237 nfetch = target_read_memory_partial (addr, bufptr, nfetch * width, &errcode)
c5aa993b 1238 / width;
c906108c
SS
1239
1240 /* Scan this chunk for the null byte that terminates the string
1241 to print. If found, we don't need to fetch any more. Note
1242 that bufptr is explicitly left pointing at the next character
1243 after the null byte, or at the next character after the end of
1244 the buffer. */
1245
1246 limit = bufptr + nfetch * width;
1247 while (bufptr < limit)
1248 {
1249 unsigned long c;
1250
1251 c = extract_unsigned_integer (bufptr, width);
1252 addr += width;
1253 bufptr += width;
1254 if (c == 0)
1255 {
1256 /* We don't care about any error which happened after
1257 the NULL terminator. */
1258 errcode = 0;
1259 found_nul = 1;
1260 break;
1261 }
1262 }
1263 }
c5aa993b
JM
1264 while (errcode == 0 /* no error */
1265 && bufptr - buffer < fetchlimit * width /* no overrun */
1266 && !found_nul); /* haven't found nul yet */
c906108c
SS
1267 }
1268 else
1269 { /* length of string is really 0! */
1270 buffer = bufptr = NULL;
1271 errcode = 0;
1272 }
1273
1274 /* bufptr and addr now point immediately beyond the last byte which we
1275 consider part of the string (including a '\0' which ends the string). */
1276
1277 /* We now have either successfully filled the buffer to fetchlimit, or
1278 terminated early due to an error or finding a null char when LEN is -1. */
1279
1280 if (len == -1 && !found_nul)
1281 {
1282 char *peekbuf;
1283
1284 /* We didn't find a null terminator we were looking for. Attempt
c5aa993b
JM
1285 to peek at the next character. If not successful, or it is not
1286 a null byte, then force ellipsis to be printed. */
c906108c
SS
1287
1288 peekbuf = (char *) alloca (width);
1289
1290 if (target_read_memory (addr, peekbuf, width) == 0
1291 && extract_unsigned_integer (peekbuf, width) != 0)
1292 force_ellipsis = 1;
1293 }
c5aa993b 1294 else if ((len >= 0 && errcode != 0) || (len > (bufptr - buffer) / width))
c906108c
SS
1295 {
1296 /* Getting an error when we have a requested length, or fetching less
c5aa993b
JM
1297 than the number of characters actually requested, always make us
1298 print ellipsis. */
c906108c
SS
1299 force_ellipsis = 1;
1300 }
1301
1302 QUIT;
1303
1304 /* If we get an error before fetching anything, don't print a string.
1305 But if we fetch something and then get an error, print the string
1306 and then the error message. */
1307 if (errcode == 0 || bufptr > buffer)
1308 {
1309 if (addressprint)
1310 {
1311 fputs_filtered (" ", stream);
1312 }
c5aa993b 1313 LA_PRINT_STRING (stream, buffer, (bufptr - buffer) / width, width, force_ellipsis);
c906108c
SS
1314 }
1315
1316 if (errcode != 0)
1317 {
1318 if (errcode == EIO)
1319 {
1320 fprintf_filtered (stream, " <Address ");
1321 print_address_numeric (addr, 1, stream);
1322 fprintf_filtered (stream, " out of bounds>");
1323 }
1324 else
1325 {
1326 fprintf_filtered (stream, " <Error reading address ");
1327 print_address_numeric (addr, 1, stream);
1328 fprintf_filtered (stream, ": %s>", safe_strerror (errcode));
1329 }
1330 }
1331 gdb_flush (stream);
1332 do_cleanups (old_chain);
c5aa993b 1333 return ((bufptr - buffer) / width);
c906108c 1334}
c906108c 1335\f
c5aa993b 1336
c906108c
SS
1337/* Validate an input or output radix setting, and make sure the user
1338 knows what they really did here. Radix setting is confusing, e.g.
1339 setting the input radix to "10" never changes it! */
1340
1341/* ARGSUSED */
1342static void
1343set_input_radix (args, from_tty, c)
1344 char *args;
1345 int from_tty;
1346 struct cmd_list_element *c;
1347{
c5aa993b 1348 set_input_radix_1 (from_tty, *(unsigned *) c->var);
c906108c
SS
1349}
1350
1351/* ARGSUSED */
1352static void
1353set_input_radix_1 (from_tty, radix)
1354 int from_tty;
1355 unsigned radix;
1356{
1357 /* We don't currently disallow any input radix except 0 or 1, which don't
1358 make any mathematical sense. In theory, we can deal with any input
1359 radix greater than 1, even if we don't have unique digits for every
1360 value from 0 to radix-1, but in practice we lose on large radix values.
1361 We should either fix the lossage or restrict the radix range more.
1362 (FIXME). */
1363
1364 if (radix < 2)
1365 {
1366 error ("Nonsense input radix ``decimal %u''; input radix unchanged.",
1367 radix);
1368 }
1369 input_radix = radix;
1370 if (from_tty)
1371 {
1372 printf_filtered ("Input radix now set to decimal %u, hex %x, octal %o.\n",
1373 radix, radix, radix);
1374 }
1375}
1376
1377/* ARGSUSED */
1378static void
1379set_output_radix (args, from_tty, c)
1380 char *args;
1381 int from_tty;
1382 struct cmd_list_element *c;
1383{
c5aa993b 1384 set_output_radix_1 (from_tty, *(unsigned *) c->var);
c906108c
SS
1385}
1386
1387static void
1388set_output_radix_1 (from_tty, radix)
1389 int from_tty;
1390 unsigned radix;
1391{
1392 /* Validate the radix and disallow ones that we aren't prepared to
1393 handle correctly, leaving the radix unchanged. */
1394 switch (radix)
1395 {
1396 case 16:
c5aa993b 1397 output_format = 'x'; /* hex */
c906108c
SS
1398 break;
1399 case 10:
c5aa993b 1400 output_format = 0; /* decimal */
c906108c
SS
1401 break;
1402 case 8:
c5aa993b 1403 output_format = 'o'; /* octal */
c906108c
SS
1404 break;
1405 default:
1406 error ("Unsupported output radix ``decimal %u''; output radix unchanged.",
1407 radix);
1408 }
1409 output_radix = radix;
1410 if (from_tty)
1411 {
1412 printf_filtered ("Output radix now set to decimal %u, hex %x, octal %o.\n",
1413 radix, radix, radix);
1414 }
1415}
1416
1417/* Set both the input and output radix at once. Try to set the output radix
1418 first, since it has the most restrictive range. An radix that is valid as
1419 an output radix is also valid as an input radix.
1420
1421 It may be useful to have an unusual input radix. If the user wishes to
1422 set an input radix that is not valid as an output radix, he needs to use
1423 the 'set input-radix' command. */
1424
1425static void
1426set_radix (arg, from_tty)
1427 char *arg;
1428 int from_tty;
1429{
1430 unsigned radix;
1431
1432 radix = (arg == NULL) ? 10 : parse_and_eval_address (arg);
1433 set_output_radix_1 (0, radix);
1434 set_input_radix_1 (0, radix);
1435 if (from_tty)
1436 {
1437 printf_filtered ("Input and output radices now set to decimal %u, hex %x, octal %o.\n",
1438 radix, radix, radix);
1439 }
1440}
1441
1442/* Show both the input and output radices. */
1443
c5aa993b 1444/*ARGSUSED */
c906108c
SS
1445static void
1446show_radix (arg, from_tty)
1447 char *arg;
1448 int from_tty;
1449{
1450 if (from_tty)
1451 {
1452 if (input_radix == output_radix)
1453 {
1454 printf_filtered ("Input and output radices set to decimal %u, hex %x, octal %o.\n",
1455 input_radix, input_radix, input_radix);
1456 }
1457 else
1458 {
1459 printf_filtered ("Input radix set to decimal %u, hex %x, octal %o.\n",
1460 input_radix, input_radix, input_radix);
1461 printf_filtered ("Output radix set to decimal %u, hex %x, octal %o.\n",
1462 output_radix, output_radix, output_radix);
1463 }
1464 }
1465}
c906108c 1466\f
c5aa993b
JM
1467
1468/*ARGSUSED */
c906108c
SS
1469static void
1470set_print (arg, from_tty)
1471 char *arg;
1472 int from_tty;
1473{
1474 printf_unfiltered (
c5aa993b 1475 "\"set print\" must be followed by the name of a print subcommand.\n");
c906108c
SS
1476 help_list (setprintlist, "set print ", -1, gdb_stdout);
1477}
1478
c5aa993b 1479/*ARGSUSED */
c906108c
SS
1480static void
1481show_print (args, from_tty)
1482 char *args;
1483 int from_tty;
1484{
1485 cmd_show_list (showprintlist, from_tty, "");
1486}
1487\f
1488void
1489_initialize_valprint ()
1490{
1491 struct cmd_list_element *c;
1492
1493 add_prefix_cmd ("print", no_class, set_print,
1494 "Generic command for setting how things print.",
1495 &setprintlist, "set print ", 0, &setlist);
c5aa993b
JM
1496 add_alias_cmd ("p", "print", no_class, 1, &setlist);
1497 /* prefer set print to set prompt */
c906108c
SS
1498 add_alias_cmd ("pr", "print", no_class, 1, &setlist);
1499
1500 add_prefix_cmd ("print", no_class, show_print,
1501 "Generic command for showing print settings.",
1502 &showprintlist, "show print ", 0, &showlist);
c5aa993b
JM
1503 add_alias_cmd ("p", "print", no_class, 1, &showlist);
1504 add_alias_cmd ("pr", "print", no_class, 1, &showlist);
c906108c
SS
1505
1506 add_show_from_set
c5aa993b 1507 (add_set_cmd ("elements", no_class, var_uinteger, (char *) &print_max,
c906108c
SS
1508 "Set limit on string chars or array elements to print.\n\
1509\"set print elements 0\" causes there to be no limit.",
1510 &setprintlist),
1511 &showprintlist);
1512
1513 add_show_from_set
1514 (add_set_cmd ("null-stop", no_class, var_boolean,
c5aa993b 1515 (char *) &stop_print_at_null,
c906108c
SS
1516 "Set printing of char arrays to stop at first null char.",
1517 &setprintlist),
1518 &showprintlist);
1519
1520 add_show_from_set
1521 (add_set_cmd ("repeats", no_class, var_uinteger,
c5aa993b 1522 (char *) &repeat_count_threshold,
c906108c
SS
1523 "Set threshold for repeated print elements.\n\
1524\"set print repeats 0\" causes all elements to be individually printed.",
1525 &setprintlist),
1526 &showprintlist);
1527
1528 add_show_from_set
1529 (add_set_cmd ("pretty", class_support, var_boolean,
c5aa993b 1530 (char *) &prettyprint_structs,
c906108c
SS
1531 "Set prettyprinting of structures.",
1532 &setprintlist),
1533 &showprintlist);
1534
1535 add_show_from_set
c5aa993b 1536 (add_set_cmd ("union", class_support, var_boolean, (char *) &unionprint,
c906108c
SS
1537 "Set printing of unions interior to structures.",
1538 &setprintlist),
1539 &showprintlist);
c5aa993b 1540
c906108c
SS
1541 add_show_from_set
1542 (add_set_cmd ("array", class_support, var_boolean,
c5aa993b 1543 (char *) &prettyprint_arrays,
c906108c
SS
1544 "Set prettyprinting of arrays.",
1545 &setprintlist),
1546 &showprintlist);
1547
1548 add_show_from_set
c5aa993b 1549 (add_set_cmd ("address", class_support, var_boolean, (char *) &addressprint,
c906108c
SS
1550 "Set printing of addresses.",
1551 &setprintlist),
1552 &showprintlist);
1553
1554 c = add_set_cmd ("input-radix", class_support, var_uinteger,
c5aa993b
JM
1555 (char *) &input_radix,
1556 "Set default input radix for entering numbers.",
1557 &setlist);
c906108c
SS
1558 add_show_from_set (c, &showlist);
1559 c->function.sfunc = set_input_radix;
1560
1561 c = add_set_cmd ("output-radix", class_support, var_uinteger,
c5aa993b
JM
1562 (char *) &output_radix,
1563 "Set default output radix for printing of values.",
1564 &setlist);
c906108c
SS
1565 add_show_from_set (c, &showlist);
1566 c->function.sfunc = set_output_radix;
1567
1568 /* The "set radix" and "show radix" commands are special in that they are
1569 like normal set and show commands but allow two normally independent
1570 variables to be either set or shown with a single command. So the
1571 usual add_set_cmd() and add_show_from_set() commands aren't really
1572 appropriate. */
1573 add_cmd ("radix", class_support, set_radix,
1574 "Set default input and output number radices.\n\
1575Use 'set input-radix' or 'set output-radix' to independently set each.\n\
1576Without an argument, sets both radices back to the default value of 10.",
1577 &setlist);
1578 add_cmd ("radix", class_support, show_radix,
1579 "Show the default input and output number radices.\n\
1580Use 'show input-radix' or 'show output-radix' to independently show each.",
1581 &showlist);
1582
1583 /* Give people the defaults which they are used to. */
1584 prettyprint_structs = 0;
1585 prettyprint_arrays = 0;
1586 unionprint = 1;
1587 addressprint = 1;
1588 print_max = PRINT_MAX_DEFAULT;
1589}