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