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