xref: /dflybsd-src/contrib/gdb-7/gdb/valprint.c (revision cf7f2e2d389e8012d562650bd94d7e433f449d6e)
15796c8dcSSimon Schubert /* Print values for GDB, the GNU debugger.
25796c8dcSSimon Schubert 
35796c8dcSSimon Schubert    Copyright (C) 1986, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
45796c8dcSSimon Schubert    1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
5*cf7f2e2dSJohn Marino    2009, 2010 Free Software Foundation, Inc.
65796c8dcSSimon Schubert 
75796c8dcSSimon Schubert    This file is part of GDB.
85796c8dcSSimon Schubert 
95796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
105796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
115796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
125796c8dcSSimon Schubert    (at your option) any later version.
135796c8dcSSimon Schubert 
145796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
155796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
165796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
175796c8dcSSimon Schubert    GNU General Public License for more details.
185796c8dcSSimon Schubert 
195796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
205796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
215796c8dcSSimon Schubert 
225796c8dcSSimon Schubert #include "defs.h"
235796c8dcSSimon Schubert #include "gdb_string.h"
245796c8dcSSimon Schubert #include "symtab.h"
255796c8dcSSimon Schubert #include "gdbtypes.h"
265796c8dcSSimon Schubert #include "value.h"
275796c8dcSSimon Schubert #include "gdbcore.h"
285796c8dcSSimon Schubert #include "gdbcmd.h"
295796c8dcSSimon Schubert #include "target.h"
305796c8dcSSimon Schubert #include "language.h"
315796c8dcSSimon Schubert #include "annotate.h"
325796c8dcSSimon Schubert #include "valprint.h"
335796c8dcSSimon Schubert #include "floatformat.h"
345796c8dcSSimon Schubert #include "doublest.h"
355796c8dcSSimon Schubert #include "exceptions.h"
365796c8dcSSimon Schubert #include "dfp.h"
375796c8dcSSimon Schubert #include "python/python.h"
38*cf7f2e2dSJohn Marino #include "ada-lang.h"
395796c8dcSSimon Schubert 
405796c8dcSSimon Schubert #include <errno.h>
415796c8dcSSimon Schubert 
425796c8dcSSimon Schubert /* Prototypes for local functions */
435796c8dcSSimon Schubert 
445796c8dcSSimon Schubert static int partial_memory_read (CORE_ADDR memaddr, gdb_byte *myaddr,
455796c8dcSSimon Schubert 				int len, int *errnoptr);
465796c8dcSSimon Schubert 
475796c8dcSSimon Schubert static void show_print (char *, int);
485796c8dcSSimon Schubert 
495796c8dcSSimon Schubert static void set_print (char *, int);
505796c8dcSSimon Schubert 
515796c8dcSSimon Schubert static void set_radix (char *, int);
525796c8dcSSimon Schubert 
535796c8dcSSimon Schubert static void show_radix (char *, int);
545796c8dcSSimon Schubert 
555796c8dcSSimon Schubert static void set_input_radix (char *, int, struct cmd_list_element *);
565796c8dcSSimon Schubert 
575796c8dcSSimon Schubert static void set_input_radix_1 (int, unsigned);
585796c8dcSSimon Schubert 
595796c8dcSSimon Schubert static void set_output_radix (char *, int, struct cmd_list_element *);
605796c8dcSSimon Schubert 
615796c8dcSSimon Schubert static void set_output_radix_1 (int, unsigned);
625796c8dcSSimon Schubert 
635796c8dcSSimon Schubert void _initialize_valprint (void);
645796c8dcSSimon Schubert 
655796c8dcSSimon Schubert #define PRINT_MAX_DEFAULT 200	/* Start print_max off at this value. */
665796c8dcSSimon Schubert 
675796c8dcSSimon Schubert struct value_print_options user_print_options =
685796c8dcSSimon Schubert {
695796c8dcSSimon Schubert   Val_pretty_default,		/* pretty */
705796c8dcSSimon Schubert   0,				/* prettyprint_arrays */
715796c8dcSSimon Schubert   0,				/* prettyprint_structs */
725796c8dcSSimon Schubert   0,				/* vtblprint */
735796c8dcSSimon Schubert   1,				/* unionprint */
745796c8dcSSimon Schubert   1,				/* addressprint */
755796c8dcSSimon Schubert   0,				/* objectprint */
765796c8dcSSimon Schubert   PRINT_MAX_DEFAULT,		/* print_max */
775796c8dcSSimon Schubert   10,				/* repeat_count_threshold */
785796c8dcSSimon Schubert   0,				/* output_format */
795796c8dcSSimon Schubert   0,				/* format */
805796c8dcSSimon Schubert   0,				/* stop_print_at_null */
815796c8dcSSimon Schubert   0,				/* inspect_it */
825796c8dcSSimon Schubert   0,				/* print_array_indexes */
835796c8dcSSimon Schubert   0,				/* deref_ref */
845796c8dcSSimon Schubert   1,				/* static_field_print */
855796c8dcSSimon Schubert   1,				/* pascal_static_field_print */
865796c8dcSSimon Schubert   0,				/* raw */
875796c8dcSSimon Schubert   0				/* summary */
885796c8dcSSimon Schubert };
895796c8dcSSimon Schubert 
905796c8dcSSimon Schubert /* Initialize *OPTS to be a copy of the user print options.  */
915796c8dcSSimon Schubert void
925796c8dcSSimon Schubert get_user_print_options (struct value_print_options *opts)
935796c8dcSSimon Schubert {
945796c8dcSSimon Schubert   *opts = user_print_options;
955796c8dcSSimon Schubert }
965796c8dcSSimon Schubert 
975796c8dcSSimon Schubert /* Initialize *OPTS to be a copy of the user print options, but with
985796c8dcSSimon Schubert    pretty-printing disabled.  */
995796c8dcSSimon Schubert void
1005796c8dcSSimon Schubert get_raw_print_options (struct value_print_options *opts)
1015796c8dcSSimon Schubert {
1025796c8dcSSimon Schubert   *opts = user_print_options;
1035796c8dcSSimon Schubert   opts->pretty = Val_no_prettyprint;
1045796c8dcSSimon Schubert }
1055796c8dcSSimon Schubert 
1065796c8dcSSimon Schubert /* Initialize *OPTS to be a copy of the user print options, but using
1075796c8dcSSimon Schubert    FORMAT as the formatting option.  */
1085796c8dcSSimon Schubert void
1095796c8dcSSimon Schubert get_formatted_print_options (struct value_print_options *opts,
1105796c8dcSSimon Schubert 			     char format)
1115796c8dcSSimon Schubert {
1125796c8dcSSimon Schubert   *opts = user_print_options;
1135796c8dcSSimon Schubert   opts->format = format;
1145796c8dcSSimon Schubert }
1155796c8dcSSimon Schubert 
1165796c8dcSSimon Schubert static void
1175796c8dcSSimon Schubert show_print_max (struct ui_file *file, int from_tty,
1185796c8dcSSimon Schubert 		struct cmd_list_element *c, const char *value)
1195796c8dcSSimon Schubert {
1205796c8dcSSimon Schubert   fprintf_filtered (file, _("\
1215796c8dcSSimon Schubert Limit on string chars or array elements to print is %s.\n"),
1225796c8dcSSimon Schubert 		    value);
1235796c8dcSSimon Schubert }
1245796c8dcSSimon Schubert 
1255796c8dcSSimon Schubert 
1265796c8dcSSimon Schubert /* Default input and output radixes, and output format letter.  */
1275796c8dcSSimon Schubert 
1285796c8dcSSimon Schubert unsigned input_radix = 10;
1295796c8dcSSimon Schubert static void
1305796c8dcSSimon Schubert show_input_radix (struct ui_file *file, int from_tty,
1315796c8dcSSimon Schubert 		  struct cmd_list_element *c, const char *value)
1325796c8dcSSimon Schubert {
1335796c8dcSSimon Schubert   fprintf_filtered (file, _("\
1345796c8dcSSimon Schubert Default input radix for entering numbers is %s.\n"),
1355796c8dcSSimon Schubert 		    value);
1365796c8dcSSimon Schubert }
1375796c8dcSSimon Schubert 
1385796c8dcSSimon Schubert unsigned output_radix = 10;
1395796c8dcSSimon Schubert static void
1405796c8dcSSimon Schubert show_output_radix (struct ui_file *file, int from_tty,
1415796c8dcSSimon Schubert 		   struct cmd_list_element *c, const char *value)
1425796c8dcSSimon Schubert {
1435796c8dcSSimon Schubert   fprintf_filtered (file, _("\
1445796c8dcSSimon Schubert Default output radix for printing of values is %s.\n"),
1455796c8dcSSimon Schubert 		    value);
1465796c8dcSSimon Schubert }
1475796c8dcSSimon Schubert 
1485796c8dcSSimon Schubert /* By default we print arrays without printing the index of each element in
1495796c8dcSSimon Schubert    the array.  This behavior can be changed by setting PRINT_ARRAY_INDEXES.  */
1505796c8dcSSimon Schubert 
1515796c8dcSSimon Schubert static void
1525796c8dcSSimon Schubert show_print_array_indexes (struct ui_file *file, int from_tty,
1535796c8dcSSimon Schubert 		          struct cmd_list_element *c, const char *value)
1545796c8dcSSimon Schubert {
1555796c8dcSSimon Schubert   fprintf_filtered (file, _("Printing of array indexes is %s.\n"), value);
1565796c8dcSSimon Schubert }
1575796c8dcSSimon Schubert 
1585796c8dcSSimon Schubert /* Print repeat counts if there are more than this many repetitions of an
1595796c8dcSSimon Schubert    element in an array.  Referenced by the low level language dependent
1605796c8dcSSimon Schubert    print routines. */
1615796c8dcSSimon Schubert 
1625796c8dcSSimon Schubert static void
1635796c8dcSSimon Schubert show_repeat_count_threshold (struct ui_file *file, int from_tty,
1645796c8dcSSimon Schubert 			     struct cmd_list_element *c, const char *value)
1655796c8dcSSimon Schubert {
1665796c8dcSSimon Schubert   fprintf_filtered (file, _("Threshold for repeated print elements is %s.\n"),
1675796c8dcSSimon Schubert 		    value);
1685796c8dcSSimon Schubert }
1695796c8dcSSimon Schubert 
1705796c8dcSSimon Schubert /* If nonzero, stops printing of char arrays at first null. */
1715796c8dcSSimon Schubert 
1725796c8dcSSimon Schubert static void
1735796c8dcSSimon Schubert show_stop_print_at_null (struct ui_file *file, int from_tty,
1745796c8dcSSimon Schubert 			 struct cmd_list_element *c, const char *value)
1755796c8dcSSimon Schubert {
1765796c8dcSSimon Schubert   fprintf_filtered (file, _("\
1775796c8dcSSimon Schubert Printing of char arrays to stop at first null char is %s.\n"),
1785796c8dcSSimon Schubert 		    value);
1795796c8dcSSimon Schubert }
1805796c8dcSSimon Schubert 
1815796c8dcSSimon Schubert /* Controls pretty printing of structures. */
1825796c8dcSSimon Schubert 
1835796c8dcSSimon Schubert static void
1845796c8dcSSimon Schubert show_prettyprint_structs (struct ui_file *file, int from_tty,
1855796c8dcSSimon Schubert 			  struct cmd_list_element *c, const char *value)
1865796c8dcSSimon Schubert {
1875796c8dcSSimon Schubert   fprintf_filtered (file, _("Prettyprinting of structures is %s.\n"), value);
1885796c8dcSSimon Schubert }
1895796c8dcSSimon Schubert 
1905796c8dcSSimon Schubert /* Controls pretty printing of arrays.  */
1915796c8dcSSimon Schubert 
1925796c8dcSSimon Schubert static void
1935796c8dcSSimon Schubert show_prettyprint_arrays (struct ui_file *file, int from_tty,
1945796c8dcSSimon Schubert 			 struct cmd_list_element *c, const char *value)
1955796c8dcSSimon Schubert {
1965796c8dcSSimon Schubert   fprintf_filtered (file, _("Prettyprinting of arrays is %s.\n"), value);
1975796c8dcSSimon Schubert }
1985796c8dcSSimon Schubert 
1995796c8dcSSimon Schubert /* If nonzero, causes unions inside structures or other unions to be
2005796c8dcSSimon Schubert    printed. */
2015796c8dcSSimon Schubert 
2025796c8dcSSimon Schubert static void
2035796c8dcSSimon Schubert show_unionprint (struct ui_file *file, int from_tty,
2045796c8dcSSimon Schubert 		 struct cmd_list_element *c, const char *value)
2055796c8dcSSimon Schubert {
2065796c8dcSSimon Schubert   fprintf_filtered (file, _("\
2075796c8dcSSimon Schubert Printing of unions interior to structures is %s.\n"),
2085796c8dcSSimon Schubert 		    value);
2095796c8dcSSimon Schubert }
2105796c8dcSSimon Schubert 
2115796c8dcSSimon Schubert /* If nonzero, causes machine addresses to be printed in certain contexts. */
2125796c8dcSSimon Schubert 
2135796c8dcSSimon Schubert static void
2145796c8dcSSimon Schubert show_addressprint (struct ui_file *file, int from_tty,
2155796c8dcSSimon Schubert 		   struct cmd_list_element *c, const char *value)
2165796c8dcSSimon Schubert {
2175796c8dcSSimon Schubert   fprintf_filtered (file, _("Printing of addresses is %s.\n"), value);
2185796c8dcSSimon Schubert }
2195796c8dcSSimon Schubert 
2205796c8dcSSimon Schubert 
2215796c8dcSSimon Schubert /* A helper function for val_print.  When printing in "summary" mode,
2225796c8dcSSimon Schubert    we want to print scalar arguments, but not aggregate arguments.
2235796c8dcSSimon Schubert    This function distinguishes between the two.  */
2245796c8dcSSimon Schubert 
2255796c8dcSSimon Schubert static int
2265796c8dcSSimon Schubert scalar_type_p (struct type *type)
2275796c8dcSSimon Schubert {
2285796c8dcSSimon Schubert   CHECK_TYPEDEF (type);
2295796c8dcSSimon Schubert   while (TYPE_CODE (type) == TYPE_CODE_REF)
2305796c8dcSSimon Schubert     {
2315796c8dcSSimon Schubert       type = TYPE_TARGET_TYPE (type);
2325796c8dcSSimon Schubert       CHECK_TYPEDEF (type);
2335796c8dcSSimon Schubert     }
2345796c8dcSSimon Schubert   switch (TYPE_CODE (type))
2355796c8dcSSimon Schubert     {
2365796c8dcSSimon Schubert     case TYPE_CODE_ARRAY:
2375796c8dcSSimon Schubert     case TYPE_CODE_STRUCT:
2385796c8dcSSimon Schubert     case TYPE_CODE_UNION:
2395796c8dcSSimon Schubert     case TYPE_CODE_SET:
2405796c8dcSSimon Schubert     case TYPE_CODE_STRING:
2415796c8dcSSimon Schubert     case TYPE_CODE_BITSTRING:
2425796c8dcSSimon Schubert       return 0;
2435796c8dcSSimon Schubert     default:
2445796c8dcSSimon Schubert       return 1;
2455796c8dcSSimon Schubert     }
2465796c8dcSSimon Schubert }
2475796c8dcSSimon Schubert 
248*cf7f2e2dSJohn Marino /* Helper function to check the validity of some bits of a value.
249*cf7f2e2dSJohn Marino 
250*cf7f2e2dSJohn Marino    If TYPE represents some aggregate type (e.g., a structure), return 1.
251*cf7f2e2dSJohn Marino 
252*cf7f2e2dSJohn Marino    Otherwise, any of the bytes starting at OFFSET and extending for
253*cf7f2e2dSJohn Marino    TYPE_LENGTH(TYPE) bytes are invalid, print a message to STREAM and
254*cf7f2e2dSJohn Marino    return 0.  The checking is done using FUNCS.
255*cf7f2e2dSJohn Marino 
256*cf7f2e2dSJohn Marino    Otherwise, return 1.  */
257*cf7f2e2dSJohn Marino 
258*cf7f2e2dSJohn Marino static int
259*cf7f2e2dSJohn Marino valprint_check_validity (struct ui_file *stream,
260*cf7f2e2dSJohn Marino 			 struct type *type,
261*cf7f2e2dSJohn Marino 			 int offset,
262*cf7f2e2dSJohn Marino 			 const struct value *val)
263*cf7f2e2dSJohn Marino {
264*cf7f2e2dSJohn Marino   CHECK_TYPEDEF (type);
265*cf7f2e2dSJohn Marino 
266*cf7f2e2dSJohn Marino   if (TYPE_CODE (type) != TYPE_CODE_UNION
267*cf7f2e2dSJohn Marino       && TYPE_CODE (type) != TYPE_CODE_STRUCT
268*cf7f2e2dSJohn Marino       && TYPE_CODE (type) != TYPE_CODE_ARRAY)
269*cf7f2e2dSJohn Marino     {
270*cf7f2e2dSJohn Marino       if (! value_bits_valid (val, TARGET_CHAR_BIT * offset,
271*cf7f2e2dSJohn Marino 			      TARGET_CHAR_BIT * TYPE_LENGTH (type)))
272*cf7f2e2dSJohn Marino 	{
273*cf7f2e2dSJohn Marino 	  fprintf_filtered (stream, _("<value optimized out>"));
274*cf7f2e2dSJohn Marino 	  return 0;
275*cf7f2e2dSJohn Marino 	}
276*cf7f2e2dSJohn Marino     }
277*cf7f2e2dSJohn Marino 
278*cf7f2e2dSJohn Marino   return 1;
279*cf7f2e2dSJohn Marino }
280*cf7f2e2dSJohn Marino 
2815796c8dcSSimon Schubert /* Print using the given LANGUAGE the data of type TYPE located at VALADDR
2825796c8dcSSimon Schubert    (within GDB), which came from the inferior at address ADDRESS, onto
2835796c8dcSSimon Schubert    stdio stream STREAM according to OPTIONS.
2845796c8dcSSimon Schubert 
2855796c8dcSSimon Schubert    If the data are a string pointer, returns the number of string characters
2865796c8dcSSimon Schubert    printed.
2875796c8dcSSimon Schubert 
2885796c8dcSSimon Schubert    FIXME:  The data at VALADDR is in target byte order.  If gdb is ever
2895796c8dcSSimon Schubert    enhanced to be able to debug more than the single target it was compiled
2905796c8dcSSimon Schubert    for (specific CPU type and thus specific target byte ordering), then
2915796c8dcSSimon Schubert    either the print routines are going to have to take this into account,
2925796c8dcSSimon Schubert    or the data is going to have to be passed into here already converted
2935796c8dcSSimon Schubert    to the host byte ordering, whichever is more convenient. */
2945796c8dcSSimon Schubert 
2955796c8dcSSimon Schubert 
2965796c8dcSSimon Schubert int
2975796c8dcSSimon Schubert val_print (struct type *type, const gdb_byte *valaddr, int embedded_offset,
2985796c8dcSSimon Schubert 	   CORE_ADDR address, struct ui_file *stream, int recurse,
299*cf7f2e2dSJohn Marino 	   const struct value *val,
3005796c8dcSSimon Schubert 	   const struct value_print_options *options,
3015796c8dcSSimon Schubert 	   const struct language_defn *language)
3025796c8dcSSimon Schubert {
3035796c8dcSSimon Schubert   volatile struct gdb_exception except;
3045796c8dcSSimon Schubert   int ret = 0;
3055796c8dcSSimon Schubert   struct value_print_options local_opts = *options;
3065796c8dcSSimon Schubert   struct type *real_type = check_typedef (type);
3075796c8dcSSimon Schubert 
3085796c8dcSSimon Schubert   if (local_opts.pretty == Val_pretty_default)
3095796c8dcSSimon Schubert     local_opts.pretty = (local_opts.prettyprint_structs
3105796c8dcSSimon Schubert 			 ? Val_prettyprint : Val_no_prettyprint);
3115796c8dcSSimon Schubert 
3125796c8dcSSimon Schubert   QUIT;
3135796c8dcSSimon Schubert 
3145796c8dcSSimon Schubert   /* Ensure that the type is complete and not just a stub.  If the type is
3155796c8dcSSimon Schubert      only a stub and we can't find and substitute its complete type, then
3165796c8dcSSimon Schubert      print appropriate string and return.  */
3175796c8dcSSimon Schubert 
3185796c8dcSSimon Schubert   if (TYPE_STUB (real_type))
3195796c8dcSSimon Schubert     {
320*cf7f2e2dSJohn Marino       fprintf_filtered (stream, _("<incomplete type>"));
3215796c8dcSSimon Schubert       gdb_flush (stream);
3225796c8dcSSimon Schubert       return (0);
3235796c8dcSSimon Schubert     }
3245796c8dcSSimon Schubert 
325*cf7f2e2dSJohn Marino   if (!valprint_check_validity (stream, real_type, embedded_offset, val))
326*cf7f2e2dSJohn Marino     return 0;
327*cf7f2e2dSJohn Marino 
3285796c8dcSSimon Schubert   if (!options->raw)
3295796c8dcSSimon Schubert     {
3305796c8dcSSimon Schubert       ret = apply_val_pretty_printer (type, valaddr, embedded_offset,
331*cf7f2e2dSJohn Marino 				      address, stream, recurse,
332*cf7f2e2dSJohn Marino 				      val, options, language);
3335796c8dcSSimon Schubert       if (ret)
3345796c8dcSSimon Schubert 	return ret;
3355796c8dcSSimon Schubert     }
3365796c8dcSSimon Schubert 
3375796c8dcSSimon Schubert   /* Handle summary mode.  If the value is a scalar, print it;
3385796c8dcSSimon Schubert      otherwise, print an ellipsis.  */
3395796c8dcSSimon Schubert   if (options->summary && !scalar_type_p (type))
3405796c8dcSSimon Schubert     {
3415796c8dcSSimon Schubert       fprintf_filtered (stream, "...");
3425796c8dcSSimon Schubert       return 0;
3435796c8dcSSimon Schubert     }
3445796c8dcSSimon Schubert 
3455796c8dcSSimon Schubert   TRY_CATCH (except, RETURN_MASK_ERROR)
3465796c8dcSSimon Schubert     {
3475796c8dcSSimon Schubert       ret = language->la_val_print (type, valaddr, embedded_offset, address,
348*cf7f2e2dSJohn Marino 				    stream, recurse, val,
349*cf7f2e2dSJohn Marino 				    &local_opts);
3505796c8dcSSimon Schubert     }
3515796c8dcSSimon Schubert   if (except.reason < 0)
3525796c8dcSSimon Schubert     fprintf_filtered (stream, _("<error reading variable>"));
3535796c8dcSSimon Schubert 
3545796c8dcSSimon Schubert   return ret;
3555796c8dcSSimon Schubert }
3565796c8dcSSimon Schubert 
3575796c8dcSSimon Schubert /* Check whether the value VAL is printable.  Return 1 if it is;
3585796c8dcSSimon Schubert    return 0 and print an appropriate error message to STREAM if it
3595796c8dcSSimon Schubert    is not.  */
3605796c8dcSSimon Schubert 
3615796c8dcSSimon Schubert static int
3625796c8dcSSimon Schubert value_check_printable (struct value *val, struct ui_file *stream)
3635796c8dcSSimon Schubert {
3645796c8dcSSimon Schubert   if (val == 0)
3655796c8dcSSimon Schubert     {
3665796c8dcSSimon Schubert       fprintf_filtered (stream, _("<address of value unknown>"));
3675796c8dcSSimon Schubert       return 0;
3685796c8dcSSimon Schubert     }
3695796c8dcSSimon Schubert 
370*cf7f2e2dSJohn Marino   if (value_entirely_optimized_out (val))
3715796c8dcSSimon Schubert     {
3725796c8dcSSimon Schubert       fprintf_filtered (stream, _("<value optimized out>"));
3735796c8dcSSimon Schubert       return 0;
3745796c8dcSSimon Schubert     }
3755796c8dcSSimon Schubert 
3765796c8dcSSimon Schubert   if (TYPE_CODE (value_type (val)) == TYPE_CODE_INTERNAL_FUNCTION)
3775796c8dcSSimon Schubert     {
3785796c8dcSSimon Schubert       fprintf_filtered (stream, _("<internal function %s>"),
3795796c8dcSSimon Schubert 			value_internal_function_name (val));
3805796c8dcSSimon Schubert       return 0;
3815796c8dcSSimon Schubert     }
3825796c8dcSSimon Schubert 
3835796c8dcSSimon Schubert   return 1;
3845796c8dcSSimon Schubert }
3855796c8dcSSimon Schubert 
3865796c8dcSSimon Schubert /* Print using the given LANGUAGE the value VAL onto stream STREAM according
3875796c8dcSSimon Schubert    to OPTIONS.
3885796c8dcSSimon Schubert 
3895796c8dcSSimon Schubert    If the data are a string pointer, returns the number of string characters
3905796c8dcSSimon Schubert    printed.
3915796c8dcSSimon Schubert 
3925796c8dcSSimon Schubert    This is a preferable interface to val_print, above, because it uses
3935796c8dcSSimon Schubert    GDB's value mechanism.  */
3945796c8dcSSimon Schubert 
3955796c8dcSSimon Schubert int
3965796c8dcSSimon Schubert common_val_print (struct value *val, struct ui_file *stream, int recurse,
3975796c8dcSSimon Schubert 		  const struct value_print_options *options,
3985796c8dcSSimon Schubert 		  const struct language_defn *language)
3995796c8dcSSimon Schubert {
4005796c8dcSSimon Schubert   if (!value_check_printable (val, stream))
4015796c8dcSSimon Schubert     return 0;
4025796c8dcSSimon Schubert 
403*cf7f2e2dSJohn Marino   if (language->la_language == language_ada)
404*cf7f2e2dSJohn Marino     /* The value might have a dynamic type, which would cause trouble
405*cf7f2e2dSJohn Marino        below when trying to extract the value contents (since the value
406*cf7f2e2dSJohn Marino        size is determined from the type size which is unknown).  So
407*cf7f2e2dSJohn Marino        get a fixed representation of our value.  */
408*cf7f2e2dSJohn Marino     val = ada_to_fixed_value (val);
409*cf7f2e2dSJohn Marino 
410*cf7f2e2dSJohn Marino   return val_print (value_type (val), value_contents_for_printing (val),
4115796c8dcSSimon Schubert 		    value_embedded_offset (val), value_address (val),
412*cf7f2e2dSJohn Marino 		    stream, recurse,
413*cf7f2e2dSJohn Marino 		    val, options, language);
4145796c8dcSSimon Schubert }
4155796c8dcSSimon Schubert 
416*cf7f2e2dSJohn Marino /* Print on stream STREAM the value VAL according to OPTIONS.  The value
417*cf7f2e2dSJohn Marino    is printed using the current_language syntax.
418*cf7f2e2dSJohn Marino 
419*cf7f2e2dSJohn Marino    If the object printed is a string pointer, return the number of string
420*cf7f2e2dSJohn Marino    bytes printed.  */
4215796c8dcSSimon Schubert 
4225796c8dcSSimon Schubert int
4235796c8dcSSimon Schubert value_print (struct value *val, struct ui_file *stream,
4245796c8dcSSimon Schubert 	     const struct value_print_options *options)
4255796c8dcSSimon Schubert {
4265796c8dcSSimon Schubert   if (!value_check_printable (val, stream))
4275796c8dcSSimon Schubert     return 0;
4285796c8dcSSimon Schubert 
4295796c8dcSSimon Schubert   if (!options->raw)
4305796c8dcSSimon Schubert     {
4315796c8dcSSimon Schubert       int r = apply_val_pretty_printer (value_type (val),
432*cf7f2e2dSJohn Marino 					value_contents_for_printing (val),
4335796c8dcSSimon Schubert 					value_embedded_offset (val),
4345796c8dcSSimon Schubert 					value_address (val),
435*cf7f2e2dSJohn Marino 					stream, 0,
436*cf7f2e2dSJohn Marino 					val, options, current_language);
437*cf7f2e2dSJohn Marino 
4385796c8dcSSimon Schubert       if (r)
4395796c8dcSSimon Schubert 	return r;
4405796c8dcSSimon Schubert     }
4415796c8dcSSimon Schubert 
4425796c8dcSSimon Schubert   return LA_VALUE_PRINT (val, stream, options);
4435796c8dcSSimon Schubert }
4445796c8dcSSimon Schubert 
4455796c8dcSSimon Schubert /* Called by various <lang>_val_print routines to print
4465796c8dcSSimon Schubert    TYPE_CODE_INT's.  TYPE is the type.  VALADDR is the address of the
4475796c8dcSSimon Schubert    value.  STREAM is where to print the value.  */
4485796c8dcSSimon Schubert 
4495796c8dcSSimon Schubert void
4505796c8dcSSimon Schubert val_print_type_code_int (struct type *type, const gdb_byte *valaddr,
4515796c8dcSSimon Schubert 			 struct ui_file *stream)
4525796c8dcSSimon Schubert {
4535796c8dcSSimon Schubert   enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
4545796c8dcSSimon Schubert 
4555796c8dcSSimon Schubert   if (TYPE_LENGTH (type) > sizeof (LONGEST))
4565796c8dcSSimon Schubert     {
4575796c8dcSSimon Schubert       LONGEST val;
4585796c8dcSSimon Schubert 
4595796c8dcSSimon Schubert       if (TYPE_UNSIGNED (type)
4605796c8dcSSimon Schubert 	  && extract_long_unsigned_integer (valaddr, TYPE_LENGTH (type),
4615796c8dcSSimon Schubert 					    byte_order, &val))
4625796c8dcSSimon Schubert 	{
4635796c8dcSSimon Schubert 	  print_longest (stream, 'u', 0, val);
4645796c8dcSSimon Schubert 	}
4655796c8dcSSimon Schubert       else
4665796c8dcSSimon Schubert 	{
4675796c8dcSSimon Schubert 	  /* Signed, or we couldn't turn an unsigned value into a
4685796c8dcSSimon Schubert 	     LONGEST.  For signed values, one could assume two's
4695796c8dcSSimon Schubert 	     complement (a reasonable assumption, I think) and do
4705796c8dcSSimon Schubert 	     better than this.  */
4715796c8dcSSimon Schubert 	  print_hex_chars (stream, (unsigned char *) valaddr,
4725796c8dcSSimon Schubert 			   TYPE_LENGTH (type), byte_order);
4735796c8dcSSimon Schubert 	}
4745796c8dcSSimon Schubert     }
4755796c8dcSSimon Schubert   else
4765796c8dcSSimon Schubert     {
4775796c8dcSSimon Schubert       print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0,
4785796c8dcSSimon Schubert 		     unpack_long (type, valaddr));
4795796c8dcSSimon Schubert     }
4805796c8dcSSimon Schubert }
4815796c8dcSSimon Schubert 
4825796c8dcSSimon Schubert void
4835796c8dcSSimon Schubert val_print_type_code_flags (struct type *type, const gdb_byte *valaddr,
4845796c8dcSSimon Schubert 			   struct ui_file *stream)
4855796c8dcSSimon Schubert {
4865796c8dcSSimon Schubert   ULONGEST val = unpack_long (type, valaddr);
4875796c8dcSSimon Schubert   int bitpos, nfields = TYPE_NFIELDS (type);
4885796c8dcSSimon Schubert 
4895796c8dcSSimon Schubert   fputs_filtered ("[ ", stream);
4905796c8dcSSimon Schubert   for (bitpos = 0; bitpos < nfields; bitpos++)
4915796c8dcSSimon Schubert     {
4925796c8dcSSimon Schubert       if (TYPE_FIELD_BITPOS (type, bitpos) != -1
4935796c8dcSSimon Schubert 	  && (val & ((ULONGEST)1 << bitpos)))
4945796c8dcSSimon Schubert 	{
4955796c8dcSSimon Schubert 	  if (TYPE_FIELD_NAME (type, bitpos))
4965796c8dcSSimon Schubert 	    fprintf_filtered (stream, "%s ", TYPE_FIELD_NAME (type, bitpos));
4975796c8dcSSimon Schubert 	  else
4985796c8dcSSimon Schubert 	    fprintf_filtered (stream, "#%d ", bitpos);
4995796c8dcSSimon Schubert 	}
5005796c8dcSSimon Schubert     }
5015796c8dcSSimon Schubert   fputs_filtered ("]", stream);
5025796c8dcSSimon Schubert }
5035796c8dcSSimon Schubert 
5045796c8dcSSimon Schubert /* Print a number according to FORMAT which is one of d,u,x,o,b,h,w,g.
5055796c8dcSSimon Schubert    The raison d'etre of this function is to consolidate printing of
5065796c8dcSSimon Schubert    LONG_LONG's into this one function. The format chars b,h,w,g are
5075796c8dcSSimon Schubert    from print_scalar_formatted().  Numbers are printed using C
5085796c8dcSSimon Schubert    format.
5095796c8dcSSimon Schubert 
5105796c8dcSSimon Schubert    USE_C_FORMAT means to use C format in all cases.  Without it,
5115796c8dcSSimon Schubert    'o' and 'x' format do not include the standard C radix prefix
5125796c8dcSSimon Schubert    (leading 0 or 0x).
5135796c8dcSSimon Schubert 
5145796c8dcSSimon Schubert    Hilfinger/2004-09-09: USE_C_FORMAT was originally called USE_LOCAL
5155796c8dcSSimon Schubert    and was intended to request formating according to the current
5165796c8dcSSimon Schubert    language and would be used for most integers that GDB prints.  The
5175796c8dcSSimon Schubert    exceptional cases were things like protocols where the format of
5185796c8dcSSimon Schubert    the integer is a protocol thing, not a user-visible thing).  The
5195796c8dcSSimon Schubert    parameter remains to preserve the information of what things might
5205796c8dcSSimon Schubert    be printed with language-specific format, should we ever resurrect
5215796c8dcSSimon Schubert    that capability. */
5225796c8dcSSimon Schubert 
5235796c8dcSSimon Schubert void
5245796c8dcSSimon Schubert print_longest (struct ui_file *stream, int format, int use_c_format,
5255796c8dcSSimon Schubert 	       LONGEST val_long)
5265796c8dcSSimon Schubert {
5275796c8dcSSimon Schubert   const char *val;
5285796c8dcSSimon Schubert 
5295796c8dcSSimon Schubert   switch (format)
5305796c8dcSSimon Schubert     {
5315796c8dcSSimon Schubert     case 'd':
5325796c8dcSSimon Schubert       val = int_string (val_long, 10, 1, 0, 1); break;
5335796c8dcSSimon Schubert     case 'u':
5345796c8dcSSimon Schubert       val = int_string (val_long, 10, 0, 0, 1); break;
5355796c8dcSSimon Schubert     case 'x':
5365796c8dcSSimon Schubert       val = int_string (val_long, 16, 0, 0, use_c_format); break;
5375796c8dcSSimon Schubert     case 'b':
5385796c8dcSSimon Schubert       val = int_string (val_long, 16, 0, 2, 1); break;
5395796c8dcSSimon Schubert     case 'h':
5405796c8dcSSimon Schubert       val = int_string (val_long, 16, 0, 4, 1); break;
5415796c8dcSSimon Schubert     case 'w':
5425796c8dcSSimon Schubert       val = int_string (val_long, 16, 0, 8, 1); break;
5435796c8dcSSimon Schubert     case 'g':
5445796c8dcSSimon Schubert       val = int_string (val_long, 16, 0, 16, 1); break;
5455796c8dcSSimon Schubert       break;
5465796c8dcSSimon Schubert     case 'o':
5475796c8dcSSimon Schubert       val = int_string (val_long, 8, 0, 0, use_c_format); break;
5485796c8dcSSimon Schubert     default:
5495796c8dcSSimon Schubert       internal_error (__FILE__, __LINE__, _("failed internal consistency check"));
5505796c8dcSSimon Schubert     }
5515796c8dcSSimon Schubert   fputs_filtered (val, stream);
5525796c8dcSSimon Schubert }
5535796c8dcSSimon Schubert 
5545796c8dcSSimon Schubert /* This used to be a macro, but I don't think it is called often enough
5555796c8dcSSimon Schubert    to merit such treatment.  */
5565796c8dcSSimon Schubert /* Convert a LONGEST to an int.  This is used in contexts (e.g. number of
5575796c8dcSSimon Schubert    arguments to a function, number in a value history, register number, etc.)
5585796c8dcSSimon Schubert    where the value must not be larger than can fit in an int.  */
5595796c8dcSSimon Schubert 
5605796c8dcSSimon Schubert int
5615796c8dcSSimon Schubert longest_to_int (LONGEST arg)
5625796c8dcSSimon Schubert {
5635796c8dcSSimon Schubert   /* Let the compiler do the work */
5645796c8dcSSimon Schubert   int rtnval = (int) arg;
5655796c8dcSSimon Schubert 
5665796c8dcSSimon Schubert   /* Check for overflows or underflows */
5675796c8dcSSimon Schubert   if (sizeof (LONGEST) > sizeof (int))
5685796c8dcSSimon Schubert     {
5695796c8dcSSimon Schubert       if (rtnval != arg)
5705796c8dcSSimon Schubert 	{
5715796c8dcSSimon Schubert 	  error (_("Value out of range."));
5725796c8dcSSimon Schubert 	}
5735796c8dcSSimon Schubert     }
5745796c8dcSSimon Schubert   return (rtnval);
5755796c8dcSSimon Schubert }
5765796c8dcSSimon Schubert 
5775796c8dcSSimon Schubert /* Print a floating point value of type TYPE (not always a
5785796c8dcSSimon Schubert    TYPE_CODE_FLT), pointed to in GDB by VALADDR, on STREAM.  */
5795796c8dcSSimon Schubert 
5805796c8dcSSimon Schubert void
5815796c8dcSSimon Schubert print_floating (const gdb_byte *valaddr, struct type *type,
5825796c8dcSSimon Schubert 		struct ui_file *stream)
5835796c8dcSSimon Schubert {
5845796c8dcSSimon Schubert   DOUBLEST doub;
5855796c8dcSSimon Schubert   int inv;
5865796c8dcSSimon Schubert   const struct floatformat *fmt = NULL;
5875796c8dcSSimon Schubert   unsigned len = TYPE_LENGTH (type);
5885796c8dcSSimon Schubert   enum float_kind kind;
5895796c8dcSSimon Schubert 
5905796c8dcSSimon Schubert   /* If it is a floating-point, check for obvious problems.  */
5915796c8dcSSimon Schubert   if (TYPE_CODE (type) == TYPE_CODE_FLT)
5925796c8dcSSimon Schubert     fmt = floatformat_from_type (type);
5935796c8dcSSimon Schubert   if (fmt != NULL)
5945796c8dcSSimon Schubert     {
5955796c8dcSSimon Schubert       kind = floatformat_classify (fmt, valaddr);
5965796c8dcSSimon Schubert       if (kind == float_nan)
5975796c8dcSSimon Schubert 	{
5985796c8dcSSimon Schubert 	  if (floatformat_is_negative (fmt, valaddr))
5995796c8dcSSimon Schubert 	    fprintf_filtered (stream, "-");
6005796c8dcSSimon Schubert 	  fprintf_filtered (stream, "nan(");
6015796c8dcSSimon Schubert 	  fputs_filtered ("0x", stream);
6025796c8dcSSimon Schubert 	  fputs_filtered (floatformat_mantissa (fmt, valaddr), stream);
6035796c8dcSSimon Schubert 	  fprintf_filtered (stream, ")");
6045796c8dcSSimon Schubert 	  return;
6055796c8dcSSimon Schubert 	}
6065796c8dcSSimon Schubert       else if (kind == float_infinite)
6075796c8dcSSimon Schubert 	{
6085796c8dcSSimon Schubert 	  if (floatformat_is_negative (fmt, valaddr))
6095796c8dcSSimon Schubert 	    fputs_filtered ("-", stream);
6105796c8dcSSimon Schubert 	  fputs_filtered ("inf", stream);
6115796c8dcSSimon Schubert 	  return;
6125796c8dcSSimon Schubert 	}
6135796c8dcSSimon Schubert     }
6145796c8dcSSimon Schubert 
6155796c8dcSSimon Schubert   /* NOTE: cagney/2002-01-15: The TYPE passed into print_floating()
6165796c8dcSSimon Schubert      isn't necessarily a TYPE_CODE_FLT.  Consequently, unpack_double
6175796c8dcSSimon Schubert      needs to be used as that takes care of any necessary type
6185796c8dcSSimon Schubert      conversions.  Such conversions are of course direct to DOUBLEST
6195796c8dcSSimon Schubert      and disregard any possible target floating point limitations.
6205796c8dcSSimon Schubert      For instance, a u64 would be converted and displayed exactly on a
6215796c8dcSSimon Schubert      host with 80 bit DOUBLEST but with loss of information on a host
6225796c8dcSSimon Schubert      with 64 bit DOUBLEST.  */
6235796c8dcSSimon Schubert 
6245796c8dcSSimon Schubert   doub = unpack_double (type, valaddr, &inv);
6255796c8dcSSimon Schubert   if (inv)
6265796c8dcSSimon Schubert     {
6275796c8dcSSimon Schubert       fprintf_filtered (stream, "<invalid float value>");
6285796c8dcSSimon Schubert       return;
6295796c8dcSSimon Schubert     }
6305796c8dcSSimon Schubert 
6315796c8dcSSimon Schubert   /* FIXME: kettenis/2001-01-20: The following code makes too much
6325796c8dcSSimon Schubert      assumptions about the host and target floating point format.  */
6335796c8dcSSimon Schubert 
6345796c8dcSSimon Schubert   /* NOTE: cagney/2002-02-03: Since the TYPE of what was passed in may
6355796c8dcSSimon Schubert      not necessarily be a TYPE_CODE_FLT, the below ignores that and
6365796c8dcSSimon Schubert      instead uses the type's length to determine the precision of the
6375796c8dcSSimon Schubert      floating-point value being printed.  */
6385796c8dcSSimon Schubert 
6395796c8dcSSimon Schubert   if (len < sizeof (double))
6405796c8dcSSimon Schubert       fprintf_filtered (stream, "%.9g", (double) doub);
6415796c8dcSSimon Schubert   else if (len == sizeof (double))
6425796c8dcSSimon Schubert       fprintf_filtered (stream, "%.17g", (double) doub);
6435796c8dcSSimon Schubert   else
6445796c8dcSSimon Schubert #ifdef PRINTF_HAS_LONG_DOUBLE
6455796c8dcSSimon Schubert     fprintf_filtered (stream, "%.35Lg", doub);
6465796c8dcSSimon Schubert #else
6475796c8dcSSimon Schubert     /* This at least wins with values that are representable as
6485796c8dcSSimon Schubert        doubles.  */
6495796c8dcSSimon Schubert     fprintf_filtered (stream, "%.17g", (double) doub);
6505796c8dcSSimon Schubert #endif
6515796c8dcSSimon Schubert }
6525796c8dcSSimon Schubert 
6535796c8dcSSimon Schubert void
6545796c8dcSSimon Schubert print_decimal_floating (const gdb_byte *valaddr, struct type *type,
6555796c8dcSSimon Schubert 			struct ui_file *stream)
6565796c8dcSSimon Schubert {
6575796c8dcSSimon Schubert   enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
6585796c8dcSSimon Schubert   char decstr[MAX_DECIMAL_STRING];
6595796c8dcSSimon Schubert   unsigned len = TYPE_LENGTH (type);
6605796c8dcSSimon Schubert 
6615796c8dcSSimon Schubert   decimal_to_string (valaddr, len, byte_order, decstr);
6625796c8dcSSimon Schubert   fputs_filtered (decstr, stream);
6635796c8dcSSimon Schubert   return;
6645796c8dcSSimon Schubert }
6655796c8dcSSimon Schubert 
6665796c8dcSSimon Schubert void
6675796c8dcSSimon Schubert print_binary_chars (struct ui_file *stream, const gdb_byte *valaddr,
6685796c8dcSSimon Schubert 		    unsigned len, enum bfd_endian byte_order)
6695796c8dcSSimon Schubert {
6705796c8dcSSimon Schubert 
6715796c8dcSSimon Schubert #define BITS_IN_BYTES 8
6725796c8dcSSimon Schubert 
6735796c8dcSSimon Schubert   const gdb_byte *p;
6745796c8dcSSimon Schubert   unsigned int i;
6755796c8dcSSimon Schubert   int b;
6765796c8dcSSimon Schubert 
6775796c8dcSSimon Schubert   /* Declared "int" so it will be signed.
6785796c8dcSSimon Schubert    * This ensures that right shift will shift in zeros.
6795796c8dcSSimon Schubert    */
6805796c8dcSSimon Schubert   const int mask = 0x080;
6815796c8dcSSimon Schubert 
6825796c8dcSSimon Schubert   /* FIXME: We should be not printing leading zeroes in most cases.  */
6835796c8dcSSimon Schubert 
6845796c8dcSSimon Schubert   if (byte_order == BFD_ENDIAN_BIG)
6855796c8dcSSimon Schubert     {
6865796c8dcSSimon Schubert       for (p = valaddr;
6875796c8dcSSimon Schubert 	   p < valaddr + len;
6885796c8dcSSimon Schubert 	   p++)
6895796c8dcSSimon Schubert 	{
6905796c8dcSSimon Schubert 	  /* Every byte has 8 binary characters; peel off
6915796c8dcSSimon Schubert 	   * and print from the MSB end.
6925796c8dcSSimon Schubert 	   */
6935796c8dcSSimon Schubert 	  for (i = 0; i < (BITS_IN_BYTES * sizeof (*p)); i++)
6945796c8dcSSimon Schubert 	    {
6955796c8dcSSimon Schubert 	      if (*p & (mask >> i))
6965796c8dcSSimon Schubert 		b = 1;
6975796c8dcSSimon Schubert 	      else
6985796c8dcSSimon Schubert 		b = 0;
6995796c8dcSSimon Schubert 
7005796c8dcSSimon Schubert 	      fprintf_filtered (stream, "%1d", b);
7015796c8dcSSimon Schubert 	    }
7025796c8dcSSimon Schubert 	}
7035796c8dcSSimon Schubert     }
7045796c8dcSSimon Schubert   else
7055796c8dcSSimon Schubert     {
7065796c8dcSSimon Schubert       for (p = valaddr + len - 1;
7075796c8dcSSimon Schubert 	   p >= valaddr;
7085796c8dcSSimon Schubert 	   p--)
7095796c8dcSSimon Schubert 	{
7105796c8dcSSimon Schubert 	  for (i = 0; i < (BITS_IN_BYTES * sizeof (*p)); i++)
7115796c8dcSSimon Schubert 	    {
7125796c8dcSSimon Schubert 	      if (*p & (mask >> i))
7135796c8dcSSimon Schubert 		b = 1;
7145796c8dcSSimon Schubert 	      else
7155796c8dcSSimon Schubert 		b = 0;
7165796c8dcSSimon Schubert 
7175796c8dcSSimon Schubert 	      fprintf_filtered (stream, "%1d", b);
7185796c8dcSSimon Schubert 	    }
7195796c8dcSSimon Schubert 	}
7205796c8dcSSimon Schubert     }
7215796c8dcSSimon Schubert }
7225796c8dcSSimon Schubert 
7235796c8dcSSimon Schubert /* VALADDR points to an integer of LEN bytes.
7245796c8dcSSimon Schubert  * Print it in octal on stream or format it in buf.
7255796c8dcSSimon Schubert  */
7265796c8dcSSimon Schubert void
7275796c8dcSSimon Schubert print_octal_chars (struct ui_file *stream, const gdb_byte *valaddr,
7285796c8dcSSimon Schubert 		   unsigned len, enum bfd_endian byte_order)
7295796c8dcSSimon Schubert {
7305796c8dcSSimon Schubert   const gdb_byte *p;
7315796c8dcSSimon Schubert   unsigned char octa1, octa2, octa3, carry;
7325796c8dcSSimon Schubert   int cycle;
7335796c8dcSSimon Schubert 
7345796c8dcSSimon Schubert   /* FIXME: We should be not printing leading zeroes in most cases.  */
7355796c8dcSSimon Schubert 
7365796c8dcSSimon Schubert 
7375796c8dcSSimon Schubert   /* Octal is 3 bits, which doesn't fit.  Yuk.  So we have to track
7385796c8dcSSimon Schubert    * the extra bits, which cycle every three bytes:
7395796c8dcSSimon Schubert    *
7405796c8dcSSimon Schubert    * Byte side:       0            1             2          3
7415796c8dcSSimon Schubert    *                         |             |            |            |
7425796c8dcSSimon Schubert    * bit number   123 456 78 | 9 012 345 6 | 78 901 234 | 567 890 12 |
7435796c8dcSSimon Schubert    *
7445796c8dcSSimon Schubert    * Octal side:   0   1   carry  3   4  carry ...
7455796c8dcSSimon Schubert    *
7465796c8dcSSimon Schubert    * Cycle number:    0             1            2
7475796c8dcSSimon Schubert    *
7485796c8dcSSimon Schubert    * But of course we are printing from the high side, so we have to
7495796c8dcSSimon Schubert    * figure out where in the cycle we are so that we end up with no
7505796c8dcSSimon Schubert    * left over bits at the end.
7515796c8dcSSimon Schubert    */
7525796c8dcSSimon Schubert #define BITS_IN_OCTAL 3
7535796c8dcSSimon Schubert #define HIGH_ZERO     0340
7545796c8dcSSimon Schubert #define LOW_ZERO      0016
7555796c8dcSSimon Schubert #define CARRY_ZERO    0003
7565796c8dcSSimon Schubert #define HIGH_ONE      0200
7575796c8dcSSimon Schubert #define MID_ONE       0160
7585796c8dcSSimon Schubert #define LOW_ONE       0016
7595796c8dcSSimon Schubert #define CARRY_ONE     0001
7605796c8dcSSimon Schubert #define HIGH_TWO      0300
7615796c8dcSSimon Schubert #define MID_TWO       0070
7625796c8dcSSimon Schubert #define LOW_TWO       0007
7635796c8dcSSimon Schubert 
7645796c8dcSSimon Schubert   /* For 32 we start in cycle 2, with two bits and one bit carry;
7655796c8dcSSimon Schubert    * for 64 in cycle in cycle 1, with one bit and a two bit carry.
7665796c8dcSSimon Schubert    */
7675796c8dcSSimon Schubert   cycle = (len * BITS_IN_BYTES) % BITS_IN_OCTAL;
7685796c8dcSSimon Schubert   carry = 0;
7695796c8dcSSimon Schubert 
7705796c8dcSSimon Schubert   fputs_filtered ("0", stream);
7715796c8dcSSimon Schubert   if (byte_order == BFD_ENDIAN_BIG)
7725796c8dcSSimon Schubert     {
7735796c8dcSSimon Schubert       for (p = valaddr;
7745796c8dcSSimon Schubert 	   p < valaddr + len;
7755796c8dcSSimon Schubert 	   p++)
7765796c8dcSSimon Schubert 	{
7775796c8dcSSimon Schubert 	  switch (cycle)
7785796c8dcSSimon Schubert 	    {
7795796c8dcSSimon Schubert 	    case 0:
7805796c8dcSSimon Schubert 	      /* No carry in, carry out two bits.
7815796c8dcSSimon Schubert 	       */
7825796c8dcSSimon Schubert 	      octa1 = (HIGH_ZERO & *p) >> 5;
7835796c8dcSSimon Schubert 	      octa2 = (LOW_ZERO & *p) >> 2;
7845796c8dcSSimon Schubert 	      carry = (CARRY_ZERO & *p);
7855796c8dcSSimon Schubert 	      fprintf_filtered (stream, "%o", octa1);
7865796c8dcSSimon Schubert 	      fprintf_filtered (stream, "%o", octa2);
7875796c8dcSSimon Schubert 	      break;
7885796c8dcSSimon Schubert 
7895796c8dcSSimon Schubert 	    case 1:
7905796c8dcSSimon Schubert 	      /* Carry in two bits, carry out one bit.
7915796c8dcSSimon Schubert 	       */
7925796c8dcSSimon Schubert 	      octa1 = (carry << 1) | ((HIGH_ONE & *p) >> 7);
7935796c8dcSSimon Schubert 	      octa2 = (MID_ONE & *p) >> 4;
7945796c8dcSSimon Schubert 	      octa3 = (LOW_ONE & *p) >> 1;
7955796c8dcSSimon Schubert 	      carry = (CARRY_ONE & *p);
7965796c8dcSSimon Schubert 	      fprintf_filtered (stream, "%o", octa1);
7975796c8dcSSimon Schubert 	      fprintf_filtered (stream, "%o", octa2);
7985796c8dcSSimon Schubert 	      fprintf_filtered (stream, "%o", octa3);
7995796c8dcSSimon Schubert 	      break;
8005796c8dcSSimon Schubert 
8015796c8dcSSimon Schubert 	    case 2:
8025796c8dcSSimon Schubert 	      /* Carry in one bit, no carry out.
8035796c8dcSSimon Schubert 	       */
8045796c8dcSSimon Schubert 	      octa1 = (carry << 2) | ((HIGH_TWO & *p) >> 6);
8055796c8dcSSimon Schubert 	      octa2 = (MID_TWO & *p) >> 3;
8065796c8dcSSimon Schubert 	      octa3 = (LOW_TWO & *p);
8075796c8dcSSimon Schubert 	      carry = 0;
8085796c8dcSSimon Schubert 	      fprintf_filtered (stream, "%o", octa1);
8095796c8dcSSimon Schubert 	      fprintf_filtered (stream, "%o", octa2);
8105796c8dcSSimon Schubert 	      fprintf_filtered (stream, "%o", octa3);
8115796c8dcSSimon Schubert 	      break;
8125796c8dcSSimon Schubert 
8135796c8dcSSimon Schubert 	    default:
8145796c8dcSSimon Schubert 	      error (_("Internal error in octal conversion;"));
8155796c8dcSSimon Schubert 	    }
8165796c8dcSSimon Schubert 
8175796c8dcSSimon Schubert 	  cycle++;
8185796c8dcSSimon Schubert 	  cycle = cycle % BITS_IN_OCTAL;
8195796c8dcSSimon Schubert 	}
8205796c8dcSSimon Schubert     }
8215796c8dcSSimon Schubert   else
8225796c8dcSSimon Schubert     {
8235796c8dcSSimon Schubert       for (p = valaddr + len - 1;
8245796c8dcSSimon Schubert 	   p >= valaddr;
8255796c8dcSSimon Schubert 	   p--)
8265796c8dcSSimon Schubert 	{
8275796c8dcSSimon Schubert 	  switch (cycle)
8285796c8dcSSimon Schubert 	    {
8295796c8dcSSimon Schubert 	    case 0:
8305796c8dcSSimon Schubert 	      /* Carry out, no carry in */
8315796c8dcSSimon Schubert 	      octa1 = (HIGH_ZERO & *p) >> 5;
8325796c8dcSSimon Schubert 	      octa2 = (LOW_ZERO & *p) >> 2;
8335796c8dcSSimon Schubert 	      carry = (CARRY_ZERO & *p);
8345796c8dcSSimon Schubert 	      fprintf_filtered (stream, "%o", octa1);
8355796c8dcSSimon Schubert 	      fprintf_filtered (stream, "%o", octa2);
8365796c8dcSSimon Schubert 	      break;
8375796c8dcSSimon Schubert 
8385796c8dcSSimon Schubert 	    case 1:
8395796c8dcSSimon Schubert 	      /* Carry in, carry out */
8405796c8dcSSimon Schubert 	      octa1 = (carry << 1) | ((HIGH_ONE & *p) >> 7);
8415796c8dcSSimon Schubert 	      octa2 = (MID_ONE & *p) >> 4;
8425796c8dcSSimon Schubert 	      octa3 = (LOW_ONE & *p) >> 1;
8435796c8dcSSimon Schubert 	      carry = (CARRY_ONE & *p);
8445796c8dcSSimon Schubert 	      fprintf_filtered (stream, "%o", octa1);
8455796c8dcSSimon Schubert 	      fprintf_filtered (stream, "%o", octa2);
8465796c8dcSSimon Schubert 	      fprintf_filtered (stream, "%o", octa3);
8475796c8dcSSimon Schubert 	      break;
8485796c8dcSSimon Schubert 
8495796c8dcSSimon Schubert 	    case 2:
8505796c8dcSSimon Schubert 	      /* Carry in, no carry out */
8515796c8dcSSimon Schubert 	      octa1 = (carry << 2) | ((HIGH_TWO & *p) >> 6);
8525796c8dcSSimon Schubert 	      octa2 = (MID_TWO & *p) >> 3;
8535796c8dcSSimon Schubert 	      octa3 = (LOW_TWO & *p);
8545796c8dcSSimon Schubert 	      carry = 0;
8555796c8dcSSimon Schubert 	      fprintf_filtered (stream, "%o", octa1);
8565796c8dcSSimon Schubert 	      fprintf_filtered (stream, "%o", octa2);
8575796c8dcSSimon Schubert 	      fprintf_filtered (stream, "%o", octa3);
8585796c8dcSSimon Schubert 	      break;
8595796c8dcSSimon Schubert 
8605796c8dcSSimon Schubert 	    default:
8615796c8dcSSimon Schubert 	      error (_("Internal error in octal conversion;"));
8625796c8dcSSimon Schubert 	    }
8635796c8dcSSimon Schubert 
8645796c8dcSSimon Schubert 	  cycle++;
8655796c8dcSSimon Schubert 	  cycle = cycle % BITS_IN_OCTAL;
8665796c8dcSSimon Schubert 	}
8675796c8dcSSimon Schubert     }
8685796c8dcSSimon Schubert 
8695796c8dcSSimon Schubert }
8705796c8dcSSimon Schubert 
8715796c8dcSSimon Schubert /* VALADDR points to an integer of LEN bytes.
8725796c8dcSSimon Schubert  * Print it in decimal on stream or format it in buf.
8735796c8dcSSimon Schubert  */
8745796c8dcSSimon Schubert void
8755796c8dcSSimon Schubert print_decimal_chars (struct ui_file *stream, const gdb_byte *valaddr,
8765796c8dcSSimon Schubert 		     unsigned len, enum bfd_endian byte_order)
8775796c8dcSSimon Schubert {
8785796c8dcSSimon Schubert #define TEN             10
8795796c8dcSSimon Schubert #define CARRY_OUT(  x ) ((x) / TEN)	/* extend char to int */
8805796c8dcSSimon Schubert #define CARRY_LEFT( x ) ((x) % TEN)
8815796c8dcSSimon Schubert #define SHIFT( x )      ((x) << 4)
8825796c8dcSSimon Schubert #define LOW_NIBBLE(  x ) ( (x) & 0x00F)
8835796c8dcSSimon Schubert #define HIGH_NIBBLE( x ) (((x) & 0x0F0) >> 4)
8845796c8dcSSimon Schubert 
8855796c8dcSSimon Schubert   const gdb_byte *p;
8865796c8dcSSimon Schubert   unsigned char *digits;
8875796c8dcSSimon Schubert   int carry;
8885796c8dcSSimon Schubert   int decimal_len;
8895796c8dcSSimon Schubert   int i, j, decimal_digits;
8905796c8dcSSimon Schubert   int dummy;
8915796c8dcSSimon Schubert   int flip;
8925796c8dcSSimon Schubert 
8935796c8dcSSimon Schubert   /* Base-ten number is less than twice as many digits
8945796c8dcSSimon Schubert    * as the base 16 number, which is 2 digits per byte.
8955796c8dcSSimon Schubert    */
8965796c8dcSSimon Schubert   decimal_len = len * 2 * 2;
8975796c8dcSSimon Schubert   digits = xmalloc (decimal_len);
8985796c8dcSSimon Schubert 
8995796c8dcSSimon Schubert   for (i = 0; i < decimal_len; i++)
9005796c8dcSSimon Schubert     {
9015796c8dcSSimon Schubert       digits[i] = 0;
9025796c8dcSSimon Schubert     }
9035796c8dcSSimon Schubert 
9045796c8dcSSimon Schubert   /* Ok, we have an unknown number of bytes of data to be printed in
9055796c8dcSSimon Schubert    * decimal.
9065796c8dcSSimon Schubert    *
9075796c8dcSSimon Schubert    * Given a hex number (in nibbles) as XYZ, we start by taking X and
9085796c8dcSSimon Schubert    * decemalizing it as "x1 x2" in two decimal nibbles.  Then we multiply
9095796c8dcSSimon Schubert    * the nibbles by 16, add Y and re-decimalize.  Repeat with Z.
9105796c8dcSSimon Schubert    *
9115796c8dcSSimon Schubert    * The trick is that "digits" holds a base-10 number, but sometimes
9125796c8dcSSimon Schubert    * the individual digits are > 10.
9135796c8dcSSimon Schubert    *
9145796c8dcSSimon Schubert    * Outer loop is per nibble (hex digit) of input, from MSD end to
9155796c8dcSSimon Schubert    * LSD end.
9165796c8dcSSimon Schubert    */
9175796c8dcSSimon Schubert   decimal_digits = 0;		/* Number of decimal digits so far */
9185796c8dcSSimon Schubert   p = (byte_order == BFD_ENDIAN_BIG) ? valaddr : valaddr + len - 1;
9195796c8dcSSimon Schubert   flip = 0;
9205796c8dcSSimon Schubert   while ((byte_order == BFD_ENDIAN_BIG) ? (p < valaddr + len) : (p >= valaddr))
9215796c8dcSSimon Schubert     {
9225796c8dcSSimon Schubert       /*
9235796c8dcSSimon Schubert        * Multiply current base-ten number by 16 in place.
9245796c8dcSSimon Schubert        * Each digit was between 0 and 9, now is between
9255796c8dcSSimon Schubert        * 0 and 144.
9265796c8dcSSimon Schubert        */
9275796c8dcSSimon Schubert       for (j = 0; j < decimal_digits; j++)
9285796c8dcSSimon Schubert 	{
9295796c8dcSSimon Schubert 	  digits[j] = SHIFT (digits[j]);
9305796c8dcSSimon Schubert 	}
9315796c8dcSSimon Schubert 
9325796c8dcSSimon Schubert       /* Take the next nibble off the input and add it to what
9335796c8dcSSimon Schubert        * we've got in the LSB position.  Bottom 'digit' is now
9345796c8dcSSimon Schubert        * between 0 and 159.
9355796c8dcSSimon Schubert        *
9365796c8dcSSimon Schubert        * "flip" is used to run this loop twice for each byte.
9375796c8dcSSimon Schubert        */
9385796c8dcSSimon Schubert       if (flip == 0)
9395796c8dcSSimon Schubert 	{
9405796c8dcSSimon Schubert 	  /* Take top nibble.
9415796c8dcSSimon Schubert 	   */
9425796c8dcSSimon Schubert 	  digits[0] += HIGH_NIBBLE (*p);
9435796c8dcSSimon Schubert 	  flip = 1;
9445796c8dcSSimon Schubert 	}
9455796c8dcSSimon Schubert       else
9465796c8dcSSimon Schubert 	{
9475796c8dcSSimon Schubert 	  /* Take low nibble and bump our pointer "p".
9485796c8dcSSimon Schubert 	   */
9495796c8dcSSimon Schubert 	  digits[0] += LOW_NIBBLE (*p);
9505796c8dcSSimon Schubert           if (byte_order == BFD_ENDIAN_BIG)
9515796c8dcSSimon Schubert 	    p++;
9525796c8dcSSimon Schubert 	  else
9535796c8dcSSimon Schubert 	    p--;
9545796c8dcSSimon Schubert 	  flip = 0;
9555796c8dcSSimon Schubert 	}
9565796c8dcSSimon Schubert 
9575796c8dcSSimon Schubert       /* Re-decimalize.  We have to do this often enough
9585796c8dcSSimon Schubert        * that we don't overflow, but once per nibble is
9595796c8dcSSimon Schubert        * overkill.  Easier this way, though.  Note that the
9605796c8dcSSimon Schubert        * carry is often larger than 10 (e.g. max initial
9615796c8dcSSimon Schubert        * carry out of lowest nibble is 15, could bubble all
9625796c8dcSSimon Schubert        * the way up greater than 10).  So we have to do
9635796c8dcSSimon Schubert        * the carrying beyond the last current digit.
9645796c8dcSSimon Schubert        */
9655796c8dcSSimon Schubert       carry = 0;
9665796c8dcSSimon Schubert       for (j = 0; j < decimal_len - 1; j++)
9675796c8dcSSimon Schubert 	{
9685796c8dcSSimon Schubert 	  digits[j] += carry;
9695796c8dcSSimon Schubert 
9705796c8dcSSimon Schubert 	  /* "/" won't handle an unsigned char with
9715796c8dcSSimon Schubert 	   * a value that if signed would be negative.
9725796c8dcSSimon Schubert 	   * So extend to longword int via "dummy".
9735796c8dcSSimon Schubert 	   */
9745796c8dcSSimon Schubert 	  dummy = digits[j];
9755796c8dcSSimon Schubert 	  carry = CARRY_OUT (dummy);
9765796c8dcSSimon Schubert 	  digits[j] = CARRY_LEFT (dummy);
9775796c8dcSSimon Schubert 
9785796c8dcSSimon Schubert 	  if (j >= decimal_digits && carry == 0)
9795796c8dcSSimon Schubert 	    {
9805796c8dcSSimon Schubert 	      /*
9815796c8dcSSimon Schubert 	       * All higher digits are 0 and we
9825796c8dcSSimon Schubert 	       * no longer have a carry.
9835796c8dcSSimon Schubert 	       *
9845796c8dcSSimon Schubert 	       * Note: "j" is 0-based, "decimal_digits" is
9855796c8dcSSimon Schubert 	       *       1-based.
9865796c8dcSSimon Schubert 	       */
9875796c8dcSSimon Schubert 	      decimal_digits = j + 1;
9885796c8dcSSimon Schubert 	      break;
9895796c8dcSSimon Schubert 	    }
9905796c8dcSSimon Schubert 	}
9915796c8dcSSimon Schubert     }
9925796c8dcSSimon Schubert 
9935796c8dcSSimon Schubert   /* Ok, now "digits" is the decimal representation, with
9945796c8dcSSimon Schubert    * the "decimal_digits" actual digits.  Print!
9955796c8dcSSimon Schubert    */
9965796c8dcSSimon Schubert   for (i = decimal_digits - 1; i >= 0; i--)
9975796c8dcSSimon Schubert     {
9985796c8dcSSimon Schubert       fprintf_filtered (stream, "%1d", digits[i]);
9995796c8dcSSimon Schubert     }
10005796c8dcSSimon Schubert   xfree (digits);
10015796c8dcSSimon Schubert }
10025796c8dcSSimon Schubert 
10035796c8dcSSimon Schubert /* VALADDR points to an integer of LEN bytes.  Print it in hex on stream.  */
10045796c8dcSSimon Schubert 
10055796c8dcSSimon Schubert void
10065796c8dcSSimon Schubert print_hex_chars (struct ui_file *stream, const gdb_byte *valaddr,
10075796c8dcSSimon Schubert 		 unsigned len, enum bfd_endian byte_order)
10085796c8dcSSimon Schubert {
10095796c8dcSSimon Schubert   const gdb_byte *p;
10105796c8dcSSimon Schubert 
10115796c8dcSSimon Schubert   /* FIXME: We should be not printing leading zeroes in most cases.  */
10125796c8dcSSimon Schubert 
10135796c8dcSSimon Schubert   fputs_filtered ("0x", stream);
10145796c8dcSSimon Schubert   if (byte_order == BFD_ENDIAN_BIG)
10155796c8dcSSimon Schubert     {
10165796c8dcSSimon Schubert       for (p = valaddr;
10175796c8dcSSimon Schubert 	   p < valaddr + len;
10185796c8dcSSimon Schubert 	   p++)
10195796c8dcSSimon Schubert 	{
10205796c8dcSSimon Schubert 	  fprintf_filtered (stream, "%02x", *p);
10215796c8dcSSimon Schubert 	}
10225796c8dcSSimon Schubert     }
10235796c8dcSSimon Schubert   else
10245796c8dcSSimon Schubert     {
10255796c8dcSSimon Schubert       for (p = valaddr + len - 1;
10265796c8dcSSimon Schubert 	   p >= valaddr;
10275796c8dcSSimon Schubert 	   p--)
10285796c8dcSSimon Schubert 	{
10295796c8dcSSimon Schubert 	  fprintf_filtered (stream, "%02x", *p);
10305796c8dcSSimon Schubert 	}
10315796c8dcSSimon Schubert     }
10325796c8dcSSimon Schubert }
10335796c8dcSSimon Schubert 
10345796c8dcSSimon Schubert /* VALADDR points to a char integer of LEN bytes.  Print it out in appropriate language form on stream.
10355796c8dcSSimon Schubert    Omit any leading zero chars.  */
10365796c8dcSSimon Schubert 
10375796c8dcSSimon Schubert void
10385796c8dcSSimon Schubert print_char_chars (struct ui_file *stream, struct type *type,
10395796c8dcSSimon Schubert 		  const gdb_byte *valaddr,
10405796c8dcSSimon Schubert 		  unsigned len, enum bfd_endian byte_order)
10415796c8dcSSimon Schubert {
10425796c8dcSSimon Schubert   const gdb_byte *p;
10435796c8dcSSimon Schubert 
10445796c8dcSSimon Schubert   if (byte_order == BFD_ENDIAN_BIG)
10455796c8dcSSimon Schubert     {
10465796c8dcSSimon Schubert       p = valaddr;
10475796c8dcSSimon Schubert       while (p < valaddr + len - 1 && *p == 0)
10485796c8dcSSimon Schubert 	++p;
10495796c8dcSSimon Schubert 
10505796c8dcSSimon Schubert       while (p < valaddr + len)
10515796c8dcSSimon Schubert 	{
10525796c8dcSSimon Schubert 	  LA_EMIT_CHAR (*p, type, stream, '\'');
10535796c8dcSSimon Schubert 	  ++p;
10545796c8dcSSimon Schubert 	}
10555796c8dcSSimon Schubert     }
10565796c8dcSSimon Schubert   else
10575796c8dcSSimon Schubert     {
10585796c8dcSSimon Schubert       p = valaddr + len - 1;
10595796c8dcSSimon Schubert       while (p > valaddr && *p == 0)
10605796c8dcSSimon Schubert 	--p;
10615796c8dcSSimon Schubert 
10625796c8dcSSimon Schubert       while (p >= valaddr)
10635796c8dcSSimon Schubert 	{
10645796c8dcSSimon Schubert 	  LA_EMIT_CHAR (*p, type, stream, '\'');
10655796c8dcSSimon Schubert 	  --p;
10665796c8dcSSimon Schubert 	}
10675796c8dcSSimon Schubert     }
10685796c8dcSSimon Schubert }
10695796c8dcSSimon Schubert 
10705796c8dcSSimon Schubert /* Assuming TYPE is a simple, non-empty array type, compute its upper
10715796c8dcSSimon Schubert    and lower bound.  Save the low bound into LOW_BOUND if not NULL.
10725796c8dcSSimon Schubert    Save the high bound into HIGH_BOUND if not NULL.
10735796c8dcSSimon Schubert 
10745796c8dcSSimon Schubert    Return 1 if the operation was successful. Return zero otherwise,
10755796c8dcSSimon Schubert    in which case the values of LOW_BOUND and HIGH_BOUNDS are unmodified.
10765796c8dcSSimon Schubert 
1077*cf7f2e2dSJohn Marino    We now simply use get_discrete_bounds call to get the values
1078*cf7f2e2dSJohn Marino    of the low and high bounds.
1079*cf7f2e2dSJohn Marino    get_discrete_bounds can return three values:
1080*cf7f2e2dSJohn Marino    1, meaning that index is a range,
1081*cf7f2e2dSJohn Marino    0, meaning that index is a discrete type,
1082*cf7f2e2dSJohn Marino    or -1 for failure.  */
10835796c8dcSSimon Schubert 
10845796c8dcSSimon Schubert int
1085*cf7f2e2dSJohn Marino get_array_bounds (struct type *type, LONGEST *low_bound, LONGEST *high_bound)
10865796c8dcSSimon Schubert {
10875796c8dcSSimon Schubert   struct type *index = TYPE_INDEX_TYPE (type);
1088*cf7f2e2dSJohn Marino   LONGEST low = 0;
1089*cf7f2e2dSJohn Marino   LONGEST high = 0;
1090*cf7f2e2dSJohn Marino   int res;
10915796c8dcSSimon Schubert 
10925796c8dcSSimon Schubert   if (index == NULL)
10935796c8dcSSimon Schubert     return 0;
10945796c8dcSSimon Schubert 
1095*cf7f2e2dSJohn Marino   res = get_discrete_bounds (index, &low, &high);
1096*cf7f2e2dSJohn Marino   if (res == -1)
10975796c8dcSSimon Schubert     return 0;
10985796c8dcSSimon Schubert 
10995796c8dcSSimon Schubert   if (low_bound)
11005796c8dcSSimon Schubert     *low_bound = low;
11015796c8dcSSimon Schubert 
11025796c8dcSSimon Schubert   if (high_bound)
11035796c8dcSSimon Schubert     *high_bound = high;
11045796c8dcSSimon Schubert 
11055796c8dcSSimon Schubert   return 1;
11065796c8dcSSimon Schubert }
11075796c8dcSSimon Schubert 
11085796c8dcSSimon Schubert /* Print on STREAM using the given OPTIONS the index for the element
11095796c8dcSSimon Schubert    at INDEX of an array whose index type is INDEX_TYPE.  */
11105796c8dcSSimon Schubert 
11115796c8dcSSimon Schubert void
11125796c8dcSSimon Schubert maybe_print_array_index (struct type *index_type, LONGEST index,
11135796c8dcSSimon Schubert                          struct ui_file *stream,
11145796c8dcSSimon Schubert 			 const struct value_print_options *options)
11155796c8dcSSimon Schubert {
11165796c8dcSSimon Schubert   struct value *index_value;
11175796c8dcSSimon Schubert 
11185796c8dcSSimon Schubert   if (!options->print_array_indexes)
11195796c8dcSSimon Schubert     return;
11205796c8dcSSimon Schubert 
11215796c8dcSSimon Schubert   index_value = value_from_longest (index_type, index);
11225796c8dcSSimon Schubert 
11235796c8dcSSimon Schubert   LA_PRINT_ARRAY_INDEX (index_value, stream, options);
11245796c8dcSSimon Schubert }
11255796c8dcSSimon Schubert 
11265796c8dcSSimon Schubert /*  Called by various <lang>_val_print routines to print elements of an
11275796c8dcSSimon Schubert    array in the form "<elem1>, <elem2>, <elem3>, ...".
11285796c8dcSSimon Schubert 
11295796c8dcSSimon Schubert    (FIXME?)  Assumes array element separator is a comma, which is correct
11305796c8dcSSimon Schubert    for all languages currently handled.
11315796c8dcSSimon Schubert    (FIXME?)  Some languages have a notation for repeated array elements,
11325796c8dcSSimon Schubert    perhaps we should try to use that notation when appropriate.
11335796c8dcSSimon Schubert  */
11345796c8dcSSimon Schubert 
11355796c8dcSSimon Schubert void
11365796c8dcSSimon Schubert val_print_array_elements (struct type *type, const gdb_byte *valaddr,
11375796c8dcSSimon Schubert 			  CORE_ADDR address, struct ui_file *stream,
11385796c8dcSSimon Schubert 			  int recurse,
1139*cf7f2e2dSJohn Marino 			  const struct value *val,
11405796c8dcSSimon Schubert 			  const struct value_print_options *options,
11415796c8dcSSimon Schubert 			  unsigned int i)
11425796c8dcSSimon Schubert {
11435796c8dcSSimon Schubert   unsigned int things_printed = 0;
11445796c8dcSSimon Schubert   unsigned len;
11455796c8dcSSimon Schubert   struct type *elttype, *index_type;
11465796c8dcSSimon Schubert   unsigned eltlen;
11475796c8dcSSimon Schubert   /* Position of the array element we are examining to see
11485796c8dcSSimon Schubert      whether it is repeated.  */
11495796c8dcSSimon Schubert   unsigned int rep1;
11505796c8dcSSimon Schubert   /* Number of repetitions we have detected so far.  */
11515796c8dcSSimon Schubert   unsigned int reps;
1152*cf7f2e2dSJohn Marino   LONGEST low_bound_index = 0;
11535796c8dcSSimon Schubert 
11545796c8dcSSimon Schubert   elttype = TYPE_TARGET_TYPE (type);
11555796c8dcSSimon Schubert   eltlen = TYPE_LENGTH (check_typedef (elttype));
11565796c8dcSSimon Schubert   index_type = TYPE_INDEX_TYPE (type);
11575796c8dcSSimon Schubert 
11585796c8dcSSimon Schubert   /* Compute the number of elements in the array.  On most arrays,
11595796c8dcSSimon Schubert      the size of its elements is not zero, and so the number of elements
11605796c8dcSSimon Schubert      is simply the size of the array divided by the size of the elements.
11615796c8dcSSimon Schubert      But for arrays of elements whose size is zero, we need to look at
11625796c8dcSSimon Schubert      the bounds.  */
11635796c8dcSSimon Schubert   if (eltlen != 0)
11645796c8dcSSimon Schubert     len = TYPE_LENGTH (type) / eltlen;
11655796c8dcSSimon Schubert   else
11665796c8dcSSimon Schubert     {
1167*cf7f2e2dSJohn Marino       LONGEST low, hi;
1168*cf7f2e2dSJohn Marino 
11695796c8dcSSimon Schubert       if (get_array_bounds (type, &low, &hi))
11705796c8dcSSimon Schubert         len = hi - low + 1;
11715796c8dcSSimon Schubert       else
11725796c8dcSSimon Schubert         {
11735796c8dcSSimon Schubert           warning (_("unable to get bounds of array, assuming null array"));
11745796c8dcSSimon Schubert           len = 0;
11755796c8dcSSimon Schubert         }
11765796c8dcSSimon Schubert     }
11775796c8dcSSimon Schubert 
11785796c8dcSSimon Schubert   /* Get the array low bound.  This only makes sense if the array
11795796c8dcSSimon Schubert      has one or more element in it.  */
11805796c8dcSSimon Schubert   if (len > 0 && !get_array_bounds (type, &low_bound_index, NULL))
11815796c8dcSSimon Schubert     {
11825796c8dcSSimon Schubert       warning (_("unable to get low bound of array, using zero as default"));
11835796c8dcSSimon Schubert       low_bound_index = 0;
11845796c8dcSSimon Schubert     }
11855796c8dcSSimon Schubert 
11865796c8dcSSimon Schubert   annotate_array_section_begin (i, elttype);
11875796c8dcSSimon Schubert 
11885796c8dcSSimon Schubert   for (; i < len && things_printed < options->print_max; i++)
11895796c8dcSSimon Schubert     {
11905796c8dcSSimon Schubert       if (i != 0)
11915796c8dcSSimon Schubert 	{
11925796c8dcSSimon Schubert 	  if (options->prettyprint_arrays)
11935796c8dcSSimon Schubert 	    {
11945796c8dcSSimon Schubert 	      fprintf_filtered (stream, ",\n");
11955796c8dcSSimon Schubert 	      print_spaces_filtered (2 + 2 * recurse, stream);
11965796c8dcSSimon Schubert 	    }
11975796c8dcSSimon Schubert 	  else
11985796c8dcSSimon Schubert 	    {
11995796c8dcSSimon Schubert 	      fprintf_filtered (stream, ", ");
12005796c8dcSSimon Schubert 	    }
12015796c8dcSSimon Schubert 	}
12025796c8dcSSimon Schubert       wrap_here (n_spaces (2 + 2 * recurse));
12035796c8dcSSimon Schubert       maybe_print_array_index (index_type, i + low_bound_index,
12045796c8dcSSimon Schubert                                stream, options);
12055796c8dcSSimon Schubert 
12065796c8dcSSimon Schubert       rep1 = i + 1;
12075796c8dcSSimon Schubert       reps = 1;
12085796c8dcSSimon Schubert       while ((rep1 < len) &&
12095796c8dcSSimon Schubert 	     !memcmp (valaddr + i * eltlen, valaddr + rep1 * eltlen, eltlen))
12105796c8dcSSimon Schubert 	{
12115796c8dcSSimon Schubert 	  ++reps;
12125796c8dcSSimon Schubert 	  ++rep1;
12135796c8dcSSimon Schubert 	}
12145796c8dcSSimon Schubert 
12155796c8dcSSimon Schubert       if (reps > options->repeat_count_threshold)
12165796c8dcSSimon Schubert 	{
12175796c8dcSSimon Schubert 	  val_print (elttype, valaddr + i * eltlen, 0, address + i * eltlen,
1218*cf7f2e2dSJohn Marino 		     stream, recurse + 1, val, options, current_language);
12195796c8dcSSimon Schubert 	  annotate_elt_rep (reps);
12205796c8dcSSimon Schubert 	  fprintf_filtered (stream, " <repeats %u times>", reps);
12215796c8dcSSimon Schubert 	  annotate_elt_rep_end ();
12225796c8dcSSimon Schubert 
12235796c8dcSSimon Schubert 	  i = rep1 - 1;
12245796c8dcSSimon Schubert 	  things_printed += options->repeat_count_threshold;
12255796c8dcSSimon Schubert 	}
12265796c8dcSSimon Schubert       else
12275796c8dcSSimon Schubert 	{
12285796c8dcSSimon Schubert 	  val_print (elttype, valaddr + i * eltlen, 0, address + i * eltlen,
1229*cf7f2e2dSJohn Marino 		     stream, recurse + 1, val, options, current_language);
12305796c8dcSSimon Schubert 	  annotate_elt ();
12315796c8dcSSimon Schubert 	  things_printed++;
12325796c8dcSSimon Schubert 	}
12335796c8dcSSimon Schubert     }
12345796c8dcSSimon Schubert   annotate_array_section_end ();
12355796c8dcSSimon Schubert   if (i < len)
12365796c8dcSSimon Schubert     {
12375796c8dcSSimon Schubert       fprintf_filtered (stream, "...");
12385796c8dcSSimon Schubert     }
12395796c8dcSSimon Schubert }
12405796c8dcSSimon Schubert 
12415796c8dcSSimon Schubert /* Read LEN bytes of target memory at address MEMADDR, placing the
12425796c8dcSSimon Schubert    results in GDB's memory at MYADDR.  Returns a count of the bytes
12435796c8dcSSimon Schubert    actually read, and optionally an errno value in the location
12445796c8dcSSimon Schubert    pointed to by ERRNOPTR if ERRNOPTR is non-null. */
12455796c8dcSSimon Schubert 
12465796c8dcSSimon Schubert /* FIXME: cagney/1999-10-14: Only used by val_print_string.  Can this
12475796c8dcSSimon Schubert    function be eliminated.  */
12485796c8dcSSimon Schubert 
12495796c8dcSSimon Schubert static int
12505796c8dcSSimon Schubert partial_memory_read (CORE_ADDR memaddr, gdb_byte *myaddr, int len, int *errnoptr)
12515796c8dcSSimon Schubert {
12525796c8dcSSimon Schubert   int nread;			/* Number of bytes actually read. */
12535796c8dcSSimon Schubert   int errcode;			/* Error from last read. */
12545796c8dcSSimon Schubert 
12555796c8dcSSimon Schubert   /* First try a complete read. */
12565796c8dcSSimon Schubert   errcode = target_read_memory (memaddr, myaddr, len);
12575796c8dcSSimon Schubert   if (errcode == 0)
12585796c8dcSSimon Schubert     {
12595796c8dcSSimon Schubert       /* Got it all. */
12605796c8dcSSimon Schubert       nread = len;
12615796c8dcSSimon Schubert     }
12625796c8dcSSimon Schubert   else
12635796c8dcSSimon Schubert     {
12645796c8dcSSimon Schubert       /* Loop, reading one byte at a time until we get as much as we can. */
12655796c8dcSSimon Schubert       for (errcode = 0, nread = 0; len > 0 && errcode == 0; nread++, len--)
12665796c8dcSSimon Schubert 	{
12675796c8dcSSimon Schubert 	  errcode = target_read_memory (memaddr++, myaddr++, 1);
12685796c8dcSSimon Schubert 	}
12695796c8dcSSimon Schubert       /* If an error, the last read was unsuccessful, so adjust count. */
12705796c8dcSSimon Schubert       if (errcode != 0)
12715796c8dcSSimon Schubert 	{
12725796c8dcSSimon Schubert 	  nread--;
12735796c8dcSSimon Schubert 	}
12745796c8dcSSimon Schubert     }
12755796c8dcSSimon Schubert   if (errnoptr != NULL)
12765796c8dcSSimon Schubert     {
12775796c8dcSSimon Schubert       *errnoptr = errcode;
12785796c8dcSSimon Schubert     }
12795796c8dcSSimon Schubert   return (nread);
12805796c8dcSSimon Schubert }
12815796c8dcSSimon Schubert 
12825796c8dcSSimon Schubert /* Read a string from the inferior, at ADDR, with LEN characters of WIDTH bytes
12835796c8dcSSimon Schubert    each.  Fetch at most FETCHLIMIT characters.  BUFFER will be set to a newly
12845796c8dcSSimon Schubert    allocated buffer containing the string, which the caller is responsible to
12855796c8dcSSimon Schubert    free, and BYTES_READ will be set to the number of bytes read.  Returns 0 on
12865796c8dcSSimon Schubert    success, or errno on failure.
12875796c8dcSSimon Schubert 
12885796c8dcSSimon Schubert    If LEN > 0, reads exactly LEN characters (including eventual NULs in
12895796c8dcSSimon Schubert    the middle or end of the string).  If LEN is -1, stops at the first
12905796c8dcSSimon Schubert    null character (not necessarily the first null byte) up to a maximum
12915796c8dcSSimon Schubert    of FETCHLIMIT characters.  Set FETCHLIMIT to UINT_MAX to read as many
12925796c8dcSSimon Schubert    characters as possible from the string.
12935796c8dcSSimon Schubert 
12945796c8dcSSimon Schubert    Unless an exception is thrown, BUFFER will always be allocated, even on
12955796c8dcSSimon Schubert    failure.  In this case, some characters might have been read before the
12965796c8dcSSimon Schubert    failure happened.  Check BYTES_READ to recognize this situation.
12975796c8dcSSimon Schubert 
12985796c8dcSSimon Schubert    Note: There was a FIXME asking to make this code use target_read_string,
12995796c8dcSSimon Schubert    but this function is more general (can read past null characters, up to
13005796c8dcSSimon Schubert    given LEN). Besides, it is used much more often than target_read_string
13015796c8dcSSimon Schubert    so it is more tested.  Perhaps callers of target_read_string should use
13025796c8dcSSimon Schubert    this function instead?  */
13035796c8dcSSimon Schubert 
13045796c8dcSSimon Schubert int
13055796c8dcSSimon Schubert read_string (CORE_ADDR addr, int len, int width, unsigned int fetchlimit,
13065796c8dcSSimon Schubert 	     enum bfd_endian byte_order, gdb_byte **buffer, int *bytes_read)
13075796c8dcSSimon Schubert {
13085796c8dcSSimon Schubert   int found_nul;		/* Non-zero if we found the nul char.  */
13095796c8dcSSimon Schubert   int errcode;			/* Errno returned from bad reads.  */
13105796c8dcSSimon Schubert   unsigned int nfetch;		/* Chars to fetch / chars fetched.  */
13115796c8dcSSimon Schubert   unsigned int chunksize;	/* Size of each fetch, in chars.  */
13125796c8dcSSimon Schubert   gdb_byte *bufptr;		/* Pointer to next available byte in buffer.  */
13135796c8dcSSimon Schubert   gdb_byte *limit;		/* First location past end of fetch buffer.  */
13145796c8dcSSimon Schubert   struct cleanup *old_chain = NULL;	/* Top of the old cleanup chain.  */
13155796c8dcSSimon Schubert 
13165796c8dcSSimon Schubert   /* Decide how large of chunks to try to read in one operation.  This
13175796c8dcSSimon Schubert      is also pretty simple.  If LEN >= zero, then we want fetchlimit chars,
13185796c8dcSSimon Schubert      so we might as well read them all in one operation.  If LEN is -1, we
13195796c8dcSSimon Schubert      are looking for a NUL terminator to end the fetching, so we might as
13205796c8dcSSimon Schubert      well read in blocks that are large enough to be efficient, but not so
13215796c8dcSSimon Schubert      large as to be slow if fetchlimit happens to be large.  So we choose the
13225796c8dcSSimon Schubert      minimum of 8 and fetchlimit.  We used to use 200 instead of 8 but
13235796c8dcSSimon Schubert      200 is way too big for remote debugging over a serial line.  */
13245796c8dcSSimon Schubert 
13255796c8dcSSimon Schubert   chunksize = (len == -1 ? min (8, fetchlimit) : fetchlimit);
13265796c8dcSSimon Schubert 
13275796c8dcSSimon Schubert   /* Loop until we either have all the characters, or we encounter
13285796c8dcSSimon Schubert      some error, such as bumping into the end of the address space.  */
13295796c8dcSSimon Schubert 
13305796c8dcSSimon Schubert   found_nul = 0;
13315796c8dcSSimon Schubert   *buffer = NULL;
13325796c8dcSSimon Schubert 
13335796c8dcSSimon Schubert   old_chain = make_cleanup (free_current_contents, buffer);
13345796c8dcSSimon Schubert 
13355796c8dcSSimon Schubert   if (len > 0)
13365796c8dcSSimon Schubert     {
13375796c8dcSSimon Schubert       *buffer = (gdb_byte *) xmalloc (len * width);
13385796c8dcSSimon Schubert       bufptr = *buffer;
13395796c8dcSSimon Schubert 
13405796c8dcSSimon Schubert       nfetch = partial_memory_read (addr, bufptr, len * width, &errcode)
13415796c8dcSSimon Schubert 	/ width;
13425796c8dcSSimon Schubert       addr += nfetch * width;
13435796c8dcSSimon Schubert       bufptr += nfetch * width;
13445796c8dcSSimon Schubert     }
13455796c8dcSSimon Schubert   else if (len == -1)
13465796c8dcSSimon Schubert     {
13475796c8dcSSimon Schubert       unsigned long bufsize = 0;
13485796c8dcSSimon Schubert 
13495796c8dcSSimon Schubert       do
13505796c8dcSSimon Schubert 	{
13515796c8dcSSimon Schubert 	  QUIT;
13525796c8dcSSimon Schubert 	  nfetch = min (chunksize, fetchlimit - bufsize);
13535796c8dcSSimon Schubert 
13545796c8dcSSimon Schubert 	  if (*buffer == NULL)
13555796c8dcSSimon Schubert 	    *buffer = (gdb_byte *) xmalloc (nfetch * width);
13565796c8dcSSimon Schubert 	  else
13575796c8dcSSimon Schubert 	    *buffer = (gdb_byte *) xrealloc (*buffer,
13585796c8dcSSimon Schubert 					     (nfetch + bufsize) * width);
13595796c8dcSSimon Schubert 
13605796c8dcSSimon Schubert 	  bufptr = *buffer + bufsize * width;
13615796c8dcSSimon Schubert 	  bufsize += nfetch;
13625796c8dcSSimon Schubert 
13635796c8dcSSimon Schubert 	  /* Read as much as we can.  */
13645796c8dcSSimon Schubert 	  nfetch = partial_memory_read (addr, bufptr, nfetch * width, &errcode)
13655796c8dcSSimon Schubert 		    / width;
13665796c8dcSSimon Schubert 
13675796c8dcSSimon Schubert 	  /* Scan this chunk for the null character that terminates the string
13685796c8dcSSimon Schubert 	     to print.  If found, we don't need to fetch any more.  Note
13695796c8dcSSimon Schubert 	     that bufptr is explicitly left pointing at the next character
13705796c8dcSSimon Schubert 	     after the null character, or at the next character after the end
13715796c8dcSSimon Schubert 	     of the buffer.  */
13725796c8dcSSimon Schubert 
13735796c8dcSSimon Schubert 	  limit = bufptr + nfetch * width;
13745796c8dcSSimon Schubert 	  while (bufptr < limit)
13755796c8dcSSimon Schubert 	    {
13765796c8dcSSimon Schubert 	      unsigned long c;
13775796c8dcSSimon Schubert 
13785796c8dcSSimon Schubert 	      c = extract_unsigned_integer (bufptr, width, byte_order);
13795796c8dcSSimon Schubert 	      addr += width;
13805796c8dcSSimon Schubert 	      bufptr += width;
13815796c8dcSSimon Schubert 	      if (c == 0)
13825796c8dcSSimon Schubert 		{
13835796c8dcSSimon Schubert 		  /* We don't care about any error which happened after
13845796c8dcSSimon Schubert 		     the NUL terminator.  */
13855796c8dcSSimon Schubert 		  errcode = 0;
13865796c8dcSSimon Schubert 		  found_nul = 1;
13875796c8dcSSimon Schubert 		  break;
13885796c8dcSSimon Schubert 		}
13895796c8dcSSimon Schubert 	    }
13905796c8dcSSimon Schubert 	}
13915796c8dcSSimon Schubert       while (errcode == 0	/* no error */
13925796c8dcSSimon Schubert 	     && bufptr - *buffer < fetchlimit * width	/* no overrun */
13935796c8dcSSimon Schubert 	     && !found_nul);	/* haven't found NUL yet */
13945796c8dcSSimon Schubert     }
13955796c8dcSSimon Schubert   else
13965796c8dcSSimon Schubert     {				/* Length of string is really 0!  */
13975796c8dcSSimon Schubert       /* We always allocate *buffer.  */
13985796c8dcSSimon Schubert       *buffer = bufptr = xmalloc (1);
13995796c8dcSSimon Schubert       errcode = 0;
14005796c8dcSSimon Schubert     }
14015796c8dcSSimon Schubert 
14025796c8dcSSimon Schubert   /* bufptr and addr now point immediately beyond the last byte which we
14035796c8dcSSimon Schubert      consider part of the string (including a '\0' which ends the string).  */
14045796c8dcSSimon Schubert   *bytes_read = bufptr - *buffer;
14055796c8dcSSimon Schubert 
14065796c8dcSSimon Schubert   QUIT;
14075796c8dcSSimon Schubert 
14085796c8dcSSimon Schubert   discard_cleanups (old_chain);
14095796c8dcSSimon Schubert 
14105796c8dcSSimon Schubert   return errcode;
14115796c8dcSSimon Schubert }
14125796c8dcSSimon Schubert 
14135796c8dcSSimon Schubert /* Print a string from the inferior, starting at ADDR and printing up to LEN
14145796c8dcSSimon Schubert    characters, of WIDTH bytes a piece, to STREAM.  If LEN is -1, printing
14155796c8dcSSimon Schubert    stops at the first null byte, otherwise printing proceeds (including null
14165796c8dcSSimon Schubert    bytes) until either print_max or LEN characters have been printed,
14175796c8dcSSimon Schubert    whichever is smaller.  */
14185796c8dcSSimon Schubert 
14195796c8dcSSimon Schubert int
14205796c8dcSSimon Schubert val_print_string (struct type *elttype, CORE_ADDR addr, int len,
14215796c8dcSSimon Schubert 		  struct ui_file *stream,
14225796c8dcSSimon Schubert 		  const struct value_print_options *options)
14235796c8dcSSimon Schubert {
14245796c8dcSSimon Schubert   int force_ellipsis = 0;	/* Force ellipsis to be printed if nonzero.  */
14255796c8dcSSimon Schubert   int errcode;			/* Errno returned from bad reads.  */
14265796c8dcSSimon Schubert   int found_nul;		/* Non-zero if we found the nul char */
14275796c8dcSSimon Schubert   unsigned int fetchlimit;	/* Maximum number of chars to print.  */
14285796c8dcSSimon Schubert   int bytes_read;
14295796c8dcSSimon Schubert   gdb_byte *buffer = NULL;	/* Dynamically growable fetch buffer.  */
14305796c8dcSSimon Schubert   struct cleanup *old_chain = NULL;	/* Top of the old cleanup chain.  */
14315796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_type_arch (elttype);
14325796c8dcSSimon Schubert   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
14335796c8dcSSimon Schubert   int width = TYPE_LENGTH (elttype);
14345796c8dcSSimon Schubert 
14355796c8dcSSimon Schubert   /* First we need to figure out the limit on the number of characters we are
14365796c8dcSSimon Schubert      going to attempt to fetch and print.  This is actually pretty simple.  If
14375796c8dcSSimon Schubert      LEN >= zero, then the limit is the minimum of LEN and print_max.  If
14385796c8dcSSimon Schubert      LEN is -1, then the limit is print_max.  This is true regardless of
14395796c8dcSSimon Schubert      whether print_max is zero, UINT_MAX (unlimited), or something in between,
14405796c8dcSSimon Schubert      because finding the null byte (or available memory) is what actually
14415796c8dcSSimon Schubert      limits the fetch.  */
14425796c8dcSSimon Schubert 
14435796c8dcSSimon Schubert   fetchlimit = (len == -1 ? options->print_max : min (len, options->print_max));
14445796c8dcSSimon Schubert 
14455796c8dcSSimon Schubert   errcode = read_string (addr, len, width, fetchlimit, byte_order,
14465796c8dcSSimon Schubert 			 &buffer, &bytes_read);
14475796c8dcSSimon Schubert   old_chain = make_cleanup (xfree, buffer);
14485796c8dcSSimon Schubert 
14495796c8dcSSimon Schubert   addr += bytes_read;
14505796c8dcSSimon Schubert 
14515796c8dcSSimon Schubert   /* We now have either successfully filled the buffer to fetchlimit, or
14525796c8dcSSimon Schubert      terminated early due to an error or finding a null char when LEN is -1.  */
14535796c8dcSSimon Schubert 
14545796c8dcSSimon Schubert   /* Determine found_nul by looking at the last character read.  */
14555796c8dcSSimon Schubert   found_nul = extract_unsigned_integer (buffer + bytes_read - width, width,
14565796c8dcSSimon Schubert 					byte_order) == 0;
14575796c8dcSSimon Schubert   if (len == -1 && !found_nul)
14585796c8dcSSimon Schubert     {
14595796c8dcSSimon Schubert       gdb_byte *peekbuf;
14605796c8dcSSimon Schubert 
14615796c8dcSSimon Schubert       /* We didn't find a NUL terminator we were looking for.  Attempt
14625796c8dcSSimon Schubert          to peek at the next character.  If not successful, or it is not
14635796c8dcSSimon Schubert          a null byte, then force ellipsis to be printed.  */
14645796c8dcSSimon Schubert 
14655796c8dcSSimon Schubert       peekbuf = (gdb_byte *) alloca (width);
14665796c8dcSSimon Schubert 
14675796c8dcSSimon Schubert       if (target_read_memory (addr, peekbuf, width) == 0
14685796c8dcSSimon Schubert 	  && extract_unsigned_integer (peekbuf, width, byte_order) != 0)
14695796c8dcSSimon Schubert 	force_ellipsis = 1;
14705796c8dcSSimon Schubert     }
14715796c8dcSSimon Schubert   else if ((len >= 0 && errcode != 0) || (len > bytes_read / width))
14725796c8dcSSimon Schubert     {
14735796c8dcSSimon Schubert       /* Getting an error when we have a requested length, or fetching less
14745796c8dcSSimon Schubert          than the number of characters actually requested, always make us
14755796c8dcSSimon Schubert          print ellipsis.  */
14765796c8dcSSimon Schubert       force_ellipsis = 1;
14775796c8dcSSimon Schubert     }
14785796c8dcSSimon Schubert 
14795796c8dcSSimon Schubert   /* If we get an error before fetching anything, don't print a string.
14805796c8dcSSimon Schubert      But if we fetch something and then get an error, print the string
14815796c8dcSSimon Schubert      and then the error message.  */
14825796c8dcSSimon Schubert   if (errcode == 0 || bytes_read > 0)
14835796c8dcSSimon Schubert     {
14845796c8dcSSimon Schubert       if (options->addressprint)
14855796c8dcSSimon Schubert 	{
14865796c8dcSSimon Schubert 	  fputs_filtered (" ", stream);
14875796c8dcSSimon Schubert 	}
1488*cf7f2e2dSJohn Marino       LA_PRINT_STRING (stream, elttype, buffer, bytes_read / width,
1489*cf7f2e2dSJohn Marino 		       NULL, force_ellipsis, options);
14905796c8dcSSimon Schubert     }
14915796c8dcSSimon Schubert 
14925796c8dcSSimon Schubert   if (errcode != 0)
14935796c8dcSSimon Schubert     {
14945796c8dcSSimon Schubert       if (errcode == EIO)
14955796c8dcSSimon Schubert 	{
14965796c8dcSSimon Schubert 	  fprintf_filtered (stream, " <Address ");
14975796c8dcSSimon Schubert 	  fputs_filtered (paddress (gdbarch, addr), stream);
14985796c8dcSSimon Schubert 	  fprintf_filtered (stream, " out of bounds>");
14995796c8dcSSimon Schubert 	}
15005796c8dcSSimon Schubert       else
15015796c8dcSSimon Schubert 	{
15025796c8dcSSimon Schubert 	  fprintf_filtered (stream, " <Error reading address ");
15035796c8dcSSimon Schubert 	  fputs_filtered (paddress (gdbarch, addr), stream);
15045796c8dcSSimon Schubert 	  fprintf_filtered (stream, ": %s>", safe_strerror (errcode));
15055796c8dcSSimon Schubert 	}
15065796c8dcSSimon Schubert     }
15075796c8dcSSimon Schubert 
15085796c8dcSSimon Schubert   gdb_flush (stream);
15095796c8dcSSimon Schubert   do_cleanups (old_chain);
15105796c8dcSSimon Schubert 
15115796c8dcSSimon Schubert   return (bytes_read / width);
15125796c8dcSSimon Schubert }
15135796c8dcSSimon Schubert 
15145796c8dcSSimon Schubert 
15155796c8dcSSimon Schubert /* The 'set input-radix' command writes to this auxiliary variable.
15165796c8dcSSimon Schubert    If the requested radix is valid, INPUT_RADIX is updated; otherwise,
15175796c8dcSSimon Schubert    it is left unchanged.  */
15185796c8dcSSimon Schubert 
15195796c8dcSSimon Schubert static unsigned input_radix_1 = 10;
15205796c8dcSSimon Schubert 
15215796c8dcSSimon Schubert /* Validate an input or output radix setting, and make sure the user
15225796c8dcSSimon Schubert    knows what they really did here.  Radix setting is confusing, e.g.
15235796c8dcSSimon Schubert    setting the input radix to "10" never changes it!  */
15245796c8dcSSimon Schubert 
15255796c8dcSSimon Schubert static void
15265796c8dcSSimon Schubert set_input_radix (char *args, int from_tty, struct cmd_list_element *c)
15275796c8dcSSimon Schubert {
15285796c8dcSSimon Schubert   set_input_radix_1 (from_tty, input_radix_1);
15295796c8dcSSimon Schubert }
15305796c8dcSSimon Schubert 
15315796c8dcSSimon Schubert static void
15325796c8dcSSimon Schubert set_input_radix_1 (int from_tty, unsigned radix)
15335796c8dcSSimon Schubert {
15345796c8dcSSimon Schubert   /* We don't currently disallow any input radix except 0 or 1, which don't
15355796c8dcSSimon Schubert      make any mathematical sense.  In theory, we can deal with any input
15365796c8dcSSimon Schubert      radix greater than 1, even if we don't have unique digits for every
15375796c8dcSSimon Schubert      value from 0 to radix-1, but in practice we lose on large radix values.
15385796c8dcSSimon Schubert      We should either fix the lossage or restrict the radix range more.
15395796c8dcSSimon Schubert      (FIXME). */
15405796c8dcSSimon Schubert 
15415796c8dcSSimon Schubert   if (radix < 2)
15425796c8dcSSimon Schubert     {
15435796c8dcSSimon Schubert       input_radix_1 = input_radix;
15445796c8dcSSimon Schubert       error (_("Nonsense input radix ``decimal %u''; input radix unchanged."),
15455796c8dcSSimon Schubert 	     radix);
15465796c8dcSSimon Schubert     }
15475796c8dcSSimon Schubert   input_radix_1 = input_radix = radix;
15485796c8dcSSimon Schubert   if (from_tty)
15495796c8dcSSimon Schubert     {
15505796c8dcSSimon Schubert       printf_filtered (_("Input radix now set to decimal %u, hex %x, octal %o.\n"),
15515796c8dcSSimon Schubert 		       radix, radix, radix);
15525796c8dcSSimon Schubert     }
15535796c8dcSSimon Schubert }
15545796c8dcSSimon Schubert 
15555796c8dcSSimon Schubert /* The 'set output-radix' command writes to this auxiliary variable.
15565796c8dcSSimon Schubert    If the requested radix is valid, OUTPUT_RADIX is updated,
15575796c8dcSSimon Schubert    otherwise, it is left unchanged.  */
15585796c8dcSSimon Schubert 
15595796c8dcSSimon Schubert static unsigned output_radix_1 = 10;
15605796c8dcSSimon Schubert 
15615796c8dcSSimon Schubert static void
15625796c8dcSSimon Schubert set_output_radix (char *args, int from_tty, struct cmd_list_element *c)
15635796c8dcSSimon Schubert {
15645796c8dcSSimon Schubert   set_output_radix_1 (from_tty, output_radix_1);
15655796c8dcSSimon Schubert }
15665796c8dcSSimon Schubert 
15675796c8dcSSimon Schubert static void
15685796c8dcSSimon Schubert set_output_radix_1 (int from_tty, unsigned radix)
15695796c8dcSSimon Schubert {
15705796c8dcSSimon Schubert   /* Validate the radix and disallow ones that we aren't prepared to
15715796c8dcSSimon Schubert      handle correctly, leaving the radix unchanged. */
15725796c8dcSSimon Schubert   switch (radix)
15735796c8dcSSimon Schubert     {
15745796c8dcSSimon Schubert     case 16:
15755796c8dcSSimon Schubert       user_print_options.output_format = 'x';	/* hex */
15765796c8dcSSimon Schubert       break;
15775796c8dcSSimon Schubert     case 10:
15785796c8dcSSimon Schubert       user_print_options.output_format = 0;	/* decimal */
15795796c8dcSSimon Schubert       break;
15805796c8dcSSimon Schubert     case 8:
15815796c8dcSSimon Schubert       user_print_options.output_format = 'o';	/* octal */
15825796c8dcSSimon Schubert       break;
15835796c8dcSSimon Schubert     default:
15845796c8dcSSimon Schubert       output_radix_1 = output_radix;
15855796c8dcSSimon Schubert       error (_("Unsupported output radix ``decimal %u''; output radix unchanged."),
15865796c8dcSSimon Schubert 	     radix);
15875796c8dcSSimon Schubert     }
15885796c8dcSSimon Schubert   output_radix_1 = output_radix = radix;
15895796c8dcSSimon Schubert   if (from_tty)
15905796c8dcSSimon Schubert     {
15915796c8dcSSimon Schubert       printf_filtered (_("Output radix now set to decimal %u, hex %x, octal %o.\n"),
15925796c8dcSSimon Schubert 		       radix, radix, radix);
15935796c8dcSSimon Schubert     }
15945796c8dcSSimon Schubert }
15955796c8dcSSimon Schubert 
15965796c8dcSSimon Schubert /* Set both the input and output radix at once.  Try to set the output radix
15975796c8dcSSimon Schubert    first, since it has the most restrictive range.  An radix that is valid as
15985796c8dcSSimon Schubert    an output radix is also valid as an input radix.
15995796c8dcSSimon Schubert 
16005796c8dcSSimon Schubert    It may be useful to have an unusual input radix.  If the user wishes to
16015796c8dcSSimon Schubert    set an input radix that is not valid as an output radix, he needs to use
16025796c8dcSSimon Schubert    the 'set input-radix' command. */
16035796c8dcSSimon Schubert 
16045796c8dcSSimon Schubert static void
16055796c8dcSSimon Schubert set_radix (char *arg, int from_tty)
16065796c8dcSSimon Schubert {
16075796c8dcSSimon Schubert   unsigned radix;
16085796c8dcSSimon Schubert 
16095796c8dcSSimon Schubert   radix = (arg == NULL) ? 10 : parse_and_eval_long (arg);
16105796c8dcSSimon Schubert   set_output_radix_1 (0, radix);
16115796c8dcSSimon Schubert   set_input_radix_1 (0, radix);
16125796c8dcSSimon Schubert   if (from_tty)
16135796c8dcSSimon Schubert     {
16145796c8dcSSimon Schubert       printf_filtered (_("Input and output radices now set to decimal %u, hex %x, octal %o.\n"),
16155796c8dcSSimon Schubert 		       radix, radix, radix);
16165796c8dcSSimon Schubert     }
16175796c8dcSSimon Schubert }
16185796c8dcSSimon Schubert 
16195796c8dcSSimon Schubert /* Show both the input and output radices. */
16205796c8dcSSimon Schubert 
16215796c8dcSSimon Schubert static void
16225796c8dcSSimon Schubert show_radix (char *arg, int from_tty)
16235796c8dcSSimon Schubert {
16245796c8dcSSimon Schubert   if (from_tty)
16255796c8dcSSimon Schubert     {
16265796c8dcSSimon Schubert       if (input_radix == output_radix)
16275796c8dcSSimon Schubert 	{
16285796c8dcSSimon Schubert 	  printf_filtered (_("Input and output radices set to decimal %u, hex %x, octal %o.\n"),
16295796c8dcSSimon Schubert 			   input_radix, input_radix, input_radix);
16305796c8dcSSimon Schubert 	}
16315796c8dcSSimon Schubert       else
16325796c8dcSSimon Schubert 	{
16335796c8dcSSimon Schubert 	  printf_filtered (_("Input radix set to decimal %u, hex %x, octal %o.\n"),
16345796c8dcSSimon Schubert 			   input_radix, input_radix, input_radix);
16355796c8dcSSimon Schubert 	  printf_filtered (_("Output radix set to decimal %u, hex %x, octal %o.\n"),
16365796c8dcSSimon Schubert 			   output_radix, output_radix, output_radix);
16375796c8dcSSimon Schubert 	}
16385796c8dcSSimon Schubert     }
16395796c8dcSSimon Schubert }
16405796c8dcSSimon Schubert 
16415796c8dcSSimon Schubert 
16425796c8dcSSimon Schubert static void
16435796c8dcSSimon Schubert set_print (char *arg, int from_tty)
16445796c8dcSSimon Schubert {
16455796c8dcSSimon Schubert   printf_unfiltered (
16465796c8dcSSimon Schubert      "\"set print\" must be followed by the name of a print subcommand.\n");
16475796c8dcSSimon Schubert   help_list (setprintlist, "set print ", -1, gdb_stdout);
16485796c8dcSSimon Schubert }
16495796c8dcSSimon Schubert 
16505796c8dcSSimon Schubert static void
16515796c8dcSSimon Schubert show_print (char *args, int from_tty)
16525796c8dcSSimon Schubert {
16535796c8dcSSimon Schubert   cmd_show_list (showprintlist, from_tty, "");
16545796c8dcSSimon Schubert }
16555796c8dcSSimon Schubert 
16565796c8dcSSimon Schubert void
16575796c8dcSSimon Schubert _initialize_valprint (void)
16585796c8dcSSimon Schubert {
16595796c8dcSSimon Schubert   add_prefix_cmd ("print", no_class, set_print,
16605796c8dcSSimon Schubert 		  _("Generic command for setting how things print."),
16615796c8dcSSimon Schubert 		  &setprintlist, "set print ", 0, &setlist);
16625796c8dcSSimon Schubert   add_alias_cmd ("p", "print", no_class, 1, &setlist);
16635796c8dcSSimon Schubert   /* prefer set print to set prompt */
16645796c8dcSSimon Schubert   add_alias_cmd ("pr", "print", no_class, 1, &setlist);
16655796c8dcSSimon Schubert 
16665796c8dcSSimon Schubert   add_prefix_cmd ("print", no_class, show_print,
16675796c8dcSSimon Schubert 		  _("Generic command for showing print settings."),
16685796c8dcSSimon Schubert 		  &showprintlist, "show print ", 0, &showlist);
16695796c8dcSSimon Schubert   add_alias_cmd ("p", "print", no_class, 1, &showlist);
16705796c8dcSSimon Schubert   add_alias_cmd ("pr", "print", no_class, 1, &showlist);
16715796c8dcSSimon Schubert 
16725796c8dcSSimon Schubert   add_setshow_uinteger_cmd ("elements", no_class,
16735796c8dcSSimon Schubert 			    &user_print_options.print_max, _("\
16745796c8dcSSimon Schubert Set limit on string chars or array elements to print."), _("\
16755796c8dcSSimon Schubert Show limit on string chars or array elements to print."), _("\
16765796c8dcSSimon Schubert \"set print elements 0\" causes there to be no limit."),
16775796c8dcSSimon Schubert 			    NULL,
16785796c8dcSSimon Schubert 			    show_print_max,
16795796c8dcSSimon Schubert 			    &setprintlist, &showprintlist);
16805796c8dcSSimon Schubert 
16815796c8dcSSimon Schubert   add_setshow_boolean_cmd ("null-stop", no_class,
16825796c8dcSSimon Schubert 			   &user_print_options.stop_print_at_null, _("\
16835796c8dcSSimon Schubert Set printing of char arrays to stop at first null char."), _("\
16845796c8dcSSimon Schubert Show printing of char arrays to stop at first null char."), NULL,
16855796c8dcSSimon Schubert 			   NULL,
16865796c8dcSSimon Schubert 			   show_stop_print_at_null,
16875796c8dcSSimon Schubert 			   &setprintlist, &showprintlist);
16885796c8dcSSimon Schubert 
16895796c8dcSSimon Schubert   add_setshow_uinteger_cmd ("repeats", no_class,
16905796c8dcSSimon Schubert 			    &user_print_options.repeat_count_threshold, _("\
16915796c8dcSSimon Schubert Set threshold for repeated print elements."), _("\
16925796c8dcSSimon Schubert Show threshold for repeated print elements."), _("\
16935796c8dcSSimon Schubert \"set print repeats 0\" causes all elements to be individually printed."),
16945796c8dcSSimon Schubert 			    NULL,
16955796c8dcSSimon Schubert 			    show_repeat_count_threshold,
16965796c8dcSSimon Schubert 			    &setprintlist, &showprintlist);
16975796c8dcSSimon Schubert 
16985796c8dcSSimon Schubert   add_setshow_boolean_cmd ("pretty", class_support,
16995796c8dcSSimon Schubert 			   &user_print_options.prettyprint_structs, _("\
17005796c8dcSSimon Schubert Set prettyprinting of structures."), _("\
17015796c8dcSSimon Schubert Show prettyprinting of structures."), NULL,
17025796c8dcSSimon Schubert 			   NULL,
17035796c8dcSSimon Schubert 			   show_prettyprint_structs,
17045796c8dcSSimon Schubert 			   &setprintlist, &showprintlist);
17055796c8dcSSimon Schubert 
17065796c8dcSSimon Schubert   add_setshow_boolean_cmd ("union", class_support,
17075796c8dcSSimon Schubert 			   &user_print_options.unionprint, _("\
17085796c8dcSSimon Schubert Set printing of unions interior to structures."), _("\
17095796c8dcSSimon Schubert Show printing of unions interior to structures."), NULL,
17105796c8dcSSimon Schubert 			   NULL,
17115796c8dcSSimon Schubert 			   show_unionprint,
17125796c8dcSSimon Schubert 			   &setprintlist, &showprintlist);
17135796c8dcSSimon Schubert 
17145796c8dcSSimon Schubert   add_setshow_boolean_cmd ("array", class_support,
17155796c8dcSSimon Schubert 			   &user_print_options.prettyprint_arrays, _("\
17165796c8dcSSimon Schubert Set prettyprinting of arrays."), _("\
17175796c8dcSSimon Schubert Show prettyprinting of arrays."), NULL,
17185796c8dcSSimon Schubert 			   NULL,
17195796c8dcSSimon Schubert 			   show_prettyprint_arrays,
17205796c8dcSSimon Schubert 			   &setprintlist, &showprintlist);
17215796c8dcSSimon Schubert 
17225796c8dcSSimon Schubert   add_setshow_boolean_cmd ("address", class_support,
17235796c8dcSSimon Schubert 			   &user_print_options.addressprint, _("\
17245796c8dcSSimon Schubert Set printing of addresses."), _("\
17255796c8dcSSimon Schubert Show printing of addresses."), NULL,
17265796c8dcSSimon Schubert 			   NULL,
17275796c8dcSSimon Schubert 			   show_addressprint,
17285796c8dcSSimon Schubert 			   &setprintlist, &showprintlist);
17295796c8dcSSimon Schubert 
17305796c8dcSSimon Schubert   add_setshow_zuinteger_cmd ("input-radix", class_support, &input_radix_1,
17315796c8dcSSimon Schubert 			     _("\
17325796c8dcSSimon Schubert Set default input radix for entering numbers."), _("\
17335796c8dcSSimon Schubert Show default input radix for entering numbers."), NULL,
17345796c8dcSSimon Schubert 			     set_input_radix,
17355796c8dcSSimon Schubert 			     show_input_radix,
17365796c8dcSSimon Schubert 			     &setlist, &showlist);
17375796c8dcSSimon Schubert 
17385796c8dcSSimon Schubert   add_setshow_zuinteger_cmd ("output-radix", class_support, &output_radix_1,
17395796c8dcSSimon Schubert 			     _("\
17405796c8dcSSimon Schubert Set default output radix for printing of values."), _("\
17415796c8dcSSimon Schubert Show default output radix for printing of values."), NULL,
17425796c8dcSSimon Schubert 			     set_output_radix,
17435796c8dcSSimon Schubert 			     show_output_radix,
17445796c8dcSSimon Schubert 			     &setlist, &showlist);
17455796c8dcSSimon Schubert 
17465796c8dcSSimon Schubert   /* The "set radix" and "show radix" commands are special in that
17475796c8dcSSimon Schubert      they are like normal set and show commands but allow two normally
17485796c8dcSSimon Schubert      independent variables to be either set or shown with a single
17495796c8dcSSimon Schubert      command.  So the usual deprecated_add_set_cmd() and [deleted]
17505796c8dcSSimon Schubert      add_show_from_set() commands aren't really appropriate. */
17515796c8dcSSimon Schubert   /* FIXME: i18n: With the new add_setshow_integer command, that is no
17525796c8dcSSimon Schubert      longer true - show can display anything.  */
17535796c8dcSSimon Schubert   add_cmd ("radix", class_support, set_radix, _("\
17545796c8dcSSimon Schubert Set default input and output number radices.\n\
17555796c8dcSSimon Schubert Use 'set input-radix' or 'set output-radix' to independently set each.\n\
17565796c8dcSSimon Schubert Without an argument, sets both radices back to the default value of 10."),
17575796c8dcSSimon Schubert 	   &setlist);
17585796c8dcSSimon Schubert   add_cmd ("radix", class_support, show_radix, _("\
17595796c8dcSSimon Schubert Show the default input and output number radices.\n\
17605796c8dcSSimon Schubert Use 'show input-radix' or 'show output-radix' to independently show each."),
17615796c8dcSSimon Schubert 	   &showlist);
17625796c8dcSSimon Schubert 
17635796c8dcSSimon Schubert   add_setshow_boolean_cmd ("array-indexes", class_support,
17645796c8dcSSimon Schubert                            &user_print_options.print_array_indexes, _("\
17655796c8dcSSimon Schubert Set printing of array indexes."), _("\
17665796c8dcSSimon Schubert Show printing of array indexes"), NULL, NULL, show_print_array_indexes,
17675796c8dcSSimon Schubert                            &setprintlist, &showprintlist);
17685796c8dcSSimon Schubert }
1769