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