gcc -Wall lint:
[deliverable/binutils-gdb.git] / gdb / valprint.c
CommitLineData
7d9884b9
JG
1/* Print values for GDB, the GNU debugger.
2 Copyright 1986, 1988, 1989, 1991 Free Software Foundation, Inc.
bd5635a1
RP
3
4This file is part of GDB.
5
36b9d39c 6This program is free software; you can redistribute it and/or modify
bd5635a1 7it under the terms of the GNU General Public License as published by
36b9d39c
JG
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
bd5635a1 10
36b9d39c 11This program is distributed in the hope that it will be useful,
bd5635a1
RP
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
36b9d39c
JG
17along with this program; if not, write to the Free Software
18Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
bd5635a1 19
bd5635a1 20#include "defs.h"
2cd99985 21#include <string.h>
bd5635a1 22#include "symtab.h"
2cd99985 23#include "gdbtypes.h"
bd5635a1
RP
24#include "value.h"
25#include "gdbcore.h"
26#include "gdbcmd.h"
27#include "target.h"
28#include "obstack.h"
be3bc7ad 29#include "language.h"
8f793aa5 30#include "demangle.h"
1c95d7ab 31#include "annotate.h"
bd5635a1
RP
32
33#include <errno.h>
2cd99985
PB
34
35/* Prototypes for local functions */
36
a8a69e63 37static void
199b2450 38print_hex_chars PARAMS ((GDB_FILE *, unsigned char *, unsigned int));
a8a69e63 39
2cd99985
PB
40static void
41show_print PARAMS ((char *, int));
42
43static void
44set_print PARAMS ((char *, int));
45
46static void
ce13daa7
FF
47set_radix PARAMS ((char *, int));
48
49static void
50show_radix PARAMS ((char *, int));
51
52static void
53set_input_radix PARAMS ((char *, int, struct cmd_list_element *));
54
55static void
56set_input_radix_1 PARAMS ((int, unsigned));
2cd99985
PB
57
58static void
59set_output_radix PARAMS ((char *, int, struct cmd_list_element *));
60
ce13daa7
FF
61static void
62set_output_radix_1 PARAMS ((int, unsigned));
63
82a2edfb
JK
64static void value_print_array_elements PARAMS ((value_ptr, GDB_FILE *, int,
65 enum val_prettyprint));
bd5635a1 66
ce13daa7
FF
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". */
bd5635a1 71
85f0a848 72unsigned int print_max;
ce13daa7 73#define PRINT_MAX_DEFAULT 200 /* Start print_max off at this value. */
bd5635a1 74
bd5635a1
RP
75/* Default input and output radixes, and output format letter. */
76
77unsigned input_radix = 10;
78unsigned output_radix = 10;
79int output_format = 0;
80
85f0a848
FF
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
85unsigned int repeat_count_threshold = 10;
0dce3774 86
a8a69e63
FF
87int prettyprint_structs; /* Controls pretty printing of structures */
88int prettyprint_arrays; /* Controls pretty printing of arrays. */
0dce3774 89
a8a69e63
FF
90/* If nonzero, causes unions inside structures or other unions to be
91 printed. */
bd5635a1 92
a8a69e63 93int unionprint; /* Controls printing of nested unions. */
bd5635a1 94
a8a69e63 95/* If nonzero, causes machine addresses to be printed in certain contexts. */
bd5635a1 96
a8a69e63 97int addressprint; /* Controls printing of machine addresses */
bd5635a1 98
a8a69e63 99\f
c7da3ed3
FF
100/* Print data of type TYPE located at VALADDR (within GDB), which came from
101 the inferior at address ADDRESS, onto stdio stream STREAM according to
102 FORMAT (a letter, or 0 for natural format using TYPE).
bd5635a1 103
c7da3ed3
FF
104 If DEREF_REF is nonzero, then dereference references, otherwise just print
105 them like pointers.
bd5635a1 106
c7da3ed3
FF
107 The PRETTY parameter controls prettyprinting.
108
109 If the data are a string pointer, returns the number of string characters
110 printed.
111
112 FIXME: The data at VALADDR is in target byte order. If gdb is ever
113 enhanced to be able to debug more than the single target it was compiled
114 for (specific CPU type and thus specific target byte ordering), then
115 either the print routines are going to have to take this into account,
116 or the data is going to have to be passed into here already converted
117 to the host byte ordering, whichever is more convenient. */
bd5635a1 118
bd5635a1 119
a8a69e63 120int
c7da3ed3 121val_print (type, valaddr, address, stream, format, deref_ref, recurse, pretty)
a8a69e63
FF
122 struct type *type;
123 char *valaddr;
124 CORE_ADDR address;
199b2450 125 GDB_FILE *stream;
a8a69e63
FF
126 int format;
127 int deref_ref;
128 int recurse;
129 enum val_prettyprint pretty;
bd5635a1 130{
a8a69e63
FF
131 if (pretty == Val_pretty_default)
132 {
133 pretty = prettyprint_structs ? Val_prettyprint : Val_no_prettyprint;
134 }
bd5635a1 135
a8a69e63
FF
136 QUIT;
137
138 /* Ensure that the type is complete and not just a stub. If the type is
139 only a stub and we can't find and substitute its complete type, then
5ce7426f 140 print appropriate string and return. */
a8a69e63
FF
141
142 check_stub_type (type);
143 if (TYPE_FLAGS (type) & TYPE_FLAG_STUB)
bd5635a1 144 {
a8a69e63 145 fprintf_filtered (stream, "<incomplete type>");
199b2450 146 gdb_flush (stream);
a8a69e63 147 return (0);
bd5635a1 148 }
a8a69e63
FF
149
150 return (LA_VAL_PRINT (type, valaddr, address, stream, format, deref_ref,
151 recurse, pretty));
bd5635a1 152}
a8a69e63 153
bd5635a1
RP
154/* Print the value VAL in C-ish syntax on stream STREAM.
155 FORMAT is a format-letter, or 0 for print in natural format of data type.
156 If the object printed is a string pointer, returns
157 the number of string bytes printed. */
158
159int
160value_print (val, stream, format, pretty)
82a2edfb 161 value_ptr val;
199b2450 162 GDB_FILE *stream;
2cd99985 163 int format;
bd5635a1
RP
164 enum val_prettyprint pretty;
165{
a8a69e63 166 register unsigned int n, typelen;
bd5635a1
RP
167
168 if (val == 0)
169 {
170 printf_filtered ("<address of value unknown>");
171 return 0;
172 }
173 if (VALUE_OPTIMIZED_OUT (val))
174 {
175 printf_filtered ("<value optimized out>");
176 return 0;
177 }
aec4cb91 178
bd5635a1
RP
179 /* A "repeated" value really contains several values in a row.
180 They are made by the @ operator.
181 Print such values as if they were arrays. */
182
a8a69e63 183 if (VALUE_REPEATED (val))
bd5635a1
RP
184 {
185 n = VALUE_REPETITIONS (val);
186 typelen = TYPE_LENGTH (VALUE_TYPE (val));
187 fprintf_filtered (stream, "{");
188 /* Print arrays of characters using string syntax. */
189 if (typelen == 1 && TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_INT
190 && format == 0)
a8a69e63 191 LA_PRINT_STRING (stream, VALUE_CONTENTS (val), n, 0);
bd5635a1
RP
192 else
193 {
a8a69e63 194 value_print_array_elements (val, stream, format, pretty);
bd5635a1
RP
195 }
196 fprintf_filtered (stream, "}");
a8a69e63 197 return (n * typelen);
bd5635a1
RP
198 }
199 else
200 {
0dce3774
JK
201 struct type *type = VALUE_TYPE (val);
202
bd5635a1
RP
203 /* If it is a pointer, indicate what it points to.
204
205 Print type also if it is a reference.
206
207 C++: if it is a member pointer, we will take care
208 of that when we print it. */
a8a69e63
FF
209 if (TYPE_CODE (type) == TYPE_CODE_PTR ||
210 TYPE_CODE (type) == TYPE_CODE_REF)
bd5635a1
RP
211 {
212 /* Hack: remove (char *) for char strings. Their
213 type is indicated by the quoted string anyway. */
a8a69e63
FF
214 if (TYPE_CODE (type) == TYPE_CODE_PTR &&
215 TYPE_LENGTH (TYPE_TARGET_TYPE (type)) == sizeof(char) &&
216 TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_INT &&
217 !TYPE_UNSIGNED (TYPE_TARGET_TYPE (type)))
bd5635a1
RP
218 {
219 /* Print nothing */
220 }
a8a69e63
FF
221 else
222 {
223 fprintf_filtered (stream, "(");
224 type_print (type, "", stream, -1);
225 fprintf_filtered (stream, ") ");
226 }
227 }
228 return (val_print (type, VALUE_CONTENTS (val),
229 VALUE_ADDRESS (val), stream, format, 1, 0, pretty));
bd5635a1
RP
230 }
231}
232
a8a69e63
FF
233/* Called by various <lang>_val_print routines to print TYPE_CODE_INT's */
234
235void
236val_print_type_code_int (type, valaddr, stream)
9e4667f6 237 struct type *type;
a8a69e63 238 char *valaddr;
199b2450 239 GDB_FILE *stream;
9e4667f6 240{
a8a69e63
FF
241 char *p;
242 /* Pointer to first (i.e. lowest address) nonzero character. */
243 char *first_addr;
244 unsigned int len;
9e4667f6 245
a8a69e63 246 if (TYPE_LENGTH (type) > sizeof (LONGEST))
9e4667f6 247 {
a8a69e63 248 if (TYPE_UNSIGNED (type))
9e4667f6 249 {
a8a69e63
FF
250 /* First figure out whether the number in fact has zeros
251 in all its bytes more significant than least significant
252 sizeof (LONGEST) ones. */
253 len = TYPE_LENGTH (type);
254
255#if TARGET_BYTE_ORDER == BIG_ENDIAN
256 for (p = valaddr;
257 len > sizeof (LONGEST) && p < valaddr + TYPE_LENGTH (type);
258 p++)
259#else /* Little endian. */
260 first_addr = valaddr;
199b2450 261 for (p = valaddr + TYPE_LENGTH (type) - 1;
a8a69e63
FF
262 len > sizeof (LONGEST) && p >= valaddr;
263 p--)
264#endif /* Little endian. */
9e4667f6 265 {
a8a69e63 266 if (*p == 0)
9e4667f6 267 {
a8a69e63 268 len--;
9e4667f6 269 }
a8a69e63 270 else
9e4667f6 271 {
a8a69e63 272 break;
9e4667f6
FF
273 }
274 }
a8a69e63
FF
275#if TARGET_BYTE_ORDER == BIG_ENDIAN
276 first_addr = p;
277#endif
278 if (len <= sizeof (LONGEST))
279 {
fb0f4231
JK
280 /* The most significant bytes are zero, so we can just get
281 the least significant sizeof (LONGEST) bytes and print it
282 in decimal. */
7efb57c3 283 print_longest (stream, 'u', 0,
fb0f4231
JK
284 extract_unsigned_integer (first_addr,
285 sizeof (LONGEST)));
a8a69e63
FF
286 }
287 else
288 {
289 /* It is big, so print it in hex. */
290 print_hex_chars (stream, (unsigned char *) first_addr, len);
291 }
292 }
293 else
294 {
295 /* Signed. One could assume two's complement (a reasonable
296 assumption, I think) and do better than this. */
297 print_hex_chars (stream, (unsigned char *) valaddr,
298 TYPE_LENGTH (type));
9e4667f6
FF
299 }
300 }
a8a69e63
FF
301 else
302 {
303#ifdef PRINT_TYPELESS_INTEGER
304 PRINT_TYPELESS_INTEGER (stream, type, unpack_long (type, valaddr));
305#else
7efb57c3
FF
306 print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0,
307 unpack_long (type, valaddr));
a8a69e63
FF
308#endif
309 }
b0f61d04 310}
9e4667f6 311
7efb57c3
FF
312/* Print a number according to FORMAT which is one of d,u,x,o,b,h,w,g.
313 The raison d'etre of this function is to consolidate printing of LONG_LONG's
314 into this one function. Some platforms have long longs but don't have a
315 printf() that supports "ll" in the format string. We handle these by seeing
316 if the number is actually a long, and if not we just bail out and print the
317 number in hex. The format chars b,h,w,g are from
1c95d7ab
JK
318 print_scalar_formatted(). If USE_LOCAL, format it according to the current
319 language (this should be used for most integers which GDB prints, the
320 exception is things like protocols where the format of the integer is
321 a protocol thing, not a user-visible thing). */
7efb57c3
FF
322
323void
324print_longest (stream, format, use_local, val_long)
199b2450 325 GDB_FILE *stream;
ce13daa7 326 int format;
7efb57c3
FF
327 int use_local;
328 LONGEST val_long;
329{
330#if defined (CC_HAS_LONG_LONG) && !defined (PRINTF_HAS_LONG_LONG)
331 long vtop, vbot;
332
333 vtop = val_long >> (sizeof (long) * HOST_CHAR_BIT);
334 vbot = (long) val_long;
335
336 if ((format == 'd' && (val_long < INT_MIN || val_long > INT_MAX))
c8ff77be 337 || ((format == 'u' || format == 'x') && (unsigned long long)val_long > UINT_MAX))
7efb57c3 338 {
199b2450 339 fprintf_filtered (stream, "0x%lx%08lx", vtop, vbot);
7efb57c3
FF
340 return;
341 }
342#endif
343
344#ifdef PRINTF_HAS_LONG_LONG
345 switch (format)
346 {
347 case 'd':
348 fprintf_filtered (stream,
349 use_local ? local_decimal_format_custom ("ll")
350 : "%lld",
351 val_long);
352 break;
353 case 'u':
354 fprintf_filtered (stream, "%llu", val_long);
355 break;
356 case 'x':
357 fprintf_filtered (stream,
358 use_local ? local_hex_format_custom ("ll")
359 : "%llx",
360 val_long);
361 break;
362 case 'o':
363 fprintf_filtered (stream,
364 use_local ? local_octal_format_custom ("ll")
365 : "%llo",
366 break;
367 case 'b':
368 fprintf_filtered (stream, local_hex_format_custom ("02ll"), val_long);
369 break;
370 case 'h':
371 fprintf_filtered (stream, local_hex_format_custom ("04ll"), val_long);
372 break;
373 case 'w':
374 fprintf_filtered (stream, local_hex_format_custom ("08ll"), val_long);
375 break;
376 case 'g':
377 fprintf_filtered (stream, local_hex_format_custom ("016ll"), val_long);
378 break;
379 default:
380 abort ();
381 }
382#else /* !PRINTF_HAS_LONG_LONG */
383 /* In the following it is important to coerce (val_long) to a long. It does
384 nothing if !LONG_LONG, but it will chop off the top half (which we know
385 we can ignore) if the host supports long longs. */
386
387 switch (format)
388 {
389 case 'd':
390 fprintf_filtered (stream,
391 use_local ? local_decimal_format_custom ("l")
392 : "%ld",
393 (long) val_long);
394 break;
395 case 'u':
396 fprintf_filtered (stream, "%lu", (unsigned long) val_long);
397 break;
398 case 'x':
399 fprintf_filtered (stream,
400 use_local ? local_hex_format_custom ("l")
401 : "%lx",
402 (long) val_long);
403 break;
404 case 'o':
405 fprintf_filtered (stream,
406 use_local ? local_octal_format_custom ("l")
407 : "%lo",
408 (long) val_long);
409 break;
410 case 'b':
411 fprintf_filtered (stream, local_hex_format_custom ("02l"),
412 (long) val_long);
413 break;
414 case 'h':
415 fprintf_filtered (stream, local_hex_format_custom ("04l"),
416 (long) val_long);
417 break;
418 case 'w':
419 fprintf_filtered (stream, local_hex_format_custom ("08l"),
420 (long) val_long);
421 break;
422 case 'g':
423 fprintf_filtered (stream, local_hex_format_custom ("016l"),
424 (long) val_long);
425 break;
426 default:
427 abort ();
428 }
429#endif /* !PRINTF_HAS_LONG_LONG */
430}
431
fb0f4231
JK
432/* This used to be a macro, but I don't think it is called often enough
433 to merit such treatment. */
434/* Convert a LONGEST to an int. This is used in contexts (e.g. number of
435 arguments to a function, number in a value history, register number, etc.)
436 where the value must not be larger than can fit in an int. */
437
438int
439longest_to_int (arg)
440 LONGEST arg;
441{
442
443 /* This check is in case a system header has botched the
444 definition of INT_MIN, like on BSDI. */
445 if (sizeof (LONGEST) <= sizeof (int))
446 return arg;
447
448 if (arg > INT_MAX || arg < INT_MIN)
449 error ("Value out of range.");
450
451 return arg;
452}
453
a8a69e63
FF
454/* Print a floating point value of type TYPE, pointed to in GDB by VALADDR,
455 on STREAM. */
bd5635a1 456
a8a69e63
FF
457void
458print_floating (valaddr, type, stream)
459 char *valaddr;
bd5635a1 460 struct type *type;
199b2450 461 GDB_FILE *stream;
bd5635a1 462{
a8a69e63
FF
463 double doub;
464 int inv;
465 unsigned len = TYPE_LENGTH (type);
466
467#if defined (IEEE_FLOAT)
bd5635a1 468
a8a69e63
FF
469 /* Check for NaN's. Note that this code does not depend on us being
470 on an IEEE conforming system. It only depends on the target
471 machine using IEEE representation. This means (a)
472 cross-debugging works right, and (2) IEEE_FLOAT can (and should)
473 be defined for systems like the 68881, which uses IEEE
474 representation, but is not IEEE conforming. */
bd5635a1 475
a8a69e63 476 {
199b2450 477 unsigned long low, high;
a8a69e63
FF
478 /* Is the sign bit 0? */
479 int nonnegative;
480 /* Is it is a NaN (i.e. the exponent is all ones and
481 the fraction is nonzero)? */
482 int is_nan;
bd5635a1 483
199b2450 484 if (len == 4)
a8a69e63 485 {
199b2450
TL
486 /* It's single precision. */
487 /* Assume that floating point byte order is the same as
488 integer byte order. */
489 low = extract_unsigned_integer (valaddr, 4);
833e0d94 490 nonnegative = ((low & 0x80000000) == 0);
a8a69e63
FF
491 is_nan = ((((low >> 23) & 0xFF) == 0xFF)
492 && 0 != (low & 0x7FFFFF));
493 low &= 0x7fffff;
494 high = 0;
495 }
199b2450 496 else if (len == 8)
a8a69e63
FF
497 {
498 /* It's double precision. Get the high and low words. */
bd5635a1 499
199b2450
TL
500 /* Assume that floating point byte order is the same as
501 integer byte order. */
a8a69e63 502#if TARGET_BYTE_ORDER == BIG_ENDIAN
199b2450
TL
503 low = extract_unsigned_integer (valaddr + 4, 4);
504 high = extract_unsigned_integer (valaddr, 4);
a8a69e63 505#else
199b2450
TL
506 low = extract_unsigned_integer (valaddr, 4);
507 high = extract_unsigned_integer (valaddr + 4, 4);
a8a69e63 508#endif
833e0d94 509 nonnegative = ((high & 0x80000000) == 0);
a8a69e63
FF
510 is_nan = (((high >> 20) & 0x7ff) == 0x7ff
511 && ! ((((high & 0xfffff) == 0)) && (low == 0)));
512 high &= 0xfffff;
513 }
199b2450
TL
514 else
515 /* Extended. We can't detect NaNs for extendeds yet. Also note
516 that currently extendeds get nuked to double in
517 REGISTER_CONVERTIBLE. */
518 is_nan = 0;
bd5635a1 519
a8a69e63
FF
520 if (is_nan)
521 {
522 /* The meaning of the sign and fraction is not defined by IEEE.
523 But the user might know what they mean. For example, they
524 (in an implementation-defined manner) distinguish between
525 signaling and quiet NaN's. */
526 if (high)
527 fprintf_filtered (stream, "-NaN(0x%lx%.8lx)" + nonnegative,
528 high, low);
529 else
530 fprintf_filtered (stream, "-NaN(0x%lx)" + nonnegative, low);
531 return;
532 }
533 }
534#endif /* IEEE_FLOAT. */
bd5635a1 535
a8a69e63
FF
536 doub = unpack_double (type, valaddr, &inv);
537 if (inv)
538 fprintf_filtered (stream, "<invalid float value>");
539 else
540 fprintf_filtered (stream, len <= sizeof(float) ? "%.9g" : "%.17g", doub);
bd5635a1
RP
541}
542
a8a69e63 543/* VALADDR points to an integer of LEN bytes. Print it in hex on stream. */
bd5635a1
RP
544
545static void
a8a69e63 546print_hex_chars (stream, valaddr, len)
199b2450 547 GDB_FILE *stream;
a8a69e63
FF
548 unsigned char *valaddr;
549 unsigned len;
bd5635a1 550{
a8a69e63 551 unsigned char *p;
b0f61d04
JK
552
553 /* FIXME: We should be not printing leading zeroes in most cases. */
554
555 fprintf_filtered (stream, local_hex_format_prefix ());
a8a69e63
FF
556#if TARGET_BYTE_ORDER == BIG_ENDIAN
557 for (p = valaddr;
558 p < valaddr + len;
559 p++)
560#else /* Little endian. */
561 for (p = valaddr + len - 1;
562 p >= valaddr;
563 p--)
564#endif
bd5635a1 565 {
a8a69e63 566 fprintf_filtered (stream, "%02x", *p);
bd5635a1 567 }
b0f61d04 568 fprintf_filtered (stream, local_hex_format_suffix ());
a8a69e63 569}
bd5635a1 570
a8a69e63
FF
571/* Called by various <lang>_val_print routines to print elements of an
572 array in the form "<elem1>, <elem2>, <elem3>, ...".
4a11eef2 573
a8a69e63
FF
574 (FIXME?) Assumes array element separator is a comma, which is correct
575 for all languages currently handled.
576 (FIXME?) Some languages have a notation for repeated array elements,
577 perhaps we should try to use that notation when appropriate.
578 */
bd5635a1 579
a8a69e63
FF
580void
581val_print_array_elements (type, valaddr, address, stream, format, deref_ref,
582 recurse, pretty, i)
583 struct type *type;
584 char *valaddr;
585 CORE_ADDR address;
199b2450 586 GDB_FILE *stream;
a8a69e63
FF
587 int format;
588 int deref_ref;
589 int recurse;
590 enum val_prettyprint pretty;
591 unsigned int i;
592{
593 unsigned int things_printed = 0;
594 unsigned len;
595 struct type *elttype;
596 unsigned eltlen;
597 /* Position of the array element we are examining to see
598 whether it is repeated. */
599 unsigned int rep1;
600 /* Number of repetitions we have detected so far. */
601 unsigned int reps;
602
603 elttype = TYPE_TARGET_TYPE (type);
604 eltlen = TYPE_LENGTH (elttype);
605 len = TYPE_LENGTH (type) / eltlen;
1c95d7ab
JK
606
607 annotate_array_section_begin (i, elttype);
608
a8a69e63 609 for (; i < len && things_printed < print_max; i++)
bd5635a1 610 {
a8a69e63 611 if (i != 0)
bd5635a1 612 {
a8a69e63 613 if (prettyprint_arrays)
bd5635a1 614 {
a8a69e63
FF
615 fprintf_filtered (stream, ",\n");
616 print_spaces_filtered (2 + 2 * recurse, stream);
bd5635a1 617 }
a8a69e63 618 else
bd5635a1 619 {
a8a69e63 620 fprintf_filtered (stream, ", ");
bd5635a1 621 }
bd5635a1 622 }
a8a69e63 623 wrap_here (n_spaces (2 + 2 * recurse));
1c95d7ab 624
a8a69e63
FF
625 rep1 = i + 1;
626 reps = 1;
627 while ((rep1 < len) &&
628 !memcmp (valaddr + i * eltlen, valaddr + rep1 * eltlen, eltlen))
629 {
630 ++reps;
631 ++rep1;
632 }
96f7edbd 633
a8a69e63 634 if (reps > repeat_count_threshold)
bd5635a1 635 {
a8a69e63
FF
636 val_print (elttype, valaddr + i * eltlen, 0, stream, format,
637 deref_ref, recurse + 1, pretty);
1c95d7ab 638 annotate_elt_rep (reps);
a8a69e63 639 fprintf_filtered (stream, " <repeats %u times>", reps);
1c95d7ab
JK
640 annotate_elt_rep_end ();
641
a8a69e63
FF
642 i = rep1 - 1;
643 things_printed += repeat_count_threshold;
bd5635a1 644 }
bd5635a1
RP
645 else
646 {
a8a69e63
FF
647 val_print (elttype, valaddr + i * eltlen, 0, stream, format,
648 deref_ref, recurse + 1, pretty);
1c95d7ab 649 annotate_elt ();
a8a69e63 650 things_printed++;
bd5635a1 651 }
a8a69e63 652 }
1c95d7ab 653 annotate_array_section_end ();
a8a69e63
FF
654 if (i < len)
655 {
656 fprintf_filtered (stream, "...");
657 }
658}
e2aab031 659
a8a69e63
FF
660static void
661value_print_array_elements (val, stream, format, pretty)
82a2edfb 662 value_ptr val;
199b2450 663 GDB_FILE *stream;
a8a69e63
FF
664 int format;
665 enum val_prettyprint pretty;
666{
667 unsigned int things_printed = 0;
668 register unsigned int i, n, typelen;
669 /* Position of the array elem we are examining to see if it is repeated. */
670 unsigned int rep1;
671 /* Number of repetitions we have detected so far. */
672 unsigned int reps;
673
674 n = VALUE_REPETITIONS (val);
675 typelen = TYPE_LENGTH (VALUE_TYPE (val));
676 for (i = 0; i < n && things_printed < print_max; i++)
677 {
678 if (i != 0)
679 {
680 fprintf_filtered (stream, ", ");
681 }
682 wrap_here ("");
683
684 rep1 = i + 1;
685 reps = 1;
686 while (rep1 < n && !memcmp (VALUE_CONTENTS (val) + typelen * i,
687 VALUE_CONTENTS (val) + typelen * rep1,
688 typelen))
689 {
690 ++reps;
691 ++rep1;
692 }
693
694 if (reps > repeat_count_threshold)
4ace50a5 695 {
a8a69e63
FF
696 val_print (VALUE_TYPE (val), VALUE_CONTENTS (val) + typelen * i,
697 VALUE_ADDRESS (val) + typelen * i, stream, format, 1,
698 0, pretty);
199b2450 699 fprintf_unfiltered (stream, " <repeats %u times>", reps);
a8a69e63
FF
700 i = rep1 - 1;
701 things_printed += repeat_count_threshold;
4ace50a5
FF
702 }
703 else
704 {
a8a69e63
FF
705 val_print (VALUE_TYPE (val), VALUE_CONTENTS (val) + typelen * i,
706 VALUE_ADDRESS (val) + typelen * i, stream, format, 1,
707 0, pretty);
708 things_printed++;
4ace50a5 709 }
a8a69e63
FF
710 }
711 if (i < n)
712 {
713 fprintf_filtered (stream, "...");
bd5635a1
RP
714 }
715}
a8a69e63 716
7efb57c3
FF
717/* Print a string from the inferior, starting at ADDR and printing up to LEN
718 characters, to STREAM. If LEN is zero, printing stops at the first null
719 byte, otherwise printing proceeds (including null bytes) until either
ce13daa7 720 print_max or LEN characters have been printed, whichever is smaller. */
7efb57c3 721
4ad0021e
JK
722/* FIXME: All callers supply LEN of zero. Supplying a non-zero LEN is
723 pointless, this routine just then becomes a convoluted version of
724 target_read_memory_partial. Removing all the LEN stuff would simplify
725 this routine enormously.
726
727 FIXME: Use target_read_string. */
728
c7da3ed3 729int
7efb57c3 730val_print_string (addr, len, stream)
c7da3ed3 731 CORE_ADDR addr;
7efb57c3 732 unsigned int len;
199b2450 733 GDB_FILE *stream;
c7da3ed3 734{
ce13daa7
FF
735 int force_ellipsis = 0; /* Force ellipsis to be printed if nonzero. */
736 int errcode; /* Errno returned from bad reads. */
737 unsigned int fetchlimit; /* Maximum number of bytes to fetch. */
738 unsigned int nfetch; /* Bytes to fetch / bytes fetched. */
739 unsigned int chunksize; /* Size of each fetch, in bytes. */
740 int bufsize; /* Size of current fetch buffer. */
741 char *buffer = NULL; /* Dynamically growable fetch buffer. */
742 char *bufptr; /* Pointer to next available byte in buffer. */
743 char *limit; /* First location past end of fetch buffer. */
199b2450 744 struct cleanup *old_chain = NULL; /* Top of the old cleanup chain. */
ce13daa7
FF
745 char peekchar; /* Place into which we can read one char. */
746
747 /* First we need to figure out the limit on the number of characters we are
748 going to attempt to fetch and print. This is actually pretty simple. If
749 LEN is nonzero, then the limit is the minimum of LEN and print_max. If
750 LEN is zero, then the limit is print_max. This is true regardless of
751 whether print_max is zero, UINT_MAX (unlimited), or something in between,
752 because finding the null byte (or available memory) is what actually
753 limits the fetch. */
754
755 fetchlimit = (len == 0 ? print_max : min (len, print_max));
756
757 /* Now decide how large of chunks to try to read in one operation. This
758 is also pretty simple. If LEN is nonzero, then we want fetchlimit bytes,
759 so we might as well read them all in one operation. If LEN is zero, we
760 are looking for a null terminator to end the fetching, so we might as
761 well read in blocks that are large enough to be efficient, but not so
762 large as to be slow if fetchlimit happens to be large. So we choose the
833e0d94
JK
763 minimum of 8 and fetchlimit. We used to use 200 instead of 8 but
764 200 is way too big for remote debugging over a serial line. */
ce13daa7 765
833e0d94 766 chunksize = (len == 0 ? min (8, fetchlimit) : fetchlimit);
ce13daa7
FF
767
768 /* Loop until we either have all the characters to print, or we encounter
769 some error, such as bumping into the end of the address space. */
770
771 bufsize = 0;
772 do {
773 QUIT;
774 /* Figure out how much to fetch this time, and grow the buffer to fit. */
775 nfetch = min (chunksize, fetchlimit - bufsize);
776 bufsize += nfetch;
777 if (buffer == NULL)
778 {
779 buffer = (char *) xmalloc (bufsize);
780 bufptr = buffer;
781 }
782 else
783 {
784 discard_cleanups (old_chain);
785 buffer = (char *) xrealloc (buffer, bufsize);
786 bufptr = buffer + bufsize - nfetch;
787 }
788 old_chain = make_cleanup (free, buffer);
789
790 /* Read as much as we can. */
791 nfetch = target_read_memory_partial (addr, bufptr, nfetch, &errcode);
792 if (len != 0)
793 {
794 addr += nfetch;
795 bufptr += nfetch;
796 }
797 else
798 {
799 /* Scan this chunk for the null byte that terminates the string
800 to print. If found, we don't need to fetch any more. Note
801 that bufptr is explicitly left pointing at the next character
802 after the null byte, or at the next character after the end of
803 the buffer. */
804 limit = bufptr + nfetch;
c8ff77be
JK
805 while (bufptr < limit)
806 {
807 ++addr;
808 ++bufptr;
809 if (bufptr[-1] == '\0')
5ce7426f
JK
810 {
811 /* We don't care about any error which happened after
812 the NULL terminator. */
813 errcode = 0;
814 break;
815 }
c8ff77be 816 }
ce13daa7
FF
817 }
818 } while (errcode == 0 /* no error */
833e0d94 819 && bufsize < fetchlimit /* no overrun */
ce13daa7
FF
820 && !(len == 0 && *(bufptr - 1) == '\0')); /* no null term */
821
c8ff77be
JK
822 /* bufptr and addr now point immediately beyond the last byte which we
823 consider part of the string (including a '\0' which ends the string). */
824
ce13daa7
FF
825 /* We now have either successfully filled the buffer to fetchlimit, or
826 terminated early due to an error or finding a null byte when LEN is
c8ff77be 827 zero. */
ce13daa7 828
c8ff77be 829 if (len == 0 && bufptr > buffer && *(bufptr - 1) != '\0')
c7da3ed3 830 {
ce13daa7
FF
831 /* We didn't find a null terminator we were looking for. Attempt
832 to peek at the next character. If not successful, or it is not
c8ff77be 833 a null byte, then force ellipsis to be printed. */
ce13daa7 834 if (target_read_memory (addr, &peekchar, 1) != 0 || peekchar != '\0')
7efb57c3 835 {
7efb57c3
FF
836 force_ellipsis = 1;
837 }
c7da3ed3 838 }
ce13daa7
FF
839 else if ((len != 0 && errcode != 0) || (len > bufptr - buffer))
840 {
841 /* Getting an error when we have a requested length, or fetching less
842 than the number of characters actually requested, always make us
843 print ellipsis. */
844 force_ellipsis = 1;
845 }
846
847 QUIT;
c8ff77be
JK
848
849 /* If we get an error before fetching anything, don't print a string.
850 But if we fetch something and then get an error, print the string
851 and then the error message. */
852 if (errcode == 0 || bufptr > buffer)
ce13daa7 853 {
c8ff77be
JK
854 if (addressprint)
855 {
856 fputs_filtered (" ", stream);
857 }
858 LA_PRINT_STRING (stream, buffer, bufptr - buffer, force_ellipsis);
ce13daa7 859 }
c8ff77be
JK
860
861 if (errcode != 0)
c7da3ed3
FF
862 {
863 if (errcode == EIO)
864 {
833e0d94 865 fprintf_filtered (stream, " <Address ");
1c95d7ab 866 print_address_numeric (addr, 1, stream);
833e0d94 867 fprintf_filtered (stream, " out of bounds>");
c7da3ed3
FF
868 }
869 else
870 {
c8ff77be 871 fprintf_filtered (stream, " <Error reading address ");
1c95d7ab 872 print_address_numeric (addr, 1, stream);
c8ff77be 873 fprintf_filtered (stream, ": %s>", safe_strerror (errcode));
c7da3ed3
FF
874 }
875 }
199b2450 876 gdb_flush (stream);
ce13daa7
FF
877 do_cleanups (old_chain);
878 return (bufptr - buffer);
c7da3ed3 879}
ce13daa7 880
bd5635a1
RP
881\f
882/* Validate an input or output radix setting, and make sure the user
883 knows what they really did here. Radix setting is confusing, e.g.
884 setting the input radix to "10" never changes it! */
885
e1ce8aa5 886/* ARGSUSED */
bd5635a1
RP
887static void
888set_input_radix (args, from_tty, c)
889 char *args;
890 int from_tty;
891 struct cmd_list_element *c;
892{
ce13daa7
FF
893 set_input_radix_1 (from_tty, *(unsigned *)c->var);
894}
bd5635a1 895
ce13daa7
FF
896/* ARGSUSED */
897static void
898set_input_radix_1 (from_tty, radix)
899 int from_tty;
900 unsigned radix;
901{
902 /* We don't currently disallow any input radix except 0 or 1, which don't
903 make any mathematical sense. In theory, we can deal with any input
904 radix greater than 1, even if we don't have unique digits for every
905 value from 0 to radix-1, but in practice we lose on large radix values.
906 We should either fix the lossage or restrict the radix range more.
907 (FIXME). */
908
909 if (radix < 2)
910 {
911 error ("Nonsense input radix ``decimal %u''; input radix unchanged.",
912 radix);
913 }
914 input_radix = radix;
bd5635a1 915 if (from_tty)
ce13daa7
FF
916 {
917 printf_filtered ("Input radix now set to decimal %u, hex %x, octal %o.\n",
918 radix, radix, radix);
919 }
bd5635a1
RP
920}
921
e1ce8aa5 922/* ARGSUSED */
bd5635a1
RP
923static void
924set_output_radix (args, from_tty, c)
925 char *args;
926 int from_tty;
927 struct cmd_list_element *c;
928{
ce13daa7
FF
929 set_output_radix_1 (from_tty, *(unsigned *)c->var);
930}
bd5635a1 931
ce13daa7
FF
932static void
933set_output_radix_1 (from_tty, radix)
934 int from_tty;
935 unsigned radix;
936{
937 /* Validate the radix and disallow ones that we aren't prepared to
938 handle correctly, leaving the radix unchanged. */
bd5635a1
RP
939 switch (radix)
940 {
941 case 16:
ce13daa7 942 output_format = 'x'; /* hex */
bd5635a1
RP
943 break;
944 case 10:
ce13daa7 945 output_format = 0; /* decimal */
bd5635a1
RP
946 break;
947 case 8:
948 output_format = 'o'; /* octal */
949 break;
950 default:
ce13daa7
FF
951 error ("Unsupported output radix ``decimal %u''; output radix unchanged.",
952 radix);
953 }
954 output_radix = radix;
955 if (from_tty)
956 {
957 printf_filtered ("Output radix now set to decimal %u, hex %x, octal %o.\n",
958 radix, radix, radix);
bd5635a1
RP
959 }
960}
961
ce13daa7
FF
962/* Set both the input and output radix at once. Try to set the output radix
963 first, since it has the most restrictive range. An radix that is valid as
964 an output radix is also valid as an input radix.
965
966 It may be useful to have an unusual input radix. If the user wishes to
967 set an input radix that is not valid as an output radix, he needs to use
968 the 'set input-radix' command. */
969
bd5635a1 970static void
ce13daa7 971set_radix (arg, from_tty)
bd5635a1
RP
972 char *arg;
973 int from_tty;
bd5635a1 974{
ce13daa7 975 unsigned radix;
bd5635a1 976
ce13daa7
FF
977 radix = (arg == NULL) ? 10 : parse_and_eval_address (arg);
978 set_output_radix_1 (0, radix);
979 set_input_radix_1 (0, radix);
bd5635a1 980 if (from_tty)
ce13daa7
FF
981 {
982 printf_filtered ("Input and output radices now set to decimal %u, hex %x, octal %o.\n",
983 radix, radix, radix);
984 }
985}
bd5635a1 986
ce13daa7 987/* Show both the input and output radices. */
bd5635a1 988
ce13daa7
FF
989/*ARGSUSED*/
990static void
991show_radix (arg, from_tty)
992 char *arg;
993 int from_tty;
994{
995 if (from_tty)
996 {
997 if (input_radix == output_radix)
998 {
999 printf_filtered ("Input and output radices set to decimal %u, hex %x, octal %o.\n",
1000 input_radix, input_radix, input_radix);
1001 }
1002 else
1003 {
1004 printf_filtered ("Input radix set to decimal %u, hex %x, octal %o.\n",
1005 input_radix, input_radix, input_radix);
1006 printf_filtered ("Output radix set to decimal %u, hex %x, octal %o.\n",
1007 output_radix, output_radix, output_radix);
1008 }
1009 }
bd5635a1 1010}
ce13daa7 1011
bd5635a1 1012\f
f266e564
JK
1013/*ARGSUSED*/
1014static void
1015set_print (arg, from_tty)
1016 char *arg;
1017 int from_tty;
1018{
199b2450 1019 printf_unfiltered (
f266e564 1020"\"set print\" must be followed by the name of a print subcommand.\n");
199b2450 1021 help_list (setprintlist, "set print ", -1, gdb_stdout);
f266e564
JK
1022}
1023
1024/*ARGSUSED*/
1025static void
1026show_print (args, from_tty)
1027 char *args;
1028 int from_tty;
1029{
1030 cmd_show_list (showprintlist, from_tty, "");
1031}
1032\f
bd5635a1
RP
1033void
1034_initialize_valprint ()
1035{
1036 struct cmd_list_element *c;
1037
f266e564
JK
1038 add_prefix_cmd ("print", no_class, set_print,
1039 "Generic command for setting how things print.",
1040 &setprintlist, "set print ", 0, &setlist);
36b9d39c 1041 add_alias_cmd ("p", "print", no_class, 1, &setlist);
199b2450
TL
1042 /* prefer set print to set prompt */
1043 add_alias_cmd ("pr", "print", no_class, 1, &setlist);
1044
f266e564
JK
1045 add_prefix_cmd ("print", no_class, show_print,
1046 "Generic command for showing print settings.",
1047 &showprintlist, "show print ", 0, &showlist);
36b9d39c
JG
1048 add_alias_cmd ("p", "print", no_class, 1, &showlist);
1049 add_alias_cmd ("pr", "print", no_class, 1, &showlist);
f266e564 1050
bd5635a1 1051 add_show_from_set
f266e564 1052 (add_set_cmd ("elements", no_class, var_uinteger, (char *)&print_max,
bd5635a1 1053 "Set limit on string chars or array elements to print.\n\
f266e564
JK
1054\"set print elements 0\" causes there to be no limit.",
1055 &setprintlist),
1056 &showprintlist);
bd5635a1 1057
85f0a848
FF
1058 add_show_from_set
1059 (add_set_cmd ("repeats", no_class, var_uinteger,
1060 (char *)&repeat_count_threshold,
1061 "Set threshold for repeated print elements.\n\
1062\"set print repeats 0\" causes all elements to be individually printed.",
1063 &setprintlist),
1064 &showprintlist);
1065
bd5635a1 1066 add_show_from_set
a8a69e63
FF
1067 (add_set_cmd ("pretty", class_support, var_boolean,
1068 (char *)&prettyprint_structs,
bd5635a1 1069 "Set prettyprinting of structures.",
f266e564
JK
1070 &setprintlist),
1071 &showprintlist);
bd5635a1
RP
1072
1073 add_show_from_set
f266e564 1074 (add_set_cmd ("union", class_support, var_boolean, (char *)&unionprint,
bd5635a1 1075 "Set printing of unions interior to structures.",
f266e564
JK
1076 &setprintlist),
1077 &showprintlist);
bd5635a1
RP
1078
1079 add_show_from_set
a8a69e63
FF
1080 (add_set_cmd ("array", class_support, var_boolean,
1081 (char *)&prettyprint_arrays,
bd5635a1 1082 "Set prettyprinting of arrays.",
f266e564
JK
1083 &setprintlist),
1084 &showprintlist);
bd5635a1
RP
1085
1086 add_show_from_set
f266e564 1087 (add_set_cmd ("address", class_support, var_boolean, (char *)&addressprint,
bd5635a1 1088 "Set printing of addresses.",
f266e564
JK
1089 &setprintlist),
1090 &showprintlist);
bd5635a1 1091
bd5635a1
RP
1092 c = add_set_cmd ("input-radix", class_support, var_uinteger,
1093 (char *)&input_radix,
1094 "Set default input radix for entering numbers.",
1095 &setlist);
1096 add_show_from_set (c, &showlist);
199b2450 1097 c->function.sfunc = set_input_radix;
bd5635a1
RP
1098
1099 c = add_set_cmd ("output-radix", class_support, var_uinteger,
1100 (char *)&output_radix,
1101 "Set default output radix for printing of values.",
1102 &setlist);
1103 add_show_from_set (c, &showlist);
199b2450 1104 c->function.sfunc = set_output_radix;
bd5635a1 1105
ce13daa7
FF
1106 /* The "set radix" and "show radix" commands are special in that they are
1107 like normal set and show commands but allow two normally independent
1108 variables to be either set or shown with a single command. So the
1109 usual add_set_cmd() and add_show_from_set() commands aren't really
1110 appropriate. */
1111 add_cmd ("radix", class_support, set_radix,
1112 "Set default input and output number radices.\n\
1113Use 'set input-radix' or 'set output-radix' to independently set each.\n\
1114Without an argument, sets both radices back to the default value of 10.",
1115 &setlist);
1116 add_cmd ("radix", class_support, show_radix,
1117 "Show the default input and output number radices.\n\
1118Use 'show input-radix' or 'show output-radix' to independently show each.",
1119 &showlist);
bd5635a1
RP
1120
1121 /* Give people the defaults which they are used to. */
a8a69e63
FF
1122 prettyprint_structs = 0;
1123 prettyprint_arrays = 0;
bd5635a1 1124 unionprint = 1;
bd5635a1 1125 addressprint = 1;
ce13daa7 1126 print_max = PRINT_MAX_DEFAULT;
bd5635a1 1127}
This page took 0.255281 seconds and 4 git commands to generate.