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