gdb/typeprint.c - gdb

Global variables defined

Data types defined

Functions defined

Source code

  1. /* Language independent support for printing types for GDB, the GNU debugger.

  2.    Copyright (C) 1986-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "gdb_obstack.h"
  16. #include "bfd.h"                /* Binary File Description */
  17. #include "symtab.h"
  18. #include "gdbtypes.h"
  19. #include "expression.h"
  20. #include "value.h"
  21. #include "gdbcore.h"
  22. #include "command.h"
  23. #include "gdbcmd.h"
  24. #include "target.h"
  25. #include "language.h"
  26. #include "cp-abi.h"
  27. #include "typeprint.h"
  28. #include "valprint.h"
  29. #include <ctype.h>
  30. #include "cli/cli-utils.h"
  31. #include "extension.h"
  32. #include "completer.h"

  33. extern void _initialize_typeprint (void);

  34. static void ptype_command (char *, int);

  35. static void whatis_command (char *, int);

  36. static void whatis_exp (char *, int);

  37. const struct type_print_options type_print_raw_options =
  38. {
  39.   1,                                /* raw */
  40.   1,                                /* print_methods */
  41.   1,                                /* print_typedefs */
  42.   NULL,                                /* local_typedefs */
  43.   NULL,                                /* global_table */
  44.   NULL                                /* global_printers */
  45. };

  46. /* The default flags for 'ptype' and 'whatis'.  */

  47. static struct type_print_options default_ptype_flags =
  48. {
  49.   0,                                /* raw */
  50.   1,                                /* print_methods */
  51.   1,                                /* print_typedefs */
  52.   NULL,                                /* local_typedefs */
  53.   NULL,                                /* global_table */
  54.   NULL                                /* global_printers */
  55. };



  56. /* A hash table holding typedef_field objects.  This is more
  57.    complicated than an ordinary hash because it must also track the
  58.    lifetime of some -- but not all -- of the contained objects.  */

  59. struct typedef_hash_table
  60. {
  61.   /* The actual hash table.  */
  62.   htab_t table;

  63.   /* Storage for typedef_field objects that must be synthesized.  */
  64.   struct obstack storage;
  65. };

  66. /* A hash function for a typedef_field.  */

  67. static hashval_t
  68. hash_typedef_field (const void *p)
  69. {
  70.   const struct typedef_field *tf = p;
  71.   struct type *t = check_typedef (tf->type);

  72.   return htab_hash_string (TYPE_SAFE_NAME (t));
  73. }

  74. /* An equality function for a typedef field.  */

  75. static int
  76. eq_typedef_field (const void *a, const void *b)
  77. {
  78.   const struct typedef_field *tfa = a;
  79.   const struct typedef_field *tfb = b;

  80.   return types_equal (tfa->type, tfb->type);
  81. }

  82. /* Add typedefs from T to the hash table TABLE.  */

  83. void
  84. recursively_update_typedef_hash (struct typedef_hash_table *table,
  85.                                  struct type *t)
  86. {
  87.   int i;

  88.   if (table == NULL)
  89.     return;

  90.   for (i = 0; i < TYPE_TYPEDEF_FIELD_COUNT (t); ++i)
  91.     {
  92.       struct typedef_field *tdef = &TYPE_TYPEDEF_FIELD (t, i);
  93.       void **slot;

  94.       slot = htab_find_slot (table->table, tdef, INSERT);
  95.       /* Only add a given typedef name once.  Really this shouldn't
  96.          happen; but it is safe enough to do the updates breadth-first
  97.          and thus use the most specific typedef.  */
  98.       if (*slot == NULL)
  99.         *slot = tdef;
  100.     }

  101.   /* Recurse into superclasses.  */
  102.   for (i = 0; i < TYPE_N_BASECLASSES (t); ++i)
  103.     recursively_update_typedef_hash (table, TYPE_BASECLASS (t, i));
  104. }

  105. /* Add template parameters from T to the typedef hash TABLE.  */

  106. void
  107. add_template_parameters (struct typedef_hash_table *table, struct type *t)
  108. {
  109.   int i;

  110.   if (table == NULL)
  111.     return;

  112.   for (i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (t); ++i)
  113.     {
  114.       struct typedef_field *tf;
  115.       void **slot;

  116.       /* We only want type-valued template parameters in the hash.  */
  117.       if (SYMBOL_CLASS (TYPE_TEMPLATE_ARGUMENT (t, i)) != LOC_TYPEDEF)
  118.         continue;

  119.       tf = XOBNEW (&table->storage, struct typedef_field);
  120.       tf->name = SYMBOL_LINKAGE_NAME (TYPE_TEMPLATE_ARGUMENT (t, i));
  121.       tf->type = SYMBOL_TYPE (TYPE_TEMPLATE_ARGUMENT (t, i));

  122.       slot = htab_find_slot (table->table, tf, INSERT);
  123.       if (*slot == NULL)
  124.         *slot = tf;
  125.     }
  126. }

  127. /* Create a new typedef-lookup hash table.  */

  128. struct typedef_hash_table *
  129. create_typedef_hash (void)
  130. {
  131.   struct typedef_hash_table *result;

  132.   result = XNEW (struct typedef_hash_table);
  133.   result->table = htab_create_alloc (10, hash_typedef_field, eq_typedef_field,
  134.                                      NULL, xcalloc, xfree);
  135.   obstack_init (&result->storage);

  136.   return result;
  137. }

  138. /* Free a typedef field table.  */

  139. void
  140. free_typedef_hash (struct typedef_hash_table *table)
  141. {
  142.   if (table != NULL)
  143.     {
  144.       htab_delete (table->table);
  145.       obstack_free (&table->storage, NULL);
  146.       xfree (table);
  147.     }
  148. }

  149. /* A cleanup for freeing a typedef_hash_table.  */

  150. static void
  151. do_free_typedef_hash (void *arg)
  152. {
  153.   free_typedef_hash (arg);
  154. }

  155. /* Return a new cleanup that frees TABLE.  */

  156. struct cleanup *
  157. make_cleanup_free_typedef_hash (struct typedef_hash_table *table)
  158. {
  159.   return make_cleanup (do_free_typedef_hash, table);
  160. }

  161. /* Helper function for copy_typedef_hash.  */

  162. static int
  163. copy_typedef_hash_element (void **slot, void *nt)
  164. {
  165.   htab_t new_table = nt;
  166.   void **new_slot;

  167.   new_slot = htab_find_slot (new_table, *slot, INSERT);
  168.   if (*new_slot == NULL)
  169.     *new_slot = *slot;

  170.   return 1;
  171. }

  172. /* Copy a typedef hash.  */

  173. struct typedef_hash_table *
  174. copy_typedef_hash (struct typedef_hash_table *table)
  175. {
  176.   struct typedef_hash_table *result;

  177.   if (table == NULL)
  178.     return NULL;

  179.   result = create_typedef_hash ();
  180.   htab_traverse_noresize (table->table, copy_typedef_hash_element,
  181.                           result->table);
  182.   return result;
  183. }

  184. /* A cleanup to free the global typedef hash.  */

  185. static void
  186. do_free_global_table (void *arg)
  187. {
  188.   struct type_print_options *flags = arg;

  189.   free_typedef_hash (flags->global_typedefs);
  190.   free_ext_lang_type_printers (flags->global_printers);
  191. }

  192. /* Create the global typedef hash.  */

  193. static struct cleanup *
  194. create_global_typedef_table (struct type_print_options *flags)
  195. {
  196.   gdb_assert (flags->global_typedefs == NULL && flags->global_printers == NULL);
  197.   flags->global_typedefs = create_typedef_hash ();
  198.   flags->global_printers = start_ext_lang_type_printers ();
  199.   return make_cleanup (do_free_global_table, flags);
  200. }

  201. /* Look up the type T in the global typedef hash.  If it is found,
  202.    return the typedef name.  If it is not found, apply the
  203.    type-printers, if any, given by start_script_type_printers and return the
  204.    resultA NULL return means that the name was not found.  */

  205. static const char *
  206. find_global_typedef (const struct type_print_options *flags,
  207.                      struct type *t)
  208. {
  209.   char *applied;
  210.   void **slot;
  211.   struct typedef_field tf, *new_tf;

  212.   if (flags->global_typedefs == NULL)
  213.     return NULL;

  214.   tf.name = NULL;
  215.   tf.type = t;

  216.   slot = htab_find_slot (flags->global_typedefs->table, &tf, INSERT);
  217.   if (*slot != NULL)
  218.     {
  219.       new_tf = *slot;
  220.       return new_tf->name;
  221.     }

  222.   /* Put an entry into the hash table now, in case
  223.      apply_ext_lang_type_printers recurses.  */
  224.   new_tf = XOBNEW (&flags->global_typedefs->storage, struct typedef_field);
  225.   new_tf->name = NULL;
  226.   new_tf->type = t;

  227.   *slot = new_tf;

  228.   applied = apply_ext_lang_type_printers (flags->global_printers, t);

  229.   if (applied != NULL)
  230.     {
  231.       new_tf->name = obstack_copy0 (&flags->global_typedefs->storage, applied,
  232.                                     strlen (applied));
  233.       xfree (applied);
  234.     }

  235.   return new_tf->name;
  236. }

  237. /* Look up the type T in the typedef hash table in with FLAGS.  If T
  238.    is in the table, return its short (class-relative) typedef name.
  239.    Otherwise return NULL.  If the table is NULL, this always returns
  240.    NULL.  */

  241. const char *
  242. find_typedef_in_hash (const struct type_print_options *flags, struct type *t)
  243. {
  244.   if (flags->local_typedefs != NULL)
  245.     {
  246.       struct typedef_field tf, *found;

  247.       tf.name = NULL;
  248.       tf.type = t;
  249.       found = htab_find (flags->local_typedefs->table, &tf);

  250.       if (found != NULL)
  251.         return found->name;
  252.     }

  253.   return find_global_typedef (flags, t);
  254. }



  255. /* Print a description of a type in the format of a
  256.    typedef for the current language.
  257.    NEW is the new name for a type TYPE.  */

  258. void
  259. typedef_print (struct type *type, struct symbol *new, struct ui_file *stream)
  260. {
  261.   LA_PRINT_TYPEDEF (type, new, stream);
  262. }

  263. /* The default way to print a typedef.  */

  264. void
  265. default_print_typedef (struct type *type, struct symbol *new_symbol,
  266.                        struct ui_file *stream)
  267. {
  268.   error (_("Language not supported."));
  269. }

  270. /* Print a description of a type TYPE in the form of a declaration of a
  271.    variable named VARSTRING.  (VARSTRING is demangled if necessary.)
  272.    Output goes to STREAM (via stdio).
  273.    If SHOW is positive, we show the contents of the outermost level
  274.    of structure even if there is a type name that could be used instead.
  275.    If SHOW is negative, we never show the details of elements' types.  */

  276. void
  277. type_print (struct type *type, const char *varstring, struct ui_file *stream,
  278.             int show)
  279. {
  280.   LA_PRINT_TYPE (type, varstring, stream, show, 0, &default_ptype_flags);
  281. }

  282. /* Print TYPE to a string, returning it.  The caller is responsible for
  283.    freeing the string.  */

  284. char *
  285. type_to_string (struct type *type)
  286. {
  287.   char *s = NULL;
  288.   struct ui_file *stb;
  289.   struct cleanup *old_chain;
  290.   volatile struct gdb_exception except;

  291.   stb = mem_fileopen ();
  292.   old_chain = make_cleanup_ui_file_delete (stb);

  293.   TRY_CATCH (except, RETURN_MASK_ALL)
  294.     {
  295.       type_print (type, "", stb, -1);
  296.       s = ui_file_xstrdup (stb, NULL);
  297.     }
  298.   if (except.reason < 0)
  299.     s = NULL;

  300.   do_cleanups (old_chain);

  301.   return s;
  302. }

  303. /* Print type of EXP, or last thing in value history if EXP == NULL.
  304.    show is passed to type_print.  */

  305. static void
  306. whatis_exp (char *exp, int show)
  307. {
  308.   struct expression *expr;
  309.   struct value *val;
  310.   struct cleanup *old_chain;
  311.   struct type *real_type = NULL;
  312.   struct type *type;
  313.   int full = 0;
  314.   int top = -1;
  315.   int using_enc = 0;
  316.   struct value_print_options opts;
  317.   struct type_print_options flags = default_ptype_flags;

  318.   old_chain = make_cleanup (null_cleanup, NULL);

  319.   if (exp)
  320.     {
  321.       if (*exp == '/')
  322.         {
  323.           int seen_one = 0;

  324.           for (++exp; *exp && !isspace (*exp); ++exp)
  325.             {
  326.               switch (*exp)
  327.                 {
  328.                 case 'r':
  329.                   flags.raw = 1;
  330.                   break;
  331.                 case 'm':
  332.                   flags.print_methods = 0;
  333.                   break;
  334.                 case 'M':
  335.                   flags.print_methods = 1;
  336.                   break;
  337.                 case 't':
  338.                   flags.print_typedefs = 0;
  339.                   break;
  340.                 case 'T':
  341.                   flags.print_typedefs = 1;
  342.                   break;
  343.                 default:
  344.                   error (_("unrecognized flag '%c'"), *exp);
  345.                 }
  346.               seen_one = 1;
  347.             }

  348.           if (!*exp && !seen_one)
  349.             error (_("flag expected"));
  350.           if (!isspace (*exp))
  351.             error (_("expected space after format"));
  352.           exp = skip_spaces (exp);
  353.         }

  354.       expr = parse_expression (exp);
  355.       make_cleanup (free_current_contents, &expr);
  356.       val = evaluate_type (expr);
  357.     }
  358.   else
  359.     val = access_value_history (0);

  360.   type = value_type (val);

  361.   get_user_print_options (&opts);
  362.   if (opts.objectprint)
  363.     {
  364.       if (((TYPE_CODE (type) == TYPE_CODE_PTR)
  365.            || (TYPE_CODE (type) == TYPE_CODE_REF))
  366.           && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_STRUCT))
  367.         real_type = value_rtti_indirect_type (val, &full, &top, &using_enc);
  368.       else if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
  369.         real_type = value_rtti_type (val, &full, &top, &using_enc);
  370.     }

  371.   printf_filtered ("type = ");

  372.   if (!flags.raw)
  373.     create_global_typedef_table (&flags);

  374.   if (real_type)
  375.     {
  376.       printf_filtered ("/* real type = ");
  377.       type_print (real_type, "", gdb_stdout, -1);
  378.       if (! full)
  379.         printf_filtered (" (incomplete object)");
  380.       printf_filtered (" */\n");
  381.     }

  382.   LA_PRINT_TYPE (type, "", gdb_stdout, show, 0, &flags);
  383.   printf_filtered ("\n");

  384.   do_cleanups (old_chain);
  385. }

  386. static void
  387. whatis_command (char *exp, int from_tty)
  388. {
  389.   /* Most of the time users do not want to see all the fields
  390.      in a structure.  If they do they can use the "ptype" command.
  391.      Hence the "-1" below.  */
  392.   whatis_exp (exp, -1);
  393. }

  394. /* TYPENAME is either the name of a type, or an expression.  */

  395. static void
  396. ptype_command (char *typename, int from_tty)
  397. {
  398.   whatis_exp (typename, 1);
  399. }

  400. /* Print integral scalar data VAL, of type TYPE, onto stdio stream STREAM.
  401.    Used to print data from type structures in a specified type.  For example,
  402.    array bounds may be characters or booleans in some languages, and this
  403.    allows the ranges to be printed in their "natural" form rather than as
  404.    decimal integer values.

  405.    FIXME:  This is here simply because only the type printing routines
  406.    currently use it, and it wasn't clear if it really belonged somewhere
  407.    else (like printcmd.c).  There are a lot of other gdb routines that do
  408.    something similar, but they are generally concerned with printing values
  409.    that come from the inferior in target byte order and target size.  */

  410. void
  411. print_type_scalar (struct type *type, LONGEST val, struct ui_file *stream)
  412. {
  413.   unsigned int i;
  414.   unsigned len;

  415.   CHECK_TYPEDEF (type);

  416.   switch (TYPE_CODE (type))
  417.     {

  418.     case TYPE_CODE_ENUM:
  419.       len = TYPE_NFIELDS (type);
  420.       for (i = 0; i < len; i++)
  421.         {
  422.           if (TYPE_FIELD_ENUMVAL (type, i) == val)
  423.             {
  424.               break;
  425.             }
  426.         }
  427.       if (i < len)
  428.         {
  429.           fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
  430.         }
  431.       else
  432.         {
  433.           print_longest (stream, 'd', 0, val);
  434.         }
  435.       break;

  436.     case TYPE_CODE_INT:
  437.       print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0, val);
  438.       break;

  439.     case TYPE_CODE_CHAR:
  440.       LA_PRINT_CHAR ((unsigned char) val, type, stream);
  441.       break;

  442.     case TYPE_CODE_BOOL:
  443.       fprintf_filtered (stream, val ? "TRUE" : "FALSE");
  444.       break;

  445.     case TYPE_CODE_RANGE:
  446.       print_type_scalar (TYPE_TARGET_TYPE (type), val, stream);
  447.       return;

  448.     case TYPE_CODE_UNDEF:
  449.     case TYPE_CODE_PTR:
  450.     case TYPE_CODE_ARRAY:
  451.     case TYPE_CODE_STRUCT:
  452.     case TYPE_CODE_UNION:
  453.     case TYPE_CODE_FUNC:
  454.     case TYPE_CODE_FLT:
  455.     case TYPE_CODE_VOID:
  456.     case TYPE_CODE_SET:
  457.     case TYPE_CODE_STRING:
  458.     case TYPE_CODE_ERROR:
  459.     case TYPE_CODE_MEMBERPTR:
  460.     case TYPE_CODE_METHODPTR:
  461.     case TYPE_CODE_METHOD:
  462.     case TYPE_CODE_REF:
  463.     case TYPE_CODE_NAMESPACE:
  464.       error (_("internal error: unhandled type in print_type_scalar"));
  465.       break;

  466.     default:
  467.       error (_("Invalid type code in symbol table."));
  468.     }
  469.   gdb_flush (stream);
  470. }

  471. /* Dump details of a type specified either directly or indirectly.
  472.    Uses the same sort of type lookup mechanism as ptype_command()
  473.    and whatis_command().  */

  474. void
  475. maintenance_print_type (char *typename, int from_tty)
  476. {
  477.   struct value *val;
  478.   struct type *type;
  479.   struct cleanup *old_chain;
  480.   struct expression *expr;

  481.   if (typename != NULL)
  482.     {
  483.       expr = parse_expression (typename);
  484.       old_chain = make_cleanup (free_current_contents, &expr);
  485.       if (expr->elts[0].opcode == OP_TYPE)
  486.         {
  487.           /* The user expression names a type directly, just use that type.  */
  488.           type = expr->elts[1].type;
  489.         }
  490.       else
  491.         {
  492.           /* The user expression may name a type indirectly by naming an
  493.              object of that type.  Find that indirectly named type.  */
  494.           val = evaluate_type (expr);
  495.           type = value_type (val);
  496.         }
  497.       if (type != NULL)
  498.         {
  499.           recursive_dump_type (type, 0);
  500.         }
  501.       do_cleanups (old_chain);
  502.     }
  503. }


  504. struct cmd_list_element *setprinttypelist;

  505. struct cmd_list_element *showprinttypelist;

  506. static void
  507. set_print_type (char *arg, int from_tty)
  508. {
  509.   printf_unfiltered (
  510.      "\"set print type\" must be followed by the name of a subcommand.\n");
  511.   help_list (setprintlist, "set print type ", all_commands, gdb_stdout);
  512. }

  513. static void
  514. show_print_type (char *args, int from_tty)
  515. {
  516.   cmd_show_list (showprinttypelist, from_tty, "");
  517. }

  518. static int print_methods = 1;

  519. static void
  520. set_print_type_methods (char *args, int from_tty, struct cmd_list_element *c)
  521. {
  522.   default_ptype_flags.print_methods = print_methods;
  523. }

  524. static void
  525. show_print_type_methods (struct ui_file *file, int from_tty,
  526.                          struct cmd_list_element *c, const char *value)
  527. {
  528.   fprintf_filtered (file, _("Printing of methods defined in a class in %s\n"),
  529.                     value);
  530. }

  531. static int print_typedefs = 1;

  532. static void
  533. set_print_type_typedefs (char *args, int from_tty, struct cmd_list_element *c)
  534. {
  535.   default_ptype_flags.print_typedefs = print_typedefs;
  536. }

  537. static void
  538. show_print_type_typedefs (struct ui_file *file, int from_tty,
  539.                          struct cmd_list_element *c, const char *value)
  540. {
  541.   fprintf_filtered (file, _("Printing of typedefs defined in a class in %s\n"),
  542.                     value);
  543. }

  544. void
  545. _initialize_typeprint (void)
  546. {
  547.   struct cmd_list_element *c;

  548.   c = add_com ("ptype", class_vars, ptype_command, _("\
  549. Print definition of type TYPE.\n\
  550. Usage: ptype[/FLAGS] TYPE | EXPRESSION\n\
  551. Argument may be any type (for example a type name defined by typedef,\n\
  552. or \"struct STRUCT-TAG\" or \"class CLASS-NAME\" or \"union UNION-TAG\"\n\
  553. or \"enum ENUM-TAG\") or an expression.\n\
  554. The selected stack frame's lexical context is used to look up the name.\n\
  555. Contrary to \"whatis\", \"ptype\" always unrolls any typedefs.\n\
  556. \n\
  557. Available FLAGS are:\n\
  558.   /r    print in \"raw\" form; do not substitute typedefs\n\
  559.   /m    do not print methods defined in a class\n\
  560.   /M    print methods defined in a class\n\
  561.   /t    do not print typedefs defined in a class\n\
  562.   /T    print typedefs defined in a class"));
  563.   set_cmd_completer (c, expression_completer);

  564.   c = add_com ("whatis", class_vars, whatis_command,
  565.                _("Print data type of expression EXP.\n\
  566. Only one level of typedefs is unrolled.  See also \"ptype\"."));
  567.   set_cmd_completer (c, expression_completer);

  568.   add_prefix_cmd ("type", no_class, show_print_type,
  569.                   _("Generic command for showing type-printing settings."),
  570.                   &showprinttypelist, "show print type ", 0, &showprintlist);
  571.   add_prefix_cmd ("type", no_class, set_print_type,
  572.                   _("Generic command for setting how types print."),
  573.                   &setprinttypelist, "show print type ", 0, &setprintlist);

  574.   add_setshow_boolean_cmd ("methods", no_class, &print_methods,
  575.                            _("\
  576. Set printing of methods defined in classes."), _("\
  577. Show printing of methods defined in classes."), NULL,
  578.                            set_print_type_methods,
  579.                            show_print_type_methods,
  580.                            &setprinttypelist, &showprinttypelist);
  581.   add_setshow_boolean_cmd ("typedefs", no_class, &print_typedefs,
  582.                            _("\
  583. Set printing of typedefs defined in classes."), _("\
  584. Show printing of typedefs defined in classes."), NULL,
  585.                            set_print_type_typedefs,
  586.                            show_print_type_typedefs,
  587.                            &setprinttypelist, &showprinttypelist);
  588. }