| 1 | /* Language independent support for printing types 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 "bfd.h" /* Binary File Description */ |
| 23 | #include "symtab.h" |
| 24 | #include "gdbtypes.h" |
| 25 | #include "expression.h" |
| 26 | #include "value.h" |
| 27 | #include "gdbcore.h" |
| 28 | #include "command.h" |
| 29 | #include "gdbcmd.h" |
| 30 | #include "target.h" |
| 31 | #include "language.h" |
| 32 | #include "cp-abi.h" |
| 33 | #include "typeprint.h" |
| 34 | #include "valprint.h" |
| 35 | #include <ctype.h> |
| 36 | #include "cli/cli-utils.h" |
| 37 | #include "extension.h" |
| 38 | #include "completer.h" |
| 39 | #include "cli/cli-style.h" |
| 40 | |
| 41 | const struct type_print_options type_print_raw_options = |
| 42 | { |
| 43 | 1, /* raw */ |
| 44 | 1, /* print_methods */ |
| 45 | 1, /* print_typedefs */ |
| 46 | 0, /* print_offsets */ |
| 47 | 0, /* print_nested_type_limit */ |
| 48 | NULL, /* local_typedefs */ |
| 49 | NULL, /* global_table */ |
| 50 | NULL /* global_printers */ |
| 51 | }; |
| 52 | |
| 53 | /* The default flags for 'ptype' and 'whatis'. */ |
| 54 | |
| 55 | static struct type_print_options default_ptype_flags = |
| 56 | { |
| 57 | 0, /* raw */ |
| 58 | 1, /* print_methods */ |
| 59 | 1, /* print_typedefs */ |
| 60 | 0, /* print_offsets */ |
| 61 | 0, /* print_nested_type_limit */ |
| 62 | NULL, /* local_typedefs */ |
| 63 | NULL, /* global_table */ |
| 64 | NULL /* global_printers */ |
| 65 | }; |
| 66 | |
| 67 | \f |
| 68 | |
| 69 | /* See typeprint.h. */ |
| 70 | |
| 71 | const int print_offset_data::indentation = 23; |
| 72 | |
| 73 | |
| 74 | /* See typeprint.h. */ |
| 75 | |
| 76 | void |
| 77 | print_offset_data::maybe_print_hole (struct ui_file *stream, |
| 78 | unsigned int bitpos, |
| 79 | const char *for_what) |
| 80 | { |
| 81 | /* We check for END_BITPOS > 0 because there is a specific |
| 82 | scenario when END_BITPOS can be zero and BITPOS can be > |
| 83 | 0: when we are dealing with a struct/class with a virtual method. |
| 84 | Because of the vtable, the first field of the struct/class will |
| 85 | have an offset of sizeof (void *) (the size of the vtable). If |
| 86 | we do not check for END_BITPOS > 0 here, GDB will report |
| 87 | a hole before the first field, which is not accurate. */ |
| 88 | if (end_bitpos > 0 && end_bitpos < bitpos) |
| 89 | { |
| 90 | /* If END_BITPOS is smaller than the current type's |
| 91 | bitpos, it means there's a hole in the struct, so we report |
| 92 | it here. */ |
| 93 | unsigned int hole = bitpos - end_bitpos; |
| 94 | unsigned int hole_byte = hole / TARGET_CHAR_BIT; |
| 95 | unsigned int hole_bit = hole % TARGET_CHAR_BIT; |
| 96 | |
| 97 | if (hole_bit > 0) |
| 98 | fprintf_filtered (stream, "/* XXX %2u-bit %s */\n", hole_bit, |
| 99 | for_what); |
| 100 | |
| 101 | if (hole_byte > 0) |
| 102 | fprintf_filtered (stream, "/* XXX %2u-byte %s */\n", hole_byte, |
| 103 | for_what); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /* See typeprint.h. */ |
| 108 | |
| 109 | void |
| 110 | print_offset_data::update (struct type *type, unsigned int field_idx, |
| 111 | struct ui_file *stream) |
| 112 | { |
| 113 | if (field_is_static (&type->field (field_idx))) |
| 114 | { |
| 115 | print_spaces_filtered (indentation, stream); |
| 116 | return; |
| 117 | } |
| 118 | |
| 119 | struct type *ftype = check_typedef (type->field (field_idx).type ()); |
| 120 | if (type->code () == TYPE_CODE_UNION) |
| 121 | { |
| 122 | /* Since union fields don't have the concept of offsets, we just |
| 123 | print their sizes. */ |
| 124 | fprintf_filtered (stream, "/* %4s */", |
| 125 | pulongest (TYPE_LENGTH (ftype))); |
| 126 | return; |
| 127 | } |
| 128 | |
| 129 | unsigned int bitpos = TYPE_FIELD_BITPOS (type, field_idx); |
| 130 | unsigned int fieldsize_byte = TYPE_LENGTH (ftype); |
| 131 | unsigned int fieldsize_bit = fieldsize_byte * TARGET_CHAR_BIT; |
| 132 | |
| 133 | maybe_print_hole (stream, bitpos, "hole"); |
| 134 | |
| 135 | if (TYPE_FIELD_PACKED (type, field_idx) |
| 136 | || offset_bitpos % TARGET_CHAR_BIT != 0) |
| 137 | { |
| 138 | /* We're dealing with a bitfield. Print the bit offset. */ |
| 139 | fieldsize_bit = TYPE_FIELD_BITSIZE (type, field_idx); |
| 140 | |
| 141 | unsigned real_bitpos = bitpos + offset_bitpos; |
| 142 | |
| 143 | fprintf_filtered (stream, "/* %4u:%2u", real_bitpos / TARGET_CHAR_BIT, |
| 144 | real_bitpos % TARGET_CHAR_BIT); |
| 145 | } |
| 146 | else |
| 147 | { |
| 148 | /* The position of the field, relative to the beginning of the |
| 149 | struct. */ |
| 150 | fprintf_filtered (stream, "/* %4u", |
| 151 | (bitpos + offset_bitpos) / TARGET_CHAR_BIT); |
| 152 | |
| 153 | fprintf_filtered (stream, " "); |
| 154 | } |
| 155 | |
| 156 | fprintf_filtered (stream, " | %4u */", fieldsize_byte); |
| 157 | |
| 158 | end_bitpos = bitpos + fieldsize_bit; |
| 159 | } |
| 160 | |
| 161 | /* See typeprint.h. */ |
| 162 | |
| 163 | void |
| 164 | print_offset_data::finish (struct type *type, int level, |
| 165 | struct ui_file *stream) |
| 166 | { |
| 167 | unsigned int bitpos = TYPE_LENGTH (type) * TARGET_CHAR_BIT; |
| 168 | maybe_print_hole (stream, bitpos, "padding"); |
| 169 | |
| 170 | fputs_filtered ("\n", stream); |
| 171 | print_spaces_filtered (level + 4 + print_offset_data::indentation, stream); |
| 172 | fprintf_filtered (stream, "/* total size (bytes): %4s */\n", |
| 173 | pulongest (TYPE_LENGTH (type))); |
| 174 | } |
| 175 | |
| 176 | \f |
| 177 | |
| 178 | /* A hash function for a typedef_field. */ |
| 179 | |
| 180 | static hashval_t |
| 181 | hash_typedef_field (const void *p) |
| 182 | { |
| 183 | const struct decl_field *tf = (const struct decl_field *) p; |
| 184 | struct type *t = check_typedef (tf->type); |
| 185 | |
| 186 | return htab_hash_string (TYPE_SAFE_NAME (t)); |
| 187 | } |
| 188 | |
| 189 | /* An equality function for a typedef field. */ |
| 190 | |
| 191 | static int |
| 192 | eq_typedef_field (const void *a, const void *b) |
| 193 | { |
| 194 | const struct decl_field *tfa = (const struct decl_field *) a; |
| 195 | const struct decl_field *tfb = (const struct decl_field *) b; |
| 196 | |
| 197 | return types_equal (tfa->type, tfb->type); |
| 198 | } |
| 199 | |
| 200 | /* See typeprint.h. */ |
| 201 | |
| 202 | void |
| 203 | typedef_hash_table::recursively_update (struct type *t) |
| 204 | { |
| 205 | int i; |
| 206 | |
| 207 | for (i = 0; i < TYPE_TYPEDEF_FIELD_COUNT (t); ++i) |
| 208 | { |
| 209 | struct decl_field *tdef = &TYPE_TYPEDEF_FIELD (t, i); |
| 210 | void **slot; |
| 211 | |
| 212 | slot = htab_find_slot (m_table, tdef, INSERT); |
| 213 | /* Only add a given typedef name once. Really this shouldn't |
| 214 | happen; but it is safe enough to do the updates breadth-first |
| 215 | and thus use the most specific typedef. */ |
| 216 | if (*slot == NULL) |
| 217 | *slot = tdef; |
| 218 | } |
| 219 | |
| 220 | /* Recurse into superclasses. */ |
| 221 | for (i = 0; i < TYPE_N_BASECLASSES (t); ++i) |
| 222 | recursively_update (TYPE_BASECLASS (t, i)); |
| 223 | } |
| 224 | |
| 225 | /* See typeprint.h. */ |
| 226 | |
| 227 | void |
| 228 | typedef_hash_table::add_template_parameters (struct type *t) |
| 229 | { |
| 230 | int i; |
| 231 | |
| 232 | for (i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (t); ++i) |
| 233 | { |
| 234 | struct decl_field *tf; |
| 235 | void **slot; |
| 236 | |
| 237 | /* We only want type-valued template parameters in the hash. */ |
| 238 | if (SYMBOL_CLASS (TYPE_TEMPLATE_ARGUMENT (t, i)) != LOC_TYPEDEF) |
| 239 | continue; |
| 240 | |
| 241 | tf = XOBNEW (&m_storage, struct decl_field); |
| 242 | tf->name = TYPE_TEMPLATE_ARGUMENT (t, i)->linkage_name (); |
| 243 | tf->type = SYMBOL_TYPE (TYPE_TEMPLATE_ARGUMENT (t, i)); |
| 244 | |
| 245 | slot = htab_find_slot (m_table, tf, INSERT); |
| 246 | if (*slot == NULL) |
| 247 | *slot = tf; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | /* See typeprint.h. */ |
| 252 | |
| 253 | typedef_hash_table::typedef_hash_table () |
| 254 | { |
| 255 | m_table = htab_create_alloc (10, hash_typedef_field, eq_typedef_field, |
| 256 | NULL, xcalloc, xfree); |
| 257 | } |
| 258 | |
| 259 | /* Free a typedef field table. */ |
| 260 | |
| 261 | typedef_hash_table::~typedef_hash_table () |
| 262 | { |
| 263 | htab_delete (m_table); |
| 264 | } |
| 265 | |
| 266 | /* Helper function for typedef_hash_table::copy. */ |
| 267 | |
| 268 | static int |
| 269 | copy_typedef_hash_element (void **slot, void *nt) |
| 270 | { |
| 271 | htab_t new_table = (htab_t) nt; |
| 272 | void **new_slot; |
| 273 | |
| 274 | new_slot = htab_find_slot (new_table, *slot, INSERT); |
| 275 | if (*new_slot == NULL) |
| 276 | *new_slot = *slot; |
| 277 | |
| 278 | return 1; |
| 279 | } |
| 280 | |
| 281 | /* See typeprint.h. */ |
| 282 | |
| 283 | typedef_hash_table::typedef_hash_table (const typedef_hash_table &table) |
| 284 | { |
| 285 | m_table = htab_create_alloc (10, hash_typedef_field, eq_typedef_field, |
| 286 | NULL, xcalloc, xfree); |
| 287 | htab_traverse_noresize (table.m_table, copy_typedef_hash_element, |
| 288 | m_table); |
| 289 | } |
| 290 | |
| 291 | /* Look up the type T in the global typedef hash. If it is found, |
| 292 | return the typedef name. If it is not found, apply the |
| 293 | type-printers, if any, given by start_script_type_printers and return the |
| 294 | result. A NULL return means that the name was not found. */ |
| 295 | |
| 296 | const char * |
| 297 | typedef_hash_table::find_global_typedef (const struct type_print_options *flags, |
| 298 | struct type *t) |
| 299 | { |
| 300 | char *applied; |
| 301 | void **slot; |
| 302 | struct decl_field tf, *new_tf; |
| 303 | |
| 304 | if (flags->global_typedefs == NULL) |
| 305 | return NULL; |
| 306 | |
| 307 | tf.name = NULL; |
| 308 | tf.type = t; |
| 309 | |
| 310 | slot = htab_find_slot (flags->global_typedefs->m_table, &tf, INSERT); |
| 311 | if (*slot != NULL) |
| 312 | { |
| 313 | new_tf = (struct decl_field *) *slot; |
| 314 | return new_tf->name; |
| 315 | } |
| 316 | |
| 317 | /* Put an entry into the hash table now, in case |
| 318 | apply_ext_lang_type_printers recurses. */ |
| 319 | new_tf = XOBNEW (&flags->global_typedefs->m_storage, struct decl_field); |
| 320 | new_tf->name = NULL; |
| 321 | new_tf->type = t; |
| 322 | |
| 323 | *slot = new_tf; |
| 324 | |
| 325 | applied = apply_ext_lang_type_printers (flags->global_printers, t); |
| 326 | |
| 327 | if (applied != NULL) |
| 328 | { |
| 329 | new_tf->name = obstack_strdup (&flags->global_typedefs->m_storage, |
| 330 | applied); |
| 331 | xfree (applied); |
| 332 | } |
| 333 | |
| 334 | return new_tf->name; |
| 335 | } |
| 336 | |
| 337 | /* See typeprint.h. */ |
| 338 | |
| 339 | const char * |
| 340 | typedef_hash_table::find_typedef (const struct type_print_options *flags, |
| 341 | struct type *t) |
| 342 | { |
| 343 | if (flags->local_typedefs != NULL) |
| 344 | { |
| 345 | struct decl_field tf, *found; |
| 346 | |
| 347 | tf.name = NULL; |
| 348 | tf.type = t; |
| 349 | found = (struct decl_field *) htab_find (flags->local_typedefs->m_table, |
| 350 | &tf); |
| 351 | |
| 352 | if (found != NULL) |
| 353 | return found->name; |
| 354 | } |
| 355 | |
| 356 | return find_global_typedef (flags, t); |
| 357 | } |
| 358 | |
| 359 | \f |
| 360 | |
| 361 | /* Print a description of a type in the format of a |
| 362 | typedef for the current language. |
| 363 | NEW is the new name for a type TYPE. */ |
| 364 | |
| 365 | void |
| 366 | typedef_print (struct type *type, struct symbol *newobj, struct ui_file *stream) |
| 367 | { |
| 368 | LA_PRINT_TYPEDEF (type, newobj, stream); |
| 369 | } |
| 370 | |
| 371 | /* Print a description of a type TYPE in the form of a declaration of a |
| 372 | variable named VARSTRING. (VARSTRING is demangled if necessary.) |
| 373 | Output goes to STREAM (via stdio). |
| 374 | If SHOW is positive, we show the contents of the outermost level |
| 375 | of structure even if there is a type name that could be used instead. |
| 376 | If SHOW is negative, we never show the details of elements' types. */ |
| 377 | |
| 378 | void |
| 379 | type_print (struct type *type, const char *varstring, struct ui_file *stream, |
| 380 | int show) |
| 381 | { |
| 382 | LA_PRINT_TYPE (type, varstring, stream, show, 0, &default_ptype_flags); |
| 383 | } |
| 384 | |
| 385 | /* Print TYPE to a string, returning it. The caller is responsible for |
| 386 | freeing the string. */ |
| 387 | |
| 388 | std::string |
| 389 | type_to_string (struct type *type) |
| 390 | { |
| 391 | try |
| 392 | { |
| 393 | string_file stb; |
| 394 | |
| 395 | type_print (type, "", &stb, -1); |
| 396 | return std::move (stb.string ()); |
| 397 | } |
| 398 | catch (const gdb_exception &except) |
| 399 | { |
| 400 | } |
| 401 | |
| 402 | return {}; |
| 403 | } |
| 404 | |
| 405 | /* See typeprint.h. */ |
| 406 | |
| 407 | void |
| 408 | type_print_unknown_return_type (struct ui_file *stream) |
| 409 | { |
| 410 | fprintf_styled (stream, metadata_style.style (), |
| 411 | _("<unknown return type>")); |
| 412 | } |
| 413 | |
| 414 | /* See typeprint.h. */ |
| 415 | |
| 416 | void |
| 417 | error_unknown_type (const char *sym_print_name) |
| 418 | { |
| 419 | error (_("'%s' has unknown type; cast it to its declared type"), |
| 420 | sym_print_name); |
| 421 | } |
| 422 | |
| 423 | /* Print type of EXP, or last thing in value history if EXP == NULL. |
| 424 | show is passed to type_print. */ |
| 425 | |
| 426 | static void |
| 427 | whatis_exp (const char *exp, int show) |
| 428 | { |
| 429 | struct value *val; |
| 430 | struct type *real_type = NULL; |
| 431 | struct type *type; |
| 432 | int full = 0; |
| 433 | LONGEST top = -1; |
| 434 | int using_enc = 0; |
| 435 | struct value_print_options opts; |
| 436 | struct type_print_options flags = default_ptype_flags; |
| 437 | |
| 438 | if (exp) |
| 439 | { |
| 440 | if (*exp == '/') |
| 441 | { |
| 442 | int seen_one = 0; |
| 443 | |
| 444 | for (++exp; *exp && !isspace (*exp); ++exp) |
| 445 | { |
| 446 | switch (*exp) |
| 447 | { |
| 448 | case 'r': |
| 449 | flags.raw = 1; |
| 450 | break; |
| 451 | case 'm': |
| 452 | flags.print_methods = 0; |
| 453 | break; |
| 454 | case 'M': |
| 455 | flags.print_methods = 1; |
| 456 | break; |
| 457 | case 't': |
| 458 | flags.print_typedefs = 0; |
| 459 | break; |
| 460 | case 'T': |
| 461 | flags.print_typedefs = 1; |
| 462 | break; |
| 463 | case 'o': |
| 464 | { |
| 465 | /* Filter out languages which don't implement the |
| 466 | feature. */ |
| 467 | if (show > 0 |
| 468 | && (current_language->la_language == language_c |
| 469 | || current_language->la_language == language_cplus |
| 470 | || current_language->la_language == language_rust)) |
| 471 | { |
| 472 | flags.print_offsets = 1; |
| 473 | flags.print_typedefs = 0; |
| 474 | flags.print_methods = 0; |
| 475 | } |
| 476 | break; |
| 477 | } |
| 478 | default: |
| 479 | error (_("unrecognized flag '%c'"), *exp); |
| 480 | } |
| 481 | seen_one = 1; |
| 482 | } |
| 483 | |
| 484 | if (!*exp && !seen_one) |
| 485 | error (_("flag expected")); |
| 486 | if (!isspace (*exp)) |
| 487 | error (_("expected space after format")); |
| 488 | exp = skip_spaces (exp); |
| 489 | } |
| 490 | |
| 491 | expression_up expr = parse_expression (exp); |
| 492 | |
| 493 | /* The behavior of "whatis" depends on whether the user |
| 494 | expression names a type directly, or a language expression |
| 495 | (including variable names). If the former, then "whatis" |
| 496 | strips one level of typedefs, only. If an expression, |
| 497 | "whatis" prints the type of the expression without stripping |
| 498 | any typedef level. "ptype" always strips all levels of |
| 499 | typedefs. */ |
| 500 | if (show == -1 && expr->elts[0].opcode == OP_TYPE) |
| 501 | { |
| 502 | /* The user expression names a type directly. */ |
| 503 | type = expr->elts[1].type; |
| 504 | |
| 505 | /* If this is a typedef, then find its immediate target. |
| 506 | Use check_typedef to resolve stubs, but ignore its result |
| 507 | because we do not want to dig past all typedefs. */ |
| 508 | check_typedef (type); |
| 509 | if (type->code () == TYPE_CODE_TYPEDEF) |
| 510 | type = TYPE_TARGET_TYPE (type); |
| 511 | |
| 512 | /* If the expression is actually a type, then there's no |
| 513 | value to fetch the dynamic type from. */ |
| 514 | val = NULL; |
| 515 | } |
| 516 | else |
| 517 | { |
| 518 | /* The user expression names a type indirectly by naming an |
| 519 | object or expression of that type. Find that |
| 520 | indirectly-named type. */ |
| 521 | val = evaluate_type (expr.get ()); |
| 522 | type = value_type (val); |
| 523 | } |
| 524 | } |
| 525 | else |
| 526 | { |
| 527 | val = access_value_history (0); |
| 528 | type = value_type (val); |
| 529 | } |
| 530 | |
| 531 | get_user_print_options (&opts); |
| 532 | if (val != NULL && opts.objectprint) |
| 533 | { |
| 534 | if (((type->code () == TYPE_CODE_PTR) || TYPE_IS_REFERENCE (type)) |
| 535 | && (TYPE_TARGET_TYPE (type)->code () == TYPE_CODE_STRUCT)) |
| 536 | real_type = value_rtti_indirect_type (val, &full, &top, &using_enc); |
| 537 | else if (type->code () == TYPE_CODE_STRUCT) |
| 538 | real_type = value_rtti_type (val, &full, &top, &using_enc); |
| 539 | } |
| 540 | |
| 541 | if (flags.print_offsets |
| 542 | && (type->code () == TYPE_CODE_STRUCT |
| 543 | || type->code () == TYPE_CODE_UNION)) |
| 544 | fprintf_filtered (gdb_stdout, "/* offset | size */ "); |
| 545 | |
| 546 | printf_filtered ("type = "); |
| 547 | |
| 548 | std::unique_ptr<typedef_hash_table> table_holder; |
| 549 | std::unique_ptr<ext_lang_type_printers> printer_holder; |
| 550 | if (!flags.raw) |
| 551 | { |
| 552 | table_holder.reset (new typedef_hash_table); |
| 553 | flags.global_typedefs = table_holder.get (); |
| 554 | |
| 555 | printer_holder.reset (new ext_lang_type_printers); |
| 556 | flags.global_printers = printer_holder.get (); |
| 557 | } |
| 558 | |
| 559 | if (real_type) |
| 560 | { |
| 561 | printf_filtered ("/* real type = "); |
| 562 | type_print (real_type, "", gdb_stdout, -1); |
| 563 | if (! full) |
| 564 | printf_filtered (" (incomplete object)"); |
| 565 | printf_filtered (" */\n"); |
| 566 | } |
| 567 | |
| 568 | LA_PRINT_TYPE (type, "", gdb_stdout, show, 0, &flags); |
| 569 | printf_filtered ("\n"); |
| 570 | } |
| 571 | |
| 572 | static void |
| 573 | whatis_command (const char *exp, int from_tty) |
| 574 | { |
| 575 | /* Most of the time users do not want to see all the fields |
| 576 | in a structure. If they do they can use the "ptype" command. |
| 577 | Hence the "-1" below. */ |
| 578 | whatis_exp (exp, -1); |
| 579 | } |
| 580 | |
| 581 | /* TYPENAME is either the name of a type, or an expression. */ |
| 582 | |
| 583 | static void |
| 584 | ptype_command (const char *type_name, int from_tty) |
| 585 | { |
| 586 | whatis_exp (type_name, 1); |
| 587 | } |
| 588 | |
| 589 | /* Print integral scalar data VAL, of type TYPE, onto stdio stream STREAM. |
| 590 | Used to print data from type structures in a specified type. For example, |
| 591 | array bounds may be characters or booleans in some languages, and this |
| 592 | allows the ranges to be printed in their "natural" form rather than as |
| 593 | decimal integer values. |
| 594 | |
| 595 | FIXME: This is here simply because only the type printing routines |
| 596 | currently use it, and it wasn't clear if it really belonged somewhere |
| 597 | else (like printcmd.c). There are a lot of other gdb routines that do |
| 598 | something similar, but they are generally concerned with printing values |
| 599 | that come from the inferior in target byte order and target size. */ |
| 600 | |
| 601 | void |
| 602 | print_type_scalar (struct type *type, LONGEST val, struct ui_file *stream) |
| 603 | { |
| 604 | unsigned int i; |
| 605 | unsigned len; |
| 606 | |
| 607 | type = check_typedef (type); |
| 608 | |
| 609 | switch (type->code ()) |
| 610 | { |
| 611 | |
| 612 | case TYPE_CODE_ENUM: |
| 613 | len = type->num_fields (); |
| 614 | for (i = 0; i < len; i++) |
| 615 | { |
| 616 | if (TYPE_FIELD_ENUMVAL (type, i) == val) |
| 617 | { |
| 618 | break; |
| 619 | } |
| 620 | } |
| 621 | if (i < len) |
| 622 | { |
| 623 | fputs_filtered (TYPE_FIELD_NAME (type, i), stream); |
| 624 | } |
| 625 | else |
| 626 | { |
| 627 | print_longest (stream, 'd', 0, val); |
| 628 | } |
| 629 | break; |
| 630 | |
| 631 | case TYPE_CODE_INT: |
| 632 | print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0, val); |
| 633 | break; |
| 634 | |
| 635 | case TYPE_CODE_CHAR: |
| 636 | LA_PRINT_CHAR ((unsigned char) val, type, stream); |
| 637 | break; |
| 638 | |
| 639 | case TYPE_CODE_BOOL: |
| 640 | fprintf_filtered (stream, val ? "TRUE" : "FALSE"); |
| 641 | break; |
| 642 | |
| 643 | case TYPE_CODE_RANGE: |
| 644 | print_type_scalar (TYPE_TARGET_TYPE (type), val, stream); |
| 645 | return; |
| 646 | |
| 647 | case TYPE_CODE_UNDEF: |
| 648 | case TYPE_CODE_PTR: |
| 649 | case TYPE_CODE_ARRAY: |
| 650 | case TYPE_CODE_STRUCT: |
| 651 | case TYPE_CODE_UNION: |
| 652 | case TYPE_CODE_FUNC: |
| 653 | case TYPE_CODE_FLT: |
| 654 | case TYPE_CODE_VOID: |
| 655 | case TYPE_CODE_SET: |
| 656 | case TYPE_CODE_STRING: |
| 657 | case TYPE_CODE_ERROR: |
| 658 | case TYPE_CODE_MEMBERPTR: |
| 659 | case TYPE_CODE_METHODPTR: |
| 660 | case TYPE_CODE_METHOD: |
| 661 | case TYPE_CODE_REF: |
| 662 | case TYPE_CODE_RVALUE_REF: |
| 663 | case TYPE_CODE_NAMESPACE: |
| 664 | error (_("internal error: unhandled type in print_type_scalar")); |
| 665 | break; |
| 666 | |
| 667 | default: |
| 668 | error (_("Invalid type code in symbol table.")); |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | /* Dump details of a type specified either directly or indirectly. |
| 673 | Uses the same sort of type lookup mechanism as ptype_command() |
| 674 | and whatis_command(). */ |
| 675 | |
| 676 | void |
| 677 | maintenance_print_type (const char *type_name, int from_tty) |
| 678 | { |
| 679 | struct value *val; |
| 680 | struct type *type; |
| 681 | |
| 682 | if (type_name != NULL) |
| 683 | { |
| 684 | expression_up expr = parse_expression (type_name); |
| 685 | if (expr->elts[0].opcode == OP_TYPE) |
| 686 | { |
| 687 | /* The user expression names a type directly, just use that type. */ |
| 688 | type = expr->elts[1].type; |
| 689 | } |
| 690 | else |
| 691 | { |
| 692 | /* The user expression may name a type indirectly by naming an |
| 693 | object of that type. Find that indirectly named type. */ |
| 694 | val = evaluate_type (expr.get ()); |
| 695 | type = value_type (val); |
| 696 | } |
| 697 | if (type != NULL) |
| 698 | { |
| 699 | recursive_dump_type (type, 0); |
| 700 | } |
| 701 | } |
| 702 | } |
| 703 | \f |
| 704 | |
| 705 | struct cmd_list_element *setprinttypelist; |
| 706 | |
| 707 | struct cmd_list_element *showprinttypelist; |
| 708 | |
| 709 | static bool print_methods = true; |
| 710 | |
| 711 | static void |
| 712 | set_print_type_methods (const char *args, |
| 713 | int from_tty, struct cmd_list_element *c) |
| 714 | { |
| 715 | default_ptype_flags.print_methods = print_methods; |
| 716 | } |
| 717 | |
| 718 | static void |
| 719 | show_print_type_methods (struct ui_file *file, int from_tty, |
| 720 | struct cmd_list_element *c, const char *value) |
| 721 | { |
| 722 | fprintf_filtered (file, _("Printing of methods defined in a class in %s\n"), |
| 723 | value); |
| 724 | } |
| 725 | |
| 726 | static bool print_typedefs = true; |
| 727 | |
| 728 | static void |
| 729 | set_print_type_typedefs (const char *args, |
| 730 | int from_tty, struct cmd_list_element *c) |
| 731 | { |
| 732 | default_ptype_flags.print_typedefs = print_typedefs; |
| 733 | } |
| 734 | |
| 735 | static void |
| 736 | show_print_type_typedefs (struct ui_file *file, int from_tty, |
| 737 | struct cmd_list_element *c, const char *value) |
| 738 | { |
| 739 | fprintf_filtered (file, _("Printing of typedefs defined in a class in %s\n"), |
| 740 | value); |
| 741 | } |
| 742 | |
| 743 | /* Limit on the number of nested type definitions to print or -1 to print |
| 744 | all nested type definitions in a class. By default, we do not print |
| 745 | nested definitions. */ |
| 746 | |
| 747 | static int print_nested_type_limit = 0; |
| 748 | |
| 749 | /* Set how many nested type definitions should be printed by the type |
| 750 | printer. */ |
| 751 | |
| 752 | static void |
| 753 | set_print_type_nested_types (const char *args, int from_tty, |
| 754 | struct cmd_list_element *c) |
| 755 | { |
| 756 | default_ptype_flags.print_nested_type_limit = print_nested_type_limit; |
| 757 | } |
| 758 | |
| 759 | /* Show how many nested type definitions the type printer will print. */ |
| 760 | |
| 761 | static void |
| 762 | show_print_type_nested_types (struct ui_file *file, int from_tty, |
| 763 | struct cmd_list_element *c, const char *value) |
| 764 | { |
| 765 | if (*value == '0') |
| 766 | { |
| 767 | fprintf_filtered (file, |
| 768 | _("Will not print nested types defined in a class\n")); |
| 769 | } |
| 770 | else |
| 771 | { |
| 772 | fprintf_filtered (file, |
| 773 | _("Will print %s nested types defined in a class\n"), |
| 774 | value); |
| 775 | } |
| 776 | } |
| 777 | |
| 778 | void _initialize_typeprint (); |
| 779 | void |
| 780 | _initialize_typeprint () |
| 781 | { |
| 782 | struct cmd_list_element *c; |
| 783 | |
| 784 | c = add_com ("ptype", class_vars, ptype_command, _("\ |
| 785 | Print definition of type TYPE.\n\ |
| 786 | Usage: ptype[/FLAGS] TYPE | EXPRESSION\n\ |
| 787 | Argument may be any type (for example a type name defined by typedef,\n\ |
| 788 | or \"struct STRUCT-TAG\" or \"class CLASS-NAME\" or \"union UNION-TAG\"\n\ |
| 789 | or \"enum ENUM-TAG\") or an expression.\n\ |
| 790 | The selected stack frame's lexical context is used to look up the name.\n\ |
| 791 | Contrary to \"whatis\", \"ptype\" always unrolls any typedefs.\n\ |
| 792 | \n\ |
| 793 | Available FLAGS are:\n\ |
| 794 | /r print in \"raw\" form; do not substitute typedefs\n\ |
| 795 | /m do not print methods defined in a class\n\ |
| 796 | /M print methods defined in a class\n\ |
| 797 | /t do not print typedefs defined in a class\n\ |
| 798 | /T print typedefs defined in a class\n\ |
| 799 | /o print offsets and sizes of fields in a struct (like pahole)")); |
| 800 | set_cmd_completer (c, expression_completer); |
| 801 | |
| 802 | c = add_com ("whatis", class_vars, whatis_command, |
| 803 | _("Print data type of expression EXP.\n\ |
| 804 | Only one level of typedefs is unrolled. See also \"ptype\".")); |
| 805 | set_cmd_completer (c, expression_completer); |
| 806 | |
| 807 | add_show_prefix_cmd ("type", no_class, |
| 808 | _("Generic command for showing type-printing settings."), |
| 809 | &showprinttypelist, "show print type ", 0, |
| 810 | &showprintlist); |
| 811 | add_basic_prefix_cmd ("type", no_class, |
| 812 | _("Generic command for setting how types print."), |
| 813 | &setprinttypelist, "set print type ", 0, |
| 814 | &setprintlist); |
| 815 | |
| 816 | add_setshow_boolean_cmd ("methods", no_class, &print_methods, |
| 817 | _("\ |
| 818 | Set printing of methods defined in classes."), _("\ |
| 819 | Show printing of methods defined in classes."), NULL, |
| 820 | set_print_type_methods, |
| 821 | show_print_type_methods, |
| 822 | &setprinttypelist, &showprinttypelist); |
| 823 | add_setshow_boolean_cmd ("typedefs", no_class, &print_typedefs, |
| 824 | _("\ |
| 825 | Set printing of typedefs defined in classes."), _("\ |
| 826 | Show printing of typedefs defined in classes."), NULL, |
| 827 | set_print_type_typedefs, |
| 828 | show_print_type_typedefs, |
| 829 | &setprinttypelist, &showprinttypelist); |
| 830 | |
| 831 | add_setshow_zuinteger_unlimited_cmd ("nested-type-limit", no_class, |
| 832 | &print_nested_type_limit, |
| 833 | _("\ |
| 834 | Set the number of recursive nested type definitions to print \ |
| 835 | (\"unlimited\" or -1 to show all)."), _("\ |
| 836 | Show the number of recursive nested type definitions to print."), NULL, |
| 837 | set_print_type_nested_types, |
| 838 | show_print_type_nested_types, |
| 839 | &setprinttypelist, &showprinttypelist); |
| 840 | } |
| 841 | |
| 842 | /* Print <not allocated> status to stream STREAM. */ |
| 843 | |
| 844 | void |
| 845 | val_print_not_allocated (struct ui_file *stream) |
| 846 | { |
| 847 | fprintf_styled (stream, metadata_style.style (), _("<not allocated>")); |
| 848 | } |
| 849 | |
| 850 | /* Print <not associated> status to stream STREAM. */ |
| 851 | |
| 852 | void |
| 853 | val_print_not_associated (struct ui_file *stream) |
| 854 | { |
| 855 | fprintf_styled (stream, metadata_style.style (), _("<not associated>")); |
| 856 | } |