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