home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / D / GDB / GDB-4.13 / GDB-4 / gdb-4.13 / gdb / cp-valprint.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-13  |  13.6 KB  |  513 lines

  1. /* Support for printing C++ values for GDB, the GNU debugger.
  2.    Copyright 1986, 1988, 1989, 1991 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "defs.h"
  21. #include "obstack.h"
  22. #include "symtab.h"
  23. #include "gdbtypes.h"
  24. #include "expression.h"
  25. #include "value.h"
  26. #include "command.h"
  27. #include "gdbcmd.h"
  28. #include "demangle.h"
  29. #include "annotate.h"
  30.  
  31. int vtblprint;            /* Controls printing of vtbl's */
  32. int objectprint;        /* Controls looking up an object's derived type
  33.                    using what we find in its vtables.  */
  34. struct obstack dont_print_obstack;
  35.  
  36. static void
  37. cplus_print_value PARAMS ((struct type *, char *, GDB_FILE *, int, int,
  38.                enum val_prettyprint, struct type **));
  39.  
  40. /* BEGIN-FIXME:  Hooks into typeprint.c, find a better home for prototypes. */
  41.  
  42. extern void
  43. c_type_print_base PARAMS ((struct type *, GDB_FILE *, int, int));
  44.  
  45. extern void
  46. c_type_print_varspec_prefix PARAMS ((struct type *, GDB_FILE *, int, int));
  47.  
  48. extern void
  49. cp_type_print_method_args PARAMS ((struct type **, char *, char *, int,
  50.                    GDB_FILE *));
  51.  
  52. extern struct obstack dont_print_obstack;
  53.  
  54. /* END-FIXME */
  55.  
  56. void
  57. cp_print_class_method (valaddr, type, stream)
  58.      char *valaddr;
  59.      struct type *type;
  60.      GDB_FILE *stream;
  61. {
  62.   struct type *domain;
  63.   struct fn_field *f = NULL;
  64.   int j = 0;
  65.   int len2;
  66.   int offset;
  67.   char *kind = "";
  68.   CORE_ADDR addr;
  69.   struct symbol *sym;
  70.   unsigned len;
  71.   unsigned int i;
  72.  
  73.   check_stub_type (TYPE_TARGET_TYPE (type));
  74.   domain = TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (type));
  75.   if (domain == (struct type *)NULL)
  76.     {
  77.       fprintf_filtered (stream, "<unknown>");
  78.       return;
  79.     }
  80.   addr = unpack_pointer (lookup_pointer_type (builtin_type_void), valaddr);
  81.   if (METHOD_PTR_IS_VIRTUAL (addr))
  82.     {
  83.       offset = METHOD_PTR_TO_VOFFSET (addr);
  84.       len = TYPE_NFN_FIELDS (domain);
  85.       for (i = 0; i < len; i++)
  86.     {
  87.       f = TYPE_FN_FIELDLIST1 (domain, i);
  88.       len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
  89.       
  90.       for (j = 0; j < len2; j++)
  91.         {
  92.           QUIT;
  93.           if (TYPE_FN_FIELD_VOFFSET (f, j) == offset)
  94.         {
  95.           kind = "virtual ";
  96.           goto common;
  97.         }
  98.         }
  99.     }
  100.     }
  101.   else
  102.     {
  103.       sym = find_pc_function (addr);
  104.       if (sym == 0)
  105.     {
  106.       error ("invalid pointer to member function");
  107.     }
  108.       len = TYPE_NFN_FIELDS (domain);
  109.       for (i = 0; i < len; i++)
  110.     {
  111.       f = TYPE_FN_FIELDLIST1 (domain, i);
  112.       len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i);
  113.       
  114.       for (j = 0; j < len2; j++)
  115.         {
  116.           QUIT;
  117.           if (TYPE_FN_FIELD_STUB (f, j))
  118.         check_stub_method (domain, i, j);
  119.           if (STREQ (SYMBOL_NAME (sym), TYPE_FN_FIELD_PHYSNAME (f, j)))
  120.         {
  121.           goto common;
  122.         }
  123.         }
  124.     }
  125.     }
  126.   common:
  127.   if (i < len)
  128.     {
  129.       fprintf_filtered (stream, "&");
  130.       c_type_print_varspec_prefix (TYPE_FN_FIELD_TYPE (f, j), stream, 0, 0);
  131.       fprintf_unfiltered (stream, kind);
  132.       if (TYPE_FN_FIELD_PHYSNAME (f, j)[0] == '_'
  133.       && TYPE_FN_FIELD_PHYSNAME (f, j)[1] == CPLUS_MARKER)
  134.     {
  135.       cp_type_print_method_args (TYPE_FN_FIELD_ARGS (f, j) + 1, "~",
  136.                      TYPE_FN_FIELDLIST_NAME (domain, i),
  137.                      0, stream);
  138.     }
  139.       else
  140.     {
  141.       cp_type_print_method_args (TYPE_FN_FIELD_ARGS (f, j), "",
  142.                      TYPE_FN_FIELDLIST_NAME (domain, i),
  143.                      0, stream);
  144.     }
  145.     }
  146.   else
  147.     {
  148.       fprintf_filtered (stream, "(");
  149.       type_print (type, "", stream, -1);
  150.       fprintf_filtered (stream, ") %d", (int) addr >> 3);
  151.     }
  152. }
  153.  
  154. /* This was what it was for gcc 2.4.5 and earlier.  */
  155. static const char vtbl_ptr_name_old[] =
  156.   { CPLUS_MARKER,'v','t','b','l','_','p','t','r','_','t','y','p','e', 0 };
  157. /* It was changed to this after 2.4.5.  */
  158. const char vtbl_ptr_name[] =
  159.   { '_','_','v','t','b','l','_','p','t','r','_','t','y','p','e', 0 };
  160.  
  161. /* Return truth value for assertion that TYPE is of the type
  162.    "pointer to virtual function".  */
  163.  
  164. int
  165. cp_is_vtbl_ptr_type(type)
  166.      struct type *type;
  167. {
  168.   char *typename = type_name_no_tag (type);
  169.  
  170.   return (typename != NULL
  171.       && (STREQ (typename, vtbl_ptr_name)
  172.           || STREQ (typename, vtbl_ptr_name_old)));
  173. }
  174.  
  175. /* Return truth value for the assertion that TYPE is of the type
  176.    "pointer to virtual function table".  */
  177.  
  178. int
  179. cp_is_vtbl_member(type)
  180.      struct type *type;
  181. {
  182.   if (TYPE_CODE (type) == TYPE_CODE_PTR)
  183.     {
  184.       type = TYPE_TARGET_TYPE (type);
  185.       if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
  186.     {
  187.       type = TYPE_TARGET_TYPE (type);
  188.       if (TYPE_CODE (type) == TYPE_CODE_STRUCT /* if not using thunks */
  189.           || TYPE_CODE (type) == TYPE_CODE_PTR) /* if using thunks */
  190.         {
  191.           /* Virtual functions tables are full of pointers
  192.          to virtual functions. */
  193.           return cp_is_vtbl_ptr_type (type);
  194.         }
  195.     }
  196.     }
  197.   return 0;
  198. }
  199.  
  200. /* Mutually recursive subroutines of cplus_print_value and c_val_print to
  201.    print out a structure's fields: cp_print_value_fields and cplus_print_value.
  202.  
  203.    TYPE, VALADDR, STREAM, RECURSE, and PRETTY have the
  204.    same meanings as in cplus_print_value and c_val_print.
  205.  
  206.    DONT_PRINT is an array of baseclass types that we
  207.    should not print, or zero if called from top level.  */
  208.  
  209. void
  210. cp_print_value_fields (type, valaddr, stream, format, recurse, pretty,
  211.                dont_print)
  212.      struct type *type;
  213.      char *valaddr;
  214.      GDB_FILE *stream;
  215.      int format;
  216.      int recurse;
  217.      enum val_prettyprint pretty;
  218.      struct type **dont_print;
  219. {
  220.   int i, len, n_baseclasses;
  221.  
  222.   check_stub_type (type);
  223.  
  224.   fprintf_filtered (stream, "{");
  225.   len = TYPE_NFIELDS (type);
  226.   n_baseclasses = TYPE_N_BASECLASSES (type);
  227.  
  228.   /* Print out baseclasses such that we don't print
  229.      duplicates of virtual baseclasses.  */
  230.   if (n_baseclasses > 0)
  231.     cplus_print_value (type, valaddr, stream, format, recurse+1, pretty,
  232.                dont_print);
  233.  
  234.   if (!len && n_baseclasses == 1)
  235.     fprintf_filtered (stream, "<No data fields>");
  236.   else
  237.     {
  238.       extern int inspect_it;
  239.       int fields_seen = 0;
  240.  
  241.       for (i = n_baseclasses; i < len; i++)
  242.     {
  243.       /* Check if static field */
  244.       if (TYPE_FIELD_STATIC (type, i))
  245.         continue;
  246.       if (fields_seen)
  247.         fprintf_filtered (stream, ", ");
  248.       else if (n_baseclasses > 0)
  249.         {
  250.           if (pretty)
  251.         {
  252.           fprintf_filtered (stream, "\n");
  253.           print_spaces_filtered (2 + 2 * recurse, stream);
  254.           fputs_filtered ("members of ", stream);
  255.           fputs_filtered (type_name_no_tag (type), stream);
  256.           fputs_filtered (": ", stream);
  257.         }
  258.         }
  259.       fields_seen = 1;
  260.  
  261.       if (pretty)
  262.         {
  263.           fprintf_filtered (stream, "\n");
  264.           print_spaces_filtered (2 + 2 * recurse, stream);
  265.         }
  266.       else 
  267.         {
  268.           wrap_here (n_spaces (2 + 2 * recurse));
  269.         }
  270.       if (inspect_it)
  271.         {
  272.           if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR)
  273.         fputs_filtered ("\"( ptr \"", stream);
  274.           else
  275.         fputs_filtered ("\"( nodef \"", stream);
  276.           fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
  277.                        language_cplus,
  278.                        DMGL_PARAMS | DMGL_ANSI);
  279.           fputs_filtered ("\" \"", stream);
  280.           fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
  281.                        language_cplus,
  282.                        DMGL_PARAMS | DMGL_ANSI);
  283.           fputs_filtered ("\") \"", stream);
  284.         }
  285.       else
  286.         {
  287.           annotate_field_begin (TYPE_FIELD_TYPE (type, i));
  288.  
  289.           fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
  290.                        language_cplus,
  291.                        DMGL_PARAMS | DMGL_ANSI);
  292.           annotate_field_name_end ();
  293.           fputs_filtered (" = ", stream);
  294.           annotate_field_value ();
  295.         }
  296.  
  297.       if (TYPE_FIELD_PACKED (type, i))
  298.         {
  299.           value_ptr v;
  300.  
  301.           /* Bitfields require special handling, especially due to byte
  302.          order problems.  */
  303.           if (TYPE_FIELD_IGNORE (type, i))
  304.         {
  305.            fputs_filtered ("<optimized out or zero length>", stream);
  306.         }
  307.           else
  308.         {
  309.                v = value_from_longest (TYPE_FIELD_TYPE (type, i),
  310.                    unpack_field_as_long (type, valaddr, i));
  311.  
  312.                    val_print (TYPE_FIELD_TYPE(type, i), VALUE_CONTENTS (v), 0,
  313.                   stream, format, 0, recurse + 1, pretty);
  314.         }
  315.         }
  316.       else
  317.         {
  318.           if (TYPE_FIELD_IGNORE (type, i))
  319.         {
  320.            fputs_filtered ("<optimized out or zero length>", stream);
  321.         }
  322.           else
  323.         {
  324.                val_print (TYPE_FIELD_TYPE (type, i), 
  325.                   valaddr + TYPE_FIELD_BITPOS (type, i) / 8,
  326.                   0, stream, format, 0, recurse + 1, pretty);
  327.         }
  328.         }
  329.       annotate_field_end ();
  330.     }
  331.  
  332.       if (pretty)
  333.     {
  334.       fprintf_filtered (stream, "\n");
  335.       print_spaces_filtered (2 * recurse, stream);
  336.     }
  337.     }
  338.   fprintf_filtered (stream, "}");
  339. }
  340.  
  341. /* Special val_print routine to avoid printing multiple copies of virtual
  342.    baseclasses.  */
  343.  
  344. static void
  345. cplus_print_value (type, valaddr, stream, format, recurse, pretty, dont_print)
  346.      struct type *type;
  347.      char *valaddr;
  348.      GDB_FILE *stream;
  349.      int format;
  350.      int recurse;
  351.      enum val_prettyprint pretty;
  352.      struct type **dont_print;
  353. {
  354.   struct obstack tmp_obstack;
  355.   struct type **last_dont_print
  356.     = (struct type **)obstack_next_free (&dont_print_obstack);
  357.   int i, n_baseclasses = TYPE_N_BASECLASSES (type);
  358.  
  359.   if (dont_print == 0)
  360.     {
  361.       /* If we're at top level, carve out a completely fresh
  362.      chunk of the obstack and use that until this particular
  363.      invocation returns.  */
  364.       tmp_obstack = dont_print_obstack;
  365.       /* Bump up the high-water mark.  Now alpha is omega.  */
  366.       obstack_finish (&dont_print_obstack);
  367.     }
  368.  
  369.   for (i = 0; i < n_baseclasses; i++)
  370.     {
  371.       /* FIXME-32x64--assumes that a target pointer can fit in a char *.
  372.      Fix it by nuking baseclass_addr.  */
  373.       char *baddr;
  374.       int err;
  375.       char *basename;
  376.  
  377.       check_stub_type (TYPE_BASECLASS (type, i));
  378.       basename = TYPE_NAME (TYPE_BASECLASS (type, i));
  379.  
  380.       if (BASETYPE_VIA_VIRTUAL (type, i))
  381.     {
  382.       struct type **first_dont_print
  383.         = (struct type **)obstack_base (&dont_print_obstack);
  384.  
  385.       int j = (struct type **)obstack_next_free (&dont_print_obstack)
  386.         - first_dont_print;
  387.  
  388.       while (--j >= 0)
  389.         if (TYPE_BASECLASS (type, i) == first_dont_print[j])
  390.           goto flush_it;
  391.  
  392.       obstack_ptr_grow (&dont_print_obstack, TYPE_BASECLASS (type, i));
  393.     }
  394.  
  395.       /* Fix to use baseclass_offset instead. FIXME */
  396.       baddr = baseclass_addr (type, i, valaddr, 0, &err);
  397.       if (err == 0 && baddr == 0)
  398.     error ("could not find virtual baseclass %s\n",
  399.            basename ? basename : "");
  400.  
  401.       if (pretty)
  402.     {
  403.       fprintf_filtered (stream, "\n");
  404.       print_spaces_filtered (2 * recurse, stream);
  405.     }
  406.       fputs_filtered ("<", stream);
  407.       /* Not sure what the best notation is in the case where there is no
  408.      baseclass name.  */
  409.       fputs_filtered (basename ? basename : "", stream);
  410.       fputs_filtered ("> = ", stream);
  411.       if (err != 0)
  412.     {
  413.       fprintf_filtered (stream, "<invalid address ");
  414.       print_address_numeric ((CORE_ADDR) baddr, 1, stream);
  415.       fprintf_filtered (stream, ">");
  416.     }
  417.       else
  418.     cp_print_value_fields (TYPE_BASECLASS (type, i), baddr, stream, format,
  419.                    recurse, pretty,
  420.                    (struct type **) obstack_base (&dont_print_obstack));
  421.       fputs_filtered (", ", stream);
  422.  
  423.     flush_it:
  424.       ;
  425.     }
  426.  
  427.   if (dont_print == 0)
  428.     {
  429.       /* Free the space used to deal with the printing
  430.      of this type from top level.  */
  431.       obstack_free (&dont_print_obstack, last_dont_print);
  432.       /* Reset watermark so that we can continue protecting
  433.      ourselves from whatever we were protecting ourselves.  */
  434.       dont_print_obstack = tmp_obstack;
  435.     }
  436. }
  437.  
  438. void
  439. cp_print_class_member (valaddr, domain, stream, prefix)
  440.      char *valaddr;
  441.      struct type *domain;
  442.      GDB_FILE *stream;
  443.      char *prefix;
  444. {
  445.   
  446.   /* VAL is a byte offset into the structure type DOMAIN.
  447.      Find the name of the field for that offset and
  448.      print it.  */
  449.   int extra = 0;
  450.   int bits = 0;
  451.   register unsigned int i;
  452.   unsigned len = TYPE_NFIELDS (domain);
  453.   /* @@ Make VAL into bit offset */
  454.   LONGEST val = unpack_long (builtin_type_int, valaddr) << 3;
  455.   for (i = TYPE_N_BASECLASSES (domain); i < len; i++)
  456.     {
  457.       int bitpos = TYPE_FIELD_BITPOS (domain, i);
  458.       QUIT;
  459.       if (val == bitpos)
  460.     break;
  461.       if (val < bitpos && i != 0)
  462.     {
  463.       /* Somehow pointing into a field.  */
  464.       i -= 1;
  465.       extra = (val - TYPE_FIELD_BITPOS (domain, i));
  466.       if (extra & 0x7)
  467.         bits = 1;
  468.       else
  469.         extra >>= 3;
  470.       break;
  471.     }
  472.     }
  473.   if (i < len)
  474.     {
  475.       char *name;
  476.       fprintf_filtered (stream, prefix);
  477.       name = type_name_no_tag (domain);
  478.       if (name)
  479.         fputs_filtered (name, stream);
  480.       else
  481.     c_type_print_base (domain, stream, 0, 0);
  482.       fprintf_filtered (stream, "::");
  483.       fputs_filtered (TYPE_FIELD_NAME (domain, i), stream);
  484.       if (extra)
  485.     fprintf_filtered (stream, " + %d bytes", extra);
  486.       if (bits)
  487.     fprintf_filtered (stream, " (offset in bits)");
  488.     }
  489.   else
  490.     fprintf_filtered (stream, "%d", val >> 3);
  491. }
  492.  
  493. void
  494. _initialize_cp_valprint ()
  495. {
  496.   add_show_from_set
  497.     (add_set_cmd ("vtbl", class_support, var_boolean, (char *)&vtblprint,
  498.           "Set printing of C++ virtual function tables.",
  499.           &setprintlist),
  500.      &showprintlist);
  501.  
  502.   add_show_from_set
  503.     (add_set_cmd ("object", class_support, var_boolean, (char *)&objectprint,
  504.       "Set printing of object's derived type based on vtable info.",
  505.           &setprintlist),
  506.      &showprintlist);
  507.  
  508.   /* Give people the defaults which they are used to.  */
  509.   objectprint = 0;
  510.   vtblprint = 0;
  511.   obstack_begin (&dont_print_obstack, 32 * sizeof (struct type *));
  512. }
  513.