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