1e93f7393Sniklas /* Print values for GDB, the GNU debugger.
2b725ae77Skettenis
3b725ae77Skettenis Copyright 1986, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
4b725ae77Skettenis 1996, 1997, 1998, 1999, 2000, 2001, 2002 Free Software Foundation,
5b725ae77Skettenis Inc.
6e93f7393Sniklas
7e93f7393Sniklas This file is part of GDB.
8e93f7393Sniklas
9e93f7393Sniklas This program is free software; you can redistribute it and/or modify
10e93f7393Sniklas it under the terms of the GNU General Public License as published by
11e93f7393Sniklas the Free Software Foundation; either version 2 of the License, or
12e93f7393Sniklas (at your option) any later version.
13e93f7393Sniklas
14e93f7393Sniklas This program is distributed in the hope that it will be useful,
15e93f7393Sniklas but WITHOUT ANY WARRANTY; without even the implied warranty of
16e93f7393Sniklas MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17e93f7393Sniklas GNU General Public License for more details.
18e93f7393Sniklas
19e93f7393Sniklas You should have received a copy of the GNU General Public License
20e93f7393Sniklas along with this program; if not, write to the Free Software
21b725ae77Skettenis Foundation, Inc., 59 Temple Place - Suite 330,
22b725ae77Skettenis Boston, MA 02111-1307, USA. */
23e93f7393Sniklas
24e93f7393Sniklas #include "defs.h"
25e93f7393Sniklas #include "gdb_string.h"
26e93f7393Sniklas #include "symtab.h"
27e93f7393Sniklas #include "gdbtypes.h"
28e93f7393Sniklas #include "value.h"
29e93f7393Sniklas #include "gdbcore.h"
30e93f7393Sniklas #include "gdbcmd.h"
31e93f7393Sniklas #include "target.h"
32e93f7393Sniklas #include "language.h"
33e93f7393Sniklas #include "annotate.h"
34e93f7393Sniklas #include "valprint.h"
35b725ae77Skettenis #include "floatformat.h"
36b725ae77Skettenis #include "doublest.h"
37e93f7393Sniklas
38e93f7393Sniklas #include <errno.h>
39e93f7393Sniklas
40e93f7393Sniklas /* Prototypes for local functions */
41e93f7393Sniklas
42b725ae77Skettenis static int partial_memory_read (CORE_ADDR memaddr, char *myaddr,
43b725ae77Skettenis int len, int *errnoptr);
44e93f7393Sniklas
45b725ae77Skettenis static void show_print (char *, int);
46e93f7393Sniklas
47b725ae77Skettenis static void set_print (char *, int);
48e93f7393Sniklas
49b725ae77Skettenis static void set_radix (char *, int);
50e93f7393Sniklas
51b725ae77Skettenis static void show_radix (char *, int);
52e93f7393Sniklas
53b725ae77Skettenis static void set_input_radix (char *, int, struct cmd_list_element *);
54e93f7393Sniklas
55b725ae77Skettenis static void set_input_radix_1 (int, unsigned);
56e93f7393Sniklas
57b725ae77Skettenis static void set_output_radix (char *, int, struct cmd_list_element *);
58e93f7393Sniklas
59b725ae77Skettenis static void set_output_radix_1 (int, unsigned);
60b725ae77Skettenis
61b725ae77Skettenis void _initialize_valprint (void);
62e93f7393Sniklas
63e93f7393Sniklas /* Maximum number of chars to print for a string pointer value or vector
64e93f7393Sniklas contents, or UINT_MAX for no limit. Note that "set print elements 0"
65e93f7393Sniklas stores UINT_MAX in print_max, which displays in a show command as
66e93f7393Sniklas "unlimited". */
67e93f7393Sniklas
68e93f7393Sniklas unsigned int print_max;
69e93f7393Sniklas #define PRINT_MAX_DEFAULT 200 /* Start print_max off at this value. */
70e93f7393Sniklas
71e93f7393Sniklas /* Default input and output radixes, and output format letter. */
72e93f7393Sniklas
73e93f7393Sniklas unsigned input_radix = 10;
74e93f7393Sniklas unsigned output_radix = 10;
75e93f7393Sniklas int output_format = 0;
76e93f7393Sniklas
77e93f7393Sniklas /* Print repeat counts if there are more than this many repetitions of an
78e93f7393Sniklas element in an array. Referenced by the low level language dependent
79e93f7393Sniklas print routines. */
80e93f7393Sniklas
81e93f7393Sniklas unsigned int repeat_count_threshold = 10;
82e93f7393Sniklas
83e93f7393Sniklas /* If nonzero, stops printing of char arrays at first null. */
84e93f7393Sniklas
85e93f7393Sniklas int stop_print_at_null;
86e93f7393Sniklas
87e93f7393Sniklas /* Controls pretty printing of structures. */
88e93f7393Sniklas
89e93f7393Sniklas int prettyprint_structs;
90e93f7393Sniklas
91e93f7393Sniklas /* Controls pretty printing of arrays. */
92e93f7393Sniklas
93e93f7393Sniklas int prettyprint_arrays;
94e93f7393Sniklas
95e93f7393Sniklas /* If nonzero, causes unions inside structures or other unions to be
96e93f7393Sniklas printed. */
97e93f7393Sniklas
98e93f7393Sniklas int unionprint; /* Controls printing of nested unions. */
99e93f7393Sniklas
100e93f7393Sniklas /* If nonzero, causes machine addresses to be printed in certain contexts. */
101e93f7393Sniklas
102e93f7393Sniklas int addressprint; /* Controls printing of machine addresses */
103e93f7393Sniklas
104b725ae77Skettenis
105e93f7393Sniklas /* Print data of type TYPE located at VALADDR (within GDB), which came from
106e93f7393Sniklas the inferior at address ADDRESS, onto stdio stream STREAM according to
107e93f7393Sniklas FORMAT (a letter, or 0 for natural format using TYPE).
108e93f7393Sniklas
109e93f7393Sniklas If DEREF_REF is nonzero, then dereference references, otherwise just print
110e93f7393Sniklas them like pointers.
111e93f7393Sniklas
112e93f7393Sniklas The PRETTY parameter controls prettyprinting.
113e93f7393Sniklas
114e93f7393Sniklas If the data are a string pointer, returns the number of string characters
115e93f7393Sniklas printed.
116e93f7393Sniklas
117e93f7393Sniklas FIXME: The data at VALADDR is in target byte order. If gdb is ever
118e93f7393Sniklas enhanced to be able to debug more than the single target it was compiled
119e93f7393Sniklas for (specific CPU type and thus specific target byte ordering), then
120e93f7393Sniklas either the print routines are going to have to take this into account,
121e93f7393Sniklas or the data is going to have to be passed into here already converted
122e93f7393Sniklas to the host byte ordering, whichever is more convenient. */
123e93f7393Sniklas
124e93f7393Sniklas
125e93f7393Sniklas int
val_print(struct type * type,char * valaddr,int embedded_offset,CORE_ADDR address,struct ui_file * stream,int format,int deref_ref,int recurse,enum val_prettyprint pretty)126b725ae77Skettenis val_print (struct type *type, char *valaddr, int embedded_offset,
127b725ae77Skettenis CORE_ADDR address, struct ui_file *stream, int format, int deref_ref,
128b725ae77Skettenis int recurse, enum val_prettyprint pretty)
129e93f7393Sniklas {
130e93f7393Sniklas struct type *real_type = check_typedef (type);
131e93f7393Sniklas if (pretty == Val_pretty_default)
132e93f7393Sniklas {
133e93f7393Sniklas pretty = prettyprint_structs ? Val_prettyprint : Val_no_prettyprint;
134e93f7393Sniklas }
135e93f7393Sniklas
136e93f7393Sniklas QUIT;
137e93f7393Sniklas
138e93f7393Sniklas /* Ensure that the type is complete and not just a stub. If the type is
139e93f7393Sniklas only a stub and we can't find and substitute its complete type, then
140e93f7393Sniklas print appropriate string and return. */
141e93f7393Sniklas
142b725ae77Skettenis if (TYPE_STUB (real_type))
143e93f7393Sniklas {
144e93f7393Sniklas fprintf_filtered (stream, "<incomplete type>");
145e93f7393Sniklas gdb_flush (stream);
146e93f7393Sniklas return (0);
147e93f7393Sniklas }
148e93f7393Sniklas
149b725ae77Skettenis return (LA_VAL_PRINT (type, valaddr, embedded_offset, address,
150b725ae77Skettenis stream, format, deref_ref, recurse, pretty));
151e93f7393Sniklas }
152e93f7393Sniklas
153e93f7393Sniklas /* Print the value VAL in C-ish syntax on stream STREAM.
154e93f7393Sniklas FORMAT is a format-letter, or 0 for print in natural format of data type.
155e93f7393Sniklas If the object printed is a string pointer, returns
156e93f7393Sniklas the number of string bytes printed. */
157e93f7393Sniklas
158e93f7393Sniklas int
value_print(struct value * val,struct ui_file * stream,int format,enum val_prettyprint pretty)159b725ae77Skettenis value_print (struct value *val, struct ui_file *stream, int format,
160b725ae77Skettenis enum val_prettyprint pretty)
161e93f7393Sniklas {
162e93f7393Sniklas if (val == 0)
163e93f7393Sniklas {
164e93f7393Sniklas printf_filtered ("<address of value unknown>");
165e93f7393Sniklas return 0;
166e93f7393Sniklas }
167e93f7393Sniklas if (VALUE_OPTIMIZED_OUT (val))
168e93f7393Sniklas {
169e93f7393Sniklas printf_filtered ("<value optimized out>");
170e93f7393Sniklas return 0;
171e93f7393Sniklas }
172e93f7393Sniklas return LA_VALUE_PRINT (val, stream, format, pretty);
173e93f7393Sniklas }
174e93f7393Sniklas
175e93f7393Sniklas /* Called by various <lang>_val_print routines to print
176e93f7393Sniklas TYPE_CODE_INT's. TYPE is the type. VALADDR is the address of the
177e93f7393Sniklas value. STREAM is where to print the value. */
178e93f7393Sniklas
179e93f7393Sniklas void
val_print_type_code_int(struct type * type,char * valaddr,struct ui_file * stream)180b725ae77Skettenis val_print_type_code_int (struct type *type, char *valaddr,
181b725ae77Skettenis struct ui_file *stream)
182e93f7393Sniklas {
183e93f7393Sniklas if (TYPE_LENGTH (type) > sizeof (LONGEST))
184e93f7393Sniklas {
185e93f7393Sniklas LONGEST val;
186e93f7393Sniklas
187e93f7393Sniklas if (TYPE_UNSIGNED (type)
188e93f7393Sniklas && extract_long_unsigned_integer (valaddr, TYPE_LENGTH (type),
189e93f7393Sniklas &val))
190e93f7393Sniklas {
191e93f7393Sniklas print_longest (stream, 'u', 0, val);
192e93f7393Sniklas }
193e93f7393Sniklas else
194e93f7393Sniklas {
195e93f7393Sniklas /* Signed, or we couldn't turn an unsigned value into a
196e93f7393Sniklas LONGEST. For signed values, one could assume two's
197e93f7393Sniklas complement (a reasonable assumption, I think) and do
198e93f7393Sniklas better than this. */
199e93f7393Sniklas print_hex_chars (stream, (unsigned char *) valaddr,
200e93f7393Sniklas TYPE_LENGTH (type));
201e93f7393Sniklas }
202e93f7393Sniklas }
203e93f7393Sniklas else
204e93f7393Sniklas {
205e93f7393Sniklas print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0,
206e93f7393Sniklas unpack_long (type, valaddr));
207e93f7393Sniklas }
208e93f7393Sniklas }
209e93f7393Sniklas
210e93f7393Sniklas /* Print a number according to FORMAT which is one of d,u,x,o,b,h,w,g.
211e93f7393Sniklas The raison d'etre of this function is to consolidate printing of
212*63addd46Skettenis LONG_LONG's into this one function. The format chars b,h,w,g are
213*63addd46Skettenis from print_scalar_formatted(). Numbers are printed using C
214*63addd46Skettenis format.
215e93f7393Sniklas
216*63addd46Skettenis USE_C_FORMAT means to use C format in all cases. Without it,
217*63addd46Skettenis 'o' and 'x' format do not include the standard C radix prefix
218*63addd46Skettenis (leading 0 or 0x).
219e93f7393Sniklas
220*63addd46Skettenis Hilfinger/2004-09-09: USE_C_FORMAT was originally called USE_LOCAL
221*63addd46Skettenis and was intended to request formating according to the current
222*63addd46Skettenis language and would be used for most integers that GDB prints. The
223*63addd46Skettenis exceptional cases were things like protocols where the format of
224*63addd46Skettenis the integer is a protocol thing, not a user-visible thing). The
225*63addd46Skettenis parameter remains to preserve the information of what things might
226*63addd46Skettenis be printed with language-specific format, should we ever resurrect
227*63addd46Skettenis that capability. */
228b725ae77Skettenis
229e93f7393Sniklas void
print_longest(struct ui_file * stream,int format,int use_c_format,LONGEST val_long)230*63addd46Skettenis print_longest (struct ui_file *stream, int format, int use_c_format,
231b725ae77Skettenis LONGEST val_long)
232e93f7393Sniklas {
233*63addd46Skettenis const char *val;
234e93f7393Sniklas
235e93f7393Sniklas switch (format)
236e93f7393Sniklas {
237e93f7393Sniklas case 'd':
238*63addd46Skettenis val = int_string (val_long, 10, 1, 0, 1); break;
239e93f7393Sniklas case 'u':
240*63addd46Skettenis val = int_string (val_long, 10, 0, 0, 1); break;
241e93f7393Sniklas case 'x':
242*63addd46Skettenis val = int_string (val_long, 16, 0, 0, use_c_format); break;
243*63addd46Skettenis case 'b':
244*63addd46Skettenis val = int_string (val_long, 16, 0, 2, 1); break;
245*63addd46Skettenis case 'h':
246*63addd46Skettenis val = int_string (val_long, 16, 0, 4, 1); break;
247*63addd46Skettenis case 'w':
248*63addd46Skettenis val = int_string (val_long, 16, 0, 8, 1); break;
249*63addd46Skettenis case 'g':
250*63addd46Skettenis val = int_string (val_long, 16, 0, 16, 1); break;
251e93f7393Sniklas break;
252e93f7393Sniklas case 'o':
253*63addd46Skettenis val = int_string (val_long, 8, 0, 0, use_c_format); break;
254e93f7393Sniklas default:
255b725ae77Skettenis internal_error (__FILE__, __LINE__, "failed internal consistency check");
256e93f7393Sniklas }
257*63addd46Skettenis fputs_filtered (val, stream);
258e93f7393Sniklas }
259e93f7393Sniklas
260e93f7393Sniklas /* This used to be a macro, but I don't think it is called often enough
261e93f7393Sniklas to merit such treatment. */
262e93f7393Sniklas /* Convert a LONGEST to an int. This is used in contexts (e.g. number of
263e93f7393Sniklas arguments to a function, number in a value history, register number, etc.)
264e93f7393Sniklas where the value must not be larger than can fit in an int. */
265e93f7393Sniklas
266e93f7393Sniklas int
longest_to_int(LONGEST arg)267b725ae77Skettenis longest_to_int (LONGEST arg)
268e93f7393Sniklas {
269e93f7393Sniklas /* Let the compiler do the work */
270e93f7393Sniklas int rtnval = (int) arg;
271e93f7393Sniklas
272e93f7393Sniklas /* Check for overflows or underflows */
273e93f7393Sniklas if (sizeof (LONGEST) > sizeof (int))
274e93f7393Sniklas {
275e93f7393Sniklas if (rtnval != arg)
276e93f7393Sniklas {
277e93f7393Sniklas error ("Value out of range.");
278e93f7393Sniklas }
279e93f7393Sniklas }
280e93f7393Sniklas return (rtnval);
281e93f7393Sniklas }
282e93f7393Sniklas
283b725ae77Skettenis /* Print a floating point value of type TYPE (not always a
284b725ae77Skettenis TYPE_CODE_FLT), pointed to in GDB by VALADDR, on STREAM. */
285e93f7393Sniklas
286e93f7393Sniklas void
print_floating(char * valaddr,struct type * type,struct ui_file * stream)287b725ae77Skettenis print_floating (char *valaddr, struct type *type, struct ui_file *stream)
288e93f7393Sniklas {
289e93f7393Sniklas DOUBLEST doub;
290e93f7393Sniklas int inv;
291b725ae77Skettenis const struct floatformat *fmt = NULL;
292e93f7393Sniklas unsigned len = TYPE_LENGTH (type);
293e93f7393Sniklas
294b725ae77Skettenis /* If it is a floating-point, check for obvious problems. */
295b725ae77Skettenis if (TYPE_CODE (type) == TYPE_CODE_FLT)
296b725ae77Skettenis fmt = floatformat_from_type (type);
297b725ae77Skettenis if (fmt != NULL && floatformat_is_nan (fmt, valaddr))
298e93f7393Sniklas {
299b725ae77Skettenis if (floatformat_is_negative (fmt, valaddr))
300b725ae77Skettenis fprintf_filtered (stream, "-");
301b725ae77Skettenis fprintf_filtered (stream, "nan(");
302*63addd46Skettenis fputs_filtered ("0x", stream);
303b725ae77Skettenis fputs_filtered (floatformat_mantissa (fmt, valaddr), stream);
304b725ae77Skettenis fprintf_filtered (stream, ")");
305e93f7393Sniklas return;
306e93f7393Sniklas }
307b725ae77Skettenis
308b725ae77Skettenis /* NOTE: cagney/2002-01-15: The TYPE passed into print_floating()
309b725ae77Skettenis isn't necessarily a TYPE_CODE_FLT. Consequently, unpack_double
310b725ae77Skettenis needs to be used as that takes care of any necessary type
311b725ae77Skettenis conversions. Such conversions are of course direct to DOUBLEST
312b725ae77Skettenis and disregard any possible target floating point limitations.
313b725ae77Skettenis For instance, a u64 would be converted and displayed exactly on a
314b725ae77Skettenis host with 80 bit DOUBLEST but with loss of information on a host
315b725ae77Skettenis with 64 bit DOUBLEST. */
316e93f7393Sniklas
317e93f7393Sniklas doub = unpack_double (type, valaddr, &inv);
318e93f7393Sniklas if (inv)
319e93f7393Sniklas {
320e93f7393Sniklas fprintf_filtered (stream, "<invalid float value>");
321e93f7393Sniklas return;
322e93f7393Sniklas }
323e93f7393Sniklas
324b725ae77Skettenis /* FIXME: kettenis/2001-01-20: The following code makes too much
325b725ae77Skettenis assumptions about the host and target floating point format. */
326b725ae77Skettenis
327b725ae77Skettenis /* NOTE: cagney/2002-02-03: Since the TYPE of what was passed in may
328b725ae77Skettenis not necessarially be a TYPE_CODE_FLT, the below ignores that and
329b725ae77Skettenis instead uses the type's length to determine the precision of the
330b725ae77Skettenis floating-point value being printed. */
331b725ae77Skettenis
332e93f7393Sniklas if (len < sizeof (double))
333e93f7393Sniklas fprintf_filtered (stream, "%.9g", (double) doub);
334e93f7393Sniklas else if (len == sizeof (double))
335e93f7393Sniklas fprintf_filtered (stream, "%.17g", (double) doub);
336e93f7393Sniklas else
337e93f7393Sniklas #ifdef PRINTF_HAS_LONG_DOUBLE
338e93f7393Sniklas fprintf_filtered (stream, "%.35Lg", doub);
339e93f7393Sniklas #else
340b725ae77Skettenis /* This at least wins with values that are representable as
341b725ae77Skettenis doubles. */
342e93f7393Sniklas fprintf_filtered (stream, "%.17g", (double) doub);
343e93f7393Sniklas #endif
344e93f7393Sniklas }
345e93f7393Sniklas
346b725ae77Skettenis void
print_binary_chars(struct ui_file * stream,unsigned char * valaddr,unsigned len)347b725ae77Skettenis print_binary_chars (struct ui_file *stream, unsigned char *valaddr,
348b725ae77Skettenis unsigned len)
349b725ae77Skettenis {
350b725ae77Skettenis
351b725ae77Skettenis #define BITS_IN_BYTES 8
352b725ae77Skettenis
353b725ae77Skettenis unsigned char *p;
354b725ae77Skettenis unsigned int i;
355b725ae77Skettenis int b;
356b725ae77Skettenis
357b725ae77Skettenis /* Declared "int" so it will be signed.
358b725ae77Skettenis * This ensures that right shift will shift in zeros.
359b725ae77Skettenis */
360b725ae77Skettenis const int mask = 0x080;
361b725ae77Skettenis
362b725ae77Skettenis /* FIXME: We should be not printing leading zeroes in most cases. */
363b725ae77Skettenis
364b725ae77Skettenis if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
365b725ae77Skettenis {
366b725ae77Skettenis for (p = valaddr;
367b725ae77Skettenis p < valaddr + len;
368b725ae77Skettenis p++)
369b725ae77Skettenis {
370b725ae77Skettenis /* Every byte has 8 binary characters; peel off
371b725ae77Skettenis * and print from the MSB end.
372b725ae77Skettenis */
373b725ae77Skettenis for (i = 0; i < (BITS_IN_BYTES * sizeof (*p)); i++)
374b725ae77Skettenis {
375b725ae77Skettenis if (*p & (mask >> i))
376b725ae77Skettenis b = 1;
377b725ae77Skettenis else
378b725ae77Skettenis b = 0;
379b725ae77Skettenis
380b725ae77Skettenis fprintf_filtered (stream, "%1d", b);
381b725ae77Skettenis }
382b725ae77Skettenis }
383b725ae77Skettenis }
384b725ae77Skettenis else
385b725ae77Skettenis {
386b725ae77Skettenis for (p = valaddr + len - 1;
387b725ae77Skettenis p >= valaddr;
388b725ae77Skettenis p--)
389b725ae77Skettenis {
390b725ae77Skettenis for (i = 0; i < (BITS_IN_BYTES * sizeof (*p)); i++)
391b725ae77Skettenis {
392b725ae77Skettenis if (*p & (mask >> i))
393b725ae77Skettenis b = 1;
394b725ae77Skettenis else
395b725ae77Skettenis b = 0;
396b725ae77Skettenis
397b725ae77Skettenis fprintf_filtered (stream, "%1d", b);
398b725ae77Skettenis }
399b725ae77Skettenis }
400b725ae77Skettenis }
401b725ae77Skettenis }
402b725ae77Skettenis
403b725ae77Skettenis /* VALADDR points to an integer of LEN bytes.
404b725ae77Skettenis * Print it in octal on stream or format it in buf.
405b725ae77Skettenis */
406b725ae77Skettenis void
print_octal_chars(struct ui_file * stream,unsigned char * valaddr,unsigned len)407b725ae77Skettenis print_octal_chars (struct ui_file *stream, unsigned char *valaddr, unsigned len)
408b725ae77Skettenis {
409b725ae77Skettenis unsigned char *p;
410b725ae77Skettenis unsigned char octa1, octa2, octa3, carry;
411b725ae77Skettenis int cycle;
412b725ae77Skettenis
413b725ae77Skettenis /* FIXME: We should be not printing leading zeroes in most cases. */
414b725ae77Skettenis
415b725ae77Skettenis
416b725ae77Skettenis /* Octal is 3 bits, which doesn't fit. Yuk. So we have to track
417b725ae77Skettenis * the extra bits, which cycle every three bytes:
418b725ae77Skettenis *
419b725ae77Skettenis * Byte side: 0 1 2 3
420b725ae77Skettenis * | | | |
421b725ae77Skettenis * bit number 123 456 78 | 9 012 345 6 | 78 901 234 | 567 890 12 |
422b725ae77Skettenis *
423b725ae77Skettenis * Octal side: 0 1 carry 3 4 carry ...
424b725ae77Skettenis *
425b725ae77Skettenis * Cycle number: 0 1 2
426b725ae77Skettenis *
427b725ae77Skettenis * But of course we are printing from the high side, so we have to
428b725ae77Skettenis * figure out where in the cycle we are so that we end up with no
429b725ae77Skettenis * left over bits at the end.
430b725ae77Skettenis */
431b725ae77Skettenis #define BITS_IN_OCTAL 3
432b725ae77Skettenis #define HIGH_ZERO 0340
433b725ae77Skettenis #define LOW_ZERO 0016
434b725ae77Skettenis #define CARRY_ZERO 0003
435b725ae77Skettenis #define HIGH_ONE 0200
436b725ae77Skettenis #define MID_ONE 0160
437b725ae77Skettenis #define LOW_ONE 0016
438b725ae77Skettenis #define CARRY_ONE 0001
439b725ae77Skettenis #define HIGH_TWO 0300
440b725ae77Skettenis #define MID_TWO 0070
441b725ae77Skettenis #define LOW_TWO 0007
442b725ae77Skettenis
443b725ae77Skettenis /* For 32 we start in cycle 2, with two bits and one bit carry;
444b725ae77Skettenis * for 64 in cycle in cycle 1, with one bit and a two bit carry.
445b725ae77Skettenis */
446b725ae77Skettenis cycle = (len * BITS_IN_BYTES) % BITS_IN_OCTAL;
447b725ae77Skettenis carry = 0;
448b725ae77Skettenis
449*63addd46Skettenis fputs_filtered ("0", stream);
450b725ae77Skettenis if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
451b725ae77Skettenis {
452b725ae77Skettenis for (p = valaddr;
453b725ae77Skettenis p < valaddr + len;
454b725ae77Skettenis p++)
455b725ae77Skettenis {
456b725ae77Skettenis switch (cycle)
457b725ae77Skettenis {
458b725ae77Skettenis case 0:
459b725ae77Skettenis /* No carry in, carry out two bits.
460b725ae77Skettenis */
461b725ae77Skettenis octa1 = (HIGH_ZERO & *p) >> 5;
462b725ae77Skettenis octa2 = (LOW_ZERO & *p) >> 2;
463b725ae77Skettenis carry = (CARRY_ZERO & *p);
464b725ae77Skettenis fprintf_filtered (stream, "%o", octa1);
465b725ae77Skettenis fprintf_filtered (stream, "%o", octa2);
466b725ae77Skettenis break;
467b725ae77Skettenis
468b725ae77Skettenis case 1:
469b725ae77Skettenis /* Carry in two bits, carry out one bit.
470b725ae77Skettenis */
471b725ae77Skettenis octa1 = (carry << 1) | ((HIGH_ONE & *p) >> 7);
472b725ae77Skettenis octa2 = (MID_ONE & *p) >> 4;
473b725ae77Skettenis octa3 = (LOW_ONE & *p) >> 1;
474b725ae77Skettenis carry = (CARRY_ONE & *p);
475b725ae77Skettenis fprintf_filtered (stream, "%o", octa1);
476b725ae77Skettenis fprintf_filtered (stream, "%o", octa2);
477b725ae77Skettenis fprintf_filtered (stream, "%o", octa3);
478b725ae77Skettenis break;
479b725ae77Skettenis
480b725ae77Skettenis case 2:
481b725ae77Skettenis /* Carry in one bit, no carry out.
482b725ae77Skettenis */
483b725ae77Skettenis octa1 = (carry << 2) | ((HIGH_TWO & *p) >> 6);
484b725ae77Skettenis octa2 = (MID_TWO & *p) >> 3;
485b725ae77Skettenis octa3 = (LOW_TWO & *p);
486b725ae77Skettenis carry = 0;
487b725ae77Skettenis fprintf_filtered (stream, "%o", octa1);
488b725ae77Skettenis fprintf_filtered (stream, "%o", octa2);
489b725ae77Skettenis fprintf_filtered (stream, "%o", octa3);
490b725ae77Skettenis break;
491b725ae77Skettenis
492b725ae77Skettenis default:
493b725ae77Skettenis error ("Internal error in octal conversion;");
494b725ae77Skettenis }
495b725ae77Skettenis
496b725ae77Skettenis cycle++;
497b725ae77Skettenis cycle = cycle % BITS_IN_OCTAL;
498b725ae77Skettenis }
499b725ae77Skettenis }
500b725ae77Skettenis else
501b725ae77Skettenis {
502b725ae77Skettenis for (p = valaddr + len - 1;
503b725ae77Skettenis p >= valaddr;
504b725ae77Skettenis p--)
505b725ae77Skettenis {
506b725ae77Skettenis switch (cycle)
507b725ae77Skettenis {
508b725ae77Skettenis case 0:
509b725ae77Skettenis /* Carry out, no carry in */
510b725ae77Skettenis octa1 = (HIGH_ZERO & *p) >> 5;
511b725ae77Skettenis octa2 = (LOW_ZERO & *p) >> 2;
512b725ae77Skettenis carry = (CARRY_ZERO & *p);
513b725ae77Skettenis fprintf_filtered (stream, "%o", octa1);
514b725ae77Skettenis fprintf_filtered (stream, "%o", octa2);
515b725ae77Skettenis break;
516b725ae77Skettenis
517b725ae77Skettenis case 1:
518b725ae77Skettenis /* Carry in, carry out */
519b725ae77Skettenis octa1 = (carry << 1) | ((HIGH_ONE & *p) >> 7);
520b725ae77Skettenis octa2 = (MID_ONE & *p) >> 4;
521b725ae77Skettenis octa3 = (LOW_ONE & *p) >> 1;
522b725ae77Skettenis carry = (CARRY_ONE & *p);
523b725ae77Skettenis fprintf_filtered (stream, "%o", octa1);
524b725ae77Skettenis fprintf_filtered (stream, "%o", octa2);
525b725ae77Skettenis fprintf_filtered (stream, "%o", octa3);
526b725ae77Skettenis break;
527b725ae77Skettenis
528b725ae77Skettenis case 2:
529b725ae77Skettenis /* Carry in, no carry out */
530b725ae77Skettenis octa1 = (carry << 2) | ((HIGH_TWO & *p) >> 6);
531b725ae77Skettenis octa2 = (MID_TWO & *p) >> 3;
532b725ae77Skettenis octa3 = (LOW_TWO & *p);
533b725ae77Skettenis carry = 0;
534b725ae77Skettenis fprintf_filtered (stream, "%o", octa1);
535b725ae77Skettenis fprintf_filtered (stream, "%o", octa2);
536b725ae77Skettenis fprintf_filtered (stream, "%o", octa3);
537b725ae77Skettenis break;
538b725ae77Skettenis
539b725ae77Skettenis default:
540b725ae77Skettenis error ("Internal error in octal conversion;");
541b725ae77Skettenis }
542b725ae77Skettenis
543b725ae77Skettenis cycle++;
544b725ae77Skettenis cycle = cycle % BITS_IN_OCTAL;
545b725ae77Skettenis }
546b725ae77Skettenis }
547b725ae77Skettenis
548b725ae77Skettenis }
549b725ae77Skettenis
550b725ae77Skettenis /* VALADDR points to an integer of LEN bytes.
551b725ae77Skettenis * Print it in decimal on stream or format it in buf.
552b725ae77Skettenis */
553b725ae77Skettenis void
print_decimal_chars(struct ui_file * stream,unsigned char * valaddr,unsigned len)554b725ae77Skettenis print_decimal_chars (struct ui_file *stream, unsigned char *valaddr,
555b725ae77Skettenis unsigned len)
556b725ae77Skettenis {
557b725ae77Skettenis #define TEN 10
558b725ae77Skettenis #define TWO_TO_FOURTH 16
559b725ae77Skettenis #define CARRY_OUT( x ) ((x) / TEN) /* extend char to int */
560b725ae77Skettenis #define CARRY_LEFT( x ) ((x) % TEN)
561b725ae77Skettenis #define SHIFT( x ) ((x) << 4)
562b725ae77Skettenis #define START_P \
563b725ae77Skettenis ((TARGET_BYTE_ORDER == BFD_ENDIAN_BIG) ? valaddr : valaddr + len - 1)
564b725ae77Skettenis #define NOT_END_P \
565b725ae77Skettenis ((TARGET_BYTE_ORDER == BFD_ENDIAN_BIG) ? (p < valaddr + len) : (p >= valaddr))
566b725ae77Skettenis #define NEXT_P \
567b725ae77Skettenis ((TARGET_BYTE_ORDER == BFD_ENDIAN_BIG) ? p++ : p-- )
568b725ae77Skettenis #define LOW_NIBBLE( x ) ( (x) & 0x00F)
569b725ae77Skettenis #define HIGH_NIBBLE( x ) (((x) & 0x0F0) >> 4)
570b725ae77Skettenis
571b725ae77Skettenis unsigned char *p;
572b725ae77Skettenis unsigned char *digits;
573b725ae77Skettenis int carry;
574b725ae77Skettenis int decimal_len;
575b725ae77Skettenis int i, j, decimal_digits;
576b725ae77Skettenis int dummy;
577b725ae77Skettenis int flip;
578b725ae77Skettenis
579b725ae77Skettenis /* Base-ten number is less than twice as many digits
580b725ae77Skettenis * as the base 16 number, which is 2 digits per byte.
581b725ae77Skettenis */
582b725ae77Skettenis decimal_len = len * 2 * 2;
583b725ae77Skettenis digits = xmalloc (decimal_len);
584b725ae77Skettenis
585b725ae77Skettenis for (i = 0; i < decimal_len; i++)
586b725ae77Skettenis {
587b725ae77Skettenis digits[i] = 0;
588b725ae77Skettenis }
589b725ae77Skettenis
590b725ae77Skettenis /* Ok, we have an unknown number of bytes of data to be printed in
591b725ae77Skettenis * decimal.
592b725ae77Skettenis *
593b725ae77Skettenis * Given a hex number (in nibbles) as XYZ, we start by taking X and
594b725ae77Skettenis * decemalizing it as "x1 x2" in two decimal nibbles. Then we multiply
595b725ae77Skettenis * the nibbles by 16, add Y and re-decimalize. Repeat with Z.
596b725ae77Skettenis *
597b725ae77Skettenis * The trick is that "digits" holds a base-10 number, but sometimes
598b725ae77Skettenis * the individual digits are > 10.
599b725ae77Skettenis *
600b725ae77Skettenis * Outer loop is per nibble (hex digit) of input, from MSD end to
601b725ae77Skettenis * LSD end.
602b725ae77Skettenis */
603b725ae77Skettenis decimal_digits = 0; /* Number of decimal digits so far */
604b725ae77Skettenis p = START_P;
605b725ae77Skettenis flip = 0;
606b725ae77Skettenis while (NOT_END_P)
607b725ae77Skettenis {
608b725ae77Skettenis /*
609b725ae77Skettenis * Multiply current base-ten number by 16 in place.
610b725ae77Skettenis * Each digit was between 0 and 9, now is between
611b725ae77Skettenis * 0 and 144.
612b725ae77Skettenis */
613b725ae77Skettenis for (j = 0; j < decimal_digits; j++)
614b725ae77Skettenis {
615b725ae77Skettenis digits[j] = SHIFT (digits[j]);
616b725ae77Skettenis }
617b725ae77Skettenis
618b725ae77Skettenis /* Take the next nibble off the input and add it to what
619b725ae77Skettenis * we've got in the LSB position. Bottom 'digit' is now
620b725ae77Skettenis * between 0 and 159.
621b725ae77Skettenis *
622b725ae77Skettenis * "flip" is used to run this loop twice for each byte.
623b725ae77Skettenis */
624b725ae77Skettenis if (flip == 0)
625b725ae77Skettenis {
626b725ae77Skettenis /* Take top nibble.
627b725ae77Skettenis */
628b725ae77Skettenis digits[0] += HIGH_NIBBLE (*p);
629b725ae77Skettenis flip = 1;
630b725ae77Skettenis }
631b725ae77Skettenis else
632b725ae77Skettenis {
633b725ae77Skettenis /* Take low nibble and bump our pointer "p".
634b725ae77Skettenis */
635b725ae77Skettenis digits[0] += LOW_NIBBLE (*p);
636b725ae77Skettenis NEXT_P;
637b725ae77Skettenis flip = 0;
638b725ae77Skettenis }
639b725ae77Skettenis
640b725ae77Skettenis /* Re-decimalize. We have to do this often enough
641b725ae77Skettenis * that we don't overflow, but once per nibble is
642b725ae77Skettenis * overkill. Easier this way, though. Note that the
643b725ae77Skettenis * carry is often larger than 10 (e.g. max initial
644b725ae77Skettenis * carry out of lowest nibble is 15, could bubble all
645b725ae77Skettenis * the way up greater than 10). So we have to do
646b725ae77Skettenis * the carrying beyond the last current digit.
647b725ae77Skettenis */
648b725ae77Skettenis carry = 0;
649b725ae77Skettenis for (j = 0; j < decimal_len - 1; j++)
650b725ae77Skettenis {
651b725ae77Skettenis digits[j] += carry;
652b725ae77Skettenis
653b725ae77Skettenis /* "/" won't handle an unsigned char with
654b725ae77Skettenis * a value that if signed would be negative.
655b725ae77Skettenis * So extend to longword int via "dummy".
656b725ae77Skettenis */
657b725ae77Skettenis dummy = digits[j];
658b725ae77Skettenis carry = CARRY_OUT (dummy);
659b725ae77Skettenis digits[j] = CARRY_LEFT (dummy);
660b725ae77Skettenis
661b725ae77Skettenis if (j >= decimal_digits && carry == 0)
662b725ae77Skettenis {
663b725ae77Skettenis /*
664b725ae77Skettenis * All higher digits are 0 and we
665b725ae77Skettenis * no longer have a carry.
666b725ae77Skettenis *
667b725ae77Skettenis * Note: "j" is 0-based, "decimal_digits" is
668b725ae77Skettenis * 1-based.
669b725ae77Skettenis */
670b725ae77Skettenis decimal_digits = j + 1;
671b725ae77Skettenis break;
672b725ae77Skettenis }
673b725ae77Skettenis }
674b725ae77Skettenis }
675b725ae77Skettenis
676b725ae77Skettenis /* Ok, now "digits" is the decimal representation, with
677b725ae77Skettenis * the "decimal_digits" actual digits. Print!
678b725ae77Skettenis */
679b725ae77Skettenis for (i = decimal_digits - 1; i >= 0; i--)
680b725ae77Skettenis {
681b725ae77Skettenis fprintf_filtered (stream, "%1d", digits[i]);
682b725ae77Skettenis }
683b725ae77Skettenis xfree (digits);
684b725ae77Skettenis }
685b725ae77Skettenis
686e93f7393Sniklas /* VALADDR points to an integer of LEN bytes. Print it in hex on stream. */
687e93f7393Sniklas
688b725ae77Skettenis void
print_hex_chars(struct ui_file * stream,unsigned char * valaddr,unsigned len)689b725ae77Skettenis print_hex_chars (struct ui_file *stream, unsigned char *valaddr, unsigned len)
690e93f7393Sniklas {
691e93f7393Sniklas unsigned char *p;
692e93f7393Sniklas
693e93f7393Sniklas /* FIXME: We should be not printing leading zeroes in most cases. */
694e93f7393Sniklas
695*63addd46Skettenis fputs_filtered ("0x", stream);
696b725ae77Skettenis if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
697e93f7393Sniklas {
698e93f7393Sniklas for (p = valaddr;
699e93f7393Sniklas p < valaddr + len;
700e93f7393Sniklas p++)
701e93f7393Sniklas {
702e93f7393Sniklas fprintf_filtered (stream, "%02x", *p);
703e93f7393Sniklas }
704e93f7393Sniklas }
705e93f7393Sniklas else
706e93f7393Sniklas {
707e93f7393Sniklas for (p = valaddr + len - 1;
708e93f7393Sniklas p >= valaddr;
709e93f7393Sniklas p--)
710e93f7393Sniklas {
711e93f7393Sniklas fprintf_filtered (stream, "%02x", *p);
712e93f7393Sniklas }
713e93f7393Sniklas }
714b725ae77Skettenis }
715b725ae77Skettenis
716b725ae77Skettenis /* VALADDR points to a char integer of LEN bytes. Print it out in appropriate language form on stream.
717b725ae77Skettenis Omit any leading zero chars. */
718b725ae77Skettenis
719b725ae77Skettenis void
print_char_chars(struct ui_file * stream,unsigned char * valaddr,unsigned len)720b725ae77Skettenis print_char_chars (struct ui_file *stream, unsigned char *valaddr, unsigned len)
721b725ae77Skettenis {
722b725ae77Skettenis unsigned char *p;
723b725ae77Skettenis
724b725ae77Skettenis if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
725b725ae77Skettenis {
726b725ae77Skettenis p = valaddr;
727b725ae77Skettenis while (p < valaddr + len - 1 && *p == 0)
728b725ae77Skettenis ++p;
729b725ae77Skettenis
730b725ae77Skettenis while (p < valaddr + len)
731b725ae77Skettenis {
732b725ae77Skettenis LA_EMIT_CHAR (*p, stream, '\'');
733b725ae77Skettenis ++p;
734b725ae77Skettenis }
735b725ae77Skettenis }
736b725ae77Skettenis else
737b725ae77Skettenis {
738b725ae77Skettenis p = valaddr + len - 1;
739b725ae77Skettenis while (p > valaddr && *p == 0)
740b725ae77Skettenis --p;
741b725ae77Skettenis
742b725ae77Skettenis while (p >= valaddr)
743b725ae77Skettenis {
744b725ae77Skettenis LA_EMIT_CHAR (*p, stream, '\'');
745b725ae77Skettenis --p;
746b725ae77Skettenis }
747b725ae77Skettenis }
748e93f7393Sniklas }
749e93f7393Sniklas
750e93f7393Sniklas /* Called by various <lang>_val_print routines to print elements of an
751e93f7393Sniklas array in the form "<elem1>, <elem2>, <elem3>, ...".
752e93f7393Sniklas
753e93f7393Sniklas (FIXME?) Assumes array element separator is a comma, which is correct
754e93f7393Sniklas for all languages currently handled.
755e93f7393Sniklas (FIXME?) Some languages have a notation for repeated array elements,
756e93f7393Sniklas perhaps we should try to use that notation when appropriate.
757e93f7393Sniklas */
758e93f7393Sniklas
759e93f7393Sniklas void
val_print_array_elements(struct type * type,char * valaddr,CORE_ADDR address,struct ui_file * stream,int format,int deref_ref,int recurse,enum val_prettyprint pretty,unsigned int i)760b725ae77Skettenis val_print_array_elements (struct type *type, char *valaddr, CORE_ADDR address,
761b725ae77Skettenis struct ui_file *stream, int format, int deref_ref,
762b725ae77Skettenis int recurse, enum val_prettyprint pretty,
763b725ae77Skettenis unsigned int i)
764e93f7393Sniklas {
765e93f7393Sniklas unsigned int things_printed = 0;
766e93f7393Sniklas unsigned len;
767e93f7393Sniklas struct type *elttype;
768e93f7393Sniklas unsigned eltlen;
769e93f7393Sniklas /* Position of the array element we are examining to see
770e93f7393Sniklas whether it is repeated. */
771e93f7393Sniklas unsigned int rep1;
772e93f7393Sniklas /* Number of repetitions we have detected so far. */
773e93f7393Sniklas unsigned int reps;
774e93f7393Sniklas
775e93f7393Sniklas elttype = TYPE_TARGET_TYPE (type);
776e93f7393Sniklas eltlen = TYPE_LENGTH (check_typedef (elttype));
777e93f7393Sniklas len = TYPE_LENGTH (type) / eltlen;
778e93f7393Sniklas
779e93f7393Sniklas annotate_array_section_begin (i, elttype);
780e93f7393Sniklas
781e93f7393Sniklas for (; i < len && things_printed < print_max; i++)
782e93f7393Sniklas {
783e93f7393Sniklas if (i != 0)
784e93f7393Sniklas {
785e93f7393Sniklas if (prettyprint_arrays)
786e93f7393Sniklas {
787e93f7393Sniklas fprintf_filtered (stream, ",\n");
788e93f7393Sniklas print_spaces_filtered (2 + 2 * recurse, stream);
789e93f7393Sniklas }
790e93f7393Sniklas else
791e93f7393Sniklas {
792e93f7393Sniklas fprintf_filtered (stream, ", ");
793e93f7393Sniklas }
794e93f7393Sniklas }
795e93f7393Sniklas wrap_here (n_spaces (2 + 2 * recurse));
796e93f7393Sniklas
797e93f7393Sniklas rep1 = i + 1;
798e93f7393Sniklas reps = 1;
799e93f7393Sniklas while ((rep1 < len) &&
800e93f7393Sniklas !memcmp (valaddr + i * eltlen, valaddr + rep1 * eltlen, eltlen))
801e93f7393Sniklas {
802e93f7393Sniklas ++reps;
803e93f7393Sniklas ++rep1;
804e93f7393Sniklas }
805e93f7393Sniklas
806e93f7393Sniklas if (reps > repeat_count_threshold)
807e93f7393Sniklas {
808b725ae77Skettenis val_print (elttype, valaddr + i * eltlen, 0, 0, stream, format,
809e93f7393Sniklas deref_ref, recurse + 1, pretty);
810e93f7393Sniklas annotate_elt_rep (reps);
811e93f7393Sniklas fprintf_filtered (stream, " <repeats %u times>", reps);
812e93f7393Sniklas annotate_elt_rep_end ();
813e93f7393Sniklas
814e93f7393Sniklas i = rep1 - 1;
815e93f7393Sniklas things_printed += repeat_count_threshold;
816e93f7393Sniklas }
817e93f7393Sniklas else
818e93f7393Sniklas {
819b725ae77Skettenis val_print (elttype, valaddr + i * eltlen, 0, 0, stream, format,
820e93f7393Sniklas deref_ref, recurse + 1, pretty);
821e93f7393Sniklas annotate_elt ();
822e93f7393Sniklas things_printed++;
823e93f7393Sniklas }
824e93f7393Sniklas }
825e93f7393Sniklas annotate_array_section_end ();
826e93f7393Sniklas if (i < len)
827e93f7393Sniklas {
828e93f7393Sniklas fprintf_filtered (stream, "...");
829e93f7393Sniklas }
830e93f7393Sniklas }
831e93f7393Sniklas
832b725ae77Skettenis /* Read LEN bytes of target memory at address MEMADDR, placing the
833b725ae77Skettenis results in GDB's memory at MYADDR. Returns a count of the bytes
834b725ae77Skettenis actually read, and optionally an errno value in the location
835b725ae77Skettenis pointed to by ERRNOPTR if ERRNOPTR is non-null. */
836b725ae77Skettenis
837b725ae77Skettenis /* FIXME: cagney/1999-10-14: Only used by val_print_string. Can this
838b725ae77Skettenis function be eliminated. */
839b725ae77Skettenis
840b725ae77Skettenis static int
partial_memory_read(CORE_ADDR memaddr,char * myaddr,int len,int * errnoptr)841b725ae77Skettenis partial_memory_read (CORE_ADDR memaddr, char *myaddr, int len, int *errnoptr)
842b725ae77Skettenis {
843b725ae77Skettenis int nread; /* Number of bytes actually read. */
844b725ae77Skettenis int errcode; /* Error from last read. */
845b725ae77Skettenis
846b725ae77Skettenis /* First try a complete read. */
847b725ae77Skettenis errcode = target_read_memory (memaddr, myaddr, len);
848b725ae77Skettenis if (errcode == 0)
849b725ae77Skettenis {
850b725ae77Skettenis /* Got it all. */
851b725ae77Skettenis nread = len;
852b725ae77Skettenis }
853b725ae77Skettenis else
854b725ae77Skettenis {
855b725ae77Skettenis /* Loop, reading one byte at a time until we get as much as we can. */
856b725ae77Skettenis for (errcode = 0, nread = 0; len > 0 && errcode == 0; nread++, len--)
857b725ae77Skettenis {
858b725ae77Skettenis errcode = target_read_memory (memaddr++, myaddr++, 1);
859b725ae77Skettenis }
860b725ae77Skettenis /* If an error, the last read was unsuccessful, so adjust count. */
861b725ae77Skettenis if (errcode != 0)
862b725ae77Skettenis {
863b725ae77Skettenis nread--;
864b725ae77Skettenis }
865b725ae77Skettenis }
866b725ae77Skettenis if (errnoptr != NULL)
867b725ae77Skettenis {
868b725ae77Skettenis *errnoptr = errcode;
869b725ae77Skettenis }
870b725ae77Skettenis return (nread);
871b725ae77Skettenis }
872b725ae77Skettenis
873e93f7393Sniklas /* Print a string from the inferior, starting at ADDR and printing up to LEN
874b725ae77Skettenis characters, of WIDTH bytes a piece, to STREAM. If LEN is -1, printing
875b725ae77Skettenis stops at the first null byte, otherwise printing proceeds (including null
876b725ae77Skettenis bytes) until either print_max or LEN characters have been printed,
877b725ae77Skettenis whichever is smaller. */
878e93f7393Sniklas
879b725ae77Skettenis /* FIXME: Use target_read_string. */
880e93f7393Sniklas
881e93f7393Sniklas int
val_print_string(CORE_ADDR addr,int len,int width,struct ui_file * stream)882b725ae77Skettenis val_print_string (CORE_ADDR addr, int len, int width, struct ui_file *stream)
883e93f7393Sniklas {
884e93f7393Sniklas int force_ellipsis = 0; /* Force ellipsis to be printed if nonzero. */
885e93f7393Sniklas int errcode; /* Errno returned from bad reads. */
886b725ae77Skettenis unsigned int fetchlimit; /* Maximum number of chars to print. */
887b725ae77Skettenis unsigned int nfetch; /* Chars to fetch / chars fetched. */
888b725ae77Skettenis unsigned int chunksize; /* Size of each fetch, in chars. */
889e93f7393Sniklas char *buffer = NULL; /* Dynamically growable fetch buffer. */
890e93f7393Sniklas char *bufptr; /* Pointer to next available byte in buffer. */
891e93f7393Sniklas char *limit; /* First location past end of fetch buffer. */
892e93f7393Sniklas struct cleanup *old_chain = NULL; /* Top of the old cleanup chain. */
893b725ae77Skettenis int found_nul; /* Non-zero if we found the nul char */
894e93f7393Sniklas
895e93f7393Sniklas /* First we need to figure out the limit on the number of characters we are
896e93f7393Sniklas going to attempt to fetch and print. This is actually pretty simple. If
897b725ae77Skettenis LEN >= zero, then the limit is the minimum of LEN and print_max. If
898b725ae77Skettenis LEN is -1, then the limit is print_max. This is true regardless of
899e93f7393Sniklas whether print_max is zero, UINT_MAX (unlimited), or something in between,
900e93f7393Sniklas because finding the null byte (or available memory) is what actually
901e93f7393Sniklas limits the fetch. */
902e93f7393Sniklas
903b725ae77Skettenis fetchlimit = (len == -1 ? print_max : min (len, print_max));
904e93f7393Sniklas
905e93f7393Sniklas /* Now decide how large of chunks to try to read in one operation. This
906b725ae77Skettenis is also pretty simple. If LEN >= zero, then we want fetchlimit chars,
907b725ae77Skettenis so we might as well read them all in one operation. If LEN is -1, we
908e93f7393Sniklas are looking for a null terminator to end the fetching, so we might as
909e93f7393Sniklas well read in blocks that are large enough to be efficient, but not so
910e93f7393Sniklas large as to be slow if fetchlimit happens to be large. So we choose the
911e93f7393Sniklas minimum of 8 and fetchlimit. We used to use 200 instead of 8 but
912e93f7393Sniklas 200 is way too big for remote debugging over a serial line. */
913e93f7393Sniklas
914b725ae77Skettenis chunksize = (len == -1 ? min (8, fetchlimit) : fetchlimit);
915e93f7393Sniklas
916e93f7393Sniklas /* Loop until we either have all the characters to print, or we encounter
917e93f7393Sniklas some error, such as bumping into the end of the address space. */
918e93f7393Sniklas
919b725ae77Skettenis found_nul = 0;
920b725ae77Skettenis old_chain = make_cleanup (null_cleanup, 0);
921b725ae77Skettenis
922b725ae77Skettenis if (len > 0)
923e93f7393Sniklas {
924b725ae77Skettenis buffer = (char *) xmalloc (len * width);
925e93f7393Sniklas bufptr = buffer;
926b725ae77Skettenis old_chain = make_cleanup (xfree, buffer);
927b725ae77Skettenis
928b725ae77Skettenis nfetch = partial_memory_read (addr, bufptr, len * width, &errcode)
929b725ae77Skettenis / width;
930b725ae77Skettenis addr += nfetch * width;
931b725ae77Skettenis bufptr += nfetch * width;
932e93f7393Sniklas }
933b725ae77Skettenis else if (len == -1)
934b725ae77Skettenis {
935b725ae77Skettenis unsigned long bufsize = 0;
936b725ae77Skettenis do
937b725ae77Skettenis {
938b725ae77Skettenis QUIT;
939b725ae77Skettenis nfetch = min (chunksize, fetchlimit - bufsize);
940b725ae77Skettenis
941b725ae77Skettenis if (buffer == NULL)
942b725ae77Skettenis buffer = (char *) xmalloc (nfetch * width);
943e93f7393Sniklas else
944e93f7393Sniklas {
945e93f7393Sniklas discard_cleanups (old_chain);
946b725ae77Skettenis buffer = (char *) xrealloc (buffer, (nfetch + bufsize) * width);
947e93f7393Sniklas }
948b725ae77Skettenis
949b725ae77Skettenis old_chain = make_cleanup (xfree, buffer);
950b725ae77Skettenis bufptr = buffer + bufsize * width;
951b725ae77Skettenis bufsize += nfetch;
952e93f7393Sniklas
953e93f7393Sniklas /* Read as much as we can. */
954b725ae77Skettenis nfetch = partial_memory_read (addr, bufptr, nfetch * width, &errcode)
955b725ae77Skettenis / width;
956b725ae77Skettenis
957e93f7393Sniklas /* Scan this chunk for the null byte that terminates the string
958e93f7393Sniklas to print. If found, we don't need to fetch any more. Note
959e93f7393Sniklas that bufptr is explicitly left pointing at the next character
960e93f7393Sniklas after the null byte, or at the next character after the end of
961e93f7393Sniklas the buffer. */
962b725ae77Skettenis
963b725ae77Skettenis limit = bufptr + nfetch * width;
964e93f7393Sniklas while (bufptr < limit)
965e93f7393Sniklas {
966b725ae77Skettenis unsigned long c;
967b725ae77Skettenis
968b725ae77Skettenis c = extract_unsigned_integer (bufptr, width);
969b725ae77Skettenis addr += width;
970b725ae77Skettenis bufptr += width;
971b725ae77Skettenis if (c == 0)
972e93f7393Sniklas {
973e93f7393Sniklas /* We don't care about any error which happened after
974e93f7393Sniklas the NULL terminator. */
975e93f7393Sniklas errcode = 0;
976b725ae77Skettenis found_nul = 1;
977e93f7393Sniklas break;
978e93f7393Sniklas }
979e93f7393Sniklas }
980e93f7393Sniklas }
981b725ae77Skettenis while (errcode == 0 /* no error */
982b725ae77Skettenis && bufptr - buffer < fetchlimit * width /* no overrun */
983b725ae77Skettenis && !found_nul); /* haven't found nul yet */
984b725ae77Skettenis }
985b725ae77Skettenis else
986b725ae77Skettenis { /* length of string is really 0! */
987b725ae77Skettenis buffer = bufptr = NULL;
988b725ae77Skettenis errcode = 0;
989b725ae77Skettenis }
990e93f7393Sniklas
991e93f7393Sniklas /* bufptr and addr now point immediately beyond the last byte which we
992e93f7393Sniklas consider part of the string (including a '\0' which ends the string). */
993e93f7393Sniklas
994e93f7393Sniklas /* We now have either successfully filled the buffer to fetchlimit, or
995b725ae77Skettenis terminated early due to an error or finding a null char when LEN is -1. */
996e93f7393Sniklas
997b725ae77Skettenis if (len == -1 && !found_nul)
998e93f7393Sniklas {
999b725ae77Skettenis char *peekbuf;
1000b725ae77Skettenis
1001e93f7393Sniklas /* We didn't find a null terminator we were looking for. Attempt
1002e93f7393Sniklas to peek at the next character. If not successful, or it is not
1003e93f7393Sniklas a null byte, then force ellipsis to be printed. */
1004b725ae77Skettenis
1005b725ae77Skettenis peekbuf = (char *) alloca (width);
1006b725ae77Skettenis
1007b725ae77Skettenis if (target_read_memory (addr, peekbuf, width) == 0
1008b725ae77Skettenis && extract_unsigned_integer (peekbuf, width) != 0)
1009e93f7393Sniklas force_ellipsis = 1;
1010e93f7393Sniklas }
1011b725ae77Skettenis else if ((len >= 0 && errcode != 0) || (len > (bufptr - buffer) / width))
1012e93f7393Sniklas {
1013e93f7393Sniklas /* Getting an error when we have a requested length, or fetching less
1014e93f7393Sniklas than the number of characters actually requested, always make us
1015e93f7393Sniklas print ellipsis. */
1016e93f7393Sniklas force_ellipsis = 1;
1017e93f7393Sniklas }
1018e93f7393Sniklas
1019e93f7393Sniklas QUIT;
1020e93f7393Sniklas
1021e93f7393Sniklas /* If we get an error before fetching anything, don't print a string.
1022e93f7393Sniklas But if we fetch something and then get an error, print the string
1023e93f7393Sniklas and then the error message. */
1024e93f7393Sniklas if (errcode == 0 || bufptr > buffer)
1025e93f7393Sniklas {
1026e93f7393Sniklas if (addressprint)
1027e93f7393Sniklas {
1028e93f7393Sniklas fputs_filtered (" ", stream);
1029e93f7393Sniklas }
1030b725ae77Skettenis LA_PRINT_STRING (stream, buffer, (bufptr - buffer) / width, width, force_ellipsis);
1031e93f7393Sniklas }
1032e93f7393Sniklas
1033e93f7393Sniklas if (errcode != 0)
1034e93f7393Sniklas {
1035e93f7393Sniklas if (errcode == EIO)
1036e93f7393Sniklas {
1037e93f7393Sniklas fprintf_filtered (stream, " <Address ");
1038e93f7393Sniklas print_address_numeric (addr, 1, stream);
1039e93f7393Sniklas fprintf_filtered (stream, " out of bounds>");
1040e93f7393Sniklas }
1041e93f7393Sniklas else
1042e93f7393Sniklas {
1043e93f7393Sniklas fprintf_filtered (stream, " <Error reading address ");
1044e93f7393Sniklas print_address_numeric (addr, 1, stream);
1045e93f7393Sniklas fprintf_filtered (stream, ": %s>", safe_strerror (errcode));
1046e93f7393Sniklas }
1047e93f7393Sniklas }
1048e93f7393Sniklas gdb_flush (stream);
1049e93f7393Sniklas do_cleanups (old_chain);
1050b725ae77Skettenis return ((bufptr - buffer) / width);
1051e93f7393Sniklas }
1052e93f7393Sniklas
1053b725ae77Skettenis
1054e93f7393Sniklas /* Validate an input or output radix setting, and make sure the user
1055e93f7393Sniklas knows what they really did here. Radix setting is confusing, e.g.
1056e93f7393Sniklas setting the input radix to "10" never changes it! */
1057e93f7393Sniklas
1058e93f7393Sniklas static void
set_input_radix(char * args,int from_tty,struct cmd_list_element * c)1059b725ae77Skettenis set_input_radix (char *args, int from_tty, struct cmd_list_element *c)
1060e93f7393Sniklas {
1061b725ae77Skettenis set_input_radix_1 (from_tty, input_radix);
1062e93f7393Sniklas }
1063e93f7393Sniklas
1064e93f7393Sniklas static void
set_input_radix_1(int from_tty,unsigned radix)1065b725ae77Skettenis set_input_radix_1 (int from_tty, unsigned radix)
1066e93f7393Sniklas {
1067e93f7393Sniklas /* We don't currently disallow any input radix except 0 or 1, which don't
1068e93f7393Sniklas make any mathematical sense. In theory, we can deal with any input
1069e93f7393Sniklas radix greater than 1, even if we don't have unique digits for every
1070e93f7393Sniklas value from 0 to radix-1, but in practice we lose on large radix values.
1071e93f7393Sniklas We should either fix the lossage or restrict the radix range more.
1072e93f7393Sniklas (FIXME). */
1073e93f7393Sniklas
1074e93f7393Sniklas if (radix < 2)
1075e93f7393Sniklas {
1076b725ae77Skettenis /* FIXME: cagney/2002-03-17: This needs to revert the bad radix
1077b725ae77Skettenis value. */
1078e93f7393Sniklas error ("Nonsense input radix ``decimal %u''; input radix unchanged.",
1079e93f7393Sniklas radix);
1080e93f7393Sniklas }
1081e93f7393Sniklas input_radix = radix;
1082e93f7393Sniklas if (from_tty)
1083e93f7393Sniklas {
1084e93f7393Sniklas printf_filtered ("Input radix now set to decimal %u, hex %x, octal %o.\n",
1085e93f7393Sniklas radix, radix, radix);
1086e93f7393Sniklas }
1087e93f7393Sniklas }
1088e93f7393Sniklas
1089e93f7393Sniklas static void
set_output_radix(char * args,int from_tty,struct cmd_list_element * c)1090b725ae77Skettenis set_output_radix (char *args, int from_tty, struct cmd_list_element *c)
1091e93f7393Sniklas {
1092b725ae77Skettenis set_output_radix_1 (from_tty, output_radix);
1093e93f7393Sniklas }
1094e93f7393Sniklas
1095e93f7393Sniklas static void
set_output_radix_1(int from_tty,unsigned radix)1096b725ae77Skettenis set_output_radix_1 (int from_tty, unsigned radix)
1097e93f7393Sniklas {
1098e93f7393Sniklas /* Validate the radix and disallow ones that we aren't prepared to
1099e93f7393Sniklas handle correctly, leaving the radix unchanged. */
1100e93f7393Sniklas switch (radix)
1101e93f7393Sniklas {
1102e93f7393Sniklas case 16:
1103e93f7393Sniklas output_format = 'x'; /* hex */
1104e93f7393Sniklas break;
1105e93f7393Sniklas case 10:
1106e93f7393Sniklas output_format = 0; /* decimal */
1107e93f7393Sniklas break;
1108e93f7393Sniklas case 8:
1109e93f7393Sniklas output_format = 'o'; /* octal */
1110e93f7393Sniklas break;
1111e93f7393Sniklas default:
1112b725ae77Skettenis /* FIXME: cagney/2002-03-17: This needs to revert the bad radix
1113b725ae77Skettenis value. */
1114e93f7393Sniklas error ("Unsupported output radix ``decimal %u''; output radix unchanged.",
1115e93f7393Sniklas radix);
1116e93f7393Sniklas }
1117e93f7393Sniklas output_radix = radix;
1118e93f7393Sniklas if (from_tty)
1119e93f7393Sniklas {
1120e93f7393Sniklas printf_filtered ("Output radix now set to decimal %u, hex %x, octal %o.\n",
1121e93f7393Sniklas radix, radix, radix);
1122e93f7393Sniklas }
1123e93f7393Sniklas }
1124e93f7393Sniklas
1125e93f7393Sniklas /* Set both the input and output radix at once. Try to set the output radix
1126e93f7393Sniklas first, since it has the most restrictive range. An radix that is valid as
1127e93f7393Sniklas an output radix is also valid as an input radix.
1128e93f7393Sniklas
1129e93f7393Sniklas It may be useful to have an unusual input radix. If the user wishes to
1130e93f7393Sniklas set an input radix that is not valid as an output radix, he needs to use
1131e93f7393Sniklas the 'set input-radix' command. */
1132e93f7393Sniklas
1133e93f7393Sniklas static void
set_radix(char * arg,int from_tty)1134b725ae77Skettenis set_radix (char *arg, int from_tty)
1135e93f7393Sniklas {
1136e93f7393Sniklas unsigned radix;
1137e93f7393Sniklas
1138b725ae77Skettenis radix = (arg == NULL) ? 10 : parse_and_eval_long (arg);
1139e93f7393Sniklas set_output_radix_1 (0, radix);
1140e93f7393Sniklas set_input_radix_1 (0, radix);
1141e93f7393Sniklas if (from_tty)
1142e93f7393Sniklas {
1143e93f7393Sniklas printf_filtered ("Input and output radices now set to decimal %u, hex %x, octal %o.\n",
1144e93f7393Sniklas radix, radix, radix);
1145e93f7393Sniklas }
1146e93f7393Sniklas }
1147e93f7393Sniklas
1148e93f7393Sniklas /* Show both the input and output radices. */
1149e93f7393Sniklas
1150e93f7393Sniklas static void
show_radix(char * arg,int from_tty)1151b725ae77Skettenis show_radix (char *arg, int from_tty)
1152e93f7393Sniklas {
1153e93f7393Sniklas if (from_tty)
1154e93f7393Sniklas {
1155e93f7393Sniklas if (input_radix == output_radix)
1156e93f7393Sniklas {
1157e93f7393Sniklas printf_filtered ("Input and output radices set to decimal %u, hex %x, octal %o.\n",
1158e93f7393Sniklas input_radix, input_radix, input_radix);
1159e93f7393Sniklas }
1160e93f7393Sniklas else
1161e93f7393Sniklas {
1162e93f7393Sniklas printf_filtered ("Input radix set to decimal %u, hex %x, octal %o.\n",
1163e93f7393Sniklas input_radix, input_radix, input_radix);
1164e93f7393Sniklas printf_filtered ("Output radix set to decimal %u, hex %x, octal %o.\n",
1165e93f7393Sniklas output_radix, output_radix, output_radix);
1166e93f7393Sniklas }
1167e93f7393Sniklas }
1168e93f7393Sniklas }
1169e93f7393Sniklas
1170b725ae77Skettenis
1171e93f7393Sniklas static void
set_print(char * arg,int from_tty)1172b725ae77Skettenis set_print (char *arg, int from_tty)
1173e93f7393Sniklas {
1174e93f7393Sniklas printf_unfiltered (
1175e93f7393Sniklas "\"set print\" must be followed by the name of a print subcommand.\n");
1176e93f7393Sniklas help_list (setprintlist, "set print ", -1, gdb_stdout);
1177e93f7393Sniklas }
1178e93f7393Sniklas
1179e93f7393Sniklas static void
show_print(char * args,int from_tty)1180b725ae77Skettenis show_print (char *args, int from_tty)
1181e93f7393Sniklas {
1182e93f7393Sniklas cmd_show_list (showprintlist, from_tty, "");
1183e93f7393Sniklas }
1184e93f7393Sniklas
1185e93f7393Sniklas void
_initialize_valprint(void)1186b725ae77Skettenis _initialize_valprint (void)
1187e93f7393Sniklas {
1188e93f7393Sniklas struct cmd_list_element *c;
1189e93f7393Sniklas
1190e93f7393Sniklas add_prefix_cmd ("print", no_class, set_print,
1191e93f7393Sniklas "Generic command for setting how things print.",
1192e93f7393Sniklas &setprintlist, "set print ", 0, &setlist);
1193e93f7393Sniklas add_alias_cmd ("p", "print", no_class, 1, &setlist);
1194e93f7393Sniklas /* prefer set print to set prompt */
1195e93f7393Sniklas add_alias_cmd ("pr", "print", no_class, 1, &setlist);
1196e93f7393Sniklas
1197e93f7393Sniklas add_prefix_cmd ("print", no_class, show_print,
1198e93f7393Sniklas "Generic command for showing print settings.",
1199e93f7393Sniklas &showprintlist, "show print ", 0, &showlist);
1200e93f7393Sniklas add_alias_cmd ("p", "print", no_class, 1, &showlist);
1201e93f7393Sniklas add_alias_cmd ("pr", "print", no_class, 1, &showlist);
1202e93f7393Sniklas
1203*63addd46Skettenis deprecated_add_show_from_set
1204e93f7393Sniklas (add_set_cmd ("elements", no_class, var_uinteger, (char *) &print_max,
1205e93f7393Sniklas "Set limit on string chars or array elements to print.\n\
1206e93f7393Sniklas \"set print elements 0\" causes there to be no limit.",
1207e93f7393Sniklas &setprintlist),
1208e93f7393Sniklas &showprintlist);
1209e93f7393Sniklas
1210*63addd46Skettenis deprecated_add_show_from_set
1211e93f7393Sniklas (add_set_cmd ("null-stop", no_class, var_boolean,
1212e93f7393Sniklas (char *) &stop_print_at_null,
1213e93f7393Sniklas "Set printing of char arrays to stop at first null char.",
1214e93f7393Sniklas &setprintlist),
1215e93f7393Sniklas &showprintlist);
1216e93f7393Sniklas
1217*63addd46Skettenis deprecated_add_show_from_set
1218e93f7393Sniklas (add_set_cmd ("repeats", no_class, var_uinteger,
1219e93f7393Sniklas (char *) &repeat_count_threshold,
1220e93f7393Sniklas "Set threshold for repeated print elements.\n\
1221e93f7393Sniklas \"set print repeats 0\" causes all elements to be individually printed.",
1222e93f7393Sniklas &setprintlist),
1223e93f7393Sniklas &showprintlist);
1224e93f7393Sniklas
1225*63addd46Skettenis deprecated_add_show_from_set
1226e93f7393Sniklas (add_set_cmd ("pretty", class_support, var_boolean,
1227e93f7393Sniklas (char *) &prettyprint_structs,
1228e93f7393Sniklas "Set prettyprinting of structures.",
1229e93f7393Sniklas &setprintlist),
1230e93f7393Sniklas &showprintlist);
1231e93f7393Sniklas
1232*63addd46Skettenis deprecated_add_show_from_set
1233e93f7393Sniklas (add_set_cmd ("union", class_support, var_boolean, (char *) &unionprint,
1234e93f7393Sniklas "Set printing of unions interior to structures.",
1235e93f7393Sniklas &setprintlist),
1236e93f7393Sniklas &showprintlist);
1237e93f7393Sniklas
1238*63addd46Skettenis deprecated_add_show_from_set
1239e93f7393Sniklas (add_set_cmd ("array", class_support, var_boolean,
1240e93f7393Sniklas (char *) &prettyprint_arrays,
1241e93f7393Sniklas "Set prettyprinting of arrays.",
1242e93f7393Sniklas &setprintlist),
1243e93f7393Sniklas &showprintlist);
1244e93f7393Sniklas
1245*63addd46Skettenis deprecated_add_show_from_set
1246e93f7393Sniklas (add_set_cmd ("address", class_support, var_boolean, (char *) &addressprint,
1247e93f7393Sniklas "Set printing of addresses.",
1248e93f7393Sniklas &setprintlist),
1249e93f7393Sniklas &showprintlist);
1250e93f7393Sniklas
1251e93f7393Sniklas c = add_set_cmd ("input-radix", class_support, var_uinteger,
1252e93f7393Sniklas (char *) &input_radix,
1253e93f7393Sniklas "Set default input radix for entering numbers.",
1254e93f7393Sniklas &setlist);
1255*63addd46Skettenis deprecated_add_show_from_set (c, &showlist);
1256b725ae77Skettenis set_cmd_sfunc (c, set_input_radix);
1257e93f7393Sniklas
1258e93f7393Sniklas c = add_set_cmd ("output-radix", class_support, var_uinteger,
1259e93f7393Sniklas (char *) &output_radix,
1260e93f7393Sniklas "Set default output radix for printing of values.",
1261e93f7393Sniklas &setlist);
1262*63addd46Skettenis deprecated_add_show_from_set (c, &showlist);
1263b725ae77Skettenis set_cmd_sfunc (c, set_output_radix);
1264e93f7393Sniklas
1265*63addd46Skettenis /* The "set radix" and "show radix" commands are special in that
1266*63addd46Skettenis they are like normal set and show commands but allow two normally
1267*63addd46Skettenis independent variables to be either set or shown with a single
1268*63addd46Skettenis command. So the usual deprecated_add_set_cmd() and
1269*63addd46Skettenis add_show_from_set() commands aren't really appropriate. */
1270e93f7393Sniklas add_cmd ("radix", class_support, set_radix,
1271e93f7393Sniklas "Set default input and output number radices.\n\
1272e93f7393Sniklas Use 'set input-radix' or 'set output-radix' to independently set each.\n\
1273e93f7393Sniklas Without an argument, sets both radices back to the default value of 10.",
1274e93f7393Sniklas &setlist);
1275e93f7393Sniklas add_cmd ("radix", class_support, show_radix,
1276e93f7393Sniklas "Show the default input and output number radices.\n\
1277e93f7393Sniklas Use 'show input-radix' or 'show output-radix' to independently show each.",
1278e93f7393Sniklas &showlist);
1279e93f7393Sniklas
1280e93f7393Sniklas /* Give people the defaults which they are used to. */
1281e93f7393Sniklas prettyprint_structs = 0;
1282e93f7393Sniklas prettyprint_arrays = 0;
1283e93f7393Sniklas unionprint = 1;
1284e93f7393Sniklas addressprint = 1;
1285e93f7393Sniklas print_max = PRINT_MAX_DEFAULT;
1286e93f7393Sniklas }
1287