1 /* Language independent support for printing types for GDB, the GNU debugger.
3 Copyright (C) 1986-2024 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/>. */
20 #include "gdbsupport/gdb_obstack.h"
24 #include "expression.h"
28 #include "cli/cli-cmds.h"
32 #include "typeprint.h"
35 #include "cli/cli-utils.h"
36 #include "extension.h"
37 #include "completer.h"
38 #include "cli/cli-style.h"
40 const struct type_print_options type_print_raw_options
=
43 1, /* print_methods */
44 1, /* print_typedefs */
45 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 */
62 0, /* print_nested_type_limit */
63 NULL
, /* local_typedefs */
64 NULL
, /* global_table */
65 NULL
/* global_printers */
70 /* See typeprint.h. */
72 const int print_offset_data::indentation
= 27;
74 /* See typeprint.h. */
76 print_offset_data::print_offset_data (const struct type_print_options
*flags
)
79 print_in_hex
= flags
->print_in_hex
;
82 /* See typeprint.h. */
85 print_offset_data::maybe_print_hole (struct ui_file
*stream
,
89 /* We check for END_BITPOS > 0 because there is a specific
90 scenario when END_BITPOS can be zero and BITPOS can be >
91 0: when we are dealing with a struct/class with a virtual method.
92 Because of the vtable, the first field of the struct/class will
93 have an offset of sizeof (void *) (the size of the vtable). If
94 we do not check for END_BITPOS > 0 here, GDB will report
95 a hole before the first field, which is not accurate. */
96 if (end_bitpos
> 0 && end_bitpos
< bitpos
)
98 /* If END_BITPOS is smaller than the current type's
99 bitpos, it means there's a hole in the struct, so we report
101 unsigned int hole
= bitpos
- end_bitpos
;
102 unsigned int hole_byte
= hole
/ TARGET_CHAR_BIT
;
103 unsigned int hole_bit
= hole
% TARGET_CHAR_BIT
;
107 fprintf_styled (stream
, highlight_style
.style (),
108 "/* XXX %2u-bit %-7s */", hole_bit
, for_what
);
109 gdb_puts ("\n", stream
);
114 fprintf_styled (stream
, highlight_style
.style (),
115 "/* XXX %2u-byte %-7s */", hole_byte
, for_what
);
116 gdb_puts ("\n", stream
);
121 /* See typeprint.h. */
124 print_offset_data::update (struct type
*type
, unsigned int field_idx
,
125 struct ui_file
*stream
)
127 if (type
->field (field_idx
).is_static ())
129 print_spaces (indentation
, stream
);
133 struct type
*ftype
= check_typedef (type
->field (field_idx
).type ());
134 if (type
->code () == TYPE_CODE_UNION
)
136 /* Since union fields don't have the concept of offsets, we just
137 print their sizes. */
138 gdb_printf (stream
, "/* %6s */",
140 hex_string_custom (ftype
->length (), 4) :
141 pulongest (ftype
->length ())));
145 unsigned int bitpos
= type
->field (field_idx
).loc_bitpos ();
146 unsigned int fieldsize_byte
= ftype
->length ();
147 unsigned int fieldsize_bit
= fieldsize_byte
* TARGET_CHAR_BIT
;
149 maybe_print_hole (stream
, bitpos
, "hole");
151 if (type
->field (field_idx
).is_packed ()
152 || offset_bitpos
% TARGET_CHAR_BIT
!= 0)
154 /* We're dealing with a bitfield. Print the bit offset. */
155 fieldsize_bit
= type
->field (field_idx
).bitsize ();
157 unsigned real_bitpos
= bitpos
+ offset_bitpos
;
160 (print_in_hex
? "/* 0x%04x: 0x%x" : "/* %6u:%2u "),
161 real_bitpos
/ TARGET_CHAR_BIT
,
162 real_bitpos
% TARGET_CHAR_BIT
);
166 /* The position of the field, relative to the beginning of the
168 gdb_printf (stream
, (print_in_hex
? "/* 0x%04x" : "/* %6u"),
169 (bitpos
+ offset_bitpos
) / TARGET_CHAR_BIT
);
171 gdb_printf (stream
, " ");
174 gdb_printf (stream
, (print_in_hex
? " | 0x%04x */" : " | %6u */"),
177 end_bitpos
= bitpos
+ fieldsize_bit
;
180 /* See typeprint.h. */
183 print_offset_data::finish (struct type
*type
, int level
,
184 struct ui_file
*stream
)
186 unsigned int bitpos
= type
->length () * TARGET_CHAR_BIT
;
187 maybe_print_hole (stream
, bitpos
, "padding");
189 gdb_puts ("\n", stream
);
190 print_spaces (level
+ 4 + print_offset_data::indentation
, stream
);
191 gdb_printf (stream
, "/* total size (bytes): %4s */\n",
192 pulongest (type
->length ()));
197 /* See typeprint.h. */
200 typedef_hash_table::recursively_update (struct type
*t
)
202 for (int i
= 0; i
< TYPE_TYPEDEF_FIELD_COUNT (t
); ++i
)
203 m_table
.emplace (&TYPE_TYPEDEF_FIELD (t
, i
));
205 /* Recurse into superclasses. */
206 for (int i
= 0; i
< TYPE_N_BASECLASSES (t
); ++i
)
207 recursively_update (TYPE_BASECLASS (t
, i
));
210 /* See typeprint.h. */
213 typedef_hash_table::add_template_parameters (struct type
*t
)
217 for (i
= 0; i
< TYPE_N_TEMPLATE_ARGUMENTS (t
); ++i
)
219 struct decl_field
*tf
;
221 /* We only want type-valued template parameters in the hash. */
222 if (TYPE_TEMPLATE_ARGUMENT (t
, i
)->aclass () != LOC_TYPEDEF
)
225 tf
= XOBNEW (&m_storage
, struct decl_field
);
226 tf
->name
= TYPE_TEMPLATE_ARGUMENT (t
, i
)->linkage_name ();
227 tf
->type
= TYPE_TEMPLATE_ARGUMENT (t
, i
)->type ();
229 m_table
.emplace (tf
);
233 /* Look up the type T in the global typedef hash. If it is found,
234 return the typedef name. If it is not found, apply the
235 type-printers, if any, given by start_script_type_printers and return the
236 result. A NULL return means that the name was not found. */
239 typedef_hash_table::find_global_typedef (const struct type_print_options
*flags
,
242 if (flags
->global_typedefs
== NULL
)
245 if (auto it
= flags
->global_typedefs
->m_table
.find (t
);
246 it
!= flags
->global_typedefs
->m_table
.end ())
249 /* Put an entry into the hash table now, in case
250 apply_ext_lang_type_printers recurses. */
252 = XOBNEW (&flags
->global_typedefs
->m_storage
, struct decl_field
);
256 flags
->global_typedefs
->m_table
.emplace (new_tf
);
258 gdb::unique_xmalloc_ptr
<char> applied
259 = apply_ext_lang_type_printers (flags
->global_printers
, t
);
261 if (applied
!= nullptr)
262 new_tf
->name
= obstack_strdup (&flags
->global_typedefs
->m_storage
,
268 /* See typeprint.h. */
271 typedef_hash_table::find_typedef (const struct type_print_options
*flags
,
274 if (flags
->local_typedefs
!= NULL
)
276 if (auto iter
= flags
->local_typedefs
->m_table
.find (t
);
277 iter
!= flags
->local_typedefs
->m_table
.end ())
278 return (*iter
)->name
;
281 return find_global_typedef (flags
, t
);
286 /* Print a description of a type in the format of a
287 typedef for the current language.
288 NEW is the new name for a type TYPE. */
291 typedef_print (struct type
*type
, struct symbol
*newobj
, struct ui_file
*stream
)
293 current_language
->print_typedef (type
, newobj
, stream
);
296 /* Print a description of a type TYPE in the form of a declaration of a
297 variable named VARSTRING. (VARSTRING is demangled if necessary.)
298 Output goes to STREAM (via stdio).
299 If SHOW is positive, we show the contents of the outermost level
300 of structure even if there is a type name that could be used instead.
301 If SHOW is negative, we never show the details of elements' types. */
304 type_print (struct type
*type
, const char *varstring
, struct ui_file
*stream
,
307 current_language
->print_type (type
, varstring
, stream
, show
, 0,
308 &default_ptype_flags
);
311 /* Print TYPE to a string, returning it. The caller is responsible for
312 freeing the string. */
315 type_to_string (struct type
*type
)
321 type_print (type
, "", &stb
, -1);
322 return stb
.release ();
324 catch (const gdb_exception
&except
)
331 /* See typeprint.h. */
334 type_print_unknown_return_type (struct ui_file
*stream
)
336 fprintf_styled (stream
, metadata_style
.style (),
337 _("<unknown return type>"));
340 /* See typeprint.h. */
343 error_unknown_type (const char *sym_print_name
)
345 error (_("'%s' has unknown type; cast it to its declared type"),
349 /* Print type of EXP, or last thing in value history if EXP == NULL.
350 show is passed to type_print. */
353 whatis_exp (const char *exp
, int show
)
356 struct type
*real_type
= NULL
;
361 struct value_print_options opts
;
362 struct type_print_options flags
= default_ptype_flags
;
370 for (++exp
; *exp
&& !isspace (*exp
); ++exp
)
378 flags
.print_methods
= 0;
381 flags
.print_methods
= 1;
384 flags
.print_typedefs
= 0;
387 flags
.print_typedefs
= 1;
391 /* Filter out languages which don't implement the
394 && current_language
->can_print_type_offsets ())
396 flags
.print_offsets
= 1;
397 flags
.print_typedefs
= 0;
398 flags
.print_methods
= 0;
403 flags
.print_in_hex
= 1;
406 flags
.print_in_hex
= 0;
409 error (_("unrecognized flag '%c'"), *exp
);
414 if (!*exp
&& !seen_one
)
415 error (_("flag expected"));
417 error (_("expected space after format"));
418 exp
= skip_spaces (exp
);
421 expression_up expr
= parse_expression (exp
);
423 /* The behavior of "whatis" depends on whether the user
424 expression names a type directly, or a language expression
425 (including variable names). If the former, then "whatis"
426 strips one level of typedefs, only. If an expression,
427 "whatis" prints the type of the expression without stripping
428 any typedef level. "ptype" always strips all levels of
430 val
= expr
->evaluate_type ();
433 if (show
== -1 && expr
->type_p ())
435 /* The user expression names a type directly. */
437 /* If this is a typedef, then find its immediate target.
438 Use check_typedef to resolve stubs, but ignore its result
439 because we do not want to dig past all typedefs. */
440 check_typedef (type
);
441 if (type
->code () == TYPE_CODE_TYPEDEF
)
442 type
= type
->target_type ();
444 /* If the expression is actually a type, then there's no
445 value to fetch the dynamic type from. */
451 val
= access_value_history (0);
455 if (flags
.print_offsets
&& is_dynamic_type (type
))
457 warning (_("ptype/o does not work with dynamic types; disabling '/o'"));
458 flags
.print_offsets
= 0;
461 get_user_print_options (&opts
);
462 if (val
!= NULL
&& opts
.objectprint
)
464 if (type
->is_pointer_or_reference ()
465 && (type
->target_type ()->code () == TYPE_CODE_STRUCT
))
466 real_type
= value_rtti_indirect_type (val
, &full
, &top
, &using_enc
);
467 else if (type
->code () == TYPE_CODE_STRUCT
)
468 real_type
= value_rtti_type (val
, &full
, &top
, &using_enc
);
471 if (flags
.print_offsets
472 && (type
->code () == TYPE_CODE_STRUCT
473 || type
->code () == TYPE_CODE_UNION
))
474 gdb_printf ("/* offset | size */ ");
476 gdb_printf ("type = ");
478 std::unique_ptr
<typedef_hash_table
> table_holder
;
479 std::unique_ptr
<ext_lang_type_printers
> printer_holder
;
482 table_holder
= std::make_unique
<typedef_hash_table
> ();
483 flags
.global_typedefs
= table_holder
.get ();
485 printer_holder
= std::make_unique
<ext_lang_type_printers
> ();
486 flags
.global_printers
= printer_holder
.get ();
491 gdb_printf ("/* real type = ");
492 type_print (real_type
, "", gdb_stdout
, -1);
494 gdb_printf (" (incomplete object)");
495 gdb_printf (" */\n");
498 current_language
->print_type (type
, "", gdb_stdout
, show
, 0, &flags
);
503 whatis_command (const char *exp
, int from_tty
)
505 /* Most of the time users do not want to see all the fields
506 in a structure. If they do they can use the "ptype" command.
507 Hence the "-1" below. */
508 whatis_exp (exp
, -1);
511 /* TYPENAME is either the name of a type, or an expression. */
514 ptype_command (const char *type_name
, int from_tty
)
516 whatis_exp (type_name
, 1);
519 /* Print integral scalar data VAL, of type TYPE, onto stdio stream STREAM.
520 Used to print data from type structures in a specified type. For example,
521 array bounds may be characters or booleans in some languages, and this
522 allows the ranges to be printed in their "natural" form rather than as
523 decimal integer values.
525 FIXME: This is here simply because only the type printing routines
526 currently use it, and it wasn't clear if it really belonged somewhere
527 else (like printcmd.c). There are a lot of other gdb routines that do
528 something similar, but they are generally concerned with printing values
529 that come from the inferior in target byte order and target size. */
532 print_type_scalar (struct type
*type
, LONGEST val
, struct ui_file
*stream
)
537 type
= check_typedef (type
);
539 switch (type
->code ())
543 len
= type
->num_fields ();
544 for (i
= 0; i
< len
; i
++)
546 if (type
->field (i
).loc_enumval () == val
)
553 gdb_puts (type
->field (i
).name (), stream
);
557 print_longest (stream
, 'd', 0, val
);
562 print_longest (stream
, type
->is_unsigned () ? 'u' : 'd', 0, val
);
566 current_language
->printchar ((unsigned char) val
, type
, stream
);
570 gdb_printf (stream
, val
? "TRUE" : "FALSE");
573 case TYPE_CODE_RANGE
:
574 print_type_scalar (type
->target_type (), val
, stream
);
577 case TYPE_CODE_FIXED_POINT
:
578 print_type_fixed_point (type
, stream
);
581 case TYPE_CODE_UNDEF
:
583 case TYPE_CODE_ARRAY
:
584 case TYPE_CODE_STRUCT
:
585 case TYPE_CODE_UNION
:
590 case TYPE_CODE_STRING
:
591 case TYPE_CODE_ERROR
:
592 case TYPE_CODE_MEMBERPTR
:
593 case TYPE_CODE_METHODPTR
:
594 case TYPE_CODE_METHOD
:
596 case TYPE_CODE_RVALUE_REF
:
597 case TYPE_CODE_NAMESPACE
:
598 error (_("internal error: unhandled type in print_type_scalar"));
602 error (_("Invalid type code in symbol table."));
606 /* See typeprint.h. */
609 print_type_fixed_point (struct type
*type
, struct ui_file
*stream
)
611 std::string small_img
= type
->fixed_point_scaling_factor ().str ();
613 gdb_printf (stream
, "%s-byte fixed point (small = %s)",
614 pulongest (type
->length ()), small_img
.c_str ());
617 /* Dump details of a type specified either directly or indirectly.
618 Uses the same sort of type lookup mechanism as ptype_command()
619 and whatis_command(). */
622 maintenance_print_type (const char *type_name
, int from_tty
)
624 if (type_name
!= NULL
)
626 expression_up expr
= parse_expression (type_name
);
627 struct value
*val
= expr
->evaluate_type ();
628 struct type
*type
= val
->type ();
631 recursive_dump_type (type
, 0);
636 struct cmd_list_element
*setprinttypelist
;
638 struct cmd_list_element
*showprinttypelist
;
640 static bool print_methods
= true;
643 set_print_type_methods (const char *args
,
644 int from_tty
, struct cmd_list_element
*c
)
646 default_ptype_flags
.print_methods
= print_methods
;
650 show_print_type_methods (struct ui_file
*file
, int from_tty
,
651 struct cmd_list_element
*c
, const char *value
)
653 gdb_printf (file
, _("Printing of methods defined in a class in %s\n"),
657 static bool print_typedefs
= true;
660 set_print_type_typedefs (const char *args
,
661 int from_tty
, struct cmd_list_element
*c
)
663 default_ptype_flags
.print_typedefs
= print_typedefs
;
667 show_print_type_typedefs (struct ui_file
*file
, int from_tty
,
668 struct cmd_list_element
*c
, const char *value
)
670 gdb_printf (file
, _("Printing of typedefs defined in a class in %s\n"),
674 /* Limit on the number of nested type definitions to print or -1 to print
675 all nested type definitions in a class. By default, we do not print
676 nested definitions. */
678 static int print_nested_type_limit
= 0;
680 /* Set how many nested type definitions should be printed by the type
684 set_print_type_nested_types (const char *args
, int from_tty
,
685 struct cmd_list_element
*c
)
687 default_ptype_flags
.print_nested_type_limit
= print_nested_type_limit
;
690 /* Show how many nested type definitions the type printer will print. */
693 show_print_type_nested_types (struct ui_file
*file
, int from_tty
,
694 struct cmd_list_element
*c
, const char *value
)
699 _("Will not print nested types defined in a class\n"));
704 _("Will print %s nested types defined in a class\n"),
709 /* When printing structs, offsets and sizes of members can be displayed using
710 decimal notation or hexadecimal notation. By default, Decimal notation is
713 static bool print_offsets_and_sizes_in_hex
= false;
715 /* Set the flags that instructs if sizes and offsets of struct members are
716 displayed in hexadecimal or decimal notation. */
719 set_print_offsets_and_sizes_in_hex (const char *args
,
720 int from_tty
, struct cmd_list_element
*c
)
722 default_ptype_flags
.print_in_hex
= print_offsets_and_sizes_in_hex
;
725 /* Display whether struct members sizes and offsets are printed
726 using decimal or hexadecimal notation. */
729 show_print_offsets_and_sizes_in_hex (struct ui_file
*file
, int from_tty
,
730 struct cmd_list_element
*c
,
733 gdb_printf (file
, _("\
734 Display of struct members offsets and sizes in hexadecimal is %s\n"),
738 void _initialize_typeprint ();
740 _initialize_typeprint ()
742 struct cmd_list_element
*c
;
744 c
= add_com ("ptype", class_vars
, ptype_command
, _("\
745 Print definition of type TYPE.\n\
746 Usage: ptype[/FLAGS] TYPE | EXPRESSION\n\
747 Argument may be any type (for example a type name defined by typedef,\n\
748 or \"struct STRUCT-TAG\" or \"class CLASS-NAME\" or \"union UNION-TAG\"\n\
749 or \"enum ENUM-TAG\") or an expression.\n\
750 The selected stack frame's lexical context is used to look up the name.\n\
751 Contrary to \"whatis\", \"ptype\" always unrolls any typedefs.\n\
753 Available FLAGS are:\n\
754 /r print in \"raw\" form; do not substitute typedefs\n\
755 /m do not print methods defined in a class\n\
756 /M print methods defined in a class\n\
757 /t do not print typedefs defined in a class\n\
758 /T print typedefs defined in a class\n\
759 /o print offsets and sizes of fields in a struct (like pahole)\n\
760 /x use hexadecimal notation when displaying sizes and offsets\n\
762 /d use decimal notation when displaying sizes and offsets\n\
763 of struct members"));
764 set_cmd_completer (c
, expression_completer
);
766 c
= add_com ("whatis", class_vars
, whatis_command
,
767 _("Print data type of expression EXP.\n\
768 Only one level of typedefs is unrolled. See also \"ptype\"."));
769 set_cmd_completer (c
, expression_completer
);
771 add_setshow_prefix_cmd ("type", no_class
,
772 _("Generic command for showing type-printing settings."),
773 _("Generic command for setting how types print."),
774 &setprinttypelist
, &showprinttypelist
,
775 &setprintlist
, &showprintlist
);
777 add_setshow_boolean_cmd ("methods", no_class
, &print_methods
,
779 Set printing of methods defined in classes."), _("\
780 Show printing of methods defined in classes."), NULL
,
781 set_print_type_methods
,
782 show_print_type_methods
,
783 &setprinttypelist
, &showprinttypelist
);
784 add_setshow_boolean_cmd ("typedefs", no_class
, &print_typedefs
,
786 Set printing of typedefs defined in classes."), _("\
787 Show printing of typedefs defined in classes."), NULL
,
788 set_print_type_typedefs
,
789 show_print_type_typedefs
,
790 &setprinttypelist
, &showprinttypelist
);
792 add_setshow_zuinteger_unlimited_cmd ("nested-type-limit", no_class
,
793 &print_nested_type_limit
,
795 Set the number of recursive nested type definitions to print.\n\
796 Use \"unlimited\" or -1 to show all."), _("\
797 Show the number of recursive nested type definitions to print."), NULL
,
798 set_print_type_nested_types
,
799 show_print_type_nested_types
,
800 &setprinttypelist
, &showprinttypelist
);
802 add_setshow_boolean_cmd ("hex", no_class
, &print_offsets_and_sizes_in_hex
,
804 Set printing of struct members sizes and offsets using hex notation."), _("\
805 Show whether sizes and offsets of struct members are printed using hex \
806 notation."), nullptr, set_print_offsets_and_sizes_in_hex
,
807 show_print_offsets_and_sizes_in_hex
,
808 &setprinttypelist
, &showprinttypelist
);
811 /* Print <not allocated> status to stream STREAM. */
814 val_print_not_allocated (struct ui_file
*stream
)
816 fprintf_styled (stream
, metadata_style
.style (), _("<not allocated>"));
819 /* Print <not associated> status to stream STREAM. */
822 val_print_not_associated (struct ui_file
*stream
)
824 fprintf_styled (stream
, metadata_style
.style (), _("<not associated>"));