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