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