* Rename remote-es1800.c to remote-es.c
[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
FF
36static void
37print_hex_chars PARAMS ((FILE *, unsigned char *, unsigned int));
38
2cd99985
PB
39static void
40show_print PARAMS ((char *, int));
41
42static void
43set_print PARAMS ((char *, int));
44
45static void
46set_radix PARAMS ((char *, int, struct cmd_list_element *));
47
48static void
49set_output_radix PARAMS ((char *, int, struct cmd_list_element *));
50
51static void
a8a69e63 52value_print_array_elements PARAMS ((value, FILE *, int, enum val_prettyprint));
bd5635a1
RP
53
54/* Maximum number of chars to print for a string pointer value
55 or vector contents, or UINT_MAX for no limit. */
56
85f0a848 57unsigned int print_max;
bd5635a1 58
bd5635a1
RP
59/* Default input and output radixes, and output format letter. */
60
61unsigned input_radix = 10;
62unsigned output_radix = 10;
63int output_format = 0;
64
85f0a848
FF
65/* Print repeat counts if there are more than this many repetitions of an
66 element in an array. Referenced by the low level language dependent
67 print routines. */
68
69unsigned int repeat_count_threshold = 10;
0dce3774 70
a8a69e63
FF
71int prettyprint_structs; /* Controls pretty printing of structures */
72int prettyprint_arrays; /* Controls pretty printing of arrays. */
0dce3774 73
a8a69e63
FF
74/* If nonzero, causes unions inside structures or other unions to be
75 printed. */
bd5635a1 76
a8a69e63 77int unionprint; /* Controls printing of nested unions. */
bd5635a1 78
a8a69e63 79/* If nonzero, causes machine addresses to be printed in certain contexts. */
bd5635a1 80
a8a69e63 81int addressprint; /* Controls printing of machine addresses */
bd5635a1 82
a8a69e63 83\f
c7da3ed3
FF
84/* Print data of type TYPE located at VALADDR (within GDB), which came from
85 the inferior at address ADDRESS, onto stdio stream STREAM according to
86 FORMAT (a letter, or 0 for natural format using TYPE).
bd5635a1 87
c7da3ed3
FF
88 If DEREF_REF is nonzero, then dereference references, otherwise just print
89 them like pointers.
bd5635a1 90
c7da3ed3
FF
91 The PRETTY parameter controls prettyprinting.
92
93 If the data are a string pointer, returns the number of string characters
94 printed.
95
96 FIXME: The data at VALADDR is in target byte order. If gdb is ever
97 enhanced to be able to debug more than the single target it was compiled
98 for (specific CPU type and thus specific target byte ordering), then
99 either the print routines are going to have to take this into account,
100 or the data is going to have to be passed into here already converted
101 to the host byte ordering, whichever is more convenient. */
bd5635a1 102
bd5635a1 103
a8a69e63 104int
c7da3ed3 105val_print (type, valaddr, address, stream, format, deref_ref, recurse, pretty)
a8a69e63
FF
106 struct type *type;
107 char *valaddr;
108 CORE_ADDR address;
bd5635a1 109 FILE *stream;
a8a69e63
FF
110 int format;
111 int deref_ref;
112 int recurse;
113 enum val_prettyprint pretty;
bd5635a1 114{
a8a69e63
FF
115 if (pretty == Val_pretty_default)
116 {
117 pretty = prettyprint_structs ? Val_prettyprint : Val_no_prettyprint;
118 }
bd5635a1 119
a8a69e63
FF
120 QUIT;
121
122 /* Ensure that the type is complete and not just a stub. If the type is
123 only a stub and we can't find and substitute its complete type, then
124 print appropriate string and return. Typical types that my be stubs
125 are structs, unions, and C++ methods. */
126
127 check_stub_type (type);
128 if (TYPE_FLAGS (type) & TYPE_FLAG_STUB)
bd5635a1 129 {
a8a69e63
FF
130 fprintf_filtered (stream, "<incomplete type>");
131 fflush (stream);
132 return (0);
bd5635a1 133 }
a8a69e63
FF
134
135 return (LA_VAL_PRINT (type, valaddr, address, stream, format, deref_ref,
136 recurse, pretty));
bd5635a1 137}
a8a69e63 138
bd5635a1
RP
139/* Print the value VAL in C-ish syntax on stream STREAM.
140 FORMAT is a format-letter, or 0 for print in natural format of data type.
141 If the object printed is a string pointer, returns
142 the number of string bytes printed. */
143
144int
145value_print (val, stream, format, pretty)
146 value val;
147 FILE *stream;
2cd99985 148 int format;
bd5635a1
RP
149 enum val_prettyprint pretty;
150{
a8a69e63 151 register unsigned int n, typelen;
bd5635a1
RP
152
153 if (val == 0)
154 {
155 printf_filtered ("<address of value unknown>");
156 return 0;
157 }
158 if (VALUE_OPTIMIZED_OUT (val))
159 {
160 printf_filtered ("<value optimized out>");
161 return 0;
162 }
aec4cb91 163
bd5635a1
RP
164 /* A "repeated" value really contains several values in a row.
165 They are made by the @ operator.
166 Print such values as if they were arrays. */
167
a8a69e63 168 if (VALUE_REPEATED (val))
bd5635a1
RP
169 {
170 n = VALUE_REPETITIONS (val);
171 typelen = TYPE_LENGTH (VALUE_TYPE (val));
172 fprintf_filtered (stream, "{");
173 /* Print arrays of characters using string syntax. */
174 if (typelen == 1 && TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_INT
175 && format == 0)
a8a69e63 176 LA_PRINT_STRING (stream, VALUE_CONTENTS (val), n, 0);
bd5635a1
RP
177 else
178 {
a8a69e63 179 value_print_array_elements (val, stream, format, pretty);
bd5635a1
RP
180 }
181 fprintf_filtered (stream, "}");
a8a69e63 182 return (n * typelen);
bd5635a1
RP
183 }
184 else
185 {
0dce3774
JK
186 struct type *type = VALUE_TYPE (val);
187
bd5635a1
RP
188 /* If it is a pointer, indicate what it points to.
189
190 Print type also if it is a reference.
191
192 C++: if it is a member pointer, we will take care
193 of that when we print it. */
a8a69e63
FF
194 if (TYPE_CODE (type) == TYPE_CODE_PTR ||
195 TYPE_CODE (type) == TYPE_CODE_REF)
bd5635a1
RP
196 {
197 /* Hack: remove (char *) for char strings. Their
198 type is indicated by the quoted string anyway. */
a8a69e63
FF
199 if (TYPE_CODE (type) == TYPE_CODE_PTR &&
200 TYPE_LENGTH (TYPE_TARGET_TYPE (type)) == sizeof(char) &&
201 TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_INT &&
202 !TYPE_UNSIGNED (TYPE_TARGET_TYPE (type)))
bd5635a1
RP
203 {
204 /* Print nothing */
205 }
a8a69e63
FF
206 else
207 {
208 fprintf_filtered (stream, "(");
209 type_print (type, "", stream, -1);
210 fprintf_filtered (stream, ") ");
211 }
212 }
213 return (val_print (type, VALUE_CONTENTS (val),
214 VALUE_ADDRESS (val), stream, format, 1, 0, pretty));
bd5635a1
RP
215 }
216}
217
a8a69e63
FF
218/* Called by various <lang>_val_print routines to print TYPE_CODE_INT's */
219
220void
221val_print_type_code_int (type, valaddr, stream)
9e4667f6 222 struct type *type;
a8a69e63 223 char *valaddr;
9e4667f6
FF
224 FILE *stream;
225{
a8a69e63
FF
226 char *p;
227 /* Pointer to first (i.e. lowest address) nonzero character. */
228 char *first_addr;
229 unsigned int len;
9e4667f6 230
a8a69e63 231 if (TYPE_LENGTH (type) > sizeof (LONGEST))
9e4667f6 232 {
a8a69e63 233 if (TYPE_UNSIGNED (type))
9e4667f6 234 {
a8a69e63
FF
235 /* First figure out whether the number in fact has zeros
236 in all its bytes more significant than least significant
237 sizeof (LONGEST) ones. */
238 len = TYPE_LENGTH (type);
239
240#if TARGET_BYTE_ORDER == BIG_ENDIAN
241 for (p = valaddr;
242 len > sizeof (LONGEST) && p < valaddr + TYPE_LENGTH (type);
243 p++)
244#else /* Little endian. */
245 first_addr = valaddr;
246 for (p = valaddr + TYPE_LENGTH (type);
247 len > sizeof (LONGEST) && p >= valaddr;
248 p--)
249#endif /* Little endian. */
9e4667f6 250 {
a8a69e63 251 if (*p == 0)
9e4667f6 252 {
a8a69e63 253 len--;
9e4667f6 254 }
a8a69e63 255 else
9e4667f6 256 {
a8a69e63 257 break;
9e4667f6
FF
258 }
259 }
a8a69e63
FF
260#if TARGET_BYTE_ORDER == BIG_ENDIAN
261 first_addr = p;
262#endif
263 if (len <= sizeof (LONGEST))
264 {
265 /* We can print it in decimal. */
7efb57c3
FF
266 print_longest (stream, 'u', 0,
267 unpack_long (BUILTIN_TYPE_LONGEST, first_addr));
a8a69e63
FF
268 }
269 else
270 {
271 /* It is big, so print it in hex. */
272 print_hex_chars (stream, (unsigned char *) first_addr, len);
273 }
274 }
275 else
276 {
277 /* Signed. One could assume two's complement (a reasonable
278 assumption, I think) and do better than this. */
279 print_hex_chars (stream, (unsigned char *) valaddr,
280 TYPE_LENGTH (type));
9e4667f6
FF
281 }
282 }
a8a69e63
FF
283 else
284 {
285#ifdef PRINT_TYPELESS_INTEGER
286 PRINT_TYPELESS_INTEGER (stream, type, unpack_long (type, valaddr));
287#else
7efb57c3
FF
288 print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0,
289 unpack_long (type, valaddr));
a8a69e63
FF
290#endif
291 }
292}
9e4667f6 293
7efb57c3
FF
294/* Print a number according to FORMAT which is one of d,u,x,o,b,h,w,g.
295 The raison d'etre of this function is to consolidate printing of LONG_LONG's
296 into this one function. Some platforms have long longs but don't have a
297 printf() that supports "ll" in the format string. We handle these by seeing
298 if the number is actually a long, and if not we just bail out and print the
299 number in hex. The format chars b,h,w,g are from
300 print_scalar_formatted(). USE_LOCAL says whether or not to call the
301 local formatting routine to get the format. */
302
303void
304print_longest (stream, format, use_local, val_long)
305 FILE *stream;
306 char format;
307 int use_local;
308 LONGEST val_long;
309{
310#if defined (CC_HAS_LONG_LONG) && !defined (PRINTF_HAS_LONG_LONG)
311 long vtop, vbot;
312
313 vtop = val_long >> (sizeof (long) * HOST_CHAR_BIT);
314 vbot = (long) val_long;
315
316 if ((format == 'd' && (val_long < INT_MIN || val_long > INT_MAX))
317 || ((format == 'u' || format == 'x') && val_long > UINT_MAX))
318 {
319 fprintf_filtered (stream, "0x%x%08x", vtop, vbot);
320 return;
321 }
322#endif
323
324#ifdef PRINTF_HAS_LONG_LONG
325 switch (format)
326 {
327 case 'd':
328 fprintf_filtered (stream,
329 use_local ? local_decimal_format_custom ("ll")
330 : "%lld",
331 val_long);
332 break;
333 case 'u':
334 fprintf_filtered (stream, "%llu", val_long);
335 break;
336 case 'x':
337 fprintf_filtered (stream,
338 use_local ? local_hex_format_custom ("ll")
339 : "%llx",
340 val_long);
341 break;
342 case 'o':
343 fprintf_filtered (stream,
344 use_local ? local_octal_format_custom ("ll")
345 : "%llo",
346 break;
347 case 'b':
348 fprintf_filtered (stream, local_hex_format_custom ("02ll"), val_long);
349 break;
350 case 'h':
351 fprintf_filtered (stream, local_hex_format_custom ("04ll"), val_long);
352 break;
353 case 'w':
354 fprintf_filtered (stream, local_hex_format_custom ("08ll"), val_long);
355 break;
356 case 'g':
357 fprintf_filtered (stream, local_hex_format_custom ("016ll"), val_long);
358 break;
359 default:
360 abort ();
361 }
362#else /* !PRINTF_HAS_LONG_LONG */
363 /* In the following it is important to coerce (val_long) to a long. It does
364 nothing if !LONG_LONG, but it will chop off the top half (which we know
365 we can ignore) if the host supports long longs. */
366
367 switch (format)
368 {
369 case 'd':
370 fprintf_filtered (stream,
371 use_local ? local_decimal_format_custom ("l")
372 : "%ld",
373 (long) val_long);
374 break;
375 case 'u':
376 fprintf_filtered (stream, "%lu", (unsigned long) val_long);
377 break;
378 case 'x':
379 fprintf_filtered (stream,
380 use_local ? local_hex_format_custom ("l")
381 : "%lx",
382 (long) val_long);
383 break;
384 case 'o':
385 fprintf_filtered (stream,
386 use_local ? local_octal_format_custom ("l")
387 : "%lo",
388 (long) val_long);
389 break;
390 case 'b':
391 fprintf_filtered (stream, local_hex_format_custom ("02l"),
392 (long) val_long);
393 break;
394 case 'h':
395 fprintf_filtered (stream, local_hex_format_custom ("04l"),
396 (long) val_long);
397 break;
398 case 'w':
399 fprintf_filtered (stream, local_hex_format_custom ("08l"),
400 (long) val_long);
401 break;
402 case 'g':
403 fprintf_filtered (stream, local_hex_format_custom ("016l"),
404 (long) val_long);
405 break;
406 default:
407 abort ();
408 }
409#endif /* !PRINTF_HAS_LONG_LONG */
410}
411
a8a69e63
FF
412/* Print a floating point value of type TYPE, pointed to in GDB by VALADDR,
413 on STREAM. */
bd5635a1 414
a8a69e63
FF
415void
416print_floating (valaddr, type, stream)
417 char *valaddr;
bd5635a1
RP
418 struct type *type;
419 FILE *stream;
bd5635a1 420{
a8a69e63
FF
421 double doub;
422 int inv;
423 unsigned len = TYPE_LENGTH (type);
424
425#if defined (IEEE_FLOAT)
bd5635a1 426
a8a69e63
FF
427 /* Check for NaN's. Note that this code does not depend on us being
428 on an IEEE conforming system. It only depends on the target
429 machine using IEEE representation. This means (a)
430 cross-debugging works right, and (2) IEEE_FLOAT can (and should)
431 be defined for systems like the 68881, which uses IEEE
432 representation, but is not IEEE conforming. */
bd5635a1 433
a8a69e63
FF
434 {
435 long low, high;
436 /* Is the sign bit 0? */
437 int nonnegative;
438 /* Is it is a NaN (i.e. the exponent is all ones and
439 the fraction is nonzero)? */
440 int is_nan;
bd5635a1 441
a8a69e63
FF
442 if (len == sizeof (float))
443 {
444 /* It's single precision. */
445 memcpy ((char *) &low, valaddr, sizeof (low));
446 /* target -> host. */
447 SWAP_TARGET_AND_HOST (&low, sizeof (float));
448 nonnegative = low >= 0;
449 is_nan = ((((low >> 23) & 0xFF) == 0xFF)
450 && 0 != (low & 0x7FFFFF));
451 low &= 0x7fffff;
452 high = 0;
453 }
454 else
455 {
456 /* It's double precision. Get the high and low words. */
bd5635a1 457
a8a69e63
FF
458#if TARGET_BYTE_ORDER == BIG_ENDIAN
459 memcpy (&low, valaddr+4, sizeof (low));
460 memcpy (&high, valaddr+0, sizeof (high));
461#else
462 memcpy (&low, valaddr+0, sizeof (low));
463 memcpy (&high, valaddr+4, sizeof (high));
464#endif
465 SWAP_TARGET_AND_HOST (&low, sizeof (low));
466 SWAP_TARGET_AND_HOST (&high, sizeof (high));
467 nonnegative = high >= 0;
468 is_nan = (((high >> 20) & 0x7ff) == 0x7ff
469 && ! ((((high & 0xfffff) == 0)) && (low == 0)));
470 high &= 0xfffff;
471 }
bd5635a1 472
a8a69e63
FF
473 if (is_nan)
474 {
475 /* The meaning of the sign and fraction is not defined by IEEE.
476 But the user might know what they mean. For example, they
477 (in an implementation-defined manner) distinguish between
478 signaling and quiet NaN's. */
479 if (high)
480 fprintf_filtered (stream, "-NaN(0x%lx%.8lx)" + nonnegative,
481 high, low);
482 else
483 fprintf_filtered (stream, "-NaN(0x%lx)" + nonnegative, low);
484 return;
485 }
486 }
487#endif /* IEEE_FLOAT. */
bd5635a1 488
a8a69e63
FF
489 doub = unpack_double (type, valaddr, &inv);
490 if (inv)
491 fprintf_filtered (stream, "<invalid float value>");
492 else
493 fprintf_filtered (stream, len <= sizeof(float) ? "%.9g" : "%.17g", doub);
bd5635a1
RP
494}
495
a8a69e63 496/* VALADDR points to an integer of LEN bytes. Print it in hex on stream. */
bd5635a1
RP
497
498static void
a8a69e63 499print_hex_chars (stream, valaddr, len)
bd5635a1 500 FILE *stream;
a8a69e63
FF
501 unsigned char *valaddr;
502 unsigned len;
bd5635a1 503{
a8a69e63
FF
504 unsigned char *p;
505
506 fprintf_filtered (stream, "0x");
507#if TARGET_BYTE_ORDER == BIG_ENDIAN
508 for (p = valaddr;
509 p < valaddr + len;
510 p++)
511#else /* Little endian. */
512 for (p = valaddr + len - 1;
513 p >= valaddr;
514 p--)
515#endif
bd5635a1 516 {
a8a69e63 517 fprintf_filtered (stream, "%02x", *p);
bd5635a1 518 }
a8a69e63 519}
bd5635a1 520
a8a69e63
FF
521/* Called by various <lang>_val_print routines to print elements of an
522 array in the form "<elem1>, <elem2>, <elem3>, ...".
4a11eef2 523
a8a69e63
FF
524 (FIXME?) Assumes array element separator is a comma, which is correct
525 for all languages currently handled.
526 (FIXME?) Some languages have a notation for repeated array elements,
527 perhaps we should try to use that notation when appropriate.
528 */
bd5635a1 529
a8a69e63
FF
530void
531val_print_array_elements (type, valaddr, address, stream, format, deref_ref,
532 recurse, pretty, i)
533 struct type *type;
534 char *valaddr;
535 CORE_ADDR address;
536 FILE *stream;
537 int format;
538 int deref_ref;
539 int recurse;
540 enum val_prettyprint pretty;
541 unsigned int i;
542{
543 unsigned int things_printed = 0;
544 unsigned len;
545 struct type *elttype;
546 unsigned eltlen;
547 /* Position of the array element we are examining to see
548 whether it is repeated. */
549 unsigned int rep1;
550 /* Number of repetitions we have detected so far. */
551 unsigned int reps;
552
553 elttype = TYPE_TARGET_TYPE (type);
554 eltlen = TYPE_LENGTH (elttype);
555 len = TYPE_LENGTH (type) / eltlen;
556
557 for (; i < len && things_printed < print_max; i++)
bd5635a1 558 {
a8a69e63 559 if (i != 0)
bd5635a1 560 {
a8a69e63 561 if (prettyprint_arrays)
bd5635a1 562 {
a8a69e63
FF
563 fprintf_filtered (stream, ",\n");
564 print_spaces_filtered (2 + 2 * recurse, stream);
bd5635a1 565 }
a8a69e63 566 else
bd5635a1 567 {
a8a69e63 568 fprintf_filtered (stream, ", ");
bd5635a1 569 }
bd5635a1 570 }
a8a69e63
FF
571 wrap_here (n_spaces (2 + 2 * recurse));
572
573 rep1 = i + 1;
574 reps = 1;
575 while ((rep1 < len) &&
576 !memcmp (valaddr + i * eltlen, valaddr + rep1 * eltlen, eltlen))
577 {
578 ++reps;
579 ++rep1;
580 }
581
582 if (reps > repeat_count_threshold)
bd5635a1 583 {
a8a69e63
FF
584 val_print (elttype, valaddr + i * eltlen, 0, stream, format,
585 deref_ref, recurse + 1, pretty);
586 fprintf_filtered (stream, " <repeats %u times>", reps);
587 i = rep1 - 1;
588 things_printed += repeat_count_threshold;
bd5635a1 589 }
bd5635a1
RP
590 else
591 {
a8a69e63
FF
592 val_print (elttype, valaddr + i * eltlen, 0, stream, format,
593 deref_ref, recurse + 1, pretty);
594 things_printed++;
bd5635a1 595 }
a8a69e63
FF
596 }
597 if (i < len)
598 {
599 fprintf_filtered (stream, "...");
600 }
601}
e2aab031 602
a8a69e63
FF
603static void
604value_print_array_elements (val, stream, format, pretty)
605 value val;
606 FILE *stream;
607 int format;
608 enum val_prettyprint pretty;
609{
610 unsigned int things_printed = 0;
611 register unsigned int i, n, typelen;
612 /* Position of the array elem we are examining to see if it is repeated. */
613 unsigned int rep1;
614 /* Number of repetitions we have detected so far. */
615 unsigned int reps;
616
617 n = VALUE_REPETITIONS (val);
618 typelen = TYPE_LENGTH (VALUE_TYPE (val));
619 for (i = 0; i < n && things_printed < print_max; i++)
620 {
621 if (i != 0)
622 {
623 fprintf_filtered (stream, ", ");
624 }
625 wrap_here ("");
626
627 rep1 = i + 1;
628 reps = 1;
629 while (rep1 < n && !memcmp (VALUE_CONTENTS (val) + typelen * i,
630 VALUE_CONTENTS (val) + typelen * rep1,
631 typelen))
632 {
633 ++reps;
634 ++rep1;
635 }
636
637 if (reps > repeat_count_threshold)
4ace50a5 638 {
a8a69e63
FF
639 val_print (VALUE_TYPE (val), VALUE_CONTENTS (val) + typelen * i,
640 VALUE_ADDRESS (val) + typelen * i, stream, format, 1,
641 0, pretty);
642 fprintf (stream, " <repeats %u times>", reps);
643 i = rep1 - 1;
644 things_printed += repeat_count_threshold;
4ace50a5
FF
645 }
646 else
647 {
a8a69e63
FF
648 val_print (VALUE_TYPE (val), VALUE_CONTENTS (val) + typelen * i,
649 VALUE_ADDRESS (val) + typelen * i, stream, format, 1,
650 0, pretty);
651 things_printed++;
4ace50a5 652 }
a8a69e63
FF
653 }
654 if (i < n)
655 {
656 fprintf_filtered (stream, "...");
bd5635a1
RP
657 }
658}
a8a69e63 659
7efb57c3
FF
660/* Print a string from the inferior, starting at ADDR and printing up to LEN
661 characters, to STREAM. If LEN is zero, printing stops at the first null
662 byte, otherwise printing proceeds (including null bytes) until either
663 print_max or LEN characters have been printed.
664
665 Always fetch print_max+1 characters, even though LA_PRINT_STRING might want
666 to print more or fewer (with repeated characters). This is so that we
667 don't spend forever fetching if we print a long string consisting of the
668 same character repeated. Also so we can do it all in one memory operation,
669 which is faster. However, this will be slower if print_max is set high,
670 e.g. if you set print_max to 1000, not only will it take a long time to
671 fetch short strings, but if you are near the end of the address space, it
672 might not work.
673
674 If the number of characters we actually print is limited because of hitting
675 print_max, when LEN would have explicitly or implicitly (in the case of a
676 null terminated string with another non-null character available to print)
677 allowed us to print more, we print ellipsis ("...") after the printed string
678 to indicate that more characters were available to print but that we were
679 limited by print_max. To do this correctly requires that we always fetch
680 one more than the number of characters we could potentially print, so that
681 if we do print the maximum number, we can tell whether or not a null byte
682 would have been the next character, in the case of C style strings.
683 For non-C style strings, only the value of LEN is pertinent in deciding
684 whether or not to print ellipsis.
685
686 FIXME: If LEN is nonzero and less than print_max, we could get away
687 with only fetching the specified number of characters from the inferior. */
688
c7da3ed3 689int
7efb57c3 690val_print_string (addr, len, stream)
c7da3ed3 691 CORE_ADDR addr;
7efb57c3 692 unsigned int len;
c7da3ed3
FF
693 FILE *stream;
694{
7efb57c3
FF
695 int first_addr_err = 0; /* Nonzero if first address out of bounds */
696 int force_ellipsis = 0; /* Force ellipsis to be printed if nonzero */
c7da3ed3
FF
697 int errcode;
698 unsigned char c;
699 char *string;
c7da3ed3
FF
700
701 /* Get first character. */
702 errcode = target_read_memory (addr, (char *)&c, 1);
703 if (errcode != 0)
704 {
705 /* First address out of bounds. */
706 first_addr_err = 1;
707 }
7efb57c3 708 else if (print_max < UINT_MAX)
c7da3ed3 709 {
7efb57c3
FF
710 string = (char *) alloca (print_max + 1);
711 memset (string, 0, print_max + 1);
c7da3ed3 712
c7da3ed3 713 QUIT;
7efb57c3 714 errcode = target_read_memory (addr, string, print_max + 1);
c7da3ed3
FF
715 if (errcode != 0)
716 {
7efb57c3
FF
717 /* Try reading just one character. If that succeeds, assume we hit
718 the end of the address space, but the initial part of the string
719 is probably safe. */
c7da3ed3
FF
720 char x[1];
721 errcode = target_read_memory (addr, x, 1);
722 }
7efb57c3
FF
723 if (len == 0)
724 {
725 /* When the length is unspecified, such as when printing C style
726 null byte terminated strings, then scan the string looking for
727 the terminator in the first print_max characters. If a terminator
728 is found, then it determines the length, otherwise print_max
729 determines the length. */
730 for (;len < print_max; len++)
c7da3ed3 731 {
7efb57c3
FF
732 if (string[len] == '\0')
733 {
734 break;
735 }
736 }
737 /* If the first unprinted character is not the null terminator, set
738 the flag to force ellipses. This is true whether or not we broke
739 out of the above loop because we found a terminator, or whether
740 we simply hit the limit on how many characters to print. */
741 if (string[len] != '\0')
742 {
743 force_ellipsis = 1;
c7da3ed3 744 }
7efb57c3
FF
745 }
746 else if (len > print_max)
747 {
748 /* Printing less than the number of characters actually requested
749 always makes us print ellipsis. */
750 len = print_max;
751 force_ellipsis = 1;
752 }
c7da3ed3
FF
753 QUIT;
754
755 if (addressprint)
756 {
757 fputs_filtered (" ", stream);
758 }
7efb57c3 759 LA_PRINT_STRING (stream, string, len, force_ellipsis);
c7da3ed3
FF
760 }
761
762 if (errcode != 0)
763 {
764 if (errcode == EIO)
765 {
766 fprintf_filtered (stream,
767 (" <Address 0x%x out of bounds>" + first_addr_err),
7efb57c3 768 addr + len);
c7da3ed3
FF
769 }
770 else
771 {
7efb57c3 772 error ("Error reading memory address 0x%x: %s.", addr + len,
c7da3ed3
FF
773 safe_strerror (errcode));
774 }
775 }
776 fflush (stream);
7efb57c3 777 return (len);
c7da3ed3 778}
bd5635a1 779\f
e1ce8aa5 780#if 0
bd5635a1
RP
781/* Validate an input or output radix setting, and make sure the user
782 knows what they really did here. Radix setting is confusing, e.g.
783 setting the input radix to "10" never changes it! */
784
e1ce8aa5 785/* ARGSUSED */
bd5635a1
RP
786static void
787set_input_radix (args, from_tty, c)
788 char *args;
789 int from_tty;
790 struct cmd_list_element *c;
791{
792 unsigned radix = *(unsigned *)c->var;
793
794 if (from_tty)
795 printf_filtered ("Input radix set to decimal %d, hex %x, octal %o\n",
796 radix, radix, radix);
797}
e1ce8aa5 798#endif
bd5635a1 799
e1ce8aa5 800/* ARGSUSED */
bd5635a1
RP
801static void
802set_output_radix (args, from_tty, c)
803 char *args;
804 int from_tty;
805 struct cmd_list_element *c;
806{
807 unsigned radix = *(unsigned *)c->var;
808
809 if (from_tty)
810 printf_filtered ("Output radix set to decimal %d, hex %x, octal %o\n",
811 radix, radix, radix);
812
813 /* FIXME, we really should be able to validate the setting BEFORE
814 it takes effect. */
815 switch (radix)
816 {
817 case 16:
818 output_format = 'x';
819 break;
820 case 10:
821 output_format = 0;
822 break;
823 case 8:
824 output_format = 'o'; /* octal */
825 break;
826 default:
827 output_format = 0;
828 error ("Unsupported radix ``decimal %d''; using decimal output",
829 radix);
830 }
831}
832
833/* Both at once */
834static void
835set_radix (arg, from_tty, c)
836 char *arg;
837 int from_tty;
838 struct cmd_list_element *c;
839{
840 unsigned radix = *(unsigned *)c->var;
841
842 if (from_tty)
843 printf_filtered ("Radix set to decimal %d, hex %x, octal %o\n",
844 radix, radix, radix);
845
846 input_radix = radix;
847 output_radix = radix;
848
849 set_output_radix (arg, 0, c);
850}
851\f
f266e564
JK
852/*ARGSUSED*/
853static void
854set_print (arg, from_tty)
855 char *arg;
856 int from_tty;
857{
858 printf (
859"\"set print\" must be followed by the name of a print subcommand.\n");
860 help_list (setprintlist, "set print ", -1, stdout);
861}
862
863/*ARGSUSED*/
864static void
865show_print (args, from_tty)
866 char *args;
867 int from_tty;
868{
869 cmd_show_list (showprintlist, from_tty, "");
870}
871\f
bd5635a1
RP
872void
873_initialize_valprint ()
874{
875 struct cmd_list_element *c;
876
f266e564
JK
877 add_prefix_cmd ("print", no_class, set_print,
878 "Generic command for setting how things print.",
879 &setprintlist, "set print ", 0, &setlist);
36b9d39c
JG
880 add_alias_cmd ("p", "print", no_class, 1, &setlist);
881 add_alias_cmd ("pr", "print", no_class, 1, &setlist); /* prefer set print
882 to set prompt */
f266e564
JK
883 add_prefix_cmd ("print", no_class, show_print,
884 "Generic command for showing print settings.",
885 &showprintlist, "show print ", 0, &showlist);
36b9d39c
JG
886 add_alias_cmd ("p", "print", no_class, 1, &showlist);
887 add_alias_cmd ("pr", "print", no_class, 1, &showlist);
f266e564 888
bd5635a1 889 add_show_from_set
f266e564 890 (add_set_cmd ("elements", no_class, var_uinteger, (char *)&print_max,
bd5635a1 891 "Set limit on string chars or array elements to print.\n\
f266e564
JK
892\"set print elements 0\" causes there to be no limit.",
893 &setprintlist),
894 &showprintlist);
bd5635a1 895
85f0a848
FF
896 add_show_from_set
897 (add_set_cmd ("repeats", no_class, var_uinteger,
898 (char *)&repeat_count_threshold,
899 "Set threshold for repeated print elements.\n\
900\"set print repeats 0\" causes all elements to be individually printed.",
901 &setprintlist),
902 &showprintlist);
903
bd5635a1 904 add_show_from_set
a8a69e63
FF
905 (add_set_cmd ("pretty", class_support, var_boolean,
906 (char *)&prettyprint_structs,
bd5635a1 907 "Set prettyprinting of structures.",
f266e564
JK
908 &setprintlist),
909 &showprintlist);
bd5635a1
RP
910
911 add_show_from_set
f266e564 912 (add_set_cmd ("union", class_support, var_boolean, (char *)&unionprint,
bd5635a1 913 "Set printing of unions interior to structures.",
f266e564
JK
914 &setprintlist),
915 &showprintlist);
bd5635a1
RP
916
917 add_show_from_set
a8a69e63
FF
918 (add_set_cmd ("array", class_support, var_boolean,
919 (char *)&prettyprint_arrays,
bd5635a1 920 "Set prettyprinting of arrays.",
f266e564
JK
921 &setprintlist),
922 &showprintlist);
bd5635a1
RP
923
924 add_show_from_set
f266e564 925 (add_set_cmd ("address", class_support, var_boolean, (char *)&addressprint,
bd5635a1 926 "Set printing of addresses.",
f266e564
JK
927 &setprintlist),
928 &showprintlist);
bd5635a1
RP
929
930#if 0
931 /* The "show radix" cmd isn't good enough to show two separate values.
932 The rest of the code works, but the show part is confusing, so don't
933 let them be set separately 'til we work out "show". */
934 c = add_set_cmd ("input-radix", class_support, var_uinteger,
935 (char *)&input_radix,
936 "Set default input radix for entering numbers.",
937 &setlist);
938 add_show_from_set (c, &showlist);
939 c->function = set_input_radix;
940
941 c = add_set_cmd ("output-radix", class_support, var_uinteger,
942 (char *)&output_radix,
943 "Set default output radix for printing of values.",
944 &setlist);
945 add_show_from_set (c, &showlist);
946 c->function = set_output_radix;
947#endif
948
949 c = add_set_cmd ("radix", class_support, var_uinteger,
950 (char *)&output_radix,
951 "Set default input and output number radix.",
952 &setlist);
953 add_show_from_set (c, &showlist);
2cd99985 954 c->function.sfunc = set_radix;
bd5635a1
RP
955
956 /* Give people the defaults which they are used to. */
a8a69e63
FF
957 prettyprint_structs = 0;
958 prettyprint_arrays = 0;
bd5635a1 959 unionprint = 1;
bd5635a1 960 addressprint = 1;
bd5635a1 961 print_max = 200;
bd5635a1 962}
This page took 0.233283 seconds and 4 git commands to generate.