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