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