Change selttest.c to use use std::vector
[deliverable/binutils-gdb.git] / gdb / valprint.c
CommitLineData
c906108c 1/* Print values for GDB, the GNU debugger.
5c1c87f0 2
618f726f 3 Copyright (C) 1986-2016 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
c906108c 16
c5aa993b 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
19
20#include "defs.h"
c906108c
SS
21#include "symtab.h"
22#include "gdbtypes.h"
23#include "value.h"
24#include "gdbcore.h"
25#include "gdbcmd.h"
26#include "target.h"
c906108c 27#include "language.h"
c906108c
SS
28#include "annotate.h"
29#include "valprint.h"
39424bef 30#include "floatformat.h"
d16aafd8 31#include "doublest.h"
7678ef8f 32#include "dfp.h"
6dddc817 33#include "extension.h"
0c3acc09 34#include "ada-lang.h"
3b2b8fea
TT
35#include "gdb_obstack.h"
36#include "charset.h"
3f2f83dd 37#include "typeprint.h"
3b2b8fea 38#include <ctype.h>
325fac50 39#include <algorithm>
c906108c 40
0d63ecda
KS
41/* Maximum number of wchars returned from wchar_iterate. */
42#define MAX_WCHARS 4
43
44/* A convenience macro to compute the size of a wchar_t buffer containing X
45 characters. */
46#define WCHAR_BUFLEN(X) ((X) * sizeof (gdb_wchar_t))
47
48/* Character buffer size saved while iterating over wchars. */
49#define WCHAR_BUFLEN_MAX WCHAR_BUFLEN (MAX_WCHARS)
50
51/* A structure to encapsulate state information from iterated
52 character conversions. */
53struct converted_character
54{
55 /* The number of characters converted. */
56 int num_chars;
57
58 /* The result of the conversion. See charset.h for more. */
59 enum wchar_iterate_result result;
60
61 /* The (saved) converted character(s). */
62 gdb_wchar_t chars[WCHAR_BUFLEN_MAX];
63
64 /* The first converted target byte. */
65 const gdb_byte *buf;
66
67 /* The number of bytes converted. */
68 size_t buflen;
69
70 /* How many times this character(s) is repeated. */
71 int repeat_count;
72};
73
74typedef struct converted_character converted_character_d;
75DEF_VEC_O (converted_character_d);
76
e7045703
DE
77/* Command lists for set/show print raw. */
78struct cmd_list_element *setprintrawlist;
79struct cmd_list_element *showprintrawlist;
0d63ecda 80
c906108c
SS
81/* Prototypes for local functions */
82
777ea8f1 83static int partial_memory_read (CORE_ADDR memaddr, gdb_byte *myaddr,
578d3588 84 int len, int *errptr);
917317f4 85
a14ed312 86static void show_print (char *, int);
c906108c 87
a14ed312 88static void set_print (char *, int);
c906108c 89
a14ed312 90static void set_radix (char *, int);
c906108c 91
a14ed312 92static void show_radix (char *, int);
c906108c 93
a14ed312 94static void set_input_radix (char *, int, struct cmd_list_element *);
c906108c 95
a14ed312 96static void set_input_radix_1 (int, unsigned);
c906108c 97
a14ed312 98static void set_output_radix (char *, int, struct cmd_list_element *);
c906108c 99
a14ed312 100static void set_output_radix_1 (int, unsigned);
c906108c 101
81516450
DE
102static void val_print_type_code_flags (struct type *type,
103 const gdb_byte *valaddr,
104 struct ui_file *stream);
105
a14ed312 106void _initialize_valprint (void);
c906108c 107
581e13c1 108#define PRINT_MAX_DEFAULT 200 /* Start print_max off at this value. */
79a45b7d
TT
109
110struct value_print_options user_print_options =
111{
2a998fc0
DE
112 Val_prettyformat_default, /* prettyformat */
113 0, /* prettyformat_arrays */
114 0, /* prettyformat_structs */
79a45b7d
TT
115 0, /* vtblprint */
116 1, /* unionprint */
117 1, /* addressprint */
118 0, /* objectprint */
119 PRINT_MAX_DEFAULT, /* print_max */
120 10, /* repeat_count_threshold */
121 0, /* output_format */
122 0, /* format */
123 0, /* stop_print_at_null */
79a45b7d
TT
124 0, /* print_array_indexes */
125 0, /* deref_ref */
126 1, /* static_field_print */
a6bac58e
TT
127 1, /* pascal_static_field_print */
128 0, /* raw */
9cb709b6
TT
129 0, /* summary */
130 1 /* symbol_print */
79a45b7d
TT
131};
132
133/* Initialize *OPTS to be a copy of the user print options. */
134void
135get_user_print_options (struct value_print_options *opts)
136{
137 *opts = user_print_options;
138}
139
140/* Initialize *OPTS to be a copy of the user print options, but with
2a998fc0 141 pretty-formatting disabled. */
79a45b7d 142void
2a998fc0 143get_no_prettyformat_print_options (struct value_print_options *opts)
79a45b7d
TT
144{
145 *opts = user_print_options;
2a998fc0 146 opts->prettyformat = Val_no_prettyformat;
79a45b7d
TT
147}
148
149/* Initialize *OPTS to be a copy of the user print options, but using
150 FORMAT as the formatting option. */
151void
152get_formatted_print_options (struct value_print_options *opts,
153 char format)
154{
155 *opts = user_print_options;
156 opts->format = format;
157}
158
920d2a44
AC
159static void
160show_print_max (struct ui_file *file, int from_tty,
161 struct cmd_list_element *c, const char *value)
162{
3e43a32a
MS
163 fprintf_filtered (file,
164 _("Limit on string chars or array "
165 "elements to print is %s.\n"),
920d2a44
AC
166 value);
167}
168
c906108c
SS
169
170/* Default input and output radixes, and output format letter. */
171
172unsigned input_radix = 10;
920d2a44
AC
173static void
174show_input_radix (struct ui_file *file, int from_tty,
175 struct cmd_list_element *c, const char *value)
176{
3e43a32a
MS
177 fprintf_filtered (file,
178 _("Default input radix for entering numbers is %s.\n"),
920d2a44
AC
179 value);
180}
181
c906108c 182unsigned output_radix = 10;
920d2a44
AC
183static void
184show_output_radix (struct ui_file *file, int from_tty,
185 struct cmd_list_element *c, const char *value)
186{
3e43a32a
MS
187 fprintf_filtered (file,
188 _("Default output radix for printing of values is %s.\n"),
920d2a44
AC
189 value);
190}
c906108c 191
e79af960
JB
192/* By default we print arrays without printing the index of each element in
193 the array. This behavior can be changed by setting PRINT_ARRAY_INDEXES. */
194
e79af960
JB
195static void
196show_print_array_indexes (struct ui_file *file, int from_tty,
197 struct cmd_list_element *c, const char *value)
198{
199 fprintf_filtered (file, _("Printing of array indexes is %s.\n"), value);
200}
201
c906108c
SS
202/* Print repeat counts if there are more than this many repetitions of an
203 element in an array. Referenced by the low level language dependent
581e13c1 204 print routines. */
c906108c 205
920d2a44
AC
206static void
207show_repeat_count_threshold (struct ui_file *file, int from_tty,
208 struct cmd_list_element *c, const char *value)
209{
210 fprintf_filtered (file, _("Threshold for repeated print elements is %s.\n"),
211 value);
212}
c906108c 213
581e13c1 214/* If nonzero, stops printing of char arrays at first null. */
c906108c 215
920d2a44
AC
216static void
217show_stop_print_at_null (struct ui_file *file, int from_tty,
218 struct cmd_list_element *c, const char *value)
219{
3e43a32a
MS
220 fprintf_filtered (file,
221 _("Printing of char arrays to stop "
222 "at first null char is %s.\n"),
920d2a44
AC
223 value);
224}
c906108c 225
581e13c1 226/* Controls pretty printing of structures. */
c906108c 227
920d2a44 228static void
2a998fc0 229show_prettyformat_structs (struct ui_file *file, int from_tty,
920d2a44
AC
230 struct cmd_list_element *c, const char *value)
231{
2a998fc0 232 fprintf_filtered (file, _("Pretty formatting of structures is %s.\n"), value);
920d2a44 233}
c906108c
SS
234
235/* Controls pretty printing of arrays. */
236
920d2a44 237static void
2a998fc0 238show_prettyformat_arrays (struct ui_file *file, int from_tty,
920d2a44
AC
239 struct cmd_list_element *c, const char *value)
240{
2a998fc0 241 fprintf_filtered (file, _("Pretty formatting of arrays is %s.\n"), value);
920d2a44 242}
c906108c
SS
243
244/* If nonzero, causes unions inside structures or other unions to be
581e13c1 245 printed. */
c906108c 246
920d2a44
AC
247static void
248show_unionprint (struct ui_file *file, int from_tty,
249 struct cmd_list_element *c, const char *value)
250{
3e43a32a
MS
251 fprintf_filtered (file,
252 _("Printing of unions interior to structures is %s.\n"),
920d2a44
AC
253 value);
254}
c906108c 255
581e13c1 256/* If nonzero, causes machine addresses to be printed in certain contexts. */
c906108c 257
920d2a44
AC
258static void
259show_addressprint (struct ui_file *file, int from_tty,
260 struct cmd_list_element *c, const char *value)
261{
262 fprintf_filtered (file, _("Printing of addresses is %s.\n"), value);
263}
9cb709b6
TT
264
265static void
266show_symbol_print (struct ui_file *file, int from_tty,
267 struct cmd_list_element *c, const char *value)
268{
269 fprintf_filtered (file,
270 _("Printing of symbols when printing pointers is %s.\n"),
271 value);
272}
273
c906108c 274\f
c5aa993b 275
a6bac58e
TT
276/* A helper function for val_print. When printing in "summary" mode,
277 we want to print scalar arguments, but not aggregate arguments.
278 This function distinguishes between the two. */
279
6211c335
YQ
280int
281val_print_scalar_type_p (struct type *type)
a6bac58e 282{
f168693b 283 type = check_typedef (type);
a6bac58e
TT
284 while (TYPE_CODE (type) == TYPE_CODE_REF)
285 {
286 type = TYPE_TARGET_TYPE (type);
f168693b 287 type = check_typedef (type);
a6bac58e
TT
288 }
289 switch (TYPE_CODE (type))
290 {
291 case TYPE_CODE_ARRAY:
292 case TYPE_CODE_STRUCT:
293 case TYPE_CODE_UNION:
294 case TYPE_CODE_SET:
295 case TYPE_CODE_STRING:
a6bac58e
TT
296 return 0;
297 default:
298 return 1;
299 }
300}
301
a72c8f6a 302/* See its definition in value.h. */
0e03807e 303
a72c8f6a 304int
0e03807e
TT
305valprint_check_validity (struct ui_file *stream,
306 struct type *type,
6b850546 307 LONGEST embedded_offset,
0e03807e
TT
308 const struct value *val)
309{
f168693b 310 type = check_typedef (type);
0e03807e 311
3f2f83dd
KB
312 if (type_not_associated (type))
313 {
314 val_print_not_associated (stream);
315 return 0;
316 }
317
318 if (type_not_allocated (type))
319 {
320 val_print_not_allocated (stream);
321 return 0;
322 }
323
0e03807e
TT
324 if (TYPE_CODE (type) != TYPE_CODE_UNION
325 && TYPE_CODE (type) != TYPE_CODE_STRUCT
326 && TYPE_CODE (type) != TYPE_CODE_ARRAY)
327 {
9a0dc9e3
PA
328 if (value_bits_any_optimized_out (val,
329 TARGET_CHAR_BIT * embedded_offset,
330 TARGET_CHAR_BIT * TYPE_LENGTH (type)))
0e03807e 331 {
901461f8 332 val_print_optimized_out (val, stream);
0e03807e
TT
333 return 0;
334 }
8cf6f0b1 335
4e07d55f 336 if (value_bits_synthetic_pointer (val, TARGET_CHAR_BIT * embedded_offset,
8cf6f0b1
TT
337 TARGET_CHAR_BIT * TYPE_LENGTH (type)))
338 {
3326303b
MG
339 const int is_ref = TYPE_CODE (type) == TYPE_CODE_REF;
340 int ref_is_addressable = 0;
341
342 if (is_ref)
343 {
344 const struct value *deref_val = coerce_ref_if_computed (val);
345
346 if (deref_val != NULL)
347 ref_is_addressable = value_lval_const (deref_val) == lval_memory;
348 }
349
350 if (!is_ref || !ref_is_addressable)
351 fputs_filtered (_("<synthetic pointer>"), stream);
352
353 /* C++ references should be valid even if they're synthetic. */
354 return is_ref;
8cf6f0b1 355 }
4e07d55f
PA
356
357 if (!value_bytes_available (val, embedded_offset, TYPE_LENGTH (type)))
358 {
359 val_print_unavailable (stream);
360 return 0;
361 }
0e03807e
TT
362 }
363
364 return 1;
365}
366
585fdaa1 367void
901461f8 368val_print_optimized_out (const struct value *val, struct ui_file *stream)
585fdaa1 369{
901461f8 370 if (val != NULL && value_lval_const (val) == lval_register)
782d47df 371 val_print_not_saved (stream);
901461f8
PA
372 else
373 fprintf_filtered (stream, _("<optimized out>"));
585fdaa1
PA
374}
375
782d47df
PA
376void
377val_print_not_saved (struct ui_file *stream)
378{
379 fprintf_filtered (stream, _("<not saved>"));
380}
381
4e07d55f
PA
382void
383val_print_unavailable (struct ui_file *stream)
384{
385 fprintf_filtered (stream, _("<unavailable>"));
386}
387
8af8e3bc
PA
388void
389val_print_invalid_address (struct ui_file *stream)
390{
391 fprintf_filtered (stream, _("<invalid address>"));
392}
393
9f436164
SM
394/* Print a pointer based on the type of its target.
395
396 Arguments to this functions are roughly the same as those in
397 generic_val_print. A difference is that ADDRESS is the address to print,
398 with embedded_offset already added. ELTTYPE represents
399 the pointed type after check_typedef. */
400
401static void
402print_unpacked_pointer (struct type *type, struct type *elttype,
403 CORE_ADDR address, struct ui_file *stream,
404 const struct value_print_options *options)
405{
406 struct gdbarch *gdbarch = get_type_arch (type);
407
408 if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
409 {
410 /* Try to print what function it points to. */
411 print_function_pointer_address (options, gdbarch, address, stream);
412 return;
413 }
414
415 if (options->symbol_print)
416 print_address_demangle (options, gdbarch, address, stream, demangle);
417 else if (options->addressprint)
418 fputs_filtered (paddress (gdbarch, address), stream);
419}
420
557dbe8a
SM
421/* generic_val_print helper for TYPE_CODE_ARRAY. */
422
423static void
424generic_val_print_array (struct type *type, const gdb_byte *valaddr,
00272ec4
TT
425 int embedded_offset, CORE_ADDR address,
426 struct ui_file *stream, int recurse,
427 const struct value *original_value,
428 const struct value_print_options *options,
429 const struct
430 generic_val_print_decorations *decorations)
557dbe8a
SM
431{
432 struct type *unresolved_elttype = TYPE_TARGET_TYPE (type);
433 struct type *elttype = check_typedef (unresolved_elttype);
434
435 if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (unresolved_elttype) > 0)
436 {
437 LONGEST low_bound, high_bound;
438
439 if (!get_array_bounds (type, &low_bound, &high_bound))
440 error (_("Could not determine the array high bound"));
441
442 if (options->prettyformat_arrays)
443 {
444 print_spaces_filtered (2 + 2 * recurse, stream);
445 }
446
00272ec4 447 fputs_filtered (decorations->array_start, stream);
557dbe8a
SM
448 val_print_array_elements (type, valaddr, embedded_offset,
449 address, stream,
450 recurse, original_value, options, 0);
00272ec4 451 fputs_filtered (decorations->array_end, stream);
557dbe8a
SM
452 }
453 else
454 {
455 /* Array of unspecified length: treat like pointer to first elt. */
456 print_unpacked_pointer (type, elttype, address + embedded_offset, stream,
457 options);
458 }
459
460}
461
81eb921a
SM
462/* generic_val_print helper for TYPE_CODE_PTR. */
463
464static void
465generic_val_print_ptr (struct type *type, const gdb_byte *valaddr,
466 int embedded_offset, struct ui_file *stream,
467 const struct value *original_value,
468 const struct value_print_options *options)
469{
3ae385af
SM
470 struct gdbarch *gdbarch = get_type_arch (type);
471 int unit_size = gdbarch_addressable_memory_unit_size (gdbarch);
472
81eb921a
SM
473 if (options->format && options->format != 's')
474 {
475 val_print_scalar_formatted (type, valaddr, embedded_offset,
476 original_value, options, 0, stream);
477 }
478 else
479 {
480 struct type *unresolved_elttype = TYPE_TARGET_TYPE(type);
481 struct type *elttype = check_typedef (unresolved_elttype);
3ae385af
SM
482 CORE_ADDR addr = unpack_pointer (type,
483 valaddr + embedded_offset * unit_size);
81eb921a
SM
484
485 print_unpacked_pointer (type, elttype, addr, stream, options);
486 }
487}
488
45000ea2
SM
489
490/* generic_val_print helper for TYPE_CODE_MEMBERPTR. */
491
492static void
493generic_val_print_memberptr (struct type *type, const gdb_byte *valaddr,
494 int embedded_offset, struct ui_file *stream,
495 const struct value *original_value,
496 const struct value_print_options *options)
497{
498 val_print_scalar_formatted (type, valaddr, embedded_offset,
499 original_value, options, 0, stream);
500}
501
3326303b
MG
502/* Print '@' followed by the address contained in ADDRESS_BUFFER. */
503
504static void
505print_ref_address (struct type *type, const gdb_byte *address_buffer,
506 int embedded_offset, struct ui_file *stream)
507{
508 struct gdbarch *gdbarch = get_type_arch (type);
509
510 if (address_buffer != NULL)
511 {
512 CORE_ADDR address
513 = extract_typed_address (address_buffer + embedded_offset, type);
514
515 fprintf_filtered (stream, "@");
516 fputs_filtered (paddress (gdbarch, address), stream);
517 }
518 /* Else: we have a non-addressable value, such as a DW_AT_const_value. */
519}
520
521/* If VAL is addressable, return the value contents buffer of a value that
522 represents a pointer to VAL. Otherwise return NULL. */
523
524static const gdb_byte *
525get_value_addr_contents (struct value *deref_val)
526{
527 gdb_assert (deref_val != NULL);
528
529 if (value_lval_const (deref_val) == lval_memory)
530 return value_contents_for_printing_const (value_addr (deref_val));
531 else
532 {
533 /* We have a non-addressable value, such as a DW_AT_const_value. */
534 return NULL;
535 }
536}
537
fe43fede
SM
538/* generic_val_print helper for TYPE_CODE_REF. */
539
540static void
541generic_val_print_ref (struct type *type, const gdb_byte *valaddr,
542 int embedded_offset, struct ui_file *stream, int recurse,
543 const struct value *original_value,
544 const struct value_print_options *options)
545{
fe43fede 546 struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type));
3326303b
MG
547 struct value *deref_val = NULL;
548 const int value_is_synthetic
549 = value_bits_synthetic_pointer (original_value,
550 TARGET_CHAR_BIT * embedded_offset,
551 TARGET_CHAR_BIT * TYPE_LENGTH (type));
552 const int must_coerce_ref = ((options->addressprint && value_is_synthetic)
553 || options->deref_ref);
554 const int type_is_defined = TYPE_CODE (elttype) != TYPE_CODE_UNDEF;
555
556 if (must_coerce_ref && type_is_defined)
557 {
558 deref_val = coerce_ref_if_computed (original_value);
559
560 if (deref_val != NULL)
561 {
562 /* More complicated computed references are not supported. */
563 gdb_assert (embedded_offset == 0);
564 }
565 else
566 deref_val = value_at (TYPE_TARGET_TYPE (type),
567 unpack_pointer (type, valaddr + embedded_offset));
568 }
569 /* Else, original_value isn't a synthetic reference or we don't have to print
570 the reference's contents.
571
572 Notice that for references to TYPE_CODE_STRUCT, 'set print object on' will
573 cause original_value to be a not_lval instead of an lval_computed,
574 which will make value_bits_synthetic_pointer return false.
575 This happens because if options->objectprint is true, c_value_print will
576 overwrite original_value's contents with the result of coercing
577 the reference through value_addr, and then set its type back to
578 TYPE_CODE_REF. In that case we don't have to coerce the reference again;
579 we can simply treat it as non-synthetic and move on. */
fe43fede
SM
580
581 if (options->addressprint)
582 {
3326303b
MG
583 const gdb_byte *address = (value_is_synthetic && type_is_defined
584 ? get_value_addr_contents (deref_val)
585 : valaddr);
586
587 print_ref_address (type, address, embedded_offset, stream);
fe43fede 588
fe43fede
SM
589 if (options->deref_ref)
590 fputs_filtered (": ", stream);
591 }
3326303b 592
fe43fede
SM
593 if (options->deref_ref)
594 {
3326303b
MG
595 if (type_is_defined)
596 common_val_print (deref_val, stream, recurse, options,
597 current_language);
fe43fede
SM
598 else
599 fputs_filtered ("???", stream);
600 }
601}
602
81516450
DE
603/* Helper function for generic_val_print_enum.
604 This is also used to print enums in TYPE_CODE_FLAGS values. */
ef0bc0dd
SM
605
606static void
81516450
DE
607generic_val_print_enum_1 (struct type *type, LONGEST val,
608 struct ui_file *stream)
ef0bc0dd
SM
609{
610 unsigned int i;
611 unsigned int len;
ef0bc0dd 612
ef0bc0dd 613 len = TYPE_NFIELDS (type);
ef0bc0dd
SM
614 for (i = 0; i < len; i++)
615 {
616 QUIT;
617 if (val == TYPE_FIELD_ENUMVAL (type, i))
618 {
619 break;
620 }
621 }
622 if (i < len)
623 {
624 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
625 }
626 else if (TYPE_FLAG_ENUM (type))
627 {
628 int first = 1;
629
630 /* We have a "flag" enum, so we try to decompose it into
631 pieces as appropriate. A flag enum has disjoint
632 constants by definition. */
633 fputs_filtered ("(", stream);
634 for (i = 0; i < len; ++i)
635 {
636 QUIT;
637
638 if ((val & TYPE_FIELD_ENUMVAL (type, i)) != 0)
639 {
640 if (!first)
641 fputs_filtered (" | ", stream);
642 first = 0;
643
644 val &= ~TYPE_FIELD_ENUMVAL (type, i);
645 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
646 }
647 }
648
649 if (first || val != 0)
650 {
651 if (!first)
652 fputs_filtered (" | ", stream);
653 fputs_filtered ("unknown: ", stream);
654 print_longest (stream, 'd', 0, val);
655 }
656
657 fputs_filtered (")", stream);
658 }
659 else
660 print_longest (stream, 'd', 0, val);
661}
662
81516450
DE
663/* generic_val_print helper for TYPE_CODE_ENUM. */
664
665static void
666generic_val_print_enum (struct type *type, const gdb_byte *valaddr,
667 int embedded_offset, struct ui_file *stream,
668 const struct value *original_value,
669 const struct value_print_options *options)
670{
671 LONGEST val;
672 struct gdbarch *gdbarch = get_type_arch (type);
673 int unit_size = gdbarch_addressable_memory_unit_size (gdbarch);
674
675 if (options->format)
676 {
677 val_print_scalar_formatted (type, valaddr, embedded_offset,
678 original_value, options, 0, stream);
679 return;
680 }
681 val = unpack_long (type, valaddr + embedded_offset * unit_size);
682
683 generic_val_print_enum_1 (type, val, stream);
684}
685
d93880bd
SM
686/* generic_val_print helper for TYPE_CODE_FLAGS. */
687
688static void
689generic_val_print_flags (struct type *type, const gdb_byte *valaddr,
690 int embedded_offset, struct ui_file *stream,
691 const struct value *original_value,
692 const struct value_print_options *options)
693
694{
695 if (options->format)
696 val_print_scalar_formatted (type, valaddr, embedded_offset, original_value,
697 options, 0, stream);
698 else
699 val_print_type_code_flags (type, valaddr + embedded_offset, stream);
700}
701
4a8c372f
SM
702/* generic_val_print helper for TYPE_CODE_FUNC and TYPE_CODE_METHOD. */
703
704static void
705generic_val_print_func (struct type *type, const gdb_byte *valaddr,
706 int embedded_offset, CORE_ADDR address,
707 struct ui_file *stream,
708 const struct value *original_value,
709 const struct value_print_options *options)
710{
711 struct gdbarch *gdbarch = get_type_arch (type);
712
713 if (options->format)
714 {
715 val_print_scalar_formatted (type, valaddr, embedded_offset,
716 original_value, options, 0, stream);
717 }
718 else
719 {
720 /* FIXME, we should consider, at least for ANSI C language,
721 eliminating the distinction made between FUNCs and POINTERs
722 to FUNCs. */
723 fprintf_filtered (stream, "{");
724 type_print (type, "", stream, -1);
725 fprintf_filtered (stream, "} ");
726 /* Try to print what function it points to, and its address. */
727 print_address_demangle (options, gdbarch, address, stream, demangle);
728 }
729}
730
e5bead4b
SM
731/* generic_val_print helper for TYPE_CODE_BOOL. */
732
733static void
734generic_val_print_bool (struct type *type, const gdb_byte *valaddr,
735 int embedded_offset, struct ui_file *stream,
736 const struct value *original_value,
737 const struct value_print_options *options,
738 const struct generic_val_print_decorations *decorations)
739{
740 LONGEST val;
3ae385af
SM
741 struct gdbarch *gdbarch = get_type_arch (type);
742 int unit_size = gdbarch_addressable_memory_unit_size (gdbarch);
e5bead4b
SM
743
744 if (options->format || options->output_format)
745 {
746 struct value_print_options opts = *options;
747 opts.format = (options->format ? options->format
748 : options->output_format);
749 val_print_scalar_formatted (type, valaddr, embedded_offset,
750 original_value, &opts, 0, stream);
751 }
752 else
753 {
3ae385af 754 val = unpack_long (type, valaddr + embedded_offset * unit_size);
e5bead4b
SM
755 if (val == 0)
756 fputs_filtered (decorations->false_name, stream);
757 else if (val == 1)
758 fputs_filtered (decorations->true_name, stream);
759 else
760 print_longest (stream, 'd', 0, val);
761 }
762}
763
b21b6342
SM
764/* generic_val_print helper for TYPE_CODE_INT. */
765
766static void
767generic_val_print_int (struct type *type, const gdb_byte *valaddr,
768 int embedded_offset, struct ui_file *stream,
769 const struct value *original_value,
770 const struct value_print_options *options)
771{
3ae385af
SM
772 struct gdbarch *gdbarch = get_type_arch (type);
773 int unit_size = gdbarch_addressable_memory_unit_size (gdbarch);
774
b21b6342
SM
775 if (options->format || options->output_format)
776 {
777 struct value_print_options opts = *options;
778
779 opts.format = (options->format ? options->format
780 : options->output_format);
781 val_print_scalar_formatted (type, valaddr, embedded_offset,
782 original_value, &opts, 0, stream);
783 }
784 else
3ae385af
SM
785 val_print_type_code_int (type, valaddr + embedded_offset * unit_size,
786 stream);
b21b6342
SM
787}
788
385f5aff
SM
789/* generic_val_print helper for TYPE_CODE_CHAR. */
790
791static void
792generic_val_print_char (struct type *type, struct type *unresolved_type,
793 const gdb_byte *valaddr, int embedded_offset,
794 struct ui_file *stream,
795 const struct value *original_value,
796 const struct value_print_options *options)
797{
798 LONGEST val;
3ae385af
SM
799 struct gdbarch *gdbarch = get_type_arch (type);
800 int unit_size = gdbarch_addressable_memory_unit_size (gdbarch);
385f5aff
SM
801
802 if (options->format || options->output_format)
803 {
804 struct value_print_options opts = *options;
805
806 opts.format = (options->format ? options->format
807 : options->output_format);
808 val_print_scalar_formatted (type, valaddr, embedded_offset,
809 original_value, &opts, 0, stream);
810 }
811 else
812 {
3ae385af 813 val = unpack_long (type, valaddr + embedded_offset * unit_size);
385f5aff
SM
814 if (TYPE_UNSIGNED (type))
815 fprintf_filtered (stream, "%u", (unsigned int) val);
816 else
817 fprintf_filtered (stream, "%d", (int) val);
818 fputs_filtered (" ", stream);
819 LA_PRINT_CHAR (val, unresolved_type, stream);
820 }
821}
822
7784724b
SM
823/* generic_val_print helper for TYPE_CODE_FLT. */
824
825static void
826generic_val_print_float (struct type *type, const gdb_byte *valaddr,
827 int embedded_offset, struct ui_file *stream,
828 const struct value *original_value,
829 const struct value_print_options *options)
830{
3ae385af
SM
831 struct gdbarch *gdbarch = get_type_arch (type);
832 int unit_size = gdbarch_addressable_memory_unit_size (gdbarch);
833
7784724b
SM
834 if (options->format)
835 {
836 val_print_scalar_formatted (type, valaddr, embedded_offset,
837 original_value, options, 0, stream);
838 }
839 else
840 {
3ae385af 841 print_floating (valaddr + embedded_offset * unit_size, type, stream);
7784724b
SM
842 }
843}
844
9550ae5e
SM
845/* generic_val_print helper for TYPE_CODE_DECFLOAT. */
846
847static void
848generic_val_print_decfloat (struct type *type, const gdb_byte *valaddr,
849 int embedded_offset, struct ui_file *stream,
850 const struct value *original_value,
851 const struct value_print_options *options)
852{
3ae385af
SM
853 struct gdbarch *gdbarch = get_type_arch (type);
854 int unit_size = gdbarch_addressable_memory_unit_size (gdbarch);
855
9550ae5e
SM
856 if (options->format)
857 val_print_scalar_formatted (type, valaddr, embedded_offset, original_value,
858 options, 0, stream);
859 else
3ae385af
SM
860 print_decimal_floating (valaddr + embedded_offset * unit_size, type,
861 stream);
9550ae5e
SM
862}
863
0c87c0bf
SM
864/* generic_val_print helper for TYPE_CODE_COMPLEX. */
865
866static void
867generic_val_print_complex (struct type *type, const gdb_byte *valaddr,
868 int embedded_offset, struct ui_file *stream,
869 const struct value *original_value,
870 const struct value_print_options *options,
871 const struct generic_val_print_decorations
872 *decorations)
873{
3ae385af
SM
874 struct gdbarch *gdbarch = get_type_arch (type);
875 int unit_size = gdbarch_addressable_memory_unit_size (gdbarch);
876
0c87c0bf
SM
877 fprintf_filtered (stream, "%s", decorations->complex_prefix);
878 if (options->format)
879 val_print_scalar_formatted (TYPE_TARGET_TYPE (type), valaddr,
880 embedded_offset, original_value, options, 0,
881 stream);
882 else
3ae385af
SM
883 print_floating (valaddr + embedded_offset * unit_size,
884 TYPE_TARGET_TYPE (type), stream);
0c87c0bf
SM
885 fprintf_filtered (stream, "%s", decorations->complex_infix);
886 if (options->format)
887 val_print_scalar_formatted (TYPE_TARGET_TYPE (type), valaddr,
888 embedded_offset
3ae385af 889 + type_length_units (TYPE_TARGET_TYPE (type)),
0c87c0bf
SM
890 original_value, options, 0, stream);
891 else
3ae385af 892 print_floating (valaddr + embedded_offset * unit_size
0c87c0bf
SM
893 + TYPE_LENGTH (TYPE_TARGET_TYPE (type)),
894 TYPE_TARGET_TYPE (type), stream);
895 fprintf_filtered (stream, "%s", decorations->complex_suffix);
896}
897
e88acd96
TT
898/* A generic val_print that is suitable for use by language
899 implementations of the la_val_print method. This function can
900 handle most type codes, though not all, notably exception
901 TYPE_CODE_UNION and TYPE_CODE_STRUCT, which must be implemented by
902 the caller.
903
904 Most arguments are as to val_print.
905
906 The additional DECORATIONS argument can be used to customize the
907 output in some small, language-specific ways. */
908
909void
910generic_val_print (struct type *type, const gdb_byte *valaddr,
911 int embedded_offset, CORE_ADDR address,
912 struct ui_file *stream, int recurse,
913 const struct value *original_value,
914 const struct value_print_options *options,
915 const struct generic_val_print_decorations *decorations)
916{
e88acd96 917 struct type *unresolved_type = type;
e88acd96 918
f168693b 919 type = check_typedef (type);
e88acd96
TT
920 switch (TYPE_CODE (type))
921 {
922 case TYPE_CODE_ARRAY:
557dbe8a 923 generic_val_print_array (type, valaddr, embedded_offset, address, stream,
00272ec4 924 recurse, original_value, options, decorations);
9f436164 925 break;
e88acd96
TT
926
927 case TYPE_CODE_MEMBERPTR:
45000ea2
SM
928 generic_val_print_memberptr (type, valaddr, embedded_offset, stream,
929 original_value, options);
e88acd96
TT
930 break;
931
932 case TYPE_CODE_PTR:
81eb921a
SM
933 generic_val_print_ptr (type, valaddr, embedded_offset, stream,
934 original_value, options);
e88acd96
TT
935 break;
936
937 case TYPE_CODE_REF:
fe43fede
SM
938 generic_val_print_ref (type, valaddr, embedded_offset, stream, recurse,
939 original_value, options);
e88acd96
TT
940 break;
941
942 case TYPE_CODE_ENUM:
ef0bc0dd
SM
943 generic_val_print_enum (type, valaddr, embedded_offset, stream,
944 original_value, options);
e88acd96
TT
945 break;
946
947 case TYPE_CODE_FLAGS:
d93880bd
SM
948 generic_val_print_flags (type, valaddr, embedded_offset, stream,
949 original_value, options);
e88acd96
TT
950 break;
951
952 case TYPE_CODE_FUNC:
953 case TYPE_CODE_METHOD:
4a8c372f
SM
954 generic_val_print_func (type, valaddr, embedded_offset, address, stream,
955 original_value, options);
e88acd96
TT
956 break;
957
958 case TYPE_CODE_BOOL:
e5bead4b
SM
959 generic_val_print_bool (type, valaddr, embedded_offset, stream,
960 original_value, options, decorations);
e88acd96
TT
961 break;
962
963 case TYPE_CODE_RANGE:
0c9c3474 964 /* FIXME: create_static_range_type does not set the unsigned bit in a
e88acd96
TT
965 range type (I think it probably should copy it from the
966 target type), so we won't print values which are too large to
967 fit in a signed integer correctly. */
968 /* FIXME: Doesn't handle ranges of enums correctly. (Can't just
969 print with the target type, though, because the size of our
970 type and the target type might differ). */
971
972 /* FALLTHROUGH */
973
974 case TYPE_CODE_INT:
b21b6342
SM
975 generic_val_print_int (type, valaddr, embedded_offset, stream,
976 original_value, options);
e88acd96
TT
977 break;
978
979 case TYPE_CODE_CHAR:
385f5aff
SM
980 generic_val_print_char (type, unresolved_type, valaddr, embedded_offset,
981 stream, original_value, options);
e88acd96
TT
982 break;
983
984 case TYPE_CODE_FLT:
7784724b
SM
985 generic_val_print_float (type, valaddr, embedded_offset, stream,
986 original_value, options);
e88acd96
TT
987 break;
988
989 case TYPE_CODE_DECFLOAT:
9550ae5e
SM
990 generic_val_print_decfloat (type, valaddr, embedded_offset, stream,
991 original_value, options);
e88acd96
TT
992 break;
993
994 case TYPE_CODE_VOID:
995 fputs_filtered (decorations->void_name, stream);
996 break;
997
998 case TYPE_CODE_ERROR:
999 fprintf_filtered (stream, "%s", TYPE_ERROR_NAME (type));
1000 break;
1001
1002 case TYPE_CODE_UNDEF:
a9ff5f12
UW
1003 /* This happens (without TYPE_STUB set) on systems which don't use
1004 dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
1005 and no complete type for struct foo in that file. */
e88acd96
TT
1006 fprintf_filtered (stream, _("<incomplete type>"));
1007 break;
1008
1009 case TYPE_CODE_COMPLEX:
0c87c0bf
SM
1010 generic_val_print_complex (type, valaddr, embedded_offset, stream,
1011 original_value, options, decorations);
e88acd96
TT
1012 break;
1013
1014 case TYPE_CODE_UNION:
1015 case TYPE_CODE_STRUCT:
1016 case TYPE_CODE_METHODPTR:
1017 default:
1018 error (_("Unhandled type code %d in symbol table."),
1019 TYPE_CODE (type));
1020 }
1021 gdb_flush (stream);
1022}
1023
32b72a42
PA
1024/* Print using the given LANGUAGE the data of type TYPE located at
1025 VALADDR + EMBEDDED_OFFSET (within GDB), which came from the
1026 inferior at address ADDRESS + EMBEDDED_OFFSET, onto stdio stream
1027 STREAM according to OPTIONS. VAL is the whole object that came
1028 from ADDRESS. VALADDR must point to the head of VAL's contents
1029 buffer.
1030
1031 The language printers will pass down an adjusted EMBEDDED_OFFSET to
1032 further helper subroutines as subfields of TYPE are printed. In
1033 such cases, VALADDR is passed down unadjusted, as well as VAL, so
1034 that VAL can be queried for metadata about the contents data being
1035 printed, using EMBEDDED_OFFSET as an offset into VAL's contents
1036 buffer. For example: "has this field been optimized out", or "I'm
1037 printing an object while inspecting a traceframe; has this
1038 particular piece of data been collected?".
1039
1040 RECURSE indicates the amount of indentation to supply before
1041 continuation lines; this amount is roughly twice the value of
35c0084b 1042 RECURSE. */
32b72a42 1043
35c0084b 1044void
6b850546 1045val_print (struct type *type, const gdb_byte *valaddr, LONGEST embedded_offset,
79a45b7d 1046 CORE_ADDR address, struct ui_file *stream, int recurse,
0e03807e 1047 const struct value *val,
79a45b7d 1048 const struct value_print_options *options,
d8ca156b 1049 const struct language_defn *language)
c906108c 1050{
19ca80ba 1051 int ret = 0;
79a45b7d 1052 struct value_print_options local_opts = *options;
c906108c 1053 struct type *real_type = check_typedef (type);
79a45b7d 1054
2a998fc0
DE
1055 if (local_opts.prettyformat == Val_prettyformat_default)
1056 local_opts.prettyformat = (local_opts.prettyformat_structs
1057 ? Val_prettyformat : Val_no_prettyformat);
c5aa993b 1058
c906108c
SS
1059 QUIT;
1060
1061 /* Ensure that the type is complete and not just a stub. If the type is
1062 only a stub and we can't find and substitute its complete type, then
1063 print appropriate string and return. */
1064
74a9bb82 1065 if (TYPE_STUB (real_type))
c906108c 1066 {
0e03807e 1067 fprintf_filtered (stream, _("<incomplete type>"));
c906108c 1068 gdb_flush (stream);
35c0084b 1069 return;
c906108c 1070 }
c5aa993b 1071
0e03807e 1072 if (!valprint_check_validity (stream, real_type, embedded_offset, val))
35c0084b 1073 return;
0e03807e 1074
a6bac58e
TT
1075 if (!options->raw)
1076 {
6dddc817
DE
1077 ret = apply_ext_lang_val_pretty_printer (type, valaddr, embedded_offset,
1078 address, stream, recurse,
1079 val, options, language);
a6bac58e 1080 if (ret)
35c0084b 1081 return;
a6bac58e
TT
1082 }
1083
1084 /* Handle summary mode. If the value is a scalar, print it;
1085 otherwise, print an ellipsis. */
6211c335 1086 if (options->summary && !val_print_scalar_type_p (type))
a6bac58e
TT
1087 {
1088 fprintf_filtered (stream, "...");
35c0084b 1089 return;
a6bac58e
TT
1090 }
1091
492d29ea 1092 TRY
19ca80ba 1093 {
d3eab38a
TT
1094 language->la_val_print (type, valaddr, embedded_offset, address,
1095 stream, recurse, val,
1096 &local_opts);
19ca80ba 1097 }
492d29ea
PA
1098 CATCH (except, RETURN_MASK_ERROR)
1099 {
1100 fprintf_filtered (stream, _("<error reading variable>"));
1101 }
1102 END_CATCH
c906108c
SS
1103}
1104
806048c6 1105/* Check whether the value VAL is printable. Return 1 if it is;
6501578c
YQ
1106 return 0 and print an appropriate error message to STREAM according to
1107 OPTIONS if it is not. */
c906108c 1108
806048c6 1109static int
6501578c
YQ
1110value_check_printable (struct value *val, struct ui_file *stream,
1111 const struct value_print_options *options)
c906108c
SS
1112{
1113 if (val == 0)
1114 {
806048c6 1115 fprintf_filtered (stream, _("<address of value unknown>"));
c906108c
SS
1116 return 0;
1117 }
806048c6 1118
0e03807e 1119 if (value_entirely_optimized_out (val))
c906108c 1120 {
6211c335 1121 if (options->summary && !val_print_scalar_type_p (value_type (val)))
6501578c
YQ
1122 fprintf_filtered (stream, "...");
1123 else
901461f8 1124 val_print_optimized_out (val, stream);
c906108c
SS
1125 return 0;
1126 }
806048c6 1127
eebc056c
AB
1128 if (value_entirely_unavailable (val))
1129 {
1130 if (options->summary && !val_print_scalar_type_p (value_type (val)))
1131 fprintf_filtered (stream, "...");
1132 else
1133 val_print_unavailable (stream);
1134 return 0;
1135 }
1136
bc3b79fd
TJB
1137 if (TYPE_CODE (value_type (val)) == TYPE_CODE_INTERNAL_FUNCTION)
1138 {
1139 fprintf_filtered (stream, _("<internal function %s>"),
1140 value_internal_function_name (val));
1141 return 0;
1142 }
1143
3f2f83dd
KB
1144 if (type_not_associated (value_type (val)))
1145 {
1146 val_print_not_associated (stream);
1147 return 0;
1148 }
1149
1150 if (type_not_allocated (value_type (val)))
1151 {
1152 val_print_not_allocated (stream);
1153 return 0;
1154 }
1155
806048c6
DJ
1156 return 1;
1157}
1158
d8ca156b 1159/* Print using the given LANGUAGE the value VAL onto stream STREAM according
79a45b7d 1160 to OPTIONS.
806048c6 1161
806048c6
DJ
1162 This is a preferable interface to val_print, above, because it uses
1163 GDB's value mechanism. */
1164
a1f5dd1b 1165void
79a45b7d
TT
1166common_val_print (struct value *val, struct ui_file *stream, int recurse,
1167 const struct value_print_options *options,
d8ca156b 1168 const struct language_defn *language)
806048c6 1169{
6501578c 1170 if (!value_check_printable (val, stream, options))
a1f5dd1b 1171 return;
806048c6 1172
0c3acc09
JB
1173 if (language->la_language == language_ada)
1174 /* The value might have a dynamic type, which would cause trouble
1175 below when trying to extract the value contents (since the value
1176 size is determined from the type size which is unknown). So
1177 get a fixed representation of our value. */
1178 val = ada_to_fixed_value (val);
1179
a1f5dd1b
TT
1180 val_print (value_type (val), value_contents_for_printing (val),
1181 value_embedded_offset (val), value_address (val),
1182 stream, recurse,
1183 val, options, language);
806048c6
DJ
1184}
1185
7348c5e1 1186/* Print on stream STREAM the value VAL according to OPTIONS. The value
8e069a98 1187 is printed using the current_language syntax. */
7348c5e1 1188
8e069a98 1189void
79a45b7d
TT
1190value_print (struct value *val, struct ui_file *stream,
1191 const struct value_print_options *options)
806048c6 1192{
6501578c 1193 if (!value_check_printable (val, stream, options))
8e069a98 1194 return;
806048c6 1195
a6bac58e
TT
1196 if (!options->raw)
1197 {
6dddc817
DE
1198 int r
1199 = apply_ext_lang_val_pretty_printer (value_type (val),
1200 value_contents_for_printing (val),
1201 value_embedded_offset (val),
1202 value_address (val),
1203 stream, 0,
1204 val, options, current_language);
a109c7c1 1205
a6bac58e 1206 if (r)
8e069a98 1207 return;
a6bac58e
TT
1208 }
1209
8e069a98 1210 LA_VALUE_PRINT (val, stream, options);
c906108c
SS
1211}
1212
1213/* Called by various <lang>_val_print routines to print
1214 TYPE_CODE_INT's. TYPE is the type. VALADDR is the address of the
1215 value. STREAM is where to print the value. */
1216
1217void
fc1a4b47 1218val_print_type_code_int (struct type *type, const gdb_byte *valaddr,
fba45db2 1219 struct ui_file *stream)
c906108c 1220{
50810684 1221 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
d44e8473 1222
c906108c
SS
1223 if (TYPE_LENGTH (type) > sizeof (LONGEST))
1224 {
1225 LONGEST val;
1226
1227 if (TYPE_UNSIGNED (type)
1228 && extract_long_unsigned_integer (valaddr, TYPE_LENGTH (type),
e17a4113 1229 byte_order, &val))
c906108c
SS
1230 {
1231 print_longest (stream, 'u', 0, val);
1232 }
1233 else
1234 {
1235 /* Signed, or we couldn't turn an unsigned value into a
1236 LONGEST. For signed values, one could assume two's
1237 complement (a reasonable assumption, I think) and do
1238 better than this. */
1239 print_hex_chars (stream, (unsigned char *) valaddr,
d44e8473 1240 TYPE_LENGTH (type), byte_order);
c906108c
SS
1241 }
1242 }
1243 else
1244 {
c906108c
SS
1245 print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0,
1246 unpack_long (type, valaddr));
c906108c
SS
1247 }
1248}
1249
81516450 1250static void
4f2aea11
MK
1251val_print_type_code_flags (struct type *type, const gdb_byte *valaddr,
1252 struct ui_file *stream)
1253{
befae759 1254 ULONGEST val = unpack_long (type, valaddr);
81516450
DE
1255 int field, nfields = TYPE_NFIELDS (type);
1256 struct gdbarch *gdbarch = get_type_arch (type);
1257 struct type *bool_type = builtin_type (gdbarch)->builtin_bool;
4f2aea11 1258
81516450
DE
1259 fputs_filtered ("[", stream);
1260 for (field = 0; field < nfields; field++)
4f2aea11 1261 {
81516450 1262 if (TYPE_FIELD_NAME (type, field)[0] != '\0')
4f2aea11 1263 {
81516450
DE
1264 struct type *field_type = TYPE_FIELD_TYPE (type, field);
1265
1266 if (field_type == bool_type
1267 /* We require boolean types here to be one bit wide. This is a
1268 problematic place to notify the user of an internal error
1269 though. Instead just fall through and print the field as an
1270 int. */
1271 && TYPE_FIELD_BITSIZE (type, field) == 1)
1272 {
1273 if (val & ((ULONGEST)1 << TYPE_FIELD_BITPOS (type, field)))
1274 fprintf_filtered (stream, " %s",
1275 TYPE_FIELD_NAME (type, field));
1276 }
4f2aea11 1277 else
81516450
DE
1278 {
1279 unsigned field_len = TYPE_FIELD_BITSIZE (type, field);
1280 ULONGEST field_val
1281 = val >> (TYPE_FIELD_BITPOS (type, field) - field_len + 1);
1282
1283 if (field_len < sizeof (ULONGEST) * TARGET_CHAR_BIT)
1284 field_val &= ((ULONGEST) 1 << field_len) - 1;
1285 fprintf_filtered (stream, " %s=",
1286 TYPE_FIELD_NAME (type, field));
1287 if (TYPE_CODE (field_type) == TYPE_CODE_ENUM)
1288 generic_val_print_enum_1 (field_type, field_val, stream);
1289 else
1290 print_longest (stream, 'd', 0, field_val);
1291 }
4f2aea11
MK
1292 }
1293 }
81516450 1294 fputs_filtered (" ]", stream);
19c37f24 1295}
ab2188aa
PA
1296
1297/* Print a scalar of data of type TYPE, pointed to in GDB by VALADDR,
1298 according to OPTIONS and SIZE on STREAM. Format i is not supported
1299 at this level.
1300
1301 This is how the elements of an array or structure are printed
1302 with a format. */
ab2188aa
PA
1303
1304void
1305val_print_scalar_formatted (struct type *type,
6b850546 1306 const gdb_byte *valaddr, LONGEST embedded_offset,
ab2188aa
PA
1307 const struct value *val,
1308 const struct value_print_options *options,
1309 int size,
1310 struct ui_file *stream)
1311{
3ae385af
SM
1312 struct gdbarch *arch = get_type_arch (type);
1313 int unit_size = gdbarch_addressable_memory_unit_size (arch);
1314
ab2188aa
PA
1315 gdb_assert (val != NULL);
1316 gdb_assert (valaddr == value_contents_for_printing_const (val));
1317
1318 /* If we get here with a string format, try again without it. Go
1319 all the way back to the language printers, which may call us
1320 again. */
1321 if (options->format == 's')
1322 {
1323 struct value_print_options opts = *options;
1324 opts.format = 0;
1325 opts.deref_ref = 0;
1326 val_print (type, valaddr, embedded_offset, 0, stream, 0, val, &opts,
1327 current_language);
1328 return;
1329 }
1330
1331 /* A scalar object that does not have all bits available can't be
1332 printed, because all bits contribute to its representation. */
9a0dc9e3
PA
1333 if (value_bits_any_optimized_out (val,
1334 TARGET_CHAR_BIT * embedded_offset,
1335 TARGET_CHAR_BIT * TYPE_LENGTH (type)))
901461f8 1336 val_print_optimized_out (val, stream);
4e07d55f
PA
1337 else if (!value_bytes_available (val, embedded_offset, TYPE_LENGTH (type)))
1338 val_print_unavailable (stream);
ab2188aa 1339 else
3ae385af 1340 print_scalar_formatted (valaddr + embedded_offset * unit_size, type,
ab2188aa 1341 options, size, stream);
4f2aea11
MK
1342}
1343
c906108c
SS
1344/* Print a number according to FORMAT which is one of d,u,x,o,b,h,w,g.
1345 The raison d'etre of this function is to consolidate printing of
581e13c1 1346 LONG_LONG's into this one function. The format chars b,h,w,g are
bb599908 1347 from print_scalar_formatted(). Numbers are printed using C
581e13c1 1348 format.
bb599908
PH
1349
1350 USE_C_FORMAT means to use C format in all cases. Without it,
1351 'o' and 'x' format do not include the standard C radix prefix
1352 (leading 0 or 0x).
1353
1354 Hilfinger/2004-09-09: USE_C_FORMAT was originally called USE_LOCAL
1355 and was intended to request formating according to the current
1356 language and would be used for most integers that GDB prints. The
1357 exceptional cases were things like protocols where the format of
1358 the integer is a protocol thing, not a user-visible thing). The
1359 parameter remains to preserve the information of what things might
1360 be printed with language-specific format, should we ever resurrect
581e13c1 1361 that capability. */
c906108c
SS
1362
1363void
bb599908 1364print_longest (struct ui_file *stream, int format, int use_c_format,
fba45db2 1365 LONGEST val_long)
c906108c 1366{
2bfb72ee
AC
1367 const char *val;
1368
c906108c
SS
1369 switch (format)
1370 {
1371 case 'd':
bb599908 1372 val = int_string (val_long, 10, 1, 0, 1); break;
c906108c 1373 case 'u':
bb599908 1374 val = int_string (val_long, 10, 0, 0, 1); break;
c906108c 1375 case 'x':
bb599908 1376 val = int_string (val_long, 16, 0, 0, use_c_format); break;
c906108c 1377 case 'b':
bb599908 1378 val = int_string (val_long, 16, 0, 2, 1); break;
c906108c 1379 case 'h':
bb599908 1380 val = int_string (val_long, 16, 0, 4, 1); break;
c906108c 1381 case 'w':
bb599908 1382 val = int_string (val_long, 16, 0, 8, 1); break;
c906108c 1383 case 'g':
bb599908 1384 val = int_string (val_long, 16, 0, 16, 1); break;
c906108c
SS
1385 break;
1386 case 'o':
bb599908 1387 val = int_string (val_long, 8, 0, 0, use_c_format); break;
c906108c 1388 default:
3e43a32a
MS
1389 internal_error (__FILE__, __LINE__,
1390 _("failed internal consistency check"));
bb599908 1391 }
2bfb72ee 1392 fputs_filtered (val, stream);
c906108c
SS
1393}
1394
c906108c
SS
1395/* This used to be a macro, but I don't think it is called often enough
1396 to merit such treatment. */
1397/* Convert a LONGEST to an int. This is used in contexts (e.g. number of
1398 arguments to a function, number in a value history, register number, etc.)
1399 where the value must not be larger than can fit in an int. */
1400
1401int
fba45db2 1402longest_to_int (LONGEST arg)
c906108c 1403{
581e13c1 1404 /* Let the compiler do the work. */
c906108c
SS
1405 int rtnval = (int) arg;
1406
581e13c1 1407 /* Check for overflows or underflows. */
c906108c
SS
1408 if (sizeof (LONGEST) > sizeof (int))
1409 {
1410 if (rtnval != arg)
1411 {
8a3fe4f8 1412 error (_("Value out of range."));
c906108c
SS
1413 }
1414 }
1415 return (rtnval);
1416}
1417
a73c86fb
AC
1418/* Print a floating point value of type TYPE (not always a
1419 TYPE_CODE_FLT), pointed to in GDB by VALADDR, on STREAM. */
c906108c
SS
1420
1421void
fc1a4b47 1422print_floating (const gdb_byte *valaddr, struct type *type,
c84141d6 1423 struct ui_file *stream)
c906108c
SS
1424{
1425 DOUBLEST doub;
1426 int inv;
a73c86fb 1427 const struct floatformat *fmt = NULL;
c906108c 1428 unsigned len = TYPE_LENGTH (type);
20389057 1429 enum float_kind kind;
c5aa993b 1430
a73c86fb
AC
1431 /* If it is a floating-point, check for obvious problems. */
1432 if (TYPE_CODE (type) == TYPE_CODE_FLT)
1433 fmt = floatformat_from_type (type);
20389057 1434 if (fmt != NULL)
39424bef 1435 {
20389057
DJ
1436 kind = floatformat_classify (fmt, valaddr);
1437 if (kind == float_nan)
1438 {
1439 if (floatformat_is_negative (fmt, valaddr))
1440 fprintf_filtered (stream, "-");
1441 fprintf_filtered (stream, "nan(");
1442 fputs_filtered ("0x", stream);
1443 fputs_filtered (floatformat_mantissa (fmt, valaddr), stream);
1444 fprintf_filtered (stream, ")");
1445 return;
1446 }
1447 else if (kind == float_infinite)
1448 {
1449 if (floatformat_is_negative (fmt, valaddr))
1450 fputs_filtered ("-", stream);
1451 fputs_filtered ("inf", stream);
1452 return;
1453 }
7355ddba 1454 }
c906108c 1455
a73c86fb
AC
1456 /* NOTE: cagney/2002-01-15: The TYPE passed into print_floating()
1457 isn't necessarily a TYPE_CODE_FLT. Consequently, unpack_double
1458 needs to be used as that takes care of any necessary type
1459 conversions. Such conversions are of course direct to DOUBLEST
1460 and disregard any possible target floating point limitations.
1461 For instance, a u64 would be converted and displayed exactly on a
1462 host with 80 bit DOUBLEST but with loss of information on a host
1463 with 64 bit DOUBLEST. */
c2f05ac9 1464
c906108c
SS
1465 doub = unpack_double (type, valaddr, &inv);
1466 if (inv)
1467 {
1468 fprintf_filtered (stream, "<invalid float value>");
1469 return;
1470 }
1471
39424bef
MK
1472 /* FIXME: kettenis/2001-01-20: The following code makes too much
1473 assumptions about the host and target floating point format. */
1474
a73c86fb 1475 /* NOTE: cagney/2002-02-03: Since the TYPE of what was passed in may
c41b8590 1476 not necessarily be a TYPE_CODE_FLT, the below ignores that and
a73c86fb
AC
1477 instead uses the type's length to determine the precision of the
1478 floating-point value being printed. */
c2f05ac9 1479
c906108c 1480 if (len < sizeof (double))
c5aa993b 1481 fprintf_filtered (stream, "%.9g", (double) doub);
c906108c 1482 else if (len == sizeof (double))
c5aa993b 1483 fprintf_filtered (stream, "%.17g", (double) doub);
c906108c
SS
1484 else
1485#ifdef PRINTF_HAS_LONG_DOUBLE
1486 fprintf_filtered (stream, "%.35Lg", doub);
1487#else
39424bef
MK
1488 /* This at least wins with values that are representable as
1489 doubles. */
c906108c
SS
1490 fprintf_filtered (stream, "%.17g", (double) doub);
1491#endif
1492}
1493
7678ef8f
TJB
1494void
1495print_decimal_floating (const gdb_byte *valaddr, struct type *type,
1496 struct ui_file *stream)
1497{
e17a4113 1498 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
7678ef8f
TJB
1499 char decstr[MAX_DECIMAL_STRING];
1500 unsigned len = TYPE_LENGTH (type);
1501
e17a4113 1502 decimal_to_string (valaddr, len, byte_order, decstr);
7678ef8f
TJB
1503 fputs_filtered (decstr, stream);
1504 return;
1505}
1506
c5aa993b 1507void
fc1a4b47 1508print_binary_chars (struct ui_file *stream, const gdb_byte *valaddr,
d44e8473 1509 unsigned len, enum bfd_endian byte_order)
c906108c
SS
1510{
1511
1512#define BITS_IN_BYTES 8
1513
fc1a4b47 1514 const gdb_byte *p;
745b8ca0 1515 unsigned int i;
c5aa993b 1516 int b;
c906108c
SS
1517
1518 /* Declared "int" so it will be signed.
581e13c1
MS
1519 This ensures that right shift will shift in zeros. */
1520
c5aa993b 1521 const int mask = 0x080;
c906108c
SS
1522
1523 /* FIXME: We should be not printing leading zeroes in most cases. */
1524
d44e8473 1525 if (byte_order == BFD_ENDIAN_BIG)
c906108c
SS
1526 {
1527 for (p = valaddr;
1528 p < valaddr + len;
1529 p++)
1530 {
c5aa993b 1531 /* Every byte has 8 binary characters; peel off
581e13c1
MS
1532 and print from the MSB end. */
1533
c5aa993b
JM
1534 for (i = 0; i < (BITS_IN_BYTES * sizeof (*p)); i++)
1535 {
1536 if (*p & (mask >> i))
1537 b = 1;
1538 else
1539 b = 0;
1540
1541 fprintf_filtered (stream, "%1d", b);
1542 }
c906108c
SS
1543 }
1544 }
1545 else
1546 {
1547 for (p = valaddr + len - 1;
1548 p >= valaddr;
1549 p--)
1550 {
c5aa993b
JM
1551 for (i = 0; i < (BITS_IN_BYTES * sizeof (*p)); i++)
1552 {
1553 if (*p & (mask >> i))
1554 b = 1;
1555 else
1556 b = 0;
1557
1558 fprintf_filtered (stream, "%1d", b);
1559 }
c906108c
SS
1560 }
1561 }
c906108c
SS
1562}
1563
1564/* VALADDR points to an integer of LEN bytes.
581e13c1
MS
1565 Print it in octal on stream or format it in buf. */
1566
c906108c 1567void
fc1a4b47 1568print_octal_chars (struct ui_file *stream, const gdb_byte *valaddr,
d44e8473 1569 unsigned len, enum bfd_endian byte_order)
c906108c 1570{
fc1a4b47 1571 const gdb_byte *p;
c906108c 1572 unsigned char octa1, octa2, octa3, carry;
c5aa993b
JM
1573 int cycle;
1574
c906108c
SS
1575 /* FIXME: We should be not printing leading zeroes in most cases. */
1576
1577
1578 /* Octal is 3 bits, which doesn't fit. Yuk. So we have to track
1579 * the extra bits, which cycle every three bytes:
1580 *
1581 * Byte side: 0 1 2 3
1582 * | | | |
1583 * bit number 123 456 78 | 9 012 345 6 | 78 901 234 | 567 890 12 |
1584 *
1585 * Octal side: 0 1 carry 3 4 carry ...
1586 *
1587 * Cycle number: 0 1 2
1588 *
1589 * But of course we are printing from the high side, so we have to
1590 * figure out where in the cycle we are so that we end up with no
1591 * left over bits at the end.
1592 */
1593#define BITS_IN_OCTAL 3
1594#define HIGH_ZERO 0340
1595#define LOW_ZERO 0016
1596#define CARRY_ZERO 0003
1597#define HIGH_ONE 0200
1598#define MID_ONE 0160
1599#define LOW_ONE 0016
1600#define CARRY_ONE 0001
1601#define HIGH_TWO 0300
1602#define MID_TWO 0070
1603#define LOW_TWO 0007
1604
1605 /* For 32 we start in cycle 2, with two bits and one bit carry;
581e13c1
MS
1606 for 64 in cycle in cycle 1, with one bit and a two bit carry. */
1607
c906108c
SS
1608 cycle = (len * BITS_IN_BYTES) % BITS_IN_OCTAL;
1609 carry = 0;
c5aa993b 1610
bb599908 1611 fputs_filtered ("0", stream);
d44e8473 1612 if (byte_order == BFD_ENDIAN_BIG)
c906108c
SS
1613 {
1614 for (p = valaddr;
1615 p < valaddr + len;
1616 p++)
1617 {
c5aa993b
JM
1618 switch (cycle)
1619 {
1620 case 0:
581e13c1
MS
1621 /* No carry in, carry out two bits. */
1622
c5aa993b
JM
1623 octa1 = (HIGH_ZERO & *p) >> 5;
1624 octa2 = (LOW_ZERO & *p) >> 2;
1625 carry = (CARRY_ZERO & *p);
1626 fprintf_filtered (stream, "%o", octa1);
1627 fprintf_filtered (stream, "%o", octa2);
1628 break;
1629
1630 case 1:
581e13c1
MS
1631 /* Carry in two bits, carry out one bit. */
1632
c5aa993b
JM
1633 octa1 = (carry << 1) | ((HIGH_ONE & *p) >> 7);
1634 octa2 = (MID_ONE & *p) >> 4;
1635 octa3 = (LOW_ONE & *p) >> 1;
1636 carry = (CARRY_ONE & *p);
1637 fprintf_filtered (stream, "%o", octa1);
1638 fprintf_filtered (stream, "%o", octa2);
1639 fprintf_filtered (stream, "%o", octa3);
1640 break;
1641
1642 case 2:
581e13c1
MS
1643 /* Carry in one bit, no carry out. */
1644
c5aa993b
JM
1645 octa1 = (carry << 2) | ((HIGH_TWO & *p) >> 6);
1646 octa2 = (MID_TWO & *p) >> 3;
1647 octa3 = (LOW_TWO & *p);
1648 carry = 0;
1649 fprintf_filtered (stream, "%o", octa1);
1650 fprintf_filtered (stream, "%o", octa2);
1651 fprintf_filtered (stream, "%o", octa3);
1652 break;
1653
1654 default:
8a3fe4f8 1655 error (_("Internal error in octal conversion;"));
c5aa993b
JM
1656 }
1657
1658 cycle++;
1659 cycle = cycle % BITS_IN_OCTAL;
c906108c
SS
1660 }
1661 }
1662 else
1663 {
1664 for (p = valaddr + len - 1;
1665 p >= valaddr;
1666 p--)
1667 {
c5aa993b
JM
1668 switch (cycle)
1669 {
1670 case 0:
1671 /* Carry out, no carry in */
581e13c1 1672
c5aa993b
JM
1673 octa1 = (HIGH_ZERO & *p) >> 5;
1674 octa2 = (LOW_ZERO & *p) >> 2;
1675 carry = (CARRY_ZERO & *p);
1676 fprintf_filtered (stream, "%o", octa1);
1677 fprintf_filtered (stream, "%o", octa2);
1678 break;
1679
1680 case 1:
1681 /* Carry in, carry out */
581e13c1 1682
c5aa993b
JM
1683 octa1 = (carry << 1) | ((HIGH_ONE & *p) >> 7);
1684 octa2 = (MID_ONE & *p) >> 4;
1685 octa3 = (LOW_ONE & *p) >> 1;
1686 carry = (CARRY_ONE & *p);
1687 fprintf_filtered (stream, "%o", octa1);
1688 fprintf_filtered (stream, "%o", octa2);
1689 fprintf_filtered (stream, "%o", octa3);
1690 break;
1691
1692 case 2:
1693 /* Carry in, no carry out */
581e13c1 1694
c5aa993b
JM
1695 octa1 = (carry << 2) | ((HIGH_TWO & *p) >> 6);
1696 octa2 = (MID_TWO & *p) >> 3;
1697 octa3 = (LOW_TWO & *p);
1698 carry = 0;
1699 fprintf_filtered (stream, "%o", octa1);
1700 fprintf_filtered (stream, "%o", octa2);
1701 fprintf_filtered (stream, "%o", octa3);
1702 break;
1703
1704 default:
8a3fe4f8 1705 error (_("Internal error in octal conversion;"));
c5aa993b
JM
1706 }
1707
1708 cycle++;
1709 cycle = cycle % BITS_IN_OCTAL;
c906108c
SS
1710 }
1711 }
1712
c906108c
SS
1713}
1714
1715/* VALADDR points to an integer of LEN bytes.
581e13c1
MS
1716 Print it in decimal on stream or format it in buf. */
1717
c906108c 1718void
fc1a4b47 1719print_decimal_chars (struct ui_file *stream, const gdb_byte *valaddr,
d44e8473 1720 unsigned len, enum bfd_endian byte_order)
c906108c
SS
1721{
1722#define TEN 10
c5aa993b 1723#define CARRY_OUT( x ) ((x) / TEN) /* extend char to int */
c906108c
SS
1724#define CARRY_LEFT( x ) ((x) % TEN)
1725#define SHIFT( x ) ((x) << 4)
c906108c
SS
1726#define LOW_NIBBLE( x ) ( (x) & 0x00F)
1727#define HIGH_NIBBLE( x ) (((x) & 0x0F0) >> 4)
1728
fc1a4b47 1729 const gdb_byte *p;
c906108c 1730 unsigned char *digits;
c5aa993b
JM
1731 int carry;
1732 int decimal_len;
1733 int i, j, decimal_digits;
1734 int dummy;
1735 int flip;
1736
c906108c 1737 /* Base-ten number is less than twice as many digits
581e13c1
MS
1738 as the base 16 number, which is 2 digits per byte. */
1739
c906108c 1740 decimal_len = len * 2 * 2;
224c3ddb 1741 digits = (unsigned char *) xmalloc (decimal_len);
c906108c 1742
c5aa993b
JM
1743 for (i = 0; i < decimal_len; i++)
1744 {
c906108c 1745 digits[i] = 0;
c5aa993b 1746 }
c906108c 1747
c906108c
SS
1748 /* Ok, we have an unknown number of bytes of data to be printed in
1749 * decimal.
1750 *
1751 * Given a hex number (in nibbles) as XYZ, we start by taking X and
1752 * decemalizing it as "x1 x2" in two decimal nibbles. Then we multiply
1753 * the nibbles by 16, add Y and re-decimalize. Repeat with Z.
1754 *
1755 * The trick is that "digits" holds a base-10 number, but sometimes
581e13c1 1756 * the individual digits are > 10.
c906108c
SS
1757 *
1758 * Outer loop is per nibble (hex digit) of input, from MSD end to
1759 * LSD end.
1760 */
c5aa993b 1761 decimal_digits = 0; /* Number of decimal digits so far */
d44e8473 1762 p = (byte_order == BFD_ENDIAN_BIG) ? valaddr : valaddr + len - 1;
c906108c 1763 flip = 0;
d44e8473 1764 while ((byte_order == BFD_ENDIAN_BIG) ? (p < valaddr + len) : (p >= valaddr))
c5aa993b 1765 {
c906108c
SS
1766 /*
1767 * Multiply current base-ten number by 16 in place.
1768 * Each digit was between 0 and 9, now is between
1769 * 0 and 144.
1770 */
c5aa993b
JM
1771 for (j = 0; j < decimal_digits; j++)
1772 {
1773 digits[j] = SHIFT (digits[j]);
1774 }
1775
c906108c
SS
1776 /* Take the next nibble off the input and add it to what
1777 * we've got in the LSB position. Bottom 'digit' is now
1778 * between 0 and 159.
1779 *
1780 * "flip" is used to run this loop twice for each byte.
1781 */
c5aa993b
JM
1782 if (flip == 0)
1783 {
581e13c1
MS
1784 /* Take top nibble. */
1785
c5aa993b
JM
1786 digits[0] += HIGH_NIBBLE (*p);
1787 flip = 1;
1788 }
1789 else
1790 {
581e13c1
MS
1791 /* Take low nibble and bump our pointer "p". */
1792
c5aa993b 1793 digits[0] += LOW_NIBBLE (*p);
d44e8473
MD
1794 if (byte_order == BFD_ENDIAN_BIG)
1795 p++;
1796 else
1797 p--;
c5aa993b
JM
1798 flip = 0;
1799 }
c906108c
SS
1800
1801 /* Re-decimalize. We have to do this often enough
1802 * that we don't overflow, but once per nibble is
1803 * overkill. Easier this way, though. Note that the
1804 * carry is often larger than 10 (e.g. max initial
1805 * carry out of lowest nibble is 15, could bubble all
1806 * the way up greater than 10). So we have to do
1807 * the carrying beyond the last current digit.
1808 */
1809 carry = 0;
c5aa993b
JM
1810 for (j = 0; j < decimal_len - 1; j++)
1811 {
1812 digits[j] += carry;
1813
1814 /* "/" won't handle an unsigned char with
1815 * a value that if signed would be negative.
1816 * So extend to longword int via "dummy".
1817 */
1818 dummy = digits[j];
1819 carry = CARRY_OUT (dummy);
1820 digits[j] = CARRY_LEFT (dummy);
1821
1822 if (j >= decimal_digits && carry == 0)
1823 {
1824 /*
1825 * All higher digits are 0 and we
1826 * no longer have a carry.
1827 *
1828 * Note: "j" is 0-based, "decimal_digits" is
1829 * 1-based.
1830 */
1831 decimal_digits = j + 1;
1832 break;
1833 }
1834 }
1835 }
c906108c
SS
1836
1837 /* Ok, now "digits" is the decimal representation, with
581e13c1
MS
1838 the "decimal_digits" actual digits. Print! */
1839
c5aa993b
JM
1840 for (i = decimal_digits - 1; i >= 0; i--)
1841 {
1842 fprintf_filtered (stream, "%1d", digits[i]);
1843 }
b8c9b27d 1844 xfree (digits);
c906108c
SS
1845}
1846
1847/* VALADDR points to an integer of LEN bytes. Print it in hex on stream. */
1848
6b9acc27 1849void
fc1a4b47 1850print_hex_chars (struct ui_file *stream, const gdb_byte *valaddr,
d44e8473 1851 unsigned len, enum bfd_endian byte_order)
c906108c 1852{
fc1a4b47 1853 const gdb_byte *p;
c906108c
SS
1854
1855 /* FIXME: We should be not printing leading zeroes in most cases. */
1856
bb599908 1857 fputs_filtered ("0x", stream);
d44e8473 1858 if (byte_order == BFD_ENDIAN_BIG)
c906108c
SS
1859 {
1860 for (p = valaddr;
1861 p < valaddr + len;
1862 p++)
1863 {
1864 fprintf_filtered (stream, "%02x", *p);
1865 }
1866 }
1867 else
1868 {
1869 for (p = valaddr + len - 1;
1870 p >= valaddr;
1871 p--)
1872 {
1873 fprintf_filtered (stream, "%02x", *p);
1874 }
1875 }
c906108c
SS
1876}
1877
3e43a32a 1878/* VALADDR points to a char integer of LEN bytes.
581e13c1 1879 Print it out in appropriate language form on stream.
6b9acc27
JJ
1880 Omit any leading zero chars. */
1881
1882void
6c7a06a3
TT
1883print_char_chars (struct ui_file *stream, struct type *type,
1884 const gdb_byte *valaddr,
d44e8473 1885 unsigned len, enum bfd_endian byte_order)
6b9acc27 1886{
fc1a4b47 1887 const gdb_byte *p;
6b9acc27 1888
d44e8473 1889 if (byte_order == BFD_ENDIAN_BIG)
6b9acc27
JJ
1890 {
1891 p = valaddr;
1892 while (p < valaddr + len - 1 && *p == 0)
1893 ++p;
1894
1895 while (p < valaddr + len)
1896 {
6c7a06a3 1897 LA_EMIT_CHAR (*p, type, stream, '\'');
6b9acc27
JJ
1898 ++p;
1899 }
1900 }
1901 else
1902 {
1903 p = valaddr + len - 1;
1904 while (p > valaddr && *p == 0)
1905 --p;
1906
1907 while (p >= valaddr)
1908 {
6c7a06a3 1909 LA_EMIT_CHAR (*p, type, stream, '\'');
6b9acc27
JJ
1910 --p;
1911 }
1912 }
1913}
1914
132c57b4
TT
1915/* Print function pointer with inferior address ADDRESS onto stdio
1916 stream STREAM. */
1917
1918void
edf0c1b7
TT
1919print_function_pointer_address (const struct value_print_options *options,
1920 struct gdbarch *gdbarch,
132c57b4 1921 CORE_ADDR address,
edf0c1b7 1922 struct ui_file *stream)
132c57b4
TT
1923{
1924 CORE_ADDR func_addr
1925 = gdbarch_convert_from_func_ptr_addr (gdbarch, address,
1926 &current_target);
1927
1928 /* If the function pointer is represented by a description, print
1929 the address of the description. */
edf0c1b7 1930 if (options->addressprint && func_addr != address)
132c57b4
TT
1931 {
1932 fputs_filtered ("@", stream);
1933 fputs_filtered (paddress (gdbarch, address), stream);
1934 fputs_filtered (": ", stream);
1935 }
edf0c1b7 1936 print_address_demangle (options, gdbarch, func_addr, stream, demangle);
132c57b4
TT
1937}
1938
1939
79a45b7d 1940/* Print on STREAM using the given OPTIONS the index for the element
e79af960
JB
1941 at INDEX of an array whose index type is INDEX_TYPE. */
1942
1943void
1944maybe_print_array_index (struct type *index_type, LONGEST index,
79a45b7d
TT
1945 struct ui_file *stream,
1946 const struct value_print_options *options)
e79af960
JB
1947{
1948 struct value *index_value;
1949
79a45b7d 1950 if (!options->print_array_indexes)
e79af960
JB
1951 return;
1952
1953 index_value = value_from_longest (index_type, index);
1954
79a45b7d
TT
1955 LA_PRINT_ARRAY_INDEX (index_value, stream, options);
1956}
e79af960 1957
c906108c 1958/* Called by various <lang>_val_print routines to print elements of an
c5aa993b 1959 array in the form "<elem1>, <elem2>, <elem3>, ...".
c906108c 1960
c5aa993b
JM
1961 (FIXME?) Assumes array element separator is a comma, which is correct
1962 for all languages currently handled.
1963 (FIXME?) Some languages have a notation for repeated array elements,
581e13c1 1964 perhaps we should try to use that notation when appropriate. */
c906108c
SS
1965
1966void
490f124f 1967val_print_array_elements (struct type *type,
6b850546 1968 const gdb_byte *valaddr, LONGEST embedded_offset,
a2bd3dcd 1969 CORE_ADDR address, struct ui_file *stream,
79a45b7d 1970 int recurse,
0e03807e 1971 const struct value *val,
79a45b7d 1972 const struct value_print_options *options,
fba45db2 1973 unsigned int i)
c906108c
SS
1974{
1975 unsigned int things_printed = 0;
1976 unsigned len;
aa715135 1977 struct type *elttype, *index_type, *base_index_type;
c906108c
SS
1978 unsigned eltlen;
1979 /* Position of the array element we are examining to see
1980 whether it is repeated. */
1981 unsigned int rep1;
1982 /* Number of repetitions we have detected so far. */
1983 unsigned int reps;
dbc98a8b 1984 LONGEST low_bound, high_bound;
aa715135 1985 LONGEST low_pos, high_pos;
c5aa993b 1986
c906108c 1987 elttype = TYPE_TARGET_TYPE (type);
3ae385af 1988 eltlen = type_length_units (check_typedef (elttype));
e79af960 1989 index_type = TYPE_INDEX_TYPE (type);
c906108c 1990
dbc98a8b 1991 if (get_array_bounds (type, &low_bound, &high_bound))
75be741b 1992 {
aa715135
JG
1993 if (TYPE_CODE (index_type) == TYPE_CODE_RANGE)
1994 base_index_type = TYPE_TARGET_TYPE (index_type);
1995 else
1996 base_index_type = index_type;
1997
1998 /* Non-contiguous enumerations types can by used as index types
1999 in some languages (e.g. Ada). In this case, the array length
2000 shall be computed from the positions of the first and last
2001 literal in the enumeration type, and not from the values
2002 of these literals. */
2003 if (!discrete_position (base_index_type, low_bound, &low_pos)
2004 || !discrete_position (base_index_type, high_bound, &high_pos))
2005 {
2006 warning (_("unable to get positions in array, use bounds instead"));
2007 low_pos = low_bound;
2008 high_pos = high_bound;
2009 }
2010
2011 /* The array length should normally be HIGH_POS - LOW_POS + 1.
75be741b 2012 But we have to be a little extra careful, because some languages
aa715135 2013 such as Ada allow LOW_POS to be greater than HIGH_POS for
75be741b
JB
2014 empty arrays. In that situation, the array length is just zero,
2015 not negative! */
aa715135 2016 if (low_pos > high_pos)
75be741b
JB
2017 len = 0;
2018 else
aa715135 2019 len = high_pos - low_pos + 1;
75be741b 2020 }
e936309c
JB
2021 else
2022 {
dbc98a8b
KW
2023 warning (_("unable to get bounds of array, assuming null array"));
2024 low_bound = 0;
2025 len = 0;
168de233
JB
2026 }
2027
c906108c
SS
2028 annotate_array_section_begin (i, elttype);
2029
79a45b7d 2030 for (; i < len && things_printed < options->print_max; i++)
c906108c
SS
2031 {
2032 if (i != 0)
2033 {
2a998fc0 2034 if (options->prettyformat_arrays)
c906108c
SS
2035 {
2036 fprintf_filtered (stream, ",\n");
2037 print_spaces_filtered (2 + 2 * recurse, stream);
2038 }
2039 else
2040 {
2041 fprintf_filtered (stream, ", ");
2042 }
2043 }
2044 wrap_here (n_spaces (2 + 2 * recurse));
dbc98a8b 2045 maybe_print_array_index (index_type, i + low_bound,
79a45b7d 2046 stream, options);
c906108c
SS
2047
2048 rep1 = i + 1;
2049 reps = 1;
35bef4fd
TT
2050 /* Only check for reps if repeat_count_threshold is not set to
2051 UINT_MAX (unlimited). */
2052 if (options->repeat_count_threshold < UINT_MAX)
c906108c 2053 {
35bef4fd 2054 while (rep1 < len
9a0dc9e3
PA
2055 && value_contents_eq (val,
2056 embedded_offset + i * eltlen,
2057 val,
2058 (embedded_offset
2059 + rep1 * eltlen),
2060 eltlen))
35bef4fd
TT
2061 {
2062 ++reps;
2063 ++rep1;
2064 }
c906108c
SS
2065 }
2066
79a45b7d 2067 if (reps > options->repeat_count_threshold)
c906108c 2068 {
490f124f
PA
2069 val_print (elttype, valaddr, embedded_offset + i * eltlen,
2070 address, stream, recurse + 1, val, options,
2071 current_language);
c906108c
SS
2072 annotate_elt_rep (reps);
2073 fprintf_filtered (stream, " <repeats %u times>", reps);
2074 annotate_elt_rep_end ();
2075
2076 i = rep1 - 1;
79a45b7d 2077 things_printed += options->repeat_count_threshold;
c906108c
SS
2078 }
2079 else
2080 {
490f124f
PA
2081 val_print (elttype, valaddr, embedded_offset + i * eltlen,
2082 address,
0e03807e 2083 stream, recurse + 1, val, options, current_language);
c906108c
SS
2084 annotate_elt ();
2085 things_printed++;
2086 }
2087 }
2088 annotate_array_section_end ();
2089 if (i < len)
2090 {
2091 fprintf_filtered (stream, "...");
2092 }
2093}
2094
917317f4
JM
2095/* Read LEN bytes of target memory at address MEMADDR, placing the
2096 results in GDB's memory at MYADDR. Returns a count of the bytes
9b409511 2097 actually read, and optionally a target_xfer_status value in the
578d3588 2098 location pointed to by ERRPTR if ERRPTR is non-null. */
917317f4
JM
2099
2100/* FIXME: cagney/1999-10-14: Only used by val_print_string. Can this
2101 function be eliminated. */
2102
2103static int
3e43a32a 2104partial_memory_read (CORE_ADDR memaddr, gdb_byte *myaddr,
578d3588 2105 int len, int *errptr)
917317f4 2106{
581e13c1
MS
2107 int nread; /* Number of bytes actually read. */
2108 int errcode; /* Error from last read. */
917317f4 2109
581e13c1 2110 /* First try a complete read. */
917317f4
JM
2111 errcode = target_read_memory (memaddr, myaddr, len);
2112 if (errcode == 0)
2113 {
581e13c1 2114 /* Got it all. */
917317f4
JM
2115 nread = len;
2116 }
2117 else
2118 {
581e13c1 2119 /* Loop, reading one byte at a time until we get as much as we can. */
917317f4
JM
2120 for (errcode = 0, nread = 0; len > 0 && errcode == 0; nread++, len--)
2121 {
2122 errcode = target_read_memory (memaddr++, myaddr++, 1);
2123 }
581e13c1 2124 /* If an error, the last read was unsuccessful, so adjust count. */
917317f4
JM
2125 if (errcode != 0)
2126 {
2127 nread--;
2128 }
2129 }
578d3588 2130 if (errptr != NULL)
917317f4 2131 {
578d3588 2132 *errptr = errcode;
917317f4
JM
2133 }
2134 return (nread);
2135}
2136
ae6a3a4c
TJB
2137/* Read a string from the inferior, at ADDR, with LEN characters of WIDTH bytes
2138 each. Fetch at most FETCHLIMIT characters. BUFFER will be set to a newly
2139 allocated buffer containing the string, which the caller is responsible to
2140 free, and BYTES_READ will be set to the number of bytes read. Returns 0 on
9b409511 2141 success, or a target_xfer_status on failure.
ae6a3a4c 2142
f380848e
SA
2143 If LEN > 0, reads the lesser of LEN or FETCHLIMIT characters
2144 (including eventual NULs in the middle or end of the string).
2145
2146 If LEN is -1, stops at the first null character (not necessarily
2147 the first null byte) up to a maximum of FETCHLIMIT characters. Set
2148 FETCHLIMIT to UINT_MAX to read as many characters as possible from
2149 the string.
ae6a3a4c
TJB
2150
2151 Unless an exception is thrown, BUFFER will always be allocated, even on
2152 failure. In this case, some characters might have been read before the
2153 failure happened. Check BYTES_READ to recognize this situation.
2154
2155 Note: There was a FIXME asking to make this code use target_read_string,
2156 but this function is more general (can read past null characters, up to
581e13c1 2157 given LEN). Besides, it is used much more often than target_read_string
ae6a3a4c
TJB
2158 so it is more tested. Perhaps callers of target_read_string should use
2159 this function instead? */
c906108c
SS
2160
2161int
ae6a3a4c 2162read_string (CORE_ADDR addr, int len, int width, unsigned int fetchlimit,
e17a4113 2163 enum bfd_endian byte_order, gdb_byte **buffer, int *bytes_read)
c906108c 2164{
ae6a3a4c
TJB
2165 int errcode; /* Errno returned from bad reads. */
2166 unsigned int nfetch; /* Chars to fetch / chars fetched. */
3e43a32a
MS
2167 gdb_byte *bufptr; /* Pointer to next available byte in
2168 buffer. */
ae6a3a4c
TJB
2169 struct cleanup *old_chain = NULL; /* Top of the old cleanup chain. */
2170
ae6a3a4c
TJB
2171 /* Loop until we either have all the characters, or we encounter
2172 some error, such as bumping into the end of the address space. */
c906108c 2173
b5096abe
PM
2174 *buffer = NULL;
2175
2176 old_chain = make_cleanup (free_current_contents, buffer);
c906108c
SS
2177
2178 if (len > 0)
2179 {
88db67ef
YQ
2180 /* We want fetchlimit chars, so we might as well read them all in
2181 one operation. */
325fac50 2182 unsigned int fetchlen = std::min ((unsigned) len, fetchlimit);
f380848e
SA
2183
2184 *buffer = (gdb_byte *) xmalloc (fetchlen * width);
ae6a3a4c 2185 bufptr = *buffer;
c906108c 2186
f380848e 2187 nfetch = partial_memory_read (addr, bufptr, fetchlen * width, &errcode)
c906108c
SS
2188 / width;
2189 addr += nfetch * width;
2190 bufptr += nfetch * width;
2191 }
2192 else if (len == -1)
2193 {
2194 unsigned long bufsize = 0;
88db67ef
YQ
2195 unsigned int chunksize; /* Size of each fetch, in chars. */
2196 int found_nul; /* Non-zero if we found the nul char. */
2197 gdb_byte *limit; /* First location past end of fetch buffer. */
2198
2199 found_nul = 0;
2200 /* We are looking for a NUL terminator to end the fetching, so we
2201 might as well read in blocks that are large enough to be efficient,
2202 but not so large as to be slow if fetchlimit happens to be large.
2203 So we choose the minimum of 8 and fetchlimit. We used to use 200
2204 instead of 8 but 200 is way too big for remote debugging over a
2205 serial line. */
325fac50 2206 chunksize = std::min (8u, fetchlimit);
ae6a3a4c 2207
c906108c
SS
2208 do
2209 {
2210 QUIT;
325fac50 2211 nfetch = std::min ((unsigned long) chunksize, fetchlimit - bufsize);
c906108c 2212
ae6a3a4c
TJB
2213 if (*buffer == NULL)
2214 *buffer = (gdb_byte *) xmalloc (nfetch * width);
c906108c 2215 else
b5096abe
PM
2216 *buffer = (gdb_byte *) xrealloc (*buffer,
2217 (nfetch + bufsize) * width);
c906108c 2218
ae6a3a4c 2219 bufptr = *buffer + bufsize * width;
c906108c
SS
2220 bufsize += nfetch;
2221
ae6a3a4c 2222 /* Read as much as we can. */
917317f4 2223 nfetch = partial_memory_read (addr, bufptr, nfetch * width, &errcode)
ae6a3a4c 2224 / width;
c906108c 2225
ae6a3a4c 2226 /* Scan this chunk for the null character that terminates the string
c906108c
SS
2227 to print. If found, we don't need to fetch any more. Note
2228 that bufptr is explicitly left pointing at the next character
ae6a3a4c
TJB
2229 after the null character, or at the next character after the end
2230 of the buffer. */
c906108c
SS
2231
2232 limit = bufptr + nfetch * width;
2233 while (bufptr < limit)
2234 {
2235 unsigned long c;
2236
e17a4113 2237 c = extract_unsigned_integer (bufptr, width, byte_order);
c906108c
SS
2238 addr += width;
2239 bufptr += width;
2240 if (c == 0)
2241 {
2242 /* We don't care about any error which happened after
ae6a3a4c 2243 the NUL terminator. */
c906108c
SS
2244 errcode = 0;
2245 found_nul = 1;
2246 break;
2247 }
2248 }
2249 }
c5aa993b 2250 while (errcode == 0 /* no error */
ae6a3a4c
TJB
2251 && bufptr - *buffer < fetchlimit * width /* no overrun */
2252 && !found_nul); /* haven't found NUL yet */
c906108c
SS
2253 }
2254 else
ae6a3a4c
TJB
2255 { /* Length of string is really 0! */
2256 /* We always allocate *buffer. */
224c3ddb 2257 *buffer = bufptr = (gdb_byte *) xmalloc (1);
c906108c
SS
2258 errcode = 0;
2259 }
2260
2261 /* bufptr and addr now point immediately beyond the last byte which we
2262 consider part of the string (including a '\0' which ends the string). */
ae6a3a4c
TJB
2263 *bytes_read = bufptr - *buffer;
2264
2265 QUIT;
2266
2267 discard_cleanups (old_chain);
2268
2269 return errcode;
2270}
2271
3b2b8fea
TT
2272/* Return true if print_wchar can display W without resorting to a
2273 numeric escape, false otherwise. */
2274
2275static int
2276wchar_printable (gdb_wchar_t w)
2277{
2278 return (gdb_iswprint (w)
2279 || w == LCST ('\a') || w == LCST ('\b')
2280 || w == LCST ('\f') || w == LCST ('\n')
2281 || w == LCST ('\r') || w == LCST ('\t')
2282 || w == LCST ('\v'));
2283}
2284
2285/* A helper function that converts the contents of STRING to wide
2286 characters and then appends them to OUTPUT. */
2287
2288static void
2289append_string_as_wide (const char *string,
2290 struct obstack *output)
2291{
2292 for (; *string; ++string)
2293 {
2294 gdb_wchar_t w = gdb_btowc (*string);
2295 obstack_grow (output, &w, sizeof (gdb_wchar_t));
2296 }
2297}
2298
2299/* Print a wide character W to OUTPUT. ORIG is a pointer to the
2300 original (target) bytes representing the character, ORIG_LEN is the
2301 number of valid bytes. WIDTH is the number of bytes in a base
2302 characters of the type. OUTPUT is an obstack to which wide
2303 characters are emitted. QUOTER is a (narrow) character indicating
2304 the style of quotes surrounding the character to be printed.
2305 NEED_ESCAPE is an in/out flag which is used to track numeric
2306 escapes across calls. */
2307
2308static void
2309print_wchar (gdb_wint_t w, const gdb_byte *orig,
2310 int orig_len, int width,
2311 enum bfd_endian byte_order,
2312 struct obstack *output,
2313 int quoter, int *need_escapep)
2314{
2315 int need_escape = *need_escapep;
2316
2317 *need_escapep = 0;
3b2b8fea 2318
95c64f92
YQ
2319 /* iswprint implementation on Windows returns 1 for tab character.
2320 In order to avoid different printout on this host, we explicitly
2321 use wchar_printable function. */
2322 switch (w)
3b2b8fea 2323 {
95c64f92
YQ
2324 case LCST ('\a'):
2325 obstack_grow_wstr (output, LCST ("\\a"));
2326 break;
2327 case LCST ('\b'):
2328 obstack_grow_wstr (output, LCST ("\\b"));
2329 break;
2330 case LCST ('\f'):
2331 obstack_grow_wstr (output, LCST ("\\f"));
2332 break;
2333 case LCST ('\n'):
2334 obstack_grow_wstr (output, LCST ("\\n"));
2335 break;
2336 case LCST ('\r'):
2337 obstack_grow_wstr (output, LCST ("\\r"));
2338 break;
2339 case LCST ('\t'):
2340 obstack_grow_wstr (output, LCST ("\\t"));
2341 break;
2342 case LCST ('\v'):
2343 obstack_grow_wstr (output, LCST ("\\v"));
2344 break;
2345 default:
3b2b8fea 2346 {
95c64f92
YQ
2347 if (wchar_printable (w) && (!need_escape || (!gdb_iswdigit (w)
2348 && w != LCST ('8')
2349 && w != LCST ('9'))))
2350 {
2351 gdb_wchar_t wchar = w;
3b2b8fea 2352
95c64f92
YQ
2353 if (w == gdb_btowc (quoter) || w == LCST ('\\'))
2354 obstack_grow_wstr (output, LCST ("\\"));
2355 obstack_grow (output, &wchar, sizeof (gdb_wchar_t));
2356 }
2357 else
2358 {
2359 int i;
3b2b8fea 2360
95c64f92
YQ
2361 for (i = 0; i + width <= orig_len; i += width)
2362 {
2363 char octal[30];
2364 ULONGEST value;
2365
2366 value = extract_unsigned_integer (&orig[i], width,
3b2b8fea 2367 byte_order);
95c64f92
YQ
2368 /* If the value fits in 3 octal digits, print it that
2369 way. Otherwise, print it as a hex escape. */
2370 if (value <= 0777)
2371 xsnprintf (octal, sizeof (octal), "\\%.3o",
2372 (int) (value & 0777));
2373 else
2374 xsnprintf (octal, sizeof (octal), "\\x%lx", (long) value);
2375 append_string_as_wide (octal, output);
2376 }
2377 /* If we somehow have extra bytes, print them now. */
2378 while (i < orig_len)
2379 {
2380 char octal[5];
2381
2382 xsnprintf (octal, sizeof (octal), "\\%.3o", orig[i] & 0xff);
2383 append_string_as_wide (octal, output);
2384 ++i;
2385 }
2386
2387 *need_escapep = 1;
2388 }
3b2b8fea
TT
2389 break;
2390 }
2391 }
2392}
2393
2394/* Print the character C on STREAM as part of the contents of a
2395 literal string whose delimiter is QUOTER. ENCODING names the
2396 encoding of C. */
2397
2398void
2399generic_emit_char (int c, struct type *type, struct ui_file *stream,
2400 int quoter, const char *encoding)
2401{
2402 enum bfd_endian byte_order
2403 = gdbarch_byte_order (get_type_arch (type));
2404 struct obstack wchar_buf, output;
2405 struct cleanup *cleanups;
2406 gdb_byte *buf;
2407 struct wchar_iterator *iter;
2408 int need_escape = 0;
2409
224c3ddb 2410 buf = (gdb_byte *) alloca (TYPE_LENGTH (type));
3b2b8fea
TT
2411 pack_long (buf, type, c);
2412
2413 iter = make_wchar_iterator (buf, TYPE_LENGTH (type),
2414 encoding, TYPE_LENGTH (type));
2415 cleanups = make_cleanup_wchar_iterator (iter);
2416
2417 /* This holds the printable form of the wchar_t data. */
2418 obstack_init (&wchar_buf);
2419 make_cleanup_obstack_free (&wchar_buf);
2420
2421 while (1)
2422 {
2423 int num_chars;
2424 gdb_wchar_t *chars;
2425 const gdb_byte *buf;
2426 size_t buflen;
2427 int print_escape = 1;
2428 enum wchar_iterate_result result;
2429
2430 num_chars = wchar_iterate (iter, &result, &chars, &buf, &buflen);
2431 if (num_chars < 0)
2432 break;
2433 if (num_chars > 0)
2434 {
2435 /* If all characters are printable, print them. Otherwise,
2436 we're going to have to print an escape sequence. We
2437 check all characters because we want to print the target
2438 bytes in the escape sequence, and we don't know character
2439 boundaries there. */
2440 int i;
2441
2442 print_escape = 0;
2443 for (i = 0; i < num_chars; ++i)
2444 if (!wchar_printable (chars[i]))
2445 {
2446 print_escape = 1;
2447 break;
2448 }
2449
2450 if (!print_escape)
2451 {
2452 for (i = 0; i < num_chars; ++i)
2453 print_wchar (chars[i], buf, buflen,
2454 TYPE_LENGTH (type), byte_order,
2455 &wchar_buf, quoter, &need_escape);
2456 }
2457 }
2458
2459 /* This handles the NUM_CHARS == 0 case as well. */
2460 if (print_escape)
2461 print_wchar (gdb_WEOF, buf, buflen, TYPE_LENGTH (type),
2462 byte_order, &wchar_buf, quoter, &need_escape);
2463 }
2464
2465 /* The output in the host encoding. */
2466 obstack_init (&output);
2467 make_cleanup_obstack_free (&output);
2468
2469 convert_between_encodings (INTERMEDIATE_ENCODING, host_charset (),
ac91cd70 2470 (gdb_byte *) obstack_base (&wchar_buf),
3b2b8fea 2471 obstack_object_size (&wchar_buf),
fff10684 2472 sizeof (gdb_wchar_t), &output, translit_char);
3b2b8fea
TT
2473 obstack_1grow (&output, '\0');
2474
79f33898 2475 fputs_filtered ((const char *) obstack_base (&output), stream);
3b2b8fea
TT
2476
2477 do_cleanups (cleanups);
2478}
2479
0d63ecda
KS
2480/* Return the repeat count of the next character/byte in ITER,
2481 storing the result in VEC. */
2482
2483static int
2484count_next_character (struct wchar_iterator *iter,
2485 VEC (converted_character_d) **vec)
2486{
2487 struct converted_character *current;
2488
2489 if (VEC_empty (converted_character_d, *vec))
2490 {
2491 struct converted_character tmp;
2492 gdb_wchar_t *chars;
2493
2494 tmp.num_chars
2495 = wchar_iterate (iter, &tmp.result, &chars, &tmp.buf, &tmp.buflen);
2496 if (tmp.num_chars > 0)
2497 {
2498 gdb_assert (tmp.num_chars < MAX_WCHARS);
2499 memcpy (tmp.chars, chars, tmp.num_chars * sizeof (gdb_wchar_t));
2500 }
2501 VEC_safe_push (converted_character_d, *vec, &tmp);
2502 }
2503
2504 current = VEC_last (converted_character_d, *vec);
2505
2506 /* Count repeated characters or bytes. */
2507 current->repeat_count = 1;
2508 if (current->num_chars == -1)
2509 {
2510 /* EOF */
2511 return -1;
2512 }
2513 else
2514 {
2515 gdb_wchar_t *chars;
2516 struct converted_character d;
2517 int repeat;
2518
2519 d.repeat_count = 0;
2520
2521 while (1)
2522 {
2523 /* Get the next character. */
2524 d.num_chars
2525 = wchar_iterate (iter, &d.result, &chars, &d.buf, &d.buflen);
2526
2527 /* If a character was successfully converted, save the character
2528 into the converted character. */
2529 if (d.num_chars > 0)
2530 {
2531 gdb_assert (d.num_chars < MAX_WCHARS);
2532 memcpy (d.chars, chars, WCHAR_BUFLEN (d.num_chars));
2533 }
2534
2535 /* Determine if the current character is the same as this
2536 new character. */
2537 if (d.num_chars == current->num_chars && d.result == current->result)
2538 {
2539 /* There are two cases to consider:
2540
2541 1) Equality of converted character (num_chars > 0)
2542 2) Equality of non-converted character (num_chars == 0) */
2543 if ((current->num_chars > 0
2544 && memcmp (current->chars, d.chars,
2545 WCHAR_BUFLEN (current->num_chars)) == 0)
2546 || (current->num_chars == 0
2547 && current->buflen == d.buflen
2548 && memcmp (current->buf, d.buf, current->buflen) == 0))
2549 ++current->repeat_count;
2550 else
2551 break;
2552 }
2553 else
2554 break;
2555 }
2556
2557 /* Push this next converted character onto the result vector. */
2558 repeat = current->repeat_count;
2559 VEC_safe_push (converted_character_d, *vec, &d);
2560 return repeat;
2561 }
2562}
2563
2564/* Print the characters in CHARS to the OBSTACK. QUOTE_CHAR is the quote
2565 character to use with string output. WIDTH is the size of the output
2566 character type. BYTE_ORDER is the the target byte order. OPTIONS
2567 is the user's print options. */
2568
2569static void
2570print_converted_chars_to_obstack (struct obstack *obstack,
2571 VEC (converted_character_d) *chars,
2572 int quote_char, int width,
2573 enum bfd_endian byte_order,
2574 const struct value_print_options *options)
2575{
2576 unsigned int idx;
2577 struct converted_character *elem;
2578 enum {START, SINGLE, REPEAT, INCOMPLETE, FINISH} state, last;
2579 gdb_wchar_t wide_quote_char = gdb_btowc (quote_char);
2580 int need_escape = 0;
2581
2582 /* Set the start state. */
2583 idx = 0;
2584 last = state = START;
2585 elem = NULL;
2586
2587 while (1)
2588 {
2589 switch (state)
2590 {
2591 case START:
2592 /* Nothing to do. */
2593 break;
2594
2595 case SINGLE:
2596 {
2597 int j;
2598
2599 /* We are outputting a single character
2600 (< options->repeat_count_threshold). */
2601
2602 if (last != SINGLE)
2603 {
2604 /* We were outputting some other type of content, so we
2605 must output and a comma and a quote. */
2606 if (last != START)
2607 obstack_grow_wstr (obstack, LCST (", "));
0d63ecda
KS
2608 obstack_grow (obstack, &wide_quote_char, sizeof (gdb_wchar_t));
2609 }
2610 /* Output the character. */
2611 for (j = 0; j < elem->repeat_count; ++j)
2612 {
2613 if (elem->result == wchar_iterate_ok)
2614 print_wchar (elem->chars[0], elem->buf, elem->buflen, width,
2615 byte_order, obstack, quote_char, &need_escape);
2616 else
2617 print_wchar (gdb_WEOF, elem->buf, elem->buflen, width,
2618 byte_order, obstack, quote_char, &need_escape);
2619 }
2620 }
2621 break;
2622
2623 case REPEAT:
2624 {
2625 int j;
2626 char *s;
2627
2628 /* We are outputting a character with a repeat count
2629 greater than options->repeat_count_threshold. */
2630
2631 if (last == SINGLE)
2632 {
2633 /* We were outputting a single string. Terminate the
2634 string. */
0d63ecda
KS
2635 obstack_grow (obstack, &wide_quote_char, sizeof (gdb_wchar_t));
2636 }
2637 if (last != START)
2638 obstack_grow_wstr (obstack, LCST (", "));
2639
2640 /* Output the character and repeat string. */
2641 obstack_grow_wstr (obstack, LCST ("'"));
2642 if (elem->result == wchar_iterate_ok)
2643 print_wchar (elem->chars[0], elem->buf, elem->buflen, width,
2644 byte_order, obstack, quote_char, &need_escape);
2645 else
2646 print_wchar (gdb_WEOF, elem->buf, elem->buflen, width,
2647 byte_order, obstack, quote_char, &need_escape);
2648 obstack_grow_wstr (obstack, LCST ("'"));
2649 s = xstrprintf (_(" <repeats %u times>"), elem->repeat_count);
2650 for (j = 0; s[j]; ++j)
2651 {
2652 gdb_wchar_t w = gdb_btowc (s[j]);
2653 obstack_grow (obstack, &w, sizeof (gdb_wchar_t));
2654 }
2655 xfree (s);
2656 }
2657 break;
2658
2659 case INCOMPLETE:
2660 /* We are outputting an incomplete sequence. */
2661 if (last == SINGLE)
2662 {
2663 /* If we were outputting a string of SINGLE characters,
2664 terminate the quote. */
0d63ecda
KS
2665 obstack_grow (obstack, &wide_quote_char, sizeof (gdb_wchar_t));
2666 }
2667 if (last != START)
2668 obstack_grow_wstr (obstack, LCST (", "));
2669
2670 /* Output the incomplete sequence string. */
2671 obstack_grow_wstr (obstack, LCST ("<incomplete sequence "));
2672 print_wchar (gdb_WEOF, elem->buf, elem->buflen, width, byte_order,
2673 obstack, 0, &need_escape);
2674 obstack_grow_wstr (obstack, LCST (">"));
2675
2676 /* We do not attempt to outupt anything after this. */
2677 state = FINISH;
2678 break;
2679
2680 case FINISH:
2681 /* All done. If we were outputting a string of SINGLE
2682 characters, the string must be terminated. Otherwise,
2683 REPEAT and INCOMPLETE are always left properly terminated. */
2684 if (last == SINGLE)
e93a8774 2685 obstack_grow (obstack, &wide_quote_char, sizeof (gdb_wchar_t));
0d63ecda
KS
2686
2687 return;
2688 }
2689
2690 /* Get the next element and state. */
2691 last = state;
2692 if (state != FINISH)
2693 {
2694 elem = VEC_index (converted_character_d, chars, idx++);
2695 switch (elem->result)
2696 {
2697 case wchar_iterate_ok:
2698 case wchar_iterate_invalid:
2699 if (elem->repeat_count > options->repeat_count_threshold)
2700 state = REPEAT;
2701 else
2702 state = SINGLE;
2703 break;
2704
2705 case wchar_iterate_incomplete:
2706 state = INCOMPLETE;
2707 break;
2708
2709 case wchar_iterate_eof:
2710 state = FINISH;
2711 break;
2712 }
2713 }
2714 }
2715}
2716
3b2b8fea
TT
2717/* Print the character string STRING, printing at most LENGTH
2718 characters. LENGTH is -1 if the string is nul terminated. TYPE is
2719 the type of each character. OPTIONS holds the printing options;
2720 printing stops early if the number hits print_max; repeat counts
2721 are printed as appropriate. Print ellipses at the end if we had to
2722 stop before printing LENGTH characters, or if FORCE_ELLIPSES.
2723 QUOTE_CHAR is the character to print at each end of the string. If
2724 C_STYLE_TERMINATOR is true, and the last character is 0, then it is
2725 omitted. */
2726
2727void
2728generic_printstr (struct ui_file *stream, struct type *type,
2729 const gdb_byte *string, unsigned int length,
2730 const char *encoding, int force_ellipses,
2731 int quote_char, int c_style_terminator,
2732 const struct value_print_options *options)
2733{
2734 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
2735 unsigned int i;
3b2b8fea
TT
2736 int width = TYPE_LENGTH (type);
2737 struct obstack wchar_buf, output;
2738 struct cleanup *cleanup;
2739 struct wchar_iterator *iter;
2740 int finished = 0;
0d63ecda
KS
2741 struct converted_character *last;
2742 VEC (converted_character_d) *converted_chars;
3b2b8fea
TT
2743
2744 if (length == -1)
2745 {
2746 unsigned long current_char = 1;
2747
2748 for (i = 0; current_char; ++i)
2749 {
2750 QUIT;
2751 current_char = extract_unsigned_integer (string + i * width,
2752 width, byte_order);
2753 }
2754 length = i;
2755 }
2756
2757 /* If the string was not truncated due to `set print elements', and
2758 the last byte of it is a null, we don't print that, in
2759 traditional C style. */
2760 if (c_style_terminator
2761 && !force_ellipses
2762 && length > 0
2763 && (extract_unsigned_integer (string + (length - 1) * width,
2764 width, byte_order) == 0))
2765 length--;
2766
2767 if (length == 0)
2768 {
2769 fputs_filtered ("\"\"", stream);
2770 return;
2771 }
2772
2773 /* Arrange to iterate over the characters, in wchar_t form. */
2774 iter = make_wchar_iterator (string, length * width, encoding, width);
2775 cleanup = make_cleanup_wchar_iterator (iter);
0d63ecda
KS
2776 converted_chars = NULL;
2777 make_cleanup (VEC_cleanup (converted_character_d), &converted_chars);
3b2b8fea 2778
0d63ecda
KS
2779 /* Convert characters until the string is over or the maximum
2780 number of printed characters has been reached. */
2781 i = 0;
2782 while (i < options->print_max)
3b2b8fea 2783 {
0d63ecda 2784 int r;
3b2b8fea
TT
2785
2786 QUIT;
2787
0d63ecda
KS
2788 /* Grab the next character and repeat count. */
2789 r = count_next_character (iter, &converted_chars);
3b2b8fea 2790
0d63ecda
KS
2791 /* If less than zero, the end of the input string was reached. */
2792 if (r < 0)
2793 break;
3b2b8fea 2794
0d63ecda
KS
2795 /* Otherwise, add the count to the total print count and get
2796 the next character. */
2797 i += r;
2798 }
3b2b8fea 2799
0d63ecda
KS
2800 /* Get the last element and determine if the entire string was
2801 processed. */
2802 last = VEC_last (converted_character_d, converted_chars);
2803 finished = (last->result == wchar_iterate_eof);
3b2b8fea 2804
0d63ecda
KS
2805 /* Ensure that CONVERTED_CHARS is terminated. */
2806 last->result = wchar_iterate_eof;
3b2b8fea 2807
0d63ecda
KS
2808 /* WCHAR_BUF is the obstack we use to represent the string in
2809 wchar_t form. */
2810 obstack_init (&wchar_buf);
2811 make_cleanup_obstack_free (&wchar_buf);
3b2b8fea 2812
0d63ecda
KS
2813 /* Print the output string to the obstack. */
2814 print_converted_chars_to_obstack (&wchar_buf, converted_chars, quote_char,
2815 width, byte_order, options);
3b2b8fea
TT
2816
2817 if (force_ellipses || !finished)
2818 obstack_grow_wstr (&wchar_buf, LCST ("..."));
2819
2820 /* OUTPUT is where we collect `char's for printing. */
2821 obstack_init (&output);
2822 make_cleanup_obstack_free (&output);
2823
2824 convert_between_encodings (INTERMEDIATE_ENCODING, host_charset (),
ac91cd70 2825 (gdb_byte *) obstack_base (&wchar_buf),
3b2b8fea 2826 obstack_object_size (&wchar_buf),
fff10684 2827 sizeof (gdb_wchar_t), &output, translit_char);
3b2b8fea
TT
2828 obstack_1grow (&output, '\0');
2829
79f33898 2830 fputs_filtered ((const char *) obstack_base (&output), stream);
3b2b8fea
TT
2831
2832 do_cleanups (cleanup);
2833}
2834
ae6a3a4c
TJB
2835/* Print a string from the inferior, starting at ADDR and printing up to LEN
2836 characters, of WIDTH bytes a piece, to STREAM. If LEN is -1, printing
2837 stops at the first null byte, otherwise printing proceeds (including null
2838 bytes) until either print_max or LEN characters have been printed,
09ca9e2e
TT
2839 whichever is smaller. ENCODING is the name of the string's
2840 encoding. It can be NULL, in which case the target encoding is
2841 assumed. */
ae6a3a4c
TJB
2842
2843int
09ca9e2e
TT
2844val_print_string (struct type *elttype, const char *encoding,
2845 CORE_ADDR addr, int len,
6c7a06a3 2846 struct ui_file *stream,
ae6a3a4c
TJB
2847 const struct value_print_options *options)
2848{
2849 int force_ellipsis = 0; /* Force ellipsis to be printed if nonzero. */
d09f2c3f 2850 int err; /* Non-zero if we got a bad read. */
581e13c1 2851 int found_nul; /* Non-zero if we found the nul char. */
ae6a3a4c
TJB
2852 unsigned int fetchlimit; /* Maximum number of chars to print. */
2853 int bytes_read;
2854 gdb_byte *buffer = NULL; /* Dynamically growable fetch buffer. */
2855 struct cleanup *old_chain = NULL; /* Top of the old cleanup chain. */
5af949e3 2856 struct gdbarch *gdbarch = get_type_arch (elttype);
e17a4113 2857 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
6c7a06a3 2858 int width = TYPE_LENGTH (elttype);
ae6a3a4c
TJB
2859
2860 /* First we need to figure out the limit on the number of characters we are
2861 going to attempt to fetch and print. This is actually pretty simple. If
2862 LEN >= zero, then the limit is the minimum of LEN and print_max. If
2863 LEN is -1, then the limit is print_max. This is true regardless of
2864 whether print_max is zero, UINT_MAX (unlimited), or something in between,
2865 because finding the null byte (or available memory) is what actually
2866 limits the fetch. */
2867
325fac50
PA
2868 fetchlimit = (len == -1 ? options->print_max : std::min ((unsigned) len,
2869 options->print_max));
ae6a3a4c 2870
d09f2c3f
PA
2871 err = read_string (addr, len, width, fetchlimit, byte_order,
2872 &buffer, &bytes_read);
ae6a3a4c
TJB
2873 old_chain = make_cleanup (xfree, buffer);
2874
2875 addr += bytes_read;
c906108c 2876
3e43a32a
MS
2877 /* We now have either successfully filled the buffer to fetchlimit,
2878 or terminated early due to an error or finding a null char when
2879 LEN is -1. */
ae6a3a4c
TJB
2880
2881 /* Determine found_nul by looking at the last character read. */
6694c411
JK
2882 found_nul = 0;
2883 if (bytes_read >= width)
2884 found_nul = extract_unsigned_integer (buffer + bytes_read - width, width,
2885 byte_order) == 0;
c906108c
SS
2886 if (len == -1 && !found_nul)
2887 {
777ea8f1 2888 gdb_byte *peekbuf;
c906108c 2889
ae6a3a4c 2890 /* We didn't find a NUL terminator we were looking for. Attempt
c5aa993b
JM
2891 to peek at the next character. If not successful, or it is not
2892 a null byte, then force ellipsis to be printed. */
c906108c 2893
777ea8f1 2894 peekbuf = (gdb_byte *) alloca (width);
c906108c
SS
2895
2896 if (target_read_memory (addr, peekbuf, width) == 0
e17a4113 2897 && extract_unsigned_integer (peekbuf, width, byte_order) != 0)
c906108c
SS
2898 force_ellipsis = 1;
2899 }
d09f2c3f 2900 else if ((len >= 0 && err != 0) || (len > bytes_read / width))
c906108c
SS
2901 {
2902 /* Getting an error when we have a requested length, or fetching less
c5aa993b 2903 than the number of characters actually requested, always make us
ae6a3a4c 2904 print ellipsis. */
c906108c
SS
2905 force_ellipsis = 1;
2906 }
2907
c906108c
SS
2908 /* If we get an error before fetching anything, don't print a string.
2909 But if we fetch something and then get an error, print the string
2910 and then the error message. */
d09f2c3f 2911 if (err == 0 || bytes_read > 0)
c906108c 2912 {
be759fcf 2913 LA_PRINT_STRING (stream, elttype, buffer, bytes_read / width,
3a772aa4 2914 encoding, force_ellipsis, options);
c906108c
SS
2915 }
2916
d09f2c3f 2917 if (err != 0)
c906108c 2918 {
578d3588
PA
2919 char *str;
2920
d09f2c3f 2921 str = memory_error_message (TARGET_XFER_E_IO, gdbarch, addr);
578d3588
PA
2922 make_cleanup (xfree, str);
2923
2924 fprintf_filtered (stream, "<error: ");
2925 fputs_filtered (str, stream);
2926 fprintf_filtered (stream, ">");
c906108c 2927 }
ae6a3a4c 2928
c906108c
SS
2929 gdb_flush (stream);
2930 do_cleanups (old_chain);
ae6a3a4c
TJB
2931
2932 return (bytes_read / width);
c906108c 2933}
c906108c 2934\f
c5aa993b 2935
09e6485f
PA
2936/* The 'set input-radix' command writes to this auxiliary variable.
2937 If the requested radix is valid, INPUT_RADIX is updated; otherwise,
2938 it is left unchanged. */
2939
2940static unsigned input_radix_1 = 10;
2941
c906108c
SS
2942/* Validate an input or output radix setting, and make sure the user
2943 knows what they really did here. Radix setting is confusing, e.g.
2944 setting the input radix to "10" never changes it! */
2945
c906108c 2946static void
fba45db2 2947set_input_radix (char *args, int from_tty, struct cmd_list_element *c)
c906108c 2948{
09e6485f 2949 set_input_radix_1 (from_tty, input_radix_1);
c906108c
SS
2950}
2951
c906108c 2952static void
fba45db2 2953set_input_radix_1 (int from_tty, unsigned radix)
c906108c
SS
2954{
2955 /* We don't currently disallow any input radix except 0 or 1, which don't
2956 make any mathematical sense. In theory, we can deal with any input
2957 radix greater than 1, even if we don't have unique digits for every
2958 value from 0 to radix-1, but in practice we lose on large radix values.
2959 We should either fix the lossage or restrict the radix range more.
581e13c1 2960 (FIXME). */
c906108c
SS
2961
2962 if (radix < 2)
2963 {
09e6485f 2964 input_radix_1 = input_radix;
8a3fe4f8 2965 error (_("Nonsense input radix ``decimal %u''; input radix unchanged."),
c906108c
SS
2966 radix);
2967 }
09e6485f 2968 input_radix_1 = input_radix = radix;
c906108c
SS
2969 if (from_tty)
2970 {
3e43a32a
MS
2971 printf_filtered (_("Input radix now set to "
2972 "decimal %u, hex %x, octal %o.\n"),
c906108c
SS
2973 radix, radix, radix);
2974 }
2975}
2976
09e6485f
PA
2977/* The 'set output-radix' command writes to this auxiliary variable.
2978 If the requested radix is valid, OUTPUT_RADIX is updated,
2979 otherwise, it is left unchanged. */
2980
2981static unsigned output_radix_1 = 10;
2982
c906108c 2983static void
fba45db2 2984set_output_radix (char *args, int from_tty, struct cmd_list_element *c)
c906108c 2985{
09e6485f 2986 set_output_radix_1 (from_tty, output_radix_1);
c906108c
SS
2987}
2988
2989static void
fba45db2 2990set_output_radix_1 (int from_tty, unsigned radix)
c906108c
SS
2991{
2992 /* Validate the radix and disallow ones that we aren't prepared to
581e13c1 2993 handle correctly, leaving the radix unchanged. */
c906108c
SS
2994 switch (radix)
2995 {
2996 case 16:
79a45b7d 2997 user_print_options.output_format = 'x'; /* hex */
c906108c
SS
2998 break;
2999 case 10:
79a45b7d 3000 user_print_options.output_format = 0; /* decimal */
c906108c
SS
3001 break;
3002 case 8:
79a45b7d 3003 user_print_options.output_format = 'o'; /* octal */
c906108c
SS
3004 break;
3005 default:
09e6485f 3006 output_radix_1 = output_radix;
3e43a32a
MS
3007 error (_("Unsupported output radix ``decimal %u''; "
3008 "output radix unchanged."),
c906108c
SS
3009 radix);
3010 }
09e6485f 3011 output_radix_1 = output_radix = radix;
c906108c
SS
3012 if (from_tty)
3013 {
3e43a32a
MS
3014 printf_filtered (_("Output radix now set to "
3015 "decimal %u, hex %x, octal %o.\n"),
c906108c
SS
3016 radix, radix, radix);
3017 }
3018}
3019
3020/* Set both the input and output radix at once. Try to set the output radix
3021 first, since it has the most restrictive range. An radix that is valid as
3022 an output radix is also valid as an input radix.
3023
3024 It may be useful to have an unusual input radix. If the user wishes to
3025 set an input radix that is not valid as an output radix, he needs to use
581e13c1 3026 the 'set input-radix' command. */
c906108c
SS
3027
3028static void
fba45db2 3029set_radix (char *arg, int from_tty)
c906108c
SS
3030{
3031 unsigned radix;
3032
bb518678 3033 radix = (arg == NULL) ? 10 : parse_and_eval_long (arg);
c906108c
SS
3034 set_output_radix_1 (0, radix);
3035 set_input_radix_1 (0, radix);
3036 if (from_tty)
3037 {
3e43a32a
MS
3038 printf_filtered (_("Input and output radices now set to "
3039 "decimal %u, hex %x, octal %o.\n"),
c906108c
SS
3040 radix, radix, radix);
3041 }
3042}
3043
581e13c1 3044/* Show both the input and output radices. */
c906108c 3045
c906108c 3046static void
fba45db2 3047show_radix (char *arg, int from_tty)
c906108c
SS
3048{
3049 if (from_tty)
3050 {
3051 if (input_radix == output_radix)
3052 {
3e43a32a
MS
3053 printf_filtered (_("Input and output radices set to "
3054 "decimal %u, hex %x, octal %o.\n"),
c906108c
SS
3055 input_radix, input_radix, input_radix);
3056 }
3057 else
3058 {
3e43a32a
MS
3059 printf_filtered (_("Input radix set to decimal "
3060 "%u, hex %x, octal %o.\n"),
c906108c 3061 input_radix, input_radix, input_radix);
3e43a32a
MS
3062 printf_filtered (_("Output radix set to decimal "
3063 "%u, hex %x, octal %o.\n"),
c906108c
SS
3064 output_radix, output_radix, output_radix);
3065 }
3066 }
3067}
c906108c 3068\f
c5aa993b 3069
c906108c 3070static void
fba45db2 3071set_print (char *arg, int from_tty)
c906108c
SS
3072{
3073 printf_unfiltered (
c5aa993b 3074 "\"set print\" must be followed by the name of a print subcommand.\n");
635c7e8a 3075 help_list (setprintlist, "set print ", all_commands, gdb_stdout);
c906108c
SS
3076}
3077
c906108c 3078static void
fba45db2 3079show_print (char *args, int from_tty)
c906108c
SS
3080{
3081 cmd_show_list (showprintlist, from_tty, "");
3082}
e7045703
DE
3083
3084static void
3085set_print_raw (char *arg, int from_tty)
3086{
3087 printf_unfiltered (
3088 "\"set print raw\" must be followed by the name of a \"print raw\" subcommand.\n");
635c7e8a 3089 help_list (setprintrawlist, "set print raw ", all_commands, gdb_stdout);
e7045703
DE
3090}
3091
3092static void
3093show_print_raw (char *args, int from_tty)
3094{
3095 cmd_show_list (showprintrawlist, from_tty, "");
3096}
3097
c906108c
SS
3098\f
3099void
fba45db2 3100_initialize_valprint (void)
c906108c 3101{
c906108c 3102 add_prefix_cmd ("print", no_class, set_print,
1bedd215 3103 _("Generic command for setting how things print."),
c906108c 3104 &setprintlist, "set print ", 0, &setlist);
c5aa993b 3105 add_alias_cmd ("p", "print", no_class, 1, &setlist);
581e13c1 3106 /* Prefer set print to set prompt. */
c906108c
SS
3107 add_alias_cmd ("pr", "print", no_class, 1, &setlist);
3108
3109 add_prefix_cmd ("print", no_class, show_print,
1bedd215 3110 _("Generic command for showing print settings."),
c906108c 3111 &showprintlist, "show print ", 0, &showlist);
c5aa993b
JM
3112 add_alias_cmd ("p", "print", no_class, 1, &showlist);
3113 add_alias_cmd ("pr", "print", no_class, 1, &showlist);
c906108c 3114
e7045703
DE
3115 add_prefix_cmd ("raw", no_class, set_print_raw,
3116 _("\
3117Generic command for setting what things to print in \"raw\" mode."),
3118 &setprintrawlist, "set print raw ", 0, &setprintlist);
3119 add_prefix_cmd ("raw", no_class, show_print_raw,
3120 _("Generic command for showing \"print raw\" settings."),
3121 &showprintrawlist, "show print raw ", 0, &showprintlist);
3122
79a45b7d
TT
3123 add_setshow_uinteger_cmd ("elements", no_class,
3124 &user_print_options.print_max, _("\
35096d9d
AC
3125Set limit on string chars or array elements to print."), _("\
3126Show limit on string chars or array elements to print."), _("\
f81d1120 3127\"set print elements unlimited\" causes there to be no limit."),
35096d9d 3128 NULL,
920d2a44 3129 show_print_max,
35096d9d 3130 &setprintlist, &showprintlist);
c906108c 3131
79a45b7d
TT
3132 add_setshow_boolean_cmd ("null-stop", no_class,
3133 &user_print_options.stop_print_at_null, _("\
5bf193a2
AC
3134Set printing of char arrays to stop at first null char."), _("\
3135Show printing of char arrays to stop at first null char."), NULL,
3136 NULL,
920d2a44 3137 show_stop_print_at_null,
5bf193a2 3138 &setprintlist, &showprintlist);
c906108c 3139
35096d9d 3140 add_setshow_uinteger_cmd ("repeats", no_class,
79a45b7d 3141 &user_print_options.repeat_count_threshold, _("\
35096d9d
AC
3142Set threshold for repeated print elements."), _("\
3143Show threshold for repeated print elements."), _("\
f81d1120 3144\"set print repeats unlimited\" causes all elements to be individually printed."),
35096d9d 3145 NULL,
920d2a44 3146 show_repeat_count_threshold,
35096d9d 3147 &setprintlist, &showprintlist);
c906108c 3148
79a45b7d 3149 add_setshow_boolean_cmd ("pretty", class_support,
2a998fc0
DE
3150 &user_print_options.prettyformat_structs, _("\
3151Set pretty formatting of structures."), _("\
3152Show pretty formatting of structures."), NULL,
5bf193a2 3153 NULL,
2a998fc0 3154 show_prettyformat_structs,
5bf193a2
AC
3155 &setprintlist, &showprintlist);
3156
79a45b7d
TT
3157 add_setshow_boolean_cmd ("union", class_support,
3158 &user_print_options.unionprint, _("\
5bf193a2
AC
3159Set printing of unions interior to structures."), _("\
3160Show printing of unions interior to structures."), NULL,
3161 NULL,
920d2a44 3162 show_unionprint,
5bf193a2
AC
3163 &setprintlist, &showprintlist);
3164
79a45b7d 3165 add_setshow_boolean_cmd ("array", class_support,
2a998fc0
DE
3166 &user_print_options.prettyformat_arrays, _("\
3167Set pretty formatting of arrays."), _("\
3168Show pretty formatting of arrays."), NULL,
5bf193a2 3169 NULL,
2a998fc0 3170 show_prettyformat_arrays,
5bf193a2
AC
3171 &setprintlist, &showprintlist);
3172
79a45b7d
TT
3173 add_setshow_boolean_cmd ("address", class_support,
3174 &user_print_options.addressprint, _("\
5bf193a2
AC
3175Set printing of addresses."), _("\
3176Show printing of addresses."), NULL,
3177 NULL,
920d2a44 3178 show_addressprint,
5bf193a2 3179 &setprintlist, &showprintlist);
c906108c 3180
9cb709b6
TT
3181 add_setshow_boolean_cmd ("symbol", class_support,
3182 &user_print_options.symbol_print, _("\
3183Set printing of symbol names when printing pointers."), _("\
3184Show printing of symbol names when printing pointers."),
3185 NULL, NULL,
3186 show_symbol_print,
3187 &setprintlist, &showprintlist);
3188
1e8fb976
PA
3189 add_setshow_zuinteger_cmd ("input-radix", class_support, &input_radix_1,
3190 _("\
35096d9d
AC
3191Set default input radix for entering numbers."), _("\
3192Show default input radix for entering numbers."), NULL,
1e8fb976
PA
3193 set_input_radix,
3194 show_input_radix,
3195 &setlist, &showlist);
35096d9d 3196
1e8fb976
PA
3197 add_setshow_zuinteger_cmd ("output-radix", class_support, &output_radix_1,
3198 _("\
35096d9d
AC
3199Set default output radix for printing of values."), _("\
3200Show default output radix for printing of values."), NULL,
1e8fb976
PA
3201 set_output_radix,
3202 show_output_radix,
3203 &setlist, &showlist);
c906108c 3204
cb1a6d5f
AC
3205 /* The "set radix" and "show radix" commands are special in that
3206 they are like normal set and show commands but allow two normally
3207 independent variables to be either set or shown with a single
b66df561 3208 command. So the usual deprecated_add_set_cmd() and [deleted]
581e13c1 3209 add_show_from_set() commands aren't really appropriate. */
b66df561
AC
3210 /* FIXME: i18n: With the new add_setshow_integer command, that is no
3211 longer true - show can display anything. */
1a966eab
AC
3212 add_cmd ("radix", class_support, set_radix, _("\
3213Set default input and output number radices.\n\
c906108c 3214Use 'set input-radix' or 'set output-radix' to independently set each.\n\
1a966eab 3215Without an argument, sets both radices back to the default value of 10."),
c906108c 3216 &setlist);
1a966eab
AC
3217 add_cmd ("radix", class_support, show_radix, _("\
3218Show the default input and output number radices.\n\
3219Use 'show input-radix' or 'show output-radix' to independently show each."),
c906108c
SS
3220 &showlist);
3221
e79af960 3222 add_setshow_boolean_cmd ("array-indexes", class_support,
79a45b7d 3223 &user_print_options.print_array_indexes, _("\
e79af960
JB
3224Set printing of array indexes."), _("\
3225Show printing of array indexes"), NULL, NULL, show_print_array_indexes,
3226 &setprintlist, &showprintlist);
c906108c 3227}
This page took 2.56879 seconds and 4 git commands to generate.