| 1 | /* Print values for GDB, the GNU debugger. |
| 2 | |
| 3 | Copyright (C) 1986, 1988-2012 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 3 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, see <http://www.gnu.org/licenses/>. */ |
| 19 | |
| 20 | #include "defs.h" |
| 21 | #include "gdb_string.h" |
| 22 | #include "symtab.h" |
| 23 | #include "gdbtypes.h" |
| 24 | #include "value.h" |
| 25 | #include "gdbcore.h" |
| 26 | #include "gdbcmd.h" |
| 27 | #include "target.h" |
| 28 | #include "language.h" |
| 29 | #include "annotate.h" |
| 30 | #include "valprint.h" |
| 31 | #include "floatformat.h" |
| 32 | #include "doublest.h" |
| 33 | #include "exceptions.h" |
| 34 | #include "dfp.h" |
| 35 | #include "python/python.h" |
| 36 | #include "ada-lang.h" |
| 37 | #include "gdb_obstack.h" |
| 38 | #include "charset.h" |
| 39 | #include <ctype.h> |
| 40 | |
| 41 | #include <errno.h> |
| 42 | |
| 43 | /* Prototypes for local functions */ |
| 44 | |
| 45 | static int partial_memory_read (CORE_ADDR memaddr, gdb_byte *myaddr, |
| 46 | int len, int *errnoptr); |
| 47 | |
| 48 | static void show_print (char *, int); |
| 49 | |
| 50 | static void set_print (char *, int); |
| 51 | |
| 52 | static void set_radix (char *, int); |
| 53 | |
| 54 | static void show_radix (char *, int); |
| 55 | |
| 56 | static void set_input_radix (char *, int, struct cmd_list_element *); |
| 57 | |
| 58 | static void set_input_radix_1 (int, unsigned); |
| 59 | |
| 60 | static void set_output_radix (char *, int, struct cmd_list_element *); |
| 61 | |
| 62 | static void set_output_radix_1 (int, unsigned); |
| 63 | |
| 64 | void _initialize_valprint (void); |
| 65 | |
| 66 | #define PRINT_MAX_DEFAULT 200 /* Start print_max off at this value. */ |
| 67 | |
| 68 | struct value_print_options user_print_options = |
| 69 | { |
| 70 | Val_pretty_default, /* pretty */ |
| 71 | 0, /* prettyprint_arrays */ |
| 72 | 0, /* prettyprint_structs */ |
| 73 | 0, /* vtblprint */ |
| 74 | 1, /* unionprint */ |
| 75 | 1, /* addressprint */ |
| 76 | 0, /* objectprint */ |
| 77 | PRINT_MAX_DEFAULT, /* print_max */ |
| 78 | 10, /* repeat_count_threshold */ |
| 79 | 0, /* output_format */ |
| 80 | 0, /* format */ |
| 81 | 0, /* stop_print_at_null */ |
| 82 | 0, /* inspect_it */ |
| 83 | 0, /* print_array_indexes */ |
| 84 | 0, /* deref_ref */ |
| 85 | 1, /* static_field_print */ |
| 86 | 1, /* pascal_static_field_print */ |
| 87 | 0, /* raw */ |
| 88 | 0, /* summary */ |
| 89 | 1 /* symbol_print */ |
| 90 | }; |
| 91 | |
| 92 | /* Initialize *OPTS to be a copy of the user print options. */ |
| 93 | void |
| 94 | get_user_print_options (struct value_print_options *opts) |
| 95 | { |
| 96 | *opts = user_print_options; |
| 97 | } |
| 98 | |
| 99 | /* Initialize *OPTS to be a copy of the user print options, but with |
| 100 | pretty-printing disabled. */ |
| 101 | void |
| 102 | get_raw_print_options (struct value_print_options *opts) |
| 103 | { |
| 104 | *opts = user_print_options; |
| 105 | opts->pretty = Val_no_prettyprint; |
| 106 | } |
| 107 | |
| 108 | /* Initialize *OPTS to be a copy of the user print options, but using |
| 109 | FORMAT as the formatting option. */ |
| 110 | void |
| 111 | get_formatted_print_options (struct value_print_options *opts, |
| 112 | char format) |
| 113 | { |
| 114 | *opts = user_print_options; |
| 115 | opts->format = format; |
| 116 | } |
| 117 | |
| 118 | static void |
| 119 | show_print_max (struct ui_file *file, int from_tty, |
| 120 | struct cmd_list_element *c, const char *value) |
| 121 | { |
| 122 | fprintf_filtered (file, |
| 123 | _("Limit on string chars or array " |
| 124 | "elements to print is %s.\n"), |
| 125 | value); |
| 126 | } |
| 127 | |
| 128 | |
| 129 | /* Default input and output radixes, and output format letter. */ |
| 130 | |
| 131 | unsigned input_radix = 10; |
| 132 | static void |
| 133 | show_input_radix (struct ui_file *file, int from_tty, |
| 134 | struct cmd_list_element *c, const char *value) |
| 135 | { |
| 136 | fprintf_filtered (file, |
| 137 | _("Default input radix for entering numbers is %s.\n"), |
| 138 | value); |
| 139 | } |
| 140 | |
| 141 | unsigned output_radix = 10; |
| 142 | static void |
| 143 | show_output_radix (struct ui_file *file, int from_tty, |
| 144 | struct cmd_list_element *c, const char *value) |
| 145 | { |
| 146 | fprintf_filtered (file, |
| 147 | _("Default output radix for printing of values is %s.\n"), |
| 148 | value); |
| 149 | } |
| 150 | |
| 151 | /* By default we print arrays without printing the index of each element in |
| 152 | the array. This behavior can be changed by setting PRINT_ARRAY_INDEXES. */ |
| 153 | |
| 154 | static void |
| 155 | show_print_array_indexes (struct ui_file *file, int from_tty, |
| 156 | struct cmd_list_element *c, const char *value) |
| 157 | { |
| 158 | fprintf_filtered (file, _("Printing of array indexes is %s.\n"), value); |
| 159 | } |
| 160 | |
| 161 | /* Print repeat counts if there are more than this many repetitions of an |
| 162 | element in an array. Referenced by the low level language dependent |
| 163 | print routines. */ |
| 164 | |
| 165 | static void |
| 166 | show_repeat_count_threshold (struct ui_file *file, int from_tty, |
| 167 | struct cmd_list_element *c, const char *value) |
| 168 | { |
| 169 | fprintf_filtered (file, _("Threshold for repeated print elements is %s.\n"), |
| 170 | value); |
| 171 | } |
| 172 | |
| 173 | /* If nonzero, stops printing of char arrays at first null. */ |
| 174 | |
| 175 | static void |
| 176 | show_stop_print_at_null (struct ui_file *file, int from_tty, |
| 177 | struct cmd_list_element *c, const char *value) |
| 178 | { |
| 179 | fprintf_filtered (file, |
| 180 | _("Printing of char arrays to stop " |
| 181 | "at first null char is %s.\n"), |
| 182 | value); |
| 183 | } |
| 184 | |
| 185 | /* Controls pretty printing of structures. */ |
| 186 | |
| 187 | static void |
| 188 | show_prettyprint_structs (struct ui_file *file, int from_tty, |
| 189 | struct cmd_list_element *c, const char *value) |
| 190 | { |
| 191 | fprintf_filtered (file, _("Prettyprinting of structures is %s.\n"), value); |
| 192 | } |
| 193 | |
| 194 | /* Controls pretty printing of arrays. */ |
| 195 | |
| 196 | static void |
| 197 | show_prettyprint_arrays (struct ui_file *file, int from_tty, |
| 198 | struct cmd_list_element *c, const char *value) |
| 199 | { |
| 200 | fprintf_filtered (file, _("Prettyprinting of arrays is %s.\n"), value); |
| 201 | } |
| 202 | |
| 203 | /* If nonzero, causes unions inside structures or other unions to be |
| 204 | printed. */ |
| 205 | |
| 206 | static void |
| 207 | show_unionprint (struct ui_file *file, int from_tty, |
| 208 | struct cmd_list_element *c, const char *value) |
| 209 | { |
| 210 | fprintf_filtered (file, |
| 211 | _("Printing of unions interior to structures is %s.\n"), |
| 212 | value); |
| 213 | } |
| 214 | |
| 215 | /* If nonzero, causes machine addresses to be printed in certain contexts. */ |
| 216 | |
| 217 | static void |
| 218 | show_addressprint (struct ui_file *file, int from_tty, |
| 219 | struct cmd_list_element *c, const char *value) |
| 220 | { |
| 221 | fprintf_filtered (file, _("Printing of addresses is %s.\n"), value); |
| 222 | } |
| 223 | |
| 224 | static void |
| 225 | show_symbol_print (struct ui_file *file, int from_tty, |
| 226 | struct cmd_list_element *c, const char *value) |
| 227 | { |
| 228 | fprintf_filtered (file, |
| 229 | _("Printing of symbols when printing pointers is %s.\n"), |
| 230 | value); |
| 231 | } |
| 232 | |
| 233 | \f |
| 234 | |
| 235 | /* A helper function for val_print. When printing in "summary" mode, |
| 236 | we want to print scalar arguments, but not aggregate arguments. |
| 237 | This function distinguishes between the two. */ |
| 238 | |
| 239 | static int |
| 240 | scalar_type_p (struct type *type) |
| 241 | { |
| 242 | CHECK_TYPEDEF (type); |
| 243 | while (TYPE_CODE (type) == TYPE_CODE_REF) |
| 244 | { |
| 245 | type = TYPE_TARGET_TYPE (type); |
| 246 | CHECK_TYPEDEF (type); |
| 247 | } |
| 248 | switch (TYPE_CODE (type)) |
| 249 | { |
| 250 | case TYPE_CODE_ARRAY: |
| 251 | case TYPE_CODE_STRUCT: |
| 252 | case TYPE_CODE_UNION: |
| 253 | case TYPE_CODE_SET: |
| 254 | case TYPE_CODE_STRING: |
| 255 | return 0; |
| 256 | default: |
| 257 | return 1; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | /* See its definition in value.h. */ |
| 262 | |
| 263 | int |
| 264 | valprint_check_validity (struct ui_file *stream, |
| 265 | struct type *type, |
| 266 | int embedded_offset, |
| 267 | const struct value *val) |
| 268 | { |
| 269 | CHECK_TYPEDEF (type); |
| 270 | |
| 271 | if (TYPE_CODE (type) != TYPE_CODE_UNION |
| 272 | && TYPE_CODE (type) != TYPE_CODE_STRUCT |
| 273 | && TYPE_CODE (type) != TYPE_CODE_ARRAY) |
| 274 | { |
| 275 | if (!value_bits_valid (val, TARGET_CHAR_BIT * embedded_offset, |
| 276 | TARGET_CHAR_BIT * TYPE_LENGTH (type))) |
| 277 | { |
| 278 | val_print_optimized_out (stream); |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | if (value_bits_synthetic_pointer (val, TARGET_CHAR_BIT * embedded_offset, |
| 283 | TARGET_CHAR_BIT * TYPE_LENGTH (type))) |
| 284 | { |
| 285 | fputs_filtered (_("<synthetic pointer>"), stream); |
| 286 | return 0; |
| 287 | } |
| 288 | |
| 289 | if (!value_bytes_available (val, embedded_offset, TYPE_LENGTH (type))) |
| 290 | { |
| 291 | val_print_unavailable (stream); |
| 292 | return 0; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | return 1; |
| 297 | } |
| 298 | |
| 299 | void |
| 300 | val_print_optimized_out (struct ui_file *stream) |
| 301 | { |
| 302 | fprintf_filtered (stream, _("<optimized out>")); |
| 303 | } |
| 304 | |
| 305 | void |
| 306 | val_print_unavailable (struct ui_file *stream) |
| 307 | { |
| 308 | fprintf_filtered (stream, _("<unavailable>")); |
| 309 | } |
| 310 | |
| 311 | void |
| 312 | val_print_invalid_address (struct ui_file *stream) |
| 313 | { |
| 314 | fprintf_filtered (stream, _("<invalid address>")); |
| 315 | } |
| 316 | |
| 317 | /* A generic val_print that is suitable for use by language |
| 318 | implementations of the la_val_print method. This function can |
| 319 | handle most type codes, though not all, notably exception |
| 320 | TYPE_CODE_UNION and TYPE_CODE_STRUCT, which must be implemented by |
| 321 | the caller. |
| 322 | |
| 323 | Most arguments are as to val_print. |
| 324 | |
| 325 | The additional DECORATIONS argument can be used to customize the |
| 326 | output in some small, language-specific ways. */ |
| 327 | |
| 328 | void |
| 329 | generic_val_print (struct type *type, const gdb_byte *valaddr, |
| 330 | int embedded_offset, CORE_ADDR address, |
| 331 | struct ui_file *stream, int recurse, |
| 332 | const struct value *original_value, |
| 333 | const struct value_print_options *options, |
| 334 | const struct generic_val_print_decorations *decorations) |
| 335 | { |
| 336 | struct gdbarch *gdbarch = get_type_arch (type); |
| 337 | enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); |
| 338 | unsigned int i = 0; /* Number of characters printed. */ |
| 339 | unsigned len; |
| 340 | struct type *elttype, *unresolved_elttype; |
| 341 | struct type *unresolved_type = type; |
| 342 | LONGEST val; |
| 343 | CORE_ADDR addr; |
| 344 | |
| 345 | CHECK_TYPEDEF (type); |
| 346 | switch (TYPE_CODE (type)) |
| 347 | { |
| 348 | case TYPE_CODE_ARRAY: |
| 349 | unresolved_elttype = TYPE_TARGET_TYPE (type); |
| 350 | elttype = check_typedef (unresolved_elttype); |
| 351 | if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (unresolved_elttype) > 0) |
| 352 | { |
| 353 | LONGEST low_bound, high_bound; |
| 354 | |
| 355 | if (!get_array_bounds (type, &low_bound, &high_bound)) |
| 356 | error (_("Could not determine the array high bound")); |
| 357 | |
| 358 | if (options->prettyprint_arrays) |
| 359 | { |
| 360 | print_spaces_filtered (2 + 2 * recurse, stream); |
| 361 | } |
| 362 | |
| 363 | fprintf_filtered (stream, "{"); |
| 364 | val_print_array_elements (type, valaddr, embedded_offset, |
| 365 | address, stream, |
| 366 | recurse, original_value, options, 0); |
| 367 | fprintf_filtered (stream, "}"); |
| 368 | break; |
| 369 | } |
| 370 | /* Array of unspecified length: treat like pointer to first |
| 371 | elt. */ |
| 372 | addr = address + embedded_offset; |
| 373 | goto print_unpacked_pointer; |
| 374 | |
| 375 | case TYPE_CODE_MEMBERPTR: |
| 376 | val_print_scalar_formatted (type, valaddr, embedded_offset, |
| 377 | original_value, options, 0, stream); |
| 378 | break; |
| 379 | |
| 380 | case TYPE_CODE_PTR: |
| 381 | if (options->format && options->format != 's') |
| 382 | { |
| 383 | val_print_scalar_formatted (type, valaddr, embedded_offset, |
| 384 | original_value, options, 0, stream); |
| 385 | break; |
| 386 | } |
| 387 | unresolved_elttype = TYPE_TARGET_TYPE (type); |
| 388 | elttype = check_typedef (unresolved_elttype); |
| 389 | { |
| 390 | addr = unpack_pointer (type, valaddr + embedded_offset); |
| 391 | print_unpacked_pointer: |
| 392 | |
| 393 | if (TYPE_CODE (elttype) == TYPE_CODE_FUNC) |
| 394 | { |
| 395 | /* Try to print what function it points to. */ |
| 396 | print_function_pointer_address (options, gdbarch, addr, stream); |
| 397 | return; |
| 398 | } |
| 399 | |
| 400 | if (options->symbol_print) |
| 401 | print_address_demangle (options, gdbarch, addr, stream, demangle); |
| 402 | else if (options->addressprint) |
| 403 | fputs_filtered (paddress (gdbarch, addr), stream); |
| 404 | } |
| 405 | break; |
| 406 | |
| 407 | case TYPE_CODE_REF: |
| 408 | elttype = check_typedef (TYPE_TARGET_TYPE (type)); |
| 409 | if (options->addressprint) |
| 410 | { |
| 411 | CORE_ADDR addr |
| 412 | = extract_typed_address (valaddr + embedded_offset, type); |
| 413 | |
| 414 | fprintf_filtered (stream, "@"); |
| 415 | fputs_filtered (paddress (gdbarch, addr), stream); |
| 416 | if (options->deref_ref) |
| 417 | fputs_filtered (": ", stream); |
| 418 | } |
| 419 | /* De-reference the reference. */ |
| 420 | if (options->deref_ref) |
| 421 | { |
| 422 | if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF) |
| 423 | { |
| 424 | struct value *deref_val; |
| 425 | |
| 426 | deref_val = coerce_ref_if_computed (original_value); |
| 427 | if (deref_val != NULL) |
| 428 | { |
| 429 | /* More complicated computed references are not supported. */ |
| 430 | gdb_assert (embedded_offset == 0); |
| 431 | } |
| 432 | else |
| 433 | deref_val = value_at (TYPE_TARGET_TYPE (type), |
| 434 | unpack_pointer (type, |
| 435 | (valaddr |
| 436 | + embedded_offset))); |
| 437 | |
| 438 | common_val_print (deref_val, stream, recurse, options, |
| 439 | current_language); |
| 440 | } |
| 441 | else |
| 442 | fputs_filtered ("???", stream); |
| 443 | } |
| 444 | break; |
| 445 | |
| 446 | case TYPE_CODE_ENUM: |
| 447 | if (options->format) |
| 448 | { |
| 449 | val_print_scalar_formatted (type, valaddr, embedded_offset, |
| 450 | original_value, options, 0, stream); |
| 451 | break; |
| 452 | } |
| 453 | len = TYPE_NFIELDS (type); |
| 454 | val = unpack_long (type, valaddr + embedded_offset); |
| 455 | for (i = 0; i < len; i++) |
| 456 | { |
| 457 | QUIT; |
| 458 | if (val == TYPE_FIELD_ENUMVAL (type, i)) |
| 459 | { |
| 460 | break; |
| 461 | } |
| 462 | } |
| 463 | if (i < len) |
| 464 | { |
| 465 | fputs_filtered (TYPE_FIELD_NAME (type, i), stream); |
| 466 | } |
| 467 | else if (TYPE_FLAG_ENUM (type)) |
| 468 | { |
| 469 | int first = 1; |
| 470 | |
| 471 | /* We have a "flag" enum, so we try to decompose it into |
| 472 | pieces as appropriate. A flag enum has disjoint |
| 473 | constants by definition. */ |
| 474 | fputs_filtered ("(", stream); |
| 475 | for (i = 0; i < len; ++i) |
| 476 | { |
| 477 | QUIT; |
| 478 | |
| 479 | if ((val & TYPE_FIELD_ENUMVAL (type, i)) != 0) |
| 480 | { |
| 481 | if (!first) |
| 482 | fputs_filtered (" | ", stream); |
| 483 | first = 0; |
| 484 | |
| 485 | val &= ~TYPE_FIELD_ENUMVAL (type, i); |
| 486 | fputs_filtered (TYPE_FIELD_NAME (type, i), stream); |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | if (first || val != 0) |
| 491 | { |
| 492 | if (!first) |
| 493 | fputs_filtered (" | ", stream); |
| 494 | fputs_filtered ("unknown: ", stream); |
| 495 | print_longest (stream, 'd', 0, val); |
| 496 | } |
| 497 | |
| 498 | fputs_filtered (")", stream); |
| 499 | } |
| 500 | else |
| 501 | print_longest (stream, 'd', 0, val); |
| 502 | break; |
| 503 | |
| 504 | case TYPE_CODE_FLAGS: |
| 505 | if (options->format) |
| 506 | val_print_scalar_formatted (type, valaddr, embedded_offset, |
| 507 | original_value, options, 0, stream); |
| 508 | else |
| 509 | val_print_type_code_flags (type, valaddr + embedded_offset, |
| 510 | stream); |
| 511 | break; |
| 512 | |
| 513 | case TYPE_CODE_FUNC: |
| 514 | case TYPE_CODE_METHOD: |
| 515 | if (options->format) |
| 516 | { |
| 517 | val_print_scalar_formatted (type, valaddr, embedded_offset, |
| 518 | original_value, options, 0, stream); |
| 519 | break; |
| 520 | } |
| 521 | /* FIXME, we should consider, at least for ANSI C language, |
| 522 | eliminating the distinction made between FUNCs and POINTERs |
| 523 | to FUNCs. */ |
| 524 | fprintf_filtered (stream, "{"); |
| 525 | type_print (type, "", stream, -1); |
| 526 | fprintf_filtered (stream, "} "); |
| 527 | /* Try to print what function it points to, and its address. */ |
| 528 | print_address_demangle (options, gdbarch, address, stream, demangle); |
| 529 | break; |
| 530 | |
| 531 | case TYPE_CODE_BOOL: |
| 532 | if (options->format || options->output_format) |
| 533 | { |
| 534 | struct value_print_options opts = *options; |
| 535 | opts.format = (options->format ? options->format |
| 536 | : options->output_format); |
| 537 | val_print_scalar_formatted (type, valaddr, embedded_offset, |
| 538 | original_value, &opts, 0, stream); |
| 539 | } |
| 540 | else |
| 541 | { |
| 542 | val = unpack_long (type, valaddr + embedded_offset); |
| 543 | if (val == 0) |
| 544 | fputs_filtered (decorations->false_name, stream); |
| 545 | else if (val == 1) |
| 546 | fputs_filtered (decorations->true_name, stream); |
| 547 | else |
| 548 | print_longest (stream, 'd', 0, val); |
| 549 | } |
| 550 | break; |
| 551 | |
| 552 | case TYPE_CODE_RANGE: |
| 553 | /* FIXME: create_range_type does not set the unsigned bit in a |
| 554 | range type (I think it probably should copy it from the |
| 555 | target type), so we won't print values which are too large to |
| 556 | fit in a signed integer correctly. */ |
| 557 | /* FIXME: Doesn't handle ranges of enums correctly. (Can't just |
| 558 | print with the target type, though, because the size of our |
| 559 | type and the target type might differ). */ |
| 560 | |
| 561 | /* FALLTHROUGH */ |
| 562 | |
| 563 | case TYPE_CODE_INT: |
| 564 | if (options->format || options->output_format) |
| 565 | { |
| 566 | struct value_print_options opts = *options; |
| 567 | |
| 568 | opts.format = (options->format ? options->format |
| 569 | : options->output_format); |
| 570 | val_print_scalar_formatted (type, valaddr, embedded_offset, |
| 571 | original_value, &opts, 0, stream); |
| 572 | } |
| 573 | else |
| 574 | val_print_type_code_int (type, valaddr + embedded_offset, stream); |
| 575 | break; |
| 576 | |
| 577 | case TYPE_CODE_CHAR: |
| 578 | if (options->format || options->output_format) |
| 579 | { |
| 580 | struct value_print_options opts = *options; |
| 581 | |
| 582 | opts.format = (options->format ? options->format |
| 583 | : options->output_format); |
| 584 | val_print_scalar_formatted (type, valaddr, embedded_offset, |
| 585 | original_value, &opts, 0, stream); |
| 586 | } |
| 587 | else |
| 588 | { |
| 589 | val = unpack_long (type, valaddr + embedded_offset); |
| 590 | if (TYPE_UNSIGNED (type)) |
| 591 | fprintf_filtered (stream, "%u", (unsigned int) val); |
| 592 | else |
| 593 | fprintf_filtered (stream, "%d", (int) val); |
| 594 | fputs_filtered (" ", stream); |
| 595 | LA_PRINT_CHAR (val, unresolved_type, stream); |
| 596 | } |
| 597 | break; |
| 598 | |
| 599 | case TYPE_CODE_FLT: |
| 600 | if (options->format) |
| 601 | { |
| 602 | val_print_scalar_formatted (type, valaddr, embedded_offset, |
| 603 | original_value, options, 0, stream); |
| 604 | } |
| 605 | else |
| 606 | { |
| 607 | print_floating (valaddr + embedded_offset, type, stream); |
| 608 | } |
| 609 | break; |
| 610 | |
| 611 | case TYPE_CODE_DECFLOAT: |
| 612 | if (options->format) |
| 613 | val_print_scalar_formatted (type, valaddr, embedded_offset, |
| 614 | original_value, options, 0, stream); |
| 615 | else |
| 616 | print_decimal_floating (valaddr + embedded_offset, |
| 617 | type, stream); |
| 618 | break; |
| 619 | |
| 620 | case TYPE_CODE_VOID: |
| 621 | fputs_filtered (decorations->void_name, stream); |
| 622 | break; |
| 623 | |
| 624 | case TYPE_CODE_ERROR: |
| 625 | fprintf_filtered (stream, "%s", TYPE_ERROR_NAME (type)); |
| 626 | break; |
| 627 | |
| 628 | case TYPE_CODE_UNDEF: |
| 629 | /* This happens (without TYPE_FLAG_STUB set) on systems which |
| 630 | don't use dbx xrefs (NO_DBX_XREFS in gcc) if a file has a |
| 631 | "struct foo *bar" and no complete type for struct foo in that |
| 632 | file. */ |
| 633 | fprintf_filtered (stream, _("<incomplete type>")); |
| 634 | break; |
| 635 | |
| 636 | case TYPE_CODE_COMPLEX: |
| 637 | fprintf_filtered (stream, "%s", decorations->complex_prefix); |
| 638 | if (options->format) |
| 639 | val_print_scalar_formatted (TYPE_TARGET_TYPE (type), |
| 640 | valaddr, embedded_offset, |
| 641 | original_value, options, 0, stream); |
| 642 | else |
| 643 | print_floating (valaddr + embedded_offset, |
| 644 | TYPE_TARGET_TYPE (type), |
| 645 | stream); |
| 646 | fprintf_filtered (stream, "%s", decorations->complex_infix); |
| 647 | if (options->format) |
| 648 | val_print_scalar_formatted (TYPE_TARGET_TYPE (type), |
| 649 | valaddr, |
| 650 | embedded_offset |
| 651 | + TYPE_LENGTH (TYPE_TARGET_TYPE (type)), |
| 652 | original_value, |
| 653 | options, 0, stream); |
| 654 | else |
| 655 | print_floating (valaddr + embedded_offset |
| 656 | + TYPE_LENGTH (TYPE_TARGET_TYPE (type)), |
| 657 | TYPE_TARGET_TYPE (type), |
| 658 | stream); |
| 659 | fprintf_filtered (stream, "%s", decorations->complex_suffix); |
| 660 | break; |
| 661 | |
| 662 | case TYPE_CODE_UNION: |
| 663 | case TYPE_CODE_STRUCT: |
| 664 | case TYPE_CODE_METHODPTR: |
| 665 | default: |
| 666 | error (_("Unhandled type code %d in symbol table."), |
| 667 | TYPE_CODE (type)); |
| 668 | } |
| 669 | gdb_flush (stream); |
| 670 | } |
| 671 | |
| 672 | /* Print using the given LANGUAGE the data of type TYPE located at |
| 673 | VALADDR + EMBEDDED_OFFSET (within GDB), which came from the |
| 674 | inferior at address ADDRESS + EMBEDDED_OFFSET, onto stdio stream |
| 675 | STREAM according to OPTIONS. VAL is the whole object that came |
| 676 | from ADDRESS. VALADDR must point to the head of VAL's contents |
| 677 | buffer. |
| 678 | |
| 679 | The language printers will pass down an adjusted EMBEDDED_OFFSET to |
| 680 | further helper subroutines as subfields of TYPE are printed. In |
| 681 | such cases, VALADDR is passed down unadjusted, as well as VAL, so |
| 682 | that VAL can be queried for metadata about the contents data being |
| 683 | printed, using EMBEDDED_OFFSET as an offset into VAL's contents |
| 684 | buffer. For example: "has this field been optimized out", or "I'm |
| 685 | printing an object while inspecting a traceframe; has this |
| 686 | particular piece of data been collected?". |
| 687 | |
| 688 | RECURSE indicates the amount of indentation to supply before |
| 689 | continuation lines; this amount is roughly twice the value of |
| 690 | RECURSE. */ |
| 691 | |
| 692 | void |
| 693 | val_print (struct type *type, const gdb_byte *valaddr, int embedded_offset, |
| 694 | CORE_ADDR address, struct ui_file *stream, int recurse, |
| 695 | const struct value *val, |
| 696 | const struct value_print_options *options, |
| 697 | const struct language_defn *language) |
| 698 | { |
| 699 | volatile struct gdb_exception except; |
| 700 | int ret = 0; |
| 701 | struct value_print_options local_opts = *options; |
| 702 | struct type *real_type = check_typedef (type); |
| 703 | |
| 704 | if (local_opts.pretty == Val_pretty_default) |
| 705 | local_opts.pretty = (local_opts.prettyprint_structs |
| 706 | ? Val_prettyprint : Val_no_prettyprint); |
| 707 | |
| 708 | QUIT; |
| 709 | |
| 710 | /* Ensure that the type is complete and not just a stub. If the type is |
| 711 | only a stub and we can't find and substitute its complete type, then |
| 712 | print appropriate string and return. */ |
| 713 | |
| 714 | if (TYPE_STUB (real_type)) |
| 715 | { |
| 716 | fprintf_filtered (stream, _("<incomplete type>")); |
| 717 | gdb_flush (stream); |
| 718 | return; |
| 719 | } |
| 720 | |
| 721 | if (!valprint_check_validity (stream, real_type, embedded_offset, val)) |
| 722 | return; |
| 723 | |
| 724 | if (!options->raw) |
| 725 | { |
| 726 | ret = apply_val_pretty_printer (type, valaddr, embedded_offset, |
| 727 | address, stream, recurse, |
| 728 | val, options, language); |
| 729 | if (ret) |
| 730 | return; |
| 731 | } |
| 732 | |
| 733 | /* Handle summary mode. If the value is a scalar, print it; |
| 734 | otherwise, print an ellipsis. */ |
| 735 | if (options->summary && !scalar_type_p (type)) |
| 736 | { |
| 737 | fprintf_filtered (stream, "..."); |
| 738 | return; |
| 739 | } |
| 740 | |
| 741 | TRY_CATCH (except, RETURN_MASK_ERROR) |
| 742 | { |
| 743 | language->la_val_print (type, valaddr, embedded_offset, address, |
| 744 | stream, recurse, val, |
| 745 | &local_opts); |
| 746 | } |
| 747 | if (except.reason < 0) |
| 748 | fprintf_filtered (stream, _("<error reading variable>")); |
| 749 | } |
| 750 | |
| 751 | /* Check whether the value VAL is printable. Return 1 if it is; |
| 752 | return 0 and print an appropriate error message to STREAM according to |
| 753 | OPTIONS if it is not. */ |
| 754 | |
| 755 | static int |
| 756 | value_check_printable (struct value *val, struct ui_file *stream, |
| 757 | const struct value_print_options *options) |
| 758 | { |
| 759 | if (val == 0) |
| 760 | { |
| 761 | fprintf_filtered (stream, _("<address of value unknown>")); |
| 762 | return 0; |
| 763 | } |
| 764 | |
| 765 | if (value_entirely_optimized_out (val)) |
| 766 | { |
| 767 | if (options->summary && !scalar_type_p (value_type (val))) |
| 768 | fprintf_filtered (stream, "..."); |
| 769 | else |
| 770 | val_print_optimized_out (stream); |
| 771 | return 0; |
| 772 | } |
| 773 | |
| 774 | if (TYPE_CODE (value_type (val)) == TYPE_CODE_INTERNAL_FUNCTION) |
| 775 | { |
| 776 | fprintf_filtered (stream, _("<internal function %s>"), |
| 777 | value_internal_function_name (val)); |
| 778 | return 0; |
| 779 | } |
| 780 | |
| 781 | return 1; |
| 782 | } |
| 783 | |
| 784 | /* Print using the given LANGUAGE the value VAL onto stream STREAM according |
| 785 | to OPTIONS. |
| 786 | |
| 787 | This is a preferable interface to val_print, above, because it uses |
| 788 | GDB's value mechanism. */ |
| 789 | |
| 790 | void |
| 791 | common_val_print (struct value *val, struct ui_file *stream, int recurse, |
| 792 | const struct value_print_options *options, |
| 793 | const struct language_defn *language) |
| 794 | { |
| 795 | if (!value_check_printable (val, stream, options)) |
| 796 | return; |
| 797 | |
| 798 | if (language->la_language == language_ada) |
| 799 | /* The value might have a dynamic type, which would cause trouble |
| 800 | below when trying to extract the value contents (since the value |
| 801 | size is determined from the type size which is unknown). So |
| 802 | get a fixed representation of our value. */ |
| 803 | val = ada_to_fixed_value (val); |
| 804 | |
| 805 | val_print (value_type (val), value_contents_for_printing (val), |
| 806 | value_embedded_offset (val), value_address (val), |
| 807 | stream, recurse, |
| 808 | val, options, language); |
| 809 | } |
| 810 | |
| 811 | /* Print on stream STREAM the value VAL according to OPTIONS. The value |
| 812 | is printed using the current_language syntax. */ |
| 813 | |
| 814 | void |
| 815 | value_print (struct value *val, struct ui_file *stream, |
| 816 | const struct value_print_options *options) |
| 817 | { |
| 818 | if (!value_check_printable (val, stream, options)) |
| 819 | return; |
| 820 | |
| 821 | if (!options->raw) |
| 822 | { |
| 823 | int r = apply_val_pretty_printer (value_type (val), |
| 824 | value_contents_for_printing (val), |
| 825 | value_embedded_offset (val), |
| 826 | value_address (val), |
| 827 | stream, 0, |
| 828 | val, options, current_language); |
| 829 | |
| 830 | if (r) |
| 831 | return; |
| 832 | } |
| 833 | |
| 834 | LA_VALUE_PRINT (val, stream, options); |
| 835 | } |
| 836 | |
| 837 | /* Called by various <lang>_val_print routines to print |
| 838 | TYPE_CODE_INT's. TYPE is the type. VALADDR is the address of the |
| 839 | value. STREAM is where to print the value. */ |
| 840 | |
| 841 | void |
| 842 | val_print_type_code_int (struct type *type, const gdb_byte *valaddr, |
| 843 | struct ui_file *stream) |
| 844 | { |
| 845 | enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type)); |
| 846 | |
| 847 | if (TYPE_LENGTH (type) > sizeof (LONGEST)) |
| 848 | { |
| 849 | LONGEST val; |
| 850 | |
| 851 | if (TYPE_UNSIGNED (type) |
| 852 | && extract_long_unsigned_integer (valaddr, TYPE_LENGTH (type), |
| 853 | byte_order, &val)) |
| 854 | { |
| 855 | print_longest (stream, 'u', 0, val); |
| 856 | } |
| 857 | else |
| 858 | { |
| 859 | /* Signed, or we couldn't turn an unsigned value into a |
| 860 | LONGEST. For signed values, one could assume two's |
| 861 | complement (a reasonable assumption, I think) and do |
| 862 | better than this. */ |
| 863 | print_hex_chars (stream, (unsigned char *) valaddr, |
| 864 | TYPE_LENGTH (type), byte_order); |
| 865 | } |
| 866 | } |
| 867 | else |
| 868 | { |
| 869 | print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0, |
| 870 | unpack_long (type, valaddr)); |
| 871 | } |
| 872 | } |
| 873 | |
| 874 | void |
| 875 | val_print_type_code_flags (struct type *type, const gdb_byte *valaddr, |
| 876 | struct ui_file *stream) |
| 877 | { |
| 878 | ULONGEST val = unpack_long (type, valaddr); |
| 879 | int bitpos, nfields = TYPE_NFIELDS (type); |
| 880 | |
| 881 | fputs_filtered ("[ ", stream); |
| 882 | for (bitpos = 0; bitpos < nfields; bitpos++) |
| 883 | { |
| 884 | if (TYPE_FIELD_BITPOS (type, bitpos) != -1 |
| 885 | && (val & ((ULONGEST)1 << bitpos))) |
| 886 | { |
| 887 | if (TYPE_FIELD_NAME (type, bitpos)) |
| 888 | fprintf_filtered (stream, "%s ", TYPE_FIELD_NAME (type, bitpos)); |
| 889 | else |
| 890 | fprintf_filtered (stream, "#%d ", bitpos); |
| 891 | } |
| 892 | } |
| 893 | fputs_filtered ("]", stream); |
| 894 | } |
| 895 | |
| 896 | /* Print a scalar of data of type TYPE, pointed to in GDB by VALADDR, |
| 897 | according to OPTIONS and SIZE on STREAM. Format i is not supported |
| 898 | at this level. |
| 899 | |
| 900 | This is how the elements of an array or structure are printed |
| 901 | with a format. */ |
| 902 | |
| 903 | void |
| 904 | val_print_scalar_formatted (struct type *type, |
| 905 | const gdb_byte *valaddr, int embedded_offset, |
| 906 | const struct value *val, |
| 907 | const struct value_print_options *options, |
| 908 | int size, |
| 909 | struct ui_file *stream) |
| 910 | { |
| 911 | gdb_assert (val != NULL); |
| 912 | gdb_assert (valaddr == value_contents_for_printing_const (val)); |
| 913 | |
| 914 | /* If we get here with a string format, try again without it. Go |
| 915 | all the way back to the language printers, which may call us |
| 916 | again. */ |
| 917 | if (options->format == 's') |
| 918 | { |
| 919 | struct value_print_options opts = *options; |
| 920 | opts.format = 0; |
| 921 | opts.deref_ref = 0; |
| 922 | val_print (type, valaddr, embedded_offset, 0, stream, 0, val, &opts, |
| 923 | current_language); |
| 924 | return; |
| 925 | } |
| 926 | |
| 927 | /* A scalar object that does not have all bits available can't be |
| 928 | printed, because all bits contribute to its representation. */ |
| 929 | if (!value_bits_valid (val, TARGET_CHAR_BIT * embedded_offset, |
| 930 | TARGET_CHAR_BIT * TYPE_LENGTH (type))) |
| 931 | val_print_optimized_out (stream); |
| 932 | else if (!value_bytes_available (val, embedded_offset, TYPE_LENGTH (type))) |
| 933 | val_print_unavailable (stream); |
| 934 | else |
| 935 | print_scalar_formatted (valaddr + embedded_offset, type, |
| 936 | options, size, stream); |
| 937 | } |
| 938 | |
| 939 | /* Print a number according to FORMAT which is one of d,u,x,o,b,h,w,g. |
| 940 | The raison d'etre of this function is to consolidate printing of |
| 941 | LONG_LONG's into this one function. The format chars b,h,w,g are |
| 942 | from print_scalar_formatted(). Numbers are printed using C |
| 943 | format. |
| 944 | |
| 945 | USE_C_FORMAT means to use C format in all cases. Without it, |
| 946 | 'o' and 'x' format do not include the standard C radix prefix |
| 947 | (leading 0 or 0x). |
| 948 | |
| 949 | Hilfinger/2004-09-09: USE_C_FORMAT was originally called USE_LOCAL |
| 950 | and was intended to request formating according to the current |
| 951 | language and would be used for most integers that GDB prints. The |
| 952 | exceptional cases were things like protocols where the format of |
| 953 | the integer is a protocol thing, not a user-visible thing). The |
| 954 | parameter remains to preserve the information of what things might |
| 955 | be printed with language-specific format, should we ever resurrect |
| 956 | that capability. */ |
| 957 | |
| 958 | void |
| 959 | print_longest (struct ui_file *stream, int format, int use_c_format, |
| 960 | LONGEST val_long) |
| 961 | { |
| 962 | const char *val; |
| 963 | |
| 964 | switch (format) |
| 965 | { |
| 966 | case 'd': |
| 967 | val = int_string (val_long, 10, 1, 0, 1); break; |
| 968 | case 'u': |
| 969 | val = int_string (val_long, 10, 0, 0, 1); break; |
| 970 | case 'x': |
| 971 | val = int_string (val_long, 16, 0, 0, use_c_format); break; |
| 972 | case 'b': |
| 973 | val = int_string (val_long, 16, 0, 2, 1); break; |
| 974 | case 'h': |
| 975 | val = int_string (val_long, 16, 0, 4, 1); break; |
| 976 | case 'w': |
| 977 | val = int_string (val_long, 16, 0, 8, 1); break; |
| 978 | case 'g': |
| 979 | val = int_string (val_long, 16, 0, 16, 1); break; |
| 980 | break; |
| 981 | case 'o': |
| 982 | val = int_string (val_long, 8, 0, 0, use_c_format); break; |
| 983 | default: |
| 984 | internal_error (__FILE__, __LINE__, |
| 985 | _("failed internal consistency check")); |
| 986 | } |
| 987 | fputs_filtered (val, stream); |
| 988 | } |
| 989 | |
| 990 | /* This used to be a macro, but I don't think it is called often enough |
| 991 | to merit such treatment. */ |
| 992 | /* Convert a LONGEST to an int. This is used in contexts (e.g. number of |
| 993 | arguments to a function, number in a value history, register number, etc.) |
| 994 | where the value must not be larger than can fit in an int. */ |
| 995 | |
| 996 | int |
| 997 | longest_to_int (LONGEST arg) |
| 998 | { |
| 999 | /* Let the compiler do the work. */ |
| 1000 | int rtnval = (int) arg; |
| 1001 | |
| 1002 | /* Check for overflows or underflows. */ |
| 1003 | if (sizeof (LONGEST) > sizeof (int)) |
| 1004 | { |
| 1005 | if (rtnval != arg) |
| 1006 | { |
| 1007 | error (_("Value out of range.")); |
| 1008 | } |
| 1009 | } |
| 1010 | return (rtnval); |
| 1011 | } |
| 1012 | |
| 1013 | /* Print a floating point value of type TYPE (not always a |
| 1014 | TYPE_CODE_FLT), pointed to in GDB by VALADDR, on STREAM. */ |
| 1015 | |
| 1016 | void |
| 1017 | print_floating (const gdb_byte *valaddr, struct type *type, |
| 1018 | struct ui_file *stream) |
| 1019 | { |
| 1020 | DOUBLEST doub; |
| 1021 | int inv; |
| 1022 | const struct floatformat *fmt = NULL; |
| 1023 | unsigned len = TYPE_LENGTH (type); |
| 1024 | enum float_kind kind; |
| 1025 | |
| 1026 | /* If it is a floating-point, check for obvious problems. */ |
| 1027 | if (TYPE_CODE (type) == TYPE_CODE_FLT) |
| 1028 | fmt = floatformat_from_type (type); |
| 1029 | if (fmt != NULL) |
| 1030 | { |
| 1031 | kind = floatformat_classify (fmt, valaddr); |
| 1032 | if (kind == float_nan) |
| 1033 | { |
| 1034 | if (floatformat_is_negative (fmt, valaddr)) |
| 1035 | fprintf_filtered (stream, "-"); |
| 1036 | fprintf_filtered (stream, "nan("); |
| 1037 | fputs_filtered ("0x", stream); |
| 1038 | fputs_filtered (floatformat_mantissa (fmt, valaddr), stream); |
| 1039 | fprintf_filtered (stream, ")"); |
| 1040 | return; |
| 1041 | } |
| 1042 | else if (kind == float_infinite) |
| 1043 | { |
| 1044 | if (floatformat_is_negative (fmt, valaddr)) |
| 1045 | fputs_filtered ("-", stream); |
| 1046 | fputs_filtered ("inf", stream); |
| 1047 | return; |
| 1048 | } |
| 1049 | } |
| 1050 | |
| 1051 | /* NOTE: cagney/2002-01-15: The TYPE passed into print_floating() |
| 1052 | isn't necessarily a TYPE_CODE_FLT. Consequently, unpack_double |
| 1053 | needs to be used as that takes care of any necessary type |
| 1054 | conversions. Such conversions are of course direct to DOUBLEST |
| 1055 | and disregard any possible target floating point limitations. |
| 1056 | For instance, a u64 would be converted and displayed exactly on a |
| 1057 | host with 80 bit DOUBLEST but with loss of information on a host |
| 1058 | with 64 bit DOUBLEST. */ |
| 1059 | |
| 1060 | doub = unpack_double (type, valaddr, &inv); |
| 1061 | if (inv) |
| 1062 | { |
| 1063 | fprintf_filtered (stream, "<invalid float value>"); |
| 1064 | return; |
| 1065 | } |
| 1066 | |
| 1067 | /* FIXME: kettenis/2001-01-20: The following code makes too much |
| 1068 | assumptions about the host and target floating point format. */ |
| 1069 | |
| 1070 | /* NOTE: cagney/2002-02-03: Since the TYPE of what was passed in may |
| 1071 | not necessarily be a TYPE_CODE_FLT, the below ignores that and |
| 1072 | instead uses the type's length to determine the precision of the |
| 1073 | floating-point value being printed. */ |
| 1074 | |
| 1075 | if (len < sizeof (double)) |
| 1076 | fprintf_filtered (stream, "%.9g", (double) doub); |
| 1077 | else if (len == sizeof (double)) |
| 1078 | fprintf_filtered (stream, "%.17g", (double) doub); |
| 1079 | else |
| 1080 | #ifdef PRINTF_HAS_LONG_DOUBLE |
| 1081 | fprintf_filtered (stream, "%.35Lg", doub); |
| 1082 | #else |
| 1083 | /* This at least wins with values that are representable as |
| 1084 | doubles. */ |
| 1085 | fprintf_filtered (stream, "%.17g", (double) doub); |
| 1086 | #endif |
| 1087 | } |
| 1088 | |
| 1089 | void |
| 1090 | print_decimal_floating (const gdb_byte *valaddr, struct type *type, |
| 1091 | struct ui_file *stream) |
| 1092 | { |
| 1093 | enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type)); |
| 1094 | char decstr[MAX_DECIMAL_STRING]; |
| 1095 | unsigned len = TYPE_LENGTH (type); |
| 1096 | |
| 1097 | decimal_to_string (valaddr, len, byte_order, decstr); |
| 1098 | fputs_filtered (decstr, stream); |
| 1099 | return; |
| 1100 | } |
| 1101 | |
| 1102 | void |
| 1103 | print_binary_chars (struct ui_file *stream, const gdb_byte *valaddr, |
| 1104 | unsigned len, enum bfd_endian byte_order) |
| 1105 | { |
| 1106 | |
| 1107 | #define BITS_IN_BYTES 8 |
| 1108 | |
| 1109 | const gdb_byte *p; |
| 1110 | unsigned int i; |
| 1111 | int b; |
| 1112 | |
| 1113 | /* Declared "int" so it will be signed. |
| 1114 | This ensures that right shift will shift in zeros. */ |
| 1115 | |
| 1116 | const int mask = 0x080; |
| 1117 | |
| 1118 | /* FIXME: We should be not printing leading zeroes in most cases. */ |
| 1119 | |
| 1120 | if (byte_order == BFD_ENDIAN_BIG) |
| 1121 | { |
| 1122 | for (p = valaddr; |
| 1123 | p < valaddr + len; |
| 1124 | p++) |
| 1125 | { |
| 1126 | /* Every byte has 8 binary characters; peel off |
| 1127 | and print from the MSB end. */ |
| 1128 | |
| 1129 | for (i = 0; i < (BITS_IN_BYTES * sizeof (*p)); i++) |
| 1130 | { |
| 1131 | if (*p & (mask >> i)) |
| 1132 | b = 1; |
| 1133 | else |
| 1134 | b = 0; |
| 1135 | |
| 1136 | fprintf_filtered (stream, "%1d", b); |
| 1137 | } |
| 1138 | } |
| 1139 | } |
| 1140 | else |
| 1141 | { |
| 1142 | for (p = valaddr + len - 1; |
| 1143 | p >= valaddr; |
| 1144 | p--) |
| 1145 | { |
| 1146 | for (i = 0; i < (BITS_IN_BYTES * sizeof (*p)); i++) |
| 1147 | { |
| 1148 | if (*p & (mask >> i)) |
| 1149 | b = 1; |
| 1150 | else |
| 1151 | b = 0; |
| 1152 | |
| 1153 | fprintf_filtered (stream, "%1d", b); |
| 1154 | } |
| 1155 | } |
| 1156 | } |
| 1157 | } |
| 1158 | |
| 1159 | /* VALADDR points to an integer of LEN bytes. |
| 1160 | Print it in octal on stream or format it in buf. */ |
| 1161 | |
| 1162 | void |
| 1163 | print_octal_chars (struct ui_file *stream, const gdb_byte *valaddr, |
| 1164 | unsigned len, enum bfd_endian byte_order) |
| 1165 | { |
| 1166 | const gdb_byte *p; |
| 1167 | unsigned char octa1, octa2, octa3, carry; |
| 1168 | int cycle; |
| 1169 | |
| 1170 | /* FIXME: We should be not printing leading zeroes in most cases. */ |
| 1171 | |
| 1172 | |
| 1173 | /* Octal is 3 bits, which doesn't fit. Yuk. So we have to track |
| 1174 | * the extra bits, which cycle every three bytes: |
| 1175 | * |
| 1176 | * Byte side: 0 1 2 3 |
| 1177 | * | | | | |
| 1178 | * bit number 123 456 78 | 9 012 345 6 | 78 901 234 | 567 890 12 | |
| 1179 | * |
| 1180 | * Octal side: 0 1 carry 3 4 carry ... |
| 1181 | * |
| 1182 | * Cycle number: 0 1 2 |
| 1183 | * |
| 1184 | * But of course we are printing from the high side, so we have to |
| 1185 | * figure out where in the cycle we are so that we end up with no |
| 1186 | * left over bits at the end. |
| 1187 | */ |
| 1188 | #define BITS_IN_OCTAL 3 |
| 1189 | #define HIGH_ZERO 0340 |
| 1190 | #define LOW_ZERO 0016 |
| 1191 | #define CARRY_ZERO 0003 |
| 1192 | #define HIGH_ONE 0200 |
| 1193 | #define MID_ONE 0160 |
| 1194 | #define LOW_ONE 0016 |
| 1195 | #define CARRY_ONE 0001 |
| 1196 | #define HIGH_TWO 0300 |
| 1197 | #define MID_TWO 0070 |
| 1198 | #define LOW_TWO 0007 |
| 1199 | |
| 1200 | /* For 32 we start in cycle 2, with two bits and one bit carry; |
| 1201 | for 64 in cycle in cycle 1, with one bit and a two bit carry. */ |
| 1202 | |
| 1203 | cycle = (len * BITS_IN_BYTES) % BITS_IN_OCTAL; |
| 1204 | carry = 0; |
| 1205 | |
| 1206 | fputs_filtered ("0", stream); |
| 1207 | if (byte_order == BFD_ENDIAN_BIG) |
| 1208 | { |
| 1209 | for (p = valaddr; |
| 1210 | p < valaddr + len; |
| 1211 | p++) |
| 1212 | { |
| 1213 | switch (cycle) |
| 1214 | { |
| 1215 | case 0: |
| 1216 | /* No carry in, carry out two bits. */ |
| 1217 | |
| 1218 | octa1 = (HIGH_ZERO & *p) >> 5; |
| 1219 | octa2 = (LOW_ZERO & *p) >> 2; |
| 1220 | carry = (CARRY_ZERO & *p); |
| 1221 | fprintf_filtered (stream, "%o", octa1); |
| 1222 | fprintf_filtered (stream, "%o", octa2); |
| 1223 | break; |
| 1224 | |
| 1225 | case 1: |
| 1226 | /* Carry in two bits, carry out one bit. */ |
| 1227 | |
| 1228 | octa1 = (carry << 1) | ((HIGH_ONE & *p) >> 7); |
| 1229 | octa2 = (MID_ONE & *p) >> 4; |
| 1230 | octa3 = (LOW_ONE & *p) >> 1; |
| 1231 | carry = (CARRY_ONE & *p); |
| 1232 | fprintf_filtered (stream, "%o", octa1); |
| 1233 | fprintf_filtered (stream, "%o", octa2); |
| 1234 | fprintf_filtered (stream, "%o", octa3); |
| 1235 | break; |
| 1236 | |
| 1237 | case 2: |
| 1238 | /* Carry in one bit, no carry out. */ |
| 1239 | |
| 1240 | octa1 = (carry << 2) | ((HIGH_TWO & *p) >> 6); |
| 1241 | octa2 = (MID_TWO & *p) >> 3; |
| 1242 | octa3 = (LOW_TWO & *p); |
| 1243 | carry = 0; |
| 1244 | fprintf_filtered (stream, "%o", octa1); |
| 1245 | fprintf_filtered (stream, "%o", octa2); |
| 1246 | fprintf_filtered (stream, "%o", octa3); |
| 1247 | break; |
| 1248 | |
| 1249 | default: |
| 1250 | error (_("Internal error in octal conversion;")); |
| 1251 | } |
| 1252 | |
| 1253 | cycle++; |
| 1254 | cycle = cycle % BITS_IN_OCTAL; |
| 1255 | } |
| 1256 | } |
| 1257 | else |
| 1258 | { |
| 1259 | for (p = valaddr + len - 1; |
| 1260 | p >= valaddr; |
| 1261 | p--) |
| 1262 | { |
| 1263 | switch (cycle) |
| 1264 | { |
| 1265 | case 0: |
| 1266 | /* Carry out, no carry in */ |
| 1267 | |
| 1268 | octa1 = (HIGH_ZERO & *p) >> 5; |
| 1269 | octa2 = (LOW_ZERO & *p) >> 2; |
| 1270 | carry = (CARRY_ZERO & *p); |
| 1271 | fprintf_filtered (stream, "%o", octa1); |
| 1272 | fprintf_filtered (stream, "%o", octa2); |
| 1273 | break; |
| 1274 | |
| 1275 | case 1: |
| 1276 | /* Carry in, carry out */ |
| 1277 | |
| 1278 | octa1 = (carry << 1) | ((HIGH_ONE & *p) >> 7); |
| 1279 | octa2 = (MID_ONE & *p) >> 4; |
| 1280 | octa3 = (LOW_ONE & *p) >> 1; |
| 1281 | carry = (CARRY_ONE & *p); |
| 1282 | fprintf_filtered (stream, "%o", octa1); |
| 1283 | fprintf_filtered (stream, "%o", octa2); |
| 1284 | fprintf_filtered (stream, "%o", octa3); |
| 1285 | break; |
| 1286 | |
| 1287 | case 2: |
| 1288 | /* Carry in, no carry out */ |
| 1289 | |
| 1290 | octa1 = (carry << 2) | ((HIGH_TWO & *p) >> 6); |
| 1291 | octa2 = (MID_TWO & *p) >> 3; |
| 1292 | octa3 = (LOW_TWO & *p); |
| 1293 | carry = 0; |
| 1294 | fprintf_filtered (stream, "%o", octa1); |
| 1295 | fprintf_filtered (stream, "%o", octa2); |
| 1296 | fprintf_filtered (stream, "%o", octa3); |
| 1297 | break; |
| 1298 | |
| 1299 | default: |
| 1300 | error (_("Internal error in octal conversion;")); |
| 1301 | } |
| 1302 | |
| 1303 | cycle++; |
| 1304 | cycle = cycle % BITS_IN_OCTAL; |
| 1305 | } |
| 1306 | } |
| 1307 | |
| 1308 | } |
| 1309 | |
| 1310 | /* VALADDR points to an integer of LEN bytes. |
| 1311 | Print it in decimal on stream or format it in buf. */ |
| 1312 | |
| 1313 | void |
| 1314 | print_decimal_chars (struct ui_file *stream, const gdb_byte *valaddr, |
| 1315 | unsigned len, enum bfd_endian byte_order) |
| 1316 | { |
| 1317 | #define TEN 10 |
| 1318 | #define CARRY_OUT( x ) ((x) / TEN) /* extend char to int */ |
| 1319 | #define CARRY_LEFT( x ) ((x) % TEN) |
| 1320 | #define SHIFT( x ) ((x) << 4) |
| 1321 | #define LOW_NIBBLE( x ) ( (x) & 0x00F) |
| 1322 | #define HIGH_NIBBLE( x ) (((x) & 0x0F0) >> 4) |
| 1323 | |
| 1324 | const gdb_byte *p; |
| 1325 | unsigned char *digits; |
| 1326 | int carry; |
| 1327 | int decimal_len; |
| 1328 | int i, j, decimal_digits; |
| 1329 | int dummy; |
| 1330 | int flip; |
| 1331 | |
| 1332 | /* Base-ten number is less than twice as many digits |
| 1333 | as the base 16 number, which is 2 digits per byte. */ |
| 1334 | |
| 1335 | decimal_len = len * 2 * 2; |
| 1336 | digits = xmalloc (decimal_len); |
| 1337 | |
| 1338 | for (i = 0; i < decimal_len; i++) |
| 1339 | { |
| 1340 | digits[i] = 0; |
| 1341 | } |
| 1342 | |
| 1343 | /* Ok, we have an unknown number of bytes of data to be printed in |
| 1344 | * decimal. |
| 1345 | * |
| 1346 | * Given a hex number (in nibbles) as XYZ, we start by taking X and |
| 1347 | * decemalizing it as "x1 x2" in two decimal nibbles. Then we multiply |
| 1348 | * the nibbles by 16, add Y and re-decimalize. Repeat with Z. |
| 1349 | * |
| 1350 | * The trick is that "digits" holds a base-10 number, but sometimes |
| 1351 | * the individual digits are > 10. |
| 1352 | * |
| 1353 | * Outer loop is per nibble (hex digit) of input, from MSD end to |
| 1354 | * LSD end. |
| 1355 | */ |
| 1356 | decimal_digits = 0; /* Number of decimal digits so far */ |
| 1357 | p = (byte_order == BFD_ENDIAN_BIG) ? valaddr : valaddr + len - 1; |
| 1358 | flip = 0; |
| 1359 | while ((byte_order == BFD_ENDIAN_BIG) ? (p < valaddr + len) : (p >= valaddr)) |
| 1360 | { |
| 1361 | /* |
| 1362 | * Multiply current base-ten number by 16 in place. |
| 1363 | * Each digit was between 0 and 9, now is between |
| 1364 | * 0 and 144. |
| 1365 | */ |
| 1366 | for (j = 0; j < decimal_digits; j++) |
| 1367 | { |
| 1368 | digits[j] = SHIFT (digits[j]); |
| 1369 | } |
| 1370 | |
| 1371 | /* Take the next nibble off the input and add it to what |
| 1372 | * we've got in the LSB position. Bottom 'digit' is now |
| 1373 | * between 0 and 159. |
| 1374 | * |
| 1375 | * "flip" is used to run this loop twice for each byte. |
| 1376 | */ |
| 1377 | if (flip == 0) |
| 1378 | { |
| 1379 | /* Take top nibble. */ |
| 1380 | |
| 1381 | digits[0] += HIGH_NIBBLE (*p); |
| 1382 | flip = 1; |
| 1383 | } |
| 1384 | else |
| 1385 | { |
| 1386 | /* Take low nibble and bump our pointer "p". */ |
| 1387 | |
| 1388 | digits[0] += LOW_NIBBLE (*p); |
| 1389 | if (byte_order == BFD_ENDIAN_BIG) |
| 1390 | p++; |
| 1391 | else |
| 1392 | p--; |
| 1393 | flip = 0; |
| 1394 | } |
| 1395 | |
| 1396 | /* Re-decimalize. We have to do this often enough |
| 1397 | * that we don't overflow, but once per nibble is |
| 1398 | * overkill. Easier this way, though. Note that the |
| 1399 | * carry is often larger than 10 (e.g. max initial |
| 1400 | * carry out of lowest nibble is 15, could bubble all |
| 1401 | * the way up greater than 10). So we have to do |
| 1402 | * the carrying beyond the last current digit. |
| 1403 | */ |
| 1404 | carry = 0; |
| 1405 | for (j = 0; j < decimal_len - 1; j++) |
| 1406 | { |
| 1407 | digits[j] += carry; |
| 1408 | |
| 1409 | /* "/" won't handle an unsigned char with |
| 1410 | * a value that if signed would be negative. |
| 1411 | * So extend to longword int via "dummy". |
| 1412 | */ |
| 1413 | dummy = digits[j]; |
| 1414 | carry = CARRY_OUT (dummy); |
| 1415 | digits[j] = CARRY_LEFT (dummy); |
| 1416 | |
| 1417 | if (j >= decimal_digits && carry == 0) |
| 1418 | { |
| 1419 | /* |
| 1420 | * All higher digits are 0 and we |
| 1421 | * no longer have a carry. |
| 1422 | * |
| 1423 | * Note: "j" is 0-based, "decimal_digits" is |
| 1424 | * 1-based. |
| 1425 | */ |
| 1426 | decimal_digits = j + 1; |
| 1427 | break; |
| 1428 | } |
| 1429 | } |
| 1430 | } |
| 1431 | |
| 1432 | /* Ok, now "digits" is the decimal representation, with |
| 1433 | the "decimal_digits" actual digits. Print! */ |
| 1434 | |
| 1435 | for (i = decimal_digits - 1; i >= 0; i--) |
| 1436 | { |
| 1437 | fprintf_filtered (stream, "%1d", digits[i]); |
| 1438 | } |
| 1439 | xfree (digits); |
| 1440 | } |
| 1441 | |
| 1442 | /* VALADDR points to an integer of LEN bytes. Print it in hex on stream. */ |
| 1443 | |
| 1444 | void |
| 1445 | print_hex_chars (struct ui_file *stream, const gdb_byte *valaddr, |
| 1446 | unsigned len, enum bfd_endian byte_order) |
| 1447 | { |
| 1448 | const gdb_byte *p; |
| 1449 | |
| 1450 | /* FIXME: We should be not printing leading zeroes in most cases. */ |
| 1451 | |
| 1452 | fputs_filtered ("0x", stream); |
| 1453 | if (byte_order == BFD_ENDIAN_BIG) |
| 1454 | { |
| 1455 | for (p = valaddr; |
| 1456 | p < valaddr + len; |
| 1457 | p++) |
| 1458 | { |
| 1459 | fprintf_filtered (stream, "%02x", *p); |
| 1460 | } |
| 1461 | } |
| 1462 | else |
| 1463 | { |
| 1464 | for (p = valaddr + len - 1; |
| 1465 | p >= valaddr; |
| 1466 | p--) |
| 1467 | { |
| 1468 | fprintf_filtered (stream, "%02x", *p); |
| 1469 | } |
| 1470 | } |
| 1471 | } |
| 1472 | |
| 1473 | /* VALADDR points to a char integer of LEN bytes. |
| 1474 | Print it out in appropriate language form on stream. |
| 1475 | Omit any leading zero chars. */ |
| 1476 | |
| 1477 | void |
| 1478 | print_char_chars (struct ui_file *stream, struct type *type, |
| 1479 | const gdb_byte *valaddr, |
| 1480 | unsigned len, enum bfd_endian byte_order) |
| 1481 | { |
| 1482 | const gdb_byte *p; |
| 1483 | |
| 1484 | if (byte_order == BFD_ENDIAN_BIG) |
| 1485 | { |
| 1486 | p = valaddr; |
| 1487 | while (p < valaddr + len - 1 && *p == 0) |
| 1488 | ++p; |
| 1489 | |
| 1490 | while (p < valaddr + len) |
| 1491 | { |
| 1492 | LA_EMIT_CHAR (*p, type, stream, '\''); |
| 1493 | ++p; |
| 1494 | } |
| 1495 | } |
| 1496 | else |
| 1497 | { |
| 1498 | p = valaddr + len - 1; |
| 1499 | while (p > valaddr && *p == 0) |
| 1500 | --p; |
| 1501 | |
| 1502 | while (p >= valaddr) |
| 1503 | { |
| 1504 | LA_EMIT_CHAR (*p, type, stream, '\''); |
| 1505 | --p; |
| 1506 | } |
| 1507 | } |
| 1508 | } |
| 1509 | |
| 1510 | /* Print function pointer with inferior address ADDRESS onto stdio |
| 1511 | stream STREAM. */ |
| 1512 | |
| 1513 | void |
| 1514 | print_function_pointer_address (const struct value_print_options *options, |
| 1515 | struct gdbarch *gdbarch, |
| 1516 | CORE_ADDR address, |
| 1517 | struct ui_file *stream) |
| 1518 | { |
| 1519 | CORE_ADDR func_addr |
| 1520 | = gdbarch_convert_from_func_ptr_addr (gdbarch, address, |
| 1521 | ¤t_target); |
| 1522 | |
| 1523 | /* If the function pointer is represented by a description, print |
| 1524 | the address of the description. */ |
| 1525 | if (options->addressprint && func_addr != address) |
| 1526 | { |
| 1527 | fputs_filtered ("@", stream); |
| 1528 | fputs_filtered (paddress (gdbarch, address), stream); |
| 1529 | fputs_filtered (": ", stream); |
| 1530 | } |
| 1531 | print_address_demangle (options, gdbarch, func_addr, stream, demangle); |
| 1532 | } |
| 1533 | |
| 1534 | |
| 1535 | /* Print on STREAM using the given OPTIONS the index for the element |
| 1536 | at INDEX of an array whose index type is INDEX_TYPE. */ |
| 1537 | |
| 1538 | void |
| 1539 | maybe_print_array_index (struct type *index_type, LONGEST index, |
| 1540 | struct ui_file *stream, |
| 1541 | const struct value_print_options *options) |
| 1542 | { |
| 1543 | struct value *index_value; |
| 1544 | |
| 1545 | if (!options->print_array_indexes) |
| 1546 | return; |
| 1547 | |
| 1548 | index_value = value_from_longest (index_type, index); |
| 1549 | |
| 1550 | LA_PRINT_ARRAY_INDEX (index_value, stream, options); |
| 1551 | } |
| 1552 | |
| 1553 | /* Called by various <lang>_val_print routines to print elements of an |
| 1554 | array in the form "<elem1>, <elem2>, <elem3>, ...". |
| 1555 | |
| 1556 | (FIXME?) Assumes array element separator is a comma, which is correct |
| 1557 | for all languages currently handled. |
| 1558 | (FIXME?) Some languages have a notation for repeated array elements, |
| 1559 | perhaps we should try to use that notation when appropriate. */ |
| 1560 | |
| 1561 | void |
| 1562 | val_print_array_elements (struct type *type, |
| 1563 | const gdb_byte *valaddr, int embedded_offset, |
| 1564 | CORE_ADDR address, struct ui_file *stream, |
| 1565 | int recurse, |
| 1566 | const struct value *val, |
| 1567 | const struct value_print_options *options, |
| 1568 | unsigned int i) |
| 1569 | { |
| 1570 | unsigned int things_printed = 0; |
| 1571 | unsigned len; |
| 1572 | struct type *elttype, *index_type; |
| 1573 | unsigned eltlen; |
| 1574 | /* Position of the array element we are examining to see |
| 1575 | whether it is repeated. */ |
| 1576 | unsigned int rep1; |
| 1577 | /* Number of repetitions we have detected so far. */ |
| 1578 | unsigned int reps; |
| 1579 | LONGEST low_bound, high_bound; |
| 1580 | |
| 1581 | elttype = TYPE_TARGET_TYPE (type); |
| 1582 | eltlen = TYPE_LENGTH (check_typedef (elttype)); |
| 1583 | index_type = TYPE_INDEX_TYPE (type); |
| 1584 | |
| 1585 | if (get_array_bounds (type, &low_bound, &high_bound)) |
| 1586 | { |
| 1587 | /* The array length should normally be HIGH_BOUND - LOW_BOUND + 1. |
| 1588 | But we have to be a little extra careful, because some languages |
| 1589 | such as Ada allow LOW_BOUND to be greater than HIGH_BOUND for |
| 1590 | empty arrays. In that situation, the array length is just zero, |
| 1591 | not negative! */ |
| 1592 | if (low_bound > high_bound) |
| 1593 | len = 0; |
| 1594 | else |
| 1595 | len = high_bound - low_bound + 1; |
| 1596 | } |
| 1597 | else |
| 1598 | { |
| 1599 | warning (_("unable to get bounds of array, assuming null array")); |
| 1600 | low_bound = 0; |
| 1601 | len = 0; |
| 1602 | } |
| 1603 | |
| 1604 | annotate_array_section_begin (i, elttype); |
| 1605 | |
| 1606 | for (; i < len && things_printed < options->print_max; i++) |
| 1607 | { |
| 1608 | if (i != 0) |
| 1609 | { |
| 1610 | if (options->prettyprint_arrays) |
| 1611 | { |
| 1612 | fprintf_filtered (stream, ",\n"); |
| 1613 | print_spaces_filtered (2 + 2 * recurse, stream); |
| 1614 | } |
| 1615 | else |
| 1616 | { |
| 1617 | fprintf_filtered (stream, ", "); |
| 1618 | } |
| 1619 | } |
| 1620 | wrap_here (n_spaces (2 + 2 * recurse)); |
| 1621 | maybe_print_array_index (index_type, i + low_bound, |
| 1622 | stream, options); |
| 1623 | |
| 1624 | rep1 = i + 1; |
| 1625 | reps = 1; |
| 1626 | /* Only check for reps if repeat_count_threshold is not set to |
| 1627 | UINT_MAX (unlimited). */ |
| 1628 | if (options->repeat_count_threshold < UINT_MAX) |
| 1629 | { |
| 1630 | while (rep1 < len |
| 1631 | && value_available_contents_eq (val, |
| 1632 | embedded_offset + i * eltlen, |
| 1633 | val, |
| 1634 | (embedded_offset |
| 1635 | + rep1 * eltlen), |
| 1636 | eltlen)) |
| 1637 | { |
| 1638 | ++reps; |
| 1639 | ++rep1; |
| 1640 | } |
| 1641 | } |
| 1642 | |
| 1643 | if (reps > options->repeat_count_threshold) |
| 1644 | { |
| 1645 | val_print (elttype, valaddr, embedded_offset + i * eltlen, |
| 1646 | address, stream, recurse + 1, val, options, |
| 1647 | current_language); |
| 1648 | annotate_elt_rep (reps); |
| 1649 | fprintf_filtered (stream, " <repeats %u times>", reps); |
| 1650 | annotate_elt_rep_end (); |
| 1651 | |
| 1652 | i = rep1 - 1; |
| 1653 | things_printed += options->repeat_count_threshold; |
| 1654 | } |
| 1655 | else |
| 1656 | { |
| 1657 | val_print (elttype, valaddr, embedded_offset + i * eltlen, |
| 1658 | address, |
| 1659 | stream, recurse + 1, val, options, current_language); |
| 1660 | annotate_elt (); |
| 1661 | things_printed++; |
| 1662 | } |
| 1663 | } |
| 1664 | annotate_array_section_end (); |
| 1665 | if (i < len) |
| 1666 | { |
| 1667 | fprintf_filtered (stream, "..."); |
| 1668 | } |
| 1669 | } |
| 1670 | |
| 1671 | /* Read LEN bytes of target memory at address MEMADDR, placing the |
| 1672 | results in GDB's memory at MYADDR. Returns a count of the bytes |
| 1673 | actually read, and optionally an errno value in the location |
| 1674 | pointed to by ERRNOPTR if ERRNOPTR is non-null. */ |
| 1675 | |
| 1676 | /* FIXME: cagney/1999-10-14: Only used by val_print_string. Can this |
| 1677 | function be eliminated. */ |
| 1678 | |
| 1679 | static int |
| 1680 | partial_memory_read (CORE_ADDR memaddr, gdb_byte *myaddr, |
| 1681 | int len, int *errnoptr) |
| 1682 | { |
| 1683 | int nread; /* Number of bytes actually read. */ |
| 1684 | int errcode; /* Error from last read. */ |
| 1685 | |
| 1686 | /* First try a complete read. */ |
| 1687 | errcode = target_read_memory (memaddr, myaddr, len); |
| 1688 | if (errcode == 0) |
| 1689 | { |
| 1690 | /* Got it all. */ |
| 1691 | nread = len; |
| 1692 | } |
| 1693 | else |
| 1694 | { |
| 1695 | /* Loop, reading one byte at a time until we get as much as we can. */ |
| 1696 | for (errcode = 0, nread = 0; len > 0 && errcode == 0; nread++, len--) |
| 1697 | { |
| 1698 | errcode = target_read_memory (memaddr++, myaddr++, 1); |
| 1699 | } |
| 1700 | /* If an error, the last read was unsuccessful, so adjust count. */ |
| 1701 | if (errcode != 0) |
| 1702 | { |
| 1703 | nread--; |
| 1704 | } |
| 1705 | } |
| 1706 | if (errnoptr != NULL) |
| 1707 | { |
| 1708 | *errnoptr = errcode; |
| 1709 | } |
| 1710 | return (nread); |
| 1711 | } |
| 1712 | |
| 1713 | /* Read a string from the inferior, at ADDR, with LEN characters of WIDTH bytes |
| 1714 | each. Fetch at most FETCHLIMIT characters. BUFFER will be set to a newly |
| 1715 | allocated buffer containing the string, which the caller is responsible to |
| 1716 | free, and BYTES_READ will be set to the number of bytes read. Returns 0 on |
| 1717 | success, or errno on failure. |
| 1718 | |
| 1719 | If LEN > 0, reads exactly LEN characters (including eventual NULs in |
| 1720 | the middle or end of the string). If LEN is -1, stops at the first |
| 1721 | null character (not necessarily the first null byte) up to a maximum |
| 1722 | of FETCHLIMIT characters. Set FETCHLIMIT to UINT_MAX to read as many |
| 1723 | characters as possible from the string. |
| 1724 | |
| 1725 | Unless an exception is thrown, BUFFER will always be allocated, even on |
| 1726 | failure. In this case, some characters might have been read before the |
| 1727 | failure happened. Check BYTES_READ to recognize this situation. |
| 1728 | |
| 1729 | Note: There was a FIXME asking to make this code use target_read_string, |
| 1730 | but this function is more general (can read past null characters, up to |
| 1731 | given LEN). Besides, it is used much more often than target_read_string |
| 1732 | so it is more tested. Perhaps callers of target_read_string should use |
| 1733 | this function instead? */ |
| 1734 | |
| 1735 | int |
| 1736 | read_string (CORE_ADDR addr, int len, int width, unsigned int fetchlimit, |
| 1737 | enum bfd_endian byte_order, gdb_byte **buffer, int *bytes_read) |
| 1738 | { |
| 1739 | int found_nul; /* Non-zero if we found the nul char. */ |
| 1740 | int errcode; /* Errno returned from bad reads. */ |
| 1741 | unsigned int nfetch; /* Chars to fetch / chars fetched. */ |
| 1742 | unsigned int chunksize; /* Size of each fetch, in chars. */ |
| 1743 | gdb_byte *bufptr; /* Pointer to next available byte in |
| 1744 | buffer. */ |
| 1745 | gdb_byte *limit; /* First location past end of fetch buffer. */ |
| 1746 | struct cleanup *old_chain = NULL; /* Top of the old cleanup chain. */ |
| 1747 | |
| 1748 | /* Decide how large of chunks to try to read in one operation. This |
| 1749 | is also pretty simple. If LEN >= zero, then we want fetchlimit chars, |
| 1750 | so we might as well read them all in one operation. If LEN is -1, we |
| 1751 | are looking for a NUL terminator to end the fetching, so we might as |
| 1752 | well read in blocks that are large enough to be efficient, but not so |
| 1753 | large as to be slow if fetchlimit happens to be large. So we choose the |
| 1754 | minimum of 8 and fetchlimit. We used to use 200 instead of 8 but |
| 1755 | 200 is way too big for remote debugging over a serial line. */ |
| 1756 | |
| 1757 | chunksize = (len == -1 ? min (8, fetchlimit) : fetchlimit); |
| 1758 | |
| 1759 | /* Loop until we either have all the characters, or we encounter |
| 1760 | some error, such as bumping into the end of the address space. */ |
| 1761 | |
| 1762 | found_nul = 0; |
| 1763 | *buffer = NULL; |
| 1764 | |
| 1765 | old_chain = make_cleanup (free_current_contents, buffer); |
| 1766 | |
| 1767 | if (len > 0) |
| 1768 | { |
| 1769 | *buffer = (gdb_byte *) xmalloc (len * width); |
| 1770 | bufptr = *buffer; |
| 1771 | |
| 1772 | nfetch = partial_memory_read (addr, bufptr, len * width, &errcode) |
| 1773 | / width; |
| 1774 | addr += nfetch * width; |
| 1775 | bufptr += nfetch * width; |
| 1776 | } |
| 1777 | else if (len == -1) |
| 1778 | { |
| 1779 | unsigned long bufsize = 0; |
| 1780 | |
| 1781 | do |
| 1782 | { |
| 1783 | QUIT; |
| 1784 | nfetch = min (chunksize, fetchlimit - bufsize); |
| 1785 | |
| 1786 | if (*buffer == NULL) |
| 1787 | *buffer = (gdb_byte *) xmalloc (nfetch * width); |
| 1788 | else |
| 1789 | *buffer = (gdb_byte *) xrealloc (*buffer, |
| 1790 | (nfetch + bufsize) * width); |
| 1791 | |
| 1792 | bufptr = *buffer + bufsize * width; |
| 1793 | bufsize += nfetch; |
| 1794 | |
| 1795 | /* Read as much as we can. */ |
| 1796 | nfetch = partial_memory_read (addr, bufptr, nfetch * width, &errcode) |
| 1797 | / width; |
| 1798 | |
| 1799 | /* Scan this chunk for the null character that terminates the string |
| 1800 | to print. If found, we don't need to fetch any more. Note |
| 1801 | that bufptr is explicitly left pointing at the next character |
| 1802 | after the null character, or at the next character after the end |
| 1803 | of the buffer. */ |
| 1804 | |
| 1805 | limit = bufptr + nfetch * width; |
| 1806 | while (bufptr < limit) |
| 1807 | { |
| 1808 | unsigned long c; |
| 1809 | |
| 1810 | c = extract_unsigned_integer (bufptr, width, byte_order); |
| 1811 | addr += width; |
| 1812 | bufptr += width; |
| 1813 | if (c == 0) |
| 1814 | { |
| 1815 | /* We don't care about any error which happened after |
| 1816 | the NUL terminator. */ |
| 1817 | errcode = 0; |
| 1818 | found_nul = 1; |
| 1819 | break; |
| 1820 | } |
| 1821 | } |
| 1822 | } |
| 1823 | while (errcode == 0 /* no error */ |
| 1824 | && bufptr - *buffer < fetchlimit * width /* no overrun */ |
| 1825 | && !found_nul); /* haven't found NUL yet */ |
| 1826 | } |
| 1827 | else |
| 1828 | { /* Length of string is really 0! */ |
| 1829 | /* We always allocate *buffer. */ |
| 1830 | *buffer = bufptr = xmalloc (1); |
| 1831 | errcode = 0; |
| 1832 | } |
| 1833 | |
| 1834 | /* bufptr and addr now point immediately beyond the last byte which we |
| 1835 | consider part of the string (including a '\0' which ends the string). */ |
| 1836 | *bytes_read = bufptr - *buffer; |
| 1837 | |
| 1838 | QUIT; |
| 1839 | |
| 1840 | discard_cleanups (old_chain); |
| 1841 | |
| 1842 | return errcode; |
| 1843 | } |
| 1844 | |
| 1845 | /* Return true if print_wchar can display W without resorting to a |
| 1846 | numeric escape, false otherwise. */ |
| 1847 | |
| 1848 | static int |
| 1849 | wchar_printable (gdb_wchar_t w) |
| 1850 | { |
| 1851 | return (gdb_iswprint (w) |
| 1852 | || w == LCST ('\a') || w == LCST ('\b') |
| 1853 | || w == LCST ('\f') || w == LCST ('\n') |
| 1854 | || w == LCST ('\r') || w == LCST ('\t') |
| 1855 | || w == LCST ('\v')); |
| 1856 | } |
| 1857 | |
| 1858 | /* A helper function that converts the contents of STRING to wide |
| 1859 | characters and then appends them to OUTPUT. */ |
| 1860 | |
| 1861 | static void |
| 1862 | append_string_as_wide (const char *string, |
| 1863 | struct obstack *output) |
| 1864 | { |
| 1865 | for (; *string; ++string) |
| 1866 | { |
| 1867 | gdb_wchar_t w = gdb_btowc (*string); |
| 1868 | obstack_grow (output, &w, sizeof (gdb_wchar_t)); |
| 1869 | } |
| 1870 | } |
| 1871 | |
| 1872 | /* Print a wide character W to OUTPUT. ORIG is a pointer to the |
| 1873 | original (target) bytes representing the character, ORIG_LEN is the |
| 1874 | number of valid bytes. WIDTH is the number of bytes in a base |
| 1875 | characters of the type. OUTPUT is an obstack to which wide |
| 1876 | characters are emitted. QUOTER is a (narrow) character indicating |
| 1877 | the style of quotes surrounding the character to be printed. |
| 1878 | NEED_ESCAPE is an in/out flag which is used to track numeric |
| 1879 | escapes across calls. */ |
| 1880 | |
| 1881 | static void |
| 1882 | print_wchar (gdb_wint_t w, const gdb_byte *orig, |
| 1883 | int orig_len, int width, |
| 1884 | enum bfd_endian byte_order, |
| 1885 | struct obstack *output, |
| 1886 | int quoter, int *need_escapep) |
| 1887 | { |
| 1888 | int need_escape = *need_escapep; |
| 1889 | |
| 1890 | *need_escapep = 0; |
| 1891 | if (gdb_iswprint (w) && (!need_escape || (!gdb_iswdigit (w) |
| 1892 | && w != LCST ('8') |
| 1893 | && w != LCST ('9')))) |
| 1894 | { |
| 1895 | gdb_wchar_t wchar = w; |
| 1896 | |
| 1897 | if (w == gdb_btowc (quoter) || w == LCST ('\\')) |
| 1898 | obstack_grow_wstr (output, LCST ("\\")); |
| 1899 | obstack_grow (output, &wchar, sizeof (gdb_wchar_t)); |
| 1900 | } |
| 1901 | else |
| 1902 | { |
| 1903 | switch (w) |
| 1904 | { |
| 1905 | case LCST ('\a'): |
| 1906 | obstack_grow_wstr (output, LCST ("\\a")); |
| 1907 | break; |
| 1908 | case LCST ('\b'): |
| 1909 | obstack_grow_wstr (output, LCST ("\\b")); |
| 1910 | break; |
| 1911 | case LCST ('\f'): |
| 1912 | obstack_grow_wstr (output, LCST ("\\f")); |
| 1913 | break; |
| 1914 | case LCST ('\n'): |
| 1915 | obstack_grow_wstr (output, LCST ("\\n")); |
| 1916 | break; |
| 1917 | case LCST ('\r'): |
| 1918 | obstack_grow_wstr (output, LCST ("\\r")); |
| 1919 | break; |
| 1920 | case LCST ('\t'): |
| 1921 | obstack_grow_wstr (output, LCST ("\\t")); |
| 1922 | break; |
| 1923 | case LCST ('\v'): |
| 1924 | obstack_grow_wstr (output, LCST ("\\v")); |
| 1925 | break; |
| 1926 | default: |
| 1927 | { |
| 1928 | int i; |
| 1929 | |
| 1930 | for (i = 0; i + width <= orig_len; i += width) |
| 1931 | { |
| 1932 | char octal[30]; |
| 1933 | ULONGEST value; |
| 1934 | |
| 1935 | value = extract_unsigned_integer (&orig[i], width, |
| 1936 | byte_order); |
| 1937 | /* If the value fits in 3 octal digits, print it that |
| 1938 | way. Otherwise, print it as a hex escape. */ |
| 1939 | if (value <= 0777) |
| 1940 | sprintf (octal, "\\%.3o", (int) (value & 0777)); |
| 1941 | else |
| 1942 | sprintf (octal, "\\x%lx", (long) value); |
| 1943 | append_string_as_wide (octal, output); |
| 1944 | } |
| 1945 | /* If we somehow have extra bytes, print them now. */ |
| 1946 | while (i < orig_len) |
| 1947 | { |
| 1948 | char octal[5]; |
| 1949 | |
| 1950 | sprintf (octal, "\\%.3o", orig[i] & 0xff); |
| 1951 | append_string_as_wide (octal, output); |
| 1952 | ++i; |
| 1953 | } |
| 1954 | |
| 1955 | *need_escapep = 1; |
| 1956 | } |
| 1957 | break; |
| 1958 | } |
| 1959 | } |
| 1960 | } |
| 1961 | |
| 1962 | /* Print the character C on STREAM as part of the contents of a |
| 1963 | literal string whose delimiter is QUOTER. ENCODING names the |
| 1964 | encoding of C. */ |
| 1965 | |
| 1966 | void |
| 1967 | generic_emit_char (int c, struct type *type, struct ui_file *stream, |
| 1968 | int quoter, const char *encoding) |
| 1969 | { |
| 1970 | enum bfd_endian byte_order |
| 1971 | = gdbarch_byte_order (get_type_arch (type)); |
| 1972 | struct obstack wchar_buf, output; |
| 1973 | struct cleanup *cleanups; |
| 1974 | gdb_byte *buf; |
| 1975 | struct wchar_iterator *iter; |
| 1976 | int need_escape = 0; |
| 1977 | |
| 1978 | buf = alloca (TYPE_LENGTH (type)); |
| 1979 | pack_long (buf, type, c); |
| 1980 | |
| 1981 | iter = make_wchar_iterator (buf, TYPE_LENGTH (type), |
| 1982 | encoding, TYPE_LENGTH (type)); |
| 1983 | cleanups = make_cleanup_wchar_iterator (iter); |
| 1984 | |
| 1985 | /* This holds the printable form of the wchar_t data. */ |
| 1986 | obstack_init (&wchar_buf); |
| 1987 | make_cleanup_obstack_free (&wchar_buf); |
| 1988 | |
| 1989 | while (1) |
| 1990 | { |
| 1991 | int num_chars; |
| 1992 | gdb_wchar_t *chars; |
| 1993 | const gdb_byte *buf; |
| 1994 | size_t buflen; |
| 1995 | int print_escape = 1; |
| 1996 | enum wchar_iterate_result result; |
| 1997 | |
| 1998 | num_chars = wchar_iterate (iter, &result, &chars, &buf, &buflen); |
| 1999 | if (num_chars < 0) |
| 2000 | break; |
| 2001 | if (num_chars > 0) |
| 2002 | { |
| 2003 | /* If all characters are printable, print them. Otherwise, |
| 2004 | we're going to have to print an escape sequence. We |
| 2005 | check all characters because we want to print the target |
| 2006 | bytes in the escape sequence, and we don't know character |
| 2007 | boundaries there. */ |
| 2008 | int i; |
| 2009 | |
| 2010 | print_escape = 0; |
| 2011 | for (i = 0; i < num_chars; ++i) |
| 2012 | if (!wchar_printable (chars[i])) |
| 2013 | { |
| 2014 | print_escape = 1; |
| 2015 | break; |
| 2016 | } |
| 2017 | |
| 2018 | if (!print_escape) |
| 2019 | { |
| 2020 | for (i = 0; i < num_chars; ++i) |
| 2021 | print_wchar (chars[i], buf, buflen, |
| 2022 | TYPE_LENGTH (type), byte_order, |
| 2023 | &wchar_buf, quoter, &need_escape); |
| 2024 | } |
| 2025 | } |
| 2026 | |
| 2027 | /* This handles the NUM_CHARS == 0 case as well. */ |
| 2028 | if (print_escape) |
| 2029 | print_wchar (gdb_WEOF, buf, buflen, TYPE_LENGTH (type), |
| 2030 | byte_order, &wchar_buf, quoter, &need_escape); |
| 2031 | } |
| 2032 | |
| 2033 | /* The output in the host encoding. */ |
| 2034 | obstack_init (&output); |
| 2035 | make_cleanup_obstack_free (&output); |
| 2036 | |
| 2037 | convert_between_encodings (INTERMEDIATE_ENCODING, host_charset (), |
| 2038 | obstack_base (&wchar_buf), |
| 2039 | obstack_object_size (&wchar_buf), |
| 2040 | 1, &output, translit_char); |
| 2041 | obstack_1grow (&output, '\0'); |
| 2042 | |
| 2043 | fputs_filtered (obstack_base (&output), stream); |
| 2044 | |
| 2045 | do_cleanups (cleanups); |
| 2046 | } |
| 2047 | |
| 2048 | /* Print the character string STRING, printing at most LENGTH |
| 2049 | characters. LENGTH is -1 if the string is nul terminated. TYPE is |
| 2050 | the type of each character. OPTIONS holds the printing options; |
| 2051 | printing stops early if the number hits print_max; repeat counts |
| 2052 | are printed as appropriate. Print ellipses at the end if we had to |
| 2053 | stop before printing LENGTH characters, or if FORCE_ELLIPSES. |
| 2054 | QUOTE_CHAR is the character to print at each end of the string. If |
| 2055 | C_STYLE_TERMINATOR is true, and the last character is 0, then it is |
| 2056 | omitted. */ |
| 2057 | |
| 2058 | void |
| 2059 | generic_printstr (struct ui_file *stream, struct type *type, |
| 2060 | const gdb_byte *string, unsigned int length, |
| 2061 | const char *encoding, int force_ellipses, |
| 2062 | int quote_char, int c_style_terminator, |
| 2063 | const struct value_print_options *options) |
| 2064 | { |
| 2065 | enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type)); |
| 2066 | unsigned int i; |
| 2067 | unsigned int things_printed = 0; |
| 2068 | int in_quotes = 0; |
| 2069 | int need_comma = 0; |
| 2070 | int width = TYPE_LENGTH (type); |
| 2071 | struct obstack wchar_buf, output; |
| 2072 | struct cleanup *cleanup; |
| 2073 | struct wchar_iterator *iter; |
| 2074 | int finished = 0; |
| 2075 | int need_escape = 0; |
| 2076 | gdb_wchar_t wide_quote_char = gdb_btowc (quote_char); |
| 2077 | |
| 2078 | if (length == -1) |
| 2079 | { |
| 2080 | unsigned long current_char = 1; |
| 2081 | |
| 2082 | for (i = 0; current_char; ++i) |
| 2083 | { |
| 2084 | QUIT; |
| 2085 | current_char = extract_unsigned_integer (string + i * width, |
| 2086 | width, byte_order); |
| 2087 | } |
| 2088 | length = i; |
| 2089 | } |
| 2090 | |
| 2091 | /* If the string was not truncated due to `set print elements', and |
| 2092 | the last byte of it is a null, we don't print that, in |
| 2093 | traditional C style. */ |
| 2094 | if (c_style_terminator |
| 2095 | && !force_ellipses |
| 2096 | && length > 0 |
| 2097 | && (extract_unsigned_integer (string + (length - 1) * width, |
| 2098 | width, byte_order) == 0)) |
| 2099 | length--; |
| 2100 | |
| 2101 | if (length == 0) |
| 2102 | { |
| 2103 | fputs_filtered ("\"\"", stream); |
| 2104 | return; |
| 2105 | } |
| 2106 | |
| 2107 | /* Arrange to iterate over the characters, in wchar_t form. */ |
| 2108 | iter = make_wchar_iterator (string, length * width, encoding, width); |
| 2109 | cleanup = make_cleanup_wchar_iterator (iter); |
| 2110 | |
| 2111 | /* WCHAR_BUF is the obstack we use to represent the string in |
| 2112 | wchar_t form. */ |
| 2113 | obstack_init (&wchar_buf); |
| 2114 | make_cleanup_obstack_free (&wchar_buf); |
| 2115 | |
| 2116 | while (!finished && things_printed < options->print_max) |
| 2117 | { |
| 2118 | int num_chars; |
| 2119 | enum wchar_iterate_result result; |
| 2120 | gdb_wchar_t *chars; |
| 2121 | const gdb_byte *buf; |
| 2122 | size_t buflen; |
| 2123 | |
| 2124 | QUIT; |
| 2125 | |
| 2126 | if (need_comma) |
| 2127 | { |
| 2128 | obstack_grow_wstr (&wchar_buf, LCST (", ")); |
| 2129 | need_comma = 0; |
| 2130 | } |
| 2131 | |
| 2132 | num_chars = wchar_iterate (iter, &result, &chars, &buf, &buflen); |
| 2133 | /* We only look at repetitions when we were able to convert a |
| 2134 | single character in isolation. This makes the code simpler |
| 2135 | and probably does the sensible thing in the majority of |
| 2136 | cases. */ |
| 2137 | while (num_chars == 1 && things_printed < options->print_max) |
| 2138 | { |
| 2139 | /* Count the number of repetitions. */ |
| 2140 | unsigned int reps = 0; |
| 2141 | gdb_wchar_t current_char = chars[0]; |
| 2142 | const gdb_byte *orig_buf = buf; |
| 2143 | int orig_len = buflen; |
| 2144 | |
| 2145 | if (need_comma) |
| 2146 | { |
| 2147 | obstack_grow_wstr (&wchar_buf, LCST (", ")); |
| 2148 | need_comma = 0; |
| 2149 | } |
| 2150 | |
| 2151 | while (num_chars == 1 && current_char == chars[0]) |
| 2152 | { |
| 2153 | num_chars = wchar_iterate (iter, &result, &chars, |
| 2154 | &buf, &buflen); |
| 2155 | ++reps; |
| 2156 | } |
| 2157 | |
| 2158 | /* Emit CURRENT_CHAR according to the repetition count and |
| 2159 | options. */ |
| 2160 | if (reps > options->repeat_count_threshold) |
| 2161 | { |
| 2162 | if (in_quotes) |
| 2163 | { |
| 2164 | if (options->inspect_it) |
| 2165 | obstack_grow_wstr (&wchar_buf, LCST ("\\")); |
| 2166 | obstack_grow (&wchar_buf, &wide_quote_char, |
| 2167 | sizeof (gdb_wchar_t)); |
| 2168 | obstack_grow_wstr (&wchar_buf, LCST (", ")); |
| 2169 | in_quotes = 0; |
| 2170 | } |
| 2171 | obstack_grow_wstr (&wchar_buf, LCST ("'")); |
| 2172 | need_escape = 0; |
| 2173 | print_wchar (current_char, orig_buf, orig_len, width, |
| 2174 | byte_order, &wchar_buf, '\'', &need_escape); |
| 2175 | obstack_grow_wstr (&wchar_buf, LCST ("'")); |
| 2176 | { |
| 2177 | /* Painful gyrations. */ |
| 2178 | int j; |
| 2179 | char *s = xstrprintf (_(" <repeats %u times>"), reps); |
| 2180 | |
| 2181 | for (j = 0; s[j]; ++j) |
| 2182 | { |
| 2183 | gdb_wchar_t w = gdb_btowc (s[j]); |
| 2184 | obstack_grow (&wchar_buf, &w, sizeof (gdb_wchar_t)); |
| 2185 | } |
| 2186 | xfree (s); |
| 2187 | } |
| 2188 | things_printed += options->repeat_count_threshold; |
| 2189 | need_comma = 1; |
| 2190 | } |
| 2191 | else |
| 2192 | { |
| 2193 | /* Saw the character one or more times, but fewer than |
| 2194 | the repetition threshold. */ |
| 2195 | if (!in_quotes) |
| 2196 | { |
| 2197 | if (options->inspect_it) |
| 2198 | obstack_grow_wstr (&wchar_buf, LCST ("\\")); |
| 2199 | obstack_grow (&wchar_buf, &wide_quote_char, |
| 2200 | sizeof (gdb_wchar_t)); |
| 2201 | in_quotes = 1; |
| 2202 | need_escape = 0; |
| 2203 | } |
| 2204 | |
| 2205 | while (reps-- > 0) |
| 2206 | { |
| 2207 | print_wchar (current_char, orig_buf, |
| 2208 | orig_len, width, |
| 2209 | byte_order, &wchar_buf, |
| 2210 | quote_char, &need_escape); |
| 2211 | ++things_printed; |
| 2212 | } |
| 2213 | } |
| 2214 | } |
| 2215 | |
| 2216 | /* NUM_CHARS and the other outputs from wchar_iterate are valid |
| 2217 | here regardless of which branch was taken above. */ |
| 2218 | if (num_chars < 0) |
| 2219 | { |
| 2220 | /* Hit EOF. */ |
| 2221 | finished = 1; |
| 2222 | break; |
| 2223 | } |
| 2224 | |
| 2225 | switch (result) |
| 2226 | { |
| 2227 | case wchar_iterate_invalid: |
| 2228 | if (!in_quotes) |
| 2229 | { |
| 2230 | if (options->inspect_it) |
| 2231 | obstack_grow_wstr (&wchar_buf, LCST ("\\")); |
| 2232 | obstack_grow (&wchar_buf, &wide_quote_char, |
| 2233 | sizeof (gdb_wchar_t)); |
| 2234 | in_quotes = 1; |
| 2235 | } |
| 2236 | need_escape = 0; |
| 2237 | print_wchar (gdb_WEOF, buf, buflen, width, byte_order, |
| 2238 | &wchar_buf, quote_char, &need_escape); |
| 2239 | break; |
| 2240 | |
| 2241 | case wchar_iterate_incomplete: |
| 2242 | if (in_quotes) |
| 2243 | { |
| 2244 | if (options->inspect_it) |
| 2245 | obstack_grow_wstr (&wchar_buf, LCST ("\\")); |
| 2246 | obstack_grow (&wchar_buf, &wide_quote_char, |
| 2247 | sizeof (gdb_wchar_t)); |
| 2248 | obstack_grow_wstr (&wchar_buf, LCST (",")); |
| 2249 | in_quotes = 0; |
| 2250 | } |
| 2251 | obstack_grow_wstr (&wchar_buf, |
| 2252 | LCST (" <incomplete sequence ")); |
| 2253 | print_wchar (gdb_WEOF, buf, buflen, width, |
| 2254 | byte_order, &wchar_buf, |
| 2255 | 0, &need_escape); |
| 2256 | obstack_grow_wstr (&wchar_buf, LCST (">")); |
| 2257 | finished = 1; |
| 2258 | break; |
| 2259 | } |
| 2260 | } |
| 2261 | |
| 2262 | /* Terminate the quotes if necessary. */ |
| 2263 | if (in_quotes) |
| 2264 | { |
| 2265 | if (options->inspect_it) |
| 2266 | obstack_grow_wstr (&wchar_buf, LCST ("\\")); |
| 2267 | obstack_grow (&wchar_buf, &wide_quote_char, |
| 2268 | sizeof (gdb_wchar_t)); |
| 2269 | } |
| 2270 | |
| 2271 | if (force_ellipses || !finished) |
| 2272 | obstack_grow_wstr (&wchar_buf, LCST ("...")); |
| 2273 | |
| 2274 | /* OUTPUT is where we collect `char's for printing. */ |
| 2275 | obstack_init (&output); |
| 2276 | make_cleanup_obstack_free (&output); |
| 2277 | |
| 2278 | convert_between_encodings (INTERMEDIATE_ENCODING, host_charset (), |
| 2279 | obstack_base (&wchar_buf), |
| 2280 | obstack_object_size (&wchar_buf), |
| 2281 | 1, &output, translit_char); |
| 2282 | obstack_1grow (&output, '\0'); |
| 2283 | |
| 2284 | fputs_filtered (obstack_base (&output), stream); |
| 2285 | |
| 2286 | do_cleanups (cleanup); |
| 2287 | } |
| 2288 | |
| 2289 | /* Print a string from the inferior, starting at ADDR and printing up to LEN |
| 2290 | characters, of WIDTH bytes a piece, to STREAM. If LEN is -1, printing |
| 2291 | stops at the first null byte, otherwise printing proceeds (including null |
| 2292 | bytes) until either print_max or LEN characters have been printed, |
| 2293 | whichever is smaller. ENCODING is the name of the string's |
| 2294 | encoding. It can be NULL, in which case the target encoding is |
| 2295 | assumed. */ |
| 2296 | |
| 2297 | int |
| 2298 | val_print_string (struct type *elttype, const char *encoding, |
| 2299 | CORE_ADDR addr, int len, |
| 2300 | struct ui_file *stream, |
| 2301 | const struct value_print_options *options) |
| 2302 | { |
| 2303 | int force_ellipsis = 0; /* Force ellipsis to be printed if nonzero. */ |
| 2304 | int errcode; /* Errno returned from bad reads. */ |
| 2305 | int found_nul; /* Non-zero if we found the nul char. */ |
| 2306 | unsigned int fetchlimit; /* Maximum number of chars to print. */ |
| 2307 | int bytes_read; |
| 2308 | gdb_byte *buffer = NULL; /* Dynamically growable fetch buffer. */ |
| 2309 | struct cleanup *old_chain = NULL; /* Top of the old cleanup chain. */ |
| 2310 | struct gdbarch *gdbarch = get_type_arch (elttype); |
| 2311 | enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); |
| 2312 | int width = TYPE_LENGTH (elttype); |
| 2313 | |
| 2314 | /* First we need to figure out the limit on the number of characters we are |
| 2315 | going to attempt to fetch and print. This is actually pretty simple. If |
| 2316 | LEN >= zero, then the limit is the minimum of LEN and print_max. If |
| 2317 | LEN is -1, then the limit is print_max. This is true regardless of |
| 2318 | whether print_max is zero, UINT_MAX (unlimited), or something in between, |
| 2319 | because finding the null byte (or available memory) is what actually |
| 2320 | limits the fetch. */ |
| 2321 | |
| 2322 | fetchlimit = (len == -1 ? options->print_max : min (len, |
| 2323 | options->print_max)); |
| 2324 | |
| 2325 | errcode = read_string (addr, len, width, fetchlimit, byte_order, |
| 2326 | &buffer, &bytes_read); |
| 2327 | old_chain = make_cleanup (xfree, buffer); |
| 2328 | |
| 2329 | addr += bytes_read; |
| 2330 | |
| 2331 | /* We now have either successfully filled the buffer to fetchlimit, |
| 2332 | or terminated early due to an error or finding a null char when |
| 2333 | LEN is -1. */ |
| 2334 | |
| 2335 | /* Determine found_nul by looking at the last character read. */ |
| 2336 | found_nul = extract_unsigned_integer (buffer + bytes_read - width, width, |
| 2337 | byte_order) == 0; |
| 2338 | if (len == -1 && !found_nul) |
| 2339 | { |
| 2340 | gdb_byte *peekbuf; |
| 2341 | |
| 2342 | /* We didn't find a NUL terminator we were looking for. Attempt |
| 2343 | to peek at the next character. If not successful, or it is not |
| 2344 | a null byte, then force ellipsis to be printed. */ |
| 2345 | |
| 2346 | peekbuf = (gdb_byte *) alloca (width); |
| 2347 | |
| 2348 | if (target_read_memory (addr, peekbuf, width) == 0 |
| 2349 | && extract_unsigned_integer (peekbuf, width, byte_order) != 0) |
| 2350 | force_ellipsis = 1; |
| 2351 | } |
| 2352 | else if ((len >= 0 && errcode != 0) || (len > bytes_read / width)) |
| 2353 | { |
| 2354 | /* Getting an error when we have a requested length, or fetching less |
| 2355 | than the number of characters actually requested, always make us |
| 2356 | print ellipsis. */ |
| 2357 | force_ellipsis = 1; |
| 2358 | } |
| 2359 | |
| 2360 | /* If we get an error before fetching anything, don't print a string. |
| 2361 | But if we fetch something and then get an error, print the string |
| 2362 | and then the error message. */ |
| 2363 | if (errcode == 0 || bytes_read > 0) |
| 2364 | { |
| 2365 | LA_PRINT_STRING (stream, elttype, buffer, bytes_read / width, |
| 2366 | encoding, force_ellipsis, options); |
| 2367 | } |
| 2368 | |
| 2369 | if (errcode != 0) |
| 2370 | { |
| 2371 | if (errcode == EIO) |
| 2372 | { |
| 2373 | fprintf_filtered (stream, "<Address "); |
| 2374 | fputs_filtered (paddress (gdbarch, addr), stream); |
| 2375 | fprintf_filtered (stream, " out of bounds>"); |
| 2376 | } |
| 2377 | else |
| 2378 | { |
| 2379 | fprintf_filtered (stream, "<Error reading address "); |
| 2380 | fputs_filtered (paddress (gdbarch, addr), stream); |
| 2381 | fprintf_filtered (stream, ": %s>", safe_strerror (errcode)); |
| 2382 | } |
| 2383 | } |
| 2384 | |
| 2385 | gdb_flush (stream); |
| 2386 | do_cleanups (old_chain); |
| 2387 | |
| 2388 | return (bytes_read / width); |
| 2389 | } |
| 2390 | \f |
| 2391 | |
| 2392 | /* The 'set input-radix' command writes to this auxiliary variable. |
| 2393 | If the requested radix is valid, INPUT_RADIX is updated; otherwise, |
| 2394 | it is left unchanged. */ |
| 2395 | |
| 2396 | static unsigned input_radix_1 = 10; |
| 2397 | |
| 2398 | /* Validate an input or output radix setting, and make sure the user |
| 2399 | knows what they really did here. Radix setting is confusing, e.g. |
| 2400 | setting the input radix to "10" never changes it! */ |
| 2401 | |
| 2402 | static void |
| 2403 | set_input_radix (char *args, int from_tty, struct cmd_list_element *c) |
| 2404 | { |
| 2405 | set_input_radix_1 (from_tty, input_radix_1); |
| 2406 | } |
| 2407 | |
| 2408 | static void |
| 2409 | set_input_radix_1 (int from_tty, unsigned radix) |
| 2410 | { |
| 2411 | /* We don't currently disallow any input radix except 0 or 1, which don't |
| 2412 | make any mathematical sense. In theory, we can deal with any input |
| 2413 | radix greater than 1, even if we don't have unique digits for every |
| 2414 | value from 0 to radix-1, but in practice we lose on large radix values. |
| 2415 | We should either fix the lossage or restrict the radix range more. |
| 2416 | (FIXME). */ |
| 2417 | |
| 2418 | if (radix < 2) |
| 2419 | { |
| 2420 | input_radix_1 = input_radix; |
| 2421 | error (_("Nonsense input radix ``decimal %u''; input radix unchanged."), |
| 2422 | radix); |
| 2423 | } |
| 2424 | input_radix_1 = input_radix = radix; |
| 2425 | if (from_tty) |
| 2426 | { |
| 2427 | printf_filtered (_("Input radix now set to " |
| 2428 | "decimal %u, hex %x, octal %o.\n"), |
| 2429 | radix, radix, radix); |
| 2430 | } |
| 2431 | } |
| 2432 | |
| 2433 | /* The 'set output-radix' command writes to this auxiliary variable. |
| 2434 | If the requested radix is valid, OUTPUT_RADIX is updated, |
| 2435 | otherwise, it is left unchanged. */ |
| 2436 | |
| 2437 | static unsigned output_radix_1 = 10; |
| 2438 | |
| 2439 | static void |
| 2440 | set_output_radix (char *args, int from_tty, struct cmd_list_element *c) |
| 2441 | { |
| 2442 | set_output_radix_1 (from_tty, output_radix_1); |
| 2443 | } |
| 2444 | |
| 2445 | static void |
| 2446 | set_output_radix_1 (int from_tty, unsigned radix) |
| 2447 | { |
| 2448 | /* Validate the radix and disallow ones that we aren't prepared to |
| 2449 | handle correctly, leaving the radix unchanged. */ |
| 2450 | switch (radix) |
| 2451 | { |
| 2452 | case 16: |
| 2453 | user_print_options.output_format = 'x'; /* hex */ |
| 2454 | break; |
| 2455 | case 10: |
| 2456 | user_print_options.output_format = 0; /* decimal */ |
| 2457 | break; |
| 2458 | case 8: |
| 2459 | user_print_options.output_format = 'o'; /* octal */ |
| 2460 | break; |
| 2461 | default: |
| 2462 | output_radix_1 = output_radix; |
| 2463 | error (_("Unsupported output radix ``decimal %u''; " |
| 2464 | "output radix unchanged."), |
| 2465 | radix); |
| 2466 | } |
| 2467 | output_radix_1 = output_radix = radix; |
| 2468 | if (from_tty) |
| 2469 | { |
| 2470 | printf_filtered (_("Output radix now set to " |
| 2471 | "decimal %u, hex %x, octal %o.\n"), |
| 2472 | radix, radix, radix); |
| 2473 | } |
| 2474 | } |
| 2475 | |
| 2476 | /* Set both the input and output radix at once. Try to set the output radix |
| 2477 | first, since it has the most restrictive range. An radix that is valid as |
| 2478 | an output radix is also valid as an input radix. |
| 2479 | |
| 2480 | It may be useful to have an unusual input radix. If the user wishes to |
| 2481 | set an input radix that is not valid as an output radix, he needs to use |
| 2482 | the 'set input-radix' command. */ |
| 2483 | |
| 2484 | static void |
| 2485 | set_radix (char *arg, int from_tty) |
| 2486 | { |
| 2487 | unsigned radix; |
| 2488 | |
| 2489 | radix = (arg == NULL) ? 10 : parse_and_eval_long (arg); |
| 2490 | set_output_radix_1 (0, radix); |
| 2491 | set_input_radix_1 (0, radix); |
| 2492 | if (from_tty) |
| 2493 | { |
| 2494 | printf_filtered (_("Input and output radices now set to " |
| 2495 | "decimal %u, hex %x, octal %o.\n"), |
| 2496 | radix, radix, radix); |
| 2497 | } |
| 2498 | } |
| 2499 | |
| 2500 | /* Show both the input and output radices. */ |
| 2501 | |
| 2502 | static void |
| 2503 | show_radix (char *arg, int from_tty) |
| 2504 | { |
| 2505 | if (from_tty) |
| 2506 | { |
| 2507 | if (input_radix == output_radix) |
| 2508 | { |
| 2509 | printf_filtered (_("Input and output radices set to " |
| 2510 | "decimal %u, hex %x, octal %o.\n"), |
| 2511 | input_radix, input_radix, input_radix); |
| 2512 | } |
| 2513 | else |
| 2514 | { |
| 2515 | printf_filtered (_("Input radix set to decimal " |
| 2516 | "%u, hex %x, octal %o.\n"), |
| 2517 | input_radix, input_radix, input_radix); |
| 2518 | printf_filtered (_("Output radix set to decimal " |
| 2519 | "%u, hex %x, octal %o.\n"), |
| 2520 | output_radix, output_radix, output_radix); |
| 2521 | } |
| 2522 | } |
| 2523 | } |
| 2524 | \f |
| 2525 | |
| 2526 | static void |
| 2527 | set_print (char *arg, int from_tty) |
| 2528 | { |
| 2529 | printf_unfiltered ( |
| 2530 | "\"set print\" must be followed by the name of a print subcommand.\n"); |
| 2531 | help_list (setprintlist, "set print ", -1, gdb_stdout); |
| 2532 | } |
| 2533 | |
| 2534 | static void |
| 2535 | show_print (char *args, int from_tty) |
| 2536 | { |
| 2537 | cmd_show_list (showprintlist, from_tty, ""); |
| 2538 | } |
| 2539 | \f |
| 2540 | void |
| 2541 | _initialize_valprint (void) |
| 2542 | { |
| 2543 | add_prefix_cmd ("print", no_class, set_print, |
| 2544 | _("Generic command for setting how things print."), |
| 2545 | &setprintlist, "set print ", 0, &setlist); |
| 2546 | add_alias_cmd ("p", "print", no_class, 1, &setlist); |
| 2547 | /* Prefer set print to set prompt. */ |
| 2548 | add_alias_cmd ("pr", "print", no_class, 1, &setlist); |
| 2549 | |
| 2550 | add_prefix_cmd ("print", no_class, show_print, |
| 2551 | _("Generic command for showing print settings."), |
| 2552 | &showprintlist, "show print ", 0, &showlist); |
| 2553 | add_alias_cmd ("p", "print", no_class, 1, &showlist); |
| 2554 | add_alias_cmd ("pr", "print", no_class, 1, &showlist); |
| 2555 | |
| 2556 | add_setshow_uinteger_cmd ("elements", no_class, |
| 2557 | &user_print_options.print_max, _("\ |
| 2558 | Set limit on string chars or array elements to print."), _("\ |
| 2559 | Show limit on string chars or array elements to print."), _("\ |
| 2560 | \"set print elements 0\" causes there to be no limit."), |
| 2561 | NULL, |
| 2562 | show_print_max, |
| 2563 | &setprintlist, &showprintlist); |
| 2564 | |
| 2565 | add_setshow_boolean_cmd ("null-stop", no_class, |
| 2566 | &user_print_options.stop_print_at_null, _("\ |
| 2567 | Set printing of char arrays to stop at first null char."), _("\ |
| 2568 | Show printing of char arrays to stop at first null char."), NULL, |
| 2569 | NULL, |
| 2570 | show_stop_print_at_null, |
| 2571 | &setprintlist, &showprintlist); |
| 2572 | |
| 2573 | add_setshow_uinteger_cmd ("repeats", no_class, |
| 2574 | &user_print_options.repeat_count_threshold, _("\ |
| 2575 | Set threshold for repeated print elements."), _("\ |
| 2576 | Show threshold for repeated print elements."), _("\ |
| 2577 | \"set print repeats 0\" causes all elements to be individually printed."), |
| 2578 | NULL, |
| 2579 | show_repeat_count_threshold, |
| 2580 | &setprintlist, &showprintlist); |
| 2581 | |
| 2582 | add_setshow_boolean_cmd ("pretty", class_support, |
| 2583 | &user_print_options.prettyprint_structs, _("\ |
| 2584 | Set prettyprinting of structures."), _("\ |
| 2585 | Show prettyprinting of structures."), NULL, |
| 2586 | NULL, |
| 2587 | show_prettyprint_structs, |
| 2588 | &setprintlist, &showprintlist); |
| 2589 | |
| 2590 | add_setshow_boolean_cmd ("union", class_support, |
| 2591 | &user_print_options.unionprint, _("\ |
| 2592 | Set printing of unions interior to structures."), _("\ |
| 2593 | Show printing of unions interior to structures."), NULL, |
| 2594 | NULL, |
| 2595 | show_unionprint, |
| 2596 | &setprintlist, &showprintlist); |
| 2597 | |
| 2598 | add_setshow_boolean_cmd ("array", class_support, |
| 2599 | &user_print_options.prettyprint_arrays, _("\ |
| 2600 | Set prettyprinting of arrays."), _("\ |
| 2601 | Show prettyprinting of arrays."), NULL, |
| 2602 | NULL, |
| 2603 | show_prettyprint_arrays, |
| 2604 | &setprintlist, &showprintlist); |
| 2605 | |
| 2606 | add_setshow_boolean_cmd ("address", class_support, |
| 2607 | &user_print_options.addressprint, _("\ |
| 2608 | Set printing of addresses."), _("\ |
| 2609 | Show printing of addresses."), NULL, |
| 2610 | NULL, |
| 2611 | show_addressprint, |
| 2612 | &setprintlist, &showprintlist); |
| 2613 | |
| 2614 | add_setshow_boolean_cmd ("symbol", class_support, |
| 2615 | &user_print_options.symbol_print, _("\ |
| 2616 | Set printing of symbol names when printing pointers."), _("\ |
| 2617 | Show printing of symbol names when printing pointers."), |
| 2618 | NULL, NULL, |
| 2619 | show_symbol_print, |
| 2620 | &setprintlist, &showprintlist); |
| 2621 | |
| 2622 | add_setshow_zuinteger_cmd ("input-radix", class_support, &input_radix_1, |
| 2623 | _("\ |
| 2624 | Set default input radix for entering numbers."), _("\ |
| 2625 | Show default input radix for entering numbers."), NULL, |
| 2626 | set_input_radix, |
| 2627 | show_input_radix, |
| 2628 | &setlist, &showlist); |
| 2629 | |
| 2630 | add_setshow_zuinteger_cmd ("output-radix", class_support, &output_radix_1, |
| 2631 | _("\ |
| 2632 | Set default output radix for printing of values."), _("\ |
| 2633 | Show default output radix for printing of values."), NULL, |
| 2634 | set_output_radix, |
| 2635 | show_output_radix, |
| 2636 | &setlist, &showlist); |
| 2637 | |
| 2638 | /* The "set radix" and "show radix" commands are special in that |
| 2639 | they are like normal set and show commands but allow two normally |
| 2640 | independent variables to be either set or shown with a single |
| 2641 | command. So the usual deprecated_add_set_cmd() and [deleted] |
| 2642 | add_show_from_set() commands aren't really appropriate. */ |
| 2643 | /* FIXME: i18n: With the new add_setshow_integer command, that is no |
| 2644 | longer true - show can display anything. */ |
| 2645 | add_cmd ("radix", class_support, set_radix, _("\ |
| 2646 | Set default input and output number radices.\n\ |
| 2647 | Use 'set input-radix' or 'set output-radix' to independently set each.\n\ |
| 2648 | Without an argument, sets both radices back to the default value of 10."), |
| 2649 | &setlist); |
| 2650 | add_cmd ("radix", class_support, show_radix, _("\ |
| 2651 | Show the default input and output number radices.\n\ |
| 2652 | Use 'show input-radix' or 'show output-radix' to independently show each."), |
| 2653 | &showlist); |
| 2654 | |
| 2655 | add_setshow_boolean_cmd ("array-indexes", class_support, |
| 2656 | &user_print_options.print_array_indexes, _("\ |
| 2657 | Set printing of array indexes."), _("\ |
| 2658 | Show printing of array indexes"), NULL, NULL, show_print_array_indexes, |
| 2659 | &setprintlist, &showprintlist); |
| 2660 | } |