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