119370Spst/* Print values for GDB, the GNU debugger.
219370Spst
3130803Smarcel   Copyright 1986, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
4130803Smarcel   1996, 1997, 1998, 1999, 2000, 2001, 2002 Free Software Foundation,
5130803Smarcel   Inc.
6130803Smarcel
798944Sobrien   This file is part of GDB.
819370Spst
998944Sobrien   This program is free software; you can redistribute it and/or modify
1098944Sobrien   it under the terms of the GNU General Public License as published by
1198944Sobrien   the Free Software Foundation; either version 2 of the License, or
1298944Sobrien   (at your option) any later version.
1319370Spst
1498944Sobrien   This program is distributed in the hope that it will be useful,
1598944Sobrien   but WITHOUT ANY WARRANTY; without even the implied warranty of
1698944Sobrien   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1798944Sobrien   GNU General Public License for more details.
1819370Spst
1998944Sobrien   You should have received a copy of the GNU General Public License
2098944Sobrien   along with this program; if not, write to the Free Software
2198944Sobrien   Foundation, Inc., 59 Temple Place - Suite 330,
2298944Sobrien   Boston, MA 02111-1307, USA.  */
2319370Spst
2419370Spst#include "defs.h"
2519370Spst#include "gdb_string.h"
2619370Spst#include "symtab.h"
2719370Spst#include "gdbtypes.h"
2819370Spst#include "value.h"
2919370Spst#include "gdbcore.h"
3019370Spst#include "gdbcmd.h"
3119370Spst#include "target.h"
3219370Spst#include "language.h"
3319370Spst#include "annotate.h"
3446283Sdfr#include "valprint.h"
3598944Sobrien#include "floatformat.h"
3698944Sobrien#include "doublest.h"
3719370Spst
3819370Spst#include <errno.h>
3919370Spst
4019370Spst/* Prototypes for local functions */
4119370Spst
4298944Sobrienstatic int partial_memory_read (CORE_ADDR memaddr, char *myaddr,
4398944Sobrien				int len, int *errnoptr);
4419370Spst
4598944Sobrienstatic void show_print (char *, int);
4619370Spst
4798944Sobrienstatic void set_print (char *, int);
4819370Spst
4998944Sobrienstatic void set_radix (char *, int);
5019370Spst
5198944Sobrienstatic void show_radix (char *, int);
5219370Spst
5398944Sobrienstatic void set_input_radix (char *, int, struct cmd_list_element *);
5419370Spst
5598944Sobrienstatic void set_input_radix_1 (int, unsigned);
5619370Spst
5798944Sobrienstatic void set_output_radix (char *, int, struct cmd_list_element *);
5819370Spst
5998944Sobrienstatic void set_output_radix_1 (int, unsigned);
6046283Sdfr
6198944Sobrienvoid _initialize_valprint (void);
6298944Sobrien
6319370Spst/* Maximum number of chars to print for a string pointer value or vector
6419370Spst   contents, or UINT_MAX for no limit.  Note that "set print elements 0"
6519370Spst   stores UINT_MAX in print_max, which displays in a show command as
6619370Spst   "unlimited". */
6719370Spst
6819370Spstunsigned int print_max;
6919370Spst#define PRINT_MAX_DEFAULT 200	/* Start print_max off at this value. */
7019370Spst
7119370Spst/* Default input and output radixes, and output format letter.  */
7219370Spst
7319370Spstunsigned input_radix = 10;
7419370Spstunsigned output_radix = 10;
7519370Spstint output_format = 0;
7619370Spst
7719370Spst/* Print repeat counts if there are more than this many repetitions of an
7819370Spst   element in an array.  Referenced by the low level language dependent
7919370Spst   print routines. */
8019370Spst
8119370Spstunsigned int repeat_count_threshold = 10;
8219370Spst
8319370Spst/* If nonzero, stops printing of char arrays at first null. */
8419370Spst
8519370Spstint stop_print_at_null;
8619370Spst
8719370Spst/* Controls pretty printing of structures. */
8819370Spst
8919370Spstint prettyprint_structs;
9019370Spst
9119370Spst/* Controls pretty printing of arrays.  */
9219370Spst
9319370Spstint prettyprint_arrays;
9419370Spst
9519370Spst/* If nonzero, causes unions inside structures or other unions to be
9619370Spst   printed. */
9719370Spst
9819370Spstint unionprint;			/* Controls printing of nested unions.  */
9919370Spst
10019370Spst/* If nonzero, causes machine addresses to be printed in certain contexts. */
10119370Spst
10219370Spstint addressprint;		/* Controls printing of machine addresses */
10398944Sobrien
10419370Spst
10519370Spst/* Print data of type TYPE located at VALADDR (within GDB), which came from
10619370Spst   the inferior at address ADDRESS, onto stdio stream STREAM according to
10719370Spst   FORMAT (a letter, or 0 for natural format using TYPE).
10819370Spst
10919370Spst   If DEREF_REF is nonzero, then dereference references, otherwise just print
11019370Spst   them like pointers.
11119370Spst
11219370Spst   The PRETTY parameter controls prettyprinting.
11319370Spst
11419370Spst   If the data are a string pointer, returns the number of string characters
11519370Spst   printed.
11619370Spst
11719370Spst   FIXME:  The data at VALADDR is in target byte order.  If gdb is ever
11819370Spst   enhanced to be able to debug more than the single target it was compiled
11919370Spst   for (specific CPU type and thus specific target byte ordering), then
12019370Spst   either the print routines are going to have to take this into account,
12119370Spst   or the data is going to have to be passed into here already converted
12219370Spst   to the host byte ordering, whichever is more convenient. */
12319370Spst
12419370Spst
12519370Spstint
12698944Sobrienval_print (struct type *type, char *valaddr, int embedded_offset,
12798944Sobrien	   CORE_ADDR address, struct ui_file *stream, int format, int deref_ref,
12898944Sobrien	   int recurse, enum val_prettyprint pretty)
12919370Spst{
13019370Spst  struct type *real_type = check_typedef (type);
13119370Spst  if (pretty == Val_pretty_default)
13219370Spst    {
13319370Spst      pretty = prettyprint_structs ? Val_prettyprint : Val_no_prettyprint;
13419370Spst    }
13598944Sobrien
13619370Spst  QUIT;
13719370Spst
13819370Spst  /* Ensure that the type is complete and not just a stub.  If the type is
13919370Spst     only a stub and we can't find and substitute its complete type, then
14019370Spst     print appropriate string and return.  */
14119370Spst
14298944Sobrien  if (TYPE_STUB (real_type))
14319370Spst    {
14419370Spst      fprintf_filtered (stream, "<incomplete type>");
14519370Spst      gdb_flush (stream);
14619370Spst      return (0);
14719370Spst    }
14898944Sobrien
14946283Sdfr  return (LA_VAL_PRINT (type, valaddr, embedded_offset, address,
15098944Sobrien			stream, format, deref_ref, recurse, pretty));
15119370Spst}
15219370Spst
153242936Semaste/* Check whether the value VAL is printable.  Return 1 if it is;
154242936Semaste   return 0 and print an appropriate error message to STREAM if it
155242936Semaste   is not.  */
15619370Spst
157242936Semastestatic int
158242936Semastevalue_check_printable (struct value *val, struct ui_file *stream)
15919370Spst{
16019370Spst  if (val == 0)
16119370Spst    {
162242936Semaste      fprintf_filtered (stream, "<address of value unknown>");
16319370Spst      return 0;
16419370Spst    }
165242936Semaste
16619370Spst  if (VALUE_OPTIMIZED_OUT (val))
16719370Spst    {
168242936Semaste      fprintf_filtered (stream, "<value optimized out>");
16919370Spst      return 0;
17019370Spst    }
171242936Semaste
172242936Semaste  return 1;
173242936Semaste}
174242936Semaste
175242936Semaste/* Print the value VAL onto stream STREAM according to FORMAT (a
176242936Semaste   letter, or 0 for natural format using TYPE).
177242936Semaste
178242936Semaste   If DEREF_REF is nonzero, then dereference references, otherwise just print
179242936Semaste   them like pointers.
180242936Semaste
181242936Semaste   The PRETTY parameter controls prettyprinting.
182242936Semaste
183242936Semaste   If the data are a string pointer, returns the number of string characters
184242936Semaste   printed.
185242936Semaste
186242936Semaste   This is a preferable interface to val_print, above, because it uses
187242936Semaste   GDB's value mechanism.  */
188242936Semaste
189242936Semasteint
190242936Semastecommon_val_print (struct value *val, struct ui_file *stream, int format,
191242936Semaste		  int deref_ref, int recurse, enum val_prettyprint pretty)
192242936Semaste{
193242936Semaste  if (!value_check_printable (val, stream))
194242936Semaste    return 0;
195242936Semaste
196242936Semaste  return val_print (VALUE_TYPE(val), VALUE_CONTENTS_ALL (val),
197242936Semaste		    VALUE_EMBEDDED_OFFSET (val), VALUE_ADDRESS (val),
198242936Semaste		    stream, format, deref_ref, recurse, pretty);
199242936Semaste}
200242936Semaste
201242936Semaste/* Print the value VAL in C-ish syntax on stream STREAM.
202242936Semaste   FORMAT is a format-letter, or 0 for print in natural format of data type.
203242936Semaste   If the object printed is a string pointer, returns
204242936Semaste   the number of string bytes printed.  */
205242936Semaste
206242936Semasteint
207242936Semastevalue_print (struct value *val, struct ui_file *stream, int format,
208242936Semaste	     enum val_prettyprint pretty)
209242936Semaste{
210242936Semaste  if (!value_check_printable (val, stream))
211242936Semaste    return 0;
212242936Semaste
21319370Spst  return LA_VALUE_PRINT (val, stream, format, pretty);
21419370Spst}
21519370Spst
21619370Spst/* Called by various <lang>_val_print routines to print
21719370Spst   TYPE_CODE_INT's.  TYPE is the type.  VALADDR is the address of the
21819370Spst   value.  STREAM is where to print the value.  */
21919370Spst
22019370Spstvoid
22198944Sobrienval_print_type_code_int (struct type *type, char *valaddr,
22298944Sobrien			 struct ui_file *stream)
22319370Spst{
22419370Spst  if (TYPE_LENGTH (type) > sizeof (LONGEST))
22519370Spst    {
22619370Spst      LONGEST val;
22719370Spst
22819370Spst      if (TYPE_UNSIGNED (type)
22919370Spst	  && extract_long_unsigned_integer (valaddr, TYPE_LENGTH (type),
23019370Spst					    &val))
23119370Spst	{
23219370Spst	  print_longest (stream, 'u', 0, val);
23319370Spst	}
23419370Spst      else
23519370Spst	{
23619370Spst	  /* Signed, or we couldn't turn an unsigned value into a
23719370Spst	     LONGEST.  For signed values, one could assume two's
23819370Spst	     complement (a reasonable assumption, I think) and do
23919370Spst	     better than this.  */
24019370Spst	  print_hex_chars (stream, (unsigned char *) valaddr,
24119370Spst			   TYPE_LENGTH (type));
24219370Spst	}
24319370Spst    }
24419370Spst  else
24519370Spst    {
24619370Spst      print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0,
24719370Spst		     unpack_long (type, valaddr));
24819370Spst    }
24919370Spst}
25019370Spst
25119370Spst/* Print a number according to FORMAT which is one of d,u,x,o,b,h,w,g.
25246283Sdfr   The raison d'etre of this function is to consolidate printing of
25346283Sdfr   LONG_LONG's into this one function.  Some platforms have long longs but
25446283Sdfr   don't have a printf() that supports "ll" in the format string.  We handle
25546283Sdfr   these by seeing if the number is representable as either a signed or
25646283Sdfr   unsigned long, depending upon what format is desired, and if not we just
25746283Sdfr   bail out and print the number in hex.
25819370Spst
25946283Sdfr   The format chars b,h,w,g are from print_scalar_formatted().  If USE_LOCAL,
26046283Sdfr   format it according to the current language (this should be used for most
26146283Sdfr   integers which GDB prints, the exception is things like protocols where
26246283Sdfr   the format of the integer is a protocol thing, not a user-visible thing).
26398944Sobrien */
26446283Sdfr
26546283Sdfr#if defined (CC_HAS_LONG_LONG) && !defined (PRINTF_HAS_LONG_LONG)
26698944Sobrienstatic void print_decimal (struct ui_file * stream, char *sign,
26798944Sobrien			   int use_local, ULONGEST val_ulong);
26846283Sdfrstatic void
26998944Sobrienprint_decimal (struct ui_file *stream, char *sign, int use_local,
27098944Sobrien	       ULONGEST val_ulong)
27146283Sdfr{
27246283Sdfr  unsigned long temp[3];
27346283Sdfr  int i = 0;
27446283Sdfr  do
27546283Sdfr    {
27646283Sdfr      temp[i] = val_ulong % (1000 * 1000 * 1000);
27746283Sdfr      val_ulong /= (1000 * 1000 * 1000);
27846283Sdfr      i++;
27946283Sdfr    }
28046283Sdfr  while (val_ulong != 0 && i < (sizeof (temp) / sizeof (temp[0])));
28146283Sdfr  switch (i)
28246283Sdfr    {
28346283Sdfr    case 1:
28446283Sdfr      fprintf_filtered (stream, "%s%lu",
28546283Sdfr			sign, temp[0]);
28646283Sdfr      break;
28746283Sdfr    case 2:
28846283Sdfr      fprintf_filtered (stream, "%s%lu%09lu",
28946283Sdfr			sign, temp[1], temp[0]);
29046283Sdfr      break;
29146283Sdfr    case 3:
29246283Sdfr      fprintf_filtered (stream, "%s%lu%09lu%09lu",
29346283Sdfr			sign, temp[2], temp[1], temp[0]);
29446283Sdfr      break;
29546283Sdfr    default:
29698944Sobrien      internal_error (__FILE__, __LINE__, "failed internal consistency check");
29746283Sdfr    }
29846283Sdfr  return;
29946283Sdfr}
30046283Sdfr#endif
30146283Sdfr
30219370Spstvoid
30398944Sobrienprint_longest (struct ui_file *stream, int format, int use_local,
30498944Sobrien	       LONGEST val_long)
30519370Spst{
30619370Spst#if defined (CC_HAS_LONG_LONG) && !defined (PRINTF_HAS_LONG_LONG)
30746283Sdfr  if (sizeof (long) < sizeof (LONGEST))
30819370Spst    {
30946283Sdfr      switch (format)
31046283Sdfr	{
31146283Sdfr	case 'd':
31246283Sdfr	  {
31346283Sdfr	    /* Print a signed value, that doesn't fit in a long */
31446283Sdfr	    if ((long) val_long != val_long)
31546283Sdfr	      {
31646283Sdfr		if (val_long < 0)
31746283Sdfr		  print_decimal (stream, "-", use_local, -val_long);
31846283Sdfr		else
31946283Sdfr		  print_decimal (stream, "", use_local, val_long);
32046283Sdfr		return;
32146283Sdfr	      }
32246283Sdfr	    break;
32346283Sdfr	  }
32446283Sdfr	case 'u':
32546283Sdfr	  {
32646283Sdfr	    /* Print an unsigned value, that doesn't fit in a long */
32746283Sdfr	    if ((unsigned long) val_long != (ULONGEST) val_long)
32846283Sdfr	      {
32946283Sdfr		print_decimal (stream, "", use_local, val_long);
33046283Sdfr		return;
33146283Sdfr	      }
33246283Sdfr	    break;
33346283Sdfr	  }
33446283Sdfr	case 'x':
33546283Sdfr	case 'o':
33646283Sdfr	case 'b':
33746283Sdfr	case 'h':
33846283Sdfr	case 'w':
33946283Sdfr	case 'g':
34046283Sdfr	  /* Print as unsigned value, must fit completely in unsigned long */
34146283Sdfr	  {
34246283Sdfr	    unsigned long temp = val_long;
34346283Sdfr	    if (temp != val_long)
34446283Sdfr	      {
34546283Sdfr		/* Urk, can't represent value in long so print in hex.
34646283Sdfr		   Do shift in two operations so that if sizeof (long)
34746283Sdfr		   == sizeof (LONGEST) we can avoid warnings from
34846283Sdfr		   picky compilers about shifts >= the size of the
34946283Sdfr		   shiftee in bits */
35046283Sdfr		unsigned long vbot = (unsigned long) val_long;
35146283Sdfr		LONGEST temp = (val_long >> (sizeof (long) * HOST_CHAR_BIT - 1));
35246283Sdfr		unsigned long vtop = temp >> 1;
35346283Sdfr		fprintf_filtered (stream, "0x%lx%08lx", vtop, vbot);
35446283Sdfr		return;
35546283Sdfr	      }
35646283Sdfr	    break;
35746283Sdfr	  }
35846283Sdfr	}
35919370Spst    }
36019370Spst#endif
36119370Spst
36246283Sdfr#if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
36319370Spst  switch (format)
36419370Spst    {
36519370Spst    case 'd':
36619370Spst      fprintf_filtered (stream,
36719370Spst			use_local ? local_decimal_format_custom ("ll")
36898944Sobrien			: "%lld",
369130803Smarcel			(long long) val_long);
37019370Spst      break;
37119370Spst    case 'u':
372130803Smarcel      fprintf_filtered (stream, "%llu", (long long) val_long);
37319370Spst      break;
37419370Spst    case 'x':
37519370Spst      fprintf_filtered (stream,
37619370Spst			use_local ? local_hex_format_custom ("ll")
37798944Sobrien			: "%llx",
378130803Smarcel			(unsigned long long) val_long);
37919370Spst      break;
38019370Spst    case 'o':
38119370Spst      fprintf_filtered (stream,
38219370Spst			use_local ? local_octal_format_custom ("ll")
38398944Sobrien			: "%llo",
384130803Smarcel			(unsigned long long) val_long);
38519370Spst      break;
38619370Spst    case 'b':
38719370Spst      fprintf_filtered (stream, local_hex_format_custom ("02ll"), val_long);
38819370Spst      break;
38919370Spst    case 'h':
39019370Spst      fprintf_filtered (stream, local_hex_format_custom ("04ll"), val_long);
39119370Spst      break;
39219370Spst    case 'w':
39319370Spst      fprintf_filtered (stream, local_hex_format_custom ("08ll"), val_long);
39419370Spst      break;
39519370Spst    case 'g':
39619370Spst      fprintf_filtered (stream, local_hex_format_custom ("016ll"), val_long);
39719370Spst      break;
39819370Spst    default:
39998944Sobrien      internal_error (__FILE__, __LINE__, "failed internal consistency check");
40019370Spst    }
40198944Sobrien#else /* !CC_HAS_LONG_LONG || !PRINTF_HAS_LONG_LONG */
40219370Spst  /* In the following it is important to coerce (val_long) to a long. It does
40319370Spst     nothing if !LONG_LONG, but it will chop off the top half (which we know
40419370Spst     we can ignore) if the host supports long longs.  */
40519370Spst
40619370Spst  switch (format)
40719370Spst    {
40819370Spst    case 'd':
40919370Spst      fprintf_filtered (stream,
41019370Spst			use_local ? local_decimal_format_custom ("l")
41198944Sobrien			: "%ld",
41219370Spst			(long) val_long);
41319370Spst      break;
41419370Spst    case 'u':
41519370Spst      fprintf_filtered (stream, "%lu", (unsigned long) val_long);
41619370Spst      break;
41719370Spst    case 'x':
41819370Spst      fprintf_filtered (stream,
41919370Spst			use_local ? local_hex_format_custom ("l")
42098944Sobrien			: "%lx",
42146283Sdfr			(unsigned long) val_long);
42219370Spst      break;
42319370Spst    case 'o':
42419370Spst      fprintf_filtered (stream,
42519370Spst			use_local ? local_octal_format_custom ("l")
42698944Sobrien			: "%lo",
42746283Sdfr			(unsigned long) val_long);
42819370Spst      break;
42919370Spst    case 'b':
43019370Spst      fprintf_filtered (stream, local_hex_format_custom ("02l"),
43146283Sdfr			(unsigned long) val_long);
43219370Spst      break;
43319370Spst    case 'h':
43419370Spst      fprintf_filtered (stream, local_hex_format_custom ("04l"),
43546283Sdfr			(unsigned long) val_long);
43619370Spst      break;
43719370Spst    case 'w':
43819370Spst      fprintf_filtered (stream, local_hex_format_custom ("08l"),
43946283Sdfr			(unsigned long) val_long);
44019370Spst      break;
44119370Spst    case 'g':
44219370Spst      fprintf_filtered (stream, local_hex_format_custom ("016l"),
44346283Sdfr			(unsigned long) val_long);
44419370Spst      break;
44519370Spst    default:
44698944Sobrien      internal_error (__FILE__, __LINE__, "failed internal consistency check");
44719370Spst    }
44846283Sdfr#endif /* CC_HAS_LONG_LONG || PRINTF_HAS_LONG_LONG */
44946283Sdfr}
45046283Sdfr
45119370Spst/* This used to be a macro, but I don't think it is called often enough
45219370Spst   to merit such treatment.  */
45319370Spst/* Convert a LONGEST to an int.  This is used in contexts (e.g. number of
45419370Spst   arguments to a function, number in a value history, register number, etc.)
45519370Spst   where the value must not be larger than can fit in an int.  */
45619370Spst
45719370Spstint
45898944Sobrienlongest_to_int (LONGEST arg)
45919370Spst{
46046283Sdfr  /* Let the compiler do the work */
46146283Sdfr  int rtnval = (int) arg;
46219370Spst
46346283Sdfr  /* Check for overflows or underflows */
46446283Sdfr  if (sizeof (LONGEST) > sizeof (int))
46546283Sdfr    {
46646283Sdfr      if (rtnval != arg)
46746283Sdfr	{
46846283Sdfr	  error ("Value out of range.");
46946283Sdfr	}
47046283Sdfr    }
47146283Sdfr  return (rtnval);
47219370Spst}
47319370Spst
47498944Sobrien/* Print a floating point value of type TYPE (not always a
47598944Sobrien   TYPE_CODE_FLT), pointed to in GDB by VALADDR, on STREAM.  */
47619370Spst
47719370Spstvoid
47898944Sobrienprint_floating (char *valaddr, struct type *type, struct ui_file *stream)
47919370Spst{
48019370Spst  DOUBLEST doub;
48119370Spst  int inv;
48298944Sobrien  const struct floatformat *fmt = NULL;
48319370Spst  unsigned len = TYPE_LENGTH (type);
48419370Spst
48598944Sobrien  /* If it is a floating-point, check for obvious problems.  */
48698944Sobrien  if (TYPE_CODE (type) == TYPE_CODE_FLT)
48798944Sobrien    fmt = floatformat_from_type (type);
48898944Sobrien  if (fmt != NULL && floatformat_is_nan (fmt, valaddr))
48998944Sobrien    {
49098944Sobrien      if (floatformat_is_negative (fmt, valaddr))
49198944Sobrien	fprintf_filtered (stream, "-");
49298944Sobrien      fprintf_filtered (stream, "nan(");
493130803Smarcel      fputs_filtered (local_hex_format_prefix (), stream);
494130803Smarcel      fputs_filtered (floatformat_mantissa (fmt, valaddr), stream);
495130803Smarcel      fputs_filtered (local_hex_format_suffix (), stream);
49698944Sobrien      fprintf_filtered (stream, ")");
49798944Sobrien      return;
49898944Sobrien    }
49919370Spst
50098944Sobrien  /* NOTE: cagney/2002-01-15: The TYPE passed into print_floating()
50198944Sobrien     isn't necessarily a TYPE_CODE_FLT.  Consequently, unpack_double
50298944Sobrien     needs to be used as that takes care of any necessary type
50398944Sobrien     conversions.  Such conversions are of course direct to DOUBLEST
50498944Sobrien     and disregard any possible target floating point limitations.
50598944Sobrien     For instance, a u64 would be converted and displayed exactly on a
50698944Sobrien     host with 80 bit DOUBLEST but with loss of information on a host
50798944Sobrien     with 64 bit DOUBLEST.  */
50819370Spst
50919370Spst  doub = unpack_double (type, valaddr, &inv);
51019370Spst  if (inv)
51119370Spst    {
51219370Spst      fprintf_filtered (stream, "<invalid float value>");
51319370Spst      return;
51419370Spst    }
51519370Spst
51698944Sobrien  /* FIXME: kettenis/2001-01-20: The following code makes too much
51798944Sobrien     assumptions about the host and target floating point format.  */
51898944Sobrien
51998944Sobrien  /* NOTE: cagney/2002-02-03: Since the TYPE of what was passed in may
52098944Sobrien     not necessarially be a TYPE_CODE_FLT, the below ignores that and
52198944Sobrien     instead uses the type's length to determine the precision of the
52298944Sobrien     floating-point value being printed.  */
52398944Sobrien
52419370Spst  if (len < sizeof (double))
52598944Sobrien      fprintf_filtered (stream, "%.9g", (double) doub);
52619370Spst  else if (len == sizeof (double))
52798944Sobrien      fprintf_filtered (stream, "%.17g", (double) doub);
52819370Spst  else
52919370Spst#ifdef PRINTF_HAS_LONG_DOUBLE
53019370Spst    fprintf_filtered (stream, "%.35Lg", doub);
53119370Spst#else
53298944Sobrien    /* This at least wins with values that are representable as
53398944Sobrien       doubles.  */
53419370Spst    fprintf_filtered (stream, "%.17g", (double) doub);
53519370Spst#endif
53619370Spst}
53719370Spst
53898944Sobrienvoid
53998944Sobrienprint_binary_chars (struct ui_file *stream, unsigned char *valaddr,
54098944Sobrien		    unsigned len)
54146283Sdfr{
54246283Sdfr
54346283Sdfr#define BITS_IN_BYTES 8
54446283Sdfr
54546283Sdfr  unsigned char *p;
54698944Sobrien  unsigned int i;
54798944Sobrien  int b;
54846283Sdfr
54946283Sdfr  /* Declared "int" so it will be signed.
55046283Sdfr   * This ensures that right shift will shift in zeros.
55146283Sdfr   */
55298944Sobrien  const int mask = 0x080;
55346283Sdfr
55446283Sdfr  /* FIXME: We should be not printing leading zeroes in most cases.  */
55546283Sdfr
556130803Smarcel  fputs_filtered (local_binary_format_prefix (), stream);
55798944Sobrien  if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
55846283Sdfr    {
55946283Sdfr      for (p = valaddr;
56046283Sdfr	   p < valaddr + len;
56146283Sdfr	   p++)
56246283Sdfr	{
56398944Sobrien	  /* Every byte has 8 binary characters; peel off
56498944Sobrien	   * and print from the MSB end.
56598944Sobrien	   */
56698944Sobrien	  for (i = 0; i < (BITS_IN_BYTES * sizeof (*p)); i++)
56798944Sobrien	    {
56898944Sobrien	      if (*p & (mask >> i))
56998944Sobrien		b = 1;
57098944Sobrien	      else
57198944Sobrien		b = 0;
57246283Sdfr
57398944Sobrien	      fprintf_filtered (stream, "%1d", b);
57498944Sobrien	    }
57546283Sdfr	}
57646283Sdfr    }
57746283Sdfr  else
57846283Sdfr    {
57946283Sdfr      for (p = valaddr + len - 1;
58046283Sdfr	   p >= valaddr;
58146283Sdfr	   p--)
58246283Sdfr	{
58398944Sobrien	  for (i = 0; i < (BITS_IN_BYTES * sizeof (*p)); i++)
58498944Sobrien	    {
58598944Sobrien	      if (*p & (mask >> i))
58698944Sobrien		b = 1;
58798944Sobrien	      else
58898944Sobrien		b = 0;
58946283Sdfr
59098944Sobrien	      fprintf_filtered (stream, "%1d", b);
59198944Sobrien	    }
59246283Sdfr	}
59346283Sdfr    }
594130803Smarcel  fputs_filtered (local_binary_format_suffix (), stream);
59546283Sdfr}
59646283Sdfr
59746283Sdfr/* VALADDR points to an integer of LEN bytes.
59846283Sdfr * Print it in octal on stream or format it in buf.
59946283Sdfr */
60046283Sdfrvoid
60198944Sobrienprint_octal_chars (struct ui_file *stream, unsigned char *valaddr, unsigned len)
60246283Sdfr{
60346283Sdfr  unsigned char *p;
60446283Sdfr  unsigned char octa1, octa2, octa3, carry;
60598944Sobrien  int cycle;
60698944Sobrien
60746283Sdfr  /* FIXME: We should be not printing leading zeroes in most cases.  */
60846283Sdfr
60946283Sdfr
61046283Sdfr  /* Octal is 3 bits, which doesn't fit.  Yuk.  So we have to track
61146283Sdfr   * the extra bits, which cycle every three bytes:
61246283Sdfr   *
61346283Sdfr   * Byte side:       0            1             2          3
61446283Sdfr   *                         |             |            |            |
61546283Sdfr   * bit number   123 456 78 | 9 012 345 6 | 78 901 234 | 567 890 12 |
61646283Sdfr   *
61746283Sdfr   * Octal side:   0   1   carry  3   4  carry ...
61846283Sdfr   *
61946283Sdfr   * Cycle number:    0             1            2
62046283Sdfr   *
62146283Sdfr   * But of course we are printing from the high side, so we have to
62246283Sdfr   * figure out where in the cycle we are so that we end up with no
62346283Sdfr   * left over bits at the end.
62446283Sdfr   */
62546283Sdfr#define BITS_IN_OCTAL 3
62646283Sdfr#define HIGH_ZERO     0340
62746283Sdfr#define LOW_ZERO      0016
62846283Sdfr#define CARRY_ZERO    0003
62946283Sdfr#define HIGH_ONE      0200
63046283Sdfr#define MID_ONE       0160
63146283Sdfr#define LOW_ONE       0016
63246283Sdfr#define CARRY_ONE     0001
63346283Sdfr#define HIGH_TWO      0300
63446283Sdfr#define MID_TWO       0070
63546283Sdfr#define LOW_TWO       0007
63646283Sdfr
63746283Sdfr  /* For 32 we start in cycle 2, with two bits and one bit carry;
63846283Sdfr   * for 64 in cycle in cycle 1, with one bit and a two bit carry.
63946283Sdfr   */
64046283Sdfr  cycle = (len * BITS_IN_BYTES) % BITS_IN_OCTAL;
64146283Sdfr  carry = 0;
64298944Sobrien
643130803Smarcel  fputs_filtered (local_octal_format_prefix (), stream);
64498944Sobrien  if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
64546283Sdfr    {
64646283Sdfr      for (p = valaddr;
64746283Sdfr	   p < valaddr + len;
64846283Sdfr	   p++)
64946283Sdfr	{
65098944Sobrien	  switch (cycle)
65198944Sobrien	    {
65298944Sobrien	    case 0:
65398944Sobrien	      /* No carry in, carry out two bits.
65498944Sobrien	       */
65598944Sobrien	      octa1 = (HIGH_ZERO & *p) >> 5;
65698944Sobrien	      octa2 = (LOW_ZERO & *p) >> 2;
65798944Sobrien	      carry = (CARRY_ZERO & *p);
65898944Sobrien	      fprintf_filtered (stream, "%o", octa1);
65998944Sobrien	      fprintf_filtered (stream, "%o", octa2);
66098944Sobrien	      break;
66146283Sdfr
66298944Sobrien	    case 1:
66398944Sobrien	      /* Carry in two bits, carry out one bit.
66498944Sobrien	       */
66598944Sobrien	      octa1 = (carry << 1) | ((HIGH_ONE & *p) >> 7);
66698944Sobrien	      octa2 = (MID_ONE & *p) >> 4;
66798944Sobrien	      octa3 = (LOW_ONE & *p) >> 1;
66898944Sobrien	      carry = (CARRY_ONE & *p);
66998944Sobrien	      fprintf_filtered (stream, "%o", octa1);
67098944Sobrien	      fprintf_filtered (stream, "%o", octa2);
67198944Sobrien	      fprintf_filtered (stream, "%o", octa3);
67298944Sobrien	      break;
67346283Sdfr
67498944Sobrien	    case 2:
67598944Sobrien	      /* Carry in one bit, no carry out.
67698944Sobrien	       */
67798944Sobrien	      octa1 = (carry << 2) | ((HIGH_TWO & *p) >> 6);
67898944Sobrien	      octa2 = (MID_TWO & *p) >> 3;
67998944Sobrien	      octa3 = (LOW_TWO & *p);
68098944Sobrien	      carry = 0;
68198944Sobrien	      fprintf_filtered (stream, "%o", octa1);
68298944Sobrien	      fprintf_filtered (stream, "%o", octa2);
68398944Sobrien	      fprintf_filtered (stream, "%o", octa3);
68498944Sobrien	      break;
68546283Sdfr
68698944Sobrien	    default:
68798944Sobrien	      error ("Internal error in octal conversion;");
68898944Sobrien	    }
68998944Sobrien
69098944Sobrien	  cycle++;
69198944Sobrien	  cycle = cycle % BITS_IN_OCTAL;
69246283Sdfr	}
69346283Sdfr    }
69446283Sdfr  else
69546283Sdfr    {
69646283Sdfr      for (p = valaddr + len - 1;
69746283Sdfr	   p >= valaddr;
69846283Sdfr	   p--)
69946283Sdfr	{
70098944Sobrien	  switch (cycle)
70198944Sobrien	    {
70298944Sobrien	    case 0:
70398944Sobrien	      /* Carry out, no carry in */
70498944Sobrien	      octa1 = (HIGH_ZERO & *p) >> 5;
70598944Sobrien	      octa2 = (LOW_ZERO & *p) >> 2;
70698944Sobrien	      carry = (CARRY_ZERO & *p);
70798944Sobrien	      fprintf_filtered (stream, "%o", octa1);
70898944Sobrien	      fprintf_filtered (stream, "%o", octa2);
70998944Sobrien	      break;
71046283Sdfr
71198944Sobrien	    case 1:
71298944Sobrien	      /* Carry in, carry out */
71398944Sobrien	      octa1 = (carry << 1) | ((HIGH_ONE & *p) >> 7);
71498944Sobrien	      octa2 = (MID_ONE & *p) >> 4;
71598944Sobrien	      octa3 = (LOW_ONE & *p) >> 1;
71698944Sobrien	      carry = (CARRY_ONE & *p);
71798944Sobrien	      fprintf_filtered (stream, "%o", octa1);
71898944Sobrien	      fprintf_filtered (stream, "%o", octa2);
71998944Sobrien	      fprintf_filtered (stream, "%o", octa3);
72098944Sobrien	      break;
72146283Sdfr
72298944Sobrien	    case 2:
72398944Sobrien	      /* Carry in, no carry out */
72498944Sobrien	      octa1 = (carry << 2) | ((HIGH_TWO & *p) >> 6);
72598944Sobrien	      octa2 = (MID_TWO & *p) >> 3;
72698944Sobrien	      octa3 = (LOW_TWO & *p);
72798944Sobrien	      carry = 0;
72898944Sobrien	      fprintf_filtered (stream, "%o", octa1);
72998944Sobrien	      fprintf_filtered (stream, "%o", octa2);
73098944Sobrien	      fprintf_filtered (stream, "%o", octa3);
73198944Sobrien	      break;
73246283Sdfr
73398944Sobrien	    default:
73498944Sobrien	      error ("Internal error in octal conversion;");
73598944Sobrien	    }
73698944Sobrien
73798944Sobrien	  cycle++;
73898944Sobrien	  cycle = cycle % BITS_IN_OCTAL;
73946283Sdfr	}
74046283Sdfr    }
74146283Sdfr
742130803Smarcel  fputs_filtered (local_octal_format_suffix (), stream);
74346283Sdfr}
74446283Sdfr
74546283Sdfr/* VALADDR points to an integer of LEN bytes.
74646283Sdfr * Print it in decimal on stream or format it in buf.
74746283Sdfr */
74846283Sdfrvoid
74998944Sobrienprint_decimal_chars (struct ui_file *stream, unsigned char *valaddr,
75098944Sobrien		     unsigned len)
75146283Sdfr{
75246283Sdfr#define TEN             10
75346283Sdfr#define TWO_TO_FOURTH   16
75498944Sobrien#define CARRY_OUT(  x ) ((x) / TEN)	/* extend char to int */
75546283Sdfr#define CARRY_LEFT( x ) ((x) % TEN)
75646283Sdfr#define SHIFT( x )      ((x) << 4)
75746283Sdfr#define START_P \
75898944Sobrien        ((TARGET_BYTE_ORDER == BFD_ENDIAN_BIG) ? valaddr : valaddr + len - 1)
75946283Sdfr#define NOT_END_P \
76098944Sobrien        ((TARGET_BYTE_ORDER == BFD_ENDIAN_BIG) ? (p < valaddr + len) : (p >= valaddr))
76146283Sdfr#define NEXT_P \
76298944Sobrien        ((TARGET_BYTE_ORDER == BFD_ENDIAN_BIG) ? p++ : p-- )
76346283Sdfr#define LOW_NIBBLE(  x ) ( (x) & 0x00F)
76446283Sdfr#define HIGH_NIBBLE( x ) (((x) & 0x0F0) >> 4)
76546283Sdfr
76646283Sdfr  unsigned char *p;
76746283Sdfr  unsigned char *digits;
76898944Sobrien  int carry;
76998944Sobrien  int decimal_len;
77098944Sobrien  int i, j, decimal_digits;
77198944Sobrien  int dummy;
77298944Sobrien  int flip;
77398944Sobrien
77446283Sdfr  /* Base-ten number is less than twice as many digits
77546283Sdfr   * as the base 16 number, which is 2 digits per byte.
77646283Sdfr   */
77746283Sdfr  decimal_len = len * 2 * 2;
77898944Sobrien  digits = xmalloc (decimal_len);
77946283Sdfr
78098944Sobrien  for (i = 0; i < decimal_len; i++)
78198944Sobrien    {
78246283Sdfr      digits[i] = 0;
78398944Sobrien    }
78446283Sdfr
785130803Smarcel  fputs_filtered (local_decimal_format_prefix (), stream);
78646283Sdfr
78746283Sdfr  /* Ok, we have an unknown number of bytes of data to be printed in
78846283Sdfr   * decimal.
78946283Sdfr   *
79046283Sdfr   * Given a hex number (in nibbles) as XYZ, we start by taking X and
79146283Sdfr   * decemalizing it as "x1 x2" in two decimal nibbles.  Then we multiply
79246283Sdfr   * the nibbles by 16, add Y and re-decimalize.  Repeat with Z.
79346283Sdfr   *
79446283Sdfr   * The trick is that "digits" holds a base-10 number, but sometimes
79546283Sdfr   * the individual digits are > 10.
79646283Sdfr   *
79746283Sdfr   * Outer loop is per nibble (hex digit) of input, from MSD end to
79846283Sdfr   * LSD end.
79946283Sdfr   */
80098944Sobrien  decimal_digits = 0;		/* Number of decimal digits so far */
80146283Sdfr  p = START_P;
80246283Sdfr  flip = 0;
80398944Sobrien  while (NOT_END_P)
80498944Sobrien    {
80546283Sdfr      /*
80646283Sdfr       * Multiply current base-ten number by 16 in place.
80746283Sdfr       * Each digit was between 0 and 9, now is between
80846283Sdfr       * 0 and 144.
80946283Sdfr       */
81098944Sobrien      for (j = 0; j < decimal_digits; j++)
81198944Sobrien	{
81298944Sobrien	  digits[j] = SHIFT (digits[j]);
81398944Sobrien	}
81498944Sobrien
81546283Sdfr      /* Take the next nibble off the input and add it to what
81646283Sdfr       * we've got in the LSB position.  Bottom 'digit' is now
81746283Sdfr       * between 0 and 159.
81846283Sdfr       *
81946283Sdfr       * "flip" is used to run this loop twice for each byte.
82046283Sdfr       */
82198944Sobrien      if (flip == 0)
82298944Sobrien	{
82398944Sobrien	  /* Take top nibble.
82498944Sobrien	   */
82598944Sobrien	  digits[0] += HIGH_NIBBLE (*p);
82698944Sobrien	  flip = 1;
82798944Sobrien	}
82898944Sobrien      else
82998944Sobrien	{
83098944Sobrien	  /* Take low nibble and bump our pointer "p".
83198944Sobrien	   */
83298944Sobrien	  digits[0] += LOW_NIBBLE (*p);
83398944Sobrien	  NEXT_P;
83498944Sobrien	  flip = 0;
83598944Sobrien	}
83646283Sdfr
83746283Sdfr      /* Re-decimalize.  We have to do this often enough
83846283Sdfr       * that we don't overflow, but once per nibble is
83946283Sdfr       * overkill.  Easier this way, though.  Note that the
84046283Sdfr       * carry is often larger than 10 (e.g. max initial
84146283Sdfr       * carry out of lowest nibble is 15, could bubble all
84246283Sdfr       * the way up greater than 10).  So we have to do
84346283Sdfr       * the carrying beyond the last current digit.
84446283Sdfr       */
84546283Sdfr      carry = 0;
84698944Sobrien      for (j = 0; j < decimal_len - 1; j++)
84798944Sobrien	{
84898944Sobrien	  digits[j] += carry;
84946283Sdfr
85098944Sobrien	  /* "/" won't handle an unsigned char with
85198944Sobrien	   * a value that if signed would be negative.
85298944Sobrien	   * So extend to longword int via "dummy".
85398944Sobrien	   */
85498944Sobrien	  dummy = digits[j];
85598944Sobrien	  carry = CARRY_OUT (dummy);
85698944Sobrien	  digits[j] = CARRY_LEFT (dummy);
85746283Sdfr
85898944Sobrien	  if (j >= decimal_digits && carry == 0)
85998944Sobrien	    {
86098944Sobrien	      /*
86198944Sobrien	       * All higher digits are 0 and we
86298944Sobrien	       * no longer have a carry.
86398944Sobrien	       *
86498944Sobrien	       * Note: "j" is 0-based, "decimal_digits" is
86598944Sobrien	       *       1-based.
86698944Sobrien	       */
86798944Sobrien	      decimal_digits = j + 1;
86898944Sobrien	      break;
86998944Sobrien	    }
87098944Sobrien	}
87198944Sobrien    }
87246283Sdfr
87346283Sdfr  /* Ok, now "digits" is the decimal representation, with
87446283Sdfr   * the "decimal_digits" actual digits.  Print!
87546283Sdfr   */
87698944Sobrien  for (i = decimal_digits - 1; i >= 0; i--)
87798944Sobrien    {
87898944Sobrien      fprintf_filtered (stream, "%1d", digits[i]);
87998944Sobrien    }
88098944Sobrien  xfree (digits);
88198944Sobrien
882130803Smarcel  fputs_filtered (local_decimal_format_suffix (), stream);
88346283Sdfr}
88446283Sdfr
88519370Spst/* VALADDR points to an integer of LEN bytes.  Print it in hex on stream.  */
88619370Spst
887130803Smarcelvoid
88898944Sobrienprint_hex_chars (struct ui_file *stream, unsigned char *valaddr, unsigned len)
88919370Spst{
89019370Spst  unsigned char *p;
89119370Spst
89219370Spst  /* FIXME: We should be not printing leading zeroes in most cases.  */
89319370Spst
894130803Smarcel  fputs_filtered (local_hex_format_prefix (), stream);
89598944Sobrien  if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
89619370Spst    {
89719370Spst      for (p = valaddr;
89819370Spst	   p < valaddr + len;
89919370Spst	   p++)
90019370Spst	{
90119370Spst	  fprintf_filtered (stream, "%02x", *p);
90219370Spst	}
90319370Spst    }
90419370Spst  else
90519370Spst    {
90619370Spst      for (p = valaddr + len - 1;
90719370Spst	   p >= valaddr;
90819370Spst	   p--)
90919370Spst	{
91019370Spst	  fprintf_filtered (stream, "%02x", *p);
91119370Spst	}
91219370Spst    }
913130803Smarcel  fputs_filtered (local_hex_format_suffix (), stream);
91419370Spst}
91519370Spst
916130803Smarcel/* VALADDR points to a char integer of LEN bytes.  Print it out in appropriate language form on stream.
917130803Smarcel   Omit any leading zero chars.  */
918130803Smarcel
919130803Smarcelvoid
920130803Smarcelprint_char_chars (struct ui_file *stream, unsigned char *valaddr, unsigned len)
921130803Smarcel{
922130803Smarcel  unsigned char *p;
923130803Smarcel
924130803Smarcel  if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
925130803Smarcel    {
926130803Smarcel      p = valaddr;
927130803Smarcel      while (p < valaddr + len - 1 && *p == 0)
928130803Smarcel	++p;
929130803Smarcel
930130803Smarcel      while (p < valaddr + len)
931130803Smarcel	{
932130803Smarcel	  LA_EMIT_CHAR (*p, stream, '\'');
933130803Smarcel	  ++p;
934130803Smarcel	}
935130803Smarcel    }
936130803Smarcel  else
937130803Smarcel    {
938130803Smarcel      p = valaddr + len - 1;
939130803Smarcel      while (p > valaddr && *p == 0)
940130803Smarcel	--p;
941130803Smarcel
942130803Smarcel      while (p >= valaddr)
943130803Smarcel	{
944130803Smarcel	  LA_EMIT_CHAR (*p, stream, '\'');
945130803Smarcel	  --p;
946130803Smarcel	}
947130803Smarcel    }
948130803Smarcel}
949130803Smarcel
95019370Spst/*  Called by various <lang>_val_print routines to print elements of an
95198944Sobrien   array in the form "<elem1>, <elem2>, <elem3>, ...".
95219370Spst
95398944Sobrien   (FIXME?)  Assumes array element separator is a comma, which is correct
95498944Sobrien   for all languages currently handled.
95598944Sobrien   (FIXME?)  Some languages have a notation for repeated array elements,
95698944Sobrien   perhaps we should try to use that notation when appropriate.
95798944Sobrien */
95819370Spst
95919370Spstvoid
96098944Sobrienval_print_array_elements (struct type *type, char *valaddr, CORE_ADDR address,
96198944Sobrien			  struct ui_file *stream, int format, int deref_ref,
96298944Sobrien			  int recurse, enum val_prettyprint pretty,
96398944Sobrien			  unsigned int i)
96419370Spst{
96519370Spst  unsigned int things_printed = 0;
96619370Spst  unsigned len;
96719370Spst  struct type *elttype;
96819370Spst  unsigned eltlen;
96919370Spst  /* Position of the array element we are examining to see
97019370Spst     whether it is repeated.  */
97119370Spst  unsigned int rep1;
97219370Spst  /* Number of repetitions we have detected so far.  */
97319370Spst  unsigned int reps;
97498944Sobrien
97519370Spst  elttype = TYPE_TARGET_TYPE (type);
97619370Spst  eltlen = TYPE_LENGTH (check_typedef (elttype));
97719370Spst  len = TYPE_LENGTH (type) / eltlen;
97819370Spst
97919370Spst  annotate_array_section_begin (i, elttype);
98019370Spst
98119370Spst  for (; i < len && things_printed < print_max; i++)
98219370Spst    {
98319370Spst      if (i != 0)
98419370Spst	{
98519370Spst	  if (prettyprint_arrays)
98619370Spst	    {
98719370Spst	      fprintf_filtered (stream, ",\n");
98819370Spst	      print_spaces_filtered (2 + 2 * recurse, stream);
98919370Spst	    }
99019370Spst	  else
99119370Spst	    {
99219370Spst	      fprintf_filtered (stream, ", ");
99319370Spst	    }
99419370Spst	}
99519370Spst      wrap_here (n_spaces (2 + 2 * recurse));
99619370Spst
99719370Spst      rep1 = i + 1;
99819370Spst      reps = 1;
99998944Sobrien      while ((rep1 < len) &&
100019370Spst	     !memcmp (valaddr + i * eltlen, valaddr + rep1 * eltlen, eltlen))
100119370Spst	{
100219370Spst	  ++reps;
100319370Spst	  ++rep1;
100419370Spst	}
100519370Spst
100619370Spst      if (reps > repeat_count_threshold)
100719370Spst	{
100846283Sdfr	  val_print (elttype, valaddr + i * eltlen, 0, 0, stream, format,
100919370Spst		     deref_ref, recurse + 1, pretty);
101019370Spst	  annotate_elt_rep (reps);
101119370Spst	  fprintf_filtered (stream, " <repeats %u times>", reps);
101219370Spst	  annotate_elt_rep_end ();
101319370Spst
101419370Spst	  i = rep1 - 1;
101519370Spst	  things_printed += repeat_count_threshold;
101619370Spst	}
101719370Spst      else
101819370Spst	{
101946283Sdfr	  val_print (elttype, valaddr + i * eltlen, 0, 0, stream, format,
102019370Spst		     deref_ref, recurse + 1, pretty);
102119370Spst	  annotate_elt ();
102219370Spst	  things_printed++;
102319370Spst	}
102419370Spst    }
102519370Spst  annotate_array_section_end ();
102619370Spst  if (i < len)
102719370Spst    {
102819370Spst      fprintf_filtered (stream, "...");
102919370Spst    }
103019370Spst}
103119370Spst
103298944Sobrien/* Read LEN bytes of target memory at address MEMADDR, placing the
103398944Sobrien   results in GDB's memory at MYADDR.  Returns a count of the bytes
103498944Sobrien   actually read, and optionally an errno value in the location
103598944Sobrien   pointed to by ERRNOPTR if ERRNOPTR is non-null. */
103698944Sobrien
103798944Sobrien/* FIXME: cagney/1999-10-14: Only used by val_print_string.  Can this
103898944Sobrien   function be eliminated.  */
103998944Sobrien
104098944Sobrienstatic int
104198944Sobrienpartial_memory_read (CORE_ADDR memaddr, char *myaddr, int len, int *errnoptr)
104298944Sobrien{
104398944Sobrien  int nread;			/* Number of bytes actually read. */
104498944Sobrien  int errcode;			/* Error from last read. */
104598944Sobrien
104698944Sobrien  /* First try a complete read. */
104798944Sobrien  errcode = target_read_memory (memaddr, myaddr, len);
104898944Sobrien  if (errcode == 0)
104998944Sobrien    {
105098944Sobrien      /* Got it all. */
105198944Sobrien      nread = len;
105298944Sobrien    }
105398944Sobrien  else
105498944Sobrien    {
105598944Sobrien      /* Loop, reading one byte at a time until we get as much as we can. */
105698944Sobrien      for (errcode = 0, nread = 0; len > 0 && errcode == 0; nread++, len--)
105798944Sobrien	{
105898944Sobrien	  errcode = target_read_memory (memaddr++, myaddr++, 1);
105998944Sobrien	}
106098944Sobrien      /* If an error, the last read was unsuccessful, so adjust count. */
106198944Sobrien      if (errcode != 0)
106298944Sobrien	{
106398944Sobrien	  nread--;
106498944Sobrien	}
106598944Sobrien    }
106698944Sobrien  if (errnoptr != NULL)
106798944Sobrien    {
106898944Sobrien      *errnoptr = errcode;
106998944Sobrien    }
107098944Sobrien  return (nread);
107198944Sobrien}
107298944Sobrien
107319370Spst/*  Print a string from the inferior, starting at ADDR and printing up to LEN
107498944Sobrien   characters, of WIDTH bytes a piece, to STREAM.  If LEN is -1, printing
107598944Sobrien   stops at the first null byte, otherwise printing proceeds (including null
107698944Sobrien   bytes) until either print_max or LEN characters have been printed,
107798944Sobrien   whichever is smaller. */
107819370Spst
107946283Sdfr/* FIXME: Use target_read_string.  */
108019370Spst
108119370Spstint
108298944Sobrienval_print_string (CORE_ADDR addr, int len, int width, struct ui_file *stream)
108319370Spst{
108419370Spst  int force_ellipsis = 0;	/* Force ellipsis to be printed if nonzero. */
108519370Spst  int errcode;			/* Errno returned from bad reads. */
108646283Sdfr  unsigned int fetchlimit;	/* Maximum number of chars to print. */
108746283Sdfr  unsigned int nfetch;		/* Chars to fetch / chars fetched. */
108846283Sdfr  unsigned int chunksize;	/* Size of each fetch, in chars. */
108919370Spst  char *buffer = NULL;		/* Dynamically growable fetch buffer. */
109019370Spst  char *bufptr;			/* Pointer to next available byte in buffer. */
109119370Spst  char *limit;			/* First location past end of fetch buffer. */
109298944Sobrien  struct cleanup *old_chain = NULL;	/* Top of the old cleanup chain. */
109346283Sdfr  int found_nul;		/* Non-zero if we found the nul char */
109419370Spst
109519370Spst  /* First we need to figure out the limit on the number of characters we are
109619370Spst     going to attempt to fetch and print.  This is actually pretty simple.  If
109746283Sdfr     LEN >= zero, then the limit is the minimum of LEN and print_max.  If
109846283Sdfr     LEN is -1, then the limit is print_max.  This is true regardless of
109919370Spst     whether print_max is zero, UINT_MAX (unlimited), or something in between,
110019370Spst     because finding the null byte (or available memory) is what actually
110119370Spst     limits the fetch. */
110219370Spst
110346283Sdfr  fetchlimit = (len == -1 ? print_max : min (len, print_max));
110419370Spst
110519370Spst  /* Now decide how large of chunks to try to read in one operation.  This
110646283Sdfr     is also pretty simple.  If LEN >= zero, then we want fetchlimit chars,
110746283Sdfr     so we might as well read them all in one operation.  If LEN is -1, we
110819370Spst     are looking for a null terminator to end the fetching, so we might as
110919370Spst     well read in blocks that are large enough to be efficient, but not so
111019370Spst     large as to be slow if fetchlimit happens to be large.  So we choose the
111119370Spst     minimum of 8 and fetchlimit.  We used to use 200 instead of 8 but
111219370Spst     200 is way too big for remote debugging over a serial line.  */
111319370Spst
111446283Sdfr  chunksize = (len == -1 ? min (8, fetchlimit) : fetchlimit);
111519370Spst
111619370Spst  /* Loop until we either have all the characters to print, or we encounter
111719370Spst     some error, such as bumping into the end of the address space. */
111819370Spst
111946283Sdfr  found_nul = 0;
112046283Sdfr  old_chain = make_cleanup (null_cleanup, 0);
112119370Spst
112246283Sdfr  if (len > 0)
112346283Sdfr    {
112446283Sdfr      buffer = (char *) xmalloc (len * width);
112546283Sdfr      bufptr = buffer;
112698944Sobrien      old_chain = make_cleanup (xfree, buffer);
112719370Spst
112898944Sobrien      nfetch = partial_memory_read (addr, bufptr, len * width, &errcode)
112946283Sdfr	/ width;
113046283Sdfr      addr += nfetch * width;
113146283Sdfr      bufptr += nfetch * width;
113246283Sdfr    }
113346283Sdfr  else if (len == -1)
113446283Sdfr    {
113546283Sdfr      unsigned long bufsize = 0;
113646283Sdfr      do
113746283Sdfr	{
113846283Sdfr	  QUIT;
113946283Sdfr	  nfetch = min (chunksize, fetchlimit - bufsize);
114046283Sdfr
114146283Sdfr	  if (buffer == NULL)
114246283Sdfr	    buffer = (char *) xmalloc (nfetch * width);
114346283Sdfr	  else
114446283Sdfr	    {
114546283Sdfr	      discard_cleanups (old_chain);
114646283Sdfr	      buffer = (char *) xrealloc (buffer, (nfetch + bufsize) * width);
114746283Sdfr	    }
114846283Sdfr
114998944Sobrien	  old_chain = make_cleanup (xfree, buffer);
115046283Sdfr	  bufptr = buffer + bufsize * width;
115146283Sdfr	  bufsize += nfetch;
115246283Sdfr
115346283Sdfr	  /* Read as much as we can. */
115498944Sobrien	  nfetch = partial_memory_read (addr, bufptr, nfetch * width, &errcode)
115598944Sobrien	    / width;
115646283Sdfr
115746283Sdfr	  /* Scan this chunk for the null byte that terminates the string
115846283Sdfr	     to print.  If found, we don't need to fetch any more.  Note
115946283Sdfr	     that bufptr is explicitly left pointing at the next character
116046283Sdfr	     after the null byte, or at the next character after the end of
116146283Sdfr	     the buffer. */
116246283Sdfr
116346283Sdfr	  limit = bufptr + nfetch * width;
116446283Sdfr	  while (bufptr < limit)
116546283Sdfr	    {
116646283Sdfr	      unsigned long c;
116746283Sdfr
116846283Sdfr	      c = extract_unsigned_integer (bufptr, width);
116946283Sdfr	      addr += width;
117046283Sdfr	      bufptr += width;
117146283Sdfr	      if (c == 0)
117246283Sdfr		{
117346283Sdfr		  /* We don't care about any error which happened after
117446283Sdfr		     the NULL terminator.  */
117546283Sdfr		  errcode = 0;
117646283Sdfr		  found_nul = 1;
117746283Sdfr		  break;
117846283Sdfr		}
117946283Sdfr	    }
118046283Sdfr	}
118198944Sobrien      while (errcode == 0	/* no error */
118298944Sobrien	     && bufptr - buffer < fetchlimit * width	/* no overrun */
118398944Sobrien	     && !found_nul);	/* haven't found nul yet */
118446283Sdfr    }
118546283Sdfr  else
118646283Sdfr    {				/* length of string is really 0! */
118746283Sdfr      buffer = bufptr = NULL;
118846283Sdfr      errcode = 0;
118946283Sdfr    }
119046283Sdfr
119119370Spst  /* bufptr and addr now point immediately beyond the last byte which we
119219370Spst     consider part of the string (including a '\0' which ends the string).  */
119319370Spst
119419370Spst  /* We now have either successfully filled the buffer to fetchlimit, or
119546283Sdfr     terminated early due to an error or finding a null char when LEN is -1. */
119619370Spst
119746283Sdfr  if (len == -1 && !found_nul)
119819370Spst    {
119946283Sdfr      char *peekbuf;
120046283Sdfr
120119370Spst      /* We didn't find a null terminator we were looking for.  Attempt
120298944Sobrien         to peek at the next character.  If not successful, or it is not
120398944Sobrien         a null byte, then force ellipsis to be printed.  */
120446283Sdfr
120546283Sdfr      peekbuf = (char *) alloca (width);
120646283Sdfr
120746283Sdfr      if (target_read_memory (addr, peekbuf, width) == 0
120846283Sdfr	  && extract_unsigned_integer (peekbuf, width) != 0)
120946283Sdfr	force_ellipsis = 1;
121019370Spst    }
121198944Sobrien  else if ((len >= 0 && errcode != 0) || (len > (bufptr - buffer) / width))
121219370Spst    {
121319370Spst      /* Getting an error when we have a requested length, or fetching less
121498944Sobrien         than the number of characters actually requested, always make us
121598944Sobrien         print ellipsis. */
121619370Spst      force_ellipsis = 1;
121719370Spst    }
121819370Spst
121919370Spst  QUIT;
122019370Spst
122119370Spst  /* If we get an error before fetching anything, don't print a string.
122219370Spst     But if we fetch something and then get an error, print the string
122319370Spst     and then the error message.  */
122419370Spst  if (errcode == 0 || bufptr > buffer)
122519370Spst    {
122619370Spst      if (addressprint)
122719370Spst	{
122819370Spst	  fputs_filtered (" ", stream);
122919370Spst	}
123098944Sobrien      LA_PRINT_STRING (stream, buffer, (bufptr - buffer) / width, width, force_ellipsis);
123119370Spst    }
123219370Spst
123319370Spst  if (errcode != 0)
123419370Spst    {
123519370Spst      if (errcode == EIO)
123619370Spst	{
123719370Spst	  fprintf_filtered (stream, " <Address ");
123819370Spst	  print_address_numeric (addr, 1, stream);
123919370Spst	  fprintf_filtered (stream, " out of bounds>");
124019370Spst	}
124119370Spst      else
124219370Spst	{
124319370Spst	  fprintf_filtered (stream, " <Error reading address ");
124419370Spst	  print_address_numeric (addr, 1, stream);
124519370Spst	  fprintf_filtered (stream, ": %s>", safe_strerror (errcode));
124619370Spst	}
124719370Spst    }
124819370Spst  gdb_flush (stream);
124919370Spst  do_cleanups (old_chain);
125098944Sobrien  return ((bufptr - buffer) / width);
125119370Spst}
125298944Sobrien
125319370Spst
125419370Spst/* Validate an input or output radix setting, and make sure the user
125519370Spst   knows what they really did here.  Radix setting is confusing, e.g.
125619370Spst   setting the input radix to "10" never changes it!  */
125719370Spst
125819370Spststatic void
125998944Sobrienset_input_radix (char *args, int from_tty, struct cmd_list_element *c)
126019370Spst{
1261130803Smarcel  set_input_radix_1 (from_tty, input_radix);
126219370Spst}
126319370Spst
126419370Spststatic void
126598944Sobrienset_input_radix_1 (int from_tty, unsigned radix)
126619370Spst{
126719370Spst  /* We don't currently disallow any input radix except 0 or 1, which don't
126819370Spst     make any mathematical sense.  In theory, we can deal with any input
126919370Spst     radix greater than 1, even if we don't have unique digits for every
127019370Spst     value from 0 to radix-1, but in practice we lose on large radix values.
127119370Spst     We should either fix the lossage or restrict the radix range more.
127219370Spst     (FIXME). */
127319370Spst
127419370Spst  if (radix < 2)
127519370Spst    {
1276130803Smarcel      /* FIXME: cagney/2002-03-17: This needs to revert the bad radix
1277130803Smarcel         value.  */
127819370Spst      error ("Nonsense input radix ``decimal %u''; input radix unchanged.",
127919370Spst	     radix);
128019370Spst    }
128119370Spst  input_radix = radix;
128219370Spst  if (from_tty)
128319370Spst    {
128419370Spst      printf_filtered ("Input radix now set to decimal %u, hex %x, octal %o.\n",
128519370Spst		       radix, radix, radix);
128619370Spst    }
128719370Spst}
128819370Spst
128919370Spststatic void
129098944Sobrienset_output_radix (char *args, int from_tty, struct cmd_list_element *c)
129119370Spst{
1292130803Smarcel  set_output_radix_1 (from_tty, output_radix);
129319370Spst}
129419370Spst
129519370Spststatic void
129698944Sobrienset_output_radix_1 (int from_tty, unsigned radix)
129719370Spst{
129819370Spst  /* Validate the radix and disallow ones that we aren't prepared to
129919370Spst     handle correctly, leaving the radix unchanged. */
130019370Spst  switch (radix)
130119370Spst    {
130219370Spst    case 16:
130398944Sobrien      output_format = 'x';	/* hex */
130419370Spst      break;
130519370Spst    case 10:
130698944Sobrien      output_format = 0;	/* decimal */
130719370Spst      break;
130819370Spst    case 8:
130998944Sobrien      output_format = 'o';	/* octal */
131019370Spst      break;
131119370Spst    default:
1312130803Smarcel      /* FIXME: cagney/2002-03-17: This needs to revert the bad radix
1313130803Smarcel         value.  */
131419370Spst      error ("Unsupported output radix ``decimal %u''; output radix unchanged.",
131519370Spst	     radix);
131619370Spst    }
131719370Spst  output_radix = radix;
131819370Spst  if (from_tty)
131919370Spst    {
132019370Spst      printf_filtered ("Output radix now set to decimal %u, hex %x, octal %o.\n",
132119370Spst		       radix, radix, radix);
132219370Spst    }
132319370Spst}
132419370Spst
132519370Spst/* Set both the input and output radix at once.  Try to set the output radix
132619370Spst   first, since it has the most restrictive range.  An radix that is valid as
132719370Spst   an output radix is also valid as an input radix.
132819370Spst
132919370Spst   It may be useful to have an unusual input radix.  If the user wishes to
133019370Spst   set an input radix that is not valid as an output radix, he needs to use
133119370Spst   the 'set input-radix' command. */
133219370Spst
133319370Spststatic void
133498944Sobrienset_radix (char *arg, int from_tty)
133519370Spst{
133619370Spst  unsigned radix;
133719370Spst
133898944Sobrien  radix = (arg == NULL) ? 10 : parse_and_eval_long (arg);
133919370Spst  set_output_radix_1 (0, radix);
134019370Spst  set_input_radix_1 (0, radix);
134119370Spst  if (from_tty)
134219370Spst    {
134319370Spst      printf_filtered ("Input and output radices now set to decimal %u, hex %x, octal %o.\n",
134419370Spst		       radix, radix, radix);
134519370Spst    }
134619370Spst}
134719370Spst
134819370Spst/* Show both the input and output radices. */
134919370Spst
135019370Spststatic void
135198944Sobrienshow_radix (char *arg, int from_tty)
135219370Spst{
135319370Spst  if (from_tty)
135419370Spst    {
135519370Spst      if (input_radix == output_radix)
135619370Spst	{
135719370Spst	  printf_filtered ("Input and output radices set to decimal %u, hex %x, octal %o.\n",
135819370Spst			   input_radix, input_radix, input_radix);
135919370Spst	}
136019370Spst      else
136119370Spst	{
136219370Spst	  printf_filtered ("Input radix set to decimal %u, hex %x, octal %o.\n",
136319370Spst			   input_radix, input_radix, input_radix);
136419370Spst	  printf_filtered ("Output radix set to decimal %u, hex %x, octal %o.\n",
136519370Spst			   output_radix, output_radix, output_radix);
136619370Spst	}
136719370Spst    }
136819370Spst}
136998944Sobrien
137019370Spst
137119370Spststatic void
137298944Sobrienset_print (char *arg, int from_tty)
137319370Spst{
137419370Spst  printf_unfiltered (
137598944Sobrien     "\"set print\" must be followed by the name of a print subcommand.\n");
137619370Spst  help_list (setprintlist, "set print ", -1, gdb_stdout);
137719370Spst}
137819370Spst
137919370Spststatic void
138098944Sobrienshow_print (char *args, int from_tty)
138119370Spst{
138219370Spst  cmd_show_list (showprintlist, from_tty, "");
138319370Spst}
138419370Spst
138519370Spstvoid
138698944Sobrien_initialize_valprint (void)
138719370Spst{
138819370Spst  struct cmd_list_element *c;
138919370Spst
139019370Spst  add_prefix_cmd ("print", no_class, set_print,
139119370Spst		  "Generic command for setting how things print.",
139219370Spst		  &setprintlist, "set print ", 0, &setlist);
139398944Sobrien  add_alias_cmd ("p", "print", no_class, 1, &setlist);
139498944Sobrien  /* prefer set print to set prompt */
139519370Spst  add_alias_cmd ("pr", "print", no_class, 1, &setlist);
139619370Spst
139719370Spst  add_prefix_cmd ("print", no_class, show_print,
139819370Spst		  "Generic command for showing print settings.",
139919370Spst		  &showprintlist, "show print ", 0, &showlist);
140098944Sobrien  add_alias_cmd ("p", "print", no_class, 1, &showlist);
140198944Sobrien  add_alias_cmd ("pr", "print", no_class, 1, &showlist);
140219370Spst
140319370Spst  add_show_from_set
140498944Sobrien    (add_set_cmd ("elements", no_class, var_uinteger, (char *) &print_max,
140519370Spst		  "Set limit on string chars or array elements to print.\n\
140619370Spst\"set print elements 0\" causes there to be no limit.",
140719370Spst		  &setprintlist),
140819370Spst     &showprintlist);
140919370Spst
141019370Spst  add_show_from_set
141119370Spst    (add_set_cmd ("null-stop", no_class, var_boolean,
141298944Sobrien		  (char *) &stop_print_at_null,
141319370Spst		  "Set printing of char arrays to stop at first null char.",
141419370Spst		  &setprintlist),
141519370Spst     &showprintlist);
141619370Spst
141719370Spst  add_show_from_set
141819370Spst    (add_set_cmd ("repeats", no_class, var_uinteger,
141998944Sobrien		  (char *) &repeat_count_threshold,
142019370Spst		  "Set threshold for repeated print elements.\n\
142119370Spst\"set print repeats 0\" causes all elements to be individually printed.",
142219370Spst		  &setprintlist),
142319370Spst     &showprintlist);
142419370Spst
142519370Spst  add_show_from_set
142619370Spst    (add_set_cmd ("pretty", class_support, var_boolean,
142798944Sobrien		  (char *) &prettyprint_structs,
142819370Spst		  "Set prettyprinting of structures.",
142919370Spst		  &setprintlist),
143019370Spst     &showprintlist);
143119370Spst
143219370Spst  add_show_from_set
143398944Sobrien    (add_set_cmd ("union", class_support, var_boolean, (char *) &unionprint,
143419370Spst		  "Set printing of unions interior to structures.",
143519370Spst		  &setprintlist),
143619370Spst     &showprintlist);
143798944Sobrien
143819370Spst  add_show_from_set
143919370Spst    (add_set_cmd ("array", class_support, var_boolean,
144098944Sobrien		  (char *) &prettyprint_arrays,
144119370Spst		  "Set prettyprinting of arrays.",
144219370Spst		  &setprintlist),
144319370Spst     &showprintlist);
144419370Spst
144519370Spst  add_show_from_set
144698944Sobrien    (add_set_cmd ("address", class_support, var_boolean, (char *) &addressprint,
144719370Spst		  "Set printing of addresses.",
144819370Spst		  &setprintlist),
144919370Spst     &showprintlist);
145019370Spst
145119370Spst  c = add_set_cmd ("input-radix", class_support, var_uinteger,
145298944Sobrien		   (char *) &input_radix,
145398944Sobrien		   "Set default input radix for entering numbers.",
145498944Sobrien		   &setlist);
145519370Spst  add_show_from_set (c, &showlist);
145698944Sobrien  set_cmd_sfunc (c, set_input_radix);
145719370Spst
145819370Spst  c = add_set_cmd ("output-radix", class_support, var_uinteger,
145998944Sobrien		   (char *) &output_radix,
146098944Sobrien		   "Set default output radix for printing of values.",
146198944Sobrien		   &setlist);
146219370Spst  add_show_from_set (c, &showlist);
146398944Sobrien  set_cmd_sfunc (c, set_output_radix);
146419370Spst
146519370Spst  /* The "set radix" and "show radix" commands are special in that they are
146619370Spst     like normal set and show commands but allow two normally independent
146719370Spst     variables to be either set or shown with a single command.  So the
146819370Spst     usual add_set_cmd() and add_show_from_set() commands aren't really
146919370Spst     appropriate. */
147019370Spst  add_cmd ("radix", class_support, set_radix,
147119370Spst	   "Set default input and output number radices.\n\
147219370SpstUse 'set input-radix' or 'set output-radix' to independently set each.\n\
147319370SpstWithout an argument, sets both radices back to the default value of 10.",
147419370Spst	   &setlist);
147519370Spst  add_cmd ("radix", class_support, show_radix,
147619370Spst	   "Show the default input and output number radices.\n\
147719370SpstUse 'show input-radix' or 'show output-radix' to independently show each.",
147819370Spst	   &showlist);
147919370Spst
148019370Spst  /* Give people the defaults which they are used to.  */
148119370Spst  prettyprint_structs = 0;
148219370Spst  prettyprint_arrays = 0;
148319370Spst  unionprint = 1;
148419370Spst  addressprint = 1;
148519370Spst  print_max = PRINT_MAX_DEFAULT;
148619370Spst}
1487