| 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 Free Software Foundation, Inc. |
| 5 | |
| 6 | This file is part of GDB. |
| 7 | |
| 8 | This program is free software; you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation; either version 3 of the License, or |
| 11 | (at your option) any later version. |
| 12 | |
| 13 | This program is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 20 | |
| 21 | #include "defs.h" |
| 22 | #include "gdb_obstack.h" |
| 23 | #include "symtab.h" |
| 24 | #include "gdbtypes.h" |
| 25 | #include "expression.h" |
| 26 | #include "value.h" |
| 27 | #include "command.h" |
| 28 | #include "gdbcmd.h" |
| 29 | #include "demangle.h" |
| 30 | #include "annotate.h" |
| 31 | #include "gdb_string.h" |
| 32 | #include "c-lang.h" |
| 33 | #include "target.h" |
| 34 | #include "cp-abi.h" |
| 35 | #include "valprint.h" |
| 36 | #include "cp-support.h" |
| 37 | #include "language.h" |
| 38 | |
| 39 | /* Controls printing of vtbl's */ |
| 40 | int vtblprint; |
| 41 | static void |
| 42 | show_vtblprint (struct ui_file *file, int from_tty, |
| 43 | struct cmd_list_element *c, const char *value) |
| 44 | { |
| 45 | fprintf_filtered (file, _("\ |
| 46 | Printing of C++ virtual function tables is %s.\n"), |
| 47 | value); |
| 48 | } |
| 49 | |
| 50 | /* Controls looking up an object's derived type using what we find in |
| 51 | its vtables. */ |
| 52 | int objectprint; |
| 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 | int static_field_print; /* Controls printing of static fields. */ |
| 64 | static void |
| 65 | show_static_field_print (struct ui_file *file, int from_tty, |
| 66 | struct cmd_list_element *c, const char *value) |
| 67 | { |
| 68 | fprintf_filtered (file, _("Printing of C++ static members is %s.\n"), |
| 69 | value); |
| 70 | } |
| 71 | |
| 72 | |
| 73 | static struct obstack dont_print_vb_obstack; |
| 74 | static struct obstack dont_print_statmem_obstack; |
| 75 | |
| 76 | extern void _initialize_cp_valprint (void); |
| 77 | |
| 78 | static void cp_print_static_field (struct type *, struct value *, |
| 79 | struct ui_file *, int, int, |
| 80 | enum val_prettyprint); |
| 81 | |
| 82 | static void cp_print_value (struct type *, struct type *, const gdb_byte *, |
| 83 | int, CORE_ADDR, struct ui_file *, int, int, |
| 84 | enum val_prettyprint, struct type **); |
| 85 | |
| 86 | static void cp_print_hpacc_virtual_table_entries (struct type *, int *, |
| 87 | struct value *, |
| 88 | struct ui_file *, int, |
| 89 | int, |
| 90 | enum val_prettyprint); |
| 91 | |
| 92 | |
| 93 | /* GCC versions after 2.4.5 use this. */ |
| 94 | const char vtbl_ptr_name[] = "__vtbl_ptr_type"; |
| 95 | |
| 96 | /* HP aCC uses different names. */ |
| 97 | const char hpacc_vtbl_ptr_name[] = "__vfp"; |
| 98 | const char hpacc_vtbl_ptr_type_name[] = "__vftyp"; |
| 99 | |
| 100 | /* Return truth value for assertion that TYPE is of the type |
| 101 | "pointer to virtual function". */ |
| 102 | |
| 103 | int |
| 104 | cp_is_vtbl_ptr_type (struct type *type) |
| 105 | { |
| 106 | char *typename = type_name_no_tag (type); |
| 107 | |
| 108 | return (typename != NULL && !strcmp (typename, vtbl_ptr_name)); |
| 109 | } |
| 110 | |
| 111 | /* Return truth value for the assertion that TYPE is of the type |
| 112 | "pointer to virtual function table". */ |
| 113 | |
| 114 | int |
| 115 | cp_is_vtbl_member (struct type *type) |
| 116 | { |
| 117 | /* With older versions of g++, the vtbl field pointed to an array |
| 118 | of structures. Nowadays it points directly to the structure. */ |
| 119 | if (TYPE_CODE (type) == TYPE_CODE_PTR) |
| 120 | { |
| 121 | type = TYPE_TARGET_TYPE (type); |
| 122 | if (TYPE_CODE (type) == TYPE_CODE_ARRAY) |
| 123 | { |
| 124 | type = TYPE_TARGET_TYPE (type); |
| 125 | if (TYPE_CODE (type) == TYPE_CODE_STRUCT /* if not using thunks */ |
| 126 | || TYPE_CODE (type) == TYPE_CODE_PTR) /* if using thunks */ |
| 127 | { |
| 128 | /* Virtual functions tables are full of pointers |
| 129 | to virtual functions. */ |
| 130 | return cp_is_vtbl_ptr_type (type); |
| 131 | } |
| 132 | } |
| 133 | else if (TYPE_CODE (type) == TYPE_CODE_STRUCT) /* if not using thunks */ |
| 134 | { |
| 135 | return cp_is_vtbl_ptr_type (type); |
| 136 | } |
| 137 | else if (TYPE_CODE (type) == TYPE_CODE_PTR) /* if using thunks */ |
| 138 | { |
| 139 | /* The type name of the thunk pointer is NULL when using dwarf2. |
| 140 | We could test for a pointer to a function, but there is |
| 141 | no type info for the virtual table either, so it wont help. */ |
| 142 | return cp_is_vtbl_ptr_type (type); |
| 143 | } |
| 144 | } |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | /* Mutually recursive subroutines of cp_print_value and c_val_print to |
| 149 | print out a structure's fields: cp_print_value_fields and cp_print_value. |
| 150 | |
| 151 | TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and PRETTY have the |
| 152 | same meanings as in cp_print_value and c_val_print. |
| 153 | |
| 154 | 2nd argument REAL_TYPE is used to carry over the type of the derived |
| 155 | class across the recursion to base classes. |
| 156 | |
| 157 | DONT_PRINT is an array of baseclass types that we |
| 158 | should not print, or zero if called from top level. */ |
| 159 | |
| 160 | void |
| 161 | cp_print_value_fields (struct type *type, struct type *real_type, |
| 162 | const gdb_byte *valaddr, int offset, CORE_ADDR address, |
| 163 | struct ui_file *stream, int format, int recurse, |
| 164 | enum val_prettyprint pretty, |
| 165 | struct type **dont_print_vb,int dont_print_statmem) |
| 166 | { |
| 167 | int i, len, n_baseclasses; |
| 168 | char *last_dont_print = obstack_next_free (&dont_print_statmem_obstack); |
| 169 | int fields_seen = 0; |
| 170 | |
| 171 | CHECK_TYPEDEF (type); |
| 172 | |
| 173 | fprintf_filtered (stream, "{"); |
| 174 | len = TYPE_NFIELDS (type); |
| 175 | n_baseclasses = TYPE_N_BASECLASSES (type); |
| 176 | |
| 177 | /* First, print out baseclasses such that we don't print |
| 178 | duplicates of virtual baseclasses. */ |
| 179 | |
| 180 | if (n_baseclasses > 0) |
| 181 | cp_print_value (type, real_type, valaddr, offset, address, stream, |
| 182 | format, recurse + 1, pretty, dont_print_vb); |
| 183 | |
| 184 | /* Second, print out data fields */ |
| 185 | |
| 186 | /* If there are no data fields, or if the only field is the |
| 187 | * vtbl pointer, skip this part */ |
| 188 | if ((len == n_baseclasses) |
| 189 | || ((len - n_baseclasses == 1) |
| 190 | && TYPE_HAS_VTABLE (type) |
| 191 | && strncmp (TYPE_FIELD_NAME (type, n_baseclasses), |
| 192 | hpacc_vtbl_ptr_name, 5) == 0) |
| 193 | || !len) |
| 194 | fprintf_filtered (stream, "<No data fields>"); |
| 195 | else |
| 196 | { |
| 197 | struct obstack tmp_obstack = dont_print_statmem_obstack; |
| 198 | |
| 199 | if (dont_print_statmem == 0) |
| 200 | { |
| 201 | /* If we're at top level, carve out a completely fresh |
| 202 | chunk of the obstack and use that until this particular |
| 203 | invocation returns. */ |
| 204 | obstack_finish (&dont_print_statmem_obstack); |
| 205 | } |
| 206 | |
| 207 | for (i = n_baseclasses; i < len; i++) |
| 208 | { |
| 209 | /* If requested, skip printing of static fields. */ |
| 210 | if (!static_field_print && TYPE_FIELD_STATIC (type, i)) |
| 211 | continue; |
| 212 | |
| 213 | /* If a vtable pointer appears, we'll print it out later */ |
| 214 | if (TYPE_HAS_VTABLE (type) |
| 215 | && strncmp (TYPE_FIELD_NAME (type, i), hpacc_vtbl_ptr_name, |
| 216 | 5) == 0) |
| 217 | continue; |
| 218 | |
| 219 | if (fields_seen) |
| 220 | fprintf_filtered (stream, ", "); |
| 221 | else if (n_baseclasses > 0) |
| 222 | { |
| 223 | if (pretty) |
| 224 | { |
| 225 | fprintf_filtered (stream, "\n"); |
| 226 | print_spaces_filtered (2 + 2 * recurse, stream); |
| 227 | fputs_filtered ("members of ", stream); |
| 228 | fputs_filtered (type_name_no_tag (type), stream); |
| 229 | fputs_filtered (": ", stream); |
| 230 | } |
| 231 | } |
| 232 | fields_seen = 1; |
| 233 | |
| 234 | if (pretty) |
| 235 | { |
| 236 | fprintf_filtered (stream, "\n"); |
| 237 | print_spaces_filtered (2 + 2 * recurse, stream); |
| 238 | } |
| 239 | else |
| 240 | { |
| 241 | wrap_here (n_spaces (2 + 2 * recurse)); |
| 242 | } |
| 243 | if (inspect_it) |
| 244 | { |
| 245 | if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR) |
| 246 | fputs_filtered ("\"( ptr \"", stream); |
| 247 | else |
| 248 | fputs_filtered ("\"( nodef \"", stream); |
| 249 | if (TYPE_FIELD_STATIC (type, i)) |
| 250 | fputs_filtered ("static ", stream); |
| 251 | fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i), |
| 252 | current_language->la_language, |
| 253 | DMGL_PARAMS | DMGL_ANSI); |
| 254 | fputs_filtered ("\" \"", stream); |
| 255 | fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i), |
| 256 | current_language->la_language, |
| 257 | DMGL_PARAMS | DMGL_ANSI); |
| 258 | fputs_filtered ("\") \"", stream); |
| 259 | } |
| 260 | else |
| 261 | { |
| 262 | annotate_field_begin (TYPE_FIELD_TYPE (type, i)); |
| 263 | |
| 264 | if (TYPE_FIELD_STATIC (type, i)) |
| 265 | fputs_filtered ("static ", stream); |
| 266 | fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i), |
| 267 | current_language->la_language, |
| 268 | DMGL_PARAMS | DMGL_ANSI); |
| 269 | annotate_field_name_end (); |
| 270 | /* do not print leading '=' in case of anonymous unions */ |
| 271 | if (strcmp (TYPE_FIELD_NAME (type, i), "")) |
| 272 | fputs_filtered (" = ", stream); |
| 273 | annotate_field_value (); |
| 274 | } |
| 275 | |
| 276 | if (!TYPE_FIELD_STATIC (type, i) && TYPE_FIELD_PACKED (type, i)) |
| 277 | { |
| 278 | struct value *v; |
| 279 | |
| 280 | /* Bitfields require special handling, especially due to byte |
| 281 | order problems. */ |
| 282 | if (TYPE_FIELD_IGNORE (type, i)) |
| 283 | { |
| 284 | fputs_filtered ("<optimized out or zero length>", stream); |
| 285 | } |
| 286 | else |
| 287 | { |
| 288 | v = value_from_longest |
| 289 | (TYPE_FIELD_TYPE (type, i), |
| 290 | unpack_field_as_long (type, valaddr + offset, i)); |
| 291 | |
| 292 | common_val_print (v, stream, format, 0, recurse + 1, pretty); |
| 293 | } |
| 294 | } |
| 295 | else |
| 296 | { |
| 297 | if (TYPE_FIELD_IGNORE (type, i)) |
| 298 | { |
| 299 | fputs_filtered ("<optimized out or zero length>", stream); |
| 300 | } |
| 301 | else if (TYPE_FIELD_STATIC (type, i)) |
| 302 | { |
| 303 | struct value *v = value_static_field (type, i); |
| 304 | if (v == NULL) |
| 305 | fputs_filtered ("<optimized out>", stream); |
| 306 | else |
| 307 | cp_print_static_field (TYPE_FIELD_TYPE (type, i), v, |
| 308 | stream, format, recurse + 1, |
| 309 | pretty); |
| 310 | } |
| 311 | else |
| 312 | { |
| 313 | val_print (TYPE_FIELD_TYPE (type, i), |
| 314 | valaddr, offset + TYPE_FIELD_BITPOS (type, i) / 8, |
| 315 | address + TYPE_FIELD_BITPOS (type, i) / 8, |
| 316 | stream, format, 0, recurse + 1, pretty); |
| 317 | } |
| 318 | } |
| 319 | annotate_field_end (); |
| 320 | } |
| 321 | |
| 322 | if (dont_print_statmem == 0) |
| 323 | { |
| 324 | /* Free the space used to deal with the printing |
| 325 | of the members from top level. */ |
| 326 | obstack_free (&dont_print_statmem_obstack, last_dont_print); |
| 327 | dont_print_statmem_obstack = tmp_obstack; |
| 328 | } |
| 329 | |
| 330 | if (pretty) |
| 331 | { |
| 332 | fprintf_filtered (stream, "\n"); |
| 333 | print_spaces_filtered (2 * recurse, stream); |
| 334 | } |
| 335 | } /* if there are data fields */ |
| 336 | /* Now print out the virtual table pointer if there is one */ |
| 337 | if (TYPE_HAS_VTABLE (type) |
| 338 | && strncmp (TYPE_FIELD_NAME (type, n_baseclasses), |
| 339 | hpacc_vtbl_ptr_name, 5) == 0) |
| 340 | { |
| 341 | struct value *v; |
| 342 | /* First get the virtual table pointer and print it out */ |
| 343 | |
| 344 | #if 0 |
| 345 | fputs_filtered ("__vfp = ", stream); |
| 346 | #endif |
| 347 | |
| 348 | fputs_filtered (", Virtual table at ", stream); |
| 349 | |
| 350 | /* pai: FIXME 32x64 problem? */ |
| 351 | /* Not sure what the best notation is in the case where there is no |
| 352 | baseclass name. */ |
| 353 | v = value_from_pointer (lookup_pointer_type (builtin_type_unsigned_long), |
| 354 | *(unsigned long *) (valaddr + offset)); |
| 355 | |
| 356 | common_val_print (v, stream, format, 0, recurse + 1, pretty); |
| 357 | fields_seen = 1; |
| 358 | |
| 359 | if (vtblprint) |
| 360 | { |
| 361 | /* Print out function pointers in vtable. */ |
| 362 | |
| 363 | /* FIXME: then-clause is for non-RRBC layout of virtual |
| 364 | * table. The RRBC case in the else-clause is yet to be |
| 365 | * implemented. The if (1) below should be changed to a |
| 366 | * test for whether the executable we have was compiled |
| 367 | * with a version of HP aCC that doesn't have RRBC |
| 368 | * support. */ |
| 369 | |
| 370 | if (1) |
| 371 | { |
| 372 | /* no RRBC support; function pointers embedded directly |
| 373 | in vtable */ |
| 374 | |
| 375 | int vfuncs = count_virtual_fns (real_type); |
| 376 | |
| 377 | fputs_filtered (" {", stream); |
| 378 | |
| 379 | /* FIXME : doesn't work at present */ |
| 380 | #if 0 |
| 381 | fprintf_filtered (stream, "%d entr%s: ", vfuncs, |
| 382 | vfuncs == 1 ? "y" : "ies"); |
| 383 | #else |
| 384 | fputs_filtered ("not implemented", stream); |
| 385 | |
| 386 | |
| 387 | #endif |
| 388 | |
| 389 | /* recursive function that prints all virtual function entries */ |
| 390 | #if 0 |
| 391 | cp_print_hpacc_virtual_table_entries (real_type, &vfuncs, v, |
| 392 | stream, format, recurse, |
| 393 | pretty); |
| 394 | #endif |
| 395 | fputs_filtered ("}", stream); |
| 396 | } /* non-RRBC case */ |
| 397 | else |
| 398 | { |
| 399 | /* FIXME -- see comments above */ |
| 400 | /* RRBC support present; function pointers are found |
| 401 | * by indirection through the class segment entries. */ |
| 402 | |
| 403 | |
| 404 | } /* RRBC case */ |
| 405 | } /* if vtblprint */ |
| 406 | |
| 407 | if (pretty) |
| 408 | { |
| 409 | fprintf_filtered (stream, "\n"); |
| 410 | print_spaces_filtered (2 * recurse, stream); |
| 411 | } |
| 412 | |
| 413 | } /* if vtable exists */ |
| 414 | |
| 415 | fprintf_filtered (stream, "}"); |
| 416 | } |
| 417 | |
| 418 | /* Special val_print routine to avoid printing multiple copies of virtual |
| 419 | baseclasses. */ |
| 420 | |
| 421 | static void |
| 422 | cp_print_value (struct type *type, struct type *real_type, |
| 423 | const gdb_byte *valaddr, int offset, CORE_ADDR address, |
| 424 | struct ui_file *stream, int format, int recurse, |
| 425 | enum val_prettyprint pretty, struct type **dont_print_vb) |
| 426 | { |
| 427 | struct type **last_dont_print |
| 428 | = (struct type **) obstack_next_free (&dont_print_vb_obstack); |
| 429 | struct obstack tmp_obstack = dont_print_vb_obstack; |
| 430 | int i, n_baseclasses = TYPE_N_BASECLASSES (type); |
| 431 | int thisoffset; |
| 432 | struct type *thistype; |
| 433 | |
| 434 | if (dont_print_vb == 0) |
| 435 | { |
| 436 | /* If we're at top level, carve out a completely fresh |
| 437 | chunk of the obstack and use that until this particular |
| 438 | invocation returns. */ |
| 439 | /* Bump up the high-water mark. Now alpha is omega. */ |
| 440 | obstack_finish (&dont_print_vb_obstack); |
| 441 | } |
| 442 | |
| 443 | for (i = 0; i < n_baseclasses; i++) |
| 444 | { |
| 445 | int boffset; |
| 446 | int skip; |
| 447 | struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i)); |
| 448 | char *basename = TYPE_NAME (baseclass); |
| 449 | const gdb_byte *base_valaddr; |
| 450 | |
| 451 | if (BASETYPE_VIA_VIRTUAL (type, i)) |
| 452 | { |
| 453 | struct type **first_dont_print |
| 454 | = (struct type **) obstack_base (&dont_print_vb_obstack); |
| 455 | |
| 456 | int j = (struct type **) obstack_next_free (&dont_print_vb_obstack) |
| 457 | - first_dont_print; |
| 458 | |
| 459 | while (--j >= 0) |
| 460 | if (baseclass == first_dont_print[j]) |
| 461 | goto flush_it; |
| 462 | |
| 463 | obstack_ptr_grow (&dont_print_vb_obstack, baseclass); |
| 464 | } |
| 465 | |
| 466 | thisoffset = offset; |
| 467 | thistype = real_type; |
| 468 | if (TYPE_HAS_VTABLE (type) && BASETYPE_VIA_VIRTUAL (type, i)) |
| 469 | { |
| 470 | /* Assume HP/Taligent runtime convention */ |
| 471 | find_rt_vbase_offset (type, TYPE_BASECLASS (type, i), |
| 472 | valaddr, offset, &boffset, &skip); |
| 473 | if (skip >= 0) |
| 474 | error (_("Virtual base class offset not found from vtable while" |
| 475 | " printing")); |
| 476 | base_valaddr = valaddr; |
| 477 | } |
| 478 | else |
| 479 | { |
| 480 | boffset = baseclass_offset (type, i, |
| 481 | valaddr + offset, |
| 482 | address); |
| 483 | skip = ((boffset == -1) || (boffset + offset) < 0) ? 1 : -1; |
| 484 | |
| 485 | if (BASETYPE_VIA_VIRTUAL (type, i)) |
| 486 | { |
| 487 | /* The virtual base class pointer might have been |
| 488 | clobbered by the user program. Make sure that it |
| 489 | still points to a valid memory location. */ |
| 490 | |
| 491 | if (boffset != -1 |
| 492 | && ((boffset + offset) < 0 |
| 493 | || (boffset + offset) >= TYPE_LENGTH (type))) |
| 494 | { |
| 495 | /* FIXME (alloca): unsafe if baseclass is really really large. */ |
| 496 | gdb_byte *buf = alloca (TYPE_LENGTH (baseclass)); |
| 497 | base_valaddr = buf; |
| 498 | if (target_read_memory (address + boffset, buf, |
| 499 | TYPE_LENGTH (baseclass)) != 0) |
| 500 | skip = 1; |
| 501 | address = address + boffset; |
| 502 | thisoffset = 0; |
| 503 | boffset = 0; |
| 504 | thistype = baseclass; |
| 505 | } |
| 506 | else |
| 507 | base_valaddr = valaddr; |
| 508 | } |
| 509 | else |
| 510 | base_valaddr = valaddr; |
| 511 | } |
| 512 | |
| 513 | /* now do the printing */ |
| 514 | if (pretty) |
| 515 | { |
| 516 | fprintf_filtered (stream, "\n"); |
| 517 | print_spaces_filtered (2 * recurse, stream); |
| 518 | } |
| 519 | fputs_filtered ("<", stream); |
| 520 | /* Not sure what the best notation is in the case where there is no |
| 521 | baseclass name. */ |
| 522 | fputs_filtered (basename ? basename : "", stream); |
| 523 | fputs_filtered ("> = ", stream); |
| 524 | |
| 525 | |
| 526 | if (skip >= 1) |
| 527 | fprintf_filtered (stream, "<invalid address>"); |
| 528 | else |
| 529 | cp_print_value_fields (baseclass, thistype, base_valaddr, |
| 530 | thisoffset + boffset, address + boffset, |
| 531 | stream, format, |
| 532 | recurse, pretty, |
| 533 | ((struct type **) |
| 534 | obstack_base (&dont_print_vb_obstack)), |
| 535 | 0); |
| 536 | fputs_filtered (", ", stream); |
| 537 | |
| 538 | flush_it: |
| 539 | ; |
| 540 | } |
| 541 | |
| 542 | if (dont_print_vb == 0) |
| 543 | { |
| 544 | /* Free the space used to deal with the printing |
| 545 | of this type from top level. */ |
| 546 | obstack_free (&dont_print_vb_obstack, last_dont_print); |
| 547 | /* Reset watermark so that we can continue protecting |
| 548 | ourselves from whatever we were protecting ourselves. */ |
| 549 | dont_print_vb_obstack = tmp_obstack; |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | /* Print value of a static member. |
| 554 | To avoid infinite recursion when printing a class that contains |
| 555 | a static instance of the class, we keep the addresses of all printed |
| 556 | static member classes in an obstack and refuse to print them more |
| 557 | than once. |
| 558 | |
| 559 | VAL contains the value to print, TYPE, STREAM, RECURSE, and PRETTY |
| 560 | have the same meanings as in c_val_print. */ |
| 561 | |
| 562 | static void |
| 563 | cp_print_static_field (struct type *type, |
| 564 | struct value *val, |
| 565 | struct ui_file *stream, |
| 566 | int format, |
| 567 | int recurse, |
| 568 | enum val_prettyprint pretty) |
| 569 | { |
| 570 | if (TYPE_CODE (type) == TYPE_CODE_STRUCT) |
| 571 | { |
| 572 | CORE_ADDR *first_dont_print; |
| 573 | int i; |
| 574 | |
| 575 | first_dont_print |
| 576 | = (CORE_ADDR *) obstack_base (&dont_print_statmem_obstack); |
| 577 | i = (CORE_ADDR *) obstack_next_free (&dont_print_statmem_obstack) |
| 578 | - first_dont_print; |
| 579 | |
| 580 | while (--i >= 0) |
| 581 | { |
| 582 | if (VALUE_ADDRESS (val) == first_dont_print[i]) |
| 583 | { |
| 584 | fputs_filtered ("<same as static member of an already" |
| 585 | " seen type>", |
| 586 | stream); |
| 587 | return; |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | obstack_grow (&dont_print_statmem_obstack, (char *) &VALUE_ADDRESS (val), |
| 592 | sizeof (CORE_ADDR)); |
| 593 | |
| 594 | CHECK_TYPEDEF (type); |
| 595 | cp_print_value_fields (type, type, value_contents_all (val), |
| 596 | value_embedded_offset (val), VALUE_ADDRESS (val), |
| 597 | stream, format, recurse, pretty, NULL, 1); |
| 598 | return; |
| 599 | } |
| 600 | val_print (type, value_contents_all (val), |
| 601 | value_embedded_offset (val), VALUE_ADDRESS (val), |
| 602 | stream, format, 0, recurse, pretty); |
| 603 | } |
| 604 | |
| 605 | |
| 606 | /* Find the field in *DOMAIN, or its non-virtual base classes, with bit offset |
| 607 | OFFSET. Set *DOMAIN to the containing type and *FIELDNO to the containing |
| 608 | field number. If OFFSET is not exactly at the start of some field, set |
| 609 | *DOMAIN to NULL. */ |
| 610 | |
| 611 | void |
| 612 | cp_find_class_member (struct type **domain_p, int *fieldno, |
| 613 | LONGEST offset) |
| 614 | { |
| 615 | struct type *domain; |
| 616 | unsigned int i; |
| 617 | unsigned len; |
| 618 | |
| 619 | *domain_p = check_typedef (*domain_p); |
| 620 | domain = *domain_p; |
| 621 | len = TYPE_NFIELDS (domain); |
| 622 | |
| 623 | for (i = TYPE_N_BASECLASSES (domain); i < len; i++) |
| 624 | { |
| 625 | LONGEST bitpos = TYPE_FIELD_BITPOS (domain, i); |
| 626 | |
| 627 | QUIT; |
| 628 | if (offset == bitpos) |
| 629 | { |
| 630 | *fieldno = i; |
| 631 | return; |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | for (i = 0; i < TYPE_N_BASECLASSES (domain); i++) |
| 636 | { |
| 637 | LONGEST bitpos = TYPE_FIELD_BITPOS (domain, i); |
| 638 | LONGEST bitsize = 8 * TYPE_LENGTH (TYPE_FIELD_TYPE (domain, i)); |
| 639 | |
| 640 | if (offset >= bitpos && offset < bitpos + bitsize) |
| 641 | { |
| 642 | *domain_p = TYPE_FIELD_TYPE (domain, i); |
| 643 | cp_find_class_member (domain_p, fieldno, offset - bitpos); |
| 644 | return; |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | *domain_p = NULL; |
| 649 | } |
| 650 | |
| 651 | void |
| 652 | cp_print_class_member (const gdb_byte *valaddr, struct type *domain, |
| 653 | struct ui_file *stream, char *prefix) |
| 654 | { |
| 655 | /* VAL is a byte offset into the structure type DOMAIN. |
| 656 | Find the name of the field for that offset and |
| 657 | print it. */ |
| 658 | unsigned int fieldno; |
| 659 | |
| 660 | LONGEST val = unpack_long (builtin_type_long, valaddr); |
| 661 | |
| 662 | /* Pointers to data members are usually byte offsets into an object. |
| 663 | Because a data member can have offset zero, and a NULL pointer to |
| 664 | member must be distinct from any valid non-NULL pointer to |
| 665 | member, either the value is biased or the NULL value has a |
| 666 | special representation; both are permitted by ISO C++. HP aCC |
| 667 | used a bias of 0x20000000; HP cfront used a bias of 1; g++ 3.x |
| 668 | and other compilers which use the Itanium ABI use -1 as the NULL |
| 669 | value. GDB only supports that last form; to add support for |
| 670 | another form, make this into a cp-abi hook. */ |
| 671 | |
| 672 | if (val == -1) |
| 673 | { |
| 674 | fprintf_filtered (stream, "NULL"); |
| 675 | return; |
| 676 | } |
| 677 | |
| 678 | cp_find_class_member (&domain, &fieldno, val << 3); |
| 679 | |
| 680 | if (domain != NULL) |
| 681 | { |
| 682 | char *name; |
| 683 | fputs_filtered (prefix, stream); |
| 684 | name = type_name_no_tag (domain); |
| 685 | if (name) |
| 686 | fputs_filtered (name, stream); |
| 687 | else |
| 688 | c_type_print_base (domain, stream, 0, 0); |
| 689 | fprintf_filtered (stream, "::"); |
| 690 | fputs_filtered (TYPE_FIELD_NAME (domain, fieldno), stream); |
| 691 | } |
| 692 | else |
| 693 | fprintf_filtered (stream, "%ld", (long) val); |
| 694 | } |
| 695 | |
| 696 | |
| 697 | /* This function prints out virtual table entries for a class; it |
| 698 | * recurses on the base classes to find all virtual functions |
| 699 | * available in a class. |
| 700 | * |
| 701 | * pai/1997-05-21 Note: As the name suggests, it's currently |
| 702 | * implemented for HP aCC runtime only. g++ objects are handled |
| 703 | * differently and I have made no attempt to fold that logic in |
| 704 | * here. The runtime layout is different for the two cases. Also, |
| 705 | * this currently has only the code for non-RRBC layouts generated by |
| 706 | * the HP aCC compiler; RRBC code is stubbed out and will have to be |
| 707 | * added later. */ |
| 708 | |
| 709 | |
| 710 | static void |
| 711 | cp_print_hpacc_virtual_table_entries (struct type *type, int *vfuncs, |
| 712 | struct value *v, struct ui_file *stream, |
| 713 | int format, int recurse, |
| 714 | enum val_prettyprint pretty) |
| 715 | { |
| 716 | int fn, oi; |
| 717 | |
| 718 | /* pai: FIXME this function doesn't work. It should handle a given |
| 719 | * virtual function only once (latest redefinition in class hierarchy) |
| 720 | */ |
| 721 | |
| 722 | /* Recursion on other classes that can share the same vtable */ |
| 723 | struct type *pbc = primary_base_class (type); |
| 724 | if (pbc) |
| 725 | cp_print_hpacc_virtual_table_entries (pbc, vfuncs, v, stream, format, |
| 726 | recurse, pretty); |
| 727 | |
| 728 | /* Now deal with vfuncs declared in this class */ |
| 729 | for (fn = 0; fn < TYPE_NFN_FIELDS (type); fn++) |
| 730 | for (oi = 0; oi < TYPE_FN_FIELDLIST_LENGTH (type, fn); oi++) |
| 731 | if (TYPE_FN_FIELD_VIRTUAL_P (TYPE_FN_FIELDLIST1 (type, fn), oi)) |
| 732 | { |
| 733 | char *vf_name; |
| 734 | const char *field_physname; |
| 735 | |
| 736 | /* virtual function offset */ |
| 737 | int vx = (TYPE_FN_FIELD_VOFFSET (TYPE_FN_FIELDLIST1 (type, fn), oi) |
| 738 | - 1); |
| 739 | |
| 740 | /* Get the address of the vfunction entry */ |
| 741 | struct value *vf = value_copy (v); |
| 742 | if (value_lazy (vf)) |
| 743 | (void) value_fetch_lazy (vf); |
| 744 | /* adjust by offset */ |
| 745 | /* NOTE: cagney/2005-01-02: THIS IS BOGUS. */ |
| 746 | value_contents_writeable (vf)[0] += 4 * (HP_ACC_VFUNC_START + vx); |
| 747 | vf = value_ind (vf); /* get the entry */ |
| 748 | /* make it a pointer */ |
| 749 | deprecated_set_value_type (vf, value_type (v)); |
| 750 | |
| 751 | /* print out the entry */ |
| 752 | common_val_print (vf, stream, format, 0, recurse + 1, pretty); |
| 753 | field_physname |
| 754 | = TYPE_FN_FIELD_PHYSNAME (TYPE_FN_FIELDLIST1 (type, fn), oi); |
| 755 | /* pai: (temp) FIXME Maybe this should be DMGL_ANSI */ |
| 756 | vf_name = cplus_demangle (field_physname, DMGL_ARM); |
| 757 | fprintf_filtered (stream, " %s", vf_name); |
| 758 | xfree (vf_name); |
| 759 | if (--(*vfuncs) > 0) |
| 760 | fputs_filtered (", ", stream); |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | |
| 765 | |
| 766 | void |
| 767 | _initialize_cp_valprint (void) |
| 768 | { |
| 769 | add_setshow_boolean_cmd ("static-members", class_support, |
| 770 | &static_field_print, _("\ |
| 771 | Set printing of C++ static members."), _("\ |
| 772 | Show printing of C++ static members."), NULL, |
| 773 | NULL, |
| 774 | show_static_field_print, |
| 775 | &setprintlist, &showprintlist); |
| 776 | /* Turn on printing of static fields. */ |
| 777 | static_field_print = 1; |
| 778 | |
| 779 | add_setshow_boolean_cmd ("vtbl", class_support, &vtblprint, _("\ |
| 780 | Set printing of C++ virtual function tables."), _("\ |
| 781 | Show printing of C++ virtual function tables."), NULL, |
| 782 | NULL, |
| 783 | show_vtblprint, |
| 784 | &setprintlist, &showprintlist); |
| 785 | |
| 786 | add_setshow_boolean_cmd ("object", class_support, &objectprint, _("\ |
| 787 | Set printing of object's derived type based on vtable info."), _("\ |
| 788 | Show printing of object's derived type based on vtable info."), NULL, |
| 789 | NULL, |
| 790 | show_objectprint, |
| 791 | &setprintlist, &showprintlist); |
| 792 | |
| 793 | /* Give people the defaults which they are used to. */ |
| 794 | objectprint = 0; |
| 795 | vtblprint = 0; |
| 796 | obstack_begin (&dont_print_vb_obstack, 32 * sizeof (struct type *)); |
| 797 | obstack_specify_allocation (&dont_print_statmem_obstack, |
| 798 | 32 * sizeof (CORE_ADDR), sizeof (CORE_ADDR), |
| 799 | xmalloc, xfree); |
| 800 | } |