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