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