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