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 2000, 2001, 2002, 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_obstack.h" 24 #include "symtab.h" 25 #include "gdbtypes.h" 26 #include "expression.h" 27 #include "value.h" 28 #include "command.h" 29 #include "gdbcmd.h" 30 #include "demangle.h" 31 #include "annotate.h" 32 #include "gdb_string.h" 33 #include "c-lang.h" 34 #include "target.h" 35 #include "cp-abi.h" 36 #include "valprint.h" 37 #include "cp-support.h" 38 #include "language.h" 39 #include "python/python.h" 40 41 /* Controls printing of vtbl's */ 42 static void 43 show_vtblprint (struct ui_file *file, int from_tty, 44 struct cmd_list_element *c, const char *value) 45 { 46 fprintf_filtered (file, _("\ 47 Printing of C++ virtual function tables is %s.\n"), 48 value); 49 } 50 51 /* Controls looking up an object's derived type using what we find in 52 its vtables. */ 53 static void 54 show_objectprint (struct ui_file *file, int from_tty, 55 struct cmd_list_element *c, 56 const char *value) 57 { 58 fprintf_filtered (file, _("\ 59 Printing of object's derived type based on vtable info is %s.\n"), 60 value); 61 } 62 63 static void 64 show_static_field_print (struct ui_file *file, int from_tty, 65 struct cmd_list_element *c, const char *value) 66 { 67 fprintf_filtered (file, _("Printing of C++ static members is %s.\n"), 68 value); 69 } 70 71 72 static struct obstack dont_print_vb_obstack; 73 static struct obstack dont_print_statmem_obstack; 74 75 extern void _initialize_cp_valprint (void); 76 77 static void cp_print_static_field (struct type *, struct value *, 78 struct ui_file *, int, 79 const struct value_print_options *); 80 81 static void cp_print_value (struct type *, struct type *, const gdb_byte *, 82 int, CORE_ADDR, struct ui_file *, int, 83 const struct value_print_options *, struct type **); 84 85 86 /* GCC versions after 2.4.5 use this. */ 87 const char vtbl_ptr_name[] = "__vtbl_ptr_type"; 88 89 /* Return truth value for assertion that TYPE is of the type 90 "pointer to virtual function". */ 91 92 int 93 cp_is_vtbl_ptr_type (struct type *type) 94 { 95 char *typename = type_name_no_tag (type); 96 97 return (typename != NULL && !strcmp (typename, vtbl_ptr_name)); 98 } 99 100 /* Return truth value for the assertion that TYPE is of the type 101 "pointer to virtual function table". */ 102 103 int 104 cp_is_vtbl_member (struct type *type) 105 { 106 /* With older versions of g++, the vtbl field pointed to an array 107 of structures. Nowadays it points directly to the structure. */ 108 if (TYPE_CODE (type) == TYPE_CODE_PTR) 109 { 110 type = TYPE_TARGET_TYPE (type); 111 if (TYPE_CODE (type) == TYPE_CODE_ARRAY) 112 { 113 type = TYPE_TARGET_TYPE (type); 114 if (TYPE_CODE (type) == TYPE_CODE_STRUCT /* if not using thunks */ 115 || TYPE_CODE (type) == TYPE_CODE_PTR) /* if using thunks */ 116 { 117 /* Virtual functions tables are full of pointers 118 to virtual functions. */ 119 return cp_is_vtbl_ptr_type (type); 120 } 121 } 122 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT) /* if not using thunks */ 123 { 124 return cp_is_vtbl_ptr_type (type); 125 } 126 else if (TYPE_CODE (type) == TYPE_CODE_PTR) /* if using thunks */ 127 { 128 /* The type name of the thunk pointer is NULL when using dwarf2. 129 We could test for a pointer to a function, but there is 130 no type info for the virtual table either, so it wont help. */ 131 return cp_is_vtbl_ptr_type (type); 132 } 133 } 134 return 0; 135 } 136 137 /* Mutually recursive subroutines of cp_print_value and c_val_print to 138 print out a structure's fields: cp_print_value_fields and cp_print_value. 139 140 TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and OPTIONS have the 141 same meanings as in cp_print_value and c_val_print. 142 143 2nd argument REAL_TYPE is used to carry over the type of the derived 144 class across the recursion to base classes. 145 146 DONT_PRINT is an array of baseclass types that we 147 should not print, or zero if called from top level. */ 148 149 void 150 cp_print_value_fields (struct type *type, struct type *real_type, 151 const gdb_byte *valaddr, int offset, CORE_ADDR address, 152 struct ui_file *stream, int recurse, 153 const struct value_print_options *options, 154 struct type **dont_print_vb, int dont_print_statmem) 155 { 156 int i, len, n_baseclasses; 157 char *last_dont_print = obstack_next_free (&dont_print_statmem_obstack); 158 int fields_seen = 0; 159 160 CHECK_TYPEDEF (type); 161 162 fprintf_filtered (stream, "{"); 163 len = TYPE_NFIELDS (type); 164 n_baseclasses = TYPE_N_BASECLASSES (type); 165 166 /* First, print out baseclasses such that we don't print 167 duplicates of virtual baseclasses. */ 168 169 if (n_baseclasses > 0) 170 cp_print_value (type, real_type, valaddr, offset, address, stream, 171 recurse + 1, options, dont_print_vb); 172 173 /* Second, print out data fields */ 174 175 /* If there are no data fields, skip this part */ 176 if (len == n_baseclasses || !len) 177 fprintf_filtered (stream, "<No data fields>"); 178 else 179 { 180 struct obstack tmp_obstack = dont_print_statmem_obstack; 181 182 if (dont_print_statmem == 0) 183 { 184 /* If we're at top level, carve out a completely fresh 185 chunk of the obstack and use that until this particular 186 invocation returns. */ 187 obstack_finish (&dont_print_statmem_obstack); 188 } 189 190 for (i = n_baseclasses; i < len; i++) 191 { 192 /* If requested, skip printing of static fields. */ 193 if (!options->static_field_print 194 && field_is_static (&TYPE_FIELD (type, i))) 195 continue; 196 197 if (fields_seen) 198 fprintf_filtered (stream, ", "); 199 else if (n_baseclasses > 0) 200 { 201 if (options->pretty) 202 { 203 fprintf_filtered (stream, "\n"); 204 print_spaces_filtered (2 + 2 * recurse, stream); 205 fputs_filtered ("members of ", stream); 206 fputs_filtered (type_name_no_tag (type), stream); 207 fputs_filtered (": ", stream); 208 } 209 } 210 fields_seen = 1; 211 212 if (options->pretty) 213 { 214 fprintf_filtered (stream, "\n"); 215 print_spaces_filtered (2 + 2 * recurse, stream); 216 } 217 else 218 { 219 wrap_here (n_spaces (2 + 2 * recurse)); 220 } 221 if (options->inspect_it) 222 { 223 if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR) 224 fputs_filtered ("\"( ptr \"", stream); 225 else 226 fputs_filtered ("\"( nodef \"", stream); 227 if (field_is_static (&TYPE_FIELD (type, i))) 228 fputs_filtered ("static ", stream); 229 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i), 230 current_language->la_language, 231 DMGL_PARAMS | DMGL_ANSI); 232 fputs_filtered ("\" \"", stream); 233 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i), 234 current_language->la_language, 235 DMGL_PARAMS | DMGL_ANSI); 236 fputs_filtered ("\") \"", stream); 237 } 238 else 239 { 240 annotate_field_begin (TYPE_FIELD_TYPE (type, i)); 241 242 if (field_is_static (&TYPE_FIELD (type, i))) 243 fputs_filtered ("static ", stream); 244 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i), 245 current_language->la_language, 246 DMGL_PARAMS | DMGL_ANSI); 247 annotate_field_name_end (); 248 /* do not print leading '=' in case of anonymous unions */ 249 if (strcmp (TYPE_FIELD_NAME (type, i), "")) 250 fputs_filtered (" = ", stream); 251 annotate_field_value (); 252 } 253 254 if (!field_is_static (&TYPE_FIELD (type, i)) 255 && TYPE_FIELD_PACKED (type, i)) 256 { 257 struct value *v; 258 259 /* Bitfields require special handling, especially due to byte 260 order problems. */ 261 if (TYPE_FIELD_IGNORE (type, i)) 262 { 263 fputs_filtered ("<optimized out or zero length>", stream); 264 } 265 else 266 { 267 struct value_print_options opts = *options; 268 opts.deref_ref = 0; 269 v = value_from_longest 270 (TYPE_FIELD_TYPE (type, i), 271 unpack_field_as_long (type, valaddr + offset, i)); 272 273 common_val_print (v, stream, recurse + 1, &opts, 274 current_language); 275 } 276 } 277 else 278 { 279 if (TYPE_FIELD_IGNORE (type, i)) 280 { 281 fputs_filtered ("<optimized out or zero length>", stream); 282 } 283 else if (field_is_static (&TYPE_FIELD (type, i))) 284 { 285 struct value *v = value_static_field (type, i); 286 if (v == NULL) 287 fputs_filtered ("<optimized out>", stream); 288 else 289 cp_print_static_field (TYPE_FIELD_TYPE (type, i), v, 290 stream, recurse + 1, options); 291 } 292 else 293 { 294 struct value_print_options opts = *options; 295 opts.deref_ref = 0; 296 val_print (TYPE_FIELD_TYPE (type, i), 297 valaddr, offset + TYPE_FIELD_BITPOS (type, i) / 8, 298 address + TYPE_FIELD_BITPOS (type, i) / 8, 299 stream, recurse + 1, &opts, 300 current_language); 301 } 302 } 303 annotate_field_end (); 304 } 305 306 if (dont_print_statmem == 0) 307 { 308 /* Free the space used to deal with the printing 309 of the members from top level. */ 310 obstack_free (&dont_print_statmem_obstack, last_dont_print); 311 dont_print_statmem_obstack = tmp_obstack; 312 } 313 314 if (options->pretty) 315 { 316 fprintf_filtered (stream, "\n"); 317 print_spaces_filtered (2 * recurse, stream); 318 } 319 } /* if there are data fields */ 320 321 fprintf_filtered (stream, "}"); 322 } 323 324 /* Special val_print routine to avoid printing multiple copies of virtual 325 baseclasses. */ 326 327 static void 328 cp_print_value (struct type *type, struct type *real_type, 329 const gdb_byte *valaddr, int offset, CORE_ADDR address, 330 struct ui_file *stream, int recurse, 331 const struct value_print_options *options, 332 struct type **dont_print_vb) 333 { 334 struct type **last_dont_print 335 = (struct type **) obstack_next_free (&dont_print_vb_obstack); 336 struct obstack tmp_obstack = dont_print_vb_obstack; 337 int i, n_baseclasses = TYPE_N_BASECLASSES (type); 338 int thisoffset; 339 struct type *thistype; 340 341 if (dont_print_vb == 0) 342 { 343 /* If we're at top level, carve out a completely fresh 344 chunk of the obstack and use that until this particular 345 invocation returns. */ 346 /* Bump up the high-water mark. Now alpha is omega. */ 347 obstack_finish (&dont_print_vb_obstack); 348 } 349 350 for (i = 0; i < n_baseclasses; i++) 351 { 352 int boffset; 353 int skip; 354 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i)); 355 char *basename = TYPE_NAME (baseclass); 356 const gdb_byte *base_valaddr; 357 358 if (BASETYPE_VIA_VIRTUAL (type, i)) 359 { 360 struct type **first_dont_print 361 = (struct type **) obstack_base (&dont_print_vb_obstack); 362 363 int j = (struct type **) obstack_next_free (&dont_print_vb_obstack) 364 - first_dont_print; 365 366 while (--j >= 0) 367 if (baseclass == first_dont_print[j]) 368 goto flush_it; 369 370 obstack_ptr_grow (&dont_print_vb_obstack, baseclass); 371 } 372 373 thisoffset = offset; 374 thistype = real_type; 375 376 boffset = baseclass_offset (type, i, valaddr + offset, address); 377 skip = ((boffset == -1) || (boffset + offset) < 0) ? 1 : -1; 378 379 if (BASETYPE_VIA_VIRTUAL (type, i)) 380 { 381 /* The virtual base class pointer might have been 382 clobbered by the user program. Make sure that it 383 still points to a valid memory location. */ 384 385 if (boffset != -1 386 && ((boffset + offset) < 0 387 || (boffset + offset) >= TYPE_LENGTH (type))) 388 { 389 /* FIXME (alloca): unsafe if baseclass is really really large. */ 390 gdb_byte *buf = alloca (TYPE_LENGTH (baseclass)); 391 base_valaddr = buf; 392 if (target_read_memory (address + boffset, buf, 393 TYPE_LENGTH (baseclass)) != 0) 394 skip = 1; 395 address = address + boffset; 396 thisoffset = 0; 397 boffset = 0; 398 thistype = baseclass; 399 } 400 else 401 base_valaddr = valaddr; 402 } 403 else 404 base_valaddr = valaddr; 405 406 /* now do the printing */ 407 if (options->pretty) 408 { 409 fprintf_filtered (stream, "\n"); 410 print_spaces_filtered (2 * recurse, stream); 411 } 412 fputs_filtered ("<", stream); 413 /* Not sure what the best notation is in the case where there is no 414 baseclass name. */ 415 fputs_filtered (basename ? basename : "", stream); 416 fputs_filtered ("> = ", stream); 417 418 419 if (skip >= 1) 420 fprintf_filtered (stream, "<invalid address>"); 421 else 422 { 423 int result = 0; 424 425 /* Attempt to run the Python pretty-printers on the 426 baseclass if possible. */ 427 if (!options->raw) 428 result = apply_val_pretty_printer (baseclass, base_valaddr, 429 thisoffset + boffset, 430 address + boffset, 431 stream, recurse, 432 options, 433 current_language); 434 435 if (!result) 436 cp_print_value_fields (baseclass, thistype, base_valaddr, 437 thisoffset + boffset, address + boffset, 438 stream, recurse, options, 439 ((struct type **) 440 obstack_base (&dont_print_vb_obstack)), 441 0); 442 } 443 fputs_filtered (", ", stream); 444 445 flush_it: 446 ; 447 } 448 449 if (dont_print_vb == 0) 450 { 451 /* Free the space used to deal with the printing 452 of this type from top level. */ 453 obstack_free (&dont_print_vb_obstack, last_dont_print); 454 /* Reset watermark so that we can continue protecting 455 ourselves from whatever we were protecting ourselves. */ 456 dont_print_vb_obstack = tmp_obstack; 457 } 458 } 459 460 /* Print value of a static member. 461 To avoid infinite recursion when printing a class that contains 462 a static instance of the class, we keep the addresses of all printed 463 static member classes in an obstack and refuse to print them more 464 than once. 465 466 VAL contains the value to print, TYPE, STREAM, RECURSE, and OPTIONS 467 have the same meanings as in c_val_print. */ 468 469 static void 470 cp_print_static_field (struct type *type, 471 struct value *val, 472 struct ui_file *stream, 473 int recurse, 474 const struct value_print_options *options) 475 { 476 struct value_print_options opts; 477 if (TYPE_CODE (type) == TYPE_CODE_STRUCT) 478 { 479 CORE_ADDR *first_dont_print; 480 CORE_ADDR addr; 481 int i; 482 483 first_dont_print 484 = (CORE_ADDR *) obstack_base (&dont_print_statmem_obstack); 485 i = (CORE_ADDR *) obstack_next_free (&dont_print_statmem_obstack) 486 - first_dont_print; 487 488 while (--i >= 0) 489 { 490 if (value_address (val) == first_dont_print[i]) 491 { 492 fputs_filtered ("<same as static member of an already" 493 " seen type>", 494 stream); 495 return; 496 } 497 } 498 499 addr = value_address (val); 500 obstack_grow (&dont_print_statmem_obstack, (char *) &addr, 501 sizeof (CORE_ADDR)); 502 503 CHECK_TYPEDEF (type); 504 cp_print_value_fields (type, type, value_contents_all (val), 505 value_embedded_offset (val), addr, 506 stream, recurse, options, NULL, 1); 507 return; 508 } 509 510 opts = *options; 511 opts.deref_ref = 0; 512 val_print (type, value_contents_all (val), 513 value_embedded_offset (val), value_address (val), 514 stream, recurse, &opts, current_language); 515 } 516 517 518 /* Find the field in *DOMAIN, or its non-virtual base classes, with bit offset 519 OFFSET. Set *DOMAIN to the containing type and *FIELDNO to the containing 520 field number. If OFFSET is not exactly at the start of some field, set 521 *DOMAIN to NULL. */ 522 523 static void 524 cp_find_class_member (struct type **domain_p, int *fieldno, 525 LONGEST offset) 526 { 527 struct type *domain; 528 unsigned int i; 529 unsigned len; 530 531 *domain_p = check_typedef (*domain_p); 532 domain = *domain_p; 533 len = TYPE_NFIELDS (domain); 534 535 for (i = TYPE_N_BASECLASSES (domain); i < len; i++) 536 { 537 LONGEST bitpos = TYPE_FIELD_BITPOS (domain, i); 538 539 QUIT; 540 if (offset == bitpos) 541 { 542 *fieldno = i; 543 return; 544 } 545 } 546 547 for (i = 0; i < TYPE_N_BASECLASSES (domain); i++) 548 { 549 LONGEST bitpos = TYPE_FIELD_BITPOS (domain, i); 550 LONGEST bitsize = 8 * TYPE_LENGTH (TYPE_FIELD_TYPE (domain, i)); 551 552 if (offset >= bitpos && offset < bitpos + bitsize) 553 { 554 *domain_p = TYPE_FIELD_TYPE (domain, i); 555 cp_find_class_member (domain_p, fieldno, offset - bitpos); 556 return; 557 } 558 } 559 560 *domain_p = NULL; 561 } 562 563 void 564 cp_print_class_member (const gdb_byte *valaddr, struct type *type, 565 struct ui_file *stream, char *prefix) 566 { 567 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type)); 568 569 /* VAL is a byte offset into the structure type DOMAIN. 570 Find the name of the field for that offset and 571 print it. */ 572 struct type *domain = TYPE_DOMAIN_TYPE (type); 573 LONGEST val; 574 unsigned int fieldno; 575 576 val = extract_signed_integer (valaddr, TYPE_LENGTH (type), byte_order); 577 578 /* Pointers to data members are usually byte offsets into an object. 579 Because a data member can have offset zero, and a NULL pointer to 580 member must be distinct from any valid non-NULL pointer to 581 member, either the value is biased or the NULL value has a 582 special representation; both are permitted by ISO C++. HP aCC 583 used a bias of 0x20000000; HP cfront used a bias of 1; g++ 3.x 584 and other compilers which use the Itanium ABI use -1 as the NULL 585 value. GDB only supports that last form; to add support for 586 another form, make this into a cp-abi hook. */ 587 588 if (val == -1) 589 { 590 fprintf_filtered (stream, "NULL"); 591 return; 592 } 593 594 cp_find_class_member (&domain, &fieldno, val << 3); 595 596 if (domain != NULL) 597 { 598 char *name; 599 fputs_filtered (prefix, stream); 600 name = type_name_no_tag (domain); 601 if (name) 602 fputs_filtered (name, stream); 603 else 604 c_type_print_base (domain, stream, 0, 0); 605 fprintf_filtered (stream, "::"); 606 fputs_filtered (TYPE_FIELD_NAME (domain, fieldno), stream); 607 } 608 else 609 fprintf_filtered (stream, "%ld", (long) val); 610 } 611 612 613 void 614 _initialize_cp_valprint (void) 615 { 616 add_setshow_boolean_cmd ("static-members", class_support, 617 &user_print_options.static_field_print, _("\ 618 Set printing of C++ static members."), _("\ 619 Show printing of C++ static members."), NULL, 620 NULL, 621 show_static_field_print, 622 &setprintlist, &showprintlist); 623 624 add_setshow_boolean_cmd ("vtbl", class_support, 625 &user_print_options.vtblprint, _("\ 626 Set printing of C++ virtual function tables."), _("\ 627 Show printing of C++ virtual function tables."), NULL, 628 NULL, 629 show_vtblprint, 630 &setprintlist, &showprintlist); 631 632 add_setshow_boolean_cmd ("object", class_support, 633 &user_print_options.objectprint, _("\ 634 Set printing of object's derived type based on vtable info."), _("\ 635 Show printing of object's derived type based on vtable info."), NULL, 636 NULL, 637 show_objectprint, 638 &setprintlist, &showprintlist); 639 640 obstack_begin (&dont_print_vb_obstack, 32 * sizeof (struct type *)); 641 obstack_specify_allocation (&dont_print_statmem_obstack, 642 32 * sizeof (CORE_ADDR), sizeof (CORE_ADDR), 643 xmalloc, xfree); 644 } 645