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