gdb/cp-valprint.c - gdb

Global variables defined

Functions defined

Source code

  1. /* Support for printing C++ values 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 "symtab.h"
  17. #include "gdbtypes.h"
  18. #include "expression.h"
  19. #include "value.h"
  20. #include "command.h"
  21. #include "gdbcmd.h"
  22. #include "demangle.h"
  23. #include "annotate.h"
  24. #include "c-lang.h"
  25. #include "target.h"
  26. #include "cp-abi.h"
  27. #include "valprint.h"
  28. #include "cp-support.h"
  29. #include "language.h"
  30. #include "extension.h"
  31. #include "typeprint.h"

  32. /* Controls printing of vtbl's.  */
  33. static void
  34. show_vtblprint (struct ui_file *file, int from_tty,
  35.                 struct cmd_list_element *c, const char *value)
  36. {
  37.   fprintf_filtered (file, _("\
  38. Printing of C++ virtual function tables is %s.\n"),
  39.                     value);
  40. }

  41. /* Controls looking up an object's derived type using what we find in
  42.    its vtables.  */
  43. static void
  44. show_objectprint (struct ui_file *file, int from_tty,
  45.                   struct cmd_list_element *c,
  46.                   const char *value)
  47. {
  48.   fprintf_filtered (file, _("\
  49. Printing of object's derived type based on vtable info is %s.\n"),
  50.                     value);
  51. }

  52. static void
  53. show_static_field_print (struct ui_file *file, int from_tty,
  54.                          struct cmd_list_element *c,
  55.                          const char *value)
  56. {
  57.   fprintf_filtered (file,
  58.                     _("Printing of C++ static members is %s.\n"),
  59.                     value);
  60. }


  61. static struct obstack dont_print_vb_obstack;
  62. static struct obstack dont_print_statmem_obstack;
  63. static struct obstack dont_print_stat_array_obstack;

  64. extern void _initialize_cp_valprint (void);

  65. static void cp_print_static_field (struct type *, struct value *,
  66.                                    struct ui_file *, int,
  67.                                    const struct value_print_options *);

  68. static void cp_print_value (struct type *, struct type *,
  69.                             const gdb_byte *, int,
  70.                             CORE_ADDR, struct ui_file *,
  71.                             int, const struct value *,
  72.                             const struct value_print_options *,
  73.                             struct type **);


  74. /* GCC versions after 2.4.5 use this.  */
  75. const char vtbl_ptr_name[] = "__vtbl_ptr_type";

  76. /* Return truth value for assertion that TYPE is of the type
  77.    "pointer to virtual function".  */

  78. int
  79. cp_is_vtbl_ptr_type (struct type *type)
  80. {
  81.   const char *typename = type_name_no_tag (type);

  82.   return (typename != NULL && !strcmp (typename, vtbl_ptr_name));
  83. }

  84. /* Return truth value for the assertion that TYPE is of the type
  85.    "pointer to virtual function table".  */

  86. int
  87. cp_is_vtbl_member (struct type *type)
  88. {
  89.   /* With older versions of g++, the vtbl field pointed to an array of
  90.      structures.  Nowadays it points directly to the structure.  */
  91.   if (TYPE_CODE (type) == TYPE_CODE_PTR)
  92.     {
  93.       type = TYPE_TARGET_TYPE (type);
  94.       if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
  95.         {
  96.           type = TYPE_TARGET_TYPE (type);
  97.           if (TYPE_CODE (type) == TYPE_CODE_STRUCT    /* if not using thunks */
  98.               || TYPE_CODE (type) == TYPE_CODE_PTR)   /* if using thunks */
  99.             {
  100.               /* Virtual functions tables are full of pointers
  101.                  to virtual functions.  */
  102.               return cp_is_vtbl_ptr_type (type);
  103.             }
  104.         }
  105.       else if (TYPE_CODE (type) == TYPE_CODE_STRUCT)  /* if not using thunks */
  106.         {
  107.           return cp_is_vtbl_ptr_type (type);
  108.         }
  109.       else if (TYPE_CODE (type) == TYPE_CODE_PTR)     /* if using thunks */
  110.         {
  111.           /* The type name of the thunk pointer is NULL when using
  112.              dwarf2.  We could test for a pointer to a function, but
  113.              there is no type info for the virtual table either, so it
  114.              wont help.  */
  115.           return cp_is_vtbl_ptr_type (type);
  116.         }
  117.     }
  118.   return 0;
  119. }

  120. /* Mutually recursive subroutines of cp_print_value and c_val_print to
  121.    print out a structure's fields: cp_print_value_fields and
  122.    cp_print_value.

  123.    TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and OPTIONS have the same
  124.    meanings as in cp_print_value and c_val_print.

  125.    2nd argument REAL_TYPE is used to carry over the type of the
  126.    derived class across the recursion to base classes.

  127.    DONT_PRINT is an array of baseclass types that we should not print,
  128.    or zero if called from top level.  */

  129. void
  130. cp_print_value_fields (struct type *type, struct type *real_type,
  131.                        const gdb_byte *valaddr, int offset,
  132.                        CORE_ADDR address, struct ui_file *stream,
  133.                        int recurse, const struct value *val,
  134.                        const struct value_print_options *options,
  135.                        struct type **dont_print_vb,
  136.                        int dont_print_statmem)
  137. {
  138.   int i, len, n_baseclasses;
  139.   int fields_seen = 0;
  140.   static int last_set_recurse = -1;

  141.   CHECK_TYPEDEF (type);

  142.   if (recurse == 0)
  143.     {
  144.       /* Any object can be left on obstacks only during an unexpected
  145.          error.  */

  146.       if (obstack_object_size (&dont_print_statmem_obstack) > 0)
  147.         {
  148.           obstack_free (&dont_print_statmem_obstack, NULL);
  149.           obstack_begin (&dont_print_statmem_obstack,
  150.                          32 * sizeof (CORE_ADDR));
  151.         }
  152.       if (obstack_object_size (&dont_print_stat_array_obstack) > 0)
  153.         {
  154.           obstack_free (&dont_print_stat_array_obstack, NULL);
  155.           obstack_begin (&dont_print_stat_array_obstack,
  156.                          32 * sizeof (struct type *));
  157.         }
  158.     }

  159.   fprintf_filtered (stream, "{");
  160.   len = TYPE_NFIELDS (type);
  161.   n_baseclasses = TYPE_N_BASECLASSES (type);

  162.   /* First, print out baseclasses such that we don't print
  163.      duplicates of virtual baseclasses.  */

  164.   if (n_baseclasses > 0)
  165.     cp_print_value (type, real_type, valaddr,
  166.                     offset, address, stream,
  167.                     recurse + 1, val, options,
  168.                     dont_print_vb);

  169.   /* Second, print out data fields */

  170.   /* If there are no data fields, skip this part */
  171.   if (len == n_baseclasses || !len)
  172.     fprintf_filtered (stream, "<No data fields>");
  173.   else
  174.     {
  175.       size_t statmem_obstack_initial_size = 0;
  176.       size_t stat_array_obstack_initial_size = 0;
  177.       struct type *vptr_basetype = NULL;
  178.       int vptr_fieldno;

  179.       if (dont_print_statmem == 0)
  180.         {
  181.           statmem_obstack_initial_size =
  182.             obstack_object_size (&dont_print_statmem_obstack);

  183.           if (last_set_recurse != recurse)
  184.             {
  185.               stat_array_obstack_initial_size =
  186.                 obstack_object_size (&dont_print_stat_array_obstack);

  187.               last_set_recurse = recurse;
  188.             }
  189.         }

  190.       vptr_fieldno = get_vptr_fieldno (type, &vptr_basetype);
  191.       for (i = n_baseclasses; i < len; i++)
  192.         {
  193.           /* If requested, skip printing of static fields.  */
  194.           if (!options->static_field_print
  195.               && field_is_static (&TYPE_FIELD (type, i)))
  196.             continue;

  197.           if (fields_seen)
  198.             fprintf_filtered (stream, ", ");
  199.           else if (n_baseclasses > 0)
  200.             {
  201.               if (options->prettyformat)
  202.                 {
  203.                   fprintf_filtered (stream, "\n");
  204.                   print_spaces_filtered (2 + 2 * recurse, stream);
  205.                   fputs_filtered ("members of ", stream);
  206.                   fputs_filtered (type_name_no_tag (type), stream);
  207.                   fputs_filtered (": ", stream);
  208.                 }
  209.             }
  210.           fields_seen = 1;

  211.           if (options->prettyformat)
  212.             {
  213.               fprintf_filtered (stream, "\n");
  214.               print_spaces_filtered (2 + 2 * recurse, stream);
  215.             }
  216.           else
  217.             {
  218.               wrap_here (n_spaces (2 + 2 * recurse));
  219.             }

  220.           annotate_field_begin (TYPE_FIELD_TYPE (type, i));

  221.           if (field_is_static (&TYPE_FIELD (type, i)))
  222.             fputs_filtered ("static ", stream);
  223.           fprintf_symbol_filtered (stream,
  224.                                    TYPE_FIELD_NAME (type, i),
  225.                                    current_language->la_language,
  226.                                    DMGL_PARAMS | DMGL_ANSI);
  227.           annotate_field_name_end ();
  228.           /* Do not print leading '=' in case of anonymous
  229.              unions.  */
  230.           if (strcmp (TYPE_FIELD_NAME (type, i), ""))
  231.             fputs_filtered (" = ", stream);
  232.           annotate_field_value ();

  233.           if (!field_is_static (&TYPE_FIELD (type, i))
  234.               && TYPE_FIELD_PACKED (type, i))
  235.             {
  236.               struct value *v;

  237.               /* Bitfields require special handling, especially due to
  238.                  byte order problems.  */
  239.               if (TYPE_FIELD_IGNORE (type, i))
  240.                 {
  241.                   fputs_filtered ("<optimized out or zero length>", stream);
  242.                 }
  243.               else if (value_bits_synthetic_pointer (val,
  244.                                                      TYPE_FIELD_BITPOS (type,
  245.                                                                         i),
  246.                                                      TYPE_FIELD_BITSIZE (type,
  247.                                                                          i)))
  248.                 {
  249.                   fputs_filtered (_("<synthetic pointer>"), stream);
  250.                 }
  251.               else
  252.                 {
  253.                   struct value_print_options opts = *options;

  254.                   opts.deref_ref = 0;

  255.                   v = value_field_bitfield (type, i, valaddr, offset, val);

  256.                   common_val_print (v, stream, recurse + 1, &opts,
  257.                                     current_language);
  258.                 }
  259.             }
  260.           else
  261.             {
  262.               if (TYPE_FIELD_IGNORE (type, i))
  263.                 {
  264.                   fputs_filtered ("<optimized out or zero length>",
  265.                                   stream);
  266.                 }
  267.               else if (field_is_static (&TYPE_FIELD (type, i)))
  268.                 {
  269.                   volatile struct gdb_exception ex;
  270.                   struct value *v = NULL;

  271.                   TRY_CATCH (ex, RETURN_MASK_ERROR)
  272.                     {
  273.                       v = value_static_field (type, i);
  274.                     }

  275.                   if (ex.reason < 0)
  276.                     fprintf_filtered (stream,
  277.                                       _("<error reading variable: %s>"),
  278.                                       ex.message);
  279.                   cp_print_static_field (TYPE_FIELD_TYPE (type, i),
  280.                                          v, stream, recurse + 1,
  281.                                          options);
  282.                 }
  283.               else if (i == vptr_fieldno && type == vptr_basetype)
  284.                 {
  285.                   int i_offset = offset + TYPE_FIELD_BITPOS (type, i) / 8;
  286.                   struct type *i_type = TYPE_FIELD_TYPE (type, i);

  287.                   if (valprint_check_validity (stream, i_type, i_offset, val))
  288.                     {
  289.                       CORE_ADDR addr;

  290.                       addr = extract_typed_address (valaddr + i_offset, i_type);
  291.                       print_function_pointer_address (options,
  292.                                                       get_type_arch (type),
  293.                                                       addr, stream);
  294.                     }
  295.                 }
  296.               else
  297.                 {
  298.                   struct value_print_options opts = *options;

  299.                   opts.deref_ref = 0;
  300.                   val_print (TYPE_FIELD_TYPE (type, i),
  301.                              valaddr,
  302.                              offset + TYPE_FIELD_BITPOS (type, i) / 8,
  303.                              address,
  304.                              stream, recurse + 1, val, &opts,
  305.                              current_language);
  306.                 }
  307.             }
  308.           annotate_field_end ();
  309.         }

  310.       if (dont_print_statmem == 0)
  311.         {
  312.           size_t obstack_final_size =
  313.            obstack_object_size (&dont_print_statmem_obstack);

  314.           if (obstack_final_size > statmem_obstack_initial_size)
  315.             {
  316.               /* In effect, a pop of the printed-statics stack.  */

  317.               void *free_to_ptr =
  318.                 (char *) obstack_next_free (&dont_print_statmem_obstack) -
  319.                 (obstack_final_size - statmem_obstack_initial_size);

  320.               obstack_free (&dont_print_statmem_obstack,
  321.                             free_to_ptr);
  322.             }

  323.           if (last_set_recurse != recurse)
  324.             {
  325.               size_t obstack_final_size =
  326.                 obstack_object_size (&dont_print_stat_array_obstack);

  327.               if (obstack_final_size > stat_array_obstack_initial_size)
  328.                 {
  329.                   void *free_to_ptr =
  330.                     (char *) obstack_next_free (&dont_print_stat_array_obstack)
  331.                     - (obstack_final_size
  332.                        - stat_array_obstack_initial_size);

  333.                   obstack_free (&dont_print_stat_array_obstack,
  334.                                 free_to_ptr);
  335.                 }
  336.               last_set_recurse = -1;
  337.             }
  338.         }

  339.       if (options->prettyformat)
  340.         {
  341.           fprintf_filtered (stream, "\n");
  342.           print_spaces_filtered (2 * recurse, stream);
  343.         }
  344.     }                                /* if there are data fields */

  345.   fprintf_filtered (stream, "}");
  346. }

  347. /* Like cp_print_value_fields, but find the runtime type of the object
  348.    and pass it as the `real_type' argument to cp_print_value_fields.
  349.    This function is a hack to work around the fact that
  350.    common_val_print passes the embedded offset to val_print, but not
  351.    the enclosing type.  */

  352. void
  353. cp_print_value_fields_rtti (struct type *type,
  354.                             const gdb_byte *valaddr, int offset,
  355.                             CORE_ADDR address,
  356.                             struct ui_file *stream, int recurse,
  357.                             const struct value *val,
  358.                             const struct value_print_options *options,
  359.                             struct type **dont_print_vb,
  360.                             int dont_print_statmem)
  361. {
  362.   struct type *real_type = NULL;

  363.   /* We require all bits to be valid in order to attempt a
  364.      conversion.  */
  365.   if (!value_bits_any_optimized_out (val,
  366.                                      TARGET_CHAR_BIT * offset,
  367.                                      TARGET_CHAR_BIT * TYPE_LENGTH (type)))
  368.     {
  369.       struct value *value;
  370.       int full, top, using_enc;

  371.       /* Ugh, we have to convert back to a value here.  */
  372.       value = value_from_contents_and_address (type, valaddr + offset,
  373.                                                address + offset);
  374.       type = value_type (value);
  375.       /* We don't actually care about most of the result here -- just
  376.          the type.  We already have the correct offset, due to how
  377.          val_print was initially called.  */
  378.       real_type = value_rtti_type (value, &full, &top, &using_enc);
  379.     }

  380.   if (!real_type)
  381.     real_type = type;

  382.   cp_print_value_fields (type, real_type, valaddr, offset,
  383.                          address, stream, recurse, val, options,
  384.                          dont_print_vb, dont_print_statmem);
  385. }

  386. /* Special val_print routine to avoid printing multiple copies of
  387.    virtual baseclasses.  */

  388. static void
  389. cp_print_value (struct type *type, struct type *real_type,
  390.                 const gdb_byte *valaddr, int offset,
  391.                 CORE_ADDR address, struct ui_file *stream,
  392.                 int recurse, const struct value *val,
  393.                 const struct value_print_options *options,
  394.                 struct type **dont_print_vb)
  395. {
  396.   struct type **last_dont_print
  397.     = (struct type **) obstack_next_free (&dont_print_vb_obstack);
  398.   struct obstack tmp_obstack = dont_print_vb_obstack;
  399.   int i, n_baseclasses = TYPE_N_BASECLASSES (type);
  400.   int thisoffset;
  401.   struct type *thistype;

  402.   if (dont_print_vb == 0)
  403.     {
  404.       /* If we're at top level, carve out a completely fresh chunk of
  405.          the obstack and use that until this particular invocation
  406.          returns.  */
  407.       /* Bump up the high-water mark.  Now alpha is omega.  */
  408.       obstack_finish (&dont_print_vb_obstack);
  409.     }

  410.   for (i = 0; i < n_baseclasses; i++)
  411.     {
  412.       int boffset = 0;
  413.       int skip;
  414.       struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
  415.       const char *basename = TYPE_NAME (baseclass);
  416.       const gdb_byte *base_valaddr = NULL;
  417.       const struct value *base_val = NULL;
  418.       volatile struct gdb_exception ex;

  419.       if (BASETYPE_VIA_VIRTUAL (type, i))
  420.         {
  421.           struct type **first_dont_print
  422.             = (struct type **) obstack_base (&dont_print_vb_obstack);

  423.           int j = (struct type **)
  424.             obstack_next_free (&dont_print_vb_obstack) - first_dont_print;

  425.           while (--j >= 0)
  426.             if (baseclass == first_dont_print[j])
  427.               goto flush_it;

  428.           obstack_ptr_grow (&dont_print_vb_obstack, baseclass);
  429.         }

  430.       thisoffset = offset;
  431.       thistype = real_type;

  432.       TRY_CATCH (ex, RETURN_MASK_ERROR)
  433.         {
  434.           boffset = baseclass_offset (type, i, valaddr, offset, address, val);
  435.         }
  436.       if (ex.reason < 0 && ex.error == NOT_AVAILABLE_ERROR)
  437.         skip = -1;
  438.       else if (ex.reason < 0)
  439.         skip = 1;
  440.       else
  441.          {
  442.           skip = 0;

  443.           if (BASETYPE_VIA_VIRTUAL (type, i))
  444.             {
  445.               /* The virtual base class pointer might have been
  446.                  clobbered by the user program. Make sure that it
  447.                  still points to a valid memory location.  */

  448.               if ((boffset + offset) < 0
  449.                   || (boffset + offset) >= TYPE_LENGTH (real_type))
  450.                 {
  451.                   gdb_byte *buf;
  452.                   struct cleanup *back_to;

  453.                   buf = xmalloc (TYPE_LENGTH (baseclass));
  454.                   back_to = make_cleanup (xfree, buf);

  455.                   if (target_read_memory (address + boffset, buf,
  456.                                           TYPE_LENGTH (baseclass)) != 0)
  457.                     skip = 1;
  458.                   base_val = value_from_contents_and_address (baseclass,
  459.                                                               buf,
  460.                                                               address + boffset);
  461.                   baseclass = value_type (base_val);
  462.                   thisoffset = 0;
  463.                   boffset = 0;
  464.                   thistype = baseclass;
  465.                   base_valaddr = value_contents_for_printing_const (base_val);
  466.                   do_cleanups (back_to);
  467.                 }
  468.               else
  469.                 {
  470.                   base_valaddr = valaddr;
  471.                   base_val = val;
  472.                 }
  473.             }
  474.           else
  475.             {
  476.               base_valaddr = valaddr;
  477.               base_val = val;
  478.             }
  479.         }

  480.       /* Now do the printing.  */
  481.       if (options->prettyformat)
  482.         {
  483.           fprintf_filtered (stream, "\n");
  484.           print_spaces_filtered (2 * recurse, stream);
  485.         }
  486.       fputs_filtered ("<", stream);
  487.       /* Not sure what the best notation is in the case where there is
  488.          no baseclass name.  */
  489.       fputs_filtered (basename ? basename : "", stream);
  490.       fputs_filtered ("> = ", stream);

  491.       if (skip < 0)
  492.         val_print_unavailable (stream);
  493.       else if (skip > 0)
  494.         val_print_invalid_address (stream);
  495.       else
  496.         {
  497.           int result = 0;

  498.           /* Attempt to run an extension language pretty-printer on the
  499.              baseclass if possible.  */
  500.           if (!options->raw)
  501.             result
  502.               = apply_ext_lang_val_pretty_printer (baseclass, base_valaddr,
  503.                                                    thisoffset + boffset,
  504.                                                    value_address (base_val),
  505.                                                    stream, recurse,
  506.                                                    base_val, options,
  507.                                                    current_language);

  508.           if (!result)
  509.             cp_print_value_fields (baseclass, thistype, base_valaddr,
  510.                                    thisoffset + boffset,
  511.                                    value_address (base_val),
  512.                                    stream, recurse, base_val, options,
  513.                                    ((struct type **)
  514.                                     obstack_base (&dont_print_vb_obstack)),
  515.                                    0);
  516.         }
  517.       fputs_filtered (", ", stream);

  518.     flush_it:
  519.       ;
  520.     }

  521.   if (dont_print_vb == 0)
  522.     {
  523.       /* Free the space used to deal with the printing
  524.          of this type from top level.  */
  525.       obstack_free (&dont_print_vb_obstack, last_dont_print);
  526.       /* Reset watermark so that we can continue protecting
  527.          ourselves from whatever we were protecting ourselves.  */
  528.       dont_print_vb_obstack = tmp_obstack;
  529.     }
  530. }

  531. /* Print value of a static member.  To avoid infinite recursion when
  532.    printing a class that contains a static instance of the class, we
  533.    keep the addresses of all printed static member classes in an
  534.    obstack and refuse to print them more than once.

  535.    VAL contains the value to print, TYPE, STREAM, RECURSE, and OPTIONS
  536.    have the same meanings as in c_val_print.  */

  537. static void
  538. cp_print_static_field (struct type *type,
  539.                        struct value *val,
  540.                        struct ui_file *stream,
  541.                        int recurse,
  542.                        const struct value_print_options *options)
  543. {
  544.   struct value_print_options opts;

  545.   if (value_entirely_optimized_out (val))
  546.     {
  547.       val_print_optimized_out (val, stream);
  548.       return;
  549.     }

  550.   if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
  551.     {
  552.       CORE_ADDR *first_dont_print;
  553.       CORE_ADDR addr;
  554.       int i;

  555.       first_dont_print
  556.         = (CORE_ADDR *) obstack_base (&dont_print_statmem_obstack);
  557.       i = obstack_object_size (&dont_print_statmem_obstack)
  558.         / sizeof (CORE_ADDR);

  559.       while (--i >= 0)
  560.         {
  561.           if (value_address (val) == first_dont_print[i])
  562.             {
  563.               fputs_filtered ("<same as static member of an already"
  564.                               " seen type>",
  565.                               stream);
  566.               return;
  567.             }
  568.         }

  569.       addr = value_address (val);
  570.       obstack_grow (&dont_print_statmem_obstack, (char *) &addr,
  571.                     sizeof (CORE_ADDR));
  572.       CHECK_TYPEDEF (type);
  573.       cp_print_value_fields (type, value_enclosing_type (val),
  574.                              value_contents_for_printing (val),
  575.                              value_embedded_offset (val), addr,
  576.                              stream, recurse, val,
  577.                              options, NULL, 1);
  578.       return;
  579.     }

  580.   if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
  581.     {
  582.       struct type **first_dont_print;
  583.       int i;
  584.       struct type *target_type = TYPE_TARGET_TYPE (type);

  585.       first_dont_print
  586.         = (struct type **) obstack_base (&dont_print_stat_array_obstack);
  587.       i = obstack_object_size (&dont_print_stat_array_obstack)
  588.         / sizeof (struct type *);

  589.       while (--i >= 0)
  590.         {
  591.           if (target_type == first_dont_print[i])
  592.             {
  593.               fputs_filtered ("<same as static member of an already"
  594.                               " seen type>",
  595.                               stream);
  596.               return;
  597.             }
  598.         }

  599.       obstack_grow (&dont_print_stat_array_obstack,
  600.                     (char *) &target_type,
  601.                     sizeof (struct type *));
  602.     }

  603.   opts = *options;
  604.   opts.deref_ref = 0;
  605.   val_print (type, value_contents_for_printing (val),
  606.              value_embedded_offset (val),
  607.              value_address (val),
  608.              stream, recurse, val,
  609.              &opts, current_language);
  610. }


  611. /* Find the field in *DOMAIN, or its non-virtual base classes, with
  612.    bit offset OFFSET.  Set *DOMAIN to the containing type and *FIELDNO
  613.    to the containing field number.  If OFFSET is not exactly at the
  614.    start of some field, set *DOMAIN to NULL.  */

  615. static void
  616. cp_find_class_member (struct type **domain_p, int *fieldno,
  617.                       LONGEST offset)
  618. {
  619.   struct type *domain;
  620.   unsigned int i;
  621.   unsigned len;

  622.   *domain_p = check_typedef (*domain_p);
  623.   domain = *domain_p;
  624.   len = TYPE_NFIELDS (domain);

  625.   for (i = TYPE_N_BASECLASSES (domain); i < len; i++)
  626.     {
  627.       LONGEST bitpos = TYPE_FIELD_BITPOS (domain, i);

  628.       QUIT;
  629.       if (offset == bitpos)
  630.         {
  631.           *fieldno = i;
  632.           return;
  633.         }
  634.     }

  635.   for (i = 0; i < TYPE_N_BASECLASSES (domain); i++)
  636.     {
  637.       LONGEST bitpos = TYPE_FIELD_BITPOS (domain, i);
  638.       LONGEST bitsize = 8 * TYPE_LENGTH (TYPE_FIELD_TYPE (domain, i));

  639.       if (offset >= bitpos && offset < bitpos + bitsize)
  640.         {
  641.           *domain_p = TYPE_FIELD_TYPE (domain, i);
  642.           cp_find_class_member (domain_p, fieldno, offset - bitpos);
  643.           return;
  644.         }
  645.     }

  646.   *domain_p = NULL;
  647. }

  648. void
  649. cp_print_class_member (const gdb_byte *valaddr, struct type *type,
  650.                        struct ui_file *stream, char *prefix)
  651. {
  652.   enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));

  653.   /* VAL is a byte offset into the structure type DOMAIN.
  654.      Find the name of the field for that offset and
  655.      print it.  */
  656.   struct type *domain = TYPE_DOMAIN_TYPE (type);
  657.   LONGEST val;
  658.   int fieldno;

  659.   val = extract_signed_integer (valaddr,
  660.                                 TYPE_LENGTH (type),
  661.                                 byte_order);

  662.   /* Pointers to data members are usually byte offsets into an object.
  663.      Because a data member can have offset zero, and a NULL pointer to
  664.      member must be distinct from any valid non-NULL pointer to
  665.      member, either the value is biased or the NULL value has a
  666.      special representation; both are permitted by ISO C++.  HP aCC
  667.      used a bias of 0x20000000; HP cfront used a bias of 1; g++ 3.x
  668.      and other compilers which use the Itanium ABI use -1 as the NULL
  669.      value.  GDB only supports that last form; to add support for
  670.      another form, make this into a cp-abi hook.  */

  671.   if (val == -1)
  672.     {
  673.       fprintf_filtered (stream, "NULL");
  674.       return;
  675.     }

  676.   cp_find_class_member (&domain, &fieldno, val << 3);

  677.   if (domain != NULL)
  678.     {
  679.       const char *name;

  680.       fputs_filtered (prefix, stream);
  681.       name = type_name_no_tag (domain);
  682.       if (name)
  683.         fputs_filtered (name, stream);
  684.       else
  685.         c_type_print_base (domain, stream, 0, 0, &type_print_raw_options);
  686.       fprintf_filtered (stream, "::");
  687.       fputs_filtered (TYPE_FIELD_NAME (domain, fieldno), stream);
  688.     }
  689.   else
  690.     fprintf_filtered (stream, "%ld", (long) val);
  691. }


  692. void
  693. _initialize_cp_valprint (void)
  694. {
  695.   add_setshow_boolean_cmd ("static-members", class_support,
  696.                            &user_print_options.static_field_print, _("\
  697. Set printing of C++ static members."), _("\
  698. Show printing of C++ static members."), NULL,
  699.                            NULL,
  700.                            show_static_field_print,
  701.                            &setprintlist, &showprintlist);

  702.   add_setshow_boolean_cmd ("vtbl", class_support,
  703.                            &user_print_options.vtblprint, _("\
  704. Set printing of C++ virtual function tables."), _("\
  705. Show printing of C++ virtual function tables."), NULL,
  706.                            NULL,
  707.                            show_vtblprint,
  708.                            &setprintlist, &showprintlist);

  709.   add_setshow_boolean_cmd ("object", class_support,
  710.                            &user_print_options.objectprint, _("\
  711. Set printing of object's derived type based on vtable info."), _("\
  712. Show printing of object's derived type based on vtable info."), NULL,
  713.                            NULL,
  714.                            show_objectprint,
  715.                            &setprintlist, &showprintlist);

  716.   obstack_begin (&dont_print_stat_array_obstack,
  717.                  32 * sizeof (struct type *));
  718.   obstack_begin (&dont_print_statmem_obstack,
  719.                  32 * sizeof (CORE_ADDR));
  720.   obstack_begin (&dont_print_vb_obstack,
  721.                  32 * sizeof (struct type *));
  722. }