]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame_incremental - gdb/valprint.c
sim: ppc: use correct macros
[thirdparty/binutils-gdb.git] / gdb / valprint.c
... / ...
CommitLineData
1/* Print values for GDB, the GNU debugger.
2
3 Copyright (C) 1986-2025 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
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.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20#include "event-top.h"
21#include "extract-store-integer.h"
22#include "symtab.h"
23#include "gdbtypes.h"
24#include "value.h"
25#include "gdbcore.h"
26#include "cli/cli-cmds.h"
27#include "target.h"
28#include "language.h"
29#include "annotate.h"
30#include "valprint.h"
31#include "target-float.h"
32#include "extension.h"
33#include "ada-lang.h"
34#include "gdbsupport/gdb_obstack.h"
35#include "charset.h"
36#include "typeprint.h"
37#include <ctype.h>
38#include <algorithm>
39#include "gdbsupport/byte-vector.h"
40#include "cli/cli-option.h"
41#include "gdbarch.h"
42#include "cli/cli-style.h"
43#include "count-one-bits.h"
44#include "c-lang.h"
45#include "cp-abi.h"
46#include "inferior.h"
47#include "gdbsupport/selftest.h"
48#include "selftest-arch.h"
49
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
83/* Command lists for set/show print raw. */
84struct cmd_list_element *setprintrawlist;
85struct cmd_list_element *showprintrawlist;
86
87/* Prototypes for local functions */
88
89static void set_input_radix_1 (int, unsigned);
90
91static void set_output_radix_1 (int, unsigned);
92
93static void val_print_type_code_flags (struct type *type,
94 struct value *original_value,
95 int embedded_offset,
96 struct ui_file *stream);
97
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
106
107struct value_print_options user_print_options =
108{
109 Val_prettyformat_default, /* prettyformat */
110 false, /* prettyformat_arrays */
111 false, /* prettyformat_structs */
112 false, /* vtblprint */
113 true, /* unionprint */
114 true, /* addressprint */
115 false, /* nibblesprint */
116 false, /* objectprint */
117 PRINT_MAX_DEFAULT, /* print_max */
118 PRINT_MAX_CHARS_DEFAULT, /* print_max_chars */
119 10, /* repeat_count_threshold */
120 0, /* output_format */
121 0, /* format */
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 */
131 PRINT_MAX_DEPTH_DEFAULT, /* max_depth */
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
142 pretty-formatting disabled. */
143void
144get_no_prettyformat_print_options (struct value_print_options *opts)
145{
146 *opts = user_print_options;
147 opts->prettyformat = Val_no_prettyformat;
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
160/* Implement 'show print elements'. */
161
162static void
163show_print_max (struct ui_file *file, int from_tty,
164 struct cmd_list_element *c, const char *value)
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)
179{
180 gdb_printf (file,
181 _("Limit on string characters to print is %s.\n"),
182 value);
183}
184
185/* Default input and output radixes, and output format letter. */
186
187unsigned input_radix = 10;
188static void
189show_input_radix (struct ui_file *file, int from_tty,
190 struct cmd_list_element *c, const char *value)
191{
192 gdb_printf (file,
193 _("Default input radix for entering numbers is %s.\n"),
194 value);
195}
196
197unsigned output_radix = 10;
198static void
199show_output_radix (struct ui_file *file, int from_tty,
200 struct cmd_list_element *c, const char *value)
201{
202 gdb_printf (file,
203 _("Default output radix for printing of values is %s.\n"),
204 value);
205}
206
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
210static void
211show_print_array_indexes (struct ui_file *file, int from_tty,
212 struct cmd_list_element *c, const char *value)
213{
214 gdb_printf (file, _("Printing of array indexes is %s.\n"), value);
215}
216
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
219 print routines. */
220
221static void
222show_repeat_count_threshold (struct ui_file *file, int from_tty,
223 struct cmd_list_element *c, const char *value)
224{
225 gdb_printf (file, _("Threshold for repeated print elements is %s.\n"),
226 value);
227}
228
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{
235 gdb_printf (file,
236 _("Printing of memory tag violations is %s.\n"),
237 value);
238}
239
240/* If nonzero, stops printing of char arrays at first null. */
241
242static void
243show_stop_print_at_null (struct ui_file *file, int from_tty,
244 struct cmd_list_element *c, const char *value)
245{
246 gdb_printf (file,
247 _("Printing of char arrays to stop "
248 "at first null char is %s.\n"),
249 value);
250}
251
252/* Controls pretty printing of structures. */
253
254static void
255show_prettyformat_structs (struct ui_file *file, int from_tty,
256 struct cmd_list_element *c, const char *value)
257{
258 gdb_printf (file, _("Pretty formatting of structures is %s.\n"), value);
259}
260
261/* Controls pretty printing of arrays. */
262
263static void
264show_prettyformat_arrays (struct ui_file *file, int from_tty,
265 struct cmd_list_element *c, const char *value)
266{
267 gdb_printf (file, _("Pretty formatting of arrays is %s.\n"), value);
268}
269
270/* If nonzero, causes unions inside structures or other unions to be
271 printed. */
272
273static void
274show_unionprint (struct ui_file *file, int from_tty,
275 struct cmd_list_element *c, const char *value)
276{
277 gdb_printf (file,
278 _("Printing of unions interior to structures is %s.\n"),
279 value);
280}
281
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
293/* If nonzero, causes machine addresses to be printed in certain contexts. */
294
295static void
296show_addressprint (struct ui_file *file, int from_tty,
297 struct cmd_list_element *c, const char *value)
298{
299 gdb_printf (file, _("Printing of addresses is %s.\n"), value);
300}
301
302static void
303show_symbol_print (struct ui_file *file, int from_tty,
304 struct cmd_list_element *c, const char *value)
305{
306 gdb_printf (file,
307 _("Printing of symbols when printing pointers is %s.\n"),
308 value);
309}
310
311\f
312
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
317int
318val_print_scalar_type_p (struct type *type)
319{
320 type = check_typedef (type);
321 while (TYPE_IS_REFERENCE (type))
322 {
323 type = type->target_type ();
324 type = check_typedef (type);
325 }
326 switch (type->code ())
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:
333 return 0;
334 default:
335 return 1;
336 }
337}
338
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)
348 || language->is_string_type_p (type));
349}
350
351/* See valprint.h. */
352
353int
354valprint_check_validity (struct ui_file *stream,
355 struct type *type,
356 LONGEST embedded_offset,
357 const struct value *val)
358{
359 type = check_typedef (type);
360
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
373 if (type->code () != TYPE_CODE_UNION
374 && type->code () != TYPE_CODE_STRUCT
375 && type->code () != TYPE_CODE_ARRAY)
376 {
377 if (val->bits_any_optimized_out (TARGET_CHAR_BIT * embedded_offset,
378 TARGET_CHAR_BIT * type->length ()))
379 {
380 val_print_optimized_out (val, stream);
381 return 0;
382 }
383
384 if (val->bits_synthetic_pointer (TARGET_CHAR_BIT * embedded_offset,
385 TARGET_CHAR_BIT * type->length ()))
386 {
387 const int is_ref = type->code () == TYPE_CODE_REF;
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)
395 ref_is_addressable = deref_val->lval () == lval_memory;
396 }
397
398 if (!is_ref || !ref_is_addressable)
399 fputs_styled (_("<synthetic pointer>"), metadata_style.style (),
400 stream);
401
402 /* C++ references should be valid even if they're synthetic. */
403 return is_ref;
404 }
405
406 if (!val->bytes_available (embedded_offset, type->length ()))
407 {
408 val_print_unavailable (stream);
409 return 0;
410 }
411 }
412
413 return 1;
414}
415
416void
417val_print_optimized_out (const struct value *val, struct ui_file *stream)
418{
419 if (val != NULL && val->lval () == lval_register)
420 val_print_not_saved (stream);
421 else
422 fprintf_styled (stream, metadata_style.style (), _("<optimized out>"));
423}
424
425void
426val_print_not_saved (struct ui_file *stream)
427{
428 fprintf_styled (stream, metadata_style.style (), _("<not saved>"));
429}
430
431void
432val_print_unavailable (struct ui_file *stream)
433{
434 fprintf_styled (stream, metadata_style.style (), _("<unavailable>"));
435}
436
437void
438val_print_invalid_address (struct ui_file *stream)
439{
440 fprintf_styled (stream, metadata_style.style (), _("<invalid address>"));
441}
442
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{
455 struct gdbarch *gdbarch = type->arch ();
456
457 if (elttype->code () == TYPE_CODE_FUNC)
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)
467 gdb_puts (paddress (gdbarch, address), stream);
468}
469
470/* generic_val_print helper for TYPE_CODE_ARRAY. */
471
472static void
473generic_val_print_array (struct value *val,
474 struct ui_file *stream, int recurse,
475 const struct value_print_options *options,
476 const struct
477 generic_val_print_decorations *decorations)
478{
479 struct type *type = check_typedef (val->type ());
480 struct type *unresolved_elttype = type->target_type ();
481 struct type *elttype = check_typedef (unresolved_elttype);
482
483 if (type->length () > 0 && unresolved_elttype->length () > 0)
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
490 gdb_puts (decorations->array_start, stream);
491 value_print_array_elements (val, stream, recurse, options, 0);
492 gdb_puts (decorations->array_end, stream);
493 }
494 else
495 {
496 /* Array of unspecified length: treat like pointer to first elt. */
497 print_unpacked_pointer (type, elttype, val->address (),
498 stream, options);
499 }
500
501}
502
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
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 {
584 struct type *type = check_typedef (val->type ());
585 struct type *elttype = check_typedef (type->target_type ());
586 const gdb_byte *valaddr = val->contents_for_printing ().data ();
587 CORE_ADDR addr = unpack_pointer (type, valaddr);
588
589 print_unpacked_pointer (type, elttype, addr, stream, options);
590 }
591}
592
593
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{
600 struct gdbarch *gdbarch = type->arch ();
601
602 if (address_buffer != NULL)
603 {
604 CORE_ADDR address
605 = extract_typed_address (address_buffer + embedded_offset, type);
606
607 gdb_printf (stream, "@");
608 gdb_puts (paddress (gdbarch, address), stream);
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
621 if (deref_val->lval () == lval_memory)
622 return value_addr (deref_val)->contents_for_printing ().data ();
623 else
624 {
625 /* We have a non-addressable value, such as a DW_AT_const_value. */
626 return NULL;
627 }
628}
629
630/* generic_val_print helper for TYPE_CODE_{RVALUE_,}REF. */
631
632static void
633generic_val_print_ref (struct type *type,
634 int embedded_offset, struct ui_file *stream, int recurse,
635 struct value *original_value,
636 const struct value_print_options *options)
637{
638 struct type *elttype = check_typedef (type->target_type ());
639 struct value *deref_val = NULL;
640 const bool value_is_synthetic
641 = original_value->bits_synthetic_pointer (TARGET_CHAR_BIT * embedded_offset,
642 TARGET_CHAR_BIT * type->length ());
643 const int must_coerce_ref = ((options->addressprint && value_is_synthetic)
644 || options->deref_ref);
645 const int type_is_defined = elttype->code () != TYPE_CODE_UNDEF;
646 const gdb_byte *valaddr = original_value->contents_for_printing ().data ();
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
658 deref_val = value_at (type->target_type (),
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. */
672
673 if (options->addressprint)
674 {
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);
680
681 if (options->deref_ref)
682 gdb_puts (": ", stream);
683 }
684
685 if (options->deref_ref)
686 {
687 if (type_is_defined)
688 common_val_print (deref_val, stream, recurse, options,
689 current_language);
690 else
691 gdb_puts ("???", stream);
692 }
693}
694
695/* Helper function for generic_val_print_enum.
696 This is also used to print enums in TYPE_CODE_FLAGS values. */
697
698static void
699generic_val_print_enum_1 (struct type *type, LONGEST val,
700 struct ui_file *stream)
701{
702 unsigned int i;
703 unsigned int len;
704
705 len = type->num_fields ();
706 for (i = 0; i < len; i++)
707 {
708 QUIT;
709 if (val == type->field (i).loc_enumval ())
710 {
711 break;
712 }
713 }
714 if (i < len)
715 {
716 fputs_styled (type->field (i).name (), variable_name_style.style (),
717 stream);
718 }
719 else if (type->is_flag_enum ())
720 {
721 int first = 1;
722
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. */
727 for (i = 0; i < len; ++i)
728 {
729 QUIT;
730
731 ULONGEST enumval = type->field (i).loc_enumval ();
732 int nbits = count_one_bits_ll (enumval);
733
734 gdb_assert (nbits == 0 || nbits == 1);
735
736 if ((val & enumval) != 0)
737 {
738 if (first)
739 {
740 gdb_puts ("(", stream);
741 first = 0;
742 }
743 else
744 gdb_puts (" | ", stream);
745
746 val &= ~type->field (i).loc_enumval ();
747 fputs_styled (type->field (i).name (),
748 variable_name_style.style (), stream);
749 }
750 }
751
752 if (val != 0)
753 {
754 /* There are leftover bits, print them. */
755 if (first)
756 gdb_puts ("(", stream);
757 else
758 gdb_puts (" | ", stream);
759
760 gdb_puts ("unknown: 0x", stream);
761 print_longest (stream, 'x', 0, val);
762 gdb_puts (")", stream);
763 }
764 else if (first)
765 {
766 /* Nothing has been printed and the value is 0, the enum value must
767 have been 0. */
768 gdb_puts ("0", stream);
769 }
770 else
771 {
772 /* Something has been printed, close the parenthesis. */
773 gdb_puts (")", stream);
774 }
775 }
776 else
777 print_longest (stream, 'd', 0, val);
778}
779
780/* generic_val_print helper for TYPE_CODE_ENUM. */
781
782static void
783generic_val_print_enum (struct type *type,
784 int embedded_offset, struct ui_file *stream,
785 struct value *original_value,
786 const struct value_print_options *options)
787{
788 LONGEST val;
789 struct gdbarch *gdbarch = type->arch ();
790 int unit_size = gdbarch_addressable_memory_unit_size (gdbarch);
791
792 gdb_assert (!options->format);
793
794 const gdb_byte *valaddr = original_value->contents_for_printing ().data ();
795
796 val = unpack_long (type, valaddr + embedded_offset * unit_size);
797
798 generic_val_print_enum_1 (type, val, stream);
799}
800
801/* generic_val_print helper for TYPE_CODE_FUNC and TYPE_CODE_METHOD. */
802
803static void
804generic_val_print_func (struct type *type,
805 int embedded_offset, CORE_ADDR address,
806 struct ui_file *stream,
807 struct value *original_value,
808 const struct value_print_options *options)
809{
810 struct gdbarch *gdbarch = type->arch ();
811
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. */
817 gdb_printf (stream, "{");
818 type_print (type, "", stream, -1);
819 gdb_printf (stream, "} ");
820 /* Try to print what function it points to, and its address. */
821 print_address_demangle (options, gdbarch, address, stream, demangle);
822}
823
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 {
841 const gdb_byte *valaddr = value->contents_for_printing ().data ();
842 struct type *type = check_typedef (value->type ());
843 LONGEST val = unpack_long (type, valaddr);
844 if (val == 0)
845 gdb_puts (decorations->false_name, stream);
846 else if (val == 1)
847 gdb_puts (decorations->true_name, stream);
848 else
849 print_longest (stream, 'd', 0, val);
850 }
851}
852
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
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 {
882 struct type *unresolved_type = value->type ();
883 struct type *type = check_typedef (unresolved_type);
884 const gdb_byte *valaddr = value->contents_for_printing ().data ();
885
886 LONGEST val = unpack_long (type, valaddr);
887 if (type->is_unsigned ())
888 gdb_printf (stream, "%u", (unsigned int) val);
889 else
890 gdb_printf (stream, "%d", (int) val);
891 gdb_puts (" ", stream);
892 current_language->printchar (val, unresolved_type, stream);
893 }
894}
895
896/* generic_val_print helper for TYPE_CODE_FLT and TYPE_CODE_DECFLOAT. */
897
898static void
899generic_val_print_float (struct type *type, struct ui_file *stream,
900 struct value *original_value,
901 const struct value_print_options *options)
902{
903 gdb_assert (!options->format);
904
905 const gdb_byte *valaddr = original_value->contents_for_printing ().data ();
906
907 print_floating (valaddr, type, stream);
908}
909
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 {
920 struct type *type = val->type ();
921
922 const gdb_byte *valaddr = val->contents_for_printing ().data ();
923 gdb_mpf f;
924
925 f.read_fixed_point (gdb::make_array_view (valaddr, type->length ()),
926 type_byte_order (type), type->is_unsigned (),
927 type->fixed_point_scaling_factor ());
928
929 const char *fmt = type->length () < 4 ? "%.11Fg" : "%.17Fg";
930 std::string str = f.str (fmt);
931 gdb_printf (stream, "%s", str.c_str ());
932 }
933}
934
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{
943 gdb_printf (stream, "%s", decorations->complex_prefix);
944
945 struct value *real_part = value_real_part (val);
946 value_print_scalar_formatted (real_part, options, 0, stream);
947 gdb_printf (stream, "%s", decorations->complex_infix);
948
949 struct value *imag_part = value_imaginary_part (val);
950 value_print_scalar_formatted (imag_part, options, 0, stream);
951 gdb_printf (stream, "%s", decorations->complex_suffix);
952}
953
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. */
967 struct type *type = check_typedef (val->type ());
968 const gdb_byte *valaddr = val->contents_for_printing ().data ();
969 cp_print_class_member (valaddr, type, stream, "&");
970 }
971 else
972 value_print_scalar_formatted (val, options, 0, stream);
973}
974
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{
982 struct type *type = val->type ();
983
984 type = check_typedef (type);
985
986 if (is_fixed_point_type (type))
987 type = type->fixed_point_type_base_type ();
988
989 /* Widen a subrange to its target type, then use that type's
990 printer. */
991 while (type->code () == TYPE_CODE_RANGE)
992 {
993 type = check_typedef (type->target_type ());
994 val = value_cast (type, val);
995 }
996
997 switch (type->code ())
998 {
999 case TYPE_CODE_ARRAY:
1000 generic_val_print_array (val, stream, recurse, options, decorations);
1001 break;
1002
1003 case TYPE_CODE_STRING:
1004 generic_val_print_string (val, stream, recurse, options, decorations);
1005 break;
1006
1007 case TYPE_CODE_MEMBERPTR:
1008 generic_value_print_memberptr (val, stream, recurse, options,
1009 decorations);
1010 break;
1011
1012 case TYPE_CODE_PTR:
1013 generic_value_print_ptr (val, stream, options);
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:
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);
1027 break;
1028
1029 case TYPE_CODE_FLAGS:
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);
1034 break;
1035
1036 case TYPE_CODE_FUNC:
1037 case TYPE_CODE_METHOD:
1038 if (options->format)
1039 value_print_scalar_formatted (val, options, 0, stream);
1040 else
1041 generic_val_print_func (type, 0, val->address (), stream,
1042 val, options);
1043 break;
1044
1045 case TYPE_CODE_BOOL:
1046 generic_value_print_bool (val, stream, options, decorations);
1047 break;
1048
1049 case TYPE_CODE_INT:
1050 generic_value_print_int (val, stream, options);
1051 break;
1052
1053 case TYPE_CODE_CHAR:
1054 generic_value_print_char (val, stream, options);
1055 break;
1056
1057 case TYPE_CODE_FLT:
1058 case TYPE_CODE_DECFLOAT:
1059 if (options->format)
1060 value_print_scalar_formatted (val, options, 0, stream);
1061 else
1062 generic_val_print_float (type, stream, val, options);
1063 break;
1064
1065 case TYPE_CODE_FIXED_POINT:
1066 generic_val_print_fixed_point (val, stream, options);
1067 break;
1068
1069 case TYPE_CODE_VOID:
1070 gdb_puts (decorations->void_name, stream);
1071 break;
1072
1073 case TYPE_CODE_ERROR:
1074 gdb_printf (stream, "%s", TYPE_ERROR_NAME (type));
1075 break;
1076
1077 case TYPE_CODE_UNDEF:
1078 /* This happens (without TYPE_STUB set) on systems which don't use
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. */
1081 fprintf_styled (stream, metadata_style.style (), _("<incomplete type>"));
1082 break;
1083
1084 case TYPE_CODE_COMPLEX:
1085 generic_value_print_complex (val, stream, options, decorations);
1086 break;
1087
1088 case TYPE_CODE_METHODPTR:
1089 cplus_print_method_ptr (val->contents_for_printing ().data (), type,
1090 stream);
1091 break;
1092
1093 case TYPE_CODE_UNION:
1094 case TYPE_CODE_STRUCT:
1095 default:
1096 error (_("Unhandled type code %d in symbol table."),
1097 type->code ());
1098 }
1099}
1100
1101/* Print using the given LANGUAGE the value VAL onto stream STREAM according
1102 to OPTIONS.
1103
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)
1111{
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
1119 if (value->lazy ())
1120 value->fetch_lazy ();
1121
1122 struct value_print_options local_opts = *options;
1123 struct type *type = value->type ();
1124 struct type *real_type = check_typedef (type);
1125
1126 if (local_opts.prettyformat == Val_prettyformat_default)
1127 local_opts.prettyformat = (local_opts.prettyformat_structs
1128 ? Val_prettyformat : Val_no_prettyformat);
1129
1130 QUIT;
1131
1132 if (!valprint_check_validity (stream, real_type, 0, value))
1133 return;
1134
1135 if (!options->raw)
1136 {
1137 if (apply_ext_lang_val_pretty_printer (value, stream, recurse, options,
1138 language))
1139 return;
1140 }
1141
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
1152 /* Handle summary mode. If the value is a scalar, print it;
1153 otherwise, print an ellipsis. */
1154 if (options->summary && !val_print_scalar_type_p (type))
1155 {
1156 gdb_printf (stream, "...");
1157 return;
1158 }
1159
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
1165 try
1166 {
1167 language->value_print_inner (value, stream, recurse, &local_opts);
1168 }
1169 catch (const gdb_exception_error &except)
1170 {
1171 fprintf_styled (stream, metadata_style.style (),
1172 _("<error reading variable: %s>"), except.what ());
1173 }
1174}
1175
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 {
1185 gdb_assert (language->struct_too_deep_ellipsis () != NULL);
1186 gdb_puts (language->struct_too_deep_ellipsis (), stream);
1187 return true;
1188 }
1189
1190 return false;
1191}
1192
1193/* Check whether the value VAL is printable. Return 1 if it is;
1194 return 0 and print an appropriate error message to STREAM according to
1195 OPTIONS if it is not. */
1196
1197static int
1198value_check_printable (struct value *val, struct ui_file *stream,
1199 const struct value_print_options *options)
1200{
1201 if (val == 0)
1202 {
1203 fprintf_styled (stream, metadata_style.style (),
1204 _("<address of value unknown>"));
1205 return 0;
1206 }
1207
1208 if (val->entirely_optimized_out ())
1209 {
1210 if (options->summary && !val_print_scalar_type_p (val->type ()))
1211 gdb_printf (stream, "...");
1212 else
1213 val_print_optimized_out (val, stream);
1214 return 0;
1215 }
1216
1217 if (val->entirely_unavailable ())
1218 {
1219 if (options->summary && !val_print_scalar_type_p (val->type ()))
1220 gdb_printf (stream, "...");
1221 else
1222 val_print_unavailable (stream);
1223 return 0;
1224 }
1225
1226 if (val->type ()->code () == TYPE_CODE_INTERNAL_FUNCTION)
1227 {
1228 fprintf_styled (stream, metadata_style.style (),
1229 _("<internal function %s>"),
1230 value_internal_function_name (val));
1231 return 0;
1232 }
1233
1234 if (type_not_allocated (val->type ()))
1235 {
1236 val_print_not_allocated (stream);
1237 return 0;
1238 }
1239
1240 return 1;
1241}
1242
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
1256/* Print on stream STREAM the value VAL according to OPTIONS. The value
1257 is printed using the current_language syntax. */
1258
1259void
1260value_print (struct value *val, struct ui_file *stream,
1261 const struct value_print_options *options)
1262{
1263 scoped_value_mark free_values;
1264
1265 if (!value_check_printable (val, stream, options))
1266 return;
1267
1268 if (!options->raw)
1269 {
1270 int r
1271 = apply_ext_lang_val_pretty_printer (val, stream, 0, options,
1272 current_language);
1273
1274 if (r)
1275 return;
1276 }
1277
1278 current_language->value_print (val, stream, options);
1279}
1280
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
1293static void
1294val_print_type_code_flags (struct type *type, struct value *original_value,
1295 int embedded_offset, struct ui_file *stream)
1296{
1297 const gdb_byte *valaddr = (original_value->contents_for_printing ().data ()
1298 + embedded_offset);
1299 ULONGEST val = unpack_long (type, valaddr);
1300 int field, nfields = type->num_fields ();
1301 struct gdbarch *gdbarch = type->arch ();
1302 struct type *bool_type = builtin_type (gdbarch)->builtin_bool;
1303
1304 gdb_puts ("[", stream);
1305 for (field = 0; field < nfields; field++)
1306 {
1307 if (type->field (field).name ()[0] != '\0')
1308 {
1309 struct type *field_type = type->field (field).type ();
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. */
1316 && type->field (field).bitsize () == 1)
1317 {
1318 if (val & ((ULONGEST)1 << type->field (field).loc_bitpos ()))
1319 gdb_printf
1320 (stream, " %ps",
1321 styled_string (variable_name_style.style (),
1322 type->field (field).name ()));
1323 }
1324 else
1325 {
1326 unsigned field_len = type->field (field).bitsize ();
1327 ULONGEST field_val = val >> type->field (field).loc_bitpos ();
1328
1329 if (field_len < sizeof (ULONGEST) * TARGET_CHAR_BIT)
1330 field_val &= ((ULONGEST) 1 << field_len) - 1;
1331 gdb_printf (stream, " %ps=",
1332 styled_string (variable_name_style.style (),
1333 type->field (field).name ()));
1334 if (field_type->code () == TYPE_CODE_ENUM)
1335 generic_val_print_enum_1 (field_type, field_val, stream);
1336 else
1337 print_longest (stream, 'd', 0, field_val);
1338 }
1339 }
1340 }
1341 gdb_puts (" ]", stream);
1342}
1343
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{
1352 struct type *type = check_typedef (val->type ());
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;
1363 opts.deref_ref = false;
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. */
1371 const gdb_byte *valaddr = val->contents_for_printing ().data ();
1372
1373 /* A scalar object that does not have all bits available can't be
1374 printed, because all bits contribute to its representation. */
1375 if (val->bits_any_optimized_out (0,
1376 TARGET_CHAR_BIT * type->length ()))
1377 val_print_optimized_out (val, stream);
1378 else if (!val->bytes_available (0, type->length ()))
1379 val_print_unavailable (stream);
1380 else
1381 print_scalar_formatted (valaddr, type, options, size, stream);
1382}
1383
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
1386 LONG_LONG's into this one function. The format chars b,h,w,g are
1387 from print_scalar_formatted(). Numbers are printed using C
1388 format.
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
1395 and was intended to request formatting according to the current
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
1401 that capability. */
1402
1403void
1404print_longest (struct ui_file *stream, int format, int use_c_format,
1405 LONGEST val_long)
1406{
1407 const char *val;
1408
1409 switch (format)
1410 {
1411 case 'd':
1412 val = int_string (val_long, 10, 1, 0, 1); break;
1413 case 'u':
1414 val = int_string (val_long, 10, 0, 0, 1); break;
1415 case 'x':
1416 val = int_string (val_long, 16, 0, 0, use_c_format); break;
1417 case 'b':
1418 val = int_string (val_long, 16, 0, 2, 1); break;
1419 case 'h':
1420 val = int_string (val_long, 16, 0, 4, 1); break;
1421 case 'w':
1422 val = int_string (val_long, 16, 0, 8, 1); break;
1423 case 'g':
1424 val = int_string (val_long, 16, 0, 16, 1); break;
1425 break;
1426 case 'o':
1427 val = int_string (val_long, 8, 0, 0, use_c_format); break;
1428 default:
1429 internal_error (_("failed internal consistency check"));
1430 }
1431 gdb_puts (val, stream);
1432}
1433
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
1441longest_to_int (LONGEST arg)
1442{
1443 /* Let the compiler do the work. */
1444 int rtnval = (int) arg;
1445
1446 /* Check for overflows or underflows. */
1447 if (sizeof (LONGEST) > sizeof (int))
1448 {
1449 if (rtnval != arg)
1450 {
1451 error (_("Value out of range."));
1452 }
1453 }
1454 return (rtnval);
1455}
1456
1457/* Print a floating point value of floating-point type TYPE,
1458 pointed to in GDB by VALADDR, on STREAM. */
1459
1460void
1461print_floating (const gdb_byte *valaddr, struct type *type,
1462 struct ui_file *stream)
1463{
1464 std::string str = target_float_to_string (valaddr, type);
1465 gdb_puts (str.c_str (), stream);
1466}
1467
1468void
1469print_binary_chars (struct ui_file *stream, const gdb_byte *valaddr,
1470 unsigned len, enum bfd_endian byte_order, bool zero_pad,
1471 const struct value_print_options *options)
1472{
1473 const gdb_byte *p;
1474 unsigned int i;
1475 int b;
1476 bool seen_a_one = false;
1477 const char *digit_separator = nullptr;
1478
1479 /* Declared "int" so it will be signed.
1480 This ensures that right shift will shift in zeros. */
1481
1482 const int mask = 0x080;
1483
1484 if (options->nibblesprint)
1485 digit_separator = current_language->get_digit_separator();
1486
1487 if (byte_order == BFD_ENDIAN_BIG)
1488 {
1489 for (p = valaddr;
1490 p < valaddr + len;
1491 p++)
1492 {
1493 /* Every byte has 8 binary characters; peel off
1494 and print from the MSB end. */
1495
1496 for (i = 0; i < (HOST_CHAR_BIT * sizeof (*p)); i++)
1497 {
1498 if (options->nibblesprint && seen_a_one && i % 4 == 0)
1499 gdb_putc (*digit_separator, stream);
1500
1501 if (*p & (mask >> i))
1502 b = '1';
1503 else
1504 b = '0';
1505
1506 if (zero_pad || seen_a_one || b == '1')
1507 gdb_putc (b, stream);
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
1515 if (b == '1')
1516 seen_a_one = true;
1517 }
1518 }
1519 }
1520 else
1521 {
1522 for (p = valaddr + len - 1;
1523 p >= valaddr;
1524 p--)
1525 {
1526 for (i = 0; i < (HOST_CHAR_BIT * sizeof (*p)); i++)
1527 {
1528 if (options->nibblesprint && seen_a_one && i % 4 == 0)
1529 gdb_putc (*digit_separator, stream);
1530
1531 if (*p & (mask >> i))
1532 b = '1';
1533 else
1534 b = '0';
1535
1536 if (zero_pad || seen_a_one || b == '1')
1537 gdb_putc (b, stream);
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
1545 if (b == '1')
1546 seen_a_one = true;
1547 }
1548 }
1549 }
1550
1551 /* When not zero-padding, ensure that something is printed when the
1552 input is 0. */
1553 if (!zero_pad && !seen_a_one)
1554 gdb_putc ('0', stream);
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)
1564 gdb_printf (stream, "%o", digit);
1565 if (digit != 0)
1566 *seen_a_one = true;
1567}
1568
1569/* VALADDR points to an integer of LEN bytes.
1570 Print it in octal on stream or format it in buf. */
1571
1572void
1573print_octal_chars (struct ui_file *stream, const gdb_byte *valaddr,
1574 unsigned len, enum bfd_endian byte_order)
1575{
1576 const gdb_byte *p;
1577 unsigned char octa1, octa2, octa3, carry;
1578 int cycle;
1579
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
1597#define LOW_ZERO 0034
1598#define CARRY_ZERO 0003
1599 static_assert (HIGH_ZERO + LOW_ZERO + CARRY_ZERO == 0xff,
1600 "cycle zero constants are wrong");
1601#define HIGH_ONE 0200
1602#define MID_ONE 0160
1603#define LOW_ONE 0016
1604#define CARRY_ONE 0001
1605 static_assert (HIGH_ONE + MID_ONE + LOW_ONE + CARRY_ONE == 0xff,
1606 "cycle one constants are wrong");
1607#define HIGH_TWO 0300
1608#define MID_TWO 0070
1609#define LOW_TWO 0007
1610 static_assert (HIGH_TWO + MID_TWO + LOW_TWO == 0xff,
1611 "cycle two constants are wrong");
1612
1613 /* For 32 we start in cycle 2, with two bits and one bit carry;
1614 for 64 in cycle in cycle 1, with one bit and a two bit carry. */
1615
1616 cycle = (len * HOST_CHAR_BIT) % BITS_IN_OCTAL;
1617 carry = 0;
1618
1619 gdb_puts ("0", stream);
1620 bool seen_a_one = false;
1621 if (byte_order == BFD_ENDIAN_BIG)
1622 {
1623 for (p = valaddr;
1624 p < valaddr + len;
1625 p++)
1626 {
1627 switch (cycle)
1628 {
1629 case 0:
1630 /* No carry in, carry out two bits. */
1631
1632 octa1 = (HIGH_ZERO & *p) >> 5;
1633 octa2 = (LOW_ZERO & *p) >> 2;
1634 carry = (CARRY_ZERO & *p);
1635 emit_octal_digit (stream, &seen_a_one, octa1);
1636 emit_octal_digit (stream, &seen_a_one, octa2);
1637 break;
1638
1639 case 1:
1640 /* Carry in two bits, carry out one bit. */
1641
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);
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);
1649 break;
1650
1651 case 2:
1652 /* Carry in one bit, no carry out. */
1653
1654 octa1 = (carry << 2) | ((HIGH_TWO & *p) >> 6);
1655 octa2 = (MID_TWO & *p) >> 3;
1656 octa3 = (LOW_TWO & *p);
1657 carry = 0;
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);
1661 break;
1662
1663 default:
1664 error (_("Internal error in octal conversion;"));
1665 }
1666
1667 cycle++;
1668 cycle = cycle % BITS_IN_OCTAL;
1669 }
1670 }
1671 else
1672 {
1673 for (p = valaddr + len - 1;
1674 p >= valaddr;
1675 p--)
1676 {
1677 switch (cycle)
1678 {
1679 case 0:
1680 /* Carry out, no carry in */
1681
1682 octa1 = (HIGH_ZERO & *p) >> 5;
1683 octa2 = (LOW_ZERO & *p) >> 2;
1684 carry = (CARRY_ZERO & *p);
1685 emit_octal_digit (stream, &seen_a_one, octa1);
1686 emit_octal_digit (stream, &seen_a_one, octa2);
1687 break;
1688
1689 case 1:
1690 /* Carry in, carry out */
1691
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);
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);
1699 break;
1700
1701 case 2:
1702 /* Carry in, no carry out */
1703
1704 octa1 = (carry << 2) | ((HIGH_TWO & *p) >> 6);
1705 octa2 = (MID_TWO & *p) >> 3;
1706 octa3 = (LOW_TWO & *p);
1707 carry = 0;
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);
1711 break;
1712
1713 default:
1714 error (_("Internal error in octal conversion;"));
1715 }
1716
1717 cycle++;
1718 cycle = cycle % BITS_IN_OCTAL;
1719 }
1720 }
1721
1722}
1723
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,
1732 gdb::byte_vector *out_vec)
1733{
1734 gdb_byte sign_byte;
1735 gdb_assert (len > 0);
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
1770/* VALADDR points to an integer of LEN bytes.
1771 Print it in decimal on stream or format it in buf. */
1772
1773void
1774print_decimal_chars (struct ui_file *stream, const gdb_byte *valaddr,
1775 unsigned len, bool is_signed,
1776 enum bfd_endian byte_order)
1777{
1778#define TEN 10
1779#define CARRY_OUT( x ) ((x) / TEN) /* extend char to int */
1780#define CARRY_LEFT( x ) ((x) % TEN)
1781#define SHIFT( x ) ((x) << 4)
1782#define LOW_NIBBLE( x ) ( (x) & 0x00F)
1783#define HIGH_NIBBLE( x ) (((x) & 0x0F0) >> 4)
1784
1785 const gdb_byte *p;
1786 int carry;
1787 int decimal_len;
1788 int i, j, decimal_digits;
1789 int dummy;
1790 int flip;
1791
1792 gdb::byte_vector negated_bytes;
1793 if (is_signed
1794 && maybe_negate_by_bytes (valaddr, len, byte_order, &negated_bytes))
1795 {
1796 gdb_puts ("-", stream);
1797 valaddr = negated_bytes.data ();
1798 }
1799
1800 /* Base-ten number is less than twice as many digits
1801 as the base 16 number, which is 2 digits per byte. */
1802
1803 decimal_len = len * 2 * 2;
1804 std::vector<unsigned char> digits (decimal_len, 0);
1805
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
1810 * decimalizing it as "x1 x2" in two decimal nibbles. Then we multiply
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
1814 * the individual digits are > 10.
1815 *
1816 * Outer loop is per nibble (hex digit) of input, from MSD end to
1817 * LSD end.
1818 */
1819 decimal_digits = 0; /* Number of decimal digits so far */
1820 p = (byte_order == BFD_ENDIAN_BIG) ? valaddr : valaddr + len - 1;
1821 flip = 0;
1822 while ((byte_order == BFD_ENDIAN_BIG) ? (p < valaddr + len) : (p >= valaddr))
1823 {
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 */
1829 for (j = 0; j < decimal_digits; j++)
1830 {
1831 digits[j] = SHIFT (digits[j]);
1832 }
1833
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 */
1840 if (flip == 0)
1841 {
1842 /* Take top nibble. */
1843
1844 digits[0] += HIGH_NIBBLE (*p);
1845 flip = 1;
1846 }
1847 else
1848 {
1849 /* Take low nibble and bump our pointer "p". */
1850
1851 digits[0] += LOW_NIBBLE (*p);
1852 if (byte_order == BFD_ENDIAN_BIG)
1853 p++;
1854 else
1855 p--;
1856 flip = 0;
1857 }
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;
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 }
1894
1895 /* Ok, now "digits" is the decimal representation, with
1896 the "decimal_digits" actual digits. Print! */
1897
1898 for (i = decimal_digits - 1; i > 0 && digits[i] == 0; --i)
1899 ;
1900
1901 for (; i >= 0; i--)
1902 {
1903 gdb_printf (stream, "%1d", digits[i]);
1904 }
1905}
1906
1907/* VALADDR points to an integer of LEN bytes. Print it in hex on stream. */
1908
1909void
1910print_hex_chars (struct ui_file *stream, const gdb_byte *valaddr,
1911 unsigned len, enum bfd_endian byte_order,
1912 bool zero_pad)
1913{
1914 const gdb_byte *p;
1915
1916 gdb_puts ("0x", stream);
1917 if (byte_order == BFD_ENDIAN_BIG)
1918 {
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 (;
1931 p < valaddr + len;
1932 p++)
1933 {
1934 /* When not zero-padding, use a different format for the
1935 very first byte printed. */
1936 if (!zero_pad && p == first)
1937 gdb_printf (stream, "%x", *p);
1938 else
1939 gdb_printf (stream, "%02x", *p);
1940 }
1941 }
1942 else
1943 {
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 (;
1956 p >= valaddr;
1957 p--)
1958 {
1959 /* When not zero-padding, use a different format for the
1960 very first byte printed. */
1961 if (!zero_pad && p == first)
1962 gdb_printf (stream, "%x", *p);
1963 else
1964 gdb_printf (stream, "%02x", *p);
1965 }
1966 }
1967}
1968
1969/* Print function pointer with inferior address ADDRESS onto stdio
1970 stream STREAM. */
1971
1972void
1973print_function_pointer_address (const struct value_print_options *options,
1974 struct gdbarch *gdbarch,
1975 CORE_ADDR address,
1976 struct ui_file *stream)
1977{
1978 CORE_ADDR func_addr = gdbarch_convert_from_func_ptr_addr
1979 (gdbarch, address, current_inferior ()->top_target ());
1980
1981 /* If the function pointer is represented by a description, print
1982 the address of the description. */
1983 if (options->addressprint && func_addr != address)
1984 {
1985 gdb_puts ("@", stream);
1986 gdb_puts (paddress (gdbarch, address), stream);
1987 gdb_puts (": ", stream);
1988 }
1989 print_address_demangle (options, gdbarch, func_addr, stream, demangle);
1990}
1991
1992
1993/* Print on STREAM using the given OPTIONS the index for the element
1994 at INDEX of an array whose index type is INDEX_TYPE. */
1995
1996void
1997maybe_print_array_index (struct type *index_type, LONGEST index,
1998 struct ui_file *stream,
1999 const struct value_print_options *options)
2000{
2001 if (!options->print_array_indexes)
2002 return;
2003
2004 current_language->print_array_index (index_type, index, stream, options);
2005}
2006
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;
2017 struct type *elttype, *index_type;
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;
2024
2025 struct type *type = check_typedef (val->type ());
2026
2027 elttype = type->target_type ();
2028 unsigned bit_stride = type->bit_stride ();
2029 if (bit_stride == 0)
2030 bit_stride = 8 * check_typedef (elttype)->length ();
2031 index_type = type->index_type ();
2032 if (index_type->code () == TYPE_CODE_RANGE)
2033 index_type = index_type->target_type ();
2034
2035 if (get_array_bounds (type, &low_bound, &high_bound))
2036 {
2037 /* The array length should normally be HIGH_BOUND - LOW_BOUND +
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! */
2042 if (low_bound > high_bound)
2043 len = 0;
2044 else
2045 len = high_bound - low_bound + 1;
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 {
2064 gdb_printf (stream, ",\n");
2065 print_spaces (2 + 2 * recurse, stream);
2066 }
2067 else
2068 gdb_printf (stream, ", ");
2069 }
2070 else if (options->prettyformat_arrays)
2071 {
2072 gdb_printf (stream, "\n");
2073 print_spaces (2 + 2 * recurse, stream);
2074 }
2075 stream->wrap_here (2 + 2 * recurse);
2076 maybe_print_array_index (index_type, i + low_bound,
2077 stream, options);
2078
2079 struct value *element = val->from_component_bitsize (elttype,
2080 bit_stride * i,
2081 bit_stride);
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 {
2088 bool unavailable = element->entirely_unavailable ();
2089 bool available = element->entirely_available ();
2090
2091 while (rep1 < len)
2092 {
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;
2097 struct value *rep_elt
2098 = val->from_component_bitsize (elttype,
2099 rep1 * bit_stride,
2100 bit_stride);
2101 bool repeated = ((available
2102 && rep_elt->entirely_available ()
2103 && element->contents_eq (rep_elt))
2104 || (unavailable
2105 && rep_elt->entirely_unavailable ()));
2106 if (!repeated)
2107 break;
2108 ++reps;
2109 ++rep1;
2110 }
2111 }
2112
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);
2119 gdb_printf (stream, " %p[<repeats %u times>%p]",
2120 metadata_style.style ().ptr (), reps, nullptr);
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)
2134 gdb_printf (stream, "...");
2135 if (options->prettyformat_arrays)
2136 {
2137 gdb_printf (stream, "\n");
2138 print_spaces (2 * recurse, stream);
2139 }
2140}
2141
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,
2183 int quoter, bool *need_escapep)
2184{
2185 bool need_escape = *need_escapep;
2186
2187 *need_escapep = false;
2188
2189 /* If any additional cases are added to this switch block, then the
2190 function wchar_printable will likely need updating too. */
2191 switch (w)
2192 {
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:
2215 {
2216 if (gdb_iswprint (w) && !(need_escape && gdb_iswxdigit (w)))
2217 {
2218 gdb_wchar_t wchar = w;
2219
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;
2227
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,
2234 byte_order);
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)
2238 {
2239 xsnprintf (octal, sizeof (octal), "\\%.3o",
2240 (int) (value & 0777));
2241 *need_escapep = false;
2242 }
2243 else
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 }
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);
2259 *need_escapep = false;
2260 append_string_as_wide (octal, output);
2261 ++i;
2262 }
2263 }
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
2278 = type_byte_order (type);
2279 gdb_byte *c_buf;
2280 bool need_escape = false;
2281
2282 c_buf = (gdb_byte *) alloca (type->length ());
2283 pack_long (c_buf, type, c);
2284
2285 wchar_iterator iter (c_buf, type->length (), encoding, type->length ());
2286
2287 /* This holds the printable form of the wchar_t data. */
2288 auto_obstack wchar_buf;
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
2299 num_chars = iter.iterate (&result, &chars, &buf, &buflen);
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,
2323 type->length (), byte_order,
2324 &wchar_buf, quoter, &need_escape);
2325 }
2326 }
2327
2328 /* This handles the NUM_CHARS == 0 case as well. */
2329 if (print_escape)
2330 print_wchar (gdb_WEOF, buf, buflen, type->length (),
2331 byte_order, &wchar_buf, quoter, &need_escape);
2332 }
2333
2334 /* The output in the host encoding. */
2335 auto_obstack output;
2336
2337 convert_between_encodings (INTERMEDIATE_ENCODING, host_charset (),
2338 (gdb_byte *) obstack_base (&wchar_buf),
2339 obstack_object_size (&wchar_buf),
2340 sizeof (gdb_wchar_t), &output, translit_char);
2341 obstack_1grow (&output, '\0');
2342
2343 gdb_puts ((const char *) obstack_base (&output), stream);
2344}
2345
2346/* Return the repeat count of the next character/byte in ITER,
2347 storing the result in VEC. */
2348
2349static int
2350count_next_character (wchar_iterator *iter,
2351 std::vector<converted_character> *vec)
2352{
2353 struct converted_character *current;
2354
2355 if (vec->empty ())
2356 {
2357 struct converted_character tmp;
2358 gdb_wchar_t *chars;
2359
2360 tmp.num_chars
2361 = iter->iterate (&tmp.result, &chars, &tmp.buf, &tmp.buflen);
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 }
2367 vec->push_back (tmp);
2368 }
2369
2370 current = &vec->back ();
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. */
2390 d.num_chars = iter->iterate (&d.result, &chars, &d.buf, &d.buflen);
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;
2424 vec->push_back (d);
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
2431 character type. BYTE_ORDER is the target byte order. OPTIONS
2432 is the user's print options. *FINISHED is set to 0 if we didn't print
2433 all the elements in CHARS. */
2434
2435static void
2436print_converted_chars_to_obstack (struct obstack *obstack,
2437 const std::vector<converted_character> &chars,
2438 int quote_char, int width,
2439 enum bfd_endian byte_order,
2440 const struct value_print_options *options,
2441 int *finished)
2442{
2443 unsigned int idx, num_elements;
2444 const converted_character *elem;
2445 enum {START, SINGLE, REPEAT, INCOMPLETE, FINISH} state, last;
2446 gdb_wchar_t wide_quote_char = gdb_btowc (quote_char);
2447 bool need_escape = false;
2448 const int print_max = options->print_max_chars > 0
2449 ? options->print_max_chars : options->print_max;
2450
2451 /* Set the start state. */
2452 idx = num_elements = 0;
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 (", "));
2477 obstack_grow (obstack, &wide_quote_char, sizeof (gdb_wchar_t));
2478 }
2479 /* Output the character. */
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)
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);
2494 num_elements += 1;
2495 }
2496 }
2497 break;
2498
2499 case REPEAT:
2500 {
2501 int j;
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. */
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 ("'"));
2524 std::string s = string_printf (_(" <repeats %u times>"),
2525 elem->repeat_count);
2526 num_elements += elem->repeat_count;
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 }
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. */
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 (">"));
2551 num_elements += 1;
2552
2553 /* We do not attempt to output anything after this. */
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)
2562 obstack_grow (obstack, &wide_quote_char, sizeof (gdb_wchar_t));
2563
2564 return;
2565 }
2566
2567 /* Get the next element and state. */
2568 last = state;
2569 if (state != FINISH)
2570 {
2571 elem = &chars[idx++];
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
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;
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.
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{
2611 enum bfd_endian byte_order = type_byte_order (type);
2612 unsigned int i;
2613 int width = type->length ();
2614 int finished = 0;
2615 struct converted_character *last;
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 {
2642 gdb_printf (stream, "%c%c", quote_char, quote_char);
2643 return;
2644 }
2645
2646 /* Arrange to iterate over the characters, in wchar_t form. */
2647 wchar_iterator iter (string, length * width, encoding, width);
2648 std::vector<converted_character> converted_chars;
2649
2650 /* Convert characters until the string is over or the maximum
2651 number of printed characters has been reached. */
2652 i = 0;
2653 unsigned int print_max_chars = get_print_max_chars (options);
2654 while (i < print_max_chars)
2655 {
2656 int r;
2657
2658 QUIT;
2659
2660 /* Grab the next character and repeat count. */
2661 r = count_next_character (&iter, &converted_chars);
2662
2663 /* If less than zero, the end of the input string was reached. */
2664 if (r < 0)
2665 break;
2666
2667 /* Otherwise, add the count to the total print count and get
2668 the next character. */
2669 i += r;
2670 }
2671
2672 /* Get the last element and determine if the entire string was
2673 processed. */
2674 last = &converted_chars.back ();
2675 finished = (last->result == wchar_iterate_eof);
2676
2677 /* Ensure that CONVERTED_CHARS is terminated. */
2678 last->result = wchar_iterate_eof;
2679
2680 /* WCHAR_BUF is the obstack we use to represent the string in
2681 wchar_t form. */
2682 auto_obstack wchar_buf;
2683
2684 /* Print the output string to the obstack. */
2685 print_converted_chars_to_obstack (&wchar_buf, converted_chars, quote_char,
2686 width, byte_order, options, &finished);
2687
2688 if (force_ellipses || !finished)
2689 obstack_grow_wstr (&wchar_buf, LCST ("..."));
2690
2691 /* OUTPUT is where we collect `char's for printing. */
2692 auto_obstack output;
2693
2694 convert_between_encodings (INTERMEDIATE_ENCODING, host_charset (),
2695 (gdb_byte *) obstack_base (&wchar_buf),
2696 obstack_object_size (&wchar_buf),
2697 sizeof (gdb_wchar_t), &output, translit_char);
2698 obstack_1grow (&output, '\0');
2699
2700 gdb_puts ((const char *) obstack_base (&output), stream);
2701}
2702
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
2706 bytes) until either print_max_chars or LEN characters have been printed,
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. */
2710
2711int
2712val_print_string (struct type *elttype, const char *encoding,
2713 CORE_ADDR addr, int len,
2714 struct ui_file *stream,
2715 const struct value_print_options *options)
2716{
2717 int force_ellipsis = 0; /* Force ellipsis to be printed if nonzero. */
2718 int err; /* Non-zero if we got a bad read. */
2719 int found_nul; /* Non-zero if we found the nul char. */
2720 unsigned int fetchlimit; /* Maximum number of chars to print. */
2721 int bytes_read;
2722 gdb::unique_xmalloc_ptr<gdb_byte> buffer; /* Dynamically growable fetch buffer. */
2723 struct gdbarch *gdbarch = elttype->arch ();
2724 enum bfd_endian byte_order = type_byte_order (elttype);
2725 int width = elttype->length ();
2726
2727 /* First we need to figure out the limit on the number of characters we are
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));
2739
2740 err = target_read_string (addr, len, width, fetchlimit,
2741 &buffer, &bytes_read);
2742
2743 addr += bytes_read;
2744
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. */
2748
2749 /* Determine found_nul by looking at the last character read. */
2750 found_nul = 0;
2751 if (bytes_read >= width)
2752 found_nul = extract_unsigned_integer (buffer.get () + bytes_read - width,
2753 width, byte_order) == 0;
2754 if (len == -1 && !found_nul)
2755 {
2756 gdb_byte *peekbuf;
2757
2758 /* We didn't find a NUL terminator we were looking for. Attempt
2759 to peek at the next character. If not successful, or it is not
2760 a null byte, then force ellipsis to be printed. */
2761
2762 peekbuf = (gdb_byte *) alloca (width);
2763
2764 if (target_read_memory (addr, peekbuf, width) == 0
2765 && extract_unsigned_integer (peekbuf, width, byte_order) != 0)
2766 force_ellipsis = 1;
2767 }
2768 else if ((len >= 0 && err != 0) || (len > bytes_read / width))
2769 {
2770 /* Getting an error when we have a requested length, or fetching less
2771 than the number of characters actually requested, always make us
2772 print ellipsis. */
2773 force_ellipsis = 1;
2774 }
2775
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. */
2779 if (err == 0 || bytes_read > 0)
2780 current_language->printstr (stream, elttype, buffer.get (),
2781 bytes_read / width,
2782 encoding, force_ellipsis, options);
2783
2784 if (err != 0)
2785 {
2786 std::string str = memory_error_message (TARGET_XFER_E_IO, gdbarch, addr);
2787
2788 gdb_printf (stream, _("<error: %ps>"),
2789 styled_string (metadata_style.style (),
2790 str.c_str ()));
2791 }
2792
2793 return (bytes_read / width);
2794}
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{
2802 gdb_printf (file, _("Maximum print depth is %s.\n"), value);
2803}
2804\f
2805
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
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
2816static void
2817set_input_radix (const char *args, int from_tty, struct cmd_list_element *c)
2818{
2819 set_input_radix_1 (from_tty, input_radix_1);
2820}
2821
2822static void
2823set_input_radix_1 (int from_tty, unsigned radix)
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.
2830 (FIXME). */
2831
2832 if (radix < 2)
2833 {
2834 input_radix_1 = input_radix;
2835 error (_("Nonsense input radix ``decimal %u''; input radix unchanged."),
2836 radix);
2837 }
2838 input_radix_1 = input_radix = radix;
2839 if (from_tty)
2840 {
2841 gdb_printf (_("Input radix now set to "
2842 "decimal %u, hex %x, octal %o.\n"),
2843 radix, radix, radix);
2844 }
2845}
2846
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
2853static void
2854set_output_radix (const char *args, int from_tty, struct cmd_list_element *c)
2855{
2856 set_output_radix_1 (from_tty, output_radix_1);
2857}
2858
2859static void
2860set_output_radix_1 (int from_tty, unsigned radix)
2861{
2862 /* Validate the radix and disallow ones that we aren't prepared to
2863 handle correctly, leaving the radix unchanged. */
2864 switch (radix)
2865 {
2866 case 16:
2867 user_print_options.output_format = 'x'; /* hex */
2868 break;
2869 case 10:
2870 user_print_options.output_format = 0; /* decimal */
2871 break;
2872 case 8:
2873 user_print_options.output_format = 'o'; /* octal */
2874 break;
2875 default:
2876 output_radix_1 = output_radix;
2877 error (_("Unsupported output radix ``decimal %u''; "
2878 "output radix unchanged."),
2879 radix);
2880 }
2881 output_radix_1 = output_radix = radix;
2882 if (from_tty)
2883 {
2884 gdb_printf (_("Output radix now set to "
2885 "decimal %u, hex %x, octal %o.\n"),
2886 radix, radix, radix);
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
2896 the 'set input-radix' command. */
2897
2898static void
2899set_radix (const char *arg, int from_tty)
2900{
2901 unsigned radix;
2902
2903 radix = (arg == NULL) ? 10 : parse_and_eval_long (arg);
2904 set_output_radix_1 (0, radix);
2905 set_input_radix_1 (0, radix);
2906 if (from_tty)
2907 {
2908 gdb_printf (_("Input and output radices now set to "
2909 "decimal %u, hex %x, octal %o.\n"),
2910 radix, radix, radix);
2911 }
2912}
2913
2914/* Show both the input and output radices. */
2915
2916static void
2917show_radix (const char *arg, int from_tty)
2918{
2919 if (from_tty)
2920 {
2921 if (input_radix == output_radix)
2922 {
2923 gdb_printf (_("Input and output radices set to "
2924 "decimal %u, hex %x, octal %o.\n"),
2925 input_radix, input_radix, input_radix);
2926 }
2927 else
2928 {
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);
2935 }
2936 }
2937}
2938\f
2939
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{
2945 gdb_printf (file, _("\
2946Printing of C++ virtual function tables is %s.\n"),
2947 value);
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{
2957 gdb_printf (file, _("\
2958Printing of object's derived type based on vtable info is %s.\n"),
2959 value);
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{
2967 gdb_printf (file,
2968 _("Printing of C++ static members is %s.\n"),
2969 value);
2970}
2971
2972\f
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>;
2980using pinteger_option_def
2981 = gdb::option::pinteger_option_def<value_print_options>;
2982
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
2992/* Definitions of options for the "print" and "compile print"
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."),
3019 N_("Show printing of array indexes."),
3020 NULL, /* help_doc */
3021 },
3022
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
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
3043 uinteger_option_def {
3044 "elements",
3045 [] (value_print_options *opt) { return &opt->print_max; },
3046 uinteger_unlimited_literals,
3047 show_print_max, /* show_cmd_cb */
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\"."),
3053 },
3054
3055 pinteger_option_def {
3056 "max-depth",
3057 [] (value_print_options *opt) { return &opt->max_depth; },
3058 pinteger_unlimited_literals,
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
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
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
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
3115 uinteger_option_def {
3116 "repeats",
3117 [] (value_print_options *opt) { return &opt->repeat_count_threshold; },
3118 uinteger_unlimited_literals,
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
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
3186 value *val = value::allocate (flags_type);
3187 gdb_byte *contents = val->contents_writeable ().data ();
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
3197INIT_GDB_FILE (valprint)
3198{
3199#if GDB_SELF_TEST
3200 selftests::register_test_foreach_arch ("print-flags", test_print_flags);
3201#endif
3202
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);
3210 /* Prefer set print to set prompt. */
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);
3223
3224 gdb::option::add_setshow_cmds_for_options
3225 (class_support, &user_print_options, value_print_option_defs,
3226 &setprintlist, &showprintlist);
3227
3228 add_setshow_zuinteger_cmd ("input-radix", class_support, &input_radix_1,
3229 _("\
3230Set default input radix for entering numbers."), _("\
3231Show default input radix for entering numbers."), NULL,
3232 set_input_radix,
3233 show_input_radix,
3234 &setlist, &showlist);
3235
3236 add_setshow_zuinteger_cmd ("output-radix", class_support, &output_radix_1,
3237 _("\
3238Set default output radix for printing of values."), _("\
3239Show default output radix for printing of values."), NULL,
3240 set_output_radix,
3241 show_output_radix,
3242 &setlist, &showlist);
3243
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
3247 command. So the usual deprecated_add_set_cmd() and [deleted]
3248 add_show_from_set() commands aren't really appropriate. */
3249 /* FIXME: i18n: With the new add_setshow_integer command, that is no
3250 longer true - show can display anything. */
3251 add_cmd ("radix", class_support, set_radix, _("\
3252Set default input and output number radices.\n\
3253Use 'set input-radix' or 'set output-radix' to independently set each.\n\
3254Without an argument, sets both radices back to the default value of 10."),
3255 &setlist);
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."),
3259 &showlist);
3260}