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