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