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