| 1 | /* Support for printing C++ values for GDB, the GNU debugger. |
| 2 | |
| 3 | Copyright (C) 1986-2019 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_obstack.h" |
| 22 | #include "symtab.h" |
| 23 | #include "gdbtypes.h" |
| 24 | #include "expression.h" |
| 25 | #include "value.h" |
| 26 | #include "command.h" |
| 27 | #include "gdbcmd.h" |
| 28 | #include "demangle.h" |
| 29 | #include "annotate.h" |
| 30 | #include "c-lang.h" |
| 31 | #include "target.h" |
| 32 | #include "cp-abi.h" |
| 33 | #include "valprint.h" |
| 34 | #include "cp-support.h" |
| 35 | #include "language.h" |
| 36 | #include "extension.h" |
| 37 | #include "typeprint.h" |
| 38 | #include "gdbsupport/byte-vector.h" |
| 39 | #include "gdbarch.h" |
| 40 | |
| 41 | static struct obstack dont_print_vb_obstack; |
| 42 | static struct obstack dont_print_statmem_obstack; |
| 43 | static struct obstack dont_print_stat_array_obstack; |
| 44 | |
| 45 | static void cp_print_static_field (struct type *, struct value *, |
| 46 | struct ui_file *, int, |
| 47 | const struct value_print_options *); |
| 48 | |
| 49 | static void cp_print_value (struct type *, struct type *, |
| 50 | LONGEST, |
| 51 | CORE_ADDR, struct ui_file *, |
| 52 | int, struct value *, |
| 53 | const struct value_print_options *, |
| 54 | struct type **); |
| 55 | |
| 56 | |
| 57 | /* GCC versions after 2.4.5 use this. */ |
| 58 | extern const char vtbl_ptr_name[] = "__vtbl_ptr_type"; |
| 59 | |
| 60 | /* Return truth value for assertion that TYPE is of the type |
| 61 | "pointer to virtual function". */ |
| 62 | |
| 63 | int |
| 64 | cp_is_vtbl_ptr_type (struct type *type) |
| 65 | { |
| 66 | const char *type_name = TYPE_NAME (type); |
| 67 | |
| 68 | return (type_name != NULL && !strcmp (type_name, vtbl_ptr_name)); |
| 69 | } |
| 70 | |
| 71 | /* Return truth value for the assertion that TYPE is of the type |
| 72 | "pointer to virtual function table". */ |
| 73 | |
| 74 | int |
| 75 | cp_is_vtbl_member (struct type *type) |
| 76 | { |
| 77 | /* With older versions of g++, the vtbl field pointed to an array of |
| 78 | structures. Nowadays it points directly to the structure. */ |
| 79 | if (TYPE_CODE (type) == TYPE_CODE_PTR) |
| 80 | { |
| 81 | type = TYPE_TARGET_TYPE (type); |
| 82 | if (TYPE_CODE (type) == TYPE_CODE_ARRAY) |
| 83 | { |
| 84 | type = TYPE_TARGET_TYPE (type); |
| 85 | if (TYPE_CODE (type) == TYPE_CODE_STRUCT /* if not using thunks */ |
| 86 | || TYPE_CODE (type) == TYPE_CODE_PTR) /* if using thunks */ |
| 87 | { |
| 88 | /* Virtual functions tables are full of pointers |
| 89 | to virtual functions. */ |
| 90 | return cp_is_vtbl_ptr_type (type); |
| 91 | } |
| 92 | } |
| 93 | else if (TYPE_CODE (type) == TYPE_CODE_STRUCT) /* if not using thunks */ |
| 94 | { |
| 95 | return cp_is_vtbl_ptr_type (type); |
| 96 | } |
| 97 | else if (TYPE_CODE (type) == TYPE_CODE_PTR) /* if using thunks */ |
| 98 | { |
| 99 | /* The type name of the thunk pointer is NULL when using |
| 100 | dwarf2. We could test for a pointer to a function, but |
| 101 | there is no type info for the virtual table either, so it |
| 102 | wont help. */ |
| 103 | return cp_is_vtbl_ptr_type (type); |
| 104 | } |
| 105 | } |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | /* Mutually recursive subroutines of cp_print_value and c_val_print to |
| 110 | print out a structure's fields: cp_print_value_fields and |
| 111 | cp_print_value. |
| 112 | |
| 113 | TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and OPTIONS have the same |
| 114 | meanings as in cp_print_value and c_val_print. |
| 115 | |
| 116 | 2nd argument REAL_TYPE is used to carry over the type of the |
| 117 | derived class across the recursion to base classes. |
| 118 | |
| 119 | DONT_PRINT is an array of baseclass types that we should not print, |
| 120 | or zero if called from top level. */ |
| 121 | |
| 122 | void |
| 123 | cp_print_value_fields (struct type *type, struct type *real_type, |
| 124 | LONGEST offset, |
| 125 | CORE_ADDR address, struct ui_file *stream, |
| 126 | int recurse, struct value *val, |
| 127 | const struct value_print_options *options, |
| 128 | struct type **dont_print_vb, |
| 129 | int dont_print_statmem) |
| 130 | { |
| 131 | int i, len, n_baseclasses; |
| 132 | int fields_seen = 0; |
| 133 | static int last_set_recurse = -1; |
| 134 | |
| 135 | type = check_typedef (type); |
| 136 | |
| 137 | if (recurse == 0) |
| 138 | { |
| 139 | /* Any object can be left on obstacks only during an unexpected |
| 140 | error. */ |
| 141 | |
| 142 | if (obstack_object_size (&dont_print_statmem_obstack) > 0) |
| 143 | { |
| 144 | obstack_free (&dont_print_statmem_obstack, NULL); |
| 145 | obstack_begin (&dont_print_statmem_obstack, |
| 146 | 32 * sizeof (CORE_ADDR)); |
| 147 | } |
| 148 | if (obstack_object_size (&dont_print_stat_array_obstack) > 0) |
| 149 | { |
| 150 | obstack_free (&dont_print_stat_array_obstack, NULL); |
| 151 | obstack_begin (&dont_print_stat_array_obstack, |
| 152 | 32 * sizeof (struct type *)); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | fprintf_filtered (stream, "{"); |
| 157 | len = TYPE_NFIELDS (type); |
| 158 | n_baseclasses = TYPE_N_BASECLASSES (type); |
| 159 | |
| 160 | /* First, print out baseclasses such that we don't print |
| 161 | duplicates of virtual baseclasses. */ |
| 162 | |
| 163 | if (n_baseclasses > 0) |
| 164 | cp_print_value (type, real_type, |
| 165 | offset, address, stream, |
| 166 | recurse + 1, val, options, |
| 167 | dont_print_vb); |
| 168 | |
| 169 | /* Second, print out data fields */ |
| 170 | |
| 171 | /* If there are no data fields, skip this part */ |
| 172 | if (len == n_baseclasses || !len) |
| 173 | fprintf_filtered (stream, "<No data fields>"); |
| 174 | else |
| 175 | { |
| 176 | size_t statmem_obstack_initial_size = 0; |
| 177 | size_t stat_array_obstack_initial_size = 0; |
| 178 | struct type *vptr_basetype = NULL; |
| 179 | int vptr_fieldno; |
| 180 | |
| 181 | if (dont_print_statmem == 0) |
| 182 | { |
| 183 | statmem_obstack_initial_size = |
| 184 | obstack_object_size (&dont_print_statmem_obstack); |
| 185 | |
| 186 | if (last_set_recurse != recurse) |
| 187 | { |
| 188 | stat_array_obstack_initial_size = |
| 189 | obstack_object_size (&dont_print_stat_array_obstack); |
| 190 | |
| 191 | last_set_recurse = recurse; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | vptr_fieldno = get_vptr_fieldno (type, &vptr_basetype); |
| 196 | for (i = n_baseclasses; i < len; i++) |
| 197 | { |
| 198 | const gdb_byte *valaddr = value_contents_for_printing (val); |
| 199 | |
| 200 | /* If requested, skip printing of static fields. */ |
| 201 | if (!options->static_field_print |
| 202 | && field_is_static (&TYPE_FIELD (type, i))) |
| 203 | continue; |
| 204 | |
| 205 | if (fields_seen) |
| 206 | { |
| 207 | fputs_filtered (",", stream); |
| 208 | if (!options->prettyformat) |
| 209 | fputs_filtered (" ", stream); |
| 210 | } |
| 211 | else if (n_baseclasses > 0) |
| 212 | { |
| 213 | if (options->prettyformat) |
| 214 | { |
| 215 | fprintf_filtered (stream, "\n"); |
| 216 | print_spaces_filtered (2 + 2 * recurse, stream); |
| 217 | fputs_filtered ("members of ", stream); |
| 218 | fputs_filtered (TYPE_NAME (type), stream); |
| 219 | fputs_filtered (":", stream); |
| 220 | } |
| 221 | } |
| 222 | fields_seen = 1; |
| 223 | |
| 224 | if (options->prettyformat) |
| 225 | { |
| 226 | fprintf_filtered (stream, "\n"); |
| 227 | print_spaces_filtered (2 + 2 * recurse, stream); |
| 228 | } |
| 229 | else |
| 230 | { |
| 231 | wrap_here (n_spaces (2 + 2 * recurse)); |
| 232 | } |
| 233 | |
| 234 | annotate_field_begin (TYPE_FIELD_TYPE (type, i)); |
| 235 | |
| 236 | if (field_is_static (&TYPE_FIELD (type, i))) |
| 237 | fputs_filtered ("static ", stream); |
| 238 | fprintf_symbol_filtered (stream, |
| 239 | TYPE_FIELD_NAME (type, i), |
| 240 | current_language->la_language, |
| 241 | DMGL_PARAMS | DMGL_ANSI); |
| 242 | annotate_field_name_end (); |
| 243 | |
| 244 | /* We tweak various options in a few cases below. */ |
| 245 | value_print_options options_copy = *options; |
| 246 | value_print_options *opts = &options_copy; |
| 247 | |
| 248 | /* Do not print leading '=' in case of anonymous |
| 249 | unions. */ |
| 250 | if (strcmp (TYPE_FIELD_NAME (type, i), "")) |
| 251 | fputs_filtered (" = ", stream); |
| 252 | else |
| 253 | { |
| 254 | /* If this is an anonymous field then we want to consider it |
| 255 | as though it is at its parent's depth when it comes to the |
| 256 | max print depth. */ |
| 257 | if (opts->max_depth != -1 && opts->max_depth < INT_MAX) |
| 258 | ++opts->max_depth; |
| 259 | } |
| 260 | annotate_field_value (); |
| 261 | |
| 262 | if (!field_is_static (&TYPE_FIELD (type, i)) |
| 263 | && TYPE_FIELD_PACKED (type, i)) |
| 264 | { |
| 265 | struct value *v; |
| 266 | |
| 267 | /* Bitfields require special handling, especially due to |
| 268 | byte order problems. */ |
| 269 | if (TYPE_FIELD_IGNORE (type, i)) |
| 270 | { |
| 271 | fputs_filtered ("<optimized out or zero length>", stream); |
| 272 | } |
| 273 | else if (value_bits_synthetic_pointer (val, |
| 274 | TYPE_FIELD_BITPOS (type, |
| 275 | i), |
| 276 | TYPE_FIELD_BITSIZE (type, |
| 277 | i))) |
| 278 | { |
| 279 | fputs_filtered (_("<synthetic pointer>"), stream); |
| 280 | } |
| 281 | else |
| 282 | { |
| 283 | opts->deref_ref = 0; |
| 284 | |
| 285 | v = value_field_bitfield (type, i, valaddr, offset, val); |
| 286 | |
| 287 | common_val_print (v, stream, recurse + 1, |
| 288 | opts, current_language); |
| 289 | } |
| 290 | } |
| 291 | else |
| 292 | { |
| 293 | if (TYPE_FIELD_IGNORE (type, i)) |
| 294 | { |
| 295 | fputs_filtered ("<optimized out or zero length>", |
| 296 | stream); |
| 297 | } |
| 298 | else if (field_is_static (&TYPE_FIELD (type, i))) |
| 299 | { |
| 300 | try |
| 301 | { |
| 302 | struct value *v = value_static_field (type, i); |
| 303 | |
| 304 | cp_print_static_field (TYPE_FIELD_TYPE (type, i), |
| 305 | v, stream, recurse + 1, |
| 306 | opts); |
| 307 | } |
| 308 | catch (const gdb_exception_error &ex) |
| 309 | { |
| 310 | fprintf_filtered (stream, |
| 311 | _("<error reading variable: %s>"), |
| 312 | ex.what ()); |
| 313 | } |
| 314 | } |
| 315 | else if (i == vptr_fieldno && type == vptr_basetype) |
| 316 | { |
| 317 | int i_offset = offset + TYPE_FIELD_BITPOS (type, i) / 8; |
| 318 | struct type *i_type = TYPE_FIELD_TYPE (type, i); |
| 319 | |
| 320 | if (valprint_check_validity (stream, i_type, i_offset, val)) |
| 321 | { |
| 322 | CORE_ADDR addr; |
| 323 | |
| 324 | addr = extract_typed_address (valaddr + i_offset, i_type); |
| 325 | print_function_pointer_address (opts, |
| 326 | get_type_arch (type), |
| 327 | addr, stream); |
| 328 | } |
| 329 | } |
| 330 | else |
| 331 | { |
| 332 | opts->deref_ref = 0; |
| 333 | val_print (TYPE_FIELD_TYPE (type, i), |
| 334 | offset + TYPE_FIELD_BITPOS (type, i) / 8, |
| 335 | address, |
| 336 | stream, recurse + 1, val, opts, |
| 337 | current_language); |
| 338 | } |
| 339 | } |
| 340 | annotate_field_end (); |
| 341 | } |
| 342 | |
| 343 | if (dont_print_statmem == 0) |
| 344 | { |
| 345 | size_t obstack_final_size = |
| 346 | obstack_object_size (&dont_print_statmem_obstack); |
| 347 | |
| 348 | if (obstack_final_size > statmem_obstack_initial_size) |
| 349 | { |
| 350 | /* In effect, a pop of the printed-statics stack. */ |
| 351 | size_t shrink_bytes |
| 352 | = statmem_obstack_initial_size - obstack_final_size; |
| 353 | obstack_blank_fast (&dont_print_statmem_obstack, shrink_bytes); |
| 354 | } |
| 355 | |
| 356 | if (last_set_recurse != recurse) |
| 357 | { |
| 358 | obstack_final_size = |
| 359 | obstack_object_size (&dont_print_stat_array_obstack); |
| 360 | |
| 361 | if (obstack_final_size > stat_array_obstack_initial_size) |
| 362 | { |
| 363 | void *free_to_ptr = |
| 364 | (char *) obstack_next_free (&dont_print_stat_array_obstack) |
| 365 | - (obstack_final_size |
| 366 | - stat_array_obstack_initial_size); |
| 367 | |
| 368 | obstack_free (&dont_print_stat_array_obstack, |
| 369 | free_to_ptr); |
| 370 | } |
| 371 | last_set_recurse = -1; |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | if (options->prettyformat) |
| 376 | { |
| 377 | fprintf_filtered (stream, "\n"); |
| 378 | print_spaces_filtered (2 * recurse, stream); |
| 379 | } |
| 380 | } /* if there are data fields */ |
| 381 | |
| 382 | fprintf_filtered (stream, "}"); |
| 383 | } |
| 384 | |
| 385 | /* Like cp_print_value_fields, but find the runtime type of the object |
| 386 | and pass it as the `real_type' argument to cp_print_value_fields. |
| 387 | This function is a hack to work around the fact that |
| 388 | common_val_print passes the embedded offset to val_print, but not |
| 389 | the enclosing type. */ |
| 390 | |
| 391 | void |
| 392 | cp_print_value_fields_rtti (struct type *type, |
| 393 | const gdb_byte *valaddr, LONGEST offset, |
| 394 | CORE_ADDR address, |
| 395 | struct ui_file *stream, int recurse, |
| 396 | struct value *val, |
| 397 | const struct value_print_options *options, |
| 398 | struct type **dont_print_vb, |
| 399 | int dont_print_statmem) |
| 400 | { |
| 401 | struct type *real_type = NULL; |
| 402 | |
| 403 | /* We require all bits to be valid in order to attempt a |
| 404 | conversion. */ |
| 405 | if (!value_bits_any_optimized_out (val, |
| 406 | TARGET_CHAR_BIT * offset, |
| 407 | TARGET_CHAR_BIT * TYPE_LENGTH (type))) |
| 408 | { |
| 409 | struct value *value; |
| 410 | int full, using_enc; |
| 411 | LONGEST top; |
| 412 | |
| 413 | /* Ugh, we have to convert back to a value here. */ |
| 414 | value = value_from_contents_and_address (type, valaddr + offset, |
| 415 | address + offset); |
| 416 | type = value_type (value); |
| 417 | /* We don't actually care about most of the result here -- just |
| 418 | the type. We already have the correct offset, due to how |
| 419 | val_print was initially called. */ |
| 420 | real_type = value_rtti_type (value, &full, &top, &using_enc); |
| 421 | } |
| 422 | |
| 423 | if (!real_type) |
| 424 | real_type = type; |
| 425 | |
| 426 | cp_print_value_fields (type, real_type, offset, |
| 427 | address, stream, recurse, val, options, |
| 428 | dont_print_vb, dont_print_statmem); |
| 429 | } |
| 430 | |
| 431 | /* Special val_print routine to avoid printing multiple copies of |
| 432 | virtual baseclasses. */ |
| 433 | |
| 434 | static void |
| 435 | cp_print_value (struct type *type, struct type *real_type, |
| 436 | LONGEST offset, |
| 437 | CORE_ADDR address, struct ui_file *stream, |
| 438 | int recurse, struct value *val, |
| 439 | const struct value_print_options *options, |
| 440 | struct type **dont_print_vb) |
| 441 | { |
| 442 | struct type **last_dont_print |
| 443 | = (struct type **) obstack_next_free (&dont_print_vb_obstack); |
| 444 | struct obstack tmp_obstack = dont_print_vb_obstack; |
| 445 | int i, n_baseclasses = TYPE_N_BASECLASSES (type); |
| 446 | LONGEST thisoffset; |
| 447 | struct type *thistype; |
| 448 | const gdb_byte *valaddr = value_contents_for_printing (val); |
| 449 | |
| 450 | if (dont_print_vb == 0) |
| 451 | { |
| 452 | /* If we're at top level, carve out a completely fresh chunk of |
| 453 | the obstack and use that until this particular invocation |
| 454 | returns. */ |
| 455 | /* Bump up the high-water mark. Now alpha is omega. */ |
| 456 | obstack_finish (&dont_print_vb_obstack); |
| 457 | } |
| 458 | |
| 459 | for (i = 0; i < n_baseclasses; i++) |
| 460 | { |
| 461 | LONGEST boffset = 0; |
| 462 | int skip = 0; |
| 463 | struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i)); |
| 464 | const char *basename = TYPE_NAME (baseclass); |
| 465 | struct value *base_val = NULL; |
| 466 | |
| 467 | if (BASETYPE_VIA_VIRTUAL (type, i)) |
| 468 | { |
| 469 | struct type **first_dont_print |
| 470 | = (struct type **) obstack_base (&dont_print_vb_obstack); |
| 471 | |
| 472 | int j = (struct type **) |
| 473 | obstack_next_free (&dont_print_vb_obstack) - first_dont_print; |
| 474 | |
| 475 | while (--j >= 0) |
| 476 | if (baseclass == first_dont_print[j]) |
| 477 | goto flush_it; |
| 478 | |
| 479 | obstack_ptr_grow (&dont_print_vb_obstack, baseclass); |
| 480 | } |
| 481 | |
| 482 | thisoffset = offset; |
| 483 | thistype = real_type; |
| 484 | |
| 485 | try |
| 486 | { |
| 487 | boffset = baseclass_offset (type, i, valaddr, offset, address, val); |
| 488 | } |
| 489 | catch (const gdb_exception_error &ex) |
| 490 | { |
| 491 | if (ex.error == NOT_AVAILABLE_ERROR) |
| 492 | skip = -1; |
| 493 | else |
| 494 | skip = 1; |
| 495 | } |
| 496 | |
| 497 | if (skip == 0) |
| 498 | { |
| 499 | if (BASETYPE_VIA_VIRTUAL (type, i)) |
| 500 | { |
| 501 | /* The virtual base class pointer might have been |
| 502 | clobbered by the user program. Make sure that it |
| 503 | still points to a valid memory location. */ |
| 504 | |
| 505 | if ((boffset + offset) < 0 |
| 506 | || (boffset + offset) >= TYPE_LENGTH (real_type)) |
| 507 | { |
| 508 | gdb::byte_vector buf (TYPE_LENGTH (baseclass)); |
| 509 | |
| 510 | if (target_read_memory (address + boffset, buf.data (), |
| 511 | TYPE_LENGTH (baseclass)) != 0) |
| 512 | skip = 1; |
| 513 | base_val = value_from_contents_and_address (baseclass, |
| 514 | buf.data (), |
| 515 | address + boffset); |
| 516 | baseclass = value_type (base_val); |
| 517 | thisoffset = 0; |
| 518 | boffset = 0; |
| 519 | thistype = baseclass; |
| 520 | } |
| 521 | else |
| 522 | { |
| 523 | base_val = val; |
| 524 | } |
| 525 | } |
| 526 | else |
| 527 | { |
| 528 | base_val = val; |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | /* Now do the printing. */ |
| 533 | if (options->prettyformat) |
| 534 | { |
| 535 | fprintf_filtered (stream, "\n"); |
| 536 | print_spaces_filtered (2 * recurse, stream); |
| 537 | } |
| 538 | fputs_filtered ("<", stream); |
| 539 | /* Not sure what the best notation is in the case where there is |
| 540 | no baseclass name. */ |
| 541 | fputs_filtered (basename ? basename : "", stream); |
| 542 | fputs_filtered ("> = ", stream); |
| 543 | |
| 544 | if (skip < 0) |
| 545 | val_print_unavailable (stream); |
| 546 | else if (skip > 0) |
| 547 | val_print_invalid_address (stream); |
| 548 | else |
| 549 | { |
| 550 | int result = 0; |
| 551 | |
| 552 | if (options->max_depth > -1 |
| 553 | && recurse >= options->max_depth) |
| 554 | { |
| 555 | const struct language_defn *language = current_language; |
| 556 | gdb_assert (language->la_struct_too_deep_ellipsis != NULL); |
| 557 | fputs_filtered (language->la_struct_too_deep_ellipsis, stream); |
| 558 | } |
| 559 | else |
| 560 | { |
| 561 | /* Attempt to run an extension language pretty-printer on the |
| 562 | baseclass if possible. */ |
| 563 | if (!options->raw) |
| 564 | result |
| 565 | = apply_ext_lang_val_pretty_printer (baseclass, |
| 566 | thisoffset + boffset, |
| 567 | value_address (base_val), |
| 568 | stream, recurse, |
| 569 | base_val, options, |
| 570 | current_language); |
| 571 | |
| 572 | if (!result) |
| 573 | cp_print_value_fields (baseclass, thistype, |
| 574 | thisoffset + boffset, |
| 575 | value_address (base_val), |
| 576 | stream, recurse, base_val, options, |
| 577 | ((struct type **) |
| 578 | obstack_base (&dont_print_vb_obstack)), |
| 579 | 0); |
| 580 | } |
| 581 | } |
| 582 | fputs_filtered (", ", stream); |
| 583 | |
| 584 | flush_it: |
| 585 | ; |
| 586 | } |
| 587 | |
| 588 | if (dont_print_vb == 0) |
| 589 | { |
| 590 | /* Free the space used to deal with the printing |
| 591 | of this type from top level. */ |
| 592 | obstack_free (&dont_print_vb_obstack, last_dont_print); |
| 593 | /* Reset watermark so that we can continue protecting |
| 594 | ourselves from whatever we were protecting ourselves. */ |
| 595 | dont_print_vb_obstack = tmp_obstack; |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | /* Print value of a static member. To avoid infinite recursion when |
| 600 | printing a class that contains a static instance of the class, we |
| 601 | keep the addresses of all printed static member classes in an |
| 602 | obstack and refuse to print them more than once. |
| 603 | |
| 604 | VAL contains the value to print, TYPE, STREAM, RECURSE, and OPTIONS |
| 605 | have the same meanings as in c_val_print. */ |
| 606 | |
| 607 | static void |
| 608 | cp_print_static_field (struct type *type, |
| 609 | struct value *val, |
| 610 | struct ui_file *stream, |
| 611 | int recurse, |
| 612 | const struct value_print_options *options) |
| 613 | { |
| 614 | struct value_print_options opts; |
| 615 | |
| 616 | if (value_entirely_optimized_out (val)) |
| 617 | { |
| 618 | val_print_optimized_out (val, stream); |
| 619 | return; |
| 620 | } |
| 621 | |
| 622 | struct type *real_type = check_typedef (type); |
| 623 | if (TYPE_CODE (real_type) == TYPE_CODE_STRUCT) |
| 624 | { |
| 625 | CORE_ADDR *first_dont_print; |
| 626 | CORE_ADDR addr; |
| 627 | int i; |
| 628 | |
| 629 | first_dont_print |
| 630 | = (CORE_ADDR *) obstack_base (&dont_print_statmem_obstack); |
| 631 | i = obstack_object_size (&dont_print_statmem_obstack) |
| 632 | / sizeof (CORE_ADDR); |
| 633 | |
| 634 | while (--i >= 0) |
| 635 | { |
| 636 | if (value_address (val) == first_dont_print[i]) |
| 637 | { |
| 638 | fputs_filtered ("<same as static member of an already" |
| 639 | " seen type>", |
| 640 | stream); |
| 641 | return; |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | addr = value_address (val); |
| 646 | obstack_grow (&dont_print_statmem_obstack, (char *) &addr, |
| 647 | sizeof (CORE_ADDR)); |
| 648 | cp_print_value_fields (type, value_enclosing_type (val), |
| 649 | value_embedded_offset (val), addr, |
| 650 | stream, recurse, val, |
| 651 | options, NULL, 1); |
| 652 | return; |
| 653 | } |
| 654 | |
| 655 | if (TYPE_CODE (real_type) == TYPE_CODE_ARRAY) |
| 656 | { |
| 657 | struct type **first_dont_print; |
| 658 | int i; |
| 659 | struct type *target_type = TYPE_TARGET_TYPE (type); |
| 660 | |
| 661 | first_dont_print |
| 662 | = (struct type **) obstack_base (&dont_print_stat_array_obstack); |
| 663 | i = obstack_object_size (&dont_print_stat_array_obstack) |
| 664 | / sizeof (struct type *); |
| 665 | |
| 666 | while (--i >= 0) |
| 667 | { |
| 668 | if (target_type == first_dont_print[i]) |
| 669 | { |
| 670 | fputs_filtered ("<same as static member of an already" |
| 671 | " seen type>", |
| 672 | stream); |
| 673 | return; |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | obstack_grow (&dont_print_stat_array_obstack, |
| 678 | (char *) &target_type, |
| 679 | sizeof (struct type *)); |
| 680 | } |
| 681 | |
| 682 | opts = *options; |
| 683 | opts.deref_ref = 0; |
| 684 | val_print (type, |
| 685 | value_embedded_offset (val), |
| 686 | value_address (val), |
| 687 | stream, recurse, val, |
| 688 | &opts, current_language); |
| 689 | } |
| 690 | |
| 691 | /* Find the field in *SELF, or its non-virtual base classes, with |
| 692 | bit offset OFFSET. Set *SELF to the containing type and *FIELDNO |
| 693 | to the containing field number. If OFFSET is not exactly at the |
| 694 | start of some field, set *SELF to NULL. */ |
| 695 | |
| 696 | static void |
| 697 | cp_find_class_member (struct type **self_p, int *fieldno, |
| 698 | LONGEST offset) |
| 699 | { |
| 700 | struct type *self; |
| 701 | unsigned int i; |
| 702 | unsigned len; |
| 703 | |
| 704 | *self_p = check_typedef (*self_p); |
| 705 | self = *self_p; |
| 706 | len = TYPE_NFIELDS (self); |
| 707 | |
| 708 | for (i = TYPE_N_BASECLASSES (self); i < len; i++) |
| 709 | { |
| 710 | LONGEST bitpos = TYPE_FIELD_BITPOS (self, i); |
| 711 | |
| 712 | QUIT; |
| 713 | if (offset == bitpos) |
| 714 | { |
| 715 | *fieldno = i; |
| 716 | return; |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | for (i = 0; i < TYPE_N_BASECLASSES (self); i++) |
| 721 | { |
| 722 | LONGEST bitpos = TYPE_FIELD_BITPOS (self, i); |
| 723 | LONGEST bitsize = 8 * TYPE_LENGTH (TYPE_FIELD_TYPE (self, i)); |
| 724 | |
| 725 | if (offset >= bitpos && offset < bitpos + bitsize) |
| 726 | { |
| 727 | *self_p = TYPE_FIELD_TYPE (self, i); |
| 728 | cp_find_class_member (self_p, fieldno, offset - bitpos); |
| 729 | return; |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | *self_p = NULL; |
| 734 | } |
| 735 | |
| 736 | void |
| 737 | cp_print_class_member (const gdb_byte *valaddr, struct type *type, |
| 738 | struct ui_file *stream, const char *prefix) |
| 739 | { |
| 740 | enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type)); |
| 741 | |
| 742 | /* VAL is a byte offset into the structure type SELF_TYPE. |
| 743 | Find the name of the field for that offset and |
| 744 | print it. */ |
| 745 | struct type *self_type = TYPE_SELF_TYPE (type); |
| 746 | LONGEST val; |
| 747 | int fieldno; |
| 748 | |
| 749 | val = extract_signed_integer (valaddr, |
| 750 | TYPE_LENGTH (type), |
| 751 | byte_order); |
| 752 | |
| 753 | /* Pointers to data members are usually byte offsets into an object. |
| 754 | Because a data member can have offset zero, and a NULL pointer to |
| 755 | member must be distinct from any valid non-NULL pointer to |
| 756 | member, either the value is biased or the NULL value has a |
| 757 | special representation; both are permitted by ISO C++. HP aCC |
| 758 | used a bias of 0x20000000; HP cfront used a bias of 1; g++ 3.x |
| 759 | and other compilers which use the Itanium ABI use -1 as the NULL |
| 760 | value. GDB only supports that last form; to add support for |
| 761 | another form, make this into a cp-abi hook. */ |
| 762 | |
| 763 | if (val == -1) |
| 764 | { |
| 765 | fprintf_filtered (stream, "NULL"); |
| 766 | return; |
| 767 | } |
| 768 | |
| 769 | cp_find_class_member (&self_type, &fieldno, val << 3); |
| 770 | |
| 771 | if (self_type != NULL) |
| 772 | { |
| 773 | const char *name; |
| 774 | |
| 775 | fputs_filtered (prefix, stream); |
| 776 | name = TYPE_NAME (self_type); |
| 777 | if (name) |
| 778 | fputs_filtered (name, stream); |
| 779 | else |
| 780 | c_type_print_base (self_type, stream, 0, 0, &type_print_raw_options); |
| 781 | fprintf_filtered (stream, "::"); |
| 782 | fputs_filtered (TYPE_FIELD_NAME (self_type, fieldno), stream); |
| 783 | } |
| 784 | else |
| 785 | fprintf_filtered (stream, "%ld", (long) val); |
| 786 | } |
| 787 | |
| 788 | |
| 789 | void |
| 790 | _initialize_cp_valprint (void) |
| 791 | { |
| 792 | obstack_begin (&dont_print_stat_array_obstack, |
| 793 | 32 * sizeof (struct type *)); |
| 794 | obstack_begin (&dont_print_statmem_obstack, |
| 795 | 32 * sizeof (CORE_ADDR)); |
| 796 | obstack_begin (&dont_print_vb_obstack, |
| 797 | 32 * sizeof (struct type *)); |
| 798 | } |