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