]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/valprint.c
*** empty log message ***
[thirdparty/binutils-gdb.git] / gdb / valprint.c
CommitLineData
c906108c 1/* Print values for GDB, the GNU debugger.
5c1c87f0 2
6aba47ca 3 Copyright (C) 1986, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
9b254dd1 4 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
777ea8f1 5 Free Software Foundation, Inc.
c906108c 6
c5aa993b 7 This file is part of GDB.
c906108c 8
c5aa993b
JM
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
a9762ec7 11 the Free Software Foundation; either version 3 of the License, or
c5aa993b 12 (at your option) any later version.
c906108c 13
c5aa993b
JM
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
c906108c 18
c5aa993b 19 You should have received a copy of the GNU General Public License
a9762ec7 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
21
22#include "defs.h"
23#include "gdb_string.h"
24#include "symtab.h"
25#include "gdbtypes.h"
26#include "value.h"
27#include "gdbcore.h"
28#include "gdbcmd.h"
29#include "target.h"
c906108c 30#include "language.h"
c906108c
SS
31#include "annotate.h"
32#include "valprint.h"
39424bef 33#include "floatformat.h"
d16aafd8 34#include "doublest.h"
19ca80ba 35#include "exceptions.h"
7678ef8f 36#include "dfp.h"
c906108c
SS
37
38#include <errno.h>
39
40/* Prototypes for local functions */
41
777ea8f1 42static int partial_memory_read (CORE_ADDR memaddr, gdb_byte *myaddr,
917317f4
JM
43 int len, int *errnoptr);
44
a14ed312 45static void show_print (char *, int);
c906108c 46
a14ed312 47static void set_print (char *, int);
c906108c 48
a14ed312 49static void set_radix (char *, int);
c906108c 50
a14ed312 51static void show_radix (char *, int);
c906108c 52
a14ed312 53static void set_input_radix (char *, int, struct cmd_list_element *);
c906108c 54
a14ed312 55static void set_input_radix_1 (int, unsigned);
c906108c 56
a14ed312 57static void set_output_radix (char *, int, struct cmd_list_element *);
c906108c 58
a14ed312 59static void set_output_radix_1 (int, unsigned);
c906108c 60
a14ed312 61void _initialize_valprint (void);
c906108c 62
c906108c 63#define PRINT_MAX_DEFAULT 200 /* Start print_max off at this value. */
79a45b7d
TT
64
65struct value_print_options user_print_options =
66{
67 Val_pretty_default, /* pretty */
68 0, /* prettyprint_arrays */
69 0, /* prettyprint_structs */
70 0, /* vtblprint */
71 1, /* unionprint */
72 1, /* addressprint */
73 0, /* objectprint */
74 PRINT_MAX_DEFAULT, /* print_max */
75 10, /* repeat_count_threshold */
76 0, /* output_format */
77 0, /* format */
78 0, /* stop_print_at_null */
79 0, /* inspect_it */
80 0, /* print_array_indexes */
81 0, /* deref_ref */
82 1, /* static_field_print */
83 1 /* pascal_static_field_print */
84};
85
86/* Initialize *OPTS to be a copy of the user print options. */
87void
88get_user_print_options (struct value_print_options *opts)
89{
90 *opts = user_print_options;
91}
92
93/* Initialize *OPTS to be a copy of the user print options, but with
94 pretty-printing disabled. */
95void
96get_raw_print_options (struct value_print_options *opts)
97{
98 *opts = user_print_options;
99 opts->pretty = Val_no_prettyprint;
100}
101
102/* Initialize *OPTS to be a copy of the user print options, but using
103 FORMAT as the formatting option. */
104void
105get_formatted_print_options (struct value_print_options *opts,
106 char format)
107{
108 *opts = user_print_options;
109 opts->format = format;
110}
111
920d2a44
AC
112static void
113show_print_max (struct ui_file *file, int from_tty,
114 struct cmd_list_element *c, const char *value)
115{
116 fprintf_filtered (file, _("\
117Limit on string chars or array elements to print is %s.\n"),
118 value);
119}
120
c906108c
SS
121
122/* Default input and output radixes, and output format letter. */
123
124unsigned input_radix = 10;
920d2a44
AC
125static void
126show_input_radix (struct ui_file *file, int from_tty,
127 struct cmd_list_element *c, const char *value)
128{
129 fprintf_filtered (file, _("\
130Default input radix for entering numbers is %s.\n"),
131 value);
132}
133
c906108c 134unsigned output_radix = 10;
920d2a44
AC
135static void
136show_output_radix (struct ui_file *file, int from_tty,
137 struct cmd_list_element *c, const char *value)
138{
139 fprintf_filtered (file, _("\
140Default output radix for printing of values is %s.\n"),
141 value);
142}
c906108c 143
e79af960
JB
144/* By default we print arrays without printing the index of each element in
145 the array. This behavior can be changed by setting PRINT_ARRAY_INDEXES. */
146
e79af960
JB
147static void
148show_print_array_indexes (struct ui_file *file, int from_tty,
149 struct cmd_list_element *c, const char *value)
150{
151 fprintf_filtered (file, _("Printing of array indexes is %s.\n"), value);
152}
153
c906108c
SS
154/* Print repeat counts if there are more than this many repetitions of an
155 element in an array. Referenced by the low level language dependent
156 print routines. */
157
920d2a44
AC
158static void
159show_repeat_count_threshold (struct ui_file *file, int from_tty,
160 struct cmd_list_element *c, const char *value)
161{
162 fprintf_filtered (file, _("Threshold for repeated print elements is %s.\n"),
163 value);
164}
c906108c
SS
165
166/* If nonzero, stops printing of char arrays at first null. */
167
920d2a44
AC
168static void
169show_stop_print_at_null (struct ui_file *file, int from_tty,
170 struct cmd_list_element *c, const char *value)
171{
172 fprintf_filtered (file, _("\
173Printing of char arrays to stop at first null char is %s.\n"),
174 value);
175}
c906108c
SS
176
177/* Controls pretty printing of structures. */
178
920d2a44
AC
179static void
180show_prettyprint_structs (struct ui_file *file, int from_tty,
181 struct cmd_list_element *c, const char *value)
182{
183 fprintf_filtered (file, _("Prettyprinting of structures is %s.\n"), value);
184}
c906108c
SS
185
186/* Controls pretty printing of arrays. */
187
920d2a44
AC
188static void
189show_prettyprint_arrays (struct ui_file *file, int from_tty,
190 struct cmd_list_element *c, const char *value)
191{
192 fprintf_filtered (file, _("Prettyprinting of arrays is %s.\n"), value);
193}
c906108c
SS
194
195/* If nonzero, causes unions inside structures or other unions to be
196 printed. */
197
920d2a44
AC
198static void
199show_unionprint (struct ui_file *file, int from_tty,
200 struct cmd_list_element *c, const char *value)
201{
202 fprintf_filtered (file, _("\
203Printing of unions interior to structures is %s.\n"),
204 value);
205}
c906108c
SS
206
207/* If nonzero, causes machine addresses to be printed in certain contexts. */
208
920d2a44
AC
209static void
210show_addressprint (struct ui_file *file, int from_tty,
211 struct cmd_list_element *c, const char *value)
212{
213 fprintf_filtered (file, _("Printing of addresses is %s.\n"), value);
214}
c906108c 215\f
c5aa993b 216
d8ca156b
JB
217/* Print using the given LANGUAGE the data of type TYPE located at VALADDR
218 (within GDB), which came from the inferior at address ADDRESS, onto
79a45b7d 219 stdio stream STREAM according to OPTIONS.
c906108c
SS
220
221 If the data are a string pointer, returns the number of string characters
222 printed.
223
224 FIXME: The data at VALADDR is in target byte order. If gdb is ever
225 enhanced to be able to debug more than the single target it was compiled
226 for (specific CPU type and thus specific target byte ordering), then
227 either the print routines are going to have to take this into account,
228 or the data is going to have to be passed into here already converted
229 to the host byte ordering, whichever is more convenient. */
230
231
232int
fc1a4b47 233val_print (struct type *type, const gdb_byte *valaddr, int embedded_offset,
79a45b7d
TT
234 CORE_ADDR address, struct ui_file *stream, int recurse,
235 const struct value_print_options *options,
d8ca156b 236 const struct language_defn *language)
c906108c 237{
19ca80ba
DJ
238 volatile struct gdb_exception except;
239 int ret = 0;
79a45b7d 240 struct value_print_options local_opts = *options;
c906108c 241 struct type *real_type = check_typedef (type);
79a45b7d
TT
242
243 if (local_opts.pretty == Val_pretty_default)
244 local_opts.pretty = (local_opts.prettyprint_structs
245 ? Val_prettyprint : Val_no_prettyprint);
c5aa993b 246
c906108c
SS
247 QUIT;
248
249 /* Ensure that the type is complete and not just a stub. If the type is
250 only a stub and we can't find and substitute its complete type, then
251 print appropriate string and return. */
252
74a9bb82 253 if (TYPE_STUB (real_type))
c906108c
SS
254 {
255 fprintf_filtered (stream, "<incomplete type>");
256 gdb_flush (stream);
257 return (0);
258 }
c5aa993b 259
19ca80ba
DJ
260 TRY_CATCH (except, RETURN_MASK_ERROR)
261 {
d8ca156b 262 ret = language->la_val_print (type, valaddr, embedded_offset, address,
79a45b7d 263 stream, recurse, &local_opts);
19ca80ba
DJ
264 }
265 if (except.reason < 0)
266 fprintf_filtered (stream, _("<error reading variable>"));
267
268 return ret;
c906108c
SS
269}
270
806048c6
DJ
271/* Check whether the value VAL is printable. Return 1 if it is;
272 return 0 and print an appropriate error message to STREAM if it
273 is not. */
c906108c 274
806048c6
DJ
275static int
276value_check_printable (struct value *val, struct ui_file *stream)
c906108c
SS
277{
278 if (val == 0)
279 {
806048c6 280 fprintf_filtered (stream, _("<address of value unknown>"));
c906108c
SS
281 return 0;
282 }
806048c6 283
feb13ab0 284 if (value_optimized_out (val))
c906108c 285 {
806048c6 286 fprintf_filtered (stream, _("<value optimized out>"));
c906108c
SS
287 return 0;
288 }
806048c6
DJ
289
290 return 1;
291}
292
d8ca156b 293/* Print using the given LANGUAGE the value VAL onto stream STREAM according
79a45b7d 294 to OPTIONS.
806048c6
DJ
295
296 If the data are a string pointer, returns the number of string characters
297 printed.
298
299 This is a preferable interface to val_print, above, because it uses
300 GDB's value mechanism. */
301
302int
79a45b7d
TT
303common_val_print (struct value *val, struct ui_file *stream, int recurse,
304 const struct value_print_options *options,
d8ca156b 305 const struct language_defn *language)
806048c6
DJ
306{
307 if (!value_check_printable (val, stream))
308 return 0;
309
310 return val_print (value_type (val), value_contents_all (val),
311 value_embedded_offset (val), VALUE_ADDRESS (val),
79a45b7d 312 stream, recurse, options, language);
806048c6
DJ
313}
314
79a45b7d
TT
315/* Print the value VAL in C-ish syntax on stream STREAM according to
316 OPTIONS.
806048c6
DJ
317 If the object printed is a string pointer, returns
318 the number of string bytes printed. */
319
320int
79a45b7d
TT
321value_print (struct value *val, struct ui_file *stream,
322 const struct value_print_options *options)
806048c6
DJ
323{
324 if (!value_check_printable (val, stream))
325 return 0;
326
79a45b7d 327 return LA_VALUE_PRINT (val, stream, options);
c906108c
SS
328}
329
330/* Called by various <lang>_val_print routines to print
331 TYPE_CODE_INT's. TYPE is the type. VALADDR is the address of the
332 value. STREAM is where to print the value. */
333
334void
fc1a4b47 335val_print_type_code_int (struct type *type, const gdb_byte *valaddr,
fba45db2 336 struct ui_file *stream)
c906108c 337{
d44e8473
MD
338 enum bfd_endian byte_order = gdbarch_byte_order (current_gdbarch);
339
c906108c
SS
340 if (TYPE_LENGTH (type) > sizeof (LONGEST))
341 {
342 LONGEST val;
343
344 if (TYPE_UNSIGNED (type)
345 && extract_long_unsigned_integer (valaddr, TYPE_LENGTH (type),
346 &val))
347 {
348 print_longest (stream, 'u', 0, val);
349 }
350 else
351 {
352 /* Signed, or we couldn't turn an unsigned value into a
353 LONGEST. For signed values, one could assume two's
354 complement (a reasonable assumption, I think) and do
355 better than this. */
356 print_hex_chars (stream, (unsigned char *) valaddr,
d44e8473 357 TYPE_LENGTH (type), byte_order);
c906108c
SS
358 }
359 }
360 else
361 {
c906108c
SS
362 print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0,
363 unpack_long (type, valaddr));
c906108c
SS
364 }
365}
366
4f2aea11
MK
367void
368val_print_type_code_flags (struct type *type, const gdb_byte *valaddr,
369 struct ui_file *stream)
370{
befae759 371 ULONGEST val = unpack_long (type, valaddr);
4f2aea11
MK
372 int bitpos, nfields = TYPE_NFIELDS (type);
373
374 fputs_filtered ("[ ", stream);
375 for (bitpos = 0; bitpos < nfields; bitpos++)
376 {
316703b9
MK
377 if (TYPE_FIELD_BITPOS (type, bitpos) != -1
378 && (val & ((ULONGEST)1 << bitpos)))
4f2aea11
MK
379 {
380 if (TYPE_FIELD_NAME (type, bitpos))
381 fprintf_filtered (stream, "%s ", TYPE_FIELD_NAME (type, bitpos));
382 else
383 fprintf_filtered (stream, "#%d ", bitpos);
384 }
385 }
386 fputs_filtered ("]", stream);
387}
388
c906108c
SS
389/* Print a number according to FORMAT which is one of d,u,x,o,b,h,w,g.
390 The raison d'etre of this function is to consolidate printing of
bb599908
PH
391 LONG_LONG's into this one function. The format chars b,h,w,g are
392 from print_scalar_formatted(). Numbers are printed using C
393 format.
394
395 USE_C_FORMAT means to use C format in all cases. Without it,
396 'o' and 'x' format do not include the standard C radix prefix
397 (leading 0 or 0x).
398
399 Hilfinger/2004-09-09: USE_C_FORMAT was originally called USE_LOCAL
400 and was intended to request formating according to the current
401 language and would be used for most integers that GDB prints. The
402 exceptional cases were things like protocols where the format of
403 the integer is a protocol thing, not a user-visible thing). The
404 parameter remains to preserve the information of what things might
405 be printed with language-specific format, should we ever resurrect
406 that capability. */
c906108c
SS
407
408void
bb599908 409print_longest (struct ui_file *stream, int format, int use_c_format,
fba45db2 410 LONGEST val_long)
c906108c 411{
2bfb72ee
AC
412 const char *val;
413
c906108c
SS
414 switch (format)
415 {
416 case 'd':
bb599908 417 val = int_string (val_long, 10, 1, 0, 1); break;
c906108c 418 case 'u':
bb599908 419 val = int_string (val_long, 10, 0, 0, 1); break;
c906108c 420 case 'x':
bb599908 421 val = int_string (val_long, 16, 0, 0, use_c_format); break;
c906108c 422 case 'b':
bb599908 423 val = int_string (val_long, 16, 0, 2, 1); break;
c906108c 424 case 'h':
bb599908 425 val = int_string (val_long, 16, 0, 4, 1); break;
c906108c 426 case 'w':
bb599908 427 val = int_string (val_long, 16, 0, 8, 1); break;
c906108c 428 case 'g':
bb599908 429 val = int_string (val_long, 16, 0, 16, 1); break;
c906108c
SS
430 break;
431 case 'o':
bb599908 432 val = int_string (val_long, 8, 0, 0, use_c_format); break;
c906108c 433 default:
e2e0b3e5 434 internal_error (__FILE__, __LINE__, _("failed internal consistency check"));
bb599908 435 }
2bfb72ee 436 fputs_filtered (val, stream);
c906108c
SS
437}
438
c906108c
SS
439/* This used to be a macro, but I don't think it is called often enough
440 to merit such treatment. */
441/* Convert a LONGEST to an int. This is used in contexts (e.g. number of
442 arguments to a function, number in a value history, register number, etc.)
443 where the value must not be larger than can fit in an int. */
444
445int
fba45db2 446longest_to_int (LONGEST arg)
c906108c
SS
447{
448 /* Let the compiler do the work */
449 int rtnval = (int) arg;
450
451 /* Check for overflows or underflows */
452 if (sizeof (LONGEST) > sizeof (int))
453 {
454 if (rtnval != arg)
455 {
8a3fe4f8 456 error (_("Value out of range."));
c906108c
SS
457 }
458 }
459 return (rtnval);
460}
461
a73c86fb
AC
462/* Print a floating point value of type TYPE (not always a
463 TYPE_CODE_FLT), pointed to in GDB by VALADDR, on STREAM. */
c906108c
SS
464
465void
fc1a4b47 466print_floating (const gdb_byte *valaddr, struct type *type,
c84141d6 467 struct ui_file *stream)
c906108c
SS
468{
469 DOUBLEST doub;
470 int inv;
a73c86fb 471 const struct floatformat *fmt = NULL;
c906108c 472 unsigned len = TYPE_LENGTH (type);
20389057 473 enum float_kind kind;
c5aa993b 474
a73c86fb
AC
475 /* If it is a floating-point, check for obvious problems. */
476 if (TYPE_CODE (type) == TYPE_CODE_FLT)
477 fmt = floatformat_from_type (type);
20389057 478 if (fmt != NULL)
39424bef 479 {
20389057
DJ
480 kind = floatformat_classify (fmt, valaddr);
481 if (kind == float_nan)
482 {
483 if (floatformat_is_negative (fmt, valaddr))
484 fprintf_filtered (stream, "-");
485 fprintf_filtered (stream, "nan(");
486 fputs_filtered ("0x", stream);
487 fputs_filtered (floatformat_mantissa (fmt, valaddr), stream);
488 fprintf_filtered (stream, ")");
489 return;
490 }
491 else if (kind == float_infinite)
492 {
493 if (floatformat_is_negative (fmt, valaddr))
494 fputs_filtered ("-", stream);
495 fputs_filtered ("inf", stream);
496 return;
497 }
7355ddba 498 }
c906108c 499
a73c86fb
AC
500 /* NOTE: cagney/2002-01-15: The TYPE passed into print_floating()
501 isn't necessarily a TYPE_CODE_FLT. Consequently, unpack_double
502 needs to be used as that takes care of any necessary type
503 conversions. Such conversions are of course direct to DOUBLEST
504 and disregard any possible target floating point limitations.
505 For instance, a u64 would be converted and displayed exactly on a
506 host with 80 bit DOUBLEST but with loss of information on a host
507 with 64 bit DOUBLEST. */
c2f05ac9 508
c906108c
SS
509 doub = unpack_double (type, valaddr, &inv);
510 if (inv)
511 {
512 fprintf_filtered (stream, "<invalid float value>");
513 return;
514 }
515
39424bef
MK
516 /* FIXME: kettenis/2001-01-20: The following code makes too much
517 assumptions about the host and target floating point format. */
518
a73c86fb 519 /* NOTE: cagney/2002-02-03: Since the TYPE of what was passed in may
c41b8590 520 not necessarily be a TYPE_CODE_FLT, the below ignores that and
a73c86fb
AC
521 instead uses the type's length to determine the precision of the
522 floating-point value being printed. */
c2f05ac9 523
c906108c 524 if (len < sizeof (double))
c5aa993b 525 fprintf_filtered (stream, "%.9g", (double) doub);
c906108c 526 else if (len == sizeof (double))
c5aa993b 527 fprintf_filtered (stream, "%.17g", (double) doub);
c906108c
SS
528 else
529#ifdef PRINTF_HAS_LONG_DOUBLE
530 fprintf_filtered (stream, "%.35Lg", doub);
531#else
39424bef
MK
532 /* This at least wins with values that are representable as
533 doubles. */
c906108c
SS
534 fprintf_filtered (stream, "%.17g", (double) doub);
535#endif
536}
537
7678ef8f
TJB
538void
539print_decimal_floating (const gdb_byte *valaddr, struct type *type,
540 struct ui_file *stream)
541{
542 char decstr[MAX_DECIMAL_STRING];
543 unsigned len = TYPE_LENGTH (type);
544
545 decimal_to_string (valaddr, len, decstr);
546 fputs_filtered (decstr, stream);
547 return;
548}
549
c5aa993b 550void
fc1a4b47 551print_binary_chars (struct ui_file *stream, const gdb_byte *valaddr,
d44e8473 552 unsigned len, enum bfd_endian byte_order)
c906108c
SS
553{
554
555#define BITS_IN_BYTES 8
556
fc1a4b47 557 const gdb_byte *p;
745b8ca0 558 unsigned int i;
c5aa993b 559 int b;
c906108c
SS
560
561 /* Declared "int" so it will be signed.
562 * This ensures that right shift will shift in zeros.
563 */
c5aa993b 564 const int mask = 0x080;
c906108c
SS
565
566 /* FIXME: We should be not printing leading zeroes in most cases. */
567
d44e8473 568 if (byte_order == BFD_ENDIAN_BIG)
c906108c
SS
569 {
570 for (p = valaddr;
571 p < valaddr + len;
572 p++)
573 {
c5aa993b
JM
574 /* Every byte has 8 binary characters; peel off
575 * and print from the MSB end.
576 */
577 for (i = 0; i < (BITS_IN_BYTES * sizeof (*p)); i++)
578 {
579 if (*p & (mask >> i))
580 b = 1;
581 else
582 b = 0;
583
584 fprintf_filtered (stream, "%1d", b);
585 }
c906108c
SS
586 }
587 }
588 else
589 {
590 for (p = valaddr + len - 1;
591 p >= valaddr;
592 p--)
593 {
c5aa993b
JM
594 for (i = 0; i < (BITS_IN_BYTES * sizeof (*p)); i++)
595 {
596 if (*p & (mask >> i))
597 b = 1;
598 else
599 b = 0;
600
601 fprintf_filtered (stream, "%1d", b);
602 }
c906108c
SS
603 }
604 }
c906108c
SS
605}
606
607/* VALADDR points to an integer of LEN bytes.
608 * Print it in octal on stream or format it in buf.
609 */
610void
fc1a4b47 611print_octal_chars (struct ui_file *stream, const gdb_byte *valaddr,
d44e8473 612 unsigned len, enum bfd_endian byte_order)
c906108c 613{
fc1a4b47 614 const gdb_byte *p;
c906108c 615 unsigned char octa1, octa2, octa3, carry;
c5aa993b
JM
616 int cycle;
617
c906108c
SS
618 /* FIXME: We should be not printing leading zeroes in most cases. */
619
620
621 /* Octal is 3 bits, which doesn't fit. Yuk. So we have to track
622 * the extra bits, which cycle every three bytes:
623 *
624 * Byte side: 0 1 2 3
625 * | | | |
626 * bit number 123 456 78 | 9 012 345 6 | 78 901 234 | 567 890 12 |
627 *
628 * Octal side: 0 1 carry 3 4 carry ...
629 *
630 * Cycle number: 0 1 2
631 *
632 * But of course we are printing from the high side, so we have to
633 * figure out where in the cycle we are so that we end up with no
634 * left over bits at the end.
635 */
636#define BITS_IN_OCTAL 3
637#define HIGH_ZERO 0340
638#define LOW_ZERO 0016
639#define CARRY_ZERO 0003
640#define HIGH_ONE 0200
641#define MID_ONE 0160
642#define LOW_ONE 0016
643#define CARRY_ONE 0001
644#define HIGH_TWO 0300
645#define MID_TWO 0070
646#define LOW_TWO 0007
647
648 /* For 32 we start in cycle 2, with two bits and one bit carry;
649 * for 64 in cycle in cycle 1, with one bit and a two bit carry.
650 */
651 cycle = (len * BITS_IN_BYTES) % BITS_IN_OCTAL;
652 carry = 0;
c5aa993b 653
bb599908 654 fputs_filtered ("0", stream);
d44e8473 655 if (byte_order == BFD_ENDIAN_BIG)
c906108c
SS
656 {
657 for (p = valaddr;
658 p < valaddr + len;
659 p++)
660 {
c5aa993b
JM
661 switch (cycle)
662 {
663 case 0:
664 /* No carry in, carry out two bits.
665 */
666 octa1 = (HIGH_ZERO & *p) >> 5;
667 octa2 = (LOW_ZERO & *p) >> 2;
668 carry = (CARRY_ZERO & *p);
669 fprintf_filtered (stream, "%o", octa1);
670 fprintf_filtered (stream, "%o", octa2);
671 break;
672
673 case 1:
674 /* Carry in two bits, carry out one bit.
675 */
676 octa1 = (carry << 1) | ((HIGH_ONE & *p) >> 7);
677 octa2 = (MID_ONE & *p) >> 4;
678 octa3 = (LOW_ONE & *p) >> 1;
679 carry = (CARRY_ONE & *p);
680 fprintf_filtered (stream, "%o", octa1);
681 fprintf_filtered (stream, "%o", octa2);
682 fprintf_filtered (stream, "%o", octa3);
683 break;
684
685 case 2:
686 /* Carry in one bit, no carry out.
687 */
688 octa1 = (carry << 2) | ((HIGH_TWO & *p) >> 6);
689 octa2 = (MID_TWO & *p) >> 3;
690 octa3 = (LOW_TWO & *p);
691 carry = 0;
692 fprintf_filtered (stream, "%o", octa1);
693 fprintf_filtered (stream, "%o", octa2);
694 fprintf_filtered (stream, "%o", octa3);
695 break;
696
697 default:
8a3fe4f8 698 error (_("Internal error in octal conversion;"));
c5aa993b
JM
699 }
700
701 cycle++;
702 cycle = cycle % BITS_IN_OCTAL;
c906108c
SS
703 }
704 }
705 else
706 {
707 for (p = valaddr + len - 1;
708 p >= valaddr;
709 p--)
710 {
c5aa993b
JM
711 switch (cycle)
712 {
713 case 0:
714 /* Carry out, no carry in */
715 octa1 = (HIGH_ZERO & *p) >> 5;
716 octa2 = (LOW_ZERO & *p) >> 2;
717 carry = (CARRY_ZERO & *p);
718 fprintf_filtered (stream, "%o", octa1);
719 fprintf_filtered (stream, "%o", octa2);
720 break;
721
722 case 1:
723 /* Carry in, carry out */
724 octa1 = (carry << 1) | ((HIGH_ONE & *p) >> 7);
725 octa2 = (MID_ONE & *p) >> 4;
726 octa3 = (LOW_ONE & *p) >> 1;
727 carry = (CARRY_ONE & *p);
728 fprintf_filtered (stream, "%o", octa1);
729 fprintf_filtered (stream, "%o", octa2);
730 fprintf_filtered (stream, "%o", octa3);
731 break;
732
733 case 2:
734 /* Carry in, no carry out */
735 octa1 = (carry << 2) | ((HIGH_TWO & *p) >> 6);
736 octa2 = (MID_TWO & *p) >> 3;
737 octa3 = (LOW_TWO & *p);
738 carry = 0;
739 fprintf_filtered (stream, "%o", octa1);
740 fprintf_filtered (stream, "%o", octa2);
741 fprintf_filtered (stream, "%o", octa3);
742 break;
743
744 default:
8a3fe4f8 745 error (_("Internal error in octal conversion;"));
c5aa993b
JM
746 }
747
748 cycle++;
749 cycle = cycle % BITS_IN_OCTAL;
c906108c
SS
750 }
751 }
752
c906108c
SS
753}
754
755/* VALADDR points to an integer of LEN bytes.
756 * Print it in decimal on stream or format it in buf.
757 */
758void
fc1a4b47 759print_decimal_chars (struct ui_file *stream, const gdb_byte *valaddr,
d44e8473 760 unsigned len, enum bfd_endian byte_order)
c906108c
SS
761{
762#define TEN 10
c5aa993b 763#define CARRY_OUT( x ) ((x) / TEN) /* extend char to int */
c906108c
SS
764#define CARRY_LEFT( x ) ((x) % TEN)
765#define SHIFT( x ) ((x) << 4)
c906108c
SS
766#define LOW_NIBBLE( x ) ( (x) & 0x00F)
767#define HIGH_NIBBLE( x ) (((x) & 0x0F0) >> 4)
768
fc1a4b47 769 const gdb_byte *p;
c906108c 770 unsigned char *digits;
c5aa993b
JM
771 int carry;
772 int decimal_len;
773 int i, j, decimal_digits;
774 int dummy;
775 int flip;
776
c906108c
SS
777 /* Base-ten number is less than twice as many digits
778 * as the base 16 number, which is 2 digits per byte.
779 */
780 decimal_len = len * 2 * 2;
3c37485b 781 digits = xmalloc (decimal_len);
c906108c 782
c5aa993b
JM
783 for (i = 0; i < decimal_len; i++)
784 {
c906108c 785 digits[i] = 0;
c5aa993b 786 }
c906108c 787
c906108c
SS
788 /* Ok, we have an unknown number of bytes of data to be printed in
789 * decimal.
790 *
791 * Given a hex number (in nibbles) as XYZ, we start by taking X and
792 * decemalizing it as "x1 x2" in two decimal nibbles. Then we multiply
793 * the nibbles by 16, add Y and re-decimalize. Repeat with Z.
794 *
795 * The trick is that "digits" holds a base-10 number, but sometimes
796 * the individual digits are > 10.
797 *
798 * Outer loop is per nibble (hex digit) of input, from MSD end to
799 * LSD end.
800 */
c5aa993b 801 decimal_digits = 0; /* Number of decimal digits so far */
d44e8473 802 p = (byte_order == BFD_ENDIAN_BIG) ? valaddr : valaddr + len - 1;
c906108c 803 flip = 0;
d44e8473 804 while ((byte_order == BFD_ENDIAN_BIG) ? (p < valaddr + len) : (p >= valaddr))
c5aa993b 805 {
c906108c
SS
806 /*
807 * Multiply current base-ten number by 16 in place.
808 * Each digit was between 0 and 9, now is between
809 * 0 and 144.
810 */
c5aa993b
JM
811 for (j = 0; j < decimal_digits; j++)
812 {
813 digits[j] = SHIFT (digits[j]);
814 }
815
c906108c
SS
816 /* Take the next nibble off the input and add it to what
817 * we've got in the LSB position. Bottom 'digit' is now
818 * between 0 and 159.
819 *
820 * "flip" is used to run this loop twice for each byte.
821 */
c5aa993b
JM
822 if (flip == 0)
823 {
824 /* Take top nibble.
825 */
826 digits[0] += HIGH_NIBBLE (*p);
827 flip = 1;
828 }
829 else
830 {
831 /* Take low nibble and bump our pointer "p".
832 */
833 digits[0] += LOW_NIBBLE (*p);
d44e8473
MD
834 if (byte_order == BFD_ENDIAN_BIG)
835 p++;
836 else
837 p--;
c5aa993b
JM
838 flip = 0;
839 }
c906108c
SS
840
841 /* Re-decimalize. We have to do this often enough
842 * that we don't overflow, but once per nibble is
843 * overkill. Easier this way, though. Note that the
844 * carry is often larger than 10 (e.g. max initial
845 * carry out of lowest nibble is 15, could bubble all
846 * the way up greater than 10). So we have to do
847 * the carrying beyond the last current digit.
848 */
849 carry = 0;
c5aa993b
JM
850 for (j = 0; j < decimal_len - 1; j++)
851 {
852 digits[j] += carry;
853
854 /* "/" won't handle an unsigned char with
855 * a value that if signed would be negative.
856 * So extend to longword int via "dummy".
857 */
858 dummy = digits[j];
859 carry = CARRY_OUT (dummy);
860 digits[j] = CARRY_LEFT (dummy);
861
862 if (j >= decimal_digits && carry == 0)
863 {
864 /*
865 * All higher digits are 0 and we
866 * no longer have a carry.
867 *
868 * Note: "j" is 0-based, "decimal_digits" is
869 * 1-based.
870 */
871 decimal_digits = j + 1;
872 break;
873 }
874 }
875 }
c906108c
SS
876
877 /* Ok, now "digits" is the decimal representation, with
878 * the "decimal_digits" actual digits. Print!
879 */
c5aa993b
JM
880 for (i = decimal_digits - 1; i >= 0; i--)
881 {
882 fprintf_filtered (stream, "%1d", digits[i]);
883 }
b8c9b27d 884 xfree (digits);
c906108c
SS
885}
886
887/* VALADDR points to an integer of LEN bytes. Print it in hex on stream. */
888
6b9acc27 889void
fc1a4b47 890print_hex_chars (struct ui_file *stream, const gdb_byte *valaddr,
d44e8473 891 unsigned len, enum bfd_endian byte_order)
c906108c 892{
fc1a4b47 893 const gdb_byte *p;
c906108c
SS
894
895 /* FIXME: We should be not printing leading zeroes in most cases. */
896
bb599908 897 fputs_filtered ("0x", stream);
d44e8473 898 if (byte_order == BFD_ENDIAN_BIG)
c906108c
SS
899 {
900 for (p = valaddr;
901 p < valaddr + len;
902 p++)
903 {
904 fprintf_filtered (stream, "%02x", *p);
905 }
906 }
907 else
908 {
909 for (p = valaddr + len - 1;
910 p >= valaddr;
911 p--)
912 {
913 fprintf_filtered (stream, "%02x", *p);
914 }
915 }
c906108c
SS
916}
917
6b9acc27
JJ
918/* VALADDR points to a char integer of LEN bytes. Print it out in appropriate language form on stream.
919 Omit any leading zero chars. */
920
921void
fc1a4b47 922print_char_chars (struct ui_file *stream, const gdb_byte *valaddr,
d44e8473 923 unsigned len, enum bfd_endian byte_order)
6b9acc27 924{
fc1a4b47 925 const gdb_byte *p;
6b9acc27 926
d44e8473 927 if (byte_order == BFD_ENDIAN_BIG)
6b9acc27
JJ
928 {
929 p = valaddr;
930 while (p < valaddr + len - 1 && *p == 0)
931 ++p;
932
933 while (p < valaddr + len)
934 {
935 LA_EMIT_CHAR (*p, stream, '\'');
936 ++p;
937 }
938 }
939 else
940 {
941 p = valaddr + len - 1;
942 while (p > valaddr && *p == 0)
943 --p;
944
945 while (p >= valaddr)
946 {
947 LA_EMIT_CHAR (*p, stream, '\'');
948 --p;
949 }
950 }
951}
952
e936309c
JB
953/* Assuming TYPE is a simple, non-empty array type, compute its upper
954 and lower bound. Save the low bound into LOW_BOUND if not NULL.
955 Save the high bound into HIGH_BOUND if not NULL.
e79af960
JB
956
957 Return 1 if the operation was successful. Return zero otherwise,
e936309c 958 in which case the values of LOW_BOUND and HIGH_BOUNDS are unmodified.
e79af960 959
e936309c
JB
960 Computing the array upper and lower bounds is pretty easy, but this
961 function does some additional verifications before returning them.
e79af960
JB
962 If something incorrect is detected, it is better to return a status
963 rather than throwing an error, making it easier for the caller to
964 implement an error-recovery plan. For instance, it may decide to
e936309c
JB
965 warn the user that the bounds were not found and then use some
966 default values instead. */
e79af960
JB
967
968int
e936309c 969get_array_bounds (struct type *type, long *low_bound, long *high_bound)
e79af960
JB
970{
971 struct type *index = TYPE_INDEX_TYPE (type);
972 long low = 0;
e936309c 973 long high = 0;
e79af960
JB
974
975 if (index == NULL)
976 return 0;
977
e936309c
JB
978 if (TYPE_CODE (index) == TYPE_CODE_RANGE)
979 {
980 low = TYPE_LOW_BOUND (index);
981 high = TYPE_HIGH_BOUND (index);
982 }
983 else if (TYPE_CODE (index) == TYPE_CODE_ENUM)
984 {
985 const int n_enums = TYPE_NFIELDS (index);
986
987 low = TYPE_FIELD_BITPOS (index, 0);
988 high = TYPE_FIELD_BITPOS (index, n_enums - 1);
989 }
990 else
e79af960
JB
991 return 0;
992
e936309c
JB
993 /* Abort if the lower bound is greater than the higher bound, except
994 when low = high + 1. This is a very common idiom used in Ada when
995 defining empty ranges (for instance "range 1 .. 0"). */
996 if (low > high + 1)
e79af960
JB
997 return 0;
998
999 if (low_bound)
1000 *low_bound = low;
1001
e936309c
JB
1002 if (high_bound)
1003 *high_bound = high;
1004
e79af960
JB
1005 return 1;
1006}
e936309c 1007
79a45b7d 1008/* Print on STREAM using the given OPTIONS the index for the element
e79af960
JB
1009 at INDEX of an array whose index type is INDEX_TYPE. */
1010
1011void
1012maybe_print_array_index (struct type *index_type, LONGEST index,
79a45b7d
TT
1013 struct ui_file *stream,
1014 const struct value_print_options *options)
e79af960
JB
1015{
1016 struct value *index_value;
1017
79a45b7d 1018 if (!options->print_array_indexes)
e79af960
JB
1019 return;
1020
1021 index_value = value_from_longest (index_type, index);
1022
79a45b7d
TT
1023 LA_PRINT_ARRAY_INDEX (index_value, stream, options);
1024}
e79af960 1025
c906108c 1026/* Called by various <lang>_val_print routines to print elements of an
c5aa993b 1027 array in the form "<elem1>, <elem2>, <elem3>, ...".
c906108c 1028
c5aa993b
JM
1029 (FIXME?) Assumes array element separator is a comma, which is correct
1030 for all languages currently handled.
1031 (FIXME?) Some languages have a notation for repeated array elements,
1032 perhaps we should try to use that notation when appropriate.
1033 */
c906108c
SS
1034
1035void
fc1a4b47 1036val_print_array_elements (struct type *type, const gdb_byte *valaddr,
a2bd3dcd 1037 CORE_ADDR address, struct ui_file *stream,
79a45b7d
TT
1038 int recurse,
1039 const struct value_print_options *options,
fba45db2 1040 unsigned int i)
c906108c
SS
1041{
1042 unsigned int things_printed = 0;
1043 unsigned len;
e79af960 1044 struct type *elttype, *index_type;
c906108c
SS
1045 unsigned eltlen;
1046 /* Position of the array element we are examining to see
1047 whether it is repeated. */
1048 unsigned int rep1;
1049 /* Number of repetitions we have detected so far. */
1050 unsigned int reps;
168de233 1051 long low_bound_index = 0;
c5aa993b 1052
c906108c
SS
1053 elttype = TYPE_TARGET_TYPE (type);
1054 eltlen = TYPE_LENGTH (check_typedef (elttype));
e79af960 1055 index_type = TYPE_INDEX_TYPE (type);
c906108c 1056
e936309c
JB
1057 /* Compute the number of elements in the array. On most arrays,
1058 the size of its elements is not zero, and so the number of elements
1059 is simply the size of the array divided by the size of the elements.
1060 But for arrays of elements whose size is zero, we need to look at
1061 the bounds. */
1062 if (eltlen != 0)
1063 len = TYPE_LENGTH (type) / eltlen;
1064 else
1065 {
1066 long low, hi;
1067 if (get_array_bounds (type, &low, &hi))
1068 len = hi - low + 1;
1069 else
1070 {
1071 warning (_("unable to get bounds of array, assuming null array"));
1072 len = 0;
1073 }
1074 }
1075
168de233
JB
1076 /* Get the array low bound. This only makes sense if the array
1077 has one or more element in it. */
e936309c 1078 if (len > 0 && !get_array_bounds (type, &low_bound_index, NULL))
168de233 1079 {
e936309c 1080 warning (_("unable to get low bound of array, using zero as default"));
168de233
JB
1081 low_bound_index = 0;
1082 }
1083
c906108c
SS
1084 annotate_array_section_begin (i, elttype);
1085
79a45b7d 1086 for (; i < len && things_printed < options->print_max; i++)
c906108c
SS
1087 {
1088 if (i != 0)
1089 {
79a45b7d 1090 if (options->prettyprint_arrays)
c906108c
SS
1091 {
1092 fprintf_filtered (stream, ",\n");
1093 print_spaces_filtered (2 + 2 * recurse, stream);
1094 }
1095 else
1096 {
1097 fprintf_filtered (stream, ", ");
1098 }
1099 }
1100 wrap_here (n_spaces (2 + 2 * recurse));
e79af960 1101 maybe_print_array_index (index_type, i + low_bound_index,
79a45b7d 1102 stream, options);
c906108c
SS
1103
1104 rep1 = i + 1;
1105 reps = 1;
c5aa993b 1106 while ((rep1 < len) &&
c906108c
SS
1107 !memcmp (valaddr + i * eltlen, valaddr + rep1 * eltlen, eltlen))
1108 {
1109 ++reps;
1110 ++rep1;
1111 }
1112
79a45b7d 1113 if (reps > options->repeat_count_threshold)
c906108c 1114 {
f9e31323
TT
1115 val_print (elttype, valaddr + i * eltlen, 0, address + i * eltlen,
1116 stream, recurse + 1, options, current_language);
c906108c
SS
1117 annotate_elt_rep (reps);
1118 fprintf_filtered (stream, " <repeats %u times>", reps);
1119 annotate_elt_rep_end ();
1120
1121 i = rep1 - 1;
79a45b7d 1122 things_printed += options->repeat_count_threshold;
c906108c
SS
1123 }
1124 else
1125 {
f9e31323
TT
1126 val_print (elttype, valaddr + i * eltlen, 0, address + i * eltlen,
1127 stream, recurse + 1, options, current_language);
c906108c
SS
1128 annotate_elt ();
1129 things_printed++;
1130 }
1131 }
1132 annotate_array_section_end ();
1133 if (i < len)
1134 {
1135 fprintf_filtered (stream, "...");
1136 }
1137}
1138
917317f4
JM
1139/* Read LEN bytes of target memory at address MEMADDR, placing the
1140 results in GDB's memory at MYADDR. Returns a count of the bytes
1141 actually read, and optionally an errno value in the location
1142 pointed to by ERRNOPTR if ERRNOPTR is non-null. */
1143
1144/* FIXME: cagney/1999-10-14: Only used by val_print_string. Can this
1145 function be eliminated. */
1146
1147static int
777ea8f1 1148partial_memory_read (CORE_ADDR memaddr, gdb_byte *myaddr, int len, int *errnoptr)
917317f4
JM
1149{
1150 int nread; /* Number of bytes actually read. */
1151 int errcode; /* Error from last read. */
1152
1153 /* First try a complete read. */
1154 errcode = target_read_memory (memaddr, myaddr, len);
1155 if (errcode == 0)
1156 {
1157 /* Got it all. */
1158 nread = len;
1159 }
1160 else
1161 {
1162 /* Loop, reading one byte at a time until we get as much as we can. */
1163 for (errcode = 0, nread = 0; len > 0 && errcode == 0; nread++, len--)
1164 {
1165 errcode = target_read_memory (memaddr++, myaddr++, 1);
1166 }
1167 /* If an error, the last read was unsuccessful, so adjust count. */
1168 if (errcode != 0)
1169 {
1170 nread--;
1171 }
1172 }
1173 if (errnoptr != NULL)
1174 {
1175 *errnoptr = errcode;
1176 }
1177 return (nread);
1178}
1179
c906108c 1180/* Print a string from the inferior, starting at ADDR and printing up to LEN
c5aa993b
JM
1181 characters, of WIDTH bytes a piece, to STREAM. If LEN is -1, printing
1182 stops at the first null byte, otherwise printing proceeds (including null
1183 bytes) until either print_max or LEN characters have been printed,
1184 whichever is smaller. */
c906108c
SS
1185
1186/* FIXME: Use target_read_string. */
1187
1188int
79a45b7d
TT
1189val_print_string (CORE_ADDR addr, int len, int width, struct ui_file *stream,
1190 const struct value_print_options *options)
c906108c
SS
1191{
1192 int force_ellipsis = 0; /* Force ellipsis to be printed if nonzero. */
1193 int errcode; /* Errno returned from bad reads. */
1194 unsigned int fetchlimit; /* Maximum number of chars to print. */
1195 unsigned int nfetch; /* Chars to fetch / chars fetched. */
1196 unsigned int chunksize; /* Size of each fetch, in chars. */
777ea8f1
DJ
1197 gdb_byte *buffer = NULL; /* Dynamically growable fetch buffer. */
1198 gdb_byte *bufptr; /* Pointer to next available byte in buffer. */
1199 gdb_byte *limit; /* First location past end of fetch buffer. */
c5aa993b 1200 struct cleanup *old_chain = NULL; /* Top of the old cleanup chain. */
c906108c
SS
1201 int found_nul; /* Non-zero if we found the nul char */
1202
1203 /* First we need to figure out the limit on the number of characters we are
1204 going to attempt to fetch and print. This is actually pretty simple. If
1205 LEN >= zero, then the limit is the minimum of LEN and print_max. If
1206 LEN is -1, then the limit is print_max. This is true regardless of
1207 whether print_max is zero, UINT_MAX (unlimited), or something in between,
1208 because finding the null byte (or available memory) is what actually
1209 limits the fetch. */
1210
79a45b7d 1211 fetchlimit = (len == -1 ? options->print_max : min (len, options->print_max));
c906108c
SS
1212
1213 /* Now decide how large of chunks to try to read in one operation. This
1214 is also pretty simple. If LEN >= zero, then we want fetchlimit chars,
1215 so we might as well read them all in one operation. If LEN is -1, we
1216 are looking for a null terminator to end the fetching, so we might as
1217 well read in blocks that are large enough to be efficient, but not so
1218 large as to be slow if fetchlimit happens to be large. So we choose the
1219 minimum of 8 and fetchlimit. We used to use 200 instead of 8 but
1220 200 is way too big for remote debugging over a serial line. */
1221
1222 chunksize = (len == -1 ? min (8, fetchlimit) : fetchlimit);
1223
1224 /* Loop until we either have all the characters to print, or we encounter
1225 some error, such as bumping into the end of the address space. */
1226
1227 found_nul = 0;
1228 old_chain = make_cleanup (null_cleanup, 0);
1229
1230 if (len > 0)
1231 {
777ea8f1 1232 buffer = (gdb_byte *) xmalloc (len * width);
c906108c 1233 bufptr = buffer;
b8c9b27d 1234 old_chain = make_cleanup (xfree, buffer);
c906108c 1235
917317f4 1236 nfetch = partial_memory_read (addr, bufptr, len * width, &errcode)
c906108c
SS
1237 / width;
1238 addr += nfetch * width;
1239 bufptr += nfetch * width;
1240 }
1241 else if (len == -1)
1242 {
1243 unsigned long bufsize = 0;
1244 do
1245 {
1246 QUIT;
1247 nfetch = min (chunksize, fetchlimit - bufsize);
1248
1249 if (buffer == NULL)
777ea8f1 1250 buffer = (gdb_byte *) xmalloc (nfetch * width);
c906108c
SS
1251 else
1252 {
1253 discard_cleanups (old_chain);
777ea8f1 1254 buffer = (gdb_byte *) xrealloc (buffer, (nfetch + bufsize) * width);
c906108c
SS
1255 }
1256
b8c9b27d 1257 old_chain = make_cleanup (xfree, buffer);
c906108c
SS
1258 bufptr = buffer + bufsize * width;
1259 bufsize += nfetch;
1260
1261 /* Read as much as we can. */
917317f4 1262 nfetch = partial_memory_read (addr, bufptr, nfetch * width, &errcode)
c5aa993b 1263 / width;
c906108c
SS
1264
1265 /* Scan this chunk for the null byte that terminates the string
1266 to print. If found, we don't need to fetch any more. Note
1267 that bufptr is explicitly left pointing at the next character
1268 after the null byte, or at the next character after the end of
1269 the buffer. */
1270
1271 limit = bufptr + nfetch * width;
1272 while (bufptr < limit)
1273 {
1274 unsigned long c;
1275
1276 c = extract_unsigned_integer (bufptr, width);
1277 addr += width;
1278 bufptr += width;
1279 if (c == 0)
1280 {
1281 /* We don't care about any error which happened after
1282 the NULL terminator. */
1283 errcode = 0;
1284 found_nul = 1;
1285 break;
1286 }
1287 }
1288 }
c5aa993b
JM
1289 while (errcode == 0 /* no error */
1290 && bufptr - buffer < fetchlimit * width /* no overrun */
1291 && !found_nul); /* haven't found nul yet */
c906108c
SS
1292 }
1293 else
1294 { /* length of string is really 0! */
1295 buffer = bufptr = NULL;
1296 errcode = 0;
1297 }
1298
1299 /* bufptr and addr now point immediately beyond the last byte which we
1300 consider part of the string (including a '\0' which ends the string). */
1301
1302 /* We now have either successfully filled the buffer to fetchlimit, or
1303 terminated early due to an error or finding a null char when LEN is -1. */
1304
1305 if (len == -1 && !found_nul)
1306 {
777ea8f1 1307 gdb_byte *peekbuf;
c906108c
SS
1308
1309 /* We didn't find a null terminator we were looking for. Attempt
c5aa993b
JM
1310 to peek at the next character. If not successful, or it is not
1311 a null byte, then force ellipsis to be printed. */
c906108c 1312
777ea8f1 1313 peekbuf = (gdb_byte *) alloca (width);
c906108c
SS
1314
1315 if (target_read_memory (addr, peekbuf, width) == 0
1316 && extract_unsigned_integer (peekbuf, width) != 0)
1317 force_ellipsis = 1;
1318 }
c5aa993b 1319 else if ((len >= 0 && errcode != 0) || (len > (bufptr - buffer) / width))
c906108c
SS
1320 {
1321 /* Getting an error when we have a requested length, or fetching less
c5aa993b
JM
1322 than the number of characters actually requested, always make us
1323 print ellipsis. */
c906108c
SS
1324 force_ellipsis = 1;
1325 }
1326
1327 QUIT;
1328
1329 /* If we get an error before fetching anything, don't print a string.
1330 But if we fetch something and then get an error, print the string
1331 and then the error message. */
1332 if (errcode == 0 || bufptr > buffer)
1333 {
79a45b7d 1334 if (options->addressprint)
c906108c
SS
1335 {
1336 fputs_filtered (" ", stream);
1337 }
79a45b7d 1338 LA_PRINT_STRING (stream, buffer, (bufptr - buffer) / width, width, force_ellipsis, options);
c906108c
SS
1339 }
1340
1341 if (errcode != 0)
1342 {
1343 if (errcode == EIO)
1344 {
1345 fprintf_filtered (stream, " <Address ");
ed49a04f 1346 fputs_filtered (paddress (addr), stream);
c906108c
SS
1347 fprintf_filtered (stream, " out of bounds>");
1348 }
1349 else
1350 {
1351 fprintf_filtered (stream, " <Error reading address ");
ed49a04f 1352 fputs_filtered (paddress (addr), stream);
c906108c
SS
1353 fprintf_filtered (stream, ": %s>", safe_strerror (errcode));
1354 }
1355 }
1356 gdb_flush (stream);
1357 do_cleanups (old_chain);
c5aa993b 1358 return ((bufptr - buffer) / width);
c906108c 1359}
c906108c 1360\f
c5aa993b 1361
c906108c
SS
1362/* Validate an input or output radix setting, and make sure the user
1363 knows what they really did here. Radix setting is confusing, e.g.
1364 setting the input radix to "10" never changes it! */
1365
c906108c 1366static void
fba45db2 1367set_input_radix (char *args, int from_tty, struct cmd_list_element *c)
c906108c 1368{
f66c9f11 1369 set_input_radix_1 (from_tty, input_radix);
c906108c
SS
1370}
1371
c906108c 1372static void
fba45db2 1373set_input_radix_1 (int from_tty, unsigned radix)
c906108c
SS
1374{
1375 /* We don't currently disallow any input radix except 0 or 1, which don't
1376 make any mathematical sense. In theory, we can deal with any input
1377 radix greater than 1, even if we don't have unique digits for every
1378 value from 0 to radix-1, but in practice we lose on large radix values.
1379 We should either fix the lossage or restrict the radix range more.
1380 (FIXME). */
1381
1382 if (radix < 2)
1383 {
f66c9f11
AC
1384 /* FIXME: cagney/2002-03-17: This needs to revert the bad radix
1385 value. */
8a3fe4f8 1386 error (_("Nonsense input radix ``decimal %u''; input radix unchanged."),
c906108c
SS
1387 radix);
1388 }
1389 input_radix = radix;
1390 if (from_tty)
1391 {
a3f17187 1392 printf_filtered (_("Input radix now set to decimal %u, hex %x, octal %o.\n"),
c906108c
SS
1393 radix, radix, radix);
1394 }
1395}
1396
c906108c 1397static void
fba45db2 1398set_output_radix (char *args, int from_tty, struct cmd_list_element *c)
c906108c 1399{
f66c9f11 1400 set_output_radix_1 (from_tty, output_radix);
c906108c
SS
1401}
1402
1403static void
fba45db2 1404set_output_radix_1 (int from_tty, unsigned radix)
c906108c
SS
1405{
1406 /* Validate the radix and disallow ones that we aren't prepared to
1407 handle correctly, leaving the radix unchanged. */
1408 switch (radix)
1409 {
1410 case 16:
79a45b7d 1411 user_print_options.output_format = 'x'; /* hex */
c906108c
SS
1412 break;
1413 case 10:
79a45b7d 1414 user_print_options.output_format = 0; /* decimal */
c906108c
SS
1415 break;
1416 case 8:
79a45b7d 1417 user_print_options.output_format = 'o'; /* octal */
c906108c
SS
1418 break;
1419 default:
f66c9f11
AC
1420 /* FIXME: cagney/2002-03-17: This needs to revert the bad radix
1421 value. */
8a3fe4f8 1422 error (_("Unsupported output radix ``decimal %u''; output radix unchanged."),
c906108c
SS
1423 radix);
1424 }
1425 output_radix = radix;
1426 if (from_tty)
1427 {
a3f17187 1428 printf_filtered (_("Output radix now set to decimal %u, hex %x, octal %o.\n"),
c906108c
SS
1429 radix, radix, radix);
1430 }
1431}
1432
1433/* Set both the input and output radix at once. Try to set the output radix
1434 first, since it has the most restrictive range. An radix that is valid as
1435 an output radix is also valid as an input radix.
1436
1437 It may be useful to have an unusual input radix. If the user wishes to
1438 set an input radix that is not valid as an output radix, he needs to use
1439 the 'set input-radix' command. */
1440
1441static void
fba45db2 1442set_radix (char *arg, int from_tty)
c906108c
SS
1443{
1444 unsigned radix;
1445
bb518678 1446 radix = (arg == NULL) ? 10 : parse_and_eval_long (arg);
c906108c
SS
1447 set_output_radix_1 (0, radix);
1448 set_input_radix_1 (0, radix);
1449 if (from_tty)
1450 {
a3f17187 1451 printf_filtered (_("Input and output radices now set to decimal %u, hex %x, octal %o.\n"),
c906108c
SS
1452 radix, radix, radix);
1453 }
1454}
1455
1456/* Show both the input and output radices. */
1457
c906108c 1458static void
fba45db2 1459show_radix (char *arg, int from_tty)
c906108c
SS
1460{
1461 if (from_tty)
1462 {
1463 if (input_radix == output_radix)
1464 {
a3f17187 1465 printf_filtered (_("Input and output radices set to decimal %u, hex %x, octal %o.\n"),
c906108c
SS
1466 input_radix, input_radix, input_radix);
1467 }
1468 else
1469 {
a3f17187 1470 printf_filtered (_("Input radix set to decimal %u, hex %x, octal %o.\n"),
c906108c 1471 input_radix, input_radix, input_radix);
a3f17187 1472 printf_filtered (_("Output radix set to decimal %u, hex %x, octal %o.\n"),
c906108c
SS
1473 output_radix, output_radix, output_radix);
1474 }
1475 }
1476}
c906108c 1477\f
c5aa993b 1478
c906108c 1479static void
fba45db2 1480set_print (char *arg, int from_tty)
c906108c
SS
1481{
1482 printf_unfiltered (
c5aa993b 1483 "\"set print\" must be followed by the name of a print subcommand.\n");
c906108c
SS
1484 help_list (setprintlist, "set print ", -1, gdb_stdout);
1485}
1486
c906108c 1487static void
fba45db2 1488show_print (char *args, int from_tty)
c906108c
SS
1489{
1490 cmd_show_list (showprintlist, from_tty, "");
1491}
1492\f
1493void
fba45db2 1494_initialize_valprint (void)
c906108c
SS
1495{
1496 struct cmd_list_element *c;
1497
1498 add_prefix_cmd ("print", no_class, set_print,
1bedd215 1499 _("Generic command for setting how things print."),
c906108c 1500 &setprintlist, "set print ", 0, &setlist);
c5aa993b
JM
1501 add_alias_cmd ("p", "print", no_class, 1, &setlist);
1502 /* prefer set print to set prompt */
c906108c
SS
1503 add_alias_cmd ("pr", "print", no_class, 1, &setlist);
1504
1505 add_prefix_cmd ("print", no_class, show_print,
1bedd215 1506 _("Generic command for showing print settings."),
c906108c 1507 &showprintlist, "show print ", 0, &showlist);
c5aa993b
JM
1508 add_alias_cmd ("p", "print", no_class, 1, &showlist);
1509 add_alias_cmd ("pr", "print", no_class, 1, &showlist);
c906108c 1510
79a45b7d
TT
1511 add_setshow_uinteger_cmd ("elements", no_class,
1512 &user_print_options.print_max, _("\
35096d9d
AC
1513Set limit on string chars or array elements to print."), _("\
1514Show limit on string chars or array elements to print."), _("\
1515\"set print elements 0\" causes there to be no limit."),
1516 NULL,
920d2a44 1517 show_print_max,
35096d9d 1518 &setprintlist, &showprintlist);
c906108c 1519
79a45b7d
TT
1520 add_setshow_boolean_cmd ("null-stop", no_class,
1521 &user_print_options.stop_print_at_null, _("\
5bf193a2
AC
1522Set printing of char arrays to stop at first null char."), _("\
1523Show printing of char arrays to stop at first null char."), NULL,
1524 NULL,
920d2a44 1525 show_stop_print_at_null,
5bf193a2 1526 &setprintlist, &showprintlist);
c906108c 1527
35096d9d 1528 add_setshow_uinteger_cmd ("repeats", no_class,
79a45b7d 1529 &user_print_options.repeat_count_threshold, _("\
35096d9d
AC
1530Set threshold for repeated print elements."), _("\
1531Show threshold for repeated print elements."), _("\
1532\"set print repeats 0\" causes all elements to be individually printed."),
1533 NULL,
920d2a44 1534 show_repeat_count_threshold,
35096d9d 1535 &setprintlist, &showprintlist);
c906108c 1536
79a45b7d
TT
1537 add_setshow_boolean_cmd ("pretty", class_support,
1538 &user_print_options.prettyprint_structs, _("\
5bf193a2
AC
1539Set prettyprinting of structures."), _("\
1540Show prettyprinting of structures."), NULL,
1541 NULL,
920d2a44 1542 show_prettyprint_structs,
5bf193a2
AC
1543 &setprintlist, &showprintlist);
1544
79a45b7d
TT
1545 add_setshow_boolean_cmd ("union", class_support,
1546 &user_print_options.unionprint, _("\
5bf193a2
AC
1547Set printing of unions interior to structures."), _("\
1548Show printing of unions interior to structures."), NULL,
1549 NULL,
920d2a44 1550 show_unionprint,
5bf193a2
AC
1551 &setprintlist, &showprintlist);
1552
79a45b7d
TT
1553 add_setshow_boolean_cmd ("array", class_support,
1554 &user_print_options.prettyprint_arrays, _("\
5bf193a2
AC
1555Set prettyprinting of arrays."), _("\
1556Show prettyprinting of arrays."), NULL,
1557 NULL,
920d2a44 1558 show_prettyprint_arrays,
5bf193a2
AC
1559 &setprintlist, &showprintlist);
1560
79a45b7d
TT
1561 add_setshow_boolean_cmd ("address", class_support,
1562 &user_print_options.addressprint, _("\
5bf193a2
AC
1563Set printing of addresses."), _("\
1564Show printing of addresses."), NULL,
1565 NULL,
920d2a44 1566 show_addressprint,
5bf193a2 1567 &setprintlist, &showprintlist);
c906108c 1568
35096d9d
AC
1569 add_setshow_uinteger_cmd ("input-radix", class_support, &input_radix, _("\
1570Set default input radix for entering numbers."), _("\
1571Show default input radix for entering numbers."), NULL,
1572 set_input_radix,
920d2a44 1573 show_input_radix,
35096d9d
AC
1574 &setlist, &showlist);
1575
1576 add_setshow_uinteger_cmd ("output-radix", class_support, &output_radix, _("\
1577Set default output radix for printing of values."), _("\
1578Show default output radix for printing of values."), NULL,
1579 set_output_radix,
920d2a44 1580 show_output_radix,
35096d9d 1581 &setlist, &showlist);
c906108c 1582
cb1a6d5f
AC
1583 /* The "set radix" and "show radix" commands are special in that
1584 they are like normal set and show commands but allow two normally
1585 independent variables to be either set or shown with a single
b66df561 1586 command. So the usual deprecated_add_set_cmd() and [deleted]
cb1a6d5f 1587 add_show_from_set() commands aren't really appropriate. */
b66df561
AC
1588 /* FIXME: i18n: With the new add_setshow_integer command, that is no
1589 longer true - show can display anything. */
1a966eab
AC
1590 add_cmd ("radix", class_support, set_radix, _("\
1591Set default input and output number radices.\n\
c906108c 1592Use 'set input-radix' or 'set output-radix' to independently set each.\n\
1a966eab 1593Without an argument, sets both radices back to the default value of 10."),
c906108c 1594 &setlist);
1a966eab
AC
1595 add_cmd ("radix", class_support, show_radix, _("\
1596Show the default input and output number radices.\n\
1597Use 'show input-radix' or 'show output-radix' to independently show each."),
c906108c
SS
1598 &showlist);
1599
e79af960 1600 add_setshow_boolean_cmd ("array-indexes", class_support,
79a45b7d 1601 &user_print_options.print_array_indexes, _("\
e79af960
JB
1602Set printing of array indexes."), _("\
1603Show printing of array indexes"), NULL, NULL, show_print_array_indexes,
1604 &setprintlist, &showprintlist);
c906108c 1605}