GDB (API)
/home/stan/gdb/src/gdb/cp-abi.c
Go to the documentation of this file.
00001 /* Generic code for supporting multiple C++ ABI's
00002 
00003    Copyright (C) 2001-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 "value.h"
00022 #include "cp-abi.h"
00023 #include "command.h"
00024 #include "exceptions.h"
00025 #include "gdbcmd.h"
00026 #include "ui-out.h"
00027 #include "gdb_assert.h"
00028 #include "gdb_string.h"
00029 
00030 static struct cp_abi_ops *find_cp_abi (const char *short_name);
00031 
00032 static struct cp_abi_ops current_cp_abi = { "", NULL };
00033 static struct cp_abi_ops auto_cp_abi = { "auto", NULL };
00034 
00035 #define CP_ABI_MAX 8
00036 static struct cp_abi_ops *cp_abis[CP_ABI_MAX];
00037 static int num_cp_abis = 0;
00038 
00039 enum ctor_kinds
00040 is_constructor_name (const char *name)
00041 {
00042   if ((current_cp_abi.is_constructor_name) == NULL)
00043     error (_("ABI doesn't define required function is_constructor_name"));
00044   return (*current_cp_abi.is_constructor_name) (name);
00045 }
00046 
00047 enum dtor_kinds
00048 is_destructor_name (const char *name)
00049 {
00050   if ((current_cp_abi.is_destructor_name) == NULL)
00051     error (_("ABI doesn't define required function is_destructor_name"));
00052   return (*current_cp_abi.is_destructor_name) (name);
00053 }
00054 
00055 int
00056 is_vtable_name (const char *name)
00057 {
00058   if ((current_cp_abi.is_vtable_name) == NULL)
00059     error (_("ABI doesn't define required function is_vtable_name"));
00060   return (*current_cp_abi.is_vtable_name) (name);
00061 }
00062 
00063 int
00064 is_operator_name (const char *name)
00065 {
00066   if ((current_cp_abi.is_operator_name) == NULL)
00067     error (_("ABI doesn't define required function is_operator_name"));
00068   return (*current_cp_abi.is_operator_name) (name);
00069 }
00070 
00071 int
00072 baseclass_offset (struct type *type, int index, const gdb_byte *valaddr,
00073                   int embedded_offset, CORE_ADDR address,
00074                   const struct value *val)
00075 {
00076   volatile struct gdb_exception ex;
00077   int res = 0;
00078 
00079   gdb_assert (current_cp_abi.baseclass_offset != NULL);
00080 
00081   TRY_CATCH (ex, RETURN_MASK_ERROR)
00082     {
00083       res = (*current_cp_abi.baseclass_offset) (type, index, valaddr,
00084                                                 embedded_offset,
00085                                                 address, val);
00086     }
00087 
00088   if (ex.reason < 0 && ex.error == NOT_AVAILABLE_ERROR)
00089     throw_error (NOT_AVAILABLE_ERROR,
00090                  _("Cannot determine virtual baseclass offset "
00091                    "of incomplete object"));
00092   else if (ex.reason < 0)
00093     throw_exception (ex);
00094   else
00095     return res;
00096 }
00097 
00098 struct value *
00099 value_virtual_fn_field (struct value **arg1p,
00100                         struct fn_field *f, int j,
00101                         struct type *type, int offset)
00102 {
00103   if ((current_cp_abi.virtual_fn_field) == NULL)
00104     return NULL;
00105   return (*current_cp_abi.virtual_fn_field) (arg1p, f, j,
00106                                              type, offset);
00107 }
00108 
00109 struct type *
00110 value_rtti_type (struct value *v, int *full,
00111                  int *top, int *using_enc)
00112 {
00113   struct type *ret = NULL;
00114   volatile struct gdb_exception e;
00115 
00116   if ((current_cp_abi.rtti_type) == NULL)
00117     return NULL;
00118   TRY_CATCH (e, RETURN_MASK_ERROR)
00119     {
00120       ret = (*current_cp_abi.rtti_type) (v, full, top, using_enc);
00121     }
00122   if (e.reason < 0)
00123     return NULL;
00124   return ret;
00125 }
00126 
00127 void
00128 cplus_print_method_ptr (const gdb_byte *contents,
00129                         struct type *type,
00130                         struct ui_file *stream)
00131 {
00132   if (current_cp_abi.print_method_ptr == NULL)
00133     error (_("GDB does not support pointers to methods on this target"));
00134   (*current_cp_abi.print_method_ptr) (contents, type, stream);
00135 }
00136 
00137 int
00138 cplus_method_ptr_size (struct type *to_type)
00139 {
00140   if (current_cp_abi.method_ptr_size == NULL)
00141     error (_("GDB does not support pointers to methods on this target"));
00142   return (*current_cp_abi.method_ptr_size) (to_type);
00143 }
00144 
00145 void
00146 cplus_make_method_ptr (struct type *type, gdb_byte *contents,
00147                        CORE_ADDR value, int is_virtual)
00148 {
00149   if (current_cp_abi.make_method_ptr == NULL)
00150     error (_("GDB does not support pointers to methods on this target"));
00151   (*current_cp_abi.make_method_ptr) (type, contents, value, is_virtual);
00152 }
00153 
00154 CORE_ADDR
00155 cplus_skip_trampoline (struct frame_info *frame,
00156                        CORE_ADDR stop_pc)
00157 {
00158   if (current_cp_abi.skip_trampoline == NULL)
00159     return 0;
00160   return (*current_cp_abi.skip_trampoline) (frame, stop_pc);
00161 }
00162 
00163 struct value *
00164 cplus_method_ptr_to_value (struct value **this_p,
00165                            struct value *method_ptr)
00166 {
00167   if (current_cp_abi.method_ptr_to_value == NULL)
00168     error (_("GDB does not support pointers to methods on this target"));
00169   return (*current_cp_abi.method_ptr_to_value) (this_p, method_ptr);
00170 }
00171 
00172 /* See cp-abi.h.  */
00173 
00174 void
00175 cplus_print_vtable (struct value *value)
00176 {
00177   if (current_cp_abi.print_vtable == NULL)
00178     error (_("GDB cannot print the vtable on this target"));
00179   (*current_cp_abi.print_vtable) (value);
00180 }
00181 
00182 /* See cp-abi.h.  */
00183 
00184 struct value *
00185 cplus_typeid (struct value *value)
00186 {
00187   if (current_cp_abi.get_typeid == NULL)
00188     error (_("GDB cannot find the typeid on this target"));
00189   return (*current_cp_abi.get_typeid) (value);
00190 }
00191 
00192 /* See cp-abi.h.  */
00193 
00194 struct type *
00195 cplus_typeid_type (struct gdbarch *gdbarch)
00196 {
00197   if (current_cp_abi.get_typeid_type == NULL)
00198     error (_("GDB cannot find the type for 'typeid' on this target"));
00199   return (*current_cp_abi.get_typeid_type) (gdbarch);
00200 }
00201 
00202 /* See cp-abi.h.  */
00203 
00204 struct type *
00205 cplus_type_from_type_info (struct value *value)
00206 {
00207   if (current_cp_abi.get_type_from_type_info == NULL)
00208     error (_("GDB cannot find the type from a std::type_info on this target"));
00209   return (*current_cp_abi.get_type_from_type_info) (value);
00210 }
00211 
00212 /* See cp-abi.h.  */
00213 
00214 char *
00215 cplus_typename_from_type_info (struct value *value)
00216 {
00217   if (current_cp_abi.get_typename_from_type_info == NULL)
00218     error (_("GDB cannot find the type name "
00219              "from a std::type_info on this target"));
00220   return (*current_cp_abi.get_typename_from_type_info) (value);
00221 }
00222 
00223 int
00224 cp_pass_by_reference (struct type *type)
00225 {
00226   if ((current_cp_abi.pass_by_reference) == NULL)
00227     return 0;
00228   return (*current_cp_abi.pass_by_reference) (type);
00229 }
00230 
00231 /* Set the current C++ ABI to SHORT_NAME.  */
00232 
00233 static int
00234 switch_to_cp_abi (const char *short_name)
00235 {
00236   struct cp_abi_ops *abi;
00237 
00238   abi = find_cp_abi (short_name);
00239   if (abi == NULL)
00240     return 0;
00241 
00242   current_cp_abi = *abi;
00243   return 1;
00244 }
00245 
00246 /* Add ABI to the list of supported C++ ABI's.  */
00247 
00248 int
00249 register_cp_abi (struct cp_abi_ops *abi)
00250 {
00251   if (num_cp_abis == CP_ABI_MAX)
00252     internal_error (__FILE__, __LINE__,
00253                     _("Too many C++ ABIs, please increase "
00254                       "CP_ABI_MAX in cp-abi.c"));
00255 
00256   cp_abis[num_cp_abis++] = abi;
00257 
00258   return 1;
00259 }
00260 
00261 /* Set the ABI to use in "auto" mode to SHORT_NAME.  */
00262 
00263 void
00264 set_cp_abi_as_auto_default (const char *short_name)
00265 {
00266   char *new_longname, *new_doc;
00267   struct cp_abi_ops *abi = find_cp_abi (short_name);
00268 
00269   if (abi == NULL)
00270     internal_error (__FILE__, __LINE__,
00271                     _("Cannot find C++ ABI \"%s\" to set it as auto default."),
00272                     short_name);
00273 
00274   if (auto_cp_abi.longname != NULL)
00275     xfree ((char *) auto_cp_abi.longname);
00276   if (auto_cp_abi.doc != NULL)
00277     xfree ((char *) auto_cp_abi.doc);
00278 
00279   auto_cp_abi = *abi;
00280 
00281   auto_cp_abi.shortname = "auto";
00282   new_longname = xstrprintf ("currently \"%s\"", abi->shortname);
00283   auto_cp_abi.longname = new_longname;
00284 
00285   new_doc = xstrprintf ("Automatically selected; currently \"%s\"",
00286              abi->shortname);
00287   auto_cp_abi.doc = new_doc;
00288 
00289   /* Since we copy the current ABI into current_cp_abi instead of
00290      using a pointer, if auto is currently the default, we need to
00291      reset it.  */
00292   if (strcmp (current_cp_abi.shortname, "auto") == 0)
00293     switch_to_cp_abi ("auto");
00294 }
00295 
00296 /* Return the ABI operations associated with SHORT_NAME.  */
00297 
00298 static struct cp_abi_ops *
00299 find_cp_abi (const char *short_name)
00300 {
00301   int i;
00302 
00303   for (i = 0; i < num_cp_abis; i++)
00304     if (strcmp (cp_abis[i]->shortname, short_name) == 0)
00305       return cp_abis[i];
00306 
00307   return NULL;
00308 }
00309 
00310 /* Display the list of registered C++ ABIs.  */
00311 
00312 static void
00313 list_cp_abis (int from_tty)
00314 {
00315   struct ui_out *uiout = current_uiout;
00316   struct cleanup *cleanup_chain;
00317   int i;
00318 
00319   ui_out_text (uiout, "The available C++ ABIs are:\n");
00320   cleanup_chain = make_cleanup_ui_out_tuple_begin_end (uiout,
00321                                                        "cp-abi-list");
00322   for (i = 0; i < num_cp_abis; i++)
00323     {
00324       char pad[14];
00325       int padcount;
00326 
00327       ui_out_text (uiout, "  ");
00328       ui_out_field_string (uiout, "cp-abi", cp_abis[i]->shortname);
00329 
00330       padcount = 16 - 2 - strlen (cp_abis[i]->shortname);
00331       pad[padcount] = 0;
00332       while (padcount > 0)
00333         pad[--padcount] = ' ';
00334       ui_out_text (uiout, pad);
00335 
00336       ui_out_field_string (uiout, "doc", cp_abis[i]->doc);
00337       ui_out_text (uiout, "\n");
00338     }
00339   do_cleanups (cleanup_chain);
00340 }
00341 
00342 /* Set the current C++ ABI, or display the list of options if no
00343    argument is given.  */
00344 
00345 static void
00346 set_cp_abi_cmd (char *args, int from_tty)
00347 {
00348   if (args == NULL)
00349     {
00350       list_cp_abis (from_tty);
00351       return;
00352     }
00353 
00354   if (!switch_to_cp_abi (args))
00355     error (_("Could not find \"%s\" in ABI list"), args);
00356 }
00357 
00358 /* A completion function for "set cp-abi".  */
00359 
00360 static VEC (char_ptr) *
00361 cp_abi_completer (struct cmd_list_element *ignore,
00362                   const char *text, const char *word)
00363 {
00364   static const char **cp_abi_names;
00365 
00366   if (cp_abi_names == NULL)
00367     {
00368       int i;
00369 
00370       cp_abi_names = XNEWVEC (const char *, num_cp_abis + 1);
00371       for (i = 0; i < num_cp_abis; ++i)
00372         cp_abi_names[i] = cp_abis[i]->shortname;
00373       cp_abi_names[i] = NULL;
00374     }
00375 
00376   return complete_on_enum (cp_abi_names, text, word);
00377 }
00378 
00379 /* Show the currently selected C++ ABI.  */
00380 
00381 static void
00382 show_cp_abi_cmd (char *args, int from_tty)
00383 {
00384   struct ui_out *uiout = current_uiout;
00385 
00386   ui_out_text (uiout, "The currently selected C++ ABI is \"");
00387 
00388   ui_out_field_string (uiout, "cp-abi", current_cp_abi.shortname);
00389   ui_out_text (uiout, "\" (");
00390   ui_out_field_string (uiout, "longname", current_cp_abi.longname);
00391   ui_out_text (uiout, ").\n");
00392 }
00393 
00394 extern initialize_file_ftype _initialize_cp_abi; /* -Wmissing-prototypes */
00395 
00396 void
00397 _initialize_cp_abi (void)
00398 {
00399   struct cmd_list_element *c;
00400 
00401   register_cp_abi (&auto_cp_abi);
00402   switch_to_cp_abi ("auto");
00403 
00404   c = add_cmd ("cp-abi", class_obscure, set_cp_abi_cmd, _("\
00405 Set the ABI used for inspecting C++ objects.\n\
00406 \"set cp-abi\" with no arguments will list the available ABIs."),
00407                &setlist);
00408   set_cmd_completer (c, cp_abi_completer);
00409 
00410   add_cmd ("cp-abi", class_obscure, show_cp_abi_cmd,
00411            _("Show the ABI used for inspecting C++ objects."),
00412            &showlist);
00413 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines