| 1 | /* Print values for GDB, the GNU debugger. |
| 2 | Copyright 1986, 1988, 1989, 1991, 1992, 1993, 1994 |
| 3 | Free Software Foundation, Inc. |
| 4 | |
| 5 | This file is part of GDB. |
| 6 | |
| 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. |
| 11 | |
| 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. |
| 16 | |
| 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, Boston, MA 02111-1307, USA. */ |
| 20 | |
| 21 | #include "defs.h" |
| 22 | #include "gdb_string.h" |
| 23 | #include "symtab.h" |
| 24 | #include "gdbtypes.h" |
| 25 | #include "value.h" |
| 26 | #include "gdbcore.h" |
| 27 | #include "gdbcmd.h" |
| 28 | #include "target.h" |
| 29 | #include "obstack.h" |
| 30 | #include "language.h" |
| 31 | #include "demangle.h" |
| 32 | #include "annotate.h" |
| 33 | |
| 34 | #include <errno.h> |
| 35 | |
| 36 | /* Prototypes for local functions */ |
| 37 | |
| 38 | static void |
| 39 | print_hex_chars PARAMS ((GDB_FILE *, unsigned char *, unsigned int)); |
| 40 | |
| 41 | static void |
| 42 | show_print PARAMS ((char *, int)); |
| 43 | |
| 44 | static void |
| 45 | set_print PARAMS ((char *, int)); |
| 46 | |
| 47 | static void |
| 48 | set_radix PARAMS ((char *, int)); |
| 49 | |
| 50 | static void |
| 51 | show_radix PARAMS ((char *, int)); |
| 52 | |
| 53 | static void |
| 54 | set_input_radix PARAMS ((char *, int, struct cmd_list_element *)); |
| 55 | |
| 56 | static void |
| 57 | set_input_radix_1 PARAMS ((int, unsigned)); |
| 58 | |
| 59 | static void |
| 60 | set_output_radix PARAMS ((char *, int, struct cmd_list_element *)); |
| 61 | |
| 62 | static void |
| 63 | set_output_radix_1 PARAMS ((int, unsigned)); |
| 64 | |
| 65 | /* Maximum number of chars to print for a string pointer value or vector |
| 66 | contents, or UINT_MAX for no limit. Note that "set print elements 0" |
| 67 | stores UINT_MAX in print_max, which displays in a show command as |
| 68 | "unlimited". */ |
| 69 | |
| 70 | unsigned int print_max; |
| 71 | #define PRINT_MAX_DEFAULT 200 /* Start print_max off at this value. */ |
| 72 | |
| 73 | /* Default input and output radixes, and output format letter. */ |
| 74 | |
| 75 | unsigned input_radix = 10; |
| 76 | unsigned output_radix = 10; |
| 77 | int output_format = 0; |
| 78 | |
| 79 | /* Print repeat counts if there are more than this many repetitions of an |
| 80 | element in an array. Referenced by the low level language dependent |
| 81 | print routines. */ |
| 82 | |
| 83 | unsigned int repeat_count_threshold = 10; |
| 84 | |
| 85 | /* If nonzero, stops printing of char arrays at first null. */ |
| 86 | |
| 87 | int stop_print_at_null; |
| 88 | |
| 89 | /* Controls pretty printing of structures. */ |
| 90 | |
| 91 | int prettyprint_structs; |
| 92 | |
| 93 | /* Controls pretty printing of arrays. */ |
| 94 | |
| 95 | int prettyprint_arrays; |
| 96 | |
| 97 | /* If nonzero, causes unions inside structures or other unions to be |
| 98 | printed. */ |
| 99 | |
| 100 | int unionprint; /* Controls printing of nested unions. */ |
| 101 | |
| 102 | /* If nonzero, causes machine addresses to be printed in certain contexts. */ |
| 103 | |
| 104 | int addressprint; /* Controls printing of machine addresses */ |
| 105 | |
| 106 | \f |
| 107 | /* Print data of type TYPE located at VALADDR (within GDB), which came from |
| 108 | the inferior at address ADDRESS, onto stdio stream STREAM according to |
| 109 | FORMAT (a letter, or 0 for natural format using TYPE). |
| 110 | |
| 111 | If DEREF_REF is nonzero, then dereference references, otherwise just print |
| 112 | them like pointers. |
| 113 | |
| 114 | The PRETTY parameter controls prettyprinting. |
| 115 | |
| 116 | If the data are a string pointer, returns the number of string characters |
| 117 | printed. |
| 118 | |
| 119 | FIXME: The data at VALADDR is in target byte order. If gdb is ever |
| 120 | enhanced to be able to debug more than the single target it was compiled |
| 121 | for (specific CPU type and thus specific target byte ordering), then |
| 122 | either the print routines are going to have to take this into account, |
| 123 | or the data is going to have to be passed into here already converted |
| 124 | to the host byte ordering, whichever is more convenient. */ |
| 125 | |
| 126 | |
| 127 | int |
| 128 | val_print (type, valaddr, address, stream, format, deref_ref, recurse, pretty) |
| 129 | struct type *type; |
| 130 | char *valaddr; |
| 131 | CORE_ADDR address; |
| 132 | GDB_FILE *stream; |
| 133 | int format; |
| 134 | int deref_ref; |
| 135 | int recurse; |
| 136 | enum val_prettyprint pretty; |
| 137 | { |
| 138 | struct type *real_type = check_typedef (type); |
| 139 | if (pretty == Val_pretty_default) |
| 140 | { |
| 141 | pretty = prettyprint_structs ? Val_prettyprint : Val_no_prettyprint; |
| 142 | } |
| 143 | |
| 144 | QUIT; |
| 145 | |
| 146 | /* Ensure that the type is complete and not just a stub. If the type is |
| 147 | only a stub and we can't find and substitute its complete type, then |
| 148 | print appropriate string and return. */ |
| 149 | |
| 150 | if (TYPE_FLAGS (real_type) & TYPE_FLAG_STUB) |
| 151 | { |
| 152 | fprintf_filtered (stream, "<incomplete type>"); |
| 153 | gdb_flush (stream); |
| 154 | return (0); |
| 155 | } |
| 156 | |
| 157 | return (LA_VAL_PRINT (type, valaddr, address, stream, format, deref_ref, |
| 158 | recurse, pretty)); |
| 159 | } |
| 160 | |
| 161 | /* Print the value VAL in C-ish syntax on stream STREAM. |
| 162 | FORMAT is a format-letter, or 0 for print in natural format of data type. |
| 163 | If the object printed is a string pointer, returns |
| 164 | the number of string bytes printed. */ |
| 165 | |
| 166 | int |
| 167 | value_print (val, stream, format, pretty) |
| 168 | value_ptr val; |
| 169 | GDB_FILE *stream; |
| 170 | int format; |
| 171 | enum val_prettyprint pretty; |
| 172 | { |
| 173 | if (val == 0) |
| 174 | { |
| 175 | printf_filtered ("<address of value unknown>"); |
| 176 | return 0; |
| 177 | } |
| 178 | if (VALUE_OPTIMIZED_OUT (val)) |
| 179 | { |
| 180 | printf_filtered ("<value optimized out>"); |
| 181 | return 0; |
| 182 | } |
| 183 | return LA_VALUE_PRINT (val, stream, format, pretty); |
| 184 | } |
| 185 | |
| 186 | /* Called by various <lang>_val_print routines to print |
| 187 | TYPE_CODE_INT's. TYPE is the type. VALADDR is the address of the |
| 188 | value. STREAM is where to print the value. */ |
| 189 | |
| 190 | void |
| 191 | val_print_type_code_int (type, valaddr, stream) |
| 192 | struct type *type; |
| 193 | char *valaddr; |
| 194 | GDB_FILE *stream; |
| 195 | { |
| 196 | if (TYPE_LENGTH (type) > sizeof (LONGEST)) |
| 197 | { |
| 198 | LONGEST val; |
| 199 | |
| 200 | if (TYPE_UNSIGNED (type) |
| 201 | && extract_long_unsigned_integer (valaddr, TYPE_LENGTH (type), |
| 202 | &val)) |
| 203 | { |
| 204 | print_longest (stream, 'u', 0, val); |
| 205 | } |
| 206 | else |
| 207 | { |
| 208 | /* Signed, or we couldn't turn an unsigned value into a |
| 209 | LONGEST. For signed values, one could assume two's |
| 210 | complement (a reasonable assumption, I think) and do |
| 211 | better than this. */ |
| 212 | print_hex_chars (stream, (unsigned char *) valaddr, |
| 213 | TYPE_LENGTH (type)); |
| 214 | } |
| 215 | } |
| 216 | else |
| 217 | { |
| 218 | #ifdef PRINT_TYPELESS_INTEGER |
| 219 | PRINT_TYPELESS_INTEGER (stream, type, unpack_long (type, valaddr)); |
| 220 | #else |
| 221 | print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0, |
| 222 | unpack_long (type, valaddr)); |
| 223 | #endif |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | /* Print a number according to FORMAT which is one of d,u,x,o,b,h,w,g. |
| 228 | The raison d'etre of this function is to consolidate printing of LONG_LONG's |
| 229 | into this one function. Some platforms have long longs but don't have a |
| 230 | printf() that supports "ll" in the format string. We handle these by seeing |
| 231 | if the number is actually a long, and if not we just bail out and print the |
| 232 | number in hex. The format chars b,h,w,g are from |
| 233 | print_scalar_formatted(). If USE_LOCAL, format it according to the current |
| 234 | language (this should be used for most integers which GDB prints, the |
| 235 | exception is things like protocols where the format of the integer is |
| 236 | a protocol thing, not a user-visible thing). */ |
| 237 | |
| 238 | void |
| 239 | print_longest (stream, format, use_local, val_long) |
| 240 | GDB_FILE *stream; |
| 241 | int format; |
| 242 | int use_local; |
| 243 | LONGEST val_long; |
| 244 | { |
| 245 | #if defined (CC_HAS_LONG_LONG) && !defined (PRINTF_HAS_LONG_LONG) |
| 246 | long vtop, vbot; |
| 247 | |
| 248 | vtop = val_long >> (sizeof (long) * HOST_CHAR_BIT); |
| 249 | vbot = (long) val_long; |
| 250 | |
| 251 | if ((format == 'd' && (val_long < INT_MIN || val_long > INT_MAX)) |
| 252 | || ((format == 'u' || format == 'x') && (unsigned long long)val_long > UINT_MAX)) |
| 253 | { |
| 254 | fprintf_filtered (stream, "0x%lx%08lx", vtop, vbot); |
| 255 | return; |
| 256 | } |
| 257 | #endif |
| 258 | |
| 259 | #ifdef PRINTF_HAS_LONG_LONG |
| 260 | switch (format) |
| 261 | { |
| 262 | case 'd': |
| 263 | fprintf_filtered (stream, |
| 264 | use_local ? local_decimal_format_custom ("ll") |
| 265 | : "%lld", |
| 266 | val_long); |
| 267 | break; |
| 268 | case 'u': |
| 269 | fprintf_filtered (stream, "%llu", val_long); |
| 270 | break; |
| 271 | case 'x': |
| 272 | fprintf_filtered (stream, |
| 273 | use_local ? local_hex_format_custom ("ll") |
| 274 | : "%llx", |
| 275 | val_long); |
| 276 | break; |
| 277 | case 'o': |
| 278 | fprintf_filtered (stream, |
| 279 | use_local ? local_octal_format_custom ("ll") |
| 280 | : "%llo", |
| 281 | val_long); |
| 282 | break; |
| 283 | case 'b': |
| 284 | fprintf_filtered (stream, local_hex_format_custom ("02ll"), val_long); |
| 285 | break; |
| 286 | case 'h': |
| 287 | fprintf_filtered (stream, local_hex_format_custom ("04ll"), val_long); |
| 288 | break; |
| 289 | case 'w': |
| 290 | fprintf_filtered (stream, local_hex_format_custom ("08ll"), val_long); |
| 291 | break; |
| 292 | case 'g': |
| 293 | fprintf_filtered (stream, local_hex_format_custom ("016ll"), val_long); |
| 294 | break; |
| 295 | default: |
| 296 | abort (); |
| 297 | } |
| 298 | #else /* !PRINTF_HAS_LONG_LONG */ |
| 299 | /* In the following it is important to coerce (val_long) to a long. It does |
| 300 | nothing if !LONG_LONG, but it will chop off the top half (which we know |
| 301 | we can ignore) if the host supports long longs. */ |
| 302 | |
| 303 | switch (format) |
| 304 | { |
| 305 | case 'd': |
| 306 | fprintf_filtered (stream, |
| 307 | use_local ? local_decimal_format_custom ("l") |
| 308 | : "%ld", |
| 309 | (long) val_long); |
| 310 | break; |
| 311 | case 'u': |
| 312 | fprintf_filtered (stream, "%lu", (unsigned long) val_long); |
| 313 | break; |
| 314 | case 'x': |
| 315 | fprintf_filtered (stream, |
| 316 | use_local ? local_hex_format_custom ("l") |
| 317 | : "%lx", |
| 318 | (long) val_long); |
| 319 | break; |
| 320 | case 'o': |
| 321 | fprintf_filtered (stream, |
| 322 | use_local ? local_octal_format_custom ("l") |
| 323 | : "%lo", |
| 324 | (long) val_long); |
| 325 | break; |
| 326 | case 'b': |
| 327 | fprintf_filtered (stream, local_hex_format_custom ("02l"), |
| 328 | (long) val_long); |
| 329 | break; |
| 330 | case 'h': |
| 331 | fprintf_filtered (stream, local_hex_format_custom ("04l"), |
| 332 | (long) val_long); |
| 333 | break; |
| 334 | case 'w': |
| 335 | fprintf_filtered (stream, local_hex_format_custom ("08l"), |
| 336 | (long) val_long); |
| 337 | break; |
| 338 | case 'g': |
| 339 | fprintf_filtered (stream, local_hex_format_custom ("016l"), |
| 340 | (long) val_long); |
| 341 | break; |
| 342 | default: |
| 343 | abort (); |
| 344 | } |
| 345 | #endif /* !PRINTF_HAS_LONG_LONG */ |
| 346 | } |
| 347 | |
| 348 | /* This used to be a macro, but I don't think it is called often enough |
| 349 | to merit such treatment. */ |
| 350 | /* Convert a LONGEST to an int. This is used in contexts (e.g. number of |
| 351 | arguments to a function, number in a value history, register number, etc.) |
| 352 | where the value must not be larger than can fit in an int. */ |
| 353 | |
| 354 | int |
| 355 | longest_to_int (arg) |
| 356 | LONGEST arg; |
| 357 | { |
| 358 | |
| 359 | /* This check is in case a system header has botched the |
| 360 | definition of INT_MIN, like on BSDI. */ |
| 361 | if (sizeof (LONGEST) <= sizeof (int)) |
| 362 | return arg; |
| 363 | |
| 364 | if (arg > INT_MAX || arg < INT_MIN) |
| 365 | error ("Value out of range."); |
| 366 | |
| 367 | return arg; |
| 368 | } |
| 369 | |
| 370 | /* Print a floating point value of type TYPE, pointed to in GDB by VALADDR, |
| 371 | on STREAM. */ |
| 372 | |
| 373 | void |
| 374 | print_floating (valaddr, type, stream) |
| 375 | char *valaddr; |
| 376 | struct type *type; |
| 377 | GDB_FILE *stream; |
| 378 | { |
| 379 | DOUBLEST doub; |
| 380 | int inv; |
| 381 | unsigned len = TYPE_LENGTH (type); |
| 382 | |
| 383 | #if defined (IEEE_FLOAT) |
| 384 | |
| 385 | /* Check for NaN's. Note that this code does not depend on us being |
| 386 | on an IEEE conforming system. It only depends on the target |
| 387 | machine using IEEE representation. This means (a) |
| 388 | cross-debugging works right, and (2) IEEE_FLOAT can (and should) |
| 389 | be defined for systems like the 68881, which uses IEEE |
| 390 | representation, but is not IEEE conforming. */ |
| 391 | |
| 392 | { |
| 393 | unsigned long low, high; |
| 394 | /* Is the sign bit 0? */ |
| 395 | int nonnegative; |
| 396 | /* Is it is a NaN (i.e. the exponent is all ones and |
| 397 | the fraction is nonzero)? */ |
| 398 | int is_nan; |
| 399 | |
| 400 | if (len == 4) |
| 401 | { |
| 402 | /* It's single precision. */ |
| 403 | /* Assume that floating point byte order is the same as |
| 404 | integer byte order. */ |
| 405 | low = extract_unsigned_integer (valaddr, 4); |
| 406 | nonnegative = ((low & 0x80000000) == 0); |
| 407 | is_nan = ((((low >> 23) & 0xFF) == 0xFF) |
| 408 | && 0 != (low & 0x7FFFFF)); |
| 409 | low &= 0x7fffff; |
| 410 | high = 0; |
| 411 | } |
| 412 | else if (len == 8) |
| 413 | { |
| 414 | /* It's double precision. Get the high and low words. */ |
| 415 | |
| 416 | /* Assume that floating point byte order is the same as |
| 417 | integer byte order. */ |
| 418 | if (TARGET_BYTE_ORDER == BIG_ENDIAN) |
| 419 | { |
| 420 | low = extract_unsigned_integer (valaddr + 4, 4); |
| 421 | high = extract_unsigned_integer (valaddr, 4); |
| 422 | } |
| 423 | else |
| 424 | { |
| 425 | low = extract_unsigned_integer (valaddr, 4); |
| 426 | high = extract_unsigned_integer (valaddr + 4, 4); |
| 427 | } |
| 428 | nonnegative = ((high & 0x80000000) == 0); |
| 429 | is_nan = (((high >> 20) & 0x7ff) == 0x7ff |
| 430 | && ! ((((high & 0xfffff) == 0)) && (low == 0))); |
| 431 | high &= 0xfffff; |
| 432 | } |
| 433 | else |
| 434 | /* Extended. We can't detect NaNs for extendeds yet. Also note |
| 435 | that currently extendeds get nuked to double in |
| 436 | REGISTER_CONVERTIBLE. */ |
| 437 | is_nan = 0; |
| 438 | |
| 439 | if (is_nan) |
| 440 | { |
| 441 | /* The meaning of the sign and fraction is not defined by IEEE. |
| 442 | But the user might know what they mean. For example, they |
| 443 | (in an implementation-defined manner) distinguish between |
| 444 | signaling and quiet NaN's. */ |
| 445 | if (high) |
| 446 | fprintf_filtered (stream, "-NaN(0x%lx%.8lx)" + nonnegative, |
| 447 | high, low); |
| 448 | else |
| 449 | fprintf_filtered (stream, "-NaN(0x%lx)" + nonnegative, low); |
| 450 | return; |
| 451 | } |
| 452 | } |
| 453 | #endif /* IEEE_FLOAT. */ |
| 454 | |
| 455 | doub = unpack_double (type, valaddr, &inv); |
| 456 | if (inv) |
| 457 | { |
| 458 | fprintf_filtered (stream, "<invalid float value>"); |
| 459 | return; |
| 460 | } |
| 461 | |
| 462 | if (len < sizeof (double)) |
| 463 | fprintf_filtered (stream, "%.9g", (double) doub); |
| 464 | else if (len == sizeof (double)) |
| 465 | fprintf_filtered (stream, "%.17g", (double) doub); |
| 466 | else |
| 467 | fprintf_filtered (stream, "%.35Lg", doub); |
| 468 | } |
| 469 | |
| 470 | /* VALADDR points to an integer of LEN bytes. Print it in hex on stream. */ |
| 471 | |
| 472 | static void |
| 473 | print_hex_chars (stream, valaddr, len) |
| 474 | GDB_FILE *stream; |
| 475 | unsigned char *valaddr; |
| 476 | unsigned len; |
| 477 | { |
| 478 | unsigned char *p; |
| 479 | |
| 480 | /* FIXME: We should be not printing leading zeroes in most cases. */ |
| 481 | |
| 482 | fprintf_filtered (stream, local_hex_format_prefix ()); |
| 483 | if (TARGET_BYTE_ORDER == BIG_ENDIAN) |
| 484 | { |
| 485 | for (p = valaddr; |
| 486 | p < valaddr + len; |
| 487 | p++) |
| 488 | { |
| 489 | fprintf_filtered (stream, "%02x", *p); |
| 490 | } |
| 491 | } |
| 492 | else |
| 493 | { |
| 494 | for (p = valaddr + len - 1; |
| 495 | p >= valaddr; |
| 496 | p--) |
| 497 | { |
| 498 | fprintf_filtered (stream, "%02x", *p); |
| 499 | } |
| 500 | } |
| 501 | fprintf_filtered (stream, local_hex_format_suffix ()); |
| 502 | } |
| 503 | |
| 504 | /* Called by various <lang>_val_print routines to print elements of an |
| 505 | array in the form "<elem1>, <elem2>, <elem3>, ...". |
| 506 | |
| 507 | (FIXME?) Assumes array element separator is a comma, which is correct |
| 508 | for all languages currently handled. |
| 509 | (FIXME?) Some languages have a notation for repeated array elements, |
| 510 | perhaps we should try to use that notation when appropriate. |
| 511 | */ |
| 512 | |
| 513 | void |
| 514 | val_print_array_elements (type, valaddr, address, stream, format, deref_ref, |
| 515 | recurse, pretty, i) |
| 516 | struct type *type; |
| 517 | char *valaddr; |
| 518 | CORE_ADDR address; |
| 519 | GDB_FILE *stream; |
| 520 | int format; |
| 521 | int deref_ref; |
| 522 | int recurse; |
| 523 | enum val_prettyprint pretty; |
| 524 | unsigned int i; |
| 525 | { |
| 526 | unsigned int things_printed = 0; |
| 527 | unsigned len; |
| 528 | struct type *elttype; |
| 529 | unsigned eltlen; |
| 530 | /* Position of the array element we are examining to see |
| 531 | whether it is repeated. */ |
| 532 | unsigned int rep1; |
| 533 | /* Number of repetitions we have detected so far. */ |
| 534 | unsigned int reps; |
| 535 | |
| 536 | elttype = TYPE_TARGET_TYPE (type); |
| 537 | eltlen = TYPE_LENGTH (check_typedef (elttype)); |
| 538 | len = TYPE_LENGTH (type) / eltlen; |
| 539 | |
| 540 | annotate_array_section_begin (i, elttype); |
| 541 | |
| 542 | for (; i < len && things_printed < print_max; i++) |
| 543 | { |
| 544 | if (i != 0) |
| 545 | { |
| 546 | if (prettyprint_arrays) |
| 547 | { |
| 548 | fprintf_filtered (stream, ",\n"); |
| 549 | print_spaces_filtered (2 + 2 * recurse, stream); |
| 550 | } |
| 551 | else |
| 552 | { |
| 553 | fprintf_filtered (stream, ", "); |
| 554 | } |
| 555 | } |
| 556 | wrap_here (n_spaces (2 + 2 * recurse)); |
| 557 | |
| 558 | rep1 = i + 1; |
| 559 | reps = 1; |
| 560 | while ((rep1 < len) && |
| 561 | !memcmp (valaddr + i * eltlen, valaddr + rep1 * eltlen, eltlen)) |
| 562 | { |
| 563 | ++reps; |
| 564 | ++rep1; |
| 565 | } |
| 566 | |
| 567 | if (reps > repeat_count_threshold) |
| 568 | { |
| 569 | val_print (elttype, valaddr + i * eltlen, 0, stream, format, |
| 570 | deref_ref, recurse + 1, pretty); |
| 571 | annotate_elt_rep (reps); |
| 572 | fprintf_filtered (stream, " <repeats %u times>", reps); |
| 573 | annotate_elt_rep_end (); |
| 574 | |
| 575 | i = rep1 - 1; |
| 576 | things_printed += repeat_count_threshold; |
| 577 | } |
| 578 | else |
| 579 | { |
| 580 | val_print (elttype, valaddr + i * eltlen, 0, stream, format, |
| 581 | deref_ref, recurse + 1, pretty); |
| 582 | annotate_elt (); |
| 583 | things_printed++; |
| 584 | } |
| 585 | } |
| 586 | annotate_array_section_end (); |
| 587 | if (i < len) |
| 588 | { |
| 589 | fprintf_filtered (stream, "..."); |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | /* Print a string from the inferior, starting at ADDR and printing up to LEN |
| 594 | characters, to STREAM. If LEN is zero, printing stops at the first null |
| 595 | byte, otherwise printing proceeds (including null bytes) until either |
| 596 | print_max or LEN characters have been printed, whichever is smaller. */ |
| 597 | |
| 598 | /* FIXME: All callers supply LEN of zero. Supplying a non-zero LEN is |
| 599 | pointless, this routine just then becomes a convoluted version of |
| 600 | target_read_memory_partial. Removing all the LEN stuff would simplify |
| 601 | this routine enormously. |
| 602 | |
| 603 | FIXME: Use target_read_string. */ |
| 604 | |
| 605 | int |
| 606 | val_print_string (addr, len, stream) |
| 607 | CORE_ADDR addr; |
| 608 | unsigned int len; |
| 609 | GDB_FILE *stream; |
| 610 | { |
| 611 | int force_ellipsis = 0; /* Force ellipsis to be printed if nonzero. */ |
| 612 | int errcode; /* Errno returned from bad reads. */ |
| 613 | unsigned int fetchlimit; /* Maximum number of bytes to fetch. */ |
| 614 | unsigned int nfetch; /* Bytes to fetch / bytes fetched. */ |
| 615 | unsigned int chunksize; /* Size of each fetch, in bytes. */ |
| 616 | unsigned int bufsize; /* Size of current fetch buffer. */ |
| 617 | char *buffer = NULL; /* Dynamically growable fetch buffer. */ |
| 618 | char *bufptr; /* Pointer to next available byte in buffer. */ |
| 619 | char *limit; /* First location past end of fetch buffer. */ |
| 620 | struct cleanup *old_chain = NULL; /* Top of the old cleanup chain. */ |
| 621 | char peekchar; /* Place into which we can read one char. */ |
| 622 | |
| 623 | /* First we need to figure out the limit on the number of characters we are |
| 624 | going to attempt to fetch and print. This is actually pretty simple. If |
| 625 | LEN is nonzero, then the limit is the minimum of LEN and print_max. If |
| 626 | LEN is zero, then the limit is print_max. This is true regardless of |
| 627 | whether print_max is zero, UINT_MAX (unlimited), or something in between, |
| 628 | because finding the null byte (or available memory) is what actually |
| 629 | limits the fetch. */ |
| 630 | |
| 631 | fetchlimit = (len == 0 ? print_max : min (len, print_max)); |
| 632 | |
| 633 | /* Now decide how large of chunks to try to read in one operation. This |
| 634 | is also pretty simple. If LEN is nonzero, then we want fetchlimit bytes, |
| 635 | so we might as well read them all in one operation. If LEN is zero, we |
| 636 | are looking for a null terminator to end the fetching, so we might as |
| 637 | well read in blocks that are large enough to be efficient, but not so |
| 638 | large as to be slow if fetchlimit happens to be large. So we choose the |
| 639 | minimum of 8 and fetchlimit. We used to use 200 instead of 8 but |
| 640 | 200 is way too big for remote debugging over a serial line. */ |
| 641 | |
| 642 | chunksize = (len == 0 ? min (8, fetchlimit) : fetchlimit); |
| 643 | |
| 644 | /* Loop until we either have all the characters to print, or we encounter |
| 645 | some error, such as bumping into the end of the address space. */ |
| 646 | |
| 647 | bufsize = 0; |
| 648 | do { |
| 649 | QUIT; |
| 650 | /* Figure out how much to fetch this time, and grow the buffer to fit. */ |
| 651 | nfetch = min (chunksize, fetchlimit - bufsize); |
| 652 | bufsize += nfetch; |
| 653 | if (buffer == NULL) |
| 654 | { |
| 655 | buffer = (char *) xmalloc (bufsize); |
| 656 | bufptr = buffer; |
| 657 | } |
| 658 | else |
| 659 | { |
| 660 | discard_cleanups (old_chain); |
| 661 | buffer = (char *) xrealloc (buffer, bufsize); |
| 662 | bufptr = buffer + bufsize - nfetch; |
| 663 | } |
| 664 | old_chain = make_cleanup (free, buffer); |
| 665 | |
| 666 | /* Read as much as we can. */ |
| 667 | nfetch = target_read_memory_partial (addr, bufptr, nfetch, &errcode); |
| 668 | if (len != 0) |
| 669 | { |
| 670 | addr += nfetch; |
| 671 | bufptr += nfetch; |
| 672 | } |
| 673 | else |
| 674 | { |
| 675 | /* Scan this chunk for the null byte that terminates the string |
| 676 | to print. If found, we don't need to fetch any more. Note |
| 677 | that bufptr is explicitly left pointing at the next character |
| 678 | after the null byte, or at the next character after the end of |
| 679 | the buffer. */ |
| 680 | limit = bufptr + nfetch; |
| 681 | while (bufptr < limit) |
| 682 | { |
| 683 | ++addr; |
| 684 | ++bufptr; |
| 685 | if (bufptr[-1] == '\0') |
| 686 | { |
| 687 | /* We don't care about any error which happened after |
| 688 | the NULL terminator. */ |
| 689 | errcode = 0; |
| 690 | break; |
| 691 | } |
| 692 | } |
| 693 | } |
| 694 | } while (errcode == 0 /* no error */ |
| 695 | && bufsize < fetchlimit /* no overrun */ |
| 696 | && !(len == 0 && *(bufptr - 1) == '\0')); /* no null term */ |
| 697 | |
| 698 | /* bufptr and addr now point immediately beyond the last byte which we |
| 699 | consider part of the string (including a '\0' which ends the string). */ |
| 700 | |
| 701 | /* We now have either successfully filled the buffer to fetchlimit, or |
| 702 | terminated early due to an error or finding a null byte when LEN is |
| 703 | zero. */ |
| 704 | |
| 705 | if (len == 0 && bufptr > buffer && *(bufptr - 1) != '\0') |
| 706 | { |
| 707 | /* We didn't find a null terminator we were looking for. Attempt |
| 708 | to peek at the next character. If not successful, or it is not |
| 709 | a null byte, then force ellipsis to be printed. */ |
| 710 | if (target_read_memory (addr, &peekchar, 1) != 0 || peekchar != '\0') |
| 711 | { |
| 712 | force_ellipsis = 1; |
| 713 | } |
| 714 | } |
| 715 | else if ((len != 0 && errcode != 0) || (len > bufptr - buffer)) |
| 716 | { |
| 717 | /* Getting an error when we have a requested length, or fetching less |
| 718 | than the number of characters actually requested, always make us |
| 719 | print ellipsis. */ |
| 720 | force_ellipsis = 1; |
| 721 | } |
| 722 | |
| 723 | QUIT; |
| 724 | |
| 725 | /* If we get an error before fetching anything, don't print a string. |
| 726 | But if we fetch something and then get an error, print the string |
| 727 | and then the error message. */ |
| 728 | if (errcode == 0 || bufptr > buffer) |
| 729 | { |
| 730 | if (addressprint) |
| 731 | { |
| 732 | fputs_filtered (" ", stream); |
| 733 | } |
| 734 | LA_PRINT_STRING (stream, buffer, bufptr - buffer, force_ellipsis); |
| 735 | } |
| 736 | |
| 737 | if (errcode != 0) |
| 738 | { |
| 739 | if (errcode == EIO) |
| 740 | { |
| 741 | fprintf_filtered (stream, " <Address "); |
| 742 | print_address_numeric (addr, 1, stream); |
| 743 | fprintf_filtered (stream, " out of bounds>"); |
| 744 | } |
| 745 | else |
| 746 | { |
| 747 | fprintf_filtered (stream, " <Error reading address "); |
| 748 | print_address_numeric (addr, 1, stream); |
| 749 | fprintf_filtered (stream, ": %s>", safe_strerror (errcode)); |
| 750 | } |
| 751 | } |
| 752 | gdb_flush (stream); |
| 753 | do_cleanups (old_chain); |
| 754 | return (bufptr - buffer); |
| 755 | } |
| 756 | |
| 757 | \f |
| 758 | /* Validate an input or output radix setting, and make sure the user |
| 759 | knows what they really did here. Radix setting is confusing, e.g. |
| 760 | setting the input radix to "10" never changes it! */ |
| 761 | |
| 762 | /* ARGSUSED */ |
| 763 | static void |
| 764 | set_input_radix (args, from_tty, c) |
| 765 | char *args; |
| 766 | int from_tty; |
| 767 | struct cmd_list_element *c; |
| 768 | { |
| 769 | set_input_radix_1 (from_tty, *(unsigned *)c->var); |
| 770 | } |
| 771 | |
| 772 | /* ARGSUSED */ |
| 773 | static void |
| 774 | set_input_radix_1 (from_tty, radix) |
| 775 | int from_tty; |
| 776 | unsigned radix; |
| 777 | { |
| 778 | /* We don't currently disallow any input radix except 0 or 1, which don't |
| 779 | make any mathematical sense. In theory, we can deal with any input |
| 780 | radix greater than 1, even if we don't have unique digits for every |
| 781 | value from 0 to radix-1, but in practice we lose on large radix values. |
| 782 | We should either fix the lossage or restrict the radix range more. |
| 783 | (FIXME). */ |
| 784 | |
| 785 | if (radix < 2) |
| 786 | { |
| 787 | error ("Nonsense input radix ``decimal %u''; input radix unchanged.", |
| 788 | radix); |
| 789 | } |
| 790 | input_radix = radix; |
| 791 | if (from_tty) |
| 792 | { |
| 793 | printf_filtered ("Input radix now set to decimal %u, hex %x, octal %o.\n", |
| 794 | radix, radix, radix); |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | /* ARGSUSED */ |
| 799 | static void |
| 800 | set_output_radix (args, from_tty, c) |
| 801 | char *args; |
| 802 | int from_tty; |
| 803 | struct cmd_list_element *c; |
| 804 | { |
| 805 | set_output_radix_1 (from_tty, *(unsigned *)c->var); |
| 806 | } |
| 807 | |
| 808 | static void |
| 809 | set_output_radix_1 (from_tty, radix) |
| 810 | int from_tty; |
| 811 | unsigned radix; |
| 812 | { |
| 813 | /* Validate the radix and disallow ones that we aren't prepared to |
| 814 | handle correctly, leaving the radix unchanged. */ |
| 815 | switch (radix) |
| 816 | { |
| 817 | case 16: |
| 818 | output_format = 'x'; /* hex */ |
| 819 | break; |
| 820 | case 10: |
| 821 | output_format = 0; /* decimal */ |
| 822 | break; |
| 823 | case 8: |
| 824 | output_format = 'o'; /* octal */ |
| 825 | break; |
| 826 | default: |
| 827 | error ("Unsupported output radix ``decimal %u''; output radix unchanged.", |
| 828 | radix); |
| 829 | } |
| 830 | output_radix = radix; |
| 831 | if (from_tty) |
| 832 | { |
| 833 | printf_filtered ("Output radix now set to decimal %u, hex %x, octal %o.\n", |
| 834 | radix, radix, radix); |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | /* Set both the input and output radix at once. Try to set the output radix |
| 839 | first, since it has the most restrictive range. An radix that is valid as |
| 840 | an output radix is also valid as an input radix. |
| 841 | |
| 842 | It may be useful to have an unusual input radix. If the user wishes to |
| 843 | set an input radix that is not valid as an output radix, he needs to use |
| 844 | the 'set input-radix' command. */ |
| 845 | |
| 846 | static void |
| 847 | set_radix (arg, from_tty) |
| 848 | char *arg; |
| 849 | int from_tty; |
| 850 | { |
| 851 | unsigned radix; |
| 852 | |
| 853 | radix = (arg == NULL) ? 10 : parse_and_eval_address (arg); |
| 854 | set_output_radix_1 (0, radix); |
| 855 | set_input_radix_1 (0, radix); |
| 856 | if (from_tty) |
| 857 | { |
| 858 | printf_filtered ("Input and output radices now set to decimal %u, hex %x, octal %o.\n", |
| 859 | radix, radix, radix); |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | /* Show both the input and output radices. */ |
| 864 | |
| 865 | /*ARGSUSED*/ |
| 866 | static void |
| 867 | show_radix (arg, from_tty) |
| 868 | char *arg; |
| 869 | int from_tty; |
| 870 | { |
| 871 | if (from_tty) |
| 872 | { |
| 873 | if (input_radix == output_radix) |
| 874 | { |
| 875 | printf_filtered ("Input and output radices set to decimal %u, hex %x, octal %o.\n", |
| 876 | input_radix, input_radix, input_radix); |
| 877 | } |
| 878 | else |
| 879 | { |
| 880 | printf_filtered ("Input radix set to decimal %u, hex %x, octal %o.\n", |
| 881 | input_radix, input_radix, input_radix); |
| 882 | printf_filtered ("Output radix set to decimal %u, hex %x, octal %o.\n", |
| 883 | output_radix, output_radix, output_radix); |
| 884 | } |
| 885 | } |
| 886 | } |
| 887 | |
| 888 | \f |
| 889 | /*ARGSUSED*/ |
| 890 | static void |
| 891 | set_print (arg, from_tty) |
| 892 | char *arg; |
| 893 | int from_tty; |
| 894 | { |
| 895 | printf_unfiltered ( |
| 896 | "\"set print\" must be followed by the name of a print subcommand.\n"); |
| 897 | help_list (setprintlist, "set print ", -1, gdb_stdout); |
| 898 | } |
| 899 | |
| 900 | /*ARGSUSED*/ |
| 901 | static void |
| 902 | show_print (args, from_tty) |
| 903 | char *args; |
| 904 | int from_tty; |
| 905 | { |
| 906 | cmd_show_list (showprintlist, from_tty, ""); |
| 907 | } |
| 908 | \f |
| 909 | void |
| 910 | _initialize_valprint () |
| 911 | { |
| 912 | struct cmd_list_element *c; |
| 913 | |
| 914 | add_prefix_cmd ("print", no_class, set_print, |
| 915 | "Generic command for setting how things print.", |
| 916 | &setprintlist, "set print ", 0, &setlist); |
| 917 | add_alias_cmd ("p", "print", no_class, 1, &setlist); |
| 918 | /* prefer set print to set prompt */ |
| 919 | add_alias_cmd ("pr", "print", no_class, 1, &setlist); |
| 920 | |
| 921 | add_prefix_cmd ("print", no_class, show_print, |
| 922 | "Generic command for showing print settings.", |
| 923 | &showprintlist, "show print ", 0, &showlist); |
| 924 | add_alias_cmd ("p", "print", no_class, 1, &showlist); |
| 925 | add_alias_cmd ("pr", "print", no_class, 1, &showlist); |
| 926 | |
| 927 | add_show_from_set |
| 928 | (add_set_cmd ("elements", no_class, var_uinteger, (char *)&print_max, |
| 929 | "Set limit on string chars or array elements to print.\n\ |
| 930 | \"set print elements 0\" causes there to be no limit.", |
| 931 | &setprintlist), |
| 932 | &showprintlist); |
| 933 | |
| 934 | add_show_from_set |
| 935 | (add_set_cmd ("null-stop", no_class, var_boolean, |
| 936 | (char *)&stop_print_at_null, |
| 937 | "Set printing of char arrays to stop at first null char.", |
| 938 | &setprintlist), |
| 939 | &showprintlist); |
| 940 | |
| 941 | add_show_from_set |
| 942 | (add_set_cmd ("repeats", no_class, var_uinteger, |
| 943 | (char *)&repeat_count_threshold, |
| 944 | "Set threshold for repeated print elements.\n\ |
| 945 | \"set print repeats 0\" causes all elements to be individually printed.", |
| 946 | &setprintlist), |
| 947 | &showprintlist); |
| 948 | |
| 949 | add_show_from_set |
| 950 | (add_set_cmd ("pretty", class_support, var_boolean, |
| 951 | (char *)&prettyprint_structs, |
| 952 | "Set prettyprinting of structures.", |
| 953 | &setprintlist), |
| 954 | &showprintlist); |
| 955 | |
| 956 | add_show_from_set |
| 957 | (add_set_cmd ("union", class_support, var_boolean, (char *)&unionprint, |
| 958 | "Set printing of unions interior to structures.", |
| 959 | &setprintlist), |
| 960 | &showprintlist); |
| 961 | |
| 962 | add_show_from_set |
| 963 | (add_set_cmd ("array", class_support, var_boolean, |
| 964 | (char *)&prettyprint_arrays, |
| 965 | "Set prettyprinting of arrays.", |
| 966 | &setprintlist), |
| 967 | &showprintlist); |
| 968 | |
| 969 | add_show_from_set |
| 970 | (add_set_cmd ("address", class_support, var_boolean, (char *)&addressprint, |
| 971 | "Set printing of addresses.", |
| 972 | &setprintlist), |
| 973 | &showprintlist); |
| 974 | |
| 975 | c = add_set_cmd ("input-radix", class_support, var_uinteger, |
| 976 | (char *)&input_radix, |
| 977 | "Set default input radix for entering numbers.", |
| 978 | &setlist); |
| 979 | add_show_from_set (c, &showlist); |
| 980 | c->function.sfunc = set_input_radix; |
| 981 | |
| 982 | c = add_set_cmd ("output-radix", class_support, var_uinteger, |
| 983 | (char *)&output_radix, |
| 984 | "Set default output radix for printing of values.", |
| 985 | &setlist); |
| 986 | add_show_from_set (c, &showlist); |
| 987 | c->function.sfunc = set_output_radix; |
| 988 | |
| 989 | /* The "set radix" and "show radix" commands are special in that they are |
| 990 | like normal set and show commands but allow two normally independent |
| 991 | variables to be either set or shown with a single command. So the |
| 992 | usual add_set_cmd() and add_show_from_set() commands aren't really |
| 993 | appropriate. */ |
| 994 | add_cmd ("radix", class_support, set_radix, |
| 995 | "Set default input and output number radices.\n\ |
| 996 | Use 'set input-radix' or 'set output-radix' to independently set each.\n\ |
| 997 | Without an argument, sets both radices back to the default value of 10.", |
| 998 | &setlist); |
| 999 | add_cmd ("radix", class_support, show_radix, |
| 1000 | "Show the default input and output number radices.\n\ |
| 1001 | Use 'show input-radix' or 'show output-radix' to independently show each.", |
| 1002 | &showlist); |
| 1003 | |
| 1004 | /* Give people the defaults which they are used to. */ |
| 1005 | prettyprint_structs = 0; |
| 1006 | prettyprint_arrays = 0; |
| 1007 | unionprint = 1; |
| 1008 | addressprint = 1; |
| 1009 | print_max = PRINT_MAX_DEFAULT; |
| 1010 | } |