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