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