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