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