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