1 /* Language independent support for printing types for GDB, the GNU debugger.
3 Copyright (C) 1986-2018 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"
40 const struct type_print_options type_print_raw_options =
43 1, /* print_methods */
44 1, /* print_typedefs */
45 0, /* print_offsets */
46 0, /* print_nested_type_limit */
47 NULL, /* local_typedefs */
48 NULL, /* global_table */
49 NULL /* global_printers */
52 /* The default flags for 'ptype' and 'whatis'. */
54 static struct type_print_options default_ptype_flags =
57 1, /* print_methods */
58 1, /* print_typedefs */
59 0, /* print_offsets */
60 0, /* print_nested_type_limit */
61 NULL, /* local_typedefs */
62 NULL, /* global_table */
63 NULL /* global_printers */
68 /* See typeprint.h. */
70 const int print_offset_data::indentation = 23;
73 /* See typeprint.h. */
76 print_offset_data::maybe_print_hole (struct ui_file *stream,
80 /* We check for END_BITPOS > 0 because there is a specific
81 scenario when END_BITPOS can be zero and BITPOS can be >
82 0: when we are dealing with a struct/class with a virtual method.
83 Because of the vtable, the first field of the struct/class will
84 have an offset of sizeof (void *) (the size of the vtable). If
85 we do not check for END_BITPOS > 0 here, GDB will report
86 a hole before the first field, which is not accurate. */
87 if (end_bitpos > 0 && end_bitpos < bitpos)
89 /* If END_BITPOS is smaller than the current type's
90 bitpos, it means there's a hole in the struct, so we report
92 unsigned int hole = bitpos - end_bitpos;
93 unsigned int hole_byte = hole / TARGET_CHAR_BIT;
94 unsigned int hole_bit = hole % TARGET_CHAR_BIT;
97 fprintf_filtered (stream, "/* XXX %2u-bit %s */\n", hole_bit,
101 fprintf_filtered (stream, "/* XXX %2u-byte %s */\n", hole_byte,
106 /* See typeprint.h. */
109 print_offset_data::update (struct type *type, unsigned int field_idx,
110 struct ui_file *stream)
112 if (field_is_static (&TYPE_FIELD (type, field_idx)))
114 print_spaces_filtered (indentation, stream);
118 struct type *ftype = check_typedef (TYPE_FIELD_TYPE (type, field_idx));
119 if (TYPE_CODE (type) == TYPE_CODE_UNION)
121 /* Since union fields don't have the concept of offsets, we just
122 print their sizes. */
123 fprintf_filtered (stream, "/* %4u */", TYPE_LENGTH (ftype));
127 unsigned int bitpos = TYPE_FIELD_BITPOS (type, field_idx);
128 unsigned int fieldsize_byte = TYPE_LENGTH (ftype);
129 unsigned int fieldsize_bit = fieldsize_byte * TARGET_CHAR_BIT;
131 maybe_print_hole (stream, bitpos, "hole");
133 if (TYPE_FIELD_PACKED (type, field_idx))
135 /* We're dealing with a bitfield. Print how many bits are left
137 unsigned int bitsize = TYPE_FIELD_BITSIZE (type, field_idx);
138 /* The bitpos relative to the beginning of our container
140 unsigned int relative_bitpos;
142 /* The following was copied from
143 value.c:value_primitive_field. */
144 if ((bitpos % fieldsize_bit) + bitsize <= fieldsize_bit)
145 relative_bitpos = bitpos % fieldsize_bit;
147 relative_bitpos = bitpos % TARGET_CHAR_BIT;
149 /* This is the exact offset (in bits) of this bitfield. */
150 unsigned int bit_offset
151 = (bitpos - relative_bitpos) + offset_bitpos;
153 /* The position of the field, relative to the beginning of the
154 struct, and how many bits are left to be used in this
156 fprintf_filtered (stream, "/* %4u:%2u", bit_offset / TARGET_CHAR_BIT,
157 fieldsize_bit - (relative_bitpos + bitsize));
158 fieldsize_bit = bitsize;
162 /* The position of the field, relative to the beginning of the
164 fprintf_filtered (stream, "/* %4u",
165 (bitpos + offset_bitpos) / TARGET_CHAR_BIT);
167 fprintf_filtered (stream, " ");
170 fprintf_filtered (stream, " | %4u */", fieldsize_byte);
172 end_bitpos = bitpos + fieldsize_bit;
175 /* See typeprint.h. */
178 print_offset_data::finish (struct type *type, int level,
179 struct ui_file *stream)
181 unsigned int bitpos = TYPE_LENGTH (type) * TARGET_CHAR_BIT;
182 maybe_print_hole (stream, bitpos, "padding");
184 fputs_filtered ("\n", stream);
185 print_spaces_filtered (level + 4 + print_offset_data::indentation, stream);
186 fprintf_filtered (stream, "/* total size (bytes): %4u */\n",
192 /* A hash function for a typedef_field. */
195 hash_typedef_field (const void *p)
197 const struct decl_field *tf = (const struct decl_field *) p;
198 struct type *t = check_typedef (tf->type);
200 return htab_hash_string (TYPE_SAFE_NAME (t));
203 /* An equality function for a typedef field. */
206 eq_typedef_field (const void *a, const void *b)
208 const struct decl_field *tfa = (const struct decl_field *) a;
209 const struct decl_field *tfb = (const struct decl_field *) b;
211 return types_equal (tfa->type, tfb->type);
214 /* See typeprint.h. */
217 typedef_hash_table::recursively_update (struct type *t)
221 for (i = 0; i < TYPE_TYPEDEF_FIELD_COUNT (t); ++i)
223 struct decl_field *tdef = &TYPE_TYPEDEF_FIELD (t, i);
226 slot = htab_find_slot (m_table, tdef, INSERT);
227 /* Only add a given typedef name once. Really this shouldn't
228 happen; but it is safe enough to do the updates breadth-first
229 and thus use the most specific typedef. */
234 /* Recurse into superclasses. */
235 for (i = 0; i < TYPE_N_BASECLASSES (t); ++i)
236 recursively_update (TYPE_BASECLASS (t, i));
239 /* See typeprint.h. */
242 typedef_hash_table::add_template_parameters (struct type *t)
246 for (i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (t); ++i)
248 struct decl_field *tf;
251 /* We only want type-valued template parameters in the hash. */
252 if (SYMBOL_CLASS (TYPE_TEMPLATE_ARGUMENT (t, i)) != LOC_TYPEDEF)
255 tf = XOBNEW (&m_storage, struct decl_field);
256 tf->name = SYMBOL_LINKAGE_NAME (TYPE_TEMPLATE_ARGUMENT (t, i));
257 tf->type = SYMBOL_TYPE (TYPE_TEMPLATE_ARGUMENT (t, i));
259 slot = htab_find_slot (m_table, tf, INSERT);
265 /* See typeprint.h. */
267 typedef_hash_table::typedef_hash_table ()
269 m_table = htab_create_alloc (10, hash_typedef_field, eq_typedef_field,
270 NULL, xcalloc, xfree);
273 /* Free a typedef field table. */
275 typedef_hash_table::~typedef_hash_table ()
277 htab_delete (m_table);
280 /* Helper function for typedef_hash_table::copy. */
283 copy_typedef_hash_element (void **slot, void *nt)
285 htab_t new_table = (htab_t) nt;
288 new_slot = htab_find_slot (new_table, *slot, INSERT);
289 if (*new_slot == NULL)
295 /* See typeprint.h. */
297 typedef_hash_table::typedef_hash_table (const typedef_hash_table &table)
299 m_table = htab_create_alloc (10, hash_typedef_field, eq_typedef_field,
300 NULL, xcalloc, xfree);
301 htab_traverse_noresize (table.m_table, copy_typedef_hash_element,
305 /* Look up the type T in the global typedef hash. If it is found,
306 return the typedef name. If it is not found, apply the
307 type-printers, if any, given by start_script_type_printers and return the
308 result. A NULL return means that the name was not found. */
311 typedef_hash_table::find_global_typedef (const struct type_print_options *flags,
316 struct decl_field tf, *new_tf;
318 if (flags->global_typedefs == NULL)
324 slot = htab_find_slot (flags->global_typedefs->m_table, &tf, INSERT);
327 new_tf = (struct decl_field *) *slot;
331 /* Put an entry into the hash table now, in case
332 apply_ext_lang_type_printers recurses. */
333 new_tf = XOBNEW (&flags->global_typedefs->m_storage, struct decl_field);
339 applied = apply_ext_lang_type_printers (flags->global_printers, t);
344 = (const char *) obstack_copy0 (&flags->global_typedefs->m_storage,
345 applied, strlen (applied));
352 /* See typeprint.h. */
355 typedef_hash_table::find_typedef (const struct type_print_options *flags,
358 if (flags->local_typedefs != NULL)
360 struct decl_field tf, *found;
364 found = (struct decl_field *) htab_find (flags->local_typedefs->m_table,
371 return find_global_typedef (flags, t);
376 /* Print a description of a type in the format of a
377 typedef for the current language.
378 NEW is the new name for a type TYPE. */
381 typedef_print (struct type *type, struct symbol *newobj, struct ui_file *stream)
383 LA_PRINT_TYPEDEF (type, newobj, stream);
386 /* The default way to print a typedef. */
389 default_print_typedef (struct type *type, struct symbol *new_symbol,
390 struct ui_file *stream)
392 error (_("Language not supported."));
395 /* Print a description of a type TYPE in the form of a declaration of a
396 variable named VARSTRING. (VARSTRING is demangled if necessary.)
397 Output goes to STREAM (via stdio).
398 If SHOW is positive, we show the contents of the outermost level
399 of structure even if there is a type name that could be used instead.
400 If SHOW is negative, we never show the details of elements' types. */
403 type_print (struct type *type, const char *varstring, struct ui_file *stream,
406 LA_PRINT_TYPE (type, varstring, stream, show, 0, &default_ptype_flags);
409 /* Print TYPE to a string, returning it. The caller is responsible for
410 freeing the string. */
413 type_to_string (struct type *type)
419 type_print (type, "", &stb, -1);
420 return std::move (stb.string ());
422 CATCH (except, RETURN_MASK_ALL)
430 /* See typeprint.h. */
433 type_print_unknown_return_type (struct ui_file *stream)
435 fprintf_filtered (stream, _("<unknown return type>"));
438 /* See typeprint.h. */
441 error_unknown_type (const char *sym_print_name)
443 error (_("'%s' has unknown type; cast it to its declared type"),
447 /* Print type of EXP, or last thing in value history if EXP == NULL.
448 show is passed to type_print. */
451 whatis_exp (const char *exp, int show)
454 struct type *real_type = NULL;
459 struct value_print_options opts;
460 struct type_print_options flags = default_ptype_flags;
468 for (++exp; *exp && !isspace (*exp); ++exp)
476 flags.print_methods = 0;
479 flags.print_methods = 1;
482 flags.print_typedefs = 0;
485 flags.print_typedefs = 1;
489 /* Filter out languages which don't implement the
492 && (current_language->la_language == language_c
493 || current_language->la_language == language_cplus))
495 flags.print_offsets = 1;
496 flags.print_typedefs = 0;
497 flags.print_methods = 0;
502 error (_("unrecognized flag '%c'"), *exp);
507 if (!*exp && !seen_one)
508 error (_("flag expected"));
510 error (_("expected space after format"));
511 exp = skip_spaces (exp);
514 expression_up expr = parse_expression (exp);
516 /* The behavior of "whatis" depends on whether the user
517 expression names a type directly, or a language expression
518 (including variable names). If the former, then "whatis"
519 strips one level of typedefs, only. If an expression,
520 "whatis" prints the type of the expression without stripping
521 any typedef level. "ptype" always strips all levels of
523 if (show == -1 && expr->elts[0].opcode == OP_TYPE)
525 /* The user expression names a type directly. */
526 type = expr->elts[1].type;
528 /* If this is a typedef, then find its immediate target.
529 Use check_typedef to resolve stubs, but ignore its result
530 because we do not want to dig past all typedefs. */
531 check_typedef (type);
532 if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
533 type = TYPE_TARGET_TYPE (type);
535 /* If the expression is actually a type, then there's no
536 value to fetch the dynamic type from. */
541 /* The user expression names a type indirectly by naming an
542 object or expression of that type. Find that
543 indirectly-named type. */
544 val = evaluate_type (expr.get ());
545 type = value_type (val);
550 val = access_value_history (0);
551 type = value_type (val);
554 get_user_print_options (&opts);
555 if (val != NULL && opts.objectprint)
557 if (((TYPE_CODE (type) == TYPE_CODE_PTR) || TYPE_IS_REFERENCE (type))
558 && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_STRUCT))
559 real_type = value_rtti_indirect_type (val, &full, &top, &using_enc);
560 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
561 real_type = value_rtti_type (val, &full, &top, &using_enc);
564 if (flags.print_offsets
565 && (TYPE_CODE (type) == TYPE_CODE_STRUCT
566 || TYPE_CODE (type) == TYPE_CODE_UNION))
567 fprintf_filtered (gdb_stdout, "/* offset | size */ ");
569 printf_filtered ("type = ");
571 std::unique_ptr<typedef_hash_table> table_holder;
572 std::unique_ptr<ext_lang_type_printers> printer_holder;
575 table_holder.reset (new typedef_hash_table);
576 flags.global_typedefs = table_holder.get ();
578 printer_holder.reset (new ext_lang_type_printers);
579 flags.global_printers = printer_holder.get ();
584 printf_filtered ("/* real type = ");
585 type_print (real_type, "", gdb_stdout, -1);
587 printf_filtered (" (incomplete object)");
588 printf_filtered (" */\n");
591 LA_PRINT_TYPE (type, "", gdb_stdout, show, 0, &flags);
592 printf_filtered ("\n");
596 whatis_command (const char *exp, int from_tty)
598 /* Most of the time users do not want to see all the fields
599 in a structure. If they do they can use the "ptype" command.
600 Hence the "-1" below. */
601 whatis_exp (exp, -1);
604 /* TYPENAME is either the name of a type, or an expression. */
607 ptype_command (const char *type_name, int from_tty)
609 whatis_exp (type_name, 1);
612 /* Print integral scalar data VAL, of type TYPE, onto stdio stream STREAM.
613 Used to print data from type structures in a specified type. For example,
614 array bounds may be characters or booleans in some languages, and this
615 allows the ranges to be printed in their "natural" form rather than as
616 decimal integer values.
618 FIXME: This is here simply because only the type printing routines
619 currently use it, and it wasn't clear if it really belonged somewhere
620 else (like printcmd.c). There are a lot of other gdb routines that do
621 something similar, but they are generally concerned with printing values
622 that come from the inferior in target byte order and target size. */
625 print_type_scalar (struct type *type, LONGEST val, struct ui_file *stream)
630 type = check_typedef (type);
632 switch (TYPE_CODE (type))
636 len = TYPE_NFIELDS (type);
637 for (i = 0; i < len; i++)
639 if (TYPE_FIELD_ENUMVAL (type, i) == val)
646 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
650 print_longest (stream, 'd', 0, val);
655 print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0, val);
659 LA_PRINT_CHAR ((unsigned char) val, type, stream);
663 fprintf_filtered (stream, val ? "TRUE" : "FALSE");
666 case TYPE_CODE_RANGE:
667 print_type_scalar (TYPE_TARGET_TYPE (type), val, stream);
670 case TYPE_CODE_UNDEF:
672 case TYPE_CODE_ARRAY:
673 case TYPE_CODE_STRUCT:
674 case TYPE_CODE_UNION:
679 case TYPE_CODE_STRING:
680 case TYPE_CODE_ERROR:
681 case TYPE_CODE_MEMBERPTR:
682 case TYPE_CODE_METHODPTR:
683 case TYPE_CODE_METHOD:
685 case TYPE_CODE_RVALUE_REF:
686 case TYPE_CODE_NAMESPACE:
687 error (_("internal error: unhandled type in print_type_scalar"));
691 error (_("Invalid type code in symbol table."));
696 /* Dump details of a type specified either directly or indirectly.
697 Uses the same sort of type lookup mechanism as ptype_command()
698 and whatis_command(). */
701 maintenance_print_type (const char *type_name, int from_tty)
706 if (type_name != NULL)
708 expression_up expr = parse_expression (type_name);
709 if (expr->elts[0].opcode == OP_TYPE)
711 /* The user expression names a type directly, just use that type. */
712 type = expr->elts[1].type;
716 /* The user expression may name a type indirectly by naming an
717 object of that type. Find that indirectly named type. */
718 val = evaluate_type (expr.get ());
719 type = value_type (val);
723 recursive_dump_type (type, 0);
729 struct cmd_list_element *setprinttypelist;
731 struct cmd_list_element *showprinttypelist;
734 set_print_type (const char *arg, int from_tty)
737 "\"set print type\" must be followed by the name of a subcommand.\n");
738 help_list (setprintlist, "set print type ", all_commands, gdb_stdout);
742 show_print_type (const char *args, int from_tty)
744 cmd_show_list (showprinttypelist, from_tty, "");
747 static int print_methods = 1;
750 set_print_type_methods (const char *args,
751 int from_tty, struct cmd_list_element *c)
753 default_ptype_flags.print_methods = print_methods;
757 show_print_type_methods (struct ui_file *file, int from_tty,
758 struct cmd_list_element *c, const char *value)
760 fprintf_filtered (file, _("Printing of methods defined in a class in %s\n"),
764 static int print_typedefs = 1;
767 set_print_type_typedefs (const char *args,
768 int from_tty, struct cmd_list_element *c)
770 default_ptype_flags.print_typedefs = print_typedefs;
774 show_print_type_typedefs (struct ui_file *file, int from_tty,
775 struct cmd_list_element *c, const char *value)
777 fprintf_filtered (file, _("Printing of typedefs defined in a class in %s\n"),
781 /* Limit on the number of nested type definitions to print or -1 to print
782 all nested type definitions in a class. By default, we do not print
783 nested definitions. */
785 static int print_nested_type_limit = 0;
787 /* Set how many nested type definitions should be printed by the type
791 set_print_type_nested_types (const char *args, int from_tty,
792 struct cmd_list_element *c)
794 default_ptype_flags.print_nested_type_limit = print_nested_type_limit;
797 /* Show how many nested type definitions the type printer will print. */
800 show_print_type_nested_types (struct ui_file *file, int from_tty,
801 struct cmd_list_element *c, const char *value)
805 fprintf_filtered (file,
806 _("Will not print nested types defined in a class\n"));
810 fprintf_filtered (file,
811 _("Will print %s nested types defined in a class\n"),
817 _initialize_typeprint (void)
819 struct cmd_list_element *c;
821 c = add_com ("ptype", class_vars, ptype_command, _("\
822 Print definition of type TYPE.\n\
823 Usage: ptype[/FLAGS] TYPE | EXPRESSION\n\
824 Argument may be any type (for example a type name defined by typedef,\n\
825 or \"struct STRUCT-TAG\" or \"class CLASS-NAME\" or \"union UNION-TAG\"\n\
826 or \"enum ENUM-TAG\") or an expression.\n\
827 The selected stack frame's lexical context is used to look up the name.\n\
828 Contrary to \"whatis\", \"ptype\" always unrolls any typedefs.\n\
830 Available FLAGS are:\n\
831 /r print in \"raw\" form; do not substitute typedefs\n\
832 /m do not print methods defined in a class\n\
833 /M print methods defined in a class\n\
834 /t do not print typedefs defined in a class\n\
835 /T print typedefs defined in a class\n\
836 /o print offsets and sizes of fields in a struct (like pahole)\n"));
837 set_cmd_completer (c, expression_completer);
839 c = add_com ("whatis", class_vars, whatis_command,
840 _("Print data type of expression EXP.\n\
841 Only one level of typedefs is unrolled. See also \"ptype\"."));
842 set_cmd_completer (c, expression_completer);
844 add_prefix_cmd ("type", no_class, show_print_type,
845 _("Generic command for showing type-printing settings."),
846 &showprinttypelist, "show print type ", 0, &showprintlist);
847 add_prefix_cmd ("type", no_class, set_print_type,
848 _("Generic command for setting how types print."),
849 &setprinttypelist, "show print type ", 0, &setprintlist);
851 add_setshow_boolean_cmd ("methods", no_class, &print_methods,
853 Set printing of methods defined in classes."), _("\
854 Show printing of methods defined in classes."), NULL,
855 set_print_type_methods,
856 show_print_type_methods,
857 &setprinttypelist, &showprinttypelist);
858 add_setshow_boolean_cmd ("typedefs", no_class, &print_typedefs,
860 Set printing of typedefs defined in classes."), _("\
861 Show printing of typedefs defined in classes."), NULL,
862 set_print_type_typedefs,
863 show_print_type_typedefs,
864 &setprinttypelist, &showprinttypelist);
866 add_setshow_zuinteger_unlimited_cmd ("nested-type-limit", no_class,
867 &print_nested_type_limit,
869 Set the number of recursive nested type definitions to print \
870 (\"unlimited\" or -1 to show all)."), _("\
871 Show the number of recursive nested type definitions to print."), NULL,
872 set_print_type_nested_types,
873 show_print_type_nested_types,
874 &setprinttypelist, &showprinttypelist);
877 /* Print <not allocated> status to stream STREAM. */
880 val_print_not_allocated (struct ui_file *stream)
882 fprintf_filtered (stream, _("<not allocated>"));
885 /* Print <not associated> status to stream STREAM. */
888 val_print_not_associated (struct ui_file *stream)
890 fprintf_filtered (stream, _("<not associated>"));