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