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