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