GDB (API)
/home/stan/gdb/src/gdb/typeprint.c
Go to the documentation of this file.
00001 /* Language independent support for printing types for GDB, the GNU debugger.
00002 
00003    Copyright (C) 1986-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 #include "gdb_obstack.h"
00022 #include "bfd.h"                /* Binary File Description */
00023 #include "symtab.h"
00024 #include "gdbtypes.h"
00025 #include "expression.h"
00026 #include "value.h"
00027 #include "gdbcore.h"
00028 #include "command.h"
00029 #include "gdbcmd.h"
00030 #include "target.h"
00031 #include "language.h"
00032 #include "cp-abi.h"
00033 #include "typeprint.h"
00034 #include "gdb_string.h"
00035 #include "exceptions.h"
00036 #include "valprint.h"
00037 #include <errno.h>
00038 #include <ctype.h>
00039 #include "cli/cli-utils.h"
00040 #include "python/python.h"
00041 #include "completer.h"
00042 
00043 extern void _initialize_typeprint (void);
00044 
00045 static void ptype_command (char *, int);
00046 
00047 static void whatis_command (char *, int);
00048 
00049 static void whatis_exp (char *, int);
00050 
00051 const struct type_print_options type_print_raw_options =
00052 {
00053   1,                            /* raw */
00054   1,                            /* print_methods */
00055   1,                            /* print_typedefs */
00056   NULL,                         /* local_typedefs */
00057   NULL,                         /* global_table */
00058   NULL                          /* global_printers */
00059 };
00060 
00061 /* The default flags for 'ptype' and 'whatis'.  */
00062 
00063 static struct type_print_options default_ptype_flags =
00064 {
00065   0,                            /* raw */
00066   1,                            /* print_methods */
00067   1,                            /* print_typedefs */
00068   NULL,                         /* local_typedefs */
00069   NULL,                         /* global_table */
00070   NULL                          /* global_printers */
00071 };
00072 
00073 
00074 
00075 /* A hash table holding typedef_field objects.  This is more
00076    complicated than an ordinary hash because it must also track the
00077    lifetime of some -- but not all -- of the contained objects.  */
00078 
00079 struct typedef_hash_table
00080 {
00081   /* The actual hash table.  */
00082   htab_t table;
00083 
00084   /* Storage for typedef_field objects that must be synthesized.  */
00085   struct obstack storage;
00086 };
00087 
00088 /* A hash function for a typedef_field.  */
00089 
00090 static hashval_t
00091 hash_typedef_field (const void *p)
00092 {
00093   const struct typedef_field *tf = p;
00094   struct type *t = check_typedef (tf->type);
00095 
00096   return htab_hash_string (TYPE_SAFE_NAME (t));
00097 }
00098 
00099 /* An equality function for a typedef field.  */
00100 
00101 static int
00102 eq_typedef_field (const void *a, const void *b)
00103 {
00104   const struct typedef_field *tfa = a;
00105   const struct typedef_field *tfb = b;
00106 
00107   return types_equal (tfa->type, tfb->type);
00108 }
00109 
00110 /* Add typedefs from T to the hash table TABLE.  */
00111 
00112 void
00113 recursively_update_typedef_hash (struct typedef_hash_table *table,
00114                                  struct type *t)
00115 {
00116   int i;
00117 
00118   if (table == NULL)
00119     return;
00120 
00121   for (i = 0; i < TYPE_TYPEDEF_FIELD_COUNT (t); ++i)
00122     {
00123       struct typedef_field *tdef = &TYPE_TYPEDEF_FIELD (t, i);
00124       void **slot;
00125 
00126       slot = htab_find_slot (table->table, tdef, INSERT);
00127       /* Only add a given typedef name once.  Really this shouldn't
00128          happen; but it is safe enough to do the updates breadth-first
00129          and thus use the most specific typedef.  */
00130       if (*slot == NULL)
00131         *slot = tdef;
00132     }
00133 
00134   /* Recurse into superclasses.  */
00135   for (i = 0; i < TYPE_N_BASECLASSES (t); ++i)
00136     recursively_update_typedef_hash (table, TYPE_BASECLASS (t, i));
00137 }
00138 
00139 /* Add template parameters from T to the typedef hash TABLE.  */
00140 
00141 void
00142 add_template_parameters (struct typedef_hash_table *table, struct type *t)
00143 {
00144   int i;
00145 
00146   if (table == NULL)
00147     return;
00148 
00149   for (i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (t); ++i)
00150     {
00151       struct typedef_field *tf;
00152       void **slot;
00153 
00154       /* We only want type-valued template parameters in the hash.  */
00155       if (SYMBOL_CLASS (TYPE_TEMPLATE_ARGUMENT (t, i)) != LOC_TYPEDEF)
00156         continue;
00157 
00158       tf = XOBNEW (&table->storage, struct typedef_field);
00159       tf->name = SYMBOL_LINKAGE_NAME (TYPE_TEMPLATE_ARGUMENT (t, i));
00160       tf->type = SYMBOL_TYPE (TYPE_TEMPLATE_ARGUMENT (t, i));
00161 
00162       slot = htab_find_slot (table->table, tf, INSERT);
00163       if (*slot == NULL)
00164         *slot = tf;
00165     }
00166 }
00167 
00168 /* Create a new typedef-lookup hash table.  */
00169 
00170 struct typedef_hash_table *
00171 create_typedef_hash (void)
00172 {
00173   struct typedef_hash_table *result;
00174 
00175   result = XNEW (struct typedef_hash_table);
00176   result->table = htab_create_alloc (10, hash_typedef_field, eq_typedef_field,
00177                                      NULL, xcalloc, xfree);
00178   obstack_init (&result->storage);
00179 
00180   return result;
00181 }
00182 
00183 /* Free a typedef field table.  */
00184 
00185 void
00186 free_typedef_hash (struct typedef_hash_table *table)
00187 {
00188   if (table != NULL)
00189     {
00190       htab_delete (table->table);
00191       obstack_free (&table->storage, NULL);
00192       xfree (table);
00193     }
00194 }
00195 
00196 /* A cleanup for freeing a typedef_hash_table.  */
00197 
00198 static void
00199 do_free_typedef_hash (void *arg)
00200 {
00201   free_typedef_hash (arg);
00202 }
00203 
00204 /* Return a new cleanup that frees TABLE.  */
00205 
00206 struct cleanup *
00207 make_cleanup_free_typedef_hash (struct typedef_hash_table *table)
00208 {
00209   return make_cleanup (do_free_typedef_hash, table);
00210 }
00211 
00212 /* Helper function for copy_typedef_hash.  */
00213 
00214 static int
00215 copy_typedef_hash_element (void **slot, void *nt)
00216 {
00217   htab_t new_table = nt;
00218   void **new_slot;
00219 
00220   new_slot = htab_find_slot (new_table, *slot, INSERT);
00221   if (*new_slot == NULL)
00222     *new_slot = *slot;
00223 
00224   return 1;
00225 }
00226 
00227 /* Copy a typedef hash.  */
00228 
00229 struct typedef_hash_table *
00230 copy_typedef_hash (struct typedef_hash_table *table)
00231 {
00232   struct typedef_hash_table *result;
00233 
00234   if (table == NULL)
00235     return NULL;
00236 
00237   result = create_typedef_hash ();
00238   htab_traverse_noresize (table->table, copy_typedef_hash_element,
00239                           result->table);
00240   return result;
00241 }
00242 
00243 /* A cleanup to free the global typedef hash.  */
00244 
00245 static void
00246 do_free_global_table (void *arg)
00247 {
00248   struct type_print_options *flags = arg;
00249 
00250   free_typedef_hash (flags->global_typedefs);
00251   free_type_printers (flags->global_printers);
00252 }
00253 
00254 /* Create the global typedef hash.  */
00255 
00256 static struct cleanup *
00257 create_global_typedef_table (struct type_print_options *flags)
00258 {
00259   gdb_assert (flags->global_typedefs == NULL && flags->global_printers == NULL);
00260   flags->global_typedefs = create_typedef_hash ();
00261   flags->global_printers = start_type_printers ();
00262   return make_cleanup (do_free_global_table, flags);
00263 }
00264 
00265 /* Look up the type T in the global typedef hash.  If it is found,
00266    return the typedef name.  If it is not found, apply the
00267    type-printers, if any, given by start_type_printers and return the
00268    result.  A NULL return means that the name was not found.  */
00269 
00270 static const char *
00271 find_global_typedef (const struct type_print_options *flags,
00272                      struct type *t)
00273 {
00274   char *applied;
00275   void **slot;
00276   struct typedef_field tf, *new_tf;
00277 
00278   if (flags->global_typedefs == NULL)
00279     return NULL;
00280 
00281   tf.name = NULL;
00282   tf.type = t;
00283 
00284   slot = htab_find_slot (flags->global_typedefs->table, &tf, INSERT);
00285   if (*slot != NULL)
00286     {
00287       new_tf = *slot;
00288       return new_tf->name;
00289     }
00290 
00291   /* Put an entry into the hash table now, in case apply_type_printers
00292      recurses.  */
00293   new_tf = XOBNEW (&flags->global_typedefs->storage, struct typedef_field);
00294   new_tf->name = NULL;
00295   new_tf->type = t;
00296 
00297   *slot = new_tf;
00298 
00299   applied = apply_type_printers (flags->global_printers, t);
00300 
00301   if (applied != NULL)
00302     {
00303       new_tf->name = obstack_copy0 (&flags->global_typedefs->storage, applied,
00304                                     strlen (applied));
00305       xfree (applied);
00306     }
00307 
00308   return new_tf->name;
00309 }
00310 
00311 /* Look up the type T in the typedef hash table in with FLAGS.  If T
00312    is in the table, return its short (class-relative) typedef name.
00313    Otherwise return NULL.  If the table is NULL, this always returns
00314    NULL.  */
00315 
00316 const char *
00317 find_typedef_in_hash (const struct type_print_options *flags, struct type *t)
00318 {
00319   if (flags->local_typedefs != NULL)
00320     {
00321       struct typedef_field tf, *found;
00322 
00323       tf.name = NULL;
00324       tf.type = t;
00325       found = htab_find (flags->local_typedefs->table, &tf);
00326 
00327       if (found != NULL)
00328         return found->name;
00329     }
00330 
00331   return find_global_typedef (flags, t);
00332 }
00333 
00334 
00335 
00336 /* Print a description of a type in the format of a 
00337    typedef for the current language.
00338    NEW is the new name for a type TYPE.  */
00339 
00340 void
00341 typedef_print (struct type *type, struct symbol *new, struct ui_file *stream)
00342 {
00343   LA_PRINT_TYPEDEF (type, new, stream);
00344 }
00345 
00346 /* The default way to print a typedef.  */
00347 
00348 void
00349 default_print_typedef (struct type *type, struct symbol *new_symbol,
00350                        struct ui_file *stream)
00351 {
00352   error (_("Language not supported."));
00353 }
00354 
00355 /* Print a description of a type TYPE in the form of a declaration of a
00356    variable named VARSTRING.  (VARSTRING is demangled if necessary.)
00357    Output goes to STREAM (via stdio).
00358    If SHOW is positive, we show the contents of the outermost level
00359    of structure even if there is a type name that could be used instead.
00360    If SHOW is negative, we never show the details of elements' types.  */
00361 
00362 void
00363 type_print (struct type *type, const char *varstring, struct ui_file *stream,
00364             int show)
00365 {
00366   LA_PRINT_TYPE (type, varstring, stream, show, 0, &default_ptype_flags);
00367 }
00368 
00369 /* Print TYPE to a string, returning it.  The caller is responsible for
00370    freeing the string.  */
00371 
00372 char *
00373 type_to_string (struct type *type)
00374 {
00375   char *s = NULL;
00376   struct ui_file *stb;
00377   struct cleanup *old_chain;
00378   volatile struct gdb_exception except;
00379 
00380   stb = mem_fileopen ();
00381   old_chain = make_cleanup_ui_file_delete (stb);
00382 
00383   TRY_CATCH (except, RETURN_MASK_ALL)
00384     {
00385       type_print (type, "", stb, -1);
00386       s = ui_file_xstrdup (stb, NULL);
00387     }
00388   if (except.reason < 0)
00389     s = NULL;
00390 
00391   do_cleanups (old_chain);
00392 
00393   return s;
00394 }
00395 
00396 /* Print type of EXP, or last thing in value history if EXP == NULL.
00397    show is passed to type_print.  */
00398 
00399 static void
00400 whatis_exp (char *exp, int show)
00401 {
00402   struct expression *expr;
00403   struct value *val;
00404   struct cleanup *old_chain;
00405   struct type *real_type = NULL;
00406   struct type *type;
00407   int full = 0;
00408   int top = -1;
00409   int using_enc = 0;
00410   struct value_print_options opts;
00411   struct type_print_options flags = default_ptype_flags;
00412 
00413   old_chain = make_cleanup (null_cleanup, NULL);
00414 
00415   if (exp)
00416     {
00417       if (*exp == '/')
00418         {
00419           int seen_one = 0;
00420 
00421           for (++exp; *exp && !isspace (*exp); ++exp)
00422             {
00423               switch (*exp)
00424                 {
00425                 case 'r':
00426                   flags.raw = 1;
00427                   break;
00428                 case 'm':
00429                   flags.print_methods = 0;
00430                   break;
00431                 case 'M':
00432                   flags.print_methods = 1;
00433                   break;
00434                 case 't':
00435                   flags.print_typedefs = 0;
00436                   break;
00437                 case 'T':
00438                   flags.print_typedefs = 1;
00439                   break;
00440                 default:
00441                   error (_("unrecognized flag '%c'"), *exp);
00442                 }
00443               seen_one = 1;
00444             }
00445 
00446           if (!*exp && !seen_one)
00447             error (_("flag expected"));
00448           if (!isspace (*exp))
00449             error (_("expected space after format"));
00450           exp = skip_spaces (exp);
00451         }
00452 
00453       expr = parse_expression (exp);
00454       make_cleanup (free_current_contents, &expr);
00455       val = evaluate_type (expr);
00456     }
00457   else
00458     val = access_value_history (0);
00459 
00460   type = value_type (val);
00461 
00462   get_user_print_options (&opts);
00463   if (opts.objectprint)
00464     {
00465       if (((TYPE_CODE (type) == TYPE_CODE_PTR)
00466            || (TYPE_CODE (type) == TYPE_CODE_REF))
00467           && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CLASS))
00468         real_type = value_rtti_indirect_type (val, &full, &top, &using_enc);
00469       else if (TYPE_CODE (type) == TYPE_CODE_CLASS)
00470         real_type = value_rtti_type (val, &full, &top, &using_enc);
00471     }
00472 
00473   printf_filtered ("type = ");
00474 
00475   if (!flags.raw)
00476     create_global_typedef_table (&flags);
00477 
00478   if (real_type)
00479     {
00480       printf_filtered ("/* real type = ");
00481       type_print (real_type, "", gdb_stdout, -1);
00482       if (! full)
00483         printf_filtered (" (incomplete object)");
00484       printf_filtered (" */\n");    
00485     }
00486 
00487   LA_PRINT_TYPE (type, "", gdb_stdout, show, 0, &flags);
00488   printf_filtered ("\n");
00489 
00490   do_cleanups (old_chain);
00491 }
00492 
00493 static void
00494 whatis_command (char *exp, int from_tty)
00495 {
00496   /* Most of the time users do not want to see all the fields
00497      in a structure.  If they do they can use the "ptype" command.
00498      Hence the "-1" below.  */
00499   whatis_exp (exp, -1);
00500 }
00501 
00502 /* TYPENAME is either the name of a type, or an expression.  */
00503 
00504 static void
00505 ptype_command (char *typename, int from_tty)
00506 {
00507   whatis_exp (typename, 1);
00508 }
00509 
00510 /* Print integral scalar data VAL, of type TYPE, onto stdio stream STREAM.
00511    Used to print data from type structures in a specified type.  For example,
00512    array bounds may be characters or booleans in some languages, and this
00513    allows the ranges to be printed in their "natural" form rather than as
00514    decimal integer values.
00515 
00516    FIXME:  This is here simply because only the type printing routines
00517    currently use it, and it wasn't clear if it really belonged somewhere
00518    else (like printcmd.c).  There are a lot of other gdb routines that do
00519    something similar, but they are generally concerned with printing values
00520    that come from the inferior in target byte order and target size.  */
00521 
00522 void
00523 print_type_scalar (struct type *type, LONGEST val, struct ui_file *stream)
00524 {
00525   unsigned int i;
00526   unsigned len;
00527 
00528   CHECK_TYPEDEF (type);
00529 
00530   switch (TYPE_CODE (type))
00531     {
00532 
00533     case TYPE_CODE_ENUM:
00534       len = TYPE_NFIELDS (type);
00535       for (i = 0; i < len; i++)
00536         {
00537           if (TYPE_FIELD_ENUMVAL (type, i) == val)
00538             {
00539               break;
00540             }
00541         }
00542       if (i < len)
00543         {
00544           fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
00545         }
00546       else
00547         {
00548           print_longest (stream, 'd', 0, val);
00549         }
00550       break;
00551 
00552     case TYPE_CODE_INT:
00553       print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0, val);
00554       break;
00555 
00556     case TYPE_CODE_CHAR:
00557       LA_PRINT_CHAR ((unsigned char) val, type, stream);
00558       break;
00559 
00560     case TYPE_CODE_BOOL:
00561       fprintf_filtered (stream, val ? "TRUE" : "FALSE");
00562       break;
00563 
00564     case TYPE_CODE_RANGE:
00565       print_type_scalar (TYPE_TARGET_TYPE (type), val, stream);
00566       return;
00567 
00568     case TYPE_CODE_UNDEF:
00569     case TYPE_CODE_PTR:
00570     case TYPE_CODE_ARRAY:
00571     case TYPE_CODE_STRUCT:
00572     case TYPE_CODE_UNION:
00573     case TYPE_CODE_FUNC:
00574     case TYPE_CODE_FLT:
00575     case TYPE_CODE_VOID:
00576     case TYPE_CODE_SET:
00577     case TYPE_CODE_STRING:
00578     case TYPE_CODE_ERROR:
00579     case TYPE_CODE_MEMBERPTR:
00580     case TYPE_CODE_METHODPTR:
00581     case TYPE_CODE_METHOD:
00582     case TYPE_CODE_REF:
00583     case TYPE_CODE_NAMESPACE:
00584       error (_("internal error: unhandled type in print_type_scalar"));
00585       break;
00586 
00587     default:
00588       error (_("Invalid type code in symbol table."));
00589     }
00590   gdb_flush (stream);
00591 }
00592 
00593 /* Dump details of a type specified either directly or indirectly.
00594    Uses the same sort of type lookup mechanism as ptype_command()
00595    and whatis_command().  */
00596 
00597 void
00598 maintenance_print_type (char *typename, int from_tty)
00599 {
00600   struct value *val;
00601   struct type *type;
00602   struct cleanup *old_chain;
00603   struct expression *expr;
00604 
00605   if (typename != NULL)
00606     {
00607       expr = parse_expression (typename);
00608       old_chain = make_cleanup (free_current_contents, &expr);
00609       if (expr->elts[0].opcode == OP_TYPE)
00610         {
00611           /* The user expression names a type directly, just use that type.  */
00612           type = expr->elts[1].type;
00613         }
00614       else
00615         {
00616           /* The user expression may name a type indirectly by naming an
00617              object of that type.  Find that indirectly named type.  */
00618           val = evaluate_type (expr);
00619           type = value_type (val);
00620         }
00621       if (type != NULL)
00622         {
00623           recursive_dump_type (type, 0);
00624         }
00625       do_cleanups (old_chain);
00626     }
00627 }
00628 
00629 
00630 struct cmd_list_element *setprinttypelist;
00631 
00632 struct cmd_list_element *showprinttypelist;
00633 
00634 static void
00635 set_print_type (char *arg, int from_tty)
00636 {
00637   printf_unfiltered (
00638      "\"set print type\" must be followed by the name of a subcommand.\n");
00639   help_list (setprintlist, "set print type ", -1, gdb_stdout);
00640 }
00641 
00642 static void
00643 show_print_type (char *args, int from_tty)
00644 {
00645   cmd_show_list (showprinttypelist, from_tty, "");
00646 }
00647 
00648 static int print_methods = 1;
00649 
00650 static void
00651 set_print_type_methods (char *args, int from_tty, struct cmd_list_element *c)
00652 {
00653   default_ptype_flags.print_methods = print_methods;
00654 }
00655 
00656 static void
00657 show_print_type_methods (struct ui_file *file, int from_tty,
00658                          struct cmd_list_element *c, const char *value)
00659 {
00660   fprintf_filtered (file, _("Printing of methods defined in a class in %s\n"),
00661                     value);
00662 }
00663 
00664 static int print_typedefs = 1;
00665 
00666 static void
00667 set_print_type_typedefs (char *args, int from_tty, struct cmd_list_element *c)
00668 {
00669   default_ptype_flags.print_typedefs = print_typedefs;
00670 }
00671 
00672 static void
00673 show_print_type_typedefs (struct ui_file *file, int from_tty,
00674                          struct cmd_list_element *c, const char *value)
00675 {
00676   fprintf_filtered (file, _("Printing of typedefs defined in a class in %s\n"),
00677                     value);
00678 }
00679 
00680 void
00681 _initialize_typeprint (void)
00682 {
00683   struct cmd_list_element *c;
00684 
00685   c = add_com ("ptype", class_vars, ptype_command, _("\
00686 Print definition of type TYPE.\n\
00687 Usage: ptype[/FLAGS] TYPE | EXPRESSION\n\
00688 Argument may be any type (for example a type name defined by typedef,\n\
00689 or \"struct STRUCT-TAG\" or \"class CLASS-NAME\" or \"union UNION-TAG\"\n\
00690 or \"enum ENUM-TAG\") or an expression.\n\
00691 The selected stack frame's lexical context is used to look up the name.\n\
00692 Contrary to \"whatis\", \"ptype\" always unrolls any typedefs.\n\
00693 \n\
00694 Available FLAGS are:\n\
00695   /r    print in \"raw\" form; do not substitute typedefs\n\
00696   /m    do not print methods defined in a class\n\
00697   /M    print methods defined in a class\n\
00698   /t    do not print typedefs defined in a class\n\
00699   /T    print typedefs defined in a class"));
00700   set_cmd_completer (c, expression_completer);
00701 
00702   c = add_com ("whatis", class_vars, whatis_command,
00703                _("Print data type of expression EXP.\n\
00704 Only one level of typedefs is unrolled.  See also \"ptype\"."));
00705   set_cmd_completer (c, expression_completer);
00706 
00707   add_prefix_cmd ("type", no_class, show_print_type,
00708                   _("Generic command for showing type-printing settings."),
00709                   &showprinttypelist, "show print type ", 0, &showprintlist);
00710   add_prefix_cmd ("type", no_class, set_print_type,
00711                   _("Generic command for setting how types print."),
00712                   &setprinttypelist, "show print type ", 0, &setprintlist);
00713 
00714   add_setshow_boolean_cmd ("methods", no_class, &print_methods,
00715                            _("\
00716 Set printing of methods defined in classes."), _("\
00717 Show printing of methods defined in classes."), NULL,
00718                            set_print_type_methods,
00719                            show_print_type_methods,
00720                            &setprinttypelist, &showprinttypelist);
00721   add_setshow_boolean_cmd ("typedefs", no_class, &print_typedefs,
00722                            _("\
00723 Set printing of typedefs defined in classes."), _("\
00724 Show printing of typedefs defined in classes."), NULL,
00725                            set_print_type_typedefs,
00726                            show_print_type_typedefs,
00727                            &setprinttypelist, &showprinttypelist);
00728 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines