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