1 /* Language independent support for printing types for GDB, the GNU debugger.
3 Copyright (C) 1986-2020 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
21 #include "gdb_obstack.h"
22 #include "bfd.h" /* Binary File Description */
25 #include "expression.h"
33 #include "typeprint.h"
36 #include "cli/cli-utils.h"
37 #include "extension.h"
38 #include "completer.h"
39 #include "cli/cli-style.h"
41 const struct type_print_options type_print_raw_options
=
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 */
53 /* The default flags for 'ptype' and 'whatis'. */
55 static struct type_print_options default_ptype_flags
=
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 */
69 /* See typeprint.h. */
71 const int print_offset_data::indentation
= 23;
74 /* See typeprint.h. */
77 print_offset_data::maybe_print_hole (struct ui_file
*stream
,
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
)
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
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
;
98 fprintf_filtered (stream
, "/* XXX %2u-bit %s */\n", hole_bit
,
102 fprintf_filtered (stream
, "/* XXX %2u-byte %s */\n", hole_byte
,
107 /* See typeprint.h. */
110 print_offset_data::update (struct type
*type
, unsigned int field_idx
,
111 struct ui_file
*stream
)
113 if (field_is_static (&type
->field (field_idx
)))
115 print_spaces_filtered (indentation
, stream
);
119 struct type
*ftype
= check_typedef (type
->field (field_idx
).type ());
120 if (type
->code () == TYPE_CODE_UNION
)
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
)));
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
;
133 maybe_print_hole (stream
, bitpos
, "hole");
135 if (TYPE_FIELD_PACKED (type
, field_idx
)
136 || offset_bitpos
% TARGET_CHAR_BIT
!= 0)
138 /* We're dealing with a bitfield. Print the bit offset. */
139 fieldsize_bit
= TYPE_FIELD_BITSIZE (type
, field_idx
);
141 unsigned real_bitpos
= bitpos
+ offset_bitpos
;
143 fprintf_filtered (stream
, "/* %4u:%2u", real_bitpos
/ TARGET_CHAR_BIT
,
144 real_bitpos
% TARGET_CHAR_BIT
);
148 /* The position of the field, relative to the beginning of the
150 fprintf_filtered (stream
, "/* %4u",
151 (bitpos
+ offset_bitpos
) / TARGET_CHAR_BIT
);
153 fprintf_filtered (stream
, " ");
156 fprintf_filtered (stream
, " | %4u */", fieldsize_byte
);
158 end_bitpos
= bitpos
+ fieldsize_bit
;
161 /* See typeprint.h. */
164 print_offset_data::finish (struct type
*type
, int level
,
165 struct ui_file
*stream
)
167 unsigned int bitpos
= TYPE_LENGTH (type
) * TARGET_CHAR_BIT
;
168 maybe_print_hole (stream
, bitpos
, "padding");
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
)));
178 /* A hash function for a typedef_field. */
181 hash_typedef_field (const void *p
)
183 const struct decl_field
*tf
= (const struct decl_field
*) p
;
184 struct type
*t
= check_typedef (tf
->type
);
186 return htab_hash_string (TYPE_SAFE_NAME (t
));
189 /* An equality function for a typedef field. */
192 eq_typedef_field (const void *a
, const void *b
)
194 const struct decl_field
*tfa
= (const struct decl_field
*) a
;
195 const struct decl_field
*tfb
= (const struct decl_field
*) b
;
197 return types_equal (tfa
->type
, tfb
->type
);
200 /* See typeprint.h. */
203 typedef_hash_table::recursively_update (struct type
*t
)
207 for (i
= 0; i
< TYPE_TYPEDEF_FIELD_COUNT (t
); ++i
)
209 struct decl_field
*tdef
= &TYPE_TYPEDEF_FIELD (t
, i
);
212 slot
= htab_find_slot (m_table
.get (), 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. */
220 /* Recurse into superclasses. */
221 for (i
= 0; i
< TYPE_N_BASECLASSES (t
); ++i
)
222 recursively_update (TYPE_BASECLASS (t
, i
));
225 /* See typeprint.h. */
228 typedef_hash_table::add_template_parameters (struct type
*t
)
232 for (i
= 0; i
< TYPE_N_TEMPLATE_ARGUMENTS (t
); ++i
)
234 struct decl_field
*tf
;
237 /* We only want type-valued template parameters in the hash. */
238 if (SYMBOL_CLASS (TYPE_TEMPLATE_ARGUMENT (t
, i
)) != LOC_TYPEDEF
)
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
));
245 slot
= htab_find_slot (m_table
.get (), tf
, INSERT
);
251 /* See typeprint.h. */
253 typedef_hash_table::typedef_hash_table ()
254 : m_table (htab_create_alloc (10, hash_typedef_field
, eq_typedef_field
,
255 NULL
, xcalloc
, xfree
))
259 /* Helper function for typedef_hash_table::copy. */
262 copy_typedef_hash_element (void **slot
, void *nt
)
264 htab_t new_table
= (htab_t
) nt
;
267 new_slot
= htab_find_slot (new_table
, *slot
, INSERT
);
268 if (*new_slot
== NULL
)
274 /* See typeprint.h. */
276 typedef_hash_table::typedef_hash_table (const typedef_hash_table
&table
)
278 m_table
.reset (htab_create_alloc (10, hash_typedef_field
, eq_typedef_field
,
279 NULL
, xcalloc
, xfree
));
280 htab_traverse_noresize (table
.m_table
.get (), copy_typedef_hash_element
,
284 /* Look up the type T in the global typedef hash. If it is found,
285 return the typedef name. If it is not found, apply the
286 type-printers, if any, given by start_script_type_printers and return the
287 result. A NULL return means that the name was not found. */
290 typedef_hash_table::find_global_typedef (const struct type_print_options
*flags
,
295 struct decl_field tf
, *new_tf
;
297 if (flags
->global_typedefs
== NULL
)
303 slot
= htab_find_slot (flags
->global_typedefs
->m_table
.get (), &tf
, INSERT
);
306 new_tf
= (struct decl_field
*) *slot
;
310 /* Put an entry into the hash table now, in case
311 apply_ext_lang_type_printers recurses. */
312 new_tf
= XOBNEW (&flags
->global_typedefs
->m_storage
, struct decl_field
);
318 applied
= apply_ext_lang_type_printers (flags
->global_printers
, t
);
322 new_tf
->name
= obstack_strdup (&flags
->global_typedefs
->m_storage
,
330 /* See typeprint.h. */
333 typedef_hash_table::find_typedef (const struct type_print_options
*flags
,
336 if (flags
->local_typedefs
!= NULL
)
338 struct decl_field tf
, *found
;
342 htab_t table
= flags
->local_typedefs
->m_table
.get ();
343 found
= (struct decl_field
*) htab_find (table
, &tf
);
349 return find_global_typedef (flags
, t
);
354 /* Print a description of a type in the format of a
355 typedef for the current language.
356 NEW is the new name for a type TYPE. */
359 typedef_print (struct type
*type
, struct symbol
*newobj
, struct ui_file
*stream
)
361 LA_PRINT_TYPEDEF (type
, newobj
, stream
);
364 /* Print a description of a type TYPE in the form of a declaration of a
365 variable named VARSTRING. (VARSTRING is demangled if necessary.)
366 Output goes to STREAM (via stdio).
367 If SHOW is positive, we show the contents of the outermost level
368 of structure even if there is a type name that could be used instead.
369 If SHOW is negative, we never show the details of elements' types. */
372 type_print (struct type
*type
, const char *varstring
, struct ui_file
*stream
,
375 LA_PRINT_TYPE (type
, varstring
, stream
, show
, 0, &default_ptype_flags
);
378 /* Print TYPE to a string, returning it. The caller is responsible for
379 freeing the string. */
382 type_to_string (struct type
*type
)
388 type_print (type
, "", &stb
, -1);
389 return std::move (stb
.string ());
391 catch (const gdb_exception
&except
)
398 /* See typeprint.h. */
401 type_print_unknown_return_type (struct ui_file
*stream
)
403 fprintf_styled (stream
, metadata_style
.style (),
404 _("<unknown return type>"));
407 /* See typeprint.h. */
410 error_unknown_type (const char *sym_print_name
)
412 error (_("'%s' has unknown type; cast it to its declared type"),
416 /* Print type of EXP, or last thing in value history if EXP == NULL.
417 show is passed to type_print. */
420 whatis_exp (const char *exp
, int show
)
423 struct type
*real_type
= NULL
;
428 struct value_print_options opts
;
429 struct type_print_options flags
= default_ptype_flags
;
437 for (++exp
; *exp
&& !isspace (*exp
); ++exp
)
445 flags
.print_methods
= 0;
448 flags
.print_methods
= 1;
451 flags
.print_typedefs
= 0;
454 flags
.print_typedefs
= 1;
458 /* Filter out languages which don't implement the
461 && (current_language
->la_language
== language_c
462 || current_language
->la_language
== language_cplus
463 || current_language
->la_language
== language_rust
))
465 flags
.print_offsets
= 1;
466 flags
.print_typedefs
= 0;
467 flags
.print_methods
= 0;
472 error (_("unrecognized flag '%c'"), *exp
);
477 if (!*exp
&& !seen_one
)
478 error (_("flag expected"));
480 error (_("expected space after format"));
481 exp
= skip_spaces (exp
);
484 expression_up expr
= parse_expression (exp
);
486 /* The behavior of "whatis" depends on whether the user
487 expression names a type directly, or a language expression
488 (including variable names). If the former, then "whatis"
489 strips one level of typedefs, only. If an expression,
490 "whatis" prints the type of the expression without stripping
491 any typedef level. "ptype" always strips all levels of
493 if (show
== -1 && expr
->elts
[0].opcode
== OP_TYPE
)
495 /* The user expression names a type directly. */
496 type
= expr
->elts
[1].type
;
498 /* If this is a typedef, then find its immediate target.
499 Use check_typedef to resolve stubs, but ignore its result
500 because we do not want to dig past all typedefs. */
501 check_typedef (type
);
502 if (type
->code () == TYPE_CODE_TYPEDEF
)
503 type
= TYPE_TARGET_TYPE (type
);
505 /* If the expression is actually a type, then there's no
506 value to fetch the dynamic type from. */
511 /* The user expression names a type indirectly by naming an
512 object or expression of that type. Find that
513 indirectly-named type. */
514 val
= evaluate_type (expr
.get ());
515 type
= value_type (val
);
520 val
= access_value_history (0);
521 type
= value_type (val
);
524 get_user_print_options (&opts
);
525 if (val
!= NULL
&& opts
.objectprint
)
527 if (((type
->code () == TYPE_CODE_PTR
) || TYPE_IS_REFERENCE (type
))
528 && (TYPE_TARGET_TYPE (type
)->code () == TYPE_CODE_STRUCT
))
529 real_type
= value_rtti_indirect_type (val
, &full
, &top
, &using_enc
);
530 else if (type
->code () == TYPE_CODE_STRUCT
)
531 real_type
= value_rtti_type (val
, &full
, &top
, &using_enc
);
534 if (flags
.print_offsets
535 && (type
->code () == TYPE_CODE_STRUCT
536 || type
->code () == TYPE_CODE_UNION
))
537 fprintf_filtered (gdb_stdout
, "/* offset | size */ ");
539 printf_filtered ("type = ");
541 std::unique_ptr
<typedef_hash_table
> table_holder
;
542 std::unique_ptr
<ext_lang_type_printers
> printer_holder
;
545 table_holder
.reset (new typedef_hash_table
);
546 flags
.global_typedefs
= table_holder
.get ();
548 printer_holder
.reset (new ext_lang_type_printers
);
549 flags
.global_printers
= printer_holder
.get ();
554 printf_filtered ("/* real type = ");
555 type_print (real_type
, "", gdb_stdout
, -1);
557 printf_filtered (" (incomplete object)");
558 printf_filtered (" */\n");
561 LA_PRINT_TYPE (type
, "", gdb_stdout
, show
, 0, &flags
);
562 printf_filtered ("\n");
566 whatis_command (const char *exp
, int from_tty
)
568 /* Most of the time users do not want to see all the fields
569 in a structure. If they do they can use the "ptype" command.
570 Hence the "-1" below. */
571 whatis_exp (exp
, -1);
574 /* TYPENAME is either the name of a type, or an expression. */
577 ptype_command (const char *type_name
, int from_tty
)
579 whatis_exp (type_name
, 1);
582 /* Print integral scalar data VAL, of type TYPE, onto stdio stream STREAM.
583 Used to print data from type structures in a specified type. For example,
584 array bounds may be characters or booleans in some languages, and this
585 allows the ranges to be printed in their "natural" form rather than as
586 decimal integer values.
588 FIXME: This is here simply because only the type printing routines
589 currently use it, and it wasn't clear if it really belonged somewhere
590 else (like printcmd.c). There are a lot of other gdb routines that do
591 something similar, but they are generally concerned with printing values
592 that come from the inferior in target byte order and target size. */
595 print_type_scalar (struct type
*type
, LONGEST val
, struct ui_file
*stream
)
600 type
= check_typedef (type
);
602 switch (type
->code ())
606 len
= type
->num_fields ();
607 for (i
= 0; i
< len
; i
++)
609 if (TYPE_FIELD_ENUMVAL (type
, i
) == val
)
616 fputs_filtered (TYPE_FIELD_NAME (type
, i
), stream
);
620 print_longest (stream
, 'd', 0, val
);
625 print_longest (stream
, type
->is_unsigned () ? 'u' : 'd', 0, val
);
629 LA_PRINT_CHAR ((unsigned char) val
, type
, stream
);
633 fprintf_filtered (stream
, val
? "TRUE" : "FALSE");
636 case TYPE_CODE_RANGE
:
637 print_type_scalar (TYPE_TARGET_TYPE (type
), val
, stream
);
640 case TYPE_CODE_UNDEF
:
642 case TYPE_CODE_ARRAY
:
643 case TYPE_CODE_STRUCT
:
644 case TYPE_CODE_UNION
:
649 case TYPE_CODE_STRING
:
650 case TYPE_CODE_ERROR
:
651 case TYPE_CODE_MEMBERPTR
:
652 case TYPE_CODE_METHODPTR
:
653 case TYPE_CODE_METHOD
:
655 case TYPE_CODE_RVALUE_REF
:
656 case TYPE_CODE_NAMESPACE
:
657 error (_("internal error: unhandled type in print_type_scalar"));
661 error (_("Invalid type code in symbol table."));
665 /* Dump details of a type specified either directly or indirectly.
666 Uses the same sort of type lookup mechanism as ptype_command()
667 and whatis_command(). */
670 maintenance_print_type (const char *type_name
, int from_tty
)
675 if (type_name
!= NULL
)
677 expression_up expr
= parse_expression (type_name
);
678 if (expr
->elts
[0].opcode
== OP_TYPE
)
680 /* The user expression names a type directly, just use that type. */
681 type
= expr
->elts
[1].type
;
685 /* The user expression may name a type indirectly by naming an
686 object of that type. Find that indirectly named type. */
687 val
= evaluate_type (expr
.get ());
688 type
= value_type (val
);
692 recursive_dump_type (type
, 0);
698 struct cmd_list_element
*setprinttypelist
;
700 struct cmd_list_element
*showprinttypelist
;
702 static bool print_methods
= true;
705 set_print_type_methods (const char *args
,
706 int from_tty
, struct cmd_list_element
*c
)
708 default_ptype_flags
.print_methods
= print_methods
;
712 show_print_type_methods (struct ui_file
*file
, int from_tty
,
713 struct cmd_list_element
*c
, const char *value
)
715 fprintf_filtered (file
, _("Printing of methods defined in a class in %s\n"),
719 static bool print_typedefs
= true;
722 set_print_type_typedefs (const char *args
,
723 int from_tty
, struct cmd_list_element
*c
)
725 default_ptype_flags
.print_typedefs
= print_typedefs
;
729 show_print_type_typedefs (struct ui_file
*file
, int from_tty
,
730 struct cmd_list_element
*c
, const char *value
)
732 fprintf_filtered (file
, _("Printing of typedefs defined in a class in %s\n"),
736 /* Limit on the number of nested type definitions to print or -1 to print
737 all nested type definitions in a class. By default, we do not print
738 nested definitions. */
740 static int print_nested_type_limit
= 0;
742 /* Set how many nested type definitions should be printed by the type
746 set_print_type_nested_types (const char *args
, int from_tty
,
747 struct cmd_list_element
*c
)
749 default_ptype_flags
.print_nested_type_limit
= print_nested_type_limit
;
752 /* Show how many nested type definitions the type printer will print. */
755 show_print_type_nested_types (struct ui_file
*file
, int from_tty
,
756 struct cmd_list_element
*c
, const char *value
)
760 fprintf_filtered (file
,
761 _("Will not print nested types defined in a class\n"));
765 fprintf_filtered (file
,
766 _("Will print %s nested types defined in a class\n"),
771 void _initialize_typeprint ();
773 _initialize_typeprint ()
775 struct cmd_list_element
*c
;
777 c
= add_com ("ptype", class_vars
, ptype_command
, _("\
778 Print definition of type TYPE.\n\
779 Usage: ptype[/FLAGS] TYPE | EXPRESSION\n\
780 Argument may be any type (for example a type name defined by typedef,\n\
781 or \"struct STRUCT-TAG\" or \"class CLASS-NAME\" or \"union UNION-TAG\"\n\
782 or \"enum ENUM-TAG\") or an expression.\n\
783 The selected stack frame's lexical context is used to look up the name.\n\
784 Contrary to \"whatis\", \"ptype\" always unrolls any typedefs.\n\
786 Available FLAGS are:\n\
787 /r print in \"raw\" form; do not substitute typedefs\n\
788 /m do not print methods defined in a class\n\
789 /M print methods defined in a class\n\
790 /t do not print typedefs defined in a class\n\
791 /T print typedefs defined in a class\n\
792 /o print offsets and sizes of fields in a struct (like pahole)"));
793 set_cmd_completer (c
, expression_completer
);
795 c
= add_com ("whatis", class_vars
, whatis_command
,
796 _("Print data type of expression EXP.\n\
797 Only one level of typedefs is unrolled. See also \"ptype\"."));
798 set_cmd_completer (c
, expression_completer
);
800 add_show_prefix_cmd ("type", no_class
,
801 _("Generic command for showing type-printing settings."),
802 &showprinttypelist
, "show print type ", 0,
804 add_basic_prefix_cmd ("type", no_class
,
805 _("Generic command for setting how types print."),
806 &setprinttypelist
, "set print type ", 0,
809 add_setshow_boolean_cmd ("methods", no_class
, &print_methods
,
811 Set printing of methods defined in classes."), _("\
812 Show printing of methods defined in classes."), NULL
,
813 set_print_type_methods
,
814 show_print_type_methods
,
815 &setprinttypelist
, &showprinttypelist
);
816 add_setshow_boolean_cmd ("typedefs", no_class
, &print_typedefs
,
818 Set printing of typedefs defined in classes."), _("\
819 Show printing of typedefs defined in classes."), NULL
,
820 set_print_type_typedefs
,
821 show_print_type_typedefs
,
822 &setprinttypelist
, &showprinttypelist
);
824 add_setshow_zuinteger_unlimited_cmd ("nested-type-limit", no_class
,
825 &print_nested_type_limit
,
827 Set the number of recursive nested type definitions to print \
828 (\"unlimited\" or -1 to show all)."), _("\
829 Show the number of recursive nested type definitions to print."), NULL
,
830 set_print_type_nested_types
,
831 show_print_type_nested_types
,
832 &setprinttypelist
, &showprinttypelist
);
835 /* Print <not allocated> status to stream STREAM. */
838 val_print_not_allocated (struct ui_file
*stream
)
840 fprintf_styled (stream
, metadata_style
.style (), _("<not allocated>"));
843 /* Print <not associated> status to stream STREAM. */
846 val_print_not_associated (struct ui_file
*stream
)
848 fprintf_styled (stream
, metadata_style
.style (), _("<not associated>"));