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