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