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