15796c8dcSSimon Schubert /* Print values for GDB, the GNU debugger.
25796c8dcSSimon Schubert
3*ef5ccd6cSJohn Marino Copyright (C) 1986-2013 Free Software Foundation, Inc.
45796c8dcSSimon Schubert
55796c8dcSSimon Schubert This file is part of GDB.
65796c8dcSSimon Schubert
75796c8dcSSimon Schubert This program is free software; you can redistribute it and/or modify
85796c8dcSSimon Schubert it under the terms of the GNU General Public License as published by
95796c8dcSSimon Schubert the Free Software Foundation; either version 3 of the License, or
105796c8dcSSimon Schubert (at your option) any later version.
115796c8dcSSimon Schubert
125796c8dcSSimon Schubert This program is distributed in the hope that it will be useful,
135796c8dcSSimon Schubert but WITHOUT ANY WARRANTY; without even the implied warranty of
145796c8dcSSimon Schubert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
155796c8dcSSimon Schubert GNU General Public License for more details.
165796c8dcSSimon Schubert
175796c8dcSSimon Schubert You should have received a copy of the GNU General Public License
185796c8dcSSimon Schubert along with this program. If not, see <http://www.gnu.org/licenses/>. */
195796c8dcSSimon Schubert
205796c8dcSSimon Schubert #include "defs.h"
215796c8dcSSimon Schubert #include "gdb_string.h"
225796c8dcSSimon Schubert #include "symtab.h"
235796c8dcSSimon Schubert #include "gdbtypes.h"
245796c8dcSSimon Schubert #include "value.h"
255796c8dcSSimon Schubert #include "gdbcore.h"
265796c8dcSSimon Schubert #include "gdbcmd.h"
275796c8dcSSimon Schubert #include "target.h"
285796c8dcSSimon Schubert #include "language.h"
295796c8dcSSimon Schubert #include "annotate.h"
305796c8dcSSimon Schubert #include "valprint.h"
315796c8dcSSimon Schubert #include "floatformat.h"
325796c8dcSSimon Schubert #include "doublest.h"
335796c8dcSSimon Schubert #include "exceptions.h"
345796c8dcSSimon Schubert #include "dfp.h"
355796c8dcSSimon Schubert #include "python/python.h"
36cf7f2e2dSJohn Marino #include "ada-lang.h"
37a45ae5f8SJohn Marino #include "gdb_obstack.h"
38a45ae5f8SJohn Marino #include "charset.h"
39a45ae5f8SJohn Marino #include <ctype.h>
405796c8dcSSimon Schubert
415796c8dcSSimon Schubert #include <errno.h>
425796c8dcSSimon Schubert
43*ef5ccd6cSJohn Marino /* Maximum number of wchars returned from wchar_iterate. */
44*ef5ccd6cSJohn Marino #define MAX_WCHARS 4
45*ef5ccd6cSJohn Marino
46*ef5ccd6cSJohn Marino /* A convenience macro to compute the size of a wchar_t buffer containing X
47*ef5ccd6cSJohn Marino characters. */
48*ef5ccd6cSJohn Marino #define WCHAR_BUFLEN(X) ((X) * sizeof (gdb_wchar_t))
49*ef5ccd6cSJohn Marino
50*ef5ccd6cSJohn Marino /* Character buffer size saved while iterating over wchars. */
51*ef5ccd6cSJohn Marino #define WCHAR_BUFLEN_MAX WCHAR_BUFLEN (MAX_WCHARS)
52*ef5ccd6cSJohn Marino
53*ef5ccd6cSJohn Marino /* A structure to encapsulate state information from iterated
54*ef5ccd6cSJohn Marino character conversions. */
55*ef5ccd6cSJohn Marino struct converted_character
56*ef5ccd6cSJohn Marino {
57*ef5ccd6cSJohn Marino /* The number of characters converted. */
58*ef5ccd6cSJohn Marino int num_chars;
59*ef5ccd6cSJohn Marino
60*ef5ccd6cSJohn Marino /* The result of the conversion. See charset.h for more. */
61*ef5ccd6cSJohn Marino enum wchar_iterate_result result;
62*ef5ccd6cSJohn Marino
63*ef5ccd6cSJohn Marino /* The (saved) converted character(s). */
64*ef5ccd6cSJohn Marino gdb_wchar_t chars[WCHAR_BUFLEN_MAX];
65*ef5ccd6cSJohn Marino
66*ef5ccd6cSJohn Marino /* The first converted target byte. */
67*ef5ccd6cSJohn Marino const gdb_byte *buf;
68*ef5ccd6cSJohn Marino
69*ef5ccd6cSJohn Marino /* The number of bytes converted. */
70*ef5ccd6cSJohn Marino size_t buflen;
71*ef5ccd6cSJohn Marino
72*ef5ccd6cSJohn Marino /* How many times this character(s) is repeated. */
73*ef5ccd6cSJohn Marino int repeat_count;
74*ef5ccd6cSJohn Marino };
75*ef5ccd6cSJohn Marino
76*ef5ccd6cSJohn Marino typedef struct converted_character converted_character_d;
77*ef5ccd6cSJohn Marino DEF_VEC_O (converted_character_d);
78*ef5ccd6cSJohn Marino
79*ef5ccd6cSJohn Marino
805796c8dcSSimon Schubert /* Prototypes for local functions */
815796c8dcSSimon Schubert
825796c8dcSSimon Schubert static int partial_memory_read (CORE_ADDR memaddr, gdb_byte *myaddr,
835796c8dcSSimon Schubert int len, int *errnoptr);
845796c8dcSSimon Schubert
855796c8dcSSimon Schubert static void show_print (char *, int);
865796c8dcSSimon Schubert
875796c8dcSSimon Schubert static void set_print (char *, int);
885796c8dcSSimon Schubert
895796c8dcSSimon Schubert static void set_radix (char *, int);
905796c8dcSSimon Schubert
915796c8dcSSimon Schubert static void show_radix (char *, int);
925796c8dcSSimon Schubert
935796c8dcSSimon Schubert static void set_input_radix (char *, int, struct cmd_list_element *);
945796c8dcSSimon Schubert
955796c8dcSSimon Schubert static void set_input_radix_1 (int, unsigned);
965796c8dcSSimon Schubert
975796c8dcSSimon Schubert static void set_output_radix (char *, int, struct cmd_list_element *);
985796c8dcSSimon Schubert
995796c8dcSSimon Schubert static void set_output_radix_1 (int, unsigned);
1005796c8dcSSimon Schubert
1015796c8dcSSimon Schubert void _initialize_valprint (void);
1025796c8dcSSimon Schubert
1035796c8dcSSimon Schubert #define PRINT_MAX_DEFAULT 200 /* Start print_max off at this value. */
1045796c8dcSSimon Schubert
1055796c8dcSSimon Schubert struct value_print_options user_print_options =
1065796c8dcSSimon Schubert {
1075796c8dcSSimon Schubert Val_pretty_default, /* pretty */
1085796c8dcSSimon Schubert 0, /* prettyprint_arrays */
1095796c8dcSSimon Schubert 0, /* prettyprint_structs */
1105796c8dcSSimon Schubert 0, /* vtblprint */
1115796c8dcSSimon Schubert 1, /* unionprint */
1125796c8dcSSimon Schubert 1, /* addressprint */
1135796c8dcSSimon Schubert 0, /* objectprint */
1145796c8dcSSimon Schubert PRINT_MAX_DEFAULT, /* print_max */
1155796c8dcSSimon Schubert 10, /* repeat_count_threshold */
1165796c8dcSSimon Schubert 0, /* output_format */
1175796c8dcSSimon Schubert 0, /* format */
1185796c8dcSSimon Schubert 0, /* stop_print_at_null */
1195796c8dcSSimon Schubert 0, /* print_array_indexes */
1205796c8dcSSimon Schubert 0, /* deref_ref */
1215796c8dcSSimon Schubert 1, /* static_field_print */
1225796c8dcSSimon Schubert 1, /* pascal_static_field_print */
1235796c8dcSSimon Schubert 0, /* raw */
124*ef5ccd6cSJohn Marino 0, /* summary */
125*ef5ccd6cSJohn Marino 1 /* symbol_print */
1265796c8dcSSimon Schubert };
1275796c8dcSSimon Schubert
1285796c8dcSSimon Schubert /* Initialize *OPTS to be a copy of the user print options. */
1295796c8dcSSimon Schubert void
get_user_print_options(struct value_print_options * opts)1305796c8dcSSimon Schubert get_user_print_options (struct value_print_options *opts)
1315796c8dcSSimon Schubert {
1325796c8dcSSimon Schubert *opts = user_print_options;
1335796c8dcSSimon Schubert }
1345796c8dcSSimon Schubert
1355796c8dcSSimon Schubert /* Initialize *OPTS to be a copy of the user print options, but with
1365796c8dcSSimon Schubert pretty-printing disabled. */
1375796c8dcSSimon Schubert void
get_raw_print_options(struct value_print_options * opts)1385796c8dcSSimon Schubert get_raw_print_options (struct value_print_options *opts)
1395796c8dcSSimon Schubert {
1405796c8dcSSimon Schubert *opts = user_print_options;
1415796c8dcSSimon Schubert opts->pretty = Val_no_prettyprint;
1425796c8dcSSimon Schubert }
1435796c8dcSSimon Schubert
1445796c8dcSSimon Schubert /* Initialize *OPTS to be a copy of the user print options, but using
1455796c8dcSSimon Schubert FORMAT as the formatting option. */
1465796c8dcSSimon Schubert void
get_formatted_print_options(struct value_print_options * opts,char format)1475796c8dcSSimon Schubert get_formatted_print_options (struct value_print_options *opts,
1485796c8dcSSimon Schubert char format)
1495796c8dcSSimon Schubert {
1505796c8dcSSimon Schubert *opts = user_print_options;
1515796c8dcSSimon Schubert opts->format = format;
1525796c8dcSSimon Schubert }
1535796c8dcSSimon Schubert
1545796c8dcSSimon Schubert static void
show_print_max(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)1555796c8dcSSimon Schubert show_print_max (struct ui_file *file, int from_tty,
1565796c8dcSSimon Schubert struct cmd_list_element *c, const char *value)
1575796c8dcSSimon Schubert {
158c50c785cSJohn Marino fprintf_filtered (file,
159c50c785cSJohn Marino _("Limit on string chars or array "
160c50c785cSJohn Marino "elements to print is %s.\n"),
1615796c8dcSSimon Schubert value);
1625796c8dcSSimon Schubert }
1635796c8dcSSimon Schubert
1645796c8dcSSimon Schubert
1655796c8dcSSimon Schubert /* Default input and output radixes, and output format letter. */
1665796c8dcSSimon Schubert
1675796c8dcSSimon Schubert unsigned input_radix = 10;
1685796c8dcSSimon Schubert static void
show_input_radix(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)1695796c8dcSSimon Schubert show_input_radix (struct ui_file *file, int from_tty,
1705796c8dcSSimon Schubert struct cmd_list_element *c, const char *value)
1715796c8dcSSimon Schubert {
172c50c785cSJohn Marino fprintf_filtered (file,
173c50c785cSJohn Marino _("Default input radix for entering numbers is %s.\n"),
1745796c8dcSSimon Schubert value);
1755796c8dcSSimon Schubert }
1765796c8dcSSimon Schubert
1775796c8dcSSimon Schubert unsigned output_radix = 10;
1785796c8dcSSimon Schubert static void
show_output_radix(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)1795796c8dcSSimon Schubert show_output_radix (struct ui_file *file, int from_tty,
1805796c8dcSSimon Schubert struct cmd_list_element *c, const char *value)
1815796c8dcSSimon Schubert {
182c50c785cSJohn Marino fprintf_filtered (file,
183c50c785cSJohn Marino _("Default output radix for printing of values is %s.\n"),
1845796c8dcSSimon Schubert value);
1855796c8dcSSimon Schubert }
1865796c8dcSSimon Schubert
1875796c8dcSSimon Schubert /* By default we print arrays without printing the index of each element in
1885796c8dcSSimon Schubert the array. This behavior can be changed by setting PRINT_ARRAY_INDEXES. */
1895796c8dcSSimon Schubert
1905796c8dcSSimon Schubert static void
show_print_array_indexes(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)1915796c8dcSSimon Schubert show_print_array_indexes (struct ui_file *file, int from_tty,
1925796c8dcSSimon Schubert struct cmd_list_element *c, const char *value)
1935796c8dcSSimon Schubert {
1945796c8dcSSimon Schubert fprintf_filtered (file, _("Printing of array indexes is %s.\n"), value);
1955796c8dcSSimon Schubert }
1965796c8dcSSimon Schubert
1975796c8dcSSimon Schubert /* Print repeat counts if there are more than this many repetitions of an
1985796c8dcSSimon Schubert element in an array. Referenced by the low level language dependent
1995796c8dcSSimon Schubert print routines. */
2005796c8dcSSimon Schubert
2015796c8dcSSimon Schubert static void
show_repeat_count_threshold(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)2025796c8dcSSimon Schubert show_repeat_count_threshold (struct ui_file *file, int from_tty,
2035796c8dcSSimon Schubert struct cmd_list_element *c, const char *value)
2045796c8dcSSimon Schubert {
2055796c8dcSSimon Schubert fprintf_filtered (file, _("Threshold for repeated print elements is %s.\n"),
2065796c8dcSSimon Schubert value);
2075796c8dcSSimon Schubert }
2085796c8dcSSimon Schubert
2095796c8dcSSimon Schubert /* If nonzero, stops printing of char arrays at first null. */
2105796c8dcSSimon Schubert
2115796c8dcSSimon Schubert static void
show_stop_print_at_null(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)2125796c8dcSSimon Schubert show_stop_print_at_null (struct ui_file *file, int from_tty,
2135796c8dcSSimon Schubert struct cmd_list_element *c, const char *value)
2145796c8dcSSimon Schubert {
215c50c785cSJohn Marino fprintf_filtered (file,
216c50c785cSJohn Marino _("Printing of char arrays to stop "
217c50c785cSJohn Marino "at first null char is %s.\n"),
2185796c8dcSSimon Schubert value);
2195796c8dcSSimon Schubert }
2205796c8dcSSimon Schubert
2215796c8dcSSimon Schubert /* Controls pretty printing of structures. */
2225796c8dcSSimon Schubert
2235796c8dcSSimon Schubert static void
show_prettyprint_structs(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)2245796c8dcSSimon Schubert show_prettyprint_structs (struct ui_file *file, int from_tty,
2255796c8dcSSimon Schubert struct cmd_list_element *c, const char *value)
2265796c8dcSSimon Schubert {
2275796c8dcSSimon Schubert fprintf_filtered (file, _("Prettyprinting of structures is %s.\n"), value);
2285796c8dcSSimon Schubert }
2295796c8dcSSimon Schubert
2305796c8dcSSimon Schubert /* Controls pretty printing of arrays. */
2315796c8dcSSimon Schubert
2325796c8dcSSimon Schubert static void
show_prettyprint_arrays(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)2335796c8dcSSimon Schubert show_prettyprint_arrays (struct ui_file *file, int from_tty,
2345796c8dcSSimon Schubert struct cmd_list_element *c, const char *value)
2355796c8dcSSimon Schubert {
2365796c8dcSSimon Schubert fprintf_filtered (file, _("Prettyprinting of arrays is %s.\n"), value);
2375796c8dcSSimon Schubert }
2385796c8dcSSimon Schubert
2395796c8dcSSimon Schubert /* If nonzero, causes unions inside structures or other unions to be
2405796c8dcSSimon Schubert printed. */
2415796c8dcSSimon Schubert
2425796c8dcSSimon Schubert static void
show_unionprint(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)2435796c8dcSSimon Schubert show_unionprint (struct ui_file *file, int from_tty,
2445796c8dcSSimon Schubert struct cmd_list_element *c, const char *value)
2455796c8dcSSimon Schubert {
246c50c785cSJohn Marino fprintf_filtered (file,
247c50c785cSJohn Marino _("Printing of unions interior to structures is %s.\n"),
2485796c8dcSSimon Schubert value);
2495796c8dcSSimon Schubert }
2505796c8dcSSimon Schubert
2515796c8dcSSimon Schubert /* If nonzero, causes machine addresses to be printed in certain contexts. */
2525796c8dcSSimon Schubert
2535796c8dcSSimon Schubert static void
show_addressprint(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)2545796c8dcSSimon Schubert show_addressprint (struct ui_file *file, int from_tty,
2555796c8dcSSimon Schubert struct cmd_list_element *c, const char *value)
2565796c8dcSSimon Schubert {
2575796c8dcSSimon Schubert fprintf_filtered (file, _("Printing of addresses is %s.\n"), value);
2585796c8dcSSimon Schubert }
259*ef5ccd6cSJohn Marino
260*ef5ccd6cSJohn Marino static void
show_symbol_print(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)261*ef5ccd6cSJohn Marino show_symbol_print (struct ui_file *file, int from_tty,
262*ef5ccd6cSJohn Marino struct cmd_list_element *c, const char *value)
263*ef5ccd6cSJohn Marino {
264*ef5ccd6cSJohn Marino fprintf_filtered (file,
265*ef5ccd6cSJohn Marino _("Printing of symbols when printing pointers is %s.\n"),
266*ef5ccd6cSJohn Marino value);
267*ef5ccd6cSJohn Marino }
268*ef5ccd6cSJohn Marino
2695796c8dcSSimon Schubert
2705796c8dcSSimon Schubert
2715796c8dcSSimon Schubert /* A helper function for val_print. When printing in "summary" mode,
2725796c8dcSSimon Schubert we want to print scalar arguments, but not aggregate arguments.
2735796c8dcSSimon Schubert This function distinguishes between the two. */
2745796c8dcSSimon Schubert
2755796c8dcSSimon Schubert static int
scalar_type_p(struct type * type)2765796c8dcSSimon Schubert scalar_type_p (struct type *type)
2775796c8dcSSimon Schubert {
2785796c8dcSSimon Schubert CHECK_TYPEDEF (type);
2795796c8dcSSimon Schubert while (TYPE_CODE (type) == TYPE_CODE_REF)
2805796c8dcSSimon Schubert {
2815796c8dcSSimon Schubert type = TYPE_TARGET_TYPE (type);
2825796c8dcSSimon Schubert CHECK_TYPEDEF (type);
2835796c8dcSSimon Schubert }
2845796c8dcSSimon Schubert switch (TYPE_CODE (type))
2855796c8dcSSimon Schubert {
2865796c8dcSSimon Schubert case TYPE_CODE_ARRAY:
2875796c8dcSSimon Schubert case TYPE_CODE_STRUCT:
2885796c8dcSSimon Schubert case TYPE_CODE_UNION:
2895796c8dcSSimon Schubert case TYPE_CODE_SET:
2905796c8dcSSimon Schubert case TYPE_CODE_STRING:
2915796c8dcSSimon Schubert return 0;
2925796c8dcSSimon Schubert default:
2935796c8dcSSimon Schubert return 1;
2945796c8dcSSimon Schubert }
2955796c8dcSSimon Schubert }
2965796c8dcSSimon Schubert
297*ef5ccd6cSJohn Marino /* See its definition in value.h. */
298cf7f2e2dSJohn Marino
299*ef5ccd6cSJohn Marino int
valprint_check_validity(struct ui_file * stream,struct type * type,int embedded_offset,const struct value * val)300cf7f2e2dSJohn Marino valprint_check_validity (struct ui_file *stream,
301cf7f2e2dSJohn Marino struct type *type,
302c50c785cSJohn Marino int embedded_offset,
303cf7f2e2dSJohn Marino const struct value *val)
304cf7f2e2dSJohn Marino {
305cf7f2e2dSJohn Marino CHECK_TYPEDEF (type);
306cf7f2e2dSJohn Marino
307cf7f2e2dSJohn Marino if (TYPE_CODE (type) != TYPE_CODE_UNION
308cf7f2e2dSJohn Marino && TYPE_CODE (type) != TYPE_CODE_STRUCT
309cf7f2e2dSJohn Marino && TYPE_CODE (type) != TYPE_CODE_ARRAY)
310cf7f2e2dSJohn Marino {
311c50c785cSJohn Marino if (!value_bits_valid (val, TARGET_CHAR_BIT * embedded_offset,
312cf7f2e2dSJohn Marino TARGET_CHAR_BIT * TYPE_LENGTH (type)))
313cf7f2e2dSJohn Marino {
314c50c785cSJohn Marino val_print_optimized_out (stream);
315c50c785cSJohn Marino return 0;
316c50c785cSJohn Marino }
317c50c785cSJohn Marino
318c50c785cSJohn Marino if (value_bits_synthetic_pointer (val, TARGET_CHAR_BIT * embedded_offset,
319c50c785cSJohn Marino TARGET_CHAR_BIT * TYPE_LENGTH (type)))
320c50c785cSJohn Marino {
321c50c785cSJohn Marino fputs_filtered (_("<synthetic pointer>"), stream);
322c50c785cSJohn Marino return 0;
323c50c785cSJohn Marino }
324c50c785cSJohn Marino
325c50c785cSJohn Marino if (!value_bytes_available (val, embedded_offset, TYPE_LENGTH (type)))
326c50c785cSJohn Marino {
327c50c785cSJohn Marino val_print_unavailable (stream);
328cf7f2e2dSJohn Marino return 0;
329cf7f2e2dSJohn Marino }
330cf7f2e2dSJohn Marino }
331cf7f2e2dSJohn Marino
332cf7f2e2dSJohn Marino return 1;
333cf7f2e2dSJohn Marino }
334cf7f2e2dSJohn Marino
335c50c785cSJohn Marino void
val_print_optimized_out(struct ui_file * stream)336c50c785cSJohn Marino val_print_optimized_out (struct ui_file *stream)
337c50c785cSJohn Marino {
338c50c785cSJohn Marino fprintf_filtered (stream, _("<optimized out>"));
339c50c785cSJohn Marino }
3405796c8dcSSimon Schubert
341c50c785cSJohn Marino void
val_print_unavailable(struct ui_file * stream)342c50c785cSJohn Marino val_print_unavailable (struct ui_file *stream)
343c50c785cSJohn Marino {
344c50c785cSJohn Marino fprintf_filtered (stream, _("<unavailable>"));
345c50c785cSJohn Marino }
3465796c8dcSSimon Schubert
347c50c785cSJohn Marino void
val_print_invalid_address(struct ui_file * stream)348c50c785cSJohn Marino val_print_invalid_address (struct ui_file *stream)
349c50c785cSJohn Marino {
350c50c785cSJohn Marino fprintf_filtered (stream, _("<invalid address>"));
351c50c785cSJohn Marino }
3525796c8dcSSimon Schubert
353*ef5ccd6cSJohn Marino /* A generic val_print that is suitable for use by language
354*ef5ccd6cSJohn Marino implementations of the la_val_print method. This function can
355*ef5ccd6cSJohn Marino handle most type codes, though not all, notably exception
356*ef5ccd6cSJohn Marino TYPE_CODE_UNION and TYPE_CODE_STRUCT, which must be implemented by
357*ef5ccd6cSJohn Marino the caller.
358*ef5ccd6cSJohn Marino
359*ef5ccd6cSJohn Marino Most arguments are as to val_print.
360*ef5ccd6cSJohn Marino
361*ef5ccd6cSJohn Marino The additional DECORATIONS argument can be used to customize the
362*ef5ccd6cSJohn Marino output in some small, language-specific ways. */
363*ef5ccd6cSJohn Marino
364*ef5ccd6cSJohn Marino void
generic_val_print(struct type * type,const gdb_byte * valaddr,int embedded_offset,CORE_ADDR address,struct ui_file * stream,int recurse,const struct value * original_value,const struct value_print_options * options,const struct generic_val_print_decorations * decorations)365*ef5ccd6cSJohn Marino generic_val_print (struct type *type, const gdb_byte *valaddr,
366*ef5ccd6cSJohn Marino int embedded_offset, CORE_ADDR address,
367*ef5ccd6cSJohn Marino struct ui_file *stream, int recurse,
368*ef5ccd6cSJohn Marino const struct value *original_value,
369*ef5ccd6cSJohn Marino const struct value_print_options *options,
370*ef5ccd6cSJohn Marino const struct generic_val_print_decorations *decorations)
371*ef5ccd6cSJohn Marino {
372*ef5ccd6cSJohn Marino struct gdbarch *gdbarch = get_type_arch (type);
373*ef5ccd6cSJohn Marino unsigned int i = 0; /* Number of characters printed. */
374*ef5ccd6cSJohn Marino unsigned len;
375*ef5ccd6cSJohn Marino struct type *elttype, *unresolved_elttype;
376*ef5ccd6cSJohn Marino struct type *unresolved_type = type;
377*ef5ccd6cSJohn Marino LONGEST val;
378*ef5ccd6cSJohn Marino CORE_ADDR addr;
379*ef5ccd6cSJohn Marino
380*ef5ccd6cSJohn Marino CHECK_TYPEDEF (type);
381*ef5ccd6cSJohn Marino switch (TYPE_CODE (type))
382*ef5ccd6cSJohn Marino {
383*ef5ccd6cSJohn Marino case TYPE_CODE_ARRAY:
384*ef5ccd6cSJohn Marino unresolved_elttype = TYPE_TARGET_TYPE (type);
385*ef5ccd6cSJohn Marino elttype = check_typedef (unresolved_elttype);
386*ef5ccd6cSJohn Marino if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (unresolved_elttype) > 0)
387*ef5ccd6cSJohn Marino {
388*ef5ccd6cSJohn Marino LONGEST low_bound, high_bound;
389*ef5ccd6cSJohn Marino
390*ef5ccd6cSJohn Marino if (!get_array_bounds (type, &low_bound, &high_bound))
391*ef5ccd6cSJohn Marino error (_("Could not determine the array high bound"));
392*ef5ccd6cSJohn Marino
393*ef5ccd6cSJohn Marino if (options->prettyprint_arrays)
394*ef5ccd6cSJohn Marino {
395*ef5ccd6cSJohn Marino print_spaces_filtered (2 + 2 * recurse, stream);
396*ef5ccd6cSJohn Marino }
397*ef5ccd6cSJohn Marino
398*ef5ccd6cSJohn Marino fprintf_filtered (stream, "{");
399*ef5ccd6cSJohn Marino val_print_array_elements (type, valaddr, embedded_offset,
400*ef5ccd6cSJohn Marino address, stream,
401*ef5ccd6cSJohn Marino recurse, original_value, options, 0);
402*ef5ccd6cSJohn Marino fprintf_filtered (stream, "}");
403*ef5ccd6cSJohn Marino break;
404*ef5ccd6cSJohn Marino }
405*ef5ccd6cSJohn Marino /* Array of unspecified length: treat like pointer to first
406*ef5ccd6cSJohn Marino elt. */
407*ef5ccd6cSJohn Marino addr = address + embedded_offset;
408*ef5ccd6cSJohn Marino goto print_unpacked_pointer;
409*ef5ccd6cSJohn Marino
410*ef5ccd6cSJohn Marino case TYPE_CODE_MEMBERPTR:
411*ef5ccd6cSJohn Marino val_print_scalar_formatted (type, valaddr, embedded_offset,
412*ef5ccd6cSJohn Marino original_value, options, 0, stream);
413*ef5ccd6cSJohn Marino break;
414*ef5ccd6cSJohn Marino
415*ef5ccd6cSJohn Marino case TYPE_CODE_PTR:
416*ef5ccd6cSJohn Marino if (options->format && options->format != 's')
417*ef5ccd6cSJohn Marino {
418*ef5ccd6cSJohn Marino val_print_scalar_formatted (type, valaddr, embedded_offset,
419*ef5ccd6cSJohn Marino original_value, options, 0, stream);
420*ef5ccd6cSJohn Marino break;
421*ef5ccd6cSJohn Marino }
422*ef5ccd6cSJohn Marino unresolved_elttype = TYPE_TARGET_TYPE (type);
423*ef5ccd6cSJohn Marino elttype = check_typedef (unresolved_elttype);
424*ef5ccd6cSJohn Marino {
425*ef5ccd6cSJohn Marino addr = unpack_pointer (type, valaddr + embedded_offset);
426*ef5ccd6cSJohn Marino print_unpacked_pointer:
427*ef5ccd6cSJohn Marino
428*ef5ccd6cSJohn Marino if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
429*ef5ccd6cSJohn Marino {
430*ef5ccd6cSJohn Marino /* Try to print what function it points to. */
431*ef5ccd6cSJohn Marino print_function_pointer_address (options, gdbarch, addr, stream);
432*ef5ccd6cSJohn Marino return;
433*ef5ccd6cSJohn Marino }
434*ef5ccd6cSJohn Marino
435*ef5ccd6cSJohn Marino if (options->symbol_print)
436*ef5ccd6cSJohn Marino print_address_demangle (options, gdbarch, addr, stream, demangle);
437*ef5ccd6cSJohn Marino else if (options->addressprint)
438*ef5ccd6cSJohn Marino fputs_filtered (paddress (gdbarch, addr), stream);
439*ef5ccd6cSJohn Marino }
440*ef5ccd6cSJohn Marino break;
441*ef5ccd6cSJohn Marino
442*ef5ccd6cSJohn Marino case TYPE_CODE_REF:
443*ef5ccd6cSJohn Marino elttype = check_typedef (TYPE_TARGET_TYPE (type));
444*ef5ccd6cSJohn Marino if (options->addressprint)
445*ef5ccd6cSJohn Marino {
446*ef5ccd6cSJohn Marino CORE_ADDR addr
447*ef5ccd6cSJohn Marino = extract_typed_address (valaddr + embedded_offset, type);
448*ef5ccd6cSJohn Marino
449*ef5ccd6cSJohn Marino fprintf_filtered (stream, "@");
450*ef5ccd6cSJohn Marino fputs_filtered (paddress (gdbarch, addr), stream);
451*ef5ccd6cSJohn Marino if (options->deref_ref)
452*ef5ccd6cSJohn Marino fputs_filtered (": ", stream);
453*ef5ccd6cSJohn Marino }
454*ef5ccd6cSJohn Marino /* De-reference the reference. */
455*ef5ccd6cSJohn Marino if (options->deref_ref)
456*ef5ccd6cSJohn Marino {
457*ef5ccd6cSJohn Marino if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
458*ef5ccd6cSJohn Marino {
459*ef5ccd6cSJohn Marino struct value *deref_val;
460*ef5ccd6cSJohn Marino
461*ef5ccd6cSJohn Marino deref_val = coerce_ref_if_computed (original_value);
462*ef5ccd6cSJohn Marino if (deref_val != NULL)
463*ef5ccd6cSJohn Marino {
464*ef5ccd6cSJohn Marino /* More complicated computed references are not supported. */
465*ef5ccd6cSJohn Marino gdb_assert (embedded_offset == 0);
466*ef5ccd6cSJohn Marino }
467*ef5ccd6cSJohn Marino else
468*ef5ccd6cSJohn Marino deref_val = value_at (TYPE_TARGET_TYPE (type),
469*ef5ccd6cSJohn Marino unpack_pointer (type,
470*ef5ccd6cSJohn Marino (valaddr
471*ef5ccd6cSJohn Marino + embedded_offset)));
472*ef5ccd6cSJohn Marino
473*ef5ccd6cSJohn Marino common_val_print (deref_val, stream, recurse, options,
474*ef5ccd6cSJohn Marino current_language);
475*ef5ccd6cSJohn Marino }
476*ef5ccd6cSJohn Marino else
477*ef5ccd6cSJohn Marino fputs_filtered ("???", stream);
478*ef5ccd6cSJohn Marino }
479*ef5ccd6cSJohn Marino break;
480*ef5ccd6cSJohn Marino
481*ef5ccd6cSJohn Marino case TYPE_CODE_ENUM:
482*ef5ccd6cSJohn Marino if (options->format)
483*ef5ccd6cSJohn Marino {
484*ef5ccd6cSJohn Marino val_print_scalar_formatted (type, valaddr, embedded_offset,
485*ef5ccd6cSJohn Marino original_value, options, 0, stream);
486*ef5ccd6cSJohn Marino break;
487*ef5ccd6cSJohn Marino }
488*ef5ccd6cSJohn Marino len = TYPE_NFIELDS (type);
489*ef5ccd6cSJohn Marino val = unpack_long (type, valaddr + embedded_offset);
490*ef5ccd6cSJohn Marino for (i = 0; i < len; i++)
491*ef5ccd6cSJohn Marino {
492*ef5ccd6cSJohn Marino QUIT;
493*ef5ccd6cSJohn Marino if (val == TYPE_FIELD_ENUMVAL (type, i))
494*ef5ccd6cSJohn Marino {
495*ef5ccd6cSJohn Marino break;
496*ef5ccd6cSJohn Marino }
497*ef5ccd6cSJohn Marino }
498*ef5ccd6cSJohn Marino if (i < len)
499*ef5ccd6cSJohn Marino {
500*ef5ccd6cSJohn Marino fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
501*ef5ccd6cSJohn Marino }
502*ef5ccd6cSJohn Marino else if (TYPE_FLAG_ENUM (type))
503*ef5ccd6cSJohn Marino {
504*ef5ccd6cSJohn Marino int first = 1;
505*ef5ccd6cSJohn Marino
506*ef5ccd6cSJohn Marino /* We have a "flag" enum, so we try to decompose it into
507*ef5ccd6cSJohn Marino pieces as appropriate. A flag enum has disjoint
508*ef5ccd6cSJohn Marino constants by definition. */
509*ef5ccd6cSJohn Marino fputs_filtered ("(", stream);
510*ef5ccd6cSJohn Marino for (i = 0; i < len; ++i)
511*ef5ccd6cSJohn Marino {
512*ef5ccd6cSJohn Marino QUIT;
513*ef5ccd6cSJohn Marino
514*ef5ccd6cSJohn Marino if ((val & TYPE_FIELD_ENUMVAL (type, i)) != 0)
515*ef5ccd6cSJohn Marino {
516*ef5ccd6cSJohn Marino if (!first)
517*ef5ccd6cSJohn Marino fputs_filtered (" | ", stream);
518*ef5ccd6cSJohn Marino first = 0;
519*ef5ccd6cSJohn Marino
520*ef5ccd6cSJohn Marino val &= ~TYPE_FIELD_ENUMVAL (type, i);
521*ef5ccd6cSJohn Marino fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
522*ef5ccd6cSJohn Marino }
523*ef5ccd6cSJohn Marino }
524*ef5ccd6cSJohn Marino
525*ef5ccd6cSJohn Marino if (first || val != 0)
526*ef5ccd6cSJohn Marino {
527*ef5ccd6cSJohn Marino if (!first)
528*ef5ccd6cSJohn Marino fputs_filtered (" | ", stream);
529*ef5ccd6cSJohn Marino fputs_filtered ("unknown: ", stream);
530*ef5ccd6cSJohn Marino print_longest (stream, 'd', 0, val);
531*ef5ccd6cSJohn Marino }
532*ef5ccd6cSJohn Marino
533*ef5ccd6cSJohn Marino fputs_filtered (")", stream);
534*ef5ccd6cSJohn Marino }
535*ef5ccd6cSJohn Marino else
536*ef5ccd6cSJohn Marino print_longest (stream, 'd', 0, val);
537*ef5ccd6cSJohn Marino break;
538*ef5ccd6cSJohn Marino
539*ef5ccd6cSJohn Marino case TYPE_CODE_FLAGS:
540*ef5ccd6cSJohn Marino if (options->format)
541*ef5ccd6cSJohn Marino val_print_scalar_formatted (type, valaddr, embedded_offset,
542*ef5ccd6cSJohn Marino original_value, options, 0, stream);
543*ef5ccd6cSJohn Marino else
544*ef5ccd6cSJohn Marino val_print_type_code_flags (type, valaddr + embedded_offset,
545*ef5ccd6cSJohn Marino stream);
546*ef5ccd6cSJohn Marino break;
547*ef5ccd6cSJohn Marino
548*ef5ccd6cSJohn Marino case TYPE_CODE_FUNC:
549*ef5ccd6cSJohn Marino case TYPE_CODE_METHOD:
550*ef5ccd6cSJohn Marino if (options->format)
551*ef5ccd6cSJohn Marino {
552*ef5ccd6cSJohn Marino val_print_scalar_formatted (type, valaddr, embedded_offset,
553*ef5ccd6cSJohn Marino original_value, options, 0, stream);
554*ef5ccd6cSJohn Marino break;
555*ef5ccd6cSJohn Marino }
556*ef5ccd6cSJohn Marino /* FIXME, we should consider, at least for ANSI C language,
557*ef5ccd6cSJohn Marino eliminating the distinction made between FUNCs and POINTERs
558*ef5ccd6cSJohn Marino to FUNCs. */
559*ef5ccd6cSJohn Marino fprintf_filtered (stream, "{");
560*ef5ccd6cSJohn Marino type_print (type, "", stream, -1);
561*ef5ccd6cSJohn Marino fprintf_filtered (stream, "} ");
562*ef5ccd6cSJohn Marino /* Try to print what function it points to, and its address. */
563*ef5ccd6cSJohn Marino print_address_demangle (options, gdbarch, address, stream, demangle);
564*ef5ccd6cSJohn Marino break;
565*ef5ccd6cSJohn Marino
566*ef5ccd6cSJohn Marino case TYPE_CODE_BOOL:
567*ef5ccd6cSJohn Marino if (options->format || options->output_format)
568*ef5ccd6cSJohn Marino {
569*ef5ccd6cSJohn Marino struct value_print_options opts = *options;
570*ef5ccd6cSJohn Marino opts.format = (options->format ? options->format
571*ef5ccd6cSJohn Marino : options->output_format);
572*ef5ccd6cSJohn Marino val_print_scalar_formatted (type, valaddr, embedded_offset,
573*ef5ccd6cSJohn Marino original_value, &opts, 0, stream);
574*ef5ccd6cSJohn Marino }
575*ef5ccd6cSJohn Marino else
576*ef5ccd6cSJohn Marino {
577*ef5ccd6cSJohn Marino val = unpack_long (type, valaddr + embedded_offset);
578*ef5ccd6cSJohn Marino if (val == 0)
579*ef5ccd6cSJohn Marino fputs_filtered (decorations->false_name, stream);
580*ef5ccd6cSJohn Marino else if (val == 1)
581*ef5ccd6cSJohn Marino fputs_filtered (decorations->true_name, stream);
582*ef5ccd6cSJohn Marino else
583*ef5ccd6cSJohn Marino print_longest (stream, 'd', 0, val);
584*ef5ccd6cSJohn Marino }
585*ef5ccd6cSJohn Marino break;
586*ef5ccd6cSJohn Marino
587*ef5ccd6cSJohn Marino case TYPE_CODE_RANGE:
588*ef5ccd6cSJohn Marino /* FIXME: create_range_type does not set the unsigned bit in a
589*ef5ccd6cSJohn Marino range type (I think it probably should copy it from the
590*ef5ccd6cSJohn Marino target type), so we won't print values which are too large to
591*ef5ccd6cSJohn Marino fit in a signed integer correctly. */
592*ef5ccd6cSJohn Marino /* FIXME: Doesn't handle ranges of enums correctly. (Can't just
593*ef5ccd6cSJohn Marino print with the target type, though, because the size of our
594*ef5ccd6cSJohn Marino type and the target type might differ). */
595*ef5ccd6cSJohn Marino
596*ef5ccd6cSJohn Marino /* FALLTHROUGH */
597*ef5ccd6cSJohn Marino
598*ef5ccd6cSJohn Marino case TYPE_CODE_INT:
599*ef5ccd6cSJohn Marino if (options->format || options->output_format)
600*ef5ccd6cSJohn Marino {
601*ef5ccd6cSJohn Marino struct value_print_options opts = *options;
602*ef5ccd6cSJohn Marino
603*ef5ccd6cSJohn Marino opts.format = (options->format ? options->format
604*ef5ccd6cSJohn Marino : options->output_format);
605*ef5ccd6cSJohn Marino val_print_scalar_formatted (type, valaddr, embedded_offset,
606*ef5ccd6cSJohn Marino original_value, &opts, 0, stream);
607*ef5ccd6cSJohn Marino }
608*ef5ccd6cSJohn Marino else
609*ef5ccd6cSJohn Marino val_print_type_code_int (type, valaddr + embedded_offset, stream);
610*ef5ccd6cSJohn Marino break;
611*ef5ccd6cSJohn Marino
612*ef5ccd6cSJohn Marino case TYPE_CODE_CHAR:
613*ef5ccd6cSJohn Marino if (options->format || options->output_format)
614*ef5ccd6cSJohn Marino {
615*ef5ccd6cSJohn Marino struct value_print_options opts = *options;
616*ef5ccd6cSJohn Marino
617*ef5ccd6cSJohn Marino opts.format = (options->format ? options->format
618*ef5ccd6cSJohn Marino : options->output_format);
619*ef5ccd6cSJohn Marino val_print_scalar_formatted (type, valaddr, embedded_offset,
620*ef5ccd6cSJohn Marino original_value, &opts, 0, stream);
621*ef5ccd6cSJohn Marino }
622*ef5ccd6cSJohn Marino else
623*ef5ccd6cSJohn Marino {
624*ef5ccd6cSJohn Marino val = unpack_long (type, valaddr + embedded_offset);
625*ef5ccd6cSJohn Marino if (TYPE_UNSIGNED (type))
626*ef5ccd6cSJohn Marino fprintf_filtered (stream, "%u", (unsigned int) val);
627*ef5ccd6cSJohn Marino else
628*ef5ccd6cSJohn Marino fprintf_filtered (stream, "%d", (int) val);
629*ef5ccd6cSJohn Marino fputs_filtered (" ", stream);
630*ef5ccd6cSJohn Marino LA_PRINT_CHAR (val, unresolved_type, stream);
631*ef5ccd6cSJohn Marino }
632*ef5ccd6cSJohn Marino break;
633*ef5ccd6cSJohn Marino
634*ef5ccd6cSJohn Marino case TYPE_CODE_FLT:
635*ef5ccd6cSJohn Marino if (options->format)
636*ef5ccd6cSJohn Marino {
637*ef5ccd6cSJohn Marino val_print_scalar_formatted (type, valaddr, embedded_offset,
638*ef5ccd6cSJohn Marino original_value, options, 0, stream);
639*ef5ccd6cSJohn Marino }
640*ef5ccd6cSJohn Marino else
641*ef5ccd6cSJohn Marino {
642*ef5ccd6cSJohn Marino print_floating (valaddr + embedded_offset, type, stream);
643*ef5ccd6cSJohn Marino }
644*ef5ccd6cSJohn Marino break;
645*ef5ccd6cSJohn Marino
646*ef5ccd6cSJohn Marino case TYPE_CODE_DECFLOAT:
647*ef5ccd6cSJohn Marino if (options->format)
648*ef5ccd6cSJohn Marino val_print_scalar_formatted (type, valaddr, embedded_offset,
649*ef5ccd6cSJohn Marino original_value, options, 0, stream);
650*ef5ccd6cSJohn Marino else
651*ef5ccd6cSJohn Marino print_decimal_floating (valaddr + embedded_offset,
652*ef5ccd6cSJohn Marino type, stream);
653*ef5ccd6cSJohn Marino break;
654*ef5ccd6cSJohn Marino
655*ef5ccd6cSJohn Marino case TYPE_CODE_VOID:
656*ef5ccd6cSJohn Marino fputs_filtered (decorations->void_name, stream);
657*ef5ccd6cSJohn Marino break;
658*ef5ccd6cSJohn Marino
659*ef5ccd6cSJohn Marino case TYPE_CODE_ERROR:
660*ef5ccd6cSJohn Marino fprintf_filtered (stream, "%s", TYPE_ERROR_NAME (type));
661*ef5ccd6cSJohn Marino break;
662*ef5ccd6cSJohn Marino
663*ef5ccd6cSJohn Marino case TYPE_CODE_UNDEF:
664*ef5ccd6cSJohn Marino /* This happens (without TYPE_FLAG_STUB set) on systems which
665*ef5ccd6cSJohn Marino don't use dbx xrefs (NO_DBX_XREFS in gcc) if a file has a
666*ef5ccd6cSJohn Marino "struct foo *bar" and no complete type for struct foo in that
667*ef5ccd6cSJohn Marino file. */
668*ef5ccd6cSJohn Marino fprintf_filtered (stream, _("<incomplete type>"));
669*ef5ccd6cSJohn Marino break;
670*ef5ccd6cSJohn Marino
671*ef5ccd6cSJohn Marino case TYPE_CODE_COMPLEX:
672*ef5ccd6cSJohn Marino fprintf_filtered (stream, "%s", decorations->complex_prefix);
673*ef5ccd6cSJohn Marino if (options->format)
674*ef5ccd6cSJohn Marino val_print_scalar_formatted (TYPE_TARGET_TYPE (type),
675*ef5ccd6cSJohn Marino valaddr, embedded_offset,
676*ef5ccd6cSJohn Marino original_value, options, 0, stream);
677*ef5ccd6cSJohn Marino else
678*ef5ccd6cSJohn Marino print_floating (valaddr + embedded_offset,
679*ef5ccd6cSJohn Marino TYPE_TARGET_TYPE (type),
680*ef5ccd6cSJohn Marino stream);
681*ef5ccd6cSJohn Marino fprintf_filtered (stream, "%s", decorations->complex_infix);
682*ef5ccd6cSJohn Marino if (options->format)
683*ef5ccd6cSJohn Marino val_print_scalar_formatted (TYPE_TARGET_TYPE (type),
684*ef5ccd6cSJohn Marino valaddr,
685*ef5ccd6cSJohn Marino embedded_offset
686*ef5ccd6cSJohn Marino + TYPE_LENGTH (TYPE_TARGET_TYPE (type)),
687*ef5ccd6cSJohn Marino original_value,
688*ef5ccd6cSJohn Marino options, 0, stream);
689*ef5ccd6cSJohn Marino else
690*ef5ccd6cSJohn Marino print_floating (valaddr + embedded_offset
691*ef5ccd6cSJohn Marino + TYPE_LENGTH (TYPE_TARGET_TYPE (type)),
692*ef5ccd6cSJohn Marino TYPE_TARGET_TYPE (type),
693*ef5ccd6cSJohn Marino stream);
694*ef5ccd6cSJohn Marino fprintf_filtered (stream, "%s", decorations->complex_suffix);
695*ef5ccd6cSJohn Marino break;
696*ef5ccd6cSJohn Marino
697*ef5ccd6cSJohn Marino case TYPE_CODE_UNION:
698*ef5ccd6cSJohn Marino case TYPE_CODE_STRUCT:
699*ef5ccd6cSJohn Marino case TYPE_CODE_METHODPTR:
700*ef5ccd6cSJohn Marino default:
701*ef5ccd6cSJohn Marino error (_("Unhandled type code %d in symbol table."),
702*ef5ccd6cSJohn Marino TYPE_CODE (type));
703*ef5ccd6cSJohn Marino }
704*ef5ccd6cSJohn Marino gdb_flush (stream);
705*ef5ccd6cSJohn Marino }
706*ef5ccd6cSJohn Marino
707c50c785cSJohn Marino /* Print using the given LANGUAGE the data of type TYPE located at
708c50c785cSJohn Marino VALADDR + EMBEDDED_OFFSET (within GDB), which came from the
709c50c785cSJohn Marino inferior at address ADDRESS + EMBEDDED_OFFSET, onto stdio stream
710c50c785cSJohn Marino STREAM according to OPTIONS. VAL is the whole object that came
711c50c785cSJohn Marino from ADDRESS. VALADDR must point to the head of VAL's contents
712c50c785cSJohn Marino buffer.
713c50c785cSJohn Marino
714c50c785cSJohn Marino The language printers will pass down an adjusted EMBEDDED_OFFSET to
715c50c785cSJohn Marino further helper subroutines as subfields of TYPE are printed. In
716c50c785cSJohn Marino such cases, VALADDR is passed down unadjusted, as well as VAL, so
717c50c785cSJohn Marino that VAL can be queried for metadata about the contents data being
718c50c785cSJohn Marino printed, using EMBEDDED_OFFSET as an offset into VAL's contents
719c50c785cSJohn Marino buffer. For example: "has this field been optimized out", or "I'm
720c50c785cSJohn Marino printing an object while inspecting a traceframe; has this
721c50c785cSJohn Marino particular piece of data been collected?".
722c50c785cSJohn Marino
723c50c785cSJohn Marino RECURSE indicates the amount of indentation to supply before
724c50c785cSJohn Marino continuation lines; this amount is roughly twice the value of
725*ef5ccd6cSJohn Marino RECURSE. */
726c50c785cSJohn Marino
727*ef5ccd6cSJohn Marino void
val_print(struct type * type,const gdb_byte * valaddr,int embedded_offset,CORE_ADDR address,struct ui_file * stream,int recurse,const struct value * val,const struct value_print_options * options,const struct language_defn * language)7285796c8dcSSimon Schubert val_print (struct type *type, const gdb_byte *valaddr, int embedded_offset,
7295796c8dcSSimon Schubert CORE_ADDR address, struct ui_file *stream, int recurse,
730cf7f2e2dSJohn Marino const struct value *val,
7315796c8dcSSimon Schubert const struct value_print_options *options,
7325796c8dcSSimon Schubert const struct language_defn *language)
7335796c8dcSSimon Schubert {
7345796c8dcSSimon Schubert volatile struct gdb_exception except;
7355796c8dcSSimon Schubert int ret = 0;
7365796c8dcSSimon Schubert struct value_print_options local_opts = *options;
7375796c8dcSSimon Schubert struct type *real_type = check_typedef (type);
7385796c8dcSSimon Schubert
7395796c8dcSSimon Schubert if (local_opts.pretty == Val_pretty_default)
7405796c8dcSSimon Schubert local_opts.pretty = (local_opts.prettyprint_structs
7415796c8dcSSimon Schubert ? Val_prettyprint : Val_no_prettyprint);
7425796c8dcSSimon Schubert
7435796c8dcSSimon Schubert QUIT;
7445796c8dcSSimon Schubert
7455796c8dcSSimon Schubert /* Ensure that the type is complete and not just a stub. If the type is
7465796c8dcSSimon Schubert only a stub and we can't find and substitute its complete type, then
7475796c8dcSSimon Schubert print appropriate string and return. */
7485796c8dcSSimon Schubert
7495796c8dcSSimon Schubert if (TYPE_STUB (real_type))
7505796c8dcSSimon Schubert {
751cf7f2e2dSJohn Marino fprintf_filtered (stream, _("<incomplete type>"));
7525796c8dcSSimon Schubert gdb_flush (stream);
753*ef5ccd6cSJohn Marino return;
7545796c8dcSSimon Schubert }
7555796c8dcSSimon Schubert
756cf7f2e2dSJohn Marino if (!valprint_check_validity (stream, real_type, embedded_offset, val))
757*ef5ccd6cSJohn Marino return;
758cf7f2e2dSJohn Marino
7595796c8dcSSimon Schubert if (!options->raw)
7605796c8dcSSimon Schubert {
7615796c8dcSSimon Schubert ret = apply_val_pretty_printer (type, valaddr, embedded_offset,
762cf7f2e2dSJohn Marino address, stream, recurse,
763cf7f2e2dSJohn Marino val, options, language);
7645796c8dcSSimon Schubert if (ret)
765*ef5ccd6cSJohn Marino return;
7665796c8dcSSimon Schubert }
7675796c8dcSSimon Schubert
7685796c8dcSSimon Schubert /* Handle summary mode. If the value is a scalar, print it;
7695796c8dcSSimon Schubert otherwise, print an ellipsis. */
7705796c8dcSSimon Schubert if (options->summary && !scalar_type_p (type))
7715796c8dcSSimon Schubert {
7725796c8dcSSimon Schubert fprintf_filtered (stream, "...");
773*ef5ccd6cSJohn Marino return;
7745796c8dcSSimon Schubert }
7755796c8dcSSimon Schubert
7765796c8dcSSimon Schubert TRY_CATCH (except, RETURN_MASK_ERROR)
7775796c8dcSSimon Schubert {
778*ef5ccd6cSJohn Marino language->la_val_print (type, valaddr, embedded_offset, address,
779cf7f2e2dSJohn Marino stream, recurse, val,
780cf7f2e2dSJohn Marino &local_opts);
7815796c8dcSSimon Schubert }
7825796c8dcSSimon Schubert if (except.reason < 0)
7835796c8dcSSimon Schubert fprintf_filtered (stream, _("<error reading variable>"));
7845796c8dcSSimon Schubert }
7855796c8dcSSimon Schubert
7865796c8dcSSimon Schubert /* Check whether the value VAL is printable. Return 1 if it is;
787a45ae5f8SJohn Marino return 0 and print an appropriate error message to STREAM according to
788a45ae5f8SJohn Marino OPTIONS if it is not. */
7895796c8dcSSimon Schubert
7905796c8dcSSimon Schubert static int
value_check_printable(struct value * val,struct ui_file * stream,const struct value_print_options * options)791a45ae5f8SJohn Marino value_check_printable (struct value *val, struct ui_file *stream,
792a45ae5f8SJohn Marino const struct value_print_options *options)
7935796c8dcSSimon Schubert {
7945796c8dcSSimon Schubert if (val == 0)
7955796c8dcSSimon Schubert {
7965796c8dcSSimon Schubert fprintf_filtered (stream, _("<address of value unknown>"));
7975796c8dcSSimon Schubert return 0;
7985796c8dcSSimon Schubert }
7995796c8dcSSimon Schubert
800cf7f2e2dSJohn Marino if (value_entirely_optimized_out (val))
8015796c8dcSSimon Schubert {
802a45ae5f8SJohn Marino if (options->summary && !scalar_type_p (value_type (val)))
803a45ae5f8SJohn Marino fprintf_filtered (stream, "...");
804a45ae5f8SJohn Marino else
805c50c785cSJohn Marino val_print_optimized_out (stream);
8065796c8dcSSimon Schubert return 0;
8075796c8dcSSimon Schubert }
8085796c8dcSSimon Schubert
8095796c8dcSSimon Schubert if (TYPE_CODE (value_type (val)) == TYPE_CODE_INTERNAL_FUNCTION)
8105796c8dcSSimon Schubert {
8115796c8dcSSimon Schubert fprintf_filtered (stream, _("<internal function %s>"),
8125796c8dcSSimon Schubert value_internal_function_name (val));
8135796c8dcSSimon Schubert return 0;
8145796c8dcSSimon Schubert }
8155796c8dcSSimon Schubert
8165796c8dcSSimon Schubert return 1;
8175796c8dcSSimon Schubert }
8185796c8dcSSimon Schubert
8195796c8dcSSimon Schubert /* Print using the given LANGUAGE the value VAL onto stream STREAM according
8205796c8dcSSimon Schubert to OPTIONS.
8215796c8dcSSimon Schubert
8225796c8dcSSimon Schubert This is a preferable interface to val_print, above, because it uses
8235796c8dcSSimon Schubert GDB's value mechanism. */
8245796c8dcSSimon Schubert
825*ef5ccd6cSJohn Marino void
common_val_print(struct value * val,struct ui_file * stream,int recurse,const struct value_print_options * options,const struct language_defn * language)8265796c8dcSSimon Schubert common_val_print (struct value *val, struct ui_file *stream, int recurse,
8275796c8dcSSimon Schubert const struct value_print_options *options,
8285796c8dcSSimon Schubert const struct language_defn *language)
8295796c8dcSSimon Schubert {
830a45ae5f8SJohn Marino if (!value_check_printable (val, stream, options))
831*ef5ccd6cSJohn Marino return;
8325796c8dcSSimon Schubert
833cf7f2e2dSJohn Marino if (language->la_language == language_ada)
834cf7f2e2dSJohn Marino /* The value might have a dynamic type, which would cause trouble
835cf7f2e2dSJohn Marino below when trying to extract the value contents (since the value
836cf7f2e2dSJohn Marino size is determined from the type size which is unknown). So
837cf7f2e2dSJohn Marino get a fixed representation of our value. */
838cf7f2e2dSJohn Marino val = ada_to_fixed_value (val);
839cf7f2e2dSJohn Marino
840*ef5ccd6cSJohn Marino val_print (value_type (val), value_contents_for_printing (val),
8415796c8dcSSimon Schubert value_embedded_offset (val), value_address (val),
842cf7f2e2dSJohn Marino stream, recurse,
843cf7f2e2dSJohn Marino val, options, language);
8445796c8dcSSimon Schubert }
8455796c8dcSSimon Schubert
846cf7f2e2dSJohn Marino /* Print on stream STREAM the value VAL according to OPTIONS. The value
847*ef5ccd6cSJohn Marino is printed using the current_language syntax. */
848cf7f2e2dSJohn Marino
849*ef5ccd6cSJohn Marino void
value_print(struct value * val,struct ui_file * stream,const struct value_print_options * options)8505796c8dcSSimon Schubert value_print (struct value *val, struct ui_file *stream,
8515796c8dcSSimon Schubert const struct value_print_options *options)
8525796c8dcSSimon Schubert {
853a45ae5f8SJohn Marino if (!value_check_printable (val, stream, options))
854*ef5ccd6cSJohn Marino return;
8555796c8dcSSimon Schubert
8565796c8dcSSimon Schubert if (!options->raw)
8575796c8dcSSimon Schubert {
8585796c8dcSSimon Schubert int r = apply_val_pretty_printer (value_type (val),
859cf7f2e2dSJohn Marino value_contents_for_printing (val),
8605796c8dcSSimon Schubert value_embedded_offset (val),
8615796c8dcSSimon Schubert value_address (val),
862cf7f2e2dSJohn Marino stream, 0,
863cf7f2e2dSJohn Marino val, options, current_language);
864cf7f2e2dSJohn Marino
8655796c8dcSSimon Schubert if (r)
866*ef5ccd6cSJohn Marino return;
8675796c8dcSSimon Schubert }
8685796c8dcSSimon Schubert
869*ef5ccd6cSJohn Marino LA_VALUE_PRINT (val, stream, options);
8705796c8dcSSimon Schubert }
8715796c8dcSSimon Schubert
8725796c8dcSSimon Schubert /* Called by various <lang>_val_print routines to print
8735796c8dcSSimon Schubert TYPE_CODE_INT's. TYPE is the type. VALADDR is the address of the
8745796c8dcSSimon Schubert value. STREAM is where to print the value. */
8755796c8dcSSimon Schubert
8765796c8dcSSimon Schubert void
val_print_type_code_int(struct type * type,const gdb_byte * valaddr,struct ui_file * stream)8775796c8dcSSimon Schubert val_print_type_code_int (struct type *type, const gdb_byte *valaddr,
8785796c8dcSSimon Schubert struct ui_file *stream)
8795796c8dcSSimon Schubert {
8805796c8dcSSimon Schubert enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
8815796c8dcSSimon Schubert
8825796c8dcSSimon Schubert if (TYPE_LENGTH (type) > sizeof (LONGEST))
8835796c8dcSSimon Schubert {
8845796c8dcSSimon Schubert LONGEST val;
8855796c8dcSSimon Schubert
8865796c8dcSSimon Schubert if (TYPE_UNSIGNED (type)
8875796c8dcSSimon Schubert && extract_long_unsigned_integer (valaddr, TYPE_LENGTH (type),
8885796c8dcSSimon Schubert byte_order, &val))
8895796c8dcSSimon Schubert {
8905796c8dcSSimon Schubert print_longest (stream, 'u', 0, val);
8915796c8dcSSimon Schubert }
8925796c8dcSSimon Schubert else
8935796c8dcSSimon Schubert {
8945796c8dcSSimon Schubert /* Signed, or we couldn't turn an unsigned value into a
8955796c8dcSSimon Schubert LONGEST. For signed values, one could assume two's
8965796c8dcSSimon Schubert complement (a reasonable assumption, I think) and do
8975796c8dcSSimon Schubert better than this. */
8985796c8dcSSimon Schubert print_hex_chars (stream, (unsigned char *) valaddr,
8995796c8dcSSimon Schubert TYPE_LENGTH (type), byte_order);
9005796c8dcSSimon Schubert }
9015796c8dcSSimon Schubert }
9025796c8dcSSimon Schubert else
9035796c8dcSSimon Schubert {
9045796c8dcSSimon Schubert print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0,
9055796c8dcSSimon Schubert unpack_long (type, valaddr));
9065796c8dcSSimon Schubert }
9075796c8dcSSimon Schubert }
9085796c8dcSSimon Schubert
9095796c8dcSSimon Schubert void
val_print_type_code_flags(struct type * type,const gdb_byte * valaddr,struct ui_file * stream)9105796c8dcSSimon Schubert val_print_type_code_flags (struct type *type, const gdb_byte *valaddr,
9115796c8dcSSimon Schubert struct ui_file *stream)
9125796c8dcSSimon Schubert {
9135796c8dcSSimon Schubert ULONGEST val = unpack_long (type, valaddr);
9145796c8dcSSimon Schubert int bitpos, nfields = TYPE_NFIELDS (type);
9155796c8dcSSimon Schubert
9165796c8dcSSimon Schubert fputs_filtered ("[ ", stream);
9175796c8dcSSimon Schubert for (bitpos = 0; bitpos < nfields; bitpos++)
9185796c8dcSSimon Schubert {
9195796c8dcSSimon Schubert if (TYPE_FIELD_BITPOS (type, bitpos) != -1
9205796c8dcSSimon Schubert && (val & ((ULONGEST)1 << bitpos)))
9215796c8dcSSimon Schubert {
9225796c8dcSSimon Schubert if (TYPE_FIELD_NAME (type, bitpos))
9235796c8dcSSimon Schubert fprintf_filtered (stream, "%s ", TYPE_FIELD_NAME (type, bitpos));
9245796c8dcSSimon Schubert else
9255796c8dcSSimon Schubert fprintf_filtered (stream, "#%d ", bitpos);
9265796c8dcSSimon Schubert }
9275796c8dcSSimon Schubert }
9285796c8dcSSimon Schubert fputs_filtered ("]", stream);
929*ef5ccd6cSJohn Marino }
930c50c785cSJohn Marino
931c50c785cSJohn Marino /* Print a scalar of data of type TYPE, pointed to in GDB by VALADDR,
932c50c785cSJohn Marino according to OPTIONS and SIZE on STREAM. Format i is not supported
933c50c785cSJohn Marino at this level.
934c50c785cSJohn Marino
935c50c785cSJohn Marino This is how the elements of an array or structure are printed
936c50c785cSJohn Marino with a format. */
937c50c785cSJohn Marino
938c50c785cSJohn Marino void
val_print_scalar_formatted(struct type * type,const gdb_byte * valaddr,int embedded_offset,const struct value * val,const struct value_print_options * options,int size,struct ui_file * stream)939c50c785cSJohn Marino val_print_scalar_formatted (struct type *type,
940c50c785cSJohn Marino const gdb_byte *valaddr, int embedded_offset,
941c50c785cSJohn Marino const struct value *val,
942c50c785cSJohn Marino const struct value_print_options *options,
943c50c785cSJohn Marino int size,
944c50c785cSJohn Marino struct ui_file *stream)
945c50c785cSJohn Marino {
946c50c785cSJohn Marino gdb_assert (val != NULL);
947c50c785cSJohn Marino gdb_assert (valaddr == value_contents_for_printing_const (val));
948c50c785cSJohn Marino
949c50c785cSJohn Marino /* If we get here with a string format, try again without it. Go
950c50c785cSJohn Marino all the way back to the language printers, which may call us
951c50c785cSJohn Marino again. */
952c50c785cSJohn Marino if (options->format == 's')
953c50c785cSJohn Marino {
954c50c785cSJohn Marino struct value_print_options opts = *options;
955c50c785cSJohn Marino opts.format = 0;
956c50c785cSJohn Marino opts.deref_ref = 0;
957c50c785cSJohn Marino val_print (type, valaddr, embedded_offset, 0, stream, 0, val, &opts,
958c50c785cSJohn Marino current_language);
959c50c785cSJohn Marino return;
960c50c785cSJohn Marino }
961c50c785cSJohn Marino
962c50c785cSJohn Marino /* A scalar object that does not have all bits available can't be
963c50c785cSJohn Marino printed, because all bits contribute to its representation. */
964c50c785cSJohn Marino if (!value_bits_valid (val, TARGET_CHAR_BIT * embedded_offset,
965c50c785cSJohn Marino TARGET_CHAR_BIT * TYPE_LENGTH (type)))
966c50c785cSJohn Marino val_print_optimized_out (stream);
967c50c785cSJohn Marino else if (!value_bytes_available (val, embedded_offset, TYPE_LENGTH (type)))
968c50c785cSJohn Marino val_print_unavailable (stream);
969c50c785cSJohn Marino else
970c50c785cSJohn Marino print_scalar_formatted (valaddr + embedded_offset, type,
971c50c785cSJohn Marino options, size, stream);
9725796c8dcSSimon Schubert }
9735796c8dcSSimon Schubert
9745796c8dcSSimon Schubert /* Print a number according to FORMAT which is one of d,u,x,o,b,h,w,g.
9755796c8dcSSimon Schubert The raison d'etre of this function is to consolidate printing of
9765796c8dcSSimon Schubert LONG_LONG's into this one function. The format chars b,h,w,g are
9775796c8dcSSimon Schubert from print_scalar_formatted(). Numbers are printed using C
9785796c8dcSSimon Schubert format.
9795796c8dcSSimon Schubert
9805796c8dcSSimon Schubert USE_C_FORMAT means to use C format in all cases. Without it,
9815796c8dcSSimon Schubert 'o' and 'x' format do not include the standard C radix prefix
9825796c8dcSSimon Schubert (leading 0 or 0x).
9835796c8dcSSimon Schubert
9845796c8dcSSimon Schubert Hilfinger/2004-09-09: USE_C_FORMAT was originally called USE_LOCAL
9855796c8dcSSimon Schubert and was intended to request formating according to the current
9865796c8dcSSimon Schubert language and would be used for most integers that GDB prints. The
9875796c8dcSSimon Schubert exceptional cases were things like protocols where the format of
9885796c8dcSSimon Schubert the integer is a protocol thing, not a user-visible thing). The
9895796c8dcSSimon Schubert parameter remains to preserve the information of what things might
9905796c8dcSSimon Schubert be printed with language-specific format, should we ever resurrect
9915796c8dcSSimon Schubert that capability. */
9925796c8dcSSimon Schubert
9935796c8dcSSimon Schubert void
print_longest(struct ui_file * stream,int format,int use_c_format,LONGEST val_long)9945796c8dcSSimon Schubert print_longest (struct ui_file *stream, int format, int use_c_format,
9955796c8dcSSimon Schubert LONGEST val_long)
9965796c8dcSSimon Schubert {
9975796c8dcSSimon Schubert const char *val;
9985796c8dcSSimon Schubert
9995796c8dcSSimon Schubert switch (format)
10005796c8dcSSimon Schubert {
10015796c8dcSSimon Schubert case 'd':
10025796c8dcSSimon Schubert val = int_string (val_long, 10, 1, 0, 1); break;
10035796c8dcSSimon Schubert case 'u':
10045796c8dcSSimon Schubert val = int_string (val_long, 10, 0, 0, 1); break;
10055796c8dcSSimon Schubert case 'x':
10065796c8dcSSimon Schubert val = int_string (val_long, 16, 0, 0, use_c_format); break;
10075796c8dcSSimon Schubert case 'b':
10085796c8dcSSimon Schubert val = int_string (val_long, 16, 0, 2, 1); break;
10095796c8dcSSimon Schubert case 'h':
10105796c8dcSSimon Schubert val = int_string (val_long, 16, 0, 4, 1); break;
10115796c8dcSSimon Schubert case 'w':
10125796c8dcSSimon Schubert val = int_string (val_long, 16, 0, 8, 1); break;
10135796c8dcSSimon Schubert case 'g':
10145796c8dcSSimon Schubert val = int_string (val_long, 16, 0, 16, 1); break;
10155796c8dcSSimon Schubert break;
10165796c8dcSSimon Schubert case 'o':
10175796c8dcSSimon Schubert val = int_string (val_long, 8, 0, 0, use_c_format); break;
10185796c8dcSSimon Schubert default:
1019c50c785cSJohn Marino internal_error (__FILE__, __LINE__,
1020c50c785cSJohn Marino _("failed internal consistency check"));
10215796c8dcSSimon Schubert }
10225796c8dcSSimon Schubert fputs_filtered (val, stream);
10235796c8dcSSimon Schubert }
10245796c8dcSSimon Schubert
10255796c8dcSSimon Schubert /* This used to be a macro, but I don't think it is called often enough
10265796c8dcSSimon Schubert to merit such treatment. */
10275796c8dcSSimon Schubert /* Convert a LONGEST to an int. This is used in contexts (e.g. number of
10285796c8dcSSimon Schubert arguments to a function, number in a value history, register number, etc.)
10295796c8dcSSimon Schubert where the value must not be larger than can fit in an int. */
10305796c8dcSSimon Schubert
10315796c8dcSSimon Schubert int
longest_to_int(LONGEST arg)10325796c8dcSSimon Schubert longest_to_int (LONGEST arg)
10335796c8dcSSimon Schubert {
1034c50c785cSJohn Marino /* Let the compiler do the work. */
10355796c8dcSSimon Schubert int rtnval = (int) arg;
10365796c8dcSSimon Schubert
1037c50c785cSJohn Marino /* Check for overflows or underflows. */
10385796c8dcSSimon Schubert if (sizeof (LONGEST) > sizeof (int))
10395796c8dcSSimon Schubert {
10405796c8dcSSimon Schubert if (rtnval != arg)
10415796c8dcSSimon Schubert {
10425796c8dcSSimon Schubert error (_("Value out of range."));
10435796c8dcSSimon Schubert }
10445796c8dcSSimon Schubert }
10455796c8dcSSimon Schubert return (rtnval);
10465796c8dcSSimon Schubert }
10475796c8dcSSimon Schubert
10485796c8dcSSimon Schubert /* Print a floating point value of type TYPE (not always a
10495796c8dcSSimon Schubert TYPE_CODE_FLT), pointed to in GDB by VALADDR, on STREAM. */
10505796c8dcSSimon Schubert
10515796c8dcSSimon Schubert void
print_floating(const gdb_byte * valaddr,struct type * type,struct ui_file * stream)10525796c8dcSSimon Schubert print_floating (const gdb_byte *valaddr, struct type *type,
10535796c8dcSSimon Schubert struct ui_file *stream)
10545796c8dcSSimon Schubert {
10555796c8dcSSimon Schubert DOUBLEST doub;
10565796c8dcSSimon Schubert int inv;
10575796c8dcSSimon Schubert const struct floatformat *fmt = NULL;
10585796c8dcSSimon Schubert unsigned len = TYPE_LENGTH (type);
10595796c8dcSSimon Schubert enum float_kind kind;
10605796c8dcSSimon Schubert
10615796c8dcSSimon Schubert /* If it is a floating-point, check for obvious problems. */
10625796c8dcSSimon Schubert if (TYPE_CODE (type) == TYPE_CODE_FLT)
10635796c8dcSSimon Schubert fmt = floatformat_from_type (type);
10645796c8dcSSimon Schubert if (fmt != NULL)
10655796c8dcSSimon Schubert {
10665796c8dcSSimon Schubert kind = floatformat_classify (fmt, valaddr);
10675796c8dcSSimon Schubert if (kind == float_nan)
10685796c8dcSSimon Schubert {
10695796c8dcSSimon Schubert if (floatformat_is_negative (fmt, valaddr))
10705796c8dcSSimon Schubert fprintf_filtered (stream, "-");
10715796c8dcSSimon Schubert fprintf_filtered (stream, "nan(");
10725796c8dcSSimon Schubert fputs_filtered ("0x", stream);
10735796c8dcSSimon Schubert fputs_filtered (floatformat_mantissa (fmt, valaddr), stream);
10745796c8dcSSimon Schubert fprintf_filtered (stream, ")");
10755796c8dcSSimon Schubert return;
10765796c8dcSSimon Schubert }
10775796c8dcSSimon Schubert else if (kind == float_infinite)
10785796c8dcSSimon Schubert {
10795796c8dcSSimon Schubert if (floatformat_is_negative (fmt, valaddr))
10805796c8dcSSimon Schubert fputs_filtered ("-", stream);
10815796c8dcSSimon Schubert fputs_filtered ("inf", stream);
10825796c8dcSSimon Schubert return;
10835796c8dcSSimon Schubert }
10845796c8dcSSimon Schubert }
10855796c8dcSSimon Schubert
10865796c8dcSSimon Schubert /* NOTE: cagney/2002-01-15: The TYPE passed into print_floating()
10875796c8dcSSimon Schubert isn't necessarily a TYPE_CODE_FLT. Consequently, unpack_double
10885796c8dcSSimon Schubert needs to be used as that takes care of any necessary type
10895796c8dcSSimon Schubert conversions. Such conversions are of course direct to DOUBLEST
10905796c8dcSSimon Schubert and disregard any possible target floating point limitations.
10915796c8dcSSimon Schubert For instance, a u64 would be converted and displayed exactly on a
10925796c8dcSSimon Schubert host with 80 bit DOUBLEST but with loss of information on a host
10935796c8dcSSimon Schubert with 64 bit DOUBLEST. */
10945796c8dcSSimon Schubert
10955796c8dcSSimon Schubert doub = unpack_double (type, valaddr, &inv);
10965796c8dcSSimon Schubert if (inv)
10975796c8dcSSimon Schubert {
10985796c8dcSSimon Schubert fprintf_filtered (stream, "<invalid float value>");
10995796c8dcSSimon Schubert return;
11005796c8dcSSimon Schubert }
11015796c8dcSSimon Schubert
11025796c8dcSSimon Schubert /* FIXME: kettenis/2001-01-20: The following code makes too much
11035796c8dcSSimon Schubert assumptions about the host and target floating point format. */
11045796c8dcSSimon Schubert
11055796c8dcSSimon Schubert /* NOTE: cagney/2002-02-03: Since the TYPE of what was passed in may
11065796c8dcSSimon Schubert not necessarily be a TYPE_CODE_FLT, the below ignores that and
11075796c8dcSSimon Schubert instead uses the type's length to determine the precision of the
11085796c8dcSSimon Schubert floating-point value being printed. */
11095796c8dcSSimon Schubert
11105796c8dcSSimon Schubert if (len < sizeof (double))
11115796c8dcSSimon Schubert fprintf_filtered (stream, "%.9g", (double) doub);
11125796c8dcSSimon Schubert else if (len == sizeof (double))
11135796c8dcSSimon Schubert fprintf_filtered (stream, "%.17g", (double) doub);
11145796c8dcSSimon Schubert else
11155796c8dcSSimon Schubert #ifdef PRINTF_HAS_LONG_DOUBLE
11165796c8dcSSimon Schubert fprintf_filtered (stream, "%.35Lg", doub);
11175796c8dcSSimon Schubert #else
11185796c8dcSSimon Schubert /* This at least wins with values that are representable as
11195796c8dcSSimon Schubert doubles. */
11205796c8dcSSimon Schubert fprintf_filtered (stream, "%.17g", (double) doub);
11215796c8dcSSimon Schubert #endif
11225796c8dcSSimon Schubert }
11235796c8dcSSimon Schubert
11245796c8dcSSimon Schubert void
print_decimal_floating(const gdb_byte * valaddr,struct type * type,struct ui_file * stream)11255796c8dcSSimon Schubert print_decimal_floating (const gdb_byte *valaddr, struct type *type,
11265796c8dcSSimon Schubert struct ui_file *stream)
11275796c8dcSSimon Schubert {
11285796c8dcSSimon Schubert enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
11295796c8dcSSimon Schubert char decstr[MAX_DECIMAL_STRING];
11305796c8dcSSimon Schubert unsigned len = TYPE_LENGTH (type);
11315796c8dcSSimon Schubert
11325796c8dcSSimon Schubert decimal_to_string (valaddr, len, byte_order, decstr);
11335796c8dcSSimon Schubert fputs_filtered (decstr, stream);
11345796c8dcSSimon Schubert return;
11355796c8dcSSimon Schubert }
11365796c8dcSSimon Schubert
11375796c8dcSSimon Schubert void
print_binary_chars(struct ui_file * stream,const gdb_byte * valaddr,unsigned len,enum bfd_endian byte_order)11385796c8dcSSimon Schubert print_binary_chars (struct ui_file *stream, const gdb_byte *valaddr,
11395796c8dcSSimon Schubert unsigned len, enum bfd_endian byte_order)
11405796c8dcSSimon Schubert {
11415796c8dcSSimon Schubert
11425796c8dcSSimon Schubert #define BITS_IN_BYTES 8
11435796c8dcSSimon Schubert
11445796c8dcSSimon Schubert const gdb_byte *p;
11455796c8dcSSimon Schubert unsigned int i;
11465796c8dcSSimon Schubert int b;
11475796c8dcSSimon Schubert
11485796c8dcSSimon Schubert /* Declared "int" so it will be signed.
1149c50c785cSJohn Marino This ensures that right shift will shift in zeros. */
1150c50c785cSJohn Marino
11515796c8dcSSimon Schubert const int mask = 0x080;
11525796c8dcSSimon Schubert
11535796c8dcSSimon Schubert /* FIXME: We should be not printing leading zeroes in most cases. */
11545796c8dcSSimon Schubert
11555796c8dcSSimon Schubert if (byte_order == BFD_ENDIAN_BIG)
11565796c8dcSSimon Schubert {
11575796c8dcSSimon Schubert for (p = valaddr;
11585796c8dcSSimon Schubert p < valaddr + len;
11595796c8dcSSimon Schubert p++)
11605796c8dcSSimon Schubert {
11615796c8dcSSimon Schubert /* Every byte has 8 binary characters; peel off
1162c50c785cSJohn Marino and print from the MSB end. */
1163c50c785cSJohn Marino
11645796c8dcSSimon Schubert for (i = 0; i < (BITS_IN_BYTES * sizeof (*p)); i++)
11655796c8dcSSimon Schubert {
11665796c8dcSSimon Schubert if (*p & (mask >> i))
11675796c8dcSSimon Schubert b = 1;
11685796c8dcSSimon Schubert else
11695796c8dcSSimon Schubert b = 0;
11705796c8dcSSimon Schubert
11715796c8dcSSimon Schubert fprintf_filtered (stream, "%1d", b);
11725796c8dcSSimon Schubert }
11735796c8dcSSimon Schubert }
11745796c8dcSSimon Schubert }
11755796c8dcSSimon Schubert else
11765796c8dcSSimon Schubert {
11775796c8dcSSimon Schubert for (p = valaddr + len - 1;
11785796c8dcSSimon Schubert p >= valaddr;
11795796c8dcSSimon Schubert p--)
11805796c8dcSSimon Schubert {
11815796c8dcSSimon Schubert for (i = 0; i < (BITS_IN_BYTES * sizeof (*p)); i++)
11825796c8dcSSimon Schubert {
11835796c8dcSSimon Schubert if (*p & (mask >> i))
11845796c8dcSSimon Schubert b = 1;
11855796c8dcSSimon Schubert else
11865796c8dcSSimon Schubert b = 0;
11875796c8dcSSimon Schubert
11885796c8dcSSimon Schubert fprintf_filtered (stream, "%1d", b);
11895796c8dcSSimon Schubert }
11905796c8dcSSimon Schubert }
11915796c8dcSSimon Schubert }
11925796c8dcSSimon Schubert }
11935796c8dcSSimon Schubert
11945796c8dcSSimon Schubert /* VALADDR points to an integer of LEN bytes.
1195c50c785cSJohn Marino Print it in octal on stream or format it in buf. */
1196c50c785cSJohn Marino
11975796c8dcSSimon Schubert void
print_octal_chars(struct ui_file * stream,const gdb_byte * valaddr,unsigned len,enum bfd_endian byte_order)11985796c8dcSSimon Schubert print_octal_chars (struct ui_file *stream, const gdb_byte *valaddr,
11995796c8dcSSimon Schubert unsigned len, enum bfd_endian byte_order)
12005796c8dcSSimon Schubert {
12015796c8dcSSimon Schubert const gdb_byte *p;
12025796c8dcSSimon Schubert unsigned char octa1, octa2, octa3, carry;
12035796c8dcSSimon Schubert int cycle;
12045796c8dcSSimon Schubert
12055796c8dcSSimon Schubert /* FIXME: We should be not printing leading zeroes in most cases. */
12065796c8dcSSimon Schubert
12075796c8dcSSimon Schubert
12085796c8dcSSimon Schubert /* Octal is 3 bits, which doesn't fit. Yuk. So we have to track
12095796c8dcSSimon Schubert * the extra bits, which cycle every three bytes:
12105796c8dcSSimon Schubert *
12115796c8dcSSimon Schubert * Byte side: 0 1 2 3
12125796c8dcSSimon Schubert * | | | |
12135796c8dcSSimon Schubert * bit number 123 456 78 | 9 012 345 6 | 78 901 234 | 567 890 12 |
12145796c8dcSSimon Schubert *
12155796c8dcSSimon Schubert * Octal side: 0 1 carry 3 4 carry ...
12165796c8dcSSimon Schubert *
12175796c8dcSSimon Schubert * Cycle number: 0 1 2
12185796c8dcSSimon Schubert *
12195796c8dcSSimon Schubert * But of course we are printing from the high side, so we have to
12205796c8dcSSimon Schubert * figure out where in the cycle we are so that we end up with no
12215796c8dcSSimon Schubert * left over bits at the end.
12225796c8dcSSimon Schubert */
12235796c8dcSSimon Schubert #define BITS_IN_OCTAL 3
12245796c8dcSSimon Schubert #define HIGH_ZERO 0340
12255796c8dcSSimon Schubert #define LOW_ZERO 0016
12265796c8dcSSimon Schubert #define CARRY_ZERO 0003
12275796c8dcSSimon Schubert #define HIGH_ONE 0200
12285796c8dcSSimon Schubert #define MID_ONE 0160
12295796c8dcSSimon Schubert #define LOW_ONE 0016
12305796c8dcSSimon Schubert #define CARRY_ONE 0001
12315796c8dcSSimon Schubert #define HIGH_TWO 0300
12325796c8dcSSimon Schubert #define MID_TWO 0070
12335796c8dcSSimon Schubert #define LOW_TWO 0007
12345796c8dcSSimon Schubert
12355796c8dcSSimon Schubert /* For 32 we start in cycle 2, with two bits and one bit carry;
1236c50c785cSJohn Marino for 64 in cycle in cycle 1, with one bit and a two bit carry. */
1237c50c785cSJohn Marino
12385796c8dcSSimon Schubert cycle = (len * BITS_IN_BYTES) % BITS_IN_OCTAL;
12395796c8dcSSimon Schubert carry = 0;
12405796c8dcSSimon Schubert
12415796c8dcSSimon Schubert fputs_filtered ("0", stream);
12425796c8dcSSimon Schubert if (byte_order == BFD_ENDIAN_BIG)
12435796c8dcSSimon Schubert {
12445796c8dcSSimon Schubert for (p = valaddr;
12455796c8dcSSimon Schubert p < valaddr + len;
12465796c8dcSSimon Schubert p++)
12475796c8dcSSimon Schubert {
12485796c8dcSSimon Schubert switch (cycle)
12495796c8dcSSimon Schubert {
12505796c8dcSSimon Schubert case 0:
1251c50c785cSJohn Marino /* No carry in, carry out two bits. */
1252c50c785cSJohn Marino
12535796c8dcSSimon Schubert octa1 = (HIGH_ZERO & *p) >> 5;
12545796c8dcSSimon Schubert octa2 = (LOW_ZERO & *p) >> 2;
12555796c8dcSSimon Schubert carry = (CARRY_ZERO & *p);
12565796c8dcSSimon Schubert fprintf_filtered (stream, "%o", octa1);
12575796c8dcSSimon Schubert fprintf_filtered (stream, "%o", octa2);
12585796c8dcSSimon Schubert break;
12595796c8dcSSimon Schubert
12605796c8dcSSimon Schubert case 1:
1261c50c785cSJohn Marino /* Carry in two bits, carry out one bit. */
1262c50c785cSJohn Marino
12635796c8dcSSimon Schubert octa1 = (carry << 1) | ((HIGH_ONE & *p) >> 7);
12645796c8dcSSimon Schubert octa2 = (MID_ONE & *p) >> 4;
12655796c8dcSSimon Schubert octa3 = (LOW_ONE & *p) >> 1;
12665796c8dcSSimon Schubert carry = (CARRY_ONE & *p);
12675796c8dcSSimon Schubert fprintf_filtered (stream, "%o", octa1);
12685796c8dcSSimon Schubert fprintf_filtered (stream, "%o", octa2);
12695796c8dcSSimon Schubert fprintf_filtered (stream, "%o", octa3);
12705796c8dcSSimon Schubert break;
12715796c8dcSSimon Schubert
12725796c8dcSSimon Schubert case 2:
1273c50c785cSJohn Marino /* Carry in one bit, no carry out. */
1274c50c785cSJohn Marino
12755796c8dcSSimon Schubert octa1 = (carry << 2) | ((HIGH_TWO & *p) >> 6);
12765796c8dcSSimon Schubert octa2 = (MID_TWO & *p) >> 3;
12775796c8dcSSimon Schubert octa3 = (LOW_TWO & *p);
12785796c8dcSSimon Schubert carry = 0;
12795796c8dcSSimon Schubert fprintf_filtered (stream, "%o", octa1);
12805796c8dcSSimon Schubert fprintf_filtered (stream, "%o", octa2);
12815796c8dcSSimon Schubert fprintf_filtered (stream, "%o", octa3);
12825796c8dcSSimon Schubert break;
12835796c8dcSSimon Schubert
12845796c8dcSSimon Schubert default:
12855796c8dcSSimon Schubert error (_("Internal error in octal conversion;"));
12865796c8dcSSimon Schubert }
12875796c8dcSSimon Schubert
12885796c8dcSSimon Schubert cycle++;
12895796c8dcSSimon Schubert cycle = cycle % BITS_IN_OCTAL;
12905796c8dcSSimon Schubert }
12915796c8dcSSimon Schubert }
12925796c8dcSSimon Schubert else
12935796c8dcSSimon Schubert {
12945796c8dcSSimon Schubert for (p = valaddr + len - 1;
12955796c8dcSSimon Schubert p >= valaddr;
12965796c8dcSSimon Schubert p--)
12975796c8dcSSimon Schubert {
12985796c8dcSSimon Schubert switch (cycle)
12995796c8dcSSimon Schubert {
13005796c8dcSSimon Schubert case 0:
13015796c8dcSSimon Schubert /* Carry out, no carry in */
1302c50c785cSJohn Marino
13035796c8dcSSimon Schubert octa1 = (HIGH_ZERO & *p) >> 5;
13045796c8dcSSimon Schubert octa2 = (LOW_ZERO & *p) >> 2;
13055796c8dcSSimon Schubert carry = (CARRY_ZERO & *p);
13065796c8dcSSimon Schubert fprintf_filtered (stream, "%o", octa1);
13075796c8dcSSimon Schubert fprintf_filtered (stream, "%o", octa2);
13085796c8dcSSimon Schubert break;
13095796c8dcSSimon Schubert
13105796c8dcSSimon Schubert case 1:
13115796c8dcSSimon Schubert /* Carry in, carry out */
1312c50c785cSJohn Marino
13135796c8dcSSimon Schubert octa1 = (carry << 1) | ((HIGH_ONE & *p) >> 7);
13145796c8dcSSimon Schubert octa2 = (MID_ONE & *p) >> 4;
13155796c8dcSSimon Schubert octa3 = (LOW_ONE & *p) >> 1;
13165796c8dcSSimon Schubert carry = (CARRY_ONE & *p);
13175796c8dcSSimon Schubert fprintf_filtered (stream, "%o", octa1);
13185796c8dcSSimon Schubert fprintf_filtered (stream, "%o", octa2);
13195796c8dcSSimon Schubert fprintf_filtered (stream, "%o", octa3);
13205796c8dcSSimon Schubert break;
13215796c8dcSSimon Schubert
13225796c8dcSSimon Schubert case 2:
13235796c8dcSSimon Schubert /* Carry in, no carry out */
1324c50c785cSJohn Marino
13255796c8dcSSimon Schubert octa1 = (carry << 2) | ((HIGH_TWO & *p) >> 6);
13265796c8dcSSimon Schubert octa2 = (MID_TWO & *p) >> 3;
13275796c8dcSSimon Schubert octa3 = (LOW_TWO & *p);
13285796c8dcSSimon Schubert carry = 0;
13295796c8dcSSimon Schubert fprintf_filtered (stream, "%o", octa1);
13305796c8dcSSimon Schubert fprintf_filtered (stream, "%o", octa2);
13315796c8dcSSimon Schubert fprintf_filtered (stream, "%o", octa3);
13325796c8dcSSimon Schubert break;
13335796c8dcSSimon Schubert
13345796c8dcSSimon Schubert default:
13355796c8dcSSimon Schubert error (_("Internal error in octal conversion;"));
13365796c8dcSSimon Schubert }
13375796c8dcSSimon Schubert
13385796c8dcSSimon Schubert cycle++;
13395796c8dcSSimon Schubert cycle = cycle % BITS_IN_OCTAL;
13405796c8dcSSimon Schubert }
13415796c8dcSSimon Schubert }
13425796c8dcSSimon Schubert
13435796c8dcSSimon Schubert }
13445796c8dcSSimon Schubert
13455796c8dcSSimon Schubert /* VALADDR points to an integer of LEN bytes.
1346c50c785cSJohn Marino Print it in decimal on stream or format it in buf. */
1347c50c785cSJohn Marino
13485796c8dcSSimon Schubert void
print_decimal_chars(struct ui_file * stream,const gdb_byte * valaddr,unsigned len,enum bfd_endian byte_order)13495796c8dcSSimon Schubert print_decimal_chars (struct ui_file *stream, const gdb_byte *valaddr,
13505796c8dcSSimon Schubert unsigned len, enum bfd_endian byte_order)
13515796c8dcSSimon Schubert {
13525796c8dcSSimon Schubert #define TEN 10
13535796c8dcSSimon Schubert #define CARRY_OUT( x ) ((x) / TEN) /* extend char to int */
13545796c8dcSSimon Schubert #define CARRY_LEFT( x ) ((x) % TEN)
13555796c8dcSSimon Schubert #define SHIFT( x ) ((x) << 4)
13565796c8dcSSimon Schubert #define LOW_NIBBLE( x ) ( (x) & 0x00F)
13575796c8dcSSimon Schubert #define HIGH_NIBBLE( x ) (((x) & 0x0F0) >> 4)
13585796c8dcSSimon Schubert
13595796c8dcSSimon Schubert const gdb_byte *p;
13605796c8dcSSimon Schubert unsigned char *digits;
13615796c8dcSSimon Schubert int carry;
13625796c8dcSSimon Schubert int decimal_len;
13635796c8dcSSimon Schubert int i, j, decimal_digits;
13645796c8dcSSimon Schubert int dummy;
13655796c8dcSSimon Schubert int flip;
13665796c8dcSSimon Schubert
13675796c8dcSSimon Schubert /* Base-ten number is less than twice as many digits
1368c50c785cSJohn Marino as the base 16 number, which is 2 digits per byte. */
1369c50c785cSJohn Marino
13705796c8dcSSimon Schubert decimal_len = len * 2 * 2;
13715796c8dcSSimon Schubert digits = xmalloc (decimal_len);
13725796c8dcSSimon Schubert
13735796c8dcSSimon Schubert for (i = 0; i < decimal_len; i++)
13745796c8dcSSimon Schubert {
13755796c8dcSSimon Schubert digits[i] = 0;
13765796c8dcSSimon Schubert }
13775796c8dcSSimon Schubert
13785796c8dcSSimon Schubert /* Ok, we have an unknown number of bytes of data to be printed in
13795796c8dcSSimon Schubert * decimal.
13805796c8dcSSimon Schubert *
13815796c8dcSSimon Schubert * Given a hex number (in nibbles) as XYZ, we start by taking X and
13825796c8dcSSimon Schubert * decemalizing it as "x1 x2" in two decimal nibbles. Then we multiply
13835796c8dcSSimon Schubert * the nibbles by 16, add Y and re-decimalize. Repeat with Z.
13845796c8dcSSimon Schubert *
13855796c8dcSSimon Schubert * The trick is that "digits" holds a base-10 number, but sometimes
13865796c8dcSSimon Schubert * the individual digits are > 10.
13875796c8dcSSimon Schubert *
13885796c8dcSSimon Schubert * Outer loop is per nibble (hex digit) of input, from MSD end to
13895796c8dcSSimon Schubert * LSD end.
13905796c8dcSSimon Schubert */
13915796c8dcSSimon Schubert decimal_digits = 0; /* Number of decimal digits so far */
13925796c8dcSSimon Schubert p = (byte_order == BFD_ENDIAN_BIG) ? valaddr : valaddr + len - 1;
13935796c8dcSSimon Schubert flip = 0;
13945796c8dcSSimon Schubert while ((byte_order == BFD_ENDIAN_BIG) ? (p < valaddr + len) : (p >= valaddr))
13955796c8dcSSimon Schubert {
13965796c8dcSSimon Schubert /*
13975796c8dcSSimon Schubert * Multiply current base-ten number by 16 in place.
13985796c8dcSSimon Schubert * Each digit was between 0 and 9, now is between
13995796c8dcSSimon Schubert * 0 and 144.
14005796c8dcSSimon Schubert */
14015796c8dcSSimon Schubert for (j = 0; j < decimal_digits; j++)
14025796c8dcSSimon Schubert {
14035796c8dcSSimon Schubert digits[j] = SHIFT (digits[j]);
14045796c8dcSSimon Schubert }
14055796c8dcSSimon Schubert
14065796c8dcSSimon Schubert /* Take the next nibble off the input and add it to what
14075796c8dcSSimon Schubert * we've got in the LSB position. Bottom 'digit' is now
14085796c8dcSSimon Schubert * between 0 and 159.
14095796c8dcSSimon Schubert *
14105796c8dcSSimon Schubert * "flip" is used to run this loop twice for each byte.
14115796c8dcSSimon Schubert */
14125796c8dcSSimon Schubert if (flip == 0)
14135796c8dcSSimon Schubert {
1414c50c785cSJohn Marino /* Take top nibble. */
1415c50c785cSJohn Marino
14165796c8dcSSimon Schubert digits[0] += HIGH_NIBBLE (*p);
14175796c8dcSSimon Schubert flip = 1;
14185796c8dcSSimon Schubert }
14195796c8dcSSimon Schubert else
14205796c8dcSSimon Schubert {
1421c50c785cSJohn Marino /* Take low nibble and bump our pointer "p". */
1422c50c785cSJohn Marino
14235796c8dcSSimon Schubert digits[0] += LOW_NIBBLE (*p);
14245796c8dcSSimon Schubert if (byte_order == BFD_ENDIAN_BIG)
14255796c8dcSSimon Schubert p++;
14265796c8dcSSimon Schubert else
14275796c8dcSSimon Schubert p--;
14285796c8dcSSimon Schubert flip = 0;
14295796c8dcSSimon Schubert }
14305796c8dcSSimon Schubert
14315796c8dcSSimon Schubert /* Re-decimalize. We have to do this often enough
14325796c8dcSSimon Schubert * that we don't overflow, but once per nibble is
14335796c8dcSSimon Schubert * overkill. Easier this way, though. Note that the
14345796c8dcSSimon Schubert * carry is often larger than 10 (e.g. max initial
14355796c8dcSSimon Schubert * carry out of lowest nibble is 15, could bubble all
14365796c8dcSSimon Schubert * the way up greater than 10). So we have to do
14375796c8dcSSimon Schubert * the carrying beyond the last current digit.
14385796c8dcSSimon Schubert */
14395796c8dcSSimon Schubert carry = 0;
14405796c8dcSSimon Schubert for (j = 0; j < decimal_len - 1; j++)
14415796c8dcSSimon Schubert {
14425796c8dcSSimon Schubert digits[j] += carry;
14435796c8dcSSimon Schubert
14445796c8dcSSimon Schubert /* "/" won't handle an unsigned char with
14455796c8dcSSimon Schubert * a value that if signed would be negative.
14465796c8dcSSimon Schubert * So extend to longword int via "dummy".
14475796c8dcSSimon Schubert */
14485796c8dcSSimon Schubert dummy = digits[j];
14495796c8dcSSimon Schubert carry = CARRY_OUT (dummy);
14505796c8dcSSimon Schubert digits[j] = CARRY_LEFT (dummy);
14515796c8dcSSimon Schubert
14525796c8dcSSimon Schubert if (j >= decimal_digits && carry == 0)
14535796c8dcSSimon Schubert {
14545796c8dcSSimon Schubert /*
14555796c8dcSSimon Schubert * All higher digits are 0 and we
14565796c8dcSSimon Schubert * no longer have a carry.
14575796c8dcSSimon Schubert *
14585796c8dcSSimon Schubert * Note: "j" is 0-based, "decimal_digits" is
14595796c8dcSSimon Schubert * 1-based.
14605796c8dcSSimon Schubert */
14615796c8dcSSimon Schubert decimal_digits = j + 1;
14625796c8dcSSimon Schubert break;
14635796c8dcSSimon Schubert }
14645796c8dcSSimon Schubert }
14655796c8dcSSimon Schubert }
14665796c8dcSSimon Schubert
14675796c8dcSSimon Schubert /* Ok, now "digits" is the decimal representation, with
1468c50c785cSJohn Marino the "decimal_digits" actual digits. Print! */
1469c50c785cSJohn Marino
14705796c8dcSSimon Schubert for (i = decimal_digits - 1; i >= 0; i--)
14715796c8dcSSimon Schubert {
14725796c8dcSSimon Schubert fprintf_filtered (stream, "%1d", digits[i]);
14735796c8dcSSimon Schubert }
14745796c8dcSSimon Schubert xfree (digits);
14755796c8dcSSimon Schubert }
14765796c8dcSSimon Schubert
14775796c8dcSSimon Schubert /* VALADDR points to an integer of LEN bytes. Print it in hex on stream. */
14785796c8dcSSimon Schubert
14795796c8dcSSimon Schubert void
print_hex_chars(struct ui_file * stream,const gdb_byte * valaddr,unsigned len,enum bfd_endian byte_order)14805796c8dcSSimon Schubert print_hex_chars (struct ui_file *stream, const gdb_byte *valaddr,
14815796c8dcSSimon Schubert unsigned len, enum bfd_endian byte_order)
14825796c8dcSSimon Schubert {
14835796c8dcSSimon Schubert const gdb_byte *p;
14845796c8dcSSimon Schubert
14855796c8dcSSimon Schubert /* FIXME: We should be not printing leading zeroes in most cases. */
14865796c8dcSSimon Schubert
14875796c8dcSSimon Schubert fputs_filtered ("0x", stream);
14885796c8dcSSimon Schubert if (byte_order == BFD_ENDIAN_BIG)
14895796c8dcSSimon Schubert {
14905796c8dcSSimon Schubert for (p = valaddr;
14915796c8dcSSimon Schubert p < valaddr + len;
14925796c8dcSSimon Schubert p++)
14935796c8dcSSimon Schubert {
14945796c8dcSSimon Schubert fprintf_filtered (stream, "%02x", *p);
14955796c8dcSSimon Schubert }
14965796c8dcSSimon Schubert }
14975796c8dcSSimon Schubert else
14985796c8dcSSimon Schubert {
14995796c8dcSSimon Schubert for (p = valaddr + len - 1;
15005796c8dcSSimon Schubert p >= valaddr;
15015796c8dcSSimon Schubert p--)
15025796c8dcSSimon Schubert {
15035796c8dcSSimon Schubert fprintf_filtered (stream, "%02x", *p);
15045796c8dcSSimon Schubert }
15055796c8dcSSimon Schubert }
15065796c8dcSSimon Schubert }
15075796c8dcSSimon Schubert
1508c50c785cSJohn Marino /* VALADDR points to a char integer of LEN bytes.
1509c50c785cSJohn Marino Print it out in appropriate language form on stream.
15105796c8dcSSimon Schubert Omit any leading zero chars. */
15115796c8dcSSimon Schubert
15125796c8dcSSimon Schubert void
print_char_chars(struct ui_file * stream,struct type * type,const gdb_byte * valaddr,unsigned len,enum bfd_endian byte_order)15135796c8dcSSimon Schubert print_char_chars (struct ui_file *stream, struct type *type,
15145796c8dcSSimon Schubert const gdb_byte *valaddr,
15155796c8dcSSimon Schubert unsigned len, enum bfd_endian byte_order)
15165796c8dcSSimon Schubert {
15175796c8dcSSimon Schubert const gdb_byte *p;
15185796c8dcSSimon Schubert
15195796c8dcSSimon Schubert if (byte_order == BFD_ENDIAN_BIG)
15205796c8dcSSimon Schubert {
15215796c8dcSSimon Schubert p = valaddr;
15225796c8dcSSimon Schubert while (p < valaddr + len - 1 && *p == 0)
15235796c8dcSSimon Schubert ++p;
15245796c8dcSSimon Schubert
15255796c8dcSSimon Schubert while (p < valaddr + len)
15265796c8dcSSimon Schubert {
15275796c8dcSSimon Schubert LA_EMIT_CHAR (*p, type, stream, '\'');
15285796c8dcSSimon Schubert ++p;
15295796c8dcSSimon Schubert }
15305796c8dcSSimon Schubert }
15315796c8dcSSimon Schubert else
15325796c8dcSSimon Schubert {
15335796c8dcSSimon Schubert p = valaddr + len - 1;
15345796c8dcSSimon Schubert while (p > valaddr && *p == 0)
15355796c8dcSSimon Schubert --p;
15365796c8dcSSimon Schubert
15375796c8dcSSimon Schubert while (p >= valaddr)
15385796c8dcSSimon Schubert {
15395796c8dcSSimon Schubert LA_EMIT_CHAR (*p, type, stream, '\'');
15405796c8dcSSimon Schubert --p;
15415796c8dcSSimon Schubert }
15425796c8dcSSimon Schubert }
15435796c8dcSSimon Schubert }
15445796c8dcSSimon Schubert
1545*ef5ccd6cSJohn Marino /* Print function pointer with inferior address ADDRESS onto stdio
1546*ef5ccd6cSJohn Marino stream STREAM. */
1547*ef5ccd6cSJohn Marino
1548*ef5ccd6cSJohn Marino void
print_function_pointer_address(const struct value_print_options * options,struct gdbarch * gdbarch,CORE_ADDR address,struct ui_file * stream)1549*ef5ccd6cSJohn Marino print_function_pointer_address (const struct value_print_options *options,
1550*ef5ccd6cSJohn Marino struct gdbarch *gdbarch,
1551*ef5ccd6cSJohn Marino CORE_ADDR address,
1552*ef5ccd6cSJohn Marino struct ui_file *stream)
1553*ef5ccd6cSJohn Marino {
1554*ef5ccd6cSJohn Marino CORE_ADDR func_addr
1555*ef5ccd6cSJohn Marino = gdbarch_convert_from_func_ptr_addr (gdbarch, address,
1556*ef5ccd6cSJohn Marino ¤t_target);
1557*ef5ccd6cSJohn Marino
1558*ef5ccd6cSJohn Marino /* If the function pointer is represented by a description, print
1559*ef5ccd6cSJohn Marino the address of the description. */
1560*ef5ccd6cSJohn Marino if (options->addressprint && func_addr != address)
1561*ef5ccd6cSJohn Marino {
1562*ef5ccd6cSJohn Marino fputs_filtered ("@", stream);
1563*ef5ccd6cSJohn Marino fputs_filtered (paddress (gdbarch, address), stream);
1564*ef5ccd6cSJohn Marino fputs_filtered (": ", stream);
1565*ef5ccd6cSJohn Marino }
1566*ef5ccd6cSJohn Marino print_address_demangle (options, gdbarch, func_addr, stream, demangle);
1567*ef5ccd6cSJohn Marino }
1568*ef5ccd6cSJohn Marino
1569*ef5ccd6cSJohn Marino
15705796c8dcSSimon Schubert /* Print on STREAM using the given OPTIONS the index for the element
15715796c8dcSSimon Schubert at INDEX of an array whose index type is INDEX_TYPE. */
15725796c8dcSSimon Schubert
15735796c8dcSSimon Schubert void
maybe_print_array_index(struct type * index_type,LONGEST index,struct ui_file * stream,const struct value_print_options * options)15745796c8dcSSimon Schubert maybe_print_array_index (struct type *index_type, LONGEST index,
15755796c8dcSSimon Schubert struct ui_file *stream,
15765796c8dcSSimon Schubert const struct value_print_options *options)
15775796c8dcSSimon Schubert {
15785796c8dcSSimon Schubert struct value *index_value;
15795796c8dcSSimon Schubert
15805796c8dcSSimon Schubert if (!options->print_array_indexes)
15815796c8dcSSimon Schubert return;
15825796c8dcSSimon Schubert
15835796c8dcSSimon Schubert index_value = value_from_longest (index_type, index);
15845796c8dcSSimon Schubert
15855796c8dcSSimon Schubert LA_PRINT_ARRAY_INDEX (index_value, stream, options);
15865796c8dcSSimon Schubert }
15875796c8dcSSimon Schubert
15885796c8dcSSimon Schubert /* Called by various <lang>_val_print routines to print elements of an
15895796c8dcSSimon Schubert array in the form "<elem1>, <elem2>, <elem3>, ...".
15905796c8dcSSimon Schubert
15915796c8dcSSimon Schubert (FIXME?) Assumes array element separator is a comma, which is correct
15925796c8dcSSimon Schubert for all languages currently handled.
15935796c8dcSSimon Schubert (FIXME?) Some languages have a notation for repeated array elements,
1594c50c785cSJohn Marino perhaps we should try to use that notation when appropriate. */
15955796c8dcSSimon Schubert
15965796c8dcSSimon Schubert void
val_print_array_elements(struct type * type,const gdb_byte * valaddr,int embedded_offset,CORE_ADDR address,struct ui_file * stream,int recurse,const struct value * val,const struct value_print_options * options,unsigned int i)1597c50c785cSJohn Marino val_print_array_elements (struct type *type,
1598c50c785cSJohn Marino const gdb_byte *valaddr, int embedded_offset,
15995796c8dcSSimon Schubert CORE_ADDR address, struct ui_file *stream,
16005796c8dcSSimon Schubert int recurse,
1601cf7f2e2dSJohn Marino const struct value *val,
16025796c8dcSSimon Schubert const struct value_print_options *options,
16035796c8dcSSimon Schubert unsigned int i)
16045796c8dcSSimon Schubert {
16055796c8dcSSimon Schubert unsigned int things_printed = 0;
16065796c8dcSSimon Schubert unsigned len;
16075796c8dcSSimon Schubert struct type *elttype, *index_type;
16085796c8dcSSimon Schubert unsigned eltlen;
16095796c8dcSSimon Schubert /* Position of the array element we are examining to see
16105796c8dcSSimon Schubert whether it is repeated. */
16115796c8dcSSimon Schubert unsigned int rep1;
16125796c8dcSSimon Schubert /* Number of repetitions we have detected so far. */
16135796c8dcSSimon Schubert unsigned int reps;
1614c50c785cSJohn Marino LONGEST low_bound, high_bound;
16155796c8dcSSimon Schubert
16165796c8dcSSimon Schubert elttype = TYPE_TARGET_TYPE (type);
16175796c8dcSSimon Schubert eltlen = TYPE_LENGTH (check_typedef (elttype));
16185796c8dcSSimon Schubert index_type = TYPE_INDEX_TYPE (type);
16195796c8dcSSimon Schubert
1620c50c785cSJohn Marino if (get_array_bounds (type, &low_bound, &high_bound))
16215796c8dcSSimon Schubert {
1622c50c785cSJohn Marino /* The array length should normally be HIGH_BOUND - LOW_BOUND + 1.
1623c50c785cSJohn Marino But we have to be a little extra careful, because some languages
1624c50c785cSJohn Marino such as Ada allow LOW_BOUND to be greater than HIGH_BOUND for
1625c50c785cSJohn Marino empty arrays. In that situation, the array length is just zero,
1626c50c785cSJohn Marino not negative! */
1627c50c785cSJohn Marino if (low_bound > high_bound)
1628c50c785cSJohn Marino len = 0;
1629c50c785cSJohn Marino else
1630c50c785cSJohn Marino len = high_bound - low_bound + 1;
1631c50c785cSJohn Marino }
16325796c8dcSSimon Schubert else
16335796c8dcSSimon Schubert {
16345796c8dcSSimon Schubert warning (_("unable to get bounds of array, assuming null array"));
1635c50c785cSJohn Marino low_bound = 0;
16365796c8dcSSimon Schubert len = 0;
16375796c8dcSSimon Schubert }
16385796c8dcSSimon Schubert
16395796c8dcSSimon Schubert annotate_array_section_begin (i, elttype);
16405796c8dcSSimon Schubert
16415796c8dcSSimon Schubert for (; i < len && things_printed < options->print_max; i++)
16425796c8dcSSimon Schubert {
16435796c8dcSSimon Schubert if (i != 0)
16445796c8dcSSimon Schubert {
16455796c8dcSSimon Schubert if (options->prettyprint_arrays)
16465796c8dcSSimon Schubert {
16475796c8dcSSimon Schubert fprintf_filtered (stream, ",\n");
16485796c8dcSSimon Schubert print_spaces_filtered (2 + 2 * recurse, stream);
16495796c8dcSSimon Schubert }
16505796c8dcSSimon Schubert else
16515796c8dcSSimon Schubert {
16525796c8dcSSimon Schubert fprintf_filtered (stream, ", ");
16535796c8dcSSimon Schubert }
16545796c8dcSSimon Schubert }
16555796c8dcSSimon Schubert wrap_here (n_spaces (2 + 2 * recurse));
1656c50c785cSJohn Marino maybe_print_array_index (index_type, i + low_bound,
16575796c8dcSSimon Schubert stream, options);
16585796c8dcSSimon Schubert
16595796c8dcSSimon Schubert rep1 = i + 1;
16605796c8dcSSimon Schubert reps = 1;
1661c50c785cSJohn Marino /* Only check for reps if repeat_count_threshold is not set to
1662c50c785cSJohn Marino UINT_MAX (unlimited). */
1663c50c785cSJohn Marino if (options->repeat_count_threshold < UINT_MAX)
1664c50c785cSJohn Marino {
1665c50c785cSJohn Marino while (rep1 < len
1666c50c785cSJohn Marino && value_available_contents_eq (val,
1667c50c785cSJohn Marino embedded_offset + i * eltlen,
1668c50c785cSJohn Marino val,
1669c50c785cSJohn Marino (embedded_offset
1670c50c785cSJohn Marino + rep1 * eltlen),
1671c50c785cSJohn Marino eltlen))
16725796c8dcSSimon Schubert {
16735796c8dcSSimon Schubert ++reps;
16745796c8dcSSimon Schubert ++rep1;
16755796c8dcSSimon Schubert }
1676c50c785cSJohn Marino }
16775796c8dcSSimon Schubert
16785796c8dcSSimon Schubert if (reps > options->repeat_count_threshold)
16795796c8dcSSimon Schubert {
1680c50c785cSJohn Marino val_print (elttype, valaddr, embedded_offset + i * eltlen,
1681c50c785cSJohn Marino address, stream, recurse + 1, val, options,
1682c50c785cSJohn Marino current_language);
16835796c8dcSSimon Schubert annotate_elt_rep (reps);
16845796c8dcSSimon Schubert fprintf_filtered (stream, " <repeats %u times>", reps);
16855796c8dcSSimon Schubert annotate_elt_rep_end ();
16865796c8dcSSimon Schubert
16875796c8dcSSimon Schubert i = rep1 - 1;
16885796c8dcSSimon Schubert things_printed += options->repeat_count_threshold;
16895796c8dcSSimon Schubert }
16905796c8dcSSimon Schubert else
16915796c8dcSSimon Schubert {
1692c50c785cSJohn Marino val_print (elttype, valaddr, embedded_offset + i * eltlen,
1693c50c785cSJohn Marino address,
1694cf7f2e2dSJohn Marino stream, recurse + 1, val, options, current_language);
16955796c8dcSSimon Schubert annotate_elt ();
16965796c8dcSSimon Schubert things_printed++;
16975796c8dcSSimon Schubert }
16985796c8dcSSimon Schubert }
16995796c8dcSSimon Schubert annotate_array_section_end ();
17005796c8dcSSimon Schubert if (i < len)
17015796c8dcSSimon Schubert {
17025796c8dcSSimon Schubert fprintf_filtered (stream, "...");
17035796c8dcSSimon Schubert }
17045796c8dcSSimon Schubert }
17055796c8dcSSimon Schubert
17065796c8dcSSimon Schubert /* Read LEN bytes of target memory at address MEMADDR, placing the
17075796c8dcSSimon Schubert results in GDB's memory at MYADDR. Returns a count of the bytes
17085796c8dcSSimon Schubert actually read, and optionally an errno value in the location
17095796c8dcSSimon Schubert pointed to by ERRNOPTR if ERRNOPTR is non-null. */
17105796c8dcSSimon Schubert
17115796c8dcSSimon Schubert /* FIXME: cagney/1999-10-14: Only used by val_print_string. Can this
17125796c8dcSSimon Schubert function be eliminated. */
17135796c8dcSSimon Schubert
17145796c8dcSSimon Schubert static int
partial_memory_read(CORE_ADDR memaddr,gdb_byte * myaddr,int len,int * errnoptr)1715c50c785cSJohn Marino partial_memory_read (CORE_ADDR memaddr, gdb_byte *myaddr,
1716c50c785cSJohn Marino int len, int *errnoptr)
17175796c8dcSSimon Schubert {
17185796c8dcSSimon Schubert int nread; /* Number of bytes actually read. */
17195796c8dcSSimon Schubert int errcode; /* Error from last read. */
17205796c8dcSSimon Schubert
17215796c8dcSSimon Schubert /* First try a complete read. */
17225796c8dcSSimon Schubert errcode = target_read_memory (memaddr, myaddr, len);
17235796c8dcSSimon Schubert if (errcode == 0)
17245796c8dcSSimon Schubert {
17255796c8dcSSimon Schubert /* Got it all. */
17265796c8dcSSimon Schubert nread = len;
17275796c8dcSSimon Schubert }
17285796c8dcSSimon Schubert else
17295796c8dcSSimon Schubert {
17305796c8dcSSimon Schubert /* Loop, reading one byte at a time until we get as much as we can. */
17315796c8dcSSimon Schubert for (errcode = 0, nread = 0; len > 0 && errcode == 0; nread++, len--)
17325796c8dcSSimon Schubert {
17335796c8dcSSimon Schubert errcode = target_read_memory (memaddr++, myaddr++, 1);
17345796c8dcSSimon Schubert }
17355796c8dcSSimon Schubert /* If an error, the last read was unsuccessful, so adjust count. */
17365796c8dcSSimon Schubert if (errcode != 0)
17375796c8dcSSimon Schubert {
17385796c8dcSSimon Schubert nread--;
17395796c8dcSSimon Schubert }
17405796c8dcSSimon Schubert }
17415796c8dcSSimon Schubert if (errnoptr != NULL)
17425796c8dcSSimon Schubert {
17435796c8dcSSimon Schubert *errnoptr = errcode;
17445796c8dcSSimon Schubert }
17455796c8dcSSimon Schubert return (nread);
17465796c8dcSSimon Schubert }
17475796c8dcSSimon Schubert
17485796c8dcSSimon Schubert /* Read a string from the inferior, at ADDR, with LEN characters of WIDTH bytes
17495796c8dcSSimon Schubert each. Fetch at most FETCHLIMIT characters. BUFFER will be set to a newly
17505796c8dcSSimon Schubert allocated buffer containing the string, which the caller is responsible to
17515796c8dcSSimon Schubert free, and BYTES_READ will be set to the number of bytes read. Returns 0 on
17525796c8dcSSimon Schubert success, or errno on failure.
17535796c8dcSSimon Schubert
17545796c8dcSSimon Schubert If LEN > 0, reads exactly LEN characters (including eventual NULs in
17555796c8dcSSimon Schubert the middle or end of the string). If LEN is -1, stops at the first
17565796c8dcSSimon Schubert null character (not necessarily the first null byte) up to a maximum
17575796c8dcSSimon Schubert of FETCHLIMIT characters. Set FETCHLIMIT to UINT_MAX to read as many
17585796c8dcSSimon Schubert characters as possible from the string.
17595796c8dcSSimon Schubert
17605796c8dcSSimon Schubert Unless an exception is thrown, BUFFER will always be allocated, even on
17615796c8dcSSimon Schubert failure. In this case, some characters might have been read before the
17625796c8dcSSimon Schubert failure happened. Check BYTES_READ to recognize this situation.
17635796c8dcSSimon Schubert
17645796c8dcSSimon Schubert Note: There was a FIXME asking to make this code use target_read_string,
17655796c8dcSSimon Schubert but this function is more general (can read past null characters, up to
17665796c8dcSSimon Schubert given LEN). Besides, it is used much more often than target_read_string
17675796c8dcSSimon Schubert so it is more tested. Perhaps callers of target_read_string should use
17685796c8dcSSimon Schubert this function instead? */
17695796c8dcSSimon Schubert
17705796c8dcSSimon Schubert int
read_string(CORE_ADDR addr,int len,int width,unsigned int fetchlimit,enum bfd_endian byte_order,gdb_byte ** buffer,int * bytes_read)17715796c8dcSSimon Schubert read_string (CORE_ADDR addr, int len, int width, unsigned int fetchlimit,
17725796c8dcSSimon Schubert enum bfd_endian byte_order, gdb_byte **buffer, int *bytes_read)
17735796c8dcSSimon Schubert {
17745796c8dcSSimon Schubert int found_nul; /* Non-zero if we found the nul char. */
17755796c8dcSSimon Schubert int errcode; /* Errno returned from bad reads. */
17765796c8dcSSimon Schubert unsigned int nfetch; /* Chars to fetch / chars fetched. */
17775796c8dcSSimon Schubert unsigned int chunksize; /* Size of each fetch, in chars. */
1778c50c785cSJohn Marino gdb_byte *bufptr; /* Pointer to next available byte in
1779c50c785cSJohn Marino buffer. */
17805796c8dcSSimon Schubert gdb_byte *limit; /* First location past end of fetch buffer. */
17815796c8dcSSimon Schubert struct cleanup *old_chain = NULL; /* Top of the old cleanup chain. */
17825796c8dcSSimon Schubert
17835796c8dcSSimon Schubert /* Decide how large of chunks to try to read in one operation. This
17845796c8dcSSimon Schubert is also pretty simple. If LEN >= zero, then we want fetchlimit chars,
17855796c8dcSSimon Schubert so we might as well read them all in one operation. If LEN is -1, we
17865796c8dcSSimon Schubert are looking for a NUL terminator to end the fetching, so we might as
17875796c8dcSSimon Schubert well read in blocks that are large enough to be efficient, but not so
17885796c8dcSSimon Schubert large as to be slow if fetchlimit happens to be large. So we choose the
17895796c8dcSSimon Schubert minimum of 8 and fetchlimit. We used to use 200 instead of 8 but
17905796c8dcSSimon Schubert 200 is way too big for remote debugging over a serial line. */
17915796c8dcSSimon Schubert
17925796c8dcSSimon Schubert chunksize = (len == -1 ? min (8, fetchlimit) : fetchlimit);
17935796c8dcSSimon Schubert
17945796c8dcSSimon Schubert /* Loop until we either have all the characters, or we encounter
17955796c8dcSSimon Schubert some error, such as bumping into the end of the address space. */
17965796c8dcSSimon Schubert
17975796c8dcSSimon Schubert found_nul = 0;
17985796c8dcSSimon Schubert *buffer = NULL;
17995796c8dcSSimon Schubert
18005796c8dcSSimon Schubert old_chain = make_cleanup (free_current_contents, buffer);
18015796c8dcSSimon Schubert
18025796c8dcSSimon Schubert if (len > 0)
18035796c8dcSSimon Schubert {
18045796c8dcSSimon Schubert *buffer = (gdb_byte *) xmalloc (len * width);
18055796c8dcSSimon Schubert bufptr = *buffer;
18065796c8dcSSimon Schubert
18075796c8dcSSimon Schubert nfetch = partial_memory_read (addr, bufptr, len * width, &errcode)
18085796c8dcSSimon Schubert / width;
18095796c8dcSSimon Schubert addr += nfetch * width;
18105796c8dcSSimon Schubert bufptr += nfetch * width;
18115796c8dcSSimon Schubert }
18125796c8dcSSimon Schubert else if (len == -1)
18135796c8dcSSimon Schubert {
18145796c8dcSSimon Schubert unsigned long bufsize = 0;
18155796c8dcSSimon Schubert
18165796c8dcSSimon Schubert do
18175796c8dcSSimon Schubert {
18185796c8dcSSimon Schubert QUIT;
18195796c8dcSSimon Schubert nfetch = min (chunksize, fetchlimit - bufsize);
18205796c8dcSSimon Schubert
18215796c8dcSSimon Schubert if (*buffer == NULL)
18225796c8dcSSimon Schubert *buffer = (gdb_byte *) xmalloc (nfetch * width);
18235796c8dcSSimon Schubert else
18245796c8dcSSimon Schubert *buffer = (gdb_byte *) xrealloc (*buffer,
18255796c8dcSSimon Schubert (nfetch + bufsize) * width);
18265796c8dcSSimon Schubert
18275796c8dcSSimon Schubert bufptr = *buffer + bufsize * width;
18285796c8dcSSimon Schubert bufsize += nfetch;
18295796c8dcSSimon Schubert
18305796c8dcSSimon Schubert /* Read as much as we can. */
18315796c8dcSSimon Schubert nfetch = partial_memory_read (addr, bufptr, nfetch * width, &errcode)
18325796c8dcSSimon Schubert / width;
18335796c8dcSSimon Schubert
18345796c8dcSSimon Schubert /* Scan this chunk for the null character that terminates the string
18355796c8dcSSimon Schubert to print. If found, we don't need to fetch any more. Note
18365796c8dcSSimon Schubert that bufptr is explicitly left pointing at the next character
18375796c8dcSSimon Schubert after the null character, or at the next character after the end
18385796c8dcSSimon Schubert of the buffer. */
18395796c8dcSSimon Schubert
18405796c8dcSSimon Schubert limit = bufptr + nfetch * width;
18415796c8dcSSimon Schubert while (bufptr < limit)
18425796c8dcSSimon Schubert {
18435796c8dcSSimon Schubert unsigned long c;
18445796c8dcSSimon Schubert
18455796c8dcSSimon Schubert c = extract_unsigned_integer (bufptr, width, byte_order);
18465796c8dcSSimon Schubert addr += width;
18475796c8dcSSimon Schubert bufptr += width;
18485796c8dcSSimon Schubert if (c == 0)
18495796c8dcSSimon Schubert {
18505796c8dcSSimon Schubert /* We don't care about any error which happened after
18515796c8dcSSimon Schubert the NUL terminator. */
18525796c8dcSSimon Schubert errcode = 0;
18535796c8dcSSimon Schubert found_nul = 1;
18545796c8dcSSimon Schubert break;
18555796c8dcSSimon Schubert }
18565796c8dcSSimon Schubert }
18575796c8dcSSimon Schubert }
18585796c8dcSSimon Schubert while (errcode == 0 /* no error */
18595796c8dcSSimon Schubert && bufptr - *buffer < fetchlimit * width /* no overrun */
18605796c8dcSSimon Schubert && !found_nul); /* haven't found NUL yet */
18615796c8dcSSimon Schubert }
18625796c8dcSSimon Schubert else
18635796c8dcSSimon Schubert { /* Length of string is really 0! */
18645796c8dcSSimon Schubert /* We always allocate *buffer. */
18655796c8dcSSimon Schubert *buffer = bufptr = xmalloc (1);
18665796c8dcSSimon Schubert errcode = 0;
18675796c8dcSSimon Schubert }
18685796c8dcSSimon Schubert
18695796c8dcSSimon Schubert /* bufptr and addr now point immediately beyond the last byte which we
18705796c8dcSSimon Schubert consider part of the string (including a '\0' which ends the string). */
18715796c8dcSSimon Schubert *bytes_read = bufptr - *buffer;
18725796c8dcSSimon Schubert
18735796c8dcSSimon Schubert QUIT;
18745796c8dcSSimon Schubert
18755796c8dcSSimon Schubert discard_cleanups (old_chain);
18765796c8dcSSimon Schubert
18775796c8dcSSimon Schubert return errcode;
18785796c8dcSSimon Schubert }
18795796c8dcSSimon Schubert
1880a45ae5f8SJohn Marino /* Return true if print_wchar can display W without resorting to a
1881a45ae5f8SJohn Marino numeric escape, false otherwise. */
1882a45ae5f8SJohn Marino
1883a45ae5f8SJohn Marino static int
wchar_printable(gdb_wchar_t w)1884a45ae5f8SJohn Marino wchar_printable (gdb_wchar_t w)
1885a45ae5f8SJohn Marino {
1886a45ae5f8SJohn Marino return (gdb_iswprint (w)
1887a45ae5f8SJohn Marino || w == LCST ('\a') || w == LCST ('\b')
1888a45ae5f8SJohn Marino || w == LCST ('\f') || w == LCST ('\n')
1889a45ae5f8SJohn Marino || w == LCST ('\r') || w == LCST ('\t')
1890a45ae5f8SJohn Marino || w == LCST ('\v'));
1891a45ae5f8SJohn Marino }
1892a45ae5f8SJohn Marino
1893a45ae5f8SJohn Marino /* A helper function that converts the contents of STRING to wide
1894a45ae5f8SJohn Marino characters and then appends them to OUTPUT. */
1895a45ae5f8SJohn Marino
1896a45ae5f8SJohn Marino static void
append_string_as_wide(const char * string,struct obstack * output)1897a45ae5f8SJohn Marino append_string_as_wide (const char *string,
1898a45ae5f8SJohn Marino struct obstack *output)
1899a45ae5f8SJohn Marino {
1900a45ae5f8SJohn Marino for (; *string; ++string)
1901a45ae5f8SJohn Marino {
1902a45ae5f8SJohn Marino gdb_wchar_t w = gdb_btowc (*string);
1903a45ae5f8SJohn Marino obstack_grow (output, &w, sizeof (gdb_wchar_t));
1904a45ae5f8SJohn Marino }
1905a45ae5f8SJohn Marino }
1906a45ae5f8SJohn Marino
1907a45ae5f8SJohn Marino /* Print a wide character W to OUTPUT. ORIG is a pointer to the
1908a45ae5f8SJohn Marino original (target) bytes representing the character, ORIG_LEN is the
1909a45ae5f8SJohn Marino number of valid bytes. WIDTH is the number of bytes in a base
1910a45ae5f8SJohn Marino characters of the type. OUTPUT is an obstack to which wide
1911a45ae5f8SJohn Marino characters are emitted. QUOTER is a (narrow) character indicating
1912a45ae5f8SJohn Marino the style of quotes surrounding the character to be printed.
1913a45ae5f8SJohn Marino NEED_ESCAPE is an in/out flag which is used to track numeric
1914a45ae5f8SJohn Marino escapes across calls. */
1915a45ae5f8SJohn Marino
1916a45ae5f8SJohn Marino static void
print_wchar(gdb_wint_t w,const gdb_byte * orig,int orig_len,int width,enum bfd_endian byte_order,struct obstack * output,int quoter,int * need_escapep)1917a45ae5f8SJohn Marino print_wchar (gdb_wint_t w, const gdb_byte *orig,
1918a45ae5f8SJohn Marino int orig_len, int width,
1919a45ae5f8SJohn Marino enum bfd_endian byte_order,
1920a45ae5f8SJohn Marino struct obstack *output,
1921a45ae5f8SJohn Marino int quoter, int *need_escapep)
1922a45ae5f8SJohn Marino {
1923a45ae5f8SJohn Marino int need_escape = *need_escapep;
1924a45ae5f8SJohn Marino
1925a45ae5f8SJohn Marino *need_escapep = 0;
1926a45ae5f8SJohn Marino if (gdb_iswprint (w) && (!need_escape || (!gdb_iswdigit (w)
1927a45ae5f8SJohn Marino && w != LCST ('8')
1928a45ae5f8SJohn Marino && w != LCST ('9'))))
1929a45ae5f8SJohn Marino {
1930a45ae5f8SJohn Marino gdb_wchar_t wchar = w;
1931a45ae5f8SJohn Marino
1932a45ae5f8SJohn Marino if (w == gdb_btowc (quoter) || w == LCST ('\\'))
1933a45ae5f8SJohn Marino obstack_grow_wstr (output, LCST ("\\"));
1934a45ae5f8SJohn Marino obstack_grow (output, &wchar, sizeof (gdb_wchar_t));
1935a45ae5f8SJohn Marino }
1936a45ae5f8SJohn Marino else
1937a45ae5f8SJohn Marino {
1938a45ae5f8SJohn Marino switch (w)
1939a45ae5f8SJohn Marino {
1940a45ae5f8SJohn Marino case LCST ('\a'):
1941a45ae5f8SJohn Marino obstack_grow_wstr (output, LCST ("\\a"));
1942a45ae5f8SJohn Marino break;
1943a45ae5f8SJohn Marino case LCST ('\b'):
1944a45ae5f8SJohn Marino obstack_grow_wstr (output, LCST ("\\b"));
1945a45ae5f8SJohn Marino break;
1946a45ae5f8SJohn Marino case LCST ('\f'):
1947a45ae5f8SJohn Marino obstack_grow_wstr (output, LCST ("\\f"));
1948a45ae5f8SJohn Marino break;
1949a45ae5f8SJohn Marino case LCST ('\n'):
1950a45ae5f8SJohn Marino obstack_grow_wstr (output, LCST ("\\n"));
1951a45ae5f8SJohn Marino break;
1952a45ae5f8SJohn Marino case LCST ('\r'):
1953a45ae5f8SJohn Marino obstack_grow_wstr (output, LCST ("\\r"));
1954a45ae5f8SJohn Marino break;
1955a45ae5f8SJohn Marino case LCST ('\t'):
1956a45ae5f8SJohn Marino obstack_grow_wstr (output, LCST ("\\t"));
1957a45ae5f8SJohn Marino break;
1958a45ae5f8SJohn Marino case LCST ('\v'):
1959a45ae5f8SJohn Marino obstack_grow_wstr (output, LCST ("\\v"));
1960a45ae5f8SJohn Marino break;
1961a45ae5f8SJohn Marino default:
1962a45ae5f8SJohn Marino {
1963a45ae5f8SJohn Marino int i;
1964a45ae5f8SJohn Marino
1965a45ae5f8SJohn Marino for (i = 0; i + width <= orig_len; i += width)
1966a45ae5f8SJohn Marino {
1967a45ae5f8SJohn Marino char octal[30];
1968a45ae5f8SJohn Marino ULONGEST value;
1969a45ae5f8SJohn Marino
1970a45ae5f8SJohn Marino value = extract_unsigned_integer (&orig[i], width,
1971a45ae5f8SJohn Marino byte_order);
1972a45ae5f8SJohn Marino /* If the value fits in 3 octal digits, print it that
1973a45ae5f8SJohn Marino way. Otherwise, print it as a hex escape. */
1974a45ae5f8SJohn Marino if (value <= 0777)
1975*ef5ccd6cSJohn Marino xsnprintf (octal, sizeof (octal), "\\%.3o",
1976*ef5ccd6cSJohn Marino (int) (value & 0777));
1977a45ae5f8SJohn Marino else
1978*ef5ccd6cSJohn Marino xsnprintf (octal, sizeof (octal), "\\x%lx", (long) value);
1979a45ae5f8SJohn Marino append_string_as_wide (octal, output);
1980a45ae5f8SJohn Marino }
1981a45ae5f8SJohn Marino /* If we somehow have extra bytes, print them now. */
1982a45ae5f8SJohn Marino while (i < orig_len)
1983a45ae5f8SJohn Marino {
1984a45ae5f8SJohn Marino char octal[5];
1985a45ae5f8SJohn Marino
1986*ef5ccd6cSJohn Marino xsnprintf (octal, sizeof (octal), "\\%.3o", orig[i] & 0xff);
1987a45ae5f8SJohn Marino append_string_as_wide (octal, output);
1988a45ae5f8SJohn Marino ++i;
1989a45ae5f8SJohn Marino }
1990a45ae5f8SJohn Marino
1991a45ae5f8SJohn Marino *need_escapep = 1;
1992a45ae5f8SJohn Marino }
1993a45ae5f8SJohn Marino break;
1994a45ae5f8SJohn Marino }
1995a45ae5f8SJohn Marino }
1996a45ae5f8SJohn Marino }
1997a45ae5f8SJohn Marino
1998a45ae5f8SJohn Marino /* Print the character C on STREAM as part of the contents of a
1999a45ae5f8SJohn Marino literal string whose delimiter is QUOTER. ENCODING names the
2000a45ae5f8SJohn Marino encoding of C. */
2001a45ae5f8SJohn Marino
2002a45ae5f8SJohn Marino void
generic_emit_char(int c,struct type * type,struct ui_file * stream,int quoter,const char * encoding)2003a45ae5f8SJohn Marino generic_emit_char (int c, struct type *type, struct ui_file *stream,
2004a45ae5f8SJohn Marino int quoter, const char *encoding)
2005a45ae5f8SJohn Marino {
2006a45ae5f8SJohn Marino enum bfd_endian byte_order
2007a45ae5f8SJohn Marino = gdbarch_byte_order (get_type_arch (type));
2008a45ae5f8SJohn Marino struct obstack wchar_buf, output;
2009a45ae5f8SJohn Marino struct cleanup *cleanups;
2010a45ae5f8SJohn Marino gdb_byte *buf;
2011a45ae5f8SJohn Marino struct wchar_iterator *iter;
2012a45ae5f8SJohn Marino int need_escape = 0;
2013a45ae5f8SJohn Marino
2014a45ae5f8SJohn Marino buf = alloca (TYPE_LENGTH (type));
2015a45ae5f8SJohn Marino pack_long (buf, type, c);
2016a45ae5f8SJohn Marino
2017a45ae5f8SJohn Marino iter = make_wchar_iterator (buf, TYPE_LENGTH (type),
2018a45ae5f8SJohn Marino encoding, TYPE_LENGTH (type));
2019a45ae5f8SJohn Marino cleanups = make_cleanup_wchar_iterator (iter);
2020a45ae5f8SJohn Marino
2021a45ae5f8SJohn Marino /* This holds the printable form of the wchar_t data. */
2022a45ae5f8SJohn Marino obstack_init (&wchar_buf);
2023a45ae5f8SJohn Marino make_cleanup_obstack_free (&wchar_buf);
2024a45ae5f8SJohn Marino
2025a45ae5f8SJohn Marino while (1)
2026a45ae5f8SJohn Marino {
2027a45ae5f8SJohn Marino int num_chars;
2028a45ae5f8SJohn Marino gdb_wchar_t *chars;
2029a45ae5f8SJohn Marino const gdb_byte *buf;
2030a45ae5f8SJohn Marino size_t buflen;
2031a45ae5f8SJohn Marino int print_escape = 1;
2032a45ae5f8SJohn Marino enum wchar_iterate_result result;
2033a45ae5f8SJohn Marino
2034a45ae5f8SJohn Marino num_chars = wchar_iterate (iter, &result, &chars, &buf, &buflen);
2035a45ae5f8SJohn Marino if (num_chars < 0)
2036a45ae5f8SJohn Marino break;
2037a45ae5f8SJohn Marino if (num_chars > 0)
2038a45ae5f8SJohn Marino {
2039a45ae5f8SJohn Marino /* If all characters are printable, print them. Otherwise,
2040a45ae5f8SJohn Marino we're going to have to print an escape sequence. We
2041a45ae5f8SJohn Marino check all characters because we want to print the target
2042a45ae5f8SJohn Marino bytes in the escape sequence, and we don't know character
2043a45ae5f8SJohn Marino boundaries there. */
2044a45ae5f8SJohn Marino int i;
2045a45ae5f8SJohn Marino
2046a45ae5f8SJohn Marino print_escape = 0;
2047a45ae5f8SJohn Marino for (i = 0; i < num_chars; ++i)
2048a45ae5f8SJohn Marino if (!wchar_printable (chars[i]))
2049a45ae5f8SJohn Marino {
2050a45ae5f8SJohn Marino print_escape = 1;
2051a45ae5f8SJohn Marino break;
2052a45ae5f8SJohn Marino }
2053a45ae5f8SJohn Marino
2054a45ae5f8SJohn Marino if (!print_escape)
2055a45ae5f8SJohn Marino {
2056a45ae5f8SJohn Marino for (i = 0; i < num_chars; ++i)
2057a45ae5f8SJohn Marino print_wchar (chars[i], buf, buflen,
2058a45ae5f8SJohn Marino TYPE_LENGTH (type), byte_order,
2059a45ae5f8SJohn Marino &wchar_buf, quoter, &need_escape);
2060a45ae5f8SJohn Marino }
2061a45ae5f8SJohn Marino }
2062a45ae5f8SJohn Marino
2063a45ae5f8SJohn Marino /* This handles the NUM_CHARS == 0 case as well. */
2064a45ae5f8SJohn Marino if (print_escape)
2065a45ae5f8SJohn Marino print_wchar (gdb_WEOF, buf, buflen, TYPE_LENGTH (type),
2066a45ae5f8SJohn Marino byte_order, &wchar_buf, quoter, &need_escape);
2067a45ae5f8SJohn Marino }
2068a45ae5f8SJohn Marino
2069a45ae5f8SJohn Marino /* The output in the host encoding. */
2070a45ae5f8SJohn Marino obstack_init (&output);
2071a45ae5f8SJohn Marino make_cleanup_obstack_free (&output);
2072a45ae5f8SJohn Marino
2073a45ae5f8SJohn Marino convert_between_encodings (INTERMEDIATE_ENCODING, host_charset (),
2074*ef5ccd6cSJohn Marino (gdb_byte *) obstack_base (&wchar_buf),
2075a45ae5f8SJohn Marino obstack_object_size (&wchar_buf),
2076*ef5ccd6cSJohn Marino sizeof (gdb_wchar_t), &output, translit_char);
2077a45ae5f8SJohn Marino obstack_1grow (&output, '\0');
2078a45ae5f8SJohn Marino
2079a45ae5f8SJohn Marino fputs_filtered (obstack_base (&output), stream);
2080a45ae5f8SJohn Marino
2081a45ae5f8SJohn Marino do_cleanups (cleanups);
2082a45ae5f8SJohn Marino }
2083a45ae5f8SJohn Marino
2084*ef5ccd6cSJohn Marino /* Return the repeat count of the next character/byte in ITER,
2085*ef5ccd6cSJohn Marino storing the result in VEC. */
2086*ef5ccd6cSJohn Marino
2087*ef5ccd6cSJohn Marino static int
count_next_character(struct wchar_iterator * iter,VEC (converted_character_d)** vec)2088*ef5ccd6cSJohn Marino count_next_character (struct wchar_iterator *iter,
2089*ef5ccd6cSJohn Marino VEC (converted_character_d) **vec)
2090*ef5ccd6cSJohn Marino {
2091*ef5ccd6cSJohn Marino struct converted_character *current;
2092*ef5ccd6cSJohn Marino
2093*ef5ccd6cSJohn Marino if (VEC_empty (converted_character_d, *vec))
2094*ef5ccd6cSJohn Marino {
2095*ef5ccd6cSJohn Marino struct converted_character tmp;
2096*ef5ccd6cSJohn Marino gdb_wchar_t *chars;
2097*ef5ccd6cSJohn Marino
2098*ef5ccd6cSJohn Marino tmp.num_chars
2099*ef5ccd6cSJohn Marino = wchar_iterate (iter, &tmp.result, &chars, &tmp.buf, &tmp.buflen);
2100*ef5ccd6cSJohn Marino if (tmp.num_chars > 0)
2101*ef5ccd6cSJohn Marino {
2102*ef5ccd6cSJohn Marino gdb_assert (tmp.num_chars < MAX_WCHARS);
2103*ef5ccd6cSJohn Marino memcpy (tmp.chars, chars, tmp.num_chars * sizeof (gdb_wchar_t));
2104*ef5ccd6cSJohn Marino }
2105*ef5ccd6cSJohn Marino VEC_safe_push (converted_character_d, *vec, &tmp);
2106*ef5ccd6cSJohn Marino }
2107*ef5ccd6cSJohn Marino
2108*ef5ccd6cSJohn Marino current = VEC_last (converted_character_d, *vec);
2109*ef5ccd6cSJohn Marino
2110*ef5ccd6cSJohn Marino /* Count repeated characters or bytes. */
2111*ef5ccd6cSJohn Marino current->repeat_count = 1;
2112*ef5ccd6cSJohn Marino if (current->num_chars == -1)
2113*ef5ccd6cSJohn Marino {
2114*ef5ccd6cSJohn Marino /* EOF */
2115*ef5ccd6cSJohn Marino return -1;
2116*ef5ccd6cSJohn Marino }
2117*ef5ccd6cSJohn Marino else
2118*ef5ccd6cSJohn Marino {
2119*ef5ccd6cSJohn Marino gdb_wchar_t *chars;
2120*ef5ccd6cSJohn Marino struct converted_character d;
2121*ef5ccd6cSJohn Marino int repeat;
2122*ef5ccd6cSJohn Marino
2123*ef5ccd6cSJohn Marino d.repeat_count = 0;
2124*ef5ccd6cSJohn Marino
2125*ef5ccd6cSJohn Marino while (1)
2126*ef5ccd6cSJohn Marino {
2127*ef5ccd6cSJohn Marino /* Get the next character. */
2128*ef5ccd6cSJohn Marino d.num_chars
2129*ef5ccd6cSJohn Marino = wchar_iterate (iter, &d.result, &chars, &d.buf, &d.buflen);
2130*ef5ccd6cSJohn Marino
2131*ef5ccd6cSJohn Marino /* If a character was successfully converted, save the character
2132*ef5ccd6cSJohn Marino into the converted character. */
2133*ef5ccd6cSJohn Marino if (d.num_chars > 0)
2134*ef5ccd6cSJohn Marino {
2135*ef5ccd6cSJohn Marino gdb_assert (d.num_chars < MAX_WCHARS);
2136*ef5ccd6cSJohn Marino memcpy (d.chars, chars, WCHAR_BUFLEN (d.num_chars));
2137*ef5ccd6cSJohn Marino }
2138*ef5ccd6cSJohn Marino
2139*ef5ccd6cSJohn Marino /* Determine if the current character is the same as this
2140*ef5ccd6cSJohn Marino new character. */
2141*ef5ccd6cSJohn Marino if (d.num_chars == current->num_chars && d.result == current->result)
2142*ef5ccd6cSJohn Marino {
2143*ef5ccd6cSJohn Marino /* There are two cases to consider:
2144*ef5ccd6cSJohn Marino
2145*ef5ccd6cSJohn Marino 1) Equality of converted character (num_chars > 0)
2146*ef5ccd6cSJohn Marino 2) Equality of non-converted character (num_chars == 0) */
2147*ef5ccd6cSJohn Marino if ((current->num_chars > 0
2148*ef5ccd6cSJohn Marino && memcmp (current->chars, d.chars,
2149*ef5ccd6cSJohn Marino WCHAR_BUFLEN (current->num_chars)) == 0)
2150*ef5ccd6cSJohn Marino || (current->num_chars == 0
2151*ef5ccd6cSJohn Marino && current->buflen == d.buflen
2152*ef5ccd6cSJohn Marino && memcmp (current->buf, d.buf, current->buflen) == 0))
2153*ef5ccd6cSJohn Marino ++current->repeat_count;
2154*ef5ccd6cSJohn Marino else
2155*ef5ccd6cSJohn Marino break;
2156*ef5ccd6cSJohn Marino }
2157*ef5ccd6cSJohn Marino else
2158*ef5ccd6cSJohn Marino break;
2159*ef5ccd6cSJohn Marino }
2160*ef5ccd6cSJohn Marino
2161*ef5ccd6cSJohn Marino /* Push this next converted character onto the result vector. */
2162*ef5ccd6cSJohn Marino repeat = current->repeat_count;
2163*ef5ccd6cSJohn Marino VEC_safe_push (converted_character_d, *vec, &d);
2164*ef5ccd6cSJohn Marino return repeat;
2165*ef5ccd6cSJohn Marino }
2166*ef5ccd6cSJohn Marino }
2167*ef5ccd6cSJohn Marino
2168*ef5ccd6cSJohn Marino /* Print the characters in CHARS to the OBSTACK. QUOTE_CHAR is the quote
2169*ef5ccd6cSJohn Marino character to use with string output. WIDTH is the size of the output
2170*ef5ccd6cSJohn Marino character type. BYTE_ORDER is the the target byte order. OPTIONS
2171*ef5ccd6cSJohn Marino is the user's print options. */
2172*ef5ccd6cSJohn Marino
2173*ef5ccd6cSJohn Marino static void
print_converted_chars_to_obstack(struct obstack * obstack,VEC (converted_character_d)* chars,int quote_char,int width,enum bfd_endian byte_order,const struct value_print_options * options)2174*ef5ccd6cSJohn Marino print_converted_chars_to_obstack (struct obstack *obstack,
2175*ef5ccd6cSJohn Marino VEC (converted_character_d) *chars,
2176*ef5ccd6cSJohn Marino int quote_char, int width,
2177*ef5ccd6cSJohn Marino enum bfd_endian byte_order,
2178*ef5ccd6cSJohn Marino const struct value_print_options *options)
2179*ef5ccd6cSJohn Marino {
2180*ef5ccd6cSJohn Marino unsigned int idx;
2181*ef5ccd6cSJohn Marino struct converted_character *elem;
2182*ef5ccd6cSJohn Marino enum {START, SINGLE, REPEAT, INCOMPLETE, FINISH} state, last;
2183*ef5ccd6cSJohn Marino gdb_wchar_t wide_quote_char = gdb_btowc (quote_char);
2184*ef5ccd6cSJohn Marino int need_escape = 0;
2185*ef5ccd6cSJohn Marino
2186*ef5ccd6cSJohn Marino /* Set the start state. */
2187*ef5ccd6cSJohn Marino idx = 0;
2188*ef5ccd6cSJohn Marino last = state = START;
2189*ef5ccd6cSJohn Marino elem = NULL;
2190*ef5ccd6cSJohn Marino
2191*ef5ccd6cSJohn Marino while (1)
2192*ef5ccd6cSJohn Marino {
2193*ef5ccd6cSJohn Marino switch (state)
2194*ef5ccd6cSJohn Marino {
2195*ef5ccd6cSJohn Marino case START:
2196*ef5ccd6cSJohn Marino /* Nothing to do. */
2197*ef5ccd6cSJohn Marino break;
2198*ef5ccd6cSJohn Marino
2199*ef5ccd6cSJohn Marino case SINGLE:
2200*ef5ccd6cSJohn Marino {
2201*ef5ccd6cSJohn Marino int j;
2202*ef5ccd6cSJohn Marino
2203*ef5ccd6cSJohn Marino /* We are outputting a single character
2204*ef5ccd6cSJohn Marino (< options->repeat_count_threshold). */
2205*ef5ccd6cSJohn Marino
2206*ef5ccd6cSJohn Marino if (last != SINGLE)
2207*ef5ccd6cSJohn Marino {
2208*ef5ccd6cSJohn Marino /* We were outputting some other type of content, so we
2209*ef5ccd6cSJohn Marino must output and a comma and a quote. */
2210*ef5ccd6cSJohn Marino if (last != START)
2211*ef5ccd6cSJohn Marino obstack_grow_wstr (obstack, LCST (", "));
2212*ef5ccd6cSJohn Marino obstack_grow (obstack, &wide_quote_char, sizeof (gdb_wchar_t));
2213*ef5ccd6cSJohn Marino }
2214*ef5ccd6cSJohn Marino /* Output the character. */
2215*ef5ccd6cSJohn Marino for (j = 0; j < elem->repeat_count; ++j)
2216*ef5ccd6cSJohn Marino {
2217*ef5ccd6cSJohn Marino if (elem->result == wchar_iterate_ok)
2218*ef5ccd6cSJohn Marino print_wchar (elem->chars[0], elem->buf, elem->buflen, width,
2219*ef5ccd6cSJohn Marino byte_order, obstack, quote_char, &need_escape);
2220*ef5ccd6cSJohn Marino else
2221*ef5ccd6cSJohn Marino print_wchar (gdb_WEOF, elem->buf, elem->buflen, width,
2222*ef5ccd6cSJohn Marino byte_order, obstack, quote_char, &need_escape);
2223*ef5ccd6cSJohn Marino }
2224*ef5ccd6cSJohn Marino }
2225*ef5ccd6cSJohn Marino break;
2226*ef5ccd6cSJohn Marino
2227*ef5ccd6cSJohn Marino case REPEAT:
2228*ef5ccd6cSJohn Marino {
2229*ef5ccd6cSJohn Marino int j;
2230*ef5ccd6cSJohn Marino char *s;
2231*ef5ccd6cSJohn Marino
2232*ef5ccd6cSJohn Marino /* We are outputting a character with a repeat count
2233*ef5ccd6cSJohn Marino greater than options->repeat_count_threshold. */
2234*ef5ccd6cSJohn Marino
2235*ef5ccd6cSJohn Marino if (last == SINGLE)
2236*ef5ccd6cSJohn Marino {
2237*ef5ccd6cSJohn Marino /* We were outputting a single string. Terminate the
2238*ef5ccd6cSJohn Marino string. */
2239*ef5ccd6cSJohn Marino obstack_grow (obstack, &wide_quote_char, sizeof (gdb_wchar_t));
2240*ef5ccd6cSJohn Marino }
2241*ef5ccd6cSJohn Marino if (last != START)
2242*ef5ccd6cSJohn Marino obstack_grow_wstr (obstack, LCST (", "));
2243*ef5ccd6cSJohn Marino
2244*ef5ccd6cSJohn Marino /* Output the character and repeat string. */
2245*ef5ccd6cSJohn Marino obstack_grow_wstr (obstack, LCST ("'"));
2246*ef5ccd6cSJohn Marino if (elem->result == wchar_iterate_ok)
2247*ef5ccd6cSJohn Marino print_wchar (elem->chars[0], elem->buf, elem->buflen, width,
2248*ef5ccd6cSJohn Marino byte_order, obstack, quote_char, &need_escape);
2249*ef5ccd6cSJohn Marino else
2250*ef5ccd6cSJohn Marino print_wchar (gdb_WEOF, elem->buf, elem->buflen, width,
2251*ef5ccd6cSJohn Marino byte_order, obstack, quote_char, &need_escape);
2252*ef5ccd6cSJohn Marino obstack_grow_wstr (obstack, LCST ("'"));
2253*ef5ccd6cSJohn Marino s = xstrprintf (_(" <repeats %u times>"), elem->repeat_count);
2254*ef5ccd6cSJohn Marino for (j = 0; s[j]; ++j)
2255*ef5ccd6cSJohn Marino {
2256*ef5ccd6cSJohn Marino gdb_wchar_t w = gdb_btowc (s[j]);
2257*ef5ccd6cSJohn Marino obstack_grow (obstack, &w, sizeof (gdb_wchar_t));
2258*ef5ccd6cSJohn Marino }
2259*ef5ccd6cSJohn Marino xfree (s);
2260*ef5ccd6cSJohn Marino }
2261*ef5ccd6cSJohn Marino break;
2262*ef5ccd6cSJohn Marino
2263*ef5ccd6cSJohn Marino case INCOMPLETE:
2264*ef5ccd6cSJohn Marino /* We are outputting an incomplete sequence. */
2265*ef5ccd6cSJohn Marino if (last == SINGLE)
2266*ef5ccd6cSJohn Marino {
2267*ef5ccd6cSJohn Marino /* If we were outputting a string of SINGLE characters,
2268*ef5ccd6cSJohn Marino terminate the quote. */
2269*ef5ccd6cSJohn Marino obstack_grow (obstack, &wide_quote_char, sizeof (gdb_wchar_t));
2270*ef5ccd6cSJohn Marino }
2271*ef5ccd6cSJohn Marino if (last != START)
2272*ef5ccd6cSJohn Marino obstack_grow_wstr (obstack, LCST (", "));
2273*ef5ccd6cSJohn Marino
2274*ef5ccd6cSJohn Marino /* Output the incomplete sequence string. */
2275*ef5ccd6cSJohn Marino obstack_grow_wstr (obstack, LCST ("<incomplete sequence "));
2276*ef5ccd6cSJohn Marino print_wchar (gdb_WEOF, elem->buf, elem->buflen, width, byte_order,
2277*ef5ccd6cSJohn Marino obstack, 0, &need_escape);
2278*ef5ccd6cSJohn Marino obstack_grow_wstr (obstack, LCST (">"));
2279*ef5ccd6cSJohn Marino
2280*ef5ccd6cSJohn Marino /* We do not attempt to outupt anything after this. */
2281*ef5ccd6cSJohn Marino state = FINISH;
2282*ef5ccd6cSJohn Marino break;
2283*ef5ccd6cSJohn Marino
2284*ef5ccd6cSJohn Marino case FINISH:
2285*ef5ccd6cSJohn Marino /* All done. If we were outputting a string of SINGLE
2286*ef5ccd6cSJohn Marino characters, the string must be terminated. Otherwise,
2287*ef5ccd6cSJohn Marino REPEAT and INCOMPLETE are always left properly terminated. */
2288*ef5ccd6cSJohn Marino if (last == SINGLE)
2289*ef5ccd6cSJohn Marino obstack_grow (obstack, &wide_quote_char, sizeof (gdb_wchar_t));
2290*ef5ccd6cSJohn Marino
2291*ef5ccd6cSJohn Marino return;
2292*ef5ccd6cSJohn Marino }
2293*ef5ccd6cSJohn Marino
2294*ef5ccd6cSJohn Marino /* Get the next element and state. */
2295*ef5ccd6cSJohn Marino last = state;
2296*ef5ccd6cSJohn Marino if (state != FINISH)
2297*ef5ccd6cSJohn Marino {
2298*ef5ccd6cSJohn Marino elem = VEC_index (converted_character_d, chars, idx++);
2299*ef5ccd6cSJohn Marino switch (elem->result)
2300*ef5ccd6cSJohn Marino {
2301*ef5ccd6cSJohn Marino case wchar_iterate_ok:
2302*ef5ccd6cSJohn Marino case wchar_iterate_invalid:
2303*ef5ccd6cSJohn Marino if (elem->repeat_count > options->repeat_count_threshold)
2304*ef5ccd6cSJohn Marino state = REPEAT;
2305*ef5ccd6cSJohn Marino else
2306*ef5ccd6cSJohn Marino state = SINGLE;
2307*ef5ccd6cSJohn Marino break;
2308*ef5ccd6cSJohn Marino
2309*ef5ccd6cSJohn Marino case wchar_iterate_incomplete:
2310*ef5ccd6cSJohn Marino state = INCOMPLETE;
2311*ef5ccd6cSJohn Marino break;
2312*ef5ccd6cSJohn Marino
2313*ef5ccd6cSJohn Marino case wchar_iterate_eof:
2314*ef5ccd6cSJohn Marino state = FINISH;
2315*ef5ccd6cSJohn Marino break;
2316*ef5ccd6cSJohn Marino }
2317*ef5ccd6cSJohn Marino }
2318*ef5ccd6cSJohn Marino }
2319*ef5ccd6cSJohn Marino }
2320*ef5ccd6cSJohn Marino
2321a45ae5f8SJohn Marino /* Print the character string STRING, printing at most LENGTH
2322a45ae5f8SJohn Marino characters. LENGTH is -1 if the string is nul terminated. TYPE is
2323a45ae5f8SJohn Marino the type of each character. OPTIONS holds the printing options;
2324a45ae5f8SJohn Marino printing stops early if the number hits print_max; repeat counts
2325a45ae5f8SJohn Marino are printed as appropriate. Print ellipses at the end if we had to
2326a45ae5f8SJohn Marino stop before printing LENGTH characters, or if FORCE_ELLIPSES.
2327a45ae5f8SJohn Marino QUOTE_CHAR is the character to print at each end of the string. If
2328a45ae5f8SJohn Marino C_STYLE_TERMINATOR is true, and the last character is 0, then it is
2329a45ae5f8SJohn Marino omitted. */
2330a45ae5f8SJohn Marino
2331a45ae5f8SJohn Marino void
generic_printstr(struct ui_file * stream,struct type * type,const gdb_byte * string,unsigned int length,const char * encoding,int force_ellipses,int quote_char,int c_style_terminator,const struct value_print_options * options)2332a45ae5f8SJohn Marino generic_printstr (struct ui_file *stream, struct type *type,
2333a45ae5f8SJohn Marino const gdb_byte *string, unsigned int length,
2334a45ae5f8SJohn Marino const char *encoding, int force_ellipses,
2335a45ae5f8SJohn Marino int quote_char, int c_style_terminator,
2336a45ae5f8SJohn Marino const struct value_print_options *options)
2337a45ae5f8SJohn Marino {
2338a45ae5f8SJohn Marino enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
2339a45ae5f8SJohn Marino unsigned int i;
2340a45ae5f8SJohn Marino int width = TYPE_LENGTH (type);
2341a45ae5f8SJohn Marino struct obstack wchar_buf, output;
2342a45ae5f8SJohn Marino struct cleanup *cleanup;
2343a45ae5f8SJohn Marino struct wchar_iterator *iter;
2344a45ae5f8SJohn Marino int finished = 0;
2345*ef5ccd6cSJohn Marino struct converted_character *last;
2346*ef5ccd6cSJohn Marino VEC (converted_character_d) *converted_chars;
2347a45ae5f8SJohn Marino
2348a45ae5f8SJohn Marino if (length == -1)
2349a45ae5f8SJohn Marino {
2350a45ae5f8SJohn Marino unsigned long current_char = 1;
2351a45ae5f8SJohn Marino
2352a45ae5f8SJohn Marino for (i = 0; current_char; ++i)
2353a45ae5f8SJohn Marino {
2354a45ae5f8SJohn Marino QUIT;
2355a45ae5f8SJohn Marino current_char = extract_unsigned_integer (string + i * width,
2356a45ae5f8SJohn Marino width, byte_order);
2357a45ae5f8SJohn Marino }
2358a45ae5f8SJohn Marino length = i;
2359a45ae5f8SJohn Marino }
2360a45ae5f8SJohn Marino
2361a45ae5f8SJohn Marino /* If the string was not truncated due to `set print elements', and
2362a45ae5f8SJohn Marino the last byte of it is a null, we don't print that, in
2363a45ae5f8SJohn Marino traditional C style. */
2364a45ae5f8SJohn Marino if (c_style_terminator
2365a45ae5f8SJohn Marino && !force_ellipses
2366a45ae5f8SJohn Marino && length > 0
2367a45ae5f8SJohn Marino && (extract_unsigned_integer (string + (length - 1) * width,
2368a45ae5f8SJohn Marino width, byte_order) == 0))
2369a45ae5f8SJohn Marino length--;
2370a45ae5f8SJohn Marino
2371a45ae5f8SJohn Marino if (length == 0)
2372a45ae5f8SJohn Marino {
2373a45ae5f8SJohn Marino fputs_filtered ("\"\"", stream);
2374a45ae5f8SJohn Marino return;
2375a45ae5f8SJohn Marino }
2376a45ae5f8SJohn Marino
2377a45ae5f8SJohn Marino /* Arrange to iterate over the characters, in wchar_t form. */
2378a45ae5f8SJohn Marino iter = make_wchar_iterator (string, length * width, encoding, width);
2379a45ae5f8SJohn Marino cleanup = make_cleanup_wchar_iterator (iter);
2380*ef5ccd6cSJohn Marino converted_chars = NULL;
2381*ef5ccd6cSJohn Marino make_cleanup (VEC_cleanup (converted_character_d), &converted_chars);
2382*ef5ccd6cSJohn Marino
2383*ef5ccd6cSJohn Marino /* Convert characters until the string is over or the maximum
2384*ef5ccd6cSJohn Marino number of printed characters has been reached. */
2385*ef5ccd6cSJohn Marino i = 0;
2386*ef5ccd6cSJohn Marino while (i < options->print_max)
2387*ef5ccd6cSJohn Marino {
2388*ef5ccd6cSJohn Marino int r;
2389*ef5ccd6cSJohn Marino
2390*ef5ccd6cSJohn Marino QUIT;
2391*ef5ccd6cSJohn Marino
2392*ef5ccd6cSJohn Marino /* Grab the next character and repeat count. */
2393*ef5ccd6cSJohn Marino r = count_next_character (iter, &converted_chars);
2394*ef5ccd6cSJohn Marino
2395*ef5ccd6cSJohn Marino /* If less than zero, the end of the input string was reached. */
2396*ef5ccd6cSJohn Marino if (r < 0)
2397*ef5ccd6cSJohn Marino break;
2398*ef5ccd6cSJohn Marino
2399*ef5ccd6cSJohn Marino /* Otherwise, add the count to the total print count and get
2400*ef5ccd6cSJohn Marino the next character. */
2401*ef5ccd6cSJohn Marino i += r;
2402*ef5ccd6cSJohn Marino }
2403*ef5ccd6cSJohn Marino
2404*ef5ccd6cSJohn Marino /* Get the last element and determine if the entire string was
2405*ef5ccd6cSJohn Marino processed. */
2406*ef5ccd6cSJohn Marino last = VEC_last (converted_character_d, converted_chars);
2407*ef5ccd6cSJohn Marino finished = (last->result == wchar_iterate_eof);
2408*ef5ccd6cSJohn Marino
2409*ef5ccd6cSJohn Marino /* Ensure that CONVERTED_CHARS is terminated. */
2410*ef5ccd6cSJohn Marino last->result = wchar_iterate_eof;
2411a45ae5f8SJohn Marino
2412a45ae5f8SJohn Marino /* WCHAR_BUF is the obstack we use to represent the string in
2413a45ae5f8SJohn Marino wchar_t form. */
2414a45ae5f8SJohn Marino obstack_init (&wchar_buf);
2415a45ae5f8SJohn Marino make_cleanup_obstack_free (&wchar_buf);
2416a45ae5f8SJohn Marino
2417*ef5ccd6cSJohn Marino /* Print the output string to the obstack. */
2418*ef5ccd6cSJohn Marino print_converted_chars_to_obstack (&wchar_buf, converted_chars, quote_char,
2419*ef5ccd6cSJohn Marino width, byte_order, options);
2420a45ae5f8SJohn Marino
2421a45ae5f8SJohn Marino if (force_ellipses || !finished)
2422a45ae5f8SJohn Marino obstack_grow_wstr (&wchar_buf, LCST ("..."));
2423a45ae5f8SJohn Marino
2424a45ae5f8SJohn Marino /* OUTPUT is where we collect `char's for printing. */
2425a45ae5f8SJohn Marino obstack_init (&output);
2426a45ae5f8SJohn Marino make_cleanup_obstack_free (&output);
2427a45ae5f8SJohn Marino
2428a45ae5f8SJohn Marino convert_between_encodings (INTERMEDIATE_ENCODING, host_charset (),
2429*ef5ccd6cSJohn Marino (gdb_byte *) obstack_base (&wchar_buf),
2430a45ae5f8SJohn Marino obstack_object_size (&wchar_buf),
2431*ef5ccd6cSJohn Marino sizeof (gdb_wchar_t), &output, translit_char);
2432a45ae5f8SJohn Marino obstack_1grow (&output, '\0');
2433a45ae5f8SJohn Marino
2434a45ae5f8SJohn Marino fputs_filtered (obstack_base (&output), stream);
2435a45ae5f8SJohn Marino
2436a45ae5f8SJohn Marino do_cleanups (cleanup);
2437a45ae5f8SJohn Marino }
2438a45ae5f8SJohn Marino
24395796c8dcSSimon Schubert /* Print a string from the inferior, starting at ADDR and printing up to LEN
24405796c8dcSSimon Schubert characters, of WIDTH bytes a piece, to STREAM. If LEN is -1, printing
24415796c8dcSSimon Schubert stops at the first null byte, otherwise printing proceeds (including null
24425796c8dcSSimon Schubert bytes) until either print_max or LEN characters have been printed,
2443c50c785cSJohn Marino whichever is smaller. ENCODING is the name of the string's
2444c50c785cSJohn Marino encoding. It can be NULL, in which case the target encoding is
2445c50c785cSJohn Marino assumed. */
24465796c8dcSSimon Schubert
24475796c8dcSSimon Schubert int
val_print_string(struct type * elttype,const char * encoding,CORE_ADDR addr,int len,struct ui_file * stream,const struct value_print_options * options)2448c50c785cSJohn Marino val_print_string (struct type *elttype, const char *encoding,
2449c50c785cSJohn Marino CORE_ADDR addr, int len,
24505796c8dcSSimon Schubert struct ui_file *stream,
24515796c8dcSSimon Schubert const struct value_print_options *options)
24525796c8dcSSimon Schubert {
24535796c8dcSSimon Schubert int force_ellipsis = 0; /* Force ellipsis to be printed if nonzero. */
24545796c8dcSSimon Schubert int errcode; /* Errno returned from bad reads. */
2455c50c785cSJohn Marino int found_nul; /* Non-zero if we found the nul char. */
24565796c8dcSSimon Schubert unsigned int fetchlimit; /* Maximum number of chars to print. */
24575796c8dcSSimon Schubert int bytes_read;
24585796c8dcSSimon Schubert gdb_byte *buffer = NULL; /* Dynamically growable fetch buffer. */
24595796c8dcSSimon Schubert struct cleanup *old_chain = NULL; /* Top of the old cleanup chain. */
24605796c8dcSSimon Schubert struct gdbarch *gdbarch = get_type_arch (elttype);
24615796c8dcSSimon Schubert enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
24625796c8dcSSimon Schubert int width = TYPE_LENGTH (elttype);
24635796c8dcSSimon Schubert
24645796c8dcSSimon Schubert /* First we need to figure out the limit on the number of characters we are
24655796c8dcSSimon Schubert going to attempt to fetch and print. This is actually pretty simple. If
24665796c8dcSSimon Schubert LEN >= zero, then the limit is the minimum of LEN and print_max. If
24675796c8dcSSimon Schubert LEN is -1, then the limit is print_max. This is true regardless of
24685796c8dcSSimon Schubert whether print_max is zero, UINT_MAX (unlimited), or something in between,
24695796c8dcSSimon Schubert because finding the null byte (or available memory) is what actually
24705796c8dcSSimon Schubert limits the fetch. */
24715796c8dcSSimon Schubert
2472c50c785cSJohn Marino fetchlimit = (len == -1 ? options->print_max : min (len,
2473c50c785cSJohn Marino options->print_max));
24745796c8dcSSimon Schubert
24755796c8dcSSimon Schubert errcode = read_string (addr, len, width, fetchlimit, byte_order,
24765796c8dcSSimon Schubert &buffer, &bytes_read);
24775796c8dcSSimon Schubert old_chain = make_cleanup (xfree, buffer);
24785796c8dcSSimon Schubert
24795796c8dcSSimon Schubert addr += bytes_read;
24805796c8dcSSimon Schubert
2481c50c785cSJohn Marino /* We now have either successfully filled the buffer to fetchlimit,
2482c50c785cSJohn Marino or terminated early due to an error or finding a null char when
2483c50c785cSJohn Marino LEN is -1. */
24845796c8dcSSimon Schubert
24855796c8dcSSimon Schubert /* Determine found_nul by looking at the last character read. */
24865796c8dcSSimon Schubert found_nul = extract_unsigned_integer (buffer + bytes_read - width, width,
24875796c8dcSSimon Schubert byte_order) == 0;
24885796c8dcSSimon Schubert if (len == -1 && !found_nul)
24895796c8dcSSimon Schubert {
24905796c8dcSSimon Schubert gdb_byte *peekbuf;
24915796c8dcSSimon Schubert
24925796c8dcSSimon Schubert /* We didn't find a NUL terminator we were looking for. Attempt
24935796c8dcSSimon Schubert to peek at the next character. If not successful, or it is not
24945796c8dcSSimon Schubert a null byte, then force ellipsis to be printed. */
24955796c8dcSSimon Schubert
24965796c8dcSSimon Schubert peekbuf = (gdb_byte *) alloca (width);
24975796c8dcSSimon Schubert
24985796c8dcSSimon Schubert if (target_read_memory (addr, peekbuf, width) == 0
24995796c8dcSSimon Schubert && extract_unsigned_integer (peekbuf, width, byte_order) != 0)
25005796c8dcSSimon Schubert force_ellipsis = 1;
25015796c8dcSSimon Schubert }
25025796c8dcSSimon Schubert else if ((len >= 0 && errcode != 0) || (len > bytes_read / width))
25035796c8dcSSimon Schubert {
25045796c8dcSSimon Schubert /* Getting an error when we have a requested length, or fetching less
25055796c8dcSSimon Schubert than the number of characters actually requested, always make us
25065796c8dcSSimon Schubert print ellipsis. */
25075796c8dcSSimon Schubert force_ellipsis = 1;
25085796c8dcSSimon Schubert }
25095796c8dcSSimon Schubert
25105796c8dcSSimon Schubert /* If we get an error before fetching anything, don't print a string.
25115796c8dcSSimon Schubert But if we fetch something and then get an error, print the string
25125796c8dcSSimon Schubert and then the error message. */
25135796c8dcSSimon Schubert if (errcode == 0 || bytes_read > 0)
25145796c8dcSSimon Schubert {
2515cf7f2e2dSJohn Marino LA_PRINT_STRING (stream, elttype, buffer, bytes_read / width,
2516c50c785cSJohn Marino encoding, force_ellipsis, options);
25175796c8dcSSimon Schubert }
25185796c8dcSSimon Schubert
25195796c8dcSSimon Schubert if (errcode != 0)
25205796c8dcSSimon Schubert {
25215796c8dcSSimon Schubert if (errcode == EIO)
25225796c8dcSSimon Schubert {
25235796c8dcSSimon Schubert fprintf_filtered (stream, "<Address ");
25245796c8dcSSimon Schubert fputs_filtered (paddress (gdbarch, addr), stream);
25255796c8dcSSimon Schubert fprintf_filtered (stream, " out of bounds>");
25265796c8dcSSimon Schubert }
25275796c8dcSSimon Schubert else
25285796c8dcSSimon Schubert {
25295796c8dcSSimon Schubert fprintf_filtered (stream, "<Error reading address ");
25305796c8dcSSimon Schubert fputs_filtered (paddress (gdbarch, addr), stream);
25315796c8dcSSimon Schubert fprintf_filtered (stream, ": %s>", safe_strerror (errcode));
25325796c8dcSSimon Schubert }
25335796c8dcSSimon Schubert }
25345796c8dcSSimon Schubert
25355796c8dcSSimon Schubert gdb_flush (stream);
25365796c8dcSSimon Schubert do_cleanups (old_chain);
25375796c8dcSSimon Schubert
25385796c8dcSSimon Schubert return (bytes_read / width);
25395796c8dcSSimon Schubert }
25405796c8dcSSimon Schubert
25415796c8dcSSimon Schubert
25425796c8dcSSimon Schubert /* The 'set input-radix' command writes to this auxiliary variable.
25435796c8dcSSimon Schubert If the requested radix is valid, INPUT_RADIX is updated; otherwise,
25445796c8dcSSimon Schubert it is left unchanged. */
25455796c8dcSSimon Schubert
25465796c8dcSSimon Schubert static unsigned input_radix_1 = 10;
25475796c8dcSSimon Schubert
25485796c8dcSSimon Schubert /* Validate an input or output radix setting, and make sure the user
25495796c8dcSSimon Schubert knows what they really did here. Radix setting is confusing, e.g.
25505796c8dcSSimon Schubert setting the input radix to "10" never changes it! */
25515796c8dcSSimon Schubert
25525796c8dcSSimon Schubert static void
set_input_radix(char * args,int from_tty,struct cmd_list_element * c)25535796c8dcSSimon Schubert set_input_radix (char *args, int from_tty, struct cmd_list_element *c)
25545796c8dcSSimon Schubert {
25555796c8dcSSimon Schubert set_input_radix_1 (from_tty, input_radix_1);
25565796c8dcSSimon Schubert }
25575796c8dcSSimon Schubert
25585796c8dcSSimon Schubert static void
set_input_radix_1(int from_tty,unsigned radix)25595796c8dcSSimon Schubert set_input_radix_1 (int from_tty, unsigned radix)
25605796c8dcSSimon Schubert {
25615796c8dcSSimon Schubert /* We don't currently disallow any input radix except 0 or 1, which don't
25625796c8dcSSimon Schubert make any mathematical sense. In theory, we can deal with any input
25635796c8dcSSimon Schubert radix greater than 1, even if we don't have unique digits for every
25645796c8dcSSimon Schubert value from 0 to radix-1, but in practice we lose on large radix values.
25655796c8dcSSimon Schubert We should either fix the lossage or restrict the radix range more.
25665796c8dcSSimon Schubert (FIXME). */
25675796c8dcSSimon Schubert
25685796c8dcSSimon Schubert if (radix < 2)
25695796c8dcSSimon Schubert {
25705796c8dcSSimon Schubert input_radix_1 = input_radix;
25715796c8dcSSimon Schubert error (_("Nonsense input radix ``decimal %u''; input radix unchanged."),
25725796c8dcSSimon Schubert radix);
25735796c8dcSSimon Schubert }
25745796c8dcSSimon Schubert input_radix_1 = input_radix = radix;
25755796c8dcSSimon Schubert if (from_tty)
25765796c8dcSSimon Schubert {
2577c50c785cSJohn Marino printf_filtered (_("Input radix now set to "
2578c50c785cSJohn Marino "decimal %u, hex %x, octal %o.\n"),
25795796c8dcSSimon Schubert radix, radix, radix);
25805796c8dcSSimon Schubert }
25815796c8dcSSimon Schubert }
25825796c8dcSSimon Schubert
25835796c8dcSSimon Schubert /* The 'set output-radix' command writes to this auxiliary variable.
25845796c8dcSSimon Schubert If the requested radix is valid, OUTPUT_RADIX is updated,
25855796c8dcSSimon Schubert otherwise, it is left unchanged. */
25865796c8dcSSimon Schubert
25875796c8dcSSimon Schubert static unsigned output_radix_1 = 10;
25885796c8dcSSimon Schubert
25895796c8dcSSimon Schubert static void
set_output_radix(char * args,int from_tty,struct cmd_list_element * c)25905796c8dcSSimon Schubert set_output_radix (char *args, int from_tty, struct cmd_list_element *c)
25915796c8dcSSimon Schubert {
25925796c8dcSSimon Schubert set_output_radix_1 (from_tty, output_radix_1);
25935796c8dcSSimon Schubert }
25945796c8dcSSimon Schubert
25955796c8dcSSimon Schubert static void
set_output_radix_1(int from_tty,unsigned radix)25965796c8dcSSimon Schubert set_output_radix_1 (int from_tty, unsigned radix)
25975796c8dcSSimon Schubert {
25985796c8dcSSimon Schubert /* Validate the radix and disallow ones that we aren't prepared to
25995796c8dcSSimon Schubert handle correctly, leaving the radix unchanged. */
26005796c8dcSSimon Schubert switch (radix)
26015796c8dcSSimon Schubert {
26025796c8dcSSimon Schubert case 16:
26035796c8dcSSimon Schubert user_print_options.output_format = 'x'; /* hex */
26045796c8dcSSimon Schubert break;
26055796c8dcSSimon Schubert case 10:
26065796c8dcSSimon Schubert user_print_options.output_format = 0; /* decimal */
26075796c8dcSSimon Schubert break;
26085796c8dcSSimon Schubert case 8:
26095796c8dcSSimon Schubert user_print_options.output_format = 'o'; /* octal */
26105796c8dcSSimon Schubert break;
26115796c8dcSSimon Schubert default:
26125796c8dcSSimon Schubert output_radix_1 = output_radix;
2613c50c785cSJohn Marino error (_("Unsupported output radix ``decimal %u''; "
2614c50c785cSJohn Marino "output radix unchanged."),
26155796c8dcSSimon Schubert radix);
26165796c8dcSSimon Schubert }
26175796c8dcSSimon Schubert output_radix_1 = output_radix = radix;
26185796c8dcSSimon Schubert if (from_tty)
26195796c8dcSSimon Schubert {
2620c50c785cSJohn Marino printf_filtered (_("Output radix now set to "
2621c50c785cSJohn Marino "decimal %u, hex %x, octal %o.\n"),
26225796c8dcSSimon Schubert radix, radix, radix);
26235796c8dcSSimon Schubert }
26245796c8dcSSimon Schubert }
26255796c8dcSSimon Schubert
26265796c8dcSSimon Schubert /* Set both the input and output radix at once. Try to set the output radix
26275796c8dcSSimon Schubert first, since it has the most restrictive range. An radix that is valid as
26285796c8dcSSimon Schubert an output radix is also valid as an input radix.
26295796c8dcSSimon Schubert
26305796c8dcSSimon Schubert It may be useful to have an unusual input radix. If the user wishes to
26315796c8dcSSimon Schubert set an input radix that is not valid as an output radix, he needs to use
26325796c8dcSSimon Schubert the 'set input-radix' command. */
26335796c8dcSSimon Schubert
26345796c8dcSSimon Schubert static void
set_radix(char * arg,int from_tty)26355796c8dcSSimon Schubert set_radix (char *arg, int from_tty)
26365796c8dcSSimon Schubert {
26375796c8dcSSimon Schubert unsigned radix;
26385796c8dcSSimon Schubert
26395796c8dcSSimon Schubert radix = (arg == NULL) ? 10 : parse_and_eval_long (arg);
26405796c8dcSSimon Schubert set_output_radix_1 (0, radix);
26415796c8dcSSimon Schubert set_input_radix_1 (0, radix);
26425796c8dcSSimon Schubert if (from_tty)
26435796c8dcSSimon Schubert {
2644c50c785cSJohn Marino printf_filtered (_("Input and output radices now set to "
2645c50c785cSJohn Marino "decimal %u, hex %x, octal %o.\n"),
26465796c8dcSSimon Schubert radix, radix, radix);
26475796c8dcSSimon Schubert }
26485796c8dcSSimon Schubert }
26495796c8dcSSimon Schubert
26505796c8dcSSimon Schubert /* Show both the input and output radices. */
26515796c8dcSSimon Schubert
26525796c8dcSSimon Schubert static void
show_radix(char * arg,int from_tty)26535796c8dcSSimon Schubert show_radix (char *arg, int from_tty)
26545796c8dcSSimon Schubert {
26555796c8dcSSimon Schubert if (from_tty)
26565796c8dcSSimon Schubert {
26575796c8dcSSimon Schubert if (input_radix == output_radix)
26585796c8dcSSimon Schubert {
2659c50c785cSJohn Marino printf_filtered (_("Input and output radices set to "
2660c50c785cSJohn Marino "decimal %u, hex %x, octal %o.\n"),
26615796c8dcSSimon Schubert input_radix, input_radix, input_radix);
26625796c8dcSSimon Schubert }
26635796c8dcSSimon Schubert else
26645796c8dcSSimon Schubert {
2665c50c785cSJohn Marino printf_filtered (_("Input radix set to decimal "
2666c50c785cSJohn Marino "%u, hex %x, octal %o.\n"),
26675796c8dcSSimon Schubert input_radix, input_radix, input_radix);
2668c50c785cSJohn Marino printf_filtered (_("Output radix set to decimal "
2669c50c785cSJohn Marino "%u, hex %x, octal %o.\n"),
26705796c8dcSSimon Schubert output_radix, output_radix, output_radix);
26715796c8dcSSimon Schubert }
26725796c8dcSSimon Schubert }
26735796c8dcSSimon Schubert }
26745796c8dcSSimon Schubert
26755796c8dcSSimon Schubert
26765796c8dcSSimon Schubert static void
set_print(char * arg,int from_tty)26775796c8dcSSimon Schubert set_print (char *arg, int from_tty)
26785796c8dcSSimon Schubert {
26795796c8dcSSimon Schubert printf_unfiltered (
26805796c8dcSSimon Schubert "\"set print\" must be followed by the name of a print subcommand.\n");
26815796c8dcSSimon Schubert help_list (setprintlist, "set print ", -1, gdb_stdout);
26825796c8dcSSimon Schubert }
26835796c8dcSSimon Schubert
26845796c8dcSSimon Schubert static void
show_print(char * args,int from_tty)26855796c8dcSSimon Schubert show_print (char *args, int from_tty)
26865796c8dcSSimon Schubert {
26875796c8dcSSimon Schubert cmd_show_list (showprintlist, from_tty, "");
26885796c8dcSSimon Schubert }
26895796c8dcSSimon Schubert
26905796c8dcSSimon Schubert void
_initialize_valprint(void)26915796c8dcSSimon Schubert _initialize_valprint (void)
26925796c8dcSSimon Schubert {
26935796c8dcSSimon Schubert add_prefix_cmd ("print", no_class, set_print,
26945796c8dcSSimon Schubert _("Generic command for setting how things print."),
26955796c8dcSSimon Schubert &setprintlist, "set print ", 0, &setlist);
26965796c8dcSSimon Schubert add_alias_cmd ("p", "print", no_class, 1, &setlist);
2697c50c785cSJohn Marino /* Prefer set print to set prompt. */
26985796c8dcSSimon Schubert add_alias_cmd ("pr", "print", no_class, 1, &setlist);
26995796c8dcSSimon Schubert
27005796c8dcSSimon Schubert add_prefix_cmd ("print", no_class, show_print,
27015796c8dcSSimon Schubert _("Generic command for showing print settings."),
27025796c8dcSSimon Schubert &showprintlist, "show print ", 0, &showlist);
27035796c8dcSSimon Schubert add_alias_cmd ("p", "print", no_class, 1, &showlist);
27045796c8dcSSimon Schubert add_alias_cmd ("pr", "print", no_class, 1, &showlist);
27055796c8dcSSimon Schubert
27065796c8dcSSimon Schubert add_setshow_uinteger_cmd ("elements", no_class,
27075796c8dcSSimon Schubert &user_print_options.print_max, _("\
27085796c8dcSSimon Schubert Set limit on string chars or array elements to print."), _("\
27095796c8dcSSimon Schubert Show limit on string chars or array elements to print."), _("\
27105796c8dcSSimon Schubert \"set print elements 0\" causes there to be no limit."),
27115796c8dcSSimon Schubert NULL,
27125796c8dcSSimon Schubert show_print_max,
27135796c8dcSSimon Schubert &setprintlist, &showprintlist);
27145796c8dcSSimon Schubert
27155796c8dcSSimon Schubert add_setshow_boolean_cmd ("null-stop", no_class,
27165796c8dcSSimon Schubert &user_print_options.stop_print_at_null, _("\
27175796c8dcSSimon Schubert Set printing of char arrays to stop at first null char."), _("\
27185796c8dcSSimon Schubert Show printing of char arrays to stop at first null char."), NULL,
27195796c8dcSSimon Schubert NULL,
27205796c8dcSSimon Schubert show_stop_print_at_null,
27215796c8dcSSimon Schubert &setprintlist, &showprintlist);
27225796c8dcSSimon Schubert
27235796c8dcSSimon Schubert add_setshow_uinteger_cmd ("repeats", no_class,
27245796c8dcSSimon Schubert &user_print_options.repeat_count_threshold, _("\
27255796c8dcSSimon Schubert Set threshold for repeated print elements."), _("\
27265796c8dcSSimon Schubert Show threshold for repeated print elements."), _("\
27275796c8dcSSimon Schubert \"set print repeats 0\" causes all elements to be individually printed."),
27285796c8dcSSimon Schubert NULL,
27295796c8dcSSimon Schubert show_repeat_count_threshold,
27305796c8dcSSimon Schubert &setprintlist, &showprintlist);
27315796c8dcSSimon Schubert
27325796c8dcSSimon Schubert add_setshow_boolean_cmd ("pretty", class_support,
27335796c8dcSSimon Schubert &user_print_options.prettyprint_structs, _("\
27345796c8dcSSimon Schubert Set prettyprinting of structures."), _("\
27355796c8dcSSimon Schubert Show prettyprinting of structures."), NULL,
27365796c8dcSSimon Schubert NULL,
27375796c8dcSSimon Schubert show_prettyprint_structs,
27385796c8dcSSimon Schubert &setprintlist, &showprintlist);
27395796c8dcSSimon Schubert
27405796c8dcSSimon Schubert add_setshow_boolean_cmd ("union", class_support,
27415796c8dcSSimon Schubert &user_print_options.unionprint, _("\
27425796c8dcSSimon Schubert Set printing of unions interior to structures."), _("\
27435796c8dcSSimon Schubert Show printing of unions interior to structures."), NULL,
27445796c8dcSSimon Schubert NULL,
27455796c8dcSSimon Schubert show_unionprint,
27465796c8dcSSimon Schubert &setprintlist, &showprintlist);
27475796c8dcSSimon Schubert
27485796c8dcSSimon Schubert add_setshow_boolean_cmd ("array", class_support,
27495796c8dcSSimon Schubert &user_print_options.prettyprint_arrays, _("\
27505796c8dcSSimon Schubert Set prettyprinting of arrays."), _("\
27515796c8dcSSimon Schubert Show prettyprinting of arrays."), NULL,
27525796c8dcSSimon Schubert NULL,
27535796c8dcSSimon Schubert show_prettyprint_arrays,
27545796c8dcSSimon Schubert &setprintlist, &showprintlist);
27555796c8dcSSimon Schubert
27565796c8dcSSimon Schubert add_setshow_boolean_cmd ("address", class_support,
27575796c8dcSSimon Schubert &user_print_options.addressprint, _("\
27585796c8dcSSimon Schubert Set printing of addresses."), _("\
27595796c8dcSSimon Schubert Show printing of addresses."), NULL,
27605796c8dcSSimon Schubert NULL,
27615796c8dcSSimon Schubert show_addressprint,
27625796c8dcSSimon Schubert &setprintlist, &showprintlist);
27635796c8dcSSimon Schubert
2764*ef5ccd6cSJohn Marino add_setshow_boolean_cmd ("symbol", class_support,
2765*ef5ccd6cSJohn Marino &user_print_options.symbol_print, _("\
2766*ef5ccd6cSJohn Marino Set printing of symbol names when printing pointers."), _("\
2767*ef5ccd6cSJohn Marino Show printing of symbol names when printing pointers."),
2768*ef5ccd6cSJohn Marino NULL, NULL,
2769*ef5ccd6cSJohn Marino show_symbol_print,
2770*ef5ccd6cSJohn Marino &setprintlist, &showprintlist);
2771*ef5ccd6cSJohn Marino
27725796c8dcSSimon Schubert add_setshow_zuinteger_cmd ("input-radix", class_support, &input_radix_1,
27735796c8dcSSimon Schubert _("\
27745796c8dcSSimon Schubert Set default input radix for entering numbers."), _("\
27755796c8dcSSimon Schubert Show default input radix for entering numbers."), NULL,
27765796c8dcSSimon Schubert set_input_radix,
27775796c8dcSSimon Schubert show_input_radix,
27785796c8dcSSimon Schubert &setlist, &showlist);
27795796c8dcSSimon Schubert
27805796c8dcSSimon Schubert add_setshow_zuinteger_cmd ("output-radix", class_support, &output_radix_1,
27815796c8dcSSimon Schubert _("\
27825796c8dcSSimon Schubert Set default output radix for printing of values."), _("\
27835796c8dcSSimon Schubert Show default output radix for printing of values."), NULL,
27845796c8dcSSimon Schubert set_output_radix,
27855796c8dcSSimon Schubert show_output_radix,
27865796c8dcSSimon Schubert &setlist, &showlist);
27875796c8dcSSimon Schubert
27885796c8dcSSimon Schubert /* The "set radix" and "show radix" commands are special in that
27895796c8dcSSimon Schubert they are like normal set and show commands but allow two normally
27905796c8dcSSimon Schubert independent variables to be either set or shown with a single
27915796c8dcSSimon Schubert command. So the usual deprecated_add_set_cmd() and [deleted]
27925796c8dcSSimon Schubert add_show_from_set() commands aren't really appropriate. */
27935796c8dcSSimon Schubert /* FIXME: i18n: With the new add_setshow_integer command, that is no
27945796c8dcSSimon Schubert longer true - show can display anything. */
27955796c8dcSSimon Schubert add_cmd ("radix", class_support, set_radix, _("\
27965796c8dcSSimon Schubert Set default input and output number radices.\n\
27975796c8dcSSimon Schubert Use 'set input-radix' or 'set output-radix' to independently set each.\n\
27985796c8dcSSimon Schubert Without an argument, sets both radices back to the default value of 10."),
27995796c8dcSSimon Schubert &setlist);
28005796c8dcSSimon Schubert add_cmd ("radix", class_support, show_radix, _("\
28015796c8dcSSimon Schubert Show the default input and output number radices.\n\
28025796c8dcSSimon Schubert Use 'show input-radix' or 'show output-radix' to independently show each."),
28035796c8dcSSimon Schubert &showlist);
28045796c8dcSSimon Schubert
28055796c8dcSSimon Schubert add_setshow_boolean_cmd ("array-indexes", class_support,
28065796c8dcSSimon Schubert &user_print_options.print_array_indexes, _("\
28075796c8dcSSimon Schubert Set printing of array indexes."), _("\
28085796c8dcSSimon Schubert Show printing of array indexes"), NULL, NULL, show_print_array_indexes,
28095796c8dcSSimon Schubert &setprintlist, &showprintlist);
28105796c8dcSSimon Schubert }
2811