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