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