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