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