GDB (API)
/home/stan/gdb/src/gdb/cp-valprint.c
Go to the documentation of this file.
00001 /* Support for printing C++ values 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 "symtab.h"
00023 #include "gdbtypes.h"
00024 #include "expression.h"
00025 #include "value.h"
00026 #include "command.h"
00027 #include "gdbcmd.h"
00028 #include "demangle.h"
00029 #include "annotate.h"
00030 #include "gdb_string.h"
00031 #include "c-lang.h"
00032 #include "target.h"
00033 #include "cp-abi.h"
00034 #include "valprint.h"
00035 #include "cp-support.h"
00036 #include "language.h"
00037 #include "python/python.h"
00038 #include "exceptions.h"
00039 #include "typeprint.h"
00040 
00041 /* Controls printing of vtbl's.  */
00042 static void
00043 show_vtblprint (struct ui_file *file, int from_tty,
00044                 struct cmd_list_element *c, const char *value)
00045 {
00046   fprintf_filtered (file, _("\
00047 Printing of C++ virtual function tables is %s.\n"),
00048                     value);
00049 }
00050 
00051 /* Controls looking up an object's derived type using what we find in
00052    its vtables.  */
00053 static void
00054 show_objectprint (struct ui_file *file, int from_tty,
00055                   struct cmd_list_element *c,
00056                   const char *value)
00057 {
00058   fprintf_filtered (file, _("\
00059 Printing of object's derived type based on vtable info is %s.\n"),
00060                     value);
00061 }
00062 
00063 static void
00064 show_static_field_print (struct ui_file *file, int from_tty,
00065                          struct cmd_list_element *c,
00066                          const char *value)
00067 {
00068   fprintf_filtered (file,
00069                     _("Printing of C++ static members is %s.\n"),
00070                     value);
00071 }
00072 
00073 
00074 static struct obstack dont_print_vb_obstack;
00075 static struct obstack dont_print_statmem_obstack;
00076 static struct obstack dont_print_stat_array_obstack;
00077 
00078 extern void _initialize_cp_valprint (void);
00079 
00080 static void cp_print_static_field (struct type *, struct value *,
00081                                    struct ui_file *, int,
00082                                    const struct value_print_options *);
00083 
00084 static void cp_print_value (struct type *, struct type *,
00085                             const gdb_byte *, int,
00086                             CORE_ADDR, struct ui_file *,
00087                             int, const struct value *,
00088                             const struct value_print_options *,
00089                             struct type **);
00090 
00091 
00092 /* GCC versions after 2.4.5 use this.  */
00093 const char vtbl_ptr_name[] = "__vtbl_ptr_type";
00094 
00095 /* Return truth value for assertion that TYPE is of the type
00096    "pointer to virtual function".  */
00097 
00098 int
00099 cp_is_vtbl_ptr_type (struct type *type)
00100 {
00101   const char *typename = type_name_no_tag (type);
00102 
00103   return (typename != NULL && !strcmp (typename, vtbl_ptr_name));
00104 }
00105 
00106 /* Return truth value for the assertion that TYPE is of the type
00107    "pointer to virtual function table".  */
00108 
00109 int
00110 cp_is_vtbl_member (struct type *type)
00111 {
00112   /* With older versions of g++, the vtbl field pointed to an array of
00113      structures.  Nowadays it points directly to the structure.  */
00114   if (TYPE_CODE (type) == TYPE_CODE_PTR)
00115     {
00116       type = TYPE_TARGET_TYPE (type);
00117       if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
00118         {
00119           type = TYPE_TARGET_TYPE (type);
00120           if (TYPE_CODE (type) == TYPE_CODE_STRUCT    /* if not using thunks */
00121               || TYPE_CODE (type) == TYPE_CODE_PTR)   /* if using thunks */
00122             {
00123               /* Virtual functions tables are full of pointers
00124                  to virtual functions.  */
00125               return cp_is_vtbl_ptr_type (type);
00126             }
00127         }
00128       else if (TYPE_CODE (type) == TYPE_CODE_STRUCT)  /* if not using thunks */
00129         {
00130           return cp_is_vtbl_ptr_type (type);
00131         }
00132       else if (TYPE_CODE (type) == TYPE_CODE_PTR)     /* if using thunks */
00133         {
00134           /* The type name of the thunk pointer is NULL when using
00135              dwarf2.  We could test for a pointer to a function, but
00136              there is no type info for the virtual table either, so it
00137              wont help.  */
00138           return cp_is_vtbl_ptr_type (type);
00139         }
00140     }
00141   return 0;
00142 }
00143 
00144 /* Mutually recursive subroutines of cp_print_value and c_val_print to
00145    print out a structure's fields: cp_print_value_fields and
00146    cp_print_value.
00147 
00148    TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and OPTIONS have the same
00149    meanings as in cp_print_value and c_val_print.
00150 
00151    2nd argument REAL_TYPE is used to carry over the type of the
00152    derived class across the recursion to base classes.
00153 
00154    DONT_PRINT is an array of baseclass types that we should not print,
00155    or zero if called from top level.  */
00156 
00157 void
00158 cp_print_value_fields (struct type *type, struct type *real_type,
00159                        const gdb_byte *valaddr, int offset,
00160                        CORE_ADDR address, struct ui_file *stream,
00161                        int recurse, const struct value *val,
00162                        const struct value_print_options *options,
00163                        struct type **dont_print_vb,
00164                        int dont_print_statmem)
00165 {
00166   int i, len, n_baseclasses;
00167   int fields_seen = 0;
00168   static int last_set_recurse = -1;
00169 
00170   CHECK_TYPEDEF (type);
00171   
00172   if (recurse == 0)
00173     {
00174       /* Any object can be left on obstacks only during an unexpected
00175          error.  */
00176 
00177       if (obstack_object_size (&dont_print_statmem_obstack) > 0)
00178         {
00179           obstack_free (&dont_print_statmem_obstack, NULL);
00180           obstack_begin (&dont_print_statmem_obstack,
00181                          32 * sizeof (CORE_ADDR));
00182         }
00183       if (obstack_object_size (&dont_print_stat_array_obstack) > 0)
00184         {
00185           obstack_free (&dont_print_stat_array_obstack, NULL);
00186           obstack_begin (&dont_print_stat_array_obstack,
00187                          32 * sizeof (struct type *));
00188         }
00189     }
00190 
00191   fprintf_filtered (stream, "{");
00192   len = TYPE_NFIELDS (type);
00193   n_baseclasses = TYPE_N_BASECLASSES (type);
00194 
00195   /* First, print out baseclasses such that we don't print
00196      duplicates of virtual baseclasses.  */
00197 
00198   if (n_baseclasses > 0)
00199     cp_print_value (type, real_type, valaddr, 
00200                     offset, address, stream,
00201                     recurse + 1, val, options,
00202                     dont_print_vb);
00203 
00204   /* Second, print out data fields */
00205 
00206   /* If there are no data fields, skip this part */
00207   if (len == n_baseclasses || !len)
00208     fprintf_filtered (stream, "<No data fields>");
00209   else
00210     {
00211       int statmem_obstack_initial_size = 0;
00212       int stat_array_obstack_initial_size = 0;
00213       struct type *vptr_basetype = NULL;
00214       int vptr_fieldno;
00215 
00216       if (dont_print_statmem == 0)
00217         {
00218           statmem_obstack_initial_size =
00219             obstack_object_size (&dont_print_statmem_obstack);
00220 
00221           if (last_set_recurse != recurse)
00222             {
00223               stat_array_obstack_initial_size =
00224                 obstack_object_size (&dont_print_stat_array_obstack);
00225 
00226               last_set_recurse = recurse;
00227             }
00228         }
00229 
00230       vptr_fieldno = get_vptr_fieldno (type, &vptr_basetype);
00231       for (i = n_baseclasses; i < len; i++)
00232         {
00233           /* If requested, skip printing of static fields.  */
00234           if (!options->static_field_print
00235               && field_is_static (&TYPE_FIELD (type, i)))
00236             continue;
00237 
00238           if (fields_seen)
00239             fprintf_filtered (stream, ", ");
00240           else if (n_baseclasses > 0)
00241             {
00242               if (options->prettyformat)
00243                 {
00244                   fprintf_filtered (stream, "\n");
00245                   print_spaces_filtered (2 + 2 * recurse, stream);
00246                   fputs_filtered ("members of ", stream);
00247                   fputs_filtered (type_name_no_tag (type), stream);
00248                   fputs_filtered (": ", stream);
00249                 }
00250             }
00251           fields_seen = 1;
00252 
00253           if (options->prettyformat)
00254             {
00255               fprintf_filtered (stream, "\n");
00256               print_spaces_filtered (2 + 2 * recurse, stream);
00257             }
00258           else
00259             {
00260               wrap_here (n_spaces (2 + 2 * recurse));
00261             }
00262 
00263           annotate_field_begin (TYPE_FIELD_TYPE (type, i));
00264 
00265           if (field_is_static (&TYPE_FIELD (type, i)))
00266             fputs_filtered ("static ", stream);
00267           fprintf_symbol_filtered (stream,
00268                                    TYPE_FIELD_NAME (type, i),
00269                                    current_language->la_language,
00270                                    DMGL_PARAMS | DMGL_ANSI);
00271           annotate_field_name_end ();
00272           /* Do not print leading '=' in case of anonymous
00273              unions.  */
00274           if (strcmp (TYPE_FIELD_NAME (type, i), ""))
00275             fputs_filtered (" = ", stream);
00276           annotate_field_value ();
00277 
00278           if (!field_is_static (&TYPE_FIELD (type, i))
00279               && TYPE_FIELD_PACKED (type, i))
00280             {
00281               struct value *v;
00282 
00283               /* Bitfields require special handling, especially due to
00284                  byte order problems.  */
00285               if (TYPE_FIELD_IGNORE (type, i))
00286                 {
00287                   fputs_filtered ("<optimized out or zero length>", stream);
00288                 }
00289               else if (value_bits_synthetic_pointer (val,
00290                                                      TYPE_FIELD_BITPOS (type,
00291                                                                         i),
00292                                                      TYPE_FIELD_BITSIZE (type,
00293                                                                          i)))
00294                 {
00295                   fputs_filtered (_("<synthetic pointer>"), stream);
00296                 }
00297               else if (!value_bits_valid (val,
00298                                           TYPE_FIELD_BITPOS (type, i),
00299                                           TYPE_FIELD_BITSIZE (type, i)))
00300                 {
00301                   val_print_optimized_out (val, stream);
00302                 }
00303               else
00304                 {
00305                   struct value_print_options opts = *options;
00306 
00307                   opts.deref_ref = 0;
00308 
00309                   v = value_field_bitfield (type, i, valaddr, offset, val);
00310 
00311                   common_val_print (v, stream, recurse + 1, &opts,
00312                                     current_language);
00313                 }
00314             }
00315           else
00316             {
00317               if (TYPE_FIELD_IGNORE (type, i))
00318                 {
00319                   fputs_filtered ("<optimized out or zero length>",
00320                                   stream);
00321                 }
00322               else if (field_is_static (&TYPE_FIELD (type, i)))
00323                 {
00324                   volatile struct gdb_exception ex;
00325                   struct value *v = NULL;
00326 
00327                   TRY_CATCH (ex, RETURN_MASK_ERROR)
00328                     {
00329                       v = value_static_field (type, i);
00330                     }
00331 
00332                   if (ex.reason < 0)
00333                     fprintf_filtered (stream,
00334                                       _("<error reading variable: %s>"),
00335                                       ex.message);
00336                   else if (v == NULL)
00337                     val_print_optimized_out (NULL, stream);
00338                   else
00339                     cp_print_static_field (TYPE_FIELD_TYPE (type, i),
00340                                            v, stream, recurse + 1,
00341                                            options);
00342                 }
00343               else if (i == vptr_fieldno && type == vptr_basetype)
00344                 {
00345                   int i_offset = offset + TYPE_FIELD_BITPOS (type, i) / 8;
00346                   struct type *i_type = TYPE_FIELD_TYPE (type, i);
00347 
00348                   if (valprint_check_validity (stream, i_type, i_offset, val))
00349                     {
00350                       CORE_ADDR addr;
00351                       
00352                       addr = extract_typed_address (valaddr + i_offset, i_type);
00353                       print_function_pointer_address (options,
00354                                                       get_type_arch (type),
00355                                                       addr, stream);
00356                     }
00357                 }
00358               else
00359                 {
00360                   struct value_print_options opts = *options;
00361 
00362                   opts.deref_ref = 0;
00363                   val_print (TYPE_FIELD_TYPE (type, i),
00364                              valaddr, 
00365                              offset + TYPE_FIELD_BITPOS (type, i) / 8,
00366                              address,
00367                              stream, recurse + 1, val, &opts,
00368                              current_language);
00369                 }
00370             }
00371           annotate_field_end ();
00372         }
00373 
00374       if (dont_print_statmem == 0)
00375         {
00376           int obstack_final_size =
00377            obstack_object_size (&dont_print_statmem_obstack);
00378 
00379           if (obstack_final_size > statmem_obstack_initial_size)
00380             {
00381               /* In effect, a pop of the printed-statics stack.  */
00382 
00383               void *free_to_ptr =
00384                 obstack_next_free (&dont_print_statmem_obstack) -
00385                 (obstack_final_size - statmem_obstack_initial_size);
00386 
00387               obstack_free (&dont_print_statmem_obstack,
00388                             free_to_ptr);
00389             }
00390 
00391           if (last_set_recurse != recurse)
00392             {
00393               int obstack_final_size =
00394                 obstack_object_size (&dont_print_stat_array_obstack);
00395               
00396               if (obstack_final_size > stat_array_obstack_initial_size)
00397                 {
00398                   void *free_to_ptr =
00399                     obstack_next_free (&dont_print_stat_array_obstack)
00400                     - (obstack_final_size
00401                        - stat_array_obstack_initial_size);
00402 
00403                   obstack_free (&dont_print_stat_array_obstack,
00404                                 free_to_ptr);
00405                 }
00406               last_set_recurse = -1;
00407             }
00408         }
00409 
00410       if (options->prettyformat)
00411         {
00412           fprintf_filtered (stream, "\n");
00413           print_spaces_filtered (2 * recurse, stream);
00414         }
00415     }                           /* if there are data fields */
00416 
00417   fprintf_filtered (stream, "}");
00418 }
00419 
00420 /* Like cp_print_value_fields, but find the runtime type of the object
00421    and pass it as the `real_type' argument to cp_print_value_fields.
00422    This function is a hack to work around the fact that
00423    common_val_print passes the embedded offset to val_print, but not
00424    the enclosing type.  */
00425 
00426 void
00427 cp_print_value_fields_rtti (struct type *type,
00428                             const gdb_byte *valaddr, int offset,
00429                             CORE_ADDR address,
00430                             struct ui_file *stream, int recurse,
00431                             const struct value *val,
00432                             const struct value_print_options *options,
00433                             struct type **dont_print_vb, 
00434                             int dont_print_statmem)
00435 {
00436   struct type *real_type = NULL;
00437 
00438   /* We require all bits to be valid in order to attempt a
00439      conversion.  */
00440   if (value_bits_valid (val, TARGET_CHAR_BIT * offset,
00441                         TARGET_CHAR_BIT * TYPE_LENGTH (type)))
00442     {
00443       struct value *value;
00444       int full, top, using_enc;
00445 
00446       /* Ugh, we have to convert back to a value here.  */
00447       value = value_from_contents_and_address (type, valaddr + offset,
00448                                                address + offset);
00449       /* We don't actually care about most of the result here -- just
00450          the type.  We already have the correct offset, due to how
00451          val_print was initially called.  */
00452       real_type = value_rtti_type (value, &full, &top, &using_enc);
00453     }
00454 
00455   if (!real_type)
00456     real_type = type;
00457 
00458   cp_print_value_fields (type, real_type, valaddr, offset,
00459                          address, stream, recurse, val, options,
00460                          dont_print_vb, dont_print_statmem);
00461 }
00462 
00463 /* Special val_print routine to avoid printing multiple copies of
00464    virtual baseclasses.  */
00465 
00466 static void
00467 cp_print_value (struct type *type, struct type *real_type,
00468                 const gdb_byte *valaddr, int offset,
00469                 CORE_ADDR address, struct ui_file *stream,
00470                 int recurse, const struct value *val,
00471                 const struct value_print_options *options,
00472                 struct type **dont_print_vb)
00473 {
00474   struct type **last_dont_print
00475     = (struct type **) obstack_next_free (&dont_print_vb_obstack);
00476   struct obstack tmp_obstack = dont_print_vb_obstack;
00477   int i, n_baseclasses = TYPE_N_BASECLASSES (type);
00478   int thisoffset;
00479   struct type *thistype;
00480 
00481   if (dont_print_vb == 0)
00482     {
00483       /* If we're at top level, carve out a completely fresh chunk of
00484          the obstack and use that until this particular invocation
00485          returns.  */
00486       /* Bump up the high-water mark.  Now alpha is omega.  */
00487       obstack_finish (&dont_print_vb_obstack);
00488     }
00489 
00490   for (i = 0; i < n_baseclasses; i++)
00491     {
00492       int boffset = 0;
00493       int skip;
00494       struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
00495       const char *basename = TYPE_NAME (baseclass);
00496       const gdb_byte *base_valaddr = NULL;
00497       const struct value *base_val = NULL;
00498       volatile struct gdb_exception ex;
00499 
00500       if (BASETYPE_VIA_VIRTUAL (type, i))
00501         {
00502           struct type **first_dont_print
00503             = (struct type **) obstack_base (&dont_print_vb_obstack);
00504 
00505           int j = (struct type **)
00506             obstack_next_free (&dont_print_vb_obstack) - first_dont_print;
00507 
00508           while (--j >= 0)
00509             if (baseclass == first_dont_print[j])
00510               goto flush_it;
00511 
00512           obstack_ptr_grow (&dont_print_vb_obstack, baseclass);
00513         }
00514 
00515       thisoffset = offset;
00516       thistype = real_type;
00517 
00518       TRY_CATCH (ex, RETURN_MASK_ERROR)
00519         {
00520           boffset = baseclass_offset (type, i, valaddr, offset, address, val);
00521         }
00522       if (ex.reason < 0 && ex.error == NOT_AVAILABLE_ERROR)
00523         skip = -1;
00524       else if (ex.reason < 0)
00525         skip = 1;
00526       else
00527         {
00528           skip = 0;
00529 
00530           if (BASETYPE_VIA_VIRTUAL (type, i))
00531             {
00532               /* The virtual base class pointer might have been
00533                  clobbered by the user program. Make sure that it
00534                  still points to a valid memory location.  */
00535 
00536               if ((boffset + offset) < 0
00537                   || (boffset + offset) >= TYPE_LENGTH (real_type))
00538                 {
00539                   gdb_byte *buf;
00540                   struct cleanup *back_to;
00541 
00542                   buf = xmalloc (TYPE_LENGTH (baseclass));
00543                   back_to = make_cleanup (xfree, buf);
00544 
00545                   if (target_read_memory (address + boffset, buf,
00546                                           TYPE_LENGTH (baseclass)) != 0)
00547                     skip = 1;
00548                   base_val = value_from_contents_and_address (baseclass,
00549                                                               buf,
00550                                                               address + boffset);
00551                   thisoffset = 0;
00552                   boffset = 0;
00553                   thistype = baseclass;
00554                   base_valaddr = value_contents_for_printing_const (base_val);
00555                   do_cleanups (back_to);
00556                 }
00557               else
00558                 {
00559                   base_valaddr = valaddr;
00560                   base_val = val;
00561                 }
00562             }
00563           else
00564             {
00565               base_valaddr = valaddr;
00566               base_val = val;
00567             }
00568         }
00569 
00570       /* Now do the printing.  */
00571       if (options->prettyformat)
00572         {
00573           fprintf_filtered (stream, "\n");
00574           print_spaces_filtered (2 * recurse, stream);
00575         }
00576       fputs_filtered ("<", stream);
00577       /* Not sure what the best notation is in the case where there is
00578          no baseclass name.  */
00579       fputs_filtered (basename ? basename : "", stream);
00580       fputs_filtered ("> = ", stream);
00581 
00582       if (skip < 0)
00583         val_print_unavailable (stream);
00584       else if (skip > 0)
00585         val_print_invalid_address (stream);
00586       else
00587         {
00588           int result = 0;
00589 
00590           /* Attempt to run the Python pretty-printers on the
00591              baseclass if possible.  */
00592           if (!options->raw)
00593             result = apply_val_pretty_printer (baseclass, base_valaddr,
00594                                                thisoffset + boffset,
00595                                                value_address (base_val),
00596                                                stream, recurse, base_val,
00597                                                options, current_language);
00598 
00599 
00600                   
00601           if (!result)
00602             cp_print_value_fields (baseclass, thistype, base_valaddr,
00603                                    thisoffset + boffset,
00604                                    value_address (base_val),
00605                                    stream, recurse, base_val, options,
00606                                    ((struct type **)
00607                                     obstack_base (&dont_print_vb_obstack)),
00608                                    0);
00609         }
00610       fputs_filtered (", ", stream);
00611 
00612     flush_it:
00613       ;
00614     }
00615 
00616   if (dont_print_vb == 0)
00617     {
00618       /* Free the space used to deal with the printing
00619          of this type from top level.  */
00620       obstack_free (&dont_print_vb_obstack, last_dont_print);
00621       /* Reset watermark so that we can continue protecting
00622          ourselves from whatever we were protecting ourselves.  */
00623       dont_print_vb_obstack = tmp_obstack;
00624     }
00625 }
00626 
00627 /* Print value of a static member.  To avoid infinite recursion when
00628    printing a class that contains a static instance of the class, we
00629    keep the addresses of all printed static member classes in an
00630    obstack and refuse to print them more than once.
00631 
00632    VAL contains the value to print, TYPE, STREAM, RECURSE, and OPTIONS
00633    have the same meanings as in c_val_print.  */
00634 
00635 static void
00636 cp_print_static_field (struct type *type,
00637                        struct value *val,
00638                        struct ui_file *stream,
00639                        int recurse,
00640                        const struct value_print_options *options)
00641 {
00642   struct value_print_options opts;
00643   
00644   if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
00645     {
00646       CORE_ADDR *first_dont_print;
00647       CORE_ADDR addr;
00648       int i;
00649 
00650       first_dont_print
00651         = (CORE_ADDR *) obstack_base (&dont_print_statmem_obstack);
00652       i = obstack_object_size (&dont_print_statmem_obstack)
00653         / sizeof (CORE_ADDR);
00654 
00655       while (--i >= 0)
00656         {
00657           if (value_address (val) == first_dont_print[i])
00658             {
00659               fputs_filtered ("<same as static member of an already"
00660                               " seen type>",
00661                               stream);
00662               return;
00663             }
00664         }
00665 
00666       addr = value_address (val);
00667       obstack_grow (&dont_print_statmem_obstack, (char *) &addr,
00668                     sizeof (CORE_ADDR));
00669       CHECK_TYPEDEF (type);
00670       cp_print_value_fields (type, value_enclosing_type (val),
00671                              value_contents_for_printing (val),
00672                              value_embedded_offset (val), addr,
00673                              stream, recurse, val,
00674                              options, NULL, 1);
00675       return;
00676     }
00677 
00678   if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
00679     {
00680       struct type **first_dont_print;
00681       int i;
00682       struct type *target_type = TYPE_TARGET_TYPE (type);
00683 
00684       first_dont_print
00685         = (struct type **) obstack_base (&dont_print_stat_array_obstack);
00686       i = obstack_object_size (&dont_print_stat_array_obstack)
00687         / sizeof (struct type *);
00688 
00689       while (--i >= 0)
00690         {
00691           if (target_type == first_dont_print[i])
00692             {
00693               fputs_filtered ("<same as static member of an already"
00694                               " seen type>",
00695                               stream);
00696               return;
00697             }
00698         }
00699 
00700       obstack_grow (&dont_print_stat_array_obstack,
00701                     (char *) &target_type,
00702                     sizeof (struct type *));
00703     }
00704 
00705   opts = *options;
00706   opts.deref_ref = 0;
00707   val_print (type, value_contents_for_printing (val), 
00708              value_embedded_offset (val),
00709              value_address (val),
00710              stream, recurse, val,
00711              &opts, current_language);
00712 }
00713 
00714 
00715 /* Find the field in *DOMAIN, or its non-virtual base classes, with
00716    bit offset OFFSET.  Set *DOMAIN to the containing type and *FIELDNO
00717    to the containing field number.  If OFFSET is not exactly at the
00718    start of some field, set *DOMAIN to NULL.  */
00719 
00720 static void
00721 cp_find_class_member (struct type **domain_p, int *fieldno,
00722                       LONGEST offset)
00723 {
00724   struct type *domain;
00725   unsigned int i;
00726   unsigned len;
00727 
00728   *domain_p = check_typedef (*domain_p);
00729   domain = *domain_p;
00730   len = TYPE_NFIELDS (domain);
00731 
00732   for (i = TYPE_N_BASECLASSES (domain); i < len; i++)
00733     {
00734       LONGEST bitpos = TYPE_FIELD_BITPOS (domain, i);
00735 
00736       QUIT;
00737       if (offset == bitpos)
00738         {
00739           *fieldno = i;
00740           return;
00741         }
00742     }
00743 
00744   for (i = 0; i < TYPE_N_BASECLASSES (domain); i++)
00745     {
00746       LONGEST bitpos = TYPE_FIELD_BITPOS (domain, i);
00747       LONGEST bitsize = 8 * TYPE_LENGTH (TYPE_FIELD_TYPE (domain, i));
00748 
00749       if (offset >= bitpos && offset < bitpos + bitsize)
00750         {
00751           *domain_p = TYPE_FIELD_TYPE (domain, i);
00752           cp_find_class_member (domain_p, fieldno, offset - bitpos);
00753           return;
00754         }
00755     }
00756 
00757   *domain_p = NULL;
00758 }
00759 
00760 void
00761 cp_print_class_member (const gdb_byte *valaddr, struct type *type,
00762                        struct ui_file *stream, char *prefix)
00763 {
00764   enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
00765 
00766   /* VAL is a byte offset into the structure type DOMAIN.
00767      Find the name of the field for that offset and
00768      print it.  */
00769   struct type *domain = TYPE_DOMAIN_TYPE (type);
00770   LONGEST val;
00771   int fieldno;
00772 
00773   val = extract_signed_integer (valaddr,
00774                                 TYPE_LENGTH (type),
00775                                 byte_order);
00776 
00777   /* Pointers to data members are usually byte offsets into an object.
00778      Because a data member can have offset zero, and a NULL pointer to
00779      member must be distinct from any valid non-NULL pointer to
00780      member, either the value is biased or the NULL value has a
00781      special representation; both are permitted by ISO C++.  HP aCC
00782      used a bias of 0x20000000; HP cfront used a bias of 1; g++ 3.x
00783      and other compilers which use the Itanium ABI use -1 as the NULL
00784      value.  GDB only supports that last form; to add support for
00785      another form, make this into a cp-abi hook.  */
00786 
00787   if (val == -1)
00788     {
00789       fprintf_filtered (stream, "NULL");
00790       return;
00791     }
00792 
00793   cp_find_class_member (&domain, &fieldno, val << 3);
00794 
00795   if (domain != NULL)
00796     {
00797       const char *name;
00798 
00799       fputs_filtered (prefix, stream);
00800       name = type_name_no_tag (domain);
00801       if (name)
00802         fputs_filtered (name, stream);
00803       else
00804         c_type_print_base (domain, stream, 0, 0, &type_print_raw_options);
00805       fprintf_filtered (stream, "::");
00806       fputs_filtered (TYPE_FIELD_NAME (domain, fieldno), stream);
00807     }
00808   else
00809     fprintf_filtered (stream, "%ld", (long) val);
00810 }
00811 
00812 
00813 void
00814 _initialize_cp_valprint (void)
00815 {
00816   add_setshow_boolean_cmd ("static-members", class_support,
00817                            &user_print_options.static_field_print, _("\
00818 Set printing of C++ static members."), _("\
00819 Show printing of C++ static members."), NULL,
00820                            NULL,
00821                            show_static_field_print,
00822                            &setprintlist, &showprintlist);
00823 
00824   add_setshow_boolean_cmd ("vtbl", class_support,
00825                            &user_print_options.vtblprint, _("\
00826 Set printing of C++ virtual function tables."), _("\
00827 Show printing of C++ virtual function tables."), NULL,
00828                            NULL,
00829                            show_vtblprint,
00830                            &setprintlist, &showprintlist);
00831 
00832   add_setshow_boolean_cmd ("object", class_support,
00833                            &user_print_options.objectprint, _("\
00834 Set printing of object's derived type based on vtable info."), _("\
00835 Show printing of object's derived type based on vtable info."), NULL,
00836                            NULL,
00837                            show_objectprint,
00838                            &setprintlist, &showprintlist);
00839 
00840   obstack_begin (&dont_print_stat_array_obstack,
00841                  32 * sizeof (struct type *));
00842   obstack_begin (&dont_print_statmem_obstack,
00843                  32 * sizeof (CORE_ADDR));
00844   obstack_begin (&dont_print_vb_obstack,
00845                  32 * sizeof (struct type *));
00846 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines