1 /* Support for printing C values for GDB, the GNU debugger. 2 3 Copyright (C) 1986, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 4 1998, 1999, 2000, 2001, 2003, 2005, 2006, 2007, 2008, 2009 5 Free Software Foundation, Inc. 6 7 This file is part of GDB. 8 9 This program is free software; you can redistribute it and/or modify 10 it under the terms of the GNU General Public License as published by 11 the Free Software Foundation; either version 3 of the License, or 12 (at your option) any later version. 13 14 This program is distributed in the hope that it will be useful, 15 but WITHOUT ANY WARRANTY; without even the implied warranty of 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 GNU General Public License for more details. 18 19 You should have received a copy of the GNU General Public License 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 21 22 #include "defs.h" 23 #include "gdb_string.h" 24 #include "symtab.h" 25 #include "gdbtypes.h" 26 #include "expression.h" 27 #include "value.h" 28 #include "valprint.h" 29 #include "language.h" 30 #include "c-lang.h" 31 #include "cp-abi.h" 32 #include "target.h" 33 34 35 /* Print function pointer with inferior address ADDRESS onto stdio 36 stream STREAM. */ 37 38 static void 39 print_function_pointer_address (struct gdbarch *gdbarch, CORE_ADDR address, 40 struct ui_file *stream, int addressprint) 41 { 42 CORE_ADDR func_addr = gdbarch_convert_from_func_ptr_addr (gdbarch, address, 43 ¤t_target); 44 45 /* If the function pointer is represented by a description, print the 46 address of the description. */ 47 if (addressprint && func_addr != address) 48 { 49 fputs_filtered ("@", stream); 50 fputs_filtered (paddress (gdbarch, address), stream); 51 fputs_filtered (": ", stream); 52 } 53 print_address_demangle (gdbarch, func_addr, stream, demangle); 54 } 55 56 57 /* A helper for textual_element_type. This checks the name of the 58 typedef. This is bogus but it isn't apparent that the compiler 59 provides us the help we may need. */ 60 61 static int 62 textual_name (const char *name) 63 { 64 return (!strcmp (name, "wchar_t") 65 || !strcmp (name, "char16_t") 66 || !strcmp (name, "char32_t")); 67 } 68 69 /* Apply a heuristic to decide whether an array of TYPE or a pointer 70 to TYPE should be printed as a textual string. Return non-zero if 71 it should, or zero if it should be treated as an array of integers 72 or pointer to integers. FORMAT is the current format letter, 73 or 0 if none. 74 75 We guess that "char" is a character. Explicitly signed and 76 unsigned character types are also characters. Integer data from 77 vector types is not. The user can override this by using the /s 78 format letter. */ 79 80 static int 81 textual_element_type (struct type *type, char format) 82 { 83 struct type *true_type, *iter_type; 84 85 if (format != 0 && format != 's') 86 return 0; 87 88 /* We also rely on this for its side effect of setting up all the 89 typedef pointers. */ 90 true_type = check_typedef (type); 91 92 /* TYPE_CODE_CHAR is always textual. */ 93 if (TYPE_CODE (true_type) == TYPE_CODE_CHAR) 94 return 1; 95 96 /* Any other character-like types must be integral. */ 97 if (TYPE_CODE (true_type) != TYPE_CODE_INT) 98 return 0; 99 100 /* We peel typedefs one by one, looking for a match. */ 101 iter_type = type; 102 while (iter_type) 103 { 104 /* Check the name of the type. */ 105 if (TYPE_NAME (iter_type) && textual_name (TYPE_NAME (iter_type))) 106 return 1; 107 108 if (TYPE_CODE (iter_type) != TYPE_CODE_TYPEDEF) 109 break; 110 111 /* Peel a single typedef. If the typedef doesn't have a target 112 type, we use check_typedef and hope the result is ok -- it 113 might be for C++, where wchar_t is a built-in type. */ 114 if (TYPE_TARGET_TYPE (iter_type)) 115 iter_type = TYPE_TARGET_TYPE (iter_type); 116 else 117 iter_type = check_typedef (iter_type); 118 } 119 120 if (format == 's') 121 { 122 /* Print this as a string if we can manage it. For now, no 123 wide character support. */ 124 if (TYPE_CODE (true_type) == TYPE_CODE_INT 125 && TYPE_LENGTH (true_type) == 1) 126 return 1; 127 } 128 else 129 { 130 /* If a one-byte TYPE_CODE_INT is missing the not-a-character 131 flag, then we treat it as text; otherwise, we assume it's 132 being used as data. */ 133 if (TYPE_CODE (true_type) == TYPE_CODE_INT 134 && TYPE_LENGTH (true_type) == 1 135 && !TYPE_NOTTEXT (true_type)) 136 return 1; 137 } 138 139 return 0; 140 } 141 142 143 /* Print data of type TYPE located at VALADDR (within GDB), which came from 144 the inferior at address ADDRESS, onto stdio stream STREAM according to 145 OPTIONS. The data at VALADDR is in target byte order. 146 147 If the data are a string pointer, returns the number of string characters 148 printed. */ 149 150 int 151 c_val_print (struct type *type, const gdb_byte *valaddr, int embedded_offset, 152 CORE_ADDR address, struct ui_file *stream, int recurse, 153 const struct value_print_options *options) 154 { 155 struct gdbarch *gdbarch = get_type_arch (type); 156 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); 157 unsigned int i = 0; /* Number of characters printed */ 158 unsigned len; 159 struct type *elttype, *unresolved_elttype; 160 struct type *unresolved_type = type; 161 unsigned eltlen; 162 LONGEST val; 163 CORE_ADDR addr; 164 165 CHECK_TYPEDEF (type); 166 switch (TYPE_CODE (type)) 167 { 168 case TYPE_CODE_ARRAY: 169 unresolved_elttype = TYPE_TARGET_TYPE (type); 170 elttype = check_typedef (unresolved_elttype); 171 if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (unresolved_elttype) > 0) 172 { 173 eltlen = TYPE_LENGTH (elttype); 174 len = TYPE_LENGTH (type) / eltlen; 175 if (options->prettyprint_arrays) 176 { 177 print_spaces_filtered (2 + 2 * recurse, stream); 178 } 179 180 /* Print arrays of textual chars with a string syntax. */ 181 if (textual_element_type (unresolved_elttype, options->format)) 182 { 183 /* If requested, look for the first null char and only print 184 elements up to it. */ 185 if (options->stop_print_at_null) 186 { 187 unsigned int temp_len; 188 189 for (temp_len = 0; 190 (temp_len < len 191 && temp_len < options->print_max 192 && extract_unsigned_integer (valaddr + embedded_offset 193 + temp_len * eltlen, 194 eltlen, byte_order) == 0); 195 ++temp_len) 196 ; 197 len = temp_len; 198 } 199 200 LA_PRINT_STRING (stream, unresolved_elttype, 201 valaddr + embedded_offset, len, 0, options); 202 i = len; 203 } 204 else 205 { 206 fprintf_filtered (stream, "{"); 207 /* If this is a virtual function table, print the 0th 208 entry specially, and the rest of the members normally. */ 209 if (cp_is_vtbl_ptr_type (elttype)) 210 { 211 i = 1; 212 fprintf_filtered (stream, _("%d vtable entries"), len - 1); 213 } 214 else 215 { 216 i = 0; 217 } 218 val_print_array_elements (type, valaddr + embedded_offset, address, stream, 219 recurse, options, i); 220 fprintf_filtered (stream, "}"); 221 } 222 break; 223 } 224 /* Array of unspecified length: treat like pointer to first elt. */ 225 addr = address; 226 goto print_unpacked_pointer; 227 228 case TYPE_CODE_MEMBERPTR: 229 if (options->format) 230 { 231 print_scalar_formatted (valaddr + embedded_offset, type, 232 options, 0, stream); 233 break; 234 } 235 cp_print_class_member (valaddr + embedded_offset, type, stream, "&"); 236 break; 237 238 case TYPE_CODE_METHODPTR: 239 cplus_print_method_ptr (valaddr + embedded_offset, type, stream); 240 break; 241 242 case TYPE_CODE_PTR: 243 if (options->format && options->format != 's') 244 { 245 print_scalar_formatted (valaddr + embedded_offset, type, 246 options, 0, stream); 247 break; 248 } 249 if (options->vtblprint && cp_is_vtbl_ptr_type (type)) 250 { 251 /* Print the unmangled name if desired. */ 252 /* Print vtable entry - we only get here if we ARE using 253 -fvtable_thunks. (Otherwise, look under TYPE_CODE_STRUCT.) */ 254 CORE_ADDR addr 255 = extract_typed_address (valaddr + embedded_offset, type); 256 print_function_pointer_address (gdbarch, addr, stream, 257 options->addressprint); 258 break; 259 } 260 unresolved_elttype = TYPE_TARGET_TYPE (type); 261 elttype = check_typedef (unresolved_elttype); 262 { 263 addr = unpack_pointer (type, valaddr + embedded_offset); 264 print_unpacked_pointer: 265 266 if (TYPE_CODE (elttype) == TYPE_CODE_FUNC) 267 { 268 /* Try to print what function it points to. */ 269 print_function_pointer_address (gdbarch, addr, stream, 270 options->addressprint); 271 /* Return value is irrelevant except for string pointers. */ 272 return (0); 273 } 274 275 if (options->addressprint) 276 fputs_filtered (paddress (gdbarch, addr), stream); 277 278 /* For a pointer to a textual type, also print the string 279 pointed to, unless pointer is null. */ 280 281 if (textual_element_type (unresolved_elttype, options->format) 282 && addr != 0) 283 { 284 i = val_print_string (unresolved_elttype, addr, -1, stream, 285 options); 286 } 287 else if (cp_is_vtbl_member (type)) 288 { 289 /* print vtbl's nicely */ 290 CORE_ADDR vt_address = unpack_pointer (type, valaddr + embedded_offset); 291 292 struct minimal_symbol *msymbol = 293 lookup_minimal_symbol_by_pc (vt_address); 294 if ((msymbol != NULL) && 295 (vt_address == SYMBOL_VALUE_ADDRESS (msymbol))) 296 { 297 fputs_filtered (" <", stream); 298 fputs_filtered (SYMBOL_PRINT_NAME (msymbol), stream); 299 fputs_filtered (">", stream); 300 } 301 if (vt_address && options->vtblprint) 302 { 303 struct value *vt_val; 304 struct symbol *wsym = (struct symbol *) NULL; 305 struct type *wtype; 306 struct block *block = (struct block *) NULL; 307 int is_this_fld; 308 309 if (msymbol != NULL) 310 wsym = lookup_symbol (SYMBOL_LINKAGE_NAME (msymbol), block, 311 VAR_DOMAIN, &is_this_fld); 312 313 if (wsym) 314 { 315 wtype = SYMBOL_TYPE (wsym); 316 } 317 else 318 { 319 wtype = unresolved_elttype; 320 } 321 vt_val = value_at (wtype, vt_address); 322 common_val_print (vt_val, stream, recurse + 1, options, 323 current_language); 324 if (options->pretty) 325 { 326 fprintf_filtered (stream, "\n"); 327 print_spaces_filtered (2 + 2 * recurse, stream); 328 } 329 } 330 } 331 332 /* Return number of characters printed, including the terminating 333 '\0' if we reached the end. val_print_string takes care including 334 the terminating '\0' if necessary. */ 335 return i; 336 } 337 break; 338 339 case TYPE_CODE_REF: 340 elttype = check_typedef (TYPE_TARGET_TYPE (type)); 341 if (options->addressprint) 342 { 343 CORE_ADDR addr 344 = extract_typed_address (valaddr + embedded_offset, type); 345 fprintf_filtered (stream, "@"); 346 fputs_filtered (paddress (gdbarch, addr), stream); 347 if (options->deref_ref) 348 fputs_filtered (": ", stream); 349 } 350 /* De-reference the reference. */ 351 if (options->deref_ref) 352 { 353 if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF) 354 { 355 struct value *deref_val = 356 value_at 357 (TYPE_TARGET_TYPE (type), 358 unpack_pointer (type, valaddr + embedded_offset)); 359 common_val_print (deref_val, stream, recurse, options, 360 current_language); 361 } 362 else 363 fputs_filtered ("???", stream); 364 } 365 break; 366 367 case TYPE_CODE_UNION: 368 if (recurse && !options->unionprint) 369 { 370 fprintf_filtered (stream, "{...}"); 371 break; 372 } 373 /* Fall through. */ 374 case TYPE_CODE_STRUCT: 375 /*FIXME: Abstract this away */ 376 if (options->vtblprint && cp_is_vtbl_ptr_type (type)) 377 { 378 /* Print the unmangled name if desired. */ 379 /* Print vtable entry - we only get here if NOT using 380 -fvtable_thunks. (Otherwise, look under TYPE_CODE_PTR.) */ 381 int offset = (embedded_offset + 382 TYPE_FIELD_BITPOS (type, VTBL_FNADDR_OFFSET) / 8); 383 struct type *field_type = TYPE_FIELD_TYPE (type, VTBL_FNADDR_OFFSET); 384 CORE_ADDR addr 385 = extract_typed_address (valaddr + offset, field_type); 386 387 print_function_pointer_address (gdbarch, addr, stream, 388 options->addressprint); 389 } 390 else 391 cp_print_value_fields (type, type, valaddr, embedded_offset, address, stream, 392 recurse, options, NULL, 0); 393 break; 394 395 case TYPE_CODE_ENUM: 396 if (options->format) 397 { 398 print_scalar_formatted (valaddr + embedded_offset, type, 399 options, 0, stream); 400 break; 401 } 402 len = TYPE_NFIELDS (type); 403 val = unpack_long (type, valaddr + embedded_offset); 404 for (i = 0; i < len; i++) 405 { 406 QUIT; 407 if (val == TYPE_FIELD_BITPOS (type, i)) 408 { 409 break; 410 } 411 } 412 if (i < len) 413 { 414 fputs_filtered (TYPE_FIELD_NAME (type, i), stream); 415 } 416 else 417 { 418 print_longest (stream, 'd', 0, val); 419 } 420 break; 421 422 case TYPE_CODE_FLAGS: 423 if (options->format) 424 print_scalar_formatted (valaddr + embedded_offset, type, 425 options, 0, stream); 426 else 427 val_print_type_code_flags (type, valaddr + embedded_offset, stream); 428 break; 429 430 case TYPE_CODE_FUNC: 431 case TYPE_CODE_METHOD: 432 if (options->format) 433 { 434 print_scalar_formatted (valaddr + embedded_offset, type, 435 options, 0, stream); 436 break; 437 } 438 /* FIXME, we should consider, at least for ANSI C language, eliminating 439 the distinction made between FUNCs and POINTERs to FUNCs. */ 440 fprintf_filtered (stream, "{"); 441 type_print (type, "", stream, -1); 442 fprintf_filtered (stream, "} "); 443 /* Try to print what function it points to, and its address. */ 444 print_address_demangle (gdbarch, address, stream, demangle); 445 break; 446 447 case TYPE_CODE_BOOL: 448 if (options->format || options->output_format) 449 { 450 struct value_print_options opts = *options; 451 opts.format = (options->format ? options->format 452 : options->output_format); 453 print_scalar_formatted (valaddr + embedded_offset, type, 454 &opts, 0, stream); 455 } 456 else 457 { 458 val = unpack_long (type, valaddr + embedded_offset); 459 if (val == 0) 460 fputs_filtered ("false", stream); 461 else if (val == 1) 462 fputs_filtered ("true", stream); 463 else 464 print_longest (stream, 'd', 0, val); 465 } 466 break; 467 468 case TYPE_CODE_RANGE: 469 /* FIXME: create_range_type does not set the unsigned bit in a 470 range type (I think it probably should copy it from the target 471 type), so we won't print values which are too large to 472 fit in a signed integer correctly. */ 473 /* FIXME: Doesn't handle ranges of enums correctly. (Can't just 474 print with the target type, though, because the size of our type 475 and the target type might differ). */ 476 /* FALLTHROUGH */ 477 478 case TYPE_CODE_INT: 479 if (options->format || options->output_format) 480 { 481 struct value_print_options opts = *options; 482 opts.format = (options->format ? options->format 483 : options->output_format); 484 print_scalar_formatted (valaddr + embedded_offset, type, 485 &opts, 0, stream); 486 } 487 else 488 { 489 val_print_type_code_int (type, valaddr + embedded_offset, stream); 490 /* C and C++ has no single byte int type, char is used instead. 491 Since we don't know whether the value is really intended to 492 be used as an integer or a character, print the character 493 equivalent as well. */ 494 if (textual_element_type (unresolved_type, options->format)) 495 { 496 fputs_filtered (" ", stream); 497 LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr + embedded_offset), 498 unresolved_type, stream); 499 } 500 } 501 break; 502 503 case TYPE_CODE_CHAR: 504 if (options->format || options->output_format) 505 { 506 struct value_print_options opts = *options; 507 opts.format = (options->format ? options->format 508 : options->output_format); 509 print_scalar_formatted (valaddr + embedded_offset, type, 510 &opts, 0, stream); 511 } 512 else 513 { 514 val = unpack_long (type, valaddr + embedded_offset); 515 if (TYPE_UNSIGNED (type)) 516 fprintf_filtered (stream, "%u", (unsigned int) val); 517 else 518 fprintf_filtered (stream, "%d", (int) val); 519 fputs_filtered (" ", stream); 520 LA_PRINT_CHAR ((unsigned char) val, unresolved_type, stream); 521 } 522 break; 523 524 case TYPE_CODE_FLT: 525 if (options->format) 526 { 527 print_scalar_formatted (valaddr + embedded_offset, type, 528 options, 0, stream); 529 } 530 else 531 { 532 print_floating (valaddr + embedded_offset, type, stream); 533 } 534 break; 535 536 case TYPE_CODE_DECFLOAT: 537 if (options->format) 538 print_scalar_formatted (valaddr + embedded_offset, type, 539 options, 0, stream); 540 else 541 print_decimal_floating (valaddr + embedded_offset, type, stream); 542 break; 543 544 case TYPE_CODE_VOID: 545 fprintf_filtered (stream, "void"); 546 break; 547 548 case TYPE_CODE_ERROR: 549 fprintf_filtered (stream, _("<error type>")); 550 break; 551 552 case TYPE_CODE_UNDEF: 553 /* This happens (without TYPE_FLAG_STUB set) on systems which don't use 554 dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar" 555 and no complete type for struct foo in that file. */ 556 fprintf_filtered (stream, _("<incomplete type>")); 557 break; 558 559 case TYPE_CODE_COMPLEX: 560 if (options->format) 561 print_scalar_formatted (valaddr + embedded_offset, 562 TYPE_TARGET_TYPE (type), 563 options, 0, stream); 564 else 565 print_floating (valaddr + embedded_offset, TYPE_TARGET_TYPE (type), 566 stream); 567 fprintf_filtered (stream, " + "); 568 if (options->format) 569 print_scalar_formatted (valaddr + embedded_offset 570 + TYPE_LENGTH (TYPE_TARGET_TYPE (type)), 571 TYPE_TARGET_TYPE (type), 572 options, 0, stream); 573 else 574 print_floating (valaddr + embedded_offset 575 + TYPE_LENGTH (TYPE_TARGET_TYPE (type)), 576 TYPE_TARGET_TYPE (type), 577 stream); 578 fprintf_filtered (stream, " * I"); 579 break; 580 581 default: 582 error (_("Invalid C/C++ type code %d in symbol table."), TYPE_CODE (type)); 583 } 584 gdb_flush (stream); 585 return (0); 586 } 587 588 int 589 c_value_print (struct value *val, struct ui_file *stream, 590 const struct value_print_options *options) 591 { 592 struct type *type, *real_type, *val_type; 593 int full, top, using_enc; 594 struct value_print_options opts = *options; 595 596 opts.deref_ref = 1; 597 598 /* If it is a pointer, indicate what it points to. 599 600 Print type also if it is a reference. 601 602 C++: if it is a member pointer, we will take care 603 of that when we print it. */ 604 605 /* Preserve the original type before stripping typedefs. We prefer 606 to pass down the original type when possible, but for local 607 checks it is better to look past the typedefs. */ 608 val_type = value_type (val); 609 type = check_typedef (val_type); 610 611 if (TYPE_CODE (type) == TYPE_CODE_PTR 612 || TYPE_CODE (type) == TYPE_CODE_REF) 613 { 614 /* Hack: remove (char *) for char strings. Their 615 type is indicated by the quoted string anyway. 616 (Don't use textual_element_type here; quoted strings 617 are always exactly (char *), (wchar_t *), or the like. */ 618 if (TYPE_CODE (val_type) == TYPE_CODE_PTR 619 && TYPE_NAME (val_type) == NULL 620 && TYPE_NAME (TYPE_TARGET_TYPE (val_type)) != NULL 621 && (strcmp (TYPE_NAME (TYPE_TARGET_TYPE (val_type)), "char") == 0 622 || textual_name (TYPE_NAME (TYPE_TARGET_TYPE (val_type))))) 623 { 624 /* Print nothing */ 625 } 626 else if (options->objectprint 627 && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CLASS)) 628 { 629 630 if (TYPE_CODE(type) == TYPE_CODE_REF) 631 { 632 /* Copy value, change to pointer, so we don't get an 633 * error about a non-pointer type in value_rtti_target_type 634 */ 635 struct value *temparg; 636 temparg=value_copy(val); 637 deprecated_set_value_type (temparg, lookup_pointer_type (TYPE_TARGET_TYPE(type))); 638 val=temparg; 639 } 640 /* Pointer to class, check real type of object */ 641 fprintf_filtered (stream, "("); 642 real_type = value_rtti_target_type (val, &full, &top, &using_enc); 643 if (real_type) 644 { 645 /* RTTI entry found */ 646 if (TYPE_CODE (type) == TYPE_CODE_PTR) 647 { 648 /* create a pointer type pointing to the real type */ 649 type = lookup_pointer_type (real_type); 650 } 651 else 652 { 653 /* create a reference type referencing the real type */ 654 type = lookup_reference_type (real_type); 655 } 656 /* JYG: Need to adjust pointer value. */ 657 /* NOTE: cagney/2005-01-02: THIS IS BOGUS. */ 658 value_contents_writeable (val)[0] -= top; 659 660 /* Note: When we look up RTTI entries, we don't get any 661 information on const or volatile attributes */ 662 } 663 type_print (type, "", stream, -1); 664 fprintf_filtered (stream, ") "); 665 val_type = type; 666 } 667 else 668 { 669 /* normal case */ 670 fprintf_filtered (stream, "("); 671 type_print (value_type (val), "", stream, -1); 672 fprintf_filtered (stream, ") "); 673 } 674 } 675 676 if (!value_initialized (val)) 677 fprintf_filtered (stream, " [uninitialized] "); 678 679 if (options->objectprint && (TYPE_CODE (type) == TYPE_CODE_CLASS)) 680 { 681 /* Attempt to determine real type of object */ 682 real_type = value_rtti_type (val, &full, &top, &using_enc); 683 if (real_type) 684 { 685 /* We have RTTI information, so use it */ 686 val = value_full_object (val, real_type, full, top, using_enc); 687 fprintf_filtered (stream, "(%s%s) ", 688 TYPE_NAME (real_type), 689 full ? "" : _(" [incomplete object]")); 690 /* Print out object: enclosing type is same as real_type if full */ 691 return val_print (value_enclosing_type (val), 692 value_contents_all (val), 0, 693 value_address (val), stream, 0, 694 &opts, current_language); 695 /* Note: When we look up RTTI entries, we don't get any information on 696 const or volatile attributes */ 697 } 698 else if (type != check_typedef (value_enclosing_type (val))) 699 { 700 /* No RTTI information, so let's do our best */ 701 fprintf_filtered (stream, "(%s ?) ", 702 TYPE_NAME (value_enclosing_type (val))); 703 return val_print (value_enclosing_type (val), 704 value_contents_all (val), 0, 705 value_address (val), stream, 0, 706 &opts, current_language); 707 } 708 /* Otherwise, we end up at the return outside this "if" */ 709 } 710 711 return val_print (val_type, value_contents_all (val), 712 value_embedded_offset (val), 713 value_address (val), 714 stream, 0, &opts, current_language); 715 } 716