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