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