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