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